1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 // Copyright (c) 2019 Mellanox Technologies.
4 #include <linux/mlx5/fs.h>
9 struct mlx5_termtbl_handle {
10 struct hlist_node termtbl_hlist;
12 struct mlx5_flow_table *termtbl;
13 struct mlx5_flow_act flow_act;
14 struct mlx5_flow_destination dest;
16 struct mlx5_flow_handle *rule;
21 mlx5_eswitch_termtbl_hash(struct mlx5_flow_act *flow_act,
22 struct mlx5_flow_destination *dest)
26 hash = jhash_1word(flow_act->action, 0);
27 hash = jhash((const void *)&flow_act->vlan,
28 sizeof(flow_act->vlan), hash);
29 hash = jhash((const void *)&dest->vport.num,
30 sizeof(dest->vport.num), hash);
31 hash = jhash((const void *)&dest->vport.vhca_id,
32 sizeof(dest->vport.num), hash);
33 if (flow_act->pkt_reformat)
34 hash = jhash(flow_act->pkt_reformat,
35 sizeof(*flow_act->pkt_reformat),
41 mlx5_eswitch_termtbl_cmp(struct mlx5_flow_act *flow_act1,
42 struct mlx5_flow_destination *dest1,
43 struct mlx5_flow_act *flow_act2,
44 struct mlx5_flow_destination *dest2)
48 ret = flow_act1->action != flow_act2->action ||
49 dest1->vport.num != dest2->vport.num ||
50 dest1->vport.vhca_id != dest2->vport.vhca_id ||
51 memcmp(&flow_act1->vlan, &flow_act2->vlan,
52 sizeof(flow_act1->vlan));
56 if (flow_act1->pkt_reformat && flow_act2->pkt_reformat)
57 return memcmp(flow_act1->pkt_reformat, flow_act2->pkt_reformat,
58 sizeof(*flow_act1->pkt_reformat));
60 return !(flow_act1->pkt_reformat == flow_act2->pkt_reformat);
64 mlx5_eswitch_termtbl_create(struct mlx5_core_dev *dev,
65 struct mlx5_termtbl_handle *tt,
66 struct mlx5_flow_act *flow_act)
68 struct mlx5_flow_table_attr ft_attr = {};
69 struct mlx5_flow_namespace *root_ns;
72 root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
74 esw_warn(dev, "Failed to get FDB flow namespace\n");
78 /* As this is the terminating action then the termination table is the
79 * same prio as the slow path
81 ft_attr.flags = MLX5_FLOW_TABLE_TERMINATION | MLX5_FLOW_TABLE_UNMANAGED |
82 MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
83 ft_attr.prio = FDB_TC_OFFLOAD;
86 ft_attr.autogroup.max_num_groups = 1;
87 tt->termtbl = mlx5_create_auto_grouped_flow_table(root_ns, &ft_attr);
88 if (IS_ERR(tt->termtbl)) {
89 err = PTR_ERR(tt->termtbl);
90 esw_warn(dev, "Failed to create termination table, err %pe\n", tt->termtbl);
94 tt->rule = mlx5_add_flow_rules(tt->termtbl, NULL, flow_act,
96 if (IS_ERR(tt->rule)) {
97 err = PTR_ERR(tt->rule);
98 esw_warn(dev, "Failed to create termination table rule, err %pe\n", tt->rule);
104 err2 = mlx5_destroy_flow_table(tt->termtbl);
106 esw_warn(dev, "Failed to destroy termination table, err %d\n", err2);
111 static struct mlx5_termtbl_handle *
112 mlx5_eswitch_termtbl_get_create(struct mlx5_eswitch *esw,
113 struct mlx5_flow_act *flow_act,
114 struct mlx5_flow_destination *dest,
115 struct mlx5_esw_flow_attr *attr)
117 struct mlx5_termtbl_handle *tt;
122 mutex_lock(&esw->offloads.termtbl_mutex);
123 hash_key = mlx5_eswitch_termtbl_hash(flow_act, dest);
124 hash_for_each_possible(esw->offloads.termtbl_tbl, tt,
125 termtbl_hlist, hash_key) {
126 if (!mlx5_eswitch_termtbl_cmp(&tt->flow_act, &tt->dest,
135 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
141 tt->dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
142 tt->dest.vport.num = dest->vport.num;
143 tt->dest.vport.vhca_id = dest->vport.vhca_id;
144 tt->dest.vport.flags = dest->vport.flags;
145 memcpy(&tt->flow_act, flow_act, sizeof(*flow_act));
147 err = mlx5_eswitch_termtbl_create(esw->dev, tt, flow_act);
151 hash_add(esw->offloads.termtbl_tbl, &tt->termtbl_hlist, hash_key);
154 mutex_unlock(&esw->offloads.termtbl_mutex);
158 mutex_unlock(&esw->offloads.termtbl_mutex);
163 mlx5_eswitch_termtbl_put(struct mlx5_eswitch *esw,
164 struct mlx5_termtbl_handle *tt)
166 mutex_lock(&esw->offloads.termtbl_mutex);
167 if (--tt->ref_count == 0)
168 hash_del(&tt->termtbl_hlist);
169 mutex_unlock(&esw->offloads.termtbl_mutex);
171 if (!tt->ref_count) {
172 mlx5_del_flow_rules(tt->rule);
173 mlx5_destroy_flow_table(tt->termtbl);
179 mlx5_eswitch_termtbl_actions_move(struct mlx5_flow_act *src,
180 struct mlx5_flow_act *dst)
182 if (src->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) {
183 src->action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
184 dst->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
185 memcpy(&dst->vlan[0], &src->vlan[0], sizeof(src->vlan[0]));
186 memset(&src->vlan[0], 0, sizeof(src->vlan[0]));
188 if (src->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2) {
189 src->action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2;
190 dst->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2;
191 memcpy(&dst->vlan[1], &src->vlan[1], sizeof(src->vlan[1]));
192 memset(&src->vlan[1], 0, sizeof(src->vlan[1]));
197 static bool mlx5_eswitch_offload_is_uplink_port(const struct mlx5_eswitch *esw,
198 const struct mlx5_flow_spec *spec)
200 u16 port_mask, port_value;
202 if (MLX5_CAP_ESW_FLOWTABLE(esw->dev, flow_source))
203 return spec->flow_context.flow_source ==
204 MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK;
206 port_mask = MLX5_GET(fte_match_param, spec->match_criteria,
207 misc_parameters.source_port);
208 port_value = MLX5_GET(fte_match_param, spec->match_value,
209 misc_parameters.source_port);
210 return (port_mask & port_value) == MLX5_VPORT_UPLINK;
214 mlx5_eswitch_termtbl_required(struct mlx5_eswitch *esw,
215 struct mlx5_flow_attr *attr,
216 struct mlx5_flow_act *flow_act,
217 struct mlx5_flow_spec *spec)
219 struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
222 if (!MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, termination_table) ||
223 !MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, ignore_flow_level) ||
224 mlx5e_tc_attr_flags_skip(attr->flags) ||
225 (!mlx5_eswitch_offload_is_uplink_port(esw, spec) && !esw_attr->int_port))
228 /* push vlan on RX */
229 if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH &&
230 !(mlx5_fs_get_capabilities(esw->dev, MLX5_FLOW_NAMESPACE_FDB) &
231 MLX5_FLOW_STEERING_CAP_VLAN_PUSH_ON_RX))
235 for (i = esw_attr->split_count; i < esw_attr->out_count; i++)
236 if (!esw_attr->dest_int_port && esw_attr->dests[i].vport_valid &&
237 esw_attr->dests[i].vport == MLX5_VPORT_UPLINK)
243 struct mlx5_flow_handle *
244 mlx5_eswitch_add_termtbl_rule(struct mlx5_eswitch *esw,
245 struct mlx5_flow_table *fdb,
246 struct mlx5_flow_spec *spec,
247 struct mlx5_esw_flow_attr *attr,
248 struct mlx5_flow_act *flow_act,
249 struct mlx5_flow_destination *dest,
252 struct mlx5_flow_act term_tbl_act = {};
253 struct mlx5_flow_handle *rule = NULL;
254 bool term_table_created = false;
255 int num_vport_dests = 0;
258 mlx5_eswitch_termtbl_actions_move(flow_act, &term_tbl_act);
259 term_tbl_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
261 for (i = 0; i < num_dest; i++) {
262 struct mlx5_termtbl_handle *tt;
264 /* only vport destinations can be terminated */
265 if (dest[i].type != MLX5_FLOW_DESTINATION_TYPE_VPORT)
268 if (attr->dests[num_vport_dests].flags & MLX5_ESW_DEST_ENCAP) {
269 term_tbl_act.action |= MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
270 term_tbl_act.pkt_reformat = attr->dests[num_vport_dests].pkt_reformat;
272 term_tbl_act.action &= ~MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
273 term_tbl_act.pkt_reformat = NULL;
276 /* get the terminating table for the action list */
277 tt = mlx5_eswitch_termtbl_get_create(esw, &term_tbl_act,
280 esw_warn(esw->dev, "Failed to get termination table, err %pe\n", tt);
283 attr->dests[num_vport_dests].termtbl = tt;
286 /* link the destination with the termination table */
287 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
288 dest[i].ft = tt->termtbl;
289 term_table_created = true;
292 /* at least one destination should reference a termination table */
293 if (!term_table_created)
297 flow_act->action &= ~MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
298 flow_act->pkt_reformat = NULL;
299 flow_act->flags |= FLOW_ACT_IGNORE_FLOW_LEVEL;
300 rule = mlx5_add_flow_rules(fdb, spec, flow_act, dest, num_dest);
307 /* revert the changes that were made to the original flow_act
308 * and fall-back to the original rule actions
310 mlx5_eswitch_termtbl_actions_move(&term_tbl_act, flow_act);
312 for (curr_dest = 0; curr_dest < num_vport_dests; curr_dest++) {
313 struct mlx5_termtbl_handle *tt = attr->dests[curr_dest].termtbl;
315 attr->dests[curr_dest].termtbl = NULL;
317 /* search for the destination associated with the
320 for (i = 0; i < num_dest; i++) {
321 if (dest[i].ft != tt->termtbl)
324 memset(&dest[i], 0, sizeof(dest[i]));
325 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
326 dest[i].vport.num = tt->dest.vport.num;
327 dest[i].vport.vhca_id = tt->dest.vport.vhca_id;
328 mlx5_eswitch_termtbl_put(esw, tt);
332 rule = mlx5_add_flow_rules(fdb, spec, flow_act, dest, num_dest);