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

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