X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=utilities%2Fovs-ofctl.c;h=69dd34fa4178b9d7db11fda168d087ab308a712a;hb=8bfd0fdace852f2208b97430bd8de9aeadbf2fb0;hp=1d1b47f95cc0898248d5c03a7b7852c37e995146;hpb=ba2fe8e9f17602a260a626c6a823584975dbe757;p=sliver-openvswitch.git diff --git a/utilities/ovs-ofctl.c b/utilities/ovs-ofctl.c index 1d1b47f95..69dd34fa4 100644 --- a/utilities/ovs-ofctl.c +++ b/utilities/ovs-ofctl.c @@ -325,7 +325,8 @@ usage(void) " benchmark TARGET N COUNT bandwidth of COUNT N-byte echos\n" "SWITCH or TARGET is an active OpenFlow connection method.\n" "\nOther commands:\n" - " ofp-parse FILE print messages read from FILE\n", + " ofp-parse FILE print messages read from FILE\n" + " ofp-parse-pcap PCAP print OpenFlow read from PCAP\n", program_name, program_name); vconn_usage(true, false, false); daemon_usage(); @@ -1539,7 +1540,7 @@ ofctl_monitor(int argc, char *argv[]) case OFP13_VERSION: break; default: - NOT_REACHED(); + OVS_NOT_REACHED(); } } @@ -1601,7 +1602,7 @@ ofctl_packet_out(int argc, char *argv[]) struct vconn *vconn; char *error; int i; - enum ofputil_protocol usable_protocols; /* TODO: Use in proto selection */ + enum ofputil_protocol usable_protocols; /* XXX: Use in proto selection */ ofpbuf_init(&ofpacts, 64); error = parse_ofpacts(argv[3], &ofpacts, &usable_protocols); @@ -1805,7 +1806,7 @@ ofctl_ofp_parse(int argc OVS_UNUSED, char *argv[]) length = ntohs(oh->length); if (length < sizeof *oh) { - ovs_fatal(0, "%s: %zu-byte message is too short for OpenFlow", + ovs_fatal(0, "%s: %"PRIuSIZE"-byte message is too short for OpenFlow", filename, length); } @@ -1825,6 +1826,92 @@ ofctl_ofp_parse(int argc OVS_UNUSED, char *argv[]) } } +static bool +is_openflow_port(ovs_be16 port_, char *ports[]) +{ + uint16_t port = ntohs(port_); + if (ports[0]) { + int i; + + for (i = 0; ports[i]; i++) { + if (port == atoi(ports[i])) { + return true; + } + } + return false; + } else { + return port == OFP_PORT || port == OFP_OLD_PORT; + } +} + +static void +ofctl_ofp_parse_pcap(int argc OVS_UNUSED, char *argv[]) +{ + struct tcp_reader *reader; + FILE *file; + int error; + bool first; + + file = ovs_pcap_open(argv[1], "rb"); + if (!file) { + ovs_fatal(errno, "%s: open failed", argv[1]); + } + + reader = tcp_reader_open(); + first = true; + for (;;) { + struct ofpbuf *packet; + long long int when; + struct flow flow; + + error = ovs_pcap_read(file, &packet, &when); + if (error) { + break; + } + flow_extract(packet, 0, 0, NULL, NULL, &flow); + if (flow.dl_type == htons(ETH_TYPE_IP) + && flow.nw_proto == IPPROTO_TCP + && (is_openflow_port(flow.tp_src, argv + 2) || + is_openflow_port(flow.tp_dst, argv + 2))) { + struct ofpbuf *payload = tcp_reader_run(reader, &flow, packet); + if (payload) { + while (payload->size >= sizeof(struct ofp_header)) { + const struct ofp_header *oh; + int length; + + /* Align OpenFlow on 8-byte boundary for safe access. */ + ofpbuf_shift(payload, -((intptr_t) payload->data & 7)); + + oh = payload->data; + length = ntohs(oh->length); + if (payload->size < length) { + break; + } + + if (!first) { + putchar('\n'); + } + first = false; + + if (timestamp) { + char *s = xastrftime_msec("%H:%M:%S.### ", when, true); + fputs(s, stdout); + free(s); + } + + printf(IP_FMT".%"PRIu16" > "IP_FMT".%"PRIu16":\n", + IP_ARGS(flow.nw_src), ntohs(flow.tp_src), + IP_ARGS(flow.nw_dst), ntohs(flow.tp_dst)); + ofp_print(stdout, payload->data, length, verbosity + 1); + ofpbuf_pull(payload, length); + } + } + } + ofpbuf_delete(packet); + } + tcp_reader_close(reader); +} + static void ofctl_ping(int argc, char *argv[]) { @@ -1835,7 +1922,7 @@ ofctl_ping(int argc, char *argv[]) payload = argc > 2 ? atoi(argv[2]) : 64; if (payload > max_payload) { - ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload); + ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload); } open_vconn(argv[1], &vconn); @@ -1863,7 +1950,7 @@ ofctl_ping(int argc, char *argv[]) printf("Reply:\n"); ofp_print(stdout, reply, reply->size, verbosity + 2); } - printf("%zu bytes from %s: xid=%08"PRIx32" time=%.1f ms\n", + printf("%"PRIuSIZE" bytes from %s: xid=%08"PRIx32" time=%.1f ms\n", reply->size, argv[1], ntohl(rpy_hdr->xid), (1000*(double)(end.tv_sec - start.tv_sec)) + (.001*(end.tv_usec - start.tv_usec))); @@ -1886,7 +1973,7 @@ ofctl_benchmark(int argc OVS_UNUSED, char *argv[]) payload_size = atoi(argv[2]); if (payload_size > max_payload) { - ovs_fatal(0, "payload must be between 0 and %zu bytes", max_payload); + ovs_fatal(0, "payload must be between 0 and %"PRIuSIZE" bytes", max_payload); } message_size = sizeof(struct ofp_header) + payload_size; @@ -2165,13 +2252,13 @@ fte_free_all(struct classifier *cls) struct cls_cursor cursor; struct fte *fte, *next; - ovs_rwlock_wrlock(&cls->rwlock); + fat_rwlock_wrlock(&cls->rwlock); cls_cursor_init(&cursor, cls, NULL); CLS_CURSOR_FOR_EACH_SAFE (fte, next, rule, &cursor) { classifier_remove(cls, &fte->rule); fte_free(fte); } - ovs_rwlock_unlock(&cls->rwlock); + fat_rwlock_unlock(&cls->rwlock); classifier_destroy(cls); } @@ -2190,9 +2277,9 @@ fte_insert(struct classifier *cls, const struct match *match, cls_rule_init(&fte->rule, match, priority); fte->versions[index] = version; - ovs_rwlock_wrlock(&cls->rwlock); + fat_rwlock_wrlock(&cls->rwlock); old = fte_from_cls_rule(classifier_replace(cls, &fte->rule)); - ovs_rwlock_unlock(&cls->rwlock); + fat_rwlock_unlock(&cls->rwlock); if (old) { fte_version_free(old->versions[index]); fte->versions[!index] = old->versions[!index]; @@ -2403,7 +2490,7 @@ ofctl_replace_flows(int argc OVS_UNUSED, char *argv[]) list_init(&requests); /* Delete flows that exist on the switch but not in the file. */ - ovs_rwlock_rdlock(&cls.rwlock); + fat_rwlock_rdlock(&cls.rwlock); cls_cursor_init(&cursor, &cls, NULL); CLS_CURSOR_FOR_EACH (fte, rule, &cursor) { struct fte_version *file_ver = fte->versions[FILE_IDX]; @@ -2427,7 +2514,7 @@ ofctl_replace_flows(int argc OVS_UNUSED, char *argv[]) fte_make_flow_mod(fte, FILE_IDX, OFPFC_ADD, protocol, &requests); } } - ovs_rwlock_unlock(&cls.rwlock); + fat_rwlock_unlock(&cls.rwlock); transact_multiple_noreply(vconn, &requests); vconn_close(vconn); @@ -2469,7 +2556,7 @@ ofctl_diff_flows(int argc OVS_UNUSED, char *argv[]) ds_init(&a_s); ds_init(&b_s); - ovs_rwlock_rdlock(&cls.rwlock); + fat_rwlock_rdlock(&cls.rwlock); cls_cursor_init(&cursor, &cls, NULL); CLS_CURSOR_FOR_EACH (fte, rule, &cursor) { struct fte_version *a = fte->versions[0]; @@ -2489,7 +2576,7 @@ ofctl_diff_flows(int argc OVS_UNUSED, char *argv[]) } } } - ovs_rwlock_unlock(&cls.rwlock); + fat_rwlock_unlock(&cls.rwlock); ds_destroy(&a_s); ds_destroy(&b_s); @@ -2766,15 +2853,15 @@ print_differences(const char *prefix, for (i = 0; i < MIN(a_len, b_len); i++) { if (a[i] != b[i]) { - printf("%s%2zu: %02"PRIx8" -> %02"PRIx8"\n", + printf("%s%2"PRIuSIZE": %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]); + printf("%s%2"PRIuSIZE": (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]); + printf("%s%2"PRIuSIZE": %02"PRIx8" -> (none)\n", prefix, i, a[i]); } } @@ -2875,7 +2962,7 @@ ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) ovs_fatal(0, "Trailing garbage in hex data"); } if (match_expout.size != sizeof(struct ofp10_match)) { - ovs_fatal(0, "Input is %zu bytes, expected %zu", + ovs_fatal(0, "Input is %"PRIuSIZE" bytes, expected %"PRIuSIZE, match_expout.size, sizeof(struct ofp10_match)); } @@ -2890,7 +2977,7 @@ ofctl_parse_ofp10_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) 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", + ovs_fatal(0, "Input is %"PRIuSIZE" bytes, expected %"PRIuSIZE, match_in.size, sizeof(struct ofp10_match)); } @@ -2939,7 +3026,7 @@ ofctl_parse_ofp11_match(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) 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", + ovs_fatal(0, "Input is %"PRIuSIZE" bytes, expected %"PRIuSIZE, match_in.size, sizeof(struct ofp11_match)); } @@ -3112,7 +3199,7 @@ ofctl_parse_pcap(int argc OVS_UNUSED, char *argv[]) { FILE *pcap; - pcap = pcap_open(argv[1], "rb"); + pcap = ovs_pcap_open(argv[1], "rb"); if (!pcap) { ovs_fatal(errno, "%s: open failed", argv[1]); } @@ -3122,7 +3209,7 @@ ofctl_parse_pcap(int argc OVS_UNUSED, char *argv[]) struct flow flow; int error; - error = pcap_read(pcap, &packet); + error = ovs_pcap_read(pcap, &packet, NULL); if (error == EOF) { break; } else if (error) { @@ -3365,11 +3452,13 @@ static const struct command all_commands[] = { { "mod-table", 3, 3, ofctl_mod_table }, { "get-frags", 1, 1, ofctl_get_frags }, { "set-frags", 2, 2, ofctl_set_frags }, - { "ofp-parse", 1, 1, ofctl_ofp_parse }, { "probe", 1, 1, ofctl_probe }, { "ping", 1, 2, ofctl_ping }, { "benchmark", 3, 3, ofctl_benchmark }, + { "ofp-parse", 1, 1, ofctl_ofp_parse }, + { "ofp-parse-pcap", 1, INT_MAX, ofctl_ofp_parse_pcap }, + { "add-group", 1, 2, ofctl_add_group }, { "add-groups", 1, 2, ofctl_add_groups }, { "mod-group", 1, 2, ofctl_mod_group },