iPXE
lotest.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2010 Michael Brown <mbrown@fensystems.co.uk>.
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License as
00006  * published by the Free Software Foundation; either version 2 of the
00007  * License, or any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012  * General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
00017  * 02110-1301, USA.
00018  *
00019  * You can also choose to distribute this program under the terms of
00020  * the Unmodified Binary Distribution Licence (as given in the file
00021  * COPYING.UBDL), provided that you have satisfied its requirements.
00022  */
00023 
00024 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00025 
00026 #include <stdint.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <stdio.h>
00030 #include <errno.h>
00031 #include <byteswap.h>
00032 #include <ipxe/iobuf.h>
00033 #include <ipxe/netdevice.h>
00034 #include <ipxe/if_ether.h>
00035 #include <ipxe/keys.h>
00036 #include <ipxe/console.h>
00037 #include <usr/ifmgmt.h>
00038 #include <usr/lotest.h>
00039 
00040 /** @file
00041  *
00042  * Loopback testing
00043  *
00044  */
00045 
00046 /** Current loopback test receiver */
00047 static struct net_device *lotest_receiver;
00048 
00049 /** Loopback testing received packets */
00050 static LIST_HEAD ( lotest_queue );
00051 
00052 /**
00053  * Process received packet
00054  *
00055  * @v iobuf             I/O buffer
00056  * @v netdev            Network device
00057  * @v ll_dest           Link-layer destination address
00058  * @v ll_source         Link-layer source address
00059  * @v flags             Packet flags
00060  * @ret rc              Return status code
00061  */
00062 static int lotest_rx ( struct io_buffer *iobuf,
00063                        struct net_device *netdev,
00064                        const void *ll_dest __unused,
00065                        const void *ll_source __unused,
00066                        unsigned int flags __unused ) {
00067 
00068         /* Add to received packet queue if currently performing a test */
00069         if ( netdev == lotest_receiver ) {
00070                 list_add_tail ( &iobuf->list, &lotest_queue );
00071         } else {
00072                 free_iob ( iobuf );
00073         }
00074 
00075         return 0;
00076 }
00077 
00078 /**
00079  * Dequeue received packet
00080  *
00081  * @ret iobuf           I/O buffer, or NULL
00082  */
00083 static struct io_buffer * lotest_dequeue ( void ) {
00084         struct io_buffer *iobuf;
00085 
00086         /* Remove first packet (if any) from received packet queue */
00087         iobuf = list_first_entry ( &lotest_queue, struct io_buffer, list );
00088         if ( ! iobuf )
00089                 return NULL;
00090         list_del ( &iobuf->list );
00091 
00092         return iobuf;
00093 }
00094 
00095 /**
00096  * Transcribe network-layer address
00097  *
00098  * @v net_addr          Network-layer address
00099  * @ret string          Human-readable transcription of address
00100  */
00101 static const char * lotest_ntoa ( const void *net_addr __unused ) {
00102         return "<INVALID>";
00103 }
00104 
00105 /**
00106  * Loopback test network-layer protocol
00107  *
00108  * Using a dedicated network-layer protocol avoids problems caused by
00109  * cards supporting features such as IPv4 checksum offload trying to
00110  * interpret the (randomly generated) network-layer content.
00111  */
00112 static struct net_protocol lotest_protocol __net_protocol = {
00113         .name = "LOTEST",
00114         .rx = lotest_rx,
00115         .ntoa = lotest_ntoa,
00116         .net_proto = htons ( 0x6950 ), /* Not a genuine protocol number */
00117         .net_addr_len = 0,
00118 };
00119 
00120 /**
00121  * Discard all received loopback test packets
00122  *
00123  */
00124 static void lotest_flush ( void ) {
00125         struct io_buffer *iobuf;
00126 
00127         while ( ( iobuf = lotest_dequeue() ) != NULL )
00128                 free_iob ( iobuf );
00129 }
00130 
00131 /**
00132  * Wait for packet to be received
00133  *
00134  * @v data              Expected data
00135  * @v len               Expected data length
00136  * @ret rc              Return status code
00137  */
00138 static int loopback_wait ( void *data, size_t len ) {
00139         struct io_buffer *iobuf;
00140 
00141         /* Poll until packet arrives */
00142         while ( 1 ) {
00143 
00144                 /* Check for cancellation */
00145                 if ( iskey() && ( getchar() == CTRL_C ) )
00146                         return -ECANCELED;
00147 
00148                 /* Poll network devices */
00149                 net_poll();
00150 
00151                 /* Dequeue packet, if available */
00152                 iobuf = lotest_dequeue();
00153                 if ( ! iobuf )
00154                         continue;
00155 
00156                 /* Check packet length */
00157                 if ( iob_len ( iobuf ) != len ) {
00158                         printf ( "\nLength mismatch: sent %zd, received %zd",
00159                                  len, iob_len ( iobuf ) );
00160                         DBG ( "\nSent:\n" );
00161                         DBG_HDA ( 0, data, len );
00162                         DBG ( "Received:\n" );
00163                         DBG_HDA ( 0, iobuf->data, iob_len ( iobuf ) );
00164                         free_iob ( iob_disown ( iobuf ) );
00165                         return -EINVAL;
00166                 }
00167 
00168                 /* Check packet content */
00169                 if ( memcmp ( iobuf->data, data, len ) != 0 ) {
00170                         printf ( "\nContent mismatch" );
00171                         DBG ( "\nSent:\n" );
00172                         DBG_HDA ( 0, data, len );
00173                         DBG ( "Received:\n" );
00174                         DBG_HDA ( 0, iobuf->data, iob_len ( iobuf ) );
00175                         free_iob ( iob_disown ( iobuf ) );
00176                         return -EINVAL;
00177                 }
00178 
00179                 /* Discard packet and return */
00180                 free_iob ( iob_disown ( iobuf ) );
00181                 return 0;
00182         }
00183 }
00184 
00185 /**
00186  * Perform loopback test between two network devices
00187  *
00188  * @v sender            Sending network device
00189  * @v receiver          Received network device
00190  * @v mtu               Packet size (excluding link-layer headers)
00191  * @v broadcast         Use broadcast link-layer address
00192  * @ret rc              Return status code
00193  */
00194 int loopback_test ( struct net_device *sender, struct net_device *receiver,
00195                     size_t mtu, int broadcast ) {
00196         uint8_t *buf;
00197         uint32_t *seq;
00198         struct io_buffer *iobuf;
00199         const void *ll_dest;
00200         unsigned int i;
00201         unsigned int successes;
00202         int rc;
00203 
00204         /* Open network devices */
00205         if ( ( rc = ifopen ( sender ) ) != 0 )
00206                 return rc;
00207         if ( ( rc = ifopen ( receiver ) ) != 0 )
00208                 return rc;
00209 
00210         /* Wait for link-up */
00211         if ( ( rc = iflinkwait ( sender, 0 ) ) != 0 )
00212                 return rc;
00213         if ( ( rc = iflinkwait ( receiver, 0 ) ) != 0 )
00214                 return rc;
00215 
00216         /* Allocate data buffer */
00217         if ( mtu < sizeof ( *seq ) )
00218                 mtu = sizeof ( *seq );
00219         buf = malloc ( mtu );
00220         if ( ! buf )
00221                 return -ENOMEM;
00222         seq = ( ( void * ) buf );
00223 
00224         /* Determine destination address */
00225         ll_dest = ( broadcast ? sender->ll_broadcast : receiver->ll_addr );
00226 
00227         /* Print initial statistics */
00228         printf ( "Performing %sloopback test from %s to %s with %zd byte MTU\n",
00229                  ( broadcast ? "broadcast " : "" ), sender->name,
00230                  receiver->name, mtu );
00231         ifstat ( sender );
00232         ifstat ( receiver );
00233 
00234         /* Start loopback test */
00235         lotest_flush();
00236         lotest_receiver = receiver;
00237 
00238         /* Perform loopback test */
00239         for ( successes = 0 ; ; successes++ ) {
00240 
00241                 /* Print running total */
00242                 printf ( "\r%d", successes );
00243 
00244                 /* Generate random packet */
00245                 *seq = htonl ( successes );
00246                 for ( i = sizeof ( *seq ) ; i < mtu ; i++ )
00247                         buf[i] = random();
00248                 iobuf = alloc_iob ( MAX_LL_HEADER_LEN + mtu );
00249                 if ( ! iobuf ) {
00250                         printf ( "\nFailed to allocate I/O buffer" );
00251                         rc = -ENOMEM;
00252                         break;
00253                 }
00254                 iob_reserve ( iobuf, MAX_LL_HEADER_LEN );
00255                 memcpy ( iob_put ( iobuf, mtu ), buf, mtu );
00256 
00257                 /* Transmit packet */
00258                 if ( ( rc = net_tx ( iob_disown ( iobuf ), sender,
00259                                      &lotest_protocol, ll_dest,
00260                                      sender->ll_addr ) ) != 0 ) {
00261                         printf ( "\nFailed to transmit packet: %s",
00262                                  strerror ( rc ) );
00263                         break;
00264                 }
00265 
00266                 /* Wait for received packet */
00267                 if ( ( rc = loopback_wait ( buf, mtu ) ) != 0 )
00268                         break;
00269         }
00270 
00271         printf ( "\n");
00272 
00273         /* Stop loopback testing */
00274         lotest_receiver = NULL;
00275         lotest_flush();
00276 
00277         /* Dump final statistics */
00278         ifstat ( sender );
00279         ifstat ( receiver );
00280 
00281         /* Free buffer */
00282         free ( buf );
00283 
00284         return 0;
00285 }