1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
37 #include <netinet/in.h>
43 #include "command-line.h"
49 #include "openflow-netlink.h"
52 #include "socket-util.h"
54 #include "ofp-print.h"
56 #include "vconn-ssl.h"
59 #define THIS_MODULE VLM_DPCTL
61 static const char* ifconfigbin = "/sbin/ifconfig";
67 void (*handler)(int argc, char *argv[]);
70 static struct command all_commands[];
72 static void usage(void) NO_RETURN;
73 static void parse_options(int argc, char *argv[]);
75 int main(int argc, char *argv[])
79 set_program_name(argv[0]);
81 parse_options(argc, argv);
86 fatal(0, "missing command name; use --help for help");
88 for (p = all_commands; p->name != NULL; p++) {
89 if (!strcmp(p->name, argv[0])) {
91 if (n_arg < p->min_args)
92 fatal(0, "'%s' command requires at least %d arguments",
93 p->name, p->min_args);
94 else if (n_arg > p->max_args)
95 fatal(0, "'%s' command takes at most %d arguments",
96 p->name, p->max_args);
98 p->handler(argc, argv);
103 fatal(0, "unknown command '%s'; use --help for help", argv[0]);
109 parse_options(int argc, char *argv[])
111 static struct option long_options[] = {
112 {"verbose", optional_argument, 0, 'v'},
113 {"help", no_argument, 0, 'h'},
114 {"version", no_argument, 0, 'V'},
116 {"private-key", required_argument, 0, 'p'},
117 {"certificate", required_argument, 0, 'c'},
118 {"ca-cert", required_argument, 0, 'C'},
122 char *short_options = long_options_to_short_options(long_options);
128 c = getopt_long(argc, argv, short_options, long_options, &indexptr);
138 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
142 vlog_set_verbosity(optarg);
147 vconn_ssl_set_private_key_file(optarg);
151 vconn_ssl_set_certificate_file(optarg);
155 vconn_ssl_set_ca_cert_file(optarg);
172 printf("%s: OpenFlow switch management utility\n"
173 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
175 "\nCommands that apply to local datapaths only:\n"
176 " adddp nl:DP_ID add a new local datapath DP_ID\n"
177 " deldp nl:DP_ID delete local datapath DP_ID\n"
178 " addif nl:DP_ID IFACE add IFACE as a port on DP_ID\n"
179 " delif nl:DP_ID IFACE delete IFACE as a port on DP_ID\n"
180 " benchmark-nl nl:DP_ID N SIZE send N packets of SIZE bytes\n"
182 "\nCommands that apply to local datapaths and remote switches:\n"
183 " show METHOD show information about METHOD\n"
184 " monitor METHOD print packets received on METHOD\n"
185 " dump-tables METHOD print table stats for METHOD\n"
186 " dump-flows METHOD T_ID print all flow entries in table T_ID of METHOD\n"
187 " dump-flows METHOD T_ID FLOW print matching FLOWs in table T_ID of METHOD\n"
188 " add-flows METHOD FILE add flows from FILE to METHOD\n"
189 "where each METHOD is an active OpenFlow connection method.\n",
190 program_name, program_name);
191 vconn_usage(true, false);
192 printf("\nOptions:\n"
193 " -v, --verbose set maximum verbosity level\n"
194 " -h, --help display this help message\n"
195 " -V, --version display version information\n");
199 static void run(int retval, const char *name)
202 fatal(retval, "%s", name);
207 /* Netlink-only commands. */
209 static int if_up(const char* intf)
212 snprintf(command, sizeof command, "%s %s up &> /dev/null",
214 return system(command);
217 static void open_nl_vconn(const char *name, bool subscribe, struct dpif *dpif)
219 if (strncmp(name, "nl:", 3)
221 || name[strspn(name + 3, "0123456789") + 3]) {
222 fatal(0, "%s: argument is not of the form \"nl:DP_ID\"", name);
224 run(dpif_open(atoi(name + 3), subscribe, dpif), "opening datapath");
227 static void do_add_dp(int argc UNUSED, char *argv[])
230 open_nl_vconn(argv[1], false, &dp);
231 run(dpif_add_dp(&dp), "add_dp");
235 static void do_del_dp(int argc UNUSED, char *argv[])
238 open_nl_vconn(argv[1], false, &dp);
239 run(dpif_del_dp(&dp), "del_dp");
243 static void do_add_port(int argc UNUSED, char *argv[])
247 open_nl_vconn(argv[1], false, &dp);
248 run(dpif_add_port(&dp, argv[2]), "add_port");
252 static void do_del_port(int argc UNUSED, char *argv[])
255 open_nl_vconn(argv[1], false, &dp);
256 run(dpif_del_port(&dp, argv[2]), "del_port");
260 #define BENCHMARK_INCR 100
262 static void do_benchmark_nl(int argc UNUSED, char *argv[])
265 uint32_t num_packets, i, milestone;
266 struct timeval start, end;
268 open_nl_vconn(argv[1], false, &dp);
269 num_packets = atoi(argv[2]);
270 milestone = BENCHMARK_INCR;
271 run(dpif_benchmark_nl(&dp, num_packets, atoi(argv[3])), "benchmark_nl");
272 if (gettimeofday(&start, NULL) == -1) {
273 run(errno, "gettimeofday");
275 for (i = 0; i < num_packets;i++) {
277 run(dpif_recv_openflow(&dp, &b, true), "dpif_recv_openflow");
278 if (i == milestone) {
279 gettimeofday(&end, NULL);
280 printf("%u packets received in %f ms\n",
282 (1000*(double)(end.tv_sec - start.tv_sec))
283 + (.001*(end.tv_usec - start.tv_usec)));
284 milestone += BENCHMARK_INCR;
289 gettimeofday(&end, NULL);
290 printf("%u packets received in %f ms\n",
291 i - (milestone - BENCHMARK_INCR),
292 (1000*(double)(end.tv_sec - start.tv_sec))
293 + (.001*(end.tv_usec - start.tv_usec)));
297 #endif /* HAVE_NETLINK */
299 /* Generic commands. */
301 static void do_show(int argc UNUSED, char *argv[])
305 run(dpif_open(atoi(argv[1]), false, &dp), "dpif_open");
306 run(dpif_show(&dp), "show");
311 static void do_monitor(int argc UNUSED, char *argv[])
314 run(dpif_open(atoi(argv[1]), true, &dp), "dpif_open");
317 run(dpif_recv_openflow(&dp, &b, true), "dpif_recv_openflow");
318 ofp_print(stderr, b->data, b->size, 2);
323 static void do_dump_tables(int argc, char *argv[])
326 run(dpif_open(atoi(argv[1]), false, &dp), "dpif_open");
327 run(dpif_dump_tables(&dp), "dump_tables");
333 str_to_int(const char *str)
336 if (sscanf(str, "%"SCNu32, &value) != 1) {
337 fatal(0, "invalid numeric format %s", str);
343 str_to_mac(const char *str, uint8_t mac[6])
345 if (sscanf(str, "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8,
346 &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) != 6) {
347 fatal(0, "invalid mac address %s", str);
352 str_to_ip(const char *str, uint32_t *ip)
354 struct in_addr in_addr;
357 retval = lookup_ip(str, &in_addr);
359 fatal(0, "%s: could not convert to IP address", str);
361 *ip = in_addr.s_addr;
365 str_to_action(const char *str, struct ofp_action *action)
369 if (!strcasecmp(str, "flood")) {
371 } else if (!strcasecmp(str, "controller")) {
372 port = OFPP_CONTROLLER;
374 port = str_to_int(str);
377 memset(action, 0, sizeof *action);
378 action->type = OFPAT_OUTPUT;
379 action->arg.output.port = htons(port);
383 str_to_flow(char *string, struct ofp_match *match, struct ofp_action *action)
388 enum { F_U8, F_U16, F_MAC, F_IP } type;
392 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
393 static const struct field fields[] = {
394 { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port) },
395 { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan) },
396 { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src) },
397 { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst) },
398 { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type) },
399 { "nw_src", OFPFW_NW_SRC, F_IP, F_OFS(nw_src) },
400 { "nw_dst", OFPFW_NW_DST, F_IP, F_OFS(nw_dst) },
401 { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto) },
402 { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src) },
403 { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst) },
408 bool got_action = false;
410 memset(match, 0, sizeof *match);
411 wildcards = OFPFW_ALL;
412 for (name = strtok(string, "="), value = strtok(NULL, " \t\n");
414 name = strtok(NULL, "="), value = strtok(NULL, " \t\n"))
416 const struct field *f;
419 if (action && !strcmp(name, "action")) {
421 str_to_action(value, action);
425 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
426 if (!strcmp(f->name, name)) {
430 fprintf(stderr, "%s: unknown field %s (fields are",
432 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
436 fprintf(stderr, " %s", f->name);
438 fprintf(stderr, ")\n");
442 data = (char *) match + f->offset;
443 if (!strcmp(value, "*")) {
444 wildcards |= f->wildcard;
446 wildcards &= ~f->wildcard;
447 if (f->type == F_U8) {
448 *(uint8_t *) data = str_to_int(value);
449 } else if (f->type == F_U16) {
450 *(uint16_t *) data = htons(str_to_int(value));
451 } else if (f->type == F_MAC) {
452 str_to_mac(value, data);
453 } else if (f->type == F_IP) {
454 str_to_ip(value, data);
460 if (name && !value) {
461 fatal(0, "field %s missing value", name);
463 if (action && !got_action) {
464 fatal(0, "must specify an action");
466 match->wildcards = htons(wildcards);
469 static void do_dump_flows(int argc, char *argv[])
472 struct ofp_match match, *matchp;
473 run(dpif_open(atoi(argv[1]), false, &dp), "dpif_open");
475 str_to_flow(argv[3], &match, NULL);
480 run(dpif_dump_flows(&dp, atoi(argv[2]), matchp), "dump_flows");
484 static void do_add_flows(int argc, char *argv[])
494 file = fopen(argv[2], "r");
496 fatal(errno, "%s: open", argv[2]);
499 sprintf(vconn_name, "nl:%d", atoi(argv[1]));
500 retval = vconn_open(vconn_name, &vconn);
502 fatal(retval, "opening datapath");
505 while (fgets(line, sizeof line, file)) {
506 struct buffer *buffer;
507 struct ofp_flow_mod *ofm;
512 /* Delete comments. */
513 comment = strchr(line, '#');
518 /* Drop empty lines. */
519 if (line[strspn(line, " \t\n")] == '\0') {
523 size = sizeof *ofm + sizeof ofm->actions[0];
524 buffer = buffer_new(size);
525 ofm = buffer_put_uninit(buffer, size);
528 memset(ofm, 0, size);
529 ofm->header.type = OFPT_FLOW_MOD;
530 ofm->header.version = OFP_VERSION;
531 ofm->header.length = htons(size);
532 ofm->command = htons(OFPFC_ADD);
533 ofm->max_idle = htons(50);
534 ofm->buffer_id = htonl(UINT32_MAX);
535 ofm->group_id = htonl(0);
536 str_to_flow(line, &ofm->match, &ofm->actions[0]);
538 retval = vconn_send_block(vconn, buffer);
540 fatal(retval, "sending to datapath");
547 static void do_help(int argc UNUSED, char *argv[] UNUSED)
552 static struct command all_commands[] = {
554 { "adddp", 1, 1, do_add_dp },
555 { "deldp", 1, 1, do_del_dp },
556 { "addif", 2, 2, do_add_port },
557 { "delif", 2, 2, do_del_port },
558 { "benchmark-nl", 3, 3, do_benchmark_nl },
561 { "show", 1, 1, do_show },
563 { "help", 0, INT_MAX, do_help },
564 { "monitor", 1, 1, do_monitor },
565 { "dump-tables", 1, 1, do_dump_tables },
566 { "dump-flows", 2, 3, do_dump_flows },
567 { "add-flows", 2, 2, do_add_flows },