1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
35 #include "ofp-print.h"
40 #include <netinet/in.h>
47 #include "dynamic-string.h"
54 static void ofp_print_port_name(struct ds *string, uint16_t port);
55 static void ofp_print_match(struct ds *, const struct ofp_match *,
58 /* Returns a string that represents the contents of the Ethernet frame in the
59 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
60 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
61 * bytes were captured).
63 * The caller must free the returned string.
65 * This starts and kills a tcpdump subprocess so it's quite expensive. */
67 ofp_packet_to_string(const void *data, size_t len, size_t total_len)
70 uint32_t magic_number; /* magic number */
71 uint16_t version_major; /* major version number */
72 uint16_t version_minor; /* minor version number */
73 int32_t thiszone; /* GMT to local correction */
74 uint32_t sigfigs; /* accuracy of timestamps */
75 uint32_t snaplen; /* max length of captured packets */
76 uint32_t network; /* data link type */
80 uint32_t ts_sec; /* timestamp seconds */
81 uint32_t ts_usec; /* timestamp microseconds */
82 uint32_t incl_len; /* number of octets of packet saved in file */
83 uint32_t orig_len; /* actual length of packet */
87 struct pcaprec_hdr prh;
89 struct ds ds = DS_EMPTY_INITIALIZER;
99 ofp_error(errno, "tmpfile");
100 return xstrdup("<error>");
103 /* The pcap reader is responsible for figuring out endianness based on the
104 * magic number, so the lack of htonX calls here is intentional. */
105 ph.magic_number = 0xa1b2c3d4;
106 ph.version_major = 2;
107 ph.version_minor = 4;
111 ph.network = 1; /* Ethernet */
116 prh.orig_len = total_len;
118 fwrite(&ph, 1, sizeof ph, pcap);
119 fwrite(&prh, 1, sizeof prh, pcap);
120 fwrite(data, 1, len, pcap);
124 ofp_error(errno, "error writing temporary file");
128 snprintf(command, sizeof command, "tcpdump -n -r /dev/fd/%d 2>/dev/null",
130 tcpdump = popen(command, "r");
133 ofp_error(errno, "exec(\"%s\")", command);
134 return xstrdup("<error>");
137 while ((c = getc(tcpdump)) != EOF) {
141 status = pclose(tcpdump);
142 if (WIFEXITED(status)) {
143 if (WEXITSTATUS(status))
144 ofp_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
145 } else if (WIFSIGNALED(status)) {
146 ofp_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
151 /* Pretty-print the OFPT_PACKET_IN packet of 'len' bytes at 'oh' to 'stream'
152 * at the given 'verbosity' level. */
154 ofp_packet_in(struct ds *string, const void *oh, size_t len, int verbosity)
156 const struct ofp_packet_in *op = oh;
159 ds_put_format(string, " total_len=%"PRIu16" in_port=",
160 ntohs(op->total_len));
161 ofp_print_port_name(string, ntohs(op->in_port));
163 if (op->reason == OFPR_ACTION)
164 ds_put_cstr(string, " (via action)");
165 else if (op->reason != OFPR_NO_MATCH)
166 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
168 data_len = len - offsetof(struct ofp_packet_in, data);
169 ds_put_format(string, " data_len=%zu", data_len);
170 if (htonl(op->buffer_id) == UINT32_MAX) {
171 ds_put_format(string, " (unbuffered)");
172 if (ntohs(op->total_len) != data_len)
173 ds_put_format(string, " (***total_len != data_len***)");
175 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
176 if (ntohs(op->total_len) < data_len)
177 ds_put_format(string, " (***total_len < data_len***)");
179 ds_put_char(string, '\n');
183 struct ofpbuf packet;
184 struct ofp_match match;
185 packet.data = (void *) op->data;
186 packet.size = data_len;
187 flow_extract(&packet, ntohs(op->in_port), &flow);
189 match.in_port = flow.in_port;
190 memcpy(match.dl_src, flow.dl_src, ETH_ADDR_LEN);
191 memcpy(match.dl_dst, flow.dl_dst, ETH_ADDR_LEN);
192 match.dl_vlan = flow.dl_vlan;
193 match.dl_type = flow.dl_type;
194 match.nw_proto = flow.nw_proto;
196 match.nw_src = flow.nw_src;
197 match.nw_dst = flow.nw_dst;
198 match.tp_src = flow.tp_src;
199 match.tp_dst = flow.tp_dst;
200 ofp_print_match(string, &match, verbosity);
201 ds_put_char(string, '\n');
204 char *packet = ofp_packet_to_string(op->data, data_len,
205 ntohs(op->total_len));
206 ds_put_cstr(string, packet);
211 static void ofp_print_port_name(struct ds *string, uint16_t port)
230 case OFPP_CONTROLLER:
240 ds_put_format(string, "%"PRIu16, port);
243 ds_put_cstr(string, name);
247 ofp_print_action(struct ds *string, const struct ofp_action *a)
249 switch (ntohs(a->type)) {
252 uint16_t port = ntohs(a->arg.output.port);
253 if (port < OFPP_MAX) {
254 ds_put_format(string, "output:%"PRIu16, port);
256 ofp_print_port_name(string, port);
257 if (port == OFPP_CONTROLLER) {
258 if (a->arg.output.max_len) {
259 ds_put_format(string, ":%"PRIu16,
260 ntohs(a->arg.output.max_len));
262 ds_put_cstr(string, ":all");
269 case OFPAT_SET_DL_VLAN:
270 ds_put_cstr(string, "mod_vlan:");
271 if (ntohs(a->arg.vlan_id) == OFP_VLAN_NONE) {
272 ds_put_cstr(string, "strip");
274 ds_put_format(string, "%"PRIu16, ntohs(a->arg.vlan_id));
278 case OFPAT_SET_DL_SRC:
279 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
280 ETH_ADDR_ARGS(a->arg.dl_addr));
283 case OFPAT_SET_DL_DST:
284 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
285 ETH_ADDR_ARGS(a->arg.dl_addr));
288 case OFPAT_SET_NW_SRC:
289 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&a->arg.nw_addr));
292 case OFPAT_SET_NW_DST:
293 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&a->arg.nw_addr));
296 case OFPAT_SET_TP_SRC:
297 ds_put_format(string, "mod_tp_src:%d", ntohs(a->arg.tp));
300 case OFPAT_SET_TP_DST:
301 ds_put_format(string, "mod_tp_dst:%d", ntohs(a->arg.tp));
305 ds_put_format(string, "(decoder %"PRIu16" not implemented)",
311 static void ofp_print_actions(struct ds *string,
312 const struct ofp_action actions[],
316 int n_actions = n_bytes / sizeof *actions;
318 ds_put_format(string, "action%s=", n_actions == 1 ? "" : "s");
319 for (i = 0; i < n_actions; i++) {
321 ds_put_cstr(string, ",");
323 ofp_print_action(string, &actions[i]);
325 if (n_bytes % sizeof *actions) {
327 ds_put_cstr(string, ",");
329 ds_put_cstr(string, ", ***trailing garbage***");
333 /* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'string'
334 * at the given 'verbosity' level. */
335 static void ofp_packet_out(struct ds *string, const void *oh, size_t len,
338 const struct ofp_packet_out *opo = oh;
339 int n_actions = ntohs(opo->n_actions);
340 int act_len = n_actions * sizeof opo->actions[0];
342 ds_put_cstr(string, " in_port=");
343 ofp_print_port_name(string, ntohs(opo->in_port));
345 ds_put_format(string, " n_actions=%d ", n_actions);
346 if (act_len > (ntohs(opo->header.length) - sizeof *opo)) {
347 ds_put_format(string, "***packet too short for number of actions***\n");
350 ofp_print_actions(string, opo->actions, act_len);
352 if (ntohl(opo->buffer_id) == UINT32_MAX) {
353 int data_len = len - sizeof *opo - act_len;
354 ds_put_format(string, " data_len=%d", data_len);
355 if (verbosity > 0 && len > sizeof *opo) {
356 char *packet = ofp_packet_to_string(&opo->actions[n_actions],
358 ds_put_char(string, '\n');
359 ds_put_cstr(string, packet);
363 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
365 ds_put_char(string, '\n');
368 /* qsort comparison function. */
370 compare_ports(const void *a_, const void *b_)
372 const struct ofp_phy_port *a = a_;
373 const struct ofp_phy_port *b = b_;
374 uint16_t ap = ntohs(a->port_no);
375 uint16_t bp = ntohs(b->port_no);
377 return ap < bp ? -1 : ap > bp;
381 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
383 uint8_t name[OFP_MAX_PORT_NAME_LEN];
386 memcpy(name, port->name, sizeof name);
387 for (j = 0; j < sizeof name - 1; j++) {
388 if (!isprint(name[j])) {
394 ds_put_char(string, ' ');
395 ofp_print_port_name(string, ntohs(port->port_no));
396 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", speed:%d, flags:%#x, "
398 ETH_ADDR_ARGS(port->hw_addr), ntohl(port->speed),
399 ntohl(port->flags), ntohl(port->features));
402 /* Pretty-print the struct ofp_switch_features of 'len' bytes at 'oh' to
403 * 'string' at the given 'verbosity' level. */
405 ofp_print_switch_features(struct ds *string, const void *oh, size_t len,
408 const struct ofp_switch_features *osf = oh;
409 struct ofp_phy_port port_list[OFPP_MAX];
413 ds_put_format(string, "dp id:%"PRIx64"\n", ntohll(osf->datapath_id));
414 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
415 ntohl(osf->n_buffers));
416 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
417 ntohl(osf->capabilities), ntohl(osf->actions));
419 if (ntohs(osf->header.length) >= sizeof *osf) {
420 len = MIN(len, ntohs(osf->header.length));
422 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
424 memcpy(port_list, osf->ports, (len - sizeof *osf));
425 qsort(port_list, n_ports, sizeof port_list[0], compare_ports);
426 for (i = 0; i < n_ports; i++) {
427 ofp_print_phy_port(string, &port_list[i]);
431 /* Pretty-print the struct ofp_switch_config of 'len' bytes at 'oh' to 'string'
432 * at the given 'verbosity' level. */
434 ofp_print_switch_config(struct ds *string, const void *oh, size_t len,
437 const struct ofp_switch_config *osc = oh;
440 flags = ntohs(osc->flags);
441 if (flags & OFPC_SEND_FLOW_EXP) {
442 flags &= ~OFPC_SEND_FLOW_EXP;
443 ds_put_format(string, " (sending flow expirations)");
446 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
449 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
452 static void print_wild(struct ds *string, const char *leader, int is_wild,
453 int verbosity, const char *format, ...)
454 __attribute__((format(printf, 5, 6)));
456 static void print_wild(struct ds *string, const char *leader, int is_wild,
457 int verbosity, const char *format, ...)
459 if (is_wild && verbosity < 2) {
462 ds_put_cstr(string, leader);
466 va_start(args, format);
467 ds_put_format_valist(string, format, args);
470 ds_put_char(string, '*');
472 ds_put_char(string, ',');
476 print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
477 uint32_t wild_bits, int verbosity)
479 if (wild_bits >= 32 && verbosity < 2) {
482 ds_put_cstr(string, leader);
483 if (wild_bits < 32) {
484 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
486 ds_put_format(string, "/%d", 32 - wild_bits);
489 ds_put_char(string, '*');
491 ds_put_char(string, ',');
494 /* Pretty-print the ofp_match structure */
495 static void ofp_print_match(struct ds *f, const struct ofp_match *om,
498 uint32_t w = ntohl(om->wildcards);
499 bool skip_type = false;
500 bool skip_proto = false;
502 if (!(w & OFPFW_DL_TYPE)) {
504 if (om->dl_type == htons(ETH_TYPE_IP)) {
505 if (!(w & OFPFW_NW_PROTO)) {
507 if (om->nw_proto == IP_TYPE_ICMP) {
508 ds_put_cstr(f, "icmp,");
509 } else if (om->nw_proto == IP_TYPE_TCP) {
510 ds_put_cstr(f, "tcp,");
511 } else if (om->nw_proto == IP_TYPE_UDP) {
512 ds_put_cstr(f, "udp,");
514 ds_put_cstr(f, "ip,");
518 ds_put_cstr(f, "ip,");
520 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
521 ds_put_cstr(f, "arp,");
526 print_wild(f, "in_port=", w & OFPFW_IN_PORT, verbosity,
527 "%d", ntohs(om->in_port));
528 print_wild(f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
529 "0x%04x", ntohs(om->dl_vlan));
530 print_wild(f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
531 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
532 print_wild(f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
533 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
535 print_wild(f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
536 "0x%04x", ntohs(om->dl_type));
538 print_ip_netmask(f, "nw_src=", om->nw_src,
539 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
540 print_ip_netmask(f, "nw_dst=", om->nw_dst,
541 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
543 print_wild(f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
546 print_wild(f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
547 "%d", ntohs(om->tp_src));
548 print_wild(f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
549 "%d", ntohs(om->tp_dst));
552 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
553 * at the given 'verbosity' level. */
555 ofp_print_flow_mod(struct ds *string, const void *oh, size_t len,
558 const struct ofp_flow_mod *ofm = oh;
560 ofp_print_match(string, &ofm->match, verbosity);
561 switch (ntohs(ofm->command)) {
563 ds_put_cstr(string, " ADD: ");
566 ds_put_cstr(string, " MOD: ");
569 ds_put_cstr(string, " DEL: ");
571 case OFPFC_DELETE_STRICT:
572 ds_put_cstr(string, " DEL_STRICT: ");
575 ds_put_format(string, " cmd:%d ", ntohs(ofm->command));
577 ds_put_format(string, "idle:%d hard:%d pri:%d buf:%#x",
578 ntohs(ofm->idle_timeout), ntohs(ofm->hard_timeout),
579 ofm->match.wildcards ? ntohs(ofm->priority) : (uint16_t)-1,
580 ntohl(ofm->buffer_id));
581 ofp_print_actions(string, ofm->actions,
582 len - offsetof(struct ofp_flow_mod, actions));
583 ds_put_char(string, '\n');
586 /* Pretty-print the OFPT_FLOW_EXPIRED packet of 'len' bytes at 'oh' to 'string'
587 * at the given 'verbosity' level. */
589 ofp_print_flow_expired(struct ds *string, const void *oh, size_t len,
592 const struct ofp_flow_expired *ofe = oh;
594 ofp_print_match(string, &ofe->match, verbosity);
595 ds_put_cstr(string, " reason=");
596 switch (ofe->reason) {
597 case OFPER_IDLE_TIMEOUT:
598 ds_put_cstr(string, "idle");
600 case OFPER_HARD_TIMEOUT:
601 ds_put_cstr(string, "hard");
604 ds_put_format(string, "**%"PRIu8"**", ofe->reason);
607 ds_put_format(string,
608 " pri%"PRIu16" secs%"PRIu32" pkts%"PRIu64" bytes%"PRIu64"\n",
609 ofe->match.wildcards ? ntohs(ofe->priority) : (uint16_t)-1,
610 ntohl(ofe->duration), ntohll(ofe->packet_count),
611 ntohll(ofe->byte_count));
620 static const struct error_type error_types[] = {
621 #define ERROR_TYPE(TYPE) {TYPE, -1, #TYPE}
622 #define ERROR_CODE(TYPE, CODE) {TYPE, CODE, #CODE}
623 ERROR_TYPE(OFPET_HELLO_FAILED),
624 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_INCOMPATIBLE),
626 ERROR_TYPE(OFPET_BAD_REQUEST),
627 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
628 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE),
629 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT),
630 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
632 #define N_ERROR_TYPES ARRAY_SIZE(error_types)
635 lookup_error_type(int type)
637 const struct error_type *t;
639 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
640 if (t->type == type && t->code == -1) {
648 lookup_error_code(int type, int code)
650 const struct error_type *t;
652 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
653 if (t->type == type && t->code == code) {
660 /* Pretty-print the OFPT_ERROR packet of 'len' bytes at 'oh' to 'string'
661 * at the given 'verbosity' level. */
663 ofp_print_error_msg(struct ds *string, const void *oh, size_t len,
666 const struct ofp_error_msg *oem = oh;
667 int type = ntohs(oem->type);
668 int code = ntohs(oem->code);
671 ds_put_format(string, " type%d(%s) code%d(%s) payload:\n",
672 type, lookup_error_type(type),
673 code, lookup_error_code(type, code));
676 case OFPET_HELLO_FAILED:
677 ds_put_printable(string, (char *) oem->data, len - sizeof *oem);
680 case OFPET_BAD_REQUEST:
681 s = ofp_to_string(oem->data, len - sizeof *oem, 1);
682 ds_put_cstr(string, s);
687 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
692 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'string'
693 * at the given 'verbosity' level. */
695 ofp_print_port_status(struct ds *string, const void *oh, size_t len,
698 const struct ofp_port_status *ops = oh;
700 if (ops->reason == OFPPR_ADD) {
701 ds_put_format(string, "add:");
702 } else if (ops->reason == OFPPR_DELETE) {
703 ds_put_format(string, "del:");
704 } else if (ops->reason == OFPPR_MOD) {
705 ds_put_format(string, "mod:");
707 ds_put_format(string, "err:");
710 ofp_print_phy_port(string, &ops->desc);
714 ofp_desc_stats_reply(struct ds *string, const void *body, size_t len,
717 const struct ofp_desc_stats *ods = body;
719 ds_put_format(string, "Manufacturer: %s\n", ods->mfr_desc);
720 ds_put_format(string, "Hardware: %s\n", ods->hw_desc);
721 ds_put_format(string, "Software: %s\n", ods->sw_desc);
722 ds_put_format(string, "Serial Num: %s\n", ods->serial_num);
726 ofp_flow_stats_request(struct ds *string, const void *oh, size_t len,
729 const struct ofp_flow_stats_request *fsr = oh;
731 if (fsr->table_id == 0xff) {
732 ds_put_format(string, " table_id=any, ");
734 ds_put_format(string, " table_id=%"PRIu8", ", fsr->table_id);
737 ofp_print_match(string, &fsr->match, verbosity);
741 ofp_flow_stats_reply(struct ds *string, const void *body_, size_t len,
744 const char *body = body_;
745 const char *pos = body;
747 const struct ofp_flow_stats *fs;
748 ptrdiff_t bytes_left = body + len - pos;
751 if (bytes_left < sizeof *fs) {
752 if (bytes_left != 0) {
753 ds_put_format(string, " ***%td leftover bytes at end***",
759 fs = (const void *) pos;
760 length = ntohs(fs->length);
761 if (length < sizeof *fs) {
762 ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
765 } else if (length > bytes_left) {
766 ds_put_format(string,
767 " ***length=%zu but only %td bytes left***",
770 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
771 ds_put_format(string,
772 " ***length=%zu has %zu bytes leftover in "
775 (length - sizeof *fs) % sizeof fs->actions[0]);
779 ds_put_format(string, " duration=%"PRIu32"s, ", ntohl(fs->duration));
780 ds_put_format(string, "table_id=%"PRIu8", ", fs->table_id);
781 ds_put_format(string, "priority=%"PRIu16", ",
782 fs->match.wildcards ? ntohs(fs->priority) : (uint16_t)-1);
783 ds_put_format(string, "n_packets=%"PRIu64", ",
784 ntohll(fs->packet_count));
785 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
786 ds_put_format(string, "idle_timeout=%"PRIu16",",
787 ntohs(fs->idle_timeout));
788 ds_put_format(string, "hard_timeout=%"PRIu16",",
789 ntohs(fs->hard_timeout));
790 ofp_print_match(string, &fs->match, verbosity);
791 ofp_print_actions(string, fs->actions, length - sizeof *fs);
792 ds_put_char(string, '\n');
799 ofp_aggregate_stats_request(struct ds *string, const void *oh, size_t len,
802 const struct ofp_aggregate_stats_request *asr = oh;
804 if (asr->table_id == 0xff) {
805 ds_put_format(string, " table_id=any, ");
807 ds_put_format(string, " table_id=%"PRIu8", ", asr->table_id);
810 ofp_print_match(string, &asr->match, verbosity);
814 ofp_aggregate_stats_reply(struct ds *string, const void *body_, size_t len,
817 const struct ofp_aggregate_stats_reply *asr = body_;
819 ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
820 ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
821 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
824 static void print_port_stat(struct ds *string, const char *leader,
825 uint64_t stat, int more)
827 ds_put_cstr(string, leader);
829 ds_put_format(string, "%"PRIu64, stat);
831 ds_put_char(string, '?');
834 ds_put_cstr(string, ", ");
836 ds_put_cstr(string, "\n");
841 ofp_port_stats_reply(struct ds *string, const void *body, size_t len,
844 const struct ofp_port_stats *ps = body;
845 size_t n = len / sizeof *ps;
846 ds_put_format(string, " %zu ports\n", n);
852 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
854 ds_put_cstr(string, "rx ");
855 print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
856 print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
857 print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
858 print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
859 print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
860 print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
861 print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
863 ds_put_cstr(string, " tx ");
864 print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
865 print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
866 print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
867 print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
868 print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
873 ofp_table_stats_reply(struct ds *string, const void *body, size_t len,
876 const struct ofp_table_stats *ts = body;
877 size_t n = len / sizeof *ts;
878 ds_put_format(string, " %zu tables\n", n);
884 char name[OFP_MAX_TABLE_NAME_LEN + 1];
885 strncpy(name, ts->name, sizeof name);
886 name[OFP_MAX_TABLE_NAME_LEN] = '\0';
888 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
889 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
890 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
891 ds_put_format(string, "active=%6"PRIu32", ", ntohl(ts->active_count));
892 ds_put_format(string, "matched=%6"PRIu64"\n",
893 ntohll(ts->matched_count));
898 vendor_stat(struct ds *string, const void *body, size_t len,
899 int verbosity UNUSED)
901 ds_put_format(string, " vendor=%08"PRIx32, ntohl(*(uint32_t *) body));
902 ds_put_format(string, " %zu bytes additional data",
903 len - sizeof(uint32_t));
906 enum stats_direction {
912 print_stats(struct ds *string, int type, const void *body, size_t body_len,
913 int verbosity, enum stats_direction direction)
916 size_t min_body, max_body;
917 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
923 struct stats_msg request;
924 struct stats_msg reply;
927 static const struct stats_type stats_types[] = {
932 { 0, SIZE_MAX, ofp_desc_stats_reply },
937 { sizeof(struct ofp_flow_stats_request),
938 sizeof(struct ofp_flow_stats_request),
939 ofp_flow_stats_request },
940 { 0, SIZE_MAX, ofp_flow_stats_reply },
945 { sizeof(struct ofp_aggregate_stats_request),
946 sizeof(struct ofp_aggregate_stats_request),
947 ofp_aggregate_stats_request },
948 { sizeof(struct ofp_aggregate_stats_reply),
949 sizeof(struct ofp_aggregate_stats_reply),
950 ofp_aggregate_stats_reply },
956 { 0, SIZE_MAX, ofp_table_stats_reply },
962 { 0, SIZE_MAX, ofp_port_stats_reply },
967 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
968 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
978 const struct stats_type *s;
979 const struct stats_msg *m;
981 if (type >= ARRAY_SIZE(stats_types) || !stats_types[type].name) {
982 ds_put_format(string, " ***unknown type %d***", type);
985 for (s = stats_types; s->type >= 0; s++) {
986 if (s->type == type) {
990 ds_put_format(string, " type=%d(%s)\n", type, s->name);
992 m = direction == REQUEST ? &s->request : &s->reply;
993 if (body_len < m->min_body || body_len > m->max_body) {
994 ds_put_format(string, " ***body_len=%zu not in %zu...%zu***",
995 body_len, m->min_body, m->max_body);
999 m->printer(string, body, body_len, verbosity);
1004 ofp_stats_request(struct ds *string, const void *oh, size_t len, int verbosity)
1006 const struct ofp_stats_request *srq = oh;
1009 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1013 print_stats(string, ntohs(srq->type), srq->body,
1014 len - offsetof(struct ofp_stats_request, body),
1015 verbosity, REQUEST);
1019 ofp_stats_reply(struct ds *string, const void *oh, size_t len, int verbosity)
1021 const struct ofp_stats_reply *srp = oh;
1023 ds_put_cstr(string, " flags=");
1025 ds_put_cstr(string, "none");
1027 uint16_t flags = ntohs(srp->flags);
1028 if (flags & OFPSF_REPLY_MORE) {
1029 ds_put_cstr(string, "[more]");
1030 flags &= ~OFPSF_REPLY_MORE;
1033 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]", flags);
1037 print_stats(string, ntohs(srp->type), srp->body,
1038 len - offsetof(struct ofp_stats_reply, body),
1043 ofp_echo(struct ds *string, const void *oh, size_t len, int verbosity)
1045 const struct ofp_header *hdr = oh;
1047 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *hdr);
1048 if (verbosity > 1) {
1049 ds_put_hex_dump(string, hdr, len - sizeof *hdr, 0, true);
1053 struct openflow_packet {
1057 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1060 static const struct openflow_packet packets[] = {
1064 sizeof (struct ofp_header),
1068 OFPT_FEATURES_REQUEST,
1070 sizeof (struct ofp_header),
1074 OFPT_FEATURES_REPLY,
1076 sizeof (struct ofp_switch_features),
1077 ofp_print_switch_features,
1080 OFPT_GET_CONFIG_REQUEST,
1081 "get_config_request",
1082 sizeof (struct ofp_header),
1086 OFPT_GET_CONFIG_REPLY,
1088 sizeof (struct ofp_switch_config),
1089 ofp_print_switch_config,
1094 sizeof (struct ofp_switch_config),
1095 ofp_print_switch_config,
1100 offsetof(struct ofp_packet_in, data),
1106 sizeof (struct ofp_packet_out),
1112 sizeof (struct ofp_flow_mod),
1118 sizeof (struct ofp_flow_expired),
1119 ofp_print_flow_expired,
1124 sizeof (struct ofp_port_mod),
1130 sizeof (struct ofp_port_status),
1131 ofp_print_port_status
1136 sizeof (struct ofp_error_msg),
1137 ofp_print_error_msg,
1142 sizeof (struct ofp_stats_request),
1148 sizeof (struct ofp_stats_reply),
1154 sizeof (struct ofp_header),
1160 sizeof (struct ofp_header),
1165 /* Composes and returns a string representing the OpenFlow packet of 'len'
1166 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1167 * verbosity and higher numbers increase verbosity. The caller is responsible
1168 * for freeing the string. */
1170 ofp_to_string(const void *oh_, size_t len, int verbosity)
1172 struct ds string = DS_EMPTY_INITIALIZER;
1173 const struct ofp_header *oh = oh_;
1174 const struct openflow_packet *pkt;
1176 if (len < sizeof(struct ofp_header)) {
1177 ds_put_cstr(&string, "OpenFlow packet too short:\n");
1178 ds_put_hex_dump(&string, oh, len, 0, true);
1179 return ds_cstr(&string);
1180 } else if (oh->version != OFP_VERSION) {
1181 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", oh->version);
1182 ds_put_hex_dump(&string, oh, len, 0, true);
1183 return ds_cstr(&string);
1186 for (pkt = packets; ; pkt++) {
1187 if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1188 ds_put_format(&string, "Unknown OpenFlow packet type %"PRIu8":\n",
1190 ds_put_hex_dump(&string, oh, len, 0, true);
1191 return ds_cstr(&string);
1192 } else if (oh->type == pkt->type) {
1197 ds_put_format(&string, "%s (xid=0x%"PRIx32"):", pkt->name, oh->xid);
1199 if (ntohs(oh->length) > len)
1200 ds_put_format(&string, " (***truncated to %zu bytes from %"PRIu16"***)",
1201 len, ntohs(oh->length));
1202 else if (ntohs(oh->length) < len) {
1203 ds_put_format(&string, " (***only uses %"PRIu16" bytes out of %zu***)\n",
1204 ntohs(oh->length), len);
1205 len = ntohs(oh->length);
1208 if (len < pkt->min_size) {
1209 ds_put_format(&string, " (***length=%zu < min_size=%zu***)\n",
1210 len, pkt->min_size);
1211 } else if (!pkt->printer) {
1212 if (len > sizeof *oh) {
1213 ds_put_format(&string, " length=%"PRIu16" (decoder not implemented)\n",
1217 pkt->printer(&string, oh, len, verbosity);
1219 if (verbosity >= 3) {
1220 ds_put_hex_dump(&string, oh, len, 0, true);
1222 if (string.string[string.length - 1] != '\n') {
1223 ds_put_char(&string, '\n');
1225 return ds_cstr(&string);
1229 print_and_free(FILE *stream, char *string)
1231 fputs(string, stream);
1235 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1236 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1237 * numbers increase verbosity. */
1239 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1241 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1244 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1245 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1246 * the Ethernet frame (of which 'len' bytes were captured).
1248 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1250 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1252 print_and_free(stream, ofp_packet_to_string(data, len, total_len));