iPXE
lotest_cmd.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 <stdio.h>
00027 #include <stdlib.h>
00028 #include <string.h>
00029 #include <getopt.h>
00030 #include <ipxe/netdevice.h>
00031 #include <ipxe/command.h>
00032 #include <ipxe/parseopt.h>
00033 #include <ipxe/if_ether.h>
00034 #include <usr/lotest.h>
00035 
00036 /** @file
00037  *
00038  * Loopback testing commands
00039  *
00040  */
00041 
00042 /** "lotest" options */
00043 struct lotest_options {
00044         /** MTU */
00045         unsigned int mtu;
00046         /** Broadcast */
00047         int broadcast;
00048 };
00049 
00050 /** "lotest" option list */
00051 static struct option_descriptor lotest_opts[] = {
00052         OPTION_DESC ( "mtu", 'm', required_argument,
00053                       struct lotest_options, mtu, parse_integer ),
00054         OPTION_DESC ( "broadcast", 'b', no_argument,
00055                       struct lotest_options, broadcast, parse_flag ),
00056 };
00057 
00058 /** "lotest" command descriptor */
00059 static struct command_descriptor lotest_cmd =
00060         COMMAND_DESC ( struct lotest_options, lotest_opts, 2, 2,
00061                        "<sending interface> <receiving interface>" );
00062 
00063 /**
00064  * "lotest" command
00065  *
00066  * @v argc              Argument count
00067  * @v argv              Argument list
00068  * @ret rc              Return status code
00069  */
00070 static int lotest_exec ( int argc, char **argv ) {
00071         struct lotest_options opts;
00072         struct net_device *sender;
00073         struct net_device *receiver;
00074         int rc;
00075 
00076         /* Parse options */
00077         if ( ( rc = parse_options ( argc, argv, &lotest_cmd, &opts ) ) != 0 )
00078                 return rc;
00079 
00080         /* Parse sending interface name */
00081         if ( ( rc = parse_netdev ( argv[optind], &sender ) ) != 0 )
00082                 return rc;
00083 
00084         /* Parse receiving interface name */
00085         if ( ( rc = parse_netdev ( argv[ optind + 1 ], &receiver ) ) != 0 )
00086                 return rc;
00087 
00088         /* Use default MTU if none specified */
00089         if ( ! opts.mtu )
00090                 opts.mtu = ETH_MAX_MTU;
00091 
00092         /* Perform loopback test */
00093         if ( ( rc = loopback_test ( sender, receiver, opts.mtu,
00094                                     opts.broadcast ) ) != 0 ) {
00095                 printf ( "Test failed: %s\n", strerror ( rc ) );
00096                 return rc;
00097         }
00098 
00099         return 0;
00100 }
00101 
00102 /** Loopback testing commands */
00103 struct command lotest_command __command = {
00104         .name = "lotest",
00105         .exec = lotest_exec,
00106 };