2 * Copyright (c) 2008, 2009, 2010, 2011 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>
29 #include "byte-order.h"
31 #include "dynamic-string.h"
33 #include "multipath.h"
37 #include "openflow/openflow.h"
38 #include "openflow/nicira-ext.h"
41 #include "type-props.h"
42 #include "unaligned.h"
45 static void ofp_print_port_name(struct ds *string, uint16_t port);
46 static void ofp_print_queue_name(struct ds *string, uint32_t port);
47 static void ofp_print_error(struct ds *, int error);
50 /* Returns a string that represents the contents of the Ethernet frame in the
51 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
52 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
53 * bytes were captured).
55 * The caller must free the returned string.
57 * This starts and kills a tcpdump subprocess so it's quite expensive. */
59 ofp_packet_to_string(const void *data, size_t len, size_t total_len OVS_UNUSED)
61 struct ds ds = DS_EMPTY_INITIALIZER;
70 ofpbuf_use_const(&buf, data, len);
74 ovs_error(errno, "tmpfile");
75 return xstrdup("<error>");
77 pcap_write_header(pcap);
78 pcap_write(pcap, &buf);
81 ovs_error(errno, "error writing temporary file");
85 snprintf(command, sizeof command, "/usr/sbin/tcpdump -t -e -n -r /dev/fd/%d 2>/dev/null",
87 tcpdump = popen(command, "r");
90 ovs_error(errno, "exec(\"%s\")", command);
91 return xstrdup("<error>");
94 while ((c = getc(tcpdump)) != EOF) {
98 status = pclose(tcpdump);
99 if (WIFEXITED(status)) {
100 if (WEXITSTATUS(status))
101 ovs_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
102 } else if (WIFSIGNALED(status)) {
103 ovs_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
109 ofp_print_packet_in(struct ds *string, const struct ofp_packet_in *op,
112 size_t len = ntohs(op->header.length);
115 ds_put_format(string, " total_len=%"PRIu16" in_port=",
116 ntohs(op->total_len));
117 ofp_print_port_name(string, ntohs(op->in_port));
119 if (op->reason == OFPR_ACTION)
120 ds_put_cstr(string, " (via action)");
121 else if (op->reason != OFPR_NO_MATCH)
122 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
124 data_len = len - offsetof(struct ofp_packet_in, data);
125 ds_put_format(string, " data_len=%zu", data_len);
126 if (op->buffer_id == htonl(UINT32_MAX)) {
127 ds_put_format(string, " (unbuffered)");
128 if (ntohs(op->total_len) != data_len)
129 ds_put_format(string, " (***total_len != data_len***)");
131 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
132 if (ntohs(op->total_len) < data_len)
133 ds_put_format(string, " (***total_len < data_len***)");
135 ds_put_char(string, '\n');
139 struct ofpbuf packet;
141 ofpbuf_use_const(&packet, op->data, data_len);
142 flow_extract(&packet, 0, ntohs(op->in_port), &flow);
143 flow_format(string, &flow);
144 ds_put_char(string, '\n');
147 char *packet = ofp_packet_to_string(op->data, data_len,
148 ntohs(op->total_len));
149 ds_put_cstr(string, packet);
154 static void ofp_print_port_name(struct ds *string, uint16_t port)
173 case OFPP_CONTROLLER:
183 ds_put_format(string, "%"PRIu16, port);
186 ds_put_cstr(string, name);
191 print_note(struct ds *string, const struct nx_action_note *nan)
196 ds_put_cstr(string, "note:");
197 len = ntohs(nan->len) - offsetof(struct nx_action_note, note);
198 for (i = 0; i < len; i++) {
200 ds_put_char(string, '.');
202 ds_put_format(string, "%02"PRIx8, nan->note[i]);
207 nx_action_len(enum nx_action_subtype subtype)
210 case NXAST_SNAT__OBSOLETE: return -1;
211 case NXAST_RESUBMIT: return sizeof(struct nx_action_resubmit);
212 case NXAST_SET_TUNNEL: return sizeof(struct nx_action_set_tunnel);
213 case NXAST_DROP_SPOOFED_ARP__OBSOLETE: return -1;
214 case NXAST_SET_QUEUE: return sizeof(struct nx_action_set_queue);
215 case NXAST_POP_QUEUE: return sizeof(struct nx_action_pop_queue);
216 case NXAST_REG_MOVE: return sizeof(struct nx_action_reg_move);
217 case NXAST_REG_LOAD: return sizeof(struct nx_action_reg_load);
218 case NXAST_NOTE: return -1;
219 case NXAST_SET_TUNNEL64: return sizeof(struct nx_action_set_tunnel64);
220 case NXAST_MULTIPATH: return sizeof(struct nx_action_multipath);
221 case NXAST_AUTOPATH: return sizeof (struct nx_action_autopath);
227 ofp_print_nx_action(struct ds *string, const struct nx_action_header *nah)
229 int subtype = ntohs(nah->subtype);
230 int required_len = nx_action_len(subtype);
231 int len = ntohs(nah->len);
233 if (required_len != -1 && required_len != len) {
234 ds_put_format(string, "***Nicira action %"PRIu16" wrong length: %d***",
239 if (subtype <= TYPE_MAXIMUM(enum nx_action_subtype)) {
240 const struct nx_action_set_tunnel64 *nast64;
241 const struct nx_action_set_tunnel *nast;
242 const struct nx_action_set_queue *nasq;
243 const struct nx_action_resubmit *nar;
244 const struct nx_action_reg_move *move;
245 const struct nx_action_reg_load *load;
246 const struct nx_action_multipath *nam;
247 const struct nx_action_autopath *naa;
249 switch ((enum nx_action_subtype) subtype) {
251 nar = (struct nx_action_resubmit *)nah;
252 ds_put_format(string, "resubmit:");
253 ofp_print_port_name(string, ntohs(nar->in_port));
256 case NXAST_SET_TUNNEL:
257 nast = (struct nx_action_set_tunnel *)nah;
258 ds_put_format(string, "set_tunnel:%#"PRIx32, ntohl(nast->tun_id));
261 case NXAST_SET_QUEUE:
262 nasq = (struct nx_action_set_queue *)nah;
263 ds_put_format(string, "set_queue:%u", ntohl(nasq->queue_id));
266 case NXAST_POP_QUEUE:
267 ds_put_cstr(string, "pop_queue");
271 print_note(string, (const struct nx_action_note *) nah);
275 move = (const struct nx_action_reg_move *) nah;
276 nxm_format_reg_move(move, string);
280 load = (const struct nx_action_reg_load *) nah;
281 nxm_format_reg_load(load, string);
284 case NXAST_SET_TUNNEL64:
285 nast64 = (const struct nx_action_set_tunnel64 *) nah;
286 ds_put_format(string, "set_tunnel64:%#"PRIx64,
287 ntohll(nast64->tun_id));
290 case NXAST_MULTIPATH:
291 nam = (const struct nx_action_multipath *) nah;
292 multipath_format(nam, string);
296 naa = (const struct nx_action_autopath *)nah;
297 ds_put_format(string, "autopath(%u,", ntohl(naa->id));
298 nxm_format_field_bits(string, ntohl(naa->dst),
299 nxm_decode_ofs(naa->ofs_nbits),
300 nxm_decode_n_bits(naa->ofs_nbits));
301 ds_put_char(string, ')');
304 case NXAST_SNAT__OBSOLETE:
305 case NXAST_DROP_SPOOFED_ARP__OBSOLETE:
311 ds_put_format(string, "***unknown Nicira action:%d***", subtype);
315 ofp_action_len(enum ofp_action_type type)
318 case OFPAT_OUTPUT: return sizeof(struct ofp_action_output);
319 case OFPAT_SET_VLAN_VID: return sizeof(struct ofp_action_vlan_vid);
320 case OFPAT_SET_VLAN_PCP: return sizeof(struct ofp_action_vlan_pcp);
321 case OFPAT_STRIP_VLAN: return sizeof(struct ofp_action_header);
322 case OFPAT_SET_DL_SRC: return sizeof(struct ofp_action_dl_addr);
323 case OFPAT_SET_DL_DST: return sizeof(struct ofp_action_dl_addr);
324 case OFPAT_SET_NW_SRC: return sizeof(struct ofp_action_nw_addr);
325 case OFPAT_SET_NW_DST: return sizeof(struct ofp_action_nw_addr);
326 case OFPAT_SET_NW_TOS: return sizeof(struct ofp_action_nw_tos);
327 case OFPAT_SET_TP_SRC: return sizeof(struct ofp_action_tp_port);
328 case OFPAT_SET_TP_DST: return sizeof(struct ofp_action_tp_port);
329 case OFPAT_ENQUEUE: return sizeof(struct ofp_action_enqueue);
330 case OFPAT_VENDOR: return -1;
336 ofp_print_action(struct ds *string, const struct ofp_action_header *ah,
339 enum ofp_action_type type;
343 if (actions_len < sizeof *ah) {
344 ds_put_format(string, "***action array too short for next action***\n");
348 type = ntohs(ah->type);
349 len = ntohs(ah->len);
350 if (actions_len < len) {
351 ds_put_format(string, "***truncated action %d***\n", (int) type);
356 ds_put_format(string, "***zero-length action***\n");
360 if ((len % OFP_ACTION_ALIGN) != 0) {
361 ds_put_format(string,
362 "***action %d length not a multiple of %d***\n",
363 (int) type, OFP_ACTION_ALIGN);
367 required_len = ofp_action_len(type);
368 if (required_len >= 0 && len != required_len) {
369 ds_put_format(string,
370 "***action %d wrong length: %zu***\n", (int) type, len);
376 struct ofp_action_output *oa = (struct ofp_action_output *)ah;
377 uint16_t port = ntohs(oa->port);
378 if (port < OFPP_MAX) {
379 ds_put_format(string, "output:%"PRIu16, port);
381 ofp_print_port_name(string, port);
382 if (port == OFPP_CONTROLLER) {
384 ds_put_format(string, ":%"PRIu16, ntohs(oa->max_len));
386 ds_put_cstr(string, ":all");
393 case OFPAT_ENQUEUE: {
394 struct ofp_action_enqueue *ea = (struct ofp_action_enqueue *)ah;
395 unsigned int port = ntohs(ea->port);
396 unsigned int queue_id = ntohl(ea->queue_id);
397 ds_put_format(string, "enqueue:");
398 if (port != OFPP_IN_PORT) {
399 ds_put_format(string, "%u", port);
401 ds_put_cstr(string, "IN_PORT");
403 ds_put_format(string, "q%u", queue_id);
407 case OFPAT_SET_VLAN_VID: {
408 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
409 ds_put_format(string, "mod_vlan_vid:%"PRIu16, ntohs(va->vlan_vid));
413 case OFPAT_SET_VLAN_PCP: {
414 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
415 ds_put_format(string, "mod_vlan_pcp:%"PRIu8, va->vlan_pcp);
419 case OFPAT_STRIP_VLAN:
420 ds_put_cstr(string, "strip_vlan");
423 case OFPAT_SET_DL_SRC: {
424 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
425 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
426 ETH_ADDR_ARGS(da->dl_addr));
430 case OFPAT_SET_DL_DST: {
431 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
432 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
433 ETH_ADDR_ARGS(da->dl_addr));
437 case OFPAT_SET_NW_SRC: {
438 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
439 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&na->nw_addr));
443 case OFPAT_SET_NW_DST: {
444 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
445 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&na->nw_addr));
449 case OFPAT_SET_NW_TOS: {
450 struct ofp_action_nw_tos *nt = (struct ofp_action_nw_tos *)ah;
451 ds_put_format(string, "mod_nw_tos:%d", nt->nw_tos);
455 case OFPAT_SET_TP_SRC: {
456 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
457 ds_put_format(string, "mod_tp_src:%d", ntohs(ta->tp_port));
461 case OFPAT_SET_TP_DST: {
462 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
463 ds_put_format(string, "mod_tp_dst:%d", ntohs(ta->tp_port));
468 struct ofp_action_vendor_header *avh
469 = (struct ofp_action_vendor_header *)ah;
470 if (len < sizeof *avh) {
471 ds_put_format(string, "***ofpat_vendor truncated***\n");
474 if (avh->vendor == htonl(NX_VENDOR_ID)) {
475 ofp_print_nx_action(string, (struct nx_action_header *)avh);
477 ds_put_format(string, "vendor action:0x%x", ntohl(avh->vendor));
483 ds_put_format(string, "(decoder %d not implemented)", (int) type);
491 ofp_print_actions(struct ds *string, const struct ofp_action_header *action,
494 uint8_t *p = (uint8_t *)action;
497 ds_put_cstr(string, "actions=");
499 ds_put_cstr(string, "drop");
501 while (actions_len > 0) {
503 ds_put_cstr(string, ",");
505 len = ofp_print_action(string, (struct ofp_action_header *)p,
516 ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
519 size_t len = ntohs(opo->header.length);
520 size_t actions_len = ntohs(opo->actions_len);
522 ds_put_cstr(string, " in_port=");
523 ofp_print_port_name(string, ntohs(opo->in_port));
525 ds_put_format(string, " actions_len=%zu ", actions_len);
526 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
527 ds_put_format(string, "***packet too short for action length***\n");
530 ofp_print_actions(string, opo->actions, actions_len);
532 if (ntohl(opo->buffer_id) == UINT32_MAX) {
533 int data_len = len - sizeof *opo - actions_len;
534 ds_put_format(string, " data_len=%d", data_len);
535 if (verbosity > 0 && len > sizeof *opo) {
536 char *packet = ofp_packet_to_string(
537 (uint8_t *)opo->actions + actions_len, data_len, data_len);
538 ds_put_char(string, '\n');
539 ds_put_cstr(string, packet);
543 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
545 ds_put_char(string, '\n');
548 /* qsort comparison function. */
550 compare_ports(const void *a_, const void *b_)
552 const struct ofp_phy_port *a = a_;
553 const struct ofp_phy_port *b = b_;
554 uint16_t ap = ntohs(a->port_no);
555 uint16_t bp = ntohs(b->port_no);
557 return ap < bp ? -1 : ap > bp;
560 static void ofp_print_port_features(struct ds *string, uint32_t features)
563 ds_put_cstr(string, "Unsupported\n");
566 if (features & OFPPF_10MB_HD) {
567 ds_put_cstr(string, "10MB-HD ");
569 if (features & OFPPF_10MB_FD) {
570 ds_put_cstr(string, "10MB-FD ");
572 if (features & OFPPF_100MB_HD) {
573 ds_put_cstr(string, "100MB-HD ");
575 if (features & OFPPF_100MB_FD) {
576 ds_put_cstr(string, "100MB-FD ");
578 if (features & OFPPF_1GB_HD) {
579 ds_put_cstr(string, "1GB-HD ");
581 if (features & OFPPF_1GB_FD) {
582 ds_put_cstr(string, "1GB-FD ");
584 if (features & OFPPF_10GB_FD) {
585 ds_put_cstr(string, "10GB-FD ");
587 if (features & OFPPF_COPPER) {
588 ds_put_cstr(string, "COPPER ");
590 if (features & OFPPF_FIBER) {
591 ds_put_cstr(string, "FIBER ");
593 if (features & OFPPF_AUTONEG) {
594 ds_put_cstr(string, "AUTO_NEG ");
596 if (features & OFPPF_PAUSE) {
597 ds_put_cstr(string, "AUTO_PAUSE ");
599 if (features & OFPPF_PAUSE_ASYM) {
600 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
602 ds_put_char(string, '\n');
606 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
608 char name[OFP_MAX_PORT_NAME_LEN];
611 memcpy(name, port->name, sizeof name);
612 for (j = 0; j < sizeof name - 1; j++) {
613 if (!isprint((unsigned char) name[j])) {
619 ds_put_char(string, ' ');
620 ofp_print_port_name(string, ntohs(port->port_no));
621 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
622 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
625 ds_put_format(string, " current: ");
626 ofp_print_port_features(string, ntohl(port->curr));
628 if (port->advertised) {
629 ds_put_format(string, " advertised: ");
630 ofp_print_port_features(string, ntohl(port->advertised));
632 if (port->supported) {
633 ds_put_format(string, " supported: ");
634 ofp_print_port_features(string, ntohl(port->supported));
637 ds_put_format(string, " peer: ");
638 ofp_print_port_features(string, ntohl(port->peer));
643 ofp_print_switch_features(struct ds *string,
644 const struct ofp_switch_features *osf)
646 size_t len = ntohs(osf->header.length);
647 struct ofp_phy_port *port_list;
651 ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
652 osf->header.version, ntohll(osf->datapath_id));
653 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
654 ntohl(osf->n_buffers));
655 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
656 ntohl(osf->capabilities), ntohl(osf->actions));
658 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
660 port_list = xmemdup(osf->ports, len - sizeof *osf);
661 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
662 for (i = 0; i < n_ports; i++) {
663 ofp_print_phy_port(string, &port_list[i]);
669 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
673 flags = ntohs(osc->flags);
675 ds_put_cstr(string, " frags=");
676 switch (flags & OFPC_FRAG_MASK) {
677 case OFPC_FRAG_NORMAL:
678 ds_put_cstr(string, "normal");
679 flags &= ~OFPC_FRAG_MASK;
682 ds_put_cstr(string, "drop");
683 flags &= ~OFPC_FRAG_MASK;
685 case OFPC_FRAG_REASM:
686 ds_put_cstr(string, "reassemble");
687 flags &= ~OFPC_FRAG_MASK;
691 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
694 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
697 static void print_wild(struct ds *string, const char *leader, int is_wild,
698 int verbosity, const char *format, ...)
699 __attribute__((format(printf, 5, 6)));
701 static void print_wild(struct ds *string, const char *leader, int is_wild,
702 int verbosity, const char *format, ...)
704 if (is_wild && verbosity < 2) {
707 ds_put_cstr(string, leader);
711 va_start(args, format);
712 ds_put_format_valist(string, format, args);
715 ds_put_char(string, '*');
717 ds_put_char(string, ',');
721 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
722 uint32_t wild_bits, int verbosity)
724 if (wild_bits >= 32 && verbosity < 2) {
727 ds_put_cstr(string, leader);
728 if (wild_bits < 32) {
729 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
731 ds_put_format(string, "/%d", 32 - wild_bits);
734 ds_put_char(string, '*');
736 ds_put_char(string, ',');
740 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
742 char *s = ofp_match_to_string(om, verbosity);
748 ofp_match_to_string(const struct ofp_match *om, int verbosity)
750 struct ds f = DS_EMPTY_INITIALIZER;
751 uint32_t w = ntohl(om->wildcards);
752 bool skip_type = false;
753 bool skip_proto = false;
755 if (!(w & OFPFW_DL_TYPE)) {
757 if (om->dl_type == htons(ETH_TYPE_IP)) {
758 if (!(w & OFPFW_NW_PROTO)) {
760 if (om->nw_proto == IPPROTO_ICMP) {
761 ds_put_cstr(&f, "icmp,");
762 } else if (om->nw_proto == IPPROTO_TCP) {
763 ds_put_cstr(&f, "tcp,");
764 } else if (om->nw_proto == IPPROTO_UDP) {
765 ds_put_cstr(&f, "udp,");
767 ds_put_cstr(&f, "ip,");
771 ds_put_cstr(&f, "ip,");
773 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
774 ds_put_cstr(&f, "arp,");
779 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
780 "%d", ntohs(om->in_port));
781 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
782 "%d", ntohs(om->dl_vlan));
783 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
784 "%d", om->dl_vlan_pcp);
785 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
786 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
787 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
788 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
790 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
791 "0x%04x", ntohs(om->dl_type));
793 print_ip_netmask(&f, "nw_src=", om->nw_src,
794 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
795 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
796 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
798 if (om->dl_type == htons(ETH_TYPE_ARP)) {
799 print_wild(&f, "opcode=", w & OFPFW_NW_PROTO, verbosity,
802 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
806 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
808 if (om->nw_proto == IPPROTO_ICMP) {
809 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
810 "%d", ntohs(om->icmp_type));
811 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
812 "%d", ntohs(om->icmp_code));
814 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
815 "%d", ntohs(om->tp_src));
816 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
817 "%d", ntohs(om->tp_dst));
819 if (ds_last(&f) == ',') {
826 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
827 enum ofputil_msg_code code, int verbosity)
833 error = ofputil_decode_flow_mod(&fm, oh, true);
835 ofp_print_error(s, error);
840 switch (fm.command) {
842 ds_put_cstr(s, "ADD");
845 ds_put_cstr(s, "MOD");
847 case OFPFC_MODIFY_STRICT:
848 ds_put_cstr(s, "MOD_STRICT");
851 ds_put_cstr(s, "DEL");
853 case OFPFC_DELETE_STRICT:
854 ds_put_cstr(s, "DEL_STRICT");
857 ds_put_format(s, "cmd:%d", fm.command);
859 if (fm.table_id != 0) {
860 ds_put_format(s, " table_id:%d", fm.table_id);
864 if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
865 const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
866 ofp_print_match(s, &ofm->match, verbosity);
868 /* ofp_print_match() doesn't print priority. */
869 need_priority = true;
870 } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
871 const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
872 const void *nxm = nfm + 1;
875 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
876 ds_put_cstr(s, nxm_s);
879 /* nx_match_to_string() doesn't print priority. */
880 need_priority = true;
882 cls_rule_format(&fm.cr, s);
884 /* cls_rule_format() does print priority. */
885 need_priority = false;
888 if (ds_last(s) != ' ') {
891 if (fm.cookie != htonll(0)) {
892 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.cookie));
894 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
895 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
897 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
898 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
900 if (fm.cr.priority != OFP_DEFAULT_PRIORITY && need_priority) {
901 ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
903 if (fm.buffer_id != UINT32_MAX) {
904 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
907 ds_put_format(s, "flags:0x%"PRIx16" ", fm.flags);
910 ofp_print_actions(s, (const struct ofp_action_header *) fm.actions,
911 fm.n_actions * sizeof *fm.actions);
915 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
917 ds_put_format(string, "%u", sec);
919 ds_put_format(string, ".%09u", nsec);
920 while (string->string[string->length - 1] == '0') {
924 ds_put_char(string, 's');
928 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
930 struct ofputil_flow_removed fr;
933 error = ofputil_decode_flow_removed(&fr, oh);
935 ofp_print_error(string, error);
939 ds_put_char(string, ' ');
940 cls_rule_format(&fr.rule, string);
942 ds_put_cstr(string, " reason=");
944 case OFPRR_IDLE_TIMEOUT:
945 ds_put_cstr(string, "idle");
947 case OFPRR_HARD_TIMEOUT:
948 ds_put_cstr(string, "hard");
951 ds_put_cstr(string, "delete");
954 ds_put_format(string, "**%"PRIu8"**", fr.reason);
958 if (fr.cookie != htonll(0)) {
959 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
961 ds_put_cstr(string, " duration");
962 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
963 ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
964 fr.idle_timeout, fr.packet_count, fr.byte_count);
968 ofp_print_port_mod(struct ds *string, const struct ofp_port_mod *opm)
970 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
971 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
972 ntohl(opm->config), ntohl(opm->mask));
973 ds_put_format(string, " advertise: ");
974 if (opm->advertise) {
975 ofp_print_port_features(string, ntohl(opm->advertise));
977 ds_put_format(string, "UNCHANGED\n");
982 ofp_print_error(struct ds *string, int error)
984 if (string->length) {
985 ds_put_char(string, ' ');
987 ds_put_cstr(string, "***decode error: ");
988 ofputil_format_error(string, error);
989 ds_put_cstr(string, "***\n");
993 ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
995 size_t len = ntohs(oem->header.length);
996 size_t payload_ofs, payload_len;
1001 error = ofputil_decode_error_msg(&oem->header, &payload_ofs);
1002 if (!is_ofp_error(error)) {
1003 ofp_print_error(string, error);
1004 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
1008 ds_put_char(string, ' ');
1009 ofputil_format_error(string, error);
1010 ds_put_char(string, '\n');
1012 payload = (const uint8_t *) oem + payload_ofs;
1013 payload_len = len - payload_ofs;
1014 switch (get_ofp_err_type(error)) {
1015 case OFPET_HELLO_FAILED:
1016 ds_put_printable(string, payload, payload_len);
1019 case OFPET_BAD_REQUEST:
1020 s = ofp_to_string(payload, payload_len, 1);
1021 ds_put_cstr(string, s);
1026 ds_put_hex_dump(string, payload, payload_len, 0, true);
1032 ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
1034 if (ops->reason == OFPPR_ADD) {
1035 ds_put_format(string, " ADD:");
1036 } else if (ops->reason == OFPPR_DELETE) {
1037 ds_put_format(string, " DEL:");
1038 } else if (ops->reason == OFPPR_MODIFY) {
1039 ds_put_format(string, " MOD:");
1042 ofp_print_phy_port(string, &ops->desc);
1046 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
1048 const struct ofp_desc_stats *ods = ofputil_stats_body(oh);
1050 ds_put_char(string, '\n');
1051 ds_put_format(string, "Manufacturer: %.*s\n",
1052 (int) sizeof ods->mfr_desc, ods->mfr_desc);
1053 ds_put_format(string, "Hardware: %.*s\n",
1054 (int) sizeof ods->hw_desc, ods->hw_desc);
1055 ds_put_format(string, "Software: %.*s\n",
1056 (int) sizeof ods->sw_desc, ods->sw_desc);
1057 ds_put_format(string, "Serial Num: %.*s\n",
1058 (int) sizeof ods->serial_num, ods->serial_num);
1059 ds_put_format(string, "DP Description: %.*s\n",
1060 (int) sizeof ods->dp_desc, ods->dp_desc);
1064 ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
1066 struct flow_stats_request fsr;
1069 error = ofputil_decode_flow_stats_request(&fsr, oh);
1071 ofp_print_error(string, error);
1075 if (fsr.table_id != 0xff) {
1076 ds_put_format(string, " table_id=%"PRIu8, fsr.table_id);
1079 if (fsr.out_port != OFPP_NONE) {
1080 ds_put_cstr(string, " out_port=");
1081 ofp_print_port_name(string, fsr.out_port);
1084 /* A flow stats request doesn't include a priority, but cls_rule_format()
1085 * will print one unless it is OFP_DEFAULT_PRIORITY. */
1086 fsr.match.priority = OFP_DEFAULT_PRIORITY;
1088 ds_put_char(string, ' ');
1089 cls_rule_format(&fsr.match, string);
1093 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1097 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1099 struct ofputil_flow_stats fs;
1102 retval = ofputil_decode_flow_stats_reply(&fs, &b);
1104 if (retval != EOF) {
1105 ds_put_cstr(string, " ***parse error***");
1110 ds_put_char(string, '\n');
1112 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1114 ofp_print_duration(string, fs.duration_sec, fs.duration_nsec);
1115 ds_put_format(string, ", table_id=%"PRIu8", ", fs.table_id);
1116 ds_put_format(string, "n_packets=%"PRIu64", ", fs.packet_count);
1117 ds_put_format(string, "n_bytes=%"PRIu64", ", fs.byte_count);
1118 if (fs.idle_timeout != OFP_FLOW_PERMANENT) {
1119 ds_put_format(string, "idle_timeout=%"PRIu16",", fs.idle_timeout);
1121 if (fs.hard_timeout != OFP_FLOW_PERMANENT) {
1122 ds_put_format(string, "hard_timeout=%"PRIu16",", fs.hard_timeout);
1125 cls_rule_format(&fs.rule, string);
1126 ds_put_char(string, ' ');
1127 ofp_print_actions(string,
1128 (const struct ofp_action_header *) fs.actions,
1129 fs.n_actions * sizeof *fs.actions);
1134 ofp_print_ofp_aggregate_stats_reply (
1135 struct ds *string, const struct ofp_aggregate_stats_reply *asr)
1137 ds_put_format(string, " packet_count=%"PRIu64,
1138 ntohll(get_32aligned_be64(&asr->packet_count)));
1139 ds_put_format(string, " byte_count=%"PRIu64,
1140 ntohll(get_32aligned_be64(&asr->byte_count)));
1141 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1145 ofp_print_ofpst_aggregate_reply(struct ds *string, const struct ofp_header *oh)
1147 ofp_print_ofp_aggregate_stats_reply(string, ofputil_stats_body(oh));
1151 ofp_print_nxst_aggregate_reply(struct ds *string,
1152 const struct nx_aggregate_stats_reply *nasr)
1154 ofp_print_ofp_aggregate_stats_reply(string, &nasr->asr);
1157 static void print_port_stat(struct ds *string, const char *leader,
1158 const ovs_32aligned_be64 *statp, int more)
1160 uint64_t stat = ntohll(get_32aligned_be64(statp));
1162 ds_put_cstr(string, leader);
1163 if (stat != UINT64_MAX) {
1164 ds_put_format(string, "%"PRIu64, stat);
1166 ds_put_char(string, '?');
1169 ds_put_cstr(string, ", ");
1171 ds_put_cstr(string, "\n");
1176 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1178 const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
1179 ds_put_format(string, " port_no=%"PRIu16, ntohs(psr->port_no));
1183 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1186 const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1187 size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
1188 ds_put_format(string, " %zu ports\n", n);
1189 if (verbosity < 1) {
1194 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1196 ds_put_cstr(string, "rx ");
1197 print_port_stat(string, "pkts=", &ps->rx_packets, 1);
1198 print_port_stat(string, "bytes=", &ps->rx_bytes, 1);
1199 print_port_stat(string, "drop=", &ps->rx_dropped, 1);
1200 print_port_stat(string, "errs=", &ps->rx_errors, 1);
1201 print_port_stat(string, "frame=", &ps->rx_frame_err, 1);
1202 print_port_stat(string, "over=", &ps->rx_over_err, 1);
1203 print_port_stat(string, "crc=", &ps->rx_crc_err, 0);
1205 ds_put_cstr(string, " tx ");
1206 print_port_stat(string, "pkts=", &ps->tx_packets, 1);
1207 print_port_stat(string, "bytes=", &ps->tx_bytes, 1);
1208 print_port_stat(string, "drop=", &ps->tx_dropped, 1);
1209 print_port_stat(string, "errs=", &ps->tx_errors, 1);
1210 print_port_stat(string, "coll=", &ps->collisions, 0);
1215 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1218 const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1219 size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
1220 ds_put_format(string, " %zu tables\n", n);
1221 if (verbosity < 1) {
1226 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1227 ovs_strlcpy(name, ts->name, sizeof name);
1229 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1230 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1231 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1232 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1233 ds_put_cstr(string, " ");
1234 ds_put_format(string, "lookup=%"PRIu64", ",
1235 ntohll(get_32aligned_be64(&ts->lookup_count)));
1236 ds_put_format(string, "matched=%"PRIu64"\n",
1237 ntohll(get_32aligned_be64(&ts->matched_count)));
1242 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1244 if (queue_id == OFPQ_ALL) {
1245 ds_put_cstr(string, "ALL");
1247 ds_put_format(string, "%"PRIu32, queue_id);
1252 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1254 const struct ofp_queue_stats_request *qsr = ofputil_stats_body(oh);
1256 ds_put_cstr(string, "port=");
1257 ofp_print_port_name(string, ntohs(qsr->port_no));
1259 ds_put_cstr(string, " queue=");
1260 ofp_print_queue_name(string, ntohl(qsr->queue_id));
1264 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1267 const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1268 size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
1269 ds_put_format(string, " %zu queues\n", n);
1270 if (verbosity < 1) {
1275 ds_put_cstr(string, " port ");
1276 ofp_print_port_name(string, ntohs(qs->port_no));
1277 ds_put_cstr(string, " queue ");
1278 ofp_print_queue_name(string, ntohl(qs->queue_id));
1279 ds_put_cstr(string, ": ");
1281 print_port_stat(string, "bytes=", &qs->tx_bytes, 1);
1282 print_port_stat(string, "pkts=", &qs->tx_packets, 1);
1283 print_port_stat(string, "errors=", &qs->tx_errors, 0);
1288 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1290 const struct ofp_stats_request *srq
1291 = (const struct ofp_stats_request *) oh;
1294 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1300 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1302 const struct ofp_stats_reply *srp = (const struct ofp_stats_reply *) oh;
1305 uint16_t flags = ntohs(srp->flags);
1307 ds_put_cstr(string, " flags=");
1308 if (flags & OFPSF_REPLY_MORE) {
1309 ds_put_cstr(string, "[more]");
1310 flags &= ~OFPSF_REPLY_MORE;
1313 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1320 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1322 size_t len = ntohs(oh->length);
1324 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1325 if (verbosity > 1) {
1326 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1331 ofp_print_nxt_role_message(struct ds *string,
1332 const struct nx_role_request *nrr)
1334 unsigned int role = ntohl(nrr->role);
1336 ds_put_cstr(string, " role=");
1337 if (role == NX_ROLE_OTHER) {
1338 ds_put_cstr(string, "other");
1339 } else if (role == NX_ROLE_MASTER) {
1340 ds_put_cstr(string, "master");
1341 } else if (role == NX_ROLE_SLAVE) {
1342 ds_put_cstr(string, "slave");
1344 ds_put_format(string, "%u", role);
1349 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1350 const struct nxt_flow_mod_table_id *nfmti)
1352 ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1356 ofp_print_nxt_set_flow_format(struct ds *string,
1357 const struct nxt_set_flow_format *nsff)
1359 uint32_t format = ntohl(nsff->format);
1361 ds_put_cstr(string, " format=");
1362 if (ofputil_flow_format_is_valid(format)) {
1363 ds_put_cstr(string, ofputil_flow_format_to_string(format));
1365 ds_put_format(string, "%"PRIu32, format);
1370 ofp_to_string__(const struct ofp_header *oh,
1371 const struct ofputil_msg_type *type, struct ds *string,
1374 enum ofputil_msg_code code;
1375 const void *msg = oh;
1377 ds_put_format(string, "%s (xid=0x%"PRIx32"):",
1378 ofputil_msg_type_name(type), ntohl(oh->xid));
1380 code = ofputil_msg_type_code(type);
1382 case OFPUTIL_INVALID:
1385 case OFPUTIL_OFPT_HELLO:
1386 ds_put_char(string, '\n');
1387 ds_put_hex_dump(string, oh + 1, ntohs(oh->length) - sizeof *oh,
1391 case OFPUTIL_OFPT_ERROR:
1392 ofp_print_error_msg(string, msg);
1395 case OFPUTIL_OFPT_ECHO_REQUEST:
1396 case OFPUTIL_OFPT_ECHO_REPLY:
1397 ofp_print_echo(string, oh, verbosity);
1400 case OFPUTIL_OFPT_FEATURES_REQUEST:
1403 case OFPUTIL_OFPT_FEATURES_REPLY:
1404 ofp_print_switch_features(string, msg);
1407 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1410 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1411 case OFPUTIL_OFPT_SET_CONFIG:
1412 ofp_print_switch_config(string, msg);
1415 case OFPUTIL_OFPT_PACKET_IN:
1416 ofp_print_packet_in(string, msg, verbosity);
1419 case OFPUTIL_OFPT_FLOW_REMOVED:
1420 case OFPUTIL_NXT_FLOW_REMOVED:
1421 ofp_print_flow_removed(string, msg);
1424 case OFPUTIL_OFPT_PORT_STATUS:
1425 ofp_print_port_status(string, msg);
1428 case OFPUTIL_OFPT_PACKET_OUT:
1429 ofp_print_packet_out(string, msg, verbosity);
1432 case OFPUTIL_OFPT_FLOW_MOD:
1433 ofp_print_flow_mod(string, msg, code, verbosity);
1436 case OFPUTIL_OFPT_PORT_MOD:
1437 ofp_print_port_mod(string, msg);
1440 case OFPUTIL_OFPT_BARRIER_REQUEST:
1441 case OFPUTIL_OFPT_BARRIER_REPLY:
1444 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1445 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1449 case OFPUTIL_OFPST_DESC_REQUEST:
1450 ofp_print_stats_request(string, oh);
1453 case OFPUTIL_OFPST_FLOW_REQUEST:
1454 case OFPUTIL_NXST_FLOW_REQUEST:
1455 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1456 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1457 ofp_print_stats_request(string, oh);
1458 ofp_print_flow_stats_request(string, oh);
1461 case OFPUTIL_OFPST_TABLE_REQUEST:
1462 ofp_print_stats_request(string, oh);
1465 case OFPUTIL_OFPST_PORT_REQUEST:
1466 ofp_print_stats_request(string, oh);
1467 ofp_print_ofpst_port_request(string, oh);
1470 case OFPUTIL_OFPST_QUEUE_REQUEST:
1471 ofp_print_stats_request(string, oh);
1472 ofp_print_ofpst_queue_request(string, oh);
1475 case OFPUTIL_OFPST_DESC_REPLY:
1476 ofp_print_stats_reply(string, oh);
1477 ofp_print_ofpst_desc_reply(string, oh);
1480 case OFPUTIL_OFPST_FLOW_REPLY:
1481 case OFPUTIL_NXST_FLOW_REPLY:
1482 ofp_print_stats_reply(string, oh);
1483 ofp_print_flow_stats_reply(string, oh);
1486 case OFPUTIL_OFPST_QUEUE_REPLY:
1487 ofp_print_stats_reply(string, oh);
1488 ofp_print_ofpst_queue_reply(string, oh, verbosity);
1491 case OFPUTIL_OFPST_PORT_REPLY:
1492 ofp_print_stats_reply(string, oh);
1493 ofp_print_ofpst_port_reply(string, oh, verbosity);
1496 case OFPUTIL_OFPST_TABLE_REPLY:
1497 ofp_print_stats_reply(string, oh);
1498 ofp_print_ofpst_table_reply(string, oh, verbosity);
1501 case OFPUTIL_OFPST_AGGREGATE_REPLY:
1502 ofp_print_stats_reply(string, oh);
1503 ofp_print_ofpst_aggregate_reply(string, oh);
1506 case OFPUTIL_NXT_ROLE_REQUEST:
1507 case OFPUTIL_NXT_ROLE_REPLY:
1508 ofp_print_nxt_role_message(string, msg);
1511 case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
1512 ofp_print_nxt_flow_mod_table_id(string, msg);
1515 case OFPUTIL_NXT_SET_FLOW_FORMAT:
1516 ofp_print_nxt_set_flow_format(string, msg);
1519 case OFPUTIL_NXT_FLOW_MOD:
1520 ofp_print_flow_mod(string, msg, code, verbosity);
1523 case OFPUTIL_NXST_AGGREGATE_REPLY:
1524 ofp_print_stats_reply(string, oh);
1525 ofp_print_nxst_aggregate_reply(string, msg);
1530 /* Composes and returns a string representing the OpenFlow packet of 'len'
1531 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1532 * verbosity and higher numbers increase verbosity. The caller is responsible
1533 * for freeing the string. */
1535 ofp_to_string(const void *oh_, size_t len, int verbosity)
1537 struct ds string = DS_EMPTY_INITIALIZER;
1538 const struct ofp_header *oh = oh_;
1541 ds_put_cstr(&string, "OpenFlow message is empty\n");
1542 } else if (len < sizeof(struct ofp_header)) {
1543 ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
1545 } else if (oh->version != OFP_VERSION) {
1546 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n",
1548 } else if (ntohs(oh->length) > len) {
1549 ds_put_format(&string,
1550 "(***truncated to %zu bytes from %"PRIu16"***)\n",
1551 len, ntohs(oh->length));
1552 } else if (ntohs(oh->length) < len) {
1553 ds_put_format(&string,
1554 "(***only uses %"PRIu16" bytes out of %zu***)\n",
1555 ntohs(oh->length), len);
1557 const struct ofputil_msg_type *type;
1560 error = ofputil_decode_msg_type(oh, &type);
1562 ofp_to_string__(oh, type, &string, verbosity);
1563 if (verbosity >= 5) {
1564 if (ds_last(&string) != '\n') {
1565 ds_put_char(&string, '\n');
1567 ds_put_hex_dump(&string, oh, len, 0, true);
1569 if (ds_last(&string) != '\n') {
1570 ds_put_char(&string, '\n');
1572 return ds_steal_cstr(&string);
1575 ofp_print_error(&string, error);
1577 ds_put_hex_dump(&string, oh, len, 0, true);
1578 return ds_steal_cstr(&string);
1581 /* Returns the name for the specified OpenFlow message type as a string,
1582 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1583 * hex number, e.g. "0x55".
1585 * The caller must free the returned string when it is no longer needed. */
1587 ofp_message_type_to_string(uint8_t type)
1598 case OFPT_ECHO_REQUEST:
1599 name = "ECHO_REQUEST";
1601 case OFPT_ECHO_REPLY:
1602 name = "ECHO_REPLY";
1607 case OFPT_FEATURES_REQUEST:
1608 name = "FEATURES_REQUEST";
1610 case OFPT_FEATURES_REPLY:
1611 name = "FEATURES_REPLY";
1613 case OFPT_GET_CONFIG_REQUEST:
1614 name = "GET_CONFIG_REQUEST";
1616 case OFPT_GET_CONFIG_REPLY:
1617 name = "GET_CONFIG_REPLY";
1619 case OFPT_SET_CONFIG:
1620 name = "SET_CONFIG";
1622 case OFPT_PACKET_IN:
1625 case OFPT_FLOW_REMOVED:
1626 name = "FLOW_REMOVED";
1628 case OFPT_PORT_STATUS:
1629 name = "PORT_STATUS";
1631 case OFPT_PACKET_OUT:
1632 name = "PACKET_OUT";
1640 case OFPT_STATS_REQUEST:
1641 name = "STATS_REQUEST";
1643 case OFPT_STATS_REPLY:
1644 name = "STATS_REPLY";
1646 case OFPT_BARRIER_REQUEST:
1647 name = "BARRIER_REQUEST";
1649 case OFPT_BARRIER_REPLY:
1650 name = "BARRIER_REPLY";
1652 case OFPT_QUEUE_GET_CONFIG_REQUEST:
1653 name = "QUEUE_GET_CONFIG_REQUEST";
1655 case OFPT_QUEUE_GET_CONFIG_REPLY:
1656 name = "QUEUE_GET_CONFIG_REPLY";
1663 return name ? xasprintf("OFPT_%s", name) : xasprintf("0x%02"PRIx8, type);
1667 print_and_free(FILE *stream, char *string)
1669 fputs(string, stream);
1673 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1674 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1675 * numbers increase verbosity. */
1677 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1679 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1682 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1683 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1684 * the Ethernet frame (of which 'len' bytes were captured).
1686 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1688 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1690 print_and_free(stream, ofp_packet_to_string(data, len, total_len));