Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

pinger.c 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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. FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include <ipxe/refcnt.h>
  28. #include <ipxe/interface.h>
  29. #include <ipxe/job.h>
  30. #include <ipxe/xfer.h>
  31. #include <ipxe/iobuf.h>
  32. #include <ipxe/open.h>
  33. #include <ipxe/socket.h>
  34. #include <ipxe/retry.h>
  35. #include <ipxe/pinger.h>
  36. /** @file
  37. *
  38. * ICMP ping sender
  39. *
  40. */
  41. /* Disambiguate the various error causes */
  42. #define EPROTO_LEN __einfo_error ( EINFO_EPROTO_LEN )
  43. #define EINFO_EPROTO_LEN __einfo_uniqify ( EINFO_EPROTO, 0x01, \
  44. "Incorrect reply length" )
  45. #define EPROTO_DATA __einfo_error ( EINFO_EPROTO_DATA )
  46. #define EINFO_EPROTO_DATA __einfo_uniqify ( EINFO_EPROTO, 0x02, \
  47. "Incorrect reply data" )
  48. #define EPROTO_SEQ __einfo_error ( EINFO_EPROTO_SEQ )
  49. #define EINFO_EPROTO_SEQ __einfo_uniqify ( EINFO_EPROTO, 0x03, \
  50. "Delayed or out-of-sequence reply" )
  51. /** A pinger */
  52. struct pinger {
  53. /** Reference count */
  54. struct refcnt refcnt;
  55. /** Job control interface */
  56. struct interface job;
  57. /** Data transfer interface */
  58. struct interface xfer;
  59. /** Timer */
  60. struct retry_timer timer;
  61. /** Timeout */
  62. unsigned long timeout;
  63. /** Payload length */
  64. size_t len;
  65. /** Current sequence number */
  66. uint16_t sequence;
  67. /** Response for current sequence number is still pending */
  68. int pending;
  69. /** Number of remaining expiry events (zero to continue indefinitely) */
  70. unsigned int remaining;
  71. /** Return status */
  72. int rc;
  73. /** Callback function
  74. *
  75. * @v src Source socket address, or NULL
  76. * @v sequence Sequence number
  77. * @v len Payload length
  78. * @v rc Status code
  79. */
  80. void ( * callback ) ( struct sockaddr *src, unsigned int sequence,
  81. size_t len, int rc );
  82. };
  83. /**
  84. * Generate payload
  85. *
  86. * @v pinger Pinger
  87. * @v data Data buffer
  88. */
  89. static void pinger_generate ( struct pinger *pinger, void *data ) {
  90. uint8_t *bytes = data;
  91. unsigned int i;
  92. /* Generate byte sequence */
  93. for ( i = 0 ; i < pinger->len ; i++ )
  94. bytes[i] = ( i & 0xff );
  95. }
  96. /**
  97. * Verify payload
  98. *
  99. * @v pinger Pinger
  100. * @v data Data buffer
  101. * @ret rc Return status code
  102. */
  103. static int pinger_verify ( struct pinger *pinger, const void *data ) {
  104. const uint8_t *bytes = data;
  105. unsigned int i;
  106. /* Check byte sequence */
  107. for ( i = 0 ; i < pinger->len ; i++ ) {
  108. if ( bytes[i] != ( i & 0xff ) )
  109. return -EPROTO_DATA;
  110. }
  111. return 0;
  112. }
  113. /**
  114. * Close pinger
  115. *
  116. * @v pinger Pinger
  117. * @v rc Reason for close
  118. */
  119. static void pinger_close ( struct pinger *pinger, int rc ) {
  120. /* Stop timer */
  121. stop_timer ( &pinger->timer );
  122. /* Shut down interfaces */
  123. intf_shutdown ( &pinger->xfer, rc );
  124. intf_shutdown ( &pinger->job, rc );
  125. }
  126. /**
  127. * Handle data transfer window change
  128. *
  129. * @v pinger Pinger
  130. */
  131. static void pinger_window_changed ( struct pinger *pinger ) {
  132. /* Do nothing if timer is already running */
  133. if ( timer_running ( &pinger->timer ) )
  134. return;
  135. /* Start timer when window opens for the first time */
  136. if ( xfer_window ( &pinger->xfer ) )
  137. start_timer_nodelay ( &pinger->timer );
  138. }
  139. /**
  140. * Handle timer expiry
  141. *
  142. * @v timer Timer
  143. * @v over Failure indicator
  144. */
  145. static void pinger_expired ( struct retry_timer *timer, int over __unused ) {
  146. struct pinger *pinger = container_of ( timer, struct pinger, timer );
  147. struct xfer_metadata meta;
  148. struct io_buffer *iobuf;
  149. int rc;
  150. /* If no response has been received, notify the callback function */
  151. if ( pinger->pending && pinger->callback )
  152. pinger->callback ( NULL, pinger->sequence, 0, -ETIMEDOUT );
  153. /* Check for termination */
  154. if ( pinger->remaining && ( --pinger->remaining == 0 ) ) {
  155. pinger_close ( pinger, pinger->rc );
  156. return;
  157. }
  158. /* Increase sequence number */
  159. pinger->sequence++;
  160. /* Restart timer. Do this before attempting to transmit, in
  161. * case the transmission attempt fails.
  162. */
  163. start_timer_fixed ( &pinger->timer, pinger->timeout );
  164. pinger->pending = 1;
  165. /* Allocate I/O buffer */
  166. iobuf = xfer_alloc_iob ( &pinger->xfer, pinger->len );
  167. if ( ! iobuf ) {
  168. DBGC ( pinger, "PINGER %p could not allocate I/O buffer\n",
  169. pinger );
  170. return;
  171. }
  172. /* Generate payload */
  173. pinger_generate ( pinger, iob_put ( iobuf, pinger->len ) );
  174. /* Generate metadata */
  175. memset ( &meta, 0, sizeof ( meta ) );
  176. meta.flags = XFER_FL_ABS_OFFSET;
  177. meta.offset = pinger->sequence;
  178. /* Transmit packet */
  179. if ( ( rc = xfer_deliver ( &pinger->xfer, iobuf, &meta ) ) != 0 ) {
  180. DBGC ( pinger, "PINGER %p could not transmit: %s\n",
  181. pinger, strerror ( rc ) );
  182. return;
  183. }
  184. }
  185. /**
  186. * Handle received data
  187. *
  188. * @v pinger Pinger
  189. * @v iobuf I/O buffer
  190. * @v meta Data transfer metadata
  191. * @ret rc Return status code
  192. */
  193. static int pinger_deliver ( struct pinger *pinger, struct io_buffer *iobuf,
  194. struct xfer_metadata *meta ) {
  195. size_t len = iob_len ( iobuf );
  196. uint16_t sequence = meta->offset;
  197. int terminate = 0;
  198. int rc;
  199. /* Clear response pending flag, if applicable */
  200. if ( sequence == pinger->sequence )
  201. pinger->pending = 0;
  202. /* Check for errors */
  203. if ( len != pinger->len ) {
  204. /* Incorrect length: terminate immediately if we are
  205. * not pinging indefinitely.
  206. */
  207. DBGC ( pinger, "PINGER %p received incorrect length %zd "
  208. "(expected %zd)\n", pinger, len, pinger->len );
  209. rc = -EPROTO_LEN;
  210. terminate = ( pinger->remaining != 0 );
  211. } else if ( ( rc = pinger_verify ( pinger, iobuf->data ) ) != 0 ) {
  212. /* Incorrect data: terminate immediately if we are not
  213. * pinging indefinitely.
  214. */
  215. DBGC ( pinger, "PINGER %p received incorrect data:\n", pinger );
  216. DBGC_HDA ( pinger, 0, iobuf->data, iob_len ( iobuf ) );
  217. terminate = ( pinger->remaining != 0 );
  218. } else if ( sequence != pinger->sequence ) {
  219. /* Incorrect sequence number (probably a delayed response):
  220. * report via callback but otherwise ignore.
  221. */
  222. DBGC ( pinger, "PINGER %p received sequence %d (expected %d)\n",
  223. pinger, sequence, pinger->sequence );
  224. rc = -EPROTO_SEQ;
  225. terminate = 0;
  226. } else {
  227. /* Success: record that a packet was successfully received,
  228. * and terminate if we expect to send no further packets.
  229. */
  230. rc = 0;
  231. pinger->rc = 0;
  232. terminate = ( pinger->remaining == 1 );
  233. }
  234. /* Discard I/O buffer */
  235. free_iob ( iobuf );
  236. /* Notify callback function, if applicable */
  237. if ( pinger->callback )
  238. pinger->callback ( meta->src, sequence, len, rc );
  239. /* Terminate if applicable */
  240. if ( terminate )
  241. pinger_close ( pinger, rc );
  242. return rc;
  243. }
  244. /** Pinger data transfer interface operations */
  245. static struct interface_operation pinger_xfer_op[] = {
  246. INTF_OP ( xfer_deliver, struct pinger *, pinger_deliver ),
  247. INTF_OP ( xfer_window_changed, struct pinger *, pinger_window_changed ),
  248. INTF_OP ( intf_close, struct pinger *, pinger_close ),
  249. };
  250. /** Pinger data transfer interface descriptor */
  251. static struct interface_descriptor pinger_xfer_desc =
  252. INTF_DESC ( struct pinger, xfer, pinger_xfer_op );
  253. /** Pinger job control interface operations */
  254. static struct interface_operation pinger_job_op[] = {
  255. INTF_OP ( intf_close, struct pinger *, pinger_close ),
  256. };
  257. /** Pinger job control interface descriptor */
  258. static struct interface_descriptor pinger_job_desc =
  259. INTF_DESC ( struct pinger, job, pinger_job_op );
  260. /**
  261. * Create pinger
  262. *
  263. * @v job Job control interface
  264. * @v hostname Hostname to ping
  265. * @v timeout Timeout (in ticks)
  266. * @v len Payload length
  267. * @v count Number of packets to send (or zero for no limit)
  268. * @v callback Callback function (or NULL)
  269. * @ret rc Return status code
  270. */
  271. int create_pinger ( struct interface *job, const char *hostname,
  272. unsigned long timeout, size_t len, unsigned int count,
  273. void ( * callback ) ( struct sockaddr *src,
  274. unsigned int sequence, size_t len,
  275. int rc ) ) {
  276. struct pinger *pinger;
  277. int rc;
  278. /* Sanity check */
  279. if ( ! timeout )
  280. return -EINVAL;
  281. /* Allocate and initialise structure */
  282. pinger = zalloc ( sizeof ( *pinger ) );
  283. if ( ! pinger )
  284. return -ENOMEM;
  285. ref_init ( &pinger->refcnt, NULL );
  286. intf_init ( &pinger->job, &pinger_job_desc, &pinger->refcnt );
  287. intf_init ( &pinger->xfer, &pinger_xfer_desc, &pinger->refcnt );
  288. timer_init ( &pinger->timer, pinger_expired, &pinger->refcnt );
  289. pinger->timeout = timeout;
  290. pinger->len = len;
  291. pinger->remaining = ( count ? ( count + 1 /* Initial packet */ ) : 0 );
  292. pinger->callback = callback;
  293. pinger->rc = -ETIMEDOUT;
  294. /* Open socket */
  295. if ( ( rc = xfer_open_named_socket ( &pinger->xfer, SOCK_ECHO, NULL,
  296. hostname, NULL ) ) != 0 ) {
  297. DBGC ( pinger, "PINGER %p could not open socket: %s\n",
  298. pinger, strerror ( rc ) );
  299. goto err;
  300. }
  301. /* Attach parent interface, mortalise self, and return */
  302. intf_plug_plug ( &pinger->job, job );
  303. ref_put ( &pinger->refcnt );
  304. return 0;
  305. err:
  306. pinger_close ( pinger, rc );
  307. ref_put ( &pinger->refcnt );
  308. return rc;
  309. }