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);
157 ofp_print_packet_out(struct ds *string, const struct ofp_header *oh,
160 struct ofputil_packet_out po;
161 struct ofpbuf ofpacts;
164 ofpbuf_init(&ofpacts, 64);
165 error = ofputil_decode_packet_out(&po, oh, &ofpacts);
167 ofpbuf_uninit(&ofpacts);
168 ofp_print_error(string, error);
172 ds_put_cstr(string, " in_port=");
173 ofputil_format_port(po.in_port, string);
175 ds_put_char(string, ' ');
176 ofpacts_format(po.ofpacts, po.ofpacts_len, string);
178 if (po.buffer_id == UINT32_MAX) {
179 ds_put_format(string, " data_len=%zu", po.packet_len);
180 if (verbosity > 0 && po.packet_len > 0) {
181 char *packet = ofp_packet_to_string(po.packet, po.packet_len);
182 ds_put_char(string, '\n');
183 ds_put_cstr(string, packet);
187 ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id);
189 ds_put_char(string, '\n');
191 ofpbuf_uninit(&ofpacts);
194 /* qsort comparison function. */
196 compare_ports(const void *a_, const void *b_)
198 const struct ofputil_phy_port *a = a_;
199 const struct ofputil_phy_port *b = b_;
200 uint16_t ap = a->port_no;
201 uint16_t bp = b->port_no;
203 return ap < bp ? -1 : ap > bp;
207 ofp_print_bit_names(struct ds *string, uint32_t bits,
208 const char *(*bit_to_name)(uint32_t bit),
215 ds_put_cstr(string, "0");
219 for (i = 0; i < 32; i++) {
220 uint32_t bit = UINT32_C(1) << i;
223 const char *name = bit_to_name(bit);
226 ds_put_char(string, separator);
228 ds_put_cstr(string, name);
236 ds_put_char(string, separator);
238 ds_put_format(string, "0x%"PRIx32, bits);
243 netdev_feature_to_name(uint32_t bit)
245 enum netdev_features f = bit;
248 case NETDEV_F_10MB_HD: return "10MB-HD";
249 case NETDEV_F_10MB_FD: return "10MB-FD";
250 case NETDEV_F_100MB_HD: return "100MB-HD";
251 case NETDEV_F_100MB_FD: return "100MB-FD";
252 case NETDEV_F_1GB_HD: return "1GB-HD";
253 case NETDEV_F_1GB_FD: return "1GB-FD";
254 case NETDEV_F_10GB_FD: return "10GB-FD";
255 case NETDEV_F_40GB_FD: return "40GB-FD";
256 case NETDEV_F_100GB_FD: return "100GB-FD";
257 case NETDEV_F_1TB_FD: return "1TB-FD";
258 case NETDEV_F_OTHER: return "OTHER";
259 case NETDEV_F_COPPER: return "COPPER";
260 case NETDEV_F_FIBER: return "FIBER";
261 case NETDEV_F_AUTONEG: return "AUTO_NEG";
262 case NETDEV_F_PAUSE: return "AUTO_PAUSE";
263 case NETDEV_F_PAUSE_ASYM: return "AUTO_PAUSE_ASYM";
270 ofp_print_port_features(struct ds *string, enum netdev_features features)
272 ofp_print_bit_names(string, features, netdev_feature_to_name, ' ');
273 ds_put_char(string, '\n');
277 ofputil_port_config_to_name(uint32_t bit)
279 enum ofputil_port_config pc = bit;
282 case OFPUTIL_PC_PORT_DOWN: return "PORT_DOWN";
283 case OFPUTIL_PC_NO_STP: return "NO_STP";
284 case OFPUTIL_PC_NO_RECV: return "NO_RECV";
285 case OFPUTIL_PC_NO_RECV_STP: return "NO_RECV_STP";
286 case OFPUTIL_PC_NO_FLOOD: return "NO_FLOOD";
287 case OFPUTIL_PC_NO_FWD: return "NO_FWD";
288 case OFPUTIL_PC_NO_PACKET_IN: return "NO_PACKET_IN";
295 ofp_print_port_config(struct ds *string, enum ofputil_port_config config)
297 ofp_print_bit_names(string, config, ofputil_port_config_to_name, ' ');
298 ds_put_char(string, '\n');
302 ofputil_port_state_to_name(uint32_t bit)
304 enum ofputil_port_state ps = bit;
307 case OFPUTIL_PS_LINK_DOWN: return "LINK_DOWN";
308 case OFPUTIL_PS_BLOCKED: return "BLOCKED";
309 case OFPUTIL_PS_LIVE: return "LIVE";
311 case OFPUTIL_PS_STP_LISTEN:
312 case OFPUTIL_PS_STP_LEARN:
313 case OFPUTIL_PS_STP_FORWARD:
314 case OFPUTIL_PS_STP_BLOCK:
315 /* Handled elsewhere. */
323 ofp_print_port_state(struct ds *string, enum ofputil_port_state state)
325 enum ofputil_port_state stp_state;
327 /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
328 * pattern. We have to special case it.
330 * OVS doesn't support STP, so this field will always be 0 if we are
331 * talking to OVS, so we'd always print STP_LISTEN in that case.
332 * Therefore, we don't print anything at all if the value is STP_LISTEN, to
333 * avoid confusing users. */
334 stp_state = state & OFPUTIL_PS_STP_MASK;
337 (stp_state == OFPUTIL_PS_STP_LEARN ? "STP_LEARN"
338 : stp_state == OFPUTIL_PS_STP_FORWARD ? "STP_FORWARD"
340 state &= ~OFPUTIL_PS_STP_MASK;
342 ofp_print_bit_names(string, state, ofputil_port_state_to_name,
346 ofp_print_bit_names(string, state, ofputil_port_state_to_name, ' ');
348 ds_put_char(string, '\n');
352 ofp_print_phy_port(struct ds *string, const struct ofputil_phy_port *port)
354 char name[sizeof port->name];
357 memcpy(name, port->name, sizeof name);
358 for (j = 0; j < sizeof name - 1; j++) {
359 if (!isprint((unsigned char) name[j])) {
365 ds_put_char(string, ' ');
366 ofputil_format_port(port->port_no, string);
367 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
368 name, ETH_ADDR_ARGS(port->hw_addr));
370 ds_put_cstr(string, " config: ");
371 ofp_print_port_config(string, port->config);
373 ds_put_cstr(string, " state: ");
374 ofp_print_port_state(string, port->state);
377 ds_put_format(string, " current: ");
378 ofp_print_port_features(string, port->curr);
380 if (port->advertised) {
381 ds_put_format(string, " advertised: ");
382 ofp_print_port_features(string, port->advertised);
384 if (port->supported) {
385 ds_put_format(string, " supported: ");
386 ofp_print_port_features(string, port->supported);
389 ds_put_format(string, " peer: ");
390 ofp_print_port_features(string, port->peer);
392 ds_put_format(string, " speed: %"PRIu32" Mbps now, "
393 "%"PRIu32" Mbps max\n",
394 port->curr_speed / UINT32_C(1000),
395 port->max_speed / UINT32_C(1000));
398 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
399 * 'ofp_version', writes a detailed description of each port into
402 ofp_print_phy_ports(struct ds *string, uint8_t ofp_version,
406 struct ofputil_phy_port *ports;
410 n_ports = ofputil_count_phy_ports(ofp_version, b);
412 ports = xmalloc(n_ports * sizeof *ports);
413 for (i = 0; i < n_ports; i++) {
414 error = ofputil_pull_phy_port(ofp_version, b, &ports[i]);
416 ofp_print_error(string, error);
420 qsort(ports, n_ports, sizeof *ports, compare_ports);
421 for (i = 0; i < n_ports; i++) {
422 ofp_print_phy_port(string, &ports[i]);
430 ofputil_capabilities_to_name(uint32_t bit)
432 enum ofputil_capabilities capabilities = bit;
434 switch (capabilities) {
435 case OFPUTIL_C_FLOW_STATS: return "FLOW_STATS";
436 case OFPUTIL_C_TABLE_STATS: return "TABLE_STATS";
437 case OFPUTIL_C_PORT_STATS: return "PORT_STATS";
438 case OFPUTIL_C_IP_REASM: return "IP_REASM";
439 case OFPUTIL_C_QUEUE_STATS: return "QUEUE_STATS";
440 case OFPUTIL_C_ARP_MATCH_IP: return "ARP_MATCH_IP";
441 case OFPUTIL_C_STP: return "STP";
442 case OFPUTIL_C_GROUP_STATS: return "GROUP_STATS";
443 case OFPUTIL_C_PORT_BLOCKED: return "PORT_BLOCKED";
450 ofputil_action_bitmap_to_name(uint32_t bit)
452 enum ofputil_action_bitmap action = bit;
455 case OFPUTIL_A_OUTPUT: return "OUTPUT";
456 case OFPUTIL_A_SET_VLAN_VID: return "SET_VLAN_VID";
457 case OFPUTIL_A_SET_VLAN_PCP: return "SET_VLAN_PCP";
458 case OFPUTIL_A_STRIP_VLAN: return "STRIP_VLAN";
459 case OFPUTIL_A_SET_DL_SRC: return "SET_DL_SRC";
460 case OFPUTIL_A_SET_DL_DST: return "SET_DL_DST";
461 case OFPUTIL_A_SET_NW_SRC: return "SET_NW_SRC";
462 case OFPUTIL_A_SET_NW_DST: return "SET_NW_DST";
463 case OFPUTIL_A_SET_NW_ECN: return "SET_NW_ECN";
464 case OFPUTIL_A_SET_NW_TOS: return "SET_NW_TOS";
465 case OFPUTIL_A_SET_TP_SRC: return "SET_TP_SRC";
466 case OFPUTIL_A_SET_TP_DST: return "SET_TP_DST";
467 case OFPUTIL_A_SET_FIELD: return "SET_FIELD";
468 case OFPUTIL_A_ENQUEUE: return "ENQUEUE";
469 case OFPUTIL_A_COPY_TTL_OUT: return "COPY_TTL_OUT";
470 case OFPUTIL_A_COPY_TTL_IN: return "COPY_TTL_IN";
471 case OFPUTIL_A_SET_MPLS_LABEL: return "SET_MPLS_LABEL";
472 case OFPUTIL_A_SET_MPLS_TC: return "SET_MPLS_TC";
473 case OFPUTIL_A_SET_MPLS_TTL: return "SET_MPLS_TTL";
474 case OFPUTIL_A_DEC_MPLS_TTL: return "DEC_MPLS_TTL";
475 case OFPUTIL_A_PUSH_VLAN: return "PUSH_VLAN";
476 case OFPUTIL_A_POP_VLAN: return "POP_VLAN";
477 case OFPUTIL_A_PUSH_MPLS: return "PUSH_MPLS";
478 case OFPUTIL_A_POP_MPLS: return "POP_MPLS";
479 case OFPUTIL_A_SET_QUEUE: return "SET_QUEUE";
480 case OFPUTIL_A_GROUP: return "GROUP";
481 case OFPUTIL_A_SET_NW_TTL: return "SET_NW_TTL";
482 case OFPUTIL_A_DEC_NW_TTL: return "DEC_NW_TTL";
489 ofp_print_switch_features(struct ds *string, const struct ofp_header *oh)
491 struct ofputil_switch_features features;
495 error = ofputil_decode_switch_features(oh, &features, &b);
497 ofp_print_error(string, error);
501 ds_put_format(string, " dpid:%016"PRIx64"\n", features.datapath_id);
503 ds_put_format(string, "n_tables:%"PRIu8", n_buffers:%"PRIu32,
504 features.n_tables, features.n_buffers);
505 if (features.auxiliary_id) {
506 ds_put_format(string, ", auxiliary_id:%"PRIu8, features.auxiliary_id);
508 ds_put_char(string, '\n');
510 ds_put_cstr(string, "capabilities: ");
511 ofp_print_bit_names(string, features.capabilities,
512 ofputil_capabilities_to_name, ' ');
513 ds_put_char(string, '\n');
515 switch ((enum ofp_version)oh->version) {
517 ds_put_cstr(string, "actions: ");
518 ofp_print_bit_names(string, features.actions,
519 ofputil_action_bitmap_to_name, ' ');
520 ds_put_char(string, '\n');
526 return; /* no ports in ofp13_switch_features */
531 ofp_print_phy_ports(string, oh->version, &b);
535 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
537 enum ofp_config_flags flags;
539 flags = ntohs(osc->flags);
541 ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
542 flags &= ~OFPC_FRAG_MASK;
544 if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) {
545 ds_put_format(string, " invalid_ttl_to_controller");
546 flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER;
550 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
553 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
556 static void print_wild(struct ds *string, const char *leader, int is_wild,
557 int verbosity, const char *format, ...)
558 __attribute__((format(printf, 5, 6)));
560 static void print_wild(struct ds *string, const char *leader, int is_wild,
561 int verbosity, const char *format, ...)
563 if (is_wild && verbosity < 2) {
566 ds_put_cstr(string, leader);
570 va_start(args, format);
571 ds_put_format_valist(string, format, args);
574 ds_put_char(string, '*');
576 ds_put_char(string, ',');
580 print_wild_port(struct ds *string, const char *leader, int is_wild,
581 int verbosity, uint16_t port)
583 if (is_wild && verbosity < 2) {
586 ds_put_cstr(string, leader);
588 ofputil_format_port(port, string);
590 ds_put_char(string, '*');
592 ds_put_char(string, ',');
596 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
597 uint32_t wild_bits, int verbosity)
599 if (wild_bits >= 32 && verbosity < 2) {
602 ds_put_cstr(string, leader);
603 if (wild_bits < 32) {
604 ds_put_format(string, IP_FMT, IP_ARGS(ip));
606 ds_put_format(string, "/%d", 32 - wild_bits);
609 ds_put_char(string, '*');
611 ds_put_char(string, ',');
615 ofp10_match_print(struct ds *f, const struct ofp10_match *om, int verbosity)
617 char *s = ofp10_match_to_string(om, verbosity);
623 ofp10_match_to_string(const struct ofp10_match *om, int verbosity)
625 struct ds f = DS_EMPTY_INITIALIZER;
626 uint32_t w = ntohl(om->wildcards);
627 bool skip_type = false;
628 bool skip_proto = false;
630 if (!(w & OFPFW10_DL_TYPE)) {
632 if (om->dl_type == htons(ETH_TYPE_IP)) {
633 if (!(w & OFPFW10_NW_PROTO)) {
635 if (om->nw_proto == IPPROTO_ICMP) {
636 ds_put_cstr(&f, "icmp,");
637 } else if (om->nw_proto == IPPROTO_TCP) {
638 ds_put_cstr(&f, "tcp,");
639 } else if (om->nw_proto == IPPROTO_UDP) {
640 ds_put_cstr(&f, "udp,");
642 ds_put_cstr(&f, "ip,");
646 ds_put_cstr(&f, "ip,");
648 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
649 ds_put_cstr(&f, "arp,");
650 } else if (om->dl_type == htons(ETH_TYPE_RARP)){
651 ds_put_cstr(&f, "rarp,");
652 } else if (om->dl_type == htons(ETH_TYPE_MPLS)) {
653 ds_put_cstr(&f, "mpls,");
654 } else if (om->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
655 ds_put_cstr(&f, "mplsm,");
660 print_wild_port(&f, "in_port=", w & OFPFW10_IN_PORT, verbosity,
662 print_wild(&f, "dl_vlan=", w & OFPFW10_DL_VLAN, verbosity,
663 "%d", ntohs(om->dl_vlan));
664 print_wild(&f, "dl_vlan_pcp=", w & OFPFW10_DL_VLAN_PCP, verbosity,
665 "%d", om->dl_vlan_pcp);
666 print_wild(&f, "dl_src=", w & OFPFW10_DL_SRC, verbosity,
667 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
668 print_wild(&f, "dl_dst=", w & OFPFW10_DL_DST, verbosity,
669 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
671 print_wild(&f, "dl_type=", w & OFPFW10_DL_TYPE, verbosity,
672 "0x%04x", ntohs(om->dl_type));
674 print_ip_netmask(&f, "nw_src=", om->nw_src,
675 (w & OFPFW10_NW_SRC_MASK) >> OFPFW10_NW_SRC_SHIFT,
677 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
678 (w & OFPFW10_NW_DST_MASK) >> OFPFW10_NW_DST_SHIFT,
681 if (om->dl_type == htons(ETH_TYPE_ARP) ||
682 om->dl_type == htons(ETH_TYPE_RARP)) {
683 print_wild(&f, "arp_op=", w & OFPFW10_NW_PROTO, verbosity,
686 print_wild(&f, "nw_proto=", w & OFPFW10_NW_PROTO, verbosity,
690 print_wild(&f, "nw_tos=", w & OFPFW10_NW_TOS, verbosity,
692 if (om->nw_proto == IPPROTO_ICMP) {
693 print_wild(&f, "icmp_type=", w & OFPFW10_ICMP_TYPE, verbosity,
694 "%d", ntohs(om->tp_src));
695 print_wild(&f, "icmp_code=", w & OFPFW10_ICMP_CODE, verbosity,
696 "%d", ntohs(om->tp_dst));
698 print_wild(&f, "tp_src=", w & OFPFW10_TP_SRC, verbosity,
699 "%d", ntohs(om->tp_src));
700 print_wild(&f, "tp_dst=", w & OFPFW10_TP_DST, verbosity,
701 "%d", ntohs(om->tp_dst));
703 if (ds_last(&f) == ',') {
710 ofp_print_flow_flags(struct ds *s, uint16_t flags)
712 if (flags & OFPFF_SEND_FLOW_REM) {
713 ds_put_cstr(s, "send_flow_rem ");
715 if (flags & OFPFF_CHECK_OVERLAP) {
716 ds_put_cstr(s, "check_overlap ");
718 if (flags & OFPFF12_RESET_COUNTS) {
719 ds_put_cstr(s, "reset_counts ");
721 if (flags & OFPFF13_NO_PKT_COUNTS) {
722 ds_put_cstr(s, "no_packet_counts ");
724 if (flags & OFPFF13_NO_BYT_COUNTS) {
725 ds_put_cstr(s, "no_byte_counts ");
728 flags &= ~(OFPFF_SEND_FLOW_REM | OFPFF_CHECK_OVERLAP
729 | OFPFF12_RESET_COUNTS
730 | OFPFF13_NO_PKT_COUNTS | OFPFF13_NO_BYT_COUNTS);
732 ds_put_format(s, "flags:0x%"PRIx16" ", flags);
737 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh, int verbosity)
739 struct ofputil_flow_mod fm;
740 struct ofpbuf ofpacts;
744 enum ofputil_protocol protocol;
746 protocol = ofputil_protocol_from_ofp_version(oh->version);
747 protocol = ofputil_protocol_set_tid(protocol, true);
749 ofpbuf_init(&ofpacts, 64);
750 error = ofputil_decode_flow_mod(&fm, oh, protocol, &ofpacts);
752 ofpbuf_uninit(&ofpacts);
753 ofp_print_error(s, error);
758 switch (fm.command) {
760 ds_put_cstr(s, "ADD");
763 ds_put_cstr(s, "MOD");
765 case OFPFC_MODIFY_STRICT:
766 ds_put_cstr(s, "MOD_STRICT");
769 ds_put_cstr(s, "DEL");
771 case OFPFC_DELETE_STRICT:
772 ds_put_cstr(s, "DEL_STRICT");
775 ds_put_format(s, "cmd:%d", fm.command);
777 if (fm.table_id != 0) {
778 ds_put_format(s, " table:%d", fm.table_id);
782 ofpraw_decode(&raw, oh);
783 if (verbosity >= 3 && raw == OFPRAW_OFPT10_FLOW_MOD) {
784 const struct ofp10_flow_mod *ofm = ofpmsg_body(oh);
785 ofp10_match_print(s, &ofm->match, verbosity);
787 /* ofp_print_match() doesn't print priority. */
788 need_priority = true;
789 } else if (verbosity >= 3 && raw == OFPRAW_NXT_FLOW_MOD) {
790 const struct nx_flow_mod *nfm = ofpmsg_body(oh);
791 const void *nxm = nfm + 1;
794 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
795 ds_put_cstr(s, nxm_s);
798 /* nx_match_to_string() doesn't print priority. */
799 need_priority = true;
801 match_format(&fm.match, s, fm.priority);
803 /* match_format() does print priority. */
804 need_priority = false;
807 if (ds_last(s) != ' ') {
810 if (fm.new_cookie != htonll(0) && fm.new_cookie != htonll(UINT64_MAX)) {
811 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.new_cookie));
813 if (fm.cookie_mask != htonll(0)) {
814 ds_put_format(s, "cookie:0x%"PRIx64"/0x%"PRIx64" ",
815 ntohll(fm.cookie), ntohll(fm.cookie_mask));
817 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
818 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
820 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
821 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
823 if (fm.priority != OFP_DEFAULT_PRIORITY && need_priority) {
824 ds_put_format(s, "pri:%"PRIu16" ", fm.priority);
826 if (fm.buffer_id != UINT32_MAX) {
827 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
829 if (fm.out_port != OFPP_ANY) {
830 ds_put_format(s, "out_port:");
831 ofputil_format_port(fm.out_port, s);
835 ofp_print_flow_flags(s, fm.flags);
838 ofpacts_format(fm.ofpacts, fm.ofpacts_len, s);
839 ofpbuf_uninit(&ofpacts);
843 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
845 ds_put_format(string, "%u", sec);
847 ds_put_format(string, ".%09u", nsec);
848 while (string->string[string->length - 1] == '0') {
852 ds_put_char(string, 's');
856 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason)
861 case OFPRR_IDLE_TIMEOUT:
863 case OFPRR_HARD_TIMEOUT:
867 case OFPRR_GROUP_DELETE:
868 return "group_delete";
872 sprintf(s, "%d", (int) reason);
878 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
880 struct ofputil_flow_removed fr;
883 error = ofputil_decode_flow_removed(&fr, oh);
885 ofp_print_error(string, error);
889 ds_put_char(string, ' ');
890 match_format(&fr.match, string, fr.priority);
892 ds_put_format(string, " reason=%s",
893 ofp_flow_removed_reason_to_string(fr.reason));
895 if (fr.table_id != 255) {
896 ds_put_format(string, " table_id=%"PRIu8, fr.table_id);
899 if (fr.cookie != htonll(0)) {
900 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
902 ds_put_cstr(string, " duration");
903 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
904 ds_put_format(string, " idle%"PRIu16, fr.idle_timeout);
905 if (fr.hard_timeout) {
906 /* The hard timeout was only added in OF1.2, so only print it if it is
907 * actually in use to avoid gratuitous change to the formatting. */
908 ds_put_format(string, " hard%"PRIu16, fr.hard_timeout);
910 ds_put_format(string, " pkts%"PRIu64" bytes%"PRIu64"\n",
911 fr.packet_count, fr.byte_count);
915 ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
917 struct ofputil_port_mod pm;
920 error = ofputil_decode_port_mod(oh, &pm);
922 ofp_print_error(string, error);
926 ds_put_cstr(string, "port: ");
927 ofputil_format_port(pm.port_no, string);
928 ds_put_format(string, ": addr:"ETH_ADDR_FMT"\n",
929 ETH_ADDR_ARGS(pm.hw_addr));
931 ds_put_cstr(string, " config: ");
932 ofp_print_port_config(string, pm.config);
934 ds_put_cstr(string, " mask: ");
935 ofp_print_port_config(string, pm.mask);
937 ds_put_cstr(string, " advertise: ");
939 ofp_print_port_features(string, pm.advertise);
941 ds_put_cstr(string, "UNCHANGED\n");
946 ofp_print_error(struct ds *string, enum ofperr error)
948 if (string->length) {
949 ds_put_char(string, ' ');
951 ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
955 ofp_print_hello(struct ds *string, const struct ofp_header *oh)
957 uint32_t allowed_versions;
960 ok = ofputil_decode_hello(oh, &allowed_versions);
962 ds_put_cstr(string, "\n version bitmap: ");
963 ofputil_format_version_bitmap(string, allowed_versions);
966 ds_put_cstr(string, "\n unknown data in hello:\n");
967 ds_put_hex_dump(string, oh, ntohs(oh->length), 0, true);
972 ofp_print_error_msg(struct ds *string, const struct ofp_header *oh)
974 size_t len = ntohs(oh->length);
975 struct ofpbuf payload;
979 error = ofperr_decode_msg(oh, &payload);
981 ds_put_cstr(string, "***decode error***");
982 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
986 ds_put_format(string, " %s\n", ofperr_get_name(error));
988 if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
989 ds_put_printable(string, payload.data, payload.size);
991 s = ofp_to_string(payload.data, payload.size, 1);
992 ds_put_cstr(string, s);
998 ofp_print_port_status(struct ds *string, const struct ofp_header *oh)
1000 struct ofputil_port_status ps;
1003 error = ofputil_decode_port_status(oh, &ps);
1005 ofp_print_error(string, error);
1009 if (ps.reason == OFPPR_ADD) {
1010 ds_put_format(string, " ADD:");
1011 } else if (ps.reason == OFPPR_DELETE) {
1012 ds_put_format(string, " DEL:");
1013 } else if (ps.reason == OFPPR_MODIFY) {
1014 ds_put_format(string, " MOD:");
1017 ofp_print_phy_port(string, &ps.desc);
1021 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
1023 const struct ofp_desc_stats *ods = ofpmsg_body(oh);
1025 ds_put_char(string, '\n');
1026 ds_put_format(string, "Manufacturer: %.*s\n",
1027 (int) sizeof ods->mfr_desc, ods->mfr_desc);
1028 ds_put_format(string, "Hardware: %.*s\n",
1029 (int) sizeof ods->hw_desc, ods->hw_desc);
1030 ds_put_format(string, "Software: %.*s\n",
1031 (int) sizeof ods->sw_desc, ods->sw_desc);
1032 ds_put_format(string, "Serial Num: %.*s\n",
1033 (int) sizeof ods->serial_num, ods->serial_num);
1034 ds_put_format(string, "DP Description: %.*s\n",
1035 (int) sizeof ods->dp_desc, ods->dp_desc);
1039 ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
1041 struct ofputil_flow_stats_request fsr;
1044 error = ofputil_decode_flow_stats_request(&fsr, oh);
1046 ofp_print_error(string, error);
1050 if (fsr.table_id != 0xff) {
1051 ds_put_format(string, " table=%"PRIu8, fsr.table_id);
1054 if (fsr.out_port != OFPP_ANY) {
1055 ds_put_cstr(string, " out_port=");
1056 ofputil_format_port(fsr.out_port, string);
1059 ds_put_char(string, ' ');
1060 match_format(&fsr.match, string, OFP_DEFAULT_PRIORITY);
1064 ofp_print_flow_stats(struct ds *string, struct ofputil_flow_stats *fs)
1066 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1067 ntohll(fs->cookie));
1069 ofp_print_duration(string, fs->duration_sec, fs->duration_nsec);
1070 ds_put_format(string, ", table=%"PRIu8", ", fs->table_id);
1071 ds_put_format(string, "n_packets=%"PRIu64", ", fs->packet_count);
1072 ds_put_format(string, "n_bytes=%"PRIu64", ", fs->byte_count);
1073 if (fs->idle_timeout != OFP_FLOW_PERMANENT) {
1074 ds_put_format(string, "idle_timeout=%"PRIu16", ", fs->idle_timeout);
1076 if (fs->hard_timeout != OFP_FLOW_PERMANENT) {
1077 ds_put_format(string, "hard_timeout=%"PRIu16", ", fs->hard_timeout);
1080 ofp_print_flow_flags(string, fs->flags);
1082 if (fs->idle_age >= 0) {
1083 ds_put_format(string, "idle_age=%d, ", fs->idle_age);
1085 if (fs->hard_age >= 0 && fs->hard_age != fs->duration_sec) {
1086 ds_put_format(string, "hard_age=%d, ", fs->hard_age);
1089 match_format(&fs->match, string, fs->priority);
1090 if (string->string[string->length - 1] != ' ') {
1091 ds_put_char(string, ' ');
1094 ofpacts_format(fs->ofpacts, fs->ofpacts_len, string);
1098 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1100 struct ofpbuf ofpacts;
1103 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1104 ofpbuf_init(&ofpacts, 64);
1106 struct ofputil_flow_stats fs;
1109 retval = ofputil_decode_flow_stats_reply(&fs, &b, true, &ofpacts);
1111 if (retval != EOF) {
1112 ds_put_cstr(string, " ***parse error***");
1116 ds_put_char(string, '\n');
1117 ofp_print_flow_stats(string, &fs);
1119 ofpbuf_uninit(&ofpacts);
1123 ofp_print_aggregate_stats_reply(struct ds *string, const struct ofp_header *oh)
1125 struct ofputil_aggregate_stats as;
1128 error = ofputil_decode_aggregate_stats_reply(&as, oh);
1130 ofp_print_error(string, error);
1134 ds_put_format(string, " packet_count=%"PRIu64, as.packet_count);
1135 ds_put_format(string, " byte_count=%"PRIu64, as.byte_count);
1136 ds_put_format(string, " flow_count=%"PRIu32, as.flow_count);
1140 print_port_stat(struct ds *string, const char *leader, uint64_t stat, int more)
1142 ds_put_cstr(string, leader);
1143 if (stat != UINT64_MAX) {
1144 ds_put_format(string, "%"PRIu64, stat);
1146 ds_put_char(string, '?');
1149 ds_put_cstr(string, ", ");
1151 ds_put_cstr(string, "\n");
1156 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1158 uint16_t ofp10_port;
1161 error = ofputil_decode_port_stats_request(oh, &ofp10_port);
1163 ofp_print_error(string, error);
1167 ds_put_cstr(string, " port_no=");
1168 ofputil_format_port(ofp10_port, string);
1172 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1177 ds_put_format(string, " %zu ports\n", ofputil_count_port_stats(oh));
1178 if (verbosity < 1) {
1182 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1184 struct ofputil_port_stats ps;
1187 retval = ofputil_decode_port_stats(&ps, &b);
1189 if (retval != EOF) {
1190 ds_put_cstr(string, " ***parse error***");
1195 ds_put_cstr(string, " port ");
1196 if (ps.port_no < 10) {
1197 ds_put_char(string, ' ');
1199 ofputil_format_port(ps.port_no, string);
1201 ds_put_cstr(string, ": rx ");
1202 print_port_stat(string, "pkts=", ps.stats.rx_packets, 1);
1203 print_port_stat(string, "bytes=", ps.stats.rx_bytes, 1);
1204 print_port_stat(string, "drop=", ps.stats.rx_dropped, 1);
1205 print_port_stat(string, "errs=", ps.stats.rx_errors, 1);
1206 print_port_stat(string, "frame=", ps.stats.rx_frame_errors, 1);
1207 print_port_stat(string, "over=", ps.stats.rx_over_errors, 1);
1208 print_port_stat(string, "crc=", ps.stats.rx_crc_errors, 0);
1210 ds_put_cstr(string, " tx ");
1211 print_port_stat(string, "pkts=", ps.stats.tx_packets, 1);
1212 print_port_stat(string, "bytes=", ps.stats.tx_bytes, 1);
1213 print_port_stat(string, "drop=", ps.stats.tx_dropped, 1);
1214 print_port_stat(string, "errs=", ps.stats.tx_errors, 1);
1215 print_port_stat(string, "coll=", ps.stats.collisions, 0);
1220 ofp_print_one_ofpst_table_reply(struct ds *string, enum ofp_version ofp_version,
1221 const char *name, struct ofp12_table_stats *ts)
1223 char name_[OFP_MAX_TABLE_NAME_LEN + 1];
1225 /* ofp13_table_stats is different */
1226 if (ofp_version > OFP12_VERSION) {
1230 ovs_strlcpy(name_, name, sizeof name_);
1232 ds_put_format(string, " %d: %-8s: ", ts->table_id, name_);
1233 ds_put_format(string, "wild=0x%05"PRIx64", ", ntohll(ts->wildcards));
1234 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1235 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1236 ds_put_cstr(string, " ");
1237 ds_put_format(string, "lookup=%"PRIu64", ", ntohll(ts->lookup_count));
1238 ds_put_format(string, "matched=%"PRIu64"\n", ntohll(ts->matched_count));
1240 if (ofp_version < OFP11_VERSION) {
1244 ds_put_cstr(string, " ");
1245 ds_put_format(string, "match=0x%08"PRIx64", ", ntohll(ts->match));
1246 ds_put_format(string, "instructions=0x%08"PRIx32", ",
1247 ntohl(ts->instructions));
1248 ds_put_format(string, "config=0x%08"PRIx32"\n", ntohl(ts->config));
1249 ds_put_cstr(string, " ");
1250 ds_put_format(string, "write_actions=0x%08"PRIx32", ",
1251 ntohl(ts->write_actions));
1252 ds_put_format(string, "apply_actions=0x%08"PRIx32"\n",
1253 ntohl(ts->apply_actions));
1255 if (ofp_version < OFP12_VERSION) {
1259 ds_put_cstr(string, " ");
1260 ds_put_format(string, "write_setfields=0x%016"PRIx64"\n",
1261 ntohll(ts->write_setfields));
1262 ds_put_cstr(string, " ");
1263 ds_put_format(string, "apply_setfields=0x%016"PRIx64"\n",
1264 ntohll(ts->apply_setfields));
1265 ds_put_cstr(string, " ");
1266 ds_put_format(string, "metadata_match=0x%016"PRIx64"\n",
1267 ntohll(ts->metadata_match));
1268 ds_put_cstr(string, " ");
1269 ds_put_format(string, "metadata_write=0x%016"PRIx64"\n",
1270 ntohll(ts->metadata_write));
1274 ofp_print_ofpst_table_reply13(struct ds *string, const struct ofp_header *oh,
1277 struct ofp13_table_stats *ts;
1281 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1282 ofpraw_pull_assert(&b);
1284 n = b.size / sizeof *ts;
1285 ds_put_format(string, " %zu tables\n", n);
1286 if (verbosity < 1) {
1291 ts = ofpbuf_try_pull(&b, sizeof *ts);
1295 ds_put_format(string,
1296 " %d: active=%"PRIu32", lookup=%"PRIu64 \
1297 ", matched=%"PRIu64"\n",
1298 ts->table_id, ntohl(ts->active_count),
1299 ntohll(ts->lookup_count), ntohll(ts->matched_count));
1304 ofp_print_ofpst_table_reply12(struct ds *string, const struct ofp_header *oh,
1307 struct ofp12_table_stats *ts;
1311 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1312 ofpraw_pull_assert(&b);
1314 n = b.size / sizeof *ts;
1315 ds_put_format(string, " %zu tables\n", n);
1316 if (verbosity < 1) {
1321 ts = ofpbuf_try_pull(&b, sizeof *ts);
1326 ofp_print_one_ofpst_table_reply(string, OFP12_VERSION, ts->name, ts);
1331 ofp_print_ofpst_table_reply11(struct ds *string, const struct ofp_header *oh,
1334 struct ofp11_table_stats *ts;
1338 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1339 ofpraw_pull_assert(&b);
1341 n = b.size / sizeof *ts;
1342 ds_put_format(string, " %zu tables\n", n);
1343 if (verbosity < 1) {
1348 struct ofp12_table_stats ts12;
1350 ts = ofpbuf_try_pull(&b, sizeof *ts);
1355 ts12.table_id = ts->table_id;
1356 ts12.wildcards = htonll(ntohl(ts->wildcards));
1357 ts12.max_entries = ts->max_entries;
1358 ts12.active_count = ts->active_count;
1359 ts12.lookup_count = ts->lookup_count;
1360 ts12.matched_count = ts->matched_count;
1361 ts12.match = htonll(ntohl(ts->match));
1362 ts12.instructions = ts->instructions;
1363 ts12.config = ts->config;
1364 ts12.write_actions = ts->write_actions;
1365 ts12.apply_actions = ts->apply_actions;
1366 ofp_print_one_ofpst_table_reply(string, OFP11_VERSION, ts->name, &ts12);
1371 ofp_print_ofpst_table_reply10(struct ds *string, const struct ofp_header *oh,
1374 struct ofp10_table_stats *ts;
1378 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1379 ofpraw_pull_assert(&b);
1381 n = b.size / sizeof *ts;
1382 ds_put_format(string, " %zu tables\n", n);
1383 if (verbosity < 1) {
1388 struct ofp12_table_stats ts12;
1390 ts = ofpbuf_try_pull(&b, sizeof *ts);
1395 ts12.table_id = ts->table_id;
1396 ts12.wildcards = htonll(ntohl(ts->wildcards));
1397 ts12.max_entries = ts->max_entries;
1398 ts12.active_count = ts->active_count;
1399 ts12.lookup_count = get_32aligned_be64(&ts->lookup_count);
1400 ts12.matched_count = get_32aligned_be64(&ts->matched_count);
1401 ofp_print_one_ofpst_table_reply(string, OFP10_VERSION, ts->name, &ts12);
1406 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1409 switch ((enum ofp_version)oh->version) {
1411 ofp_print_ofpst_table_reply13(string, oh, verbosity);
1415 ofp_print_ofpst_table_reply12(string, oh, verbosity);
1419 ofp_print_ofpst_table_reply11(string, oh, verbosity);
1423 ofp_print_ofpst_table_reply10(string, oh, verbosity);
1432 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1434 if (queue_id == OFPQ_ALL) {
1435 ds_put_cstr(string, "ALL");
1437 ds_put_format(string, "%"PRIu32, queue_id);
1442 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1444 struct ofputil_queue_stats_request oqsr;
1447 error = ofputil_decode_queue_stats_request(oh, &oqsr);
1449 ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1453 ds_put_cstr(string, "port=");
1454 ofputil_format_port(oqsr.port_no, string);
1456 ds_put_cstr(string, " queue=");
1457 ofp_print_queue_name(string, oqsr.queue_id);
1461 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1466 ds_put_format(string, " %zu queues\n", ofputil_count_queue_stats(oh));
1467 if (verbosity < 1) {
1471 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1473 struct ofputil_queue_stats qs;
1476 retval = ofputil_decode_queue_stats(&qs, &b);
1478 if (retval != EOF) {
1479 ds_put_cstr(string, " ***parse error***");
1484 ds_put_cstr(string, " port ");
1485 ofputil_format_port(qs.port_no, string);
1486 ds_put_cstr(string, " queue ");
1487 ofp_print_queue_name(string, qs.queue_id);
1488 ds_put_cstr(string, ": ");
1490 print_port_stat(string, "bytes=", qs.stats.tx_bytes, 1);
1491 print_port_stat(string, "pkts=", qs.stats.tx_packets, 1);
1492 print_port_stat(string, "errors=", qs.stats.tx_errors, 0);
1497 ofp_print_ofpst_port_desc_reply(struct ds *string,
1498 const struct ofp_header *oh)
1502 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1503 ofpraw_pull_assert(&b);
1504 ds_put_char(string, '\n');
1505 ofp_print_phy_ports(string, oh->version, &b);
1509 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1511 uint16_t flags = ofpmp_flags(oh);
1514 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
1519 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1521 uint16_t flags = ofpmp_flags(oh);
1524 ds_put_cstr(string, " flags=");
1525 if (flags & OFPSF_REPLY_MORE) {
1526 ds_put_cstr(string, "[more]");
1527 flags &= ~OFPSF_REPLY_MORE;
1530 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1537 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1539 size_t len = ntohs(oh->length);
1541 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1542 if (verbosity > 1) {
1543 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1548 ofp_print_role_message(struct ds *string, const struct ofp_header *oh)
1550 struct ofputil_role_request rr;
1553 error = ofputil_decode_role_message(oh, &rr);
1555 ofp_print_error(string, error);
1559 ds_put_cstr(string, " role=");
1562 case OFPCR12_ROLE_NOCHANGE:
1563 ds_put_cstr(string, "nochange");
1565 case OFPCR12_ROLE_EQUAL:
1566 ds_put_cstr(string, "equal"); /* OF 1.2 wording */
1568 case OFPCR12_ROLE_MASTER:
1569 ds_put_cstr(string, "master");
1571 case OFPCR12_ROLE_SLAVE:
1572 ds_put_cstr(string, "slave");
1578 if (rr.have_generation_id) {
1579 ds_put_format(string, " generation_id=%"PRIu64, rr.generation_id);
1584 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1585 const struct nx_flow_mod_table_id *nfmti)
1587 ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1591 ofp_print_nxt_set_flow_format(struct ds *string,
1592 const struct nx_set_flow_format *nsff)
1594 uint32_t format = ntohl(nsff->format);
1596 ds_put_cstr(string, " format=");
1597 if (ofputil_nx_flow_format_is_valid(format)) {
1598 ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
1600 ds_put_format(string, "%"PRIu32, format);
1605 ofp_print_nxt_set_packet_in_format(struct ds *string,
1606 const struct nx_set_packet_in_format *nspf)
1608 uint32_t format = ntohl(nspf->format);
1610 ds_put_cstr(string, " format=");
1611 if (ofputil_packet_in_format_is_valid(format)) {
1612 ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1614 ds_put_format(string, "%"PRIu32, format);
1619 ofp_port_reason_to_string(enum ofp_port_reason reason)
1634 sprintf(s, "%d", (int) reason);
1640 ofp_print_nxt_set_async_config(struct ds *string,
1641 const struct nx_async_config *nac)
1645 for (i = 0; i < 2; i++) {
1648 ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1650 ds_put_cstr(string, " PACKET_IN:");
1651 for (j = 0; j < 32; j++) {
1652 if (nac->packet_in_mask[i] & htonl(1u << j)) {
1653 ds_put_format(string, " %s",
1654 ofputil_packet_in_reason_to_string(j));
1657 if (!nac->packet_in_mask[i]) {
1658 ds_put_cstr(string, " (off)");
1660 ds_put_char(string, '\n');
1662 ds_put_cstr(string, " PORT_STATUS:");
1663 for (j = 0; j < 32; j++) {
1664 if (nac->port_status_mask[i] & htonl(1u << j)) {
1665 ds_put_format(string, " %s", ofp_port_reason_to_string(j));
1668 if (!nac->port_status_mask[i]) {
1669 ds_put_cstr(string, " (off)");
1671 ds_put_char(string, '\n');
1673 ds_put_cstr(string, " FLOW_REMOVED:");
1674 for (j = 0; j < 32; j++) {
1675 if (nac->flow_removed_mask[i] & htonl(1u << j)) {
1676 ds_put_format(string, " %s",
1677 ofp_flow_removed_reason_to_string(j));
1680 if (!nac->flow_removed_mask[i]) {
1681 ds_put_cstr(string, " (off)");
1683 ds_put_char(string, '\n');
1688 ofp_print_nxt_set_controller_id(struct ds *string,
1689 const struct nx_controller_id *nci)
1691 ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
1695 ofp_print_nxt_flow_monitor_cancel(struct ds *string,
1696 const struct ofp_header *oh)
1698 ds_put_format(string, " id=%"PRIu32,
1699 ofputil_decode_flow_monitor_cancel(oh));
1703 nx_flow_monitor_flags_to_name(uint32_t bit)
1705 enum nx_flow_monitor_flags fmf = bit;
1708 case NXFMF_INITIAL: return "initial";
1709 case NXFMF_ADD: return "add";
1710 case NXFMF_DELETE: return "delete";
1711 case NXFMF_MODIFY: return "modify";
1712 case NXFMF_ACTIONS: return "actions";
1713 case NXFMF_OWN: return "own";
1720 ofp_print_nxst_flow_monitor_request(struct ds *string,
1721 const struct ofp_header *oh)
1725 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1727 struct ofputil_flow_monitor_request request;
1730 retval = ofputil_decode_flow_monitor_request(&request, &b);
1732 if (retval != EOF) {
1733 ofp_print_error(string, retval);
1738 ds_put_format(string, "\n id=%"PRIu32" flags=", request.id);
1739 ofp_print_bit_names(string, request.flags,
1740 nx_flow_monitor_flags_to_name, ',');
1742 if (request.out_port != OFPP_NONE) {
1743 ds_put_cstr(string, " out_port=");
1744 ofputil_format_port(request.out_port, string);
1747 if (request.table_id != 0xff) {
1748 ds_put_format(string, " table=%"PRIu8, request.table_id);
1751 ds_put_char(string, ' ');
1752 match_format(&request.match, string, OFP_DEFAULT_PRIORITY);
1753 ds_chomp(string, ' ');
1758 ofp_print_nxst_flow_monitor_reply(struct ds *string,
1759 const struct ofp_header *oh)
1761 uint64_t ofpacts_stub[1024 / 8];
1762 struct ofpbuf ofpacts;
1765 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1766 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1768 struct ofputil_flow_update update;
1772 update.match = &match;
1773 retval = ofputil_decode_flow_update(&update, &b, &ofpacts);
1775 if (retval != EOF) {
1776 ofp_print_error(string, retval);
1778 ofpbuf_uninit(&ofpacts);
1782 ds_put_cstr(string, "\n event=");
1783 switch (update.event) {
1785 ds_put_cstr(string, "ADDED");
1789 ds_put_format(string, "DELETED reason=%s",
1790 ofp_flow_removed_reason_to_string(update.reason));
1793 case NXFME_MODIFIED:
1794 ds_put_cstr(string, "MODIFIED");
1798 ds_put_format(string, "ABBREV xid=0x%"PRIx32, ntohl(update.xid));
1802 ds_put_format(string, " table=%"PRIu8, update.table_id);
1803 if (update.idle_timeout != OFP_FLOW_PERMANENT) {
1804 ds_put_format(string, " idle_timeout=%"PRIu16,
1805 update.idle_timeout);
1807 if (update.hard_timeout != OFP_FLOW_PERMANENT) {
1808 ds_put_format(string, " hard_timeout=%"PRIu16,
1809 update.hard_timeout);
1811 ds_put_format(string, " cookie=%#"PRIx64, ntohll(update.cookie));
1813 ds_put_char(string, ' ');
1814 match_format(update.match, string, OFP_DEFAULT_PRIORITY);
1816 if (update.ofpacts_len) {
1817 if (string->string[string->length - 1] != ' ') {
1818 ds_put_char(string, ' ');
1820 ofpacts_format(update.ofpacts, update.ofpacts_len, string);
1826 ofp_print_version(const struct ofp_header *oh,
1829 switch (oh->version) {
1833 ds_put_cstr(string, " (OF1.1)");
1836 ds_put_cstr(string, " (OF1.2)");
1839 ds_put_cstr(string, " (OF1.3)");
1842 ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
1845 ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
1849 ofp_header_to_string__(const struct ofp_header *oh, enum ofpraw raw,
1852 ds_put_cstr(string, ofpraw_get_name(raw));
1853 ofp_print_version(oh, string);
1857 ofp_print_not_implemented(struct ds *string)
1859 ds_put_cstr(string, "NOT IMPLEMENTED YET!\n");
1863 ofp_to_string__(const struct ofp_header *oh, enum ofpraw raw,
1864 struct ds *string, int verbosity)
1866 const void *msg = oh;
1868 ofp_header_to_string__(oh, raw, string);
1869 switch (ofptype_from_ofpraw(raw)) {
1871 /* FIXME: Change the following once they are implemented: */
1872 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
1873 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
1874 case OFPTYPE_GET_ASYNC_REQUEST:
1875 case OFPTYPE_GET_ASYNC_REPLY:
1876 case OFPTYPE_METER_MOD:
1877 case OFPTYPE_GROUP_REQUEST:
1878 case OFPTYPE_GROUP_REPLY:
1879 case OFPTYPE_GROUP_DESC_REQUEST:
1880 case OFPTYPE_GROUP_DESC_REPLY:
1881 case OFPTYPE_GROUP_FEATURES_REQUEST:
1882 case OFPTYPE_GROUP_FEATURES_REPLY:
1883 case OFPTYPE_METER_REQUEST:
1884 case OFPTYPE_METER_REPLY:
1885 case OFPTYPE_METER_CONFIG_REQUEST:
1886 case OFPTYPE_METER_CONFIG_REPLY:
1887 case OFPTYPE_METER_FEATURES_REQUEST:
1888 case OFPTYPE_METER_FEATURES_REPLY:
1889 case OFPTYPE_TABLE_FEATURES_REQUEST:
1890 case OFPTYPE_TABLE_FEATURES_REPLY:
1891 ofp_print_not_implemented(string);
1895 ofp_print_hello(string, oh);
1899 ofp_print_error_msg(string, oh);
1902 case OFPTYPE_ECHO_REQUEST:
1903 case OFPTYPE_ECHO_REPLY:
1904 ofp_print_echo(string, oh, verbosity);
1907 case OFPTYPE_FEATURES_REQUEST:
1910 case OFPTYPE_FEATURES_REPLY:
1911 ofp_print_switch_features(string, oh);
1914 case OFPTYPE_GET_CONFIG_REQUEST:
1917 case OFPTYPE_GET_CONFIG_REPLY:
1918 case OFPTYPE_SET_CONFIG:
1919 ofp_print_switch_config(string, ofpmsg_body(oh));
1922 case OFPTYPE_PACKET_IN:
1923 ofp_print_packet_in(string, oh, verbosity);
1926 case OFPTYPE_FLOW_REMOVED:
1927 ofp_print_flow_removed(string, oh);
1930 case OFPTYPE_PORT_STATUS:
1931 ofp_print_port_status(string, oh);
1934 case OFPTYPE_PACKET_OUT:
1935 ofp_print_packet_out(string, oh, verbosity);
1938 case OFPTYPE_FLOW_MOD:
1939 ofp_print_flow_mod(string, oh, verbosity);
1942 case OFPTYPE_PORT_MOD:
1943 ofp_print_port_mod(string, oh);
1946 case OFPTYPE_BARRIER_REQUEST:
1947 case OFPTYPE_BARRIER_REPLY:
1950 case OFPTYPE_ROLE_REQUEST:
1951 case OFPTYPE_ROLE_REPLY:
1952 ofp_print_role_message(string, oh);
1955 case OFPTYPE_DESC_STATS_REQUEST:
1956 case OFPTYPE_PORT_DESC_STATS_REQUEST:
1957 ofp_print_stats_request(string, oh);
1960 case OFPTYPE_FLOW_STATS_REQUEST:
1961 case OFPTYPE_AGGREGATE_STATS_REQUEST:
1962 ofp_print_stats_request(string, oh);
1963 ofp_print_flow_stats_request(string, oh);
1966 case OFPTYPE_TABLE_STATS_REQUEST:
1967 ofp_print_stats_request(string, oh);
1970 case OFPTYPE_PORT_STATS_REQUEST:
1971 ofp_print_stats_request(string, oh);
1972 ofp_print_ofpst_port_request(string, oh);
1975 case OFPTYPE_QUEUE_STATS_REQUEST:
1976 ofp_print_stats_request(string, oh);
1977 ofp_print_ofpst_queue_request(string, oh);
1980 case OFPTYPE_DESC_STATS_REPLY:
1981 ofp_print_stats_reply(string, oh);
1982 ofp_print_ofpst_desc_reply(string, oh);
1985 case OFPTYPE_FLOW_STATS_REPLY:
1986 ofp_print_stats_reply(string, oh);
1987 ofp_print_flow_stats_reply(string, oh);
1990 case OFPTYPE_QUEUE_STATS_REPLY:
1991 ofp_print_stats_reply(string, oh);
1992 ofp_print_ofpst_queue_reply(string, oh, verbosity);
1995 case OFPTYPE_PORT_STATS_REPLY:
1996 ofp_print_stats_reply(string, oh);
1997 ofp_print_ofpst_port_reply(string, oh, verbosity);
2000 case OFPTYPE_TABLE_STATS_REPLY:
2001 ofp_print_stats_reply(string, oh);
2002 ofp_print_ofpst_table_reply(string, oh, verbosity);
2005 case OFPTYPE_AGGREGATE_STATS_REPLY:
2006 ofp_print_stats_reply(string, oh);
2007 ofp_print_aggregate_stats_reply(string, oh);
2010 case OFPTYPE_PORT_DESC_STATS_REPLY:
2011 ofp_print_stats_reply(string, oh);
2012 ofp_print_ofpst_port_desc_reply(string, oh);
2015 case OFPTYPE_FLOW_MOD_TABLE_ID:
2016 ofp_print_nxt_flow_mod_table_id(string, ofpmsg_body(oh));
2019 case OFPTYPE_SET_FLOW_FORMAT:
2020 ofp_print_nxt_set_flow_format(string, ofpmsg_body(oh));
2023 case OFPTYPE_SET_PACKET_IN_FORMAT:
2024 ofp_print_nxt_set_packet_in_format(string, ofpmsg_body(oh));
2027 case OFPTYPE_FLOW_AGE:
2030 case OFPTYPE_SET_CONTROLLER_ID:
2031 ofp_print_nxt_set_controller_id(string, ofpmsg_body(oh));
2034 case OFPTYPE_SET_ASYNC_CONFIG:
2035 ofp_print_nxt_set_async_config(string, ofpmsg_body(oh));
2038 case OFPTYPE_FLOW_MONITOR_CANCEL:
2039 ofp_print_nxt_flow_monitor_cancel(string, msg);
2042 case OFPTYPE_FLOW_MONITOR_PAUSED:
2043 case OFPTYPE_FLOW_MONITOR_RESUMED:
2046 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
2047 ofp_print_nxst_flow_monitor_request(string, msg);
2050 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
2051 ofp_print_nxst_flow_monitor_reply(string, msg);
2056 /* Composes and returns a string representing the OpenFlow packet of 'len'
2057 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
2058 * verbosity and higher numbers increase verbosity. The caller is responsible
2059 * for freeing the string. */
2061 ofp_to_string(const void *oh_, size_t len, int verbosity)
2063 struct ds string = DS_EMPTY_INITIALIZER;
2064 const struct ofp_header *oh = oh_;
2067 ds_put_cstr(&string, "OpenFlow message is empty\n");
2068 } else if (len < sizeof(struct ofp_header)) {
2069 ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
2071 } else if (ntohs(oh->length) > len) {
2075 error = ofpraw_decode_partial(&raw, oh, len);
2077 ofp_header_to_string__(oh, raw, &string);
2078 ds_put_char(&string, '\n');
2081 ds_put_format(&string,
2082 "(***truncated to %zu bytes from %"PRIu16"***)\n",
2083 len, ntohs(oh->length));
2084 } else if (ntohs(oh->length) < len) {
2085 ds_put_format(&string,
2086 "(***only uses %"PRIu16" bytes out of %zu***)\n",
2087 ntohs(oh->length), len);
2092 error = ofpraw_decode(&raw, oh);
2094 ofp_to_string__(oh, raw, &string, verbosity);
2095 if (verbosity >= 5) {
2096 if (ds_last(&string) != '\n') {
2097 ds_put_char(&string, '\n');
2099 ds_put_hex_dump(&string, oh, len, 0, true);
2101 if (ds_last(&string) != '\n') {
2102 ds_put_char(&string, '\n');
2104 return ds_steal_cstr(&string);
2107 ofp_print_error(&string, error);
2109 ds_put_hex_dump(&string, oh, len, 0, true);
2110 return ds_steal_cstr(&string);
2114 print_and_free(FILE *stream, char *string)
2116 fputs(string, stream);
2120 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
2121 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
2122 * numbers increase verbosity. */
2124 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
2126 print_and_free(stream, ofp_to_string(oh, len, verbosity));
2129 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
2130 * 'data' to 'stream'. */
2132 ofp_print_packet(FILE *stream, const void *data, size_t len)
2134 print_and_free(stream, ofp_packet_to_string(data, len));