Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

slam.c 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  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., 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 <strings.h>
  23. #include <errno.h>
  24. #include <assert.h>
  25. #include <byteswap.h>
  26. #include <gpxe/features.h>
  27. #include <gpxe/iobuf.h>
  28. #include <gpxe/bitmap.h>
  29. #include <gpxe/xfer.h>
  30. #include <gpxe/open.h>
  31. #include <gpxe/uri.h>
  32. #include <gpxe/tcpip.h>
  33. #include <gpxe/timer.h>
  34. #include <gpxe/retry.h>
  35. /** @file
  36. *
  37. * Scalable Local Area Multicast protocol
  38. *
  39. * The SLAM protocol is supported only by Etherboot; it was designed
  40. * and implemented by Eric Biederman. A server implementation is
  41. * available in contrib/mini-slamd. There does not appear to be any
  42. * documentation beyond a few sparse comments in Etherboot's
  43. * proto_slam.c.
  44. *
  45. * SLAM packets use three types of data field:
  46. *
  47. * Nul : A single NUL (0) byte, used as a list terminator
  48. *
  49. * Raw : A block of raw data
  50. *
  51. * Int : A variable-length integer, in big-endian order. The length
  52. * of the integer is encoded in the most significant three bits.
  53. *
  54. * Packets received by the client have the following layout:
  55. *
  56. * Int : Transaction identifier. This is an opaque value.
  57. *
  58. * Int : Total number of bytes in the transfer.
  59. *
  60. * Int : Block size, in bytes.
  61. *
  62. * Int : Packet sequence number within the transfer (if this packet
  63. * contains data).
  64. *
  65. * Raw : Packet data (if this packet contains data).
  66. *
  67. * Packets transmitted by the client consist of a run-length-encoded
  68. * representation of the received-blocks bitmap, looking something
  69. * like:
  70. *
  71. * Int : Number of consecutive successfully-received packets
  72. * Int : Number of consecutive missing packets
  73. * Int : Number of consecutive successfully-received packets
  74. * Int : Number of consecutive missing packets
  75. * ....
  76. * Nul
  77. *
  78. */
  79. FEATURE ( FEATURE_PROTOCOL, "SLAM", DHCP_EB_FEATURE_SLAM, 1 );
  80. /** Default SLAM server port */
  81. #define SLAM_DEFAULT_PORT 10000
  82. /** Default SLAM multicast IP address */
  83. #define SLAM_DEFAULT_MULTICAST_IP \
  84. ( ( 239 << 24 ) | ( 255 << 16 ) | ( 1 << 8 ) | ( 1 << 0 ) )
  85. /** Default SLAM multicast port */
  86. #define SLAM_DEFAULT_MULTICAST_PORT 10000
  87. /** Maximum SLAM header length */
  88. #define SLAM_MAX_HEADER_LEN ( 7 /* transaction id */ + 7 /* total_bytes */ + \
  89. 7 /* block_size */ )
  90. /** Maximum number of blocks to request per NACK
  91. *
  92. * This is a policy decision equivalent to selecting a TCP window
  93. * size.
  94. */
  95. #define SLAM_MAX_BLOCKS_PER_NACK 4
  96. /** Maximum SLAM NACK length
  97. *
  98. * We only ever send a NACK for a single range of up to @c
  99. * SLAM_MAX_BLOCKS_PER_NACK blocks.
  100. */
  101. #define SLAM_MAX_NACK_LEN ( 7 /* block */ + 7 /* #blocks */ + 1 /* NUL */ )
  102. /** SLAM slave timeout */
  103. #define SLAM_SLAVE_TIMEOUT ( 1 * TICKS_PER_SEC )
  104. /** A SLAM request */
  105. struct slam_request {
  106. /** Reference counter */
  107. struct refcnt refcnt;
  108. /** Data transfer interface */
  109. struct xfer_interface xfer;
  110. /** Unicast socket */
  111. struct xfer_interface socket;
  112. /** Multicast socket */
  113. struct xfer_interface mc_socket;
  114. /** Master client retry timer */
  115. struct retry_timer master_timer;
  116. /** Slave client retry timer */
  117. struct retry_timer slave_timer;
  118. /** Cached header */
  119. uint8_t header[SLAM_MAX_HEADER_LEN];
  120. /** Size of cached header */
  121. size_t header_len;
  122. /** Total number of bytes in transfer */
  123. unsigned long total_bytes;
  124. /** Transfer block size */
  125. unsigned long block_size;
  126. /** Number of blocks in transfer */
  127. unsigned long num_blocks;
  128. /** Block bitmap */
  129. struct bitmap bitmap;
  130. /** NACK sent flag */
  131. int nack_sent;
  132. };
  133. /**
  134. * Free a SLAM request
  135. *
  136. * @v refcnt Reference counter
  137. */
  138. static void slam_free ( struct refcnt *refcnt ) {
  139. struct slam_request *slam =
  140. container_of ( refcnt, struct slam_request, refcnt );
  141. bitmap_free ( &slam->bitmap );
  142. free ( slam );
  143. }
  144. /**
  145. * Mark SLAM request as complete
  146. *
  147. * @v slam SLAM request
  148. * @v rc Return status code
  149. */
  150. static void slam_finished ( struct slam_request *slam, int rc ) {
  151. static const uint8_t slam_disconnect[] = { 0 };
  152. DBGC ( slam, "SLAM %p finished with status code %d (%s)\n",
  153. slam, rc, strerror ( rc ) );
  154. /* Send a disconnect message if we ever sent anything to the
  155. * server.
  156. */
  157. if ( slam->nack_sent ) {
  158. xfer_deliver_raw ( &slam->socket, slam_disconnect,
  159. sizeof ( slam_disconnect ) );
  160. }
  161. /* Stop the retry timers */
  162. stop_timer ( &slam->master_timer );
  163. stop_timer ( &slam->slave_timer );
  164. /* Close all data transfer interfaces */
  165. xfer_nullify ( &slam->socket );
  166. xfer_close ( &slam->socket, rc );
  167. xfer_nullify ( &slam->mc_socket );
  168. xfer_close ( &slam->mc_socket, rc );
  169. xfer_nullify ( &slam->xfer );
  170. xfer_close ( &slam->xfer, rc );
  171. }
  172. /****************************************************************************
  173. *
  174. * TX datapath
  175. *
  176. */
  177. /**
  178. * Add a variable-length value to a SLAM packet
  179. *
  180. * @v slam SLAM request
  181. * @v iobuf I/O buffer
  182. * @v value Value to add
  183. * @ret rc Return status code
  184. *
  185. * Adds a variable-length value to the end of an I/O buffer. Will
  186. * always leave at least one byte of tailroom in the I/O buffer (to
  187. * allow space for the terminating NUL).
  188. */
  189. static int slam_put_value ( struct slam_request *slam,
  190. struct io_buffer *iobuf, unsigned long value ) {
  191. uint8_t *data;
  192. size_t len;
  193. unsigned int i;
  194. /* Calculate variable length required to store value. Always
  195. * leave at least one byte in the I/O buffer.
  196. */
  197. len = ( ( flsl ( value ) + 10 ) / 8 );
  198. if ( len >= iob_tailroom ( iobuf ) ) {
  199. DBGC2 ( slam, "SLAM %p cannot add %zd-byte value\n",
  200. slam, len );
  201. return -ENOBUFS;
  202. }
  203. /* There is no valid way within the protocol that we can end
  204. * up trying to push a full-sized long (i.e. without space for
  205. * the length encoding).
  206. */
  207. assert ( len <= sizeof ( value ) );
  208. /* Add value */
  209. data = iob_put ( iobuf, len );
  210. for ( i = len ; i-- ; ) {
  211. data[i] = value;
  212. value >>= 8;
  213. }
  214. *data |= ( len << 5 );
  215. assert ( value == 0 );
  216. return 0;
  217. }
  218. /**
  219. * Send SLAM NACK packet
  220. *
  221. * @v slam SLAM request
  222. * @ret rc Return status code
  223. */
  224. static int slam_tx_nack ( struct slam_request *slam ) {
  225. struct io_buffer *iobuf;
  226. unsigned long first_block;
  227. unsigned long num_blocks;
  228. uint8_t *nul;
  229. int rc;
  230. /* Mark NACK as sent, so that we know we have to disconnect later */
  231. slam->nack_sent = 1;
  232. /* Allocate I/O buffer */
  233. iobuf = xfer_alloc_iob ( &slam->socket, SLAM_MAX_NACK_LEN );
  234. if ( ! iobuf ) {
  235. DBGC ( slam, "SLAM %p could not allocate I/O buffer\n",
  236. slam );
  237. return -ENOMEM;
  238. }
  239. /* Construct NACK. We always request only a single packet;
  240. * this allows us to force multicast-TFTP-style flow control
  241. * on the SLAM server, which will otherwise just blast the
  242. * data out as fast as it can. On a gigabit network, without
  243. * RX checksumming, this would inevitably cause packet drops.
  244. */
  245. first_block = bitmap_first_gap ( &slam->bitmap );
  246. for ( num_blocks = 1 ; ; num_blocks++ ) {
  247. if ( num_blocks >= SLAM_MAX_BLOCKS_PER_NACK )
  248. break;
  249. if ( ( first_block + num_blocks ) >= slam->num_blocks )
  250. break;
  251. if ( bitmap_test ( &slam->bitmap,
  252. ( first_block + num_blocks ) ) )
  253. break;
  254. }
  255. if ( first_block ) {
  256. DBGCP ( slam, "SLAM %p transmitting NACK for blocks "
  257. "%ld-%ld\n", slam, first_block,
  258. ( first_block + num_blocks - 1 ) );
  259. } else {
  260. DBGC ( slam, "SLAM %p transmitting initial NACK for blocks "
  261. "0-%ld\n", slam, ( num_blocks - 1 ) );
  262. }
  263. if ( ( rc = slam_put_value ( slam, iobuf, first_block ) ) != 0 )
  264. return rc;
  265. if ( ( rc = slam_put_value ( slam, iobuf, num_blocks ) ) != 0 )
  266. return rc;
  267. nul = iob_put ( iobuf, 1 );
  268. *nul = 0;
  269. /* Transmit packet */
  270. return xfer_deliver_iob ( &slam->socket, iobuf );
  271. }
  272. /**
  273. * Handle SLAM master client retry timer expiry
  274. *
  275. * @v timer Master retry timer
  276. * @v fail Failure indicator
  277. */
  278. static void slam_master_timer_expired ( struct retry_timer *timer,
  279. int fail ) {
  280. struct slam_request *slam =
  281. container_of ( timer, struct slam_request, master_timer );
  282. if ( fail ) {
  283. /* Allow timer to stop running. We will terminate the
  284. * connection only if the slave timer times out.
  285. */
  286. DBGC ( slam, "SLAM %p giving up acting as master client\n",
  287. slam );
  288. } else {
  289. /* Retransmit NACK */
  290. start_timer ( timer );
  291. slam_tx_nack ( slam );
  292. }
  293. }
  294. /**
  295. * Handle SLAM slave client retry timer expiry
  296. *
  297. * @v timer Master retry timer
  298. * @v fail Failure indicator
  299. */
  300. static void slam_slave_timer_expired ( struct retry_timer *timer,
  301. int fail ) {
  302. struct slam_request *slam =
  303. container_of ( timer, struct slam_request, slave_timer );
  304. if ( fail ) {
  305. /* Terminate connection */
  306. slam_finished ( slam, -ETIMEDOUT );
  307. } else {
  308. /* Try sending a NACK */
  309. DBGC ( slam, "SLAM %p trying to become master client\n",
  310. slam );
  311. start_timer ( timer );
  312. slam_tx_nack ( slam );
  313. }
  314. }
  315. /****************************************************************************
  316. *
  317. * RX datapath
  318. *
  319. */
  320. /**
  321. * Read and strip a variable-length value from a SLAM packet
  322. *
  323. * @v slam SLAM request
  324. * @v iobuf I/O buffer
  325. * @v value Value to fill in, or NULL to ignore value
  326. * @ret rc Return status code
  327. *
  328. * Reads a variable-length value from the start of the I/O buffer.
  329. */
  330. static int slam_pull_value ( struct slam_request *slam,
  331. struct io_buffer *iobuf,
  332. unsigned long *value ) {
  333. uint8_t *data;
  334. size_t len;
  335. /* Sanity check */
  336. if ( iob_len ( iobuf ) == 0 ) {
  337. DBGC ( slam, "SLAM %p empty value\n", slam );
  338. return -EINVAL;
  339. }
  340. /* Read and verify length of value */
  341. data = iobuf->data;
  342. len = ( *data >> 5 );
  343. if ( ( len == 0 ) ||
  344. ( value && ( len > sizeof ( *value ) ) ) ) {
  345. DBGC ( slam, "SLAM %p invalid value length %zd bytes\n",
  346. slam, len );
  347. return -EINVAL;
  348. }
  349. if ( len > iob_len ( iobuf ) ) {
  350. DBGC ( slam, "SLAM %p value extends beyond I/O buffer\n",
  351. slam );
  352. return -EINVAL;
  353. }
  354. /* Read value */
  355. iob_pull ( iobuf, len );
  356. *value = ( *data & 0x1f );
  357. while ( --len ) {
  358. *value <<= 8;
  359. *value |= *(++data);
  360. }
  361. return 0;
  362. }
  363. /**
  364. * Read and strip SLAM header
  365. *
  366. * @v slam SLAM request
  367. * @v iobuf I/O buffer
  368. * @ret rc Return status code
  369. */
  370. static int slam_pull_header ( struct slam_request *slam,
  371. struct io_buffer *iobuf ) {
  372. void *header = iobuf->data;
  373. int rc;
  374. /* If header matches cached header, just pull it and return */
  375. if ( ( slam->header_len <= iob_len ( iobuf ) ) &&
  376. ( memcmp ( slam->header, iobuf->data, slam->header_len ) == 0 )){
  377. iob_pull ( iobuf, slam->header_len );
  378. return 0;
  379. }
  380. DBGC ( slam, "SLAM %p detected changed header; resetting\n", slam );
  381. /* Read and strip transaction ID, total number of bytes, and
  382. * block size.
  383. */
  384. if ( ( rc = slam_pull_value ( slam, iobuf, NULL ) ) != 0 )
  385. return rc;
  386. if ( ( rc = slam_pull_value ( slam, iobuf,
  387. &slam->total_bytes ) ) != 0 )
  388. return rc;
  389. if ( ( rc = slam_pull_value ( slam, iobuf,
  390. &slam->block_size ) ) != 0 )
  391. return rc;
  392. /* Update the cached header */
  393. slam->header_len = ( iobuf->data - header );
  394. assert ( slam->header_len <= sizeof ( slam->header ) );
  395. memcpy ( slam->header, header, slam->header_len );
  396. /* Calculate number of blocks */
  397. slam->num_blocks = ( ( slam->total_bytes + slam->block_size - 1 ) /
  398. slam->block_size );
  399. DBGC ( slam, "SLAM %p has total bytes %ld, block size %ld, num "
  400. "blocks %ld\n", slam, slam->total_bytes, slam->block_size,
  401. slam->num_blocks );
  402. /* Discard and reset the bitmap */
  403. bitmap_free ( &slam->bitmap );
  404. memset ( &slam->bitmap, 0, sizeof ( slam->bitmap ) );
  405. /* Allocate a new bitmap */
  406. if ( ( rc = bitmap_resize ( &slam->bitmap,
  407. slam->num_blocks ) ) != 0 ) {
  408. /* Failure to allocate a bitmap is fatal */
  409. DBGC ( slam, "SLAM %p could not allocate bitmap for %ld "
  410. "blocks: %s\n", slam, slam->num_blocks,
  411. strerror ( rc ) );
  412. slam_finished ( slam, rc );
  413. return rc;
  414. }
  415. /* Notify recipient of file size */
  416. xfer_seek ( &slam->xfer, slam->total_bytes, SEEK_SET );
  417. return 0;
  418. }
  419. /**
  420. * Receive SLAM data packet
  421. *
  422. * @v mc_socket SLAM multicast socket
  423. * @v iobuf I/O buffer
  424. * @ret rc Return status code
  425. */
  426. static int slam_mc_socket_deliver ( struct xfer_interface *mc_socket,
  427. struct io_buffer *iobuf,
  428. struct xfer_metadata *rx_meta __unused ) {
  429. struct slam_request *slam =
  430. container_of ( mc_socket, struct slam_request, mc_socket );
  431. struct xfer_metadata meta;
  432. unsigned long packet;
  433. size_t len;
  434. int rc;
  435. /* Stop the master client timer. Restart the slave client timer. */
  436. stop_timer ( &slam->master_timer );
  437. stop_timer ( &slam->slave_timer );
  438. start_timer_fixed ( &slam->slave_timer, SLAM_SLAVE_TIMEOUT );
  439. /* Read and strip packet header */
  440. if ( ( rc = slam_pull_header ( slam, iobuf ) ) != 0 )
  441. goto err_discard;
  442. /* Read and strip packet number */
  443. if ( ( rc = slam_pull_value ( slam, iobuf, &packet ) ) != 0 )
  444. goto err_discard;
  445. /* Sanity check packet number */
  446. if ( packet >= slam->num_blocks ) {
  447. DBGC ( slam, "SLAM %p received out-of-range packet %ld "
  448. "(num_blocks=%ld)\n", slam, packet, slam->num_blocks );
  449. rc = -EINVAL;
  450. goto err_discard;
  451. }
  452. /* Sanity check length */
  453. len = iob_len ( iobuf );
  454. if ( len > slam->block_size ) {
  455. DBGC ( slam, "SLAM %p received oversize packet of %zd bytes "
  456. "(block_size=%ld)\n", slam, len, slam->block_size );
  457. rc = -EINVAL;
  458. goto err_discard;
  459. }
  460. if ( ( packet != ( slam->num_blocks - 1 ) ) &&
  461. ( len < slam->block_size ) ) {
  462. DBGC ( slam, "SLAM %p received short packet of %zd bytes "
  463. "(block_size=%ld)\n", slam, len, slam->block_size );
  464. rc = -EINVAL;
  465. goto err_discard;
  466. }
  467. /* If we have already seen this packet, discard it */
  468. if ( bitmap_test ( &slam->bitmap, packet ) ) {
  469. goto discard;
  470. }
  471. /* Pass to recipient */
  472. memset ( &meta, 0, sizeof ( meta ) );
  473. meta.whence = SEEK_SET;
  474. meta.offset = ( packet * slam->block_size );
  475. if ( ( rc = xfer_deliver_iob_meta ( &slam->xfer, iobuf,
  476. &meta ) ) != 0 )
  477. goto err;
  478. /* Mark block as received */
  479. bitmap_set ( &slam->bitmap, packet );
  480. /* If we have received all blocks, terminate */
  481. if ( bitmap_full ( &slam->bitmap ) )
  482. slam_finished ( slam, 0 );
  483. return 0;
  484. err_discard:
  485. discard:
  486. free_iob ( iobuf );
  487. err:
  488. return rc;
  489. }
  490. /**
  491. * Receive SLAM non-data packet
  492. *
  493. * @v socket SLAM unicast socket
  494. * @v iobuf I/O buffer
  495. * @ret rc Return status code
  496. */
  497. static int slam_socket_deliver ( struct xfer_interface *socket,
  498. struct io_buffer *iobuf,
  499. struct xfer_metadata *rx_meta __unused ) {
  500. struct slam_request *slam =
  501. container_of ( socket, struct slam_request, socket );
  502. int rc;
  503. /* Restart the master client timer */
  504. stop_timer ( &slam->master_timer );
  505. start_timer ( &slam->master_timer );
  506. /* Read and strip packet header */
  507. if ( ( rc = slam_pull_header ( slam, iobuf ) ) != 0 )
  508. goto discard;
  509. /* Sanity check */
  510. if ( iob_len ( iobuf ) != 0 ) {
  511. DBGC ( slam, "SLAM %p received trailing garbage:\n", slam );
  512. DBGC_HD ( slam, iobuf->data, iob_len ( iobuf ) );
  513. rc = -EINVAL;
  514. goto discard;
  515. }
  516. /* Discard packet */
  517. free_iob ( iobuf );
  518. /* Send NACK in reply */
  519. slam_tx_nack ( slam );
  520. return 0;
  521. discard:
  522. free_iob ( iobuf );
  523. return rc;
  524. }
  525. /**
  526. * Close SLAM unicast socket
  527. *
  528. * @v socket SLAM unicast socket
  529. * @v rc Reason for close
  530. */
  531. static void slam_socket_close ( struct xfer_interface *socket, int rc ) {
  532. struct slam_request *slam =
  533. container_of ( socket, struct slam_request, socket );
  534. DBGC ( slam, "SLAM %p unicast socket closed: %s\n",
  535. slam, strerror ( rc ) );
  536. slam_finished ( slam, rc );
  537. }
  538. /** SLAM unicast socket data transfer operations */
  539. static struct xfer_interface_operations slam_socket_operations = {
  540. .close = slam_socket_close,
  541. .vredirect = xfer_vreopen,
  542. .window = unlimited_xfer_window,
  543. .alloc_iob = default_xfer_alloc_iob,
  544. .deliver_iob = slam_socket_deliver,
  545. .deliver_raw = xfer_deliver_as_iob,
  546. };
  547. /**
  548. * Close SLAM multicast socket
  549. *
  550. * @v mc_socket SLAM multicast socket
  551. * @v rc Reason for close
  552. */
  553. static void slam_mc_socket_close ( struct xfer_interface *mc_socket, int rc ){
  554. struct slam_request *slam =
  555. container_of ( mc_socket, struct slam_request, mc_socket );
  556. DBGC ( slam, "SLAM %p multicast socket closed: %s\n",
  557. slam, strerror ( rc ) );
  558. slam_finished ( slam, rc );
  559. }
  560. /** SLAM multicast socket data transfer operations */
  561. static struct xfer_interface_operations slam_mc_socket_operations = {
  562. .close = slam_mc_socket_close,
  563. .vredirect = xfer_vreopen,
  564. .window = unlimited_xfer_window,
  565. .alloc_iob = default_xfer_alloc_iob,
  566. .deliver_iob = slam_mc_socket_deliver,
  567. .deliver_raw = xfer_deliver_as_iob,
  568. };
  569. /****************************************************************************
  570. *
  571. * Data transfer interface
  572. *
  573. */
  574. /**
  575. * Close SLAM data transfer interface
  576. *
  577. * @v xfer SLAM data transfer interface
  578. * @v rc Reason for close
  579. */
  580. static void slam_xfer_close ( struct xfer_interface *xfer, int rc ) {
  581. struct slam_request *slam =
  582. container_of ( xfer, struct slam_request, xfer );
  583. DBGC ( slam, "SLAM %p data transfer interface closed: %s\n",
  584. slam, strerror ( rc ) );
  585. slam_finished ( slam, rc );
  586. }
  587. /** SLAM data transfer operations */
  588. static struct xfer_interface_operations slam_xfer_operations = {
  589. .close = slam_xfer_close,
  590. .vredirect = ignore_xfer_vredirect,
  591. .window = unlimited_xfer_window,
  592. .alloc_iob = default_xfer_alloc_iob,
  593. .deliver_iob = xfer_deliver_as_raw,
  594. .deliver_raw = ignore_xfer_deliver_raw,
  595. };
  596. /**
  597. * Parse SLAM URI multicast address
  598. *
  599. * @v slam SLAM request
  600. * @v path Path portion of x-slam:// URI
  601. * @v address Socket address to fill in
  602. * @ret rc Return status code
  603. */
  604. static int slam_parse_multicast_address ( struct slam_request *slam,
  605. const char *path,
  606. struct sockaddr_in *address ) {
  607. char path_dup[ strlen ( path ) /* no +1 */ ];
  608. char *sep;
  609. char *end;
  610. /* Create temporary copy of path, minus the leading '/' */
  611. assert ( *path == '/' );
  612. memcpy ( path_dup, ( path + 1 ) , sizeof ( path_dup ) );
  613. /* Parse port, if present */
  614. sep = strchr ( path_dup, ':' );
  615. if ( sep ) {
  616. *(sep++) = '\0';
  617. address->sin_port = htons ( strtoul ( sep, &end, 0 ) );
  618. if ( *end != '\0' ) {
  619. DBGC ( slam, "SLAM %p invalid multicast port "
  620. "\"%s\"\n", slam, sep );
  621. return -EINVAL;
  622. }
  623. }
  624. /* Parse address */
  625. if ( inet_aton ( path_dup, &address->sin_addr ) == 0 ) {
  626. DBGC ( slam, "SLAM %p invalid multicast address \"%s\"\n",
  627. slam, path_dup );
  628. return -EINVAL;
  629. }
  630. return 0;
  631. }
  632. /**
  633. * Initiate a SLAM request
  634. *
  635. * @v xfer Data transfer interface
  636. * @v uri Uniform Resource Identifier
  637. * @ret rc Return status code
  638. */
  639. static int slam_open ( struct xfer_interface *xfer, struct uri *uri ) {
  640. static const struct sockaddr_in default_multicast = {
  641. .sin_family = AF_INET,
  642. .sin_port = htons ( SLAM_DEFAULT_MULTICAST_PORT ),
  643. .sin_addr = { htonl ( SLAM_DEFAULT_MULTICAST_IP ) },
  644. };
  645. struct slam_request *slam;
  646. struct sockaddr_tcpip server;
  647. struct sockaddr_in multicast;
  648. int rc;
  649. /* Sanity checks */
  650. if ( ! uri->host )
  651. return -EINVAL;
  652. /* Allocate and populate structure */
  653. slam = zalloc ( sizeof ( *slam ) );
  654. if ( ! slam )
  655. return -ENOMEM;
  656. slam->refcnt.free = slam_free;
  657. xfer_init ( &slam->xfer, &slam_xfer_operations, &slam->refcnt );
  658. xfer_init ( &slam->socket, &slam_socket_operations, &slam->refcnt );
  659. xfer_init ( &slam->mc_socket, &slam_mc_socket_operations,
  660. &slam->refcnt );
  661. slam->master_timer.expired = slam_master_timer_expired;
  662. slam->slave_timer.expired = slam_slave_timer_expired;
  663. /* Fake an invalid cached header of { 0x00, ... } */
  664. slam->header_len = 1;
  665. /* Fake parameters for initial NACK */
  666. slam->num_blocks = 1;
  667. if ( ( rc = bitmap_resize ( &slam->bitmap, 1 ) ) != 0 ) {
  668. DBGC ( slam, "SLAM %p could not allocate initial bitmap: "
  669. "%s\n", slam, strerror ( rc ) );
  670. goto err;
  671. }
  672. /* Open unicast socket */
  673. memset ( &server, 0, sizeof ( server ) );
  674. server.st_port = htons ( uri_port ( uri, SLAM_DEFAULT_PORT ) );
  675. if ( ( rc = xfer_open_named_socket ( &slam->socket, SOCK_DGRAM,
  676. ( struct sockaddr * ) &server,
  677. uri->host, NULL ) ) != 0 ) {
  678. DBGC ( slam, "SLAM %p could not open unicast socket: %s\n",
  679. slam, strerror ( rc ) );
  680. goto err;
  681. }
  682. /* Open multicast socket */
  683. memcpy ( &multicast, &default_multicast, sizeof ( multicast ) );
  684. if ( uri->path &&
  685. ( ( rc = slam_parse_multicast_address ( slam, uri->path,
  686. &multicast ) ) != 0 ) ) {
  687. goto err;
  688. }
  689. if ( ( rc = xfer_open_socket ( &slam->mc_socket, SOCK_DGRAM,
  690. ( struct sockaddr * ) &multicast,
  691. ( struct sockaddr * ) &multicast ) ) != 0 ) {
  692. DBGC ( slam, "SLAM %p could not open multicast socket: %s\n",
  693. slam, strerror ( rc ) );
  694. goto err;
  695. }
  696. /* Start slave retry timer */
  697. start_timer_fixed ( &slam->slave_timer, SLAM_SLAVE_TIMEOUT );
  698. /* Attach to parent interface, mortalise self, and return */
  699. xfer_plug_plug ( &slam->xfer, xfer );
  700. ref_put ( &slam->refcnt );
  701. return 0;
  702. err:
  703. slam_finished ( slam, rc );
  704. ref_put ( &slam->refcnt );
  705. return rc;
  706. }
  707. /** SLAM URI opener */
  708. struct uri_opener slam_uri_opener __uri_opener = {
  709. .scheme = "x-slam",
  710. .open = slam_open,
  711. };