2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 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"
51 static void ofp_print_queue_name(struct ds *string, uint32_t port);
52 static void ofp_print_error(struct ds *, enum ofperr);
55 /* Returns a string that represents the contents of the Ethernet frame in the
56 * 'len' bytes starting at 'data'. The caller must free the returned string.*/
58 ofp_packet_to_string(const void *data, size_t len)
60 struct ds ds = DS_EMPTY_INITIALIZER;
64 ofpbuf_use_const(&buf, data, len);
65 flow_extract(&buf, 0, 0, NULL, 0, &flow);
66 flow_format(&ds, &flow);
69 if (flow.nw_proto == IPPROTO_TCP) {
70 struct tcp_header *th = buf.l4;
71 ds_put_format(&ds, " tcp_csum:%"PRIx16,
73 } else if (flow.nw_proto == IPPROTO_UDP) {
74 struct udp_header *uh = buf.l4;
75 ds_put_format(&ds, " udp_csum:%"PRIx16,
80 ds_put_char(&ds, '\n');
86 ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
89 struct ofputil_packet_in pin;
93 error = ofputil_decode_packet_in(&pin, oh);
95 ofp_print_error(string, error);
100 ds_put_format(string, " table_id=%"PRIu8, pin.table_id);
104 ds_put_format(string, " cookie=0x%"PRIx64, ntohll(pin.cookie));
107 ds_put_format(string, " total_len=%"PRIu16" in_port=", pin.total_len);
108 ofputil_format_port(pin.fmd.in_port, string);
110 if (pin.fmd.tun_id != htonll(0)) {
111 ds_put_format(string, " tun_id=0x%"PRIx64, ntohll(pin.fmd.tun_id));
114 if (pin.fmd.tun_src != htonl(0)) {
115 ds_put_format(string, " tun_src="IP_FMT, IP_ARGS(pin.fmd.tun_src));
118 if (pin.fmd.tun_dst != htonl(0)) {
119 ds_put_format(string, " tun_dst="IP_FMT, IP_ARGS(pin.fmd.tun_dst));
122 if (pin.fmd.metadata != htonll(0)) {
123 ds_put_format(string, " metadata=0x%"PRIx64, ntohll(pin.fmd.metadata));
126 for (i = 0; i < FLOW_N_REGS; i++) {
127 if (pin.fmd.regs[i]) {
128 ds_put_format(string, " reg%d=0x%"PRIx32, i, pin.fmd.regs[i]);
132 ds_put_format(string, " (via %s)",
133 ofputil_packet_in_reason_to_string(pin.reason));
135 ds_put_format(string, " data_len=%zu", pin.packet_len);
136 if (pin.buffer_id == UINT32_MAX) {
137 ds_put_format(string, " (unbuffered)");
138 if (pin.total_len != pin.packet_len) {
139 ds_put_format(string, " (***total_len != data_len***)");
142 ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
143 if (pin.total_len < pin.packet_len) {
144 ds_put_format(string, " (***total_len < data_len***)");
147 ds_put_char(string, '\n');
150 char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
151 ds_put_cstr(string, packet);
155 ds_put_hex_dump(string, pin.packet, pin.packet_len, 0, false);
160 ofp_print_packet_out(struct ds *string, const struct ofp_header *oh,
163 struct ofputil_packet_out po;
164 struct ofpbuf ofpacts;
167 ofpbuf_init(&ofpacts, 64);
168 error = ofputil_decode_packet_out(&po, oh, &ofpacts);
170 ofpbuf_uninit(&ofpacts);
171 ofp_print_error(string, error);
175 ds_put_cstr(string, " in_port=");
176 ofputil_format_port(po.in_port, string);
178 ds_put_char(string, ' ');
179 ofpacts_format(po.ofpacts, po.ofpacts_len, string);
181 if (po.buffer_id == UINT32_MAX) {
182 ds_put_format(string, " data_len=%zu", po.packet_len);
183 if (verbosity > 0 && po.packet_len > 0) {
184 char *packet = ofp_packet_to_string(po.packet, po.packet_len);
185 ds_put_char(string, '\n');
186 ds_put_cstr(string, packet);
190 ds_put_hex_dump(string, po.packet, po.packet_len, 0, false);
193 ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id);
196 ofpbuf_uninit(&ofpacts);
199 /* qsort comparison function. */
201 compare_ports(const void *a_, const void *b_)
203 const struct ofputil_phy_port *a = a_;
204 const struct ofputil_phy_port *b = b_;
205 uint16_t ap = a->port_no;
206 uint16_t bp = b->port_no;
208 return ap < bp ? -1 : ap > bp;
212 ofp_print_bit_names(struct ds *string, uint32_t bits,
213 const char *(*bit_to_name)(uint32_t bit),
220 ds_put_cstr(string, "0");
224 for (i = 0; i < 32; i++) {
225 uint32_t bit = UINT32_C(1) << i;
228 const char *name = bit_to_name(bit);
231 ds_put_char(string, separator);
233 ds_put_cstr(string, name);
241 ds_put_char(string, separator);
243 ds_put_format(string, "0x%"PRIx32, bits);
248 netdev_feature_to_name(uint32_t bit)
250 enum netdev_features f = bit;
253 case NETDEV_F_10MB_HD: return "10MB-HD";
254 case NETDEV_F_10MB_FD: return "10MB-FD";
255 case NETDEV_F_100MB_HD: return "100MB-HD";
256 case NETDEV_F_100MB_FD: return "100MB-FD";
257 case NETDEV_F_1GB_HD: return "1GB-HD";
258 case NETDEV_F_1GB_FD: return "1GB-FD";
259 case NETDEV_F_10GB_FD: return "10GB-FD";
260 case NETDEV_F_40GB_FD: return "40GB-FD";
261 case NETDEV_F_100GB_FD: return "100GB-FD";
262 case NETDEV_F_1TB_FD: return "1TB-FD";
263 case NETDEV_F_OTHER: return "OTHER";
264 case NETDEV_F_COPPER: return "COPPER";
265 case NETDEV_F_FIBER: return "FIBER";
266 case NETDEV_F_AUTONEG: return "AUTO_NEG";
267 case NETDEV_F_PAUSE: return "AUTO_PAUSE";
268 case NETDEV_F_PAUSE_ASYM: return "AUTO_PAUSE_ASYM";
275 ofp_print_port_features(struct ds *string, enum netdev_features features)
277 ofp_print_bit_names(string, features, netdev_feature_to_name, ' ');
278 ds_put_char(string, '\n');
282 ofputil_port_config_to_name(uint32_t bit)
284 enum ofputil_port_config pc = bit;
287 case OFPUTIL_PC_PORT_DOWN: return "PORT_DOWN";
288 case OFPUTIL_PC_NO_STP: return "NO_STP";
289 case OFPUTIL_PC_NO_RECV: return "NO_RECV";
290 case OFPUTIL_PC_NO_RECV_STP: return "NO_RECV_STP";
291 case OFPUTIL_PC_NO_FLOOD: return "NO_FLOOD";
292 case OFPUTIL_PC_NO_FWD: return "NO_FWD";
293 case OFPUTIL_PC_NO_PACKET_IN: return "NO_PACKET_IN";
300 ofp_print_port_config(struct ds *string, enum ofputil_port_config config)
302 ofp_print_bit_names(string, config, ofputil_port_config_to_name, ' ');
303 ds_put_char(string, '\n');
307 ofputil_port_state_to_name(uint32_t bit)
309 enum ofputil_port_state ps = bit;
312 case OFPUTIL_PS_LINK_DOWN: return "LINK_DOWN";
313 case OFPUTIL_PS_BLOCKED: return "BLOCKED";
314 case OFPUTIL_PS_LIVE: return "LIVE";
316 case OFPUTIL_PS_STP_LISTEN:
317 case OFPUTIL_PS_STP_LEARN:
318 case OFPUTIL_PS_STP_FORWARD:
319 case OFPUTIL_PS_STP_BLOCK:
320 /* Handled elsewhere. */
328 ofp_print_port_state(struct ds *string, enum ofputil_port_state state)
330 enum ofputil_port_state stp_state;
332 /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
333 * pattern. We have to special case it.
335 * OVS doesn't support STP, so this field will always be 0 if we are
336 * talking to OVS, so we'd always print STP_LISTEN in that case.
337 * Therefore, we don't print anything at all if the value is STP_LISTEN, to
338 * avoid confusing users. */
339 stp_state = state & OFPUTIL_PS_STP_MASK;
342 (stp_state == OFPUTIL_PS_STP_LEARN ? "STP_LEARN"
343 : stp_state == OFPUTIL_PS_STP_FORWARD ? "STP_FORWARD"
345 state &= ~OFPUTIL_PS_STP_MASK;
347 ofp_print_bit_names(string, state, ofputil_port_state_to_name,
351 ofp_print_bit_names(string, state, ofputil_port_state_to_name, ' ');
353 ds_put_char(string, '\n');
357 ofp_print_phy_port(struct ds *string, const struct ofputil_phy_port *port)
359 char name[sizeof port->name];
362 memcpy(name, port->name, sizeof name);
363 for (j = 0; j < sizeof name - 1; j++) {
364 if (!isprint((unsigned char) name[j])) {
370 ds_put_char(string, ' ');
371 ofputil_format_port(port->port_no, string);
372 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
373 name, ETH_ADDR_ARGS(port->hw_addr));
375 ds_put_cstr(string, " config: ");
376 ofp_print_port_config(string, port->config);
378 ds_put_cstr(string, " state: ");
379 ofp_print_port_state(string, port->state);
382 ds_put_format(string, " current: ");
383 ofp_print_port_features(string, port->curr);
385 if (port->advertised) {
386 ds_put_format(string, " advertised: ");
387 ofp_print_port_features(string, port->advertised);
389 if (port->supported) {
390 ds_put_format(string, " supported: ");
391 ofp_print_port_features(string, port->supported);
394 ds_put_format(string, " peer: ");
395 ofp_print_port_features(string, port->peer);
397 ds_put_format(string, " speed: %"PRIu32" Mbps now, "
398 "%"PRIu32" Mbps max\n",
399 port->curr_speed / UINT32_C(1000),
400 port->max_speed / UINT32_C(1000));
403 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
404 * 'ofp_version', writes a detailed description of each port into
407 ofp_print_phy_ports(struct ds *string, uint8_t ofp_version,
411 struct ofputil_phy_port *ports;
415 n_ports = ofputil_count_phy_ports(ofp_version, b);
417 ports = xmalloc(n_ports * sizeof *ports);
418 for (i = 0; i < n_ports; i++) {
419 error = ofputil_pull_phy_port(ofp_version, b, &ports[i]);
421 ofp_print_error(string, error);
425 qsort(ports, n_ports, sizeof *ports, compare_ports);
426 for (i = 0; i < n_ports; i++) {
427 ofp_print_phy_port(string, &ports[i]);
435 ofputil_capabilities_to_name(uint32_t bit)
437 enum ofputil_capabilities capabilities = bit;
439 switch (capabilities) {
440 case OFPUTIL_C_FLOW_STATS: return "FLOW_STATS";
441 case OFPUTIL_C_TABLE_STATS: return "TABLE_STATS";
442 case OFPUTIL_C_PORT_STATS: return "PORT_STATS";
443 case OFPUTIL_C_IP_REASM: return "IP_REASM";
444 case OFPUTIL_C_QUEUE_STATS: return "QUEUE_STATS";
445 case OFPUTIL_C_ARP_MATCH_IP: return "ARP_MATCH_IP";
446 case OFPUTIL_C_STP: return "STP";
447 case OFPUTIL_C_GROUP_STATS: return "GROUP_STATS";
448 case OFPUTIL_C_PORT_BLOCKED: return "PORT_BLOCKED";
455 ofputil_action_bitmap_to_name(uint32_t bit)
457 enum ofputil_action_bitmap action = bit;
460 case OFPUTIL_A_OUTPUT: return "OUTPUT";
461 case OFPUTIL_A_SET_VLAN_VID: return "SET_VLAN_VID";
462 case OFPUTIL_A_SET_VLAN_PCP: return "SET_VLAN_PCP";
463 case OFPUTIL_A_STRIP_VLAN: return "STRIP_VLAN";
464 case OFPUTIL_A_SET_DL_SRC: return "SET_DL_SRC";
465 case OFPUTIL_A_SET_DL_DST: return "SET_DL_DST";
466 case OFPUTIL_A_SET_NW_SRC: return "SET_NW_SRC";
467 case OFPUTIL_A_SET_NW_DST: return "SET_NW_DST";
468 case OFPUTIL_A_SET_NW_ECN: return "SET_NW_ECN";
469 case OFPUTIL_A_SET_NW_TOS: return "SET_NW_TOS";
470 case OFPUTIL_A_SET_TP_SRC: return "SET_TP_SRC";
471 case OFPUTIL_A_SET_TP_DST: return "SET_TP_DST";
472 case OFPUTIL_A_SET_FIELD: return "SET_FIELD";
473 case OFPUTIL_A_ENQUEUE: return "ENQUEUE";
474 case OFPUTIL_A_COPY_TTL_OUT: return "COPY_TTL_OUT";
475 case OFPUTIL_A_COPY_TTL_IN: return "COPY_TTL_IN";
476 case OFPUTIL_A_SET_MPLS_LABEL: return "SET_MPLS_LABEL";
477 case OFPUTIL_A_SET_MPLS_TC: return "SET_MPLS_TC";
478 case OFPUTIL_A_SET_MPLS_TTL: return "SET_MPLS_TTL";
479 case OFPUTIL_A_DEC_MPLS_TTL: return "DEC_MPLS_TTL";
480 case OFPUTIL_A_PUSH_VLAN: return "PUSH_VLAN";
481 case OFPUTIL_A_POP_VLAN: return "POP_VLAN";
482 case OFPUTIL_A_PUSH_MPLS: return "PUSH_MPLS";
483 case OFPUTIL_A_POP_MPLS: return "POP_MPLS";
484 case OFPUTIL_A_SET_QUEUE: return "SET_QUEUE";
485 case OFPUTIL_A_GROUP: return "GROUP";
486 case OFPUTIL_A_SET_NW_TTL: return "SET_NW_TTL";
487 case OFPUTIL_A_DEC_NW_TTL: return "DEC_NW_TTL";
494 ofp_print_switch_features(struct ds *string, const struct ofp_header *oh)
496 struct ofputil_switch_features features;
500 error = ofputil_decode_switch_features(oh, &features, &b);
502 ofp_print_error(string, error);
506 ds_put_format(string, " dpid:%016"PRIx64"\n", features.datapath_id);
508 ds_put_format(string, "n_tables:%"PRIu8", n_buffers:%"PRIu32,
509 features.n_tables, features.n_buffers);
510 if (features.auxiliary_id) {
511 ds_put_format(string, ", auxiliary_id:%"PRIu8, features.auxiliary_id);
513 ds_put_char(string, '\n');
515 ds_put_cstr(string, "capabilities: ");
516 ofp_print_bit_names(string, features.capabilities,
517 ofputil_capabilities_to_name, ' ');
518 ds_put_char(string, '\n');
520 switch ((enum ofp_version)oh->version) {
522 ds_put_cstr(string, "actions: ");
523 ofp_print_bit_names(string, features.actions,
524 ofputil_action_bitmap_to_name, ' ');
525 ds_put_char(string, '\n');
531 return; /* no ports in ofp13_switch_features */
536 ofp_print_phy_ports(string, oh->version, &b);
540 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
542 enum ofp_config_flags flags;
544 flags = ntohs(osc->flags);
546 ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
547 flags &= ~OFPC_FRAG_MASK;
549 if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) {
550 ds_put_format(string, " invalid_ttl_to_controller");
551 flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER;
555 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
558 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
561 static void print_wild(struct ds *string, const char *leader, int is_wild,
562 int verbosity, const char *format, ...)
563 __attribute__((format(printf, 5, 6)));
565 static void print_wild(struct ds *string, const char *leader, int is_wild,
566 int verbosity, const char *format, ...)
568 if (is_wild && verbosity < 2) {
571 ds_put_cstr(string, leader);
575 va_start(args, format);
576 ds_put_format_valist(string, format, args);
579 ds_put_char(string, '*');
581 ds_put_char(string, ',');
585 print_wild_port(struct ds *string, const char *leader, int is_wild,
586 int verbosity, uint16_t port)
588 if (is_wild && verbosity < 2) {
591 ds_put_cstr(string, leader);
593 ofputil_format_port(port, string);
595 ds_put_char(string, '*');
597 ds_put_char(string, ',');
601 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
602 uint32_t wild_bits, int verbosity)
604 if (wild_bits >= 32 && verbosity < 2) {
607 ds_put_cstr(string, leader);
608 if (wild_bits < 32) {
609 ds_put_format(string, IP_FMT, IP_ARGS(ip));
611 ds_put_format(string, "/%d", 32 - wild_bits);
614 ds_put_char(string, '*');
616 ds_put_char(string, ',');
620 ofp10_match_print(struct ds *f, const struct ofp10_match *om, int verbosity)
622 char *s = ofp10_match_to_string(om, verbosity);
628 ofp10_match_to_string(const struct ofp10_match *om, int verbosity)
630 struct ds f = DS_EMPTY_INITIALIZER;
631 uint32_t w = ntohl(om->wildcards);
632 bool skip_type = false;
633 bool skip_proto = false;
635 if (!(w & OFPFW10_DL_TYPE)) {
637 if (om->dl_type == htons(ETH_TYPE_IP)) {
638 if (!(w & OFPFW10_NW_PROTO)) {
640 if (om->nw_proto == IPPROTO_ICMP) {
641 ds_put_cstr(&f, "icmp,");
642 } else if (om->nw_proto == IPPROTO_TCP) {
643 ds_put_cstr(&f, "tcp,");
644 } else if (om->nw_proto == IPPROTO_UDP) {
645 ds_put_cstr(&f, "udp,");
647 ds_put_cstr(&f, "ip,");
651 ds_put_cstr(&f, "ip,");
653 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
654 ds_put_cstr(&f, "arp,");
655 } else if (om->dl_type == htons(ETH_TYPE_RARP)){
656 ds_put_cstr(&f, "rarp,");
657 } else if (om->dl_type == htons(ETH_TYPE_MPLS)) {
658 ds_put_cstr(&f, "mpls,");
659 } else if (om->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
660 ds_put_cstr(&f, "mplsm,");
665 print_wild_port(&f, "in_port=", w & OFPFW10_IN_PORT, verbosity,
667 print_wild(&f, "dl_vlan=", w & OFPFW10_DL_VLAN, verbosity,
668 "%d", ntohs(om->dl_vlan));
669 print_wild(&f, "dl_vlan_pcp=", w & OFPFW10_DL_VLAN_PCP, verbosity,
670 "%d", om->dl_vlan_pcp);
671 print_wild(&f, "dl_src=", w & OFPFW10_DL_SRC, verbosity,
672 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
673 print_wild(&f, "dl_dst=", w & OFPFW10_DL_DST, verbosity,
674 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
676 print_wild(&f, "dl_type=", w & OFPFW10_DL_TYPE, verbosity,
677 "0x%04x", ntohs(om->dl_type));
679 print_ip_netmask(&f, "nw_src=", om->nw_src,
680 (w & OFPFW10_NW_SRC_MASK) >> OFPFW10_NW_SRC_SHIFT,
682 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
683 (w & OFPFW10_NW_DST_MASK) >> OFPFW10_NW_DST_SHIFT,
686 if (om->dl_type == htons(ETH_TYPE_ARP) ||
687 om->dl_type == htons(ETH_TYPE_RARP)) {
688 print_wild(&f, "arp_op=", w & OFPFW10_NW_PROTO, verbosity,
691 print_wild(&f, "nw_proto=", w & OFPFW10_NW_PROTO, verbosity,
695 print_wild(&f, "nw_tos=", w & OFPFW10_NW_TOS, verbosity,
697 if (om->nw_proto == IPPROTO_ICMP) {
698 print_wild(&f, "icmp_type=", w & OFPFW10_ICMP_TYPE, verbosity,
699 "%d", ntohs(om->tp_src));
700 print_wild(&f, "icmp_code=", w & OFPFW10_ICMP_CODE, verbosity,
701 "%d", ntohs(om->tp_dst));
703 print_wild(&f, "tp_src=", w & OFPFW10_TP_SRC, verbosity,
704 "%d", ntohs(om->tp_src));
705 print_wild(&f, "tp_dst=", w & OFPFW10_TP_DST, verbosity,
706 "%d", ntohs(om->tp_dst));
708 if (ds_last(&f) == ',') {
715 ofp_print_flow_flags(struct ds *s, uint16_t flags)
717 if (flags & OFPFF_SEND_FLOW_REM) {
718 ds_put_cstr(s, "send_flow_rem ");
720 if (flags & OFPFF_CHECK_OVERLAP) {
721 ds_put_cstr(s, "check_overlap ");
723 if (flags & OFPFF12_RESET_COUNTS) {
724 ds_put_cstr(s, "reset_counts ");
726 if (flags & OFPFF13_NO_PKT_COUNTS) {
727 ds_put_cstr(s, "no_packet_counts ");
729 if (flags & OFPFF13_NO_BYT_COUNTS) {
730 ds_put_cstr(s, "no_byte_counts ");
733 flags &= ~(OFPFF_SEND_FLOW_REM | OFPFF_CHECK_OVERLAP
734 | OFPFF12_RESET_COUNTS
735 | OFPFF13_NO_PKT_COUNTS | OFPFF13_NO_BYT_COUNTS);
737 ds_put_format(s, "flags:0x%"PRIx16" ", flags);
742 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh, int verbosity)
744 struct ofputil_flow_mod fm;
745 struct ofpbuf ofpacts;
749 enum ofputil_protocol protocol;
751 protocol = ofputil_protocol_from_ofp_version(oh->version);
752 protocol = ofputil_protocol_set_tid(protocol, true);
754 ofpbuf_init(&ofpacts, 64);
755 error = ofputil_decode_flow_mod(&fm, oh, protocol, &ofpacts);
757 ofpbuf_uninit(&ofpacts);
758 ofp_print_error(s, error);
763 switch (fm.command) {
765 ds_put_cstr(s, "ADD");
768 ds_put_cstr(s, "MOD");
770 case OFPFC_MODIFY_STRICT:
771 ds_put_cstr(s, "MOD_STRICT");
774 ds_put_cstr(s, "DEL");
776 case OFPFC_DELETE_STRICT:
777 ds_put_cstr(s, "DEL_STRICT");
780 ds_put_format(s, "cmd:%d", fm.command);
782 if (fm.table_id != 0) {
783 ds_put_format(s, " table:%d", fm.table_id);
787 ofpraw_decode(&raw, oh);
788 if (verbosity >= 3 && raw == OFPRAW_OFPT10_FLOW_MOD) {
789 const struct ofp10_flow_mod *ofm = ofpmsg_body(oh);
790 ofp10_match_print(s, &ofm->match, verbosity);
792 /* ofp_print_match() doesn't print priority. */
793 need_priority = true;
794 } else if (verbosity >= 3 && raw == OFPRAW_NXT_FLOW_MOD) {
795 const struct nx_flow_mod *nfm = ofpmsg_body(oh);
796 const void *nxm = nfm + 1;
799 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
800 ds_put_cstr(s, nxm_s);
803 /* nx_match_to_string() doesn't print priority. */
804 need_priority = true;
806 match_format(&fm.match, s, fm.priority);
808 /* match_format() does print priority. */
809 need_priority = false;
812 if (ds_last(s) != ' ') {
815 if (fm.new_cookie != htonll(0) && fm.new_cookie != htonll(UINT64_MAX)) {
816 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.new_cookie));
818 if (fm.cookie_mask != htonll(0)) {
819 ds_put_format(s, "cookie:0x%"PRIx64"/0x%"PRIx64" ",
820 ntohll(fm.cookie), ntohll(fm.cookie_mask));
822 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
823 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
825 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
826 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
828 if (fm.priority != OFP_DEFAULT_PRIORITY && need_priority) {
829 ds_put_format(s, "pri:%"PRIu16" ", fm.priority);
831 if (fm.buffer_id != UINT32_MAX) {
832 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
834 if (fm.out_port != OFPP_ANY) {
835 ds_put_format(s, "out_port:");
836 ofputil_format_port(fm.out_port, s);
840 ofp_print_flow_flags(s, fm.flags);
843 ofpacts_format(fm.ofpacts, fm.ofpacts_len, s);
844 ofpbuf_uninit(&ofpacts);
848 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
850 ds_put_format(string, "%u", sec);
852 ds_put_format(string, ".%09u", nsec);
853 while (string->string[string->length - 1] == '0') {
857 ds_put_char(string, 's');
861 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason)
866 case OFPRR_IDLE_TIMEOUT:
868 case OFPRR_HARD_TIMEOUT:
872 case OFPRR_GROUP_DELETE:
873 return "group_delete";
877 sprintf(s, "%d", (int) reason);
883 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
885 struct ofputil_flow_removed fr;
888 error = ofputil_decode_flow_removed(&fr, oh);
890 ofp_print_error(string, error);
894 ds_put_char(string, ' ');
895 match_format(&fr.match, string, fr.priority);
897 ds_put_format(string, " reason=%s",
898 ofp_flow_removed_reason_to_string(fr.reason));
900 if (fr.table_id != 255) {
901 ds_put_format(string, " table_id=%"PRIu8, fr.table_id);
904 if (fr.cookie != htonll(0)) {
905 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
907 ds_put_cstr(string, " duration");
908 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
909 ds_put_format(string, " idle%"PRIu16, fr.idle_timeout);
910 if (fr.hard_timeout) {
911 /* The hard timeout was only added in OF1.2, so only print it if it is
912 * actually in use to avoid gratuitous change to the formatting. */
913 ds_put_format(string, " hard%"PRIu16, fr.hard_timeout);
915 ds_put_format(string, " pkts%"PRIu64" bytes%"PRIu64"\n",
916 fr.packet_count, fr.byte_count);
920 ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
922 struct ofputil_port_mod pm;
925 error = ofputil_decode_port_mod(oh, &pm);
927 ofp_print_error(string, error);
931 ds_put_cstr(string, "port: ");
932 ofputil_format_port(pm.port_no, string);
933 ds_put_format(string, ": addr:"ETH_ADDR_FMT"\n",
934 ETH_ADDR_ARGS(pm.hw_addr));
936 ds_put_cstr(string, " config: ");
937 ofp_print_port_config(string, pm.config);
939 ds_put_cstr(string, " mask: ");
940 ofp_print_port_config(string, pm.mask);
942 ds_put_cstr(string, " advertise: ");
944 ofp_print_port_features(string, pm.advertise);
946 ds_put_cstr(string, "UNCHANGED\n");
951 ofp_print_error(struct ds *string, enum ofperr error)
953 if (string->length) {
954 ds_put_char(string, ' ');
956 ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
960 ofp_print_hello(struct ds *string, const struct ofp_header *oh)
962 uint32_t allowed_versions;
965 ok = ofputil_decode_hello(oh, &allowed_versions);
967 ds_put_cstr(string, "\n version bitmap: ");
968 ofputil_format_version_bitmap(string, allowed_versions);
971 ds_put_cstr(string, "\n unknown data in hello:\n");
972 ds_put_hex_dump(string, oh, ntohs(oh->length), 0, true);
977 ofp_print_error_msg(struct ds *string, const struct ofp_header *oh)
979 size_t len = ntohs(oh->length);
980 struct ofpbuf payload;
984 error = ofperr_decode_msg(oh, &payload);
986 ds_put_cstr(string, "***decode error***");
987 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
991 ds_put_format(string, " %s\n", ofperr_get_name(error));
993 if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
994 ds_put_printable(string, payload.data, payload.size);
996 s = ofp_to_string(payload.data, payload.size, 1);
997 ds_put_cstr(string, s);
1003 ofp_print_port_status(struct ds *string, const struct ofp_header *oh)
1005 struct ofputil_port_status ps;
1008 error = ofputil_decode_port_status(oh, &ps);
1010 ofp_print_error(string, error);
1014 if (ps.reason == OFPPR_ADD) {
1015 ds_put_format(string, " ADD:");
1016 } else if (ps.reason == OFPPR_DELETE) {
1017 ds_put_format(string, " DEL:");
1018 } else if (ps.reason == OFPPR_MODIFY) {
1019 ds_put_format(string, " MOD:");
1022 ofp_print_phy_port(string, &ps.desc);
1026 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
1028 const struct ofp_desc_stats *ods = ofpmsg_body(oh);
1030 ds_put_char(string, '\n');
1031 ds_put_format(string, "Manufacturer: %.*s\n",
1032 (int) sizeof ods->mfr_desc, ods->mfr_desc);
1033 ds_put_format(string, "Hardware: %.*s\n",
1034 (int) sizeof ods->hw_desc, ods->hw_desc);
1035 ds_put_format(string, "Software: %.*s\n",
1036 (int) sizeof ods->sw_desc, ods->sw_desc);
1037 ds_put_format(string, "Serial Num: %.*s\n",
1038 (int) sizeof ods->serial_num, ods->serial_num);
1039 ds_put_format(string, "DP Description: %.*s\n",
1040 (int) sizeof ods->dp_desc, ods->dp_desc);
1044 ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
1046 struct ofputil_flow_stats_request fsr;
1049 error = ofputil_decode_flow_stats_request(&fsr, oh);
1051 ofp_print_error(string, error);
1055 if (fsr.table_id != 0xff) {
1056 ds_put_format(string, " table=%"PRIu8, fsr.table_id);
1059 if (fsr.out_port != OFPP_ANY) {
1060 ds_put_cstr(string, " out_port=");
1061 ofputil_format_port(fsr.out_port, string);
1064 ds_put_char(string, ' ');
1065 match_format(&fsr.match, string, OFP_DEFAULT_PRIORITY);
1069 ofp_print_flow_stats(struct ds *string, struct ofputil_flow_stats *fs)
1071 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1072 ntohll(fs->cookie));
1074 ofp_print_duration(string, fs->duration_sec, fs->duration_nsec);
1075 ds_put_format(string, ", table=%"PRIu8", ", fs->table_id);
1076 ds_put_format(string, "n_packets=%"PRIu64", ", fs->packet_count);
1077 ds_put_format(string, "n_bytes=%"PRIu64", ", fs->byte_count);
1078 if (fs->idle_timeout != OFP_FLOW_PERMANENT) {
1079 ds_put_format(string, "idle_timeout=%"PRIu16", ", fs->idle_timeout);
1081 if (fs->hard_timeout != OFP_FLOW_PERMANENT) {
1082 ds_put_format(string, "hard_timeout=%"PRIu16", ", fs->hard_timeout);
1085 ofp_print_flow_flags(string, fs->flags);
1087 if (fs->idle_age >= 0) {
1088 ds_put_format(string, "idle_age=%d, ", fs->idle_age);
1090 if (fs->hard_age >= 0 && fs->hard_age != fs->duration_sec) {
1091 ds_put_format(string, "hard_age=%d, ", fs->hard_age);
1094 match_format(&fs->match, string, fs->priority);
1095 if (string->string[string->length - 1] != ' ') {
1096 ds_put_char(string, ' ');
1099 ofpacts_format(fs->ofpacts, fs->ofpacts_len, string);
1103 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1105 struct ofpbuf ofpacts;
1108 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1109 ofpbuf_init(&ofpacts, 64);
1111 struct ofputil_flow_stats fs;
1114 retval = ofputil_decode_flow_stats_reply(&fs, &b, true, &ofpacts);
1116 if (retval != EOF) {
1117 ds_put_cstr(string, " ***parse error***");
1121 ds_put_char(string, '\n');
1122 ofp_print_flow_stats(string, &fs);
1124 ofpbuf_uninit(&ofpacts);
1128 ofp_print_aggregate_stats_reply(struct ds *string, const struct ofp_header *oh)
1130 struct ofputil_aggregate_stats as;
1133 error = ofputil_decode_aggregate_stats_reply(&as, oh);
1135 ofp_print_error(string, error);
1139 ds_put_format(string, " packet_count=%"PRIu64, as.packet_count);
1140 ds_put_format(string, " byte_count=%"PRIu64, as.byte_count);
1141 ds_put_format(string, " flow_count=%"PRIu32, as.flow_count);
1145 print_port_stat(struct ds *string, const char *leader, uint64_t stat, int more)
1147 ds_put_cstr(string, leader);
1148 if (stat != UINT64_MAX) {
1149 ds_put_format(string, "%"PRIu64, stat);
1151 ds_put_char(string, '?');
1154 ds_put_cstr(string, ", ");
1156 ds_put_cstr(string, "\n");
1161 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1163 uint16_t ofp10_port;
1166 error = ofputil_decode_port_stats_request(oh, &ofp10_port);
1168 ofp_print_error(string, error);
1172 ds_put_cstr(string, " port_no=");
1173 ofputil_format_port(ofp10_port, string);
1177 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1182 ds_put_format(string, " %zu ports\n", ofputil_count_port_stats(oh));
1183 if (verbosity < 1) {
1187 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1189 struct ofputil_port_stats ps;
1192 retval = ofputil_decode_port_stats(&ps, &b);
1194 if (retval != EOF) {
1195 ds_put_cstr(string, " ***parse error***");
1200 ds_put_cstr(string, " port ");
1201 if (ps.port_no < 10) {
1202 ds_put_char(string, ' ');
1204 ofputil_format_port(ps.port_no, string);
1206 ds_put_cstr(string, ": rx ");
1207 print_port_stat(string, "pkts=", ps.stats.rx_packets, 1);
1208 print_port_stat(string, "bytes=", ps.stats.rx_bytes, 1);
1209 print_port_stat(string, "drop=", ps.stats.rx_dropped, 1);
1210 print_port_stat(string, "errs=", ps.stats.rx_errors, 1);
1211 print_port_stat(string, "frame=", ps.stats.rx_frame_errors, 1);
1212 print_port_stat(string, "over=", ps.stats.rx_over_errors, 1);
1213 print_port_stat(string, "crc=", ps.stats.rx_crc_errors, 0);
1215 ds_put_cstr(string, " tx ");
1216 print_port_stat(string, "pkts=", ps.stats.tx_packets, 1);
1217 print_port_stat(string, "bytes=", ps.stats.tx_bytes, 1);
1218 print_port_stat(string, "drop=", ps.stats.tx_dropped, 1);
1219 print_port_stat(string, "errs=", ps.stats.tx_errors, 1);
1220 print_port_stat(string, "coll=", ps.stats.collisions, 0);
1222 if (ps.duration_sec != UINT32_MAX) {
1223 ds_put_cstr(string, " duration=");
1224 ofp_print_duration(string, ps.duration_sec, ps.duration_nsec);
1225 ds_put_char(string, '\n');
1231 ofp_print_one_ofpst_table_reply(struct ds *string, enum ofp_version ofp_version,
1232 const char *name, struct ofp12_table_stats *ts)
1234 char name_[OFP_MAX_TABLE_NAME_LEN + 1];
1236 /* ofp13_table_stats is different */
1237 if (ofp_version > OFP12_VERSION) {
1241 ovs_strlcpy(name_, name, sizeof name_);
1243 ds_put_format(string, " %d: %-8s: ", ts->table_id, name_);
1244 ds_put_format(string, "wild=0x%05"PRIx64", ", ntohll(ts->wildcards));
1245 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1246 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1247 ds_put_cstr(string, " ");
1248 ds_put_format(string, "lookup=%"PRIu64", ", ntohll(ts->lookup_count));
1249 ds_put_format(string, "matched=%"PRIu64"\n", ntohll(ts->matched_count));
1251 if (ofp_version < OFP11_VERSION) {
1255 ds_put_cstr(string, " ");
1256 ds_put_format(string, "match=0x%08"PRIx64", ", ntohll(ts->match));
1257 ds_put_format(string, "instructions=0x%08"PRIx32", ",
1258 ntohl(ts->instructions));
1259 ds_put_format(string, "config=0x%08"PRIx32"\n", ntohl(ts->config));
1260 ds_put_cstr(string, " ");
1261 ds_put_format(string, "write_actions=0x%08"PRIx32", ",
1262 ntohl(ts->write_actions));
1263 ds_put_format(string, "apply_actions=0x%08"PRIx32"\n",
1264 ntohl(ts->apply_actions));
1266 if (ofp_version < OFP12_VERSION) {
1270 ds_put_cstr(string, " ");
1271 ds_put_format(string, "write_setfields=0x%016"PRIx64"\n",
1272 ntohll(ts->write_setfields));
1273 ds_put_cstr(string, " ");
1274 ds_put_format(string, "apply_setfields=0x%016"PRIx64"\n",
1275 ntohll(ts->apply_setfields));
1276 ds_put_cstr(string, " ");
1277 ds_put_format(string, "metadata_match=0x%016"PRIx64"\n",
1278 ntohll(ts->metadata_match));
1279 ds_put_cstr(string, " ");
1280 ds_put_format(string, "metadata_write=0x%016"PRIx64"\n",
1281 ntohll(ts->metadata_write));
1285 ofp_print_ofpst_table_reply13(struct ds *string, const struct ofp_header *oh,
1288 struct ofp13_table_stats *ts;
1292 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1293 ofpraw_pull_assert(&b);
1295 n = b.size / sizeof *ts;
1296 ds_put_format(string, " %zu tables\n", n);
1297 if (verbosity < 1) {
1302 ts = ofpbuf_try_pull(&b, sizeof *ts);
1306 ds_put_format(string,
1307 " %d: active=%"PRIu32", lookup=%"PRIu64 \
1308 ", matched=%"PRIu64"\n",
1309 ts->table_id, ntohl(ts->active_count),
1310 ntohll(ts->lookup_count), ntohll(ts->matched_count));
1315 ofp_print_ofpst_table_reply12(struct ds *string, const struct ofp_header *oh,
1318 struct ofp12_table_stats *ts;
1322 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1323 ofpraw_pull_assert(&b);
1325 n = b.size / sizeof *ts;
1326 ds_put_format(string, " %zu tables\n", n);
1327 if (verbosity < 1) {
1332 ts = ofpbuf_try_pull(&b, sizeof *ts);
1337 ofp_print_one_ofpst_table_reply(string, OFP12_VERSION, ts->name, ts);
1342 ofp_print_ofpst_table_reply11(struct ds *string, const struct ofp_header *oh,
1345 struct ofp11_table_stats *ts;
1349 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1350 ofpraw_pull_assert(&b);
1352 n = b.size / sizeof *ts;
1353 ds_put_format(string, " %zu tables\n", n);
1354 if (verbosity < 1) {
1359 struct ofp12_table_stats ts12;
1361 ts = ofpbuf_try_pull(&b, sizeof *ts);
1366 ts12.table_id = ts->table_id;
1367 ts12.wildcards = htonll(ntohl(ts->wildcards));
1368 ts12.max_entries = ts->max_entries;
1369 ts12.active_count = ts->active_count;
1370 ts12.lookup_count = ts->lookup_count;
1371 ts12.matched_count = ts->matched_count;
1372 ts12.match = htonll(ntohl(ts->match));
1373 ts12.instructions = ts->instructions;
1374 ts12.config = ts->config;
1375 ts12.write_actions = ts->write_actions;
1376 ts12.apply_actions = ts->apply_actions;
1377 ofp_print_one_ofpst_table_reply(string, OFP11_VERSION, ts->name, &ts12);
1382 ofp_print_ofpst_table_reply10(struct ds *string, const struct ofp_header *oh,
1385 struct ofp10_table_stats *ts;
1389 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1390 ofpraw_pull_assert(&b);
1392 n = b.size / sizeof *ts;
1393 ds_put_format(string, " %zu tables\n", n);
1394 if (verbosity < 1) {
1399 struct ofp12_table_stats ts12;
1401 ts = ofpbuf_try_pull(&b, sizeof *ts);
1406 ts12.table_id = ts->table_id;
1407 ts12.wildcards = htonll(ntohl(ts->wildcards));
1408 ts12.max_entries = ts->max_entries;
1409 ts12.active_count = ts->active_count;
1410 ts12.lookup_count = get_32aligned_be64(&ts->lookup_count);
1411 ts12.matched_count = get_32aligned_be64(&ts->matched_count);
1412 ofp_print_one_ofpst_table_reply(string, OFP10_VERSION, ts->name, &ts12);
1417 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1420 switch ((enum ofp_version)oh->version) {
1422 ofp_print_ofpst_table_reply13(string, oh, verbosity);
1426 ofp_print_ofpst_table_reply12(string, oh, verbosity);
1430 ofp_print_ofpst_table_reply11(string, oh, verbosity);
1434 ofp_print_ofpst_table_reply10(string, oh, verbosity);
1443 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1445 if (queue_id == OFPQ_ALL) {
1446 ds_put_cstr(string, "ALL");
1448 ds_put_format(string, "%"PRIu32, queue_id);
1453 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1455 struct ofputil_queue_stats_request oqsr;
1458 error = ofputil_decode_queue_stats_request(oh, &oqsr);
1460 ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1464 ds_put_cstr(string, "port=");
1465 ofputil_format_port(oqsr.port_no, string);
1467 ds_put_cstr(string, " queue=");
1468 ofp_print_queue_name(string, oqsr.queue_id);
1472 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1477 ds_put_format(string, " %zu queues\n", ofputil_count_queue_stats(oh));
1478 if (verbosity < 1) {
1482 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1484 struct ofputil_queue_stats qs;
1487 retval = ofputil_decode_queue_stats(&qs, &b);
1489 if (retval != EOF) {
1490 ds_put_cstr(string, " ***parse error***");
1495 ds_put_cstr(string, " port ");
1496 ofputil_format_port(qs.port_no, string);
1497 ds_put_cstr(string, " queue ");
1498 ofp_print_queue_name(string, qs.queue_id);
1499 ds_put_cstr(string, ": ");
1501 print_port_stat(string, "bytes=", qs.stats.tx_bytes, 1);
1502 print_port_stat(string, "pkts=", qs.stats.tx_packets, 1);
1503 print_port_stat(string, "errors=", qs.stats.tx_errors, 0);
1508 ofp_print_ofpst_port_desc_reply(struct ds *string,
1509 const struct ofp_header *oh)
1513 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1514 ofpraw_pull_assert(&b);
1515 ds_put_char(string, '\n');
1516 ofp_print_phy_ports(string, oh->version, &b);
1520 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1522 uint16_t flags = ofpmp_flags(oh);
1525 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
1530 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1532 uint16_t flags = ofpmp_flags(oh);
1535 ds_put_cstr(string, " flags=");
1536 if (flags & OFPSF_REPLY_MORE) {
1537 ds_put_cstr(string, "[more]");
1538 flags &= ~OFPSF_REPLY_MORE;
1541 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1548 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1550 size_t len = ntohs(oh->length);
1552 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1553 if (verbosity > 1) {
1554 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1559 ofp_print_role_message(struct ds *string, const struct ofp_header *oh)
1561 struct ofputil_role_request rr;
1564 error = ofputil_decode_role_message(oh, &rr);
1566 ofp_print_error(string, error);
1570 ds_put_cstr(string, " role=");
1573 case OFPCR12_ROLE_NOCHANGE:
1574 ds_put_cstr(string, "nochange");
1576 case OFPCR12_ROLE_EQUAL:
1577 ds_put_cstr(string, "equal"); /* OF 1.2 wording */
1579 case OFPCR12_ROLE_MASTER:
1580 ds_put_cstr(string, "master");
1582 case OFPCR12_ROLE_SLAVE:
1583 ds_put_cstr(string, "slave");
1589 if (rr.have_generation_id) {
1590 ds_put_format(string, " generation_id=%"PRIu64, rr.generation_id);
1595 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1596 const struct nx_flow_mod_table_id *nfmti)
1598 ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1602 ofp_print_nxt_set_flow_format(struct ds *string,
1603 const struct nx_set_flow_format *nsff)
1605 uint32_t format = ntohl(nsff->format);
1607 ds_put_cstr(string, " format=");
1608 if (ofputil_nx_flow_format_is_valid(format)) {
1609 ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
1611 ds_put_format(string, "%"PRIu32, format);
1616 ofp_print_nxt_set_packet_in_format(struct ds *string,
1617 const struct nx_set_packet_in_format *nspf)
1619 uint32_t format = ntohl(nspf->format);
1621 ds_put_cstr(string, " format=");
1622 if (ofputil_packet_in_format_is_valid(format)) {
1623 ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1625 ds_put_format(string, "%"PRIu32, format);
1630 ofp_port_reason_to_string(enum ofp_port_reason reason)
1645 sprintf(s, "%d", (int) reason);
1651 ofp_print_nxt_set_async_config(struct ds *string,
1652 const struct nx_async_config *nac)
1656 for (i = 0; i < 2; i++) {
1659 ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1661 ds_put_cstr(string, " PACKET_IN:");
1662 for (j = 0; j < 32; j++) {
1663 if (nac->packet_in_mask[i] & htonl(1u << j)) {
1664 ds_put_format(string, " %s",
1665 ofputil_packet_in_reason_to_string(j));
1668 if (!nac->packet_in_mask[i]) {
1669 ds_put_cstr(string, " (off)");
1671 ds_put_char(string, '\n');
1673 ds_put_cstr(string, " PORT_STATUS:");
1674 for (j = 0; j < 32; j++) {
1675 if (nac->port_status_mask[i] & htonl(1u << j)) {
1676 ds_put_format(string, " %s", ofp_port_reason_to_string(j));
1679 if (!nac->port_status_mask[i]) {
1680 ds_put_cstr(string, " (off)");
1682 ds_put_char(string, '\n');
1684 ds_put_cstr(string, " FLOW_REMOVED:");
1685 for (j = 0; j < 32; j++) {
1686 if (nac->flow_removed_mask[i] & htonl(1u << j)) {
1687 ds_put_format(string, " %s",
1688 ofp_flow_removed_reason_to_string(j));
1691 if (!nac->flow_removed_mask[i]) {
1692 ds_put_cstr(string, " (off)");
1694 ds_put_char(string, '\n');
1699 ofp_print_nxt_set_controller_id(struct ds *string,
1700 const struct nx_controller_id *nci)
1702 ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
1706 ofp_print_nxt_flow_monitor_cancel(struct ds *string,
1707 const struct ofp_header *oh)
1709 ds_put_format(string, " id=%"PRIu32,
1710 ofputil_decode_flow_monitor_cancel(oh));
1714 nx_flow_monitor_flags_to_name(uint32_t bit)
1716 enum nx_flow_monitor_flags fmf = bit;
1719 case NXFMF_INITIAL: return "initial";
1720 case NXFMF_ADD: return "add";
1721 case NXFMF_DELETE: return "delete";
1722 case NXFMF_MODIFY: return "modify";
1723 case NXFMF_ACTIONS: return "actions";
1724 case NXFMF_OWN: return "own";
1731 ofp_print_nxst_flow_monitor_request(struct ds *string,
1732 const struct ofp_header *oh)
1736 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1738 struct ofputil_flow_monitor_request request;
1741 retval = ofputil_decode_flow_monitor_request(&request, &b);
1743 if (retval != EOF) {
1744 ofp_print_error(string, retval);
1749 ds_put_format(string, "\n id=%"PRIu32" flags=", request.id);
1750 ofp_print_bit_names(string, request.flags,
1751 nx_flow_monitor_flags_to_name, ',');
1753 if (request.out_port != OFPP_NONE) {
1754 ds_put_cstr(string, " out_port=");
1755 ofputil_format_port(request.out_port, string);
1758 if (request.table_id != 0xff) {
1759 ds_put_format(string, " table=%"PRIu8, request.table_id);
1762 ds_put_char(string, ' ');
1763 match_format(&request.match, string, OFP_DEFAULT_PRIORITY);
1764 ds_chomp(string, ' ');
1769 ofp_print_nxst_flow_monitor_reply(struct ds *string,
1770 const struct ofp_header *oh)
1772 uint64_t ofpacts_stub[1024 / 8];
1773 struct ofpbuf ofpacts;
1776 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1777 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1779 struct ofputil_flow_update update;
1783 update.match = &match;
1784 retval = ofputil_decode_flow_update(&update, &b, &ofpacts);
1786 if (retval != EOF) {
1787 ofp_print_error(string, retval);
1789 ofpbuf_uninit(&ofpacts);
1793 ds_put_cstr(string, "\n event=");
1794 switch (update.event) {
1796 ds_put_cstr(string, "ADDED");
1800 ds_put_format(string, "DELETED reason=%s",
1801 ofp_flow_removed_reason_to_string(update.reason));
1804 case NXFME_MODIFIED:
1805 ds_put_cstr(string, "MODIFIED");
1809 ds_put_format(string, "ABBREV xid=0x%"PRIx32, ntohl(update.xid));
1813 ds_put_format(string, " table=%"PRIu8, update.table_id);
1814 if (update.idle_timeout != OFP_FLOW_PERMANENT) {
1815 ds_put_format(string, " idle_timeout=%"PRIu16,
1816 update.idle_timeout);
1818 if (update.hard_timeout != OFP_FLOW_PERMANENT) {
1819 ds_put_format(string, " hard_timeout=%"PRIu16,
1820 update.hard_timeout);
1822 ds_put_format(string, " cookie=%#"PRIx64, ntohll(update.cookie));
1824 ds_put_char(string, ' ');
1825 match_format(update.match, string, OFP_DEFAULT_PRIORITY);
1827 if (update.ofpacts_len) {
1828 if (string->string[string->length - 1] != ' ') {
1829 ds_put_char(string, ' ');
1831 ofpacts_format(update.ofpacts, update.ofpacts_len, string);
1837 ofp_print_version(const struct ofp_header *oh,
1840 switch (oh->version) {
1844 ds_put_cstr(string, " (OF1.1)");
1847 ds_put_cstr(string, " (OF1.2)");
1850 ds_put_cstr(string, " (OF1.3)");
1853 ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
1856 ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
1860 ofp_header_to_string__(const struct ofp_header *oh, enum ofpraw raw,
1863 ds_put_cstr(string, ofpraw_get_name(raw));
1864 ofp_print_version(oh, string);
1868 ofp_print_not_implemented(struct ds *string)
1870 ds_put_cstr(string, "NOT IMPLEMENTED YET!\n");
1874 ofp_to_string__(const struct ofp_header *oh, enum ofpraw raw,
1875 struct ds *string, int verbosity)
1877 const void *msg = oh;
1879 ofp_header_to_string__(oh, raw, string);
1880 switch (ofptype_from_ofpraw(raw)) {
1882 /* FIXME: Change the following once they are implemented: */
1883 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
1884 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
1885 case OFPTYPE_GET_ASYNC_REQUEST:
1886 case OFPTYPE_GET_ASYNC_REPLY:
1887 case OFPTYPE_METER_MOD:
1888 case OFPTYPE_GROUP_REQUEST:
1889 case OFPTYPE_GROUP_REPLY:
1890 case OFPTYPE_GROUP_DESC_REQUEST:
1891 case OFPTYPE_GROUP_DESC_REPLY:
1892 case OFPTYPE_GROUP_FEATURES_REQUEST:
1893 case OFPTYPE_GROUP_FEATURES_REPLY:
1894 case OFPTYPE_METER_REQUEST:
1895 case OFPTYPE_METER_REPLY:
1896 case OFPTYPE_METER_CONFIG_REQUEST:
1897 case OFPTYPE_METER_CONFIG_REPLY:
1898 case OFPTYPE_METER_FEATURES_REQUEST:
1899 case OFPTYPE_METER_FEATURES_REPLY:
1900 case OFPTYPE_TABLE_FEATURES_REQUEST:
1901 case OFPTYPE_TABLE_FEATURES_REPLY:
1902 ofp_print_not_implemented(string);
1906 ofp_print_hello(string, oh);
1910 ofp_print_error_msg(string, oh);
1913 case OFPTYPE_ECHO_REQUEST:
1914 case OFPTYPE_ECHO_REPLY:
1915 ofp_print_echo(string, oh, verbosity);
1918 case OFPTYPE_FEATURES_REQUEST:
1921 case OFPTYPE_FEATURES_REPLY:
1922 ofp_print_switch_features(string, oh);
1925 case OFPTYPE_GET_CONFIG_REQUEST:
1928 case OFPTYPE_GET_CONFIG_REPLY:
1929 case OFPTYPE_SET_CONFIG:
1930 ofp_print_switch_config(string, ofpmsg_body(oh));
1933 case OFPTYPE_PACKET_IN:
1934 ofp_print_packet_in(string, oh, verbosity);
1937 case OFPTYPE_FLOW_REMOVED:
1938 ofp_print_flow_removed(string, oh);
1941 case OFPTYPE_PORT_STATUS:
1942 ofp_print_port_status(string, oh);
1945 case OFPTYPE_PACKET_OUT:
1946 ofp_print_packet_out(string, oh, verbosity);
1949 case OFPTYPE_FLOW_MOD:
1950 ofp_print_flow_mod(string, oh, verbosity);
1953 case OFPTYPE_PORT_MOD:
1954 ofp_print_port_mod(string, oh);
1957 case OFPTYPE_BARRIER_REQUEST:
1958 case OFPTYPE_BARRIER_REPLY:
1961 case OFPTYPE_ROLE_REQUEST:
1962 case OFPTYPE_ROLE_REPLY:
1963 ofp_print_role_message(string, oh);
1966 case OFPTYPE_DESC_STATS_REQUEST:
1967 case OFPTYPE_PORT_DESC_STATS_REQUEST:
1968 ofp_print_stats_request(string, oh);
1971 case OFPTYPE_FLOW_STATS_REQUEST:
1972 case OFPTYPE_AGGREGATE_STATS_REQUEST:
1973 ofp_print_stats_request(string, oh);
1974 ofp_print_flow_stats_request(string, oh);
1977 case OFPTYPE_TABLE_STATS_REQUEST:
1978 ofp_print_stats_request(string, oh);
1981 case OFPTYPE_PORT_STATS_REQUEST:
1982 ofp_print_stats_request(string, oh);
1983 ofp_print_ofpst_port_request(string, oh);
1986 case OFPTYPE_QUEUE_STATS_REQUEST:
1987 ofp_print_stats_request(string, oh);
1988 ofp_print_ofpst_queue_request(string, oh);
1991 case OFPTYPE_DESC_STATS_REPLY:
1992 ofp_print_stats_reply(string, oh);
1993 ofp_print_ofpst_desc_reply(string, oh);
1996 case OFPTYPE_FLOW_STATS_REPLY:
1997 ofp_print_stats_reply(string, oh);
1998 ofp_print_flow_stats_reply(string, oh);
2001 case OFPTYPE_QUEUE_STATS_REPLY:
2002 ofp_print_stats_reply(string, oh);
2003 ofp_print_ofpst_queue_reply(string, oh, verbosity);
2006 case OFPTYPE_PORT_STATS_REPLY:
2007 ofp_print_stats_reply(string, oh);
2008 ofp_print_ofpst_port_reply(string, oh, verbosity);
2011 case OFPTYPE_TABLE_STATS_REPLY:
2012 ofp_print_stats_reply(string, oh);
2013 ofp_print_ofpst_table_reply(string, oh, verbosity);
2016 case OFPTYPE_AGGREGATE_STATS_REPLY:
2017 ofp_print_stats_reply(string, oh);
2018 ofp_print_aggregate_stats_reply(string, oh);
2021 case OFPTYPE_PORT_DESC_STATS_REPLY:
2022 ofp_print_stats_reply(string, oh);
2023 ofp_print_ofpst_port_desc_reply(string, oh);
2026 case OFPTYPE_FLOW_MOD_TABLE_ID:
2027 ofp_print_nxt_flow_mod_table_id(string, ofpmsg_body(oh));
2030 case OFPTYPE_SET_FLOW_FORMAT:
2031 ofp_print_nxt_set_flow_format(string, ofpmsg_body(oh));
2034 case OFPTYPE_SET_PACKET_IN_FORMAT:
2035 ofp_print_nxt_set_packet_in_format(string, ofpmsg_body(oh));
2038 case OFPTYPE_FLOW_AGE:
2041 case OFPTYPE_SET_CONTROLLER_ID:
2042 ofp_print_nxt_set_controller_id(string, ofpmsg_body(oh));
2045 case OFPTYPE_SET_ASYNC_CONFIG:
2046 ofp_print_nxt_set_async_config(string, ofpmsg_body(oh));
2049 case OFPTYPE_FLOW_MONITOR_CANCEL:
2050 ofp_print_nxt_flow_monitor_cancel(string, msg);
2053 case OFPTYPE_FLOW_MONITOR_PAUSED:
2054 case OFPTYPE_FLOW_MONITOR_RESUMED:
2057 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
2058 ofp_print_nxst_flow_monitor_request(string, msg);
2061 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
2062 ofp_print_nxst_flow_monitor_reply(string, msg);
2067 /* Composes and returns a string representing the OpenFlow packet of 'len'
2068 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
2069 * verbosity and higher numbers increase verbosity. The caller is responsible
2070 * for freeing the string. */
2072 ofp_to_string(const void *oh_, size_t len, int verbosity)
2074 struct ds string = DS_EMPTY_INITIALIZER;
2075 const struct ofp_header *oh = oh_;
2078 ds_put_cstr(&string, "OpenFlow message is empty\n");
2079 } else if (len < sizeof(struct ofp_header)) {
2080 ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
2082 } else if (ntohs(oh->length) > len) {
2086 error = ofpraw_decode_partial(&raw, oh, len);
2088 ofp_header_to_string__(oh, raw, &string);
2089 ds_put_char(&string, '\n');
2092 ds_put_format(&string,
2093 "(***truncated to %zu bytes from %"PRIu16"***)\n",
2094 len, ntohs(oh->length));
2095 } else if (ntohs(oh->length) < len) {
2096 ds_put_format(&string,
2097 "(***only uses %"PRIu16" bytes out of %zu***)\n",
2098 ntohs(oh->length), len);
2103 error = ofpraw_decode(&raw, oh);
2105 ofp_to_string__(oh, raw, &string, verbosity);
2106 if (verbosity >= 5) {
2107 if (ds_last(&string) != '\n') {
2108 ds_put_char(&string, '\n');
2110 ds_put_hex_dump(&string, oh, len, 0, true);
2112 if (ds_last(&string) != '\n') {
2113 ds_put_char(&string, '\n');
2115 return ds_steal_cstr(&string);
2118 ofp_print_error(&string, error);
2120 ds_put_hex_dump(&string, oh, len, 0, true);
2121 return ds_steal_cstr(&string);
2125 print_and_free(FILE *stream, char *string)
2127 fputs(string, stream);
2131 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
2132 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
2133 * numbers increase verbosity. */
2135 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
2137 print_and_free(stream, ofp_to_string(oh, len, verbosity));
2140 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
2141 * 'data' to 'stream'. */
2143 ofp_print_packet(FILE *stream, const void *data, size_t len)
2145 print_and_free(stream, ofp_packet_to_string(data, len));