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 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. /**
  34. * Execute if<xxx> command
  35. *
  36. * @v argc Argument count
  37. * @v argv Argument list
  38. * @v cmd Command descriptor
  39. * @v payload Command to execute
  40. * @v verb Verb describing the action of the command
  41. * @ret rc Return status code
  42. */
  43. int ifcommon_exec ( int argc, char **argv,
  44. struct ifcommon_command_descriptor *ifcmd ) {
  45. struct command_descriptor *cmd = &ifcmd->cmd;
  46. uint8_t opts[cmd->len];
  47. struct net_device *netdev;
  48. int i;
  49. int rc;
  50. /* Parse options */
  51. if ( ( rc = parse_options ( argc, argv, cmd, opts ) ) != 0 )
  52. return rc;
  53. if ( optind != argc ) {
  54. /* Treat arguments as a list of interfaces to try */
  55. for ( i = optind ; i < argc ; i++ ) {
  56. if ( ( rc = parse_netdev ( argv[i], &netdev ) ) != 0 )
  57. continue;
  58. if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
  59. && ifcmd->stop_on_first_success ) {
  60. return 0;
  61. }
  62. }
  63. } else {
  64. /* Try all interfaces */
  65. rc = -ENODEV;
  66. for_each_netdev ( netdev ) {
  67. if ( ( ( rc = ifcmd->payload ( netdev, opts ) ) == 0 )
  68. && ifcmd->stop_on_first_success ) {
  69. return 0;
  70. }
  71. }
  72. }
  73. return rc;
  74. }
  75. /** "ifopen" options */
  76. struct ifopen_options {};
  77. /** "ifopen" option list */
  78. static struct option_descriptor ifopen_opts[] = {};
  79. /**
  80. * "ifopen" payload
  81. *
  82. * @v netdev Network device
  83. * @v opts Command options
  84. * @ret rc Return status code
  85. */
  86. static int ifopen_payload ( struct net_device *netdev,
  87. struct ifopen_options *opts __unused ) {
  88. return ifopen ( netdev );
  89. }
  90. /** "ifopen" command descriptor */
  91. static struct ifcommon_command_descriptor ifopen_cmd =
  92. IFCOMMON_COMMAND_DESC ( struct ifopen_options, ifopen_opts,
  93. 0, MAX_ARGUMENTS, "[<interface>...]",
  94. ifopen_payload, 0 );
  95. /**
  96. * The "ifopen" command
  97. *
  98. * @v argc Argument count
  99. * @v argv Argument list
  100. * @ret rc Return status code
  101. */
  102. static int ifopen_exec ( int argc, char **argv ) {
  103. return ifcommon_exec ( argc, argv, &ifopen_cmd );
  104. }
  105. /** "ifclose" options */
  106. struct ifclose_options {};
  107. /** "ifclose" option list */
  108. static struct option_descriptor ifclose_opts[] = {};
  109. /**
  110. * "ifclose" payload
  111. *
  112. * @v netdev Network device
  113. * @v opts Command options
  114. * @ret rc Return status code
  115. */
  116. static int ifclose_payload ( struct net_device *netdev,
  117. struct ifclose_options *opts __unused ) {
  118. ifclose ( netdev );
  119. return 0;
  120. }
  121. /** "ifclose" command descriptor */
  122. static struct ifcommon_command_descriptor ifclose_cmd =
  123. IFCOMMON_COMMAND_DESC ( struct ifclose_options, ifclose_opts,
  124. 0, MAX_ARGUMENTS, "[<interface>...]",
  125. ifclose_payload, 0 );
  126. /**
  127. * The "ifclose" command
  128. *
  129. * @v argc Argument count
  130. * @v argv Argument list
  131. * @ret rc Return status code
  132. */
  133. static int ifclose_exec ( int argc, char **argv ) {
  134. return ifcommon_exec ( argc, argv, &ifclose_cmd );
  135. }
  136. /** "ifstat" options */
  137. struct ifstat_options {};
  138. /** "ifstat" option list */
  139. static struct option_descriptor ifstat_opts[] = {};
  140. /**
  141. * "ifstat" payload
  142. *
  143. * @v netdev Network device
  144. * @v opts Command options
  145. * @ret rc Return status code
  146. */
  147. static int ifstat_payload ( struct net_device *netdev,
  148. struct ifstat_options *opts __unused ) {
  149. ifstat ( netdev );
  150. return 0;
  151. }
  152. /** "ifstat" command descriptor */
  153. static struct ifcommon_command_descriptor ifstat_cmd =
  154. IFCOMMON_COMMAND_DESC ( struct ifstat_options, ifstat_opts,
  155. 0, MAX_ARGUMENTS, "[<interface>...]",
  156. ifstat_payload, 0 );
  157. /**
  158. * The "ifstat" command
  159. *
  160. * @v argc Argument count
  161. * @v argv Argument list
  162. * @ret rc Return status code
  163. */
  164. static int ifstat_exec ( int argc, char **argv ) {
  165. return ifcommon_exec ( argc, argv, &ifstat_cmd );
  166. }
  167. /** "ifconf" options */
  168. struct ifconf_options {
  169. /** Configurator */
  170. struct net_device_configurator *configurator;
  171. };
  172. /** "ifconf" option list */
  173. static struct option_descriptor ifconf_opts[] = {
  174. OPTION_DESC ( "configurator", 'c', required_argument,
  175. struct ifconf_options, configurator,
  176. parse_netdev_configurator ),
  177. };
  178. /**
  179. * "ifconf" payload
  180. *
  181. * @v netdev Network device
  182. * @v opts Command options
  183. * @ret rc Return status code
  184. */
  185. static int ifconf_payload ( struct net_device *netdev,
  186. struct ifconf_options *opts ) {
  187. int rc;
  188. /* Attempt configuration */
  189. if ( ( rc = ifconf ( netdev, opts->configurator ) ) != 0 ) {
  190. /* Close device on failure, to avoid memory exhaustion */
  191. netdev_close ( netdev );
  192. return rc;
  193. }
  194. return 0;
  195. }
  196. /** "ifconf" command descriptor */
  197. static struct ifcommon_command_descriptor ifconf_cmd =
  198. IFCOMMON_COMMAND_DESC ( struct ifconf_options, ifconf_opts,
  199. 0, MAX_ARGUMENTS, "[<interface>...]",
  200. ifconf_payload, 1 );
  201. /**
  202. * The "ifconf" command
  203. *
  204. * @v argc Argument count
  205. * @v argv Argument list
  206. * @ret rc Return status code
  207. */
  208. int ifconf_exec ( int argc, char **argv ) {
  209. return ifcommon_exec ( argc, argv, &ifconf_cmd );
  210. }
  211. /** Interface management commands */
  212. struct command ifmgmt_commands[] __command = {
  213. {
  214. .name = "ifopen",
  215. .exec = ifopen_exec,
  216. },
  217. {
  218. .name = "ifclose",
  219. .exec = ifclose_exec,
  220. },
  221. {
  222. .name = "ifstat",
  223. .exec = ifstat_exec,
  224. },
  225. {
  226. .name = "ifconf",
  227. .exec = ifconf_exec,
  228. },
  229. };