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);
41 /* Returns a string that represents the contents of the Ethernet frame in the
42 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
43 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
44 * bytes were captured).
46 * The caller must free the returned string.
48 * This starts and kills a tcpdump subprocess so it's quite expensive. */
50 ofp_packet_to_string(const void *data, size_t len, size_t total_len UNUSED)
52 struct ds ds = DS_EMPTY_INITIALIZER;
61 buf.data = (void *) data;
66 ovs_error(errno, "tmpfile");
67 return xstrdup("<error>");
69 pcap_write_header(pcap);
70 pcap_write(pcap, &buf);
73 ovs_error(errno, "error writing temporary file");
77 snprintf(command, sizeof command, "/usr/sbin/tcpdump -e -n -r /dev/fd/%d 2>/dev/null",
79 tcpdump = popen(command, "r");
82 ovs_error(errno, "exec(\"%s\")", command);
83 return xstrdup("<error>");
86 while ((c = getc(tcpdump)) != EOF) {
90 status = pclose(tcpdump);
91 if (WIFEXITED(status)) {
92 if (WEXITSTATUS(status))
93 ovs_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
94 } else if (WIFSIGNALED(status)) {
95 ovs_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
100 /* Pretty-print the OFPT_PACKET_IN packet of 'len' bytes at 'oh' to 'stream'
101 * at the given 'verbosity' level. */
103 ofp_packet_in(struct ds *string, const void *oh, size_t len, int verbosity)
105 const struct ofp_packet_in *op = oh;
108 ds_put_format(string, " total_len=%"PRIu16" in_port=",
109 ntohs(op->total_len));
110 ofp_print_port_name(string, ntohs(op->in_port));
112 if (op->reason == OFPR_ACTION)
113 ds_put_cstr(string, " (via action)");
114 else if (op->reason != OFPR_NO_MATCH)
115 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
117 data_len = len - offsetof(struct ofp_packet_in, data);
118 ds_put_format(string, " data_len=%zu", data_len);
119 if (htonl(op->buffer_id) == UINT32_MAX) {
120 ds_put_format(string, " (unbuffered)");
121 if (ntohs(op->total_len) != data_len)
122 ds_put_format(string, " (***total_len != data_len***)");
124 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
125 if (ntohs(op->total_len) < data_len)
126 ds_put_format(string, " (***total_len < data_len***)");
128 ds_put_char(string, '\n');
132 struct ofpbuf packet;
133 struct ofp_match match;
134 packet.data = (void *) op->data;
135 packet.size = data_len;
136 flow_extract(&packet, ntohs(op->in_port), &flow);
137 flow_to_match(&flow, 0, &match);
138 ofp_print_match(string, &match, verbosity);
139 ds_put_char(string, '\n');
142 char *packet = ofp_packet_to_string(op->data, data_len,
143 ntohs(op->total_len));
144 ds_put_cstr(string, packet);
149 static void ofp_print_port_name(struct ds *string, uint16_t port)
168 case OFPP_CONTROLLER:
178 ds_put_format(string, "%"PRIu16, port);
181 ds_put_cstr(string, name);
185 ofp_print_nx_action(struct ds *string, const struct nx_action_header *nah)
187 switch (ntohs(nah->subtype)) {
188 case NXAST_RESUBMIT: {
189 const struct nx_action_resubmit *nar = (struct nx_action_resubmit *)nah;
190 ds_put_format(string, "resubmit:");
191 ofp_print_port_name(string, ntohs(nar->in_port));
196 ds_put_format(string, "***unknown Nicira action:%d***\n",
197 ntohs(nah->subtype));
202 ofp_print_action(struct ds *string, const struct ofp_action_header *ah,
208 struct openflow_action {
213 const struct openflow_action of_actions[] = {
215 sizeof(struct ofp_action_output),
216 sizeof(struct ofp_action_output),
218 [OFPAT_SET_VLAN_VID] = {
219 sizeof(struct ofp_action_vlan_vid),
220 sizeof(struct ofp_action_vlan_vid),
222 [OFPAT_SET_VLAN_PCP] = {
223 sizeof(struct ofp_action_vlan_pcp),
224 sizeof(struct ofp_action_vlan_pcp),
226 [OFPAT_STRIP_VLAN] = {
227 sizeof(struct ofp_action_header),
228 sizeof(struct ofp_action_header),
230 [OFPAT_SET_DL_SRC] = {
231 sizeof(struct ofp_action_dl_addr),
232 sizeof(struct ofp_action_dl_addr),
234 [OFPAT_SET_DL_DST] = {
235 sizeof(struct ofp_action_dl_addr),
236 sizeof(struct ofp_action_dl_addr),
238 [OFPAT_SET_NW_SRC] = {
239 sizeof(struct ofp_action_nw_addr),
240 sizeof(struct ofp_action_nw_addr),
242 [OFPAT_SET_NW_DST] = {
243 sizeof(struct ofp_action_nw_addr),
244 sizeof(struct ofp_action_nw_addr),
246 [OFPAT_SET_NW_TOS] = {
247 sizeof(struct ofp_action_nw_tos),
248 sizeof(struct ofp_action_nw_tos),
250 [OFPAT_SET_TP_SRC] = {
251 sizeof(struct ofp_action_tp_port),
252 sizeof(struct ofp_action_tp_port),
254 [OFPAT_SET_TP_DST] = {
255 sizeof(struct ofp_action_tp_port),
256 sizeof(struct ofp_action_tp_port),
258 /* OFPAT_VENDOR is not here, since it would blow up the array size. */
261 if (actions_len < sizeof *ah) {
262 ds_put_format(string, "***action array too short for next action***\n");
266 type = ntohs(ah->type);
267 len = ntohs(ah->len);
268 if (actions_len < len) {
269 ds_put_format(string, "***truncated action %"PRIu16"***\n", type);
273 if ((len % 8) != 0) {
274 ds_put_format(string,
275 "***action %"PRIu16" length not a multiple of 8***\n",
280 if (type < ARRAY_SIZE(of_actions)) {
281 const struct openflow_action *act = &of_actions[type];
282 if ((len < act->min_size) || (len > act->max_size)) {
283 ds_put_format(string,
284 "***action %"PRIu16" wrong length: %zu***\n", type, len);
291 struct ofp_action_output *oa = (struct ofp_action_output *)ah;
292 uint16_t port = ntohs(oa->port);
293 if (port < OFPP_MAX) {
294 ds_put_format(string, "output:%"PRIu16, port);
296 ofp_print_port_name(string, port);
297 if (port == OFPP_CONTROLLER) {
299 ds_put_format(string, ":%"PRIu16, ntohs(oa->max_len));
301 ds_put_cstr(string, ":all");
308 case OFPAT_SET_VLAN_VID: {
309 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
310 ds_put_format(string, "mod_vlan_vid:%"PRIu16, ntohs(va->vlan_vid));
314 case OFPAT_SET_VLAN_PCP: {
315 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
316 ds_put_format(string, "mod_vlan_pcp:%"PRIu8, va->vlan_pcp);
320 case OFPAT_STRIP_VLAN:
321 ds_put_cstr(string, "strip_vlan");
324 case OFPAT_SET_DL_SRC: {
325 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
326 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
327 ETH_ADDR_ARGS(da->dl_addr));
331 case OFPAT_SET_DL_DST: {
332 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
333 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
334 ETH_ADDR_ARGS(da->dl_addr));
338 case OFPAT_SET_NW_SRC: {
339 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
340 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&na->nw_addr));
344 case OFPAT_SET_NW_DST: {
345 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
346 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&na->nw_addr));
350 case OFPAT_SET_NW_TOS: {
351 struct ofp_action_nw_tos *nt = (struct ofp_action_nw_tos *)ah;
352 ds_put_format(string, "mod_nw_tos:%d", nt->nw_tos);
356 case OFPAT_SET_TP_SRC: {
357 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
358 ds_put_format(string, "mod_tp_src:%d", ntohs(ta->tp_port));
362 case OFPAT_SET_TP_DST: {
363 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
364 ds_put_format(string, "mod_tp_dst:%d", ntohs(ta->tp_port));
369 struct ofp_action_vendor_header *avh
370 = (struct ofp_action_vendor_header *)ah;
371 if (len < sizeof *avh) {
372 ds_put_format(string, "***ofpat_vendor truncated***\n");
375 if (avh->vendor == htonl(NX_VENDOR_ID)) {
376 ofp_print_nx_action(string, (struct nx_action_header *)avh);
378 ds_put_format(string, "vendor action:0x%x", ntohl(avh->vendor));
384 ds_put_format(string, "(decoder %"PRIu16" not implemented)", type);
392 ofp_print_actions(struct ds *string, const struct ofp_action_header *action,
395 uint8_t *p = (uint8_t *)action;
398 ds_put_cstr(string, "actions=");
400 ds_put_cstr(string, "drop");
402 while (actions_len > 0) {
404 ds_put_cstr(string, ",");
406 len = ofp_print_action(string, (struct ofp_action_header *)p,
416 /* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'string'
417 * at the given 'verbosity' level. */
418 static void ofp_packet_out(struct ds *string, const void *oh, size_t len,
421 const struct ofp_packet_out *opo = oh;
422 size_t actions_len = ntohs(opo->actions_len);
424 ds_put_cstr(string, " in_port=");
425 ofp_print_port_name(string, ntohs(opo->in_port));
427 ds_put_format(string, " actions_len=%zu ", actions_len);
428 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
429 ds_put_format(string, "***packet too short for action length***\n");
432 ofp_print_actions(string, opo->actions, actions_len);
434 if (ntohl(opo->buffer_id) == UINT32_MAX) {
435 int data_len = len - sizeof *opo - actions_len;
436 ds_put_format(string, " data_len=%d", data_len);
437 if (verbosity > 0 && len > sizeof *opo) {
438 char *packet = ofp_packet_to_string(
439 (uint8_t *)opo->actions + actions_len, data_len, data_len);
440 ds_put_char(string, '\n');
441 ds_put_cstr(string, packet);
445 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
447 ds_put_char(string, '\n');
450 /* qsort comparison function. */
452 compare_ports(const void *a_, const void *b_)
454 const struct ofp_phy_port *a = a_;
455 const struct ofp_phy_port *b = b_;
456 uint16_t ap = ntohs(a->port_no);
457 uint16_t bp = ntohs(b->port_no);
459 return ap < bp ? -1 : ap > bp;
462 static void ofp_print_port_features(struct ds *string, uint32_t features)
465 ds_put_cstr(string, "Unsupported\n");
468 if (features & OFPPF_10MB_HD) {
469 ds_put_cstr(string, "10MB-HD ");
471 if (features & OFPPF_10MB_FD) {
472 ds_put_cstr(string, "10MB-FD ");
474 if (features & OFPPF_100MB_HD) {
475 ds_put_cstr(string, "100MB-HD ");
477 if (features & OFPPF_100MB_FD) {
478 ds_put_cstr(string, "100MB-FD ");
480 if (features & OFPPF_1GB_HD) {
481 ds_put_cstr(string, "1GB-HD ");
483 if (features & OFPPF_1GB_FD) {
484 ds_put_cstr(string, "1GB-FD ");
486 if (features & OFPPF_10GB_FD) {
487 ds_put_cstr(string, "10GB-FD ");
489 if (features & OFPPF_COPPER) {
490 ds_put_cstr(string, "COPPER ");
492 if (features & OFPPF_FIBER) {
493 ds_put_cstr(string, "FIBER ");
495 if (features & OFPPF_AUTONEG) {
496 ds_put_cstr(string, "AUTO_NEG ");
498 if (features & OFPPF_PAUSE) {
499 ds_put_cstr(string, "AUTO_PAUSE ");
501 if (features & OFPPF_PAUSE_ASYM) {
502 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
504 ds_put_char(string, '\n');
508 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
510 uint8_t name[OFP_MAX_PORT_NAME_LEN];
513 memcpy(name, port->name, sizeof name);
514 for (j = 0; j < sizeof name - 1; j++) {
515 if (!isprint(name[j])) {
521 ds_put_char(string, ' ');
522 ofp_print_port_name(string, ntohs(port->port_no));
523 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
524 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
527 ds_put_format(string, " current: ");
528 ofp_print_port_features(string, ntohl(port->curr));
530 if (port->advertised) {
531 ds_put_format(string, " advertised: ");
532 ofp_print_port_features(string, ntohl(port->advertised));
534 if (port->supported) {
535 ds_put_format(string, " supported: ");
536 ofp_print_port_features(string, ntohl(port->supported));
539 ds_put_format(string, " peer: ");
540 ofp_print_port_features(string, ntohl(port->peer));
544 /* Pretty-print the struct ofp_switch_features of 'len' bytes at 'oh' to
545 * 'string' at the given 'verbosity' level. */
547 ofp_print_switch_features(struct ds *string, const void *oh, size_t len,
548 int verbosity UNUSED)
550 const struct ofp_switch_features *osf = oh;
551 struct ofp_phy_port *port_list;
555 ds_put_format(string, " ver:0x%x, dpid:%"PRIx64"\n",
556 osf->header.version, ntohll(osf->datapath_id));
557 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
558 ntohl(osf->n_buffers));
559 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
560 ntohl(osf->capabilities), ntohl(osf->actions));
562 if (ntohs(osf->header.length) >= sizeof *osf) {
563 len = MIN(len, ntohs(osf->header.length));
565 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
567 port_list = xmemdup(osf->ports, len - sizeof *osf);
568 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
569 for (i = 0; i < n_ports; i++) {
570 ofp_print_phy_port(string, &port_list[i]);
575 /* Pretty-print the struct ofp_switch_config of 'len' bytes at 'oh' to 'string'
576 * at the given 'verbosity' level. */
578 ofp_print_switch_config(struct ds *string, const void *oh, size_t len UNUSED,
579 int verbosity UNUSED)
581 const struct ofp_switch_config *osc = oh;
584 flags = ntohs(osc->flags);
585 if (flags & OFPC_SEND_FLOW_EXP) {
586 flags &= ~OFPC_SEND_FLOW_EXP;
587 ds_put_format(string, " (sending flow expirations)");
590 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
593 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
596 static void print_wild(struct ds *string, const char *leader, int is_wild,
597 int verbosity, const char *format, ...)
598 __attribute__((format(printf, 5, 6)));
600 static void print_wild(struct ds *string, const char *leader, int is_wild,
601 int verbosity, const char *format, ...)
603 if (is_wild && verbosity < 2) {
606 ds_put_cstr(string, leader);
610 va_start(args, format);
611 ds_put_format_valist(string, format, args);
614 ds_put_char(string, '*');
616 ds_put_char(string, ',');
620 print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
621 uint32_t wild_bits, int verbosity)
623 if (wild_bits >= 32 && verbosity < 2) {
626 ds_put_cstr(string, leader);
627 if (wild_bits < 32) {
628 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
630 ds_put_format(string, "/%d", 32 - wild_bits);
633 ds_put_char(string, '*');
635 ds_put_char(string, ',');
639 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
641 char *s = ofp_match_to_string(om, verbosity);
647 ofp_match_to_string(const struct ofp_match *om, int verbosity)
649 struct ds f = DS_EMPTY_INITIALIZER;
650 uint32_t w = ntohl(om->wildcards);
651 bool skip_type = false;
652 bool skip_proto = false;
654 if (!(w & OFPFW_DL_TYPE)) {
656 if (om->dl_type == htons(ETH_TYPE_IP)) {
657 if (!(w & OFPFW_NW_PROTO)) {
659 if (om->nw_proto == IP_TYPE_ICMP) {
660 ds_put_cstr(&f, "icmp,");
661 } else if (om->nw_proto == IP_TYPE_TCP) {
662 ds_put_cstr(&f, "tcp,");
663 } else if (om->nw_proto == IP_TYPE_UDP) {
664 ds_put_cstr(&f, "udp,");
666 ds_put_cstr(&f, "ip,");
670 ds_put_cstr(&f, "ip,");
672 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
673 ds_put_cstr(&f, "arp,");
678 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
679 "%d", ntohs(om->in_port));
680 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
681 "0x%04x", ntohs(om->dl_vlan));
682 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
683 "%d", om->dl_vlan_pcp);
684 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
685 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
686 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
687 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
689 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
690 "0x%04x", ntohs(om->dl_type));
692 print_ip_netmask(&f, "nw_src=", om->nw_src,
693 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
694 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
695 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
697 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
700 if (om->nw_proto == IP_TYPE_ICMP) {
701 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
702 "%d", ntohs(om->icmp_type));
703 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
704 "%d", ntohs(om->icmp_code));
706 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
707 "%d", ntohs(om->tp_src));
708 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
709 "%d", ntohs(om->tp_dst));
714 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
715 * at the given 'verbosity' level. */
717 ofp_print_flow_mod(struct ds *string, const void *oh, size_t len,
720 const struct ofp_flow_mod *ofm = oh;
722 ofp_print_match(string, &ofm->match, verbosity);
723 switch (ntohs(ofm->command)) {
725 ds_put_cstr(string, " ADD: ");
728 ds_put_cstr(string, " MOD: ");
730 case OFPFC_MODIFY_STRICT:
731 ds_put_cstr(string, " MOD_STRICT: ");
734 ds_put_cstr(string, " DEL: ");
736 case OFPFC_DELETE_STRICT:
737 ds_put_cstr(string, " DEL_STRICT: ");
740 ds_put_format(string, " cmd:%d ", ntohs(ofm->command));
742 ds_put_format(string, "idle:%d hard:%d pri:%d buf:%#x",
743 ntohs(ofm->idle_timeout), ntohs(ofm->hard_timeout),
744 ofm->match.wildcards ? ntohs(ofm->priority) : (uint16_t)-1,
745 ntohl(ofm->buffer_id));
746 ofp_print_actions(string, ofm->actions,
747 len - offsetof(struct ofp_flow_mod, actions));
748 ds_put_char(string, '\n');
751 /* Pretty-print the OFPT_FLOW_EXPIRED packet of 'len' bytes at 'oh' to 'string'
752 * at the given 'verbosity' level. */
754 ofp_print_flow_expired(struct ds *string, const void *oh, size_t len UNUSED,
757 const struct ofp_flow_expired *ofe = oh;
759 ofp_print_match(string, &ofe->match, verbosity);
760 ds_put_cstr(string, " reason=");
761 switch (ofe->reason) {
762 case OFPER_IDLE_TIMEOUT:
763 ds_put_cstr(string, "idle");
765 case OFPER_HARD_TIMEOUT:
766 ds_put_cstr(string, "hard");
769 ds_put_format(string, "**%"PRIu8"**", ofe->reason);
772 ds_put_format(string,
773 " pri%"PRIu16" secs%"PRIu32" pkts%"PRIu64" bytes%"PRIu64"\n",
774 ofe->match.wildcards ? ntohs(ofe->priority) : (uint16_t)-1,
775 ntohl(ofe->duration), ntohll(ofe->packet_count),
776 ntohll(ofe->byte_count));
780 ofp_print_port_mod(struct ds *string, const void *oh, size_t len UNUSED,
781 int verbosity UNUSED)
783 const struct ofp_port_mod *opm = oh;
785 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
786 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
787 ntohl(opm->config), ntohl(opm->mask));
788 ds_put_format(string, " advertise: ");
789 if (opm->advertise) {
790 ofp_print_port_features(string, ntohl(opm->advertise));
792 ds_put_format(string, "UNCHANGED\n");
802 static const struct error_type error_types[] = {
803 #define ERROR_TYPE(TYPE) {TYPE, -1, #TYPE}
804 #define ERROR_CODE(TYPE, CODE) {TYPE, CODE, #CODE}
805 ERROR_TYPE(OFPET_HELLO_FAILED),
806 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_INCOMPATIBLE),
808 ERROR_TYPE(OFPET_BAD_REQUEST),
809 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
810 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE),
811 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT),
812 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR),
813 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE),
814 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH),
815 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_EMPTY),
816 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_COOKIE),
818 ERROR_TYPE(OFPET_BAD_ACTION),
819 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE),
820 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_LEN),
821 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR),
822 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE),
823 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT),
824 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT),
825 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_TOO_MANY),
827 ERROR_TYPE(OFPET_FLOW_MOD_FAILED),
828 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL),
829 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND),
831 ERROR_TYPE(OFPET_PORT_MOD_FAILED),
832 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT),
833 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR)
835 #define N_ERROR_TYPES ARRAY_SIZE(error_types)
838 lookup_error_type(int type)
840 const struct error_type *t;
842 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
843 if (t->type == type && t->code == -1) {
851 lookup_error_code(int type, int code)
853 const struct error_type *t;
855 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
856 if (t->type == type && t->code == code) {
863 /* Pretty-print the OFPT_ERROR packet of 'len' bytes at 'oh' to 'string'
864 * at the given 'verbosity' level. */
866 ofp_print_error_msg(struct ds *string, const void *oh, size_t len,
867 int verbosity UNUSED)
869 const struct ofp_error_msg *oem = oh;
870 int type = ntohs(oem->type);
871 int code = ntohs(oem->code);
874 ds_put_format(string, " type%d(%s) code%d(%s) payload:\n",
875 type, lookup_error_type(type),
876 code, lookup_error_code(type, code));
879 case OFPET_HELLO_FAILED:
880 ds_put_printable(string, (char *) oem->data, len - sizeof *oem);
883 case OFPET_BAD_REQUEST:
884 s = ofp_to_string(oem->data, len - sizeof *oem, 1);
885 ds_put_cstr(string, s);
890 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
895 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'string'
896 * at the given 'verbosity' level. */
898 ofp_print_port_status(struct ds *string, const void *oh, size_t len UNUSED,
899 int verbosity UNUSED)
901 const struct ofp_port_status *ops = oh;
903 if (ops->reason == OFPPR_ADD) {
904 ds_put_format(string, " ADD:");
905 } else if (ops->reason == OFPPR_DELETE) {
906 ds_put_format(string, " DEL:");
907 } else if (ops->reason == OFPPR_MODIFY) {
908 ds_put_format(string, " MOD:");
911 ofp_print_phy_port(string, &ops->desc);
915 ofp_desc_stats_reply(struct ds *string, const void *body, size_t len UNUSED,
916 int verbosity UNUSED)
918 const struct ofp_desc_stats *ods = body;
920 ds_put_format(string, "Manufacturer: %s\n", ods->mfr_desc);
921 ds_put_format(string, "Hardware: %s\n", ods->hw_desc);
922 ds_put_format(string, "Software: %s\n", ods->sw_desc);
923 ds_put_format(string, "Serial Num: %s\n", ods->serial_num);
927 ofp_flow_stats_request(struct ds *string, const void *oh, size_t len UNUSED,
930 const struct ofp_flow_stats_request *fsr = oh;
932 if (fsr->table_id == 0xff) {
933 ds_put_format(string, " table_id=any, ");
935 ds_put_format(string, " table_id=%"PRIu8", ", fsr->table_id);
938 ofp_print_match(string, &fsr->match, verbosity);
942 ofp_flow_stats_reply(struct ds *string, const void *body_, size_t len,
945 const char *body = body_;
946 const char *pos = body;
948 const struct ofp_flow_stats *fs;
949 ptrdiff_t bytes_left = body + len - pos;
952 if (bytes_left < sizeof *fs) {
953 if (bytes_left != 0) {
954 ds_put_format(string, " ***%td leftover bytes at end***",
960 fs = (const void *) pos;
961 length = ntohs(fs->length);
962 if (length < sizeof *fs) {
963 ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
966 } else if (length > bytes_left) {
967 ds_put_format(string,
968 " ***length=%zu but only %td bytes left***",
971 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
972 ds_put_format(string,
973 " ***length=%zu has %zu bytes leftover in "
976 (length - sizeof *fs) % sizeof fs->actions[0]);
980 ds_put_format(string, " duration=%"PRIu32"s, ", ntohl(fs->duration));
981 ds_put_format(string, "table_id=%"PRIu8", ", fs->table_id);
982 ds_put_format(string, "priority=%"PRIu16", ",
983 fs->match.wildcards ? ntohs(fs->priority) : (uint16_t)-1);
984 ds_put_format(string, "n_packets=%"PRIu64", ",
985 ntohll(fs->packet_count));
986 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
987 if (fs->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
988 ds_put_format(string, "idle_timeout=%"PRIu16",",
989 ntohs(fs->idle_timeout));
991 if (fs->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
992 ds_put_format(string, "hard_timeout=%"PRIu16",",
993 ntohs(fs->hard_timeout));
995 ofp_print_match(string, &fs->match, verbosity);
996 ofp_print_actions(string, fs->actions, length - sizeof *fs);
997 ds_put_char(string, '\n');
1004 ofp_aggregate_stats_request(struct ds *string, const void *oh,
1005 size_t len UNUSED, int verbosity)
1007 const struct ofp_aggregate_stats_request *asr = oh;
1009 if (asr->table_id == 0xff) {
1010 ds_put_format(string, " table_id=any, ");
1012 ds_put_format(string, " table_id=%"PRIu8", ", asr->table_id);
1015 ofp_print_match(string, &asr->match, verbosity);
1019 ofp_aggregate_stats_reply(struct ds *string, const void *body_,
1020 size_t len UNUSED, int verbosity UNUSED)
1022 const struct ofp_aggregate_stats_reply *asr = body_;
1024 ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
1025 ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
1026 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1029 static void print_port_stat(struct ds *string, const char *leader,
1030 uint64_t stat, int more)
1032 ds_put_cstr(string, leader);
1034 ds_put_format(string, "%"PRIu64, stat);
1036 ds_put_char(string, '?');
1039 ds_put_cstr(string, ", ");
1041 ds_put_cstr(string, "\n");
1046 ofp_port_stats_reply(struct ds *string, const void *body, size_t len,
1049 const struct ofp_port_stats *ps = body;
1050 size_t n = len / sizeof *ps;
1051 ds_put_format(string, " %zu ports\n", n);
1052 if (verbosity < 1) {
1057 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1059 ds_put_cstr(string, "rx ");
1060 print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
1061 print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
1062 print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
1063 print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
1064 print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
1065 print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
1066 print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
1068 ds_put_cstr(string, " tx ");
1069 print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
1070 print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
1071 print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
1072 print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
1073 print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
1078 ofp_table_stats_reply(struct ds *string, const void *body, size_t len,
1081 const struct ofp_table_stats *ts = body;
1082 size_t n = len / sizeof *ts;
1083 ds_put_format(string, " %zu tables\n", n);
1084 if (verbosity < 1) {
1089 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1090 strncpy(name, ts->name, sizeof name);
1091 name[OFP_MAX_TABLE_NAME_LEN] = '\0';
1093 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1094 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1095 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1096 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1097 ds_put_cstr(string, " ");
1098 ds_put_format(string, "lookup=%"PRIu64", ",
1099 ntohll(ts->lookup_count));
1100 ds_put_format(string, "matched=%"PRIu64"\n",
1101 ntohll(ts->matched_count));
1106 vendor_stat(struct ds *string, const void *body, size_t len,
1107 int verbosity UNUSED)
1109 ds_put_format(string, " vendor=%08"PRIx32, ntohl(*(uint32_t *) body));
1110 ds_put_format(string, " %zu bytes additional data",
1111 len - sizeof(uint32_t));
1114 enum stats_direction {
1120 print_stats(struct ds *string, int type, const void *body, size_t body_len,
1121 int verbosity, enum stats_direction direction)
1124 size_t min_body, max_body;
1125 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1131 struct stats_msg request;
1132 struct stats_msg reply;
1135 static const struct stats_type stats_types[] = {
1140 { 0, SIZE_MAX, ofp_desc_stats_reply },
1145 { sizeof(struct ofp_flow_stats_request),
1146 sizeof(struct ofp_flow_stats_request),
1147 ofp_flow_stats_request },
1148 { 0, SIZE_MAX, ofp_flow_stats_reply },
1153 { sizeof(struct ofp_aggregate_stats_request),
1154 sizeof(struct ofp_aggregate_stats_request),
1155 ofp_aggregate_stats_request },
1156 { sizeof(struct ofp_aggregate_stats_reply),
1157 sizeof(struct ofp_aggregate_stats_reply),
1158 ofp_aggregate_stats_reply },
1164 { 0, SIZE_MAX, ofp_table_stats_reply },
1170 { 0, SIZE_MAX, ofp_port_stats_reply },
1175 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1176 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1186 const struct stats_type *s;
1187 const struct stats_msg *m;
1189 if (type >= ARRAY_SIZE(stats_types) || !stats_types[type].name) {
1190 ds_put_format(string, " ***unknown type %d***", type);
1193 for (s = stats_types; s->type >= 0; s++) {
1194 if (s->type == type) {
1198 ds_put_format(string, " type=%d(%s)\n", type, s->name);
1200 m = direction == REQUEST ? &s->request : &s->reply;
1201 if (body_len < m->min_body || body_len > m->max_body) {
1202 ds_put_format(string, " ***body_len=%zu not in %zu...%zu***",
1203 body_len, m->min_body, m->max_body);
1207 m->printer(string, body, body_len, verbosity);
1212 ofp_stats_request(struct ds *string, const void *oh, size_t len, int verbosity)
1214 const struct ofp_stats_request *srq = oh;
1217 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1221 print_stats(string, ntohs(srq->type), srq->body,
1222 len - offsetof(struct ofp_stats_request, body),
1223 verbosity, REQUEST);
1227 ofp_stats_reply(struct ds *string, const void *oh, size_t len, int verbosity)
1229 const struct ofp_stats_reply *srp = oh;
1231 ds_put_cstr(string, " flags=");
1233 ds_put_cstr(string, "none");
1235 uint16_t flags = ntohs(srp->flags);
1236 if (flags & OFPSF_REPLY_MORE) {
1237 ds_put_cstr(string, "[more]");
1238 flags &= ~OFPSF_REPLY_MORE;
1241 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]", flags);
1245 print_stats(string, ntohs(srp->type), srp->body,
1246 len - offsetof(struct ofp_stats_reply, body),
1251 ofp_echo(struct ds *string, const void *oh, size_t len, int verbosity)
1253 const struct ofp_header *hdr = oh;
1255 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *hdr);
1256 if (verbosity > 1) {
1257 ds_put_hex_dump(string, hdr, len - sizeof *hdr, 0, true);
1261 struct openflow_packet {
1265 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1268 static const struct openflow_packet packets[] = {
1272 sizeof (struct ofp_header),
1276 OFPT_FEATURES_REQUEST,
1278 sizeof (struct ofp_header),
1282 OFPT_FEATURES_REPLY,
1284 sizeof (struct ofp_switch_features),
1285 ofp_print_switch_features,
1288 OFPT_GET_CONFIG_REQUEST,
1289 "get_config_request",
1290 sizeof (struct ofp_header),
1294 OFPT_GET_CONFIG_REPLY,
1296 sizeof (struct ofp_switch_config),
1297 ofp_print_switch_config,
1302 sizeof (struct ofp_switch_config),
1303 ofp_print_switch_config,
1308 offsetof(struct ofp_packet_in, data),
1314 sizeof (struct ofp_packet_out),
1320 sizeof (struct ofp_flow_mod),
1326 sizeof (struct ofp_flow_expired),
1327 ofp_print_flow_expired,
1332 sizeof (struct ofp_port_mod),
1338 sizeof (struct ofp_port_status),
1339 ofp_print_port_status
1344 sizeof (struct ofp_error_msg),
1345 ofp_print_error_msg,
1350 sizeof (struct ofp_stats_request),
1356 sizeof (struct ofp_stats_reply),
1362 sizeof (struct ofp_header),
1368 sizeof (struct ofp_header),
1374 sizeof (struct ofp_vendor_header),
1379 /* Composes and returns a string representing the OpenFlow packet of 'len'
1380 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1381 * verbosity and higher numbers increase verbosity. The caller is responsible
1382 * for freeing the string. */
1384 ofp_to_string(const void *oh_, size_t len, int verbosity)
1386 struct ds string = DS_EMPTY_INITIALIZER;
1387 const struct ofp_header *oh = oh_;
1388 const struct openflow_packet *pkt;
1390 if (len < sizeof(struct ofp_header)) {
1391 ds_put_cstr(&string, "OpenFlow packet too short:\n");
1392 ds_put_hex_dump(&string, oh, len, 0, true);
1393 return ds_cstr(&string);
1394 } else if (oh->version != OFP_VERSION) {
1395 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", oh->version);
1396 ds_put_hex_dump(&string, oh, len, 0, true);
1397 return ds_cstr(&string);
1400 for (pkt = packets; ; pkt++) {
1401 if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1402 ds_put_format(&string, "Unknown OpenFlow packet type %"PRIu8":\n",
1404 ds_put_hex_dump(&string, oh, len, 0, true);
1405 return ds_cstr(&string);
1406 } else if (oh->type == pkt->type) {
1411 ds_put_format(&string, "%s (xid=0x%"PRIx32"):", pkt->name, oh->xid);
1413 if (ntohs(oh->length) > len)
1414 ds_put_format(&string, " (***truncated to %zu bytes from %"PRIu16"***)",
1415 len, ntohs(oh->length));
1416 else if (ntohs(oh->length) < len) {
1417 ds_put_format(&string, " (***only uses %"PRIu16" bytes out of %zu***)\n",
1418 ntohs(oh->length), len);
1419 len = ntohs(oh->length);
1422 if (len < pkt->min_size) {
1423 ds_put_format(&string, " (***length=%zu < min_size=%zu***)\n",
1424 len, pkt->min_size);
1425 } else if (!pkt->printer) {
1426 if (len > sizeof *oh) {
1427 ds_put_format(&string, " length=%"PRIu16" (decoder not implemented)\n",
1431 pkt->printer(&string, oh, len, verbosity);
1433 if (verbosity >= 3) {
1434 ds_put_hex_dump(&string, oh, len, 0, true);
1436 if (string.string[string.length - 1] != '\n') {
1437 ds_put_char(&string, '\n');
1439 return ds_cstr(&string);
1442 /* Returns the name for the specified OpenFlow message type as a string,
1443 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1444 * hex number, e.g. "0x55".
1446 * The caller must free the returned string when it is no longer needed. */
1448 ofp_message_type_to_string(uint8_t type)
1450 struct ds s = DS_EMPTY_INITIALIZER;
1451 const struct openflow_packet *pkt;
1452 for (pkt = packets; ; pkt++) {
1453 if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1454 ds_put_format(&s, "0x%02"PRIx8, type);
1456 } else if (type == pkt->type) {
1459 ds_put_cstr(&s, "OFPT_");
1460 for (p = pkt->name; *p; p++) {
1461 ds_put_char(&s, toupper((unsigned char) *p));
1470 print_and_free(FILE *stream, char *string)
1472 fputs(string, stream);
1476 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1477 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1478 * numbers increase verbosity. */
1480 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1482 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1485 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1486 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1487 * the Ethernet frame (of which 'len' bytes were captured).
1489 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1491 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1493 print_and_free(stream, ofp_packet_to_string(data, len, total_len));