2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 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"
48 #include "type-props.h"
49 #include "unaligned.h"
52 static void ofp_print_queue_name(struct ds
*string
, uint32_t port
);
53 static void ofp_print_error(struct ds
*, enum ofperr
);
56 /* Returns a string that represents the contents of the Ethernet frame in the
57 * 'len' bytes starting at 'data'. The caller must free the returned string.*/
59 ofp_packet_to_string(const void *data
, size_t len
)
61 struct ds ds
= DS_EMPTY_INITIALIZER
;
65 ofpbuf_use_const(&buf
, data
, len
);
66 flow_extract(&buf
, 0, NULL
, 0, &flow
);
67 flow_format(&ds
, &flow
);
70 if (flow
.nw_proto
== IPPROTO_TCP
) {
71 struct tcp_header
*th
= buf
.l4
;
72 ds_put_format(&ds
, " tcp_csum:%"PRIx16
,
74 } else if (flow
.nw_proto
== IPPROTO_UDP
) {
75 struct udp_header
*uh
= buf
.l4
;
76 ds_put_format(&ds
, " udp_csum:%"PRIx16
,
81 ds_put_char(&ds
, '\n');
87 ofp_print_packet_in(struct ds
*string
, const struct ofp_header
*oh
,
90 struct ofputil_packet_in pin
;
94 error
= ofputil_decode_packet_in(&pin
, oh
);
96 ofp_print_error(string
, error
);
101 ds_put_format(string
, " table_id=%"PRIu8
, pin
.table_id
);
105 ds_put_format(string
, " cookie=0x%"PRIx64
, ntohll(pin
.cookie
));
108 ds_put_format(string
, " total_len=%"PRIu16
" in_port=", pin
.total_len
);
109 ofputil_format_port(pin
.fmd
.in_port
, string
);
111 if (pin
.fmd
.tun_id
!= htonll(0)) {
112 ds_put_format(string
, " tun_id=0x%"PRIx64
, ntohll(pin
.fmd
.tun_id
));
115 if (pin
.fmd
.metadata
!= htonll(0)) {
116 ds_put_format(string
, " metadata=0x%"PRIx64
, ntohll(pin
.fmd
.metadata
));
119 for (i
= 0; i
< FLOW_N_REGS
; i
++) {
120 if (pin
.fmd
.regs
[i
]) {
121 ds_put_format(string
, " reg%d=0x%"PRIx32
, i
, pin
.fmd
.regs
[i
]);
125 ds_put_format(string
, " (via %s)",
126 ofputil_packet_in_reason_to_string(pin
.reason
));
128 ds_put_format(string
, " data_len=%zu", pin
.packet_len
);
129 if (pin
.buffer_id
== UINT32_MAX
) {
130 ds_put_format(string
, " (unbuffered)");
131 if (pin
.total_len
!= pin
.packet_len
) {
132 ds_put_format(string
, " (***total_len != data_len***)");
135 ds_put_format(string
, " buffer=0x%08"PRIx32
, pin
.buffer_id
);
136 if (pin
.total_len
< pin
.packet_len
) {
137 ds_put_format(string
, " (***total_len < data_len***)");
140 ds_put_char(string
, '\n');
143 char *packet
= ofp_packet_to_string(pin
.packet
, pin
.packet_len
);
144 ds_put_cstr(string
, packet
);
150 ofp_print_packet_out(struct ds
*string
, const struct ofp_header
*oh
,
153 struct ofputil_packet_out po
;
154 struct ofpbuf ofpacts
;
157 ofpbuf_init(&ofpacts
, 64);
158 error
= ofputil_decode_packet_out(&po
, oh
, &ofpacts
);
160 ofpbuf_uninit(&ofpacts
);
161 ofp_print_error(string
, error
);
165 ds_put_cstr(string
, " in_port=");
166 ofputil_format_port(po
.in_port
, string
);
168 ds_put_char(string
, ' ');
169 ofpacts_format(po
.ofpacts
, po
.ofpacts_len
, string
);
171 if (po
.buffer_id
== UINT32_MAX
) {
172 ds_put_format(string
, " data_len=%zu", po
.packet_len
);
173 if (verbosity
> 0 && po
.packet_len
> 0) {
174 char *packet
= ofp_packet_to_string(po
.packet
, po
.packet_len
);
175 ds_put_char(string
, '\n');
176 ds_put_cstr(string
, packet
);
180 ds_put_format(string
, " buffer=0x%08"PRIx32
, po
.buffer_id
);
182 ds_put_char(string
, '\n');
184 ofpbuf_uninit(&ofpacts
);
187 /* qsort comparison function. */
189 compare_ports(const void *a_
, const void *b_
)
191 const struct ofputil_phy_port
*a
= a_
;
192 const struct ofputil_phy_port
*b
= b_
;
193 uint16_t ap
= a
->port_no
;
194 uint16_t bp
= b
->port_no
;
196 return ap
< bp
? -1 : ap
> bp
;
200 ofp_print_bit_names(struct ds
*string
, uint32_t bits
,
201 const char *(*bit_to_name
)(uint32_t bit
),
208 ds_put_cstr(string
, "0");
212 for (i
= 0; i
< 32; i
++) {
213 uint32_t bit
= UINT32_C(1) << i
;
216 const char *name
= bit_to_name(bit
);
219 ds_put_char(string
, separator
);
221 ds_put_cstr(string
, name
);
229 ds_put_char(string
, separator
);
231 ds_put_format(string
, "0x%"PRIx32
, bits
);
236 netdev_feature_to_name(uint32_t bit
)
238 enum netdev_features f
= bit
;
241 case NETDEV_F_10MB_HD
: return "10MB-HD";
242 case NETDEV_F_10MB_FD
: return "10MB-FD";
243 case NETDEV_F_100MB_HD
: return "100MB-HD";
244 case NETDEV_F_100MB_FD
: return "100MB-FD";
245 case NETDEV_F_1GB_HD
: return "1GB-HD";
246 case NETDEV_F_1GB_FD
: return "1GB-FD";
247 case NETDEV_F_10GB_FD
: return "10GB-FD";
248 case NETDEV_F_40GB_FD
: return "40GB-FD";
249 case NETDEV_F_100GB_FD
: return "100GB-FD";
250 case NETDEV_F_1TB_FD
: return "1TB-FD";
251 case NETDEV_F_OTHER
: return "OTHER";
252 case NETDEV_F_COPPER
: return "COPPER";
253 case NETDEV_F_FIBER
: return "FIBER";
254 case NETDEV_F_AUTONEG
: return "AUTO_NEG";
255 case NETDEV_F_PAUSE
: return "AUTO_PAUSE";
256 case NETDEV_F_PAUSE_ASYM
: return "AUTO_PAUSE_ASYM";
263 ofp_print_port_features(struct ds
*string
, enum netdev_features features
)
265 ofp_print_bit_names(string
, features
, netdev_feature_to_name
, ' ');
266 ds_put_char(string
, '\n');
270 ofputil_port_config_to_name(uint32_t bit
)
272 enum ofputil_port_config pc
= bit
;
275 case OFPUTIL_PC_PORT_DOWN
: return "PORT_DOWN";
276 case OFPUTIL_PC_NO_STP
: return "NO_STP";
277 case OFPUTIL_PC_NO_RECV
: return "NO_RECV";
278 case OFPUTIL_PC_NO_RECV_STP
: return "NO_RECV_STP";
279 case OFPUTIL_PC_NO_FLOOD
: return "NO_FLOOD";
280 case OFPUTIL_PC_NO_FWD
: return "NO_FWD";
281 case OFPUTIL_PC_NO_PACKET_IN
: return "NO_PACKET_IN";
288 ofp_print_port_config(struct ds
*string
, enum ofputil_port_config config
)
290 ofp_print_bit_names(string
, config
, ofputil_port_config_to_name
, ' ');
291 ds_put_char(string
, '\n');
295 ofputil_port_state_to_name(uint32_t bit
)
297 enum ofputil_port_state ps
= bit
;
300 case OFPUTIL_PS_LINK_DOWN
: return "LINK_DOWN";
301 case OFPUTIL_PS_BLOCKED
: return "BLOCKED";
302 case OFPUTIL_PS_LIVE
: return "LIVE";
304 case OFPUTIL_PS_STP_LISTEN
:
305 case OFPUTIL_PS_STP_LEARN
:
306 case OFPUTIL_PS_STP_FORWARD
:
307 case OFPUTIL_PS_STP_BLOCK
:
308 /* Handled elsewhere. */
316 ofp_print_port_state(struct ds
*string
, enum ofputil_port_state state
)
318 enum ofputil_port_state stp_state
;
320 /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
321 * pattern. We have to special case it.
323 * OVS doesn't support STP, so this field will always be 0 if we are
324 * talking to OVS, so we'd always print STP_LISTEN in that case.
325 * Therefore, we don't print anything at all if the value is STP_LISTEN, to
326 * avoid confusing users. */
327 stp_state
= state
& OFPUTIL_PS_STP_MASK
;
330 (stp_state
== OFPUTIL_PS_STP_LEARN
? "STP_LEARN"
331 : stp_state
== OFPUTIL_PS_STP_FORWARD
? "STP_FORWARD"
333 state
&= ~OFPUTIL_PS_STP_MASK
;
335 ofp_print_bit_names(string
, state
, ofputil_port_state_to_name
,
339 ofp_print_bit_names(string
, state
, ofputil_port_state_to_name
, ' ');
341 ds_put_char(string
, '\n');
345 ofp_print_phy_port(struct ds
*string
, const struct ofputil_phy_port
*port
)
347 char name
[sizeof port
->name
];
350 memcpy(name
, port
->name
, sizeof name
);
351 for (j
= 0; j
< sizeof name
- 1; j
++) {
352 if (!isprint((unsigned char) name
[j
])) {
358 ds_put_char(string
, ' ');
359 ofputil_format_port(port
->port_no
, string
);
360 ds_put_format(string
, "(%s): addr:"ETH_ADDR_FMT
"\n",
361 name
, ETH_ADDR_ARGS(port
->hw_addr
));
363 ds_put_cstr(string
, " config: ");
364 ofp_print_port_config(string
, port
->config
);
366 ds_put_cstr(string
, " state: ");
367 ofp_print_port_state(string
, port
->state
);
370 ds_put_format(string
, " current: ");
371 ofp_print_port_features(string
, port
->curr
);
373 if (port
->advertised
) {
374 ds_put_format(string
, " advertised: ");
375 ofp_print_port_features(string
, port
->advertised
);
377 if (port
->supported
) {
378 ds_put_format(string
, " supported: ");
379 ofp_print_port_features(string
, port
->supported
);
382 ds_put_format(string
, " peer: ");
383 ofp_print_port_features(string
, port
->peer
);
385 ds_put_format(string
, " speed: %"PRIu32
" Mbps now, "
386 "%"PRIu32
" Mbps max\n",
387 port
->curr_speed
/ UINT32_C(1000),
388 port
->max_speed
/ UINT32_C(1000));
391 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
392 * 'ofp_version', writes a detailed description of each port into
395 ofp_print_phy_ports(struct ds
*string
, uint8_t ofp_version
,
399 struct ofputil_phy_port
*ports
;
403 n_ports
= ofputil_count_phy_ports(ofp_version
, b
);
405 ports
= xmalloc(n_ports
* sizeof *ports
);
406 for (i
= 0; i
< n_ports
; i
++) {
407 error
= ofputil_pull_phy_port(ofp_version
, b
, &ports
[i
]);
409 ofp_print_error(string
, error
);
413 qsort(ports
, n_ports
, sizeof *ports
, compare_ports
);
414 for (i
= 0; i
< n_ports
; i
++) {
415 ofp_print_phy_port(string
, &ports
[i
]);
423 ofputil_capabilities_to_name(uint32_t bit
)
425 enum ofputil_capabilities capabilities
= bit
;
427 switch (capabilities
) {
428 case OFPUTIL_C_FLOW_STATS
: return "FLOW_STATS";
429 case OFPUTIL_C_TABLE_STATS
: return "TABLE_STATS";
430 case OFPUTIL_C_PORT_STATS
: return "PORT_STATS";
431 case OFPUTIL_C_IP_REASM
: return "IP_REASM";
432 case OFPUTIL_C_QUEUE_STATS
: return "QUEUE_STATS";
433 case OFPUTIL_C_ARP_MATCH_IP
: return "ARP_MATCH_IP";
434 case OFPUTIL_C_STP
: return "STP";
435 case OFPUTIL_C_GROUP_STATS
: return "GROUP_STATS";
436 case OFPUTIL_C_PORT_BLOCKED
: return "PORT_BLOCKED";
443 ofputil_action_bitmap_to_name(uint32_t bit
)
445 enum ofputil_action_bitmap action
= bit
;
448 case OFPUTIL_A_OUTPUT
: return "OUTPUT";
449 case OFPUTIL_A_SET_VLAN_VID
: return "SET_VLAN_VID";
450 case OFPUTIL_A_SET_VLAN_PCP
: return "SET_VLAN_PCP";
451 case OFPUTIL_A_STRIP_VLAN
: return "STRIP_VLAN";
452 case OFPUTIL_A_SET_DL_SRC
: return "SET_DL_SRC";
453 case OFPUTIL_A_SET_DL_DST
: return "SET_DL_DST";
454 case OFPUTIL_A_SET_NW_SRC
: return "SET_NW_SRC";
455 case OFPUTIL_A_SET_NW_DST
: return "SET_NW_DST";
456 case OFPUTIL_A_SET_NW_ECN
: return "SET_NW_ECN";
457 case OFPUTIL_A_SET_NW_TOS
: return "SET_NW_TOS";
458 case OFPUTIL_A_SET_TP_SRC
: return "SET_TP_SRC";
459 case OFPUTIL_A_SET_TP_DST
: return "SET_TP_DST";
460 case OFPUTIL_A_SET_FIELD
: return "SET_FIELD";
461 case OFPUTIL_A_ENQUEUE
: return "ENQUEUE";
462 case OFPUTIL_A_COPY_TTL_OUT
: return "COPY_TTL_OUT";
463 case OFPUTIL_A_COPY_TTL_IN
: return "COPY_TTL_IN";
464 case OFPUTIL_A_SET_MPLS_LABEL
: return "SET_MPLS_LABEL";
465 case OFPUTIL_A_SET_MPLS_TC
: return "SET_MPLS_TC";
466 case OFPUTIL_A_SET_MPLS_TTL
: return "SET_MPLS_TTL";
467 case OFPUTIL_A_DEC_MPLS_TTL
: return "DEC_MPLS_TTL";
468 case OFPUTIL_A_PUSH_VLAN
: return "PUSH_VLAN";
469 case OFPUTIL_A_POP_VLAN
: return "POP_VLAN";
470 case OFPUTIL_A_PUSH_MPLS
: return "PUSH_MPLS";
471 case OFPUTIL_A_POP_MPLS
: return "POP_MPLS";
472 case OFPUTIL_A_SET_QUEUE
: return "SET_QUEUE";
473 case OFPUTIL_A_GROUP
: return "GROUP";
474 case OFPUTIL_A_SET_NW_TTL
: return "SET_NW_TTL";
475 case OFPUTIL_A_DEC_NW_TTL
: return "DEC_NW_TTL";
482 ofp_print_switch_features(struct ds
*string
, const struct ofp_header
*oh
)
484 struct ofputil_switch_features features
;
488 error
= ofputil_decode_switch_features(oh
, &features
, &b
);
490 ofp_print_error(string
, error
);
494 ds_put_format(string
, " dpid:%016"PRIx64
"\n", features
.datapath_id
);
495 ds_put_format(string
, "n_tables:%"PRIu8
", n_buffers:%"PRIu32
"\n",
496 features
.n_tables
, features
.n_buffers
);
498 ds_put_cstr(string
, "capabilities: ");
499 ofp_print_bit_names(string
, features
.capabilities
,
500 ofputil_capabilities_to_name
, ' ');
501 ds_put_char(string
, '\n');
503 switch ((enum ofp_version
)oh
->version
) {
505 ds_put_cstr(string
, "actions: ");
506 ofp_print_bit_names(string
, features
.actions
,
507 ofputil_action_bitmap_to_name
, ' ');
508 ds_put_char(string
, '\n');
517 ofp_print_phy_ports(string
, oh
->version
, &b
);
521 ofp_print_switch_config(struct ds
*string
, const struct ofp_switch_config
*osc
)
523 enum ofp_config_flags flags
;
525 flags
= ntohs(osc
->flags
);
527 ds_put_format(string
, " frags=%s", ofputil_frag_handling_to_string(flags
));
528 flags
&= ~OFPC_FRAG_MASK
;
530 if (flags
& OFPC_INVALID_TTL_TO_CONTROLLER
) {
531 ds_put_format(string
, " invalid_ttl_to_controller");
532 flags
&= ~OFPC_INVALID_TTL_TO_CONTROLLER
;
536 ds_put_format(string
, " ***unknown flags 0x%04"PRIx16
"***", flags
);
539 ds_put_format(string
, " miss_send_len=%"PRIu16
"\n", ntohs(osc
->miss_send_len
));
542 static void print_wild(struct ds
*string
, const char *leader
, int is_wild
,
543 int verbosity
, const char *format
, ...)
544 __attribute__((format(printf
, 5, 6)));
546 static void print_wild(struct ds
*string
, const char *leader
, int is_wild
,
547 int verbosity
, const char *format
, ...)
549 if (is_wild
&& verbosity
< 2) {
552 ds_put_cstr(string
, leader
);
556 va_start(args
, format
);
557 ds_put_format_valist(string
, format
, args
);
560 ds_put_char(string
, '*');
562 ds_put_char(string
, ',');
566 print_ip_netmask(struct ds
*string
, const char *leader
, ovs_be32 ip
,
567 uint32_t wild_bits
, int verbosity
)
569 if (wild_bits
>= 32 && verbosity
< 2) {
572 ds_put_cstr(string
, leader
);
573 if (wild_bits
< 32) {
574 ds_put_format(string
, IP_FMT
, IP_ARGS(&ip
));
576 ds_put_format(string
, "/%d", 32 - wild_bits
);
579 ds_put_char(string
, '*');
581 ds_put_char(string
, ',');
585 ofp10_match_print(struct ds
*f
, const struct ofp10_match
*om
, int verbosity
)
587 char *s
= ofp10_match_to_string(om
, verbosity
);
593 ofp10_match_to_string(const struct ofp10_match
*om
, int verbosity
)
595 struct ds f
= DS_EMPTY_INITIALIZER
;
596 uint32_t w
= ntohl(om
->wildcards
);
597 bool skip_type
= false;
598 bool skip_proto
= false;
600 if (!(w
& OFPFW10_DL_TYPE
)) {
602 if (om
->dl_type
== htons(ETH_TYPE_IP
)) {
603 if (!(w
& OFPFW10_NW_PROTO
)) {
605 if (om
->nw_proto
== IPPROTO_ICMP
) {
606 ds_put_cstr(&f
, "icmp,");
607 } else if (om
->nw_proto
== IPPROTO_TCP
) {
608 ds_put_cstr(&f
, "tcp,");
609 } else if (om
->nw_proto
== IPPROTO_UDP
) {
610 ds_put_cstr(&f
, "udp,");
612 ds_put_cstr(&f
, "ip,");
616 ds_put_cstr(&f
, "ip,");
618 } else if (om
->dl_type
== htons(ETH_TYPE_ARP
)) {
619 ds_put_cstr(&f
, "arp,");
624 print_wild(&f
, "in_port=", w
& OFPFW10_IN_PORT
, verbosity
,
625 "%d", ntohs(om
->in_port
));
626 print_wild(&f
, "dl_vlan=", w
& OFPFW10_DL_VLAN
, verbosity
,
627 "%d", ntohs(om
->dl_vlan
));
628 print_wild(&f
, "dl_vlan_pcp=", w
& OFPFW10_DL_VLAN_PCP
, verbosity
,
629 "%d", om
->dl_vlan_pcp
);
630 print_wild(&f
, "dl_src=", w
& OFPFW10_DL_SRC
, verbosity
,
631 ETH_ADDR_FMT
, ETH_ADDR_ARGS(om
->dl_src
));
632 print_wild(&f
, "dl_dst=", w
& OFPFW10_DL_DST
, verbosity
,
633 ETH_ADDR_FMT
, ETH_ADDR_ARGS(om
->dl_dst
));
635 print_wild(&f
, "dl_type=", w
& OFPFW10_DL_TYPE
, verbosity
,
636 "0x%04x", ntohs(om
->dl_type
));
638 print_ip_netmask(&f
, "nw_src=", om
->nw_src
,
639 (w
& OFPFW10_NW_SRC_MASK
) >> OFPFW10_NW_SRC_SHIFT
,
641 print_ip_netmask(&f
, "nw_dst=", om
->nw_dst
,
642 (w
& OFPFW10_NW_DST_MASK
) >> OFPFW10_NW_DST_SHIFT
,
645 if (om
->dl_type
== htons(ETH_TYPE_ARP
)) {
646 print_wild(&f
, "arp_op=", w
& OFPFW10_NW_PROTO
, verbosity
,
649 print_wild(&f
, "nw_proto=", w
& OFPFW10_NW_PROTO
, verbosity
,
653 print_wild(&f
, "nw_tos=", w
& OFPFW10_NW_TOS
, verbosity
,
655 if (om
->nw_proto
== IPPROTO_ICMP
) {
656 print_wild(&f
, "icmp_type=", w
& OFPFW10_ICMP_TYPE
, verbosity
,
657 "%d", ntohs(om
->tp_src
));
658 print_wild(&f
, "icmp_code=", w
& OFPFW10_ICMP_CODE
, verbosity
,
659 "%d", ntohs(om
->tp_dst
));
661 print_wild(&f
, "tp_src=", w
& OFPFW10_TP_SRC
, verbosity
,
662 "%d", ntohs(om
->tp_src
));
663 print_wild(&f
, "tp_dst=", w
& OFPFW10_TP_DST
, verbosity
,
664 "%d", ntohs(om
->tp_dst
));
666 if (ds_last(&f
) == ',') {
673 ofp_print_flow_mod(struct ds
*s
, const struct ofp_header
*oh
, int verbosity
)
675 struct ofputil_flow_mod fm
;
676 struct ofpbuf ofpacts
;
680 enum ofputil_protocol protocol
;
682 protocol
= ofputil_protocol_from_ofp_version(oh
->version
);
683 protocol
= ofputil_protocol_set_tid(protocol
, true);
685 ofpbuf_init(&ofpacts
, 64);
686 error
= ofputil_decode_flow_mod(&fm
, oh
, protocol
, &ofpacts
);
688 ofpbuf_uninit(&ofpacts
);
689 ofp_print_error(s
, error
);
694 switch (fm
.command
) {
696 ds_put_cstr(s
, "ADD");
699 ds_put_cstr(s
, "MOD");
701 case OFPFC_MODIFY_STRICT
:
702 ds_put_cstr(s
, "MOD_STRICT");
705 ds_put_cstr(s
, "DEL");
707 case OFPFC_DELETE_STRICT
:
708 ds_put_cstr(s
, "DEL_STRICT");
711 ds_put_format(s
, "cmd:%d", fm
.command
);
713 if (fm
.table_id
!= 0) {
714 ds_put_format(s
, " table:%d", fm
.table_id
);
718 ofpraw_decode(&raw
, oh
);
719 if (verbosity
>= 3 && raw
== OFPRAW_OFPT10_FLOW_MOD
) {
720 const struct ofp10_flow_mod
*ofm
= ofpmsg_body(oh
);
721 ofp10_match_print(s
, &ofm
->match
, verbosity
);
723 /* ofp_print_match() doesn't print priority. */
724 need_priority
= true;
725 } else if (verbosity
>= 3 && raw
== OFPRAW_NXT_FLOW_MOD
) {
726 const struct nx_flow_mod
*nfm
= ofpmsg_body(oh
);
727 const void *nxm
= nfm
+ 1;
730 nxm_s
= nx_match_to_string(nxm
, ntohs(nfm
->match_len
));
731 ds_put_cstr(s
, nxm_s
);
734 /* nx_match_to_string() doesn't print priority. */
735 need_priority
= true;
737 match_format(&fm
.match
, s
, fm
.priority
);
739 /* match_format() does print priority. */
740 need_priority
= false;
743 if (ds_last(s
) != ' ') {
746 if (fm
.new_cookie
!= htonll(0)) {
747 ds_put_format(s
, "cookie:0x%"PRIx64
" ", ntohll(fm
.new_cookie
));
749 if (fm
.cookie_mask
!= htonll(0)) {
750 ds_put_format(s
, "cookie:0x%"PRIx64
"/0x%"PRIx64
" ",
751 ntohll(fm
.cookie
), ntohll(fm
.cookie_mask
));
753 if (fm
.idle_timeout
!= OFP_FLOW_PERMANENT
) {
754 ds_put_format(s
, "idle:%"PRIu16
" ", fm
.idle_timeout
);
756 if (fm
.hard_timeout
!= OFP_FLOW_PERMANENT
) {
757 ds_put_format(s
, "hard:%"PRIu16
" ", fm
.hard_timeout
);
759 if (fm
.priority
!= OFP_DEFAULT_PRIORITY
&& need_priority
) {
760 ds_put_format(s
, "pri:%"PRIu16
" ", fm
.priority
);
762 if (fm
.buffer_id
!= UINT32_MAX
) {
763 ds_put_format(s
, "buf:0x%"PRIx32
" ", fm
.buffer_id
);
765 if (fm
.out_port
!= OFPP_NONE
) {
766 ds_put_format(s
, "out_port:");
767 ofputil_format_port(fm
.out_port
, s
);
771 uint16_t flags
= fm
.flags
;
773 if (flags
& OFPFF_SEND_FLOW_REM
) {
774 ds_put_cstr(s
, "send_flow_rem ");
776 if (flags
& OFPFF_CHECK_OVERLAP
) {
777 ds_put_cstr(s
, "check_overlap ");
779 if (flags
& OFPFF10_EMERG
) {
780 ds_put_cstr(s
, "emerg ");
783 flags
&= ~(OFPFF_SEND_FLOW_REM
| OFPFF_CHECK_OVERLAP
| OFPFF10_EMERG
);
785 ds_put_format(s
, "flags:0x%"PRIx16
" ", flags
);
789 ofpacts_format(fm
.ofpacts
, fm
.ofpacts_len
, s
);
790 ofpbuf_uninit(&ofpacts
);
794 ofp_print_duration(struct ds
*string
, unsigned int sec
, unsigned int nsec
)
796 ds_put_format(string
, "%u", sec
);
798 ds_put_format(string
, ".%09u", nsec
);
799 while (string
->string
[string
->length
- 1] == '0') {
803 ds_put_char(string
, 's');
807 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason
)
812 case OFPRR_IDLE_TIMEOUT
:
814 case OFPRR_HARD_TIMEOUT
:
818 case OFPRR_GROUP_DELETE
:
819 return "group_delete";
823 sprintf(s
, "%d", (int) reason
);
829 ofp_print_flow_removed(struct ds
*string
, const struct ofp_header
*oh
)
831 struct ofputil_flow_removed fr
;
834 error
= ofputil_decode_flow_removed(&fr
, oh
);
836 ofp_print_error(string
, error
);
840 ds_put_char(string
, ' ');
841 match_format(&fr
.match
, string
, fr
.priority
);
843 ds_put_format(string
, " reason=%s",
844 ofp_flow_removed_reason_to_string(fr
.reason
));
846 if (fr
.table_id
!= 255) {
847 ds_put_format(string
, " table_id=%"PRIu8
, fr
.table_id
);
850 if (fr
.cookie
!= htonll(0)) {
851 ds_put_format(string
, " cookie:0x%"PRIx64
, ntohll(fr
.cookie
));
853 ds_put_cstr(string
, " duration");
854 ofp_print_duration(string
, fr
.duration_sec
, fr
.duration_nsec
);
855 ds_put_format(string
, " idle%"PRIu16
, fr
.idle_timeout
);
856 if (fr
.hard_timeout
) {
857 /* The hard timeout was only added in OF1.2, so only print it if it is
858 * actually in use to avoid gratuitous change to the formatting. */
859 ds_put_format(string
, " hard%"PRIu16
, fr
.hard_timeout
);
861 ds_put_format(string
, " pkts%"PRIu64
" bytes%"PRIu64
"\n",
862 fr
.packet_count
, fr
.byte_count
);
866 ofp_print_port_mod(struct ds
*string
, const struct ofp_header
*oh
)
868 struct ofputil_port_mod pm
;
871 error
= ofputil_decode_port_mod(oh
, &pm
);
873 ofp_print_error(string
, error
);
877 ds_put_format(string
, "port: %"PRIu16
": addr:"ETH_ADDR_FMT
"\n",
878 pm
.port_no
, ETH_ADDR_ARGS(pm
.hw_addr
));
880 ds_put_format(string
, " config: ");
881 ofp_print_port_config(string
, pm
.config
);
883 ds_put_format(string
, " mask: ");
884 ofp_print_port_config(string
, pm
.mask
);
886 ds_put_format(string
, " advertise: ");
888 ofp_print_port_features(string
, pm
.advertise
);
890 ds_put_format(string
, "UNCHANGED\n");
895 ofp_print_error(struct ds
*string
, enum ofperr error
)
897 if (string
->length
) {
898 ds_put_char(string
, ' ');
900 ds_put_format(string
, "***decode error: %s***\n", ofperr_get_name(error
));
904 ofp_print_error_msg(struct ds
*string
, const struct ofp_header
*oh
)
906 size_t len
= ntohs(oh
->length
);
907 struct ofpbuf payload
;
911 error
= ofperr_decode_msg(oh
, &payload
);
913 ds_put_cstr(string
, "***decode error***");
914 ds_put_hex_dump(string
, oh
+ 1, len
- sizeof *oh
, 0, true);
918 ds_put_format(string
, " %s\n", ofperr_get_name(error
));
920 if (error
== OFPERR_OFPHFC_INCOMPATIBLE
|| error
== OFPERR_OFPHFC_EPERM
) {
921 ds_put_printable(string
, payload
.data
, payload
.size
);
923 s
= ofp_to_string(payload
.data
, payload
.size
, 1);
924 ds_put_cstr(string
, s
);
930 ofp_print_port_status(struct ds
*string
, const struct ofp_header
*oh
)
932 struct ofputil_port_status ps
;
935 error
= ofputil_decode_port_status(oh
, &ps
);
937 ofp_print_error(string
, error
);
941 if (ps
.reason
== OFPPR_ADD
) {
942 ds_put_format(string
, " ADD:");
943 } else if (ps
.reason
== OFPPR_DELETE
) {
944 ds_put_format(string
, " DEL:");
945 } else if (ps
.reason
== OFPPR_MODIFY
) {
946 ds_put_format(string
, " MOD:");
949 ofp_print_phy_port(string
, &ps
.desc
);
953 ofp_print_ofpst_desc_reply(struct ds
*string
, const struct ofp_header
*oh
)
955 const struct ofp_desc_stats
*ods
= ofpmsg_body(oh
);
957 ds_put_char(string
, '\n');
958 ds_put_format(string
, "Manufacturer: %.*s\n",
959 (int) sizeof ods
->mfr_desc
, ods
->mfr_desc
);
960 ds_put_format(string
, "Hardware: %.*s\n",
961 (int) sizeof ods
->hw_desc
, ods
->hw_desc
);
962 ds_put_format(string
, "Software: %.*s\n",
963 (int) sizeof ods
->sw_desc
, ods
->sw_desc
);
964 ds_put_format(string
, "Serial Num: %.*s\n",
965 (int) sizeof ods
->serial_num
, ods
->serial_num
);
966 ds_put_format(string
, "DP Description: %.*s\n",
967 (int) sizeof ods
->dp_desc
, ods
->dp_desc
);
971 ofp_print_flow_stats_request(struct ds
*string
, const struct ofp_header
*oh
)
973 struct ofputil_flow_stats_request fsr
;
976 error
= ofputil_decode_flow_stats_request(&fsr
, oh
);
978 ofp_print_error(string
, error
);
982 if (fsr
.table_id
!= 0xff) {
983 ds_put_format(string
, " table=%"PRIu8
, fsr
.table_id
);
986 if (fsr
.out_port
!= OFPP_NONE
) {
987 ds_put_cstr(string
, " out_port=");
988 ofputil_format_port(fsr
.out_port
, string
);
991 ds_put_char(string
, ' ');
992 match_format(&fsr
.match
, string
, OFP_DEFAULT_PRIORITY
);
996 ofp_print_flow_stats(struct ds
*string
, struct ofputil_flow_stats
*fs
)
998 ds_put_format(string
, " cookie=0x%"PRIx64
", duration=",
1001 ofp_print_duration(string
, fs
->duration_sec
, fs
->duration_nsec
);
1002 ds_put_format(string
, ", table=%"PRIu8
", ", fs
->table_id
);
1003 ds_put_format(string
, "n_packets=%"PRIu64
", ", fs
->packet_count
);
1004 ds_put_format(string
, "n_bytes=%"PRIu64
", ", fs
->byte_count
);
1005 if (fs
->idle_timeout
!= OFP_FLOW_PERMANENT
) {
1006 ds_put_format(string
, "idle_timeout=%"PRIu16
", ", fs
->idle_timeout
);
1008 if (fs
->hard_timeout
!= OFP_FLOW_PERMANENT
) {
1009 ds_put_format(string
, "hard_timeout=%"PRIu16
", ", fs
->hard_timeout
);
1011 if (fs
->idle_age
>= 0) {
1012 ds_put_format(string
, "idle_age=%d, ", fs
->idle_age
);
1014 if (fs
->hard_age
>= 0 && fs
->hard_age
!= fs
->duration_sec
) {
1015 ds_put_format(string
, "hard_age=%d, ", fs
->hard_age
);
1018 match_format(&fs
->match
, string
, fs
->priority
);
1019 if (string
->string
[string
->length
- 1] != ' ') {
1020 ds_put_char(string
, ' ');
1023 ofpacts_format(fs
->ofpacts
, fs
->ofpacts_len
, string
);
1027 ofp_print_flow_stats_reply(struct ds
*string
, const struct ofp_header
*oh
)
1029 struct ofpbuf ofpacts
;
1032 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
1033 ofpbuf_init(&ofpacts
, 64);
1035 struct ofputil_flow_stats fs
;
1038 retval
= ofputil_decode_flow_stats_reply(&fs
, &b
, true, &ofpacts
);
1040 if (retval
!= EOF
) {
1041 ds_put_cstr(string
, " ***parse error***");
1045 ds_put_char(string
, '\n');
1046 ofp_print_flow_stats(string
, &fs
);
1048 ofpbuf_uninit(&ofpacts
);
1052 ofp_print_aggregate_stats_reply(struct ds
*string
, const struct ofp_header
*oh
)
1054 struct ofputil_aggregate_stats as
;
1057 error
= ofputil_decode_aggregate_stats_reply(&as
, oh
);
1059 ofp_print_error(string
, error
);
1063 ds_put_format(string
, " packet_count=%"PRIu64
, as
.packet_count
);
1064 ds_put_format(string
, " byte_count=%"PRIu64
, as
.byte_count
);
1065 ds_put_format(string
, " flow_count=%"PRIu32
, as
.flow_count
);
1069 print_port_stat(struct ds
*string
, const char *leader
, uint64_t stat
, int more
)
1071 ds_put_cstr(string
, leader
);
1072 if (stat
!= UINT64_MAX
) {
1073 ds_put_format(string
, "%"PRIu64
, stat
);
1075 ds_put_char(string
, '?');
1078 ds_put_cstr(string
, ", ");
1080 ds_put_cstr(string
, "\n");
1085 ofp_print_ofpst_port_request(struct ds
*string
, const struct ofp_header
*oh
)
1087 uint16_t ofp10_port
;
1090 error
= ofputil_decode_port_stats_request(oh
, &ofp10_port
);
1092 ofp_print_error(string
, error
);
1096 ds_put_format(string
, " port_no=%2"PRIu16
, ofp10_port
);
1100 ofp_print_ofpst_port_reply(struct ds
*string
, const struct ofp_header
*oh
,
1105 ds_put_format(string
, " %zu ports\n", ofputil_count_port_stats(oh
));
1106 if (verbosity
< 1) {
1110 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
1112 struct ofputil_port_stats ps
;
1115 retval
= ofputil_decode_port_stats(&ps
, &b
);
1117 if (retval
!= EOF
) {
1118 ds_put_cstr(string
, " ***parse error***");
1123 ds_put_format(string
, " port %2"PRIu16
, ps
.port_no
);
1125 ds_put_cstr(string
, ": rx ");
1126 print_port_stat(string
, "pkts=", ps
.stats
.rx_packets
, 1);
1127 print_port_stat(string
, "bytes=", ps
.stats
.rx_bytes
, 1);
1128 print_port_stat(string
, "drop=", ps
.stats
.rx_dropped
, 1);
1129 print_port_stat(string
, "errs=", ps
.stats
.rx_errors
, 1);
1130 print_port_stat(string
, "frame=", ps
.stats
.rx_frame_errors
, 1);
1131 print_port_stat(string
, "over=", ps
.stats
.rx_over_errors
, 1);
1132 print_port_stat(string
, "crc=", ps
.stats
.rx_crc_errors
, 0);
1134 ds_put_cstr(string
, " tx ");
1135 print_port_stat(string
, "pkts=", ps
.stats
.tx_packets
, 1);
1136 print_port_stat(string
, "bytes=", ps
.stats
.tx_bytes
, 1);
1137 print_port_stat(string
, "drop=", ps
.stats
.tx_dropped
, 1);
1138 print_port_stat(string
, "errs=", ps
.stats
.tx_errors
, 1);
1139 print_port_stat(string
, "coll=", ps
.stats
.collisions
, 0);
1144 ofp_print_one_ofpst_table_reply(struct ds
*string
, enum ofp_version ofp_version
,
1145 const char *name
, struct ofp12_table_stats
*ts
)
1147 char name_
[OFP_MAX_TABLE_NAME_LEN
+ 1];
1149 ovs_strlcpy(name_
, name
, sizeof name_
);
1151 ds_put_format(string
, " %d: %-8s: ", ts
->table_id
, name_
);
1152 ds_put_format(string
, "wild=0x%05"PRIx64
", ", ntohll(ts
->wildcards
));
1153 ds_put_format(string
, "max=%6"PRIu32
", ", ntohl(ts
->max_entries
));
1154 ds_put_format(string
, "active=%"PRIu32
"\n", ntohl(ts
->active_count
));
1155 ds_put_cstr(string
, " ");
1156 ds_put_format(string
, "lookup=%"PRIu64
", ", ntohll(ts
->lookup_count
));
1157 ds_put_format(string
, "matched=%"PRIu64
"\n", ntohll(ts
->matched_count
));
1159 if (ofp_version
< OFP11_VERSION
) {
1163 ds_put_cstr(string
, " ");
1164 ds_put_format(string
, "match=0x%08"PRIx64
", ", ntohll(ts
->match
));
1165 ds_put_format(string
, "instructions=0x%08"PRIx32
", ",
1166 ntohl(ts
->instructions
));
1167 ds_put_format(string
, "config=0x%08"PRIx32
"\n", ntohl(ts
->config
));
1168 ds_put_cstr(string
, " ");
1169 ds_put_format(string
, "write_actions=0x%08"PRIx32
", ",
1170 ntohl(ts
->write_actions
));
1171 ds_put_format(string
, "apply_actions=0x%08"PRIx32
"\n",
1172 ntohl(ts
->apply_actions
));
1174 if (ofp_version
< OFP12_VERSION
) {
1178 ds_put_cstr(string
, " ");
1179 ds_put_format(string
, "write_setfields=0x%016"PRIx64
"\n",
1180 ntohll(ts
->write_setfields
));
1181 ds_put_cstr(string
, " ");
1182 ds_put_format(string
, "apply_setfields=0x%016"PRIx64
"\n",
1183 ntohll(ts
->apply_setfields
));
1184 ds_put_cstr(string
, " ");
1185 ds_put_format(string
, "metadata_match=0x%016"PRIx64
"\n",
1186 ntohll(ts
->metadata_match
));
1187 ds_put_cstr(string
, " ");
1188 ds_put_format(string
, "metadata_write=0x%016"PRIx64
"\n",
1189 ntohll(ts
->metadata_write
));
1193 ofp_print_ofpst_table_reply12(struct ds
*string
, const struct ofp_header
*oh
,
1196 struct ofp12_table_stats
*ts
;
1200 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
1201 ofpraw_pull_assert(&b
);
1203 n
= b
.size
/ sizeof *ts
;
1204 ds_put_format(string
, " %zu tables\n", n
);
1205 if (verbosity
< 1) {
1210 ts
= ofpbuf_try_pull(&b
, sizeof *ts
);
1215 ofp_print_one_ofpst_table_reply(string
, OFP12_VERSION
, ts
->name
, ts
);
1220 ofp_print_ofpst_table_reply11(struct ds
*string
, const struct ofp_header
*oh
,
1223 struct ofp11_table_stats
*ts
;
1227 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
1228 ofpraw_pull_assert(&b
);
1230 n
= b
.size
/ sizeof *ts
;
1231 ds_put_format(string
, " %zu tables\n", n
);
1232 if (verbosity
< 1) {
1237 struct ofp12_table_stats ts12
;
1239 ts
= ofpbuf_try_pull(&b
, sizeof *ts
);
1244 ts12
.table_id
= ts
->table_id
;
1245 ts12
.wildcards
= htonll(ntohl(ts
->wildcards
));
1246 ts12
.max_entries
= ts
->max_entries
;
1247 ts12
.active_count
= ts
->active_count
;
1248 ts12
.lookup_count
= ts
->lookup_count
;
1249 ts12
.matched_count
= ts
->matched_count
;
1250 ts12
.match
= htonll(ntohl(ts
->match
));
1251 ts12
.instructions
= ts
->instructions
;
1252 ts12
.config
= ts
->config
;
1253 ts12
.write_actions
= ts
->write_actions
;
1254 ts12
.apply_actions
= ts
->apply_actions
;
1255 ofp_print_one_ofpst_table_reply(string
, OFP11_VERSION
, ts
->name
, &ts12
);
1260 ofp_print_ofpst_table_reply10(struct ds
*string
, const struct ofp_header
*oh
,
1263 struct ofp10_table_stats
*ts
;
1267 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
1268 ofpraw_pull_assert(&b
);
1270 n
= b
.size
/ sizeof *ts
;
1271 ds_put_format(string
, " %zu tables\n", n
);
1272 if (verbosity
< 1) {
1277 struct ofp12_table_stats ts12
;
1279 ts
= ofpbuf_try_pull(&b
, sizeof *ts
);
1284 ts12
.table_id
= ts
->table_id
;
1285 ts12
.wildcards
= htonll(ntohl(ts
->wildcards
));
1286 ts12
.max_entries
= ts
->max_entries
;
1287 ts12
.active_count
= ts
->active_count
;
1288 ts12
.lookup_count
= get_32aligned_be64(&ts
->lookup_count
);
1289 ts12
.matched_count
= get_32aligned_be64(&ts
->matched_count
);
1290 ofp_print_one_ofpst_table_reply(string
, OFP10_VERSION
, ts
->name
, &ts12
);
1295 ofp_print_ofpst_table_reply(struct ds
*string
, const struct ofp_header
*oh
,
1298 switch ((enum ofp_version
)oh
->version
) {
1300 ofp_print_ofpst_table_reply12(string
, oh
, verbosity
);
1304 ofp_print_ofpst_table_reply11(string
, oh
, verbosity
);
1308 ofp_print_ofpst_table_reply10(string
, oh
, verbosity
);
1317 ofp_print_queue_name(struct ds
*string
, uint32_t queue_id
)
1319 if (queue_id
== OFPQ_ALL
) {
1320 ds_put_cstr(string
, "ALL");
1322 ds_put_format(string
, "%"PRIu32
, queue_id
);
1327 ofp_print_ofpst_queue_request(struct ds
*string
, const struct ofp_header
*oh
)
1329 struct ofputil_queue_stats_request oqsr
;
1332 error
= ofputil_decode_queue_stats_request(oh
, &oqsr
);
1334 ds_put_format(string
, "***decode error: %s***\n", ofperr_get_name(error
));
1338 ds_put_cstr(string
, "port=");
1339 ofputil_format_port(oqsr
.port_no
, string
);
1341 ds_put_cstr(string
, " queue=");
1342 ofp_print_queue_name(string
, oqsr
.queue_id
);
1346 ofp_print_ofpst_queue_reply(struct ds
*string
, const struct ofp_header
*oh
,
1351 ds_put_format(string
, " %zu queues\n", ofputil_count_queue_stats(oh
));
1352 if (verbosity
< 1) {
1356 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
1358 struct ofputil_queue_stats qs
;
1361 retval
= ofputil_decode_queue_stats(&qs
, &b
);
1363 if (retval
!= EOF
) {
1364 ds_put_cstr(string
, " ***parse error***");
1369 ds_put_cstr(string
, " port ");
1370 ofputil_format_port(qs
.port_no
, string
);
1371 ds_put_cstr(string
, " queue ");
1372 ofp_print_queue_name(string
, qs
.queue_id
);
1373 ds_put_cstr(string
, ": ");
1375 print_port_stat(string
, "bytes=", qs
.stats
.tx_bytes
, 1);
1376 print_port_stat(string
, "pkts=", qs
.stats
.tx_packets
, 1);
1377 print_port_stat(string
, "errors=", qs
.stats
.tx_errors
, 0);
1382 ofp_print_ofpst_port_desc_reply(struct ds
*string
,
1383 const struct ofp_header
*oh
)
1387 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
1388 ofpraw_pull_assert(&b
);
1389 ds_put_char(string
, '\n');
1390 ofp_print_phy_ports(string
, oh
->version
, &b
);
1394 ofp_print_stats_request(struct ds
*string
, const struct ofp_header
*oh
)
1396 uint16_t flags
= ofpmp_flags(oh
);
1399 ds_put_format(string
, " ***unknown flags 0x%04"PRIx16
"***", flags
);
1404 ofp_print_stats_reply(struct ds
*string
, const struct ofp_header
*oh
)
1406 uint16_t flags
= ofpmp_flags(oh
);
1409 ds_put_cstr(string
, " flags=");
1410 if (flags
& OFPSF_REPLY_MORE
) {
1411 ds_put_cstr(string
, "[more]");
1412 flags
&= ~OFPSF_REPLY_MORE
;
1415 ds_put_format(string
, "[***unknown flags 0x%04"PRIx16
"***]",
1422 ofp_print_echo(struct ds
*string
, const struct ofp_header
*oh
, int verbosity
)
1424 size_t len
= ntohs(oh
->length
);
1426 ds_put_format(string
, " %zu bytes of payload\n", len
- sizeof *oh
);
1427 if (verbosity
> 1) {
1428 ds_put_hex_dump(string
, oh
+ 1, len
- sizeof *oh
, 0, true);
1433 ofp_print_nxt_role_message(struct ds
*string
,
1434 const struct nx_role_request
*nrr
)
1436 unsigned int role
= ntohl(nrr
->role
);
1438 ds_put_cstr(string
, " role=");
1439 if (role
== NX_ROLE_OTHER
) {
1440 ds_put_cstr(string
, "other");
1441 } else if (role
== NX_ROLE_MASTER
) {
1442 ds_put_cstr(string
, "master");
1443 } else if (role
== NX_ROLE_SLAVE
) {
1444 ds_put_cstr(string
, "slave");
1446 ds_put_format(string
, "%u", role
);
1451 ofp_print_nxt_flow_mod_table_id(struct ds
*string
,
1452 const struct nx_flow_mod_table_id
*nfmti
)
1454 ds_put_format(string
, " %s", nfmti
->set
? "enable" : "disable");
1458 ofp_print_nxt_set_flow_format(struct ds
*string
,
1459 const struct nx_set_flow_format
*nsff
)
1461 uint32_t format
= ntohl(nsff
->format
);
1463 ds_put_cstr(string
, " format=");
1464 if (ofputil_nx_flow_format_is_valid(format
)) {
1465 ds_put_cstr(string
, ofputil_nx_flow_format_to_string(format
));
1467 ds_put_format(string
, "%"PRIu32
, format
);
1472 ofp_print_nxt_set_packet_in_format(struct ds
*string
,
1473 const struct nx_set_packet_in_format
*nspf
)
1475 uint32_t format
= ntohl(nspf
->format
);
1477 ds_put_cstr(string
, " format=");
1478 if (ofputil_packet_in_format_is_valid(format
)) {
1479 ds_put_cstr(string
, ofputil_packet_in_format_to_string(format
));
1481 ds_put_format(string
, "%"PRIu32
, format
);
1486 ofp_port_reason_to_string(enum ofp_port_reason reason
)
1501 sprintf(s
, "%d", (int) reason
);
1507 ofp_print_nxt_set_async_config(struct ds
*string
,
1508 const struct nx_async_config
*nac
)
1512 for (i
= 0; i
< 2; i
++) {
1515 ds_put_format(string
, "\n %s:\n", i
== 0 ? "master" : "slave");
1517 ds_put_cstr(string
, " PACKET_IN:");
1518 for (j
= 0; j
< 32; j
++) {
1519 if (nac
->packet_in_mask
[i
] & htonl(1u << j
)) {
1520 ds_put_format(string
, " %s",
1521 ofputil_packet_in_reason_to_string(j
));
1524 if (!nac
->packet_in_mask
[i
]) {
1525 ds_put_cstr(string
, " (off)");
1527 ds_put_char(string
, '\n');
1529 ds_put_cstr(string
, " PORT_STATUS:");
1530 for (j
= 0; j
< 32; j
++) {
1531 if (nac
->port_status_mask
[i
] & htonl(1u << j
)) {
1532 ds_put_format(string
, " %s", ofp_port_reason_to_string(j
));
1535 if (!nac
->port_status_mask
[i
]) {
1536 ds_put_cstr(string
, " (off)");
1538 ds_put_char(string
, '\n');
1540 ds_put_cstr(string
, " FLOW_REMOVED:");
1541 for (j
= 0; j
< 32; j
++) {
1542 if (nac
->flow_removed_mask
[i
] & htonl(1u << j
)) {
1543 ds_put_format(string
, " %s",
1544 ofp_flow_removed_reason_to_string(j
));
1547 if (!nac
->flow_removed_mask
[i
]) {
1548 ds_put_cstr(string
, " (off)");
1550 ds_put_char(string
, '\n');
1555 ofp_print_nxt_set_controller_id(struct ds
*string
,
1556 const struct nx_controller_id
*nci
)
1558 ds_put_format(string
, " id=%"PRIu16
, ntohs(nci
->controller_id
));
1562 ofp_print_nxt_flow_monitor_cancel(struct ds
*string
,
1563 const struct ofp_header
*oh
)
1565 ds_put_format(string
, " id=%"PRIu32
,
1566 ofputil_decode_flow_monitor_cancel(oh
));
1570 nx_flow_monitor_flags_to_name(uint32_t bit
)
1572 enum nx_flow_monitor_flags fmf
= bit
;
1575 case NXFMF_INITIAL
: return "initial";
1576 case NXFMF_ADD
: return "add";
1577 case NXFMF_DELETE
: return "delete";
1578 case NXFMF_MODIFY
: return "modify";
1579 case NXFMF_ACTIONS
: return "actions";
1580 case NXFMF_OWN
: return "own";
1587 ofp_print_nxst_flow_monitor_request(struct ds
*string
,
1588 const struct ofp_header
*oh
)
1592 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
1594 struct ofputil_flow_monitor_request request
;
1597 retval
= ofputil_decode_flow_monitor_request(&request
, &b
);
1599 if (retval
!= EOF
) {
1600 ofp_print_error(string
, retval
);
1605 ds_put_format(string
, "\n id=%"PRIu32
" flags=", request
.id
);
1606 ofp_print_bit_names(string
, request
.flags
,
1607 nx_flow_monitor_flags_to_name
, ',');
1609 if (request
.out_port
!= OFPP_NONE
) {
1610 ds_put_cstr(string
, " out_port=");
1611 ofputil_format_port(request
.out_port
, string
);
1614 if (request
.table_id
!= 0xff) {
1615 ds_put_format(string
, " table=%"PRIu8
, request
.table_id
);
1618 ds_put_char(string
, ' ');
1619 match_format(&request
.match
, string
, OFP_DEFAULT_PRIORITY
);
1620 ds_chomp(string
, ' ');
1625 ofp_print_nxst_flow_monitor_reply(struct ds
*string
,
1626 const struct ofp_header
*oh
)
1628 uint64_t ofpacts_stub
[1024 / 8];
1629 struct ofpbuf ofpacts
;
1632 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
1633 ofpbuf_use_stub(&ofpacts
, ofpacts_stub
, sizeof ofpacts_stub
);
1635 struct ofputil_flow_update update
;
1639 update
.match
= &match
;
1640 retval
= ofputil_decode_flow_update(&update
, &b
, &ofpacts
);
1642 if (retval
!= EOF
) {
1643 ofp_print_error(string
, retval
);
1645 ofpbuf_uninit(&ofpacts
);
1649 ds_put_cstr(string
, "\n event=");
1650 switch (update
.event
) {
1652 ds_put_cstr(string
, "ADDED");
1656 ds_put_format(string
, "DELETED reason=%s",
1657 ofp_flow_removed_reason_to_string(update
.reason
));
1660 case NXFME_MODIFIED
:
1661 ds_put_cstr(string
, "MODIFIED");
1665 ds_put_format(string
, "ABBREV xid=0x%"PRIx32
, ntohl(update
.xid
));
1669 ds_put_format(string
, " table=%"PRIu8
, update
.table_id
);
1670 if (update
.idle_timeout
!= OFP_FLOW_PERMANENT
) {
1671 ds_put_format(string
, " idle_timeout=%"PRIu16
,
1672 update
.idle_timeout
);
1674 if (update
.hard_timeout
!= OFP_FLOW_PERMANENT
) {
1675 ds_put_format(string
, " hard_timeout=%"PRIu16
,
1676 update
.hard_timeout
);
1678 ds_put_format(string
, " cookie=%#"PRIx64
, ntohll(update
.cookie
));
1680 ds_put_char(string
, ' ');
1681 match_format(update
.match
, string
, OFP_DEFAULT_PRIORITY
);
1683 if (update
.ofpacts_len
) {
1684 if (string
->string
[string
->length
- 1] != ' ') {
1685 ds_put_char(string
, ' ');
1687 ofpacts_format(update
.ofpacts
, update
.ofpacts_len
, string
);
1693 ofp_print_version(const struct ofp_header
*oh
,
1696 switch (oh
->version
) {
1700 ds_put_cstr(string
, " (OF1.1)");
1703 ds_put_cstr(string
, " (OF1.2)");
1706 ds_put_format(string
, " (OF 0x%02"PRIx8
")", oh
->version
);
1709 ds_put_format(string
, " (xid=0x%"PRIx32
"):", ntohl(oh
->xid
));
1713 ofp_header_to_string__(const struct ofp_header
*oh
, enum ofpraw raw
,
1716 ds_put_cstr(string
, ofpraw_get_name(raw
));
1717 ofp_print_version(oh
, string
);
1721 ofp_to_string__(const struct ofp_header
*oh
, enum ofpraw raw
,
1722 struct ds
*string
, int verbosity
)
1724 const void *msg
= oh
;
1726 ofp_header_to_string__(oh
, raw
, string
);
1727 switch (ofptype_from_ofpraw(raw
)) {
1729 ds_put_char(string
, '\n');
1730 ds_put_hex_dump(string
, oh
+ 1, ntohs(oh
->length
) - sizeof *oh
,
1735 ofp_print_error_msg(string
, oh
);
1738 case OFPTYPE_ECHO_REQUEST
:
1739 case OFPTYPE_ECHO_REPLY
:
1740 ofp_print_echo(string
, oh
, verbosity
);
1743 case OFPTYPE_FEATURES_REQUEST
:
1746 case OFPTYPE_FEATURES_REPLY
:
1747 ofp_print_switch_features(string
, oh
);
1750 case OFPTYPE_GET_CONFIG_REQUEST
:
1753 case OFPTYPE_GET_CONFIG_REPLY
:
1754 case OFPTYPE_SET_CONFIG
:
1755 ofp_print_switch_config(string
, ofpmsg_body(oh
));
1758 case OFPTYPE_PACKET_IN
:
1759 ofp_print_packet_in(string
, oh
, verbosity
);
1762 case OFPTYPE_FLOW_REMOVED
:
1763 ofp_print_flow_removed(string
, oh
);
1766 case OFPTYPE_PORT_STATUS
:
1767 ofp_print_port_status(string
, oh
);
1770 case OFPTYPE_PACKET_OUT
:
1771 ofp_print_packet_out(string
, oh
, verbosity
);
1774 case OFPTYPE_FLOW_MOD
:
1775 ofp_print_flow_mod(string
, oh
, verbosity
);
1778 case OFPTYPE_PORT_MOD
:
1779 ofp_print_port_mod(string
, oh
);
1782 case OFPTYPE_BARRIER_REQUEST
:
1783 case OFPTYPE_BARRIER_REPLY
:
1786 case OFPTYPE_DESC_STATS_REQUEST
:
1787 case OFPTYPE_PORT_DESC_STATS_REQUEST
:
1788 ofp_print_stats_request(string
, oh
);
1791 case OFPTYPE_FLOW_STATS_REQUEST
:
1792 case OFPTYPE_AGGREGATE_STATS_REQUEST
:
1793 ofp_print_stats_request(string
, oh
);
1794 ofp_print_flow_stats_request(string
, oh
);
1797 case OFPTYPE_TABLE_STATS_REQUEST
:
1798 ofp_print_stats_request(string
, oh
);
1801 case OFPTYPE_PORT_STATS_REQUEST
:
1802 ofp_print_stats_request(string
, oh
);
1803 ofp_print_ofpst_port_request(string
, oh
);
1806 case OFPTYPE_QUEUE_STATS_REQUEST
:
1807 ofp_print_stats_request(string
, oh
);
1808 ofp_print_ofpst_queue_request(string
, oh
);
1811 case OFPTYPE_DESC_STATS_REPLY
:
1812 ofp_print_stats_reply(string
, oh
);
1813 ofp_print_ofpst_desc_reply(string
, oh
);
1816 case OFPTYPE_FLOW_STATS_REPLY
:
1817 ofp_print_stats_reply(string
, oh
);
1818 ofp_print_flow_stats_reply(string
, oh
);
1821 case OFPTYPE_QUEUE_STATS_REPLY
:
1822 ofp_print_stats_reply(string
, oh
);
1823 ofp_print_ofpst_queue_reply(string
, oh
, verbosity
);
1826 case OFPTYPE_PORT_STATS_REPLY
:
1827 ofp_print_stats_reply(string
, oh
);
1828 ofp_print_ofpst_port_reply(string
, oh
, verbosity
);
1831 case OFPTYPE_TABLE_STATS_REPLY
:
1832 ofp_print_stats_reply(string
, oh
);
1833 ofp_print_ofpst_table_reply(string
, oh
, verbosity
);
1836 case OFPTYPE_AGGREGATE_STATS_REPLY
:
1837 ofp_print_stats_reply(string
, oh
);
1838 ofp_print_aggregate_stats_reply(string
, oh
);
1841 case OFPTYPE_PORT_DESC_STATS_REPLY
:
1842 ofp_print_stats_reply(string
, oh
);
1843 ofp_print_ofpst_port_desc_reply(string
, oh
);
1846 case OFPTYPE_ROLE_REQUEST
:
1847 case OFPTYPE_ROLE_REPLY
:
1848 ofp_print_nxt_role_message(string
, ofpmsg_body(oh
));
1851 case OFPTYPE_FLOW_MOD_TABLE_ID
:
1852 ofp_print_nxt_flow_mod_table_id(string
, ofpmsg_body(oh
));
1855 case OFPTYPE_SET_FLOW_FORMAT
:
1856 ofp_print_nxt_set_flow_format(string
, ofpmsg_body(oh
));
1859 case OFPTYPE_SET_PACKET_IN_FORMAT
:
1860 ofp_print_nxt_set_packet_in_format(string
, ofpmsg_body(oh
));
1863 case OFPTYPE_FLOW_AGE
:
1866 case OFPTYPE_SET_CONTROLLER_ID
:
1867 ofp_print_nxt_set_controller_id(string
, ofpmsg_body(oh
));
1870 case OFPTYPE_SET_ASYNC_CONFIG
:
1871 ofp_print_nxt_set_async_config(string
, ofpmsg_body(oh
));
1874 case OFPTYPE_FLOW_MONITOR_CANCEL
:
1875 ofp_print_nxt_flow_monitor_cancel(string
, msg
);
1878 case OFPTYPE_FLOW_MONITOR_PAUSED
:
1879 case OFPTYPE_FLOW_MONITOR_RESUMED
:
1882 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST
:
1883 ofp_print_nxst_flow_monitor_request(string
, msg
);
1886 case OFPTYPE_FLOW_MONITOR_STATS_REPLY
:
1887 ofp_print_nxst_flow_monitor_reply(string
, msg
);
1892 /* Composes and returns a string representing the OpenFlow packet of 'len'
1893 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1894 * verbosity and higher numbers increase verbosity. The caller is responsible
1895 * for freeing the string. */
1897 ofp_to_string(const void *oh_
, size_t len
, int verbosity
)
1899 struct ds string
= DS_EMPTY_INITIALIZER
;
1900 const struct ofp_header
*oh
= oh_
;
1903 ds_put_cstr(&string
, "OpenFlow message is empty\n");
1904 } else if (len
< sizeof(struct ofp_header
)) {
1905 ds_put_format(&string
, "OpenFlow packet too short (only %zu bytes):\n",
1907 } else if (ntohs(oh
->length
) > len
) {
1911 error
= ofpraw_decode_partial(&raw
, oh
, len
);
1913 ofp_header_to_string__(oh
, raw
, &string
);
1914 ds_put_char(&string
, '\n');
1917 ds_put_format(&string
,
1918 "(***truncated to %zu bytes from %"PRIu16
"***)\n",
1919 len
, ntohs(oh
->length
));
1920 } else if (ntohs(oh
->length
) < len
) {
1921 ds_put_format(&string
,
1922 "(***only uses %"PRIu16
" bytes out of %zu***)\n",
1923 ntohs(oh
->length
), len
);
1928 error
= ofpraw_decode(&raw
, oh
);
1930 ofp_to_string__(oh
, raw
, &string
, verbosity
);
1931 if (verbosity
>= 5) {
1932 if (ds_last(&string
) != '\n') {
1933 ds_put_char(&string
, '\n');
1935 ds_put_hex_dump(&string
, oh
, len
, 0, true);
1937 if (ds_last(&string
) != '\n') {
1938 ds_put_char(&string
, '\n');
1940 return ds_steal_cstr(&string
);
1943 ofp_print_error(&string
, error
);
1945 ds_put_hex_dump(&string
, oh
, len
, 0, true);
1946 return ds_steal_cstr(&string
);
1950 print_and_free(FILE *stream
, char *string
)
1952 fputs(string
, stream
);
1956 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1957 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1958 * numbers increase verbosity. */
1960 ofp_print(FILE *stream
, const void *oh
, size_t len
, int verbosity
)
1962 print_and_free(stream
, ofp_to_string(oh
, len
, verbosity
));
1965 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1966 * 'data' to 'stream'. */
1968 ofp_print_packet(FILE *stream
, const void *data
, size_t len
)
1970 print_and_free(stream
, ofp_packet_to_string(data
, len
));