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