Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
0002 /*
0003  * Copyright (c) 2005 Voltaire Inc.  All rights reserved.
0004  * Copyright (c) 2005 Intel Corporation.  All rights reserved.
0005  */
0006 
0007 #ifndef IB_ADDR_H
0008 #define IB_ADDR_H
0009 
0010 #include <linux/ethtool.h>
0011 #include <linux/in.h>
0012 #include <linux/in6.h>
0013 #include <linux/if_arp.h>
0014 #include <linux/netdevice.h>
0015 #include <linux/inetdevice.h>
0016 #include <linux/socket.h>
0017 #include <linux/if_vlan.h>
0018 #include <net/ipv6.h>
0019 #include <net/if_inet6.h>
0020 #include <net/ip.h>
0021 #include <rdma/ib_verbs.h>
0022 #include <rdma/ib_pack.h>
0023 #include <net/net_namespace.h>
0024 
0025 /**
0026  * struct rdma_dev_addr - Contains resolved RDMA hardware addresses
0027  * @src_dev_addr:   Source MAC address.
0028  * @dst_dev_addr:   Destination MAC address.
0029  * @broadcast:      Broadcast address of the device.
0030  * @dev_type:       The interface hardware type of the device.
0031  * @bound_dev_if:   An optional device interface index.
0032  * @transport:      The transport type used.
0033  * @net:        Network namespace containing the bound_dev_if net_dev.
0034  * @sgid_attr:      GID attribute to use for identified SGID
0035  */
0036 struct rdma_dev_addr {
0037     unsigned char src_dev_addr[MAX_ADDR_LEN];
0038     unsigned char dst_dev_addr[MAX_ADDR_LEN];
0039     unsigned char broadcast[MAX_ADDR_LEN];
0040     unsigned short dev_type;
0041     int bound_dev_if;
0042     enum rdma_transport_type transport;
0043     struct net *net;
0044     const struct ib_gid_attr *sgid_attr;
0045     enum rdma_network_type network;
0046     int hoplimit;
0047 };
0048 
0049 /**
0050  * rdma_translate_ip - Translate a local IP address to an RDMA hardware
0051  *   address.
0052  *
0053  * The dev_addr->net field must be initialized.
0054  */
0055 int rdma_translate_ip(const struct sockaddr *addr,
0056               struct rdma_dev_addr *dev_addr);
0057 
0058 /**
0059  * rdma_resolve_ip - Resolve source and destination IP addresses to
0060  *   RDMA hardware addresses.
0061  * @src_addr: An optional source address to use in the resolution.  If a
0062  *   source address is not provided, a usable address will be returned via
0063  *   the callback.
0064  * @dst_addr: The destination address to resolve.
0065  * @addr: A reference to a data location that will receive the resolved
0066  *   addresses.  The data location must remain valid until the callback has
0067  *   been invoked. The net field of the addr struct must be valid.
0068  * @timeout_ms: Amount of time to wait for the address resolution to complete.
0069  * @callback: Call invoked once address resolution has completed, timed out,
0070  *   or been canceled.  A status of 0 indicates success.
0071  * @resolve_by_gid_attr:    Resolve the ip based on the GID attribute from
0072  *              rdma_dev_addr.
0073  * @context: User-specified context associated with the call.
0074  */
0075 int rdma_resolve_ip(struct sockaddr *src_addr, const struct sockaddr *dst_addr,
0076             struct rdma_dev_addr *addr, unsigned long timeout_ms,
0077             void (*callback)(int status, struct sockaddr *src_addr,
0078                      struct rdma_dev_addr *addr, void *context),
0079             bool resolve_by_gid_attr, void *context);
0080 
0081 void rdma_addr_cancel(struct rdma_dev_addr *addr);
0082 
0083 int rdma_addr_size(const struct sockaddr *addr);
0084 int rdma_addr_size_in6(struct sockaddr_in6 *addr);
0085 int rdma_addr_size_kss(struct __kernel_sockaddr_storage *addr);
0086 
0087 static inline u16 ib_addr_get_pkey(struct rdma_dev_addr *dev_addr)
0088 {
0089     return ((u16)dev_addr->broadcast[8] << 8) | (u16)dev_addr->broadcast[9];
0090 }
0091 
0092 static inline void ib_addr_set_pkey(struct rdma_dev_addr *dev_addr, u16 pkey)
0093 {
0094     dev_addr->broadcast[8] = pkey >> 8;
0095     dev_addr->broadcast[9] = (unsigned char) pkey;
0096 }
0097 
0098 static inline void ib_addr_get_mgid(struct rdma_dev_addr *dev_addr,
0099                     union ib_gid *gid)
0100 {
0101     memcpy(gid, dev_addr->broadcast + 4, sizeof *gid);
0102 }
0103 
0104 static inline int rdma_addr_gid_offset(struct rdma_dev_addr *dev_addr)
0105 {
0106     return dev_addr->dev_type == ARPHRD_INFINIBAND ? 4 : 0;
0107 }
0108 
0109 static inline u16 rdma_vlan_dev_vlan_id(const struct net_device *dev)
0110 {
0111     return is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 0xffff;
0112 }
0113 
0114 static inline int rdma_ip2gid(struct sockaddr *addr, union ib_gid *gid)
0115 {
0116     switch (addr->sa_family) {
0117     case AF_INET:
0118         ipv6_addr_set_v4mapped(((struct sockaddr_in *)
0119                     addr)->sin_addr.s_addr,
0120                        (struct in6_addr *)gid);
0121         break;
0122     case AF_INET6:
0123         *(struct in6_addr *)&gid->raw =
0124             ((struct sockaddr_in6 *)addr)->sin6_addr;
0125         break;
0126     default:
0127         return -EINVAL;
0128     }
0129     return 0;
0130 }
0131 
0132 /* Important - sockaddr should be a union of sockaddr_in and sockaddr_in6 */
0133 static inline void rdma_gid2ip(struct sockaddr *out, const union ib_gid *gid)
0134 {
0135     if (ipv6_addr_v4mapped((struct in6_addr *)gid)) {
0136         struct sockaddr_in *out_in = (struct sockaddr_in *)out;
0137         memset(out_in, 0, sizeof(*out_in));
0138         out_in->sin_family = AF_INET;
0139         memcpy(&out_in->sin_addr.s_addr, gid->raw + 12, 4);
0140     } else {
0141         struct sockaddr_in6 *out_in = (struct sockaddr_in6 *)out;
0142         memset(out_in, 0, sizeof(*out_in));
0143         out_in->sin6_family = AF_INET6;
0144         memcpy(&out_in->sin6_addr.s6_addr, gid->raw, 16);
0145     }
0146 }
0147 
0148 /*
0149  * rdma_get/set_sgid/dgid() APIs are applicable to IB, and iWarp.
0150  * They are not applicable to RoCE.
0151  * RoCE GIDs are derived from the IP addresses.
0152  */
0153 static inline void rdma_addr_get_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
0154 {
0155     memcpy(gid, dev_addr->src_dev_addr + rdma_addr_gid_offset(dev_addr),
0156            sizeof(*gid));
0157 }
0158 
0159 static inline void rdma_addr_set_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
0160 {
0161     memcpy(dev_addr->src_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
0162 }
0163 
0164 static inline void rdma_addr_get_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
0165 {
0166     memcpy(gid, dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), sizeof *gid);
0167 }
0168 
0169 static inline void rdma_addr_set_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
0170 {
0171     memcpy(dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
0172 }
0173 
0174 static inline enum ib_mtu iboe_get_mtu(int mtu)
0175 {
0176     /*
0177      * Reduce IB headers from effective IBoE MTU.
0178      */
0179     mtu = mtu - (IB_GRH_BYTES + IB_UDP_BYTES + IB_BTH_BYTES +
0180              IB_EXT_XRC_BYTES + IB_EXT_ATOMICETH_BYTES +
0181              IB_ICRC_BYTES);
0182 
0183     if (mtu >= ib_mtu_enum_to_int(IB_MTU_4096))
0184         return IB_MTU_4096;
0185     else if (mtu >= ib_mtu_enum_to_int(IB_MTU_2048))
0186         return IB_MTU_2048;
0187     else if (mtu >= ib_mtu_enum_to_int(IB_MTU_1024))
0188         return IB_MTU_1024;
0189     else if (mtu >= ib_mtu_enum_to_int(IB_MTU_512))
0190         return IB_MTU_512;
0191     else if (mtu >= ib_mtu_enum_to_int(IB_MTU_256))
0192         return IB_MTU_256;
0193     else
0194         return 0;
0195 }
0196 
0197 static inline int iboe_get_rate(struct net_device *dev)
0198 {
0199     struct ethtool_link_ksettings cmd;
0200     int err;
0201 
0202     rtnl_lock();
0203     err = __ethtool_get_link_ksettings(dev, &cmd);
0204     rtnl_unlock();
0205     if (err)
0206         return IB_RATE_PORT_CURRENT;
0207 
0208     if (cmd.base.speed >= 40000)
0209         return IB_RATE_40_GBPS;
0210     else if (cmd.base.speed >= 30000)
0211         return IB_RATE_30_GBPS;
0212     else if (cmd.base.speed >= 20000)
0213         return IB_RATE_20_GBPS;
0214     else if (cmd.base.speed >= 10000)
0215         return IB_RATE_10_GBPS;
0216     else
0217         return IB_RATE_PORT_CURRENT;
0218 }
0219 
0220 static inline int rdma_link_local_addr(struct in6_addr *addr)
0221 {
0222     if (addr->s6_addr32[0] == htonl(0xfe800000) &&
0223         addr->s6_addr32[1] == 0)
0224         return 1;
0225 
0226     return 0;
0227 }
0228 
0229 static inline void rdma_get_ll_mac(struct in6_addr *addr, u8 *mac)
0230 {
0231     memcpy(mac, &addr->s6_addr[8], 3);
0232     memcpy(mac + 3, &addr->s6_addr[13], 3);
0233     mac[0] ^= 2;
0234 }
0235 
0236 static inline int rdma_is_multicast_addr(struct in6_addr *addr)
0237 {
0238     __be32 ipv4_addr;
0239 
0240     if (addr->s6_addr[0] == 0xff)
0241         return 1;
0242 
0243     ipv4_addr = addr->s6_addr32[3];
0244     return (ipv6_addr_v4mapped(addr) && ipv4_is_multicast(ipv4_addr));
0245 }
0246 
0247 static inline void rdma_get_mcast_mac(struct in6_addr *addr, u8 *mac)
0248 {
0249     int i;
0250 
0251     mac[0] = 0x33;
0252     mac[1] = 0x33;
0253     for (i = 2; i < 6; ++i)
0254         mac[i] = addr->s6_addr[i + 10];
0255 }
0256 
0257 static inline u16 rdma_get_vlan_id(union ib_gid *dgid)
0258 {
0259     u16 vid;
0260 
0261     vid = dgid->raw[11] << 8 | dgid->raw[12];
0262     return vid < 0x1000 ? vid : 0xffff;
0263 }
0264 
0265 static inline struct net_device *rdma_vlan_dev_real_dev(const struct net_device *dev)
0266 {
0267     return is_vlan_dev(dev) ? vlan_dev_real_dev(dev) : NULL;
0268 }
0269 
0270 #endif /* IB_ADDR_H */