datapath: Convert odp_flow_key to use Netlink attributes instead.
[sliver-openvswitch.git] / utilities / ovs-dpctl.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 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 #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 "dpif.h"
37 #include "dynamic-string.h"
38 #include "netdev.h"
39 #include "odp-util.h"
40 #include "shash.h"
41 #include "svec.h"
42 #include "timeval.h"
43 #include "util.h"
44 #include "vlog.h"
45
46 VLOG_DEFINE_THIS_MODULE(dpctl);
47
48 static const struct command all_commands[];
49
50 static void usage(void) NO_RETURN;
51 static void parse_options(int argc, char *argv[]);
52
53 int
54 main(int argc, char *argv[])
55 {
56     set_program_name(argv[0]);
57     parse_options(argc, argv);
58     signal(SIGPIPE, SIG_IGN);
59     run_command(argc - optind, argv + optind, all_commands);
60     return 0;
61 }
62
63 static void
64 parse_options(int argc, char *argv[])
65 {
66     enum {
67         OPT_DUMMY = UCHAR_MAX + 1,
68         VLOG_OPTION_ENUMS
69     };
70     static struct option long_options[] = {
71         {"timeout", required_argument, 0, 't'},
72         {"help", no_argument, 0, 'h'},
73         {"version", no_argument, 0, 'V'},
74         VLOG_LONG_OPTIONS,
75         {0, 0, 0, 0},
76     };
77     char *short_options = long_options_to_short_options(long_options);
78
79     for (;;) {
80         unsigned long int timeout;
81         int c;
82
83         c = getopt_long(argc, argv, short_options, long_options, NULL);
84         if (c == -1) {
85             break;
86         }
87
88         switch (c) {
89         case 't':
90             timeout = strtoul(optarg, NULL, 10);
91             if (timeout <= 0) {
92                 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
93                           optarg);
94             } else {
95                 time_alarm(timeout);
96             }
97             break;
98
99         case 'h':
100             usage();
101
102         case 'V':
103             OVS_PRINT_VERSION(0, 0);
104             exit(EXIT_SUCCESS);
105
106         VLOG_OPTION_HANDLERS
107
108         case '?':
109             exit(EXIT_FAILURE);
110
111         default:
112             abort();
113         }
114     }
115     free(short_options);
116 }
117
118 static void
119 usage(void)
120 {
121     printf("%s: Open vSwitch datapath management utility\n"
122            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
123            "  add-dp DP [IFACE...]     add new datapath DP (with IFACEs)\n"
124            "  del-dp DP                delete local datapath DP\n"
125            "  add-if DP IFACE...       add each IFACE as a port on DP\n"
126            "  del-if DP IFACE...       delete each IFACE from DP\n"
127            "  dump-dps                 display names of all datapaths\n"
128            "  show                     show basic info on all datapaths\n"
129            "  show DP...               show basic info on each DP\n"
130            "  dump-flows DP            display flows in DP\n"
131            "  del-flows DP             delete all flows from DP\n",
132            program_name, program_name);
133     vlog_usage();
134     printf("\nOther options:\n"
135            "  -t, --timeout=SECS          give up after SECS seconds\n"
136            "  -h, --help                  display this help message\n"
137            "  -V, --version               display version information\n");
138     exit(EXIT_SUCCESS);
139 }
140
141 static void run(int retval, const char *message, ...)
142     PRINTF_FORMAT(2, 3);
143
144 static void run(int retval, const char *message, ...)
145 {
146     if (retval) {
147         va_list args;
148
149         fprintf(stderr, "%s: ", program_name);
150         va_start(args, message);
151         vfprintf(stderr, message, args);
152         va_end(args);
153         if (retval == EOF) {
154             fputs(": unexpected end of file\n", stderr);
155         } else {
156             fprintf(stderr, ": %s\n", strerror(retval));
157         }
158
159         exit(EXIT_FAILURE);
160     }
161 }
162 \f
163 static void do_add_if(int argc, char *argv[]);
164
165 static int if_up(const char *netdev_name)
166 {
167     struct netdev *netdev;
168     int retval;
169
170     retval = netdev_open_default(netdev_name, &netdev);
171     if (!retval) {
172         retval = netdev_turn_flags_on(netdev, NETDEV_UP, true);
173         netdev_close(netdev);
174     }
175     return retval;
176 }
177
178 static int
179 parsed_dpif_open(const char *arg_, bool create, struct dpif **dpifp)
180 {
181     int result;
182     char *name, *type;
183
184     dp_parse_name(arg_, &name, &type);
185
186     if (create) {
187         result = dpif_create(name, type, dpifp);
188     } else {
189         result = dpif_open(name, type, dpifp);
190     }
191
192     free(name);
193     free(type);
194     return result;
195 }
196
197 static void
198 do_add_dp(int argc OVS_UNUSED, char *argv[])
199 {
200     struct dpif *dpif;
201     run(parsed_dpif_open(argv[1], true, &dpif), "add_dp");
202     dpif_close(dpif);
203     if (argc > 2) {
204         do_add_if(argc, argv);
205     }
206 }
207
208 static void
209 do_del_dp(int argc OVS_UNUSED, char *argv[])
210 {
211     struct dpif *dpif;
212     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
213     run(dpif_delete(dpif), "del_dp");
214     dpif_close(dpif);
215 }
216
217 static int
218 compare_ports(const void *a_, const void *b_)
219 {
220     const struct odp_port *a = a_;
221     const struct odp_port *b = b_;
222     return a->port < b->port ? -1 : a->port > b->port;
223 }
224
225 static void
226 query_ports(struct dpif *dpif, struct odp_port **ports, size_t *n_ports)
227 {
228     run(dpif_port_list(dpif, ports, n_ports), "listing ports");
229     qsort(*ports, *n_ports, sizeof **ports, compare_ports);
230 }
231
232 static void
233 do_add_if(int argc OVS_UNUSED, char *argv[])
234 {
235     bool failure = false;
236     struct dpif *dpif;
237     int i;
238
239     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
240     for (i = 2; i < argc; i++) {
241         char *save_ptr = NULL;
242         struct netdev_options options;
243         struct netdev *netdev;
244         struct shash args;
245         char *option;
246         int error;
247
248         options.name = strtok_r(argv[i], ",", &save_ptr);
249         options.type = "system";
250         options.args = &args;
251         options.ethertype = NETDEV_ETH_TYPE_NONE;
252
253         if (!options.name) {
254             ovs_error(0, "%s is not a valid network device name", argv[i]);
255             continue;
256         }
257
258         shash_init(&args);
259         while ((option = strtok_r(NULL, "", &save_ptr)) != NULL) {
260             char *save_ptr_2 = NULL;
261             char *key, *value;
262
263             key = strtok_r(option, "=", &save_ptr_2);
264             value = strtok_r(NULL, "", &save_ptr_2);
265             if (!value) {
266                 value = "";
267             }
268
269             if (!strcmp(key, "type")) {
270                 options.type = value;
271             } else if (!shash_add_once(&args, key, value)) {
272                 ovs_error(0, "duplicate \"%s\" option", key);
273             }
274         }
275
276         error = netdev_open(&options, &netdev);
277         if (error) {
278             ovs_error(error, "%s: failed to open network device",
279                       options.name);
280         } else {
281             error = dpif_port_add(dpif, netdev, NULL);
282             if (error) {
283                 ovs_error(error, "adding %s to %s failed",
284                           options.name, argv[1]);
285             } else {
286                 error = if_up(options.name);
287             }
288             netdev_close(netdev);
289         }
290         if (error) {
291             failure = true;
292         }
293     }
294     dpif_close(dpif);
295     if (failure) {
296         exit(EXIT_FAILURE);
297     }
298 }
299
300 static bool
301 get_port_number(struct dpif *dpif, const char *name, uint16_t *port)
302 {
303     struct odp_port odp_port;
304
305     if (!dpif_port_query_by_name(dpif, name, &odp_port)) {
306         *port = odp_port.port;
307         return true;
308     } else {
309         ovs_error(0, "no port named %s", name);
310         return false;
311     }
312 }
313
314 static void
315 do_del_if(int argc OVS_UNUSED, char *argv[])
316 {
317     bool failure = false;
318     struct dpif *dpif;
319     int i;
320
321     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
322     for (i = 2; i < argc; i++) {
323         const char *name = argv[i];
324         uint16_t port;
325         int error;
326
327         if (!name[strspn(name, "0123456789")]) {
328             port = atoi(name);
329         } else if (!get_port_number(dpif, name, &port)) {
330             failure = true;
331             continue;
332         }
333
334         error = dpif_port_del(dpif, port);
335         if (error) {
336             ovs_error(error, "deleting port %s from %s failed", name, argv[1]);
337             failure = true;
338         }
339     }
340     dpif_close(dpif);
341     if (failure) {
342         exit(EXIT_FAILURE);
343     }
344 }
345
346 static void
347 show_dpif(struct dpif *dpif)
348 {
349     struct odp_port *ports;
350     struct odp_stats stats;
351     size_t n_ports;
352     size_t i;
353
354     printf("%s:\n", dpif_name(dpif));
355     if (!dpif_get_dp_stats(dpif, &stats)) {
356         printf("\tflows: cur:%"PRIu32", soft-max:%"PRIu32", "
357                "hard-max:%"PRIu32"\n",
358                stats.n_flows, stats.cur_capacity, stats.max_capacity);
359         printf("\tports: cur:%"PRIu32", max:%"PRIu32"\n",
360                stats.n_ports, stats.max_ports);
361         printf("\tlookups: frags:%llu, hit:%llu, missed:%llu, lost:%llu\n",
362                (unsigned long long int) stats.n_frags,
363                (unsigned long long int) stats.n_hit,
364                (unsigned long long int) stats.n_missed,
365                (unsigned long long int) stats.n_lost);
366         printf("\tqueues: max-miss:%"PRIu16", max-action:%"PRIu16"\n",
367                stats.max_miss_queue, stats.max_action_queue);
368     }
369     query_ports(dpif, &ports, &n_ports);
370     for (i = 0; i < n_ports; i++) {
371         const struct odp_port *p = &ports[i];
372         struct ds ds;
373
374         printf("\tport %u: %s", p->port, p->devname);
375
376         ds_init(&ds);
377         format_odp_port_type(&ds, p);
378         printf("%s\n", ds_cstr(&ds));
379         ds_destroy(&ds);
380     }
381     free(ports);
382     dpif_close(dpif);
383 }
384
385 static void
386 do_show(int argc, char *argv[])
387 {
388     bool failure = false;
389     if (argc > 1) {
390         int i;
391         for (i = 1; i < argc; i++) {
392             const char *name = argv[i];
393             struct dpif *dpif;
394             int error;
395
396             error = parsed_dpif_open(name, false, &dpif);
397             if (!error) {
398                 show_dpif(dpif);
399             } else {
400                 ovs_error(error, "opening datapath %s failed", name);
401                 failure = true;
402             }
403         }
404     } else {
405         struct svec types;
406         const char *type;
407         size_t i;
408
409         svec_init(&types);
410         dp_enumerate_types(&types);
411         SVEC_FOR_EACH (i, type, &types) {
412             struct svec names;
413             const char *name;
414             size_t j;
415
416             svec_init(&names);
417             if (dp_enumerate_names(type, &names)) {
418                 failure = true;
419                 continue;
420             }
421             SVEC_FOR_EACH (j, name, &names) {
422                 struct dpif *dpif;
423                 int error;
424
425                 error = dpif_open(name, type, &dpif);
426                 if (!error) {
427                     show_dpif(dpif);
428                 } else {
429                     ovs_error(error, "opening datapath %s failed", name);
430                     failure = true;
431                 }
432             }
433             svec_destroy(&names);
434         }
435         svec_destroy(&types);
436     }
437     if (failure) {
438         exit(EXIT_FAILURE);
439     }
440 }
441
442 static void
443 do_dump_dps(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
444 {
445     struct svec dpif_names, dpif_types;
446     unsigned int i;
447     int error = 0;
448
449     svec_init(&dpif_names);
450     svec_init(&dpif_types);
451     dp_enumerate_types(&dpif_types);
452
453     for (i = 0; i < dpif_types.n; i++) {
454         unsigned int j;
455         int retval;
456
457         retval = dp_enumerate_names(dpif_types.names[i], &dpif_names);
458         if (retval) {
459             error = retval;
460         }
461
462         for (j = 0; j < dpif_names.n; j++) {
463             struct dpif *dpif;
464             if (!dpif_open(dpif_names.names[j], dpif_types.names[i], &dpif)) {
465                 printf("%s\n", dpif_name(dpif));
466                 dpif_close(dpif);
467             }
468         }
469     }
470
471     svec_destroy(&dpif_names);
472     svec_destroy(&dpif_types);
473     if (error) {
474         exit(EXIT_FAILURE);
475     }
476 }
477
478 static void
479 do_dump_flows(int argc OVS_UNUSED, char *argv[])
480 {
481     struct dpif_flow_dump dump;
482     struct dpif *dpif;
483     struct ds ds;
484
485     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
486
487     ds_init(&ds);
488     dpif_flow_dump_start(&dump, dpif);
489     for (;;) {
490         enum { MAX_ACTIONS = 4096 }; /* An arbitrary but large number. */
491         uint32_t actions[MAX_ACTIONS * sizeof(struct nlattr) / 4];
492         uint32_t keybuf[ODPUTIL_FLOW_KEY_U32S];
493         struct odp_flow f;
494
495         memset(&f, 0, sizeof f);
496         f.actions = (struct nlattr *) actions;
497         f.actions_len = sizeof actions;
498         f.key = (struct nlattr *) keybuf;
499         f.key_len = sizeof keybuf;
500
501         if (!dpif_flow_dump_next(&dump, &f)) {
502             break;
503         }
504
505         ds_clear(&ds);
506         format_odp_flow(&ds, &f);
507         printf("%s\n", ds_cstr(&ds));
508     }
509     dpif_flow_dump_done(&dump);
510     ds_destroy(&ds);
511     dpif_close(dpif);
512 }
513
514 static void
515 do_del_flows(int argc OVS_UNUSED, char *argv[])
516 {
517     struct dpif *dpif;
518
519     run(parsed_dpif_open(argv[1], false, &dpif), "opening datapath");
520     run(dpif_flow_flush(dpif), "deleting all flows");
521     dpif_close(dpif);
522 }
523
524 static void
525 do_help(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
526 {
527     usage();
528 }
529
530 static const struct command all_commands[] = {
531     { "add-dp", 1, INT_MAX, do_add_dp },
532     { "del-dp", 1, 1, do_del_dp },
533     { "add-if", 2, INT_MAX, do_add_if },
534     { "del-if", 2, INT_MAX, do_del_if },
535     { "dump-dps", 0, 0, do_dump_dps },
536     { "show", 0, INT_MAX, do_show },
537     { "dump-flows", 1, 1, do_dump_flows },
538     { "del-flows", 1, 1, do_del_flows },
539     { "help", 0, INT_MAX, do_help },
540     { NULL, 0, 0, NULL },
541 };