X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=lib%2Fofp-print.c;h=8df439de1af4124e4664e6dd48b74bb386c4be61;hb=2953097759d3baaf22223b876d0028be8df78d3d;hp=6278395ddcbf0aababe3c2f15f8c92c7dd88f746;hpb=848e88098fec85336b89c0c652c1d91577c87b11;p=openvswitch diff --git a/lib/ofp-print.c b/lib/ofp-print.c index 6278395d..8df439de 100644 --- a/lib/ofp-print.c +++ b/lib/ofp-print.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks. + * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,7 +33,9 @@ #include "flow.h" #include "learn.h" #include "multipath.h" +#include "meta-flow.h" #include "nx-match.h" +#include "ofp-errors.h" #include "ofp-util.h" #include "ofpbuf.h" #include "openflow/openflow.h" @@ -45,108 +47,117 @@ #include "util.h" static void ofp_print_queue_name(struct ds *string, uint32_t port); -static void ofp_print_error(struct ds *, int error); +static void ofp_print_error(struct ds *, enum ofperr); /* Returns a string that represents the contents of the Ethernet frame in the - * 'len' bytes starting at 'data' to 'stream' as output by tcpdump. - * 'total_len' specifies the full length of the Ethernet frame (of which 'len' - * bytes were captured). - * - * The caller must free the returned string. - * - * This starts and kills a tcpdump subprocess so it's quite expensive. */ + * 'len' bytes starting at 'data'. The caller must free the returned string.*/ char * -ofp_packet_to_string(const void *data, size_t len, size_t total_len OVS_UNUSED) +ofp_packet_to_string(const void *data, size_t len) { struct ds ds = DS_EMPTY_INITIALIZER; struct ofpbuf buf; - - char command[128]; - FILE *pcap; - FILE *tcpdump; - int status; - int c; + struct flow flow; ofpbuf_use_const(&buf, data, len); - - pcap = tmpfile(); - if (!pcap) { - ovs_error(errno, "tmpfile"); - return xstrdup(""); - } - pcap_write_header(pcap); - pcap_write(pcap, &buf); - fflush(pcap); - if (ferror(pcap)) { - ovs_error(errno, "error writing temporary file"); + flow_extract(&buf, 0, 0, 0, &flow); + flow_format(&ds, &flow); + + if (buf.l7) { + if (flow.nw_proto == IPPROTO_TCP) { + struct tcp_header *th = buf.l4; + ds_put_format(&ds, " tcp_csum:%"PRIx16, + ntohs(th->tcp_csum)); + } else if (flow.nw_proto == IPPROTO_UDP) { + struct udp_header *uh = buf.l4; + ds_put_format(&ds, " udp_csum:%"PRIx16, + ntohs(uh->udp_csum)); + } } - rewind(pcap); - snprintf(command, sizeof command, "/usr/sbin/tcpdump -t -e -n -r /dev/fd/%d 2>/dev/null", - fileno(pcap)); - tcpdump = popen(command, "r"); - fclose(pcap); - if (!tcpdump) { - ovs_error(errno, "exec(\"%s\")", command); - return xstrdup(""); - } + ds_put_char(&ds, '\n'); - while ((c = getc(tcpdump)) != EOF) { - ds_put_char(&ds, c); - } + return ds_cstr(&ds); +} - status = pclose(tcpdump); - if (WIFEXITED(status)) { - if (WEXITSTATUS(status)) - ovs_error(0, "tcpdump exited with status %d", WEXITSTATUS(status)); - } else if (WIFSIGNALED(status)) { - ovs_error(0, "tcpdump exited with signal %d", WTERMSIG(status)); +static const char * +ofp_packet_in_reason_to_string(enum ofp_packet_in_reason reason) +{ + static char s[32]; + + switch (reason) { + case OFPR_NO_MATCH: + return "no_match"; + case OFPR_ACTION: + return "action"; + case OFPR_INVALID_TTL: + return "invalid_ttl"; + default: + sprintf(s, "%d", (int) reason); + return s; } - return ds_cstr(&ds); } static void -ofp_print_packet_in(struct ds *string, const struct ofp_packet_in *op, +ofp_print_packet_in(struct ds *string, const struct ofp_header *oh, int verbosity) { - size_t len = ntohs(op->header.length); - size_t data_len; + struct ofputil_packet_in pin; + int error; + int i; - ds_put_format(string, " total_len=%"PRIu16" in_port=", - ntohs(op->total_len)); - ofputil_format_port(ntohs(op->in_port), string); + error = ofputil_decode_packet_in(&pin, oh); + if (error) { + ofp_print_error(string, error); + return; + } + + if (pin.table_id) { + ds_put_format(string, " table_id=%"PRIu8, pin.table_id); + } + + if (pin.cookie) { + ds_put_format(string, " cookie=0x%"PRIx64, ntohll(pin.cookie)); + } - if (op->reason == OFPR_ACTION) - ds_put_cstr(string, " (via action)"); - else if (op->reason != OFPR_NO_MATCH) - ds_put_format(string, " (***reason %"PRIu8"***)", op->reason); + ds_put_format(string, " total_len=%"PRIu16" in_port=", pin.total_len); + ofputil_format_port(pin.fmd.in_port, string); - data_len = len - offsetof(struct ofp_packet_in, data); - ds_put_format(string, " data_len=%zu", data_len); - if (op->buffer_id == htonl(UINT32_MAX)) { + if (pin.fmd.tun_id_mask) { + ds_put_format(string, " tun_id=0x%"PRIx64, ntohll(pin.fmd.tun_id)); + if (pin.fmd.tun_id_mask != htonll(UINT64_MAX)) { + ds_put_format(string, "/0x%"PRIx64, ntohll(pin.fmd.tun_id_mask)); + } + } + + for (i = 0; i < FLOW_N_REGS; i++) { + if (pin.fmd.reg_masks[i]) { + ds_put_format(string, " reg%d=0x%"PRIx32, i, pin.fmd.regs[i]); + if (pin.fmd.reg_masks[i] != UINT32_MAX) { + ds_put_format(string, "/0x%"PRIx32, pin.fmd.reg_masks[i]); + } + } + } + + ds_put_format(string, " (via %s)", + ofp_packet_in_reason_to_string(pin.reason)); + + ds_put_format(string, " data_len=%zu", pin.packet_len); + if (pin.buffer_id == UINT32_MAX) { ds_put_format(string, " (unbuffered)"); - if (ntohs(op->total_len) != data_len) + if (pin.total_len != pin.packet_len) { ds_put_format(string, " (***total_len != data_len***)"); + } } else { - ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id)); - if (ntohs(op->total_len) < data_len) + ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id); + if (pin.total_len < pin.packet_len) { ds_put_format(string, " (***total_len < data_len***)"); + } } ds_put_char(string, '\n'); if (verbosity > 0) { - struct flow flow; - struct ofpbuf packet; - - ofpbuf_use_const(&packet, op->data, data_len); - flow_extract(&packet, 0, ntohs(op->in_port), &flow); - flow_format(string, &flow); - ds_put_char(string, '\n'); - } - if (verbosity > 1) { - char *packet = ofp_packet_to_string(op->data, data_len, - ntohs(op->total_len)); + char *packet = ofp_packet_to_string(pin.packet, pin.packet_len); ds_put_cstr(string, packet); free(packet); } @@ -183,6 +194,7 @@ ofp_print_action(struct ds *s, const union ofp_action *a, const struct nx_action_multipath *nam; const struct nx_action_autopath *naa; const struct nx_action_output_reg *naor; + struct mf_subfield subfield; uint16_t port; switch (code) { @@ -315,9 +327,8 @@ ofp_print_action(struct ds *s, const union ofp_action *a, case OFPUTIL_NXAST_AUTOPATH: naa = (const struct nx_action_autopath *)a; ds_put_format(s, "autopath(%u,", ntohl(naa->id)); - nxm_format_field_bits(s, ntohl(naa->dst), - nxm_decode_ofs(naa->ofs_nbits), - nxm_decode_n_bits(naa->ofs_nbits)); + nxm_decode(&subfield, naa->dst, naa->ofs_nbits); + mf_format_subfield(&subfield, s); ds_put_char(s, ')'); break; @@ -329,15 +340,18 @@ ofp_print_action(struct ds *s, const union ofp_action *a, case OFPUTIL_NXAST_OUTPUT_REG: naor = (const struct nx_action_output_reg *) a; ds_put_cstr(s, "output:"); - nxm_format_field_bits(s, ntohl(naor->src), - nxm_decode_ofs(naor->ofs_nbits), - nxm_decode_n_bits(naor->ofs_nbits)); + nxm_decode(&subfield, naor->src, naor->ofs_nbits); + mf_format_subfield(&subfield, s); break; case OFPUTIL_NXAST_LEARN: learn_format((const struct nx_action_learn *) a, s); break; + case OFPUTIL_NXAST_DEC_TTL: + ds_put_cstr(s, "dec_ttl"); + break; + case OFPUTIL_NXAST_EXIT: ds_put_cstr(s, "exit"); break; @@ -380,36 +394,31 @@ static void ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo, int verbosity) { - size_t len = ntohs(opo->header.length); - size_t actions_len = ntohs(opo->actions_len); + struct ofputil_packet_out po; + enum ofperr error; - ds_put_cstr(string, " in_port="); - ofputil_format_port(ntohs(opo->in_port), string); - - ds_put_format(string, " actions_len=%zu ", actions_len); - if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) { - ds_put_format(string, "***packet too short for action length***\n"); + error = ofputil_decode_packet_out(&po, opo); + if (error) { + ofp_print_error(string, error); return; } - if (actions_len % sizeof(union ofp_action)) { - ds_put_format(string, "***action length not a multiple of %zu***\n", - sizeof(union ofp_action)); - } - ofp_print_actions(string, (const union ofp_action *) opo->actions, - actions_len / sizeof(union ofp_action)); - if (ntohl(opo->buffer_id) == UINT32_MAX) { - int data_len = len - sizeof *opo - actions_len; - ds_put_format(string, " data_len=%d", data_len); - if (verbosity > 0 && len > sizeof *opo) { - char *packet = ofp_packet_to_string( - (uint8_t *)opo->actions + actions_len, data_len, data_len); + ds_put_cstr(string, " in_port="); + ofputil_format_port(po.in_port, string); + + ds_put_char(string, ' '); + ofp_print_actions(string, po.actions, po.n_actions); + + if (po.buffer_id == UINT32_MAX) { + ds_put_format(string, " data_len=%zu", po.packet_len); + if (verbosity > 0 && po.packet_len > 0) { + char *packet = ofp_packet_to_string(po.packet, po.packet_len); ds_put_char(string, '\n'); ds_put_cstr(string, packet); free(packet); } } else { - ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id)); + ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id); } ds_put_char(string, '\n'); } @@ -604,13 +613,18 @@ ofp_print_switch_features(struct ds *string, static void ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc) { - uint16_t flags; + enum ofp_config_flags flags; flags = ntohs(osc->flags); ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags)); flags &= ~OFPC_FRAG_MASK; + if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) { + ds_put_format(string, " invalid_ttl_to_controller"); + flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER; + } + if (flags) { ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags); } @@ -752,7 +766,7 @@ ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh, { struct ofputil_flow_mod fm; bool need_priority; - int error; + enum ofperr error; error = ofputil_decode_flow_mod(&fm, oh, true); if (error) { @@ -828,7 +842,22 @@ ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh, ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id); } if (fm.flags != 0) { - ds_put_format(s, "flags:0x%"PRIx16" ", fm.flags); + uint16_t flags = fm.flags; + + if (flags & OFPFF_SEND_FLOW_REM) { + ds_put_cstr(s, "send_flow_rem "); + } + if (flags & OFPFF_CHECK_OVERLAP) { + ds_put_cstr(s, "check_overlap "); + } + if (flags & OFPFF_EMERG) { + ds_put_cstr(s, "emerg "); + } + + flags &= ~(OFPFF_SEND_FLOW_REM | OFPFF_CHECK_OVERLAP | OFPFF_EMERG); + if (flags) { + ds_put_format(s, "flags:0x%"PRIx16" ", flags); + } } ofp_print_actions(s, fm.actions, fm.n_actions); @@ -847,11 +876,29 @@ ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec) ds_put_char(string, 's'); } +static const char * +ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason) +{ + static char s[32]; + + switch (reason) { + case OFPRR_IDLE_TIMEOUT: + return "idle"; + case OFPRR_HARD_TIMEOUT: + return "hard"; + case OFPRR_DELETE: + return "delete"; + default: + sprintf(s, "%d", (int) reason); + return s; + } +} + static void ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh) { struct ofputil_flow_removed fr; - int error; + enum ofperr error; error = ofputil_decode_flow_removed(&fr, oh); if (error) { @@ -862,21 +909,8 @@ ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh) ds_put_char(string, ' '); cls_rule_format(&fr.rule, string); - ds_put_cstr(string, " reason="); - switch (fr.reason) { - case OFPRR_IDLE_TIMEOUT: - ds_put_cstr(string, "idle"); - break; - case OFPRR_HARD_TIMEOUT: - ds_put_cstr(string, "hard"); - break; - case OFPRR_DELETE: - ds_put_cstr(string, "delete"); - break; - default: - ds_put_format(string, "**%"PRIu8"**", fr.reason); - break; - } + ds_put_format(string, " reason=%s", + ofp_flow_removed_reason_to_string(fr.reason)); if (fr.cookie != htonll(0)) { ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie)); @@ -902,14 +936,12 @@ ofp_print_port_mod(struct ds *string, const struct ofp_port_mod *opm) } static void -ofp_print_error(struct ds *string, int error) +ofp_print_error(struct ds *string, enum ofperr error) { if (string->length) { ds_put_char(string, ' '); } - ds_put_cstr(string, "***decode error: "); - ofputil_format_error(string, error); - ds_put_cstr(string, "***\n"); + ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error)); } static void @@ -918,32 +950,26 @@ ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem) size_t len = ntohs(oem->header.length); size_t payload_ofs, payload_len; const void *payload; - int error; + enum ofperr error; char *s; - error = ofputil_decode_error_msg(&oem->header, &payload_ofs); - if (!is_ofp_error(error)) { - ofp_print_error(string, error); + error = ofperr_decode_msg(&oem->header, &payload_ofs); + if (!error) { + ds_put_cstr(string, "***decode error***"); ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true); return; } - ds_put_char(string, ' '); - ofputil_format_error(string, error); - ds_put_char(string, '\n'); + ds_put_format(string, " %s\n", ofperr_get_name(error)); payload = (const uint8_t *) oem + payload_ofs; payload_len = len - payload_ofs; - switch (get_ofp_err_type(error)) { - case OFPET_HELLO_FAILED: + if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) { ds_put_printable(string, payload, payload_len); - break; - - default: + } else { s = ofp_to_string(payload, payload_len, 1); ds_put_cstr(string, s); free(s); - break; } } @@ -982,7 +1008,7 @@ ofp_print_flow_stats_request(struct ds *string, const struct ofp_stats_msg *osm) { struct ofputil_flow_stats_request fsr; - int error; + enum ofperr error; error = ofputil_decode_flow_stats_request(&fsr, &osm->header); if (error) { @@ -1017,7 +1043,7 @@ ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh) struct ofputil_flow_stats fs; int retval; - retval = ofputil_decode_flow_stats_reply(&fs, &b); + retval = ofputil_decode_flow_stats_reply(&fs, &b, true); if (retval) { if (retval != EOF) { ds_put_cstr(string, " ***parse error***"); @@ -1039,6 +1065,12 @@ ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh) if (fs.hard_timeout != OFP_FLOW_PERMANENT) { ds_put_format(string, "hard_timeout=%"PRIu16",", fs.hard_timeout); } + if (fs.idle_age >= 0) { + ds_put_format(string, "idle_age=%d,", fs.idle_age); + } + if (fs.hard_age >= 0 && fs.hard_age != fs.duration_sec) { + ds_put_format(string, "hard_age=%d,", fs.hard_age); + } cls_rule_format(&fs.rule, string); if (string->string[string->length - 1] != ' ') { @@ -1259,14 +1291,14 @@ ofp_print_nxt_role_message(struct ds *string, static void ofp_print_nxt_flow_mod_table_id(struct ds *string, - const struct nxt_flow_mod_table_id *nfmti) + const struct nx_flow_mod_table_id *nfmti) { ds_put_format(string, " %s", nfmti->set ? "enable" : "disable"); } static void ofp_print_nxt_set_flow_format(struct ds *string, - const struct nxt_set_flow_format *nsff) + const struct nx_set_flow_format *nsff) { uint32_t format = ntohl(nsff->format); @@ -1278,6 +1310,89 @@ ofp_print_nxt_set_flow_format(struct ds *string, } } +static void +ofp_print_nxt_set_packet_in_format(struct ds *string, + const struct nx_set_packet_in_format *nspf) +{ + uint32_t format = ntohl(nspf->format); + + ds_put_cstr(string, " format="); + if (ofputil_packet_in_format_is_valid(format)) { + ds_put_cstr(string, ofputil_packet_in_format_to_string(format)); + } else { + ds_put_format(string, "%"PRIu32, format); + } +} + +static const char * +ofp_port_reason_to_string(enum ofp_port_reason reason) +{ + static char s[32]; + + switch (reason) { + case OFPPR_ADD: + return "add"; + + case OFPPR_DELETE: + return "delete"; + + case OFPPR_MODIFY: + return "modify"; + + default: + sprintf(s, "%d", (int) reason); + return s; + } +} + +static void +ofp_print_nxt_set_async_config(struct ds *string, + const struct nx_async_config *nac) +{ + int i; + + for (i = 0; i < 2; i++) { + int j; + + ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave"); + + ds_put_cstr(string, " PACKET_IN:"); + for (j = 0; j < 32; j++) { + if (nac->packet_in_mask[i] & htonl(1u << j)) { + ds_put_format(string, " %s", + ofp_packet_in_reason_to_string(j)); + } + } + if (!nac->packet_in_mask[i]) { + ds_put_cstr(string, " (off)"); + } + ds_put_char(string, '\n'); + + ds_put_cstr(string, " PORT_STATUS:"); + for (j = 0; j < 32; j++) { + if (nac->port_status_mask[i] & htonl(1u << j)) { + ds_put_format(string, " %s", ofp_port_reason_to_string(j)); + } + } + if (!nac->port_status_mask[i]) { + ds_put_cstr(string, " (off)"); + } + ds_put_char(string, '\n'); + + ds_put_cstr(string, " FLOW_REMOVED:"); + for (j = 0; j < 32; j++) { + if (nac->flow_removed_mask[i] & htonl(1u << j)) { + ds_put_format(string, " %s", + ofp_flow_removed_reason_to_string(j)); + } + } + if (!nac->flow_removed_mask[i]) { + ds_put_cstr(string, " (off)"); + } + ds_put_char(string, '\n'); + } +} + static void ofp_to_string__(const struct ofp_header *oh, const struct ofputil_msg_type *type, struct ds *string, @@ -1325,6 +1440,7 @@ ofp_to_string__(const struct ofp_header *oh, break; case OFPUTIL_OFPT_PACKET_IN: + case OFPUTIL_NXT_PACKET_IN: ofp_print_packet_in(string, msg, verbosity); break; @@ -1342,6 +1458,7 @@ ofp_to_string__(const struct ofp_header *oh, break; case OFPUTIL_OFPT_FLOW_MOD: + case OFPUTIL_NXT_FLOW_MOD: ofp_print_flow_mod(string, msg, code, verbosity); break; @@ -1428,8 +1545,15 @@ ofp_to_string__(const struct ofp_header *oh, ofp_print_nxt_set_flow_format(string, msg); break; - case OFPUTIL_NXT_FLOW_MOD: - ofp_print_flow_mod(string, msg, code, verbosity); + case OFPUTIL_NXT_SET_PACKET_IN_FORMAT: + ofp_print_nxt_set_packet_in_format(string, msg); + break; + + case OFPUTIL_NXT_FLOW_AGE: + break; + + case OFPUTIL_NXT_SET_ASYNC_CONFIG: + ofp_print_nxt_set_async_config(string, msg); break; case OFPUTIL_NXST_AGGREGATE_REPLY: @@ -1454,9 +1578,6 @@ ofp_to_string(const void *oh_, size_t len, int verbosity) } else if (len < sizeof(struct ofp_header)) { ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n", len); - } else if (oh->version != OFP_VERSION) { - ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", - oh->version); } else if (ntohs(oh->length) > len) { ds_put_format(&string, "(***truncated to %zu bytes from %"PRIu16"***)\n", @@ -1467,7 +1588,7 @@ ofp_to_string(const void *oh_, size_t len, int verbosity) ntohs(oh->length), len); } else { const struct ofputil_msg_type *type; - int error; + enum ofperr error; error = ofputil_decode_msg_type(oh, &type); if (!error) { @@ -1592,12 +1713,9 @@ ofp_print(FILE *stream, const void *oh, size_t len, int verbosity) } /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at - * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of - * the Ethernet frame (of which 'len' bytes were captured). - * - * This starts and kills a tcpdump subprocess so it's quite expensive. */ + * 'data' to 'stream'. */ void -ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len) +ofp_print_packet(FILE *stream, const void *data, size_t len) { - print_and_free(stream, ofp_packet_to_string(data, len, total_len)); + print_and_free(stream, ofp_packet_to_string(data, len)); }