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 7.9KB

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 " is %04x sl %d rate "
  69. "%d\n", ibdev->name, IB_GID_ARGS ( dgid ), path->av.lid,
  70. path->av.sl, path->av.rate );
  71. out:
  72. /* Destroy the completed transaction */
  73. ib_destroy_madx ( ibdev, mi, madx );
  74. path->madx = NULL;
  75. /* Hand off to upper completion handler */
  76. path->op->complete ( ibdev, path, rc, &path->av );
  77. }
  78. /** Path transaction completion operations */
  79. static struct ib_mad_transaction_operations ib_path_op = {
  80. .complete = ib_path_complete,
  81. };
  82. /**
  83. * Create path
  84. *
  85. * @v ibdev Infiniband device
  86. * @v av Address vector to complete
  87. * @v op Path operations
  88. * @ret path Path
  89. */
  90. struct ib_path *
  91. ib_create_path ( struct ib_device *ibdev, struct ib_address_vector *av,
  92. struct ib_path_operations *op ) {
  93. struct ib_path *path;
  94. union ib_mad mad;
  95. struct ib_mad_sa *sa = &mad.sa;
  96. /* Allocate and initialise structure */
  97. path = zalloc ( sizeof ( *path ) );
  98. if ( ! path )
  99. goto err_alloc_path;
  100. path->ibdev = ibdev;
  101. memcpy ( &path->av, av, sizeof ( path->av ) );
  102. path->op = op;
  103. /* Construct path request */
  104. memset ( sa, 0, sizeof ( *sa ) );
  105. sa->mad_hdr.mgmt_class = IB_MGMT_CLASS_SUBN_ADM;
  106. sa->mad_hdr.class_version = IB_SA_CLASS_VERSION;
  107. sa->mad_hdr.method = IB_MGMT_METHOD_GET;
  108. sa->mad_hdr.attr_id = htons ( IB_SA_ATTR_PATH_REC );
  109. sa->sa_hdr.comp_mask[1] =
  110. htonl ( IB_SA_PATH_REC_DGID | IB_SA_PATH_REC_SGID );
  111. memcpy ( &sa->sa_data.path_record.dgid, &path->av.gid,
  112. sizeof ( sa->sa_data.path_record.dgid ) );
  113. memcpy ( &sa->sa_data.path_record.sgid, &ibdev->gid,
  114. sizeof ( sa->sa_data.path_record.sgid ) );
  115. /* Create management transaction */
  116. path->madx = ib_create_madx ( ibdev, ibdev->gsi, &mad, NULL,
  117. &ib_path_op );
  118. if ( ! path->madx )
  119. goto err_create_madx;
  120. ib_madx_set_ownerdata ( path->madx, path );
  121. return path;
  122. ib_destroy_madx ( ibdev, ibdev->gsi, path->madx );
  123. err_create_madx:
  124. free ( path );
  125. err_alloc_path:
  126. return NULL;
  127. }
  128. /**
  129. * Destroy path
  130. *
  131. * @v ibdev Infiniband device
  132. * @v path Path
  133. */
  134. void ib_destroy_path ( struct ib_device *ibdev, struct ib_path *path ) {
  135. if ( path->madx )
  136. ib_destroy_madx ( ibdev, ibdev->gsi, path->madx );
  137. free ( path );
  138. }
  139. /** Number of path cache entries
  140. *
  141. * Must be a power of two.
  142. */
  143. #define IB_NUM_CACHED_PATHS 4
  144. /** A cached path */
  145. struct ib_cached_path {
  146. /** Path */
  147. struct ib_path *path;
  148. };
  149. /** Path cache */
  150. static struct ib_cached_path ib_path_cache[IB_NUM_CACHED_PATHS];
  151. /** Oldest path cache entry index */
  152. static unsigned int ib_path_cache_idx;
  153. /**
  154. * Find path cache entry
  155. *
  156. * @v ibdev Infiniband device
  157. * @v dgid Destination GID
  158. * @ret path Path cache entry, or NULL
  159. */
  160. static struct ib_cached_path *
  161. ib_find_path_cache_entry ( struct ib_device *ibdev, union ib_gid *dgid ) {
  162. struct ib_cached_path *cached;
  163. unsigned int i;
  164. for ( i = 0 ; i < IB_NUM_CACHED_PATHS ; i++ ) {
  165. cached = &ib_path_cache[i];
  166. if ( ! cached->path )
  167. continue;
  168. if ( cached->path->ibdev != ibdev )
  169. continue;
  170. if ( memcmp ( &cached->path->av.gid, dgid,
  171. sizeof ( cached->path->av.gid ) ) != 0 )
  172. continue;
  173. return cached;
  174. }
  175. return NULL;
  176. }
  177. /**
  178. * Handle cached path transaction completion
  179. *
  180. * @v ibdev Infiniband device
  181. * @v path Path
  182. * @v rc Status code
  183. * @v av Address vector, or NULL on error
  184. */
  185. static void ib_cached_path_complete ( struct ib_device *ibdev,
  186. struct ib_path *path, int rc,
  187. struct ib_address_vector *av __unused ) {
  188. struct ib_cached_path *cached = ib_path_get_ownerdata ( path );
  189. /* If the transaction failed, erase the cache entry */
  190. if ( rc != 0 ) {
  191. /* Destroy the old cache entry */
  192. ib_destroy_path ( ibdev, path );
  193. memset ( cached, 0, sizeof ( *cached ) );
  194. return;
  195. }
  196. /* Do not destroy the completed transaction; we still need to
  197. * refer to the resolved path.
  198. */
  199. }
  200. /** Cached path transaction completion operations */
  201. static struct ib_path_operations ib_cached_path_op = {
  202. .complete = ib_cached_path_complete,
  203. };
  204. /**
  205. * Resolve path
  206. *
  207. * @v ibdev Infiniband device
  208. * @v av Address vector to complete
  209. * @ret rc Return status code
  210. *
  211. * This provides a non-transactional way to resolve a path, via a
  212. * cache similar to ARP.
  213. */
  214. int ib_resolve_path ( struct ib_device *ibdev, struct ib_address_vector *av ) {
  215. union ib_gid *gid = &av->gid;
  216. struct ib_cached_path *cached;
  217. unsigned int cache_idx;
  218. /* Sanity check */
  219. if ( ! av->gid_present ) {
  220. DBGC ( ibdev, "IBDEV %s attempt to look up path without GID\n",
  221. ibdev->name );
  222. return -EINVAL;
  223. }
  224. /* Look in cache for a matching entry */
  225. cached = ib_find_path_cache_entry ( ibdev, gid );
  226. if ( cached && cached->path->av.lid ) {
  227. /* Populated entry found */
  228. av->lid = cached->path->av.lid;
  229. av->rate = cached->path->av.rate;
  230. av->sl = cached->path->av.sl;
  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. }