ofproto: Add selective Flow Removed messages and flow deletes (OpenFlow 0.9)
[sliver-openvswitch.git] / utilities / ovs-ofctl.c
1 /*
2  * Copyright (c) 2008, 2009 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 <net/if.h>
23 #include <netinet/in.h>
24 #include <signal.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31
32 #include "command-line.h"
33 #include "compiler.h"
34 #include "dirs.h"
35 #include "dpif.h"
36 #include "dynamic-string.h"
37 #include "netdev.h"
38 #include "netlink.h"
39 #include "odp-util.h"
40 #include "ofp-print.h"
41 #include "ofpbuf.h"
42 #include "openflow/nicira-ext.h"
43 #include "openflow/openflow.h"
44 #include "packets.h"
45 #include "random.h"
46 #include "socket-util.h"
47 #include "timeval.h"
48 #include "util.h"
49 #include "vconn-ssl.h"
50 #include "vconn.h"
51
52 #include "vlog.h"
53 #define THIS_MODULE VLM_ofctl
54
55 #define DEFAULT_IDLE_TIMEOUT 60
56
57 #define MOD_PORT_CMD_UP      "up"
58 #define MOD_PORT_CMD_DOWN    "down"
59 #define MOD_PORT_CMD_FLOOD   "flood"
60 #define MOD_PORT_CMD_NOFLOOD "noflood"
61
62
63 /* Settings that may be configured by the user. */
64 struct settings {
65     bool strict;        /* Use strict matching for flow mod commands */
66 };
67
68 struct command {
69     const char *name;
70     int min_args;
71     int max_args;
72     void (*handler)(const struct settings *, int argc, char *argv[]);
73 };
74
75 static struct command all_commands[];
76
77 static void usage(void) NO_RETURN;
78 static void parse_options(int argc, char *argv[], struct settings *);
79
80 int main(int argc, char *argv[])
81 {
82     struct settings s;
83     struct command *p;
84
85     set_program_name(argv[0]);
86     time_init();
87     vlog_init();
88     parse_options(argc, argv, &s);
89     signal(SIGPIPE, SIG_IGN);
90
91     argc -= optind;
92     argv += optind;
93     if (argc < 1)
94         ovs_fatal(0, "missing command name; use --help for help");
95
96     for (p = all_commands; p->name != NULL; p++) {
97         if (!strcmp(p->name, argv[0])) {
98             int n_arg = argc - 1;
99             if (n_arg < p->min_args)
100                 ovs_fatal(0, "'%s' command requires at least %d arguments",
101                           p->name, p->min_args);
102             else if (n_arg > p->max_args)
103                 ovs_fatal(0, "'%s' command takes at most %d arguments",
104                           p->name, p->max_args);
105             else {
106                 p->handler(&s, argc, argv);
107                 if (ferror(stdout)) {
108                     ovs_fatal(0, "write to stdout failed");
109                 }
110                 if (ferror(stderr)) {
111                     ovs_fatal(0, "write to stderr failed");
112                 }
113                 exit(0);
114             }
115         }
116     }
117     ovs_fatal(0, "unknown command '%s'; use --help for help", argv[0]);
118
119     return 0;
120 }
121
122 static void
123 parse_options(int argc, char *argv[], struct settings *s)
124 {
125     enum {
126         OPT_STRICT = UCHAR_MAX + 1,
127         VLOG_OPTION_ENUMS
128     };
129     static struct option long_options[] = {
130         {"timeout", required_argument, 0, 't'},
131         {"strict", no_argument, 0, OPT_STRICT},
132         {"help", no_argument, 0, 'h'},
133         {"version", no_argument, 0, 'V'},
134         VLOG_LONG_OPTIONS,
135         VCONN_SSL_LONG_OPTIONS
136         {0, 0, 0, 0},
137     };
138     char *short_options = long_options_to_short_options(long_options);
139
140     /* Set defaults that we can figure out before parsing options. */
141     s->strict = false;
142
143     for (;;) {
144         unsigned long int timeout;
145         int c;
146
147         c = getopt_long(argc, argv, short_options, long_options, NULL);
148         if (c == -1) {
149             break;
150         }
151
152         switch (c) {
153         case 't':
154             timeout = strtoul(optarg, NULL, 10);
155             if (timeout <= 0) {
156                 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
157                           optarg);
158             } else {
159                 time_alarm(timeout);
160             }
161             break;
162
163         case 'h':
164             usage();
165
166         case 'V':
167             OVS_PRINT_VERSION(OFP_VERSION, OFP_VERSION);
168             exit(EXIT_SUCCESS);
169
170         case OPT_STRICT:
171             s->strict = true;
172             break;
173
174         VLOG_OPTION_HANDLERS
175         VCONN_SSL_OPTION_HANDLERS
176
177         case '?':
178             exit(EXIT_FAILURE);
179
180         default:
181             abort();
182         }
183     }
184     free(short_options);
185 }
186
187 static void
188 usage(void)
189 {
190     printf("%s: OpenFlow switch management utility\n"
191            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
192            "\nFor OpenFlow switches:\n"
193            "  show SWITCH                 show OpenFlow information\n"
194            "  status SWITCH [KEY]         report statistics (about KEY)\n"
195            "  dump-desc SWITCH            print switch description\n"
196            "  dump-tables SWITCH          print table stats\n"
197            "  mod-port SWITCH IFACE ACT   modify port behavior\n"
198            "  dump-ports SWITCH           print port statistics\n"
199            "  dump-flows SWITCH           print all flow entries\n"
200            "  dump-flows SWITCH FLOW      print matching FLOWs\n"
201            "  dump-aggregate SWITCH       print aggregate flow statistics\n"
202            "  dump-aggregate SWITCH FLOW  print aggregate stats for FLOWs\n"
203            "  add-flow SWITCH FLOW        add flow described by FLOW\n"
204            "  add-flows SWITCH FILE       add flows from FILE\n"
205            "  mod-flows SWITCH FLOW       modify actions of matching FLOWs\n"
206            "  del-flows SWITCH [FLOW]     delete matching FLOWs\n"
207            "  monitor SWITCH MISSLEN EXP  print packets received from SWITCH\n"
208            "  execute SWITCH CMD [ARG...] execute CMD with ARGS on SWITCH\n"
209            "\nFor OpenFlow switches and controllers:\n"
210            "  probe VCONN                 probe whether VCONN is up\n"
211            "  ping VCONN [N]              latency of N-byte echos\n"
212            "  benchmark VCONN N COUNT     bandwidth of COUNT N-byte echos\n"
213            "where each SWITCH is an active OpenFlow connection method.\n",
214            program_name, program_name);
215     vconn_usage(true, false, false);
216     vlog_usage();
217     printf("\nOther options:\n"
218            "  --strict                    use strict match for flow commands\n"
219            "  -t, --timeout=SECS          give up after SECS seconds\n"
220            "  -h, --help                  display this help message\n"
221            "  -V, --version               display version information\n");
222     exit(EXIT_SUCCESS);
223 }
224
225 static void run(int retval, const char *message, ...)
226     PRINTF_FORMAT(2, 3);
227
228 static void run(int retval, const char *message, ...)
229 {
230     if (retval) {
231         va_list args;
232
233         fprintf(stderr, "%s: ", program_name);
234         va_start(args, message);
235         vfprintf(stderr, message, args);
236         va_end(args);
237         if (retval == EOF) {
238             fputs(": unexpected end of file\n", stderr);
239         } else {
240             fprintf(stderr, ": %s\n", strerror(retval));
241         }
242
243         exit(EXIT_FAILURE);
244     }
245 }
246 \f
247 /* Generic commands. */
248
249 static void
250 open_vconn(const char *name, struct vconn **vconnp)
251 {
252     struct dpif *dpif;
253     struct stat s;
254
255     if (strstr(name, ":")) {
256         run(vconn_open_block(name, OFP_VERSION, vconnp),
257             "connecting to %s", name);
258     } else if (!stat(name, &s) && S_ISSOCK(s.st_mode)) {
259         char *vconn_name = xasprintf("unix:%s", name);
260         VLOG_INFO("connecting to %s", vconn_name);
261         run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
262             "connecting to %s", vconn_name);
263         free(vconn_name);
264     } else if (!dpif_open(name, &dpif)) {
265         char dpif_name[IF_NAMESIZE + 1];
266         char *socket_name;
267         char *vconn_name;
268
269         run(dpif_port_get_name(dpif, ODPP_LOCAL, dpif_name, sizeof dpif_name),
270             "obtaining name of %s", dpif_name);
271         dpif_close(dpif);
272         if (strcmp(dpif_name, name)) {
273             VLOG_INFO("datapath %s is named %s", name, dpif_name);
274         }
275
276         socket_name = xasprintf("%s/%s.mgmt", ovs_rundir, dpif_name);
277         if (stat(socket_name, &s)) {
278             ovs_fatal(errno, "cannot connect to %s: stat failed on %s",
279                       name, socket_name);
280         } else if (!S_ISSOCK(s.st_mode)) {
281             ovs_fatal(0, "cannot connect to %s: %s is not a socket",
282                       name, socket_name);
283         }
284
285         vconn_name = xasprintf("unix:%s", socket_name);
286         VLOG_INFO("connecting to %s", vconn_name);
287         run(vconn_open_block(vconn_name, OFP_VERSION, vconnp),
288             "connecting to %s", vconn_name);
289         free(socket_name);
290         free(vconn_name);
291     } else {
292         ovs_fatal(0, "%s is not a valid connection method", name);
293     }
294 }
295
296 static void *
297 alloc_stats_request(size_t body_len, uint16_t type, struct ofpbuf **bufferp)
298 {
299     struct ofp_stats_request *rq;
300     rq = make_openflow((offsetof(struct ofp_stats_request, body)
301                         + body_len), OFPT_STATS_REQUEST, bufferp);
302     rq->type = htons(type);
303     rq->flags = htons(0);
304     return rq->body;
305 }
306
307 static void
308 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
309 {
310     update_openflow_length(buffer);
311     run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
312 }
313
314 static void
315 dump_transaction(const char *vconn_name, struct ofpbuf *request)
316 {
317     struct vconn *vconn;
318     struct ofpbuf *reply;
319
320     update_openflow_length(request);
321     open_vconn(vconn_name, &vconn);
322     run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
323     ofp_print(stdout, reply->data, reply->size, 1);
324     vconn_close(vconn);
325 }
326
327 static void
328 dump_trivial_transaction(const char *vconn_name, uint8_t request_type)
329 {
330     struct ofpbuf *request;
331     make_openflow(sizeof(struct ofp_header), request_type, &request);
332     dump_transaction(vconn_name, request);
333 }
334
335 static void
336 dump_stats_transaction(const char *vconn_name, struct ofpbuf *request)
337 {
338     uint32_t send_xid = ((struct ofp_header *) request->data)->xid;
339     struct vconn *vconn;
340     bool done = false;
341
342     open_vconn(vconn_name, &vconn);
343     send_openflow_buffer(vconn, request);
344     while (!done) {
345         uint32_t recv_xid;
346         struct ofpbuf *reply;
347
348         run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
349         recv_xid = ((struct ofp_header *) reply->data)->xid;
350         if (send_xid == recv_xid) {
351             struct ofp_stats_reply *osr;
352
353             ofp_print(stdout, reply->data, reply->size, 1);
354
355             osr = ofpbuf_at(reply, 0, sizeof *osr);
356             done = !osr || !(ntohs(osr->flags) & OFPSF_REPLY_MORE);
357         } else {
358             VLOG_DBG("received reply with xid %08"PRIx32" "
359                      "!= expected %08"PRIx32, recv_xid, send_xid);
360         }
361         ofpbuf_delete(reply);
362     }
363     vconn_close(vconn);
364 }
365
366 static void
367 dump_trivial_stats_transaction(const char *vconn_name, uint8_t stats_type)
368 {
369     struct ofpbuf *request;
370     alloc_stats_request(0, stats_type, &request);
371     dump_stats_transaction(vconn_name, request);
372 }
373
374 static void
375 do_show(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
376 {
377     dump_trivial_transaction(argv[1], OFPT_FEATURES_REQUEST);
378     dump_trivial_transaction(argv[1], OFPT_GET_CONFIG_REQUEST);
379 }
380
381 static void
382 do_status(const struct settings *s UNUSED, int argc, char *argv[])
383 {
384     struct nicira_header *request, *reply;
385     struct vconn *vconn;
386     struct ofpbuf *b;
387
388     request = make_openflow(sizeof *request, OFPT_VENDOR, &b);
389     request->vendor = htonl(NX_VENDOR_ID);
390     request->subtype = htonl(NXT_STATUS_REQUEST);
391     if (argc > 2) {
392         ofpbuf_put(b, argv[2], strlen(argv[2]));
393         update_openflow_length(b);
394     }
395     open_vconn(argv[1], &vconn);
396     run(vconn_transact(vconn, b, &b), "talking to %s", argv[1]);
397     vconn_close(vconn);
398
399     if (b->size < sizeof *reply) {
400         ovs_fatal(0, "short reply (%zu bytes)", b->size);
401     }
402     reply = b->data;
403     if (reply->header.type != OFPT_VENDOR
404         || reply->vendor != ntohl(NX_VENDOR_ID)
405         || reply->subtype != ntohl(NXT_STATUS_REPLY)) {
406         ofp_print(stderr, b->data, b->size, 2);
407         ovs_fatal(0, "bad reply");
408     }
409
410     fwrite(reply + 1, b->size - sizeof *reply, 1, stdout);
411 }
412
413 static void
414 do_dump_desc(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
415 {
416     dump_trivial_stats_transaction(argv[1], OFPST_DESC);
417 }
418
419 static void
420 do_dump_tables(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
421 {
422     dump_trivial_stats_transaction(argv[1], OFPST_TABLE);
423 }
424
425
426 static uint32_t
427 str_to_u32(const char *str) 
428 {
429     char *tail;
430     uint32_t value;
431
432     errno = 0;
433     value = strtoul(str, &tail, 0);
434     if (errno == EINVAL || errno == ERANGE || *tail) {
435         ovs_fatal(0, "invalid numeric format %s", str);
436     }
437     return value;
438 }
439
440 static void
441 str_to_mac(const char *str, uint8_t mac[6]) 
442 {
443     if (sscanf(str, ETH_ADDR_SCAN_FMT, ETH_ADDR_SCAN_ARGS(mac))
444         != ETH_ADDR_SCAN_COUNT) {
445         ovs_fatal(0, "invalid mac address %s", str);
446     }
447 }
448
449 static uint32_t
450 str_to_ip(const char *str_, uint32_t *ip)
451 {
452     char *str = xstrdup(str_);
453     char *save_ptr = NULL;
454     const char *name, *netmask;
455     struct in_addr in_addr;
456     int n_wild, retval;
457
458     name = strtok_r(str, "/", &save_ptr);
459     retval = name ? lookup_ip(name, &in_addr) : EINVAL;
460     if (retval) {
461         ovs_fatal(0, "%s: could not convert to IP address", str);
462     }
463     *ip = in_addr.s_addr;
464
465     netmask = strtok_r(NULL, "/", &save_ptr);
466     if (netmask) {
467         uint8_t o[4];
468         if (sscanf(netmask, "%"SCNu8".%"SCNu8".%"SCNu8".%"SCNu8,
469                    &o[0], &o[1], &o[2], &o[3]) == 4) {
470             uint32_t nm = (o[0] << 24) | (o[1] << 16) | (o[2] << 8) | o[3];
471             int i;
472
473             /* Find first 1-bit. */
474             for (i = 0; i < 32; i++) {
475                 if (nm & (1u << i)) {
476                     break;
477                 }
478             }
479             n_wild = i;
480
481             /* Verify that the rest of the bits are 1-bits. */
482             for (; i < 32; i++) {
483                 if (!(nm & (1u << i))) {
484                     ovs_fatal(0, "%s: %s is not a valid netmask",
485                               str, netmask);
486                 }
487             }
488         } else {
489             int prefix = atoi(netmask);
490             if (prefix <= 0 || prefix > 32) {
491                 ovs_fatal(0, "%s: network prefix bits not between 1 and 32",
492                           str);
493             }
494             n_wild = 32 - prefix;
495         }
496     } else {
497         n_wild = 0;
498     }
499
500     free(str);
501     return n_wild;
502 }
503
504 static void *
505 put_action(struct ofpbuf *b, size_t size, uint16_t type)
506 {
507     struct ofp_action_header *ah = ofpbuf_put_zeros(b, size);
508     ah->type = htons(type);
509     ah->len = htons(size);
510     return ah;
511 }
512
513 static struct ofp_action_output *
514 put_output_action(struct ofpbuf *b, uint16_t port)
515 {
516     struct ofp_action_output *oao = put_action(b, sizeof *oao, OFPAT_OUTPUT);
517     oao->port = htons(port);
518     return oao;
519 }
520
521 static void
522 put_dl_addr_action(struct ofpbuf *b, uint16_t type, const char *addr)
523 {
524     struct ofp_action_dl_addr *oada = put_action(b, sizeof *oada, type);
525     str_to_mac(addr, oada->dl_addr);
526 }
527
528
529 static bool
530 parse_port_name(const char *name, uint16_t *port)
531 {
532     struct pair {
533         const char *name;
534         uint16_t value;
535     };
536     static const struct pair pairs[] = {
537 #define DEF_PAIR(NAME) {#NAME, OFPP_##NAME}
538         DEF_PAIR(IN_PORT),
539         DEF_PAIR(TABLE),
540         DEF_PAIR(NORMAL),
541         DEF_PAIR(FLOOD),
542         DEF_PAIR(ALL),
543         DEF_PAIR(CONTROLLER),
544         DEF_PAIR(LOCAL),
545         DEF_PAIR(NONE),
546 #undef DEF_PAIR
547     };
548     static const int n_pairs = ARRAY_SIZE(pairs);
549     size_t i;
550
551     for (i = 0; i < n_pairs; i++) {
552         if (!strcasecmp(name, pairs[i].name)) {
553             *port = pairs[i].value;
554             return true;
555         }
556     }
557     return false;
558 }
559
560 static void
561 str_to_action(char *str, struct ofpbuf *b)
562 {
563     char *act, *arg;
564     char *saveptr = NULL;
565     bool drop = false;
566     int n_actions;
567
568     for (act = strtok_r(str, ", \t\r\n", &saveptr), n_actions = 0; act;
569          act = strtok_r(NULL, ", \t\r\n", &saveptr), n_actions++) 
570     {
571         uint16_t port;
572
573         if (drop) {
574             ovs_fatal(0, "Drop actions must not be followed by other actions");
575         }
576
577         /* Arguments are separated by colons */
578         arg = strchr(act, ':');
579         if (arg) {
580             *arg = '\0';
581             arg++;
582         }
583
584         if (!strcasecmp(act, "mod_vlan_vid")) {
585             struct ofp_action_vlan_vid *va;
586             va = put_action(b, sizeof *va, OFPAT_SET_VLAN_VID);
587             va->vlan_vid = htons(str_to_u32(arg));
588         } else if (!strcasecmp(act, "mod_vlan_pcp")) {
589             struct ofp_action_vlan_pcp *va;
590             va = put_action(b, sizeof *va, OFPAT_SET_VLAN_PCP);
591             va->vlan_pcp = str_to_u32(arg);
592         } else if (!strcasecmp(act, "strip_vlan")) {
593             struct ofp_action_header *ah;
594             ah = put_action(b, sizeof *ah, OFPAT_STRIP_VLAN);
595             ah->type = htons(OFPAT_STRIP_VLAN);
596         } else if (!strcasecmp(act, "mod_dl_src")) {
597             put_dl_addr_action(b, OFPAT_SET_DL_SRC, arg);
598         } else if (!strcasecmp(act, "mod_dl_dst")) {
599             put_dl_addr_action(b, OFPAT_SET_DL_DST, arg);
600         } else if (!strcasecmp(act, "mod_nw_src")) {
601             struct ofp_action_nw_addr *na;
602             na = put_action(b, sizeof *na, OFPAT_SET_NW_SRC);
603             str_to_ip(arg, &na->nw_addr);
604         } else if (!strcasecmp(act, "mod_nw_dst")) {
605             struct ofp_action_nw_addr *na;
606             na = put_action(b, sizeof *na, OFPAT_SET_NW_DST);
607             str_to_ip(arg, &na->nw_addr);
608         } else if (!strcasecmp(act, "mod_tp_src")) {
609             struct ofp_action_tp_port *ta;
610             ta = put_action(b, sizeof *ta, OFPAT_SET_TP_SRC);
611             ta->tp_port = htons(str_to_u32(arg));
612         } else if (!strcasecmp(act, "mod_tp_dst")) {
613             struct ofp_action_tp_port *ta;
614             ta = put_action(b, sizeof *ta, OFPAT_SET_TP_DST);
615             ta->tp_port = htons(str_to_u32(arg));
616         } else if (!strcasecmp(act, "mod_nw_tos")) {
617             struct ofp_action_nw_tos *nt;
618             nt = put_action(b, sizeof *nt, OFPAT_SET_NW_TOS);
619             nt->nw_tos = str_to_u32(arg);
620         } else if (!strcasecmp(act, "output")) {
621             put_output_action(b, str_to_u32(arg));
622         } else if (!strcasecmp(act, "drop")) {
623             /* A drop action in OpenFlow occurs by just not setting 
624              * an action. */
625             drop = true;
626             if (n_actions) {
627                 ovs_fatal(0, "Drop actions must not be preceded by other "
628                           "actions");
629             }
630         } else if (!strcasecmp(act, "CONTROLLER")) {
631             struct ofp_action_output *oao;
632             oao = put_output_action(b, OFPP_CONTROLLER);
633
634             /* Unless a numeric argument is specified, we send the whole
635              * packet to the controller. */
636             if (arg && (strspn(act, "0123456789") == strlen(act))) {
637                oao->max_len = htons(str_to_u32(arg));
638             } else {
639                 oao->max_len = htons(UINT16_MAX);
640             }
641         } else if (parse_port_name(act, &port)) {
642             put_output_action(b, port);
643         } else if (strspn(act, "0123456789") == strlen(act)) {
644             put_output_action(b, str_to_u32(act));
645         } else {
646             ovs_fatal(0, "Unknown action: %s", act);
647         }
648     }
649 }
650
651 struct protocol {
652     const char *name;
653     uint16_t dl_type;
654     uint8_t nw_proto;
655 };
656
657 static bool
658 parse_protocol(const char *name, const struct protocol **p_out)
659 {
660     static const struct protocol protocols[] = {
661         { "ip", ETH_TYPE_IP, 0 },
662         { "arp", ETH_TYPE_ARP, 0 },
663         { "icmp", ETH_TYPE_IP, IP_TYPE_ICMP },
664         { "tcp", ETH_TYPE_IP, IP_TYPE_TCP },
665         { "udp", ETH_TYPE_IP, IP_TYPE_UDP },
666     };
667     const struct protocol *p;
668
669     for (p = protocols; p < &protocols[ARRAY_SIZE(protocols)]; p++) {
670         if (!strcmp(p->name, name)) {
671             *p_out = p;
672             return true;
673         }
674     }
675     *p_out = NULL;
676     return false;
677 }
678
679 struct field {
680     const char *name;
681     uint32_t wildcard;
682     enum { F_U8, F_U16, F_MAC, F_IP } type;
683     size_t offset, shift;
684 };
685
686 static bool
687 parse_field(const char *name, const struct field **f_out) 
688 {
689 #define F_OFS(MEMBER) offsetof(struct ofp_match, MEMBER)
690     static const struct field fields[] = { 
691         { "in_port", OFPFW_IN_PORT, F_U16, F_OFS(in_port), 0 },
692         { "dl_vlan", OFPFW_DL_VLAN, F_U16, F_OFS(dl_vlan), 0 },
693         { "dl_vlan_pcp", OFPFW_DL_VLAN_PCP, F_U8, F_OFS(dl_vlan_pcp), 0 },
694         { "dl_src", OFPFW_DL_SRC, F_MAC, F_OFS(dl_src), 0 },
695         { "dl_dst", OFPFW_DL_DST, F_MAC, F_OFS(dl_dst), 0 },
696         { "dl_type", OFPFW_DL_TYPE, F_U16, F_OFS(dl_type), 0 },
697         { "nw_src", OFPFW_NW_SRC_MASK, F_IP,
698           F_OFS(nw_src), OFPFW_NW_SRC_SHIFT },
699         { "nw_dst", OFPFW_NW_DST_MASK, F_IP,
700           F_OFS(nw_dst), OFPFW_NW_DST_SHIFT },
701         { "nw_proto", OFPFW_NW_PROTO, F_U8, F_OFS(nw_proto), 0 },
702         { "tp_src", OFPFW_TP_SRC, F_U16, F_OFS(tp_src), 0 },
703         { "tp_dst", OFPFW_TP_DST, F_U16, F_OFS(tp_dst), 0 },
704         { "icmp_type", OFPFW_ICMP_TYPE, F_U16, F_OFS(icmp_type), 0 },
705         { "icmp_code", OFPFW_ICMP_CODE, F_U16, F_OFS(icmp_code), 0 }
706     };
707     const struct field *f;
708
709     for (f = fields; f < &fields[ARRAY_SIZE(fields)]; f++) {
710         if (!strcmp(f->name, name)) {
711             *f_out = f;
712             return true;
713         }
714     }
715     *f_out = NULL;
716     return false;
717 }
718
719 static void
720 str_to_flow(char *string, struct ofp_match *match, struct ofpbuf *actions,
721             uint8_t *table_idx, uint16_t *out_port, uint16_t *priority, 
722             uint16_t *idle_timeout, uint16_t *hard_timeout)
723 {
724     char *save_ptr = NULL;
725     char *name;
726     uint32_t wildcards;
727
728     if (table_idx) {
729         *table_idx = 0xff;
730     }
731     if (out_port) {
732         *out_port = OFPP_NONE;
733     }
734     if (priority) {
735         *priority = OFP_DEFAULT_PRIORITY;
736     }
737     if (idle_timeout) {
738         *idle_timeout = DEFAULT_IDLE_TIMEOUT;
739     }
740     if (hard_timeout) {
741         *hard_timeout = OFP_FLOW_PERMANENT;
742     }
743     if (actions) {
744         char *act_str = strstr(string, "action");
745         if (!act_str) {
746             ovs_fatal(0, "must specify an action");
747         }
748         *(act_str-1) = '\0';
749
750         act_str = strchr(act_str, '=');
751         if (!act_str) {
752             ovs_fatal(0, "must specify an action");
753         }
754
755         act_str++;
756
757         str_to_action(act_str, actions);
758     }
759     memset(match, 0, sizeof *match);
760     wildcards = OFPFW_ALL;
761     for (name = strtok_r(string, "=, \t\r\n", &save_ptr); name;
762          name = strtok_r(NULL, "=, \t\r\n", &save_ptr)) {
763         const struct protocol *p;
764
765         if (parse_protocol(name, &p)) {
766             wildcards &= ~OFPFW_DL_TYPE;
767             match->dl_type = htons(p->dl_type);
768             if (p->nw_proto) {
769                 wildcards &= ~OFPFW_NW_PROTO;
770                 match->nw_proto = p->nw_proto;
771             }
772         } else {
773             const struct field *f;
774             char *value;
775
776             value = strtok_r(NULL, ", \t\r\n", &save_ptr);
777             if (!value) {
778                 ovs_fatal(0, "field %s missing value", name);
779             }
780         
781             if (table_idx && !strcmp(name, "table")) {
782                 *table_idx = atoi(value);
783             } else if (out_port && !strcmp(name, "out_port")) {
784                 *out_port = atoi(value);
785             } else if (priority && !strcmp(name, "priority")) {
786                 *priority = atoi(value);
787             } else if (idle_timeout && !strcmp(name, "idle_timeout")) {
788                 *idle_timeout = atoi(value);
789             } else if (hard_timeout && !strcmp(name, "hard_timeout")) {
790                 *hard_timeout = atoi(value);
791             } else if (parse_field(name, &f)) {
792                 void *data = (char *) match + f->offset;
793                 if (!strcmp(value, "*") || !strcmp(value, "ANY")) {
794                     wildcards |= f->wildcard;
795                 } else {
796                     wildcards &= ~f->wildcard;
797                     if (f->wildcard == OFPFW_IN_PORT
798                         && parse_port_name(value, (uint16_t *) data)) {
799                         /* Nothing to do. */
800                     } else if (f->type == F_U8) {
801                         *(uint8_t *) data = str_to_u32(value);
802                     } else if (f->type == F_U16) {
803                         *(uint16_t *) data = htons(str_to_u32(value));
804                     } else if (f->type == F_MAC) {
805                         str_to_mac(value, data);
806                     } else if (f->type == F_IP) {
807                         wildcards |= str_to_ip(value, data) << f->shift;
808                     } else {
809                         NOT_REACHED();
810                     }
811                 }
812             } else {
813                 ovs_fatal(0, "unknown keyword %s", name);
814             }
815         }
816     }
817     match->wildcards = htonl(wildcards);
818 }
819
820 static void
821 do_dump_flows(const struct settings *s UNUSED, int argc, char *argv[])
822 {
823     struct ofp_flow_stats_request *req;
824     uint16_t out_port;
825     struct ofpbuf *request;
826
827     req = alloc_stats_request(sizeof *req, OFPST_FLOW, &request);
828     str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
829                 &req->table_id, &out_port, NULL, NULL, NULL);
830     memset(&req->pad, 0, sizeof req->pad);
831     req->out_port = htons(out_port);
832
833     dump_stats_transaction(argv[1], request);
834 }
835
836 static void
837 do_dump_aggregate(const struct settings *s UNUSED, int argc, char *argv[])
838 {
839     struct ofp_aggregate_stats_request *req;
840     struct ofpbuf *request;
841     uint16_t out_port;
842
843     req = alloc_stats_request(sizeof *req, OFPST_AGGREGATE, &request);
844     str_to_flow(argc > 2 ? argv[2] : "", &req->match, NULL,
845                 &req->table_id, &out_port, NULL, NULL, NULL);
846     memset(&req->pad, 0, sizeof req->pad);
847     req->out_port = htons(out_port);
848
849     dump_stats_transaction(argv[1], request);
850 }
851
852 static void
853 do_add_flow(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
854 {
855     struct vconn *vconn;
856     struct ofpbuf *buffer;
857     struct ofp_flow_mod *ofm;
858     uint16_t priority, idle_timeout, hard_timeout;
859     struct ofp_match match;
860
861     /* Parse and send.  str_to_flow() will expand and reallocate the data in
862      * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
863     make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
864     str_to_flow(argv[2], &match, buffer,
865                 NULL, NULL, &priority, &idle_timeout, &hard_timeout);
866     ofm = buffer->data;
867     ofm->match = match;
868     ofm->command = htons(OFPFC_ADD);
869     ofm->idle_timeout = htons(idle_timeout);
870     ofm->hard_timeout = htons(hard_timeout);
871     ofm->buffer_id = htonl(UINT32_MAX);
872     ofm->priority = htons(priority);
873     ofm->reserved = htonl(0);
874
875     open_vconn(argv[1], &vconn);
876     send_openflow_buffer(vconn, buffer);
877     vconn_close(vconn);
878 }
879
880 static void
881 do_add_flows(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
882 {
883     struct vconn *vconn;
884     FILE *file;
885     char line[1024];
886
887     file = fopen(argv[2], "r");
888     if (file == NULL) {
889         ovs_fatal(errno, "%s: open", argv[2]);
890     }
891
892     open_vconn(argv[1], &vconn);
893     while (fgets(line, sizeof line, file)) {
894         struct ofpbuf *buffer;
895         struct ofp_flow_mod *ofm;
896         uint16_t priority, idle_timeout, hard_timeout;
897         struct ofp_match match;
898
899         char *comment;
900
901         /* Delete comments. */
902         comment = strchr(line, '#');
903         if (comment) {
904             *comment = '\0';
905         }
906
907         /* Drop empty lines. */
908         if (line[strspn(line, " \t\n")] == '\0') {
909             continue;
910         }
911
912         /* Parse and send.  str_to_flow() will expand and reallocate the data
913          * in 'buffer', so we can't keep pointers to across the str_to_flow()
914          * call. */
915         ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
916         str_to_flow(line, &match, buffer,
917                     NULL, NULL, &priority, &idle_timeout, &hard_timeout);
918         ofm = buffer->data;
919         ofm->match = match;
920         ofm->command = htons(OFPFC_ADD);
921         ofm->idle_timeout = htons(idle_timeout);
922         ofm->hard_timeout = htons(hard_timeout);
923         ofm->buffer_id = htonl(UINT32_MAX);
924         ofm->priority = htons(priority);
925         ofm->reserved = htonl(0);
926
927         send_openflow_buffer(vconn, buffer);
928     }
929     vconn_close(vconn);
930     fclose(file);
931 }
932
933 static void
934 do_mod_flows(const struct settings *s, int argc UNUSED, char *argv[])
935 {
936     uint16_t priority, idle_timeout, hard_timeout;
937     struct vconn *vconn;
938     struct ofpbuf *buffer;
939     struct ofp_flow_mod *ofm;
940     struct ofp_match match;
941
942     /* Parse and send.  str_to_flow() will expand and reallocate the data in
943      * 'buffer', so we can't keep pointers to across the str_to_flow() call. */
944     make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
945     str_to_flow(argv[2], &match, buffer,
946                 NULL, NULL, &priority, &idle_timeout, &hard_timeout);
947     ofm = buffer->data;
948     ofm->match = match;
949     if (s->strict) {
950         ofm->command = htons(OFPFC_MODIFY_STRICT);
951     } else {
952         ofm->command = htons(OFPFC_MODIFY);
953     }
954     ofm->idle_timeout = htons(idle_timeout);
955     ofm->hard_timeout = htons(hard_timeout);
956     ofm->buffer_id = htonl(UINT32_MAX);
957     ofm->priority = htons(priority);
958     ofm->reserved = htonl(0);
959
960     open_vconn(argv[1], &vconn);
961     send_openflow_buffer(vconn, buffer);
962     vconn_close(vconn);
963 }
964
965 static void do_del_flows(const struct settings *s, int argc, char *argv[])
966 {
967     struct vconn *vconn;
968     uint16_t priority;
969     uint16_t out_port;
970     struct ofpbuf *buffer;
971     struct ofp_flow_mod *ofm;
972
973     /* Parse and send. */
974     ofm = make_openflow(sizeof *ofm, OFPT_FLOW_MOD, &buffer);
975     str_to_flow(argc > 2 ? argv[2] : "", &ofm->match, NULL, NULL, 
976                 &out_port, &priority, NULL, NULL);
977     if (s->strict) {
978         ofm->command = htons(OFPFC_DELETE_STRICT);
979     } else {
980         ofm->command = htons(OFPFC_DELETE);
981     }
982     ofm->idle_timeout = htons(0);
983     ofm->hard_timeout = htons(0);
984     ofm->buffer_id = htonl(UINT32_MAX);
985     ofm->out_port = htons(out_port);
986     ofm->priority = htons(priority);
987     ofm->reserved = htonl(0);
988
989     open_vconn(argv[1], &vconn);
990     send_openflow_buffer(vconn, buffer);
991     vconn_close(vconn);
992 }
993
994 static void
995 do_monitor(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
996 {
997     struct vconn *vconn;
998
999     open_vconn(argv[1], &vconn);
1000     if (argc > 2) {
1001         int miss_send_len = atoi(argv[2]);
1002         struct ofp_switch_config *osc;
1003         struct ofpbuf *buf;
1004
1005         osc = make_openflow(sizeof *osc, OFPT_SET_CONFIG, &buf);
1006         osc->miss_send_len = htons(miss_send_len);
1007         send_openflow_buffer(vconn, buf);
1008     }
1009     for (;;) {
1010         struct ofpbuf *b;
1011         run(vconn_recv_block(vconn, &b), "vconn_recv");
1012         ofp_print(stderr, b->data, b->size, 2);
1013         ofpbuf_delete(b);
1014     }
1015 }
1016
1017 static void
1018 do_dump_ports(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
1019 {
1020     dump_trivial_stats_transaction(argv[1], OFPST_PORT);
1021 }
1022
1023 static void
1024 do_probe(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
1025 {
1026     struct ofpbuf *request;
1027     struct vconn *vconn;
1028     struct ofpbuf *reply;
1029
1030     make_openflow(sizeof(struct ofp_header), OFPT_ECHO_REQUEST, &request);
1031     open_vconn(argv[1], &vconn);
1032     run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1033     if (reply->size != sizeof(struct ofp_header)) {
1034         ovs_fatal(0, "reply does not match request");
1035     }
1036     ofpbuf_delete(reply);
1037     vconn_close(vconn);
1038 }
1039
1040 static void
1041 do_mod_port(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
1042 {
1043     struct ofpbuf *request, *reply;
1044     struct ofp_switch_features *osf;
1045     struct ofp_port_mod *opm;
1046     struct vconn *vconn;
1047     char *endptr;
1048     int n_ports;
1049     int port_idx;
1050     int port_no;
1051     
1052
1053     /* Check if the argument is a port index.  Otherwise, treat it as
1054      * the port name. */
1055     port_no = strtol(argv[2], &endptr, 10);
1056     if (port_no == 0 && endptr == argv[2]) {
1057         port_no = -1;
1058     }
1059
1060     /* Send a "Features Request" to get the information we need in order 
1061      * to modify the port. */
1062     make_openflow(sizeof(struct ofp_header), OFPT_FEATURES_REQUEST, &request);
1063     open_vconn(argv[1], &vconn);
1064     run(vconn_transact(vconn, request, &reply), "talking to %s", argv[1]);
1065
1066     osf = reply->data;
1067     n_ports = (reply->size - sizeof *osf) / sizeof *osf->ports;
1068
1069     for (port_idx = 0; port_idx < n_ports; port_idx++) {
1070         if (port_no != -1) {
1071             /* Check argument as a port index */
1072             if (osf->ports[port_idx].port_no == htons(port_no)) {
1073                 break;
1074             }
1075         } else {
1076             /* Check argument as an interface name */
1077             if (!strncmp((char *)osf->ports[port_idx].name, argv[2], 
1078                         sizeof osf->ports[0].name)) {
1079                 break;
1080             }
1081
1082         }
1083     }
1084     if (port_idx == n_ports) {
1085         ovs_fatal(0, "couldn't find monitored port: %s", argv[2]);
1086     }
1087
1088     opm = make_openflow(sizeof(struct ofp_port_mod), OFPT_PORT_MOD, &request);
1089     opm->port_no = osf->ports[port_idx].port_no;
1090     memcpy(opm->hw_addr, osf->ports[port_idx].hw_addr, sizeof opm->hw_addr);
1091     opm->config = htonl(0);
1092     opm->mask = htonl(0);
1093     opm->advertise = htonl(0);
1094
1095     printf("modifying port: %s\n", osf->ports[port_idx].name);
1096
1097     if (!strncasecmp(argv[3], MOD_PORT_CMD_UP, sizeof MOD_PORT_CMD_UP)) {
1098         opm->mask |= htonl(OFPPC_PORT_DOWN);
1099     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_DOWN, 
1100                 sizeof MOD_PORT_CMD_DOWN)) {
1101         opm->mask |= htonl(OFPPC_PORT_DOWN);
1102         opm->config |= htonl(OFPPC_PORT_DOWN);
1103     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_FLOOD, 
1104                 sizeof MOD_PORT_CMD_FLOOD)) {
1105         opm->mask |= htonl(OFPPC_NO_FLOOD);
1106     } else if (!strncasecmp(argv[3], MOD_PORT_CMD_NOFLOOD, 
1107                 sizeof MOD_PORT_CMD_NOFLOOD)) {
1108         opm->mask |= htonl(OFPPC_NO_FLOOD);
1109         opm->config |= htonl(OFPPC_NO_FLOOD);
1110     } else {
1111         ovs_fatal(0, "unknown mod-port command '%s'", argv[3]);
1112     }
1113
1114     send_openflow_buffer(vconn, request);
1115
1116     ofpbuf_delete(reply);
1117     vconn_close(vconn);
1118 }
1119
1120 static void
1121 do_ping(const struct settings *s UNUSED, int argc, char *argv[])
1122 {
1123     size_t max_payload = 65535 - sizeof(struct ofp_header);
1124     unsigned int payload;
1125     struct vconn *vconn;
1126     int i;
1127
1128     payload = argc > 2 ? atoi(argv[2]) : 64;
1129     if (payload > max_payload) {
1130         ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1131     }
1132
1133     open_vconn(argv[1], &vconn);
1134     for (i = 0; i < 10; i++) {
1135         struct timeval start, end;
1136         struct ofpbuf *request, *reply;
1137         struct ofp_header *rq_hdr, *rpy_hdr;
1138
1139         rq_hdr = make_openflow(sizeof(struct ofp_header) + payload,
1140                                OFPT_ECHO_REQUEST, &request);
1141         random_bytes(rq_hdr + 1, payload);
1142
1143         gettimeofday(&start, NULL);
1144         run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
1145         gettimeofday(&end, NULL);
1146
1147         rpy_hdr = reply->data;
1148         if (reply->size != request->size
1149             || memcmp(rpy_hdr + 1, rq_hdr + 1, payload)
1150             || rpy_hdr->xid != rq_hdr->xid
1151             || rpy_hdr->type != OFPT_ECHO_REPLY) {
1152             printf("Reply does not match request.  Request:\n");
1153             ofp_print(stdout, request, request->size, 2);
1154             printf("Reply:\n");
1155             ofp_print(stdout, reply, reply->size, 2);
1156         }
1157         printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
1158                reply->size - sizeof *rpy_hdr, argv[1], rpy_hdr->xid,
1159                    (1000*(double)(end.tv_sec - start.tv_sec))
1160                    + (.001*(end.tv_usec - start.tv_usec)));
1161         ofpbuf_delete(request);
1162         ofpbuf_delete(reply);
1163     }
1164     vconn_close(vconn);
1165 }
1166
1167 static void
1168 do_benchmark(const struct settings *s UNUSED, int argc UNUSED, char *argv[])
1169 {
1170     size_t max_payload = 65535 - sizeof(struct ofp_header);
1171     struct timeval start, end;
1172     unsigned int payload_size, message_size;
1173     struct vconn *vconn;
1174     double duration;
1175     int count;
1176     int i;
1177
1178     payload_size = atoi(argv[2]);
1179     if (payload_size > max_payload) {
1180         ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload);
1181     }
1182     message_size = sizeof(struct ofp_header) + payload_size;
1183
1184     count = atoi(argv[3]);
1185
1186     printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
1187            count, message_size, count * message_size);
1188
1189     open_vconn(argv[1], &vconn);
1190     gettimeofday(&start, NULL);
1191     for (i = 0; i < count; i++) {
1192         struct ofpbuf *request, *reply;
1193         struct ofp_header *rq_hdr;
1194
1195         rq_hdr = make_openflow(message_size, OFPT_ECHO_REQUEST, &request);
1196         memset(rq_hdr + 1, 0, payload_size);
1197         run(vconn_transact(vconn, request, &reply), "transact");
1198         ofpbuf_delete(reply);
1199     }
1200     gettimeofday(&end, NULL);
1201     vconn_close(vconn);
1202
1203     duration = ((1000*(double)(end.tv_sec - start.tv_sec))
1204                 + (.001*(end.tv_usec - start.tv_usec)));
1205     printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
1206            duration, count / (duration / 1000.0),
1207            count * message_size / (duration / 1000.0));
1208 }
1209
1210 static void
1211 do_execute(const struct settings *s UNUSED, int argc, char *argv[])
1212 {
1213     struct vconn *vconn;
1214     struct ofpbuf *request;
1215     struct nicira_header *nicira;
1216     struct nx_command_reply *ncr;
1217     uint32_t xid;
1218     int i;
1219
1220     nicira = make_openflow(sizeof *nicira, OFPT_VENDOR, &request);
1221     xid = nicira->header.xid;
1222     nicira->vendor = htonl(NX_VENDOR_ID);
1223     nicira->subtype = htonl(NXT_COMMAND_REQUEST);
1224     ofpbuf_put(request, argv[2], strlen(argv[2]));
1225     for (i = 3; i < argc; i++) {
1226         ofpbuf_put_zeros(request, 1);
1227         ofpbuf_put(request, argv[i], strlen(argv[i]));
1228     }
1229     update_openflow_length(request);
1230
1231     open_vconn(argv[1], &vconn);
1232     run(vconn_send_block(vconn, request), "send");
1233
1234     for (;;) {
1235         struct ofpbuf *reply;
1236         uint32_t status;
1237
1238         run(vconn_recv_xid(vconn, xid, &reply), "recv_xid");
1239         if (reply->size < sizeof *ncr) {
1240             ovs_fatal(0, "reply is too short (%zu bytes < %zu bytes)",
1241                       reply->size, sizeof *ncr);
1242         }
1243         ncr = reply->data;
1244         if (ncr->nxh.header.type != OFPT_VENDOR
1245             || ncr->nxh.vendor != htonl(NX_VENDOR_ID)
1246             || ncr->nxh.subtype != htonl(NXT_COMMAND_REPLY)) {
1247             ovs_fatal(0, "reply is invalid");
1248         }
1249
1250         status = ntohl(ncr->status);
1251         if (status & NXT_STATUS_STARTED) {
1252             /* Wait for a second reply. */
1253             continue;
1254         } else if (status & NXT_STATUS_EXITED) {
1255             fprintf(stderr, "process terminated normally with exit code %d",
1256                     status & NXT_STATUS_EXITSTATUS);
1257         } else if (status & NXT_STATUS_SIGNALED) {
1258             fprintf(stderr, "process terminated by signal %d",
1259                     status & NXT_STATUS_TERMSIG);
1260         } else if (status & NXT_STATUS_ERROR) {
1261             fprintf(stderr, "error executing command");
1262         } else {
1263             fprintf(stderr, "process terminated for unknown reason");
1264         }
1265         if (status & NXT_STATUS_COREDUMP) {
1266             fprintf(stderr, " (core dumped)");
1267         }
1268         putc('\n', stderr);
1269
1270         fwrite(ncr + 1, reply->size - sizeof *ncr, 1, stdout);
1271         break;
1272     }
1273 }
1274
1275 static void
1276 do_help(const struct settings *s UNUSED, int argc UNUSED, char *argv[] UNUSED)
1277 {
1278     usage();
1279 }
1280
1281 static struct command all_commands[] = {
1282     { "show", 1, 1, do_show },
1283     { "status", 1, 2, do_status },
1284     { "monitor", 1, 3, do_monitor },
1285     { "dump-desc", 1, 1, do_dump_desc },
1286     { "dump-tables", 1, 1, do_dump_tables },
1287     { "dump-flows", 1, 2, do_dump_flows },
1288     { "dump-aggregate", 1, 2, do_dump_aggregate },
1289     { "add-flow", 2, 2, do_add_flow },
1290     { "add-flows", 2, 2, do_add_flows },
1291     { "mod-flows", 2, 2, do_mod_flows },
1292     { "del-flows", 1, 2, do_del_flows },
1293     { "dump-ports", 1, 1, do_dump_ports },
1294     { "mod-port", 3, 3, do_mod_port },
1295     { "probe", 1, 1, do_probe },
1296     { "ping", 1, 2, do_ping },
1297     { "benchmark", 3, 3, do_benchmark },
1298     { "execute", 2, INT_MAX, do_execute },
1299     { "help", 0, INT_MAX, do_help },
1300     { NULL, 0, 0, NULL },
1301 };