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;
45 static int mlx5e_route_lookup_ipv4(struct mlx5e_priv *priv,
46 struct net_device *mirred_dev,
47 struct net_device **out_dev,
48 struct net_device **route_dev,
50 struct neighbour **out_n,
54 struct neighbour *n = NULL;
56 #if IS_ENABLED(CONFIG_INET)
57 struct mlx5_core_dev *mdev = priv->mdev;
58 struct net_device *uplink_dev;
61 if (mlx5_lag_is_multipath(mdev)) {
62 struct mlx5_eswitch *esw = mdev->priv.eswitch;
64 uplink_dev = mlx5_eswitch_uplink_get_proto_dev(esw, REP_ETH);
65 fl4->flowi4_oif = uplink_dev->ifindex;
68 rt = ip_route_output_key(dev_net(mirred_dev), fl4);
69 ret = PTR_ERR_OR_ZERO(rt);
73 if (mlx5_lag_is_multipath(mdev) && !rt->rt_gateway)
79 ret = get_route_and_out_devs(priv, rt->dst.dev, route_dev, out_dev);
84 *out_ttl = ip4_dst_hoplimit(&rt->dst);
85 n = dst_neigh_lookup(&rt->dst, &fl4->daddr);
94 static const char *mlx5e_netdev_kind(struct net_device *dev)
96 if (dev->rtnl_link_ops)
97 return dev->rtnl_link_ops->kind;
102 static int mlx5e_route_lookup_ipv6(struct mlx5e_priv *priv,
103 struct net_device *mirred_dev,
104 struct net_device **out_dev,
105 struct net_device **route_dev,
107 struct neighbour **out_n,
110 struct neighbour *n = NULL;
111 struct dst_entry *dst;
113 #if IS_ENABLED(CONFIG_INET) && IS_ENABLED(CONFIG_IPV6)
116 ret = ipv6_stub->ipv6_dst_lookup(dev_net(mirred_dev), NULL, &dst,
122 *out_ttl = ip6_dst_hoplimit(dst);
124 ret = get_route_and_out_devs(priv, dst->dev, route_dev, out_dev);
131 n = dst_neigh_lookup(dst, &fl6->daddr);
140 static int mlx5e_gen_vxlan_header(char buf[], struct ip_tunnel_key *tun_key)
142 __be32 tun_id = tunnel_id_to_key32(tun_key->tun_id);
143 struct udphdr *udp = (struct udphdr *)(buf);
144 struct vxlanhdr *vxh = (struct vxlanhdr *)
145 ((char *)udp + sizeof(struct udphdr));
147 udp->dest = tun_key->tp_dst;
148 vxh->vx_flags = VXLAN_HF_VNI;
149 vxh->vx_vni = vxlan_vni_field(tun_id);
154 static int mlx5e_gen_gre_header(char buf[], struct ip_tunnel_key *tun_key)
156 __be32 tun_id = tunnel_id_to_key32(tun_key->tun_id);
158 struct gre_base_hdr *greh = (struct gre_base_hdr *)(buf);
160 /* the HW does not calculate GRE csum or sequences */
161 if (tun_key->tun_flags & (TUNNEL_CSUM | TUNNEL_SEQ))
164 greh->protocol = htons(ETH_P_TEB);
167 hdr_len = gre_calc_hlen(tun_key->tun_flags);
168 greh->flags = gre_tnl_flags_to_gre_flags(tun_key->tun_flags);
169 if (tun_key->tun_flags & TUNNEL_KEY) {
170 __be32 *ptr = (__be32 *)(((u8 *)greh) + hdr_len - 4);
178 static int mlx5e_gen_ip_tunnel_header(char buf[], __u8 *ip_proto,
179 struct mlx5e_encap_entry *e)
182 struct ip_tunnel_key *key = &e->tun_info.key;
184 if (e->tunnel_type == MLX5E_TC_TUNNEL_TYPE_VXLAN) {
185 *ip_proto = IPPROTO_UDP;
186 err = mlx5e_gen_vxlan_header(buf, key);
187 } else if (e->tunnel_type == MLX5E_TC_TUNNEL_TYPE_GRETAP) {
188 *ip_proto = IPPROTO_GRE;
189 err = mlx5e_gen_gre_header(buf, key);
191 pr_warn("mlx5: Cannot generate tunnel header for tunnel type (%d)\n"
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 struct ip_tunnel_key *tun_key = &e->tun_info.key;
229 struct net_device *out_dev, *route_dev;
230 struct neighbour *n = NULL;
231 struct flowi4 fl4 = {};
238 /* add the IP fields */
239 fl4.flowi4_tos = tun_key->tos;
240 fl4.daddr = tun_key->u.ipv4.dst;
241 fl4.saddr = tun_key->u.ipv4.src;
244 err = mlx5e_route_lookup_ipv4(priv, mirred_dev, &out_dev, &route_dev,
250 (is_vlan_dev(route_dev) ? VLAN_ETH_HLEN : ETH_HLEN) +
251 sizeof(struct iphdr) +
254 if (max_encap_size < ipv4_encap_size) {
255 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
256 ipv4_encap_size, max_encap_size);
260 encap_header = kzalloc(ipv4_encap_size, GFP_KERNEL);
264 /* used by mlx5e_detach_encap to lookup a neigh hash table
265 * entry in the neigh hash table when a user deletes a rule
267 e->m_neigh.dev = n->dev;
268 e->m_neigh.family = n->ops->family;
269 memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
270 e->out_dev = out_dev;
271 e->route_dev = route_dev;
273 /* It's important to add the neigh to the hash table before checking
274 * the neigh validity state. So if we'll get a notification, in case the
275 * neigh changes it's validity state, we would find the relevant neigh
278 err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
282 read_lock_bh(&n->lock);
283 nud_state = n->nud_state;
284 ether_addr_copy(e->h_dest, n->ha);
285 read_unlock_bh(&n->lock);
287 /* add ethernet header */
288 ip = (struct iphdr *)gen_eth_tnl_hdr(encap_header, route_dev, e,
292 ip->tos = tun_key->tos;
296 ip->daddr = fl4.daddr;
297 ip->saddr = fl4.saddr;
299 /* add tunneling protocol header */
300 err = mlx5e_gen_ip_tunnel_header((char *)ip + sizeof(struct iphdr),
303 goto destroy_neigh_entry;
305 e->encap_size = ipv4_encap_size;
306 e->encap_header = encap_header;
308 if (!(nud_state & NUD_VALID)) {
309 neigh_event_send(n, NULL);
310 /* the encap entry will be made valid on neigh update event
311 * and not used before that.
316 err = mlx5_packet_reformat_alloc(priv->mdev,
318 ipv4_encap_size, encap_header,
319 MLX5_FLOW_NAMESPACE_FDB,
322 goto destroy_neigh_entry;
324 e->flags |= MLX5_ENCAP_ENTRY_VALID;
325 mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
330 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
339 int mlx5e_tc_tun_create_header_ipv6(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 struct ip_tunnel_key *tun_key = &e->tun_info.key;
345 struct net_device *out_dev, *route_dev;
346 struct neighbour *n = NULL;
347 struct flowi6 fl6 = {};
348 struct ipv6hdr *ip6h;
356 fl6.flowlabel = ip6_make_flowinfo(RT_TOS(tun_key->tos), tun_key->label);
357 fl6.daddr = tun_key->u.ipv6.dst;
358 fl6.saddr = tun_key->u.ipv6.src;
360 err = mlx5e_route_lookup_ipv6(priv, mirred_dev, &out_dev, &route_dev,
366 (is_vlan_dev(route_dev) ? VLAN_ETH_HLEN : ETH_HLEN) +
367 sizeof(struct ipv6hdr) +
370 if (max_encap_size < ipv6_encap_size) {
371 mlx5_core_warn(priv->mdev, "encap size %d too big, max supported is %d\n",
372 ipv6_encap_size, max_encap_size);
376 encap_header = kzalloc(ipv6_encap_size, GFP_KERNEL);
380 /* used by mlx5e_detach_encap to lookup a neigh hash table
381 * entry in the neigh hash table when a user deletes a rule
383 e->m_neigh.dev = n->dev;
384 e->m_neigh.family = n->ops->family;
385 memcpy(&e->m_neigh.dst_ip, n->primary_key, n->tbl->key_len);
386 e->out_dev = out_dev;
387 e->route_dev = route_dev;
389 /* It's importent to add the neigh to the hash table before checking
390 * the neigh validity state. So if we'll get a notification, in case the
391 * neigh changes it's validity state, we would find the relevant neigh
394 err = mlx5e_rep_encap_entry_attach(netdev_priv(out_dev), e);
398 read_lock_bh(&n->lock);
399 nud_state = n->nud_state;
400 ether_addr_copy(e->h_dest, n->ha);
401 read_unlock_bh(&n->lock);
403 /* add ethernet header */
404 ip6h = (struct ipv6hdr *)gen_eth_tnl_hdr(encap_header, route_dev, e,
408 ip6_flow_hdr(ip6h, tun_key->tos, 0);
409 /* the HW fills up ipv6 payload len */
410 ip6h->hop_limit = ttl;
411 ip6h->daddr = fl6.daddr;
412 ip6h->saddr = fl6.saddr;
414 /* add tunneling protocol header */
415 err = mlx5e_gen_ip_tunnel_header((char *)ip6h + sizeof(struct ipv6hdr),
418 goto destroy_neigh_entry;
420 e->encap_size = ipv6_encap_size;
421 e->encap_header = encap_header;
423 if (!(nud_state & NUD_VALID)) {
424 neigh_event_send(n, NULL);
425 /* the encap entry will be made valid on neigh update event
426 * and not used before that.
431 err = mlx5_packet_reformat_alloc(priv->mdev,
433 ipv6_encap_size, encap_header,
434 MLX5_FLOW_NAMESPACE_FDB,
437 goto destroy_neigh_entry;
439 e->flags |= MLX5_ENCAP_ENTRY_VALID;
440 mlx5e_rep_queue_neigh_stats_work(netdev_priv(out_dev));
445 mlx5e_rep_encap_entry_detach(netdev_priv(e->out_dev), e);
454 int mlx5e_tc_tun_get_type(struct net_device *tunnel_dev)
456 if (netif_is_vxlan(tunnel_dev))
457 return MLX5E_TC_TUNNEL_TYPE_VXLAN;
458 else if (netif_is_gretap(tunnel_dev) ||
459 netif_is_ip6gretap(tunnel_dev))
460 return MLX5E_TC_TUNNEL_TYPE_GRETAP;
462 return MLX5E_TC_TUNNEL_TYPE_UNKNOWN;
465 bool mlx5e_tc_tun_device_to_offload(struct mlx5e_priv *priv,
466 struct net_device *netdev)
468 int tunnel_type = mlx5e_tc_tun_get_type(netdev);
470 if (tunnel_type == MLX5E_TC_TUNNEL_TYPE_VXLAN &&
471 MLX5_CAP_ESW(priv->mdev, vxlan_encap_decap))
473 else if (tunnel_type == MLX5E_TC_TUNNEL_TYPE_GRETAP &&
474 MLX5_CAP_ESW(priv->mdev, nvgre_encap_decap))
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 e->tunnel_type = mlx5e_tc_tun_get_type(tunnel_dev);
487 if (e->tunnel_type == MLX5E_TC_TUNNEL_TYPE_VXLAN) {
488 int dst_port = be16_to_cpu(e->tun_info.key.tp_dst);
490 if (!mlx5_vxlan_lookup_port(priv->mdev->vxlan, dst_port)) {
491 NL_SET_ERR_MSG_MOD(extack,
492 "vxlan udp dport was not registered with the HW");
493 netdev_warn(priv->netdev,
494 "%d isn't an offloaded vxlan udp dport\n",
498 e->reformat_type = MLX5_REFORMAT_TYPE_L2_TO_VXLAN;
499 e->tunnel_hlen = VXLAN_HLEN;
500 } else if (e->tunnel_type == MLX5E_TC_TUNNEL_TYPE_GRETAP) {
501 e->reformat_type = MLX5_REFORMAT_TYPE_L2_TO_NVGRE;
502 e->tunnel_hlen = gre_calc_hlen(e->tun_info.key.tun_flags);
504 e->reformat_type = -1;
511 static int mlx5e_tc_tun_parse_vxlan(struct mlx5e_priv *priv,
512 struct mlx5_flow_spec *spec,
513 struct tc_cls_flower_offload *f,
517 struct flow_rule *rule = tc_cls_flower_offload_flow_rule(f);
518 struct netlink_ext_ack *extack = f->common.extack;
519 void *misc_c = MLX5_ADDR_OF(fte_match_param,
520 spec->match_criteria,
522 void *misc_v = MLX5_ADDR_OF(fte_match_param,
525 struct flow_match_ports enc_ports;
527 flow_rule_match_enc_ports(rule, &enc_ports);
529 /* Full udp dst port must be given */
530 if (!flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_PORTS) ||
531 memchr_inv(&enc_ports.mask->dst, 0xff, sizeof(enc_ports.mask->dst))) {
532 NL_SET_ERR_MSG_MOD(extack,
533 "VXLAN decap filter must include enc_dst_port condition");
534 netdev_warn(priv->netdev,
535 "VXLAN decap filter must include enc_dst_port condition\n");
539 /* udp dst port must be knonwn as a VXLAN port */
540 if (!mlx5_vxlan_lookup_port(priv->mdev->vxlan, be16_to_cpu(enc_ports.key->dst))) {
541 NL_SET_ERR_MSG_MOD(extack,
542 "Matched UDP port is not registered as a VXLAN port");
543 netdev_warn(priv->netdev,
544 "UDP port %d is not registered as a VXLAN port\n",
545 be16_to_cpu(enc_ports.key->dst));
549 /* dst UDP port is valid here */
550 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, headers_c, ip_protocol);
551 MLX5_SET(fte_match_set_lyr_2_4, headers_v, ip_protocol, IPPROTO_UDP);
553 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_dport,
554 ntohs(enc_ports.mask->dst));
555 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_dport,
556 ntohs(enc_ports.key->dst));
558 MLX5_SET(fte_match_set_lyr_2_4, headers_c, udp_sport,
559 ntohs(enc_ports.mask->src));
560 MLX5_SET(fte_match_set_lyr_2_4, headers_v, udp_sport,
561 ntohs(enc_ports.key->src));
564 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
565 struct flow_match_enc_keyid enc_keyid;
567 flow_rule_match_enc_keyid(rule, &enc_keyid);
569 MLX5_SET(fte_match_set_misc, misc_c, vxlan_vni,
570 be32_to_cpu(enc_keyid.mask->keyid));
571 MLX5_SET(fte_match_set_misc, misc_v, vxlan_vni,
572 be32_to_cpu(enc_keyid.key->keyid));
577 static int mlx5e_tc_tun_parse_gretap(struct mlx5e_priv *priv,
578 struct mlx5_flow_spec *spec,
579 struct tc_cls_flower_offload *f,
580 void *outer_headers_c,
581 void *outer_headers_v)
583 void *misc_c = MLX5_ADDR_OF(fte_match_param, spec->match_criteria,
585 void *misc_v = MLX5_ADDR_OF(fte_match_param, spec->match_value,
587 struct flow_rule *rule = tc_cls_flower_offload_flow_rule(f);
589 if (!MLX5_CAP_ESW(priv->mdev, nvgre_encap_decap)) {
590 NL_SET_ERR_MSG_MOD(f->common.extack,
591 "GRE HW offloading is not supported");
592 netdev_warn(priv->netdev, "GRE HW offloading is not supported\n");
596 MLX5_SET_TO_ONES(fte_match_set_lyr_2_4, outer_headers_c, ip_protocol);
597 MLX5_SET(fte_match_set_lyr_2_4, outer_headers_v,
598 ip_protocol, IPPROTO_GRE);
601 MLX5_SET_TO_ONES(fte_match_set_misc, misc_c, gre_protocol);
602 MLX5_SET(fte_match_set_misc, misc_v, gre_protocol, ETH_P_TEB);
605 if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
606 struct flow_match_enc_keyid enc_keyid;
608 flow_rule_match_enc_keyid(rule, &enc_keyid);
609 MLX5_SET(fte_match_set_misc, misc_c,
610 gre_key.key, be32_to_cpu(enc_keyid.mask->keyid));
611 MLX5_SET(fte_match_set_misc, misc_v,
612 gre_key.key, be32_to_cpu(enc_keyid.key->keyid));
618 int mlx5e_tc_tun_parse(struct net_device *filter_dev,
619 struct mlx5e_priv *priv,
620 struct mlx5_flow_spec *spec,
621 struct tc_cls_flower_offload *f,
623 void *headers_v, u8 *match_level)
628 tunnel_type = mlx5e_tc_tun_get_type(filter_dev);
629 if (tunnel_type == MLX5E_TC_TUNNEL_TYPE_VXLAN) {
630 *match_level = MLX5_MATCH_L4;
631 err = mlx5e_tc_tun_parse_vxlan(priv, spec, f,
632 headers_c, headers_v);
633 } else if (tunnel_type == MLX5E_TC_TUNNEL_TYPE_GRETAP) {
634 *match_level = MLX5_MATCH_L3;
635 err = mlx5e_tc_tun_parse_gretap(priv, spec, f,
636 headers_c, headers_v);
638 netdev_warn(priv->netdev,
639 "decapsulation offload is not supported for %s net device (%d)\n",
640 mlx5e_netdev_kind(filter_dev), tunnel_type);