iPXE
Data Structures | Macros | 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...
 

Macros

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

Functions

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

Variables

unsigned int peerdisc_timeout_secs
 Recommended discovery timeout (in seconds) More...
 

Detailed Description

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

Definition in file peerdisc.h.

Macro Definition Documentation

◆ PEERDISC_SOCKETS

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

PeerDist discovery socket table.

Definition at line 36 of file peerdisc.h.

◆ __peerdisc_socket

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.

◆ peerdisc_stat_TYPE

#define peerdisc_stat_TYPE (   object_type)
Value:
typeof ( void ( object_type, struct peerdisc_peer *peer, \
struct list_head *peers ) )
A doubly-linked list entry (or list head)
Definition: list.h:18
struct peerdisc_peer * peer
Current position in discovered peer list.
Definition: peerblk.h:113
A PeerDist discovery peer.
Definition: peerdisc.h:71

Definition at line 114 of file peerdisc.h.

Function Documentation

◆ FILE_LICENCE()

FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL  )

◆ peerdisc_init()

static void peerdisc_init ( struct peerdisc_client peerdisc,
struct peerdisc_client_operations op 
)
inlinestatic

Initialise PeerDist discovery.

Parameters
peerdiscPeerDist discovery client
opDiscovery operations

Definition at line 104 of file peerdisc.h.

105  {
106 
107  peerdisc->op = op;
108 }
static uint16_t struct vmbus_xfer_pages_operations * op
Definition: netvsc.h:327
struct peerdisc_client_operations * op
Operations.
Definition: peerdisc.h:85

References peerdisc_client::op, and op.

Referenced by peerblk_open().

◆ peerdisc_stat()

void peerdisc_stat ( struct interface intf,
struct peerdisc_peer peer,
struct list_head peers 
)

Report peer discovery statistics.

Parameters
intfInterface
peerSelected peer (or NULL)
peersList of available peers

Definition at line 97 of file peerdisc.c.

98  {
99  struct interface *dest;
100  peerdisc_stat_TYPE ( void * ) *op =
101  intf_get_dest_op ( intf, peerdisc_stat, &dest );
102  void *object = intf_object ( dest );
103 
104  if ( op ) {
105  op ( object, peer, peers );
106  } else {
107  /* Default is to do nothing */
108  }
109 
110  intf_put ( dest );
111 }
void * intf_object(struct interface *intf)
Get pointer to object containing object interface.
Definition: interface.c:154
void peerdisc_stat(struct interface *intf, struct peerdisc_peer *peer, struct list_head *peers)
Report peer discovery statistics.
Definition: peerdisc.c:97
An object interface.
Definition: interface.h:109
static void * dest
Definition: strings.h:176
static uint16_t struct vmbus_xfer_pages_operations * op
Definition: netvsc.h:327
#define peerdisc_stat_TYPE(object_type)
Definition: peerdisc.h:114
void intf_put(struct interface *intf)
Decrement reference count on an object interface.
Definition: interface.c:144
#define intf_get_dest_op(intf, type, dest)
Get object interface destination and operation method.
Definition: interface.h:214

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

Referenced by peerblk_done(), and peerdisc_stat().

◆ peerdisc_open()

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

Open PeerDist discovery client.

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

Definition at line 536 of file peerdisc.c.

