ofproto: Add support for sending OFPTYPE_ROLE_STATUS messages.
[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=%zu 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=%zu", 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=%zu", 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         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     if (error) {
762         ofpbuf_uninit(&ofpacts);
763         ofp_print_error(s, error);
764         return;
765     }
766
767     ds_put_char(s, ' ');
768     switch (fm.command) {
769     case OFPFC_ADD:
770         ds_put_cstr(s, "ADD");
771         break;
772     case OFPFC_MODIFY:
773         ds_put_cstr(s, "MOD");
774         break;
775     case OFPFC_MODIFY_STRICT:
776         ds_put_cstr(s, "MOD_STRICT");
777         break;
778     case OFPFC_DELETE:
779         ds_put_cstr(s, "DEL");
780         break;
781     case OFPFC_DELETE_STRICT:
782         ds_put_cstr(s, "DEL_STRICT");
783         break;
784     default:
785         ds_put_format(s, "cmd:%d", fm.command);
786     }
787     if (fm.table_id != 0) {
788         ds_put_format(s, " table:%d", fm.table_id);
789     }
790
791     ds_put_char(s, ' ');
792     ofpraw_decode(&raw, oh);
793     if (verbosity >= 3 && raw == OFPRAW_OFPT10_FLOW_MOD) {
794         const struct ofp10_flow_mod *ofm = ofpmsg_body(oh);
795         ofp10_match_print(s, &ofm->match, verbosity);
796
797         /* ofp_print_match() doesn't print priority. */
798         need_priority = true;
799     } else if (verbosity >= 3 && raw == OFPRAW_NXT_FLOW_MOD) {
800         const struct nx_flow_mod *nfm = ofpmsg_body(oh);
801         const void *nxm = nfm + 1;
802         char *nxm_s;
803
804         nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
805         ds_put_cstr(s, nxm_s);
806         free(nxm_s);
807
808         /* nx_match_to_string() doesn't print priority. */
809         need_priority = true;
810     } else {
811         match_format(&fm.match, s, fm.priority);
812
813         /* match_format() does print priority. */
814         need_priority = false;
815     }
816
817     if (ds_last(s) != ' ') {
818         ds_put_char(s, ' ');
819     }
820     if (fm.new_cookie != htonll(0) && fm.new_cookie != OVS_BE64_MAX) {
821         ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.new_cookie));
822     }
823     if (fm.cookie_mask != htonll(0)) {
824         ds_put_format(s, "cookie:0x%"PRIx64"/0x%"PRIx64" ",
825                 ntohll(fm.cookie), ntohll(fm.cookie_mask));
826     }
827     if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
828         ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
829     }
830     if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
831         ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
832     }
833     if (fm.priority != OFP_DEFAULT_PRIORITY && need_priority) {
834         ds_put_format(s, "pri:%"PRIu16" ", fm.priority);
835     }
836     if (fm.buffer_id != UINT32_MAX) {
837         ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
838     }
839     if (fm.out_port != OFPP_ANY) {
840         ds_put_format(s, "out_port:");
841         ofputil_format_port(fm.out_port, s);
842         ds_put_char(s, ' ');
843     }
844
845     if (oh->version == OFP10_VERSION || oh->version == OFP11_VERSION) {
846         /* Don't print the reset_counts flag for OF1.0 and OF1.1 because those
847          * versions don't really have such a flag and printing one is likely to
848          * confuse people. */
849         fm.flags &= ~OFPUTIL_FF_RESET_COUNTS;
850     }
851     ofp_print_flow_flags(s, fm.flags);
852
853     ds_put_cstr(s, "actions=");
854     ofpacts_format(fm.ofpacts, fm.ofpacts_len, s);
855     ofpbuf_uninit(&ofpacts);
856 }
857
858 static void
859 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
860 {
861     ds_put_format(string, "%u", sec);
862     if (nsec > 0) {
863         ds_put_format(string, ".%09u", nsec);
864         while (string->string[string->length - 1] == '0') {
865             string->length--;
866         }
867     }
868     ds_put_char(string, 's');
869 }
870
871 /* Returns a string form of 'reason'.  The return value is either a statically
872  * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
873  * 'bufsize' should be at least OFP_FLOW_REMOVED_REASON_BUFSIZE. */
874 #define OFP_FLOW_REMOVED_REASON_BUFSIZE (INT_STRLEN(int) + 1)
875 static const char *
876 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason,
877                                   char *reasonbuf, size_t bufsize)
878 {
879     switch (reason) {
880     case OFPRR_IDLE_TIMEOUT:
881         return "idle";
882     case OFPRR_HARD_TIMEOUT:
883         return "hard";
884     case OFPRR_DELETE:
885         return "delete";
886     case OFPRR_GROUP_DELETE:
887         return "group_delete";
888     case OFPRR_EVICTION:
889         return "eviction";
890     case OFPRR_METER_DELETE:
891         return "meter_delete";
892     default:
893         snprintf(reasonbuf, bufsize, "%d", (int) reason);
894         return reasonbuf;
895     }
896 }
897
898 static void
899 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
900 {
901     char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
902     struct ofputil_flow_removed fr;
903     enum ofperr error;
904
905     error = ofputil_decode_flow_removed(&fr, oh);
906     if (error) {
907         ofp_print_error(string, error);
908         return;
909     }
910
911     ds_put_char(string, ' ');
912     match_format(&fr.match, string, fr.priority);
913
914     ds_put_format(string, " reason=%s",
915                   ofp_flow_removed_reason_to_string(fr.reason, reasonbuf,
916                                                     sizeof reasonbuf));
917
918     if (fr.table_id != 255) {
919         ds_put_format(string, " table_id=%"PRIu8, fr.table_id);
920     }
921
922     if (fr.cookie != htonll(0)) {
923         ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
924     }
925     ds_put_cstr(string, " duration");
926     ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
927     ds_put_format(string, " idle%"PRIu16, fr.idle_timeout);
928     if (fr.hard_timeout) {
929         /* The hard timeout was only added in OF1.2, so only print it if it is
930          * actually in use to avoid gratuitous change to the formatting. */
931         ds_put_format(string, " hard%"PRIu16, fr.hard_timeout);
932     }
933     ds_put_format(string, " pkts%"PRIu64" bytes%"PRIu64"\n",
934                   fr.packet_count, fr.byte_count);
935 }
936
937 static void
938 ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
939 {
940     struct ofputil_port_mod pm;
941     enum ofperr error;
942
943     error = ofputil_decode_port_mod(oh, &pm);
944     if (error) {
945         ofp_print_error(string, error);
946         return;
947     }
948
949     ds_put_cstr(string, "port: ");
950     ofputil_format_port(pm.port_no, string);
951     ds_put_format(string, ": addr:"ETH_ADDR_FMT"\n",
952                   ETH_ADDR_ARGS(pm.hw_addr));
953
954     ds_put_cstr(string, "     config: ");
955     ofp_print_port_config(string, pm.config);
956
957     ds_put_cstr(string, "     mask:   ");
958     ofp_print_port_config(string, pm.mask);
959
960     ds_put_cstr(string, "     advertise: ");
961     if (pm.advertise) {
962         ofp_print_port_features(string, pm.advertise);
963     } else {
964         ds_put_cstr(string, "UNCHANGED\n");
965     }
966 }
967
968 static void
969 ofp_print_table_miss_config(struct ds *string, const uint32_t config)
970 {
971     uint32_t table_miss_config = config & OFPTC11_TABLE_MISS_MASK;
972
973     switch (table_miss_config) {
974     case OFPTC11_TABLE_MISS_CONTROLLER:
975         ds_put_cstr(string, "controller\n");
976         break;
977     case OFPTC11_TABLE_MISS_CONTINUE:
978         ds_put_cstr(string, "continue\n");
979         break;
980     case OFPTC11_TABLE_MISS_DROP:
981         ds_put_cstr(string, "drop\n");
982         break;
983     default:
984         ds_put_cstr(string, "Unknown\n");
985         break;
986     }
987 }
988
989 static void
990 ofp_print_table_mod(struct ds *string, const struct ofp_header *oh)
991 {
992     struct ofputil_table_mod pm;
993     enum ofperr error;
994
995     error = ofputil_decode_table_mod(oh, &pm);
996     if (error) {
997         ofp_print_error(string, error);
998         return;
999     }
1000
1001     if (pm.table_id == 0xff) {
1002         ds_put_cstr(string, " table_id: ALL_TABLES");
1003     } else {
1004         ds_put_format(string, " table_id=%"PRIu8, pm.table_id);
1005     }
1006
1007     ds_put_cstr(string, ", flow_miss_config=");
1008     ofp_print_table_miss_config(string, pm.config);
1009 }
1010
1011 static void
1012 ofp_print_queue_get_config_request(struct ds *string,
1013                                    const struct ofp_header *oh)
1014 {
1015     enum ofperr error;
1016     ofp_port_t port;
1017
1018     error = ofputil_decode_queue_get_config_request(oh, &port);
1019     if (error) {
1020         ofp_print_error(string, error);
1021         return;
1022     }
1023
1024     ds_put_cstr(string, " port=");
1025     ofputil_format_port(port, string);
1026 }
1027
1028 static void
1029 print_queue_rate(struct ds *string, const char *name, unsigned int rate)
1030 {
1031     if (rate <= 1000) {
1032         ds_put_format(string, " %s:%u.%u%%", name, rate / 10, rate % 10);
1033     } else if (rate < UINT16_MAX) {
1034         ds_put_format(string, " %s:(disabled)", name);
1035     }
1036 }
1037
1038 static void
1039 ofp_print_queue_get_config_reply(struct ds *string,
1040                                  const struct ofp_header *oh)
1041 {
1042     enum ofperr error;
1043     struct ofpbuf b;
1044     ofp_port_t port;
1045
1046     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1047     error = ofputil_decode_queue_get_config_reply(&b, &port);
1048     if (error) {
1049         ofp_print_error(string, error);
1050         return;
1051     }
1052
1053     ds_put_cstr(string, " port=");
1054     ofputil_format_port(port, string);
1055     ds_put_char(string, '\n');
1056
1057     for (;;) {
1058         struct ofputil_queue_config queue;
1059         int retval;
1060
1061         retval = ofputil_pull_queue_get_config_reply(&b, &queue);
1062         if (retval) {
1063             if (retval != EOF) {
1064                 ofp_print_error(string, retval);
1065             }
1066             break;
1067         }
1068
1069         ds_put_format(string, "queue %"PRIu32":", queue.queue_id);
1070         print_queue_rate(string, "min_rate", queue.min_rate);
1071         print_queue_rate(string, "max_rate", queue.max_rate);
1072         ds_put_char(string, '\n');
1073     }
1074 }
1075
1076 static void
1077 ofp_print_meter_flags(struct ds *s, uint16_t flags)
1078 {
1079     if (flags & OFPMF13_KBPS) {
1080         ds_put_cstr(s, "kbps ");
1081     }
1082     if (flags & OFPMF13_PKTPS) {
1083         ds_put_cstr(s, "pktps ");
1084     }
1085     if (flags & OFPMF13_BURST) {
1086         ds_put_cstr(s, "burst ");
1087     }
1088     if (flags & OFPMF13_STATS) {
1089         ds_put_cstr(s, "stats ");
1090     }
1091
1092     flags &= ~(OFPMF13_KBPS | OFPMF13_PKTPS | OFPMF13_BURST | OFPMF13_STATS);
1093     if (flags) {
1094         ds_put_format(s, "flags:0x%"PRIx16" ", flags);
1095     }
1096 }
1097
1098 static void
1099 ofp_print_meter_band(struct ds *s, uint16_t flags,
1100                      const struct ofputil_meter_band *mb)
1101 {
1102     ds_put_cstr(s, "\ntype=");
1103     switch (mb->type) {
1104     case OFPMBT13_DROP:
1105         ds_put_cstr(s, "drop");
1106         break;
1107     case OFPMBT13_DSCP_REMARK:
1108         ds_put_cstr(s, "dscp_remark");
1109         break;
1110     default:
1111         ds_put_format(s, "%u", mb->type);
1112     }
1113
1114     ds_put_format(s, " rate=%"PRIu32, mb->rate);
1115
1116     if (flags & OFPMF13_BURST) {
1117         ds_put_format(s, " burst_size=%"PRIu32, mb->burst_size);
1118     }
1119     if (mb->type == OFPMBT13_DSCP_REMARK) {
1120         ds_put_format(s, " prec_level=%"PRIu8, mb->prec_level);
1121     }
1122 }
1123
1124 static void
1125 ofp_print_meter_stats(struct ds *s, const struct ofputil_meter_stats *ms)
1126 {
1127     uint16_t i;
1128
1129     ds_put_format(s, "meter:%"PRIu32" ", ms->meter_id);
1130     ds_put_format(s, "flow_count:%"PRIu32" ", ms->flow_count);
1131     ds_put_format(s, "packet_in_count:%"PRIu64" ", ms->packet_in_count);
1132     ds_put_format(s, "byte_in_count:%"PRIu64" ", ms->byte_in_count);
1133     ds_put_cstr(s, "duration:");
1134     ofp_print_duration(s, ms->duration_sec, ms->duration_nsec);
1135     ds_put_char(s, ' ');
1136
1137     ds_put_cstr(s, "bands:\n");
1138     for (i = 0; i < ms->n_bands; ++i) {
1139         ds_put_format(s, "%d: ", i);
1140         ds_put_format(s, "packet_count:%"PRIu64" ", ms->bands[i].packet_count);
1141         ds_put_format(s, "byte_count:%"PRIu64"\n", ms->bands[i].byte_count);
1142     }
1143 }
1144
1145 static void
1146 ofp_print_meter_config(struct ds *s, const struct ofputil_meter_config *mc)
1147 {
1148     uint16_t i;
1149
1150     ds_put_format(s, "meter=%"PRIu32" ", mc->meter_id);
1151
1152     ofp_print_meter_flags(s, mc->flags);
1153
1154     ds_put_cstr(s, "bands=");
1155     for (i = 0; i < mc->n_bands; ++i) {
1156         ofp_print_meter_band(s, mc->flags, &mc->bands[i]);
1157     }
1158     ds_put_char(s, '\n');
1159 }
1160
1161 static void
1162 ofp_print_meter_mod(struct ds *s, const struct ofp_header *oh)
1163 {
1164     struct ofputil_meter_mod mm;
1165     struct ofpbuf bands;
1166     enum ofperr error;
1167
1168     ofpbuf_init(&bands, 64);
1169     error = ofputil_decode_meter_mod(oh, &mm, &bands);
1170     if (error) {
1171         ofpbuf_uninit(&bands);
1172         ofp_print_error(s, error);
1173         return;
1174     }
1175
1176     switch (mm.command) {
1177     case OFPMC13_ADD:
1178         ds_put_cstr(s, " ADD ");
1179         break;
1180     case OFPMC13_MODIFY:
1181         ds_put_cstr(s, " MOD ");
1182         break;
1183     case OFPMC13_DELETE:
1184         ds_put_cstr(s, " DEL ");
1185         break;
1186     default:
1187         ds_put_format(s, " cmd:%d ", mm.command);
1188     }
1189
1190     ofp_print_meter_config(s, &mm.meter);
1191     ofpbuf_uninit(&bands);
1192 }
1193
1194 static void
1195 ofp_print_meter_stats_request(struct ds *s, const struct ofp_header *oh)
1196 {
1197     uint32_t meter_id;
1198
1199     ofputil_decode_meter_request(oh, &meter_id);
1200
1201     ds_put_format(s, " meter=%"PRIu32, meter_id);
1202 }
1203
1204 static const char *
1205 ofputil_meter_capabilities_to_name(uint32_t bit)
1206 {
1207     enum ofp13_meter_flags flag = bit;
1208
1209     switch (flag) {
1210     case OFPMF13_KBPS:    return "kbps";
1211     case OFPMF13_PKTPS:   return "pktps";
1212     case OFPMF13_BURST:   return "burst";
1213     case OFPMF13_STATS:   return "stats";
1214     }
1215
1216     return NULL;
1217 }
1218
1219 static const char *
1220 ofputil_meter_band_types_to_name(uint32_t bit)
1221 {
1222     switch (bit) {
1223     case 1 << OFPMBT13_DROP:          return "drop";
1224     case 1 << OFPMBT13_DSCP_REMARK:   return "dscp_remark";
1225     }
1226
1227     return NULL;
1228 }
1229
1230 static void
1231 ofp_print_meter_features_reply(struct ds *s, const struct ofp_header *oh)
1232 {
1233     struct ofputil_meter_features mf;
1234
1235     ofputil_decode_meter_features(oh, &mf);
1236
1237     ds_put_format(s, "\nmax_meter:%"PRIu32, mf.max_meters);
1238     ds_put_format(s, " max_bands:%"PRIu8, mf.max_bands);
1239     ds_put_format(s, " max_color:%"PRIu8"\n", mf.max_color);
1240
1241     ds_put_cstr(s, "band_types: ");
1242     ofp_print_bit_names(s, mf.band_types,
1243                         ofputil_meter_band_types_to_name, ' ');
1244     ds_put_char(s, '\n');
1245
1246     ds_put_cstr(s, "capabilities: ");
1247     ofp_print_bit_names(s, mf.capabilities,
1248                         ofputil_meter_capabilities_to_name, ' ');
1249     ds_put_char(s, '\n');
1250 }
1251
1252 static void
1253 ofp_print_meter_config_reply(struct ds *s, const struct ofp_header *oh)
1254 {
1255     struct ofpbuf bands;
1256     struct ofpbuf b;
1257
1258     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1259     ofpbuf_init(&bands, 64);
1260     for (;;) {
1261         struct ofputil_meter_config mc;
1262         int retval;
1263
1264         retval = ofputil_decode_meter_config(&b, &mc, &bands);
1265         if (retval) {
1266             if (retval != EOF) {
1267                 ofp_print_error(s, retval);
1268             }
1269             break;
1270         }
1271         ds_put_char(s, '\n');
1272         ofp_print_meter_config(s, &mc);
1273     }
1274     ofpbuf_uninit(&bands);
1275 }
1276
1277 static void
1278 ofp_print_meter_stats_reply(struct ds *s, const struct ofp_header *oh)
1279 {
1280     struct ofpbuf bands;
1281     struct ofpbuf b;
1282
1283     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1284     ofpbuf_init(&bands, 64);
1285     for (;;) {
1286         struct ofputil_meter_stats ms;
1287         int retval;
1288
1289         retval = ofputil_decode_meter_stats(&b, &ms, &bands);
1290         if (retval) {
1291             if (retval != EOF) {
1292                 ofp_print_error(s, retval);
1293             }
1294             break;
1295         }
1296         ds_put_char(s, '\n');
1297         ofp_print_meter_stats(s, &ms);
1298     }
1299     ofpbuf_uninit(&bands);
1300 }
1301
1302 static void
1303 ofp_print_error(struct ds *string, enum ofperr error)
1304 {
1305     if (string->length) {
1306         ds_put_char(string, ' ');
1307     }
1308     ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1309 }
1310
1311 static void
1312 ofp_print_hello(struct ds *string, const struct ofp_header *oh)
1313 {
1314     uint32_t allowed_versions;
1315     bool ok;
1316
1317     ok = ofputil_decode_hello(oh, &allowed_versions);
1318
1319     ds_put_cstr(string, "\n version bitmap: ");
1320     ofputil_format_version_bitmap(string, allowed_versions);
1321
1322     if (!ok) {
1323         ds_put_cstr(string, "\n unknown data in hello:\n");
1324         ds_put_hex_dump(string, oh, ntohs(oh->length), 0, true);
1325     }
1326 }
1327
1328 static void
1329 ofp_print_error_msg(struct ds *string, const struct ofp_header *oh)
1330 {
1331     size_t len = ntohs(oh->length);
1332     struct ofpbuf payload;
1333     enum ofperr error;
1334     char *s;
1335
1336     error = ofperr_decode_msg(oh, &payload);
1337     if (!error) {
1338         ds_put_cstr(string, "***decode error***");
1339         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1340         return;
1341     }
1342
1343     ds_put_format(string, " %s\n", ofperr_get_name(error));
1344
1345     if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
1346         ds_put_printable(string, payload.data, payload.size);
1347     } else {
1348         s = ofp_to_string(payload.data, payload.size, 1);
1349         ds_put_cstr(string, s);
1350         free(s);
1351     }
1352 }
1353
1354 static void
1355 ofp_print_port_status(struct ds *string, const struct ofp_header *oh)
1356 {
1357     struct ofputil_port_status ps;
1358     enum ofperr error;
1359
1360     error = ofputil_decode_port_status(oh, &ps);
1361     if (error) {
1362         ofp_print_error(string, error);
1363         return;
1364     }
1365
1366     if (ps.reason == OFPPR_ADD) {
1367         ds_put_format(string, " ADD:");
1368     } else if (ps.reason == OFPPR_DELETE) {
1369         ds_put_format(string, " DEL:");
1370     } else if (ps.reason == OFPPR_MODIFY) {
1371         ds_put_format(string, " MOD:");
1372     }
1373
1374     ofp_print_phy_port(string, &ps.desc);
1375 }
1376
1377 static void
1378 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
1379 {
1380     const struct ofp_desc_stats *ods = ofpmsg_body(oh);
1381
1382     ds_put_char(string, '\n');
1383     ds_put_format(string, "Manufacturer: %.*s\n",
1384             (int) sizeof ods->mfr_desc, ods->mfr_desc);
1385     ds_put_format(string, "Hardware: %.*s\n",
1386             (int) sizeof ods->hw_desc, ods->hw_desc);
1387     ds_put_format(string, "Software: %.*s\n",
1388             (int) sizeof ods->sw_desc, ods->sw_desc);
1389     ds_put_format(string, "Serial Num: %.*s\n",
1390             (int) sizeof ods->serial_num, ods->serial_num);
1391     ds_put_format(string, "DP Description: %.*s\n",
1392             (int) sizeof ods->dp_desc, ods->dp_desc);
1393 }
1394
1395 static void
1396 ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
1397 {
1398     struct ofputil_flow_stats_request fsr;
1399     enum ofperr error;
1400
1401     error = ofputil_decode_flow_stats_request(&fsr, oh);
1402     if (error) {
1403         ofp_print_error(string, error);
1404         return;
1405     }
1406
1407     if (fsr.table_id != 0xff) {
1408         ds_put_format(string, " table=%"PRIu8, fsr.table_id);
1409     }
1410
1411     if (fsr.out_port != OFPP_ANY) {
1412         ds_put_cstr(string, " out_port=");
1413         ofputil_format_port(fsr.out_port, string);
1414     }
1415
1416     ds_put_char(string, ' ');
1417     match_format(&fsr.match, string, OFP_DEFAULT_PRIORITY);
1418 }
1419
1420 void
1421 ofp_print_flow_stats(struct ds *string, struct ofputil_flow_stats *fs)
1422 {
1423     ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1424                   ntohll(fs->cookie));
1425
1426     ofp_print_duration(string, fs->duration_sec, fs->duration_nsec);
1427     ds_put_format(string, ", table=%"PRIu8", ", fs->table_id);
1428     ds_put_format(string, "n_packets=%"PRIu64", ", fs->packet_count);
1429     ds_put_format(string, "n_bytes=%"PRIu64", ", fs->byte_count);
1430     if (fs->idle_timeout != OFP_FLOW_PERMANENT) {
1431         ds_put_format(string, "idle_timeout=%"PRIu16", ", fs->idle_timeout);
1432     }
1433     if (fs->hard_timeout != OFP_FLOW_PERMANENT) {
1434         ds_put_format(string, "hard_timeout=%"PRIu16", ", fs->hard_timeout);
1435     }
1436     if (fs->flags) {
1437         ofp_print_flow_flags(string, fs->flags);
1438     }
1439     if (fs->idle_age >= 0) {
1440         ds_put_format(string, "idle_age=%d, ", fs->idle_age);
1441     }
1442     if (fs->hard_age >= 0 && fs->hard_age != fs->duration_sec) {
1443         ds_put_format(string, "hard_age=%d, ", fs->hard_age);
1444     }
1445
1446     match_format(&fs->match, string, fs->priority);
1447     if (string->string[string->length - 1] != ' ') {
1448         ds_put_char(string, ' ');
1449     }
1450
1451     ds_put_cstr(string, "actions=");
1452     ofpacts_format(fs->ofpacts, fs->ofpacts_len, string);
1453 }
1454
1455 static void
1456 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1457 {
1458     struct ofpbuf ofpacts;
1459     struct ofpbuf b;
1460
1461     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1462     ofpbuf_init(&ofpacts, 64);
1463     for (;;) {
1464         struct ofputil_flow_stats fs;
1465         int retval;
1466
1467         retval = ofputil_decode_flow_stats_reply(&fs, &b, true, &ofpacts);
1468         if (retval) {
1469             if (retval != EOF) {
1470                 ds_put_cstr(string, " ***parse error***");
1471             }
1472             break;
1473         }
1474         ds_put_char(string, '\n');
1475         ofp_print_flow_stats(string, &fs);
1476      }
1477     ofpbuf_uninit(&ofpacts);
1478 }
1479
1480 static void
1481 ofp_print_aggregate_stats_reply(struct ds *string, const struct ofp_header *oh)
1482 {
1483     struct ofputil_aggregate_stats as;
1484     enum ofperr error;
1485
1486     error = ofputil_decode_aggregate_stats_reply(&as, oh);
1487     if (error) {
1488         ofp_print_error(string, error);
1489         return;
1490     }
1491
1492     ds_put_format(string, " packet_count=%"PRIu64, as.packet_count);
1493     ds_put_format(string, " byte_count=%"PRIu64, as.byte_count);
1494     ds_put_format(string, " flow_count=%"PRIu32, as.flow_count);
1495 }
1496
1497 static void
1498 print_port_stat(struct ds *string, const char *leader, uint64_t stat, int more)
1499 {
1500     ds_put_cstr(string, leader);
1501     if (stat != UINT64_MAX) {
1502         ds_put_format(string, "%"PRIu64, stat);
1503     } else {
1504         ds_put_char(string, '?');
1505     }
1506     if (more) {
1507         ds_put_cstr(string, ", ");
1508     } else {
1509         ds_put_cstr(string, "\n");
1510     }
1511 }
1512
1513 static void
1514 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1515 {
1516     ofp_port_t ofp10_port;
1517     enum ofperr error;
1518
1519     error = ofputil_decode_port_stats_request(oh, &ofp10_port);
1520     if (error) {
1521         ofp_print_error(string, error);
1522         return;
1523     }
1524
1525     ds_put_cstr(string, " port_no=");
1526     ofputil_format_port(ofp10_port, string);
1527 }
1528
1529 static void
1530 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1531                            int verbosity)
1532 {
1533     struct ofpbuf b;
1534
1535     ds_put_format(string, " %zu ports\n", ofputil_count_port_stats(oh));
1536     if (verbosity < 1) {
1537         return;
1538     }
1539
1540     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1541     for (;;) {
1542         struct ofputil_port_stats ps;
1543         int retval;
1544
1545         retval = ofputil_decode_port_stats(&ps, &b);
1546         if (retval) {
1547             if (retval != EOF) {
1548                 ds_put_cstr(string, " ***parse error***");
1549             }
1550             return;
1551         }
1552
1553         ds_put_cstr(string, "  port ");
1554         if (ofp_to_u16(ps.port_no) < 10) {
1555             ds_put_char(string, ' ');
1556         }
1557         ofputil_format_port(ps.port_no, string);
1558
1559         ds_put_cstr(string, ": rx ");
1560         print_port_stat(string, "pkts=", ps.stats.rx_packets, 1);
1561         print_port_stat(string, "bytes=", ps.stats.rx_bytes, 1);
1562         print_port_stat(string, "drop=", ps.stats.rx_dropped, 1);
1563         print_port_stat(string, "errs=", ps.stats.rx_errors, 1);
1564         print_port_stat(string, "frame=", ps.stats.rx_frame_errors, 1);
1565         print_port_stat(string, "over=", ps.stats.rx_over_errors, 1);
1566         print_port_stat(string, "crc=", ps.stats.rx_crc_errors, 0);
1567
1568         ds_put_cstr(string, "           tx ");
1569         print_port_stat(string, "pkts=", ps.stats.tx_packets, 1);
1570         print_port_stat(string, "bytes=", ps.stats.tx_bytes, 1);
1571         print_port_stat(string, "drop=", ps.stats.tx_dropped, 1);
1572         print_port_stat(string, "errs=", ps.stats.tx_errors, 1);
1573         print_port_stat(string, "coll=", ps.stats.collisions, 0);
1574
1575         if (ps.duration_sec != UINT32_MAX) {
1576             ds_put_cstr(string, "           duration=");
1577             ofp_print_duration(string, ps.duration_sec, ps.duration_nsec);
1578             ds_put_char(string, '\n');
1579         }
1580     }
1581 }
1582
1583 static void
1584 ofp_print_one_ofpst_table_reply(struct ds *string, enum ofp_version ofp_version,
1585                                 const char *name, struct ofp12_table_stats *ts)
1586 {
1587     char name_[OFP_MAX_TABLE_NAME_LEN + 1];
1588
1589     /* ofp13_table_stats is different */
1590     if (ofp_version > OFP12_VERSION) {
1591         return;
1592     }
1593
1594     ovs_strlcpy(name_, name, sizeof name_);
1595
1596     ds_put_format(string, "  %d: %-8s: ", ts->table_id, name_);
1597     ds_put_format(string, "wild=0x%05"PRIx64", ", ntohll(ts->wildcards));
1598     ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1599     ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1600     ds_put_cstr(string, "               ");
1601     ds_put_format(string, "lookup=%"PRIu64", ", ntohll(ts->lookup_count));
1602     ds_put_format(string, "matched=%"PRIu64"\n", ntohll(ts->matched_count));
1603
1604     if (ofp_version < OFP11_VERSION) {
1605         return;
1606     }
1607
1608     ds_put_cstr(string, "               ");
1609     ds_put_format(string, "match=0x%08"PRIx64", ", ntohll(ts->match));
1610     ds_put_format(string, "instructions=0x%08"PRIx32", ",
1611                   ntohl(ts->instructions));
1612     ds_put_format(string, "config=0x%08"PRIx32"\n", ntohl(ts->config));
1613     ds_put_cstr(string, "               ");
1614     ds_put_format(string, "write_actions=0x%08"PRIx32", ",
1615                   ntohl(ts->write_actions));
1616     ds_put_format(string, "apply_actions=0x%08"PRIx32"\n",
1617                   ntohl(ts->apply_actions));
1618
1619     if (ofp_version < OFP12_VERSION) {
1620         return;
1621     }
1622
1623     ds_put_cstr(string, "               ");
1624     ds_put_format(string, "write_setfields=0x%016"PRIx64"\n",
1625                   ntohll(ts->write_setfields));
1626     ds_put_cstr(string, "               ");
1627     ds_put_format(string, "apply_setfields=0x%016"PRIx64"\n",
1628                   ntohll(ts->apply_setfields));
1629     ds_put_cstr(string, "               ");
1630     ds_put_format(string, "metadata_match=0x%016"PRIx64"\n",
1631                   ntohll(ts->metadata_match));
1632     ds_put_cstr(string, "               ");
1633     ds_put_format(string, "metadata_write=0x%016"PRIx64"\n",
1634                   ntohll(ts->metadata_write));
1635 }
1636
1637 static void
1638 ofp_print_ofpst_table_reply13(struct ds *string, const struct ofp_header *oh,
1639                               int verbosity)
1640 {
1641     struct ofp13_table_stats *ts;
1642     struct ofpbuf b;
1643     size_t n;
1644
1645     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1646     ofpraw_pull_assert(&b);
1647
1648     n = b.size / sizeof *ts;
1649     ds_put_format(string, " %zu tables\n", n);
1650     if (verbosity < 1) {
1651         return;
1652     }
1653
1654     for (;;) {
1655         ts = ofpbuf_try_pull(&b, sizeof *ts);
1656         if (!ts) {
1657             return;
1658         }
1659         ds_put_format(string,
1660                       "  %d: active=%"PRIu32", lookup=%"PRIu64  \
1661                       ", matched=%"PRIu64"\n",
1662                       ts->table_id, ntohl(ts->active_count),
1663                       ntohll(ts->lookup_count), ntohll(ts->matched_count));
1664     }
1665 }
1666
1667 static void
1668 ofp_print_ofpst_table_reply12(struct ds *string, const struct ofp_header *oh,
1669                               int verbosity)
1670 {
1671     struct ofp12_table_stats *ts;
1672     struct ofpbuf b;
1673     size_t n;
1674
1675     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1676     ofpraw_pull_assert(&b);
1677
1678     n = b.size / sizeof *ts;
1679     ds_put_format(string, " %zu tables\n", n);
1680     if (verbosity < 1) {
1681         return;
1682     }
1683
1684     for (;;) {
1685         ts = ofpbuf_try_pull(&b, sizeof *ts);
1686         if (!ts) {
1687             return;
1688         }
1689
1690         ofp_print_one_ofpst_table_reply(string, OFP12_VERSION, ts->name, ts);
1691      }
1692 }
1693
1694 static void
1695 ofp_print_ofpst_table_reply11(struct ds *string, const struct ofp_header *oh,
1696                               int verbosity)
1697 {
1698     struct ofp11_table_stats *ts;
1699     struct ofpbuf b;
1700     size_t n;
1701
1702     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1703     ofpraw_pull_assert(&b);
1704
1705     n = b.size / sizeof *ts;
1706     ds_put_format(string, " %zu tables\n", n);
1707     if (verbosity < 1) {
1708         return;
1709     }
1710
1711     for (;;) {
1712         struct ofp12_table_stats ts12;
1713
1714         ts = ofpbuf_try_pull(&b, sizeof *ts);
1715         if (!ts) {
1716             return;
1717         }
1718
1719         ts12.table_id = ts->table_id;
1720         ts12.wildcards = htonll(ntohl(ts->wildcards));
1721         ts12.max_entries = ts->max_entries;
1722         ts12.active_count = ts->active_count;
1723         ts12.lookup_count = ts->lookup_count;
1724         ts12.matched_count = ts->matched_count;
1725         ts12.match = htonll(ntohl(ts->match));
1726         ts12.instructions = ts->instructions;
1727         ts12.config = ts->config;
1728         ts12.write_actions = ts->write_actions;
1729         ts12.apply_actions = ts->apply_actions;
1730         ofp_print_one_ofpst_table_reply(string, OFP11_VERSION, ts->name, &ts12);
1731      }
1732 }
1733
1734 static void
1735 ofp_print_ofpst_table_reply10(struct ds *string, const struct ofp_header *oh,
1736                               int verbosity)
1737 {
1738     struct ofp10_table_stats *ts;
1739     struct ofpbuf b;
1740     size_t n;
1741
1742     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1743     ofpraw_pull_assert(&b);
1744
1745     n = b.size / sizeof *ts;
1746     ds_put_format(string, " %zu tables\n", n);
1747     if (verbosity < 1) {
1748         return;
1749     }
1750
1751     for (;;) {
1752         struct ofp12_table_stats ts12;
1753
1754         ts = ofpbuf_try_pull(&b, sizeof *ts);
1755         if (!ts) {
1756             return;
1757         }
1758
1759         ts12.table_id = ts->table_id;
1760         ts12.wildcards = htonll(ntohl(ts->wildcards));
1761         ts12.max_entries = ts->max_entries;
1762         ts12.active_count = ts->active_count;
1763         ts12.lookup_count = get_32aligned_be64(&ts->lookup_count);
1764         ts12.matched_count = get_32aligned_be64(&ts->matched_count);
1765         ofp_print_one_ofpst_table_reply(string, OFP10_VERSION, ts->name, &ts12);
1766      }
1767 }
1768
1769 static void
1770 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1771                             int verbosity)
1772 {
1773     switch ((enum ofp_version)oh->version) {
1774     case OFP13_VERSION:
1775         ofp_print_ofpst_table_reply13(string, oh, verbosity);
1776         break;
1777
1778     case OFP12_VERSION:
1779         ofp_print_ofpst_table_reply12(string, oh, verbosity);
1780         break;
1781
1782     case OFP11_VERSION:
1783         ofp_print_ofpst_table_reply11(string, oh, verbosity);
1784         break;
1785
1786     case OFP10_VERSION:
1787         ofp_print_ofpst_table_reply10(string, oh, verbosity);
1788         break;
1789
1790     default:
1791         NOT_REACHED();
1792     }
1793 }
1794
1795 static void
1796 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1797 {
1798     if (queue_id == OFPQ_ALL) {
1799         ds_put_cstr(string, "ALL");
1800     } else {
1801         ds_put_format(string, "%"PRIu32, queue_id);
1802     }
1803 }
1804
1805 static void
1806 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1807 {
1808     struct ofputil_queue_stats_request oqsr;
1809     enum ofperr error;
1810
1811     error = ofputil_decode_queue_stats_request(oh, &oqsr);
1812     if (error) {
1813         ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1814         return;
1815     }
1816
1817     ds_put_cstr(string, "port=");
1818     ofputil_format_port(oqsr.port_no, string);
1819
1820     ds_put_cstr(string, " queue=");
1821     ofp_print_queue_name(string, oqsr.queue_id);
1822 }
1823
1824 static void
1825 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1826                             int verbosity)
1827 {
1828     struct ofpbuf b;
1829
1830     ds_put_format(string, " %zu queues\n", ofputil_count_queue_stats(oh));
1831     if (verbosity < 1) {
1832         return;
1833     }
1834
1835     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1836     for (;;) {
1837         struct ofputil_queue_stats qs;
1838         int retval;
1839
1840         retval = ofputil_decode_queue_stats(&qs, &b);
1841         if (retval) {
1842             if (retval != EOF) {
1843                 ds_put_cstr(string, " ***parse error***");
1844             }
1845             return;
1846         }
1847
1848         ds_put_cstr(string, "  port ");
1849         ofputil_format_port(qs.port_no, string);
1850         ds_put_cstr(string, " queue ");
1851         ofp_print_queue_name(string, qs.queue_id);
1852         ds_put_cstr(string, ": ");
1853
1854         print_port_stat(string, "bytes=", qs.tx_bytes, 1);
1855         print_port_stat(string, "pkts=", qs.tx_packets, 1);
1856         print_port_stat(string, "errors=", qs.tx_errors, 1);
1857
1858         ds_put_cstr(string, "duration=");
1859         if (qs.duration_sec != UINT32_MAX) {
1860             ofp_print_duration(string, qs.duration_sec, qs.duration_nsec);
1861         } else {
1862             ds_put_char(string, '?');
1863         }
1864         ds_put_char(string, '\n');
1865     }
1866 }
1867
1868 static void
1869 ofp_print_ofpst_port_desc_reply(struct ds *string,
1870                                 const struct ofp_header *oh)
1871 {
1872     struct ofpbuf b;
1873
1874     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1875     ofpraw_pull_assert(&b);
1876     ds_put_char(string, '\n');
1877     ofp_print_phy_ports(string, oh->version, &b);
1878 }
1879
1880 static void
1881 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1882 {
1883     uint16_t flags = ofpmp_flags(oh);
1884
1885     if (flags) {
1886         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
1887     }
1888 }
1889
1890 static void
1891 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1892 {
1893     uint16_t flags = ofpmp_flags(oh);
1894
1895     if (flags) {
1896         ds_put_cstr(string, " flags=");
1897         if (flags & OFPSF_REPLY_MORE) {
1898             ds_put_cstr(string, "[more]");
1899             flags &= ~OFPSF_REPLY_MORE;
1900         }
1901         if (flags) {
1902             ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1903                           flags);
1904         }
1905     }
1906 }
1907
1908 static void
1909 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1910 {
1911     size_t len = ntohs(oh->length);
1912
1913     ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1914     if (verbosity > 1) {
1915         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1916     }
1917 }
1918
1919 static void
1920 ofp_print_role_generic(struct ds *string, enum ofp12_controller_role role,
1921                        uint64_t generation_id)
1922 {
1923     ds_put_cstr(string, " role=");
1924
1925     switch (role) {
1926     case OFPCR12_ROLE_NOCHANGE:
1927         ds_put_cstr(string, "nochange");
1928         break;
1929     case OFPCR12_ROLE_EQUAL:
1930         ds_put_cstr(string, "equal"); /* OF 1.2 wording */
1931         break;
1932     case OFPCR12_ROLE_MASTER:
1933         ds_put_cstr(string, "master");
1934         break;
1935     case OFPCR12_ROLE_SLAVE:
1936         ds_put_cstr(string, "slave");
1937         break;
1938     default:
1939         NOT_REACHED();
1940     }
1941
1942     if (generation_id != UINT64_MAX) {
1943         ds_put_format(string, " generation_id=%"PRIu64, generation_id);
1944     }
1945 }
1946
1947 static void
1948 ofp_print_role_message(struct ds *string, const struct ofp_header *oh)
1949 {
1950     struct ofputil_role_request rr;
1951     enum ofperr error;
1952
1953     error = ofputil_decode_role_message(oh, &rr);
1954     if (error) {
1955         ofp_print_error(string, error);
1956         return;
1957     }
1958
1959     ofp_print_role_generic(string, rr.role, rr.have_generation_id ? rr.generation_id : UINT64_MAX);
1960 }
1961
1962 static void
1963 ofp_print_role_status_message(struct ds *string, const struct ofp_header *oh)
1964 {
1965     struct ofputil_role_status rs;
1966     enum ofperr error;
1967
1968     error = ofputil_decode_role_status(oh, &rs);
1969     if (error) {
1970         ofp_print_error(string, error);
1971         return;
1972     }
1973
1974     ofp_print_role_generic(string, rs.role, rs.generation_id);
1975
1976     ds_put_cstr(string, " reason=");
1977
1978     switch (rs.reason) {
1979     case OFPCRR_MASTER_REQUEST:
1980         ds_put_cstr(string, "master_request");
1981         break;
1982     case OFPCRR_CONFIG:
1983         ds_put_cstr(string, "configuration_changed");
1984         break;
1985     case OFPCRR_EXPERIMENTER:
1986         ds_put_cstr(string, "experimenter_data_changed");
1987         break;
1988     default:
1989         NOT_REACHED();
1990     }
1991 }
1992
1993 static void
1994 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1995                                 const struct nx_flow_mod_table_id *nfmti)
1996 {
1997     ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1998 }
1999
2000 static void
2001 ofp_print_nxt_set_flow_format(struct ds *string,
2002                               const struct nx_set_flow_format *nsff)
2003 {
2004     uint32_t format = ntohl(nsff->format);
2005
2006     ds_put_cstr(string, " format=");
2007     if (ofputil_nx_flow_format_is_valid(format)) {
2008         ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
2009     } else {
2010         ds_put_format(string, "%"PRIu32, format);
2011     }
2012 }
2013
2014 static void
2015 ofp_print_nxt_set_packet_in_format(struct ds *string,
2016                                    const struct nx_set_packet_in_format *nspf)
2017 {
2018     uint32_t format = ntohl(nspf->format);
2019
2020     ds_put_cstr(string, " format=");
2021     if (ofputil_packet_in_format_is_valid(format)) {
2022         ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
2023     } else {
2024         ds_put_format(string, "%"PRIu32, format);
2025     }
2026 }
2027
2028 /* Returns a string form of 'reason'.  The return value is either a statically
2029  * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
2030  * 'bufsize' should be at least OFP_PORT_REASON_BUFSIZE. */
2031 #define OFP_PORT_REASON_BUFSIZE (INT_STRLEN(int) + 1)
2032 static const char *
2033 ofp_port_reason_to_string(enum ofp_port_reason reason,
2034                           char *reasonbuf, size_t bufsize)
2035 {
2036     switch (reason) {
2037     case OFPPR_ADD:
2038         return "add";
2039
2040     case OFPPR_DELETE:
2041         return "delete";
2042
2043     case OFPPR_MODIFY:
2044         return "modify";
2045
2046     default:
2047         snprintf(reasonbuf, bufsize, "%d", (int) reason);
2048         return reasonbuf;
2049     }
2050 }
2051
2052 static void
2053 ofp_print_nxt_set_async_config(struct ds *string,
2054                                const struct nx_async_config *nac)
2055 {
2056     int i;
2057
2058     for (i = 0; i < 2; i++) {
2059         int j;
2060
2061         ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
2062
2063         ds_put_cstr(string, "       PACKET_IN:");
2064         for (j = 0; j < 32; j++) {
2065             if (nac->packet_in_mask[i] & htonl(1u << j)) {
2066                 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
2067                 const char *reason;
2068
2069                 reason = ofputil_packet_in_reason_to_string(j, reasonbuf,
2070                                                             sizeof reasonbuf);
2071                 ds_put_format(string, " %s", reason);
2072             }
2073         }
2074         if (!nac->packet_in_mask[i]) {
2075             ds_put_cstr(string, " (off)");
2076         }
2077         ds_put_char(string, '\n');
2078
2079         ds_put_cstr(string, "     PORT_STATUS:");
2080         for (j = 0; j < 32; j++) {
2081             if (nac->port_status_mask[i] & htonl(1u << j)) {
2082                 char reasonbuf[OFP_PORT_REASON_BUFSIZE];
2083                 const char *reason;
2084
2085                 reason = ofp_port_reason_to_string(j, reasonbuf,
2086                                                    sizeof reasonbuf);
2087                 ds_put_format(string, " %s", reason);
2088             }
2089         }
2090         if (!nac->port_status_mask[i]) {
2091             ds_put_cstr(string, " (off)");
2092         }
2093         ds_put_char(string, '\n');
2094
2095         ds_put_cstr(string, "    FLOW_REMOVED:");
2096         for (j = 0; j < 32; j++) {
2097             if (nac->flow_removed_mask[i] & htonl(1u << j)) {
2098                 char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
2099                 const char *reason;
2100
2101                 reason = ofp_flow_removed_reason_to_string(j, reasonbuf,
2102                                                            sizeof reasonbuf);
2103                 ds_put_format(string, " %s", reason);
2104             }
2105         }
2106         if (!nac->flow_removed_mask[i]) {
2107             ds_put_cstr(string, " (off)");
2108         }
2109         ds_put_char(string, '\n');
2110     }
2111 }
2112
2113 static void
2114 ofp_print_nxt_set_controller_id(struct ds *string,
2115                                 const struct nx_controller_id *nci)
2116 {
2117     ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
2118 }
2119
2120 static void
2121 ofp_print_nxt_flow_monitor_cancel(struct ds *string,
2122                                   const struct ofp_header *oh)
2123 {
2124     ds_put_format(string, " id=%"PRIu32,
2125                   ofputil_decode_flow_monitor_cancel(oh));
2126 }
2127
2128 static const char *
2129 nx_flow_monitor_flags_to_name(uint32_t bit)
2130 {
2131     enum nx_flow_monitor_flags fmf = bit;
2132
2133     switch (fmf) {
2134     case NXFMF_INITIAL: return "initial";
2135     case NXFMF_ADD: return "add";
2136     case NXFMF_DELETE: return "delete";
2137     case NXFMF_MODIFY: return "modify";
2138     case NXFMF_ACTIONS: return "actions";
2139     case NXFMF_OWN: return "own";
2140     }
2141
2142     return NULL;
2143 }
2144
2145 static void
2146 ofp_print_nxst_flow_monitor_request(struct ds *string,
2147                                     const struct ofp_header *oh)
2148 {
2149     struct ofpbuf b;
2150
2151     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2152     for (;;) {
2153         struct ofputil_flow_monitor_request request;
2154         int retval;
2155
2156         retval = ofputil_decode_flow_monitor_request(&request, &b);
2157         if (retval) {
2158             if (retval != EOF) {
2159                 ofp_print_error(string, retval);
2160             }
2161             return;
2162         }
2163
2164         ds_put_format(string, "\n id=%"PRIu32" flags=", request.id);
2165         ofp_print_bit_names(string, request.flags,
2166                             nx_flow_monitor_flags_to_name, ',');
2167
2168         if (request.out_port != OFPP_NONE) {
2169             ds_put_cstr(string, " out_port=");
2170             ofputil_format_port(request.out_port, string);
2171         }
2172
2173         if (request.table_id != 0xff) {
2174             ds_put_format(string, " table=%"PRIu8, request.table_id);
2175         }
2176
2177         ds_put_char(string, ' ');
2178         match_format(&request.match, string, OFP_DEFAULT_PRIORITY);
2179         ds_chomp(string, ' ');
2180     }
2181 }
2182
2183 static void
2184 ofp_print_nxst_flow_monitor_reply(struct ds *string,
2185                                   const struct ofp_header *oh)
2186 {
2187     uint64_t ofpacts_stub[1024 / 8];
2188     struct ofpbuf ofpacts;
2189     struct ofpbuf b;
2190
2191     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2192     ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2193     for (;;) {
2194         char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
2195         struct ofputil_flow_update update;
2196         struct match match;
2197         int retval;
2198
2199         update.match = &match;
2200         retval = ofputil_decode_flow_update(&update, &b, &ofpacts);
2201         if (retval) {
2202             if (retval != EOF) {
2203                 ofp_print_error(string, retval);
2204             }
2205             ofpbuf_uninit(&ofpacts);
2206             return;
2207         }
2208
2209         ds_put_cstr(string, "\n event=");
2210         switch (update.event) {
2211         case NXFME_ADDED:
2212             ds_put_cstr(string, "ADDED");
2213             break;
2214
2215         case NXFME_DELETED:
2216             ds_put_format(string, "DELETED reason=%s",
2217                           ofp_flow_removed_reason_to_string(update.reason,
2218                                                             reasonbuf,
2219                                                             sizeof reasonbuf));
2220             break;
2221
2222         case NXFME_MODIFIED:
2223             ds_put_cstr(string, "MODIFIED");
2224             break;
2225
2226         case NXFME_ABBREV:
2227             ds_put_format(string, "ABBREV xid=0x%"PRIx32, ntohl(update.xid));
2228             continue;
2229         }
2230
2231         ds_put_format(string, " table=%"PRIu8, update.table_id);
2232         if (update.idle_timeout != OFP_FLOW_PERMANENT) {
2233             ds_put_format(string, " idle_timeout=%"PRIu16,
2234                           update.idle_timeout);
2235         }
2236         if (update.hard_timeout != OFP_FLOW_PERMANENT) {
2237             ds_put_format(string, " hard_timeout=%"PRIu16,
2238                           update.hard_timeout);
2239         }
2240         ds_put_format(string, " cookie=%#"PRIx64, ntohll(update.cookie));
2241
2242         ds_put_char(string, ' ');
2243         match_format(update.match, string, OFP_DEFAULT_PRIORITY);
2244
2245         if (update.ofpacts_len) {
2246             if (string->string[string->length - 1] != ' ') {
2247                 ds_put_char(string, ' ');
2248             }
2249             ds_put_cstr(string, "actions=");
2250             ofpacts_format(update.ofpacts, update.ofpacts_len, string);
2251         }
2252     }
2253 }
2254
2255 void
2256 ofp_print_version(const struct ofp_header *oh,
2257                   struct ds *string)
2258 {
2259     switch (oh->version) {
2260     case OFP10_VERSION:
2261         break;
2262     case OFP11_VERSION:
2263         ds_put_cstr(string, " (OF1.1)");
2264         break;
2265     case OFP12_VERSION:
2266         ds_put_cstr(string, " (OF1.2)");
2267         break;
2268     case OFP13_VERSION:
2269         ds_put_cstr(string, " (OF1.3)");
2270         break;
2271     default:
2272         ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
2273         break;
2274     }
2275     ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
2276 }
2277
2278 static void
2279 ofp_header_to_string__(const struct ofp_header *oh, enum ofpraw raw,
2280                        struct ds *string)
2281 {
2282     ds_put_cstr(string, ofpraw_get_name(raw));
2283     ofp_print_version(oh, string);
2284 }
2285
2286 static void
2287 ofp_print_not_implemented(struct ds *string)
2288 {
2289     ds_put_cstr(string, "NOT IMPLEMENTED YET!\n");
2290 }
2291
2292 static void
2293 ofp_print_group(struct ds *s, uint32_t group_id, uint8_t type,
2294                 struct list *p_buckets)
2295 {
2296     static const char *type_str[] = { "all", "select", "indirect",
2297                                       "ff", "unknown" };
2298     struct ofputil_bucket *bucket;
2299
2300     ds_put_format(s, "group_id=%"PRIu32",type=%s",
2301                   group_id, type_str[type > 4 ? 4 : type]);
2302     if (!p_buckets) {
2303         return;
2304     }
2305
2306     LIST_FOR_EACH (bucket, list_node, p_buckets) {
2307         ds_put_cstr(s, ",bucket=");
2308
2309         if (bucket->weight != 1) {
2310             ds_put_format(s, "weight:%"PRIu16",", bucket->weight);
2311         }
2312         if (bucket->watch_port != OFPP_NONE) {
2313             ds_put_format(s, "watch_port:%"PRIu32",", bucket->watch_port);
2314         }
2315         if (bucket->watch_group != OFPG11_ANY) {
2316             ds_put_format(s, "watch_group:%"PRIu32",", bucket->watch_group);
2317         }
2318
2319         ds_put_cstr(s, "actions=");
2320         ofpacts_format(bucket->ofpacts, bucket->ofpacts_len, s);
2321     }
2322 }
2323
2324 static void
2325 ofp_print_group_desc(struct ds *s, const struct ofp_header *oh)
2326 {
2327     struct ofpbuf b;
2328
2329     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2330     for (;;) {
2331         struct ofputil_group_desc gd;
2332         int retval;
2333
2334         retval = ofputil_decode_group_desc_reply(&gd, &b, oh->version);
2335         if (retval) {
2336             if (retval != EOF) {
2337                 ds_put_cstr(s, " ***parse error***");
2338             }
2339             break;
2340         }
2341
2342         ds_put_char(s, '\n');
2343         ds_put_char(s, ' ');
2344         ofp_print_group(s, gd.group_id, gd.type, &gd.buckets);
2345      }
2346 }
2347
2348 static void
2349 ofp_print_ofpst_group_request(struct ds *string, const struct ofp_header *oh)
2350 {
2351     enum ofperr error;
2352     uint32_t group_id;
2353
2354     error = ofputil_decode_group_stats_request(oh, &group_id);
2355     if (error) {
2356         ofp_print_error(string, error);
2357         return;
2358     }
2359
2360     ds_put_cstr(string, " group_id=");
2361     ofputil_format_group(group_id, string);
2362 }
2363
2364 static void
2365 ofp_print_group_stats(struct ds *s, const struct ofp_header *oh)
2366 {
2367     struct ofpbuf b;
2368     uint32_t bucket_i;
2369
2370     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2371
2372     for (;;) {
2373         struct ofputil_group_stats gs;
2374         int retval;
2375
2376         retval = ofputil_decode_group_stats_reply(&b, &gs);
2377         if (retval) {
2378             if (retval != EOF) {
2379                 ds_put_cstr(s, " ***parse error***");
2380             }
2381             break;
2382         }
2383
2384         ds_put_char(s, '\n');
2385
2386         ds_put_char(s, ' ');
2387         ds_put_format(s, "group_id=%"PRIu32",", gs.group_id);
2388
2389         if (gs.duration_sec != UINT32_MAX) {
2390             ds_put_cstr(s, "duration=");
2391             ofp_print_duration(s, gs.duration_sec, gs.duration_nsec);
2392             ds_put_char(s, ',');
2393         }
2394         ds_put_format(s, "ref_count=%"PRIu32",", gs.ref_count);
2395         ds_put_format(s, "packet_count=%"PRIu64",", gs.packet_count);
2396         ds_put_format(s, "byte_count=%"PRIu64"", gs.byte_count);
2397
2398         for (bucket_i = 0; bucket_i < gs.n_buckets; bucket_i++) {
2399             if (gs.bucket_stats[bucket_i].packet_count != UINT64_MAX) {
2400                 ds_put_format(s, ",bucket%"PRIu32":", bucket_i);
2401                 ds_put_format(s, "packet_count=%"PRIu64",", gs.bucket_stats[bucket_i].packet_count);
2402                 ds_put_format(s, "byte_count=%"PRIu64"", gs.bucket_stats[bucket_i].byte_count);
2403             }
2404         }
2405
2406         free(gs.bucket_stats);
2407      }
2408 }
2409
2410 static void
2411 ofp_print_group_features(struct ds *string, const struct ofp_header *oh)
2412 {
2413     struct ofputil_group_features features;
2414
2415     ofputil_decode_group_features_reply(oh, &features);
2416
2417     ds_put_format(string, "\n Group table:\n");
2418     ds_put_format(string, "    Types:  0x%"PRIx32"\n", features.types);
2419     ds_put_format(string, "    Capabilities:  0x%"PRIx32"\n",
2420                   features.capabilities);
2421
2422     if (features.types & (1u << OFPGT11_ALL)) {
2423         ds_put_format(string, "    All group :\n");
2424         ds_put_format(string,
2425                       "        max_groups = %#"PRIx32" actions=0x%08"PRIx32"\n",
2426                       features.max_groups[0], features.actions[0]);
2427     }
2428
2429     if (features.types & (1u << OFPGT11_SELECT)) {
2430         ds_put_format(string, "    Select group :\n");
2431         ds_put_format(string, "        max_groups = %#"PRIx32" "
2432                       "actions=0x%08"PRIx32"\n",
2433                       features.max_groups[1], features.actions[1]);
2434     }
2435
2436     if (features.types & (1u << OFPGT11_INDIRECT)) {
2437         ds_put_format(string, "    Indirect group :\n");
2438         ds_put_format(string, "        max_groups = %#"PRIx32" "
2439                       "actions=0x%08"PRIx32"\n",
2440                       features.max_groups[2], features.actions[2]);
2441     }
2442
2443     if (features.types & (1u << OFPGT11_FF)) {
2444         ds_put_format(string, "    Fast Failover group :\n");
2445         ds_put_format(string, "        max_groups = %#"PRIx32" "
2446                       "actions=0x%08"PRIx32"\n",
2447                       features.max_groups[3], features.actions[3]);
2448     }
2449 }
2450
2451 static void
2452 ofp_print_group_mod(struct ds *s, const struct ofp_header *oh)
2453 {
2454     struct ofputil_group_mod gm;
2455     int error;
2456
2457     error = ofputil_decode_group_mod(oh, &gm);
2458     if (error) {
2459         ofp_print_error(s, error);
2460         return;
2461     }
2462
2463     ds_put_char(s, '\n');
2464
2465     ds_put_char(s, ' ');
2466     switch (gm.command) {
2467     case OFPGC11_ADD:
2468         ds_put_cstr(s, "ADD");
2469         break;
2470
2471     case OFPGC11_MODIFY:
2472         ds_put_cstr(s, "MOD");
2473         break;
2474
2475     case OFPGC11_DELETE:
2476         ds_put_cstr(s, "DEL");
2477         break;
2478
2479     default:
2480         ds_put_format(s, "cmd:%"PRIu16"", gm.command);
2481     }
2482     ds_put_char(s, ' ');
2483
2484     ofp_print_group(s, gm.group_id, gm.type, &gm.buckets);
2485 }
2486
2487 static void
2488 ofp_to_string__(const struct ofp_header *oh, enum ofpraw raw,
2489                 struct ds *string, int verbosity)
2490 {
2491     const void *msg = oh;
2492
2493     ofp_header_to_string__(oh, raw, string);
2494     switch (ofptype_from_ofpraw(raw)) {
2495
2496     case OFPTYPE_GROUP_STATS_REQUEST:
2497         ofp_print_stats_request(string, oh);
2498         ofp_print_ofpst_group_request(string, oh);
2499         break;
2500
2501     case OFPTYPE_GROUP_STATS_REPLY:
2502         ofp_print_group_stats(string, oh);
2503         break;
2504
2505     case OFPTYPE_GROUP_DESC_STATS_REQUEST:
2506         ofp_print_stats_request(string, oh);
2507         break;
2508
2509     case OFPTYPE_GROUP_DESC_STATS_REPLY:
2510         ofp_print_group_desc(string, oh);
2511         break;
2512
2513     case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
2514         ofp_print_stats_request(string, oh);
2515         break;
2516
2517     case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
2518         ofp_print_group_features(string, oh);
2519         break;
2520
2521     case OFPTYPE_GROUP_MOD:
2522         ofp_print_group_mod(string, oh);
2523         break;
2524
2525     case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
2526     case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
2527         ofp_print_not_implemented(string);
2528         break;
2529
2530     case OFPTYPE_HELLO:
2531         ofp_print_hello(string, oh);
2532         break;
2533
2534     case OFPTYPE_ERROR:
2535         ofp_print_error_msg(string, oh);
2536         break;
2537
2538     case OFPTYPE_ECHO_REQUEST:
2539     case OFPTYPE_ECHO_REPLY:
2540         ofp_print_echo(string, oh, verbosity);
2541         break;
2542
2543     case OFPTYPE_FEATURES_REQUEST:
2544         break;
2545
2546     case OFPTYPE_FEATURES_REPLY:
2547         ofp_print_switch_features(string, oh);
2548         break;
2549
2550     case OFPTYPE_GET_CONFIG_REQUEST:
2551         break;
2552
2553     case OFPTYPE_GET_CONFIG_REPLY:
2554     case OFPTYPE_SET_CONFIG:
2555         ofp_print_switch_config(string, ofpmsg_body(oh));
2556         break;
2557
2558     case OFPTYPE_PACKET_IN:
2559         ofp_print_packet_in(string, oh, verbosity);
2560         break;
2561
2562     case OFPTYPE_FLOW_REMOVED:
2563         ofp_print_flow_removed(string, oh);
2564         break;
2565
2566     case OFPTYPE_PORT_STATUS:
2567         ofp_print_port_status(string, oh);
2568         break;
2569
2570     case OFPTYPE_PACKET_OUT:
2571         ofp_print_packet_out(string, oh, verbosity);
2572         break;
2573
2574     case OFPTYPE_FLOW_MOD:
2575         ofp_print_flow_mod(string, oh, verbosity);
2576         break;
2577
2578     case OFPTYPE_PORT_MOD:
2579         ofp_print_port_mod(string, oh);
2580         break;
2581
2582     case OFPTYPE_TABLE_MOD:
2583         ofp_print_table_mod(string, oh);
2584         break;
2585
2586     case OFPTYPE_METER_MOD:
2587         ofp_print_meter_mod(string, oh);
2588         break;
2589
2590     case OFPTYPE_BARRIER_REQUEST:
2591     case OFPTYPE_BARRIER_REPLY:
2592         break;
2593
2594     case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
2595         ofp_print_queue_get_config_request(string, oh);
2596         break;
2597
2598     case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
2599         ofp_print_queue_get_config_reply(string, oh);
2600         break;
2601
2602     case OFPTYPE_ROLE_REQUEST:
2603     case OFPTYPE_ROLE_REPLY:
2604         ofp_print_role_message(string, oh);
2605         break;
2606     case OFPTYPE_ROLE_STATUS:
2607         ofp_print_role_status_message(string, oh);
2608         break;
2609
2610     case OFPTYPE_METER_STATS_REQUEST:
2611     case OFPTYPE_METER_CONFIG_STATS_REQUEST:
2612         ofp_print_stats_request(string, oh);
2613         ofp_print_meter_stats_request(string, oh);
2614         break;
2615
2616     case OFPTYPE_METER_STATS_REPLY:
2617         ofp_print_stats_reply(string, oh);
2618         ofp_print_meter_stats_reply(string, oh);
2619         break;
2620
2621     case OFPTYPE_METER_CONFIG_STATS_REPLY:
2622         ofp_print_stats_reply(string, oh);
2623         ofp_print_meter_config_reply(string, oh);
2624         break;
2625
2626     case OFPTYPE_METER_FEATURES_STATS_REPLY:
2627         ofp_print_stats_reply(string, oh);
2628         ofp_print_meter_features_reply(string, oh);
2629         break;
2630
2631     case OFPTYPE_DESC_STATS_REQUEST:
2632     case OFPTYPE_PORT_DESC_STATS_REQUEST:
2633     case OFPTYPE_METER_FEATURES_STATS_REQUEST:
2634         ofp_print_stats_request(string, oh);
2635         break;
2636
2637     case OFPTYPE_FLOW_STATS_REQUEST:
2638     case OFPTYPE_AGGREGATE_STATS_REQUEST:
2639         ofp_print_stats_request(string, oh);
2640         ofp_print_flow_stats_request(string, oh);
2641         break;
2642
2643     case OFPTYPE_TABLE_STATS_REQUEST:
2644         ofp_print_stats_request(string, oh);
2645         break;
2646
2647     case OFPTYPE_PORT_STATS_REQUEST:
2648         ofp_print_stats_request(string, oh);
2649         ofp_print_ofpst_port_request(string, oh);
2650         break;
2651
2652     case OFPTYPE_QUEUE_STATS_REQUEST:
2653         ofp_print_stats_request(string, oh);
2654         ofp_print_ofpst_queue_request(string, oh);
2655         break;
2656
2657     case OFPTYPE_DESC_STATS_REPLY:
2658         ofp_print_stats_reply(string, oh);
2659         ofp_print_ofpst_desc_reply(string, oh);
2660         break;
2661
2662     case OFPTYPE_FLOW_STATS_REPLY:
2663         ofp_print_stats_reply(string, oh);
2664         ofp_print_flow_stats_reply(string, oh);
2665         break;
2666
2667     case OFPTYPE_QUEUE_STATS_REPLY:
2668         ofp_print_stats_reply(string, oh);
2669         ofp_print_ofpst_queue_reply(string, oh, verbosity);
2670         break;
2671
2672     case OFPTYPE_PORT_STATS_REPLY:
2673         ofp_print_stats_reply(string, oh);
2674         ofp_print_ofpst_port_reply(string, oh, verbosity);
2675         break;
2676
2677     case OFPTYPE_TABLE_STATS_REPLY:
2678         ofp_print_stats_reply(string, oh);
2679         ofp_print_ofpst_table_reply(string, oh, verbosity);
2680         break;
2681
2682     case OFPTYPE_AGGREGATE_STATS_REPLY:
2683         ofp_print_stats_reply(string, oh);
2684         ofp_print_aggregate_stats_reply(string, oh);
2685         break;
2686
2687     case OFPTYPE_PORT_DESC_STATS_REPLY:
2688         ofp_print_stats_reply(string, oh);
2689         ofp_print_ofpst_port_desc_reply(string, oh);
2690         break;
2691
2692     case OFPTYPE_FLOW_MOD_TABLE_ID:
2693         ofp_print_nxt_flow_mod_table_id(string, ofpmsg_body(oh));
2694         break;
2695
2696     case OFPTYPE_SET_FLOW_FORMAT:
2697         ofp_print_nxt_set_flow_format(string, ofpmsg_body(oh));
2698         break;
2699
2700     case OFPTYPE_SET_PACKET_IN_FORMAT:
2701         ofp_print_nxt_set_packet_in_format(string, ofpmsg_body(oh));
2702         break;
2703
2704     case OFPTYPE_FLOW_AGE:
2705         break;
2706
2707     case OFPTYPE_SET_CONTROLLER_ID:
2708         ofp_print_nxt_set_controller_id(string, ofpmsg_body(oh));
2709         break;
2710
2711     case OFPTYPE_GET_ASYNC_REPLY:
2712     case OFPTYPE_SET_ASYNC_CONFIG:
2713         ofp_print_nxt_set_async_config(string, ofpmsg_body(oh));
2714         break;
2715     case OFPTYPE_GET_ASYNC_REQUEST:
2716         break;
2717     case OFPTYPE_FLOW_MONITOR_CANCEL:
2718         ofp_print_nxt_flow_monitor_cancel(string, msg);
2719         break;
2720
2721     case OFPTYPE_FLOW_MONITOR_PAUSED:
2722     case OFPTYPE_FLOW_MONITOR_RESUMED:
2723         break;
2724
2725     case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
2726         ofp_print_nxst_flow_monitor_request(string, msg);
2727         break;
2728
2729     case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
2730         ofp_print_nxst_flow_monitor_reply(string, msg);
2731         break;
2732     }
2733 }
2734
2735 /* Composes and returns a string representing the OpenFlow packet of 'len'
2736  * bytes at 'oh' at the given 'verbosity' level.  0 is a minimal amount of
2737  * verbosity and higher numbers increase verbosity.  The caller is responsible
2738  * for freeing the string. */
2739 char *
2740 ofp_to_string(const void *oh_, size_t len, int verbosity)
2741 {
2742     struct ds string = DS_EMPTY_INITIALIZER;
2743     const struct ofp_header *oh = oh_;
2744
2745     if (!len) {
2746         ds_put_cstr(&string, "OpenFlow message is empty\n");
2747     } else if (len < sizeof(struct ofp_header)) {
2748         ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
2749                       len);
2750     } else if (ntohs(oh->length) > len) {
2751         enum ofperr error;
2752         enum ofpraw raw;
2753
2754         error = ofpraw_decode_partial(&raw, oh, len);
2755         if (!error) {
2756             ofp_header_to_string__(oh, raw, &string);
2757             ds_put_char(&string, '\n');
2758         }
2759
2760         ds_put_format(&string,
2761                       "(***truncated to %zu bytes from %"PRIu16"***)\n",
2762                       len, ntohs(oh->length));
2763     } else if (ntohs(oh->length) < len) {
2764         ds_put_format(&string,
2765                       "(***only uses %"PRIu16" bytes out of %zu***)\n",
2766                       ntohs(oh->length), len);
2767     } else {
2768         enum ofperr error;
2769         enum ofpraw raw;
2770
2771         error = ofpraw_decode(&raw, oh);
2772         if (!error) {
2773             ofp_to_string__(oh, raw, &string, verbosity);
2774             if (verbosity >= 5) {
2775                 if (ds_last(&string) != '\n') {
2776                     ds_put_char(&string, '\n');
2777                 }
2778                 ds_put_hex_dump(&string, oh, len, 0, true);
2779             }
2780             if (ds_last(&string) != '\n') {
2781                 ds_put_char(&string, '\n');
2782             }
2783             return ds_steal_cstr(&string);
2784         }
2785
2786         ofp_print_error(&string, error);
2787     }
2788     ds_put_hex_dump(&string, oh, len, 0, true);
2789     return ds_steal_cstr(&string);
2790 }
2791 \f
2792 static void
2793 print_and_free(FILE *stream, char *string)
2794 {
2795     fputs(string, stream);
2796     free(string);
2797 }
2798
2799 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
2800  * given 'verbosity' level.  0 is a minimal amount of verbosity and higher
2801  * numbers increase verbosity. */
2802 void
2803 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
2804 {
2805     print_and_free(stream, ofp_to_string(oh, len, verbosity));
2806 }
2807
2808 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
2809  * 'data' to 'stream'. */
2810 void
2811 ofp_print_packet(FILE *stream, const void *data, size_t len)
2812 {
2813     print_and_free(stream, ofp_packet_to_string(data, len));
2814 }