xref: /openbmc/linux/drivers/net/ethernet/netronome/nfp/flower/action.c (revision a0ae2562c6c4b2721d9fddba63b7286c13517d9f)
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/bitfield.h>
35 #include <net/pkt_cls.h>
36 #include <net/switchdev.h>
37 #include <net/tc_act/tc_csum.h>
38 #include <net/tc_act/tc_gact.h>
39 #include <net/tc_act/tc_mirred.h>
40 #include <net/tc_act/tc_pedit.h>
41 #include <net/tc_act/tc_vlan.h>
42 #include <net/tc_act/tc_tunnel_key.h>
43 
44 #include "cmsg.h"
45 #include "main.h"
46 #include "../nfp_net_repr.h"
47 
48 #define NFP_FL_SUPPORTED_IPV4_UDP_TUN_FLAGS	(TUNNEL_CSUM | TUNNEL_KEY)
49 
50 static void nfp_fl_pop_vlan(struct nfp_fl_pop_vlan *pop_vlan)
51 {
52 	size_t act_size = sizeof(struct nfp_fl_pop_vlan);
53 
54 	pop_vlan->head.jump_id = NFP_FL_ACTION_OPCODE_POP_VLAN;
55 	pop_vlan->head.len_lw = act_size >> NFP_FL_LW_SIZ;
56 	pop_vlan->reserved = 0;
57 }
58 
59 static void
60 nfp_fl_push_vlan(struct nfp_fl_push_vlan *push_vlan,
61 		 const struct tc_action *action)
62 {
63 	size_t act_size = sizeof(struct nfp_fl_push_vlan);
64 	u16 tmp_push_vlan_tci;
65 
66 	push_vlan->head.jump_id = NFP_FL_ACTION_OPCODE_PUSH_VLAN;
67 	push_vlan->head.len_lw = act_size >> NFP_FL_LW_SIZ;
68 	push_vlan->reserved = 0;
69 	push_vlan->vlan_tpid = tcf_vlan_push_proto(action);
70 
71 	tmp_push_vlan_tci =
72 		FIELD_PREP(NFP_FL_PUSH_VLAN_PRIO, tcf_vlan_push_prio(action)) |
73 		FIELD_PREP(NFP_FL_PUSH_VLAN_VID, tcf_vlan_push_vid(action)) |
74 		NFP_FL_PUSH_VLAN_CFI;
75 	push_vlan->vlan_tci = cpu_to_be16(tmp_push_vlan_tci);
76 }
77 
78 static int
79 nfp_fl_pre_lag(struct nfp_app *app, const struct tc_action *action,
80 	       struct nfp_fl_payload *nfp_flow, int act_len)
81 {
82 	size_t act_size = sizeof(struct nfp_fl_pre_lag);
83 	struct nfp_fl_pre_lag *pre_lag;
84 	struct net_device *out_dev;
85 	int err;
86 
87 	out_dev = tcf_mirred_dev(action);
88 	if (!out_dev || !netif_is_lag_master(out_dev))
89 		return 0;
90 
91 	if (act_len + act_size > NFP_FL_MAX_A_SIZ)
92 		return -EOPNOTSUPP;
93 
94 	/* Pre_lag action must be first on action list.
95 	 * If other actions already exist they need pushed forward.
96 	 */
97 	if (act_len)
98 		memmove(nfp_flow->action_data + act_size,
99 			nfp_flow->action_data, act_len);
100 
101 	pre_lag = (struct nfp_fl_pre_lag *)nfp_flow->action_data;
102 	err = nfp_flower_lag_populate_pre_action(app, out_dev, pre_lag);
103 	if (err)
104 		return err;
105 
106 	pre_lag->head.jump_id = NFP_FL_ACTION_OPCODE_PRE_LAG;
107 	pre_lag->head.len_lw = act_size >> NFP_FL_LW_SIZ;
108 
109 	nfp_flow->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
110 
111 	return act_size;
112 }
113 
114 static bool nfp_fl_netdev_is_tunnel_type(struct net_device *out_dev,
115 					 enum nfp_flower_tun_type tun_type)
116 {
117 	if (!out_dev->rtnl_link_ops)
118 		return false;
119 
120 	if (!strcmp(out_dev->rtnl_link_ops->kind, "vxlan"))
121 		return tun_type == NFP_FL_TUNNEL_VXLAN;
122 
123 	if (!strcmp(out_dev->rtnl_link_ops->kind, "geneve"))
124 		return tun_type == NFP_FL_TUNNEL_GENEVE;
125 
126 	return false;
127 }
128 
129 static int
130 nfp_fl_output(struct nfp_app *app, struct nfp_fl_output *output,
131 	      const struct tc_action *action, struct nfp_fl_payload *nfp_flow,
132 	      bool last, struct net_device *in_dev,
133 	      enum nfp_flower_tun_type tun_type, int *tun_out_cnt)
134 {
135 	size_t act_size = sizeof(struct nfp_fl_output);
136 	struct nfp_flower_priv *priv = app->priv;
137 	struct net_device *out_dev;
138 	u16 tmp_flags;
139 
140 	output->head.jump_id = NFP_FL_ACTION_OPCODE_OUTPUT;
141 	output->head.len_lw = act_size >> NFP_FL_LW_SIZ;
142 
143 	out_dev = tcf_mirred_dev(action);
144 	if (!out_dev)
145 		return -EOPNOTSUPP;
146 
147 	tmp_flags = last ? NFP_FL_OUT_FLAGS_LAST : 0;
148 
149 	if (tun_type) {
150 		/* Verify the egress netdev matches the tunnel type. */
151 		if (!nfp_fl_netdev_is_tunnel_type(out_dev, tun_type))
152 			return -EOPNOTSUPP;
153 
154 		if (*tun_out_cnt)
155 			return -EOPNOTSUPP;
156 		(*tun_out_cnt)++;
157 
158 		output->flags = cpu_to_be16(tmp_flags |
159 					    NFP_FL_OUT_FLAGS_USE_TUN);
160 		output->port = cpu_to_be32(NFP_FL_PORT_TYPE_TUN | tun_type);
161 	} else if (netif_is_lag_master(out_dev) &&
162 		   priv->flower_ext_feats & NFP_FL_FEATS_LAG) {
163 		int gid;
164 
165 		output->flags = cpu_to_be16(tmp_flags);
166 		gid = nfp_flower_lag_get_output_id(app, out_dev);
167 		if (gid < 0)
168 			return gid;
169 		output->port = cpu_to_be32(NFP_FL_LAG_OUT | gid);
170 	} else {
171 		/* Set action output parameters. */
172 		output->flags = cpu_to_be16(tmp_flags);
173 
174 		/* Only offload if egress ports are on the same device as the
175 		 * ingress port.
176 		 */
177 		if (!switchdev_port_same_parent_id(in_dev, out_dev))
178 			return -EOPNOTSUPP;
179 		if (!nfp_netdev_is_nfp_repr(out_dev))
180 			return -EOPNOTSUPP;
181 
182 		output->port = cpu_to_be32(nfp_repr_get_port_id(out_dev));
183 		if (!output->port)
184 			return -EOPNOTSUPP;
185 	}
186 	nfp_flow->meta.shortcut = output->port;
187 
188 	return 0;
189 }
190 
191 static enum nfp_flower_tun_type
192 nfp_fl_get_tun_from_act_l4_port(struct nfp_app *app,
193 				const struct tc_action *action)
194 {
195 	struct ip_tunnel_info *tun = tcf_tunnel_info(action);
196 	struct nfp_flower_priv *priv = app->priv;
197 
198 	switch (tun->key.tp_dst) {
199 	case htons(NFP_FL_VXLAN_PORT):
200 		return NFP_FL_TUNNEL_VXLAN;
201 	case htons(NFP_FL_GENEVE_PORT):
202 		if (priv->flower_ext_feats & NFP_FL_FEATS_GENEVE)
203 			return NFP_FL_TUNNEL_GENEVE;
204 		/* FALLTHROUGH */
205 	default:
206 		return NFP_FL_TUNNEL_NONE;
207 	}
208 }
209 
210 static struct nfp_fl_pre_tunnel *nfp_fl_pre_tunnel(char *act_data, int act_len)
211 {
212 	size_t act_size = sizeof(struct nfp_fl_pre_tunnel);
213 	struct nfp_fl_pre_tunnel *pre_tun_act;
214 
215 	/* Pre_tunnel action must be first on action list.
216 	 * If other actions already exist they need to be pushed forward.
217 	 */
218 	if (act_len)
219 		memmove(act_data + act_size, act_data, act_len);
220 
221 	pre_tun_act = (struct nfp_fl_pre_tunnel *)act_data;
222 
223 	memset(pre_tun_act, 0, act_size);
224 
225 	pre_tun_act->head.jump_id = NFP_FL_ACTION_OPCODE_PRE_TUNNEL;
226 	pre_tun_act->head.len_lw = act_size >> NFP_FL_LW_SIZ;
227 
228 	return pre_tun_act;
229 }
230 
231 static int
232 nfp_fl_set_ipv4_udp_tun(struct nfp_fl_set_ipv4_udp_tun *set_tun,
233 			const struct tc_action *action,
234 			struct nfp_fl_pre_tunnel *pre_tun,
235 			enum nfp_flower_tun_type tun_type,
236 			struct net_device *netdev)
237 {
238 	size_t act_size = sizeof(struct nfp_fl_set_ipv4_udp_tun);
239 	struct ip_tunnel_info *ip_tun = tcf_tunnel_info(action);
240 	u32 tmp_set_ip_tun_type_index = 0;
241 	struct flowi4 flow = {};
242 	/* Currently support one pre-tunnel so index is always 0. */
243 	int pretun_idx = 0;
244 	struct rtable *rt;
245 	struct net *net;
246 	int err;
247 
248 	if (ip_tun->options_len)
249 		return -EOPNOTSUPP;
250 
251 	net = dev_net(netdev);
252 
253 	set_tun->head.jump_id = NFP_FL_ACTION_OPCODE_SET_IPV4_TUNNEL;
254 	set_tun->head.len_lw = act_size >> NFP_FL_LW_SIZ;
255 
256 	/* Set tunnel type and pre-tunnel index. */
257 	tmp_set_ip_tun_type_index |=
258 		FIELD_PREP(NFP_FL_IPV4_TUNNEL_TYPE, tun_type) |
259 		FIELD_PREP(NFP_FL_IPV4_PRE_TUN_INDEX, pretun_idx);
260 
261 	set_tun->tun_type_index = cpu_to_be32(tmp_set_ip_tun_type_index);
262 	set_tun->tun_id = ip_tun->key.tun_id;
263 
264 	/* Do a route lookup to determine ttl - if fails then use default.
265 	 * Note that CONFIG_INET is a requirement of CONFIG_NET_SWITCHDEV so
266 	 * must be defined here.
267 	 */
268 	flow.daddr = ip_tun->key.u.ipv4.dst;
269 	flow.flowi4_proto = IPPROTO_UDP;
270 	rt = ip_route_output_key(net, &flow);
271 	err = PTR_ERR_OR_ZERO(rt);
272 	if (!err) {
273 		set_tun->ttl = ip4_dst_hoplimit(&rt->dst);
274 		ip_rt_put(rt);
275 	} else {
276 		set_tun->ttl = net->ipv4.sysctl_ip_default_ttl;
277 	}
278 
279 	set_tun->tos = ip_tun->key.tos;
280 
281 	if (!(ip_tun->key.tun_flags & TUNNEL_KEY) ||
282 	    ip_tun->key.tun_flags & ~NFP_FL_SUPPORTED_IPV4_UDP_TUN_FLAGS)
283 		return -EOPNOTSUPP;
284 	set_tun->tun_flags = ip_tun->key.tun_flags;
285 
286 	/* Complete pre_tunnel action. */
287 	pre_tun->ipv4_dst = ip_tun->key.u.ipv4.dst;
288 
289 	return 0;
290 }
291 
292 static void nfp_fl_set_helper32(u32 value, u32 mask, u8 *p_exact, u8 *p_mask)
293 {
294 	u32 oldvalue = get_unaligned((u32 *)p_exact);
295 	u32 oldmask = get_unaligned((u32 *)p_mask);
296 
297 	value &= mask;
298 	value |= oldvalue & ~mask;
299 
300 	put_unaligned(oldmask | mask, (u32 *)p_mask);
301 	put_unaligned(value, (u32 *)p_exact);
302 }
303 
304 static int
305 nfp_fl_set_eth(const struct tc_action *action, int idx, u32 off,
306 	       struct nfp_fl_set_eth *set_eth)
307 {
308 	u32 exact, mask;
309 
310 	if (off + 4 > ETH_ALEN * 2)
311 		return -EOPNOTSUPP;
312 
313 	mask = ~tcf_pedit_mask(action, idx);
314 	exact = tcf_pedit_val(action, idx);
315 
316 	if (exact & ~mask)
317 		return -EOPNOTSUPP;
318 
319 	nfp_fl_set_helper32(exact, mask, &set_eth->eth_addr_val[off],
320 			    &set_eth->eth_addr_mask[off]);
321 
322 	set_eth->reserved = cpu_to_be16(0);
323 	set_eth->head.jump_id = NFP_FL_ACTION_OPCODE_SET_ETHERNET;
324 	set_eth->head.len_lw = sizeof(*set_eth) >> NFP_FL_LW_SIZ;
325 
326 	return 0;
327 }
328 
329 static int
330 nfp_fl_set_ip4(const struct tc_action *action, int idx, u32 off,
331 	       struct nfp_fl_set_ip4_addrs *set_ip_addr)
332 {
333 	__be32 exact, mask;
334 
335 	/* We are expecting tcf_pedit to return a big endian value */
336 	mask = (__force __be32)~tcf_pedit_mask(action, idx);
337 	exact = (__force __be32)tcf_pedit_val(action, idx);
338 
339 	if (exact & ~mask)
340 		return -EOPNOTSUPP;
341 
342 	switch (off) {
343 	case offsetof(struct iphdr, daddr):
344 		set_ip_addr->ipv4_dst_mask = mask;
345 		set_ip_addr->ipv4_dst = exact;
346 		break;
347 	case offsetof(struct iphdr, saddr):
348 		set_ip_addr->ipv4_src_mask = mask;
349 		set_ip_addr->ipv4_src = exact;
350 		break;
351 	default:
352 		return -EOPNOTSUPP;
353 	}
354 
355 	set_ip_addr->reserved = cpu_to_be16(0);
356 	set_ip_addr->head.jump_id = NFP_FL_ACTION_OPCODE_SET_IPV4_ADDRS;
357 	set_ip_addr->head.len_lw = sizeof(*set_ip_addr) >> NFP_FL_LW_SIZ;
358 
359 	return 0;
360 }
361 
362 static void
363 nfp_fl_set_ip6_helper(int opcode_tag, int idx, __be32 exact, __be32 mask,
364 		      struct nfp_fl_set_ipv6_addr *ip6)
365 {
366 	ip6->ipv6[idx % 4].mask = mask;
367 	ip6->ipv6[idx % 4].exact = exact;
368 
369 	ip6->reserved = cpu_to_be16(0);
370 	ip6->head.jump_id = opcode_tag;
371 	ip6->head.len_lw = sizeof(*ip6) >> NFP_FL_LW_SIZ;
372 }
373 
374 static int
375 nfp_fl_set_ip6(const struct tc_action *action, int idx, u32 off,
376 	       struct nfp_fl_set_ipv6_addr *ip_dst,
377 	       struct nfp_fl_set_ipv6_addr *ip_src)
378 {
379 	__be32 exact, mask;
380 
381 	/* We are expecting tcf_pedit to return a big endian value */
382 	mask = (__force __be32)~tcf_pedit_mask(action, idx);
383 	exact = (__force __be32)tcf_pedit_val(action, idx);
384 
385 	if (exact & ~mask)
386 		return -EOPNOTSUPP;
387 
388 	if (off < offsetof(struct ipv6hdr, saddr))
389 		return -EOPNOTSUPP;
390 	else if (off < offsetof(struct ipv6hdr, daddr))
391 		nfp_fl_set_ip6_helper(NFP_FL_ACTION_OPCODE_SET_IPV6_SRC, idx,
392 				      exact, mask, ip_src);
393 	else if (off < offsetof(struct ipv6hdr, daddr) +
394 		       sizeof(struct in6_addr))
395 		nfp_fl_set_ip6_helper(NFP_FL_ACTION_OPCODE_SET_IPV6_DST, idx,
396 				      exact, mask, ip_dst);
397 	else
398 		return -EOPNOTSUPP;
399 
400 	return 0;
401 }
402 
403 static int
404 nfp_fl_set_tport(const struct tc_action *action, int idx, u32 off,
405 		 struct nfp_fl_set_tport *set_tport, int opcode)
406 {
407 	u32 exact, mask;
408 
409 	if (off)
410 		return -EOPNOTSUPP;
411 
412 	mask = ~tcf_pedit_mask(action, idx);
413 	exact = tcf_pedit_val(action, idx);
414 
415 	if (exact & ~mask)
416 		return -EOPNOTSUPP;
417 
418 	nfp_fl_set_helper32(exact, mask, set_tport->tp_port_val,
419 			    set_tport->tp_port_mask);
420 
421 	set_tport->reserved = cpu_to_be16(0);
422 	set_tport->head.jump_id = opcode;
423 	set_tport->head.len_lw = sizeof(*set_tport) >> NFP_FL_LW_SIZ;
424 
425 	return 0;
426 }
427 
428 static u32 nfp_fl_csum_l4_to_flag(u8 ip_proto)
429 {
430 	switch (ip_proto) {
431 	case 0:
432 		/* Filter doesn't force proto match,
433 		 * both TCP and UDP will be updated if encountered
434 		 */
435 		return TCA_CSUM_UPDATE_FLAG_TCP | TCA_CSUM_UPDATE_FLAG_UDP;
436 	case IPPROTO_TCP:
437 		return TCA_CSUM_UPDATE_FLAG_TCP;
438 	case IPPROTO_UDP:
439 		return TCA_CSUM_UPDATE_FLAG_UDP;
440 	default:
441 		/* All other protocols will be ignored by FW */
442 		return 0;
443 	}
444 }
445 
446 static int
447 nfp_fl_pedit(const struct tc_action *action, struct tc_cls_flower_offload *flow,
448 	     char *nfp_action, int *a_len, u32 *csum_updated)
449 {
450 	struct nfp_fl_set_ipv6_addr set_ip6_dst, set_ip6_src;
451 	struct nfp_fl_set_ip4_addrs set_ip_addr;
452 	struct nfp_fl_set_tport set_tport;
453 	struct nfp_fl_set_eth set_eth;
454 	enum pedit_header_type htype;
455 	int idx, nkeys, err;
456 	size_t act_size;
457 	u32 offset, cmd;
458 	u8 ip_proto = 0;
459 
460 	memset(&set_ip6_dst, 0, sizeof(set_ip6_dst));
461 	memset(&set_ip6_src, 0, sizeof(set_ip6_src));
462 	memset(&set_ip_addr, 0, sizeof(set_ip_addr));
463 	memset(&set_tport, 0, sizeof(set_tport));
464 	memset(&set_eth, 0, sizeof(set_eth));
465 	nkeys = tcf_pedit_nkeys(action);
466 
467 	for (idx = 0; idx < nkeys; idx++) {
468 		cmd = tcf_pedit_cmd(action, idx);
469 		htype = tcf_pedit_htype(action, idx);
470 		offset = tcf_pedit_offset(action, idx);
471 
472 		if (cmd != TCA_PEDIT_KEY_EX_CMD_SET)
473 			return -EOPNOTSUPP;
474 
475 		switch (htype) {
476 		case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
477 			err = nfp_fl_set_eth(action, idx, offset, &set_eth);
478 			break;
479 		case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
480 			err = nfp_fl_set_ip4(action, idx, offset, &set_ip_addr);
481 			break;
482 		case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
483 			err = nfp_fl_set_ip6(action, idx, offset, &set_ip6_dst,
484 					     &set_ip6_src);
485 			break;
486 		case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
487 			err = nfp_fl_set_tport(action, idx, offset, &set_tport,
488 					       NFP_FL_ACTION_OPCODE_SET_TCP);
489 			break;
490 		case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
491 			err = nfp_fl_set_tport(action, idx, offset, &set_tport,
492 					       NFP_FL_ACTION_OPCODE_SET_UDP);
493 			break;
494 		default:
495 			return -EOPNOTSUPP;
496 		}
497 		if (err)
498 			return err;
499 	}
500 
501 	if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
502 		struct flow_dissector_key_basic *basic;
503 
504 		basic = skb_flow_dissector_target(flow->dissector,
505 						  FLOW_DISSECTOR_KEY_BASIC,
506 						  flow->key);
507 		ip_proto = basic->ip_proto;
508 	}
509 
510 	if (set_eth.head.len_lw) {
511 		act_size = sizeof(set_eth);
512 		memcpy(nfp_action, &set_eth, act_size);
513 		*a_len += act_size;
514 	} else if (set_ip_addr.head.len_lw) {
515 		act_size = sizeof(set_ip_addr);
516 		memcpy(nfp_action, &set_ip_addr, act_size);
517 		*a_len += act_size;
518 
519 		/* Hardware will automatically fix IPv4 and TCP/UDP checksum. */
520 		*csum_updated |= TCA_CSUM_UPDATE_FLAG_IPV4HDR |
521 				nfp_fl_csum_l4_to_flag(ip_proto);
522 	} else if (set_ip6_dst.head.len_lw && set_ip6_src.head.len_lw) {
523 		/* TC compiles set src and dst IPv6 address as a single action,
524 		 * the hardware requires this to be 2 separate actions.
525 		 */
526 		act_size = sizeof(set_ip6_src);
527 		memcpy(nfp_action, &set_ip6_src, act_size);
528 		*a_len += act_size;
529 
530 		act_size = sizeof(set_ip6_dst);
531 		memcpy(&nfp_action[sizeof(set_ip6_src)], &set_ip6_dst,
532 		       act_size);
533 		*a_len += act_size;
534 
535 		/* Hardware will automatically fix TCP/UDP checksum. */
536 		*csum_updated |= nfp_fl_csum_l4_to_flag(ip_proto);
537 	} else if (set_ip6_dst.head.len_lw) {
538 		act_size = sizeof(set_ip6_dst);
539 		memcpy(nfp_action, &set_ip6_dst, act_size);
540 		*a_len += act_size;
541 
542 		/* Hardware will automatically fix TCP/UDP checksum. */
543 		*csum_updated |= nfp_fl_csum_l4_to_flag(ip_proto);
544 	} else if (set_ip6_src.head.len_lw) {
545 		act_size = sizeof(set_ip6_src);
546 		memcpy(nfp_action, &set_ip6_src, act_size);
547 		*a_len += act_size;
548 
549 		/* Hardware will automatically fix TCP/UDP checksum. */
550 		*csum_updated |= nfp_fl_csum_l4_to_flag(ip_proto);
551 	} else if (set_tport.head.len_lw) {
552 		act_size = sizeof(set_tport);
553 		memcpy(nfp_action, &set_tport, act_size);
554 		*a_len += act_size;
555 
556 		/* Hardware will automatically fix TCP/UDP checksum. */
557 		*csum_updated |= nfp_fl_csum_l4_to_flag(ip_proto);
558 	}
559 
560 	return 0;
561 }
562 
563 static int
564 nfp_flower_output_action(struct nfp_app *app, const struct tc_action *a,
565 			 struct nfp_fl_payload *nfp_fl, int *a_len,
566 			 struct net_device *netdev, bool last,
567 			 enum nfp_flower_tun_type *tun_type, int *tun_out_cnt,
568 			 int *out_cnt, u32 *csum_updated)
569 {
570 	struct nfp_flower_priv *priv = app->priv;
571 	struct nfp_fl_output *output;
572 	int err, prelag_size;
573 
574 	/* If csum_updated has not been reset by now, it means HW will
575 	 * incorrectly update csums when they are not requested.
576 	 */
577 	if (*csum_updated)
578 		return -EOPNOTSUPP;
579 
580 	if (*a_len + sizeof(struct nfp_fl_output) > NFP_FL_MAX_A_SIZ)
581 		return -EOPNOTSUPP;
582 
583 	output = (struct nfp_fl_output *)&nfp_fl->action_data[*a_len];
584 	err = nfp_fl_output(app, output, a, nfp_fl, last, netdev, *tun_type,
585 			    tun_out_cnt);
586 	if (err)
587 		return err;
588 
589 	*a_len += sizeof(struct nfp_fl_output);
590 
591 	if (priv->flower_ext_feats & NFP_FL_FEATS_LAG) {
592 		/* nfp_fl_pre_lag returns -err or size of prelag action added.
593 		 * This will be 0 if it is not egressing to a lag dev.
594 		 */
595 		prelag_size = nfp_fl_pre_lag(app, a, nfp_fl, *a_len);
596 		if (prelag_size < 0)
597 			return prelag_size;
598 		else if (prelag_size > 0 && (!last || *out_cnt))
599 			return -EOPNOTSUPP;
600 
601 		*a_len += prelag_size;
602 	}
603 	(*out_cnt)++;
604 
605 	return 0;
606 }
607 
608 static int
609 nfp_flower_loop_action(struct nfp_app *app, const struct tc_action *a,
610 		       struct tc_cls_flower_offload *flow,
611 		       struct nfp_fl_payload *nfp_fl, int *a_len,
612 		       struct net_device *netdev,
613 		       enum nfp_flower_tun_type *tun_type, int *tun_out_cnt,
614 		       int *out_cnt, u32 *csum_updated)
615 {
616 	struct nfp_fl_set_ipv4_udp_tun *set_tun;
617 	struct nfp_fl_pre_tunnel *pre_tun;
618 	struct nfp_fl_push_vlan *psh_v;
619 	struct nfp_fl_pop_vlan *pop_v;
620 	int err;
621 
622 	if (is_tcf_gact_shot(a)) {
623 		nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_DROP);
624 	} else if (is_tcf_mirred_egress_redirect(a)) {
625 		err = nfp_flower_output_action(app, a, nfp_fl, a_len, netdev,
626 					       true, tun_type, tun_out_cnt,
627 					       out_cnt, csum_updated);
628 		if (err)
629 			return err;
630 
631 	} else if (is_tcf_mirred_egress_mirror(a)) {
632 		err = nfp_flower_output_action(app, a, nfp_fl, a_len, netdev,
633 					       false, tun_type, tun_out_cnt,
634 					       out_cnt, csum_updated);
635 		if (err)
636 			return err;
637 
638 	} else if (is_tcf_vlan(a) && tcf_vlan_action(a) == TCA_VLAN_ACT_POP) {
639 		if (*a_len + sizeof(struct nfp_fl_pop_vlan) > NFP_FL_MAX_A_SIZ)
640 			return -EOPNOTSUPP;
641 
642 		pop_v = (struct nfp_fl_pop_vlan *)&nfp_fl->action_data[*a_len];
643 		nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_POPV);
644 
645 		nfp_fl_pop_vlan(pop_v);
646 		*a_len += sizeof(struct nfp_fl_pop_vlan);
647 	} else if (is_tcf_vlan(a) && tcf_vlan_action(a) == TCA_VLAN_ACT_PUSH) {
648 		if (*a_len + sizeof(struct nfp_fl_push_vlan) > NFP_FL_MAX_A_SIZ)
649 			return -EOPNOTSUPP;
650 
651 		psh_v = (struct nfp_fl_push_vlan *)&nfp_fl->action_data[*a_len];
652 		nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
653 
654 		nfp_fl_push_vlan(psh_v, a);
655 		*a_len += sizeof(struct nfp_fl_push_vlan);
656 	} else if (is_tcf_tunnel_set(a)) {
657 		struct nfp_repr *repr = netdev_priv(netdev);
658 		*tun_type = nfp_fl_get_tun_from_act_l4_port(repr->app, a);
659 		if (*tun_type == NFP_FL_TUNNEL_NONE)
660 			return -EOPNOTSUPP;
661 
662 		/* Pre-tunnel action is required for tunnel encap.
663 		 * This checks for next hop entries on NFP.
664 		 * If none, the packet falls back before applying other actions.
665 		 */
666 		if (*a_len + sizeof(struct nfp_fl_pre_tunnel) +
667 		    sizeof(struct nfp_fl_set_ipv4_udp_tun) > NFP_FL_MAX_A_SIZ)
668 			return -EOPNOTSUPP;
669 
670 		pre_tun = nfp_fl_pre_tunnel(nfp_fl->action_data, *a_len);
671 		nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
672 		*a_len += sizeof(struct nfp_fl_pre_tunnel);
673 
674 		set_tun = (void *)&nfp_fl->action_data[*a_len];
675 		err = nfp_fl_set_ipv4_udp_tun(set_tun, a, pre_tun, *tun_type,
676 					      netdev);
677 		if (err)
678 			return err;
679 		*a_len += sizeof(struct nfp_fl_set_ipv4_udp_tun);
680 	} else if (is_tcf_tunnel_release(a)) {
681 		/* Tunnel decap is handled by default so accept action. */
682 		return 0;
683 	} else if (is_tcf_pedit(a)) {
684 		if (nfp_fl_pedit(a, flow, &nfp_fl->action_data[*a_len],
685 				 a_len, csum_updated))
686 			return -EOPNOTSUPP;
687 	} else if (is_tcf_csum(a)) {
688 		/* csum action requests recalc of something we have not fixed */
689 		if (tcf_csum_update_flags(a) & ~*csum_updated)
690 			return -EOPNOTSUPP;
691 		/* If we will correctly fix the csum we can remove it from the
692 		 * csum update list. Which will later be used to check support.
693 		 */
694 		*csum_updated &= ~tcf_csum_update_flags(a);
695 	} else {
696 		/* Currently we do not handle any other actions. */
697 		return -EOPNOTSUPP;
698 	}
699 
700 	return 0;
701 }
702 
703 int nfp_flower_compile_action(struct nfp_app *app,
704 			      struct tc_cls_flower_offload *flow,
705 			      struct net_device *netdev,
706 			      struct nfp_fl_payload *nfp_flow)
707 {
708 	int act_len, act_cnt, err, tun_out_cnt, out_cnt;
709 	enum nfp_flower_tun_type tun_type;
710 	const struct tc_action *a;
711 	u32 csum_updated = 0;
712 	LIST_HEAD(actions);
713 
714 	memset(nfp_flow->action_data, 0, NFP_FL_MAX_A_SIZ);
715 	nfp_flow->meta.act_len = 0;
716 	tun_type = NFP_FL_TUNNEL_NONE;
717 	act_len = 0;
718 	act_cnt = 0;
719 	tun_out_cnt = 0;
720 	out_cnt = 0;
721 
722 	tcf_exts_to_list(flow->exts, &actions);
723 	list_for_each_entry(a, &actions, list) {
724 		err = nfp_flower_loop_action(app, a, flow, nfp_flow, &act_len,
725 					     netdev, &tun_type, &tun_out_cnt,
726 					     &out_cnt, &csum_updated);
727 		if (err)
728 			return err;
729 		act_cnt++;
730 	}
731 
732 	/* We optimise when the action list is small, this can unfortunately
733 	 * not happen once we have more than one action in the action list.
734 	 */
735 	if (act_cnt > 1)
736 		nfp_flow->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
737 
738 	nfp_flow->meta.act_len = act_len;
739 
740 	return 0;
741 }
742