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