1bd3920eaSMaor Gottlieb // SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
2bd3920eaSMaor Gottlieb /*
3bd3920eaSMaor Gottlieb * Copyright (c) 2020 Mellanox Technologies. All rights reserved.
4bd3920eaSMaor Gottlieb */
5bd3920eaSMaor Gottlieb
6bd3920eaSMaor Gottlieb #include <rdma/ib_verbs.h>
7bd3920eaSMaor Gottlieb #include <rdma/ib_cache.h>
8bd3920eaSMaor Gottlieb #include <rdma/lag.h>
9bd3920eaSMaor Gottlieb
rdma_build_skb(struct net_device * netdev,struct rdma_ah_attr * ah_attr,gfp_t flags)10*05195dcbSYunsheng Lin static struct sk_buff *rdma_build_skb(struct net_device *netdev,
11bd3920eaSMaor Gottlieb struct rdma_ah_attr *ah_attr,
12bd3920eaSMaor Gottlieb gfp_t flags)
13bd3920eaSMaor Gottlieb {
14bd3920eaSMaor Gottlieb struct ipv6hdr *ip6h;
15bd3920eaSMaor Gottlieb struct sk_buff *skb;
16bd3920eaSMaor Gottlieb struct ethhdr *eth;
17bd3920eaSMaor Gottlieb struct iphdr *iph;
18bd3920eaSMaor Gottlieb struct udphdr *uh;
19bd3920eaSMaor Gottlieb u8 smac[ETH_ALEN];
20bd3920eaSMaor Gottlieb bool is_ipv4;
21bd3920eaSMaor Gottlieb int hdr_len;
22bd3920eaSMaor Gottlieb
23bd3920eaSMaor Gottlieb is_ipv4 = ipv6_addr_v4mapped((struct in6_addr *)ah_attr->grh.dgid.raw);
24bd3920eaSMaor Gottlieb hdr_len = ETH_HLEN + sizeof(struct udphdr) + LL_RESERVED_SPACE(netdev);
25bd3920eaSMaor Gottlieb hdr_len += is_ipv4 ? sizeof(struct iphdr) : sizeof(struct ipv6hdr);
26bd3920eaSMaor Gottlieb
27bd3920eaSMaor Gottlieb skb = alloc_skb(hdr_len, flags);
28bd3920eaSMaor Gottlieb if (!skb)
29bd3920eaSMaor Gottlieb return NULL;
30bd3920eaSMaor Gottlieb
31bd3920eaSMaor Gottlieb skb->dev = netdev;
32bd3920eaSMaor Gottlieb skb_reserve(skb, hdr_len);
33bd3920eaSMaor Gottlieb skb_push(skb, sizeof(struct udphdr));
34bd3920eaSMaor Gottlieb skb_reset_transport_header(skb);
35bd3920eaSMaor Gottlieb uh = udp_hdr(skb);
369611d53aSMaor Gottlieb uh->source =
379611d53aSMaor Gottlieb htons(rdma_flow_label_to_udp_sport(ah_attr->grh.flow_label));
38bd3920eaSMaor Gottlieb uh->dest = htons(ROCE_V2_UDP_DPORT);
39bd3920eaSMaor Gottlieb uh->len = htons(sizeof(struct udphdr));
40bd3920eaSMaor Gottlieb
41bd3920eaSMaor Gottlieb if (is_ipv4) {
42bd3920eaSMaor Gottlieb skb_push(skb, sizeof(struct iphdr));
43bd3920eaSMaor Gottlieb skb_reset_network_header(skb);
44bd3920eaSMaor Gottlieb iph = ip_hdr(skb);
45bd3920eaSMaor Gottlieb iph->frag_off = 0;
46bd3920eaSMaor Gottlieb iph->version = 4;
47bd3920eaSMaor Gottlieb iph->protocol = IPPROTO_UDP;
48bd3920eaSMaor Gottlieb iph->ihl = 0x5;
49bd3920eaSMaor Gottlieb iph->tot_len = htons(sizeof(struct udphdr) + sizeof(struct
50bd3920eaSMaor Gottlieb iphdr));
51bd3920eaSMaor Gottlieb memcpy(&iph->saddr, ah_attr->grh.sgid_attr->gid.raw + 12,
52bd3920eaSMaor Gottlieb sizeof(struct in_addr));
53bd3920eaSMaor Gottlieb memcpy(&iph->daddr, ah_attr->grh.dgid.raw + 12,
54bd3920eaSMaor Gottlieb sizeof(struct in_addr));
55bd3920eaSMaor Gottlieb } else {
56bd3920eaSMaor Gottlieb skb_push(skb, sizeof(struct ipv6hdr));
57bd3920eaSMaor Gottlieb skb_reset_network_header(skb);
58bd3920eaSMaor Gottlieb ip6h = ipv6_hdr(skb);
59bd3920eaSMaor Gottlieb ip6h->version = 6;
60bd3920eaSMaor Gottlieb ip6h->nexthdr = IPPROTO_UDP;
61bd3920eaSMaor Gottlieb memcpy(&ip6h->flow_lbl, &ah_attr->grh.flow_label,
62bd3920eaSMaor Gottlieb sizeof(*ip6h->flow_lbl));
63bd3920eaSMaor Gottlieb memcpy(&ip6h->saddr, ah_attr->grh.sgid_attr->gid.raw,
64bd3920eaSMaor Gottlieb sizeof(struct in6_addr));
65bd3920eaSMaor Gottlieb memcpy(&ip6h->daddr, ah_attr->grh.dgid.raw,
66bd3920eaSMaor Gottlieb sizeof(struct in6_addr));
67bd3920eaSMaor Gottlieb }
68bd3920eaSMaor Gottlieb
69bd3920eaSMaor Gottlieb skb_push(skb, sizeof(struct ethhdr));
70bd3920eaSMaor Gottlieb skb_reset_mac_header(skb);
71bd3920eaSMaor Gottlieb eth = eth_hdr(skb);
72bd3920eaSMaor Gottlieb skb->protocol = eth->h_proto = htons(is_ipv4 ? ETH_P_IP : ETH_P_IPV6);
73bd3920eaSMaor Gottlieb rdma_read_gid_l2_fields(ah_attr->grh.sgid_attr, NULL, smac);
74bd3920eaSMaor Gottlieb memcpy(eth->h_source, smac, ETH_ALEN);
75bd3920eaSMaor Gottlieb memcpy(eth->h_dest, ah_attr->roce.dmac, ETH_ALEN);
76bd3920eaSMaor Gottlieb
77bd3920eaSMaor Gottlieb return skb;
78bd3920eaSMaor Gottlieb }
79bd3920eaSMaor Gottlieb
rdma_get_xmit_slave_udp(struct ib_device * device,struct net_device * master,struct rdma_ah_attr * ah_attr,gfp_t flags)80bd3920eaSMaor Gottlieb static struct net_device *rdma_get_xmit_slave_udp(struct ib_device *device,
81bd3920eaSMaor Gottlieb struct net_device *master,
82bd3920eaSMaor Gottlieb struct rdma_ah_attr *ah_attr,
83bd3920eaSMaor Gottlieb gfp_t flags)
84bd3920eaSMaor Gottlieb {
85bd3920eaSMaor Gottlieb struct net_device *slave;
86bd3920eaSMaor Gottlieb struct sk_buff *skb;
87bd3920eaSMaor Gottlieb
88*05195dcbSYunsheng Lin skb = rdma_build_skb(master, ah_attr, flags);
89bd3920eaSMaor Gottlieb if (!skb)
90bd3920eaSMaor Gottlieb return ERR_PTR(-ENOMEM);
91bd3920eaSMaor Gottlieb
92bd3920eaSMaor Gottlieb rcu_read_lock();
93bd3920eaSMaor Gottlieb slave = netdev_get_xmit_slave(master, skb,
94bd3920eaSMaor Gottlieb !!(device->lag_flags &
95bd3920eaSMaor Gottlieb RDMA_LAG_FLAGS_HASH_ALL_SLAVES));
96bd3920eaSMaor Gottlieb dev_hold(slave);
97bd3920eaSMaor Gottlieb rcu_read_unlock();
98bd3920eaSMaor Gottlieb kfree_skb(skb);
99bd3920eaSMaor Gottlieb return slave;
100bd3920eaSMaor Gottlieb }
101bd3920eaSMaor Gottlieb
rdma_lag_put_ah_roce_slave(struct net_device * xmit_slave)102bd3920eaSMaor Gottlieb void rdma_lag_put_ah_roce_slave(struct net_device *xmit_slave)
103bd3920eaSMaor Gottlieb {
104bd3920eaSMaor Gottlieb if (xmit_slave)
105bd3920eaSMaor Gottlieb dev_put(xmit_slave);
106bd3920eaSMaor Gottlieb }
107bd3920eaSMaor Gottlieb
rdma_lag_get_ah_roce_slave(struct ib_device * device,struct rdma_ah_attr * ah_attr,gfp_t flags)108bd3920eaSMaor Gottlieb struct net_device *rdma_lag_get_ah_roce_slave(struct ib_device *device,
109bd3920eaSMaor Gottlieb struct rdma_ah_attr *ah_attr,
110bd3920eaSMaor Gottlieb gfp_t flags)
111bd3920eaSMaor Gottlieb {
112bd3920eaSMaor Gottlieb struct net_device *slave = NULL;
113bd3920eaSMaor Gottlieb struct net_device *master;
114bd3920eaSMaor Gottlieb
115bd3920eaSMaor Gottlieb if (!(ah_attr->type == RDMA_AH_ATTR_TYPE_ROCE &&
1169611d53aSMaor Gottlieb ah_attr->grh.sgid_attr->gid_type == IB_GID_TYPE_ROCE_UDP_ENCAP &&
1179611d53aSMaor Gottlieb ah_attr->grh.flow_label))
118bd3920eaSMaor Gottlieb return NULL;
119bd3920eaSMaor Gottlieb
120bd3920eaSMaor Gottlieb rcu_read_lock();
121bd3920eaSMaor Gottlieb master = rdma_read_gid_attr_ndev_rcu(ah_attr->grh.sgid_attr);
122bd3920eaSMaor Gottlieb if (IS_ERR(master)) {
123bd3920eaSMaor Gottlieb rcu_read_unlock();
124bd3920eaSMaor Gottlieb return master;
125bd3920eaSMaor Gottlieb }
126bd3920eaSMaor Gottlieb dev_hold(master);
127bd3920eaSMaor Gottlieb rcu_read_unlock();
128bd3920eaSMaor Gottlieb
129bd3920eaSMaor Gottlieb if (!netif_is_bond_master(master))
130bd3920eaSMaor Gottlieb goto put;
131bd3920eaSMaor Gottlieb
132bd3920eaSMaor Gottlieb slave = rdma_get_xmit_slave_udp(device, master, ah_attr, flags);
133bd3920eaSMaor Gottlieb put:
134bd3920eaSMaor Gottlieb dev_put(master);
135bd3920eaSMaor Gottlieb return slave;
136bd3920eaSMaor Gottlieb }
137