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