Browse Source

[peerdist] Allow for the use of a hosted cache server

Allow a PeerDist hosted cache server to be specified via the
${peerhost} setting, e.g.:

  # Use 192.168.0.1 as hosted cache server
  set peerhost 192.168.0.1

Note that this simply treats the hosted cache server as a permanently
discovered peer for all segments.

Signed-off-by: Michael Brown <mcb30@ipxe.org>
tags/v1.20.1
Michael Brown 5 years ago
parent
commit
a2d3bedf1f
1 changed files with 65 additions and 4 deletions
  1. 65
    4
      src/net/peerdisc.c

+ 65
- 4
src/net/peerdisc.c View File

37
 #include <ipxe/netdevice.h>
37
 #include <ipxe/netdevice.h>
38
 #include <ipxe/timer.h>
38
 #include <ipxe/timer.h>
39
 #include <ipxe/fault.h>
39
 #include <ipxe/fault.h>
40
+#include <ipxe/settings.h>
40
 #include <ipxe/pccrd.h>
41
 #include <ipxe/pccrd.h>
41
 #include <ipxe/peerdisc.h>
42
 #include <ipxe/peerdisc.h>
42
 
43
 
72
  */
73
  */
73
 unsigned int peerdisc_timeout_secs = PEERDISC_DEFAULT_TIMEOUT_SECS;
74
 unsigned int peerdisc_timeout_secs = PEERDISC_DEFAULT_TIMEOUT_SECS;
74
 
75
 
76
+/** Hosted cache server */
77
+static char *peerhost;
78
+
75
 static struct peerdisc_segment * peerdisc_find ( const char *id );
79
 static struct peerdisc_segment * peerdisc_find ( const char *id );
76
 static int peerdisc_discovered ( struct peerdisc_segment *segment,
80
 static int peerdisc_discovered ( struct peerdisc_segment *segment,
77
 				 const char *location );
81
 				 const char *location );
442
 	char *uuid_copy;
446
 	char *uuid_copy;
443
 	char *id_copy;
447
 	char *id_copy;
444
 	unsigned int i;
448
 	unsigned int i;
449
+	int rc;
445
 
450
 
446
 	/* Generate a random message UUID.  This does not require high
451
 	/* Generate a random message UUID.  This does not require high
447
 	 * quality randomness.
452
 	 * quality randomness.
458
 	/* Allocate and initialise structure */
463
 	/* Allocate and initialise structure */
459
 	segment = zalloc ( sizeof ( *segment ) + id_len + uuid_len );
464
 	segment = zalloc ( sizeof ( *segment ) + id_len + uuid_len );
460
 	if ( ! segment )
465
 	if ( ! segment )
461
-		return NULL;
466
+		goto err_alloc;
462
 	id_copy = ( ( ( void * ) segment ) + sizeof ( *segment ) );
467
 	id_copy = ( ( ( void * ) segment ) + sizeof ( *segment ) );
463
 	memcpy ( id_copy, id, id_len );
468
 	memcpy ( id_copy, id, id_len );
464
 	uuid_copy = ( ( ( void * ) id_copy ) + id_len );
469
 	uuid_copy = ( ( ( void * ) id_copy ) + id_len );
469
 	INIT_LIST_HEAD ( &segment->peers );
474
 	INIT_LIST_HEAD ( &segment->peers );
470
 	INIT_LIST_HEAD ( &segment->clients );
475
 	INIT_LIST_HEAD ( &segment->clients );
471
 	timer_init ( &segment->timer, peerdisc_expired, &segment->refcnt );
476
 	timer_init ( &segment->timer, peerdisc_expired, &segment->refcnt );
472
-	DBGC2 ( segment, "PEERDISC %p discovering %s\n", segment, segment->id );
473
 
477
 
474
-	/* Start discovery timer */
475
-	start_timer_nodelay ( &segment->timer );
478
+	/* Add hosted cache server or initiate discovery */
479
+	if ( peerhost ) {
480
+
481
+		/* Add hosted cache server to list of peers */
482
+		if ( ( rc = peerdisc_discovered ( segment, peerhost ) ) != 0 )
483
+			goto err_peerhost;
484
+
485
+	} else {
486
+
487
+		/* Start discovery timer */
488
+		start_timer_nodelay ( &segment->timer );
489
+		DBGC2 ( segment, "PEERDISC %p discovering %s\n",
490
+			segment, segment->id );
491
+	}
476
 
492
 
477
 	/* Add to list of segments, transfer reference to list, and return */
493
 	/* Add to list of segments, transfer reference to list, and return */
478
 	list_add_tail ( &segment->list, &peerdisc_segments );
494
 	list_add_tail ( &segment->list, &peerdisc_segments );
479
 	return segment;
495
 	return segment;
496
+
497
+ err_peerhost:
498
+	ref_put ( &segment->refcnt );
499
+ err_alloc:
500
+	return NULL;
480
 }
501
 }
481
 
502
 
482
 /**
503
 /**
579
 	if ( list_empty ( &peerdisc_segments ) )
600
 	if ( list_empty ( &peerdisc_segments ) )
580
 		peerdisc_socket_close ( 0 );
601
 		peerdisc_socket_close ( 0 );
581
 }
602
 }
603
+
604
+/******************************************************************************
605
+ *
606
+ * Settings
607
+ *
608
+ ******************************************************************************
609
+ */
610
+
611
+/** PeerDist hosted cache server setting */
612
+const struct setting peerhost_setting __setting ( SETTING_MISC, peerhost ) = {
613
+	.name = "peerhost",
614
+	.description = "PeerDist hosted cache",
615
+	.type = &setting_type_string,
616
+};
617
+
618
+/**
619
+ * Apply PeerDist discovery settings
620
+ *
621
+ * @ret rc		Return status code
622
+ */
623
+static int apply_peerdisc_settings ( void ) {
624
+
625
+	/* Free any existing hosted cache server */
626
+	free ( peerhost );
627
+	peerhost = NULL;
628
+
629
+	/* Fetch hosted cache server */
630
+	fetch_string_setting_copy ( NULL, &peerhost_setting, &peerhost );
631
+	if ( peerhost ) {
632
+		DBGC ( &peerhost, "PEERDISC using hosted cache %s\n",
633
+		       peerhost );
634
+	}
635
+
636
+	return 0;
637
+}
638
+
639
+/** PeerDist discovery settings applicator */
640
+struct settings_applicator peerdisc_applicator __settings_applicator = {
641
+	.apply = apply_peerdisc_settings,
642
+};

Loading…
Cancel
Save