2 * Copyright (c) 2008, 2009, 2010 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"
22 #include <sys/types.h>
23 #include <netinet/in.h>
30 #include "dynamic-string.h"
33 #include "openflow/openflow.h"
34 #include "openflow/nicira-ext.h"
40 static void ofp_print_port_name(struct ds *string, uint16_t port);
42 /* Returns a string that represents the contents of the Ethernet frame in the
43 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
44 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
45 * bytes were captured).
47 * The caller must free the returned string.
49 * This starts and kills a tcpdump subprocess so it's quite expensive. */
51 ofp_packet_to_string(const void *data, size_t len, size_t total_len OVS_UNUSED)
53 struct ds ds = DS_EMPTY_INITIALIZER;
62 buf.data = (void *) data;
67 ovs_error(errno, "tmpfile");
68 return xstrdup("<error>");
70 pcap_write_header(pcap);
71 pcap_write(pcap, &buf);
74 ovs_error(errno, "error writing temporary file");
78 snprintf(command, sizeof command, "/usr/sbin/tcpdump -e -n -r /dev/fd/%d 2>/dev/null",
80 tcpdump = popen(command, "r");
83 ovs_error(errno, "exec(\"%s\")", command);
84 return xstrdup("<error>");
87 while ((c = getc(tcpdump)) != EOF) {
91 status = pclose(tcpdump);
92 if (WIFEXITED(status)) {
93 if (WEXITSTATUS(status))
94 ovs_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
95 } else if (WIFSIGNALED(status)) {
96 ovs_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
101 /* Pretty-print the OFPT_PACKET_IN packet of 'len' bytes at 'oh' to 'stream'
102 * at the given 'verbosity' level. */
104 ofp_packet_in(struct ds *string, const void *oh, size_t len, int verbosity)
106 const struct ofp_packet_in *op = oh;
109 ds_put_format(string, " total_len=%"PRIu16" in_port=",
110 ntohs(op->total_len));
111 ofp_print_port_name(string, ntohs(op->in_port));
113 if (op->reason == OFPR_ACTION)
114 ds_put_cstr(string, " (via action)");
115 else if (op->reason != OFPR_NO_MATCH)
116 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
118 data_len = len - offsetof(struct ofp_packet_in, data);
119 ds_put_format(string, " data_len=%zu", data_len);
120 if (htonl(op->buffer_id) == UINT32_MAX) {
121 ds_put_format(string, " (unbuffered)");
122 if (ntohs(op->total_len) != data_len)
123 ds_put_format(string, " (***total_len != data_len***)");
125 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
126 if (ntohs(op->total_len) < data_len)
127 ds_put_format(string, " (***total_len < data_len***)");
129 ds_put_char(string, '\n');
133 struct ofpbuf packet;
134 struct ofp_match match;
135 packet.data = (void *) op->data;
136 packet.size = data_len;
137 flow_extract(&packet, ntohs(op->in_port), &flow);
138 flow_to_match(&flow, 0, &match);
139 ofp_print_match(string, &match, verbosity);
140 ds_put_char(string, '\n');
143 char *packet = ofp_packet_to_string(op->data, data_len,
144 ntohs(op->total_len));
145 ds_put_cstr(string, packet);
150 static void ofp_print_port_name(struct ds *string, uint16_t port)
169 case OFPP_CONTROLLER:
179 ds_put_format(string, "%"PRIu16, port);
182 ds_put_cstr(string, name);
186 ofp_print_nx_action(struct ds *string, const struct nx_action_header *nah)
188 switch (ntohs(nah->subtype)) {
189 case NXAST_RESUBMIT: {
190 const struct nx_action_resubmit *nar = (struct nx_action_resubmit *)nah;
191 ds_put_format(string, "resubmit:");
192 ofp_print_port_name(string, ntohs(nar->in_port));
197 ds_put_format(string, "***unknown Nicira action:%d***\n",
198 ntohs(nah->subtype));
203 ofp_print_action(struct ds *string, const struct ofp_action_header *ah,
209 struct openflow_action {
214 const struct openflow_action of_actions[] = {
216 sizeof(struct ofp_action_output),
217 sizeof(struct ofp_action_output),
219 [OFPAT_SET_VLAN_VID] = {
220 sizeof(struct ofp_action_vlan_vid),
221 sizeof(struct ofp_action_vlan_vid),
223 [OFPAT_SET_VLAN_PCP] = {
224 sizeof(struct ofp_action_vlan_pcp),
225 sizeof(struct ofp_action_vlan_pcp),
227 [OFPAT_STRIP_VLAN] = {
228 sizeof(struct ofp_action_header),
229 sizeof(struct ofp_action_header),
231 [OFPAT_SET_DL_SRC] = {
232 sizeof(struct ofp_action_dl_addr),
233 sizeof(struct ofp_action_dl_addr),
235 [OFPAT_SET_DL_DST] = {
236 sizeof(struct ofp_action_dl_addr),
237 sizeof(struct ofp_action_dl_addr),
239 [OFPAT_SET_NW_SRC] = {
240 sizeof(struct ofp_action_nw_addr),
241 sizeof(struct ofp_action_nw_addr),
243 [OFPAT_SET_NW_DST] = {
244 sizeof(struct ofp_action_nw_addr),
245 sizeof(struct ofp_action_nw_addr),
247 [OFPAT_SET_NW_TOS] = {
248 sizeof(struct ofp_action_nw_tos),
249 sizeof(struct ofp_action_nw_tos),
251 [OFPAT_SET_TP_SRC] = {
252 sizeof(struct ofp_action_tp_port),
253 sizeof(struct ofp_action_tp_port),
255 [OFPAT_SET_TP_DST] = {
256 sizeof(struct ofp_action_tp_port),
257 sizeof(struct ofp_action_tp_port),
259 /* OFPAT_VENDOR is not here, since it would blow up the array size. */
262 if (actions_len < sizeof *ah) {
263 ds_put_format(string, "***action array too short for next action***\n");
267 type = ntohs(ah->type);
268 len = ntohs(ah->len);
269 if (actions_len < len) {
270 ds_put_format(string, "***truncated action %"PRIu16"***\n", type);
274 if ((len % 8) != 0) {
275 ds_put_format(string,
276 "***action %"PRIu16" length not a multiple of 8***\n",
281 if (type < ARRAY_SIZE(of_actions)) {
282 const struct openflow_action *act = &of_actions[type];
283 if ((len < act->min_size) || (len > act->max_size)) {
284 ds_put_format(string,
285 "***action %"PRIu16" wrong length: %zu***\n", type, len);
292 struct ofp_action_output *oa = (struct ofp_action_output *)ah;
293 uint16_t port = ntohs(oa->port);
294 if (port < OFPP_MAX) {
295 ds_put_format(string, "output:%"PRIu16, port);
297 ofp_print_port_name(string, port);
298 if (port == OFPP_CONTROLLER) {
300 ds_put_format(string, ":%"PRIu16, ntohs(oa->max_len));
302 ds_put_cstr(string, ":all");
309 case OFPAT_SET_VLAN_VID: {
310 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
311 ds_put_format(string, "mod_vlan_vid:%"PRIu16, ntohs(va->vlan_vid));
315 case OFPAT_SET_VLAN_PCP: {
316 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
317 ds_put_format(string, "mod_vlan_pcp:%"PRIu8, va->vlan_pcp);
321 case OFPAT_STRIP_VLAN:
322 ds_put_cstr(string, "strip_vlan");
325 case OFPAT_SET_DL_SRC: {
326 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
327 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
328 ETH_ADDR_ARGS(da->dl_addr));
332 case OFPAT_SET_DL_DST: {
333 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
334 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
335 ETH_ADDR_ARGS(da->dl_addr));
339 case OFPAT_SET_NW_SRC: {
340 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
341 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&na->nw_addr));
345 case OFPAT_SET_NW_DST: {
346 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
347 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&na->nw_addr));
351 case OFPAT_SET_NW_TOS: {
352 struct ofp_action_nw_tos *nt = (struct ofp_action_nw_tos *)ah;
353 ds_put_format(string, "mod_nw_tos:%d", nt->nw_tos);
357 case OFPAT_SET_TP_SRC: {
358 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
359 ds_put_format(string, "mod_tp_src:%d", ntohs(ta->tp_port));
363 case OFPAT_SET_TP_DST: {
364 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
365 ds_put_format(string, "mod_tp_dst:%d", ntohs(ta->tp_port));
370 struct ofp_action_vendor_header *avh
371 = (struct ofp_action_vendor_header *)ah;
372 if (len < sizeof *avh) {
373 ds_put_format(string, "***ofpat_vendor truncated***\n");
376 if (avh->vendor == htonl(NX_VENDOR_ID)) {
377 ofp_print_nx_action(string, (struct nx_action_header *)avh);
379 ds_put_format(string, "vendor action:0x%x", ntohl(avh->vendor));
385 ds_put_format(string, "(decoder %"PRIu16" not implemented)", type);
393 ofp_print_actions(struct ds *string, const struct ofp_action_header *action,
396 uint8_t *p = (uint8_t *)action;
399 ds_put_cstr(string, "actions=");
401 ds_put_cstr(string, "drop");
403 while (actions_len > 0) {
405 ds_put_cstr(string, ",");
407 len = ofp_print_action(string, (struct ofp_action_header *)p,
417 /* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'string'
418 * at the given 'verbosity' level. */
419 static void ofp_packet_out(struct ds *string, const void *oh, size_t len,
422 const struct ofp_packet_out *opo = oh;
423 size_t actions_len = ntohs(opo->actions_len);
425 ds_put_cstr(string, " in_port=");
426 ofp_print_port_name(string, ntohs(opo->in_port));
428 ds_put_format(string, " actions_len=%zu ", actions_len);
429 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
430 ds_put_format(string, "***packet too short for action length***\n");
433 ofp_print_actions(string, opo->actions, actions_len);
435 if (ntohl(opo->buffer_id) == UINT32_MAX) {
436 int data_len = len - sizeof *opo - actions_len;
437 ds_put_format(string, " data_len=%d", data_len);
438 if (verbosity > 0 && len > sizeof *opo) {
439 char *packet = ofp_packet_to_string(
440 (uint8_t *)opo->actions + actions_len, data_len, data_len);
441 ds_put_char(string, '\n');
442 ds_put_cstr(string, packet);
446 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
448 ds_put_char(string, '\n');
451 /* qsort comparison function. */
453 compare_ports(const void *a_, const void *b_)
455 const struct ofp_phy_port *a = a_;
456 const struct ofp_phy_port *b = b_;
457 uint16_t ap = ntohs(a->port_no);
458 uint16_t bp = ntohs(b->port_no);
460 return ap < bp ? -1 : ap > bp;
463 static void ofp_print_port_features(struct ds *string, uint32_t features)
466 ds_put_cstr(string, "Unsupported\n");
469 if (features & OFPPF_10MB_HD) {
470 ds_put_cstr(string, "10MB-HD ");
472 if (features & OFPPF_10MB_FD) {
473 ds_put_cstr(string, "10MB-FD ");
475 if (features & OFPPF_100MB_HD) {
476 ds_put_cstr(string, "100MB-HD ");
478 if (features & OFPPF_100MB_FD) {
479 ds_put_cstr(string, "100MB-FD ");
481 if (features & OFPPF_1GB_HD) {
482 ds_put_cstr(string, "1GB-HD ");
484 if (features & OFPPF_1GB_FD) {
485 ds_put_cstr(string, "1GB-FD ");
487 if (features & OFPPF_10GB_FD) {
488 ds_put_cstr(string, "10GB-FD ");
490 if (features & OFPPF_COPPER) {
491 ds_put_cstr(string, "COPPER ");
493 if (features & OFPPF_FIBER) {
494 ds_put_cstr(string, "FIBER ");
496 if (features & OFPPF_AUTONEG) {
497 ds_put_cstr(string, "AUTO_NEG ");
499 if (features & OFPPF_PAUSE) {
500 ds_put_cstr(string, "AUTO_PAUSE ");
502 if (features & OFPPF_PAUSE_ASYM) {
503 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
505 ds_put_char(string, '\n');
509 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
511 uint8_t name[OFP_MAX_PORT_NAME_LEN];
514 memcpy(name, port->name, sizeof name);
515 for (j = 0; j < sizeof name - 1; j++) {
516 if (!isprint(name[j])) {
522 ds_put_char(string, ' ');
523 ofp_print_port_name(string, ntohs(port->port_no));
524 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
525 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
528 ds_put_format(string, " current: ");
529 ofp_print_port_features(string, ntohl(port->curr));
531 if (port->advertised) {
532 ds_put_format(string, " advertised: ");
533 ofp_print_port_features(string, ntohl(port->advertised));
535 if (port->supported) {
536 ds_put_format(string, " supported: ");
537 ofp_print_port_features(string, ntohl(port->supported));
540 ds_put_format(string, " peer: ");
541 ofp_print_port_features(string, ntohl(port->peer));
545 /* Pretty-print the struct ofp_switch_features of 'len' bytes at 'oh' to
546 * 'string' at the given 'verbosity' level. */
548 ofp_print_switch_features(struct ds *string, const void *oh, size_t len,
549 int verbosity OVS_UNUSED)
551 const struct ofp_switch_features *osf = oh;
552 struct ofp_phy_port *port_list;
556 ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
557 osf->header.version, ntohll(osf->datapath_id));
558 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
559 ntohl(osf->n_buffers));
560 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
561 ntohl(osf->capabilities), ntohl(osf->actions));
563 if (ntohs(osf->header.length) >= sizeof *osf) {
564 len = MIN(len, ntohs(osf->header.length));
566 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
568 port_list = xmemdup(osf->ports, len - sizeof *osf);
569 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
570 for (i = 0; i < n_ports; i++) {
571 ofp_print_phy_port(string, &port_list[i]);
576 /* Pretty-print the struct ofp_switch_config of 'len' bytes at 'oh' to 'string'
577 * at the given 'verbosity' level. */
579 ofp_print_switch_config(struct ds *string, const void *oh,
580 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
582 const struct ofp_switch_config *osc = oh;
585 flags = ntohs(osc->flags);
587 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
590 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
593 static void print_wild(struct ds *string, const char *leader, int is_wild,
594 int verbosity, const char *format, ...)
595 __attribute__((format(printf, 5, 6)));
597 static void print_wild(struct ds *string, const char *leader, int is_wild,
598 int verbosity, const char *format, ...)
600 if (is_wild && verbosity < 2) {
603 ds_put_cstr(string, leader);
607 va_start(args, format);
608 ds_put_format_valist(string, format, args);
611 ds_put_char(string, '*');
613 ds_put_char(string, ',');
617 print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
618 uint32_t wild_bits, int verbosity)
620 if (wild_bits >= 32 && verbosity < 2) {
623 ds_put_cstr(string, leader);
624 if (wild_bits < 32) {
625 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
627 ds_put_format(string, "/%d", 32 - wild_bits);
630 ds_put_char(string, '*');
632 ds_put_char(string, ',');
636 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
638 char *s = ofp_match_to_string(om, verbosity);
644 ofp_match_to_string(const struct ofp_match *om, int verbosity)
646 struct ds f = DS_EMPTY_INITIALIZER;
647 uint32_t w = ntohl(om->wildcards);
648 bool skip_type = false;
649 bool skip_proto = false;
651 if (!(w & OFPFW_DL_TYPE)) {
653 if (om->dl_type == htons(ETH_TYPE_IP)) {
654 if (!(w & OFPFW_NW_PROTO)) {
656 if (om->nw_proto == IP_TYPE_ICMP) {
657 ds_put_cstr(&f, "icmp,");
658 } else if (om->nw_proto == IP_TYPE_TCP) {
659 ds_put_cstr(&f, "tcp,");
660 } else if (om->nw_proto == IP_TYPE_UDP) {
661 ds_put_cstr(&f, "udp,");
663 ds_put_cstr(&f, "ip,");
667 ds_put_cstr(&f, "ip,");
669 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
670 ds_put_cstr(&f, "arp,");
675 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
676 "%d", ntohs(om->in_port));
677 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
678 "0x%04x", ntohs(om->dl_vlan));
679 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
680 "%d", om->dl_vlan_pcp);
681 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
682 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
683 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
684 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
686 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
687 "0x%04x", ntohs(om->dl_type));
689 print_ip_netmask(&f, "nw_src=", om->nw_src,
690 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
691 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
692 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
694 if (om->dl_type == htons(ETH_TYPE_ARP)) {
695 print_wild(&f, "opcode=", w & OFPFW_NW_PROTO, verbosity,
698 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
700 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
704 if (om->nw_proto == IP_TYPE_ICMP) {
705 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
706 "%d", ntohs(om->icmp_type));
707 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
708 "%d", ntohs(om->icmp_code));
710 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
711 "%d", ntohs(om->tp_src));
712 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
713 "%d", ntohs(om->tp_dst));
718 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
719 * at the given 'verbosity' level. */
721 ofp_print_flow_mod(struct ds *string, const void *oh, size_t len,
724 const struct ofp_flow_mod *ofm = oh;
726 ofp_print_match(string, &ofm->match, verbosity);
727 switch (ntohs(ofm->command)) {
729 ds_put_cstr(string, " ADD: ");
732 ds_put_cstr(string, " MOD: ");
734 case OFPFC_MODIFY_STRICT:
735 ds_put_cstr(string, " MOD_STRICT: ");
738 ds_put_cstr(string, " DEL: ");
740 case OFPFC_DELETE_STRICT:
741 ds_put_cstr(string, " DEL_STRICT: ");
744 ds_put_format(string, " cmd:%d ", ntohs(ofm->command));
746 ds_put_format(string, "cookie:%"PRIx64" idle:%d hard:%d pri:%d "
747 "buf:%#x flags:%"PRIx16" ", ntohll(ofm->cookie),
748 ntohs(ofm->idle_timeout), ntohs(ofm->hard_timeout),
749 ofm->match.wildcards ? ntohs(ofm->priority) : (uint16_t)-1,
750 ntohl(ofm->buffer_id), ntohs(ofm->flags));
751 ofp_print_actions(string, ofm->actions,
752 len - offsetof(struct ofp_flow_mod, actions));
753 ds_put_char(string, '\n');
756 /* Pretty-print the OFPT_FLOW_REMOVED packet of 'len' bytes at 'oh' to 'string'
757 * at the given 'verbosity' level. */
759 ofp_print_flow_removed(struct ds *string, const void *oh,
760 size_t len OVS_UNUSED, int verbosity)
762 const struct ofp_flow_removed *ofr = oh;
764 ofp_print_match(string, &ofr->match, verbosity);
765 ds_put_cstr(string, " reason=");
766 switch (ofr->reason) {
767 case OFPRR_IDLE_TIMEOUT:
768 ds_put_cstr(string, "idle");
770 case OFPRR_HARD_TIMEOUT:
771 ds_put_cstr(string, "hard");
774 ds_put_cstr(string, "delete");
777 ds_put_format(string, "**%"PRIu8"**", ofr->reason);
780 ds_put_format(string,
781 " cookie%"PRIx64" pri%"PRIu16" secs%"PRIu32" nsecs%"PRIu32
782 " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
784 ofr->match.wildcards ? ntohs(ofr->priority) : (uint16_t)-1,
785 ntohl(ofr->duration_sec), ntohl(ofr->duration_nsec),
786 ntohs(ofr->idle_timeout), ntohll(ofr->packet_count),
787 ntohll(ofr->byte_count));
791 ofp_print_port_mod(struct ds *string, const void *oh, size_t len OVS_UNUSED,
792 int verbosity OVS_UNUSED)
794 const struct ofp_port_mod *opm = oh;
796 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
797 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
798 ntohl(opm->config), ntohl(opm->mask));
799 ds_put_format(string, " advertise: ");
800 if (opm->advertise) {
801 ofp_print_port_features(string, ntohl(opm->advertise));
803 ds_put_format(string, "UNCHANGED\n");
813 static const struct error_type error_types[] = {
814 #define ERROR_TYPE(TYPE) {TYPE, -1, #TYPE}
815 #define ERROR_CODE(TYPE, CODE) {TYPE, CODE, #CODE}
816 ERROR_TYPE(OFPET_HELLO_FAILED),
817 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_INCOMPATIBLE),
818 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_EPERM),
820 ERROR_TYPE(OFPET_BAD_REQUEST),
821 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
822 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE),
823 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT),
824 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR),
825 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE),
826 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_EPERM),
827 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN),
828 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_EMPTY),
829 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_UNKNOWN),
831 ERROR_TYPE(OFPET_BAD_ACTION),
832 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE),
833 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_LEN),
834 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR),
835 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE),
836 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT),
837 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT),
838 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_EPERM),
839 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_TOO_MANY),
841 ERROR_TYPE(OFPET_FLOW_MOD_FAILED),
842 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL),
843 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP),
844 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_EPERM),
845 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_EMERG_TIMEOUT),
846 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND),
848 ERROR_TYPE(OFPET_PORT_MOD_FAILED),
849 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT),
850 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR)
852 #define N_ERROR_TYPES ARRAY_SIZE(error_types)
855 lookup_error_type(int type)
857 const struct error_type *t;
859 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
860 if (t->type == type && t->code == -1) {
868 lookup_error_code(int type, int code)
870 const struct error_type *t;
872 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
873 if (t->type == type && t->code == code) {
880 /* Pretty-print the OFPT_ERROR packet of 'len' bytes at 'oh' to 'string'
881 * at the given 'verbosity' level. */
883 ofp_print_error_msg(struct ds *string, const void *oh, size_t len,
884 int verbosity OVS_UNUSED)
886 const struct ofp_error_msg *oem = oh;
887 int type = ntohs(oem->type);
888 int code = ntohs(oem->code);
891 ds_put_format(string, " type%d(%s) code%d(%s) payload:\n",
892 type, lookup_error_type(type),
893 code, lookup_error_code(type, code));
896 case OFPET_HELLO_FAILED:
897 ds_put_printable(string, (char *) oem->data, len - sizeof *oem);
900 case OFPET_BAD_REQUEST:
901 s = ofp_to_string(oem->data, len - sizeof *oem, 1);
902 ds_put_cstr(string, s);
907 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
912 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'string'
913 * at the given 'verbosity' level. */
915 ofp_print_port_status(struct ds *string, const void *oh, size_t len OVS_UNUSED,
916 int verbosity OVS_UNUSED)
918 const struct ofp_port_status *ops = oh;
920 if (ops->reason == OFPPR_ADD) {
921 ds_put_format(string, " ADD:");
922 } else if (ops->reason == OFPPR_DELETE) {
923 ds_put_format(string, " DEL:");
924 } else if (ops->reason == OFPPR_MODIFY) {
925 ds_put_format(string, " MOD:");
928 ofp_print_phy_port(string, &ops->desc);
932 ofp_desc_stats_reply(struct ds *string, const void *body,
933 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
935 const struct ofp_desc_stats *ods = body;
937 ds_put_format(string, "Manufacturer: %.*s\n",
938 (int) sizeof ods->mfr_desc, ods->mfr_desc);
939 ds_put_format(string, "Hardware: %.*s\n",
940 (int) sizeof ods->hw_desc, ods->hw_desc);
941 ds_put_format(string, "Software: %.*s\n",
942 (int) sizeof ods->sw_desc, ods->sw_desc);
943 ds_put_format(string, "Serial Num: %.*s\n",
944 (int) sizeof ods->serial_num, ods->serial_num);
945 ds_put_format(string, "DP Description: %.*s\n",
946 (int) sizeof ods->dp_desc, ods->dp_desc);
950 ofp_flow_stats_request(struct ds *string, const void *oh,
951 size_t len OVS_UNUSED, int verbosity)
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, " cookie=%"PRIu64"s, ", ntohll(fs->cookie));
1004 ds_put_format(string, "duration_sec=%"PRIu32"s, ",
1005 ntohl(fs->duration_sec));
1006 ds_put_format(string, "duration_nsec=%"PRIu32"s, ",
1007 ntohl(fs->duration_nsec));
1008 ds_put_format(string, "table_id=%"PRIu8", ", fs->table_id);
1009 ds_put_format(string, "priority=%"PRIu16", ",
1010 fs->match.wildcards ? ntohs(fs->priority) : (uint16_t)-1);
1011 ds_put_format(string, "n_packets=%"PRIu64", ",
1012 ntohll(fs->packet_count));
1013 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
1014 if (fs->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
1015 ds_put_format(string, "idle_timeout=%"PRIu16",",
1016 ntohs(fs->idle_timeout));
1018 if (fs->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
1019 ds_put_format(string, "hard_timeout=%"PRIu16",",
1020 ntohs(fs->hard_timeout));
1022 ofp_print_match(string, &fs->match, verbosity);
1023 ofp_print_actions(string, fs->actions, length - sizeof *fs);
1024 ds_put_char(string, '\n');
1031 ofp_aggregate_stats_request(struct ds *string, const void *oh,
1032 size_t len OVS_UNUSED, int verbosity)
1034 const struct ofp_aggregate_stats_request *asr = oh;
1036 if (asr->table_id == 0xff) {
1037 ds_put_format(string, " table_id=any, ");
1039 ds_put_format(string, " table_id=%"PRIu8", ", asr->table_id);
1042 ofp_print_match(string, &asr->match, verbosity);
1046 ofp_aggregate_stats_reply(struct ds *string, const void *body_,
1047 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
1049 const struct ofp_aggregate_stats_reply *asr = body_;
1051 ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
1052 ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
1053 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1056 static void print_port_stat(struct ds *string, const char *leader,
1057 uint64_t stat, int more)
1059 ds_put_cstr(string, leader);
1061 ds_put_format(string, "%"PRIu64, stat);
1063 ds_put_char(string, '?');
1066 ds_put_cstr(string, ", ");
1068 ds_put_cstr(string, "\n");
1073 ofp_port_stats_request(struct ds *string, const void *body_,
1074 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
1076 const struct ofp_port_stats_request *psr = body_;
1077 ds_put_format(string, "port_no=%"PRIu16, ntohs(psr->port_no));
1081 ofp_port_stats_reply(struct ds *string, const void *body, size_t len,
1084 const struct ofp_port_stats *ps = body;
1085 size_t n = len / sizeof *ps;
1086 ds_put_format(string, " %zu ports\n", n);
1087 if (verbosity < 1) {
1092 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1094 ds_put_cstr(string, "rx ");
1095 print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
1096 print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
1097 print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
1098 print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
1099 print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
1100 print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
1101 print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
1103 ds_put_cstr(string, " tx ");
1104 print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
1105 print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
1106 print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
1107 print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
1108 print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
1113 ofp_table_stats_reply(struct ds *string, const void *body, size_t len,
1116 const struct ofp_table_stats *ts = body;
1117 size_t n = len / sizeof *ts;
1118 ds_put_format(string, " %zu tables\n", n);
1119 if (verbosity < 1) {
1124 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1125 strncpy(name, ts->name, sizeof name);
1126 name[OFP_MAX_TABLE_NAME_LEN] = '\0';
1128 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1129 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1130 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1131 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1132 ds_put_cstr(string, " ");
1133 ds_put_format(string, "lookup=%"PRIu64", ",
1134 ntohll(ts->lookup_count));
1135 ds_put_format(string, "matched=%"PRIu64"\n",
1136 ntohll(ts->matched_count));
1141 vendor_stat(struct ds *string, const void *body, size_t len,
1142 int verbosity OVS_UNUSED)
1144 ds_put_format(string, " vendor=%08"PRIx32, ntohl(*(uint32_t *) body));
1145 ds_put_format(string, " %zu bytes additional data",
1146 len - sizeof(uint32_t));
1149 enum stats_direction {
1155 print_stats(struct ds *string, int type, const void *body, size_t body_len,
1156 int verbosity, enum stats_direction direction)
1159 size_t min_body, max_body;
1160 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1166 struct stats_msg request;
1167 struct stats_msg reply;
1170 static const struct stats_type stats_types[] = {
1175 { 0, SIZE_MAX, ofp_desc_stats_reply },
1180 { sizeof(struct ofp_flow_stats_request),
1181 sizeof(struct ofp_flow_stats_request),
1182 ofp_flow_stats_request },
1183 { 0, SIZE_MAX, ofp_flow_stats_reply },
1188 { sizeof(struct ofp_aggregate_stats_request),
1189 sizeof(struct ofp_aggregate_stats_request),
1190 ofp_aggregate_stats_request },
1191 { sizeof(struct ofp_aggregate_stats_reply),
1192 sizeof(struct ofp_aggregate_stats_reply),
1193 ofp_aggregate_stats_reply },
1199 { 0, SIZE_MAX, ofp_table_stats_reply },
1204 { sizeof(struct ofp_port_stats_request),
1205 sizeof(struct ofp_port_stats_request),
1206 ofp_port_stats_request },
1207 { 0, SIZE_MAX, ofp_port_stats_reply },
1212 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1213 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1223 const struct stats_type *s;
1224 const struct stats_msg *m;
1226 if (type >= ARRAY_SIZE(stats_types) || !stats_types[type].name) {
1227 ds_put_format(string, " ***unknown type %d***", type);
1230 for (s = stats_types; s->type >= 0; s++) {
1231 if (s->type == type) {
1235 ds_put_format(string, " type=%d(%s)\n", type, s->name);
1237 m = direction == REQUEST ? &s->request : &s->reply;
1238 if (body_len < m->min_body || body_len > m->max_body) {
1239 ds_put_format(string, " ***body_len=%zu not in %zu...%zu***",
1240 body_len, m->min_body, m->max_body);
1244 m->printer(string, body, body_len, verbosity);
1249 ofp_stats_request(struct ds *string, const void *oh, size_t len, int verbosity)
1251 const struct ofp_stats_request *srq = oh;
1254 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1258 print_stats(string, ntohs(srq->type), srq->body,
1259 len - offsetof(struct ofp_stats_request, body),
1260 verbosity, REQUEST);
1264 ofp_stats_reply(struct ds *string, const void *oh, size_t len, int verbosity)
1266 const struct ofp_stats_reply *srp = oh;
1268 ds_put_cstr(string, " flags=");
1270 ds_put_cstr(string, "none");
1272 uint16_t flags = ntohs(srp->flags);
1273 if (flags & OFPSF_REPLY_MORE) {
1274 ds_put_cstr(string, "[more]");
1275 flags &= ~OFPSF_REPLY_MORE;
1278 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]", flags);
1282 print_stats(string, ntohs(srp->type), srp->body,
1283 len - offsetof(struct ofp_stats_reply, body),
1288 ofp_echo(struct ds *string, const void *oh, size_t len, int verbosity)
1290 const struct ofp_header *hdr = oh;
1292 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *hdr);
1293 if (verbosity > 1) {
1294 ds_put_hex_dump(string, hdr, len - sizeof *hdr, 0, true);
1298 struct openflow_packet {
1302 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1305 static const struct openflow_packet packets[] = {
1309 sizeof (struct ofp_header),
1313 OFPT_FEATURES_REQUEST,
1315 sizeof (struct ofp_header),
1319 OFPT_FEATURES_REPLY,
1321 sizeof (struct ofp_switch_features),
1322 ofp_print_switch_features,
1325 OFPT_GET_CONFIG_REQUEST,
1326 "get_config_request",
1327 sizeof (struct ofp_header),
1331 OFPT_GET_CONFIG_REPLY,
1333 sizeof (struct ofp_switch_config),
1334 ofp_print_switch_config,
1339 sizeof (struct ofp_switch_config),
1340 ofp_print_switch_config,
1345 offsetof(struct ofp_packet_in, data),
1351 sizeof (struct ofp_packet_out),
1357 sizeof (struct ofp_flow_mod),
1363 sizeof (struct ofp_flow_removed),
1364 ofp_print_flow_removed,
1369 sizeof (struct ofp_port_mod),
1375 sizeof (struct ofp_port_status),
1376 ofp_print_port_status
1381 sizeof (struct ofp_error_msg),
1382 ofp_print_error_msg,
1387 sizeof (struct ofp_stats_request),
1393 sizeof (struct ofp_stats_reply),
1399 sizeof (struct ofp_header),
1405 sizeof (struct ofp_header),
1411 sizeof (struct ofp_vendor_header),
1415 OFPT_BARRIER_REQUEST,
1417 sizeof (struct ofp_header),
1423 sizeof (struct ofp_header),
1428 /* Composes and returns a string representing the OpenFlow packet of 'len'
1429 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1430 * verbosity and higher numbers increase verbosity. The caller is responsible
1431 * for freeing the string. */
1433 ofp_to_string(const void *oh_, size_t len, int verbosity)
1435 struct ds string = DS_EMPTY_INITIALIZER;
1436 const struct ofp_header *oh = oh_;
1437 const struct openflow_packet *pkt;
1439 if (len < sizeof(struct ofp_header)) {
1440 ds_put_cstr(&string, "OpenFlow packet too short:\n");
1441 ds_put_hex_dump(&string, oh, len, 0, true);
1442 return ds_cstr(&string);
1443 } else if (oh->version != OFP_VERSION) {
1444 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", oh->version);
1445 ds_put_hex_dump(&string, oh, len, 0, true);
1446 return ds_cstr(&string);
1449 for (pkt = packets; ; pkt++) {
1450 if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1451 ds_put_format(&string, "Unknown OpenFlow packet type %"PRIu8":\n",
1453 ds_put_hex_dump(&string, oh, len, 0, true);
1454 return ds_cstr(&string);
1455 } else if (oh->type == pkt->type) {
1460 ds_put_format(&string, "%s (xid=0x%"PRIx32"):", pkt->name, oh->xid);
1462 if (ntohs(oh->length) > len)
1463 ds_put_format(&string, " (***truncated to %zu bytes from %"PRIu16"***)",
1464 len, ntohs(oh->length));
1465 else if (ntohs(oh->length) < len) {
1466 ds_put_format(&string, " (***only uses %"PRIu16" bytes out of %zu***)\n",
1467 ntohs(oh->length), len);
1468 len = ntohs(oh->length);
1471 if (len < pkt->min_size) {
1472 ds_put_format(&string, " (***length=%zu < min_size=%zu***)\n",
1473 len, pkt->min_size);
1474 } else if (!pkt->printer) {
1475 if (len > sizeof *oh) {
1476 ds_put_format(&string, " length=%"PRIu16" (decoder not implemented)\n",
1480 pkt->printer(&string, oh, len, verbosity);
1482 if (verbosity >= 3) {
1483 ds_put_hex_dump(&string, oh, len, 0, true);
1485 if (string.string[string.length - 1] != '\n') {
1486 ds_put_char(&string, '\n');
1488 return ds_cstr(&string);
1491 /* Returns the name for the specified OpenFlow message type as a string,
1492 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1493 * hex number, e.g. "0x55".
1495 * The caller must free the returned string when it is no longer needed. */
1497 ofp_message_type_to_string(uint8_t type)
1499 struct ds s = DS_EMPTY_INITIALIZER;
1500 const struct openflow_packet *pkt;
1501 for (pkt = packets; ; pkt++) {
1502 if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1503 ds_put_format(&s, "0x%02"PRIx8, type);
1505 } else if (type == pkt->type) {
1508 ds_put_cstr(&s, "OFPT_");
1509 for (p = pkt->name; *p; p++) {
1510 ds_put_char(&s, toupper((unsigned char) *p));
1519 print_and_free(FILE *stream, char *string)
1521 fputs(string, stream);
1525 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1526 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1527 * numbers increase verbosity. */
1529 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1531 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1534 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1535 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1536 * the Ethernet frame (of which 'len' bytes were captured).
1538 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1540 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1542 print_and_free(stream, ofp_packet_to_string(data, len, total_len));