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