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.1KB

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