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=%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)
201 case OFPP_CONTROLLER:
211 ds_put_format(string, "%"PRIu16, port);
214 ds_put_cstr(string, name);
218 ofp_print_action(struct ds *string, const struct ofp_action *a)
220 switch (ntohs(a->type)) {
223 uint16_t port = ntohs(a->arg.output.port);
224 if (port < OFPP_MAX) {
225 ds_put_format(string, "output:%"PRIu16, port);
227 ofp_print_port_name(string, port);
228 if (port == OFPP_CONTROLLER) {
229 if (a->arg.output.max_len) {
230 ds_put_format(string, ":%"PRIu16,
231 ntohs(a->arg.output.max_len));
233 ds_put_cstr(string, ":all");
240 case OFPAT_SET_DL_VLAN:
241 ds_put_cstr(string, "mod_vlan:");
242 if (ntohs(a->arg.vlan_id) == OFP_VLAN_NONE) {
243 ds_put_cstr(string, "strip");
245 ds_put_format(string, "%"PRIu16, ntohs(a->arg.vlan_id));
249 case OFPAT_SET_DL_SRC:
250 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
251 ETH_ADDR_ARGS(a->arg.dl_addr));
254 case OFPAT_SET_DL_DST:
255 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
256 ETH_ADDR_ARGS(a->arg.dl_addr));
259 case OFPAT_SET_NW_SRC:
260 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&a->arg.nw_addr));
263 case OFPAT_SET_NW_DST:
264 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&a->arg.nw_addr));
267 case OFPAT_SET_TP_SRC:
268 ds_put_format(string, "mod_tp_src:%d", ntohs(a->arg.tp));
271 case OFPAT_SET_TP_DST:
272 ds_put_format(string, "mod_tp_dst:%d", ntohs(a->arg.tp));
276 ds_put_format(string, "(decoder %"PRIu16" not implemented)",
282 static void ofp_print_actions(struct ds *string,
283 const struct ofp_action actions[],
287 int n_actions = n_bytes / sizeof *actions;
289 ds_put_format(string, "action%s=", n_actions == 1 ? "" : "s");
290 for (i = 0; i < n_actions; i++) {
292 ds_put_cstr(string, ",");
294 ofp_print_action(string, &actions[i]);
296 if (n_bytes % sizeof *actions) {
298 ds_put_cstr(string, ",");
300 ds_put_cstr(string, ", ***trailing garbage***");
304 /* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'string'
305 * at the given 'verbosity' level. */
306 static void ofp_packet_out(struct ds *string, const void *oh, size_t len,
309 const struct ofp_packet_out *opo = oh;
311 ds_put_cstr(string, " in_port=");
312 ofp_print_port_name(string, ntohs(opo->in_port));
314 if (ntohl(opo->buffer_id) == UINT32_MAX) {
315 ds_put_cstr(string, " out_port=");
316 ofp_print_port_name(string, ntohs(opo->out_port));
317 if (verbosity > 0 && len > sizeof *opo) {
318 char *packet = ofp_packet_to_string(opo->u.data, len - sizeof *opo,
320 ds_put_char(string, '\n');
321 ds_put_cstr(string, packet);
325 ds_put_format(string, " buffer=%08"PRIx32, ntohl(opo->buffer_id));
326 ofp_print_actions(string, opo->u.actions, len - sizeof *opo);
328 ds_put_char(string, '\n');
331 /* qsort comparison function. */
333 compare_ports(const void *a_, const void *b_)
335 const struct ofp_phy_port *a = a_;
336 const struct ofp_phy_port *b = b_;
337 uint16_t ap = ntohs(a->port_no);
338 uint16_t bp = ntohs(b->port_no);
340 return ap < bp ? -1 : ap > bp;
344 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
346 uint8_t name[OFP_MAX_PORT_NAME_LEN];
349 memcpy(name, port->name, sizeof name);
350 for (j = 0; j < sizeof name - 1; j++) {
351 if (!isprint(name[j])) {
357 ds_put_char(string, ' ');
358 ofp_print_port_name(string, ntohs(port->port_no));
359 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", speed:%d, flags:%#x, "
361 ETH_ADDR_ARGS(port->hw_addr), ntohl(port->speed),
362 ntohl(port->flags), ntohl(port->features));
365 /* Pretty-print the struct ofp_switch_features of 'len' bytes at 'oh' to
366 * 'string' at the given 'verbosity' level. */
368 ofp_print_switch_features(struct ds *string, const void *oh, size_t len,
371 const struct ofp_switch_features *osf = oh;
372 struct ofp_phy_port port_list[OFPP_MAX];
376 ds_put_format(string, "dp id:%"PRIx64"\n", ntohll(osf->datapath_id));
377 ds_put_format(string, "tables: exact:%d, compressed:%d, general:%d\n",
379 ntohl(osf->n_compression), ntohl(osf->n_general));
380 ds_put_format(string, "buffers: size:%d, number:%d\n",
381 ntohl(osf->buffer_mb), ntohl(osf->n_buffers));
382 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
383 ntohl(osf->capabilities), ntohl(osf->actions));
385 if (ntohs(osf->header.length) >= sizeof *osf) {
386 len = MIN(len, ntohs(osf->header.length));
388 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
390 memcpy(port_list, osf->ports, (len - sizeof *osf));
391 qsort(port_list, n_ports, sizeof port_list[0], compare_ports);
392 for (i = 0; i < n_ports; i++) {
393 ofp_print_phy_port(string, &port_list[i]);
397 /* Pretty-print the struct ofp_switch_config of 'len' bytes at 'oh' to 'string'
398 * at the given 'verbosity' level. */
400 ofp_print_switch_config(struct ds *string, const void *oh, size_t len,
403 const struct ofp_switch_config *osc = oh;
406 flags = ntohs(osc->flags);
407 if (flags & OFPC_SEND_FLOW_EXP) {
408 flags &= ~OFPC_SEND_FLOW_EXP;
409 ds_put_format(string, " (sending flow expirations)");
412 ds_put_format(string, " ***unknown flags %04"PRIx16"***", flags);
415 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
418 static void print_wild(struct ds *string, const char *leader, int is_wild,
419 int verbosity, const char *format, ...)
420 __attribute__((format(printf, 5, 6)));
422 static void print_wild(struct ds *string, const char *leader, int is_wild,
423 int verbosity, const char *format, ...)
425 if (is_wild && verbosity < 2) {
428 ds_put_cstr(string, leader);
432 va_start(args, format);
433 ds_put_format_valist(string, format, args);
436 ds_put_char(string, '*');
438 ds_put_char(string, ',');
441 /* Pretty-print the ofp_match structure */
442 static void ofp_print_match(struct ds *f, const struct ofp_match *om,
445 uint16_t w = ntohs(om->wildcards);
447 print_wild(f, "in_port=", w & OFPFW_IN_PORT, verbosity,
448 "%d", ntohs(om->in_port));
449 print_wild(f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
450 "%04x", ntohs(om->dl_vlan));
451 print_wild(f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
452 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
453 print_wild(f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
454 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
455 print_wild(f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
456 "%04x", ntohs(om->dl_type));
457 print_wild(f, "nw_src=", w & OFPFW_NW_SRC, verbosity,
458 IP_FMT, IP_ARGS(&om->nw_src));
459 print_wild(f, "nw_dst=", w & OFPFW_NW_DST, verbosity,
460 IP_FMT, IP_ARGS(&om->nw_dst));
461 print_wild(f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
463 print_wild(f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
464 "%d", ntohs(om->tp_src));
465 print_wild(f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
466 "%d", ntohs(om->tp_dst));
469 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
470 * at the given 'verbosity' level. */
472 ofp_print_flow_mod(struct ds *string, const void *oh, size_t len,
475 const struct ofp_flow_mod *ofm = oh;
477 ofp_print_match(string, &ofm->match, verbosity);
478 ds_put_format(string, " cmd:%d idle:%d hard:%d pri:%d buf:%#x",
479 ntohs(ofm->command), ntohs(ofm->idle_timeout),
480 ntohs(ofm->hard_timeout),
481 ofm->match.wildcards ? ntohs(ofm->priority) : (uint16_t)-1,
482 ntohl(ofm->buffer_id));
483 ofp_print_actions(string, ofm->actions,
484 len - offsetof(struct ofp_flow_mod, actions));
485 ds_put_char(string, '\n');
488 /* Pretty-print the OFPT_FLOW_EXPIRED packet of 'len' bytes at 'oh' to 'string'
489 * at the given 'verbosity' level. */
491 ofp_print_flow_expired(struct ds *string, const void *oh, size_t len,
494 const struct ofp_flow_expired *ofe = oh;
496 ofp_print_match(string, &ofe->match, verbosity);
497 ds_put_cstr(string, " reason=");
498 switch (ofe->reason) {
499 case OFPER_IDLE_TIMEOUT:
500 ds_put_cstr(string, "idle");
502 case OFPER_HARD_TIMEOUT:
503 ds_put_cstr(string, "hard");
506 ds_put_format(string, "**%"PRIu8"**", ofe->reason);
509 ds_put_format(string,
510 " pri%"PRIu16" secs%"PRIu32" pkts%"PRIu64" bytes%"PRIu64"\n",
511 ofe->match.wildcards ? ntohs(ofe->priority) : (uint16_t)-1,
512 ntohl(ofe->duration), ntohll(ofe->packet_count),
513 ntohll(ofe->byte_count));
516 /* Pretty-print the OFPT_ERROR_MSG packet of 'len' bytes at 'oh' to 'string'
517 * at the given 'verbosity' level. */
519 ofp_print_error_msg(struct ds *string, const void *oh, size_t len,
522 const struct ofp_error_msg *oem = oh;
524 ds_put_format(string,
525 " type%d code%d\n", ntohs(oem->type), ntohs(oem->code));
528 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'string'
529 * at the given 'verbosity' level. */
531 ofp_print_port_status(struct ds *string, const void *oh, size_t len,
534 const struct ofp_port_status *ops = oh;
536 if (ops->reason == OFPPR_ADD) {
537 ds_put_format(string, "add:");
538 } else if (ops->reason == OFPPR_DELETE) {
539 ds_put_format(string, "del:");
540 } else if (ops->reason == OFPPR_MOD) {
541 ds_put_format(string, "mod:");
543 ds_put_format(string, "err:");
546 ofp_print_phy_port(string, &ops->desc);
550 ofp_flow_stats_request(struct ds *string, const void *oh, size_t len,
553 const struct ofp_flow_stats_request *fsr = oh;
555 if (fsr->table_id == 0xff) {
556 ds_put_format(string, " table_id=any, ");
558 ds_put_format(string, " table_id=%"PRIu8", ", fsr->table_id);
561 ofp_print_match(string, &fsr->match, verbosity);
565 ofp_flow_stats_reply(struct ds *string, const void *body_, size_t len,
568 const char *body = body_;
569 const char *pos = body;
571 const struct ofp_flow_stats *fs;
572 ptrdiff_t bytes_left = body + len - pos;
575 if (bytes_left < sizeof *fs) {
576 if (bytes_left != 0) {
577 ds_put_format(string, " ***%td leftover bytes at end***",
583 fs = (const void *) pos;
584 length = ntohs(fs->length);
585 if (length < sizeof *fs) {
586 ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
589 } else if (length > bytes_left) {
590 ds_put_format(string,
591 " ***length=%zu but only %td bytes left***",
594 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
595 ds_put_format(string,
596 " ***length=%zu has %zu bytes leftover in "
599 (length - sizeof *fs) % sizeof fs->actions[0]);
603 ds_put_format(string, " duration=%"PRIu32"s, ", ntohl(fs->duration));
604 ds_put_format(string, "table_id=%"PRIu8", ", fs->table_id);
605 ds_put_format(string, "priority=%"PRIu16", ",
606 fs->match.wildcards ? ntohs(fs->priority) : (uint16_t)-1);
607 ds_put_format(string, "n_packets=%"PRIu64", ",
608 ntohll(fs->packet_count));
609 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
610 ds_put_format(string, "idle_timeout=%"PRIu16",",
611 ntohs(fs->idle_timeout));
612 ds_put_format(string, "hard_timeout=%"PRIu16",",
613 ntohs(fs->hard_timeout));
614 ofp_print_match(string, &fs->match, verbosity);
615 ofp_print_actions(string, fs->actions, length - sizeof *fs);
616 ds_put_char(string, '\n');
623 ofp_aggregate_stats_request(struct ds *string, const void *oh, size_t len,
626 const struct ofp_aggregate_stats_request *asr = oh;
628 if (asr->table_id == 0xff) {
629 ds_put_format(string, " table_id=any, ");
631 ds_put_format(string, " table_id=%"PRIu8", ", asr->table_id);
634 ofp_print_match(string, &asr->match, verbosity);
638 ofp_aggregate_stats_reply(struct ds *string, const void *body_, size_t len,
641 const struct ofp_aggregate_stats_reply *asr = body_;
643 ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
644 ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
645 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
649 ofp_port_stats_reply(struct ds *string, const void *body, size_t len,
652 const struct ofp_port_stats *ps = body;
653 size_t n = len / sizeof *ps;
654 ds_put_format(string, " %zu ports\n", n);
660 ds_put_format(string, " port %"PRIu16": ", ntohs(ps->port_no));
661 ds_put_format(string, "rx %"PRIu64", ", ntohll(ps->rx_count));
662 ds_put_format(string, "tx %"PRIu64", ", ntohll(ps->tx_count));
663 ds_put_format(string, "dropped %"PRIu64"\n", ntohll(ps->drop_count));
668 ofp_table_stats_reply(struct ds *string, const void *body, size_t len,
671 const struct ofp_table_stats *ts = body;
672 size_t n = len / sizeof *ts;
673 ds_put_format(string, " %zu tables\n", n);
679 char name[OFP_MAX_TABLE_NAME_LEN + 1];
680 strncpy(name, ts->name, sizeof name);
681 name[OFP_MAX_TABLE_NAME_LEN] = '\0';
683 ds_put_format(string, " table %"PRIu8": ", ts->table_id);
684 ds_put_format(string, "name %-8s, ", name);
685 ds_put_format(string, "max %6"PRIu32", ", ntohl(ts->max_entries));
686 ds_put_format(string, "active %6"PRIu32", ", ntohl(ts->active_count));
687 ds_put_format(string, "matched %6"PRIu64"\n",
688 ntohll(ts->matched_count));
692 enum stats_direction {
698 print_stats(struct ds *string, int type, const void *body, size_t body_len,
699 int verbosity, enum stats_direction direction)
702 size_t min_body, max_body;
703 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
708 struct stats_msg request;
709 struct stats_msg reply;
712 static const struct stats_type stats_types[] = {
715 { sizeof(struct ofp_flow_stats_request),
716 sizeof(struct ofp_flow_stats_request),
717 ofp_flow_stats_request },
718 { 0, SIZE_MAX, ofp_flow_stats_reply },
720 [OFPST_AGGREGATE] = {
722 { sizeof(struct ofp_aggregate_stats_request),
723 sizeof(struct ofp_aggregate_stats_request),
724 ofp_aggregate_stats_request },
725 { sizeof(struct ofp_aggregate_stats_reply),
726 sizeof(struct ofp_aggregate_stats_reply),
727 ofp_aggregate_stats_reply },
732 { 0, SIZE_MAX, ofp_table_stats_reply },
737 { 0, SIZE_MAX, ofp_port_stats_reply },
741 const struct stats_type *s;
742 const struct stats_msg *m;
744 if (type >= ARRAY_SIZE(stats_types) || !stats_types[type].name) {
745 ds_put_format(string, " ***unknown type %d***", type);
748 s = &stats_types[type];
749 ds_put_format(string, " type=%d(%s)\n", type, s->name);
751 m = direction == REQUEST ? &s->request : &s->reply;
752 if (body_len < m->min_body || body_len > m->max_body) {
753 ds_put_format(string, " ***body_len=%zu not in %zu...%zu***",
754 body_len, m->min_body, m->max_body);
758 m->printer(string, body, body_len, verbosity);
763 ofp_stats_request(struct ds *string, const void *oh, size_t len, int verbosity)
765 const struct ofp_stats_request *srq = oh;
768 ds_put_format(string, " ***unknown flags %04"PRIx16"***",
772 print_stats(string, ntohs(srq->type), srq->body,
773 len - offsetof(struct ofp_stats_request, body),
778 ofp_stats_reply(struct ds *string, const void *oh, size_t len, int verbosity)
780 const struct ofp_stats_reply *srp = oh;
782 ds_put_cstr(string, " flags=");
784 ds_put_cstr(string, "none");
786 uint16_t flags = ntohs(srp->flags);
787 if (flags & OFPSF_REPLY_MORE) {
788 ds_put_cstr(string, "[more]");
789 flags &= ~OFPSF_REPLY_MORE;
792 ds_put_format(string, "[***unknown%04"PRIx16"***]", flags);
796 print_stats(string, ntohs(srp->type), srp->body,
797 len - offsetof(struct ofp_stats_reply, body),
802 ofp_echo(struct ds *string, const void *oh, size_t len, int verbosity)
804 const struct ofp_header *hdr = oh;
806 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *hdr);
808 ds_put_hex_dump(string, hdr, len - sizeof *hdr, 0, true);
812 struct openflow_packet {
815 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
818 static const struct openflow_packet packets[] = {
819 [OFPT_FEATURES_REQUEST] = {
821 sizeof (struct ofp_header),
824 [OFPT_FEATURES_REPLY] = {
826 sizeof (struct ofp_switch_features),
827 ofp_print_switch_features,
829 [OFPT_GET_CONFIG_REQUEST] = {
830 "get_config_request",
831 sizeof (struct ofp_header),
834 [OFPT_GET_CONFIG_REPLY] = {
836 sizeof (struct ofp_switch_config),
837 ofp_print_switch_config,
839 [OFPT_SET_CONFIG] = {
841 sizeof (struct ofp_switch_config),
842 ofp_print_switch_config,
846 offsetof(struct ofp_packet_in, data),
849 [OFPT_PACKET_OUT] = {
851 sizeof (struct ofp_packet_out),
856 sizeof (struct ofp_flow_mod),
859 [OFPT_FLOW_EXPIRED] = {
861 sizeof (struct ofp_flow_expired),
862 ofp_print_flow_expired,
866 sizeof (struct ofp_port_mod),
869 [OFPT_PORT_STATUS] = {
871 sizeof (struct ofp_port_status),
872 ofp_print_port_status
876 sizeof (struct ofp_error_msg),
879 [OFPT_STATS_REQUEST] = {
881 sizeof (struct ofp_stats_request),
884 [OFPT_STATS_REPLY] = {
886 sizeof (struct ofp_stats_reply),
889 [OFPT_ECHO_REQUEST] = {
891 sizeof (struct ofp_header),
894 [OFPT_ECHO_REPLY] = {
896 sizeof (struct ofp_header),
901 /* Composes and returns a string representing the OpenFlow packet of 'len'
902 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
903 * verbosity and higher numbers increase verbosity. The caller is responsible
904 * for freeing the string. */
906 ofp_to_string(const void *oh_, size_t len, int verbosity)
908 struct ds string = DS_EMPTY_INITIALIZER;
909 const struct ofp_header *oh = oh_;
910 const struct openflow_packet *pkt;
912 if (len < sizeof(struct ofp_header)) {
913 ds_put_cstr(&string, "OpenFlow packet too short:\n");
914 ds_put_hex_dump(&string, oh, len, 0, true);
915 return ds_cstr(&string);
916 } else if (oh->version != OFP_VERSION) {
917 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", oh->version);
918 ds_put_hex_dump(&string, oh, len, 0, true);
919 return ds_cstr(&string);
920 } else if (oh->type >= ARRAY_SIZE(packets) || !packets[oh->type].name) {
921 ds_put_format(&string, "Unknown OpenFlow packet type %"PRIu8":\n",
923 ds_put_hex_dump(&string, oh, len, 0, true);
924 return ds_cstr(&string);
927 pkt = &packets[oh->type];
928 ds_put_format(&string, "%s (xid=%"PRIx32"):", pkt->name, oh->xid);
930 if (ntohs(oh->length) > len)
931 ds_put_format(&string, " (***truncated to %zu bytes from %"PRIu16"***)",
932 len, ntohs(oh->length));
933 else if (ntohs(oh->length) < len) {
934 ds_put_format(&string, " (***only uses %"PRIu16" bytes out of %zu***)\n",
935 ntohs(oh->length), len);
936 len = ntohs(oh->length);
939 if (len < pkt->min_size) {
940 ds_put_format(&string, " (***length=%zu < min_size=%zu***)\n",
942 } else if (!pkt->printer) {
943 if (len > sizeof *oh) {
944 ds_put_format(&string, " length=%"PRIu16" (decoder not implemented)\n",
948 pkt->printer(&string, oh, len, verbosity);
950 if (verbosity >= 3) {
951 ds_put_hex_dump(&string, oh, len, 0, true);
953 if (string.string[string.length - 1] != '\n') {
954 ds_put_char(&string, '\n');
956 return ds_cstr(&string);
960 print_and_free(FILE *stream, char *string)
962 fputs(string, stream);
966 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
967 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
968 * numbers increase verbosity. */
970 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
972 print_and_free(stream, ofp_to_string(oh, len, verbosity));
975 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
976 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
977 * the Ethernet frame (of which 'len' bytes were captured).
979 * This starts and kills a tcpdump subprocess so it's quite expensive. */
981 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
983 print_and_free(stream, ofp_packet_to_string(data, len, total_len));