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