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