2 * Copyright (C) 2017 Netronome Systems, Inc.
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.
9 * The BSD 2-Clause License:
11 * Redistribution and use in source and binary forms, with or
12 * without modification, are permitted provided that the following
15 * 1. Redistributions of source code must retain the above
16 * copyright notice, this list of conditions and the following
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.
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
34 #include <linux/bitfield.h>
35 #include <net/pkt_cls.h>
36 #include <net/switchdev.h>
37 #include <net/tc_act/tc_gact.h>
38 #include <net/tc_act/tc_mirred.h>
39 #include <net/tc_act/tc_pedit.h>
40 #include <net/tc_act/tc_vlan.h>
41 #include <net/tc_act/tc_tunnel_key.h>
45 #include "../nfp_net_repr.h"
47 static void nfp_fl_pop_vlan(struct nfp_fl_pop_vlan *pop_vlan)
49 size_t act_size = sizeof(struct nfp_fl_pop_vlan);
51 pop_vlan->head.jump_id = NFP_FL_ACTION_OPCODE_POP_VLAN;
52 pop_vlan->head.len_lw = act_size >> NFP_FL_LW_SIZ;
53 pop_vlan->reserved = 0;
57 nfp_fl_push_vlan(struct nfp_fl_push_vlan *push_vlan,
58 const struct tc_action *action)
60 size_t act_size = sizeof(struct nfp_fl_push_vlan);
61 u16 tmp_push_vlan_tci;
63 push_vlan->head.jump_id = NFP_FL_ACTION_OPCODE_PUSH_VLAN;
64 push_vlan->head.len_lw = act_size >> NFP_FL_LW_SIZ;
65 push_vlan->reserved = 0;
66 push_vlan->vlan_tpid = tcf_vlan_push_proto(action);
69 FIELD_PREP(NFP_FL_PUSH_VLAN_PRIO, tcf_vlan_push_prio(action)) |
70 FIELD_PREP(NFP_FL_PUSH_VLAN_VID, tcf_vlan_push_vid(action)) |
72 push_vlan->vlan_tci = cpu_to_be16(tmp_push_vlan_tci);
75 static bool nfp_fl_netdev_is_tunnel_type(struct net_device *out_dev,
76 enum nfp_flower_tun_type tun_type)
78 if (!out_dev->rtnl_link_ops)
81 if (!strcmp(out_dev->rtnl_link_ops->kind, "vxlan"))
82 return tun_type == NFP_FL_TUNNEL_VXLAN;
84 if (!strcmp(out_dev->rtnl_link_ops->kind, "geneve"))
85 return tun_type == NFP_FL_TUNNEL_GENEVE;
91 nfp_fl_output(struct nfp_fl_output *output, const struct tc_action *action,
92 struct nfp_fl_payload *nfp_flow, bool last,
93 struct net_device *in_dev, enum nfp_flower_tun_type tun_type,
96 size_t act_size = sizeof(struct nfp_fl_output);
97 struct net_device *out_dev;
100 output->head.jump_id = NFP_FL_ACTION_OPCODE_OUTPUT;
101 output->head.len_lw = act_size >> NFP_FL_LW_SIZ;
103 out_dev = tcf_mirred_dev(action);
107 tmp_flags = last ? NFP_FL_OUT_FLAGS_LAST : 0;
110 /* Verify the egress netdev matches the tunnel type. */
111 if (!nfp_fl_netdev_is_tunnel_type(out_dev, tun_type))
118 output->flags = cpu_to_be16(tmp_flags |
119 NFP_FL_OUT_FLAGS_USE_TUN);
120 output->port = cpu_to_be32(NFP_FL_PORT_TYPE_TUN | tun_type);
122 /* Set action output parameters. */
123 output->flags = cpu_to_be16(tmp_flags);
125 /* Only offload if egress ports are on the same device as the
128 if (!switchdev_port_same_parent_id(in_dev, out_dev))
130 if (!nfp_netdev_is_nfp_repr(out_dev))
133 output->port = cpu_to_be32(nfp_repr_get_port_id(out_dev));
137 nfp_flow->meta.shortcut = output->port;
142 static enum nfp_flower_tun_type
143 nfp_fl_get_tun_from_act_l4_port(struct nfp_app *app,
144 const struct tc_action *action)
146 struct ip_tunnel_info *tun = tcf_tunnel_info(action);
147 struct nfp_flower_priv *priv = app->priv;
149 switch (tun->key.tp_dst) {
150 case htons(NFP_FL_VXLAN_PORT):
151 return NFP_FL_TUNNEL_VXLAN;
152 case htons(NFP_FL_GENEVE_PORT):
153 if (priv->flower_ext_feats & NFP_FL_FEATS_GENEVE)
154 return NFP_FL_TUNNEL_GENEVE;
157 return NFP_FL_TUNNEL_NONE;
161 static struct nfp_fl_pre_tunnel *nfp_fl_pre_tunnel(char *act_data, int act_len)
163 size_t act_size = sizeof(struct nfp_fl_pre_tunnel);
164 struct nfp_fl_pre_tunnel *pre_tun_act;
166 /* Pre_tunnel action must be first on action list.
167 * If other actions already exist they need pushed forward.
170 memmove(act_data + act_size, act_data, act_len);
172 pre_tun_act = (struct nfp_fl_pre_tunnel *)act_data;
174 memset(pre_tun_act, 0, act_size);
176 pre_tun_act->head.jump_id = NFP_FL_ACTION_OPCODE_PRE_TUNNEL;
177 pre_tun_act->head.len_lw = act_size >> NFP_FL_LW_SIZ;
183 nfp_fl_set_ipv4_udp_tun(struct nfp_fl_set_ipv4_udp_tun *set_tun,
184 const struct tc_action *action,
185 struct nfp_fl_pre_tunnel *pre_tun,
186 enum nfp_flower_tun_type tun_type)
188 size_t act_size = sizeof(struct nfp_fl_set_ipv4_udp_tun);
189 struct ip_tunnel_info *ip_tun = tcf_tunnel_info(action);
190 u32 tmp_set_ip_tun_type_index = 0;
191 /* Currently support one pre-tunnel so index is always 0. */
194 if (ip_tun->options_len)
197 set_tun->head.jump_id = NFP_FL_ACTION_OPCODE_SET_IPV4_TUNNEL;
198 set_tun->head.len_lw = act_size >> NFP_FL_LW_SIZ;
200 /* Set tunnel type and pre-tunnel index. */
201 tmp_set_ip_tun_type_index |=
202 FIELD_PREP(NFP_FL_IPV4_TUNNEL_TYPE, tun_type) |
203 FIELD_PREP(NFP_FL_IPV4_PRE_TUN_INDEX, pretun_idx);
205 set_tun->tun_type_index = cpu_to_be32(tmp_set_ip_tun_type_index);
206 set_tun->tun_id = ip_tun->key.tun_id;
208 /* Complete pre_tunnel action. */
209 pre_tun->ipv4_dst = ip_tun->key.u.ipv4.dst;
214 static void nfp_fl_set_helper32(u32 value, u32 mask, u8 *p_exact, u8 *p_mask)
216 u32 oldvalue = get_unaligned((u32 *)p_exact);
217 u32 oldmask = get_unaligned((u32 *)p_mask);
220 value |= oldvalue & ~mask;
222 put_unaligned(oldmask | mask, (u32 *)p_mask);
223 put_unaligned(value, (u32 *)p_exact);
227 nfp_fl_set_eth(const struct tc_action *action, int idx, u32 off,
228 struct nfp_fl_set_eth *set_eth)
232 if (off + 4 > ETH_ALEN * 2)
235 mask = ~tcf_pedit_mask(action, idx);
236 exact = tcf_pedit_val(action, idx);
241 nfp_fl_set_helper32(exact, mask, &set_eth->eth_addr_val[off],
242 &set_eth->eth_addr_mask[off]);
244 set_eth->reserved = cpu_to_be16(0);
245 set_eth->head.jump_id = NFP_FL_ACTION_OPCODE_SET_ETHERNET;
246 set_eth->head.len_lw = sizeof(*set_eth) >> NFP_FL_LW_SIZ;
252 nfp_fl_set_ip4(const struct tc_action *action, int idx, u32 off,
253 struct nfp_fl_set_ip4_addrs *set_ip_addr)
257 /* We are expecting tcf_pedit to return a big endian value */
258 mask = (__force __be32)~tcf_pedit_mask(action, idx);
259 exact = (__force __be32)tcf_pedit_val(action, idx);
265 case offsetof(struct iphdr, daddr):
266 set_ip_addr->ipv4_dst_mask = mask;
267 set_ip_addr->ipv4_dst = exact;
269 case offsetof(struct iphdr, saddr):
270 set_ip_addr->ipv4_src_mask = mask;
271 set_ip_addr->ipv4_src = exact;
277 set_ip_addr->reserved = cpu_to_be16(0);
278 set_ip_addr->head.jump_id = NFP_FL_ACTION_OPCODE_SET_IPV4_ADDRS;
279 set_ip_addr->head.len_lw = sizeof(*set_ip_addr) >> NFP_FL_LW_SIZ;
285 nfp_fl_set_ip6_helper(int opcode_tag, int idx, __be32 exact, __be32 mask,
286 struct nfp_fl_set_ipv6_addr *ip6)
288 ip6->ipv6[idx % 4].mask = mask;
289 ip6->ipv6[idx % 4].exact = exact;
291 ip6->reserved = cpu_to_be16(0);
292 ip6->head.jump_id = opcode_tag;
293 ip6->head.len_lw = sizeof(*ip6) >> NFP_FL_LW_SIZ;
297 nfp_fl_set_ip6(const struct tc_action *action, int idx, u32 off,
298 struct nfp_fl_set_ipv6_addr *ip_dst,
299 struct nfp_fl_set_ipv6_addr *ip_src)
303 /* We are expecting tcf_pedit to return a big endian value */
304 mask = (__force __be32)~tcf_pedit_mask(action, idx);
305 exact = (__force __be32)tcf_pedit_val(action, idx);
310 if (off < offsetof(struct ipv6hdr, saddr))
312 else if (off < offsetof(struct ipv6hdr, daddr))
313 nfp_fl_set_ip6_helper(NFP_FL_ACTION_OPCODE_SET_IPV6_SRC, idx,
314 exact, mask, ip_src);
315 else if (off < offsetof(struct ipv6hdr, daddr) +
316 sizeof(struct in6_addr))
317 nfp_fl_set_ip6_helper(NFP_FL_ACTION_OPCODE_SET_IPV6_DST, idx,
318 exact, mask, ip_dst);
326 nfp_fl_set_tport(const struct tc_action *action, int idx, u32 off,
327 struct nfp_fl_set_tport *set_tport, int opcode)
334 mask = ~tcf_pedit_mask(action, idx);
335 exact = tcf_pedit_val(action, idx);
340 nfp_fl_set_helper32(exact, mask, set_tport->tp_port_val,
341 set_tport->tp_port_mask);
343 set_tport->reserved = cpu_to_be16(0);
344 set_tport->head.jump_id = opcode;
345 set_tport->head.len_lw = sizeof(*set_tport) >> NFP_FL_LW_SIZ;
351 nfp_fl_pedit(const struct tc_action *action, char *nfp_action, int *a_len)
353 struct nfp_fl_set_ipv6_addr set_ip6_dst, set_ip6_src;
354 struct nfp_fl_set_ip4_addrs set_ip_addr;
355 struct nfp_fl_set_tport set_tport;
356 struct nfp_fl_set_eth set_eth;
357 enum pedit_header_type htype;
362 memset(&set_ip6_dst, 0, sizeof(set_ip6_dst));
363 memset(&set_ip6_src, 0, sizeof(set_ip6_src));
364 memset(&set_ip_addr, 0, sizeof(set_ip_addr));
365 memset(&set_tport, 0, sizeof(set_tport));
366 memset(&set_eth, 0, sizeof(set_eth));
367 nkeys = tcf_pedit_nkeys(action);
369 for (idx = 0; idx < nkeys; idx++) {
370 cmd = tcf_pedit_cmd(action, idx);
371 htype = tcf_pedit_htype(action, idx);
372 offset = tcf_pedit_offset(action, idx);
374 if (cmd != TCA_PEDIT_KEY_EX_CMD_SET)
378 case TCA_PEDIT_KEY_EX_HDR_TYPE_ETH:
379 err = nfp_fl_set_eth(action, idx, offset, &set_eth);
381 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP4:
382 err = nfp_fl_set_ip4(action, idx, offset, &set_ip_addr);
384 case TCA_PEDIT_KEY_EX_HDR_TYPE_IP6:
385 err = nfp_fl_set_ip6(action, idx, offset, &set_ip6_dst,
388 case TCA_PEDIT_KEY_EX_HDR_TYPE_TCP:
389 err = nfp_fl_set_tport(action, idx, offset, &set_tport,
390 NFP_FL_ACTION_OPCODE_SET_TCP);
392 case TCA_PEDIT_KEY_EX_HDR_TYPE_UDP:
393 err = nfp_fl_set_tport(action, idx, offset, &set_tport,
394 NFP_FL_ACTION_OPCODE_SET_UDP);
403 if (set_eth.head.len_lw) {
404 act_size = sizeof(set_eth);
405 memcpy(nfp_action, &set_eth, act_size);
407 } else if (set_ip_addr.head.len_lw) {
408 act_size = sizeof(set_ip_addr);
409 memcpy(nfp_action, &set_ip_addr, act_size);
411 } else if (set_ip6_dst.head.len_lw && set_ip6_src.head.len_lw) {
412 /* TC compiles set src and dst IPv6 address as a single action,
413 * the hardware requires this to be 2 separate actions.
415 act_size = sizeof(set_ip6_src);
416 memcpy(nfp_action, &set_ip6_src, act_size);
419 act_size = sizeof(set_ip6_dst);
420 memcpy(&nfp_action[sizeof(set_ip6_src)], &set_ip6_dst,
423 } else if (set_ip6_dst.head.len_lw) {
424 act_size = sizeof(set_ip6_dst);
425 memcpy(nfp_action, &set_ip6_dst, act_size);
427 } else if (set_ip6_src.head.len_lw) {
428 act_size = sizeof(set_ip6_src);
429 memcpy(nfp_action, &set_ip6_src, act_size);
431 } else if (set_tport.head.len_lw) {
432 act_size = sizeof(set_tport);
433 memcpy(nfp_action, &set_tport, act_size);
441 nfp_flower_loop_action(const struct tc_action *a,
442 struct nfp_fl_payload *nfp_fl, int *a_len,
443 struct net_device *netdev,
444 enum nfp_flower_tun_type *tun_type, int *tun_out_cnt)
446 struct nfp_fl_set_ipv4_udp_tun *set_tun;
447 struct nfp_fl_pre_tunnel *pre_tun;
448 struct nfp_fl_push_vlan *psh_v;
449 struct nfp_fl_pop_vlan *pop_v;
450 struct nfp_fl_output *output;
453 if (is_tcf_gact_shot(a)) {
454 nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_DROP);
455 } else if (is_tcf_mirred_egress_redirect(a)) {
456 if (*a_len + sizeof(struct nfp_fl_output) > NFP_FL_MAX_A_SIZ)
459 output = (struct nfp_fl_output *)&nfp_fl->action_data[*a_len];
460 err = nfp_fl_output(output, a, nfp_fl, true, netdev, *tun_type,
465 *a_len += sizeof(struct nfp_fl_output);
466 } else if (is_tcf_mirred_egress_mirror(a)) {
467 if (*a_len + sizeof(struct nfp_fl_output) > NFP_FL_MAX_A_SIZ)
470 output = (struct nfp_fl_output *)&nfp_fl->action_data[*a_len];
471 err = nfp_fl_output(output, a, nfp_fl, false, netdev, *tun_type,
476 *a_len += sizeof(struct nfp_fl_output);
477 } else if (is_tcf_vlan(a) && tcf_vlan_action(a) == TCA_VLAN_ACT_POP) {
478 if (*a_len + sizeof(struct nfp_fl_pop_vlan) > NFP_FL_MAX_A_SIZ)
481 pop_v = (struct nfp_fl_pop_vlan *)&nfp_fl->action_data[*a_len];
482 nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_POPV);
484 nfp_fl_pop_vlan(pop_v);
485 *a_len += sizeof(struct nfp_fl_pop_vlan);
486 } else if (is_tcf_vlan(a) && tcf_vlan_action(a) == TCA_VLAN_ACT_PUSH) {
487 if (*a_len + sizeof(struct nfp_fl_push_vlan) > NFP_FL_MAX_A_SIZ)
490 psh_v = (struct nfp_fl_push_vlan *)&nfp_fl->action_data[*a_len];
491 nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
493 nfp_fl_push_vlan(psh_v, a);
494 *a_len += sizeof(struct nfp_fl_push_vlan);
495 } else if (is_tcf_tunnel_set(a)) {
496 struct nfp_repr *repr = netdev_priv(netdev);
497 *tun_type = nfp_fl_get_tun_from_act_l4_port(repr->app, a);
498 if (*tun_type == NFP_FL_TUNNEL_NONE)
501 /* Pre-tunnel action is required for tunnel encap.
502 * This checks for next hop entries on NFP.
503 * If none, the packet falls back before applying other actions.
505 if (*a_len + sizeof(struct nfp_fl_pre_tunnel) +
506 sizeof(struct nfp_fl_set_ipv4_udp_tun) > NFP_FL_MAX_A_SIZ)
509 pre_tun = nfp_fl_pre_tunnel(nfp_fl->action_data, *a_len);
510 nfp_fl->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
511 *a_len += sizeof(struct nfp_fl_pre_tunnel);
513 set_tun = (void *)&nfp_fl->action_data[*a_len];
514 err = nfp_fl_set_ipv4_udp_tun(set_tun, a, pre_tun, *tun_type);
517 *a_len += sizeof(struct nfp_fl_set_ipv4_udp_tun);
518 } else if (is_tcf_tunnel_release(a)) {
519 /* Tunnel decap is handled by default so accept action. */
521 } else if (is_tcf_pedit(a)) {
522 if (nfp_fl_pedit(a, &nfp_fl->action_data[*a_len], a_len))
525 /* Currently we do not handle any other actions. */
532 int nfp_flower_compile_action(struct tc_cls_flower_offload *flow,
533 struct net_device *netdev,
534 struct nfp_fl_payload *nfp_flow)
536 int act_len, act_cnt, err, tun_out_cnt;
537 enum nfp_flower_tun_type tun_type;
538 const struct tc_action *a;
541 memset(nfp_flow->action_data, 0, NFP_FL_MAX_A_SIZ);
542 nfp_flow->meta.act_len = 0;
543 tun_type = NFP_FL_TUNNEL_NONE;
548 tcf_exts_to_list(flow->exts, &actions);
549 list_for_each_entry(a, &actions, list) {
550 err = nfp_flower_loop_action(a, nfp_flow, &act_len, netdev,
551 &tun_type, &tun_out_cnt);
557 /* We optimise when the action list is small, this can unfortunately
558 * not happen once we have more than one action in the action list.
561 nfp_flow->meta.shortcut = cpu_to_be32(NFP_FL_SC_ACT_NULL);
563 nfp_flow->meta.act_len = act_len;