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"
43 #include "openflow/openflow.h"
44 #include "openflow/nicira-ext.h"
47 #include "type-props.h"
48 #include "unaligned.h"
51 static void ofp_print_queue_name(struct ds *string, uint32_t port);
52 static void ofp_print_error(struct ds *, enum ofperr);
55 /* Returns a string that represents the contents of the Ethernet frame in the
56 * 'len' bytes starting at 'data'. The caller must free the returned string.*/
58 ofp_packet_to_string(const void *data, size_t len)
60 struct ds ds = DS_EMPTY_INITIALIZER;
64 ofpbuf_use_const(&buf, data, len);
65 flow_extract(&buf, 0, 0, 0, &flow);
66 flow_format(&ds, &flow);
69 if (flow.nw_proto == IPPROTO_TCP) {
70 struct tcp_header *th = buf.l4;
71 ds_put_format(&ds, " tcp_csum:%"PRIx16,
73 } else if (flow.nw_proto == IPPROTO_UDP) {
74 struct udp_header *uh = buf.l4;
75 ds_put_format(&ds, " udp_csum:%"PRIx16,
80 ds_put_char(&ds, '\n');
86 ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
89 struct ofputil_packet_in pin;
93 error = ofputil_decode_packet_in(&pin, oh);
95 ofp_print_error(string, error);
100 ds_put_format(string, " table_id=%"PRIu8, pin.table_id);
104 ds_put_format(string, " cookie=0x%"PRIx64, ntohll(pin.cookie));
107 ds_put_format(string, " total_len=%"PRIu16" in_port=", pin.total_len);
108 ofputil_format_port(pin.fmd.in_port, string);
110 if (pin.fmd.tun_id_mask) {
111 ds_put_format(string, " tun_id=0x%"PRIx64, ntohll(pin.fmd.tun_id));
112 if (pin.fmd.tun_id_mask != htonll(UINT64_MAX)) {
113 ds_put_format(string, "/0x%"PRIx64, ntohll(pin.fmd.tun_id_mask));
117 if (pin.fmd.metadata_mask) {
118 ds_put_format(string, " metadata=0x%"PRIx64, ntohll(pin.fmd.metadata));
119 if (pin.fmd.metadata_mask != htonll(UINT64_MAX)) {
120 ds_put_format(string, "/0x%"PRIx64, ntohll(pin.fmd.metadata_mask));
124 for (i = 0; i < FLOW_N_REGS; i++) {
125 if (pin.fmd.reg_masks[i]) {
126 ds_put_format(string, " reg%d=0x%"PRIx32, i, pin.fmd.regs[i]);
127 if (pin.fmd.reg_masks[i] != UINT32_MAX) {
128 ds_put_format(string, "/0x%"PRIx32, pin.fmd.reg_masks[i]);
133 ds_put_format(string, " (via %s)",
134 ofputil_packet_in_reason_to_string(pin.reason));
136 ds_put_format(string, " data_len=%zu", pin.packet_len);
137 if (pin.buffer_id == UINT32_MAX) {
138 ds_put_format(string, " (unbuffered)");
139 if (pin.total_len != pin.packet_len) {
140 ds_put_format(string, " (***total_len != data_len***)");
143 ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
144 if (pin.total_len < pin.packet_len) {
145 ds_put_format(string, " (***total_len < data_len***)");
148 ds_put_char(string, '\n');
151 char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
152 ds_put_cstr(string, packet);
158 ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
161 struct ofputil_packet_out po;
162 struct ofpbuf ofpacts;
165 ofpbuf_init(&ofpacts, 64);
166 error = ofputil_decode_packet_out(&po, opo, &ofpacts);
168 ofpbuf_uninit(&ofpacts);
169 ofp_print_error(string, error);
173 ds_put_cstr(string, " in_port=");
174 ofputil_format_port(po.in_port, string);
176 ds_put_char(string, ' ');
177 ofpacts_format(po.ofpacts, po.ofpacts_len, string);
179 if (po.buffer_id == UINT32_MAX) {
180 ds_put_format(string, " data_len=%zu", po.packet_len);
181 if (verbosity > 0 && po.packet_len > 0) {
182 char *packet = ofp_packet_to_string(po.packet, po.packet_len);
183 ds_put_char(string, '\n');
184 ds_put_cstr(string, packet);
188 ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id);
190 ds_put_char(string, '\n');
192 ofpbuf_uninit(&ofpacts);
195 /* qsort comparison function. */
197 compare_ports(const void *a_, const void *b_)
199 const struct ofputil_phy_port *a = a_;
200 const struct ofputil_phy_port *b = b_;
201 uint16_t ap = a->port_no;
202 uint16_t bp = b->port_no;
204 return ap < bp ? -1 : ap > bp;
208 ofp_print_bit_names(struct ds *string, uint32_t bits,
209 const char *(*bit_to_name)(uint32_t bit))
215 ds_put_cstr(string, "0");
219 for (i = 0; i < 32; i++) {
220 uint32_t bit = UINT32_C(1) << i;
223 const char *name = bit_to_name(bit);
226 ds_put_char(string, ' ');
228 ds_put_cstr(string, name);
236 ds_put_char(string, ' ');
238 ds_put_format(string, "0x%"PRIx32, bits);
243 netdev_feature_to_name(uint32_t bit)
245 enum netdev_features f = bit;
248 case NETDEV_F_10MB_HD: return "10MB-HD";
249 case NETDEV_F_10MB_FD: return "10MB-FD";
250 case NETDEV_F_100MB_HD: return "100MB-HD";
251 case NETDEV_F_100MB_FD: return "100MB-FD";
252 case NETDEV_F_1GB_HD: return "1GB-HD";
253 case NETDEV_F_1GB_FD: return "1GB-FD";
254 case NETDEV_F_10GB_FD: return "10GB-FD";
255 case NETDEV_F_40GB_FD: return "40GB-FD";
256 case NETDEV_F_100GB_FD: return "100GB-FD";
257 case NETDEV_F_1TB_FD: return "1TB-FD";
258 case NETDEV_F_OTHER: return "OTHER";
259 case NETDEV_F_COPPER: return "COPPER";
260 case NETDEV_F_FIBER: return "FIBER";
261 case NETDEV_F_AUTONEG: return "AUTO_NEG";
262 case NETDEV_F_PAUSE: return "AUTO_PAUSE";
263 case NETDEV_F_PAUSE_ASYM: return "AUTO_PAUSE_ASYM";
270 ofp_print_port_features(struct ds *string, enum netdev_features features)
272 ofp_print_bit_names(string, features, netdev_feature_to_name);
273 ds_put_char(string, '\n');
277 ofputil_port_config_to_name(uint32_t bit)
279 enum ofputil_port_config pc = bit;
282 case OFPUTIL_PC_PORT_DOWN: return "PORT_DOWN";
283 case OFPUTIL_PC_NO_STP: return "NO_STP";
284 case OFPUTIL_PC_NO_RECV: return "NO_RECV";
285 case OFPUTIL_PC_NO_RECV_STP: return "NO_RECV_STP";
286 case OFPUTIL_PC_NO_FLOOD: return "NO_FLOOD";
287 case OFPUTIL_PC_NO_FWD: return "NO_FWD";
288 case OFPUTIL_PC_NO_PACKET_IN: return "NO_PACKET_IN";
295 ofp_print_port_config(struct ds *string, enum ofputil_port_config config)
297 ofp_print_bit_names(string, config, ofputil_port_config_to_name);
298 ds_put_char(string, '\n');
302 ofputil_port_state_to_name(uint32_t bit)
304 enum ofputil_port_state ps = bit;
307 case OFPUTIL_PS_LINK_DOWN: return "LINK_DOWN";
308 case OFPUTIL_PS_BLOCKED: return "BLOCKED";
309 case OFPUTIL_PS_LIVE: return "LIVE";
311 case OFPUTIL_PS_STP_LISTEN:
312 case OFPUTIL_PS_STP_LEARN:
313 case OFPUTIL_PS_STP_FORWARD:
314 case OFPUTIL_PS_STP_BLOCK:
315 /* Handled elsewhere. */
323 ofp_print_port_state(struct ds *string, enum ofputil_port_state state)
325 enum ofputil_port_state stp_state;
327 /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
328 * pattern. We have to special case it.
330 * OVS doesn't support STP, so this field will always be 0 if we are
331 * talking to OVS, so we'd always print STP_LISTEN in that case.
332 * Therefore, we don't print anything at all if the value is STP_LISTEN, to
333 * avoid confusing users. */
334 stp_state = state & OFPUTIL_PS_STP_MASK;
337 (stp_state == OFPUTIL_PS_STP_LEARN ? "STP_LEARN"
338 : stp_state == OFPUTIL_PS_STP_FORWARD ? "STP_FORWARD"
340 state &= ~OFPUTIL_PS_STP_MASK;
342 ofp_print_bit_names(string, state, ofputil_port_state_to_name);
345 ofp_print_bit_names(string, state, ofputil_port_state_to_name);
347 ds_put_char(string, '\n');
351 ofp_print_phy_port(struct ds *string, const struct ofputil_phy_port *port)
353 char name[sizeof port->name];
356 memcpy(name, port->name, sizeof name);
357 for (j = 0; j < sizeof name - 1; j++) {
358 if (!isprint((unsigned char) name[j])) {
364 ds_put_char(string, ' ');
365 ofputil_format_port(port->port_no, string);
366 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
367 name, ETH_ADDR_ARGS(port->hw_addr));
369 ds_put_cstr(string, " config: ");
370 ofp_print_port_config(string, port->config);
372 ds_put_cstr(string, " state: ");
373 ofp_print_port_state(string, port->state);
376 ds_put_format(string, " current: ");
377 ofp_print_port_features(string, port->curr);
379 if (port->advertised) {
380 ds_put_format(string, " advertised: ");
381 ofp_print_port_features(string, port->advertised);
383 if (port->supported) {
384 ds_put_format(string, " supported: ");
385 ofp_print_port_features(string, port->supported);
388 ds_put_format(string, " peer: ");
389 ofp_print_port_features(string, port->peer);
391 ds_put_format(string, " speed: %"PRIu32" Mbps now, "
392 "%"PRIu32" Mbps max\n",
393 port->curr_speed / UINT32_C(1000),
394 port->max_speed / UINT32_C(1000));
397 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
398 * 'ofp_version', writes a detailed description of each port into
401 ofp_print_phy_ports(struct ds *string, uint8_t ofp_version,
405 struct ofputil_phy_port *ports;
409 n_ports = ofputil_count_phy_ports(ofp_version, b);
411 ports = xmalloc(n_ports * sizeof *ports);
412 for (i = 0; i < n_ports; i++) {
413 error = ofputil_pull_phy_port(ofp_version, b, &ports[i]);
415 ofp_print_error(string, error);
419 qsort(ports, n_ports, sizeof *ports, compare_ports);
420 for (i = 0; i < n_ports; i++) {
421 ofp_print_phy_port(string, &ports[i]);
429 ofputil_capabilities_to_name(uint32_t bit)
431 enum ofputil_capabilities capabilities = bit;
433 switch (capabilities) {
434 case OFPUTIL_C_FLOW_STATS: return "FLOW_STATS";
435 case OFPUTIL_C_TABLE_STATS: return "TABLE_STATS";
436 case OFPUTIL_C_PORT_STATS: return "PORT_STATS";
437 case OFPUTIL_C_IP_REASM: return "IP_REASM";
438 case OFPUTIL_C_QUEUE_STATS: return "QUEUE_STATS";
439 case OFPUTIL_C_ARP_MATCH_IP: return "ARP_MATCH_IP";
440 case OFPUTIL_C_STP: return "STP";
441 case OFPUTIL_C_GROUP_STATS: return "GROUP_STATS";
448 ofputil_action_bitmap_to_name(uint32_t bit)
450 enum ofputil_action_bitmap action = bit;
453 case OFPUTIL_A_OUTPUT: return "OUTPUT";
454 case OFPUTIL_A_SET_VLAN_VID: return "SET_VLAN_VID";
455 case OFPUTIL_A_SET_VLAN_PCP: return "SET_VLAN_PCP";
456 case OFPUTIL_A_STRIP_VLAN: return "STRIP_VLAN";
457 case OFPUTIL_A_SET_DL_SRC: return "SET_DL_SRC";
458 case OFPUTIL_A_SET_DL_DST: return "SET_DL_DST";
459 case OFPUTIL_A_SET_NW_SRC: return "SET_NW_SRC";
460 case OFPUTIL_A_SET_NW_DST: return "SET_NW_DST";
461 case OFPUTIL_A_SET_NW_ECN: return "SET_NW_ECN";
462 case OFPUTIL_A_SET_NW_TOS: return "SET_NW_TOS";
463 case OFPUTIL_A_SET_TP_SRC: return "SET_TP_SRC";
464 case OFPUTIL_A_SET_TP_DST: return "SET_TP_DST";
465 case OFPUTIL_A_ENQUEUE: return "ENQUEUE";
466 case OFPUTIL_A_COPY_TTL_OUT: return "COPY_TTL_OUT";
467 case OFPUTIL_A_COPY_TTL_IN: return "COPY_TTL_IN";
468 case OFPUTIL_A_SET_MPLS_LABEL: return "SET_MPLS_LABEL";
469 case OFPUTIL_A_SET_MPLS_TC: return "SET_MPLS_TC";
470 case OFPUTIL_A_SET_MPLS_TTL: return "SET_MPLS_TTL";
471 case OFPUTIL_A_DEC_MPLS_TTL: return "DEC_MPLS_TTL";
472 case OFPUTIL_A_PUSH_VLAN: return "PUSH_VLAN";
473 case OFPUTIL_A_POP_VLAN: return "POP_VLAN";
474 case OFPUTIL_A_PUSH_MPLS: return "PUSH_MPLS";
475 case OFPUTIL_A_POP_MPLS: return "POP_MPLS";
476 case OFPUTIL_A_SET_QUEUE: return "SET_QUEUE";
477 case OFPUTIL_A_GROUP: return "GROUP";
478 case OFPUTIL_A_SET_NW_TTL: return "SET_NW_TTL";
479 case OFPUTIL_A_DEC_NW_TTL: return "DEC_NW_TTL";
486 ofp_print_switch_features(struct ds *string,
487 const struct ofp_switch_features *osf)
489 struct ofputil_switch_features features;
493 error = ofputil_decode_switch_features(osf, &features, &b);
495 ofp_print_error(string, error);
499 ds_put_format(string, " dpid:%016"PRIx64"\n", features.datapath_id);
500 ds_put_format(string, "n_tables:%"PRIu8", n_buffers:%"PRIu32"\n",
501 features.n_tables, features.n_buffers);
503 ds_put_cstr(string, "capabilities: ");
504 ofp_print_bit_names(string, features.capabilities,
505 ofputil_capabilities_to_name);
506 ds_put_char(string, '\n');
508 ds_put_cstr(string, "actions: ");
509 ofp_print_bit_names(string, features.actions,
510 ofputil_action_bitmap_to_name);
511 ds_put_char(string, '\n');
513 ofp_print_phy_ports(string, osf->header.version, &b);
517 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
519 enum ofp_config_flags flags;
521 flags = ntohs(osc->flags);
523 ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
524 flags &= ~OFPC_FRAG_MASK;
526 if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) {
527 ds_put_format(string, " invalid_ttl_to_controller");
528 flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER;
532 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
535 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
538 static void print_wild(struct ds *string, const char *leader, int is_wild,
539 int verbosity, const char *format, ...)
540 __attribute__((format(printf, 5, 6)));
542 static void print_wild(struct ds *string, const char *leader, int is_wild,
543 int verbosity, const char *format, ...)
545 if (is_wild && verbosity < 2) {
548 ds_put_cstr(string, leader);
552 va_start(args, format);
553 ds_put_format_valist(string, format, args);
556 ds_put_char(string, '*');
558 ds_put_char(string, ',');
562 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
563 uint32_t wild_bits, int verbosity)
565 if (wild_bits >= 32 && verbosity < 2) {
568 ds_put_cstr(string, leader);
569 if (wild_bits < 32) {
570 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
572 ds_put_format(string, "/%d", 32 - wild_bits);
575 ds_put_char(string, '*');
577 ds_put_char(string, ',');
581 ofp10_match_print(struct ds *f, const struct ofp10_match *om, int verbosity)
583 char *s = ofp10_match_to_string(om, verbosity);
589 ofp10_match_to_string(const struct ofp10_match *om, int verbosity)
591 struct ds f = DS_EMPTY_INITIALIZER;
592 uint32_t w = ntohl(om->wildcards);
593 bool skip_type = false;
594 bool skip_proto = false;
596 if (!(w & OFPFW10_DL_TYPE)) {
598 if (om->dl_type == htons(ETH_TYPE_IP)) {
599 if (!(w & OFPFW10_NW_PROTO)) {
601 if (om->nw_proto == IPPROTO_ICMP) {
602 ds_put_cstr(&f, "icmp,");
603 } else if (om->nw_proto == IPPROTO_TCP) {
604 ds_put_cstr(&f, "tcp,");
605 } else if (om->nw_proto == IPPROTO_UDP) {
606 ds_put_cstr(&f, "udp,");
608 ds_put_cstr(&f, "ip,");
612 ds_put_cstr(&f, "ip,");
614 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
615 ds_put_cstr(&f, "arp,");
620 print_wild(&f, "in_port=", w & OFPFW10_IN_PORT, verbosity,
621 "%d", ntohs(om->in_port));
622 print_wild(&f, "dl_vlan=", w & OFPFW10_DL_VLAN, verbosity,
623 "%d", ntohs(om->dl_vlan));
624 print_wild(&f, "dl_vlan_pcp=", w & OFPFW10_DL_VLAN_PCP, verbosity,
625 "%d", om->dl_vlan_pcp);
626 print_wild(&f, "dl_src=", w & OFPFW10_DL_SRC, verbosity,
627 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
628 print_wild(&f, "dl_dst=", w & OFPFW10_DL_DST, verbosity,
629 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
631 print_wild(&f, "dl_type=", w & OFPFW10_DL_TYPE, verbosity,
632 "0x%04x", ntohs(om->dl_type));
634 print_ip_netmask(&f, "nw_src=", om->nw_src,
635 (w & OFPFW10_NW_SRC_MASK) >> OFPFW10_NW_SRC_SHIFT,
637 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
638 (w & OFPFW10_NW_DST_MASK) >> OFPFW10_NW_DST_SHIFT,
641 if (om->dl_type == htons(ETH_TYPE_ARP)) {
642 print_wild(&f, "arp_op=", w & OFPFW10_NW_PROTO, verbosity,
645 print_wild(&f, "nw_proto=", w & OFPFW10_NW_PROTO, verbosity,
649 print_wild(&f, "nw_tos=", w & OFPFW10_NW_TOS, verbosity,
651 if (om->nw_proto == IPPROTO_ICMP) {
652 print_wild(&f, "icmp_type=", w & OFPFW10_ICMP_TYPE, verbosity,
653 "%d", ntohs(om->tp_src));
654 print_wild(&f, "icmp_code=", w & OFPFW10_ICMP_CODE, verbosity,
655 "%d", ntohs(om->tp_dst));
657 print_wild(&f, "tp_src=", w & OFPFW10_TP_SRC, verbosity,
658 "%d", ntohs(om->tp_src));
659 print_wild(&f, "tp_dst=", w & OFPFW10_TP_DST, verbosity,
660 "%d", ntohs(om->tp_dst));
662 if (ds_last(&f) == ',') {
669 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
670 enum ofputil_msg_code code, int verbosity)
672 struct ofputil_flow_mod fm;
673 struct ofpbuf ofpacts;
677 ofpbuf_init(&ofpacts, 64);
678 error = ofputil_decode_flow_mod(&fm, oh, OFPUTIL_P_OF10_TID, &ofpacts);
680 ofpbuf_uninit(&ofpacts);
681 ofp_print_error(s, error);
686 switch (fm.command) {
688 ds_put_cstr(s, "ADD");
691 ds_put_cstr(s, "MOD");
693 case OFPFC_MODIFY_STRICT:
694 ds_put_cstr(s, "MOD_STRICT");
697 ds_put_cstr(s, "DEL");
699 case OFPFC_DELETE_STRICT:
700 ds_put_cstr(s, "DEL_STRICT");
703 ds_put_format(s, "cmd:%d", fm.command);
705 if (fm.table_id != 0) {
706 ds_put_format(s, " table:%d", fm.table_id);
710 if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
711 const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
712 ofp10_match_print(s, &ofm->match, verbosity);
714 /* ofp_print_match() doesn't print priority. */
715 need_priority = true;
716 } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
717 const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
718 const void *nxm = nfm + 1;
721 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
722 ds_put_cstr(s, nxm_s);
725 /* nx_match_to_string() doesn't print priority. */
726 need_priority = true;
728 cls_rule_format(&fm.cr, s);
730 /* cls_rule_format() does print priority. */
731 need_priority = false;
734 if (ds_last(s) != ' ') {
737 if (fm.new_cookie != htonll(0)) {
738 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.new_cookie));
740 if (fm.cookie_mask != htonll(0)) {
741 ds_put_format(s, "cookie:0x%"PRIx64"/0x%"PRIx64" ",
742 ntohll(fm.cookie), ntohll(fm.cookie_mask));
744 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
745 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
747 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
748 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
750 if (fm.cr.priority != OFP_DEFAULT_PRIORITY && need_priority) {
751 ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
753 if (fm.buffer_id != UINT32_MAX) {
754 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
756 if (fm.out_port != OFPP_NONE) {
757 ds_put_format(s, "out_port:");
758 ofputil_format_port(fm.out_port, s);
762 uint16_t flags = fm.flags;
764 if (flags & OFPFF_SEND_FLOW_REM) {
765 ds_put_cstr(s, "send_flow_rem ");
767 if (flags & OFPFF_CHECK_OVERLAP) {
768 ds_put_cstr(s, "check_overlap ");
770 if (flags & OFPFF_EMERG) {
771 ds_put_cstr(s, "emerg ");
774 flags &= ~(OFPFF_SEND_FLOW_REM | OFPFF_CHECK_OVERLAP | OFPFF_EMERG);
776 ds_put_format(s, "flags:0x%"PRIx16" ", flags);
780 ofpacts_format(fm.ofpacts, fm.ofpacts_len, s);
781 ofpbuf_uninit(&ofpacts);
785 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
787 ds_put_format(string, "%u", sec);
789 ds_put_format(string, ".%09u", nsec);
790 while (string->string[string->length - 1] == '0') {
794 ds_put_char(string, 's');
798 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason)
803 case OFPRR_IDLE_TIMEOUT:
805 case OFPRR_HARD_TIMEOUT:
809 case OFPRR_GROUP_DELETE:
810 return "group_delete";
812 sprintf(s, "%d", (int) reason);
818 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
820 struct ofputil_flow_removed fr;
823 error = ofputil_decode_flow_removed(&fr, oh);
825 ofp_print_error(string, error);
829 ds_put_char(string, ' ');
830 cls_rule_format(&fr.rule, string);
832 ds_put_format(string, " reason=%s",
833 ofp_flow_removed_reason_to_string(fr.reason));
835 if (fr.cookie != htonll(0)) {
836 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
838 ds_put_cstr(string, " duration");
839 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
840 ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
841 fr.idle_timeout, fr.packet_count, fr.byte_count);
845 ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
847 struct ofputil_port_mod pm;
850 error = ofputil_decode_port_mod(oh, &pm);
852 ofp_print_error(string, error);
856 ds_put_format(string, "port: %"PRIu16": addr:"ETH_ADDR_FMT"\n",
857 pm.port_no, ETH_ADDR_ARGS(pm.hw_addr));
859 ds_put_format(string, " config: ");
860 ofp_print_port_config(string, pm.config);
862 ds_put_format(string, " mask: ");
863 ofp_print_port_config(string, pm.mask);
865 ds_put_format(string, " advertise: ");
867 ofp_print_port_features(string, pm.advertise);
869 ds_put_format(string, "UNCHANGED\n");
874 ofp_print_error(struct ds *string, enum ofperr error)
876 if (string->length) {
877 ds_put_char(string, ' ');
879 ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
883 ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
885 size_t len = ntohs(oem->header.length);
886 size_t payload_ofs, payload_len;
891 error = ofperr_decode_msg(&oem->header, &payload_ofs);
893 ds_put_cstr(string, "***decode error***");
894 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
898 ds_put_format(string, " %s\n", ofperr_get_name(error));
900 payload = (const uint8_t *) oem + payload_ofs;
901 payload_len = len - payload_ofs;
902 if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
903 ds_put_printable(string, payload, payload_len);
905 s = ofp_to_string(payload, payload_len, 1);
906 ds_put_cstr(string, s);
912 ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
914 struct ofputil_port_status ps;
917 error = ofputil_decode_port_status(ops, &ps);
919 ofp_print_error(string, error);
923 if (ps.reason == OFPPR_ADD) {
924 ds_put_format(string, " ADD:");
925 } else if (ps.reason == OFPPR_DELETE) {
926 ds_put_format(string, " DEL:");
927 } else if (ps.reason == OFPPR_MODIFY) {
928 ds_put_format(string, " MOD:");
931 ofp_print_phy_port(string, &ps.desc);
935 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_desc_stats *ods)
937 ds_put_char(string, '\n');
938 ds_put_format(string, "Manufacturer: %.*s\n",
939 (int) sizeof ods->mfr_desc, ods->mfr_desc);
940 ds_put_format(string, "Hardware: %.*s\n",
941 (int) sizeof ods->hw_desc, ods->hw_desc);
942 ds_put_format(string, "Software: %.*s\n",
943 (int) sizeof ods->sw_desc, ods->sw_desc);
944 ds_put_format(string, "Serial Num: %.*s\n",
945 (int) sizeof ods->serial_num, ods->serial_num);
946 ds_put_format(string, "DP Description: %.*s\n",
947 (int) sizeof ods->dp_desc, ods->dp_desc);
951 ofp_print_flow_stats_request(struct ds *string,
952 const struct ofp_stats_msg *osm)
954 struct ofputil_flow_stats_request fsr;
957 error = ofputil_decode_flow_stats_request(&fsr, &osm->header);
959 ofp_print_error(string, error);
963 if (fsr.table_id != 0xff) {
964 ds_put_format(string, " table=%"PRIu8, fsr.table_id);
967 if (fsr.out_port != OFPP_NONE) {
968 ds_put_cstr(string, " out_port=");
969 ofputil_format_port(fsr.out_port, string);
972 /* A flow stats request doesn't include a priority, but cls_rule_format()
973 * will print one unless it is OFP_DEFAULT_PRIORITY. */
974 fsr.match.priority = OFP_DEFAULT_PRIORITY;
976 ds_put_char(string, ' ');
977 cls_rule_format(&fsr.match, string);
981 ofp_print_flow_stats(struct ds *string, struct ofputil_flow_stats *fs)
983 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
986 ofp_print_duration(string, fs->duration_sec, fs->duration_nsec);
987 ds_put_format(string, ", table=%"PRIu8", ", fs->table_id);
988 ds_put_format(string, "n_packets=%"PRIu64", ", fs->packet_count);
989 ds_put_format(string, "n_bytes=%"PRIu64", ", fs->byte_count);
990 if (fs->idle_timeout != OFP_FLOW_PERMANENT) {
991 ds_put_format(string, "idle_timeout=%"PRIu16", ", fs->idle_timeout);
993 if (fs->hard_timeout != OFP_FLOW_PERMANENT) {
994 ds_put_format(string, "hard_timeout=%"PRIu16", ", fs->hard_timeout);
996 if (fs->idle_age >= 0) {
997 ds_put_format(string, "idle_age=%d, ", fs->idle_age);
999 if (fs->hard_age >= 0 && fs->hard_age != fs->duration_sec) {
1000 ds_put_format(string, "hard_age=%d, ", fs->hard_age);
1003 cls_rule_format(&fs->rule, string);
1004 if (string->string[string->length - 1] != ' ') {
1005 ds_put_char(string, ' ');
1008 ofpacts_format(fs->ofpacts, fs->ofpacts_len, string);
1012 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1014 struct ofpbuf ofpacts;
1017 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1018 ofpbuf_init(&ofpacts, 64);
1020 struct ofputil_flow_stats fs;
1023 retval = ofputil_decode_flow_stats_reply(&fs, &b, true, &ofpacts);
1025 if (retval != EOF) {
1026 ds_put_cstr(string, " ***parse error***");
1030 ds_put_char(string, '\n');
1031 ofp_print_flow_stats(string, &fs);
1036 ofp_print_ofpst_aggregate_reply(struct ds *string,
1037 const struct ofp_aggregate_stats_reply *asr)
1039 ds_put_format(string, " packet_count=%"PRIu64,
1040 ntohll(get_32aligned_be64(&asr->packet_count)));
1041 ds_put_format(string, " byte_count=%"PRIu64,
1042 ntohll(get_32aligned_be64(&asr->byte_count)));
1043 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1047 ofp_print_nxst_aggregate_reply(struct ds *string,
1048 const struct nx_aggregate_stats_reply *nasr)
1050 ds_put_format(string, " packet_count=%"PRIu64, ntohll(nasr->packet_count));
1051 ds_put_format(string, " byte_count=%"PRIu64, ntohll(nasr->byte_count));
1052 ds_put_format(string, " flow_count=%"PRIu32, ntohl(nasr->flow_count));
1055 static void print_port_stat(struct ds *string, const char *leader,
1056 const ovs_32aligned_be64 *statp, int more)
1058 uint64_t stat = ntohll(get_32aligned_be64(statp));
1060 ds_put_cstr(string, leader);
1061 if (stat != UINT64_MAX) {
1062 ds_put_format(string, "%"PRIu64, stat);
1064 ds_put_char(string, '?');
1067 ds_put_cstr(string, ", ");
1069 ds_put_cstr(string, "\n");
1074 ofp_print_ofpst_port_request(struct ds *string,
1075 const struct ofp_port_stats_request *psr)
1077 ds_put_format(string, " port_no=%"PRIu16, ntohs(psr->port_no));
1081 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1084 const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1085 size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
1086 ds_put_format(string, " %zu ports\n", n);
1087 if (verbosity < 1) {
1092 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1094 ds_put_cstr(string, "rx ");
1095 print_port_stat(string, "pkts=", &ps->rx_packets, 1);
1096 print_port_stat(string, "bytes=", &ps->rx_bytes, 1);
1097 print_port_stat(string, "drop=", &ps->rx_dropped, 1);
1098 print_port_stat(string, "errs=", &ps->rx_errors, 1);
1099 print_port_stat(string, "frame=", &ps->rx_frame_err, 1);
1100 print_port_stat(string, "over=", &ps->rx_over_err, 1);
1101 print_port_stat(string, "crc=", &ps->rx_crc_err, 0);
1103 ds_put_cstr(string, " tx ");
1104 print_port_stat(string, "pkts=", &ps->tx_packets, 1);
1105 print_port_stat(string, "bytes=", &ps->tx_bytes, 1);
1106 print_port_stat(string, "drop=", &ps->tx_dropped, 1);
1107 print_port_stat(string, "errs=", &ps->tx_errors, 1);
1108 print_port_stat(string, "coll=", &ps->collisions, 0);
1113 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1116 const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1117 size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
1118 ds_put_format(string, " %zu tables\n", n);
1119 if (verbosity < 1) {
1124 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1125 ovs_strlcpy(name, ts->name, sizeof name);
1127 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1128 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1129 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1130 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1131 ds_put_cstr(string, " ");
1132 ds_put_format(string, "lookup=%"PRIu64", ",
1133 ntohll(get_32aligned_be64(&ts->lookup_count)));
1134 ds_put_format(string, "matched=%"PRIu64"\n",
1135 ntohll(get_32aligned_be64(&ts->matched_count)));
1140 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1142 if (queue_id == OFPQ_ALL) {
1143 ds_put_cstr(string, "ALL");
1145 ds_put_format(string, "%"PRIu32, queue_id);
1150 ofp_print_ofpst_queue_request(struct ds *string,
1151 const struct ofp_queue_stats_request *qsr)
1153 ds_put_cstr(string, "port=");
1154 ofputil_format_port(ntohs(qsr->port_no), string);
1156 ds_put_cstr(string, " queue=");
1157 ofp_print_queue_name(string, ntohl(qsr->queue_id));
1161 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1164 const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1165 size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
1166 ds_put_format(string, " %zu queues\n", n);
1167 if (verbosity < 1) {
1172 ds_put_cstr(string, " port ");
1173 ofputil_format_port(ntohs(qs->port_no), string);
1174 ds_put_cstr(string, " queue ");
1175 ofp_print_queue_name(string, ntohl(qs->queue_id));
1176 ds_put_cstr(string, ": ");
1178 print_port_stat(string, "bytes=", &qs->tx_bytes, 1);
1179 print_port_stat(string, "pkts=", &qs->tx_packets, 1);
1180 print_port_stat(string, "errors=", &qs->tx_errors, 0);
1185 ofp_print_ofpst_port_desc_reply(struct ds *string,
1186 const struct ofp_header *oh)
1190 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1191 ofpbuf_pull(&b, sizeof(struct ofp_stats_msg));
1192 ds_put_char(string, '\n');
1193 ofp_print_phy_ports(string, oh->version, &b);
1197 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1199 const struct ofp_stats_msg *srq = (const struct ofp_stats_msg *) oh;
1202 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1208 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1210 const struct ofp_stats_msg *srp = (const struct ofp_stats_msg *) oh;
1213 uint16_t flags = ntohs(srp->flags);
1215 ds_put_cstr(string, " flags=");
1216 if (flags & OFPSF_REPLY_MORE) {
1217 ds_put_cstr(string, "[more]");
1218 flags &= ~OFPSF_REPLY_MORE;
1221 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1228 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1230 size_t len = ntohs(oh->length);
1232 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1233 if (verbosity > 1) {
1234 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1239 ofp_print_nxt_role_message(struct ds *string,
1240 const struct nx_role_request *nrr)
1242 unsigned int role = ntohl(nrr->role);
1244 ds_put_cstr(string, " role=");
1245 if (role == NX_ROLE_OTHER) {
1246 ds_put_cstr(string, "other");
1247 } else if (role == NX_ROLE_MASTER) {
1248 ds_put_cstr(string, "master");
1249 } else if (role == NX_ROLE_SLAVE) {
1250 ds_put_cstr(string, "slave");
1252 ds_put_format(string, "%u", role);
1257 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1258 const struct nx_flow_mod_table_id *nfmti)
1260 ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1264 ofp_print_nxt_set_flow_format(struct ds *string,
1265 const struct nx_set_flow_format *nsff)
1267 uint32_t format = ntohl(nsff->format);
1269 ds_put_cstr(string, " format=");
1270 if (ofputil_nx_flow_format_is_valid(format)) {
1271 ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
1273 ds_put_format(string, "%"PRIu32, format);
1278 ofp_print_nxt_set_packet_in_format(struct ds *string,
1279 const struct nx_set_packet_in_format *nspf)
1281 uint32_t format = ntohl(nspf->format);
1283 ds_put_cstr(string, " format=");
1284 if (ofputil_packet_in_format_is_valid(format)) {
1285 ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1287 ds_put_format(string, "%"PRIu32, format);
1292 ofp_port_reason_to_string(enum ofp_port_reason reason)
1307 sprintf(s, "%d", (int) reason);
1313 ofp_print_nxt_set_async_config(struct ds *string,
1314 const struct nx_async_config *nac)
1318 for (i = 0; i < 2; i++) {
1321 ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1323 ds_put_cstr(string, " PACKET_IN:");
1324 for (j = 0; j < 32; j++) {
1325 if (nac->packet_in_mask[i] & htonl(1u << j)) {
1326 ds_put_format(string, " %s",
1327 ofputil_packet_in_reason_to_string(j));
1330 if (!nac->packet_in_mask[i]) {
1331 ds_put_cstr(string, " (off)");
1333 ds_put_char(string, '\n');
1335 ds_put_cstr(string, " PORT_STATUS:");
1336 for (j = 0; j < 32; j++) {
1337 if (nac->port_status_mask[i] & htonl(1u << j)) {
1338 ds_put_format(string, " %s", ofp_port_reason_to_string(j));
1341 if (!nac->port_status_mask[i]) {
1342 ds_put_cstr(string, " (off)");
1344 ds_put_char(string, '\n');
1346 ds_put_cstr(string, " FLOW_REMOVED:");
1347 for (j = 0; j < 32; j++) {
1348 if (nac->flow_removed_mask[i] & htonl(1u << j)) {
1349 ds_put_format(string, " %s",
1350 ofp_flow_removed_reason_to_string(j));
1353 if (!nac->flow_removed_mask[i]) {
1354 ds_put_cstr(string, " (off)");
1356 ds_put_char(string, '\n');
1361 ofp_print_nxt_set_controller_id(struct ds *string,
1362 const struct nx_controller_id *nci)
1364 ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
1368 ofp_print_version(const struct ofp_header *oh,
1371 switch (oh->version) {
1375 ds_put_cstr(string, " (OF1.1)");
1378 ds_put_cstr(string, " (OF1.2)");
1381 ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
1384 ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
1388 ofp_to_string__(const struct ofp_header *oh,
1389 const struct ofputil_msg_type *type, struct ds *string,
1392 enum ofputil_msg_code code;
1393 const void *msg = oh;
1395 ds_put_cstr(string, ofputil_msg_type_name(type));
1396 ofp_print_version(oh, string);
1397 code = ofputil_msg_type_code(type);
1399 case OFPUTIL_MSG_INVALID:
1402 case OFPUTIL_OFPT_HELLO:
1403 ds_put_char(string, '\n');
1404 ds_put_hex_dump(string, oh + 1, ntohs(oh->length) - sizeof *oh,
1408 case OFPUTIL_OFPT_ERROR:
1409 ofp_print_error_msg(string, msg);
1412 case OFPUTIL_OFPT_ECHO_REQUEST:
1413 case OFPUTIL_OFPT_ECHO_REPLY:
1414 ofp_print_echo(string, oh, verbosity);
1417 case OFPUTIL_OFPT_FEATURES_REQUEST:
1420 case OFPUTIL_OFPT_FEATURES_REPLY:
1421 ofp_print_switch_features(string, msg);
1424 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1427 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1428 case OFPUTIL_OFPT_SET_CONFIG:
1429 ofp_print_switch_config(string, msg);
1432 case OFPUTIL_OFPT_PACKET_IN:
1433 case OFPUTIL_NXT_PACKET_IN:
1434 ofp_print_packet_in(string, msg, verbosity);
1437 case OFPUTIL_OFPT_FLOW_REMOVED:
1438 case OFPUTIL_NXT_FLOW_REMOVED:
1439 ofp_print_flow_removed(string, msg);
1442 case OFPUTIL_OFPT_PORT_STATUS:
1443 ofp_print_port_status(string, msg);
1446 case OFPUTIL_OFPT_PACKET_OUT:
1447 ofp_print_packet_out(string, msg, verbosity);
1450 case OFPUTIL_OFPT_FLOW_MOD:
1451 case OFPUTIL_NXT_FLOW_MOD:
1452 ofp_print_flow_mod(string, msg, code, verbosity);
1455 case OFPUTIL_OFPT_PORT_MOD:
1456 ofp_print_port_mod(string, msg);
1459 case OFPUTIL_OFPT_BARRIER_REQUEST:
1460 case OFPUTIL_OFPT_BARRIER_REPLY:
1463 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1464 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1468 case OFPUTIL_OFPST_DESC_REQUEST:
1469 case OFPUTIL_OFPST_PORT_DESC_REQUEST:
1470 ofp_print_stats_request(string, oh);
1473 case OFPUTIL_OFPST_FLOW_REQUEST:
1474 case OFPUTIL_NXST_FLOW_REQUEST:
1475 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1476 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1477 ofp_print_stats_request(string, oh);
1478 ofp_print_flow_stats_request(string, msg);
1481 case OFPUTIL_OFPST_TABLE_REQUEST:
1482 ofp_print_stats_request(string, oh);
1485 case OFPUTIL_OFPST_PORT_REQUEST:
1486 ofp_print_stats_request(string, oh);
1487 ofp_print_ofpst_port_request(string, msg);
1490 case OFPUTIL_OFPST_QUEUE_REQUEST:
1491 ofp_print_stats_request(string, oh);
1492 ofp_print_ofpst_queue_request(string, msg);
1495 case OFPUTIL_OFPST_DESC_REPLY:
1496 ofp_print_stats_reply(string, oh);
1497 ofp_print_ofpst_desc_reply(string, msg);
1500 case OFPUTIL_OFPST_FLOW_REPLY:
1501 case OFPUTIL_NXST_FLOW_REPLY:
1502 ofp_print_stats_reply(string, oh);
1503 ofp_print_flow_stats_reply(string, oh);
1506 case OFPUTIL_OFPST_QUEUE_REPLY:
1507 ofp_print_stats_reply(string, oh);
1508 ofp_print_ofpst_queue_reply(string, oh, verbosity);
1511 case OFPUTIL_OFPST_PORT_REPLY:
1512 ofp_print_stats_reply(string, oh);
1513 ofp_print_ofpst_port_reply(string, oh, verbosity);
1516 case OFPUTIL_OFPST_TABLE_REPLY:
1517 ofp_print_stats_reply(string, oh);
1518 ofp_print_ofpst_table_reply(string, oh, verbosity);
1521 case OFPUTIL_OFPST_AGGREGATE_REPLY:
1522 ofp_print_stats_reply(string, oh);
1523 ofp_print_ofpst_aggregate_reply(string, msg);
1526 case OFPUTIL_OFPST_PORT_DESC_REPLY:
1527 ofp_print_stats_reply(string, oh);
1528 ofp_print_ofpst_port_desc_reply(string, oh);
1531 case OFPUTIL_NXT_ROLE_REQUEST:
1532 case OFPUTIL_NXT_ROLE_REPLY:
1533 ofp_print_nxt_role_message(string, msg);
1536 case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
1537 ofp_print_nxt_flow_mod_table_id(string, msg);
1540 case OFPUTIL_NXT_SET_FLOW_FORMAT:
1541 ofp_print_nxt_set_flow_format(string, msg);
1544 case OFPUTIL_NXT_SET_PACKET_IN_FORMAT:
1545 ofp_print_nxt_set_packet_in_format(string, msg);
1548 case OFPUTIL_NXT_FLOW_AGE:
1551 case OFPUTIL_NXT_SET_CONTROLLER_ID:
1552 ofp_print_nxt_set_controller_id(string, msg);
1555 case OFPUTIL_NXT_SET_ASYNC_CONFIG:
1556 ofp_print_nxt_set_async_config(string, msg);
1559 case OFPUTIL_NXST_AGGREGATE_REPLY:
1560 ofp_print_stats_reply(string, oh);
1561 ofp_print_nxst_aggregate_reply(string, msg);
1566 /* Composes and returns a string representing the OpenFlow packet of 'len'
1567 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1568 * verbosity and higher numbers increase verbosity. The caller is responsible
1569 * for freeing the string. */
1571 ofp_to_string(const void *oh_, size_t len, int verbosity)
1573 struct ds string = DS_EMPTY_INITIALIZER;
1574 const struct ofp_header *oh = oh_;
1577 ds_put_cstr(&string, "OpenFlow message is empty\n");
1578 } else if (len < sizeof(struct ofp_header)) {
1579 ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
1581 } else if (ntohs(oh->length) > len) {
1582 ds_put_format(&string,
1583 "(***truncated to %zu bytes from %"PRIu16"***)\n",
1584 len, ntohs(oh->length));
1585 } else if (ntohs(oh->length) < len) {
1586 ds_put_format(&string,
1587 "(***only uses %"PRIu16" bytes out of %zu***)\n",
1588 ntohs(oh->length), len);
1590 const struct ofputil_msg_type *type;
1593 error = ofputil_decode_msg_type(oh, &type);
1595 ofp_to_string__(oh, type, &string, verbosity);
1596 if (verbosity >= 5) {
1597 if (ds_last(&string) != '\n') {
1598 ds_put_char(&string, '\n');
1600 ds_put_hex_dump(&string, oh, len, 0, true);
1602 if (ds_last(&string) != '\n') {
1603 ds_put_char(&string, '\n');
1605 return ds_steal_cstr(&string);
1608 ofp_print_error(&string, error);
1610 ds_put_hex_dump(&string, oh, len, 0, true);
1611 return ds_steal_cstr(&string);
1615 print_and_free(FILE *stream, char *string)
1617 fputs(string, stream);
1621 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1622 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1623 * numbers increase verbosity. */
1625 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1627 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1630 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1631 * 'data' to 'stream'. */
1633 ofp_print_packet(FILE *stream, const void *data, size_t len)
1635 print_and_free(stream, ofp_packet_to_string(data, len));