Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

ifmgmt_cmd.c 4.2KB

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