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