xref: /freebsd/sys/ofed/include/rdma/ib_addr.h (revision 8ac2952539365cb63f95336536ee16c5e4b4dc8b)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
5  * Copyright (c) 2005 Intel Corporation.  All rights reserved.
6  *
7  * This software is available to you under a choice of one of two
8  * licenses.  You may choose to be licensed under the terms of the GNU
9  * General Public License (GPL) Version 2, available from the file
10  * COPYING in the main directory of this source tree, or the
11  * OpenIB.org BSD license below:
12  *
13  *     Redistribution and use in source and binary forms, with or
14  *     without modification, are permitted provided that the following
15  *     conditions are met:
16  *
17  *      - Redistributions of source code must retain the above
18  *        copyright notice, this list of conditions and the following
19  *        disclaimer.
20  *
21  *      - Redistributions in binary form must reproduce the above
22  *        copyright notice, this list of conditions and the following
23  *        disclaimer in the documentation and/or other materials
24  *        provided with the distribution.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33  * SOFTWARE.
34  *
35  * $FreeBSD$
36  */
37 
38 #if !defined(IB_ADDR_H)
39 #define IB_ADDR_H
40 
41 #include <linux/in.h>
42 #include <linux/in6.h>
43 #include <linux/if_arp.h>
44 #include <linux/netdevice.h>
45 #include <linux/inetdevice.h>
46 #include <linux/socket.h>
47 #include <linux/if_vlan.h>
48 #include <net/ipv6.h>
49 #include <net/if_inet6.h>
50 #include <net/ip.h>
51 #include <rdma/ib_verbs.h>
52 #include <rdma/ib_pack.h>
53 #include <net/ipv6.h>
54 
55 struct rdma_addr_client {
56 	atomic_t refcount;
57 	struct completion comp;
58 };
59 
60 /**
61  * rdma_addr_register_client - Register an address client.
62  */
63 void rdma_addr_register_client(struct rdma_addr_client *client);
64 
65 /**
66  * rdma_addr_unregister_client - Deregister an address client.
67  * @client: Client object to deregister.
68  */
69 void rdma_addr_unregister_client(struct rdma_addr_client *client);
70 
71 /**
72  * struct rdma_dev_addr - Contains resolved RDMA hardware addresses
73  * @src_dev_addr:	Source MAC address.
74  * @dst_dev_addr:	Destination MAC address.
75  * @broadcast:		Broadcast address of the device.
76  * @dev_type:		The interface hardware type of the device.
77  * @bound_dev_if:	An optional device interface index.
78  * @transport:		The transport type used.
79  * @net:		Network namespace containing the bound_dev_if net_dev.
80  */
81 struct vnet;
82 struct rdma_dev_addr {
83 	unsigned char src_dev_addr[MAX_ADDR_LEN];
84 	unsigned char dst_dev_addr[MAX_ADDR_LEN];
85 	unsigned char broadcast[MAX_ADDR_LEN];
86 	unsigned short dev_type;
87 	int bound_dev_if;
88 	enum rdma_transport_type transport;
89 	struct vnet *net;
90 	enum rdma_network_type network;
91 	int hoplimit;
92 };
93 
94 /**
95  * rdma_translate_ip - Translate a local IP address to an RDMA hardware
96  *   address.
97  *
98  * The dev_addr->net and dev_addr->bound_dev_if fields must be initialized.
99  */
100 int rdma_translate_ip(const struct sockaddr *addr,
101 		      struct rdma_dev_addr *dev_addr);
102 
103 /**
104  * rdma_resolve_ip - Resolve source and destination IP addresses to
105  *   RDMA hardware addresses.
106  * @client: Address client associated with request.
107  * @src_addr: An optional source address to use in the resolution.  If a
108  *   source address is not provided, a usable address will be returned via
109  *   the callback.
110  * @dst_addr: The destination address to resolve.
111  * @addr: A reference to a data location that will receive the resolved
112  *   addresses.  The data location must remain valid until the callback has
113  *   been invoked. The net field of the addr struct must be valid.
114  * @timeout_ms: Amount of time to wait for the address resolution to complete.
115  * @callback: Call invoked once address resolution has completed, timed out,
116  *   or been canceled.  A status of 0 indicates success.
117  * @context: User-specified context associated with the call.
118  */
119 int rdma_resolve_ip(struct rdma_addr_client *client,
120 		    struct sockaddr *src_addr, struct sockaddr *dst_addr,
121 		    struct rdma_dev_addr *addr, int timeout_ms,
122 		    void (*callback)(int status, struct sockaddr *src_addr,
123 				     struct rdma_dev_addr *addr, void *context),
124 		    void *context);
125 
126 int rdma_resolve_ip_route(struct sockaddr *src_addr,
127 			  const struct sockaddr *dst_addr,
128 			  struct rdma_dev_addr *addr);
129 
130 void rdma_addr_cancel(struct rdma_dev_addr *addr);
131 
132 int rdma_copy_addr(struct rdma_dev_addr *dev_addr, struct net_device *dev,
133 	      const unsigned char *dst_dev_addr);
134 
135 int rdma_addr_size(struct sockaddr *addr);
136 int rdma_addr_size_in6(struct sockaddr_in6 *addr);
137 int rdma_addr_size_kss(struct sockaddr_storage *addr);
138 
139 int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid,
140 				 const union ib_gid *dgid,
141 				 u8 *smac, struct net_device *dev,
142 				 int *hoplimit);
143 
144 static inline u16 ib_addr_get_pkey(struct rdma_dev_addr *dev_addr)
145 {
146 	return ((u16)dev_addr->broadcast[8] << 8) | (u16)dev_addr->broadcast[9];
147 }
148 
149 static inline void ib_addr_set_pkey(struct rdma_dev_addr *dev_addr, u16 pkey)
150 {
151 	dev_addr->broadcast[8] = pkey >> 8;
152 	dev_addr->broadcast[9] = (unsigned char) pkey;
153 }
154 
155 static inline void ib_addr_get_mgid(struct rdma_dev_addr *dev_addr,
156 				    union ib_gid *gid)
157 {
158 	memcpy(gid, dev_addr->broadcast + 4, sizeof *gid);
159 }
160 
161 static inline int rdma_addr_gid_offset(struct rdma_dev_addr *dev_addr)
162 {
163 	return dev_addr->dev_type == ARPHRD_INFINIBAND ? 4 : 0;
164 }
165 
166 static inline u16 rdma_vlan_dev_vlan_id(const struct net_device *dev)
167 {
168 	uint16_t tag;
169 
170 	if (dev->if_pcp != IFNET_PCP_NONE)
171 		return 0x0000;	/* prio-tagged traffic */
172 	if (VLAN_TAG(__DECONST(struct ifnet *, dev), &tag) != 0)
173 		return 0xffff;
174 	return tag;
175 }
176 
177 static inline int rdma_ip2gid(const struct sockaddr *addr, union ib_gid *gid)
178 {
179 	switch (addr->sa_family) {
180 	case AF_INET:
181 		ipv6_addr_set_v4mapped(((const struct sockaddr_in *)
182 					addr)->sin_addr.s_addr,
183 				       (struct in6_addr *)gid);
184 		break;
185 	case AF_INET6:
186 		memcpy(gid->raw, &((const struct sockaddr_in6 *)addr)->sin6_addr, 16);
187 		/* make sure scope ID gets zeroed inside GID */
188 		if (IN6_IS_SCOPE_LINKLOCAL((struct in6_addr *)gid->raw) ||
189 		    IN6_IS_ADDR_MC_INTFACELOCAL((struct in6_addr *)gid->raw)) {
190 			gid->raw[2] = 0;
191 			gid->raw[3] = 0;
192 		}
193 		break;
194 	default:
195 		return -EINVAL;
196 	}
197 	return 0;
198 }
199 
200 /* Important - sockaddr should be a union of sockaddr_in and sockaddr_in6 */
201 static inline void rdma_gid2ip(struct sockaddr *out, const union ib_gid *gid)
202 {
203 	if (ipv6_addr_v4mapped((const struct in6_addr *)gid)) {
204 		struct sockaddr_in *out_in = (struct sockaddr_in *)out;
205 		memset(out_in, 0, sizeof(*out_in));
206 		out_in->sin_len = sizeof(*out_in);
207 		out_in->sin_family = AF_INET;
208 		memcpy(&out_in->sin_addr.s_addr, gid->raw + 12, 4);
209 	} else {
210 		struct sockaddr_in6 *out_in = (struct sockaddr_in6 *)out;
211 		memset(out_in, 0, sizeof(*out_in));
212 		out_in->sin6_len = sizeof(*out_in);
213 		out_in->sin6_family = AF_INET6;
214 		memcpy(&out_in->sin6_addr.s6_addr, gid->raw, 16);
215 	}
216 }
217 
218 static inline void iboe_addr_get_sgid(struct rdma_dev_addr *dev_addr,
219 				      union ib_gid *gid)
220 {
221 	struct net_device *dev;
222 	struct ifaddr *ifa;
223 
224 	dev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
225 	if (dev) {
226 		CK_STAILQ_FOREACH(ifa, &dev->if_addrhead, ifa_link) {
227 			if (ifa->ifa_addr == NULL ||
228 			    ifa->ifa_addr->sa_family != AF_INET)
229 				continue;
230 			ipv6_addr_set_v4mapped(((struct sockaddr_in *)
231 					       ifa->ifa_addr)->sin_addr.s_addr,
232 					       (struct in6_addr *)gid);
233 			break;
234 		}
235 		dev_put(dev);
236 	}
237 }
238 
239 static inline void rdma_addr_get_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
240 {
241 	if (dev_addr->transport == RDMA_TRANSPORT_IB &&
242 	    dev_addr->dev_type != ARPHRD_INFINIBAND)
243 		iboe_addr_get_sgid(dev_addr, gid);
244 	else
245 		memcpy(gid, dev_addr->src_dev_addr +
246 		       rdma_addr_gid_offset(dev_addr), sizeof *gid);
247 }
248 
249 static inline void rdma_addr_set_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
250 {
251 	memcpy(dev_addr->src_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
252 }
253 
254 static inline void rdma_addr_get_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
255 {
256 	memcpy(gid, dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), sizeof *gid);
257 }
258 
259 static inline void rdma_addr_set_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
260 {
261 	memcpy(dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
262 }
263 
264 static inline enum ib_mtu iboe_get_mtu(int mtu)
265 {
266 	/*
267 	 * reduce IB headers from effective IBoE MTU. 28 stands for
268 	 * atomic header which is the biggest possible header after BTH
269 	 */
270 	mtu = mtu - IB_GRH_BYTES - IB_BTH_BYTES - 28;
271 
272 	if (mtu >= ib_mtu_enum_to_int(IB_MTU_4096))
273 		return IB_MTU_4096;
274 	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_2048))
275 		return IB_MTU_2048;
276 	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_1024))
277 		return IB_MTU_1024;
278 	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_512))
279 		return IB_MTU_512;
280 	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_256))
281 		return IB_MTU_256;
282 	else
283 		return 0;
284 }
285 
286 static inline int iboe_get_rate(struct net_device *dev)
287 {
288 	uint64_t baudrate = dev->if_baudrate;
289 #ifdef if_baudrate_pf
290 	int exp;
291 	for (exp = dev->if_baudrate_pf; exp > 0; exp--)
292 		baudrate *= 10;
293 #endif
294 	if (baudrate >= IF_Gbps(40))
295 		return IB_RATE_40_GBPS;
296 	else if (baudrate >= IF_Gbps(30))
297 		return IB_RATE_30_GBPS;
298 	else if (baudrate >= IF_Gbps(20))
299 		return IB_RATE_20_GBPS;
300 	else if (baudrate >= IF_Gbps(10))
301 		return IB_RATE_10_GBPS;
302 	else
303 		return IB_RATE_PORT_CURRENT;
304 }
305 
306 static inline int rdma_link_local_addr(struct in6_addr *addr)
307 {
308 	if (addr->s6_addr32[0] == htonl(0xfe800000) &&
309 	    addr->s6_addr32[1] == 0)
310 		return 1;
311 
312 	return 0;
313 }
314 
315 static inline void rdma_get_ll_mac(struct in6_addr *addr, u8 *mac)
316 {
317 	memcpy(mac, &addr->s6_addr[8], 3);
318 	memcpy(mac + 3, &addr->s6_addr[13], 3);
319 	mac[0] ^= 2;
320 }
321 
322 static inline int rdma_is_multicast_addr(struct in6_addr *addr)
323 {
324 	__be32 ipv4_addr;
325 
326 	if (addr->s6_addr[0] == 0xff)
327 		return 1;
328 
329 	ipv4_addr = addr->s6_addr32[3];
330 	return (ipv6_addr_v4mapped(addr) && ipv4_is_multicast(ipv4_addr));
331 }
332 
333 static inline void rdma_get_mcast_mac(struct in6_addr *addr, u8 *mac)
334 {
335 	int i;
336 
337 	mac[0] = 0x33;
338 	mac[1] = 0x33;
339 	for (i = 2; i < 6; ++i)
340 		mac[i] = addr->s6_addr[i + 10];
341 }
342 
343 static inline u16 rdma_get_vlan_id(union ib_gid *dgid)
344 {
345 	u16 vid;
346 
347 	vid = dgid->raw[11] << 8 | dgid->raw[12];
348 	return vid < 0x1000 ? vid : 0xffff;
349 }
350 
351 static inline struct net_device *rdma_vlan_dev_real_dev(struct net_device *dev)
352 {
353 	if (dev->if_pcp != IFNET_PCP_NONE)
354 		return dev; /* prio-tagged traffic */
355 	return VLAN_TRUNKDEV(__DECONST(struct ifnet *, dev));
356 }
357 
358 #endif /* IB_ADDR_H */
359