xref: /openbmc/linux/drivers/net/ethernet/netronome/nfp/flower/action.c (revision a1b2f04ea527397fcacacd09e0d690927feef429)
1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
3 
4 #include <linux/bitfield.h>
5 #include <linux/mpls.h>
6 #include <net/pkt_cls.h>
7 #include <net/tc_act/tc_csum.h>
8 #include <net/tc_act/tc_gact.h>
9 #include <net/tc_act/tc_mirred.h>
10 #include <net/tc_act/tc_mpls.h>
11 #include <net/tc_act/tc_pedit.h>
12 #include <net/tc_act/tc_vlan.h>
13 #include <net/tc_act/tc_tunnel_key.h>
14 
15 #include "cmsg.h"
16 #include "main.h"
17 #include "../nfp_net_repr.h"
18 
19 /* The kernel versions of TUNNEL_* are not ABI and therefore vulnerable
20  * to change. Such changes will break our FW ABI.
21  */
22 #define NFP_FL_TUNNEL_CSUM			cpu_to_be16(0x01)
23 #define NFP_FL_TUNNEL_KEY			cpu_to_be16(0x04)
24 #define NFP_FL_TUNNEL_GENEVE_OPT		cpu_to_be16(0x0800)
25 #define NFP_FL_SUPPORTED_TUNNEL_INFO_FLAGS	IP_TUNNEL_INFO_TX
26 #define NFP_FL_SUPPORTED_IPV4_UDP_TUN_FLAGS	(NFP_FL_TUNNEL_CSUM | \
27 						 NFP_FL_TUNNEL_KEY | \
28 						 NFP_FL_TUNNEL_GENEVE_OPT)
29 
30 static int
31 nfp_fl_push_mpls(struct nfp_fl_push_mpls *push_mpls,
32 		 const struct flow_action_entry *act,
33 		 struct netlink_ext_ack *extack)
34 {
35 	size_t act_size = sizeof(struct nfp_fl_push_mpls);
36 	u32 mpls_lse = 0;
37 
38 	push_mpls->head.jump_id = NFP_FL_ACTION_OPCODE_PUSH_MPLS;
39 	push_mpls->head.len_lw = act_size >> NFP_FL_LW_SIZ;
40 
41 	/* BOS is optional in the TC action but required for offload. */
42 	if (act->mpls_push.bos != ACT_MPLS_BOS_NOT_SET) {
43 		mpls_lse |= act->mpls_push.bos << MPLS_LS_S_SHIFT;
44 	} else {
45 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: BOS field must explicitly be set for MPLS push");
46 		return -EOPNOTSUPP;
47 	}
48 
49 	/* Leave MPLS TC as a default value of 0 if not explicitly set. */
50 	if (act->mpls_push.tc != ACT_MPLS_TC_NOT_SET)
51 		mpls_lse |= act->mpls_push.tc << MPLS_LS_TC_SHIFT;
52 
53 	/* Proto, label and TTL are enforced and verified for MPLS push. */
54 	mpls_lse |= act->mpls_push.label << MPLS_LS_LABEL_SHIFT;
55 	mpls_lse |= act->mpls_push.ttl << MPLS_LS_TTL_SHIFT;
56 	push_mpls->ethtype = act->mpls_push.proto;
57 	push_mpls->lse = cpu_to_be32(mpls_lse);
58 
59 	return 0;
60 }
61 
62 static void
63 nfp_fl_pop_mpls(struct nfp_fl_pop_mpls *pop_mpls,
64 		const struct flow_action_entry *act)
65 {
66 	size_t act_size = sizeof(struct nfp_fl_pop_mpls);
67 
68 	pop_mpls->head.jump_id = NFP_FL_ACTION_OPCODE_POP_MPLS;
69 	pop_mpls->head.len_lw = act_size >> NFP_FL_LW_SIZ;
70 	pop_mpls->ethtype = act->mpls_pop.proto;
71 }
72 
73 static void
74 nfp_fl_set_mpls(struct nfp_fl_set_mpls *set_mpls,
75 		const struct flow_action_entry *act)
76 {
77 	size_t act_size = sizeof(struct nfp_fl_set_mpls);
78 	u32 mpls_lse = 0, mpls_mask = 0;
79 
80 	set_mpls->head.jump_id = NFP_FL_ACTION_OPCODE_SET_MPLS;
81 	set_mpls->head.len_lw = act_size >> NFP_FL_LW_SIZ;
82 
83 	if (act->mpls_mangle.label != ACT_MPLS_LABEL_NOT_SET) {
84 		mpls_lse |= act->mpls_mangle.label << MPLS_LS_LABEL_SHIFT;
85 		mpls_mask |= MPLS_LS_LABEL_MASK;
86 	}
87 	if (act->mpls_mangle.tc != ACT_MPLS_TC_NOT_SET) {
88 		mpls_lse |= act->mpls_mangle.tc << MPLS_LS_TC_SHIFT;
89 		mpls_mask |= MPLS_LS_TC_MASK;
90 	}
91 	if (act->mpls_mangle.bos != ACT_MPLS_BOS_NOT_SET) {
92 		mpls_lse |= act->mpls_mangle.bos << MPLS_LS_S_SHIFT;
93 		mpls_mask |= MPLS_LS_S_MASK;
94 	}
95 	if (act->mpls_mangle.ttl) {
96 		mpls_lse |= act->mpls_mangle.ttl << MPLS_LS_TTL_SHIFT;
97 		mpls_mask |= MPLS_LS_TTL_MASK;
98 	}
99 
100 	set_mpls->lse = cpu_to_be32(mpls_lse);
101 	set_mpls->lse_mask = cpu_to_be32(mpls_mask);
102 }
103 
104 static void nfp_fl_pop_vlan(struct nfp_fl_pop_vlan *pop_vlan)
105 {
106 	size_t act_size = sizeof(struct nfp_fl_pop_vlan);
107 
108 	pop_vlan->head.jump_id = NFP_FL_ACTION_OPCODE_POP_VLAN;
109 	pop_vlan->head.len_lw = act_size >> NFP_FL_LW_SIZ;
110 	pop_vlan->reserved = 0;
111 }
112 
113 static void
114 nfp_fl_push_vlan(struct nfp_fl_push_vlan *push_vlan,
115 		 const struct flow_action_entry *act)
116 {
117 	size_t act_size = sizeof(struct nfp_fl_push_vlan);
118 	u16 tmp_push_vlan_tci;
119 
120 	push_vlan->head.jump_id = NFP_FL_ACTION_OPCODE_PUSH_VLAN;
121 	push_vlan->head.len_lw = act_size >> NFP_FL_LW_SIZ;
122 	push_vlan->reserved = 0;
123 	push_vlan->vlan_tpid = act->vlan.proto;
124 
125 	tmp_push_vlan_tci =
126 		FIELD_PREP(NFP_FL_PUSH_VLAN_PRIO, act->vlan.prio) |
127 		FIELD_PREP(NFP_FL_PUSH_VLAN_VID, act->vlan.vid);
128 	push_vlan->vlan_tci = cpu_to_be16(tmp_push_vlan_tci);
129 }
130 
131 static int
132 nfp_fl_pre_lag(struct nfp_app *app, const struct flow_action_entry *act,
133 	       struct nfp_fl_payload *nfp_flow, int act_len,
134 	       struct netlink_ext_ack *extack)
135 {
136 	size_t act_size = sizeof(struct nfp_fl_pre_lag);
137 	struct nfp_fl_pre_lag *pre_lag;
138 	struct net_device *out_dev;
139 	int err;
140 
141 	out_dev = act->dev;
142 	if (!out_dev || !netif_is_lag_master(out_dev))
143 		return 0;
144 
145 	if (act_len + act_size > NFP_FL_MAX_A_SIZ) {
146 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: maximum allowed action list size exceeded at LAG action");
147 		return -EOPNOTSUPP;
148 	}
149 
150 	/* Pre_lag action must be first on action list.
151 	 * If other actions already exist they need pushed forward.
152 	 */
153 	if (act_len)
154 		memmove(nfp_flow->action_data + act_size,
155 			nfp_flow->action_data, act_len);
156 
157 	pre_lag = (struct nfp_fl_pre_lag *)nfp_flow->action_data;
158 	err = nfp_flower_lag_populate_pre_action(app, out_dev, pre_lag, extack);
159 	if (err)
160 		return err;
161 
162 	pre_lag->head.jump_id = NFP_FL_ACTION_OPCODE_PRE_LAG;
163 	pre_lag->head.len_lw = act_size >> NFP_FL_LW_SIZ;
164 
165 	nfp_flow->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
166 
167 	return act_size;
168 }
169 
170 static int
171 nfp_fl_output(struct nfp_app *app, struct nfp_fl_output *output,
172 	      const struct flow_action_entry *act,
173 	      struct nfp_fl_payload *nfp_flow,
174 	      bool last, struct net_device *in_dev,
175 	      enum nfp_flower_tun_type tun_type, int *tun_out_cnt,
176 	      struct netlink_ext_ack *extack)
177 {
178 	size_t act_size = sizeof(struct nfp_fl_output);
179 	struct nfp_flower_priv *priv = app->priv;
180 	struct net_device *out_dev;
181 	u16 tmp_flags;
182 
183 	output->head.jump_id = NFP_FL_ACTION_OPCODE_OUTPUT;
184 	output->head.len_lw = act_size >> NFP_FL_LW_SIZ;
185 
186 	out_dev = act->dev;
187 	if (!out_dev) {
188 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: invalid egress interface for mirred action");
189 		return -EOPNOTSUPP;
190 	}
191 
192 	tmp_flags = last ? NFP_FL_OUT_FLAGS_LAST : 0;
193 
194 	if (tun_type) {
195 		/* Verify the egress netdev matches the tunnel type. */
196 		if (!nfp_fl_netdev_is_tunnel_type(out_dev, tun_type)) {
197 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: egress interface does not match the required tunnel type");
198 			return -EOPNOTSUPP;
199 		}
200 
201 		if (*tun_out_cnt) {
202 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: cannot offload more than one tunnel mirred output per filter");
203 			return -EOPNOTSUPP;
204 		}
205 		(*tun_out_cnt)++;
206 
207 		output->flags = cpu_to_be16(tmp_flags |
208 					    NFP_FL_OUT_FLAGS_USE_TUN);
209 		output->port = cpu_to_be32(NFP_FL_PORT_TYPE_TUN | tun_type);
210 	} else if (netif_is_lag_master(out_dev) &&
211 		   priv->flower_ext_feats & NFP_FL_FEATS_LAG) {
212 		int gid;
213 
214 		output->flags = cpu_to_be16(tmp_flags);
215 		gid = nfp_flower_lag_get_output_id(app, out_dev);
216 		if (gid < 0) {
217 			NL_SET_ERR_MSG_MOD(extack, "invalid entry: cannot find group id for LAG action");
218 			return gid;
219 		}
220 		output->port = cpu_to_be32(NFP_FL_LAG_OUT | gid);
221 	} else {
222 		/* Set action output parameters. */
223 		output->flags = cpu_to_be16(tmp_flags);
224 
225 		if (nfp_netdev_is_nfp_repr(in_dev)) {
226 			/* Confirm ingress and egress are on same device. */
227 			if (!netdev_port_same_parent_id(in_dev, out_dev)) {
228 				NL_SET_ERR_MSG_MOD(extack, "unsupported offload: ingress and egress interfaces are on different devices");
229 				return -EOPNOTSUPP;
230 			}
231 		}
232 
233 		if (!nfp_netdev_is_nfp_repr(out_dev)) {
234 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: egress interface is not an nfp port");
235 			return -EOPNOTSUPP;
236 		}
237 
238 		output->port = cpu_to_be32(nfp_repr_get_port_id(out_dev));
239 		if (!output->port) {
240 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: invalid port id for egress interface");
241 			return -EOPNOTSUPP;
242 		}
243 	}
244 	nfp_flow->meta.shortcut = output->port;
245 
246 	return 0;
247 }
248 
249 static bool
250 nfp_flower_tun_is_gre(struct flow_cls_offload *flow, int start_idx)
251 {
252 	struct flow_action_entry *act = flow->rule->action.entries;
253 	int num_act = flow->rule->action.num_entries;
254 	int act_idx;
255 
256 	/* Preparse action list for next mirred or redirect action */
257 	for (act_idx = start_idx + 1; act_idx < num_act; act_idx++)
258 		if (act[act_idx].id == FLOW_ACTION_REDIRECT ||
259 		    act[act_idx].id == FLOW_ACTION_MIRRED)
260 			return netif_is_gretap(act[act_idx].dev);
261 
262 	return false;
263 }
264 
265 static enum nfp_flower_tun_type
266 nfp_fl_get_tun_from_act(struct nfp_app *app,
267 			struct flow_cls_offload *flow,
268 			const struct flow_action_entry *act, int act_idx)
269 {
270 	const struct ip_tunnel_info *tun = act->tunnel;
271 	struct nfp_flower_priv *priv = app->priv;
272 
273 	/* Determine the tunnel type based on the egress netdev
274 	 * in the mirred action for tunnels without l4.
275 	 */
276 	if (nfp_flower_tun_is_gre(flow, act_idx))
277 		return NFP_FL_TUNNEL_GRE;
278 
279 	switch (tun->key.tp_dst) {
280 	case htons(IANA_VXLAN_UDP_PORT):
281 		return NFP_FL_TUNNEL_VXLAN;
282 	case htons(GENEVE_UDP_PORT):
283 		if (priv->flower_ext_feats & NFP_FL_FEATS_GENEVE)
284 			return NFP_FL_TUNNEL_GENEVE;
285 		/* FALLTHROUGH */
286 	default:
287 		return NFP_FL_TUNNEL_NONE;
288 	}
289 }
290 
291 static struct nfp_fl_pre_tunnel *nfp_fl_pre_tunnel(char *act_data, int act_len)
292 {
293 	size_t act_size = sizeof(struct nfp_fl_pre_tunnel);
294 	struct nfp_fl_pre_tunnel *pre_tun_act;
295 
296 	/* Pre_tunnel action must be first on action list.
297 	 * If other actions already exist they need to be pushed forward.
298 	 */
299 	if (act_len)
300 		memmove(act_data + act_size, act_data, act_len);
301 
302 	pre_tun_act = (struct nfp_fl_pre_tunnel *)act_data;
303 
304 	memset(pre_tun_act, 0, act_size);
305 
306 	pre_tun_act->head.jump_id = NFP_FL_ACTION_OPCODE_PRE_TUNNEL;
307 	pre_tun_act->head.len_lw = act_size >> NFP_FL_LW_SIZ;
308 
309 	return pre_tun_act;
310 }
311 
312 static int
313 nfp_fl_push_geneve_options(struct nfp_fl_payload *nfp_fl, int *list_len,
314 			   const struct flow_action_entry *act,
315 			   struct netlink_ext_ack *extack)
316 {
317 	struct ip_tunnel_info *ip_tun = (struct ip_tunnel_info *)act->tunnel;
318 	int opt_len, opt_cnt, act_start, tot_push_len;
319 	u8 *src = ip_tunnel_info_opts(ip_tun);
320 
321 	/* We need to populate the options in reverse order for HW.
322 	 * Therefore we go through the options, calculating the
323 	 * number of options and the total size, then we populate
324 	 * them in reverse order in the action list.
325 	 */
326 	opt_cnt = 0;
327 	tot_push_len = 0;
328 	opt_len = ip_tun->options_len;
329 	while (opt_len > 0) {
330 		struct geneve_opt *opt = (struct geneve_opt *)src;
331 
332 		opt_cnt++;
333 		if (opt_cnt > NFP_FL_MAX_GENEVE_OPT_CNT) {
334 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: maximum allowed number of geneve options exceeded");
335 			return -EOPNOTSUPP;
336 		}
337 
338 		tot_push_len += sizeof(struct nfp_fl_push_geneve) +
339 			       opt->length * 4;
340 		if (tot_push_len > NFP_FL_MAX_GENEVE_OPT_ACT) {
341 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: maximum allowed action list size exceeded at push geneve options");
342 			return -EOPNOTSUPP;
343 		}
344 
345 		opt_len -= sizeof(struct geneve_opt) + opt->length * 4;
346 		src += sizeof(struct geneve_opt) + opt->length * 4;
347 	}
348 
349 	if (*list_len + tot_push_len > NFP_FL_MAX_A_SIZ) {
350 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: maximum allowed action list size exceeded at push geneve options");
351 		return -EOPNOTSUPP;
352 	}
353 
354 	act_start = *list_len;
355 	*list_len += tot_push_len;
356 	src = ip_tunnel_info_opts(ip_tun);
357 	while (opt_cnt) {
358 		struct geneve_opt *opt = (struct geneve_opt *)src;
359 		struct nfp_fl_push_geneve *push;
360 		size_t act_size, len;
361 
362 		opt_cnt--;
363 		act_size = sizeof(struct nfp_fl_push_geneve) + opt->length * 4;
364 		tot_push_len -= act_size;
365 		len = act_start + tot_push_len;
366 
367 		push = (struct nfp_fl_push_geneve *)&nfp_fl->action_data[len];
368 		push->head.jump_id = NFP_FL_ACTION_OPCODE_PUSH_GENEVE;
369 		push->head.len_lw = act_size >> NFP_FL_LW_SIZ;
370 		push->reserved = 0;
371 		push->class = opt->opt_class;
372 		push->type = opt->type;
373 		push->length = opt->length;
374 		memcpy(&push->opt_data, opt->opt_data, opt->length * 4);
375 
376 		src += sizeof(struct geneve_opt) + opt->length * 4;
377 	}
378 
379 	return 0;
380 }
381 
382 static int
383 nfp_fl_set_ipv4_tun(struct nfp_app *app, struct nfp_fl_set_ipv4_tun *set_tun,
384 		    const struct flow_action_entry *act,
385 		    struct nfp_fl_pre_tunnel *pre_tun,
386 		    enum nfp_flower_tun_type tun_type,
387 		    struct net_device *netdev, struct netlink_ext_ack *extack)
388 {
389 	size_t act_size = sizeof(struct nfp_fl_set_ipv4_tun);
390 	const struct ip_tunnel_info *ip_tun = act->tunnel;
391 	struct nfp_flower_priv *priv = app->priv;
392 	u32 tmp_set_ip_tun_type_index = 0;
393 	/* Currently support one pre-tunnel so index is always 0. */
394 	int pretun_idx = 0;
395 
396 	BUILD_BUG_ON(NFP_FL_TUNNEL_CSUM != TUNNEL_CSUM ||
397 		     NFP_FL_TUNNEL_KEY	!= TUNNEL_KEY ||
398 		     NFP_FL_TUNNEL_GENEVE_OPT != TUNNEL_GENEVE_OPT);
399 	if (ip_tun->options_len &&
400 	    (tun_type != NFP_FL_TUNNEL_GENEVE ||
401 	    !(priv->flower_ext_feats & NFP_FL_FEATS_GENEVE_OPT))) {
402 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: loaded firmware does not support geneve options offload");
403 		return -EOPNOTSUPP;
404 	}
405 
406 	set_tun->head.jump_id = NFP_FL_ACTION_OPCODE_SET_IPV4_TUNNEL;
407 	set_tun->head.len_lw = act_size >> NFP_FL_LW_SIZ;
408 
409 	/* Set tunnel type and pre-tunnel index. */
410 	tmp_set_ip_tun_type_index |=
411 		FIELD_PREP(NFP_FL_IPV4_TUNNEL_TYPE, tun_type) |
412 		FIELD_PREP(NFP_FL_IPV4_PRE_TUN_INDEX, pretun_idx);
413 
414 	set_tun->tun_type_index = cpu_to_be32(tmp_set_ip_tun_type_index);
415 	set_tun->tun_id = ip_tun->key.tun_id;
416 
417 	if (ip_tun->key.ttl) {
418 		set_tun->ttl = ip_tun->key.ttl;
419 	} else {
420 		struct net *net = dev_net(netdev);
421 		struct flowi4 flow = {};
422 		struct rtable *rt;
423 		int err;
424 
425 		/* Do a route lookup to determine ttl - if fails then use
426 		 * default. Note that CONFIG_INET is a requirement of
427 		 * CONFIG_NET_SWITCHDEV so must be defined here.
428 		 */
429 		flow.daddr = ip_tun->key.u.ipv4.dst;
430 		flow.flowi4_proto = IPPROTO_UDP;
431 		rt = ip_route_output_key(net, &flow);
432 		err = PTR_ERR_OR_ZERO(rt);
433 		if (!err) {
434 			set_tun->ttl = ip4_dst_hoplimit(&rt->dst);
435 			ip_rt_put(rt);
436 		} else {
437 			set_tun->ttl = net->ipv4.sysctl_ip_default_ttl;
438 		}
439 	}
440 
441 	set_tun->tos = ip_tun->key.tos;
442 
443 	if (!(ip_tun->key.tun_flags & NFP_FL_TUNNEL_KEY) ||
444 	    ip_tun->key.tun_flags & ~NFP_FL_SUPPORTED_IPV4_UDP_TUN_FLAGS) {
445 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: loaded firmware does not support tunnel flag offload");
446 		return -EOPNOTSUPP;
447 	}
448 	set_tun->tun_flags = ip_tun->key.tun_flags;
449 
450 	if (tun_type == NFP_FL_TUNNEL_GENEVE) {
451 		set_tun->tun_proto = htons(ETH_P_TEB);
452 		set_tun->tun_len = ip_tun->options_len / 4;
453 	}
454 
455 	/* Complete pre_tunnel action. */
456 	pre_tun->ipv4_dst = ip_tun->key.u.ipv4.dst;
457 
458 	return 0;
459 }
460 
461 static void nfp_fl_set_helper32(u32 value, u32 mask, u8 *p_exact, u8 *p_mask)
462 {
463 	u32 oldvalue = get_unaligned((u32 *)p_exact);
464 	u32 oldmask = get_unaligned((u32 *)p_mask);
465 
466 	value &= mask;
467 	value |= oldvalue & ~mask;
468 
469 	put_unaligned(oldmask | mask, (u32 *)p_mask);
470 	put_unaligned(value, (u32 *)p_exact);
471 }
472 
473 static int
474 nfp_fl_set_eth(const struct flow_action_entry *act, u32 off,
475 	       struct nfp_fl_set_eth *set_eth, struct netlink_ext_ack *extack)
476 {
477 	u32 exact, mask;
478 
479 	if (off + 4 > ETH_ALEN * 2) {
480 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: invalid pedit ethernet action");
481 		return -EOPNOTSUPP;
482 	}
483 
484 	mask = ~act->mangle.mask;
485 	exact = act->mangle.val;
486 
487 	if (exact & ~mask) {
488 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: invalid pedit ethernet action");
489 		return -EOPNOTSUPP;
490 	}
491 
492 	nfp_fl_set_helper32(exact, mask, &set_eth->eth_addr_val[off],
493 			    &set_eth->eth_addr_mask[off]);
494 
495 	set_eth->reserved = cpu_to_be16(0);
496 	set_eth->head.jump_id = NFP_FL_ACTION_OPCODE_SET_ETHERNET;
497 	set_eth->head.len_lw = sizeof(*set_eth) >> NFP_FL_LW_SIZ;
498 
499 	return 0;
500 }
501 
502 struct ipv4_ttl_word {
503 	__u8	ttl;
504 	__u8	protocol;
505 	__sum16	check;
506 };
507 
508 static int
509 nfp_fl_set_ip4(const struct flow_action_entry *act, u32 off,
510 	       struct nfp_fl_set_ip4_addrs *set_ip_addr,
511 	       struct nfp_fl_set_ip4_ttl_tos *set_ip_ttl_tos,
512 	       struct netlink_ext_ack *extack)
513 {
514 	struct ipv4_ttl_word *ttl_word_mask;
515 	struct ipv4_ttl_word *ttl_word;
516 	struct iphdr *tos_word_mask;
517 	struct iphdr *tos_word;
518 	__be32 exact, mask;
519 
520 	/* We are expecting tcf_pedit to return a big endian value */
521 	mask = (__force __be32)~act->mangle.mask;
522 	exact = (__force __be32)act->mangle.val;
523 
524 	if (exact & ~mask) {
525 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: invalid pedit IPv4 action");
526 		return -EOPNOTSUPP;
527 	}
528 
529 	switch (off) {
530 	case offsetof(struct iphdr, daddr):
531 		set_ip_addr->ipv4_dst_mask |= mask;
532 		set_ip_addr->ipv4_dst &= ~mask;
533 		set_ip_addr->ipv4_dst |= exact & mask;
534 		set_ip_addr->head.jump_id = NFP_FL_ACTION_OPCODE_SET_IPV4_ADDRS;
535 		set_ip_addr->head.len_lw = sizeof(*set_ip_addr) >>
536 					   NFP_FL_LW_SIZ;
537 		break;
538 	case offsetof(struct iphdr, saddr):
539 		set_ip_addr->ipv4_src_mask |= mask;
540 		set_ip_addr->ipv4_src &= ~mask;
541 		set_ip_addr->ipv4_src |= exact & mask;
542 		set_ip_addr->head.jump_id = NFP_FL_ACTION_OPCODE_SET_IPV4_ADDRS;
543 		set_ip_addr->head.len_lw = sizeof(*set_ip_addr) >>
544 					   NFP_FL_LW_SIZ;
545 		break;
546 	case offsetof(struct iphdr, ttl):
547 		ttl_word_mask = (struct ipv4_ttl_word *)&mask;
548 		ttl_word = (struct ipv4_ttl_word *)&exact;
549 
550 		if (ttl_word_mask->protocol || ttl_word_mask->check) {
551 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: invalid pedit IPv4 ttl action");
552 			return -EOPNOTSUPP;
553 		}
554 
555 		set_ip_ttl_tos->ipv4_ttl_mask |= ttl_word_mask->ttl;
556 		set_ip_ttl_tos->ipv4_ttl &= ~ttl_word_mask->ttl;
557 		set_ip_ttl_tos->ipv4_ttl |= ttl_word->ttl & ttl_word_mask->ttl;
558 		set_ip_ttl_tos->head.jump_id =
559 			NFP_FL_ACTION_OPCODE_SET_IPV4_TTL_TOS;
560 		set_ip_ttl_tos->head.len_lw = sizeof(*set_ip_ttl_tos) >>
561 					      NFP_FL_LW_SIZ;
562 		break;
563 	case round_down(offsetof(struct iphdr, tos), 4):
564 		tos_word_mask = (struct iphdr *)&mask;
565 		tos_word = (struct iphdr *)&exact;
566 
567 		if (tos_word_mask->version || tos_word_mask->ihl ||
568 		    tos_word_mask->tot_len) {
569 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: invalid pedit IPv4 tos action");
570 			return -EOPNOTSUPP;
571 		}
572 
573 		set_ip_ttl_tos->ipv4_tos_mask |= tos_word_mask->tos;
574 		set_ip_ttl_tos->ipv4_tos &= ~tos_word_mask->tos;
575 		set_ip_ttl_tos->ipv4_tos |= tos_word->tos & tos_word_mask->tos;
576 		set_ip_ttl_tos->head.jump_id =
577 			NFP_FL_ACTION_OPCODE_SET_IPV4_TTL_TOS;
578 		set_ip_ttl_tos->head.len_lw = sizeof(*set_ip_ttl_tos) >>
579 					      NFP_FL_LW_SIZ;
580 		break;
581 	default:
582 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: pedit on unsupported section of IPv4 header");
583 		return -EOPNOTSUPP;
584 	}
585 
586 	return 0;
587 }
588 
589 static void
590 nfp_fl_set_ip6_helper(int opcode_tag, u8 word, __be32 exact, __be32 mask,
591 		      struct nfp_fl_set_ipv6_addr *ip6)
592 {
593 	ip6->ipv6[word].mask |= mask;
594 	ip6->ipv6[word].exact &= ~mask;
595 	ip6->ipv6[word].exact |= exact & mask;
596 
597 	ip6->reserved = cpu_to_be16(0);
598 	ip6->head.jump_id = opcode_tag;
599 	ip6->head.len_lw = sizeof(*ip6) >> NFP_FL_LW_SIZ;
600 }
601 
602 struct ipv6_hop_limit_word {
603 	__be16 payload_len;
604 	u8 nexthdr;
605 	u8 hop_limit;
606 };
607 
608 static int
609 nfp_fl_set_ip6_hop_limit_flow_label(u32 off, __be32 exact, __be32 mask,
610 				    struct nfp_fl_set_ipv6_tc_hl_fl *ip_hl_fl,
611 				    struct netlink_ext_ack *extack)
612 {
613 	struct ipv6_hop_limit_word *fl_hl_mask;
614 	struct ipv6_hop_limit_word *fl_hl;
615 
616 	switch (off) {
617 	case offsetof(struct ipv6hdr, payload_len):
618 		fl_hl_mask = (struct ipv6_hop_limit_word *)&mask;
619 		fl_hl = (struct ipv6_hop_limit_word *)&exact;
620 
621 		if (fl_hl_mask->nexthdr || fl_hl_mask->payload_len) {
622 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: invalid pedit IPv6 hop limit action");
623 			return -EOPNOTSUPP;
624 		}
625 
626 		ip_hl_fl->ipv6_hop_limit_mask |= fl_hl_mask->hop_limit;
627 		ip_hl_fl->ipv6_hop_limit &= ~fl_hl_mask->hop_limit;
628 		ip_hl_fl->ipv6_hop_limit |= fl_hl->hop_limit &
629 					    fl_hl_mask->hop_limit;
630 		break;
631 	case round_down(offsetof(struct ipv6hdr, flow_lbl), 4):
632 		if (mask & ~IPV6_FLOW_LABEL_MASK ||
633 		    exact & ~IPV6_FLOW_LABEL_MASK) {
634 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: invalid pedit IPv6 flow label action");
635 			return -EOPNOTSUPP;
636 		}
637 
638 		ip_hl_fl->ipv6_label_mask |= mask;
639 		ip_hl_fl->ipv6_label &= ~mask;
640 		ip_hl_fl->ipv6_label |= exact & mask;
641 		break;
642 	}
643 
644 	ip_hl_fl->head.jump_id = NFP_FL_ACTION_OPCODE_SET_IPV6_TC_HL_FL;
645 	ip_hl_fl->head.len_lw = sizeof(*ip_hl_fl) >> NFP_FL_LW_SIZ;
646 
647 	return 0;
648 }
649 
650 static int
651 nfp_fl_set_ip6(const struct flow_action_entry *act, u32 off,
652 	       struct nfp_fl_set_ipv6_addr *ip_dst,
653 	       struct nfp_fl_set_ipv6_addr *ip_src,
654 	       struct nfp_fl_set_ipv6_tc_hl_fl *ip_hl_fl,
655 	       struct netlink_ext_ack *extack)
656 {
657 	__be32 exact, mask;
658 	int err = 0;
659 	u8 word;
660 
661 	/* We are expecting tcf_pedit to return a big endian value */
662 	mask = (__force __be32)~act->mangle.mask;
663 	exact = (__force __be32)act->mangle.val;
664 
665 	if (exact & ~mask) {
666 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: invalid pedit IPv6 action");
667 		return -EOPNOTSUPP;
668 	}
669 
670 	if (off < offsetof(struct ipv6hdr, saddr)) {
671 		err = nfp_fl_set_ip6_hop_limit_flow_label(off, exact, mask,
672 							  ip_hl_fl, extack);
673 	} else if (off < offsetof(struct ipv6hdr, daddr)) {
674 		word = (off - offsetof(struct ipv6hdr, saddr)) / sizeof(exact);
675 		nfp_fl_set_ip6_helper(NFP_FL_ACTION_OPCODE_SET_IPV6_SRC, word,
676 				      exact, mask, ip_src);
677 	} else if (off < offsetof(struct ipv6hdr, daddr) +
678 		       sizeof(struct in6_addr)) {
679 		word = (off - offsetof(struct ipv6hdr, daddr)) / sizeof(exact);
680 		nfp_fl_set_ip6_helper(NFP_FL_ACTION_OPCODE_SET_IPV6_DST, word,
681 				      exact, mask, ip_dst);
682 	} else {
683 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: pedit on unsupported section of IPv6 header");
684 		return -EOPNOTSUPP;
685 	}
686 
687 	return err;
688 }
689 
690 static int
691 nfp_fl_set_tport(const struct flow_action_entry *act, u32 off,
692 		 struct nfp_fl_set_tport *set_tport, int opcode,
693 		 struct netlink_ext_ack *extack)
694 {
695 	u32 exact, mask;
696 
697 	if (off) {
698 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: pedit on unsupported section of L4 header");
699 		return -EOPNOTSUPP;
700 	}
701 
702 	mask = ~act->mangle.mask;
703 	exact = act->mangle.val;
704 
705 	if (exact & ~mask) {
706 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: invalid pedit L4 action");
707 		return -EOPNOTSUPP;
708 	}
709 
710 	nfp_fl_set_helper32(exact, mask, set_tport->tp_port_val,
711 			    set_tport->tp_port_mask);
712 
713 	set_tport->reserved = cpu_to_be16(0);
714 	set_tport->head.jump_id = opcode;
715 	set_tport->head.len_lw = sizeof(*set_tport) >> NFP_FL_LW_SIZ;
716 
717 	return 0;
718 }
719 
720 static u32 nfp_fl_csum_l4_to_flag(u8 ip_proto)
721 {
722 	switch (ip_proto) {
723 	case 0:
724 		/* Filter doesn't force proto match,
725 		 * both TCP and UDP will be updated if encountered
726 		 */
727 		return TCA_CSUM_UPDATE_FLAG_TCP | TCA_CSUM_UPDATE_FLAG_UDP;
728 	case IPPROTO_TCP:
729 		return TCA_CSUM_UPDATE_FLAG_TCP;
730 	case IPPROTO_UDP:
731 		return TCA_CSUM_UPDATE_FLAG_UDP;
732 	default:
733 		/* All other protocols will be ignored by FW */
734 		return 0;
735 	}
736 }
737 
738 struct nfp_flower_pedit_acts {
739 	struct nfp_fl_set_ipv6_addr set_ip6_dst, set_ip6_src;
740 	struct nfp_fl_set_ipv6_tc_hl_fl set_ip6_tc_hl_fl;
741 	struct nfp_fl_set_ip4_ttl_tos set_ip_ttl_tos;
742 	struct nfp_fl_set_ip4_addrs set_ip_addr;
743 	struct nfp_fl_set_tport set_tport;
744 	struct nfp_fl_set_eth set_eth;
745 };
746 
747 static int
748 nfp_fl_commit_mangle(struct flow_cls_offload *flow, char *nfp_action,
749 		     int *a_len, struct nfp_flower_pedit_acts *set_act,
750 		     u32 *csum_updated)
751 {
752 	struct flow_rule *rule = flow_cls_offload_flow_rule(flow);
753 	size_t act_size = 0;
754 	u8 ip_proto = 0;
755 
756 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
757 		struct flow_match_basic match;
758 
759 		flow_rule_match_basic(rule, &match);
760 		ip_proto = match.key->ip_proto;
761 	}
762 
763 	if (set_act->set_eth.head.len_lw) {
764 		act_size = sizeof(set_act->set_eth);
765 		memcpy(nfp_action, &set_act->set_eth, act_size);
766 		*a_len += act_size;
767 	}
768 
769 	if (set_act->set_ip_ttl_tos.head.len_lw) {
770 		nfp_action += act_size;
771 		act_size = sizeof(set_act->set_ip_ttl_tos);
772 		memcpy(nfp_action, &set_act->set_ip_ttl_tos, act_size);
773 		*a_len += act_size;
774 
775 		/* Hardware will automatically fix IPv4 and TCP/UDP checksum. */
776 		*csum_updated |= TCA_CSUM_UPDATE_FLAG_IPV4HDR |
777 				nfp_fl_csum_l4_to_flag(ip_proto);
778 	}
779 
780 	if (set_act->set_ip_addr.head.len_lw) {
781 		nfp_action += act_size;
782 		act_size = sizeof(set_act->set_ip_addr);
783 		memcpy(nfp_action, &set_act->set_ip_addr, act_size);
784 		*a_len += act_size;
785 
786 		/* Hardware will automatically fix IPv4 and TCP/UDP checksum. */
787 		*csum_updated |= TCA_CSUM_UPDATE_FLAG_IPV4HDR |
788 				nfp_fl_csum_l4_to_flag(ip_proto);
789 	}
790 
791 	if (set_act->set_ip6_tc_hl_fl.head.len_lw) {
792 		nfp_action += act_size;
793 		act_size = sizeof(set_act->set_ip6_tc_hl_fl);
794 		memcpy(nfp_action, &set_act->set_ip6_tc_hl_fl, act_size);
795 		*a_len += act_size;
796 
797 		/* Hardware will automatically fix TCP/UDP checksum. */
798 		*csum_updated |= nfp_fl_csum_l4_to_flag(ip_proto);
799 	}
800 
801 	if (set_act->set_ip6_dst.head.len_lw &&
802 	    set_act->set_ip6_src.head.len_lw) {
803 		/* TC compiles set src and dst IPv6 address as a single action,
804 		 * the hardware requires this to be 2 separate actions.
805 		 */
806 		nfp_action += act_size;
807 		act_size = sizeof(set_act->set_ip6_src);
808 		memcpy(nfp_action, &set_act->set_ip6_src, act_size);
809 		*a_len += act_size;
810 
811 		act_size = sizeof(set_act->set_ip6_dst);
812 		memcpy(&nfp_action[sizeof(set_act->set_ip6_src)],
813 		       &set_act->set_ip6_dst, act_size);
814 		*a_len += act_size;
815 
816 		/* Hardware will automatically fix TCP/UDP checksum. */
817 		*csum_updated |= nfp_fl_csum_l4_to_flag(ip_proto);
818 	} else if (set_act->set_ip6_dst.head.len_lw) {
819 		nfp_action += act_size;
820 		act_size = sizeof(set_act->set_ip6_dst);
821 		memcpy(nfp_action, &set_act->set_ip6_dst, act_size);
822 		*a_len += act_size;
823 
824 		/* Hardware will automatically fix TCP/UDP checksum. */
825 		*csum_updated |= nfp_fl_csum_l4_to_flag(ip_proto);
826 	} else if (set_act->set_ip6_src.head.len_lw) {
827 		nfp_action += act_size;
828 		act_size = sizeof(set_act->set_ip6_src);
829 		memcpy(nfp_action, &set_act->set_ip6_src, act_size);
830 		*a_len += act_size;
831 
832 		/* Hardware will automatically fix TCP/UDP checksum. */
833 		*csum_updated |= nfp_fl_csum_l4_to_flag(ip_proto);
834 	}
835 	if (set_act->set_tport.head.len_lw) {
836 		nfp_action += act_size;
837 		act_size = sizeof(set_act->set_tport);
838 		memcpy(nfp_action, &set_act->set_tport, act_size);
839 		*a_len += act_size;
840 
841 		/* Hardware will automatically fix TCP/UDP checksum. */
842 		*csum_updated |= nfp_fl_csum_l4_to_flag(ip_proto);
843 	}
844 
845 	return 0;
846 }
847 
848 static int
849 nfp_fl_pedit(const struct flow_action_entry *act,
850 	     struct flow_cls_offload *flow, char *nfp_action, int *a_len,
851 	     u32 *csum_updated, struct nfp_flower_pedit_acts *set_act,
852 	     struct netlink_ext_ack *extack)
853 {
854 	enum flow_action_mangle_base htype;
855 	u32 offset;
856 
857 	htype = act->mangle.htype;
858 	offset = act->mangle.offset;
859 
860 	switch (htype) {
861 	case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
862 		return nfp_fl_set_eth(act, offset, &set_act->set_eth, extack);
863 	case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
864 		return nfp_fl_set_ip4(act, offset, &set_act->set_ip_addr,
865 				      &set_act->set_ip_ttl_tos, extack);
866 	case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
867 		return nfp_fl_set_ip6(act, offset, &set_act->set_ip6_dst,
868 				      &set_act->set_ip6_src,
869 				      &set_act->set_ip6_tc_hl_fl, extack);
870 	case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
871 		return nfp_fl_set_tport(act, offset, &set_act->set_tport,
872 					NFP_FL_ACTION_OPCODE_SET_TCP, extack);
873 	case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
874 		return nfp_fl_set_tport(act, offset, &set_act->set_tport,
875 					NFP_FL_ACTION_OPCODE_SET_UDP, extack);
876 	default:
877 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: pedit on unsupported header");
878 		return -EOPNOTSUPP;
879 	}
880 }
881 
882 static int
883 nfp_flower_output_action(struct nfp_app *app,
884 			 const struct flow_action_entry *act,
885 			 struct nfp_fl_payload *nfp_fl, int *a_len,
886 			 struct net_device *netdev, bool last,
887 			 enum nfp_flower_tun_type *tun_type, int *tun_out_cnt,
888 			 int *out_cnt, u32 *csum_updated,
889 			 struct netlink_ext_ack *extack)
890 {
891 	struct nfp_flower_priv *priv = app->priv;
892 	struct nfp_fl_output *output;
893 	int err, prelag_size;
894 
895 	/* If csum_updated has not been reset by now, it means HW will
896 	 * incorrectly update csums when they are not requested.
897 	 */
898 	if (*csum_updated) {
899 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: set actions without updating checksums are not supported");
900 		return -EOPNOTSUPP;
901 	}
902 
903 	if (*a_len + sizeof(struct nfp_fl_output) > NFP_FL_MAX_A_SIZ) {
904 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: mirred output increases action list size beyond the allowed maximum");
905 		return -EOPNOTSUPP;
906 	}
907 
908 	output = (struct nfp_fl_output *)&nfp_fl->action_data[*a_len];
909 	err = nfp_fl_output(app, output, act, nfp_fl, last, netdev, *tun_type,
910 			    tun_out_cnt, extack);
911 	if (err)
912 		return err;
913 
914 	*a_len += sizeof(struct nfp_fl_output);
915 
916 	if (priv->flower_ext_feats & NFP_FL_FEATS_LAG) {
917 		/* nfp_fl_pre_lag returns -err or size of prelag action added.
918 		 * This will be 0 if it is not egressing to a lag dev.
919 		 */
920 		prelag_size = nfp_fl_pre_lag(app, act, nfp_fl, *a_len, extack);
921 		if (prelag_size < 0) {
922 			return prelag_size;
923 		} else if (prelag_size > 0 && (!last || *out_cnt)) {
924 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: LAG action has to be last action in action list");
925 			return -EOPNOTSUPP;
926 		}
927 
928 		*a_len += prelag_size;
929 	}
930 	(*out_cnt)++;
931 
932 	return 0;
933 }
934 
935 static int
936 nfp_flower_loop_action(struct nfp_app *app, const struct flow_action_entry *act,
937 		       struct flow_cls_offload *flow,
938 		       struct nfp_fl_payload *nfp_fl, int *a_len,
939 		       struct net_device *netdev,
940 		       enum nfp_flower_tun_type *tun_type, int *tun_out_cnt,
941 		       int *out_cnt, u32 *csum_updated,
942 		       struct nfp_flower_pedit_acts *set_act,
943 		       struct netlink_ext_ack *extack, int act_idx)
944 {
945 	struct nfp_fl_set_ipv4_tun *set_tun;
946 	struct nfp_fl_pre_tunnel *pre_tun;
947 	struct nfp_fl_push_vlan *psh_v;
948 	struct nfp_fl_push_mpls *psh_m;
949 	struct nfp_fl_pop_vlan *pop_v;
950 	struct nfp_fl_pop_mpls *pop_m;
951 	struct nfp_fl_set_mpls *set_m;
952 	int err;
953 
954 	switch (act->id) {
955 	case FLOW_ACTION_DROP:
956 		nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_DROP);
957 		break;
958 	case FLOW_ACTION_REDIRECT:
959 		err = nfp_flower_output_action(app, act, nfp_fl, a_len, netdev,
960 					       true, tun_type, tun_out_cnt,
961 					       out_cnt, csum_updated, extack);
962 		if (err)
963 			return err;
964 		break;
965 	case FLOW_ACTION_MIRRED:
966 		err = nfp_flower_output_action(app, act, nfp_fl, a_len, netdev,
967 					       false, tun_type, tun_out_cnt,
968 					       out_cnt, csum_updated, extack);
969 		if (err)
970 			return err;
971 		break;
972 	case FLOW_ACTION_VLAN_POP:
973 		if (*a_len +
974 		    sizeof(struct nfp_fl_pop_vlan) > NFP_FL_MAX_A_SIZ) {
975 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: maximum allowed action list size exceeded at pop vlan");
976 			return -EOPNOTSUPP;
977 		}
978 
979 		pop_v = (struct nfp_fl_pop_vlan *)&nfp_fl->action_data[*a_len];
980 		nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_POPV);
981 
982 		nfp_fl_pop_vlan(pop_v);
983 		*a_len += sizeof(struct nfp_fl_pop_vlan);
984 		break;
985 	case FLOW_ACTION_VLAN_PUSH:
986 		if (*a_len +
987 		    sizeof(struct nfp_fl_push_vlan) > NFP_FL_MAX_A_SIZ) {
988 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: maximum allowed action list size exceeded at push vlan");
989 			return -EOPNOTSUPP;
990 		}
991 
992 		psh_v = (struct nfp_fl_push_vlan *)&nfp_fl->action_data[*a_len];
993 		nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
994 
995 		nfp_fl_push_vlan(psh_v, act);
996 		*a_len += sizeof(struct nfp_fl_push_vlan);
997 		break;
998 	case FLOW_ACTION_TUNNEL_ENCAP: {
999 		const struct ip_tunnel_info *ip_tun = act->tunnel;
1000 
1001 		*tun_type = nfp_fl_get_tun_from_act(app, flow, act, act_idx);
1002 		if (*tun_type == NFP_FL_TUNNEL_NONE) {
1003 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: unsupported tunnel type in action list");
1004 			return -EOPNOTSUPP;
1005 		}
1006 
1007 		if (ip_tun->mode & ~NFP_FL_SUPPORTED_TUNNEL_INFO_FLAGS) {
1008 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: unsupported tunnel flags in action list");
1009 			return -EOPNOTSUPP;
1010 		}
1011 
1012 		/* Pre-tunnel action is required for tunnel encap.
1013 		 * This checks for next hop entries on NFP.
1014 		 * If none, the packet falls back before applying other actions.
1015 		 */
1016 		if (*a_len + sizeof(struct nfp_fl_pre_tunnel) +
1017 		    sizeof(struct nfp_fl_set_ipv4_tun) > NFP_FL_MAX_A_SIZ) {
1018 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: maximum allowed action list size exceeded at tunnel encap");
1019 			return -EOPNOTSUPP;
1020 		}
1021 
1022 		pre_tun = nfp_fl_pre_tunnel(nfp_fl->action_data, *a_len);
1023 		nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
1024 		*a_len += sizeof(struct nfp_fl_pre_tunnel);
1025 
1026 		err = nfp_fl_push_geneve_options(nfp_fl, a_len, act, extack);
1027 		if (err)
1028 			return err;
1029 
1030 		set_tun = (void *)&nfp_fl->action_data[*a_len];
1031 		err = nfp_fl_set_ipv4_tun(app, set_tun, act, pre_tun,
1032 					  *tun_type, netdev, extack);
1033 		if (err)
1034 			return err;
1035 		*a_len += sizeof(struct nfp_fl_set_ipv4_tun);
1036 		}
1037 		break;
1038 	case FLOW_ACTION_TUNNEL_DECAP:
1039 		/* Tunnel decap is handled by default so accept action. */
1040 		return 0;
1041 	case FLOW_ACTION_MANGLE:
1042 		if (nfp_fl_pedit(act, flow, &nfp_fl->action_data[*a_len],
1043 				 a_len, csum_updated, set_act, extack))
1044 			return -EOPNOTSUPP;
1045 		break;
1046 	case FLOW_ACTION_CSUM:
1047 		/* csum action requests recalc of something we have not fixed */
1048 		if (act->csum_flags & ~*csum_updated) {
1049 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: unsupported csum update action in action list");
1050 			return -EOPNOTSUPP;
1051 		}
1052 		/* If we will correctly fix the csum we can remove it from the
1053 		 * csum update list. Which will later be used to check support.
1054 		 */
1055 		*csum_updated &= ~act->csum_flags;
1056 		break;
1057 	case FLOW_ACTION_MPLS_PUSH:
1058 		if (*a_len +
1059 		    sizeof(struct nfp_fl_push_mpls) > NFP_FL_MAX_A_SIZ) {
1060 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: maximum allowed action list size exceeded at push MPLS");
1061 			return -EOPNOTSUPP;
1062 		}
1063 
1064 		psh_m = (struct nfp_fl_push_mpls *)&nfp_fl->action_data[*a_len];
1065 		nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
1066 
1067 		err = nfp_fl_push_mpls(psh_m, act, extack);
1068 		if (err)
1069 			return err;
1070 		*a_len += sizeof(struct nfp_fl_push_mpls);
1071 		break;
1072 	case FLOW_ACTION_MPLS_POP:
1073 		if (*a_len +
1074 		    sizeof(struct nfp_fl_pop_mpls) > NFP_FL_MAX_A_SIZ) {
1075 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: maximum allowed action list size exceeded at pop MPLS");
1076 			return -EOPNOTSUPP;
1077 		}
1078 
1079 		pop_m = (struct nfp_fl_pop_mpls *)&nfp_fl->action_data[*a_len];
1080 		nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
1081 
1082 		nfp_fl_pop_mpls(pop_m, act);
1083 		*a_len += sizeof(struct nfp_fl_pop_mpls);
1084 		break;
1085 	case FLOW_ACTION_MPLS_MANGLE:
1086 		if (*a_len +
1087 		    sizeof(struct nfp_fl_set_mpls) > NFP_FL_MAX_A_SIZ) {
1088 			NL_SET_ERR_MSG_MOD(extack, "unsupported offload: maximum allowed action list size exceeded at set MPLS");
1089 			return -EOPNOTSUPP;
1090 		}
1091 
1092 		set_m = (struct nfp_fl_set_mpls *)&nfp_fl->action_data[*a_len];
1093 		nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
1094 
1095 		nfp_fl_set_mpls(set_m, act);
1096 		*a_len += sizeof(struct nfp_fl_set_mpls);
1097 		break;
1098 	default:
1099 		/* Currently we do not handle any other actions. */
1100 		NL_SET_ERR_MSG_MOD(extack, "unsupported offload: unsupported action in action list");
1101 		return -EOPNOTSUPP;
1102 	}
1103 
1104 	return 0;
1105 }
1106 
1107 static bool nfp_fl_check_mangle_start(struct flow_action *flow_act,
1108 				      int current_act_idx)
1109 {
1110 	struct flow_action_entry current_act;
1111 	struct flow_action_entry prev_act;
1112 
1113 	current_act = flow_act->entries[current_act_idx];
1114 	if (current_act.id != FLOW_ACTION_MANGLE)
1115 		return false;
1116 
1117 	if (current_act_idx == 0)
1118 		return true;
1119 
1120 	prev_act = flow_act->entries[current_act_idx - 1];
1121 
1122 	return prev_act.id != FLOW_ACTION_MANGLE;
1123 }
1124 
1125 static bool nfp_fl_check_mangle_end(struct flow_action *flow_act,
1126 				    int current_act_idx)
1127 {
1128 	struct flow_action_entry current_act;
1129 	struct flow_action_entry next_act;
1130 
1131 	current_act = flow_act->entries[current_act_idx];
1132 	if (current_act.id != FLOW_ACTION_MANGLE)
1133 		return false;
1134 
1135 	if (current_act_idx == flow_act->num_entries)
1136 		return true;
1137 
1138 	next_act = flow_act->entries[current_act_idx + 1];
1139 
1140 	return next_act.id != FLOW_ACTION_MANGLE;
1141 }
1142 
1143 int nfp_flower_compile_action(struct nfp_app *app,
1144 			      struct flow_cls_offload *flow,
1145 			      struct net_device *netdev,
1146 			      struct nfp_fl_payload *nfp_flow,
1147 			      struct netlink_ext_ack *extack)
1148 {
1149 	int act_len, act_cnt, err, tun_out_cnt, out_cnt, i;
1150 	struct nfp_flower_pedit_acts set_act;
1151 	enum nfp_flower_tun_type tun_type;
1152 	struct flow_action_entry *act;
1153 	u32 csum_updated = 0;
1154 
1155 	memset(nfp_flow->action_data, 0, NFP_FL_MAX_A_SIZ);
1156 	nfp_flow->meta.act_len = 0;
1157 	tun_type = NFP_FL_TUNNEL_NONE;
1158 	act_len = 0;
1159 	act_cnt = 0;
1160 	tun_out_cnt = 0;
1161 	out_cnt = 0;
1162 
1163 	flow_action_for_each(i, act, &flow->rule->action) {
1164 		if (nfp_fl_check_mangle_start(&flow->rule->action, i))
1165 			memset(&set_act, 0, sizeof(set_act));
1166 		err = nfp_flower_loop_action(app, act, flow, nfp_flow, &act_len,
1167 					     netdev, &tun_type, &tun_out_cnt,
1168 					     &out_cnt, &csum_updated,
1169 					     &set_act, extack, i);
1170 		if (err)
1171 			return err;
1172 		act_cnt++;
1173 		if (nfp_fl_check_mangle_end(&flow->rule->action, i))
1174 			nfp_fl_commit_mangle(flow,
1175 					     &nfp_flow->action_data[act_len],
1176 					     &act_len, &set_act, &csum_updated);
1177 	}
1178 
1179 	/* We optimise when the action list is small, this can unfortunately
1180 	 * not happen once we have more than one action in the action list.
1181 	 */
1182 	if (act_cnt > 1)
1183 		nfp_flow->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
1184 
1185 	nfp_flow->meta.act_len = act_len;
1186 
1187 	return 0;
1188 }
1189