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