Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

vlan_cmd.c 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2010 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 <stdlib.h>
  21. #include <string.h>
  22. #include <getopt.h>
  23. #include <ipxe/netdevice.h>
  24. #include <ipxe/command.h>
  25. #include <ipxe/vlan.h>
  26. /** @file
  27. *
  28. * VLAN commands
  29. *
  30. */
  31. static void vcreate_syntax ( char **argv ) {
  32. printf ( "Usage:\n %s --tag <tag> [--priority <priority] "
  33. "<trunk interface>\n", argv[0] );
  34. }
  35. static int vcreate_exec ( int argc, char **argv ) {
  36. static struct option vcreate_opts[] = {
  37. { "help", 0, NULL, 'h' },
  38. { "tag", required_argument, NULL, 't' },
  39. { "priority", required_argument, NULL, 'p' },
  40. { NULL, 0, NULL, 0 },
  41. };
  42. const char *trunk_name;
  43. const char *tag_text = NULL;
  44. const char *priority_text = NULL;
  45. struct net_device *trunk;
  46. unsigned int tag;
  47. unsigned int priority;
  48. char *endp;
  49. int c;
  50. int rc;
  51. /* Parse command line */
  52. while ( ( c = getopt_long ( argc, argv, "ht:p:", vcreate_opts,
  53. NULL ) ) >= 0 ) {
  54. switch ( c ) {
  55. case 't':
  56. tag_text = optarg;
  57. break;
  58. case 'p':
  59. priority_text = optarg;
  60. break;
  61. case 'h':
  62. /* Display help text */
  63. default:
  64. /* Unrecognised/invalid option */
  65. vcreate_syntax ( argv );
  66. return 1;
  67. }
  68. }
  69. if ( optind != ( argc - 1 ) ) {
  70. vcreate_syntax ( argv );
  71. return 1;
  72. }
  73. trunk_name = argv[optind];
  74. if ( ! tag_text ) {
  75. vcreate_syntax ( argv );
  76. return 1;
  77. }
  78. /* Identify network device */
  79. trunk = find_netdev ( trunk_name );
  80. if ( ! trunk ) {
  81. printf ( "%s: no such interface\n", trunk_name );
  82. return 1;
  83. }
  84. tag = strtoul ( tag_text, &endp, 10 );
  85. if ( *endp ) {
  86. printf ( "%s: invalid tag\n", tag_text );
  87. return 1;
  88. }
  89. if ( priority_text ) {
  90. priority = strtoul ( priority_text, &endp, 10 );
  91. if ( *endp ) {
  92. printf ( "%s: invalid priority\n", priority_text );
  93. return 1;
  94. }
  95. } else {
  96. priority = 0;
  97. }
  98. /* Create VLAN device */
  99. if ( ( rc = vlan_create ( trunk, tag, priority ) ) != 0 ) {
  100. printf ( "Could not create VLAN device: %s\n",
  101. strerror ( rc ) );
  102. return 1;
  103. }
  104. return 0;
  105. }
  106. static void vdestroy_syntax ( char **argv ) {
  107. printf ( "Usage:\n %s <interface>\n", argv[0] );
  108. }
  109. static int vdestroy_exec ( int argc, char **argv ) {
  110. static struct option vdestroy_opts[] = {
  111. { "help", 0, NULL, 'h' },
  112. { NULL, 0, NULL, 0 },
  113. };
  114. const char *netdev_name;
  115. struct net_device *netdev;
  116. int c;
  117. int rc;
  118. /* Parse command line */
  119. while ( ( c = getopt_long ( argc, argv, "h", vdestroy_opts,
  120. NULL ) ) >= 0 ) {
  121. switch ( c ) {
  122. case 'h':
  123. /* Display help text */
  124. default:
  125. /* Unrecognised/invalid option */
  126. vdestroy_syntax ( argv );
  127. return 1;
  128. }
  129. }
  130. if ( optind != ( argc - 1 ) ) {
  131. vdestroy_syntax ( argv );
  132. return 1;
  133. }
  134. netdev_name = argv[optind];
  135. /* Identify network device */
  136. netdev = find_netdev ( netdev_name );
  137. if ( ! netdev ) {
  138. printf ( "%s: no such interface\n", netdev_name );
  139. return 1;
  140. }
  141. /* Destroy VLAN device */
  142. if ( ( rc = vlan_destroy ( netdev ) ) != 0 ) {
  143. printf ( "Could not destroy VLAN device: %s\n",
  144. strerror ( rc ) );
  145. return 1;
  146. }
  147. return 0;
  148. }
  149. struct command vlan_commands[] __command = {
  150. {
  151. .name = "vcreate",
  152. .exec = vcreate_exec,
  153. },
  154. {
  155. .name = "vdestroy",
  156. .exec = vdestroy_exec,
  157. },
  158. };