Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ib_pathrec.c 8.0KB

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