2 * Copyright (c) 2008, 2009 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-print.h"
23 #include <netinet/in.h>
30 #include "dynamic-string.h"
33 #include "openflow/openflow.h"
34 #include "openflow/nicira-ext.h"
39 static void ofp_print_port_name(struct ds *string, uint16_t port);
40 static void ofp_print_match(struct ds *, const struct ofp_match *,
43 /* Returns a string that represents the contents of the Ethernet frame in the
44 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
45 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
46 * bytes were captured).
48 * The caller must free the returned string.
50 * This starts and kills a tcpdump subprocess so it's quite expensive. */
52 ofp_packet_to_string(const void *data, size_t len, size_t total_len UNUSED)
54 struct ds ds = DS_EMPTY_INITIALIZER;
63 buf.data = (void *) data;
68 ovs_error(errno, "tmpfile");
69 return xstrdup("<error>");
71 pcap_write_header(pcap);
72 pcap_write(pcap, &buf);
75 ovs_error(errno, "error writing temporary file");
79 snprintf(command, sizeof command, "/usr/sbin/tcpdump -e -n -r /dev/fd/%d 2>/dev/null",
81 tcpdump = popen(command, "r");
84 ovs_error(errno, "exec(\"%s\")", command);
85 return xstrdup("<error>");
88 while ((c = getc(tcpdump)) != EOF) {
92 status = pclose(tcpdump);
93 if (WIFEXITED(status)) {
94 if (WEXITSTATUS(status))
95 ovs_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
96 } else if (WIFSIGNALED(status)) {
97 ovs_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
102 /* Pretty-print the OFPT_PACKET_IN packet of 'len' bytes at 'oh' to 'stream'
103 * at the given 'verbosity' level. */
105 ofp_packet_in(struct ds *string, const void *oh, size_t len, int verbosity)
107 const struct ofp_packet_in *op = oh;
110 ds_put_format(string, " total_len=%"PRIu16" in_port=",
111 ntohs(op->total_len));
112 ofp_print_port_name(string, ntohs(op->in_port));
114 if (op->reason == OFPR_ACTION)
115 ds_put_cstr(string, " (via action)");
116 else if (op->reason != OFPR_NO_MATCH)
117 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
119 data_len = len - offsetof(struct ofp_packet_in, data);
120 ds_put_format(string, " data_len=%zu", data_len);
121 if (htonl(op->buffer_id) == UINT32_MAX) {
122 ds_put_format(string, " (unbuffered)");
123 if (ntohs(op->total_len) != data_len)
124 ds_put_format(string, " (***total_len != data_len***)");
126 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
127 if (ntohs(op->total_len) < data_len)
128 ds_put_format(string, " (***total_len < data_len***)");
130 ds_put_char(string, '\n');
134 struct ofpbuf packet;
135 struct ofp_match match;
136 packet.data = (void *) op->data;
137 packet.size = data_len;
138 flow_extract(&packet, ntohs(op->in_port), &flow);
139 flow_to_match(&flow, 0, &match);
140 ofp_print_match(string, &match, verbosity);
141 ds_put_char(string, '\n');
144 char *packet = ofp_packet_to_string(op->data, data_len,
145 ntohs(op->total_len));
146 ds_put_cstr(string, packet);
151 static void ofp_print_port_name(struct ds *string, uint16_t port)
170 case OFPP_CONTROLLER:
180 ds_put_format(string, "%"PRIu16, port);
183 ds_put_cstr(string, name);
187 ofp_print_nx_action(struct ds *string, const struct nx_action_header *nah)
189 switch (ntohs(nah->subtype)) {
190 case NXAST_RESUBMIT: {
191 const struct nx_action_resubmit *nar = (struct nx_action_resubmit *)nah;
192 ds_put_format(string, "resubmit:");
193 ofp_print_port_name(string, ntohs(nar->in_port));
198 ds_put_format(string, "***unknown Nicira action:%d***\n",
199 ntohs(nah->subtype));
204 ofp_print_action(struct ds *string, const struct ofp_action_header *ah,
210 struct openflow_action {
215 const struct openflow_action of_actions[] = {
217 sizeof(struct ofp_action_output),
218 sizeof(struct ofp_action_output),
220 [OFPAT_SET_VLAN_VID] = {
221 sizeof(struct ofp_action_vlan_vid),
222 sizeof(struct ofp_action_vlan_vid),
224 [OFPAT_SET_VLAN_PCP] = {
225 sizeof(struct ofp_action_vlan_pcp),
226 sizeof(struct ofp_action_vlan_pcp),
228 [OFPAT_STRIP_VLAN] = {
229 sizeof(struct ofp_action_header),
230 sizeof(struct ofp_action_header),
232 [OFPAT_SET_DL_SRC] = {
233 sizeof(struct ofp_action_dl_addr),
234 sizeof(struct ofp_action_dl_addr),
236 [OFPAT_SET_DL_DST] = {
237 sizeof(struct ofp_action_dl_addr),
238 sizeof(struct ofp_action_dl_addr),
240 [OFPAT_SET_NW_SRC] = {
241 sizeof(struct ofp_action_nw_addr),
242 sizeof(struct ofp_action_nw_addr),
244 [OFPAT_SET_NW_DST] = {
245 sizeof(struct ofp_action_nw_addr),
246 sizeof(struct ofp_action_nw_addr),
248 [OFPAT_SET_TP_SRC] = {
249 sizeof(struct ofp_action_tp_port),
250 sizeof(struct ofp_action_tp_port),
252 [OFPAT_SET_TP_DST] = {
253 sizeof(struct ofp_action_tp_port),
254 sizeof(struct ofp_action_tp_port),
256 /* OFPAT_VENDOR is not here, since it would blow up the array size. */
259 if (actions_len < sizeof *ah) {
260 ds_put_format(string, "***action array too short for next action***\n");
264 type = ntohs(ah->type);
265 len = ntohs(ah->len);
266 if (actions_len < len) {
267 ds_put_format(string, "***truncated action %"PRIu16"***\n", type);
271 if ((len % 8) != 0) {
272 ds_put_format(string,
273 "***action %"PRIu16" length not a multiple of 8***\n",
278 if (type < ARRAY_SIZE(of_actions)) {
279 const struct openflow_action *act = &of_actions[type];
280 if ((len < act->min_size) || (len > act->max_size)) {
281 ds_put_format(string,
282 "***action %"PRIu16" wrong length: %zu***\n", type, len);
289 struct ofp_action_output *oa = (struct ofp_action_output *)ah;
290 uint16_t port = ntohs(oa->port);
291 if (port < OFPP_MAX) {
292 ds_put_format(string, "output:%"PRIu16, port);
294 ofp_print_port_name(string, port);
295 if (port == OFPP_CONTROLLER) {
297 ds_put_format(string, ":%"PRIu16, ntohs(oa->max_len));
299 ds_put_cstr(string, ":all");
306 case OFPAT_SET_VLAN_VID: {
307 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
308 ds_put_format(string, "mod_vlan_vid:%"PRIu16, ntohs(va->vlan_vid));
312 case OFPAT_SET_VLAN_PCP: {
313 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
314 ds_put_format(string, "mod_vlan_pcp:%"PRIu8, va->vlan_pcp);
318 case OFPAT_STRIP_VLAN:
319 ds_put_cstr(string, "strip_vlan");
322 case OFPAT_SET_DL_SRC: {
323 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
324 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
325 ETH_ADDR_ARGS(da->dl_addr));
329 case OFPAT_SET_DL_DST: {
330 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
331 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
332 ETH_ADDR_ARGS(da->dl_addr));
336 case OFPAT_SET_NW_SRC: {
337 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
338 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&na->nw_addr));
342 case OFPAT_SET_NW_DST: {
343 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
344 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&na->nw_addr));
348 case OFPAT_SET_TP_SRC: {
349 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
350 ds_put_format(string, "mod_tp_src:%d", ntohs(ta->tp_port));
354 case OFPAT_SET_TP_DST: {
355 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
356 ds_put_format(string, "mod_tp_dst:%d", ntohs(ta->tp_port));
361 struct ofp_action_vendor_header *avh
362 = (struct ofp_action_vendor_header *)ah;
363 if (len < sizeof *avh) {
364 ds_put_format(string, "***ofpat_vendor truncated***\n");
367 if (avh->vendor == htonl(NX_VENDOR_ID)) {
368 ofp_print_nx_action(string, (struct nx_action_header *)avh);
370 ds_put_format(string, "vendor action:0x%x", ntohl(avh->vendor));
376 ds_put_format(string, "(decoder %"PRIu16" not implemented)", type);
384 ofp_print_actions(struct ds *string, const struct ofp_action_header *action,
387 uint8_t *p = (uint8_t *)action;
390 ds_put_cstr(string, "actions=");
392 ds_put_cstr(string, "drop");
394 while (actions_len > 0) {
396 ds_put_cstr(string, ",");
398 len = ofp_print_action(string, (struct ofp_action_header *)p,
408 /* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'string'
409 * at the given 'verbosity' level. */
410 static void ofp_packet_out(struct ds *string, const void *oh, size_t len,
413 const struct ofp_packet_out *opo = oh;
414 size_t actions_len = ntohs(opo->actions_len);
416 ds_put_cstr(string, " in_port=");
417 ofp_print_port_name(string, ntohs(opo->in_port));
419 ds_put_format(string, " actions_len=%zu ", actions_len);
420 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
421 ds_put_format(string, "***packet too short for action length***\n");
424 ofp_print_actions(string, opo->actions, actions_len);
426 if (ntohl(opo->buffer_id) == UINT32_MAX) {
427 int data_len = len - sizeof *opo - actions_len;
428 ds_put_format(string, " data_len=%d", data_len);
429 if (verbosity > 0 && len > sizeof *opo) {
430 char *packet = ofp_packet_to_string(
431 (uint8_t *)opo->actions + actions_len, data_len, data_len);
432 ds_put_char(string, '\n');
433 ds_put_cstr(string, packet);
437 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
439 ds_put_char(string, '\n');
442 /* qsort comparison function. */
444 compare_ports(const void *a_, const void *b_)
446 const struct ofp_phy_port *a = a_;
447 const struct ofp_phy_port *b = b_;
448 uint16_t ap = ntohs(a->port_no);
449 uint16_t bp = ntohs(b->port_no);
451 return ap < bp ? -1 : ap > bp;
454 static void ofp_print_port_features(struct ds *string, uint32_t features)
457 ds_put_cstr(string, "Unsupported\n");
460 if (features & OFPPF_10MB_HD) {
461 ds_put_cstr(string, "10MB-HD ");
463 if (features & OFPPF_10MB_FD) {
464 ds_put_cstr(string, "10MB-FD ");
466 if (features & OFPPF_100MB_HD) {
467 ds_put_cstr(string, "100MB-HD ");
469 if (features & OFPPF_100MB_FD) {
470 ds_put_cstr(string, "100MB-FD ");
472 if (features & OFPPF_1GB_HD) {
473 ds_put_cstr(string, "1GB-HD ");
475 if (features & OFPPF_1GB_FD) {
476 ds_put_cstr(string, "1GB-FD ");
478 if (features & OFPPF_10GB_FD) {
479 ds_put_cstr(string, "10GB-FD ");
481 if (features & OFPPF_COPPER) {
482 ds_put_cstr(string, "COPPER ");
484 if (features & OFPPF_FIBER) {
485 ds_put_cstr(string, "FIBER ");
487 if (features & OFPPF_AUTONEG) {
488 ds_put_cstr(string, "AUTO_NEG ");
490 if (features & OFPPF_PAUSE) {
491 ds_put_cstr(string, "AUTO_PAUSE ");
493 if (features & OFPPF_PAUSE_ASYM) {
494 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
496 ds_put_char(string, '\n');
500 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
502 uint8_t name[OFP_MAX_PORT_NAME_LEN];
505 memcpy(name, port->name, sizeof name);
506 for (j = 0; j < sizeof name - 1; j++) {
507 if (!isprint(name[j])) {
513 ds_put_char(string, ' ');
514 ofp_print_port_name(string, ntohs(port->port_no));
515 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
516 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
519 ds_put_format(string, " current: ");
520 ofp_print_port_features(string, ntohl(port->curr));
522 if (port->advertised) {
523 ds_put_format(string, " advertised: ");
524 ofp_print_port_features(string, ntohl(port->advertised));
526 if (port->supported) {
527 ds_put_format(string, " supported: ");
528 ofp_print_port_features(string, ntohl(port->supported));
531 ds_put_format(string, " peer: ");
532 ofp_print_port_features(string, ntohl(port->peer));
536 /* Pretty-print the struct ofp_switch_features of 'len' bytes at 'oh' to
537 * 'string' at the given 'verbosity' level. */
539 ofp_print_switch_features(struct ds *string, const void *oh, size_t len,
540 int verbosity UNUSED)
542 const struct ofp_switch_features *osf = oh;
543 struct ofp_phy_port *port_list;
547 ds_put_format(string, " ver:0x%x, dpid:%"PRIx64"\n",
548 osf->header.version, ntohll(osf->datapath_id));
549 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
550 ntohl(osf->n_buffers));
551 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
552 ntohl(osf->capabilities), ntohl(osf->actions));
554 if (ntohs(osf->header.length) >= sizeof *osf) {
555 len = MIN(len, ntohs(osf->header.length));
557 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
559 port_list = xmemdup(osf->ports, len - sizeof *osf);
560 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
561 for (i = 0; i < n_ports; i++) {
562 ofp_print_phy_port(string, &port_list[i]);
567 /* Pretty-print the struct ofp_switch_config of 'len' bytes at 'oh' to 'string'
568 * at the given 'verbosity' level. */
570 ofp_print_switch_config(struct ds *string, const void *oh, size_t len UNUSED,
571 int verbosity UNUSED)
573 const struct ofp_switch_config *osc = oh;
576 flags = ntohs(osc->flags);
577 if (flags & OFPC_SEND_FLOW_EXP) {
578 flags &= ~OFPC_SEND_FLOW_EXP;
579 ds_put_format(string, " (sending flow expirations)");
582 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
585 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
588 static void print_wild(struct ds *string, const char *leader, int is_wild,
589 int verbosity, const char *format, ...)
590 __attribute__((format(printf, 5, 6)));
592 static void print_wild(struct ds *string, const char *leader, int is_wild,
593 int verbosity, const char *format, ...)
595 if (is_wild && verbosity < 2) {
598 ds_put_cstr(string, leader);
602 va_start(args, format);
603 ds_put_format_valist(string, format, args);
606 ds_put_char(string, '*');
608 ds_put_char(string, ',');
612 print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
613 uint32_t wild_bits, int verbosity)
615 if (wild_bits >= 32 && verbosity < 2) {
618 ds_put_cstr(string, leader);
619 if (wild_bits < 32) {
620 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
622 ds_put_format(string, "/%d", 32 - wild_bits);
625 ds_put_char(string, '*');
627 ds_put_char(string, ',');
631 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
633 char *s = ofp_match_to_string(om, verbosity);
639 ofp_match_to_string(const struct ofp_match *om, int verbosity)
641 struct ds f = DS_EMPTY_INITIALIZER;
642 uint32_t w = ntohl(om->wildcards);
643 bool skip_type = false;
644 bool skip_proto = false;
646 if (!(w & OFPFW_DL_TYPE)) {
648 if (om->dl_type == htons(ETH_TYPE_IP)) {
649 if (!(w & OFPFW_NW_PROTO)) {
651 if (om->nw_proto == IP_TYPE_ICMP) {
652 ds_put_cstr(&f, "icmp,");
653 } else if (om->nw_proto == IP_TYPE_TCP) {
654 ds_put_cstr(&f, "tcp,");
655 } else if (om->nw_proto == IP_TYPE_UDP) {
656 ds_put_cstr(&f, "udp,");
658 ds_put_cstr(&f, "ip,");
662 ds_put_cstr(&f, "ip,");
664 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
665 ds_put_cstr(&f, "arp,");
670 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
671 "%d", ntohs(om->in_port));
672 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
673 "0x%04x", ntohs(om->dl_vlan));
674 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
675 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
676 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
677 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
679 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
680 "0x%04x", ntohs(om->dl_type));
682 print_ip_netmask(&f, "nw_src=", om->nw_src,
683 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
684 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
685 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
687 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
690 if (om->nw_proto == IP_TYPE_ICMP) {
691 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
692 "%d", ntohs(om->icmp_type));
693 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
694 "%d", ntohs(om->icmp_code));
696 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
697 "%d", ntohs(om->tp_src));
698 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
699 "%d", ntohs(om->tp_dst));
704 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
705 * at the given 'verbosity' level. */
707 ofp_print_flow_mod(struct ds *string, const void *oh, size_t len,
710 const struct ofp_flow_mod *ofm = oh;
712 ofp_print_match(string, &ofm->match, verbosity);
713 switch (ntohs(ofm->command)) {
715 ds_put_cstr(string, " ADD: ");
718 ds_put_cstr(string, " MOD: ");
720 case OFPFC_MODIFY_STRICT:
721 ds_put_cstr(string, " MOD_STRICT: ");
724 ds_put_cstr(string, " DEL: ");
726 case OFPFC_DELETE_STRICT:
727 ds_put_cstr(string, " DEL_STRICT: ");
730 ds_put_format(string, " cmd:%d ", ntohs(ofm->command));
732 ds_put_format(string, "idle:%d hard:%d pri:%d buf:%#x",
733 ntohs(ofm->idle_timeout), ntohs(ofm->hard_timeout),
734 ofm->match.wildcards ? ntohs(ofm->priority) : (uint16_t)-1,
735 ntohl(ofm->buffer_id));
736 ofp_print_actions(string, ofm->actions,
737 len - offsetof(struct ofp_flow_mod, actions));
738 ds_put_char(string, '\n');
741 /* Pretty-print the OFPT_FLOW_EXPIRED packet of 'len' bytes at 'oh' to 'string'
742 * at the given 'verbosity' level. */
744 ofp_print_flow_expired(struct ds *string, const void *oh, size_t len UNUSED,
747 const struct ofp_flow_expired *ofe = oh;
749 ofp_print_match(string, &ofe->match, verbosity);
750 ds_put_cstr(string, " reason=");
751 switch (ofe->reason) {
752 case OFPER_IDLE_TIMEOUT:
753 ds_put_cstr(string, "idle");
755 case OFPER_HARD_TIMEOUT:
756 ds_put_cstr(string, "hard");
759 ds_put_format(string, "**%"PRIu8"**", ofe->reason);
762 ds_put_format(string,
763 " pri%"PRIu16" secs%"PRIu32" pkts%"PRIu64" bytes%"PRIu64"\n",
764 ofe->match.wildcards ? ntohs(ofe->priority) : (uint16_t)-1,
765 ntohl(ofe->duration), ntohll(ofe->packet_count),
766 ntohll(ofe->byte_count));
770 ofp_print_port_mod(struct ds *string, const void *oh, size_t len UNUSED,
771 int verbosity UNUSED)
773 const struct ofp_port_mod *opm = oh;
775 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
776 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
777 ntohl(opm->config), ntohl(opm->mask));
778 ds_put_format(string, " advertise: ");
779 if (opm->advertise) {
780 ofp_print_port_features(string, ntohl(opm->advertise));
782 ds_put_format(string, "UNCHANGED\n");
792 static const struct error_type error_types[] = {
793 #define ERROR_TYPE(TYPE) {TYPE, -1, #TYPE}
794 #define ERROR_CODE(TYPE, CODE) {TYPE, CODE, #CODE}
795 ERROR_TYPE(OFPET_HELLO_FAILED),
796 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_INCOMPATIBLE),
798 ERROR_TYPE(OFPET_BAD_REQUEST),
799 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
800 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE),
801 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT),
802 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
804 ERROR_TYPE(OFPET_BAD_ACTION),
805 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE),
806 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_LEN),
807 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR),
808 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE),
809 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT),
811 ERROR_TYPE(OFPET_FLOW_MOD_FAILED),
812 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL)
814 #define N_ERROR_TYPES ARRAY_SIZE(error_types)
817 lookup_error_type(int type)
819 const struct error_type *t;
821 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
822 if (t->type == type && t->code == -1) {
830 lookup_error_code(int type, int code)
832 const struct error_type *t;
834 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
835 if (t->type == type && t->code == code) {
842 /* Pretty-print the OFPT_ERROR packet of 'len' bytes at 'oh' to 'string'
843 * at the given 'verbosity' level. */
845 ofp_print_error_msg(struct ds *string, const void *oh, size_t len,
846 int verbosity UNUSED)
848 const struct ofp_error_msg *oem = oh;
849 int type = ntohs(oem->type);
850 int code = ntohs(oem->code);
853 ds_put_format(string, " type%d(%s) code%d(%s) payload:\n",
854 type, lookup_error_type(type),
855 code, lookup_error_code(type, code));
858 case OFPET_HELLO_FAILED:
859 ds_put_printable(string, (char *) oem->data, len - sizeof *oem);
862 case OFPET_BAD_REQUEST:
863 s = ofp_to_string(oem->data, len - sizeof *oem, 1);
864 ds_put_cstr(string, s);
869 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
874 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'string'
875 * at the given 'verbosity' level. */
877 ofp_print_port_status(struct ds *string, const void *oh, size_t len UNUSED,
878 int verbosity UNUSED)
880 const struct ofp_port_status *ops = oh;
882 if (ops->reason == OFPPR_ADD) {
883 ds_put_format(string, " ADD:");
884 } else if (ops->reason == OFPPR_DELETE) {
885 ds_put_format(string, " DEL:");
886 } else if (ops->reason == OFPPR_MODIFY) {
887 ds_put_format(string, " MOD:");
890 ofp_print_phy_port(string, &ops->desc);
894 ofp_desc_stats_reply(struct ds *string, const void *body, size_t len UNUSED,
895 int verbosity UNUSED)
897 const struct ofp_desc_stats *ods = body;
899 ds_put_format(string, "Manufacturer: %s\n", ods->mfr_desc);
900 ds_put_format(string, "Hardware: %s\n", ods->hw_desc);
901 ds_put_format(string, "Software: %s\n", ods->sw_desc);
902 ds_put_format(string, "Serial Num: %s\n", ods->serial_num);
906 ofp_flow_stats_request(struct ds *string, const void *oh, size_t len UNUSED,
909 const struct ofp_flow_stats_request *fsr = oh;
911 if (fsr->table_id == 0xff) {
912 ds_put_format(string, " table_id=any, ");
914 ds_put_format(string, " table_id=%"PRIu8", ", fsr->table_id);
917 ofp_print_match(string, &fsr->match, verbosity);
921 ofp_flow_stats_reply(struct ds *string, const void *body_, size_t len,
924 const char *body = body_;
925 const char *pos = body;
927 const struct ofp_flow_stats *fs;
928 ptrdiff_t bytes_left = body + len - pos;
931 if (bytes_left < sizeof *fs) {
932 if (bytes_left != 0) {
933 ds_put_format(string, " ***%td leftover bytes at end***",
939 fs = (const void *) pos;
940 length = ntohs(fs->length);
941 if (length < sizeof *fs) {
942 ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
945 } else if (length > bytes_left) {
946 ds_put_format(string,
947 " ***length=%zu but only %td bytes left***",
950 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
951 ds_put_format(string,
952 " ***length=%zu has %zu bytes leftover in "
955 (length - sizeof *fs) % sizeof fs->actions[0]);
959 ds_put_format(string, " duration=%"PRIu32"s, ", ntohl(fs->duration));
960 ds_put_format(string, "table_id=%"PRIu8", ", fs->table_id);
961 ds_put_format(string, "priority=%"PRIu16", ",
962 fs->match.wildcards ? ntohs(fs->priority) : (uint16_t)-1);
963 ds_put_format(string, "n_packets=%"PRIu64", ",
964 ntohll(fs->packet_count));
965 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
966 if (fs->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
967 ds_put_format(string, "idle_timeout=%"PRIu16",",
968 ntohs(fs->idle_timeout));
970 if (fs->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
971 ds_put_format(string, "hard_timeout=%"PRIu16",",
972 ntohs(fs->hard_timeout));
974 ofp_print_match(string, &fs->match, verbosity);
975 ofp_print_actions(string, fs->actions, length - sizeof *fs);
976 ds_put_char(string, '\n');
983 ofp_aggregate_stats_request(struct ds *string, const void *oh,
984 size_t len UNUSED, int verbosity)
986 const struct ofp_aggregate_stats_request *asr = oh;
988 if (asr->table_id == 0xff) {
989 ds_put_format(string, " table_id=any, ");
991 ds_put_format(string, " table_id=%"PRIu8", ", asr->table_id);
994 ofp_print_match(string, &asr->match, verbosity);
998 ofp_aggregate_stats_reply(struct ds *string, const void *body_,
999 size_t len UNUSED, int verbosity UNUSED)
1001 const struct ofp_aggregate_stats_reply *asr = body_;
1003 ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
1004 ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
1005 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1008 static void print_port_stat(struct ds *string, const char *leader,
1009 uint64_t stat, int more)
1011 ds_put_cstr(string, leader);
1013 ds_put_format(string, "%"PRIu64, stat);
1015 ds_put_char(string, '?');
1018 ds_put_cstr(string, ", ");
1020 ds_put_cstr(string, "\n");
1025 ofp_port_stats_reply(struct ds *string, const void *body, size_t len,
1028 const struct ofp_port_stats *ps = body;
1029 size_t n = len / sizeof *ps;
1030 ds_put_format(string, " %zu ports\n", n);
1031 if (verbosity < 1) {
1036 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1038 ds_put_cstr(string, "rx ");
1039 print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
1040 print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
1041 print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
1042 print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
1043 print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
1044 print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
1045 print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
1047 ds_put_cstr(string, " tx ");
1048 print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
1049 print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
1050 print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
1051 print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
1052 print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
1057 ofp_table_stats_reply(struct ds *string, const void *body, size_t len,
1060 const struct ofp_table_stats *ts = body;
1061 size_t n = len / sizeof *ts;
1062 ds_put_format(string, " %zu tables\n", n);
1063 if (verbosity < 1) {
1068 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1069 strncpy(name, ts->name, sizeof name);
1070 name[OFP_MAX_TABLE_NAME_LEN] = '\0';
1072 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1073 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1074 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1075 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1076 ds_put_cstr(string, " ");
1077 ds_put_format(string, "lookup=%"PRIu64", ",
1078 ntohll(ts->lookup_count));
1079 ds_put_format(string, "matched=%"PRIu64"\n",
1080 ntohll(ts->matched_count));
1085 vendor_stat(struct ds *string, const void *body, size_t len,
1086 int verbosity UNUSED)
1088 ds_put_format(string, " vendor=%08"PRIx32, ntohl(*(uint32_t *) body));
1089 ds_put_format(string, " %zu bytes additional data",
1090 len - sizeof(uint32_t));
1093 enum stats_direction {
1099 print_stats(struct ds *string, int type, const void *body, size_t body_len,
1100 int verbosity, enum stats_direction direction)
1103 size_t min_body, max_body;
1104 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1110 struct stats_msg request;
1111 struct stats_msg reply;
1114 static const struct stats_type stats_types[] = {
1119 { 0, SIZE_MAX, ofp_desc_stats_reply },
1124 { sizeof(struct ofp_flow_stats_request),
1125 sizeof(struct ofp_flow_stats_request),
1126 ofp_flow_stats_request },
1127 { 0, SIZE_MAX, ofp_flow_stats_reply },
1132 { sizeof(struct ofp_aggregate_stats_request),
1133 sizeof(struct ofp_aggregate_stats_request),
1134 ofp_aggregate_stats_request },
1135 { sizeof(struct ofp_aggregate_stats_reply),
1136 sizeof(struct ofp_aggregate_stats_reply),
1137 ofp_aggregate_stats_reply },
1143 { 0, SIZE_MAX, ofp_table_stats_reply },
1149 { 0, SIZE_MAX, ofp_port_stats_reply },
1154 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1155 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1165 const struct stats_type *s;
1166 const struct stats_msg *m;
1168 if (type >= ARRAY_SIZE(stats_types) || !stats_types[type].name) {
1169 ds_put_format(string, " ***unknown type %d***", type);
1172 for (s = stats_types; s->type >= 0; s++) {
1173 if (s->type == type) {
1177 ds_put_format(string, " type=%d(%s)\n", type, s->name);
1179 m = direction == REQUEST ? &s->request : &s->reply;
1180 if (body_len < m->min_body || body_len > m->max_body) {
1181 ds_put_format(string, " ***body_len=%zu not in %zu...%zu***",
1182 body_len, m->min_body, m->max_body);
1186 m->printer(string, body, body_len, verbosity);
1191 ofp_stats_request(struct ds *string, const void *oh, size_t len, int verbosity)
1193 const struct ofp_stats_request *srq = oh;
1196 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1200 print_stats(string, ntohs(srq->type), srq->body,
1201 len - offsetof(struct ofp_stats_request, body),
1202 verbosity, REQUEST);
1206 ofp_stats_reply(struct ds *string, const void *oh, size_t len, int verbosity)
1208 const struct ofp_stats_reply *srp = oh;
1210 ds_put_cstr(string, " flags=");
1212 ds_put_cstr(string, "none");
1214 uint16_t flags = ntohs(srp->flags);
1215 if (flags & OFPSF_REPLY_MORE) {
1216 ds_put_cstr(string, "[more]");
1217 flags &= ~OFPSF_REPLY_MORE;
1220 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]", flags);
1224 print_stats(string, ntohs(srp->type), srp->body,
1225 len - offsetof(struct ofp_stats_reply, body),
1230 ofp_echo(struct ds *string, const void *oh, size_t len, int verbosity)
1232 const struct ofp_header *hdr = oh;
1234 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *hdr);
1235 if (verbosity > 1) {
1236 ds_put_hex_dump(string, hdr, len - sizeof *hdr, 0, true);
1240 struct openflow_packet {
1244 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1247 static const struct openflow_packet packets[] = {
1251 sizeof (struct ofp_header),
1255 OFPT_FEATURES_REQUEST,
1257 sizeof (struct ofp_header),
1261 OFPT_FEATURES_REPLY,
1263 sizeof (struct ofp_switch_features),
1264 ofp_print_switch_features,
1267 OFPT_GET_CONFIG_REQUEST,
1268 "get_config_request",
1269 sizeof (struct ofp_header),
1273 OFPT_GET_CONFIG_REPLY,
1275 sizeof (struct ofp_switch_config),
1276 ofp_print_switch_config,
1281 sizeof (struct ofp_switch_config),
1282 ofp_print_switch_config,
1287 offsetof(struct ofp_packet_in, data),
1293 sizeof (struct ofp_packet_out),
1299 sizeof (struct ofp_flow_mod),
1305 sizeof (struct ofp_flow_expired),
1306 ofp_print_flow_expired,
1311 sizeof (struct ofp_port_mod),
1317 sizeof (struct ofp_port_status),
1318 ofp_print_port_status
1323 sizeof (struct ofp_error_msg),
1324 ofp_print_error_msg,
1329 sizeof (struct ofp_stats_request),
1335 sizeof (struct ofp_stats_reply),
1341 sizeof (struct ofp_header),
1347 sizeof (struct ofp_header),
1353 sizeof (struct ofp_vendor_header),
1358 /* Composes and returns a string representing the OpenFlow packet of 'len'
1359 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1360 * verbosity and higher numbers increase verbosity. The caller is responsible
1361 * for freeing the string. */
1363 ofp_to_string(const void *oh_, size_t len, int verbosity)
1365 struct ds string = DS_EMPTY_INITIALIZER;
1366 const struct ofp_header *oh = oh_;
1367 const struct openflow_packet *pkt;
1369 if (len < sizeof(struct ofp_header)) {
1370 ds_put_cstr(&string, "OpenFlow packet too short:\n");
1371 ds_put_hex_dump(&string, oh, len, 0, true);
1372 return ds_cstr(&string);
1373 } else if (oh->version != OFP_VERSION) {
1374 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", oh->version);
1375 ds_put_hex_dump(&string, oh, len, 0, true);
1376 return ds_cstr(&string);
1379 for (pkt = packets; ; pkt++) {
1380 if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1381 ds_put_format(&string, "Unknown OpenFlow packet type %"PRIu8":\n",
1383 ds_put_hex_dump(&string, oh, len, 0, true);
1384 return ds_cstr(&string);
1385 } else if (oh->type == pkt->type) {
1390 ds_put_format(&string, "%s (xid=0x%"PRIx32"):", pkt->name, oh->xid);
1392 if (ntohs(oh->length) > len)
1393 ds_put_format(&string, " (***truncated to %zu bytes from %"PRIu16"***)",
1394 len, ntohs(oh->length));
1395 else if (ntohs(oh->length) < len) {
1396 ds_put_format(&string, " (***only uses %"PRIu16" bytes out of %zu***)\n",
1397 ntohs(oh->length), len);
1398 len = ntohs(oh->length);
1401 if (len < pkt->min_size) {
1402 ds_put_format(&string, " (***length=%zu < min_size=%zu***)\n",
1403 len, pkt->min_size);
1404 } else if (!pkt->printer) {
1405 if (len > sizeof *oh) {
1406 ds_put_format(&string, " length=%"PRIu16" (decoder not implemented)\n",
1410 pkt->printer(&string, oh, len, verbosity);
1412 if (verbosity >= 3) {
1413 ds_put_hex_dump(&string, oh, len, 0, true);
1415 if (string.string[string.length - 1] != '\n') {
1416 ds_put_char(&string, '\n');
1418 return ds_cstr(&string);
1421 /* Returns the name for the specified OpenFlow message type as a string,
1422 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1423 * hex number, e.g. "0x55".
1425 * The caller must free the returned string when it is no longer needed. */
1427 ofp_message_type_to_string(uint8_t type)
1429 struct ds s = DS_EMPTY_INITIALIZER;
1430 const struct openflow_packet *pkt;
1431 for (pkt = packets; ; pkt++) {
1432 if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1433 ds_put_format(&s, "0x%02"PRIx8, type);
1435 } else if (type == pkt->type) {
1438 ds_put_cstr(&s, "OFPT_");
1439 for (p = pkt->name; *p; p++) {
1440 ds_put_char(&s, toupper((unsigned char) *p));
1449 print_and_free(FILE *stream, char *string)
1451 fputs(string, stream);
1455 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1456 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1457 * numbers increase verbosity. */
1459 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1461 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1464 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1465 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1466 * the Ethernet frame (of which 'len' bytes were captured).
1468 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1470 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1472 print_and_free(stream, ofp_packet_to_string(data, len, total_len));