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