xref: /freebsd/sys/ofed/include/rdma/ib_addr.h (revision bf8641fedb55b1121814475c73e745c702791da0)
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.
3409938b21SHans Petter Selasky  *
3509938b21SHans Petter Selasky  * $FreeBSD$
36aa0a1e58SJeff Roberson  */
37aa0a1e58SJeff Roberson 
38478d3005SHans Petter Selasky #if !defined(IB_ADDR_H)
39aa0a1e58SJeff Roberson #define IB_ADDR_H
40aa0a1e58SJeff Roberson 
41aa0a1e58SJeff Roberson #include <linux/in.h>
42aa0a1e58SJeff Roberson #include <linux/in6.h>
43aa0a1e58SJeff Roberson #include <linux/if_arp.h>
44aa0a1e58SJeff Roberson #include <linux/netdevice.h>
45b5c1e0cbSHans Petter Selasky #include <linux/inetdevice.h>
46aa0a1e58SJeff Roberson #include <linux/socket.h>
47b5c1e0cbSHans Petter Selasky #include <linux/if_vlan.h>
48478d3005SHans Petter Selasky #include <net/ipv6.h>
49478d3005SHans Petter Selasky #include <net/if_inet6.h>
50478d3005SHans Petter Selasky #include <net/ip.h>
51aa0a1e58SJeff Roberson #include <rdma/ib_verbs.h>
52aa0a1e58SJeff Roberson #include <rdma/ib_pack.h>
53b5c1e0cbSHans Petter Selasky #include <net/ipv6.h>
54aa0a1e58SJeff Roberson 
55aa0a1e58SJeff Roberson struct rdma_addr_client {
56aa0a1e58SJeff Roberson 	atomic_t refcount;
57aa0a1e58SJeff Roberson 	struct completion comp;
58aa0a1e58SJeff Roberson };
59aa0a1e58SJeff Roberson 
60aa0a1e58SJeff Roberson /**
61aa0a1e58SJeff Roberson  * rdma_addr_register_client - Register an address client.
62aa0a1e58SJeff Roberson  */
63aa0a1e58SJeff Roberson void rdma_addr_register_client(struct rdma_addr_client *client);
64aa0a1e58SJeff Roberson 
65aa0a1e58SJeff Roberson /**
66aa0a1e58SJeff Roberson  * rdma_addr_unregister_client - Deregister an address client.
67aa0a1e58SJeff Roberson  * @client: Client object to deregister.
68aa0a1e58SJeff Roberson  */
69aa0a1e58SJeff Roberson void rdma_addr_unregister_client(struct rdma_addr_client *client);
70aa0a1e58SJeff Roberson 
71478d3005SHans Petter Selasky /**
72478d3005SHans Petter Selasky  * struct rdma_dev_addr - Contains resolved RDMA hardware addresses
73478d3005SHans Petter Selasky  * @src_dev_addr:	Source MAC address.
74478d3005SHans Petter Selasky  * @dst_dev_addr:	Destination MAC address.
75478d3005SHans Petter Selasky  * @broadcast:		Broadcast address of the device.
76478d3005SHans Petter Selasky  * @dev_type:		The interface hardware type of the device.
77478d3005SHans Petter Selasky  * @bound_dev_if:	An optional device interface index.
78478d3005SHans Petter Selasky  * @transport:		The transport type used.
79478d3005SHans Petter Selasky  * @net:		Network namespace containing the bound_dev_if net_dev.
80478d3005SHans Petter Selasky  */
81478d3005SHans Petter Selasky struct vnet;
82aa0a1e58SJeff Roberson struct rdma_dev_addr {
83aa0a1e58SJeff Roberson 	unsigned char src_dev_addr[MAX_ADDR_LEN];
84aa0a1e58SJeff Roberson 	unsigned char dst_dev_addr[MAX_ADDR_LEN];
85aa0a1e58SJeff Roberson 	unsigned char broadcast[MAX_ADDR_LEN];
86aa0a1e58SJeff Roberson 	unsigned short dev_type;
87aa0a1e58SJeff Roberson 	int bound_dev_if;
88aa0a1e58SJeff Roberson 	enum rdma_transport_type transport;
89478d3005SHans Petter Selasky 	struct vnet *net;
90478d3005SHans Petter Selasky 	enum rdma_network_type network;
91478d3005SHans Petter Selasky 	int hoplimit;
92aa0a1e58SJeff Roberson };
93aa0a1e58SJeff Roberson 
94aa0a1e58SJeff Roberson /**
95aa0a1e58SJeff Roberson  * rdma_translate_ip - Translate a local IP address to an RDMA hardware
96aa0a1e58SJeff Roberson  *   address.
97478d3005SHans Petter Selasky  *
98*bf8641feSHans Petter Selasky  * The dev_addr->net and dev_addr->bound_dev_if fields must be initialized.
99aa0a1e58SJeff Roberson  */
100478d3005SHans Petter Selasky int rdma_translate_ip(const struct sockaddr *addr,
101*bf8641feSHans Petter Selasky 		      struct rdma_dev_addr *dev_addr);
102aa0a1e58SJeff Roberson 
103aa0a1e58SJeff Roberson /**
104aa0a1e58SJeff Roberson  * rdma_resolve_ip - Resolve source and destination IP addresses to
105aa0a1e58SJeff Roberson  *   RDMA hardware addresses.
106aa0a1e58SJeff Roberson  * @client: Address client associated with request.
107aa0a1e58SJeff Roberson  * @src_addr: An optional source address to use in the resolution.  If a
108aa0a1e58SJeff Roberson  *   source address is not provided, a usable address will be returned via
109aa0a1e58SJeff Roberson  *   the callback.
110aa0a1e58SJeff Roberson  * @dst_addr: The destination address to resolve.
111aa0a1e58SJeff Roberson  * @addr: A reference to a data location that will receive the resolved
112aa0a1e58SJeff Roberson  *   addresses.  The data location must remain valid until the callback has
113478d3005SHans Petter Selasky  *   been invoked. The net field of the addr struct must be valid.
114aa0a1e58SJeff Roberson  * @timeout_ms: Amount of time to wait for the address resolution to complete.
115aa0a1e58SJeff Roberson  * @callback: Call invoked once address resolution has completed, timed out,
116aa0a1e58SJeff Roberson  *   or been canceled.  A status of 0 indicates success.
117aa0a1e58SJeff Roberson  * @context: User-specified context associated with the call.
118aa0a1e58SJeff Roberson  */
119aa0a1e58SJeff Roberson int rdma_resolve_ip(struct rdma_addr_client *client,
120aa0a1e58SJeff Roberson 		    struct sockaddr *src_addr, struct sockaddr *dst_addr,
121aa0a1e58SJeff Roberson 		    struct rdma_dev_addr *addr, int timeout_ms,
122aa0a1e58SJeff Roberson 		    void (*callback)(int status, struct sockaddr *src_addr,
123aa0a1e58SJeff Roberson 				     struct rdma_dev_addr *addr, void *context),
124aa0a1e58SJeff Roberson 		    void *context);
125aa0a1e58SJeff Roberson 
126478d3005SHans Petter Selasky int rdma_resolve_ip_route(struct sockaddr *src_addr,
127478d3005SHans Petter Selasky 			  const struct sockaddr *dst_addr,
128478d3005SHans Petter Selasky 			  struct rdma_dev_addr *addr);
129478d3005SHans Petter Selasky 
130aa0a1e58SJeff Roberson void rdma_addr_cancel(struct rdma_dev_addr *addr);
131aa0a1e58SJeff Roberson 
132aa0a1e58SJeff Roberson int rdma_copy_addr(struct rdma_dev_addr *dev_addr, struct net_device *dev,
133aa0a1e58SJeff Roberson 	      const unsigned char *dst_dev_addr);
134aa0a1e58SJeff Roberson 
135478d3005SHans Petter Selasky int rdma_addr_size(struct sockaddr *addr);
136478d3005SHans Petter Selasky 
137478d3005SHans Petter Selasky int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid,
138478d3005SHans Petter Selasky 				 const union ib_gid *dgid,
139*bf8641feSHans Petter Selasky 				 u8 *smac, struct net_device *dev,
140478d3005SHans Petter Selasky 				 int *hoplimit);
141aa0a1e58SJeff Roberson 
142aa0a1e58SJeff Roberson static inline u16 ib_addr_get_pkey(struct rdma_dev_addr *dev_addr)
143aa0a1e58SJeff Roberson {
144aa0a1e58SJeff Roberson 	return ((u16)dev_addr->broadcast[8] << 8) | (u16)dev_addr->broadcast[9];
145aa0a1e58SJeff Roberson }
146aa0a1e58SJeff Roberson 
147aa0a1e58SJeff Roberson static inline void ib_addr_set_pkey(struct rdma_dev_addr *dev_addr, u16 pkey)
148aa0a1e58SJeff Roberson {
149aa0a1e58SJeff Roberson 	dev_addr->broadcast[8] = pkey >> 8;
150aa0a1e58SJeff Roberson 	dev_addr->broadcast[9] = (unsigned char) pkey;
151aa0a1e58SJeff Roberson }
152aa0a1e58SJeff Roberson 
153aa0a1e58SJeff Roberson static inline void ib_addr_get_mgid(struct rdma_dev_addr *dev_addr,
154aa0a1e58SJeff Roberson 				    union ib_gid *gid)
155aa0a1e58SJeff Roberson {
156aa0a1e58SJeff Roberson 	memcpy(gid, dev_addr->broadcast + 4, sizeof *gid);
157aa0a1e58SJeff Roberson }
158aa0a1e58SJeff Roberson 
159aa0a1e58SJeff Roberson static inline int rdma_addr_gid_offset(struct rdma_dev_addr *dev_addr)
160aa0a1e58SJeff Roberson {
161aa0a1e58SJeff Roberson 	return dev_addr->dev_type == ARPHRD_INFINIBAND ? 4 : 0;
162aa0a1e58SJeff Roberson }
163aa0a1e58SJeff Roberson 
164aa0a1e58SJeff Roberson static inline u16 rdma_vlan_dev_vlan_id(const struct net_device *dev)
165aa0a1e58SJeff Roberson {
166aa0a1e58SJeff Roberson 	uint16_t tag;
167aa0a1e58SJeff Roberson 
168aa0a1e58SJeff Roberson 	if (VLAN_TAG(__DECONST(struct ifnet *, dev), &tag) != 0)
169aa0a1e58SJeff Roberson 		return 0xffff;
170aa0a1e58SJeff Roberson 	return tag;
171aa0a1e58SJeff Roberson }
172aa0a1e58SJeff Roberson 
173478d3005SHans Petter Selasky static inline int rdma_ip2gid(const struct sockaddr *addr, union ib_gid *gid)
174b5c1e0cbSHans Petter Selasky {
175b5c1e0cbSHans Petter Selasky 	switch (addr->sa_family) {
176b5c1e0cbSHans Petter Selasky 	case AF_INET:
177478d3005SHans Petter Selasky 		ipv6_addr_set_v4mapped(((const struct sockaddr_in *)
178478d3005SHans Petter Selasky 					addr)->sin_addr.s_addr,
179b5c1e0cbSHans Petter Selasky 				       (struct in6_addr *)gid);
180b5c1e0cbSHans Petter Selasky 		break;
181b5c1e0cbSHans Petter Selasky 	case AF_INET6:
182478d3005SHans Petter Selasky 		memcpy(gid->raw, &((const struct sockaddr_in6 *)addr)->sin6_addr, 16);
183aacb0377SHans Petter Selasky 		/* make sure scope ID gets zeroed inside GID */
184aacb0377SHans Petter Selasky 		if (IN6_IS_SCOPE_LINKLOCAL((struct in6_addr *)gid->raw) ||
185aacb0377SHans Petter Selasky 		    IN6_IS_ADDR_MC_INTFACELOCAL((struct in6_addr *)gid->raw)) {
186aacb0377SHans Petter Selasky 			gid->raw[2] = 0;
187aacb0377SHans Petter Selasky 			gid->raw[3] = 0;
188aacb0377SHans Petter Selasky 		}
189b5c1e0cbSHans Petter Selasky 		break;
190b5c1e0cbSHans Petter Selasky 	default:
191b5c1e0cbSHans Petter Selasky 		return -EINVAL;
192b5c1e0cbSHans Petter Selasky 	}
193b5c1e0cbSHans Petter Selasky 	return 0;
194b5c1e0cbSHans Petter Selasky }
195b5c1e0cbSHans Petter Selasky 
196b5c1e0cbSHans Petter Selasky /* Important - sockaddr should be a union of sockaddr_in and sockaddr_in6 */
197478d3005SHans Petter Selasky static inline void rdma_gid2ip(struct sockaddr *out, const union ib_gid *gid)
198b5c1e0cbSHans Petter Selasky {
199478d3005SHans Petter Selasky 	if (ipv6_addr_v4mapped((const struct in6_addr *)gid)) {
200b5c1e0cbSHans Petter Selasky 		struct sockaddr_in *out_in = (struct sockaddr_in *)out;
201b5c1e0cbSHans Petter Selasky 		memset(out_in, 0, sizeof(*out_in));
202b5c1e0cbSHans Petter Selasky 		out_in->sin_len = sizeof(*out_in);
203b5c1e0cbSHans Petter Selasky 		out_in->sin_family = AF_INET;
204b5c1e0cbSHans Petter Selasky 		memcpy(&out_in->sin_addr.s_addr, gid->raw + 12, 4);
205b5c1e0cbSHans Petter Selasky 	} else {
206b5c1e0cbSHans Petter Selasky 		struct sockaddr_in6 *out_in = (struct sockaddr_in6 *)out;
207b5c1e0cbSHans Petter Selasky 		memset(out_in, 0, sizeof(*out_in));
2080bab509bSHans Petter Selasky 		out_in->sin6_len = sizeof(*out_in);
209b5c1e0cbSHans Petter Selasky 		out_in->sin6_family = AF_INET6;
210b5c1e0cbSHans Petter Selasky 		memcpy(&out_in->sin6_addr.s6_addr, gid->raw, 16);
211b5c1e0cbSHans Petter Selasky 	}
212b5c1e0cbSHans Petter Selasky }
213b5c1e0cbSHans Petter Selasky 
214aa0a1e58SJeff Roberson static inline void iboe_addr_get_sgid(struct rdma_dev_addr *dev_addr,
215aa0a1e58SJeff Roberson 				      union ib_gid *gid)
216aa0a1e58SJeff Roberson {
217478d3005SHans Petter Selasky 	struct net_device *dev;
218478d3005SHans Petter Selasky 	struct ifaddr *ifa;
219478d3005SHans Petter Selasky 
220478d3005SHans Petter Selasky 	dev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
221478d3005SHans Petter Selasky 	if (dev) {
222478d3005SHans Petter Selasky 		TAILQ_FOREACH(ifa, &dev->if_addrhead, ifa_link) {
223478d3005SHans Petter Selasky 			if (ifa->ifa_addr == NULL ||
224478d3005SHans Petter Selasky 			    ifa->ifa_addr->sa_family != AF_INET)
225478d3005SHans Petter Selasky 				continue;
226478d3005SHans Petter Selasky 			ipv6_addr_set_v4mapped(((struct sockaddr_in *)
227478d3005SHans Petter Selasky 					       ifa->ifa_addr)->sin_addr.s_addr,
228478d3005SHans Petter Selasky 					       (struct in6_addr *)gid);
229478d3005SHans Petter Selasky 			break;
230478d3005SHans Petter Selasky 		}
231478d3005SHans Petter Selasky 		dev_put(dev);
232478d3005SHans Petter Selasky 	}
233aa0a1e58SJeff Roberson }
234aa0a1e58SJeff Roberson 
235aa0a1e58SJeff Roberson static inline void rdma_addr_get_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
236aa0a1e58SJeff Roberson {
237aa0a1e58SJeff Roberson 	if (dev_addr->transport == RDMA_TRANSPORT_IB &&
238aa0a1e58SJeff Roberson 	    dev_addr->dev_type != ARPHRD_INFINIBAND)
239aa0a1e58SJeff Roberson 		iboe_addr_get_sgid(dev_addr, gid);
240aa0a1e58SJeff Roberson 	else
241aa0a1e58SJeff Roberson 		memcpy(gid, dev_addr->src_dev_addr +
242aa0a1e58SJeff Roberson 		       rdma_addr_gid_offset(dev_addr), sizeof *gid);
243aa0a1e58SJeff Roberson }
244aa0a1e58SJeff Roberson 
245aa0a1e58SJeff Roberson static inline void rdma_addr_set_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
246aa0a1e58SJeff Roberson {
247aa0a1e58SJeff Roberson 	memcpy(dev_addr->src_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
248aa0a1e58SJeff Roberson }
249aa0a1e58SJeff Roberson 
250aa0a1e58SJeff Roberson static inline void rdma_addr_get_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
251aa0a1e58SJeff Roberson {
252aa0a1e58SJeff Roberson 	memcpy(gid, dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), sizeof *gid);
253aa0a1e58SJeff Roberson }
254aa0a1e58SJeff Roberson 
255aa0a1e58SJeff Roberson static inline void rdma_addr_set_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
256aa0a1e58SJeff Roberson {
257aa0a1e58SJeff Roberson 	memcpy(dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
258aa0a1e58SJeff Roberson }
259aa0a1e58SJeff Roberson 
260aa0a1e58SJeff Roberson static inline enum ib_mtu iboe_get_mtu(int mtu)
261aa0a1e58SJeff Roberson {
262aa0a1e58SJeff Roberson 	/*
263aa0a1e58SJeff Roberson 	 * reduce IB headers from effective IBoE MTU. 28 stands for
264aa0a1e58SJeff Roberson 	 * atomic header which is the biggest possible header after BTH
265aa0a1e58SJeff Roberson 	 */
266aa0a1e58SJeff Roberson 	mtu = mtu - IB_GRH_BYTES - IB_BTH_BYTES - 28;
267aa0a1e58SJeff Roberson 
268aa0a1e58SJeff Roberson 	if (mtu >= ib_mtu_enum_to_int(IB_MTU_4096))
269aa0a1e58SJeff Roberson 		return IB_MTU_4096;
270aa0a1e58SJeff Roberson 	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_2048))
271aa0a1e58SJeff Roberson 		return IB_MTU_2048;
272aa0a1e58SJeff Roberson 	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_1024))
273aa0a1e58SJeff Roberson 		return IB_MTU_1024;
274aa0a1e58SJeff Roberson 	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_512))
275aa0a1e58SJeff Roberson 		return IB_MTU_512;
276aa0a1e58SJeff Roberson 	else if (mtu >= ib_mtu_enum_to_int(IB_MTU_256))
277aa0a1e58SJeff Roberson 		return IB_MTU_256;
278aa0a1e58SJeff Roberson 	else
279aa0a1e58SJeff Roberson 		return 0;
280aa0a1e58SJeff Roberson }
281aa0a1e58SJeff Roberson 
282aa0a1e58SJeff Roberson static inline int iboe_get_rate(struct net_device *dev)
283aa0a1e58SJeff Roberson {
284478d3005SHans Petter Selasky 	uint64_t baudrate = dev->if_baudrate;
285478d3005SHans Petter Selasky #ifdef if_baudrate_pf
286478d3005SHans Petter Selasky 	int exp;
287478d3005SHans Petter Selasky 	for (exp = dev->if_baudrate_pf; exp > 0; exp--)
288478d3005SHans Petter Selasky 		baudrate *= 10;
289478d3005SHans Petter Selasky #endif
290478d3005SHans Petter Selasky 	if (baudrate >= IF_Gbps(40))
291aa0a1e58SJeff Roberson 		return IB_RATE_40_GBPS;
292478d3005SHans Petter Selasky 	else if (baudrate >= IF_Gbps(30))
293aa0a1e58SJeff Roberson 		return IB_RATE_30_GBPS;
294478d3005SHans Petter Selasky 	else if (baudrate >= IF_Gbps(20))
295aa0a1e58SJeff Roberson 		return IB_RATE_20_GBPS;
296478d3005SHans Petter Selasky 	else if (baudrate >= IF_Gbps(10))
297aa0a1e58SJeff Roberson 		return IB_RATE_10_GBPS;
298aa0a1e58SJeff Roberson 	else
299aa0a1e58SJeff Roberson 		return IB_RATE_PORT_CURRENT;
300aa0a1e58SJeff Roberson }
301aa0a1e58SJeff Roberson 
302aa0a1e58SJeff Roberson static inline int rdma_link_local_addr(struct in6_addr *addr)
303aa0a1e58SJeff Roberson {
304b5c1e0cbSHans Petter Selasky 	if (addr->s6_addr32[0] == htonl(0xfe800000) &&
305aa0a1e58SJeff Roberson 	    addr->s6_addr32[1] == 0)
306aa0a1e58SJeff Roberson 		return 1;
307aa0a1e58SJeff Roberson 
308aa0a1e58SJeff Roberson 	return 0;
309aa0a1e58SJeff Roberson }
310aa0a1e58SJeff Roberson 
311aa0a1e58SJeff Roberson static inline void rdma_get_ll_mac(struct in6_addr *addr, u8 *mac)
312aa0a1e58SJeff Roberson {
313aa0a1e58SJeff Roberson 	memcpy(mac, &addr->s6_addr[8], 3);
314aa0a1e58SJeff Roberson 	memcpy(mac + 3, &addr->s6_addr[13], 3);
315aa0a1e58SJeff Roberson 	mac[0] ^= 2;
316aa0a1e58SJeff Roberson }
317aa0a1e58SJeff Roberson 
318aa0a1e58SJeff Roberson static inline int rdma_is_multicast_addr(struct in6_addr *addr)
319aa0a1e58SJeff Roberson {
320aa0a1e58SJeff Roberson 	return addr->s6_addr[0] == 0xff;
321aa0a1e58SJeff Roberson }
322aa0a1e58SJeff Roberson 
323aa0a1e58SJeff Roberson static inline void rdma_get_mcast_mac(struct in6_addr *addr, u8 *mac)
324aa0a1e58SJeff Roberson {
325aa0a1e58SJeff Roberson 	int i;
326aa0a1e58SJeff Roberson 
327aa0a1e58SJeff Roberson 	mac[0] = 0x33;
328aa0a1e58SJeff Roberson 	mac[1] = 0x33;
329aa0a1e58SJeff Roberson 	for (i = 2; i < 6; ++i)
330aa0a1e58SJeff Roberson 		mac[i] = addr->s6_addr[i + 10];
331aa0a1e58SJeff Roberson }
332aa0a1e58SJeff Roberson 
333aa0a1e58SJeff Roberson static inline u16 rdma_get_vlan_id(union ib_gid *dgid)
334aa0a1e58SJeff Roberson {
335aa0a1e58SJeff Roberson 	u16 vid;
336aa0a1e58SJeff Roberson 
337aa0a1e58SJeff Roberson 	vid = dgid->raw[11] << 8 | dgid->raw[12];
338aa0a1e58SJeff Roberson 	return vid < 0x1000 ? vid : 0xffff;
339aa0a1e58SJeff Roberson }
340aa0a1e58SJeff Roberson 
341aa0a1e58SJeff Roberson static inline struct net_device *rdma_vlan_dev_real_dev(const struct net_device *dev)
342aa0a1e58SJeff Roberson {
343aa0a1e58SJeff Roberson 	return VLAN_TRUNKDEV(__DECONST(struct ifnet *, dev));
344aa0a1e58SJeff Roberson }
345aa0a1e58SJeff Roberson 
346aa0a1e58SJeff Roberson #endif /* IB_ADDR_H */
347