1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /* Copyright (c) 2018 Mellanox Technologies. */
4 #include <net/inet_ecn.h>
7 #include <net/geneve.h>
8 #include <net/bareudp.h>
10 #include "en/tc_priv.h"
13 #include "rep/neigh.h"
17 struct mlx5e_tc_tun_route_attr {
18 struct net_device *out_dev;
19 struct net_device *route_dev;
28 #define TC_TUN_ROUTE_ATTR_INIT(name) struct mlx5e_tc_tun_route_attr name = {}
30 static void mlx5e_tc_tun_route_attr_cleanup(struct mlx5e_tc_tun_route_attr *attr)
33 neigh_release(attr->n);
35 dev_put(attr->route_dev);
38 struct mlx5e_tc_tunnel *mlx5e_get_tc_tun(struct net_device *tunnel_dev)
40 if (netif_is_vxlan(tunnel_dev))
42 else if (netif_is_geneve(tunnel_dev))
43 return &geneve_tunnel;
44 else if (netif_is_gretap(tunnel_dev) ||
45 netif_is_ip6gretap(tunnel_dev))
47 else if (netif_is_bareudp(tunnel_dev))
48 return &mplsoudp_tunnel;
53 static int get_route_and_out_devs(struct mlx5e_priv *priv,
54 struct net_device *dev,
55 struct net_device **route_dev,
56 struct net_device **out_dev)
58 struct net_device *uplink_dev, *uplink_upper, *real_dev;
59 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
62 real_dev = is_vlan_dev(dev) ? vlan_dev_real_dev(dev) : dev;
63 uplink_dev = mlx5_eswitch_uplink_get_proto_dev(esw, REP_ETH);
66 uplink_upper = netdev_master_upper_dev_get_rcu(uplink_dev);
67 /* mlx5_lag_is_sriov() is a blocking function which can't be called
68 * while holding rcu read lock. Take the net_device for correctness
72 dev_hold(uplink_upper);
75 dst_is_lag_dev = (uplink_upper &&
76 netif_is_lag_master(uplink_upper) &&
77 real_dev == uplink_upper &&
78 mlx5_lag_is_sriov(priv->mdev));
80 dev_put(uplink_upper);
82 /* if the egress device isn't on the same HW e-switch or
83 * it's a LAG device, use the uplink
86 if (!netdev_port_same_parent_id(priv->netdev, real_dev) ||
87 dst_is_lag_dev || is_vlan_dev(*route_dev) ||
88 netif_is_ovs_master(*route_dev))
89 *out_dev = uplink_dev;
90 else if (mlx5e_eswitch_rep(dev) &&
91 mlx5e_is_valid_eswitch_fwd_dev(priv, dev))
92 *out_dev = *route_dev;
96 if (!(mlx5e_eswitch_rep(*out_dev) &&
97 mlx5e_is_uplink_rep(netdev_priv(*out_dev))))
100 if (mlx5e_eswitch_uplink_rep(priv->netdev) && *out_dev != priv->netdev)
106 static int mlx5e_route_lookup_ipv4_get(struct mlx5e_priv *priv,
107 struct net_device *dev,
108 struct mlx5e_tc_tun_route_attr *attr)
110 struct net_device *route_dev;
111 struct net_device *out_dev;
115 #if IS_ENABLED(CONFIG_INET)
116 struct mlx5_core_dev *mdev = priv->mdev;
117 struct net_device *uplink_dev;
120 if (mlx5_lag_is_multipath(mdev)) {
121 struct mlx5_eswitch *esw = mdev->priv.eswitch;
123 uplink_dev = mlx5_eswitch_uplink_get_proto_dev(esw, REP_ETH);
124 attr->fl.fl4.flowi4_oif = uplink_dev->ifindex;
126 struct mlx5e_tc_tunnel *tunnel = mlx5e_get_tc_tun(dev);
128 if (tunnel && tunnel->get_remote_ifindex)
129 attr->fl.fl4.flowi4_oif = tunnel->get_remote_ifindex(dev);
132 rt = ip_route_output_key(dev_net(dev), &attr->fl.fl4);
136 if (rt->rt_type != RTN_UNICAST) {
141 if (mlx5_lag_is_multipath(mdev) && rt->rt_gw_family != AF_INET) {
149 ret = get_route_and_out_devs(priv, rt->dst.dev, &route_dev, &out_dev);
155 attr->ttl = ip4_dst_hoplimit(&rt->dst);
156 n = dst_neigh_lookup(&rt->dst, &attr->fl.fl4.daddr);
159 goto err_dev_release;
163 attr->route_dev = route_dev;
164 attr->out_dev = out_dev;
175 static void mlx5e_route_lookup_ipv4_put(struct mlx5e_tc_tun_route_attr *attr)
177 mlx5e_tc_tun_route_attr_cleanup(attr);
180 static const char *mlx5e_netdev_kind(struct net_device *dev)
182 if (dev->rtnl_link_ops)
183 return dev->rtnl_link_ops->kind;
188 static int mlx5e_gen_ip_tunnel_header(char buf[], __u8 *ip_proto,
189 struct mlx5e_encap_entry *e)
192 pr_warn("mlx5: Cannot generate tunnel header for this tunnel\n");
196 return e->tunnel->generate_ip_tun_hdr(buf, ip_proto, e);
199 static char *gen_eth_tnl_hdr(char *buf, struct net_device *dev,
200 struct mlx5e_encap_entry *e,
203 struct ethhdr *eth = (struct ethhdr *)buf;
206 ether_addr_copy(eth->h_dest, e->h_dest);
207 ether_addr_copy(eth->h_source, dev->dev_addr);
208 if (is_vlan_dev(dev)) {
209 struct vlan_hdr *vlan = (struct vlan_hdr *)
210 ((char *)eth + ETH_HLEN);
211 ip = (char *)vlan + VLAN_HLEN;
212 eth->h_proto = vlan_dev_vlan_proto(dev);
213 vlan->h_vlan_TCI = htons(vlan_dev_vlan_id(dev));
214 vlan->h_vlan_encapsulated_proto = htons(proto);
216 eth->h_proto = htons(proto);
217 ip = (char *)eth + ETH_HLEN;
223 int mlx5e_tc_tun_create_header_ipv4(struct mlx5e_priv *priv,
224 struct net_device *mirred_dev,
225 struct mlx5e_encap_entry *e)
227 int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
228 const struct ip_tunnel_key *tun_key = &e->tun_info->key;
229 struct mlx5_pkt_reformat_params reformat_params;
230 struct mlx5e_neigh m_neigh = {};
231 TC_TUN_ROUTE_ATTR_INIT(attr);
238 /* add the IP fields */
239 attr.fl.fl4.flowi4_tos = tun_key->tos & ~INET_ECN_MASK;
240 attr.fl.fl4.daddr = tun_key->u.ipv4.dst;
241 attr.fl.fl4.saddr = tun_key->u.ipv4.src;
242 attr.ttl = tun_key->ttl;
244 err = mlx5e_route_lookup_ipv4_get(priv, mirred_dev, &attr);
249 (is_vlan_dev(attr.route_dev) ? VLAN_ETH_HLEN : ETH_HLEN) +
250 sizeof(struct iphdr) +
251 e->tunnel->calc_hlen(e);
253 if (max_encap_size < ipv4_encap_size) {
254 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
255 ipv4_encap_size, max_encap_size);
260 encap_header = kzalloc(ipv4_encap_size, GFP_KERNEL);
266 m_neigh.family = attr.n->ops->family;
267 memcpy(&m_neigh.dst_ip, attr.n->primary_key, attr.n->tbl->key_len);
268 e->out_dev = attr.out_dev;
269 e->route_dev_ifindex = attr.route_dev->ifindex;
271 /* It's important to add the neigh to the hash table before checking
272 * the neigh validity state. So if we'll get a notification, in case the
273 * neigh changes it's validity state, we would find the relevant neigh
276 err = mlx5e_rep_encap_entry_attach(netdev_priv(attr.out_dev), e, &m_neigh, attr.n->dev);
280 read_lock_bh(&attr.n->lock);
281 nud_state = attr.n->nud_state;
282 ether_addr_copy(e->h_dest, attr.n->ha);
283 read_unlock_bh(&attr.n->lock);
285 /* add ethernet header */
286 ip = (struct iphdr *)gen_eth_tnl_hdr(encap_header, attr.route_dev, e,
290 ip->tos = tun_key->tos;
294 ip->daddr = attr.fl.fl4.daddr;
295 ip->saddr = attr.fl.fl4.saddr;
297 /* add tunneling protocol header */
298 err = mlx5e_gen_ip_tunnel_header((char *)ip + sizeof(struct iphdr),
301 goto destroy_neigh_entry;
303 e->encap_size = ipv4_encap_size;
304 e->encap_header = encap_header;
306 if (!(nud_state & NUD_VALID)) {
307 neigh_event_send(attr.n, NULL);
308 /* the encap entry will be made valid on neigh update event
309 * and not used before that.
314 memset(&reformat_params, 0, sizeof(reformat_params));
315 reformat_params.type = e->reformat_type;
316 reformat_params.size = ipv4_encap_size;
317 reformat_params.data = encap_header;
318 e->pkt_reformat = mlx5_packet_reformat_alloc(priv->mdev, &reformat_params,
319 MLX5_FLOW_NAMESPACE_FDB);
320 if (IS_ERR(e->pkt_reformat)) {
321 err = PTR_ERR(e->pkt_reformat);
322 goto destroy_neigh_entry;
325 e->flags |= MLX5_ENCAP_ENTRY_VALID;
326 mlx5e_rep_queue_neigh_stats_work(netdev_priv(attr.out_dev));
327 mlx5e_route_lookup_ipv4_put(&attr);
331 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
335 mlx5e_route_lookup_ipv4_put(&attr);
339 int mlx5e_tc_tun_update_header_ipv4(struct mlx5e_priv *priv,
340 struct net_device *mirred_dev,
341 struct mlx5e_encap_entry *e)
343 int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
344 const struct ip_tunnel_key *tun_key = &e->tun_info->key;
345 struct mlx5_pkt_reformat_params reformat_params;
346 TC_TUN_ROUTE_ATTR_INIT(attr);
353 /* add the IP fields */
354 attr.fl.fl4.flowi4_tos = tun_key->tos & ~INET_ECN_MASK;
355 attr.fl.fl4.daddr = tun_key->u.ipv4.dst;
356 attr.fl.fl4.saddr = tun_key->u.ipv4.src;
357 attr.ttl = tun_key->ttl;
359 err = mlx5e_route_lookup_ipv4_get(priv, mirred_dev, &attr);
364 (is_vlan_dev(attr.route_dev) ? VLAN_ETH_HLEN : ETH_HLEN) +
365 sizeof(struct iphdr) +
366 e->tunnel->calc_hlen(e);
368 if (max_encap_size < ipv4_encap_size) {
369 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
370 ipv4_encap_size, max_encap_size);
375 encap_header = kzalloc(ipv4_encap_size, GFP_KERNEL);
381 e->route_dev_ifindex = attr.route_dev->ifindex;
383 read_lock_bh(&attr.n->lock);
384 nud_state = attr.n->nud_state;
385 ether_addr_copy(e->h_dest, attr.n->ha);
386 WRITE_ONCE(e->nhe->neigh_dev, attr.n->dev);
387 read_unlock_bh(&attr.n->lock);
389 /* add ethernet header */
390 ip = (struct iphdr *)gen_eth_tnl_hdr(encap_header, attr.route_dev, e,
394 ip->tos = tun_key->tos;
398 ip->daddr = attr.fl.fl4.daddr;
399 ip->saddr = attr.fl.fl4.saddr;
401 /* add tunneling protocol header */
402 err = mlx5e_gen_ip_tunnel_header((char *)ip + sizeof(struct iphdr),
407 e->encap_size = ipv4_encap_size;
408 kfree(e->encap_header);
409 e->encap_header = encap_header;
411 if (!(nud_state & NUD_VALID)) {
412 neigh_event_send(attr.n, NULL);
413 /* the encap entry will be made valid on neigh update event
414 * and not used before that.
419 memset(&reformat_params, 0, sizeof(reformat_params));
420 reformat_params.type = e->reformat_type;
421 reformat_params.size = ipv4_encap_size;
422 reformat_params.data = encap_header;
423 e->pkt_reformat = mlx5_packet_reformat_alloc(priv->mdev, &reformat_params,
424 MLX5_FLOW_NAMESPACE_FDB);
425 if (IS_ERR(e->pkt_reformat)) {
426 err = PTR_ERR(e->pkt_reformat);
430 e->flags |= MLX5_ENCAP_ENTRY_VALID;
431 mlx5e_rep_queue_neigh_stats_work(netdev_priv(attr.out_dev));
432 mlx5e_route_lookup_ipv4_put(&attr);
438 mlx5e_route_lookup_ipv4_put(&attr);
442 #if IS_ENABLED(CONFIG_INET) && IS_ENABLED(CONFIG_IPV6)
443 static int mlx5e_route_lookup_ipv6_get(struct mlx5e_priv *priv,
444 struct net_device *dev,
445 struct mlx5e_tc_tun_route_attr *attr)
447 struct mlx5e_tc_tunnel *tunnel = mlx5e_get_tc_tun(dev);
448 struct net_device *route_dev;
449 struct net_device *out_dev;
450 struct dst_entry *dst;
454 if (tunnel && tunnel->get_remote_ifindex)
455 attr->fl.fl6.flowi6_oif = tunnel->get_remote_ifindex(dev);
456 dst = ipv6_stub->ipv6_dst_lookup_flow(dev_net(dev), NULL, &attr->fl.fl6,
462 attr->ttl = ip6_dst_hoplimit(dst);
464 ret = get_route_and_out_devs(priv, dst->dev, &route_dev, &out_dev);
466 goto err_dst_release;
469 n = dst_neigh_lookup(dst, &attr->fl.fl6.daddr);
472 goto err_dev_release;
476 attr->out_dev = out_dev;
477 attr->route_dev = route_dev;
488 static void mlx5e_route_lookup_ipv6_put(struct mlx5e_tc_tun_route_attr *attr)
490 mlx5e_tc_tun_route_attr_cleanup(attr);
493 int mlx5e_tc_tun_create_header_ipv6(struct mlx5e_priv *priv,
494 struct net_device *mirred_dev,
495 struct mlx5e_encap_entry *e)
497 int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
498 const struct ip_tunnel_key *tun_key = &e->tun_info->key;
499 struct mlx5_pkt_reformat_params reformat_params;
500 struct mlx5e_neigh m_neigh = {};
501 TC_TUN_ROUTE_ATTR_INIT(attr);
502 struct ipv6hdr *ip6h;
508 attr.ttl = tun_key->ttl;
509 attr.fl.fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tun_key->tos), tun_key->label);
510 attr.fl.fl6.daddr = tun_key->u.ipv6.dst;
511 attr.fl.fl6.saddr = tun_key->u.ipv6.src;
513 err = mlx5e_route_lookup_ipv6_get(priv, mirred_dev, &attr);
518 (is_vlan_dev(attr.route_dev) ? VLAN_ETH_HLEN : ETH_HLEN) +
519 sizeof(struct ipv6hdr) +
520 e->tunnel->calc_hlen(e);
522 if (max_encap_size < ipv6_encap_size) {
523 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
524 ipv6_encap_size, max_encap_size);
529 encap_header = kzalloc(ipv6_encap_size, GFP_KERNEL);
535 m_neigh.family = attr.n->ops->family;
536 memcpy(&m_neigh.dst_ip, attr.n->primary_key, attr.n->tbl->key_len);
537 e->out_dev = attr.out_dev;
538 e->route_dev_ifindex = attr.route_dev->ifindex;
540 /* It's important to add the neigh to the hash table before checking
541 * the neigh validity state. So if we'll get a notification, in case the
542 * neigh changes it's validity state, we would find the relevant neigh
545 err = mlx5e_rep_encap_entry_attach(netdev_priv(attr.out_dev), e, &m_neigh, attr.n->dev);
549 read_lock_bh(&attr.n->lock);
550 nud_state = attr.n->nud_state;
551 ether_addr_copy(e->h_dest, attr.n->ha);
552 read_unlock_bh(&attr.n->lock);
554 /* add ethernet header */
555 ip6h = (struct ipv6hdr *)gen_eth_tnl_hdr(encap_header, attr.route_dev, e,
559 ip6_flow_hdr(ip6h, tun_key->tos, 0);
560 /* the HW fills up ipv6 payload len */
561 ip6h->hop_limit = attr.ttl;
562 ip6h->daddr = attr.fl.fl6.daddr;
563 ip6h->saddr = attr.fl.fl6.saddr;
565 /* add tunneling protocol header */
566 err = mlx5e_gen_ip_tunnel_header((char *)ip6h + sizeof(struct ipv6hdr),
569 goto destroy_neigh_entry;
571 e->encap_size = ipv6_encap_size;
572 e->encap_header = encap_header;
574 if (!(nud_state & NUD_VALID)) {
575 neigh_event_send(attr.n, NULL);
576 /* the encap entry will be made valid on neigh update event
577 * and not used before that.
582 memset(&reformat_params, 0, sizeof(reformat_params));
583 reformat_params.type = e->reformat_type;
584 reformat_params.size = ipv6_encap_size;
585 reformat_params.data = encap_header;
586 e->pkt_reformat = mlx5_packet_reformat_alloc(priv->mdev, &reformat_params,
587 MLX5_FLOW_NAMESPACE_FDB);
588 if (IS_ERR(e->pkt_reformat)) {
589 err = PTR_ERR(e->pkt_reformat);
590 goto destroy_neigh_entry;
593 e->flags |= MLX5_ENCAP_ENTRY_VALID;
594 mlx5e_rep_queue_neigh_stats_work(netdev_priv(attr.out_dev));
595 mlx5e_route_lookup_ipv6_put(&attr);
599 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
603 mlx5e_route_lookup_ipv6_put(&attr);
607 int mlx5e_tc_tun_update_header_ipv6(struct mlx5e_priv *priv,
608 struct net_device *mirred_dev,
609 struct mlx5e_encap_entry *e)
611 int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
612 const struct ip_tunnel_key *tun_key = &e->tun_info->key;
613 struct mlx5_pkt_reformat_params reformat_params;
614 TC_TUN_ROUTE_ATTR_INIT(attr);
615 struct ipv6hdr *ip6h;
621 attr.ttl = tun_key->ttl;
623 attr.fl.fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tun_key->tos), tun_key->label);
624 attr.fl.fl6.daddr = tun_key->u.ipv6.dst;
625 attr.fl.fl6.saddr = tun_key->u.ipv6.src;
627 err = mlx5e_route_lookup_ipv6_get(priv, mirred_dev, &attr);
632 (is_vlan_dev(attr.route_dev) ? VLAN_ETH_HLEN : ETH_HLEN) +
633 sizeof(struct ipv6hdr) +
634 e->tunnel->calc_hlen(e);
636 if (max_encap_size < ipv6_encap_size) {
637 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
638 ipv6_encap_size, max_encap_size);
643 encap_header = kzalloc(ipv6_encap_size, GFP_KERNEL);
649 e->route_dev_ifindex = attr.route_dev->ifindex;
651 read_lock_bh(&attr.n->lock);
652 nud_state = attr.n->nud_state;
653 ether_addr_copy(e->h_dest, attr.n->ha);
654 WRITE_ONCE(e->nhe->neigh_dev, attr.n->dev);
655 read_unlock_bh(&attr.n->lock);
657 /* add ethernet header */
658 ip6h = (struct ipv6hdr *)gen_eth_tnl_hdr(encap_header, attr.route_dev, e,
662 ip6_flow_hdr(ip6h, tun_key->tos, 0);
663 /* the HW fills up ipv6 payload len */
664 ip6h->hop_limit = attr.ttl;
665 ip6h->daddr = attr.fl.fl6.daddr;
666 ip6h->saddr = attr.fl.fl6.saddr;
668 /* add tunneling protocol header */
669 err = mlx5e_gen_ip_tunnel_header((char *)ip6h + sizeof(struct ipv6hdr),
674 e->encap_size = ipv6_encap_size;
675 kfree(e->encap_header);
676 e->encap_header = encap_header;
678 if (!(nud_state & NUD_VALID)) {
679 neigh_event_send(attr.n, NULL);
680 /* the encap entry will be made valid on neigh update event
681 * and not used before that.
686 memset(&reformat_params, 0, sizeof(reformat_params));
687 reformat_params.type = e->reformat_type;
688 reformat_params.size = ipv6_encap_size;
689 reformat_params.data = encap_header;
690 e->pkt_reformat = mlx5_packet_reformat_alloc(priv->mdev, &reformat_params,
691 MLX5_FLOW_NAMESPACE_FDB);
692 if (IS_ERR(e->pkt_reformat)) {
693 err = PTR_ERR(e->pkt_reformat);
697 e->flags |= MLX5_ENCAP_ENTRY_VALID;
698 mlx5e_rep_queue_neigh_stats_work(netdev_priv(attr.out_dev));
699 mlx5e_route_lookup_ipv6_put(&attr);
705 mlx5e_route_lookup_ipv6_put(&attr);
710 int mlx5e_tc_tun_route_lookup(struct mlx5e_priv *priv,
711 struct mlx5_flow_spec *spec,
712 struct mlx5_flow_attr *flow_attr,
713 struct net_device *filter_dev)
715 struct mlx5_esw_flow_attr *esw_attr = flow_attr->esw_attr;
716 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
717 struct mlx5e_tc_int_port *int_port;
718 TC_TUN_ROUTE_ATTR_INIT(attr);
722 if (flow_attr->tun_ip_version == 4) {
723 /* Addresses are swapped for decap */
724 attr.fl.fl4.saddr = esw_attr->rx_tun_attr->dst_ip.v4;
725 attr.fl.fl4.daddr = esw_attr->rx_tun_attr->src_ip.v4;
726 err = mlx5e_route_lookup_ipv4_get(priv, filter_dev, &attr);
728 #if IS_ENABLED(CONFIG_INET) && IS_ENABLED(CONFIG_IPV6)
729 else if (flow_attr->tun_ip_version == 6) {
730 /* Addresses are swapped for decap */
731 attr.fl.fl6.saddr = esw_attr->rx_tun_attr->dst_ip.v6;
732 attr.fl.fl6.daddr = esw_attr->rx_tun_attr->src_ip.v6;
733 err = mlx5e_route_lookup_ipv6_get(priv, filter_dev, &attr);
742 if (attr.route_dev->netdev_ops == &mlx5e_netdev_ops &&
743 mlx5e_tc_is_vf_tunnel(attr.out_dev, attr.route_dev)) {
744 err = mlx5e_tc_query_route_vport(attr.out_dev, attr.route_dev, &vport_num);
748 esw_attr->rx_tun_attr->vni = MLX5_GET(fte_match_param, spec->match_value,
749 misc_parameters.vxlan_vni);
750 esw_attr->rx_tun_attr->decap_vport = vport_num;
751 } else if (netif_is_ovs_master(attr.route_dev) && mlx5e_tc_int_port_supported(esw)) {
752 int_port = mlx5e_tc_int_port_get(mlx5e_get_int_port_priv(priv),
753 attr.route_dev->ifindex,
754 MLX5E_TC_INT_PORT_INGRESS);
755 if (IS_ERR(int_port)) {
756 err = PTR_ERR(int_port);
759 esw_attr->int_port = int_port;
763 if (flow_attr->tun_ip_version == 4)
764 mlx5e_route_lookup_ipv4_put(&attr);
765 #if IS_ENABLED(CONFIG_INET) && IS_ENABLED(CONFIG_IPV6)
766 else if (flow_attr->tun_ip_version == 6)
767 mlx5e_route_lookup_ipv6_put(&attr);
772 bool mlx5e_tc_tun_device_to_offload(struct mlx5e_priv *priv,
773 struct net_device *netdev)
775 struct mlx5e_tc_tunnel *tunnel = mlx5e_get_tc_tun(netdev);
777 if (tunnel && tunnel->can_offload(priv))
783 int mlx5e_tc_tun_init_encap_attr(struct net_device *tunnel_dev,
784 struct mlx5e_priv *priv,
785 struct mlx5e_encap_entry *e,
786 struct netlink_ext_ack *extack)
788 struct mlx5e_tc_tunnel *tunnel = mlx5e_get_tc_tun(tunnel_dev);
791 e->reformat_type = -1;
795 return tunnel->init_encap_attr(tunnel_dev, priv, e, extack);
798 int mlx5e_tc_tun_parse(struct net_device *filter_dev,
799 struct mlx5e_priv *priv,
800 struct mlx5_flow_spec *spec,
801 struct flow_cls_offload *f,
804 struct mlx5e_tc_tunnel *tunnel = mlx5e_get_tc_tun(filter_dev);
805 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
806 void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
808 void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
810 struct netlink_ext_ack *extack = f->common.extack;
814 netdev_warn(priv->netdev,
815 "decapsulation offload is not supported for %s net device\n",
816 mlx5e_netdev_kind(filter_dev));
821 *match_level = tunnel->match_level;
823 if (tunnel->parse_udp_ports) {
824 err = tunnel->parse_udp_ports(priv, spec, f,
825 headers_c, headers_v);
830 if (tunnel->parse_tunnel) {
831 err = tunnel->parse_tunnel(priv, spec, f,
832 headers_c, headers_v);
837 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_CONTROL)) {
838 struct flow_dissector_key_basic key_basic = {};
839 struct flow_dissector_key_basic mask_basic = {
840 .n_proto = htons(0xFFFF),
842 struct flow_match_basic match_basic = {
843 .key = &key_basic, .mask = &mask_basic,
845 struct flow_match_control match;
848 flow_rule_match_enc_control(rule, &match);
849 addr_type = match.key->addr_type;
851 /* For tunnel addr_type used same key id`s as for non-tunnel */
852 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
853 struct flow_match_ipv4_addrs match;
855 flow_rule_match_enc_ipv4_addrs(rule, &match);
856 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
857 src_ipv4_src_ipv6.ipv4_layout.ipv4,
858 ntohl(match.mask->src));
859 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
860 src_ipv4_src_ipv6.ipv4_layout.ipv4,
861 ntohl(match.key->src));
863 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
864 dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
865 ntohl(match.mask->dst));
866 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
867 dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
868 ntohl(match.key->dst));
870 key_basic.n_proto = htons(ETH_P_IP);
871 mlx5e_tc_set_ethertype(priv->mdev, &match_basic, true,
872 headers_c, headers_v);
873 } else if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
874 struct flow_match_ipv6_addrs match;
876 flow_rule_match_enc_ipv6_addrs(rule, &match);
877 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
878 src_ipv4_src_ipv6.ipv6_layout.ipv6),
879 &match.mask->src, MLX5_FLD_SZ_BYTES(ipv6_layout,
881 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
882 src_ipv4_src_ipv6.ipv6_layout.ipv6),
883 &match.key->src, MLX5_FLD_SZ_BYTES(ipv6_layout,
886 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
887 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
888 &match.mask->dst, MLX5_FLD_SZ_BYTES(ipv6_layout,
890 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
891 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
892 &match.key->dst, MLX5_FLD_SZ_BYTES(ipv6_layout,
895 key_basic.n_proto = htons(ETH_P_IPV6);
896 mlx5e_tc_set_ethertype(priv->mdev, &match_basic, true,
897 headers_c, headers_v);
901 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_IP)) {
902 struct flow_match_ip match;
904 flow_rule_match_enc_ip(rule, &match);
905 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_ecn,
906 match.mask->tos & 0x3);
907 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn,
908 match.key->tos & 0x3);
910 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_dscp,
911 match.mask->tos >> 2);
912 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp,
913 match.key->tos >> 2);
915 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ttl_hoplimit,
917 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ttl_hoplimit,
920 if (match.mask->ttl &&
921 !MLX5_CAP_ESW_FLOWTABLE_FDB
923 ft_field_support.outer_ipv4_ttl)) {
924 NL_SET_ERR_MSG_MOD(extack,
925 "Matching on TTL is not supported");
931 /* let software handle IP fragments */
932 MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
933 MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 0);
941 int mlx5e_tc_tun_parse_udp_ports(struct mlx5e_priv *priv,
942 struct mlx5_flow_spec *spec,
943 struct flow_cls_offload *f,
947 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
948 struct netlink_ext_ack *extack = f->common.extack;
949 struct flow_match_ports enc_ports;
951 /* Full udp dst port must be given */
953 if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
954 NL_SET_ERR_MSG_MOD(extack,
955 "UDP tunnel decap filter must include enc_dst_port condition");
956 netdev_warn(priv->netdev,
957 "UDP tunnel decap filter must include enc_dst_port condition\n");
961 flow_rule_match_enc_ports(rule, &enc_ports);
963 if (memchr_inv(&enc_ports.mask->dst, 0xff,
964 sizeof(enc_ports.mask->dst))) {
965 NL_SET_ERR_MSG_MOD(extack,
966 "UDP tunnel decap filter must match enc_dst_port fully");
967 netdev_warn(priv->netdev,
968 "UDP tunnel decap filter must match enc_dst_port fully\n");
972 /* match on UDP protocol and dst port number */
974 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ip_protocol);
975 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
977 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
978 ntohs(enc_ports.mask->dst));
979 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
980 ntohs(enc_ports.key->dst));
982 /* UDP src port on outer header is generated by HW,
983 * so it is probably a bad idea to request matching it.
984 * Nonetheless, it is allowed.
987 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
988 ntohs(enc_ports.mask->src));
989 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
990 ntohs(enc_ports.key->src));