ib_addr.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /* SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB */
  2. /*
  3. * Copyright (c) 2005 Voltaire Inc. All rights reserved.
  4. * Copyright (c) 2005 Intel Corporation. All rights reserved.
  5. */
  6. #ifndef IB_ADDR_H
  7. #define IB_ADDR_H
  8. #include <linux/ethtool.h>
  9. #include <linux/in.h>
  10. #include <linux/in6.h>
  11. #include <linux/if_arp.h>
  12. #include <linux/netdevice.h>
  13. #include <linux/inetdevice.h>
  14. #include <linux/socket.h>
  15. #include <linux/if_vlan.h>
  16. #include <net/ipv6.h>
  17. #include <net/if_inet6.h>
  18. #include <net/ip.h>
  19. #include <rdma/ib_verbs.h>
  20. #include <rdma/ib_pack.h>
  21. #include <net/net_namespace.h>
  22. /**
  23. * struct rdma_dev_addr - Contains resolved RDMA hardware addresses
  24. * @src_dev_addr: Source MAC address.
  25. * @dst_dev_addr: Destination MAC address.
  26. * @broadcast: Broadcast address of the device.
  27. * @dev_type: The interface hardware type of the device.
  28. * @bound_dev_if: An optional device interface index.
  29. * @transport: The transport type used.
  30. * @net: Network namespace containing the bound_dev_if net_dev.
  31. * @sgid_attr: GID attribute to use for identified SGID
  32. */
  33. struct rdma_dev_addr {
  34. unsigned char src_dev_addr[MAX_ADDR_LEN];
  35. unsigned char dst_dev_addr[MAX_ADDR_LEN];
  36. unsigned char broadcast[MAX_ADDR_LEN];
  37. unsigned short dev_type;
  38. int bound_dev_if;
  39. enum rdma_transport_type transport;
  40. struct net *net;
  41. const struct ib_gid_attr *sgid_attr;
  42. enum rdma_network_type network;
  43. int hoplimit;
  44. };
  45. /**
  46. * rdma_translate_ip - Translate a local IP address to an RDMA hardware
  47. * address.
  48. *
  49. * The dev_addr->net field must be initialized.
  50. */
  51. int rdma_translate_ip(const struct sockaddr *addr,
  52. struct rdma_dev_addr *dev_addr);
  53. /**
  54. * rdma_resolve_ip - Resolve source and destination IP addresses to
  55. * RDMA hardware addresses.
  56. * @src_addr: An optional source address to use in the resolution. If a
  57. * source address is not provided, a usable address will be returned via
  58. * the callback.
  59. * @dst_addr: The destination address to resolve.
  60. * @addr: A reference to a data location that will receive the resolved
  61. * addresses. The data location must remain valid until the callback has
  62. * been invoked. The net field of the addr struct must be valid.
  63. * @timeout_ms: Amount of time to wait for the address resolution to complete.
  64. * @callback: Call invoked once address resolution has completed, timed out,
  65. * or been canceled. A status of 0 indicates success.
  66. * @resolve_by_gid_attr: Resolve the ip based on the GID attribute from
  67. * rdma_dev_addr.
  68. * @context: User-specified context associated with the call.
  69. */
  70. int rdma_resolve_ip(struct sockaddr *src_addr, const struct sockaddr *dst_addr,
  71. struct rdma_dev_addr *addr, unsigned long timeout_ms,
  72. void (*callback)(int status, struct sockaddr *src_addr,
  73. struct rdma_dev_addr *addr, void *context),
  74. bool resolve_by_gid_attr, void *context);
  75. void rdma_addr_cancel(struct rdma_dev_addr *addr);
  76. int rdma_addr_size(const struct sockaddr *addr);
  77. int rdma_addr_size_in6(struct sockaddr_in6 *addr);
  78. int rdma_addr_size_kss(struct __kernel_sockaddr_storage *addr);
  79. static inline u16 ib_addr_get_pkey(struct rdma_dev_addr *dev_addr)
  80. {
  81. return ((u16)dev_addr->broadcast[8] << 8) | (u16)dev_addr->broadcast[9];
  82. }
  83. static inline void ib_addr_set_pkey(struct rdma_dev_addr *dev_addr, u16 pkey)
  84. {
  85. dev_addr->broadcast[8] = pkey >> 8;
  86. dev_addr->broadcast[9] = (unsigned char) pkey;
  87. }
  88. static inline void ib_addr_get_mgid(struct rdma_dev_addr *dev_addr,
  89. union ib_gid *gid)
  90. {
  91. memcpy(gid, dev_addr->broadcast + 4, sizeof *gid);
  92. }
  93. static inline int rdma_addr_gid_offset(struct rdma_dev_addr *dev_addr)
  94. {
  95. return dev_addr->dev_type == ARPHRD_INFINIBAND ? 4 : 0;
  96. }
  97. static inline u16 rdma_vlan_dev_vlan_id(const struct net_device *dev)
  98. {
  99. return is_vlan_dev(dev) ? vlan_dev_vlan_id(dev) : 0xffff;
  100. }
  101. static inline int rdma_ip2gid(struct sockaddr *addr, union ib_gid *gid)
  102. {
  103. switch (addr->sa_family) {
  104. case AF_INET:
  105. ipv6_addr_set_v4mapped(((struct sockaddr_in *)
  106. addr)->sin_addr.s_addr,
  107. (struct in6_addr *)gid);
  108. break;
  109. case AF_INET6:
  110. *(struct in6_addr *)&gid->raw =
  111. ((struct sockaddr_in6 *)addr)->sin6_addr;
  112. break;
  113. default:
  114. return -EINVAL;
  115. }
  116. return 0;
  117. }
  118. /* Important - sockaddr should be a union of sockaddr_in and sockaddr_in6 */
  119. static inline void rdma_gid2ip(struct sockaddr *out, const union ib_gid *gid)
  120. {
  121. if (ipv6_addr_v4mapped((struct in6_addr *)gid)) {
  122. struct sockaddr_in *out_in = (struct sockaddr_in *)out;
  123. memset(out_in, 0, sizeof(*out_in));
  124. out_in->sin_family = AF_INET;
  125. memcpy(&out_in->sin_addr.s_addr, gid->raw + 12, 4);
  126. } else {
  127. struct sockaddr_in6 *out_in = (struct sockaddr_in6 *)out;
  128. memset(out_in, 0, sizeof(*out_in));
  129. out_in->sin6_family = AF_INET6;
  130. memcpy(&out_in->sin6_addr.s6_addr, gid->raw, 16);
  131. }
  132. }
  133. /*
  134. * rdma_get/set_sgid/dgid() APIs are applicable to IB, and iWarp.
  135. * They are not applicable to RoCE.
  136. * RoCE GIDs are derived from the IP addresses.
  137. */
  138. static inline void rdma_addr_get_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
  139. {
  140. memcpy(gid, dev_addr->src_dev_addr + rdma_addr_gid_offset(dev_addr),
  141. sizeof(*gid));
  142. }
  143. static inline void rdma_addr_set_sgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
  144. {
  145. memcpy(dev_addr->src_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
  146. }
  147. static inline void rdma_addr_get_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
  148. {
  149. memcpy(gid, dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), sizeof *gid);
  150. }
  151. static inline void rdma_addr_set_dgid(struct rdma_dev_addr *dev_addr, union ib_gid *gid)
  152. {
  153. memcpy(dev_addr->dst_dev_addr + rdma_addr_gid_offset(dev_addr), gid, sizeof *gid);
  154. }
  155. static inline enum ib_mtu iboe_get_mtu(int mtu)
  156. {
  157. /*
  158. * Reduce IB headers from effective IBoE MTU.
  159. */
  160. mtu = mtu - (IB_GRH_BYTES + IB_UDP_BYTES + IB_BTH_BYTES +
  161. IB_EXT_XRC_BYTES + IB_EXT_ATOMICETH_BYTES +
  162. IB_ICRC_BYTES);
  163. if (mtu >= ib_mtu_enum_to_int(IB_MTU_4096))
  164. return IB_MTU_4096;
  165. else if (mtu >= ib_mtu_enum_to_int(IB_MTU_2048))
  166. return IB_MTU_2048;
  167. else if (mtu >= ib_mtu_enum_to_int(IB_MTU_1024))
  168. return IB_MTU_1024;
  169. else if (mtu >= ib_mtu_enum_to_int(IB_MTU_512))
  170. return IB_MTU_512;
  171. else if (mtu >= ib_mtu_enum_to_int(IB_MTU_256))
  172. return IB_MTU_256;
  173. else
  174. return 0;
  175. }
  176. static inline int rdma_link_local_addr(struct in6_addr *addr)
  177. {
  178. if (addr->s6_addr32[0] == htonl(0xfe800000) &&
  179. addr->s6_addr32[1] == 0)
  180. return 1;
  181. return 0;
  182. }
  183. static inline void rdma_get_ll_mac(struct in6_addr *addr, u8 *mac)
  184. {
  185. memcpy(mac, &addr->s6_addr[8], 3);
  186. memcpy(mac + 3, &addr->s6_addr[13], 3);
  187. mac[0] ^= 2;
  188. }
  189. static inline int rdma_is_multicast_addr(struct in6_addr *addr)
  190. {
  191. __be32 ipv4_addr;
  192. if (addr->s6_addr[0] == 0xff)
  193. return 1;
  194. ipv4_addr = addr->s6_addr32[3];
  195. return (ipv6_addr_v4mapped(addr) && ipv4_is_multicast(ipv4_addr));
  196. }
  197. static inline void rdma_get_mcast_mac(struct in6_addr *addr, u8 *mac)
  198. {
  199. int i;
  200. mac[0] = 0x33;
  201. mac[1] = 0x33;
  202. for (i = 2; i < 6; ++i)
  203. mac[i] = addr->s6_addr[i + 10];
  204. }
  205. static inline u16 rdma_get_vlan_id(union ib_gid *dgid)
  206. {
  207. u16 vid;
  208. vid = dgid->raw[11] << 8 | dgid->raw[12];
  209. return vid < 0x1000 ? vid : 0xffff;
  210. }
  211. static inline struct net_device *rdma_vlan_dev_real_dev(const struct net_device *dev)
  212. {
  213. return is_vlan_dev(dev) ? vlan_dev_real_dev(dev) : NULL;
  214. }
  215. #endif /* IB_ADDR_H */