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-errors.h"
42 #include "openflow/openflow.h"
43 #include "openflow/nicira-ext.h"
46 #include "type-props.h"
47 #include "unaligned.h"
50 static void ofp_print_queue_name(struct ds
*string
, uint32_t port
);
51 static void ofp_print_error(struct ds
*, enum ofperr
);
54 /* Returns a string that represents the contents of the Ethernet frame in the
55 * 'len' bytes starting at 'data'. The caller must free the returned string.*/
57 ofp_packet_to_string(const void *data
, size_t len
)
59 struct ds ds
= DS_EMPTY_INITIALIZER
;
63 ofpbuf_use_const(&buf
, data
, len
);
64 flow_extract(&buf
, 0, 0, 0, &flow
);
65 flow_format(&ds
, &flow
);
68 if (flow
.nw_proto
== IPPROTO_TCP
) {
69 struct tcp_header
*th
= buf
.l4
;
70 ds_put_format(&ds
, " tcp_csum:%"PRIx16
,
72 } else if (flow
.nw_proto
== IPPROTO_UDP
) {
73 struct udp_header
*uh
= buf
.l4
;
74 ds_put_format(&ds
, " udp_csum:%"PRIx16
,
79 ds_put_char(&ds
, '\n');
85 ofp_print_packet_in(struct ds
*string
, const struct ofp_header
*oh
,
88 struct ofputil_packet_in pin
;
92 error
= ofputil_decode_packet_in(&pin
, oh
);
94 ofp_print_error(string
, error
);
99 ds_put_format(string
, " table_id=%"PRIu8
, pin
.table_id
);
103 ds_put_format(string
, " cookie=0x%"PRIx64
, ntohll(pin
.cookie
));
106 ds_put_format(string
, " total_len=%"PRIu16
" in_port=", pin
.total_len
);
107 ofputil_format_port(pin
.fmd
.in_port
, string
);
109 if (pin
.fmd
.tun_id_mask
) {
110 ds_put_format(string
, " tun_id=0x%"PRIx64
, ntohll(pin
.fmd
.tun_id
));
111 if (pin
.fmd
.tun_id_mask
!= htonll(UINT64_MAX
)) {
112 ds_put_format(string
, "/0x%"PRIx64
, ntohll(pin
.fmd
.tun_id_mask
));
116 if (pin
.fmd
.metadata_mask
) {
117 ds_put_format(string
, " metadata=0x%"PRIx64
, ntohll(pin
.fmd
.metadata
));
118 if (pin
.fmd
.metadata_mask
!= htonll(UINT64_MAX
)) {
119 ds_put_format(string
, "/0x%"PRIx64
, ntohll(pin
.fmd
.metadata_mask
));
123 for (i
= 0; i
< FLOW_N_REGS
; i
++) {
124 if (pin
.fmd
.reg_masks
[i
]) {
125 ds_put_format(string
, " reg%d=0x%"PRIx32
, i
, pin
.fmd
.regs
[i
]);
126 if (pin
.fmd
.reg_masks
[i
] != UINT32_MAX
) {
127 ds_put_format(string
, "/0x%"PRIx32
, pin
.fmd
.reg_masks
[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 print_note(struct ds
*string
, const struct nx_action_note
*nan
)
162 ds_put_cstr(string
, "note:");
163 len
= ntohs(nan
->len
) - offsetof(struct nx_action_note
, note
);
164 for (i
= 0; i
< len
; i
++) {
166 ds_put_char(string
, '.');
168 ds_put_format(string
, "%02"PRIx8
, nan
->note
[i
]);
173 ofp_print_action(struct ds
*s
, const union ofp_action
*a
,
174 enum ofputil_action_code code
)
176 const struct ofp_action_enqueue
*oae
;
177 const struct ofp_action_dl_addr
*oada
;
178 const struct nx_action_set_tunnel64
*nast64
;
179 const struct nx_action_set_tunnel
*nast
;
180 const struct nx_action_set_queue
*nasq
;
181 const struct nx_action_resubmit
*nar
;
182 const struct nx_action_reg_move
*move
;
183 const struct nx_action_reg_load
*load
;
184 const struct nx_action_multipath
*nam
;
185 const struct nx_action_autopath
*naa
;
186 const struct nx_action_output_reg
*naor
;
187 const struct nx_action_fin_timeout
*naft
;
188 const struct nx_action_controller
*nac
;
189 struct mf_subfield subfield
;
193 case OFPUTIL_OFPAT10_OUTPUT
:
194 port
= ntohs(a
->output
.port
);
195 if (port
< OFPP_MAX
) {
196 ds_put_format(s
, "output:%"PRIu16
, port
);
198 ofputil_format_port(port
, s
);
199 if (port
== OFPP_CONTROLLER
) {
200 if (a
->output
.max_len
!= htons(0)) {
201 ds_put_format(s
, ":%"PRIu16
, ntohs(a
->output
.max_len
));
203 ds_put_cstr(s
, ":all");
209 case OFPUTIL_OFPAT10_ENQUEUE
:
210 oae
= (const struct ofp_action_enqueue
*) a
;
211 ds_put_format(s
, "enqueue:");
212 ofputil_format_port(ntohs(oae
->port
), s
);
213 ds_put_format(s
, "q%"PRIu32
, ntohl(oae
->queue_id
));
216 case OFPUTIL_OFPAT10_SET_VLAN_VID
:
217 ds_put_format(s
, "mod_vlan_vid:%"PRIu16
,
218 ntohs(a
->vlan_vid
.vlan_vid
));
221 case OFPUTIL_OFPAT10_SET_VLAN_PCP
:
222 ds_put_format(s
, "mod_vlan_pcp:%"PRIu8
, a
->vlan_pcp
.vlan_pcp
);
225 case OFPUTIL_OFPAT10_STRIP_VLAN
:
226 ds_put_cstr(s
, "strip_vlan");
229 case OFPUTIL_OFPAT10_SET_DL_SRC
:
230 oada
= (const struct ofp_action_dl_addr
*) a
;
231 ds_put_format(s
, "mod_dl_src:"ETH_ADDR_FMT
,
232 ETH_ADDR_ARGS(oada
->dl_addr
));
235 case OFPUTIL_OFPAT10_SET_DL_DST
:
236 oada
= (const struct ofp_action_dl_addr
*) a
;
237 ds_put_format(s
, "mod_dl_dst:"ETH_ADDR_FMT
,
238 ETH_ADDR_ARGS(oada
->dl_addr
));
241 case OFPUTIL_OFPAT10_SET_NW_SRC
:
242 ds_put_format(s
, "mod_nw_src:"IP_FMT
, IP_ARGS(&a
->nw_addr
.nw_addr
));
245 case OFPUTIL_OFPAT10_SET_NW_DST
:
246 ds_put_format(s
, "mod_nw_dst:"IP_FMT
, IP_ARGS(&a
->nw_addr
.nw_addr
));
249 case OFPUTIL_OFPAT10_SET_NW_TOS
:
250 ds_put_format(s
, "mod_nw_tos:%d", a
->nw_tos
.nw_tos
);
253 case OFPUTIL_OFPAT10_SET_TP_SRC
:
254 ds_put_format(s
, "mod_tp_src:%d", ntohs(a
->tp_port
.tp_port
));
257 case OFPUTIL_OFPAT10_SET_TP_DST
:
258 ds_put_format(s
, "mod_tp_dst:%d", ntohs(a
->tp_port
.tp_port
));
261 case OFPUTIL_NXAST_RESUBMIT
:
262 nar
= (struct nx_action_resubmit
*)a
;
263 ds_put_format(s
, "resubmit:");
264 ofputil_format_port(ntohs(nar
->in_port
), s
);
267 case OFPUTIL_NXAST_RESUBMIT_TABLE
:
268 nar
= (struct nx_action_resubmit
*)a
;
269 ds_put_format(s
, "resubmit(");
270 if (nar
->in_port
!= htons(OFPP_IN_PORT
)) {
271 ofputil_format_port(ntohs(nar
->in_port
), s
);
274 if (nar
->table
!= 255) {
275 ds_put_format(s
, "%"PRIu8
, nar
->table
);
280 case OFPUTIL_NXAST_SET_TUNNEL
:
281 nast
= (struct nx_action_set_tunnel
*)a
;
282 ds_put_format(s
, "set_tunnel:%#"PRIx32
, ntohl(nast
->tun_id
));
285 case OFPUTIL_NXAST_SET_QUEUE
:
286 nasq
= (struct nx_action_set_queue
*)a
;
287 ds_put_format(s
, "set_queue:%u", ntohl(nasq
->queue_id
));
290 case OFPUTIL_NXAST_POP_QUEUE
:
291 ds_put_cstr(s
, "pop_queue");
294 case OFPUTIL_NXAST_NOTE
:
295 print_note(s
, (const struct nx_action_note
*) a
);
298 case OFPUTIL_NXAST_REG_MOVE
:
299 move
= (const struct nx_action_reg_move
*) a
;
300 nxm_format_reg_move(move
, s
);
303 case OFPUTIL_NXAST_REG_LOAD
:
304 load
= (const struct nx_action_reg_load
*) a
;
305 nxm_format_reg_load(load
, s
);
308 case OFPUTIL_NXAST_SET_TUNNEL64
:
309 nast64
= (const struct nx_action_set_tunnel64
*) a
;
310 ds_put_format(s
, "set_tunnel64:%#"PRIx64
,
311 ntohll(nast64
->tun_id
));
314 case OFPUTIL_NXAST_MULTIPATH
:
315 nam
= (const struct nx_action_multipath
*) a
;
316 multipath_format(nam
, s
);
319 case OFPUTIL_NXAST_AUTOPATH
:
320 naa
= (const struct nx_action_autopath
*)a
;
321 ds_put_format(s
, "autopath(%u,", ntohl(naa
->id
));
322 nxm_decode(&subfield
, naa
->dst
, naa
->ofs_nbits
);
323 mf_format_subfield(&subfield
, s
);
327 case OFPUTIL_NXAST_BUNDLE
:
328 case OFPUTIL_NXAST_BUNDLE_LOAD
:
329 bundle_format((const struct nx_action_bundle
*) a
, s
);
332 case OFPUTIL_NXAST_OUTPUT_REG
:
333 naor
= (const struct nx_action_output_reg
*) a
;
334 ds_put_cstr(s
, "output:");
335 nxm_decode(&subfield
, naor
->src
, naor
->ofs_nbits
);
336 mf_format_subfield(&subfield
, s
);
339 case OFPUTIL_NXAST_LEARN
:
340 learn_format((const struct nx_action_learn
*) a
, s
);
343 case OFPUTIL_NXAST_DEC_TTL
:
344 ds_put_cstr(s
, "dec_ttl");
347 case OFPUTIL_NXAST_EXIT
:
348 ds_put_cstr(s
, "exit");
351 case OFPUTIL_NXAST_FIN_TIMEOUT
:
352 naft
= (const struct nx_action_fin_timeout
*) a
;
353 ds_put_cstr(s
, "fin_timeout(");
354 if (naft
->fin_idle_timeout
) {
355 ds_put_format(s
, "idle_timeout=%"PRIu16
",",
356 ntohs(naft
->fin_idle_timeout
));
358 if (naft
->fin_hard_timeout
) {
359 ds_put_format(s
, "hard_timeout=%"PRIu16
",",
360 ntohs(naft
->fin_hard_timeout
));
366 case OFPUTIL_NXAST_CONTROLLER
:
367 nac
= (const struct nx_action_controller
*) a
;
368 ds_put_cstr(s
, "controller(");
369 if (nac
->reason
!= OFPR_ACTION
) {
370 ds_put_format(s
, "reason=%s,",
371 ofputil_packet_in_reason_to_string(nac
->reason
));
373 if (nac
->max_len
!= htons(UINT16_MAX
)) {
374 ds_put_format(s
, "max_len=%"PRIu16
",", ntohs(nac
->max_len
));
376 if (nac
->controller_id
!= htons(0)) {
377 ds_put_format(s
, "id=%"PRIu16
",", ntohs(nac
->controller_id
));
389 ofp_print_actions(struct ds
*string
, const union ofp_action
*actions
,
392 const union ofp_action
*a
;
395 ds_put_cstr(string
, "actions=");
397 ds_put_cstr(string
, "drop");
400 OFPUTIL_ACTION_FOR_EACH (a
, left
, actions
, n_actions
) {
401 int code
= ofputil_decode_action(a
);
404 ds_put_cstr(string
, ",");
406 ofp_print_action(string
, a
, code
);
408 ofp_print_error(string
, -code
);
412 ds_put_format(string
, " ***%zu leftover bytes following actions",
418 ofp_print_packet_out(struct ds
*string
, const struct ofp_packet_out
*opo
,
421 struct ofputil_packet_out po
;
424 error
= ofputil_decode_packet_out(&po
, opo
);
426 ofp_print_error(string
, error
);
430 ds_put_cstr(string
, " in_port=");
431 ofputil_format_port(po
.in_port
, string
);
433 ds_put_char(string
, ' ');
434 ofp_print_actions(string
, po
.actions
, po
.n_actions
);
436 if (po
.buffer_id
== UINT32_MAX
) {
437 ds_put_format(string
, " data_len=%zu", po
.packet_len
);
438 if (verbosity
> 0 && po
.packet_len
> 0) {
439 char *packet
= ofp_packet_to_string(po
.packet
, po
.packet_len
);
440 ds_put_char(string
, '\n');
441 ds_put_cstr(string
, packet
);
445 ds_put_format(string
, " buffer=0x%08"PRIx32
, po
.buffer_id
);
447 ds_put_char(string
, '\n');
450 /* qsort comparison function. */
452 compare_ports(const void *a_
, const void *b_
)
454 const struct ofputil_phy_port
*a
= a_
;
455 const struct ofputil_phy_port
*b
= b_
;
456 uint16_t ap
= a
->port_no
;
457 uint16_t bp
= b
->port_no
;
459 return ap
< bp
? -1 : ap
> bp
;
463 ofp_print_bit_names(struct ds
*string
, uint32_t bits
,
464 const char *(*bit_to_name
)(uint32_t bit
))
470 ds_put_cstr(string
, "0");
474 for (i
= 0; i
< 32; i
++) {
475 uint32_t bit
= UINT32_C(1) << i
;
478 const char *name
= bit_to_name(bit
);
481 ds_put_char(string
, ' ');
483 ds_put_cstr(string
, name
);
491 ds_put_char(string
, ' ');
493 ds_put_format(string
, "0x%"PRIx32
, bits
);
498 netdev_feature_to_name(uint32_t bit
)
500 enum netdev_features f
= bit
;
503 case NETDEV_F_10MB_HD
: return "10MB-HD";
504 case NETDEV_F_10MB_FD
: return "10MB-FD";
505 case NETDEV_F_100MB_HD
: return "100MB-HD";
506 case NETDEV_F_100MB_FD
: return "100MB-FD";
507 case NETDEV_F_1GB_HD
: return "1GB-HD";
508 case NETDEV_F_1GB_FD
: return "1GB-FD";
509 case NETDEV_F_10GB_FD
: return "10GB-FD";
510 case NETDEV_F_40GB_FD
: return "40GB-FD";
511 case NETDEV_F_100GB_FD
: return "100GB-FD";
512 case NETDEV_F_1TB_FD
: return "1TB-FD";
513 case NETDEV_F_OTHER
: return "OTHER";
514 case NETDEV_F_COPPER
: return "COPPER";
515 case NETDEV_F_FIBER
: return "FIBER";
516 case NETDEV_F_AUTONEG
: return "AUTO_NEG";
517 case NETDEV_F_PAUSE
: return "AUTO_PAUSE";
518 case NETDEV_F_PAUSE_ASYM
: return "AUTO_PAUSE_ASYM";
525 ofp_print_port_features(struct ds
*string
, enum netdev_features features
)
527 ofp_print_bit_names(string
, features
, netdev_feature_to_name
);
528 ds_put_char(string
, '\n');
532 ofputil_port_config_to_name(uint32_t bit
)
534 enum ofputil_port_config pc
= bit
;
537 case OFPUTIL_PC_PORT_DOWN
: return "PORT_DOWN";
538 case OFPUTIL_PC_NO_STP
: return "NO_STP";
539 case OFPUTIL_PC_NO_RECV
: return "NO_RECV";
540 case OFPUTIL_PC_NO_RECV_STP
: return "NO_RECV_STP";
541 case OFPUTIL_PC_NO_FLOOD
: return "NO_FLOOD";
542 case OFPUTIL_PC_NO_FWD
: return "NO_FWD";
543 case OFPUTIL_PC_NO_PACKET_IN
: return "NO_PACKET_IN";
550 ofp_print_port_config(struct ds
*string
, enum ofputil_port_config config
)
552 ofp_print_bit_names(string
, config
, ofputil_port_config_to_name
);
553 ds_put_char(string
, '\n');
557 ofputil_port_state_to_name(uint32_t bit
)
559 enum ofputil_port_state ps
= bit
;
562 case OFPUTIL_PS_LINK_DOWN
: return "LINK_DOWN";
563 case OFPUTIL_PS_BLOCKED
: return "BLOCKED";
564 case OFPUTIL_PS_LIVE
: return "LIVE";
566 case OFPUTIL_PS_STP_LISTEN
:
567 case OFPUTIL_PS_STP_LEARN
:
568 case OFPUTIL_PS_STP_FORWARD
:
569 case OFPUTIL_PS_STP_BLOCK
:
570 /* Handled elsewhere. */
578 ofp_print_port_state(struct ds
*string
, enum ofputil_port_state state
)
580 enum ofputil_port_state stp_state
;
582 /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
583 * pattern. We have to special case it.
585 * OVS doesn't support STP, so this field will always be 0 if we are
586 * talking to OVS, so we'd always print STP_LISTEN in that case.
587 * Therefore, we don't print anything at all if the value is STP_LISTEN, to
588 * avoid confusing users. */
589 stp_state
= state
& OFPUTIL_PS_STP_MASK
;
592 (stp_state
== OFPUTIL_PS_STP_LEARN
? "STP_LEARN"
593 : stp_state
== OFPUTIL_PS_STP_FORWARD
? "STP_FORWARD"
595 state
&= ~OFPUTIL_PS_STP_MASK
;
597 ofp_print_bit_names(string
, state
, ofputil_port_state_to_name
);
600 ofp_print_bit_names(string
, state
, ofputil_port_state_to_name
);
602 ds_put_char(string
, '\n');
606 ofp_print_phy_port(struct ds
*string
, const struct ofputil_phy_port
*port
)
608 char name
[sizeof port
->name
];
611 memcpy(name
, port
->name
, sizeof name
);
612 for (j
= 0; j
< sizeof name
- 1; j
++) {
613 if (!isprint((unsigned char) name
[j
])) {
619 ds_put_char(string
, ' ');
620 ofputil_format_port(port
->port_no
, string
);
621 ds_put_format(string
, "(%s): addr:"ETH_ADDR_FMT
"\n",
622 name
, ETH_ADDR_ARGS(port
->hw_addr
));
624 ds_put_cstr(string
, " config: ");
625 ofp_print_port_config(string
, port
->config
);
627 ds_put_cstr(string
, " state: ");
628 ofp_print_port_state(string
, port
->state
);
631 ds_put_format(string
, " current: ");
632 ofp_print_port_features(string
, port
->curr
);
634 if (port
->advertised
) {
635 ds_put_format(string
, " advertised: ");
636 ofp_print_port_features(string
, port
->advertised
);
638 if (port
->supported
) {
639 ds_put_format(string
, " supported: ");
640 ofp_print_port_features(string
, port
->supported
);
643 ds_put_format(string
, " peer: ");
644 ofp_print_port_features(string
, port
->peer
);
646 ds_put_format(string
, " speed: %"PRIu32
" Mbps now, "
647 "%"PRIu32
" Mbps max\n",
648 port
->curr_speed
/ UINT32_C(1000),
649 port
->max_speed
/ UINT32_C(1000));
652 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
653 * 'ofp_version', writes a detailed description of each port into
656 ofp_print_phy_ports(struct ds
*string
, uint8_t ofp_version
,
660 struct ofputil_phy_port
*ports
;
664 n_ports
= ofputil_count_phy_ports(ofp_version
, b
);
666 ports
= xmalloc(n_ports
* sizeof *ports
);
667 for (i
= 0; i
< n_ports
; i
++) {
668 error
= ofputil_pull_phy_port(ofp_version
, b
, &ports
[i
]);
670 ofp_print_error(string
, error
);
674 qsort(ports
, n_ports
, sizeof *ports
, compare_ports
);
675 for (i
= 0; i
< n_ports
; i
++) {
676 ofp_print_phy_port(string
, &ports
[i
]);
684 ofputil_capabilities_to_name(uint32_t bit
)
686 enum ofputil_capabilities capabilities
= bit
;
688 switch (capabilities
) {
689 case OFPUTIL_C_FLOW_STATS
: return "FLOW_STATS";
690 case OFPUTIL_C_TABLE_STATS
: return "TABLE_STATS";
691 case OFPUTIL_C_PORT_STATS
: return "PORT_STATS";
692 case OFPUTIL_C_IP_REASM
: return "IP_REASM";
693 case OFPUTIL_C_QUEUE_STATS
: return "QUEUE_STATS";
694 case OFPUTIL_C_ARP_MATCH_IP
: return "ARP_MATCH_IP";
695 case OFPUTIL_C_STP
: return "STP";
696 case OFPUTIL_C_GROUP_STATS
: return "GROUP_STATS";
703 ofputil_action_bitmap_to_name(uint32_t bit
)
705 enum ofputil_action_bitmap action
= bit
;
708 case OFPUTIL_A_OUTPUT
: return "OUTPUT";
709 case OFPUTIL_A_SET_VLAN_VID
: return "SET_VLAN_VID";
710 case OFPUTIL_A_SET_VLAN_PCP
: return "SET_VLAN_PCP";
711 case OFPUTIL_A_STRIP_VLAN
: return "STRIP_VLAN";
712 case OFPUTIL_A_SET_DL_SRC
: return "SET_DL_SRC";
713 case OFPUTIL_A_SET_DL_DST
: return "SET_DL_DST";
714 case OFPUTIL_A_SET_NW_SRC
: return "SET_NW_SRC";
715 case OFPUTIL_A_SET_NW_DST
: return "SET_NW_DST";
716 case OFPUTIL_A_SET_NW_ECN
: return "SET_NW_ECN";
717 case OFPUTIL_A_SET_NW_TOS
: return "SET_NW_TOS";
718 case OFPUTIL_A_SET_TP_SRC
: return "SET_TP_SRC";
719 case OFPUTIL_A_SET_TP_DST
: return "SET_TP_DST";
720 case OFPUTIL_A_ENQUEUE
: return "ENQUEUE";
721 case OFPUTIL_A_COPY_TTL_OUT
: return "COPY_TTL_OUT";
722 case OFPUTIL_A_COPY_TTL_IN
: return "COPY_TTL_IN";
723 case OFPUTIL_A_SET_MPLS_LABEL
: return "SET_MPLS_LABEL";
724 case OFPUTIL_A_SET_MPLS_TC
: return "SET_MPLS_TC";
725 case OFPUTIL_A_SET_MPLS_TTL
: return "SET_MPLS_TTL";
726 case OFPUTIL_A_DEC_MPLS_TTL
: return "DEC_MPLS_TTL";
727 case OFPUTIL_A_PUSH_VLAN
: return "PUSH_VLAN";
728 case OFPUTIL_A_POP_VLAN
: return "POP_VLAN";
729 case OFPUTIL_A_PUSH_MPLS
: return "PUSH_MPLS";
730 case OFPUTIL_A_POP_MPLS
: return "POP_MPLS";
731 case OFPUTIL_A_SET_QUEUE
: return "SET_QUEUE";
732 case OFPUTIL_A_GROUP
: return "GROUP";
733 case OFPUTIL_A_SET_NW_TTL
: return "SET_NW_TTL";
734 case OFPUTIL_A_DEC_NW_TTL
: return "DEC_NW_TTL";
741 ofp_print_switch_features(struct ds
*string
,
742 const struct ofp_switch_features
*osf
)
744 struct ofputil_switch_features features
;
748 error
= ofputil_decode_switch_features(osf
, &features
, &b
);
750 ofp_print_error(string
, error
);
754 ds_put_format(string
, " dpid:%016"PRIx64
"\n", features
.datapath_id
);
755 ds_put_format(string
, "n_tables:%"PRIu8
", n_buffers:%"PRIu32
"\n",
756 features
.n_tables
, features
.n_buffers
);
758 ds_put_cstr(string
, "capabilities: ");
759 ofp_print_bit_names(string
, features
.capabilities
,
760 ofputil_capabilities_to_name
);
761 ds_put_char(string
, '\n');
763 ds_put_cstr(string
, "actions: ");
764 ofp_print_bit_names(string
, features
.actions
,
765 ofputil_action_bitmap_to_name
);
766 ds_put_char(string
, '\n');
768 ofp_print_phy_ports(string
, osf
->header
.version
, &b
);
772 ofp_print_switch_config(struct ds
*string
, const struct ofp_switch_config
*osc
)
774 enum ofp_config_flags flags
;
776 flags
= ntohs(osc
->flags
);
778 ds_put_format(string
, " frags=%s", ofputil_frag_handling_to_string(flags
));
779 flags
&= ~OFPC_FRAG_MASK
;
781 if (flags
& OFPC_INVALID_TTL_TO_CONTROLLER
) {
782 ds_put_format(string
, " invalid_ttl_to_controller");
783 flags
&= ~OFPC_INVALID_TTL_TO_CONTROLLER
;
787 ds_put_format(string
, " ***unknown flags 0x%04"PRIx16
"***", flags
);
790 ds_put_format(string
, " miss_send_len=%"PRIu16
"\n", ntohs(osc
->miss_send_len
));
793 static void print_wild(struct ds
*string
, const char *leader
, int is_wild
,
794 int verbosity
, const char *format
, ...)
795 __attribute__((format(printf
, 5, 6)));
797 static void print_wild(struct ds
*string
, const char *leader
, int is_wild
,
798 int verbosity
, const char *format
, ...)
800 if (is_wild
&& verbosity
< 2) {
803 ds_put_cstr(string
, leader
);
807 va_start(args
, format
);
808 ds_put_format_valist(string
, format
, args
);
811 ds_put_char(string
, '*');
813 ds_put_char(string
, ',');
817 print_ip_netmask(struct ds
*string
, const char *leader
, ovs_be32 ip
,
818 uint32_t wild_bits
, int verbosity
)
820 if (wild_bits
>= 32 && verbosity
< 2) {
823 ds_put_cstr(string
, leader
);
824 if (wild_bits
< 32) {
825 ds_put_format(string
, IP_FMT
, IP_ARGS(&ip
));
827 ds_put_format(string
, "/%d", 32 - wild_bits
);
830 ds_put_char(string
, '*');
832 ds_put_char(string
, ',');
836 ofp10_match_print(struct ds
*f
, const struct ofp10_match
*om
, int verbosity
)
838 char *s
= ofp10_match_to_string(om
, verbosity
);
844 ofp10_match_to_string(const struct ofp10_match
*om
, int verbosity
)
846 struct ds f
= DS_EMPTY_INITIALIZER
;
847 uint32_t w
= ntohl(om
->wildcards
);
848 bool skip_type
= false;
849 bool skip_proto
= false;
851 if (!(w
& OFPFW10_DL_TYPE
)) {
853 if (om
->dl_type
== htons(ETH_TYPE_IP
)) {
854 if (!(w
& OFPFW10_NW_PROTO
)) {
856 if (om
->nw_proto
== IPPROTO_ICMP
) {
857 ds_put_cstr(&f
, "icmp,");
858 } else if (om
->nw_proto
== IPPROTO_TCP
) {
859 ds_put_cstr(&f
, "tcp,");
860 } else if (om
->nw_proto
== IPPROTO_UDP
) {
861 ds_put_cstr(&f
, "udp,");
863 ds_put_cstr(&f
, "ip,");
867 ds_put_cstr(&f
, "ip,");
869 } else if (om
->dl_type
== htons(ETH_TYPE_ARP
)) {
870 ds_put_cstr(&f
, "arp,");
875 print_wild(&f
, "in_port=", w
& OFPFW10_IN_PORT
, verbosity
,
876 "%d", ntohs(om
->in_port
));
877 print_wild(&f
, "dl_vlan=", w
& OFPFW10_DL_VLAN
, verbosity
,
878 "%d", ntohs(om
->dl_vlan
));
879 print_wild(&f
, "dl_vlan_pcp=", w
& OFPFW10_DL_VLAN_PCP
, verbosity
,
880 "%d", om
->dl_vlan_pcp
);
881 print_wild(&f
, "dl_src=", w
& OFPFW10_DL_SRC
, verbosity
,
882 ETH_ADDR_FMT
, ETH_ADDR_ARGS(om
->dl_src
));
883 print_wild(&f
, "dl_dst=", w
& OFPFW10_DL_DST
, verbosity
,
884 ETH_ADDR_FMT
, ETH_ADDR_ARGS(om
->dl_dst
));
886 print_wild(&f
, "dl_type=", w
& OFPFW10_DL_TYPE
, verbosity
,
887 "0x%04x", ntohs(om
->dl_type
));
889 print_ip_netmask(&f
, "nw_src=", om
->nw_src
,
890 (w
& OFPFW10_NW_SRC_MASK
) >> OFPFW10_NW_SRC_SHIFT
,
892 print_ip_netmask(&f
, "nw_dst=", om
->nw_dst
,
893 (w
& OFPFW10_NW_DST_MASK
) >> OFPFW10_NW_DST_SHIFT
,
896 if (om
->dl_type
== htons(ETH_TYPE_ARP
)) {
897 print_wild(&f
, "arp_op=", w
& OFPFW10_NW_PROTO
, verbosity
,
900 print_wild(&f
, "nw_proto=", w
& OFPFW10_NW_PROTO
, verbosity
,
904 print_wild(&f
, "nw_tos=", w
& OFPFW10_NW_TOS
, verbosity
,
906 if (om
->nw_proto
== IPPROTO_ICMP
) {
907 print_wild(&f
, "icmp_type=", w
& OFPFW10_ICMP_TYPE
, verbosity
,
908 "%d", ntohs(om
->tp_src
));
909 print_wild(&f
, "icmp_code=", w
& OFPFW10_ICMP_CODE
, verbosity
,
910 "%d", ntohs(om
->tp_dst
));
912 print_wild(&f
, "tp_src=", w
& OFPFW10_TP_SRC
, verbosity
,
913 "%d", ntohs(om
->tp_src
));
914 print_wild(&f
, "tp_dst=", w
& OFPFW10_TP_DST
, verbosity
,
915 "%d", ntohs(om
->tp_dst
));
917 if (ds_last(&f
) == ',') {
924 ofp_print_flow_mod(struct ds
*s
, const struct ofp_header
*oh
,
925 enum ofputil_msg_code code
, int verbosity
)
927 struct ofputil_flow_mod fm
;
931 error
= ofputil_decode_flow_mod(&fm
, oh
, OFPUTIL_P_OF10_TID
);
933 ofp_print_error(s
, error
);
938 switch (fm
.command
) {
940 ds_put_cstr(s
, "ADD");
943 ds_put_cstr(s
, "MOD");
945 case OFPFC_MODIFY_STRICT
:
946 ds_put_cstr(s
, "MOD_STRICT");
949 ds_put_cstr(s
, "DEL");
951 case OFPFC_DELETE_STRICT
:
952 ds_put_cstr(s
, "DEL_STRICT");
955 ds_put_format(s
, "cmd:%d", fm
.command
);
957 if (fm
.table_id
!= 0) {
958 ds_put_format(s
, " table:%d", fm
.table_id
);
962 if (verbosity
>= 3 && code
== OFPUTIL_OFPT_FLOW_MOD
) {
963 const struct ofp_flow_mod
*ofm
= (const struct ofp_flow_mod
*) oh
;
964 ofp10_match_print(s
, &ofm
->match
, verbosity
);
966 /* ofp_print_match() doesn't print priority. */
967 need_priority
= true;
968 } else if (verbosity
>= 3 && code
== OFPUTIL_NXT_FLOW_MOD
) {
969 const struct nx_flow_mod
*nfm
= (const struct nx_flow_mod
*) oh
;
970 const void *nxm
= nfm
+ 1;
973 nxm_s
= nx_match_to_string(nxm
, ntohs(nfm
->match_len
));
974 ds_put_cstr(s
, nxm_s
);
977 /* nx_match_to_string() doesn't print priority. */
978 need_priority
= true;
980 cls_rule_format(&fm
.cr
, s
);
982 /* cls_rule_format() does print priority. */
983 need_priority
= false;
986 if (ds_last(s
) != ' ') {
989 if (fm
.new_cookie
!= htonll(0)) {
990 ds_put_format(s
, "cookie:0x%"PRIx64
" ", ntohll(fm
.new_cookie
));
992 if (fm
.cookie_mask
!= htonll(0)) {
993 ds_put_format(s
, "cookie:0x%"PRIx64
"/0x%"PRIx64
" ",
994 ntohll(fm
.cookie
), ntohll(fm
.cookie_mask
));
996 if (fm
.idle_timeout
!= OFP_FLOW_PERMANENT
) {
997 ds_put_format(s
, "idle:%"PRIu16
" ", fm
.idle_timeout
);
999 if (fm
.hard_timeout
!= OFP_FLOW_PERMANENT
) {
1000 ds_put_format(s
, "hard:%"PRIu16
" ", fm
.hard_timeout
);
1002 if (fm
.cr
.priority
!= OFP_DEFAULT_PRIORITY
&& need_priority
) {
1003 ds_put_format(s
, "pri:%"PRIu16
" ", fm
.cr
.priority
);
1005 if (fm
.buffer_id
!= UINT32_MAX
) {
1006 ds_put_format(s
, "buf:0x%"PRIx32
" ", fm
.buffer_id
);
1008 if (fm
.flags
!= 0) {
1009 uint16_t flags
= fm
.flags
;
1011 if (flags
& OFPFF_SEND_FLOW_REM
) {
1012 ds_put_cstr(s
, "send_flow_rem ");
1014 if (flags
& OFPFF_CHECK_OVERLAP
) {
1015 ds_put_cstr(s
, "check_overlap ");
1017 if (flags
& OFPFF_EMERG
) {
1018 ds_put_cstr(s
, "emerg ");
1021 flags
&= ~(OFPFF_SEND_FLOW_REM
| OFPFF_CHECK_OVERLAP
| OFPFF_EMERG
);
1023 ds_put_format(s
, "flags:0x%"PRIx16
" ", flags
);
1027 ofp_print_actions(s
, fm
.actions
, fm
.n_actions
);
1031 ofp_print_duration(struct ds
*string
, unsigned int sec
, unsigned int nsec
)
1033 ds_put_format(string
, "%u", sec
);
1035 ds_put_format(string
, ".%09u", nsec
);
1036 while (string
->string
[string
->length
- 1] == '0') {
1040 ds_put_char(string
, 's');
1044 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason
)
1049 case OFPRR_IDLE_TIMEOUT
:
1051 case OFPRR_HARD_TIMEOUT
:
1055 case OFPRR_GROUP_DELETE
:
1056 return "group_delete";
1058 sprintf(s
, "%d", (int) reason
);
1064 ofp_print_flow_removed(struct ds
*string
, const struct ofp_header
*oh
)
1066 struct ofputil_flow_removed fr
;
1069 error
= ofputil_decode_flow_removed(&fr
, oh
);
1071 ofp_print_error(string
, error
);
1075 ds_put_char(string
, ' ');
1076 cls_rule_format(&fr
.rule
, string
);
1078 ds_put_format(string
, " reason=%s",
1079 ofp_flow_removed_reason_to_string(fr
.reason
));
1081 if (fr
.cookie
!= htonll(0)) {
1082 ds_put_format(string
, " cookie:0x%"PRIx64
, ntohll(fr
.cookie
));
1084 ds_put_cstr(string
, " duration");
1085 ofp_print_duration(string
, fr
.duration_sec
, fr
.duration_nsec
);
1086 ds_put_format(string
, " idle%"PRIu16
" pkts%"PRIu64
" bytes%"PRIu64
"\n",
1087 fr
.idle_timeout
, fr
.packet_count
, fr
.byte_count
);
1091 ofp_print_port_mod(struct ds
*string
, const struct ofp_header
*oh
)
1093 struct ofputil_port_mod pm
;
1096 error
= ofputil_decode_port_mod(oh
, &pm
);
1098 ofp_print_error(string
, error
);
1102 ds_put_format(string
, "port: %"PRIu16
": addr:"ETH_ADDR_FMT
"\n",
1103 pm
.port_no
, ETH_ADDR_ARGS(pm
.hw_addr
));
1105 ds_put_format(string
, " config: ");
1106 ofp_print_port_config(string
, pm
.config
);
1108 ds_put_format(string
, " mask: ");
1109 ofp_print_port_config(string
, pm
.mask
);
1111 ds_put_format(string
, " advertise: ");
1113 ofp_print_port_features(string
, pm
.advertise
);
1115 ds_put_format(string
, "UNCHANGED\n");
1120 ofp_print_error(struct ds
*string
, enum ofperr error
)
1122 if (string
->length
) {
1123 ds_put_char(string
, ' ');
1125 ds_put_format(string
, "***decode error: %s***\n", ofperr_get_name(error
));
1129 ofp_print_error_msg(struct ds
*string
, const struct ofp_error_msg
*oem
)
1131 size_t len
= ntohs(oem
->header
.length
);
1132 size_t payload_ofs
, payload_len
;
1133 const void *payload
;
1137 error
= ofperr_decode_msg(&oem
->header
, &payload_ofs
);
1139 ds_put_cstr(string
, "***decode error***");
1140 ds_put_hex_dump(string
, oem
->data
, len
- sizeof *oem
, 0, true);
1144 ds_put_format(string
, " %s\n", ofperr_get_name(error
));
1146 payload
= (const uint8_t *) oem
+ payload_ofs
;
1147 payload_len
= len
- payload_ofs
;
1148 if (error
== OFPERR_OFPHFC_INCOMPATIBLE
|| error
== OFPERR_OFPHFC_EPERM
) {
1149 ds_put_printable(string
, payload
, payload_len
);
1151 s
= ofp_to_string(payload
, payload_len
, 1);
1152 ds_put_cstr(string
, s
);
1158 ofp_print_port_status(struct ds
*string
, const struct ofp_port_status
*ops
)
1160 struct ofputil_port_status ps
;
1163 error
= ofputil_decode_port_status(ops
, &ps
);
1165 ofp_print_error(string
, error
);
1169 if (ps
.reason
== OFPPR_ADD
) {
1170 ds_put_format(string
, " ADD:");
1171 } else if (ps
.reason
== OFPPR_DELETE
) {
1172 ds_put_format(string
, " DEL:");
1173 } else if (ps
.reason
== OFPPR_MODIFY
) {
1174 ds_put_format(string
, " MOD:");
1177 ofp_print_phy_port(string
, &ps
.desc
);
1181 ofp_print_ofpst_desc_reply(struct ds
*string
, const struct ofp_desc_stats
*ods
)
1183 ds_put_char(string
, '\n');
1184 ds_put_format(string
, "Manufacturer: %.*s\n",
1185 (int) sizeof ods
->mfr_desc
, ods
->mfr_desc
);
1186 ds_put_format(string
, "Hardware: %.*s\n",
1187 (int) sizeof ods
->hw_desc
, ods
->hw_desc
);
1188 ds_put_format(string
, "Software: %.*s\n",
1189 (int) sizeof ods
->sw_desc
, ods
->sw_desc
);
1190 ds_put_format(string
, "Serial Num: %.*s\n",
1191 (int) sizeof ods
->serial_num
, ods
->serial_num
);
1192 ds_put_format(string
, "DP Description: %.*s\n",
1193 (int) sizeof ods
->dp_desc
, ods
->dp_desc
);
1197 ofp_print_flow_stats_request(struct ds
*string
,
1198 const struct ofp_stats_msg
*osm
)
1200 struct ofputil_flow_stats_request fsr
;
1203 error
= ofputil_decode_flow_stats_request(&fsr
, &osm
->header
);
1205 ofp_print_error(string
, error
);
1209 if (fsr
.table_id
!= 0xff) {
1210 ds_put_format(string
, " table=%"PRIu8
, fsr
.table_id
);
1213 if (fsr
.out_port
!= OFPP_NONE
) {
1214 ds_put_cstr(string
, " out_port=");
1215 ofputil_format_port(fsr
.out_port
, string
);
1218 /* A flow stats request doesn't include a priority, but cls_rule_format()
1219 * will print one unless it is OFP_DEFAULT_PRIORITY. */
1220 fsr
.match
.priority
= OFP_DEFAULT_PRIORITY
;
1222 ds_put_char(string
, ' ');
1223 cls_rule_format(&fsr
.match
, string
);
1227 ofp_print_flow_stats_reply(struct ds
*string
, const struct ofp_header
*oh
)
1231 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
1233 struct ofputil_flow_stats fs
;
1236 retval
= ofputil_decode_flow_stats_reply(&fs
, &b
, true);
1238 if (retval
!= EOF
) {
1239 ds_put_cstr(string
, " ***parse error***");
1244 ds_put_char(string
, '\n');
1246 ds_put_format(string
, " cookie=0x%"PRIx64
", duration=",
1248 ofp_print_duration(string
, fs
.duration_sec
, fs
.duration_nsec
);
1249 ds_put_format(string
, ", table=%"PRIu8
", ", fs
.table_id
);
1250 ds_put_format(string
, "n_packets=%"PRIu64
", ", fs
.packet_count
);
1251 ds_put_format(string
, "n_bytes=%"PRIu64
", ", fs
.byte_count
);
1252 if (fs
.idle_timeout
!= OFP_FLOW_PERMANENT
) {
1253 ds_put_format(string
, "idle_timeout=%"PRIu16
", ", fs
.idle_timeout
);
1255 if (fs
.hard_timeout
!= OFP_FLOW_PERMANENT
) {
1256 ds_put_format(string
, "hard_timeout=%"PRIu16
", ", fs
.hard_timeout
);
1258 if (fs
.idle_age
>= 0) {
1259 ds_put_format(string
, "idle_age=%d, ", fs
.idle_age
);
1261 if (fs
.hard_age
>= 0 && fs
.hard_age
!= fs
.duration_sec
) {
1262 ds_put_format(string
, "hard_age=%d, ", fs
.hard_age
);
1265 cls_rule_format(&fs
.rule
, string
);
1266 if (string
->string
[string
->length
- 1] != ' ') {
1267 ds_put_char(string
, ' ');
1269 ofp_print_actions(string
, fs
.actions
, fs
.n_actions
);
1274 ofp_print_ofpst_aggregate_reply(struct ds
*string
,
1275 const struct ofp_aggregate_stats_reply
*asr
)
1277 ds_put_format(string
, " packet_count=%"PRIu64
,
1278 ntohll(get_32aligned_be64(&asr
->packet_count
)));
1279 ds_put_format(string
, " byte_count=%"PRIu64
,
1280 ntohll(get_32aligned_be64(&asr
->byte_count
)));
1281 ds_put_format(string
, " flow_count=%"PRIu32
, ntohl(asr
->flow_count
));
1285 ofp_print_nxst_aggregate_reply(struct ds
*string
,
1286 const struct nx_aggregate_stats_reply
*nasr
)
1288 ds_put_format(string
, " packet_count=%"PRIu64
, ntohll(nasr
->packet_count
));
1289 ds_put_format(string
, " byte_count=%"PRIu64
, ntohll(nasr
->byte_count
));
1290 ds_put_format(string
, " flow_count=%"PRIu32
, ntohl(nasr
->flow_count
));
1293 static void print_port_stat(struct ds
*string
, const char *leader
,
1294 const ovs_32aligned_be64
*statp
, int more
)
1296 uint64_t stat
= ntohll(get_32aligned_be64(statp
));
1298 ds_put_cstr(string
, leader
);
1299 if (stat
!= UINT64_MAX
) {
1300 ds_put_format(string
, "%"PRIu64
, stat
);
1302 ds_put_char(string
, '?');
1305 ds_put_cstr(string
, ", ");
1307 ds_put_cstr(string
, "\n");
1312 ofp_print_ofpst_port_request(struct ds
*string
,
1313 const struct ofp_port_stats_request
*psr
)
1315 ds_put_format(string
, " port_no=%"PRIu16
, ntohs(psr
->port_no
));
1319 ofp_print_ofpst_port_reply(struct ds
*string
, const struct ofp_header
*oh
,
1322 const struct ofp_port_stats
*ps
= ofputil_stats_body(oh
);
1323 size_t n
= ofputil_stats_body_len(oh
) / sizeof *ps
;
1324 ds_put_format(string
, " %zu ports\n", n
);
1325 if (verbosity
< 1) {
1330 ds_put_format(string
, " port %2"PRIu16
": ", ntohs(ps
->port_no
));
1332 ds_put_cstr(string
, "rx ");
1333 print_port_stat(string
, "pkts=", &ps
->rx_packets
, 1);
1334 print_port_stat(string
, "bytes=", &ps
->rx_bytes
, 1);
1335 print_port_stat(string
, "drop=", &ps
->rx_dropped
, 1);
1336 print_port_stat(string
, "errs=", &ps
->rx_errors
, 1);
1337 print_port_stat(string
, "frame=", &ps
->rx_frame_err
, 1);
1338 print_port_stat(string
, "over=", &ps
->rx_over_err
, 1);
1339 print_port_stat(string
, "crc=", &ps
->rx_crc_err
, 0);
1341 ds_put_cstr(string
, " tx ");
1342 print_port_stat(string
, "pkts=", &ps
->tx_packets
, 1);
1343 print_port_stat(string
, "bytes=", &ps
->tx_bytes
, 1);
1344 print_port_stat(string
, "drop=", &ps
->tx_dropped
, 1);
1345 print_port_stat(string
, "errs=", &ps
->tx_errors
, 1);
1346 print_port_stat(string
, "coll=", &ps
->collisions
, 0);
1351 ofp_print_ofpst_table_reply(struct ds
*string
, const struct ofp_header
*oh
,
1354 const struct ofp_table_stats
*ts
= ofputil_stats_body(oh
);
1355 size_t n
= ofputil_stats_body_len(oh
) / sizeof *ts
;
1356 ds_put_format(string
, " %zu tables\n", n
);
1357 if (verbosity
< 1) {
1362 char name
[OFP_MAX_TABLE_NAME_LEN
+ 1];
1363 ovs_strlcpy(name
, ts
->name
, sizeof name
);
1365 ds_put_format(string
, " %d: %-8s: ", ts
->table_id
, name
);
1366 ds_put_format(string
, "wild=0x%05"PRIx32
", ", ntohl(ts
->wildcards
));
1367 ds_put_format(string
, "max=%6"PRIu32
", ", ntohl(ts
->max_entries
));
1368 ds_put_format(string
, "active=%"PRIu32
"\n", ntohl(ts
->active_count
));
1369 ds_put_cstr(string
, " ");
1370 ds_put_format(string
, "lookup=%"PRIu64
", ",
1371 ntohll(get_32aligned_be64(&ts
->lookup_count
)));
1372 ds_put_format(string
, "matched=%"PRIu64
"\n",
1373 ntohll(get_32aligned_be64(&ts
->matched_count
)));
1378 ofp_print_queue_name(struct ds
*string
, uint32_t queue_id
)
1380 if (queue_id
== OFPQ_ALL
) {
1381 ds_put_cstr(string
, "ALL");
1383 ds_put_format(string
, "%"PRIu32
, queue_id
);
1388 ofp_print_ofpst_queue_request(struct ds
*string
,
1389 const struct ofp_queue_stats_request
*qsr
)
1391 ds_put_cstr(string
, "port=");
1392 ofputil_format_port(ntohs(qsr
->port_no
), string
);
1394 ds_put_cstr(string
, " queue=");
1395 ofp_print_queue_name(string
, ntohl(qsr
->queue_id
));
1399 ofp_print_ofpst_queue_reply(struct ds
*string
, const struct ofp_header
*oh
,
1402 const struct ofp_queue_stats
*qs
= ofputil_stats_body(oh
);
1403 size_t n
= ofputil_stats_body_len(oh
) / sizeof *qs
;
1404 ds_put_format(string
, " %zu queues\n", n
);
1405 if (verbosity
< 1) {
1410 ds_put_cstr(string
, " port ");
1411 ofputil_format_port(ntohs(qs
->port_no
), string
);
1412 ds_put_cstr(string
, " queue ");
1413 ofp_print_queue_name(string
, ntohl(qs
->queue_id
));
1414 ds_put_cstr(string
, ": ");
1416 print_port_stat(string
, "bytes=", &qs
->tx_bytes
, 1);
1417 print_port_stat(string
, "pkts=", &qs
->tx_packets
, 1);
1418 print_port_stat(string
, "errors=", &qs
->tx_errors
, 0);
1423 ofp_print_ofpst_port_desc_reply(struct ds
*string
,
1424 const struct ofp_header
*oh
)
1428 ofpbuf_use_const(&b
, oh
, ntohs(oh
->length
));
1429 ofpbuf_pull(&b
, sizeof(struct ofp_stats_msg
));
1430 ds_put_char(string
, '\n');
1431 ofp_print_phy_ports(string
, oh
->version
, &b
);
1435 ofp_print_stats_request(struct ds
*string
, const struct ofp_header
*oh
)
1437 const struct ofp_stats_msg
*srq
= (const struct ofp_stats_msg
*) oh
;
1440 ds_put_format(string
, " ***unknown flags 0x%04"PRIx16
"***",
1446 ofp_print_stats_reply(struct ds
*string
, const struct ofp_header
*oh
)
1448 const struct ofp_stats_msg
*srp
= (const struct ofp_stats_msg
*) oh
;
1451 uint16_t flags
= ntohs(srp
->flags
);
1453 ds_put_cstr(string
, " flags=");
1454 if (flags
& OFPSF_REPLY_MORE
) {
1455 ds_put_cstr(string
, "[more]");
1456 flags
&= ~OFPSF_REPLY_MORE
;
1459 ds_put_format(string
, "[***unknown flags 0x%04"PRIx16
"***]",
1466 ofp_print_echo(struct ds
*string
, const struct ofp_header
*oh
, int verbosity
)
1468 size_t len
= ntohs(oh
->length
);
1470 ds_put_format(string
, " %zu bytes of payload\n", len
- sizeof *oh
);
1471 if (verbosity
> 1) {
1472 ds_put_hex_dump(string
, oh
+ 1, len
- sizeof *oh
, 0, true);
1477 ofp_print_nxt_role_message(struct ds
*string
,
1478 const struct nx_role_request
*nrr
)
1480 unsigned int role
= ntohl(nrr
->role
);
1482 ds_put_cstr(string
, " role=");
1483 if (role
== NX_ROLE_OTHER
) {
1484 ds_put_cstr(string
, "other");
1485 } else if (role
== NX_ROLE_MASTER
) {
1486 ds_put_cstr(string
, "master");
1487 } else if (role
== NX_ROLE_SLAVE
) {
1488 ds_put_cstr(string
, "slave");
1490 ds_put_format(string
, "%u", role
);
1495 ofp_print_nxt_flow_mod_table_id(struct ds
*string
,
1496 const struct nx_flow_mod_table_id
*nfmti
)
1498 ds_put_format(string
, " %s", nfmti
->set
? "enable" : "disable");
1502 ofp_print_nxt_set_flow_format(struct ds
*string
,
1503 const struct nx_set_flow_format
*nsff
)
1505 uint32_t format
= ntohl(nsff
->format
);
1507 ds_put_cstr(string
, " format=");
1508 if (ofputil_nx_flow_format_is_valid(format
)) {
1509 ds_put_cstr(string
, ofputil_nx_flow_format_to_string(format
));
1511 ds_put_format(string
, "%"PRIu32
, format
);
1516 ofp_print_nxt_set_packet_in_format(struct ds
*string
,
1517 const struct nx_set_packet_in_format
*nspf
)
1519 uint32_t format
= ntohl(nspf
->format
);
1521 ds_put_cstr(string
, " format=");
1522 if (ofputil_packet_in_format_is_valid(format
)) {
1523 ds_put_cstr(string
, ofputil_packet_in_format_to_string(format
));
1525 ds_put_format(string
, "%"PRIu32
, format
);
1530 ofp_port_reason_to_string(enum ofp_port_reason reason
)
1545 sprintf(s
, "%d", (int) reason
);
1551 ofp_print_nxt_set_async_config(struct ds
*string
,
1552 const struct nx_async_config
*nac
)
1556 for (i
= 0; i
< 2; i
++) {
1559 ds_put_format(string
, "\n %s:\n", i
== 0 ? "master" : "slave");
1561 ds_put_cstr(string
, " PACKET_IN:");
1562 for (j
= 0; j
< 32; j
++) {
1563 if (nac
->packet_in_mask
[i
] & htonl(1u << j
)) {
1564 ds_put_format(string
, " %s",
1565 ofputil_packet_in_reason_to_string(j
));
1568 if (!nac
->packet_in_mask
[i
]) {
1569 ds_put_cstr(string
, " (off)");
1571 ds_put_char(string
, '\n');
1573 ds_put_cstr(string
, " PORT_STATUS:");
1574 for (j
= 0; j
< 32; j
++) {
1575 if (nac
->port_status_mask
[i
] & htonl(1u << j
)) {
1576 ds_put_format(string
, " %s", ofp_port_reason_to_string(j
));
1579 if (!nac
->port_status_mask
[i
]) {
1580 ds_put_cstr(string
, " (off)");
1582 ds_put_char(string
, '\n');
1584 ds_put_cstr(string
, " FLOW_REMOVED:");
1585 for (j
= 0; j
< 32; j
++) {
1586 if (nac
->flow_removed_mask
[i
] & htonl(1u << j
)) {
1587 ds_put_format(string
, " %s",
1588 ofp_flow_removed_reason_to_string(j
));
1591 if (!nac
->flow_removed_mask
[i
]) {
1592 ds_put_cstr(string
, " (off)");
1594 ds_put_char(string
, '\n');
1599 ofp_print_nxt_set_controller_id(struct ds
*string
,
1600 const struct nx_controller_id
*nci
)
1602 ds_put_format(string
, " id=%"PRIu16
, ntohs(nci
->controller_id
));
1606 ofp_to_string__(const struct ofp_header
*oh
,
1607 const struct ofputil_msg_type
*type
, struct ds
*string
,
1610 enum ofputil_msg_code code
;
1611 const void *msg
= oh
;
1613 ds_put_cstr(string
, ofputil_msg_type_name(type
));
1614 switch (oh
->version
) {
1618 ds_put_cstr(string
, " (OF1.1)");
1621 ds_put_cstr(string
, " (OF1.2)");
1624 ds_put_format(string
, " (OF 0x%02"PRIx8
")", oh
->version
);
1627 ds_put_format(string
, " (xid=0x%"PRIx32
"):", ntohl(oh
->xid
));
1629 code
= ofputil_msg_type_code(type
);
1631 case OFPUTIL_MSG_INVALID
:
1634 case OFPUTIL_OFPT_HELLO
:
1635 ds_put_char(string
, '\n');
1636 ds_put_hex_dump(string
, oh
+ 1, ntohs(oh
->length
) - sizeof *oh
,
1640 case OFPUTIL_OFPT_ERROR
:
1641 ofp_print_error_msg(string
, msg
);
1644 case OFPUTIL_OFPT_ECHO_REQUEST
:
1645 case OFPUTIL_OFPT_ECHO_REPLY
:
1646 ofp_print_echo(string
, oh
, verbosity
);
1649 case OFPUTIL_OFPT_FEATURES_REQUEST
:
1652 case OFPUTIL_OFPT_FEATURES_REPLY
:
1653 ofp_print_switch_features(string
, msg
);
1656 case OFPUTIL_OFPT_GET_CONFIG_REQUEST
:
1659 case OFPUTIL_OFPT_GET_CONFIG_REPLY
:
1660 case OFPUTIL_OFPT_SET_CONFIG
:
1661 ofp_print_switch_config(string
, msg
);
1664 case OFPUTIL_OFPT_PACKET_IN
:
1665 case OFPUTIL_NXT_PACKET_IN
:
1666 ofp_print_packet_in(string
, msg
, verbosity
);
1669 case OFPUTIL_OFPT_FLOW_REMOVED
:
1670 case OFPUTIL_NXT_FLOW_REMOVED
:
1671 ofp_print_flow_removed(string
, msg
);
1674 case OFPUTIL_OFPT_PORT_STATUS
:
1675 ofp_print_port_status(string
, msg
);
1678 case OFPUTIL_OFPT_PACKET_OUT
:
1679 ofp_print_packet_out(string
, msg
, verbosity
);
1682 case OFPUTIL_OFPT_FLOW_MOD
:
1683 case OFPUTIL_NXT_FLOW_MOD
:
1684 ofp_print_flow_mod(string
, msg
, code
, verbosity
);
1687 case OFPUTIL_OFPT_PORT_MOD
:
1688 ofp_print_port_mod(string
, msg
);
1691 case OFPUTIL_OFPT_BARRIER_REQUEST
:
1692 case OFPUTIL_OFPT_BARRIER_REPLY
:
1695 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST
:
1696 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY
:
1700 case OFPUTIL_OFPST_DESC_REQUEST
:
1701 case OFPUTIL_OFPST_PORT_DESC_REQUEST
:
1702 ofp_print_stats_request(string
, oh
);
1705 case OFPUTIL_OFPST_FLOW_REQUEST
:
1706 case OFPUTIL_NXST_FLOW_REQUEST
:
1707 case OFPUTIL_OFPST_AGGREGATE_REQUEST
:
1708 case OFPUTIL_NXST_AGGREGATE_REQUEST
:
1709 ofp_print_stats_request(string
, oh
);
1710 ofp_print_flow_stats_request(string
, msg
);
1713 case OFPUTIL_OFPST_TABLE_REQUEST
:
1714 ofp_print_stats_request(string
, oh
);
1717 case OFPUTIL_OFPST_PORT_REQUEST
:
1718 ofp_print_stats_request(string
, oh
);
1719 ofp_print_ofpst_port_request(string
, msg
);
1722 case OFPUTIL_OFPST_QUEUE_REQUEST
:
1723 ofp_print_stats_request(string
, oh
);
1724 ofp_print_ofpst_queue_request(string
, msg
);
1727 case OFPUTIL_OFPST_DESC_REPLY
:
1728 ofp_print_stats_reply(string
, oh
);
1729 ofp_print_ofpst_desc_reply(string
, msg
);
1732 case OFPUTIL_OFPST_FLOW_REPLY
:
1733 case OFPUTIL_NXST_FLOW_REPLY
:
1734 ofp_print_stats_reply(string
, oh
);
1735 ofp_print_flow_stats_reply(string
, oh
);
1738 case OFPUTIL_OFPST_QUEUE_REPLY
:
1739 ofp_print_stats_reply(string
, oh
);
1740 ofp_print_ofpst_queue_reply(string
, oh
, verbosity
);
1743 case OFPUTIL_OFPST_PORT_REPLY
:
1744 ofp_print_stats_reply(string
, oh
);
1745 ofp_print_ofpst_port_reply(string
, oh
, verbosity
);
1748 case OFPUTIL_OFPST_TABLE_REPLY
:
1749 ofp_print_stats_reply(string
, oh
);
1750 ofp_print_ofpst_table_reply(string
, oh
, verbosity
);
1753 case OFPUTIL_OFPST_AGGREGATE_REPLY
:
1754 ofp_print_stats_reply(string
, oh
);
1755 ofp_print_ofpst_aggregate_reply(string
, msg
);
1758 case OFPUTIL_OFPST_PORT_DESC_REPLY
:
1759 ofp_print_stats_reply(string
, oh
);
1760 ofp_print_ofpst_port_desc_reply(string
, oh
);
1763 case OFPUTIL_NXT_ROLE_REQUEST
:
1764 case OFPUTIL_NXT_ROLE_REPLY
:
1765 ofp_print_nxt_role_message(string
, msg
);
1768 case OFPUTIL_NXT_FLOW_MOD_TABLE_ID
:
1769 ofp_print_nxt_flow_mod_table_id(string
, msg
);
1772 case OFPUTIL_NXT_SET_FLOW_FORMAT
:
1773 ofp_print_nxt_set_flow_format(string
, msg
);
1776 case OFPUTIL_NXT_SET_PACKET_IN_FORMAT
:
1777 ofp_print_nxt_set_packet_in_format(string
, msg
);
1780 case OFPUTIL_NXT_FLOW_AGE
:
1783 case OFPUTIL_NXT_SET_CONTROLLER_ID
:
1784 ofp_print_nxt_set_controller_id(string
, msg
);
1787 case OFPUTIL_NXT_SET_ASYNC_CONFIG
:
1788 ofp_print_nxt_set_async_config(string
, msg
);
1791 case OFPUTIL_NXST_AGGREGATE_REPLY
:
1792 ofp_print_stats_reply(string
, oh
);
1793 ofp_print_nxst_aggregate_reply(string
, msg
);
1798 /* Composes and returns a string representing the OpenFlow packet of 'len'
1799 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1800 * verbosity and higher numbers increase verbosity. The caller is responsible
1801 * for freeing the string. */
1803 ofp_to_string(const void *oh_
, size_t len
, int verbosity
)
1805 struct ds string
= DS_EMPTY_INITIALIZER
;
1806 const struct ofp_header
*oh
= oh_
;
1809 ds_put_cstr(&string
, "OpenFlow message is empty\n");
1810 } else if (len
< sizeof(struct ofp_header
)) {
1811 ds_put_format(&string
, "OpenFlow packet too short (only %zu bytes):\n",
1813 } else if (ntohs(oh
->length
) > len
) {
1814 ds_put_format(&string
,
1815 "(***truncated to %zu bytes from %"PRIu16
"***)\n",
1816 len
, ntohs(oh
->length
));
1817 } else if (ntohs(oh
->length
) < len
) {
1818 ds_put_format(&string
,
1819 "(***only uses %"PRIu16
" bytes out of %zu***)\n",
1820 ntohs(oh
->length
), len
);
1822 const struct ofputil_msg_type
*type
;
1825 error
= ofputil_decode_msg_type(oh
, &type
);
1827 ofp_to_string__(oh
, type
, &string
, verbosity
);
1828 if (verbosity
>= 5) {
1829 if (ds_last(&string
) != '\n') {
1830 ds_put_char(&string
, '\n');
1832 ds_put_hex_dump(&string
, oh
, len
, 0, true);
1834 if (ds_last(&string
) != '\n') {
1835 ds_put_char(&string
, '\n');
1837 return ds_steal_cstr(&string
);
1840 ofp_print_error(&string
, error
);
1842 ds_put_hex_dump(&string
, oh
, len
, 0, true);
1843 return ds_steal_cstr(&string
);
1847 print_and_free(FILE *stream
, char *string
)
1849 fputs(string
, stream
);
1853 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1854 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1855 * numbers increase verbosity. */
1857 ofp_print(FILE *stream
, const void *oh
, size_t len
, int verbosity
)
1859 print_and_free(stream
, ofp_to_string(oh
, len
, verbosity
));
1862 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1863 * 'data' to 'stream'. */
1865 ofp_print_packet(FILE *stream
, const void *data
, size_t len
)
1867 print_and_free(stream
, ofp_packet_to_string(data
, len
));