OpenFlow-level flow-based tunneling support.
[sliver-openvswitch.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, 0, &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         }
78     }
79
80     ds_put_char(&ds, '\n');
81
82     return ds_cstr(&ds);
83 }
84
85 static void
86 ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
87                     int verbosity)
88 {
89     struct ofputil_packet_in pin;
90     int error;
91     int i;
92
93     error = ofputil_decode_packet_in(&pin, oh);
94     if (error) {
95         ofp_print_error(string, error);
96         return;
97     }
98
99     if (pin.table_id) {
100         ds_put_format(string, " table_id=%"PRIu8, pin.table_id);
101     }
102
103     if (pin.cookie) {
104         ds_put_format(string, " cookie=0x%"PRIx64, ntohll(pin.cookie));
105     }
106
107     ds_put_format(string, " total_len=%"PRIu16" in_port=", pin.total_len);
108     ofputil_format_port(pin.fmd.in_port, string);
109
110     if (pin.fmd.tun_id != htonll(0)) {
111         ds_put_format(string, " tun_id=0x%"PRIx64, ntohll(pin.fmd.tun_id));
112     }
113
114     if (pin.fmd.tun_src != htonl(0)) {
115         ds_put_format(string, " tun_src="IP_FMT, IP_ARGS(pin.fmd.tun_src));
116     }
117
118     if (pin.fmd.tun_dst != htonl(0)) {
119         ds_put_format(string, " tun_dst="IP_FMT, IP_ARGS(pin.fmd.tun_dst));
120     }
121
122     if (pin.fmd.metadata != htonll(0)) {
123         ds_put_format(string, " metadata=0x%"PRIx64, ntohll(pin.fmd.metadata));
124     }
125
126     for (i = 0; i < FLOW_N_REGS; i++) {
127         if (pin.fmd.regs[i]) {
128             ds_put_format(string, " reg%d=0x%"PRIx32, i, pin.fmd.regs[i]);
129         }
130     }
131
132     ds_put_format(string, " (via %s)",
133                   ofputil_packet_in_reason_to_string(pin.reason));
134
135     ds_put_format(string, " data_len=%zu", pin.packet_len);
136     if (pin.buffer_id == UINT32_MAX) {
137         ds_put_format(string, " (unbuffered)");
138         if (pin.total_len != pin.packet_len) {
139             ds_put_format(string, " (***total_len != data_len***)");
140         }
141     } else {
142         ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
143         if (pin.total_len < pin.packet_len) {
144             ds_put_format(string, " (***total_len < data_len***)");
145         }
146     }
147     ds_put_char(string, '\n');
148
149     if (verbosity > 0) {
150         char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
151         ds_put_cstr(string, packet);
152         free(packet);
153     }
154 }
155
156 static void
157 ofp_print_packet_out(struct ds *string, const struct ofp_header *oh,
158                      int verbosity)
159 {
160     struct ofputil_packet_out po;
161     struct ofpbuf ofpacts;
162     enum ofperr error;
163
164     ofpbuf_init(&ofpacts, 64);
165     error = ofputil_decode_packet_out(&po, oh, &ofpacts);
166     if (error) {
167         ofpbuf_uninit(&ofpacts);
168         ofp_print_error(string, error);
169         return;
170     }
171
172     ds_put_cstr(string, " in_port=");
173     ofputil_format_port(po.in_port, string);
174
175     ds_put_char(string, ' ');
176     ofpacts_format(po.ofpacts, po.ofpacts_len, string);
177
178     if (po.buffer_id == UINT32_MAX) {
179         ds_put_format(string, " data_len=%zu", po.packet_len);
180         if (verbosity > 0 && po.packet_len > 0) {
181             char *packet = ofp_packet_to_string(po.packet, po.packet_len);
182             ds_put_char(string, '\n');
183             ds_put_cstr(string, packet);
184             free(packet);
185         }
186     } else {
187         ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id);
188     }
189     ds_put_char(string, '\n');
190
191     ofpbuf_uninit(&ofpacts);
192 }
193
194 /* qsort comparison function. */
195 static int
196 compare_ports(const void *a_, const void *b_)
197 {
198     const struct ofputil_phy_port *a = a_;
199     const struct ofputil_phy_port *b = b_;
200     uint16_t ap = a->port_no;
201     uint16_t bp = b->port_no;
202
203     return ap < bp ? -1 : ap > bp;
204 }
205
206 static void
207 ofp_print_bit_names(struct ds *string, uint32_t bits,
208                     const char *(*bit_to_name)(uint32_t bit),
209                     char separator)
210 {
211     int n = 0;
212     int i;
213
214     if (!bits) {
215         ds_put_cstr(string, "0");
216         return;
217     }
218
219     for (i = 0; i < 32; i++) {
220         uint32_t bit = UINT32_C(1) << i;
221
222         if (bits & bit) {
223             const char *name = bit_to_name(bit);
224             if (name) {
225                 if (n++) {
226                     ds_put_char(string, separator);
227                 }
228                 ds_put_cstr(string, name);
229                 bits &= ~bit;
230             }
231         }
232     }
233
234     if (bits) {
235         if (n) {
236             ds_put_char(string, separator);
237         }
238         ds_put_format(string, "0x%"PRIx32, bits);
239     }
240 }
241
242 static const char *
243 netdev_feature_to_name(uint32_t bit)
244 {
245     enum netdev_features f = bit;
246
247     switch (f) {
248     case NETDEV_F_10MB_HD:    return "10MB-HD";
249     case NETDEV_F_10MB_FD:    return "10MB-FD";
250     case NETDEV_F_100MB_HD:   return "100MB-HD";
251     case NETDEV_F_100MB_FD:   return "100MB-FD";
252     case NETDEV_F_1GB_HD:     return "1GB-HD";
253     case NETDEV_F_1GB_FD:     return "1GB-FD";
254     case NETDEV_F_10GB_FD:    return "10GB-FD";
255     case NETDEV_F_40GB_FD:    return "40GB-FD";
256     case NETDEV_F_100GB_FD:   return "100GB-FD";
257     case NETDEV_F_1TB_FD:     return "1TB-FD";
258     case NETDEV_F_OTHER:      return "OTHER";
259     case NETDEV_F_COPPER:     return "COPPER";
260     case NETDEV_F_FIBER:      return "FIBER";
261     case NETDEV_F_AUTONEG:    return "AUTO_NEG";
262     case NETDEV_F_PAUSE:      return "AUTO_PAUSE";
263     case NETDEV_F_PAUSE_ASYM: return "AUTO_PAUSE_ASYM";
264     }
265
266     return NULL;
267 }
268
269 static void
270 ofp_print_port_features(struct ds *string, enum netdev_features features)
271 {
272     ofp_print_bit_names(string, features, netdev_feature_to_name, ' ');
273     ds_put_char(string, '\n');
274 }
275
276 static const char *
277 ofputil_port_config_to_name(uint32_t bit)
278 {
279     enum ofputil_port_config pc = bit;
280
281     switch (pc) {
282     case OFPUTIL_PC_PORT_DOWN:    return "PORT_DOWN";
283     case OFPUTIL_PC_NO_STP:       return "NO_STP";
284     case OFPUTIL_PC_NO_RECV:      return "NO_RECV";
285     case OFPUTIL_PC_NO_RECV_STP:  return "NO_RECV_STP";
286     case OFPUTIL_PC_NO_FLOOD:     return "NO_FLOOD";
287     case OFPUTIL_PC_NO_FWD:       return "NO_FWD";
288     case OFPUTIL_PC_NO_PACKET_IN: return "NO_PACKET_IN";
289     }
290
291     return NULL;
292 }
293
294 static void
295 ofp_print_port_config(struct ds *string, enum ofputil_port_config config)
296 {
297     ofp_print_bit_names(string, config, ofputil_port_config_to_name, ' ');
298     ds_put_char(string, '\n');
299 }
300
301 static const char *
302 ofputil_port_state_to_name(uint32_t bit)
303 {
304     enum ofputil_port_state ps = bit;
305
306     switch (ps) {
307     case OFPUTIL_PS_LINK_DOWN: return "LINK_DOWN";
308     case OFPUTIL_PS_BLOCKED:   return "BLOCKED";
309     case OFPUTIL_PS_LIVE:      return "LIVE";
310
311     case OFPUTIL_PS_STP_LISTEN:
312     case OFPUTIL_PS_STP_LEARN:
313     case OFPUTIL_PS_STP_FORWARD:
314     case OFPUTIL_PS_STP_BLOCK:
315         /* Handled elsewhere. */
316         return NULL;
317     }
318
319     return NULL;
320 }
321
322 static void
323 ofp_print_port_state(struct ds *string, enum ofputil_port_state state)
324 {
325     enum ofputil_port_state stp_state;
326
327     /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
328      * pattern.  We have to special case it.
329      *
330      * OVS doesn't support STP, so this field will always be 0 if we are
331      * talking to OVS, so we'd always print STP_LISTEN in that case.
332      * Therefore, we don't print anything at all if the value is STP_LISTEN, to
333      * avoid confusing users. */
334     stp_state = state & OFPUTIL_PS_STP_MASK;
335     if (stp_state) {
336         ds_put_cstr(string,
337                     (stp_state == OFPUTIL_PS_STP_LEARN ? "STP_LEARN"
338                      : stp_state == OFPUTIL_PS_STP_FORWARD ? "STP_FORWARD"
339                      : "STP_BLOCK"));
340         state &= ~OFPUTIL_PS_STP_MASK;
341         if (state) {
342             ofp_print_bit_names(string, state, ofputil_port_state_to_name,
343                                 ' ');
344         }
345     } else {
346         ofp_print_bit_names(string, state, ofputil_port_state_to_name, ' ');
347     }
348     ds_put_char(string, '\n');
349 }
350
351 static void
352 ofp_print_phy_port(struct ds *string, const struct ofputil_phy_port *port)
353 {
354     char name[sizeof port->name];
355     int j;
356
357     memcpy(name, port->name, sizeof name);
358     for (j = 0; j < sizeof name - 1; j++) {
359         if (!isprint((unsigned char) name[j])) {
360             break;
361         }
362     }
363     name[j] = '\0';
364
365     ds_put_char(string, ' ');
366     ofputil_format_port(port->port_no, string);
367     ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
368                   name, ETH_ADDR_ARGS(port->hw_addr));
369
370     ds_put_cstr(string, "     config:     ");
371     ofp_print_port_config(string, port->config);
372
373     ds_put_cstr(string, "     state:      ");
374     ofp_print_port_state(string, port->state);
375
376     if (port->curr) {
377         ds_put_format(string, "     current:    ");
378         ofp_print_port_features(string, port->curr);
379     }
380     if (port->advertised) {
381         ds_put_format(string, "     advertised: ");
382         ofp_print_port_features(string, port->advertised);
383     }
384     if (port->supported) {
385         ds_put_format(string, "     supported:  ");
386         ofp_print_port_features(string, port->supported);
387     }
388     if (port->peer) {
389         ds_put_format(string, "     peer:       ");
390         ofp_print_port_features(string, port->peer);
391     }
392     ds_put_format(string, "     speed: %"PRIu32" Mbps now, "
393                   "%"PRIu32" Mbps max\n",
394                   port->curr_speed / UINT32_C(1000),
395                   port->max_speed / UINT32_C(1000));
396 }
397
398 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
399  * 'ofp_version', writes a detailed description of each port into
400  * 'string'. */
401 static void
402 ofp_print_phy_ports(struct ds *string, uint8_t ofp_version,
403                     struct ofpbuf *b)
404 {
405     size_t n_ports;
406     struct ofputil_phy_port *ports;
407     enum ofperr error;
408     size_t i;
409
410     n_ports = ofputil_count_phy_ports(ofp_version, b);
411
412     ports = xmalloc(n_ports * sizeof *ports);
413     for (i = 0; i < n_ports; i++) {
414         error = ofputil_pull_phy_port(ofp_version, b, &ports[i]);
415         if (error) {
416             ofp_print_error(string, error);
417             goto exit;
418         }
419     }
420     qsort(ports, n_ports, sizeof *ports, compare_ports);
421     for (i = 0; i < n_ports; i++) {
422         ofp_print_phy_port(string, &ports[i]);
423     }
424
425 exit:
426     free(ports);
427 }
428
429 static const char *
430 ofputil_capabilities_to_name(uint32_t bit)
431 {
432     enum ofputil_capabilities capabilities = bit;
433
434     switch (capabilities) {
435     case OFPUTIL_C_FLOW_STATS:   return "FLOW_STATS";
436     case OFPUTIL_C_TABLE_STATS:  return "TABLE_STATS";
437     case OFPUTIL_C_PORT_STATS:   return "PORT_STATS";
438     case OFPUTIL_C_IP_REASM:     return "IP_REASM";
439     case OFPUTIL_C_QUEUE_STATS:  return "QUEUE_STATS";
440     case OFPUTIL_C_ARP_MATCH_IP: return "ARP_MATCH_IP";
441     case OFPUTIL_C_STP:          return "STP";
442     case OFPUTIL_C_GROUP_STATS:  return "GROUP_STATS";
443     case OFPUTIL_C_PORT_BLOCKED: return "PORT_BLOCKED";
444     }
445
446     return NULL;
447 }
448
449 static const char *
450 ofputil_action_bitmap_to_name(uint32_t bit)
451 {
452     enum ofputil_action_bitmap action = bit;
453
454     switch (action) {
455     case OFPUTIL_A_OUTPUT:         return "OUTPUT";
456     case OFPUTIL_A_SET_VLAN_VID:   return "SET_VLAN_VID";
457     case OFPUTIL_A_SET_VLAN_PCP:   return "SET_VLAN_PCP";
458     case OFPUTIL_A_STRIP_VLAN:     return "STRIP_VLAN";
459     case OFPUTIL_A_SET_DL_SRC:     return "SET_DL_SRC";
460     case OFPUTIL_A_SET_DL_DST:     return "SET_DL_DST";
461     case OFPUTIL_A_SET_NW_SRC:     return "SET_NW_SRC";
462     case OFPUTIL_A_SET_NW_DST:     return "SET_NW_DST";
463     case OFPUTIL_A_SET_NW_ECN:     return "SET_NW_ECN";
464     case OFPUTIL_A_SET_NW_TOS:     return "SET_NW_TOS";
465     case OFPUTIL_A_SET_TP_SRC:     return "SET_TP_SRC";
466     case OFPUTIL_A_SET_TP_DST:     return "SET_TP_DST";
467     case OFPUTIL_A_SET_FIELD:      return "SET_FIELD";
468     case OFPUTIL_A_ENQUEUE:        return "ENQUEUE";
469     case OFPUTIL_A_COPY_TTL_OUT:   return "COPY_TTL_OUT";
470     case OFPUTIL_A_COPY_TTL_IN:    return "COPY_TTL_IN";
471     case OFPUTIL_A_SET_MPLS_LABEL: return "SET_MPLS_LABEL";
472     case OFPUTIL_A_SET_MPLS_TC:    return "SET_MPLS_TC";
473     case OFPUTIL_A_SET_MPLS_TTL:   return "SET_MPLS_TTL";
474     case OFPUTIL_A_DEC_MPLS_TTL:   return "DEC_MPLS_TTL";
475     case OFPUTIL_A_PUSH_VLAN:      return "PUSH_VLAN";
476     case OFPUTIL_A_POP_VLAN:       return "POP_VLAN";
477     case OFPUTIL_A_PUSH_MPLS:      return "PUSH_MPLS";
478     case OFPUTIL_A_POP_MPLS:       return "POP_MPLS";
479     case OFPUTIL_A_SET_QUEUE:      return "SET_QUEUE";
480     case OFPUTIL_A_GROUP:          return "GROUP";
481     case OFPUTIL_A_SET_NW_TTL:     return "SET_NW_TTL";
482     case OFPUTIL_A_DEC_NW_TTL:     return "DEC_NW_TTL";
483     }
484
485     return NULL;
486 }
487
488 static void
489 ofp_print_switch_features(struct ds *string, const struct ofp_header *oh)
490 {
491     struct ofputil_switch_features features;
492     enum ofperr error;
493     struct ofpbuf b;
494
495     error = ofputil_decode_switch_features(oh, &features, &b);
496     if (error) {
497         ofp_print_error(string, error);
498         return;
499     }
500
501     ds_put_format(string, " dpid:%016"PRIx64"\n", features.datapath_id);
502
503     ds_put_format(string, "n_tables:%"PRIu8", n_buffers:%"PRIu32,
504                   features.n_tables, features.n_buffers);
505     if (features.auxiliary_id) {
506         ds_put_format(string, ", auxiliary_id:%"PRIu8, features.auxiliary_id);
507     }
508     ds_put_char(string, '\n');
509
510     ds_put_cstr(string, "capabilities: ");
511     ofp_print_bit_names(string, features.capabilities,
512                         ofputil_capabilities_to_name, ' ');
513     ds_put_char(string, '\n');
514
515     switch ((enum ofp_version)oh->version) {
516     case OFP10_VERSION:
517         ds_put_cstr(string, "actions: ");
518         ofp_print_bit_names(string, features.actions,
519                             ofputil_action_bitmap_to_name, ' ');
520         ds_put_char(string, '\n');
521         break;
522     case OFP11_VERSION:
523     case OFP12_VERSION:
524         break;
525     case OFP13_VERSION:
526         return; /* no ports in ofp13_switch_features */
527     default:
528         NOT_REACHED();
529     }
530
531     ofp_print_phy_ports(string, oh->version, &b);
532 }
533
534 static void
535 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
536 {
537     enum ofp_config_flags flags;
538
539     flags = ntohs(osc->flags);
540
541     ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
542     flags &= ~OFPC_FRAG_MASK;
543
544     if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) {
545         ds_put_format(string, " invalid_ttl_to_controller");
546         flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER;
547     }
548
549     if (flags) {
550         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
551     }
552
553     ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
554 }
555
556 static void print_wild(struct ds *string, const char *leader, int is_wild,
557             int verbosity, const char *format, ...)
558             __attribute__((format(printf, 5, 6)));
559
560 static void print_wild(struct ds *string, const char *leader, int is_wild,
561                        int verbosity, const char *format, ...)
562 {
563     if (is_wild && verbosity < 2) {
564         return;
565     }
566     ds_put_cstr(string, leader);
567     if (!is_wild) {
568         va_list args;
569
570         va_start(args, format);
571         ds_put_format_valist(string, format, args);
572         va_end(args);
573     } else {
574         ds_put_char(string, '*');
575     }
576     ds_put_char(string, ',');
577 }
578
579 static void
580 print_wild_port(struct ds *string, const char *leader, int is_wild,
581                 int verbosity, uint16_t port)
582 {
583     if (is_wild && verbosity < 2) {
584         return;
585     }
586     ds_put_cstr(string, leader);
587     if (!is_wild) {
588         ofputil_format_port(port, string);
589     } else {
590         ds_put_char(string, '*');
591     }
592     ds_put_char(string, ',');
593 }
594
595 static void
596 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
597                  uint32_t wild_bits, int verbosity)
598 {
599     if (wild_bits >= 32 && verbosity < 2) {
600         return;
601     }
602     ds_put_cstr(string, leader);
603     if (wild_bits < 32) {
604         ds_put_format(string, IP_FMT, IP_ARGS(ip));
605         if (wild_bits) {
606             ds_put_format(string, "/%d", 32 - wild_bits);
607         }
608     } else {
609         ds_put_char(string, '*');
610     }
611     ds_put_char(string, ',');
612 }
613
614 void
615 ofp10_match_print(struct ds *f, const struct ofp10_match *om, int verbosity)
616 {
617     char *s = ofp10_match_to_string(om, verbosity);
618     ds_put_cstr(f, s);
619     free(s);
620 }
621
622 char *
623 ofp10_match_to_string(const struct ofp10_match *om, int verbosity)
624 {
625     struct ds f = DS_EMPTY_INITIALIZER;
626     uint32_t w = ntohl(om->wildcards);
627     bool skip_type = false;
628     bool skip_proto = false;
629
630     if (!(w & OFPFW10_DL_TYPE)) {
631         skip_type = true;
632         if (om->dl_type == htons(ETH_TYPE_IP)) {
633             if (!(w & OFPFW10_NW_PROTO)) {
634                 skip_proto = true;
635                 if (om->nw_proto == IPPROTO_ICMP) {
636                     ds_put_cstr(&f, "icmp,");
637                 } else if (om->nw_proto == IPPROTO_TCP) {
638                     ds_put_cstr(&f, "tcp,");
639                 } else if (om->nw_proto == IPPROTO_UDP) {
640                     ds_put_cstr(&f, "udp,");
641                 } else {
642                     ds_put_cstr(&f, "ip,");
643                     skip_proto = false;
644                 }
645             } else {
646                 ds_put_cstr(&f, "ip,");
647             }
648         } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
649             ds_put_cstr(&f, "arp,");
650         } else if (om->dl_type == htons(ETH_TYPE_RARP)){
651             ds_put_cstr(&f, "rarp,");
652         } else if (om->dl_type == htons(ETH_TYPE_MPLS)) {
653             ds_put_cstr(&f, "mpls,");
654         } else if (om->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
655             ds_put_cstr(&f, "mplsm,");
656         } else {
657             skip_type = false;
658         }
659     }
660     print_wild_port(&f, "in_port=", w & OFPFW10_IN_PORT, verbosity,
661                     ntohs(om->in_port));
662     print_wild(&f, "dl_vlan=", w & OFPFW10_DL_VLAN, verbosity,
663                "%d", ntohs(om->dl_vlan));
664     print_wild(&f, "dl_vlan_pcp=", w & OFPFW10_DL_VLAN_PCP, verbosity,
665                "%d", om->dl_vlan_pcp);
666     print_wild(&f, "dl_src=", w & OFPFW10_DL_SRC, verbosity,
667                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
668     print_wild(&f, "dl_dst=", w & OFPFW10_DL_DST, verbosity,
669                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
670     if (!skip_type) {
671         print_wild(&f, "dl_type=", w & OFPFW10_DL_TYPE, verbosity,
672                    "0x%04x", ntohs(om->dl_type));
673     }
674     print_ip_netmask(&f, "nw_src=", om->nw_src,
675                      (w & OFPFW10_NW_SRC_MASK) >> OFPFW10_NW_SRC_SHIFT,
676                      verbosity);
677     print_ip_netmask(&f, "nw_dst=", om->nw_dst,
678                      (w & OFPFW10_NW_DST_MASK) >> OFPFW10_NW_DST_SHIFT,
679                      verbosity);
680     if (!skip_proto) {
681         if (om->dl_type == htons(ETH_TYPE_ARP) ||
682             om->dl_type == htons(ETH_TYPE_RARP)) {
683             print_wild(&f, "arp_op=", w & OFPFW10_NW_PROTO, verbosity,
684                        "%u", om->nw_proto);
685         } else {
686             print_wild(&f, "nw_proto=", w & OFPFW10_NW_PROTO, verbosity,
687                        "%u", om->nw_proto);
688         }
689     }
690     print_wild(&f, "nw_tos=", w & OFPFW10_NW_TOS, verbosity,
691                "%u", om->nw_tos);
692     if (om->nw_proto == IPPROTO_ICMP) {
693         print_wild(&f, "icmp_type=", w & OFPFW10_ICMP_TYPE, verbosity,
694                    "%d", ntohs(om->tp_src));
695         print_wild(&f, "icmp_code=", w & OFPFW10_ICMP_CODE, verbosity,
696                    "%d", ntohs(om->tp_dst));
697     } else {
698         print_wild(&f, "tp_src=", w & OFPFW10_TP_SRC, verbosity,
699                    "%d", ntohs(om->tp_src));
700         print_wild(&f, "tp_dst=", w & OFPFW10_TP_DST, verbosity,
701                    "%d", ntohs(om->tp_dst));
702     }
703     if (ds_last(&f) == ',') {
704         f.length--;
705     }
706     return ds_cstr(&f);
707 }
708
709 static void
710 ofp_print_flow_flags(struct ds *s, uint16_t flags)
711 {
712     if (flags & OFPFF_SEND_FLOW_REM) {
713         ds_put_cstr(s, "send_flow_rem ");
714     }
715     if (flags & OFPFF_CHECK_OVERLAP) {
716         ds_put_cstr(s, "check_overlap ");
717     }
718     if (flags & OFPFF12_RESET_COUNTS) {
719         ds_put_cstr(s, "reset_counts ");
720     }
721     if (flags & OFPFF13_NO_PKT_COUNTS) {
722         ds_put_cstr(s, "no_packet_counts ");
723     }
724     if (flags & OFPFF13_NO_BYT_COUNTS) {
725         ds_put_cstr(s, "no_byte_counts ");
726     }
727
728     flags &= ~(OFPFF_SEND_FLOW_REM | OFPFF_CHECK_OVERLAP
729                | OFPFF12_RESET_COUNTS
730                | OFPFF13_NO_PKT_COUNTS | OFPFF13_NO_BYT_COUNTS);
731     if (flags) {
732         ds_put_format(s, "flags:0x%"PRIx16" ", flags);
733     }
734 }
735
736 static void
737 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh, int verbosity)
738 {
739     struct ofputil_flow_mod fm;
740     struct ofpbuf ofpacts;
741     bool need_priority;
742     enum ofperr error;
743     enum ofpraw raw;
744     enum ofputil_protocol protocol;
745
746     protocol = ofputil_protocol_from_ofp_version(oh->version);
747     protocol = ofputil_protocol_set_tid(protocol, true);
748
749     ofpbuf_init(&ofpacts, 64);
750     error = ofputil_decode_flow_mod(&fm, oh, protocol, &ofpacts);
751     if (error) {
752         ofpbuf_uninit(&ofpacts);
753         ofp_print_error(s, error);
754         return;
755     }
756
757     ds_put_char(s, ' ');
758     switch (fm.command) {
759     case OFPFC_ADD:
760         ds_put_cstr(s, "ADD");
761         break;
762     case OFPFC_MODIFY:
763         ds_put_cstr(s, "MOD");
764         break;
765     case OFPFC_MODIFY_STRICT:
766         ds_put_cstr(s, "MOD_STRICT");
767         break;
768     case OFPFC_DELETE:
769         ds_put_cstr(s, "DEL");
770         break;
771     case OFPFC_DELETE_STRICT:
772         ds_put_cstr(s, "DEL_STRICT");
773         break;
774     default:
775         ds_put_format(s, "cmd:%d", fm.command);
776     }
777     if (fm.table_id != 0) {
778         ds_put_format(s, " table:%d", fm.table_id);
779     }
780
781     ds_put_char(s, ' ');
782     ofpraw_decode(&raw, oh);
783     if (verbosity >= 3 && raw == OFPRAW_OFPT10_FLOW_MOD) {
784         const struct ofp10_flow_mod *ofm = ofpmsg_body(oh);
785         ofp10_match_print(s, &ofm->match, verbosity);
786
787         /* ofp_print_match() doesn't print priority. */
788         need_priority = true;
789     } else if (verbosity >= 3 && raw == OFPRAW_NXT_FLOW_MOD) {
790         const struct nx_flow_mod *nfm = ofpmsg_body(oh);
791         const void *nxm = nfm + 1;
792         char *nxm_s;
793
794         nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
795         ds_put_cstr(s, nxm_s);
796         free(nxm_s);
797
798         /* nx_match_to_string() doesn't print priority. */
799         need_priority = true;
800     } else {
801         match_format(&fm.match, s, fm.priority);
802
803         /* match_format() does print priority. */
804         need_priority = false;
805     }
806
807     if (ds_last(s) != ' ') {
808         ds_put_char(s, ' ');
809     }
810     if (fm.new_cookie != htonll(0) && fm.new_cookie != htonll(UINT64_MAX)) {
811         ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.new_cookie));
812     }
813     if (fm.cookie_mask != htonll(0)) {
814         ds_put_format(s, "cookie:0x%"PRIx64"/0x%"PRIx64" ",
815                 ntohll(fm.cookie), ntohll(fm.cookie_mask));
816     }
817     if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
818         ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
819     }
820     if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
821         ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
822     }
823     if (fm.priority != OFP_DEFAULT_PRIORITY && need_priority) {
824         ds_put_format(s, "pri:%"PRIu16" ", fm.priority);
825     }
826     if (fm.buffer_id != UINT32_MAX) {
827         ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
828     }
829     if (fm.out_port != OFPP_ANY) {
830         ds_put_format(s, "out_port:");
831         ofputil_format_port(fm.out_port, s);
832         ds_put_char(s, ' ');
833     }
834     if (fm.flags != 0) {
835         ofp_print_flow_flags(s, fm.flags);
836     }
837
838     ofpacts_format(fm.ofpacts, fm.ofpacts_len, s);
839     ofpbuf_uninit(&ofpacts);
840 }
841
842 static void
843 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
844 {
845     ds_put_format(string, "%u", sec);
846     if (nsec > 0) {
847         ds_put_format(string, ".%09u", nsec);
848         while (string->string[string->length - 1] == '0') {
849             string->length--;
850         }
851     }
852     ds_put_char(string, 's');
853 }
854
855 static const char *
856 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason)
857 {
858     static char s[32];
859
860     switch (reason) {
861     case OFPRR_IDLE_TIMEOUT:
862         return "idle";
863     case OFPRR_HARD_TIMEOUT:
864         return "hard";
865     case OFPRR_DELETE:
866         return "delete";
867     case OFPRR_GROUP_DELETE:
868         return "group_delete";
869     case OFPRR_EVICTION:
870         return "eviction";
871     default:
872         sprintf(s, "%d", (int) reason);
873         return s;
874     }
875 }
876
877 static void
878 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
879 {
880     struct ofputil_flow_removed fr;
881     enum ofperr error;
882
883     error = ofputil_decode_flow_removed(&fr, oh);
884     if (error) {
885         ofp_print_error(string, error);
886         return;
887     }
888
889     ds_put_char(string, ' ');
890     match_format(&fr.match, string, fr.priority);
891
892     ds_put_format(string, " reason=%s",
893                   ofp_flow_removed_reason_to_string(fr.reason));
894
895     if (fr.table_id != 255) {
896         ds_put_format(string, " table_id=%"PRIu8, fr.table_id);
897     }
898
899     if (fr.cookie != htonll(0)) {
900         ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
901     }
902     ds_put_cstr(string, " duration");
903     ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
904     ds_put_format(string, " idle%"PRIu16, fr.idle_timeout);
905     if (fr.hard_timeout) {
906         /* The hard timeout was only added in OF1.2, so only print it if it is
907          * actually in use to avoid gratuitous change to the formatting. */
908         ds_put_format(string, " hard%"PRIu16, fr.hard_timeout);
909     }
910     ds_put_format(string, " pkts%"PRIu64" bytes%"PRIu64"\n",
911                   fr.packet_count, fr.byte_count);
912 }
913
914 static void
915 ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
916 {
917     struct ofputil_port_mod pm;
918     enum ofperr error;
919
920     error = ofputil_decode_port_mod(oh, &pm);
921     if (error) {
922         ofp_print_error(string, error);
923         return;
924     }
925
926     ds_put_cstr(string, "port: ");
927     ofputil_format_port(pm.port_no, string);
928     ds_put_format(string, ": addr:"ETH_ADDR_FMT"\n",
929                   ETH_ADDR_ARGS(pm.hw_addr));
930
931     ds_put_cstr(string, "     config: ");
932     ofp_print_port_config(string, pm.config);
933
934     ds_put_cstr(string, "     mask:   ");
935     ofp_print_port_config(string, pm.mask);
936
937     ds_put_cstr(string, "     advertise: ");
938     if (pm.advertise) {
939         ofp_print_port_features(string, pm.advertise);
940     } else {
941         ds_put_cstr(string, "UNCHANGED\n");
942     }
943 }
944
945 static void
946 ofp_print_error(struct ds *string, enum ofperr error)
947 {
948     if (string->length) {
949         ds_put_char(string, ' ');
950     }
951     ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
952 }
953
954 static void
955 ofp_print_hello(struct ds *string, const struct ofp_header *oh)
956 {
957     uint32_t allowed_versions;
958     bool ok;
959
960     ok = ofputil_decode_hello(oh, &allowed_versions);
961
962     ds_put_cstr(string, "\n version bitmap: ");
963     ofputil_format_version_bitmap(string, allowed_versions);
964
965     if (!ok) {
966         ds_put_cstr(string, "\n unknown data in hello:\n");
967         ds_put_hex_dump(string, oh, ntohs(oh->length), 0, true);
968     }
969 }
970
971 static void
972 ofp_print_error_msg(struct ds *string, const struct ofp_header *oh)
973 {
974     size_t len = ntohs(oh->length);
975     struct ofpbuf payload;
976     enum ofperr error;
977     char *s;
978
979     error = ofperr_decode_msg(oh, &payload);
980     if (!error) {
981         ds_put_cstr(string, "***decode error***");
982         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
983         return;
984     }
985
986     ds_put_format(string, " %s\n", ofperr_get_name(error));
987
988     if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
989         ds_put_printable(string, payload.data, payload.size);
990     } else {
991         s = ofp_to_string(payload.data, payload.size, 1);
992         ds_put_cstr(string, s);
993         free(s);
994     }
995 }
996
997 static void
998 ofp_print_port_status(struct ds *string, const struct ofp_header *oh)
999 {
1000     struct ofputil_port_status ps;
1001     enum ofperr error;
1002
1003     error = ofputil_decode_port_status(oh, &ps);
1004     if (error) {
1005         ofp_print_error(string, error);
1006         return;
1007     }
1008
1009     if (ps.reason == OFPPR_ADD) {
1010         ds_put_format(string, " ADD:");
1011     } else if (ps.reason == OFPPR_DELETE) {
1012         ds_put_format(string, " DEL:");
1013     } else if (ps.reason == OFPPR_MODIFY) {
1014         ds_put_format(string, " MOD:");
1015     }
1016
1017     ofp_print_phy_port(string, &ps.desc);
1018 }
1019
1020 static void
1021 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
1022 {
1023     const struct ofp_desc_stats *ods = ofpmsg_body(oh);
1024
1025     ds_put_char(string, '\n');
1026     ds_put_format(string, "Manufacturer: %.*s\n",
1027             (int) sizeof ods->mfr_desc, ods->mfr_desc);
1028     ds_put_format(string, "Hardware: %.*s\n",
1029             (int) sizeof ods->hw_desc, ods->hw_desc);
1030     ds_put_format(string, "Software: %.*s\n",
1031             (int) sizeof ods->sw_desc, ods->sw_desc);
1032     ds_put_format(string, "Serial Num: %.*s\n",
1033             (int) sizeof ods->serial_num, ods->serial_num);
1034     ds_put_format(string, "DP Description: %.*s\n",
1035             (int) sizeof ods->dp_desc, ods->dp_desc);
1036 }
1037
1038 static void
1039 ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
1040 {
1041     struct ofputil_flow_stats_request fsr;
1042     enum ofperr error;
1043
1044     error = ofputil_decode_flow_stats_request(&fsr, oh);
1045     if (error) {
1046         ofp_print_error(string, error);
1047         return;
1048     }
1049
1050     if (fsr.table_id != 0xff) {
1051         ds_put_format(string, " table=%"PRIu8, fsr.table_id);
1052     }
1053
1054     if (fsr.out_port != OFPP_ANY) {
1055         ds_put_cstr(string, " out_port=");
1056         ofputil_format_port(fsr.out_port, string);
1057     }
1058
1059     ds_put_char(string, ' ');
1060     match_format(&fsr.match, string, OFP_DEFAULT_PRIORITY);
1061 }
1062
1063 void
1064 ofp_print_flow_stats(struct ds *string, struct ofputil_flow_stats *fs)
1065 {
1066     ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1067                   ntohll(fs->cookie));
1068
1069     ofp_print_duration(string, fs->duration_sec, fs->duration_nsec);
1070     ds_put_format(string, ", table=%"PRIu8", ", fs->table_id);
1071     ds_put_format(string, "n_packets=%"PRIu64", ", fs->packet_count);
1072     ds_put_format(string, "n_bytes=%"PRIu64", ", fs->byte_count);
1073     if (fs->idle_timeout != OFP_FLOW_PERMANENT) {
1074         ds_put_format(string, "idle_timeout=%"PRIu16", ", fs->idle_timeout);
1075     }
1076     if (fs->hard_timeout != OFP_FLOW_PERMANENT) {
1077         ds_put_format(string, "hard_timeout=%"PRIu16", ", fs->hard_timeout);
1078     }
1079     if (fs->flags) {
1080         ofp_print_flow_flags(string, fs->flags);
1081     }
1082     if (fs->idle_age >= 0) {
1083         ds_put_format(string, "idle_age=%d, ", fs->idle_age);
1084     }
1085     if (fs->hard_age >= 0 && fs->hard_age != fs->duration_sec) {
1086         ds_put_format(string, "hard_age=%d, ", fs->hard_age);
1087     }
1088
1089     match_format(&fs->match, string, fs->priority);
1090     if (string->string[string->length - 1] != ' ') {
1091         ds_put_char(string, ' ');
1092     }
1093
1094     ofpacts_format(fs->ofpacts, fs->ofpacts_len, string);
1095 }
1096
1097 static void
1098 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1099 {
1100     struct ofpbuf ofpacts;
1101     struct ofpbuf b;
1102
1103     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1104     ofpbuf_init(&ofpacts, 64);
1105     for (;;) {
1106         struct ofputil_flow_stats fs;
1107         int retval;
1108
1109         retval = ofputil_decode_flow_stats_reply(&fs, &b, true, &ofpacts);
1110         if (retval) {
1111             if (retval != EOF) {
1112                 ds_put_cstr(string, " ***parse error***");
1113             }
1114             break;
1115         }
1116         ds_put_char(string, '\n');
1117         ofp_print_flow_stats(string, &fs);
1118      }
1119     ofpbuf_uninit(&ofpacts);
1120 }
1121
1122 static void
1123 ofp_print_aggregate_stats_reply(struct ds *string, const struct ofp_header *oh)
1124 {
1125     struct ofputil_aggregate_stats as;
1126     enum ofperr error;
1127
1128     error = ofputil_decode_aggregate_stats_reply(&as, oh);
1129     if (error) {
1130         ofp_print_error(string, error);
1131         return;
1132     }
1133
1134     ds_put_format(string, " packet_count=%"PRIu64, as.packet_count);
1135     ds_put_format(string, " byte_count=%"PRIu64, as.byte_count);
1136     ds_put_format(string, " flow_count=%"PRIu32, as.flow_count);
1137 }
1138
1139 static void
1140 print_port_stat(struct ds *string, const char *leader, uint64_t stat, int more)
1141 {
1142     ds_put_cstr(string, leader);
1143     if (stat != UINT64_MAX) {
1144         ds_put_format(string, "%"PRIu64, stat);
1145     } else {
1146         ds_put_char(string, '?');
1147     }
1148     if (more) {
1149         ds_put_cstr(string, ", ");
1150     } else {
1151         ds_put_cstr(string, "\n");
1152     }
1153 }
1154
1155 static void
1156 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1157 {
1158     uint16_t ofp10_port;
1159     enum ofperr error;
1160
1161     error = ofputil_decode_port_stats_request(oh, &ofp10_port);
1162     if (error) {
1163         ofp_print_error(string, error);
1164         return;
1165     }
1166
1167     ds_put_cstr(string, " port_no=");
1168     ofputil_format_port(ofp10_port, string);
1169 }
1170
1171 static void
1172 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1173                            int verbosity)
1174 {
1175     struct ofpbuf b;
1176
1177     ds_put_format(string, " %zu ports\n", ofputil_count_port_stats(oh));
1178     if (verbosity < 1) {
1179         return;
1180     }
1181
1182     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1183     for (;;) {
1184         struct ofputil_port_stats ps;
1185         int retval;
1186
1187         retval = ofputil_decode_port_stats(&ps, &b);
1188         if (retval) {
1189             if (retval != EOF) {
1190                 ds_put_cstr(string, " ***parse error***");
1191             }
1192             return;
1193         }
1194
1195         ds_put_cstr(string, "  port ");
1196         if (ps.port_no < 10) {
1197             ds_put_char(string, ' ');
1198         }
1199         ofputil_format_port(ps.port_no, string);
1200
1201         ds_put_cstr(string, ": rx ");
1202         print_port_stat(string, "pkts=", ps.stats.rx_packets, 1);
1203         print_port_stat(string, "bytes=", ps.stats.rx_bytes, 1);
1204         print_port_stat(string, "drop=", ps.stats.rx_dropped, 1);
1205         print_port_stat(string, "errs=", ps.stats.rx_errors, 1);
1206         print_port_stat(string, "frame=", ps.stats.rx_frame_errors, 1);
1207         print_port_stat(string, "over=", ps.stats.rx_over_errors, 1);
1208         print_port_stat(string, "crc=", ps.stats.rx_crc_errors, 0);
1209
1210         ds_put_cstr(string, "           tx ");
1211         print_port_stat(string, "pkts=", ps.stats.tx_packets, 1);
1212         print_port_stat(string, "bytes=", ps.stats.tx_bytes, 1);
1213         print_port_stat(string, "drop=", ps.stats.tx_dropped, 1);
1214         print_port_stat(string, "errs=", ps.stats.tx_errors, 1);
1215         print_port_stat(string, "coll=", ps.stats.collisions, 0);
1216     }
1217 }
1218
1219 static void
1220 ofp_print_one_ofpst_table_reply(struct ds *string, enum ofp_version ofp_version,
1221                                 const char *name, struct ofp12_table_stats *ts)
1222 {
1223     char name_[OFP_MAX_TABLE_NAME_LEN + 1];
1224
1225     /* ofp13_table_stats is different */
1226     if (ofp_version > OFP12_VERSION) {
1227         return;
1228     }
1229
1230     ovs_strlcpy(name_, name, sizeof name_);
1231
1232     ds_put_format(string, "  %d: %-8s: ", ts->table_id, name_);
1233     ds_put_format(string, "wild=0x%05"PRIx64", ", ntohll(ts->wildcards));
1234     ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1235     ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1236     ds_put_cstr(string, "               ");
1237     ds_put_format(string, "lookup=%"PRIu64", ", ntohll(ts->lookup_count));
1238     ds_put_format(string, "matched=%"PRIu64"\n", ntohll(ts->matched_count));
1239
1240     if (ofp_version < OFP11_VERSION) {
1241         return;
1242     }
1243
1244     ds_put_cstr(string, "               ");
1245     ds_put_format(string, "match=0x%08"PRIx64", ", ntohll(ts->match));
1246     ds_put_format(string, "instructions=0x%08"PRIx32", ",
1247                   ntohl(ts->instructions));
1248     ds_put_format(string, "config=0x%08"PRIx32"\n", ntohl(ts->config));
1249     ds_put_cstr(string, "               ");
1250     ds_put_format(string, "write_actions=0x%08"PRIx32", ",
1251                   ntohl(ts->write_actions));
1252     ds_put_format(string, "apply_actions=0x%08"PRIx32"\n",
1253                   ntohl(ts->apply_actions));
1254
1255     if (ofp_version < OFP12_VERSION) {
1256         return;
1257     }
1258
1259     ds_put_cstr(string, "               ");
1260     ds_put_format(string, "write_setfields=0x%016"PRIx64"\n",
1261                   ntohll(ts->write_setfields));
1262     ds_put_cstr(string, "               ");
1263     ds_put_format(string, "apply_setfields=0x%016"PRIx64"\n",
1264                   ntohll(ts->apply_setfields));
1265     ds_put_cstr(string, "               ");
1266     ds_put_format(string, "metadata_match=0x%016"PRIx64"\n",
1267                   ntohll(ts->metadata_match));
1268     ds_put_cstr(string, "               ");
1269     ds_put_format(string, "metadata_write=0x%016"PRIx64"\n",
1270                   ntohll(ts->metadata_write));
1271 }
1272
1273 static void
1274 ofp_print_ofpst_table_reply13(struct ds *string, const struct ofp_header *oh,
1275                               int verbosity)
1276 {
1277     struct ofp13_table_stats *ts;
1278     struct ofpbuf b;
1279     size_t n;
1280
1281     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1282     ofpraw_pull_assert(&b);
1283
1284     n = b.size / sizeof *ts;
1285     ds_put_format(string, " %zu tables\n", n);
1286     if (verbosity < 1) {
1287         return;
1288     }
1289
1290     for (;;) {
1291         ts = ofpbuf_try_pull(&b, sizeof *ts);
1292         if (!ts) {
1293             return;
1294         }
1295         ds_put_format(string,
1296                       "  %d: active=%"PRIu32", lookup=%"PRIu64  \
1297                       ", matched=%"PRIu64"\n",
1298                       ts->table_id, ntohl(ts->active_count),
1299                       ntohll(ts->lookup_count), ntohll(ts->matched_count));
1300     }
1301 }
1302
1303 static void
1304 ofp_print_ofpst_table_reply12(struct ds *string, const struct ofp_header *oh,
1305                               int verbosity)
1306 {
1307     struct ofp12_table_stats *ts;
1308     struct ofpbuf b;
1309     size_t n;
1310
1311     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1312     ofpraw_pull_assert(&b);
1313
1314     n = b.size / sizeof *ts;
1315     ds_put_format(string, " %zu tables\n", n);
1316     if (verbosity < 1) {
1317         return;
1318     }
1319
1320     for (;;) {
1321         ts = ofpbuf_try_pull(&b, sizeof *ts);
1322         if (!ts) {
1323             return;
1324         }
1325
1326         ofp_print_one_ofpst_table_reply(string, OFP12_VERSION, ts->name, ts);
1327      }
1328 }
1329
1330 static void
1331 ofp_print_ofpst_table_reply11(struct ds *string, const struct ofp_header *oh,
1332                               int verbosity)
1333 {
1334     struct ofp11_table_stats *ts;
1335     struct ofpbuf b;
1336     size_t n;
1337
1338     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1339     ofpraw_pull_assert(&b);
1340
1341     n = b.size / sizeof *ts;
1342     ds_put_format(string, " %zu tables\n", n);
1343     if (verbosity < 1) {
1344         return;
1345     }
1346
1347     for (;;) {
1348         struct ofp12_table_stats ts12;
1349
1350         ts = ofpbuf_try_pull(&b, sizeof *ts);
1351         if (!ts) {
1352             return;
1353         }
1354
1355         ts12.table_id = ts->table_id;
1356         ts12.wildcards = htonll(ntohl(ts->wildcards));
1357         ts12.max_entries = ts->max_entries;
1358         ts12.active_count = ts->active_count;
1359         ts12.lookup_count = ts->lookup_count;
1360         ts12.matched_count = ts->matched_count;
1361         ts12.match = htonll(ntohl(ts->match));
1362         ts12.instructions = ts->instructions;
1363         ts12.config = ts->config;
1364         ts12.write_actions = ts->write_actions;
1365         ts12.apply_actions = ts->apply_actions;
1366         ofp_print_one_ofpst_table_reply(string, OFP11_VERSION, ts->name, &ts12);
1367      }
1368 }
1369
1370 static void
1371 ofp_print_ofpst_table_reply10(struct ds *string, const struct ofp_header *oh,
1372                               int verbosity)
1373 {
1374     struct ofp10_table_stats *ts;
1375     struct ofpbuf b;
1376     size_t n;
1377
1378     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1379     ofpraw_pull_assert(&b);
1380
1381     n = b.size / sizeof *ts;
1382     ds_put_format(string, " %zu tables\n", n);
1383     if (verbosity < 1) {
1384         return;
1385     }
1386
1387     for (;;) {
1388         struct ofp12_table_stats ts12;
1389
1390         ts = ofpbuf_try_pull(&b, sizeof *ts);
1391         if (!ts) {
1392             return;
1393         }
1394
1395         ts12.table_id = ts->table_id;
1396         ts12.wildcards = htonll(ntohl(ts->wildcards));
1397         ts12.max_entries = ts->max_entries;
1398         ts12.active_count = ts->active_count;
1399         ts12.lookup_count = get_32aligned_be64(&ts->lookup_count);
1400         ts12.matched_count = get_32aligned_be64(&ts->matched_count);
1401         ofp_print_one_ofpst_table_reply(string, OFP10_VERSION, ts->name, &ts12);
1402      }
1403 }
1404
1405 static void
1406 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1407                             int verbosity)
1408 {
1409     switch ((enum ofp_version)oh->version) {
1410     case OFP13_VERSION:
1411         ofp_print_ofpst_table_reply13(string, oh, verbosity);
1412         break;
1413
1414     case OFP12_VERSION:
1415         ofp_print_ofpst_table_reply12(string, oh, verbosity);
1416         break;
1417
1418     case OFP11_VERSION:
1419         ofp_print_ofpst_table_reply11(string, oh, verbosity);
1420         break;
1421
1422     case OFP10_VERSION:
1423         ofp_print_ofpst_table_reply10(string, oh, verbosity);
1424         break;
1425
1426     default:
1427         NOT_REACHED();
1428     }
1429 }
1430
1431 static void
1432 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1433 {
1434     if (queue_id == OFPQ_ALL) {
1435         ds_put_cstr(string, "ALL");
1436     } else {
1437         ds_put_format(string, "%"PRIu32, queue_id);
1438     }
1439 }
1440
1441 static void
1442 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1443 {
1444     struct ofputil_queue_stats_request oqsr;
1445     enum ofperr error;
1446
1447     error = ofputil_decode_queue_stats_request(oh, &oqsr);
1448     if (error) {
1449         ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1450         return;
1451     }
1452
1453     ds_put_cstr(string, "port=");
1454     ofputil_format_port(oqsr.port_no, string);
1455
1456     ds_put_cstr(string, " queue=");
1457     ofp_print_queue_name(string, oqsr.queue_id);
1458 }
1459
1460 static void
1461 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1462                             int verbosity)
1463 {
1464     struct ofpbuf b;
1465
1466     ds_put_format(string, " %zu queues\n", ofputil_count_queue_stats(oh));
1467     if (verbosity < 1) {
1468         return;
1469     }
1470
1471     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1472     for (;;) {
1473         struct ofputil_queue_stats qs;
1474         int retval;
1475
1476         retval = ofputil_decode_queue_stats(&qs, &b);
1477         if (retval) {
1478             if (retval != EOF) {
1479                 ds_put_cstr(string, " ***parse error***");
1480             }
1481             return;
1482         }
1483
1484         ds_put_cstr(string, "  port ");
1485         ofputil_format_port(qs.port_no, string);
1486         ds_put_cstr(string, " queue ");
1487         ofp_print_queue_name(string, qs.queue_id);
1488         ds_put_cstr(string, ": ");
1489
1490         print_port_stat(string, "bytes=", qs.stats.tx_bytes, 1);
1491         print_port_stat(string, "pkts=", qs.stats.tx_packets, 1);
1492         print_port_stat(string, "errors=", qs.stats.tx_errors, 0);
1493     }
1494 }
1495
1496 static void
1497 ofp_print_ofpst_port_desc_reply(struct ds *string,
1498                                 const struct ofp_header *oh)
1499 {
1500     struct ofpbuf b;
1501
1502     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1503     ofpraw_pull_assert(&b);
1504     ds_put_char(string, '\n');
1505     ofp_print_phy_ports(string, oh->version, &b);
1506 }
1507
1508 static void
1509 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1510 {
1511     uint16_t flags = ofpmp_flags(oh);
1512
1513     if (flags) {
1514         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
1515     }
1516 }
1517
1518 static void
1519 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1520 {
1521     uint16_t flags = ofpmp_flags(oh);
1522
1523     if (flags) {
1524         ds_put_cstr(string, " flags=");
1525         if (flags & OFPSF_REPLY_MORE) {
1526             ds_put_cstr(string, "[more]");
1527             flags &= ~OFPSF_REPLY_MORE;
1528         }
1529         if (flags) {
1530             ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1531                           flags);
1532         }
1533     }
1534 }
1535
1536 static void
1537 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1538 {
1539     size_t len = ntohs(oh->length);
1540
1541     ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1542     if (verbosity > 1) {
1543         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1544     }
1545 }
1546
1547 static void
1548 ofp_print_role_message(struct ds *string, const struct ofp_header *oh)
1549 {
1550     struct ofputil_role_request rr;
1551     enum ofperr error;
1552
1553     error = ofputil_decode_role_message(oh, &rr);
1554     if (error) {
1555         ofp_print_error(string, error);
1556         return;
1557     }
1558
1559     ds_put_cstr(string, " role=");
1560
1561     switch (rr.role) {
1562     case OFPCR12_ROLE_NOCHANGE:
1563         ds_put_cstr(string, "nochange");
1564         break;
1565     case OFPCR12_ROLE_EQUAL:
1566         ds_put_cstr(string, "equal"); /* OF 1.2 wording */
1567         break;
1568     case OFPCR12_ROLE_MASTER:
1569         ds_put_cstr(string, "master");
1570         break;
1571     case OFPCR12_ROLE_SLAVE:
1572         ds_put_cstr(string, "slave");
1573         break;
1574     default:
1575         NOT_REACHED();
1576     }
1577
1578     if (rr.have_generation_id) {
1579         ds_put_format(string, " generation_id=%"PRIu64, rr.generation_id);
1580     }
1581 }
1582
1583 static void
1584 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1585                                 const struct nx_flow_mod_table_id *nfmti)
1586 {
1587     ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1588 }
1589
1590 static void
1591 ofp_print_nxt_set_flow_format(struct ds *string,
1592                               const struct nx_set_flow_format *nsff)
1593 {
1594     uint32_t format = ntohl(nsff->format);
1595
1596     ds_put_cstr(string, " format=");
1597     if (ofputil_nx_flow_format_is_valid(format)) {
1598         ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
1599     } else {
1600         ds_put_format(string, "%"PRIu32, format);
1601     }
1602 }
1603
1604 static void
1605 ofp_print_nxt_set_packet_in_format(struct ds *string,
1606                                    const struct nx_set_packet_in_format *nspf)
1607 {
1608     uint32_t format = ntohl(nspf->format);
1609
1610     ds_put_cstr(string, " format=");
1611     if (ofputil_packet_in_format_is_valid(format)) {
1612         ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1613     } else {
1614         ds_put_format(string, "%"PRIu32, format);
1615     }
1616 }
1617
1618 static const char *
1619 ofp_port_reason_to_string(enum ofp_port_reason reason)
1620 {
1621     static char s[32];
1622
1623     switch (reason) {
1624     case OFPPR_ADD:
1625         return "add";
1626
1627     case OFPPR_DELETE:
1628         return "delete";
1629
1630     case OFPPR_MODIFY:
1631         return "modify";
1632
1633     default:
1634         sprintf(s, "%d", (int) reason);
1635         return s;
1636     }
1637 }
1638
1639 static void
1640 ofp_print_nxt_set_async_config(struct ds *string,
1641                                const struct nx_async_config *nac)
1642 {
1643     int i;
1644
1645     for (i = 0; i < 2; i++) {
1646         int j;
1647
1648         ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1649
1650         ds_put_cstr(string, "       PACKET_IN:");
1651         for (j = 0; j < 32; j++) {
1652             if (nac->packet_in_mask[i] & htonl(1u << j)) {
1653                 ds_put_format(string, " %s",
1654                               ofputil_packet_in_reason_to_string(j));
1655             }
1656         }
1657         if (!nac->packet_in_mask[i]) {
1658             ds_put_cstr(string, " (off)");
1659         }
1660         ds_put_char(string, '\n');
1661
1662         ds_put_cstr(string, "     PORT_STATUS:");
1663         for (j = 0; j < 32; j++) {
1664             if (nac->port_status_mask[i] & htonl(1u << j)) {
1665                 ds_put_format(string, " %s", ofp_port_reason_to_string(j));
1666             }
1667         }
1668         if (!nac->port_status_mask[i]) {
1669             ds_put_cstr(string, " (off)");
1670         }
1671         ds_put_char(string, '\n');
1672
1673         ds_put_cstr(string, "    FLOW_REMOVED:");
1674         for (j = 0; j < 32; j++) {
1675             if (nac->flow_removed_mask[i] & htonl(1u << j)) {
1676                 ds_put_format(string, " %s",
1677                               ofp_flow_removed_reason_to_string(j));
1678             }
1679         }
1680         if (!nac->flow_removed_mask[i]) {
1681             ds_put_cstr(string, " (off)");
1682         }
1683         ds_put_char(string, '\n');
1684     }
1685 }
1686
1687 static void
1688 ofp_print_nxt_set_controller_id(struct ds *string,
1689                                 const struct nx_controller_id *nci)
1690 {
1691     ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
1692 }
1693
1694 static void
1695 ofp_print_nxt_flow_monitor_cancel(struct ds *string,
1696                                   const struct ofp_header *oh)
1697 {
1698     ds_put_format(string, " id=%"PRIu32,
1699                   ofputil_decode_flow_monitor_cancel(oh));
1700 }
1701
1702 static const char *
1703 nx_flow_monitor_flags_to_name(uint32_t bit)
1704 {
1705     enum nx_flow_monitor_flags fmf = bit;
1706
1707     switch (fmf) {
1708     case NXFMF_INITIAL: return "initial";
1709     case NXFMF_ADD: return "add";
1710     case NXFMF_DELETE: return "delete";
1711     case NXFMF_MODIFY: return "modify";
1712     case NXFMF_ACTIONS: return "actions";
1713     case NXFMF_OWN: return "own";
1714     }
1715
1716     return NULL;
1717 }
1718
1719 static void
1720 ofp_print_nxst_flow_monitor_request(struct ds *string,
1721                                     const struct ofp_header *oh)
1722 {
1723     struct ofpbuf b;
1724
1725     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1726     for (;;) {
1727         struct ofputil_flow_monitor_request request;
1728         int retval;
1729
1730         retval = ofputil_decode_flow_monitor_request(&request, &b);
1731         if (retval) {
1732             if (retval != EOF) {
1733                 ofp_print_error(string, retval);
1734             }
1735             return;
1736         }
1737
1738         ds_put_format(string, "\n id=%"PRIu32" flags=", request.id);
1739         ofp_print_bit_names(string, request.flags,
1740                             nx_flow_monitor_flags_to_name, ',');
1741
1742         if (request.out_port != OFPP_NONE) {
1743             ds_put_cstr(string, " out_port=");
1744             ofputil_format_port(request.out_port, string);
1745         }
1746
1747         if (request.table_id != 0xff) {
1748             ds_put_format(string, " table=%"PRIu8, request.table_id);
1749         }
1750
1751         ds_put_char(string, ' ');
1752         match_format(&request.match, string, OFP_DEFAULT_PRIORITY);
1753         ds_chomp(string, ' ');
1754     }
1755 }
1756
1757 static void
1758 ofp_print_nxst_flow_monitor_reply(struct ds *string,
1759                                   const struct ofp_header *oh)
1760 {
1761     uint64_t ofpacts_stub[1024 / 8];
1762     struct ofpbuf ofpacts;
1763     struct ofpbuf b;
1764
1765     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1766     ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1767     for (;;) {
1768         struct ofputil_flow_update update;
1769         struct match match;
1770         int retval;
1771
1772         update.match = &match;
1773         retval = ofputil_decode_flow_update(&update, &b, &ofpacts);
1774         if (retval) {
1775             if (retval != EOF) {
1776                 ofp_print_error(string, retval);
1777             }
1778             ofpbuf_uninit(&ofpacts);
1779             return;
1780         }
1781
1782         ds_put_cstr(string, "\n event=");
1783         switch (update.event) {
1784         case NXFME_ADDED:
1785             ds_put_cstr(string, "ADDED");
1786             break;
1787
1788         case NXFME_DELETED:
1789             ds_put_format(string, "DELETED reason=%s",
1790                           ofp_flow_removed_reason_to_string(update.reason));
1791             break;
1792
1793         case NXFME_MODIFIED:
1794             ds_put_cstr(string, "MODIFIED");
1795             break;
1796
1797         case NXFME_ABBREV:
1798             ds_put_format(string, "ABBREV xid=0x%"PRIx32, ntohl(update.xid));
1799             continue;
1800         }
1801
1802         ds_put_format(string, " table=%"PRIu8, update.table_id);
1803         if (update.idle_timeout != OFP_FLOW_PERMANENT) {
1804             ds_put_format(string, " idle_timeout=%"PRIu16,
1805                           update.idle_timeout);
1806         }
1807         if (update.hard_timeout != OFP_FLOW_PERMANENT) {
1808             ds_put_format(string, " hard_timeout=%"PRIu16,
1809                           update.hard_timeout);
1810         }
1811         ds_put_format(string, " cookie=%#"PRIx64, ntohll(update.cookie));
1812
1813         ds_put_char(string, ' ');
1814         match_format(update.match, string, OFP_DEFAULT_PRIORITY);
1815
1816         if (update.ofpacts_len) {
1817             if (string->string[string->length - 1] != ' ') {
1818                 ds_put_char(string, ' ');
1819             }
1820             ofpacts_format(update.ofpacts, update.ofpacts_len, string);
1821         }
1822     }
1823 }
1824
1825 void
1826 ofp_print_version(const struct ofp_header *oh,
1827                   struct ds *string)
1828 {
1829     switch (oh->version) {
1830     case OFP10_VERSION:
1831         break;
1832     case OFP11_VERSION:
1833         ds_put_cstr(string, " (OF1.1)");
1834         break;
1835     case OFP12_VERSION:
1836         ds_put_cstr(string, " (OF1.2)");
1837         break;
1838     case OFP13_VERSION:
1839         ds_put_cstr(string, " (OF1.3)");
1840         break;
1841     default:
1842         ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
1843         break;
1844     }
1845     ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
1846 }
1847
1848 static void
1849 ofp_header_to_string__(const struct ofp_header *oh, enum ofpraw raw,
1850                        struct ds *string)
1851 {
1852     ds_put_cstr(string, ofpraw_get_name(raw));
1853     ofp_print_version(oh, string);
1854 }
1855
1856 static void
1857 ofp_print_not_implemented(struct ds *string)
1858 {
1859     ds_put_cstr(string, "NOT IMPLEMENTED YET!\n");
1860 }
1861
1862 static void
1863 ofp_to_string__(const struct ofp_header *oh, enum ofpraw raw,
1864                 struct ds *string, int verbosity)
1865 {
1866     const void *msg = oh;
1867
1868     ofp_header_to_string__(oh, raw, string);
1869     switch (ofptype_from_ofpraw(raw)) {
1870
1871         /* FIXME: Change the following once they are implemented: */
1872     case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
1873     case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
1874     case OFPTYPE_GET_ASYNC_REQUEST:
1875     case OFPTYPE_GET_ASYNC_REPLY:
1876     case OFPTYPE_METER_MOD:
1877     case OFPTYPE_GROUP_REQUEST:
1878     case OFPTYPE_GROUP_REPLY:
1879     case OFPTYPE_GROUP_DESC_REQUEST:
1880     case OFPTYPE_GROUP_DESC_REPLY:
1881     case OFPTYPE_GROUP_FEATURES_REQUEST:
1882     case OFPTYPE_GROUP_FEATURES_REPLY:
1883     case OFPTYPE_METER_REQUEST:
1884     case OFPTYPE_METER_REPLY:
1885     case OFPTYPE_METER_CONFIG_REQUEST:
1886     case OFPTYPE_METER_CONFIG_REPLY:
1887     case OFPTYPE_METER_FEATURES_REQUEST:
1888     case OFPTYPE_METER_FEATURES_REPLY:
1889     case OFPTYPE_TABLE_FEATURES_REQUEST:
1890     case OFPTYPE_TABLE_FEATURES_REPLY:
1891         ofp_print_not_implemented(string);
1892         break;
1893
1894     case OFPTYPE_HELLO:
1895         ofp_print_hello(string, oh);
1896         break;
1897
1898     case OFPTYPE_ERROR:
1899         ofp_print_error_msg(string, oh);
1900         break;
1901
1902     case OFPTYPE_ECHO_REQUEST:
1903     case OFPTYPE_ECHO_REPLY:
1904         ofp_print_echo(string, oh, verbosity);
1905         break;
1906
1907     case OFPTYPE_FEATURES_REQUEST:
1908         break;
1909
1910     case OFPTYPE_FEATURES_REPLY:
1911         ofp_print_switch_features(string, oh);
1912         break;
1913
1914     case OFPTYPE_GET_CONFIG_REQUEST:
1915         break;
1916
1917     case OFPTYPE_GET_CONFIG_REPLY:
1918     case OFPTYPE_SET_CONFIG:
1919         ofp_print_switch_config(string, ofpmsg_body(oh));
1920         break;
1921
1922     case OFPTYPE_PACKET_IN:
1923         ofp_print_packet_in(string, oh, verbosity);
1924         break;
1925
1926     case OFPTYPE_FLOW_REMOVED:
1927         ofp_print_flow_removed(string, oh);
1928         break;
1929
1930     case OFPTYPE_PORT_STATUS:
1931         ofp_print_port_status(string, oh);
1932         break;
1933
1934     case OFPTYPE_PACKET_OUT:
1935         ofp_print_packet_out(string, oh, verbosity);
1936         break;
1937
1938     case OFPTYPE_FLOW_MOD:
1939         ofp_print_flow_mod(string, oh, verbosity);
1940         break;
1941
1942     case OFPTYPE_PORT_MOD:
1943         ofp_print_port_mod(string, oh);
1944         break;
1945
1946     case OFPTYPE_BARRIER_REQUEST:
1947     case OFPTYPE_BARRIER_REPLY:
1948         break;
1949
1950     case OFPTYPE_ROLE_REQUEST:
1951     case OFPTYPE_ROLE_REPLY:
1952         ofp_print_role_message(string, oh);
1953         break;
1954
1955     case OFPTYPE_DESC_STATS_REQUEST:
1956     case OFPTYPE_PORT_DESC_STATS_REQUEST:
1957         ofp_print_stats_request(string, oh);
1958         break;
1959
1960     case OFPTYPE_FLOW_STATS_REQUEST:
1961     case OFPTYPE_AGGREGATE_STATS_REQUEST:
1962         ofp_print_stats_request(string, oh);
1963         ofp_print_flow_stats_request(string, oh);
1964         break;
1965
1966     case OFPTYPE_TABLE_STATS_REQUEST:
1967         ofp_print_stats_request(string, oh);
1968         break;
1969
1970     case OFPTYPE_PORT_STATS_REQUEST:
1971         ofp_print_stats_request(string, oh);
1972         ofp_print_ofpst_port_request(string, oh);
1973         break;
1974
1975     case OFPTYPE_QUEUE_STATS_REQUEST:
1976         ofp_print_stats_request(string, oh);
1977         ofp_print_ofpst_queue_request(string, oh);
1978         break;
1979
1980     case OFPTYPE_DESC_STATS_REPLY:
1981         ofp_print_stats_reply(string, oh);
1982         ofp_print_ofpst_desc_reply(string, oh);
1983         break;
1984
1985     case OFPTYPE_FLOW_STATS_REPLY:
1986         ofp_print_stats_reply(string, oh);
1987         ofp_print_flow_stats_reply(string, oh);
1988         break;
1989
1990     case OFPTYPE_QUEUE_STATS_REPLY:
1991         ofp_print_stats_reply(string, oh);
1992         ofp_print_ofpst_queue_reply(string, oh, verbosity);
1993         break;
1994
1995     case OFPTYPE_PORT_STATS_REPLY:
1996         ofp_print_stats_reply(string, oh);
1997         ofp_print_ofpst_port_reply(string, oh, verbosity);
1998         break;
1999
2000     case OFPTYPE_TABLE_STATS_REPLY:
2001         ofp_print_stats_reply(string, oh);
2002         ofp_print_ofpst_table_reply(string, oh, verbosity);
2003         break;
2004
2005     case OFPTYPE_AGGREGATE_STATS_REPLY:
2006         ofp_print_stats_reply(string, oh);
2007         ofp_print_aggregate_stats_reply(string, oh);
2008         break;
2009
2010     case OFPTYPE_PORT_DESC_STATS_REPLY:
2011         ofp_print_stats_reply(string, oh);
2012         ofp_print_ofpst_port_desc_reply(string, oh);
2013         break;
2014
2015     case OFPTYPE_FLOW_MOD_TABLE_ID:
2016         ofp_print_nxt_flow_mod_table_id(string, ofpmsg_body(oh));
2017         break;
2018
2019     case OFPTYPE_SET_FLOW_FORMAT:
2020         ofp_print_nxt_set_flow_format(string, ofpmsg_body(oh));
2021         break;
2022
2023     case OFPTYPE_SET_PACKET_IN_FORMAT:
2024         ofp_print_nxt_set_packet_in_format(string, ofpmsg_body(oh));
2025         break;
2026
2027     case OFPTYPE_FLOW_AGE:
2028         break;
2029
2030     case OFPTYPE_SET_CONTROLLER_ID:
2031         ofp_print_nxt_set_controller_id(string, ofpmsg_body(oh));
2032         break;
2033
2034     case OFPTYPE_SET_ASYNC_CONFIG:
2035         ofp_print_nxt_set_async_config(string, ofpmsg_body(oh));
2036         break;
2037
2038     case OFPTYPE_FLOW_MONITOR_CANCEL:
2039         ofp_print_nxt_flow_monitor_cancel(string, msg);
2040         break;
2041
2042     case OFPTYPE_FLOW_MONITOR_PAUSED:
2043     case OFPTYPE_FLOW_MONITOR_RESUMED:
2044         break;
2045
2046     case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
2047         ofp_print_nxst_flow_monitor_request(string, msg);
2048         break;
2049
2050     case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
2051         ofp_print_nxst_flow_monitor_reply(string, msg);
2052         break;
2053     }
2054 }
2055
2056 /* Composes and returns a string representing the OpenFlow packet of 'len'
2057  * bytes at 'oh' at the given 'verbosity' level.  0 is a minimal amount of
2058  * verbosity and higher numbers increase verbosity.  The caller is responsible
2059  * for freeing the string. */
2060 char *
2061 ofp_to_string(const void *oh_, size_t len, int verbosity)
2062 {
2063     struct ds string = DS_EMPTY_INITIALIZER;
2064     const struct ofp_header *oh = oh_;
2065
2066     if (!len) {
2067         ds_put_cstr(&string, "OpenFlow message is empty\n");
2068     } else if (len < sizeof(struct ofp_header)) {
2069         ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
2070                       len);
2071     } else if (ntohs(oh->length) > len) {
2072         enum ofperr error;
2073         enum ofpraw raw;
2074
2075         error = ofpraw_decode_partial(&raw, oh, len);
2076         if (!error) {
2077             ofp_header_to_string__(oh, raw, &string);
2078             ds_put_char(&string, '\n');
2079         }
2080
2081         ds_put_format(&string,
2082                       "(***truncated to %zu bytes from %"PRIu16"***)\n",
2083                       len, ntohs(oh->length));
2084     } else if (ntohs(oh->length) < len) {
2085         ds_put_format(&string,
2086                       "(***only uses %"PRIu16" bytes out of %zu***)\n",
2087                       ntohs(oh->length), len);
2088     } else {
2089         enum ofperr error;
2090         enum ofpraw raw;
2091
2092         error = ofpraw_decode(&raw, oh);
2093         if (!error) {
2094             ofp_to_string__(oh, raw, &string, verbosity);
2095             if (verbosity >= 5) {
2096                 if (ds_last(&string) != '\n') {
2097                     ds_put_char(&string, '\n');
2098                 }
2099                 ds_put_hex_dump(&string, oh, len, 0, true);
2100             }
2101             if (ds_last(&string) != '\n') {
2102                 ds_put_char(&string, '\n');
2103             }
2104             return ds_steal_cstr(&string);
2105         }
2106
2107         ofp_print_error(&string, error);
2108     }
2109     ds_put_hex_dump(&string, oh, len, 0, true);
2110     return ds_steal_cstr(&string);
2111 }
2112 \f
2113 static void
2114 print_and_free(FILE *stream, char *string)
2115 {
2116     fputs(string, stream);
2117     free(string);
2118 }
2119
2120 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
2121  * given 'verbosity' level.  0 is a minimal amount of verbosity and higher
2122  * numbers increase verbosity. */
2123 void
2124 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
2125 {
2126     print_and_free(stream, ofp_to_string(oh, len, verbosity));
2127 }
2128
2129 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
2130  * 'data' to 'stream'. */
2131 void
2132 ofp_print_packet(FILE *stream, const void *data, size_t len)
2133 {
2134     print_and_free(stream, ofp_packet_to_string(data, len));
2135 }