command-line: add ovs_cmdl_context
[cascardo/ovs.git] / utilities / ovs-ofctl.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 Nicira, Inc.
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 <ctype.h>
19 #include <errno.h>
20 #include <getopt.h>
21 #include <inttypes.h>
22 #include <sys/socket.h>
23 #include <net/if.h>
24 #include <signal.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31
32 #include "byte-order.h"
33 #include "classifier.h"
34 #include "command-line.h"
35 #include "daemon.h"
36 #include "compiler.h"
37 #include "dirs.h"
38 #include "dynamic-string.h"
39 #include "fatal-signal.h"
40 #include "nx-match.h"
41 #include "odp-util.h"
42 #include "ofp-actions.h"
43 #include "ofp-errors.h"
44 #include "ofp-msgs.h"
45 #include "ofp-parse.h"
46 #include "ofp-print.h"
47 #include "ofp-util.h"
48 #include "ofp-version-opt.h"
49 #include "ofpbuf.h"
50 #include "ofproto/ofproto.h"
51 #include "openflow/nicira-ext.h"
52 #include "openflow/openflow.h"
53 #include "dp-packet.h"
54 #include "packets.h"
55 #include "pcap-file.h"
56 #include "poll-loop.h"
57 #include "random.h"
58 #include "stream-ssl.h"
59 #include "socket-util.h"
60 #include "timeval.h"
61 #include "unixctl.h"
62 #include "util.h"
63 #include "openvswitch/vconn.h"
64 #include "openvswitch/vlog.h"
65 #include "meta-flow.h"
66 #include "sort.h"
67
68 VLOG_DEFINE_THIS_MODULE(ofctl);
69
70 /* --strict: Use strict matching for flow mod commands?  Additionally governs
71  * use of nx_pull_match() instead of nx_pull_match_loose() in parse-nx-match.
72  */
73 static bool strict;
74
75 /* --readd: If true, on replace-flows, re-add even flows that have not changed
76  * (to reset flow counters). */
77 static bool readd;
78
79 /* -F, --flow-format: Allowed protocols.  By default, any protocol is
80  * allowed. */
81 static enum ofputil_protocol allowed_protocols = OFPUTIL_P_ANY;
82
83 /* -P, --packet-in-format: Packet IN format to use in monitor and snoop
84  * commands.  Either one of NXPIF_* to force a particular packet_in format, or
85  * -1 to let ovs-ofctl choose the default. */
86 static int preferred_packet_in_format = -1;
87
88 /* -m, --more: Additional verbosity for ofp-print functions. */
89 static int verbosity;
90
91 /* --timestamp: Print a timestamp before each received packet on "monitor" and
92  * "snoop" command? */
93 static bool timestamp;
94
95 /* --unixctl-path: Path to use for unixctl server, for "monitor" and "snoop"
96      commands. */
97 static char *unixctl_path;
98
99 /* --sort, --rsort: Sort order. */
100 enum sort_order { SORT_ASC, SORT_DESC };
101 struct sort_criterion {
102     const struct mf_field *field; /* NULL means to sort by priority. */
103     enum sort_order order;
104 };
105 static struct sort_criterion *criteria;
106 static size_t n_criteria, allocated_criteria;
107
108 static const struct ovs_cmdl_command *get_all_commands(void);
109
110 OVS_NO_RETURN static void usage(void);
111 static void parse_options(int argc, char *argv[]);
112
113 static bool recv_flow_stats_reply(struct vconn *, ovs_be32 send_xid,
114                                   struct ofpbuf **replyp,
115                                   struct ofputil_flow_stats *,
116                                   struct ofpbuf *ofpacts);
117 int
118 main(int argc, char *argv[])
119 {
120     struct ovs_cmdl_context ctx = { .argc = 0, };
121     set_program_name(argv[0]);
122     service_start(&argc, &argv);
123     parse_options(argc, argv);
124     fatal_ignore_sigpipe();
125     ctx.argc = argc - optind;
126     ctx.argv = argv + optind;
127     ovs_cmdl_run_command(&ctx, get_all_commands());
128     return 0;
129 }
130
131 static void
132 add_sort_criterion(enum sort_order order, const char *field)
133 {
134     struct sort_criterion *sc;
135
136     if (n_criteria >= allocated_criteria) {
137         criteria = x2nrealloc(criteria, &allocated_criteria, sizeof *criteria);
138     }
139
140     sc = &criteria[n_criteria++];
141     if (!field || !strcasecmp(field, "priority")) {
142         sc->field = NULL;
143     } else {
144         sc->field = mf_from_name(field);
145         if (!sc->field) {
146             ovs_fatal(0, "%s: unknown field name", field);
147         }
148     }
149     sc->order = order;
150 }
151
152 static void
153 parse_options(int argc, char *argv[])
154 {
155     enum {
156         OPT_STRICT = UCHAR_MAX + 1,
157         OPT_READD,
158         OPT_TIMESTAMP,
159         OPT_SORT,
160         OPT_RSORT,
161         OPT_UNIXCTL,
162         DAEMON_OPTION_ENUMS,
163         OFP_VERSION_OPTION_ENUMS,
164         VLOG_OPTION_ENUMS
165     };
166     static const struct option long_options[] = {
167         {"timeout", required_argument, NULL, 't'},
168         {"strict", no_argument, NULL, OPT_STRICT},
169         {"readd", no_argument, NULL, OPT_READD},
170         {"flow-format", required_argument, NULL, 'F'},
171         {"packet-in-format", required_argument, NULL, 'P'},
172         {"more", no_argument, NULL, 'm'},
173         {"timestamp", no_argument, NULL, OPT_TIMESTAMP},
174         {"sort", optional_argument, NULL, OPT_SORT},
175         {"rsort", optional_argument, NULL, OPT_RSORT},
176         {"unixctl",     required_argument, NULL, OPT_UNIXCTL},
177         {"help", no_argument, NULL, 'h'},
178         {"option", no_argument, NULL, 'o'},
179         DAEMON_LONG_OPTIONS,
180         OFP_VERSION_LONG_OPTIONS,
181         VLOG_LONG_OPTIONS,
182         STREAM_SSL_LONG_OPTIONS,
183         {NULL, 0, NULL, 0},
184     };
185     char *short_options = ovs_cmdl_long_options_to_short_options(long_options);
186     uint32_t versions;
187     enum ofputil_protocol version_protocols;
188
189     /* For now, ovs-ofctl only enables OpenFlow 1.0 by default.  This is
190      * because ovs-ofctl implements command such as "add-flow" as raw OpenFlow
191      * requests, but those requests have subtly different semantics in
192      * different OpenFlow versions.  For example:
193      *
194      *     - In OpenFlow 1.0, a "mod-flow" operation that does not find any
195      *       existing flow to modify adds a new flow.
196      *
197      *     - In OpenFlow 1.1, a "mod-flow" operation that does not find any
198      *       existing flow to modify adds a new flow, but only if the mod-flow
199      *       did not match on the flow cookie.
200      *
201      *     - In OpenFlow 1.2 and a later, a "mod-flow" operation never adds a
202      *       new flow.
203      */
204     set_allowed_ofp_versions("OpenFlow10");
205
206     for (;;) {
207         unsigned long int timeout;
208         int c;
209
210         c = getopt_long(argc, argv, short_options, long_options, NULL);
211         if (c == -1) {
212             break;
213         }
214
215         switch (c) {
216         case 't':
217             timeout = strtoul(optarg, NULL, 10);
218             if (timeout <= 0) {
219                 ovs_fatal(0, "value %s on -t or --timeout is not at least 1",
220                           optarg);
221             } else {
222                 time_alarm(timeout);
223             }
224             break;
225
226         case 'F':
227             allowed_protocols = ofputil_protocols_from_string(optarg);
228             if (!allowed_protocols) {
229                 ovs_fatal(0, "%s: invalid flow format(s)", optarg);
230             }
231             break;
232
233         case 'P':
234             preferred_packet_in_format =
235                 ofputil_packet_in_format_from_string(optarg);
236             if (preferred_packet_in_format < 0) {
237                 ovs_fatal(0, "unknown packet-in format `%s'", optarg);
238             }
239             break;
240
241         case 'm':
242             verbosity++;
243             break;
244
245         case 'h':
246             usage();
247
248         case 'o':
249             ovs_cmdl_print_options(long_options);
250             exit(EXIT_SUCCESS);
251
252         case OPT_STRICT:
253             strict = true;
254             break;
255
256         case OPT_READD:
257             readd = true;
258             break;
259
260         case OPT_TIMESTAMP:
261             timestamp = true;
262             break;
263
264         case OPT_SORT:
265             add_sort_criterion(SORT_ASC, optarg);
266             break;
267
268         case OPT_RSORT:
269             add_sort_criterion(SORT_DESC, optarg);
270             break;
271
272         case OPT_UNIXCTL:
273             unixctl_path = optarg;
274             break;
275
276         DAEMON_OPTION_HANDLERS
277         OFP_VERSION_OPTION_HANDLERS
278         VLOG_OPTION_HANDLERS
279         STREAM_SSL_OPTION_HANDLERS
280
281         case '?':
282             exit(EXIT_FAILURE);
283
284         default:
285             abort();
286         }
287     }
288
289     if (n_criteria) {
290         /* Always do a final sort pass based on priority. */
291         add_sort_criterion(SORT_DESC, "priority");
292     }
293
294     free(short_options);
295
296     versions = get_allowed_ofp_versions();
297     version_protocols = ofputil_protocols_from_version_bitmap(versions);
298     if (!(allowed_protocols & version_protocols)) {
299         char *protocols = ofputil_protocols_to_string(allowed_protocols);
300         struct ds version_s = DS_EMPTY_INITIALIZER;
301
302         ofputil_format_version_bitmap_names(&version_s, versions);
303         ovs_fatal(0, "None of the enabled OpenFlow versions (%s) supports "
304                   "any of the enabled flow formats (%s).  (Use -O to enable "
305                   "additional OpenFlow versions or -F to enable additional "
306                   "flow formats.)", ds_cstr(&version_s), protocols);
307     }
308     allowed_protocols &= version_protocols;
309     mask_allowed_ofp_versions(ofputil_protocols_to_version_bitmap(
310                                   allowed_protocols));
311 }
312
313 static void
314 usage(void)
315 {
316     printf("%s: OpenFlow switch management utility\n"
317            "usage: %s [OPTIONS] COMMAND [ARG...]\n"
318            "\nFor OpenFlow switches:\n"
319            "  show SWITCH                 show OpenFlow information\n"
320            "  dump-desc SWITCH            print switch description\n"
321            "  dump-tables SWITCH          print table stats\n"
322            "  dump-table-features SWITCH  print table features\n"
323            "  mod-port SWITCH IFACE ACT   modify port behavior\n"
324            "  mod-table SWITCH MOD        modify flow table behavior\n"
325            "  get-frags SWITCH            print fragment handling behavior\n"
326            "  set-frags SWITCH FRAG_MODE  set fragment handling behavior\n"
327            "  dump-ports SWITCH [PORT]    print port statistics\n"
328            "  dump-ports-desc SWITCH [PORT]  print port descriptions\n"
329            "  dump-flows SWITCH           print all flow entries\n"
330            "  dump-flows SWITCH FLOW      print matching FLOWs\n"
331            "  dump-aggregate SWITCH       print aggregate flow statistics\n"
332            "  dump-aggregate SWITCH FLOW  print aggregate stats for FLOWs\n"
333            "  queue-stats SWITCH [PORT [QUEUE]]  dump queue stats\n"
334            "  add-flow SWITCH FLOW        add flow described by FLOW\n"
335            "  add-flows SWITCH FILE       add flows from FILE\n"
336            "  mod-flows SWITCH FLOW       modify actions of matching FLOWs\n"
337            "  del-flows SWITCH [FLOW]     delete matching FLOWs\n"
338            "  replace-flows SWITCH FILE   replace flows with those in FILE\n"
339            "  diff-flows SOURCE1 SOURCE2  compare flows from two sources\n"
340            "  packet-out SWITCH IN_PORT ACTIONS PACKET...\n"
341            "                              execute ACTIONS on PACKET\n"
342            "  monitor SWITCH [MISSLEN] [invalid_ttl] [watch:[...]]\n"
343            "                              print packets received from SWITCH\n"
344            "  snoop SWITCH                snoop on SWITCH and its controller\n"
345            "  add-group SWITCH GROUP      add group described by GROUP\n"
346            "  add-groups SWITCH FILE      add group from FILE\n"
347            "  mod-group SWITCH GROUP      modify specific group\n"
348            "  del-groups SWITCH [GROUP]   delete matching GROUPs\n"
349            "  insert-buckets SWITCH [GROUP] add buckets to GROUP\n"
350            "  remove-buckets SWITCH [GROUP] remove buckets from GROUP\n"
351            "  dump-group-features SWITCH  print group features\n"
352            "  dump-groups SWITCH [GROUP]  print group description\n"
353            "  dump-group-stats SWITCH [GROUP]  print group statistics\n"
354            "  queue-get-config SWITCH PORT  print queue information for port\n"
355            "  add-meter SWITCH METER      add meter described by METER\n"
356            "  mod-meter SWITCH METER      modify specific METER\n"
357            "  del-meter SWITCH METER      delete METER\n"
358            "  del-meters SWITCH           delete all meters\n"
359            "  dump-meter SWITCH METER     print METER configuration\n"
360            "  dump-meters SWITCH          print all meter configuration\n"
361            "  meter-stats SWITCH [METER]  print meter statistics\n"
362            "  meter-features SWITCH       print meter features\n"
363            "\nFor OpenFlow switches and controllers:\n"
364            "  probe TARGET                probe whether TARGET is up\n"
365            "  ping TARGET [N]             latency of N-byte echos\n"
366            "  benchmark TARGET N COUNT    bandwidth of COUNT N-byte echos\n"
367            "SWITCH or TARGET is an active OpenFlow connection method.\n"
368            "\nOther commands:\n"
369            "  ofp-parse FILE              print messages read from FILE\n"
370            "  ofp-parse-pcap PCAP         print OpenFlow read from PCAP\n",
371            program_name, program_name);
372     vconn_usage(true, false, false);
373     daemon_usage();
374     ofp_version_usage();
375     vlog_usage();
376     printf("\nOther options:\n"
377            "  --strict                    use strict match for flow commands\n"
378            "  --readd                     replace flows that haven't changed\n"
379            "  -F, --flow-format=FORMAT    force particular flow format\n"
380            "  -P, --packet-in-format=FRMT force particular packet in format\n"
381            "  -m, --more                  be more verbose printing OpenFlow\n"
382            "  --timestamp                 (monitor, snoop) print timestamps\n"
383            "  -t, --timeout=SECS          give up after SECS seconds\n"
384            "  --sort[=field]              sort in ascending order\n"
385            "  --rsort[=field]             sort in descending order\n"
386            "  --unixctl=SOCKET            set control socket name\n"
387            "  -h, --help                  display this help message\n"
388            "  -V, --version               display version information\n");
389     exit(EXIT_SUCCESS);
390 }
391
392 static void
393 ofctl_exit(struct unixctl_conn *conn, int argc OVS_UNUSED,
394            const char *argv[] OVS_UNUSED, void *exiting_)
395 {
396     bool *exiting = exiting_;
397     *exiting = true;
398     unixctl_command_reply(conn, NULL);
399 }
400
401 static void run(int retval, const char *message, ...)
402     OVS_PRINTF_FORMAT(2, 3);
403
404 static void
405 run(int retval, const char *message, ...)
406 {
407     if (retval) {
408         va_list args;
409
410         va_start(args, message);
411         ovs_fatal_valist(retval, message, args);
412     }
413 }
414 \f
415 /* Generic commands. */
416
417 static int
418 open_vconn_socket(const char *name, struct vconn **vconnp)
419 {
420     char *vconn_name = xasprintf("unix:%s", name);
421     int error;
422
423     error = vconn_open(vconn_name, get_allowed_ofp_versions(), DSCP_DEFAULT,
424                        vconnp);
425     if (error && error != ENOENT) {
426         ovs_fatal(0, "%s: failed to open socket (%s)", name,
427                   ovs_strerror(error));
428     }
429     free(vconn_name);
430
431     return error;
432 }
433
434 enum open_target { MGMT, SNOOP };
435
436 static enum ofputil_protocol
437 open_vconn__(const char *name, enum open_target target,
438              struct vconn **vconnp)
439 {
440     const char *suffix = target == MGMT ? "mgmt" : "snoop";
441     char *datapath_name, *datapath_type, *socket_name;
442     enum ofputil_protocol protocol;
443     char *bridge_path;
444     int ofp_version;
445     int error;
446
447     bridge_path = xasprintf("%s/%s.%s", ovs_rundir(), name, suffix);
448
449     ofproto_parse_name(name, &datapath_name, &datapath_type);
450     socket_name = xasprintf("%s/%s.%s", ovs_rundir(), datapath_name, suffix);
451     free(datapath_name);
452     free(datapath_type);
453
454     if (strchr(name, ':')) {
455         run(vconn_open(name, get_allowed_ofp_versions(), DSCP_DEFAULT, vconnp),
456             "connecting to %s", name);
457     } else if (!open_vconn_socket(name, vconnp)) {
458         /* Fall Through. */
459     } else if (!open_vconn_socket(bridge_path, vconnp)) {
460         /* Fall Through. */
461     } else if (!open_vconn_socket(socket_name, vconnp)) {
462         /* Fall Through. */
463     } else {
464         ovs_fatal(0, "%s is not a bridge or a socket", name);
465     }
466
467     if (target == SNOOP) {
468         vconn_set_recv_any_version(*vconnp);
469     }
470
471     free(bridge_path);
472     free(socket_name);
473
474     VLOG_DBG("connecting to %s", vconn_get_name(*vconnp));
475     error = vconn_connect_block(*vconnp);
476     if (error) {
477         ovs_fatal(0, "%s: failed to connect to socket (%s)", name,
478                   ovs_strerror(error));
479     }
480
481     ofp_version = vconn_get_version(*vconnp);
482     protocol = ofputil_protocol_from_ofp_version(ofp_version);
483     if (!protocol) {
484         ovs_fatal(0, "%s: unsupported OpenFlow version 0x%02x",
485                   name, ofp_version);
486     }
487     return protocol;
488 }
489
490 static enum ofputil_protocol
491 open_vconn(const char *name, struct vconn **vconnp)
492 {
493     return open_vconn__(name, MGMT, vconnp);
494 }
495
496 static void
497 send_openflow_buffer(struct vconn *vconn, struct ofpbuf *buffer)
498 {
499     ofpmsg_update_length(buffer);
500     run(vconn_send_block(vconn, buffer), "failed to send packet to switch");
501 }
502
503 static void
504 dump_transaction(struct vconn *vconn, struct ofpbuf *request)
505 {
506     struct ofpbuf *reply;
507
508     ofpmsg_update_length(request);
509     run(vconn_transact(vconn, request, &reply), "talking to %s",
510         vconn_get_name(vconn));
511     ofp_print(stdout, reply->data, reply->size, verbosity + 1);
512     ofpbuf_delete(reply);
513 }
514
515 static void
516 dump_trivial_transaction(const char *vconn_name, enum ofpraw raw)
517 {
518     struct ofpbuf *request;
519     struct vconn *vconn;
520
521     open_vconn(vconn_name, &vconn);
522     request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
523     dump_transaction(vconn, request);
524     vconn_close(vconn);
525 }
526
527 static void
528 dump_stats_transaction(struct vconn *vconn, struct ofpbuf *request)
529 {
530     const struct ofp_header *request_oh = request->data;
531     ovs_be32 send_xid = request_oh->xid;
532     enum ofpraw request_raw;
533     enum ofpraw reply_raw;
534     bool done = false;
535
536     ofpraw_decode_partial(&request_raw, request->data, request->size);
537     reply_raw = ofpraw_stats_request_to_reply(request_raw,
538                                               request_oh->version);
539
540     send_openflow_buffer(vconn, request);
541     while (!done) {
542         ovs_be32 recv_xid;
543         struct ofpbuf *reply;
544
545         run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
546         recv_xid = ((struct ofp_header *) reply->data)->xid;
547         if (send_xid == recv_xid) {
548             enum ofpraw raw;
549
550             ofp_print(stdout, reply->data, reply->size, verbosity + 1);
551
552             ofpraw_decode(&raw, reply->data);
553             if (ofptype_from_ofpraw(raw) == OFPTYPE_ERROR) {
554                 done = true;
555             } else if (raw == reply_raw) {
556                 done = !ofpmp_more(reply->data);
557             } else {
558                 ovs_fatal(0, "received bad reply: %s",
559                           ofp_to_string(reply->data, reply->size,
560                                         verbosity + 1));
561             }
562         } else {
563             VLOG_DBG("received reply with xid %08"PRIx32" "
564                      "!= expected %08"PRIx32, recv_xid, send_xid);
565         }
566         ofpbuf_delete(reply);
567     }
568 }
569
570 static void
571 dump_trivial_stats_transaction(const char *vconn_name, enum ofpraw raw)
572 {
573     struct ofpbuf *request;
574     struct vconn *vconn;
575
576     open_vconn(vconn_name, &vconn);
577     request = ofpraw_alloc(raw, vconn_get_version(vconn), 0);
578     dump_stats_transaction(vconn, request);
579     vconn_close(vconn);
580 }
581
582 /* Sends all of the 'requests', which should be requests that only have replies
583  * if an error occurs, and waits for them to succeed or fail.  If an error does
584  * occur, prints it and exits with an error.
585  *
586  * Destroys all of the 'requests'. */
587 static void
588 transact_multiple_noreply(struct vconn *vconn, struct ovs_list *requests)
589 {
590     struct ofpbuf *request, *reply;
591
592     LIST_FOR_EACH (request, list_node, requests) {
593         ofpmsg_update_length(request);
594     }
595
596     run(vconn_transact_multiple_noreply(vconn, requests, &reply),
597         "talking to %s", vconn_get_name(vconn));
598     if (reply) {
599         ofp_print(stderr, reply->data, reply->size, verbosity + 2);
600         exit(1);
601     }
602     ofpbuf_delete(reply);
603 }
604
605 /* Sends 'request', which should be a request that only has a reply if an error
606  * occurs, and waits for it to succeed or fail.  If an error does occur, prints
607  * it and exits with an error.
608  *
609  * Destroys 'request'. */
610 static void
611 transact_noreply(struct vconn *vconn, struct ofpbuf *request)
612 {
613     struct ovs_list requests;
614
615     list_init(&requests);
616     list_push_back(&requests, &request->list_node);
617     transact_multiple_noreply(vconn, &requests);
618 }
619
620 static void
621 fetch_switch_config(struct vconn *vconn, struct ofp_switch_config *config_)
622 {
623     struct ofp_switch_config *config;
624     struct ofpbuf *request;
625     struct ofpbuf *reply;
626     enum ofptype type;
627
628     request = ofpraw_alloc(OFPRAW_OFPT_GET_CONFIG_REQUEST,
629                            vconn_get_version(vconn), 0);
630     run(vconn_transact(vconn, request, &reply),
631         "talking to %s", vconn_get_name(vconn));
632
633     if (ofptype_pull(&type, reply) || type != OFPTYPE_GET_CONFIG_REPLY) {
634         ovs_fatal(0, "%s: bad reply to config request", vconn_get_name(vconn));
635     }
636
637     config = ofpbuf_pull(reply, sizeof *config);
638     *config_ = *config;
639
640     ofpbuf_delete(reply);
641 }
642
643 static void
644 set_switch_config(struct vconn *vconn, const struct ofp_switch_config *config)
645 {
646     struct ofpbuf *request;
647
648     request = ofpraw_alloc(OFPRAW_OFPT_SET_CONFIG, vconn_get_version(vconn), 0);
649     ofpbuf_put(request, config, sizeof *config);
650
651     transact_noreply(vconn, request);
652 }
653
654 static void
655 ofctl_show(struct ovs_cmdl_context *ctx)
656 {
657     const char *vconn_name = ctx->argv[1];
658     enum ofp_version version;
659     struct vconn *vconn;
660     struct ofpbuf *request;
661     struct ofpbuf *reply;
662     bool has_ports;
663
664     open_vconn(vconn_name, &vconn);
665     version = vconn_get_version(vconn);
666     request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST, version, 0);
667     run(vconn_transact(vconn, request, &reply), "talking to %s", vconn_name);
668
669     has_ports = ofputil_switch_features_has_ports(reply);
670     ofp_print(stdout, reply->data, reply->size, verbosity + 1);
671     ofpbuf_delete(reply);
672
673     if (!has_ports) {
674         request = ofputil_encode_port_desc_stats_request(version, OFPP_ANY);
675         dump_stats_transaction(vconn, request);
676     }
677     dump_trivial_transaction(vconn_name, OFPRAW_OFPT_GET_CONFIG_REQUEST);
678     vconn_close(vconn);
679 }
680
681 static void
682 ofctl_dump_desc(struct ovs_cmdl_context *ctx)
683 {
684     dump_trivial_stats_transaction(ctx->argv[1], OFPRAW_OFPST_DESC_REQUEST);
685 }
686
687 static void
688 ofctl_dump_tables(struct ovs_cmdl_context *ctx)
689 {
690     dump_trivial_stats_transaction(ctx->argv[1], OFPRAW_OFPST_TABLE_REQUEST);
691 }
692
693 static void
694 ofctl_dump_table_features(struct ovs_cmdl_context *ctx)
695 {
696     struct ofpbuf *request;
697     struct vconn *vconn;
698
699     open_vconn(ctx->argv[1], &vconn);
700     request = ofputil_encode_table_features_request(vconn_get_version(vconn));
701     if (request) {
702         dump_stats_transaction(vconn, request);
703     }
704
705     vconn_close(vconn);
706 }
707
708 static bool fetch_port_by_stats(struct vconn *,
709                                 const char *port_name, ofp_port_t port_no,
710                                 struct ofputil_phy_port *);
711
712 /* Uses OFPT_FEATURES_REQUEST to attempt to fetch information about the port
713  * named 'port_name' or numbered 'port_no' into '*pp'.  Returns true if
714  * successful, false on failure.
715  *
716  * This is only appropriate for OpenFlow 1.0, 1.1, and 1.2, which include a
717  * list of ports in OFPT_FEATURES_REPLY. */
718 static bool
719 fetch_port_by_features(struct vconn *vconn,
720                        const char *port_name, ofp_port_t port_no,
721                        struct ofputil_phy_port *pp)
722 {
723     struct ofputil_switch_features features;
724     const struct ofp_header *oh;
725     struct ofpbuf *request, *reply;
726     enum ofperr error;
727     enum ofptype type;
728     struct ofpbuf b;
729     bool found = false;
730
731     /* Fetch the switch's ofp_switch_features. */
732     request = ofpraw_alloc(OFPRAW_OFPT_FEATURES_REQUEST,
733                            vconn_get_version(vconn), 0);
734     run(vconn_transact(vconn, request, &reply),
735         "talking to %s", vconn_get_name(vconn));
736
737     oh = reply->data;
738     if (ofptype_decode(&type, reply->data)
739         || type != OFPTYPE_FEATURES_REPLY) {
740         ovs_fatal(0, "%s: received bad features reply", vconn_get_name(vconn));
741     }
742     if (!ofputil_switch_features_has_ports(reply)) {
743         /* The switch features reply does not contain a complete list of ports.
744          * Probably, there are more ports than will fit into a single 64 kB
745          * OpenFlow message.  Use OFPST_PORT_DESC to get a complete list of
746          * ports. */
747         ofpbuf_delete(reply);
748         return fetch_port_by_stats(vconn, port_name, port_no, pp);
749     }
750
751     error = ofputil_decode_switch_features(oh, &features, &b);
752     if (error) {
753         ovs_fatal(0, "%s: failed to decode features reply (%s)",
754                   vconn_get_name(vconn), ofperr_to_string(error));
755     }
756
757     while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
758         if (port_no != OFPP_NONE
759             ? port_no == pp->port_no
760             : !strcmp(pp->name, port_name)) {
761             found = true;
762             break;
763         }
764     }
765     ofpbuf_delete(reply);
766     return found;
767 }
768
769 /* Uses a OFPST_PORT_DESC request to attempt to fetch information about the
770  * port named 'port_name' or numbered 'port_no' into '*pp'.  Returns true if
771  * successful, false on failure.
772  *
773  * This is most appropriate for OpenFlow 1.3 and later.  Open vSwitch 1.7 and
774  * later also implements OFPST_PORT_DESC, as an extension, for OpenFlow 1.0,
775  * 1.1, and 1.2, so this can be used as a fallback in those versions when there
776  * are too many ports than fit in an OFPT_FEATURES_REPLY. */
777 static bool
778 fetch_port_by_stats(struct vconn *vconn,
779                     const char *port_name, ofp_port_t port_no,
780                     struct ofputil_phy_port *pp)
781 {
782     struct ofpbuf *request;
783     ovs_be32 send_xid;
784     bool done = false;
785     bool found = false;
786
787     request = ofputil_encode_port_desc_stats_request(vconn_get_version(vconn),
788                                                      port_no);
789     send_xid = ((struct ofp_header *) request->data)->xid;
790
791     send_openflow_buffer(vconn, request);
792     while (!done) {
793         ovs_be32 recv_xid;
794         struct ofpbuf *reply;
795
796         run(vconn_recv_block(vconn, &reply), "OpenFlow packet receive failed");
797         recv_xid = ((struct ofp_header *) reply->data)->xid;
798         if (send_xid == recv_xid) {
799             struct ofp_header *oh = reply->data;
800             enum ofptype type;
801             struct ofpbuf b;
802             uint16_t flags;
803
804             ofpbuf_use_const(&b, oh, ntohs(oh->length));
805             if (ofptype_pull(&type, &b)
806                 || type != OFPTYPE_PORT_DESC_STATS_REPLY) {
807                 ovs_fatal(0, "received bad reply: %s",
808                           ofp_to_string(reply->data, reply->size,
809                                         verbosity + 1));
810             }
811
812             flags = ofpmp_flags(oh);
813             done = !(flags & OFPSF_REPLY_MORE);
814
815             if (found) {
816                 /* We've already found the port, but we need to drain
817                  * the queue of any other replies for this request. */
818                 continue;
819             }
820
821             while (!ofputil_pull_phy_port(oh->version, &b, pp)) {
822                 if (port_no != OFPP_NONE ? port_no == pp->port_no
823                                          : !strcmp(pp->name, port_name)) {
824                     found = true;
825                     break;
826                 }
827             }
828         } else {
829             VLOG_DBG("received reply with xid %08"PRIx32" "
830                      "!= expected %08"PRIx32, recv_xid, send_xid);
831         }
832         ofpbuf_delete(reply);
833     }
834
835     return found;
836 }
837
838 static bool
839 str_to_ofp(const char *s, ofp_port_t *ofp_port)
840 {
841     bool ret;
842     uint32_t port_;
843
844     ret = str_to_uint(s, 10, &port_);
845     *ofp_port = u16_to_ofp(port_);
846     return ret;
847 }
848
849 /* Opens a connection to 'vconn_name', fetches the port structure for
850  * 'port_name' (which may be a port name or number), and copies it into
851  * '*pp'. */
852 static void
853 fetch_ofputil_phy_port(const char *vconn_name, const char *port_name,
854                        struct ofputil_phy_port *pp)
855 {
856     struct vconn *vconn;
857     ofp_port_t port_no;
858     bool found;
859
860     /* Try to interpret the argument as a port number. */
861     if (!str_to_ofp(port_name, &port_no)) {
862         port_no = OFPP_NONE;
863     }
864
865     /* OpenFlow 1.0, 1.1, and 1.2 put the list of ports in the
866      * OFPT_FEATURES_REPLY message.  OpenFlow 1.3 and later versions put it
867      * into the OFPST_PORT_DESC reply.  Try it the correct way. */
868     open_vconn(vconn_name, &vconn);
869     found = (vconn_get_version(vconn) < OFP13_VERSION
870              ? fetch_port_by_features(vconn, port_name, port_no, pp)
871              : fetch_port_by_stats(vconn, port_name, port_no, pp));
872     vconn_close(vconn);
873
874     if (!found) {
875         ovs_fatal(0, "%s: couldn't find port `%s'", vconn_name, port_name);
876     }
877 }
878
879 /* Returns the port number corresponding to 'port_name' (which may be a port
880  * name or number) within the switch 'vconn_name'. */
881 static ofp_port_t
882 str_to_port_no(const char *vconn_name, const char *port_name)
883 {
884     ofp_port_t port_no;
885
886     if (ofputil_port_from_string(port_name, &port_no)) {
887         return port_no;
888     } else {
889         struct ofputil_phy_port pp;
890
891         fetch_ofputil_phy_port(vconn_name, port_name, &pp);
892         return pp.port_no;
893     }
894 }
895
896 static bool
897 try_set_protocol(struct vconn *vconn, enum ofputil_protocol want,
898                  enum ofputil_protocol *cur)
899 {
900     for (;;) {
901         struct ofpbuf *request, *reply;
902         enum ofputil_protocol next;
903
904         request = ofputil_encode_set_protocol(*cur, want, &next);
905         if (!request) {
906             return *cur == want;
907         }
908
909         run(vconn_transact_noreply(vconn, request, &reply),
910             "talking to %s", vconn_get_name(vconn));
911         if (reply) {
912             char *s = ofp_to_string(reply->data, reply->size, 2);
913             VLOG_DBG("%s: failed to set protocol, switch replied: %s",
914                      vconn_get_name(vconn), s);
915             free(s);
916             ofpbuf_delete(reply);
917             return false;
918         }
919
920         *cur = next;
921     }
922 }
923
924 static enum ofputil_protocol
925 set_protocol_for_flow_dump(struct vconn *vconn,
926                            enum ofputil_protocol cur_protocol,
927                            enum ofputil_protocol usable_protocols)
928 {
929     char *usable_s;
930     int i;
931
932     for (i = 0; i < ofputil_n_flow_dump_protocols; i++) {
933         enum ofputil_protocol f = ofputil_flow_dump_protocols[i];
934         if (f & usable_protocols & allowed_protocols
935             && try_set_protocol(vconn, f, &cur_protocol)) {
936             return f;
937         }
938     }
939
940     usable_s = ofputil_protocols_to_string(usable_protocols);
941     if (usable_protocols & allowed_protocols) {
942         ovs_fatal(0, "switch does not support any of the usable flow "
943                   "formats (%s)", usable_s);
944     } else {
945         char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
946         ovs_fatal(0, "none of the usable flow formats (%s) is among the "
947                   "allowed flow formats (%s)", usable_s, allowed_s);
948     }
949 }
950
951 static struct vconn *
952 prepare_dump_flows(int argc, char *argv[], bool aggregate,
953                    struct ofpbuf **requestp)
954 {
955     enum ofputil_protocol usable_protocols, protocol;
956     struct ofputil_flow_stats_request fsr;
957     struct vconn *vconn;
958     char *error;
959
960     error = parse_ofp_flow_stats_request_str(&fsr, aggregate,
961                                              argc > 2 ? argv[2] : "",
962                                              &usable_protocols);
963     if (error) {
964         ovs_fatal(0, "%s", error);
965     }
966
967     protocol = open_vconn(argv[1], &vconn);
968     protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
969     *requestp = ofputil_encode_flow_stats_request(&fsr, protocol);
970     return vconn;
971 }
972
973 static void
974 ofctl_dump_flows__(int argc, char *argv[], bool aggregate)
975 {
976     struct ofpbuf *request;
977     struct vconn *vconn;
978
979     vconn = prepare_dump_flows(argc, argv, aggregate, &request);
980     dump_stats_transaction(vconn, request);
981     vconn_close(vconn);
982 }
983
984 static int
985 compare_flows(const void *afs_, const void *bfs_)
986 {
987     const struct ofputil_flow_stats *afs = afs_;
988     const struct ofputil_flow_stats *bfs = bfs_;
989     const struct match *a = &afs->match;
990     const struct match *b = &bfs->match;
991     const struct sort_criterion *sc;
992
993     for (sc = criteria; sc < &criteria[n_criteria]; sc++) {
994         const struct mf_field *f = sc->field;
995         int ret;
996
997         if (!f) {
998             int a_pri = afs->priority;
999             int b_pri = bfs->priority;
1000             ret = a_pri < b_pri ? -1 : a_pri > b_pri;
1001         } else {
1002             bool ina, inb;
1003
1004             ina = mf_are_prereqs_ok(f, &a->flow) && !mf_is_all_wild(f, &a->wc);
1005             inb = mf_are_prereqs_ok(f, &b->flow) && !mf_is_all_wild(f, &b->wc);
1006             if (ina != inb) {
1007                 /* Skip the test for sc->order, so that missing fields always
1008                  * sort to the end whether we're sorting in ascending or
1009                  * descending order. */
1010                 return ina ? -1 : 1;
1011             } else {
1012                 union mf_value aval, bval;
1013
1014                 mf_get_value(f, &a->flow, &aval);
1015                 mf_get_value(f, &b->flow, &bval);
1016                 ret = memcmp(&aval, &bval, f->n_bytes);
1017             }
1018         }
1019
1020         if (ret) {
1021             return sc->order == SORT_ASC ? ret : -ret;
1022         }
1023     }
1024
1025     return 0;
1026 }
1027
1028 static void
1029 ofctl_dump_flows(struct ovs_cmdl_context *ctx)
1030 {
1031     if (!n_criteria) {
1032         ofctl_dump_flows__(ctx->argc, ctx->argv, false);
1033         return;
1034     } else {
1035         struct ofputil_flow_stats *fses;
1036         size_t n_fses, allocated_fses;
1037         struct ofpbuf *request;
1038         struct ofpbuf ofpacts;
1039         struct ofpbuf *reply;
1040         struct vconn *vconn;
1041         ovs_be32 send_xid;
1042         struct ds s;
1043         size_t i;
1044
1045         vconn = prepare_dump_flows(ctx->argc, ctx->argv, false, &request);
1046         send_xid = ((struct ofp_header *) request->data)->xid;
1047         send_openflow_buffer(vconn, request);
1048
1049         fses = NULL;
1050         n_fses = allocated_fses = 0;
1051         reply = NULL;
1052         ofpbuf_init(&ofpacts, 0);
1053         for (;;) {
1054             struct ofputil_flow_stats *fs;
1055
1056             if (n_fses >= allocated_fses) {
1057                 fses = x2nrealloc(fses, &allocated_fses, sizeof *fses);
1058             }
1059
1060             fs = &fses[n_fses];
1061             if (!recv_flow_stats_reply(vconn, send_xid, &reply, fs,
1062                                        &ofpacts)) {
1063                 break;
1064             }
1065             fs->ofpacts = xmemdup(fs->ofpacts, fs->ofpacts_len);
1066             n_fses++;
1067         }
1068         ofpbuf_uninit(&ofpacts);
1069
1070         qsort(fses, n_fses, sizeof *fses, compare_flows);
1071
1072         ds_init(&s);
1073         for (i = 0; i < n_fses; i++) {
1074             ds_clear(&s);
1075             ofp_print_flow_stats(&s, &fses[i]);
1076             puts(ds_cstr(&s));
1077         }
1078         ds_destroy(&s);
1079
1080         for (i = 0; i < n_fses; i++) {
1081             free(CONST_CAST(struct ofpact *, fses[i].ofpacts));
1082         }
1083         free(fses);
1084
1085         vconn_close(vconn);
1086     }
1087 }
1088
1089 static void
1090 ofctl_dump_aggregate(struct ovs_cmdl_context *ctx)
1091 {
1092     ofctl_dump_flows__(ctx->argc, ctx->argv, true);
1093 }
1094
1095 static void
1096 ofctl_queue_stats(struct ovs_cmdl_context *ctx)
1097 {
1098     struct ofpbuf *request;
1099     struct vconn *vconn;
1100     struct ofputil_queue_stats_request oqs;
1101
1102     open_vconn(ctx->argv[1], &vconn);
1103
1104     if (ctx->argc > 2 && ctx->argv[2][0] && strcasecmp(ctx->argv[2], "all")) {
1105         oqs.port_no = str_to_port_no(ctx->argv[1], ctx->argv[2]);
1106     } else {
1107         oqs.port_no = OFPP_ANY;
1108     }
1109     if (ctx->argc > 3 && ctx->argv[3][0] && strcasecmp(ctx->argv[3], "all")) {
1110         oqs.queue_id = atoi(ctx->argv[3]);
1111     } else {
1112         oqs.queue_id = OFPQ_ALL;
1113     }
1114
1115     request = ofputil_encode_queue_stats_request(vconn_get_version(vconn), &oqs);
1116     dump_stats_transaction(vconn, request);
1117     vconn_close(vconn);
1118 }
1119
1120 static void
1121 ofctl_queue_get_config(struct ovs_cmdl_context *ctx)
1122 {
1123     const char *vconn_name = ctx->argv[1];
1124     const char *port_name = ctx->argv[2];
1125     enum ofputil_protocol protocol;
1126     enum ofp_version version;
1127     struct ofpbuf *request;
1128     struct vconn *vconn;
1129     ofp_port_t port;
1130
1131     port = str_to_port_no(vconn_name, port_name);
1132
1133     protocol = open_vconn(vconn_name, &vconn);
1134     version = ofputil_protocol_to_ofp_version(protocol);
1135     request = ofputil_encode_queue_get_config_request(version, port);
1136     dump_transaction(vconn, request);
1137     vconn_close(vconn);
1138 }
1139
1140 static enum ofputil_protocol
1141 open_vconn_for_flow_mod(const char *remote, struct vconn **vconnp,
1142                         enum ofputil_protocol usable_protocols)
1143 {
1144     enum ofputil_protocol cur_protocol;
1145     char *usable_s;
1146     int i;
1147
1148     if (!(usable_protocols & allowed_protocols)) {
1149         char *allowed_s = ofputil_protocols_to_string(allowed_protocols);
1150         usable_s = ofputil_protocols_to_string(usable_protocols);
1151         ovs_fatal(0, "none of the usable flow formats (%s) is among the "
1152                   "allowed flow formats (%s)", usable_s, allowed_s);
1153     }
1154
1155     /* If the initial flow format is allowed and usable, keep it. */
1156     cur_protocol = open_vconn(remote, vconnp);
1157     if (usable_protocols & allowed_protocols & cur_protocol) {
1158         return cur_protocol;
1159     }
1160
1161     /* Otherwise try each flow format in turn. */
1162     for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1163         enum ofputil_protocol f = 1 << i;
1164
1165         if (f != cur_protocol
1166             && f & usable_protocols & allowed_protocols
1167             && try_set_protocol(*vconnp, f, &cur_protocol)) {
1168             return f;
1169         }
1170     }
1171
1172     usable_s = ofputil_protocols_to_string(usable_protocols);
1173     ovs_fatal(0, "switch does not support any of the usable flow "
1174               "formats (%s)", usable_s);
1175 }
1176
1177 static void
1178 ofctl_flow_mod__(const char *remote, struct ofputil_flow_mod *fms,
1179                  size_t n_fms, enum ofputil_protocol usable_protocols)
1180 {
1181     enum ofputil_protocol protocol;
1182     struct vconn *vconn;
1183     size_t i;
1184
1185     protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
1186
1187     for (i = 0; i < n_fms; i++) {
1188         struct ofputil_flow_mod *fm = &fms[i];
1189
1190         transact_noreply(vconn, ofputil_encode_flow_mod(fm, protocol));
1191         free(CONST_CAST(struct ofpact *, fm->ofpacts));
1192     }
1193     vconn_close(vconn);
1194 }
1195
1196 static void
1197 ofctl_flow_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
1198 {
1199     enum ofputil_protocol usable_protocols;
1200     struct ofputil_flow_mod *fms = NULL;
1201     size_t n_fms = 0;
1202     char *error;
1203
1204     error = parse_ofp_flow_mod_file(argv[2], command, &fms, &n_fms,
1205                                     &usable_protocols);
1206     if (error) {
1207         ovs_fatal(0, "%s", error);
1208     }
1209     ofctl_flow_mod__(argv[1], fms, n_fms, usable_protocols);
1210     free(fms);
1211 }
1212
1213 static void
1214 ofctl_flow_mod(int argc, char *argv[], uint16_t command)
1215 {
1216     if (argc > 2 && !strcmp(argv[2], "-")) {
1217         ofctl_flow_mod_file(argc, argv, command);
1218     } else {
1219         struct ofputil_flow_mod fm;
1220         char *error;
1221         enum ofputil_protocol usable_protocols;
1222
1223         error = parse_ofp_flow_mod_str(&fm, argc > 2 ? argv[2] : "", command,
1224                                        &usable_protocols);
1225         if (error) {
1226             ovs_fatal(0, "%s", error);
1227         }
1228         ofctl_flow_mod__(argv[1], &fm, 1, usable_protocols);
1229     }
1230 }
1231
1232 static void
1233 ofctl_add_flow(struct ovs_cmdl_context *ctx)
1234 {
1235     ofctl_flow_mod(ctx->argc, ctx->argv, OFPFC_ADD);
1236 }
1237
1238 static void
1239 ofctl_add_flows(struct ovs_cmdl_context *ctx)
1240 {
1241     ofctl_flow_mod_file(ctx->argc, ctx->argv, OFPFC_ADD);
1242 }
1243
1244 static void
1245 ofctl_mod_flows(struct ovs_cmdl_context *ctx)
1246 {
1247     ofctl_flow_mod(ctx->argc, ctx->argv, strict ? OFPFC_MODIFY_STRICT : OFPFC_MODIFY);
1248 }
1249
1250 static void
1251 ofctl_del_flows(struct ovs_cmdl_context *ctx)
1252 {
1253     ofctl_flow_mod(ctx->argc, ctx->argv, strict ? OFPFC_DELETE_STRICT : OFPFC_DELETE);
1254 }
1255
1256 static void
1257 set_packet_in_format(struct vconn *vconn,
1258                      enum nx_packet_in_format packet_in_format)
1259 {
1260     struct ofpbuf *spif;
1261
1262     spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1263                                              packet_in_format);
1264     transact_noreply(vconn, spif);
1265     VLOG_DBG("%s: using user-specified packet in format %s",
1266              vconn_get_name(vconn),
1267              ofputil_packet_in_format_to_string(packet_in_format));
1268 }
1269
1270 static int
1271 monitor_set_invalid_ttl_to_controller(struct vconn *vconn)
1272 {
1273     struct ofp_switch_config config;
1274     enum ofp_config_flags flags;
1275
1276     fetch_switch_config(vconn, &config);
1277     flags = ntohs(config.flags);
1278     if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1279         /* Set the invalid ttl config. */
1280         flags |= OFPC_INVALID_TTL_TO_CONTROLLER;
1281
1282         config.flags = htons(flags);
1283         set_switch_config(vconn, &config);
1284
1285         /* Then retrieve the configuration to see if it really took.  OpenFlow
1286          * doesn't define error reporting for bad modes, so this is all we can
1287          * do. */
1288         fetch_switch_config(vconn, &config);
1289         flags = ntohs(config.flags);
1290         if (!(flags & OFPC_INVALID_TTL_TO_CONTROLLER)) {
1291             ovs_fatal(0, "setting invalid_ttl_to_controller failed (this "
1292                       "switch probably doesn't support mode)");
1293             return -EOPNOTSUPP;
1294         }
1295     }
1296     return 0;
1297 }
1298
1299 /* Converts hex digits in 'hex' to an OpenFlow message in '*msgp'.  The
1300  * caller must free '*msgp'.  On success, returns NULL.  On failure, returns
1301  * an error message and stores NULL in '*msgp'. */
1302 static const char *
1303 openflow_from_hex(const char *hex, struct ofpbuf **msgp)
1304 {
1305     struct ofp_header *oh;
1306     struct ofpbuf *msg;
1307
1308     msg = ofpbuf_new(strlen(hex) / 2);
1309     *msgp = NULL;
1310
1311     if (ofpbuf_put_hex(msg, hex, NULL)[0] != '\0') {
1312         ofpbuf_delete(msg);
1313         return "Trailing garbage in hex data";
1314     }
1315
1316     if (msg->size < sizeof(struct ofp_header)) {
1317         ofpbuf_delete(msg);
1318         return "Message too short for OpenFlow";
1319     }
1320
1321     oh = msg->data;
1322     if (msg->size != ntohs(oh->length)) {
1323         ofpbuf_delete(msg);
1324         return "Message size does not match length in OpenFlow header";
1325     }
1326
1327     *msgp = msg;
1328     return NULL;
1329 }
1330
1331 static void
1332 ofctl_send(struct unixctl_conn *conn, int argc,
1333            const char *argv[], void *vconn_)
1334 {
1335     struct vconn *vconn = vconn_;
1336     struct ds reply;
1337     bool ok;
1338     int i;
1339
1340     ok = true;
1341     ds_init(&reply);
1342     for (i = 1; i < argc; i++) {
1343         const char *error_msg;
1344         struct ofpbuf *msg;
1345         int error;
1346
1347         error_msg = openflow_from_hex(argv[i], &msg);
1348         if (error_msg) {
1349             ds_put_format(&reply, "%s\n", error_msg);
1350             ok = false;
1351             continue;
1352         }
1353
1354         fprintf(stderr, "send: ");
1355         ofp_print(stderr, msg->data, msg->size, verbosity);
1356
1357         error = vconn_send_block(vconn, msg);
1358         if (error) {
1359             ofpbuf_delete(msg);
1360             ds_put_format(&reply, "%s\n", ovs_strerror(error));
1361             ok = false;
1362         } else {
1363             ds_put_cstr(&reply, "sent\n");
1364         }
1365     }
1366
1367     if (ok) {
1368         unixctl_command_reply(conn, ds_cstr(&reply));
1369     } else {
1370         unixctl_command_reply_error(conn, ds_cstr(&reply));
1371     }
1372     ds_destroy(&reply);
1373 }
1374
1375 struct barrier_aux {
1376     struct vconn *vconn;        /* OpenFlow connection for sending barrier. */
1377     struct unixctl_conn *conn;  /* Connection waiting for barrier response. */
1378 };
1379
1380 static void
1381 ofctl_barrier(struct unixctl_conn *conn, int argc OVS_UNUSED,
1382               const char *argv[] OVS_UNUSED, void *aux_)
1383 {
1384     struct barrier_aux *aux = aux_;
1385     struct ofpbuf *msg;
1386     int error;
1387
1388     if (aux->conn) {
1389         unixctl_command_reply_error(conn, "already waiting for barrier reply");
1390         return;
1391     }
1392
1393     msg = ofputil_encode_barrier_request(vconn_get_version(aux->vconn));
1394     error = vconn_send_block(aux->vconn, msg);
1395     if (error) {
1396         ofpbuf_delete(msg);
1397         unixctl_command_reply_error(conn, ovs_strerror(error));
1398     } else {
1399         aux->conn = conn;
1400     }
1401 }
1402
1403 static void
1404 ofctl_set_output_file(struct unixctl_conn *conn, int argc OVS_UNUSED,
1405                       const char *argv[], void *aux OVS_UNUSED)
1406 {
1407     int fd;
1408
1409     fd = open(argv[1], O_CREAT | O_TRUNC | O_WRONLY, 0666);
1410     if (fd < 0) {
1411         unixctl_command_reply_error(conn, ovs_strerror(errno));
1412         return;
1413     }
1414
1415     fflush(stderr);
1416     dup2(fd, STDERR_FILENO);
1417     close(fd);
1418     unixctl_command_reply(conn, NULL);
1419 }
1420
1421 static void
1422 ofctl_block(struct unixctl_conn *conn, int argc OVS_UNUSED,
1423             const char *argv[] OVS_UNUSED, void *blocked_)
1424 {
1425     bool *blocked = blocked_;
1426
1427     if (!*blocked) {
1428         *blocked = true;
1429         unixctl_command_reply(conn, NULL);
1430     } else {
1431         unixctl_command_reply(conn, "already blocking");
1432     }
1433 }
1434
1435 static void
1436 ofctl_unblock(struct unixctl_conn *conn, int argc OVS_UNUSED,
1437               const char *argv[] OVS_UNUSED, void *blocked_)
1438 {
1439     bool *blocked = blocked_;
1440
1441     if (*blocked) {
1442         *blocked = false;
1443         unixctl_command_reply(conn, NULL);
1444     } else {
1445         unixctl_command_reply(conn, "already unblocked");
1446     }
1447 }
1448
1449 /* Prints to stdout all of the messages received on 'vconn'.
1450  *
1451  * Iff 'reply_to_echo_requests' is true, sends a reply to any echo request
1452  * received on 'vconn'. */
1453 static void
1454 monitor_vconn(struct vconn *vconn, bool reply_to_echo_requests)
1455 {
1456     struct barrier_aux barrier_aux = { vconn, NULL };
1457     struct unixctl_server *server;
1458     bool exiting = false;
1459     bool blocked = false;
1460     int error;
1461
1462     daemon_save_fd(STDERR_FILENO);
1463     daemonize_start();
1464     error = unixctl_server_create(unixctl_path, &server);
1465     if (error) {
1466         ovs_fatal(error, "failed to create unixctl server");
1467     }
1468     unixctl_command_register("exit", "", 0, 0, ofctl_exit, &exiting);
1469     unixctl_command_register("ofctl/send", "OFMSG...", 1, INT_MAX,
1470                              ofctl_send, vconn);
1471     unixctl_command_register("ofctl/barrier", "", 0, 0,
1472                              ofctl_barrier, &barrier_aux);
1473     unixctl_command_register("ofctl/set-output-file", "FILE", 1, 1,
1474                              ofctl_set_output_file, NULL);
1475
1476     unixctl_command_register("ofctl/block", "", 0, 0, ofctl_block, &blocked);
1477     unixctl_command_register("ofctl/unblock", "", 0, 0, ofctl_unblock,
1478                              &blocked);
1479
1480     daemonize_complete();
1481
1482     for (;;) {
1483         struct ofpbuf *b;
1484         int retval;
1485
1486         unixctl_server_run(server);
1487
1488         while (!blocked) {
1489             enum ofptype type;
1490
1491             retval = vconn_recv(vconn, &b);
1492             if (retval == EAGAIN) {
1493                 break;
1494             }
1495             run(retval, "vconn_recv");
1496
1497             if (timestamp) {
1498                 char *s = xastrftime_msec("%Y-%m-%d %H:%M:%S.###: ",
1499                                           time_wall_msec(), true);
1500                 fputs(s, stderr);
1501                 free(s);
1502             }
1503
1504             ofptype_decode(&type, b->data);
1505             ofp_print(stderr, b->data, b->size, verbosity + 2);
1506             fflush(stderr);
1507
1508             switch ((int) type) {
1509             case OFPTYPE_BARRIER_REPLY:
1510                 if (barrier_aux.conn) {
1511                     unixctl_command_reply(barrier_aux.conn, NULL);
1512                     barrier_aux.conn = NULL;
1513                 }
1514                 break;
1515
1516             case OFPTYPE_ECHO_REQUEST:
1517                 if (reply_to_echo_requests) {
1518                     struct ofpbuf *reply;
1519
1520                     reply = make_echo_reply(b->data);
1521                     retval = vconn_send_block(vconn, reply);
1522                     if (retval) {
1523                         ovs_fatal(retval, "failed to send echo reply");
1524                     }
1525                 }
1526                 break;
1527             }
1528             ofpbuf_delete(b);
1529         }
1530
1531         if (exiting) {
1532             break;
1533         }
1534
1535         vconn_run(vconn);
1536         vconn_run_wait(vconn);
1537         if (!blocked) {
1538             vconn_recv_wait(vconn);
1539         }
1540         unixctl_server_wait(server);
1541         poll_block();
1542     }
1543     vconn_close(vconn);
1544     unixctl_server_destroy(server);
1545 }
1546
1547 static void
1548 ofctl_monitor(struct ovs_cmdl_context *ctx)
1549 {
1550     struct vconn *vconn;
1551     int i;
1552     enum ofputil_protocol usable_protocols;
1553
1554     open_vconn(ctx->argv[1], &vconn);
1555     for (i = 2; i < ctx->argc; i++) {
1556         const char *arg = ctx->argv[i];
1557
1558         if (isdigit((unsigned char) *arg)) {
1559             struct ofp_switch_config config;
1560
1561             fetch_switch_config(vconn, &config);
1562             config.miss_send_len = htons(atoi(arg));
1563             set_switch_config(vconn, &config);
1564         } else if (!strcmp(arg, "invalid_ttl")) {
1565             monitor_set_invalid_ttl_to_controller(vconn);
1566         } else if (!strncmp(arg, "watch:", 6)) {
1567             struct ofputil_flow_monitor_request fmr;
1568             struct ofpbuf *msg;
1569             char *error;
1570
1571             error = parse_flow_monitor_request(&fmr, arg + 6,
1572                                                &usable_protocols);
1573             if (error) {
1574                 ovs_fatal(0, "%s", error);
1575             }
1576
1577             msg = ofpbuf_new(0);
1578             ofputil_append_flow_monitor_request(&fmr, msg);
1579             dump_stats_transaction(vconn, msg);
1580             fflush(stdout);
1581         } else {
1582             ovs_fatal(0, "%s: unsupported \"monitor\" argument", arg);
1583         }
1584     }
1585
1586     if (preferred_packet_in_format >= 0) {
1587         set_packet_in_format(vconn, preferred_packet_in_format);
1588     } else {
1589         enum ofp_version version = vconn_get_version(vconn);
1590
1591         switch (version) {
1592         case OFP10_VERSION: {
1593             struct ofpbuf *spif, *reply;
1594
1595             spif = ofputil_make_set_packet_in_format(vconn_get_version(vconn),
1596                                                      NXPIF_NXM);
1597             run(vconn_transact_noreply(vconn, spif, &reply),
1598                 "talking to %s", vconn_get_name(vconn));
1599             if (reply) {
1600                 char *s = ofp_to_string(reply->data, reply->size, 2);
1601                 VLOG_DBG("%s: failed to set packet in format to nxm, controller"
1602                         " replied: %s. Falling back to the switch default.",
1603                         vconn_get_name(vconn), s);
1604                 free(s);
1605                 ofpbuf_delete(reply);
1606             }
1607             break;
1608         }
1609         case OFP11_VERSION:
1610         case OFP12_VERSION:
1611         case OFP13_VERSION:
1612         case OFP14_VERSION:
1613         case OFP15_VERSION:
1614             break;
1615         default:
1616             OVS_NOT_REACHED();
1617         }
1618     }
1619
1620     monitor_vconn(vconn, true);
1621 }
1622
1623 static void
1624 ofctl_snoop(struct ovs_cmdl_context *ctx)
1625 {
1626     struct vconn *vconn;
1627
1628     open_vconn__(ctx->argv[1], SNOOP, &vconn);
1629     monitor_vconn(vconn, false);
1630 }
1631
1632 static void
1633 ofctl_dump_ports(struct ovs_cmdl_context *ctx)
1634 {
1635     struct ofpbuf *request;
1636     struct vconn *vconn;
1637     ofp_port_t port;
1638
1639     open_vconn(ctx->argv[1], &vconn);
1640     port = ctx->argc > 2 ? str_to_port_no(ctx->argv[1], ctx->argv[2]) : OFPP_ANY;
1641     request = ofputil_encode_dump_ports_request(vconn_get_version(vconn), port);
1642     dump_stats_transaction(vconn, request);
1643     vconn_close(vconn);
1644 }
1645
1646 static void
1647 ofctl_dump_ports_desc(struct ovs_cmdl_context *ctx)
1648 {
1649     struct ofpbuf *request;
1650     struct vconn *vconn;
1651     ofp_port_t port;
1652
1653     open_vconn(ctx->argv[1], &vconn);
1654     port = ctx->argc > 2 ? str_to_port_no(ctx->argv[1], ctx->argv[2]) : OFPP_ANY;
1655     request = ofputil_encode_port_desc_stats_request(vconn_get_version(vconn),
1656                                                      port);
1657     dump_stats_transaction(vconn, request);
1658     vconn_close(vconn);
1659 }
1660
1661 static void
1662 ofctl_probe(struct ovs_cmdl_context *ctx)
1663 {
1664     struct ofpbuf *request;
1665     struct vconn *vconn;
1666     struct ofpbuf *reply;
1667
1668     open_vconn(ctx->argv[1], &vconn);
1669     request = make_echo_request(vconn_get_version(vconn));
1670     run(vconn_transact(vconn, request, &reply), "talking to %s", ctx->argv[1]);
1671     if (reply->size != sizeof(struct ofp_header)) {
1672         ovs_fatal(0, "reply does not match request");
1673     }
1674     ofpbuf_delete(reply);
1675     vconn_close(vconn);
1676 }
1677
1678 static void
1679 ofctl_packet_out(struct ovs_cmdl_context *ctx)
1680 {
1681     enum ofputil_protocol protocol;
1682     struct ofputil_packet_out po;
1683     struct ofpbuf ofpacts;
1684     struct vconn *vconn;
1685     char *error;
1686     int i;
1687     enum ofputil_protocol usable_protocols; /* XXX: Use in proto selection */
1688
1689     ofpbuf_init(&ofpacts, 64);
1690     error = ofpacts_parse_actions(ctx->argv[3], &ofpacts, &usable_protocols);
1691     if (error) {
1692         ovs_fatal(0, "%s", error);
1693     }
1694
1695     po.buffer_id = UINT32_MAX;
1696     po.in_port = str_to_port_no(ctx->argv[1], ctx->argv[2]);
1697     po.ofpacts = ofpacts.data;
1698     po.ofpacts_len = ofpacts.size;
1699
1700     protocol = open_vconn(ctx->argv[1], &vconn);
1701     for (i = 4; i < ctx->argc; i++) {
1702         struct dp_packet *packet;
1703         struct ofpbuf *opo;
1704         const char *error_msg;
1705
1706         error_msg = eth_from_hex(ctx->argv[i], &packet);
1707         if (error_msg) {
1708             ovs_fatal(0, "%s", error_msg);
1709         }
1710
1711         po.packet = dp_packet_data(packet);
1712         po.packet_len = dp_packet_size(packet);
1713         opo = ofputil_encode_packet_out(&po, protocol);
1714         transact_noreply(vconn, opo);
1715         dp_packet_delete(packet);
1716     }
1717     vconn_close(vconn);
1718     ofpbuf_uninit(&ofpacts);
1719 }
1720
1721 static void
1722 ofctl_mod_port(struct ovs_cmdl_context *ctx)
1723 {
1724     struct ofp_config_flag {
1725         const char *name;             /* The flag's name. */
1726         enum ofputil_port_config bit; /* Bit to turn on or off. */
1727         bool on;                      /* Value to set the bit to. */
1728     };
1729     static const struct ofp_config_flag flags[] = {
1730         { "up",          OFPUTIL_PC_PORT_DOWN,    false },
1731         { "down",        OFPUTIL_PC_PORT_DOWN,    true  },
1732         { "stp",         OFPUTIL_PC_NO_STP,       false },
1733         { "receive",     OFPUTIL_PC_NO_RECV,      false },
1734         { "receive-stp", OFPUTIL_PC_NO_RECV_STP,  false },
1735         { "flood",       OFPUTIL_PC_NO_FLOOD,     false },
1736         { "forward",     OFPUTIL_PC_NO_FWD,       false },
1737         { "packet-in",   OFPUTIL_PC_NO_PACKET_IN, false },
1738     };
1739
1740     const struct ofp_config_flag *flag;
1741     enum ofputil_protocol protocol;
1742     struct ofputil_port_mod pm;
1743     struct ofputil_phy_port pp;
1744     struct vconn *vconn;
1745     const char *command;
1746     bool not;
1747
1748     fetch_ofputil_phy_port(ctx->argv[1], ctx->argv[2], &pp);
1749
1750     pm.port_no = pp.port_no;
1751     memcpy(pm.hw_addr, pp.hw_addr, ETH_ADDR_LEN);
1752     pm.config = 0;
1753     pm.mask = 0;
1754     pm.advertise = 0;
1755
1756     if (!strncasecmp(ctx->argv[3], "no-", 3)) {
1757         command = ctx->argv[3] + 3;
1758         not = true;
1759     } else if (!strncasecmp(ctx->argv[3], "no", 2)) {
1760         command = ctx->argv[3] + 2;
1761         not = true;
1762     } else {
1763         command = ctx->argv[3];
1764         not = false;
1765     }
1766     for (flag = flags; flag < &flags[ARRAY_SIZE(flags)]; flag++) {
1767         if (!strcasecmp(command, flag->name)) {
1768             pm.mask = flag->bit;
1769             pm.config = flag->on ^ not ? flag->bit : 0;
1770             goto found;
1771         }
1772     }
1773     ovs_fatal(0, "unknown mod-port command '%s'", ctx->argv[3]);
1774
1775 found:
1776     protocol = open_vconn(ctx->argv[1], &vconn);
1777     transact_noreply(vconn, ofputil_encode_port_mod(&pm, protocol));
1778     vconn_close(vconn);
1779 }
1780
1781 static void
1782 ofctl_mod_table(struct ovs_cmdl_context *ctx)
1783 {
1784     enum ofputil_protocol protocol, usable_protocols;
1785     struct ofputil_table_mod tm;
1786     struct vconn *vconn;
1787     char *error;
1788     int i;
1789
1790     error = parse_ofp_table_mod(&tm, ctx->argv[2], ctx->argv[3], &usable_protocols);
1791     if (error) {
1792         ovs_fatal(0, "%s", error);
1793     }
1794
1795     protocol = open_vconn(ctx->argv[1], &vconn);
1796     if (!(protocol & usable_protocols)) {
1797         for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
1798             enum ofputil_protocol f = 1 << i;
1799             if (f != protocol
1800                 && f & usable_protocols
1801                 && try_set_protocol(vconn, f, &protocol)) {
1802                 protocol = f;
1803                 break;
1804             }
1805         }
1806     }
1807
1808     if (!(protocol & usable_protocols)) {
1809         char *usable_s = ofputil_protocols_to_string(usable_protocols);
1810         ovs_fatal(0, "Switch does not support table mod message(%s)", usable_s);
1811     }
1812
1813     transact_noreply(vconn, ofputil_encode_table_mod(&tm, protocol));
1814     vconn_close(vconn);
1815 }
1816
1817 static void
1818 ofctl_get_frags(struct ovs_cmdl_context *ctx)
1819 {
1820     struct ofp_switch_config config;
1821     struct vconn *vconn;
1822
1823     open_vconn(ctx->argv[1], &vconn);
1824     fetch_switch_config(vconn, &config);
1825     puts(ofputil_frag_handling_to_string(ntohs(config.flags)));
1826     vconn_close(vconn);
1827 }
1828
1829 static void
1830 ofctl_set_frags(struct ovs_cmdl_context *ctx)
1831 {
1832     struct ofp_switch_config config;
1833     enum ofp_config_flags mode;
1834     struct vconn *vconn;
1835     ovs_be16 flags;
1836
1837     if (!ofputil_frag_handling_from_string(ctx->argv[2], &mode)) {
1838         ovs_fatal(0, "%s: unknown fragment handling mode", ctx->argv[2]);
1839     }
1840
1841     open_vconn(ctx->argv[1], &vconn);
1842     fetch_switch_config(vconn, &config);
1843     flags = htons(mode) | (config.flags & htons(~OFPC_FRAG_MASK));
1844     if (flags != config.flags) {
1845         /* Set the configuration. */
1846         config.flags = flags;
1847         set_switch_config(vconn, &config);
1848
1849         /* Then retrieve the configuration to see if it really took.  OpenFlow
1850          * doesn't define error reporting for bad modes, so this is all we can
1851          * do. */
1852         fetch_switch_config(vconn, &config);
1853         if (flags != config.flags) {
1854             ovs_fatal(0, "%s: setting fragment handling mode failed (this "
1855                       "switch probably doesn't support mode \"%s\")",
1856                       ctx->argv[1], ofputil_frag_handling_to_string(mode));
1857         }
1858     }
1859     vconn_close(vconn);
1860 }
1861
1862 static void
1863 ofctl_ofp_parse(struct ovs_cmdl_context *ctx)
1864 {
1865     const char *filename = ctx->argv[1];
1866     struct ofpbuf b;
1867     FILE *file;
1868
1869     file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
1870     if (file == NULL) {
1871         ovs_fatal(errno, "%s: open", filename);
1872     }
1873
1874     ofpbuf_init(&b, 65536);
1875     for (;;) {
1876         struct ofp_header *oh;
1877         size_t length, tail_len;
1878         void *tail;
1879         size_t n;
1880
1881         ofpbuf_clear(&b);
1882         oh = ofpbuf_put_uninit(&b, sizeof *oh);
1883         n = fread(oh, 1, sizeof *oh, file);
1884         if (n == 0) {
1885             break;
1886         } else if (n < sizeof *oh) {
1887             ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1888         }
1889
1890         length = ntohs(oh->length);
1891         if (length < sizeof *oh) {
1892             ovs_fatal(0, "%s: %"PRIuSIZE"-byte message is too short for OpenFlow",
1893                       filename, length);
1894         }
1895
1896         tail_len = length - sizeof *oh;
1897         tail = ofpbuf_put_uninit(&b, tail_len);
1898         n = fread(tail, 1, tail_len, file);
1899         if (n < tail_len) {
1900             ovs_fatal(0, "%s: unexpected end of file mid-message", filename);
1901         }
1902
1903         ofp_print(stdout, b.data, b.size, verbosity + 2);
1904     }
1905     ofpbuf_uninit(&b);
1906
1907     if (file != stdin) {
1908         fclose(file);
1909     }
1910 }
1911
1912 static bool
1913 is_openflow_port(ovs_be16 port_, char *ports[])
1914 {
1915     uint16_t port = ntohs(port_);
1916     if (ports[0]) {
1917         int i;
1918
1919         for (i = 0; ports[i]; i++) {
1920             if (port == atoi(ports[i])) {
1921                 return true;
1922             }
1923         }
1924         return false;
1925     } else {
1926         return port == OFP_PORT || port == OFP_OLD_PORT;
1927     }
1928 }
1929
1930 static void
1931 ofctl_ofp_parse_pcap(struct ovs_cmdl_context *ctx)
1932 {
1933     struct tcp_reader *reader;
1934     FILE *file;
1935     int error;
1936     bool first;
1937
1938     file = ovs_pcap_open(ctx->argv[1], "rb");
1939     if (!file) {
1940         ovs_fatal(errno, "%s: open failed", ctx->argv[1]);
1941     }
1942
1943     reader = tcp_reader_open();
1944     first = true;
1945     for (;;) {
1946         struct dp_packet *packet;
1947         long long int when;
1948         struct flow flow;
1949
1950         error = ovs_pcap_read(file, &packet, &when);
1951         if (error) {
1952             break;
1953         }
1954         packet->md = PKT_METADATA_INITIALIZER(ODPP_NONE);
1955         flow_extract(packet, &flow);
1956         if (flow.dl_type == htons(ETH_TYPE_IP)
1957             && flow.nw_proto == IPPROTO_TCP
1958             && (is_openflow_port(flow.tp_src, ctx->argv + 2) ||
1959                 is_openflow_port(flow.tp_dst, ctx->argv + 2))) {
1960             struct dp_packet *payload = tcp_reader_run(reader, &flow, packet);
1961             if (payload) {
1962                 while (dp_packet_size(payload) >= sizeof(struct ofp_header)) {
1963                     const struct ofp_header *oh;
1964                     void *data = dp_packet_data(payload);
1965                     int length;
1966
1967                     /* Align OpenFlow on 8-byte boundary for safe access. */
1968                     dp_packet_shift(payload, -((intptr_t) data & 7));
1969
1970                     oh = dp_packet_data(payload);
1971                     length = ntohs(oh->length);
1972                     if (dp_packet_size(payload) < length) {
1973                         break;
1974                     }
1975
1976                     if (!first) {
1977                         putchar('\n');
1978                     }
1979                     first = false;
1980
1981                     if (timestamp) {
1982                         char *s = xastrftime_msec("%H:%M:%S.### ", when, true);
1983                         fputs(s, stdout);
1984                         free(s);
1985                     }
1986
1987                     printf(IP_FMT".%"PRIu16" > "IP_FMT".%"PRIu16":\n",
1988                            IP_ARGS(flow.nw_src), ntohs(flow.tp_src),
1989                            IP_ARGS(flow.nw_dst), ntohs(flow.tp_dst));
1990                     ofp_print(stdout, dp_packet_data(payload), length, verbosity + 1);
1991                     dp_packet_pull(payload, length);
1992                 }
1993             }
1994         }
1995         dp_packet_delete(packet);
1996     }
1997     tcp_reader_close(reader);
1998 }
1999
2000 static void
2001 ofctl_ping(struct ovs_cmdl_context *ctx)
2002 {
2003     size_t max_payload = 65535 - sizeof(struct ofp_header);
2004     unsigned int payload;
2005     struct vconn *vconn;
2006     int i;
2007
2008     payload = ctx->argc > 2 ? atoi(ctx->argv[2]) : 64;
2009     if (payload > max_payload) {
2010         ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
2011     }
2012
2013     open_vconn(ctx->argv[1], &vconn);
2014     for (i = 0; i < 10; i++) {
2015         struct timeval start, end;
2016         struct ofpbuf *request, *reply;
2017         const struct ofp_header *rpy_hdr;
2018         enum ofptype type;
2019
2020         request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
2021                                vconn_get_version(vconn), payload);
2022         random_bytes(ofpbuf_put_uninit(request, payload), payload);
2023
2024         xgettimeofday(&start);
2025         run(vconn_transact(vconn, ofpbuf_clone(request), &reply), "transact");
2026         xgettimeofday(&end);
2027
2028         rpy_hdr = reply->data;
2029         if (ofptype_pull(&type, reply)
2030             || type != OFPTYPE_ECHO_REPLY
2031             || reply->size != payload
2032             || memcmp(request->msg, reply->msg, payload)) {
2033             printf("Reply does not match request.  Request:\n");
2034             ofp_print(stdout, request, request->size, verbosity + 2);
2035             printf("Reply:\n");
2036             ofp_print(stdout, reply, reply->size, verbosity + 2);
2037         }
2038         printf("%"PRIu32" bytes from %s: xid=%08"PRIx32" time=%.1f ms\n",
2039                reply->size, ctx->argv[1], ntohl(rpy_hdr->xid),
2040                    (1000*(double)(end.tv_sec - start.tv_sec))
2041                    + (.001*(end.tv_usec - start.tv_usec)));
2042         ofpbuf_delete(request);
2043         ofpbuf_delete(reply);
2044     }
2045     vconn_close(vconn);
2046 }
2047
2048 static void
2049 ofctl_benchmark(struct ovs_cmdl_context *ctx)
2050 {
2051     size_t max_payload = 65535 - sizeof(struct ofp_header);
2052     struct timeval start, end;
2053     unsigned int payload_size, message_size;
2054     struct vconn *vconn;
2055     double duration;
2056     int count;
2057     int i;
2058
2059     payload_size = atoi(ctx->argv[2]);
2060     if (payload_size > max_payload) {
2061         ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload);
2062     }
2063     message_size = sizeof(struct ofp_header) + payload_size;
2064
2065     count = atoi(ctx->argv[3]);
2066
2067     printf("Sending %d packets * %u bytes (with header) = %u bytes total\n",
2068            count, message_size, count * message_size);
2069
2070     open_vconn(ctx->argv[1], &vconn);
2071     xgettimeofday(&start);
2072     for (i = 0; i < count; i++) {
2073         struct ofpbuf *request, *reply;
2074
2075         request = ofpraw_alloc(OFPRAW_OFPT_ECHO_REQUEST,
2076                                vconn_get_version(vconn), payload_size);
2077         ofpbuf_put_zeros(request, payload_size);
2078         run(vconn_transact(vconn, request, &reply), "transact");
2079         ofpbuf_delete(reply);
2080     }
2081     xgettimeofday(&end);
2082     vconn_close(vconn);
2083
2084     duration = ((1000*(double)(end.tv_sec - start.tv_sec))
2085                 + (.001*(end.tv_usec - start.tv_usec)));
2086     printf("Finished in %.1f ms (%.0f packets/s) (%.0f bytes/s)\n",
2087            duration, count / (duration / 1000.0),
2088            count * message_size / (duration / 1000.0));
2089 }
2090
2091 static void
2092 ofctl_group_mod__(const char *remote, struct ofputil_group_mod *gms,
2093                   size_t n_gms, enum ofputil_protocol usable_protocols)
2094 {
2095     enum ofputil_protocol protocol;
2096     struct ofputil_group_mod *gm;
2097     enum ofp_version version;
2098     struct ofpbuf *request;
2099
2100     struct vconn *vconn;
2101     size_t i;
2102
2103     protocol = open_vconn_for_flow_mod(remote, &vconn, usable_protocols);
2104     version = ofputil_protocol_to_ofp_version(protocol);
2105
2106     for (i = 0; i < n_gms; i++) {
2107         gm = &gms[i];
2108         request = ofputil_encode_group_mod(version, gm);
2109         if (request) {
2110             transact_noreply(vconn, request);
2111         }
2112     }
2113
2114     vconn_close(vconn);
2115
2116 }
2117
2118
2119 static void
2120 ofctl_group_mod_file(int argc OVS_UNUSED, char *argv[], uint16_t command)
2121 {
2122     struct ofputil_group_mod *gms = NULL;
2123     enum ofputil_protocol usable_protocols;
2124     size_t n_gms = 0;
2125     char *error;
2126     int i;
2127
2128     error = parse_ofp_group_mod_file(argv[2], command, &gms, &n_gms,
2129                                      &usable_protocols);
2130     if (error) {
2131         ovs_fatal(0, "%s", error);
2132     }
2133     ofctl_group_mod__(argv[1], gms, n_gms, usable_protocols);
2134     for (i = 0; i < n_gms; i++) {
2135         ofputil_bucket_list_destroy(&gms[i].buckets);
2136     }
2137     free(gms);
2138 }
2139
2140 static void
2141 ofctl_group_mod(int argc, char *argv[], uint16_t command)
2142 {
2143     if (argc > 2 && !strcmp(argv[2], "-")) {
2144         ofctl_group_mod_file(argc, argv, command);
2145     } else {
2146         enum ofputil_protocol usable_protocols;
2147         struct ofputil_group_mod gm;
2148         char *error;
2149
2150         error = parse_ofp_group_mod_str(&gm, command, argc > 2 ? argv[2] : "",
2151                                         &usable_protocols);
2152         if (error) {
2153             ovs_fatal(0, "%s", error);
2154         }
2155         ofctl_group_mod__(argv[1], &gm, 1, usable_protocols);
2156         ofputil_bucket_list_destroy(&gm.buckets);
2157     }
2158 }
2159
2160 static void
2161 ofctl_add_group(struct ovs_cmdl_context *ctx)
2162 {
2163     ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_ADD);
2164 }
2165
2166 static void
2167 ofctl_add_groups(struct ovs_cmdl_context *ctx)
2168 {
2169     ofctl_group_mod_file(ctx->argc, ctx->argv, OFPGC11_ADD);
2170 }
2171
2172 static void
2173 ofctl_mod_group(struct ovs_cmdl_context *ctx)
2174 {
2175     ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_MODIFY);
2176 }
2177
2178 static void
2179 ofctl_del_groups(struct ovs_cmdl_context *ctx)
2180 {
2181     ofctl_group_mod(ctx->argc, ctx->argv, OFPGC11_DELETE);
2182 }
2183
2184 static void
2185 ofctl_insert_bucket(struct ovs_cmdl_context *ctx)
2186 {
2187     ofctl_group_mod(ctx->argc, ctx->argv, OFPGC15_INSERT_BUCKET);
2188 }
2189
2190 static void
2191 ofctl_remove_bucket(struct ovs_cmdl_context *ctx)
2192 {
2193     ofctl_group_mod(ctx->argc, ctx->argv, OFPGC15_REMOVE_BUCKET);
2194 }
2195
2196 static void
2197 ofctl_dump_group_stats(struct ovs_cmdl_context *ctx)
2198 {
2199     enum ofputil_protocol usable_protocols;
2200     struct ofputil_group_mod gm;
2201     struct ofpbuf *request;
2202     struct vconn *vconn;
2203     uint32_t group_id;
2204     char *error;
2205
2206     memset(&gm, 0, sizeof gm);
2207
2208     error = parse_ofp_group_mod_str(&gm, OFPGC11_DELETE,
2209                                     ctx->argc > 2 ? ctx->argv[2] : "",
2210                                     &usable_protocols);
2211     if (error) {
2212         ovs_fatal(0, "%s", error);
2213     }
2214
2215     group_id = gm.group_id;
2216
2217     open_vconn(ctx->argv[1], &vconn);
2218     request = ofputil_encode_group_stats_request(vconn_get_version(vconn),
2219                                                  group_id);
2220     if (request) {
2221         dump_stats_transaction(vconn, request);
2222     }
2223
2224     vconn_close(vconn);
2225 }
2226
2227 static void
2228 ofctl_dump_group_desc(struct ovs_cmdl_context *ctx)
2229 {
2230     struct ofpbuf *request;
2231     struct vconn *vconn;
2232     uint32_t group_id;
2233
2234     open_vconn(ctx->argv[1], &vconn);
2235
2236     if (ctx->argc < 3 || !ofputil_group_from_string(ctx->argv[2], &group_id)) {
2237         group_id = OFPG11_ALL;
2238     }
2239
2240     request = ofputil_encode_group_desc_request(vconn_get_version(vconn),
2241                                                 group_id);
2242     if (request) {
2243         dump_stats_transaction(vconn, request);
2244     }
2245
2246     vconn_close(vconn);
2247 }
2248
2249 static void
2250 ofctl_dump_group_features(struct ovs_cmdl_context *ctx)
2251 {
2252     struct ofpbuf *request;
2253     struct vconn *vconn;
2254
2255     open_vconn(ctx->argv[1], &vconn);
2256     request = ofputil_encode_group_features_request(vconn_get_version(vconn));
2257     if (request) {
2258         dump_stats_transaction(vconn, request);
2259     }
2260
2261     vconn_close(vconn);
2262 }
2263
2264 static void
2265 ofctl_help(struct ovs_cmdl_context *ctx OVS_UNUSED)
2266 {
2267     usage();
2268 }
2269
2270 static void
2271 ofctl_list_commands(struct ovs_cmdl_context *ctx OVS_UNUSED)
2272 {
2273     ovs_cmdl_print_commands(get_all_commands());
2274 }
2275 \f
2276 /* replace-flows and diff-flows commands. */
2277
2278 /* A flow table entry, possibly with two different versions. */
2279 struct fte {
2280     struct cls_rule rule;       /* Within a "struct classifier". */
2281     struct fte_version *versions[2];
2282 };
2283
2284 /* One version of a Flow Table Entry. */
2285 struct fte_version {
2286     ovs_be64 cookie;
2287     uint16_t idle_timeout;
2288     uint16_t hard_timeout;
2289     uint16_t importance;
2290     uint16_t flags;
2291     struct ofpact *ofpacts;
2292     size_t ofpacts_len;
2293 };
2294
2295 /* Frees 'version' and the data that it owns. */
2296 static void
2297 fte_version_free(struct fte_version *version)
2298 {
2299     if (version) {
2300         free(CONST_CAST(struct ofpact *, version->ofpacts));
2301         free(version);
2302     }
2303 }
2304
2305 /* Returns true if 'a' and 'b' are the same, false if they differ.
2306  *
2307  * Ignores differences in 'flags' because there's no way to retrieve flags from
2308  * an OpenFlow switch.  We have to assume that they are the same. */
2309 static bool
2310 fte_version_equals(const struct fte_version *a, const struct fte_version *b)
2311 {
2312     return (a->cookie == b->cookie
2313             && a->idle_timeout == b->idle_timeout
2314             && a->hard_timeout == b->hard_timeout
2315             && a->importance == b->importance
2316             && ofpacts_equal(a->ofpacts, a->ofpacts_len,
2317                              b->ofpacts, b->ofpacts_len));
2318 }
2319
2320 /* Clears 's', then if 's' has a version 'index', formats 'fte' and version
2321  * 'index' into 's', followed by a new-line. */
2322 static void
2323 fte_version_format(const struct fte *fte, int index, struct ds *s)
2324 {
2325     const struct fte_version *version = fte->versions[index];
2326
2327     ds_clear(s);
2328     if (!version) {
2329         return;
2330     }
2331
2332     cls_rule_format(&fte->rule, s);
2333     if (version->cookie != htonll(0)) {
2334         ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
2335     }
2336     if (version->idle_timeout != OFP_FLOW_PERMANENT) {
2337         ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
2338     }
2339     if (version->hard_timeout != OFP_FLOW_PERMANENT) {
2340         ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
2341     }
2342     if (version->importance != 0) {
2343         ds_put_format(s, " importance=%"PRIu16, version->importance);
2344     }
2345
2346     ds_put_cstr(s, " actions=");
2347     ofpacts_format(version->ofpacts, version->ofpacts_len, s);
2348
2349     ds_put_char(s, '\n');
2350 }
2351
2352 static struct fte *
2353 fte_from_cls_rule(const struct cls_rule *cls_rule)
2354 {
2355     return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
2356 }
2357
2358 /* Frees 'fte' and its versions. */
2359 static void
2360 fte_free(struct fte *fte)
2361 {
2362     if (fte) {
2363         fte_version_free(fte->versions[0]);
2364         fte_version_free(fte->versions[1]);
2365         cls_rule_destroy(&fte->rule);
2366         free(fte);
2367     }
2368 }
2369
2370 /* Frees all of the FTEs within 'cls'. */
2371 static void
2372 fte_free_all(struct classifier *cls)
2373 {
2374     struct fte *fte;
2375
2376     classifier_defer(cls);
2377     CLS_FOR_EACH (fte, rule, cls) {
2378         classifier_remove(cls, &fte->rule);
2379         ovsrcu_postpone(fte_free, fte);
2380     }
2381     classifier_destroy(cls);
2382 }
2383
2384 /* Searches 'cls' for an FTE matching 'rule', inserting a new one if
2385  * necessary.  Sets 'version' as the version of that rule with the given
2386  * 'index', replacing any existing version, if any.
2387  *
2388  * Takes ownership of 'version'. */
2389 static void
2390 fte_insert(struct classifier *cls, const struct match *match,
2391            int priority, struct fte_version *version, int index)
2392 {
2393     struct fte *old, *fte;
2394
2395     fte = xzalloc(sizeof *fte);
2396     cls_rule_init(&fte->rule, match, priority);
2397     fte->versions[index] = version;
2398
2399     old = fte_from_cls_rule(classifier_replace(cls, &fte->rule, NULL, 0));
2400     if (old) {
2401         fte->versions[!index] = old->versions[!index];
2402         old->versions[!index] = NULL;
2403
2404         ovsrcu_postpone(fte_free, old);
2405     }
2406 }
2407
2408 /* Reads the flows in 'filename' as flow table entries in 'cls' for the version
2409  * with the specified 'index'.  Returns the flow formats able to represent the
2410  * flows that were read. */
2411 static enum ofputil_protocol
2412 read_flows_from_file(const char *filename, struct classifier *cls, int index)
2413 {
2414     enum ofputil_protocol usable_protocols;
2415     int line_number;
2416     struct ds s;
2417     FILE *file;
2418
2419     file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
2420     if (file == NULL) {
2421         ovs_fatal(errno, "%s: open", filename);
2422     }
2423
2424     ds_init(&s);
2425     usable_protocols = OFPUTIL_P_ANY;
2426     line_number = 0;
2427     classifier_defer(cls);
2428     while (!ds_get_preprocessed_line(&s, file, &line_number)) {
2429         struct fte_version *version;
2430         struct ofputil_flow_mod fm;
2431         char *error;
2432         enum ofputil_protocol usable;
2433
2434         error = parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), &usable);
2435         if (error) {
2436             ovs_fatal(0, "%s:%d: %s", filename, line_number, error);
2437         }
2438         usable_protocols &= usable;
2439
2440         version = xmalloc(sizeof *version);
2441         version->cookie = fm.new_cookie;
2442         version->idle_timeout = fm.idle_timeout;
2443         version->hard_timeout = fm.hard_timeout;
2444         version->importance = fm.importance;
2445         version->flags = fm.flags & (OFPUTIL_FF_SEND_FLOW_REM
2446                                      | OFPUTIL_FF_EMERG);
2447         version->ofpacts = fm.ofpacts;
2448         version->ofpacts_len = fm.ofpacts_len;
2449
2450         fte_insert(cls, &fm.match, fm.priority, version, index);
2451     }
2452     classifier_publish(cls);
2453     ds_destroy(&s);
2454
2455     if (file != stdin) {
2456         fclose(file);
2457     }
2458
2459     return usable_protocols;
2460 }
2461
2462 static bool
2463 recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
2464                       struct ofpbuf **replyp,
2465                       struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
2466 {
2467     struct ofpbuf *reply = *replyp;
2468
2469     for (;;) {
2470         int retval;
2471         bool more;
2472
2473         /* Get a flow stats reply message, if we don't already have one. */
2474         if (!reply) {
2475             enum ofptype type;
2476             enum ofperr error;
2477
2478             do {
2479                 run(vconn_recv_block(vconn, &reply),
2480                     "OpenFlow packet receive failed");
2481             } while (((struct ofp_header *) reply->data)->xid != send_xid);
2482
2483             error = ofptype_decode(&type, reply->data);
2484             if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
2485                 ovs_fatal(0, "received bad reply: %s",
2486                           ofp_to_string(reply->data, reply->size,
2487                                         verbosity + 1));
2488             }
2489         }
2490
2491         /* Pull an individual flow stats reply out of the message. */
2492         retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
2493         switch (retval) {
2494         case 0:
2495             *replyp = reply;
2496             return true;
2497
2498         case EOF:
2499             more = ofpmp_more(reply->header);
2500             ofpbuf_delete(reply);
2501             reply = NULL;
2502             if (!more) {
2503                 *replyp = NULL;
2504                 return false;
2505             }
2506             break;
2507
2508         default:
2509             ovs_fatal(0, "parse error in reply (%s)",
2510                       ofperr_to_string(retval));
2511         }
2512     }
2513 }
2514
2515 /* Reads the OpenFlow flow table from 'vconn', which has currently active flow
2516  * format 'protocol', and adds them as flow table entries in 'cls' for the
2517  * version with the specified 'index'. */
2518 static void
2519 read_flows_from_switch(struct vconn *vconn,
2520                        enum ofputil_protocol protocol,
2521                        struct classifier *cls, int index)
2522 {
2523     struct ofputil_flow_stats_request fsr;
2524     struct ofputil_flow_stats fs;
2525     struct ofpbuf *request;
2526     struct ofpbuf ofpacts;
2527     struct ofpbuf *reply;
2528     ovs_be32 send_xid;
2529
2530     fsr.aggregate = false;
2531     match_init_catchall(&fsr.match);
2532     fsr.out_port = OFPP_ANY;
2533     fsr.table_id = 0xff;
2534     fsr.cookie = fsr.cookie_mask = htonll(0);
2535     request = ofputil_encode_flow_stats_request(&fsr, protocol);
2536     send_xid = ((struct ofp_header *) request->data)->xid;
2537     send_openflow_buffer(vconn, request);
2538
2539     reply = NULL;
2540     ofpbuf_init(&ofpacts, 0);
2541     classifier_defer(cls);
2542     while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
2543         struct fte_version *version;
2544
2545         version = xmalloc(sizeof *version);
2546         version->cookie = fs.cookie;
2547         version->idle_timeout = fs.idle_timeout;
2548         version->hard_timeout = fs.hard_timeout;
2549         version->importance = fs.importance;
2550         version->flags = 0;
2551         version->ofpacts_len = fs.ofpacts_len;
2552         version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
2553
2554         fte_insert(cls, &fs.match, fs.priority, version, index);
2555     }
2556     classifier_publish(cls);
2557     ofpbuf_uninit(&ofpacts);
2558 }
2559
2560 static void
2561 fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
2562                   enum ofputil_protocol protocol, struct ovs_list *packets)
2563 {
2564     const struct fte_version *version = fte->versions[index];
2565     struct ofputil_flow_mod fm;
2566     struct ofpbuf *ofm;
2567
2568     minimatch_expand(&fte->rule.match, &fm.match);
2569     fm.priority = fte->rule.priority;
2570     fm.cookie = htonll(0);
2571     fm.cookie_mask = htonll(0);
2572     fm.new_cookie = version->cookie;
2573     fm.modify_cookie = true;
2574     fm.table_id = 0xff;
2575     fm.command = command;
2576     fm.idle_timeout = version->idle_timeout;
2577     fm.hard_timeout = version->hard_timeout;
2578     fm.importance = version->importance;
2579     fm.buffer_id = UINT32_MAX;
2580     fm.out_port = OFPP_ANY;
2581     fm.flags = version->flags;
2582     if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
2583         command == OFPFC_MODIFY_STRICT) {
2584         fm.ofpacts = version->ofpacts;
2585         fm.ofpacts_len = version->ofpacts_len;
2586     } else {
2587         fm.ofpacts = NULL;
2588         fm.ofpacts_len = 0;
2589     }
2590     fm.delete_reason = OFPRR_DELETE;
2591
2592     ofm = ofputil_encode_flow_mod(&fm, protocol);
2593     list_push_back(packets, &ofm->list_node);
2594 }
2595
2596 static void
2597 ofctl_replace_flows(struct ovs_cmdl_context *ctx)
2598 {
2599     enum { FILE_IDX = 0, SWITCH_IDX = 1 };
2600     enum ofputil_protocol usable_protocols, protocol;
2601     struct classifier cls;
2602     struct ovs_list requests;
2603     struct vconn *vconn;
2604     struct fte *fte;
2605
2606     classifier_init(&cls, NULL);
2607     usable_protocols = read_flows_from_file(ctx->argv[2], &cls, FILE_IDX);
2608
2609     protocol = open_vconn(ctx->argv[1], &vconn);
2610     protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
2611
2612     read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
2613
2614     list_init(&requests);
2615
2616     /* Delete flows that exist on the switch but not in the file. */
2617     CLS_FOR_EACH (fte, rule, &cls) {
2618         struct fte_version *file_ver = fte->versions[FILE_IDX];
2619         struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2620
2621         if (sw_ver && !file_ver) {
2622             fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
2623                               protocol, &requests);
2624         }
2625     }
2626
2627     /* Add flows that exist in the file but not on the switch.
2628      * Update flows that exist in both places but differ. */
2629     CLS_FOR_EACH (fte, rule, &cls) {
2630         struct fte_version *file_ver = fte->versions[FILE_IDX];
2631         struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
2632
2633         if (file_ver
2634             && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
2635             fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
2636         }
2637     }
2638     transact_multiple_noreply(vconn, &requests);
2639     vconn_close(vconn);
2640
2641     fte_free_all(&cls);
2642 }
2643
2644 static void
2645 read_flows_from_source(const char *source, struct classifier *cls, int index)
2646 {
2647     struct stat s;
2648
2649     if (source[0] == '/' || source[0] == '.'
2650         || (!strchr(source, ':') && !stat(source, &s))) {
2651         read_flows_from_file(source, cls, index);
2652     } else {
2653         enum ofputil_protocol protocol;
2654         struct vconn *vconn;
2655
2656         protocol = open_vconn(source, &vconn);
2657         protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
2658         read_flows_from_switch(vconn, protocol, cls, index);
2659         vconn_close(vconn);
2660     }
2661 }
2662
2663 static void
2664 ofctl_diff_flows(struct ovs_cmdl_context *ctx)
2665 {
2666     bool differences = false;
2667     struct classifier cls;
2668     struct ds a_s, b_s;
2669     struct fte *fte;
2670
2671     classifier_init(&cls, NULL);
2672     read_flows_from_source(ctx->argv[1], &cls, 0);
2673     read_flows_from_source(ctx->argv[2], &cls, 1);
2674
2675     ds_init(&a_s);
2676     ds_init(&b_s);
2677
2678     CLS_FOR_EACH (fte, rule, &cls) {
2679         struct fte_version *a = fte->versions[0];
2680         struct fte_version *b = fte->versions[1];
2681
2682         if (!a || !b || !fte_version_equals(a, b)) {
2683             fte_version_format(fte, 0, &a_s);
2684             fte_version_format(fte, 1, &b_s);
2685             if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
2686                 if (a_s.length) {
2687                     printf("-%s", ds_cstr(&a_s));
2688                 }
2689                 if (b_s.length) {
2690                     printf("+%s", ds_cstr(&b_s));
2691                 }
2692                 differences = true;
2693             }
2694         }
2695     }
2696
2697     ds_destroy(&a_s);
2698     ds_destroy(&b_s);
2699
2700     fte_free_all(&cls);
2701
2702     if (differences) {
2703         exit(2);
2704     }
2705 }
2706
2707 static void
2708 ofctl_meter_mod__(const char *bridge, const char *str, int command)
2709 {
2710     struct ofputil_meter_mod mm;
2711     struct vconn *vconn;
2712     enum ofputil_protocol protocol;
2713     enum ofputil_protocol usable_protocols;
2714     enum ofp_version version;
2715
2716     if (str) {
2717         char *error;
2718         error = parse_ofp_meter_mod_str(&mm, str, command, &usable_protocols);
2719         if (error) {
2720             ovs_fatal(0, "%s", error);
2721         }
2722     } else {
2723         usable_protocols = OFPUTIL_P_OF13_UP;
2724         mm.command = command;
2725         mm.meter.meter_id = OFPM13_ALL;
2726     }
2727
2728     protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2729     version = ofputil_protocol_to_ofp_version(protocol);
2730     transact_noreply(vconn, ofputil_encode_meter_mod(version, &mm));
2731     vconn_close(vconn);
2732 }
2733
2734 static void
2735 ofctl_meter_request__(const char *bridge, const char *str,
2736                       enum ofputil_meter_request_type type)
2737 {
2738     struct ofputil_meter_mod mm;
2739     struct vconn *vconn;
2740     enum ofputil_protocol usable_protocols;
2741     enum ofputil_protocol protocol;
2742     enum ofp_version version;
2743
2744     if (str) {
2745         char *error;
2746         error = parse_ofp_meter_mod_str(&mm, str, -1, &usable_protocols);
2747         if (error) {
2748             ovs_fatal(0, "%s", error);
2749         }
2750     } else {
2751         usable_protocols = OFPUTIL_P_OF13_UP;
2752         mm.meter.meter_id = OFPM13_ALL;
2753     }
2754
2755     protocol = open_vconn_for_flow_mod(bridge, &vconn, usable_protocols);
2756     version = ofputil_protocol_to_ofp_version(protocol);
2757     transact_noreply(vconn, ofputil_encode_meter_request(version,
2758                                                          type,
2759                                                          mm.meter.meter_id));
2760     vconn_close(vconn);
2761 }
2762
2763
2764 static void
2765 ofctl_add_meter(struct ovs_cmdl_context *ctx)
2766 {
2767     ofctl_meter_mod__(ctx->argv[1], ctx->argv[2], OFPMC13_ADD);
2768 }
2769
2770 static void
2771 ofctl_mod_meter(struct ovs_cmdl_context *ctx)
2772 {
2773     ofctl_meter_mod__(ctx->argv[1], ctx->argv[2], OFPMC13_MODIFY);
2774 }
2775
2776 static void
2777 ofctl_del_meters(struct ovs_cmdl_context *ctx)
2778 {
2779     ofctl_meter_mod__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL, OFPMC13_DELETE);
2780 }
2781
2782 static void
2783 ofctl_dump_meters(struct ovs_cmdl_context *ctx)
2784 {
2785     ofctl_meter_request__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL,
2786                           OFPUTIL_METER_CONFIG);
2787 }
2788
2789 static void
2790 ofctl_meter_stats(struct ovs_cmdl_context *ctx)
2791 {
2792     ofctl_meter_request__(ctx->argv[1], ctx->argc > 2 ? ctx->argv[2] : NULL,
2793                           OFPUTIL_METER_STATS);
2794 }
2795
2796 static void
2797 ofctl_meter_features(struct ovs_cmdl_context *ctx)
2798 {
2799     ofctl_meter_request__(ctx->argv[1], NULL, OFPUTIL_METER_FEATURES);
2800 }
2801
2802 \f
2803 /* Undocumented commands for unit testing. */
2804
2805 static void
2806 ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms,
2807                     enum ofputil_protocol usable_protocols)
2808 {
2809     enum ofputil_protocol protocol = 0;
2810     char *usable_s;
2811     size_t i;
2812
2813     usable_s = ofputil_protocols_to_string(usable_protocols);
2814     printf("usable protocols: %s\n", usable_s);
2815     free(usable_s);
2816
2817     if (!(usable_protocols & allowed_protocols)) {
2818         ovs_fatal(0, "no usable protocol");
2819     }
2820     for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
2821         protocol = 1 << i;
2822         if (protocol & usable_protocols & allowed_protocols) {
2823             break;
2824         }
2825     }
2826     ovs_assert(is_pow2(protocol));
2827
2828     printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
2829
2830     for (i = 0; i < n_fms; i++) {
2831         struct ofputil_flow_mod *fm = &fms[i];
2832         struct ofpbuf *msg;
2833
2834         msg = ofputil_encode_flow_mod(fm, protocol);
2835         ofp_print(stdout, msg->data, msg->size, verbosity);
2836         ofpbuf_delete(msg);
2837
2838         free(CONST_CAST(struct ofpact *, fm->ofpacts));
2839     }
2840 }
2841
2842 /* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
2843  * it back to stdout.  */
2844 static void
2845 ofctl_parse_flow(struct ovs_cmdl_context *ctx)
2846 {
2847     enum ofputil_protocol usable_protocols;
2848     struct ofputil_flow_mod fm;
2849     char *error;
2850
2851     error = parse_ofp_flow_mod_str(&fm, ctx->argv[1], OFPFC_ADD, &usable_protocols);
2852     if (error) {
2853         ovs_fatal(0, "%s", error);
2854     }
2855     ofctl_parse_flows__(&fm, 1, usable_protocols);
2856 }
2857
2858 /* "parse-flows FILENAME": reads the named file as a sequence of flows (like
2859  * add-flows) and prints each of the flows back to stdout.  */
2860 static void
2861 ofctl_parse_flows(struct ovs_cmdl_context *ctx)
2862 {
2863     enum ofputil_protocol usable_protocols;
2864     struct ofputil_flow_mod *fms = NULL;
2865     size_t n_fms = 0;
2866     char *error;
2867
2868     error = parse_ofp_flow_mod_file(ctx->argv[1], OFPFC_ADD, &fms, &n_fms,
2869                                     &usable_protocols);
2870     if (error) {
2871         ovs_fatal(0, "%s", error);
2872     }
2873     ofctl_parse_flows__(fms, n_fms, usable_protocols);
2874     free(fms);
2875 }
2876
2877 static void
2878 ofctl_parse_nxm__(bool oxm, enum ofp_version version)
2879 {
2880     struct ds in;
2881
2882     ds_init(&in);
2883     while (!ds_get_test_line(&in, stdin)) {
2884         struct ofpbuf nx_match;
2885         struct match match;
2886         ovs_be64 cookie, cookie_mask;
2887         enum ofperr error;
2888         int match_len;
2889
2890         /* Convert string to nx_match. */
2891         ofpbuf_init(&nx_match, 0);
2892         if (oxm) {
2893             match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
2894         } else {
2895             match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
2896         }
2897
2898         /* Convert nx_match to match. */
2899         if (strict) {
2900             if (oxm) {
2901                 error = oxm_pull_match(&nx_match, &match);
2902             } else {
2903                 error = nx_pull_match(&nx_match, match_len, &match,
2904                                       &cookie, &cookie_mask);
2905             }
2906         } else {
2907             if (oxm) {
2908                 error = oxm_pull_match_loose(&nx_match, &match);
2909             } else {
2910                 error = nx_pull_match_loose(&nx_match, match_len, &match,
2911                                             &cookie, &cookie_mask);
2912             }
2913         }
2914
2915
2916         if (!error) {
2917             char *out;
2918
2919             /* Convert match back to nx_match. */
2920             ofpbuf_uninit(&nx_match);
2921             ofpbuf_init(&nx_match, 0);
2922             if (oxm) {
2923                 match_len = oxm_put_match(&nx_match, &match, version);
2924                 out = oxm_match_to_string(&nx_match, match_len);
2925             } else {
2926                 match_len = nx_put_match(&nx_match, &match,
2927                                          cookie, cookie_mask);
2928                 out = nx_match_to_string(nx_match.data, match_len);
2929             }
2930
2931             puts(out);
2932             free(out);
2933
2934             if (verbosity > 0) {
2935                 ovs_hex_dump(stdout, nx_match.data, nx_match.size, 0, false);
2936             }
2937         } else {
2938             printf("nx_pull_match() returned error %s\n",
2939                    ofperr_get_name(error));
2940         }
2941
2942         ofpbuf_uninit(&nx_match);
2943     }
2944     ds_destroy(&in);
2945 }
2946
2947 /* "parse-nxm": reads a series of NXM nx_match specifications as strings from
2948  * stdin, does some internal fussing with them, and then prints them back as
2949  * strings on stdout. */
2950 static void
2951 ofctl_parse_nxm(struct ovs_cmdl_context *ctx OVS_UNUSED)
2952 {
2953     ofctl_parse_nxm__(false, 0);
2954 }
2955
2956 /* "parse-oxm VERSION": reads a series of OXM nx_match specifications as
2957  * strings from stdin, does some internal fussing with them, and then prints
2958  * them back as strings on stdout.  VERSION must specify an OpenFlow version,
2959  * e.g. "OpenFlow12". */
2960 static void
2961 ofctl_parse_oxm(struct ovs_cmdl_context *ctx)
2962 {
2963     enum ofp_version version = ofputil_version_from_string(ctx->argv[1]);
2964     if (version < OFP12_VERSION) {
2965         ovs_fatal(0, "%s: not a valid version for OXM", ctx->argv[1]);
2966     }
2967
2968     ofctl_parse_nxm__(true, version);
2969 }
2970
2971 static void
2972 print_differences(const char *prefix,
2973                   const void *a_, size_t a_len,
2974                   const void *b_, size_t b_len)
2975 {
2976     const uint8_t *a = a_;
2977     const uint8_t *b = b_;
2978     size_t i;
2979
2980     for (i = 0; i < MIN(a_len, b_len); i++) {
2981         if (a[i] != b[i]) {
2982             printf("%s%2"PRIuSIZE": %02"PRIx8" -> %02"PRIx8"\n",
2983                    prefix, i, a[i], b[i]);
2984         }
2985     }
2986     for (i = a_len; i < b_len; i++) {
2987         printf("%s%2"PRIuSIZE": (none) -> %02"PRIx8"\n", prefix, i, b[i]);
2988     }
2989     for (i = b_len; i < a_len; i++) {
2990         printf("%s%2"PRIuSIZE": %02"PRIx8" -> (none)\n", prefix, i, a[i]);
2991     }
2992 }
2993
2994 static void
2995 ofctl_parse_actions__(const char *version_s, bool instructions)
2996 {
2997     enum ofp_version version;
2998     struct ds in;
2999
3000     version = ofputil_version_from_string(version_s);
3001     if (!version) {
3002         ovs_fatal(0, "%s: not a valid OpenFlow version", version_s);
3003     }
3004
3005     ds_init(&in);
3006     while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3007         struct ofpbuf of_out;
3008         struct ofpbuf of_in;
3009         struct ofpbuf ofpacts;
3010         const char *table_id;
3011         char *actions;
3012         enum ofperr error;
3013         size_t size;
3014         struct ds s;
3015
3016         /* Parse table_id separated with the follow-up actions by ",", if
3017          * any. */
3018         actions = ds_cstr(&in);
3019         table_id = NULL;
3020         if (strstr(actions, ",")) {
3021             table_id = strsep(&actions, ",");
3022         }
3023
3024         /* Parse hex bytes. */
3025         ofpbuf_init(&of_in, 0);
3026         if (ofpbuf_put_hex(&of_in, actions, NULL)[0] != '\0') {
3027             ovs_fatal(0, "Trailing garbage in hex data");
3028         }
3029
3030         /* Convert to ofpacts. */
3031         ofpbuf_init(&ofpacts, 0);
3032         size = of_in.size;
3033         error = (instructions
3034                  ? ofpacts_pull_openflow_instructions
3035                  : ofpacts_pull_openflow_actions)(
3036                      &of_in, of_in.size, version, &ofpacts);
3037         if (!error && instructions) {
3038             /* Verify actions, enforce consistency. */
3039             enum ofputil_protocol protocol;
3040             struct flow flow;
3041
3042             memset(&flow, 0, sizeof flow);
3043             protocol = ofputil_protocols_from_ofp_version(version);
3044             error = ofpacts_check_consistency(ofpacts.data, ofpacts.size,
3045                                               &flow, OFPP_MAX,
3046                                               table_id ? atoi(table_id) : 0,
3047                                               255, protocol);
3048         }
3049         if (error) {
3050             printf("bad %s %s: %s\n\n",
3051                    version_s, instructions ? "instructions" : "actions",
3052                    ofperr_get_name(error));
3053             ofpbuf_uninit(&ofpacts);
3054             ofpbuf_uninit(&of_in);
3055             continue;
3056         }
3057         ofpbuf_push_uninit(&of_in, size);
3058
3059         /* Print cls_rule. */
3060         ds_init(&s);
3061         ds_put_cstr(&s, "actions=");
3062         ofpacts_format(ofpacts.data, ofpacts.size, &s);
3063         puts(ds_cstr(&s));
3064         ds_destroy(&s);
3065
3066         /* Convert back to ofp10 actions and print differences from input. */
3067         ofpbuf_init(&of_out, 0);
3068         if (instructions) {
3069            ofpacts_put_openflow_instructions(ofpacts.data, ofpacts.size,
3070                                              &of_out, version);
3071         } else {
3072            ofpacts_put_openflow_actions(ofpacts.data, ofpacts.size,
3073                                          &of_out, version);
3074         }
3075
3076         print_differences("", of_in.data, of_in.size,
3077                           of_out.data, of_out.size);
3078         putchar('\n');
3079
3080         ofpbuf_uninit(&ofpacts);
3081         ofpbuf_uninit(&of_in);
3082         ofpbuf_uninit(&of_out);
3083     }
3084     ds_destroy(&in);
3085 }
3086
3087 /* "parse-actions VERSION": reads a series of action specifications for the
3088  * given OpenFlow VERSION as hex bytes from stdin, converts them to ofpacts,
3089  * prints them as strings on stdout, and then converts them back to hex bytes
3090  * and prints any differences from the input. */
3091 static void
3092 ofctl_parse_actions(struct ovs_cmdl_context *ctx)
3093 {
3094     ofctl_parse_actions__(ctx->argv[1], false);
3095 }
3096
3097 /* "parse-actions VERSION": reads a series of instruction specifications for
3098  * the given OpenFlow VERSION as hex bytes from stdin, converts them to
3099  * ofpacts, prints them as strings on stdout, and then converts them back to
3100  * hex bytes and prints any differences from the input. */
3101 static void
3102 ofctl_parse_instructions(struct ovs_cmdl_context *ctx)
3103 {
3104     ofctl_parse_actions__(ctx->argv[1], true);
3105 }
3106
3107 /* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
3108  * bytes from stdin, converts them to cls_rules, prints them as strings on
3109  * stdout, and then converts them back to hex bytes and prints any differences
3110  * from the input.
3111  *
3112  * The input hex bytes may contain "x"s to represent "don't-cares", bytes whose
3113  * values are ignored in the input and will be set to zero when OVS converts
3114  * them back to hex bytes.  ovs-ofctl actually sets "x"s to random bits when
3115  * it does the conversion to hex, to ensure that in fact they are ignored. */
3116 static void
3117 ofctl_parse_ofp10_match(struct ovs_cmdl_context *ctx OVS_UNUSED)
3118 {
3119     struct ds expout;
3120     struct ds in;
3121
3122     ds_init(&in);
3123     ds_init(&expout);
3124     while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3125         struct ofpbuf match_in, match_expout;
3126         struct ofp10_match match_out;
3127         struct ofp10_match match_normal;
3128         struct match match;
3129         char *p;
3130
3131         /* Parse hex bytes to use for expected output. */
3132         ds_clear(&expout);
3133         ds_put_cstr(&expout, ds_cstr(&in));
3134         for (p = ds_cstr(&expout); *p; p++) {
3135             if (*p == 'x') {
3136                 *p = '0';
3137             }
3138         }
3139         ofpbuf_init(&match_expout, 0);
3140         if (ofpbuf_put_hex(&match_expout, ds_cstr(&expout), NULL)[0] != '\0') {
3141             ovs_fatal(0, "Trailing garbage in hex data");
3142         }
3143         if (match_expout.size != sizeof(struct ofp10_match)) {
3144             ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3145                       match_expout.size, sizeof(struct ofp10_match));
3146         }
3147
3148         /* Parse hex bytes for input. */
3149         for (p = ds_cstr(&in); *p; p++) {
3150             if (*p == 'x') {
3151                 *p = "0123456789abcdef"[random_uint32() & 0xf];
3152             }
3153         }
3154         ofpbuf_init(&match_in, 0);
3155         if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3156             ovs_fatal(0, "Trailing garbage in hex data");
3157         }
3158         if (match_in.size != sizeof(struct ofp10_match)) {
3159             ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3160                       match_in.size, sizeof(struct ofp10_match));
3161         }
3162
3163         /* Convert to cls_rule and print. */
3164         ofputil_match_from_ofp10_match(match_in.data, &match);
3165         match_print(&match);
3166
3167         /* Convert back to ofp10_match and print differences from input. */
3168         ofputil_match_to_ofp10_match(&match, &match_out);
3169         print_differences("", match_expout.data, match_expout.size,
3170                           &match_out, sizeof match_out);
3171
3172         /* Normalize, then convert and compare again. */
3173         ofputil_normalize_match(&match);
3174         ofputil_match_to_ofp10_match(&match, &match_normal);
3175         print_differences("normal: ", &match_out, sizeof match_out,
3176                           &match_normal, sizeof match_normal);
3177         putchar('\n');
3178
3179         ofpbuf_uninit(&match_in);
3180         ofpbuf_uninit(&match_expout);
3181     }
3182     ds_destroy(&in);
3183     ds_destroy(&expout);
3184 }
3185
3186 /* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
3187  * bytes from stdin, converts them to "struct match"es, prints them as strings
3188  * on stdout, and then converts them back to hex bytes and prints any
3189  * differences from the input. */
3190 static void
3191 ofctl_parse_ofp11_match(struct ovs_cmdl_context *ctx OVS_UNUSED)
3192 {
3193     struct ds in;
3194
3195     ds_init(&in);
3196     while (!ds_get_preprocessed_line(&in, stdin, NULL)) {
3197         struct ofpbuf match_in;
3198         struct ofp11_match match_out;
3199         struct match match;
3200         enum ofperr error;
3201
3202         /* Parse hex bytes. */
3203         ofpbuf_init(&match_in, 0);
3204         if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
3205             ovs_fatal(0, "Trailing garbage in hex data");
3206         }
3207         if (match_in.size != sizeof(struct ofp11_match)) {
3208             ovs_fatal(0, "Input is %"PRIu32" bytes, expected %"PRIuSIZE,
3209                       match_in.size, sizeof(struct ofp11_match));
3210         }
3211
3212         /* Convert to match. */
3213         error = ofputil_match_from_ofp11_match(match_in.data, &match);
3214         if (error) {
3215             printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
3216             ofpbuf_uninit(&match_in);
3217             continue;
3218         }
3219
3220         /* Print match. */
3221         match_print(&match);
3222
3223         /* Convert back to ofp11_match and print differences from input. */
3224         ofputil_match_to_ofp11_match(&match, &match_out);
3225
3226         print_differences("", match_in.data, match_in.size,
3227                           &match_out, sizeof match_out);
3228         putchar('\n');
3229
3230         ofpbuf_uninit(&match_in);
3231     }
3232     ds_destroy(&in);
3233 }
3234
3235 /* "parse-pcap PCAP": read packets from PCAP and print their flows. */
3236 static void
3237 ofctl_parse_pcap(struct ovs_cmdl_context *ctx)
3238 {
3239     FILE *pcap;
3240
3241     pcap = ovs_pcap_open(ctx->argv[1], "rb");
3242     if (!pcap) {
3243         ovs_fatal(errno, "%s: open failed", ctx->argv[1]);
3244     }
3245
3246     for (;;) {
3247         struct dp_packet *packet;
3248         struct flow flow;
3249         int error;
3250
3251         error = ovs_pcap_read(pcap, &packet, NULL);
3252         if (error == EOF) {
3253             break;
3254         } else if (error) {
3255             ovs_fatal(error, "%s: read failed", ctx->argv[1]);
3256         }
3257
3258         packet->md = PKT_METADATA_INITIALIZER(ODPP_NONE);
3259         flow_extract(packet, &flow);
3260         flow_print(stdout, &flow);
3261         putchar('\n');
3262         dp_packet_delete(packet);
3263     }
3264 }
3265
3266 /* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
3267  * mask values to and from various formats and prints the results. */
3268 static void
3269 ofctl_check_vlan(struct ovs_cmdl_context *ctx)
3270 {
3271     struct match match;
3272
3273     char *string_s;
3274     struct ofputil_flow_mod fm;
3275
3276     struct ofpbuf nxm;
3277     struct match nxm_match;
3278     int nxm_match_len;
3279     char *nxm_s;
3280
3281     struct ofp10_match of10_raw;
3282     struct match of10_match;
3283
3284     struct ofp11_match of11_raw;
3285     struct match of11_match;
3286
3287     enum ofperr error;
3288     char *error_s;
3289
3290     enum ofputil_protocol usable_protocols; /* Unused for now. */
3291
3292     match_init_catchall(&match);
3293     match.flow.vlan_tci = htons(strtoul(ctx->argv[1], NULL, 16));
3294     match.wc.masks.vlan_tci = htons(strtoul(ctx->argv[2], NULL, 16));
3295
3296     /* Convert to and from string. */
3297     string_s = match_to_string(&match, OFP_DEFAULT_PRIORITY);
3298     printf("%s -> ", string_s);
3299     fflush(stdout);
3300     error_s = parse_ofp_str(&fm, -1, string_s, &usable_protocols);
3301     if (error_s) {
3302         ovs_fatal(0, "%s", error_s);
3303     }
3304     printf("%04"PRIx16"/%04"PRIx16"\n",
3305            ntohs(fm.match.flow.vlan_tci),
3306            ntohs(fm.match.wc.masks.vlan_tci));
3307     free(string_s);
3308
3309     /* Convert to and from NXM. */
3310     ofpbuf_init(&nxm, 0);
3311     nxm_match_len = nx_put_match(&nxm, &match, htonll(0), htonll(0));
3312     nxm_s = nx_match_to_string(nxm.data, nxm_match_len);
3313     error = nx_pull_match(&nxm, nxm_match_len, &nxm_match, NULL, NULL);
3314     printf("NXM: %s -> ", nxm_s);
3315     if (error) {
3316         printf("%s\n", ofperr_to_string(error));
3317     } else {
3318         printf("%04"PRIx16"/%04"PRIx16"\n",
3319                ntohs(nxm_match.flow.vlan_tci),
3320                ntohs(nxm_match.wc.masks.vlan_tci));
3321     }
3322     free(nxm_s);
3323     ofpbuf_uninit(&nxm);
3324
3325     /* Convert to and from OXM. */
3326     ofpbuf_init(&nxm, 0);
3327     nxm_match_len = oxm_put_match(&nxm, &match, OFP12_VERSION);
3328     nxm_s = oxm_match_to_string(&nxm, nxm_match_len);
3329     error = oxm_pull_match(&nxm, &nxm_match);
3330     printf("OXM: %s -> ", nxm_s);
3331     if (error) {
3332         printf("%s\n", ofperr_to_string(error));
3333     } else {
3334         uint16_t vid = ntohs(nxm_match.flow.vlan_tci) &
3335             (VLAN_VID_MASK | VLAN_CFI);
3336         uint16_t mask = ntohs(nxm_match.wc.masks.vlan_tci) &
3337             (VLAN_VID_MASK | VLAN_CFI);
3338
3339         printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
3340         if (vid && vlan_tci_to_pcp(nxm_match.wc.masks.vlan_tci)) {
3341             printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_match.flow.vlan_tci));
3342         } else {
3343             printf("--\n");
3344         }
3345     }
3346     free(nxm_s);
3347     ofpbuf_uninit(&nxm);
3348
3349     /* Convert to and from OpenFlow 1.0. */
3350     ofputil_match_to_ofp10_match(&match, &of10_raw);
3351     ofputil_match_from_ofp10_match(&of10_raw, &of10_match);
3352     printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3353            ntohs(of10_raw.dl_vlan),
3354            (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
3355            of10_raw.dl_vlan_pcp,
3356            (of10_raw.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
3357            ntohs(of10_match.flow.vlan_tci),
3358            ntohs(of10_match.wc.masks.vlan_tci));
3359
3360     /* Convert to and from OpenFlow 1.1. */
3361     ofputil_match_to_ofp11_match(&match, &of11_raw);
3362     ofputil_match_from_ofp11_match(&of11_raw, &of11_match);
3363     printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
3364            ntohs(of11_raw.dl_vlan),
3365            (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
3366            of11_raw.dl_vlan_pcp,
3367            (of11_raw.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
3368            ntohs(of11_match.flow.vlan_tci),
3369            ntohs(of11_match.wc.masks.vlan_tci));
3370 }
3371
3372 /* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
3373  * version. */
3374 static void
3375 ofctl_print_error(struct ovs_cmdl_context *ctx)
3376 {
3377     enum ofperr error;
3378     int version;
3379
3380     error = ofperr_from_name(ctx->argv[1]);
3381     if (!error) {
3382         ovs_fatal(0, "unknown error \"%s\"", ctx->argv[1]);
3383     }
3384
3385     for (version = 0; version <= UINT8_MAX; version++) {
3386         const char *name = ofperr_domain_get_name(version);
3387         if (name) {
3388             int vendor = ofperr_get_vendor(error, version);
3389             int type = ofperr_get_type(error, version);
3390             int code = ofperr_get_code(error, version);
3391
3392             if (vendor != -1 || type != -1 || code != -1) {
3393                 printf("%s: vendor %#x, type %d, code %d\n",
3394                        name, vendor, type, code);
3395             }
3396         }
3397     }
3398 }
3399
3400 /* "encode-error-reply ENUM REQUEST": Encodes an error reply to REQUEST for the
3401  * error named ENUM and prints the error reply in hex. */
3402 static void
3403 ofctl_encode_error_reply(struct ovs_cmdl_context *ctx)
3404 {
3405     const struct ofp_header *oh;
3406     struct ofpbuf request, *reply;
3407     enum ofperr error;
3408
3409     error = ofperr_from_name(ctx->argv[1]);
3410     if (!error) {
3411         ovs_fatal(0, "unknown error \"%s\"", ctx->argv[1]);
3412     }
3413
3414     ofpbuf_init(&request, 0);
3415     if (ofpbuf_put_hex(&request, ctx->argv[2], NULL)[0] != '\0') {
3416         ovs_fatal(0, "Trailing garbage in hex data");
3417     }
3418     if (request.size < sizeof(struct ofp_header)) {
3419         ovs_fatal(0, "Request too short");
3420     }
3421
3422     oh = request.data;
3423     if (request.size != ntohs(oh->length)) {
3424         ovs_fatal(0, "Request size inconsistent");
3425     }
3426
3427     reply = ofperr_encode_reply(error, request.data);
3428     ofpbuf_uninit(&request);
3429
3430     ovs_hex_dump(stdout, reply->data, reply->size, 0, false);
3431     ofpbuf_delete(reply);
3432 }
3433
3434 /* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
3435  * binary data, interpreting them as an OpenFlow message, and prints the
3436  * OpenFlow message on stdout, at VERBOSITY (level 2 by default).
3437  *
3438  * Alternative usage: "ofp-print [VERBOSITY] - < HEXSTRING_FILE", where
3439  * HEXSTRING_FILE contains the HEXSTRING. */
3440 static void
3441 ofctl_ofp_print(struct ovs_cmdl_context *ctx)
3442 {
3443     struct ofpbuf packet;
3444     char *buffer;
3445     int verbosity = 2;
3446     struct ds line;
3447
3448     ds_init(&line);
3449
3450     if (!strcmp(ctx->argv[ctx->argc-1], "-")) {
3451         if (ds_get_line(&line, stdin)) {
3452            VLOG_FATAL("Failed to read stdin");
3453         }
3454
3455         buffer = line.string;
3456         verbosity = ctx->argc > 2 ? atoi(ctx->argv[1]) : verbosity;
3457     } else if (ctx->argc > 2) {
3458         buffer = ctx->argv[1];
3459         verbosity = atoi(ctx->argv[2]);
3460     } else {
3461         buffer = ctx->argv[1];
3462     }
3463
3464     ofpbuf_init(&packet, strlen(buffer) / 2);
3465     if (ofpbuf_put_hex(&packet, buffer, NULL)[0] != '\0') {
3466         ovs_fatal(0, "trailing garbage following hex bytes");
3467     }
3468     ofp_print(stdout, packet.data, packet.size, verbosity);
3469     ofpbuf_uninit(&packet);
3470     ds_destroy(&line);
3471 }
3472
3473 /* "encode-hello BITMAP...": Encodes each BITMAP as an OpenFlow hello message
3474  * and dumps each message in hex.  */
3475 static void
3476 ofctl_encode_hello(struct ovs_cmdl_context *ctx)
3477 {
3478     uint32_t bitmap = strtol(ctx->argv[1], NULL, 0);
3479     struct ofpbuf *hello;
3480
3481     hello = ofputil_encode_hello(bitmap);
3482     ovs_hex_dump(stdout, hello->data, hello->size, 0, false);
3483     ofp_print(stdout, hello->data, hello->size, verbosity);
3484     ofpbuf_delete(hello);
3485 }
3486
3487 static const struct ovs_cmdl_command all_commands[] = {
3488     { "show", "switch",
3489       1, 1, ofctl_show },
3490     { "monitor", "switch [misslen] [invalid_ttl] [watch:[...]]",
3491       1, 3, ofctl_monitor },
3492     { "snoop", "switch",
3493       1, 1, ofctl_snoop },
3494     { "dump-desc", "switch",
3495       1, 1, ofctl_dump_desc },
3496     { "dump-tables", "switch",
3497       1, 1, ofctl_dump_tables },
3498     { "dump-table-features", "switch",
3499       1, 1, ofctl_dump_table_features },
3500     { "dump-flows", "switch",
3501       1, 2, ofctl_dump_flows },
3502     { "dump-aggregate", "switch",
3503       1, 2, ofctl_dump_aggregate },
3504     { "queue-stats", "switch [port [queue]]",
3505       1, 3, ofctl_queue_stats },
3506     { "queue-get-config", "switch port",
3507       2, 2, ofctl_queue_get_config },
3508     { "add-flow", "switch flow",
3509       2, 2, ofctl_add_flow },
3510     { "add-flows", "switch file",
3511       2, 2, ofctl_add_flows },
3512     { "mod-flows", "switch flow",
3513       2, 2, ofctl_mod_flows },
3514     { "del-flows", "switch [flow]",
3515       1, 2, ofctl_del_flows },
3516     { "replace-flows", "switch file",
3517       2, 2, ofctl_replace_flows },
3518     { "diff-flows", "source1 source2",
3519       2, 2, ofctl_diff_flows },
3520     { "add-meter", "switch meter",
3521       2, 2, ofctl_add_meter },
3522     { "mod-meter", "switch meter",
3523       2, 2, ofctl_mod_meter },
3524     { "del-meter", "switch meter",
3525       2, 2, ofctl_del_meters },
3526     { "del-meters", "switch",
3527       1, 1, ofctl_del_meters },
3528     { "dump-meter", "switch meter",
3529       2, 2, ofctl_dump_meters },
3530     { "dump-meters", "switch",
3531       1, 1, ofctl_dump_meters },
3532     { "meter-stats", "switch [meter]",
3533       1, 2, ofctl_meter_stats },
3534     { "meter-features", "switch",
3535       1, 1, ofctl_meter_features },
3536     { "packet-out", "switch in_port actions packet...",
3537       4, INT_MAX, ofctl_packet_out },
3538     { "dump-ports", "switch [port]",
3539       1, 2, ofctl_dump_ports },
3540     { "dump-ports-desc", "switch [port]",
3541       1, 2, ofctl_dump_ports_desc },
3542     { "mod-port", "switch iface act",
3543       3, 3, ofctl_mod_port },
3544     { "mod-table", "switch mod",
3545       3, 3, ofctl_mod_table },
3546     { "get-frags", "switch",
3547       1, 1, ofctl_get_frags },
3548     { "set-frags", "switch frag_mode",
3549       2, 2, ofctl_set_frags },
3550     { "probe", "target",
3551       1, 1, ofctl_probe },
3552     { "ping", "target [n]",
3553       1, 2, ofctl_ping },
3554     { "benchmark", "target n count",
3555       3, 3, ofctl_benchmark },
3556
3557     { "ofp-parse", "file",
3558       1, 1, ofctl_ofp_parse },
3559     { "ofp-parse-pcap", "pcap",
3560       1, INT_MAX, ofctl_ofp_parse_pcap },
3561
3562     { "add-group", "switch group",
3563       1, 2, ofctl_add_group },
3564     { "add-groups", "switch file",
3565       1, 2, ofctl_add_groups },
3566     { "mod-group", "switch group",
3567       1, 2, ofctl_mod_group },
3568     { "del-groups", "switch [group]",
3569       1, 2, ofctl_del_groups },
3570     { "insert-buckets", "switch [group]",
3571       1, 2, ofctl_insert_bucket },
3572     { "remove-buckets", "switch [group]",
3573       1, 2, ofctl_remove_bucket },
3574     { "dump-groups", "switch [group]",
3575       1, 2, ofctl_dump_group_desc },
3576     { "dump-group-stats", "switch [group]",
3577       1, 2, ofctl_dump_group_stats },
3578     { "dump-group-features", "switch",
3579       1, 1, ofctl_dump_group_features },
3580     { "help", NULL, 0, INT_MAX, ofctl_help },
3581     { "list-commands", NULL, 0, INT_MAX, ofctl_list_commands },
3582
3583     /* Undocumented commands for testing. */
3584     { "parse-flow", NULL, 1, 1, ofctl_parse_flow },
3585     { "parse-flows", NULL, 1, 1, ofctl_parse_flows },
3586     { "parse-nx-match", NULL, 0, 0, ofctl_parse_nxm },
3587     { "parse-nxm", NULL, 0, 0, ofctl_parse_nxm },
3588     { "parse-oxm", NULL, 1, 1, ofctl_parse_oxm },
3589     { "parse-actions", NULL, 1, 1, ofctl_parse_actions },
3590     { "parse-instructions", NULL, 1, 1, ofctl_parse_instructions },
3591     { "parse-ofp10-match", NULL, 0, 0, ofctl_parse_ofp10_match },
3592     { "parse-ofp11-match", NULL, 0, 0, ofctl_parse_ofp11_match },
3593     { "parse-pcap", NULL, 1, 1, ofctl_parse_pcap },
3594     { "check-vlan", NULL, 2, 2, ofctl_check_vlan },
3595     { "print-error", NULL, 1, 1, ofctl_print_error },
3596     { "encode-error-reply", NULL, 2, 2, ofctl_encode_error_reply },
3597     { "ofp-print", NULL, 1, 2, ofctl_ofp_print },
3598     { "encode-hello", NULL, 1, 1, ofctl_encode_hello },
3599
3600     { NULL, NULL, 0, 0, NULL },
3601 };
3602
3603 static const struct ovs_cmdl_command *get_all_commands(void)
3604 {
3605     return all_commands;
3606 }