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_VLAN_VID:
270 ds_put_format(string, "mod_vlan_vid:%"PRIu16, ntohs(a->arg.vlan_vid));
273 case OFPAT_SET_VLAN_PCP:
274 ds_put_format(string, "mod_vlan_pcp:%"PRIu8, a->arg.vlan_pcp);
277 case OFPAT_STRIP_VLAN:
278 ds_put_cstr(string, "strip_vlan");
281 case OFPAT_SET_DL_SRC:
282 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
283 ETH_ADDR_ARGS(a->arg.dl_addr));
286 case OFPAT_SET_DL_DST:
287 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
288 ETH_ADDR_ARGS(a->arg.dl_addr));
291 case OFPAT_SET_NW_SRC:
292 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&a->arg.nw_addr));
295 case OFPAT_SET_NW_DST:
296 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&a->arg.nw_addr));
299 case OFPAT_SET_TP_SRC:
300 ds_put_format(string, "mod_tp_src:%d", ntohs(a->arg.tp));
303 case OFPAT_SET_TP_DST:
304 ds_put_format(string, "mod_tp_dst:%d", ntohs(a->arg.tp));
308 ds_put_format(string, "(decoder %"PRIu16" not implemented)",
314 static void ofp_print_actions(struct ds *string,
315 const struct ofp_action actions[],
319 int n_actions = n_bytes / sizeof *actions;
321 ds_put_format(string, "action%s=", n_actions == 1 ? "" : "s");
322 for (i = 0; i < n_actions; i++) {
324 ds_put_cstr(string, ",");
326 ofp_print_action(string, &actions[i]);
328 if (n_bytes % sizeof *actions) {
330 ds_put_cstr(string, ",");
332 ds_put_cstr(string, ", ***trailing garbage***");
336 /* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'string'
337 * at the given 'verbosity' level. */
338 static void ofp_packet_out(struct ds *string, const void *oh, size_t len,
341 const struct ofp_packet_out *opo = oh;
342 int n_actions = ntohs(opo->n_actions);
343 int act_len = n_actions * sizeof opo->actions[0];
345 ds_put_cstr(string, " in_port=");
346 ofp_print_port_name(string, ntohs(opo->in_port));
348 ds_put_format(string, " n_actions=%d ", n_actions);
349 if (act_len > (ntohs(opo->header.length) - sizeof *opo)) {
350 ds_put_format(string, "***packet too short for number of actions***\n");
353 ofp_print_actions(string, opo->actions, act_len);
355 if (ntohl(opo->buffer_id) == UINT32_MAX) {
356 int data_len = len - sizeof *opo - act_len;
357 ds_put_format(string, " data_len=%d", data_len);
358 if (verbosity > 0 && len > sizeof *opo) {
359 char *packet = ofp_packet_to_string(&opo->actions[n_actions],
361 ds_put_char(string, '\n');
362 ds_put_cstr(string, packet);
366 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
368 ds_put_char(string, '\n');
371 /* qsort comparison function. */
373 compare_ports(const void *a_, const void *b_)
375 const struct ofp_phy_port *a = a_;
376 const struct ofp_phy_port *b = b_;
377 uint16_t ap = ntohs(a->port_no);
378 uint16_t bp = ntohs(b->port_no);
380 return ap < bp ? -1 : ap > bp;
383 static void ofp_print_port_features(struct ds *string, uint32_t features)
386 ds_put_cstr(string, "Unsupported\n");
389 if (features & OFPPF_10MB_HD) {
390 ds_put_cstr(string, "10MB-HD ");
392 if (features & OFPPF_10MB_FD) {
393 ds_put_cstr(string, "10MB-FD ");
395 if (features & OFPPF_100MB_HD) {
396 ds_put_cstr(string, "100MB-HD ");
398 if (features & OFPPF_100MB_FD) {
399 ds_put_cstr(string, "100MB-FD ");
401 if (features & OFPPF_1GB_HD) {
402 ds_put_cstr(string, "1GB-HD ");
404 if (features & OFPPF_1GB_FD) {
405 ds_put_cstr(string, "1GB-FD ");
407 if (features & OFPPF_10GB_FD) {
408 ds_put_cstr(string, "10GB-FD ");
410 if (features & OFPPF_COPPER) {
411 ds_put_cstr(string, "COPPER ");
413 if (features & OFPPF_FIBER) {
414 ds_put_cstr(string, "FIBER ");
416 if (features & OFPPF_AUTONEG) {
417 ds_put_cstr(string, "AUTO_NEG ");
419 if (features & OFPPF_PAUSE) {
420 ds_put_cstr(string, "AUTO_PAUSE ");
422 if (features & OFPPF_PAUSE_ASYM) {
423 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
425 ds_put_char(string, '\n');
429 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
431 uint8_t name[OFP_MAX_PORT_NAME_LEN];
434 memcpy(name, port->name, sizeof name);
435 for (j = 0; j < sizeof name - 1; j++) {
436 if (!isprint(name[j])) {
442 ds_put_char(string, ' ');
443 ofp_print_port_name(string, ntohs(port->port_no));
444 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
445 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
448 ds_put_format(string, " current: ");
449 ofp_print_port_features(string, ntohl(port->curr));
451 if (port->advertised) {
452 ds_put_format(string, " advertised: ");
453 ofp_print_port_features(string, ntohl(port->advertised));
455 if (port->supported) {
456 ds_put_format(string, " supported: ");
457 ofp_print_port_features(string, ntohl(port->supported));
460 ds_put_format(string, " peer: ");
461 ofp_print_port_features(string, ntohl(port->peer));
465 /* Pretty-print the struct ofp_switch_features of 'len' bytes at 'oh' to
466 * 'string' at the given 'verbosity' level. */
468 ofp_print_switch_features(struct ds *string, const void *oh, size_t len,
471 const struct ofp_switch_features *osf = oh;
472 struct ofp_phy_port port_list[OFPP_MAX];
476 ds_put_format(string, "dp id:%"PRIx64"\n", ntohll(osf->datapath_id));
477 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
478 ntohl(osf->n_buffers));
479 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
480 ntohl(osf->capabilities), ntohl(osf->actions));
482 if (ntohs(osf->header.length) >= sizeof *osf) {
483 len = MIN(len, ntohs(osf->header.length));
485 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
487 memcpy(port_list, osf->ports, (len - sizeof *osf));
488 qsort(port_list, n_ports, sizeof port_list[0], compare_ports);
489 for (i = 0; i < n_ports; i++) {
490 ofp_print_phy_port(string, &port_list[i]);
494 /* Pretty-print the struct ofp_switch_config of 'len' bytes at 'oh' to 'string'
495 * at the given 'verbosity' level. */
497 ofp_print_switch_config(struct ds *string, const void *oh, size_t len,
500 const struct ofp_switch_config *osc = oh;
503 flags = ntohs(osc->flags);
504 if (flags & OFPC_SEND_FLOW_EXP) {
505 flags &= ~OFPC_SEND_FLOW_EXP;
506 ds_put_format(string, " (sending flow expirations)");
509 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
512 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
515 static void print_wild(struct ds *string, const char *leader, int is_wild,
516 int verbosity, const char *format, ...)
517 __attribute__((format(printf, 5, 6)));
519 static void print_wild(struct ds *string, const char *leader, int is_wild,
520 int verbosity, const char *format, ...)
522 if (is_wild && verbosity < 2) {
525 ds_put_cstr(string, leader);
529 va_start(args, format);
530 ds_put_format_valist(string, format, args);
533 ds_put_char(string, '*');
535 ds_put_char(string, ',');
539 print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
540 uint32_t wild_bits, int verbosity)
542 if (wild_bits >= 32 && verbosity < 2) {
545 ds_put_cstr(string, leader);
546 if (wild_bits < 32) {
547 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
549 ds_put_format(string, "/%d", 32 - wild_bits);
552 ds_put_char(string, '*');
554 ds_put_char(string, ',');
557 /* Pretty-print the ofp_match structure */
558 static void ofp_print_match(struct ds *f, const struct ofp_match *om,
561 uint32_t w = ntohl(om->wildcards);
562 bool skip_type = false;
563 bool skip_proto = false;
565 if (!(w & OFPFW_DL_TYPE)) {
567 if (om->dl_type == htons(ETH_TYPE_IP)) {
568 if (!(w & OFPFW_NW_PROTO)) {
570 if (om->nw_proto == IP_TYPE_ICMP) {
571 ds_put_cstr(f, "icmp,");
572 } else if (om->nw_proto == IP_TYPE_TCP) {
573 ds_put_cstr(f, "tcp,");
574 } else if (om->nw_proto == IP_TYPE_UDP) {
575 ds_put_cstr(f, "udp,");
577 ds_put_cstr(f, "ip,");
581 ds_put_cstr(f, "ip,");
583 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
584 ds_put_cstr(f, "arp,");
589 print_wild(f, "in_port=", w & OFPFW_IN_PORT, verbosity,
590 "%d", ntohs(om->in_port));
591 print_wild(f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
592 "0x%04x", ntohs(om->dl_vlan));
593 print_wild(f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
594 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
595 print_wild(f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
596 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
598 print_wild(f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
599 "0x%04x", ntohs(om->dl_type));
601 print_ip_netmask(f, "nw_src=", om->nw_src,
602 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
603 print_ip_netmask(f, "nw_dst=", om->nw_dst,
604 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
606 print_wild(f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
609 print_wild(f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
610 "%d", ntohs(om->tp_src));
611 print_wild(f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
612 "%d", ntohs(om->tp_dst));
615 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
616 * at the given 'verbosity' level. */
618 ofp_print_flow_mod(struct ds *string, const void *oh, size_t len,
621 const struct ofp_flow_mod *ofm = oh;
623 ofp_print_match(string, &ofm->match, verbosity);
624 switch (ntohs(ofm->command)) {
626 ds_put_cstr(string, " ADD: ");
629 ds_put_cstr(string, " MOD: ");
631 case OFPFC_MODIFY_STRICT:
632 ds_put_cstr(string, " MOD_STRICT: ");
635 ds_put_cstr(string, " DEL: ");
637 case OFPFC_DELETE_STRICT:
638 ds_put_cstr(string, " DEL_STRICT: ");
641 ds_put_format(string, " cmd:%d ", ntohs(ofm->command));
643 ds_put_format(string, "idle:%d hard:%d pri:%d buf:%#x",
644 ntohs(ofm->idle_timeout), ntohs(ofm->hard_timeout),
645 ofm->match.wildcards ? ntohs(ofm->priority) : (uint16_t)-1,
646 ntohl(ofm->buffer_id));
647 ofp_print_actions(string, ofm->actions,
648 len - offsetof(struct ofp_flow_mod, actions));
649 ds_put_char(string, '\n');
652 /* Pretty-print the OFPT_FLOW_EXPIRED packet of 'len' bytes at 'oh' to 'string'
653 * at the given 'verbosity' level. */
655 ofp_print_flow_expired(struct ds *string, const void *oh, size_t len,
658 const struct ofp_flow_expired *ofe = oh;
660 ofp_print_match(string, &ofe->match, verbosity);
661 ds_put_cstr(string, " reason=");
662 switch (ofe->reason) {
663 case OFPER_IDLE_TIMEOUT:
664 ds_put_cstr(string, "idle");
666 case OFPER_HARD_TIMEOUT:
667 ds_put_cstr(string, "hard");
670 ds_put_format(string, "**%"PRIu8"**", ofe->reason);
673 ds_put_format(string,
674 " pri%"PRIu16" secs%"PRIu32" pkts%"PRIu64" bytes%"PRIu64"\n",
675 ofe->match.wildcards ? ntohs(ofe->priority) : (uint16_t)-1,
676 ntohl(ofe->duration), ntohll(ofe->packet_count),
677 ntohll(ofe->byte_count));
681 ofp_print_port_mod(struct ds *string, const void *oh, size_t len,
684 const struct ofp_port_mod *opm = oh;
686 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
687 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
688 ntohl(opm->config), ntohl(opm->mask));
689 ds_put_format(string, " advertise: ");
690 if (opm->advertise) {
691 ofp_print_port_features(string, ntohl(opm->advertise));
693 ds_put_format(string, "UNCHANGED\n");
703 static const struct error_type error_types[] = {
704 #define ERROR_TYPE(TYPE) {TYPE, -1, #TYPE}
705 #define ERROR_CODE(TYPE, CODE) {TYPE, CODE, #CODE}
706 ERROR_TYPE(OFPET_HELLO_FAILED),
707 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_INCOMPATIBLE),
709 ERROR_TYPE(OFPET_BAD_REQUEST),
710 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
711 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE),
712 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT),
713 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
715 #define N_ERROR_TYPES ARRAY_SIZE(error_types)
718 lookup_error_type(int type)
720 const struct error_type *t;
722 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
723 if (t->type == type && t->code == -1) {
731 lookup_error_code(int type, int code)
733 const struct error_type *t;
735 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
736 if (t->type == type && t->code == code) {
743 /* Pretty-print the OFPT_ERROR packet of 'len' bytes at 'oh' to 'string'
744 * at the given 'verbosity' level. */
746 ofp_print_error_msg(struct ds *string, const void *oh, size_t len,
749 const struct ofp_error_msg *oem = oh;
750 int type = ntohs(oem->type);
751 int code = ntohs(oem->code);
754 ds_put_format(string, " type%d(%s) code%d(%s) payload:\n",
755 type, lookup_error_type(type),
756 code, lookup_error_code(type, code));
759 case OFPET_HELLO_FAILED:
760 ds_put_printable(string, (char *) oem->data, len - sizeof *oem);
763 case OFPET_BAD_REQUEST:
764 s = ofp_to_string(oem->data, len - sizeof *oem, 1);
765 ds_put_cstr(string, s);
770 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
775 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'string'
776 * at the given 'verbosity' level. */
778 ofp_print_port_status(struct ds *string, const void *oh, size_t len,
781 const struct ofp_port_status *ops = oh;
783 if (ops->reason == OFPPR_ADD) {
784 ds_put_format(string, " ADD:");
785 } else if (ops->reason == OFPPR_DELETE) {
786 ds_put_format(string, " DEL:");
787 } else if (ops->reason == OFPPR_MODIFY) {
788 ds_put_format(string, " MOD:");
791 ofp_print_phy_port(string, &ops->desc);
795 ofp_desc_stats_reply(struct ds *string, const void *body, size_t len,
798 const struct ofp_desc_stats *ods = body;
800 ds_put_format(string, "Manufacturer: %s\n", ods->mfr_desc);
801 ds_put_format(string, "Hardware: %s\n", ods->hw_desc);
802 ds_put_format(string, "Software: %s\n", ods->sw_desc);
803 ds_put_format(string, "Serial Num: %s\n", ods->serial_num);
807 ofp_flow_stats_request(struct ds *string, const void *oh, size_t len,
810 const struct ofp_flow_stats_request *fsr = oh;
812 if (fsr->table_id == 0xff) {
813 ds_put_format(string, " table_id=any, ");
815 ds_put_format(string, " table_id=%"PRIu8", ", fsr->table_id);
818 ofp_print_match(string, &fsr->match, verbosity);
822 ofp_flow_stats_reply(struct ds *string, const void *body_, size_t len,
825 const char *body = body_;
826 const char *pos = body;
828 const struct ofp_flow_stats *fs;
829 ptrdiff_t bytes_left = body + len - pos;
832 if (bytes_left < sizeof *fs) {
833 if (bytes_left != 0) {
834 ds_put_format(string, " ***%td leftover bytes at end***",
840 fs = (const void *) pos;
841 length = ntohs(fs->length);
842 if (length < sizeof *fs) {
843 ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
846 } else if (length > bytes_left) {
847 ds_put_format(string,
848 " ***length=%zu but only %td bytes left***",
851 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
852 ds_put_format(string,
853 " ***length=%zu has %zu bytes leftover in "
856 (length - sizeof *fs) % sizeof fs->actions[0]);
860 ds_put_format(string, " duration=%"PRIu32"s, ", ntohl(fs->duration));
861 ds_put_format(string, "table_id=%"PRIu8", ", fs->table_id);
862 ds_put_format(string, "priority=%"PRIu16", ",
863 fs->match.wildcards ? ntohs(fs->priority) : (uint16_t)-1);
864 ds_put_format(string, "n_packets=%"PRIu64", ",
865 ntohll(fs->packet_count));
866 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
867 ds_put_format(string, "idle_timeout=%"PRIu16",",
868 ntohs(fs->idle_timeout));
869 ds_put_format(string, "hard_timeout=%"PRIu16",",
870 ntohs(fs->hard_timeout));
871 ofp_print_match(string, &fs->match, verbosity);
872 ofp_print_actions(string, fs->actions, length - sizeof *fs);
873 ds_put_char(string, '\n');
880 ofp_aggregate_stats_request(struct ds *string, const void *oh, size_t len,
883 const struct ofp_aggregate_stats_request *asr = oh;
885 if (asr->table_id == 0xff) {
886 ds_put_format(string, " table_id=any, ");
888 ds_put_format(string, " table_id=%"PRIu8", ", asr->table_id);
891 ofp_print_match(string, &asr->match, verbosity);
895 ofp_aggregate_stats_reply(struct ds *string, const void *body_, size_t len,
898 const struct ofp_aggregate_stats_reply *asr = body_;
900 ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
901 ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
902 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
905 static void print_port_stat(struct ds *string, const char *leader,
906 uint64_t stat, int more)
908 ds_put_cstr(string, leader);
910 ds_put_format(string, "%"PRIu64, stat);
912 ds_put_char(string, '?');
915 ds_put_cstr(string, ", ");
917 ds_put_cstr(string, "\n");
922 ofp_port_stats_reply(struct ds *string, const void *body, size_t len,
925 const struct ofp_port_stats *ps = body;
926 size_t n = len / sizeof *ps;
927 ds_put_format(string, " %zu ports\n", n);
933 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
935 ds_put_cstr(string, "rx ");
936 print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
937 print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
938 print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
939 print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
940 print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
941 print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
942 print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
944 ds_put_cstr(string, " tx ");
945 print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
946 print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
947 print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
948 print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
949 print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
954 ofp_table_stats_reply(struct ds *string, const void *body, size_t len,
957 const struct ofp_table_stats *ts = body;
958 size_t n = len / sizeof *ts;
959 ds_put_format(string, " %zu tables\n", n);
965 char name[OFP_MAX_TABLE_NAME_LEN + 1];
966 strncpy(name, ts->name, sizeof name);
967 name[OFP_MAX_TABLE_NAME_LEN] = '\0';
969 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
970 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
971 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
972 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
973 ds_put_cstr(string, " ");
974 ds_put_format(string, "lookup=%"PRIu64", ",
975 ntohll(ts->lookup_count));
976 ds_put_format(string, "matched=%"PRIu64"\n",
977 ntohll(ts->matched_count));
982 vendor_stat(struct ds *string, const void *body, size_t len,
983 int verbosity UNUSED)
985 ds_put_format(string, " vendor=%08"PRIx32, ntohl(*(uint32_t *) body));
986 ds_put_format(string, " %zu bytes additional data",
987 len - sizeof(uint32_t));
990 enum stats_direction {
996 print_stats(struct ds *string, int type, const void *body, size_t body_len,
997 int verbosity, enum stats_direction direction)
1000 size_t min_body, max_body;
1001 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1007 struct stats_msg request;
1008 struct stats_msg reply;
1011 static const struct stats_type stats_types[] = {
1016 { 0, SIZE_MAX, ofp_desc_stats_reply },
1021 { sizeof(struct ofp_flow_stats_request),
1022 sizeof(struct ofp_flow_stats_request),
1023 ofp_flow_stats_request },
1024 { 0, SIZE_MAX, ofp_flow_stats_reply },
1029 { sizeof(struct ofp_aggregate_stats_request),
1030 sizeof(struct ofp_aggregate_stats_request),
1031 ofp_aggregate_stats_request },
1032 { sizeof(struct ofp_aggregate_stats_reply),
1033 sizeof(struct ofp_aggregate_stats_reply),
1034 ofp_aggregate_stats_reply },
1040 { 0, SIZE_MAX, ofp_table_stats_reply },
1046 { 0, SIZE_MAX, ofp_port_stats_reply },
1051 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1052 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1062 const struct stats_type *s;
1063 const struct stats_msg *m;
1065 if (type >= ARRAY_SIZE(stats_types) || !stats_types[type].name) {
1066 ds_put_format(string, " ***unknown type %d***", type);
1069 for (s = stats_types; s->type >= 0; s++) {
1070 if (s->type == type) {
1074 ds_put_format(string, " type=%d(%s)\n", type, s->name);
1076 m = direction == REQUEST ? &s->request : &s->reply;
1077 if (body_len < m->min_body || body_len > m->max_body) {
1078 ds_put_format(string, " ***body_len=%zu not in %zu...%zu***",
1079 body_len, m->min_body, m->max_body);
1083 m->printer(string, body, body_len, verbosity);
1088 ofp_stats_request(struct ds *string, const void *oh, size_t len, int verbosity)
1090 const struct ofp_stats_request *srq = oh;
1093 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1097 print_stats(string, ntohs(srq->type), srq->body,
1098 len - offsetof(struct ofp_stats_request, body),
1099 verbosity, REQUEST);
1103 ofp_stats_reply(struct ds *string, const void *oh, size_t len, int verbosity)
1105 const struct ofp_stats_reply *srp = oh;
1107 ds_put_cstr(string, " flags=");
1109 ds_put_cstr(string, "none");
1111 uint16_t flags = ntohs(srp->flags);
1112 if (flags & OFPSF_REPLY_MORE) {
1113 ds_put_cstr(string, "[more]");
1114 flags &= ~OFPSF_REPLY_MORE;
1117 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]", flags);
1121 print_stats(string, ntohs(srp->type), srp->body,
1122 len - offsetof(struct ofp_stats_reply, body),
1127 ofp_echo(struct ds *string, const void *oh, size_t len, int verbosity)
1129 const struct ofp_header *hdr = oh;
1131 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *hdr);
1132 if (verbosity > 1) {
1133 ds_put_hex_dump(string, hdr, len - sizeof *hdr, 0, true);
1137 struct openflow_packet {
1141 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1144 static const struct openflow_packet packets[] = {
1148 sizeof (struct ofp_header),
1152 OFPT_FEATURES_REQUEST,
1154 sizeof (struct ofp_header),
1158 OFPT_FEATURES_REPLY,
1160 sizeof (struct ofp_switch_features),
1161 ofp_print_switch_features,
1164 OFPT_GET_CONFIG_REQUEST,
1165 "get_config_request",
1166 sizeof (struct ofp_header),
1170 OFPT_GET_CONFIG_REPLY,
1172 sizeof (struct ofp_switch_config),
1173 ofp_print_switch_config,
1178 sizeof (struct ofp_switch_config),
1179 ofp_print_switch_config,
1184 offsetof(struct ofp_packet_in, data),
1190 sizeof (struct ofp_packet_out),
1196 sizeof (struct ofp_flow_mod),
1202 sizeof (struct ofp_flow_expired),
1203 ofp_print_flow_expired,
1208 sizeof (struct ofp_port_mod),
1214 sizeof (struct ofp_port_status),
1215 ofp_print_port_status
1220 sizeof (struct ofp_error_msg),
1221 ofp_print_error_msg,
1226 sizeof (struct ofp_stats_request),
1232 sizeof (struct ofp_stats_reply),
1238 sizeof (struct ofp_header),
1244 sizeof (struct ofp_header),
1249 /* Composes and returns a string representing the OpenFlow packet of 'len'
1250 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1251 * verbosity and higher numbers increase verbosity. The caller is responsible
1252 * for freeing the string. */
1254 ofp_to_string(const void *oh_, size_t len, int verbosity)
1256 struct ds string = DS_EMPTY_INITIALIZER;
1257 const struct ofp_header *oh = oh_;
1258 const struct openflow_packet *pkt;
1260 if (len < sizeof(struct ofp_header)) {
1261 ds_put_cstr(&string, "OpenFlow packet too short:\n");
1262 ds_put_hex_dump(&string, oh, len, 0, true);
1263 return ds_cstr(&string);
1264 } else if (oh->version != OFP_VERSION) {
1265 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", oh->version);
1266 ds_put_hex_dump(&string, oh, len, 0, true);
1267 return ds_cstr(&string);
1270 for (pkt = packets; ; pkt++) {
1271 if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1272 ds_put_format(&string, "Unknown OpenFlow packet type %"PRIu8":\n",
1274 ds_put_hex_dump(&string, oh, len, 0, true);
1275 return ds_cstr(&string);
1276 } else if (oh->type == pkt->type) {
1281 ds_put_format(&string, "%s (xid=0x%"PRIx32"):", pkt->name, oh->xid);
1283 if (ntohs(oh->length) > len)
1284 ds_put_format(&string, " (***truncated to %zu bytes from %"PRIu16"***)",
1285 len, ntohs(oh->length));
1286 else if (ntohs(oh->length) < len) {
1287 ds_put_format(&string, " (***only uses %"PRIu16" bytes out of %zu***)\n",
1288 ntohs(oh->length), len);
1289 len = ntohs(oh->length);
1292 if (len < pkt->min_size) {
1293 ds_put_format(&string, " (***length=%zu < min_size=%zu***)\n",
1294 len, pkt->min_size);
1295 } else if (!pkt->printer) {
1296 if (len > sizeof *oh) {
1297 ds_put_format(&string, " length=%"PRIu16" (decoder not implemented)\n",
1301 pkt->printer(&string, oh, len, verbosity);
1303 if (verbosity >= 3) {
1304 ds_put_hex_dump(&string, oh, len, 0, true);
1306 if (string.string[string.length - 1] != '\n') {
1307 ds_put_char(&string, '\n');
1309 return ds_cstr(&string);
1313 print_and_free(FILE *stream, char *string)
1315 fputs(string, stream);
1319 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1320 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1321 * numbers increase verbosity. */
1323 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1325 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1328 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1329 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1330 * the Ethernet frame (of which 'len' bytes were captured).
1332 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1334 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1336 print_and_free(stream, ofp_packet_to_string(data, len, total_len));