2 * Copyright (C) 2013 Michael Brown <mbrown@fensystems.co.uk>.
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.
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.
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
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.
24 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL
);
29 #include <ipxe/refcnt.h>
30 #include <ipxe/interface.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>
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" )
58 /** Reference count */
61 /** Job control interface */
63 /** Data transfer interface */
64 struct interface xfer
;
67 struct retry_timer timer
;
69 unsigned long timeout
;
73 /** Current sequence number */
75 /** Response for current sequence number is still pending */
77 /** Number of remaining expiry events (zero to continue indefinitely) */
78 unsigned int remaining
;
84 * @v src Source socket address, or NULL
85 * @v sequence Sequence number
86 * @v len Payload length
89 void ( * callback
) ( struct sockaddr
*src
, unsigned int sequence
,
99 static void pinger_generate ( struct pinger
*pinger
, void *data
) {
100 uint8_t *bytes
= data
;
103 /* Generate byte sequence */
104 for ( i
= 0 ; i
< pinger
->len
; i
++ )
105 bytes
[i
] = ( i
& 0xff );
112 * @v data Data buffer
113 * @ret rc Return status code
115 static int pinger_verify ( struct pinger
*pinger
, const void *data
) {
116 const uint8_t *bytes
= data
;
119 /* Check byte sequence */
120 for ( i
= 0 ; i
< pinger
->len
; i
++ ) {
121 if ( bytes
[i
] != ( i
& 0xff ) )
132 * @v rc Reason for close
134 static void pinger_close ( struct pinger
*pinger
, int rc
) {
137 stop_timer ( &pinger
->timer
);
139 /* Shut down interfaces */
140 intf_shutdown ( &pinger
->xfer
, rc
);
141 intf_shutdown ( &pinger
->job
, rc
);
145 * Handle data transfer window change
149 static void pinger_window_changed ( struct pinger
*pinger
) {
151 /* Do nothing if timer is already running */
152 if ( timer_running ( &pinger
->timer
) )
155 /* Start timer when window opens for the first time */
156 if ( xfer_window ( &pinger
->xfer
) )
157 start_timer_nodelay ( &pinger
->timer
);
161 * Handle timer expiry
164 * @v over Failure indicator
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
;
172 /* If no response has been received, notify the callback function */
173 if ( pinger
->pending
&& pinger
->callback
)
174 pinger
->callback ( NULL
, pinger
->sequence
, 0, -ETIMEDOUT
);
176 /* Check for termination */
177 if ( pinger
->remaining
&& ( --pinger
->remaining
== 0 ) ) {
178 pinger_close ( pinger
, pinger
->rc
);
182 /* Increase sequence number */
185 /* Restart timer. Do this before attempting to transmit, in
186 * case the transmission attempt fails.
188 start_timer_fixed ( &pinger
->timer
, pinger
->timeout
);
191 /* Allocate I/O buffer */
192 iobuf
= xfer_alloc_iob ( &pinger
->xfer
, pinger
->len
);
194 DBGC ( pinger
, "PINGER %p could not allocate I/O buffer\n",
199 /* Generate payload */
200 pinger_generate ( pinger
, iob_put ( iobuf
, pinger
->len
) );
202 /* Generate metadata */
203 memset ( &meta
, 0, sizeof ( meta
) );
204 meta
.flags
= XFER_FL_ABS_OFFSET
;
205 meta
.offset
= pinger
->sequence
;
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
) );
216 * Handle received data
219 * @v iobuf I/O buffer
220 * @v meta Data transfer metadata
221 * @ret rc Return status code
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
;
230 /* Clear response pending flag, if applicable */
231 if ( sequence
== pinger
->sequence
)
234 /* Check for errors */
235 if ( len
!= pinger
->len
) {
236 /* Incorrect length: terminate immediately if we are
237 * not pinging indefinitely.
239 DBGC ( pinger
, "PINGER %p received incorrect length %zd "
240 "(expected %zd)\n", pinger
, len
, pinger
->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.
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.
254 DBGC ( pinger
, "PINGER %p received sequence %d (expected %d)\n",
255 pinger
, sequence
, pinger
->sequence
);
259 /* Success: record that a packet was successfully received,
260 * and terminate if we expect to send no further packets.
264 terminate
= ( pinger
->remaining
== 1 );
267 /* Discard I/O buffer */
270 /* Notify callback function, if applicable */
271 if ( pinger
->callback
)
272 pinger
->callback ( meta
->src
, sequence
, len
, rc
);
274 /* Terminate if applicable */
276 pinger_close ( pinger
, rc
);
281 /** Pinger data transfer interface operations */
282 static struct interface_operation pinger_xfer_op
[] = {
283 INTF_OP ( xfer_deliver
, struct pinger
*, pinger_deliver
),
284 INTF_OP ( xfer_window_changed
, struct pinger
*, pinger_window_changed
),
285 INTF_OP ( intf_close
, struct pinger
*, pinger_close
),
288 /** Pinger data transfer interface descriptor */
289 static struct interface_descriptor pinger_xfer_desc
=
290 INTF_DESC ( struct pinger
, xfer
, pinger_xfer_op
);
292 /** Pinger job control interface operations */
293 static struct interface_operation pinger_job_op
[] = {
294 INTF_OP ( intf_close
, struct pinger
*, pinger_close
),
297 /** Pinger job control interface descriptor */
298 static struct interface_descriptor pinger_job_desc
=
299 INTF_DESC ( struct pinger
, job
, pinger_job_op
);
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
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
,
317 struct pinger
*pinger
;
324 /* Allocate and initialise structure */
325 pinger
= zalloc ( sizeof ( *pinger
) );
328 ref_init ( &pinger
->refcnt
, NULL
);
329 intf_init ( &pinger
->job
, &pinger_job_desc
, &pinger
->refcnt
);
330 intf_init ( &pinger
->xfer
, &pinger_xfer_desc
, &pinger
->refcnt
);
331 timer_init ( &pinger
->timer
, pinger_expired
, &pinger
->refcnt
);
332 pinger
->timeout
= timeout
;
334 pinger
->remaining
= ( count ?
( count
+ 1 /* Initial packet */ ) : 0 );
335 pinger
->callback
= callback
;
336 pinger
->rc
= -ETIMEDOUT
;
339 if ( ( rc
= xfer_open_named_socket ( &pinger
->xfer
, SOCK_ECHO
, NULL
,
340 hostname
, NULL
) ) != 0 ) {
341 DBGC ( pinger
, "PINGER %p could not open socket: %s\n",
342 pinger
, strerror ( rc
) );
346 /* Attach parent interface, mortalise self, and return */
347 intf_plug_plug ( &pinger
->job
, job
);
348 ref_put ( &pinger
->refcnt
);
352 pinger_close ( pinger
, rc
);
353 ref_put ( &pinger
->refcnt
);