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