iPXE
pinger.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 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 <stdlib.h>
27 #include <string.h>
28 #include <errno.h>
29 #include <ipxe/refcnt.h>
30 #include <ipxe/interface.h>
31 #include <ipxe/job.h>
32 #include <ipxe/xfer.h>
33 #include <ipxe/iobuf.h>
34 #include <ipxe/open.h>
35 #include <ipxe/socket.h>
36 #include <ipxe/retry.h>
37 #include <ipxe/pinger.h>
38 
39 /** @file
40  *
41  * ICMP ping sender
42  *
43  */
44 
45 /* Disambiguate the various error causes */
46 #define EPROTO_LEN __einfo_error ( EINFO_EPROTO_LEN )
47 #define EINFO_EPROTO_LEN __einfo_uniqify ( EINFO_EPROTO, 0x01, \
48  "Incorrect reply length" )
49 #define EPROTO_DATA __einfo_error ( EINFO_EPROTO_DATA )
50 #define EINFO_EPROTO_DATA __einfo_uniqify ( EINFO_EPROTO, 0x02, \
51  "Incorrect reply data" )
52 #define EPROTO_SEQ __einfo_error ( EINFO_EPROTO_SEQ )
53 #define EINFO_EPROTO_SEQ __einfo_uniqify ( EINFO_EPROTO, 0x03, \
54  "Delayed or out-of-sequence reply" )
55 
56 /** A pinger */
57 struct pinger {
58  /** Reference count */
59  struct refcnt refcnt;
60 
61  /** Job control interface */
62  struct interface job;
63  /** Data transfer interface */
64  struct interface xfer;
65 
66  /** Timer */
68  /** Timeout */
69  unsigned long timeout;
70 
71  /** Payload length */
72  size_t len;
73  /** Current sequence number */
75  /** Response for current sequence number is still pending */
76  int pending;
77  /** Number of remaining expiry events (zero to continue indefinitely) */
78  unsigned int remaining;
79  /** Return status */
80  int rc;
81 
82  /** Callback function
83  *
84  * @v src Source socket address, or NULL
85  * @v sequence Sequence number
86  * @v len Payload length
87  * @v rc Status code
88  */
89  void ( * callback ) ( struct sockaddr *src, unsigned int sequence,
90  size_t len, int rc );
91 };
92 
93 /**
94  * Generate payload
95  *
96  * @v pinger Pinger
97  * @v data Data buffer
98  */
99 static void pinger_generate ( struct pinger *pinger, void *data ) {
100  uint8_t *bytes = data;
101  unsigned int i;
102 
103  /* Generate byte sequence */
104  for ( i = 0 ; i < pinger->len ; i++ )
105  bytes[i] = ( i & 0xff );
106 }
107 
108 /**
109  * Verify payload
110  *
111  * @v pinger Pinger
112  * @v data Data buffer
113  * @ret rc Return status code
114  */
115 static int pinger_verify ( struct pinger *pinger, const void *data ) {
116  const uint8_t *bytes = data;
117  unsigned int i;
118 
119  /* Check byte sequence */
120  for ( i = 0 ; i < pinger->len ; i++ ) {
121  if ( bytes[i] != ( i & 0xff ) )
122  return -EPROTO_DATA;
123  }
124 
125  return 0;
126 }
127 
128 /**
129  * Close pinger
130  *
131  * @v pinger Pinger
132  * @v rc Reason for close
133  */
134 static void pinger_close ( struct pinger *pinger, int rc ) {
135 
136  /* Stop timer */
137  stop_timer ( &pinger->timer );
138 
139  /* Shut down interfaces */
140  intf_shutdown ( &pinger->xfer, rc );
141  intf_shutdown ( &pinger->job, rc );
142 }
143 
144 /**
145  * Handle data transfer window change
146  *
147  * @v pinger Pinger
148  */
149 static void pinger_window_changed ( struct pinger *pinger ) {
150 
151  /* Do nothing if timer is already running */
152  if ( timer_running ( &pinger->timer ) )
153  return;
154 
155  /* Start timer when window opens for the first time */
156  if ( xfer_window ( &pinger->xfer ) )
158 }
159 
160 /**
161  * Handle timer expiry
162  *
163  * @v timer Timer
164  * @v over Failure indicator
165  */
166 static void pinger_expired ( struct retry_timer *timer, int over __unused ) {
167  struct pinger *pinger = container_of ( timer, struct pinger, timer );
168  struct xfer_metadata meta;
169  struct io_buffer *iobuf;
170  int rc;
171 
172  /* If no response has been received, notify the callback function */
173  if ( pinger->pending && pinger->callback )
175 
176  /* Check for termination */
177  if ( pinger->remaining && ( --pinger->remaining == 0 ) ) {
179  return;
180  }
181 
182  /* Increase sequence number */
183  pinger->sequence++;
184 
185  /* Restart timer. Do this before attempting to transmit, in
186  * case the transmission attempt fails.
187  */
189  pinger->pending = 1;
190 
191  /* Allocate I/O buffer */
192  iobuf = xfer_alloc_iob ( &pinger->xfer, pinger->len );
193  if ( ! iobuf ) {
194  DBGC ( pinger, "PINGER %p could not allocate I/O buffer\n",
195  pinger );
196  return;
197  }
198 
199  /* Generate payload */
200  pinger_generate ( pinger, iob_put ( iobuf, pinger->len ) );
201 
202  /* Generate metadata */
203  memset ( &meta, 0, sizeof ( meta ) );
204  meta.flags = XFER_FL_ABS_OFFSET;
205  meta.offset = pinger->sequence;
206 
207  /* Transmit packet */
208  if ( ( rc = xfer_deliver ( &pinger->xfer, iobuf, &meta ) ) != 0 ) {
209  DBGC ( pinger, "PINGER %p could not transmit: %s\n",
210  pinger, strerror ( rc ) );
211  return;
212  }
213 }
214 
215 /**
216  * Handle received data
217  *
218  * @v pinger Pinger
219  * @v iobuf I/O buffer
220  * @v meta Data transfer metadata
221  * @ret rc Return status code
222  */
223 static int pinger_deliver ( struct pinger *pinger, struct io_buffer *iobuf,
224  struct xfer_metadata *meta ) {
225  size_t len = iob_len ( iobuf );
226  uint16_t sequence = meta->offset;
227  int terminate = 0;
228  int rc;
229 
230  /* Clear response pending flag, if applicable */
231  if ( sequence == pinger->sequence )
232  pinger->pending = 0;
233 
234  /* Check for errors */
235  if ( len != pinger->len ) {
236  /* Incorrect length: terminate immediately if we are
237  * not pinging indefinitely.
238  */
239  DBGC ( pinger, "PINGER %p received incorrect length %zd "
240  "(expected %zd)\n", pinger, len, pinger->len );
241  rc = -EPROTO_LEN;
242  terminate = ( pinger->remaining != 0 );
243  } else if ( ( rc = pinger_verify ( pinger, iobuf->data ) ) != 0 ) {
244  /* Incorrect data: terminate immediately if we are not
245  * pinging indefinitely.
246  */
247  DBGC ( pinger, "PINGER %p received incorrect data:\n", pinger );
248  DBGC_HDA ( pinger, 0, iobuf->data, iob_len ( iobuf ) );
249  terminate = ( pinger->remaining != 0 );
250  } else if ( sequence != pinger->sequence ) {
251  /* Incorrect sequence number (probably a delayed response):
252  * report via callback but otherwise ignore.
253  */
254  DBGC ( pinger, "PINGER %p received sequence %d (expected %d)\n",
255  pinger, sequence, pinger->sequence );
256  rc = -EPROTO_SEQ;
257  terminate = 0;
258  } else {
259  /* Success: record that a packet was successfully received,
260  * and terminate if we expect to send no further packets.
261  */
262  rc = 0;
263  pinger->rc = 0;
264  terminate = ( pinger->remaining == 1 );
265  }
266 
267  /* Discard I/O buffer */
268  free_iob ( iobuf );
269 
270  /* Notify callback function, if applicable */
271  if ( pinger->callback )
272  pinger->callback ( meta->src, sequence, len, rc );
273 
274  /* Terminate if applicable */
275  if ( terminate )
276  pinger_close ( pinger, rc );
277 
278  return rc;
279 }
280 
281 /** Pinger data transfer interface operations */
283  INTF_OP ( xfer_deliver, struct pinger *, pinger_deliver ),
285  INTF_OP ( intf_close, struct pinger *, pinger_close ),
286 };
287 
288 /** Pinger data transfer interface descriptor */
290  INTF_DESC ( struct pinger, xfer, pinger_xfer_op );
291 
292 /** Pinger job control interface operations */
294  INTF_OP ( intf_close, struct pinger *, pinger_close ),
295 };
296 
297 /** Pinger job control interface descriptor */
299  INTF_DESC ( struct pinger, job, pinger_job_op );
300 
301 /**
302  * Create pinger
303  *
304  * @v job Job control interface
305  * @v hostname Hostname to ping
306  * @v timeout Timeout (in ticks)
307  * @v len Payload length
308  * @v count Number of packets to send (or zero for no limit)
309  * @v callback Callback function (or NULL)
310  * @ret rc Return status code
311  */
312 int create_pinger ( struct interface *job, const char *hostname,
313  unsigned long timeout, size_t len, unsigned int count,
314  void ( * callback ) ( struct sockaddr *src,
315  unsigned int sequence, size_t len,
316  int rc ) ) {
317  struct pinger *pinger;
318  int rc;
319 
320  /* Sanity check */
321  if ( ! timeout )
322  return -EINVAL;
323 
324  /* Allocate and initialise structure */
325  pinger = zalloc ( sizeof ( *pinger ) );
326  if ( ! pinger )
327  return -ENOMEM;
328  ref_init ( &pinger->refcnt, NULL );
331  timer_init ( &pinger->timer, pinger_expired, &pinger->refcnt );
333  pinger->len = len;
334  pinger->remaining = ( count ? ( count + 1 /* Initial packet */ ) : 0 );
336  pinger->rc = -ETIMEDOUT;
337 
338  /* Open socket */
340  hostname, NULL ) ) != 0 ) {
341  DBGC ( pinger, "PINGER %p could not open socket: %s\n",
342  pinger, strerror ( rc ) );
343  goto err;
344  }
345 
346  /* Attach parent interface, mortalise self, and return */
347  intf_plug_plug ( &pinger->job, job );
348  ref_put ( &pinger->refcnt );
349  return 0;
350 
351  err:
352  pinger_close ( pinger, rc );
353  ref_put ( &pinger->refcnt );
354  return rc;
355 }
#define EINVAL
Invalid argument.
Definition: errno.h:428
An object interface operation.
Definition: interface.h:17
static const void * src
Definition: string.h:47
struct arbelprm_rc_send_wqe rc
Definition: arbel.h:14
void xfer_window_changed(struct interface *intf)
Report change of flow control window.
Definition: xfer.c:146
unsigned short uint16_t
Definition: stdint.h:11
void intf_close(struct interface *intf, int rc)
Close an object interface.
Definition: interface.c:249
#define iob_put(iobuf, len)
Definition: iobuf.h:120
Data transfer metadata.
Definition: xfer.h:22
void intf_shutdown(struct interface *intf, int rc)
Shut down an object interface.
Definition: interface.c:278
struct refcnt refcnt
Reference count.
Definition: pinger.c:59
static void start_timer_nodelay(struct retry_timer *timer)
Start timer with no delay.
Definition: retry.h:99
struct interface xfer
Data transfer interface.
Definition: pinger.c:64
#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.
static int pinger_deliver(struct pinger *pinger, struct io_buffer *iobuf, struct xfer_metadata *meta)
Handle received data.
Definition: pinger.c:223
size_t len
Payload length.
Definition: pinger.c:72
I/O buffers.
void free_iob(struct io_buffer *iobuf)
Free I/O buffer.
Definition: iobuf.c:146
static void pinger_window_changed(struct pinger *pinger)
Handle data transfer window change.
Definition: pinger.c:149
Retry timers.
#define EPROTO_DATA
Definition: pinger.c:49
#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:107
void(* callback)(struct sockaddr *src, unsigned int sequence, size_t len, int rc)
Callback function.
Definition: pinger.c:89
struct io_buffer * xfer_alloc_iob(struct interface *intf, size_t len)
Allocate I/O buffer.
Definition: xfer.c:158
#define EPROTO_SEQ
Definition: pinger.c:52
int create_pinger(struct interface *job, const char *hostname, unsigned long timeout, size_t len, unsigned int count, void(*callback)(struct sockaddr *src, unsigned int sequence, size_t len, int rc))
Create pinger.
Definition: pinger.c:312
size_t xfer_window(struct interface *intf)
Check flow control window.
Definition: xfer.c:116
uint16_t sequence
Current sequence number.
Definition: pinger.c:74
static void pinger_expired(struct retry_timer *timer, int over __unused)
Handle timer expiry.
Definition: pinger.c:166
Data transfer interfaces.
A reference counter.
Definition: refcnt.h:26
A timer.
Definition: timer.h:28
int rc
Return status.
Definition: pinger.c:80
#define ENOMEM
Not enough space.
Definition: errno.h:534
static int pinger_verify(struct pinger *pinger, const void *data)
Verify payload.
Definition: pinger.c:115
#define __unused
Declare a variable or data structure as unused.
Definition: compiler.h:573
static struct interface_operation pinger_job_op[]
Pinger job control interface operations.
Definition: pinger.c:293
#define container_of(ptr, type, field)
Get containing structure.
Definition: stddef.h:35
An object interface.
Definition: interface.h:124
#define SOCK_ECHO
Definition: socket.h:34
struct interface job
Job control interface.
Definition: pinger.c:62
#define DBGC_HDA(...)
Definition: compiler.h:506
Object interfaces.
ICMP ping sender.
uint16_t count
Number of entries.
Definition: ena.h:22
struct retry_timer timer
Timer.
Definition: pinger.c:67
#define EPROTO_LEN
Definition: pinger.c:46
static void pinger_close(struct pinger *pinger, int rc)
Close pinger.
Definition: pinger.c:134
int meta(WINDOW *, bool)
Generalized socket address structure.
Definition: socket.h:96
An object interface descriptor.
Definition: interface.h:55
FILE_LICENCE(GPL2_OR_LATER_OR_UBDL)
char * strerror(int errno)
Retrieve string representation of error number.
Definition: strerror.c:78
void * zalloc(size_t size)
Allocate cleared memory.
Definition: malloc.c:624
static size_t iob_len(struct io_buffer *iobuf)
Calculate length of data in an I/O buffer.
Definition: iobuf.h:155
static struct interface_operation pinger_xfer_op[]
Pinger data transfer interface operations.
Definition: pinger.c:282
#define INTF_OP(op_type, object_type, op_func)
Define an object interface operation.
Definition: interface.h:32
int xfer_deliver(struct interface *intf, struct io_buffer *iobuf, struct xfer_metadata *meta)
Deliver datagram.
Definition: xfer.c:194
unsigned char uint8_t
Definition: stdint.h:10
Data transfer interface opening.
Job control interfaces.
unsigned long timeout
Timeout.
Definition: pinger.c:69
void start_timer_fixed(struct retry_timer *timer, unsigned long timeout)
Start timer with a specified timeout.
Definition: retry.c:64
#define INTF_DESC(object_type, intf, operations)
Define an object interface descriptor.
Definition: interface.h:80
void stop_timer(struct retry_timer *timer)
Stop timer.
Definition: retry.c:117
static void pinger_generate(struct pinger *pinger, void *data)
Generate payload.
Definition: pinger.c:99
void * data
Start of data.
Definition: iobuf.h:48
Reference counting.
uint8_t data[48]
Additional event data.
Definition: ena.h:22
void timeout(int)
static struct interface_descriptor pinger_xfer_desc
Pinger data transfer interface descriptor.
Definition: pinger.c:289
static void intf_init(struct interface *intf, struct interface_descriptor *desc, struct refcnt *refcnt)
Initialise an object interface.
Definition: interface.h:203
Socket addresses.
uint8_t bytes[64]
Definition: ib_mad.h:16
static struct interface_descriptor pinger_job_desc
Pinger job control interface descriptor.
Definition: pinger.c:298
unsigned int remaining
Number of remaining expiry events (zero to continue indefinitely)
Definition: pinger.c:78
uint32_t len
Length.
Definition: ena.h:14
int pending
Response for current sequence number is still pending.
Definition: pinger.c:76
#define NULL
NULL pointer (VOID *)
Definition: Base.h:321
#define ETIMEDOUT
Connection timed out.
Definition: errno.h:669
String functions.
A pinger.
Definition: pinger.c:57
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
void * memset(void *dest, int character, size_t len) __nonnull
A persistent I/O buffer.
Definition: iobuf.h:33