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.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdio.h>
  21. #include <errno.h>
  22. #include <getopt.h>
  23. #include <ipxe/netdevice.h>
  24. #include <ipxe/command.h>
  25. #include <ipxe/parseopt.h>
  26. #include <usr/ifmgmt.h>
  27. #include <hci/ifmgmt_cmd.h>
  28. /** @file
  29. *
  30. * Network interface management commands
  31. *
  32. */
  33. /** "if<xxx>" command options */
  34. struct option_descriptor ifcommon_opts[0];
  35. /**
  36. * Execute if<xxx> command
  37. *
  38. * @v argc Argument count
  39. * @v argv Argument list
  40. * @v cmd Command descriptor
  41. * @v payload Command to execute
  42. * @v verb Verb describing the action of the command
  43. * @ret rc Return status code
  44. */
  45. int ifcommon_exec ( int argc, char **argv,
  46. struct command_descriptor *cmd,
  47. int ( * payload ) ( struct net_device * ),
  48. int stop_on_first_success ) {
  49. struct ifcommon_options opts;
  50. struct net_device *netdev;
  51. int rc;
  52. /* Parse options */
  53. if ( ( rc = parse_options ( argc, argv, cmd, &opts ) ) != 0 )
  54. return rc;
  55. if ( optind != argc ) {
  56. /* Treat arguments as a list of interfaces to try */
  57. while ( optind != argc ) {
  58. if ( ( rc = parse_netdev ( argv[optind++],
  59. &netdev ) ) != 0 ) {
  60. continue;
  61. }
  62. if ( ( ( rc = payload ( netdev ) ) == 0 ) &&
  63. stop_on_first_success ) {
  64. return 0;
  65. }
  66. }
  67. } else {
  68. /* Try all interfaces */
  69. rc = -ENODEV;
  70. for_each_netdev ( netdev ) {
  71. if ( ( ( rc = payload ( netdev ) ) == 0 ) &&
  72. stop_on_first_success ) {
  73. return 0;
  74. }
  75. }
  76. }
  77. return rc;
  78. }
  79. /** "ifopen" command descriptor */
  80. static struct command_descriptor ifopen_cmd =
  81. COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
  82. "[<interface>...]" );
  83. /**
  84. * "ifopen" payload
  85. *
  86. * @v netdev Network device
  87. * @ret rc Return status code
  88. */
  89. static int ifopen_payload ( struct net_device *netdev ) {
  90. return ifopen ( netdev );
  91. }
  92. /**
  93. * The "ifopen" command
  94. *
  95. * @v argc Argument count
  96. * @v argv Argument list
  97. * @ret rc Return status code
  98. */
  99. static int ifopen_exec ( int argc, char **argv ) {
  100. return ifcommon_exec ( argc, argv, &ifopen_cmd, ifopen_payload, 0 );
  101. }
  102. /** "ifclose" command descriptor */
  103. static struct command_descriptor ifclose_cmd =
  104. COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
  105. "[<interface>...]" );
  106. /**
  107. * "ifclose" payload
  108. *
  109. * @v netdev Network device
  110. * @ret rc Return status code
  111. */
  112. static int ifclose_payload ( struct net_device *netdev ) {
  113. ifclose ( netdev );
  114. return 0;
  115. }
  116. /**
  117. * The "ifclose" command
  118. *
  119. * @v argc Argument count
  120. * @v argv Argument list
  121. * @ret rc Return status code
  122. */
  123. static int ifclose_exec ( int argc, char **argv ) {
  124. return ifcommon_exec ( argc, argv, &ifclose_cmd, ifclose_payload, 0 );
  125. }
  126. /** "ifstat" command descriptor */
  127. static struct command_descriptor ifstat_cmd =
  128. COMMAND_DESC ( struct ifcommon_options, ifcommon_opts, 0, MAX_ARGUMENTS,
  129. "[<interface>...]" );
  130. /**
  131. * "ifstat" payload
  132. *
  133. * @v netdev Network device
  134. * @ret rc Return status code
  135. */
  136. static int ifstat_payload ( struct net_device *netdev ) {
  137. ifstat ( netdev );
  138. return 0;
  139. }
  140. /**
  141. * The "ifstat" command
  142. *
  143. * @v argc Argument count
  144. * @v argv Argument list
  145. * @ret rc Return status code
  146. */
  147. static int ifstat_exec ( int argc, char **argv ) {
  148. return ifcommon_exec ( argc, argv, &ifstat_cmd, ifstat_payload, 0 );
  149. }
  150. /** Interface management commands */
  151. struct command ifmgmt_commands[] __command = {
  152. {
  153. .name = "ifopen",
  154. .exec = ifopen_exec,
  155. },
  156. {
  157. .name = "ifclose",
  158. .exec = ifclose_exec,
  159. },
  160. {
  161. .name = "ifstat",
  162. .exec = ifstat_exec,
  163. },
  164. };