2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
4 * Copyright (c) 2005 Voltaire Inc. All rights reserved.
5 * Copyright (c) 2005 Intel Corporation. All rights reserved.
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:
13 * Redistribution and use in source and binary forms, with or
14 * without modification, are permitted provided that the following
17 * - Redistributions of source code must retain the above
18 * copyright notice, this list of conditions and the following
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.
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
38 #if !defined(IB_ADDR_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>
49 #include <net/if_inet6.h>
51 #include <rdma/ib_verbs.h>
52 #include <rdma/ib_pack.h>
56 struct rdma_addr_client {
58 struct completion comp;
62 * rdma_addr_register_client - Register an address client.
64 void rdma_addr_register_client(struct rdma_addr_client *client);
67 * rdma_addr_unregister_client - Deregister an address client.
68 * @client: Client object to deregister.
70 void rdma_addr_unregister_client(struct rdma_addr_client *client);
73 * struct rdma_dev_addr - Contains resolved RDMA hardware addresses
74 * @src_dev_addr: Source MAC address.
75 * @dst_dev_addr: Destination MAC address.
76 * @broadcast: Broadcast address of the device.
77 * @dev_type: The interface hardware type of the device.
78 * @bound_dev_if: An optional device interface index.
79 * @transport: The transport type used.
80 * @net: Network namespace containing the bound_dev_if net_dev.
83 struct rdma_dev_addr {
84 unsigned char src_dev_addr[MAX_ADDR_LEN];
85 unsigned char dst_dev_addr[MAX_ADDR_LEN];
86 unsigned char broadcast[MAX_ADDR_LEN];
87 unsigned short dev_type;
89 enum rdma_transport_type transport;
91 enum rdma_network_type network;
96 * rdma_translate_ip - Translate a local IP address to an RDMA hardware
99 * The dev_addr->net and dev_addr->bound_dev_if fields must be initialized.
101 int rdma_translate_ip(const struct sockaddr *addr,
102 struct rdma_dev_addr *dev_addr);
105 * rdma_resolve_ip - Resolve source and destination IP addresses to
106 * RDMA hardware addresses.
107 * @client: Address client associated with request.
108 * @src_addr: An optional source address to use in the resolution. If a
109 * source address is not provided, a usable address will be returned via
111 * @dst_addr: The destination address to resolve.
112 * @addr: A reference to a data location that will receive the resolved
113 * addresses. The data location must remain valid until the callback has
114 * been invoked. The net field of the addr struct must be valid.
115 * @timeout_ms: Amount of time to wait for the address resolution to complete.
116 * @callback: Call invoked once address resolution has completed, timed out,
117 * or been canceled. A status of 0 indicates success.
118 * @context: User-specified context associated with the call.
120 int rdma_resolve_ip(struct rdma_addr_client *client,
121 struct sockaddr *src_addr, struct sockaddr *dst_addr,
122 struct rdma_dev_addr *addr, int timeout_ms,
123 void (*callback)(int status, struct sockaddr *src_addr,
124 struct rdma_dev_addr *addr, void *context),
127 int rdma_resolve_ip_route(struct sockaddr *src_addr,
128 const struct sockaddr *dst_addr,
129 struct rdma_dev_addr *addr);
131 void rdma_addr_cancel(struct rdma_dev_addr *addr);
133 int rdma_copy_addr(struct rdma_dev_addr *dev_addr, struct net_device *dev,
134 const unsigned char *dst_dev_addr);
136 int rdma_addr_size(struct sockaddr *addr);
137 int rdma_addr_size_in6(struct sockaddr_in6 *addr);
138 int rdma_addr_size_kss(struct sockaddr_storage *addr);
140 int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid,
141 const union ib_gid *dgid,
142 u8 *smac, struct net_device *dev,
145 static inline u16 ib_addr_get_pkey(struct rdma_dev_addr *dev_addr)
147 return ((u16)dev_addr->broadcast[8] << 8) | (u16)dev_addr->broadcast[9];
150 static inline void ib_addr_set_pkey(struct rdma_dev_addr *dev_addr, u16 pkey)
152 dev_addr->broadcast[8] = pkey >> 8;
153 dev_addr->broadcast[9] = (unsigned char) pkey;
156 static inline void ib_addr_get_mgid(struct rdma_dev_addr *dev_addr,
159 memcpy(gid, dev_addr->broadcast + 4, sizeof *gid);
162 static inline int rdma_addr_gid_offset(struct rdma_dev_addr *dev_addr)
164 return dev_addr->dev_type == ARPHRD_INFINIBAND ? 4 : 0;
167 static inline u16 rdma_vlan_dev_vlan_id(const struct net_device *dev)
171 if (dev->if_pcp != IFNET_PCP_NONE)
172 return 0x0000; /* prio-tagged traffic */
173 if (VLAN_TAG(__DECONST(struct ifnet *, dev), &tag) != 0)
178 static inline int rdma_ip2gid(const struct sockaddr *addr, union ib_gid *gid)
180 switch (addr->sa_family) {
182 ipv6_addr_set_v4mapped(((const struct sockaddr_in *)
183 addr)->sin_addr.s_addr,
184 (struct in6_addr *)gid);
187 memcpy(gid->raw, &((const struct sockaddr_in6 *)addr)->sin6_addr, 16);
188 /* make sure scope ID gets zeroed inside GID */
189 if (IN6_IS_SCOPE_LINKLOCAL((struct in6_addr *)gid->raw) ||
190 IN6_IS_ADDR_MC_INTFACELOCAL((struct in6_addr *)gid->raw)) {
201 /* Important - sockaddr should be a union of sockaddr_in and sockaddr_in6 */
202 static inline void rdma_gid2ip(struct sockaddr *out, const union ib_gid *gid)
204 if (ipv6_addr_v4mapped((const struct in6_addr *)gid)) {
205 struct sockaddr_in *out_in = (struct sockaddr_in *)out;
206 memset(out_in, 0, sizeof(*out_in));
207 out_in->sin_len = sizeof(*out_in);
208 out_in->sin_family = AF_INET;
209 memcpy(&out_in->sin_addr.s_addr, gid->raw + 12, 4);
211 struct sockaddr_in6 *out_in = (struct sockaddr_in6 *)out;
212 memset(out_in, 0, sizeof(*out_in));
213 out_in->sin6_len = sizeof(*out_in);
214 out_in->sin6_family = AF_INET6;
215 memcpy(&out_in->sin6_addr.s6_addr, gid->raw, 16);
219 static inline void iboe_addr_get_sgid(struct rdma_dev_addr *dev_addr,
222 struct net_device *dev;
225 dev = dev_get_by_index(&init_net, dev_addr->bound_dev_if);
227 CK_STAILQ_FOREACH(ifa, &dev->if_addrhead, ifa_link) {
228 if (ifa->ifa_addr == NULL ||
229 ifa->ifa_addr->sa_family != AF_INET)
231 ipv6_addr_set_v4mapped(((struct sockaddr_in *)
232 ifa->ifa_addr)->sin_addr.s_addr,
233 (struct in6_addr *)gid);
240 static inline void rdma_addr_get_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
242 if (dev_addr->transport == RDMA_TRANSPORT_IB &&
243 dev_addr->dev_type != ARPHRD_INFINIBAND)
244 iboe_addr_get_sgid(dev_addr, gid);
246 memcpy(gid, dev_addr->src_dev_addr +
247 rdma_addr_gid_offset(dev_addr), sizeof *gid);
250 static inline void rdma_addr_set_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
252 memcpy(dev_addr->src_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
255 static inline void rdma_addr_get_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
257 memcpy(gid, dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), sizeof *gid);
260 static inline void rdma_addr_set_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
262 memcpy(dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
265 static inline enum ib_mtu iboe_get_mtu(int mtu)
268 * reduce IB headers from effective IBoE MTU. 28 stands for
269 * atomic header which is the biggest possible header after BTH
271 mtu = mtu - IB_GRH_BYTES - IB_BTH_BYTES - 28;
273 if (mtu >= ib_mtu_enum_to_int(IB_MTU_4096))
275 else if (mtu >= ib_mtu_enum_to_int(IB_MTU_2048))
277 else if (mtu >= ib_mtu_enum_to_int(IB_MTU_1024))
279 else if (mtu >= ib_mtu_enum_to_int(IB_MTU_512))
281 else if (mtu >= ib_mtu_enum_to_int(IB_MTU_256))
287 static inline int iboe_get_rate(struct net_device *dev)
289 uint64_t baudrate = dev->if_baudrate;
290 #ifdef if_baudrate_pf
292 for (exp = dev->if_baudrate_pf; exp > 0; exp--)
295 if (baudrate >= IF_Gbps(40))
296 return IB_RATE_40_GBPS;
297 else if (baudrate >= IF_Gbps(30))
298 return IB_RATE_30_GBPS;
299 else if (baudrate >= IF_Gbps(20))
300 return IB_RATE_20_GBPS;
301 else if (baudrate >= IF_Gbps(10))
302 return IB_RATE_10_GBPS;
304 return IB_RATE_PORT_CURRENT;
307 static inline int rdma_link_local_addr(struct in6_addr *addr)
309 if (addr->s6_addr32[0] == htonl(0xfe800000) &&
310 addr->s6_addr32[1] == 0)
316 static inline void rdma_get_ll_mac(struct in6_addr *addr, u8 *mac)
318 memcpy(mac, &addr->s6_addr[8], 3);
319 memcpy(mac + 3, &addr->s6_addr[13], 3);
323 static inline int rdma_is_multicast_addr(struct in6_addr *addr)
327 if (addr->s6_addr[0] == 0xff)
330 ipv4_addr = addr->s6_addr32[3];
331 return (ipv6_addr_v4mapped(addr) && ipv4_is_multicast(ipv4_addr));
334 static inline void rdma_get_mcast_mac(struct in6_addr *addr, u8 *mac)
340 for (i = 2; i < 6; ++i)
341 mac[i] = addr->s6_addr[i + 10];
344 static inline u16 rdma_get_vlan_id(union ib_gid *dgid)
348 vid = dgid->raw[11] << 8 | dgid->raw[12];
349 return vid < 0x1000 ? vid : 0xffff;
352 static inline struct net_device *rdma_vlan_dev_real_dev(struct net_device *dev)
354 if (dev->if_pcp != IFNET_PCP_NONE)
355 return dev; /* prio-tagged traffic */
356 return VLAN_TRUNKDEV(__DECONST(struct ifnet *, dev));
359 #endif /* IB_ADDR_H */