1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 // Copyright (c) 2019 Mellanox Technologies.
3 
4 #include <linux/mlx5/fs.h>
5 #include "eswitch.h"
6 #include "en_tc.h"
7 #include "fs_core.h"
8 
9 struct mlx5_termtbl_handle {
10 	struct hlist_node termtbl_hlist;
11 
12 	struct mlx5_flow_table *termtbl;
13 	struct mlx5_flow_act flow_act;
14 	struct mlx5_flow_destination dest;
15 
16 	struct mlx5_flow_handle *rule;
17 	int ref_count;
18 };
19 
20 static u32
21 mlx5_eswitch_termtbl_hash(struct mlx5_flow_act *flow_act,
22 			  struct mlx5_flow_destination *dest)
23 {
24 	u32 hash;
25 
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 (dest->vport.pkt_reformat)
34 		hash = jhash(dest->vport.pkt_reformat,
35 			     sizeof(*dest->vport.pkt_reformat),
36 			     hash);
37 	return hash;
38 }
39 
40 static int
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)
45 {
46 	int ret;
47 
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));
53 	if (ret)
54 		return ret;
55 
56 	return dest1->vport.pkt_reformat && dest2->vport.pkt_reformat ?
57 	       memcmp(dest1->vport.pkt_reformat, dest2->vport.pkt_reformat,
58 		      sizeof(*dest1->vport.pkt_reformat)) : 0;
59 }
60 
61 static int
62 mlx5_eswitch_termtbl_create(struct mlx5_core_dev *dev,
63 			    struct mlx5_termtbl_handle *tt,
64 			    struct mlx5_flow_act *flow_act)
65 {
66 	struct mlx5_flow_table_attr ft_attr = {};
67 	struct mlx5_flow_namespace *root_ns;
68 	int err;
69 
70 	root_ns = mlx5_get_flow_namespace(dev, MLX5_FLOW_NAMESPACE_FDB);
71 	if (!root_ns) {
72 		esw_warn(dev, "Failed to get FDB flow namespace\n");
73 		return -EOPNOTSUPP;
74 	}
75 
76 	/* As this is the terminating action then the termination table is the
77 	 * same prio as the slow path
78 	 */
79 	ft_attr.flags = MLX5_FLOW_TABLE_TERMINATION |
80 			MLX5_FLOW_TABLE_TUNNEL_EN_REFORMAT;
81 	ft_attr.prio = FDB_SLOW_PATH;
82 	ft_attr.max_fte = 1;
83 	ft_attr.autogroup.max_num_groups = 1;
84 	tt->termtbl = mlx5_create_auto_grouped_flow_table(root_ns, &ft_attr);
85 	if (IS_ERR(tt->termtbl)) {
86 		esw_warn(dev, "Failed to create termination table\n");
87 		return -EOPNOTSUPP;
88 	}
89 
90 	tt->rule = mlx5_add_flow_rules(tt->termtbl, NULL, flow_act,
91 				       &tt->dest, 1);
92 	if (IS_ERR(tt->rule)) {
93 		esw_warn(dev, "Failed to create termination table rule\n");
94 		goto add_flow_err;
95 	}
96 	return 0;
97 
98 add_flow_err:
99 	err = mlx5_destroy_flow_table(tt->termtbl);
100 	if (err)
101 		esw_warn(dev, "Failed to destroy termination table\n");
102 
103 	return -EOPNOTSUPP;
104 }
105 
106 static struct mlx5_termtbl_handle *
107 mlx5_eswitch_termtbl_get_create(struct mlx5_eswitch *esw,
108 				struct mlx5_flow_act *flow_act,
109 				struct mlx5_flow_destination *dest,
110 				struct mlx5_esw_flow_attr *attr)
111 {
112 	struct mlx5_termtbl_handle *tt;
113 	bool found = false;
114 	u32 hash_key;
115 	int err;
116 
117 	mutex_lock(&esw->offloads.termtbl_mutex);
118 	hash_key = mlx5_eswitch_termtbl_hash(flow_act, dest);
119 	hash_for_each_possible(esw->offloads.termtbl_tbl, tt,
120 			       termtbl_hlist, hash_key) {
121 		if (!mlx5_eswitch_termtbl_cmp(&tt->flow_act, &tt->dest,
122 					      flow_act, dest)) {
123 			found = true;
124 			break;
125 		}
126 	}
127 	if (found)
128 		goto tt_add_ref;
129 
130 	tt = kzalloc(sizeof(*tt), GFP_KERNEL);
131 	if (!tt) {
132 		err = -ENOMEM;
133 		goto tt_create_err;
134 	}
135 
136 	tt->dest.type = MLX5_FLOW_DESTINATION_TYPE_VPORT;
137 	tt->dest.vport.num = dest->vport.num;
138 	tt->dest.vport.vhca_id = dest->vport.vhca_id;
139 	tt->dest.vport.flags = dest->vport.flags;
140 	memcpy(&tt->flow_act, flow_act, sizeof(*flow_act));
141 
142 	err = mlx5_eswitch_termtbl_create(esw->dev, tt, flow_act);
143 	if (err) {
144 		esw_warn(esw->dev, "Failed to create termination table\n");
145 		goto tt_create_err;
146 	}
147 	hash_add(esw->offloads.termtbl_tbl, &tt->termtbl_hlist, hash_key);
148 tt_add_ref:
149 	tt->ref_count++;
150 	mutex_unlock(&esw->offloads.termtbl_mutex);
151 	return tt;
152 tt_create_err:
153 	kfree(tt);
154 	mutex_unlock(&esw->offloads.termtbl_mutex);
155 	return ERR_PTR(err);
156 }
157 
158 void
159 mlx5_eswitch_termtbl_put(struct mlx5_eswitch *esw,
160 			 struct mlx5_termtbl_handle *tt)
161 {
162 	mutex_lock(&esw->offloads.termtbl_mutex);
163 	if (--tt->ref_count == 0)
164 		hash_del(&tt->termtbl_hlist);
165 	mutex_unlock(&esw->offloads.termtbl_mutex);
166 
167 	if (!tt->ref_count) {
168 		mlx5_del_flow_rules(tt->rule);
169 		mlx5_destroy_flow_table(tt->termtbl);
170 		kfree(tt);
171 	}
172 }
173 
174 static bool mlx5_eswitch_termtbl_is_encap_reformat(struct mlx5_pkt_reformat *rt)
175 {
176 	switch (rt->reformat_type) {
177 	case MLX5_REFORMAT_TYPE_L2_TO_VXLAN:
178 	case MLX5_REFORMAT_TYPE_L2_TO_NVGRE:
179 	case MLX5_REFORMAT_TYPE_L2_TO_L2_TUNNEL:
180 	case MLX5_REFORMAT_TYPE_L2_TO_L3_TUNNEL:
181 		return true;
182 	default:
183 		return false;
184 	}
185 }
186 
187 static void
188 mlx5_eswitch_termtbl_actions_move(struct mlx5_flow_act *src,
189 				  struct mlx5_flow_act *dst)
190 {
191 	if (src->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH) {
192 		src->action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
193 		dst->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH;
194 		memcpy(&dst->vlan[0], &src->vlan[0], sizeof(src->vlan[0]));
195 		memset(&src->vlan[0], 0, sizeof(src->vlan[0]));
196 
197 		if (src->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2) {
198 			src->action &= ~MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2;
199 			dst->action |= MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH_2;
200 			memcpy(&dst->vlan[1], &src->vlan[1], sizeof(src->vlan[1]));
201 			memset(&src->vlan[1], 0, sizeof(src->vlan[1]));
202 		}
203 	}
204 
205 	if (src->action & MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT &&
206 	    mlx5_eswitch_termtbl_is_encap_reformat(src->pkt_reformat)) {
207 		src->action &= ~MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
208 		dst->action |= MLX5_FLOW_CONTEXT_ACTION_PACKET_REFORMAT;
209 		dst->pkt_reformat = src->pkt_reformat;
210 		src->pkt_reformat = NULL;
211 	}
212 }
213 
214 static bool mlx5_eswitch_offload_is_uplink_port(const struct mlx5_eswitch *esw,
215 						const struct mlx5_flow_spec *spec)
216 {
217 	u16 port_mask, port_value;
218 
219 	if (MLX5_CAP_ESW_FLOWTABLE(esw->dev, flow_source))
220 		return spec->flow_context.flow_source ==
221 					MLX5_FLOW_CONTEXT_FLOW_SOURCE_UPLINK;
222 
223 	port_mask = MLX5_GET(fte_match_param, spec->match_criteria,
224 			     misc_parameters.source_port);
225 	port_value = MLX5_GET(fte_match_param, spec->match_value,
226 			      misc_parameters.source_port);
227 	return (port_mask & port_value) == MLX5_VPORT_UPLINK;
228 }
229 
230 bool
231 mlx5_eswitch_termtbl_required(struct mlx5_eswitch *esw,
232 			      struct mlx5_flow_attr *attr,
233 			      struct mlx5_flow_act *flow_act,
234 			      struct mlx5_flow_spec *spec)
235 {
236 	struct mlx5_esw_flow_attr *esw_attr = attr->esw_attr;
237 	int i;
238 
239 	if (!MLX5_CAP_ESW_FLOWTABLE_FDB(esw->dev, termination_table) ||
240 	    attr->flags & MLX5_ESW_ATTR_FLAG_SLOW_PATH ||
241 	    !mlx5_eswitch_offload_is_uplink_port(esw, spec))
242 		return false;
243 
244 	/* push vlan on RX */
245 	if (flow_act->action & MLX5_FLOW_CONTEXT_ACTION_VLAN_PUSH)
246 		return true;
247 
248 	/* hairpin */
249 	for (i = esw_attr->split_count; i < esw_attr->out_count; i++)
250 		if (esw_attr->dests[i].rep->vport == MLX5_VPORT_UPLINK)
251 			return true;
252 
253 	return false;
254 }
255 
256 struct mlx5_flow_handle *
257 mlx5_eswitch_add_termtbl_rule(struct mlx5_eswitch *esw,
258 			      struct mlx5_flow_table *fdb,
259 			      struct mlx5_flow_spec *spec,
260 			      struct mlx5_esw_flow_attr *attr,
261 			      struct mlx5_flow_act *flow_act,
262 			      struct mlx5_flow_destination *dest,
263 			      int num_dest)
264 {
265 	struct mlx5_flow_act term_tbl_act = {};
266 	struct mlx5_flow_handle *rule = NULL;
267 	bool term_table_created = false;
268 	int num_vport_dests = 0;
269 	int i, curr_dest;
270 
271 	mlx5_eswitch_termtbl_actions_move(flow_act, &term_tbl_act);
272 	term_tbl_act.action |= MLX5_FLOW_CONTEXT_ACTION_FWD_DEST;
273 
274 	for (i = 0; i < num_dest; i++) {
275 		struct mlx5_termtbl_handle *tt;
276 
277 		/* only vport destinations can be terminated */
278 		if (dest[i].type != MLX5_FLOW_DESTINATION_TYPE_VPORT)
279 			continue;
280 
281 		/* get the terminating table for the action list */
282 		tt = mlx5_eswitch_termtbl_get_create(esw, &term_tbl_act,
283 						     &dest[i], attr);
284 		if (IS_ERR(tt)) {
285 			esw_warn(esw->dev, "Failed to create termination table\n");
286 			goto revert_changes;
287 		}
288 		attr->dests[num_vport_dests].termtbl = tt;
289 		num_vport_dests++;
290 
291 		/* link the destination with the termination table */
292 		dest[i].type = MLX5_FLOW_DESTINATION_TYPE_FLOW_TABLE;
293 		dest[i].ft = tt->termtbl;
294 		term_table_created = true;
295 	}
296 
297 	/* at least one destination should reference a termination table */
298 	if (!term_table_created)
299 		goto revert_changes;
300 
301 	/* create the FTE */
302 	rule = mlx5_add_flow_rules(fdb, spec, flow_act, dest, num_dest);
303 	if (IS_ERR(rule))
304 		goto revert_changes;
305 
306 	goto out;
307 
308 revert_changes:
309 	/* revert the changes that were made to the original flow_act
310 	 * and fall-back to the original rule actions
311 	 */
312 	mlx5_eswitch_termtbl_actions_move(&term_tbl_act, flow_act);
313 
314 	for (curr_dest = 0; curr_dest < num_vport_dests; curr_dest++) {
315 		struct mlx5_termtbl_handle *tt = attr->dests[curr_dest].termtbl;
316 
317 		/* search for the destination associated with the
318 		 * current term table
319 		 */
320 		for (i = 0; i < num_dest; i++) {
321 			if (dest[i].ft != tt->termtbl)
322 				continue;
323 
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);
329 			break;
330 		}
331 	}
332 	rule = mlx5_add_flow_rules(fdb, spec, flow_act, dest, num_dest);
333 out:
334 	return rule;
335 }
336