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