tunnel: Geneve TLV handling support for OpenFlow.
[cascardo/ovs.git] / utilities / ovs-appctl.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 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
19 #include <errno.h>
20 #include <getopt.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24
25 #include "command-line.h"
26 #include "daemon.h"
27 #include "dirs.h"
28 #include "dynamic-string.h"
29 #include "jsonrpc.h"
30 #include "process.h"
31 #include "timeval.h"
32 #include "unixctl.h"
33 #include "util.h"
34 #include "openvswitch/vlog.h"
35
36 static void usage(void);
37 static const char *parse_command_line(int argc, char *argv[]);
38 static struct jsonrpc *connect_to_target(const char *target);
39
40 int
41 main(int argc, char *argv[])
42 {
43     char *cmd_result, *cmd_error;
44     struct jsonrpc *client;
45     char *cmd, **cmd_argv;
46     const char *target;
47     int cmd_argc;
48     int error;
49
50     set_program_name(argv[0]);
51
52     /* Parse command line and connect to target. */
53     target = parse_command_line(argc, argv);
54     client = connect_to_target(target);
55
56     /* Transact request and process reply. */
57     cmd = argv[optind++];
58     cmd_argc = argc - optind;
59     cmd_argv = cmd_argc ? argv + optind : NULL;
60     error = unixctl_client_transact(client, cmd, cmd_argc, cmd_argv,
61                                     &cmd_result, &cmd_error);
62     if (error) {
63         ovs_fatal(error, "%s: transaction error", target);
64     }
65
66     if (cmd_error) {
67         jsonrpc_close(client);
68         fputs(cmd_error, stderr);
69         ovs_error(0, "%s: server returned an error", target);
70         exit(2);
71     } else if (cmd_result) {
72         fputs(cmd_result, stdout);
73     } else {
74         OVS_NOT_REACHED();
75     }
76
77     jsonrpc_close(client);
78     free(cmd_result);
79     free(cmd_error);
80     return 0;
81 }
82
83 static void
84 usage(void)
85 {
86     printf("\
87 %s, for querying and controlling Open vSwitch daemon\n\
88 usage: %s [TARGET] COMMAND [ARG...]\n\
89 Targets:\n\
90   -t, --target=TARGET  pidfile or socket to contact\n\
91 Common commands:\n\
92   list-commands      List commands supported by the target\n\
93   version            Print version of the target\n\
94   vlog/list          List current logging levels\n\
95   vlog/set [SPEC]\n\
96       Set log levels as detailed in SPEC, which may include:\n\
97       A valid module name (all modules, by default)\n\
98       'syslog', 'console', 'file' (all destinations, by default))\n\
99       'off', 'emer', 'err', 'warn', 'info', or 'dbg' ('dbg', bydefault)\n\
100   vlog/reopen        Make the program reopen its log file\n\
101 Other options:\n\
102   --timeout=SECS     wait at most SECS seconds for a response\n\
103   -h, --help         Print this helpful information\n\
104   -V, --version      Display ovs-appctl version information\n",
105            program_name, program_name);
106     exit(EXIT_SUCCESS);
107 }
108
109 static const char *
110 parse_command_line(int argc, char *argv[])
111 {
112     enum {
113         OPT_START = UCHAR_MAX + 1,
114         VLOG_OPTION_ENUMS
115     };
116     static const struct option long_options[] = {
117         {"target", required_argument, NULL, 't'},
118         {"execute", no_argument, NULL, 'e'},
119         {"help", no_argument, NULL, 'h'},
120         {"option", no_argument, NULL, 'o'},
121         {"version", no_argument, NULL, 'V'},
122         {"timeout", required_argument, NULL, 'T'},
123         VLOG_LONG_OPTIONS,
124         {NULL, 0, NULL, 0},
125     };
126     char *short_options_ = ovs_cmdl_long_options_to_short_options(long_options);
127     char *short_options = xasprintf("+%s", short_options_);
128     const char *target;
129     int e_options;
130
131     target = NULL;
132     e_options = 0;
133     for (;;) {
134         int option;
135
136         option = getopt_long(argc, argv, short_options, long_options, NULL);
137         if (option == -1) {
138             break;
139         }
140         switch (option) {
141         case 't':
142             if (target) {
143                 ovs_fatal(0, "-t or --target may be specified only once");
144             }
145             target = optarg;
146             break;
147
148         case 'e':
149             /* We ignore -e for compatibility.  Older versions specified the
150              * command as the argument to -e.  Since the current version takes
151              * the command as non-option arguments and we say that -e has no
152              * arguments, this just works in the common case. */
153             if (e_options++) {
154                 ovs_fatal(0, "-e or --execute may be speciifed only once");
155             }
156             break;
157
158         case 'h':
159             usage();
160             break;
161
162         case 'o':
163             ovs_cmdl_print_options(long_options);
164             exit(EXIT_SUCCESS);
165
166         case 'T':
167             time_alarm(atoi(optarg));
168             break;
169
170         case 'V':
171             ovs_print_version(0, 0);
172             exit(EXIT_SUCCESS);
173
174         VLOG_OPTION_HANDLERS
175
176         case '?':
177             exit(EXIT_FAILURE);
178
179         default:
180             OVS_NOT_REACHED();
181         }
182     }
183     free(short_options_);
184     free(short_options);
185
186     if (optind >= argc) {
187         ovs_fatal(0, "at least one non-option argument is required "
188                   "(use --help for help)");
189     }
190
191     return target ? target : "ovs-vswitchd";
192 }
193
194 static struct jsonrpc *
195 connect_to_target(const char *target)
196 {
197     struct jsonrpc *client;
198     char *socket_name;
199     int error;
200
201 #ifndef _WIN32
202     if (target[0] != '/') {
203         char *pidfile_name;
204         pid_t pid;
205
206         pidfile_name = xasprintf("%s/%s.pid", ovs_rundir(), target);
207         pid = read_pidfile(pidfile_name);
208         if (pid < 0) {
209             ovs_fatal(-pid, "cannot read pidfile \"%s\"", pidfile_name);
210         }
211         free(pidfile_name);
212         socket_name = xasprintf("%s/%s.%ld.ctl",
213                                 ovs_rundir(), target, (long int) pid);
214 #else
215     /* On windows, if the 'target' contains ':', we make an assumption that
216      * it is an absolute path. */
217     if (!strchr(target, ':')) {
218         socket_name = xasprintf("%s/%s.ctl", ovs_rundir(), target);
219 #endif
220     } else {
221         socket_name = xstrdup(target);
222     }
223
224     error = unixctl_client_create(socket_name, &client);
225     if (error) {
226         ovs_fatal(error, "cannot connect to \"%s\"", socket_name);
227     }
228     free(socket_name);
229
230     return client;
231 }
232