]>
git.proxmox.com Git - ovs.git/blob - utilities/ovs-dpctl.c
2 * Copyright (c) 2008, 2009 Nicira Networks.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <arpa/inet.h>
23 #include <netinet/in.h>
32 #include "command-line.h"
36 #include "dynamic-string.h"
44 #define THIS_MODULE VLM_dpctl
46 static const struct command all_commands
[];
48 static void usage(void) NO_RETURN
;
49 static void parse_options(int argc
, char *argv
[]);
52 main(int argc
, char *argv
[])
54 set_program_name(argv
[0]);
57 parse_options(argc
, argv
);
58 signal(SIGPIPE
, SIG_IGN
);
59 run_command(argc
- optind
, argv
+ optind
, all_commands
);
64 parse_options(int argc
, char *argv
[])
66 static struct option long_options
[] = {
67 {"timeout", required_argument
, 0, 't'},
68 {"verbose", optional_argument
, 0, 'v'},
69 {"help", no_argument
, 0, 'h'},
70 {"version", no_argument
, 0, 'V'},
73 char *short_options
= long_options_to_short_options(long_options
);
76 unsigned long int timeout
;
79 c
= getopt_long(argc
, argv
, short_options
, long_options
, NULL
);
86 timeout
= strtoul(optarg
, NULL
, 10);
88 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
99 OVS_PRINT_VERSION(0, 0);
103 vlog_set_verbosity(optarg
);
119 printf("%s: Open vSwitch datapath management utility\n"
120 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
121 " add-dp DP [IFACE...] add new datapath DP (with IFACEs)\n"
122 " del-dp DP delete local datapath DP\n"
123 " add-if DP IFACE... add each IFACE as a port on DP\n"
124 " del-if DP IFACE... delete each IFACE from DP\n"
125 " dump-dps display names of all datapaths\n"
126 " show show basic info on all datapaths\n"
127 " show DP... show basic info on each DP\n"
128 " dump-flows DP display flows in DP\n"
129 " del-flows DP delete all flows from DP\n"
130 " dump-groups DP display port groups in DP\n",
131 program_name
, program_name
);
133 printf("\nOther options:\n"
134 " -t, --timeout=SECS give up after SECS seconds\n"
135 " -h, --help display this help message\n"
136 " -V, --version display version information\n");
140 static void run(int retval
, const char *message
, ...)
143 static void run(int retval
, const char *message
, ...)
148 fprintf(stderr
, "%s: ", program_name
);
149 va_start(args
, message
);
150 vfprintf(stderr
, message
, args
);
153 fputs(": unexpected end of file\n", stderr
);
155 fprintf(stderr
, ": %s\n", strerror(retval
));
162 static void do_add_if(int argc
, char *argv
[]);
164 static int if_up(const char *netdev_name
)
166 struct netdev
*netdev
;
169 retval
= netdev_open(netdev_name
, NETDEV_ETH_TYPE_NONE
, &netdev
);
171 retval
= netdev_turn_flags_on(netdev
, NETDEV_UP
, true);
172 netdev_close(netdev
);
178 do_add_dp(int argc UNUSED
, char *argv
[])
181 run(dpif_create(argv
[1], &dpif
), "add_dp");
184 do_add_if(argc
, argv
);
189 do_del_dp(int argc UNUSED
, char *argv
[])
192 run(dpif_open(argv
[1], &dpif
), "opening datapath");
193 run(dpif_delete(dpif
), "del_dp");
198 compare_ports(const void *a_
, const void *b_
)
200 const struct odp_port
*a
= a_
;
201 const struct odp_port
*b
= b_
;
202 return a
->port
< b
->port
? -1 : a
->port
> b
->port
;
206 query_ports(struct dpif
*dpif
, struct odp_port
**ports
, size_t *n_ports
)
208 run(dpif_port_list(dpif
, ports
, n_ports
), "listing ports");
209 qsort(*ports
, *n_ports
, sizeof **ports
, compare_ports
);
213 do_add_if(int argc UNUSED
, char *argv
[])
215 bool failure
= false;
219 run(dpif_open(argv
[1], &dpif
), "opening datapath");
220 for (i
= 2; i
< argc
; i
++) {
221 char *save_ptr
= NULL
;
222 char *devname
, *suboptions
;
226 devname
= strtok_r(argv
[i
], ",", &save_ptr
);
228 ovs_error(0, "%s is not a valid network device name", argv
[i
]);
232 suboptions
= strtok_r(NULL
, "", &save_ptr
);
237 static char *options
[] = {
241 while (*suboptions
!= '\0') {
244 switch (getsubopt(&suboptions
, options
, &value
)) {
246 flags
|= ODP_PORT_INTERNAL
;
250 ovs_error(0, "unknown suboption '%s'", value
);
256 error
= dpif_port_add(dpif
, devname
, flags
, NULL
);
258 ovs_error(error
, "adding %s to %s failed", devname
, argv
[1]);
260 } else if (if_up(devname
)) {
271 get_port_number(struct dpif
*dpif
, const char *name
, uint16_t *port
)
273 struct odp_port
*ports
;
277 query_ports(dpif
, &ports
, &n_ports
);
278 for (i
= 0; i
< n_ports
; i
++) {
279 if (!strcmp(name
, ports
[i
].devname
)) {
280 *port
= ports
[i
].port
;
286 ovs_error(0, "no port named %s", name
);
291 do_del_if(int argc UNUSED
, char *argv
[])
293 bool failure
= false;
297 run(dpif_open(argv
[1], &dpif
), "opening datapath");
298 for (i
= 2; i
< argc
; i
++) {
299 const char *name
= argv
[i
];
303 if (!name
[strspn(name
, "0123456789")]) {
305 } else if (!get_port_number(dpif
, name
, &port
)) {
310 error
= dpif_port_del(dpif
, port
);
312 ovs_error(error
, "deleting port %s from %s failed", name
, argv
[1]);
323 show_dpif(struct dpif
*dpif
)
325 struct odp_port
*ports
;
326 struct odp_stats stats
;
330 printf("%s:\n", dpif_name(dpif
));
331 if (!dpif_get_dp_stats(dpif
, &stats
)) {
332 printf("\tflows: cur:%"PRIu32
", soft-max:%"PRIu32
", "
333 "hard-max:%"PRIu32
"\n",
334 stats
.n_flows
, stats
.cur_capacity
, stats
.max_capacity
);
335 printf("\tports: cur:%"PRIu32
", max:%"PRIu32
"\n",
336 stats
.n_ports
, stats
.max_ports
);
337 printf("\tgroups: max:%"PRIu16
"\n", stats
.max_groups
);
338 printf("\tlookups: frags:%"PRIu64
", hit:%"PRIu64
", missed:%"PRIu64
", "
340 stats
.n_frags
, stats
.n_hit
, stats
.n_missed
, stats
.n_lost
);
341 printf("\tqueues: max-miss:%"PRIu16
", max-action:%"PRIu16
"\n",
342 stats
.max_miss_queue
, stats
.max_action_queue
);
344 query_ports(dpif
, &ports
, &n_ports
);
345 for (i
= 0; i
< n_ports
; i
++) {
346 printf("\tport %u: %s", ports
[i
].port
, ports
[i
].devname
);
347 if (ports
[i
].flags
& ODP_PORT_INTERNAL
) {
348 printf(" (internal)");
357 do_show(int argc
, char *argv
[])
359 bool failure
= false;
362 for (i
= 1; i
< argc
; i
++) {
363 const char *name
= argv
[i
];
367 error
= dpif_open(name
, &dpif
);
371 ovs_error(error
, "opening datapath %s failed", name
);
377 for (i
= 0; i
< ODP_MAX
; i
++) {
382 sprintf(name
, "dp%u", i
);
383 error
= dpif_open(name
, &dpif
);
386 } else if (error
!= ENODEV
) {
387 ovs_error(error
, "opening datapath %s failed", name
);
398 do_dump_dps(int argc UNUSED
, char *argv
[] UNUSED
)
405 error
= dp_enumerate(&all_dps
);
407 for (i
= 0; i
< all_dps
.n
; i
++) {
409 if (!dpif_open(all_dps
.names
[i
], &dpif
)) {
410 printf("%s\n", dpif_name(dpif
));
415 svec_destroy(&all_dps
);
422 do_dump_flows(int argc UNUSED
, char *argv
[])
424 struct odp_flow
*flows
;
430 run(dpif_open(argv
[1], &dpif
), "opening datapath");
431 run(dpif_flow_list_all(dpif
, &flows
, &n_flows
), "listing all flows");
434 for (i
= 0; i
< n_flows
; i
++) {
435 struct odp_flow
*f
= &flows
[i
];
436 enum { MAX_ACTIONS
= 4096 / sizeof(union odp_action
) };
437 union odp_action actions
[MAX_ACTIONS
];
439 f
->actions
= actions
;
440 f
->n_actions
= MAX_ACTIONS
;
441 dpif_flow_get(dpif
, f
);
444 format_odp_flow(&ds
, f
);
445 printf("%s\n", ds_cstr(&ds
));
452 do_del_flows(int argc UNUSED
, char *argv
[])
456 run(dpif_open(argv
[1], &dpif
), "opening datapath");
457 run(dpif_flow_flush(dpif
), "deleting all flows");
462 do_dump_groups(int argc UNUSED
, char *argv
[])
464 struct odp_stats stats
;
468 run(dpif_open(argv
[1], &dpif
), "opening datapath");
469 run(dpif_get_dp_stats(dpif
, &stats
), "get datapath stats");
470 for (i
= 0; i
< stats
.max_groups
; i
++) {
474 if (!dpif_port_group_get(dpif
, i
, &ports
, &n_ports
) && n_ports
) {
477 printf("group %u:", i
);
478 for (j
= 0; j
< n_ports
; j
++) {
479 printf(" %"PRIu16
, ports
[j
]);
489 do_help(int argc UNUSED
, char *argv
[] UNUSED
)
494 static const struct command all_commands
[] = {
495 { "add-dp", 1, INT_MAX
, do_add_dp
},
496 { "del-dp", 1, 1, do_del_dp
},
497 { "add-if", 2, INT_MAX
, do_add_if
},
498 { "del-if", 2, INT_MAX
, do_del_if
},
499 { "dump-dps", 0, 0, do_dump_dps
},
500 { "show", 0, INT_MAX
, do_show
},
501 { "dump-flows", 1, 1, do_dump_flows
},
502 { "del-flows", 1, 1, do_del_flows
},
503 { "dump-groups", 1, 1, do_dump_groups
},
504 { "help", 0, INT_MAX
, do_help
},
505 { NULL
, 0, 0, NULL
},