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