iPXE
pinger.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include <errno.h>
00029 #include <ipxe/refcnt.h>
00030 #include <ipxe/interface.h>
00031 #include <ipxe/job.h>
00032 #include <ipxe/xfer.h>
00033 #include <ipxe/iobuf.h>
00034 #include <ipxe/open.h>
00035 #include <ipxe/socket.h>
00036 #include <ipxe/retry.h>
00037 #include <ipxe/pinger.h>
00038 
00039 /** @file
00040  *
00041  * ICMP ping sender
00042  *
00043  */
00044 
00045 /* Disambiguate the various error causes */
00046 #define EPROTO_LEN __einfo_error ( EINFO_EPROTO_LEN )
00047 #define EINFO_EPROTO_LEN __einfo_uniqify ( EINFO_EPROTO, 0x01, \
00048                                            "Incorrect reply length" )
00049 #define EPROTO_DATA __einfo_error ( EINFO_EPROTO_DATA )
00050 #define EINFO_EPROTO_DATA __einfo_uniqify ( EINFO_EPROTO, 0x02, \
00051                                             "Incorrect reply data" )
00052 #define EPROTO_SEQ __einfo_error ( EINFO_EPROTO_SEQ )
00053 #define EINFO_EPROTO_SEQ __einfo_uniqify ( EINFO_EPROTO, 0x03, \
00054                                            "Delayed or out-of-sequence reply" )
00055 
00056 /** A pinger */
00057 struct pinger {
00058         /** Reference count */
00059         struct refcnt refcnt;
00060 
00061         /** Job control interface */
00062         struct interface job;
00063         /** Data transfer interface */
00064         struct interface xfer;
00065 
00066         /** Timer */
00067         struct retry_timer timer;
00068         /** Timeout */
00069         unsigned long timeout;
00070 
00071         /** Payload length */
00072         size_t len;
00073         /** Current sequence number */
00074         uint16_t sequence;
00075         /** Response for current sequence number is still pending */
00076         int pending;
00077         /** Number of remaining expiry events (zero to continue indefinitely) */
00078         unsigned int remaining;
00079         /** Return status */
00080         int rc;
00081 
00082         /** Callback function
00083          *
00084          * @v src               Source socket address, or NULL
00085          * @v sequence          Sequence number
00086          * @v len               Payload length
00087          * @v rc                Status code
00088          */
00089         void ( * callback ) ( struct sockaddr *src, unsigned int sequence,
00090                               size_t len, int rc );
00091 };
00092 
00093 /**
00094  * Generate payload
00095  *
00096  * @v pinger            Pinger
00097  * @v data              Data buffer
00098  */
00099 static void pinger_generate ( struct pinger *pinger, void *data ) {
00100         uint8_t *bytes = data;
00101         unsigned int i;
00102 
00103         /* Generate byte sequence */
00104         for ( i = 0 ; i < pinger->len ; i++ )
00105                 bytes[i] = ( i & 0xff );
00106 }
00107 
00108 /**
00109  * Verify payload
00110  *
00111  * @v pinger            Pinger
00112  * @v data              Data buffer
00113  * @ret rc              Return status code
00114  */
00115 static int pinger_verify ( struct pinger *pinger, const void *data ) {
00116         const uint8_t *bytes = data;
00117         unsigned int i;
00118 
00119         /* Check byte sequence */
00120         for ( i = 0 ; i < pinger->len ; i++ ) {
00121                 if ( bytes[i] != ( i & 0xff ) )
00122                         return -EPROTO_DATA;
00123         }
00124 
00125         return 0;
00126 }
00127 
00128 /**
00129  * Close pinger
00130  *
00131  * @v pinger            Pinger
00132  * @v rc                Reason for close
00133  */
00134 static void pinger_close ( struct pinger *pinger, int rc ) {
00135 
00136         /* Stop timer */
00137         stop_timer ( &pinger->timer );
00138 
00139         /* Shut down interfaces */
00140         intf_shutdown ( &pinger->xfer, rc );
00141         intf_shutdown ( &pinger->job, rc );
00142 }
00143 
00144 /**
00145  * Handle data transfer window change
00146  *
00147  * @v pinger            Pinger
00148  */
00149 static void pinger_window_changed ( struct pinger *pinger ) {
00150 
00151         /* Do nothing if timer is already running */
00152         if ( timer_running ( &pinger->timer ) )
00153                 return;
00154 
00155         /* Start timer when window opens for the first time */
00156         if ( xfer_window ( &pinger->xfer ) )
00157                 start_timer_nodelay ( &pinger->timer );
00158 }
00159 
00160 /**
00161  * Handle timer expiry
00162  *
00163  * @v timer             Timer
00164  * @v over              Failure indicator
00165  */
00166 static void pinger_expired ( struct retry_timer *timer, int over __unused ) {
00167         struct pinger *pinger = container_of ( timer, struct pinger, timer );
00168         struct xfer_metadata meta;
00169         struct io_buffer *iobuf;
00170         int rc;
00171 
00172         /* If no response has been received, notify the callback function */
00173         if ( pinger->pending && pinger->callback )
00174                 pinger->callback ( NULL, pinger->sequence, 0, -ETIMEDOUT );
00175 
00176         /* Check for termination */
00177         if ( pinger->remaining && ( --pinger->remaining == 0 ) ) {
00178                 pinger_close ( pinger, pinger->rc );
00179                 return;
00180         }
00181 
00182         /* Increase sequence number */
00183         pinger->sequence++;
00184 
00185         /* Restart timer.  Do this before attempting to transmit, in
00186          * case the transmission attempt fails.
00187          */
00188         start_timer_fixed ( &pinger->timer, pinger->timeout );
00189         pinger->pending = 1;
00190 
00191         /* Allocate I/O buffer */
00192         iobuf = xfer_alloc_iob ( &pinger->xfer, pinger->len );
00193         if ( ! iobuf ) {
00194                 DBGC ( pinger, "PINGER %p could not allocate I/O buffer\n",
00195                        pinger );
00196                 return;
00197         }
00198 
00199         /* Generate payload */
00200         pinger_generate ( pinger, iob_put ( iobuf, pinger->len ) );
00201 
00202         /* Generate metadata */
00203         memset ( &meta, 0, sizeof ( meta ) );
00204         meta.flags = XFER_FL_ABS_OFFSET;
00205         meta.offset = pinger->sequence;
00206 
00207         /* Transmit packet */
00208         if ( ( rc = xfer_deliver ( &pinger->xfer, iobuf, &meta ) ) != 0 ) {
00209                 DBGC ( pinger, "PINGER %p could not transmit: %s\n",
00210                        pinger, strerror ( rc ) );
00211                 return;
00212         }
00213 }
00214 
00215 /**
00216  * Handle received data
00217  *
00218  * @v pinger            Pinger
00219  * @v iobuf             I/O buffer
00220  * @v meta              Data transfer metadata
00221  * @ret rc              Return status code
00222  */
00223 static int pinger_deliver ( struct pinger *pinger, struct io_buffer *iobuf,
00224                             struct xfer_metadata *meta ) {
00225         size_t len = iob_len ( iobuf );
00226         uint16_t sequence = meta->offset;
00227         int terminate = 0;
00228         int rc;
00229 
00230         /* Clear response pending flag, if applicable */
00231         if ( sequence == pinger->sequence )
00232                 pinger->pending = 0;
00233 
00234         /* Check for errors */
00235         if ( len != pinger->len ) {
00236                 /* Incorrect length: terminate immediately if we are
00237                  * not pinging indefinitely.
00238                  */
00239                 DBGC ( pinger, "PINGER %p received incorrect length %zd "
00240                        "(expected %zd)\n", pinger, len, pinger->len );
00241                 rc = -EPROTO_LEN;
00242                 terminate = ( pinger->remaining != 0 );
00243         } else if ( ( rc = pinger_verify ( pinger, iobuf->data ) ) != 0 ) {
00244                 /* Incorrect data: terminate immediately if we are not
00245                  * pinging indefinitely.
00246                  */
00247                 DBGC ( pinger, "PINGER %p received incorrect data:\n", pinger );
00248                 DBGC_HDA ( pinger, 0, iobuf->data, iob_len ( iobuf ) );
00249                 terminate = ( pinger->remaining != 0 );
00250         } else if ( sequence != pinger->sequence ) {
00251                 /* Incorrect sequence number (probably a delayed response):
00252                  * report via callback but otherwise ignore.
00253                  */
00254                 DBGC ( pinger, "PINGER %p received sequence %d (expected %d)\n",
00255                        pinger, sequence, pinger->sequence );
00256                 rc = -EPROTO_SEQ;
00257                 terminate = 0;
00258         } else {
00259                 /* Success: record that a packet was successfully received,
00260                  * and terminate if we expect to send no further packets.
00261                  */
00262                 rc = 0;
00263                 pinger->rc = 0;
00264                 terminate = ( pinger->remaining == 1 );
00265         }
00266 
00267         /* Discard I/O buffer */
00268         free_iob ( iobuf );
00269 
00270         /* Notify callback function, if applicable */
00271         if ( pinger->callback )
00272                 pinger->callback ( meta->src, sequence, len, rc );
00273 
00274         /* Terminate if applicable */
00275         if ( terminate )
00276                 pinger_close ( pinger, rc );
00277 
00278         return rc;
00279 }
00280 
00281 /** Pinger data transfer interface operations */
00282 static struct interface_operation pinger_xfer_op[] = {
00283         INTF_OP ( xfer_deliver, struct pinger *, pinger_deliver ),
00284         INTF_OP ( xfer_window_changed, struct pinger *, pinger_window_changed ),
00285         INTF_OP ( intf_close, struct pinger *, pinger_close ),
00286 };
00287 
00288 /** Pinger data transfer interface descriptor */
00289 static struct interface_descriptor pinger_xfer_desc =
00290         INTF_DESC ( struct pinger, xfer, pinger_xfer_op );
00291 
00292 /** Pinger job control interface operations */
00293 static struct interface_operation pinger_job_op[] = {
00294         INTF_OP ( intf_close, struct pinger *, pinger_close ),
00295 };
00296 
00297 /** Pinger job control interface descriptor */
00298 static struct interface_descriptor pinger_job_desc =
00299         INTF_DESC ( struct pinger, job, pinger_job_op );
00300 
00301 /**
00302  * Create pinger
00303  *
00304  * @v job               Job control interface
00305  * @v hostname          Hostname to ping
00306  * @v timeout           Timeout (in ticks)
00307  * @v len               Payload length
00308  * @v count             Number of packets to send (or zero for no limit)
00309  * @v callback          Callback function (or NULL)
00310  * @ret rc              Return status code
00311  */
00312 int create_pinger ( struct interface *job, const char *hostname,
00313                     unsigned long timeout, size_t len, unsigned int count,
00314                     void ( * callback ) ( struct sockaddr *src,
00315                                           unsigned int sequence, size_t len,
00316                                           int rc ) ) {
00317         struct pinger *pinger;
00318         int rc;
00319 
00320         /* Sanity check */
00321         if ( ! timeout )
00322                 return -EINVAL;
00323 
00324         /* Allocate and initialise structure */
00325         pinger = zalloc ( sizeof ( *pinger ) );
00326         if ( ! pinger )
00327                 return -ENOMEM;
00328         ref_init ( &pinger->refcnt, NULL );
00329         intf_init ( &pinger->job, &pinger_job_desc, &pinger->refcnt );
00330         intf_init ( &pinger->xfer, &pinger_xfer_desc, &pinger->refcnt );
00331         timer_init ( &pinger->timer, pinger_expired, &pinger->refcnt );
00332         pinger->timeout = timeout;
00333         pinger->len = len;
00334         pinger->remaining = ( count ? ( count + 1 /* Initial packet */ ) : 0 );
00335         pinger->callback = callback;
00336         pinger->rc = -ETIMEDOUT;
00337 
00338         /* Open socket */
00339         if ( ( rc = xfer_open_named_socket ( &pinger->xfer, SOCK_ECHO, NULL,
00340                                              hostname, NULL ) ) != 0 ) {
00341                 DBGC ( pinger, "PINGER %p could not open socket: %s\n",
00342                        pinger, strerror ( rc ) );
00343                 goto err;
00344         }
00345 
00346         /* Attach parent interface, mortalise self, and return */
00347         intf_plug_plug ( &pinger->job, job );
00348         ref_put ( &pinger->refcnt );
00349         return 0;
00350 
00351  err:
00352         pinger_close ( pinger, rc );
00353         ref_put ( &pinger->refcnt );
00354         return rc;
00355 }