summaryrefslogtreecommitdiff
path: root/utilities/ovs-vlan-bug-workaround.c
blob: add800e56633527341e90b8d9013511363b59c7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * Copyright (c) 2011 Nicira, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <config.h>

#include <errno.h>
#include <getopt.h>
#include <linux/if_vlan.h>
#include <linux/sockios.h>
#include <net/if.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include "command-line.h"
#include "util.h"

#define ADD_ALL_VLANS_CMD 10
#define DEL_ALL_VLANS_CMD 11

static void usage(void);
static void parse_options(int argc, char *argv[]);

int
main(int argc, char *argv[])
{
    struct vlan_ioctl_args vlan_args;
    const char *netdev, *setting;
    int fd;

    set_program_name(argv[0]);

    parse_options(argc, argv);
    if (argc - optind != 2) {
        ovs_fatal(0, "exactly two non-option arguments are required "
                  "(use --help for help)");
    }

    memset(&vlan_args, 0, sizeof vlan_args);

    /* Get command. */
    setting = argv[optind + 1];
    if (!strcmp(setting, "on")) {
        vlan_args.cmd = ADD_ALL_VLANS_CMD;
    } else if (!strcmp(setting, "off")) {
        vlan_args.cmd = DEL_ALL_VLANS_CMD;
    } else {
        ovs_fatal(0, "second command line argument must be \"on\" or \"off\" "
                  "(not \"%s\")", setting);
    }

    /* Get network device name. */
    netdev = argv[optind];
    if (strlen(netdev) >= IFNAMSIZ) {
        ovs_fatal(0, "%s: network device name too long", netdev);
    }
    strcpy(vlan_args.device1, netdev);

    /* Execute operation. */
    fd = socket(AF_INET, SOCK_STREAM, 0);
    if (fd < 0) {
        ovs_fatal(errno, "socket creation failed");
    }
    if (ioctl(fd, SIOCSIFVLAN, &vlan_args) < 0) {
        if (errno == ENOPKG) {
            ovs_fatal(0, "operation failed (8021q module not loaded)");
        } else if (errno == EOPNOTSUPP) {
            ovs_fatal(0, "operation failed (kernel does not support the "
                      "VLAN bug workaround)");
        } else {
            ovs_fatal(errno, "operation failed");
        }
    }
    close(fd);

    return 0;
}

static void
usage(void)
{
    printf("\
%s, for enabling or disabling the kernel VLAN bug workaround\n\
usage: %s NETDEV SETTING\n\
where NETDEV is a network device (e.g. \"eth0\")\n\
  and SETTING is \"on\" to enable the workaround or \"off\" to disable it.\n\
\n\
Options:\n\
  -h, --help         Print this helpful information\n\
  -V, --version      Display version information\n",
           program_name, program_name);
    exit(EXIT_SUCCESS);
}

static void
parse_options(int argc, char *argv[])
{
    static const struct option long_options[] = {
        {"help", no_argument, NULL, 'h'},
        {"version", no_argument, NULL, 'V'},
        {NULL, 0, NULL, 0},
    };
    char *short_options = ovs_cmdl_long_options_to_short_options(long_options);

    for (;;) {
        int option;

        option = getopt_long(argc, argv, "+t:hVe", long_options, NULL);
        if (option == -1) {
            break;
        }
        switch (option) {
        case 'h':
            usage();
            break;

        case 'V':
            ovs_print_version(0, 0);
            exit(EXIT_SUCCESS);

        case '?':
            exit(EXIT_FAILURE);

        default:
            OVS_NOT_REACHED();
        }
    }
    free(short_options);
}