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 (htonl(op->buffer_id) == 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:
214 return sizeof(struct nx_action_drop_spoofed_arp);
215 case NXAST_SET_QUEUE: return sizeof(struct nx_action_set_queue);
216 case NXAST_POP_QUEUE: return sizeof(struct nx_action_pop_queue);
217 case NXAST_REG_MOVE: return sizeof(struct nx_action_reg_move);
218 case NXAST_REG_LOAD: return sizeof(struct nx_action_reg_load);
219 case NXAST_NOTE: return -1;
220 case NXAST_SET_TUNNEL64: return sizeof(struct nx_action_set_tunnel64);
221 case NXAST_MULTIPATH: return sizeof(struct nx_action_multipath);
222 case NXAST_AUTOPATH: return sizeof (struct nx_action_autopath);
228 ofp_print_nx_action(struct ds *string, const struct nx_action_header *nah)
230 int subtype = ntohs(nah->subtype);
231 int required_len = nx_action_len(subtype);
232 int len = ntohs(nah->len);
234 if (required_len != -1 && required_len != len) {
235 ds_put_format(string, "***Nicira action %"PRIu16" wrong length: %d***",
240 if (subtype <= TYPE_MAXIMUM(enum nx_action_subtype)) {
241 const struct nx_action_set_tunnel64 *nast64;
242 const struct nx_action_set_tunnel *nast;
243 const struct nx_action_set_queue *nasq;
244 const struct nx_action_resubmit *nar;
245 const struct nx_action_reg_move *move;
246 const struct nx_action_reg_load *load;
247 const struct nx_action_multipath *nam;
248 const struct nx_action_autopath *naa;
250 switch ((enum nx_action_subtype) subtype) {
252 nar = (struct nx_action_resubmit *)nah;
253 ds_put_format(string, "resubmit:");
254 ofp_print_port_name(string, ntohs(nar->in_port));
257 case NXAST_SET_TUNNEL:
258 nast = (struct nx_action_set_tunnel *)nah;
259 ds_put_format(string, "set_tunnel:%#"PRIx32, ntohl(nast->tun_id));
262 case NXAST_DROP_SPOOFED_ARP:
263 ds_put_cstr(string, "drop_spoofed_arp");
266 case NXAST_SET_QUEUE:
267 nasq = (struct nx_action_set_queue *)nah;
268 ds_put_format(string, "set_queue:%u", ntohl(nasq->queue_id));
271 case NXAST_POP_QUEUE:
272 ds_put_cstr(string, "pop_queue");
276 print_note(string, (const struct nx_action_note *) nah);
280 move = (const struct nx_action_reg_move *) nah;
281 nxm_format_reg_move(move, string);
285 load = (const struct nx_action_reg_load *) nah;
286 nxm_format_reg_load(load, string);
289 case NXAST_SET_TUNNEL64:
290 nast64 = (const struct nx_action_set_tunnel64 *) nah;
291 ds_put_format(string, "set_tunnel64:%#"PRIx64,
292 ntohll(nast64->tun_id));
295 case NXAST_MULTIPATH:
296 nam = (const struct nx_action_multipath *) nah;
297 multipath_format(nam, string);
301 naa = (const struct nx_action_autopath *)nah;
302 ds_put_format(string, "autopath(%u,", ntohl(naa->id));
303 nxm_format_field_bits(string, ntohl(naa->dst),
304 nxm_decode_ofs(naa->ofs_nbits),
305 nxm_decode_n_bits(naa->ofs_nbits));
306 ds_put_char(string, ')');
309 case NXAST_SNAT__OBSOLETE:
315 ds_put_format(string, "***unknown Nicira action:%d***", subtype);
319 ofp_action_len(enum ofp_action_type type)
322 case OFPAT_OUTPUT: return sizeof(struct ofp_action_output);
323 case OFPAT_SET_VLAN_VID: return sizeof(struct ofp_action_vlan_vid);
324 case OFPAT_SET_VLAN_PCP: return sizeof(struct ofp_action_vlan_pcp);
325 case OFPAT_STRIP_VLAN: return sizeof(struct ofp_action_header);
326 case OFPAT_SET_DL_SRC: return sizeof(struct ofp_action_dl_addr);
327 case OFPAT_SET_DL_DST: return sizeof(struct ofp_action_dl_addr);
328 case OFPAT_SET_NW_SRC: return sizeof(struct ofp_action_nw_addr);
329 case OFPAT_SET_NW_DST: return sizeof(struct ofp_action_nw_addr);
330 case OFPAT_SET_NW_TOS: return sizeof(struct ofp_action_nw_tos);
331 case OFPAT_SET_TP_SRC: return sizeof(struct ofp_action_tp_port);
332 case OFPAT_SET_TP_DST: return sizeof(struct ofp_action_tp_port);
333 case OFPAT_ENQUEUE: return sizeof(struct ofp_action_enqueue);
334 case OFPAT_VENDOR: return -1;
340 ofp_print_action(struct ds *string, const struct ofp_action_header *ah,
343 enum ofp_action_type type;
347 if (actions_len < sizeof *ah) {
348 ds_put_format(string, "***action array too short for next action***\n");
352 type = ntohs(ah->type);
353 len = ntohs(ah->len);
354 if (actions_len < len) {
355 ds_put_format(string, "***truncated action %d***\n", (int) type);
360 ds_put_format(string, "***zero-length action***\n");
364 if ((len % OFP_ACTION_ALIGN) != 0) {
365 ds_put_format(string,
366 "***action %d length not a multiple of %d***\n",
367 (int) type, OFP_ACTION_ALIGN);
371 required_len = ofp_action_len(type);
372 if (required_len >= 0 && len != required_len) {
373 ds_put_format(string,
374 "***action %d wrong length: %zu***\n", (int) type, len);
380 struct ofp_action_output *oa = (struct ofp_action_output *)ah;
381 uint16_t port = ntohs(oa->port);
382 if (port < OFPP_MAX) {
383 ds_put_format(string, "output:%"PRIu16, port);
385 ofp_print_port_name(string, port);
386 if (port == OFPP_CONTROLLER) {
388 ds_put_format(string, ":%"PRIu16, ntohs(oa->max_len));
390 ds_put_cstr(string, ":all");
397 case OFPAT_ENQUEUE: {
398 struct ofp_action_enqueue *ea = (struct ofp_action_enqueue *)ah;
399 unsigned int port = ntohs(ea->port);
400 unsigned int queue_id = ntohl(ea->queue_id);
401 ds_put_format(string, "enqueue:");
402 if (port != OFPP_IN_PORT) {
403 ds_put_format(string, "%u", port);
405 ds_put_cstr(string, "IN_PORT");
407 ds_put_format(string, "q%u", queue_id);
411 case OFPAT_SET_VLAN_VID: {
412 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
413 ds_put_format(string, "mod_vlan_vid:%"PRIu16, ntohs(va->vlan_vid));
417 case OFPAT_SET_VLAN_PCP: {
418 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
419 ds_put_format(string, "mod_vlan_pcp:%"PRIu8, va->vlan_pcp);
423 case OFPAT_STRIP_VLAN:
424 ds_put_cstr(string, "strip_vlan");
427 case OFPAT_SET_DL_SRC: {
428 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
429 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
430 ETH_ADDR_ARGS(da->dl_addr));
434 case OFPAT_SET_DL_DST: {
435 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
436 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
437 ETH_ADDR_ARGS(da->dl_addr));
441 case OFPAT_SET_NW_SRC: {
442 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
443 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&na->nw_addr));
447 case OFPAT_SET_NW_DST: {
448 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
449 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&na->nw_addr));
453 case OFPAT_SET_NW_TOS: {
454 struct ofp_action_nw_tos *nt = (struct ofp_action_nw_tos *)ah;
455 ds_put_format(string, "mod_nw_tos:%d", nt->nw_tos);
459 case OFPAT_SET_TP_SRC: {
460 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
461 ds_put_format(string, "mod_tp_src:%d", ntohs(ta->tp_port));
465 case OFPAT_SET_TP_DST: {
466 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
467 ds_put_format(string, "mod_tp_dst:%d", ntohs(ta->tp_port));
472 struct ofp_action_vendor_header *avh
473 = (struct ofp_action_vendor_header *)ah;
474 if (len < sizeof *avh) {
475 ds_put_format(string, "***ofpat_vendor truncated***\n");
478 if (avh->vendor == htonl(NX_VENDOR_ID)) {
479 ofp_print_nx_action(string, (struct nx_action_header *)avh);
481 ds_put_format(string, "vendor action:0x%x", ntohl(avh->vendor));
487 ds_put_format(string, "(decoder %d not implemented)", (int) type);
495 ofp_print_actions(struct ds *string, const struct ofp_action_header *action,
498 uint8_t *p = (uint8_t *)action;
501 ds_put_cstr(string, "actions=");
503 ds_put_cstr(string, "drop");
505 while (actions_len > 0) {
507 ds_put_cstr(string, ",");
509 len = ofp_print_action(string, (struct ofp_action_header *)p,
520 ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
523 size_t len = ntohs(opo->header.length);
524 size_t actions_len = ntohs(opo->actions_len);
526 ds_put_cstr(string, " in_port=");
527 ofp_print_port_name(string, ntohs(opo->in_port));
529 ds_put_format(string, " actions_len=%zu ", actions_len);
530 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
531 ds_put_format(string, "***packet too short for action length***\n");
534 ofp_print_actions(string, opo->actions, actions_len);
536 if (ntohl(opo->buffer_id) == UINT32_MAX) {
537 int data_len = len - sizeof *opo - actions_len;
538 ds_put_format(string, " data_len=%d", data_len);
539 if (verbosity > 0 && len > sizeof *opo) {
540 char *packet = ofp_packet_to_string(
541 (uint8_t *)opo->actions + actions_len, data_len, data_len);
542 ds_put_char(string, '\n');
543 ds_put_cstr(string, packet);
547 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
549 ds_put_char(string, '\n');
552 /* qsort comparison function. */
554 compare_ports(const void *a_, const void *b_)
556 const struct ofp_phy_port *a = a_;
557 const struct ofp_phy_port *b = b_;
558 uint16_t ap = ntohs(a->port_no);
559 uint16_t bp = ntohs(b->port_no);
561 return ap < bp ? -1 : ap > bp;
564 static void ofp_print_port_features(struct ds *string, uint32_t features)
567 ds_put_cstr(string, "Unsupported\n");
570 if (features & OFPPF_10MB_HD) {
571 ds_put_cstr(string, "10MB-HD ");
573 if (features & OFPPF_10MB_FD) {
574 ds_put_cstr(string, "10MB-FD ");
576 if (features & OFPPF_100MB_HD) {
577 ds_put_cstr(string, "100MB-HD ");
579 if (features & OFPPF_100MB_FD) {
580 ds_put_cstr(string, "100MB-FD ");
582 if (features & OFPPF_1GB_HD) {
583 ds_put_cstr(string, "1GB-HD ");
585 if (features & OFPPF_1GB_FD) {
586 ds_put_cstr(string, "1GB-FD ");
588 if (features & OFPPF_10GB_FD) {
589 ds_put_cstr(string, "10GB-FD ");
591 if (features & OFPPF_COPPER) {
592 ds_put_cstr(string, "COPPER ");
594 if (features & OFPPF_FIBER) {
595 ds_put_cstr(string, "FIBER ");
597 if (features & OFPPF_AUTONEG) {
598 ds_put_cstr(string, "AUTO_NEG ");
600 if (features & OFPPF_PAUSE) {
601 ds_put_cstr(string, "AUTO_PAUSE ");
603 if (features & OFPPF_PAUSE_ASYM) {
604 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
606 ds_put_char(string, '\n');
610 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
612 char name[OFP_MAX_PORT_NAME_LEN];
615 memcpy(name, port->name, sizeof name);
616 for (j = 0; j < sizeof name - 1; j++) {
617 if (!isprint((unsigned char) name[j])) {
623 ds_put_char(string, ' ');
624 ofp_print_port_name(string, ntohs(port->port_no));
625 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
626 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
629 ds_put_format(string, " current: ");
630 ofp_print_port_features(string, ntohl(port->curr));
632 if (port->advertised) {
633 ds_put_format(string, " advertised: ");
634 ofp_print_port_features(string, ntohl(port->advertised));
636 if (port->supported) {
637 ds_put_format(string, " supported: ");
638 ofp_print_port_features(string, ntohl(port->supported));
641 ds_put_format(string, " peer: ");
642 ofp_print_port_features(string, ntohl(port->peer));
647 ofp_print_switch_features(struct ds *string,
648 const struct ofp_switch_features *osf)
650 size_t len = ntohs(osf->header.length);
651 struct ofp_phy_port *port_list;
655 ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
656 osf->header.version, ntohll(osf->datapath_id));
657 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
658 ntohl(osf->n_buffers));
659 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
660 ntohl(osf->capabilities), ntohl(osf->actions));
662 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
664 port_list = xmemdup(osf->ports, len - sizeof *osf);
665 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
666 for (i = 0; i < n_ports; i++) {
667 ofp_print_phy_port(string, &port_list[i]);
673 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
677 flags = ntohs(osc->flags);
679 ds_put_cstr(string, " frags=");
680 switch (flags & OFPC_FRAG_MASK) {
681 case OFPC_FRAG_NORMAL:
682 ds_put_cstr(string, "normal");
683 flags &= ~OFPC_FRAG_MASK;
686 ds_put_cstr(string, "drop");
687 flags &= ~OFPC_FRAG_MASK;
689 case OFPC_FRAG_REASM:
690 ds_put_cstr(string, "reassemble");
691 flags &= ~OFPC_FRAG_MASK;
695 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
698 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
701 static void print_wild(struct ds *string, const char *leader, int is_wild,
702 int verbosity, const char *format, ...)
703 __attribute__((format(printf, 5, 6)));
705 static void print_wild(struct ds *string, const char *leader, int is_wild,
706 int verbosity, const char *format, ...)
708 if (is_wild && verbosity < 2) {
711 ds_put_cstr(string, leader);
715 va_start(args, format);
716 ds_put_format_valist(string, format, args);
719 ds_put_char(string, '*');
721 ds_put_char(string, ',');
725 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
726 uint32_t wild_bits, int verbosity)
728 if (wild_bits >= 32 && verbosity < 2) {
731 ds_put_cstr(string, leader);
732 if (wild_bits < 32) {
733 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
735 ds_put_format(string, "/%d", 32 - wild_bits);
738 ds_put_char(string, '*');
740 ds_put_char(string, ',');
744 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
746 char *s = ofp_match_to_string(om, verbosity);
752 ofp_match_to_string(const struct ofp_match *om, int verbosity)
754 struct ds f = DS_EMPTY_INITIALIZER;
755 uint32_t w = ntohl(om->wildcards);
756 bool skip_type = false;
757 bool skip_proto = false;
759 if (!(w & OFPFW_DL_TYPE)) {
761 if (om->dl_type == htons(ETH_TYPE_IP)) {
762 if (!(w & OFPFW_NW_PROTO)) {
764 if (om->nw_proto == IPPROTO_ICMP) {
765 ds_put_cstr(&f, "icmp,");
766 } else if (om->nw_proto == IPPROTO_TCP) {
767 ds_put_cstr(&f, "tcp,");
768 } else if (om->nw_proto == IPPROTO_UDP) {
769 ds_put_cstr(&f, "udp,");
771 ds_put_cstr(&f, "ip,");
775 ds_put_cstr(&f, "ip,");
777 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
778 ds_put_cstr(&f, "arp,");
783 if (w & NXFW_TUN_ID) {
784 ds_put_cstr(&f, "tun_id_wild,");
786 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
787 "%d", ntohs(om->in_port));
788 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
789 "%d", ntohs(om->dl_vlan));
790 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
791 "%d", om->dl_vlan_pcp);
792 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
793 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
794 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
795 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
797 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
798 "0x%04x", ntohs(om->dl_type));
800 print_ip_netmask(&f, "nw_src=", om->nw_src,
801 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
802 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
803 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
805 if (om->dl_type == htons(ETH_TYPE_ARP)) {
806 print_wild(&f, "opcode=", w & OFPFW_NW_PROTO, verbosity,
809 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
813 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
815 if (om->nw_proto == IPPROTO_ICMP) {
816 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
817 "%d", ntohs(om->icmp_type));
818 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
819 "%d", ntohs(om->icmp_code));
821 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
822 "%d", ntohs(om->tp_src));
823 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
824 "%d", ntohs(om->tp_dst));
826 if (ds_last(&f) == ',') {
833 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
834 enum ofputil_msg_code code, int verbosity)
840 error = ofputil_decode_flow_mod(&fm, oh, NXFF_OPENFLOW10, true);
842 ofp_print_error(s, error);
847 switch (fm.command) {
849 ds_put_cstr(s, "ADD");
852 ds_put_cstr(s, "MOD");
854 case OFPFC_MODIFY_STRICT:
855 ds_put_cstr(s, "MOD_STRICT");
858 ds_put_cstr(s, "DEL");
860 case OFPFC_DELETE_STRICT:
861 ds_put_cstr(s, "DEL_STRICT");
864 ds_put_format(s, "cmd:%d", fm.command);
866 if (fm.table_id != 0) {
867 ds_put_format(s, " table_id:%d", fm.table_id);
871 if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
872 const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
873 ofp_print_match(s, &ofm->match, verbosity);
875 /* ofp_print_match() doesn't print priority. */
876 need_priority = true;
877 } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
878 const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
879 const void *nxm = nfm + 1;
882 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
883 ds_put_cstr(s, nxm_s);
886 /* nx_match_to_string() doesn't print priority. */
887 need_priority = true;
889 cls_rule_format(&fm.cr, s);
891 /* cls_rule_format() does print priority. */
892 need_priority = false;
895 if (ds_last(s) != ' ') {
898 if (fm.cookie != htonll(0)) {
899 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.cookie));
901 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
902 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
904 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
905 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
907 if (fm.cr.priority != OFP_DEFAULT_PRIORITY && need_priority) {
908 ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
910 if (fm.buffer_id != UINT32_MAX) {
911 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
914 ds_put_format(s, "flags:0x%"PRIx16" ", fm.flags);
917 ofp_print_actions(s, (const struct ofp_action_header *) fm.actions,
918 fm.n_actions * sizeof *fm.actions);
922 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
924 ds_put_format(string, "%u", sec);
926 ds_put_format(string, ".%09u", nsec);
927 while (string->string[string->length - 1] == '0') {
931 ds_put_char(string, 's');
935 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
937 struct ofputil_flow_removed fr;
940 error = ofputil_decode_flow_removed(&fr, oh, NXFF_OPENFLOW10);
942 ofp_print_error(string, error);
946 ds_put_char(string, ' ');
947 cls_rule_format(&fr.rule, string);
949 ds_put_cstr(string, " reason=");
951 case OFPRR_IDLE_TIMEOUT:
952 ds_put_cstr(string, "idle");
954 case OFPRR_HARD_TIMEOUT:
955 ds_put_cstr(string, "hard");
958 ds_put_cstr(string, "delete");
961 ds_put_format(string, "**%"PRIu8"**", fr.reason);
965 if (fr.cookie != htonll(0)) {
966 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
968 ds_put_cstr(string, " duration");
969 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
970 ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
971 fr.idle_timeout, fr.packet_count, fr.byte_count);
975 ofp_print_port_mod(struct ds *string, const struct ofp_port_mod *opm)
977 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
978 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
979 ntohl(opm->config), ntohl(opm->mask));
980 ds_put_format(string, " advertise: ");
981 if (opm->advertise) {
982 ofp_print_port_features(string, ntohl(opm->advertise));
984 ds_put_format(string, "UNCHANGED\n");
989 ofp_print_error(struct ds *string, int error)
991 if (string->length) {
992 ds_put_char(string, ' ');
994 ds_put_cstr(string, "***decode error: ");
995 ofputil_format_error(string, error);
996 ds_put_cstr(string, "***\n");
1000 ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
1002 size_t len = ntohs(oem->header.length);
1003 size_t payload_ofs, payload_len;
1004 const void *payload;
1008 error = ofputil_decode_error_msg(&oem->header, &payload_ofs);
1009 if (!is_ofp_error(error)) {
1010 ofp_print_error(string, error);
1011 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
1015 ds_put_char(string, ' ');
1016 ofputil_format_error(string, error);
1017 ds_put_char(string, '\n');
1019 payload = (const uint8_t *) oem + payload_ofs;
1020 payload_len = len - payload_ofs;
1021 switch (get_ofp_err_type(error)) {
1022 case OFPET_HELLO_FAILED:
1023 ds_put_printable(string, payload, payload_len);
1026 case OFPET_BAD_REQUEST:
1027 s = ofp_to_string(payload, payload_len, 1);
1028 ds_put_cstr(string, s);
1033 ds_put_hex_dump(string, payload, payload_len, 0, true);
1039 ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
1041 if (ops->reason == OFPPR_ADD) {
1042 ds_put_format(string, " ADD:");
1043 } else if (ops->reason == OFPPR_DELETE) {
1044 ds_put_format(string, " DEL:");
1045 } else if (ops->reason == OFPPR_MODIFY) {
1046 ds_put_format(string, " MOD:");
1049 ofp_print_phy_port(string, &ops->desc);
1053 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
1055 const struct ofp_desc_stats *ods = ofputil_stats_body(oh);
1057 ds_put_char(string, '\n');
1058 ds_put_format(string, "Manufacturer: %.*s\n",
1059 (int) sizeof ods->mfr_desc, ods->mfr_desc);
1060 ds_put_format(string, "Hardware: %.*s\n",
1061 (int) sizeof ods->hw_desc, ods->hw_desc);
1062 ds_put_format(string, "Software: %.*s\n",
1063 (int) sizeof ods->sw_desc, ods->sw_desc);
1064 ds_put_format(string, "Serial Num: %.*s\n",
1065 (int) sizeof ods->serial_num, ods->serial_num);
1066 ds_put_format(string, "DP Description: %.*s\n",
1067 (int) sizeof ods->dp_desc, ods->dp_desc);
1071 ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
1073 struct flow_stats_request fsr;
1076 error = ofputil_decode_flow_stats_request(&fsr, oh, NXFF_OPENFLOW10);
1078 ofp_print_error(string, error);
1082 if (fsr.table_id != 0xff) {
1083 ds_put_format(string, " table_id=%"PRIu8, fsr.table_id);
1086 if (fsr.out_port != OFPP_NONE) {
1087 ds_put_cstr(string, " out_port=");
1088 ofp_print_port_name(string, fsr.out_port);
1091 /* A flow stats request doesn't include a priority, but cls_rule_format()
1092 * will print one unless it is OFP_DEFAULT_PRIORITY. */
1093 fsr.match.priority = OFP_DEFAULT_PRIORITY;
1095 ds_put_char(string, ' ');
1096 cls_rule_format(&fsr.match, string);
1100 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1104 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1106 struct ofputil_flow_stats fs;
1109 retval = ofputil_decode_flow_stats_reply(&fs, &b, NXFF_OPENFLOW10);
1111 if (retval != EOF) {
1112 ds_put_cstr(string, " ***parse error***");
1117 ds_put_char(string, '\n');
1119 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1121 ofp_print_duration(string, fs.duration_sec, fs.duration_nsec);
1122 ds_put_format(string, ", table_id=%"PRIu8", ", fs.table_id);
1123 ds_put_format(string, "n_packets=%"PRIu64", ", fs.packet_count);
1124 ds_put_format(string, "n_bytes=%"PRIu64", ", fs.byte_count);
1125 if (fs.idle_timeout != OFP_FLOW_PERMANENT) {
1126 ds_put_format(string, "idle_timeout=%"PRIu16",", fs.idle_timeout);
1128 if (fs.hard_timeout != OFP_FLOW_PERMANENT) {
1129 ds_put_format(string, "hard_timeout=%"PRIu16",", fs.hard_timeout);
1132 cls_rule_format(&fs.rule, string);
1133 ds_put_char(string, ' ');
1134 ofp_print_actions(string,
1135 (const struct ofp_action_header *) fs.actions,
1136 fs.n_actions * sizeof *fs.actions);
1141 ofp_print_ofp_aggregate_stats_reply (
1142 struct ds *string, const struct ofp_aggregate_stats_reply *asr)
1144 ds_put_format(string, " packet_count=%"PRIu64,
1145 ntohll(get_32aligned_be64(&asr->packet_count)));
1146 ds_put_format(string, " byte_count=%"PRIu64,
1147 ntohll(get_32aligned_be64(&asr->byte_count)));
1148 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1152 ofp_print_ofpst_aggregate_reply(struct ds *string, const struct ofp_header *oh)
1154 ofp_print_ofp_aggregate_stats_reply(string, ofputil_stats_body(oh));
1158 ofp_print_nxst_aggregate_reply(struct ds *string,
1159 const struct nx_aggregate_stats_reply *nasr)
1161 ofp_print_ofp_aggregate_stats_reply(string, &nasr->asr);
1164 static void print_port_stat(struct ds *string, const char *leader,
1165 const ovs_32aligned_be64 *statp, int more)
1167 uint64_t stat = ntohll(get_32aligned_be64(statp));
1169 ds_put_cstr(string, leader);
1170 if (stat != UINT64_MAX) {
1171 ds_put_format(string, "%"PRIu64, stat);
1173 ds_put_char(string, '?');
1176 ds_put_cstr(string, ", ");
1178 ds_put_cstr(string, "\n");
1183 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1185 const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
1186 ds_put_format(string, " port_no=%"PRIu16, ntohs(psr->port_no));
1190 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1193 const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1194 size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
1195 ds_put_format(string, " %zu ports\n", n);
1196 if (verbosity < 1) {
1201 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1203 ds_put_cstr(string, "rx ");
1204 print_port_stat(string, "pkts=", &ps->rx_packets, 1);
1205 print_port_stat(string, "bytes=", &ps->rx_bytes, 1);
1206 print_port_stat(string, "drop=", &ps->rx_dropped, 1);
1207 print_port_stat(string, "errs=", &ps->rx_errors, 1);
1208 print_port_stat(string, "frame=", &ps->rx_frame_err, 1);
1209 print_port_stat(string, "over=", &ps->rx_over_err, 1);
1210 print_port_stat(string, "crc=", &ps->rx_crc_err, 0);
1212 ds_put_cstr(string, " tx ");
1213 print_port_stat(string, "pkts=", &ps->tx_packets, 1);
1214 print_port_stat(string, "bytes=", &ps->tx_bytes, 1);
1215 print_port_stat(string, "drop=", &ps->tx_dropped, 1);
1216 print_port_stat(string, "errs=", &ps->tx_errors, 1);
1217 print_port_stat(string, "coll=", &ps->collisions, 0);
1222 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1225 const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1226 size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
1227 ds_put_format(string, " %zu tables\n", n);
1228 if (verbosity < 1) {
1233 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1234 ovs_strlcpy(name, ts->name, sizeof name);
1236 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1237 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1238 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1239 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1240 ds_put_cstr(string, " ");
1241 ds_put_format(string, "lookup=%"PRIu64", ",
1242 ntohll(get_32aligned_be64(&ts->lookup_count)));
1243 ds_put_format(string, "matched=%"PRIu64"\n",
1244 ntohll(get_32aligned_be64(&ts->matched_count)));
1249 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1251 if (queue_id == OFPQ_ALL) {
1252 ds_put_cstr(string, "ALL");
1254 ds_put_format(string, "%"PRIu32, queue_id);
1259 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1261 const struct ofp_queue_stats_request *qsr = ofputil_stats_body(oh);
1263 ds_put_cstr(string, "port=");
1264 ofp_print_port_name(string, ntohs(qsr->port_no));
1266 ds_put_cstr(string, " queue=");
1267 ofp_print_queue_name(string, ntohl(qsr->queue_id));
1271 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1274 const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1275 size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
1276 ds_put_format(string, " %zu queues\n", n);
1277 if (verbosity < 1) {
1282 ds_put_cstr(string, " port ");
1283 ofp_print_port_name(string, ntohs(qs->port_no));
1284 ds_put_cstr(string, " queue ");
1285 ofp_print_queue_name(string, ntohl(qs->queue_id));
1286 ds_put_cstr(string, ": ");
1288 print_port_stat(string, "bytes=", &qs->tx_bytes, 1);
1289 print_port_stat(string, "pkts=", &qs->tx_packets, 1);
1290 print_port_stat(string, "errors=", &qs->tx_errors, 0);
1295 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1297 const struct ofp_stats_request *srq
1298 = (const struct ofp_stats_request *) oh;
1301 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1307 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1309 const struct ofp_stats_reply *srp = (const struct ofp_stats_reply *) oh;
1312 uint16_t flags = ntohs(srp->flags);
1314 ds_put_cstr(string, " flags=");
1315 if (flags & OFPSF_REPLY_MORE) {
1316 ds_put_cstr(string, "[more]");
1317 flags &= ~OFPSF_REPLY_MORE;
1320 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1327 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1329 size_t len = ntohs(oh->length);
1331 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1332 if (verbosity > 1) {
1333 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1338 ofp_print_nxt_tun_id_from_cookie(struct ds *string,
1339 const struct nxt_tun_id_cookie *ntic)
1341 ds_put_format(string, " set=%"PRIu8, ntic->set);
1345 ofp_print_nxt_role_message(struct ds *string,
1346 const struct nx_role_request *nrr)
1348 unsigned int role = ntohl(nrr->role);
1350 ds_put_cstr(string, " role=");
1351 if (role == NX_ROLE_OTHER) {
1352 ds_put_cstr(string, "other");
1353 } else if (role == NX_ROLE_MASTER) {
1354 ds_put_cstr(string, "master");
1355 } else if (role == NX_ROLE_SLAVE) {
1356 ds_put_cstr(string, "slave");
1358 ds_put_format(string, "%u", role);
1363 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1364 const struct nxt_flow_mod_table_id *nfmti)
1366 ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1370 ofp_print_nxt_set_flow_format(struct ds *string,
1371 const struct nxt_set_flow_format *nsff)
1373 uint32_t format = ntohl(nsff->format);
1375 ds_put_cstr(string, " format=");
1376 if (ofputil_flow_format_is_valid(format)) {
1377 ds_put_cstr(string, ofputil_flow_format_to_string(format));
1379 ds_put_format(string, "%"PRIu32, format);
1384 ofp_to_string__(const struct ofp_header *oh,
1385 const struct ofputil_msg_type *type, struct ds *string,
1388 enum ofputil_msg_code code;
1389 const void *msg = oh;
1391 ds_put_format(string, "%s (xid=0x%"PRIx32"):",
1392 ofputil_msg_type_name(type), ntohl(oh->xid));
1394 code = ofputil_msg_type_code(type);
1396 case OFPUTIL_INVALID:
1399 case OFPUTIL_OFPT_HELLO:
1400 ds_put_char(string, '\n');
1401 ds_put_hex_dump(string, oh + 1, ntohs(oh->length) - sizeof *oh,
1405 case OFPUTIL_OFPT_ERROR:
1406 ofp_print_error_msg(string, msg);
1409 case OFPUTIL_OFPT_ECHO_REQUEST:
1410 case OFPUTIL_OFPT_ECHO_REPLY:
1411 ofp_print_echo(string, oh, verbosity);
1414 case OFPUTIL_OFPT_FEATURES_REQUEST:
1417 case OFPUTIL_OFPT_FEATURES_REPLY:
1418 ofp_print_switch_features(string, msg);
1421 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1424 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1425 case OFPUTIL_OFPT_SET_CONFIG:
1426 ofp_print_switch_config(string, msg);
1429 case OFPUTIL_OFPT_PACKET_IN:
1430 ofp_print_packet_in(string, msg, verbosity);
1433 case OFPUTIL_OFPT_FLOW_REMOVED:
1434 case OFPUTIL_NXT_FLOW_REMOVED:
1435 ofp_print_flow_removed(string, msg);
1438 case OFPUTIL_OFPT_PORT_STATUS:
1439 ofp_print_port_status(string, msg);
1442 case OFPUTIL_OFPT_PACKET_OUT:
1443 ofp_print_packet_out(string, msg, verbosity);
1446 case OFPUTIL_OFPT_FLOW_MOD:
1447 ofp_print_flow_mod(string, msg, code, verbosity);
1450 case OFPUTIL_OFPT_PORT_MOD:
1451 ofp_print_port_mod(string, msg);
1454 case OFPUTIL_OFPT_BARRIER_REQUEST:
1455 case OFPUTIL_OFPT_BARRIER_REPLY:
1458 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1459 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1463 case OFPUTIL_OFPST_DESC_REQUEST:
1464 ofp_print_stats_request(string, oh);
1467 case OFPUTIL_OFPST_FLOW_REQUEST:
1468 case OFPUTIL_NXST_FLOW_REQUEST:
1469 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1470 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1471 ofp_print_stats_request(string, oh);
1472 ofp_print_flow_stats_request(string, oh);
1475 case OFPUTIL_OFPST_TABLE_REQUEST:
1476 ofp_print_stats_request(string, oh);
1479 case OFPUTIL_OFPST_PORT_REQUEST:
1480 ofp_print_stats_request(string, oh);
1481 ofp_print_ofpst_port_request(string, oh);
1484 case OFPUTIL_OFPST_QUEUE_REQUEST:
1485 ofp_print_stats_request(string, oh);
1486 ofp_print_ofpst_queue_request(string, oh);
1489 case OFPUTIL_OFPST_DESC_REPLY:
1490 ofp_print_stats_reply(string, oh);
1491 ofp_print_ofpst_desc_reply(string, oh);
1494 case OFPUTIL_OFPST_FLOW_REPLY:
1495 case OFPUTIL_NXST_FLOW_REPLY:
1496 ofp_print_stats_reply(string, oh);
1497 ofp_print_flow_stats_reply(string, oh);
1500 case OFPUTIL_OFPST_QUEUE_REPLY:
1501 ofp_print_stats_reply(string, oh);
1502 ofp_print_ofpst_queue_reply(string, oh, verbosity);
1505 case OFPUTIL_OFPST_PORT_REPLY:
1506 ofp_print_stats_reply(string, oh);
1507 ofp_print_ofpst_port_reply(string, oh, verbosity);
1510 case OFPUTIL_OFPST_TABLE_REPLY:
1511 ofp_print_stats_reply(string, oh);
1512 ofp_print_ofpst_table_reply(string, oh, verbosity);
1515 case OFPUTIL_OFPST_AGGREGATE_REPLY:
1516 ofp_print_stats_reply(string, oh);
1517 ofp_print_ofpst_aggregate_reply(string, oh);
1520 case OFPUTIL_NXT_TUN_ID_FROM_COOKIE:
1521 ofp_print_nxt_tun_id_from_cookie(string, msg);
1524 case OFPUTIL_NXT_ROLE_REQUEST:
1525 case OFPUTIL_NXT_ROLE_REPLY:
1526 ofp_print_nxt_role_message(string, msg);
1529 case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
1530 ofp_print_nxt_flow_mod_table_id(string, msg);
1533 case OFPUTIL_NXT_SET_FLOW_FORMAT:
1534 ofp_print_nxt_set_flow_format(string, msg);
1537 case OFPUTIL_NXT_FLOW_MOD:
1538 ofp_print_flow_mod(string, msg, code, verbosity);
1541 case OFPUTIL_NXST_AGGREGATE_REPLY:
1542 ofp_print_stats_reply(string, oh);
1543 ofp_print_nxst_aggregate_reply(string, msg);
1548 /* Composes and returns a string representing the OpenFlow packet of 'len'
1549 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1550 * verbosity and higher numbers increase verbosity. The caller is responsible
1551 * for freeing the string. */
1553 ofp_to_string(const void *oh_, size_t len, int verbosity)
1555 struct ds string = DS_EMPTY_INITIALIZER;
1556 const struct ofp_header *oh = oh_;
1559 ds_put_cstr(&string, "OpenFlow message is empty\n");
1560 } else if (len < sizeof(struct ofp_header)) {
1561 ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
1563 } else if (oh->version != OFP_VERSION) {
1564 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n",
1566 } else if (ntohs(oh->length) > len) {
1567 ds_put_format(&string,
1568 "(***truncated to %zu bytes from %"PRIu16"***)\n",
1569 len, ntohs(oh->length));
1570 } else if (ntohs(oh->length) < len) {
1571 ds_put_format(&string,
1572 "(***only uses %"PRIu16" bytes out of %zu***)\n",
1573 ntohs(oh->length), len);
1575 const struct ofputil_msg_type *type;
1578 error = ofputil_decode_msg_type(oh, &type);
1580 ofp_to_string__(oh, type, &string, verbosity);
1581 if (verbosity >= 5) {
1582 if (ds_last(&string) != '\n') {
1583 ds_put_char(&string, '\n');
1585 ds_put_hex_dump(&string, oh, len, 0, true);
1587 if (ds_last(&string) != '\n') {
1588 ds_put_char(&string, '\n');
1590 return ds_steal_cstr(&string);
1593 ofp_print_error(&string, error);
1595 ds_put_hex_dump(&string, oh, len, 0, true);
1596 return ds_steal_cstr(&string);
1599 /* Returns the name for the specified OpenFlow message type as a string,
1600 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1601 * hex number, e.g. "0x55".
1603 * The caller must free the returned string when it is no longer needed. */
1605 ofp_message_type_to_string(uint8_t type)
1616 case OFPT_ECHO_REQUEST:
1617 name = "ECHO_REQUEST";
1619 case OFPT_ECHO_REPLY:
1620 name = "ECHO_REPLY";
1625 case OFPT_FEATURES_REQUEST:
1626 name = "FEATURES_REQUEST";
1628 case OFPT_FEATURES_REPLY:
1629 name = "FEATURES_REPLY";
1631 case OFPT_GET_CONFIG_REQUEST:
1632 name = "GET_CONFIG_REQUEST";
1634 case OFPT_GET_CONFIG_REPLY:
1635 name = "GET_CONFIG_REPLY";
1637 case OFPT_SET_CONFIG:
1638 name = "SET_CONFIG";
1640 case OFPT_PACKET_IN:
1643 case OFPT_FLOW_REMOVED:
1644 name = "FLOW_REMOVED";
1646 case OFPT_PORT_STATUS:
1647 name = "PORT_STATUS";
1649 case OFPT_PACKET_OUT:
1650 name = "PACKET_OUT";
1658 case OFPT_STATS_REQUEST:
1659 name = "STATS_REQUEST";
1661 case OFPT_STATS_REPLY:
1662 name = "STATS_REPLY";
1664 case OFPT_BARRIER_REQUEST:
1665 name = "BARRIER_REQUEST";
1667 case OFPT_BARRIER_REPLY:
1668 name = "BARRIER_REPLY";
1670 case OFPT_QUEUE_GET_CONFIG_REQUEST:
1671 name = "QUEUE_GET_CONFIG_REQUEST";
1673 case OFPT_QUEUE_GET_CONFIG_REPLY:
1674 name = "QUEUE_GET_CONFIG_REPLY";
1681 return name ? xasprintf("OFPT_%s", name) : xasprintf("0x%02"PRIx8, type);
1685 print_and_free(FILE *stream, char *string)
1687 fputs(string, stream);
1691 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1692 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1693 * numbers increase verbosity. */
1695 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1697 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1700 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1701 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1702 * the Ethernet frame (of which 'len' bytes were captured).
1704 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1706 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1708 print_and_free(stream, ofp_packet_to_string(data, len, total_len));