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.

peerblk.c 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  1. /*
  2. * Copyright (C) 2015 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 (at your option) 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 <stdlib.h>
  25. #include <stdio.h>
  26. #include <errno.h>
  27. #include <ipxe/http.h>
  28. #include <ipxe/iobuf.h>
  29. #include <ipxe/xfer.h>
  30. #include <ipxe/uri.h>
  31. #include <ipxe/timer.h>
  32. #include <ipxe/profile.h>
  33. #include <ipxe/fault.h>
  34. #include <ipxe/pccrr.h>
  35. #include <ipxe/peerblk.h>
  36. /** @file
  37. *
  38. * Peer Content Caching and Retrieval (PeerDist) protocol block downloads
  39. *
  40. */
  41. /** PeerDist decryption chunksize
  42. *
  43. * This is a policy decision.
  44. */
  45. #define PEERBLK_DECRYPT_CHUNKSIZE 2048
  46. /** PeerDist raw block download attempt initial progress timeout
  47. *
  48. * This is a policy decision.
  49. */
  50. #define PEERBLK_RAW_OPEN_TIMEOUT ( 10 * TICKS_PER_SEC )
  51. /** PeerDist raw block download attempt ongoing progress timeout
  52. *
  53. * This is a policy decision.
  54. */
  55. #define PEERBLK_RAW_RX_TIMEOUT ( 15 * TICKS_PER_SEC )
  56. /** PeerDist retrieval protocol block download attempt initial progress timeout
  57. *
  58. * This is a policy decision.
  59. */
  60. #define PEERBLK_RETRIEVAL_OPEN_TIMEOUT ( 3 * TICKS_PER_SEC )
  61. /** PeerDist retrieval protocol block download attempt ongoing progress timeout
  62. *
  63. * This is a policy decision.
  64. */
  65. #define PEERBLK_RETRIEVAL_RX_TIMEOUT ( 5 * TICKS_PER_SEC )
  66. /** PeerDist maximum number of full download attempt cycles
  67. *
  68. * This is the maximum number of times that we will try a full cycle
  69. * of download attempts (i.e. a retrieval protocol download attempt
  70. * from each discovered peer plus a raw download attempt from the
  71. * origin server).
  72. *
  73. * This is a policy decision.
  74. */
  75. #define PEERBLK_MAX_ATTEMPT_CYCLES 4
  76. /** PeerDist block download profiler */
  77. static struct profiler peerblk_download_profiler __profiler =
  78. { .name = "peerblk.download" };
  79. /** PeerDist block download attempt success profiler */
  80. static struct profiler peerblk_attempt_success_profiler __profiler =
  81. { .name = "peerblk.attempt.success" };
  82. /** PeerDist block download attempt failure profiler */
  83. static struct profiler peerblk_attempt_failure_profiler __profiler =
  84. { .name = "peerblk.attempt.failure" };
  85. /** PeerDist block download attempt timeout profiler */
  86. static struct profiler peerblk_attempt_timeout_profiler __profiler =
  87. { .name = "peerblk.attempt.timeout" };
  88. /** PeerDist block download discovery success profiler */
  89. static struct profiler peerblk_discovery_success_profiler __profiler =
  90. { .name = "peerblk.discovery.success" };
  91. /** PeerDist block download discovery timeout profiler */
  92. static struct profiler peerblk_discovery_timeout_profiler __profiler =
  93. { .name = "peerblk.discovery.timeout" };
  94. /**
  95. * Get profiling timestamp
  96. *
  97. * @ret timestamp Timestamp
  98. */
  99. static inline __attribute__ (( always_inline )) unsigned long
  100. peerblk_timestamp ( void ) {
  101. if ( PROFILING ) {
  102. return currticks();
  103. } else {
  104. return 0;
  105. }
  106. }
  107. /**
  108. * Free PeerDist block download
  109. *
  110. * @v refcnt Reference count
  111. */
  112. static void peerblk_free ( struct refcnt *refcnt ) {
  113. struct peerdist_block *peerblk =
  114. container_of ( refcnt, struct peerdist_block, refcnt );
  115. uri_put ( peerblk->uri );
  116. free ( peerblk->cipherctx );
  117. free ( peerblk );
  118. }
  119. /**
  120. * Reset PeerDist block download attempt
  121. *
  122. * @v peerblk PeerDist block download
  123. * @v rc Reason for reset
  124. */
  125. static void peerblk_reset ( struct peerdist_block *peerblk, int rc ) {
  126. /* Stop decryption process */
  127. process_del ( &peerblk->process );
  128. /* Stop timer */
  129. stop_timer ( &peerblk->timer );
  130. /* Abort any current download attempt */
  131. intf_restart ( &peerblk->raw, rc );
  132. intf_restart ( &peerblk->retrieval, rc );
  133. /* Empty received data buffer */
  134. xferbuf_free ( &peerblk->buffer );
  135. peerblk->pos = 0;
  136. /* Reset digest and free cipher context */
  137. digest_init ( peerblk->digest, peerblk->digestctx );
  138. free ( peerblk->cipherctx );
  139. peerblk->cipherctx = NULL;
  140. peerblk->cipher = NULL;
  141. /* Reset trim thresholds */
  142. peerblk->start = ( peerblk->trim.start - peerblk->range.start );
  143. peerblk->end = ( peerblk->trim.end - peerblk->range.start );
  144. assert ( peerblk->start <= peerblk->end );
  145. }
  146. /**
  147. * Close PeerDist block download
  148. *
  149. * @v peerblk PeerDist block download
  150. * @v rc Reason for close
  151. */
  152. static void peerblk_close ( struct peerdist_block *peerblk, int rc ) {
  153. unsigned long now = peerblk_timestamp();
  154. /* Profile overall block download */
  155. profile_custom ( &peerblk_download_profiler,
  156. ( now - peerblk->started ) );
  157. /* Reset download attempt */
  158. peerblk_reset ( peerblk, rc );
  159. /* Close discovery */
  160. peerdisc_close ( &peerblk->discovery );
  161. /* Shut down all interfaces */
  162. intf_shutdown ( &peerblk->retrieval, rc );
  163. intf_shutdown ( &peerblk->raw, rc );
  164. intf_shutdown ( &peerblk->xfer, rc );
  165. }
  166. /**
  167. * Calculate offset within overall download
  168. *
  169. * @v peerblk PeerDist block download
  170. * @v pos Position within incoming data stream
  171. * @ret offset Offset within overall download
  172. */
  173. static inline __attribute__ (( always_inline )) size_t
  174. peerblk_offset ( struct peerdist_block *peerblk, size_t pos ) {
  175. return ( ( pos - peerblk->start ) + peerblk->offset );
  176. }
  177. /**
  178. * Deliver download attempt data block
  179. *
  180. * @v peerblk PeerDist block download
  181. * @v iobuf I/O buffer
  182. * @v meta Original data transfer metadata
  183. * @v pos Position within incoming data stream
  184. * @ret rc Return status code
  185. */
  186. static int peerblk_deliver ( struct peerdist_block *peerblk,
  187. struct io_buffer *iobuf,
  188. struct xfer_metadata *meta, size_t pos ) {
  189. struct xfer_metadata xfer_meta;
  190. size_t len = iob_len ( iobuf );
  191. size_t start = pos;
  192. size_t end = ( pos + len );
  193. int rc;
  194. /* Discard zero-length packets and packets which lie entirely
  195. * outside the trimmed range.
  196. */
  197. if ( ( start >= peerblk->end ) || ( end <= peerblk->start ) ||
  198. ( len == 0 ) ) {
  199. free_iob ( iobuf );
  200. return 0;
  201. }
  202. /* Truncate data to within trimmed range */
  203. if ( start < peerblk->start ) {
  204. iob_pull ( iobuf, ( peerblk->start - start ) );
  205. start = peerblk->start;
  206. }
  207. if ( end > peerblk->end ) {
  208. iob_unput ( iobuf, ( end - peerblk->end ) );
  209. end = peerblk->end;
  210. }
  211. /* Construct metadata */
  212. memcpy ( &xfer_meta, meta, sizeof ( xfer_meta ) );
  213. xfer_meta.flags |= XFER_FL_ABS_OFFSET;
  214. xfer_meta.offset = peerblk_offset ( peerblk, start );
  215. /* Deliver data */
  216. if ( ( rc = xfer_deliver ( &peerblk->xfer, iob_disown ( iobuf ),
  217. &xfer_meta ) ) != 0 ) {
  218. DBGC ( peerblk, "PEERBLK %p %d.%d could not deliver data: %s\n",
  219. peerblk, peerblk->segment, peerblk->block,
  220. strerror ( rc ) );
  221. return rc;
  222. }
  223. return 0;
  224. }
  225. /**
  226. * Finish PeerDist block download attempt
  227. *
  228. * @v peerblk PeerDist block download
  229. * @v rc Reason for close
  230. */
  231. static void peerblk_done ( struct peerdist_block *peerblk, int rc ) {
  232. struct digest_algorithm *digest = peerblk->digest;
  233. struct peerdisc_segment *segment = peerblk->discovery.segment;
  234. struct peerdisc_peer *head;
  235. struct peerdisc_peer *peer;
  236. uint8_t hash[digest->digestsize];
  237. unsigned long now = peerblk_timestamp();
  238. /* Check for errors on completion */
  239. if ( rc != 0 ) {
  240. DBGC ( peerblk, "PEERBLK %p %d.%d attempt failed: %s\n",
  241. peerblk, peerblk->segment, peerblk->block,
  242. strerror ( rc ) );
  243. goto err;
  244. }
  245. /* Check digest */
  246. digest_final ( digest, peerblk->digestctx, hash );
  247. if ( memcmp ( hash, peerblk->hash, peerblk->digestsize ) != 0 ) {
  248. DBGC ( peerblk, "PEERBLK %p %d.%d digest mismatch:\n",
  249. peerblk, peerblk->segment, peerblk->block );
  250. DBGC_HDA ( peerblk, 0, hash, peerblk->digestsize );
  251. DBGC_HDA ( peerblk, 0, peerblk->hash, peerblk->digestsize );
  252. rc = -EIO;
  253. goto err;
  254. }
  255. /* Profile successful attempt */
  256. profile_custom ( &peerblk_attempt_success_profiler,
  257. ( now - peerblk->attempted ) );
  258. /* Report peer statistics */
  259. head = list_entry ( &segment->peers, struct peerdisc_peer, list );
  260. peer = ( ( peerblk->peer == head ) ? NULL : peerblk->peer );
  261. peerdisc_stat ( &peerblk->xfer, peer, &segment->peers );
  262. /* Close download */
  263. peerblk_close ( peerblk, 0 );
  264. return;
  265. err:
  266. /* Record failure reason and schedule a retry attempt */
  267. profile_custom ( &peerblk_attempt_failure_profiler,
  268. ( now - peerblk->attempted ) );
  269. peerblk_reset ( peerblk, rc );
  270. peerblk->rc = rc;
  271. start_timer_nodelay ( &peerblk->timer );
  272. }
  273. /******************************************************************************
  274. *
  275. * Raw block download attempts (using an HTTP range request)
  276. *
  277. ******************************************************************************
  278. */
  279. /**
  280. * Open PeerDist raw block download attempt
  281. *
  282. * @v peerblk PeerDist block download
  283. * @ret rc Return status code
  284. */
  285. static int peerblk_raw_open ( struct peerdist_block *peerblk ) {
  286. struct http_request_range range;
  287. int rc;
  288. DBGC2 ( peerblk, "PEERBLK %p %d.%d attempting raw range request\n",
  289. peerblk, peerblk->segment, peerblk->block );
  290. /* Construct HTTP range */
  291. memset ( &range, 0, sizeof ( range ) );
  292. range.start = peerblk->range.start;
  293. range.len = ( peerblk->range.end - peerblk->range.start );
  294. /* Initiate range request to retrieve block */
  295. if ( ( rc = http_open ( &peerblk->raw, &http_get, peerblk->uri,
  296. &range, NULL ) ) != 0 ) {
  297. DBGC ( peerblk, "PEERBLK %p %d.%d could not create range "
  298. "request: %s\n", peerblk, peerblk->segment,
  299. peerblk->block, strerror ( rc ) );
  300. return rc;
  301. }
  302. /* Annul HTTP connection (for testing) if applicable. Do not
  303. * report as an immediate error, in order to test our ability
  304. * to recover from a totally unresponsive HTTP server.
  305. */
  306. if ( inject_fault ( PEERBLK_ANNUL_RATE ) )
  307. intf_restart ( &peerblk->raw, 0 );
  308. return 0;
  309. }
  310. /**
  311. * Receive PeerDist raw data
  312. *
  313. * @v peerblk PeerDist block download
  314. * @v iobuf I/O buffer
  315. * @v meta Data transfer metadata
  316. * @ret rc Return status code
  317. */
  318. static int peerblk_raw_rx ( struct peerdist_block *peerblk,
  319. struct io_buffer *iobuf,
  320. struct xfer_metadata *meta ) {
  321. size_t len = iob_len ( iobuf );
  322. size_t pos = peerblk->pos;
  323. size_t mid = ( ( peerblk->range.end - peerblk->range.start ) / 2 );
  324. int rc;
  325. /* Corrupt received data (for testing) if applicable */
  326. inject_corruption ( PEERBLK_CORRUPT_RATE, iobuf->data, len );
  327. /* Fail if data is delivered out of order, since the streaming
  328. * digest requires strict ordering.
  329. */
  330. if ( ( rc = xfer_check_order ( meta, &peerblk->pos, len ) ) != 0 )
  331. goto err;
  332. /* Add data to digest */
  333. digest_update ( peerblk->digest, peerblk->digestctx, iobuf->data, len );
  334. /* Deliver data */
  335. if ( ( rc = peerblk_deliver ( peerblk, iob_disown ( iobuf ), meta,
  336. pos ) ) != 0 )
  337. goto err;
  338. /* Extend download attempt timer */
  339. start_timer_fixed ( &peerblk->timer, PEERBLK_RAW_RX_TIMEOUT );
  340. /* Stall download attempt (for testing) if applicable */
  341. if ( ( pos < mid ) && ( ( pos + len ) >= mid ) &&
  342. ( ( rc = inject_fault ( PEERBLK_STALL_RATE ) ) != 0 ) ) {
  343. intf_restart ( &peerblk->raw, rc );
  344. }
  345. return 0;
  346. err:
  347. free_iob ( iobuf );
  348. peerblk_done ( peerblk, rc );
  349. return rc;
  350. }
  351. /**
  352. * Close PeerDist raw block download attempt
  353. *
  354. * @v peerblk PeerDist block download
  355. * @v rc Reason for close
  356. */
  357. static void peerblk_raw_close ( struct peerdist_block *peerblk, int rc ) {
  358. /* Restart interface */
  359. intf_restart ( &peerblk->raw, rc );
  360. /* Fail immediately if we have an error */
  361. if ( rc != 0 )
  362. goto done;
  363. /* Abort download attempt (for testing) if applicable */
  364. if ( ( rc = inject_fault ( PEERBLK_ABORT_RATE ) ) != 0 )
  365. goto done;
  366. done:
  367. /* Complete download attempt */
  368. peerblk_done ( peerblk, rc );
  369. }
  370. /******************************************************************************
  371. *
  372. * Retrieval protocol block download attempts (using HTTP POST)
  373. *
  374. ******************************************************************************
  375. */
  376. /**
  377. * Construct PeerDist retrieval protocol URI
  378. *
  379. * @v location Peer location
  380. * @ret uri Retrieval URI, or NULL on error
  381. */
  382. static struct uri * peerblk_retrieval_uri ( const char *location ) {
  383. char uri_string[ 7 /* "http://" */ + strlen ( location ) +
  384. sizeof ( PEERDIST_MAGIC_PATH /* includes NUL */ ) ];
  385. /* Construct URI string */
  386. snprintf ( uri_string, sizeof ( uri_string ),
  387. ( "http://%s" PEERDIST_MAGIC_PATH ), location );
  388. /* Parse URI string */
  389. return parse_uri ( uri_string );
  390. }
  391. /**
  392. * Open PeerDist retrieval protocol block download attempt
  393. *
  394. * @v peerblk PeerDist block download
  395. * @v location Peer location
  396. * @ret rc Return status code
  397. */
  398. static int peerblk_retrieval_open ( struct peerdist_block *peerblk,
  399. const char *location ) {
  400. size_t digestsize = peerblk->digestsize;
  401. peerdist_msg_getblks_t ( digestsize, 1, 0 ) req;
  402. peerblk_msg_blk_t ( digestsize, 0, 0, 0 ) *rsp;
  403. struct http_request_content content;
  404. struct uri *uri;
  405. int rc;
  406. DBGC2 ( peerblk, "PEERBLK %p %d.%d attempting retrieval from %s\n",
  407. peerblk, peerblk->segment, peerblk->block, location );
  408. /* Construct block fetch request */
  409. memset ( &req, 0, sizeof ( req ) );
  410. req.getblks.hdr.version.raw = htonl ( PEERDIST_MSG_GETBLKS_VERSION );
  411. req.getblks.hdr.type = htonl ( PEERDIST_MSG_GETBLKS_TYPE );
  412. req.getblks.hdr.len = htonl ( sizeof ( req ) );
  413. req.getblks.hdr.algorithm = htonl ( PEERDIST_MSG_AES_128_CBC );
  414. req.segment.segment.digestsize = htonl ( digestsize );
  415. memcpy ( req.segment.id, peerblk->id, digestsize );
  416. req.ranges.ranges.count = htonl ( 1 );
  417. req.ranges.range[0].first = htonl ( peerblk->block );
  418. req.ranges.range[0].count = htonl ( 1 );
  419. /* Construct POST request content */
  420. memset ( &content, 0, sizeof ( content ) );
  421. content.data = &req;
  422. content.len = sizeof ( req );
  423. /* Construct URI */
  424. if ( ( uri = peerblk_retrieval_uri ( location ) ) == NULL ) {
  425. rc = -ENOMEM;
  426. goto err_uri;
  427. }
  428. /* Update trim thresholds */
  429. peerblk->start += offsetof ( typeof ( *rsp ), msg.vrf );
  430. peerblk->end += offsetof ( typeof ( *rsp ), msg.vrf );
  431. /* Initiate HTTP POST to retrieve block */
  432. if ( ( rc = http_open ( &peerblk->retrieval, &http_post, uri,
  433. NULL, &content ) ) != 0 ) {
  434. DBGC ( peerblk, "PEERBLK %p %d.%d could not create retrieval "
  435. "request: %s\n", peerblk, peerblk->segment,
  436. peerblk->block, strerror ( rc ) );
  437. goto err_open;
  438. }
  439. /* Annul HTTP connection (for testing) if applicable. Do not
  440. * report as an immediate error, in order to test our ability
  441. * to recover from a totally unresponsive HTTP server.
  442. */
  443. if ( inject_fault ( PEERBLK_ANNUL_RATE ) )
  444. intf_restart ( &peerblk->retrieval, 0 );
  445. err_open:
  446. uri_put ( uri );
  447. err_uri:
  448. return rc;
  449. }
  450. /**
  451. * Receive PeerDist retrieval protocol data
  452. *
  453. * @v peerblk PeerDist block download
  454. * @v iobuf I/O buffer
  455. * @v meta Data transfer metadata
  456. * @ret rc Return status code
  457. */
  458. static int peerblk_retrieval_rx ( struct peerdist_block *peerblk,
  459. struct io_buffer *iobuf,
  460. struct xfer_metadata *meta ) {
  461. size_t len = iob_len ( iobuf );
  462. size_t start;
  463. size_t end;
  464. size_t before;
  465. size_t after;
  466. size_t cut;
  467. int rc;
  468. /* Some genius at Microsoft thought it would be a great idea
  469. * to place the AES-CBC initialisation vector *after* the
  470. * encrypted data, thereby making it logically impossible to
  471. * decrypt each packet as it arrives.
  472. *
  473. * To work around this mindless stupidity, we deliver the
  474. * ciphertext as-is and later use xfer_buffer() to obtain
  475. * access to the underlying data transfer buffer in order to
  476. * perform the decryption.
  477. *
  478. * There will be some data both before and after the bytes
  479. * corresponding to the trimmed plaintext: a MSG_BLK
  480. * header/footer, some block padding for the AES-CBC cipher,
  481. * and a possibly large quantity of unwanted ciphertext which
  482. * is excluded from the trimmed content range. We store this
  483. * data in a local data transfer buffer. If the amount of
  484. * data to be stored is too large, we will fail allocation and
  485. * so eventually fall back to using a range request (which
  486. * does not require this kind of temporary storage
  487. * allocation).
  488. */
  489. /* Corrupt received data (for testing) if applicable */
  490. inject_corruption ( PEERBLK_CORRUPT_RATE, iobuf->data, len );
  491. /* Calculate start and end positions of this buffer */
  492. start = peerblk->pos;
  493. if ( meta->flags & XFER_FL_ABS_OFFSET )
  494. start = 0;
  495. start += meta->offset;
  496. end = ( start + len );
  497. /* Buffer any data before the trimmed content */
  498. if ( ( start < peerblk->start ) && ( len > 0 ) ) {
  499. /* Calculate length of data before the trimmed content */
  500. before = ( peerblk->start - start );
  501. if ( before > len )
  502. before = len;
  503. /* Buffer data before the trimmed content */
  504. if ( ( rc = xferbuf_write ( &peerblk->buffer, start,
  505. iobuf->data, before ) ) != 0 ) {
  506. DBGC ( peerblk, "PEERBLK %p %d.%d could not buffer "
  507. "data: %s\n", peerblk, peerblk->segment,
  508. peerblk->block, strerror ( rc ) );
  509. goto err;
  510. }
  511. }
  512. /* Buffer any data after the trimmed content */
  513. if ( ( end > peerblk->end ) && ( len > 0 ) ) {
  514. /* Calculate length of data after the trimmed content */
  515. after = ( end - peerblk->end );
  516. if ( after > len )
  517. after = len;
  518. /* Buffer data after the trimmed content */
  519. cut = ( peerblk->end - peerblk->start );
  520. if ( ( rc = xferbuf_write ( &peerblk->buffer,
  521. ( end - after - cut ),
  522. ( iobuf->data + len - after ),
  523. after ) ) != 0 ) {
  524. DBGC ( peerblk, "PEERBLK %p %d.%d could not buffer "
  525. "data: %s\n", peerblk, peerblk->segment,
  526. peerblk->block, strerror ( rc ) );
  527. goto err;
  528. }
  529. }
  530. /* Deliver any remaining data */
  531. if ( ( rc = peerblk_deliver ( peerblk, iob_disown ( iobuf ), meta,
  532. start ) ) != 0 )
  533. goto err;
  534. /* Update position */
  535. peerblk->pos = end;
  536. /* Extend download attempt timer */
  537. start_timer_fixed ( &peerblk->timer, PEERBLK_RETRIEVAL_RX_TIMEOUT );
  538. /* Stall download attempt (for testing) if applicable */
  539. if ( ( start < peerblk->end ) && ( end >= peerblk->end ) &&
  540. ( ( rc = inject_fault ( PEERBLK_STALL_RATE ) ) != 0 ) ) {
  541. intf_restart ( &peerblk->retrieval, rc );
  542. }
  543. return 0;
  544. err:
  545. free_iob ( iobuf );
  546. peerblk_done ( peerblk, rc );
  547. return rc;
  548. }
  549. /**
  550. * Parse retrieval protocol message header
  551. *
  552. * @v peerblk PeerDist block download
  553. * @ret rc Return status code
  554. */
  555. static int peerblk_parse_header ( struct peerdist_block *peerblk ) {
  556. struct {
  557. struct peerdist_msg_transport_header hdr;
  558. struct peerdist_msg_header msg;
  559. } __attribute__ (( packed )) *msg = peerblk->buffer.data;
  560. struct cipher_algorithm *cipher;
  561. size_t len = peerblk->buffer.len;
  562. size_t keylen = 0;
  563. int rc;
  564. /* Check message length */
  565. if ( len < sizeof ( *msg ) ) {
  566. DBGC ( peerblk, "PEERBLK %p %d.%d message too short for header "
  567. "(%zd bytes)\n", peerblk, peerblk->segment,
  568. peerblk->block, len );
  569. return -ERANGE;
  570. }
  571. /* Check message type */
  572. if ( msg->msg.type != htonl ( PEERDIST_MSG_BLK_TYPE ) ) {
  573. DBGC ( peerblk, "PEERBLK %p %d.%d unexpected message type "
  574. "%#08x\n", peerblk, peerblk->segment, peerblk->block,
  575. ntohl ( msg->msg.type ) );
  576. return -EPROTO;
  577. }
  578. /* Determine cipher algorithm and key length */
  579. cipher = &aes_cbc_algorithm;
  580. switch ( msg->msg.algorithm ) {
  581. case htonl ( PEERDIST_MSG_PLAINTEXT ) :
  582. cipher = NULL;
  583. break;
  584. case htonl ( PEERDIST_MSG_AES_128_CBC ) :
  585. keylen = ( 128 / 8 );
  586. break;
  587. case htonl ( PEERDIST_MSG_AES_192_CBC ) :
  588. keylen = ( 192 / 8 );
  589. break;
  590. case htonl ( PEERDIST_MSG_AES_256_CBC ) :
  591. keylen = ( 256 / 8 );
  592. break;
  593. default:
  594. DBGC ( peerblk, "PEERBLK %p %d.%d unrecognised algorithm "
  595. "%#08x\n", peerblk, peerblk->segment, peerblk->block,
  596. ntohl ( msg->msg.algorithm ) );
  597. return -ENOTSUP;
  598. }
  599. DBGC2 ( peerblk, "PEERBLK %p %d.%d using %s with %zd-bit key\n",
  600. peerblk, peerblk->segment, peerblk->block,
  601. ( cipher ? cipher->name : "plaintext" ), ( 8 * keylen ) );
  602. /* Sanity check key length against maximum secret length */
  603. if ( keylen > peerblk->digestsize ) {
  604. DBGC ( peerblk, "PEERBLK %p %d.%d %zd-byte secret too short "
  605. "for %zd-bit key\n", peerblk, peerblk->segment,
  606. peerblk->block, peerblk->digestsize, ( 8 * keylen ) );
  607. return -EPROTO;
  608. }
  609. /* Allocate cipher context, if applicable. Freeing the cipher
  610. * context (on error or otherwise) is handled by peerblk_reset().
  611. */
  612. peerblk->cipher = cipher;
  613. assert ( peerblk->cipherctx == NULL );
  614. if ( cipher ) {
  615. peerblk->cipherctx = malloc ( cipher->ctxsize );
  616. if ( ! peerblk->cipherctx )
  617. return -ENOMEM;
  618. }
  619. /* Initialise cipher, if applicable */
  620. if ( cipher &&
  621. ( rc = cipher_setkey ( cipher, peerblk->cipherctx, peerblk->secret,
  622. keylen ) ) != 0 ) {
  623. DBGC ( peerblk, "PEERBLK %p %d.%d could not set key: %s\n",
  624. peerblk, peerblk->segment, peerblk->block,
  625. strerror ( rc ) );
  626. return rc;
  627. }
  628. return 0;
  629. }
  630. /**
  631. * Parse retrieval protocol message segment and block details
  632. *
  633. * @v peerblk PeerDist block download
  634. * @v buf_len Length of buffered data to fill in
  635. * @ret rc Return status code
  636. */
  637. static int peerblk_parse_block ( struct peerdist_block *peerblk,
  638. size_t *buf_len ) {
  639. size_t digestsize = peerblk->digestsize;
  640. peerblk_msg_blk_t ( digestsize, 0, 0, 0 ) *msg = peerblk->buffer.data;
  641. size_t len = peerblk->buffer.len;
  642. size_t data_len;
  643. size_t total;
  644. /* Check message length */
  645. if ( len < offsetof ( typeof ( *msg ), msg.block.data ) ) {
  646. DBGC ( peerblk, "PEERBLK %p %d.%d message too short for "
  647. "zero-length data (%zd bytes)\n", peerblk,
  648. peerblk->segment, peerblk->block, len );
  649. return -ERANGE;
  650. }
  651. /* Check digest size */
  652. if ( ntohl ( msg->msg.segment.segment.digestsize ) != digestsize ) {
  653. DBGC ( peerblk, "PEERBLK %p %d.%d incorrect digest size %d\n",
  654. peerblk, peerblk->segment, peerblk->block,
  655. ntohl ( msg->msg.segment.segment.digestsize ) );
  656. return -EPROTO;
  657. }
  658. /* Check segment ID */
  659. if ( memcmp ( msg->msg.segment.id, peerblk->id, digestsize ) != 0 ) {
  660. DBGC ( peerblk, "PEERBLK %p %d.%d segment ID mismatch\n",
  661. peerblk, peerblk->segment, peerblk->block );
  662. return -EPROTO;
  663. }
  664. /* Check block ID */
  665. if ( ntohl ( msg->msg.index ) != peerblk->block ) {
  666. DBGC ( peerblk, "PEERBLK %p %d.%d block ID mismatch (got %d)\n",
  667. peerblk, peerblk->segment, peerblk->block,
  668. ntohl ( msg->msg.index ) );
  669. return -EPROTO;
  670. }
  671. /* Check for missing blocks */
  672. data_len = be32_to_cpu ( msg->msg.block.block.len );
  673. if ( ! data_len ) {
  674. DBGC ( peerblk, "PEERBLK %p %d.%d block not found\n",
  675. peerblk, peerblk->segment, peerblk->block );
  676. return -ENOENT;
  677. }
  678. /* Check for underlength blocks */
  679. if ( data_len < ( peerblk->range.end - peerblk->range.start ) ) {
  680. DBGC ( peerblk, "PEERBLK %p %d.%d underlength block (%zd "
  681. "bytes)\n", peerblk, peerblk->segment, peerblk->block,
  682. data_len );
  683. return -ERANGE;
  684. }
  685. /* Calculate buffered data length (i.e. excluding data which
  686. * was delivered to the final data transfer buffer).
  687. */
  688. *buf_len = ( data_len - ( peerblk->end - peerblk->start ) );
  689. /* Describe data before the trimmed content */
  690. peerblk->decrypt[PEERBLK_BEFORE].xferbuf = &peerblk->buffer;
  691. peerblk->decrypt[PEERBLK_BEFORE].offset =
  692. offsetof ( typeof ( *msg ), msg.block.data );
  693. peerblk->decrypt[PEERBLK_BEFORE].len =
  694. ( peerblk->start -
  695. offsetof ( typeof ( *msg ), msg.block.data ) );
  696. total = peerblk->decrypt[PEERBLK_BEFORE].len;
  697. /* Describe data within the trimmed content */
  698. peerblk->decrypt[PEERBLK_DURING].offset =
  699. peerblk_offset ( peerblk, peerblk->start );
  700. peerblk->decrypt[PEERBLK_DURING].len =
  701. ( peerblk->end - peerblk->start );
  702. total += peerblk->decrypt[PEERBLK_DURING].len;
  703. /* Describe data after the trimmed content */
  704. peerblk->decrypt[PEERBLK_AFTER].xferbuf = &peerblk->buffer;
  705. peerblk->decrypt[PEERBLK_AFTER].offset = peerblk->start;
  706. peerblk->decrypt[PEERBLK_AFTER].len =
  707. ( offsetof ( typeof ( *msg ), msg.block.data )
  708. + *buf_len - peerblk->start );
  709. total += peerblk->decrypt[PEERBLK_AFTER].len;
  710. /* Sanity check */
  711. assert ( total == be32_to_cpu ( msg->msg.block.block.len ) );
  712. /* Initialise cipher and digest lengths */
  713. peerblk->cipher_remaining = total;
  714. peerblk->digest_remaining =
  715. ( peerblk->range.end - peerblk->range.start );
  716. assert ( peerblk->cipher_remaining >= peerblk->digest_remaining );
  717. return 0;
  718. }
  719. /**
  720. * Parse retrieval protocol message useless details
  721. *
  722. * @v peerblk PeerDist block download
  723. * @v buf_len Length of buffered data
  724. * @v vrf_len Length of uselessness to fill in
  725. * @ret rc Return status code
  726. */
  727. static int peerblk_parse_useless ( struct peerdist_block *peerblk,
  728. size_t buf_len, size_t *vrf_len ) {
  729. size_t digestsize = peerblk->digestsize;
  730. peerblk_msg_blk_t ( digestsize, buf_len, 0, 0 ) *msg =
  731. peerblk->buffer.data;
  732. size_t len = peerblk->buffer.len;
  733. /* Check message length */
  734. if ( len < offsetof ( typeof ( *msg ), msg.vrf.data ) ) {
  735. DBGC ( peerblk, "PEERBLK %p %d.%d message too short for "
  736. "zero-length uselessness (%zd bytes)\n", peerblk,
  737. peerblk->segment, peerblk->block, len );
  738. return -ERANGE;
  739. }
  740. /* Extract length of uselessness */
  741. *vrf_len = be32_to_cpu ( msg->msg.vrf.vrf.len );
  742. return 0;
  743. }
  744. /**
  745. * Parse retrieval protocol message initialisation vector details
  746. *
  747. * @v peerblk PeerDist block download
  748. * @v buf_len Length of buffered data
  749. * @v vrf_len Length of uselessness
  750. * @ret rc Return status code
  751. */
  752. static int peerblk_parse_iv ( struct peerdist_block *peerblk, size_t buf_len,
  753. size_t vrf_len ) {
  754. size_t digestsize = peerblk->digestsize;
  755. size_t blksize = peerblk->cipher->blocksize;
  756. peerblk_msg_blk_t ( digestsize, buf_len, vrf_len, blksize ) *msg =
  757. peerblk->buffer.data;
  758. size_t len = peerblk->buffer.len;
  759. /* Check message length */
  760. if ( len < sizeof ( *msg ) ) {
  761. DBGC ( peerblk, "PEERBLK %p %d.%d message too short for "
  762. "initialisation vector (%zd bytes)\n", peerblk,
  763. peerblk->segment, peerblk->block, len );
  764. return -ERANGE;
  765. }
  766. /* Check initialisation vector size */
  767. if ( ntohl ( msg->msg.iv.iv.blksize ) != blksize ) {
  768. DBGC ( peerblk, "PEERBLK %p %d.%d incorrect IV size %d\n",
  769. peerblk, peerblk->segment, peerblk->block,
  770. ntohl ( msg->msg.iv.iv.blksize ) );
  771. return -EPROTO;
  772. }
  773. /* Set initialisation vector */
  774. cipher_setiv ( peerblk->cipher, peerblk->cipherctx, msg->msg.iv.data );
  775. return 0;
  776. }
  777. /**
  778. * Read from decryption buffers
  779. *
  780. * @v peerblk PeerDist block download
  781. * @v data Data buffer
  782. * @v len Length to read
  783. * @ret rc Return status code
  784. */
  785. static int peerblk_decrypt_read ( struct peerdist_block *peerblk,
  786. void *data, size_t len ) {
  787. struct peerdist_block_decrypt *decrypt = peerblk->decrypt;
  788. size_t frag_len;
  789. int rc;
  790. /* Read from each decryption buffer in turn */
  791. for ( ; len ; decrypt++, data += frag_len, len -= frag_len ) {
  792. /* Calculate length to use from this buffer */
  793. frag_len = decrypt->len;
  794. if ( frag_len > len )
  795. frag_len = len;
  796. if ( ! frag_len )
  797. continue;
  798. /* Read from this buffer */
  799. if ( ( rc = xferbuf_read ( decrypt->xferbuf, decrypt->offset,
  800. data, frag_len ) ) != 0 )
  801. return rc;
  802. }
  803. return 0;
  804. }
  805. /**
  806. * Write to decryption buffers and update offsets and lengths
  807. *
  808. * @v peerblk PeerDist block download
  809. * @v data Data buffer
  810. * @v len Length to read
  811. * @ret rc Return status code
  812. */
  813. static int peerblk_decrypt_write ( struct peerdist_block *peerblk,
  814. const void *data, size_t len ) {
  815. struct peerdist_block_decrypt *decrypt = peerblk->decrypt;
  816. size_t frag_len;
  817. int rc;
  818. /* Write to each decryption buffer in turn */
  819. for ( ; len ; decrypt++, data += frag_len, len -= frag_len ) {
  820. /* Calculate length to use from this buffer */
  821. frag_len = decrypt->len;
  822. if ( frag_len > len )
  823. frag_len = len;
  824. if ( ! frag_len )
  825. continue;
  826. /* Write to this buffer */
  827. if ( ( rc = xferbuf_write ( decrypt->xferbuf, decrypt->offset,
  828. data, frag_len ) ) != 0 )
  829. return rc;
  830. /* Update offset and length */
  831. decrypt->offset += frag_len;
  832. decrypt->len -= frag_len;
  833. }
  834. return 0;
  835. }
  836. /**
  837. * Decrypt one chunk of PeerDist retrieval protocol data
  838. *
  839. * @v peerblk PeerDist block download
  840. */
  841. static void peerblk_decrypt ( struct peerdist_block *peerblk ) {
  842. struct cipher_algorithm *cipher = peerblk->cipher;
  843. struct digest_algorithm *digest = peerblk->digest;
  844. struct xfer_buffer *xferbuf;
  845. size_t cipher_len;
  846. size_t digest_len;
  847. void *data;
  848. int rc;
  849. /* Sanity check */
  850. assert ( ( PEERBLK_DECRYPT_CHUNKSIZE % cipher->blocksize ) == 0 );
  851. /* Get the underlying data transfer buffer */
  852. xferbuf = xfer_buffer ( &peerblk->xfer );
  853. if ( ! xferbuf ) {
  854. DBGC ( peerblk, "PEERBLK %p %d.%d has no underlying data "
  855. "transfer buffer\n", peerblk, peerblk->segment,
  856. peerblk->block );
  857. rc = -ENOTSUP;
  858. goto err_xfer_buffer;
  859. }
  860. peerblk->decrypt[PEERBLK_DURING].xferbuf = xferbuf;
  861. /* Calculate cipher and digest lengths */
  862. cipher_len = PEERBLK_DECRYPT_CHUNKSIZE;
  863. if ( cipher_len > peerblk->cipher_remaining )
  864. cipher_len = peerblk->cipher_remaining;
  865. digest_len = cipher_len;
  866. if ( digest_len > peerblk->digest_remaining )
  867. digest_len = peerblk->digest_remaining;
  868. assert ( ( cipher_len & ( cipher->blocksize - 1 ) ) == 0 );
  869. /* Allocate temporary data buffer */
  870. data = malloc ( cipher_len );
  871. if ( ! data ) {
  872. rc = -ENOMEM;
  873. goto err_alloc_data;
  874. }
  875. /* Read ciphertext */
  876. if ( ( rc = peerblk_decrypt_read ( peerblk, data, cipher_len ) ) != 0 ){
  877. DBGC ( peerblk, "PEERBLK %p %d.%d could not read ciphertext: "
  878. "%s\n", peerblk, peerblk->segment, peerblk->block,
  879. strerror ( rc ) );
  880. goto err_read;
  881. }
  882. /* Decrypt data */
  883. cipher_decrypt ( cipher, peerblk->cipherctx, data, data, cipher_len );
  884. /* Add data to digest */
  885. digest_update ( digest, peerblk->digestctx, data, digest_len );
  886. /* Write plaintext */
  887. if ( ( rc = peerblk_decrypt_write ( peerblk, data, cipher_len ) ) != 0){
  888. DBGC ( peerblk, "PEERBLK %p %d.%d could not write plaintext: "
  889. "%s\n", peerblk, peerblk->segment, peerblk->block,
  890. strerror ( rc ) );
  891. goto err_write;
  892. }
  893. /* Consume input */
  894. peerblk->cipher_remaining -= cipher_len;
  895. peerblk->digest_remaining -= digest_len;
  896. /* Free temporary data buffer */
  897. free ( data );
  898. /* Continue processing until all input is consumed */
  899. if ( peerblk->cipher_remaining )
  900. return;
  901. /* Complete download attempt */
  902. peerblk_done ( peerblk, 0 );
  903. return;
  904. err_write:
  905. err_read:
  906. free ( data );
  907. err_alloc_data:
  908. err_xfer_buffer:
  909. peerblk_done ( peerblk, rc );
  910. }
  911. /**
  912. * Close PeerDist retrieval protocol block download attempt
  913. *
  914. * @v peerblk PeerDist block download
  915. * @v rc Reason for close
  916. */
  917. static void peerblk_retrieval_close ( struct peerdist_block *peerblk, int rc ) {
  918. size_t buf_len;
  919. size_t vrf_len;
  920. /* Restart interface */
  921. intf_restart ( &peerblk->retrieval, rc );
  922. /* Fail immediately if we have an error */
  923. if ( rc != 0 )
  924. goto done;
  925. /* Abort download attempt (for testing) if applicable */
  926. if ( ( rc = inject_fault ( PEERBLK_ABORT_RATE ) ) != 0 )
  927. goto done;
  928. /* Parse message header */
  929. if ( ( rc = peerblk_parse_header ( peerblk ) ) != 0 )
  930. goto done;
  931. /* Parse message segment and block details */
  932. if ( ( rc = peerblk_parse_block ( peerblk, &buf_len ) ) != 0 )
  933. goto done;
  934. /* If the block was plaintext, then there is nothing more to do */
  935. if ( ! peerblk->cipher )
  936. goto done;
  937. /* Parse message useless details */
  938. if ( ( rc = peerblk_parse_useless ( peerblk, buf_len, &vrf_len ) ) != 0)
  939. goto done;
  940. /* Parse message initialisation vector details */
  941. if ( ( rc = peerblk_parse_iv ( peerblk, buf_len, vrf_len ) ) != 0 )
  942. goto done;
  943. /* Fail if decryption length is not aligned to the cipher block size */
  944. if ( peerblk->cipher_remaining & ( peerblk->cipher->blocksize - 1 ) ) {
  945. DBGC ( peerblk, "PEERBLK %p %d.%d unaligned data length %zd\n",
  946. peerblk, peerblk->segment, peerblk->block,
  947. peerblk->cipher_remaining );
  948. rc = -EPROTO;
  949. goto done;
  950. }
  951. /* Stop the download attempt timer: there is no point in
  952. * timing out while decrypting.
  953. */
  954. stop_timer ( &peerblk->timer );
  955. /* Start decryption process */
  956. process_add ( &peerblk->process );
  957. return;
  958. done:
  959. /* Complete download attempt */
  960. peerblk_done ( peerblk, rc );
  961. }
  962. /******************************************************************************
  963. *
  964. * Retry policy
  965. *
  966. ******************************************************************************
  967. */
  968. /**
  969. * Handle PeerDist retry timer expiry
  970. *
  971. * @v timer Retry timer
  972. * @v over Failure indicator
  973. */
  974. static void peerblk_expired ( struct retry_timer *timer, int over __unused ) {
  975. struct peerdist_block *peerblk =
  976. container_of ( timer, struct peerdist_block, timer );
  977. struct peerdisc_segment *segment = peerblk->discovery.segment;
  978. struct peerdisc_peer *head;
  979. unsigned long now = peerblk_timestamp();
  980. const char *location;
  981. int rc;
  982. /* Profile discovery timeout, if applicable */
  983. if ( ( peerblk->peer == NULL ) && ( timer->timeout != 0 ) ) {
  984. profile_custom ( &peerblk_discovery_timeout_profiler,
  985. ( now - peerblk->started ) );
  986. DBGC ( peerblk, "PEERBLK %p %d.%d discovery timed out after "
  987. "%ld ticks\n", peerblk, peerblk->segment,
  988. peerblk->block, timer->timeout );
  989. }
  990. /* Profile download timeout, if applicable */
  991. if ( ( peerblk->peer != NULL ) && ( timer->timeout != 0 ) ) {
  992. profile_custom ( &peerblk_attempt_timeout_profiler,
  993. ( now - peerblk->attempted ) );
  994. DBGC ( peerblk, "PEERBLK %p %d.%d timed out after %ld ticks\n",
  995. peerblk, peerblk->segment, peerblk->block,
  996. timer->timeout );
  997. }
  998. /* Abort any current download attempt */
  999. peerblk_reset ( peerblk, -ETIMEDOUT );
  1000. /* Record attempt start time */
  1001. peerblk->attempted = now;
  1002. /* If we have exceeded our maximum number of attempt cycles
  1003. * (each cycle comprising a retrieval protocol download from
  1004. * each peer in the list followed by a raw download from the
  1005. * origin server), then abort the overall download.
  1006. */
  1007. head = list_entry ( &segment->peers, struct peerdisc_peer, list );
  1008. if ( ( peerblk->peer == head ) &&
  1009. ( ++peerblk->cycles >= PEERBLK_MAX_ATTEMPT_CYCLES ) ) {
  1010. rc = peerblk->rc;
  1011. assert ( rc != 0 );
  1012. goto err;
  1013. }
  1014. /* If we have not yet made any download attempts, then move to
  1015. * the start of the peer list.
  1016. */
  1017. if ( peerblk->peer == NULL )
  1018. peerblk->peer = head;
  1019. /* Attempt retrieval protocol download from next usable peer */
  1020. list_for_each_entry_continue ( peerblk->peer, &segment->peers, list ) {
  1021. /* Attempt retrieval protocol download from this peer */
  1022. location = peerblk->peer->location;
  1023. if ( ( rc = peerblk_retrieval_open ( peerblk,
  1024. location ) ) != 0 ) {
  1025. /* Non-fatal: continue to try next peer */
  1026. continue;
  1027. }
  1028. /* Start download attempt timer */
  1029. peerblk->rc = -ETIMEDOUT;
  1030. start_timer_fixed ( &peerblk->timer,
  1031. PEERBLK_RETRIEVAL_OPEN_TIMEOUT );
  1032. return;
  1033. }
  1034. /* Attempt raw download */
  1035. if ( ( rc = peerblk_raw_open ( peerblk ) ) != 0 )
  1036. goto err;
  1037. /* Start download attempt timer */
  1038. peerblk->rc = -ETIMEDOUT;
  1039. start_timer_fixed ( &peerblk->timer, PEERBLK_RAW_OPEN_TIMEOUT );
  1040. return;
  1041. err:
  1042. peerblk_close ( peerblk, rc );
  1043. }
  1044. /**
  1045. * Handle PeerDist peer discovery
  1046. *
  1047. * @v discovery PeerDist discovery client
  1048. */
  1049. static void peerblk_discovered ( struct peerdisc_client *discovery ) {
  1050. struct peerdist_block *peerblk =
  1051. container_of ( discovery, struct peerdist_block, discovery );
  1052. unsigned long now = peerblk_timestamp();
  1053. /* Do nothing unless we are still waiting for the initial
  1054. * discovery timeout.
  1055. */
  1056. if ( ( peerblk->peer != NULL ) || ( peerblk->timer.timeout == 0 ) )
  1057. return;
  1058. /* Schedule an immediate retry */
  1059. start_timer_nodelay ( &peerblk->timer );
  1060. /* Profile discovery success */
  1061. profile_custom ( &peerblk_discovery_success_profiler,
  1062. ( now - peerblk->started ) );
  1063. }
  1064. /******************************************************************************
  1065. *
  1066. * Opener
  1067. *
  1068. ******************************************************************************
  1069. */
  1070. /** PeerDist block download data transfer interface operations */
  1071. static struct interface_operation peerblk_xfer_operations[] = {
  1072. INTF_OP ( intf_close, struct peerdist_block *, peerblk_close ),
  1073. };
  1074. /** PeerDist block download data transfer interface descriptor */
  1075. static struct interface_descriptor peerblk_xfer_desc =
  1076. INTF_DESC ( struct peerdist_block, xfer, peerblk_xfer_operations );
  1077. /** PeerDist block download raw data interface operations */
  1078. static struct interface_operation peerblk_raw_operations[] = {
  1079. INTF_OP ( xfer_deliver, struct peerdist_block *, peerblk_raw_rx ),
  1080. INTF_OP ( intf_close, struct peerdist_block *, peerblk_raw_close ),
  1081. };
  1082. /** PeerDist block download raw data interface descriptor */
  1083. static struct interface_descriptor peerblk_raw_desc =
  1084. INTF_DESC ( struct peerdist_block, raw, peerblk_raw_operations );
  1085. /** PeerDist block download retrieval protocol interface operations */
  1086. static struct interface_operation peerblk_retrieval_operations[] = {
  1087. INTF_OP ( xfer_deliver, struct peerdist_block *, peerblk_retrieval_rx ),
  1088. INTF_OP ( intf_close, struct peerdist_block *, peerblk_retrieval_close),
  1089. };
  1090. /** PeerDist block download retrieval protocol interface descriptor */
  1091. static struct interface_descriptor peerblk_retrieval_desc =
  1092. INTF_DESC ( struct peerdist_block, retrieval,
  1093. peerblk_retrieval_operations );
  1094. /** PeerDist block download decryption process descriptor */
  1095. static struct process_descriptor peerblk_process_desc =
  1096. PROC_DESC ( struct peerdist_block, process, peerblk_decrypt );
  1097. /** PeerDist block download discovery operations */
  1098. static struct peerdisc_client_operations peerblk_discovery_operations = {
  1099. .discovered = peerblk_discovered,
  1100. };
  1101. /**
  1102. * Open PeerDist block download
  1103. *
  1104. * @v xfer Data transfer interface
  1105. * @v uri Original URI
  1106. * @v info Content information block
  1107. * @ret rc Return status code
  1108. */
  1109. int peerblk_open ( struct interface *xfer, struct uri *uri,
  1110. struct peerdist_info_block *block ) {
  1111. const struct peerdist_info_segment *segment = block->segment;
  1112. const struct peerdist_info *info = segment->info;
  1113. struct digest_algorithm *digest = info->digest;
  1114. struct peerdist_block *peerblk;
  1115. unsigned long timeout;
  1116. size_t digestsize;
  1117. int rc;
  1118. /* Allocate and initialise structure */
  1119. peerblk = zalloc ( sizeof ( *peerblk ) + digest->ctxsize );
  1120. if ( ! peerblk ) {
  1121. rc = -ENOMEM;
  1122. goto err_alloc;
  1123. }
  1124. ref_init ( &peerblk->refcnt, peerblk_free );
  1125. intf_init ( &peerblk->xfer, &peerblk_xfer_desc, &peerblk->refcnt );
  1126. intf_init ( &peerblk->raw, &peerblk_raw_desc, &peerblk->refcnt );
  1127. intf_init ( &peerblk->retrieval, &peerblk_retrieval_desc,
  1128. &peerblk->refcnt );
  1129. peerblk->uri = uri_get ( uri );
  1130. memcpy ( &peerblk->range, &block->range, sizeof ( peerblk->range ) );
  1131. memcpy ( &peerblk->trim, &block->trim, sizeof ( peerblk->trim ) );
  1132. peerblk->offset = ( block->trim.start - info->trim.start );
  1133. peerblk->digest = info->digest;
  1134. peerblk->digestsize = digestsize = info->digestsize;
  1135. peerblk->digestctx = ( ( ( void * ) peerblk ) + sizeof ( *peerblk ) );
  1136. peerblk->segment = segment->index;
  1137. memcpy ( peerblk->id, segment->id, sizeof ( peerblk->id ) );
  1138. memcpy ( peerblk->secret, segment->secret, sizeof ( peerblk->secret ) );
  1139. peerblk->block = block->index;
  1140. memcpy ( peerblk->hash, block->hash, sizeof ( peerblk->hash ) );
  1141. xferbuf_malloc_init ( &peerblk->buffer );
  1142. process_init_stopped ( &peerblk->process, &peerblk_process_desc,
  1143. &peerblk->refcnt );
  1144. peerdisc_init ( &peerblk->discovery, &peerblk_discovery_operations );
  1145. timer_init ( &peerblk->timer, peerblk_expired, &peerblk->refcnt );
  1146. DBGC2 ( peerblk, "PEERBLK %p %d.%d id %02x%02x%02x%02x%02x..."
  1147. "%02x%02x%02x [%08zx,%08zx)", peerblk, peerblk->segment,
  1148. peerblk->block, peerblk->id[0], peerblk->id[1], peerblk->id[2],
  1149. peerblk->id[3], peerblk->id[4], peerblk->id[ digestsize - 3 ],
  1150. peerblk->id[ digestsize - 2 ], peerblk->id[ digestsize - 1 ],
  1151. peerblk->range.start, peerblk->range.end );
  1152. if ( ( peerblk->trim.start != peerblk->range.start ) ||
  1153. ( peerblk->trim.end != peerblk->range.end ) ) {
  1154. DBGC2 ( peerblk, " covers [%08zx,%08zx)",
  1155. peerblk->trim.start, peerblk->trim.end );
  1156. }
  1157. DBGC2 ( peerblk, "\n" );
  1158. /* Open discovery */
  1159. if ( ( rc = peerdisc_open ( &peerblk->discovery, peerblk->id,
  1160. peerblk->digestsize ) ) != 0 )
  1161. goto err_open_discovery;
  1162. /* Schedule a retry attempt either immediately (if we already
  1163. * have some peers) or after the discovery timeout.
  1164. */
  1165. timeout = ( list_empty ( &peerblk->discovery.segment->peers ) ?
  1166. ( peerdisc_timeout_secs * TICKS_PER_SEC ) : 0 );
  1167. start_timer_fixed ( &peerblk->timer, timeout );
  1168. /* Record start time */
  1169. peerblk->started = peerblk_timestamp();
  1170. /* Attach to parent interface, mortalise self, and return */
  1171. intf_plug_plug ( xfer, &peerblk->xfer );
  1172. ref_put ( &peerblk->refcnt );
  1173. return 0;
  1174. err_open_discovery:
  1175. peerblk_close ( peerblk, rc );
  1176. err_alloc:
  1177. return rc;
  1178. }