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.

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