ud_header.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /*
  2. * Copyright (c) 2004 Topspin Corporation. All rights reserved.
  3. * Copyright (c) 2005 Sun Microsystems, Inc. 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. #include <linux/errno.h>
  34. #include <linux/string.h>
  35. #include <linux/if_ether.h>
  36. #include <rdma/ib_pack.h>
  37. #define STRUCT_FIELD(header, field) \
  38. .struct_offset_bytes = offsetof(struct ib_unpacked_ ## header, field), \
  39. .struct_size_bytes = sizeof ((struct ib_unpacked_ ## header *) 0)->field, \
  40. .field_name = #header ":" #field
  41. static const struct ib_field lrh_table[] = {
  42. { STRUCT_FIELD(lrh, virtual_lane),
  43. .offset_words = 0,
  44. .offset_bits = 0,
  45. .size_bits = 4 },
  46. { STRUCT_FIELD(lrh, link_version),
  47. .offset_words = 0,
  48. .offset_bits = 4,
  49. .size_bits = 4 },
  50. { STRUCT_FIELD(lrh, service_level),
  51. .offset_words = 0,
  52. .offset_bits = 8,
  53. .size_bits = 4 },
  54. { RESERVED,
  55. .offset_words = 0,
  56. .offset_bits = 12,
  57. .size_bits = 2 },
  58. { STRUCT_FIELD(lrh, link_next_header),
  59. .offset_words = 0,
  60. .offset_bits = 14,
  61. .size_bits = 2 },
  62. { STRUCT_FIELD(lrh, destination_lid),
  63. .offset_words = 0,
  64. .offset_bits = 16,
  65. .size_bits = 16 },
  66. { RESERVED,
  67. .offset_words = 1,
  68. .offset_bits = 0,
  69. .size_bits = 5 },
  70. { STRUCT_FIELD(lrh, packet_length),
  71. .offset_words = 1,
  72. .offset_bits = 5,
  73. .size_bits = 11 },
  74. { STRUCT_FIELD(lrh, source_lid),
  75. .offset_words = 1,
  76. .offset_bits = 16,
  77. .size_bits = 16 }
  78. };
  79. static const struct ib_field eth_table[] = {
  80. { STRUCT_FIELD(eth, dmac_h),
  81. .offset_words = 0,
  82. .offset_bits = 0,
  83. .size_bits = 32 },
  84. { STRUCT_FIELD(eth, dmac_l),
  85. .offset_words = 1,
  86. .offset_bits = 0,
  87. .size_bits = 16 },
  88. { STRUCT_FIELD(eth, smac_h),
  89. .offset_words = 1,
  90. .offset_bits = 16,
  91. .size_bits = 16 },
  92. { STRUCT_FIELD(eth, smac_l),
  93. .offset_words = 2,
  94. .offset_bits = 0,
  95. .size_bits = 32 },
  96. { STRUCT_FIELD(eth, type),
  97. .offset_words = 3,
  98. .offset_bits = 0,
  99. .size_bits = 16 }
  100. };
  101. static const struct ib_field vlan_table[] = {
  102. { STRUCT_FIELD(vlan, tag),
  103. .offset_words = 0,
  104. .offset_bits = 0,
  105. .size_bits = 16 },
  106. { STRUCT_FIELD(vlan, type),
  107. .offset_words = 0,
  108. .offset_bits = 16,
  109. .size_bits = 16 }
  110. };
  111. static const struct ib_field grh_table[] = {
  112. { STRUCT_FIELD(grh, ip_version),
  113. .offset_words = 0,
  114. .offset_bits = 0,
  115. .size_bits = 4 },
  116. { STRUCT_FIELD(grh, traffic_class),
  117. .offset_words = 0,
  118. .offset_bits = 4,
  119. .size_bits = 8 },
  120. { STRUCT_FIELD(grh, flow_label),
  121. .offset_words = 0,
  122. .offset_bits = 12,
  123. .size_bits = 20 },
  124. { STRUCT_FIELD(grh, payload_length),
  125. .offset_words = 1,
  126. .offset_bits = 0,
  127. .size_bits = 16 },
  128. { STRUCT_FIELD(grh, next_header),
  129. .offset_words = 1,
  130. .offset_bits = 16,
  131. .size_bits = 8 },
  132. { STRUCT_FIELD(grh, hop_limit),
  133. .offset_words = 1,
  134. .offset_bits = 24,
  135. .size_bits = 8 },
  136. { STRUCT_FIELD(grh, source_gid),
  137. .offset_words = 2,
  138. .offset_bits = 0,
  139. .size_bits = 128 },
  140. { STRUCT_FIELD(grh, destination_gid),
  141. .offset_words = 6,
  142. .offset_bits = 0,
  143. .size_bits = 128 }
  144. };
  145. static const struct ib_field bth_table[] = {
  146. { STRUCT_FIELD(bth, opcode),
  147. .offset_words = 0,
  148. .offset_bits = 0,
  149. .size_bits = 8 },
  150. { STRUCT_FIELD(bth, solicited_event),
  151. .offset_words = 0,
  152. .offset_bits = 8,
  153. .size_bits = 1 },
  154. { STRUCT_FIELD(bth, mig_req),
  155. .offset_words = 0,
  156. .offset_bits = 9,
  157. .size_bits = 1 },
  158. { STRUCT_FIELD(bth, pad_count),
  159. .offset_words = 0,
  160. .offset_bits = 10,
  161. .size_bits = 2 },
  162. { STRUCT_FIELD(bth, transport_header_version),
  163. .offset_words = 0,
  164. .offset_bits = 12,
  165. .size_bits = 4 },
  166. { STRUCT_FIELD(bth, pkey),
  167. .offset_words = 0,
  168. .offset_bits = 16,
  169. .size_bits = 16 },
  170. { RESERVED,
  171. .offset_words = 1,
  172. .offset_bits = 0,
  173. .size_bits = 8 },
  174. { STRUCT_FIELD(bth, destination_qpn),
  175. .offset_words = 1,
  176. .offset_bits = 8,
  177. .size_bits = 24 },
  178. { STRUCT_FIELD(bth, ack_req),
  179. .offset_words = 2,
  180. .offset_bits = 0,
  181. .size_bits = 1 },
  182. { RESERVED,
  183. .offset_words = 2,
  184. .offset_bits = 1,
  185. .size_bits = 7 },
  186. { STRUCT_FIELD(bth, psn),
  187. .offset_words = 2,
  188. .offset_bits = 8,
  189. .size_bits = 24 }
  190. };
  191. static const struct ib_field deth_table[] = {
  192. { STRUCT_FIELD(deth, qkey),
  193. .offset_words = 0,
  194. .offset_bits = 0,
  195. .size_bits = 32 },
  196. { RESERVED,
  197. .offset_words = 1,
  198. .offset_bits = 0,
  199. .size_bits = 8 },
  200. { STRUCT_FIELD(deth, source_qpn),
  201. .offset_words = 1,
  202. .offset_bits = 8,
  203. .size_bits = 24 }
  204. };
  205. /**
  206. * ib_ud_header_init - Initialize UD header structure
  207. * @payload_bytes:Length of packet payload
  208. * @lrh_present: specify if LRH is present
  209. * @eth_present: specify if Eth header is present
  210. * @vlan_present: packet is tagged vlan
  211. * @grh_present:GRH flag (if non-zero, GRH will be included)
  212. * @immediate_present: specify if immediate data is present
  213. * @header:Structure to initialize
  214. */
  215. void ib_ud_header_init(int payload_bytes,
  216. int lrh_present,
  217. int eth_present,
  218. int vlan_present,
  219. int grh_present,
  220. int immediate_present,
  221. struct ib_ud_header *header)
  222. {
  223. memset(header, 0, sizeof *header);
  224. if (lrh_present) {
  225. u16 packet_length;
  226. header->lrh.link_version = 0;
  227. header->lrh.link_next_header =
  228. grh_present ? IB_LNH_IBA_GLOBAL : IB_LNH_IBA_LOCAL;
  229. packet_length = (IB_LRH_BYTES +
  230. IB_BTH_BYTES +
  231. IB_DETH_BYTES +
  232. (grh_present ? IB_GRH_BYTES : 0) +
  233. payload_bytes +
  234. 4 + /* ICRC */
  235. 3) / 4; /* round up */
  236. header->lrh.packet_length = cpu_to_be16(packet_length);
  237. }
  238. if (vlan_present)
  239. header->eth.type = cpu_to_be16(ETH_P_8021Q);
  240. if (grh_present) {
  241. header->grh.ip_version = 6;
  242. header->grh.payload_length =
  243. cpu_to_be16((IB_BTH_BYTES +
  244. IB_DETH_BYTES +
  245. payload_bytes +
  246. 4 + /* ICRC */
  247. 3) & ~3); /* round up */
  248. header->grh.next_header = 0x1b;
  249. }
  250. if (immediate_present)
  251. header->bth.opcode = IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE;
  252. else
  253. header->bth.opcode = IB_OPCODE_UD_SEND_ONLY;
  254. header->bth.pad_count = (4 - payload_bytes) & 3;
  255. header->bth.transport_header_version = 0;
  256. header->lrh_present = lrh_present;
  257. header->eth_present = eth_present;
  258. header->vlan_present = vlan_present;
  259. header->grh_present = grh_present;
  260. header->immediate_present = immediate_present;
  261. }
  262. EXPORT_SYMBOL(ib_ud_header_init);
  263. /**
  264. * ib_ud_header_pack - Pack UD header struct into wire format
  265. * @header:UD header struct
  266. * @buf:Buffer to pack into
  267. *
  268. * ib_ud_header_pack() packs the UD header structure @header into wire
  269. * format in the buffer @buf.
  270. */
  271. int ib_ud_header_pack(struct ib_ud_header *header,
  272. void *buf)
  273. {
  274. int len = 0;
  275. if (header->lrh_present) {
  276. ib_pack(lrh_table, ARRAY_SIZE(lrh_table),
  277. &header->lrh, buf + len);
  278. len += IB_LRH_BYTES;
  279. }
  280. if (header->eth_present) {
  281. ib_pack(eth_table, ARRAY_SIZE(eth_table),
  282. &header->eth, buf + len);
  283. len += IB_ETH_BYTES;
  284. }
  285. if (header->vlan_present) {
  286. ib_pack(vlan_table, ARRAY_SIZE(vlan_table),
  287. &header->vlan, buf + len);
  288. len += IB_VLAN_BYTES;
  289. }
  290. if (header->grh_present) {
  291. ib_pack(grh_table, ARRAY_SIZE(grh_table),
  292. &header->grh, buf + len);
  293. len += IB_GRH_BYTES;
  294. }
  295. ib_pack(bth_table, ARRAY_SIZE(bth_table),
  296. &header->bth, buf + len);
  297. len += IB_BTH_BYTES;
  298. ib_pack(deth_table, ARRAY_SIZE(deth_table),
  299. &header->deth, buf + len);
  300. len += IB_DETH_BYTES;
  301. if (header->immediate_present) {
  302. memcpy(buf + len, &header->immediate_data, sizeof header->immediate_data);
  303. len += sizeof header->immediate_data;
  304. }
  305. return len;
  306. }
  307. EXPORT_SYMBOL(ib_ud_header_pack);
  308. /**
  309. * ib_ud_header_unpack - Unpack UD header struct from wire format
  310. * @header:UD header struct
  311. * @buf:Buffer to pack into
  312. *
  313. * ib_ud_header_pack() unpacks the UD header structure @header from wire
  314. * format in the buffer @buf.
  315. */
  316. int ib_ud_header_unpack(void *buf,
  317. struct ib_ud_header *header)
  318. {
  319. ib_unpack(lrh_table, ARRAY_SIZE(lrh_table),
  320. buf, &header->lrh);
  321. buf += IB_LRH_BYTES;
  322. if (header->lrh.link_version != 0) {
  323. printk(KERN_WARNING "Invalid LRH.link_version %d\n",
  324. header->lrh.link_version);
  325. return -EINVAL;
  326. }
  327. switch (header->lrh.link_next_header) {
  328. case IB_LNH_IBA_LOCAL:
  329. header->grh_present = 0;
  330. break;
  331. case IB_LNH_IBA_GLOBAL:
  332. header->grh_present = 1;
  333. ib_unpack(grh_table, ARRAY_SIZE(grh_table),
  334. buf, &header->grh);
  335. buf += IB_GRH_BYTES;
  336. if (header->grh.ip_version != 6) {
  337. printk(KERN_WARNING "Invalid GRH.ip_version %d\n",
  338. header->grh.ip_version);
  339. return -EINVAL;
  340. }
  341. if (header->grh.next_header != 0x1b) {
  342. printk(KERN_WARNING "Invalid GRH.next_header 0x%02x\n",
  343. header->grh.next_header);
  344. return -EINVAL;
  345. }
  346. break;
  347. default:
  348. printk(KERN_WARNING "Invalid LRH.link_next_header %d\n",
  349. header->lrh.link_next_header);
  350. return -EINVAL;
  351. }
  352. ib_unpack(bth_table, ARRAY_SIZE(bth_table),
  353. buf, &header->bth);
  354. buf += IB_BTH_BYTES;
  355. switch (header->bth.opcode) {
  356. case IB_OPCODE_UD_SEND_ONLY:
  357. header->immediate_present = 0;
  358. break;
  359. case IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE:
  360. header->immediate_present = 1;
  361. break;
  362. default:
  363. printk(KERN_WARNING "Invalid BTH.opcode 0x%02x\n",
  364. header->bth.opcode);
  365. return -EINVAL;
  366. }
  367. if (header->bth.transport_header_version != 0) {
  368. printk(KERN_WARNING "Invalid BTH.transport_header_version %d\n",
  369. header->bth.transport_header_version);
  370. return -EINVAL;
  371. }
  372. ib_unpack(deth_table, ARRAY_SIZE(deth_table),
  373. buf, &header->deth);
  374. buf += IB_DETH_BYTES;
  375. if (header->immediate_present)
  376. memcpy(&header->immediate_data, buf, sizeof header->immediate_data);
  377. return 0;
  378. }
  379. EXPORT_SYMBOL(ib_ud_header_unpack);