537  {
538  struct peerdisc_segment *segment;
539  char id_string[ base16_encoded_len ( len ) + 1 /* NUL */ ];
540  char *id_chr;
541  int rc;
542 
543  /* Construct ID string */
544  base16_encode ( id, len, id_string, sizeof ( id_string ) );
545  for ( id_chr = id_string ; *id_chr ; id_chr++ )
546  *id_chr = toupper ( *id_chr );
547 
548  /* Sanity check */
549  assert ( peerdisc->segment == NULL );
550 
551  /* Open socket if this is the first segment */
552  if ( list_empty ( &peerdisc_segments ) &&
553  ( ( rc = peerdisc_socket_open() ) != 0 ) )
554  return rc;
555 
556  /* Find or create segment */
557  if ( ! ( ( segment = peerdisc_find ( id_string ) ) ||
558  ( segment = peerdisc_create ( id_string ) ) ) )
559  return -ENOMEM;
560 
561  /* Add to list of clients */
562  ref_get ( &segment->refcnt );
563  peerdisc->segment = segment;
564  list_add_tail ( &peerdisc->list, &segment->clients );
565 
566  return 0;
567 }
uint16_t segment
Code segment.
Definition: librm.h:252
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
A PeerDist discovery segment.
Definition: peerdisc.h:42
struct list_head list
List of clients.
Definition: peerdisc.h:83
static struct peerdisc_segment * peerdisc_find(const char *id)
Find PeerDist discovery segment.
Definition: peerdisc.c:362
#define list_empty(list)
Test whether a list is empty.
Definition: list.h:136
static size_t base16_encoded_len(size_t raw_len)
Calculate length of base16-encoded data.
Definition: base16.h:21
static int toupper(int character)
Convert character to upper case.
Definition: ctype.h:109
#define ENOMEM
Not enough space.
Definition: errno.h:534
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define list_add_tail(new, head)
Add a new entry to the tail of a list.
Definition: list.h:93
uint8_t id_string[24]
ID string.
Definition: eltorito.h:24
#define ref_get(refcnt)
Get additional reference to object.
Definition: refcnt.h:92
struct peerdisc_segment * segment
Discovery segment.
Definition: peerdisc.h:81
static struct peerdisc_segment * peerdisc_create(const char *id)
Create PeerDist discovery segment.
Definition: peerdisc.c:437
uint32_t len
Length.
Definition: ena.h:14
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
static int peerdisc_socket_open(void)
Open all PeerDist discovery sockets.
Definition: peerdisc.c:125

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

Referenced by peerblk_open().

◆ peerdisc_close()

void peerdisc_close ( struct peerdisc_client peerdisc)

Close PeerDist discovery client.

Parameters
peerdiscPeerDist discovery client

Definition at line 574 of file peerdisc.c.

574  {
575  struct peerdisc_segment *segment = peerdisc->segment;
576 
577  /* Ignore if discovery is already closed */
578  if ( ! segment )
579  return;
580 
581  /* If no peers were discovered, reduce the recommended
582  * discovery timeout to minimise delays on future requests.
583  */
584  if ( list_empty ( &segment->peers ) && peerdisc_timeout_secs ) {
586  DBGC ( segment, "PEERDISC %p reducing timeout to %d "
587  "seconds\n", peerdisc, peerdisc_timeout_secs );
588  }
589 
590  /* Remove from list of clients */
591  peerdisc->segment = NULL;
592  list_del ( &peerdisc->list );
593  ref_put ( &segment->refcnt );
594 
595  /* If this was the last clients, destroy the segment */
596  if ( list_empty ( &segment->clients ) )
598 
599  /* If there are no more segments, close the socket */
600  if ( list_empty ( &peerdisc_segments ) )
601  peerdisc_socket_close ( 0 );
602 }
uint16_t segment
Code segment.
Definition: librm.h:252
static void peerdisc_socket_close(int rc)
Close all PeerDist discovery sockets.
Definition: peerdisc.c:285
A PeerDist discovery segment.
Definition: peerdisc.h:42
#define DBGC(...)
Definition: compiler.h:505
struct list_head list
List of clients.
Definition: peerdisc.h:83
#define list_empty(list)
Test whether a list is empty.
Definition: list.h:136
#define list_del(list)
Delete an entry from a list.
Definition: list.h:119
struct peerdisc_segment * segment
Discovery segment.
Definition: peerdisc.h:81
static void peerdisc_destroy(struct peerdisc_segment *segment)
Destroy PeerDist discovery segment.
Definition: peerdisc.c:508
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
unsigned int peerdisc_timeout_secs
Recommended discovery timeout (in seconds)
Definition: peerdisc.c:74

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

Referenced by peerblk_close().

Variable Documentation

◆ peerdisc_timeout_secs

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 74 of file peerdisc.c.

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