ud_header.c 11 KB

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