1/* 2 * Copyright (c) 2005 Voltaire Inc. All rights reserved. 3 * Copyright (c) 2005 Intel Corporation. All rights reserved. 4 * 5 * This software is available to you under a choice of one of two 6 * licenses. You may choose to be licensed under the terms of the GNU 7 * General Public License (GPL) Version 2, available from the file 8 * COPYING in the main directory of this source tree, or the 9 * OpenIB.org BSD license below: 10 * 11 * Redistribution and use in source and binary forms, with or 12 * without modification, are permitted provided that the following 13 * conditions are met: 14 * 15 * - Redistributions of source code must retain the above 16 * copyright notice, this list of conditions and the following 17 * disclaimer. 18 * 19 * - Redistributions in binary form must reproduce the above 20 * copyright notice, this list of conditions and the following 21 * disclaimer in the documentation and/or other materials 22 * provided with the distribution. 23 * 24 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 28 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 29 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 30 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31 * SOFTWARE. 32 * 33 * $FreeBSD: stable/11/sys/ofed/include/rdma/ib_addr.h 338612 2018-09-12 08:56:08Z hselasky $ 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/inetdevice.h> 44#include <linux/socket.h> 45#include <linux/if_vlan.h> 46#include <net/ipv6.h> 47#include <net/if_inet6.h> 48#include <net/ip.h> 49#include <rdma/ib_verbs.h> 50#include <rdma/ib_pack.h> 51#include <net/ipv6.h> 52 53struct rdma_addr_client { 54 atomic_t refcount; 55 struct completion comp; 56}; 57 58/** 59 * rdma_addr_register_client - Register an address client. 60 */ 61void rdma_addr_register_client(struct rdma_addr_client *client); 62 63/** 64 * rdma_addr_unregister_client - Deregister an address client. 65 * @client: Client object to deregister. 66 */ 67void rdma_addr_unregister_client(struct rdma_addr_client *client); 68 69/** 70 * struct rdma_dev_addr - Contains resolved RDMA hardware addresses 71 * @src_dev_addr: Source MAC address. 72 * @dst_dev_addr: Destination MAC address. 73 * @broadcast: Broadcast address of the device. 74 * @dev_type: The interface hardware type of the device. 75 * @bound_dev_if: An optional device interface index. 76 * @transport: The transport type used. 77 * @net: Network namespace containing the bound_dev_if net_dev. 78 */ 79struct vnet; 80struct rdma_dev_addr { 81 unsigned char src_dev_addr[MAX_ADDR_LEN]; 82 unsigned char dst_dev_addr[MAX_ADDR_LEN]; 83 unsigned char broadcast[MAX_ADDR_LEN]; 84 unsigned short dev_type; 85 int bound_dev_if; 86 enum rdma_transport_type transport; 87 struct vnet *net; 88 enum rdma_network_type network; 89 int hoplimit; 90}; 91 92/** 93 * rdma_translate_ip - Translate a local IP address to an RDMA hardware 94 * address. 95 * 96 * The dev_addr->net and dev_addr->bound_dev_if fields must be initialized. 97 */ 98int rdma_translate_ip(const struct sockaddr *addr, 99 struct rdma_dev_addr *dev_addr); 100 101/** 102 * rdma_resolve_ip - Resolve source and destination IP addresses to 103 * RDMA hardware addresses. 104 * @client: Address client associated with request. 105 * @src_addr: An optional source address to use in the resolution. If a 106 * source address is not provided, a usable address will be returned via 107 * the callback. 108 * @dst_addr: The destination address to resolve. 109 * @addr: A reference to a data location that will receive the resolved 110 * addresses. The data location must remain valid until the callback has 111 * been invoked. The net field of the addr struct must be valid. 112 * @timeout_ms: Amount of time to wait for the address resolution to complete. 113 * @callback: Call invoked once address resolution has completed, timed out, 114 * or been canceled. A status of 0 indicates success. 115 * @context: User-specified context associated with the call. 116 */ 117int rdma_resolve_ip(struct rdma_addr_client *client, 118 struct sockaddr *src_addr, struct sockaddr *dst_addr, 119 struct rdma_dev_addr *addr, int timeout_ms, 120 void (*callback)(int status, struct sockaddr *src_addr, 121 struct rdma_dev_addr *addr, void *context), 122 void *context); 123 124int rdma_resolve_ip_route(struct sockaddr *src_addr, 125 const struct sockaddr *dst_addr, 126 struct rdma_dev_addr *addr); 127 128void rdma_addr_cancel(struct rdma_dev_addr *addr); 129 130int rdma_copy_addr(struct rdma_dev_addr *dev_addr, struct net_device *dev, 131 const unsigned char *dst_dev_addr); 132 133int rdma_addr_size(struct sockaddr *addr); 134int rdma_addr_size_in6(struct sockaddr_in6 *addr); 135int rdma_addr_size_kss(struct sockaddr_storage *addr); 136 137int rdma_addr_find_l2_eth_by_grh(const union ib_gid *sgid, 138 const union ib_gid *dgid, 139 u8 *smac, struct net_device *dev, 140 int *hoplimit); 141 142static inline u16 ib_addr_get_pkey(struct rdma_dev_addr *dev_addr) 143{ 144 return ((u16)dev_addr->broadcast[8] << 8) | (u16)dev_addr->broadcast[9]; 145} 146 147static inline void ib_addr_set_pkey(struct rdma_dev_addr *dev_addr, u16 pkey) 148{ 149 dev_addr->broadcast[8] = pkey >> 8; 150 dev_addr->broadcast[9] = (unsigned char) pkey; 151} 152 153static inline void ib_addr_get_mgid(struct rdma_dev_addr *dev_addr, 154 union ib_gid *gid) 155{ 156 memcpy(gid, dev_addr->broadcast + 4, sizeof *gid); 157} 158 159static inline int rdma_addr_gid_offset(struct rdma_dev_addr *dev_addr) 160{ 161 return dev_addr->dev_type == ARPHRD_INFINIBAND ? 4 : 0; 162} 163 164static inline u16 rdma_vlan_dev_vlan_id(const struct net_device *dev) 165{ 166 uint16_t tag; 167 168 if (dev->if_type == IFT_ETHER && dev->if_pcp != IFNET_PCP_NONE) 169 return 0x0000; /* prio-tagged traffic */ 170 if (VLAN_TAG(__DECONST(struct ifnet *, dev), &tag) != 0) 171 return 0xffff; 172 return tag; 173} 174 175static inline int rdma_ip2gid(const struct sockaddr *addr, union ib_gid *gid) 176{ 177 switch (addr->sa_family) { 178 case AF_INET: 179 ipv6_addr_set_v4mapped(((const struct sockaddr_in *) 180 addr)->sin_addr.s_addr, 181 (struct in6_addr *)gid); 182 break; 183 case AF_INET6: 184 memcpy(gid->raw, &((const struct sockaddr_in6 *)addr)->sin6_addr, 16); 185 /* make sure scope ID gets zeroed inside GID */ 186 if (IN6_IS_SCOPE_LINKLOCAL((struct in6_addr *)gid->raw) || 187 IN6_IS_ADDR_MC_INTFACELOCAL((struct in6_addr *)gid->raw)) { 188 gid->raw[2] = 0; 189 gid->raw[3] = 0; 190 } 191 break; 192 default: 193 return -EINVAL; 194 } 195 return 0; 196} 197 198/* Important - sockaddr should be a union of sockaddr_in and sockaddr_in6 */ 199static inline void rdma_gid2ip(struct sockaddr *out, const union ib_gid *gid) 200{ 201 if (ipv6_addr_v4mapped((const struct in6_addr *)gid)) { 202 struct sockaddr_in *out_in = (struct sockaddr_in *)out; 203 memset(out_in, 0, sizeof(*out_in)); 204 out_in->sin_len = sizeof(*out_in); 205 out_in->sin_family = AF_INET; 206 memcpy(&out_in->sin_addr.s_addr, gid->raw + 12, 4); 207 } else { 208 struct sockaddr_in6 *out_in = (struct sockaddr_in6 *)out; 209 memset(out_in, 0, sizeof(*out_in)); 210 out_in->sin6_len = sizeof(*out_in); 211 out_in->sin6_family = AF_INET6; 212 memcpy(&out_in->sin6_addr.s6_addr, gid->raw, 16); 213 } 214} 215 216static inline void iboe_addr_get_sgid(struct rdma_dev_addr *dev_addr, 217 union ib_gid *gid) 218{ 219 struct net_device *dev; 220 struct ifaddr *ifa; 221 222#ifdef VIMAGE 223 if (dev_addr->net == NULL) 224 return; 225#endif 226 dev = dev_get_by_index(dev_addr->net, dev_addr->bound_dev_if); 227 if (dev) { 228 TAILQ_FOREACH(ifa, &dev->if_addrhead, ifa_link) { 229 if (ifa->ifa_addr == NULL || 230 ifa->ifa_addr->sa_family != AF_INET) 231 continue; 232 ipv6_addr_set_v4mapped(((struct sockaddr_in *) 233 ifa->ifa_addr)->sin_addr.s_addr, 234 (struct in6_addr *)gid); 235 break; 236 } 237 dev_put(dev); 238 } 239} 240 241static inline void rdma_addr_get_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid) 242{ 243 if (dev_addr->transport == RDMA_TRANSPORT_IB && 244 dev_addr->dev_type != ARPHRD_INFINIBAND) 245 iboe_addr_get_sgid(dev_addr, gid); 246 else 247 memcpy(gid, dev_addr->src_dev_addr + 248 rdma_addr_gid_offset(dev_addr), sizeof *gid); 249} 250 251static inline void rdma_addr_set_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid) 252{ 253 memcpy(dev_addr->src_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid); 254} 255 256static inline void rdma_addr_get_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid) 257{ 258 memcpy(gid, dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), sizeof *gid); 259} 260 261static inline void rdma_addr_set_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid) 262{ 263 memcpy(dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid); 264} 265 266static inline enum ib_mtu iboe_get_mtu(int mtu) 267{ 268 /* 269 * reduce IB headers from effective IBoE MTU. 28 stands for 270 * atomic header which is the biggest possible header after BTH 271 */ 272 mtu = mtu - IB_GRH_BYTES - IB_BTH_BYTES - 28; 273 274 if (mtu >= ib_mtu_enum_to_int(IB_MTU_4096)) 275 return IB_MTU_4096; 276 else if (mtu >= ib_mtu_enum_to_int(IB_MTU_2048)) 277 return IB_MTU_2048; 278 else if (mtu >= ib_mtu_enum_to_int(IB_MTU_1024)) 279 return IB_MTU_1024; 280 else if (mtu >= ib_mtu_enum_to_int(IB_MTU_512)) 281 return IB_MTU_512; 282 else if (mtu >= ib_mtu_enum_to_int(IB_MTU_256)) 283 return IB_MTU_256; 284 else 285 return 0; 286} 287 288static inline int iboe_get_rate(struct net_device *dev) 289{ 290 uint64_t baudrate = dev->if_baudrate; 291#ifdef if_baudrate_pf 292 int exp; 293 for (exp = dev->if_baudrate_pf; exp > 0; exp--) 294 baudrate *= 10; 295#endif 296 if (baudrate >= IF_Gbps(40)) 297 return IB_RATE_40_GBPS; 298 else if (baudrate >= IF_Gbps(30)) 299 return IB_RATE_30_GBPS; 300 else if (baudrate >= IF_Gbps(20)) 301 return IB_RATE_20_GBPS; 302 else if (baudrate >= IF_Gbps(10)) 303 return IB_RATE_10_GBPS; 304 else 305 return IB_RATE_PORT_CURRENT; 306} 307 308static inline int rdma_link_local_addr(struct in6_addr *addr) 309{ 310 if (addr->s6_addr32[0] == htonl(0xfe800000) && 311 addr->s6_addr32[1] == 0) 312 return 1; 313 314 return 0; 315} 316 317static inline void rdma_get_ll_mac(struct in6_addr *addr, u8 *mac) 318{ 319 memcpy(mac, &addr->s6_addr[8], 3); 320 memcpy(mac + 3, &addr->s6_addr[13], 3); 321 mac[0] ^= 2; 322} 323 324static inline int rdma_is_multicast_addr(struct in6_addr *addr) 325{ 326 __be32 ipv4_addr; 327 328 if (addr->s6_addr[0] == 0xff) 329 return 1; 330 331 ipv4_addr = addr->s6_addr32[3]; 332 return (ipv6_addr_v4mapped(addr) && ipv4_is_multicast(ipv4_addr)); 333} 334 335static inline void rdma_get_mcast_mac(struct in6_addr *addr, u8 *mac) 336{ 337 int i; 338 339 mac[0] = 0x33; 340 mac[1] = 0x33; 341 for (i = 2; i < 6; ++i) 342 mac[i] = addr->s6_addr[i + 10]; 343} 344 345static inline u16 rdma_get_vlan_id(union ib_gid *dgid) 346{ 347 u16 vid; 348 349 vid = dgid->raw[11] << 8 | dgid->raw[12]; 350 return vid < 0x1000 ? vid : 0xffff; 351} 352 353static inline struct net_device *rdma_vlan_dev_real_dev(struct net_device *dev) 354{ 355 if (dev->if_type == IFT_ETHER && dev->if_pcp != IFNET_PCP_NONE) 356 return dev; /* prio-tagged traffic */ 357 return VLAN_TRUNKDEV(__DECONST(struct ifnet *, dev)); 358} 359 360#endif /* IB_ADDR_H */ 361