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