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