2 * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 #include "ofp-print.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
30 #include "byte-order.h"
32 #include "dynamic-string.h"
35 #include "multipath.h"
39 #include "openflow/openflow.h"
40 #include "openflow/nicira-ext.h"
43 #include "type-props.h"
44 #include "unaligned.h"
47 static void ofp_print_queue_name(struct ds
*string
, uint32_t port
);
48 static void ofp_print_error(struct ds
*, int error
);
51 /* Returns a string that represents the contents of the Ethernet frame in the
52 * 'len' bytes starting at 'data'. The caller must free the returned string.*/
54 ofp_packet_to_string(const void *data
, size_t len
)
56 struct ds ds
= DS_EMPTY_INITIALIZER
;
60 ofpbuf_use_const(&buf
, data
, len
);
61 flow_extract(&buf
, 0, 0, 0, &flow
);
62 flow_format(&ds
, &flow
);
63 ds_put_char(&ds
, '\n');
69 ofp_print_packet_in(struct ds
*string
, const struct ofp_packet_in
*op
,
72 size_t len
= ntohs(op
->header
.length
);
75 ds_put_format(string
, " total_len=%"PRIu16
" in_port=",
76 ntohs(op
->total_len
));
77 ofputil_format_port(ntohs(op
->in_port
), string
);
79 if (op
->reason
== OFPR_ACTION
)
80 ds_put_cstr(string
, " (via action)");
81 else if (op
->reason
!= OFPR_NO_MATCH
)
82 ds_put_format(string
, " (***reason %"PRIu8
"***)", op
->reason
);
84 data_len
= len
- offsetof(struct ofp_packet_in
, data
);
85 ds_put_format(string
, " data_len=%zu", data_len
);
86 if (op
->buffer_id
== htonl(UINT32_MAX
)) {
87 ds_put_format(string
, " (unbuffered)");
88 if (ntohs(op
->total_len
) != data_len
)
89 ds_put_format(string
, " (***total_len != data_len***)");
91 ds_put_format(string
, " buffer=0x%08"PRIx32
, ntohl(op
->buffer_id
));
92 if (ntohs(op
->total_len
) < data_len
)
93 ds_put_format(string
, " (***total_len < data_len***)");
95 ds_put_char(string
, '\n');
98 char *packet
= ofp_packet_to_string(op
->data
, data_len
);
99 ds_put_cstr(string
, packet
);
105 print_note(struct ds
*string
, const struct nx_action_note
*nan
)
110 ds_put_cstr(string
, "note:");
111 len
= ntohs(nan
->len
) - offsetof(struct nx_action_note
, note
);
112 for (i
= 0; i
< len
; i
++) {
114 ds_put_char(string
, '.');
116 ds_put_format(string
, "%02"PRIx8
, nan
->note
[i
]);
121 ofp_print_action(struct ds
*s
, const union ofp_action
*a
,
122 enum ofputil_action_code code
)
124 const struct ofp_action_enqueue
*oae
;
125 const struct ofp_action_dl_addr
*oada
;
126 const struct nx_action_set_tunnel64
*nast64
;
127 const struct nx_action_set_tunnel
*nast
;
128 const struct nx_action_set_queue
*nasq
;
129 const struct nx_action_resubmit
*nar
;
130 const struct nx_action_reg_move
*move
;
131 const struct nx_action_reg_load
*load
;
132 const struct nx_action_multipath
*nam
;
133 const struct nx_action_autopath
*naa
;
134 const struct nx_action_output_reg
*naor
;
138 case OFPUTIL_OFPAT_OUTPUT
:
139 port
= ntohs(a
->output
.port
);
140 if (port
< OFPP_MAX
) {
141 ds_put_format(s
, "output:%"PRIu16
, port
);
143 ofputil_format_port(port
, s
);
144 if (port
== OFPP_CONTROLLER
) {
145 if (a
->output
.max_len
!= htons(0)) {
146 ds_put_format(s
, ":%"PRIu16
, ntohs(a
->output
.max_len
));
148 ds_put_cstr(s
, ":all");
154 case OFPUTIL_OFPAT_ENQUEUE
:
155 oae
= (const struct ofp_action_enqueue
*) a
;
156 ds_put_format(s
, "enqueue:");
157 ofputil_format_port(ntohs(oae
->port
), s
);
158 ds_put_format(s
, "q%"PRIu32
, ntohl(oae
->queue_id
));
161 case OFPUTIL_OFPAT_SET_VLAN_VID
:
162 ds_put_format(s
, "mod_vlan_vid:%"PRIu16
,
163 ntohs(a
->vlan_vid
.vlan_vid
));
166 case OFPUTIL_OFPAT_SET_VLAN_PCP
:
167 ds_put_format(s
, "mod_vlan_pcp:%"PRIu8
, a
->vlan_pcp
.vlan_pcp
);
170 case OFPUTIL_OFPAT_STRIP_VLAN
:
171 ds_put_cstr(s
, "strip_vlan");
174 case OFPUTIL_OFPAT_SET_DL_SRC
:
175 oada
= (const struct ofp_action_dl_addr
*) a
;
176 ds_put_format(s
, "mod_dl_src:"ETH_ADDR_FMT
,
177 ETH_ADDR_ARGS(oada
->dl_addr
));
180 case OFPUTIL_OFPAT_SET_DL_DST
:
181 oada
= (const struct ofp_action_dl_addr
*) a
;
182 ds_put_format(s
, "mod_dl_dst:"ETH_ADDR_FMT
,
183 ETH_ADDR_ARGS(oada
->dl_addr
));
186 case OFPUTIL_OFPAT_SET_NW_SRC
:
187 ds_put_format(s
, "mod_nw_src:"IP_FMT
, IP_ARGS(&a
->nw_addr
.nw_addr
));
190 case OFPUTIL_OFPAT_SET_NW_DST
:
191 ds_put_format(s
, "mod_nw_dst:"IP_FMT
, IP_ARGS(&a
->nw_addr
.nw_addr
));
194 case OFPUTIL_OFPAT_SET_NW_TOS
:
195 ds_put_format(s
, "mod_nw_tos:%d", a
->nw_tos
.nw_tos
);
198 case OFPUTIL_OFPAT_SET_TP_SRC
:
199 ds_put_format(s
, "mod_tp_src:%d", ntohs(a
->tp_port
.tp_port
));
202 case OFPUTIL_OFPAT_SET_TP_DST
:
203 ds_put_format(s
, "mod_tp_dst:%d", ntohs(a
->tp_port
.tp_port
));
206 case OFPUTIL_NXAST_RESUBMIT
:
207 nar
= (struct nx_action_resubmit
*)a
;
208 ds_put_format(s
, "resubmit:");
209 ofputil_format_port(ntohs(nar
->in_port
), s
);
212 case OFPUTIL_NXAST_RESUBMIT_TABLE
:
213 nar
= (struct nx_action_resubmit
*)a
;
214 ds_put_format(s
, "resubmit(");
215 if (nar
->in_port
!= htons(OFPP_IN_PORT
)) {
216 ofputil_format_port(ntohs(nar
->in_port
), s
);
219 if (nar
->table
!= 255) {
220 ds_put_format(s
, "%"PRIu8
, nar
->table
);
225 case OFPUTIL_NXAST_SET_TUNNEL
:
226 nast
= (struct nx_action_set_tunnel
*)a
;
227 ds_put_format(s
, "set_tunnel:%#"PRIx32
, ntohl(nast
->tun_id
));
230 case OFPUTIL_NXAST_SET_QUEUE
:
231 nasq
= (struct nx_action_set_queue
*)a
;
232 ds_put_format(s
, "set_queue:%u", ntohl(nasq
->queue_id
));
235 case OFPUTIL_NXAST_POP_QUEUE
:
236 ds_put_cstr(s
, "pop_queue");
239 case OFPUTIL_NXAST_NOTE
:
240 print_note(s
, (const struct nx_action_note
*) a
);
243 case OFPUTIL_NXAST_REG_MOVE
:
244 move
= (const struct nx_action_reg_move
*) a
;
245 nxm_format_reg_move(move
, s
);
248 case OFPUTIL_NXAST_REG_LOAD
:
249 load
= (const struct nx_action_reg_load
*) a
;
250 nxm_format_reg_load(load
, s
);
253 case OFPUTIL_NXAST_SET_TUNNEL64
:
254 nast64
= (const struct nx_action_set_tunnel64
*) a
;
255 ds_put_format(s
, "set_tunnel64:%#"PRIx64
,
256 ntohll(nast64
->tun_id
));
259 case OFPUTIL_NXAST_MULTIPATH
:
260 nam
= (const struct nx_action_multipath
*) a
;
261 multipath_format(nam
, s
);
264 case OFPUTIL_NXAST_AUTOPATH
:
265 naa
= (const struct nx_action_autopath
*)a
;
266 ds_put_format(s
, "autopath(%u,", ntohl(naa
->id
));
267 nxm_format_field_bits(s
, ntohl(naa
->dst
),
268 nxm_decode_ofs(naa
->ofs_nbits
),
269 nxm_decode_n_bits(naa
->ofs_nbits
));
273 case OFPUTIL_NXAST_BUNDLE
:
274 case OFPUTIL_NXAST_BUNDLE_LOAD
:
275 bundle_format((const struct nx_action_bundle
*) a
, s
);
278 case OFPUTIL_NXAST_OUTPUT_REG
:
279 naor
= (const struct nx_action_output_reg
*) a
;
280 ds_put_cstr(s
, "output:");
281 nxm_format_field_bits(s
, ntohl(naor
->src
),
282 nxm_decode_ofs(naor
->ofs_nbits
),
283 nxm_decode_n_bits(naor
->ofs_nbits
));
286 case OFPUTIL_NXAST_LEARN
:
287 learn_format((const struct nx_action_learn
*) a
, s
);
290 case OFPUTIL_NXAST_EXIT
:
291 ds_put_cstr(s
, "exit");
300 ofp_print_actions(struct ds
*string
, const union ofp_action
*actions
,
303 const union ofp_action
*a
;
306 ds_put_cstr(string
, "actions=");
308 ds_put_cstr(string
, "drop");
311 OFPUTIL_ACTION_FOR_EACH (a
, left
, actions
, n_actions
) {
312 int code
= ofputil_decode_action(a
);
315 ds_put_cstr(string
, ",");
317 ofp_print_action(string
, a
, code
);
319 ofp_print_error(string
, -code
);
323 ds_put_format(string
, " ***%zu leftover bytes following actions",
329 ofp_print_packet_out(struct ds
*string
, const struct ofp_packet_out
*opo
,
332 size_t len
= ntohs(opo
->header
.length
);
333 size_t actions_len
= ntohs(opo
->actions_len
);
335 ds_put_cstr(string
, " in_port=");
336 ofputil_format_port(ntohs(opo
->in_port
), string
);
338 ds_put_format(string
, " actions_len=%zu ", actions_len
);
339 if (actions_len
> (ntohs(opo
->header
.length
) - sizeof *opo
)) {
340 ds_put_format(string
, "***packet too short for action length***\n");
343 if (actions_len
% sizeof(union ofp_action
)) {
344 ds_put_format(string
, "***action length not a multiple of %zu***\n",
345 sizeof(union ofp_action
));
347 ofp_print_actions(string
, (const union ofp_action
*) opo
->actions
,
348 actions_len
/ sizeof(union ofp_action
));
350 if (ntohl(opo
->buffer_id
) == UINT32_MAX
) {
351 int data_len
= len
- sizeof *opo
- actions_len
;
352 ds_put_format(string
, " data_len=%d", data_len
);
353 if (verbosity
> 0 && len
> sizeof *opo
) {
354 char *packet
= ofp_packet_to_string(
355 (uint8_t *) opo
->actions
+ actions_len
, data_len
);
356 ds_put_char(string
, '\n');
357 ds_put_cstr(string
, packet
);
361 ds_put_format(string
, " buffer=0x%08"PRIx32
, ntohl(opo
->buffer_id
));
363 ds_put_char(string
, '\n');
366 /* qsort comparison function. */
368 compare_ports(const void *a_
, const void *b_
)
370 const struct ofp_phy_port
*a
= a_
;
371 const struct ofp_phy_port
*b
= b_
;
372 uint16_t ap
= ntohs(a
->port_no
);
373 uint16_t bp
= ntohs(b
->port_no
);
375 return ap
< bp
? -1 : ap
> bp
;
384 ofp_print_bit_names(struct ds
*string
, uint32_t bits
,
385 const struct bit_name bit_names
[])
390 ds_put_cstr(string
, "0");
394 for (; bits
&& bit_names
->name
; bit_names
++) {
395 if (bits
& bit_names
->bit
) {
397 ds_put_char(string
, ' ');
399 ds_put_cstr(string
, bit_names
->name
);
400 bits
&= ~bit_names
->bit
;
406 ds_put_char(string
, ' ');
408 ds_put_format(string
, "0x%"PRIx32
, bits
);
413 ofp_print_port_features(struct ds
*string
, uint32_t features
)
415 static const struct bit_name feature_bits
[] = {
416 { OFPPF_10MB_HD
, "10MB-HD" },
417 { OFPPF_10MB_FD
, "10MB-FD" },
418 { OFPPF_100MB_HD
, "100MB-HD" },
419 { OFPPF_100MB_FD
, "100MB-FD" },
420 { OFPPF_1GB_HD
, "1GB-HD" },
421 { OFPPF_1GB_FD
, "1GB-FD" },
422 { OFPPF_10GB_FD
, "10GB-FD" },
423 { OFPPF_COPPER
, "COPPER" },
424 { OFPPF_FIBER
, "FIBER" },
425 { OFPPF_AUTONEG
, "AUTO_NEG" },
426 { OFPPF_PAUSE
, "AUTO_PAUSE" },
427 { OFPPF_PAUSE_ASYM
, "AUTO_PAUSE_ASYM" },
431 ofp_print_bit_names(string
, features
, feature_bits
);
432 ds_put_char(string
, '\n');
436 ofp_print_port_config(struct ds
*string
, uint32_t config
)
438 static const struct bit_name config_bits
[] = {
439 { OFPPC_PORT_DOWN
, "PORT_DOWN" },
440 { OFPPC_NO_STP
, "NO_STP" },
441 { OFPPC_NO_RECV
, "NO_RECV" },
442 { OFPPC_NO_RECV_STP
, "NO_RECV_STP" },
443 { OFPPC_NO_FLOOD
, "NO_FLOOD" },
444 { OFPPC_NO_FWD
, "NO_FWD" },
445 { OFPPC_NO_PACKET_IN
, "NO_PACKET_IN" },
449 ofp_print_bit_names(string
, config
, config_bits
);
450 ds_put_char(string
, '\n');
454 ofp_print_port_state(struct ds
*string
, uint32_t state
)
456 static const struct bit_name state_bits
[] = {
457 { OFPPS_LINK_DOWN
, "LINK_DOWN" },
462 /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
463 * pattern. We have to special case it.
465 * OVS doesn't support STP, so this field will always be 0 if we are
466 * talking to OVS, so we'd always print STP_LISTEN in that case.
467 * Therefore, we don't print anything at all if the value is STP_LISTEN, to
468 * avoid confusing users. */
469 stp_state
= state
& OFPPS_STP_MASK
;
471 ds_put_cstr(string
, (stp_state
== OFPPS_STP_LEARN
? "STP_LEARN"
472 : stp_state
== OFPPS_STP_FORWARD
? "STP_FORWARD"
474 state
&= ~OFPPS_STP_MASK
;
476 ofp_print_bit_names(string
, state
, state_bits
);
479 ofp_print_bit_names(string
, state
, state_bits
);
481 ds_put_char(string
, '\n');
485 ofp_print_phy_port(struct ds
*string
, const struct ofp_phy_port
*port
)
487 char name
[OFP_MAX_PORT_NAME_LEN
];
490 memcpy(name
, port
->name
, sizeof name
);
491 for (j
= 0; j
< sizeof name
- 1; j
++) {
492 if (!isprint((unsigned char) name
[j
])) {
498 ds_put_char(string
, ' ');
499 ofputil_format_port(ntohs(port
->port_no
), string
);
500 ds_put_format(string
, "(%s): addr:"ETH_ADDR_FMT
"\n",
501 name
, ETH_ADDR_ARGS(port
->hw_addr
));
503 ds_put_cstr(string
, " config: ");
504 ofp_print_port_config(string
, ntohl(port
->config
));
506 ds_put_cstr(string
, " state: ");
507 ofp_print_port_state(string
, ntohl(port
->state
));
510 ds_put_format(string
, " current: ");
511 ofp_print_port_features(string
, ntohl(port
->curr
));
513 if (port
->advertised
) {
514 ds_put_format(string
, " advertised: ");
515 ofp_print_port_features(string
, ntohl(port
->advertised
));
517 if (port
->supported
) {
518 ds_put_format(string
, " supported: ");
519 ofp_print_port_features(string
, ntohl(port
->supported
));
522 ds_put_format(string
, " peer: ");
523 ofp_print_port_features(string
, ntohl(port
->peer
));
528 ofp_print_switch_features(struct ds
*string
,
529 const struct ofp_switch_features
*osf
)
531 size_t len
= ntohs(osf
->header
.length
);
532 struct ofp_phy_port
*port_list
;
536 ds_put_format(string
, " ver:0x%x, dpid:%016"PRIx64
"\n",
537 osf
->header
.version
, ntohll(osf
->datapath_id
));
538 ds_put_format(string
, "n_tables:%d, n_buffers:%d\n", osf
->n_tables
,
539 ntohl(osf
->n_buffers
));
540 ds_put_format(string
, "features: capabilities:%#x, actions:%#x\n",
541 ntohl(osf
->capabilities
), ntohl(osf
->actions
));
543 n_ports
= (len
- sizeof *osf
) / sizeof *osf
->ports
;
545 port_list
= xmemdup(osf
->ports
, len
- sizeof *osf
);
546 qsort(port_list
, n_ports
, sizeof *port_list
, compare_ports
);
547 for (i
= 0; i
< n_ports
; i
++) {
548 ofp_print_phy_port(string
, &port_list
[i
]);
554 ofp_print_switch_config(struct ds
*string
, const struct ofp_switch_config
*osc
)
558 flags
= ntohs(osc
->flags
);
560 ds_put_format(string
, " frags=%s", ofputil_frag_handling_to_string(flags
));
561 flags
&= ~OFPC_FRAG_MASK
;
564 ds_put_format(string
, " ***unknown flags 0x%04"PRIx16
"***", flags
);
567 ds_put_format(string
, " miss_send_len=%"PRIu16
"\n", ntohs(osc
->miss_send_len
));
570 static void print_wild(struct ds
*string
, const char *leader
, int is_wild
,
571 int verbosity
, const char *format
, ...)
572 __attribute__((format(printf
, 5, 6)));
574 static void print_wild(struct ds
*string
, const char *leader
, int is_wild
,
575 int verbosity
, const char *format
, ...)
577 if (is_wild
&& verbosity
< 2) {
580 ds_put_cstr(string
, leader
);
584 va_start(args
, format
);
585 ds_put_format_valist(string
, format
, args
);
588 ds_put_char(string
, '*');
590 ds_put_char(string
, ',');
594 print_ip_netmask(struct ds
*string
, const char *leader
, ovs_be32 ip
,
595 uint32_t wild_bits
, int verbosity
)
597 if (wild_bits
>= 32 && verbosity
< 2) {
600 ds_put_cstr(string
, leader
);
601 if (wild_bits
< 32) {
602 ds_put_format(string
, IP_FMT
, IP_ARGS(&ip
));
604 ds_put_format(string
, "/%d", 32 - wild_bits
);
607 ds_put_char(string
, '*');
609 ds_put_char(string
, ',');
613 ofp_print_match(struct ds
*f
, const struct ofp_match
*om
, int verbosity
)
615 char *s
= ofp_match_to_string(om
, verbosity
);
621 ofp_match_to_string(const struct ofp_match
*om
, int verbosity
)
623 struct ds f
= DS_EMPTY_INITIALIZER
;
624 uint32_t w
= ntohl(om
->wildcards
);
625 bool skip_type
= false;
626 bool skip_proto
= false;
628 if (!(w
& OFPFW_DL_TYPE
)) {
630 if (om
->dl_type
== htons(ETH_TYPE_IP
)) {
631 if (!(w
& OFPFW_NW_PROTO
)) {
633 if (om
->nw_proto
== IPPROTO_ICMP
) {
634 ds_put_cstr(&f
, "icmp,");
635 } else if (om
->nw_proto
== IPPROTO_TCP
) {
636 ds_put_cstr(&f
, "tcp,");
637 } else if (om
->nw_proto
== IPPROTO_UDP
) {
638 ds_put_cstr(&f
, "udp,");
640 ds_put_cstr(&f
, "ip,");
644 ds_put_cstr(&f
, "ip,");
646 } else if (om
->dl_type
== htons(ETH_TYPE_ARP
)) {
647 ds_put_cstr(&f
, "arp,");
652 print_wild(&f
, "in_port=", w
& OFPFW_IN_PORT
, verbosity
,
653 "%d", ntohs(om
->in_port
));
654 print_wild(&f
, "dl_vlan=", w
& OFPFW_DL_VLAN
, verbosity
,
655 "%d", ntohs(om
->dl_vlan
));
656 print_wild(&f
, "dl_vlan_pcp=", w
& OFPFW_DL_VLAN_PCP
, verbosity
,
657 "%d", om
->dl_vlan_pcp
);
658 print_wild(&f
, "dl_src=", w
& OFPFW_DL_SRC
, verbosity
,
659 ETH_ADDR_FMT
, ETH_ADDR_ARGS(om
->dl_src
));
660 print_wild(&f
, "dl_dst=", w
& OFPFW_DL_DST
, verbosity
,
661 ETH_ADDR_FMT
, ETH_ADDR_ARGS(om
->dl_dst
));
663 print_wild(&f
, "dl_type=", w
& OFPFW_DL_TYPE
, verbosity
,
664 "0x%04x", ntohs(om
->dl_type
));
666 print_ip_netmask(&f
, "nw_src=", om
->nw_src
,
667 (w
& OFPFW_NW_SRC_MASK
) >> OFPFW_NW_SRC_SHIFT
, verbosity
);
668 print_ip_netmask(&f
, "nw_dst=", om
->nw_dst
,
669 (w
& OFPFW_NW_DST_MASK
) >> OFPFW_NW_DST_SHIFT
, verbosity
);
671 if (om
->dl_type
== htons(ETH_TYPE_ARP
)) {
672 print_wild(&f
, "arp_op=", w
& OFPFW_NW_PROTO
, verbosity
,
675 print_wild(&f
, "nw_proto=", w
& OFPFW_NW_PROTO
, verbosity
,
679 print_wild(&f
, "nw_tos=", w
& OFPFW_NW_TOS
, verbosity
,
681 if (om
->nw_proto
== IPPROTO_ICMP
) {
682 print_wild(&f
, "icmp_type=", w
& OFPFW_ICMP_TYPE
, verbosity
,
683 "%d", ntohs(om
->tp_src
));
684 print_wild(&f
, "icmp_code=", w
& OFPFW_ICMP_CODE
, verbosity
,
685 "%d", ntohs(om
->tp_dst
));
687 print_wild(&f
, "tp_src=", w
& OFPFW_TP_SRC
, verbosity
,
688 "%d", ntohs(om
->tp_src
));
689 print_wild(&f
, "tp_dst=", w
& OFPFW_TP_DST
, verbosity
,
690 "%d", ntohs(om
->tp_dst
));
692 if (ds_last(&f
) == ',') {
699 ofp_print_flow_mod(struct ds
*s
, const struct ofp_header
*oh
,
700 enum ofputil_msg_code code
, int verbosity
)
702 struct ofputil_flow_mod fm
;
706 error
= ofputil_decode_flow_mod(&fm
, oh
, true);
708 ofp_print_error(s
, error
);
713 switch (fm
.command
) {
715 ds_put_cstr(s
, "ADD");
718 ds_put_cstr(s
, "MOD");
720 case OFPFC_MODIFY_STRICT
:
721 ds_put_cstr(s
, "MOD_STRICT");
724 ds_put_cstr(s
, "DEL");
726 case OFPFC_DELETE_STRICT
:
727 ds_put_cstr(s
, "DEL_STRICT");
730 ds_put_format(s
, "cmd:%d", fm
.command
);
732 if (fm
.table_id
!= 0) {
733 ds_put_format(s
, " table:%d", fm
.table_id
);
737 if (verbosity
>= 3 && code
== OFPUTIL_OFPT_FLOW_MOD
) {
738 const struct ofp_flow_mod
*ofm
= (const struct ofp_flow_mod
*) oh
;
739 ofp_print_match(s
, &ofm
->match
, verbosity
);
741 /* ofp_print_match() doesn't print priority. */
742 need_priority
= true;
743 } else if (verbosity
>= 3 && code
== OFPUTIL_NXT_FLOW_MOD
) {
744 const struct nx_flow_mod
*nfm
= (const struct nx_flow_mod
*) oh
;
745 const void *nxm
= nfm
+ 1;
748 nxm_s
= nx_match_to_string(nxm
, ntohs(nfm
->match_len
));
749 ds_put_cstr(s
, nxm_s
);
752 /* nx_match_to_string() doesn't print priority. */
753 need_priority
= true;
755 cls_rule_format(&fm
.cr
, s
);
757 /* cls_rule_format() does print priority. */
758 need_priority
= false;
761 if (ds_last(s
) != ' ') {
764 if (fm
.cookie
!= htonll(0)) {
765 ds_put_format(s
, "cookie:0x%"PRIx64
" ", ntohll(fm
.cookie
));
767 if (fm
.idle_timeout
!= OFP_FLOW_PERMANENT
) {
768 ds_put_format(s
, "idle:%"PRIu16
" ", fm
.idle_timeout
);
770 if (fm
.hard_timeout
!= OFP_FLOW_PERMANENT
) {
771 ds_put_format(s
, "hard:%"PRIu16
" ", fm
.hard_timeout
);
773 if (fm
.cr
.priority
!= OFP_DEFAULT_PRIORITY
&& need_priority
) {
774 ds_put_format(s
, "pri:%"PRIu16
" ", fm
.cr
.priority
);
776 if (fm
.buffer_id
!= UINT32_MAX
) {
777 ds_put_format(s
, "buf:0x%"PRIx32
" ", fm
.buffer_id
);
780 ds_put_format(s
, "flags:0x%"PRIx16
" ", fm
.flags
);
783 ofp_print_actions(s
, fm
.actions
, fm
.n_actions
);
787 ofp_print_duration(struct ds
*string
, unsigned int sec
, unsigned int nsec
)
789 ds_put_format(string
, "%u", sec
);
791 ds_put_format(string
, ".%09u", nsec
);
792 while (string
->string
[string
->length
- 1] == '0') {
796 ds_put_char(string
, 's');
800 ofp_print_flow_removed(struct ds
*string
, const struct ofp_header
*oh
)
802 struct ofputil_flow_removed fr
;
805 error
= ofputil_decode_flow_removed(&fr
, oh
);
807 ofp_print_error(string
, error
);
811 ds_put_char(string
, ' ');
812 cls_rule_format(&fr
.rule
, string
);
814 ds_put_cstr(string
, " reason=");
816 case OFPRR_IDLE_TIMEOUT
:
817 ds_put_cstr(string
, "idle");
819 case OFPRR_HARD_TIMEOUT
:
820 ds_put_cstr(string
, "hard");
823 ds_put_cstr(string
, "delete");
826 ds_put_format(string
, "**%"PRIu8
"**", fr
.reason
);
830 if (fr
.cookie
!= htonll(0)) {
831 ds_put_format(string
, " cookie:0x%"PRIx64
, ntohll(fr
.cookie
));
833 ds_put_cstr(string
, " duration");
834 ofp_print_duration(string
, fr
.duration_sec
, fr
.duration_nsec
);
835 ds_put_format(string
, " idle%"PRIu16
" pkts%"PRIu64
" bytes%"PRIu64
"\n",
836 fr
.idle_timeout
, fr
.packet_count
, fr
.byte_count
);
840 ofp_print_port_mod(struct ds
*string
, const struct ofp_port_mod
*opm
)
842 ds_put_format(string
, "port: %d: addr:"ETH_ADDR_FMT
", config: %#x, mask:%#x\n",
843 ntohs(opm
->port_no
), ETH_ADDR_ARGS(opm
->hw_addr
),
844 ntohl(opm
->config
), ntohl(opm
->mask
));
845 ds_put_format(string
, " advertise: ");
846 if (opm
->advertise
) {
847 ofp_print_port_features(string
, ntohl(opm
->advertise
));
849 ds_put_format(string
, "UNCHANGED\n");
854 ofp_print_error(struct ds
*string
, int error
)
856 if (string
->length
) {
857 ds_put_char(string
, ' ');
859 ds_put_cstr(string
, "***decode error: ");
860 ofputil_format_error(string
, error
);
861 ds_put_cstr(string
, "***\n");
865 ofp_print_error_msg(struct ds
*string
, const struct ofp_error_msg
*oem
)
867 size_t len
= ntohs(oem
->header
.length
);
868 size_t payload_ofs
, payload_len
;
873 error
= ofputil_decode_error_msg(&oem
->header
, &payload_ofs
);
874 if (!is_ofp_error(error
)) {
875 ofp_print_error(string
, error
);
876 ds_put_hex_dump(string
, oem
->data
, len
- sizeof *oem
, 0, true);
880 ds_put_char(string
, ' ');
881 ofputil_format_error(string
, error
);
882 ds_put_char(string
, '\n');
884 payload
= (const uint8_t *) oem
+ payload_ofs
;
885 payload_len
= len
- payload_ofs
;
886 switch (get_ofp_err_type(error
)) {
887 case OFPET_HELLO_FAILED
:
888 ds_put_printable(string
, payload
, payload_len
);
892 s
= ofp_to_string(payload
, payload_len
, 1);
893 ds_put_cstr(string
, s
);
900 ofp_print_port_status(struct ds
*string
, const struct ofp_port_status
*ops
)
902 if (ops
->reason
== OFPPR_ADD
) {
903 ds_put_format(string
, " ADD:");
904 } else if (ops
->reason
== OFPPR_DELETE
) {
905 ds_put_format(string
, " DEL:");
906 } else if (ops
->reason
== OFPPR_MODIFY
) {
907 ds_put_format(string
, " MOD:");
910 ofp_print_phy_port(string
, &ops
->desc
);
914 ofp_print_ofpst_desc_reply(struct ds
*string
, const struct ofp_desc_stats
*ods
)
916 ds_put_char(string
, '\n');
917 ds_put_format(string
, "Manufacturer: %.*s\n",
918 (int) sizeof ods
->mfr_desc
, ods
->mfr_desc
);
919 ds_put_format(string
, "Hardware: %.*s\n",
920 (int) sizeof ods
->hw_desc
, ods
->hw_desc
);
921 ds_put_format(string
, "Software: %.*s\n",
922 (int) sizeof ods
->sw_desc
, ods
->sw_desc
);
923 ds_put_format(string
, "Serial Num: %.*s\n",
924 (int) sizeof ods
->serial_num
, ods
->serial_num
);
925 ds_put_format(string
, "DP Description: %.*s\n",
926 (int) sizeof ods
->dp_desc
, ods
->dp_desc
);
930 ofp_print_flow_stats_request(struct ds
*string
,
931 const struct ofp_stats_msg
*osm
)
933 struct ofputil_flow_stats_request fsr
;
936 error
= ofputil_decode_flow_stats_request(&fsr
, &osm
->header
);
938 ofp_print_error(string
, error
);
942 if (fsr
.table_id
!= 0xff) {
943 ds_put_format(string
, " table=%"PRIu8
, fsr
.table_id
);
946 if (fsr
.out_port
!= OFPP_NONE
) {
947 ds_put_cstr(string
, " out_port=");
948 ofputil_format_port(fsr
.out_port
, string
);
951 /* A flow stats request doesn't include a priority, but cls_rule_format()
952 * will print one unless it is OFP_DEFAULT_PRIORITY. */
953 fsr
.match
.priority
= OFP_DEFAULT_PRIORITY
;
955 ds_put_char(string
, ' ');
956 cls_rule_format(&fsr
.match
, string
);
960 ofp_print_flow_stats_reply(struct ds
*string
, const struct ofp_header
*oh
)
964 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
966 struct ofputil_flow_stats fs
;
969 retval
= ofputil_decode_flow_stats_reply(&fs
, &b
);
972 ds_put_cstr(string
, " ***parse error***");
977 ds_put_char(string
, '\n');
979 ds_put_format(string
, " cookie=0x%"PRIx64
", duration=",
981 ofp_print_duration(string
, fs
.duration_sec
, fs
.duration_nsec
);
982 ds_put_format(string
, ", table=%"PRIu8
", ", fs
.table_id
);
983 ds_put_format(string
, "n_packets=%"PRIu64
", ", fs
.packet_count
);
984 ds_put_format(string
, "n_bytes=%"PRIu64
", ", fs
.byte_count
);
985 if (fs
.idle_timeout
!= OFP_FLOW_PERMANENT
) {
986 ds_put_format(string
, "idle_timeout=%"PRIu16
",", fs
.idle_timeout
);
988 if (fs
.hard_timeout
!= OFP_FLOW_PERMANENT
) {
989 ds_put_format(string
, "hard_timeout=%"PRIu16
",", fs
.hard_timeout
);
992 cls_rule_format(&fs
.rule
, string
);
993 if (string
->string
[string
->length
- 1] != ' ') {
994 ds_put_char(string
, ' ');
996 ofp_print_actions(string
, fs
.actions
, fs
.n_actions
);
1001 ofp_print_ofpst_aggregate_reply(struct ds
*string
,
1002 const struct ofp_aggregate_stats_reply
*asr
)
1004 ds_put_format(string
, " packet_count=%"PRIu64
,
1005 ntohll(get_32aligned_be64(&asr
->packet_count
)));
1006 ds_put_format(string
, " byte_count=%"PRIu64
,
1007 ntohll(get_32aligned_be64(&asr
->byte_count
)));
1008 ds_put_format(string
, " flow_count=%"PRIu32
, ntohl(asr
->flow_count
));
1012 ofp_print_nxst_aggregate_reply(struct ds
*string
,
1013 const struct nx_aggregate_stats_reply
*nasr
)
1015 ds_put_format(string
, " packet_count=%"PRIu64
, ntohll(nasr
->packet_count
));
1016 ds_put_format(string
, " byte_count=%"PRIu64
, ntohll(nasr
->byte_count
));
1017 ds_put_format(string
, " flow_count=%"PRIu32
, ntohl(nasr
->flow_count
));
1020 static void print_port_stat(struct ds
*string
, const char *leader
,
1021 const ovs_32aligned_be64
*statp
, int more
)
1023 uint64_t stat
= ntohll(get_32aligned_be64(statp
));
1025 ds_put_cstr(string
, leader
);
1026 if (stat
!= UINT64_MAX
) {
1027 ds_put_format(string
, "%"PRIu64
, stat
);
1029 ds_put_char(string
, '?');
1032 ds_put_cstr(string
, ", ");
1034 ds_put_cstr(string
, "\n");
1039 ofp_print_ofpst_port_request(struct ds
*string
,
1040 const struct ofp_port_stats_request
*psr
)
1042 ds_put_format(string
, " port_no=%"PRIu16
, ntohs(psr
->port_no
));
1046 ofp_print_ofpst_port_reply(struct ds
*string
, const struct ofp_header
*oh
,
1049 const struct ofp_port_stats
*ps
= ofputil_stats_body(oh
);
1050 size_t n
= ofputil_stats_body_len(oh
) / sizeof *ps
;
1051 ds_put_format(string
, " %zu ports\n", n
);
1052 if (verbosity
< 1) {
1057 ds_put_format(string
, " port %2"PRIu16
": ", ntohs(ps
->port_no
));
1059 ds_put_cstr(string
, "rx ");
1060 print_port_stat(string
, "pkts=", &ps
->rx_packets
, 1);
1061 print_port_stat(string
, "bytes=", &ps
->rx_bytes
, 1);
1062 print_port_stat(string
, "drop=", &ps
->rx_dropped
, 1);
1063 print_port_stat(string
, "errs=", &ps
->rx_errors
, 1);
1064 print_port_stat(string
, "frame=", &ps
->rx_frame_err
, 1);
1065 print_port_stat(string
, "over=", &ps
->rx_over_err
, 1);
1066 print_port_stat(string
, "crc=", &ps
->rx_crc_err
, 0);
1068 ds_put_cstr(string
, " tx ");
1069 print_port_stat(string
, "pkts=", &ps
->tx_packets
, 1);
1070 print_port_stat(string
, "bytes=", &ps
->tx_bytes
, 1);
1071 print_port_stat(string
, "drop=", &ps
->tx_dropped
, 1);
1072 print_port_stat(string
, "errs=", &ps
->tx_errors
, 1);
1073 print_port_stat(string
, "coll=", &ps
->collisions
, 0);
1078 ofp_print_ofpst_table_reply(struct ds
*string
, const struct ofp_header
*oh
,
1081 const struct ofp_table_stats
*ts
= ofputil_stats_body(oh
);
1082 size_t n
= ofputil_stats_body_len(oh
) / sizeof *ts
;
1083 ds_put_format(string
, " %zu tables\n", n
);
1084 if (verbosity
< 1) {
1089 char name
[OFP_MAX_TABLE_NAME_LEN
+ 1];
1090 ovs_strlcpy(name
, ts
->name
, sizeof name
);
1092 ds_put_format(string
, " %d: %-8s: ", ts
->table_id
, name
);
1093 ds_put_format(string
, "wild=0x%05"PRIx32
", ", ntohl(ts
->wildcards
));
1094 ds_put_format(string
, "max=%6"PRIu32
", ", ntohl(ts
->max_entries
));
1095 ds_put_format(string
, "active=%"PRIu32
"\n", ntohl(ts
->active_count
));
1096 ds_put_cstr(string
, " ");
1097 ds_put_format(string
, "lookup=%"PRIu64
", ",
1098 ntohll(get_32aligned_be64(&ts
->lookup_count
)));
1099 ds_put_format(string
, "matched=%"PRIu64
"\n",
1100 ntohll(get_32aligned_be64(&ts
->matched_count
)));
1105 ofp_print_queue_name(struct ds
*string
, uint32_t queue_id
)
1107 if (queue_id
== OFPQ_ALL
) {
1108 ds_put_cstr(string
, "ALL");
1110 ds_put_format(string
, "%"PRIu32
, queue_id
);
1115 ofp_print_ofpst_queue_request(struct ds
*string
,
1116 const struct ofp_queue_stats_request
*qsr
)
1118 ds_put_cstr(string
, "port=");
1119 ofputil_format_port(ntohs(qsr
->port_no
), string
);
1121 ds_put_cstr(string
, " queue=");
1122 ofp_print_queue_name(string
, ntohl(qsr
->queue_id
));
1126 ofp_print_ofpst_queue_reply(struct ds
*string
, const struct ofp_header
*oh
,
1129 const struct ofp_queue_stats
*qs
= ofputil_stats_body(oh
);
1130 size_t n
= ofputil_stats_body_len(oh
) / sizeof *qs
;
1131 ds_put_format(string
, " %zu queues\n", n
);
1132 if (verbosity
< 1) {
1137 ds_put_cstr(string
, " port ");
1138 ofputil_format_port(ntohs(qs
->port_no
), string
);
1139 ds_put_cstr(string
, " queue ");
1140 ofp_print_queue_name(string
, ntohl(qs
->queue_id
));
1141 ds_put_cstr(string
, ": ");
1143 print_port_stat(string
, "bytes=", &qs
->tx_bytes
, 1);
1144 print_port_stat(string
, "pkts=", &qs
->tx_packets
, 1);
1145 print_port_stat(string
, "errors=", &qs
->tx_errors
, 0);
1150 ofp_print_stats_request(struct ds
*string
, const struct ofp_header
*oh
)
1152 const struct ofp_stats_msg
*srq
= (const struct ofp_stats_msg
*) oh
;
1155 ds_put_format(string
, " ***unknown flags 0x%04"PRIx16
"***",
1161 ofp_print_stats_reply(struct ds
*string
, const struct ofp_header
*oh
)
1163 const struct ofp_stats_msg
*srp
= (const struct ofp_stats_msg
*) oh
;
1166 uint16_t flags
= ntohs(srp
->flags
);
1168 ds_put_cstr(string
, " flags=");
1169 if (flags
& OFPSF_REPLY_MORE
) {
1170 ds_put_cstr(string
, "[more]");
1171 flags
&= ~OFPSF_REPLY_MORE
;
1174 ds_put_format(string
, "[***unknown flags 0x%04"PRIx16
"***]",
1181 ofp_print_echo(struct ds
*string
, const struct ofp_header
*oh
, int verbosity
)
1183 size_t len
= ntohs(oh
->length
);
1185 ds_put_format(string
, " %zu bytes of payload\n", len
- sizeof *oh
);
1186 if (verbosity
> 1) {
1187 ds_put_hex_dump(string
, oh
+ 1, len
- sizeof *oh
, 0, true);
1192 ofp_print_nxt_role_message(struct ds
*string
,
1193 const struct nx_role_request
*nrr
)
1195 unsigned int role
= ntohl(nrr
->role
);
1197 ds_put_cstr(string
, " role=");
1198 if (role
== NX_ROLE_OTHER
) {
1199 ds_put_cstr(string
, "other");
1200 } else if (role
== NX_ROLE_MASTER
) {
1201 ds_put_cstr(string
, "master");
1202 } else if (role
== NX_ROLE_SLAVE
) {
1203 ds_put_cstr(string
, "slave");
1205 ds_put_format(string
, "%u", role
);
1210 ofp_print_nxt_flow_mod_table_id(struct ds
*string
,
1211 const struct nxt_flow_mod_table_id
*nfmti
)
1213 ds_put_format(string
, " %s", nfmti
->set
? "enable" : "disable");
1217 ofp_print_nxt_set_flow_format(struct ds
*string
,
1218 const struct nxt_set_flow_format
*nsff
)
1220 uint32_t format
= ntohl(nsff
->format
);
1222 ds_put_cstr(string
, " format=");
1223 if (ofputil_flow_format_is_valid(format
)) {
1224 ds_put_cstr(string
, ofputil_flow_format_to_string(format
));
1226 ds_put_format(string
, "%"PRIu32
, format
);
1231 ofp_to_string__(const struct ofp_header
*oh
,
1232 const struct ofputil_msg_type
*type
, struct ds
*string
,
1235 enum ofputil_msg_code code
;
1236 const void *msg
= oh
;
1238 ds_put_format(string
, "%s (xid=0x%"PRIx32
"):",
1239 ofputil_msg_type_name(type
), ntohl(oh
->xid
));
1241 code
= ofputil_msg_type_code(type
);
1243 case OFPUTIL_MSG_INVALID
:
1246 case OFPUTIL_OFPT_HELLO
:
1247 ds_put_char(string
, '\n');
1248 ds_put_hex_dump(string
, oh
+ 1, ntohs(oh
->length
) - sizeof *oh
,
1252 case OFPUTIL_OFPT_ERROR
:
1253 ofp_print_error_msg(string
, msg
);
1256 case OFPUTIL_OFPT_ECHO_REQUEST
:
1257 case OFPUTIL_OFPT_ECHO_REPLY
:
1258 ofp_print_echo(string
, oh
, verbosity
);
1261 case OFPUTIL_OFPT_FEATURES_REQUEST
:
1264 case OFPUTIL_OFPT_FEATURES_REPLY
:
1265 ofp_print_switch_features(string
, msg
);
1268 case OFPUTIL_OFPT_GET_CONFIG_REQUEST
:
1271 case OFPUTIL_OFPT_GET_CONFIG_REPLY
:
1272 case OFPUTIL_OFPT_SET_CONFIG
:
1273 ofp_print_switch_config(string
, msg
);
1276 case OFPUTIL_OFPT_PACKET_IN
:
1277 ofp_print_packet_in(string
, msg
, verbosity
);
1280 case OFPUTIL_OFPT_FLOW_REMOVED
:
1281 case OFPUTIL_NXT_FLOW_REMOVED
:
1282 ofp_print_flow_removed(string
, msg
);
1285 case OFPUTIL_OFPT_PORT_STATUS
:
1286 ofp_print_port_status(string
, msg
);
1289 case OFPUTIL_OFPT_PACKET_OUT
:
1290 ofp_print_packet_out(string
, msg
, verbosity
);
1293 case OFPUTIL_OFPT_FLOW_MOD
:
1294 ofp_print_flow_mod(string
, msg
, code
, verbosity
);
1297 case OFPUTIL_OFPT_PORT_MOD
:
1298 ofp_print_port_mod(string
, msg
);
1301 case OFPUTIL_OFPT_BARRIER_REQUEST
:
1302 case OFPUTIL_OFPT_BARRIER_REPLY
:
1305 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST
:
1306 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY
:
1310 case OFPUTIL_OFPST_DESC_REQUEST
:
1311 ofp_print_stats_request(string
, oh
);
1314 case OFPUTIL_OFPST_FLOW_REQUEST
:
1315 case OFPUTIL_NXST_FLOW_REQUEST
:
1316 case OFPUTIL_OFPST_AGGREGATE_REQUEST
:
1317 case OFPUTIL_NXST_AGGREGATE_REQUEST
:
1318 ofp_print_stats_request(string
, oh
);
1319 ofp_print_flow_stats_request(string
, msg
);
1322 case OFPUTIL_OFPST_TABLE_REQUEST
:
1323 ofp_print_stats_request(string
, oh
);
1326 case OFPUTIL_OFPST_PORT_REQUEST
:
1327 ofp_print_stats_request(string
, oh
);
1328 ofp_print_ofpst_port_request(string
, msg
);
1331 case OFPUTIL_OFPST_QUEUE_REQUEST
:
1332 ofp_print_stats_request(string
, oh
);
1333 ofp_print_ofpst_queue_request(string
, msg
);
1336 case OFPUTIL_OFPST_DESC_REPLY
:
1337 ofp_print_stats_reply(string
, oh
);
1338 ofp_print_ofpst_desc_reply(string
, msg
);
1341 case OFPUTIL_OFPST_FLOW_REPLY
:
1342 case OFPUTIL_NXST_FLOW_REPLY
:
1343 ofp_print_stats_reply(string
, oh
);
1344 ofp_print_flow_stats_reply(string
, oh
);
1347 case OFPUTIL_OFPST_QUEUE_REPLY
:
1348 ofp_print_stats_reply(string
, oh
);
1349 ofp_print_ofpst_queue_reply(string
, oh
, verbosity
);
1352 case OFPUTIL_OFPST_PORT_REPLY
:
1353 ofp_print_stats_reply(string
, oh
);
1354 ofp_print_ofpst_port_reply(string
, oh
, verbosity
);
1357 case OFPUTIL_OFPST_TABLE_REPLY
:
1358 ofp_print_stats_reply(string
, oh
);
1359 ofp_print_ofpst_table_reply(string
, oh
, verbosity
);
1362 case OFPUTIL_OFPST_AGGREGATE_REPLY
:
1363 ofp_print_stats_reply(string
, oh
);
1364 ofp_print_ofpst_aggregate_reply(string
, msg
);
1367 case OFPUTIL_NXT_ROLE_REQUEST
:
1368 case OFPUTIL_NXT_ROLE_REPLY
:
1369 ofp_print_nxt_role_message(string
, msg
);
1372 case OFPUTIL_NXT_FLOW_MOD_TABLE_ID
:
1373 ofp_print_nxt_flow_mod_table_id(string
, msg
);
1376 case OFPUTIL_NXT_SET_FLOW_FORMAT
:
1377 ofp_print_nxt_set_flow_format(string
, msg
);
1380 case OFPUTIL_NXT_FLOW_MOD
:
1381 ofp_print_flow_mod(string
, msg
, code
, verbosity
);
1384 case OFPUTIL_NXST_AGGREGATE_REPLY
:
1385 ofp_print_stats_reply(string
, oh
);
1386 ofp_print_nxst_aggregate_reply(string
, msg
);
1391 /* Composes and returns a string representing the OpenFlow packet of 'len'
1392 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1393 * verbosity and higher numbers increase verbosity. The caller is responsible
1394 * for freeing the string. */
1396 ofp_to_string(const void *oh_
, size_t len
, int verbosity
)
1398 struct ds string
= DS_EMPTY_INITIALIZER
;
1399 const struct ofp_header
*oh
= oh_
;
1402 ds_put_cstr(&string
, "OpenFlow message is empty\n");
1403 } else if (len
< sizeof(struct ofp_header
)) {
1404 ds_put_format(&string
, "OpenFlow packet too short (only %zu bytes):\n",
1406 } else if (oh
->version
!= OFP_VERSION
) {
1407 ds_put_format(&string
, "Bad OpenFlow version %"PRIu8
":\n",
1409 } else if (ntohs(oh
->length
) > len
) {
1410 ds_put_format(&string
,
1411 "(***truncated to %zu bytes from %"PRIu16
"***)\n",
1412 len
, ntohs(oh
->length
));
1413 } else if (ntohs(oh
->length
) < len
) {
1414 ds_put_format(&string
,
1415 "(***only uses %"PRIu16
" bytes out of %zu***)\n",
1416 ntohs(oh
->length
), len
);
1418 const struct ofputil_msg_type
*type
;
1421 error
= ofputil_decode_msg_type(oh
, &type
);
1423 ofp_to_string__(oh
, type
, &string
, verbosity
);
1424 if (verbosity
>= 5) {
1425 if (ds_last(&string
) != '\n') {
1426 ds_put_char(&string
, '\n');
1428 ds_put_hex_dump(&string
, oh
, len
, 0, true);
1430 if (ds_last(&string
) != '\n') {
1431 ds_put_char(&string
, '\n');
1433 return ds_steal_cstr(&string
);
1436 ofp_print_error(&string
, error
);
1438 ds_put_hex_dump(&string
, oh
, len
, 0, true);
1439 return ds_steal_cstr(&string
);
1442 /* Returns the name for the specified OpenFlow message type as a string,
1443 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1444 * hex number, e.g. "0x55".
1446 * The caller must free the returned string when it is no longer needed. */
1448 ofp_message_type_to_string(uint8_t type
)
1459 case OFPT_ECHO_REQUEST
:
1460 name
= "ECHO_REQUEST";
1462 case OFPT_ECHO_REPLY
:
1463 name
= "ECHO_REPLY";
1468 case OFPT_FEATURES_REQUEST
:
1469 name
= "FEATURES_REQUEST";
1471 case OFPT_FEATURES_REPLY
:
1472 name
= "FEATURES_REPLY";
1474 case OFPT_GET_CONFIG_REQUEST
:
1475 name
= "GET_CONFIG_REQUEST";
1477 case OFPT_GET_CONFIG_REPLY
:
1478 name
= "GET_CONFIG_REPLY";
1480 case OFPT_SET_CONFIG
:
1481 name
= "SET_CONFIG";
1483 case OFPT_PACKET_IN
:
1486 case OFPT_FLOW_REMOVED
:
1487 name
= "FLOW_REMOVED";
1489 case OFPT_PORT_STATUS
:
1490 name
= "PORT_STATUS";
1492 case OFPT_PACKET_OUT
:
1493 name
= "PACKET_OUT";
1501 case OFPT_STATS_REQUEST
:
1502 name
= "STATS_REQUEST";
1504 case OFPT_STATS_REPLY
:
1505 name
= "STATS_REPLY";
1507 case OFPT_BARRIER_REQUEST
:
1508 name
= "BARRIER_REQUEST";
1510 case OFPT_BARRIER_REPLY
:
1511 name
= "BARRIER_REPLY";
1513 case OFPT_QUEUE_GET_CONFIG_REQUEST
:
1514 name
= "QUEUE_GET_CONFIG_REQUEST";
1516 case OFPT_QUEUE_GET_CONFIG_REPLY
:
1517 name
= "QUEUE_GET_CONFIG_REPLY";
1524 return name
? xasprintf("OFPT_%s", name
) : xasprintf("0x%02"PRIx8
, type
);
1528 print_and_free(FILE *stream
, char *string
)
1530 fputs(string
, stream
);
1534 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1535 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1536 * numbers increase verbosity. */
1538 ofp_print(FILE *stream
, const void *oh
, size_t len
, int verbosity
)
1540 print_and_free(stream
, ofp_to_string(oh
, len
, verbosity
));
1543 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1544 * 'data' to 'stream'. */
1546 ofp_print_packet(FILE *stream
, const void *data
, size_t len
)
1548 print_and_free(stream
, ofp_packet_to_string(data
, len
));