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"
50 #include "openflow/openflow.h"
51 #include "openflow/nicira-ext.h"
55 static void ofp_print_port_name(struct ds *string, uint16_t port);
56 static void ofp_print_match(struct ds *, const struct ofp_match *,
59 /* Returns a string that represents the contents of the Ethernet frame in the
60 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
61 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
62 * bytes were captured).
64 * The caller must free the returned string.
66 * This starts and kills a tcpdump subprocess so it's quite expensive. */
68 ofp_packet_to_string(const void *data, size_t len, size_t total_len)
71 uint32_t magic_number; /* magic number */
72 uint16_t version_major; /* major version number */
73 uint16_t version_minor; /* minor version number */
74 int32_t thiszone; /* GMT to local correction */
75 uint32_t sigfigs; /* accuracy of timestamps */
76 uint32_t snaplen; /* max length of captured packets */
77 uint32_t network; /* data link type */
81 uint32_t ts_sec; /* timestamp seconds */
82 uint32_t ts_usec; /* timestamp microseconds */
83 uint32_t incl_len; /* number of octets of packet saved in file */
84 uint32_t orig_len; /* actual length of packet */
88 struct pcaprec_hdr prh;
90 struct ds ds = DS_EMPTY_INITIALIZER;
100 ofp_error(errno, "tmpfile");
101 return xstrdup("<error>");
104 /* The pcap reader is responsible for figuring out endianness based on the
105 * magic number, so the lack of htonX calls here is intentional. */
106 ph.magic_number = 0xa1b2c3d4;
107 ph.version_major = 2;
108 ph.version_minor = 4;
112 ph.network = 1; /* Ethernet */
117 prh.orig_len = total_len;
119 fwrite(&ph, 1, sizeof ph, pcap);
120 fwrite(&prh, 1, sizeof prh, pcap);
121 fwrite(data, 1, len, pcap);
125 ofp_error(errno, "error writing temporary file");
129 snprintf(command, sizeof command, "/usr/sbin/tcpdump -e -n -r /dev/fd/%d 2>/dev/null",
131 tcpdump = popen(command, "r");
134 ofp_error(errno, "exec(\"%s\")", command);
135 return xstrdup("<error>");
138 while ((c = getc(tcpdump)) != EOF) {
142 status = pclose(tcpdump);
143 if (WIFEXITED(status)) {
144 if (WEXITSTATUS(status))
145 ofp_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
146 } else if (WIFSIGNALED(status)) {
147 ofp_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
152 /* Pretty-print the OFPT_PACKET_IN packet of 'len' bytes at 'oh' to 'stream'
153 * at the given 'verbosity' level. */
155 ofp_packet_in(struct ds *string, const void *oh, size_t len, int verbosity)
157 const struct ofp_packet_in *op = oh;
160 ds_put_format(string, " total_len=%"PRIu16" in_port=",
161 ntohs(op->total_len));
162 ofp_print_port_name(string, ntohs(op->in_port));
164 if (op->reason == OFPR_ACTION)
165 ds_put_cstr(string, " (via action)");
166 else if (op->reason != OFPR_NO_MATCH)
167 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
169 data_len = len - offsetof(struct ofp_packet_in, data);
170 ds_put_format(string, " data_len=%zu", data_len);
171 if (htonl(op->buffer_id) == UINT32_MAX) {
172 ds_put_format(string, " (unbuffered)");
173 if (ntohs(op->total_len) != data_len)
174 ds_put_format(string, " (***total_len != data_len***)");
176 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
177 if (ntohs(op->total_len) < data_len)
178 ds_put_format(string, " (***total_len < data_len***)");
180 ds_put_char(string, '\n');
184 struct ofpbuf packet;
185 struct ofp_match match;
186 packet.data = (void *) op->data;
187 packet.size = data_len;
188 flow_extract(&packet, ntohs(op->in_port), &flow);
190 match.in_port = flow.in_port;
191 memcpy(match.dl_src, flow.dl_src, ETH_ADDR_LEN);
192 memcpy(match.dl_dst, flow.dl_dst, ETH_ADDR_LEN);
193 match.dl_vlan = flow.dl_vlan;
194 match.dl_type = flow.dl_type;
195 match.nw_proto = flow.nw_proto;
197 match.nw_src = flow.nw_src;
198 match.nw_dst = flow.nw_dst;
199 match.tp_src = flow.tp_src;
200 match.tp_dst = flow.tp_dst;
201 ofp_print_match(string, &match, verbosity);
202 ds_put_char(string, '\n');
205 char *packet = ofp_packet_to_string(op->data, data_len,
206 ntohs(op->total_len));
207 ds_put_cstr(string, packet);
212 static void ofp_print_port_name(struct ds *string, uint16_t port)
231 case OFPP_CONTROLLER:
241 ds_put_format(string, "%"PRIu16, port);
244 ds_put_cstr(string, name);
248 ofp_print_nx_action(struct ds *string, const struct nx_action_header *nah)
251 if (nah->subtype == htonl(NXAST_SNAT)) {
252 const struct nx_action_snat *nas = (struct nx_action_snat *)nah;
253 uint16_t port = ntohs(nas->port);
255 if (port < OFPP_MAX) {
256 ds_put_format(string, "nat:%"PRIu16, port);
258 ds_put_format(string, "nat:%"PRIu16" (invalid port)", port);
261 ds_put_format(string, "***unknown Nicira action:%d***\n",
262 ntohl(nah->subtype));
267 ofp_print_action(struct ds *string, const struct ofp_action_header *ah,
273 struct openflow_action {
278 const struct openflow_action of_actions[] = {
280 sizeof(struct ofp_action_output),
281 sizeof(struct ofp_action_output),
283 [OFPAT_SET_VLAN_VID] = {
284 sizeof(struct ofp_action_vlan_vid),
285 sizeof(struct ofp_action_vlan_vid),
287 [OFPAT_SET_VLAN_PCP] = {
288 sizeof(struct ofp_action_vlan_pcp),
289 sizeof(struct ofp_action_vlan_pcp),
291 [OFPAT_STRIP_VLAN] = {
292 sizeof(struct ofp_action_header),
293 sizeof(struct ofp_action_header),
295 [OFPAT_SET_DL_SRC] = {
296 sizeof(struct ofp_action_dl_addr),
297 sizeof(struct ofp_action_dl_addr),
299 [OFPAT_SET_DL_DST] = {
300 sizeof(struct ofp_action_dl_addr),
301 sizeof(struct ofp_action_dl_addr),
303 [OFPAT_SET_NW_SRC] = {
304 sizeof(struct ofp_action_nw_addr),
305 sizeof(struct ofp_action_nw_addr),
307 [OFPAT_SET_NW_DST] = {
308 sizeof(struct ofp_action_nw_addr),
309 sizeof(struct ofp_action_nw_addr),
311 [OFPAT_SET_TP_SRC] = {
312 sizeof(struct ofp_action_tp_port),
313 sizeof(struct ofp_action_tp_port),
315 [OFPAT_SET_TP_DST] = {
316 sizeof(struct ofp_action_tp_port),
317 sizeof(struct ofp_action_tp_port),
319 /* OFPAT_VENDOR is not here, since it would blow up the array size. */
322 if (actions_len < sizeof *ah) {
323 ds_put_format(string, "***action array too short for next action***\n");
327 type = ntohs(ah->type);
328 len = ntohs(ah->len);
329 if (actions_len < len) {
330 ds_put_format(string, "***truncated action %"PRIu16"***\n", type);
334 if ((len % 8) != 0) {
335 ds_put_format(string,
336 "***action %"PRIu16" length not a multiple of 8***\n",
341 if (type < ARRAY_SIZE(of_actions)) {
342 const struct openflow_action *act = &of_actions[type];
343 if ((len < act->min_size) || (len > act->max_size)) {
344 ds_put_format(string,
345 "***action %"PRIu16" wrong length: %zu***\n", type, len);
352 struct ofp_action_output *oa = (struct ofp_action_output *)ah;
353 uint16_t port = ntohs(oa->port);
354 if (port < OFPP_MAX) {
355 ds_put_format(string, "output:%"PRIu16, port);
357 ofp_print_port_name(string, port);
358 if (port == OFPP_CONTROLLER) {
360 ds_put_format(string, ":%"PRIu16, ntohs(oa->max_len));
362 ds_put_cstr(string, ":all");
369 case OFPAT_SET_VLAN_VID: {
370 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
371 ds_put_format(string, "mod_vlan_vid:%"PRIu16, ntohs(va->vlan_vid));
375 case OFPAT_SET_VLAN_PCP: {
376 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
377 ds_put_format(string, "mod_vlan_pcp:%"PRIu8, va->vlan_pcp);
381 case OFPAT_STRIP_VLAN:
382 ds_put_cstr(string, "strip_vlan");
385 case OFPAT_SET_DL_SRC: {
386 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
387 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
388 ETH_ADDR_ARGS(da->dl_addr));
392 case OFPAT_SET_DL_DST: {
393 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
394 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
395 ETH_ADDR_ARGS(da->dl_addr));
399 case OFPAT_SET_NW_SRC: {
400 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
401 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&na->nw_addr));
405 case OFPAT_SET_NW_DST: {
406 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
407 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&na->nw_addr));
411 case OFPAT_SET_TP_SRC: {
412 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
413 ds_put_format(string, "mod_tp_src:%d", ntohs(ta->tp_port));
417 case OFPAT_SET_TP_DST: {
418 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
419 ds_put_format(string, "mod_tp_dst:%d", ntohs(ta->tp_port));
424 struct ofp_action_vendor_header *avh
425 = (struct ofp_action_vendor_header *)ah;
426 if (len < sizeof *avh) {
427 ds_put_format(string, "***ofpat_vendor truncated***\n");
430 if (avh->vendor == htonl(NX_VENDOR_ID)) {
431 ofp_print_nx_action(string, (struct nx_action_header *)avh);
433 ds_put_format(string, "vendor action:0x%x", ntohl(avh->vendor));
439 ds_put_format(string, "(decoder %"PRIu16" not implemented)", type);
447 ofp_print_actions(struct ds *string, const struct ofp_action_header *action,
450 uint8_t *p = (uint8_t *)action;
453 ds_put_cstr(string, "actions=");
454 while (actions_len > 0) {
456 ds_put_cstr(string, ",");
458 len = ofp_print_action(string, (struct ofp_action_header *)p,
468 /* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'string'
469 * at the given 'verbosity' level. */
470 static void ofp_packet_out(struct ds *string, const void *oh, size_t len,
473 const struct ofp_packet_out *opo = oh;
474 size_t actions_len = ntohs(opo->actions_len);
476 ds_put_cstr(string, " in_port=");
477 ofp_print_port_name(string, ntohs(opo->in_port));
479 ds_put_format(string, " actions_len=%zu ", actions_len);
480 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
481 ds_put_format(string, "***packet too short for action length***\n");
484 ofp_print_actions(string, opo->actions, actions_len);
486 if (ntohl(opo->buffer_id) == UINT32_MAX) {
487 int data_len = len - sizeof *opo - actions_len;
488 ds_put_format(string, " data_len=%d", data_len);
489 if (verbosity > 0 && len > sizeof *opo) {
490 char *packet = ofp_packet_to_string(
491 (uint8_t *)opo->actions + actions_len, data_len, data_len);
492 ds_put_char(string, '\n');
493 ds_put_cstr(string, packet);
497 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
499 ds_put_char(string, '\n');
502 /* qsort comparison function. */
504 compare_ports(const void *a_, const void *b_)
506 const struct ofp_phy_port *a = a_;
507 const struct ofp_phy_port *b = b_;
508 uint16_t ap = ntohs(a->port_no);
509 uint16_t bp = ntohs(b->port_no);
511 return ap < bp ? -1 : ap > bp;
514 static void ofp_print_port_features(struct ds *string, uint32_t features)
517 ds_put_cstr(string, "Unsupported\n");
520 if (features & OFPPF_10MB_HD) {
521 ds_put_cstr(string, "10MB-HD ");
523 if (features & OFPPF_10MB_FD) {
524 ds_put_cstr(string, "10MB-FD ");
526 if (features & OFPPF_100MB_HD) {
527 ds_put_cstr(string, "100MB-HD ");
529 if (features & OFPPF_100MB_FD) {
530 ds_put_cstr(string, "100MB-FD ");
532 if (features & OFPPF_1GB_HD) {
533 ds_put_cstr(string, "1GB-HD ");
535 if (features & OFPPF_1GB_FD) {
536 ds_put_cstr(string, "1GB-FD ");
538 if (features & OFPPF_10GB_FD) {
539 ds_put_cstr(string, "10GB-FD ");
541 if (features & OFPPF_COPPER) {
542 ds_put_cstr(string, "COPPER ");
544 if (features & OFPPF_FIBER) {
545 ds_put_cstr(string, "FIBER ");
547 if (features & OFPPF_AUTONEG) {
548 ds_put_cstr(string, "AUTO_NEG ");
550 if (features & OFPPF_PAUSE) {
551 ds_put_cstr(string, "AUTO_PAUSE ");
553 if (features & OFPPF_PAUSE_ASYM) {
554 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
556 ds_put_char(string, '\n');
560 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
562 uint8_t name[OFP_MAX_PORT_NAME_LEN];
565 memcpy(name, port->name, sizeof name);
566 for (j = 0; j < sizeof name - 1; j++) {
567 if (!isprint(name[j])) {
573 ds_put_char(string, ' ');
574 ofp_print_port_name(string, ntohs(port->port_no));
575 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
576 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
579 ds_put_format(string, " current: ");
580 ofp_print_port_features(string, ntohl(port->curr));
582 if (port->advertised) {
583 ds_put_format(string, " advertised: ");
584 ofp_print_port_features(string, ntohl(port->advertised));
586 if (port->supported) {
587 ds_put_format(string, " supported: ");
588 ofp_print_port_features(string, ntohl(port->supported));
591 ds_put_format(string, " peer: ");
592 ofp_print_port_features(string, ntohl(port->peer));
596 /* Pretty-print the struct ofp_switch_features of 'len' bytes at 'oh' to
597 * 'string' at the given 'verbosity' level. */
599 ofp_print_switch_features(struct ds *string, const void *oh, size_t len,
602 const struct ofp_switch_features *osf = oh;
603 struct ofp_phy_port *port_list;
607 ds_put_format(string, " ver:0x%x, dpid:%"PRIx64"\n",
608 osf->header.version, ntohll(osf->datapath_id));
609 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
610 ntohl(osf->n_buffers));
611 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
612 ntohl(osf->capabilities), ntohl(osf->actions));
614 if (ntohs(osf->header.length) >= sizeof *osf) {
615 len = MIN(len, ntohs(osf->header.length));
617 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
619 port_list = xmemdup(osf->ports, len - sizeof *osf);
620 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
621 for (i = 0; i < n_ports; i++) {
622 ofp_print_phy_port(string, &port_list[i]);
627 /* Pretty-print the struct ofp_switch_config of 'len' bytes at 'oh' to 'string'
628 * at the given 'verbosity' level. */
630 ofp_print_switch_config(struct ds *string, const void *oh, size_t len,
633 const struct ofp_switch_config *osc = oh;
636 flags = ntohs(osc->flags);
637 if (flags & OFPC_SEND_FLOW_EXP) {
638 flags &= ~OFPC_SEND_FLOW_EXP;
639 ds_put_format(string, " (sending flow expirations)");
642 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
645 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
648 static void print_wild(struct ds *string, const char *leader, int is_wild,
649 int verbosity, const char *format, ...)
650 __attribute__((format(printf, 5, 6)));
652 static void print_wild(struct ds *string, const char *leader, int is_wild,
653 int verbosity, const char *format, ...)
655 if (is_wild && verbosity < 2) {
658 ds_put_cstr(string, leader);
662 va_start(args, format);
663 ds_put_format_valist(string, format, args);
666 ds_put_char(string, '*');
668 ds_put_char(string, ',');
672 print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
673 uint32_t wild_bits, int verbosity)
675 if (wild_bits >= 32 && verbosity < 2) {
678 ds_put_cstr(string, leader);
679 if (wild_bits < 32) {
680 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
682 ds_put_format(string, "/%d", 32 - wild_bits);
685 ds_put_char(string, '*');
687 ds_put_char(string, ',');
690 /* Pretty-print the ofp_match structure */
691 static void ofp_print_match(struct ds *f, const struct ofp_match *om,
694 uint32_t w = ntohl(om->wildcards);
695 bool skip_type = false;
696 bool skip_proto = false;
698 if (!(w & OFPFW_DL_TYPE)) {
700 if (om->dl_type == htons(ETH_TYPE_IP)) {
701 if (!(w & OFPFW_NW_PROTO)) {
703 if (om->nw_proto == IP_TYPE_ICMP) {
704 ds_put_cstr(f, "icmp,");
705 } else if (om->nw_proto == IP_TYPE_TCP) {
706 ds_put_cstr(f, "tcp,");
707 } else if (om->nw_proto == IP_TYPE_UDP) {
708 ds_put_cstr(f, "udp,");
710 ds_put_cstr(f, "ip,");
714 ds_put_cstr(f, "ip,");
716 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
717 ds_put_cstr(f, "arp,");
722 print_wild(f, "in_port=", w & OFPFW_IN_PORT, verbosity,
723 "%d", ntohs(om->in_port));
724 print_wild(f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
725 "0x%04x", ntohs(om->dl_vlan));
726 print_wild(f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
727 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
728 print_wild(f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
729 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
731 print_wild(f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
732 "0x%04x", ntohs(om->dl_type));
734 print_ip_netmask(f, "nw_src=", om->nw_src,
735 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
736 print_ip_netmask(f, "nw_dst=", om->nw_dst,
737 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
739 print_wild(f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
742 print_wild(f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
743 "%d", ntohs(om->tp_src));
744 print_wild(f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
745 "%d", ntohs(om->tp_dst));
748 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
749 * at the given 'verbosity' level. */
751 ofp_print_flow_mod(struct ds *string, const void *oh, size_t len,
754 const struct ofp_flow_mod *ofm = oh;
756 ofp_print_match(string, &ofm->match, verbosity);
757 switch (ntohs(ofm->command)) {
759 ds_put_cstr(string, " ADD: ");
762 ds_put_cstr(string, " MOD: ");
764 case OFPFC_MODIFY_STRICT:
765 ds_put_cstr(string, " MOD_STRICT: ");
768 ds_put_cstr(string, " DEL: ");
770 case OFPFC_DELETE_STRICT:
771 ds_put_cstr(string, " DEL_STRICT: ");
774 ds_put_format(string, " cmd:%d ", ntohs(ofm->command));
776 ds_put_format(string, "idle:%d hard:%d pri:%d buf:%#x",
777 ntohs(ofm->idle_timeout), ntohs(ofm->hard_timeout),
778 ofm->match.wildcards ? ntohs(ofm->priority) : (uint16_t)-1,
779 ntohl(ofm->buffer_id));
780 ofp_print_actions(string, ofm->actions,
781 len - offsetof(struct ofp_flow_mod, actions));
782 ds_put_char(string, '\n');
785 /* Pretty-print the OFPT_FLOW_EXPIRED packet of 'len' bytes at 'oh' to 'string'
786 * at the given 'verbosity' level. */
788 ofp_print_flow_expired(struct ds *string, const void *oh, size_t len,
791 const struct ofp_flow_expired *ofe = oh;
793 ofp_print_match(string, &ofe->match, verbosity);
794 ds_put_cstr(string, " reason=");
795 switch (ofe->reason) {
796 case OFPER_IDLE_TIMEOUT:
797 ds_put_cstr(string, "idle");
799 case OFPER_HARD_TIMEOUT:
800 ds_put_cstr(string, "hard");
803 ds_put_format(string, "**%"PRIu8"**", ofe->reason);
806 ds_put_format(string,
807 " pri%"PRIu16" secs%"PRIu32" pkts%"PRIu64" bytes%"PRIu64"\n",
808 ofe->match.wildcards ? ntohs(ofe->priority) : (uint16_t)-1,
809 ntohl(ofe->duration), ntohll(ofe->packet_count),
810 ntohll(ofe->byte_count));
814 ofp_print_port_mod(struct ds *string, const void *oh, size_t len,
817 const struct ofp_port_mod *opm = oh;
819 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
820 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
821 ntohl(opm->config), ntohl(opm->mask));
822 ds_put_format(string, " advertise: ");
823 if (opm->advertise) {
824 ofp_print_port_features(string, ntohl(opm->advertise));
826 ds_put_format(string, "UNCHANGED\n");
836 static const struct error_type error_types[] = {
837 #define ERROR_TYPE(TYPE) {TYPE, -1, #TYPE}
838 #define ERROR_CODE(TYPE, CODE) {TYPE, CODE, #CODE}
839 ERROR_TYPE(OFPET_HELLO_FAILED),
840 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_INCOMPATIBLE),
842 ERROR_TYPE(OFPET_BAD_REQUEST),
843 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
844 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE),
845 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT),
846 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
848 ERROR_TYPE(OFPET_BAD_ACTION),
849 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE),
850 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_LEN),
851 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR),
852 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE),
853 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT),
855 ERROR_TYPE(OFPET_FLOW_MOD_FAILED),
856 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL)
858 #define N_ERROR_TYPES ARRAY_SIZE(error_types)
861 lookup_error_type(int type)
863 const struct error_type *t;
865 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
866 if (t->type == type && t->code == -1) {
874 lookup_error_code(int type, int code)
876 const struct error_type *t;
878 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
879 if (t->type == type && t->code == code) {
886 /* Pretty-print the OFPT_ERROR packet of 'len' bytes at 'oh' to 'string'
887 * at the given 'verbosity' level. */
889 ofp_print_error_msg(struct ds *string, const void *oh, size_t len,
892 const struct ofp_error_msg *oem = oh;
893 int type = ntohs(oem->type);
894 int code = ntohs(oem->code);
897 ds_put_format(string, " type%d(%s) code%d(%s) payload:\n",
898 type, lookup_error_type(type),
899 code, lookup_error_code(type, code));
902 case OFPET_HELLO_FAILED:
903 ds_put_printable(string, (char *) oem->data, len - sizeof *oem);
906 case OFPET_BAD_REQUEST:
907 s = ofp_to_string(oem->data, len - sizeof *oem, 1);
908 ds_put_cstr(string, s);
913 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
918 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'string'
919 * at the given 'verbosity' level. */
921 ofp_print_port_status(struct ds *string, const void *oh, size_t len,
924 const struct ofp_port_status *ops = oh;
926 if (ops->reason == OFPPR_ADD) {
927 ds_put_format(string, " ADD:");
928 } else if (ops->reason == OFPPR_DELETE) {
929 ds_put_format(string, " DEL:");
930 } else if (ops->reason == OFPPR_MODIFY) {
931 ds_put_format(string, " MOD:");
934 ofp_print_phy_port(string, &ops->desc);
938 ofp_desc_stats_reply(struct ds *string, const void *body, size_t len,
941 const struct ofp_desc_stats *ods = body;
943 ds_put_format(string, "Manufacturer: %s\n", ods->mfr_desc);
944 ds_put_format(string, "Hardware: %s\n", ods->hw_desc);
945 ds_put_format(string, "Software: %s\n", ods->sw_desc);
946 ds_put_format(string, "Serial Num: %s\n", ods->serial_num);
950 ofp_flow_stats_request(struct ds *string, const void *oh, size_t len,
953 const struct ofp_flow_stats_request *fsr = oh;
955 if (fsr->table_id == 0xff) {
956 ds_put_format(string, " table_id=any, ");
958 ds_put_format(string, " table_id=%"PRIu8", ", fsr->table_id);
961 ofp_print_match(string, &fsr->match, verbosity);
965 ofp_flow_stats_reply(struct ds *string, const void *body_, size_t len,
968 const char *body = body_;
969 const char *pos = body;
971 const struct ofp_flow_stats *fs;
972 ptrdiff_t bytes_left = body + len - pos;
975 if (bytes_left < sizeof *fs) {
976 if (bytes_left != 0) {
977 ds_put_format(string, " ***%td leftover bytes at end***",
983 fs = (const void *) pos;
984 length = ntohs(fs->length);
985 if (length < sizeof *fs) {
986 ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
989 } else if (length > bytes_left) {
990 ds_put_format(string,
991 " ***length=%zu but only %td bytes left***",
994 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
995 ds_put_format(string,
996 " ***length=%zu has %zu bytes leftover in "
999 (length - sizeof *fs) % sizeof fs->actions[0]);
1003 ds_put_format(string, " duration=%"PRIu32"s, ", ntohl(fs->duration));
1004 ds_put_format(string, "table_id=%"PRIu8", ", fs->table_id);
1005 ds_put_format(string, "priority=%"PRIu16", ",
1006 fs->match.wildcards ? ntohs(fs->priority) : (uint16_t)-1);
1007 ds_put_format(string, "n_packets=%"PRIu64", ",
1008 ntohll(fs->packet_count));
1009 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
1010 ds_put_format(string, "idle_timeout=%"PRIu16",",
1011 ntohs(fs->idle_timeout));
1012 ds_put_format(string, "hard_timeout=%"PRIu16",",
1013 ntohs(fs->hard_timeout));
1014 ofp_print_match(string, &fs->match, verbosity);
1015 ofp_print_actions(string, fs->actions, length - sizeof *fs);
1016 ds_put_char(string, '\n');
1023 ofp_aggregate_stats_request(struct ds *string, const void *oh, size_t len,
1026 const struct ofp_aggregate_stats_request *asr = oh;
1028 if (asr->table_id == 0xff) {
1029 ds_put_format(string, " table_id=any, ");
1031 ds_put_format(string, " table_id=%"PRIu8", ", asr->table_id);
1034 ofp_print_match(string, &asr->match, verbosity);
1038 ofp_aggregate_stats_reply(struct ds *string, const void *body_, size_t len,
1041 const struct ofp_aggregate_stats_reply *asr = body_;
1043 ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
1044 ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
1045 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1048 static void print_port_stat(struct ds *string, const char *leader,
1049 uint64_t stat, int more)
1051 ds_put_cstr(string, leader);
1053 ds_put_format(string, "%"PRIu64, stat);
1055 ds_put_char(string, '?');
1058 ds_put_cstr(string, ", ");
1060 ds_put_cstr(string, "\n");
1065 ofp_port_stats_reply(struct ds *string, const void *body, size_t len,
1068 const struct ofp_port_stats *ps = body;
1069 size_t n = len / sizeof *ps;
1070 ds_put_format(string, " %zu ports\n", n);
1071 if (verbosity < 1) {
1076 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1078 ds_put_cstr(string, "rx ");
1079 print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
1080 print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
1081 print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
1082 print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
1083 print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
1084 print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
1085 print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
1087 ds_put_cstr(string, " tx ");
1088 print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
1089 print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
1090 print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
1091 print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
1092 print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
1097 ofp_table_stats_reply(struct ds *string, const void *body, size_t len,
1100 const struct ofp_table_stats *ts = body;
1101 size_t n = len / sizeof *ts;
1102 ds_put_format(string, " %zu tables\n", n);
1103 if (verbosity < 1) {
1108 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1109 strncpy(name, ts->name, sizeof name);
1110 name[OFP_MAX_TABLE_NAME_LEN] = '\0';
1112 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1113 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1114 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1115 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1116 ds_put_cstr(string, " ");
1117 ds_put_format(string, "lookup=%"PRIu64", ",
1118 ntohll(ts->lookup_count));
1119 ds_put_format(string, "matched=%"PRIu64"\n",
1120 ntohll(ts->matched_count));
1125 vendor_stat(struct ds *string, const void *body, size_t len,
1126 int verbosity UNUSED)
1128 ds_put_format(string, " vendor=%08"PRIx32, ntohl(*(uint32_t *) body));
1129 ds_put_format(string, " %zu bytes additional data",
1130 len - sizeof(uint32_t));
1133 enum stats_direction {
1139 print_stats(struct ds *string, int type, const void *body, size_t body_len,
1140 int verbosity, enum stats_direction direction)
1143 size_t min_body, max_body;
1144 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1150 struct stats_msg request;
1151 struct stats_msg reply;
1154 static const struct stats_type stats_types[] = {
1159 { 0, SIZE_MAX, ofp_desc_stats_reply },
1164 { sizeof(struct ofp_flow_stats_request),
1165 sizeof(struct ofp_flow_stats_request),
1166 ofp_flow_stats_request },
1167 { 0, SIZE_MAX, ofp_flow_stats_reply },
1172 { sizeof(struct ofp_aggregate_stats_request),
1173 sizeof(struct ofp_aggregate_stats_request),
1174 ofp_aggregate_stats_request },
1175 { sizeof(struct ofp_aggregate_stats_reply),
1176 sizeof(struct ofp_aggregate_stats_reply),
1177 ofp_aggregate_stats_reply },
1183 { 0, SIZE_MAX, ofp_table_stats_reply },
1189 { 0, SIZE_MAX, ofp_port_stats_reply },
1194 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1195 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1205 const struct stats_type *s;
1206 const struct stats_msg *m;
1208 if (type >= ARRAY_SIZE(stats_types) || !stats_types[type].name) {
1209 ds_put_format(string, " ***unknown type %d***", type);
1212 for (s = stats_types; s->type >= 0; s++) {
1213 if (s->type == type) {
1217 ds_put_format(string, " type=%d(%s)\n", type, s->name);
1219 m = direction == REQUEST ? &s->request : &s->reply;
1220 if (body_len < m->min_body || body_len > m->max_body) {
1221 ds_put_format(string, " ***body_len=%zu not in %zu...%zu***",
1222 body_len, m->min_body, m->max_body);
1226 m->printer(string, body, body_len, verbosity);
1231 ofp_stats_request(struct ds *string, const void *oh, size_t len, int verbosity)
1233 const struct ofp_stats_request *srq = oh;
1236 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1240 print_stats(string, ntohs(srq->type), srq->body,
1241 len - offsetof(struct ofp_stats_request, body),
1242 verbosity, REQUEST);
1246 ofp_stats_reply(struct ds *string, const void *oh, size_t len, int verbosity)
1248 const struct ofp_stats_reply *srp = oh;
1250 ds_put_cstr(string, " flags=");
1252 ds_put_cstr(string, "none");
1254 uint16_t flags = ntohs(srp->flags);
1255 if (flags & OFPSF_REPLY_MORE) {
1256 ds_put_cstr(string, "[more]");
1257 flags &= ~OFPSF_REPLY_MORE;
1260 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]", flags);
1264 print_stats(string, ntohs(srp->type), srp->body,
1265 len - offsetof(struct ofp_stats_reply, body),
1270 ofp_echo(struct ds *string, const void *oh, size_t len, int verbosity)
1272 const struct ofp_header *hdr = oh;
1274 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *hdr);
1275 if (verbosity > 1) {
1276 ds_put_hex_dump(string, hdr, len - sizeof *hdr, 0, true);
1280 struct openflow_packet {
1284 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1287 static const struct openflow_packet packets[] = {
1291 sizeof (struct ofp_header),
1295 OFPT_FEATURES_REQUEST,
1297 sizeof (struct ofp_header),
1301 OFPT_FEATURES_REPLY,
1303 sizeof (struct ofp_switch_features),
1304 ofp_print_switch_features,
1307 OFPT_GET_CONFIG_REQUEST,
1308 "get_config_request",
1309 sizeof (struct ofp_header),
1313 OFPT_GET_CONFIG_REPLY,
1315 sizeof (struct ofp_switch_config),
1316 ofp_print_switch_config,
1321 sizeof (struct ofp_switch_config),
1322 ofp_print_switch_config,
1327 offsetof(struct ofp_packet_in, data),
1333 sizeof (struct ofp_packet_out),
1339 sizeof (struct ofp_flow_mod),
1345 sizeof (struct ofp_flow_expired),
1346 ofp_print_flow_expired,
1351 sizeof (struct ofp_port_mod),
1357 sizeof (struct ofp_port_status),
1358 ofp_print_port_status
1363 sizeof (struct ofp_error_msg),
1364 ofp_print_error_msg,
1369 sizeof (struct ofp_stats_request),
1375 sizeof (struct ofp_stats_reply),
1381 sizeof (struct ofp_header),
1387 sizeof (struct ofp_header),
1392 /* Composes and returns a string representing the OpenFlow packet of 'len'
1393 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1394 * verbosity and higher numbers increase verbosity. The caller is responsible
1395 * for freeing the string. */
1397 ofp_to_string(const void *oh_, size_t len, int verbosity)
1399 struct ds string = DS_EMPTY_INITIALIZER;
1400 const struct ofp_header *oh = oh_;
1401 const struct openflow_packet *pkt;
1403 if (len < sizeof(struct ofp_header)) {
1404 ds_put_cstr(&string, "OpenFlow packet too short:\n");
1405 ds_put_hex_dump(&string, oh, len, 0, true);
1406 return ds_cstr(&string);
1407 } else if (oh->version != OFP_VERSION) {
1408 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", oh->version);
1409 ds_put_hex_dump(&string, oh, len, 0, true);
1410 return ds_cstr(&string);
1413 for (pkt = packets; ; pkt++) {
1414 if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1415 ds_put_format(&string, "Unknown OpenFlow packet type %"PRIu8":\n",
1417 ds_put_hex_dump(&string, oh, len, 0, true);
1418 return ds_cstr(&string);
1419 } else if (oh->type == pkt->type) {
1424 ds_put_format(&string, "%s (xid=0x%"PRIx32"):", pkt->name, oh->xid);
1426 if (ntohs(oh->length) > len)
1427 ds_put_format(&string, " (***truncated to %zu bytes from %"PRIu16"***)",
1428 len, ntohs(oh->length));
1429 else if (ntohs(oh->length) < len) {
1430 ds_put_format(&string, " (***only uses %"PRIu16" bytes out of %zu***)\n",
1431 ntohs(oh->length), len);
1432 len = ntohs(oh->length);
1435 if (len < pkt->min_size) {
1436 ds_put_format(&string, " (***length=%zu < min_size=%zu***)\n",
1437 len, pkt->min_size);
1438 } else if (!pkt->printer) {
1439 if (len > sizeof *oh) {
1440 ds_put_format(&string, " length=%"PRIu16" (decoder not implemented)\n",
1444 pkt->printer(&string, oh, len, verbosity);
1446 if (verbosity >= 3) {
1447 ds_put_hex_dump(&string, oh, len, 0, true);
1449 if (string.string[string.length - 1] != '\n') {
1450 ds_put_char(&string, '\n');
1452 return ds_cstr(&string);
1456 print_and_free(FILE *stream, char *string)
1458 fputs(string, stream);
1462 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1463 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1464 * numbers increase verbosity. */
1466 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1468 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1471 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1472 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1473 * the Ethernet frame (of which 'len' bytes were captured).
1475 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1477 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1479 print_and_free(stream, ofp_packet_to_string(data, len, total_len));