You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

lotest.c 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. /*
  2. * Copyright (C) 2010 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., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stdint.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <byteswap.h>
  25. #include <ipxe/iobuf.h>
  26. #include <ipxe/netdevice.h>
  27. #include <ipxe/if_ether.h>
  28. #include <ipxe/keys.h>
  29. #include <ipxe/console.h>
  30. #include <usr/ifmgmt.h>
  31. #include <usr/lotest.h>
  32. /** @file
  33. *
  34. * Loopback testing
  35. *
  36. */
  37. #define LINK_WAIT_MS 15000
  38. /**
  39. * Process received packet
  40. *
  41. * @v iobuf I/O buffer
  42. * @v netdev Network device
  43. * @v ll_dest Link-layer destination address
  44. * @v ll_source Link-layer source address
  45. * @v flags Packet flags
  46. * @ret rc Return status code
  47. */
  48. static int lotest_rx ( struct io_buffer *iobuf,
  49. struct net_device *netdev __unused,
  50. const void *ll_dest __unused,
  51. const void *ll_source __unused,
  52. unsigned int flags __unused ) {
  53. free_iob ( iobuf );
  54. return -ENOTSUP;
  55. }
  56. /**
  57. * Transcribe network-layer address
  58. *
  59. * @v net_addr Network-layer address
  60. * @ret string Human-readable transcription of address
  61. */
  62. static const char * lotest_ntoa ( const void *net_addr __unused ) {
  63. return "<INVALID>";
  64. }
  65. /**
  66. * Loopback test network-layer protocol
  67. *
  68. * Using a dedicated network-layer protocol avoids problems caused by
  69. * cards supporting features such as IPv4 checksum offload trying to
  70. * interpret the (randomly generated) network-layer content.
  71. */
  72. static struct net_protocol lotest_protocol __net_protocol = {
  73. .name = "LOTEST",
  74. .rx = lotest_rx,
  75. .ntoa = lotest_ntoa,
  76. .net_proto = htons ( 0x6950 ), /* Not a genuine protocol number */
  77. .net_addr_len = 0,
  78. };
  79. /**
  80. * Wait for packet to be received
  81. *
  82. * @v receiver Receiving network device*
  83. * @v data Expected data
  84. * @v len Expected data length
  85. * @ret rc Return status code
  86. */
  87. static int loopback_wait ( struct net_device *receiver, void *data,
  88. size_t len ) {
  89. struct ll_protocol *ll_protocol = receiver->ll_protocol;
  90. struct io_buffer *iobuf;
  91. const void *ll_dest;
  92. const void *ll_source;
  93. uint16_t net_proto;
  94. unsigned int flags;
  95. int rc;
  96. /* Poll until packet arrives */
  97. while ( 1 ) {
  98. /* Check for cancellation */
  99. if ( iskey() && ( getchar() == CTRL_C ) )
  100. return -ECANCELED;
  101. /* Poll network devices */
  102. net_poll();
  103. /* Dequeue packet, if available */
  104. iobuf = netdev_rx_dequeue ( receiver );
  105. if ( ! iobuf )
  106. continue;
  107. /* Strip link-layer header */
  108. if ( ( rc = ll_protocol->pull ( receiver, iobuf, &ll_dest,
  109. &ll_source, &net_proto,
  110. &flags ) ) != 0 ) {
  111. printf ( "\nFailed to strip link-layer header: %s",
  112. strerror ( rc ) );
  113. free_iob ( iob_disown ( iobuf ) );
  114. return rc;
  115. }
  116. /* Ignore non-loopback packets */
  117. if ( net_proto != lotest_protocol.net_proto ) {
  118. printf ( "\nReceived spurious packet type %04x\n",
  119. ntohs ( net_proto ) );
  120. free_iob ( iob_disown ( iobuf ) );
  121. continue;
  122. }
  123. /* Check packet length */
  124. if ( iob_len ( iobuf ) != len ) {
  125. printf ( "\nLength mismatch: sent %zd, received %zd",
  126. len, iob_len ( iobuf ) );
  127. DBG ( "\nSent:\n" );
  128. DBG_HDA ( 0, data, len );
  129. DBG ( "Received:\n" );
  130. DBG_HDA ( 0, iobuf->data, iob_len ( iobuf ) );
  131. free_iob ( iob_disown ( iobuf ) );
  132. return -EINVAL;
  133. }
  134. /* Check packet content */
  135. if ( memcmp ( iobuf->data, data, len ) != 0 ) {
  136. printf ( "\nContent mismatch" );
  137. DBG ( "\nSent:\n" );
  138. DBG_HDA ( 0, data, len );
  139. DBG ( "Received:\n" );
  140. DBG_HDA ( 0, iobuf->data, iob_len ( iobuf ) );
  141. free_iob ( iob_disown ( iobuf ) );
  142. return -EINVAL;
  143. }
  144. /* Discard packet and return */
  145. free_iob ( iob_disown ( iobuf ) );
  146. return 0;
  147. }
  148. }
  149. /**
  150. * Perform loopback test between two network devices
  151. *
  152. * @v sender Sending network device
  153. * @v receiver Received network device
  154. * @v mtu Packet size (excluding link-layer headers)
  155. * @ret rc Return status code
  156. */
  157. int loopback_test ( struct net_device *sender, struct net_device *receiver,
  158. size_t mtu ) {
  159. uint8_t buf[mtu];
  160. struct io_buffer *iobuf;
  161. unsigned int i;
  162. unsigned int successes;
  163. int rc;
  164. /* Open network devices */
  165. if ( ( rc = ifopen ( sender ) ) != 0 )
  166. return rc;
  167. if ( ( rc = ifopen ( receiver ) ) != 0 )
  168. return rc;
  169. /* Wait for link-up */
  170. if ( ( rc = iflinkwait ( sender, LINK_WAIT_MS ) ) != 0 )
  171. return rc;
  172. if ( ( rc = iflinkwait ( receiver, LINK_WAIT_MS ) ) != 0 )
  173. return rc;
  174. /* Print initial statistics */
  175. printf ( "Performing loopback test from %s to %s with %zd byte MTU\n",
  176. sender->name, receiver->name, mtu );
  177. ifstat ( sender );
  178. ifstat ( receiver );
  179. /* Freeze receive queue processing on the receiver, so that we
  180. * can extract all received packets.
  181. */
  182. netdev_rx_freeze ( receiver );
  183. /* Perform loopback test */
  184. for ( successes = 0 ; ; successes++ ) {
  185. /* Print running total */
  186. printf ( "\r%d", successes );
  187. /* Generate random packet */
  188. for ( i = 0 ; i < sizeof ( buf ) ; i++ )
  189. buf[i] = random();
  190. iobuf = alloc_iob ( MAX_LL_HEADER_LEN + sizeof ( buf ) );
  191. if ( ! iobuf ) {
  192. printf ( "\nFailed to allocate I/O buffer" );
  193. rc = -ENOMEM;
  194. break;
  195. }
  196. iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
  197. memcpy ( iob_put ( iobuf, sizeof ( buf ) ),
  198. buf, sizeof ( buf ) );
  199. /* Transmit packet */
  200. if ( ( rc = net_tx ( iob_disown ( iobuf ), sender,
  201. &lotest_protocol, receiver->ll_addr,
  202. sender->ll_addr ) ) != 0 ) {
  203. printf ( "\nFailed to transmit packet: %s",
  204. strerror ( rc ) );
  205. break;
  206. }
  207. /* Wait for received packet */
  208. if ( ( rc = loopback_wait ( receiver, buf,
  209. sizeof ( buf ) ) ) != 0 ) {
  210. break;
  211. }
  212. }
  213. printf ( "\n");
  214. netdev_rx_unfreeze ( receiver );
  215. /* Dump final statistics */
  216. ifstat ( sender );
  217. ifstat ( receiver );
  218. return 0;
  219. }