xref: /freebsd/sys/ofed/include/rdma/ib_addr.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
1fe267a55SPedro F. Giffuni /*-
2fe267a55SPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3fe267a55SPedro F. Giffuni  *
4aa0a1e58SJeff Roberson  * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
5aa0a1e58SJeff Roberson  * Copyright (c) 2005 Intel Corporation.  All rights reserved.
6aa0a1e58SJeff Roberson  *
7aa0a1e58SJeff Roberson  * This software is available to you under a choice of one of two
8aa0a1e58SJeff Roberson  * licenses.  You may choose to be licensed under the terms of the GNU
9aa0a1e58SJeff Roberson  * General Public License (GPL) Version 2, available from the file
10aa0a1e58SJeff Roberson  * COPYING in the main directory of this source tree, or the
11aa0a1e58SJeff Roberson  * OpenIB.org BSD license below:
12aa0a1e58SJeff Roberson  *
13aa0a1e58SJeff Roberson  *     Redistribution and use in source and binary forms, with or
14aa0a1e58SJeff Roberson  *     without modification, are permitted provided that the following
15aa0a1e58SJeff Roberson  *     conditions are met:
16aa0a1e58SJeff Roberson  *
17aa0a1e58SJeff Roberson  *      - Redistributions of source code must retain the above
18aa0a1e58SJeff Roberson  *        copyright notice, this list of conditions and the following
19aa0a1e58SJeff Roberson  *        disclaimer.
20aa0a1e58SJeff Roberson  *
21aa0a1e58SJeff Roberson  *      - Redistributions in binary form must reproduce the above
22aa0a1e58SJeff Roberson  *        copyright notice, this list of conditions and the following
23aa0a1e58SJeff Roberson  *        disclaimer in the documentation and/or other materials
24aa0a1e58SJeff Roberson  *        provided with the distribution.
25aa0a1e58SJeff Roberson  *
26aa0a1e58SJeff Roberson  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27aa0a1e58SJeff Roberson  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28aa0a1e58SJeff Roberson  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29aa0a1e58SJeff Roberson  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
30aa0a1e58SJeff Roberson  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
31aa0a1e58SJeff Roberson  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
32aa0a1e58SJeff Roberson  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33aa0a1e58SJeff Roberson  * SOFTWARE.
34aa0a1e58SJeff Roberson  */
35aa0a1e58SJeff Roberson 
36478d3005SHans Petter Selasky #if !defined(IB_ADDR_H)
37aa0a1e58SJeff Roberson #define IB_ADDR_H
38aa0a1e58SJeff Roberson 
39aa0a1e58SJeff Roberson #include <linux/in.h>
40aa0a1e58SJeff Roberson #include <linux/in6.h>
41aa0a1e58SJeff Roberson #include <linux/if_arp.h>
42aa0a1e58SJeff Roberson #include <linux/netdevice.h>
43aa0a1e58SJeff Roberson #include <linux/socket.h>
44b5c1e0cbSHans Petter Selasky #include <linux/if_vlan.h>
45478d3005SHans Petter Selasky #include <net/ipv6.h>
46478d3005SHans Petter Selasky #include <net/if_inet6.h>
47478d3005SHans Petter Selasky #include <net/ip.h>
48aa0a1e58SJeff Roberson #include <rdma/ib_verbs.h>
49aa0a1e58SJeff Roberson #include <rdma/ib_pack.h>
50c35034b3SBjoern A. Zeeb #include <rdma/ib_addr_freebsd.h>
51aa0a1e58SJeff Roberson 
52825b7d4cSBjoern A. Zeeb /* Linux netdevice.h but for working on an ifnet rather than a net_device. */
53825b7d4cSBjoern A. Zeeb #define	dev_hold(d)	if_ref(d)
54825b7d4cSBjoern A. Zeeb #define	dev_put(d)	if_rele(d)
553e142e07SJustin Hibbits #define	dev_net(d)	if_getvnet(d)
56825b7d4cSBjoern A. Zeeb #define	net_eq(a,b)	((a) == (b))
57825b7d4cSBjoern A. Zeeb 
58825b7d4cSBjoern A. Zeeb 
59aa0a1e58SJeff Roberson struct rdma_addr_client {
60aa0a1e58SJeff Roberson 	atomic_t refcount;
61aa0a1e58SJeff Roberson 	struct completion comp;
62aa0a1e58SJeff Roberson };
63aa0a1e58SJeff Roberson 
64536457e1SEric van Gyzen union rdma_sockaddr {
65536457e1SEric van Gyzen 	struct sockaddr         _sockaddr;
66536457e1SEric van Gyzen 	struct sockaddr_in      _sockaddr_in;
67536457e1SEric van Gyzen 	struct sockaddr_in6     _sockaddr_in6;
68536457e1SEric van Gyzen 	struct sockaddr_storage _sockaddr_ss;
69536457e1SEric van Gyzen };
70536457e1SEric van Gyzen 
71aa0a1e58SJeff Roberson /**
72aa0a1e58SJeff Roberson  * rdma_addr_register_client - Register an address client.
73aa0a1e58SJeff Roberson  */
74aa0a1e58SJeff Roberson void rdma_addr_register_client(struct rdma_addr_client *client);
75aa0a1e58SJeff Roberson 
76aa0a1e58SJeff Roberson /**
77aa0a1e58SJeff Roberson  * rdma_addr_unregister_client - Deregister an address client.
78aa0a1e58SJeff Roberson  * @client: Client object to deregister.
79aa0a1e58SJeff Roberson  */
80aa0a1e58SJeff Roberson void rdma_addr_unregister_client(struct rdma_addr_client *client);
81aa0a1e58SJeff Roberson 
82478d3005SHans Petter Selasky /**
83478d3005SHans Petter Selasky  * struct rdma_dev_addr - Contains resolved RDMA hardware addresses
84478d3005SHans Petter Selasky  * @src_dev_addr:	Source MAC address.
85478d3005SHans Petter Selasky  * @dst_dev_addr:	Destination MAC address.
86478d3005SHans Petter Selasky  * @broadcast:		Broadcast address of the device.
87478d3005SHans Petter Selasky  * @dev_type:		The interface hardware type of the device.
88478d3005SHans Petter Selasky  * @bound_dev_if:	An optional device interface index.
89478d3005SHans Petter Selasky  * @transport:		The transport type used.
90478d3005SHans Petter Selasky  * @net:		Network namespace containing the bound_dev_if net_dev.
91478d3005SHans Petter Selasky  */
92478d3005SHans Petter Selasky struct vnet;
93aa0a1e58SJeff Roberson struct rdma_dev_addr {
94aa0a1e58SJeff Roberson 	unsigned char src_dev_addr[MAX_ADDR_LEN];
95aa0a1e58SJeff Roberson 	unsigned char dst_dev_addr[MAX_ADDR_LEN];
96aa0a1e58SJeff Roberson 	unsigned char broadcast[MAX_ADDR_LEN];
97aa0a1e58SJeff Roberson 	unsigned short dev_type;
98aa0a1e58SJeff Roberson 	int bound_dev_if;
99aa0a1e58SJeff Roberson 	enum rdma_transport_type transport;
100478d3005SHans Petter Selasky 	struct vnet *net;
101478d3005SHans Petter Selasky 	enum rdma_network_type network;
102478d3005SHans Petter Selasky 	int hoplimit;
103aa0a1e58SJeff Roberson };
104aa0a1e58SJeff Roberson 
105aa0a1e58SJeff Roberson /**
106aa0a1e58SJeff Roberson  * rdma_translate_ip - Translate a local IP address to an RDMA hardware
107aa0a1e58SJeff Roberson  *   address.
108478d3005SHans Petter Selasky  *
109bf8641feSHans Petter Selasky  * The dev_addr->net and dev_addr->bound_dev_if fields must be initialized.
110aa0a1e58SJeff Roberson  */
111478d3005SHans Petter Selasky int rdma_translate_ip(const struct sockaddr *addr,
112bf8641feSHans Petter Selasky 		      struct rdma_dev_addr *dev_addr);
113aa0a1e58SJeff Roberson 
114aa0a1e58SJeff Roberson /**
115aa0a1e58SJeff Roberson  * rdma_resolve_ip - Resolve source and destination IP addresses to
116aa0a1e58SJeff Roberson  *   RDMA hardware addresses.
117aa0a1e58SJeff Roberson  * @client: Address client associated with request.
118aa0a1e58SJeff Roberson  * @src_addr: An optional source address to use in the resolution.  If a
119aa0a1e58SJeff Roberson  *   source address is not provided, a usable address will be returned via
120aa0a1e58SJeff Roberson  *   the callback.
121aa0a1e58SJeff Roberson  * @dst_addr: The destination address to resolve.
122aa0a1e58SJeff Roberson  * @addr: A reference to a data location that will receive the resolved
123aa0a1e58SJeff Roberson  *   addresses.  The data location must remain valid until the callback has
124478d3005SHans Petter Selasky  *   been invoked. The net field of the addr struct must be valid.
125aa0a1e58SJeff Roberson  * @timeout_ms: Amount of time to wait for the address resolution to complete.
126aa0a1e58SJeff Roberson  * @callback: Call invoked once address resolution has completed, timed out,
127aa0a1e58SJeff Roberson  *   or been canceled.  A status of 0 indicates success.
128aa0a1e58SJeff Roberson  * @context: User-specified context associated with the call.
129aa0a1e58SJeff Roberson  */
130aa0a1e58SJeff Roberson int rdma_resolve_ip(struct rdma_addr_client *client,
131aa0a1e58SJeff Roberson 		    struct sockaddr *src_addr, struct sockaddr *dst_addr,
132aa0a1e58SJeff Roberson 		    struct rdma_dev_addr *addr, int timeout_ms,
133aa0a1e58SJeff Roberson 		    void (*callback)(int status, struct sockaddr *src_addr,
134aa0a1e58SJeff Roberson 				     struct rdma_dev_addr *addr, void *context),
135aa0a1e58SJeff Roberson 		    void *context);
136aa0a1e58SJeff Roberson 
137478d3005SHans Petter Selasky int rdma_resolve_ip_route(struct sockaddr *src_addr,
138478d3005SHans Petter Selasky 			  const struct sockaddr *dst_addr,
139478d3005SHans Petter Selasky 			  struct rdma_dev_addr *addr);
140478d3005SHans Petter Selasky 
141aa0a1e58SJeff Roberson void rdma_addr_cancel(struct rdma_dev_addr *addr);
142aa0a1e58SJeff Roberson 
1433e142e07SJustin Hibbits int rdma_copy_addr(struct rdma_dev_addr *dev_addr, if_t dev,
144aa0a1e58SJeff Roberson 	      const unsigned char *dst_dev_addr);
145aa0a1e58SJeff Roberson 
146478d3005SHans Petter Selasky int rdma_addr_size(struct sockaddr *addr);
147855ad7cfSHans Petter Selasky int rdma_addr_size_in6(struct sockaddr_in6 *addr);
148855ad7cfSHans Petter Selasky int rdma_addr_size_kss(struct sockaddr_storage *addr);
149478d3005SHans Petter Selasky 
150478d3005SHans Petter Selasky int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid,
151478d3005SHans Petter Selasky 				 const union ib_gid *dgid,
1523e142e07SJustin Hibbits 				 u8 *smac, if_t dev,
153478d3005SHans Petter Selasky 				 int *hoplimit);
154aa0a1e58SJeff Roberson 
ib_addr_get_pkey(struct rdma_dev_addr * dev_addr)155aa0a1e58SJeff Roberson static inline u16 ib_addr_get_pkey(struct rdma_dev_addr *dev_addr)
156aa0a1e58SJeff Roberson {
157aa0a1e58SJeff Roberson 	return ((u16)dev_addr->broadcast[8] << 8) | (u16)dev_addr->broadcast[9];
158aa0a1e58SJeff Roberson }
159aa0a1e58SJeff Roberson 
ib_addr_set_pkey(struct rdma_dev_addr * dev_addr,u16 pkey)160aa0a1e58SJeff Roberson static inline void ib_addr_set_pkey(struct rdma_dev_addr *dev_addr, u16 pkey)
161aa0a1e58SJeff Roberson {
162aa0a1e58SJeff Roberson 	dev_addr->broadcast[8] = pkey >> 8;
163aa0a1e58SJeff Roberson 	dev_addr->broadcast[9] = (unsigned char) pkey;
164aa0a1e58SJeff Roberson }
165aa0a1e58SJeff Roberson 
ib_addr_get_mgid(struct rdma_dev_addr * dev_addr,union ib_gid * gid)166aa0a1e58SJeff Roberson static inline void ib_addr_get_mgid(struct rdma_dev_addr *dev_addr,
167aa0a1e58SJeff Roberson 				    union ib_gid *gid)
168aa0a1e58SJeff Roberson {
169aa0a1e58SJeff Roberson 	memcpy(gid, dev_addr->broadcast + 4, sizeof *gid);
170aa0a1e58SJeff Roberson }
171aa0a1e58SJeff Roberson 
rdma_addr_gid_offset(struct rdma_dev_addr * dev_addr)172aa0a1e58SJeff Roberson static inline int rdma_addr_gid_offset(struct rdma_dev_addr *dev_addr)
173aa0a1e58SJeff Roberson {
174aa0a1e58SJeff Roberson 	return dev_addr->dev_type == ARPHRD_INFINIBAND ? 4 : 0;
175aa0a1e58SJeff Roberson }
176aa0a1e58SJeff Roberson 
rdma_vlan_dev_vlan_id(if_t dev)1773e142e07SJustin Hibbits static inline u16 rdma_vlan_dev_vlan_id(if_t dev)
178aa0a1e58SJeff Roberson {
179aa0a1e58SJeff Roberson 	uint16_t tag;
180aa0a1e58SJeff Roberson 
181*12e99b63SJustin Hibbits 	if (if_gettype(dev) == IFT_ETHER && if_getpcp(dev) != IFNET_PCP_NONE)
182f4546fa3SHans Petter Selasky 		return 0x0000;	/* prio-tagged traffic */
1833e142e07SJustin Hibbits 	if (VLAN_TAG(__DECONST(if_t, dev), &tag) != 0)
184aa0a1e58SJeff Roberson 		return 0xffff;
185aa0a1e58SJeff Roberson 	return tag;
186aa0a1e58SJeff Roberson }
187aa0a1e58SJeff Roberson 
rdma_ip2gid(const struct sockaddr * addr,union ib_gid * gid)188478d3005SHans Petter Selasky static inline int rdma_ip2gid(const struct sockaddr *addr, union ib_gid *gid)
189b5c1e0cbSHans Petter Selasky {
190b5c1e0cbSHans Petter Selasky 	switch (addr->sa_family) {
191b5c1e0cbSHans Petter Selasky 	case AF_INET:
192478d3005SHans Petter Selasky 		ipv6_addr_set_v4mapped(((const struct sockaddr_in *)
193478d3005SHans Petter Selasky 					addr)->sin_addr.s_addr,
194b5c1e0cbSHans Petter Selasky 				       (struct in6_addr *)gid);
195b5c1e0cbSHans Petter Selasky 		break;
196b5c1e0cbSHans Petter Selasky 	case AF_INET6:
197478d3005SHans Petter Selasky 		memcpy(gid->raw, &((const struct sockaddr_in6 *)addr)->sin6_addr, 16);
198aacb0377SHans Petter Selasky 		/* make sure scope ID gets zeroed inside GID */
199aacb0377SHans Petter Selasky 		if (IN6_IS_SCOPE_LINKLOCAL((struct in6_addr *)gid->raw) ||
200aacb0377SHans Petter Selasky 		    IN6_IS_ADDR_MC_INTFACELOCAL((struct in6_addr *)gid->raw)) {
201aacb0377SHans Petter Selasky 			gid->raw[2] = 0;
202aacb0377SHans Petter Selasky 			gid->raw[3] = 0;
203aacb0377SHans Petter Selasky 		}
204b5c1e0cbSHans Petter Selasky 		break;
205b5c1e0cbSHans Petter Selasky 	default:
206b5c1e0cbSHans Petter Selasky 		return -EINVAL;
207b5c1e0cbSHans Petter Selasky 	}
208b5c1e0cbSHans Petter Selasky 	return 0;
209b5c1e0cbSHans Petter Selasky }
210b5c1e0cbSHans Petter Selasky 
211b5c1e0cbSHans Petter Selasky /* Important - sockaddr should be a union of sockaddr_in and sockaddr_in6 */
rdma_gid2ip(struct sockaddr * out,const union ib_gid * gid)212478d3005SHans Petter Selasky static inline void rdma_gid2ip(struct sockaddr *out, const union ib_gid *gid)
213b5c1e0cbSHans Petter Selasky {
214478d3005SHans Petter Selasky 	if (ipv6_addr_v4mapped((const struct in6_addr *)gid)) {
215b5c1e0cbSHans Petter Selasky 		struct sockaddr_in *out_in = (struct sockaddr_in *)out;
216b5c1e0cbSHans Petter Selasky 		memset(out_in, 0, sizeof(*out_in));
217b5c1e0cbSHans Petter Selasky 		out_in->sin_len = sizeof(*out_in);
218b5c1e0cbSHans Petter Selasky 		out_in->sin_family = AF_INET;
219b5c1e0cbSHans Petter Selasky 		memcpy(&out_in->sin_addr.s_addr, gid->raw + 12, 4);
220b5c1e0cbSHans Petter Selasky 	} else {
221b5c1e0cbSHans Petter Selasky 		struct sockaddr_in6 *out_in = (struct sockaddr_in6 *)out;
222b5c1e0cbSHans Petter Selasky 		memset(out_in, 0, sizeof(*out_in));
2230bab509bSHans Petter Selasky 		out_in->sin6_len = sizeof(*out_in);
224b5c1e0cbSHans Petter Selasky 		out_in->sin6_family = AF_INET6;
225b5c1e0cbSHans Petter Selasky 		memcpy(&out_in->sin6_addr.s6_addr, gid->raw, 16);
226b5c1e0cbSHans Petter Selasky 	}
227b5c1e0cbSHans Petter Selasky }
228b5c1e0cbSHans Petter Selasky 
2293e142e07SJustin Hibbits static u_int
_iboe_addr_get_sgid_ia_cb(void * arg,struct ifaddr * ifa,u_int count __unused)2303e142e07SJustin Hibbits _iboe_addr_get_sgid_ia_cb(void *arg, struct ifaddr *ifa, u_int count __unused)
2313e142e07SJustin Hibbits {
2323e142e07SJustin Hibbits 	ipv6_addr_set_v4mapped(((struct sockaddr_in *)
2333e142e07SJustin Hibbits 			       ifa->ifa_addr)->sin_addr.s_addr,
2343e142e07SJustin Hibbits 			       (struct in6_addr *)arg);
2353e142e07SJustin Hibbits 	return (0);
2363e142e07SJustin Hibbits }
2373e142e07SJustin Hibbits 
iboe_addr_get_sgid(struct rdma_dev_addr * dev_addr,union ib_gid * gid)238aa0a1e58SJeff Roberson static inline void iboe_addr_get_sgid(struct rdma_dev_addr *dev_addr,
239aa0a1e58SJeff Roberson 				      union ib_gid *gid)
240aa0a1e58SJeff Roberson {
2413e142e07SJustin Hibbits 	if_t dev;
242478d3005SHans Petter Selasky 
2437877f593SHans Petter Selasky #ifdef VIMAGE
2447877f593SHans Petter Selasky 	if (dev_addr->net == NULL)
2457877f593SHans Petter Selasky 		return;
2467877f593SHans Petter Selasky #endif
2477877f593SHans Petter Selasky 	dev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if);
248478d3005SHans Petter Selasky 	if (dev) {
2493e142e07SJustin Hibbits 		if_foreach_addr_type(dev, AF_INET,
2503e142e07SJustin Hibbits 				     _iboe_addr_get_sgid_ia_cb, gid);
251478d3005SHans Petter Selasky 		dev_put(dev);
252478d3005SHans Petter Selasky 	}
253aa0a1e58SJeff Roberson }
254aa0a1e58SJeff Roberson 
rdma_addr_get_sgid(struct rdma_dev_addr * dev_addr,union ib_gid * gid)255aa0a1e58SJeff Roberson static inline void rdma_addr_get_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
256aa0a1e58SJeff Roberson {
257aa0a1e58SJeff Roberson 	if (dev_addr->transport == RDMA_TRANSPORT_IB &&
258aa0a1e58SJeff Roberson 	    dev_addr->dev_type != ARPHRD_INFINIBAND)
259aa0a1e58SJeff Roberson 		iboe_addr_get_sgid(dev_addr, gid);
260aa0a1e58SJeff Roberson 	else
261aa0a1e58SJeff Roberson 		memcpy(gid, dev_addr->src_dev_addr +
262aa0a1e58SJeff Roberson 		       rdma_addr_gid_offset(dev_addr), sizeof *gid);
263aa0a1e58SJeff Roberson }
264aa0a1e58SJeff Roberson 
rdma_addr_set_sgid(struct rdma_dev_addr * dev_addr,union ib_gid * gid)265aa0a1e58SJeff Roberson static inline void rdma_addr_set_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
266aa0a1e58SJeff Roberson {
267aa0a1e58SJeff Roberson 	memcpy(dev_addr->src_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
268aa0a1e58SJeff Roberson }
269aa0a1e58SJeff Roberson 
rdma_addr_get_dgid(struct rdma_dev_addr * dev_addr,union ib_gid * gid)270aa0a1e58SJeff Roberson static inline void rdma_addr_get_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
271aa0a1e58SJeff Roberson {
272aa0a1e58SJeff Roberson 	memcpy(gid, dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), sizeof *gid);
273aa0a1e58SJeff Roberson }
274aa0a1e58SJeff Roberson 
rdma_addr_set_dgid(struct rdma_dev_addr * dev_addr,union ib_gid * gid)275aa0a1e58SJeff Roberson static inline void rdma_addr_set_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
276aa0a1e58SJeff Roberson {
277aa0a1e58SJeff Roberson 	memcpy(dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
278aa0a1e58SJeff Roberson }
279aa0a1e58SJeff Roberson 
iboe_get_mtu(int mtu)280aa0a1e58SJeff Roberson static inline enum ib_mtu iboe_get_mtu(int mtu)
281aa0a1e58SJeff Roberson {
282aa0a1e58SJeff Roberson 	/*
283aa0a1e58SJeff Roberson 	 * reduce IB headers from effective IBoE MTU. 28 stands for
284aa0a1e58SJeff Roberson 	 * atomic header which is the biggest possible header after BTH
285aa0a1e58SJeff Roberson 	 */
286aa0a1e58SJeff Roberson 	mtu = mtu - IB_GRH_BYTES - IB_BTH_BYTES - 28;
287aa0a1e58SJeff Roberson 
288aa0a1e58SJeff Roberson 	if (mtu >= ib_mtu_enum_to_int(IB_MTU_4096))
289aa0a1e58SJeff Roberson 		return IB_MTU_4096;
290aa0a1e58SJeff Roberson 	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_2048))
291aa0a1e58SJeff Roberson 		return IB_MTU_2048;
292aa0a1e58SJeff Roberson 	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_1024))
293aa0a1e58SJeff Roberson 		return IB_MTU_1024;
294aa0a1e58SJeff Roberson 	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_512))
295aa0a1e58SJeff Roberson 		return IB_MTU_512;
296aa0a1e58SJeff Roberson 	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_256))
297aa0a1e58SJeff Roberson 		return IB_MTU_256;
298aa0a1e58SJeff Roberson 	else
299aa0a1e58SJeff Roberson 		return 0;
300aa0a1e58SJeff Roberson }
301aa0a1e58SJeff Roberson 
iboe_get_rate(if_t dev)3023e142e07SJustin Hibbits static inline int iboe_get_rate(if_t dev)
303aa0a1e58SJeff Roberson {
3043e142e07SJustin Hibbits 	uint64_t baudrate = if_getbaudrate(dev);
305478d3005SHans Petter Selasky #ifdef if_baudrate_pf
306478d3005SHans Petter Selasky 	int exp;
307478d3005SHans Petter Selasky 	for (exp = dev->if_baudrate_pf; exp > 0; exp--)
308478d3005SHans Petter Selasky 		baudrate *= 10;
309478d3005SHans Petter Selasky #endif
310478d3005SHans Petter Selasky 	if (baudrate >= IF_Gbps(40))
311aa0a1e58SJeff Roberson 		return IB_RATE_40_GBPS;
312478d3005SHans Petter Selasky 	else if (baudrate >= IF_Gbps(30))
313aa0a1e58SJeff Roberson 		return IB_RATE_30_GBPS;
314478d3005SHans Petter Selasky 	else if (baudrate >= IF_Gbps(20))
315aa0a1e58SJeff Roberson 		return IB_RATE_20_GBPS;
316478d3005SHans Petter Selasky 	else if (baudrate >= IF_Gbps(10))
317aa0a1e58SJeff Roberson 		return IB_RATE_10_GBPS;
318aa0a1e58SJeff Roberson 	else
319aa0a1e58SJeff Roberson 		return IB_RATE_PORT_CURRENT;
320aa0a1e58SJeff Roberson }
321aa0a1e58SJeff Roberson 
rdma_link_local_addr(struct in6_addr * addr)322aa0a1e58SJeff Roberson static inline int rdma_link_local_addr(struct in6_addr *addr)
323aa0a1e58SJeff Roberson {
324b5c1e0cbSHans Petter Selasky 	if (addr->s6_addr32[0] == htonl(0xfe800000) &&
325aa0a1e58SJeff Roberson 	    addr->s6_addr32[1] == 0)
326aa0a1e58SJeff Roberson 		return 1;
327aa0a1e58SJeff Roberson 
328aa0a1e58SJeff Roberson 	return 0;
329aa0a1e58SJeff Roberson }
330aa0a1e58SJeff Roberson 
rdma_get_ll_mac(struct in6_addr * addr,u8 * mac)331aa0a1e58SJeff Roberson static inline void rdma_get_ll_mac(struct in6_addr *addr, u8 *mac)
332aa0a1e58SJeff Roberson {
333aa0a1e58SJeff Roberson 	memcpy(mac, &addr->s6_addr[8], 3);
334aa0a1e58SJeff Roberson 	memcpy(mac + 3, &addr->s6_addr[13], 3);
335aa0a1e58SJeff Roberson 	mac[0] ^= 2;
336aa0a1e58SJeff Roberson }
337aa0a1e58SJeff Roberson 
rdma_is_multicast_addr(struct in6_addr * addr)338aa0a1e58SJeff Roberson static inline int rdma_is_multicast_addr(struct in6_addr *addr)
339aa0a1e58SJeff Roberson {
340b70db327SHans Petter Selasky 	__be32 ipv4_addr;
341b70db327SHans Petter Selasky 
342b70db327SHans Petter Selasky 	if (addr->s6_addr[0] == 0xff)
343b70db327SHans Petter Selasky 		return 1;
344b70db327SHans Petter Selasky 
345b70db327SHans Petter Selasky 	ipv4_addr = addr->s6_addr32[3];
346b70db327SHans Petter Selasky 	return (ipv6_addr_v4mapped(addr) && ipv4_is_multicast(ipv4_addr));
347aa0a1e58SJeff Roberson }
348aa0a1e58SJeff Roberson 
rdma_get_mcast_mac(struct in6_addr * addr,u8 * mac)349aa0a1e58SJeff Roberson static inline void rdma_get_mcast_mac(struct in6_addr *addr, u8 *mac)
350aa0a1e58SJeff Roberson {
351aa0a1e58SJeff Roberson 	int i;
352aa0a1e58SJeff Roberson 
353aa0a1e58SJeff Roberson 	mac[0] = 0x33;
354aa0a1e58SJeff Roberson 	mac[1] = 0x33;
355aa0a1e58SJeff Roberson 	for (i = 2; i < 6; ++i)
356aa0a1e58SJeff Roberson 		mac[i] = addr->s6_addr[i + 10];
357aa0a1e58SJeff Roberson }
358aa0a1e58SJeff Roberson 
rdma_get_vlan_id(union ib_gid * dgid)359aa0a1e58SJeff Roberson static inline u16 rdma_get_vlan_id(union ib_gid *dgid)
360aa0a1e58SJeff Roberson {
361aa0a1e58SJeff Roberson 	u16 vid;
362aa0a1e58SJeff Roberson 
363aa0a1e58SJeff Roberson 	vid = dgid->raw[11] << 8 | dgid->raw[12];
364aa0a1e58SJeff Roberson 	return vid < 0x1000 ? vid : 0xffff;
365aa0a1e58SJeff Roberson }
366aa0a1e58SJeff Roberson 
rdma_vlan_dev_real_dev(if_t dev)3673e142e07SJustin Hibbits static inline if_t rdma_vlan_dev_real_dev(if_t dev)
368aa0a1e58SJeff Roberson {
369758a35d0SHans Petter Selasky 	struct epoch_tracker et;
370758a35d0SHans Petter Selasky 
371758a35d0SHans Petter Selasky 	NET_EPOCH_ENTER(et);
3723e142e07SJustin Hibbits 	if (if_gettype(dev) != IFT_ETHER || if_getpcp(dev) == IFNET_PCP_NONE)
373758a35d0SHans Petter Selasky 		dev = VLAN_TRUNKDEV(dev);	/* non prio-tagged traffic */
374758a35d0SHans Petter Selasky 	NET_EPOCH_EXIT(et);
375758a35d0SHans Petter Selasky 	return (dev);
376aa0a1e58SJeff Roberson }
377aa0a1e58SJeff Roberson 
378aa0a1e58SJeff Roberson #endif /* IB_ADDR_H */
379