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