1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2020 Mellanox Technologies. */
3 
4 #include <linux/netdevice.h>
5 #include <linux/if_macvlan.h>
6 #include <linux/list.h>
7 #include <linux/rculist.h>
8 #include <linux/rtnetlink.h>
9 #include <linux/workqueue.h>
10 #include <linux/spinlock.h>
11 #include "tc.h"
12 #include "neigh.h"
13 #include "en_rep.h"
14 #include "eswitch.h"
15 #include "lib/fs_chains.h"
16 #include "en/tc_ct.h"
17 #include "en/mapping.h"
18 #include "en/tc_tun.h"
19 #include "lib/port_tun.h"
20 #include "en/tc/sample.h"
21 #include "en_accel/ipsec_rxtx.h"
22 #include "en/tc/int_port.h"
23 #include "en/tc/act/act.h"
24 
25 struct mlx5e_rep_indr_block_priv {
26 	struct net_device *netdev;
27 	struct mlx5e_rep_priv *rpriv;
28 	enum flow_block_binder_type binder_type;
29 
30 	struct list_head list;
31 };
32 
mlx5e_rep_encap_entry_attach(struct mlx5e_priv * priv,struct mlx5e_encap_entry * e,struct mlx5e_neigh * m_neigh,struct net_device * neigh_dev)33 int mlx5e_rep_encap_entry_attach(struct mlx5e_priv *priv,
34 				 struct mlx5e_encap_entry *e,
35 				 struct mlx5e_neigh *m_neigh,
36 				 struct net_device *neigh_dev)
37 {
38 	struct mlx5e_rep_priv *rpriv = priv->ppriv;
39 	struct mlx5_rep_uplink_priv *uplink_priv = &rpriv->uplink_priv;
40 	struct mlx5_tun_entropy *tun_entropy = &uplink_priv->tun_entropy;
41 	struct mlx5e_neigh_hash_entry *nhe;
42 	int err;
43 
44 	err = mlx5_tun_entropy_refcount_inc(tun_entropy, e->reformat_type);
45 	if (err)
46 		return err;
47 
48 	mutex_lock(&rpriv->neigh_update.encap_lock);
49 	nhe = mlx5e_rep_neigh_entry_lookup(priv, m_neigh);
50 	if (!nhe) {
51 		err = mlx5e_rep_neigh_entry_create(priv, m_neigh, neigh_dev, &nhe);
52 		if (err) {
53 			mutex_unlock(&rpriv->neigh_update.encap_lock);
54 			mlx5_tun_entropy_refcount_dec(tun_entropy,
55 						      e->reformat_type);
56 			return err;
57 		}
58 	}
59 
60 	e->nhe = nhe;
61 	spin_lock(&nhe->encap_list_lock);
62 	list_add_rcu(&e->encap_list, &nhe->encap_list);
63 	spin_unlock(&nhe->encap_list_lock);
64 
65 	mutex_unlock(&rpriv->neigh_update.encap_lock);
66 
67 	return 0;
68 }
69 
mlx5e_rep_encap_entry_detach(struct mlx5e_priv * priv,struct mlx5e_encap_entry * e)70 void mlx5e_rep_encap_entry_detach(struct mlx5e_priv *priv,
71 				  struct mlx5e_encap_entry *e)
72 {
73 	struct mlx5e_rep_priv *rpriv = priv->ppriv;
74 	struct mlx5_rep_uplink_priv *uplink_priv = &rpriv->uplink_priv;
75 	struct mlx5_tun_entropy *tun_entropy = &uplink_priv->tun_entropy;
76 
77 	if (!e->nhe)
78 		return;
79 
80 	spin_lock(&e->nhe->encap_list_lock);
81 	list_del_rcu(&e->encap_list);
82 	spin_unlock(&e->nhe->encap_list_lock);
83 
84 	mlx5e_rep_neigh_entry_release(e->nhe);
85 	e->nhe = NULL;
86 	mlx5_tun_entropy_refcount_dec(tun_entropy, e->reformat_type);
87 }
88 
mlx5e_rep_update_flows(struct mlx5e_priv * priv,struct mlx5e_encap_entry * e,bool neigh_connected,unsigned char ha[ETH_ALEN])89 void mlx5e_rep_update_flows(struct mlx5e_priv *priv,
90 			    struct mlx5e_encap_entry *e,
91 			    bool neigh_connected,
92 			    unsigned char ha[ETH_ALEN])
93 {
94 	struct ethhdr *eth = (struct ethhdr *)e->encap_header;
95 	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
96 	bool encap_connected;
97 	LIST_HEAD(flow_list);
98 
99 	ASSERT_RTNL();
100 
101 	mutex_lock(&esw->offloads.encap_tbl_lock);
102 	encap_connected = !!(e->flags & MLX5_ENCAP_ENTRY_VALID);
103 	if (encap_connected == neigh_connected && ether_addr_equal(e->h_dest, ha))
104 		goto unlock;
105 
106 	mlx5e_take_all_encap_flows(e, &flow_list);
107 
108 	if ((e->flags & MLX5_ENCAP_ENTRY_VALID) &&
109 	    (!neigh_connected || !ether_addr_equal(e->h_dest, ha)))
110 		mlx5e_tc_encap_flows_del(priv, e, &flow_list);
111 
112 	if (neigh_connected && !(e->flags & MLX5_ENCAP_ENTRY_VALID)) {
113 		struct net_device *route_dev;
114 
115 		ether_addr_copy(e->h_dest, ha);
116 		ether_addr_copy(eth->h_dest, ha);
117 		/* Update the encap source mac, in case that we delete
118 		 * the flows when encap source mac changed.
119 		 */
120 		route_dev = __dev_get_by_index(dev_net(priv->netdev), e->route_dev_ifindex);
121 		if (route_dev)
122 			ether_addr_copy(eth->h_source, route_dev->dev_addr);
123 
124 		mlx5e_tc_encap_flows_add(priv, e, &flow_list);
125 	}
126 unlock:
127 	mutex_unlock(&esw->offloads.encap_tbl_lock);
128 	mlx5e_put_flow_list(priv, &flow_list);
129 }
130 
131 static int
mlx5e_rep_setup_tc_cls_flower(struct mlx5e_priv * priv,struct flow_cls_offload * cls_flower,int flags)132 mlx5e_rep_setup_tc_cls_flower(struct mlx5e_priv *priv,
133 			      struct flow_cls_offload *cls_flower, int flags)
134 {
135 	switch (cls_flower->command) {
136 	case FLOW_CLS_REPLACE:
137 		return mlx5e_configure_flower(priv->netdev, priv, cls_flower,
138 					      flags);
139 	case FLOW_CLS_DESTROY:
140 		return mlx5e_delete_flower(priv->netdev, priv, cls_flower,
141 					   flags);
142 	case FLOW_CLS_STATS:
143 		return mlx5e_stats_flower(priv->netdev, priv, cls_flower,
144 					  flags);
145 	default:
146 		return -EOPNOTSUPP;
147 	}
148 }
149 
150 static
mlx5e_rep_setup_tc_cls_matchall(struct mlx5e_priv * priv,struct tc_cls_matchall_offload * ma)151 int mlx5e_rep_setup_tc_cls_matchall(struct mlx5e_priv *priv,
152 				    struct tc_cls_matchall_offload *ma)
153 {
154 	switch (ma->command) {
155 	case TC_CLSMATCHALL_REPLACE:
156 		return mlx5e_tc_configure_matchall(priv, ma);
157 	case TC_CLSMATCHALL_DESTROY:
158 		return mlx5e_tc_delete_matchall(priv, ma);
159 	case TC_CLSMATCHALL_STATS:
160 		mlx5e_tc_stats_matchall(priv, ma);
161 		return 0;
162 	default:
163 		return -EOPNOTSUPP;
164 	}
165 }
166 
mlx5e_rep_setup_tc_cb(enum tc_setup_type type,void * type_data,void * cb_priv)167 static int mlx5e_rep_setup_tc_cb(enum tc_setup_type type, void *type_data,
168 				 void *cb_priv)
169 {
170 	unsigned long flags = MLX5_TC_FLAG(INGRESS) | MLX5_TC_FLAG(ESW_OFFLOAD);
171 	struct mlx5e_priv *priv = cb_priv;
172 
173 	if (!priv->netdev || !netif_device_present(priv->netdev))
174 		return -EOPNOTSUPP;
175 
176 	switch (type) {
177 	case TC_SETUP_CLSFLOWER:
178 		return mlx5e_rep_setup_tc_cls_flower(priv, type_data, flags);
179 	case TC_SETUP_CLSMATCHALL:
180 		return mlx5e_rep_setup_tc_cls_matchall(priv, type_data);
181 	default:
182 		return -EOPNOTSUPP;
183 	}
184 }
185 
mlx5e_rep_setup_ft_cb(enum tc_setup_type type,void * type_data,void * cb_priv)186 static int mlx5e_rep_setup_ft_cb(enum tc_setup_type type, void *type_data,
187 				 void *cb_priv)
188 {
189 	struct flow_cls_offload tmp, *f = type_data;
190 	struct mlx5e_priv *priv = cb_priv;
191 	struct mlx5_eswitch *esw;
192 	unsigned long flags;
193 	int err;
194 
195 	flags = MLX5_TC_FLAG(INGRESS) |
196 		MLX5_TC_FLAG(ESW_OFFLOAD) |
197 		MLX5_TC_FLAG(FT_OFFLOAD);
198 	esw = priv->mdev->priv.eswitch;
199 
200 	switch (type) {
201 	case TC_SETUP_CLSFLOWER:
202 		memcpy(&tmp, f, sizeof(*f));
203 
204 		if (!mlx5_chains_prios_supported(esw_chains(esw)))
205 			return -EOPNOTSUPP;
206 
207 		/* Re-use tc offload path by moving the ft flow to the
208 		 * reserved ft chain.
209 		 *
210 		 * FT offload can use prio range [0, INT_MAX], so we normalize
211 		 * it to range [1, mlx5_esw_chains_get_prio_range(esw)]
212 		 * as with tc, where prio 0 isn't supported.
213 		 *
214 		 * We only support chain 0 of FT offload.
215 		 */
216 		if (tmp.common.prio >= mlx5_chains_get_prio_range(esw_chains(esw)))
217 			return -EOPNOTSUPP;
218 		if (tmp.common.chain_index != 0)
219 			return -EOPNOTSUPP;
220 
221 		tmp.common.chain_index = mlx5_chains_get_nf_ft_chain(esw_chains(esw));
222 		tmp.common.prio++;
223 		err = mlx5e_rep_setup_tc_cls_flower(priv, &tmp, flags);
224 		memcpy(&f->stats, &tmp.stats, sizeof(f->stats));
225 		return err;
226 	default:
227 		return -EOPNOTSUPP;
228 	}
229 }
230 
231 static LIST_HEAD(mlx5e_rep_block_tc_cb_list);
232 static LIST_HEAD(mlx5e_rep_block_ft_cb_list);
mlx5e_rep_setup_tc(struct net_device * dev,enum tc_setup_type type,void * type_data)233 int mlx5e_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
234 		       void *type_data)
235 {
236 	struct mlx5e_priv *priv = netdev_priv(dev);
237 	struct flow_block_offload *f = type_data;
238 
239 	f->unlocked_driver_cb = true;
240 
241 	switch (type) {
242 	case TC_SETUP_BLOCK:
243 		return flow_block_cb_setup_simple(type_data,
244 						  &mlx5e_rep_block_tc_cb_list,
245 						  mlx5e_rep_setup_tc_cb,
246 						  priv, priv, true);
247 	case TC_SETUP_FT:
248 		return flow_block_cb_setup_simple(type_data,
249 						  &mlx5e_rep_block_ft_cb_list,
250 						  mlx5e_rep_setup_ft_cb,
251 						  priv, priv, true);
252 	default:
253 		return -EOPNOTSUPP;
254 	}
255 }
256 
mlx5e_rep_tc_init(struct mlx5e_rep_priv * rpriv)257 int mlx5e_rep_tc_init(struct mlx5e_rep_priv *rpriv)
258 {
259 	struct mlx5_rep_uplink_priv *uplink_priv = &rpriv->uplink_priv;
260 	int err;
261 
262 	mutex_init(&uplink_priv->unready_flows_lock);
263 	INIT_LIST_HEAD(&uplink_priv->unready_flows);
264 
265 	/* init shared tc flow table */
266 	err = mlx5e_tc_esw_init(uplink_priv);
267 	return err;
268 }
269 
mlx5e_rep_tc_cleanup(struct mlx5e_rep_priv * rpriv)270 void mlx5e_rep_tc_cleanup(struct mlx5e_rep_priv *rpriv)
271 {
272 	/* delete shared tc flow table */
273 	mlx5e_tc_esw_cleanup(&rpriv->uplink_priv);
274 	mutex_destroy(&rpriv->uplink_priv.unready_flows_lock);
275 }
276 
mlx5e_rep_tc_enable(struct mlx5e_priv * priv)277 void mlx5e_rep_tc_enable(struct mlx5e_priv *priv)
278 {
279 	struct mlx5e_rep_priv *rpriv = priv->ppriv;
280 
281 	INIT_WORK(&rpriv->uplink_priv.reoffload_flows_work,
282 		  mlx5e_tc_reoffload_flows_work);
283 }
284 
mlx5e_rep_tc_disable(struct mlx5e_priv * priv)285 void mlx5e_rep_tc_disable(struct mlx5e_priv *priv)
286 {
287 	struct mlx5e_rep_priv *rpriv = priv->ppriv;
288 
289 	cancel_work_sync(&rpriv->uplink_priv.reoffload_flows_work);
290 }
291 
mlx5e_rep_tc_event_port_affinity(struct mlx5e_priv * priv)292 int mlx5e_rep_tc_event_port_affinity(struct mlx5e_priv *priv)
293 {
294 	struct mlx5e_rep_priv *rpriv = priv->ppriv;
295 
296 	queue_work(priv->wq, &rpriv->uplink_priv.reoffload_flows_work);
297 
298 	return NOTIFY_OK;
299 }
300 
301 static struct mlx5e_rep_indr_block_priv *
mlx5e_rep_indr_block_priv_lookup(struct mlx5e_rep_priv * rpriv,struct net_device * netdev,enum flow_block_binder_type binder_type)302 mlx5e_rep_indr_block_priv_lookup(struct mlx5e_rep_priv *rpriv,
303 				 struct net_device *netdev,
304 				 enum flow_block_binder_type binder_type)
305 {
306 	struct mlx5e_rep_indr_block_priv *cb_priv;
307 
308 	list_for_each_entry(cb_priv,
309 			    &rpriv->uplink_priv.tc_indr_block_priv_list,
310 			    list)
311 		if (cb_priv->netdev == netdev &&
312 		    cb_priv->binder_type == binder_type)
313 			return cb_priv;
314 
315 	return NULL;
316 }
317 
318 static int
mlx5e_rep_indr_offload(struct net_device * netdev,struct flow_cls_offload * flower,struct mlx5e_rep_indr_block_priv * indr_priv,unsigned long flags)319 mlx5e_rep_indr_offload(struct net_device *netdev,
320 		       struct flow_cls_offload *flower,
321 		       struct mlx5e_rep_indr_block_priv *indr_priv,
322 		       unsigned long flags)
323 {
324 	struct mlx5e_priv *priv = netdev_priv(indr_priv->rpriv->netdev);
325 	int err = 0;
326 
327 	if (!netif_device_present(indr_priv->rpriv->netdev))
328 		return -EOPNOTSUPP;
329 
330 	switch (flower->command) {
331 	case FLOW_CLS_REPLACE:
332 		err = mlx5e_configure_flower(netdev, priv, flower, flags);
333 		break;
334 	case FLOW_CLS_DESTROY:
335 		err = mlx5e_delete_flower(netdev, priv, flower, flags);
336 		break;
337 	case FLOW_CLS_STATS:
338 		err = mlx5e_stats_flower(netdev, priv, flower, flags);
339 		break;
340 	default:
341 		err = -EOPNOTSUPP;
342 	}
343 
344 	return err;
345 }
346 
mlx5e_rep_indr_setup_tc_cb(enum tc_setup_type type,void * type_data,void * indr_priv)347 static int mlx5e_rep_indr_setup_tc_cb(enum tc_setup_type type,
348 				      void *type_data, void *indr_priv)
349 {
350 	unsigned long flags = MLX5_TC_FLAG(ESW_OFFLOAD);
351 	struct mlx5e_rep_indr_block_priv *priv = indr_priv;
352 
353 	flags |= (priv->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) ?
354 		MLX5_TC_FLAG(EGRESS) :
355 		MLX5_TC_FLAG(INGRESS);
356 
357 	switch (type) {
358 	case TC_SETUP_CLSFLOWER:
359 		return mlx5e_rep_indr_offload(priv->netdev, type_data, priv,
360 					      flags);
361 	default:
362 		return -EOPNOTSUPP;
363 	}
364 }
365 
mlx5e_rep_indr_setup_ft_cb(enum tc_setup_type type,void * type_data,void * indr_priv)366 static int mlx5e_rep_indr_setup_ft_cb(enum tc_setup_type type,
367 				      void *type_data, void *indr_priv)
368 {
369 	struct mlx5e_rep_indr_block_priv *priv = indr_priv;
370 	struct flow_cls_offload *f = type_data;
371 	struct flow_cls_offload tmp;
372 	struct mlx5e_priv *mpriv;
373 	struct mlx5_eswitch *esw;
374 	unsigned long flags;
375 	int err;
376 
377 	mpriv = netdev_priv(priv->rpriv->netdev);
378 	esw = mpriv->mdev->priv.eswitch;
379 
380 	flags = MLX5_TC_FLAG(EGRESS) |
381 		MLX5_TC_FLAG(ESW_OFFLOAD) |
382 		MLX5_TC_FLAG(FT_OFFLOAD);
383 
384 	switch (type) {
385 	case TC_SETUP_CLSFLOWER:
386 		memcpy(&tmp, f, sizeof(*f));
387 
388 		/* Re-use tc offload path by moving the ft flow to the
389 		 * reserved ft chain.
390 		 *
391 		 * FT offload can use prio range [0, INT_MAX], so we normalize
392 		 * it to range [1, mlx5_esw_chains_get_prio_range(esw)]
393 		 * as with tc, where prio 0 isn't supported.
394 		 *
395 		 * We only support chain 0 of FT offload.
396 		 */
397 		if (!mlx5_chains_prios_supported(esw_chains(esw)) ||
398 		    tmp.common.prio >= mlx5_chains_get_prio_range(esw_chains(esw)) ||
399 		    tmp.common.chain_index)
400 			return -EOPNOTSUPP;
401 
402 		tmp.common.chain_index = mlx5_chains_get_nf_ft_chain(esw_chains(esw));
403 		tmp.common.prio++;
404 		err = mlx5e_rep_indr_offload(priv->netdev, &tmp, priv, flags);
405 		memcpy(&f->stats, &tmp.stats, sizeof(f->stats));
406 		return err;
407 	default:
408 		return -EOPNOTSUPP;
409 	}
410 }
411 
mlx5e_rep_indr_block_unbind(void * cb_priv)412 static void mlx5e_rep_indr_block_unbind(void *cb_priv)
413 {
414 	struct mlx5e_rep_indr_block_priv *indr_priv = cb_priv;
415 
416 	list_del(&indr_priv->list);
417 	kfree(indr_priv);
418 }
419 
420 static LIST_HEAD(mlx5e_block_cb_list);
421 
mlx5e_rep_macvlan_mode_supported(const struct net_device * dev)422 static bool mlx5e_rep_macvlan_mode_supported(const struct net_device *dev)
423 {
424 	struct macvlan_dev *macvlan = netdev_priv(dev);
425 
426 	return macvlan->mode == MACVLAN_MODE_PASSTHRU;
427 }
428 
429 static bool
mlx5e_rep_check_indr_block_supported(struct mlx5e_rep_priv * rpriv,struct net_device * netdev,struct flow_block_offload * f)430 mlx5e_rep_check_indr_block_supported(struct mlx5e_rep_priv *rpriv,
431 				     struct net_device *netdev,
432 				     struct flow_block_offload *f)
433 {
434 	struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
435 	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
436 	struct net_device *macvlan_real_dev;
437 
438 	if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS &&
439 	    f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS)
440 		return false;
441 
442 	if (mlx5e_tc_tun_device_to_offload(priv, netdev))
443 		return true;
444 
445 	if (is_vlan_dev(netdev) && vlan_dev_real_dev(netdev) == rpriv->netdev)
446 		return true;
447 
448 	if (netif_is_macvlan(netdev)) {
449 		if (!mlx5e_rep_macvlan_mode_supported(netdev)) {
450 			netdev_warn(netdev, "Offloading ingress filter is supported only with macvlan passthru mode");
451 			return false;
452 		}
453 
454 		macvlan_real_dev = macvlan_dev_real_dev(netdev);
455 
456 		if (macvlan_real_dev == rpriv->netdev)
457 			return true;
458 		if (netif_is_bond_master(macvlan_real_dev))
459 			return true;
460 	}
461 
462 	if (netif_is_ovs_master(netdev) && f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS &&
463 	    mlx5e_tc_int_port_supported(esw))
464 		return true;
465 
466 	return false;
467 }
468 
469 static int
mlx5e_rep_indr_setup_block(struct net_device * netdev,struct Qdisc * sch,struct mlx5e_rep_priv * rpriv,struct flow_block_offload * f,flow_setup_cb_t * setup_cb,void * data,void (* cleanup)(struct flow_block_cb * block_cb))470 mlx5e_rep_indr_setup_block(struct net_device *netdev, struct Qdisc *sch,
471 			   struct mlx5e_rep_priv *rpriv,
472 			   struct flow_block_offload *f,
473 			   flow_setup_cb_t *setup_cb,
474 			   void *data,
475 			   void (*cleanup)(struct flow_block_cb *block_cb))
476 {
477 	struct mlx5e_rep_indr_block_priv *indr_priv;
478 	struct flow_block_cb *block_cb;
479 
480 	if (!mlx5e_rep_check_indr_block_supported(rpriv, netdev, f))
481 		return -EOPNOTSUPP;
482 
483 	f->unlocked_driver_cb = true;
484 	f->driver_block_list = &mlx5e_block_cb_list;
485 
486 	switch (f->command) {
487 	case FLOW_BLOCK_BIND:
488 		indr_priv = mlx5e_rep_indr_block_priv_lookup(rpriv, netdev, f->binder_type);
489 		if (indr_priv)
490 			return -EEXIST;
491 
492 		indr_priv = kmalloc(sizeof(*indr_priv), GFP_KERNEL);
493 		if (!indr_priv)
494 			return -ENOMEM;
495 
496 		indr_priv->netdev = netdev;
497 		indr_priv->rpriv = rpriv;
498 		indr_priv->binder_type = f->binder_type;
499 		list_add(&indr_priv->list,
500 			 &rpriv->uplink_priv.tc_indr_block_priv_list);
501 
502 		block_cb = flow_indr_block_cb_alloc(setup_cb, indr_priv, indr_priv,
503 						    mlx5e_rep_indr_block_unbind,
504 						    f, netdev, sch, data, rpriv,
505 						    cleanup);
506 		if (IS_ERR(block_cb)) {
507 			list_del(&indr_priv->list);
508 			kfree(indr_priv);
509 			return PTR_ERR(block_cb);
510 		}
511 		flow_block_cb_add(block_cb, f);
512 		list_add_tail(&block_cb->driver_list, &mlx5e_block_cb_list);
513 
514 		return 0;
515 	case FLOW_BLOCK_UNBIND:
516 		indr_priv = mlx5e_rep_indr_block_priv_lookup(rpriv, netdev, f->binder_type);
517 		if (!indr_priv)
518 			return -ENOENT;
519 
520 		block_cb = flow_block_cb_lookup(f->block, setup_cb, indr_priv);
521 		if (!block_cb)
522 			return -ENOENT;
523 
524 		flow_indr_block_cb_remove(block_cb, f);
525 		list_del(&block_cb->driver_list);
526 		return 0;
527 	default:
528 		return -EOPNOTSUPP;
529 	}
530 	return 0;
531 }
532 
533 static int
mlx5e_rep_indr_replace_act(struct mlx5e_rep_priv * rpriv,struct flow_offload_action * fl_act)534 mlx5e_rep_indr_replace_act(struct mlx5e_rep_priv *rpriv,
535 			   struct flow_offload_action *fl_act)
536 
537 {
538 	struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
539 	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
540 	enum mlx5_flow_namespace_type ns_type;
541 	struct flow_action_entry *action;
542 	struct mlx5e_tc_act *act;
543 	bool add = false;
544 	int i;
545 
546 	/* There is no use case currently for more than one action (e.g. pedit).
547 	 * when there will be, need to handle cleaning multiple actions on err.
548 	 */
549 	if (!flow_offload_has_one_action(&fl_act->action))
550 		return -EOPNOTSUPP;
551 
552 	if (esw && esw->mode == MLX5_ESWITCH_OFFLOADS)
553 		ns_type = MLX5_FLOW_NAMESPACE_FDB;
554 	else
555 		ns_type = MLX5_FLOW_NAMESPACE_KERNEL;
556 
557 	flow_action_for_each(i, action, &fl_act->action) {
558 		act = mlx5e_tc_act_get(action->id, ns_type);
559 		if (!act)
560 			continue;
561 
562 		if (!act->offload_action)
563 			continue;
564 
565 		if (!act->offload_action(priv, fl_act, action))
566 			add = true;
567 	}
568 
569 	return add ? 0 : -EOPNOTSUPP;
570 }
571 
572 static int
mlx5e_rep_indr_destroy_act(struct mlx5e_rep_priv * rpriv,struct flow_offload_action * fl_act)573 mlx5e_rep_indr_destroy_act(struct mlx5e_rep_priv *rpriv,
574 			   struct flow_offload_action *fl_act)
575 {
576 	struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
577 	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
578 	enum mlx5_flow_namespace_type ns_type;
579 	struct mlx5e_tc_act *act;
580 
581 	if (esw && esw->mode == MLX5_ESWITCH_OFFLOADS)
582 		ns_type = MLX5_FLOW_NAMESPACE_FDB;
583 	else
584 		ns_type = MLX5_FLOW_NAMESPACE_KERNEL;
585 
586 	act = mlx5e_tc_act_get(fl_act->id, ns_type);
587 	if (!act || !act->destroy_action)
588 		return -EOPNOTSUPP;
589 
590 	return act->destroy_action(priv, fl_act);
591 }
592 
593 static int
mlx5e_rep_indr_stats_act(struct mlx5e_rep_priv * rpriv,struct flow_offload_action * fl_act)594 mlx5e_rep_indr_stats_act(struct mlx5e_rep_priv *rpriv,
595 			 struct flow_offload_action *fl_act)
596 
597 {
598 	struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
599 	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
600 	enum mlx5_flow_namespace_type ns_type;
601 	struct mlx5e_tc_act *act;
602 
603 	if (esw && esw->mode == MLX5_ESWITCH_OFFLOADS)
604 		ns_type = MLX5_FLOW_NAMESPACE_FDB;
605 	else
606 		ns_type = MLX5_FLOW_NAMESPACE_KERNEL;
607 
608 	act = mlx5e_tc_act_get(fl_act->id, ns_type);
609 	if (!act || !act->stats_action)
610 		return mlx5e_tc_fill_action_stats(priv, fl_act);
611 
612 	return act->stats_action(priv, fl_act);
613 }
614 
615 static int
mlx5e_rep_indr_setup_act(struct mlx5e_rep_priv * rpriv,struct flow_offload_action * fl_act)616 mlx5e_rep_indr_setup_act(struct mlx5e_rep_priv *rpriv,
617 			 struct flow_offload_action *fl_act)
618 {
619 	switch (fl_act->command) {
620 	case FLOW_ACT_REPLACE:
621 		return mlx5e_rep_indr_replace_act(rpriv, fl_act);
622 	case FLOW_ACT_DESTROY:
623 		return mlx5e_rep_indr_destroy_act(rpriv, fl_act);
624 	case FLOW_ACT_STATS:
625 		return mlx5e_rep_indr_stats_act(rpriv, fl_act);
626 	default:
627 		return -EOPNOTSUPP;
628 	}
629 }
630 
631 static int
mlx5e_rep_indr_no_dev_setup(struct mlx5e_rep_priv * rpriv,enum tc_setup_type type,void * data)632 mlx5e_rep_indr_no_dev_setup(struct mlx5e_rep_priv *rpriv,
633 			    enum tc_setup_type type,
634 			    void *data)
635 {
636 	if (!data)
637 		return -EOPNOTSUPP;
638 
639 	switch (type) {
640 	case TC_SETUP_ACT:
641 		return mlx5e_rep_indr_setup_act(rpriv, data);
642 	default:
643 		return -EOPNOTSUPP;
644 	}
645 }
646 
647 static
mlx5e_rep_indr_setup_cb(struct net_device * netdev,struct Qdisc * sch,void * cb_priv,enum tc_setup_type type,void * type_data,void * data,void (* cleanup)(struct flow_block_cb * block_cb))648 int mlx5e_rep_indr_setup_cb(struct net_device *netdev, struct Qdisc *sch, void *cb_priv,
649 			    enum tc_setup_type type, void *type_data,
650 			    void *data,
651 			    void (*cleanup)(struct flow_block_cb *block_cb))
652 {
653 	if (!netdev)
654 		return mlx5e_rep_indr_no_dev_setup(cb_priv, type, data);
655 
656 	switch (type) {
657 	case TC_SETUP_BLOCK:
658 		return mlx5e_rep_indr_setup_block(netdev, sch, cb_priv, type_data,
659 						  mlx5e_rep_indr_setup_tc_cb,
660 						  data, cleanup);
661 	case TC_SETUP_FT:
662 		return mlx5e_rep_indr_setup_block(netdev, sch, cb_priv, type_data,
663 						  mlx5e_rep_indr_setup_ft_cb,
664 						  data, cleanup);
665 	default:
666 		return -EOPNOTSUPP;
667 	}
668 }
669 
mlx5e_rep_tc_netdevice_event_register(struct mlx5e_rep_priv * rpriv)670 int mlx5e_rep_tc_netdevice_event_register(struct mlx5e_rep_priv *rpriv)
671 {
672 	struct mlx5_rep_uplink_priv *uplink_priv = &rpriv->uplink_priv;
673 
674 	/* init indirect block notifications */
675 	INIT_LIST_HEAD(&uplink_priv->tc_indr_block_priv_list);
676 
677 	return flow_indr_dev_register(mlx5e_rep_indr_setup_cb, rpriv);
678 }
679 
mlx5e_rep_tc_netdevice_event_unregister(struct mlx5e_rep_priv * rpriv)680 void mlx5e_rep_tc_netdevice_event_unregister(struct mlx5e_rep_priv *rpriv)
681 {
682 	flow_indr_dev_unregister(mlx5e_rep_indr_setup_cb, rpriv,
683 				 mlx5e_rep_indr_block_unbind);
684 }
685 
mlx5e_rep_tc_receive(struct mlx5_cqe64 * cqe,struct mlx5e_rq * rq,struct sk_buff * skb)686 void mlx5e_rep_tc_receive(struct mlx5_cqe64 *cqe, struct mlx5e_rq *rq,
687 			  struct sk_buff *skb)
688 {
689 	u32 reg_c0, reg_c1, zone_restore_id, tunnel_id;
690 	struct mlx5e_tc_update_priv tc_priv = {};
691 	struct mlx5_rep_uplink_priv *uplink_priv;
692 	struct mlx5e_rep_priv *uplink_rpriv;
693 	struct mlx5_tc_ct_priv *ct_priv;
694 	struct mapping_ctx *mapping_ctx;
695 	struct mlx5_eswitch *esw;
696 	struct mlx5e_priv *priv;
697 
698 	reg_c0 = (be32_to_cpu(cqe->sop_drop_qpn) & MLX5E_TC_FLOW_ID_MASK);
699 	if (!reg_c0 || reg_c0 == MLX5_FS_DEFAULT_FLOW_TAG)
700 		goto forward;
701 
702 	/* If mapped_obj_id is not equal to the default flow tag then skb->mark
703 	 * is not supported and must be reset back to 0.
704 	 */
705 	skb->mark = 0;
706 
707 	priv = netdev_priv(skb->dev);
708 	esw = priv->mdev->priv.eswitch;
709 	mapping_ctx = esw->offloads.reg_c0_obj_pool;
710 	reg_c1 = be32_to_cpu(cqe->ft_metadata);
711 	zone_restore_id = reg_c1 & ESW_ZONE_ID_MASK;
712 	tunnel_id = (reg_c1 >> ESW_TUN_OFFSET) & TUNNEL_ID_MASK;
713 
714 	uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
715 	uplink_priv = &uplink_rpriv->uplink_priv;
716 	ct_priv = uplink_priv->ct_priv;
717 
718 #ifdef CONFIG_MLX5_EN_IPSEC
719 	if (!(tunnel_id >> ESW_TUN_OPTS_BITS)) {
720 		u32 mapped_id;
721 		u32 metadata;
722 
723 		mapped_id = tunnel_id & ESW_IPSEC_RX_MAPPED_ID_MASK;
724 		if (mapped_id &&
725 		    !mlx5_esw_ipsec_rx_make_metadata(priv, mapped_id, &metadata))
726 			mlx5e_ipsec_offload_handle_rx_skb(priv->netdev, skb, metadata);
727 	}
728 #endif
729 
730 	if (!mlx5e_tc_update_skb(cqe, skb, mapping_ctx, reg_c0, ct_priv,
731 				 zone_restore_id, tunnel_id, &tc_priv))
732 		goto free_skb;
733 
734 forward:
735 	if (tc_priv.skb_done)
736 		goto free_skb;
737 
738 	if (tc_priv.forward_tx)
739 		dev_queue_xmit(skb);
740 	else
741 		napi_gro_receive(rq->cq.napi, skb);
742 
743 	dev_put(tc_priv.fwd_dev);
744 
745 	return;
746 
747 free_skb:
748 	dev_put(tc_priv.fwd_dev);
749 	dev_kfree_skb_any(skb);
750 }
751