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.

ifmgmt_cmd.c 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. /*
  2. * Copyright (C) 2007 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. #include <stdio.h>
  19. #include <getopt.h>
  20. #include <gpxe/netdevice.h>
  21. #include <gpxe/command.h>
  22. #include <usr/ifmgmt.h>
  23. /** @file
  24. *
  25. * Network interface management commands
  26. *
  27. */
  28. /** Options shared by all if<xxx> commands */
  29. static struct option ifcommon_longopts[] = {
  30. { "help", 0, NULL, 'h' },
  31. { NULL, 0, NULL, 0 },
  32. };
  33. /**
  34. * Print syntax of if<xxx> command
  35. *
  36. * @v argv Command arguments
  37. * @v verb Verb describing the action of the command
  38. */
  39. static void ifcommon_syntax ( char **argv, const char *verb ) {
  40. printf ( "Usage:\n"
  41. " %s [<interface>] [<interface>...]\n"
  42. "\n"
  43. "%s the specified network interfaces\n",
  44. argv[0], verb );
  45. }
  46. /**
  47. * Execute if<xxx> command over all network devices
  48. *
  49. * @v payload Command to execute
  50. * @ret rc Exit code
  51. */
  52. static int ifcommon_do_all ( int ( * payload ) ( struct net_device * ) ) {
  53. struct net_device *netdev;
  54. int rc = 0;
  55. /* Execute payload for each network device */
  56. for_each_netdev ( netdev ) {
  57. if ( payload ( netdev ) != 0 )
  58. rc = 1;
  59. }
  60. return rc;
  61. }
  62. /**
  63. * Execute if<xxx> command over list of network devices
  64. *
  65. * @v payload Command to execute
  66. * @ret rc Exit code
  67. */
  68. static int ifcommon_do_list ( int ( * payload ) ( struct net_device * ),
  69. char **list, unsigned int count ) {
  70. const char *netdev_name;
  71. struct net_device *netdev;
  72. int rc = 0;
  73. while ( count-- ) {
  74. netdev_name = *(list++);
  75. netdev = find_netdev ( netdev_name );
  76. if ( ! netdev ) {
  77. printf ( "%s: no such interface\n", netdev_name );
  78. rc = 1;
  79. continue;
  80. }
  81. if ( payload ( netdev ) != 0 )
  82. rc = 1;
  83. }
  84. return rc;
  85. }
  86. /**
  87. * Execute if<xxx> command
  88. *
  89. * @v payload Command to execute
  90. * @v verb Verb describing the action of the command
  91. * @v argc Argument count
  92. * @v argv Argument list
  93. * @ret rc Exit code
  94. */
  95. static __attribute__ (( regparm ( 2 ) )) int
  96. ifcommon_exec ( int ( * payload ) ( struct net_device * ),
  97. const char *verb, int argc, char **argv ) {
  98. int c;
  99. /* Parse options */
  100. while ( ( c = getopt_long ( argc, argv, "h", ifcommon_longopts,
  101. NULL ) ) >= 0 ) {
  102. switch ( c ) {
  103. case 'h':
  104. /* Display help text */
  105. default:
  106. /* Unrecognised/invalid option */
  107. ifcommon_syntax ( argv, verb );
  108. return 1;
  109. }
  110. }
  111. if ( optind == argc ) {
  112. return ifcommon_do_all ( payload );
  113. } else {
  114. return ifcommon_do_list ( payload, &argv[optind],
  115. ( argc - optind ) );
  116. }
  117. }
  118. /* "ifopen" command */
  119. static int ifopen_payload ( struct net_device *netdev ) {
  120. return ifopen ( netdev );
  121. }
  122. static int ifopen_exec ( int argc, char **argv ) {
  123. return ifcommon_exec ( ifopen_payload, "Open", argc, argv );
  124. }
  125. /* "ifclose" command */
  126. static int ifclose_payload ( struct net_device *netdev ) {
  127. ifclose ( netdev );
  128. return 0;
  129. }
  130. static int ifclose_exec ( int argc, char **argv ) {
  131. return ifcommon_exec ( ifclose_payload, "Close", argc, argv );
  132. }
  133. /* "ifstat" command */
  134. static int ifstat_payload ( struct net_device *netdev ) {
  135. ifstat ( netdev );
  136. return 0;
  137. }
  138. static int ifstat_exec ( int argc, char **argv ) {
  139. return ifcommon_exec ( ifstat_payload, "Display status of",
  140. argc, argv );
  141. }
  142. /** Interface management commands */
  143. struct command ifmgmt_commands[] __command = {
  144. {
  145. .name = "ifopen",
  146. .exec = ifopen_exec,
  147. },
  148. {
  149. .name = "ifclose",
  150. .exec = ifclose_exec,
  151. },
  152. {
  153. .name = "ifstat",
  154. .exec = ifstat_exec,
  155. },
  156. };