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>
44 #include "command-line.h"
50 #include "openflow-netlink.h"
53 #include "socket-util.h"
55 #include "ofp-print.h"
57 #include "vconn-ssl.h"
60 #define THIS_MODULE VLM_dpctl
62 static const char* ifconfigbin = "/sbin/ifconfig";
68 void (*handler)(int argc, char *argv[]);
71 static struct command all_commands[];
73 static void usage(void) NO_RETURN;
74 static void parse_options(int argc, char *argv[]);
76 int main(int argc, char *argv[])
80 set_program_name(argv[0]);
82 parse_options(argc, argv);
87 fatal(0, "missing command name; use --help for help");
89 for (p = all_commands; p->name != NULL; p++) {
90 if (!strcmp(p->name, argv[0])) {
92 if (n_arg < p->min_args)
93 fatal(0, "'%s' command requires at least %d arguments",
94 p->name, p->min_args);
95 else if (n_arg > p->max_args)
96 fatal(0, "'%s' command takes at most %d arguments",
97 p->name, p->max_args);
99 p->handler(argc, argv);
104 fatal(0, "unknown command '%s'; use --help for help", argv[0]);
110 parse_options(int argc, char *argv[])
112 static struct option long_options[] = {
113 {"verbose", optional_argument, 0, 'v'},
114 {"help", no_argument, 0, 'h'},
115 {"version", no_argument, 0, 'V'},
116 VCONN_SSL_LONG_OPTIONS
119 char *short_options = long_options_to_short_options(long_options);
125 c = getopt_long(argc, argv, short_options, long_options, &indexptr);
135 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
139 vlog_set_verbosity(optarg);
142 VCONN_SSL_OPTION_HANDLERS
157 printf("%s: OpenFlow switch management utility\n"
158 "usage: %s [OPTIONS] COMMAND [ARG...]\n"
160 "\nCommands that apply to local datapaths only:\n"
161 " adddp nl:DP_ID add a new local datapath DP_ID\n"
162 " deldp nl:DP_ID delete local datapath DP_ID\n"
163 " addif nl:DP_ID IFACE add IFACE as a port on DP_ID\n"
164 " delif nl:DP_ID IFACE delete IFACE as a port on DP_ID\n"
165 " monitor nl:DP_ID print packets received\n"
166 " benchmark-nl nl:DP_ID N SIZE send N packets of SIZE bytes\n"
168 "\nCommands that apply to local datapaths and remote switches:\n"
169 " show SWITCH show information\n"
170 " dump-tables SWITCH print table stats\n"
171 " dump-ports SWITCH print port statistics\n"
172 " dump-flows SWITCH print all flow entries\n"
173 " dump-flows SWITCH FLOW print matching FLOWs\n"
174 " add-flows SWITCH FILE add flows from FILE\n"
175 " del-flows SWITCH FLOW delete matching FLOWs\n"
176 "where each SWITCH is an active OpenFlow connection method.\n",
177 program_name, program_name);
178 vconn_usage(true, false);
179 printf("\nOptions:\n"
180 " -v, --verbose set maximum verbosity level\n"
181 " -h, --help display this help message\n"
182 " -V, --version display version information\n");
186 static void run(int retval, const char *message, ...)
189 static void run(int retval, const char *message, ...)
194 fprintf(stderr, "%s: ", program_name);
195 va_start(args, message);
196 vfprintf(stderr, message, args);
199 fputs(": unexpected end of file\n", stderr);
201 fprintf(stderr, ": %s\n", strerror(retval));
209 /* Netlink-only commands. */
211 static int if_up(const char* intf)
214 snprintf(command, sizeof command, "%s %s up &> /dev/null",
216 return system(command);
219 static void open_nl_vconn(const char *name, bool subscribe, struct dpif *dpif)
221 if (strncmp(name, "nl:", 3)
223 || name[strspn(name + 3, "0123456789") + 3]) {
224 fatal(0, "%s: argument is not of the form \"nl:DP_ID\"", name);
226 run(dpif_open(atoi(name + 3), subscribe, dpif), "opening datapath");
229 static void do_add_dp(int argc UNUSED, char *argv[])
232 open_nl_vconn(argv[1], false, &dp);
233 run(dpif_add_dp(&dp), "add_dp");
237 static void do_del_dp(int argc UNUSED, char *argv[])
240 open_nl_vconn(argv[1], false, &dp);
241 run(dpif_del_dp(&dp), "del_dp");
245 static void do_add_port(int argc UNUSED, char *argv[])
249 open_nl_vconn(argv[1], false, &dp);
250 run(dpif_add_port(&dp, argv[2]), "add_port");
254 static void do_del_port(int argc UNUSED, char *argv[])
257 open_nl_vconn(argv[1], false, &dp);
258 run(dpif_del_port(&dp, argv[2]), "del_port");
262 static void do_monitor(int argc UNUSED, char *argv[])
265 open_nl_vconn(argv[1], true, &dp);
268 run(dpif_recv_openflow(&dp, &b, true), "dpif_recv_openflow");
269 ofp_print(stderr, b->data, b->size, 2);
274 #define BENCHMARK_INCR 100
276 static void do_benchmark_nl(int argc UNUSED, char *argv[])
279 uint32_t num_packets, i, milestone;
280 struct timeval start, end;
282 open_nl_vconn(argv[1], false, &dp);
283 num_packets = atoi(argv[2]);
284 milestone = BENCHMARK_INCR;
285 run(dpif_benchmark_nl(&dp, num_packets, atoi(argv[3])), "benchmark_nl");
286 if (gettimeofday(&start, NULL) == -1) {
287 run(errno, "gettimeofday");
289 for (i = 0; i < num_packets;i++) {
291 run(dpif_recv_openflow(&dp, &b, true), "dpif_recv_openflow");
292 if (i == milestone) {
293 gettimeofday(&end, NULL);
294 printf("%u packets received in %f ms\n",
296 (1000*(double)(end.tv_sec - start.tv_sec))
297 + (.001*(end.tv_usec - start.tv_usec)));
298 milestone += BENCHMARK_INCR;
303 gettimeofday(&end, NULL);
304 printf("%u packets received in %f ms\n",
305 i - (milestone - BENCHMARK_INCR),
306 (1000*(double)(end.tv_sec - start.tv_sec))
307 + (.001*(end.tv_usec - start.tv_usec)));
311 #endif /* HAVE_NETLINK */
313 /* Generic commands. */
318 static bool inited = false;
322 if (gettimeofday(&tv, NULL) < 0) {
323 fatal(errno, "gettimeofday");
325 srand(tv.tv_sec ^ tv.tv_usec);
331 alloc_openflow_buffer(size_t openflow_len, uint8_t type,
332 struct buffer **bufferp)
334 struct buffer *buffer;
335 struct ofp_header *oh;
337 buffer = *bufferp = buffer_new(openflow_len);
338 oh = buffer_put_uninit(buffer, openflow_len);
339 memset(oh, 0, openflow_len);
340 oh->version = OFP_VERSION;
343 oh->xid = random_xid();
348 send_openflow_buffer(struct vconn *vconn, struct buffer *buffer)
350 struct ofp_header *oh;
352 oh = buffer_at_assert(buffer, 0, sizeof *oh);
353 oh->length = htons(buffer->size);
355 run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
358 static struct buffer *
359 transact_openflow(struct vconn *vconn, struct buffer *request)
361 uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
363 send_openflow_buffer(vconn, request);
366 struct buffer *reply;
368 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
369 recv_xid = ((struct ofp_header *) reply->data)->xid;
370 if (send_xid == recv_xid) {
374 VLOG_DBG("received reply with xid %08"PRIx32" != expected %08"PRIx32,
376 buffer_delete(reply);
381 dump_transaction(const char *vconn_name, uint8_t request_type)
384 struct buffer *request, *reply;
386 run(vconn_open_block(vconn_name, &vconn), "connecting to %s", vconn_name);
387 alloc_openflow_buffer(sizeof(struct ofp_header), request_type, &request);
388 reply = transact_openflow(vconn, request);
389 ofp_print(stdout, reply->data, reply->size, 1);
394 do_show(int argc UNUSED, char *argv[])
396 dump_transaction(argv[1], OFPT_FEATURES_REQUEST);
397 dump_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
402 do_dump_tables(int argc, char *argv[])
404 dump_transaction(argv[1], OFPT_TABLE_STATS_REQUEST);
409 str_to_int(const char *str)
412 if (sscanf(str, "%"SCNu32, &value) != 1) {
413 fatal(0, "invalid numeric format %s", str);
419 str_to_mac(const char *str, uint8_t mac[6])
421 if (sscanf(str, "%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8":%"SCNx8,
422 &mac[0], &mac[1], &mac[2], &mac[3], &mac[4], &mac[5]) != 6) {
423 fatal(0, "invalid mac address %s", str);
428 str_to_ip(const char *str, uint32_t *ip)
430 struct in_addr in_addr;
433 retval = lookup_ip(str, &in_addr);
435 fatal(0, "%s: could not convert to IP address", str);
437 *ip = in_addr.s_addr;
441 str_to_action(const char *str, struct ofp_action *action)
445 if (!strcasecmp(str, "flood")) {
447 } else if (!strcasecmp(str, "controller")) {
448 port = OFPP_CONTROLLER;
450 port = str_to_int(str);
453 memset(action, 0, sizeof *action);
454 action->type = OFPAT_OUTPUT;
455 action->arg.output.port = htons(port);
459 str_to_flow(char *string, struct ofp_match *match, struct ofp_action *action,
460 uint8_t *table_idx, uint16_t *priority)
465 enum { F_U8, F_U16, F_MAC, F_IP } type;
469 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
470 static const struct field fields[] = {
471 { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port) },
472 { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan) },
473 { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src) },
474 { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst) },
475 { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type) },
476 { "nw_src", OFPFW_NW_SRC, F_IP, F_OFS(nw_src) },
477 { "nw_dst", OFPFW_NW_DST, F_IP, F_OFS(nw_dst) },
478 { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto) },
479 { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src) },
480 { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst) },
485 bool got_action = false;
491 *priority = OFP_DEFAULT_PRIORITY;
493 memset(match, 0, sizeof *match);
494 wildcards = OFPFW_ALL;
495 for (name = strtok(string, "="), value = strtok(NULL, " \t\n");
497 name = strtok(NULL, "="), value = strtok(NULL, " \t\n"))
499 const struct field *f;
502 if (action && !strcmp(name, "action")) {
504 str_to_action(value, action);
508 if (table_idx && !strcmp(name, "table")) {
509 *table_idx = atoi(value);
513 if (priority && !strcmp(name, "priority")) {
514 *priority = atoi(value);
518 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
519 if (!strcmp(f->name, name)) {
523 fprintf(stderr, "%s: unknown field %s (fields are",
525 for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
529 fprintf(stderr, " %s", f->name);
531 fprintf(stderr, ")\n");
535 data = (char *) match + f->offset;
536 if (!strcmp(value, "*")) {
537 wildcards |= f->wildcard;
539 wildcards &= ~f->wildcard;
540 if (f->type == F_U8) {
541 *(uint8_t *) data = str_to_int(value);
542 } else if (f->type == F_U16) {
543 *(uint16_t *) data = htons(str_to_int(value));
544 } else if (f->type == F_MAC) {
545 str_to_mac(value, data);
546 } else if (f->type == F_IP) {
547 str_to_ip(value, data);
553 if (name && !value) {
554 fatal(0, "field %s missing value", name);
556 if (action && !got_action) {
557 fatal(0, "must specify an action");
559 match->wildcards = htons(wildcards);
562 static void do_dump_flows(int argc, char *argv[])
565 struct buffer *request;
566 struct ofp_flow_stats_request *req;
570 run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
571 req = alloc_openflow_buffer(sizeof *req, OFPT_FLOW_STATS_REQUEST,
573 str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL, &req->table_id,
575 req->type = OFPFS_INDIV;
578 send_xid = ((struct ofp_header *) request->data)->xid;
579 send_openflow_buffer(vconn, request);
582 struct buffer *reply;
584 run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
585 recv_xid = ((struct ofp_header *) reply->data)->xid;
586 if (send_xid == recv_xid) {
587 struct ofp_flow_stats_reply *rpy;
589 ofp_print(stdout, reply->data, reply->size, 1);
591 rpy = buffer_at(reply, 0, sizeof *rpy);
593 || rpy->header.version != OFP_VERSION
594 || rpy->header.type != OFPT_FLOW_STATS_REPLY
595 || (ntohs(rpy->header.length)
596 < sizeof rpy->header + sizeof *rpy->flows));
598 VLOG_DBG("received reply with xid %08"PRIx32" "
599 "!= expected %08"PRIx32, recv_xid, send_xid);
601 buffer_delete(reply);
606 static void do_add_flows(int argc, char *argv[])
613 file = fopen(argv[2], "r");
615 fatal(errno, "%s: open", argv[2]);
618 run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
619 while (fgets(line, sizeof line, file)) {
620 struct buffer *buffer;
621 struct ofp_flow_mod *ofm;
627 /* Delete comments. */
628 comment = strchr(line, '#');
633 /* Drop empty lines. */
634 if (line[strspn(line, " \t\n")] == '\0') {
638 /* Parse and send. */
639 size = sizeof *ofm + sizeof ofm->actions[0];
640 ofm = alloc_openflow_buffer(size, OFPT_FLOW_MOD, &buffer);
641 str_to_flow(line, &ofm->match, &ofm->actions[0], NULL, &priority);
642 ofm->command = htons(OFPFC_ADD);
643 ofm->max_idle = htons(50);
644 ofm->buffer_id = htonl(UINT32_MAX);
645 ofm->group_id = htonl(0);
646 ofm->priority = htons(priority);
647 ofm->reserved = htonl(0);
649 send_openflow_buffer(vconn, buffer);
655 static void do_del_flows(int argc, char *argv[])
659 run(vconn_open_block(argv[1], &vconn), "connecting to %s", argv[1]);
660 struct buffer *buffer;
661 struct ofp_flow_mod *ofm;
665 /* Parse and send. */
667 ofm = alloc_openflow_buffer(size, OFPT_FLOW_MOD, &buffer);
668 ofm->command = htons(OFPFC_DELETE);
669 ofm->max_idle = htons(0);
670 ofm->buffer_id = htonl(UINT32_MAX);
671 ofm->group_id = htonl(0);
672 ofm->priority = htons(0);
673 ofm->reserved = htonl(0);
674 str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL, NULL);
676 send_openflow_buffer(vconn, buffer);
682 do_dump_ports(int argc, char *argv[])
684 dump_transaction(argv[1], OFPT_PORT_STATS_REQUEST);
687 static void do_help(int argc UNUSED, char *argv[] UNUSED)
692 static struct command all_commands[] = {
694 { "adddp", 1, 1, do_add_dp },
695 { "deldp", 1, 1, do_del_dp },
696 { "addif", 2, 2, do_add_port },
697 { "delif", 2, 2, do_del_port },
698 { "benchmark-nl", 3, 3, do_benchmark_nl },
701 { "show", 1, 1, do_show },
703 { "help", 0, INT_MAX, do_help },
704 { "monitor", 1, 1, do_monitor },
705 { "dump-tables", 1, 1, do_dump_tables },
706 { "dump-flows", 1, 2, do_dump_flows },
707 { "add-flows", 2, 2, do_add_flows },
708 { "del-flows", 1, 2, do_del_flows },
709 { "dump-ports", 1, 1, do_dump_ports },
710 { NULL, 0, 0, NULL },