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"
52 static void ofp_print_port_name(struct ds *string, uint16_t port);
54 /* Returns a string that represents the contents of the Ethernet frame in the
55 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
56 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
57 * bytes were captured).
59 * The caller must free the returned string.
61 * This starts and kills a tcpdump subprocess so it's quite expensive. */
63 ofp_packet_to_string(const void *data, size_t len, size_t total_len)
66 uint32_t magic_number; /* magic number */
67 uint16_t version_major; /* major version number */
68 uint16_t version_minor; /* minor version number */
69 int32_t thiszone; /* GMT to local correction */
70 uint32_t sigfigs; /* accuracy of timestamps */
71 uint32_t snaplen; /* max length of captured packets */
72 uint32_t network; /* data link type */
76 uint32_t ts_sec; /* timestamp seconds */
77 uint32_t ts_usec; /* timestamp microseconds */
78 uint32_t incl_len; /* number of octets of packet saved in file */
79 uint32_t orig_len; /* actual length of packet */
83 struct pcaprec_hdr prh;
85 struct ds ds = DS_EMPTY_INITIALIZER;
95 error(errno, "tmpfile");
96 return xstrdup("<error>");
99 /* The pcap reader is responsible for figuring out endianness based on the
100 * magic number, so the lack of htonX calls here is intentional. */
101 ph.magic_number = 0xa1b2c3d4;
102 ph.version_major = 2;
103 ph.version_minor = 4;
107 ph.network = 1; /* Ethernet */
112 prh.orig_len = total_len;
114 fwrite(&ph, 1, sizeof ph, pcap);
115 fwrite(&prh, 1, sizeof prh, pcap);
116 fwrite(data, 1, len, pcap);
120 error(errno, "error writing temporary file");
124 snprintf(command, sizeof command, "tcpdump -n -r /dev/fd/%d 2>/dev/null",
126 tcpdump = popen(command, "r");
129 error(errno, "exec(\"%s\")", command);
130 return xstrdup("<error>");
133 while ((c = getc(tcpdump)) != EOF) {
137 status = pclose(tcpdump);
138 if (WIFEXITED(status)) {
139 if (WEXITSTATUS(status))
140 error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
141 } else if (WIFSIGNALED(status)) {
142 error(0, "tcpdump exited with signal %d", WTERMSIG(status));
147 /* Pretty-print the OFPT_PACKET_IN packet of 'len' bytes at 'oh' to 'stream'
148 * at the given 'verbosity' level. */
150 ofp_packet_in(struct ds *string, const void *oh, size_t len, int verbosity)
152 const struct ofp_packet_in *op = oh;
155 ds_put_format(string, " total_len=%"PRIu16" in_port=",
156 ntohs(op->total_len));
157 ofp_print_port_name(string, ntohs(op->in_port));
159 if (op->reason == OFPR_ACTION)
160 ds_put_cstr(string, " (via action)");
161 else if (op->reason != OFPR_NO_MATCH)
162 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
164 data_len = len - offsetof(struct ofp_packet_in, data);
165 ds_put_format(string, " data_len=%zu", data_len);
166 if (htonl(op->buffer_id) == UINT32_MAX) {
167 ds_put_format(string, " (unbuffered)");
168 if (ntohs(op->total_len) != data_len)
169 ds_put_format(string, " (***total_len != data_len***)");
171 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
172 if (ntohs(op->total_len) < data_len)
173 ds_put_format(string, " (***total_len < data_len***)");
175 ds_put_char(string, '\n');
178 char *packet = ofp_packet_to_string(op->data, data_len,
179 ntohs(op->total_len));
180 ds_put_cstr(string, packet);
185 static void ofp_print_port_name(struct ds *string, uint16_t port)
204 case OFPP_CONTROLLER:
214 ds_put_format(string, "%"PRIu16, port);
217 ds_put_cstr(string, name);
221 ofp_print_action(struct ds *string, const struct ofp_action *a)
223 switch (ntohs(a->type)) {
226 uint16_t port = ntohs(a->arg.output.port);
227 if (port < OFPP_MAX) {
228 ds_put_format(string, "output:%"PRIu16, port);
230 ofp_print_port_name(string, port);
231 if (port == OFPP_CONTROLLER) {
232 if (a->arg.output.max_len) {
233 ds_put_format(string, ":%"PRIu16,
234 ntohs(a->arg.output.max_len));
236 ds_put_cstr(string, ":all");
243 case OFPAT_SET_DL_VLAN:
244 ds_put_cstr(string, "mod_vlan:");
245 if (ntohs(a->arg.vlan_id) == OFP_VLAN_NONE) {
246 ds_put_cstr(string, "strip");
248 ds_put_format(string, "%"PRIu16, ntohs(a->arg.vlan_id));
252 case OFPAT_SET_DL_SRC:
253 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
254 ETH_ADDR_ARGS(a->arg.dl_addr));
257 case OFPAT_SET_DL_DST:
258 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
259 ETH_ADDR_ARGS(a->arg.dl_addr));
262 case OFPAT_SET_NW_SRC:
263 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&a->arg.nw_addr));
266 case OFPAT_SET_NW_DST:
267 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&a->arg.nw_addr));
270 case OFPAT_SET_TP_SRC:
271 ds_put_format(string, "mod_tp_src:%d", ntohs(a->arg.tp));
274 case OFPAT_SET_TP_DST:
275 ds_put_format(string, "mod_tp_dst:%d", ntohs(a->arg.tp));
279 ds_put_format(string, "(decoder %"PRIu16" not implemented)",
285 static void ofp_print_actions(struct ds *string,
286 const struct ofp_action actions[],
290 int n_actions = n_bytes / sizeof *actions;
292 ds_put_format(string, "action%s=", n_actions == 1 ? "" : "s");
293 for (i = 0; i < n_actions; i++) {
295 ds_put_cstr(string, ",");
297 ofp_print_action(string, &actions[i]);
299 if (n_bytes % sizeof *actions) {
301 ds_put_cstr(string, ",");
303 ds_put_cstr(string, ", ***trailing garbage***");
307 /* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'string'
308 * at the given 'verbosity' level. */
309 static void ofp_packet_out(struct ds *string, const void *oh, size_t len,
312 const struct ofp_packet_out *opo = oh;
313 int n_actions = ntohs(opo->n_actions);
314 int act_len = n_actions * sizeof opo->actions[0];
316 ds_put_cstr(string, " in_port=");
317 ofp_print_port_name(string, ntohs(opo->in_port));
319 ds_put_format(string, " n_actions=%d ", n_actions);
320 if (act_len > (ntohs(opo->header.length) - sizeof *opo)) {
321 ds_put_format(string, "***packet too short for number of actions***\n");
324 ofp_print_actions(string, opo->actions, act_len);
326 if (ntohl(opo->buffer_id) == UINT32_MAX) {
327 int data_len = len - sizeof *opo - act_len;
328 ds_put_format(string, " data_len=%d", data_len);
329 if (verbosity > 0 && len > sizeof *opo) {
330 char *packet = ofp_packet_to_string(&opo->actions[n_actions],
332 ds_put_char(string, '\n');
333 ds_put_cstr(string, packet);
337 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
339 ds_put_char(string, '\n');
342 /* qsort comparison function. */
344 compare_ports(const void *a_, const void *b_)
346 const struct ofp_phy_port *a = a_;
347 const struct ofp_phy_port *b = b_;
348 uint16_t ap = ntohs(a->port_no);
349 uint16_t bp = ntohs(b->port_no);
351 return ap < bp ? -1 : ap > bp;
355 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
357 uint8_t name[OFP_MAX_PORT_NAME_LEN];
360 memcpy(name, port->name, sizeof name);
361 for (j = 0; j < sizeof name - 1; j++) {
362 if (!isprint(name[j])) {
368 ds_put_char(string, ' ');
369 ofp_print_port_name(string, ntohs(port->port_no));
370 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", speed:%d, flags:%#x, "
372 ETH_ADDR_ARGS(port->hw_addr), ntohl(port->speed),
373 ntohl(port->flags), ntohl(port->features));
376 /* Pretty-print the struct ofp_switch_features of 'len' bytes at 'oh' to
377 * 'string' at the given 'verbosity' level. */
379 ofp_print_switch_features(struct ds *string, const void *oh, size_t len,
382 const struct ofp_switch_features *osf = oh;
383 struct ofp_phy_port port_list[OFPP_MAX];
387 ds_put_format(string, "dp id:%"PRIx64"\n", ntohll(osf->datapath_id));
388 ds_put_format(string, "tables: exact:%d, compressed:%d, general:%d\n",
390 ntohl(osf->n_compression), ntohl(osf->n_general));
391 ds_put_format(string, "buffers: size:%d, number:%d\n",
392 ntohl(osf->buffer_mb), ntohl(osf->n_buffers));
393 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
394 ntohl(osf->capabilities), ntohl(osf->actions));
396 if (ntohs(osf->header.length) >= sizeof *osf) {
397 len = MIN(len, ntohs(osf->header.length));
399 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
401 memcpy(port_list, osf->ports, (len - sizeof *osf));
402 qsort(port_list, n_ports, sizeof port_list[0], compare_ports);
403 for (i = 0; i < n_ports; i++) {
404 ofp_print_phy_port(string, &port_list[i]);
408 /* Pretty-print the struct ofp_switch_config of 'len' bytes at 'oh' to 'string'
409 * at the given 'verbosity' level. */
411 ofp_print_switch_config(struct ds *string, const void *oh, size_t len,
414 const struct ofp_switch_config *osc = oh;
417 flags = ntohs(osc->flags);
418 if (flags & OFPC_SEND_FLOW_EXP) {
419 flags &= ~OFPC_SEND_FLOW_EXP;
420 ds_put_format(string, " (sending flow expirations)");
423 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
426 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
429 static void print_wild(struct ds *string, const char *leader, int is_wild,
430 int verbosity, const char *format, ...)
431 __attribute__((format(printf, 5, 6)));
433 static void print_wild(struct ds *string, const char *leader, int is_wild,
434 int verbosity, const char *format, ...)
436 if (is_wild && verbosity < 2) {
439 ds_put_cstr(string, leader);
443 va_start(args, format);
444 ds_put_format_valist(string, format, args);
447 ds_put_char(string, '*');
449 ds_put_char(string, ',');
453 print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
454 uint32_t wild_bits, int verbosity)
456 if (wild_bits >= 32 && verbosity < 2) {
459 ds_put_cstr(string, leader);
460 if (wild_bits < 32) {
461 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
463 ds_put_format(string, "/%d", 32 - wild_bits);
466 ds_put_char(string, '*');
468 ds_put_char(string, ',');
471 /* Pretty-print the ofp_match structure */
472 static void ofp_print_match(struct ds *f, const struct ofp_match *om,
475 uint32_t w = ntohl(om->wildcards);
476 bool skip_type = false;
477 bool skip_proto = false;
479 if (!(w & OFPFW_DL_TYPE) &&om->dl_type == htons(ETH_TYPE_IP)) {
481 if (!(w & OFPFW_NW_PROTO)) {
483 if (om->nw_proto == IP_TYPE_ICMP) {
484 ds_put_cstr(f, "icmp,");
485 } else if (om->nw_proto == IP_TYPE_TCP) {
486 ds_put_cstr(f, "tcp,");
487 } else if (om->nw_proto == IP_TYPE_UDP) {
488 ds_put_cstr(f, "udp,");
490 ds_put_cstr(f, "ip,");
494 ds_put_cstr(f, "ip,");
497 print_wild(f, "in_port=", w & OFPFW_IN_PORT, verbosity,
498 "%d", ntohs(om->in_port));
499 print_wild(f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
500 "0x%04x", ntohs(om->dl_vlan));
501 print_wild(f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
502 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
503 print_wild(f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
504 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
506 print_wild(f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
507 "0x%04x", ntohs(om->dl_type));
509 print_ip_netmask(f, "nw_src=", om->nw_src,
510 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
511 print_ip_netmask(f, "nw_dst=", om->nw_dst,
512 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
514 print_wild(f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
517 print_wild(f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
518 "%d", ntohs(om->tp_src));
519 print_wild(f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
520 "%d", ntohs(om->tp_dst));
523 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
524 * at the given 'verbosity' level. */
526 ofp_print_flow_mod(struct ds *string, const void *oh, size_t len,
529 const struct ofp_flow_mod *ofm = oh;
531 ofp_print_match(string, &ofm->match, verbosity);
532 ds_put_format(string, " cmd:%d idle:%d hard:%d pri:%d buf:%#x",
533 ntohs(ofm->command), ntohs(ofm->idle_timeout),
534 ntohs(ofm->hard_timeout),
535 ofm->match.wildcards ? ntohs(ofm->priority) : (uint16_t)-1,
536 ntohl(ofm->buffer_id));
537 ofp_print_actions(string, ofm->actions,
538 len - offsetof(struct ofp_flow_mod, actions));
539 ds_put_char(string, '\n');
542 /* Pretty-print the OFPT_FLOW_EXPIRED packet of 'len' bytes at 'oh' to 'string'
543 * at the given 'verbosity' level. */
545 ofp_print_flow_expired(struct ds *string, const void *oh, size_t len,
548 const struct ofp_flow_expired *ofe = oh;
550 ofp_print_match(string, &ofe->match, verbosity);
551 ds_put_cstr(string, " reason=");
552 switch (ofe->reason) {
553 case OFPER_IDLE_TIMEOUT:
554 ds_put_cstr(string, "idle");
556 case OFPER_HARD_TIMEOUT:
557 ds_put_cstr(string, "hard");
560 ds_put_format(string, "**%"PRIu8"**", ofe->reason);
563 ds_put_format(string,
564 " pri%"PRIu16" secs%"PRIu32" pkts%"PRIu64" bytes%"PRIu64"\n",
565 ofe->match.wildcards ? ntohs(ofe->priority) : (uint16_t)-1,
566 ntohl(ofe->duration), ntohll(ofe->packet_count),
567 ntohll(ofe->byte_count));
570 /* Pretty-print the OFPT_ERROR_MSG packet of 'len' bytes at 'oh' to 'string'
571 * at the given 'verbosity' level. */
573 ofp_print_error_msg(struct ds *string, const void *oh, size_t len,
576 const struct ofp_error_msg *oem = oh;
578 ds_put_format(string,
579 " type%d code%d\n", ntohs(oem->type), ntohs(oem->code));
582 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'string'
583 * at the given 'verbosity' level. */
585 ofp_print_port_status(struct ds *string, const void *oh, size_t len,
588 const struct ofp_port_status *ops = oh;
590 if (ops->reason == OFPPR_ADD) {
591 ds_put_format(string, "add:");
592 } else if (ops->reason == OFPPR_DELETE) {
593 ds_put_format(string, "del:");
594 } else if (ops->reason == OFPPR_MOD) {
595 ds_put_format(string, "mod:");
597 ds_put_format(string, "err:");
600 ofp_print_phy_port(string, &ops->desc);
604 ofp_version_stats_reply(struct ds *string, const void *body, size_t len,
607 const struct ofp_version_stats *vs = body;
609 ds_put_format(string, "Manufacturer: %s\n", vs->mfr_desc);
610 ds_put_format(string, "Hardware: %s\n", vs->hw_desc);
611 ds_put_format(string, "Software: %s\n", vs->sw_desc);
615 ofp_flow_stats_request(struct ds *string, const void *oh, size_t len,
618 const struct ofp_flow_stats_request *fsr = oh;
620 if (fsr->table_id == 0xff) {
621 ds_put_format(string, " table_id=any, ");
623 ds_put_format(string, " table_id=%"PRIu8", ", fsr->table_id);
626 ofp_print_match(string, &fsr->match, verbosity);
630 ofp_flow_stats_reply(struct ds *string, const void *body_, size_t len,
633 const char *body = body_;
634 const char *pos = body;
636 const struct ofp_flow_stats *fs;
637 ptrdiff_t bytes_left = body + len - pos;
640 if (bytes_left < sizeof *fs) {
641 if (bytes_left != 0) {
642 ds_put_format(string, " ***%td leftover bytes at end***",
648 fs = (const void *) pos;
649 length = ntohs(fs->length);
650 if (length < sizeof *fs) {
651 ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
654 } else if (length > bytes_left) {
655 ds_put_format(string,
656 " ***length=%zu but only %td bytes left***",
659 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
660 ds_put_format(string,
661 " ***length=%zu has %zu bytes leftover in "
664 (length - sizeof *fs) % sizeof fs->actions[0]);
668 ds_put_format(string, " duration=%"PRIu32"s, ", ntohl(fs->duration));
669 ds_put_format(string, "table_id=%"PRIu8", ", fs->table_id);
670 ds_put_format(string, "priority=%"PRIu16", ",
671 fs->match.wildcards ? ntohs(fs->priority) : (uint16_t)-1);
672 ds_put_format(string, "n_packets=%"PRIu64", ",
673 ntohll(fs->packet_count));
674 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
675 ds_put_format(string, "idle_timeout=%"PRIu16",",
676 ntohs(fs->idle_timeout));
677 ds_put_format(string, "hard_timeout=%"PRIu16",",
678 ntohs(fs->hard_timeout));
679 ofp_print_match(string, &fs->match, verbosity);
680 ofp_print_actions(string, fs->actions, length - sizeof *fs);
681 ds_put_char(string, '\n');
688 ofp_aggregate_stats_request(struct ds *string, const void *oh, size_t len,
691 const struct ofp_aggregate_stats_request *asr = oh;
693 if (asr->table_id == 0xff) {
694 ds_put_format(string, " table_id=any, ");
696 ds_put_format(string, " table_id=%"PRIu8", ", asr->table_id);
699 ofp_print_match(string, &asr->match, verbosity);
703 ofp_aggregate_stats_reply(struct ds *string, const void *body_, size_t len,
706 const struct ofp_aggregate_stats_reply *asr = body_;
708 ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
709 ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
710 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
713 static void print_port_stat(struct ds *string, const char *leader,
714 uint64_t stat, int more)
716 ds_put_cstr(string, leader);
718 ds_put_format(string, "%"PRIu64, stat);
720 ds_put_char(string, '?');
723 ds_put_cstr(string, ", ");
725 ds_put_cstr(string, "\n");
730 ofp_port_stats_reply(struct ds *string, const void *body, size_t len,
733 const struct ofp_port_stats *ps = body;
734 size_t n = len / sizeof *ps;
735 ds_put_format(string, " %zu ports\n", n);
741 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
743 ds_put_cstr(string, "rx ");
744 print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
745 print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
746 print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
747 print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
748 print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
749 print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
750 print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
752 ds_put_cstr(string, " tx ");
753 print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
754 print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
755 print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
756 print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
757 print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
762 ofp_table_stats_reply(struct ds *string, const void *body, size_t len,
765 const struct ofp_table_stats *ts = body;
766 size_t n = len / sizeof *ts;
767 ds_put_format(string, " %zu tables\n", n);
773 char name[OFP_MAX_TABLE_NAME_LEN + 1];
774 strncpy(name, ts->name, sizeof name);
775 name[OFP_MAX_TABLE_NAME_LEN] = '\0';
777 ds_put_format(string, " table %"PRIu8": ", ts->table_id);
778 ds_put_format(string, "name %-8s, ", name);
779 ds_put_format(string, "max %6"PRIu32", ", ntohl(ts->max_entries));
780 ds_put_format(string, "active %6"PRIu32", ", ntohl(ts->active_count));
781 ds_put_format(string, "matched %6"PRIu64"\n",
782 ntohll(ts->matched_count));
786 enum stats_direction {
792 print_stats(struct ds *string, int type, const void *body, size_t body_len,
793 int verbosity, enum stats_direction direction)
796 size_t min_body, max_body;
797 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
802 struct stats_msg request;
803 struct stats_msg reply;
806 static const struct stats_type stats_types[] = {
810 { 0, SIZE_MAX, ofp_version_stats_reply },
814 { sizeof(struct ofp_flow_stats_request),
815 sizeof(struct ofp_flow_stats_request),
816 ofp_flow_stats_request },
817 { 0, SIZE_MAX, ofp_flow_stats_reply },
819 [OFPST_AGGREGATE] = {
821 { sizeof(struct ofp_aggregate_stats_request),
822 sizeof(struct ofp_aggregate_stats_request),
823 ofp_aggregate_stats_request },
824 { sizeof(struct ofp_aggregate_stats_reply),
825 sizeof(struct ofp_aggregate_stats_reply),
826 ofp_aggregate_stats_reply },
831 { 0, SIZE_MAX, ofp_table_stats_reply },
836 { 0, SIZE_MAX, ofp_port_stats_reply },
840 const struct stats_type *s;
841 const struct stats_msg *m;
843 if (type >= ARRAY_SIZE(stats_types) || !stats_types[type].name) {
844 ds_put_format(string, " ***unknown type %d***", type);
847 s = &stats_types[type];
848 ds_put_format(string, " type=%d(%s)\n", type, s->name);
850 m = direction == REQUEST ? &s->request : &s->reply;
851 if (body_len < m->min_body || body_len > m->max_body) {
852 ds_put_format(string, " ***body_len=%zu not in %zu...%zu***",
853 body_len, m->min_body, m->max_body);
857 m->printer(string, body, body_len, verbosity);
862 ofp_stats_request(struct ds *string, const void *oh, size_t len, int verbosity)
864 const struct ofp_stats_request *srq = oh;
867 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
871 print_stats(string, ntohs(srq->type), srq->body,
872 len - offsetof(struct ofp_stats_request, body),
877 ofp_stats_reply(struct ds *string, const void *oh, size_t len, int verbosity)
879 const struct ofp_stats_reply *srp = oh;
881 ds_put_cstr(string, " flags=");
883 ds_put_cstr(string, "none");
885 uint16_t flags = ntohs(srp->flags);
886 if (flags & OFPSF_REPLY_MORE) {
887 ds_put_cstr(string, "[more]");
888 flags &= ~OFPSF_REPLY_MORE;
891 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]", flags);
895 print_stats(string, ntohs(srp->type), srp->body,
896 len - offsetof(struct ofp_stats_reply, body),
901 ofp_echo(struct ds *string, const void *oh, size_t len, int verbosity)
903 const struct ofp_header *hdr = oh;
905 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *hdr);
907 ds_put_hex_dump(string, hdr, len - sizeof *hdr, 0, true);
911 struct openflow_packet {
914 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
917 static const struct openflow_packet packets[] = {
918 [OFPT_FEATURES_REQUEST] = {
920 sizeof (struct ofp_header),
923 [OFPT_FEATURES_REPLY] = {
925 sizeof (struct ofp_switch_features),
926 ofp_print_switch_features,
928 [OFPT_GET_CONFIG_REQUEST] = {
929 "get_config_request",
930 sizeof (struct ofp_header),
933 [OFPT_GET_CONFIG_REPLY] = {
935 sizeof (struct ofp_switch_config),
936 ofp_print_switch_config,
938 [OFPT_SET_CONFIG] = {
940 sizeof (struct ofp_switch_config),
941 ofp_print_switch_config,
945 offsetof(struct ofp_packet_in, data),
948 [OFPT_PACKET_OUT] = {
950 sizeof (struct ofp_packet_out),
955 sizeof (struct ofp_flow_mod),
958 [OFPT_FLOW_EXPIRED] = {
960 sizeof (struct ofp_flow_expired),
961 ofp_print_flow_expired,
965 sizeof (struct ofp_port_mod),
968 [OFPT_PORT_STATUS] = {
970 sizeof (struct ofp_port_status),
971 ofp_print_port_status
975 sizeof (struct ofp_error_msg),
978 [OFPT_STATS_REQUEST] = {
980 sizeof (struct ofp_stats_request),
983 [OFPT_STATS_REPLY] = {
985 sizeof (struct ofp_stats_reply),
988 [OFPT_ECHO_REQUEST] = {
990 sizeof (struct ofp_header),
993 [OFPT_ECHO_REPLY] = {
995 sizeof (struct ofp_header),
1000 /* Composes and returns a string representing the OpenFlow packet of 'len'
1001 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1002 * verbosity and higher numbers increase verbosity. The caller is responsible
1003 * for freeing the string. */
1005 ofp_to_string(const void *oh_, size_t len, int verbosity)
1007 struct ds string = DS_EMPTY_INITIALIZER;
1008 const struct ofp_header *oh = oh_;
1009 const struct openflow_packet *pkt;
1011 if (len < sizeof(struct ofp_header)) {
1012 ds_put_cstr(&string, "OpenFlow packet too short:\n");
1013 ds_put_hex_dump(&string, oh, len, 0, true);
1014 return ds_cstr(&string);
1015 } else if (oh->version != OFP_VERSION) {
1016 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", oh->version);
1017 ds_put_hex_dump(&string, oh, len, 0, true);
1018 return ds_cstr(&string);
1019 } else if (oh->type >= ARRAY_SIZE(packets) || !packets[oh->type].name) {
1020 ds_put_format(&string, "Unknown OpenFlow packet type %"PRIu8":\n",
1022 ds_put_hex_dump(&string, oh, len, 0, true);
1023 return ds_cstr(&string);
1026 pkt = &packets[oh->type];
1027 ds_put_format(&string, "%s (xid=0x%"PRIx32"):", pkt->name, oh->xid);
1029 if (ntohs(oh->length) > len)
1030 ds_put_format(&string, " (***truncated to %zu bytes from %"PRIu16"***)",
1031 len, ntohs(oh->length));
1032 else if (ntohs(oh->length) < len) {
1033 ds_put_format(&string, " (***only uses %"PRIu16" bytes out of %zu***)\n",
1034 ntohs(oh->length), len);
1035 len = ntohs(oh->length);
1038 if (len < pkt->min_size) {
1039 ds_put_format(&string, " (***length=%zu < min_size=%zu***)\n",
1040 len, pkt->min_size);
1041 } else if (!pkt->printer) {
1042 if (len > sizeof *oh) {
1043 ds_put_format(&string, " length=%"PRIu16" (decoder not implemented)\n",
1047 pkt->printer(&string, oh, len, verbosity);
1049 if (verbosity >= 3) {
1050 ds_put_hex_dump(&string, oh, len, 0, true);
1052 if (string.string[string.length - 1] != '\n') {
1053 ds_put_char(&string, '\n');
1055 return ds_cstr(&string);
1059 print_and_free(FILE *stream, char *string)
1061 fputs(string, stream);
1065 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1066 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1067 * numbers increase verbosity. */
1069 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1071 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1074 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1075 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1076 * the Ethernet frame (of which 'len' bytes were captured).
1078 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1080 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1082 print_and_free(stream, ofp_packet_to_string(data, len, total_len));