Skip to content

Commit

Permalink
[ping] Allow termination after a specified number of packets
Browse files Browse the repository at this point in the history
Add the "-c <count>" option to the "ping" command, allowing for
automatic termination after a specified number of packets.

When a number of packets is specified:

  - if a serious error (i.e. length mismatch or content mismatch)
    occurs, then the ping will be immediately terminated with the relevant
    status code;

  - if at least one response is received successfully, and all errors
    are non-serious (i.e. timeouts or out-of-sequence responses), then
    the ping will be terminated after the final response (or timeout)
    with a success status;

  - if no responses are received successfully, then the ping will be
    terminated after the final timeout with ETIMEDOUT.

If no number of packets is specified, then the ping will continue
until manually interrupted.

Originally-implemented-by: Cedric Levasseur <cyr-ius@ipocus.net>
Signed-off-by: Michael Brown <mcb30@ipxe.org>
  • Loading branch information
mcb30 committed Oct 23, 2014
1 parent d1afe73 commit 1c34ca7
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 7 deletions.
39 changes: 37 additions & 2 deletions src/core/pinger.c
Expand Up @@ -70,6 +70,10 @@ struct pinger {
uint16_t sequence;
/** Response for current sequence number is still pending */
int pending;
/** Number of remaining expiry events (zero to continue indefinitely) */
unsigned int remaining;
/** Return status */
int rc;

/** Callback function
*
Expand Down Expand Up @@ -164,7 +168,12 @@ static void pinger_expired ( struct retry_timer *timer, int over __unused ) {
/* If no response has been received, notify the callback function */
if ( pinger->pending )
pinger->callback ( NULL, pinger->sequence, 0, -ETIMEDOUT );
pinger->pending = 1;

/* Check for termination */
if ( pinger->remaining && ( --pinger->remaining == 0 ) ) {
pinger_close ( pinger, pinger->rc );
return;
}

/* Increase sequence number */
pinger->sequence++;
Expand All @@ -173,6 +182,7 @@ static void pinger_expired ( struct retry_timer *timer, int over __unused ) {
* case the transmission attempt fails.
*/
start_timer_fixed ( &pinger->timer, pinger->timeout );
pinger->pending = 1;

/* Allocate I/O buffer */
iobuf = xfer_alloc_iob ( &pinger->xfer, pinger->len );
Expand Down Expand Up @@ -210,6 +220,7 @@ static int pinger_deliver ( struct pinger *pinger, struct io_buffer *iobuf,
struct xfer_metadata *meta ) {
size_t len = iob_len ( iobuf );
uint16_t sequence = meta->offset;
int terminate = 0;
int rc;

/* Clear response pending flag, if applicable */
Expand All @@ -218,18 +229,35 @@ static int pinger_deliver ( struct pinger *pinger, struct io_buffer *iobuf,

/* Check for errors */
if ( len != pinger->len ) {
/* Incorrect length: terminate immediately if we are
* not pinging indefinitely.
*/
DBGC ( pinger, "PINGER %p received incorrect length %zd "
"(expected %zd)\n", pinger, len, pinger->len );
rc = -EPROTO_LEN;
terminate = ( pinger->remaining != 0 );
} else if ( ( rc = pinger_verify ( pinger, iobuf->data ) ) != 0 ) {
/* Incorrect data: terminate immediately if we are not
* pinging indefinitely.
*/
DBGC ( pinger, "PINGER %p received incorrect data:\n", pinger );
DBGC_HDA ( pinger, 0, iobuf->data, iob_len ( iobuf ) );
terminate = ( pinger->remaining != 0 );
} else if ( sequence != pinger->sequence ) {
/* Incorrect sequence number (probably a delayed response):
* report via callback but otherwise ignore.
*/
DBGC ( pinger, "PINGER %p received sequence %d (expected %d)\n",
pinger, sequence, pinger->sequence );
rc = -EPROTO_SEQ;
terminate = 0;
} else {
/* Success: record that a packet was successfully received,
* and terminate if we expect to send no further packets.
*/
rc = 0;
pinger->rc = 0;
terminate = ( pinger->remaining == 1 );
}

/* Discard I/O buffer */
Expand All @@ -238,6 +266,10 @@ static int pinger_deliver ( struct pinger *pinger, struct io_buffer *iobuf,
/* Notify callback function */
pinger->callback ( meta->src, sequence, len, rc );

/* Terminate if applicable */
if ( terminate )
pinger_close ( pinger, rc );

return rc;
}

Expand Down Expand Up @@ -268,10 +300,11 @@ static struct interface_descriptor pinger_job_desc =
* @v hostname Hostname to ping
* @v timeout Timeout (in ticks)
* @v len Payload length
* @v count Number of packets to send (or zero for no limit)
* @ret rc Return status code
*/
int create_pinger ( struct interface *job, const char *hostname,
unsigned long timeout, size_t len,
unsigned long timeout, size_t len, unsigned int count,
void ( * callback ) ( struct sockaddr *src,
unsigned int sequence, size_t len,
int rc ) ) {
Expand All @@ -292,7 +325,9 @@ int create_pinger ( struct interface *job, const char *hostname,
timer_init ( &pinger->timer, pinger_expired, &pinger->refcnt );
pinger->timeout = timeout;
pinger->len = len;
pinger->remaining = ( count ? ( count + 1 /* Initial packet */ ) : 0 );
pinger->callback = callback;
pinger->rc = -ETIMEDOUT;

/* Open socket */
if ( ( rc = xfer_open_named_socket ( &pinger->xfer, SOCK_ECHO, NULL,
Expand Down
7 changes: 6 additions & 1 deletion src/hci/commands/ping_cmd.c
Expand Up @@ -48,6 +48,8 @@ struct ping_options {
unsigned int size;
/** Timeout (in ms) */
unsigned long timeout;
/** Number of packets to send (or zero for no limit) */
unsigned int count;
};

/** "ping" option list */
Expand All @@ -56,6 +58,8 @@ static struct option_descriptor ping_opts[] = {
struct ping_options, size, parse_integer ),
OPTION_DESC ( "timeout", 't', required_argument,
struct ping_options, timeout, parse_timeout ),
OPTION_DESC ( "count", 'c', required_argument,
struct ping_options, count, parse_integer ),
};

/** "ping" command descriptor */
Expand Down Expand Up @@ -87,7 +91,8 @@ static int ping_exec ( int argc, char **argv ) {
hostname = argv[optind];

/* Ping */
if ( ( rc = ping ( hostname, opts.timeout, opts.size ) ) != 0 )
if ( ( rc = ping ( hostname, opts.timeout, opts.size,
opts.count ) ) != 0 )
return rc;

return 0;
Expand Down
1 change: 1 addition & 0 deletions src/include/ipxe/pinger.h
Expand Up @@ -15,6 +15,7 @@ FILE_LICENCE ( GPL2_OR_LATER );

extern int create_pinger ( struct interface *job, const char *hostname,
unsigned long timeout, size_t len,
unsigned int count,
void ( * callback ) ( struct sockaddr *peer,
unsigned int sequence,
size_t len,
Expand Down
3 changes: 2 additions & 1 deletion src/include/usr/pingmgmt.h
Expand Up @@ -11,6 +11,7 @@ FILE_LICENCE ( GPL2_OR_LATER );

#include <stdint.h>

extern int ping ( const char *hostname, unsigned long timeout, size_t len );
extern int ping ( const char *hostname, unsigned long timeout, size_t len,
unsigned int count );

#endif /* _USR_PINGMGMT_H */
8 changes: 5 additions & 3 deletions src/usr/pingmgmt.c
Expand Up @@ -58,14 +58,16 @@ static void ping_callback ( struct sockaddr *peer, unsigned int sequence,
* @v hostname Hostname
* @v timeout Timeout between pings, in ticks
* @v len Payload length
* @v count Number of packets to send (or zero for no limit)
* @ret rc Return status code
*/
int ping ( const char *hostname, unsigned long timeout, size_t len ) {
int ping ( const char *hostname, unsigned long timeout, size_t len,
unsigned int count ) {
int rc;

/* Create pinger */
if ( ( rc = create_pinger ( &monojob, hostname, timeout,
len, ping_callback ) ) != 0 ) {
if ( ( rc = create_pinger ( &monojob, hostname, timeout, len,
count, ping_callback ) ) != 0 ) {
printf ( "Could not start ping: %s\n", strerror ( rc ) );
return rc;
}
Expand Down

0 comments on commit 1c34ca7

Please sign in to comment.