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