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_pathrec.c 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * Copyright (C) 2009 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. * You can also choose to distribute this program under the terms of
  20. * the Unmodified Binary Distribution Licence (as given in the file
  21. * COPYING.UBDL), provided that you have satisfied its requirements.
  22. */
  23. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdint.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <byteswap.h>
  28. #include <errno.h>
  29. #include <ipxe/infiniband.h>
  30. #include <ipxe/ib_mi.h>
  31. #include <ipxe/ib_pathrec.h>
  32. /** @file
  33. *
  34. * Infiniband path lookups
  35. *
  36. */
  37. /**
  38. * Handle path transaction completion
  39. *
  40. * @v ibdev Infiniband device
  41. * @v mi Management interface
  42. * @v madx Management transaction
  43. * @v rc Status code
  44. * @v mad Received MAD (or NULL on error)
  45. * @v av Source address vector (or NULL on error)
  46. */
  47. static void ib_path_complete ( struct ib_device *ibdev,
  48. struct ib_mad_interface *mi,
  49. struct ib_mad_transaction *madx,
  50. int rc, union ib_mad *mad,
  51. struct ib_address_vector *av __unused ) {
  52. struct ib_path *path = ib_madx_get_ownerdata ( madx );
  53. union ib_gid *dgid = &path->av.gid;
  54. struct ib_path_record *pathrec = &mad->sa.sa_data.path_record;
  55. /* Report failures */
  56. if ( ( rc == 0 ) && ( mad->hdr.status != htons ( IB_MGMT_STATUS_OK ) ))
  57. rc = -ENETUNREACH;
  58. if ( rc != 0 ) {
  59. DBGC ( ibdev, "IBDEV %s path lookup for " IB_GID_FMT
  60. " failed: %s\n",
  61. ibdev->name, IB_GID_ARGS ( dgid ), strerror ( rc ) );
  62. goto out;
  63. }
  64. /* Extract values from MAD */
  65. path->av.lid = ntohs ( pathrec->dlid );
  66. path->av.sl = ( pathrec->reserved__sl & 0x0f );
  67. path->av.rate = ( pathrec->rate_selector__rate & 0x3f );
  68. DBGC ( ibdev, "IBDEV %s path to " IB_GID_FMT " lid %d sl %d rate "
  69. "%d\n", ibdev->name, IB_GID_ARGS ( dgid ), path->av.lid,
  70. path->av.sl, path->av.rate );
  71. /* Use only the LID if no GRH is needed for this path */
  72. if ( memcmp ( &path->av.gid.s.prefix, &ibdev->gid.s.prefix,
  73. sizeof ( path->av.gid.s.prefix ) ) == 0 ) {
  74. path->av.gid_present = 0;
  75. }
  76. out:
  77. /* Destroy the completed transaction */
  78. ib_destroy_madx ( ibdev, mi, madx );
  79. path->madx = NULL;
  80. /* Hand off to upper completion handler */
  81. path->op->complete ( ibdev, path, rc, &path->av );
  82. }
  83. /** Path transaction completion operations */
  84. static struct ib_mad_transaction_operations ib_path_op = {
  85. .complete = ib_path_complete,
  86. };
  87. /**
  88. * Create path
  89. *
  90. * @v ibdev Infiniband device
  91. * @v av Address vector to complete
  92. * @v op Path operations
  93. * @ret path Path
  94. */
  95. struct ib_path *
  96. ib_create_path ( struct ib_device *ibdev, struct ib_address_vector *av,
  97. struct ib_path_operations *op ) {
  98. struct ib_path *path;
  99. union ib_mad mad;
  100. struct ib_mad_sa *sa = &mad.sa;
  101. /* Allocate and initialise structure */
  102. path = zalloc ( sizeof ( *path ) );
  103. if ( ! path )
  104. goto err_alloc_path;
  105. path->ibdev = ibdev;
  106. memcpy ( &path->av, av, sizeof ( path->av ) );
  107. path->op = op;
  108. /* Construct path request */
  109. memset ( sa, 0, sizeof ( *sa ) );
  110. sa->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  111. sa->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  112. sa->mad_hdr.method = IB_MGMT_METHOD_GET;
  113. sa->mad_hdr.attr_id = htons ( IB_SA_ATTR_PATH_REC );
  114. sa->sa_hdr.comp_mask[1] =
  115. htonl ( IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID );
  116. memcpy ( &sa->sa_data.path_record.dgid, &path->av.gid,
  117. sizeof ( sa->sa_data.path_record.dgid ) );
  118. memcpy ( &sa->sa_data.path_record.sgid, &ibdev->gid,
  119. sizeof ( sa->sa_data.path_record.sgid ) );
  120. /* Create management transaction */
  121. path->madx = ib_create_madx ( ibdev, ibdev->gsi, &mad, NULL,
  122. &ib_path_op );
  123. if ( ! path->madx )
  124. goto err_create_madx;
  125. ib_madx_set_ownerdata ( path->madx, path );
  126. return path;
  127. ib_destroy_madx ( ibdev, ibdev->gsi, path->madx );
  128. err_create_madx:
  129. free ( path );
  130. err_alloc_path:
  131. return NULL;
  132. }
  133. /**
  134. * Destroy path
  135. *
  136. * @v ibdev Infiniband device
  137. * @v path Path
  138. */
  139. void ib_destroy_path ( struct ib_device *ibdev, struct ib_path *path ) {
  140. if ( path->madx )
  141. ib_destroy_madx ( ibdev, ibdev->gsi, path->madx );
  142. free ( path );
  143. }
  144. /** Number of path cache entries
  145. *
  146. * Must be a power of two.
  147. */
  148. #define IB_NUM_CACHED_PATHS 4
  149. /** A cached path */
  150. struct ib_cached_path {
  151. /** Path */
  152. struct ib_path *path;
  153. };
  154. /** Path cache */
  155. static struct ib_cached_path ib_path_cache[IB_NUM_CACHED_PATHS];
  156. /** Oldest path cache entry index */
  157. static unsigned int ib_path_cache_idx;
  158. /**
  159. * Find path cache entry
  160. *
  161. * @v ibdev Infiniband device
  162. * @v dgid Destination GID
  163. * @ret path Path cache entry, or NULL
  164. */
  165. static struct ib_cached_path *
  166. ib_find_path_cache_entry ( struct ib_device *ibdev, union ib_gid *dgid ) {
  167. struct ib_cached_path *cached;
  168. unsigned int i;
  169. for ( i = 0 ; i < IB_NUM_CACHED_PATHS ; i++ ) {
  170. cached = &ib_path_cache[i];
  171. if ( ! cached->path )
  172. continue;
  173. if ( cached->path->ibdev != ibdev )
  174. continue;
  175. if ( memcmp ( &cached->path->av.gid, dgid,
  176. sizeof ( cached->path->av.gid ) ) != 0 )
  177. continue;
  178. return cached;
  179. }
  180. return NULL;
  181. }
  182. /**
  183. * Handle cached path transaction completion
  184. *
  185. * @v ibdev Infiniband device
  186. * @v path Path
  187. * @v rc Status code
  188. * @v av Address vector, or NULL on error
  189. */
  190. static void ib_cached_path_complete ( struct ib_device *ibdev,
  191. struct ib_path *path, int rc,
  192. struct ib_address_vector *av __unused ) {
  193. struct ib_cached_path *cached = ib_path_get_ownerdata ( path );
  194. /* If the transaction failed, erase the cache entry */
  195. if ( rc != 0 ) {
  196. /* Destroy the old cache entry */
  197. ib_destroy_path ( ibdev, path );
  198. memset ( cached, 0, sizeof ( *cached ) );
  199. return;
  200. }
  201. /* Do not destroy the completed transaction; we still need to
  202. * refer to the resolved path.
  203. */
  204. }
  205. /** Cached path transaction completion operations */
  206. static struct ib_path_operations ib_cached_path_op = {
  207. .complete = ib_cached_path_complete,
  208. };
  209. /**
  210. * Resolve path
  211. *
  212. * @v ibdev Infiniband device
  213. * @v av Address vector to complete
  214. * @ret rc Return status code
  215. *
  216. * This provides a non-transactional way to resolve a path, via a
  217. * cache similar to ARP.
  218. */
  219. int ib_resolve_path ( struct ib_device *ibdev, struct ib_address_vector *av ) {
  220. union ib_gid *gid = &av->gid;
  221. struct ib_cached_path *cached;
  222. unsigned int cache_idx;
  223. /* Look in cache for a matching entry */
  224. cached = ib_find_path_cache_entry ( ibdev, gid );
  225. if ( cached && cached->path->av.lid ) {
  226. /* Populated entry found */
  227. av->lid = cached->path->av.lid;
  228. av->rate = cached->path->av.rate;
  229. av->sl = cached->path->av.sl;
  230. av->gid_present = cached->path->av.gid_present;
  231. DBGC2 ( ibdev, "IBDEV %s cache hit for " IB_GID_FMT "\n",
  232. ibdev->name, IB_GID_ARGS ( gid ) );
  233. return 0;
  234. }
  235. DBGC ( ibdev, "IBDEV %s cache miss for " IB_GID_FMT "%s\n", ibdev->name,
  236. IB_GID_ARGS ( gid ), ( cached ? " (in progress)" : "" ) );
  237. /* If lookup is already in progress, do nothing */
  238. if ( cached )
  239. return -ENOENT;
  240. /* Locate a new cache entry to use */
  241. cache_idx = ( (ib_path_cache_idx++) % IB_NUM_CACHED_PATHS );
  242. cached = &ib_path_cache[cache_idx];
  243. /* Destroy the old cache entry */
  244. if ( cached->path )
  245. ib_destroy_path ( ibdev, cached->path );
  246. memset ( cached, 0, sizeof ( *cached ) );
  247. /* Create new path */
  248. cached->path = ib_create_path ( ibdev, av, &ib_cached_path_op );
  249. if ( ! cached->path ) {
  250. DBGC ( ibdev, "IBDEV %s could not create path\n",
  251. ibdev->name );
  252. return -ENOMEM;
  253. }
  254. ib_path_set_ownerdata ( cached->path, cached );
  255. /* Not found yet */
  256. return -ENOENT;
  257. }