1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /* Copyright (c) 2018 Mellanox Technologies. */
9 static int get_route_and_out_devs(struct mlx5e_priv *priv,
10 struct net_device *dev,
11 struct net_device **route_dev,
12 struct net_device **out_dev)
14 struct mlx5_eswitch *esw = priv->mdev->priv.eswitch;
15 struct net_device *uplink_dev, *uplink_upper;
18 uplink_dev = mlx5_eswitch_uplink_get_proto_dev(esw, REP_ETH);
19 uplink_upper = netdev_master_upper_dev_get(uplink_dev);
20 dst_is_lag_dev = (uplink_upper &&
21 netif_is_lag_master(uplink_upper) &&
22 dev == uplink_upper &&
23 mlx5_lag_is_sriov(priv->mdev));
25 /* if the egress device isn't on the same HW e-switch or
26 * it's a LAG device, use the uplink
28 if (!netdev_port_same_parent_id(priv->netdev, dev) ||
30 *route_dev = uplink_dev;
31 *out_dev = *route_dev;
34 if (is_vlan_dev(*route_dev))
35 *out_dev = uplink_dev;
36 else if (mlx5e_eswitch_rep(dev))
37 *out_dev = *route_dev;
42 if (!(mlx5e_eswitch_rep(*out_dev) &&
43 mlx5e_is_uplink_rep(netdev_priv(*out_dev))))
49 static int mlx5e_route_lookup_ipv4(struct mlx5e_priv *priv,
50 struct net_device *mirred_dev,
51 struct net_device **out_dev,
52 struct net_device **route_dev,
54 struct neighbour **out_n,
58 struct neighbour *n = NULL;
60 #if IS_ENABLED(CONFIG_INET)
61 struct mlx5_core_dev *mdev = priv->mdev;
62 struct net_device *uplink_dev;
65 if (mlx5_lag_is_multipath(mdev)) {
66 struct mlx5_eswitch *esw = mdev->priv.eswitch;
68 uplink_dev = mlx5_eswitch_uplink_get_proto_dev(esw, REP_ETH);
69 fl4->flowi4_oif = uplink_dev->ifindex;
72 rt = ip_route_output_key(dev_net(mirred_dev), fl4);
73 ret = PTR_ERR_OR_ZERO(rt);
77 if (mlx5_lag_is_multipath(mdev) && rt->rt_gw_family != AF_INET)
83 ret = get_route_and_out_devs(priv, rt->dst.dev, route_dev, out_dev);
88 *out_ttl = ip4_dst_hoplimit(&rt->dst);
89 n = dst_neigh_lookup(&rt->dst, &fl4->daddr);
98 static const char *mlx5e_netdev_kind(struct net_device *dev)
100 if (dev->rtnl_link_ops)
101 return dev->rtnl_link_ops->kind;
106 static int mlx5e_route_lookup_ipv6(struct mlx5e_priv *priv,
107 struct net_device *mirred_dev,
108 struct net_device **out_dev,
109 struct net_device **route_dev,
111 struct neighbour **out_n,
114 struct neighbour *n = NULL;
115 struct dst_entry *dst;
117 #if IS_ENABLED(CONFIG_INET) && IS_ENABLED(CONFIG_IPV6)
120 ret = ipv6_stub->ipv6_dst_lookup(dev_net(mirred_dev), NULL, &dst,
126 *out_ttl = ip6_dst_hoplimit(dst);
128 ret = get_route_and_out_devs(priv, dst->dev, route_dev, out_dev);
135 n = dst_neigh_lookup(dst, &fl6->daddr);
144 static int mlx5e_gen_vxlan_header(char buf[], struct ip_tunnel_key *tun_key)
146 __be32 tun_id = tunnel_id_to_key32(tun_key->tun_id);
147 struct udphdr *udp = (struct udphdr *)(buf);
148 struct vxlanhdr *vxh = (struct vxlanhdr *)
149 ((char *)udp + sizeof(struct udphdr));
151 udp->dest = tun_key->tp_dst;
152 vxh->vx_flags = VXLAN_HF_VNI;
153 vxh->vx_vni = vxlan_vni_field(tun_id);
158 static int mlx5e_gen_gre_header(char buf[], struct ip_tunnel_key *tun_key)
160 __be32 tun_id = tunnel_id_to_key32(tun_key->tun_id);
162 struct gre_base_hdr *greh = (struct gre_base_hdr *)(buf);
164 /* the HW does not calculate GRE csum or sequences */
165 if (tun_key->tun_flags & (TUNNEL_CSUM | TUNNEL_SEQ))
168 greh->protocol = htons(ETH_P_TEB);
171 hdr_len = gre_calc_hlen(tun_key->tun_flags);
172 greh->flags = gre_tnl_flags_to_gre_flags(tun_key->tun_flags);
173 if (tun_key->tun_flags & TUNNEL_KEY) {
174 __be32 *ptr = (__be32 *)(((u8 *)greh) + hdr_len - 4);
182 static int mlx5e_gen_ip_tunnel_header(char buf[], __u8 *ip_proto,
183 struct mlx5e_encap_entry *e)
186 struct ip_tunnel_key *key = &e->tun_info.key;
188 if (e->tunnel_type == MLX5E_TC_TUNNEL_TYPE_VXLAN) {
189 *ip_proto = IPPROTO_UDP;
190 err = mlx5e_gen_vxlan_header(buf, key);
191 } else if (e->tunnel_type == MLX5E_TC_TUNNEL_TYPE_GRETAP) {
192 *ip_proto = IPPROTO_GRE;
193 err = mlx5e_gen_gre_header(buf, key);
195 pr_warn("mlx5: Cannot generate tunnel header for tunnel type (%d)\n"
203 static char *gen_eth_tnl_hdr(char *buf, struct net_device *dev,
204 struct mlx5e_encap_entry *e,
207 struct ethhdr *eth = (struct ethhdr *)buf;
210 ether_addr_copy(eth->h_dest, e->h_dest);
211 ether_addr_copy(eth->h_source, dev->dev_addr);
212 if (is_vlan_dev(dev)) {
213 struct vlan_hdr *vlan = (struct vlan_hdr *)
214 ((char *)eth + ETH_HLEN);
215 ip = (char *)vlan + VLAN_HLEN;
216 eth->h_proto = vlan_dev_vlan_proto(dev);
217 vlan->h_vlan_TCI = htons(vlan_dev_vlan_id(dev));
218 vlan->h_vlan_encapsulated_proto = htons(proto);
220 eth->h_proto = htons(proto);
221 ip = (char *)eth + ETH_HLEN;
227 int mlx5e_tc_tun_create_header_ipv4(struct mlx5e_priv *priv,
228 struct net_device *mirred_dev,
229 struct mlx5e_encap_entry *e)
231 int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
232 struct ip_tunnel_key *tun_key = &e->tun_info.key;
233 struct net_device *out_dev, *route_dev;
234 struct neighbour *n = NULL;
235 struct flowi4 fl4 = {};
242 /* add the IP fields */
243 fl4.flowi4_tos = tun_key->tos;
244 fl4.daddr = tun_key->u.ipv4.dst;
245 fl4.saddr = tun_key->u.ipv4.src;
248 err = mlx5e_route_lookup_ipv4(priv, mirred_dev, &out_dev, &route_dev,
254 (is_vlan_dev(route_dev) ? VLAN_ETH_HLEN : ETH_HLEN) +
255 sizeof(struct iphdr) +
258 if (max_encap_size < ipv4_encap_size) {
259 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
260 ipv4_encap_size, max_encap_size);
264 encap_header = kzalloc(ipv4_encap_size, GFP_KERNEL);
268 /* used by mlx5e_detach_encap to lookup a neigh hash table
269 * entry in the neigh hash table when a user deletes a rule
271 e->m_neigh.dev = n->dev;
272 e->m_neigh.family = n->ops->family;
273 memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
274 e->out_dev = out_dev;
275 e->route_dev = route_dev;
277 /* It's important to add the neigh to the hash table before checking
278 * the neigh validity state. So if we'll get a notification, in case the
279 * neigh changes it's validity state, we would find the relevant neigh
282 err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
286 read_lock_bh(&n->lock);
287 nud_state = n->nud_state;
288 ether_addr_copy(e->h_dest, n->ha);
289 read_unlock_bh(&n->lock);
291 /* add ethernet header */
292 ip = (struct iphdr *)gen_eth_tnl_hdr(encap_header, route_dev, e,
296 ip->tos = tun_key->tos;
300 ip->daddr = fl4.daddr;
301 ip->saddr = fl4.saddr;
303 /* add tunneling protocol header */
304 err = mlx5e_gen_ip_tunnel_header((char *)ip + sizeof(struct iphdr),
307 goto destroy_neigh_entry;
309 e->encap_size = ipv4_encap_size;
310 e->encap_header = encap_header;
312 if (!(nud_state & NUD_VALID)) {
313 neigh_event_send(n, NULL);
314 /* the encap entry will be made valid on neigh update event
315 * and not used before that.
320 err = mlx5_packet_reformat_alloc(priv->mdev,
322 ipv4_encap_size, encap_header,
323 MLX5_FLOW_NAMESPACE_FDB,
326 goto destroy_neigh_entry;
328 e->flags |= MLX5_ENCAP_ENTRY_VALID;
329 mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
334 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
343 int mlx5e_tc_tun_create_header_ipv6(struct mlx5e_priv *priv,
344 struct net_device *mirred_dev,
345 struct mlx5e_encap_entry *e)
347 int max_encap_size = MLX5_CAP_ESW(priv->mdev, max_encap_header_size);
348 struct ip_tunnel_key *tun_key = &e->tun_info.key;
349 struct net_device *out_dev, *route_dev;
350 struct neighbour *n = NULL;
351 struct flowi6 fl6 = {};
352 struct ipv6hdr *ip6h;
360 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tun_key->tos), tun_key->label);
361 fl6.daddr = tun_key->u.ipv6.dst;
362 fl6.saddr = tun_key->u.ipv6.src;
364 err = mlx5e_route_lookup_ipv6(priv, mirred_dev, &out_dev, &route_dev,
370 (is_vlan_dev(route_dev) ? VLAN_ETH_HLEN : ETH_HLEN) +
371 sizeof(struct ipv6hdr) +
374 if (max_encap_size < ipv6_encap_size) {
375 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
376 ipv6_encap_size, max_encap_size);
380 encap_header = kzalloc(ipv6_encap_size, GFP_KERNEL);
384 /* used by mlx5e_detach_encap to lookup a neigh hash table
385 * entry in the neigh hash table when a user deletes a rule
387 e->m_neigh.dev = n->dev;
388 e->m_neigh.family = n->ops->family;
389 memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
390 e->out_dev = out_dev;
391 e->route_dev = route_dev;
393 /* It's importent to add the neigh to the hash table before checking
394 * the neigh validity state. So if we'll get a notification, in case the
395 * neigh changes it's validity state, we would find the relevant neigh
398 err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
402 read_lock_bh(&n->lock);
403 nud_state = n->nud_state;
404 ether_addr_copy(e->h_dest, n->ha);
405 read_unlock_bh(&n->lock);
407 /* add ethernet header */
408 ip6h = (struct ipv6hdr *)gen_eth_tnl_hdr(encap_header, route_dev, e,
412 ip6_flow_hdr(ip6h, tun_key->tos, 0);
413 /* the HW fills up ipv6 payload len */
414 ip6h->hop_limit = ttl;
415 ip6h->daddr = fl6.daddr;
416 ip6h->saddr = fl6.saddr;
418 /* add tunneling protocol header */
419 err = mlx5e_gen_ip_tunnel_header((char *)ip6h + sizeof(struct ipv6hdr),
422 goto destroy_neigh_entry;
424 e->encap_size = ipv6_encap_size;
425 e->encap_header = encap_header;
427 if (!(nud_state & NUD_VALID)) {
428 neigh_event_send(n, NULL);
429 /* the encap entry will be made valid on neigh update event
430 * and not used before that.
435 err = mlx5_packet_reformat_alloc(priv->mdev,
437 ipv6_encap_size, encap_header,
438 MLX5_FLOW_NAMESPACE_FDB,
441 goto destroy_neigh_entry;
443 e->flags |= MLX5_ENCAP_ENTRY_VALID;
444 mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
449 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
458 int mlx5e_tc_tun_get_type(struct net_device *tunnel_dev)
460 if (netif_is_vxlan(tunnel_dev))
461 return MLX5E_TC_TUNNEL_TYPE_VXLAN;
462 else if (netif_is_gretap(tunnel_dev) ||
463 netif_is_ip6gretap(tunnel_dev))
464 return MLX5E_TC_TUNNEL_TYPE_GRETAP;
466 return MLX5E_TC_TUNNEL_TYPE_UNKNOWN;
469 bool mlx5e_tc_tun_device_to_offload(struct mlx5e_priv *priv,
470 struct net_device *netdev)
472 int tunnel_type = mlx5e_tc_tun_get_type(netdev);
474 if (tunnel_type == MLX5E_TC_TUNNEL_TYPE_VXLAN &&
475 MLX5_CAP_ESW(priv->mdev, vxlan_encap_decap))
477 else if (tunnel_type == MLX5E_TC_TUNNEL_TYPE_GRETAP &&
478 MLX5_CAP_ESW(priv->mdev, nvgre_encap_decap))
484 int mlx5e_tc_tun_init_encap_attr(struct net_device *tunnel_dev,
485 struct mlx5e_priv *priv,
486 struct mlx5e_encap_entry *e,
487 struct netlink_ext_ack *extack)
489 e->tunnel_type = mlx5e_tc_tun_get_type(tunnel_dev);
491 if (e->tunnel_type == MLX5E_TC_TUNNEL_TYPE_VXLAN) {
492 int dst_port = be16_to_cpu(e->tun_info.key.tp_dst);
494 if (!mlx5_vxlan_lookup_port(priv->mdev->vxlan, dst_port)) {
495 NL_SET_ERR_MSG_MOD(extack,
496 "vxlan udp dport was not registered with the HW");
497 netdev_warn(priv->netdev,
498 "%d isn't an offloaded vxlan udp dport\n",
502 e->reformat_type = MLX5_REFORMAT_TYPE_L2_TO_VXLAN;
503 e->tunnel_hlen = VXLAN_HLEN;
504 } else if (e->tunnel_type == MLX5E_TC_TUNNEL_TYPE_GRETAP) {
505 e->reformat_type = MLX5_REFORMAT_TYPE_L2_TO_NVGRE;
506 e->tunnel_hlen = gre_calc_hlen(e->tun_info.key.tun_flags);
508 e->reformat_type = -1;
515 static int mlx5e_tc_tun_parse_vxlan(struct mlx5e_priv *priv,
516 struct mlx5_flow_spec *spec,
517 struct tc_cls_flower_offload *f,
521 struct flow_rule *rule = tc_cls_flower_offload_flow_rule(f);
522 struct netlink_ext_ack *extack = f->common.extack;
523 void *misc_c = MLX5_ADDR_OF(fte_match_param,
524 spec->match_criteria,
526 void *misc_v = MLX5_ADDR_OF(fte_match_param,
529 struct flow_match_ports enc_ports;
531 flow_rule_match_enc_ports(rule, &enc_ports);
533 /* Full udp dst port must be given */
534 if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_PORTS) ||
535 memchr_inv(&enc_ports.mask->dst, 0xff, sizeof(enc_ports.mask->dst))) {
536 NL_SET_ERR_MSG_MOD(extack,
537 "VXLAN decap filter must include enc_dst_port condition");
538 netdev_warn(priv->netdev,
539 "VXLAN decap filter must include enc_dst_port condition\n");
543 /* udp dst port must be knonwn as a VXLAN port */
544 if (!mlx5_vxlan_lookup_port(priv->mdev->vxlan, be16_to_cpu(enc_ports.key->dst))) {
545 NL_SET_ERR_MSG_MOD(extack,
546 "Matched UDP port is not registered as a VXLAN port");
547 netdev_warn(priv->netdev,
548 "UDP port %d is not registered as a VXLAN port\n",
549 be16_to_cpu(enc_ports.key->dst));
553 /* dst UDP port is valid here */
554 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ip_protocol);
555 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
557 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
558 ntohs(enc_ports.mask->dst));
559 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
560 ntohs(enc_ports.key->dst));
562 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
563 ntohs(enc_ports.mask->src));
564 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
565 ntohs(enc_ports.key->src));
568 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
569 struct flow_match_enc_keyid enc_keyid;
571 flow_rule_match_enc_keyid(rule, &enc_keyid);
573 MLX5_SET(fte_match_set_misc, misc_c, vxlan_vni,
574 be32_to_cpu(enc_keyid.mask->keyid));
575 MLX5_SET(fte_match_set_misc, misc_v, vxlan_vni,
576 be32_to_cpu(enc_keyid.key->keyid));
581 static int mlx5e_tc_tun_parse_gretap(struct mlx5e_priv *priv,
582 struct mlx5_flow_spec *spec,
583 struct tc_cls_flower_offload *f,
584 void *outer_headers_c,
585 void *outer_headers_v)
587 void *misc_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
589 void *misc_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
591 struct flow_rule *rule = tc_cls_flower_offload_flow_rule(f);
593 if (!MLX5_CAP_ESW(priv->mdev, nvgre_encap_decap)) {
594 NL_SET_ERR_MSG_MOD(f->common.extack,
595 "GRE HW offloading is not supported");
596 netdev_warn(priv->netdev, "GRE HW offloading is not supported\n");
600 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol);
601 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
602 ip_protocol, IPPROTO_GRE);
605 MLX5_SET_TO_ONES(fte_match_set_misc, misc_c, gre_protocol);
606 MLX5_SET(fte_match_set_misc, misc_v, gre_protocol, ETH_P_TEB);
609 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
610 struct flow_match_enc_keyid enc_keyid;
612 flow_rule_match_enc_keyid(rule, &enc_keyid);
613 MLX5_SET(fte_match_set_misc, misc_c,
614 gre_key.key, be32_to_cpu(enc_keyid.mask->keyid));
615 MLX5_SET(fte_match_set_misc, misc_v,
616 gre_key.key, be32_to_cpu(enc_keyid.key->keyid));
622 int mlx5e_tc_tun_parse(struct net_device *filter_dev,
623 struct mlx5e_priv *priv,
624 struct mlx5_flow_spec *spec,
625 struct tc_cls_flower_offload *f,
627 void *headers_v, u8 *match_level)
632 tunnel_type = mlx5e_tc_tun_get_type(filter_dev);
633 if (tunnel_type == MLX5E_TC_TUNNEL_TYPE_VXLAN) {
634 *match_level = MLX5_MATCH_L4;
635 err = mlx5e_tc_tun_parse_vxlan(priv, spec, f,
636 headers_c, headers_v);
637 } else if (tunnel_type == MLX5E_TC_TUNNEL_TYPE_GRETAP) {
638 *match_level = MLX5_MATCH_L3;
639 err = mlx5e_tc_tun_parse_gretap(priv, spec, f,
640 headers_c, headers_v);
642 netdev_warn(priv->netdev,
643 "decapsulation offload is not supported for %s (kind: \"%s\")\n",
644 netdev_name(filter_dev),
645 mlx5e_netdev_kind(filter_dev));