8dc7f06540e628938599f57849365cd5ce017784
[cascardo/ovs.git] / lib / ofp-print.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3  *
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:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #include <config.h>
18 #include "ofp-print.h"
19
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <sys/wait.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28
29 #include "bundle.h"
30 #include "byte-order.h"
31 #include "compiler.h"
32 #include "dynamic-string.h"
33 #include "flow.h"
34 #include "learn.h"
35 #include "multipath.h"
36 #include "meta-flow.h"
37 #include "netdev.h"
38 #include "nx-match.h"
39 #include "ofp-actions.h"
40 #include "ofp-errors.h"
41 #include "ofp-msgs.h"
42 #include "ofp-util.h"
43 #include "ofpbuf.h"
44 #include "openflow/openflow.h"
45 #include "openflow/nicira-ext.h"
46 #include "packets.h"
47 #include "type-props.h"
48 #include "unaligned.h"
49 #include "odp-util.h"
50 #include "util.h"
51
52 static void ofp_print_queue_name(struct ds *string, uint32_t port);
53 static void ofp_print_error(struct ds *, enum ofperr);
54 static void ofp_print_table_features(struct ds *,
55                                      const struct ofputil_table_features *,
56                                      const struct ofputil_table_stats *);
57
58 /* Returns a string that represents the contents of the Ethernet frame in the
59  * 'len' bytes starting at 'data'.  The caller must free the returned string.*/
60 char *
61 ofp_packet_to_string(const void *data, size_t len)
62 {
63     struct ds ds = DS_EMPTY_INITIALIZER;
64     const struct pkt_metadata md = PKT_METADATA_INITIALIZER(0);
65     struct ofpbuf buf;
66     struct flow flow;
67     size_t l4_size;
68
69     ofpbuf_use_const(&buf, data, len);
70     flow_extract(&buf, &md, &flow);
71     flow_format(&ds, &flow);
72
73     l4_size = ofpbuf_l4_size(&buf);
74
75     if (flow.nw_proto == IPPROTO_TCP && l4_size >= TCP_HEADER_LEN) {
76         struct tcp_header *th = ofpbuf_l4(&buf);
77         ds_put_format(&ds, " tcp_csum:%"PRIx16, ntohs(th->tcp_csum));
78     } else if (flow.nw_proto == IPPROTO_UDP && l4_size >= UDP_HEADER_LEN) {
79         struct udp_header *uh = ofpbuf_l4(&buf);
80         ds_put_format(&ds, " udp_csum:%"PRIx16, ntohs(uh->udp_csum));
81     } else if (flow.nw_proto == IPPROTO_SCTP && l4_size >= SCTP_HEADER_LEN) {
82         struct sctp_header *sh = ofpbuf_l4(&buf);
83         ds_put_format(&ds, " sctp_csum:%"PRIx32,
84                       ntohl(get_16aligned_be32(&sh->sctp_csum)));
85     }
86
87     ds_put_char(&ds, '\n');
88
89     return ds_cstr(&ds);
90 }
91
92 static void
93 ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
94                     int verbosity)
95 {
96     char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
97     struct ofputil_packet_in pin;
98     int error;
99     int i;
100
101     error = ofputil_decode_packet_in(&pin, oh);
102     if (error) {
103         ofp_print_error(string, error);
104         return;
105     }
106
107     if (pin.table_id) {
108         ds_put_format(string, " table_id=%"PRIu8, pin.table_id);
109     }
110
111     if (pin.cookie != OVS_BE64_MAX) {
112         ds_put_format(string, " cookie=0x%"PRIx64, ntohll(pin.cookie));
113     }
114
115     ds_put_format(string, " total_len=%"PRIuSIZE" in_port=", pin.total_len);
116     ofputil_format_port(pin.fmd.in_port, string);
117
118     if (pin.fmd.tun_id != htonll(0)) {
119         ds_put_format(string, " tun_id=0x%"PRIx64, ntohll(pin.fmd.tun_id));
120     }
121
122     if (pin.fmd.tun_src != htonl(0)) {
123         ds_put_format(string, " tun_src="IP_FMT, IP_ARGS(pin.fmd.tun_src));
124     }
125
126     if (pin.fmd.tun_dst != htonl(0)) {
127         ds_put_format(string, " tun_dst="IP_FMT, IP_ARGS(pin.fmd.tun_dst));
128     }
129
130     if (pin.fmd.metadata != htonll(0)) {
131         ds_put_format(string, " metadata=0x%"PRIx64, ntohll(pin.fmd.metadata));
132     }
133
134     for (i = 0; i < FLOW_N_REGS; i++) {
135         if (pin.fmd.regs[i]) {
136             ds_put_format(string, " reg%d=0x%"PRIx32, i, pin.fmd.regs[i]);
137         }
138     }
139
140     if (pin.fmd.pkt_mark != 0) {
141         ds_put_format(string, " pkt_mark=0x%"PRIx32, pin.fmd.pkt_mark);
142     }
143
144     ds_put_format(string, " (via %s)",
145                   ofputil_packet_in_reason_to_string(pin.reason, reasonbuf,
146                                                      sizeof reasonbuf));
147
148     ds_put_format(string, " data_len=%"PRIuSIZE, pin.packet_len);
149     if (pin.buffer_id == UINT32_MAX) {
150         ds_put_format(string, " (unbuffered)");
151         if (pin.total_len != pin.packet_len) {
152             ds_put_format(string, " (***total_len != data_len***)");
153         }
154     } else {
155         ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
156         if (pin.total_len < pin.packet_len) {
157             ds_put_format(string, " (***total_len < data_len***)");
158         }
159     }
160     ds_put_char(string, '\n');
161
162     if (verbosity > 0) {
163         char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
164         ds_put_cstr(string, packet);
165         free(packet);
166     }
167     if (verbosity > 2) {
168         ds_put_hex_dump(string, pin.packet, pin.packet_len, 0, false);
169     }
170 }
171
172 static void
173 ofp_print_packet_out(struct ds *string, const struct ofp_header *oh,
174                      int verbosity)
175 {
176     struct ofputil_packet_out po;
177     struct ofpbuf ofpacts;
178     enum ofperr error;
179
180     ofpbuf_init(&ofpacts, 64);
181     error = ofputil_decode_packet_out(&po, oh, &ofpacts);
182     if (error) {
183         ofpbuf_uninit(&ofpacts);
184         ofp_print_error(string, error);
185         return;
186     }
187
188     ds_put_cstr(string, " in_port=");
189     ofputil_format_port(po.in_port, string);
190
191     ds_put_cstr(string, " actions=");
192     ofpacts_format(po.ofpacts, po.ofpacts_len, string);
193
194     if (po.buffer_id == UINT32_MAX) {
195         ds_put_format(string, " data_len=%"PRIuSIZE, po.packet_len);
196         if (verbosity > 0 && po.packet_len > 0) {
197             char *packet = ofp_packet_to_string(po.packet, po.packet_len);
198             ds_put_char(string, '\n');
199             ds_put_cstr(string, packet);
200             free(packet);
201         }
202         if (verbosity > 2) {
203             ds_put_hex_dump(string, po.packet, po.packet_len, 0, false);
204         }
205     } else {
206         ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id);
207     }
208
209     ofpbuf_uninit(&ofpacts);
210 }
211
212 /* qsort comparison function. */
213 static int
214 compare_ports(const void *a_, const void *b_)
215 {
216     const struct ofputil_phy_port *a = a_;
217     const struct ofputil_phy_port *b = b_;
218     uint16_t ap = ofp_to_u16(a->port_no);
219     uint16_t bp = ofp_to_u16(b->port_no);
220
221     return ap < bp ? -1 : ap > bp;
222 }
223
224 static void
225 ofp_print_bit_names(struct ds *string, uint32_t bits,
226                     const char *(*bit_to_name)(uint32_t bit),
227                     char separator)
228 {
229     int n = 0;
230     int i;
231
232     if (!bits) {
233         ds_put_cstr(string, "0");
234         return;
235     }
236
237     for (i = 0; i < 32; i++) {
238         uint32_t bit = UINT32_C(1) << i;
239
240         if (bits & bit) {
241             const char *name = bit_to_name(bit);
242             if (name) {
243                 if (n++) {
244                     ds_put_char(string, separator);
245                 }
246                 ds_put_cstr(string, name);
247                 bits &= ~bit;
248             }
249         }
250     }
251
252     if (bits) {
253         if (n) {
254             ds_put_char(string, separator);
255         }
256         ds_put_format(string, "0x%"PRIx32, bits);
257     }
258 }
259
260 static const char *
261 netdev_feature_to_name(uint32_t bit)
262 {
263     enum netdev_features f = bit;
264
265     switch (f) {
266     case NETDEV_F_10MB_HD:    return "10MB-HD";
267     case NETDEV_F_10MB_FD:    return "10MB-FD";
268     case NETDEV_F_100MB_HD:   return "100MB-HD";
269     case NETDEV_F_100MB_FD:   return "100MB-FD";
270     case NETDEV_F_1GB_HD:     return "1GB-HD";
271     case NETDEV_F_1GB_FD:     return "1GB-FD";
272     case NETDEV_F_10GB_FD:    return "10GB-FD";
273     case NETDEV_F_40GB_FD:    return "40GB-FD";
274     case NETDEV_F_100GB_FD:   return "100GB-FD";
275     case NETDEV_F_1TB_FD:     return "1TB-FD";
276     case NETDEV_F_OTHER:      return "OTHER";
277     case NETDEV_F_COPPER:     return "COPPER";
278     case NETDEV_F_FIBER:      return "FIBER";
279     case NETDEV_F_AUTONEG:    return "AUTO_NEG";
280     case NETDEV_F_PAUSE:      return "AUTO_PAUSE";
281     case NETDEV_F_PAUSE_ASYM: return "AUTO_PAUSE_ASYM";
282     }
283
284     return NULL;
285 }
286
287 static void
288 ofp_print_port_features(struct ds *string, enum netdev_features features)
289 {
290     ofp_print_bit_names(string, features, netdev_feature_to_name, ' ');
291     ds_put_char(string, '\n');
292 }
293
294 static const char *
295 ofputil_port_config_to_name(uint32_t bit)
296 {
297     enum ofputil_port_config pc = bit;
298
299     switch (pc) {
300     case OFPUTIL_PC_PORT_DOWN:    return "PORT_DOWN";
301     case OFPUTIL_PC_NO_STP:       return "NO_STP";
302     case OFPUTIL_PC_NO_RECV:      return "NO_RECV";
303     case OFPUTIL_PC_NO_RECV_STP:  return "NO_RECV_STP";
304     case OFPUTIL_PC_NO_FLOOD:     return "NO_FLOOD";
305     case OFPUTIL_PC_NO_FWD:       return "NO_FWD";
306     case OFPUTIL_PC_NO_PACKET_IN: return "NO_PACKET_IN";
307     }
308
309     return NULL;
310 }
311
312 static void
313 ofp_print_port_config(struct ds *string, enum ofputil_port_config config)
314 {
315     ofp_print_bit_names(string, config, ofputil_port_config_to_name, ' ');
316     ds_put_char(string, '\n');
317 }
318
319 static const char *
320 ofputil_port_state_to_name(uint32_t bit)
321 {
322     enum ofputil_port_state ps = bit;
323
324     switch (ps) {
325     case OFPUTIL_PS_LINK_DOWN: return "LINK_DOWN";
326     case OFPUTIL_PS_BLOCKED:   return "BLOCKED";
327     case OFPUTIL_PS_LIVE:      return "LIVE";
328
329     case OFPUTIL_PS_STP_LISTEN:
330     case OFPUTIL_PS_STP_LEARN:
331     case OFPUTIL_PS_STP_FORWARD:
332     case OFPUTIL_PS_STP_BLOCK:
333         /* Handled elsewhere. */
334         return NULL;
335     }
336
337     return NULL;
338 }
339
340 static void
341 ofp_print_port_state(struct ds *string, enum ofputil_port_state state)
342 {
343     enum ofputil_port_state stp_state;
344
345     /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
346      * pattern.  We have to special case it.
347      *
348      * OVS doesn't support STP, so this field will always be 0 if we are
349      * talking to OVS, so we'd always print STP_LISTEN in that case.
350      * Therefore, we don't print anything at all if the value is STP_LISTEN, to
351      * avoid confusing users. */
352     stp_state = state & OFPUTIL_PS_STP_MASK;
353     if (stp_state) {
354         ds_put_cstr(string,
355                     (stp_state == OFPUTIL_PS_STP_LEARN ? "STP_LEARN"
356                      : stp_state == OFPUTIL_PS_STP_FORWARD ? "STP_FORWARD"
357                      : "STP_BLOCK"));
358         state &= ~OFPUTIL_PS_STP_MASK;
359         if (state) {
360             ofp_print_bit_names(string, state, ofputil_port_state_to_name,
361                                 ' ');
362         }
363     } else {
364         ofp_print_bit_names(string, state, ofputil_port_state_to_name, ' ');
365     }
366     ds_put_char(string, '\n');
367 }
368
369 static void
370 ofp_print_phy_port(struct ds *string, const struct ofputil_phy_port *port)
371 {
372     char name[sizeof port->name];
373     int j;
374
375     memcpy(name, port->name, sizeof name);
376     for (j = 0; j < sizeof name - 1; j++) {
377         if (!isprint((unsigned char) name[j])) {
378             break;
379         }
380     }
381     name[j] = '\0';
382
383     ds_put_char(string, ' ');
384     ofputil_format_port(port->port_no, string);
385     ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
386                   name, ETH_ADDR_ARGS(port->hw_addr));
387
388     ds_put_cstr(string, "     config:     ");
389     ofp_print_port_config(string, port->config);
390
391     ds_put_cstr(string, "     state:      ");
392     ofp_print_port_state(string, port->state);
393
394     if (port->curr) {
395         ds_put_format(string, "     current:    ");
396         ofp_print_port_features(string, port->curr);
397     }
398     if (port->advertised) {
399         ds_put_format(string, "     advertised: ");
400         ofp_print_port_features(string, port->advertised);
401     }
402     if (port->supported) {
403         ds_put_format(string, "     supported:  ");
404         ofp_print_port_features(string, port->supported);
405     }
406     if (port->peer) {
407         ds_put_format(string, "     peer:       ");
408         ofp_print_port_features(string, port->peer);
409     }
410     ds_put_format(string, "     speed: %"PRIu32" Mbps now, "
411                   "%"PRIu32" Mbps max\n",
412                   port->curr_speed / UINT32_C(1000),
413                   port->max_speed / UINT32_C(1000));
414 }
415
416 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
417  * 'ofp_version', writes a detailed description of each port into
418  * 'string'. */
419 static void
420 ofp_print_phy_ports(struct ds *string, uint8_t ofp_version,
421                     struct ofpbuf *b)
422 {
423     struct ofputil_phy_port *ports;
424     size_t allocated_ports, n_ports;
425     int retval;
426     size_t i;
427
428     ports = NULL;
429     allocated_ports = 0;
430     for (n_ports = 0; ; n_ports++) {
431         if (n_ports >= allocated_ports) {
432             ports = x2nrealloc(ports, &allocated_ports, sizeof *ports);
433         }
434
435         retval = ofputil_pull_phy_port(ofp_version, b, &ports[n_ports]);
436         if (retval) {
437             break;
438         }
439     }
440
441     qsort(ports, n_ports, sizeof *ports, compare_ports);
442     for (i = 0; i < n_ports; i++) {
443         ofp_print_phy_port(string, &ports[i]);
444     }
445     free(ports);
446
447     if (retval != EOF) {
448         ofp_print_error(string, retval);
449     }
450 }
451
452 static const char *
453 ofputil_capabilities_to_name(uint32_t bit)
454 {
455     enum ofputil_capabilities capabilities = bit;
456
457     switch (capabilities) {
458     case OFPUTIL_C_FLOW_STATS:   return "FLOW_STATS";
459     case OFPUTIL_C_TABLE_STATS:  return "TABLE_STATS";
460     case OFPUTIL_C_PORT_STATS:   return "PORT_STATS";
461     case OFPUTIL_C_IP_REASM:     return "IP_REASM";
462     case OFPUTIL_C_QUEUE_STATS:  return "QUEUE_STATS";
463     case OFPUTIL_C_ARP_MATCH_IP: return "ARP_MATCH_IP";
464     case OFPUTIL_C_STP:          return "STP";
465     case OFPUTIL_C_GROUP_STATS:  return "GROUP_STATS";
466     case OFPUTIL_C_PORT_BLOCKED: return "PORT_BLOCKED";
467     }
468
469     return NULL;
470 }
471
472 static void
473 ofp_print_switch_features(struct ds *string, const struct ofp_header *oh)
474 {
475     struct ofputil_switch_features features;
476     enum ofperr error;
477     struct ofpbuf b;
478
479     error = ofputil_decode_switch_features(oh, &features, &b);
480     if (error) {
481         ofp_print_error(string, error);
482         return;
483     }
484
485     ds_put_format(string, " dpid:%016"PRIx64"\n", features.datapath_id);
486
487     ds_put_format(string, "n_tables:%"PRIu8", n_buffers:%"PRIu32,
488                   features.n_tables, features.n_buffers);
489     if (features.auxiliary_id) {
490         ds_put_format(string, ", auxiliary_id:%"PRIu8, features.auxiliary_id);
491     }
492     ds_put_char(string, '\n');
493
494     ds_put_cstr(string, "capabilities: ");
495     ofp_print_bit_names(string, features.capabilities,
496                         ofputil_capabilities_to_name, ' ');
497     ds_put_char(string, '\n');
498
499     switch ((enum ofp_version)oh->version) {
500     case OFP10_VERSION:
501         ds_put_cstr(string, "actions: ");
502         ofpact_bitmap_format(features.ofpacts, string);
503         ds_put_char(string, '\n');
504         break;
505     case OFP11_VERSION:
506     case OFP12_VERSION:
507         break;
508     case OFP13_VERSION:
509     case OFP14_VERSION:
510     case OFP15_VERSION:
511         return; /* no ports in ofp13_switch_features */
512     default:
513         OVS_NOT_REACHED();
514     }
515
516     ofp_print_phy_ports(string, oh->version, &b);
517 }
518
519 static void
520 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
521 {
522     enum ofp_config_flags flags;
523
524     flags = ntohs(osc->flags);
525
526     ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
527     flags &= ~OFPC_FRAG_MASK;
528
529     if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) {
530         ds_put_format(string, " invalid_ttl_to_controller");
531         flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER;
532     }
533
534     if (flags) {
535         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
536     }
537
538     ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
539 }
540
541 static void print_wild(struct ds *string, const char *leader, int is_wild,
542             int verbosity, const char *format, ...)
543             PRINTF_FORMAT(5, 6);
544
545 static void print_wild(struct ds *string, const char *leader, int is_wild,
546                        int verbosity, const char *format, ...)
547 {
548     if (is_wild && verbosity < 2) {
549         return;
550     }
551     ds_put_cstr(string, leader);
552     if (!is_wild) {
553         va_list args;
554
555         va_start(args, format);
556         ds_put_format_valist(string, format, args);
557         va_end(args);
558     } else {
559         ds_put_char(string, '*');
560     }
561     ds_put_char(string, ',');
562 }
563
564 static void
565 print_wild_port(struct ds *string, const char *leader, int is_wild,
566                 int verbosity, ofp_port_t port)
567 {
568     if (is_wild && verbosity < 2) {
569         return;
570     }
571     ds_put_cstr(string, leader);
572     if (!is_wild) {
573         ofputil_format_port(port, string);
574     } else {
575         ds_put_char(string, '*');
576     }
577     ds_put_char(string, ',');
578 }
579
580 static void
581 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
582                  uint32_t wild_bits, int verbosity)
583 {
584     if (wild_bits >= 32 && verbosity < 2) {
585         return;
586     }
587     ds_put_cstr(string, leader);
588     if (wild_bits < 32) {
589         ds_put_format(string, IP_FMT, IP_ARGS(ip));
590         if (wild_bits) {
591             ds_put_format(string, "/%d", 32 - wild_bits);
592         }
593     } else {
594         ds_put_char(string, '*');
595     }
596     ds_put_char(string, ',');
597 }
598
599 void
600 ofp10_match_print(struct ds *f, const struct ofp10_match *om, int verbosity)
601 {
602     char *s = ofp10_match_to_string(om, verbosity);
603     ds_put_cstr(f, s);
604     free(s);
605 }
606
607 char *
608 ofp10_match_to_string(const struct ofp10_match *om, int verbosity)
609 {
610     struct ds f = DS_EMPTY_INITIALIZER;
611     uint32_t w = ntohl(om->wildcards);
612     bool skip_type = false;
613     bool skip_proto = false;
614
615     if (!(w & OFPFW10_DL_TYPE)) {
616         skip_type = true;
617         if (om->dl_type == htons(ETH_TYPE_IP)) {
618             if (!(w & OFPFW10_NW_PROTO)) {
619                 skip_proto = true;
620                 if (om->nw_proto == IPPROTO_ICMP) {
621                     ds_put_cstr(&f, "icmp,");
622                 } else if (om->nw_proto == IPPROTO_TCP) {
623                     ds_put_cstr(&f, "tcp,");
624                 } else if (om->nw_proto == IPPROTO_UDP) {
625                     ds_put_cstr(&f, "udp,");
626                 } else if (om->nw_proto == IPPROTO_SCTP) {
627                     ds_put_cstr(&f, "sctp,");
628                 } else {
629                     ds_put_cstr(&f, "ip,");
630                     skip_proto = false;
631                 }
632             } else {
633                 ds_put_cstr(&f, "ip,");
634             }
635         } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
636             ds_put_cstr(&f, "arp,");
637         } else if (om->dl_type == htons(ETH_TYPE_RARP)){
638             ds_put_cstr(&f, "rarp,");
639         } else if (om->dl_type == htons(ETH_TYPE_MPLS)) {
640             ds_put_cstr(&f, "mpls,");
641         } else if (om->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
642             ds_put_cstr(&f, "mplsm,");
643         } else {
644             skip_type = false;
645         }
646     }
647     print_wild_port(&f, "in_port=", w & OFPFW10_IN_PORT, verbosity,
648                     u16_to_ofp(ntohs(om->in_port)));
649     print_wild(&f, "dl_vlan=", w & OFPFW10_DL_VLAN, verbosity,
650                "%d", ntohs(om->dl_vlan));
651     print_wild(&f, "dl_vlan_pcp=", w & OFPFW10_DL_VLAN_PCP, verbosity,
652                "%d", om->dl_vlan_pcp);
653     print_wild(&f, "dl_src=", w & OFPFW10_DL_SRC, verbosity,
654                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
655     print_wild(&f, "dl_dst=", w & OFPFW10_DL_DST, verbosity,
656                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
657     if (!skip_type) {
658         print_wild(&f, "dl_type=", w & OFPFW10_DL_TYPE, verbosity,
659                    "0x%04x", ntohs(om->dl_type));
660     }
661     print_ip_netmask(&f, "nw_src=", om->nw_src,
662                      (w & OFPFW10_NW_SRC_MASK) >> OFPFW10_NW_SRC_SHIFT,
663                      verbosity);
664     print_ip_netmask(&f, "nw_dst=", om->nw_dst,
665                      (w & OFPFW10_NW_DST_MASK) >> OFPFW10_NW_DST_SHIFT,
666                      verbosity);
667     if (!skip_proto) {
668         if (om->dl_type == htons(ETH_TYPE_ARP) ||
669             om->dl_type == htons(ETH_TYPE_RARP)) {
670             print_wild(&f, "arp_op=", w & OFPFW10_NW_PROTO, verbosity,
671                        "%u", om->nw_proto);
672         } else {
673             print_wild(&f, "nw_proto=", w & OFPFW10_NW_PROTO, verbosity,
674                        "%u", om->nw_proto);
675         }
676     }
677     print_wild(&f, "nw_tos=", w & OFPFW10_NW_TOS, verbosity,
678                "%u", om->nw_tos);
679     if (om->nw_proto == IPPROTO_ICMP) {
680         print_wild(&f, "icmp_type=", w & OFPFW10_ICMP_TYPE, verbosity,
681                    "%d", ntohs(om->tp_src));
682         print_wild(&f, "icmp_code=", w & OFPFW10_ICMP_CODE, verbosity,
683                    "%d", ntohs(om->tp_dst));
684     } else {
685         print_wild(&f, "tp_src=", w & OFPFW10_TP_SRC, verbosity,
686                    "%d", ntohs(om->tp_src));
687         print_wild(&f, "tp_dst=", w & OFPFW10_TP_DST, verbosity,
688                    "%d", ntohs(om->tp_dst));
689     }
690     if (ds_last(&f) == ',') {
691         f.length--;
692     }
693     return ds_cstr(&f);
694 }
695
696 static void
697 ofp_print_flow_flags(struct ds *s, enum ofputil_flow_mod_flags flags)
698 {
699     if (flags & OFPUTIL_FF_SEND_FLOW_REM) {
700         ds_put_cstr(s, "send_flow_rem ");
701     }
702     if (flags & OFPUTIL_FF_CHECK_OVERLAP) {
703         ds_put_cstr(s, "check_overlap ");
704     }
705     if (flags & OFPUTIL_FF_RESET_COUNTS) {
706         ds_put_cstr(s, "reset_counts ");
707     }
708     if (flags & OFPUTIL_FF_NO_PKT_COUNTS) {
709         ds_put_cstr(s, "no_packet_counts ");
710     }
711     if (flags & OFPUTIL_FF_NO_BYT_COUNTS) {
712         ds_put_cstr(s, "no_byte_counts ");
713     }
714     if (flags & OFPUTIL_FF_HIDDEN_FIELDS) {
715         ds_put_cstr(s, "allow_hidden_fields ");
716     }
717     if (flags & OFPUTIL_FF_NO_READONLY) {
718         ds_put_cstr(s, "no_readonly_table ");
719     }
720 }
721
722 static void
723 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh, int verbosity)
724 {
725     struct ofputil_flow_mod fm;
726     struct ofpbuf ofpacts;
727     bool need_priority;
728     enum ofperr error;
729     enum ofpraw raw;
730     enum ofputil_protocol protocol;
731
732     protocol = ofputil_protocol_from_ofp_version(oh->version);
733     protocol = ofputil_protocol_set_tid(protocol, true);
734
735     ofpbuf_init(&ofpacts, 64);
736     error = ofputil_decode_flow_mod(&fm, oh, protocol, &ofpacts,
737                                     OFPP_MAX, 255);
738     if (error) {
739         ofpbuf_uninit(&ofpacts);
740         ofp_print_error(s, error);
741         return;
742     }
743
744     ds_put_char(s, ' ');
745     switch (fm.command) {
746     case OFPFC_ADD:
747         ds_put_cstr(s, "ADD");
748         break;
749     case OFPFC_MODIFY:
750         ds_put_cstr(s, "MOD");
751         break;
752     case OFPFC_MODIFY_STRICT:
753         ds_put_cstr(s, "MOD_STRICT");
754         break;
755     case OFPFC_DELETE:
756         ds_put_cstr(s, "DEL");
757         break;
758     case OFPFC_DELETE_STRICT:
759         ds_put_cstr(s, "DEL_STRICT");
760         break;
761     default:
762         ds_put_format(s, "cmd:%d", fm.command);
763     }
764     if (fm.table_id != 0) {
765         ds_put_format(s, " table:%d", fm.table_id);
766     }
767
768     ds_put_char(s, ' ');
769     ofpraw_decode(&raw, oh);
770     if (verbosity >= 3 && raw == OFPRAW_OFPT10_FLOW_MOD) {
771         const struct ofp10_flow_mod *ofm = ofpmsg_body(oh);
772         ofp10_match_print(s, &ofm->match, verbosity);
773
774         /* ofp_print_match() doesn't print priority. */
775         need_priority = true;
776     } else if (verbosity >= 3 && raw == OFPRAW_NXT_FLOW_MOD) {
777         const struct nx_flow_mod *nfm = ofpmsg_body(oh);
778         const void *nxm = nfm + 1;
779         char *nxm_s;
780
781         nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
782         ds_put_cstr(s, nxm_s);
783         free(nxm_s);
784
785         /* nx_match_to_string() doesn't print priority. */
786         need_priority = true;
787     } else {
788         match_format(&fm.match, s, fm.priority);
789
790         /* match_format() does print priority. */
791         need_priority = false;
792     }
793
794     if (ds_last(s) != ' ') {
795         ds_put_char(s, ' ');
796     }
797     if (fm.new_cookie != htonll(0) && fm.new_cookie != OVS_BE64_MAX) {
798         ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.new_cookie));
799     }
800     if (fm.cookie_mask != htonll(0)) {
801         ds_put_format(s, "cookie:0x%"PRIx64"/0x%"PRIx64" ",
802                 ntohll(fm.cookie), ntohll(fm.cookie_mask));
803     }
804     if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
805         ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
806     }
807     if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
808         ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
809     }
810     if (fm.priority != OFP_DEFAULT_PRIORITY && need_priority) {
811         ds_put_format(s, "pri:%"PRIu16" ", fm.priority);
812     }
813     if (fm.buffer_id != UINT32_MAX) {
814         ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
815     }
816     if (fm.out_port != OFPP_ANY) {
817         ds_put_format(s, "out_port:");
818         ofputil_format_port(fm.out_port, s);
819         ds_put_char(s, ' ');
820     }
821
822     if (oh->version == OFP10_VERSION || oh->version == OFP11_VERSION) {
823         /* Don't print the reset_counts flag for OF1.0 and OF1.1 because those
824          * versions don't really have such a flag and printing one is likely to
825          * confuse people. */
826         fm.flags &= ~OFPUTIL_FF_RESET_COUNTS;
827     }
828     ofp_print_flow_flags(s, fm.flags);
829
830     ds_put_cstr(s, "actions=");
831     ofpacts_format(fm.ofpacts, fm.ofpacts_len, s);
832     ofpbuf_uninit(&ofpacts);
833 }
834
835 static void
836 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
837 {
838     ds_put_format(string, "%u", sec);
839
840     /* If there are no fractional seconds, don't print any decimals.
841      *
842      * If the fractional seconds can be expressed exactly as milliseconds,
843      * print 3 decimals.  Open vSwitch provides millisecond precision for most
844      * time measurements, so printing 3 decimals every time makes it easier to
845      * spot real changes in flow dumps that refresh themselves quickly.
846      *
847      * If the fractional seconds are more precise than milliseconds, print the
848      * number of decimals needed to express them exactly.
849      */
850     if (nsec > 0) {
851         unsigned int msec = nsec / 1000000;
852         if (msec * 1000000 == nsec) {
853             ds_put_format(string, ".%03u", msec);
854         } else {
855             ds_put_format(string, ".%09u", nsec);
856             while (string->string[string->length - 1] == '0') {
857                 string->length--;
858             }
859         }
860     }
861     ds_put_char(string, 's');
862 }
863
864 /* Returns a string form of 'reason'.  The return value is either a statically
865  * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
866  * 'bufsize' should be at least OFP_FLOW_REMOVED_REASON_BUFSIZE. */
867 #define OFP_FLOW_REMOVED_REASON_BUFSIZE (INT_STRLEN(int) + 1)
868 static const char *
869 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason,
870                                   char *reasonbuf, size_t bufsize)
871 {
872     switch (reason) {
873     case OFPRR_IDLE_TIMEOUT:
874         return "idle";
875     case OFPRR_HARD_TIMEOUT:
876         return "hard";
877     case OFPRR_DELETE:
878         return "delete";
879     case OFPRR_GROUP_DELETE:
880         return "group_delete";
881     case OFPRR_EVICTION:
882         return "eviction";
883     case OFPRR_METER_DELETE:
884         return "meter_delete";
885     default:
886         snprintf(reasonbuf, bufsize, "%d", (int) reason);
887         return reasonbuf;
888     }
889 }
890
891 static void
892 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
893 {
894     char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
895     struct ofputil_flow_removed fr;
896     enum ofperr error;
897
898     error = ofputil_decode_flow_removed(&fr, oh);
899     if (error) {
900         ofp_print_error(string, error);
901         return;
902     }
903
904     ds_put_char(string, ' ');
905     match_format(&fr.match, string, fr.priority);
906
907     ds_put_format(string, " reason=%s",
908                   ofp_flow_removed_reason_to_string(fr.reason, reasonbuf,
909                                                     sizeof reasonbuf));
910
911     if (fr.table_id != 255) {
912         ds_put_format(string, " table_id=%"PRIu8, fr.table_id);
913     }
914
915     if (fr.cookie != htonll(0)) {
916         ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
917     }
918     ds_put_cstr(string, " duration");
919     ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
920     ds_put_format(string, " idle%"PRIu16, fr.idle_timeout);
921     if (fr.hard_timeout) {
922         /* The hard timeout was only added in OF1.2, so only print it if it is
923          * actually in use to avoid gratuitous change to the formatting. */
924         ds_put_format(string, " hard%"PRIu16, fr.hard_timeout);
925     }
926     ds_put_format(string, " pkts%"PRIu64" bytes%"PRIu64"\n",
927                   fr.packet_count, fr.byte_count);
928 }
929
930 static void
931 ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
932 {
933     struct ofputil_port_mod pm;
934     enum ofperr error;
935
936     error = ofputil_decode_port_mod(oh, &pm, true);
937     if (error) {
938         ofp_print_error(string, error);
939         return;
940     }
941
942     ds_put_cstr(string, "port: ");
943     ofputil_format_port(pm.port_no, string);
944     ds_put_format(string, ": addr:"ETH_ADDR_FMT"\n",
945                   ETH_ADDR_ARGS(pm.hw_addr));
946
947     ds_put_cstr(string, "     config: ");
948     ofp_print_port_config(string, pm.config);
949
950     ds_put_cstr(string, "     mask:   ");
951     ofp_print_port_config(string, pm.mask);
952
953     ds_put_cstr(string, "     advertise: ");
954     if (pm.advertise) {
955         ofp_print_port_features(string, pm.advertise);
956     } else {
957         ds_put_cstr(string, "UNCHANGED\n");
958     }
959 }
960
961 static void
962 ofp_print_table_miss_config(struct ds *string, enum ofputil_table_miss miss)
963 {
964     switch (miss) {
965     case OFPUTIL_TABLE_MISS_CONTROLLER:
966         ds_put_cstr(string, "controller\n");
967         break;
968     case OFPUTIL_TABLE_MISS_CONTINUE:
969         ds_put_cstr(string, "continue\n");
970         break;
971     case OFPUTIL_TABLE_MISS_DROP:
972         ds_put_cstr(string, "drop\n");
973         break;
974     case OFPUTIL_TABLE_MISS_DEFAULT:
975     default:
976         ds_put_format(string, "Unknown (%d)\n", miss);
977         break;
978     }
979 }
980
981 static void
982 ofp_print_table_mod(struct ds *string, const struct ofp_header *oh)
983 {
984     struct ofputil_table_mod pm;
985     enum ofperr error;
986
987     error = ofputil_decode_table_mod(oh, &pm);
988     if (error) {
989         ofp_print_error(string, error);
990         return;
991     }
992
993     if (pm.table_id == 0xff) {
994         ds_put_cstr(string, " table_id: ALL_TABLES");
995     } else {
996         ds_put_format(string, " table_id=%"PRIu8, pm.table_id);
997     }
998
999     if (pm.miss_config != OFPUTIL_TABLE_MISS_DEFAULT) {
1000         ds_put_cstr(string, ", flow_miss_config=");
1001         ofp_print_table_miss_config(string, pm.miss_config);
1002     }
1003 }
1004
1005 static void
1006 ofp_print_queue_get_config_request(struct ds *string,
1007                                    const struct ofp_header *oh)
1008 {
1009     enum ofperr error;
1010     ofp_port_t port;
1011
1012     error = ofputil_decode_queue_get_config_request(oh, &port);
1013     if (error) {
1014         ofp_print_error(string, error);
1015         return;
1016     }
1017
1018     ds_put_cstr(string, " port=");
1019     ofputil_format_port(port, string);
1020 }
1021
1022 static void
1023 print_queue_rate(struct ds *string, const char *name, unsigned int rate)
1024 {
1025     if (rate <= 1000) {
1026         ds_put_format(string, " %s:%u.%u%%", name, rate / 10, rate % 10);
1027     } else if (rate < UINT16_MAX) {
1028         ds_put_format(string, " %s:(disabled)", name);
1029     }
1030 }
1031
1032 static void
1033 ofp_print_queue_get_config_reply(struct ds *string,
1034                                  const struct ofp_header *oh)
1035 {
1036     enum ofperr error;
1037     struct ofpbuf b;
1038     ofp_port_t port;
1039
1040     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1041     error = ofputil_decode_queue_get_config_reply(&b, &port);
1042     if (error) {
1043         ofp_print_error(string, error);
1044         return;
1045     }
1046
1047     ds_put_cstr(string, " port=");
1048     ofputil_format_port(port, string);
1049     ds_put_char(string, '\n');
1050
1051     for (;;) {
1052         struct ofputil_queue_config queue;
1053         int retval;
1054
1055         retval = ofputil_pull_queue_get_config_reply(&b, &queue);
1056         if (retval) {
1057             if (retval != EOF) {
1058                 ofp_print_error(string, retval);
1059             }
1060             break;
1061         }
1062
1063         ds_put_format(string, "queue %"PRIu32":", queue.queue_id);
1064         print_queue_rate(string, "min_rate", queue.min_rate);
1065         print_queue_rate(string, "max_rate", queue.max_rate);
1066         ds_put_char(string, '\n');
1067     }
1068 }
1069
1070 static void
1071 ofp_print_meter_flags(struct ds *s, uint16_t flags)
1072 {
1073     if (flags & OFPMF13_KBPS) {
1074         ds_put_cstr(s, "kbps ");
1075     }
1076     if (flags & OFPMF13_PKTPS) {
1077         ds_put_cstr(s, "pktps ");
1078     }
1079     if (flags & OFPMF13_BURST) {
1080         ds_put_cstr(s, "burst ");
1081     }
1082     if (flags & OFPMF13_STATS) {
1083         ds_put_cstr(s, "stats ");
1084     }
1085
1086     flags &= ~(OFPMF13_KBPS | OFPMF13_PKTPS | OFPMF13_BURST | OFPMF13_STATS);
1087     if (flags) {
1088         ds_put_format(s, "flags:0x%"PRIx16" ", flags);
1089     }
1090 }
1091
1092 static void
1093 ofp_print_meter_band(struct ds *s, uint16_t flags,
1094                      const struct ofputil_meter_band *mb)
1095 {
1096     ds_put_cstr(s, "\ntype=");
1097     switch (mb->type) {
1098     case OFPMBT13_DROP:
1099         ds_put_cstr(s, "drop");
1100         break;
1101     case OFPMBT13_DSCP_REMARK:
1102         ds_put_cstr(s, "dscp_remark");
1103         break;
1104     default:
1105         ds_put_format(s, "%u", mb->type);
1106     }
1107
1108     ds_put_format(s, " rate=%"PRIu32, mb->rate);
1109
1110     if (flags & OFPMF13_BURST) {
1111         ds_put_format(s, " burst_size=%"PRIu32, mb->burst_size);
1112     }
1113     if (mb->type == OFPMBT13_DSCP_REMARK) {
1114         ds_put_format(s, " prec_level=%"PRIu8, mb->prec_level);
1115     }
1116 }
1117
1118 static void
1119 ofp_print_meter_stats(struct ds *s, const struct ofputil_meter_stats *ms)
1120 {
1121     uint16_t i;
1122
1123     ds_put_format(s, "meter:%"PRIu32" ", ms->meter_id);
1124     ds_put_format(s, "flow_count:%"PRIu32" ", ms->flow_count);
1125     ds_put_format(s, "packet_in_count:%"PRIu64" ", ms->packet_in_count);
1126     ds_put_format(s, "byte_in_count:%"PRIu64" ", ms->byte_in_count);
1127     ds_put_cstr(s, "duration:");
1128     ofp_print_duration(s, ms->duration_sec, ms->duration_nsec);
1129     ds_put_char(s, ' ');
1130
1131     ds_put_cstr(s, "bands:\n");
1132     for (i = 0; i < ms->n_bands; ++i) {
1133         ds_put_format(s, "%d: ", i);
1134         ds_put_format(s, "packet_count:%"PRIu64" ", ms->bands[i].packet_count);
1135         ds_put_format(s, "byte_count:%"PRIu64"\n", ms->bands[i].byte_count);
1136     }
1137 }
1138
1139 static void
1140 ofp_print_meter_config(struct ds *s, const struct ofputil_meter_config *mc)
1141 {
1142     uint16_t i;
1143
1144     ds_put_format(s, "meter=%"PRIu32" ", mc->meter_id);
1145
1146     ofp_print_meter_flags(s, mc->flags);
1147
1148     ds_put_cstr(s, "bands=");
1149     for (i = 0; i < mc->n_bands; ++i) {
1150         ofp_print_meter_band(s, mc->flags, &mc->bands[i]);
1151     }
1152     ds_put_char(s, '\n');
1153 }
1154
1155 static void
1156 ofp_print_meter_mod(struct ds *s, const struct ofp_header *oh)
1157 {
1158     struct ofputil_meter_mod mm;
1159     struct ofpbuf bands;
1160     enum ofperr error;
1161
1162     ofpbuf_init(&bands, 64);
1163     error = ofputil_decode_meter_mod(oh, &mm, &bands);
1164     if (error) {
1165         ofpbuf_uninit(&bands);
1166         ofp_print_error(s, error);
1167         return;
1168     }
1169
1170     switch (mm.command) {
1171     case OFPMC13_ADD:
1172         ds_put_cstr(s, " ADD ");
1173         break;
1174     case OFPMC13_MODIFY:
1175         ds_put_cstr(s, " MOD ");
1176         break;
1177     case OFPMC13_DELETE:
1178         ds_put_cstr(s, " DEL ");
1179         break;
1180     default:
1181         ds_put_format(s, " cmd:%d ", mm.command);
1182     }
1183
1184     ofp_print_meter_config(s, &mm.meter);
1185     ofpbuf_uninit(&bands);
1186 }
1187
1188 static void
1189 ofp_print_meter_stats_request(struct ds *s, const struct ofp_header *oh)
1190 {
1191     uint32_t meter_id;
1192
1193     ofputil_decode_meter_request(oh, &meter_id);
1194
1195     ds_put_format(s, " meter=%"PRIu32, meter_id);
1196 }
1197
1198 static const char *
1199 ofputil_meter_capabilities_to_name(uint32_t bit)
1200 {
1201     enum ofp13_meter_flags flag = bit;
1202
1203     switch (flag) {
1204     case OFPMF13_KBPS:    return "kbps";
1205     case OFPMF13_PKTPS:   return "pktps";
1206     case OFPMF13_BURST:   return "burst";
1207     case OFPMF13_STATS:   return "stats";
1208     }
1209
1210     return NULL;
1211 }
1212
1213 static const char *
1214 ofputil_meter_band_types_to_name(uint32_t bit)
1215 {
1216     switch (bit) {
1217     case 1 << OFPMBT13_DROP:          return "drop";
1218     case 1 << OFPMBT13_DSCP_REMARK:   return "dscp_remark";
1219     }
1220
1221     return NULL;
1222 }
1223
1224 static void
1225 ofp_print_meter_features_reply(struct ds *s, const struct ofp_header *oh)
1226 {
1227     struct ofputil_meter_features mf;
1228
1229     ofputil_decode_meter_features(oh, &mf);
1230
1231     ds_put_format(s, "\nmax_meter:%"PRIu32, mf.max_meters);
1232     ds_put_format(s, " max_bands:%"PRIu8, mf.max_bands);
1233     ds_put_format(s, " max_color:%"PRIu8"\n", mf.max_color);
1234
1235     ds_put_cstr(s, "band_types: ");
1236     ofp_print_bit_names(s, mf.band_types,
1237                         ofputil_meter_band_types_to_name, ' ');
1238     ds_put_char(s, '\n');
1239
1240     ds_put_cstr(s, "capabilities: ");
1241     ofp_print_bit_names(s, mf.capabilities,
1242                         ofputil_meter_capabilities_to_name, ' ');
1243     ds_put_char(s, '\n');
1244 }
1245
1246 static void
1247 ofp_print_meter_config_reply(struct ds *s, const struct ofp_header *oh)
1248 {
1249     struct ofpbuf bands;
1250     struct ofpbuf b;
1251
1252     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1253     ofpbuf_init(&bands, 64);
1254     for (;;) {
1255         struct ofputil_meter_config mc;
1256         int retval;
1257
1258         retval = ofputil_decode_meter_config(&b, &mc, &bands);
1259         if (retval) {
1260             if (retval != EOF) {
1261                 ofp_print_error(s, retval);
1262             }
1263             break;
1264         }
1265         ds_put_char(s, '\n');
1266         ofp_print_meter_config(s, &mc);
1267     }
1268     ofpbuf_uninit(&bands);
1269 }
1270
1271 static void
1272 ofp_print_meter_stats_reply(struct ds *s, const struct ofp_header *oh)
1273 {
1274     struct ofpbuf bands;
1275     struct ofpbuf b;
1276
1277     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1278     ofpbuf_init(&bands, 64);
1279     for (;;) {
1280         struct ofputil_meter_stats ms;
1281         int retval;
1282
1283         retval = ofputil_decode_meter_stats(&b, &ms, &bands);
1284         if (retval) {
1285             if (retval != EOF) {
1286                 ofp_print_error(s, retval);
1287             }
1288             break;
1289         }
1290         ds_put_char(s, '\n');
1291         ofp_print_meter_stats(s, &ms);
1292     }
1293     ofpbuf_uninit(&bands);
1294 }
1295
1296 static void
1297 ofp_print_error(struct ds *string, enum ofperr error)
1298 {
1299     if (string->length) {
1300         ds_put_char(string, ' ');
1301     }
1302     ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1303 }
1304
1305 static void
1306 ofp_print_hello(struct ds *string, const struct ofp_header *oh)
1307 {
1308     uint32_t allowed_versions;
1309     bool ok;
1310
1311     ok = ofputil_decode_hello(oh, &allowed_versions);
1312
1313     ds_put_cstr(string, "\n version bitmap: ");
1314     ofputil_format_version_bitmap(string, allowed_versions);
1315
1316     if (!ok) {
1317         ds_put_cstr(string, "\n unknown data in hello:\n");
1318         ds_put_hex_dump(string, oh, ntohs(oh->length), 0, true);
1319     }
1320 }
1321
1322 static void
1323 ofp_print_error_msg(struct ds *string, const struct ofp_header *oh)
1324 {
1325     size_t len = ntohs(oh->length);
1326     struct ofpbuf payload;
1327     enum ofperr error;
1328     char *s;
1329
1330     error = ofperr_decode_msg(oh, &payload);
1331     if (!error) {
1332         ds_put_cstr(string, "***decode error***");
1333         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1334         return;
1335     }
1336
1337     ds_put_format(string, " %s\n", ofperr_get_name(error));
1338
1339     if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
1340         ds_put_printable(string, ofpbuf_data(&payload), ofpbuf_size(&payload));
1341     } else {
1342         s = ofp_to_string(ofpbuf_data(&payload), ofpbuf_size(&payload), 1);
1343         ds_put_cstr(string, s);
1344         free(s);
1345     }
1346     ofpbuf_uninit(&payload);
1347 }
1348
1349 static void
1350 ofp_print_port_status(struct ds *string, const struct ofp_header *oh)
1351 {
1352     struct ofputil_port_status ps;
1353     enum ofperr error;
1354
1355     error = ofputil_decode_port_status(oh, &ps);
1356     if (error) {
1357         ofp_print_error(string, error);
1358         return;
1359     }
1360
1361     if (ps.reason == OFPPR_ADD) {
1362         ds_put_format(string, " ADD:");
1363     } else if (ps.reason == OFPPR_DELETE) {
1364         ds_put_format(string, " DEL:");
1365     } else if (ps.reason == OFPPR_MODIFY) {
1366         ds_put_format(string, " MOD:");
1367     }
1368
1369     ofp_print_phy_port(string, &ps.desc);
1370 }
1371
1372 static void
1373 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
1374 {
1375     const struct ofp_desc_stats *ods = ofpmsg_body(oh);
1376
1377     ds_put_char(string, '\n');
1378     ds_put_format(string, "Manufacturer: %.*s\n",
1379             (int) sizeof ods->mfr_desc, ods->mfr_desc);
1380     ds_put_format(string, "Hardware: %.*s\n",
1381             (int) sizeof ods->hw_desc, ods->hw_desc);
1382     ds_put_format(string, "Software: %.*s\n",
1383             (int) sizeof ods->sw_desc, ods->sw_desc);
1384     ds_put_format(string, "Serial Num: %.*s\n",
1385             (int) sizeof ods->serial_num, ods->serial_num);
1386     ds_put_format(string, "DP Description: %.*s\n",
1387             (int) sizeof ods->dp_desc, ods->dp_desc);
1388 }
1389
1390 static void
1391 ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
1392 {
1393     struct ofputil_flow_stats_request fsr;
1394     enum ofperr error;
1395
1396     error = ofputil_decode_flow_stats_request(&fsr, oh);
1397     if (error) {
1398         ofp_print_error(string, error);
1399         return;
1400     }
1401
1402     if (fsr.table_id != 0xff) {
1403         ds_put_format(string, " table=%"PRIu8, fsr.table_id);
1404     }
1405
1406     if (fsr.out_port != OFPP_ANY) {
1407         ds_put_cstr(string, " out_port=");
1408         ofputil_format_port(fsr.out_port, string);
1409     }
1410
1411     ds_put_char(string, ' ');
1412     match_format(&fsr.match, string, OFP_DEFAULT_PRIORITY);
1413 }
1414
1415 void
1416 ofp_print_flow_stats(struct ds *string, struct ofputil_flow_stats *fs)
1417 {
1418     ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1419                   ntohll(fs->cookie));
1420
1421     ofp_print_duration(string, fs->duration_sec, fs->duration_nsec);
1422     ds_put_format(string, ", table=%"PRIu8", ", fs->table_id);
1423     ds_put_format(string, "n_packets=%"PRIu64", ", fs->packet_count);
1424     ds_put_format(string, "n_bytes=%"PRIu64", ", fs->byte_count);
1425     if (fs->idle_timeout != OFP_FLOW_PERMANENT) {
1426         ds_put_format(string, "idle_timeout=%"PRIu16", ", fs->idle_timeout);
1427     }
1428     if (fs->hard_timeout != OFP_FLOW_PERMANENT) {
1429         ds_put_format(string, "hard_timeout=%"PRIu16", ", fs->hard_timeout);
1430     }
1431     if (fs->flags) {
1432         ofp_print_flow_flags(string, fs->flags);
1433     }
1434     if (fs->idle_age >= 0) {
1435         ds_put_format(string, "idle_age=%d, ", fs->idle_age);
1436     }
1437     if (fs->hard_age >= 0 && fs->hard_age != fs->duration_sec) {
1438         ds_put_format(string, "hard_age=%d, ", fs->hard_age);
1439     }
1440
1441     match_format(&fs->match, string, fs->priority);
1442     if (string->string[string->length - 1] != ' ') {
1443         ds_put_char(string, ' ');
1444     }
1445
1446     ds_put_cstr(string, "actions=");
1447     ofpacts_format(fs->ofpacts, fs->ofpacts_len, string);
1448 }
1449
1450 static void
1451 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1452 {
1453     struct ofpbuf ofpacts;
1454     struct ofpbuf b;
1455
1456     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1457     ofpbuf_init(&ofpacts, 64);
1458     for (;;) {
1459         struct ofputil_flow_stats fs;
1460         int retval;
1461
1462         retval = ofputil_decode_flow_stats_reply(&fs, &b, true, &ofpacts);
1463         if (retval) {
1464             if (retval != EOF) {
1465                 ds_put_cstr(string, " ***parse error***");
1466             }
1467             break;
1468         }
1469         ds_put_char(string, '\n');
1470         ofp_print_flow_stats(string, &fs);
1471      }
1472     ofpbuf_uninit(&ofpacts);
1473 }
1474
1475 static void
1476 ofp_print_aggregate_stats_reply(struct ds *string, const struct ofp_header *oh)
1477 {
1478     struct ofputil_aggregate_stats as;
1479     enum ofperr error;
1480
1481     error = ofputil_decode_aggregate_stats_reply(&as, oh);
1482     if (error) {
1483         ofp_print_error(string, error);
1484         return;
1485     }
1486
1487     ds_put_format(string, " packet_count=%"PRIu64, as.packet_count);
1488     ds_put_format(string, " byte_count=%"PRIu64, as.byte_count);
1489     ds_put_format(string, " flow_count=%"PRIu32, as.flow_count);
1490 }
1491
1492 static void
1493 print_port_stat(struct ds *string, const char *leader, uint64_t stat, int more)
1494 {
1495     ds_put_cstr(string, leader);
1496     if (stat != UINT64_MAX) {
1497         ds_put_format(string, "%"PRIu64, stat);
1498     } else {
1499         ds_put_char(string, '?');
1500     }
1501     if (more) {
1502         ds_put_cstr(string, ", ");
1503     } else {
1504         ds_put_cstr(string, "\n");
1505     }
1506 }
1507
1508 static void
1509 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1510 {
1511     ofp_port_t ofp10_port;
1512     enum ofperr error;
1513
1514     error = ofputil_decode_port_stats_request(oh, &ofp10_port);
1515     if (error) {
1516         ofp_print_error(string, error);
1517         return;
1518     }
1519
1520     ds_put_cstr(string, " port_no=");
1521     ofputil_format_port(ofp10_port, string);
1522 }
1523
1524 static void
1525 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1526                            int verbosity)
1527 {
1528     struct ofpbuf b;
1529
1530     ds_put_format(string, " %"PRIuSIZE" ports\n", ofputil_count_port_stats(oh));
1531     if (verbosity < 1) {
1532         return;
1533     }
1534
1535     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1536     for (;;) {
1537         struct ofputil_port_stats ps;
1538         int retval;
1539
1540         retval = ofputil_decode_port_stats(&ps, &b);
1541         if (retval) {
1542             if (retval != EOF) {
1543                 ds_put_cstr(string, " ***parse error***");
1544             }
1545             return;
1546         }
1547
1548         ds_put_cstr(string, "  port ");
1549         if (ofp_to_u16(ps.port_no) < 10) {
1550             ds_put_char(string, ' ');
1551         }
1552         ofputil_format_port(ps.port_no, string);
1553
1554         ds_put_cstr(string, ": rx ");
1555         print_port_stat(string, "pkts=", ps.stats.rx_packets, 1);
1556         print_port_stat(string, "bytes=", ps.stats.rx_bytes, 1);
1557         print_port_stat(string, "drop=", ps.stats.rx_dropped, 1);
1558         print_port_stat(string, "errs=", ps.stats.rx_errors, 1);
1559         print_port_stat(string, "frame=", ps.stats.rx_frame_errors, 1);
1560         print_port_stat(string, "over=", ps.stats.rx_over_errors, 1);
1561         print_port_stat(string, "crc=", ps.stats.rx_crc_errors, 0);
1562
1563         ds_put_cstr(string, "           tx ");
1564         print_port_stat(string, "pkts=", ps.stats.tx_packets, 1);
1565         print_port_stat(string, "bytes=", ps.stats.tx_bytes, 1);
1566         print_port_stat(string, "drop=", ps.stats.tx_dropped, 1);
1567         print_port_stat(string, "errs=", ps.stats.tx_errors, 1);
1568         print_port_stat(string, "coll=", ps.stats.collisions, 0);
1569
1570         if (ps.duration_sec != UINT32_MAX) {
1571             ds_put_cstr(string, "           duration=");
1572             ofp_print_duration(string, ps.duration_sec, ps.duration_nsec);
1573             ds_put_char(string, '\n');
1574         }
1575     }
1576 }
1577
1578 static void
1579 ofp_print_table_stats_reply(struct ds *string, const struct ofp_header *oh)
1580 {
1581     struct ofpbuf b;
1582
1583     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1584     ofpraw_pull_assert(&b);
1585
1586     for (;;) {
1587         struct ofputil_table_features features;
1588         struct ofputil_table_stats stats;
1589         int retval;
1590
1591         retval = ofputil_decode_table_stats_reply(&b, &stats, &features);
1592         if (retval) {
1593             if (retval != EOF) {
1594                 ofp_print_error(string, retval);
1595             }
1596             return;
1597         }
1598
1599         ofp_print_table_features(string, &features, &stats);
1600     }
1601 }
1602
1603 static void
1604 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1605 {
1606     if (queue_id == OFPQ_ALL) {
1607         ds_put_cstr(string, "ALL");
1608     } else {
1609         ds_put_format(string, "%"PRIu32, queue_id);
1610     }
1611 }
1612
1613 static void
1614 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1615 {
1616     struct ofputil_queue_stats_request oqsr;
1617     enum ofperr error;
1618
1619     error = ofputil_decode_queue_stats_request(oh, &oqsr);
1620     if (error) {
1621         ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1622         return;
1623     }
1624
1625     ds_put_cstr(string, "port=");
1626     ofputil_format_port(oqsr.port_no, string);
1627
1628     ds_put_cstr(string, " queue=");
1629     ofp_print_queue_name(string, oqsr.queue_id);
1630 }
1631
1632 static void
1633 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1634                             int verbosity)
1635 {
1636     struct ofpbuf b;
1637
1638     ds_put_format(string, " %"PRIuSIZE" queues\n", ofputil_count_queue_stats(oh));
1639     if (verbosity < 1) {
1640         return;
1641     }
1642
1643     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1644     for (;;) {
1645         struct ofputil_queue_stats qs;
1646         int retval;
1647
1648         retval = ofputil_decode_queue_stats(&qs, &b);
1649         if (retval) {
1650             if (retval != EOF) {
1651                 ds_put_cstr(string, " ***parse error***");
1652             }
1653             return;
1654         }
1655
1656         ds_put_cstr(string, "  port ");
1657         ofputil_format_port(qs.port_no, string);
1658         ds_put_cstr(string, " queue ");
1659         ofp_print_queue_name(string, qs.queue_id);
1660         ds_put_cstr(string, ": ");
1661
1662         print_port_stat(string, "bytes=", qs.tx_bytes, 1);
1663         print_port_stat(string, "pkts=", qs.tx_packets, 1);
1664         print_port_stat(string, "errors=", qs.tx_errors, 1);
1665
1666         ds_put_cstr(string, "duration=");
1667         if (qs.duration_sec != UINT32_MAX) {
1668             ofp_print_duration(string, qs.duration_sec, qs.duration_nsec);
1669         } else {
1670             ds_put_char(string, '?');
1671         }
1672         ds_put_char(string, '\n');
1673     }
1674 }
1675
1676 static void
1677 ofp_print_ofpst_port_desc_request(struct ds *string,
1678                                   const struct ofp_header *oh)
1679 {
1680     enum ofperr error;
1681     ofp_port_t port;
1682
1683     error = ofputil_decode_port_desc_stats_request(oh, &port);
1684     if (error) {
1685         ofp_print_error(string, error);
1686         return;
1687     }
1688
1689     ds_put_cstr(string, " port=");
1690     ofputil_format_port(port, string);
1691 }
1692
1693 static void
1694 ofp_print_ofpst_port_desc_reply(struct ds *string,
1695                                 const struct ofp_header *oh)
1696 {
1697     struct ofpbuf b;
1698
1699     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1700     ofpraw_pull_assert(&b);
1701     ds_put_char(string, '\n');
1702     ofp_print_phy_ports(string, oh->version, &b);
1703 }
1704
1705 static void
1706 ofp_print_stats(struct ds *string, const struct ofp_header *oh)
1707 {
1708     uint16_t flags = ofpmp_flags(oh);
1709
1710     if (flags) {
1711         ds_put_cstr(string, " flags=");
1712         if ((!ofpmsg_is_stat_request(oh) || oh->version >= OFP13_VERSION)
1713             && (flags & OFPSF_REPLY_MORE)) {
1714             ds_put_cstr(string, "[more]");
1715             flags &= ~OFPSF_REPLY_MORE;
1716         }
1717         if (flags) {
1718             ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1719                           flags);
1720         }
1721     }
1722 }
1723
1724 static void
1725 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1726 {
1727     size_t len = ntohs(oh->length);
1728
1729     ds_put_format(string, " %"PRIuSIZE" bytes of payload\n", len - sizeof *oh);
1730     if (verbosity > 1) {
1731         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1732     }
1733 }
1734
1735 static void
1736 ofp_print_role_generic(struct ds *string, enum ofp12_controller_role role,
1737                        uint64_t generation_id)
1738 {
1739     ds_put_cstr(string, " role=");
1740
1741     switch (role) {
1742     case OFPCR12_ROLE_NOCHANGE:
1743         ds_put_cstr(string, "nochange");
1744         break;
1745     case OFPCR12_ROLE_EQUAL:
1746         ds_put_cstr(string, "equal"); /* OF 1.2 wording */
1747         break;
1748     case OFPCR12_ROLE_MASTER:
1749         ds_put_cstr(string, "master");
1750         break;
1751     case OFPCR12_ROLE_SLAVE:
1752         ds_put_cstr(string, "slave");
1753         break;
1754     default:
1755         OVS_NOT_REACHED();
1756     }
1757
1758     if (generation_id != UINT64_MAX) {
1759         ds_put_format(string, " generation_id=%"PRIu64, generation_id);
1760     }
1761 }
1762
1763 static void
1764 ofp_print_role_message(struct ds *string, const struct ofp_header *oh)
1765 {
1766     struct ofputil_role_request rr;
1767     enum ofperr error;
1768
1769     error = ofputil_decode_role_message(oh, &rr);
1770     if (error) {
1771         ofp_print_error(string, error);
1772         return;
1773     }
1774
1775     ofp_print_role_generic(string, rr.role, rr.have_generation_id ? rr.generation_id : UINT64_MAX);
1776 }
1777
1778 static void
1779 ofp_print_role_status_message(struct ds *string, const struct ofp_header *oh)
1780 {
1781     struct ofputil_role_status rs;
1782     enum ofperr error;
1783
1784     error = ofputil_decode_role_status(oh, &rs);
1785     if (error) {
1786         ofp_print_error(string, error);
1787         return;
1788     }
1789
1790     ofp_print_role_generic(string, rs.role, rs.generation_id);
1791
1792     ds_put_cstr(string, " reason=");
1793
1794     switch (rs.reason) {
1795     case OFPCRR_MASTER_REQUEST:
1796         ds_put_cstr(string, "master_request");
1797         break;
1798     case OFPCRR_CONFIG:
1799         ds_put_cstr(string, "configuration_changed");
1800         break;
1801     case OFPCRR_EXPERIMENTER:
1802         ds_put_cstr(string, "experimenter_data_changed");
1803         break;
1804     default:
1805         OVS_NOT_REACHED();
1806     }
1807 }
1808
1809 static void
1810 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1811                                 const struct nx_flow_mod_table_id *nfmti)
1812 {
1813     ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1814 }
1815
1816 static void
1817 ofp_print_nxt_set_flow_format(struct ds *string,
1818                               const struct nx_set_flow_format *nsff)
1819 {
1820     uint32_t format = ntohl(nsff->format);
1821
1822     ds_put_cstr(string, " format=");
1823     if (ofputil_nx_flow_format_is_valid(format)) {
1824         ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
1825     } else {
1826         ds_put_format(string, "%"PRIu32, format);
1827     }
1828 }
1829
1830 static void
1831 ofp_print_nxt_set_packet_in_format(struct ds *string,
1832                                    const struct nx_set_packet_in_format *nspf)
1833 {
1834     uint32_t format = ntohl(nspf->format);
1835
1836     ds_put_cstr(string, " format=");
1837     if (ofputil_packet_in_format_is_valid(format)) {
1838         ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1839     } else {
1840         ds_put_format(string, "%"PRIu32, format);
1841     }
1842 }
1843
1844 /* Returns a string form of 'reason'.  The return value is either a statically
1845  * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
1846  * 'bufsize' should be at least OFP_PORT_REASON_BUFSIZE. */
1847 #define OFP_PORT_REASON_BUFSIZE (INT_STRLEN(int) + 1)
1848 static const char *
1849 ofp_port_reason_to_string(enum ofp_port_reason reason,
1850                           char *reasonbuf, size_t bufsize)
1851 {
1852     switch (reason) {
1853     case OFPPR_ADD:
1854         return "add";
1855
1856     case OFPPR_DELETE:
1857         return "delete";
1858
1859     case OFPPR_MODIFY:
1860         return "modify";
1861
1862     default:
1863         snprintf(reasonbuf, bufsize, "%d", (int) reason);
1864         return reasonbuf;
1865     }
1866 }
1867
1868 static void
1869 ofp_print_nxt_set_async_config(struct ds *string,
1870                                const struct nx_async_config *nac)
1871 {
1872     int i;
1873
1874     for (i = 0; i < 2; i++) {
1875         int j;
1876
1877         ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1878
1879         ds_put_cstr(string, "       PACKET_IN:");
1880         for (j = 0; j < 32; j++) {
1881             if (nac->packet_in_mask[i] & htonl(1u << j)) {
1882                 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
1883                 const char *reason;
1884
1885                 reason = ofputil_packet_in_reason_to_string(j, reasonbuf,
1886                                                             sizeof reasonbuf);
1887                 ds_put_format(string, " %s", reason);
1888             }
1889         }
1890         if (!nac->packet_in_mask[i]) {
1891             ds_put_cstr(string, " (off)");
1892         }
1893         ds_put_char(string, '\n');
1894
1895         ds_put_cstr(string, "     PORT_STATUS:");
1896         for (j = 0; j < 32; j++) {
1897             if (nac->port_status_mask[i] & htonl(1u << j)) {
1898                 char reasonbuf[OFP_PORT_REASON_BUFSIZE];
1899                 const char *reason;
1900
1901                 reason = ofp_port_reason_to_string(j, reasonbuf,
1902                                                    sizeof reasonbuf);
1903                 ds_put_format(string, " %s", reason);
1904             }
1905         }
1906         if (!nac->port_status_mask[i]) {
1907             ds_put_cstr(string, " (off)");
1908         }
1909         ds_put_char(string, '\n');
1910
1911         ds_put_cstr(string, "    FLOW_REMOVED:");
1912         for (j = 0; j < 32; j++) {
1913             if (nac->flow_removed_mask[i] & htonl(1u << j)) {
1914                 char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
1915                 const char *reason;
1916
1917                 reason = ofp_flow_removed_reason_to_string(j, reasonbuf,
1918                                                            sizeof reasonbuf);
1919                 ds_put_format(string, " %s", reason);
1920             }
1921         }
1922         if (!nac->flow_removed_mask[i]) {
1923             ds_put_cstr(string, " (off)");
1924         }
1925         ds_put_char(string, '\n');
1926     }
1927 }
1928
1929 static void
1930 ofp_print_nxt_set_controller_id(struct ds *string,
1931                                 const struct nx_controller_id *nci)
1932 {
1933     ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
1934 }
1935
1936 static void
1937 ofp_print_nxt_flow_monitor_cancel(struct ds *string,
1938                                   const struct ofp_header *oh)
1939 {
1940     ds_put_format(string, " id=%"PRIu32,
1941                   ofputil_decode_flow_monitor_cancel(oh));
1942 }
1943
1944 static const char *
1945 nx_flow_monitor_flags_to_name(uint32_t bit)
1946 {
1947     enum nx_flow_monitor_flags fmf = bit;
1948
1949     switch (fmf) {
1950     case NXFMF_INITIAL: return "initial";
1951     case NXFMF_ADD: return "add";
1952     case NXFMF_DELETE: return "delete";
1953     case NXFMF_MODIFY: return "modify";
1954     case NXFMF_ACTIONS: return "actions";
1955     case NXFMF_OWN: return "own";
1956     }
1957
1958     return NULL;
1959 }
1960
1961 static void
1962 ofp_print_nxst_flow_monitor_request(struct ds *string,
1963                                     const struct ofp_header *oh)
1964 {
1965     struct ofpbuf b;
1966
1967     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1968     for (;;) {
1969         struct ofputil_flow_monitor_request request;
1970         int retval;
1971
1972         retval = ofputil_decode_flow_monitor_request(&request, &b);
1973         if (retval) {
1974             if (retval != EOF) {
1975                 ofp_print_error(string, retval);
1976             }
1977             return;
1978         }
1979
1980         ds_put_format(string, "\n id=%"PRIu32" flags=", request.id);
1981         ofp_print_bit_names(string, request.flags,
1982                             nx_flow_monitor_flags_to_name, ',');
1983
1984         if (request.out_port != OFPP_NONE) {
1985             ds_put_cstr(string, " out_port=");
1986             ofputil_format_port(request.out_port, string);
1987         }
1988
1989         if (request.table_id != 0xff) {
1990             ds_put_format(string, " table=%"PRIu8, request.table_id);
1991         }
1992
1993         ds_put_char(string, ' ');
1994         match_format(&request.match, string, OFP_DEFAULT_PRIORITY);
1995         ds_chomp(string, ' ');
1996     }
1997 }
1998
1999 static void
2000 ofp_print_nxst_flow_monitor_reply(struct ds *string,
2001                                   const struct ofp_header *oh)
2002 {
2003     uint64_t ofpacts_stub[1024 / 8];
2004     struct ofpbuf ofpacts;
2005     struct ofpbuf b;
2006
2007     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2008     ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2009     for (;;) {
2010         char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
2011         struct ofputil_flow_update update;
2012         struct match match;
2013         int retval;
2014
2015         update.match = &match;
2016         retval = ofputil_decode_flow_update(&update, &b, &ofpacts);
2017         if (retval) {
2018             if (retval != EOF) {
2019                 ofp_print_error(string, retval);
2020             }
2021             ofpbuf_uninit(&ofpacts);
2022             return;
2023         }
2024
2025         ds_put_cstr(string, "\n event=");
2026         switch (update.event) {
2027         case NXFME_ADDED:
2028             ds_put_cstr(string, "ADDED");
2029             break;
2030
2031         case NXFME_DELETED:
2032             ds_put_format(string, "DELETED reason=%s",
2033                           ofp_flow_removed_reason_to_string(update.reason,
2034                                                             reasonbuf,
2035                                                             sizeof reasonbuf));
2036             break;
2037
2038         case NXFME_MODIFIED:
2039             ds_put_cstr(string, "MODIFIED");
2040             break;
2041
2042         case NXFME_ABBREV:
2043             ds_put_format(string, "ABBREV xid=0x%"PRIx32, ntohl(update.xid));
2044             continue;
2045         }
2046
2047         ds_put_format(string, " table=%"PRIu8, update.table_id);
2048         if (update.idle_timeout != OFP_FLOW_PERMANENT) {
2049             ds_put_format(string, " idle_timeout=%"PRIu16,
2050                           update.idle_timeout);
2051         }
2052         if (update.hard_timeout != OFP_FLOW_PERMANENT) {
2053             ds_put_format(string, " hard_timeout=%"PRIu16,
2054                           update.hard_timeout);
2055         }
2056         ds_put_format(string, " cookie=%#"PRIx64, ntohll(update.cookie));
2057
2058         ds_put_char(string, ' ');
2059         match_format(update.match, string, OFP_DEFAULT_PRIORITY);
2060
2061         if (update.ofpacts_len) {
2062             if (string->string[string->length - 1] != ' ') {
2063                 ds_put_char(string, ' ');
2064             }
2065             ds_put_cstr(string, "actions=");
2066             ofpacts_format(update.ofpacts, update.ofpacts_len, string);
2067         }
2068     }
2069 }
2070
2071 void
2072 ofp_print_version(const struct ofp_header *oh,
2073                   struct ds *string)
2074 {
2075     switch (oh->version) {
2076     case OFP10_VERSION:
2077         break;
2078     case OFP11_VERSION:
2079         ds_put_cstr(string, " (OF1.1)");
2080         break;
2081     case OFP12_VERSION:
2082         ds_put_cstr(string, " (OF1.2)");
2083         break;
2084     case OFP13_VERSION:
2085         ds_put_cstr(string, " (OF1.3)");
2086         break;
2087     case OFP14_VERSION:
2088         ds_put_cstr(string, " (OF1.4)");
2089         break;
2090     case OFP15_VERSION:
2091         ds_put_cstr(string, " (OF1.5)");
2092         break;
2093     default:
2094         ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
2095         break;
2096     }
2097     ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
2098 }
2099
2100 static void
2101 ofp_header_to_string__(const struct ofp_header *oh, enum ofpraw raw,
2102                        struct ds *string)
2103 {
2104     ds_put_cstr(string, ofpraw_get_name(raw));
2105     ofp_print_version(oh, string);
2106 }
2107
2108 static void
2109 ofp_print_group(struct ds *s, uint32_t group_id, uint8_t type,
2110                 struct list *p_buckets)
2111 {
2112     static const char *type_str[] = { "all", "select", "indirect",
2113                                       "ff", "unknown" };
2114     struct ofputil_bucket *bucket;
2115
2116     ds_put_format(s, "group_id=%"PRIu32",type=%s",
2117                   group_id, type_str[type > 4 ? 4 : type]);
2118     if (!p_buckets) {
2119         return;
2120     }
2121
2122     LIST_FOR_EACH (bucket, list_node, p_buckets) {
2123         ds_put_cstr(s, ",bucket=");
2124
2125         if (bucket->weight != 1) {
2126             ds_put_format(s, "weight:%"PRIu16",", bucket->weight);
2127         }
2128         if (bucket->watch_port != OFPP_NONE) {
2129             ds_put_format(s, "watch_port:%"PRIu32",", bucket->watch_port);
2130         }
2131         if (bucket->watch_group != OFPG11_ANY) {
2132             ds_put_format(s, "watch_group:%"PRIu32",", bucket->watch_group);
2133         }
2134
2135         ds_put_cstr(s, "actions=");
2136         ofpacts_format(bucket->ofpacts, bucket->ofpacts_len, s);
2137     }
2138 }
2139
2140 static void
2141 ofp_print_ofpst_group_desc_request(struct ds *string,
2142                                    const struct ofp_header *oh)
2143 {
2144     uint32_t group_id = ofputil_decode_group_desc_request(oh);
2145     ds_put_cstr(string, " group_id=");
2146     ofputil_format_group(group_id, string);
2147 }
2148
2149 static void
2150 ofp_print_group_desc(struct ds *s, const struct ofp_header *oh)
2151 {
2152     struct ofpbuf b;
2153
2154     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2155     for (;;) {
2156         struct ofputil_group_desc gd;
2157         int retval;
2158
2159         retval = ofputil_decode_group_desc_reply(&gd, &b, oh->version);
2160         if (retval) {
2161             if (retval != EOF) {
2162                 ds_put_cstr(s, " ***parse error***");
2163             }
2164             break;
2165         }
2166
2167         ds_put_char(s, '\n');
2168         ds_put_char(s, ' ');
2169         ofp_print_group(s, gd.group_id, gd.type, &gd.buckets);
2170         ofputil_bucket_list_destroy(&gd.buckets);
2171      }
2172 }
2173
2174 static void
2175 ofp_print_ofpst_group_request(struct ds *string, const struct ofp_header *oh)
2176 {
2177     enum ofperr error;
2178     uint32_t group_id;
2179
2180     error = ofputil_decode_group_stats_request(oh, &group_id);
2181     if (error) {
2182         ofp_print_error(string, error);
2183         return;
2184     }
2185
2186     ds_put_cstr(string, " group_id=");
2187     ofputil_format_group(group_id, string);
2188 }
2189
2190 static void
2191 ofp_print_group_stats(struct ds *s, const struct ofp_header *oh)
2192 {
2193     struct ofpbuf b;
2194     uint32_t bucket_i;
2195
2196     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2197
2198     for (;;) {
2199         struct ofputil_group_stats gs;
2200         int retval;
2201
2202         retval = ofputil_decode_group_stats_reply(&b, &gs);
2203         if (retval) {
2204             if (retval != EOF) {
2205                 ds_put_cstr(s, " ***parse error***");
2206             }
2207             break;
2208         }
2209
2210         ds_put_char(s, '\n');
2211
2212         ds_put_char(s, ' ');
2213         ds_put_format(s, "group_id=%"PRIu32",", gs.group_id);
2214
2215         if (gs.duration_sec != UINT32_MAX) {
2216             ds_put_cstr(s, "duration=");
2217             ofp_print_duration(s, gs.duration_sec, gs.duration_nsec);
2218             ds_put_char(s, ',');
2219         }
2220         ds_put_format(s, "ref_count=%"PRIu32",", gs.ref_count);
2221         ds_put_format(s, "packet_count=%"PRIu64",", gs.packet_count);
2222         ds_put_format(s, "byte_count=%"PRIu64"", gs.byte_count);
2223
2224         for (bucket_i = 0; bucket_i < gs.n_buckets; bucket_i++) {
2225             if (gs.bucket_stats[bucket_i].packet_count != UINT64_MAX) {
2226                 ds_put_format(s, ",bucket%"PRIu32":", bucket_i);
2227                 ds_put_format(s, "packet_count=%"PRIu64",", gs.bucket_stats[bucket_i].packet_count);
2228                 ds_put_format(s, "byte_count=%"PRIu64"", gs.bucket_stats[bucket_i].byte_count);
2229             }
2230         }
2231
2232         free(gs.bucket_stats);
2233      }
2234 }
2235
2236 static const char *
2237 group_type_to_string(enum ofp11_group_type type)
2238 {
2239     switch (type) {
2240     case OFPGT11_ALL: return "all";
2241     case OFPGT11_SELECT: return "select";
2242     case OFPGT11_INDIRECT: return "indirect";
2243     case OFPGT11_FF: return "fast failover";
2244     default: OVS_NOT_REACHED();
2245     }
2246 }
2247
2248 static void
2249 ofp_print_group_features(struct ds *string, const struct ofp_header *oh)
2250 {
2251     struct ofputil_group_features features;
2252     int i;
2253
2254     ofputil_decode_group_features_reply(oh, &features);
2255
2256     ds_put_format(string, "\n Group table:\n");
2257     ds_put_format(string, "    Types:  0x%"PRIx32"\n", features.types);
2258     ds_put_format(string, "    Capabilities:  0x%"PRIx32"\n",
2259                   features.capabilities);
2260
2261     for (i = 0; i < OFPGT12_N_TYPES; i++) {
2262         if (features.types & (1u << i)) {
2263             ds_put_format(string, "    %s group:\n", group_type_to_string(i));
2264             ds_put_format(string, "       max_groups=%#"PRIx32"\n",
2265                           features.max_groups[i]);
2266             ds_put_format(string, "       actions: ");
2267             ofpact_bitmap_format(features.ofpacts[i], string);
2268             ds_put_char(string, '\n');
2269         }
2270     }
2271 }
2272
2273 static void
2274 ofp_print_group_mod(struct ds *s, const struct ofp_header *oh)
2275 {
2276     struct ofputil_group_mod gm;
2277     int error;
2278
2279     error = ofputil_decode_group_mod(oh, &gm);
2280     if (error) {
2281         ofp_print_error(s, error);
2282         return;
2283     }
2284
2285     ds_put_char(s, '\n');
2286
2287     ds_put_char(s, ' ');
2288     switch (gm.command) {
2289     case OFPGC11_ADD:
2290         ds_put_cstr(s, "ADD");
2291         break;
2292
2293     case OFPGC11_MODIFY:
2294         ds_put_cstr(s, "MOD");
2295         break;
2296
2297     case OFPGC11_DELETE:
2298         ds_put_cstr(s, "DEL");
2299         break;
2300
2301     default:
2302         ds_put_format(s, "cmd:%"PRIu16"", gm.command);
2303     }
2304     ds_put_char(s, ' ');
2305
2306     ofp_print_group(s, gm.group_id, gm.type, &gm.buckets);
2307     ofputil_bucket_list_destroy(&gm.buckets);
2308 }
2309
2310 static void
2311 print_table_action_features(struct ds *s,
2312                             const struct ofputil_table_action_features *taf)
2313 {
2314     if (taf->ofpacts) {
2315         ds_put_cstr(s, "        actions: ");
2316         ofpact_bitmap_format(taf->ofpacts, s);
2317         ds_put_char(s, '\n');
2318     }
2319
2320     if (!bitmap_is_all_zeros(taf->set_fields.bm, MFF_N_IDS)) {
2321         int i;
2322
2323         ds_put_cstr(s, "        supported on Set-Field:");
2324         BITMAP_FOR_EACH_1 (i, MFF_N_IDS, taf->set_fields.bm) {
2325             ds_put_format(s, " %s", mf_from_id(i)->name);
2326         }
2327         ds_put_char(s, '\n');
2328     }
2329 }
2330
2331 static bool
2332 table_action_features_equal(const struct ofputil_table_action_features *a,
2333                             const struct ofputil_table_action_features *b)
2334 {
2335     return (a->ofpacts == b->ofpacts
2336             && bitmap_equal(a->set_fields.bm, b->set_fields.bm, MFF_N_IDS));
2337 }
2338
2339 static bool
2340 table_action_features_empty(const struct ofputil_table_action_features *taf)
2341 {
2342     return !taf->ofpacts && bitmap_is_all_zeros(taf->set_fields.bm, MFF_N_IDS);
2343 }
2344
2345 static void
2346 print_table_instruction_features(
2347     struct ds *s, const struct ofputil_table_instruction_features *tif)
2348 {
2349     int start, end;
2350
2351     if (!bitmap_is_all_zeros(tif->next, 255)) {
2352         ds_put_cstr(s, "      next tables: ");
2353         for (start = bitmap_scan(tif->next, 1, 0, 255); start < 255;
2354              start = bitmap_scan(tif->next, 1, end, 255)) {
2355             end = bitmap_scan(tif->next, 0, start + 1, 255);
2356             if (end == start + 1) {
2357                 ds_put_format(s, "%d,", start);
2358             } else {
2359                 ds_put_format(s, "%d-%d,", start, end - 1);
2360             }
2361         }
2362         ds_chomp(s, ',');
2363         if (ds_last(s) == ' ') {
2364             ds_put_cstr(s, "none");
2365         }
2366         ds_put_char(s, '\n');
2367     }
2368
2369     if (tif->instructions) {
2370         ds_put_cstr(s, "      instructions: ");
2371         int i;
2372
2373         for (i = 0; i < 32; i++) {
2374             if (tif->instructions & (1u << i)) {
2375                 ds_put_format(s, "%s,", ovs_instruction_name_from_type(i));
2376             }
2377         }
2378         ds_chomp(s, ',');
2379         ds_put_char(s, '\n');
2380     }
2381
2382     if (!table_action_features_equal(&tif->write, &tif->apply)) {
2383         ds_put_cstr(s, "      Write-Actions features:\n");
2384         print_table_action_features(s, &tif->write);
2385         ds_put_cstr(s, "      Apply-Actions features:\n");
2386         print_table_action_features(s, &tif->apply);
2387     } else if (tif->write.ofpacts
2388                || !bitmap_is_all_zeros(tif->write.set_fields.bm, MFF_N_IDS)) {
2389         ds_put_cstr(s, "      Write-Actions and Apply-Actions features:\n");
2390         print_table_action_features(s, &tif->write);
2391     }
2392 }
2393
2394 static bool
2395 table_instruction_features_equal(
2396     const struct ofputil_table_instruction_features *a,
2397     const struct ofputil_table_instruction_features *b)
2398 {
2399     return (bitmap_equal(a->next, b->next, 255)
2400             && a->instructions == b->instructions
2401             && table_action_features_equal(&a->write, &b->write)
2402             && table_action_features_equal(&a->apply, &b->apply));
2403 }
2404
2405 static bool
2406 table_instruction_features_empty(
2407     const struct ofputil_table_instruction_features *tif)
2408 {
2409     return (bitmap_is_all_zeros(tif->next, 255)
2410             && !tif->instructions
2411             && table_action_features_empty(&tif->write)
2412             && table_action_features_empty(&tif->apply));
2413 }
2414
2415 static void
2416 ofp_print_table_features(struct ds *s,
2417                          const struct ofputil_table_features *features,
2418                          const struct ofputil_table_stats *stats)
2419 {
2420     int i;
2421
2422     ds_put_format(s, "\n  table %"PRIu8, features->table_id);
2423     if (features->name[0]) {
2424         ds_put_format(s, " (\"%s\")", features->name);
2425     }
2426     ds_put_cstr(s, ":\n");
2427     if (stats) {
2428         ds_put_format(s, "    active=%"PRIu32", ", stats->active_count);
2429         ds_put_format(s, "lookup=%"PRIu64", ", stats->lookup_count);
2430         ds_put_format(s, "matched=%"PRIu64"\n", stats->matched_count);
2431     }
2432     if (features->metadata_match || features->metadata_match) {
2433         ds_put_format(s, "    metadata: match=%#"PRIx64" write=%#"PRIx64"\n",
2434                       ntohll(features->metadata_match),
2435                       ntohll(features->metadata_write));
2436     }
2437
2438     if (features->miss_config != OFPUTIL_TABLE_MISS_DEFAULT) {
2439         ds_put_cstr(s, "    config=");
2440         ofp_print_table_miss_config(s, features->miss_config);
2441     }
2442
2443     if (features->max_entries) {
2444         ds_put_format(s, "    max_entries=%"PRIu32"\n", features->max_entries);
2445     }
2446
2447     if (!table_instruction_features_equal(&features->nonmiss,
2448                                           &features->miss)) {
2449         ds_put_cstr(s, "    instructions (other than table miss):\n");
2450         print_table_instruction_features(s, &features->nonmiss);
2451         ds_put_cstr(s, "    instructions (table miss):\n");
2452         print_table_instruction_features(s, &features->miss);
2453     } else if (!table_instruction_features_empty(&features->nonmiss)) {
2454         ds_put_cstr(s, "    instructions (table miss and others):\n");
2455         print_table_instruction_features(s, &features->nonmiss);
2456     }
2457
2458     if (!bitmap_is_all_zeros(features->match.bm, MFF_N_IDS)){
2459         ds_put_cstr(s, "    matching:\n");
2460         BITMAP_FOR_EACH_1 (i, MFF_N_IDS, features->match.bm) {
2461             const struct mf_field *f = mf_from_id(i);
2462             bool mask = bitmap_is_set(features->mask.bm, i);
2463             bool wildcard = bitmap_is_set(features->wildcard.bm, i);
2464
2465             ds_put_format(s, "      %s: %s\n",
2466                           f->name,
2467                           (mask ? "arbitrary mask"
2468                            : wildcard ? "exact match or wildcard"
2469                            : "must exact match"));
2470         }
2471     }
2472 }
2473
2474 static void
2475 ofp_print_table_features_reply(struct ds *s, const struct ofp_header *oh)
2476 {
2477     struct ofpbuf b;
2478
2479     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2480
2481     for (;;) {
2482         struct ofputil_table_features tf;
2483         int retval;
2484
2485         retval = ofputil_decode_table_features(&b, &tf, true);
2486         if (retval) {
2487             if (retval != EOF) {
2488                 ofp_print_error(s, retval);
2489             }
2490             return;
2491         }
2492         ofp_print_table_features(s, &tf, NULL);
2493     }
2494 }
2495
2496 static const char *
2497 bundle_flags_to_name(uint32_t bit)
2498 {
2499     switch (bit) {
2500     case OFPBF_ATOMIC:
2501         return "atomic";
2502     case OFPBF_ORDERED:
2503         return "ordered";
2504     default:
2505         return NULL;
2506     }
2507 }
2508
2509 static void
2510 ofp_print_bundle_ctrl(struct ds *s, const struct ofp_header *oh)
2511 {
2512     int error;
2513     struct ofputil_bundle_ctrl_msg bctrl;
2514
2515     error = ofputil_decode_bundle_ctrl(oh, &bctrl);
2516     if (error) {
2517         ofp_print_error(s, error);
2518         return;
2519     }
2520
2521     ds_put_char(s, '\n');
2522
2523     ds_put_format(s, " bundle_id=%#"PRIx32" type=",  bctrl.bundle_id);
2524     switch (bctrl.type) {
2525     case OFPBCT_OPEN_REQUEST:
2526         ds_put_cstr(s, "OPEN_REQUEST");
2527         break;
2528     case OFPBCT_OPEN_REPLY:
2529         ds_put_cstr(s, "OPEN_REPLY");
2530         break;
2531     case OFPBCT_CLOSE_REQUEST:
2532         ds_put_cstr(s, "CLOSE_REQUEST");
2533         break;
2534     case OFPBCT_CLOSE_REPLY:
2535         ds_put_cstr(s, "CLOSE_REPLY");
2536         break;
2537     case OFPBCT_COMMIT_REQUEST:
2538         ds_put_cstr(s, "COMMIT_REQUEST");
2539         break;
2540     case OFPBCT_COMMIT_REPLY:
2541         ds_put_cstr(s, "COMMIT_REPLY");
2542         break;
2543     case OFPBCT_DISCARD_REQUEST:
2544         ds_put_cstr(s, "DISCARD_REQUEST");
2545         break;
2546     case OFPBCT_DISCARD_REPLY:
2547         ds_put_cstr(s, "DISCARD_REPLY");
2548         break;
2549     }
2550
2551     ds_put_cstr(s, " flags=");
2552     ofp_print_bit_names(s, bctrl.flags, bundle_flags_to_name, ' ');
2553 }
2554
2555 static void
2556 ofp_print_bundle_add(struct ds *s, const struct ofp_header *oh, int verbosity)
2557 {
2558     int error;
2559     struct ofputil_bundle_add_msg badd;
2560     char *msg;
2561
2562     error = ofputil_decode_bundle_add(oh, &badd);
2563     if (error) {
2564         ofp_print_error(s, error);
2565         return;
2566     }
2567
2568     ds_put_char(s, '\n');
2569     ds_put_format(s, " bundle_id=%#"PRIx32,  badd.bundle_id);
2570     ds_put_cstr(s, " flags=");
2571     ofp_print_bit_names(s, badd.flags, bundle_flags_to_name, ' ');
2572
2573     ds_put_char(s, '\n');
2574     msg = ofp_to_string(badd.msg, ntohs(badd.msg->length), verbosity);
2575     if (msg) {
2576         ds_put_cstr(s, msg);
2577     }
2578 }
2579
2580 static void
2581 ofp_to_string__(const struct ofp_header *oh, enum ofpraw raw,
2582                 struct ds *string, int verbosity)
2583 {
2584     const void *msg = oh;
2585
2586     ofp_header_to_string__(oh, raw, string);
2587     switch (ofptype_from_ofpraw(raw)) {
2588
2589     case OFPTYPE_GROUP_STATS_REQUEST:
2590         ofp_print_stats(string, oh);
2591         ofp_print_ofpst_group_request(string, oh);
2592         break;
2593
2594     case OFPTYPE_GROUP_STATS_REPLY:
2595         ofp_print_group_stats(string, oh);
2596         break;
2597
2598     case OFPTYPE_GROUP_DESC_STATS_REQUEST:
2599         ofp_print_stats(string, oh);
2600         ofp_print_ofpst_group_desc_request(string, oh);
2601         break;
2602
2603     case OFPTYPE_GROUP_DESC_STATS_REPLY:
2604         ofp_print_group_desc(string, oh);
2605         break;
2606
2607     case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
2608         ofp_print_stats(string, oh);
2609         break;
2610
2611     case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
2612         ofp_print_group_features(string, oh);
2613         break;
2614
2615     case OFPTYPE_GROUP_MOD:
2616         ofp_print_group_mod(string, oh);
2617         break;
2618
2619     case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
2620     case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
2621         ofp_print_table_features_reply(string, oh);
2622         break;
2623
2624     case OFPTYPE_HELLO:
2625         ofp_print_hello(string, oh);
2626         break;
2627
2628     case OFPTYPE_ERROR:
2629         ofp_print_error_msg(string, oh);
2630         break;
2631
2632     case OFPTYPE_ECHO_REQUEST:
2633     case OFPTYPE_ECHO_REPLY:
2634         ofp_print_echo(string, oh, verbosity);
2635         break;
2636
2637     case OFPTYPE_FEATURES_REQUEST:
2638         break;
2639
2640     case OFPTYPE_FEATURES_REPLY:
2641         ofp_print_switch_features(string, oh);
2642         break;
2643
2644     case OFPTYPE_GET_CONFIG_REQUEST:
2645         break;
2646
2647     case OFPTYPE_GET_CONFIG_REPLY:
2648     case OFPTYPE_SET_CONFIG:
2649         ofp_print_switch_config(string, ofpmsg_body(oh));
2650         break;
2651
2652     case OFPTYPE_PACKET_IN:
2653         ofp_print_packet_in(string, oh, verbosity);
2654         break;
2655
2656     case OFPTYPE_FLOW_REMOVED:
2657         ofp_print_flow_removed(string, oh);
2658         break;
2659
2660     case OFPTYPE_PORT_STATUS:
2661         ofp_print_port_status(string, oh);
2662         break;
2663
2664     case OFPTYPE_PACKET_OUT:
2665         ofp_print_packet_out(string, oh, verbosity);
2666         break;
2667
2668     case OFPTYPE_FLOW_MOD:
2669         ofp_print_flow_mod(string, oh, verbosity);
2670         break;
2671
2672     case OFPTYPE_PORT_MOD:
2673         ofp_print_port_mod(string, oh);
2674         break;
2675
2676     case OFPTYPE_TABLE_MOD:
2677         ofp_print_table_mod(string, oh);
2678         break;
2679
2680     case OFPTYPE_METER_MOD:
2681         ofp_print_meter_mod(string, oh);
2682         break;
2683
2684     case OFPTYPE_BARRIER_REQUEST:
2685     case OFPTYPE_BARRIER_REPLY:
2686         break;
2687
2688     case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
2689         ofp_print_queue_get_config_request(string, oh);
2690         break;
2691
2692     case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
2693         ofp_print_queue_get_config_reply(string, oh);
2694         break;
2695
2696     case OFPTYPE_ROLE_REQUEST:
2697     case OFPTYPE_ROLE_REPLY:
2698         ofp_print_role_message(string, oh);
2699         break;
2700     case OFPTYPE_ROLE_STATUS:
2701         ofp_print_role_status_message(string, oh);
2702         break;
2703
2704     case OFPTYPE_METER_STATS_REQUEST:
2705     case OFPTYPE_METER_CONFIG_STATS_REQUEST:
2706         ofp_print_stats(string, oh);
2707         ofp_print_meter_stats_request(string, oh);
2708         break;
2709
2710     case OFPTYPE_METER_STATS_REPLY:
2711         ofp_print_stats(string, oh);
2712         ofp_print_meter_stats_reply(string, oh);
2713         break;
2714
2715     case OFPTYPE_METER_CONFIG_STATS_REPLY:
2716         ofp_print_stats(string, oh);
2717         ofp_print_meter_config_reply(string, oh);
2718         break;
2719
2720     case OFPTYPE_METER_FEATURES_STATS_REPLY:
2721         ofp_print_stats(string, oh);
2722         ofp_print_meter_features_reply(string, oh);
2723         break;
2724
2725     case OFPTYPE_DESC_STATS_REQUEST:
2726     case OFPTYPE_METER_FEATURES_STATS_REQUEST:
2727         ofp_print_stats(string, oh);
2728         break;
2729
2730     case OFPTYPE_FLOW_STATS_REQUEST:
2731     case OFPTYPE_AGGREGATE_STATS_REQUEST:
2732         ofp_print_stats(string, oh);
2733         ofp_print_flow_stats_request(string, oh);
2734         break;
2735
2736     case OFPTYPE_TABLE_STATS_REQUEST:
2737         ofp_print_stats(string, oh);
2738         break;
2739
2740     case OFPTYPE_PORT_STATS_REQUEST:
2741         ofp_print_stats(string, oh);
2742         ofp_print_ofpst_port_request(string, oh);
2743         break;
2744
2745     case OFPTYPE_QUEUE_STATS_REQUEST:
2746         ofp_print_stats(string, oh);
2747         ofp_print_ofpst_queue_request(string, oh);
2748         break;
2749
2750     case OFPTYPE_DESC_STATS_REPLY:
2751         ofp_print_stats(string, oh);
2752         ofp_print_ofpst_desc_reply(string, oh);
2753         break;
2754
2755     case OFPTYPE_FLOW_STATS_REPLY:
2756         ofp_print_stats(string, oh);
2757         ofp_print_flow_stats_reply(string, oh);
2758         break;
2759
2760     case OFPTYPE_QUEUE_STATS_REPLY:
2761         ofp_print_stats(string, oh);
2762         ofp_print_ofpst_queue_reply(string, oh, verbosity);
2763         break;
2764
2765     case OFPTYPE_PORT_STATS_REPLY:
2766         ofp_print_stats(string, oh);
2767         ofp_print_ofpst_port_reply(string, oh, verbosity);
2768         break;
2769
2770     case OFPTYPE_TABLE_STATS_REPLY:
2771         ofp_print_stats(string, oh);
2772         ofp_print_table_stats_reply(string, oh);
2773         break;
2774
2775     case OFPTYPE_AGGREGATE_STATS_REPLY:
2776         ofp_print_stats(string, oh);
2777         ofp_print_aggregate_stats_reply(string, oh);
2778         break;
2779
2780     case OFPTYPE_PORT_DESC_STATS_REQUEST:
2781         ofp_print_stats(string, oh);
2782         ofp_print_ofpst_port_desc_request(string, oh);
2783         break;
2784
2785     case OFPTYPE_PORT_DESC_STATS_REPLY:
2786         ofp_print_stats(string, oh);
2787         ofp_print_ofpst_port_desc_reply(string, oh);
2788         break;
2789
2790     case OFPTYPE_FLOW_MOD_TABLE_ID:
2791         ofp_print_nxt_flow_mod_table_id(string, ofpmsg_body(oh));
2792         break;
2793
2794     case OFPTYPE_SET_FLOW_FORMAT:
2795         ofp_print_nxt_set_flow_format(string, ofpmsg_body(oh));
2796         break;
2797
2798     case OFPTYPE_SET_PACKET_IN_FORMAT:
2799         ofp_print_nxt_set_packet_in_format(string, ofpmsg_body(oh));
2800         break;
2801
2802     case OFPTYPE_FLOW_AGE:
2803         break;
2804
2805     case OFPTYPE_SET_CONTROLLER_ID:
2806         ofp_print_nxt_set_controller_id(string, ofpmsg_body(oh));
2807         break;
2808
2809     case OFPTYPE_GET_ASYNC_REPLY:
2810     case OFPTYPE_SET_ASYNC_CONFIG:
2811         ofp_print_nxt_set_async_config(string, ofpmsg_body(oh));
2812         break;
2813     case OFPTYPE_GET_ASYNC_REQUEST:
2814         break;
2815     case OFPTYPE_FLOW_MONITOR_CANCEL:
2816         ofp_print_nxt_flow_monitor_cancel(string, msg);
2817         break;
2818
2819     case OFPTYPE_FLOW_MONITOR_PAUSED:
2820     case OFPTYPE_FLOW_MONITOR_RESUMED:
2821         break;
2822
2823     case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
2824         ofp_print_nxst_flow_monitor_request(string, msg);
2825         break;
2826
2827     case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
2828         ofp_print_nxst_flow_monitor_reply(string, msg);
2829         break;
2830
2831     case OFPTYPE_BUNDLE_CONTROL:
2832         ofp_print_bundle_ctrl(string, msg);
2833         break;
2834
2835     case OFPTYPE_BUNDLE_ADD_MESSAGE:
2836         ofp_print_bundle_add(string, msg, verbosity);
2837         break;
2838     }
2839 }
2840
2841 /* Composes and returns a string representing the OpenFlow packet of 'len'
2842  * bytes at 'oh' at the given 'verbosity' level.  0 is a minimal amount of
2843  * verbosity and higher numbers increase verbosity.  The caller is responsible
2844  * for freeing the string. */
2845 char *
2846 ofp_to_string(const void *oh_, size_t len, int verbosity)
2847 {
2848     struct ds string = DS_EMPTY_INITIALIZER;
2849     const struct ofp_header *oh = oh_;
2850
2851     if (!len) {
2852         ds_put_cstr(&string, "OpenFlow message is empty\n");
2853     } else if (len < sizeof(struct ofp_header)) {
2854         ds_put_format(&string, "OpenFlow packet too short (only %"PRIuSIZE" bytes):\n",
2855                       len);
2856     } else if (ntohs(oh->length) > len) {
2857         enum ofperr error;
2858         enum ofpraw raw;
2859
2860         error = ofpraw_decode_partial(&raw, oh, len);
2861         if (!error) {
2862             ofp_header_to_string__(oh, raw, &string);
2863             ds_put_char(&string, '\n');
2864         }
2865
2866         ds_put_format(&string,
2867                       "(***truncated to %"PRIuSIZE" bytes from %"PRIu16"***)\n",
2868                       len, ntohs(oh->length));
2869     } else if (ntohs(oh->length) < len) {
2870         ds_put_format(&string,
2871                       "(***only uses %"PRIu16" bytes out of %"PRIuSIZE"***)\n",
2872                       ntohs(oh->length), len);
2873     } else {
2874         enum ofperr error;
2875         enum ofpraw raw;
2876
2877         error = ofpraw_decode(&raw, oh);
2878         if (!error) {
2879             ofp_to_string__(oh, raw, &string, verbosity);
2880             if (verbosity >= 5) {
2881                 if (ds_last(&string) != '\n') {
2882                     ds_put_char(&string, '\n');
2883                 }
2884                 ds_put_hex_dump(&string, oh, len, 0, true);
2885             }
2886             if (ds_last(&string) != '\n') {
2887                 ds_put_char(&string, '\n');
2888             }
2889             return ds_steal_cstr(&string);
2890         }
2891
2892         ofp_print_error(&string, error);
2893     }
2894     ds_put_hex_dump(&string, oh, len, 0, true);
2895     return ds_steal_cstr(&string);
2896 }
2897 \f
2898 static void
2899 print_and_free(FILE *stream, char *string)
2900 {
2901     fputs(string, stream);
2902     free(string);
2903 }
2904
2905 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
2906  * given 'verbosity' level.  0 is a minimal amount of verbosity and higher
2907  * numbers increase verbosity. */
2908 void
2909 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
2910 {
2911     print_and_free(stream, ofp_to_string(oh, len, verbosity));
2912 }
2913
2914 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
2915  * 'data' to 'stream'. */
2916 void
2917 ofp_print_packet(FILE *stream, const void *data, size_t len)
2918 {
2919     print_and_free(stream, ofp_packet_to_string(data, len));
2920 }