ib_addr.h revision 263102
1219820Sjeff/* 2219820Sjeff * Copyright (c) 2005 Voltaire Inc. All rights reserved. 3219820Sjeff * Copyright (c) 2005 Intel Corporation. All rights reserved. 4219820Sjeff * 5219820Sjeff * This software is available to you under a choice of one of two 6219820Sjeff * licenses. You may choose to be licensed under the terms of the GNU 7219820Sjeff * General Public License (GPL) Version 2, available from the file 8219820Sjeff * COPYING in the main directory of this source tree, or the 9219820Sjeff * OpenIB.org BSD license below: 10219820Sjeff * 11219820Sjeff * Redistribution and use in source and binary forms, with or 12219820Sjeff * without modification, are permitted provided that the following 13219820Sjeff * conditions are met: 14219820Sjeff * 15219820Sjeff * - Redistributions of source code must retain the above 16219820Sjeff * copyright notice, this list of conditions and the following 17219820Sjeff * disclaimer. 18219820Sjeff * 19219820Sjeff * - Redistributions in binary form must reproduce the above 20219820Sjeff * copyright notice, this list of conditions and the following 21219820Sjeff * disclaimer in the documentation and/or other materials 22219820Sjeff * provided with the distribution. 23219820Sjeff * 24219820Sjeff * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 25219820Sjeff * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 26219820Sjeff * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 27219820Sjeff * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 28219820Sjeff * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 29219820Sjeff * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 30219820Sjeff * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 31219820Sjeff * SOFTWARE. 32219820Sjeff */ 33219820Sjeff 34219820Sjeff#if !defined(IB_ADDR_H) 35219820Sjeff#define IB_ADDR_H 36219820Sjeff 37219820Sjeff#include <linux/in.h> 38219820Sjeff#include <linux/in6.h> 39219820Sjeff#include <linux/if_arp.h> 40219820Sjeff#include <linux/netdevice.h> 41219820Sjeff#include <linux/socket.h> 42219820Sjeff#include <rdma/ib_verbs.h> 43219820Sjeff#include <rdma/ib_pack.h> 44219820Sjeff#include <linux/ethtool.h> 45219820Sjeff#include <linux/if_vlan.h> 46219820Sjeff 47219820Sjeffstruct rdma_addr_client { 48219820Sjeff atomic_t refcount; 49219820Sjeff struct completion comp; 50219820Sjeff}; 51219820Sjeff 52219820Sjeff/** 53219820Sjeff * rdma_addr_register_client - Register an address client. 54219820Sjeff */ 55219820Sjeffvoid rdma_addr_register_client(struct rdma_addr_client *client); 56219820Sjeff 57219820Sjeff/** 58219820Sjeff * rdma_addr_unregister_client - Deregister an address client. 59219820Sjeff * @client: Client object to deregister. 60219820Sjeff */ 61219820Sjeffvoid rdma_addr_unregister_client(struct rdma_addr_client *client); 62219820Sjeff 63219820Sjeffstruct rdma_dev_addr { 64219820Sjeff unsigned char src_dev_addr[MAX_ADDR_LEN]; 65219820Sjeff unsigned char dst_dev_addr[MAX_ADDR_LEN]; 66219820Sjeff unsigned char broadcast[MAX_ADDR_LEN]; 67219820Sjeff unsigned short dev_type; 68219820Sjeff int bound_dev_if; 69219820Sjeff enum rdma_transport_type transport; 70219820Sjeff}; 71219820Sjeff 72219820Sjeff/** 73219820Sjeff * rdma_translate_ip - Translate a local IP address to an RDMA hardware 74219820Sjeff * address. 75219820Sjeff */ 76219820Sjeffint rdma_translate_ip(struct sockaddr *addr, struct rdma_dev_addr *dev_addr); 77219820Sjeff 78219820Sjeff/** 79219820Sjeff * rdma_resolve_ip - Resolve source and destination IP addresses to 80219820Sjeff * RDMA hardware addresses. 81219820Sjeff * @client: Address client associated with request. 82219820Sjeff * @src_addr: An optional source address to use in the resolution. If a 83219820Sjeff * source address is not provided, a usable address will be returned via 84219820Sjeff * the callback. 85219820Sjeff * @dst_addr: The destination address to resolve. 86219820Sjeff * @addr: A reference to a data location that will receive the resolved 87219820Sjeff * addresses. The data location must remain valid until the callback has 88219820Sjeff * been invoked. 89219820Sjeff * @timeout_ms: Amount of time to wait for the address resolution to complete. 90219820Sjeff * @callback: Call invoked once address resolution has completed, timed out, 91219820Sjeff * or been canceled. A status of 0 indicates success. 92219820Sjeff * @context: User-specified context associated with the call. 93219820Sjeff */ 94219820Sjeffint rdma_resolve_ip(struct rdma_addr_client *client, 95219820Sjeff struct sockaddr *src_addr, struct sockaddr *dst_addr, 96219820Sjeff struct rdma_dev_addr *addr, int timeout_ms, 97219820Sjeff void (*callback)(int status, struct sockaddr *src_addr, 98219820Sjeff struct rdma_dev_addr *addr, void *context), 99219820Sjeff void *context); 100219820Sjeff 101219820Sjeffvoid rdma_addr_cancel(struct rdma_dev_addr *addr); 102219820Sjeff 103219820Sjeffint rdma_copy_addr(struct rdma_dev_addr *dev_addr, struct net_device *dev, 104219820Sjeff const unsigned char *dst_dev_addr); 105219820Sjeff 106219820Sjeffstatic inline int ip_addr_size(struct sockaddr *addr) 107219820Sjeff{ 108219820Sjeff return addr->sa_family == AF_INET6 ? 109219820Sjeff sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in); 110219820Sjeff} 111219820Sjeff 112219820Sjeffstatic inline u16 ib_addr_get_pkey(struct rdma_dev_addr *dev_addr) 113219820Sjeff{ 114219820Sjeff return ((u16)dev_addr->broadcast[8] << 8) | (u16)dev_addr->broadcast[9]; 115219820Sjeff} 116219820Sjeff 117219820Sjeffstatic inline void ib_addr_set_pkey(struct rdma_dev_addr *dev_addr, u16 pkey) 118219820Sjeff{ 119219820Sjeff dev_addr->broadcast[8] = pkey >> 8; 120219820Sjeff dev_addr->broadcast[9] = (unsigned char) pkey; 121219820Sjeff} 122219820Sjeff 123219820Sjeffstatic inline void ib_addr_get_mgid(struct rdma_dev_addr *dev_addr, 124219820Sjeff union ib_gid *gid) 125219820Sjeff{ 126219820Sjeff memcpy(gid, dev_addr->broadcast + 4, sizeof *gid); 127219820Sjeff} 128219820Sjeff 129219820Sjeffstatic inline int rdma_addr_gid_offset(struct rdma_dev_addr *dev_addr) 130219820Sjeff{ 131219820Sjeff return dev_addr->dev_type == ARPHRD_INFINIBAND ? 4 : 0; 132219820Sjeff} 133219820Sjeff 134219820Sjeffstatic inline void iboe_mac_vlan_to_ll(union ib_gid *gid, u8 *mac, u16 vid) 135219820Sjeff{ 136219820Sjeff memset(gid->raw, 0, 16); 137219820Sjeff *((u32 *)gid->raw) = cpu_to_be32(0xfe800000); 138219820Sjeff if (vid < 0x1000) { 139219820Sjeff gid->raw[12] = vid & 0xff; 140219820Sjeff gid->raw[11] = vid >> 8; 141219820Sjeff } else { 142219820Sjeff gid->raw[12] = 0xfe; 143219820Sjeff gid->raw[11] = 0xff; 144219820Sjeff } 145219820Sjeff 146219820Sjeff memcpy(gid->raw + 13, mac + 3, 3); 147219820Sjeff memcpy(gid->raw + 8, mac, 3); 148219820Sjeff gid->raw[8] ^= 2; 149219820Sjeff} 150219820Sjeff 151219820Sjeffstatic inline u16 rdma_vlan_dev_vlan_id(const struct net_device *dev) 152219820Sjeff{ 153219820Sjeff#ifdef __linux__ 154219820Sjeff return dev->priv_flags & IFF_802_1Q_VLAN ? 155219820Sjeff vlan_dev_vlan_id(dev) : 0xffff; 156219820Sjeff#else 157219820Sjeff uint16_t tag; 158219820Sjeff 159219820Sjeff if (VLAN_TAG(__DECONST(struct ifnet *, dev), &tag) != 0) 160219820Sjeff return 0xffff; 161219820Sjeff return tag; 162219820Sjeff#endif 163219820Sjeff} 164219820Sjeff 165219820Sjeffstatic inline void iboe_addr_get_sgid(struct rdma_dev_addr *dev_addr, 166219820Sjeff union ib_gid *gid) 167219820Sjeff{ 168219820Sjeff struct net_device *dev; 169219820Sjeff u16 vid = 0xffff; 170219820Sjeff 171219820Sjeff dev = dev_get_by_index(&init_net, dev_addr->bound_dev_if); 172219820Sjeff if (dev) { 173219820Sjeff vid = rdma_vlan_dev_vlan_id(dev); 174219820Sjeff dev_put(dev); 175219820Sjeff } 176219820Sjeff 177219820Sjeff iboe_mac_vlan_to_ll(gid, dev_addr->src_dev_addr, vid); 178219820Sjeff} 179219820Sjeff 180219820Sjeffstatic inline void rdma_addr_get_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid) 181219820Sjeff{ 182219820Sjeff if (dev_addr->transport == RDMA_TRANSPORT_IB && 183219820Sjeff dev_addr->dev_type != ARPHRD_INFINIBAND) 184219820Sjeff iboe_addr_get_sgid(dev_addr, gid); 185219820Sjeff else 186219820Sjeff memcpy(gid, dev_addr->src_dev_addr + 187219820Sjeff rdma_addr_gid_offset(dev_addr), sizeof *gid); 188219820Sjeff} 189219820Sjeff 190219820Sjeffstatic inline void rdma_addr_set_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid) 191219820Sjeff{ 192219820Sjeff memcpy(dev_addr->src_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid); 193219820Sjeff} 194219820Sjeff 195219820Sjeffstatic inline void rdma_addr_get_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid) 196219820Sjeff{ 197219820Sjeff memcpy(gid, dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), sizeof *gid); 198219820Sjeff} 199219820Sjeff 200219820Sjeffstatic inline void rdma_addr_set_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid) 201219820Sjeff{ 202219820Sjeff memcpy(dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid); 203219820Sjeff} 204219820Sjeff 205219820Sjeffstatic inline enum ib_mtu iboe_get_mtu(int mtu) 206219820Sjeff{ 207219820Sjeff /* 208219820Sjeff * reduce IB headers from effective IBoE MTU. 28 stands for 209219820Sjeff * atomic header which is the biggest possible header after BTH 210219820Sjeff */ 211219820Sjeff mtu = mtu - IB_GRH_BYTES - IB_BTH_BYTES - 28; 212219820Sjeff 213219820Sjeff if (mtu >= ib_mtu_enum_to_int(IB_MTU_4096)) 214219820Sjeff return IB_MTU_4096; 215219820Sjeff else if (mtu >= ib_mtu_enum_to_int(IB_MTU_2048)) 216219820Sjeff return IB_MTU_2048; 217219820Sjeff else if (mtu >= ib_mtu_enum_to_int(IB_MTU_1024)) 218219820Sjeff return IB_MTU_1024; 219219820Sjeff else if (mtu >= ib_mtu_enum_to_int(IB_MTU_512)) 220219820Sjeff return IB_MTU_512; 221219820Sjeff else if (mtu >= ib_mtu_enum_to_int(IB_MTU_256)) 222219820Sjeff return IB_MTU_256; 223219820Sjeff else 224219820Sjeff return 0; 225219820Sjeff} 226219820Sjeff 227219820Sjeff#ifdef __linux__ 228219820Sjeffstatic inline int iboe_get_rate(struct net_device *dev) 229219820Sjeff{ 230219820Sjeff struct ethtool_cmd cmd; 231219820Sjeff 232219820Sjeff if (!dev->ethtool_ops || !dev->ethtool_ops->get_settings || 233219820Sjeff dev->ethtool_ops->get_settings(dev, &cmd)) 234219820Sjeff return IB_RATE_PORT_CURRENT; 235219820Sjeff 236219820Sjeff if (cmd.speed >= 40000) 237219820Sjeff return IB_RATE_40_GBPS; 238219820Sjeff else if (cmd.speed >= 30000) 239219820Sjeff return IB_RATE_30_GBPS; 240219820Sjeff else if (cmd.speed >= 20000) 241219820Sjeff return IB_RATE_20_GBPS; 242219820Sjeff else if (cmd.speed >= 10000) 243219820Sjeff return IB_RATE_10_GBPS; 244219820Sjeff else 245219820Sjeff return IB_RATE_PORT_CURRENT; 246219820Sjeff} 247219820Sjeff#else 248219820Sjeffstatic inline int iboe_get_rate(struct net_device *dev) 249219820Sjeff{ 250263102Sglebius if (dev->if_baudrate >= IF_Gbps(40)) 251219820Sjeff return IB_RATE_40_GBPS; 252263102Sglebius else if (dev->if_baudrate >= IF_Gbps(30)) 253219820Sjeff return IB_RATE_30_GBPS; 254263102Sglebius else if (dev->if_baudrate >= IF_Gbps(20)) 255219820Sjeff return IB_RATE_20_GBPS; 256263102Sglebius else if (dev->if_baudrate >= IF_Gbps(10)) 257219820Sjeff return IB_RATE_10_GBPS; 258219820Sjeff else 259219820Sjeff return IB_RATE_PORT_CURRENT; 260219820Sjeff} 261219820Sjeff#endif 262219820Sjeff 263219820Sjeffstatic inline int rdma_link_local_addr(struct in6_addr *addr) 264219820Sjeff{ 265219820Sjeff if (addr->s6_addr32[0] == cpu_to_be32(0xfe800000) && 266219820Sjeff addr->s6_addr32[1] == 0) 267219820Sjeff return 1; 268219820Sjeff 269219820Sjeff return 0; 270219820Sjeff} 271219820Sjeff 272219820Sjeffstatic inline void rdma_get_ll_mac(struct in6_addr *addr, u8 *mac) 273219820Sjeff{ 274219820Sjeff memcpy(mac, &addr->s6_addr[8], 3); 275219820Sjeff memcpy(mac + 3, &addr->s6_addr[13], 3); 276219820Sjeff mac[0] ^= 2; 277219820Sjeff} 278219820Sjeff 279219820Sjeffstatic inline int rdma_is_multicast_addr(struct in6_addr *addr) 280219820Sjeff{ 281219820Sjeff return addr->s6_addr[0] == 0xff; 282219820Sjeff} 283219820Sjeff 284219820Sjeffstatic inline void rdma_get_mcast_mac(struct in6_addr *addr, u8 *mac) 285219820Sjeff{ 286219820Sjeff int i; 287219820Sjeff 288219820Sjeff mac[0] = 0x33; 289219820Sjeff mac[1] = 0x33; 290219820Sjeff for (i = 2; i < 6; ++i) 291219820Sjeff mac[i] = addr->s6_addr[i + 10]; 292219820Sjeff} 293219820Sjeff 294219820Sjeffstatic inline u16 rdma_get_vlan_id(union ib_gid *dgid) 295219820Sjeff{ 296219820Sjeff u16 vid; 297219820Sjeff 298219820Sjeff vid = dgid->raw[11] << 8 | dgid->raw[12]; 299219820Sjeff return vid < 0x1000 ? vid : 0xffff; 300219820Sjeff} 301219820Sjeff 302219820Sjeffstatic inline struct net_device *rdma_vlan_dev_real_dev(const struct net_device *dev) 303219820Sjeff{ 304219820Sjeff#ifdef __linux__ 305219820Sjeff return dev->priv_flags & IFF_802_1Q_VLAN ? 306219820Sjeff vlan_dev_real_dev(dev) : 0; 307219820Sjeff#else 308219820Sjeff return VLAN_TRUNKDEV(__DECONST(struct ifnet *, dev)); 309219820Sjeff#endif 310219820Sjeff} 311219820Sjeff 312219820Sjeff#endif /* IB_ADDR_H */ 313