ofp-print.c: Fix remaining port number prints.
[sliver-openvswitch.git] / lib / ofp-print.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 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 {
646             skip_type = false;
647         }
648     }
649     print_wild_port(&f, "in_port=", w & OFPFW10_IN_PORT, verbosity,
650                     ntohs(om->in_port));
651     print_wild(&f, "dl_vlan=", w & OFPFW10_DL_VLAN, verbosity,
652                "%d", ntohs(om->dl_vlan));
653     print_wild(&f, "dl_vlan_pcp=", w & OFPFW10_DL_VLAN_PCP, verbosity,
654                "%d", om->dl_vlan_pcp);
655     print_wild(&f, "dl_src=", w & OFPFW10_DL_SRC, verbosity,
656                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
657     print_wild(&f, "dl_dst=", w & OFPFW10_DL_DST, verbosity,
658                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
659     if (!skip_type) {
660         print_wild(&f, "dl_type=", w & OFPFW10_DL_TYPE, verbosity,
661                    "0x%04x", ntohs(om->dl_type));
662     }
663     print_ip_netmask(&f, "nw_src=", om->nw_src,
664                      (w & OFPFW10_NW_SRC_MASK) >> OFPFW10_NW_SRC_SHIFT,
665                      verbosity);
666     print_ip_netmask(&f, "nw_dst=", om->nw_dst,
667                      (w & OFPFW10_NW_DST_MASK) >> OFPFW10_NW_DST_SHIFT,
668                      verbosity);
669     if (!skip_proto) {
670         if (om->dl_type == htons(ETH_TYPE_ARP) ||
671             om->dl_type == htons(ETH_TYPE_RARP)) {
672             print_wild(&f, "arp_op=", w & OFPFW10_NW_PROTO, verbosity,
673                        "%u", om->nw_proto);
674         } else {
675             print_wild(&f, "nw_proto=", w & OFPFW10_NW_PROTO, verbosity,
676                        "%u", om->nw_proto);
677         }
678     }
679     print_wild(&f, "nw_tos=", w & OFPFW10_NW_TOS, verbosity,
680                "%u", om->nw_tos);
681     if (om->nw_proto == IPPROTO_ICMP) {
682         print_wild(&f, "icmp_type=", w & OFPFW10_ICMP_TYPE, verbosity,
683                    "%d", ntohs(om->tp_src));
684         print_wild(&f, "icmp_code=", w & OFPFW10_ICMP_CODE, verbosity,
685                    "%d", ntohs(om->tp_dst));
686     } else {
687         print_wild(&f, "tp_src=", w & OFPFW10_TP_SRC, verbosity,
688                    "%d", ntohs(om->tp_src));
689         print_wild(&f, "tp_dst=", w & OFPFW10_TP_DST, verbosity,
690                    "%d", ntohs(om->tp_dst));
691     }
692     if (ds_last(&f) == ',') {
693         f.length--;
694     }
695     return ds_cstr(&f);
696 }
697
698 static void
699 ofp_print_flow_flags(struct ds *s, uint16_t flags)
700 {
701     if (flags & OFPFF_SEND_FLOW_REM) {
702         ds_put_cstr(s, "send_flow_rem ");
703     }
704     if (flags & OFPFF_CHECK_OVERLAP) {
705         ds_put_cstr(s, "check_overlap ");
706     }
707     if (flags & OFPFF12_RESET_COUNTS) {
708         ds_put_cstr(s, "reset_counts ");
709     }
710     if (flags & OFPFF13_NO_PKT_COUNTS) {
711         ds_put_cstr(s, "no_packet_counts ");
712     }
713     if (flags & OFPFF13_NO_BYT_COUNTS) {
714         ds_put_cstr(s, "no_byte_counts ");
715     }
716
717     flags &= ~(OFPFF_SEND_FLOW_REM | OFPFF_CHECK_OVERLAP
718                | OFPFF12_RESET_COUNTS
719                | OFPFF13_NO_PKT_COUNTS | OFPFF13_NO_BYT_COUNTS);
720     if (flags) {
721         ds_put_format(s, "flags:0x%"PRIx16" ", flags);
722     }
723 }
724
725 static void
726 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh, int verbosity)
727 {
728     struct ofputil_flow_mod fm;
729     struct ofpbuf ofpacts;
730     bool need_priority;
731     enum ofperr error;
732     enum ofpraw raw;
733     enum ofputil_protocol protocol;
734
735     protocol = ofputil_protocol_from_ofp_version(oh->version);
736     protocol = ofputil_protocol_set_tid(protocol, true);
737
738     ofpbuf_init(&ofpacts, 64);
739     error = ofputil_decode_flow_mod(&fm, oh, protocol, &ofpacts);
740     if (error) {
741         ofpbuf_uninit(&ofpacts);
742         ofp_print_error(s, error);
743         return;
744     }
745
746     ds_put_char(s, ' ');
747     switch (fm.command) {
748     case OFPFC_ADD:
749         ds_put_cstr(s, "ADD");
750         break;
751     case OFPFC_MODIFY:
752         ds_put_cstr(s, "MOD");
753         break;
754     case OFPFC_MODIFY_STRICT:
755         ds_put_cstr(s, "MOD_STRICT");
756         break;
757     case OFPFC_DELETE:
758         ds_put_cstr(s, "DEL");
759         break;
760     case OFPFC_DELETE_STRICT:
761         ds_put_cstr(s, "DEL_STRICT");
762         break;
763     default:
764         ds_put_format(s, "cmd:%d", fm.command);
765     }
766     if (fm.table_id != 0) {
767         ds_put_format(s, " table:%d", fm.table_id);
768     }
769
770     ds_put_char(s, ' ');
771     ofpraw_decode(&raw, oh);
772     if (verbosity >= 3 && raw == OFPRAW_OFPT10_FLOW_MOD) {
773         const struct ofp10_flow_mod *ofm = ofpmsg_body(oh);
774         ofp10_match_print(s, &ofm->match, verbosity);
775
776         /* ofp_print_match() doesn't print priority. */
777         need_priority = true;
778     } else if (verbosity >= 3 && raw == OFPRAW_NXT_FLOW_MOD) {
779         const struct nx_flow_mod *nfm = ofpmsg_body(oh);
780         const void *nxm = nfm + 1;
781         char *nxm_s;
782
783         nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
784         ds_put_cstr(s, nxm_s);
785         free(nxm_s);
786
787         /* nx_match_to_string() doesn't print priority. */
788         need_priority = true;
789     } else {
790         match_format(&fm.match, s, fm.priority);
791
792         /* match_format() does print priority. */
793         need_priority = false;
794     }
795
796     if (ds_last(s) != ' ') {
797         ds_put_char(s, ' ');
798     }
799     if (fm.new_cookie != htonll(0) && fm.new_cookie != htonll(UINT64_MAX)) {
800         ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.new_cookie));
801     }
802     if (fm.cookie_mask != htonll(0)) {
803         ds_put_format(s, "cookie:0x%"PRIx64"/0x%"PRIx64" ",
804                 ntohll(fm.cookie), ntohll(fm.cookie_mask));
805     }
806     if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
807         ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
808     }
809     if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
810         ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
811     }
812     if (fm.priority != OFP_DEFAULT_PRIORITY && need_priority) {
813         ds_put_format(s, "pri:%"PRIu16" ", fm.priority);
814     }
815     if (fm.buffer_id != UINT32_MAX) {
816         ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
817     }
818     if (fm.out_port != OFPP_ANY) {
819         ds_put_format(s, "out_port:");
820         ofputil_format_port(fm.out_port, s);
821         ds_put_char(s, ' ');
822     }
823     if (fm.flags != 0) {
824         ofp_print_flow_flags(s, fm.flags);
825     }
826
827     ofpacts_format(fm.ofpacts, fm.ofpacts_len, s);
828     ofpbuf_uninit(&ofpacts);
829 }
830
831 static void
832 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
833 {
834     ds_put_format(string, "%u", sec);
835     if (nsec > 0) {
836         ds_put_format(string, ".%09u", nsec);
837         while (string->string[string->length - 1] == '0') {
838             string->length--;
839         }
840     }
841     ds_put_char(string, 's');
842 }
843
844 static const char *
845 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason)
846 {
847     static char s[32];
848
849     switch (reason) {
850     case OFPRR_IDLE_TIMEOUT:
851         return "idle";
852     case OFPRR_HARD_TIMEOUT:
853         return "hard";
854     case OFPRR_DELETE:
855         return "delete";
856     case OFPRR_GROUP_DELETE:
857         return "group_delete";
858     case OFPRR_EVICTION:
859         return "eviction";
860     default:
861         sprintf(s, "%d", (int) reason);
862         return s;
863     }
864 }
865
866 static void
867 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
868 {
869     struct ofputil_flow_removed fr;
870     enum ofperr error;
871
872     error = ofputil_decode_flow_removed(&fr, oh);
873     if (error) {
874         ofp_print_error(string, error);
875         return;
876     }
877
878     ds_put_char(string, ' ');
879     match_format(&fr.match, string, fr.priority);
880
881     ds_put_format(string, " reason=%s",
882                   ofp_flow_removed_reason_to_string(fr.reason));
883
884     if (fr.table_id != 255) {
885         ds_put_format(string, " table_id=%"PRIu8, fr.table_id);
886     }
887
888     if (fr.cookie != htonll(0)) {
889         ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
890     }
891     ds_put_cstr(string, " duration");
892     ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
893     ds_put_format(string, " idle%"PRIu16, fr.idle_timeout);
894     if (fr.hard_timeout) {
895         /* The hard timeout was only added in OF1.2, so only print it if it is
896          * actually in use to avoid gratuitous change to the formatting. */
897         ds_put_format(string, " hard%"PRIu16, fr.hard_timeout);
898     }
899     ds_put_format(string, " pkts%"PRIu64" bytes%"PRIu64"\n",
900                   fr.packet_count, fr.byte_count);
901 }
902
903 static void
904 ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
905 {
906     struct ofputil_port_mod pm;
907     enum ofperr error;
908
909     error = ofputil_decode_port_mod(oh, &pm);
910     if (error) {
911         ofp_print_error(string, error);
912         return;
913     }
914
915     ds_put_cstr(string, "port: ");
916     ofputil_format_port(pm.port_no, string);
917     ds_put_format(string, ": addr:"ETH_ADDR_FMT"\n",
918                   ETH_ADDR_ARGS(pm.hw_addr));
919
920     ds_put_cstr(string, "     config: ");
921     ofp_print_port_config(string, pm.config);
922
923     ds_put_cstr(string, "     mask:   ");
924     ofp_print_port_config(string, pm.mask);
925
926     ds_put_cstr(string, "     advertise: ");
927     if (pm.advertise) {
928         ofp_print_port_features(string, pm.advertise);
929     } else {
930         ds_put_cstr(string, "UNCHANGED\n");
931     }
932 }
933
934 static void
935 ofp_print_error(struct ds *string, enum ofperr error)
936 {
937     if (string->length) {
938         ds_put_char(string, ' ');
939     }
940     ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
941 }
942
943 static void
944 ofp_print_hello(struct ds *string, const struct ofp_header *oh)
945 {
946     uint32_t allowed_versions;
947     bool ok;
948
949     ok = ofputil_decode_hello(oh, &allowed_versions);
950
951     ds_put_cstr(string, "\n version bitmap: ");
952     ofputil_format_version_bitmap(string, allowed_versions);
953
954     if (!ok) {
955         ds_put_cstr(string, "\n unknown data in hello:\n");
956         ds_put_hex_dump(string, oh, ntohs(oh->length), 0, true);
957     }
958 }
959
960 static void
961 ofp_print_error_msg(struct ds *string, const struct ofp_header *oh)
962 {
963     size_t len = ntohs(oh->length);
964     struct ofpbuf payload;
965     enum ofperr error;
966     char *s;
967
968     error = ofperr_decode_msg(oh, &payload);
969     if (!error) {
970         ds_put_cstr(string, "***decode error***");
971         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
972         return;
973     }
974
975     ds_put_format(string, " %s\n", ofperr_get_name(error));
976
977     if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
978         ds_put_printable(string, payload.data, payload.size);
979     } else {
980         s = ofp_to_string(payload.data, payload.size, 1);
981         ds_put_cstr(string, s);
982         free(s);
983     }
984 }
985
986 static void
987 ofp_print_port_status(struct ds *string, const struct ofp_header *oh)
988 {
989     struct ofputil_port_status ps;
990     enum ofperr error;
991
992     error = ofputil_decode_port_status(oh, &ps);
993     if (error) {
994         ofp_print_error(string, error);
995         return;
996     }
997
998     if (ps.reason == OFPPR_ADD) {
999         ds_put_format(string, " ADD:");
1000     } else if (ps.reason == OFPPR_DELETE) {
1001         ds_put_format(string, " DEL:");
1002     } else if (ps.reason == OFPPR_MODIFY) {
1003         ds_put_format(string, " MOD:");
1004     }
1005
1006     ofp_print_phy_port(string, &ps.desc);
1007 }
1008
1009 static void
1010 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
1011 {
1012     const struct ofp_desc_stats *ods = ofpmsg_body(oh);
1013
1014     ds_put_char(string, '\n');
1015     ds_put_format(string, "Manufacturer: %.*s\n",
1016             (int) sizeof ods->mfr_desc, ods->mfr_desc);
1017     ds_put_format(string, "Hardware: %.*s\n",
1018             (int) sizeof ods->hw_desc, ods->hw_desc);
1019     ds_put_format(string, "Software: %.*s\n",
1020             (int) sizeof ods->sw_desc, ods->sw_desc);
1021     ds_put_format(string, "Serial Num: %.*s\n",
1022             (int) sizeof ods->serial_num, ods->serial_num);
1023     ds_put_format(string, "DP Description: %.*s\n",
1024             (int) sizeof ods->dp_desc, ods->dp_desc);
1025 }
1026
1027 static void
1028 ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
1029 {
1030     struct ofputil_flow_stats_request fsr;
1031     enum ofperr error;
1032
1033     error = ofputil_decode_flow_stats_request(&fsr, oh);
1034     if (error) {
1035         ofp_print_error(string, error);
1036         return;
1037     }
1038
1039     if (fsr.table_id != 0xff) {
1040         ds_put_format(string, " table=%"PRIu8, fsr.table_id);
1041     }
1042
1043     if (fsr.out_port != OFPP_ANY) {
1044         ds_put_cstr(string, " out_port=");
1045         ofputil_format_port(fsr.out_port, string);
1046     }
1047
1048     ds_put_char(string, ' ');
1049     match_format(&fsr.match, string, OFP_DEFAULT_PRIORITY);
1050 }
1051
1052 void
1053 ofp_print_flow_stats(struct ds *string, struct ofputil_flow_stats *fs)
1054 {
1055     ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1056                   ntohll(fs->cookie));
1057
1058     ofp_print_duration(string, fs->duration_sec, fs->duration_nsec);
1059     ds_put_format(string, ", table=%"PRIu8", ", fs->table_id);
1060     ds_put_format(string, "n_packets=%"PRIu64", ", fs->packet_count);
1061     ds_put_format(string, "n_bytes=%"PRIu64", ", fs->byte_count);
1062     if (fs->idle_timeout != OFP_FLOW_PERMANENT) {
1063         ds_put_format(string, "idle_timeout=%"PRIu16", ", fs->idle_timeout);
1064     }
1065     if (fs->hard_timeout != OFP_FLOW_PERMANENT) {
1066         ds_put_format(string, "hard_timeout=%"PRIu16", ", fs->hard_timeout);
1067     }
1068     if (fs->flags) {
1069         ofp_print_flow_flags(string, fs->flags);
1070     }
1071     if (fs->idle_age >= 0) {
1072         ds_put_format(string, "idle_age=%d, ", fs->idle_age);
1073     }
1074     if (fs->hard_age >= 0 && fs->hard_age != fs->duration_sec) {
1075         ds_put_format(string, "hard_age=%d, ", fs->hard_age);
1076     }
1077
1078     match_format(&fs->match, string, fs->priority);
1079     if (string->string[string->length - 1] != ' ') {
1080         ds_put_char(string, ' ');
1081     }
1082
1083     ofpacts_format(fs->ofpacts, fs->ofpacts_len, string);
1084 }
1085
1086 static void
1087 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1088 {
1089     struct ofpbuf ofpacts;
1090     struct ofpbuf b;
1091
1092     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1093     ofpbuf_init(&ofpacts, 64);
1094     for (;;) {
1095         struct ofputil_flow_stats fs;
1096         int retval;
1097
1098         retval = ofputil_decode_flow_stats_reply(&fs, &b, true, &ofpacts);
1099         if (retval) {
1100             if (retval != EOF) {
1101                 ds_put_cstr(string, " ***parse error***");
1102             }
1103             break;
1104         }
1105         ds_put_char(string, '\n');
1106         ofp_print_flow_stats(string, &fs);
1107      }
1108     ofpbuf_uninit(&ofpacts);
1109 }
1110
1111 static void
1112 ofp_print_aggregate_stats_reply(struct ds *string, const struct ofp_header *oh)
1113 {
1114     struct ofputil_aggregate_stats as;
1115     enum ofperr error;
1116
1117     error = ofputil_decode_aggregate_stats_reply(&as, oh);
1118     if (error) {
1119         ofp_print_error(string, error);
1120         return;
1121     }
1122
1123     ds_put_format(string, " packet_count=%"PRIu64, as.packet_count);
1124     ds_put_format(string, " byte_count=%"PRIu64, as.byte_count);
1125     ds_put_format(string, " flow_count=%"PRIu32, as.flow_count);
1126 }
1127
1128 static void
1129 print_port_stat(struct ds *string, const char *leader, uint64_t stat, int more)
1130 {
1131     ds_put_cstr(string, leader);
1132     if (stat != UINT64_MAX) {
1133         ds_put_format(string, "%"PRIu64, stat);
1134     } else {
1135         ds_put_char(string, '?');
1136     }
1137     if (more) {
1138         ds_put_cstr(string, ", ");
1139     } else {
1140         ds_put_cstr(string, "\n");
1141     }
1142 }
1143
1144 static void
1145 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1146 {
1147     uint16_t ofp10_port;
1148     enum ofperr error;
1149
1150     error = ofputil_decode_port_stats_request(oh, &ofp10_port);
1151     if (error) {
1152         ofp_print_error(string, error);
1153         return;
1154     }
1155
1156     ds_put_cstr(string, " port_no=");
1157     ofputil_format_port(ofp10_port, string);
1158 }
1159
1160 static void
1161 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1162                            int verbosity)
1163 {
1164     struct ofpbuf b;
1165
1166     ds_put_format(string, " %zu ports\n", ofputil_count_port_stats(oh));
1167     if (verbosity < 1) {
1168         return;
1169     }
1170
1171     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1172     for (;;) {
1173         struct ofputil_port_stats ps;
1174         int retval;
1175
1176         retval = ofputil_decode_port_stats(&ps, &b);
1177         if (retval) {
1178             if (retval != EOF) {
1179                 ds_put_cstr(string, " ***parse error***");
1180             }
1181             return;
1182         }
1183
1184         ds_put_cstr(string, "  port ");
1185         if (ps.port_no < 10) {
1186             ds_put_char(string, ' ');
1187         }
1188         ofputil_format_port(ps.port_no, string);
1189
1190         ds_put_cstr(string, ": rx ");
1191         print_port_stat(string, "pkts=", ps.stats.rx_packets, 1);
1192         print_port_stat(string, "bytes=", ps.stats.rx_bytes, 1);
1193         print_port_stat(string, "drop=", ps.stats.rx_dropped, 1);
1194         print_port_stat(string, "errs=", ps.stats.rx_errors, 1);
1195         print_port_stat(string, "frame=", ps.stats.rx_frame_errors, 1);
1196         print_port_stat(string, "over=", ps.stats.rx_over_errors, 1);
1197         print_port_stat(string, "crc=", ps.stats.rx_crc_errors, 0);
1198
1199         ds_put_cstr(string, "           tx ");
1200         print_port_stat(string, "pkts=", ps.stats.tx_packets, 1);
1201         print_port_stat(string, "bytes=", ps.stats.tx_bytes, 1);
1202         print_port_stat(string, "drop=", ps.stats.tx_dropped, 1);
1203         print_port_stat(string, "errs=", ps.stats.tx_errors, 1);
1204         print_port_stat(string, "coll=", ps.stats.collisions, 0);
1205     }
1206 }
1207
1208 static void
1209 ofp_print_one_ofpst_table_reply(struct ds *string, enum ofp_version ofp_version,
1210                                 const char *name, struct ofp12_table_stats *ts)
1211 {
1212     char name_[OFP_MAX_TABLE_NAME_LEN + 1];
1213
1214     /* ofp13_table_stats is different */
1215     if (ofp_version > OFP12_VERSION) {
1216         return;
1217     }
1218
1219     ovs_strlcpy(name_, name, sizeof name_);
1220
1221     ds_put_format(string, "  %d: %-8s: ", ts->table_id, name_);
1222     ds_put_format(string, "wild=0x%05"PRIx64", ", ntohll(ts->wildcards));
1223     ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1224     ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1225     ds_put_cstr(string, "               ");
1226     ds_put_format(string, "lookup=%"PRIu64", ", ntohll(ts->lookup_count));
1227     ds_put_format(string, "matched=%"PRIu64"\n", ntohll(ts->matched_count));
1228
1229     if (ofp_version < OFP11_VERSION) {
1230         return;
1231     }
1232
1233     ds_put_cstr(string, "               ");
1234     ds_put_format(string, "match=0x%08"PRIx64", ", ntohll(ts->match));
1235     ds_put_format(string, "instructions=0x%08"PRIx32", ",
1236                   ntohl(ts->instructions));
1237     ds_put_format(string, "config=0x%08"PRIx32"\n", ntohl(ts->config));
1238     ds_put_cstr(string, "               ");
1239     ds_put_format(string, "write_actions=0x%08"PRIx32", ",
1240                   ntohl(ts->write_actions));
1241     ds_put_format(string, "apply_actions=0x%08"PRIx32"\n",
1242                   ntohl(ts->apply_actions));
1243
1244     if (ofp_version < OFP12_VERSION) {
1245         return;
1246     }
1247
1248     ds_put_cstr(string, "               ");
1249     ds_put_format(string, "write_setfields=0x%016"PRIx64"\n",
1250                   ntohll(ts->write_setfields));
1251     ds_put_cstr(string, "               ");
1252     ds_put_format(string, "apply_setfields=0x%016"PRIx64"\n",
1253                   ntohll(ts->apply_setfields));
1254     ds_put_cstr(string, "               ");
1255     ds_put_format(string, "metadata_match=0x%016"PRIx64"\n",
1256                   ntohll(ts->metadata_match));
1257     ds_put_cstr(string, "               ");
1258     ds_put_format(string, "metadata_write=0x%016"PRIx64"\n",
1259                   ntohll(ts->metadata_write));
1260 }
1261
1262 static void
1263 ofp_print_ofpst_table_reply13(struct ds *string, const struct ofp_header *oh,
1264                               int verbosity)
1265 {
1266     struct ofp13_table_stats *ts;
1267     struct ofpbuf b;
1268     size_t n;
1269
1270     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1271     ofpraw_pull_assert(&b);
1272
1273     n = b.size / sizeof *ts;
1274     ds_put_format(string, " %zu tables\n", n);
1275     if (verbosity < 1) {
1276         return;
1277     }
1278
1279     for (;;) {
1280         ts = ofpbuf_try_pull(&b, sizeof *ts);
1281         if (!ts) {
1282             return;
1283         }
1284         ds_put_format(string,
1285                       "  %d: active=%"PRIu32", lookup=%"PRIu64  \
1286                       ", matched=%"PRIu64"\n",
1287                       ts->table_id, ntohl(ts->active_count),
1288                       ntohll(ts->lookup_count), ntohll(ts->matched_count));
1289     }
1290 }
1291
1292 static void
1293 ofp_print_ofpst_table_reply12(struct ds *string, const struct ofp_header *oh,
1294                               int verbosity)
1295 {
1296     struct ofp12_table_stats *ts;
1297     struct ofpbuf b;
1298     size_t n;
1299
1300     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1301     ofpraw_pull_assert(&b);
1302
1303     n = b.size / sizeof *ts;
1304     ds_put_format(string, " %zu tables\n", n);
1305     if (verbosity < 1) {
1306         return;
1307     }
1308
1309     for (;;) {
1310         ts = ofpbuf_try_pull(&b, sizeof *ts);
1311         if (!ts) {
1312             return;
1313         }
1314
1315         ofp_print_one_ofpst_table_reply(string, OFP12_VERSION, ts->name, ts);
1316      }
1317 }
1318
1319 static void
1320 ofp_print_ofpst_table_reply11(struct ds *string, const struct ofp_header *oh,
1321                               int verbosity)
1322 {
1323     struct ofp11_table_stats *ts;
1324     struct ofpbuf b;
1325     size_t n;
1326
1327     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1328     ofpraw_pull_assert(&b);
1329
1330     n = b.size / sizeof *ts;
1331     ds_put_format(string, " %zu tables\n", n);
1332     if (verbosity < 1) {
1333         return;
1334     }
1335
1336     for (;;) {
1337         struct ofp12_table_stats ts12;
1338
1339         ts = ofpbuf_try_pull(&b, sizeof *ts);
1340         if (!ts) {
1341             return;
1342         }
1343
1344         ts12.table_id = ts->table_id;
1345         ts12.wildcards = htonll(ntohl(ts->wildcards));
1346         ts12.max_entries = ts->max_entries;
1347         ts12.active_count = ts->active_count;
1348         ts12.lookup_count = ts->lookup_count;
1349         ts12.matched_count = ts->matched_count;
1350         ts12.match = htonll(ntohl(ts->match));
1351         ts12.instructions = ts->instructions;
1352         ts12.config = ts->config;
1353         ts12.write_actions = ts->write_actions;
1354         ts12.apply_actions = ts->apply_actions;
1355         ofp_print_one_ofpst_table_reply(string, OFP11_VERSION, ts->name, &ts12);
1356      }
1357 }
1358
1359 static void
1360 ofp_print_ofpst_table_reply10(struct ds *string, const struct ofp_header *oh,
1361                               int verbosity)
1362 {
1363     struct ofp10_table_stats *ts;
1364     struct ofpbuf b;
1365     size_t n;
1366
1367     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1368     ofpraw_pull_assert(&b);
1369
1370     n = b.size / sizeof *ts;
1371     ds_put_format(string, " %zu tables\n", n);
1372     if (verbosity < 1) {
1373         return;
1374     }
1375
1376     for (;;) {
1377         struct ofp12_table_stats ts12;
1378
1379         ts = ofpbuf_try_pull(&b, sizeof *ts);
1380         if (!ts) {
1381             return;
1382         }
1383
1384         ts12.table_id = ts->table_id;
1385         ts12.wildcards = htonll(ntohl(ts->wildcards));
1386         ts12.max_entries = ts->max_entries;
1387         ts12.active_count = ts->active_count;
1388         ts12.lookup_count = get_32aligned_be64(&ts->lookup_count);
1389         ts12.matched_count = get_32aligned_be64(&ts->matched_count);
1390         ofp_print_one_ofpst_table_reply(string, OFP10_VERSION, ts->name, &ts12);
1391      }
1392 }
1393
1394 static void
1395 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1396                             int verbosity)
1397 {
1398     switch ((enum ofp_version)oh->version) {
1399     case OFP13_VERSION:
1400         ofp_print_ofpst_table_reply13(string, oh, verbosity);
1401         break;
1402
1403     case OFP12_VERSION:
1404         ofp_print_ofpst_table_reply12(string, oh, verbosity);
1405         break;
1406
1407     case OFP11_VERSION:
1408         ofp_print_ofpst_table_reply11(string, oh, verbosity);
1409         break;
1410
1411     case OFP10_VERSION:
1412         ofp_print_ofpst_table_reply10(string, oh, verbosity);
1413         break;
1414
1415     default:
1416         NOT_REACHED();
1417     }
1418 }
1419
1420 static void
1421 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1422 {
1423     if (queue_id == OFPQ_ALL) {
1424         ds_put_cstr(string, "ALL");
1425     } else {
1426         ds_put_format(string, "%"PRIu32, queue_id);
1427     }
1428 }
1429
1430 static void
1431 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1432 {
1433     struct ofputil_queue_stats_request oqsr;
1434     enum ofperr error;
1435
1436     error = ofputil_decode_queue_stats_request(oh, &oqsr);
1437     if (error) {
1438         ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1439         return;
1440     }
1441
1442     ds_put_cstr(string, "port=");
1443     ofputil_format_port(oqsr.port_no, string);
1444
1445     ds_put_cstr(string, " queue=");
1446     ofp_print_queue_name(string, oqsr.queue_id);
1447 }
1448
1449 static void
1450 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1451                             int verbosity)
1452 {
1453     struct ofpbuf b;
1454
1455     ds_put_format(string, " %zu queues\n", ofputil_count_queue_stats(oh));
1456     if (verbosity < 1) {
1457         return;
1458     }
1459
1460     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1461     for (;;) {
1462         struct ofputil_queue_stats qs;
1463         int retval;
1464
1465         retval = ofputil_decode_queue_stats(&qs, &b);
1466         if (retval) {
1467             if (retval != EOF) {
1468                 ds_put_cstr(string, " ***parse error***");
1469             }
1470             return;
1471         }
1472
1473         ds_put_cstr(string, "  port ");
1474         ofputil_format_port(qs.port_no, string);
1475         ds_put_cstr(string, " queue ");
1476         ofp_print_queue_name(string, qs.queue_id);
1477         ds_put_cstr(string, ": ");
1478
1479         print_port_stat(string, "bytes=", qs.stats.tx_bytes, 1);
1480         print_port_stat(string, "pkts=", qs.stats.tx_packets, 1);
1481         print_port_stat(string, "errors=", qs.stats.tx_errors, 0);
1482     }
1483 }
1484
1485 static void
1486 ofp_print_ofpst_port_desc_reply(struct ds *string,
1487                                 const struct ofp_header *oh)
1488 {
1489     struct ofpbuf b;
1490
1491     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1492     ofpraw_pull_assert(&b);
1493     ds_put_char(string, '\n');
1494     ofp_print_phy_ports(string, oh->version, &b);
1495 }
1496
1497 static void
1498 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1499 {
1500     uint16_t flags = ofpmp_flags(oh);
1501
1502     if (flags) {
1503         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
1504     }
1505 }
1506
1507 static void
1508 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1509 {
1510     uint16_t flags = ofpmp_flags(oh);
1511
1512     if (flags) {
1513         ds_put_cstr(string, " flags=");
1514         if (flags & OFPSF_REPLY_MORE) {
1515             ds_put_cstr(string, "[more]");
1516             flags &= ~OFPSF_REPLY_MORE;
1517         }
1518         if (flags) {
1519             ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1520                           flags);
1521         }
1522     }
1523 }
1524
1525 static void
1526 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1527 {
1528     size_t len = ntohs(oh->length);
1529
1530     ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1531     if (verbosity > 1) {
1532         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1533     }
1534 }
1535
1536 static void
1537 ofp_print_role_message(struct ds *string, const struct ofp_header *oh)
1538 {
1539     struct ofputil_role_request rr;
1540     enum ofperr error;
1541
1542     error = ofputil_decode_role_message(oh, &rr);
1543     if (error) {
1544         ofp_print_error(string, error);
1545         return;
1546     }
1547
1548     ds_put_cstr(string, " role=");
1549     if (rr.request_current_role_only) {
1550         ds_put_cstr(string, "nochange");
1551         return;
1552     }
1553
1554     switch (rr.role) {
1555     case NX_ROLE_OTHER:
1556         ds_put_cstr(string, "equal"); /* OF 1.2 wording */
1557         break;
1558     case NX_ROLE_MASTER:
1559         ds_put_cstr(string, "master");
1560         break;
1561     case NX_ROLE_SLAVE:
1562         ds_put_cstr(string, "slave");
1563         break;
1564     default:
1565         NOT_REACHED();
1566     }
1567
1568     if (rr.have_generation_id) {
1569         ds_put_format(string, " generation_id=%"PRIu64, rr.generation_id);
1570     }
1571 }
1572
1573 static void
1574 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1575                                 const struct nx_flow_mod_table_id *nfmti)
1576 {
1577     ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1578 }
1579
1580 static void
1581 ofp_print_nxt_set_flow_format(struct ds *string,
1582                               const struct nx_set_flow_format *nsff)
1583 {
1584     uint32_t format = ntohl(nsff->format);
1585
1586     ds_put_cstr(string, " format=");
1587     if (ofputil_nx_flow_format_is_valid(format)) {
1588         ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
1589     } else {
1590         ds_put_format(string, "%"PRIu32, format);
1591     }
1592 }
1593
1594 static void
1595 ofp_print_nxt_set_packet_in_format(struct ds *string,
1596                                    const struct nx_set_packet_in_format *nspf)
1597 {
1598     uint32_t format = ntohl(nspf->format);
1599
1600     ds_put_cstr(string, " format=");
1601     if (ofputil_packet_in_format_is_valid(format)) {
1602         ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1603     } else {
1604         ds_put_format(string, "%"PRIu32, format);
1605     }
1606 }
1607
1608 static const char *
1609 ofp_port_reason_to_string(enum ofp_port_reason reason)
1610 {
1611     static char s[32];
1612
1613     switch (reason) {
1614     case OFPPR_ADD:
1615         return "add";
1616
1617     case OFPPR_DELETE:
1618         return "delete";
1619
1620     case OFPPR_MODIFY:
1621         return "modify";
1622
1623     default:
1624         sprintf(s, "%d", (int) reason);
1625         return s;
1626     }
1627 }
1628
1629 static void
1630 ofp_print_nxt_set_async_config(struct ds *string,
1631                                const struct nx_async_config *nac)
1632 {
1633     int i;
1634
1635     for (i = 0; i < 2; i++) {
1636         int j;
1637
1638         ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1639
1640         ds_put_cstr(string, "       PACKET_IN:");
1641         for (j = 0; j < 32; j++) {
1642             if (nac->packet_in_mask[i] & htonl(1u << j)) {
1643                 ds_put_format(string, " %s",
1644                               ofputil_packet_in_reason_to_string(j));
1645             }
1646         }
1647         if (!nac->packet_in_mask[i]) {
1648             ds_put_cstr(string, " (off)");
1649         }
1650         ds_put_char(string, '\n');
1651
1652         ds_put_cstr(string, "     PORT_STATUS:");
1653         for (j = 0; j < 32; j++) {
1654             if (nac->port_status_mask[i] & htonl(1u << j)) {
1655                 ds_put_format(string, " %s", ofp_port_reason_to_string(j));
1656             }
1657         }
1658         if (!nac->port_status_mask[i]) {
1659             ds_put_cstr(string, " (off)");
1660         }
1661         ds_put_char(string, '\n');
1662
1663         ds_put_cstr(string, "    FLOW_REMOVED:");
1664         for (j = 0; j < 32; j++) {
1665             if (nac->flow_removed_mask[i] & htonl(1u << j)) {
1666                 ds_put_format(string, " %s",
1667                               ofp_flow_removed_reason_to_string(j));
1668             }
1669         }
1670         if (!nac->flow_removed_mask[i]) {
1671             ds_put_cstr(string, " (off)");
1672         }
1673         ds_put_char(string, '\n');
1674     }
1675 }
1676
1677 static void
1678 ofp_print_nxt_set_controller_id(struct ds *string,
1679                                 const struct nx_controller_id *nci)
1680 {
1681     ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
1682 }
1683
1684 static void
1685 ofp_print_nxt_flow_monitor_cancel(struct ds *string,
1686                                   const struct ofp_header *oh)
1687 {
1688     ds_put_format(string, " id=%"PRIu32,
1689                   ofputil_decode_flow_monitor_cancel(oh));
1690 }
1691
1692 static const char *
1693 nx_flow_monitor_flags_to_name(uint32_t bit)
1694 {
1695     enum nx_flow_monitor_flags fmf = bit;
1696
1697     switch (fmf) {
1698     case NXFMF_INITIAL: return "initial";
1699     case NXFMF_ADD: return "add";
1700     case NXFMF_DELETE: return "delete";
1701     case NXFMF_MODIFY: return "modify";
1702     case NXFMF_ACTIONS: return "actions";
1703     case NXFMF_OWN: return "own";
1704     }
1705
1706     return NULL;
1707 }
1708
1709 static void
1710 ofp_print_nxst_flow_monitor_request(struct ds *string,
1711                                     const struct ofp_header *oh)
1712 {
1713     struct ofpbuf b;
1714
1715     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1716     for (;;) {
1717         struct ofputil_flow_monitor_request request;
1718         int retval;
1719
1720         retval = ofputil_decode_flow_monitor_request(&request, &b);
1721         if (retval) {
1722             if (retval != EOF) {
1723                 ofp_print_error(string, retval);
1724             }
1725             return;
1726         }
1727
1728         ds_put_format(string, "\n id=%"PRIu32" flags=", request.id);
1729         ofp_print_bit_names(string, request.flags,
1730                             nx_flow_monitor_flags_to_name, ',');
1731
1732         if (request.out_port != OFPP_NONE) {
1733             ds_put_cstr(string, " out_port=");
1734             ofputil_format_port(request.out_port, string);
1735         }
1736
1737         if (request.table_id != 0xff) {
1738             ds_put_format(string, " table=%"PRIu8, request.table_id);
1739         }
1740
1741         ds_put_char(string, ' ');
1742         match_format(&request.match, string, OFP_DEFAULT_PRIORITY);
1743         ds_chomp(string, ' ');
1744     }
1745 }
1746
1747 static void
1748 ofp_print_nxst_flow_monitor_reply(struct ds *string,
1749                                   const struct ofp_header *oh)
1750 {
1751     uint64_t ofpacts_stub[1024 / 8];
1752     struct ofpbuf ofpacts;
1753     struct ofpbuf b;
1754
1755     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1756     ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1757     for (;;) {
1758         struct ofputil_flow_update update;
1759         struct match match;
1760         int retval;
1761
1762         update.match = &match;
1763         retval = ofputil_decode_flow_update(&update, &b, &ofpacts);
1764         if (retval) {
1765             if (retval != EOF) {
1766                 ofp_print_error(string, retval);
1767             }
1768             ofpbuf_uninit(&ofpacts);
1769             return;
1770         }
1771
1772         ds_put_cstr(string, "\n event=");
1773         switch (update.event) {
1774         case NXFME_ADDED:
1775             ds_put_cstr(string, "ADDED");
1776             break;
1777
1778         case NXFME_DELETED:
1779             ds_put_format(string, "DELETED reason=%s",
1780                           ofp_flow_removed_reason_to_string(update.reason));
1781             break;
1782
1783         case NXFME_MODIFIED:
1784             ds_put_cstr(string, "MODIFIED");
1785             break;
1786
1787         case NXFME_ABBREV:
1788             ds_put_format(string, "ABBREV xid=0x%"PRIx32, ntohl(update.xid));
1789             continue;
1790         }
1791
1792         ds_put_format(string, " table=%"PRIu8, update.table_id);
1793         if (update.idle_timeout != OFP_FLOW_PERMANENT) {
1794             ds_put_format(string, " idle_timeout=%"PRIu16,
1795                           update.idle_timeout);
1796         }
1797         if (update.hard_timeout != OFP_FLOW_PERMANENT) {
1798             ds_put_format(string, " hard_timeout=%"PRIu16,
1799                           update.hard_timeout);
1800         }
1801         ds_put_format(string, " cookie=%#"PRIx64, ntohll(update.cookie));
1802
1803         ds_put_char(string, ' ');
1804         match_format(update.match, string, OFP_DEFAULT_PRIORITY);
1805
1806         if (update.ofpacts_len) {
1807             if (string->string[string->length - 1] != ' ') {
1808                 ds_put_char(string, ' ');
1809             }
1810             ofpacts_format(update.ofpacts, update.ofpacts_len, string);
1811         }
1812     }
1813 }
1814
1815 void
1816 ofp_print_version(const struct ofp_header *oh,
1817                   struct ds *string)
1818 {
1819     switch (oh->version) {
1820     case OFP10_VERSION:
1821         break;
1822     case OFP11_VERSION:
1823         ds_put_cstr(string, " (OF1.1)");
1824         break;
1825     case OFP12_VERSION:
1826         ds_put_cstr(string, " (OF1.2)");
1827         break;
1828     case OFP13_VERSION:
1829         ds_put_cstr(string, " (OF1.3)");
1830         break;
1831     default:
1832         ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
1833         break;
1834     }
1835     ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
1836 }
1837
1838 static void
1839 ofp_header_to_string__(const struct ofp_header *oh, enum ofpraw raw,
1840                        struct ds *string)
1841 {
1842     ds_put_cstr(string, ofpraw_get_name(raw));
1843     ofp_print_version(oh, string);
1844 }
1845
1846 static void
1847 ofp_print_not_implemented(struct ds *string)
1848 {
1849     ds_put_cstr(string, "NOT IMPLEMENTED YET!\n");
1850 }
1851
1852 static void
1853 ofp_to_string__(const struct ofp_header *oh, enum ofpraw raw,
1854                 struct ds *string, int verbosity)
1855 {
1856     const void *msg = oh;
1857
1858     ofp_header_to_string__(oh, raw, string);
1859     switch (ofptype_from_ofpraw(raw)) {
1860
1861         /* FIXME: Change the following once they are implemented: */
1862     case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
1863     case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
1864     case OFPTYPE_GET_ASYNC_REQUEST:
1865     case OFPTYPE_GET_ASYNC_REPLY:
1866     case OFPTYPE_METER_MOD:
1867     case OFPTYPE_GROUP_REQUEST:
1868     case OFPTYPE_GROUP_REPLY:
1869     case OFPTYPE_GROUP_DESC_REQUEST:
1870     case OFPTYPE_GROUP_DESC_REPLY:
1871     case OFPTYPE_GROUP_FEATURES_REQUEST:
1872     case OFPTYPE_GROUP_FEATURES_REPLY:
1873     case OFPTYPE_METER_REQUEST:
1874     case OFPTYPE_METER_REPLY:
1875     case OFPTYPE_METER_CONFIG_REQUEST:
1876     case OFPTYPE_METER_CONFIG_REPLY:
1877     case OFPTYPE_METER_FEATURES_REQUEST:
1878     case OFPTYPE_METER_FEATURES_REPLY:
1879     case OFPTYPE_TABLE_FEATURES_REQUEST:
1880     case OFPTYPE_TABLE_FEATURES_REPLY:
1881         ofp_print_not_implemented(string);
1882         break;
1883
1884     case OFPTYPE_HELLO:
1885         ofp_print_hello(string, oh);
1886         break;
1887
1888     case OFPTYPE_ERROR:
1889         ofp_print_error_msg(string, oh);
1890         break;
1891
1892     case OFPTYPE_ECHO_REQUEST:
1893     case OFPTYPE_ECHO_REPLY:
1894         ofp_print_echo(string, oh, verbosity);
1895         break;
1896
1897     case OFPTYPE_FEATURES_REQUEST:
1898         break;
1899
1900     case OFPTYPE_FEATURES_REPLY:
1901         ofp_print_switch_features(string, oh);
1902         break;
1903
1904     case OFPTYPE_GET_CONFIG_REQUEST:
1905         break;
1906
1907     case OFPTYPE_GET_CONFIG_REPLY:
1908     case OFPTYPE_SET_CONFIG:
1909         ofp_print_switch_config(string, ofpmsg_body(oh));
1910         break;
1911
1912     case OFPTYPE_PACKET_IN:
1913         ofp_print_packet_in(string, oh, verbosity);
1914         break;
1915
1916     case OFPTYPE_FLOW_REMOVED:
1917         ofp_print_flow_removed(string, oh);
1918         break;
1919
1920     case OFPTYPE_PORT_STATUS:
1921         ofp_print_port_status(string, oh);
1922         break;
1923
1924     case OFPTYPE_PACKET_OUT:
1925         ofp_print_packet_out(string, oh, verbosity);
1926         break;
1927
1928     case OFPTYPE_FLOW_MOD:
1929         ofp_print_flow_mod(string, oh, verbosity);
1930         break;
1931
1932     case OFPTYPE_PORT_MOD:
1933         ofp_print_port_mod(string, oh);
1934         break;
1935
1936     case OFPTYPE_BARRIER_REQUEST:
1937     case OFPTYPE_BARRIER_REPLY:
1938         break;
1939
1940     case OFPTYPE_ROLE_REQUEST:
1941     case OFPTYPE_ROLE_REPLY:
1942         ofp_print_role_message(string, oh);
1943         break;
1944
1945     case OFPTYPE_DESC_STATS_REQUEST:
1946     case OFPTYPE_PORT_DESC_STATS_REQUEST:
1947         ofp_print_stats_request(string, oh);
1948         break;
1949
1950     case OFPTYPE_FLOW_STATS_REQUEST:
1951     case OFPTYPE_AGGREGATE_STATS_REQUEST:
1952         ofp_print_stats_request(string, oh);
1953         ofp_print_flow_stats_request(string, oh);
1954         break;
1955
1956     case OFPTYPE_TABLE_STATS_REQUEST:
1957         ofp_print_stats_request(string, oh);
1958         break;
1959
1960     case OFPTYPE_PORT_STATS_REQUEST:
1961         ofp_print_stats_request(string, oh);
1962         ofp_print_ofpst_port_request(string, oh);
1963         break;
1964
1965     case OFPTYPE_QUEUE_STATS_REQUEST:
1966         ofp_print_stats_request(string, oh);
1967         ofp_print_ofpst_queue_request(string, oh);
1968         break;
1969
1970     case OFPTYPE_DESC_STATS_REPLY:
1971         ofp_print_stats_reply(string, oh);
1972         ofp_print_ofpst_desc_reply(string, oh);
1973         break;
1974
1975     case OFPTYPE_FLOW_STATS_REPLY:
1976         ofp_print_stats_reply(string, oh);
1977         ofp_print_flow_stats_reply(string, oh);
1978         break;
1979
1980     case OFPTYPE_QUEUE_STATS_REPLY:
1981         ofp_print_stats_reply(string, oh);
1982         ofp_print_ofpst_queue_reply(string, oh, verbosity);
1983         break;
1984
1985     case OFPTYPE_PORT_STATS_REPLY:
1986         ofp_print_stats_reply(string, oh);
1987         ofp_print_ofpst_port_reply(string, oh, verbosity);
1988         break;
1989
1990     case OFPTYPE_TABLE_STATS_REPLY:
1991         ofp_print_stats_reply(string, oh);
1992         ofp_print_ofpst_table_reply(string, oh, verbosity);
1993         break;
1994
1995     case OFPTYPE_AGGREGATE_STATS_REPLY:
1996         ofp_print_stats_reply(string, oh);
1997         ofp_print_aggregate_stats_reply(string, oh);
1998         break;
1999
2000     case OFPTYPE_PORT_DESC_STATS_REPLY:
2001         ofp_print_stats_reply(string, oh);
2002         ofp_print_ofpst_port_desc_reply(string, oh);
2003         break;
2004
2005     case OFPTYPE_FLOW_MOD_TABLE_ID:
2006         ofp_print_nxt_flow_mod_table_id(string, ofpmsg_body(oh));
2007         break;
2008
2009     case OFPTYPE_SET_FLOW_FORMAT:
2010         ofp_print_nxt_set_flow_format(string, ofpmsg_body(oh));
2011         break;
2012
2013     case OFPTYPE_SET_PACKET_IN_FORMAT:
2014         ofp_print_nxt_set_packet_in_format(string, ofpmsg_body(oh));
2015         break;
2016
2017     case OFPTYPE_FLOW_AGE:
2018         break;
2019
2020     case OFPTYPE_SET_CONTROLLER_ID:
2021         ofp_print_nxt_set_controller_id(string, ofpmsg_body(oh));
2022         break;
2023
2024     case OFPTYPE_SET_ASYNC_CONFIG:
2025         ofp_print_nxt_set_async_config(string, ofpmsg_body(oh));
2026         break;
2027
2028     case OFPTYPE_FLOW_MONITOR_CANCEL:
2029         ofp_print_nxt_flow_monitor_cancel(string, msg);
2030         break;
2031
2032     case OFPTYPE_FLOW_MONITOR_PAUSED:
2033     case OFPTYPE_FLOW_MONITOR_RESUMED:
2034         break;
2035
2036     case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
2037         ofp_print_nxst_flow_monitor_request(string, msg);
2038         break;
2039
2040     case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
2041         ofp_print_nxst_flow_monitor_reply(string, msg);
2042         break;
2043     }
2044 }
2045
2046 /* Composes and returns a string representing the OpenFlow packet of 'len'
2047  * bytes at 'oh' at the given 'verbosity' level.  0 is a minimal amount of
2048  * verbosity and higher numbers increase verbosity.  The caller is responsible
2049  * for freeing the string. */
2050 char *
2051 ofp_to_string(const void *oh_, size_t len, int verbosity)
2052 {
2053     struct ds string = DS_EMPTY_INITIALIZER;
2054     const struct ofp_header *oh = oh_;
2055
2056     if (!len) {
2057         ds_put_cstr(&string, "OpenFlow message is empty\n");
2058     } else if (len < sizeof(struct ofp_header)) {
2059         ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
2060                       len);
2061     } else if (ntohs(oh->length) > len) {
2062         enum ofperr error;
2063         enum ofpraw raw;
2064
2065         error = ofpraw_decode_partial(&raw, oh, len);
2066         if (!error) {
2067             ofp_header_to_string__(oh, raw, &string);
2068             ds_put_char(&string, '\n');
2069         }
2070
2071         ds_put_format(&string,
2072                       "(***truncated to %zu bytes from %"PRIu16"***)\n",
2073                       len, ntohs(oh->length));
2074     } else if (ntohs(oh->length) < len) {
2075         ds_put_format(&string,
2076                       "(***only uses %"PRIu16" bytes out of %zu***)\n",
2077                       ntohs(oh->length), len);
2078     } else {
2079         enum ofperr error;
2080         enum ofpraw raw;
2081
2082         error = ofpraw_decode(&raw, oh);
2083         if (!error) {
2084             ofp_to_string__(oh, raw, &string, verbosity);
2085             if (verbosity >= 5) {
2086                 if (ds_last(&string) != '\n') {
2087                     ds_put_char(&string, '\n');
2088                 }
2089                 ds_put_hex_dump(&string, oh, len, 0, true);
2090             }
2091             if (ds_last(&string) != '\n') {
2092                 ds_put_char(&string, '\n');
2093             }
2094             return ds_steal_cstr(&string);
2095         }
2096
2097         ofp_print_error(&string, error);
2098     }
2099     ds_put_hex_dump(&string, oh, len, 0, true);
2100     return ds_steal_cstr(&string);
2101 }
2102 \f
2103 static void
2104 print_and_free(FILE *stream, char *string)
2105 {
2106     fputs(string, stream);
2107     free(string);
2108 }
2109
2110 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
2111  * given 'verbosity' level.  0 is a minimal amount of verbosity and higher
2112  * numbers increase verbosity. */
2113 void
2114 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
2115 {
2116     print_and_free(stream, ofp_to_string(oh, len, verbosity));
2117 }
2118
2119 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
2120  * 'data' to 'stream'. */
2121 void
2122 ofp_print_packet(FILE *stream, const void *data, size_t len)
2123 {
2124     print_and_free(stream, ofp_packet_to_string(data, len));
2125 }