2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-print.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
30 #include "byte-order.h"
32 #include "dynamic-string.h"
35 #include "multipath.h"
36 #include "meta-flow.h"
39 #include "ofp-actions.h"
40 #include "ofp-errors.h"
44 #include "openflow/openflow.h"
45 #include "openflow/nicira-ext.h"
47 #include "type-props.h"
48 #include "unaligned.h"
52 static void ofp_print_queue_name(struct ds *string, uint32_t port);
53 static void ofp_print_error(struct ds *, enum ofperr);
56 /* Returns a string that represents the contents of the Ethernet frame in the
57 * 'len' bytes starting at 'data'. The caller must free the returned string.*/
59 ofp_packet_to_string(const void *data, size_t len)
61 struct ds ds = DS_EMPTY_INITIALIZER;
62 const struct pkt_metadata md = PKT_METADATA_INITIALIZER(0);
67 ofpbuf_use_const(&buf, data, len);
68 flow_extract(&buf, &md, &flow);
69 flow_format(&ds, &flow);
71 l4_size = ofpbuf_l4_size(&buf);
73 if (flow.nw_proto == IPPROTO_TCP && l4_size >= TCP_HEADER_LEN) {
74 struct tcp_header *th = ofpbuf_l4(&buf);
75 ds_put_format(&ds, " tcp_csum:%"PRIx16, ntohs(th->tcp_csum));
76 } else if (flow.nw_proto == IPPROTO_UDP && l4_size >= UDP_HEADER_LEN) {
77 struct udp_header *uh = ofpbuf_l4(&buf);
78 ds_put_format(&ds, " udp_csum:%"PRIx16, ntohs(uh->udp_csum));
79 } else if (flow.nw_proto == IPPROTO_SCTP && l4_size >= SCTP_HEADER_LEN) {
80 struct sctp_header *sh = ofpbuf_l4(&buf);
81 ds_put_format(&ds, " sctp_csum:%"PRIx32,
82 ntohl(get_16aligned_be32(&sh->sctp_csum)));
85 ds_put_char(&ds, '\n');
91 ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
94 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
95 struct ofputil_packet_in pin;
99 error = ofputil_decode_packet_in(&pin, oh);
101 ofp_print_error(string, error);
106 ds_put_format(string, " table_id=%"PRIu8, pin.table_id);
109 if (pin.cookie != OVS_BE64_MAX) {
110 ds_put_format(string, " cookie=0x%"PRIx64, ntohll(pin.cookie));
113 ds_put_format(string, " total_len=%"PRIuSIZE" in_port=", pin.total_len);
114 ofputil_format_port(pin.fmd.in_port, string);
116 if (pin.fmd.tun_id != htonll(0)) {
117 ds_put_format(string, " tun_id=0x%"PRIx64, ntohll(pin.fmd.tun_id));
120 if (pin.fmd.tun_src != htonl(0)) {
121 ds_put_format(string, " tun_src="IP_FMT, IP_ARGS(pin.fmd.tun_src));
124 if (pin.fmd.tun_dst != htonl(0)) {
125 ds_put_format(string, " tun_dst="IP_FMT, IP_ARGS(pin.fmd.tun_dst));
128 if (pin.fmd.metadata != htonll(0)) {
129 ds_put_format(string, " metadata=0x%"PRIx64, ntohll(pin.fmd.metadata));
132 for (i = 0; i < FLOW_N_REGS; i++) {
133 if (pin.fmd.regs[i]) {
134 ds_put_format(string, " reg%d=0x%"PRIx32, i, pin.fmd.regs[i]);
138 if (pin.fmd.pkt_mark != 0) {
139 ds_put_format(string, " pkt_mark=0x%"PRIx32, pin.fmd.pkt_mark);
142 ds_put_format(string, " (via %s)",
143 ofputil_packet_in_reason_to_string(pin.reason, reasonbuf,
146 ds_put_format(string, " data_len=%"PRIuSIZE, pin.packet_len);
147 if (pin.buffer_id == UINT32_MAX) {
148 ds_put_format(string, " (unbuffered)");
149 if (pin.total_len != pin.packet_len) {
150 ds_put_format(string, " (***total_len != data_len***)");
153 ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
154 if (pin.total_len < pin.packet_len) {
155 ds_put_format(string, " (***total_len < data_len***)");
158 ds_put_char(string, '\n');
161 char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
162 ds_put_cstr(string, packet);
166 ds_put_hex_dump(string, pin.packet, pin.packet_len, 0, false);
171 ofp_print_packet_out(struct ds *string, const struct ofp_header *oh,
174 struct ofputil_packet_out po;
175 struct ofpbuf ofpacts;
178 ofpbuf_init(&ofpacts, 64);
179 error = ofputil_decode_packet_out(&po, oh, &ofpacts);
181 ofpbuf_uninit(&ofpacts);
182 ofp_print_error(string, error);
186 ds_put_cstr(string, " in_port=");
187 ofputil_format_port(po.in_port, string);
189 ds_put_cstr(string, " actions=");
190 ofpacts_format(po.ofpacts, po.ofpacts_len, string);
192 if (po.buffer_id == UINT32_MAX) {
193 ds_put_format(string, " data_len=%"PRIuSIZE, po.packet_len);
194 if (verbosity > 0 && po.packet_len > 0) {
195 char *packet = ofp_packet_to_string(po.packet, po.packet_len);
196 ds_put_char(string, '\n');
197 ds_put_cstr(string, packet);
201 ds_put_hex_dump(string, po.packet, po.packet_len, 0, false);
204 ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id);
207 ofpbuf_uninit(&ofpacts);
210 /* qsort comparison function. */
212 compare_ports(const void *a_, const void *b_)
214 const struct ofputil_phy_port *a = a_;
215 const struct ofputil_phy_port *b = b_;
216 uint16_t ap = ofp_to_u16(a->port_no);
217 uint16_t bp = ofp_to_u16(b->port_no);
219 return ap < bp ? -1 : ap > bp;
223 ofp_print_bit_names(struct ds *string, uint32_t bits,
224 const char *(*bit_to_name)(uint32_t bit),
231 ds_put_cstr(string, "0");
235 for (i = 0; i < 32; i++) {
236 uint32_t bit = UINT32_C(1) << i;
239 const char *name = bit_to_name(bit);
242 ds_put_char(string, separator);
244 ds_put_cstr(string, name);
252 ds_put_char(string, separator);
254 ds_put_format(string, "0x%"PRIx32, bits);
259 netdev_feature_to_name(uint32_t bit)
261 enum netdev_features f = bit;
264 case NETDEV_F_10MB_HD: return "10MB-HD";
265 case NETDEV_F_10MB_FD: return "10MB-FD";
266 case NETDEV_F_100MB_HD: return "100MB-HD";
267 case NETDEV_F_100MB_FD: return "100MB-FD";
268 case NETDEV_F_1GB_HD: return "1GB-HD";
269 case NETDEV_F_1GB_FD: return "1GB-FD";
270 case NETDEV_F_10GB_FD: return "10GB-FD";
271 case NETDEV_F_40GB_FD: return "40GB-FD";
272 case NETDEV_F_100GB_FD: return "100GB-FD";
273 case NETDEV_F_1TB_FD: return "1TB-FD";
274 case NETDEV_F_OTHER: return "OTHER";
275 case NETDEV_F_COPPER: return "COPPER";
276 case NETDEV_F_FIBER: return "FIBER";
277 case NETDEV_F_AUTONEG: return "AUTO_NEG";
278 case NETDEV_F_PAUSE: return "AUTO_PAUSE";
279 case NETDEV_F_PAUSE_ASYM: return "AUTO_PAUSE_ASYM";
286 ofp_print_port_features(struct ds *string, enum netdev_features features)
288 ofp_print_bit_names(string, features, netdev_feature_to_name, ' ');
289 ds_put_char(string, '\n');
293 ofputil_port_config_to_name(uint32_t bit)
295 enum ofputil_port_config pc = bit;
298 case OFPUTIL_PC_PORT_DOWN: return "PORT_DOWN";
299 case OFPUTIL_PC_NO_STP: return "NO_STP";
300 case OFPUTIL_PC_NO_RECV: return "NO_RECV";
301 case OFPUTIL_PC_NO_RECV_STP: return "NO_RECV_STP";
302 case OFPUTIL_PC_NO_FLOOD: return "NO_FLOOD";
303 case OFPUTIL_PC_NO_FWD: return "NO_FWD";
304 case OFPUTIL_PC_NO_PACKET_IN: return "NO_PACKET_IN";
311 ofp_print_port_config(struct ds *string, enum ofputil_port_config config)
313 ofp_print_bit_names(string, config, ofputil_port_config_to_name, ' ');
314 ds_put_char(string, '\n');
318 ofputil_port_state_to_name(uint32_t bit)
320 enum ofputil_port_state ps = bit;
323 case OFPUTIL_PS_LINK_DOWN: return "LINK_DOWN";
324 case OFPUTIL_PS_BLOCKED: return "BLOCKED";
325 case OFPUTIL_PS_LIVE: return "LIVE";
327 case OFPUTIL_PS_STP_LISTEN:
328 case OFPUTIL_PS_STP_LEARN:
329 case OFPUTIL_PS_STP_FORWARD:
330 case OFPUTIL_PS_STP_BLOCK:
331 /* Handled elsewhere. */
339 ofp_print_port_state(struct ds *string, enum ofputil_port_state state)
341 enum ofputil_port_state stp_state;
343 /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
344 * pattern. We have to special case it.
346 * OVS doesn't support STP, so this field will always be 0 if we are
347 * talking to OVS, so we'd always print STP_LISTEN in that case.
348 * Therefore, we don't print anything at all if the value is STP_LISTEN, to
349 * avoid confusing users. */
350 stp_state = state & OFPUTIL_PS_STP_MASK;
353 (stp_state == OFPUTIL_PS_STP_LEARN ? "STP_LEARN"
354 : stp_state == OFPUTIL_PS_STP_FORWARD ? "STP_FORWARD"
356 state &= ~OFPUTIL_PS_STP_MASK;
358 ofp_print_bit_names(string, state, ofputil_port_state_to_name,
362 ofp_print_bit_names(string, state, ofputil_port_state_to_name, ' ');
364 ds_put_char(string, '\n');
368 ofp_print_phy_port(struct ds *string, const struct ofputil_phy_port *port)
370 char name[sizeof port->name];
373 memcpy(name, port->name, sizeof name);
374 for (j = 0; j < sizeof name - 1; j++) {
375 if (!isprint((unsigned char) name[j])) {
381 ds_put_char(string, ' ');
382 ofputil_format_port(port->port_no, string);
383 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
384 name, ETH_ADDR_ARGS(port->hw_addr));
386 ds_put_cstr(string, " config: ");
387 ofp_print_port_config(string, port->config);
389 ds_put_cstr(string, " state: ");
390 ofp_print_port_state(string, port->state);
393 ds_put_format(string, " current: ");
394 ofp_print_port_features(string, port->curr);
396 if (port->advertised) {
397 ds_put_format(string, " advertised: ");
398 ofp_print_port_features(string, port->advertised);
400 if (port->supported) {
401 ds_put_format(string, " supported: ");
402 ofp_print_port_features(string, port->supported);
405 ds_put_format(string, " peer: ");
406 ofp_print_port_features(string, port->peer);
408 ds_put_format(string, " speed: %"PRIu32" Mbps now, "
409 "%"PRIu32" Mbps max\n",
410 port->curr_speed / UINT32_C(1000),
411 port->max_speed / UINT32_C(1000));
414 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
415 * 'ofp_version', writes a detailed description of each port into
418 ofp_print_phy_ports(struct ds *string, uint8_t ofp_version,
421 struct ofputil_phy_port *ports;
422 size_t allocated_ports, n_ports;
428 for (n_ports = 0; ; n_ports++) {
429 if (n_ports >= allocated_ports) {
430 ports = x2nrealloc(ports, &allocated_ports, sizeof *ports);
433 retval = ofputil_pull_phy_port(ofp_version, b, &ports[n_ports]);
439 qsort(ports, n_ports, sizeof *ports, compare_ports);
440 for (i = 0; i < n_ports; i++) {
441 ofp_print_phy_port(string, &ports[i]);
446 ofp_print_error(string, retval);
451 ofputil_capabilities_to_name(uint32_t bit)
453 enum ofputil_capabilities capabilities = bit;
455 switch (capabilities) {
456 case OFPUTIL_C_FLOW_STATS: return "FLOW_STATS";
457 case OFPUTIL_C_TABLE_STATS: return "TABLE_STATS";
458 case OFPUTIL_C_PORT_STATS: return "PORT_STATS";
459 case OFPUTIL_C_IP_REASM: return "IP_REASM";
460 case OFPUTIL_C_QUEUE_STATS: return "QUEUE_STATS";
461 case OFPUTIL_C_ARP_MATCH_IP: return "ARP_MATCH_IP";
462 case OFPUTIL_C_STP: return "STP";
463 case OFPUTIL_C_GROUP_STATS: return "GROUP_STATS";
464 case OFPUTIL_C_PORT_BLOCKED: return "PORT_BLOCKED";
471 ofputil_action_bitmap_to_name(uint32_t bit)
473 enum ofputil_action_bitmap action = bit;
476 case OFPUTIL_A_OUTPUT: return "OUTPUT";
477 case OFPUTIL_A_SET_VLAN_VID: return "SET_VLAN_VID";
478 case OFPUTIL_A_SET_VLAN_PCP: return "SET_VLAN_PCP";
479 case OFPUTIL_A_STRIP_VLAN: return "STRIP_VLAN";
480 case OFPUTIL_A_SET_DL_SRC: return "SET_DL_SRC";
481 case OFPUTIL_A_SET_DL_DST: return "SET_DL_DST";
482 case OFPUTIL_A_SET_NW_SRC: return "SET_NW_SRC";
483 case OFPUTIL_A_SET_NW_DST: return "SET_NW_DST";
484 case OFPUTIL_A_SET_NW_ECN: return "SET_NW_ECN";
485 case OFPUTIL_A_SET_NW_TOS: return "SET_NW_TOS";
486 case OFPUTIL_A_SET_TP_SRC: return "SET_TP_SRC";
487 case OFPUTIL_A_SET_TP_DST: return "SET_TP_DST";
488 case OFPUTIL_A_SET_FIELD: return "SET_FIELD";
489 case OFPUTIL_A_ENQUEUE: return "ENQUEUE";
490 case OFPUTIL_A_COPY_TTL_OUT: return "COPY_TTL_OUT";
491 case OFPUTIL_A_COPY_TTL_IN: return "COPY_TTL_IN";
492 case OFPUTIL_A_SET_MPLS_LABEL: return "SET_MPLS_LABEL";
493 case OFPUTIL_A_SET_MPLS_TC: return "SET_MPLS_TC";
494 case OFPUTIL_A_SET_MPLS_TTL: return "SET_MPLS_TTL";
495 case OFPUTIL_A_DEC_MPLS_TTL: return "DEC_MPLS_TTL";
496 case OFPUTIL_A_PUSH_VLAN: return "PUSH_VLAN";
497 case OFPUTIL_A_POP_VLAN: return "POP_VLAN";
498 case OFPUTIL_A_PUSH_MPLS: return "PUSH_MPLS";
499 case OFPUTIL_A_POP_MPLS: return "POP_MPLS";
500 case OFPUTIL_A_SET_QUEUE: return "SET_QUEUE";
501 case OFPUTIL_A_GROUP: return "GROUP";
502 case OFPUTIL_A_SET_NW_TTL: return "SET_NW_TTL";
503 case OFPUTIL_A_DEC_NW_TTL: return "DEC_NW_TTL";
510 ofp_print_switch_features(struct ds *string, const struct ofp_header *oh)
512 struct ofputil_switch_features features;
516 error = ofputil_decode_switch_features(oh, &features, &b);
518 ofp_print_error(string, error);
522 ds_put_format(string, " dpid:%016"PRIx64"\n", features.datapath_id);
524 ds_put_format(string, "n_tables:%"PRIu8", n_buffers:%"PRIu32,
525 features.n_tables, features.n_buffers);
526 if (features.auxiliary_id) {
527 ds_put_format(string, ", auxiliary_id:%"PRIu8, features.auxiliary_id);
529 ds_put_char(string, '\n');
531 ds_put_cstr(string, "capabilities: ");
532 ofp_print_bit_names(string, features.capabilities,
533 ofputil_capabilities_to_name, ' ');
534 ds_put_char(string, '\n');
536 switch ((enum ofp_version)oh->version) {
538 ds_put_cstr(string, "actions: ");
539 ofp_print_bit_names(string, features.actions,
540 ofputil_action_bitmap_to_name, ' ');
541 ds_put_char(string, '\n');
549 return; /* no ports in ofp13_switch_features */
554 ofp_print_phy_ports(string, oh->version, &b);
558 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
560 enum ofp_config_flags flags;
562 flags = ntohs(osc->flags);
564 ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
565 flags &= ~OFPC_FRAG_MASK;
567 if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) {
568 ds_put_format(string, " invalid_ttl_to_controller");
569 flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER;
573 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
576 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
579 static void print_wild(struct ds *string, const char *leader, int is_wild,
580 int verbosity, const char *format, ...)
583 static void print_wild(struct ds *string, const char *leader, int is_wild,
584 int verbosity, const char *format, ...)
586 if (is_wild && verbosity < 2) {
589 ds_put_cstr(string, leader);
593 va_start(args, format);
594 ds_put_format_valist(string, format, args);
597 ds_put_char(string, '*');
599 ds_put_char(string, ',');
603 print_wild_port(struct ds *string, const char *leader, int is_wild,
604 int verbosity, ofp_port_t port)
606 if (is_wild && verbosity < 2) {
609 ds_put_cstr(string, leader);
611 ofputil_format_port(port, string);
613 ds_put_char(string, '*');
615 ds_put_char(string, ',');
619 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
620 uint32_t wild_bits, int verbosity)
622 if (wild_bits >= 32 && verbosity < 2) {
625 ds_put_cstr(string, leader);
626 if (wild_bits < 32) {
627 ds_put_format(string, IP_FMT, IP_ARGS(ip));
629 ds_put_format(string, "/%d", 32 - wild_bits);
632 ds_put_char(string, '*');
634 ds_put_char(string, ',');
638 ofp10_match_print(struct ds *f, const struct ofp10_match *om, int verbosity)
640 char *s = ofp10_match_to_string(om, verbosity);
646 ofp10_match_to_string(const struct ofp10_match *om, int verbosity)
648 struct ds f = DS_EMPTY_INITIALIZER;
649 uint32_t w = ntohl(om->wildcards);
650 bool skip_type = false;
651 bool skip_proto = false;
653 if (!(w & OFPFW10_DL_TYPE)) {
655 if (om->dl_type == htons(ETH_TYPE_IP)) {
656 if (!(w & OFPFW10_NW_PROTO)) {
658 if (om->nw_proto == IPPROTO_ICMP) {
659 ds_put_cstr(&f, "icmp,");
660 } else if (om->nw_proto == IPPROTO_TCP) {
661 ds_put_cstr(&f, "tcp,");
662 } else if (om->nw_proto == IPPROTO_UDP) {
663 ds_put_cstr(&f, "udp,");
664 } else if (om->nw_proto == IPPROTO_SCTP) {
665 ds_put_cstr(&f, "sctp,");
667 ds_put_cstr(&f, "ip,");
671 ds_put_cstr(&f, "ip,");
673 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
674 ds_put_cstr(&f, "arp,");
675 } else if (om->dl_type == htons(ETH_TYPE_RARP)){
676 ds_put_cstr(&f, "rarp,");
677 } else if (om->dl_type == htons(ETH_TYPE_MPLS)) {
678 ds_put_cstr(&f, "mpls,");
679 } else if (om->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
680 ds_put_cstr(&f, "mplsm,");
685 print_wild_port(&f, "in_port=", w & OFPFW10_IN_PORT, verbosity,
686 u16_to_ofp(ntohs(om->in_port)));
687 print_wild(&f, "dl_vlan=", w & OFPFW10_DL_VLAN, verbosity,
688 "%d", ntohs(om->dl_vlan));
689 print_wild(&f, "dl_vlan_pcp=", w & OFPFW10_DL_VLAN_PCP, verbosity,
690 "%d", om->dl_vlan_pcp);
691 print_wild(&f, "dl_src=", w & OFPFW10_DL_SRC, verbosity,
692 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
693 print_wild(&f, "dl_dst=", w & OFPFW10_DL_DST, verbosity,
694 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
696 print_wild(&f, "dl_type=", w & OFPFW10_DL_TYPE, verbosity,
697 "0x%04x", ntohs(om->dl_type));
699 print_ip_netmask(&f, "nw_src=", om->nw_src,
700 (w & OFPFW10_NW_SRC_MASK) >> OFPFW10_NW_SRC_SHIFT,
702 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
703 (w & OFPFW10_NW_DST_MASK) >> OFPFW10_NW_DST_SHIFT,
706 if (om->dl_type == htons(ETH_TYPE_ARP) ||
707 om->dl_type == htons(ETH_TYPE_RARP)) {
708 print_wild(&f, "arp_op=", w & OFPFW10_NW_PROTO, verbosity,
711 print_wild(&f, "nw_proto=", w & OFPFW10_NW_PROTO, verbosity,
715 print_wild(&f, "nw_tos=", w & OFPFW10_NW_TOS, verbosity,
717 if (om->nw_proto == IPPROTO_ICMP) {
718 print_wild(&f, "icmp_type=", w & OFPFW10_ICMP_TYPE, verbosity,
719 "%d", ntohs(om->tp_src));
720 print_wild(&f, "icmp_code=", w & OFPFW10_ICMP_CODE, verbosity,
721 "%d", ntohs(om->tp_dst));
723 print_wild(&f, "tp_src=", w & OFPFW10_TP_SRC, verbosity,
724 "%d", ntohs(om->tp_src));
725 print_wild(&f, "tp_dst=", w & OFPFW10_TP_DST, verbosity,
726 "%d", ntohs(om->tp_dst));
728 if (ds_last(&f) == ',') {
735 ofp_print_flow_flags(struct ds *s, enum ofputil_flow_mod_flags flags)
737 if (flags & OFPUTIL_FF_SEND_FLOW_REM) {
738 ds_put_cstr(s, "send_flow_rem ");
740 if (flags & OFPUTIL_FF_CHECK_OVERLAP) {
741 ds_put_cstr(s, "check_overlap ");
743 if (flags & OFPUTIL_FF_RESET_COUNTS) {
744 ds_put_cstr(s, "reset_counts ");
746 if (flags & OFPUTIL_FF_NO_PKT_COUNTS) {
747 ds_put_cstr(s, "no_packet_counts ");
749 if (flags & OFPUTIL_FF_NO_BYT_COUNTS) {
750 ds_put_cstr(s, "no_byte_counts ");
752 if (flags & OFPUTIL_FF_HIDDEN_FIELDS) {
753 ds_put_cstr(s, "allow_hidden_fields ");
755 if (flags & OFPUTIL_FF_NO_READONLY) {
756 ds_put_cstr(s, "no_readonly_table ");
761 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh, int verbosity)
763 struct ofputil_flow_mod fm;
764 struct ofpbuf ofpacts;
768 enum ofputil_protocol protocol;
770 protocol = ofputil_protocol_from_ofp_version(oh->version);
771 protocol = ofputil_protocol_set_tid(protocol, true);
773 ofpbuf_init(&ofpacts, 64);
774 error = ofputil_decode_flow_mod(&fm, oh, protocol, &ofpacts,
777 ofpbuf_uninit(&ofpacts);
778 ofp_print_error(s, error);
783 switch (fm.command) {
785 ds_put_cstr(s, "ADD");
788 ds_put_cstr(s, "MOD");
790 case OFPFC_MODIFY_STRICT:
791 ds_put_cstr(s, "MOD_STRICT");
794 ds_put_cstr(s, "DEL");
796 case OFPFC_DELETE_STRICT:
797 ds_put_cstr(s, "DEL_STRICT");
800 ds_put_format(s, "cmd:%d", fm.command);
802 if (fm.table_id != 0) {
803 ds_put_format(s, " table:%d", fm.table_id);
807 ofpraw_decode(&raw, oh);
808 if (verbosity >= 3 && raw == OFPRAW_OFPT10_FLOW_MOD) {
809 const struct ofp10_flow_mod *ofm = ofpmsg_body(oh);
810 ofp10_match_print(s, &ofm->match, verbosity);
812 /* ofp_print_match() doesn't print priority. */
813 need_priority = true;
814 } else if (verbosity >= 3 && raw == OFPRAW_NXT_FLOW_MOD) {
815 const struct nx_flow_mod *nfm = ofpmsg_body(oh);
816 const void *nxm = nfm + 1;
819 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
820 ds_put_cstr(s, nxm_s);
823 /* nx_match_to_string() doesn't print priority. */
824 need_priority = true;
826 match_format(&fm.match, s, fm.priority);
828 /* match_format() does print priority. */
829 need_priority = false;
832 if (ds_last(s) != ' ') {
835 if (fm.new_cookie != htonll(0) && fm.new_cookie != OVS_BE64_MAX) {
836 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.new_cookie));
838 if (fm.cookie_mask != htonll(0)) {
839 ds_put_format(s, "cookie:0x%"PRIx64"/0x%"PRIx64" ",
840 ntohll(fm.cookie), ntohll(fm.cookie_mask));
842 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
843 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
845 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
846 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
848 if (fm.priority != OFP_DEFAULT_PRIORITY && need_priority) {
849 ds_put_format(s, "pri:%"PRIu16" ", fm.priority);
851 if (fm.buffer_id != UINT32_MAX) {
852 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
854 if (fm.out_port != OFPP_ANY) {
855 ds_put_format(s, "out_port:");
856 ofputil_format_port(fm.out_port, s);
860 if (oh->version == OFP10_VERSION || oh->version == OFP11_VERSION) {
861 /* Don't print the reset_counts flag for OF1.0 and OF1.1 because those
862 * versions don't really have such a flag and printing one is likely to
864 fm.flags &= ~OFPUTIL_FF_RESET_COUNTS;
866 ofp_print_flow_flags(s, fm.flags);
868 ds_put_cstr(s, "actions=");
869 ofpacts_format(fm.ofpacts, fm.ofpacts_len, s);
870 ofpbuf_uninit(&ofpacts);
874 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
876 ds_put_format(string, "%u", sec);
878 /* If there are no fractional seconds, don't print any decimals.
880 * If the fractional seconds can be expressed exactly as milliseconds,
881 * print 3 decimals. Open vSwitch provides millisecond precision for most
882 * time measurements, so printing 3 decimals every time makes it easier to
883 * spot real changes in flow dumps that refresh themselves quickly.
885 * If the fractional seconds are more precise than milliseconds, print the
886 * number of decimals needed to express them exactly.
889 unsigned int msec = nsec / 1000000;
890 if (msec * 1000000 == nsec) {
891 ds_put_format(string, ".%03u", msec);
893 ds_put_format(string, ".%09u", nsec);
894 while (string->string[string->length - 1] == '0') {
899 ds_put_char(string, 's');
902 /* Returns a string form of 'reason'. The return value is either a statically
903 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
904 * 'bufsize' should be at least OFP_FLOW_REMOVED_REASON_BUFSIZE. */
905 #define OFP_FLOW_REMOVED_REASON_BUFSIZE (INT_STRLEN(int) + 1)
907 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason,
908 char *reasonbuf, size_t bufsize)
911 case OFPRR_IDLE_TIMEOUT:
913 case OFPRR_HARD_TIMEOUT:
917 case OFPRR_GROUP_DELETE:
918 return "group_delete";
921 case OFPRR_METER_DELETE:
922 return "meter_delete";
924 snprintf(reasonbuf, bufsize, "%d", (int) reason);
930 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
932 char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
933 struct ofputil_flow_removed fr;
936 error = ofputil_decode_flow_removed(&fr, oh);
938 ofp_print_error(string, error);
942 ds_put_char(string, ' ');
943 match_format(&fr.match, string, fr.priority);
945 ds_put_format(string, " reason=%s",
946 ofp_flow_removed_reason_to_string(fr.reason, reasonbuf,
949 if (fr.table_id != 255) {
950 ds_put_format(string, " table_id=%"PRIu8, fr.table_id);
953 if (fr.cookie != htonll(0)) {
954 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
956 ds_put_cstr(string, " duration");
957 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
958 ds_put_format(string, " idle%"PRIu16, fr.idle_timeout);
959 if (fr.hard_timeout) {
960 /* The hard timeout was only added in OF1.2, so only print it if it is
961 * actually in use to avoid gratuitous change to the formatting. */
962 ds_put_format(string, " hard%"PRIu16, fr.hard_timeout);
964 ds_put_format(string, " pkts%"PRIu64" bytes%"PRIu64"\n",
965 fr.packet_count, fr.byte_count);
969 ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
971 struct ofputil_port_mod pm;
974 error = ofputil_decode_port_mod(oh, &pm, true);
976 ofp_print_error(string, error);
980 ds_put_cstr(string, "port: ");
981 ofputil_format_port(pm.port_no, string);
982 ds_put_format(string, ": addr:"ETH_ADDR_FMT"\n",
983 ETH_ADDR_ARGS(pm.hw_addr));
985 ds_put_cstr(string, " config: ");
986 ofp_print_port_config(string, pm.config);
988 ds_put_cstr(string, " mask: ");
989 ofp_print_port_config(string, pm.mask);
991 ds_put_cstr(string, " advertise: ");
993 ofp_print_port_features(string, pm.advertise);
995 ds_put_cstr(string, "UNCHANGED\n");
1000 ofp_print_table_miss_config(struct ds *string, const uint32_t config)
1002 uint32_t table_miss_config = config & OFPTC11_TABLE_MISS_MASK;
1004 switch (table_miss_config) {
1005 case OFPTC11_TABLE_MISS_CONTROLLER:
1006 ds_put_cstr(string, "controller\n");
1008 case OFPTC11_TABLE_MISS_CONTINUE:
1009 ds_put_cstr(string, "continue\n");
1011 case OFPTC11_TABLE_MISS_DROP:
1012 ds_put_cstr(string, "drop\n");
1015 ds_put_cstr(string, "Unknown\n");
1021 ofp_print_table_mod(struct ds *string, const struct ofp_header *oh)
1023 struct ofputil_table_mod pm;
1026 error = ofputil_decode_table_mod(oh, &pm);
1028 ofp_print_error(string, error);
1032 if (pm.table_id == 0xff) {
1033 ds_put_cstr(string, " table_id: ALL_TABLES");
1035 ds_put_format(string, " table_id=%"PRIu8, pm.table_id);
1038 ds_put_cstr(string, ", flow_miss_config=");
1039 ofp_print_table_miss_config(string, pm.config);
1043 ofp_print_queue_get_config_request(struct ds *string,
1044 const struct ofp_header *oh)
1049 error = ofputil_decode_queue_get_config_request(oh, &port);
1051 ofp_print_error(string, error);
1055 ds_put_cstr(string, " port=");
1056 ofputil_format_port(port, string);
1060 print_queue_rate(struct ds *string, const char *name, unsigned int rate)
1063 ds_put_format(string, " %s:%u.%u%%", name, rate / 10, rate % 10);
1064 } else if (rate < UINT16_MAX) {
1065 ds_put_format(string, " %s:(disabled)", name);
1070 ofp_print_queue_get_config_reply(struct ds *string,
1071 const struct ofp_header *oh)
1077 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1078 error = ofputil_decode_queue_get_config_reply(&b, &port);
1080 ofp_print_error(string, error);
1084 ds_put_cstr(string, " port=");
1085 ofputil_format_port(port, string);
1086 ds_put_char(string, '\n');
1089 struct ofputil_queue_config queue;
1092 retval = ofputil_pull_queue_get_config_reply(&b, &queue);
1094 if (retval != EOF) {
1095 ofp_print_error(string, retval);
1100 ds_put_format(string, "queue %"PRIu32":", queue.queue_id);
1101 print_queue_rate(string, "min_rate", queue.min_rate);
1102 print_queue_rate(string, "max_rate", queue.max_rate);
1103 ds_put_char(string, '\n');
1108 ofp_print_meter_flags(struct ds *s, uint16_t flags)
1110 if (flags & OFPMF13_KBPS) {
1111 ds_put_cstr(s, "kbps ");
1113 if (flags & OFPMF13_PKTPS) {
1114 ds_put_cstr(s, "pktps ");
1116 if (flags & OFPMF13_BURST) {
1117 ds_put_cstr(s, "burst ");
1119 if (flags & OFPMF13_STATS) {
1120 ds_put_cstr(s, "stats ");
1123 flags &= ~(OFPMF13_KBPS | OFPMF13_PKTPS | OFPMF13_BURST | OFPMF13_STATS);
1125 ds_put_format(s, "flags:0x%"PRIx16" ", flags);
1130 ofp_print_meter_band(struct ds *s, uint16_t flags,
1131 const struct ofputil_meter_band *mb)
1133 ds_put_cstr(s, "\ntype=");
1136 ds_put_cstr(s, "drop");
1138 case OFPMBT13_DSCP_REMARK:
1139 ds_put_cstr(s, "dscp_remark");
1142 ds_put_format(s, "%u", mb->type);
1145 ds_put_format(s, " rate=%"PRIu32, mb->rate);
1147 if (flags & OFPMF13_BURST) {
1148 ds_put_format(s, " burst_size=%"PRIu32, mb->burst_size);
1150 if (mb->type == OFPMBT13_DSCP_REMARK) {
1151 ds_put_format(s, " prec_level=%"PRIu8, mb->prec_level);
1156 ofp_print_meter_stats(struct ds *s, const struct ofputil_meter_stats *ms)
1160 ds_put_format(s, "meter:%"PRIu32" ", ms->meter_id);
1161 ds_put_format(s, "flow_count:%"PRIu32" ", ms->flow_count);
1162 ds_put_format(s, "packet_in_count:%"PRIu64" ", ms->packet_in_count);
1163 ds_put_format(s, "byte_in_count:%"PRIu64" ", ms->byte_in_count);
1164 ds_put_cstr(s, "duration:");
1165 ofp_print_duration(s, ms->duration_sec, ms->duration_nsec);
1166 ds_put_char(s, ' ');
1168 ds_put_cstr(s, "bands:\n");
1169 for (i = 0; i < ms->n_bands; ++i) {
1170 ds_put_format(s, "%d: ", i);
1171 ds_put_format(s, "packet_count:%"PRIu64" ", ms->bands[i].packet_count);
1172 ds_put_format(s, "byte_count:%"PRIu64"\n", ms->bands[i].byte_count);
1177 ofp_print_meter_config(struct ds *s, const struct ofputil_meter_config *mc)
1181 ds_put_format(s, "meter=%"PRIu32" ", mc->meter_id);
1183 ofp_print_meter_flags(s, mc->flags);
1185 ds_put_cstr(s, "bands=");
1186 for (i = 0; i < mc->n_bands; ++i) {
1187 ofp_print_meter_band(s, mc->flags, &mc->bands[i]);
1189 ds_put_char(s, '\n');
1193 ofp_print_meter_mod(struct ds *s, const struct ofp_header *oh)
1195 struct ofputil_meter_mod mm;
1196 struct ofpbuf bands;
1199 ofpbuf_init(&bands, 64);
1200 error = ofputil_decode_meter_mod(oh, &mm, &bands);
1202 ofpbuf_uninit(&bands);
1203 ofp_print_error(s, error);
1207 switch (mm.command) {
1209 ds_put_cstr(s, " ADD ");
1211 case OFPMC13_MODIFY:
1212 ds_put_cstr(s, " MOD ");
1214 case OFPMC13_DELETE:
1215 ds_put_cstr(s, " DEL ");
1218 ds_put_format(s, " cmd:%d ", mm.command);
1221 ofp_print_meter_config(s, &mm.meter);
1222 ofpbuf_uninit(&bands);
1226 ofp_print_meter_stats_request(struct ds *s, const struct ofp_header *oh)
1230 ofputil_decode_meter_request(oh, &meter_id);
1232 ds_put_format(s, " meter=%"PRIu32, meter_id);
1236 ofputil_meter_capabilities_to_name(uint32_t bit)
1238 enum ofp13_meter_flags flag = bit;
1241 case OFPMF13_KBPS: return "kbps";
1242 case OFPMF13_PKTPS: return "pktps";
1243 case OFPMF13_BURST: return "burst";
1244 case OFPMF13_STATS: return "stats";
1251 ofputil_meter_band_types_to_name(uint32_t bit)
1254 case 1 << OFPMBT13_DROP: return "drop";
1255 case 1 << OFPMBT13_DSCP_REMARK: return "dscp_remark";
1262 ofp_print_meter_features_reply(struct ds *s, const struct ofp_header *oh)
1264 struct ofputil_meter_features mf;
1266 ofputil_decode_meter_features(oh, &mf);
1268 ds_put_format(s, "\nmax_meter:%"PRIu32, mf.max_meters);
1269 ds_put_format(s, " max_bands:%"PRIu8, mf.max_bands);
1270 ds_put_format(s, " max_color:%"PRIu8"\n", mf.max_color);
1272 ds_put_cstr(s, "band_types: ");
1273 ofp_print_bit_names(s, mf.band_types,
1274 ofputil_meter_band_types_to_name, ' ');
1275 ds_put_char(s, '\n');
1277 ds_put_cstr(s, "capabilities: ");
1278 ofp_print_bit_names(s, mf.capabilities,
1279 ofputil_meter_capabilities_to_name, ' ');
1280 ds_put_char(s, '\n');
1284 ofp_print_meter_config_reply(struct ds *s, const struct ofp_header *oh)
1286 struct ofpbuf bands;
1289 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1290 ofpbuf_init(&bands, 64);
1292 struct ofputil_meter_config mc;
1295 retval = ofputil_decode_meter_config(&b, &mc, &bands);
1297 if (retval != EOF) {
1298 ofp_print_error(s, retval);
1302 ds_put_char(s, '\n');
1303 ofp_print_meter_config(s, &mc);
1305 ofpbuf_uninit(&bands);
1309 ofp_print_meter_stats_reply(struct ds *s, const struct ofp_header *oh)
1311 struct ofpbuf bands;
1314 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1315 ofpbuf_init(&bands, 64);
1317 struct ofputil_meter_stats ms;
1320 retval = ofputil_decode_meter_stats(&b, &ms, &bands);
1322 if (retval != EOF) {
1323 ofp_print_error(s, retval);
1327 ds_put_char(s, '\n');
1328 ofp_print_meter_stats(s, &ms);
1330 ofpbuf_uninit(&bands);
1334 ofp_print_error(struct ds *string, enum ofperr error)
1336 if (string->length) {
1337 ds_put_char(string, ' ');
1339 ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1343 ofp_print_hello(struct ds *string, const struct ofp_header *oh)
1345 uint32_t allowed_versions;
1348 ok = ofputil_decode_hello(oh, &allowed_versions);
1350 ds_put_cstr(string, "\n version bitmap: ");
1351 ofputil_format_version_bitmap(string, allowed_versions);
1354 ds_put_cstr(string, "\n unknown data in hello:\n");
1355 ds_put_hex_dump(string, oh, ntohs(oh->length), 0, true);
1360 ofp_print_error_msg(struct ds *string, const struct ofp_header *oh)
1362 size_t len = ntohs(oh->length);
1363 struct ofpbuf payload;
1367 error = ofperr_decode_msg(oh, &payload);
1369 ds_put_cstr(string, "***decode error***");
1370 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1374 ds_put_format(string, " %s\n", ofperr_get_name(error));
1376 if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
1377 ds_put_printable(string, ofpbuf_data(&payload), ofpbuf_size(&payload));
1379 s = ofp_to_string(ofpbuf_data(&payload), ofpbuf_size(&payload), 1);
1380 ds_put_cstr(string, s);
1383 ofpbuf_uninit(&payload);
1387 ofp_print_port_status(struct ds *string, const struct ofp_header *oh)
1389 struct ofputil_port_status ps;
1392 error = ofputil_decode_port_status(oh, &ps);
1394 ofp_print_error(string, error);
1398 if (ps.reason == OFPPR_ADD) {
1399 ds_put_format(string, " ADD:");
1400 } else if (ps.reason == OFPPR_DELETE) {
1401 ds_put_format(string, " DEL:");
1402 } else if (ps.reason == OFPPR_MODIFY) {
1403 ds_put_format(string, " MOD:");
1406 ofp_print_phy_port(string, &ps.desc);
1410 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
1412 const struct ofp_desc_stats *ods = ofpmsg_body(oh);
1414 ds_put_char(string, '\n');
1415 ds_put_format(string, "Manufacturer: %.*s\n",
1416 (int) sizeof ods->mfr_desc, ods->mfr_desc);
1417 ds_put_format(string, "Hardware: %.*s\n",
1418 (int) sizeof ods->hw_desc, ods->hw_desc);
1419 ds_put_format(string, "Software: %.*s\n",
1420 (int) sizeof ods->sw_desc, ods->sw_desc);
1421 ds_put_format(string, "Serial Num: %.*s\n",
1422 (int) sizeof ods->serial_num, ods->serial_num);
1423 ds_put_format(string, "DP Description: %.*s\n",
1424 (int) sizeof ods->dp_desc, ods->dp_desc);
1428 ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
1430 struct ofputil_flow_stats_request fsr;
1433 error = ofputil_decode_flow_stats_request(&fsr, oh);
1435 ofp_print_error(string, error);
1439 if (fsr.table_id != 0xff) {
1440 ds_put_format(string, " table=%"PRIu8, fsr.table_id);
1443 if (fsr.out_port != OFPP_ANY) {
1444 ds_put_cstr(string, " out_port=");
1445 ofputil_format_port(fsr.out_port, string);
1448 ds_put_char(string, ' ');
1449 match_format(&fsr.match, string, OFP_DEFAULT_PRIORITY);
1453 ofp_print_flow_stats(struct ds *string, struct ofputil_flow_stats *fs)
1455 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1456 ntohll(fs->cookie));
1458 ofp_print_duration(string, fs->duration_sec, fs->duration_nsec);
1459 ds_put_format(string, ", table=%"PRIu8", ", fs->table_id);
1460 ds_put_format(string, "n_packets=%"PRIu64", ", fs->packet_count);
1461 ds_put_format(string, "n_bytes=%"PRIu64", ", fs->byte_count);
1462 if (fs->idle_timeout != OFP_FLOW_PERMANENT) {
1463 ds_put_format(string, "idle_timeout=%"PRIu16", ", fs->idle_timeout);
1465 if (fs->hard_timeout != OFP_FLOW_PERMANENT) {
1466 ds_put_format(string, "hard_timeout=%"PRIu16", ", fs->hard_timeout);
1469 ofp_print_flow_flags(string, fs->flags);
1471 if (fs->idle_age >= 0) {
1472 ds_put_format(string, "idle_age=%d, ", fs->idle_age);
1474 if (fs->hard_age >= 0 && fs->hard_age != fs->duration_sec) {
1475 ds_put_format(string, "hard_age=%d, ", fs->hard_age);
1478 match_format(&fs->match, string, fs->priority);
1479 if (string->string[string->length - 1] != ' ') {
1480 ds_put_char(string, ' ');
1483 ds_put_cstr(string, "actions=");
1484 ofpacts_format(fs->ofpacts, fs->ofpacts_len, string);
1488 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1490 struct ofpbuf ofpacts;
1493 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1494 ofpbuf_init(&ofpacts, 64);
1496 struct ofputil_flow_stats fs;
1499 retval = ofputil_decode_flow_stats_reply(&fs, &b, true, &ofpacts);
1501 if (retval != EOF) {
1502 ds_put_cstr(string, " ***parse error***");
1506 ds_put_char(string, '\n');
1507 ofp_print_flow_stats(string, &fs);
1509 ofpbuf_uninit(&ofpacts);
1513 ofp_print_aggregate_stats_reply(struct ds *string, const struct ofp_header *oh)
1515 struct ofputil_aggregate_stats as;
1518 error = ofputil_decode_aggregate_stats_reply(&as, oh);
1520 ofp_print_error(string, error);
1524 ds_put_format(string, " packet_count=%"PRIu64, as.packet_count);
1525 ds_put_format(string, " byte_count=%"PRIu64, as.byte_count);
1526 ds_put_format(string, " flow_count=%"PRIu32, as.flow_count);
1530 print_port_stat(struct ds *string, const char *leader, uint64_t stat, int more)
1532 ds_put_cstr(string, leader);
1533 if (stat != UINT64_MAX) {
1534 ds_put_format(string, "%"PRIu64, stat);
1536 ds_put_char(string, '?');
1539 ds_put_cstr(string, ", ");
1541 ds_put_cstr(string, "\n");
1546 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1548 ofp_port_t ofp10_port;
1551 error = ofputil_decode_port_stats_request(oh, &ofp10_port);
1553 ofp_print_error(string, error);
1557 ds_put_cstr(string, " port_no=");
1558 ofputil_format_port(ofp10_port, string);
1562 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1567 ds_put_format(string, " %"PRIuSIZE" ports\n", ofputil_count_port_stats(oh));
1568 if (verbosity < 1) {
1572 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1574 struct ofputil_port_stats ps;
1577 retval = ofputil_decode_port_stats(&ps, &b);
1579 if (retval != EOF) {
1580 ds_put_cstr(string, " ***parse error***");
1585 ds_put_cstr(string, " port ");
1586 if (ofp_to_u16(ps.port_no) < 10) {
1587 ds_put_char(string, ' ');
1589 ofputil_format_port(ps.port_no, string);
1591 ds_put_cstr(string, ": rx ");
1592 print_port_stat(string, "pkts=", ps.stats.rx_packets, 1);
1593 print_port_stat(string, "bytes=", ps.stats.rx_bytes, 1);
1594 print_port_stat(string, "drop=", ps.stats.rx_dropped, 1);
1595 print_port_stat(string, "errs=", ps.stats.rx_errors, 1);
1596 print_port_stat(string, "frame=", ps.stats.rx_frame_errors, 1);
1597 print_port_stat(string, "over=", ps.stats.rx_over_errors, 1);
1598 print_port_stat(string, "crc=", ps.stats.rx_crc_errors, 0);
1600 ds_put_cstr(string, " tx ");
1601 print_port_stat(string, "pkts=", ps.stats.tx_packets, 1);
1602 print_port_stat(string, "bytes=", ps.stats.tx_bytes, 1);
1603 print_port_stat(string, "drop=", ps.stats.tx_dropped, 1);
1604 print_port_stat(string, "errs=", ps.stats.tx_errors, 1);
1605 print_port_stat(string, "coll=", ps.stats.collisions, 0);
1607 if (ps.duration_sec != UINT32_MAX) {
1608 ds_put_cstr(string, " duration=");
1609 ofp_print_duration(string, ps.duration_sec, ps.duration_nsec);
1610 ds_put_char(string, '\n');
1616 ofp_print_one_ofpst_table_reply(struct ds *string, enum ofp_version ofp_version,
1617 const char *name, struct ofp12_table_stats *ts)
1619 char name_[OFP_MAX_TABLE_NAME_LEN + 1];
1621 /* ofp13_table_stats is different */
1622 if (ofp_version > OFP12_VERSION) {
1626 ovs_strlcpy(name_, name, sizeof name_);
1628 ds_put_format(string, " %d: %-8s: ", ts->table_id, name_);
1629 ds_put_format(string, "wild=0x%05"PRIx64", ", ntohll(ts->wildcards));
1630 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1631 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1632 ds_put_cstr(string, " ");
1633 ds_put_format(string, "lookup=%"PRIu64", ", ntohll(ts->lookup_count));
1634 ds_put_format(string, "matched=%"PRIu64"\n", ntohll(ts->matched_count));
1636 if (ofp_version < OFP11_VERSION) {
1640 ds_put_cstr(string, " ");
1641 ds_put_format(string, "match=0x%08"PRIx64", ", ntohll(ts->match));
1642 ds_put_format(string, "instructions=0x%08"PRIx32", ",
1643 ntohl(ts->instructions));
1644 ds_put_format(string, "config=0x%08"PRIx32"\n", ntohl(ts->config));
1645 ds_put_cstr(string, " ");
1646 ds_put_format(string, "write_actions=0x%08"PRIx32", ",
1647 ntohl(ts->write_actions));
1648 ds_put_format(string, "apply_actions=0x%08"PRIx32"\n",
1649 ntohl(ts->apply_actions));
1651 if (ofp_version < OFP12_VERSION) {
1655 ds_put_cstr(string, " ");
1656 ds_put_format(string, "write_setfields=0x%016"PRIx64"\n",
1657 ntohll(ts->write_setfields));
1658 ds_put_cstr(string, " ");
1659 ds_put_format(string, "apply_setfields=0x%016"PRIx64"\n",
1660 ntohll(ts->apply_setfields));
1661 ds_put_cstr(string, " ");
1662 ds_put_format(string, "metadata_match=0x%016"PRIx64"\n",
1663 ntohll(ts->metadata_match));
1664 ds_put_cstr(string, " ");
1665 ds_put_format(string, "metadata_write=0x%016"PRIx64"\n",
1666 ntohll(ts->metadata_write));
1670 ofp_print_ofpst_table_reply13(struct ds *string, const struct ofp_header *oh,
1673 struct ofp13_table_stats *ts;
1677 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1678 ofpraw_pull_assert(&b);
1680 n = ofpbuf_size(&b) / sizeof *ts;
1681 ds_put_format(string, " %"PRIuSIZE" tables\n", n);
1682 if (verbosity < 1) {
1687 ts = ofpbuf_try_pull(&b, sizeof *ts);
1691 ds_put_format(string,
1692 " %d: active=%"PRIu32", lookup=%"PRIu64 \
1693 ", matched=%"PRIu64"\n",
1694 ts->table_id, ntohl(ts->active_count),
1695 ntohll(ts->lookup_count), ntohll(ts->matched_count));
1700 ofp_print_ofpst_table_reply12(struct ds *string, const struct ofp_header *oh,
1703 struct ofp12_table_stats *ts;
1707 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1708 ofpraw_pull_assert(&b);
1710 n = ofpbuf_size(&b) / sizeof *ts;
1711 ds_put_format(string, " %"PRIuSIZE" tables\n", n);
1712 if (verbosity < 1) {
1717 ts = ofpbuf_try_pull(&b, sizeof *ts);
1722 ofp_print_one_ofpst_table_reply(string, OFP12_VERSION, ts->name, ts);
1727 ofp_print_ofpst_table_reply11(struct ds *string, const struct ofp_header *oh,
1730 struct ofp11_table_stats *ts;
1734 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1735 ofpraw_pull_assert(&b);
1737 n = ofpbuf_size(&b) / sizeof *ts;
1738 ds_put_format(string, " %"PRIuSIZE" tables\n", n);
1739 if (verbosity < 1) {
1744 struct ofp12_table_stats ts12;
1746 ts = ofpbuf_try_pull(&b, sizeof *ts);
1751 ts12.table_id = ts->table_id;
1752 ts12.wildcards = htonll(ntohl(ts->wildcards));
1753 ts12.max_entries = ts->max_entries;
1754 ts12.active_count = ts->active_count;
1755 ts12.lookup_count = ts->lookup_count;
1756 ts12.matched_count = ts->matched_count;
1757 ts12.match = htonll(ntohl(ts->match));
1758 ts12.instructions = ts->instructions;
1759 ts12.config = ts->config;
1760 ts12.write_actions = ts->write_actions;
1761 ts12.apply_actions = ts->apply_actions;
1762 ofp_print_one_ofpst_table_reply(string, OFP11_VERSION, ts->name, &ts12);
1767 ofp_print_ofpst_table_reply10(struct ds *string, const struct ofp_header *oh,
1770 struct ofp10_table_stats *ts;
1774 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1775 ofpraw_pull_assert(&b);
1777 n = ofpbuf_size(&b) / sizeof *ts;
1778 ds_put_format(string, " %"PRIuSIZE" tables\n", n);
1779 if (verbosity < 1) {
1784 struct ofp12_table_stats ts12;
1786 ts = ofpbuf_try_pull(&b, sizeof *ts);
1791 ts12.table_id = ts->table_id;
1792 ts12.wildcards = htonll(ntohl(ts->wildcards));
1793 ts12.max_entries = ts->max_entries;
1794 ts12.active_count = ts->active_count;
1795 ts12.lookup_count = get_32aligned_be64(&ts->lookup_count);
1796 ts12.matched_count = get_32aligned_be64(&ts->matched_count);
1797 ofp_print_one_ofpst_table_reply(string, OFP10_VERSION, ts->name, &ts12);
1802 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1805 switch ((enum ofp_version)oh->version) {
1809 ofp_print_ofpst_table_reply13(string, oh, verbosity);
1813 ofp_print_ofpst_table_reply12(string, oh, verbosity);
1817 ofp_print_ofpst_table_reply11(string, oh, verbosity);
1821 ofp_print_ofpst_table_reply10(string, oh, verbosity);
1830 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1832 if (queue_id == OFPQ_ALL) {
1833 ds_put_cstr(string, "ALL");
1835 ds_put_format(string, "%"PRIu32, queue_id);
1840 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1842 struct ofputil_queue_stats_request oqsr;
1845 error = ofputil_decode_queue_stats_request(oh, &oqsr);
1847 ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1851 ds_put_cstr(string, "port=");
1852 ofputil_format_port(oqsr.port_no, string);
1854 ds_put_cstr(string, " queue=");
1855 ofp_print_queue_name(string, oqsr.queue_id);
1859 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1864 ds_put_format(string, " %"PRIuSIZE" queues\n", ofputil_count_queue_stats(oh));
1865 if (verbosity < 1) {
1869 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1871 struct ofputil_queue_stats qs;
1874 retval = ofputil_decode_queue_stats(&qs, &b);
1876 if (retval != EOF) {
1877 ds_put_cstr(string, " ***parse error***");
1882 ds_put_cstr(string, " port ");
1883 ofputil_format_port(qs.port_no, string);
1884 ds_put_cstr(string, " queue ");
1885 ofp_print_queue_name(string, qs.queue_id);
1886 ds_put_cstr(string, ": ");
1888 print_port_stat(string, "bytes=", qs.tx_bytes, 1);
1889 print_port_stat(string, "pkts=", qs.tx_packets, 1);
1890 print_port_stat(string, "errors=", qs.tx_errors, 1);
1892 ds_put_cstr(string, "duration=");
1893 if (qs.duration_sec != UINT32_MAX) {
1894 ofp_print_duration(string, qs.duration_sec, qs.duration_nsec);
1896 ds_put_char(string, '?');
1898 ds_put_char(string, '\n');
1903 ofp_print_ofpst_port_desc_request(struct ds *string,
1904 const struct ofp_header *oh)
1909 error = ofputil_decode_port_desc_stats_request(oh, &port);
1911 ofp_print_error(string, error);
1915 ds_put_cstr(string, " port=");
1916 ofputil_format_port(port, string);
1920 ofp_print_ofpst_port_desc_reply(struct ds *string,
1921 const struct ofp_header *oh)
1925 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1926 ofpraw_pull_assert(&b);
1927 ds_put_char(string, '\n');
1928 ofp_print_phy_ports(string, oh->version, &b);
1932 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1934 uint16_t flags = ofpmp_flags(oh);
1937 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
1942 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1944 uint16_t flags = ofpmp_flags(oh);
1947 ds_put_cstr(string, " flags=");
1948 if (flags & OFPSF_REPLY_MORE) {
1949 ds_put_cstr(string, "[more]");
1950 flags &= ~OFPSF_REPLY_MORE;
1953 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1960 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1962 size_t len = ntohs(oh->length);
1964 ds_put_format(string, " %"PRIuSIZE" bytes of payload\n", len - sizeof *oh);
1965 if (verbosity > 1) {
1966 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1971 ofp_print_role_generic(struct ds *string, enum ofp12_controller_role role,
1972 uint64_t generation_id)
1974 ds_put_cstr(string, " role=");
1977 case OFPCR12_ROLE_NOCHANGE:
1978 ds_put_cstr(string, "nochange");
1980 case OFPCR12_ROLE_EQUAL:
1981 ds_put_cstr(string, "equal"); /* OF 1.2 wording */
1983 case OFPCR12_ROLE_MASTER:
1984 ds_put_cstr(string, "master");
1986 case OFPCR12_ROLE_SLAVE:
1987 ds_put_cstr(string, "slave");
1993 if (generation_id != UINT64_MAX) {
1994 ds_put_format(string, " generation_id=%"PRIu64, generation_id);
1999 ofp_print_role_message(struct ds *string, const struct ofp_header *oh)
2001 struct ofputil_role_request rr;
2004 error = ofputil_decode_role_message(oh, &rr);
2006 ofp_print_error(string, error);
2010 ofp_print_role_generic(string, rr.role, rr.have_generation_id ? rr.generation_id : UINT64_MAX);
2014 ofp_print_role_status_message(struct ds *string, const struct ofp_header *oh)
2016 struct ofputil_role_status rs;
2019 error = ofputil_decode_role_status(oh, &rs);
2021 ofp_print_error(string, error);
2025 ofp_print_role_generic(string, rs.role, rs.generation_id);
2027 ds_put_cstr(string, " reason=");
2029 switch (rs.reason) {
2030 case OFPCRR_MASTER_REQUEST:
2031 ds_put_cstr(string, "master_request");
2034 ds_put_cstr(string, "configuration_changed");
2036 case OFPCRR_EXPERIMENTER:
2037 ds_put_cstr(string, "experimenter_data_changed");
2045 ofp_print_nxt_flow_mod_table_id(struct ds *string,
2046 const struct nx_flow_mod_table_id *nfmti)
2048 ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
2052 ofp_print_nxt_set_flow_format(struct ds *string,
2053 const struct nx_set_flow_format *nsff)
2055 uint32_t format = ntohl(nsff->format);
2057 ds_put_cstr(string, " format=");
2058 if (ofputil_nx_flow_format_is_valid(format)) {
2059 ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
2061 ds_put_format(string, "%"PRIu32, format);
2066 ofp_print_nxt_set_packet_in_format(struct ds *string,
2067 const struct nx_set_packet_in_format *nspf)
2069 uint32_t format = ntohl(nspf->format);
2071 ds_put_cstr(string, " format=");
2072 if (ofputil_packet_in_format_is_valid(format)) {
2073 ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
2075 ds_put_format(string, "%"PRIu32, format);
2079 /* Returns a string form of 'reason'. The return value is either a statically
2080 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
2081 * 'bufsize' should be at least OFP_PORT_REASON_BUFSIZE. */
2082 #define OFP_PORT_REASON_BUFSIZE (INT_STRLEN(int) + 1)
2084 ofp_port_reason_to_string(enum ofp_port_reason reason,
2085 char *reasonbuf, size_t bufsize)
2098 snprintf(reasonbuf, bufsize, "%d", (int) reason);
2104 ofp_print_nxt_set_async_config(struct ds *string,
2105 const struct nx_async_config *nac)
2109 for (i = 0; i < 2; i++) {
2112 ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
2114 ds_put_cstr(string, " PACKET_IN:");
2115 for (j = 0; j < 32; j++) {
2116 if (nac->packet_in_mask[i] & htonl(1u << j)) {
2117 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
2120 reason = ofputil_packet_in_reason_to_string(j, reasonbuf,
2122 ds_put_format(string, " %s", reason);
2125 if (!nac->packet_in_mask[i]) {
2126 ds_put_cstr(string, " (off)");
2128 ds_put_char(string, '\n');
2130 ds_put_cstr(string, " PORT_STATUS:");
2131 for (j = 0; j < 32; j++) {
2132 if (nac->port_status_mask[i] & htonl(1u << j)) {
2133 char reasonbuf[OFP_PORT_REASON_BUFSIZE];
2136 reason = ofp_port_reason_to_string(j, reasonbuf,
2138 ds_put_format(string, " %s", reason);
2141 if (!nac->port_status_mask[i]) {
2142 ds_put_cstr(string, " (off)");
2144 ds_put_char(string, '\n');
2146 ds_put_cstr(string, " FLOW_REMOVED:");
2147 for (j = 0; j < 32; j++) {
2148 if (nac->flow_removed_mask[i] & htonl(1u << j)) {
2149 char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
2152 reason = ofp_flow_removed_reason_to_string(j, reasonbuf,
2154 ds_put_format(string, " %s", reason);
2157 if (!nac->flow_removed_mask[i]) {
2158 ds_put_cstr(string, " (off)");
2160 ds_put_char(string, '\n');
2165 ofp_print_nxt_set_controller_id(struct ds *string,
2166 const struct nx_controller_id *nci)
2168 ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
2172 ofp_print_nxt_flow_monitor_cancel(struct ds *string,
2173 const struct ofp_header *oh)
2175 ds_put_format(string, " id=%"PRIu32,
2176 ofputil_decode_flow_monitor_cancel(oh));
2180 nx_flow_monitor_flags_to_name(uint32_t bit)
2182 enum nx_flow_monitor_flags fmf = bit;
2185 case NXFMF_INITIAL: return "initial";
2186 case NXFMF_ADD: return "add";
2187 case NXFMF_DELETE: return "delete";
2188 case NXFMF_MODIFY: return "modify";
2189 case NXFMF_ACTIONS: return "actions";
2190 case NXFMF_OWN: return "own";
2197 ofp_print_nxst_flow_monitor_request(struct ds *string,
2198 const struct ofp_header *oh)
2202 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2204 struct ofputil_flow_monitor_request request;
2207 retval = ofputil_decode_flow_monitor_request(&request, &b);
2209 if (retval != EOF) {
2210 ofp_print_error(string, retval);
2215 ds_put_format(string, "\n id=%"PRIu32" flags=", request.id);
2216 ofp_print_bit_names(string, request.flags,
2217 nx_flow_monitor_flags_to_name, ',');
2219 if (request.out_port != OFPP_NONE) {
2220 ds_put_cstr(string, " out_port=");
2221 ofputil_format_port(request.out_port, string);
2224 if (request.table_id != 0xff) {
2225 ds_put_format(string, " table=%"PRIu8, request.table_id);
2228 ds_put_char(string, ' ');
2229 match_format(&request.match, string, OFP_DEFAULT_PRIORITY);
2230 ds_chomp(string, ' ');
2235 ofp_print_nxst_flow_monitor_reply(struct ds *string,
2236 const struct ofp_header *oh)
2238 uint64_t ofpacts_stub[1024 / 8];
2239 struct ofpbuf ofpacts;
2242 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2243 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2245 char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
2246 struct ofputil_flow_update update;
2250 update.match = &match;
2251 retval = ofputil_decode_flow_update(&update, &b, &ofpacts);
2253 if (retval != EOF) {
2254 ofp_print_error(string, retval);
2256 ofpbuf_uninit(&ofpacts);
2260 ds_put_cstr(string, "\n event=");
2261 switch (update.event) {
2263 ds_put_cstr(string, "ADDED");
2267 ds_put_format(string, "DELETED reason=%s",
2268 ofp_flow_removed_reason_to_string(update.reason,
2273 case NXFME_MODIFIED:
2274 ds_put_cstr(string, "MODIFIED");
2278 ds_put_format(string, "ABBREV xid=0x%"PRIx32, ntohl(update.xid));
2282 ds_put_format(string, " table=%"PRIu8, update.table_id);
2283 if (update.idle_timeout != OFP_FLOW_PERMANENT) {
2284 ds_put_format(string, " idle_timeout=%"PRIu16,
2285 update.idle_timeout);
2287 if (update.hard_timeout != OFP_FLOW_PERMANENT) {
2288 ds_put_format(string, " hard_timeout=%"PRIu16,
2289 update.hard_timeout);
2291 ds_put_format(string, " cookie=%#"PRIx64, ntohll(update.cookie));
2293 ds_put_char(string, ' ');
2294 match_format(update.match, string, OFP_DEFAULT_PRIORITY);
2296 if (update.ofpacts_len) {
2297 if (string->string[string->length - 1] != ' ') {
2298 ds_put_char(string, ' ');
2300 ds_put_cstr(string, "actions=");
2301 ofpacts_format(update.ofpacts, update.ofpacts_len, string);
2307 ofp_print_version(const struct ofp_header *oh,
2310 switch (oh->version) {
2314 ds_put_cstr(string, " (OF1.1)");
2317 ds_put_cstr(string, " (OF1.2)");
2320 ds_put_cstr(string, " (OF1.3)");
2323 ds_put_cstr(string, " (OF1.4)");
2326 ds_put_cstr(string, " (OF1.5)");
2329 ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
2332 ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
2336 ofp_header_to_string__(const struct ofp_header *oh, enum ofpraw raw,
2339 ds_put_cstr(string, ofpraw_get_name(raw));
2340 ofp_print_version(oh, string);
2344 ofp_print_group(struct ds *s, uint32_t group_id, uint8_t type,
2345 struct list *p_buckets)
2347 static const char *type_str[] = { "all", "select", "indirect",
2349 struct ofputil_bucket *bucket;
2351 ds_put_format(s, "group_id=%"PRIu32",type=%s",
2352 group_id, type_str[type > 4 ? 4 : type]);
2357 LIST_FOR_EACH (bucket, list_node, p_buckets) {
2358 ds_put_cstr(s, ",bucket=");
2360 if (bucket->weight != 1) {
2361 ds_put_format(s, "weight:%"PRIu16",", bucket->weight);
2363 if (bucket->watch_port != OFPP_NONE) {
2364 ds_put_format(s, "watch_port:%"PRIu32",", bucket->watch_port);
2366 if (bucket->watch_group != OFPG11_ANY) {
2367 ds_put_format(s, "watch_group:%"PRIu32",", bucket->watch_group);
2370 ds_put_cstr(s, "actions=");
2371 ofpacts_format(bucket->ofpacts, bucket->ofpacts_len, s);
2376 ofp_print_ofpst_group_desc_request(struct ds *string,
2377 const struct ofp_header *oh)
2379 uint32_t group_id = ofputil_decode_group_desc_request(oh);
2380 ds_put_cstr(string, " group_id=");
2381 ofputil_format_group(group_id, string);
2385 ofp_print_group_desc(struct ds *s, const struct ofp_header *oh)
2389 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2391 struct ofputil_group_desc gd;
2394 retval = ofputil_decode_group_desc_reply(&gd, &b, oh->version);
2396 if (retval != EOF) {
2397 ds_put_cstr(s, " ***parse error***");
2402 ds_put_char(s, '\n');
2403 ds_put_char(s, ' ');
2404 ofp_print_group(s, gd.group_id, gd.type, &gd.buckets);
2409 ofp_print_ofpst_group_request(struct ds *string, const struct ofp_header *oh)
2414 error = ofputil_decode_group_stats_request(oh, &group_id);
2416 ofp_print_error(string, error);
2420 ds_put_cstr(string, " group_id=");
2421 ofputil_format_group(group_id, string);
2425 ofp_print_group_stats(struct ds *s, const struct ofp_header *oh)
2430 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2433 struct ofputil_group_stats gs;
2436 retval = ofputil_decode_group_stats_reply(&b, &gs);
2438 if (retval != EOF) {
2439 ds_put_cstr(s, " ***parse error***");
2444 ds_put_char(s, '\n');
2446 ds_put_char(s, ' ');
2447 ds_put_format(s, "group_id=%"PRIu32",", gs.group_id);
2449 if (gs.duration_sec != UINT32_MAX) {
2450 ds_put_cstr(s, "duration=");
2451 ofp_print_duration(s, gs.duration_sec, gs.duration_nsec);
2452 ds_put_char(s, ',');
2454 ds_put_format(s, "ref_count=%"PRIu32",", gs.ref_count);
2455 ds_put_format(s, "packet_count=%"PRIu64",", gs.packet_count);
2456 ds_put_format(s, "byte_count=%"PRIu64"", gs.byte_count);
2458 for (bucket_i = 0; bucket_i < gs.n_buckets; bucket_i++) {
2459 if (gs.bucket_stats[bucket_i].packet_count != UINT64_MAX) {
2460 ds_put_format(s, ",bucket%"PRIu32":", bucket_i);
2461 ds_put_format(s, "packet_count=%"PRIu64",", gs.bucket_stats[bucket_i].packet_count);
2462 ds_put_format(s, "byte_count=%"PRIu64"", gs.bucket_stats[bucket_i].byte_count);
2466 free(gs.bucket_stats);
2471 ofp_print_group_features(struct ds *string, const struct ofp_header *oh)
2473 struct ofputil_group_features features;
2475 ofputil_decode_group_features_reply(oh, &features);
2477 ds_put_format(string, "\n Group table:\n");
2478 ds_put_format(string, " Types: 0x%"PRIx32"\n", features.types);
2479 ds_put_format(string, " Capabilities: 0x%"PRIx32"\n",
2480 features.capabilities);
2482 if (features.types & (1u << OFPGT11_ALL)) {
2483 ds_put_format(string, " All group :\n");
2484 ds_put_format(string,
2485 " max_groups = %#"PRIx32" actions=0x%08"PRIx32"\n",
2486 features.max_groups[0], features.actions[0]);
2489 if (features.types & (1u << OFPGT11_SELECT)) {
2490 ds_put_format(string, " Select group :\n");
2491 ds_put_format(string, " max_groups = %#"PRIx32" "
2492 "actions=0x%08"PRIx32"\n",
2493 features.max_groups[1], features.actions[1]);
2496 if (features.types & (1u << OFPGT11_INDIRECT)) {
2497 ds_put_format(string, " Indirect group :\n");
2498 ds_put_format(string, " max_groups = %#"PRIx32" "
2499 "actions=0x%08"PRIx32"\n",
2500 features.max_groups[2], features.actions[2]);
2503 if (features.types & (1u << OFPGT11_FF)) {
2504 ds_put_format(string, " Fast Failover group :\n");
2505 ds_put_format(string, " max_groups = %#"PRIx32" "
2506 "actions=0x%08"PRIx32"\n",
2507 features.max_groups[3], features.actions[3]);
2512 ofp_print_group_mod(struct ds *s, const struct ofp_header *oh)
2514 struct ofputil_group_mod gm;
2517 error = ofputil_decode_group_mod(oh, &gm);
2519 ofp_print_error(s, error);
2523 ds_put_char(s, '\n');
2525 ds_put_char(s, ' ');
2526 switch (gm.command) {
2528 ds_put_cstr(s, "ADD");
2531 case OFPGC11_MODIFY:
2532 ds_put_cstr(s, "MOD");
2535 case OFPGC11_DELETE:
2536 ds_put_cstr(s, "DEL");
2540 ds_put_format(s, "cmd:%"PRIu16"", gm.command);
2542 ds_put_char(s, ' ');
2544 ofp_print_group(s, gm.group_id, gm.type, &gm.buckets);
2548 ofp13_action_to_string(uint32_t bit)
2551 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME) \
2552 case 1u << ENUM: return NAME;
2553 #include "ofp-util.def"
2559 print_table_action_features(struct ds *s,
2560 const struct ofputil_table_action_features *taf)
2562 ds_put_cstr(s, " actions: ");
2563 ofp_print_bit_names(s, taf->actions, ofp13_action_to_string, ',');
2564 ds_put_char(s, '\n');
2566 ds_put_cstr(s, " supported on Set-Field: ");
2567 if (taf->set_fields) {
2570 for (i = 0; i < MFF_N_IDS; i++) {
2571 uint64_t bit = UINT64_C(1) << i;
2573 if (taf->set_fields & bit) {
2574 ds_put_format(s, "%s,", mf_from_id(i)->name);
2579 ds_put_cstr(s, "none");
2581 ds_put_char(s, '\n');
2585 table_action_features_equal(const struct ofputil_table_action_features *a,
2586 const struct ofputil_table_action_features *b)
2588 return a->actions == b->actions && a->set_fields == b->set_fields;
2592 print_table_instruction_features(
2593 struct ds *s, const struct ofputil_table_instruction_features *tif)
2597 ds_put_cstr(s, " next tables: ");
2598 for (start = bitmap_scan(tif->next, 1, 0, 255); start < 255;
2599 start = bitmap_scan(tif->next, 1, end, 255)) {
2600 end = bitmap_scan(tif->next, 0, start + 1, 255);
2601 if (end == start + 1) {
2602 ds_put_format(s, "%d,", start);
2604 ds_put_format(s, "%d-%d,", start, end - 1);
2608 if (ds_last(s) == ' ') {
2609 ds_put_cstr(s, "none");
2611 ds_put_char(s, '\n');
2613 ds_put_cstr(s, " instructions: ");
2614 if (tif->instructions) {
2617 for (i = 0; i < 32; i++) {
2618 if (tif->instructions & (1u << i)) {
2619 ds_put_format(s, "%s,", ovs_instruction_name_from_type(i));
2624 ds_put_cstr(s, "none");
2626 ds_put_char(s, '\n');
2628 if (table_action_features_equal(&tif->write, &tif->apply)) {
2629 ds_put_cstr(s, " Write-Actions and Apply-Actions features:\n");
2630 print_table_action_features(s, &tif->write);
2632 ds_put_cstr(s, " Write-Actions features:\n");
2633 print_table_action_features(s, &tif->write);
2634 ds_put_cstr(s, " Apply-Actions features:\n");
2635 print_table_action_features(s, &tif->apply);
2640 table_instruction_features_equal(
2641 const struct ofputil_table_instruction_features *a,
2642 const struct ofputil_table_instruction_features *b)
2644 return (bitmap_equal(a->next, b->next, 255)
2645 && a->instructions == b->instructions
2646 && table_action_features_equal(&a->write, &b->write)
2647 && table_action_features_equal(&a->apply, &b->apply));
2651 ofp_print_table_features(struct ds *s, const struct ofp_header *oh)
2655 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2658 struct ofputil_table_features tf;
2662 retval = ofputil_decode_table_features(&b, &tf, true);
2664 if (retval != EOF) {
2665 ofp_print_error(s, retval);
2670 ds_put_format(s, "\n table %"PRIu8":\n", tf.table_id);
2671 ds_put_format(s, " name=\"%s\"\n", tf.name);
2672 ds_put_format(s, " metadata: match=%#"PRIx64" write=%#"PRIx64"\n",
2673 ntohll(tf.metadata_match), ntohll(tf.metadata_write));
2675 ds_put_cstr(s, " config=");
2676 ofp_print_table_miss_config(s, tf.config);
2678 ds_put_format(s, " max_entries=%"PRIu32"\n", tf.max_entries);
2680 if (table_instruction_features_equal(&tf.nonmiss, &tf.miss)) {
2681 ds_put_cstr(s, " instructions (table miss and others):\n");
2682 print_table_instruction_features(s, &tf.nonmiss);
2684 ds_put_cstr(s, " instructions (other than table miss):\n");
2685 print_table_instruction_features(s, &tf.nonmiss);
2686 ds_put_cstr(s, " instructions (table miss):\n");
2687 print_table_instruction_features(s, &tf.miss);
2690 ds_put_cstr(s, " matching:\n");
2691 for (i = 0; i < MFF_N_IDS; i++) {
2692 uint64_t bit = UINT64_C(1) << i;
2694 if (tf.match & bit) {
2695 const struct mf_field *f = mf_from_id(i);
2697 ds_put_format(s, " %s: %s\n",
2699 (tf.mask ? "arbitrary mask"
2700 : tf.wildcard ? "exact match or wildcard"
2701 : "must exact match"));
2708 bundle_flags_to_name(uint32_t bit)
2721 ofp_print_bundle_ctrl(struct ds *s, const struct ofp_header *oh)
2724 struct ofputil_bundle_ctrl_msg bctrl;
2726 error = ofputil_decode_bundle_ctrl(oh, &bctrl);
2728 ofp_print_error(s, error);
2732 ds_put_char(s, '\n');
2734 ds_put_format(s, " bundle_id=%#"PRIx32" type=", bctrl.bundle_id);
2735 switch (bctrl.type) {
2736 case OFPBCT_OPEN_REQUEST:
2737 ds_put_cstr(s, "OPEN_REQUEST");
2739 case OFPBCT_OPEN_REPLY:
2740 ds_put_cstr(s, "OPEN_REPLY");
2742 case OFPBCT_CLOSE_REQUEST:
2743 ds_put_cstr(s, "CLOSE_REQUEST");
2745 case OFPBCT_CLOSE_REPLY:
2746 ds_put_cstr(s, "CLOSE_REPLY");
2748 case OFPBCT_COMMIT_REQUEST:
2749 ds_put_cstr(s, "COMMIT_REQUEST");
2751 case OFPBCT_COMMIT_REPLY:
2752 ds_put_cstr(s, "COMMIT_REPLY");
2754 case OFPBCT_DISCARD_REQUEST:
2755 ds_put_cstr(s, "DISCARD_REQUEST");
2757 case OFPBCT_DISCARD_REPLY:
2758 ds_put_cstr(s, "DISCARD_REPLY");
2762 ds_put_cstr(s, " flags=");
2763 ofp_print_bit_names(s, bctrl.flags, bundle_flags_to_name, ' ');
2767 ofp_print_bundle_add(struct ds *s, const struct ofp_header *oh, int verbosity)
2770 struct ofputil_bundle_add_msg badd;
2773 error = ofputil_decode_bundle_add(oh, &badd);
2775 ofp_print_error(s, error);
2779 ds_put_char(s, '\n');
2780 ds_put_format(s, " bundle_id=%#"PRIx32, badd.bundle_id);
2781 ds_put_cstr(s, " flags=");
2782 ofp_print_bit_names(s, badd.flags, bundle_flags_to_name, ' ');
2784 ds_put_char(s, '\n');
2785 msg = ofp_to_string(badd.msg, ntohs(badd.msg->length), verbosity);
2787 ds_put_cstr(s, msg);
2792 ofp_to_string__(const struct ofp_header *oh, enum ofpraw raw,
2793 struct ds *string, int verbosity)
2795 const void *msg = oh;
2797 ofp_header_to_string__(oh, raw, string);
2798 switch (ofptype_from_ofpraw(raw)) {
2800 case OFPTYPE_GROUP_STATS_REQUEST:
2801 ofp_print_stats_request(string, oh);
2802 ofp_print_ofpst_group_request(string, oh);
2805 case OFPTYPE_GROUP_STATS_REPLY:
2806 ofp_print_group_stats(string, oh);
2809 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
2810 ofp_print_stats_request(string, oh);
2811 ofp_print_ofpst_group_desc_request(string, oh);
2814 case OFPTYPE_GROUP_DESC_STATS_REPLY:
2815 ofp_print_group_desc(string, oh);
2818 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
2819 ofp_print_stats_request(string, oh);
2822 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
2823 ofp_print_group_features(string, oh);
2826 case OFPTYPE_GROUP_MOD:
2827 ofp_print_group_mod(string, oh);
2830 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
2831 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
2832 ofp_print_table_features(string, oh);
2836 ofp_print_hello(string, oh);
2840 ofp_print_error_msg(string, oh);
2843 case OFPTYPE_ECHO_REQUEST:
2844 case OFPTYPE_ECHO_REPLY:
2845 ofp_print_echo(string, oh, verbosity);
2848 case OFPTYPE_FEATURES_REQUEST:
2851 case OFPTYPE_FEATURES_REPLY:
2852 ofp_print_switch_features(string, oh);
2855 case OFPTYPE_GET_CONFIG_REQUEST:
2858 case OFPTYPE_GET_CONFIG_REPLY:
2859 case OFPTYPE_SET_CONFIG:
2860 ofp_print_switch_config(string, ofpmsg_body(oh));
2863 case OFPTYPE_PACKET_IN:
2864 ofp_print_packet_in(string, oh, verbosity);
2867 case OFPTYPE_FLOW_REMOVED:
2868 ofp_print_flow_removed(string, oh);
2871 case OFPTYPE_PORT_STATUS:
2872 ofp_print_port_status(string, oh);
2875 case OFPTYPE_PACKET_OUT:
2876 ofp_print_packet_out(string, oh, verbosity);
2879 case OFPTYPE_FLOW_MOD:
2880 ofp_print_flow_mod(string, oh, verbosity);
2883 case OFPTYPE_PORT_MOD:
2884 ofp_print_port_mod(string, oh);
2887 case OFPTYPE_TABLE_MOD:
2888 ofp_print_table_mod(string, oh);
2891 case OFPTYPE_METER_MOD:
2892 ofp_print_meter_mod(string, oh);
2895 case OFPTYPE_BARRIER_REQUEST:
2896 case OFPTYPE_BARRIER_REPLY:
2899 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
2900 ofp_print_queue_get_config_request(string, oh);
2903 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
2904 ofp_print_queue_get_config_reply(string, oh);
2907 case OFPTYPE_ROLE_REQUEST:
2908 case OFPTYPE_ROLE_REPLY:
2909 ofp_print_role_message(string, oh);
2911 case OFPTYPE_ROLE_STATUS:
2912 ofp_print_role_status_message(string, oh);
2915 case OFPTYPE_METER_STATS_REQUEST:
2916 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
2917 ofp_print_stats_request(string, oh);
2918 ofp_print_meter_stats_request(string, oh);
2921 case OFPTYPE_METER_STATS_REPLY:
2922 ofp_print_stats_reply(string, oh);
2923 ofp_print_meter_stats_reply(string, oh);
2926 case OFPTYPE_METER_CONFIG_STATS_REPLY:
2927 ofp_print_stats_reply(string, oh);
2928 ofp_print_meter_config_reply(string, oh);
2931 case OFPTYPE_METER_FEATURES_STATS_REPLY:
2932 ofp_print_stats_reply(string, oh);
2933 ofp_print_meter_features_reply(string, oh);
2936 case OFPTYPE_DESC_STATS_REQUEST:
2937 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
2938 ofp_print_stats_request(string, oh);
2941 case OFPTYPE_FLOW_STATS_REQUEST:
2942 case OFPTYPE_AGGREGATE_STATS_REQUEST:
2943 ofp_print_stats_request(string, oh);
2944 ofp_print_flow_stats_request(string, oh);
2947 case OFPTYPE_TABLE_STATS_REQUEST:
2948 ofp_print_stats_request(string, oh);
2951 case OFPTYPE_PORT_STATS_REQUEST:
2952 ofp_print_stats_request(string, oh);
2953 ofp_print_ofpst_port_request(string, oh);
2956 case OFPTYPE_QUEUE_STATS_REQUEST:
2957 ofp_print_stats_request(string, oh);
2958 ofp_print_ofpst_queue_request(string, oh);
2961 case OFPTYPE_DESC_STATS_REPLY:
2962 ofp_print_stats_reply(string, oh);
2963 ofp_print_ofpst_desc_reply(string, oh);
2966 case OFPTYPE_FLOW_STATS_REPLY:
2967 ofp_print_stats_reply(string, oh);
2968 ofp_print_flow_stats_reply(string, oh);
2971 case OFPTYPE_QUEUE_STATS_REPLY:
2972 ofp_print_stats_reply(string, oh);
2973 ofp_print_ofpst_queue_reply(string, oh, verbosity);
2976 case OFPTYPE_PORT_STATS_REPLY:
2977 ofp_print_stats_reply(string, oh);
2978 ofp_print_ofpst_port_reply(string, oh, verbosity);
2981 case OFPTYPE_TABLE_STATS_REPLY:
2982 ofp_print_stats_reply(string, oh);
2983 ofp_print_ofpst_table_reply(string, oh, verbosity);
2986 case OFPTYPE_AGGREGATE_STATS_REPLY:
2987 ofp_print_stats_reply(string, oh);
2988 ofp_print_aggregate_stats_reply(string, oh);
2991 case OFPTYPE_PORT_DESC_STATS_REQUEST:
2992 ofp_print_stats_request(string, oh);
2993 ofp_print_ofpst_port_desc_request(string, oh);
2996 case OFPTYPE_PORT_DESC_STATS_REPLY:
2997 ofp_print_stats_reply(string, oh);
2998 ofp_print_ofpst_port_desc_reply(string, oh);
3001 case OFPTYPE_FLOW_MOD_TABLE_ID:
3002 ofp_print_nxt_flow_mod_table_id(string, ofpmsg_body(oh));
3005 case OFPTYPE_SET_FLOW_FORMAT:
3006 ofp_print_nxt_set_flow_format(string, ofpmsg_body(oh));
3009 case OFPTYPE_SET_PACKET_IN_FORMAT:
3010 ofp_print_nxt_set_packet_in_format(string, ofpmsg_body(oh));
3013 case OFPTYPE_FLOW_AGE:
3016 case OFPTYPE_SET_CONTROLLER_ID:
3017 ofp_print_nxt_set_controller_id(string, ofpmsg_body(oh));
3020 case OFPTYPE_GET_ASYNC_REPLY:
3021 case OFPTYPE_SET_ASYNC_CONFIG:
3022 ofp_print_nxt_set_async_config(string, ofpmsg_body(oh));
3024 case OFPTYPE_GET_ASYNC_REQUEST:
3026 case OFPTYPE_FLOW_MONITOR_CANCEL:
3027 ofp_print_nxt_flow_monitor_cancel(string, msg);
3030 case OFPTYPE_FLOW_MONITOR_PAUSED:
3031 case OFPTYPE_FLOW_MONITOR_RESUMED:
3034 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
3035 ofp_print_nxst_flow_monitor_request(string, msg);
3038 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
3039 ofp_print_nxst_flow_monitor_reply(string, msg);
3042 case OFPTYPE_BUNDLE_CONTROL:
3043 ofp_print_bundle_ctrl(string, msg);
3046 case OFPTYPE_BUNDLE_ADD_MESSAGE:
3047 ofp_print_bundle_add(string, msg, verbosity);
3052 /* Composes and returns a string representing the OpenFlow packet of 'len'
3053 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
3054 * verbosity and higher numbers increase verbosity. The caller is responsible
3055 * for freeing the string. */
3057 ofp_to_string(const void *oh_, size_t len, int verbosity)
3059 struct ds string = DS_EMPTY_INITIALIZER;
3060 const struct ofp_header *oh = oh_;
3063 ds_put_cstr(&string, "OpenFlow message is empty\n");
3064 } else if (len < sizeof(struct ofp_header)) {
3065 ds_put_format(&string, "OpenFlow packet too short (only %"PRIuSIZE" bytes):\n",
3067 } else if (ntohs(oh->length) > len) {
3071 error = ofpraw_decode_partial(&raw, oh, len);
3073 ofp_header_to_string__(oh, raw, &string);
3074 ds_put_char(&string, '\n');
3077 ds_put_format(&string,
3078 "(***truncated to %"PRIuSIZE" bytes from %"PRIu16"***)\n",
3079 len, ntohs(oh->length));
3080 } else if (ntohs(oh->length) < len) {
3081 ds_put_format(&string,
3082 "(***only uses %"PRIu16" bytes out of %"PRIuSIZE"***)\n",
3083 ntohs(oh->length), len);
3088 error = ofpraw_decode(&raw, oh);
3090 ofp_to_string__(oh, raw, &string, verbosity);
3091 if (verbosity >= 5) {
3092 if (ds_last(&string) != '\n') {
3093 ds_put_char(&string, '\n');
3095 ds_put_hex_dump(&string, oh, len, 0, true);
3097 if (ds_last(&string) != '\n') {
3098 ds_put_char(&string, '\n');
3100 return ds_steal_cstr(&string);
3103 ofp_print_error(&string, error);
3105 ds_put_hex_dump(&string, oh, len, 0, true);
3106 return ds_steal_cstr(&string);
3110 print_and_free(FILE *stream, char *string)
3112 fputs(string, stream);
3116 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
3117 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
3118 * numbers increase verbosity. */
3120 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
3122 print_and_free(stream, ofp_to_string(oh, len, verbosity));
3125 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
3126 * 'data' to 'stream'. */
3128 ofp_print_packet(FILE *stream, const void *data, size_t len)
3130 print_and_free(stream, ofp_packet_to_string(data, len));