2 * Copyright (C) 2015 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
31 #include <ipxe/xfer.h>
32 #include <ipxe/iobuf.h>
33 #include <ipxe/open.h>
34 #include <ipxe/tcpip.h>
35 #include <ipxe/uuid.h>
36 #include <ipxe/base16.h>
37 #include <ipxe/netdevice.h>
38 #include <ipxe/timer.h>
39 #include <ipxe/fault.h>
40 #include <ipxe/pccrd.h>
41 #include <ipxe/peerdisc.h>
45 * Peer Content Caching and Retrieval (PeerDist) protocol peer discovery
49 /** List of discovery segments */
50 static LIST_HEAD ( peerdisc_segments );
52 /** Number of repeated discovery attempts */
53 #define PEERDISC_REPEAT_COUNT 2
55 /** Time between repeated discovery attempts */
56 #define PEERDISC_REPEAT_TIMEOUT ( 1 * TICKS_PER_SEC )
58 /** Default discovery timeout (in seconds) */
59 #define PEERDISC_DEFAULT_TIMEOUT_SECS 2
61 /** Recommended discovery timeout (in seconds)
63 * We reduce the recommended discovery timeout whenever a segment
64 * fails to discover any peers, and restore the default value whenever
65 * a valid discovery reply is received. We continue to send discovery
66 * requests even if the recommended timeout is reduced to zero.
68 * This strategy is intended to minimise discovery delays when no
69 * peers are available on the network, while allowing downloads to
70 * quickly switch back to using PeerDist acceleration if new peers
73 unsigned int peerdisc_timeout_secs = PEERDISC_DEFAULT_TIMEOUT_SECS;
75 static struct peerdisc_segment * peerdisc_find ( const char *id );
76 static int peerdisc_discovered ( struct peerdisc_segment *segment,
77 const char *location );
79 /******************************************************************************
83 ******************************************************************************
87 * Open all PeerDist discovery sockets
89 * @ret rc Return status code
91 static int peerdisc_socket_open ( void ) {
92 struct peerdisc_socket *socket;
95 /* Open each socket */
96 for_each_table_entry ( socket, PEERDISC_SOCKETS ) {
97 if ( ( rc = xfer_open_socket ( &socket->xfer, SOCK_DGRAM,
100 DBGC ( socket, "PEERDISC %s could not open socket: "
101 "%s\n", socket->name, strerror ( rc ) );
109 for_each_table_entry_continue_reverse ( socket, PEERDISC_SOCKETS )
110 intf_restart ( &socket->xfer, rc );
115 * Attempt to transmit PeerDist discovery requests on all sockets
117 * @v uuid Message UUID string
118 * @v id Segment identifier string
120 static void peerdisc_socket_tx ( const char *uuid, const char *id ) {
121 struct peerdisc_socket *socket;
122 struct net_device *netdev;
123 struct xfer_metadata meta;
126 struct sockaddr_tcpip st;
132 /* Construct discovery request */
133 request = peerdist_discovery_request ( uuid, id );
136 len = strlen ( request );
138 /* Initialise data transfer metadata */
139 memset ( &meta, 0, sizeof ( meta ) );
140 meta.dest = &address.sa;
142 /* Send message on each socket */
143 for_each_table_entry ( socket, PEERDISC_SOCKETS ) {
145 /* Initialise socket address */
146 memcpy ( &address.sa, &socket->address.sa,
147 sizeof ( address.sa ) );
149 /* Send message on each open network device */
150 for_each_netdev ( netdev ) {
152 /* Skip unopened network devices */
153 if ( ! netdev_is_open ( netdev ) )
155 address.st.st_scope_id = netdev->index;
157 /* Discard request (for test purposes) if applicable */
158 if ( inject_fault ( PEERDISC_DISCARD_RATE ) )
161 /* Transmit request */
162 if ( ( rc = xfer_deliver_raw_meta ( &socket->xfer,
165 DBGC ( socket, "PEERDISC %s could not transmit "
166 "via %s: %s\n", socket->name,
167 netdev->name, strerror ( rc ) );
168 /* Contine to try other net devices/sockets */
180 * Handle received PeerDist discovery reply
182 * @v socket PeerDist discovery socket
183 * @v iobuf I/O buffer
184 * @v meta Data transfer metadata
185 * @ret rc Return status code
187 static int peerdisc_socket_rx ( struct peerdisc_socket *socket,
188 struct io_buffer *iobuf,
189 struct xfer_metadata *meta __unused ) {
190 struct peerdist_discovery_reply reply;
191 struct peerdisc_segment *segment;
196 /* Discard reply (for test purposes) if applicable */
197 if ( ( rc = inject_fault ( PEERDISC_DISCARD_RATE ) ) != 0 )
201 if ( ( rc = peerdist_discovery_reply ( iobuf->data, iob_len ( iobuf ),
203 DBGC ( socket, "PEERDISC %s could not parse reply: %s\n",
204 socket->name, strerror ( rc ) );
205 DBGC_HDA ( socket, 0, iobuf->data, iob_len ( iobuf ) );
209 /* Any kind of discovery reply indicates that there are active
210 * peers on a local network, so restore the recommended
211 * discovery timeout to its default value for future requests.
213 if ( peerdisc_timeout_secs != PEERDISC_DEFAULT_TIMEOUT_SECS ) {
214 DBGC ( socket, "PEERDISC %s restoring timeout to %d seconds\n",
215 socket->name, PEERDISC_DEFAULT_TIMEOUT_SECS );
217 peerdisc_timeout_secs = PEERDISC_DEFAULT_TIMEOUT_SECS;
219 /* Iterate over segment IDs */
220 for ( id = reply.ids ; *id ; id += ( strlen ( id ) + 1 /* NUL */ ) ) {
222 /* Find corresponding segment */
223 segment = peerdisc_find ( id );
225 DBGC ( socket, "PEERDISC %s ignoring reply for %s\n",
230 /* Report all discovered peer locations */
231 for ( location = reply.locations ; *location ;
232 location += ( strlen ( location ) + 1 /* NUL */ ) ) {
234 /* Report discovered peer location */
235 if ( ( rc = peerdisc_discovered ( segment,
247 * Close all PeerDist discovery sockets
249 * @v rc Reason for close
251 static void peerdisc_socket_close ( int rc ) {
252 struct peerdisc_socket *socket;
254 /* Close all sockets */
255 for_each_table_entry ( socket, PEERDISC_SOCKETS )
256 intf_restart ( &socket->xfer, rc );
259 /** PeerDist discovery socket interface operations */
260 static struct interface_operation peerdisc_socket_operations[] = {
261 INTF_OP ( xfer_deliver, struct peerdisc_socket *, peerdisc_socket_rx ),
264 /** PeerDist discovery socket interface descriptor */
265 static struct interface_descriptor peerdisc_socket_desc =
266 INTF_DESC ( struct peerdisc_socket, xfer, peerdisc_socket_operations );
268 /** PeerDist discovery IPv4 socket */
269 struct peerdisc_socket peerdisc_socket_ipv4 __peerdisc_socket = {
273 .sin_family = AF_INET,
274 .sin_port = htons ( PEERDIST_DISCOVERY_PORT ),
275 .sin_addr.s_addr = htonl ( PEERDIST_DISCOVERY_IPV4 ),
278 .xfer = INTF_INIT ( peerdisc_socket_desc ),
281 /** PeerDist discovery IPv6 socket */
282 struct peerdisc_socket peerdisc_socket_ipv6 __peerdisc_socket = {
286 .sin6_family = AF_INET6,
287 .sin6_port = htons ( PEERDIST_DISCOVERY_PORT ),
288 .sin6_addr.s6_addr = PEERDIST_DISCOVERY_IPV6,
291 .xfer = INTF_INIT ( peerdisc_socket_desc ),
294 /******************************************************************************
298 ******************************************************************************
302 * Free PeerDist discovery segment
304 * @v refcnt Reference count
306 static void peerdisc_free ( struct refcnt *refcnt ) {
307 struct peerdisc_segment *segment =
308 container_of ( refcnt, struct peerdisc_segment, refcnt );
309 struct peerdisc_peer *peer;
310 struct peerdisc_peer *tmp;
312 /* Free all discovered peers */
313 list_for_each_entry_safe ( peer, tmp, &segment->peers, list ) {
314 list_del ( &peer->list );
323 * Find PeerDist discovery segment
326 * @ret segment PeerDist discovery segment, or NULL if not found
328 static struct peerdisc_segment * peerdisc_find ( const char *id ) {
329 struct peerdisc_segment *segment;
331 /* Look for a matching segment */
332 list_for_each_entry ( segment, &peerdisc_segments, list ) {
333 if ( strcmp ( id, segment->id ) == 0 )
341 * Add discovered PeerDist peer
343 * @v segment PeerDist discovery segment
344 * @v location Peer location
345 * @ret rc Return status code
347 static int peerdisc_discovered ( struct peerdisc_segment *segment,
348 const char *location ) {
349 struct peerdisc_peer *peer;
350 struct peerdisc_client *peerdisc;
351 struct peerdisc_client *tmp;
353 /* Ignore duplicate peers */
354 list_for_each_entry ( peer, &segment->peers, list ) {
355 if ( strcmp ( peer->location, location ) == 0 ) {
356 DBGC2 ( segment, "PEERDISC %p duplicate %s\n",
361 DBGC2 ( segment, "PEERDISC %p discovered %s\n", segment, location );
363 /* Allocate and initialise structure */
364 peer = zalloc ( sizeof ( *peer ) + strlen ( location ) + 1 /* NUL */ );
367 strcpy ( peer->location, location );
369 /* Add to end of list of peers */
370 list_add_tail ( &peer->list, &segment->peers );
372 /* Notify all clients */
373 list_for_each_entry_safe ( peerdisc, tmp, &segment->clients, list )
374 peerdisc->op->discovered ( peerdisc );
380 * Handle discovery timer expiry
382 * @v timer Discovery timer
383 * @v over Failure indicator
385 static void peerdisc_expired ( struct retry_timer *timer, int over __unused ) {
386 struct peerdisc_segment *segment =
387 container_of ( timer, struct peerdisc_segment, timer );
389 /* Attempt to transmit discovery requests */
390 peerdisc_socket_tx ( segment->uuid, segment->id );
392 /* Schedule next transmission, if applicable */
393 if ( timer->count < PEERDISC_REPEAT_COUNT )
394 start_timer_fixed ( &segment->timer, PEERDISC_REPEAT_TIMEOUT );
398 * Create PeerDist discovery segment
401 * @ret segment PeerDist discovery segment, or NULL on error
403 static struct peerdisc_segment * peerdisc_create ( const char *id ) {
404 struct peerdisc_segment *segment;
407 uint32_t dword[ sizeof ( union uuid ) / sizeof ( uint32_t ) ];
416 /* Generate a random message UUID. This does not require high
417 * quality randomness.
419 for ( i = 0 ; i < ( sizeof ( random_uuid.dword ) /
420 sizeof ( random_uuid.dword[0] ) ) ; i++ )
421 random_uuid.dword[i] = random();
422 uuid = uuid_ntoa ( &random_uuid.uuid );
424 /* Calculate string lengths */
425 id_len = ( strlen ( id ) + 1 /* NUL */ );
426 uuid_len = ( strlen ( uuid ) + 1 /* NUL */ );
428 /* Allocate and initialise structure */
429 segment = zalloc ( sizeof ( *segment ) + id_len + uuid_len );
432 id_copy = ( ( ( void * ) segment ) + sizeof ( *segment ) );
433 memcpy ( id_copy, id, id_len );
434 uuid_copy = ( ( ( void * ) id_copy ) + id_len );
435 memcpy ( uuid_copy, uuid, uuid_len );
436 ref_init ( &segment->refcnt, peerdisc_free );
437 segment->id = id_copy;
438 segment->uuid = uuid_copy;
439 INIT_LIST_HEAD ( &segment->peers );
440 INIT_LIST_HEAD ( &segment->clients );
441 timer_init ( &segment->timer, peerdisc_expired, &segment->refcnt );
442 DBGC2 ( segment, "PEERDISC %p discovering %s\n", segment, segment->id );
444 /* Start discovery timer */
445 start_timer_nodelay ( &segment->timer );
447 /* Add to list of segments, transfer reference to list, and return */
448 list_add_tail ( &segment->list, &peerdisc_segments );
453 * Destroy PeerDist discovery segment
455 * @v segment PeerDist discovery segment
457 static void peerdisc_destroy ( struct peerdisc_segment *segment ) {
460 assert ( list_empty ( &segment->clients ) );
463 stop_timer ( &segment->timer );
465 /* Remove from list of segments and drop list's reference */
466 list_del ( &segment->list );
467 ref_put ( &segment->refcnt );
470 /******************************************************************************
474 ******************************************************************************
478 * Open PeerDist discovery client
480 * @v peerdisc PeerDist discovery client
482 * @v len Length of segment ID
483 * @ret rc Return status code
485 int peerdisc_open ( struct peerdisc_client *peerdisc, const void *id,
487 struct peerdisc_segment *segment;
488 char id_string[ base16_encoded_len ( len ) + 1 /* NUL */ ];
492 /* Construct ID string */
493 base16_encode ( id, len, id_string, sizeof ( id_string ) );
494 for ( id_chr = id_string ; *id_chr ; id_chr++ )
495 *id_chr = toupper ( *id_chr );
498 assert ( peerdisc->segment == NULL );
500 /* Open socket if this is the first segment */
501 if ( list_empty ( &peerdisc_segments ) &&
502 ( ( rc = peerdisc_socket_open() ) != 0 ) )
505 /* Find or create segment */
506 if ( ! ( ( segment = peerdisc_find ( id_string ) ) ||
507 ( segment = peerdisc_create ( id_string ) ) ) )
510 /* Add to list of clients */
511 ref_get ( &segment->refcnt );
512 peerdisc->segment = segment;
513 list_add_tail ( &peerdisc->list, &segment->clients );
519 * Close PeerDist discovery client
521 * @v peerdisc PeerDist discovery client
523 void peerdisc_close ( struct peerdisc_client *peerdisc ) {
524 struct peerdisc_segment *segment = peerdisc->segment;
526 /* Ignore if discovery is already closed */
530 /* If no peers were discovered, reduce the recommended
531 * discovery timeout to minimise delays on future requests.
533 if ( list_empty ( &segment->peers ) && peerdisc_timeout_secs ) {
534 peerdisc_timeout_secs--;
535 DBGC ( segment, "PEERDISC %p reducing timeout to %d "
536 "seconds\n", peerdisc, peerdisc_timeout_secs );
539 /* Remove from list of clients */
540 peerdisc->segment = NULL;
541 list_del ( &peerdisc->list );
542 ref_put ( &segment->refcnt );
544 /* If this was the last clients, destroy the segment */
545 if ( list_empty ( &segment->clients ) )
546 peerdisc_destroy ( segment );
548 /* If there are no more segments, close the socket */
549 if ( list_empty ( &peerdisc_segments ) )
550 peerdisc_socket_close ( 0 );