1 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
2 /*
3 * Copyright (c) 2005 Voltaire Inc. All rights reserved.
4 * Copyright (c) 2005 Intel Corporation. All rights reserved.
5 */
6
7 #ifndef IB_ADDR_H
8 #define IB_ADDR_H
9
10 #include <linux/in.h>
11 #include <linux/in6.h>
12 #include <linux/if_arp.h>
13 #include <linux/netdevice.h>
14 #include <linux/inetdevice.h>
15 #include <linux/socket.h>
16 #include <linux/if_vlan.h>
17 #include <net/ipv6.h>
18 #include <net/if_inet6.h>
19 #include <net/ip.h>
20 #include <rdma/ib_verbs.h>
21 #include <rdma/ib_pack.h>
22 #include <net/net_namespace.h>
23
24 /**
25 * struct rdma_dev_addr - Contains resolved RDMA hardware addresses
26 * @src_dev_addr: Source MAC address.
27 * @dst_dev_addr: Destination MAC address.
28 * @broadcast: Broadcast address of the device.
29 * @dev_type: The interface hardware type of the device.
30 * @bound_dev_if: An optional device interface index.
31 * @transport: The transport type used.
32 * @net: Network namespace containing the bound_dev_if net_dev.
33 * @sgid_attr: GID attribute to use for identified SGID
34 */
35 struct rdma_dev_addr {
36 unsigned char src_dev_addr[MAX_ADDR_LEN];
37 unsigned char dst_dev_addr[MAX_ADDR_LEN];
38 unsigned char broadcast[MAX_ADDR_LEN];
39 unsigned short dev_type;
40 int bound_dev_if;
41 enum rdma_transport_type transport;
42 struct net *net;
43 const struct ib_gid_attr *sgid_attr;
44 enum rdma_network_type network;
45 int hoplimit;
46 };
47
48 /**
49 * rdma_translate_ip - Translate a local IP address to an RDMA hardware
50 * address.
51 *
52 * The dev_addr->net field must be initialized.
53 */
54 int rdma_translate_ip(const struct sockaddr *addr,
55 struct rdma_dev_addr *dev_addr);
56
57 /**
58 * rdma_resolve_ip - Resolve source and destination IP addresses to
59 * RDMA hardware addresses.
60 * @src_addr: An optional source address to use in the resolution. If a
61 * source address is not provided, a usable address will be returned via
62 * the callback.
63 * @dst_addr: The destination address to resolve.
64 * @addr: A reference to a data location that will receive the resolved
65 * addresses. The data location must remain valid until the callback has
66 * been invoked. The net field of the addr struct must be valid.
67 * @timeout_ms: Amount of time to wait for the address resolution to complete.
68 * @callback: Call invoked once address resolution has completed, timed out,
69 * or been canceled. A status of 0 indicates success.
70 * @resolve_by_gid_attr: Resolve the ip based on the GID attribute from
71 * rdma_dev_addr.
72 * @context: User-specified context associated with the call.
73 */
74 int rdma_resolve_ip(struct sockaddr *src_addr, const struct sockaddr *dst_addr,
75 struct rdma_dev_addr *addr, unsigned long timeout_ms,
76 void (*callback)(int status, struct sockaddr *src_addr,
77 struct rdma_dev_addr *addr, void *context),
78 bool resolve_by_gid_attr, void *context);
79
80 void rdma_addr_cancel(struct rdma_dev_addr *addr);
81
82 int rdma_addr_size(const struct sockaddr *addr);
83 int rdma_addr_size_in6(struct sockaddr_in6 *addr);
84 int rdma_addr_size_kss(struct __kernel_sockaddr_storage *addr);
85
ib_addr_get_pkey(struct rdma_dev_addr * dev_addr)86 static inline u16 ib_addr_get_pkey(struct rdma_dev_addr *dev_addr)
87 {
88 return ((u16)dev_addr->broadcast[8] << 8) | (u16)dev_addr->broadcast[9];
89 }
90
ib_addr_set_pkey(struct rdma_dev_addr * dev_addr,u16 pkey)91 static inline void ib_addr_set_pkey(struct rdma_dev_addr *dev_addr, u16 pkey)
92 {
93 dev_addr->broadcast[8] = pkey >> 8;
94 dev_addr->broadcast[9] = (unsigned char) pkey;
95 }
96
ib_addr_get_mgid(struct rdma_dev_addr * dev_addr,union ib_gid * gid)97 static inline void ib_addr_get_mgid(struct rdma_dev_addr *dev_addr,
98 union ib_gid *gid)
99 {
100 memcpy(gid, dev_addr->broadcast + 4, sizeof *gid);
101 }
102
rdma_addr_gid_offset(struct rdma_dev_addr * dev_addr)103 static inline int rdma_addr_gid_offset(struct rdma_dev_addr *dev_addr)
104 {
105 return dev_addr->dev_type == ARPHRD_INFINIBAND ? 4 : 0;
106 }
107
rdma_vlan_dev_vlan_id(const struct net_device * dev)108 static inline u16 rdma_vlan_dev_vlan_id(const struct net_device *dev)
109 {
110 return is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 0xffff;
111 }
112
rdma_ip2gid(struct sockaddr * addr,union ib_gid * gid)113 static inline int rdma_ip2gid(struct sockaddr *addr, union ib_gid *gid)
114 {
115 switch (addr->sa_family) {
116 case AF_INET:
117 ipv6_addr_set_v4mapped(((struct sockaddr_in *)
118 addr)->sin_addr.s_addr,
119 (struct in6_addr *)gid);
120 break;
121 case AF_INET6:
122 *(struct in6_addr *)&gid->raw =
123 ((struct sockaddr_in6 *)addr)->sin6_addr;
124 break;
125 default:
126 return -EINVAL;
127 }
128 return 0;
129 }
130
131 /* Important - sockaddr should be a union of sockaddr_in and sockaddr_in6 */
rdma_gid2ip(struct sockaddr * out,const union ib_gid * gid)132 static inline void rdma_gid2ip(struct sockaddr *out, const union ib_gid *gid)
133 {
134 if (ipv6_addr_v4mapped((struct in6_addr *)gid)) {
135 struct sockaddr_in *out_in = (struct sockaddr_in *)out;
136 memset(out_in, 0, sizeof(*out_in));
137 out_in->sin_family = AF_INET;
138 memcpy(&out_in->sin_addr.s_addr, gid->raw + 12, 4);
139 } else {
140 struct sockaddr_in6 *out_in = (struct sockaddr_in6 *)out;
141 memset(out_in, 0, sizeof(*out_in));
142 out_in->sin6_family = AF_INET6;
143 memcpy(&out_in->sin6_addr.s6_addr, gid->raw, 16);
144 }
145 }
146
147 /*
148 * rdma_get/set_sgid/dgid() APIs are applicable to IB, and iWarp.
149 * They are not applicable to RoCE.
150 * RoCE GIDs are derived from the IP addresses.
151 */
rdma_addr_get_sgid(struct rdma_dev_addr * dev_addr,union ib_gid * gid)152 static inline void rdma_addr_get_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
153 {
154 memcpy(gid, dev_addr->src_dev_addr + rdma_addr_gid_offset(dev_addr),
155 sizeof(*gid));
156 }
157
rdma_addr_set_sgid(struct rdma_dev_addr * dev_addr,union ib_gid * gid)158 static inline void rdma_addr_set_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
159 {
160 memcpy(dev_addr->src_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
161 }
162
rdma_addr_get_dgid(struct rdma_dev_addr * dev_addr,union ib_gid * gid)163 static inline void rdma_addr_get_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
164 {
165 memcpy(gid, dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), sizeof *gid);
166 }
167
rdma_addr_set_dgid(struct rdma_dev_addr * dev_addr,union ib_gid * gid)168 static inline void rdma_addr_set_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
169 {
170 memcpy(dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
171 }
172
iboe_get_mtu(int mtu)173 static inline enum ib_mtu iboe_get_mtu(int mtu)
174 {
175 /*
176 * Reduce IB headers from effective IBoE MTU.
177 */
178 mtu = mtu - (IB_GRH_BYTES + IB_UDP_BYTES + IB_BTH_BYTES +
179 IB_EXT_XRC_BYTES + IB_EXT_ATOMICETH_BYTES +
180 IB_ICRC_BYTES);
181
182 if (mtu >= ib_mtu_enum_to_int(IB_MTU_4096))
183 return IB_MTU_4096;
184 else if (mtu >= ib_mtu_enum_to_int(IB_MTU_2048))
185 return IB_MTU_2048;
186 else if (mtu >= ib_mtu_enum_to_int(IB_MTU_1024))
187 return IB_MTU_1024;
188 else if (mtu >= ib_mtu_enum_to_int(IB_MTU_512))
189 return IB_MTU_512;
190 else if (mtu >= ib_mtu_enum_to_int(IB_MTU_256))
191 return IB_MTU_256;
192 else
193 return 0;
194 }
195
rdma_link_local_addr(struct in6_addr * addr)196 static inline int rdma_link_local_addr(struct in6_addr *addr)
197 {
198 if (addr->s6_addr32[0] == htonl(0xfe800000) &&
199 addr->s6_addr32[1] == 0)
200 return 1;
201
202 return 0;
203 }
204
rdma_get_ll_mac(struct in6_addr * addr,u8 * mac)205 static inline void rdma_get_ll_mac(struct in6_addr *addr, u8 *mac)
206 {
207 memcpy(mac, &addr->s6_addr[8], 3);
208 memcpy(mac + 3, &addr->s6_addr[13], 3);
209 mac[0] ^= 2;
210 }
211
rdma_is_multicast_addr(struct in6_addr * addr)212 static inline int rdma_is_multicast_addr(struct in6_addr *addr)
213 {
214 __be32 ipv4_addr;
215
216 if (addr->s6_addr[0] == 0xff)
217 return 1;
218
219 ipv4_addr = addr->s6_addr32[3];
220 return (ipv6_addr_v4mapped(addr) && ipv4_is_multicast(ipv4_addr));
221 }
222
rdma_get_mcast_mac(struct in6_addr * addr,u8 * mac)223 static inline void rdma_get_mcast_mac(struct in6_addr *addr, u8 *mac)
224 {
225 int i;
226
227 mac[0] = 0x33;
228 mac[1] = 0x33;
229 for (i = 2; i < 6; ++i)
230 mac[i] = addr->s6_addr[i + 10];
231 }
232
rdma_get_vlan_id(union ib_gid * dgid)233 static inline u16 rdma_get_vlan_id(union ib_gid *dgid)
234 {
235 u16 vid;
236
237 vid = dgid->raw[11] << 8 | dgid->raw[12];
238 return vid < 0x1000 ? vid : 0xffff;
239 }
240
rdma_vlan_dev_real_dev(const struct net_device * dev)241 static inline struct net_device *rdma_vlan_dev_real_dev(const struct net_device *dev)
242 {
243 return is_vlan_dev(dev) ? vlan_dev_real_dev(dev) : NULL;
244 }
245
246 #endif /* IB_ADDR_H */
247