2 * Copyright (C) 2010 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
20 FILE_LICENCE ( GPL2_OR_LATER );
28 #include <ipxe/iobuf.h>
29 #include <ipxe/netdevice.h>
30 #include <ipxe/if_ether.h>
31 #include <ipxe/keys.h>
32 #include <ipxe/console.h>
33 #include <usr/ifmgmt.h>
34 #include <usr/lotest.h>
42 /** Current loopback test receiver */
43 static struct net_device *lotest_receiver;
45 /** Loopback testing received packets */
46 static LIST_HEAD ( lotest_queue );
49 * Process received packet
52 * @v netdev Network device
53 * @v ll_dest Link-layer destination address
54 * @v ll_source Link-layer source address
55 * @v flags Packet flags
56 * @ret rc Return status code
58 static int lotest_rx ( struct io_buffer *iobuf,
59 struct net_device *netdev,
60 const void *ll_dest __unused,
61 const void *ll_source __unused,
62 unsigned int flags __unused ) {
64 /* Add to received packet queue if currently performing a test */
65 if ( netdev == lotest_receiver ) {
66 list_add_tail ( &iobuf->list, &lotest_queue );
75 * Dequeue received packet
77 * @ret iobuf I/O buffer, or NULL
79 static struct io_buffer * lotest_dequeue ( void ) {
80 struct io_buffer *iobuf;
82 /* Remove first packet (if any) from received packet queue */
83 iobuf = list_first_entry ( &lotest_queue, struct io_buffer, list );
86 list_del ( &iobuf->list );
92 * Transcribe network-layer address
94 * @v net_addr Network-layer address
95 * @ret string Human-readable transcription of address
97 static const char * lotest_ntoa ( const void *net_addr __unused ) {
102 * Loopback test network-layer protocol
104 * Using a dedicated network-layer protocol avoids problems caused by
105 * cards supporting features such as IPv4 checksum offload trying to
106 * interpret the (randomly generated) network-layer content.
108 static struct net_protocol lotest_protocol __net_protocol = {
112 .net_proto = htons ( 0x6950 ), /* Not a genuine protocol number */
117 * Discard all received loopback test packets
120 static void lotest_flush ( void ) {
121 struct io_buffer *iobuf;
123 while ( ( iobuf = lotest_dequeue() ) != NULL )
128 * Wait for packet to be received
130 * @v data Expected data
131 * @v len Expected data length
132 * @ret rc Return status code
134 static int loopback_wait ( void *data, size_t len ) {
135 struct io_buffer *iobuf;
137 /* Poll until packet arrives */
140 /* Check for cancellation */
141 if ( iskey() && ( getchar() == CTRL_C ) )
144 /* Poll network devices */
147 /* Dequeue packet, if available */
148 iobuf = lotest_dequeue();
152 /* Check packet length */
153 if ( iob_len ( iobuf ) != len ) {
154 printf ( "\nLength mismatch: sent %zd, received %zd",
155 len, iob_len ( iobuf ) );
157 DBG_HDA ( 0, data, len );
158 DBG ( "Received:\n" );
159 DBG_HDA ( 0, iobuf->data, iob_len ( iobuf ) );
160 free_iob ( iob_disown ( iobuf ) );
164 /* Check packet content */
165 if ( memcmp ( iobuf->data, data, len ) != 0 ) {
166 printf ( "\nContent mismatch" );
168 DBG_HDA ( 0, data, len );
169 DBG ( "Received:\n" );
170 DBG_HDA ( 0, iobuf->data, iob_len ( iobuf ) );
171 free_iob ( iob_disown ( iobuf ) );
175 /* Discard packet and return */
176 free_iob ( iob_disown ( iobuf ) );
182 * Perform loopback test between two network devices
184 * @v sender Sending network device
185 * @v receiver Received network device
186 * @v mtu Packet size (excluding link-layer headers)
187 * @ret rc Return status code
189 int loopback_test ( struct net_device *sender, struct net_device *receiver,
193 struct io_buffer *iobuf;
195 unsigned int successes;
198 /* Open network devices */
199 if ( ( rc = ifopen ( sender ) ) != 0 )
201 if ( ( rc = ifopen ( receiver ) ) != 0 )
204 /* Wait for link-up */
205 if ( ( rc = iflinkwait ( sender, 0 ) ) != 0 )
207 if ( ( rc = iflinkwait ( receiver, 0 ) ) != 0 )
210 /* Allocate data buffer */
211 if ( mtu < sizeof ( *seq ) )
212 mtu = sizeof ( *seq );
213 buf = malloc ( mtu );
216 seq = ( ( void * ) buf );
218 /* Print initial statistics */
219 printf ( "Performing loopback test from %s to %s with %zd byte MTU\n",
220 sender->name, receiver->name, mtu );
224 /* Start loopback test */
226 lotest_receiver = receiver;
228 /* Perform loopback test */
229 for ( successes = 0 ; ; successes++ ) {
231 /* Print running total */
232 printf ( "\r%d", successes );
234 /* Generate random packet */
235 *seq = htonl ( successes );
236 for ( i = sizeof ( *seq ) ; i < mtu ; i++ )
238 iobuf = alloc_iob ( MAX_LL_HEADER_LEN + mtu );
240 printf ( "\nFailed to allocate I/O buffer" );
244 iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
245 memcpy ( iob_put ( iobuf, mtu ), buf, mtu );
247 /* Transmit packet */
248 if ( ( rc = net_tx ( iob_disown ( iobuf ), sender,
249 &lotest_protocol, receiver->ll_addr,
250 sender->ll_addr ) ) != 0 ) {
251 printf ( "\nFailed to transmit packet: %s",
256 /* Wait for received packet */
257 if ( ( rc = loopback_wait ( buf, mtu ) ) != 0 )
263 /* Stop loopback testing */
264 lotest_receiver = NULL;
267 /* Dump final statistics */