]> git.ipfire.org Git - thirdparty/ipxe.git/commitdiff
[peerdist] Allow for the use of a hosted cache server
authorMichael Brown <mcb30@ipxe.org>
Sun, 15 Dec 2019 23:26:02 +0000 (23:26 +0000)
committerMichael Brown <mcb30@ipxe.org>
Sun, 15 Dec 2019 23:29:44 +0000 (23:29 +0000)
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>
src/net/peerdisc.c

index 20ac2427b1d2ff9c43c39fbfe874984d78edffa4..55e3f7fa7bf102bd5fb2a6d1ed9886d4f5d3a3bf 100644 (file)
@@ -37,6 +37,7 @@ FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
 #include <ipxe/netdevice.h>
 #include <ipxe/timer.h>
 #include <ipxe/fault.h>
+#include <ipxe/settings.h>
 #include <ipxe/pccrd.h>
 #include <ipxe/peerdisc.h>
 
@@ -72,6 +73,9 @@ static LIST_HEAD ( peerdisc_segments );
  */
 unsigned int peerdisc_timeout_secs = PEERDISC_DEFAULT_TIMEOUT_SECS;
 
+/** Hosted cache server */
+static char *peerhost;
+
 static struct peerdisc_segment * peerdisc_find ( const char *id );
 static int peerdisc_discovered ( struct peerdisc_segment *segment,
                                 const char *location );
@@ -442,6 +446,7 @@ static struct peerdisc_segment * peerdisc_create ( const char *id ) {
        char *uuid_copy;
        char *id_copy;
        unsigned int i;
+       int rc;
 
        /* Generate a random message UUID.  This does not require high
         * quality randomness.
@@ -458,7 +463,7 @@ static struct peerdisc_segment * peerdisc_create ( const char *id ) {
        /* Allocate and initialise structure */
        segment = zalloc ( sizeof ( *segment ) + id_len + uuid_len );
        if ( ! segment )
-               return NULL;
+               goto err_alloc;
        id_copy = ( ( ( void * ) segment ) + sizeof ( *segment ) );
        memcpy ( id_copy, id, id_len );
        uuid_copy = ( ( ( void * ) id_copy ) + id_len );
@@ -469,14 +474,30 @@ static struct peerdisc_segment * peerdisc_create ( const char *id ) {
        INIT_LIST_HEAD ( &segment->peers );
        INIT_LIST_HEAD ( &segment->clients );
        timer_init ( &segment->timer, peerdisc_expired, &segment->refcnt );
-       DBGC2 ( segment, "PEERDISC %p discovering %s\n", segment, segment->id );
 
-       /* Start discovery timer */
-       start_timer_nodelay ( &segment->timer );
+       /* Add hosted cache server or initiate discovery */
+       if ( peerhost ) {
+
+               /* Add hosted cache server to list of peers */
+               if ( ( rc = peerdisc_discovered ( segment, peerhost ) ) != 0 )
+                       goto err_peerhost;
+
+       } else {
+
+               /* Start discovery timer */
+               start_timer_nodelay ( &segment->timer );
+               DBGC2 ( segment, "PEERDISC %p discovering %s\n",
+                       segment, segment->id );
+       }
 
        /* Add to list of segments, transfer reference to list, and return */
        list_add_tail ( &segment->list, &peerdisc_segments );
        return segment;
+
+ err_peerhost:
+       ref_put ( &segment->refcnt );
+ err_alloc:
+       return NULL;
 }
 
 /**
@@ -579,3 +600,43 @@ void peerdisc_close ( struct peerdisc_client *peerdisc ) {
        if ( list_empty ( &peerdisc_segments ) )
                peerdisc_socket_close ( 0 );
 }
+
+/******************************************************************************
+ *
+ * Settings
+ *
+ ******************************************************************************
+ */
+
+/** PeerDist hosted cache server setting */
+const struct setting peerhost_setting __setting ( SETTING_MISC, peerhost ) = {
+       .name = "peerhost",
+       .description = "PeerDist hosted cache",
+       .type = &setting_type_string,
+};
+
+/**
+ * Apply PeerDist discovery settings
+ *
+ * @ret rc             Return status code
+ */
+static int apply_peerdisc_settings ( void ) {
+
+       /* Free any existing hosted cache server */
+       free ( peerhost );
+       peerhost = NULL;
+
+       /* Fetch hosted cache server */
+       fetch_string_setting_copy ( NULL, &peerhost_setting, &peerhost );
+       if ( peerhost ) {
+               DBGC ( &peerhost, "PEERDISC using hosted cache %s\n",
+                      peerhost );
+       }
+
+       return 0;
+}
+
+/** PeerDist discovery settings applicator */
+struct settings_applicator peerdisc_applicator __settings_applicator = {
+       .apply = apply_peerdisc_settings,
+};