xref: /freebsd/sys/ofed/include/rdma/ib_addr.h (revision 43a5ec4eb41567cc92586503212743d89686d78f)
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/socket.h>
46 #include <linux/if_vlan.h>
47 #include <net/ipv6.h>
48 #include <net/if_inet6.h>
49 #include <net/ip.h>
50 #include <rdma/ib_verbs.h>
51 #include <rdma/ib_pack.h>
52 #include <rdma/ib_addr_freebsd.h>
53 
54 /* Linux netdevice.h but for working on an ifnet rather than a net_device. */
55 #define	dev_hold(d)	if_ref(d)
56 #define	dev_put(d)	if_rele(d)
57 #define	dev_net(d)	((d)->if_vnet)
58 #define	net_eq(a,b)	((a) == (b))
59 
60 
61 struct rdma_addr_client {
62 	atomic_t refcount;
63 	struct completion comp;
64 };
65 
66 union rdma_sockaddr {
67 	struct sockaddr         _sockaddr;
68 	struct sockaddr_in      _sockaddr_in;
69 	struct sockaddr_in6     _sockaddr_in6;
70 	struct sockaddr_storage _sockaddr_ss;
71 };
72 
73 /**
74  * rdma_addr_register_client - Register an address client.
75  */
76 void rdma_addr_register_client(struct rdma_addr_client *client);
77 
78 /**
79  * rdma_addr_unregister_client - Deregister an address client.
80  * @client: Client object to deregister.
81  */
82 void rdma_addr_unregister_client(struct rdma_addr_client *client);
83 
84 /**
85  * struct rdma_dev_addr - Contains resolved RDMA hardware addresses
86  * @src_dev_addr:	Source MAC address.
87  * @dst_dev_addr:	Destination MAC address.
88  * @broadcast:		Broadcast address of the device.
89  * @dev_type:		The interface hardware type of the device.
90  * @bound_dev_if:	An optional device interface index.
91  * @transport:		The transport type used.
92  * @net:		Network namespace containing the bound_dev_if net_dev.
93  */
94 struct vnet;
95 struct rdma_dev_addr {
96 	unsigned char src_dev_addr[MAX_ADDR_LEN];
97 	unsigned char dst_dev_addr[MAX_ADDR_LEN];
98 	unsigned char broadcast[MAX_ADDR_LEN];
99 	unsigned short dev_type;
100 	int bound_dev_if;
101 	enum rdma_transport_type transport;
102 	struct vnet *net;
103 	enum rdma_network_type network;
104 	int hoplimit;
105 };
106 
107 /**
108  * rdma_translate_ip - Translate a local IP address to an RDMA hardware
109  *   address.
110  *
111  * The dev_addr->net and dev_addr->bound_dev_if fields must be initialized.
112  */
113 int rdma_translate_ip(const struct sockaddr *addr,
114 		      struct rdma_dev_addr *dev_addr);
115 
116 /**
117  * rdma_resolve_ip - Resolve source and destination IP addresses to
118  *   RDMA hardware addresses.
119  * @client: Address client associated with request.
120  * @src_addr: An optional source address to use in the resolution.  If a
121  *   source address is not provided, a usable address will be returned via
122  *   the callback.
123  * @dst_addr: The destination address to resolve.
124  * @addr: A reference to a data location that will receive the resolved
125  *   addresses.  The data location must remain valid until the callback has
126  *   been invoked. The net field of the addr struct must be valid.
127  * @timeout_ms: Amount of time to wait for the address resolution to complete.
128  * @callback: Call invoked once address resolution has completed, timed out,
129  *   or been canceled.  A status of 0 indicates success.
130  * @context: User-specified context associated with the call.
131  */
132 int rdma_resolve_ip(struct rdma_addr_client *client,
133 		    struct sockaddr *src_addr, struct sockaddr *dst_addr,
134 		    struct rdma_dev_addr *addr, int timeout_ms,
135 		    void (*callback)(int status, struct sockaddr *src_addr,
136 				     struct rdma_dev_addr *addr, void *context),
137 		    void *context);
138 
139 int rdma_resolve_ip_route(struct sockaddr *src_addr,
140 			  const struct sockaddr *dst_addr,
141 			  struct rdma_dev_addr *addr);
142 
143 void rdma_addr_cancel(struct rdma_dev_addr *addr);
144 
145 int rdma_copy_addr(struct rdma_dev_addr *dev_addr, struct ifnet *dev,
146 	      const unsigned char *dst_dev_addr);
147 
148 int rdma_addr_size(struct sockaddr *addr);
149 int rdma_addr_size_in6(struct sockaddr_in6 *addr);
150 int rdma_addr_size_kss(struct sockaddr_storage *addr);
151 
152 int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid,
153 				 const union ib_gid *dgid,
154 				 u8 *smac, struct ifnet *dev,
155 				 int *hoplimit);
156 
157 static inline u16 ib_addr_get_pkey(struct rdma_dev_addr *dev_addr)
158 {
159 	return ((u16)dev_addr->broadcast[8] << 8) | (u16)dev_addr->broadcast[9];
160 }
161 
162 static inline void ib_addr_set_pkey(struct rdma_dev_addr *dev_addr, u16 pkey)
163 {
164 	dev_addr->broadcast[8] = pkey >> 8;
165 	dev_addr->broadcast[9] = (unsigned char) pkey;
166 }
167 
168 static inline void ib_addr_get_mgid(struct rdma_dev_addr *dev_addr,
169 				    union ib_gid *gid)
170 {
171 	memcpy(gid, dev_addr->broadcast + 4, sizeof *gid);
172 }
173 
174 static inline int rdma_addr_gid_offset(struct rdma_dev_addr *dev_addr)
175 {
176 	return dev_addr->dev_type == ARPHRD_INFINIBAND ? 4 : 0;
177 }
178 
179 static inline u16 rdma_vlan_dev_vlan_id(const struct ifnet *dev)
180 {
181 	uint16_t tag;
182 
183 	if (dev->if_type == IFT_ETHER && dev->if_pcp != IFNET_PCP_NONE)
184 		return 0x0000;	/* prio-tagged traffic */
185 	if (VLAN_TAG(__DECONST(struct ifnet *, dev), &tag) != 0)
186 		return 0xffff;
187 	return tag;
188 }
189 
190 static inline int rdma_ip2gid(const struct sockaddr *addr, union ib_gid *gid)
191 {
192 	switch (addr->sa_family) {
193 	case AF_INET:
194 		ipv6_addr_set_v4mapped(((const struct sockaddr_in *)
195 					addr)->sin_addr.s_addr,
196 				       (struct in6_addr *)gid);
197 		break;
198 	case AF_INET6:
199 		memcpy(gid->raw, &((const struct sockaddr_in6 *)addr)->sin6_addr, 16);
200 		/* make sure scope ID gets zeroed inside GID */
201 		if (IN6_IS_SCOPE_LINKLOCAL((struct in6_addr *)gid->raw) ||
202 		    IN6_IS_ADDR_MC_INTFACELOCAL((struct in6_addr *)gid->raw)) {
203 			gid->raw[2] = 0;
204 			gid->raw[3] = 0;
205 		}
206 		break;
207 	default:
208 		return -EINVAL;
209 	}
210 	return 0;
211 }
212 
213 /* Important - sockaddr should be a union of sockaddr_in and sockaddr_in6 */
214 static inline void rdma_gid2ip(struct sockaddr *out, const union ib_gid *gid)
215 {
216 	if (ipv6_addr_v4mapped((const struct in6_addr *)gid)) {
217 		struct sockaddr_in *out_in = (struct sockaddr_in *)out;
218 		memset(out_in, 0, sizeof(*out_in));
219 		out_in->sin_len = sizeof(*out_in);
220 		out_in->sin_family = AF_INET;
221 		memcpy(&out_in->sin_addr.s_addr, gid->raw + 12, 4);
222 	} else {
223 		struct sockaddr_in6 *out_in = (struct sockaddr_in6 *)out;
224 		memset(out_in, 0, sizeof(*out_in));
225 		out_in->sin6_len = sizeof(*out_in);
226 		out_in->sin6_family = AF_INET6;
227 		memcpy(&out_in->sin6_addr.s6_addr, gid->raw, 16);
228 	}
229 }
230 
231 static inline void iboe_addr_get_sgid(struct rdma_dev_addr *dev_addr,
232 				      union ib_gid *gid)
233 {
234 	struct ifnet *dev;
235 	struct ifaddr *ifa;
236 
237 #ifdef VIMAGE
238 	if (dev_addr->net == NULL)
239 		return;
240 #endif
241 	dev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
242 	if (dev) {
243 		CK_STAILQ_FOREACH(ifa, &dev->if_addrhead, ifa_link) {
244 			if (ifa->ifa_addr == NULL ||
245 			    ifa->ifa_addr->sa_family != AF_INET)
246 				continue;
247 			ipv6_addr_set_v4mapped(((struct sockaddr_in *)
248 					       ifa->ifa_addr)->sin_addr.s_addr,
249 					       (struct in6_addr *)gid);
250 			break;
251 		}
252 		dev_put(dev);
253 	}
254 }
255 
256 static inline void rdma_addr_get_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
257 {
258 	if (dev_addr->transport == RDMA_TRANSPORT_IB &&
259 	    dev_addr->dev_type != ARPHRD_INFINIBAND)
260 		iboe_addr_get_sgid(dev_addr, gid);
261 	else
262 		memcpy(gid, dev_addr->src_dev_addr +
263 		       rdma_addr_gid_offset(dev_addr), sizeof *gid);
264 }
265 
266 static inline void rdma_addr_set_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
267 {
268 	memcpy(dev_addr->src_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
269 }
270 
271 static inline void rdma_addr_get_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
272 {
273 	memcpy(gid, dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), sizeof *gid);
274 }
275 
276 static inline void rdma_addr_set_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
277 {
278 	memcpy(dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
279 }
280 
281 static inline enum ib_mtu iboe_get_mtu(int mtu)
282 {
283 	/*
284 	 * reduce IB headers from effective IBoE MTU. 28 stands for
285 	 * atomic header which is the biggest possible header after BTH
286 	 */
287 	mtu = mtu - IB_GRH_BYTES - IB_BTH_BYTES - 28;
288 
289 	if (mtu >= ib_mtu_enum_to_int(IB_MTU_4096))
290 		return IB_MTU_4096;
291 	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_2048))
292 		return IB_MTU_2048;
293 	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_1024))
294 		return IB_MTU_1024;
295 	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_512))
296 		return IB_MTU_512;
297 	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_256))
298 		return IB_MTU_256;
299 	else
300 		return 0;
301 }
302 
303 static inline int iboe_get_rate(struct ifnet *dev)
304 {
305 	uint64_t baudrate = dev->if_baudrate;
306 #ifdef if_baudrate_pf
307 	int exp;
308 	for (exp = dev->if_baudrate_pf; exp > 0; exp--)
309 		baudrate *= 10;
310 #endif
311 	if (baudrate >= IF_Gbps(40))
312 		return IB_RATE_40_GBPS;
313 	else if (baudrate >= IF_Gbps(30))
314 		return IB_RATE_30_GBPS;
315 	else if (baudrate >= IF_Gbps(20))
316 		return IB_RATE_20_GBPS;
317 	else if (baudrate >= IF_Gbps(10))
318 		return IB_RATE_10_GBPS;
319 	else
320 		return IB_RATE_PORT_CURRENT;
321 }
322 
323 static inline int rdma_link_local_addr(struct in6_addr *addr)
324 {
325 	if (addr->s6_addr32[0] == htonl(0xfe800000) &&
326 	    addr->s6_addr32[1] == 0)
327 		return 1;
328 
329 	return 0;
330 }
331 
332 static inline void rdma_get_ll_mac(struct in6_addr *addr, u8 *mac)
333 {
334 	memcpy(mac, &addr->s6_addr[8], 3);
335 	memcpy(mac + 3, &addr->s6_addr[13], 3);
336 	mac[0] ^= 2;
337 }
338 
339 static inline int rdma_is_multicast_addr(struct in6_addr *addr)
340 {
341 	__be32 ipv4_addr;
342 
343 	if (addr->s6_addr[0] == 0xff)
344 		return 1;
345 
346 	ipv4_addr = addr->s6_addr32[3];
347 	return (ipv6_addr_v4mapped(addr) && ipv4_is_multicast(ipv4_addr));
348 }
349 
350 static inline void rdma_get_mcast_mac(struct in6_addr *addr, u8 *mac)
351 {
352 	int i;
353 
354 	mac[0] = 0x33;
355 	mac[1] = 0x33;
356 	for (i = 2; i < 6; ++i)
357 		mac[i] = addr->s6_addr[i + 10];
358 }
359 
360 static inline u16 rdma_get_vlan_id(union ib_gid *dgid)
361 {
362 	u16 vid;
363 
364 	vid = dgid->raw[11] << 8 | dgid->raw[12];
365 	return vid < 0x1000 ? vid : 0xffff;
366 }
367 
368 static inline struct ifnet *rdma_vlan_dev_real_dev(struct ifnet *dev)
369 {
370 	struct epoch_tracker et;
371 
372 	NET_EPOCH_ENTER(et);
373 	if (dev->if_type != IFT_ETHER || dev->if_pcp == IFNET_PCP_NONE)
374 		dev = VLAN_TRUNKDEV(dev);	/* non prio-tagged traffic */
375 	NET_EPOCH_EXIT(et);
376 	return (dev);
377 }
378 
379 #endif /* IB_ADDR_H */
380