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