Data Structures | Defines | Functions | Variables
peerdisc.h File Reference

Peer Content Caching and Retrieval (PeerDist) protocol peer discovery. More...

#include <stdint.h>
#include <ipxe/refcnt.h>
#include <ipxe/list.h>
#include <ipxe/tables.h>
#include <ipxe/retry.h>
#include <ipxe/socket.h>
#include <ipxe/interface.h>
#include <ipxe/pccrc.h>

Go to the source code of this file.

Data Structures

struct  peerdisc_socket
 A PeerDist discovery socket. More...
struct  peerdisc_segment
 A PeerDist discovery segment. More...
struct  peerdisc_peer
 A PeerDist discovery peer. More...
struct  peerdisc_client
 A PeerDist discovery client. More...
struct  peerdisc_client_operations
 PeerDist discovery client operations. More...


#define PEERDISC_SOCKETS   __table ( struct peerdisc_socket, "peerdisc_sockets" )
 PeerDist discovery socket table.
#define __peerdisc_socket   __table_entry ( PEERDISC_SOCKETS, 01 )
 Declare a PeerDist discovery socket.
#define peerdisc_stat_TYPE(object_type)


static void peerdisc_init (struct peerdisc_client *peerdisc, struct peerdisc_client_operations *op)
 Initialise PeerDist discovery.
void peerdisc_stat (struct interface *intf, struct peerdisc_peer *peer, struct list_head *peers)
 Report peer discovery statistics.
int peerdisc_open (struct peerdisc_client *peerdisc, const void *id, size_t len)
 Open PeerDist discovery client.
void peerdisc_close (struct peerdisc_client *peerdisc)
 Close PeerDist discovery client.


unsigned int peerdisc_timeout_secs
 Recommended discovery timeout (in seconds)

Detailed Description

Peer Content Caching and Retrieval (PeerDist) protocol peer discovery.

Definition in file peerdisc.h.

Define Documentation

#define PEERDISC_SOCKETS   __table ( struct peerdisc_socket, "peerdisc_sockets" )

PeerDist discovery socket table.

Definition at line 36 of file peerdisc.h.

Referenced by peerdisc_socket_close(), peerdisc_socket_open(), and peerdisc_socket_tx().

struct peerdisc_socket peerdisc_socket_ipv6 __peerdisc_socket   __table_entry ( PEERDISC_SOCKETS, 01 )

Declare a PeerDist discovery socket.

PeerDist discovery IPv6 socket.

Definition at line 39 of file peerdisc.h.

#define peerdisc_stat_TYPE (   object_type)
typeof ( void ( object_type, struct peerdisc_peer *peer,        \
                        struct list_head *peers ) )

Definition at line 114 of file peerdisc.h.

Referenced by peerdisc_stat().

Function Documentation

static void peerdisc_init ( struct peerdisc_client peerdisc,
struct peerdisc_client_operations op 
) [inline, static]

Initialise PeerDist discovery.

peerdiscPeerDist discovery client
opDiscovery operations

Definition at line 104 of file peerdisc.h.

References op.

Referenced by peerblk_open().


        peerdisc->op = op;
void peerdisc_stat ( struct interface intf,
struct peerdisc_peer peer,
struct list_head peers 

Report peer discovery statistics.

peerSelected peer (or NULL)
peersList of available peers

Definition at line 93 of file peerdisc.c.

References dest, intf_get_dest_op, intf_object(), intf_put(), op, peerdisc_stat(), and peerdisc_stat_TYPE.

Referenced by peerblk_done(), and peerdisc_stat().

        struct interface *dest;
        peerdisc_stat_TYPE ( void * ) *op =
                intf_get_dest_op ( intf, peerdisc_stat, &dest );
        void *object = intf_object ( dest );

        if ( op ) {
                op ( object, peer, peers );
        } else {
                /* Default is to do nothing */

        intf_put ( dest );
int peerdisc_open ( struct peerdisc_client peerdisc,
const void *  id,
size_t  len 

Open PeerDist discovery client.

peerdiscPeerDist discovery client
idSegment ID
lenLength of segment ID
Return values:
rcReturn status code

Definition at line 515 of file peerdisc.c.

References assert, base16_encoded_len(), peerdisc_segment::clients, ENOMEM, id_string, peerdisc_client::list, list_add_tail, list_empty, NULL, peerdisc_create(), peerdisc_find(), peerdisc_socket_open(), rc, ref_get, peerdisc_segment::refcnt, peerdisc_client::segment, segment, and toupper().

Referenced by peerblk_open().

        struct peerdisc_segment *segment;
        char id_string[ base16_encoded_len ( len ) + 1 /* NUL */ ];
        char *id_chr;
        int rc;

        /* Construct ID string */
        base16_encode ( id, len, id_string, sizeof ( id_string ) );
        for ( id_chr = id_string ; *id_chr ; id_chr++ )
                *id_chr = toupper ( *id_chr );

        /* Sanity check */
        assert ( peerdisc->segment == NULL );

        /* Open socket if this is the first segment */
        if ( list_empty ( &peerdisc_segments ) &&
             ( ( rc = peerdisc_socket_open() ) != 0 ) )
                return rc;

        /* Find or create segment */
        if ( ! ( ( segment = peerdisc_find ( id_string ) ) ||
                 ( segment = peerdisc_create ( id_string ) ) ) )
                return -ENOMEM;

        /* Add to list of clients */
        ref_get ( &segment->refcnt );
        peerdisc->segment = segment;
        list_add_tail ( &peerdisc->list, &segment->clients );

        return 0;
void peerdisc_close ( struct peerdisc_client peerdisc)

Close PeerDist discovery client.

peerdiscPeerDist discovery client

Definition at line 553 of file peerdisc.c.

References peerdisc_segment::clients, DBGC, peerdisc_client::list, list_del, list_empty, NULL, peerdisc_destroy(), peerdisc_socket_close(), peerdisc_timeout_secs, peerdisc_segment::peers, ref_put, peerdisc_segment::refcnt, peerdisc_client::segment, and segment.

Referenced by peerblk_close().

        struct peerdisc_segment *segment = peerdisc->segment;

        /* Ignore if discovery is already closed */
        if ( ! segment )

        /* If no peers were discovered, reduce the recommended
         * discovery timeout to minimise delays on future requests.
        if ( list_empty ( &segment->peers ) && peerdisc_timeout_secs ) {
                DBGC ( segment, "PEERDISC %p reducing timeout to %d "
                       "seconds\n", peerdisc, peerdisc_timeout_secs );

        /* Remove from list of clients */
        peerdisc->segment = NULL;
        list_del ( &peerdisc->list );
        ref_put ( &segment->refcnt );

        /* If this was the last clients, destroy the segment */
        if ( list_empty ( &segment->clients ) )
                peerdisc_destroy ( segment );

        /* If there are no more segments, close the socket */
        if ( list_empty ( &peerdisc_segments ) )
                peerdisc_socket_close ( 0 );

Variable Documentation

unsigned int peerdisc_timeout_secs

Recommended discovery timeout (in seconds)

We reduce the recommended discovery timeout whenever a segment fails to discover any peers, and restore the default value whenever a valid discovery reply is received. We continue to send discovery requests even if the recommended timeout is reduced to zero.

This strategy is intended to minimise discovery delays when no peers are available on the network, while allowing downloads to quickly switch back to using PeerDist acceleration if new peers become available.

Definition at line 73 of file peerdisc.c.

Referenced by peerblk_open(), peerdisc_close(), and peerdisc_socket_rx().