ud_header.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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_lrh_header_pack - Pack LRH header struct into wire format
  265. * @lrh:unpacked LRH header struct
  266. * @buf:Buffer to pack into
  267. *
  268. * ib_lrh_header_pack() packs the LRH header structure @lrh into
  269. * wire format in the buffer @buf.
  270. */
  271. int ib_lrh_header_pack(struct ib_unpacked_lrh *lrh, void *buf)
  272. {
  273. ib_pack(lrh_table, ARRAY_SIZE(lrh_table), lrh, buf);
  274. return 0;
  275. }
  276. EXPORT_SYMBOL(ib_lrh_header_pack);
  277. /**
  278. * ib_lrh_header_unpack - Unpack LRH structure from wire format
  279. * @lrh:unpacked LRH header struct
  280. * @buf:Buffer to pack into
  281. *
  282. * ib_lrh_header_unpack() unpacks the LRH header structure from
  283. * wire format (in buf) into @lrh.
  284. */
  285. int ib_lrh_header_unpack(void *buf, struct ib_unpacked_lrh *lrh)
  286. {
  287. ib_unpack(lrh_table, ARRAY_SIZE(lrh_table), buf, lrh);
  288. return 0;
  289. }
  290. EXPORT_SYMBOL(ib_lrh_header_unpack);
  291. /**
  292. * ib_ud_header_pack - Pack UD header struct into wire format
  293. * @header:UD header struct
  294. * @buf:Buffer to pack into
  295. *
  296. * ib_ud_header_pack() packs the UD header structure @header into wire
  297. * format in the buffer @buf.
  298. */
  299. int ib_ud_header_pack(struct ib_ud_header *header,
  300. void *buf)
  301. {
  302. int len = 0;
  303. if (header->lrh_present) {
  304. ib_pack(lrh_table, ARRAY_SIZE(lrh_table),
  305. &header->lrh, buf + len);
  306. len += IB_LRH_BYTES;
  307. }
  308. if (header->eth_present) {
  309. ib_pack(eth_table, ARRAY_SIZE(eth_table),
  310. &header->eth, buf + len);
  311. len += IB_ETH_BYTES;
  312. }
  313. if (header->vlan_present) {
  314. ib_pack(vlan_table, ARRAY_SIZE(vlan_table),
  315. &header->vlan, buf + len);
  316. len += IB_VLAN_BYTES;
  317. }
  318. if (header->grh_present) {
  319. ib_pack(grh_table, ARRAY_SIZE(grh_table),
  320. &header->grh, buf + len);
  321. len += IB_GRH_BYTES;
  322. }
  323. ib_pack(bth_table, ARRAY_SIZE(bth_table),
  324. &header->bth, buf + len);
  325. len += IB_BTH_BYTES;
  326. ib_pack(deth_table, ARRAY_SIZE(deth_table),
  327. &header->deth, buf + len);
  328. len += IB_DETH_BYTES;
  329. if (header->immediate_present) {
  330. memcpy(buf + len, &header->immediate_data, sizeof header->immediate_data);
  331. len += sizeof header->immediate_data;
  332. }
  333. return len;
  334. }
  335. EXPORT_SYMBOL(ib_ud_header_pack);
  336. /**
  337. * ib_ud_header_unpack - Unpack UD header struct from wire format
  338. * @header:UD header struct
  339. * @buf:Buffer to pack into
  340. *
  341. * ib_ud_header_pack() unpacks the UD header structure @header from wire
  342. * format in the buffer @buf.
  343. */
  344. int ib_ud_header_unpack(void *buf,
  345. struct ib_ud_header *header)
  346. {
  347. ib_unpack(lrh_table, ARRAY_SIZE(lrh_table),
  348. buf, &header->lrh);
  349. buf += IB_LRH_BYTES;
  350. if (header->lrh.link_version != 0) {
  351. printk(KERN_WARNING "Invalid LRH.link_version %d\n",
  352. header->lrh.link_version);
  353. return -EINVAL;
  354. }
  355. switch (header->lrh.link_next_header) {
  356. case IB_LNH_IBA_LOCAL:
  357. header->grh_present = 0;
  358. break;
  359. case IB_LNH_IBA_GLOBAL:
  360. header->grh_present = 1;
  361. ib_unpack(grh_table, ARRAY_SIZE(grh_table),
  362. buf, &header->grh);
  363. buf += IB_GRH_BYTES;
  364. if (header->grh.ip_version != 6) {
  365. printk(KERN_WARNING "Invalid GRH.ip_version %d\n",
  366. header->grh.ip_version);
  367. return -EINVAL;
  368. }
  369. if (header->grh.next_header != 0x1b) {
  370. printk(KERN_WARNING "Invalid GRH.next_header 0x%02x\n",
  371. header->grh.next_header);
  372. return -EINVAL;
  373. }
  374. break;
  375. default:
  376. printk(KERN_WARNING "Invalid LRH.link_next_header %d\n",
  377. header->lrh.link_next_header);
  378. return -EINVAL;
  379. }
  380. ib_unpack(bth_table, ARRAY_SIZE(bth_table),
  381. buf, &header->bth);
  382. buf += IB_BTH_BYTES;
  383. switch (header->bth.opcode) {
  384. case IB_OPCODE_UD_SEND_ONLY:
  385. header->immediate_present = 0;
  386. break;
  387. case IB_OPCODE_UD_SEND_ONLY_WITH_IMMEDIATE:
  388. header->immediate_present = 1;
  389. break;
  390. default:
  391. printk(KERN_WARNING "Invalid BTH.opcode 0x%02x\n",
  392. header->bth.opcode);
  393. return -EINVAL;
  394. }
  395. if (header->bth.transport_header_version != 0) {
  396. printk(KERN_WARNING "Invalid BTH.transport_header_version %d\n",
  397. header->bth.transport_header_version);
  398. return -EINVAL;
  399. }
  400. ib_unpack(deth_table, ARRAY_SIZE(deth_table),
  401. buf, &header->deth);
  402. buf += IB_DETH_BYTES;
  403. if (header->immediate_present)
  404. memcpy(&header->immediate_data, buf, sizeof header->immediate_data);
  405. return 0;
  406. }
  407. EXPORT_SYMBOL(ib_ud_header_unpack);