1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /* Copyright (c) 2018 Mellanox Technologies. */
6 #include <net/geneve.h>
7 #include <net/bareudp.h>
11 #include "rep/neigh.h"
13 struct mlx5e_tc_tunnel *mlx5e_get_tc_tun(struct net_device *tunnel_dev)
15 if (netif_is_vxlan(tunnel_dev))
17 else if (netif_is_geneve(tunnel_dev))
18 return &geneve_tunnel;
19 else if (netif_is_gretap(tunnel_dev) ||
20 netif_is_ip6gretap(tunnel_dev))
22 else if (netif_is_bareudp(tunnel_dev))
23 return &mplsoudp_tunnel;
28 static int get_route_and_out_devs(struct mlx5e_priv *priv,
29 struct net_device *dev,
30 struct net_device **route_dev,
31 struct net_device **out_dev)
33 struct net_device *uplink_dev, *uplink_upper, *real_dev;
34 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
37 real_dev = is_vlan_dev(dev) ? vlan_dev_real_dev(dev) : dev;
38 uplink_dev = mlx5_eswitch_uplink_get_proto_dev(esw, REP_ETH);
41 uplink_upper = netdev_master_upper_dev_get_rcu(uplink_dev);
42 /* mlx5_lag_is_sriov() is a blocking function which can't be called
43 * while holding rcu read lock. Take the net_device for correctness
47 dev_hold(uplink_upper);
50 dst_is_lag_dev = (uplink_upper &&
51 netif_is_lag_master(uplink_upper) &&
52 real_dev == uplink_upper &&
53 mlx5_lag_is_sriov(priv->mdev));
55 dev_put(uplink_upper);
57 /* if the egress device isn't on the same HW e-switch or
58 * it's a LAG device, use the uplink
61 if (!netdev_port_same_parent_id(priv->netdev, real_dev) ||
62 dst_is_lag_dev || is_vlan_dev(*route_dev))
63 *out_dev = uplink_dev;
64 else if (mlx5e_eswitch_rep(dev) &&
65 mlx5e_is_valid_eswitch_fwd_dev(priv, dev))
66 *out_dev = *route_dev;
70 if (!(mlx5e_eswitch_rep(*out_dev) &&
71 mlx5e_is_uplink_rep(netdev_priv(*out_dev))))
74 if (mlx5e_eswitch_uplink_rep(priv->netdev) && *out_dev != priv->netdev)
80 static int mlx5e_route_lookup_ipv4_get(struct mlx5e_priv *priv,
81 struct net_device *mirred_dev,
82 struct net_device **out_dev,
83 struct net_device **route_dev,
85 struct neighbour **out_n,
91 #if IS_ENABLED(CONFIG_INET)
92 struct mlx5_core_dev *mdev = priv->mdev;
93 struct net_device *uplink_dev;
96 if (mlx5_lag_is_multipath(mdev)) {
97 struct mlx5_eswitch *esw = mdev->priv.eswitch;
99 uplink_dev = mlx5_eswitch_uplink_get_proto_dev(esw, REP_ETH);
100 fl4->flowi4_oif = uplink_dev->ifindex;
103 rt = ip_route_output_key(dev_net(mirred_dev), fl4);
107 if (mlx5_lag_is_multipath(mdev) && rt->rt_gw_family != AF_INET) {
115 ret = get_route_and_out_devs(priv, rt->dst.dev, route_dev, out_dev);
120 dev_hold(*route_dev);
123 *out_ttl = ip4_dst_hoplimit(&rt->dst);
124 n = dst_neigh_lookup(&rt->dst, &fl4->daddr);
135 static void mlx5e_route_lookup_ipv4_put(struct net_device *route_dev,
142 static const char *mlx5e_netdev_kind(struct net_device *dev)
144 if (dev->rtnl_link_ops)
145 return dev->rtnl_link_ops->kind;
150 static int mlx5e_gen_ip_tunnel_header(char buf[], __u8 *ip_proto,
151 struct mlx5e_encap_entry *e)
154 pr_warn("mlx5: Cannot generate tunnel header for this tunnel\n");
158 return e->tunnel->generate_ip_tun_hdr(buf, ip_proto, e);
161 static char *gen_eth_tnl_hdr(char *buf, struct net_device *dev,
162 struct mlx5e_encap_entry *e,
165 struct ethhdr *eth = (struct ethhdr *)buf;
168 ether_addr_copy(eth->h_dest, e->h_dest);
169 ether_addr_copy(eth->h_source, dev->dev_addr);
170 if (is_vlan_dev(dev)) {
171 struct vlan_hdr *vlan = (struct vlan_hdr *)
172 ((char *)eth + ETH_HLEN);
173 ip = (char *)vlan + VLAN_HLEN;
174 eth->h_proto = vlan_dev_vlan_proto(dev);
175 vlan->h_vlan_TCI = htons(vlan_dev_vlan_id(dev));
176 vlan->h_vlan_encapsulated_proto = htons(proto);
178 eth->h_proto = htons(proto);
179 ip = (char *)eth + ETH_HLEN;
185 int mlx5e_tc_tun_create_header_ipv4(struct mlx5e_priv *priv,
186 struct net_device *mirred_dev,
187 struct mlx5e_encap_entry *e)
189 int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
190 const struct ip_tunnel_key *tun_key = &e->tun_info->key;
191 struct net_device *out_dev, *route_dev;
192 struct flowi4 fl4 = {};
200 /* add the IP fields */
201 fl4.flowi4_tos = tun_key->tos;
202 fl4.daddr = tun_key->u.ipv4.dst;
203 fl4.saddr = tun_key->u.ipv4.src;
206 err = mlx5e_route_lookup_ipv4_get(priv, mirred_dev, &out_dev, &route_dev,
212 (is_vlan_dev(route_dev) ? VLAN_ETH_HLEN : ETH_HLEN) +
213 sizeof(struct iphdr) +
214 e->tunnel->calc_hlen(e);
216 if (max_encap_size < ipv4_encap_size) {
217 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
218 ipv4_encap_size, max_encap_size);
223 encap_header = kzalloc(ipv4_encap_size, GFP_KERNEL);
229 /* used by mlx5e_detach_encap to lookup a neigh hash table
230 * entry in the neigh hash table when a user deletes a rule
232 e->m_neigh.dev = n->dev;
233 e->m_neigh.family = n->ops->family;
234 memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
235 e->out_dev = out_dev;
236 e->route_dev_ifindex = route_dev->ifindex;
238 /* It's important to add the neigh to the hash table before checking
239 * the neigh validity state. So if we'll get a notification, in case the
240 * neigh changes it's validity state, we would find the relevant neigh
243 err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
247 read_lock_bh(&n->lock);
248 nud_state = n->nud_state;
249 ether_addr_copy(e->h_dest, n->ha);
250 read_unlock_bh(&n->lock);
252 /* add ethernet header */
253 ip = (struct iphdr *)gen_eth_tnl_hdr(encap_header, route_dev, e,
257 ip->tos = tun_key->tos;
261 ip->daddr = fl4.daddr;
262 ip->saddr = fl4.saddr;
264 /* add tunneling protocol header */
265 err = mlx5e_gen_ip_tunnel_header((char *)ip + sizeof(struct iphdr),
268 goto destroy_neigh_entry;
270 e->encap_size = ipv4_encap_size;
271 e->encap_header = encap_header;
273 if (!(nud_state & NUD_VALID)) {
274 neigh_event_send(n, NULL);
275 /* the encap entry will be made valid on neigh update event
276 * and not used before that.
280 e->pkt_reformat = mlx5_packet_reformat_alloc(priv->mdev,
282 ipv4_encap_size, encap_header,
283 MLX5_FLOW_NAMESPACE_FDB);
284 if (IS_ERR(e->pkt_reformat)) {
285 err = PTR_ERR(e->pkt_reformat);
286 goto destroy_neigh_entry;
289 e->flags |= MLX5_ENCAP_ENTRY_VALID;
290 mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
291 mlx5e_route_lookup_ipv4_put(route_dev, n);
295 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
299 mlx5e_route_lookup_ipv4_put(route_dev, n);
303 #if IS_ENABLED(CONFIG_INET) && IS_ENABLED(CONFIG_IPV6)
304 static int mlx5e_route_lookup_ipv6_get(struct mlx5e_priv *priv,
305 struct net_device *mirred_dev,
306 struct net_device **out_dev,
307 struct net_device **route_dev,
309 struct neighbour **out_n,
312 struct dst_entry *dst;
317 dst = ipv6_stub->ipv6_dst_lookup_flow(dev_net(mirred_dev), NULL, fl6,
323 *out_ttl = ip6_dst_hoplimit(dst);
325 ret = get_route_and_out_devs(priv, dst->dev, route_dev, out_dev);
331 dev_hold(*route_dev);
332 n = dst_neigh_lookup(dst, &fl6->daddr);
343 static void mlx5e_route_lookup_ipv6_put(struct net_device *route_dev,
350 int mlx5e_tc_tun_create_header_ipv6(struct mlx5e_priv *priv,
351 struct net_device *mirred_dev,
352 struct mlx5e_encap_entry *e)
354 int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
355 const struct ip_tunnel_key *tun_key = &e->tun_info->key;
356 struct net_device *out_dev, *route_dev;
357 struct flowi6 fl6 = {};
358 struct ipv6hdr *ip6h;
359 struct neighbour *n = NULL;
367 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tun_key->tos), tun_key->label);
368 fl6.daddr = tun_key->u.ipv6.dst;
369 fl6.saddr = tun_key->u.ipv6.src;
371 err = mlx5e_route_lookup_ipv6_get(priv, mirred_dev, &out_dev, &route_dev,
377 (is_vlan_dev(route_dev) ? VLAN_ETH_HLEN : ETH_HLEN) +
378 sizeof(struct ipv6hdr) +
379 e->tunnel->calc_hlen(e);
381 if (max_encap_size < ipv6_encap_size) {
382 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
383 ipv6_encap_size, max_encap_size);
388 encap_header = kzalloc(ipv6_encap_size, GFP_KERNEL);
394 /* used by mlx5e_detach_encap to lookup a neigh hash table
395 * entry in the neigh hash table when a user deletes a rule
397 e->m_neigh.dev = n->dev;
398 e->m_neigh.family = n->ops->family;
399 memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
400 e->out_dev = out_dev;
401 e->route_dev_ifindex = route_dev->ifindex;
403 /* It's importent to add the neigh to the hash table before checking
404 * the neigh validity state. So if we'll get a notification, in case the
405 * neigh changes it's validity state, we would find the relevant neigh
408 err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
412 read_lock_bh(&n->lock);
413 nud_state = n->nud_state;
414 ether_addr_copy(e->h_dest, n->ha);
415 read_unlock_bh(&n->lock);
417 /* add ethernet header */
418 ip6h = (struct ipv6hdr *)gen_eth_tnl_hdr(encap_header, route_dev, e,
422 ip6_flow_hdr(ip6h, tun_key->tos, 0);
423 /* the HW fills up ipv6 payload len */
424 ip6h->hop_limit = ttl;
425 ip6h->daddr = fl6.daddr;
426 ip6h->saddr = fl6.saddr;
428 /* add tunneling protocol header */
429 err = mlx5e_gen_ip_tunnel_header((char *)ip6h + sizeof(struct ipv6hdr),
432 goto destroy_neigh_entry;
434 e->encap_size = ipv6_encap_size;
435 e->encap_header = encap_header;
437 if (!(nud_state & NUD_VALID)) {
438 neigh_event_send(n, NULL);
439 /* the encap entry will be made valid on neigh update event
440 * and not used before that.
445 e->pkt_reformat = mlx5_packet_reformat_alloc(priv->mdev,
447 ipv6_encap_size, encap_header,
448 MLX5_FLOW_NAMESPACE_FDB);
449 if (IS_ERR(e->pkt_reformat)) {
450 err = PTR_ERR(e->pkt_reformat);
451 goto destroy_neigh_entry;
454 e->flags |= MLX5_ENCAP_ENTRY_VALID;
455 mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
456 mlx5e_route_lookup_ipv6_put(route_dev, n);
460 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
464 mlx5e_route_lookup_ipv6_put(route_dev, n);
469 bool mlx5e_tc_tun_device_to_offload(struct mlx5e_priv *priv,
470 struct net_device *netdev)
472 struct mlx5e_tc_tunnel *tunnel = mlx5e_get_tc_tun(netdev);
474 if (tunnel && tunnel->can_offload(priv))
480 int mlx5e_tc_tun_init_encap_attr(struct net_device *tunnel_dev,
481 struct mlx5e_priv *priv,
482 struct mlx5e_encap_entry *e,
483 struct netlink_ext_ack *extack)
485 struct mlx5e_tc_tunnel *tunnel = mlx5e_get_tc_tun(tunnel_dev);
488 e->reformat_type = -1;
492 return tunnel->init_encap_attr(tunnel_dev, priv, e, extack);
495 int mlx5e_tc_tun_parse(struct net_device *filter_dev,
496 struct mlx5e_priv *priv,
497 struct mlx5_flow_spec *spec,
498 struct flow_cls_offload *f,
501 struct mlx5e_tc_tunnel *tunnel = mlx5e_get_tc_tun(filter_dev);
502 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
503 void *headers_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
505 void *headers_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
507 struct netlink_ext_ack *extack = f->common.extack;
511 netdev_warn(priv->netdev,
512 "decapsulation offload is not supported for %s net device\n",
513 mlx5e_netdev_kind(filter_dev));
518 *match_level = tunnel->match_level;
520 if (tunnel->parse_udp_ports) {
521 err = tunnel->parse_udp_ports(priv, spec, f,
522 headers_c, headers_v);
527 if (tunnel->parse_tunnel) {
528 err = tunnel->parse_tunnel(priv, spec, f,
529 headers_c, headers_v);
534 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_CONTROL)) {
535 struct flow_dissector_key_basic key_basic = {};
536 struct flow_dissector_key_basic mask_basic = {
537 .n_proto = htons(0xFFFF),
539 struct flow_match_basic match_basic = {
540 .key = &key_basic, .mask = &mask_basic,
542 struct flow_match_control match;
545 flow_rule_match_enc_control(rule, &match);
546 addr_type = match.key->addr_type;
548 /* For tunnel addr_type used same key id`s as for non-tunnel */
549 if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
550 struct flow_match_ipv4_addrs match;
552 flow_rule_match_enc_ipv4_addrs(rule, &match);
553 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
554 src_ipv4_src_ipv6.ipv4_layout.ipv4,
555 ntohl(match.mask->src));
556 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
557 src_ipv4_src_ipv6.ipv4_layout.ipv4,
558 ntohl(match.key->src));
560 MLX5_SET(fte_match_set_lyr_2_4, headers_c,
561 dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
562 ntohl(match.mask->dst));
563 MLX5_SET(fte_match_set_lyr_2_4, headers_v,
564 dst_ipv4_dst_ipv6.ipv4_layout.ipv4,
565 ntohl(match.key->dst));
567 key_basic.n_proto = htons(ETH_P_IP);
568 mlx5e_tc_set_ethertype(priv->mdev, &match_basic, true,
569 headers_c, headers_v);
570 } else if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
571 struct flow_match_ipv6_addrs match;
573 flow_rule_match_enc_ipv6_addrs(rule, &match);
574 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
575 src_ipv4_src_ipv6.ipv6_layout.ipv6),
576 &match.mask->src, MLX5_FLD_SZ_BYTES(ipv6_layout,
578 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
579 src_ipv4_src_ipv6.ipv6_layout.ipv6),
580 &match.key->src, MLX5_FLD_SZ_BYTES(ipv6_layout,
583 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_c,
584 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
585 &match.mask->dst, MLX5_FLD_SZ_BYTES(ipv6_layout,
587 memcpy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
588 dst_ipv4_dst_ipv6.ipv6_layout.ipv6),
589 &match.key->dst, MLX5_FLD_SZ_BYTES(ipv6_layout,
592 key_basic.n_proto = htons(ETH_P_IPV6);
593 mlx5e_tc_set_ethertype(priv->mdev, &match_basic, true,
594 headers_c, headers_v);
598 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_IP)) {
599 struct flow_match_ip match;
601 flow_rule_match_enc_ip(rule, &match);
602 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_ecn,
603 match.mask->tos & 0x3);
604 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_ecn,
605 match.key->tos & 0x3);
607 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ip_dscp,
608 match.mask->tos >> 2);
609 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_dscp,
610 match.key->tos >> 2);
612 MLX5_SET(fte_match_set_lyr_2_4, headers_c, ttl_hoplimit,
614 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ttl_hoplimit,
617 if (match.mask->ttl &&
618 !MLX5_CAP_ESW_FLOWTABLE_FDB
620 ft_field_support.outer_ipv4_ttl)) {
621 NL_SET_ERR_MSG_MOD(extack,
622 "Matching on TTL is not supported");
628 /* Enforce DMAC when offloading incoming tunneled flows.
629 * Flow counters require a match on the DMAC.
631 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_47_16);
632 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, dmac_15_0);
633 ether_addr_copy(MLX5_ADDR_OF(fte_match_set_lyr_2_4, headers_v,
634 dmac_47_16), priv->netdev->dev_addr);
636 /* let software handle IP fragments */
637 MLX5_SET(fte_match_set_lyr_2_4, headers_c, frag, 1);
638 MLX5_SET(fte_match_set_lyr_2_4, headers_v, frag, 0);
646 int mlx5e_tc_tun_parse_udp_ports(struct mlx5e_priv *priv,
647 struct mlx5_flow_spec *spec,
648 struct flow_cls_offload *f,
652 struct flow_rule *rule = flow_cls_offload_flow_rule(f);
653 struct netlink_ext_ack *extack = f->common.extack;
654 struct flow_match_ports enc_ports;
656 /* Full udp dst port must be given */
658 if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_PORTS)) {
659 NL_SET_ERR_MSG_MOD(extack,
660 "UDP tunnel decap filter must include enc_dst_port condition");
661 netdev_warn(priv->netdev,
662 "UDP tunnel decap filter must include enc_dst_port condition\n");
666 flow_rule_match_enc_ports(rule, &enc_ports);
668 if (memchr_inv(&enc_ports.mask->dst, 0xff,
669 sizeof(enc_ports.mask->dst))) {
670 NL_SET_ERR_MSG_MOD(extack,
671 "UDP tunnel decap filter must match enc_dst_port fully");
672 netdev_warn(priv->netdev,
673 "UDP tunnel decap filter must match enc_dst_port fully\n");
677 /* match on UDP protocol and dst port number */
679 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ip_protocol);
680 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
682 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
683 ntohs(enc_ports.mask->dst));
684 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
685 ntohs(enc_ports.key->dst));
687 /* UDP src port on outer header is generated by HW,
688 * so it is probably a bad idea to request matching it.
689 * Nonetheless, it is allowed.
692 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
693 ntohs(enc_ports.mask->src));
694 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
695 ntohs(enc_ports.key->src));