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