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.
34 #include "ofp-print.h"
39 #include <netinet/in.h>
46 #include "dynamic-string.h"
51 static void ofp_print_port_name(struct ds *string, uint16_t port);
53 /* Returns a string that represents the contents of the Ethernet frame in the
54 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
55 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
56 * bytes were captured).
58 * The caller must free the returned string.
60 * This starts and kills a tcpdump subprocess so it's quite expensive. */
62 ofp_packet_to_string(const void *data, size_t len, size_t total_len)
65 uint32_t magic_number; /* magic number */
66 uint16_t version_major; /* major version number */
67 uint16_t version_minor; /* minor version number */
68 int32_t thiszone; /* GMT to local correction */
69 uint32_t sigfigs; /* accuracy of timestamps */
70 uint32_t snaplen; /* max length of captured packets */
71 uint32_t network; /* data link type */
75 uint32_t ts_sec; /* timestamp seconds */
76 uint32_t ts_usec; /* timestamp microseconds */
77 uint32_t incl_len; /* number of octets of packet saved in file */
78 uint32_t orig_len; /* actual length of packet */
82 struct pcaprec_hdr prh;
84 struct ds ds = DS_EMPTY_INITIALIZER;
94 error(errno, "tmpfile");
95 return xstrdup("<error>");
98 /* The pcap reader is responsible for figuring out endianness based on the
99 * magic number, so the lack of htonX calls here is intentional. */
100 ph.magic_number = 0xa1b2c3d4;
101 ph.version_major = 2;
102 ph.version_minor = 4;
106 ph.network = 1; /* Ethernet */
111 prh.orig_len = total_len;
113 fwrite(&ph, 1, sizeof ph, pcap);
114 fwrite(&prh, 1, sizeof prh, pcap);
115 fwrite(data, 1, len, pcap);
119 error(errno, "error writing temporary file");
123 snprintf(command, sizeof command, "tcpdump -n -r /dev/fd/%d 2>/dev/null",
125 tcpdump = popen(command, "r");
128 error(errno, "exec(\"%s\")", command);
129 return xstrdup("<error>");
132 while ((c = getc(tcpdump)) != EOF) {
136 status = pclose(tcpdump);
137 if (WIFEXITED(status)) {
138 if (WEXITSTATUS(status))
139 error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
140 } else if (WIFSIGNALED(status)) {
141 error(0, "tcpdump exited with signal %d", WTERMSIG(status));
146 /* Pretty-print the OFPT_PACKET_IN packet of 'len' bytes at 'oh' to 'stream'
147 * at the given 'verbosity' level. */
149 ofp_packet_in(struct ds *string, const void *oh, size_t len, int verbosity)
151 const struct ofp_packet_in *op = oh;
154 ds_put_format(string, " total_len=%"PRIu16" in_port=",
155 ntohs(op->total_len));
156 ofp_print_port_name(string, ntohs(op->in_port));
158 if (op->reason == OFPR_ACTION)
159 ds_put_cstr(string, " (via action)");
160 else if (op->reason != OFPR_NO_MATCH)
161 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
163 data_len = len - offsetof(struct ofp_packet_in, data);
164 ds_put_format(string, " data_len=%zu", data_len);
165 if (htonl(op->buffer_id) == UINT32_MAX) {
166 ds_put_format(string, " (unbuffered)");
167 if (ntohs(op->total_len) != data_len)
168 ds_put_format(string, " (***total_len != data_len***)");
170 ds_put_format(string, " buffer=%08"PRIx32, ntohl(op->buffer_id));
171 if (ntohs(op->total_len) < data_len)
172 ds_put_format(string, " (***total_len < data_len***)");
174 ds_put_char(string, '\n');
177 char *packet = ofp_packet_to_string(op->data, data_len,
178 ntohs(op->total_len));
179 ds_put_cstr(string, packet);
184 static void ofp_print_port_name(struct ds *string, uint16_t port)
200 case OFPP_CONTROLLER:
210 ds_put_format(string, "%"PRIu16, port);
213 ds_put_cstr(string, name);
217 ofp_print_action(struct ds *string, const struct ofp_action *a)
219 switch (ntohs(a->type)) {
221 ds_put_cstr(string, "output(");
222 ofp_print_port_name(string, ntohs(a->arg.output.port));
223 if (a->arg.output.port == htons(OFPP_CONTROLLER)) {
224 ds_put_format(string, ", max %"PRIu16" bytes", ntohs(a->arg.output.max_len));
226 ds_put_cstr(string, ")");
230 ds_put_format(string, "(decoder %"PRIu16" not implemented)", ntohs(a->type));
235 static void ofp_print_actions(struct ds *string,
236 const struct ofp_action actions[],
241 ds_put_cstr(string, " actions[");
242 for (i = 0; i < n_bytes / sizeof *actions; i++) {
244 ds_put_cstr(string, "; ");
246 ofp_print_action(string, &actions[i]);
248 if (n_bytes % sizeof *actions) {
250 ds_put_cstr(string, "; ");
252 ds_put_cstr(string, "; ***trailing garbage***");
254 ds_put_cstr(string, "]");
257 /* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'string'
258 * at the given 'verbosity' level. */
259 static void ofp_packet_out(struct ds *string, const void *oh, size_t len,
262 const struct ofp_packet_out *opo = oh;
264 ds_put_cstr(string, " in_port=");
265 ofp_print_port_name(string, ntohs(opo->in_port));
267 if (ntohl(opo->buffer_id) == UINT32_MAX) {
268 ds_put_cstr(string, " out_port=");
269 ofp_print_port_name(string, ntohs(opo->out_port));
270 if (verbosity > 0 && len > sizeof *opo) {
271 char *packet = ofp_packet_to_string(opo->u.data, len - sizeof *opo,
273 ds_put_char(string, '\n');
274 ds_put_cstr(string, packet);
278 ds_put_format(string, " buffer=%08"PRIx32, ntohl(opo->buffer_id));
279 ofp_print_actions(string, opo->u.actions, len - sizeof *opo);
281 ds_put_char(string, '\n');
284 /* qsort comparison function. */
286 compare_ports(const void *a_, const void *b_)
288 const struct ofp_phy_port *a = a_;
289 const struct ofp_phy_port *b = b_;
290 uint16_t ap = ntohs(a->port_no);
291 uint16_t bp = ntohs(b->port_no);
293 return ap < bp ? -1 : ap > bp;
297 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
299 uint8_t name[OFP_MAX_PORT_NAME_LEN];
302 memcpy(name, port->name, sizeof name);
303 for (j = 0; j < sizeof name - 1; j++) {
304 if (!isprint(name[j])) {
310 ds_put_char(string, ' ');
311 ofp_print_port_name(string, ntohs(port->port_no));
312 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", speed:%d, flags:%#x, "
314 ETH_ADDR_ARGS(port->hw_addr), ntohl(port->speed),
315 ntohl(port->flags), ntohl(port->features));
318 /* Pretty-print the struct ofp_switch_features of 'len' bytes at 'oh' to
319 * 'string' at the given 'verbosity' level. */
321 ofp_print_switch_features(struct ds *string, const void *oh, size_t len,
324 const struct ofp_switch_features *osf = oh;
325 struct ofp_phy_port port_list[OFPP_MAX];
329 ds_put_format(string, "dp id:%"PRIx64"\n", ntohll(osf->datapath_id));
330 ds_put_format(string, "tables: exact:%d, compressed:%d, general:%d\n",
332 ntohl(osf->n_compression), ntohl(osf->n_general));
333 ds_put_format(string, "buffers: size:%d, number:%d\n",
334 ntohl(osf->buffer_mb), ntohl(osf->n_buffers));
335 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
336 ntohl(osf->capabilities), ntohl(osf->actions));
338 if (ntohs(osf->header.length) >= sizeof *osf) {
339 len = MIN(len, ntohs(osf->header.length));
341 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
343 memcpy(port_list, osf->ports, (len - sizeof *osf));
344 qsort(port_list, n_ports, sizeof port_list[0], compare_ports);
345 for (i = 0; i < n_ports; i++) {
346 ofp_print_phy_port(string, &port_list[i]);
350 /* Pretty-print the struct ofp_switch_config of 'len' bytes at 'oh' to 'string'
351 * at the given 'verbosity' level. */
353 ofp_print_switch_config(struct ds *string, const void *oh, size_t len,
356 const struct ofp_switch_config *osc = oh;
359 flags = ntohs(osc->flags);
360 if (flags & OFPC_SEND_FLOW_EXP) {
361 flags &= ~OFPC_SEND_FLOW_EXP;
362 ds_put_format(string, " (sending flow expirations)");
365 ds_put_format(string, " ***unknown flags %04"PRIx16"***", flags);
368 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
371 static void print_wild(struct ds *string, const char *leader, int is_wild,
372 const char *format, ...) __attribute__((format(printf, 4, 5)));
374 static void print_wild(struct ds *string, const char *leader, int is_wild,
375 const char *format, ...)
377 ds_put_cstr(string, leader);
381 va_start(args, format);
382 ds_put_format_valist(string, format, args);
385 ds_put_char(string, '?');
389 /* Pretty-print the ofp_match structure */
390 static void ofp_print_match(struct ds *f, const struct ofp_match *om)
392 uint16_t w = ntohs(om->wildcards);
394 print_wild(f, " inport", w & OFPFW_IN_PORT, "%d", ntohs(om->in_port));
395 print_wild(f, ":vlan", w & OFPFW_DL_VLAN, "%04x", ntohs(om->dl_vlan));
396 print_wild(f, " mac[", w & OFPFW_DL_SRC,
397 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
398 print_wild(f, "->", w & OFPFW_DL_DST,
399 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
400 print_wild(f, "] type", w & OFPFW_DL_TYPE, "%04x", ntohs(om->dl_type));
401 print_wild(f, " ip[", w & OFPFW_NW_SRC, IP_FMT, IP_ARGS(&om->nw_src));
402 print_wild(f, "->", w & OFPFW_NW_DST, IP_FMT, IP_ARGS(&om->nw_dst));
403 print_wild(f, "] proto", w & OFPFW_NW_PROTO, "%u", om->nw_proto);
404 print_wild(f, " tport[", w & OFPFW_TP_SRC, "%d", ntohs(om->tp_src));
405 print_wild(f, "->", w & OFPFW_TP_DST, "%d", ntohs(om->tp_dst));
409 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
410 * at the given 'verbosity' level. */
412 ofp_print_flow_mod(struct ds *string, const void *oh, size_t len,
415 const struct ofp_flow_mod *ofm = oh;
417 ofp_print_match(string, &ofm->match);
418 ds_put_format(string, " cmd:%d idle:%d pri:%d buf:%#x",
419 ntohs(ofm->command), ntohs(ofm->max_idle),
420 ofm->match.wildcards ? ntohs(ofm->priority) : (uint16_t)-1,
421 ntohl(ofm->buffer_id));
422 ofp_print_actions(string, ofm->actions,
423 len - offsetof(struct ofp_flow_mod, actions));
424 ds_put_char(string, '\n');
427 /* Pretty-print the OFPT_FLOW_EXPIRED packet of 'len' bytes at 'oh' to 'string'
428 * at the given 'verbosity' level. */
430 ofp_print_flow_expired(struct ds *string, const void *oh, size_t len,
433 const struct ofp_flow_expired *ofe = oh;
435 ofp_print_match(string, &ofe->match);
436 ds_put_format(string,
437 " pri%"PRIu16" secs%"PRIu32" pkts%"PRIu64" bytes%"PRIu64"\n",
438 ofe->match.wildcards ? ntohs(ofe->priority) : (uint16_t)-1,
439 ntohl(ofe->duration), ntohll(ofe->packet_count),
440 ntohll(ofe->byte_count));
443 /* Pretty-print the OFPT_ERROR_MSG packet of 'len' bytes at 'oh' to 'string'
444 * at the given 'verbosity' level. */
446 ofp_print_error_msg(struct ds *string, const void *oh, size_t len,
449 const struct ofp_error_msg *oem = oh;
451 ds_put_format(string,
452 " type%d code%d\n", ntohs(oem->type), ntohs(oem->code));
455 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'string'
456 * at the given 'verbosity' level. */
458 ofp_print_port_status(struct ds *string, const void *oh, size_t len,
461 const struct ofp_port_status *ops = oh;
463 if (ops->reason == OFPPR_ADD) {
464 ds_put_format(string, "add:");
465 } else if (ops->reason == OFPPR_DELETE) {
466 ds_put_format(string, "del:");
467 } else if (ops->reason == OFPPR_MOD) {
468 ds_put_format(string, "mod:");
470 ds_put_format(string, "err:");
473 ofp_print_phy_port(string, &ops->desc);
477 ofp_flow_stats_request(struct ds *string, const void *oh, size_t len,
480 const struct ofp_flow_stats_request *fsr = oh;
482 if (fsr->table_id == 0xff) {
483 ds_put_format(string, " table_id=any, ");
485 ds_put_format(string, " table_id=%"PRIu8", ", fsr->table_id);
488 ofp_print_match(string, &fsr->match);
492 ofp_flow_stats_reply(struct ds *string, const void *body_, size_t len,
495 const char *body = body_;
496 const char *pos = body;
498 const struct ofp_flow_stats *fs;
499 ptrdiff_t bytes_left = body + len - pos;
502 if (bytes_left < sizeof *fs) {
503 if (bytes_left != 0) {
504 ds_put_format(string, " ***%td leftover bytes at end***",
510 fs = (const void *) pos;
511 length = ntohs(fs->length);
512 if (length < sizeof *fs) {
513 ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
516 } else if (length > bytes_left) {
517 ds_put_format(string,
518 " ***length=%zu but only %td bytes left***",
521 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
522 ds_put_format(string,
523 " ***length=%zu has %zu bytes leftover in "
526 (length - sizeof *fs) % sizeof fs->actions[0]);
530 ds_put_format(string, " duration=%"PRIu32"s, ", ntohl(fs->duration));
531 ds_put_format(string, "table_id=%"PRIu8", ", fs->table_id);
532 ds_put_format(string, "priority=%"PRIu16", ",
533 fs->match.wildcards ? ntohs(fs->priority) : (uint16_t)-1);
534 ds_put_format(string, "n_packets=%"PRIu64", ",
535 ntohll(fs->packet_count));
536 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
537 ds_put_format(string, "max_idle=%"PRIu16",", ntohs(fs->max_idle));
538 ofp_print_match(string, &fs->match);
539 ofp_print_actions(string, fs->actions, length - sizeof *fs);
540 ds_put_char(string, '\n');
547 ofp_aggregate_stats_request(struct ds *string, const void *oh, size_t len,
550 const struct ofp_aggregate_stats_request *asr = oh;
552 if (asr->table_id == 0xff) {
553 ds_put_format(string, " table_id=any, ");
555 ds_put_format(string, " table_id=%"PRIu8", ", asr->table_id);
558 ofp_print_match(string, &asr->match);
562 ofp_aggregate_stats_reply(struct ds *string, const void *body_, size_t len,
565 const struct ofp_aggregate_stats_reply *asr = body_;
567 ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
568 ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
569 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
573 ofp_port_stats_reply(struct ds *string, const void *body, size_t len,
576 const struct ofp_port_stats *ps = body;
577 size_t n = len / sizeof *ps;
578 ds_put_format(string, " %zu ports\n", n);
584 ds_put_format(string, " port %"PRIu16": ", ntohs(ps->port_no));
585 ds_put_format(string, "rx %"PRIu64", ", ntohll(ps->rx_count));
586 ds_put_format(string, "tx %"PRIu64", ", ntohll(ps->tx_count));
587 ds_put_format(string, "dropped %"PRIu64"\n", ntohll(ps->drop_count));
592 ofp_table_stats_reply(struct ds *string, const void *body, size_t len,
595 const struct ofp_table_stats *ts = body;
596 size_t n = len / sizeof *ts;
597 ds_put_format(string, " %zu tables\n", n);
603 char name[OFP_MAX_TABLE_NAME_LEN + 1];
604 strncpy(name, ts->name, sizeof name);
605 name[OFP_MAX_TABLE_NAME_LEN] = '\0';
607 ds_put_format(string, " table %"PRIu8": ", ts->table_id);
608 ds_put_format(string, "name %-8s, ", name);
609 ds_put_format(string, "max %6"PRIu32", ", ntohl(ts->max_entries));
610 ds_put_format(string, "active %6"PRIu32", ", ntohl(ts->active_count));
611 ds_put_format(string, "matched %6"PRIu64"\n",
612 ntohll(ts->matched_count));
616 enum stats_direction {
622 print_stats(struct ds *string, int type, const void *body, size_t body_len,
623 int verbosity, enum stats_direction direction)
626 size_t min_body, max_body;
627 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
632 struct stats_msg request;
633 struct stats_msg reply;
636 static const struct stats_type stats_types[] = {
639 { sizeof(struct ofp_flow_stats_request),
640 sizeof(struct ofp_flow_stats_request),
641 ofp_flow_stats_request },
642 { 0, SIZE_MAX, ofp_flow_stats_reply },
644 [OFPST_AGGREGATE] = {
646 { sizeof(struct ofp_aggregate_stats_request),
647 sizeof(struct ofp_aggregate_stats_request),
648 ofp_aggregate_stats_request },
649 { sizeof(struct ofp_aggregate_stats_reply),
650 sizeof(struct ofp_aggregate_stats_reply),
651 ofp_aggregate_stats_reply },
656 { 0, SIZE_MAX, ofp_table_stats_reply },
661 { 0, SIZE_MAX, ofp_port_stats_reply },
665 const struct stats_type *s;
666 const struct stats_msg *m;
668 if (type >= ARRAY_SIZE(stats_types) || !stats_types[type].name) {
669 ds_put_format(string, " ***unknown type %d***", type);
672 s = &stats_types[type];
673 ds_put_format(string, " type=%d(%s)\n", type, s->name);
675 m = direction == REQUEST ? &s->request : &s->reply;
676 if (body_len < m->min_body || body_len > m->max_body) {
677 ds_put_format(string, " ***body_len=%zu not in %zu...%zu***",
678 body_len, m->min_body, m->max_body);
682 m->printer(string, body, body_len, verbosity);
687 ofp_stats_request(struct ds *string, const void *oh, size_t len, int verbosity)
689 const struct ofp_stats_request *srq = oh;
692 ds_put_format(string, " ***unknown flags %04"PRIx16"***",
696 print_stats(string, ntohs(srq->type), srq->body,
697 len - offsetof(struct ofp_stats_request, body),
702 ofp_stats_reply(struct ds *string, const void *oh, size_t len, int verbosity)
704 const struct ofp_stats_reply *srp = oh;
706 ds_put_cstr(string, " flags=");
708 ds_put_cstr(string, "none");
710 uint16_t flags = ntohs(srp->flags);
711 if (flags & OFPSF_REPLY_MORE) {
712 ds_put_cstr(string, "[more]");
713 flags &= ~OFPSF_REPLY_MORE;
716 ds_put_format(string, "[***unknown%04"PRIx16"***]", flags);
720 print_stats(string, ntohs(srp->type), srp->body,
721 len - offsetof(struct ofp_stats_reply, body),
725 struct openflow_packet {
728 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
731 static const struct openflow_packet packets[] = {
732 [OFPT_FEATURES_REQUEST] = {
734 sizeof (struct ofp_header),
737 [OFPT_FEATURES_REPLY] = {
739 sizeof (struct ofp_switch_features),
740 ofp_print_switch_features,
742 [OFPT_GET_CONFIG_REQUEST] = {
743 "get_config_request",
744 sizeof (struct ofp_header),
747 [OFPT_GET_CONFIG_REPLY] = {
749 sizeof (struct ofp_switch_config),
750 ofp_print_switch_config,
752 [OFPT_SET_CONFIG] = {
754 sizeof (struct ofp_switch_config),
755 ofp_print_switch_config,
759 offsetof(struct ofp_packet_in, data),
762 [OFPT_PACKET_OUT] = {
764 sizeof (struct ofp_packet_out),
769 sizeof (struct ofp_flow_mod),
772 [OFPT_FLOW_EXPIRED] = {
774 sizeof (struct ofp_flow_expired),
775 ofp_print_flow_expired,
779 sizeof (struct ofp_port_mod),
782 [OFPT_PORT_STATUS] = {
784 sizeof (struct ofp_port_status),
785 ofp_print_port_status
789 sizeof (struct ofp_error_msg),
792 [OFPT_STATS_REQUEST] = {
794 sizeof (struct ofp_stats_request),
797 [OFPT_STATS_REPLY] = {
799 sizeof (struct ofp_stats_reply),
804 /* Composes and returns a string representing the OpenFlow packet of 'len'
805 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
806 * verbosity and higher numbers increase verbosity. The caller is responsible
807 * for freeing the string. */
809 ofp_to_string(const void *oh_, size_t len, int verbosity)
811 struct ds string = DS_EMPTY_INITIALIZER;
812 const struct ofp_header *oh = oh_;
813 const struct openflow_packet *pkt;
815 if (len < sizeof(struct ofp_header)) {
816 ds_put_cstr(&string, "OpenFlow packet too short:\n");
817 ds_put_hex_dump(&string, oh, len, 0, true);
818 return ds_cstr(&string);
819 } else if (oh->version != OFP_VERSION) {
820 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", oh->version);
821 ds_put_hex_dump(&string, oh, len, 0, true);
822 return ds_cstr(&string);
823 } else if (oh->type >= ARRAY_SIZE(packets) || !packets[oh->type].name) {
824 ds_put_format(&string, "Unknown OpenFlow packet type %"PRIu8":\n",
826 ds_put_hex_dump(&string, oh, len, 0, true);
827 return ds_cstr(&string);
830 pkt = &packets[oh->type];
831 ds_put_format(&string, "%s (xid=%"PRIx32"):", pkt->name, oh->xid);
833 if (ntohs(oh->length) > len)
834 ds_put_format(&string, " (***truncated to %zu bytes from %"PRIu16"***)",
835 len, ntohs(oh->length));
836 else if (ntohs(oh->length) < len) {
837 ds_put_format(&string, " (***only uses %"PRIu16" bytes out of %zu***)\n",
838 ntohs(oh->length), len);
839 len = ntohs(oh->length);
842 if (len < pkt->min_size) {
843 ds_put_format(&string, " (***length=%zu < min_size=%zu***)\n",
845 } else if (!pkt->printer) {
846 if (len > sizeof *oh) {
847 ds_put_format(&string, " length=%"PRIu16" (decoder not implemented)\n",
851 pkt->printer(&string, oh, len, verbosity);
853 if (verbosity >= 3) {
854 ds_put_hex_dump(&string, oh, len, 0, true);
856 if (string.string[string.length - 1] != '\n') {
857 ds_put_char(&string, '\n');
859 return ds_cstr(&string);
863 print_and_free(FILE *stream, char *string)
865 fputs(string, stream);
869 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
870 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
871 * numbers increase verbosity. */
873 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
875 print_and_free(stream, ofp_to_string(oh, len, verbosity));
878 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
879 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
880 * the Ethernet frame (of which 'len' bytes were captured).
882 * This starts and kills a tcpdump subprocess so it's quite expensive. */
884 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
886 print_and_free(stream, ofp_packet_to_string(data, len, total_len));