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