+\f
+/* replace-flows and diff-flows commands. */
+
+/* A flow table entry, possibly with two different versions. */
+struct fte {
+ struct cls_rule rule; /* Within a "struct classifier". */
+ struct fte_version *versions[2];
+};
+
+/* One version of a Flow Table Entry. */
+struct fte_version {
+ ovs_be64 cookie;
+ uint16_t idle_timeout;
+ uint16_t hard_timeout;
+ uint16_t flags;
+ struct ofpact *ofpacts;
+ size_t ofpacts_len;
+};
+
+/* Frees 'version' and the data that it owns. */
+static void
+fte_version_free(struct fte_version *version)
+{
+ if (version) {
+ free(version->ofpacts);
+ free(version);
+ }
+}
+
+/* Returns true if 'a' and 'b' are the same, false if they differ.
+ *
+ * Ignores differences in 'flags' because there's no way to retrieve flags from
+ * an OpenFlow switch. We have to assume that they are the same. */
+static bool
+fte_version_equals(const struct fte_version *a, const struct fte_version *b)
+{
+ return (a->cookie == b->cookie
+ && a->idle_timeout == b->idle_timeout
+ && a->hard_timeout == b->hard_timeout
+ && ofpacts_equal(a->ofpacts, a->ofpacts_len,
+ b->ofpacts, b->ofpacts_len));
+}
+
+/* Clears 's', then if 's' has a version 'index', formats 'fte' and version
+ * 'index' into 's', followed by a new-line. */
+static void
+fte_version_format(const struct fte *fte, int index, struct ds *s)
+{
+ const struct fte_version *version = fte->versions[index];
+
+ ds_clear(s);
+ if (!version) {
+ return;
+ }
+
+ cls_rule_format(&fte->rule, s);
+ if (version->cookie != htonll(0)) {
+ ds_put_format(s, " cookie=0x%"PRIx64, ntohll(version->cookie));
+ }
+ if (version->idle_timeout != OFP_FLOW_PERMANENT) {
+ ds_put_format(s, " idle_timeout=%"PRIu16, version->idle_timeout);
+ }
+ if (version->hard_timeout != OFP_FLOW_PERMANENT) {
+ ds_put_format(s, " hard_timeout=%"PRIu16, version->hard_timeout);
+ }
+
+ ds_put_char(s, ' ');
+ ofpacts_format(version->ofpacts, version->ofpacts_len, s);
+
+ ds_put_char(s, '\n');
+}
+
+static struct fte *
+fte_from_cls_rule(const struct cls_rule *cls_rule)
+{
+ return cls_rule ? CONTAINER_OF(cls_rule, struct fte, rule) : NULL;
+}
+
+/* Frees 'fte' and its versions. */
+static void
+fte_free(struct fte *fte)
+{
+ if (fte) {
+ fte_version_free(fte->versions[0]);
+ fte_version_free(fte->versions[1]);
+ free(fte);
+ }
+}
+
+/* Frees all of the FTEs within 'cls'. */
+static void
+fte_free_all(struct classifier *cls)
+{
+ struct cls_cursor cursor;
+ struct fte *fte, *next;
+
+ cls_cursor_init(&cursor, cls, NULL);
+ CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) {
+ classifier_remove(cls, &fte->rule);
+ fte_free(fte);
+ }
+ classifier_destroy(cls);
+}
+
+/* Searches 'cls' for an FTE matching 'rule', inserting a new one if
+ * necessary. Sets 'version' as the version of that rule with the given
+ * 'index', replacing any existing version, if any.
+ *
+ * Takes ownership of 'version'. */
+static void
+fte_insert(struct classifier *cls, const struct cls_rule *rule,
+ struct fte_version *version, int index)
+{
+ struct fte *old, *fte;
+
+ fte = xzalloc(sizeof *fte);
+ fte->rule = *rule;
+ fte->versions[index] = version;
+
+ old = fte_from_cls_rule(classifier_replace(cls, &fte->rule));
+ if (old) {
+ fte_version_free(old->versions[index]);
+ fte->versions[!index] = old->versions[!index];
+ free(old);
+ }
+}
+
+/* Reads the flows in 'filename' as flow table entries in 'cls' for the version
+ * with the specified 'index'. Returns the flow formats able to represent the
+ * flows that were read. */
+static enum ofputil_protocol
+read_flows_from_file(const char *filename, struct classifier *cls, int index)
+{
+ enum ofputil_protocol usable_protocols;
+ struct ds s;
+ FILE *file;
+
+ file = !strcmp(filename, "-") ? stdin : fopen(filename, "r");
+ if (file == NULL) {
+ ovs_fatal(errno, "%s: open", filename);
+ }
+
+ ds_init(&s);
+ usable_protocols = OFPUTIL_P_ANY;
+ while (!ds_get_preprocessed_line(&s, file)) {
+ struct fte_version *version;
+ struct ofputil_flow_mod fm;
+
+ parse_ofp_str(&fm, OFPFC_ADD, ds_cstr(&s), true);
+
+ version = xmalloc(sizeof *version);
+ version->cookie = fm.new_cookie;
+ version->idle_timeout = fm.idle_timeout;
+ version->hard_timeout = fm.hard_timeout;
+ version->flags = fm.flags & (OFPFF_SEND_FLOW_REM | OFPFF10_EMERG);
+ version->ofpacts = fm.ofpacts;
+ version->ofpacts_len = fm.ofpacts_len;
+
+ usable_protocols &= ofputil_usable_protocols(&fm.cr);
+
+ fte_insert(cls, &fm.cr, version, index);
+ }
+ ds_destroy(&s);
+
+ if (file != stdin) {
+ fclose(file);
+ }
+
+ return usable_protocols;
+}
+
+static bool
+recv_flow_stats_reply(struct vconn *vconn, ovs_be32 send_xid,
+ struct ofpbuf **replyp,
+ struct ofputil_flow_stats *fs, struct ofpbuf *ofpacts)
+{
+ struct ofpbuf *reply = *replyp;
+
+ for (;;) {
+ int retval;
+ bool more;
+
+ /* Get a flow stats reply message, if we don't already have one. */
+ if (!reply) {
+ enum ofptype type;
+ enum ofperr error;
+
+ do {
+ run(vconn_recv_block(vconn, &reply),
+ "OpenFlow packet receive failed");
+ } while (((struct ofp_header *) reply->data)->xid != send_xid);
+
+ error = ofptype_decode(&type, reply->data);
+ if (error || type != OFPTYPE_FLOW_STATS_REPLY) {
+ ovs_fatal(0, "received bad reply: %s",
+ ofp_to_string(reply->data, reply->size,
+ verbosity + 1));
+ }
+ }
+
+ /* Pull an individual flow stats reply out of the message. */
+ retval = ofputil_decode_flow_stats_reply(fs, reply, false, ofpacts);
+ switch (retval) {
+ case 0:
+ *replyp = reply;
+ return true;
+
+ case EOF:
+ more = ofpmp_more(reply->l2);
+ ofpbuf_delete(reply);
+ reply = NULL;
+ if (!more) {
+ *replyp = NULL;
+ return false;
+ }
+ break;
+
+ default:
+ ovs_fatal(0, "parse error in reply (%s)",
+ ofperr_to_string(retval));
+ }
+ }
+}
+
+/* Reads the OpenFlow flow table from 'vconn', which has currently active flow
+ * format 'protocol', and adds them as flow table entries in 'cls' for the
+ * version with the specified 'index'. */
+static void
+read_flows_from_switch(struct vconn *vconn,
+ enum ofputil_protocol protocol,
+ struct classifier *cls, int index)
+{
+ struct ofputil_flow_stats_request fsr;
+ struct ofputil_flow_stats fs;
+ struct ofpbuf *request;
+ struct ofpbuf ofpacts;
+ struct ofpbuf *reply;
+ ovs_be32 send_xid;
+
+ fsr.aggregate = false;
+ cls_rule_init_catchall(&fsr.match, 0);
+ fsr.out_port = OFPP_NONE;
+ fsr.table_id = 0xff;
+ fsr.cookie = fsr.cookie_mask = htonll(0);
+ request = ofputil_encode_flow_stats_request(&fsr, protocol);
+ send_xid = ((struct ofp_header *) request->data)->xid;
+ send_openflow_buffer(vconn, request);
+
+ reply = NULL;
+ ofpbuf_init(&ofpacts, 0);
+ while (recv_flow_stats_reply(vconn, send_xid, &reply, &fs, &ofpacts)) {
+ struct fte_version *version;
+
+ version = xmalloc(sizeof *version);
+ version->cookie = fs.cookie;
+ version->idle_timeout = fs.idle_timeout;
+ version->hard_timeout = fs.hard_timeout;
+ version->flags = 0;
+ version->ofpacts_len = fs.ofpacts_len;
+ version->ofpacts = xmemdup(fs.ofpacts, fs.ofpacts_len);
+
+ fte_insert(cls, &fs.rule, version, index);
+ }
+ ofpbuf_uninit(&ofpacts);
+}
+
+static void
+fte_make_flow_mod(const struct fte *fte, int index, uint16_t command,
+ enum ofputil_protocol protocol, struct list *packets)
+{
+ const struct fte_version *version = fte->versions[index];
+ struct ofputil_flow_mod fm;
+ struct ofpbuf *ofm;
+
+ fm.cr = fte->rule;
+ fm.cookie = htonll(0);
+ fm.cookie_mask = htonll(0);
+ fm.new_cookie = version->cookie;
+ fm.table_id = 0xff;
+ fm.command = command;
+ fm.idle_timeout = version->idle_timeout;
+ fm.hard_timeout = version->hard_timeout;
+ fm.buffer_id = UINT32_MAX;
+ fm.out_port = OFPP_NONE;
+ fm.flags = version->flags;
+ if (command == OFPFC_ADD || command == OFPFC_MODIFY ||
+ command == OFPFC_MODIFY_STRICT) {
+ fm.ofpacts = version->ofpacts;
+ fm.ofpacts_len = version->ofpacts_len;
+ } else {
+ fm.ofpacts = NULL;
+ fm.ofpacts_len = 0;
+ }
+
+ ofm = ofputil_encode_flow_mod(&fm, protocol);
+ list_push_back(packets, &ofm->list_node);
+}
+
+static void
+ofctl_replace_flows(int argc OVS_UNUSED, char *argv[])
+{
+ enum { FILE_IDX = 0, SWITCH_IDX = 1 };
+ enum ofputil_protocol usable_protocols, protocol;
+ struct cls_cursor cursor;
+ struct classifier cls;
+ struct list requests;
+ struct vconn *vconn;
+ struct fte *fte;
+
+ classifier_init(&cls);
+ usable_protocols = read_flows_from_file(argv[2], &cls, FILE_IDX);
+
+ protocol = open_vconn(argv[1], &vconn);
+ protocol = set_protocol_for_flow_dump(vconn, protocol, usable_protocols);
+
+ read_flows_from_switch(vconn, protocol, &cls, SWITCH_IDX);
+
+ list_init(&requests);
+
+ /* Delete flows that exist on the switch but not in the file. */
+ cls_cursor_init(&cursor, &cls, NULL);
+ CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
+ struct fte_version *file_ver = fte->versions[FILE_IDX];
+ struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
+
+ if (sw_ver && !file_ver) {
+ fte_make_flow_mod(fte, SWITCH_IDX, OFPFC_DELETE_STRICT,
+ protocol, &requests);
+ }
+ }
+
+ /* Add flows that exist in the file but not on the switch.
+ * Update flows that exist in both places but differ. */
+ cls_cursor_init(&cursor, &cls, NULL);
+ CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
+ struct fte_version *file_ver = fte->versions[FILE_IDX];
+ struct fte_version *sw_ver = fte->versions[SWITCH_IDX];
+
+ if (file_ver
+ && (readd || !sw_ver || !fte_version_equals(sw_ver, file_ver))) {
+ fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests);
+ }
+ }
+ transact_multiple_noreply(vconn, &requests);
+ vconn_close(vconn);
+
+ fte_free_all(&cls);
+}
+
+static void
+read_flows_from_source(const char *source, struct classifier *cls, int index)
+{
+ struct stat s;
+
+ if (source[0] == '/' || source[0] == '.'
+ || (!strchr(source, ':') && !stat(source, &s))) {
+ read_flows_from_file(source, cls, index);
+ } else {
+ enum ofputil_protocol protocol;
+ struct vconn *vconn;
+
+ protocol = open_vconn(source, &vconn);
+ protocol = set_protocol_for_flow_dump(vconn, protocol, OFPUTIL_P_ANY);
+ read_flows_from_switch(vconn, protocol, cls, index);
+ vconn_close(vconn);
+ }
+}
+
+static void
+ofctl_diff_flows(int argc OVS_UNUSED, char *argv[])
+{
+ bool differences = false;
+ struct cls_cursor cursor;
+ struct classifier cls;
+ struct ds a_s, b_s;
+ struct fte *fte;
+
+ classifier_init(&cls);
+ read_flows_from_source(argv[1], &cls, 0);
+ read_flows_from_source(argv[2], &cls, 1);
+
+ ds_init(&a_s);
+ ds_init(&b_s);
+
+ cls_cursor_init(&cursor, &cls, NULL);
+ CLS_CURSOR_FOR_EACH (fte, rule, &cursor) {
+ struct fte_version *a = fte->versions[0];
+ struct fte_version *b = fte->versions[1];
+
+ if (!a || !b || !fte_version_equals(a, b)) {
+ fte_version_format(fte, 0, &a_s);
+ fte_version_format(fte, 1, &b_s);
+ if (strcmp(ds_cstr(&a_s), ds_cstr(&b_s))) {
+ if (a_s.length) {
+ printf("-%s", ds_cstr(&a_s));
+ }
+ if (b_s.length) {
+ printf("+%s", ds_cstr(&b_s));
+ }
+ differences = true;
+ }
+ }
+ }
+
+ ds_destroy(&a_s);
+ ds_destroy(&b_s);
+
+ fte_free_all(&cls);
+
+ if (differences) {
+ exit(2);
+ }
+}
+\f
+/* Undocumented commands for unit testing. */
+
+static void
+ofctl_parse_flows__(struct ofputil_flow_mod *fms, size_t n_fms)
+{
+ enum ofputil_protocol usable_protocols;
+ enum ofputil_protocol protocol = 0;
+ char *usable_s;
+ size_t i;
+
+ usable_protocols = ofputil_flow_mod_usable_protocols(fms, n_fms);
+ usable_s = ofputil_protocols_to_string(usable_protocols);
+ printf("usable protocols: %s\n", usable_s);
+ free(usable_s);
+
+ if (!(usable_protocols & allowed_protocols)) {
+ ovs_fatal(0, "no usable protocol");
+ }
+ for (i = 0; i < sizeof(enum ofputil_protocol) * CHAR_BIT; i++) {
+ protocol = 1 << i;
+ if (protocol & usable_protocols & allowed_protocols) {
+ break;
+ }
+ }
+ assert(IS_POW2(protocol));
+
+ printf("chosen protocol: %s\n", ofputil_protocol_to_string(protocol));
+
+ for (i = 0; i < n_fms; i++) {
+ struct ofputil_flow_mod *fm = &fms[i];
+ struct ofpbuf *msg;
+
+ msg = ofputil_encode_flow_mod(fm, protocol);
+ ofp_print(stdout, msg->data, msg->size, verbosity);
+ ofpbuf_delete(msg);
+
+ free(fm->ofpacts);
+ }
+}
+
+/* "parse-flow FLOW": parses the argument as a flow (like add-flow) and prints
+ * it back to stdout. */
+static void
+ofctl_parse_flow(int argc OVS_UNUSED, char *argv[])
+{
+ struct ofputil_flow_mod fm;
+
+ parse_ofp_flow_mod_str(&fm, argv[1], OFPFC_ADD, false);
+ ofctl_parse_flows__(&fm, 1);
+}
+
+/* "parse-flows FILENAME": reads the named file as a sequence of flows (like
+ * add-flows) and prints each of the flows back to stdout. */
+static void
+ofctl_parse_flows(int argc OVS_UNUSED, char *argv[])
+{
+ struct ofputil_flow_mod *fms = NULL;
+ size_t n_fms = 0;
+
+ parse_ofp_flow_mod_file(argv[1], OFPFC_ADD, &fms, &n_fms);
+ ofctl_parse_flows__(fms, n_fms);
+ free(fms);
+}
+
+static void
+ofctl_parse_nxm__(bool oxm)
+{
+ struct ds in;
+
+ ds_init(&in);
+ while (!ds_get_test_line(&in, stdin)) {
+ struct ofpbuf nx_match;
+ struct cls_rule rule;
+ ovs_be64 cookie, cookie_mask;
+ enum ofperr error;
+ int match_len;
+
+ /* Convert string to nx_match. */
+ ofpbuf_init(&nx_match, 0);
+ if (oxm) {
+ match_len = oxm_match_from_string(ds_cstr(&in), &nx_match);
+ } else {
+ match_len = nx_match_from_string(ds_cstr(&in), &nx_match);
+ }
+
+ /* Convert nx_match to cls_rule. */
+ if (strict) {
+ if (oxm) {
+ error = oxm_pull_match(&nx_match, 0, &rule);
+ } else {
+ error = nx_pull_match(&nx_match, match_len, 0, &rule,
+ &cookie, &cookie_mask);
+ }
+ } else {
+ if (oxm) {
+ error = oxm_pull_match_loose(&nx_match, 0, &rule);
+ } else {
+ error = nx_pull_match_loose(&nx_match, match_len, 0, &rule,
+ &cookie, &cookie_mask);
+ }
+ }
+
+
+ if (!error) {
+ char *out;
+
+ /* Convert cls_rule back to nx_match. */
+ ofpbuf_uninit(&nx_match);
+ ofpbuf_init(&nx_match, 0);
+ if (oxm) {
+ match_len = oxm_put_match(&nx_match, &rule);
+ out = oxm_match_to_string(nx_match.data, match_len);
+ } else {
+ match_len = nx_put_match(&nx_match, &rule,
+ cookie, cookie_mask);
+ out = nx_match_to_string(nx_match.data, match_len);
+ }
+
+ puts(out);
+ free(out);
+ } else {
+ printf("nx_pull_match() returned error %s\n",
+ ofperr_get_name(error));
+ }
+
+ ofpbuf_uninit(&nx_match);
+ }
+ ds_destroy(&in);
+}
+
+/* "parse-nxm": reads a series of NXM nx_match specifications as strings from
+ * stdin, does some internal fussing with them, and then prints them back as
+ * strings on stdout. */
+static void
+ofctl_parse_nxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+ return ofctl_parse_nxm__(false);
+}
+
+/* "parse-oxm": reads a series of OXM nx_match specifications as strings from
+ * stdin, does some internal fussing with them, and then prints them back as
+ * strings on stdout. */
+static void
+ofctl_parse_oxm(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+ return ofctl_parse_nxm__(true);
+}
+
+static void
+print_differences(const char *prefix,
+ const void *a_, size_t a_len,
+ const void *b_, size_t b_len)
+{
+ const uint8_t *a = a_;
+ const uint8_t *b = b_;
+ size_t i;
+
+ for (i = 0; i < MIN(a_len, b_len); i++) {
+ if (a[i] != b[i]) {
+ printf("%s%2zu: %02"PRIx8" -> %02"PRIx8"\n",
+ prefix, i, a[i], b[i]);
+ }
+ }
+ for (i = a_len; i < b_len; i++) {
+ printf("%s%2zu: (none) -> %02"PRIx8"\n", prefix, i, b[i]);
+ }
+ for (i = b_len; i < a_len; i++) {
+ printf("%s%2zu: %02"PRIx8" -> (none)\n", prefix, i, a[i]);
+ }
+}
+
+/* "parse-ofp10-actions": reads a series of OpenFlow 1.0 action specifications
+ * as hex bytes from stdin, converts them to ofpacts, prints them as strings
+ * on stdout, and then converts them back to hex bytes and prints any
+ * differences from the input. */
+static void
+ofctl_parse_ofp10_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+ struct ds in;
+
+ ds_init(&in);
+ while (!ds_get_preprocessed_line(&in, stdin)) {
+ struct ofpbuf of10_out;
+ struct ofpbuf of10_in;
+ struct ofpbuf ofpacts;
+ enum ofperr error;
+ size_t size;
+ struct ds s;
+
+ /* Parse hex bytes. */
+ ofpbuf_init(&of10_in, 0);
+ if (ofpbuf_put_hex(&of10_in, ds_cstr(&in), NULL)[0] != '\0') {
+ ovs_fatal(0, "Trailing garbage in hex data");
+ }
+
+ /* Convert to ofpacts. */
+ ofpbuf_init(&ofpacts, 0);
+ size = of10_in.size;
+ error = ofpacts_pull_openflow10(&of10_in, of10_in.size, &ofpacts);
+ if (error) {
+ printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
+ ofpbuf_uninit(&ofpacts);
+ ofpbuf_uninit(&of10_in);
+ continue;
+ }
+ ofpbuf_push_uninit(&of10_in, size);
+
+ /* Print cls_rule. */
+ ds_init(&s);
+ ofpacts_format(ofpacts.data, ofpacts.size, &s);
+ puts(ds_cstr(&s));
+ ds_destroy(&s);
+
+ /* Convert back to ofp10 actions and print differences from input. */
+ ofpbuf_init(&of10_out, 0);
+ ofpacts_put_openflow10(ofpacts.data, ofpacts.size, &of10_out);
+
+ print_differences("", of10_in.data, of10_in.size,
+ of10_out.data, of10_out.size);
+ putchar('\n');
+
+ ofpbuf_uninit(&ofpacts);
+ ofpbuf_uninit(&of10_in);
+ ofpbuf_uninit(&of10_out);
+ }
+ ds_destroy(&in);
+}
+
+/* "parse-ofp10-match": reads a series of ofp10_match specifications as hex
+ * bytes from stdin, converts them to cls_rules, prints them as strings on
+ * stdout, and then converts them back to hex bytes and prints any differences
+ * from the input. */
+static void
+ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+ struct ds in;
+
+ ds_init(&in);
+ while (!ds_get_preprocessed_line(&in, stdin)) {
+ struct ofpbuf match_in;
+ struct ofp10_match match_out;
+ struct ofp10_match match_normal;
+ struct cls_rule rule;
+
+ /* Parse hex bytes. */
+ ofpbuf_init(&match_in, 0);
+ if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
+ ovs_fatal(0, "Trailing garbage in hex data");
+ }
+ if (match_in.size != sizeof(struct ofp10_match)) {
+ ovs_fatal(0, "Input is %zu bytes, expected %zu",
+ match_in.size, sizeof(struct ofp10_match));
+ }
+
+ /* Convert to cls_rule and print. */
+ ofputil_cls_rule_from_ofp10_match(match_in.data, OFP_DEFAULT_PRIORITY,
+ &rule);
+ cls_rule_print(&rule);
+
+ /* Convert back to ofp10_match and print differences from input. */
+ ofputil_cls_rule_to_ofp10_match(&rule, &match_out);
+ print_differences("", match_in.data, match_in.size,
+ &match_out, sizeof match_out);
+
+ /* Normalize, then convert and compare again. */
+ ofputil_normalize_rule(&rule);
+ ofputil_cls_rule_to_ofp10_match(&rule, &match_normal);
+ print_differences("normal: ", &match_out, sizeof match_out,
+ &match_normal, sizeof match_normal);
+ putchar('\n');
+
+ ofpbuf_uninit(&match_in);
+ }
+ ds_destroy(&in);
+}
+
+/* "parse-ofp11-match": reads a series of ofp11_match specifications as hex
+ * bytes from stdin, converts them to cls_rules, prints them as strings on
+ * stdout, and then converts them back to hex bytes and prints any differences
+ * from the input. */
+static void
+ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+ struct ds in;
+
+ ds_init(&in);
+ while (!ds_get_preprocessed_line(&in, stdin)) {
+ struct ofpbuf match_in;
+ struct ofp11_match match_out;
+ struct cls_rule rule;
+ enum ofperr error;
+
+ /* Parse hex bytes. */
+ ofpbuf_init(&match_in, 0);
+ if (ofpbuf_put_hex(&match_in, ds_cstr(&in), NULL)[0] != '\0') {
+ ovs_fatal(0, "Trailing garbage in hex data");
+ }
+ if (match_in.size != sizeof(struct ofp11_match)) {
+ ovs_fatal(0, "Input is %zu bytes, expected %zu",
+ match_in.size, sizeof(struct ofp11_match));
+ }
+
+ /* Convert to cls_rule. */
+ error = ofputil_cls_rule_from_ofp11_match(match_in.data,
+ OFP_DEFAULT_PRIORITY, &rule);
+ if (error) {
+ printf("bad ofp11_match: %s\n\n", ofperr_get_name(error));
+ ofpbuf_uninit(&match_in);
+ continue;
+ }
+
+ /* Print cls_rule. */
+ cls_rule_print(&rule);
+
+ /* Convert back to ofp11_match and print differences from input. */
+ ofputil_cls_rule_to_ofp11_match(&rule, &match_out);
+
+ print_differences("", match_in.data, match_in.size,
+ &match_out, sizeof match_out);
+ putchar('\n');
+
+ ofpbuf_uninit(&match_in);
+ }
+ ds_destroy(&in);
+}
+
+/* "parse-ofp11-actions": reads a series of OpenFlow 1.1 action specifications
+ * as hex bytes from stdin, converts them to ofpacts, prints them as strings
+ * on stdout, and then converts them back to hex bytes and prints any
+ * differences from the input. */
+static void
+ofctl_parse_ofp11_actions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+ struct ds in;
+
+ ds_init(&in);
+ while (!ds_get_preprocessed_line(&in, stdin)) {
+ struct ofpbuf of11_out;
+ struct ofpbuf of11_in;
+ struct ofpbuf ofpacts;
+ enum ofperr error;
+ size_t size;
+ struct ds s;
+
+ /* Parse hex bytes. */
+ ofpbuf_init(&of11_in, 0);
+ if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
+ ovs_fatal(0, "Trailing garbage in hex data");
+ }
+
+ /* Convert to ofpacts. */
+ ofpbuf_init(&ofpacts, 0);
+ size = of11_in.size;
+ error = ofpacts_pull_openflow11_actions(&of11_in, of11_in.size,
+ &ofpacts);
+ if (error) {
+ printf("bad OF1.1 actions: %s\n\n", ofperr_get_name(error));
+ ofpbuf_uninit(&ofpacts);
+ ofpbuf_uninit(&of11_in);
+ continue;
+ }
+ ofpbuf_push_uninit(&of11_in, size);
+
+ /* Print cls_rule. */
+ ds_init(&s);
+ ofpacts_format(ofpacts.data, ofpacts.size, &s);
+ puts(ds_cstr(&s));
+ ds_destroy(&s);
+
+ /* Convert back to ofp11 actions and print differences from input. */
+ ofpbuf_init(&of11_out, 0);
+ ofpacts_put_openflow11_actions(ofpacts.data, ofpacts.size, &of11_out);
+
+ print_differences("", of11_in.data, of11_in.size,
+ of11_out.data, of11_out.size);
+ putchar('\n');
+
+ ofpbuf_uninit(&ofpacts);
+ ofpbuf_uninit(&of11_in);
+ ofpbuf_uninit(&of11_out);
+ }
+ ds_destroy(&in);
+}
+
+/* "parse-ofp11-instructions": reads a series of OpenFlow 1.1 instruction
+ * specifications as hex bytes from stdin, converts them to ofpacts, prints
+ * them as strings on stdout, and then converts them back to hex bytes and
+ * prints any differences from the input. */
+static void
+ofctl_parse_ofp11_instructions(int argc OVS_UNUSED, char *argv[] OVS_UNUSED)
+{
+ struct ds in;
+
+ ds_init(&in);
+ while (!ds_get_preprocessed_line(&in, stdin)) {
+ struct ofpbuf of11_out;
+ struct ofpbuf of11_in;
+ struct ofpbuf ofpacts;
+ enum ofperr error;
+ size_t size;
+ struct ds s;
+
+ /* Parse hex bytes. */
+ ofpbuf_init(&of11_in, 0);
+ if (ofpbuf_put_hex(&of11_in, ds_cstr(&in), NULL)[0] != '\0') {
+ ovs_fatal(0, "Trailing garbage in hex data");
+ }
+
+ /* Convert to ofpacts. */
+ ofpbuf_init(&ofpacts, 0);
+ size = of11_in.size;
+ error = ofpacts_pull_openflow11_instructions(&of11_in, of11_in.size,
+ &ofpacts);
+ if (error) {
+ printf("bad OF1.1 instructions: %s\n\n", ofperr_get_name(error));
+ ofpbuf_uninit(&ofpacts);
+ ofpbuf_uninit(&of11_in);
+ continue;
+ }
+ ofpbuf_push_uninit(&of11_in, size);
+
+ /* Print cls_rule. */
+ ds_init(&s);
+ ofpacts_format(ofpacts.data, ofpacts.size, &s);
+ puts(ds_cstr(&s));
+ ds_destroy(&s);
+
+ /* Convert back to ofp11 instructions and print differences from
+ * input. */
+ ofpbuf_init(&of11_out, 0);
+ ofpacts_put_openflow11_instructions(ofpacts.data, ofpacts.size,
+ &of11_out);
+
+ print_differences("", of11_in.data, of11_in.size,
+ of11_out.data, of11_out.size);
+ putchar('\n');
+
+ ofpbuf_uninit(&ofpacts);
+ ofpbuf_uninit(&of11_in);
+ ofpbuf_uninit(&of11_out);
+ }
+ ds_destroy(&in);
+}
+
+/* "check-vlan VLAN_TCI VLAN_TCI_MASK": converts the specified vlan_tci and
+ * mask values to and from various formats and prints the results. */
+static void
+ofctl_check_vlan(int argc OVS_UNUSED, char *argv[])
+{
+ struct cls_rule rule;
+
+ char *string_s;
+ struct ofputil_flow_mod fm;
+
+ struct ofpbuf nxm;
+ struct cls_rule nxm_rule;
+ int nxm_match_len;
+ char *nxm_s;
+
+ struct ofp10_match of10_match;
+ struct cls_rule of10_rule;
+
+ struct ofp11_match of11_match;
+ struct cls_rule of11_rule;
+
+ enum ofperr error;
+
+ cls_rule_init_catchall(&rule, OFP_DEFAULT_PRIORITY);
+ rule.flow.vlan_tci = htons(strtoul(argv[1], NULL, 16));
+ rule.wc.vlan_tci_mask = htons(strtoul(argv[2], NULL, 16));
+
+ /* Convert to and from string. */
+ string_s = cls_rule_to_string(&rule);
+ printf("%s -> ", string_s);
+ fflush(stdout);
+ parse_ofp_str(&fm, -1, string_s, false);
+ printf("%04"PRIx16"/%04"PRIx16"\n",
+ ntohs(fm.cr.flow.vlan_tci),
+ ntohs(fm.cr.wc.vlan_tci_mask));
+
+ /* Convert to and from NXM. */
+ ofpbuf_init(&nxm, 0);
+ nxm_match_len = nx_put_match(&nxm, &rule, htonll(0), htonll(0));
+ nxm_s = nx_match_to_string(nxm.data, nxm_match_len);
+ error = nx_pull_match(&nxm, nxm_match_len, 0, &nxm_rule, NULL, NULL);
+ printf("NXM: %s -> ", nxm_s);
+ if (error) {
+ printf("%s\n", ofperr_to_string(error));
+ } else {
+ printf("%04"PRIx16"/%04"PRIx16"\n",
+ ntohs(nxm_rule.flow.vlan_tci),
+ ntohs(nxm_rule.wc.vlan_tci_mask));
+ }
+ free(nxm_s);
+ ofpbuf_uninit(&nxm);
+
+ /* Convert to and from OXM. */
+ ofpbuf_init(&nxm, 0);
+ nxm_match_len = oxm_put_match(&nxm, &rule);
+ nxm_s = oxm_match_to_string(nxm.data, nxm_match_len);
+ error = oxm_pull_match(&nxm, 0, &nxm_rule);
+ printf("OXM: %s -> ", nxm_s);
+ if (error) {
+ printf("%s\n", ofperr_to_string(error));
+ } else {
+ uint16_t vid = ntohs(nxm_rule.flow.vlan_tci) &
+ (VLAN_VID_MASK | VLAN_CFI);
+ uint16_t mask = ntohs(nxm_rule.wc.vlan_tci_mask) &
+ (VLAN_VID_MASK | VLAN_CFI);
+
+ printf("%04"PRIx16"/%04"PRIx16",", vid, mask);
+ if (vid && vlan_tci_to_pcp(nxm_rule.wc.vlan_tci_mask)) {
+ printf("%02"PRIx8"\n", vlan_tci_to_pcp(nxm_rule.flow.vlan_tci));
+ } else {
+ printf("--\n");
+ }
+ }
+ free(nxm_s);
+ ofpbuf_uninit(&nxm);
+
+ /* Convert to and from OpenFlow 1.0. */
+ ofputil_cls_rule_to_ofp10_match(&rule, &of10_match);
+ ofputil_cls_rule_from_ofp10_match(&of10_match, 0, &of10_rule);
+ printf("OF1.0: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
+ ntohs(of10_match.dl_vlan),
+ (of10_match.wildcards & htonl(OFPFW10_DL_VLAN)) != 0,
+ of10_match.dl_vlan_pcp,
+ (of10_match.wildcards & htonl(OFPFW10_DL_VLAN_PCP)) != 0,
+ ntohs(of10_rule.flow.vlan_tci),
+ ntohs(of10_rule.wc.vlan_tci_mask));
+
+ /* Convert to and from OpenFlow 1.1. */
+ ofputil_cls_rule_to_ofp11_match(&rule, &of11_match);
+ ofputil_cls_rule_from_ofp11_match(&of11_match, 0, &of11_rule);
+ printf("OF1.1: %04"PRIx16"/%d,%02"PRIx8"/%d -> %04"PRIx16"/%04"PRIx16"\n",
+ ntohs(of11_match.dl_vlan),
+ (of11_match.wildcards & htonl(OFPFW11_DL_VLAN)) != 0,
+ of11_match.dl_vlan_pcp,
+ (of11_match.wildcards & htonl(OFPFW11_DL_VLAN_PCP)) != 0,
+ ntohs(of11_rule.flow.vlan_tci),
+ ntohs(of11_rule.wc.vlan_tci_mask));
+}
+
+/* "print-error ENUM": Prints the type and code of ENUM for every OpenFlow
+ * version. */
+static void
+ofctl_print_error(int argc OVS_UNUSED, char *argv[])
+{
+ enum ofperr error;
+ int version;
+
+ error = ofperr_from_name(argv[1]);
+ if (!error) {
+ ovs_fatal(0, "unknown error \"%s\"", argv[1]);
+ }
+
+ for (version = 0; version <= UINT8_MAX; version++) {
+ const struct ofperr_domain *domain;
+
+ domain = ofperr_domain_from_version(version);
+ if (!domain) {
+ continue;
+ }
+
+ printf("%s: %d,%d\n",
+ ofperr_domain_get_name(domain),
+ ofperr_get_type(error, domain),
+ ofperr_get_code(error, domain));
+ }
+}
+
+/* "ofp-print HEXSTRING [VERBOSITY]": Converts the hex digits in HEXSTRING into
+ * binary data, interpreting them as an OpenFlow message, and prints the
+ * OpenFlow message on stdout, at VERBOSITY (level 2 by default). */
+static void
+ofctl_ofp_print(int argc, char *argv[])
+{
+ struct ofpbuf packet;
+
+ ofpbuf_init(&packet, strlen(argv[1]) / 2);
+ if (ofpbuf_put_hex(&packet, argv[1], NULL)[0] != '\0') {
+ ovs_fatal(0, "trailing garbage following hex bytes");
+ }
+ ofp_print(stdout, packet.data, packet.size, argc > 2 ? atoi(argv[2]) : 2);
+ ofpbuf_uninit(&packet);
+}
+
+static const struct command all_commands[] = {
+ { "show", 1, 1, ofctl_show },
+ { "monitor", 1, 3, ofctl_monitor },
+ { "snoop", 1, 1, ofctl_snoop },
+ { "dump-desc", 1, 1, ofctl_dump_desc },
+ { "dump-tables", 1, 1, ofctl_dump_tables },
+ { "dump-flows", 1, 2, ofctl_dump_flows },
+ { "dump-aggregate", 1, 2, ofctl_dump_aggregate },
+ { "queue-stats", 1, 3, ofctl_queue_stats },
+ { "add-flow", 2, 2, ofctl_add_flow },
+ { "add-flows", 2, 2, ofctl_add_flows },
+ { "mod-flows", 2, 2, ofctl_mod_flows },
+ { "del-flows", 1, 2, ofctl_del_flows },
+ { "replace-flows", 2, 2, ofctl_replace_flows },
+ { "diff-flows", 2, 2, ofctl_diff_flows },
+ { "packet-out", 4, INT_MAX, ofctl_packet_out },
+ { "dump-ports", 1, 2, ofctl_dump_ports },
+ { "dump-ports-desc", 1, 1, ofctl_dump_ports_desc },
+ { "mod-port", 3, 3, ofctl_mod_port },
+ { "get-frags", 1, 1, ofctl_get_frags },
+ { "set-frags", 2, 2, ofctl_set_frags },
+ { "probe", 1, 1, ofctl_probe },
+ { "ping", 1, 2, ofctl_ping },
+ { "benchmark", 3, 3, ofctl_benchmark },
+ { "help", 0, INT_MAX, ofctl_help },
+
+ /* Undocumented commands for testing. */
+ { "parse-flow", 1, 1, ofctl_parse_flow },
+ { "parse-flows", 1, 1, ofctl_parse_flows },
+ { "parse-nx-match", 0, 0, ofctl_parse_nxm },
+ { "parse-nxm", 0, 0, ofctl_parse_nxm },
+ { "parse-oxm", 0, 0, ofctl_parse_oxm },
+ { "parse-ofp10-actions", 0, 0, ofctl_parse_ofp10_actions },
+ { "parse-ofp10-match", 0, 0, ofctl_parse_ofp10_match },
+ { "parse-ofp11-match", 0, 0, ofctl_parse_ofp11_match },
+ { "parse-ofp11-actions", 0, 0, ofctl_parse_ofp11_actions },
+ { "parse-ofp11-instructions", 0, 0, ofctl_parse_ofp11_instructions },
+ { "check-vlan", 2, 2, ofctl_check_vlan },
+ { "print-error", 1, 1, ofctl_print_error },
+ { "ofp-print", 1, 2, ofctl_ofp_print },