Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

ifmgmt_cmd.c 4.1KB

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