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