xref: /openbmc/linux/drivers/net/ethernet/netronome/nfp/flower/offload.c (revision 2e6ae11dd0d1c37f44cec51a58fb2092e55ed0f5)
1 /*
2  * Copyright (C) 2017 Netronome Systems, Inc.
3  *
4  * This software is dual licensed under the GNU General License Version 2,
5  * June 1991 as shown in the file COPYING in the top-level directory of this
6  * source tree or the BSD 2-Clause License provided below.  You have the
7  * option to license this software under the complete terms of either license.
8  *
9  * The BSD 2-Clause License:
10  *
11  *     Redistribution and use in source and binary forms, with or
12  *     without modification, are permitted provided that the following
13  *     conditions are met:
14  *
15  *      1. Redistributions of source code must retain the above
16  *         copyright notice, this list of conditions and the following
17  *         disclaimer.
18  *
19  *      2. Redistributions in binary form must reproduce the above
20  *         copyright notice, this list of conditions and the following
21  *         disclaimer in the documentation and/or other materials
22  *         provided with the distribution.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
28  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
29  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
30  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31  * SOFTWARE.
32  */
33 
34 #include <linux/skbuff.h>
35 #include <net/devlink.h>
36 #include <net/pkt_cls.h>
37 
38 #include "cmsg.h"
39 #include "main.h"
40 #include "../nfpcore/nfp_cpp.h"
41 #include "../nfpcore/nfp_nsp.h"
42 #include "../nfp_app.h"
43 #include "../nfp_main.h"
44 #include "../nfp_net.h"
45 #include "../nfp_port.h"
46 
47 #define NFP_FLOWER_SUPPORTED_TCPFLAGS \
48 	(TCPHDR_FIN | TCPHDR_SYN | TCPHDR_RST | \
49 	 TCPHDR_PSH | TCPHDR_URG)
50 
51 #define NFP_FLOWER_SUPPORTED_CTLFLAGS \
52 	(FLOW_DIS_IS_FRAGMENT | \
53 	 FLOW_DIS_FIRST_FRAG)
54 
55 #define NFP_FLOWER_WHITELIST_DISSECTOR \
56 	(BIT(FLOW_DISSECTOR_KEY_CONTROL) | \
57 	 BIT(FLOW_DISSECTOR_KEY_BASIC) | \
58 	 BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) | \
59 	 BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) | \
60 	 BIT(FLOW_DISSECTOR_KEY_TCP) | \
61 	 BIT(FLOW_DISSECTOR_KEY_PORTS) | \
62 	 BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) | \
63 	 BIT(FLOW_DISSECTOR_KEY_VLAN) | \
64 	 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) | \
65 	 BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) | \
66 	 BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) | \
67 	 BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) | \
68 	 BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) | \
69 	 BIT(FLOW_DISSECTOR_KEY_ENC_OPTS) | \
70 	 BIT(FLOW_DISSECTOR_KEY_ENC_IP) | \
71 	 BIT(FLOW_DISSECTOR_KEY_MPLS) | \
72 	 BIT(FLOW_DISSECTOR_KEY_IP))
73 
74 #define NFP_FLOWER_WHITELIST_TUN_DISSECTOR \
75 	(BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) | \
76 	 BIT(FLOW_DISSECTOR_KEY_ENC_KEYID) | \
77 	 BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) | \
78 	 BIT(FLOW_DISSECTOR_KEY_ENC_IPV6_ADDRS) | \
79 	 BIT(FLOW_DISSECTOR_KEY_ENC_OPTS) | \
80 	 BIT(FLOW_DISSECTOR_KEY_ENC_PORTS) | \
81 	 BIT(FLOW_DISSECTOR_KEY_ENC_IP))
82 
83 #define NFP_FLOWER_WHITELIST_TUN_DISSECTOR_R \
84 	(BIT(FLOW_DISSECTOR_KEY_ENC_CONTROL) | \
85 	 BIT(FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS) | \
86 	 BIT(FLOW_DISSECTOR_KEY_ENC_PORTS))
87 
88 static int
89 nfp_flower_xmit_flow(struct net_device *netdev,
90 		     struct nfp_fl_payload *nfp_flow, u8 mtype)
91 {
92 	u32 meta_len, key_len, mask_len, act_len, tot_len;
93 	struct nfp_repr *priv = netdev_priv(netdev);
94 	struct sk_buff *skb;
95 	unsigned char *msg;
96 
97 	meta_len =  sizeof(struct nfp_fl_rule_metadata);
98 	key_len = nfp_flow->meta.key_len;
99 	mask_len = nfp_flow->meta.mask_len;
100 	act_len = nfp_flow->meta.act_len;
101 
102 	tot_len = meta_len + key_len + mask_len + act_len;
103 
104 	/* Convert to long words as firmware expects
105 	 * lengths in units of NFP_FL_LW_SIZ.
106 	 */
107 	nfp_flow->meta.key_len >>= NFP_FL_LW_SIZ;
108 	nfp_flow->meta.mask_len >>= NFP_FL_LW_SIZ;
109 	nfp_flow->meta.act_len >>= NFP_FL_LW_SIZ;
110 
111 	skb = nfp_flower_cmsg_alloc(priv->app, tot_len, mtype, GFP_KERNEL);
112 	if (!skb)
113 		return -ENOMEM;
114 
115 	msg = nfp_flower_cmsg_get_data(skb);
116 	memcpy(msg, &nfp_flow->meta, meta_len);
117 	memcpy(&msg[meta_len], nfp_flow->unmasked_data, key_len);
118 	memcpy(&msg[meta_len + key_len], nfp_flow->mask_data, mask_len);
119 	memcpy(&msg[meta_len + key_len + mask_len],
120 	       nfp_flow->action_data, act_len);
121 
122 	/* Convert back to bytes as software expects
123 	 * lengths in units of bytes.
124 	 */
125 	nfp_flow->meta.key_len <<= NFP_FL_LW_SIZ;
126 	nfp_flow->meta.mask_len <<= NFP_FL_LW_SIZ;
127 	nfp_flow->meta.act_len <<= NFP_FL_LW_SIZ;
128 
129 	nfp_ctrl_tx(priv->app->ctrl, skb);
130 
131 	return 0;
132 }
133 
134 static bool nfp_flower_check_higher_than_mac(struct tc_cls_flower_offload *f)
135 {
136 	return dissector_uses_key(f->dissector,
137 				  FLOW_DISSECTOR_KEY_IPV4_ADDRS) ||
138 		dissector_uses_key(f->dissector,
139 				   FLOW_DISSECTOR_KEY_IPV6_ADDRS) ||
140 		dissector_uses_key(f->dissector,
141 				   FLOW_DISSECTOR_KEY_PORTS) ||
142 		dissector_uses_key(f->dissector, FLOW_DISSECTOR_KEY_ICMP);
143 }
144 
145 static int
146 nfp_flower_calc_opt_layer(struct flow_dissector_key_enc_opts *enc_opts,
147 			  u32 *key_layer_two, int *key_size)
148 {
149 	if (enc_opts->len > NFP_FL_MAX_GENEVE_OPT_KEY)
150 		return -EOPNOTSUPP;
151 
152 	if (enc_opts->len > 0) {
153 		*key_layer_two |= NFP_FLOWER_LAYER2_GENEVE_OP;
154 		*key_size += sizeof(struct nfp_flower_geneve_options);
155 	}
156 
157 	return 0;
158 }
159 
160 static int
161 nfp_flower_calculate_key_layers(struct nfp_app *app,
162 				struct nfp_fl_key_ls *ret_key_ls,
163 				struct tc_cls_flower_offload *flow,
164 				bool egress,
165 				enum nfp_flower_tun_type *tun_type)
166 {
167 	struct flow_dissector_key_basic *mask_basic = NULL;
168 	struct flow_dissector_key_basic *key_basic = NULL;
169 	struct nfp_flower_priv *priv = app->priv;
170 	u32 key_layer_two;
171 	u8 key_layer;
172 	int key_size;
173 	int err;
174 
175 	if (flow->dissector->used_keys & ~NFP_FLOWER_WHITELIST_DISSECTOR)
176 		return -EOPNOTSUPP;
177 
178 	/* If any tun dissector is used then the required set must be used. */
179 	if (flow->dissector->used_keys & NFP_FLOWER_WHITELIST_TUN_DISSECTOR &&
180 	    (flow->dissector->used_keys & NFP_FLOWER_WHITELIST_TUN_DISSECTOR_R)
181 	    != NFP_FLOWER_WHITELIST_TUN_DISSECTOR_R)
182 		return -EOPNOTSUPP;
183 
184 	key_layer_two = 0;
185 	key_layer = NFP_FLOWER_LAYER_PORT;
186 	key_size = sizeof(struct nfp_flower_meta_tci) +
187 		   sizeof(struct nfp_flower_in_port);
188 
189 	if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_ETH_ADDRS) ||
190 	    dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_MPLS)) {
191 		key_layer |= NFP_FLOWER_LAYER_MAC;
192 		key_size += sizeof(struct nfp_flower_mac_mpls);
193 	}
194 
195 	if (dissector_uses_key(flow->dissector,
196 			       FLOW_DISSECTOR_KEY_ENC_CONTROL)) {
197 		struct flow_dissector_key_ipv4_addrs *mask_ipv4 = NULL;
198 		struct flow_dissector_key_ports *mask_enc_ports = NULL;
199 		struct flow_dissector_key_enc_opts *enc_op = NULL;
200 		struct flow_dissector_key_ports *enc_ports = NULL;
201 		struct flow_dissector_key_control *mask_enc_ctl =
202 			skb_flow_dissector_target(flow->dissector,
203 						  FLOW_DISSECTOR_KEY_ENC_CONTROL,
204 						  flow->mask);
205 		struct flow_dissector_key_control *enc_ctl =
206 			skb_flow_dissector_target(flow->dissector,
207 						  FLOW_DISSECTOR_KEY_ENC_CONTROL,
208 						  flow->key);
209 		if (!egress)
210 			return -EOPNOTSUPP;
211 
212 		if (mask_enc_ctl->addr_type != 0xffff ||
213 		    enc_ctl->addr_type != FLOW_DISSECTOR_KEY_IPV4_ADDRS)
214 			return -EOPNOTSUPP;
215 
216 		/* These fields are already verified as used. */
217 		mask_ipv4 =
218 			skb_flow_dissector_target(flow->dissector,
219 						  FLOW_DISSECTOR_KEY_ENC_IPV4_ADDRS,
220 						  flow->mask);
221 		if (mask_ipv4->dst != cpu_to_be32(~0))
222 			return -EOPNOTSUPP;
223 
224 		mask_enc_ports =
225 			skb_flow_dissector_target(flow->dissector,
226 						  FLOW_DISSECTOR_KEY_ENC_PORTS,
227 						  flow->mask);
228 		enc_ports =
229 			skb_flow_dissector_target(flow->dissector,
230 						  FLOW_DISSECTOR_KEY_ENC_PORTS,
231 						  flow->key);
232 
233 		if (mask_enc_ports->dst != cpu_to_be16(~0))
234 			return -EOPNOTSUPP;
235 
236 		if (dissector_uses_key(flow->dissector,
237 				       FLOW_DISSECTOR_KEY_ENC_OPTS)) {
238 			enc_op = skb_flow_dissector_target(flow->dissector,
239 							   FLOW_DISSECTOR_KEY_ENC_OPTS,
240 							   flow->key);
241 		}
242 
243 		switch (enc_ports->dst) {
244 		case htons(NFP_FL_VXLAN_PORT):
245 			*tun_type = NFP_FL_TUNNEL_VXLAN;
246 			key_layer |= NFP_FLOWER_LAYER_VXLAN;
247 			key_size += sizeof(struct nfp_flower_ipv4_udp_tun);
248 
249 			if (enc_op)
250 				return -EOPNOTSUPP;
251 			break;
252 		case htons(NFP_FL_GENEVE_PORT):
253 			if (!(priv->flower_ext_feats & NFP_FL_FEATS_GENEVE))
254 				return -EOPNOTSUPP;
255 			*tun_type = NFP_FL_TUNNEL_GENEVE;
256 			key_layer |= NFP_FLOWER_LAYER_EXT_META;
257 			key_size += sizeof(struct nfp_flower_ext_meta);
258 			key_layer_two |= NFP_FLOWER_LAYER2_GENEVE;
259 			key_size += sizeof(struct nfp_flower_ipv4_udp_tun);
260 
261 			if (!enc_op)
262 				break;
263 			if (!(priv->flower_ext_feats & NFP_FL_FEATS_GENEVE_OPT))
264 				return -EOPNOTSUPP;
265 			err = nfp_flower_calc_opt_layer(enc_op, &key_layer_two,
266 							&key_size);
267 			if (err)
268 				return err;
269 			break;
270 		default:
271 			return -EOPNOTSUPP;
272 		}
273 	} else if (egress) {
274 		/* Reject non tunnel matches offloaded to egress repr. */
275 		return -EOPNOTSUPP;
276 	}
277 
278 	if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
279 		mask_basic = skb_flow_dissector_target(flow->dissector,
280 						       FLOW_DISSECTOR_KEY_BASIC,
281 						       flow->mask);
282 
283 		key_basic = skb_flow_dissector_target(flow->dissector,
284 						      FLOW_DISSECTOR_KEY_BASIC,
285 						      flow->key);
286 	}
287 
288 	if (mask_basic && mask_basic->n_proto) {
289 		/* Ethernet type is present in the key. */
290 		switch (key_basic->n_proto) {
291 		case cpu_to_be16(ETH_P_IP):
292 			key_layer |= NFP_FLOWER_LAYER_IPV4;
293 			key_size += sizeof(struct nfp_flower_ipv4);
294 			break;
295 
296 		case cpu_to_be16(ETH_P_IPV6):
297 			key_layer |= NFP_FLOWER_LAYER_IPV6;
298 			key_size += sizeof(struct nfp_flower_ipv6);
299 			break;
300 
301 		/* Currently we do not offload ARP
302 		 * because we rely on it to get to the host.
303 		 */
304 		case cpu_to_be16(ETH_P_ARP):
305 			return -EOPNOTSUPP;
306 
307 		case cpu_to_be16(ETH_P_MPLS_UC):
308 		case cpu_to_be16(ETH_P_MPLS_MC):
309 			if (!(key_layer & NFP_FLOWER_LAYER_MAC)) {
310 				key_layer |= NFP_FLOWER_LAYER_MAC;
311 				key_size += sizeof(struct nfp_flower_mac_mpls);
312 			}
313 			break;
314 
315 		/* Will be included in layer 2. */
316 		case cpu_to_be16(ETH_P_8021Q):
317 			break;
318 
319 		default:
320 			/* Other ethtype - we need check the masks for the
321 			 * remainder of the key to ensure we can offload.
322 			 */
323 			if (nfp_flower_check_higher_than_mac(flow))
324 				return -EOPNOTSUPP;
325 			break;
326 		}
327 	}
328 
329 	if (mask_basic && mask_basic->ip_proto) {
330 		/* Ethernet type is present in the key. */
331 		switch (key_basic->ip_proto) {
332 		case IPPROTO_TCP:
333 		case IPPROTO_UDP:
334 		case IPPROTO_SCTP:
335 		case IPPROTO_ICMP:
336 		case IPPROTO_ICMPV6:
337 			key_layer |= NFP_FLOWER_LAYER_TP;
338 			key_size += sizeof(struct nfp_flower_tp_ports);
339 			break;
340 		default:
341 			/* Other ip proto - we need check the masks for the
342 			 * remainder of the key to ensure we can offload.
343 			 */
344 			return -EOPNOTSUPP;
345 		}
346 	}
347 
348 	if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_TCP)) {
349 		struct flow_dissector_key_tcp *tcp;
350 		u32 tcp_flags;
351 
352 		tcp = skb_flow_dissector_target(flow->dissector,
353 						FLOW_DISSECTOR_KEY_TCP,
354 						flow->key);
355 		tcp_flags = be16_to_cpu(tcp->flags);
356 
357 		if (tcp_flags & ~NFP_FLOWER_SUPPORTED_TCPFLAGS)
358 			return -EOPNOTSUPP;
359 
360 		/* We only support PSH and URG flags when either
361 		 * FIN, SYN or RST is present as well.
362 		 */
363 		if ((tcp_flags & (TCPHDR_PSH | TCPHDR_URG)) &&
364 		    !(tcp_flags & (TCPHDR_FIN | TCPHDR_SYN | TCPHDR_RST)))
365 			return -EOPNOTSUPP;
366 
367 		/* We need to store TCP flags in the IPv4 key space, thus
368 		 * we need to ensure we include a IPv4 key layer if we have
369 		 * not done so already.
370 		 */
371 		if (!(key_layer & NFP_FLOWER_LAYER_IPV4)) {
372 			key_layer |= NFP_FLOWER_LAYER_IPV4;
373 			key_size += sizeof(struct nfp_flower_ipv4);
374 		}
375 	}
376 
377 	if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_CONTROL)) {
378 		struct flow_dissector_key_control *key_ctl;
379 
380 		key_ctl = skb_flow_dissector_target(flow->dissector,
381 						    FLOW_DISSECTOR_KEY_CONTROL,
382 						    flow->key);
383 
384 		if (key_ctl->flags & ~NFP_FLOWER_SUPPORTED_CTLFLAGS)
385 			return -EOPNOTSUPP;
386 	}
387 
388 	ret_key_ls->key_layer = key_layer;
389 	ret_key_ls->key_layer_two = key_layer_two;
390 	ret_key_ls->key_size = key_size;
391 
392 	return 0;
393 }
394 
395 static struct nfp_fl_payload *
396 nfp_flower_allocate_new(struct nfp_fl_key_ls *key_layer, bool egress)
397 {
398 	struct nfp_fl_payload *flow_pay;
399 
400 	flow_pay = kmalloc(sizeof(*flow_pay), GFP_KERNEL);
401 	if (!flow_pay)
402 		return NULL;
403 
404 	flow_pay->meta.key_len = key_layer->key_size;
405 	flow_pay->unmasked_data = kmalloc(key_layer->key_size, GFP_KERNEL);
406 	if (!flow_pay->unmasked_data)
407 		goto err_free_flow;
408 
409 	flow_pay->meta.mask_len = key_layer->key_size;
410 	flow_pay->mask_data = kmalloc(key_layer->key_size, GFP_KERNEL);
411 	if (!flow_pay->mask_data)
412 		goto err_free_unmasked;
413 
414 	flow_pay->action_data = kmalloc(NFP_FL_MAX_A_SIZ, GFP_KERNEL);
415 	if (!flow_pay->action_data)
416 		goto err_free_mask;
417 
418 	flow_pay->nfp_tun_ipv4_addr = 0;
419 	flow_pay->meta.flags = 0;
420 	spin_lock_init(&flow_pay->lock);
421 
422 	flow_pay->ingress_offload = !egress;
423 
424 	return flow_pay;
425 
426 err_free_mask:
427 	kfree(flow_pay->mask_data);
428 err_free_unmasked:
429 	kfree(flow_pay->unmasked_data);
430 err_free_flow:
431 	kfree(flow_pay);
432 	return NULL;
433 }
434 
435 /**
436  * nfp_flower_add_offload() - Adds a new flow to hardware.
437  * @app:	Pointer to the APP handle
438  * @netdev:	netdev structure.
439  * @flow:	TC flower classifier offload structure.
440  * @egress:	NFP netdev is the egress.
441  *
442  * Adds a new flow to the repeated hash structure and action payload.
443  *
444  * Return: negative value on error, 0 if configured successfully.
445  */
446 static int
447 nfp_flower_add_offload(struct nfp_app *app, struct net_device *netdev,
448 		       struct tc_cls_flower_offload *flow, bool egress)
449 {
450 	enum nfp_flower_tun_type tun_type = NFP_FL_TUNNEL_NONE;
451 	struct nfp_port *port = nfp_port_from_netdev(netdev);
452 	struct nfp_flower_priv *priv = app->priv;
453 	struct nfp_fl_payload *flow_pay;
454 	struct nfp_fl_key_ls *key_layer;
455 	struct net_device *ingr_dev;
456 	int err;
457 
458 	ingr_dev = egress ? NULL : netdev;
459 	flow_pay = nfp_flower_search_fl_table(app, flow->cookie, ingr_dev,
460 					      NFP_FL_STATS_CTX_DONT_CARE);
461 	if (flow_pay) {
462 		/* Ignore as duplicate if it has been added by different cb. */
463 		if (flow_pay->ingress_offload && egress)
464 			return 0;
465 		else
466 			return -EOPNOTSUPP;
467 	}
468 
469 	key_layer = kmalloc(sizeof(*key_layer), GFP_KERNEL);
470 	if (!key_layer)
471 		return -ENOMEM;
472 
473 	err = nfp_flower_calculate_key_layers(app, key_layer, flow, egress,
474 					      &tun_type);
475 	if (err)
476 		goto err_free_key_ls;
477 
478 	flow_pay = nfp_flower_allocate_new(key_layer, egress);
479 	if (!flow_pay) {
480 		err = -ENOMEM;
481 		goto err_free_key_ls;
482 	}
483 
484 	flow_pay->ingress_dev = egress ? NULL : netdev;
485 
486 	err = nfp_flower_compile_flow_match(flow, key_layer, netdev, flow_pay,
487 					    tun_type);
488 	if (err)
489 		goto err_destroy_flow;
490 
491 	err = nfp_flower_compile_action(app, flow, netdev, flow_pay);
492 	if (err)
493 		goto err_destroy_flow;
494 
495 	err = nfp_compile_flow_metadata(app, flow, flow_pay,
496 					flow_pay->ingress_dev);
497 	if (err)
498 		goto err_destroy_flow;
499 
500 	err = nfp_flower_xmit_flow(netdev, flow_pay,
501 				   NFP_FLOWER_CMSG_TYPE_FLOW_ADD);
502 	if (err)
503 		goto err_destroy_flow;
504 
505 	INIT_HLIST_NODE(&flow_pay->link);
506 	flow_pay->tc_flower_cookie = flow->cookie;
507 	hash_add_rcu(priv->flow_table, &flow_pay->link, flow->cookie);
508 	port->tc_offload_cnt++;
509 
510 	/* Deallocate flow payload when flower rule has been destroyed. */
511 	kfree(key_layer);
512 
513 	return 0;
514 
515 err_destroy_flow:
516 	kfree(flow_pay->action_data);
517 	kfree(flow_pay->mask_data);
518 	kfree(flow_pay->unmasked_data);
519 	kfree(flow_pay);
520 err_free_key_ls:
521 	kfree(key_layer);
522 	return err;
523 }
524 
525 /**
526  * nfp_flower_del_offload() - Removes a flow from hardware.
527  * @app:	Pointer to the APP handle
528  * @netdev:	netdev structure.
529  * @flow:	TC flower classifier offload structure
530  * @egress:	Netdev is the egress dev.
531  *
532  * Removes a flow from the repeated hash structure and clears the
533  * action payload.
534  *
535  * Return: negative value on error, 0 if removed successfully.
536  */
537 static int
538 nfp_flower_del_offload(struct nfp_app *app, struct net_device *netdev,
539 		       struct tc_cls_flower_offload *flow, bool egress)
540 {
541 	struct nfp_port *port = nfp_port_from_netdev(netdev);
542 	struct nfp_fl_payload *nfp_flow;
543 	struct net_device *ingr_dev;
544 	int err;
545 
546 	ingr_dev = egress ? NULL : netdev;
547 	nfp_flow = nfp_flower_search_fl_table(app, flow->cookie, ingr_dev,
548 					      NFP_FL_STATS_CTX_DONT_CARE);
549 	if (!nfp_flow)
550 		return egress ? 0 : -ENOENT;
551 
552 	err = nfp_modify_flow_metadata(app, nfp_flow);
553 	if (err)
554 		goto err_free_flow;
555 
556 	if (nfp_flow->nfp_tun_ipv4_addr)
557 		nfp_tunnel_del_ipv4_off(app, nfp_flow->nfp_tun_ipv4_addr);
558 
559 	err = nfp_flower_xmit_flow(netdev, nfp_flow,
560 				   NFP_FLOWER_CMSG_TYPE_FLOW_DEL);
561 	if (err)
562 		goto err_free_flow;
563 
564 err_free_flow:
565 	hash_del_rcu(&nfp_flow->link);
566 	port->tc_offload_cnt--;
567 	kfree(nfp_flow->action_data);
568 	kfree(nfp_flow->mask_data);
569 	kfree(nfp_flow->unmasked_data);
570 	kfree_rcu(nfp_flow, rcu);
571 	return err;
572 }
573 
574 /**
575  * nfp_flower_get_stats() - Populates flow stats obtained from hardware.
576  * @app:	Pointer to the APP handle
577  * @netdev:	Netdev structure.
578  * @flow:	TC flower classifier offload structure
579  * @egress:	Netdev is the egress dev.
580  *
581  * Populates a flow statistics structure which which corresponds to a
582  * specific flow.
583  *
584  * Return: negative value on error, 0 if stats populated successfully.
585  */
586 static int
587 nfp_flower_get_stats(struct nfp_app *app, struct net_device *netdev,
588 		     struct tc_cls_flower_offload *flow, bool egress)
589 {
590 	struct nfp_fl_payload *nfp_flow;
591 	struct net_device *ingr_dev;
592 
593 	ingr_dev = egress ? NULL : netdev;
594 	nfp_flow = nfp_flower_search_fl_table(app, flow->cookie, ingr_dev,
595 					      NFP_FL_STATS_CTX_DONT_CARE);
596 	if (!nfp_flow)
597 		return -EINVAL;
598 
599 	if (nfp_flow->ingress_offload && egress)
600 		return 0;
601 
602 	spin_lock_bh(&nfp_flow->lock);
603 	tcf_exts_stats_update(flow->exts, nfp_flow->stats.bytes,
604 			      nfp_flow->stats.pkts, nfp_flow->stats.used);
605 
606 	nfp_flow->stats.pkts = 0;
607 	nfp_flow->stats.bytes = 0;
608 	spin_unlock_bh(&nfp_flow->lock);
609 
610 	return 0;
611 }
612 
613 static int
614 nfp_flower_repr_offload(struct nfp_app *app, struct net_device *netdev,
615 			struct tc_cls_flower_offload *flower, bool egress)
616 {
617 	if (!eth_proto_is_802_3(flower->common.protocol))
618 		return -EOPNOTSUPP;
619 
620 	switch (flower->command) {
621 	case TC_CLSFLOWER_REPLACE:
622 		return nfp_flower_add_offload(app, netdev, flower, egress);
623 	case TC_CLSFLOWER_DESTROY:
624 		return nfp_flower_del_offload(app, netdev, flower, egress);
625 	case TC_CLSFLOWER_STATS:
626 		return nfp_flower_get_stats(app, netdev, flower, egress);
627 	default:
628 		return -EOPNOTSUPP;
629 	}
630 }
631 
632 int nfp_flower_setup_tc_egress_cb(enum tc_setup_type type, void *type_data,
633 				  void *cb_priv)
634 {
635 	struct nfp_repr *repr = cb_priv;
636 
637 	if (!tc_cls_can_offload_and_chain0(repr->netdev, type_data))
638 		return -EOPNOTSUPP;
639 
640 	switch (type) {
641 	case TC_SETUP_CLSFLOWER:
642 		return nfp_flower_repr_offload(repr->app, repr->netdev,
643 					       type_data, true);
644 	default:
645 		return -EOPNOTSUPP;
646 	}
647 }
648 
649 static int nfp_flower_setup_tc_block_cb(enum tc_setup_type type,
650 					void *type_data, void *cb_priv)
651 {
652 	struct nfp_repr *repr = cb_priv;
653 
654 	if (!tc_cls_can_offload_and_chain0(repr->netdev, type_data))
655 		return -EOPNOTSUPP;
656 
657 	switch (type) {
658 	case TC_SETUP_CLSFLOWER:
659 		return nfp_flower_repr_offload(repr->app, repr->netdev,
660 					       type_data, false);
661 	default:
662 		return -EOPNOTSUPP;
663 	}
664 }
665 
666 static int nfp_flower_setup_tc_block(struct net_device *netdev,
667 				     struct tc_block_offload *f)
668 {
669 	struct nfp_repr *repr = netdev_priv(netdev);
670 
671 	if (f->binder_type != TCF_BLOCK_BINDER_TYPE_CLSACT_INGRESS)
672 		return -EOPNOTSUPP;
673 
674 	switch (f->command) {
675 	case TC_BLOCK_BIND:
676 		return tcf_block_cb_register(f->block,
677 					     nfp_flower_setup_tc_block_cb,
678 					     repr, repr, f->extack);
679 	case TC_BLOCK_UNBIND:
680 		tcf_block_cb_unregister(f->block,
681 					nfp_flower_setup_tc_block_cb,
682 					repr);
683 		return 0;
684 	default:
685 		return -EOPNOTSUPP;
686 	}
687 }
688 
689 int nfp_flower_setup_tc(struct nfp_app *app, struct net_device *netdev,
690 			enum tc_setup_type type, void *type_data)
691 {
692 	switch (type) {
693 	case TC_SETUP_BLOCK:
694 		return nfp_flower_setup_tc_block(netdev, type_data);
695 	default:
696 		return -EOPNOTSUPP;
697 	}
698 }
699