Support vlan_group workaround implemented in XenServer kernels.
[sliver-openvswitch.git] / utilities / ovs-vlan-bug-workaround.c
1 /*
2  * Copyright (c) 2011 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18
19 #include <errno.h>
20 #include <getopt.h>
21 #include <linux/if_vlan.h>
22 #include <linux/sockios.h>
23 #include <net/if.h>
24 #include <stdlib.h>
25 #include <sys/ioctl.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29
30 #include "command-line.h"
31 #include "util.h"
32
33 #define ADD_ALL_VLANS_CMD 10
34 #define DEL_ALL_VLANS_CMD 11
35
36 static void usage(void);
37 static void parse_options(int argc, char *argv[]);
38
39 int
40 main(int argc, char *argv[])
41 {
42     struct vlan_ioctl_args vlan_args;
43     const char *netdev, *setting;
44     int fd;
45
46     set_program_name(argv[0]);
47
48     parse_options(argc, argv);
49     if (argc - optind != 2) {
50         ovs_fatal(0, "exactly two non-option arguments are required "
51                   "(use --help for help)");
52     }
53
54     memset(&vlan_args, 0, sizeof vlan_args);
55
56     /* Get command. */
57     setting = argv[optind + 1];
58     if (!strcmp(setting, "on")) {
59         vlan_args.cmd = ADD_ALL_VLANS_CMD;
60     } else if (!strcmp(setting, "off")) {
61         vlan_args.cmd = DEL_ALL_VLANS_CMD;
62     } else {
63         ovs_fatal(0, "second command line argument must be \"on\" or \"off\" "
64                   "(not \"%s\")", setting);
65     }
66
67     /* Get network device name. */
68     netdev = argv[optind];
69     if (strlen(netdev) >= IFNAMSIZ) {
70         ovs_fatal(0, "%s: network device name too long", netdev);
71     }
72     strcpy(vlan_args.device1, netdev);
73
74     /* Execute operation. */
75     fd = socket(AF_INET, SOCK_STREAM, 0);
76     if (fd < 0) {
77         ovs_fatal(errno, "socket creation failed");
78     }
79     if (ioctl(fd, SIOCSIFVLAN, &vlan_args) < 0) {
80         if (errno == ENOPKG) {
81             ovs_fatal(0, "operation failed (8021q module not loaded)");
82         } else if (errno == EOPNOTSUPP) {
83             ovs_fatal(0, "operation failed (kernel does not support the "
84                       "VLAN bug workaround)");
85         } else {
86             ovs_fatal(errno, "operation failed");
87         }
88     }
89     close(fd);
90
91     return 0;
92 }
93
94 static void
95 usage(void)
96 {
97     printf("\
98 %s, for enabling or disabling the kernel VLAN bug workaround\n\
99 usage: %s NETDEV SETTING\n\
100 where NETDEV is a network device (e.g. \"eth0\")\n\
101   and SETTING is \"on\" to enable the workaround or \"off\" to disable it.\n\
102 \n\
103 Options:\n\
104   -h, --help         Print this helpful information\n\
105   -V, --version      Display version information\n",
106            program_name, program_name);
107     exit(EXIT_SUCCESS);
108 }
109
110 static void
111 parse_options(int argc, char *argv[])
112 {
113     static const struct option long_options[] = {
114         {"help", no_argument, NULL, 'h'},
115         {"version", no_argument, NULL, 'V'},
116         {0, 0, 0, 0},
117     };
118     char *short_options = long_options_to_short_options(long_options);
119
120     for (;;) {
121         int option;
122
123         option = getopt_long(argc, argv, "+t:hVe", long_options, NULL);
124         if (option == -1) {
125             break;
126         }
127         switch (option) {
128         case 'h':
129             usage();
130             break;
131
132         case 'V':
133             OVS_PRINT_VERSION(0, 0);
134             exit(EXIT_SUCCESS);
135
136         case '?':
137             exit(EXIT_FAILURE);
138
139         default:
140             NOT_REACHED();
141         }
142     }
143     free(short_options);
144 }