Add qemu 2.4.0
[kvmfornfv.git] / qemu / roms / ipxe / src / hci / commands / vlan_cmd.c
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., 51 Franklin Street, Fifth Floor, Boston, MA
17  * 02110-1301, USA.
18  */
19
20 FILE_LICENCE ( GPL2_OR_LATER );
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <getopt.h>
26 #include <ipxe/netdevice.h>
27 #include <ipxe/command.h>
28 #include <ipxe/parseopt.h>
29 #include <ipxe/vlan.h>
30
31 /** @file
32  *
33  * VLAN commands
34  *
35  */
36
37 /** "vcreate" options */
38 struct vcreate_options {
39         /** VLAN tag */
40         unsigned int tag;
41         /** VLAN default priority */
42         unsigned int priority;
43 };
44
45 /** "vcreate" option list */
46 static struct option_descriptor vcreate_opts[] = {
47         OPTION_DESC ( "tag", 't', required_argument,
48                       struct vcreate_options, tag, parse_integer ),
49         OPTION_DESC ( "priority", 'p', required_argument,
50                       struct vcreate_options, priority, parse_integer ),
51 };
52
53 /** "vcreate" command descriptor */
54 static struct command_descriptor vcreate_cmd =
55         COMMAND_DESC ( struct vcreate_options, vcreate_opts, 1, 1,
56                        "<trunk interface>" );
57
58 /**
59  * "vcreate" command
60  *
61  * @v argc              Argument count
62  * @v argv              Argument list
63  * @ret rc              Return status code
64  */
65 static int vcreate_exec ( int argc, char **argv ) {
66         struct vcreate_options opts;
67         struct net_device *trunk;
68         int rc;
69
70         /* Parse options */
71         if ( ( rc = parse_options ( argc, argv, &vcreate_cmd, &opts ) ) != 0 )
72                 return rc;
73
74         /* Parse trunk interface */
75         if ( ( rc = parse_netdev ( argv[optind], &trunk ) ) != 0 )
76                 return rc;
77
78         /* Create VLAN device */
79         if ( ( rc = vlan_create ( trunk, opts.tag, opts.priority ) ) != 0 ) {
80                 printf ( "Could not create VLAN device: %s\n",
81                          strerror ( rc ) );
82                 return rc;
83         }
84
85         return 0;
86 }
87
88 /** "vdestroy" options */
89 struct vdestroy_options {};
90
91 /** "vdestroy" option list */
92 static struct option_descriptor vdestroy_opts[] = {};
93
94 /** "vdestroy" command descriptor */
95 static struct command_descriptor vdestroy_cmd =
96         COMMAND_DESC ( struct vdestroy_options, vdestroy_opts, 1, 1,
97                        "<VLAN interface>" );
98
99 /**
100  * "vdestroy" command
101  *
102  * @v argc              Argument count
103  * @v argv              Argument list
104  * @ret rc              Return status code
105  */
106 static int vdestroy_exec ( int argc, char **argv ) {
107         struct vdestroy_options opts;
108         struct net_device *netdev;
109         int rc;
110
111         /* Parse options */
112         if ( ( rc = parse_options ( argc, argv, &vdestroy_cmd, &opts ) ) != 0 )
113                 return rc;
114
115         /* Parse trunk interface */
116         if ( ( rc = parse_netdev ( argv[optind], &netdev ) ) != 0 )
117                 return rc;
118
119         /* Destroy VLAN device */
120         if ( ( rc = vlan_destroy ( netdev ) ) != 0 ) {
121                 printf ( "Could not destroy VLAN device: %s\n",
122                          strerror ( rc ) );
123                 return rc;
124         }
125
126         return 0;
127 }
128
129 /** VLAN commands */
130 struct command vlan_commands[] __command = {
131         {
132                 .name = "vcreate",
133                 .exec = vcreate_exec,
134         },
135         {
136                 .name = "vdestroy",
137                 .exec = vdestroy_exec,
138         },
139 };