1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2017-2018 Netronome Systems, Inc. */
4 #include <linux/bitfield.h>
5 #include <net/geneve.h>
6 #include <net/pkt_cls.h>
7 #include <net/switchdev.h>
8 #include <net/tc_act/tc_csum.h>
9 #include <net/tc_act/tc_gact.h>
10 #include <net/tc_act/tc_mirred.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>
17 #include "../nfp_net_repr.h"
19 /* The kernel versions of TUNNEL_* are not ABI and therefore vulnerable
20 * to change. Such changes will break our FW ABI.
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 | \
28 NFP_FL_TUNNEL_GENEVE_OPT)
30 static void nfp_fl_pop_vlan(struct nfp_fl_pop_vlan *pop_vlan)
32 size_t act_size = sizeof(struct nfp_fl_pop_vlan);
34 pop_vlan->head.jump_id = NFP_FL_ACTION_OPCODE_POP_VLAN;
35 pop_vlan->head.len_lw = act_size >> NFP_FL_LW_SIZ;
36 pop_vlan->reserved = 0;
40 nfp_fl_push_vlan(struct nfp_fl_push_vlan *push_vlan,
41 const struct tc_action *action)
43 size_t act_size = sizeof(struct nfp_fl_push_vlan);
44 u16 tmp_push_vlan_tci;
46 push_vlan->head.jump_id = NFP_FL_ACTION_OPCODE_PUSH_VLAN;
47 push_vlan->head.len_lw = act_size >> NFP_FL_LW_SIZ;
48 push_vlan->reserved = 0;
49 push_vlan->vlan_tpid = tcf_vlan_push_proto(action);
52 FIELD_PREP(NFP_FL_PUSH_VLAN_PRIO, tcf_vlan_push_prio(action)) |
53 FIELD_PREP(NFP_FL_PUSH_VLAN_VID, tcf_vlan_push_vid(action)) |
55 push_vlan->vlan_tci = cpu_to_be16(tmp_push_vlan_tci);
59 nfp_fl_pre_lag(struct nfp_app *app, const struct tc_action *action,
60 struct nfp_fl_payload *nfp_flow, int act_len)
62 size_t act_size = sizeof(struct nfp_fl_pre_lag);
63 struct nfp_fl_pre_lag *pre_lag;
64 struct net_device *out_dev;
67 out_dev = tcf_mirred_dev(action);
68 if (!out_dev || !netif_is_lag_master(out_dev))
71 if (act_len + act_size > NFP_FL_MAX_A_SIZ)
74 /* Pre_lag action must be first on action list.
75 * If other actions already exist they need pushed forward.
78 memmove(nfp_flow->action_data + act_size,
79 nfp_flow->action_data, act_len);
81 pre_lag = (struct nfp_fl_pre_lag *)nfp_flow->action_data;
82 err = nfp_flower_lag_populate_pre_action(app, out_dev, pre_lag);
86 pre_lag->head.jump_id = NFP_FL_ACTION_OPCODE_PRE_LAG;
87 pre_lag->head.len_lw = act_size >> NFP_FL_LW_SIZ;
89 nfp_flow->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
94 static bool nfp_fl_netdev_is_tunnel_type(struct net_device *out_dev,
95 enum nfp_flower_tun_type tun_type)
97 if (!out_dev->rtnl_link_ops)
100 if (!strcmp(out_dev->rtnl_link_ops->kind, "vxlan"))
101 return tun_type == NFP_FL_TUNNEL_VXLAN;
103 if (!strcmp(out_dev->rtnl_link_ops->kind, "geneve"))
104 return tun_type == NFP_FL_TUNNEL_GENEVE;
110 nfp_fl_output(struct nfp_app *app, struct nfp_fl_output *output,
111 const struct tc_action *action, struct nfp_fl_payload *nfp_flow,
112 bool last, struct net_device *in_dev,
113 enum nfp_flower_tun_type tun_type, int *tun_out_cnt)
115 size_t act_size = sizeof(struct nfp_fl_output);
116 struct nfp_flower_priv *priv = app->priv;
117 struct net_device *out_dev;
120 output->head.jump_id = NFP_FL_ACTION_OPCODE_OUTPUT;
121 output->head.len_lw = act_size >> NFP_FL_LW_SIZ;
123 out_dev = tcf_mirred_dev(action);
127 tmp_flags = last ? NFP_FL_OUT_FLAGS_LAST : 0;
130 /* Verify the egress netdev matches the tunnel type. */
131 if (!nfp_fl_netdev_is_tunnel_type(out_dev, tun_type))
138 output->flags = cpu_to_be16(tmp_flags |
139 NFP_FL_OUT_FLAGS_USE_TUN);
140 output->port = cpu_to_be32(NFP_FL_PORT_TYPE_TUN | tun_type);
141 } else if (netif_is_lag_master(out_dev) &&
142 priv->flower_ext_feats & NFP_FL_FEATS_LAG) {
145 output->flags = cpu_to_be16(tmp_flags);
146 gid = nfp_flower_lag_get_output_id(app, out_dev);
149 output->port = cpu_to_be32(NFP_FL_LAG_OUT | gid);
151 /* Set action output parameters. */
152 output->flags = cpu_to_be16(tmp_flags);
154 /* Only offload if egress ports are on the same device as the
157 if (!switchdev_port_same_parent_id(in_dev, out_dev))
159 if (!nfp_netdev_is_nfp_repr(out_dev))
162 output->port = cpu_to_be32(nfp_repr_get_port_id(out_dev));
166 nfp_flow->meta.shortcut = output->port;
171 static enum nfp_flower_tun_type
172 nfp_fl_get_tun_from_act_l4_port(struct nfp_app *app,
173 const struct tc_action *action)
175 struct ip_tunnel_info *tun = tcf_tunnel_info(action);
176 struct nfp_flower_priv *priv = app->priv;
178 switch (tun->key.tp_dst) {
179 case htons(NFP_FL_VXLAN_PORT):
180 return NFP_FL_TUNNEL_VXLAN;
181 case htons(NFP_FL_GENEVE_PORT):
182 if (priv->flower_ext_feats & NFP_FL_FEATS_GENEVE)
183 return NFP_FL_TUNNEL_GENEVE;
186 return NFP_FL_TUNNEL_NONE;
190 static struct nfp_fl_pre_tunnel *nfp_fl_pre_tunnel(char *act_data, int act_len)
192 size_t act_size = sizeof(struct nfp_fl_pre_tunnel);
193 struct nfp_fl_pre_tunnel *pre_tun_act;
195 /* Pre_tunnel action must be first on action list.
196 * If other actions already exist they need to be pushed forward.
199 memmove(act_data + act_size, act_data, act_len);
201 pre_tun_act = (struct nfp_fl_pre_tunnel *)act_data;
203 memset(pre_tun_act, 0, act_size);
205 pre_tun_act->head.jump_id = NFP_FL_ACTION_OPCODE_PRE_TUNNEL;
206 pre_tun_act->head.len_lw = act_size >> NFP_FL_LW_SIZ;
212 nfp_fl_push_geneve_options(struct nfp_fl_payload *nfp_fl, int *list_len,
213 const struct tc_action *action)
215 struct ip_tunnel_info *ip_tun = tcf_tunnel_info(action);
216 int opt_len, opt_cnt, act_start, tot_push_len;
217 u8 *src = ip_tunnel_info_opts(ip_tun);
219 /* We need to populate the options in reverse order for HW.
220 * Therefore we go through the options, calculating the
221 * number of options and the total size, then we populate
222 * them in reverse order in the action list.
226 opt_len = ip_tun->options_len;
227 while (opt_len > 0) {
228 struct geneve_opt *opt = (struct geneve_opt *)src;
231 if (opt_cnt > NFP_FL_MAX_GENEVE_OPT_CNT)
234 tot_push_len += sizeof(struct nfp_fl_push_geneve) +
236 if (tot_push_len > NFP_FL_MAX_GENEVE_OPT_ACT)
239 opt_len -= sizeof(struct geneve_opt) + opt->length * 4;
240 src += sizeof(struct geneve_opt) + opt->length * 4;
243 if (*list_len + tot_push_len > NFP_FL_MAX_A_SIZ)
246 act_start = *list_len;
247 *list_len += tot_push_len;
248 src = ip_tunnel_info_opts(ip_tun);
250 struct geneve_opt *opt = (struct geneve_opt *)src;
251 struct nfp_fl_push_geneve *push;
252 size_t act_size, len;
255 act_size = sizeof(struct nfp_fl_push_geneve) + opt->length * 4;
256 tot_push_len -= act_size;
257 len = act_start + tot_push_len;
259 push = (struct nfp_fl_push_geneve *)&nfp_fl->action_data[len];
260 push->head.jump_id = NFP_FL_ACTION_OPCODE_PUSH_GENEVE;
261 push->head.len_lw = act_size >> NFP_FL_LW_SIZ;
263 push->class = opt->opt_class;
264 push->type = opt->type;
265 push->length = opt->length;
266 memcpy(&push->opt_data, opt->opt_data, opt->length * 4);
268 src += sizeof(struct geneve_opt) + opt->length * 4;
275 nfp_fl_set_ipv4_udp_tun(struct nfp_app *app,
276 struct nfp_fl_set_ipv4_udp_tun *set_tun,
277 const struct tc_action *action,
278 struct nfp_fl_pre_tunnel *pre_tun,
279 enum nfp_flower_tun_type tun_type,
280 struct net_device *netdev)
282 size_t act_size = sizeof(struct nfp_fl_set_ipv4_udp_tun);
283 struct ip_tunnel_info *ip_tun = tcf_tunnel_info(action);
284 struct nfp_flower_priv *priv = app->priv;
285 u32 tmp_set_ip_tun_type_index = 0;
286 /* Currently support one pre-tunnel so index is always 0. */
289 BUILD_BUG_ON(NFP_FL_TUNNEL_CSUM != TUNNEL_CSUM ||
290 NFP_FL_TUNNEL_KEY != TUNNEL_KEY ||
291 NFP_FL_TUNNEL_GENEVE_OPT != TUNNEL_GENEVE_OPT);
292 if (ip_tun->options_len &&
293 (tun_type != NFP_FL_TUNNEL_GENEVE ||
294 !(priv->flower_ext_feats & NFP_FL_FEATS_GENEVE_OPT)))
297 set_tun->head.jump_id = NFP_FL_ACTION_OPCODE_SET_IPV4_TUNNEL;
298 set_tun->head.len_lw = act_size >> NFP_FL_LW_SIZ;
300 /* Set tunnel type and pre-tunnel index. */
301 tmp_set_ip_tun_type_index |=
302 FIELD_PREP(NFP_FL_IPV4_TUNNEL_TYPE, tun_type) |
303 FIELD_PREP(NFP_FL_IPV4_PRE_TUN_INDEX, pretun_idx);
305 set_tun->tun_type_index = cpu_to_be32(tmp_set_ip_tun_type_index);
306 set_tun->tun_id = ip_tun->key.tun_id;
308 if (ip_tun->key.ttl) {
309 set_tun->ttl = ip_tun->key.ttl;
311 struct net *net = dev_net(netdev);
312 struct flowi4 flow = {};
316 /* Do a route lookup to determine ttl - if fails then use
317 * default. Note that CONFIG_INET is a requirement of
318 * CONFIG_NET_SWITCHDEV so must be defined here.
320 flow.daddr = ip_tun->key.u.ipv4.dst;
321 flow.flowi4_proto = IPPROTO_UDP;
322 rt = ip_route_output_key(net, &flow);
323 err = PTR_ERR_OR_ZERO(rt);
325 set_tun->ttl = ip4_dst_hoplimit(&rt->dst);
328 set_tun->ttl = net->ipv4.sysctl_ip_default_ttl;
332 set_tun->tos = ip_tun->key.tos;
334 if (!(ip_tun->key.tun_flags & NFP_FL_TUNNEL_KEY) ||
335 ip_tun->key.tun_flags & ~NFP_FL_SUPPORTED_IPV4_UDP_TUN_FLAGS)
337 set_tun->tun_flags = ip_tun->key.tun_flags;
339 if (tun_type == NFP_FL_TUNNEL_GENEVE) {
340 set_tun->tun_proto = htons(ETH_P_TEB);
341 set_tun->tun_len = ip_tun->options_len / 4;
344 /* Complete pre_tunnel action. */
345 pre_tun->ipv4_dst = ip_tun->key.u.ipv4.dst;
350 static void nfp_fl_set_helper32(u32 value, u32 mask, u8 *p_exact, u8 *p_mask)
352 u32 oldvalue = get_unaligned((u32 *)p_exact);
353 u32 oldmask = get_unaligned((u32 *)p_mask);
356 value |= oldvalue & ~mask;
358 put_unaligned(oldmask | mask, (u32 *)p_mask);
359 put_unaligned(value, (u32 *)p_exact);
363 nfp_fl_set_eth(const struct tc_action *action, int idx, u32 off,
364 struct nfp_fl_set_eth *set_eth)
368 if (off + 4 > ETH_ALEN * 2)
371 mask = ~tcf_pedit_mask(action, idx);
372 exact = tcf_pedit_val(action, idx);
377 nfp_fl_set_helper32(exact, mask, &set_eth->eth_addr_val[off],
378 &set_eth->eth_addr_mask[off]);
380 set_eth->reserved = cpu_to_be16(0);
381 set_eth->head.jump_id = NFP_FL_ACTION_OPCODE_SET_ETHERNET;
382 set_eth->head.len_lw = sizeof(*set_eth) >> NFP_FL_LW_SIZ;
387 struct ipv4_ttl_word {
394 nfp_fl_set_ip4(const struct tc_action *action, int idx, u32 off,
395 struct nfp_fl_set_ip4_addrs *set_ip_addr,
396 struct nfp_fl_set_ip4_ttl_tos *set_ip_ttl_tos)
398 struct ipv4_ttl_word *ttl_word_mask;
399 struct ipv4_ttl_word *ttl_word;
400 struct iphdr *tos_word_mask;
401 struct iphdr *tos_word;
404 /* We are expecting tcf_pedit to return a big endian value */
405 mask = (__force __be32)~tcf_pedit_mask(action, idx);
406 exact = (__force __be32)tcf_pedit_val(action, idx);
412 case offsetof(struct iphdr, daddr):
413 set_ip_addr->ipv4_dst_mask |= mask;
414 set_ip_addr->ipv4_dst &= ~mask;
415 set_ip_addr->ipv4_dst |= exact & mask;
416 set_ip_addr->head.jump_id = NFP_FL_ACTION_OPCODE_SET_IPV4_ADDRS;
417 set_ip_addr->head.len_lw = sizeof(*set_ip_addr) >>
420 case offsetof(struct iphdr, saddr):
421 set_ip_addr->ipv4_src_mask |= mask;
422 set_ip_addr->ipv4_src &= ~mask;
423 set_ip_addr->ipv4_src |= exact & mask;
424 set_ip_addr->head.jump_id = NFP_FL_ACTION_OPCODE_SET_IPV4_ADDRS;
425 set_ip_addr->head.len_lw = sizeof(*set_ip_addr) >>
428 case offsetof(struct iphdr, ttl):
429 ttl_word_mask = (struct ipv4_ttl_word *)&mask;
430 ttl_word = (struct ipv4_ttl_word *)&exact;
432 if (ttl_word_mask->protocol || ttl_word_mask->check)
435 set_ip_ttl_tos->ipv4_ttl_mask |= ttl_word_mask->ttl;
436 set_ip_ttl_tos->ipv4_ttl &= ~ttl_word_mask->ttl;
437 set_ip_ttl_tos->ipv4_ttl |= ttl_word->ttl & ttl_word_mask->ttl;
438 set_ip_ttl_tos->head.jump_id =
439 NFP_FL_ACTION_OPCODE_SET_IPV4_TTL_TOS;
440 set_ip_ttl_tos->head.len_lw = sizeof(*set_ip_ttl_tos) >>
443 case round_down(offsetof(struct iphdr, tos), 4):
444 tos_word_mask = (struct iphdr *)&mask;
445 tos_word = (struct iphdr *)&exact;
447 if (tos_word_mask->version || tos_word_mask->ihl ||
448 tos_word_mask->tot_len)
451 set_ip_ttl_tos->ipv4_tos_mask |= tos_word_mask->tos;
452 set_ip_ttl_tos->ipv4_tos &= ~tos_word_mask->tos;
453 set_ip_ttl_tos->ipv4_tos |= tos_word->tos & tos_word_mask->tos;
454 set_ip_ttl_tos->head.jump_id =
455 NFP_FL_ACTION_OPCODE_SET_IPV4_TTL_TOS;
456 set_ip_ttl_tos->head.len_lw = sizeof(*set_ip_ttl_tos) >>
467 nfp_fl_set_ip6_helper(int opcode_tag, u8 word, __be32 exact, __be32 mask,
468 struct nfp_fl_set_ipv6_addr *ip6)
470 ip6->ipv6[word].mask |= mask;
471 ip6->ipv6[word].exact &= ~mask;
472 ip6->ipv6[word].exact |= exact & mask;
474 ip6->reserved = cpu_to_be16(0);
475 ip6->head.jump_id = opcode_tag;
476 ip6->head.len_lw = sizeof(*ip6) >> NFP_FL_LW_SIZ;
480 nfp_fl_set_ip6(const struct tc_action *action, int idx, u32 off,
481 struct nfp_fl_set_ipv6_addr *ip_dst,
482 struct nfp_fl_set_ipv6_addr *ip_src)
487 /* We are expecting tcf_pedit to return a big endian value */
488 mask = (__force __be32)~tcf_pedit_mask(action, idx);
489 exact = (__force __be32)tcf_pedit_val(action, idx);
494 if (off < offsetof(struct ipv6hdr, saddr)) {
496 } else if (off < offsetof(struct ipv6hdr, daddr)) {
497 word = (off - offsetof(struct ipv6hdr, saddr)) / sizeof(exact);
498 nfp_fl_set_ip6_helper(NFP_FL_ACTION_OPCODE_SET_IPV6_SRC, word,
499 exact, mask, ip_src);
500 } else if (off < offsetof(struct ipv6hdr, daddr) +
501 sizeof(struct in6_addr)) {
502 word = (off - offsetof(struct ipv6hdr, daddr)) / sizeof(exact);
503 nfp_fl_set_ip6_helper(NFP_FL_ACTION_OPCODE_SET_IPV6_DST, word,
504 exact, mask, ip_dst);
513 nfp_fl_set_tport(const struct tc_action *action, int idx, u32 off,
514 struct nfp_fl_set_tport *set_tport, int opcode)
521 mask = ~tcf_pedit_mask(action, idx);
522 exact = tcf_pedit_val(action, idx);
527 nfp_fl_set_helper32(exact, mask, set_tport->tp_port_val,
528 set_tport->tp_port_mask);
530 set_tport->reserved = cpu_to_be16(0);
531 set_tport->head.jump_id = opcode;
532 set_tport->head.len_lw = sizeof(*set_tport) >> NFP_FL_LW_SIZ;
537 static u32 nfp_fl_csum_l4_to_flag(u8 ip_proto)
541 /* Filter doesn't force proto match,
542 * both TCP and UDP will be updated if encountered
544 return TCA_CSUM_UPDATE_FLAG_TCP | TCA_CSUM_UPDATE_FLAG_UDP;
546 return TCA_CSUM_UPDATE_FLAG_TCP;
548 return TCA_CSUM_UPDATE_FLAG_UDP;
550 /* All other protocols will be ignored by FW */
556 nfp_fl_pedit(const struct tc_action *action, struct tc_cls_flower_offload *flow,
557 char *nfp_action, int *a_len, u32 *csum_updated)
559 struct nfp_fl_set_ipv6_addr set_ip6_dst, set_ip6_src;
560 struct nfp_fl_set_ip4_ttl_tos set_ip_ttl_tos;
561 struct nfp_fl_set_ip4_addrs set_ip_addr;
562 struct nfp_fl_set_tport set_tport;
563 struct nfp_fl_set_eth set_eth;
564 enum pedit_header_type htype;
570 memset(&set_ip_ttl_tos, 0, sizeof(set_ip_ttl_tos));
571 memset(&set_ip6_dst, 0, sizeof(set_ip6_dst));
572 memset(&set_ip6_src, 0, sizeof(set_ip6_src));
573 memset(&set_ip_addr, 0, sizeof(set_ip_addr));
574 memset(&set_tport, 0, sizeof(set_tport));
575 memset(&set_eth, 0, sizeof(set_eth));
576 nkeys = tcf_pedit_nkeys(action);
578 for (idx = 0; idx < nkeys; idx++) {
579 cmd = tcf_pedit_cmd(action, idx);
580 htype = tcf_pedit_htype(action, idx);
581 offset = tcf_pedit_offset(action, idx);
583 if (cmd != TCA_PEDIT_KEY_EX_CMD_SET)
587 case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
588 err = nfp_fl_set_eth(action, idx, offset, &set_eth);
590 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
591 err = nfp_fl_set_ip4(action, idx, offset, &set_ip_addr,
594 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
595 err = nfp_fl_set_ip6(action, idx, offset, &set_ip6_dst,
598 case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
599 err = nfp_fl_set_tport(action, idx, offset, &set_tport,
600 NFP_FL_ACTION_OPCODE_SET_TCP);
602 case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
603 err = nfp_fl_set_tport(action, idx, offset, &set_tport,
604 NFP_FL_ACTION_OPCODE_SET_UDP);
613 if (dissector_uses_key(flow->dissector, FLOW_DISSECTOR_KEY_BASIC)) {
614 struct flow_dissector_key_basic *basic;
616 basic = skb_flow_dissector_target(flow->dissector,
617 FLOW_DISSECTOR_KEY_BASIC,
619 ip_proto = basic->ip_proto;
622 if (set_eth.head.len_lw) {
623 act_size = sizeof(set_eth);
624 memcpy(nfp_action, &set_eth, act_size);
627 if (set_ip_ttl_tos.head.len_lw) {
628 nfp_action += act_size;
629 act_size = sizeof(set_ip_ttl_tos);
630 memcpy(nfp_action, &set_ip_ttl_tos, act_size);
633 /* Hardware will automatically fix IPv4 and TCP/UDP checksum. */
634 *csum_updated |= TCA_CSUM_UPDATE_FLAG_IPV4HDR |
635 nfp_fl_csum_l4_to_flag(ip_proto);
637 if (set_ip_addr.head.len_lw) {
638 nfp_action += act_size;
639 act_size = sizeof(set_ip_addr);
640 memcpy(nfp_action, &set_ip_addr, act_size);
643 /* Hardware will automatically fix IPv4 and TCP/UDP checksum. */
644 *csum_updated |= TCA_CSUM_UPDATE_FLAG_IPV4HDR |
645 nfp_fl_csum_l4_to_flag(ip_proto);
647 if (set_ip6_dst.head.len_lw && set_ip6_src.head.len_lw) {
648 /* TC compiles set src and dst IPv6 address as a single action,
649 * the hardware requires this to be 2 separate actions.
651 nfp_action += act_size;
652 act_size = sizeof(set_ip6_src);
653 memcpy(nfp_action, &set_ip6_src, act_size);
656 act_size = sizeof(set_ip6_dst);
657 memcpy(&nfp_action[sizeof(set_ip6_src)], &set_ip6_dst,
661 /* Hardware will automatically fix TCP/UDP checksum. */
662 *csum_updated |= nfp_fl_csum_l4_to_flag(ip_proto);
663 } else if (set_ip6_dst.head.len_lw) {
664 nfp_action += act_size;
665 act_size = sizeof(set_ip6_dst);
666 memcpy(nfp_action, &set_ip6_dst, act_size);
669 /* Hardware will automatically fix TCP/UDP checksum. */
670 *csum_updated |= nfp_fl_csum_l4_to_flag(ip_proto);
671 } else if (set_ip6_src.head.len_lw) {
672 nfp_action += act_size;
673 act_size = sizeof(set_ip6_src);
674 memcpy(nfp_action, &set_ip6_src, act_size);
677 /* Hardware will automatically fix TCP/UDP checksum. */
678 *csum_updated |= nfp_fl_csum_l4_to_flag(ip_proto);
680 if (set_tport.head.len_lw) {
681 nfp_action += act_size;
682 act_size = sizeof(set_tport);
683 memcpy(nfp_action, &set_tport, act_size);
686 /* Hardware will automatically fix TCP/UDP checksum. */
687 *csum_updated |= nfp_fl_csum_l4_to_flag(ip_proto);
694 nfp_flower_output_action(struct nfp_app *app, const struct tc_action *a,
695 struct nfp_fl_payload *nfp_fl, int *a_len,
696 struct net_device *netdev, bool last,
697 enum nfp_flower_tun_type *tun_type, int *tun_out_cnt,
698 int *out_cnt, u32 *csum_updated)
700 struct nfp_flower_priv *priv = app->priv;
701 struct nfp_fl_output *output;
702 int err, prelag_size;
704 /* If csum_updated has not been reset by now, it means HW will
705 * incorrectly update csums when they are not requested.
710 if (*a_len + sizeof(struct nfp_fl_output) > NFP_FL_MAX_A_SIZ)
713 output = (struct nfp_fl_output *)&nfp_fl->action_data[*a_len];
714 err = nfp_fl_output(app, output, a, nfp_fl, last, netdev, *tun_type,
719 *a_len += sizeof(struct nfp_fl_output);
721 if (priv->flower_ext_feats & NFP_FL_FEATS_LAG) {
722 /* nfp_fl_pre_lag returns -err or size of prelag action added.
723 * This will be 0 if it is not egressing to a lag dev.
725 prelag_size = nfp_fl_pre_lag(app, a, nfp_fl, *a_len);
728 else if (prelag_size > 0 && (!last || *out_cnt))
731 *a_len += prelag_size;
739 nfp_flower_loop_action(struct nfp_app *app, const struct tc_action *a,
740 struct tc_cls_flower_offload *flow,
741 struct nfp_fl_payload *nfp_fl, int *a_len,
742 struct net_device *netdev,
743 enum nfp_flower_tun_type *tun_type, int *tun_out_cnt,
744 int *out_cnt, u32 *csum_updated)
746 struct nfp_fl_set_ipv4_udp_tun *set_tun;
747 struct nfp_fl_pre_tunnel *pre_tun;
748 struct nfp_fl_push_vlan *psh_v;
749 struct nfp_fl_pop_vlan *pop_v;
752 if (is_tcf_gact_shot(a)) {
753 nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_DROP);
754 } else if (is_tcf_mirred_egress_redirect(a)) {
755 err = nfp_flower_output_action(app, a, nfp_fl, a_len, netdev,
756 true, tun_type, tun_out_cnt,
757 out_cnt, csum_updated);
761 } else if (is_tcf_mirred_egress_mirror(a)) {
762 err = nfp_flower_output_action(app, a, nfp_fl, a_len, netdev,
763 false, tun_type, tun_out_cnt,
764 out_cnt, csum_updated);
768 } else if (is_tcf_vlan(a) && tcf_vlan_action(a) == TCA_VLAN_ACT_POP) {
769 if (*a_len + sizeof(struct nfp_fl_pop_vlan) > NFP_FL_MAX_A_SIZ)
772 pop_v = (struct nfp_fl_pop_vlan *)&nfp_fl->action_data[*a_len];
773 nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_POPV);
775 nfp_fl_pop_vlan(pop_v);
776 *a_len += sizeof(struct nfp_fl_pop_vlan);
777 } else if (is_tcf_vlan(a) && tcf_vlan_action(a) == TCA_VLAN_ACT_PUSH) {
778 if (*a_len + sizeof(struct nfp_fl_push_vlan) > NFP_FL_MAX_A_SIZ)
781 psh_v = (struct nfp_fl_push_vlan *)&nfp_fl->action_data[*a_len];
782 nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
784 nfp_fl_push_vlan(psh_v, a);
785 *a_len += sizeof(struct nfp_fl_push_vlan);
786 } else if (is_tcf_tunnel_set(a)) {
787 struct ip_tunnel_info *ip_tun = tcf_tunnel_info(a);
788 struct nfp_repr *repr = netdev_priv(netdev);
790 *tun_type = nfp_fl_get_tun_from_act_l4_port(repr->app, a);
791 if (*tun_type == NFP_FL_TUNNEL_NONE)
794 if (ip_tun->mode & ~NFP_FL_SUPPORTED_TUNNEL_INFO_FLAGS)
797 /* Pre-tunnel action is required for tunnel encap.
798 * This checks for next hop entries on NFP.
799 * If none, the packet falls back before applying other actions.
801 if (*a_len + sizeof(struct nfp_fl_pre_tunnel) +
802 sizeof(struct nfp_fl_set_ipv4_udp_tun) > NFP_FL_MAX_A_SIZ)
805 pre_tun = nfp_fl_pre_tunnel(nfp_fl->action_data, *a_len);
806 nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
807 *a_len += sizeof(struct nfp_fl_pre_tunnel);
809 err = nfp_fl_push_geneve_options(nfp_fl, a_len, a);
813 set_tun = (void *)&nfp_fl->action_data[*a_len];
814 err = nfp_fl_set_ipv4_udp_tun(app, set_tun, a, pre_tun,
818 *a_len += sizeof(struct nfp_fl_set_ipv4_udp_tun);
819 } else if (is_tcf_tunnel_release(a)) {
820 /* Tunnel decap is handled by default so accept action. */
822 } else if (is_tcf_pedit(a)) {
823 if (nfp_fl_pedit(a, flow, &nfp_fl->action_data[*a_len],
824 a_len, csum_updated))
826 } else if (is_tcf_csum(a)) {
827 /* csum action requests recalc of something we have not fixed */
828 if (tcf_csum_update_flags(a) & ~*csum_updated)
830 /* If we will correctly fix the csum we can remove it from the
831 * csum update list. Which will later be used to check support.
833 *csum_updated &= ~tcf_csum_update_flags(a);
835 /* Currently we do not handle any other actions. */
842 int nfp_flower_compile_action(struct nfp_app *app,
843 struct tc_cls_flower_offload *flow,
844 struct net_device *netdev,
845 struct nfp_fl_payload *nfp_flow)
847 int act_len, act_cnt, err, tun_out_cnt, out_cnt, i;
848 enum nfp_flower_tun_type tun_type;
849 const struct tc_action *a;
850 u32 csum_updated = 0;
852 memset(nfp_flow->action_data, 0, NFP_FL_MAX_A_SIZ);
853 nfp_flow->meta.act_len = 0;
854 tun_type = NFP_FL_TUNNEL_NONE;
860 tcf_exts_for_each_action(i, a, flow->exts) {
861 err = nfp_flower_loop_action(app, a, flow, nfp_flow, &act_len,
862 netdev, &tun_type, &tun_out_cnt,
863 &out_cnt, &csum_updated);
869 /* We optimise when the action list is small, this can unfortunately
870 * not happen once we have more than one action in the action list.
873 nfp_flow->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
875 nfp_flow->meta.act_len = act_len;