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>
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"
44 static void ofp_print_port_name(struct ds *string, uint16_t port);
45 static void ofp_print_queue_name(struct ds *string, uint32_t port);
46 static void ofp_print_error(struct ds *, int error);
49 /* Returns a string that represents the contents of the Ethernet frame in the
50 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
51 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
52 * bytes were captured).
54 * The caller must free the returned string.
56 * This starts and kills a tcpdump subprocess so it's quite expensive. */
58 ofp_packet_to_string(const void *data, size_t len, size_t total_len OVS_UNUSED)
60 struct ds ds = DS_EMPTY_INITIALIZER;
69 ofpbuf_use_const(&buf, data, len);
73 ovs_error(errno, "tmpfile");
74 return xstrdup("<error>");
76 pcap_write_header(pcap);
77 pcap_write(pcap, &buf);
80 ovs_error(errno, "error writing temporary file");
84 snprintf(command, sizeof command, "/usr/sbin/tcpdump -e -n -r /dev/fd/%d 2>/dev/null",
86 tcpdump = popen(command, "r");
89 ovs_error(errno, "exec(\"%s\")", command);
90 return xstrdup("<error>");
93 while ((c = getc(tcpdump)) != EOF) {
97 status = pclose(tcpdump);
98 if (WIFEXITED(status)) {
99 if (WEXITSTATUS(status))
100 ovs_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
101 } else if (WIFSIGNALED(status)) {
102 ovs_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
108 ofp_print_packet_in(struct ds *string, const struct ofp_packet_in *op,
111 size_t len = ntohs(op->header.length);
114 ds_put_format(string, " total_len=%"PRIu16" in_port=",
115 ntohs(op->total_len));
116 ofp_print_port_name(string, ntohs(op->in_port));
118 if (op->reason == OFPR_ACTION)
119 ds_put_cstr(string, " (via action)");
120 else if (op->reason != OFPR_NO_MATCH)
121 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
123 data_len = len - offsetof(struct ofp_packet_in, data);
124 ds_put_format(string, " data_len=%zu", data_len);
125 if (htonl(op->buffer_id) == UINT32_MAX) {
126 ds_put_format(string, " (unbuffered)");
127 if (ntohs(op->total_len) != data_len)
128 ds_put_format(string, " (***total_len != data_len***)");
130 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
131 if (ntohs(op->total_len) < data_len)
132 ds_put_format(string, " (***total_len < data_len***)");
134 ds_put_char(string, '\n');
138 struct ofpbuf packet;
140 ofpbuf_use_const(&packet, op->data, data_len);
141 flow_extract(&packet, 0, ntohs(op->in_port), &flow);
142 flow_format(string, &flow);
143 ds_put_char(string, '\n');
146 char *packet = ofp_packet_to_string(op->data, data_len,
147 ntohs(op->total_len));
148 ds_put_cstr(string, packet);
153 static void ofp_print_port_name(struct ds *string, uint16_t port)
172 case OFPP_CONTROLLER:
182 ds_put_format(string, "%"PRIu16, port);
185 ds_put_cstr(string, name);
190 print_note(struct ds *string, const struct nx_action_note *nan)
195 ds_put_cstr(string, "note:");
196 len = ntohs(nan->len) - offsetof(struct nx_action_note, note);
197 for (i = 0; i < len; i++) {
199 ds_put_char(string, '.');
201 ds_put_format(string, "%02"PRIx8, nan->note[i]);
206 nx_action_len(enum nx_action_subtype subtype)
209 case NXAST_SNAT__OBSOLETE: return -1;
210 case NXAST_RESUBMIT: return sizeof(struct nx_action_resubmit);
211 case NXAST_SET_TUNNEL: return sizeof(struct nx_action_set_tunnel);
212 case NXAST_DROP_SPOOFED_ARP:
213 return sizeof(struct nx_action_drop_spoofed_arp);
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);
226 ofp_print_nx_action(struct ds *string, const struct nx_action_header *nah)
228 uint16_t subtype = ntohs(nah->subtype);
229 int required_len = nx_action_len(subtype);
230 int len = ntohs(nah->len);
232 if (required_len != -1 && required_len != len) {
233 ds_put_format(string, "***Nicira action %"PRIu16" wrong length: %d***",
238 if (subtype <= TYPE_MAXIMUM(enum nx_action_subtype)) {
239 const struct nx_action_set_tunnel64 *nast64;
240 const struct nx_action_set_tunnel *nast;
241 const struct nx_action_set_queue *nasq;
242 const struct nx_action_resubmit *nar;
243 const struct nx_action_reg_move *move;
244 const struct nx_action_reg_load *load;
245 const struct nx_action_multipath *nam;
247 switch ((enum nx_action_subtype) subtype) {
249 nar = (struct nx_action_resubmit *)nah;
250 ds_put_format(string, "resubmit:");
251 ofp_print_port_name(string, ntohs(nar->in_port));
254 case NXAST_SET_TUNNEL:
255 nast = (struct nx_action_set_tunnel *)nah;
256 ds_put_format(string, "set_tunnel:%#"PRIx32, ntohl(nast->tun_id));
259 case NXAST_DROP_SPOOFED_ARP:
260 ds_put_cstr(string, "drop_spoofed_arp");
263 case NXAST_SET_QUEUE:
264 nasq = (struct nx_action_set_queue *)nah;
265 ds_put_format(string, "set_queue:%u", ntohl(nasq->queue_id));
268 case NXAST_POP_QUEUE:
269 ds_put_cstr(string, "pop_queue");
273 print_note(string, (const struct nx_action_note *) nah);
277 move = (const struct nx_action_reg_move *) nah;
278 nxm_format_reg_move(move, string);
282 load = (const struct nx_action_reg_load *) nah;
283 nxm_format_reg_load(load, string);
286 case NXAST_SET_TUNNEL64:
287 nast64 = (const struct nx_action_set_tunnel64 *) nah;
288 ds_put_format(string, "set_tunnel64:%#"PRIx64,
289 ntohll(nast64->tun_id));
292 case NXAST_MULTIPATH:
293 nam = (const struct nx_action_multipath *) nah;
294 multipath_format(nam, string);
297 case NXAST_SNAT__OBSOLETE:
303 ds_put_format(string, "***unknown Nicira action:%"PRIu16"***", subtype);
307 ofp_action_len(enum ofp_action_type type)
310 case OFPAT_OUTPUT: return sizeof(struct ofp_action_output);
311 case OFPAT_SET_VLAN_VID: return sizeof(struct ofp_action_vlan_vid);
312 case OFPAT_SET_VLAN_PCP: return sizeof(struct ofp_action_vlan_pcp);
313 case OFPAT_STRIP_VLAN: return sizeof(struct ofp_action_header);
314 case OFPAT_SET_DL_SRC: return sizeof(struct ofp_action_dl_addr);
315 case OFPAT_SET_DL_DST: return sizeof(struct ofp_action_dl_addr);
316 case OFPAT_SET_NW_SRC: return sizeof(struct ofp_action_nw_addr);
317 case OFPAT_SET_NW_DST: return sizeof(struct ofp_action_nw_addr);
318 case OFPAT_SET_NW_TOS: return sizeof(struct ofp_action_nw_tos);
319 case OFPAT_SET_TP_SRC: return sizeof(struct ofp_action_tp_port);
320 case OFPAT_SET_TP_DST: return sizeof(struct ofp_action_tp_port);
321 case OFPAT_ENQUEUE: return sizeof(struct ofp_action_enqueue);
322 case OFPAT_VENDOR: return -1;
328 ofp_print_action(struct ds *string, const struct ofp_action_header *ah,
331 enum ofp_action_type type;
335 if (actions_len < sizeof *ah) {
336 ds_put_format(string, "***action array too short for next action***\n");
340 type = ntohs(ah->type);
341 len = ntohs(ah->len);
342 if (actions_len < len) {
343 ds_put_format(string, "***truncated action %d***\n", (int) type);
348 ds_put_format(string, "***zero-length action***\n");
352 if ((len % OFP_ACTION_ALIGN) != 0) {
353 ds_put_format(string,
354 "***action %d length not a multiple of %d***\n",
355 (int) type, OFP_ACTION_ALIGN);
359 required_len = ofp_action_len(type);
360 if (required_len >= 0 && len != required_len) {
361 ds_put_format(string,
362 "***action %d wrong length: %zu***\n", (int) type, len);
368 struct ofp_action_output *oa = (struct ofp_action_output *)ah;
369 uint16_t port = ntohs(oa->port);
370 if (port < OFPP_MAX) {
371 ds_put_format(string, "output:%"PRIu16, port);
373 ofp_print_port_name(string, port);
374 if (port == OFPP_CONTROLLER) {
376 ds_put_format(string, ":%"PRIu16, ntohs(oa->max_len));
378 ds_put_cstr(string, ":all");
385 case OFPAT_ENQUEUE: {
386 struct ofp_action_enqueue *ea = (struct ofp_action_enqueue *)ah;
387 unsigned int port = ntohs(ea->port);
388 unsigned int queue_id = ntohl(ea->queue_id);
389 ds_put_format(string, "enqueue:");
390 if (port != OFPP_IN_PORT) {
391 ds_put_format(string, "%u", port);
393 ds_put_cstr(string, "IN_PORT");
395 ds_put_format(string, "q%u", queue_id);
399 case OFPAT_SET_VLAN_VID: {
400 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
401 ds_put_format(string, "mod_vlan_vid:%"PRIu16, ntohs(va->vlan_vid));
405 case OFPAT_SET_VLAN_PCP: {
406 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
407 ds_put_format(string, "mod_vlan_pcp:%"PRIu8, va->vlan_pcp);
411 case OFPAT_STRIP_VLAN:
412 ds_put_cstr(string, "strip_vlan");
415 case OFPAT_SET_DL_SRC: {
416 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
417 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
418 ETH_ADDR_ARGS(da->dl_addr));
422 case OFPAT_SET_DL_DST: {
423 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
424 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
425 ETH_ADDR_ARGS(da->dl_addr));
429 case OFPAT_SET_NW_SRC: {
430 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
431 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&na->nw_addr));
435 case OFPAT_SET_NW_DST: {
436 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
437 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&na->nw_addr));
441 case OFPAT_SET_NW_TOS: {
442 struct ofp_action_nw_tos *nt = (struct ofp_action_nw_tos *)ah;
443 ds_put_format(string, "mod_nw_tos:%d", nt->nw_tos);
447 case OFPAT_SET_TP_SRC: {
448 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
449 ds_put_format(string, "mod_tp_src:%d", ntohs(ta->tp_port));
453 case OFPAT_SET_TP_DST: {
454 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
455 ds_put_format(string, "mod_tp_dst:%d", ntohs(ta->tp_port));
460 struct ofp_action_vendor_header *avh
461 = (struct ofp_action_vendor_header *)ah;
462 if (len < sizeof *avh) {
463 ds_put_format(string, "***ofpat_vendor truncated***\n");
466 if (avh->vendor == htonl(NX_VENDOR_ID)) {
467 ofp_print_nx_action(string, (struct nx_action_header *)avh);
469 ds_put_format(string, "vendor action:0x%x", ntohl(avh->vendor));
475 ds_put_format(string, "(decoder %d not implemented)", (int) type);
483 ofp_print_actions(struct ds *string, const struct ofp_action_header *action,
486 uint8_t *p = (uint8_t *)action;
489 ds_put_cstr(string, "actions=");
491 ds_put_cstr(string, "drop");
493 while (actions_len > 0) {
495 ds_put_cstr(string, ",");
497 len = ofp_print_action(string, (struct ofp_action_header *)p,
508 ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
511 size_t len = ntohs(opo->header.length);
512 size_t actions_len = ntohs(opo->actions_len);
514 ds_put_cstr(string, " in_port=");
515 ofp_print_port_name(string, ntohs(opo->in_port));
517 ds_put_format(string, " actions_len=%zu ", actions_len);
518 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
519 ds_put_format(string, "***packet too short for action length***\n");
522 ofp_print_actions(string, opo->actions, actions_len);
524 if (ntohl(opo->buffer_id) == UINT32_MAX) {
525 int data_len = len - sizeof *opo - actions_len;
526 ds_put_format(string, " data_len=%d", data_len);
527 if (verbosity > 0 && len > sizeof *opo) {
528 char *packet = ofp_packet_to_string(
529 (uint8_t *)opo->actions + actions_len, data_len, data_len);
530 ds_put_char(string, '\n');
531 ds_put_cstr(string, packet);
535 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
537 ds_put_char(string, '\n');
540 /* qsort comparison function. */
542 compare_ports(const void *a_, const void *b_)
544 const struct ofp_phy_port *a = a_;
545 const struct ofp_phy_port *b = b_;
546 uint16_t ap = ntohs(a->port_no);
547 uint16_t bp = ntohs(b->port_no);
549 return ap < bp ? -1 : ap > bp;
552 static void ofp_print_port_features(struct ds *string, uint32_t features)
555 ds_put_cstr(string, "Unsupported\n");
558 if (features & OFPPF_10MB_HD) {
559 ds_put_cstr(string, "10MB-HD ");
561 if (features & OFPPF_10MB_FD) {
562 ds_put_cstr(string, "10MB-FD ");
564 if (features & OFPPF_100MB_HD) {
565 ds_put_cstr(string, "100MB-HD ");
567 if (features & OFPPF_100MB_FD) {
568 ds_put_cstr(string, "100MB-FD ");
570 if (features & OFPPF_1GB_HD) {
571 ds_put_cstr(string, "1GB-HD ");
573 if (features & OFPPF_1GB_FD) {
574 ds_put_cstr(string, "1GB-FD ");
576 if (features & OFPPF_10GB_FD) {
577 ds_put_cstr(string, "10GB-FD ");
579 if (features & OFPPF_COPPER) {
580 ds_put_cstr(string, "COPPER ");
582 if (features & OFPPF_FIBER) {
583 ds_put_cstr(string, "FIBER ");
585 if (features & OFPPF_AUTONEG) {
586 ds_put_cstr(string, "AUTO_NEG ");
588 if (features & OFPPF_PAUSE) {
589 ds_put_cstr(string, "AUTO_PAUSE ");
591 if (features & OFPPF_PAUSE_ASYM) {
592 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
594 ds_put_char(string, '\n');
598 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
600 char name[OFP_MAX_PORT_NAME_LEN];
603 memcpy(name, port->name, sizeof name);
604 for (j = 0; j < sizeof name - 1; j++) {
605 if (!isprint(name[j])) {
611 ds_put_char(string, ' ');
612 ofp_print_port_name(string, ntohs(port->port_no));
613 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
614 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
617 ds_put_format(string, " current: ");
618 ofp_print_port_features(string, ntohl(port->curr));
620 if (port->advertised) {
621 ds_put_format(string, " advertised: ");
622 ofp_print_port_features(string, ntohl(port->advertised));
624 if (port->supported) {
625 ds_put_format(string, " supported: ");
626 ofp_print_port_features(string, ntohl(port->supported));
629 ds_put_format(string, " peer: ");
630 ofp_print_port_features(string, ntohl(port->peer));
635 ofp_print_switch_features(struct ds *string,
636 const struct ofp_switch_features *osf)
638 size_t len = ntohs(osf->header.length);
639 struct ofp_phy_port *port_list;
643 ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
644 osf->header.version, ntohll(osf->datapath_id));
645 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
646 ntohl(osf->n_buffers));
647 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
648 ntohl(osf->capabilities), ntohl(osf->actions));
650 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
652 port_list = xmemdup(osf->ports, len - sizeof *osf);
653 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
654 for (i = 0; i < n_ports; i++) {
655 ofp_print_phy_port(string, &port_list[i]);
661 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
665 flags = ntohs(osc->flags);
667 ds_put_cstr(string, " frags=");
668 switch (flags & OFPC_FRAG_MASK) {
669 case OFPC_FRAG_NORMAL:
670 ds_put_cstr(string, "normal");
671 flags &= ~OFPC_FRAG_MASK;
674 ds_put_cstr(string, "drop");
675 flags &= ~OFPC_FRAG_MASK;
677 case OFPC_FRAG_REASM:
678 ds_put_cstr(string, "reassemble");
679 flags &= ~OFPC_FRAG_MASK;
683 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
686 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
689 static void print_wild(struct ds *string, const char *leader, int is_wild,
690 int verbosity, const char *format, ...)
691 __attribute__((format(printf, 5, 6)));
693 static void print_wild(struct ds *string, const char *leader, int is_wild,
694 int verbosity, const char *format, ...)
696 if (is_wild && verbosity < 2) {
699 ds_put_cstr(string, leader);
703 va_start(args, format);
704 ds_put_format_valist(string, format, args);
707 ds_put_char(string, '*');
709 ds_put_char(string, ',');
713 print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
714 uint32_t wild_bits, int verbosity)
716 if (wild_bits >= 32 && verbosity < 2) {
719 ds_put_cstr(string, leader);
720 if (wild_bits < 32) {
721 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
723 ds_put_format(string, "/%d", 32 - wild_bits);
726 ds_put_char(string, '*');
728 ds_put_char(string, ',');
732 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
734 char *s = ofp_match_to_string(om, verbosity);
740 ofp_match_to_string(const struct ofp_match *om, int verbosity)
742 struct ds f = DS_EMPTY_INITIALIZER;
743 uint32_t w = ntohl(om->wildcards);
744 bool skip_type = false;
745 bool skip_proto = false;
747 if (!(w & OFPFW_DL_TYPE)) {
749 if (om->dl_type == htons(ETH_TYPE_IP)) {
750 if (!(w & OFPFW_NW_PROTO)) {
752 if (om->nw_proto == IP_TYPE_ICMP) {
753 ds_put_cstr(&f, "icmp,");
754 } else if (om->nw_proto == IP_TYPE_TCP) {
755 ds_put_cstr(&f, "tcp,");
756 } else if (om->nw_proto == IP_TYPE_UDP) {
757 ds_put_cstr(&f, "udp,");
759 ds_put_cstr(&f, "ip,");
763 ds_put_cstr(&f, "ip,");
765 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
766 ds_put_cstr(&f, "arp,");
771 if (w & NXFW_TUN_ID) {
772 ds_put_cstr(&f, "tun_id_wild,");
774 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
775 "%d", ntohs(om->in_port));
776 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
777 "%d", ntohs(om->dl_vlan));
778 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
779 "%d", om->dl_vlan_pcp);
780 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
781 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
782 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
783 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
785 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
786 "0x%04x", ntohs(om->dl_type));
788 print_ip_netmask(&f, "nw_src=", om->nw_src,
789 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
790 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
791 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
793 if (om->dl_type == htons(ETH_TYPE_ARP)) {
794 print_wild(&f, "opcode=", w & OFPFW_NW_PROTO, verbosity,
797 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
801 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
803 if (om->nw_proto == IP_TYPE_ICMP) {
804 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
805 "%d", ntohs(om->icmp_type));
806 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
807 "%d", ntohs(om->icmp_code));
809 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
810 "%d", ntohs(om->tp_src));
811 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
812 "%d", ntohs(om->tp_dst));
814 if (ds_last(&f) == ',') {
821 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
822 enum ofputil_msg_code code, int verbosity)
828 error = ofputil_decode_flow_mod(&fm, oh, NXFF_OPENFLOW10);
830 ofp_print_error(s, error);
835 switch (fm.command) {
837 ds_put_cstr(s, "ADD");
840 ds_put_cstr(s, "MOD");
842 case OFPFC_MODIFY_STRICT:
843 ds_put_cstr(s, "MOD_STRICT");
846 ds_put_cstr(s, "DEL");
848 case OFPFC_DELETE_STRICT:
849 ds_put_cstr(s, "DEL_STRICT");
852 ds_put_format(s, "cmd:%d", fm.command);
856 if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
857 const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
858 ofp_print_match(s, &ofm->match, verbosity);
860 /* ofp_print_match() doesn't print priority. */
861 need_priority = true;
862 } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
863 const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
864 const void *nxm = nfm + 1;
867 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
868 ds_put_cstr(s, nxm_s);
871 /* nx_match_to_string() doesn't print priority. */
872 need_priority = true;
874 cls_rule_format(&fm.cr, s);
876 /* cls_rule_format() does print priority. */
877 need_priority = false;
880 if (ds_last(s) != ' ') {
883 if (fm.cookie != htonll(0)) {
884 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.cookie));
886 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
887 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
889 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
890 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
892 if (fm.cr.priority != OFP_DEFAULT_PRIORITY && need_priority) {
893 ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
895 if (fm.buffer_id != UINT32_MAX) {
896 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
899 ds_put_format(s, "flags:0x%"PRIx16" ", fm.flags);
902 ofp_print_actions(s, (const struct ofp_action_header *) fm.actions,
903 fm.n_actions * sizeof *fm.actions);
907 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
909 ds_put_format(string, "%u", sec);
911 ds_put_format(string, ".%09u", nsec);
912 while (string->string[string->length - 1] == '0') {
916 ds_put_char(string, 's');
920 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
922 struct ofputil_flow_removed fr;
925 error = ofputil_decode_flow_removed(&fr, oh, NXFF_OPENFLOW10);
927 ofp_print_error(string, error);
931 ds_put_char(string, ' ');
932 cls_rule_format(&fr.rule, string);
934 ds_put_cstr(string, " reason=");
936 case OFPRR_IDLE_TIMEOUT:
937 ds_put_cstr(string, "idle");
939 case OFPRR_HARD_TIMEOUT:
940 ds_put_cstr(string, "hard");
943 ds_put_cstr(string, "delete");
946 ds_put_format(string, "**%"PRIu8"**", fr.reason);
950 if (fr.cookie != htonll(0)) {
951 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
953 ds_put_cstr(string, " duration");
954 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
955 ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
956 fr.idle_timeout, fr.packet_count, fr.byte_count);
960 ofp_print_port_mod(struct ds *string, const struct ofp_port_mod *opm)
962 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
963 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
964 ntohl(opm->config), ntohl(opm->mask));
965 ds_put_format(string, " advertise: ");
966 if (opm->advertise) {
967 ofp_print_port_features(string, ntohl(opm->advertise));
969 ds_put_format(string, "UNCHANGED\n");
974 ofp_print_error(struct ds *string, int error)
976 if (string->length) {
977 ds_put_char(string, ' ');
979 ds_put_cstr(string, "***decode error: ");
980 ofputil_format_error(string, error);
981 ds_put_cstr(string, "***\n");
985 ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
987 size_t len = ntohs(oem->header.length);
988 size_t payload_ofs, payload_len;
993 error = ofputil_decode_error_msg(&oem->header, &payload_ofs);
994 if (!is_ofp_error(error)) {
995 ofp_print_error(string, error);
996 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
1000 ds_put_char(string, ' ');
1001 ofputil_format_error(string, error);
1002 ds_put_char(string, '\n');
1004 payload = (const uint8_t *) oem + payload_ofs;
1005 payload_len = len - payload_ofs;
1006 switch (get_ofp_err_type(error)) {
1007 case OFPET_HELLO_FAILED:
1008 ds_put_printable(string, payload, payload_len);
1011 case OFPET_BAD_REQUEST:
1012 s = ofp_to_string(payload, payload_len, 1);
1013 ds_put_cstr(string, s);
1018 ds_put_hex_dump(string, payload, payload_len, 0, true);
1024 ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
1026 if (ops->reason == OFPPR_ADD) {
1027 ds_put_format(string, " ADD:");
1028 } else if (ops->reason == OFPPR_DELETE) {
1029 ds_put_format(string, " DEL:");
1030 } else if (ops->reason == OFPPR_MODIFY) {
1031 ds_put_format(string, " MOD:");
1034 ofp_print_phy_port(string, &ops->desc);
1038 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
1040 const struct ofp_desc_stats *ods = ofputil_stats_body(oh);
1042 ds_put_char(string, '\n');
1043 ds_put_format(string, "Manufacturer: %.*s\n",
1044 (int) sizeof ods->mfr_desc, ods->mfr_desc);
1045 ds_put_format(string, "Hardware: %.*s\n",
1046 (int) sizeof ods->hw_desc, ods->hw_desc);
1047 ds_put_format(string, "Software: %.*s\n",
1048 (int) sizeof ods->sw_desc, ods->sw_desc);
1049 ds_put_format(string, "Serial Num: %.*s\n",
1050 (int) sizeof ods->serial_num, ods->serial_num);
1051 ds_put_format(string, "DP Description: %.*s\n",
1052 (int) sizeof ods->dp_desc, ods->dp_desc);
1056 ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
1058 struct flow_stats_request fsr;
1061 error = ofputil_decode_flow_stats_request(&fsr, oh, NXFF_OPENFLOW10);
1063 ofp_print_error(string, error);
1067 if (fsr.table_id != 0xff) {
1068 ds_put_format(string, " table_id=%"PRIu8, fsr.table_id);
1071 if (fsr.out_port != OFPP_NONE) {
1072 ds_put_cstr(string, " out_port=");
1073 ofp_print_port_name(string, fsr.out_port);
1076 ds_put_char(string, ' ');
1077 cls_rule_format(&fsr.match, string);
1081 ofp_print_ofpst_flow_reply(struct ds *string, const struct ofp_header *oh,
1084 size_t len = ofputil_stats_body_len(oh);
1085 const char *body = ofputil_stats_body(oh);
1086 const char *pos = body;
1088 const struct ofp_flow_stats *fs;
1089 ptrdiff_t bytes_left = body + len - pos;
1092 ds_put_char(string, '\n');
1094 if (bytes_left < sizeof *fs) {
1095 if (bytes_left != 0) {
1096 ds_put_format(string, " ***%td leftover bytes at end***",
1102 fs = (const void *) pos;
1103 length = ntohs(fs->length);
1104 if (length < sizeof *fs) {
1105 ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
1106 length, sizeof *fs);
1108 } else if (length > bytes_left) {
1109 ds_put_format(string,
1110 " ***length=%zu but only %td bytes left***",
1111 length, bytes_left);
1113 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
1114 ds_put_format(string,
1115 " ***length=%zu has %zu bytes leftover in "
1118 (length - sizeof *fs) % sizeof fs->actions[0]);
1122 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1123 ntohll(fs->cookie));
1124 ofp_print_duration(string, ntohl(fs->duration_sec),
1125 ntohl(fs->duration_nsec));
1126 ds_put_format(string, ", table_id=%"PRIu8", ", fs->table_id);
1127 ds_put_format(string, "priority=%"PRIu16", ", ntohs(fs->priority));
1128 ds_put_format(string, "n_packets=%"PRIu64", ",
1129 ntohll(fs->packet_count));
1130 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
1131 if (fs->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
1132 ds_put_format(string, "idle_timeout=%"PRIu16",",
1133 ntohs(fs->idle_timeout));
1135 if (fs->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
1136 ds_put_format(string, "hard_timeout=%"PRIu16",",
1137 ntohs(fs->hard_timeout));
1139 ofp_print_match(string, &fs->match, verbosity);
1140 ds_put_char(string, ' ');
1141 ofp_print_actions(string, fs->actions, length - sizeof *fs);
1148 ofp_print_nxst_flow_reply(struct ds *string, const struct ofp_header *oh)
1152 ofpbuf_use_const(&b, ofputil_nxstats_body(oh),
1153 ofputil_nxstats_body_len(oh));
1154 while (b.size > 0) {
1155 const struct nx_flow_stats *fs;
1156 union ofp_action *actions;
1157 struct cls_rule rule;
1158 size_t actions_len, n_actions;
1163 ds_put_char(string, '\n');
1165 fs = ofpbuf_try_pull(&b, sizeof *fs);
1167 ds_put_format(string, " ***%td leftover bytes at end***", b.size);
1171 length = ntohs(fs->length);
1172 if (length < sizeof *fs) {
1173 ds_put_format(string, " ***nx_flow_stats claims length %zu***",
1178 match_len = ntohs(fs->match_len);
1179 if (match_len > length - sizeof *fs) {
1180 ds_put_format(string, " ***length=%zu match_len=%d***",
1185 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1186 ntohll(fs->cookie));
1187 ofp_print_duration(string, ntohl(fs->duration_sec),
1188 ntohl(fs->duration_nsec));
1189 ds_put_format(string, ", table_id=%"PRIu8", ", fs->table_id);
1190 ds_put_format(string, "n_packets=%"PRIu64", ",
1191 ntohll(fs->packet_count));
1192 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
1193 if (fs->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
1194 ds_put_format(string, "idle_timeout=%"PRIu16",",
1195 ntohs(fs->idle_timeout));
1197 if (fs->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
1198 ds_put_format(string, "hard_timeout=%"PRIu16",",
1199 ntohs(fs->hard_timeout));
1202 error = nx_pull_match(&b, match_len, ntohs(fs->priority), &rule);
1204 ofp_print_error(string, error);
1208 actions_len = length - sizeof *fs - ROUND_UP(match_len, 8);
1209 error = ofputil_pull_actions(&b, actions_len, &actions, &n_actions);
1211 ofp_print_error(string, error);
1215 cls_rule_format(&rule, string);
1216 ds_put_char(string, ' ');
1217 ofp_print_actions(string, (const struct ofp_action_header *) actions,
1218 n_actions * sizeof *actions);
1223 ofp_print_ofp_aggregate_stats_reply (
1224 struct ds *string, const struct ofp_aggregate_stats_reply *asr)
1226 ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
1227 ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
1228 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1232 ofp_print_ofpst_aggregate_reply(struct ds *string, const struct ofp_header *oh)
1234 ofp_print_ofp_aggregate_stats_reply(string, ofputil_stats_body(oh));
1238 ofp_print_nxst_aggregate_reply(struct ds *string,
1239 const struct nx_aggregate_stats_reply *nasr)
1241 ofp_print_ofp_aggregate_stats_reply(string, &nasr->asr);
1244 static void print_port_stat(struct ds *string, const char *leader,
1245 uint64_t stat, int more)
1247 ds_put_cstr(string, leader);
1249 ds_put_format(string, "%"PRIu64, stat);
1251 ds_put_char(string, '?');
1254 ds_put_cstr(string, ", ");
1256 ds_put_cstr(string, "\n");
1261 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1263 const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
1264 ds_put_format(string, " port_no=%"PRIu16, ntohs(psr->port_no));
1268 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1271 const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1272 size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
1273 ds_put_format(string, " %zu ports\n", n);
1274 if (verbosity < 1) {
1279 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1281 ds_put_cstr(string, "rx ");
1282 print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
1283 print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
1284 print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
1285 print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
1286 print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
1287 print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
1288 print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
1290 ds_put_cstr(string, " tx ");
1291 print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
1292 print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
1293 print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
1294 print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
1295 print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
1300 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1303 const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1304 size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
1305 ds_put_format(string, " %zu tables\n", n);
1306 if (verbosity < 1) {
1311 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1312 strncpy(name, ts->name, sizeof name);
1313 name[OFP_MAX_TABLE_NAME_LEN] = '\0';
1315 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1316 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1317 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1318 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1319 ds_put_cstr(string, " ");
1320 ds_put_format(string, "lookup=%"PRIu64", ",
1321 ntohll(ts->lookup_count));
1322 ds_put_format(string, "matched=%"PRIu64"\n",
1323 ntohll(ts->matched_count));
1328 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1330 if (queue_id == OFPQ_ALL) {
1331 ds_put_cstr(string, "ALL");
1333 ds_put_format(string, "%"PRIu32, queue_id);
1338 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1340 const struct ofp_queue_stats_request *qsr = ofputil_stats_body(oh);
1342 ds_put_cstr(string, "port=");
1343 ofp_print_port_name(string, ntohs(qsr->port_no));
1345 ds_put_cstr(string, " queue=");
1346 ofp_print_queue_name(string, ntohl(qsr->queue_id));
1350 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1353 const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1354 size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
1355 ds_put_format(string, " %zu queues\n", n);
1356 if (verbosity < 1) {
1361 ds_put_cstr(string, " port ");
1362 ofp_print_port_name(string, ntohs(qs->port_no));
1363 ds_put_cstr(string, " queue ");
1364 ofp_print_queue_name(string, ntohl(qs->queue_id));
1365 ds_put_cstr(string, ": ");
1367 print_port_stat(string, "bytes=", ntohll(qs->tx_bytes), 1);
1368 print_port_stat(string, "pkts=", ntohll(qs->tx_packets), 1);
1369 print_port_stat(string, "errors=", ntohll(qs->tx_errors), 0);
1374 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1376 const struct ofp_stats_request *srq
1377 = (const struct ofp_stats_request *) oh;
1380 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1386 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1388 const struct ofp_stats_reply *srp = (const struct ofp_stats_reply *) oh;
1391 uint16_t flags = ntohs(srp->flags);
1393 ds_put_cstr(string, " flags=");
1394 if (flags & OFPSF_REPLY_MORE) {
1395 ds_put_cstr(string, "[more]");
1396 flags &= ~OFPSF_REPLY_MORE;
1399 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1406 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1408 size_t len = ntohs(oh->length);
1410 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1411 if (verbosity > 1) {
1412 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1417 ofp_print_nxt_status_message(struct ds *string, const struct ofp_header *oh)
1421 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1422 ofpbuf_pull(&b, sizeof(struct nicira_header));
1423 ds_put_cstr(string, " \"");
1424 ds_put_printable(string, b.data, b.size);
1425 ds_put_char(string, '"');
1429 ofp_print_nxt_tun_id_from_cookie(struct ds *string,
1430 const struct nxt_tun_id_cookie *ntic)
1432 ds_put_format(string, " set=%"PRIu8, ntic->set);
1436 ofp_print_nxt_role_message(struct ds *string,
1437 const struct nx_role_request *nrr)
1439 unsigned int role = ntohl(nrr->role);
1441 ds_put_cstr(string, " role=");
1442 if (role == NX_ROLE_OTHER) {
1443 ds_put_cstr(string, "other");
1444 } else if (role == NX_ROLE_MASTER) {
1445 ds_put_cstr(string, "master");
1446 } else if (role == NX_ROLE_SLAVE) {
1447 ds_put_cstr(string, "slave");
1449 ds_put_format(string, "%u", role);
1454 ofp_print_nxt_set_flow_format(struct ds *string,
1455 const struct nxt_set_flow_format *nsff)
1457 uint32_t format = ntohl(nsff->format);
1459 ds_put_cstr(string, " format=");
1460 if (ofputil_flow_format_is_valid(format)) {
1461 ds_put_cstr(string, ofputil_flow_format_to_string(format));
1463 ds_put_format(string, "%"PRIu32, format);
1468 ofp_to_string__(const struct ofp_header *oh,
1469 const struct ofputil_msg_type *type, struct ds *string,
1472 enum ofputil_msg_code code;
1473 const void *msg = oh;
1475 ds_put_format(string, "%s (xid=0x%"PRIx32"):",
1476 ofputil_msg_type_name(type), ntohl(oh->xid));
1478 code = ofputil_msg_type_code(type);
1480 case OFPUTIL_INVALID:
1483 case OFPUTIL_OFPT_HELLO:
1484 ds_put_char(string, '\n');
1485 ds_put_hex_dump(string, oh + 1, ntohs(oh->length) - sizeof *oh,
1489 case OFPUTIL_OFPT_ERROR:
1490 ofp_print_error_msg(string, msg);
1493 case OFPUTIL_OFPT_ECHO_REQUEST:
1494 case OFPUTIL_OFPT_ECHO_REPLY:
1495 ofp_print_echo(string, oh, verbosity);
1498 case OFPUTIL_OFPT_FEATURES_REQUEST:
1501 case OFPUTIL_OFPT_FEATURES_REPLY:
1502 ofp_print_switch_features(string, msg);
1505 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1508 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1509 case OFPUTIL_OFPT_SET_CONFIG:
1510 ofp_print_switch_config(string, msg);
1513 case OFPUTIL_OFPT_PACKET_IN:
1514 ofp_print_packet_in(string, msg, verbosity);
1517 case OFPUTIL_OFPT_FLOW_REMOVED:
1518 case OFPUTIL_NXT_FLOW_REMOVED:
1519 ofp_print_flow_removed(string, msg);
1522 case OFPUTIL_OFPT_PORT_STATUS:
1523 ofp_print_port_status(string, msg);
1526 case OFPUTIL_OFPT_PACKET_OUT:
1527 ofp_print_packet_out(string, msg, verbosity);
1530 case OFPUTIL_OFPT_FLOW_MOD:
1531 ofp_print_flow_mod(string, msg, code, verbosity);
1534 case OFPUTIL_OFPT_PORT_MOD:
1535 ofp_print_port_mod(string, msg);
1538 case OFPUTIL_OFPT_BARRIER_REQUEST:
1539 case OFPUTIL_OFPT_BARRIER_REPLY:
1542 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1543 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1547 case OFPUTIL_OFPST_DESC_REQUEST:
1548 ofp_print_stats_request(string, oh);
1551 case OFPUTIL_OFPST_FLOW_REQUEST:
1552 case OFPUTIL_NXST_FLOW_REQUEST:
1553 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1554 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1555 ofp_print_stats_request(string, oh);
1556 ofp_print_flow_stats_request(string, oh);
1559 case OFPUTIL_OFPST_TABLE_REQUEST:
1560 ofp_print_stats_request(string, oh);
1563 case OFPUTIL_OFPST_PORT_REQUEST:
1564 ofp_print_stats_request(string, oh);
1565 ofp_print_ofpst_port_request(string, oh);
1568 case OFPUTIL_OFPST_QUEUE_REQUEST:
1569 ofp_print_stats_request(string, oh);
1570 ofp_print_ofpst_queue_request(string, oh);
1573 case OFPUTIL_OFPST_DESC_REPLY:
1574 ofp_print_stats_reply(string, oh);
1575 ofp_print_ofpst_desc_reply(string, oh);
1578 case OFPUTIL_OFPST_FLOW_REPLY:
1579 ofp_print_stats_reply(string, oh);
1580 ofp_print_ofpst_flow_reply(string, oh, verbosity);
1583 case OFPUTIL_OFPST_QUEUE_REPLY:
1584 ofp_print_stats_reply(string, oh);
1585 ofp_print_ofpst_queue_reply(string, oh, verbosity);
1588 case OFPUTIL_OFPST_PORT_REPLY:
1589 ofp_print_stats_reply(string, oh);
1590 ofp_print_ofpst_port_reply(string, oh, verbosity);
1593 case OFPUTIL_OFPST_TABLE_REPLY:
1594 ofp_print_stats_reply(string, oh);
1595 ofp_print_ofpst_table_reply(string, oh, verbosity);
1598 case OFPUTIL_OFPST_AGGREGATE_REPLY:
1599 ofp_print_stats_reply(string, oh);
1600 ofp_print_ofpst_aggregate_reply(string, oh);
1603 case OFPUTIL_NXT_STATUS_REQUEST:
1604 case OFPUTIL_NXT_STATUS_REPLY:
1605 ofp_print_nxt_status_message(string, oh);
1608 case OFPUTIL_NXT_TUN_ID_FROM_COOKIE:
1609 ofp_print_nxt_tun_id_from_cookie(string, msg);
1612 case OFPUTIL_NXT_ROLE_REQUEST:
1613 case OFPUTIL_NXT_ROLE_REPLY:
1614 ofp_print_nxt_role_message(string, msg);
1617 case OFPUTIL_NXT_SET_FLOW_FORMAT:
1618 ofp_print_nxt_set_flow_format(string, msg);
1621 case OFPUTIL_NXT_FLOW_MOD:
1622 ofp_print_flow_mod(string, msg, code, verbosity);
1625 case OFPUTIL_NXST_FLOW_REPLY:
1626 ofp_print_nxst_flow_reply(string, oh);
1629 case OFPUTIL_NXST_AGGREGATE_REPLY:
1630 ofp_print_stats_reply(string, oh);
1631 ofp_print_nxst_aggregate_reply(string, msg);
1636 /* Composes and returns a string representing the OpenFlow packet of 'len'
1637 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1638 * verbosity and higher numbers increase verbosity. The caller is responsible
1639 * for freeing the string. */
1641 ofp_to_string(const void *oh_, size_t len, int verbosity)
1643 struct ds string = DS_EMPTY_INITIALIZER;
1644 const struct ofp_header *oh = oh_;
1647 ds_put_cstr(&string, "OpenFlow message is empty\n");
1648 } else if (len < sizeof(struct ofp_header)) {
1649 ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
1651 } else if (oh->version != OFP_VERSION) {
1652 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n",
1654 } else if (ntohs(oh->length) > len) {
1655 ds_put_format(&string,
1656 "(***truncated to %zu bytes from %"PRIu16"***)\n",
1657 len, ntohs(oh->length));
1658 } else if (ntohs(oh->length) < len) {
1659 ds_put_format(&string,
1660 "(***only uses %"PRIu16" bytes out of %zu***)\n",
1661 ntohs(oh->length), len);
1663 const struct ofputil_msg_type *type;
1666 error = ofputil_decode_msg_type(oh, &type);
1668 ofp_to_string__(oh, type, &string, verbosity);
1669 if (verbosity >= 5) {
1670 if (ds_last(&string) != '\n') {
1671 ds_put_char(&string, '\n');
1673 ds_put_hex_dump(&string, oh, len, 0, true);
1675 if (ds_last(&string) != '\n') {
1676 ds_put_char(&string, '\n');
1678 return ds_steal_cstr(&string);
1681 ofp_print_error(&string, error);
1683 ds_put_hex_dump(&string, oh, len, 0, true);
1684 return ds_steal_cstr(&string);
1687 /* Returns the name for the specified OpenFlow message type as a string,
1688 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1689 * hex number, e.g. "0x55".
1691 * The caller must free the returned string when it is no longer needed. */
1693 ofp_message_type_to_string(uint8_t type)
1704 case OFPT_ECHO_REQUEST:
1705 name = "ECHO_REQUEST";
1707 case OFPT_ECHO_REPLY:
1708 name = "ECHO_REPLY";
1713 case OFPT_FEATURES_REQUEST:
1714 name = "FEATURES_REQUEST";
1716 case OFPT_FEATURES_REPLY:
1717 name = "FEATURES_REPLY";
1719 case OFPT_GET_CONFIG_REQUEST:
1720 name = "GET_CONFIG_REQUEST";
1722 case OFPT_GET_CONFIG_REPLY:
1723 name = "GET_CONFIG_REPLY";
1725 case OFPT_SET_CONFIG:
1726 name = "SET_CONFIG";
1728 case OFPT_PACKET_IN:
1731 case OFPT_FLOW_REMOVED:
1732 name = "FLOW_REMOVED";
1734 case OFPT_PORT_STATUS:
1735 name = "PORT_STATUS";
1737 case OFPT_PACKET_OUT:
1738 name = "PACKET_OUT";
1746 case OFPT_STATS_REQUEST:
1747 name = "STATS_REQUEST";
1749 case OFPT_STATS_REPLY:
1750 name = "STATS_REPLY";
1752 case OFPT_BARRIER_REQUEST:
1753 name = "BARRIER_REQUEST";
1755 case OFPT_BARRIER_REPLY:
1756 name = "BARRIER_REPLY";
1758 case OFPT_QUEUE_GET_CONFIG_REQUEST:
1759 name = "QUEUE_GET_CONFIG_REQUEST";
1761 case OFPT_QUEUE_GET_CONFIG_REPLY:
1762 name = "QUEUE_GET_CONFIG_REPLY";
1769 return name ? xasprintf("OFPT_%s", name) : xasprintf("0x%02"PRIx8, type);
1773 print_and_free(FILE *stream, char *string)
1775 fputs(string, stream);
1779 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1780 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1781 * numbers increase verbosity. */
1783 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1785 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1788 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1789 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1790 * the Ethernet frame (of which 'len' bytes were captured).
1792 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1794 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1796 print_and_free(stream, ofp_packet_to_string(data, len, total_len));