1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 // Copyright (c) 2019 Mellanox Technologies.
4 #include <linux/mlx5/fs.h>
8 struct mlx5_termtbl_handle {
9 struct hlist_node termtbl_hlist;
11 struct mlx5_flow_table *termtbl;
12 struct mlx5_flow_act flow_act;
13 struct mlx5_flow_destination dest;
15 struct mlx5_flow_handle *rule;
20 mlx5_eswitch_termtbl_hash(struct mlx5_flow_act *flow_act,
21 struct mlx5_flow_destination *dest)
25 hash = jhash_1word(flow_act->action, 0);
26 hash = jhash((const void *)&flow_act->vlan,
27 sizeof(flow_act->vlan), hash);
28 hash = jhash((const void *)&dest->vport.num,
29 sizeof(dest->vport.num), hash);
30 hash = jhash((const void *)&dest->vport.vhca_id,
31 sizeof(dest->vport.num), hash);
32 if (dest->vport.pkt_reformat)
33 hash = jhash(dest->vport.pkt_reformat,
34 sizeof(*dest->vport.pkt_reformat),
40 mlx5_eswitch_termtbl_cmp(struct mlx5_flow_act *flow_act1,
41 struct mlx5_flow_destination *dest1,
42 struct mlx5_flow_act *flow_act2,
43 struct mlx5_flow_destination *dest2)
47 ret = flow_act1->action != flow_act2->action ||
48 dest1->vport.num != dest2->vport.num ||
49 dest1->vport.vhca_id != dest2->vport.vhca_id ||
50 memcmp(&flow_act1->vlan, &flow_act2->vlan,
51 sizeof(flow_act1->vlan));
55 return dest1->vport.pkt_reformat && dest2->vport.pkt_reformat ?
56 memcmp(dest1->vport.pkt_reformat, dest2->vport.pkt_reformat,
57 sizeof(*dest1->vport.pkt_reformat)) : 0;
61 mlx5_eswitch_termtbl_create(struct mlx5_core_dev *dev,
62 struct mlx5_termtbl_handle *tt,
63 struct mlx5_flow_act *flow_act)
65 struct mlx5_flow_table_attr ft_attr = {};
66 struct mlx5_flow_namespace *root_ns;
69 root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
71 esw_warn(dev, "Failed to get FDB flow namespace\n");
75 /* As this is the terminating action then the termination table is the
76 * same prio as the slow path
78 ft_attr.flags = MLX5_FLOW_TABLE_TERMINATION |
79 MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
80 ft_attr.prio = FDB_SLOW_PATH;
82 ft_attr.autogroup.max_num_groups = 1;
83 tt->termtbl = mlx5_create_auto_grouped_flow_table(root_ns, &ft_attr);
84 if (IS_ERR(tt->termtbl)) {
85 esw_warn(dev, "Failed to create termination table\n");
89 tt->rule = mlx5_add_flow_rules(tt->termtbl, NULL, flow_act,
91 if (IS_ERR(tt->rule)) {
92 esw_warn(dev, "Failed to create termination table rule\n");
98 err = mlx5_destroy_flow_table(tt->termtbl);
100 esw_warn(dev, "Failed to destroy termination table\n");
105 static struct mlx5_termtbl_handle *
106 mlx5_eswitch_termtbl_get_create(struct mlx5_eswitch *esw,
107 struct mlx5_flow_act *flow_act,
108 struct mlx5_flow_destination *dest,
109 struct mlx5_esw_flow_attr *attr)
111 struct mlx5_termtbl_handle *tt;
116 mutex_lock(&esw->offloads.termtbl_mutex);
117 hash_key = mlx5_eswitch_termtbl_hash(flow_act, dest);
118 hash_for_each_possible(esw->offloads.termtbl_tbl, tt,
119 termtbl_hlist, hash_key) {
120 if (!mlx5_eswitch_termtbl_cmp(&tt->flow_act, &tt->dest,
129 tt = kzalloc(sizeof(*tt), GFP_KERNEL);
135 tt->dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
136 tt->dest.vport.num = dest->vport.num;
137 tt->dest.vport.vhca_id = dest->vport.vhca_id;
138 tt->dest.vport.flags = dest->vport.flags;
139 memcpy(&tt->flow_act, flow_act, sizeof(*flow_act));
141 err = mlx5_eswitch_termtbl_create(esw->dev, tt, flow_act);
143 esw_warn(esw->dev, "Failed to create termination table\n");
146 hash_add(esw->offloads.termtbl_tbl, &tt->termtbl_hlist, hash_key);
149 mutex_unlock(&esw->offloads.termtbl_mutex);
153 mutex_unlock(&esw->offloads.termtbl_mutex);
158 mlx5_eswitch_termtbl_put(struct mlx5_eswitch *esw,
159 struct mlx5_termtbl_handle *tt)
161 mutex_lock(&esw->offloads.termtbl_mutex);
162 if (--tt->ref_count == 0)
163 hash_del(&tt->termtbl_hlist);
164 mutex_unlock(&esw->offloads.termtbl_mutex);
166 if (!tt->ref_count) {
167 mlx5_del_flow_rules(tt->rule);
168 mlx5_destroy_flow_table(tt->termtbl);
173 static bool mlx5_eswitch_termtbl_is_encap_reformat(struct mlx5_pkt_reformat *rt)
175 switch (rt->reformat_type) {
176 case MLX5_REFORMAT_TYPE_L2_TO_VXLAN:
177 case MLX5_REFORMAT_TYPE_L2_TO_NVGRE:
178 case MLX5_REFORMAT_TYPE_L2_TO_L2_TUNNEL:
179 case MLX5_REFORMAT_TYPE_L2_TO_L3_TUNNEL:
187 mlx5_eswitch_termtbl_actions_move(struct mlx5_flow_act *src,
188 struct mlx5_flow_act *dst)
190 if (src->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) {
191 src->action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
192 dst->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
193 memcpy(&dst->vlan[0], &src->vlan[0], sizeof(src->vlan[0]));
194 memset(&src->vlan[0], 0, sizeof(src->vlan[0]));
196 if (src->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2) {
197 src->action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2;
198 dst->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2;
199 memcpy(&dst->vlan[1], &src->vlan[1], sizeof(src->vlan[1]));
200 memset(&src->vlan[1], 0, sizeof(src->vlan[1]));
204 if (src->action & MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT &&
205 mlx5_eswitch_termtbl_is_encap_reformat(src->pkt_reformat)) {
206 src->action &= ~MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
207 dst->action |= MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
208 dst->pkt_reformat = src->pkt_reformat;
209 src->pkt_reformat = NULL;
213 static bool mlx5_eswitch_offload_is_uplink_port(const struct mlx5_eswitch *esw,
214 const struct mlx5_flow_spec *spec)
216 u16 port_mask, port_value;
218 if (MLX5_CAP_ESW_FLOWTABLE(esw->dev, flow_source))
219 return spec->flow_context.flow_source ==
220 MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK;
222 port_mask = MLX5_GET(fte_match_param, spec->match_criteria,
223 misc_parameters.source_port);
224 port_value = MLX5_GET(fte_match_param, spec->match_value,
225 misc_parameters.source_port);
226 return (port_mask & port_value) == MLX5_VPORT_UPLINK;
230 mlx5_eswitch_termtbl_required(struct mlx5_eswitch *esw,
231 struct mlx5_esw_flow_attr *attr,
232 struct mlx5_flow_act *flow_act,
233 struct mlx5_flow_spec *spec)
237 if (!MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, termination_table) ||
238 attr->flags & MLX5_ESW_ATTR_FLAG_SLOW_PATH ||
239 !mlx5_eswitch_offload_is_uplink_port(esw, spec))
242 /* push vlan on RX */
243 if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH)
247 for (i = attr->split_count; i < attr->out_count; i++)
248 if (attr->dests[i].rep->vport == MLX5_VPORT_UPLINK)
254 struct mlx5_flow_handle *
255 mlx5_eswitch_add_termtbl_rule(struct mlx5_eswitch *esw,
256 struct mlx5_flow_table *fdb,
257 struct mlx5_flow_spec *spec,
258 struct mlx5_esw_flow_attr *attr,
259 struct mlx5_flow_act *flow_act,
260 struct mlx5_flow_destination *dest,
263 struct mlx5_flow_act term_tbl_act = {};
264 struct mlx5_flow_handle *rule = NULL;
265 bool term_table_created = false;
266 int num_vport_dests = 0;
269 mlx5_eswitch_termtbl_actions_move(flow_act, &term_tbl_act);
270 term_tbl_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
272 for (i = 0; i < num_dest; i++) {
273 struct mlx5_termtbl_handle *tt;
275 /* only vport destinations can be terminated */
276 if (dest[i].type != MLX5_FLOW_DESTINATION_TYPE_VPORT)
279 /* get the terminating table for the action list */
280 tt = mlx5_eswitch_termtbl_get_create(esw, &term_tbl_act,
283 esw_warn(esw->dev, "Failed to create termination table\n");
286 attr->dests[num_vport_dests].termtbl = tt;
289 /* link the destination with the termination table */
290 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
291 dest[i].ft = tt->termtbl;
292 term_table_created = true;
295 /* at least one destination should reference a termination table */
296 if (!term_table_created)
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 /* search for the destination associated with the
318 for (i = 0; i < num_dest; i++) {
319 if (dest[i].ft != tt->termtbl)
322 memset(&dest[i], 0, sizeof(dest[i]));
323 dest[i].type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
324 dest[i].vport.num = tt->dest.vport.num;
325 dest[i].vport.vhca_id = tt->dest.vport.vhca_id;
326 mlx5_eswitch_termtbl_put(esw, tt);
330 rule = mlx5_add_flow_rules(fdb, spec, flow_act, dest, num_dest);