• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2 /* Copyright (c) 2020 Mellanox Technologies. */
3 
4 #include <net/dst_metadata.h>
5 #include <linux/netdevice.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 
21 struct mlx5e_rep_indr_block_priv {
22 	struct net_device *netdev;
23 	struct mlx5e_rep_priv *rpriv;
24 
25 	struct list_head list;
26 };
27 
mlx5e_rep_encap_entry_attach(struct mlx5e_priv * priv,struct mlx5e_encap_entry * e)28 int mlx5e_rep_encap_entry_attach(struct mlx5e_priv *priv,
29 				 struct mlx5e_encap_entry *e)
30 {
31 	struct mlx5e_rep_priv *rpriv = priv->ppriv;
32 	struct mlx5_rep_uplink_priv *uplink_priv = &rpriv->uplink_priv;
33 	struct mlx5_tun_entropy *tun_entropy = &uplink_priv->tun_entropy;
34 	struct mlx5e_neigh_hash_entry *nhe;
35 	int err;
36 
37 	err = mlx5_tun_entropy_refcount_inc(tun_entropy, e->reformat_type);
38 	if (err)
39 		return err;
40 
41 	mutex_lock(&rpriv->neigh_update.encap_lock);
42 	nhe = mlx5e_rep_neigh_entry_lookup(priv, &e->m_neigh);
43 	if (!nhe) {
44 		err = mlx5e_rep_neigh_entry_create(priv, e, &nhe);
45 		if (err) {
46 			mutex_unlock(&rpriv->neigh_update.encap_lock);
47 			mlx5_tun_entropy_refcount_dec(tun_entropy,
48 						      e->reformat_type);
49 			return err;
50 		}
51 	}
52 
53 	e->nhe = nhe;
54 	spin_lock(&nhe->encap_list_lock);
55 	list_add_rcu(&e->encap_list, &nhe->encap_list);
56 	spin_unlock(&nhe->encap_list_lock);
57 
58 	mutex_unlock(&rpriv->neigh_update.encap_lock);
59 
60 	return 0;
61 }
62 
mlx5e_rep_encap_entry_detach(struct mlx5e_priv * priv,struct mlx5e_encap_entry * e)63 void mlx5e_rep_encap_entry_detach(struct mlx5e_priv *priv,
64 				  struct mlx5e_encap_entry *e)
65 {
66 	struct mlx5e_rep_priv *rpriv = priv->ppriv;
67 	struct mlx5_rep_uplink_priv *uplink_priv = &rpriv->uplink_priv;
68 	struct mlx5_tun_entropy *tun_entropy = &uplink_priv->tun_entropy;
69 
70 	if (!e->nhe)
71 		return;
72 
73 	spin_lock(&e->nhe->encap_list_lock);
74 	list_del_rcu(&e->encap_list);
75 	spin_unlock(&e->nhe->encap_list_lock);
76 
77 	mlx5e_rep_neigh_entry_release(e->nhe);
78 	e->nhe = NULL;
79 	mlx5_tun_entropy_refcount_dec(tun_entropy, e->reformat_type);
80 }
81 
mlx5e_rep_update_flows(struct mlx5e_priv * priv,struct mlx5e_encap_entry * e,bool neigh_connected,unsigned char ha[ETH_ALEN])82 void mlx5e_rep_update_flows(struct mlx5e_priv *priv,
83 			    struct mlx5e_encap_entry *e,
84 			    bool neigh_connected,
85 			    unsigned char ha[ETH_ALEN])
86 {
87 	struct ethhdr *eth = (struct ethhdr *)e->encap_header;
88 	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
89 	bool encap_connected;
90 	LIST_HEAD(flow_list);
91 
92 	ASSERT_RTNL();
93 
94 	/* wait for encap to be fully initialized */
95 	wait_for_completion(&e->res_ready);
96 
97 	mutex_lock(&esw->offloads.encap_tbl_lock);
98 	encap_connected = !!(e->flags & MLX5_ENCAP_ENTRY_VALID);
99 	if (e->compl_result < 0 || (encap_connected == neigh_connected &&
100 				    ether_addr_equal(e->h_dest, ha)))
101 		goto unlock;
102 
103 	mlx5e_take_all_encap_flows(e, &flow_list);
104 
105 	if ((e->flags & MLX5_ENCAP_ENTRY_VALID) &&
106 	    (!neigh_connected || !ether_addr_equal(e->h_dest, ha)))
107 		mlx5e_tc_encap_flows_del(priv, e, &flow_list);
108 
109 	if (neigh_connected && !(e->flags & MLX5_ENCAP_ENTRY_VALID)) {
110 		struct net_device *route_dev;
111 
112 		ether_addr_copy(e->h_dest, ha);
113 		ether_addr_copy(eth->h_dest, ha);
114 		/* Update the encap source mac, in case that we delete
115 		 * the flows when encap source mac changed.
116 		 */
117 		route_dev = __dev_get_by_index(dev_net(priv->netdev), e->route_dev_ifindex);
118 		if (route_dev)
119 			ether_addr_copy(eth->h_source, route_dev->dev_addr);
120 
121 		mlx5e_tc_encap_flows_add(priv, e, &flow_list);
122 	}
123 unlock:
124 	mutex_unlock(&esw->offloads.encap_tbl_lock);
125 	mlx5e_put_encap_flow_list(priv, &flow_list);
126 }
127 
128 static int
mlx5e_rep_setup_tc_cls_flower(struct mlx5e_priv * priv,struct flow_cls_offload * cls_flower,int flags)129 mlx5e_rep_setup_tc_cls_flower(struct mlx5e_priv *priv,
130 			      struct flow_cls_offload *cls_flower, int flags)
131 {
132 	switch (cls_flower->command) {
133 	case FLOW_CLS_REPLACE:
134 		return mlx5e_configure_flower(priv->netdev, priv, cls_flower,
135 					      flags);
136 	case FLOW_CLS_DESTROY:
137 		return mlx5e_delete_flower(priv->netdev, priv, cls_flower,
138 					   flags);
139 	case FLOW_CLS_STATS:
140 		return mlx5e_stats_flower(priv->netdev, priv, cls_flower,
141 					  flags);
142 	default:
143 		return -EOPNOTSUPP;
144 	}
145 }
146 
147 static
mlx5e_rep_setup_tc_cls_matchall(struct mlx5e_priv * priv,struct tc_cls_matchall_offload * ma)148 int mlx5e_rep_setup_tc_cls_matchall(struct mlx5e_priv *priv,
149 				    struct tc_cls_matchall_offload *ma)
150 {
151 	switch (ma->command) {
152 	case TC_CLSMATCHALL_REPLACE:
153 		return mlx5e_tc_configure_matchall(priv, ma);
154 	case TC_CLSMATCHALL_DESTROY:
155 		return mlx5e_tc_delete_matchall(priv, ma);
156 	case TC_CLSMATCHALL_STATS:
157 		mlx5e_tc_stats_matchall(priv, ma);
158 		return 0;
159 	default:
160 		return -EOPNOTSUPP;
161 	}
162 }
163 
mlx5e_rep_setup_tc_cb(enum tc_setup_type type,void * type_data,void * cb_priv)164 static int mlx5e_rep_setup_tc_cb(enum tc_setup_type type, void *type_data,
165 				 void *cb_priv)
166 {
167 	unsigned long flags = MLX5_TC_FLAG(INGRESS) | MLX5_TC_FLAG(ESW_OFFLOAD);
168 	struct mlx5e_priv *priv = cb_priv;
169 
170 	switch (type) {
171 	case TC_SETUP_CLSFLOWER:
172 		return mlx5e_rep_setup_tc_cls_flower(priv, type_data, flags);
173 	case TC_SETUP_CLSMATCHALL:
174 		return mlx5e_rep_setup_tc_cls_matchall(priv, type_data);
175 	default:
176 		return -EOPNOTSUPP;
177 	}
178 }
179 
mlx5e_rep_setup_ft_cb(enum tc_setup_type type,void * type_data,void * cb_priv)180 static int mlx5e_rep_setup_ft_cb(enum tc_setup_type type, void *type_data,
181 				 void *cb_priv)
182 {
183 	struct flow_cls_offload tmp, *f = type_data;
184 	struct mlx5e_priv *priv = cb_priv;
185 	struct mlx5_eswitch *esw;
186 	unsigned long flags;
187 	int err;
188 
189 	flags = MLX5_TC_FLAG(INGRESS) |
190 		MLX5_TC_FLAG(ESW_OFFLOAD) |
191 		MLX5_TC_FLAG(FT_OFFLOAD);
192 	esw = priv->mdev->priv.eswitch;
193 
194 	switch (type) {
195 	case TC_SETUP_CLSFLOWER:
196 		memcpy(&tmp, f, sizeof(*f));
197 
198 		if (!mlx5_chains_prios_supported(esw_chains(esw)))
199 			return -EOPNOTSUPP;
200 
201 		/* Re-use tc offload path by moving the ft flow to the
202 		 * reserved ft chain.
203 		 *
204 		 * FT offload can use prio range [0, INT_MAX], so we normalize
205 		 * it to range [1, mlx5_esw_chains_get_prio_range(esw)]
206 		 * as with tc, where prio 0 isn't supported.
207 		 *
208 		 * We only support chain 0 of FT offload.
209 		 */
210 		if (tmp.common.prio >= mlx5_chains_get_prio_range(esw_chains(esw)))
211 			return -EOPNOTSUPP;
212 		if (tmp.common.chain_index != 0)
213 			return -EOPNOTSUPP;
214 
215 		tmp.common.chain_index = mlx5_chains_get_nf_ft_chain(esw_chains(esw));
216 		tmp.common.prio++;
217 		err = mlx5e_rep_setup_tc_cls_flower(priv, &tmp, flags);
218 		memcpy(&f->stats, &tmp.stats, sizeof(f->stats));
219 		return err;
220 	default:
221 		return -EOPNOTSUPP;
222 	}
223 }
224 
225 static LIST_HEAD(mlx5e_rep_block_tc_cb_list);
226 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)227 int mlx5e_rep_setup_tc(struct net_device *dev, enum tc_setup_type type,
228 		       void *type_data)
229 {
230 	struct mlx5e_priv *priv = netdev_priv(dev);
231 	struct flow_block_offload *f = type_data;
232 
233 	f->unlocked_driver_cb = true;
234 
235 	switch (type) {
236 	case TC_SETUP_BLOCK:
237 		return flow_block_cb_setup_simple(type_data,
238 						  &mlx5e_rep_block_tc_cb_list,
239 						  mlx5e_rep_setup_tc_cb,
240 						  priv, priv, true);
241 	case TC_SETUP_FT:
242 		return flow_block_cb_setup_simple(type_data,
243 						  &mlx5e_rep_block_ft_cb_list,
244 						  mlx5e_rep_setup_ft_cb,
245 						  priv, priv, true);
246 	default:
247 		return -EOPNOTSUPP;
248 	}
249 }
250 
mlx5e_rep_tc_init(struct mlx5e_rep_priv * rpriv)251 int mlx5e_rep_tc_init(struct mlx5e_rep_priv *rpriv)
252 {
253 	struct mlx5_rep_uplink_priv *uplink_priv = &rpriv->uplink_priv;
254 	int err;
255 
256 	mutex_init(&uplink_priv->unready_flows_lock);
257 	INIT_LIST_HEAD(&uplink_priv->unready_flows);
258 
259 	/* init shared tc flow table */
260 	err = mlx5e_tc_esw_init(&uplink_priv->tc_ht);
261 	return err;
262 }
263 
mlx5e_rep_tc_cleanup(struct mlx5e_rep_priv * rpriv)264 void mlx5e_rep_tc_cleanup(struct mlx5e_rep_priv *rpriv)
265 {
266 	/* delete shared tc flow table */
267 	mlx5e_tc_esw_cleanup(&rpriv->uplink_priv.tc_ht);
268 	mutex_destroy(&rpriv->uplink_priv.unready_flows_lock);
269 }
270 
mlx5e_rep_tc_enable(struct mlx5e_priv * priv)271 void mlx5e_rep_tc_enable(struct mlx5e_priv *priv)
272 {
273 	struct mlx5e_rep_priv *rpriv = priv->ppriv;
274 
275 	INIT_WORK(&rpriv->uplink_priv.reoffload_flows_work,
276 		  mlx5e_tc_reoffload_flows_work);
277 }
278 
mlx5e_rep_tc_disable(struct mlx5e_priv * priv)279 void mlx5e_rep_tc_disable(struct mlx5e_priv *priv)
280 {
281 	struct mlx5e_rep_priv *rpriv = priv->ppriv;
282 
283 	cancel_work_sync(&rpriv->uplink_priv.reoffload_flows_work);
284 }
285 
mlx5e_rep_tc_event_port_affinity(struct mlx5e_priv * priv)286 int mlx5e_rep_tc_event_port_affinity(struct mlx5e_priv *priv)
287 {
288 	struct mlx5e_rep_priv *rpriv = priv->ppriv;
289 
290 	queue_work(priv->wq, &rpriv->uplink_priv.reoffload_flows_work);
291 
292 	return NOTIFY_OK;
293 }
294 
295 static struct mlx5e_rep_indr_block_priv *
mlx5e_rep_indr_block_priv_lookup(struct mlx5e_rep_priv * rpriv,struct net_device * netdev)296 mlx5e_rep_indr_block_priv_lookup(struct mlx5e_rep_priv *rpriv,
297 				 struct net_device *netdev)
298 {
299 	struct mlx5e_rep_indr_block_priv *cb_priv;
300 
301 	list_for_each_entry(cb_priv,
302 			    &rpriv->uplink_priv.tc_indr_block_priv_list,
303 			    list)
304 		if (cb_priv->netdev == netdev)
305 			return cb_priv;
306 
307 	return NULL;
308 }
309 
310 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)311 mlx5e_rep_indr_offload(struct net_device *netdev,
312 		       struct flow_cls_offload *flower,
313 		       struct mlx5e_rep_indr_block_priv *indr_priv,
314 		       unsigned long flags)
315 {
316 	struct mlx5e_priv *priv = netdev_priv(indr_priv->rpriv->netdev);
317 	int err = 0;
318 
319 	switch (flower->command) {
320 	case FLOW_CLS_REPLACE:
321 		err = mlx5e_configure_flower(netdev, priv, flower, flags);
322 		break;
323 	case FLOW_CLS_DESTROY:
324 		err = mlx5e_delete_flower(netdev, priv, flower, flags);
325 		break;
326 	case FLOW_CLS_STATS:
327 		err = mlx5e_stats_flower(netdev, priv, flower, flags);
328 		break;
329 	default:
330 		err = -EOPNOTSUPP;
331 	}
332 
333 	return err;
334 }
335 
mlx5e_rep_indr_setup_tc_cb(enum tc_setup_type type,void * type_data,void * indr_priv)336 static int mlx5e_rep_indr_setup_tc_cb(enum tc_setup_type type,
337 				      void *type_data, void *indr_priv)
338 {
339 	unsigned long flags = MLX5_TC_FLAG(EGRESS) | MLX5_TC_FLAG(ESW_OFFLOAD);
340 	struct mlx5e_rep_indr_block_priv *priv = indr_priv;
341 
342 	switch (type) {
343 	case TC_SETUP_CLSFLOWER:
344 		return mlx5e_rep_indr_offload(priv->netdev, type_data, priv,
345 					      flags);
346 	default:
347 		return -EOPNOTSUPP;
348 	}
349 }
350 
mlx5e_rep_indr_setup_ft_cb(enum tc_setup_type type,void * type_data,void * indr_priv)351 static int mlx5e_rep_indr_setup_ft_cb(enum tc_setup_type type,
352 				      void *type_data, void *indr_priv)
353 {
354 	struct mlx5e_rep_indr_block_priv *priv = indr_priv;
355 	struct flow_cls_offload *f = type_data;
356 	struct flow_cls_offload tmp;
357 	struct mlx5e_priv *mpriv;
358 	struct mlx5_eswitch *esw;
359 	unsigned long flags;
360 	int err;
361 
362 	mpriv = netdev_priv(priv->rpriv->netdev);
363 	esw = mpriv->mdev->priv.eswitch;
364 
365 	flags = MLX5_TC_FLAG(EGRESS) |
366 		MLX5_TC_FLAG(ESW_OFFLOAD) |
367 		MLX5_TC_FLAG(FT_OFFLOAD);
368 
369 	switch (type) {
370 	case TC_SETUP_CLSFLOWER:
371 		memcpy(&tmp, f, sizeof(*f));
372 
373 		/* Re-use tc offload path by moving the ft flow to the
374 		 * reserved ft chain.
375 		 *
376 		 * FT offload can use prio range [0, INT_MAX], so we normalize
377 		 * it to range [1, mlx5_esw_chains_get_prio_range(esw)]
378 		 * as with tc, where prio 0 isn't supported.
379 		 *
380 		 * We only support chain 0 of FT offload.
381 		 */
382 		if (!mlx5_chains_prios_supported(esw_chains(esw)) ||
383 		    tmp.common.prio >= mlx5_chains_get_prio_range(esw_chains(esw)) ||
384 		    tmp.common.chain_index)
385 			return -EOPNOTSUPP;
386 
387 		tmp.common.chain_index = mlx5_chains_get_nf_ft_chain(esw_chains(esw));
388 		tmp.common.prio++;
389 		err = mlx5e_rep_indr_offload(priv->netdev, &tmp, priv, flags);
390 		memcpy(&f->stats, &tmp.stats, sizeof(f->stats));
391 		return err;
392 	default:
393 		return -EOPNOTSUPP;
394 	}
395 }
396 
mlx5e_rep_indr_block_unbind(void * cb_priv)397 static void mlx5e_rep_indr_block_unbind(void *cb_priv)
398 {
399 	struct mlx5e_rep_indr_block_priv *indr_priv = cb_priv;
400 
401 	list_del(&indr_priv->list);
402 	kfree(indr_priv);
403 }
404 
405 static LIST_HEAD(mlx5e_block_cb_list);
406 
407 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))408 mlx5e_rep_indr_setup_block(struct net_device *netdev, struct Qdisc *sch,
409 			   struct mlx5e_rep_priv *rpriv,
410 			   struct flow_block_offload *f,
411 			   flow_setup_cb_t *setup_cb,
412 			   void *data,
413 			   void (*cleanup)(struct flow_block_cb *block_cb))
414 {
415 	struct mlx5e_priv *priv = netdev_priv(rpriv->netdev);
416 	struct mlx5e_rep_indr_block_priv *indr_priv;
417 	struct flow_block_cb *block_cb;
418 
419 	if (!mlx5e_tc_tun_device_to_offload(priv, netdev) &&
420 	    !(is_vlan_dev(netdev) && vlan_dev_real_dev(netdev) == rpriv->netdev))
421 		return -EOPNOTSUPP;
422 
423 	if (f->binder_type != FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
424 		return -EOPNOTSUPP;
425 
426 	f->unlocked_driver_cb = true;
427 	f->driver_block_list = &mlx5e_block_cb_list;
428 
429 	switch (f->command) {
430 	case FLOW_BLOCK_BIND:
431 		indr_priv = mlx5e_rep_indr_block_priv_lookup(rpriv, netdev);
432 		if (indr_priv)
433 			return -EEXIST;
434 
435 		indr_priv = kmalloc(sizeof(*indr_priv), GFP_KERNEL);
436 		if (!indr_priv)
437 			return -ENOMEM;
438 
439 		indr_priv->netdev = netdev;
440 		indr_priv->rpriv = rpriv;
441 		list_add(&indr_priv->list,
442 			 &rpriv->uplink_priv.tc_indr_block_priv_list);
443 
444 		block_cb = flow_indr_block_cb_alloc(setup_cb, indr_priv, indr_priv,
445 						    mlx5e_rep_indr_block_unbind,
446 						    f, netdev, sch, data, rpriv,
447 						    cleanup);
448 		if (IS_ERR(block_cb)) {
449 			list_del(&indr_priv->list);
450 			kfree(indr_priv);
451 			return PTR_ERR(block_cb);
452 		}
453 		flow_block_cb_add(block_cb, f);
454 		list_add_tail(&block_cb->driver_list, &mlx5e_block_cb_list);
455 
456 		return 0;
457 	case FLOW_BLOCK_UNBIND:
458 		indr_priv = mlx5e_rep_indr_block_priv_lookup(rpriv, netdev);
459 		if (!indr_priv)
460 			return -ENOENT;
461 
462 		block_cb = flow_block_cb_lookup(f->block, setup_cb, indr_priv);
463 		if (!block_cb)
464 			return -ENOENT;
465 
466 		flow_indr_block_cb_remove(block_cb, f);
467 		list_del(&block_cb->driver_list);
468 		return 0;
469 	default:
470 		return -EOPNOTSUPP;
471 	}
472 	return 0;
473 }
474 
475 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))476 int mlx5e_rep_indr_setup_cb(struct net_device *netdev, struct Qdisc *sch, void *cb_priv,
477 			    enum tc_setup_type type, void *type_data,
478 			    void *data,
479 			    void (*cleanup)(struct flow_block_cb *block_cb))
480 {
481 	switch (type) {
482 	case TC_SETUP_BLOCK:
483 		return mlx5e_rep_indr_setup_block(netdev, sch, cb_priv, type_data,
484 						  mlx5e_rep_indr_setup_tc_cb,
485 						  data, cleanup);
486 	case TC_SETUP_FT:
487 		return mlx5e_rep_indr_setup_block(netdev, sch, cb_priv, type_data,
488 						  mlx5e_rep_indr_setup_ft_cb,
489 						  data, cleanup);
490 	default:
491 		return -EOPNOTSUPP;
492 	}
493 }
494 
mlx5e_rep_tc_netdevice_event_register(struct mlx5e_rep_priv * rpriv)495 int mlx5e_rep_tc_netdevice_event_register(struct mlx5e_rep_priv *rpriv)
496 {
497 	struct mlx5_rep_uplink_priv *uplink_priv = &rpriv->uplink_priv;
498 
499 	/* init indirect block notifications */
500 	INIT_LIST_HEAD(&uplink_priv->tc_indr_block_priv_list);
501 
502 	return flow_indr_dev_register(mlx5e_rep_indr_setup_cb, rpriv);
503 }
504 
mlx5e_rep_tc_netdevice_event_unregister(struct mlx5e_rep_priv * rpriv)505 void mlx5e_rep_tc_netdevice_event_unregister(struct mlx5e_rep_priv *rpriv)
506 {
507 	flow_indr_dev_unregister(mlx5e_rep_indr_setup_cb, rpriv,
508 				 mlx5e_rep_indr_block_unbind);
509 }
510 
511 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
mlx5e_restore_tunnel(struct mlx5e_priv * priv,struct sk_buff * skb,struct mlx5e_tc_update_priv * tc_priv,u32 tunnel_id)512 static bool mlx5e_restore_tunnel(struct mlx5e_priv *priv, struct sk_buff *skb,
513 				 struct mlx5e_tc_update_priv *tc_priv,
514 				 u32 tunnel_id)
515 {
516 	struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
517 	struct tunnel_match_enc_opts enc_opts = {};
518 	struct mlx5_rep_uplink_priv *uplink_priv;
519 	struct mlx5e_rep_priv *uplink_rpriv;
520 	struct metadata_dst *tun_dst;
521 	struct tunnel_match_key key;
522 	u32 tun_id, enc_opts_id;
523 	struct net_device *dev;
524 	int err;
525 
526 	enc_opts_id = tunnel_id & ENC_OPTS_BITS_MASK;
527 	tun_id = tunnel_id >> ENC_OPTS_BITS;
528 
529 	if (!tun_id)
530 		return true;
531 
532 	uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
533 	uplink_priv = &uplink_rpriv->uplink_priv;
534 
535 	err = mapping_find(uplink_priv->tunnel_mapping, tun_id, &key);
536 	if (err) {
537 		WARN_ON_ONCE(true);
538 		netdev_dbg(priv->netdev,
539 			   "Couldn't find tunnel for tun_id: %d, err: %d\n",
540 			   tun_id, err);
541 		return false;
542 	}
543 
544 	if (enc_opts_id) {
545 		err = mapping_find(uplink_priv->tunnel_enc_opts_mapping,
546 				   enc_opts_id, &enc_opts);
547 		if (err) {
548 			netdev_dbg(priv->netdev,
549 				   "Couldn't find tunnel (opts) for tun_id: %d, err: %d\n",
550 				   enc_opts_id, err);
551 			return false;
552 		}
553 	}
554 
555 	if (key.enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
556 		tun_dst = __ip_tun_set_dst(key.enc_ipv4.src, key.enc_ipv4.dst,
557 					   key.enc_ip.tos, key.enc_ip.ttl,
558 					   key.enc_tp.dst, TUNNEL_KEY,
559 					   key32_to_tunnel_id(key.enc_key_id.keyid),
560 					   enc_opts.key.len);
561 	} else if (key.enc_control.addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
562 		tun_dst = __ipv6_tun_set_dst(&key.enc_ipv6.src, &key.enc_ipv6.dst,
563 					     key.enc_ip.tos, key.enc_ip.ttl,
564 					     key.enc_tp.dst, 0, TUNNEL_KEY,
565 					     key32_to_tunnel_id(key.enc_key_id.keyid),
566 					     enc_opts.key.len);
567 	} else {
568 		netdev_dbg(priv->netdev,
569 			   "Couldn't restore tunnel, unsupported addr_type: %d\n",
570 			   key.enc_control.addr_type);
571 		return false;
572 	}
573 
574 	if (!tun_dst) {
575 		netdev_dbg(priv->netdev, "Couldn't restore tunnel, no tun_dst\n");
576 		return false;
577 	}
578 
579 	tun_dst->u.tun_info.key.tp_src = key.enc_tp.src;
580 
581 	if (enc_opts.key.len)
582 		ip_tunnel_info_opts_set(&tun_dst->u.tun_info,
583 					enc_opts.key.data,
584 					enc_opts.key.len,
585 					enc_opts.key.dst_opt_type);
586 
587 	skb_dst_set(skb, (struct dst_entry *)tun_dst);
588 	dev = dev_get_by_index(&init_net, key.filter_ifindex);
589 	if (!dev) {
590 		netdev_dbg(priv->netdev,
591 			   "Couldn't find tunnel device with ifindex: %d\n",
592 			   key.filter_ifindex);
593 		return false;
594 	}
595 
596 	/* Set tun_dev so we do dev_put() after datapath */
597 	tc_priv->tun_dev = dev;
598 
599 	skb->dev = dev;
600 
601 	return true;
602 }
603 #endif /* CONFIG_NET_TC_SKB_EXT */
604 
mlx5e_rep_tc_update_skb(struct mlx5_cqe64 * cqe,struct sk_buff * skb,struct mlx5e_tc_update_priv * tc_priv)605 bool mlx5e_rep_tc_update_skb(struct mlx5_cqe64 *cqe,
606 			     struct sk_buff *skb,
607 			     struct mlx5e_tc_update_priv *tc_priv)
608 {
609 #if IS_ENABLED(CONFIG_NET_TC_SKB_EXT)
610 	u32 chain = 0, reg_c0, reg_c1, tunnel_id, zone_restore_id;
611 	struct mlx5_rep_uplink_priv *uplink_priv;
612 	struct mlx5e_rep_priv *uplink_rpriv;
613 	struct tc_skb_ext *tc_skb_ext;
614 	struct mlx5_eswitch *esw;
615 	struct mlx5e_priv *priv;
616 	int err;
617 
618 	reg_c0 = (be32_to_cpu(cqe->sop_drop_qpn) & MLX5E_TC_FLOW_ID_MASK);
619 	if (reg_c0 == MLX5_FS_DEFAULT_FLOW_TAG)
620 		reg_c0 = 0;
621 	reg_c1 = be32_to_cpu(cqe->ft_metadata);
622 
623 	if (!reg_c0)
624 		return true;
625 
626 	/* If reg_c0 is not equal to the default flow tag then skb->mark
627 	 * is not supported and must be reset back to 0.
628 	 */
629 	skb->mark = 0;
630 
631 	priv = netdev_priv(skb->dev);
632 	esw = priv->mdev->priv.eswitch;
633 
634 	err = mlx5_get_chain_for_tag(esw_chains(esw), reg_c0, &chain);
635 	if (err) {
636 		netdev_dbg(priv->netdev,
637 			   "Couldn't find chain for chain tag: %d, err: %d\n",
638 			   reg_c0, err);
639 		return false;
640 	}
641 
642 	if (chain) {
643 		tc_skb_ext = tc_skb_ext_alloc(skb);
644 		if (!tc_skb_ext) {
645 			WARN_ON(1);
646 			return false;
647 		}
648 
649 		tc_skb_ext->chain = chain;
650 
651 		zone_restore_id = reg_c1 & ZONE_RESTORE_MAX;
652 
653 		uplink_rpriv = mlx5_eswitch_get_uplink_priv(esw, REP_ETH);
654 		uplink_priv = &uplink_rpriv->uplink_priv;
655 		if (!mlx5e_tc_ct_restore_flow(uplink_priv->ct_priv, skb,
656 					      zone_restore_id))
657 			return false;
658 	}
659 
660 	tunnel_id = reg_c1 >> REG_MAPPING_SHIFT(TUNNEL_TO_REG);
661 	return mlx5e_restore_tunnel(priv, skb, tc_priv, tunnel_id);
662 #endif /* CONFIG_NET_TC_SKB_EXT */
663 
664 	return true;
665 }
666 
mlx5_rep_tc_post_napi_receive(struct mlx5e_tc_update_priv * tc_priv)667 void mlx5_rep_tc_post_napi_receive(struct mlx5e_tc_update_priv *tc_priv)
668 {
669 	if (tc_priv->tun_dev)
670 		dev_put(tc_priv->tun_dev);
671 }
672