You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ib_packet.c 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdint.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <byteswap.h>
  25. #include <ipxe/iobuf.h>
  26. #include <ipxe/infiniband.h>
  27. #include <ipxe/ib_packet.h>
  28. /**
  29. * @file
  30. *
  31. * Infiniband Packet Formats
  32. *
  33. */
  34. /**
  35. * Add IB headers
  36. *
  37. * @v ibdev Infiniband device
  38. * @v iobuf I/O buffer to contain headers
  39. * @v qp Queue pair
  40. * @v payload_len Payload length
  41. * @v dest Destination address vector
  42. * @ret rc Return status code
  43. */
  44. int ib_push ( struct ib_device *ibdev, struct io_buffer *iobuf,
  45. struct ib_queue_pair *qp, size_t payload_len,
  46. const struct ib_address_vector *dest ) {
  47. struct ib_local_route_header *lrh;
  48. struct ib_global_route_header *grh;
  49. struct ib_base_transport_header *bth;
  50. struct ib_datagram_extended_transport_header *deth;
  51. size_t orig_iob_len = iob_len ( iobuf );
  52. size_t pad_len;
  53. size_t lrh_len;
  54. size_t grh_len;
  55. unsigned int vl;
  56. unsigned int lnh;
  57. DBGC2 ( ibdev, "IBDEV %p TX %04x:%08lx => %04x:%08lx (key %08lx)\n",
  58. ibdev, ibdev->lid, qp->ext_qpn, dest->lid, dest->qpn,
  59. dest->qkey );
  60. /* Calculate packet length */
  61. pad_len = ( (-payload_len) & 0x3 );
  62. payload_len += pad_len;
  63. payload_len += 4; /* ICRC */
  64. /* Reserve space for headers */
  65. orig_iob_len = iob_len ( iobuf );
  66. deth = iob_push ( iobuf, sizeof ( *deth ) );
  67. bth = iob_push ( iobuf, sizeof ( *bth ) );
  68. grh_len = ( payload_len + iob_len ( iobuf ) - orig_iob_len );
  69. grh = ( dest->gid_present ?
  70. iob_push ( iobuf, sizeof ( *grh ) ) : NULL );
  71. lrh = iob_push ( iobuf, sizeof ( *lrh ) );
  72. lrh_len = ( payload_len + iob_len ( iobuf ) - orig_iob_len );
  73. /* Construct LRH */
  74. vl = ( ( qp->ext_qpn == IB_QPN_SMI ) ? IB_VL_SMP : IB_VL_DEFAULT );
  75. lrh->vl__lver = ( vl << 4 );
  76. lnh = ( grh ? IB_LNH_GRH : IB_LNH_BTH );
  77. lrh->sl__lnh = ( ( dest->sl << 4 ) | lnh );
  78. lrh->dlid = htons ( dest->lid );
  79. lrh->length = htons ( lrh_len >> 2 );
  80. lrh->slid = htons ( ibdev->lid );
  81. /* Construct GRH, if required */
  82. if ( grh ) {
  83. grh->ipver__tclass__flowlabel =
  84. htonl ( IB_GRH_IPVER_IPv6 << 28 );
  85. grh->paylen = htons ( grh_len );
  86. grh->nxthdr = IB_GRH_NXTHDR_IBA;
  87. grh->hoplmt = 0;
  88. memcpy ( &grh->sgid, &ibdev->gid, sizeof ( grh->sgid ) );
  89. memcpy ( &grh->dgid, &dest->gid, sizeof ( grh->dgid ) );
  90. }
  91. /* Construct BTH */
  92. bth->opcode = BTH_OPCODE_UD_SEND;
  93. bth->se__m__padcnt__tver = ( pad_len << 4 );
  94. bth->pkey = htons ( ibdev->pkey );
  95. bth->dest_qp = htonl ( dest->qpn );
  96. bth->ack__psn = htonl ( ( qp->send.psn++ ) & 0xffffffUL );
  97. /* Construct DETH */
  98. deth->qkey = htonl ( dest->qkey );
  99. deth->src_qp = htonl ( qp->ext_qpn );
  100. DBGCP_HDA ( ibdev, 0, iobuf->data,
  101. ( iob_len ( iobuf ) - orig_iob_len ) );
  102. return 0;
  103. }
  104. /**
  105. * Remove IB headers
  106. *
  107. * @v ibdev Infiniband device
  108. * @v iobuf I/O buffer containing headers
  109. * @v qp Queue pair to fill in, or NULL
  110. * @v payload_len Payload length to fill in, or NULL
  111. * @v dest Destination address vector to fill in
  112. * @v source Source address vector to fill in
  113. * @ret rc Return status code
  114. */
  115. int ib_pull ( struct ib_device *ibdev, struct io_buffer *iobuf,
  116. struct ib_queue_pair **qp, size_t *payload_len,
  117. struct ib_address_vector *dest,
  118. struct ib_address_vector *source ) {
  119. struct ib_local_route_header *lrh;
  120. struct ib_global_route_header *grh;
  121. struct ib_base_transport_header *bth;
  122. struct ib_datagram_extended_transport_header *deth;
  123. size_t orig_iob_len = iob_len ( iobuf );
  124. unsigned int lnh;
  125. size_t pad_len;
  126. /* Clear return values */
  127. if ( qp )
  128. *qp = NULL;
  129. if ( payload_len )
  130. *payload_len = 0;
  131. memset ( dest, 0, sizeof ( *dest ) );
  132. memset ( source, 0, sizeof ( *source ) );
  133. /* Extract LRH */
  134. if ( iob_len ( iobuf ) < sizeof ( *lrh ) ) {
  135. DBGC ( ibdev, "IBDEV %p RX too short (%zd bytes) for LRH\n",
  136. ibdev, iob_len ( iobuf ) );
  137. return -EINVAL;
  138. }
  139. lrh = iobuf->data;
  140. iob_pull ( iobuf, sizeof ( *lrh ) );
  141. dest->lid = ntohs ( lrh->dlid );
  142. dest->sl = ( lrh->sl__lnh >> 4 );
  143. source->lid = ntohs ( lrh->slid );
  144. source->sl = ( lrh->sl__lnh >> 4 );
  145. lnh = ( lrh->sl__lnh & 0x3 );
  146. /* Reject unsupported packets */
  147. if ( ! ( ( lnh == IB_LNH_BTH ) || ( lnh == IB_LNH_GRH ) ) ) {
  148. DBGC ( ibdev, "IBDEV %p RX unsupported LNH %x\n",
  149. ibdev, lnh );
  150. return -ENOTSUP;
  151. }
  152. /* Extract GRH, if present */
  153. if ( lnh == IB_LNH_GRH ) {
  154. if ( iob_len ( iobuf ) < sizeof ( *grh ) ) {
  155. DBGC ( ibdev, "IBDEV %p RX too short (%zd bytes) "
  156. "for GRH\n", ibdev, iob_len ( iobuf ) );
  157. return -EINVAL;
  158. }
  159. grh = iobuf->data;
  160. iob_pull ( iobuf, sizeof ( *grh ) );
  161. dest->gid_present = 1;
  162. memcpy ( &dest->gid, &grh->dgid, sizeof ( dest->gid ) );
  163. source->gid_present = 1;
  164. memcpy ( &source->gid, &grh->sgid, sizeof ( source->gid ) );
  165. } else {
  166. grh = NULL;
  167. }
  168. /* Extract BTH */
  169. if ( iob_len ( iobuf ) < sizeof ( *bth ) ) {
  170. DBGC ( ibdev, "IBDEV %p RX too short (%zd bytes) for BTH\n",
  171. ibdev, iob_len ( iobuf ) );
  172. return -EINVAL;
  173. }
  174. bth = iobuf->data;
  175. iob_pull ( iobuf, sizeof ( *bth ) );
  176. if ( bth->opcode != BTH_OPCODE_UD_SEND ) {
  177. DBGC ( ibdev, "IBDEV %p unsupported BTH opcode %x\n",
  178. ibdev, bth->opcode );
  179. return -ENOTSUP;
  180. }
  181. dest->qpn = ntohl ( bth->dest_qp );
  182. /* Extract DETH */
  183. if ( iob_len ( iobuf ) < sizeof ( *deth ) ) {
  184. DBGC ( ibdev, "IBDEV %p RX too short (%zd bytes) for DETH\n",
  185. ibdev, iob_len ( iobuf ) );
  186. return -EINVAL;
  187. }
  188. deth = iobuf->data;
  189. iob_pull ( iobuf, sizeof ( *deth ) );
  190. source->qpn = ntohl ( deth->src_qp );
  191. source->qkey = ntohl ( deth->qkey );
  192. /* Calculate payload length, if applicable */
  193. if ( payload_len ) {
  194. pad_len = ( ( bth->se__m__padcnt__tver >> 4 ) & 0x3 );
  195. *payload_len = ( ( ntohs ( lrh->length ) << 2 )
  196. - ( orig_iob_len - iob_len ( iobuf ) )
  197. - pad_len - 4 /* ICRC */ );
  198. }
  199. /* Determine destination QP, if applicable */
  200. if ( qp ) {
  201. if ( IB_LID_MULTICAST ( dest->lid ) && grh ) {
  202. if ( ! ( *qp = ib_find_qp_mgid ( ibdev, &grh->dgid ))){
  203. DBGC ( ibdev, "IBDEV %p RX for unknown MGID "
  204. IB_GID_FMT "\n",
  205. ibdev, IB_GID_ARGS ( &grh->dgid ) );
  206. return -ENODEV;
  207. }
  208. } else {
  209. if ( ! ( *qp = ib_find_qp_qpn ( ibdev, dest->qpn ) ) ) {
  210. DBGC ( ibdev, "IBDEV %p RX for nonexistent "
  211. "QPN %lx\n", ibdev, dest->qpn );
  212. return -ENODEV;
  213. }
  214. }
  215. assert ( *qp );
  216. }
  217. DBGC2 ( ibdev, "IBDEV %p RX %04x:%08lx <= %04x:%08lx (key %08x)\n",
  218. ibdev, dest->lid, ( IB_LID_MULTICAST ( dest->lid ) ?
  219. ( qp ? (*qp)->ext_qpn : -1UL ) : dest->qpn ),
  220. source->lid, source->qpn, ntohl ( deth->qkey ) );
  221. DBGCP_HDA ( ibdev, 0,
  222. ( iobuf->data - ( orig_iob_len - iob_len ( iobuf ) ) ),
  223. ( orig_iob_len - iob_len ( iobuf ) ) );
  224. return 0;
  225. }