ib_pack.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. /*
  2. * Copyright (c) 2004 Topspin Corporation. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * OpenIB.org BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. */
  32. #ifndef IB_PACK_H
  33. #define IB_PACK_H
  34. #include <rdma/ib_verbs.h>
  35. #include <uapi/linux/if_ether.h>
  36. enum {
  37. IB_LRH_BYTES = 8,
  38. IB_ETH_BYTES = 14,
  39. IB_VLAN_BYTES = 4,
  40. IB_GRH_BYTES = 40,
  41. IB_IP4_BYTES = 20,
  42. IB_UDP_BYTES = 8,
  43. IB_BTH_BYTES = 12,
  44. IB_DETH_BYTES = 8,
  45. IB_EXT_ATOMICETH_BYTES = 28,
  46. IB_EXT_XRC_BYTES = 4,
  47. IB_ICRC_BYTES = 4
  48. };
  49. struct ib_field {
  50. size_t struct_offset_bytes;
  51. size_t struct_size_bytes;
  52. int offset_words;
  53. int offset_bits;
  54. int size_bits;
  55. char *field_name;
  56. };
  57. #define RESERVED \
  58. .field_name = "reserved"
  59. /*
  60. * This macro cleans up the definitions of constants for BTH opcodes.
  61. * It is used to define constants such as IB_OPCODE_UD_SEND_ONLY,
  62. * which becomes IB_OPCODE_UD + IB_OPCODE_SEND_ONLY, and this gives
  63. * the correct value.
  64. *
  65. * In short, user code should use the constants defined using the
  66. * macro rather than worrying about adding together other constants.
  67. */
  68. #define IB_OPCODE(transport, op) \
  69. IB_OPCODE_ ## transport ## _ ## op = \
  70. IB_OPCODE_ ## transport + IB_OPCODE_ ## op
  71. enum {
  72. /* transport types -- just used to define real constants */
  73. IB_OPCODE_RC = 0x00,
  74. IB_OPCODE_UC = 0x20,
  75. IB_OPCODE_RD = 0x40,
  76. IB_OPCODE_UD = 0x60,
  77. /* per IBTA 1.3 vol 1 Table 38, A10.3.2 */
  78. IB_OPCODE_CNP = 0x80,
  79. /* operations -- just used to define real constants */
  80. IB_OPCODE_SEND_FIRST = 0x00,
  81. IB_OPCODE_SEND_MIDDLE = 0x01,
  82. IB_OPCODE_SEND_LAST = 0x02,
  83. IB_OPCODE_SEND_LAST_WITH_IMMEDIATE = 0x03,
  84. IB_OPCODE_SEND_ONLY = 0x04,
  85. IB_OPCODE_SEND_ONLY_WITH_IMMEDIATE = 0x05,
  86. IB_OPCODE_RDMA_WRITE_FIRST = 0x06,
  87. IB_OPCODE_RDMA_WRITE_MIDDLE = 0x07,
  88. IB_OPCODE_RDMA_WRITE_LAST = 0x08,
  89. IB_OPCODE_RDMA_WRITE_LAST_WITH_IMMEDIATE = 0x09,
  90. IB_OPCODE_RDMA_WRITE_ONLY = 0x0a,
  91. IB_OPCODE_RDMA_WRITE_ONLY_WITH_IMMEDIATE = 0x0b,
  92. IB_OPCODE_RDMA_READ_REQUEST = 0x0c,
  93. IB_OPCODE_RDMA_READ_RESPONSE_FIRST = 0x0d,
  94. IB_OPCODE_RDMA_READ_RESPONSE_MIDDLE = 0x0e,
  95. IB_OPCODE_RDMA_READ_RESPONSE_LAST = 0x0f,
  96. IB_OPCODE_RDMA_READ_RESPONSE_ONLY = 0x10,
  97. IB_OPCODE_ACKNOWLEDGE = 0x11,
  98. IB_OPCODE_ATOMIC_ACKNOWLEDGE = 0x12,
  99. IB_OPCODE_COMPARE_SWAP = 0x13,
  100. IB_OPCODE_FETCH_ADD = 0x14,
  101. /* opcode 0x15 is reserved */
  102. IB_OPCODE_SEND_LAST_WITH_INVALIDATE = 0x16,
  103. IB_OPCODE_SEND_ONLY_WITH_INVALIDATE = 0x17,
  104. /* real constants follow -- see comment about above IB_OPCODE()
  105. macro for more details */
  106. /* RC */
  107. IB_OPCODE(RC, SEND_FIRST),
  108. IB_OPCODE(RC, SEND_MIDDLE),
  109. IB_OPCODE(RC, SEND_LAST),
  110. IB_OPCODE(RC, SEND_LAST_WITH_IMMEDIATE),
  111. IB_OPCODE(RC, SEND_ONLY),
  112. IB_OPCODE(RC, SEND_ONLY_WITH_IMMEDIATE),
  113. IB_OPCODE(RC, RDMA_WRITE_FIRST),
  114. IB_OPCODE(RC, RDMA_WRITE_MIDDLE),
  115. IB_OPCODE(RC, RDMA_WRITE_LAST),
  116. IB_OPCODE(RC, RDMA_WRITE_LAST_WITH_IMMEDIATE),
  117. IB_OPCODE(RC, RDMA_WRITE_ONLY),
  118. IB_OPCODE(RC, RDMA_WRITE_ONLY_WITH_IMMEDIATE),
  119. IB_OPCODE(RC, RDMA_READ_REQUEST),
  120. IB_OPCODE(RC, RDMA_READ_RESPONSE_FIRST),
  121. IB_OPCODE(RC, RDMA_READ_RESPONSE_MIDDLE),
  122. IB_OPCODE(RC, RDMA_READ_RESPONSE_LAST),
  123. IB_OPCODE(RC, RDMA_READ_RESPONSE_ONLY),
  124. IB_OPCODE(RC, ACKNOWLEDGE),
  125. IB_OPCODE(RC, ATOMIC_ACKNOWLEDGE),
  126. IB_OPCODE(RC, COMPARE_SWAP),
  127. IB_OPCODE(RC, FETCH_ADD),
  128. IB_OPCODE(RC, SEND_LAST_WITH_INVALIDATE),
  129. IB_OPCODE(RC, SEND_ONLY_WITH_INVALIDATE),
  130. /* UC */
  131. IB_OPCODE(UC, SEND_FIRST),
  132. IB_OPCODE(UC, SEND_MIDDLE),
  133. IB_OPCODE(UC, SEND_LAST),
  134. IB_OPCODE(UC, SEND_LAST_WITH_IMMEDIATE),
  135. IB_OPCODE(UC, SEND_ONLY),
  136. IB_OPCODE(UC, SEND_ONLY_WITH_IMMEDIATE),
  137. IB_OPCODE(UC, RDMA_WRITE_FIRST),
  138. IB_OPCODE(UC, RDMA_WRITE_MIDDLE),
  139. IB_OPCODE(UC, RDMA_WRITE_LAST),
  140. IB_OPCODE(UC, RDMA_WRITE_LAST_WITH_IMMEDIATE),
  141. IB_OPCODE(UC, RDMA_WRITE_ONLY),
  142. IB_OPCODE(UC, RDMA_WRITE_ONLY_WITH_IMMEDIATE),
  143. /* RD */
  144. IB_OPCODE(RD, SEND_FIRST),
  145. IB_OPCODE(RD, SEND_MIDDLE),
  146. IB_OPCODE(RD, SEND_LAST),
  147. IB_OPCODE(RD, SEND_LAST_WITH_IMMEDIATE),
  148. IB_OPCODE(RD, SEND_ONLY),
  149. IB_OPCODE(RD, SEND_ONLY_WITH_IMMEDIATE),
  150. IB_OPCODE(RD, RDMA_WRITE_FIRST),
  151. IB_OPCODE(RD, RDMA_WRITE_MIDDLE),
  152. IB_OPCODE(RD, RDMA_WRITE_LAST),
  153. IB_OPCODE(RD, RDMA_WRITE_LAST_WITH_IMMEDIATE),
  154. IB_OPCODE(RD, RDMA_WRITE_ONLY),
  155. IB_OPCODE(RD, RDMA_WRITE_ONLY_WITH_IMMEDIATE),
  156. IB_OPCODE(RD, RDMA_READ_REQUEST),
  157. IB_OPCODE(RD, RDMA_READ_RESPONSE_FIRST),
  158. IB_OPCODE(RD, RDMA_READ_RESPONSE_MIDDLE),
  159. IB_OPCODE(RD, RDMA_READ_RESPONSE_LAST),
  160. IB_OPCODE(RD, RDMA_READ_RESPONSE_ONLY),
  161. IB_OPCODE(RD, ACKNOWLEDGE),
  162. IB_OPCODE(RD, ATOMIC_ACKNOWLEDGE),
  163. IB_OPCODE(RD, COMPARE_SWAP),
  164. IB_OPCODE(RD, FETCH_ADD),
  165. /* UD */
  166. IB_OPCODE(UD, SEND_ONLY),
  167. IB_OPCODE(UD, SEND_ONLY_WITH_IMMEDIATE)
  168. };
  169. enum {
  170. IB_LNH_RAW = 0,
  171. IB_LNH_IP = 1,
  172. IB_LNH_IBA_LOCAL = 2,
  173. IB_LNH_IBA_GLOBAL = 3
  174. };
  175. struct ib_unpacked_lrh {
  176. u8 virtual_lane;
  177. u8 link_version;
  178. u8 service_level;
  179. u8 link_next_header;
  180. __be16 destination_lid;
  181. __be16 packet_length;
  182. __be16 source_lid;
  183. };
  184. struct ib_unpacked_grh {
  185. u8 ip_version;
  186. u8 traffic_class;
  187. __be32 flow_label;
  188. __be16 payload_length;
  189. u8 next_header;
  190. u8 hop_limit;
  191. union ib_gid source_gid;
  192. union ib_gid destination_gid;
  193. };
  194. struct ib_unpacked_bth {
  195. u8 opcode;
  196. u8 solicited_event;
  197. u8 mig_req;
  198. u8 pad_count;
  199. u8 transport_header_version;
  200. __be16 pkey;
  201. __be32 destination_qpn;
  202. u8 ack_req;
  203. __be32 psn;
  204. };
  205. struct ib_unpacked_deth {
  206. __be32 qkey;
  207. __be32 source_qpn;
  208. };
  209. struct ib_unpacked_eth {
  210. u8 dmac_h[4];
  211. u8 dmac_l[2];
  212. u8 smac_h[2];
  213. u8 smac_l[4];
  214. __be16 type;
  215. };
  216. struct ib_unpacked_ip4 {
  217. u8 ver;
  218. u8 hdr_len;
  219. u8 tos;
  220. __be16 tot_len;
  221. __be16 id;
  222. __be16 frag_off;
  223. u8 ttl;
  224. u8 protocol;
  225. __sum16 check;
  226. __be32 saddr;
  227. __be32 daddr;
  228. };
  229. struct ib_unpacked_udp {
  230. __be16 sport;
  231. __be16 dport;
  232. __be16 length;
  233. __be16 csum;
  234. };
  235. struct ib_unpacked_vlan {
  236. __be16 tag;
  237. __be16 type;
  238. };
  239. struct ib_ud_header {
  240. int lrh_present;
  241. struct ib_unpacked_lrh lrh;
  242. int eth_present;
  243. struct ib_unpacked_eth eth;
  244. int vlan_present;
  245. struct ib_unpacked_vlan vlan;
  246. int grh_present;
  247. struct ib_unpacked_grh grh;
  248. int ipv4_present;
  249. struct ib_unpacked_ip4 ip4;
  250. int udp_present;
  251. struct ib_unpacked_udp udp;
  252. struct ib_unpacked_bth bth;
  253. struct ib_unpacked_deth deth;
  254. int immediate_present;
  255. __be32 immediate_data;
  256. };
  257. void ib_pack(const struct ib_field *desc,
  258. int desc_len,
  259. void *structure,
  260. void *buf);
  261. void ib_unpack(const struct ib_field *desc,
  262. int desc_len,
  263. void *buf,
  264. void *structure);
  265. __sum16 ib_ud_ip4_csum(struct ib_ud_header *header);
  266. int ib_ud_header_init(int payload_bytes,
  267. int lrh_present,
  268. int eth_present,
  269. int vlan_present,
  270. int grh_present,
  271. int ip_version,
  272. int udp_present,
  273. int immediate_present,
  274. struct ib_ud_header *header);
  275. int ib_ud_header_pack(struct ib_ud_header *header,
  276. void *buf);
  277. int ib_ud_header_unpack(void *buf,
  278. struct ib_ud_header *header);
  279. #endif /* IB_PACK_H */