compiler: Define NO_RETURN for MSVC.
[cascardo/ovs.git] / utilities / ovs-dpctl.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
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 #include <arpa/inet.h>
19 #include <errno.h>
20 #include <getopt.h>
21 #include <inttypes.h>
22 #include <sys/socket.h>
23 #include <net/if.h>
24 #include <netinet/in.h>
25 #include <signal.h>
26 #include <stdarg.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <sys/stat.h>
31 #include <sys/time.h>
32
33 #include "command-line.h"
34 #include "compiler.h"
35 #include "dirs.h"
36 #include "dpctl.h"
37 #include "fatal-signal.h"
38 #include "odp-util.h"
39 #include "ofp-parse.h"
40 #include "packets.h"
41 #include "timeval.h"
42 #include "util.h"
43 #include "vlog.h"
44
45 static struct dpctl_params dpctl_p;
46
47 NO_RETURN static void usage(void *userdata OVS_UNUSED);
48 static void parse_options(int argc, char *argv[]);
49
50 static void
51 dpctl_print(void *userdata OVS_UNUSED, bool error, const char *msg)
52 {
53     FILE *outfile = error ? stderr : stdout;
54     fputs(msg, outfile);
55 }
56
57 int
58 main(int argc, char *argv[])
59 {
60     int error;
61     set_program_name(argv[0]);
62     parse_options(argc, argv);
63     fatal_ignore_sigpipe();
64
65     dpctl_p.output = dpctl_print;
66     dpctl_p.usage = usage;
67
68     error = dpctl_run_command(argc - optind, (const char **) argv + optind,
69                               &dpctl_p);
70     return error ? EXIT_FAILURE : EXIT_SUCCESS;
71 }
72
73 static void
74 parse_options(int argc, char *argv[])
75 {
76     enum {
77         OPT_CLEAR = UCHAR_MAX + 1,
78         OPT_MAY_CREATE,
79         VLOG_OPTION_ENUMS
80     };
81     static const struct option long_options[] = {
82         {"statistics", no_argument, NULL, 's'},
83         {"clear", no_argument, NULL, OPT_CLEAR},
84         {"may-create", no_argument, NULL, OPT_MAY_CREATE},
85         {"more", no_argument, NULL, 'm'},
86         {"timeout", required_argument, NULL, 't'},
87         {"help", no_argument, NULL, 'h'},
88         {"version", no_argument, NULL, 'V'},
89         VLOG_LONG_OPTIONS,
90         {NULL, 0, NULL, 0},
91     };
92     char *short_options = long_options_to_short_options(long_options);
93
94     for (;;) {
95         unsigned long int timeout;
96         int c;
97
98         c = getopt_long(argc, argv, short_options, long_options, NULL);
99         if (c == -1) {
100             break;
101         }
102
103         switch (c) {
104         case 's':
105             dpctl_p.print_statistics = true;
106             break;
107
108         case OPT_CLEAR:
109             dpctl_p.zero_statistics = true;
110             break;
111
112         case OPT_MAY_CREATE:
113             dpctl_p.may_create = true;
114             break;
115
116         case 'm':
117             dpctl_p.verbosity++;
118             break;
119
120         case 't':
121             timeout = strtoul(optarg, NULL, 10);
122             if (timeout <= 0) {
123                 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
124                           optarg);
125             } else {
126                 time_alarm(timeout);
127             }
128             break;
129
130         case 'h':
131             usage(NULL);
132
133         case 'V':
134             ovs_print_version(0, 0);
135             exit(EXIT_SUCCESS);
136
137         VLOG_OPTION_HANDLERS
138
139         case '?':
140             exit(EXIT_FAILURE);
141
142         default:
143             abort();
144         }
145     }
146     free(short_options);
147 }
148
149 static void
150 usage(void *userdata OVS_UNUSED)
151 {
152     printf("%s: Open vSwitch datapath management utility\n"
153            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
154            "  add-dp DP [IFACE...]     add new datapath DP (with IFACEs)\n"
155            "  del-dp DP                delete local datapath DP\n"
156            "  add-if DP IFACE...       add each IFACE as a port on DP\n"
157            "  set-if DP IFACE...       reconfigure each IFACE within DP\n"
158            "  del-if DP IFACE...       delete each IFACE from DP\n"
159            "  dump-dps                 display names of all datapaths\n"
160            "  show                     show basic info on all datapaths\n"
161            "  show DP...               show basic info on each DP\n"
162            "  dump-flows [DP]          display flows in DP\n"
163            "  add-flow [DP] FLOW ACTIONS add FLOW with ACTIONS to DP\n"
164            "  mod-flow [DP] FLOW ACTIONS change FLOW actions to ACTIONS in DP\n"
165            "  del-flow [DP] FLOW         delete FLOW from DP\n"
166            "  del-flows [DP]             delete all flows from DP\n"
167            "Each IFACE on add-dp, add-if, and set-if may be followed by\n"
168            "comma-separated options.  See ovs-dpctl(8) for syntax, or the\n"
169            "Interface table in ovs-vswitchd.conf.db(5) for an options list.\n"
170            "For COMMAND dump-flows, add-flow, mod-flow, del-flow and\n"
171            "del-flows, DP is optional if there is only one datapath.\n",
172            program_name, program_name);
173     vlog_usage();
174     printf("\nOptions for show and mod-flow:\n"
175            "  -s,  --statistics           print statistics for port or flow\n"
176            "\nOptions for dump-flows:\n"
177            "  -m, --more                  increase verbosity of output\n"
178            "\nOptions for mod-flow:\n"
179            "  --may-create                create flow if it doesn't exist\n"
180            "  --clear                     reset existing stats to zero\n"
181            "\nOther options:\n"
182            "  -t, --timeout=SECS          give up after SECS seconds\n"
183            "  -h, --help                  display this help message\n"
184            "  -V, --version               display version information\n");
185     exit(EXIT_SUCCESS);
186 }
187