iPXE
slam.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008 Michael Brown <mbrown@fensystems.co.uk>.
3  *
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 any later version.
8  *
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.
13  *
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
17  * 02110-1301, USA.
18  *
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.
22  */
23 
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
25 
26 #include <stdint.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <strings.h>
30 #include <errno.h>
31 #include <assert.h>
32 #include <byteswap.h>
33 #include <ipxe/features.h>
34 #include <ipxe/iobuf.h>
35 #include <ipxe/bitmap.h>
36 #include <ipxe/xfer.h>
37 #include <ipxe/open.h>
38 #include <ipxe/uri.h>
39 #include <ipxe/tcpip.h>
40 #include <ipxe/timer.h>
41 #include <ipxe/retry.h>
42 
43 /** @file
44  *
45  * Scalable Local Area Multicast protocol
46  *
47  * The SLAM protocol is supported only by Etherboot; it was designed
48  * and implemented by Eric Biederman. A server implementation is
49  * available in contrib/mini-slamd. There does not appear to be any
50  * documentation beyond a few sparse comments in Etherboot's
51  * proto_slam.c.
52  *
53  * SLAM packets use three types of data field:
54  *
55  * Nul : A single NUL (0) byte, used as a list terminator
56  *
57  * Raw : A block of raw data
58  *
59  * Int : A variable-length integer, in big-endian order. The length
60  * of the integer is encoded in the most significant three bits.
61  *
62  * Packets received by the client have the following layout:
63  *
64  * Int : Transaction identifier. This is an opaque value.
65  *
66  * Int : Total number of bytes in the transfer.
67  *
68  * Int : Block size, in bytes.
69  *
70  * Int : Packet sequence number within the transfer (if this packet
71  * contains data).
72  *
73  * Raw : Packet data (if this packet contains data).
74  *
75  * Packets transmitted by the client consist of a run-length-encoded
76  * representation of the received-blocks bitmap, looking something
77  * like:
78  *
79  * Int : Number of consecutive successfully-received packets
80  * Int : Number of consecutive missing packets
81  * Int : Number of consecutive successfully-received packets
82  * Int : Number of consecutive missing packets
83  * ....
84  * Nul
85  *
86  */
87 
89 
90 /** Default SLAM server port */
91 #define SLAM_DEFAULT_PORT 10000
92 
93 /** Default SLAM multicast IP address */
94 #define SLAM_DEFAULT_MULTICAST_IP \
95  ( ( 239 << 24 ) | ( 255 << 16 ) | ( 1 << 8 ) | ( 1 << 0 ) )
96 
97 /** Default SLAM multicast port */
98 #define SLAM_DEFAULT_MULTICAST_PORT 10000
99 
100 /** Maximum SLAM header length */
101 #define SLAM_MAX_HEADER_LEN ( 7 /* transaction id */ + 7 /* total_bytes */ + \
102  7 /* block_size */ )
103 
104 /** Maximum number of blocks to request per NACK
105  *
106  * This is a policy decision equivalent to selecting a TCP window
107  * size.
108  */
109 #define SLAM_MAX_BLOCKS_PER_NACK 4
110 
111 /** Maximum SLAM NACK length
112  *
113  * We only ever send a NACK for a single range of up to @c
114  * SLAM_MAX_BLOCKS_PER_NACK blocks.
115  */
116 #define SLAM_MAX_NACK_LEN ( 7 /* block */ + 7 /* #blocks */ + 1 /* NUL */ )
117 
118 /** SLAM slave timeout */
119 #define SLAM_SLAVE_TIMEOUT ( 1 * TICKS_PER_SEC )
120 
121 /** A SLAM request */
122 struct slam_request {
123  /** Reference counter */
124  struct refcnt refcnt;
125 
126  /** Data transfer interface */
127  struct interface xfer;
128  /** Unicast socket */
130  /** Multicast socket */
132 
133  /** Master client retry timer */
135  /** Slave client retry timer */
137 
138  /** Cached header */
140  /** Size of cached header */
141  size_t header_len;
142  /** Total number of bytes in transfer */
143  unsigned long total_bytes;
144  /** Transfer block size */
145  unsigned long block_size;
146  /** Number of blocks in transfer */
147  unsigned long num_blocks;
148  /** Block bitmap */
149  struct bitmap bitmap;
150  /** NACK sent flag */
152 };
153 
154 /**
155  * Free a SLAM request
156  *
157  * @v refcnt Reference counter
158  */
159 static void slam_free ( struct refcnt *refcnt ) {
160  struct slam_request *slam =
161  container_of ( refcnt, struct slam_request, refcnt );
162 
163  bitmap_free ( &slam->bitmap );
164  free ( slam );
165 }
166 
167 /**
168  * Mark SLAM request as complete
169  *
170  * @v slam SLAM request
171  * @v rc Return status code
172  */
173 static void slam_finished ( struct slam_request *slam, int rc ) {
174  static const uint8_t slam_disconnect[] = { 0 };
175 
176  DBGC ( slam, "SLAM %p finished with status code %d (%s)\n",
177  slam, rc, strerror ( rc ) );
178 
179  /* Send a disconnect message if we ever sent anything to the
180  * server.
181  */
182  if ( slam->nack_sent ) {
183  xfer_deliver_raw ( &slam->socket, slam_disconnect,
184  sizeof ( slam_disconnect ) );
185  }
186 
187  /* Stop the retry timers */
188  stop_timer ( &slam->master_timer );
189  stop_timer ( &slam->slave_timer );
190 
191  /* Close all data transfer interfaces */
192  intf_shutdown ( &slam->socket, rc );
193  intf_shutdown ( &slam->mc_socket, rc );
194  intf_shutdown ( &slam->xfer, rc );
195 }
196 
197 /****************************************************************************
198  *
199  * TX datapath
200  *
201  */
202 
203 /**
204  * Add a variable-length value to a SLAM packet
205  *
206  * @v slam SLAM request
207  * @v iobuf I/O buffer
208  * @v value Value to add
209  * @ret rc Return status code
210  *
211  * Adds a variable-length value to the end of an I/O buffer. Will
212  * always leave at least one byte of tailroom in the I/O buffer (to
213  * allow space for the terminating NUL).
214  */
215 static int slam_put_value ( struct slam_request *slam,
216  struct io_buffer *iobuf, unsigned long value ) {
217  uint8_t *data;
218  size_t len;
219  unsigned int i;
220 
221  /* Calculate variable length required to store value. Always
222  * leave at least one byte in the I/O buffer.
223  */
224  len = ( ( flsl ( value ) + 10 ) / 8 );
225  if ( len >= iob_tailroom ( iobuf ) ) {
226  DBGC2 ( slam, "SLAM %p cannot add %zd-byte value\n",
227  slam, len );
228  return -ENOBUFS;
229  }
230  /* There is no valid way within the protocol that we can end
231  * up trying to push a full-sized long (i.e. without space for
232  * the length encoding).
233  */
234  assert ( len <= sizeof ( value ) );
235 
236  /* Add value */
237  data = iob_put ( iobuf, len );
238  for ( i = len ; i-- ; ) {
239  data[i] = value;
240  value >>= 8;
241  }
242  *data |= ( len << 5 );
243  assert ( value == 0 );
244 
245  return 0;
246 }
247 
248 /**
249  * Send SLAM NACK packet
250  *
251  * @v slam SLAM request
252  * @ret rc Return status code
253  */
254 static int slam_tx_nack ( struct slam_request *slam ) {
255  struct io_buffer *iobuf;
256  unsigned long first_block;
257  unsigned long num_blocks;
258  uint8_t *nul;
259  int rc;
260 
261  /* Mark NACK as sent, so that we know we have to disconnect later */
262  slam->nack_sent = 1;
263 
264  /* Allocate I/O buffer */
265  iobuf = xfer_alloc_iob ( &slam->socket, SLAM_MAX_NACK_LEN );
266  if ( ! iobuf ) {
267  DBGC ( slam, "SLAM %p could not allocate I/O buffer\n",
268  slam );
269  rc = -ENOMEM;
270  goto err_alloc;
271  }
272 
273  /* Construct NACK. We always request only a single packet;
274  * this allows us to force multicast-TFTP-style flow control
275  * on the SLAM server, which will otherwise just blast the
276  * data out as fast as it can. On a gigabit network, without
277  * RX checksumming, this would inevitably cause packet drops.
278  */
279  first_block = bitmap_first_gap ( &slam->bitmap );
280  for ( num_blocks = 1 ; ; num_blocks++ ) {
281  if ( num_blocks >= SLAM_MAX_BLOCKS_PER_NACK )
282  break;
283  if ( ( first_block + num_blocks ) >= slam->num_blocks )
284  break;
285  if ( bitmap_test ( &slam->bitmap,
286  ( first_block + num_blocks ) ) )
287  break;
288  }
289  if ( first_block ) {
290  DBGCP ( slam, "SLAM %p transmitting NACK for blocks "
291  "%ld-%ld\n", slam, first_block,
292  ( first_block + num_blocks - 1 ) );
293  } else {
294  DBGC ( slam, "SLAM %p transmitting initial NACK for blocks "
295  "0-%ld\n", slam, ( num_blocks - 1 ) );
296  }
297  if ( ( rc = slam_put_value ( slam, iobuf, first_block ) ) != 0 )
298  goto err_put_value;
299  if ( ( rc = slam_put_value ( slam, iobuf, num_blocks ) ) != 0 )
300  goto err_put_value;
301  nul = iob_put ( iobuf, 1 );
302  *nul = 0;
303 
304  /* Transmit packet */
305  return xfer_deliver_iob ( &slam->socket, iob_disown ( iobuf ) );
306 
307  err_put_value:
308  free_iob ( iobuf );
309  err_alloc:
310  return rc;
311 }
312 
313 /**
314  * Handle SLAM master client retry timer expiry
315  *
316  * @v timer Master retry timer
317  * @v fail Failure indicator
318  */
320  int fail ) {
321  struct slam_request *slam =
323 
324  if ( fail ) {
325  /* Allow timer to stop running. We will terminate the
326  * connection only if the slave timer times out.
327  */
328  DBGC ( slam, "SLAM %p giving up acting as master client\n",
329  slam );
330  } else {
331  /* Retransmit NACK */
332  start_timer ( timer );
333  slam_tx_nack ( slam );
334  }
335 }
336 
337 /**
338  * Handle SLAM slave client retry timer expiry
339  *
340  * @v timer Master retry timer
341  * @v fail Failure indicator
342  */
344  int fail ) {
345  struct slam_request *slam =
347 
348  if ( fail ) {
349  /* Terminate connection */
350  slam_finished ( slam, -ETIMEDOUT );
351  } else {
352  /* Try sending a NACK */
353  DBGC ( slam, "SLAM %p trying to become master client\n",
354  slam );
355  start_timer ( timer );
356  slam_tx_nack ( slam );
357  }
358 }
359 
360 /****************************************************************************
361  *
362  * RX datapath
363  *
364  */
365 
366 /**
367  * Read and strip a variable-length value from a SLAM packet
368  *
369  * @v slam SLAM request
370  * @v iobuf I/O buffer
371  * @v value Value to fill in, or NULL to ignore value
372  * @ret rc Return status code
373  *
374  * Reads a variable-length value from the start of the I/O buffer.
375  */
376 static int slam_pull_value ( struct slam_request *slam,
377  struct io_buffer *iobuf,
378  unsigned long *value ) {
379  uint8_t *data;
380  size_t len;
381 
382  /* Sanity check */
383  if ( iob_len ( iobuf ) == 0 ) {
384  DBGC ( slam, "SLAM %p empty value\n", slam );
385  return -EINVAL;
386  }
387 
388  /* Read and verify length of value */
389  data = iobuf->data;
390  len = ( *data >> 5 );
391  if ( ( len == 0 ) ||
392  ( value && ( len > sizeof ( *value ) ) ) ) {
393  DBGC ( slam, "SLAM %p invalid value length %zd bytes\n",
394  slam, len );
395  return -EINVAL;
396  }
397  if ( len > iob_len ( iobuf ) ) {
398  DBGC ( slam, "SLAM %p value extends beyond I/O buffer\n",
399  slam );
400  return -EINVAL;
401  }
402 
403  /* Strip value */
404  iob_pull ( iobuf, len );
405 
406  /* Read value, if applicable */
407  if ( value ) {
408  *value = ( *data & 0x1f );
409  while ( --len ) {
410  *value <<= 8;
411  *value |= *(++data);
412  }
413  }
414 
415  return 0;
416 }
417 
418 /**
419  * Read and strip SLAM header
420  *
421  * @v slam SLAM request
422  * @v iobuf I/O buffer
423  * @ret rc Return status code
424  */
425 static int slam_pull_header ( struct slam_request *slam,
426  struct io_buffer *iobuf ) {
427  void *header = iobuf->data;
428  unsigned long total_bytes;
429  unsigned long block_size;
430  int rc;
431 
432  /* If header matches cached header, just pull it and return */
433  if ( ( slam->header_len <= iob_len ( iobuf ) ) &&
434  ( memcmp ( slam->header, iobuf->data, slam->header_len ) == 0 )){
435  iob_pull ( iobuf, slam->header_len );
436  return 0;
437  }
438 
439  DBGC ( slam, "SLAM %p detected changed header; resetting\n", slam );
440 
441  /* Read and strip transaction ID, total number of bytes, and
442  * block size.
443  */
444  if ( ( rc = slam_pull_value ( slam, iobuf, NULL ) ) != 0 )
445  return rc;
446  if ( ( rc = slam_pull_value ( slam, iobuf, &total_bytes ) ) != 0 )
447  return rc;
448  if ( ( rc = slam_pull_value ( slam, iobuf, &block_size ) ) != 0 )
449  return rc;
450 
451  /* Sanity check */
452  if ( block_size == 0 ) {
453  DBGC ( slam, "SLAM %p ignoring zero block size\n", slam );
454  return -EINVAL;
455  }
456 
457  /* Update the cached header */
458  slam->header_len = ( iobuf->data - header );
459  assert ( slam->header_len <= sizeof ( slam->header ) );
460  memcpy ( slam->header, header, slam->header_len );
461 
462  /* Calculate number of blocks */
463  slam->total_bytes = total_bytes;
464  slam->block_size = block_size;
465  slam->num_blocks = ( ( total_bytes + block_size - 1 ) / block_size );
466  DBGC ( slam, "SLAM %p has total bytes %ld, block size %ld, num "
467  "blocks %ld\n", slam, slam->total_bytes, slam->block_size,
468  slam->num_blocks );
469 
470  /* Discard and reset the bitmap */
471  bitmap_free ( &slam->bitmap );
472  memset ( &slam->bitmap, 0, sizeof ( slam->bitmap ) );
473 
474  /* Allocate a new bitmap */
475  if ( ( rc = bitmap_resize ( &slam->bitmap,
476  slam->num_blocks ) ) != 0 ) {
477  /* Failure to allocate a bitmap is fatal */
478  DBGC ( slam, "SLAM %p could not allocate bitmap for %ld "
479  "blocks: %s\n", slam, slam->num_blocks,
480  strerror ( rc ) );
481  slam_finished ( slam, rc );
482  return rc;
483  }
484 
485  /* Notify recipient of file size */
486  xfer_seek ( &slam->xfer, slam->total_bytes );
487 
488  return 0;
489 }
490 
491 /**
492  * Receive SLAM data packet
493  *
494  * @v slam SLAM request
495  * @v iobuf I/O buffer
496  * @ret rc Return status code
497  */
498 static int slam_mc_socket_deliver ( struct slam_request *slam,
499  struct io_buffer *iobuf,
500  struct xfer_metadata *rx_meta __unused ) {
501  struct xfer_metadata meta;
502  unsigned long packet;
503  size_t len;
504  int rc;
505 
506  /* Stop the master client timer. Restart the slave client timer. */
507  stop_timer ( &slam->master_timer );
508  stop_timer ( &slam->slave_timer );
510 
511  /* Read and strip packet header */
512  if ( ( rc = slam_pull_header ( slam, iobuf ) ) != 0 )
513  goto err_discard;
514 
515  /* Read and strip packet number */
516  if ( ( rc = slam_pull_value ( slam, iobuf, &packet ) ) != 0 )
517  goto err_discard;
518 
519  /* Sanity check packet number */
520  if ( packet >= slam->num_blocks ) {
521  DBGC ( slam, "SLAM %p received out-of-range packet %ld "
522  "(num_blocks=%ld)\n", slam, packet, slam->num_blocks );
523  rc = -EINVAL;
524  goto err_discard;
525  }
526 
527  /* Sanity check length */
528  len = iob_len ( iobuf );
529  if ( len > slam->block_size ) {
530  DBGC ( slam, "SLAM %p received oversize packet of %zd bytes "
531  "(block_size=%ld)\n", slam, len, slam->block_size );
532  rc = -EINVAL;
533  goto err_discard;
534  }
535  if ( ( packet != ( slam->num_blocks - 1 ) ) &&
536  ( len < slam->block_size ) ) {
537  DBGC ( slam, "SLAM %p received short packet of %zd bytes "
538  "(block_size=%ld)\n", slam, len, slam->block_size );
539  rc = -EINVAL;
540  goto err_discard;
541  }
542 
543  /* If we have already seen this packet, discard it */
544  if ( bitmap_test ( &slam->bitmap, packet ) ) {
545  goto discard;
546  }
547 
548  /* Pass to recipient */
549  memset ( &meta, 0, sizeof ( meta ) );
550  meta.flags = XFER_FL_ABS_OFFSET;
551  meta.offset = ( packet * slam->block_size );
552  if ( ( rc = xfer_deliver ( &slam->xfer, iobuf, &meta ) ) != 0 )
553  goto err;
554 
555  /* Mark block as received */
556  bitmap_set ( &slam->bitmap, packet );
557 
558  /* If we have received all blocks, terminate */
559  if ( bitmap_full ( &slam->bitmap ) )
560  slam_finished ( slam, 0 );
561 
562  return 0;
563 
564  err_discard:
565  discard:
566  free_iob ( iobuf );
567  err:
568  return rc;
569 }
570 
571 /**
572  * Receive SLAM non-data packet
573  *
574  * @v slam SLAM request
575  * @v iobuf I/O buffer
576  * @ret rc Return status code
577  */
578 static int slam_socket_deliver ( struct slam_request *slam,
579  struct io_buffer *iobuf,
580  struct xfer_metadata *rx_meta __unused ) {
581  int rc;
582 
583  /* Restart the master client timer */
584  stop_timer ( &slam->master_timer );
585  start_timer ( &slam->master_timer );
586 
587  /* Read and strip packet header */
588  if ( ( rc = slam_pull_header ( slam, iobuf ) ) != 0 )
589  goto discard;
590 
591  /* Sanity check */
592  if ( iob_len ( iobuf ) != 0 ) {
593  DBGC ( slam, "SLAM %p received trailing garbage:\n", slam );
594  DBGC_HD ( slam, iobuf->data, iob_len ( iobuf ) );
595  rc = -EINVAL;
596  goto discard;
597  }
598 
599  /* Discard packet */
600  free_iob ( iobuf );
601 
602  /* Send NACK in reply */
603  slam_tx_nack ( slam );
604 
605  return 0;
606 
607  discard:
608  free_iob ( iobuf );
609  return rc;
610 
611 }
612 
613 /** SLAM unicast socket interface operations */
617 };
618 
619 /** SLAM unicast socket interface descriptor */
621  INTF_DESC ( struct slam_request, socket, slam_socket_operations );
622 
623 /** SLAM multicast socket interface operations */
627 };
628 
629 /** SLAM multicast socket interface descriptor */
631  INTF_DESC ( struct slam_request, mc_socket, slam_mc_socket_operations );
632 
633 /****************************************************************************
634  *
635  * Data transfer interface
636  *
637  */
638 
639 /** SLAM data transfer interface operations */
642 };
643 
644 /** SLAM data transfer interface descriptor */
646  INTF_DESC ( struct slam_request, xfer, slam_xfer_operations );
647 
648 /**
649  * Parse SLAM URI multicast address
650  *
651  * @v slam SLAM request
652  * @v path Path portion of x-slam:// URI
653  * @v address Socket address to fill in
654  * @ret rc Return status code
655  */
656 static int slam_parse_multicast_address ( struct slam_request *slam,
657  const char *path,
658  struct sockaddr_tcpip *address ) {
659  char *path_dup;
660  char *sep;
661  char *end;
662  int rc;
663 
664  /* Create temporary copy of path, minus the leading '/' */
665  assert ( *path == '/' );
666  path_dup = strdup ( path + 1 );
667  if ( ! path_dup ) {
668  rc = -ENOMEM;
669  goto err_strdup;
670  }
671 
672  /* Parse port, if present */
673  sep = strchr ( path_dup, ':' );
674  if ( sep ) {
675  *(sep++) = '\0';
676  address->st_port = htons ( strtoul ( sep, &end, 0 ) );
677  if ( *end != '\0' ) {
678  DBGC ( slam, "SLAM %p invalid multicast port "
679  "\"%s\"\n", slam, sep );
680  rc = -EINVAL;
681  goto err_port;
682  }
683  }
684 
685  /* Parse address */
686  if ( sock_aton ( path_dup, ( ( struct sockaddr * ) address ) ) == 0 ) {
687  DBGC ( slam, "SLAM %p invalid multicast address \"%s\"\n",
688  slam, path_dup );
689  rc = -EINVAL;
690  goto err_addr;
691  }
692 
693  /* Success */
694  rc = 0;
695 
696  err_addr:
697  err_port:
698  free ( path_dup );
699  err_strdup:
700  return rc;
701 }
702 
703 /**
704  * Initiate a SLAM request
705  *
706  * @v xfer Data transfer interface
707  * @v uri Uniform Resource Identifier
708  * @ret rc Return status code
709  */
710 static int slam_open ( struct interface *xfer, struct uri *uri ) {
711  static const struct sockaddr_in default_multicast = {
712  .sin_family = AF_INET,
713  .sin_port = htons ( SLAM_DEFAULT_MULTICAST_PORT ),
714  .sin_addr = { htonl ( SLAM_DEFAULT_MULTICAST_IP ) },
715  };
716  struct slam_request *slam;
717  struct sockaddr_tcpip server;
718  struct sockaddr_tcpip multicast;
719  int rc;
720 
721  /* Sanity checks */
722  if ( ! uri->host )
723  return -EINVAL;
724 
725  /* Allocate and populate structure */
726  slam = zalloc ( sizeof ( *slam ) );
727  if ( ! slam )
728  return -ENOMEM;
729  ref_init ( &slam->refcnt, slam_free );
730  intf_init ( &slam->xfer, &slam_xfer_desc, &slam->refcnt );
731  intf_init ( &slam->socket, &slam_socket_desc, &slam->refcnt );
732  intf_init ( &slam->mc_socket, &slam_mc_socket_desc, &slam->refcnt );
733  timer_init ( &slam->master_timer, slam_master_timer_expired,
734  &slam->refcnt );
735  timer_init ( &slam->slave_timer, slam_slave_timer_expired,
736  &slam->refcnt );
737  /* Fake an invalid cached header of { 0x00, ... } */
738  slam->header_len = 1;
739  /* Fake parameters for initial NACK */
740  slam->num_blocks = 1;
741  if ( ( rc = bitmap_resize ( &slam->bitmap, 1 ) ) != 0 ) {
742  DBGC ( slam, "SLAM %p could not allocate initial bitmap: "
743  "%s\n", slam, strerror ( rc ) );
744  goto err;
745  }
746 
747  /* Open unicast socket */
748  memset ( &server, 0, sizeof ( server ) );
749  server.st_port = htons ( uri_port ( uri, SLAM_DEFAULT_PORT ) );
750  if ( ( rc = xfer_open_named_socket ( &slam->socket, SOCK_DGRAM,
751  ( struct sockaddr * ) &server,
752  uri->host, NULL ) ) != 0 ) {
753  DBGC ( slam, "SLAM %p could not open unicast socket: %s\n",
754  slam, strerror ( rc ) );
755  goto err;
756  }
757 
758  /* Open multicast socket */
759  memcpy ( &multicast, &default_multicast, sizeof ( multicast ) );
760  if ( uri->path &&
761  ( ( rc = slam_parse_multicast_address ( slam, uri->path,
762  &multicast ) ) != 0 ) ) {
763  goto err;
764  }
765  if ( ( rc = xfer_open_socket ( &slam->mc_socket, SOCK_DGRAM,
766  ( struct sockaddr * ) &multicast,
767  ( struct sockaddr * ) &multicast ) ) != 0 ) {
768  DBGC ( slam, "SLAM %p could not open multicast socket: %s\n",
769  slam, strerror ( rc ) );
770  goto err;
771  }
772 
773  /* Start slave retry timer */
775 
776  /* Attach to parent interface, mortalise self, and return */
777  intf_plug_plug ( &slam->xfer, xfer );
778  ref_put ( &slam->refcnt );
779  return 0;
780 
781  err:
782  slam_finished ( slam, rc );
783  ref_put ( &slam->refcnt );
784  return rc;
785 }
786 
787 /** SLAM URI opener */
788 struct uri_opener slam_uri_opener __uri_opener = {
789  .scheme = "x-slam",
790  .open = slam_open,
791 };
#define iob_pull(iobuf, len)
Definition: iobuf.h:98
#define EINVAL
Invalid argument.
Definition: errno.h:428
An object interface operation.
Definition: interface.h:17
size_t header_len
Size of cached header.
Definition: slam.c:141
TCP/IP socket address.
Definition: tcpip.h:75
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:244
#define iob_put(iobuf, len)
Definition: iobuf.h:116
Data transfer metadata.
Definition: xfer.h:22
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition: interface.c:273
struct refcnt refcnt
Reference counter.
Definition: slam.c:124
int xfer_deliver_iob(struct interface *intf, struct io_buffer *iobuf)
Deliver datagram as I/O buffer without metadata.
Definition: xfer.c:254
static int slam_pull_value(struct slam_request *slam, struct io_buffer *iobuf, unsigned long *value)
Read and strip a variable-length value from a SLAM packet.
Definition: slam.c:376
unsigned long strtoul(const char *string, char **endp, int base)
Convert string to numeric value.
Definition: string.c:441
static struct interface_operation slam_socket_operations[]
SLAM unicast socket interface operations.
Definition: slam.c:614
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
#define FEATURE_PROTOCOL
Network protocols.
Definition: features.h:21
static void bitmap_free(struct bitmap *bitmap)
Free bitmap resources.
Definition: bitmap.h:57
static struct interface_descriptor slam_xfer_desc
SLAM data transfer interface descriptor.
Definition: slam.c:645
uint8_t header[SLAM_MAX_HEADER_LEN]
Cached header.
Definition: slam.c:139
#define XFER_FL_ABS_OFFSET
Offset is absolute.
Definition: xfer.h:47
#define ref_init(refcnt, free)
Initialise a reference counter.
Definition: refcnt.h:64
Error codes.
int xfer_open_socket(struct interface *intf, int semantics, struct sockaddr *peer, struct sockaddr *local)
Open socket.
Definition: open.c:141
I/O buffers.
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:145
uint64_t address
Base address.
Definition: ena.h:24
#define SOCK_DGRAM
Definition: socket.h:29
Retry timers.
static int slam_socket_deliver(struct slam_request *slam, struct io_buffer *iobuf, struct xfer_metadata *rx_meta __unused)
Receive SLAM non-data packet.
Definition: slam.c:578
#define DBGC(...)
Definition: compiler.h:505
A retry timer.
Definition: retry.h:21
void intf_plug_plug(struct interface *a, struct interface *b)
Plug two object interfaces together.
Definition: interface.c:102
struct uri_opener slam_uri_opener __uri_opener
SLAM URI opener.
Definition: slam.c:788
struct io_buffer * xfer_alloc_iob(struct interface *intf, size_t len)
Allocate I/O buffer.
Definition: xfer.c:157
iPXE timers
IPv4 socket address.
Definition: in.h:82
Uniform Resource Identifiers.
int sock_aton(const char *string, struct sockaddr *sa)
Parse socket address.
Definition: socket.c:59
sa_family_t sin_family
Socket address family (part of struct sockaddr)
Definition: in.h:87
#define htonl(value)
Definition: byteswap.h:133
A SLAM request.
Definition: slam.c:122
int xfer_deliver_raw(struct interface *intf, const void *data, size_t len)
Deliver datagram as raw data without metadata.
Definition: xfer.c:287
int nack_sent
NACK sent flag.
Definition: slam.c:151
Data transfer interfaces.
A reference counter.
Definition: refcnt.h:26
A timer.
Definition: timer.h:28
int bitmap_test(struct bitmap *bitmap, unsigned int bit)
Test bit in bitmap.
Definition: bitmap.c:78
#define ENOMEM
Not enough space.
Definition: errno.h:534
#define iob_disown(iobuf)
Disown an I/O buffer.
Definition: iobuf.h:208
void * memcpy(void *dest, const void *src, size_t len) __nonnull
Assertions.
assert((readw(&hdr->flags) &(GTF_reading|GTF_writing))==0)
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
An object interface.
Definition: interface.h:109
#define SLAM_DEFAULT_PORT
Default SLAM server port.
Definition: slam.c:91
static int slam_put_value(struct slam_request *slam, struct io_buffer *iobuf, unsigned long value)
Add a variable-length value to a SLAM packet.
Definition: slam.c:215
const char * path
Path.
Definition: uri.h:66
unsigned long block_size
Transfer block size.
Definition: slam.c:145
static int slam_open(struct interface *xfer, struct uri *uri)
Initiate a SLAM request.
Definition: slam.c:710
const char * scheme
URI protocol name.
Definition: open.h:53
static struct interface_operation slam_xfer_operations[]
SLAM data transfer interface operations.
Definition: slam.c:640
Transport-network layer interface.
static int slam_pull_header(struct slam_request *slam, struct io_buffer *iobuf)
Read and strip SLAM header.
Definition: slam.c:425
Feature list.
int bitmap_resize(struct bitmap *bitmap, unsigned int new_length)
Resize bitmap.
Definition: bitmap.c:42
static struct interface_descriptor slam_socket_desc
SLAM unicast socket interface descriptor.
Definition: slam.c:620
A bitmap.
Definition: bitmap.h:39
struct retry_timer master_timer
Master client retry timer.
Definition: slam.c:134
int xfer_seek(struct interface *intf, off_t offset)
Seek to position.
Definition: xfer.c:350
unsigned long num_blocks
Number of blocks in transfer.
Definition: slam.c:147
pseudo_bit_t value[0x00020]
Definition: arbel.h:13
int meta(WINDOW *, bool)
static struct interface_descriptor slam_mc_socket_desc
SLAM multicast socket interface descriptor.
Definition: slam.c:630
uint16_t st_port
TCP/IP port.
Definition: tcpip.h:81
Generalized socket address structure.
Definition: socket.h:96
An object interface descriptor.
Definition: interface.h:40
#define SLAM_DEFAULT_MULTICAST_PORT
Default SLAM multicast port.
Definition: slam.c:98
struct interface socket
Unicast socket.
Definition: slam.c:129
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
static void(* free)(struct refcnt *refcnt))
Definition: refcnt.h:54
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
char * strchr(const char *src, int character)
Find character within a string.
Definition: string.c:241
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:151
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
char * strdup(const char *src)
Duplicate string.
Definition: string.c:350
#define DBGC_HD(...)
Definition: compiler.h:507
static size_t iob_tailroom(struct io_buffer *iobuf)
Calculate available space at end of an I/O buffer.
Definition: iobuf.h:171
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:193
unsigned char uint8_t
Definition: stdint.h:10
Data transfer interface opening.
struct interface mc_socket
Multicast socket.
Definition: slam.c:131
#define SLAM_DEFAULT_MULTICAST_IP
Default SLAM multicast IP address.
Definition: slam.c:94
static void slam_master_timer_expired(struct retry_timer *timer, int fail)
Handle SLAM master client retry timer expiry.
Definition: slam.c:319
const char * host
Host name.
Definition: uri.h:62
void bitmap_set(struct bitmap *bitmap, unsigned int bit)
Set bit in bitmap.
Definition: bitmap.c:93
void start_timer(struct retry_timer *timer)
Start timer.
Definition: retry.c:93
void start_timer_fixed(struct retry_timer *timer, unsigned long timeout)
Start timer with a specified timeout.
Definition: retry.c:64
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
#define DHCP_EB_FEATURE_SLAM
SLAM protocol.
Definition: features.h:46
static void slam_slave_timer_expired(struct retry_timer *timer, int fail)
Handle SLAM slave client retry timer expiry.
Definition: slam.c:343
#define SLAM_MAX_BLOCKS_PER_NACK
Maximum number of blocks to request per NACK.
Definition: slam.c:109
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:65
void stop_timer(struct retry_timer *timer)
Stop timer.
Definition: retry.c:117
Bitmaps for multicast downloads.
static void slam_finished(struct slam_request *slam, int rc)
Mark SLAM request as complete.
Definition: slam.c:173
struct interface xfer
Data transfer interface.
Definition: slam.c:127
uint32_t len
Length.
Definition: ena.h:14
#define ENOBUFS
No buffer space available.
Definition: errno.h:498
#define DBGC2(...)
Definition: compiler.h:522
#define SLAM_MAX_NACK_LEN
Maximum SLAM NACK length.
Definition: slam.c:116
FEATURE(FEATURE_PROTOCOL, "SLAM", DHCP_EB_FEATURE_SLAM, 1)
void * data
Start of data.
Definition: iobuf.h:44
#define SLAM_SLAVE_TIMEOUT
SLAM slave timeout.
Definition: slam.c:119
struct ena_aq_header header
Header.
Definition: ena.h:12
device nvs block_size
Definition: threewire.h:58
unsigned int uri_port(const struct uri *uri, unsigned int default_port)
Get port from URI.
Definition: uri.c:441
uint32_t end
Ending offset.
Definition: netvsc.h:18
static unsigned int bitmap_first_gap(struct bitmap *bitmap)
Get first gap within bitmap.
Definition: bitmap.h:69
#define DBGCP(...)
Definition: compiler.h:539
#define SLAM_MAX_HEADER_LEN
Maximum SLAM header length.
Definition: slam.c:101
A Uniform Resource Identifier.
Definition: uri.h:50
static int slam_parse_multicast_address(struct slam_request *slam, const char *path, struct sockaddr_tcpip *address)
Parse SLAM URI multicast address.
Definition: slam.c:656
#define flsl(x)
Find last (i.e.
Definition: strings.h:157
static int slam_mc_socket_deliver(struct slam_request *slam, struct io_buffer *iobuf, struct xfer_metadata *rx_meta __unused)
Receive SLAM data packet.
Definition: slam.c:498
struct bitmap bitmap
Block bitmap.
Definition: slam.c:149
struct arbelprm_port_state_change_st data
Message.
Definition: arbel.h:12
static void slam_free(struct refcnt *refcnt)
Free a SLAM request.
Definition: slam.c:159
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:173
static struct interface_operation slam_mc_socket_operations[]
SLAM multicast socket interface operations.
Definition: slam.c:624
A URI opener.
Definition: open.h:47
static int bitmap_full(struct bitmap *bitmap)
Check to see if bitmap is full.
Definition: bitmap.h:81
int memcmp(const void *first, const void *second, size_t len)
Compare memory regions.
Definition: string.c:98
struct retry_timer slave_timer
Slave client retry timer.
Definition: slam.c:136
unsigned long total_bytes
Total number of bytes in transfer.
Definition: slam.c:143
#define NULL
NULL pointer (VOID *)
Definition: Base.h:362
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:669
String functions.
static int slam_tx_nack(struct slam_request *slam)
Send SLAM NACK packet.
Definition: slam.c:254
#define htons(value)
Definition: byteswap.h:135
#define AF_INET
IPv4 Internet addresses.
Definition: socket.h:63
int xfer_open_named_socket(struct interface *xfer, int semantics, struct sockaddr *peer, const char *name, struct sockaddr *local)
Open named socket.
Definition: resolv.c:402
#define ref_put(refcnt)
Drop reference to object.
Definition: refcnt.h:106
String functions.
void * memset(void *dest, int character, size_t len) __nonnull
A persistent I/O buffer.
Definition: iobuf.h:32