Remove assumption that there are 64 or fewer fields.
[cascardo/ovs.git] / lib / ofp-print.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "ofp-print.h"
19
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <sys/wait.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28
29 #include "bundle.h"
30 #include "byte-order.h"
31 #include "compiler.h"
32 #include "dynamic-string.h"
33 #include "flow.h"
34 #include "learn.h"
35 #include "multipath.h"
36 #include "meta-flow.h"
37 #include "netdev.h"
38 #include "nx-match.h"
39 #include "ofp-actions.h"
40 #include "ofp-errors.h"
41 #include "ofp-msgs.h"
42 #include "ofp-util.h"
43 #include "ofpbuf.h"
44 #include "openflow/openflow.h"
45 #include "openflow/nicira-ext.h"
46 #include "packets.h"
47 #include "type-props.h"
48 #include "unaligned.h"
49 #include "odp-util.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     const struct pkt_metadata md = PKT_METADATA_INITIALIZER(0);
63     struct ofpbuf buf;
64     struct flow flow;
65     size_t l4_size;
66
67     ofpbuf_use_const(&buf, data, len);
68     flow_extract(&buf, &md, &flow);
69     flow_format(&ds, &flow);
70
71     l4_size = ofpbuf_l4_size(&buf);
72
73     if (flow.nw_proto == IPPROTO_TCP && l4_size >= TCP_HEADER_LEN) {
74         struct tcp_header *th = ofpbuf_l4(&buf);
75         ds_put_format(&ds, " tcp_csum:%"PRIx16, ntohs(th->tcp_csum));
76     } else if (flow.nw_proto == IPPROTO_UDP && l4_size >= UDP_HEADER_LEN) {
77         struct udp_header *uh = ofpbuf_l4(&buf);
78         ds_put_format(&ds, " udp_csum:%"PRIx16, ntohs(uh->udp_csum));
79     } else if (flow.nw_proto == IPPROTO_SCTP && l4_size >= SCTP_HEADER_LEN) {
80         struct sctp_header *sh = ofpbuf_l4(&buf);
81         ds_put_format(&ds, " sctp_csum:%"PRIx32,
82                       ntohl(get_16aligned_be32(&sh->sctp_csum)));
83     }
84
85     ds_put_char(&ds, '\n');
86
87     return ds_cstr(&ds);
88 }
89
90 static void
91 ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
92                     int verbosity)
93 {
94     char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
95     struct ofputil_packet_in pin;
96     int error;
97     int i;
98
99     error = ofputil_decode_packet_in(&pin, oh);
100     if (error) {
101         ofp_print_error(string, error);
102         return;
103     }
104
105     if (pin.table_id) {
106         ds_put_format(string, " table_id=%"PRIu8, pin.table_id);
107     }
108
109     if (pin.cookie != OVS_BE64_MAX) {
110         ds_put_format(string, " cookie=0x%"PRIx64, ntohll(pin.cookie));
111     }
112
113     ds_put_format(string, " total_len=%"PRIuSIZE" in_port=", pin.total_len);
114     ofputil_format_port(pin.fmd.in_port, string);
115
116     if (pin.fmd.tun_id != htonll(0)) {
117         ds_put_format(string, " tun_id=0x%"PRIx64, ntohll(pin.fmd.tun_id));
118     }
119
120     if (pin.fmd.tun_src != htonl(0)) {
121         ds_put_format(string, " tun_src="IP_FMT, IP_ARGS(pin.fmd.tun_src));
122     }
123
124     if (pin.fmd.tun_dst != htonl(0)) {
125         ds_put_format(string, " tun_dst="IP_FMT, IP_ARGS(pin.fmd.tun_dst));
126     }
127
128     if (pin.fmd.metadata != htonll(0)) {
129         ds_put_format(string, " metadata=0x%"PRIx64, ntohll(pin.fmd.metadata));
130     }
131
132     for (i = 0; i < FLOW_N_REGS; i++) {
133         if (pin.fmd.regs[i]) {
134             ds_put_format(string, " reg%d=0x%"PRIx32, i, pin.fmd.regs[i]);
135         }
136     }
137
138     if (pin.fmd.pkt_mark != 0) {
139         ds_put_format(string, " pkt_mark=0x%"PRIx32, pin.fmd.pkt_mark);
140     }
141
142     ds_put_format(string, " (via %s)",
143                   ofputil_packet_in_reason_to_string(pin.reason, reasonbuf,
144                                                      sizeof reasonbuf));
145
146     ds_put_format(string, " data_len=%"PRIuSIZE, pin.packet_len);
147     if (pin.buffer_id == UINT32_MAX) {
148         ds_put_format(string, " (unbuffered)");
149         if (pin.total_len != pin.packet_len) {
150             ds_put_format(string, " (***total_len != data_len***)");
151         }
152     } else {
153         ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
154         if (pin.total_len < pin.packet_len) {
155             ds_put_format(string, " (***total_len < data_len***)");
156         }
157     }
158     ds_put_char(string, '\n');
159
160     if (verbosity > 0) {
161         char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
162         ds_put_cstr(string, packet);
163         free(packet);
164     }
165     if (verbosity > 2) {
166         ds_put_hex_dump(string, pin.packet, pin.packet_len, 0, false);
167     }
168 }
169
170 static void
171 ofp_print_packet_out(struct ds *string, const struct ofp_header *oh,
172                      int verbosity)
173 {
174     struct ofputil_packet_out po;
175     struct ofpbuf ofpacts;
176     enum ofperr error;
177
178     ofpbuf_init(&ofpacts, 64);
179     error = ofputil_decode_packet_out(&po, oh, &ofpacts);
180     if (error) {
181         ofpbuf_uninit(&ofpacts);
182         ofp_print_error(string, error);
183         return;
184     }
185
186     ds_put_cstr(string, " in_port=");
187     ofputil_format_port(po.in_port, string);
188
189     ds_put_cstr(string, " actions=");
190     ofpacts_format(po.ofpacts, po.ofpacts_len, string);
191
192     if (po.buffer_id == UINT32_MAX) {
193         ds_put_format(string, " data_len=%"PRIuSIZE, po.packet_len);
194         if (verbosity > 0 && po.packet_len > 0) {
195             char *packet = ofp_packet_to_string(po.packet, po.packet_len);
196             ds_put_char(string, '\n');
197             ds_put_cstr(string, packet);
198             free(packet);
199         }
200         if (verbosity > 2) {
201             ds_put_hex_dump(string, po.packet, po.packet_len, 0, false);
202         }
203     } else {
204         ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id);
205     }
206
207     ofpbuf_uninit(&ofpacts);
208 }
209
210 /* qsort comparison function. */
211 static int
212 compare_ports(const void *a_, const void *b_)
213 {
214     const struct ofputil_phy_port *a = a_;
215     const struct ofputil_phy_port *b = b_;
216     uint16_t ap = ofp_to_u16(a->port_no);
217     uint16_t bp = ofp_to_u16(b->port_no);
218
219     return ap < bp ? -1 : ap > bp;
220 }
221
222 static void
223 ofp_print_bit_names(struct ds *string, uint32_t bits,
224                     const char *(*bit_to_name)(uint32_t bit),
225                     char separator)
226 {
227     int n = 0;
228     int i;
229
230     if (!bits) {
231         ds_put_cstr(string, "0");
232         return;
233     }
234
235     for (i = 0; i < 32; i++) {
236         uint32_t bit = UINT32_C(1) << i;
237
238         if (bits & bit) {
239             const char *name = bit_to_name(bit);
240             if (name) {
241                 if (n++) {
242                     ds_put_char(string, separator);
243                 }
244                 ds_put_cstr(string, name);
245                 bits &= ~bit;
246             }
247         }
248     }
249
250     if (bits) {
251         if (n) {
252             ds_put_char(string, separator);
253         }
254         ds_put_format(string, "0x%"PRIx32, bits);
255     }
256 }
257
258 static const char *
259 netdev_feature_to_name(uint32_t bit)
260 {
261     enum netdev_features f = bit;
262
263     switch (f) {
264     case NETDEV_F_10MB_HD:    return "10MB-HD";
265     case NETDEV_F_10MB_FD:    return "10MB-FD";
266     case NETDEV_F_100MB_HD:   return "100MB-HD";
267     case NETDEV_F_100MB_FD:   return "100MB-FD";
268     case NETDEV_F_1GB_HD:     return "1GB-HD";
269     case NETDEV_F_1GB_FD:     return "1GB-FD";
270     case NETDEV_F_10GB_FD:    return "10GB-FD";
271     case NETDEV_F_40GB_FD:    return "40GB-FD";
272     case NETDEV_F_100GB_FD:   return "100GB-FD";
273     case NETDEV_F_1TB_FD:     return "1TB-FD";
274     case NETDEV_F_OTHER:      return "OTHER";
275     case NETDEV_F_COPPER:     return "COPPER";
276     case NETDEV_F_FIBER:      return "FIBER";
277     case NETDEV_F_AUTONEG:    return "AUTO_NEG";
278     case NETDEV_F_PAUSE:      return "AUTO_PAUSE";
279     case NETDEV_F_PAUSE_ASYM: return "AUTO_PAUSE_ASYM";
280     }
281
282     return NULL;
283 }
284
285 static void
286 ofp_print_port_features(struct ds *string, enum netdev_features features)
287 {
288     ofp_print_bit_names(string, features, netdev_feature_to_name, ' ');
289     ds_put_char(string, '\n');
290 }
291
292 static const char *
293 ofputil_port_config_to_name(uint32_t bit)
294 {
295     enum ofputil_port_config pc = bit;
296
297     switch (pc) {
298     case OFPUTIL_PC_PORT_DOWN:    return "PORT_DOWN";
299     case OFPUTIL_PC_NO_STP:       return "NO_STP";
300     case OFPUTIL_PC_NO_RECV:      return "NO_RECV";
301     case OFPUTIL_PC_NO_RECV_STP:  return "NO_RECV_STP";
302     case OFPUTIL_PC_NO_FLOOD:     return "NO_FLOOD";
303     case OFPUTIL_PC_NO_FWD:       return "NO_FWD";
304     case OFPUTIL_PC_NO_PACKET_IN: return "NO_PACKET_IN";
305     }
306
307     return NULL;
308 }
309
310 static void
311 ofp_print_port_config(struct ds *string, enum ofputil_port_config config)
312 {
313     ofp_print_bit_names(string, config, ofputil_port_config_to_name, ' ');
314     ds_put_char(string, '\n');
315 }
316
317 static const char *
318 ofputil_port_state_to_name(uint32_t bit)
319 {
320     enum ofputil_port_state ps = bit;
321
322     switch (ps) {
323     case OFPUTIL_PS_LINK_DOWN: return "LINK_DOWN";
324     case OFPUTIL_PS_BLOCKED:   return "BLOCKED";
325     case OFPUTIL_PS_LIVE:      return "LIVE";
326
327     case OFPUTIL_PS_STP_LISTEN:
328     case OFPUTIL_PS_STP_LEARN:
329     case OFPUTIL_PS_STP_FORWARD:
330     case OFPUTIL_PS_STP_BLOCK:
331         /* Handled elsewhere. */
332         return NULL;
333     }
334
335     return NULL;
336 }
337
338 static void
339 ofp_print_port_state(struct ds *string, enum ofputil_port_state state)
340 {
341     enum ofputil_port_state stp_state;
342
343     /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
344      * pattern.  We have to special case it.
345      *
346      * OVS doesn't support STP, so this field will always be 0 if we are
347      * talking to OVS, so we'd always print STP_LISTEN in that case.
348      * Therefore, we don't print anything at all if the value is STP_LISTEN, to
349      * avoid confusing users. */
350     stp_state = state & OFPUTIL_PS_STP_MASK;
351     if (stp_state) {
352         ds_put_cstr(string,
353                     (stp_state == OFPUTIL_PS_STP_LEARN ? "STP_LEARN"
354                      : stp_state == OFPUTIL_PS_STP_FORWARD ? "STP_FORWARD"
355                      : "STP_BLOCK"));
356         state &= ~OFPUTIL_PS_STP_MASK;
357         if (state) {
358             ofp_print_bit_names(string, state, ofputil_port_state_to_name,
359                                 ' ');
360         }
361     } else {
362         ofp_print_bit_names(string, state, ofputil_port_state_to_name, ' ');
363     }
364     ds_put_char(string, '\n');
365 }
366
367 static void
368 ofp_print_phy_port(struct ds *string, const struct ofputil_phy_port *port)
369 {
370     char name[sizeof port->name];
371     int j;
372
373     memcpy(name, port->name, sizeof name);
374     for (j = 0; j < sizeof name - 1; j++) {
375         if (!isprint((unsigned char) name[j])) {
376             break;
377         }
378     }
379     name[j] = '\0';
380
381     ds_put_char(string, ' ');
382     ofputil_format_port(port->port_no, string);
383     ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
384                   name, ETH_ADDR_ARGS(port->hw_addr));
385
386     ds_put_cstr(string, "     config:     ");
387     ofp_print_port_config(string, port->config);
388
389     ds_put_cstr(string, "     state:      ");
390     ofp_print_port_state(string, port->state);
391
392     if (port->curr) {
393         ds_put_format(string, "     current:    ");
394         ofp_print_port_features(string, port->curr);
395     }
396     if (port->advertised) {
397         ds_put_format(string, "     advertised: ");
398         ofp_print_port_features(string, port->advertised);
399     }
400     if (port->supported) {
401         ds_put_format(string, "     supported:  ");
402         ofp_print_port_features(string, port->supported);
403     }
404     if (port->peer) {
405         ds_put_format(string, "     peer:       ");
406         ofp_print_port_features(string, port->peer);
407     }
408     ds_put_format(string, "     speed: %"PRIu32" Mbps now, "
409                   "%"PRIu32" Mbps max\n",
410                   port->curr_speed / UINT32_C(1000),
411                   port->max_speed / UINT32_C(1000));
412 }
413
414 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
415  * 'ofp_version', writes a detailed description of each port into
416  * 'string'. */
417 static void
418 ofp_print_phy_ports(struct ds *string, uint8_t ofp_version,
419                     struct ofpbuf *b)
420 {
421     struct ofputil_phy_port *ports;
422     size_t allocated_ports, n_ports;
423     int retval;
424     size_t i;
425
426     ports = NULL;
427     allocated_ports = 0;
428     for (n_ports = 0; ; n_ports++) {
429         if (n_ports >= allocated_ports) {
430             ports = x2nrealloc(ports, &allocated_ports, sizeof *ports);
431         }
432
433         retval = ofputil_pull_phy_port(ofp_version, b, &ports[n_ports]);
434         if (retval) {
435             break;
436         }
437     }
438
439     qsort(ports, n_ports, sizeof *ports, compare_ports);
440     for (i = 0; i < n_ports; i++) {
441         ofp_print_phy_port(string, &ports[i]);
442     }
443     free(ports);
444
445     if (retval != EOF) {
446         ofp_print_error(string, retval);
447     }
448 }
449
450 static const char *
451 ofputil_capabilities_to_name(uint32_t bit)
452 {
453     enum ofputil_capabilities capabilities = bit;
454
455     switch (capabilities) {
456     case OFPUTIL_C_FLOW_STATS:   return "FLOW_STATS";
457     case OFPUTIL_C_TABLE_STATS:  return "TABLE_STATS";
458     case OFPUTIL_C_PORT_STATS:   return "PORT_STATS";
459     case OFPUTIL_C_IP_REASM:     return "IP_REASM";
460     case OFPUTIL_C_QUEUE_STATS:  return "QUEUE_STATS";
461     case OFPUTIL_C_ARP_MATCH_IP: return "ARP_MATCH_IP";
462     case OFPUTIL_C_STP:          return "STP";
463     case OFPUTIL_C_GROUP_STATS:  return "GROUP_STATS";
464     case OFPUTIL_C_PORT_BLOCKED: return "PORT_BLOCKED";
465     }
466
467     return NULL;
468 }
469
470 static const char *
471 ofputil_action_bitmap_to_name(uint32_t bit)
472 {
473     enum ofputil_action_bitmap action = bit;
474
475     switch (action) {
476     case OFPUTIL_A_OUTPUT:         return "OUTPUT";
477     case OFPUTIL_A_SET_VLAN_VID:   return "SET_VLAN_VID";
478     case OFPUTIL_A_SET_VLAN_PCP:   return "SET_VLAN_PCP";
479     case OFPUTIL_A_STRIP_VLAN:     return "STRIP_VLAN";
480     case OFPUTIL_A_SET_DL_SRC:     return "SET_DL_SRC";
481     case OFPUTIL_A_SET_DL_DST:     return "SET_DL_DST";
482     case OFPUTIL_A_SET_NW_SRC:     return "SET_NW_SRC";
483     case OFPUTIL_A_SET_NW_DST:     return "SET_NW_DST";
484     case OFPUTIL_A_SET_NW_ECN:     return "SET_NW_ECN";
485     case OFPUTIL_A_SET_NW_TOS:     return "SET_NW_TOS";
486     case OFPUTIL_A_SET_TP_SRC:     return "SET_TP_SRC";
487     case OFPUTIL_A_SET_TP_DST:     return "SET_TP_DST";
488     case OFPUTIL_A_SET_FIELD:      return "SET_FIELD";
489     case OFPUTIL_A_ENQUEUE:        return "ENQUEUE";
490     case OFPUTIL_A_COPY_TTL_OUT:   return "COPY_TTL_OUT";
491     case OFPUTIL_A_COPY_TTL_IN:    return "COPY_TTL_IN";
492     case OFPUTIL_A_SET_MPLS_LABEL: return "SET_MPLS_LABEL";
493     case OFPUTIL_A_SET_MPLS_TC:    return "SET_MPLS_TC";
494     case OFPUTIL_A_SET_MPLS_TTL:   return "SET_MPLS_TTL";
495     case OFPUTIL_A_DEC_MPLS_TTL:   return "DEC_MPLS_TTL";
496     case OFPUTIL_A_PUSH_VLAN:      return "PUSH_VLAN";
497     case OFPUTIL_A_POP_VLAN:       return "POP_VLAN";
498     case OFPUTIL_A_PUSH_MPLS:      return "PUSH_MPLS";
499     case OFPUTIL_A_POP_MPLS:       return "POP_MPLS";
500     case OFPUTIL_A_SET_QUEUE:      return "SET_QUEUE";
501     case OFPUTIL_A_GROUP:          return "GROUP";
502     case OFPUTIL_A_SET_NW_TTL:     return "SET_NW_TTL";
503     case OFPUTIL_A_DEC_NW_TTL:     return "DEC_NW_TTL";
504     }
505
506     return NULL;
507 }
508
509 static void
510 ofp_print_switch_features(struct ds *string, const struct ofp_header *oh)
511 {
512     struct ofputil_switch_features features;
513     enum ofperr error;
514     struct ofpbuf b;
515
516     error = ofputil_decode_switch_features(oh, &features, &b);
517     if (error) {
518         ofp_print_error(string, error);
519         return;
520     }
521
522     ds_put_format(string, " dpid:%016"PRIx64"\n", features.datapath_id);
523
524     ds_put_format(string, "n_tables:%"PRIu8", n_buffers:%"PRIu32,
525                   features.n_tables, features.n_buffers);
526     if (features.auxiliary_id) {
527         ds_put_format(string, ", auxiliary_id:%"PRIu8, features.auxiliary_id);
528     }
529     ds_put_char(string, '\n');
530
531     ds_put_cstr(string, "capabilities: ");
532     ofp_print_bit_names(string, features.capabilities,
533                         ofputil_capabilities_to_name, ' ');
534     ds_put_char(string, '\n');
535
536     switch ((enum ofp_version)oh->version) {
537     case OFP10_VERSION:
538         ds_put_cstr(string, "actions: ");
539         ofp_print_bit_names(string, features.actions,
540                             ofputil_action_bitmap_to_name, ' ');
541         ds_put_char(string, '\n');
542         break;
543     case OFP11_VERSION:
544     case OFP12_VERSION:
545         break;
546     case OFP13_VERSION:
547     case OFP14_VERSION:
548     case OFP15_VERSION:
549         return; /* no ports in ofp13_switch_features */
550     default:
551         OVS_NOT_REACHED();
552     }
553
554     ofp_print_phy_ports(string, oh->version, &b);
555 }
556
557 static void
558 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
559 {
560     enum ofp_config_flags flags;
561
562     flags = ntohs(osc->flags);
563
564     ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
565     flags &= ~OFPC_FRAG_MASK;
566
567     if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) {
568         ds_put_format(string, " invalid_ttl_to_controller");
569         flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER;
570     }
571
572     if (flags) {
573         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
574     }
575
576     ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
577 }
578
579 static void print_wild(struct ds *string, const char *leader, int is_wild,
580             int verbosity, const char *format, ...)
581             PRINTF_FORMAT(5, 6);
582
583 static void print_wild(struct ds *string, const char *leader, int is_wild,
584                        int verbosity, const char *format, ...)
585 {
586     if (is_wild && verbosity < 2) {
587         return;
588     }
589     ds_put_cstr(string, leader);
590     if (!is_wild) {
591         va_list args;
592
593         va_start(args, format);
594         ds_put_format_valist(string, format, args);
595         va_end(args);
596     } else {
597         ds_put_char(string, '*');
598     }
599     ds_put_char(string, ',');
600 }
601
602 static void
603 print_wild_port(struct ds *string, const char *leader, int is_wild,
604                 int verbosity, ofp_port_t port)
605 {
606     if (is_wild && verbosity < 2) {
607         return;
608     }
609     ds_put_cstr(string, leader);
610     if (!is_wild) {
611         ofputil_format_port(port, string);
612     } else {
613         ds_put_char(string, '*');
614     }
615     ds_put_char(string, ',');
616 }
617
618 static void
619 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
620                  uint32_t wild_bits, int verbosity)
621 {
622     if (wild_bits >= 32 && verbosity < 2) {
623         return;
624     }
625     ds_put_cstr(string, leader);
626     if (wild_bits < 32) {
627         ds_put_format(string, IP_FMT, IP_ARGS(ip));
628         if (wild_bits) {
629             ds_put_format(string, "/%d", 32 - wild_bits);
630         }
631     } else {
632         ds_put_char(string, '*');
633     }
634     ds_put_char(string, ',');
635 }
636
637 void
638 ofp10_match_print(struct ds *f, const struct ofp10_match *om, int verbosity)
639 {
640     char *s = ofp10_match_to_string(om, verbosity);
641     ds_put_cstr(f, s);
642     free(s);
643 }
644
645 char *
646 ofp10_match_to_string(const struct ofp10_match *om, int verbosity)
647 {
648     struct ds f = DS_EMPTY_INITIALIZER;
649     uint32_t w = ntohl(om->wildcards);
650     bool skip_type = false;
651     bool skip_proto = false;
652
653     if (!(w & OFPFW10_DL_TYPE)) {
654         skip_type = true;
655         if (om->dl_type == htons(ETH_TYPE_IP)) {
656             if (!(w & OFPFW10_NW_PROTO)) {
657                 skip_proto = true;
658                 if (om->nw_proto == IPPROTO_ICMP) {
659                     ds_put_cstr(&f, "icmp,");
660                 } else if (om->nw_proto == IPPROTO_TCP) {
661                     ds_put_cstr(&f, "tcp,");
662                 } else if (om->nw_proto == IPPROTO_UDP) {
663                     ds_put_cstr(&f, "udp,");
664                 } else if (om->nw_proto == IPPROTO_SCTP) {
665                     ds_put_cstr(&f, "sctp,");
666                 } else {
667                     ds_put_cstr(&f, "ip,");
668                     skip_proto = false;
669                 }
670             } else {
671                 ds_put_cstr(&f, "ip,");
672             }
673         } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
674             ds_put_cstr(&f, "arp,");
675         } else if (om->dl_type == htons(ETH_TYPE_RARP)){
676             ds_put_cstr(&f, "rarp,");
677         } else if (om->dl_type == htons(ETH_TYPE_MPLS)) {
678             ds_put_cstr(&f, "mpls,");
679         } else if (om->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
680             ds_put_cstr(&f, "mplsm,");
681         } else {
682             skip_type = false;
683         }
684     }
685     print_wild_port(&f, "in_port=", w & OFPFW10_IN_PORT, verbosity,
686                     u16_to_ofp(ntohs(om->in_port)));
687     print_wild(&f, "dl_vlan=", w & OFPFW10_DL_VLAN, verbosity,
688                "%d", ntohs(om->dl_vlan));
689     print_wild(&f, "dl_vlan_pcp=", w & OFPFW10_DL_VLAN_PCP, verbosity,
690                "%d", om->dl_vlan_pcp);
691     print_wild(&f, "dl_src=", w & OFPFW10_DL_SRC, verbosity,
692                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
693     print_wild(&f, "dl_dst=", w & OFPFW10_DL_DST, verbosity,
694                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
695     if (!skip_type) {
696         print_wild(&f, "dl_type=", w & OFPFW10_DL_TYPE, verbosity,
697                    "0x%04x", ntohs(om->dl_type));
698     }
699     print_ip_netmask(&f, "nw_src=", om->nw_src,
700                      (w & OFPFW10_NW_SRC_MASK) >> OFPFW10_NW_SRC_SHIFT,
701                      verbosity);
702     print_ip_netmask(&f, "nw_dst=", om->nw_dst,
703                      (w & OFPFW10_NW_DST_MASK) >> OFPFW10_NW_DST_SHIFT,
704                      verbosity);
705     if (!skip_proto) {
706         if (om->dl_type == htons(ETH_TYPE_ARP) ||
707             om->dl_type == htons(ETH_TYPE_RARP)) {
708             print_wild(&f, "arp_op=", w & OFPFW10_NW_PROTO, verbosity,
709                        "%u", om->nw_proto);
710         } else {
711             print_wild(&f, "nw_proto=", w & OFPFW10_NW_PROTO, verbosity,
712                        "%u", om->nw_proto);
713         }
714     }
715     print_wild(&f, "nw_tos=", w & OFPFW10_NW_TOS, verbosity,
716                "%u", om->nw_tos);
717     if (om->nw_proto == IPPROTO_ICMP) {
718         print_wild(&f, "icmp_type=", w & OFPFW10_ICMP_TYPE, verbosity,
719                    "%d", ntohs(om->tp_src));
720         print_wild(&f, "icmp_code=", w & OFPFW10_ICMP_CODE, verbosity,
721                    "%d", ntohs(om->tp_dst));
722     } else {
723         print_wild(&f, "tp_src=", w & OFPFW10_TP_SRC, verbosity,
724                    "%d", ntohs(om->tp_src));
725         print_wild(&f, "tp_dst=", w & OFPFW10_TP_DST, verbosity,
726                    "%d", ntohs(om->tp_dst));
727     }
728     if (ds_last(&f) == ',') {
729         f.length--;
730     }
731     return ds_cstr(&f);
732 }
733
734 static void
735 ofp_print_flow_flags(struct ds *s, enum ofputil_flow_mod_flags flags)
736 {
737     if (flags & OFPUTIL_FF_SEND_FLOW_REM) {
738         ds_put_cstr(s, "send_flow_rem ");
739     }
740     if (flags & OFPUTIL_FF_CHECK_OVERLAP) {
741         ds_put_cstr(s, "check_overlap ");
742     }
743     if (flags & OFPUTIL_FF_RESET_COUNTS) {
744         ds_put_cstr(s, "reset_counts ");
745     }
746     if (flags & OFPUTIL_FF_NO_PKT_COUNTS) {
747         ds_put_cstr(s, "no_packet_counts ");
748     }
749     if (flags & OFPUTIL_FF_NO_BYT_COUNTS) {
750         ds_put_cstr(s, "no_byte_counts ");
751     }
752     if (flags & OFPUTIL_FF_HIDDEN_FIELDS) {
753         ds_put_cstr(s, "allow_hidden_fields ");
754     }
755     if (flags & OFPUTIL_FF_NO_READONLY) {
756         ds_put_cstr(s, "no_readonly_table ");
757     }
758 }
759
760 static void
761 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh, int verbosity)
762 {
763     struct ofputil_flow_mod fm;
764     struct ofpbuf ofpacts;
765     bool need_priority;
766     enum ofperr error;
767     enum ofpraw raw;
768     enum ofputil_protocol protocol;
769
770     protocol = ofputil_protocol_from_ofp_version(oh->version);
771     protocol = ofputil_protocol_set_tid(protocol, true);
772
773     ofpbuf_init(&ofpacts, 64);
774     error = ofputil_decode_flow_mod(&fm, oh, protocol, &ofpacts,
775                                     OFPP_MAX, 255);
776     if (error) {
777         ofpbuf_uninit(&ofpacts);
778         ofp_print_error(s, error);
779         return;
780     }
781
782     ds_put_char(s, ' ');
783     switch (fm.command) {
784     case OFPFC_ADD:
785         ds_put_cstr(s, "ADD");
786         break;
787     case OFPFC_MODIFY:
788         ds_put_cstr(s, "MOD");
789         break;
790     case OFPFC_MODIFY_STRICT:
791         ds_put_cstr(s, "MOD_STRICT");
792         break;
793     case OFPFC_DELETE:
794         ds_put_cstr(s, "DEL");
795         break;
796     case OFPFC_DELETE_STRICT:
797         ds_put_cstr(s, "DEL_STRICT");
798         break;
799     default:
800         ds_put_format(s, "cmd:%d", fm.command);
801     }
802     if (fm.table_id != 0) {
803         ds_put_format(s, " table:%d", fm.table_id);
804     }
805
806     ds_put_char(s, ' ');
807     ofpraw_decode(&raw, oh);
808     if (verbosity >= 3 && raw == OFPRAW_OFPT10_FLOW_MOD) {
809         const struct ofp10_flow_mod *ofm = ofpmsg_body(oh);
810         ofp10_match_print(s, &ofm->match, verbosity);
811
812         /* ofp_print_match() doesn't print priority. */
813         need_priority = true;
814     } else if (verbosity >= 3 && raw == OFPRAW_NXT_FLOW_MOD) {
815         const struct nx_flow_mod *nfm = ofpmsg_body(oh);
816         const void *nxm = nfm + 1;
817         char *nxm_s;
818
819         nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
820         ds_put_cstr(s, nxm_s);
821         free(nxm_s);
822
823         /* nx_match_to_string() doesn't print priority. */
824         need_priority = true;
825     } else {
826         match_format(&fm.match, s, fm.priority);
827
828         /* match_format() does print priority. */
829         need_priority = false;
830     }
831
832     if (ds_last(s) != ' ') {
833         ds_put_char(s, ' ');
834     }
835     if (fm.new_cookie != htonll(0) && fm.new_cookie != OVS_BE64_MAX) {
836         ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.new_cookie));
837     }
838     if (fm.cookie_mask != htonll(0)) {
839         ds_put_format(s, "cookie:0x%"PRIx64"/0x%"PRIx64" ",
840                 ntohll(fm.cookie), ntohll(fm.cookie_mask));
841     }
842     if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
843         ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
844     }
845     if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
846         ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
847     }
848     if (fm.priority != OFP_DEFAULT_PRIORITY && need_priority) {
849         ds_put_format(s, "pri:%"PRIu16" ", fm.priority);
850     }
851     if (fm.buffer_id != UINT32_MAX) {
852         ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
853     }
854     if (fm.out_port != OFPP_ANY) {
855         ds_put_format(s, "out_port:");
856         ofputil_format_port(fm.out_port, s);
857         ds_put_char(s, ' ');
858     }
859
860     if (oh->version == OFP10_VERSION || oh->version == OFP11_VERSION) {
861         /* Don't print the reset_counts flag for OF1.0 and OF1.1 because those
862          * versions don't really have such a flag and printing one is likely to
863          * confuse people. */
864         fm.flags &= ~OFPUTIL_FF_RESET_COUNTS;
865     }
866     ofp_print_flow_flags(s, fm.flags);
867
868     ds_put_cstr(s, "actions=");
869     ofpacts_format(fm.ofpacts, fm.ofpacts_len, s);
870     ofpbuf_uninit(&ofpacts);
871 }
872
873 static void
874 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
875 {
876     ds_put_format(string, "%u", sec);
877
878     /* If there are no fractional seconds, don't print any decimals.
879      *
880      * If the fractional seconds can be expressed exactly as milliseconds,
881      * print 3 decimals.  Open vSwitch provides millisecond precision for most
882      * time measurements, so printing 3 decimals every time makes it easier to
883      * spot real changes in flow dumps that refresh themselves quickly.
884      *
885      * If the fractional seconds are more precise than milliseconds, print the
886      * number of decimals needed to express them exactly.
887      */
888     if (nsec > 0) {
889         unsigned int msec = nsec / 1000000;
890         if (msec * 1000000 == nsec) {
891             ds_put_format(string, ".%03u", msec);
892         } else {
893             ds_put_format(string, ".%09u", nsec);
894             while (string->string[string->length - 1] == '0') {
895                 string->length--;
896             }
897         }
898     }
899     ds_put_char(string, 's');
900 }
901
902 /* Returns a string form of 'reason'.  The return value is either a statically
903  * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
904  * 'bufsize' should be at least OFP_FLOW_REMOVED_REASON_BUFSIZE. */
905 #define OFP_FLOW_REMOVED_REASON_BUFSIZE (INT_STRLEN(int) + 1)
906 static const char *
907 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason,
908                                   char *reasonbuf, size_t bufsize)
909 {
910     switch (reason) {
911     case OFPRR_IDLE_TIMEOUT:
912         return "idle";
913     case OFPRR_HARD_TIMEOUT:
914         return "hard";
915     case OFPRR_DELETE:
916         return "delete";
917     case OFPRR_GROUP_DELETE:
918         return "group_delete";
919     case OFPRR_EVICTION:
920         return "eviction";
921     case OFPRR_METER_DELETE:
922         return "meter_delete";
923     default:
924         snprintf(reasonbuf, bufsize, "%d", (int) reason);
925         return reasonbuf;
926     }
927 }
928
929 static void
930 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
931 {
932     char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
933     struct ofputil_flow_removed fr;
934     enum ofperr error;
935
936     error = ofputil_decode_flow_removed(&fr, oh);
937     if (error) {
938         ofp_print_error(string, error);
939         return;
940     }
941
942     ds_put_char(string, ' ');
943     match_format(&fr.match, string, fr.priority);
944
945     ds_put_format(string, " reason=%s",
946                   ofp_flow_removed_reason_to_string(fr.reason, reasonbuf,
947                                                     sizeof reasonbuf));
948
949     if (fr.table_id != 255) {
950         ds_put_format(string, " table_id=%"PRIu8, fr.table_id);
951     }
952
953     if (fr.cookie != htonll(0)) {
954         ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
955     }
956     ds_put_cstr(string, " duration");
957     ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
958     ds_put_format(string, " idle%"PRIu16, fr.idle_timeout);
959     if (fr.hard_timeout) {
960         /* The hard timeout was only added in OF1.2, so only print it if it is
961          * actually in use to avoid gratuitous change to the formatting. */
962         ds_put_format(string, " hard%"PRIu16, fr.hard_timeout);
963     }
964     ds_put_format(string, " pkts%"PRIu64" bytes%"PRIu64"\n",
965                   fr.packet_count, fr.byte_count);
966 }
967
968 static void
969 ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
970 {
971     struct ofputil_port_mod pm;
972     enum ofperr error;
973
974     error = ofputil_decode_port_mod(oh, &pm, true);
975     if (error) {
976         ofp_print_error(string, error);
977         return;
978     }
979
980     ds_put_cstr(string, "port: ");
981     ofputil_format_port(pm.port_no, string);
982     ds_put_format(string, ": addr:"ETH_ADDR_FMT"\n",
983                   ETH_ADDR_ARGS(pm.hw_addr));
984
985     ds_put_cstr(string, "     config: ");
986     ofp_print_port_config(string, pm.config);
987
988     ds_put_cstr(string, "     mask:   ");
989     ofp_print_port_config(string, pm.mask);
990
991     ds_put_cstr(string, "     advertise: ");
992     if (pm.advertise) {
993         ofp_print_port_features(string, pm.advertise);
994     } else {
995         ds_put_cstr(string, "UNCHANGED\n");
996     }
997 }
998
999 static void
1000 ofp_print_table_miss_config(struct ds *string, const uint32_t config)
1001 {
1002     uint32_t table_miss_config = config & OFPTC11_TABLE_MISS_MASK;
1003
1004     switch (table_miss_config) {
1005     case OFPTC11_TABLE_MISS_CONTROLLER:
1006         ds_put_cstr(string, "controller\n");
1007         break;
1008     case OFPTC11_TABLE_MISS_CONTINUE:
1009         ds_put_cstr(string, "continue\n");
1010         break;
1011     case OFPTC11_TABLE_MISS_DROP:
1012         ds_put_cstr(string, "drop\n");
1013         break;
1014     default:
1015         ds_put_cstr(string, "Unknown\n");
1016         break;
1017     }
1018 }
1019
1020 static void
1021 ofp_print_table_mod(struct ds *string, const struct ofp_header *oh)
1022 {
1023     struct ofputil_table_mod pm;
1024     enum ofperr error;
1025
1026     error = ofputil_decode_table_mod(oh, &pm);
1027     if (error) {
1028         ofp_print_error(string, error);
1029         return;
1030     }
1031
1032     if (pm.table_id == 0xff) {
1033         ds_put_cstr(string, " table_id: ALL_TABLES");
1034     } else {
1035         ds_put_format(string, " table_id=%"PRIu8, pm.table_id);
1036     }
1037
1038     ds_put_cstr(string, ", flow_miss_config=");
1039     ofp_print_table_miss_config(string, pm.config);
1040 }
1041
1042 static void
1043 ofp_print_queue_get_config_request(struct ds *string,
1044                                    const struct ofp_header *oh)
1045 {
1046     enum ofperr error;
1047     ofp_port_t port;
1048
1049     error = ofputil_decode_queue_get_config_request(oh, &port);
1050     if (error) {
1051         ofp_print_error(string, error);
1052         return;
1053     }
1054
1055     ds_put_cstr(string, " port=");
1056     ofputil_format_port(port, string);
1057 }
1058
1059 static void
1060 print_queue_rate(struct ds *string, const char *name, unsigned int rate)
1061 {
1062     if (rate <= 1000) {
1063         ds_put_format(string, " %s:%u.%u%%", name, rate / 10, rate % 10);
1064     } else if (rate < UINT16_MAX) {
1065         ds_put_format(string, " %s:(disabled)", name);
1066     }
1067 }
1068
1069 static void
1070 ofp_print_queue_get_config_reply(struct ds *string,
1071                                  const struct ofp_header *oh)
1072 {
1073     enum ofperr error;
1074     struct ofpbuf b;
1075     ofp_port_t port;
1076
1077     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1078     error = ofputil_decode_queue_get_config_reply(&b, &port);
1079     if (error) {
1080         ofp_print_error(string, error);
1081         return;
1082     }
1083
1084     ds_put_cstr(string, " port=");
1085     ofputil_format_port(port, string);
1086     ds_put_char(string, '\n');
1087
1088     for (;;) {
1089         struct ofputil_queue_config queue;
1090         int retval;
1091
1092         retval = ofputil_pull_queue_get_config_reply(&b, &queue);
1093         if (retval) {
1094             if (retval != EOF) {
1095                 ofp_print_error(string, retval);
1096             }
1097             break;
1098         }
1099
1100         ds_put_format(string, "queue %"PRIu32":", queue.queue_id);
1101         print_queue_rate(string, "min_rate", queue.min_rate);
1102         print_queue_rate(string, "max_rate", queue.max_rate);
1103         ds_put_char(string, '\n');
1104     }
1105 }
1106
1107 static void
1108 ofp_print_meter_flags(struct ds *s, uint16_t flags)
1109 {
1110     if (flags & OFPMF13_KBPS) {
1111         ds_put_cstr(s, "kbps ");
1112     }
1113     if (flags & OFPMF13_PKTPS) {
1114         ds_put_cstr(s, "pktps ");
1115     }
1116     if (flags & OFPMF13_BURST) {
1117         ds_put_cstr(s, "burst ");
1118     }
1119     if (flags & OFPMF13_STATS) {
1120         ds_put_cstr(s, "stats ");
1121     }
1122
1123     flags &= ~(OFPMF13_KBPS | OFPMF13_PKTPS | OFPMF13_BURST | OFPMF13_STATS);
1124     if (flags) {
1125         ds_put_format(s, "flags:0x%"PRIx16" ", flags);
1126     }
1127 }
1128
1129 static void
1130 ofp_print_meter_band(struct ds *s, uint16_t flags,
1131                      const struct ofputil_meter_band *mb)
1132 {
1133     ds_put_cstr(s, "\ntype=");
1134     switch (mb->type) {
1135     case OFPMBT13_DROP:
1136         ds_put_cstr(s, "drop");
1137         break;
1138     case OFPMBT13_DSCP_REMARK:
1139         ds_put_cstr(s, "dscp_remark");
1140         break;
1141     default:
1142         ds_put_format(s, "%u", mb->type);
1143     }
1144
1145     ds_put_format(s, " rate=%"PRIu32, mb->rate);
1146
1147     if (flags & OFPMF13_BURST) {
1148         ds_put_format(s, " burst_size=%"PRIu32, mb->burst_size);
1149     }
1150     if (mb->type == OFPMBT13_DSCP_REMARK) {
1151         ds_put_format(s, " prec_level=%"PRIu8, mb->prec_level);
1152     }
1153 }
1154
1155 static void
1156 ofp_print_meter_stats(struct ds *s, const struct ofputil_meter_stats *ms)
1157 {
1158     uint16_t i;
1159
1160     ds_put_format(s, "meter:%"PRIu32" ", ms->meter_id);
1161     ds_put_format(s, "flow_count:%"PRIu32" ", ms->flow_count);
1162     ds_put_format(s, "packet_in_count:%"PRIu64" ", ms->packet_in_count);
1163     ds_put_format(s, "byte_in_count:%"PRIu64" ", ms->byte_in_count);
1164     ds_put_cstr(s, "duration:");
1165     ofp_print_duration(s, ms->duration_sec, ms->duration_nsec);
1166     ds_put_char(s, ' ');
1167
1168     ds_put_cstr(s, "bands:\n");
1169     for (i = 0; i < ms->n_bands; ++i) {
1170         ds_put_format(s, "%d: ", i);
1171         ds_put_format(s, "packet_count:%"PRIu64" ", ms->bands[i].packet_count);
1172         ds_put_format(s, "byte_count:%"PRIu64"\n", ms->bands[i].byte_count);
1173     }
1174 }
1175
1176 static void
1177 ofp_print_meter_config(struct ds *s, const struct ofputil_meter_config *mc)
1178 {
1179     uint16_t i;
1180
1181     ds_put_format(s, "meter=%"PRIu32" ", mc->meter_id);
1182
1183     ofp_print_meter_flags(s, mc->flags);
1184
1185     ds_put_cstr(s, "bands=");
1186     for (i = 0; i < mc->n_bands; ++i) {
1187         ofp_print_meter_band(s, mc->flags, &mc->bands[i]);
1188     }
1189     ds_put_char(s, '\n');
1190 }
1191
1192 static void
1193 ofp_print_meter_mod(struct ds *s, const struct ofp_header *oh)
1194 {
1195     struct ofputil_meter_mod mm;
1196     struct ofpbuf bands;
1197     enum ofperr error;
1198
1199     ofpbuf_init(&bands, 64);
1200     error = ofputil_decode_meter_mod(oh, &mm, &bands);
1201     if (error) {
1202         ofpbuf_uninit(&bands);
1203         ofp_print_error(s, error);
1204         return;
1205     }
1206
1207     switch (mm.command) {
1208     case OFPMC13_ADD:
1209         ds_put_cstr(s, " ADD ");
1210         break;
1211     case OFPMC13_MODIFY:
1212         ds_put_cstr(s, " MOD ");
1213         break;
1214     case OFPMC13_DELETE:
1215         ds_put_cstr(s, " DEL ");
1216         break;
1217     default:
1218         ds_put_format(s, " cmd:%d ", mm.command);
1219     }
1220
1221     ofp_print_meter_config(s, &mm.meter);
1222     ofpbuf_uninit(&bands);
1223 }
1224
1225 static void
1226 ofp_print_meter_stats_request(struct ds *s, const struct ofp_header *oh)
1227 {
1228     uint32_t meter_id;
1229
1230     ofputil_decode_meter_request(oh, &meter_id);
1231
1232     ds_put_format(s, " meter=%"PRIu32, meter_id);
1233 }
1234
1235 static const char *
1236 ofputil_meter_capabilities_to_name(uint32_t bit)
1237 {
1238     enum ofp13_meter_flags flag = bit;
1239
1240     switch (flag) {
1241     case OFPMF13_KBPS:    return "kbps";
1242     case OFPMF13_PKTPS:   return "pktps";
1243     case OFPMF13_BURST:   return "burst";
1244     case OFPMF13_STATS:   return "stats";
1245     }
1246
1247     return NULL;
1248 }
1249
1250 static const char *
1251 ofputil_meter_band_types_to_name(uint32_t bit)
1252 {
1253     switch (bit) {
1254     case 1 << OFPMBT13_DROP:          return "drop";
1255     case 1 << OFPMBT13_DSCP_REMARK:   return "dscp_remark";
1256     }
1257
1258     return NULL;
1259 }
1260
1261 static void
1262 ofp_print_meter_features_reply(struct ds *s, const struct ofp_header *oh)
1263 {
1264     struct ofputil_meter_features mf;
1265
1266     ofputil_decode_meter_features(oh, &mf);
1267
1268     ds_put_format(s, "\nmax_meter:%"PRIu32, mf.max_meters);
1269     ds_put_format(s, " max_bands:%"PRIu8, mf.max_bands);
1270     ds_put_format(s, " max_color:%"PRIu8"\n", mf.max_color);
1271
1272     ds_put_cstr(s, "band_types: ");
1273     ofp_print_bit_names(s, mf.band_types,
1274                         ofputil_meter_band_types_to_name, ' ');
1275     ds_put_char(s, '\n');
1276
1277     ds_put_cstr(s, "capabilities: ");
1278     ofp_print_bit_names(s, mf.capabilities,
1279                         ofputil_meter_capabilities_to_name, ' ');
1280     ds_put_char(s, '\n');
1281 }
1282
1283 static void
1284 ofp_print_meter_config_reply(struct ds *s, const struct ofp_header *oh)
1285 {
1286     struct ofpbuf bands;
1287     struct ofpbuf b;
1288
1289     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1290     ofpbuf_init(&bands, 64);
1291     for (;;) {
1292         struct ofputil_meter_config mc;
1293         int retval;
1294
1295         retval = ofputil_decode_meter_config(&b, &mc, &bands);
1296         if (retval) {
1297             if (retval != EOF) {
1298                 ofp_print_error(s, retval);
1299             }
1300             break;
1301         }
1302         ds_put_char(s, '\n');
1303         ofp_print_meter_config(s, &mc);
1304     }
1305     ofpbuf_uninit(&bands);
1306 }
1307
1308 static void
1309 ofp_print_meter_stats_reply(struct ds *s, const struct ofp_header *oh)
1310 {
1311     struct ofpbuf bands;
1312     struct ofpbuf b;
1313
1314     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1315     ofpbuf_init(&bands, 64);
1316     for (;;) {
1317         struct ofputil_meter_stats ms;
1318         int retval;
1319
1320         retval = ofputil_decode_meter_stats(&b, &ms, &bands);
1321         if (retval) {
1322             if (retval != EOF) {
1323                 ofp_print_error(s, retval);
1324             }
1325             break;
1326         }
1327         ds_put_char(s, '\n');
1328         ofp_print_meter_stats(s, &ms);
1329     }
1330     ofpbuf_uninit(&bands);
1331 }
1332
1333 static void
1334 ofp_print_error(struct ds *string, enum ofperr error)
1335 {
1336     if (string->length) {
1337         ds_put_char(string, ' ');
1338     }
1339     ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1340 }
1341
1342 static void
1343 ofp_print_hello(struct ds *string, const struct ofp_header *oh)
1344 {
1345     uint32_t allowed_versions;
1346     bool ok;
1347
1348     ok = ofputil_decode_hello(oh, &allowed_versions);
1349
1350     ds_put_cstr(string, "\n version bitmap: ");
1351     ofputil_format_version_bitmap(string, allowed_versions);
1352
1353     if (!ok) {
1354         ds_put_cstr(string, "\n unknown data in hello:\n");
1355         ds_put_hex_dump(string, oh, ntohs(oh->length), 0, true);
1356     }
1357 }
1358
1359 static void
1360 ofp_print_error_msg(struct ds *string, const struct ofp_header *oh)
1361 {
1362     size_t len = ntohs(oh->length);
1363     struct ofpbuf payload;
1364     enum ofperr error;
1365     char *s;
1366
1367     error = ofperr_decode_msg(oh, &payload);
1368     if (!error) {
1369         ds_put_cstr(string, "***decode error***");
1370         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1371         return;
1372     }
1373
1374     ds_put_format(string, " %s\n", ofperr_get_name(error));
1375
1376     if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
1377         ds_put_printable(string, ofpbuf_data(&payload), ofpbuf_size(&payload));
1378     } else {
1379         s = ofp_to_string(ofpbuf_data(&payload), ofpbuf_size(&payload), 1);
1380         ds_put_cstr(string, s);
1381         free(s);
1382     }
1383     ofpbuf_uninit(&payload);
1384 }
1385
1386 static void
1387 ofp_print_port_status(struct ds *string, const struct ofp_header *oh)
1388 {
1389     struct ofputil_port_status ps;
1390     enum ofperr error;
1391
1392     error = ofputil_decode_port_status(oh, &ps);
1393     if (error) {
1394         ofp_print_error(string, error);
1395         return;
1396     }
1397
1398     if (ps.reason == OFPPR_ADD) {
1399         ds_put_format(string, " ADD:");
1400     } else if (ps.reason == OFPPR_DELETE) {
1401         ds_put_format(string, " DEL:");
1402     } else if (ps.reason == OFPPR_MODIFY) {
1403         ds_put_format(string, " MOD:");
1404     }
1405
1406     ofp_print_phy_port(string, &ps.desc);
1407 }
1408
1409 static void
1410 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
1411 {
1412     const struct ofp_desc_stats *ods = ofpmsg_body(oh);
1413
1414     ds_put_char(string, '\n');
1415     ds_put_format(string, "Manufacturer: %.*s\n",
1416             (int) sizeof ods->mfr_desc, ods->mfr_desc);
1417     ds_put_format(string, "Hardware: %.*s\n",
1418             (int) sizeof ods->hw_desc, ods->hw_desc);
1419     ds_put_format(string, "Software: %.*s\n",
1420             (int) sizeof ods->sw_desc, ods->sw_desc);
1421     ds_put_format(string, "Serial Num: %.*s\n",
1422             (int) sizeof ods->serial_num, ods->serial_num);
1423     ds_put_format(string, "DP Description: %.*s\n",
1424             (int) sizeof ods->dp_desc, ods->dp_desc);
1425 }
1426
1427 static void
1428 ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
1429 {
1430     struct ofputil_flow_stats_request fsr;
1431     enum ofperr error;
1432
1433     error = ofputil_decode_flow_stats_request(&fsr, oh);
1434     if (error) {
1435         ofp_print_error(string, error);
1436         return;
1437     }
1438
1439     if (fsr.table_id != 0xff) {
1440         ds_put_format(string, " table=%"PRIu8, fsr.table_id);
1441     }
1442
1443     if (fsr.out_port != OFPP_ANY) {
1444         ds_put_cstr(string, " out_port=");
1445         ofputil_format_port(fsr.out_port, string);
1446     }
1447
1448     ds_put_char(string, ' ');
1449     match_format(&fsr.match, string, OFP_DEFAULT_PRIORITY);
1450 }
1451
1452 void
1453 ofp_print_flow_stats(struct ds *string, struct ofputil_flow_stats *fs)
1454 {
1455     ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1456                   ntohll(fs->cookie));
1457
1458     ofp_print_duration(string, fs->duration_sec, fs->duration_nsec);
1459     ds_put_format(string, ", table=%"PRIu8", ", fs->table_id);
1460     ds_put_format(string, "n_packets=%"PRIu64", ", fs->packet_count);
1461     ds_put_format(string, "n_bytes=%"PRIu64", ", fs->byte_count);
1462     if (fs->idle_timeout != OFP_FLOW_PERMANENT) {
1463         ds_put_format(string, "idle_timeout=%"PRIu16", ", fs->idle_timeout);
1464     }
1465     if (fs->hard_timeout != OFP_FLOW_PERMANENT) {
1466         ds_put_format(string, "hard_timeout=%"PRIu16", ", fs->hard_timeout);
1467     }
1468     if (fs->flags) {
1469         ofp_print_flow_flags(string, fs->flags);
1470     }
1471     if (fs->idle_age >= 0) {
1472         ds_put_format(string, "idle_age=%d, ", fs->idle_age);
1473     }
1474     if (fs->hard_age >= 0 && fs->hard_age != fs->duration_sec) {
1475         ds_put_format(string, "hard_age=%d, ", fs->hard_age);
1476     }
1477
1478     match_format(&fs->match, string, fs->priority);
1479     if (string->string[string->length - 1] != ' ') {
1480         ds_put_char(string, ' ');
1481     }
1482
1483     ds_put_cstr(string, "actions=");
1484     ofpacts_format(fs->ofpacts, fs->ofpacts_len, string);
1485 }
1486
1487 static void
1488 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1489 {
1490     struct ofpbuf ofpacts;
1491     struct ofpbuf b;
1492
1493     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1494     ofpbuf_init(&ofpacts, 64);
1495     for (;;) {
1496         struct ofputil_flow_stats fs;
1497         int retval;
1498
1499         retval = ofputil_decode_flow_stats_reply(&fs, &b, true, &ofpacts);
1500         if (retval) {
1501             if (retval != EOF) {
1502                 ds_put_cstr(string, " ***parse error***");
1503             }
1504             break;
1505         }
1506         ds_put_char(string, '\n');
1507         ofp_print_flow_stats(string, &fs);
1508      }
1509     ofpbuf_uninit(&ofpacts);
1510 }
1511
1512 static void
1513 ofp_print_aggregate_stats_reply(struct ds *string, const struct ofp_header *oh)
1514 {
1515     struct ofputil_aggregate_stats as;
1516     enum ofperr error;
1517
1518     error = ofputil_decode_aggregate_stats_reply(&as, oh);
1519     if (error) {
1520         ofp_print_error(string, error);
1521         return;
1522     }
1523
1524     ds_put_format(string, " packet_count=%"PRIu64, as.packet_count);
1525     ds_put_format(string, " byte_count=%"PRIu64, as.byte_count);
1526     ds_put_format(string, " flow_count=%"PRIu32, as.flow_count);
1527 }
1528
1529 static void
1530 print_port_stat(struct ds *string, const char *leader, uint64_t stat, int more)
1531 {
1532     ds_put_cstr(string, leader);
1533     if (stat != UINT64_MAX) {
1534         ds_put_format(string, "%"PRIu64, stat);
1535     } else {
1536         ds_put_char(string, '?');
1537     }
1538     if (more) {
1539         ds_put_cstr(string, ", ");
1540     } else {
1541         ds_put_cstr(string, "\n");
1542     }
1543 }
1544
1545 static void
1546 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1547 {
1548     ofp_port_t ofp10_port;
1549     enum ofperr error;
1550
1551     error = ofputil_decode_port_stats_request(oh, &ofp10_port);
1552     if (error) {
1553         ofp_print_error(string, error);
1554         return;
1555     }
1556
1557     ds_put_cstr(string, " port_no=");
1558     ofputil_format_port(ofp10_port, string);
1559 }
1560
1561 static void
1562 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1563                            int verbosity)
1564 {
1565     struct ofpbuf b;
1566
1567     ds_put_format(string, " %"PRIuSIZE" ports\n", ofputil_count_port_stats(oh));
1568     if (verbosity < 1) {
1569         return;
1570     }
1571
1572     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1573     for (;;) {
1574         struct ofputil_port_stats ps;
1575         int retval;
1576
1577         retval = ofputil_decode_port_stats(&ps, &b);
1578         if (retval) {
1579             if (retval != EOF) {
1580                 ds_put_cstr(string, " ***parse error***");
1581             }
1582             return;
1583         }
1584
1585         ds_put_cstr(string, "  port ");
1586         if (ofp_to_u16(ps.port_no) < 10) {
1587             ds_put_char(string, ' ');
1588         }
1589         ofputil_format_port(ps.port_no, string);
1590
1591         ds_put_cstr(string, ": rx ");
1592         print_port_stat(string, "pkts=", ps.stats.rx_packets, 1);
1593         print_port_stat(string, "bytes=", ps.stats.rx_bytes, 1);
1594         print_port_stat(string, "drop=", ps.stats.rx_dropped, 1);
1595         print_port_stat(string, "errs=", ps.stats.rx_errors, 1);
1596         print_port_stat(string, "frame=", ps.stats.rx_frame_errors, 1);
1597         print_port_stat(string, "over=", ps.stats.rx_over_errors, 1);
1598         print_port_stat(string, "crc=", ps.stats.rx_crc_errors, 0);
1599
1600         ds_put_cstr(string, "           tx ");
1601         print_port_stat(string, "pkts=", ps.stats.tx_packets, 1);
1602         print_port_stat(string, "bytes=", ps.stats.tx_bytes, 1);
1603         print_port_stat(string, "drop=", ps.stats.tx_dropped, 1);
1604         print_port_stat(string, "errs=", ps.stats.tx_errors, 1);
1605         print_port_stat(string, "coll=", ps.stats.collisions, 0);
1606
1607         if (ps.duration_sec != UINT32_MAX) {
1608             ds_put_cstr(string, "           duration=");
1609             ofp_print_duration(string, ps.duration_sec, ps.duration_nsec);
1610             ds_put_char(string, '\n');
1611         }
1612     }
1613 }
1614
1615 static void
1616 ofp_print_one_ofpst_table_reply(struct ds *string, enum ofp_version ofp_version,
1617                                 const char *name, struct ofp12_table_stats *ts)
1618 {
1619     char name_[OFP_MAX_TABLE_NAME_LEN + 1];
1620
1621     /* ofp13_table_stats is different */
1622     if (ofp_version > OFP12_VERSION) {
1623         return;
1624     }
1625
1626     ovs_strlcpy(name_, name, sizeof name_);
1627
1628     ds_put_format(string, "  %d: %-8s: ", ts->table_id, name_);
1629     ds_put_format(string, "wild=0x%05"PRIx64", ", ntohll(ts->wildcards));
1630     ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1631     ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1632     ds_put_cstr(string, "               ");
1633     ds_put_format(string, "lookup=%"PRIu64", ", ntohll(ts->lookup_count));
1634     ds_put_format(string, "matched=%"PRIu64"\n", ntohll(ts->matched_count));
1635
1636     if (ofp_version < OFP11_VERSION) {
1637         return;
1638     }
1639
1640     ds_put_cstr(string, "               ");
1641     ds_put_format(string, "match=0x%08"PRIx64", ", ntohll(ts->match));
1642     ds_put_format(string, "instructions=0x%08"PRIx32", ",
1643                   ntohl(ts->instructions));
1644     ds_put_format(string, "config=0x%08"PRIx32"\n", ntohl(ts->config));
1645     ds_put_cstr(string, "               ");
1646     ds_put_format(string, "write_actions=0x%08"PRIx32", ",
1647                   ntohl(ts->write_actions));
1648     ds_put_format(string, "apply_actions=0x%08"PRIx32"\n",
1649                   ntohl(ts->apply_actions));
1650
1651     if (ofp_version < OFP12_VERSION) {
1652         return;
1653     }
1654
1655     ds_put_cstr(string, "               ");
1656     ds_put_format(string, "write_setfields=0x%016"PRIx64"\n",
1657                   ntohll(ts->write_setfields));
1658     ds_put_cstr(string, "               ");
1659     ds_put_format(string, "apply_setfields=0x%016"PRIx64"\n",
1660                   ntohll(ts->apply_setfields));
1661     ds_put_cstr(string, "               ");
1662     ds_put_format(string, "metadata_match=0x%016"PRIx64"\n",
1663                   ntohll(ts->metadata_match));
1664     ds_put_cstr(string, "               ");
1665     ds_put_format(string, "metadata_write=0x%016"PRIx64"\n",
1666                   ntohll(ts->metadata_write));
1667 }
1668
1669 static void
1670 ofp_print_ofpst_table_reply13(struct ds *string, const struct ofp_header *oh,
1671                               int verbosity)
1672 {
1673     struct ofp13_table_stats *ts;
1674     struct ofpbuf b;
1675     size_t n;
1676
1677     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1678     ofpraw_pull_assert(&b);
1679
1680     n = ofpbuf_size(&b) / sizeof *ts;
1681     ds_put_format(string, " %"PRIuSIZE" tables\n", n);
1682     if (verbosity < 1) {
1683         return;
1684     }
1685
1686     for (;;) {
1687         ts = ofpbuf_try_pull(&b, sizeof *ts);
1688         if (!ts) {
1689             return;
1690         }
1691         ds_put_format(string,
1692                       "  %d: active=%"PRIu32", lookup=%"PRIu64  \
1693                       ", matched=%"PRIu64"\n",
1694                       ts->table_id, ntohl(ts->active_count),
1695                       ntohll(ts->lookup_count), ntohll(ts->matched_count));
1696     }
1697 }
1698
1699 static void
1700 ofp_print_ofpst_table_reply12(struct ds *string, const struct ofp_header *oh,
1701                               int verbosity)
1702 {
1703     struct ofp12_table_stats *ts;
1704     struct ofpbuf b;
1705     size_t n;
1706
1707     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1708     ofpraw_pull_assert(&b);
1709
1710     n = ofpbuf_size(&b) / sizeof *ts;
1711     ds_put_format(string, " %"PRIuSIZE" tables\n", n);
1712     if (verbosity < 1) {
1713         return;
1714     }
1715
1716     for (;;) {
1717         ts = ofpbuf_try_pull(&b, sizeof *ts);
1718         if (!ts) {
1719             return;
1720         }
1721
1722         ofp_print_one_ofpst_table_reply(string, OFP12_VERSION, ts->name, ts);
1723      }
1724 }
1725
1726 static void
1727 ofp_print_ofpst_table_reply11(struct ds *string, const struct ofp_header *oh,
1728                               int verbosity)
1729 {
1730     struct ofp11_table_stats *ts;
1731     struct ofpbuf b;
1732     size_t n;
1733
1734     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1735     ofpraw_pull_assert(&b);
1736
1737     n = ofpbuf_size(&b) / sizeof *ts;
1738     ds_put_format(string, " %"PRIuSIZE" tables\n", n);
1739     if (verbosity < 1) {
1740         return;
1741     }
1742
1743     for (;;) {
1744         struct ofp12_table_stats ts12;
1745
1746         ts = ofpbuf_try_pull(&b, sizeof *ts);
1747         if (!ts) {
1748             return;
1749         }
1750
1751         ts12.table_id = ts->table_id;
1752         ts12.wildcards = htonll(ntohl(ts->wildcards));
1753         ts12.max_entries = ts->max_entries;
1754         ts12.active_count = ts->active_count;
1755         ts12.lookup_count = ts->lookup_count;
1756         ts12.matched_count = ts->matched_count;
1757         ts12.match = htonll(ntohl(ts->match));
1758         ts12.instructions = ts->instructions;
1759         ts12.config = ts->config;
1760         ts12.write_actions = ts->write_actions;
1761         ts12.apply_actions = ts->apply_actions;
1762         ofp_print_one_ofpst_table_reply(string, OFP11_VERSION, ts->name, &ts12);
1763      }
1764 }
1765
1766 static void
1767 ofp_print_ofpst_table_reply10(struct ds *string, const struct ofp_header *oh,
1768                               int verbosity)
1769 {
1770     struct ofp10_table_stats *ts;
1771     struct ofpbuf b;
1772     size_t n;
1773
1774     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1775     ofpraw_pull_assert(&b);
1776
1777     n = ofpbuf_size(&b) / sizeof *ts;
1778     ds_put_format(string, " %"PRIuSIZE" tables\n", n);
1779     if (verbosity < 1) {
1780         return;
1781     }
1782
1783     for (;;) {
1784         struct ofp12_table_stats ts12;
1785
1786         ts = ofpbuf_try_pull(&b, sizeof *ts);
1787         if (!ts) {
1788             return;
1789         }
1790
1791         ts12.table_id = ts->table_id;
1792         ts12.wildcards = htonll(ntohl(ts->wildcards));
1793         ts12.max_entries = ts->max_entries;
1794         ts12.active_count = ts->active_count;
1795         ts12.lookup_count = get_32aligned_be64(&ts->lookup_count);
1796         ts12.matched_count = get_32aligned_be64(&ts->matched_count);
1797         ofp_print_one_ofpst_table_reply(string, OFP10_VERSION, ts->name, &ts12);
1798      }
1799 }
1800
1801 static void
1802 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1803                             int verbosity)
1804 {
1805     switch ((enum ofp_version)oh->version) {
1806     case OFP15_VERSION:
1807     case OFP14_VERSION:
1808     case OFP13_VERSION:
1809         ofp_print_ofpst_table_reply13(string, oh, verbosity);
1810         break;
1811
1812     case OFP12_VERSION:
1813         ofp_print_ofpst_table_reply12(string, oh, verbosity);
1814         break;
1815
1816     case OFP11_VERSION:
1817         ofp_print_ofpst_table_reply11(string, oh, verbosity);
1818         break;
1819
1820     case OFP10_VERSION:
1821         ofp_print_ofpst_table_reply10(string, oh, verbosity);
1822         break;
1823
1824     default:
1825         OVS_NOT_REACHED();
1826     }
1827 }
1828
1829 static void
1830 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1831 {
1832     if (queue_id == OFPQ_ALL) {
1833         ds_put_cstr(string, "ALL");
1834     } else {
1835         ds_put_format(string, "%"PRIu32, queue_id);
1836     }
1837 }
1838
1839 static void
1840 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1841 {
1842     struct ofputil_queue_stats_request oqsr;
1843     enum ofperr error;
1844
1845     error = ofputil_decode_queue_stats_request(oh, &oqsr);
1846     if (error) {
1847         ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1848         return;
1849     }
1850
1851     ds_put_cstr(string, "port=");
1852     ofputil_format_port(oqsr.port_no, string);
1853
1854     ds_put_cstr(string, " queue=");
1855     ofp_print_queue_name(string, oqsr.queue_id);
1856 }
1857
1858 static void
1859 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1860                             int verbosity)
1861 {
1862     struct ofpbuf b;
1863
1864     ds_put_format(string, " %"PRIuSIZE" queues\n", ofputil_count_queue_stats(oh));
1865     if (verbosity < 1) {
1866         return;
1867     }
1868
1869     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1870     for (;;) {
1871         struct ofputil_queue_stats qs;
1872         int retval;
1873
1874         retval = ofputil_decode_queue_stats(&qs, &b);
1875         if (retval) {
1876             if (retval != EOF) {
1877                 ds_put_cstr(string, " ***parse error***");
1878             }
1879             return;
1880         }
1881
1882         ds_put_cstr(string, "  port ");
1883         ofputil_format_port(qs.port_no, string);
1884         ds_put_cstr(string, " queue ");
1885         ofp_print_queue_name(string, qs.queue_id);
1886         ds_put_cstr(string, ": ");
1887
1888         print_port_stat(string, "bytes=", qs.tx_bytes, 1);
1889         print_port_stat(string, "pkts=", qs.tx_packets, 1);
1890         print_port_stat(string, "errors=", qs.tx_errors, 1);
1891
1892         ds_put_cstr(string, "duration=");
1893         if (qs.duration_sec != UINT32_MAX) {
1894             ofp_print_duration(string, qs.duration_sec, qs.duration_nsec);
1895         } else {
1896             ds_put_char(string, '?');
1897         }
1898         ds_put_char(string, '\n');
1899     }
1900 }
1901
1902 static void
1903 ofp_print_ofpst_port_desc_request(struct ds *string,
1904                                   const struct ofp_header *oh)
1905 {
1906     enum ofperr error;
1907     ofp_port_t port;
1908
1909     error = ofputil_decode_port_desc_stats_request(oh, &port);
1910     if (error) {
1911         ofp_print_error(string, error);
1912         return;
1913     }
1914
1915     ds_put_cstr(string, " port=");
1916     ofputil_format_port(port, string);
1917 }
1918
1919 static void
1920 ofp_print_ofpst_port_desc_reply(struct ds *string,
1921                                 const struct ofp_header *oh)
1922 {
1923     struct ofpbuf b;
1924
1925     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1926     ofpraw_pull_assert(&b);
1927     ds_put_char(string, '\n');
1928     ofp_print_phy_ports(string, oh->version, &b);
1929 }
1930
1931 static void
1932 ofp_print_stats(struct ds *string, const struct ofp_header *oh)
1933 {
1934     uint16_t flags = ofpmp_flags(oh);
1935
1936     if (flags) {
1937         ds_put_cstr(string, " flags=");
1938         if ((!ofpmsg_is_stat_request(oh) || oh->version >= OFP13_VERSION)
1939             && (flags & OFPSF_REPLY_MORE)) {
1940             ds_put_cstr(string, "[more]");
1941             flags &= ~OFPSF_REPLY_MORE;
1942         }
1943         if (flags) {
1944             ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1945                           flags);
1946         }
1947     }
1948 }
1949
1950 static void
1951 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1952 {
1953     size_t len = ntohs(oh->length);
1954
1955     ds_put_format(string, " %"PRIuSIZE" bytes of payload\n", len - sizeof *oh);
1956     if (verbosity > 1) {
1957         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1958     }
1959 }
1960
1961 static void
1962 ofp_print_role_generic(struct ds *string, enum ofp12_controller_role role,
1963                        uint64_t generation_id)
1964 {
1965     ds_put_cstr(string, " role=");
1966
1967     switch (role) {
1968     case OFPCR12_ROLE_NOCHANGE:
1969         ds_put_cstr(string, "nochange");
1970         break;
1971     case OFPCR12_ROLE_EQUAL:
1972         ds_put_cstr(string, "equal"); /* OF 1.2 wording */
1973         break;
1974     case OFPCR12_ROLE_MASTER:
1975         ds_put_cstr(string, "master");
1976         break;
1977     case OFPCR12_ROLE_SLAVE:
1978         ds_put_cstr(string, "slave");
1979         break;
1980     default:
1981         OVS_NOT_REACHED();
1982     }
1983
1984     if (generation_id != UINT64_MAX) {
1985         ds_put_format(string, " generation_id=%"PRIu64, generation_id);
1986     }
1987 }
1988
1989 static void
1990 ofp_print_role_message(struct ds *string, const struct ofp_header *oh)
1991 {
1992     struct ofputil_role_request rr;
1993     enum ofperr error;
1994
1995     error = ofputil_decode_role_message(oh, &rr);
1996     if (error) {
1997         ofp_print_error(string, error);
1998         return;
1999     }
2000
2001     ofp_print_role_generic(string, rr.role, rr.have_generation_id ? rr.generation_id : UINT64_MAX);
2002 }
2003
2004 static void
2005 ofp_print_role_status_message(struct ds *string, const struct ofp_header *oh)
2006 {
2007     struct ofputil_role_status rs;
2008     enum ofperr error;
2009
2010     error = ofputil_decode_role_status(oh, &rs);
2011     if (error) {
2012         ofp_print_error(string, error);
2013         return;
2014     }
2015
2016     ofp_print_role_generic(string, rs.role, rs.generation_id);
2017
2018     ds_put_cstr(string, " reason=");
2019
2020     switch (rs.reason) {
2021     case OFPCRR_MASTER_REQUEST:
2022         ds_put_cstr(string, "master_request");
2023         break;
2024     case OFPCRR_CONFIG:
2025         ds_put_cstr(string, "configuration_changed");
2026         break;
2027     case OFPCRR_EXPERIMENTER:
2028         ds_put_cstr(string, "experimenter_data_changed");
2029         break;
2030     default:
2031         OVS_NOT_REACHED();
2032     }
2033 }
2034
2035 static void
2036 ofp_print_nxt_flow_mod_table_id(struct ds *string,
2037                                 const struct nx_flow_mod_table_id *nfmti)
2038 {
2039     ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
2040 }
2041
2042 static void
2043 ofp_print_nxt_set_flow_format(struct ds *string,
2044                               const struct nx_set_flow_format *nsff)
2045 {
2046     uint32_t format = ntohl(nsff->format);
2047
2048     ds_put_cstr(string, " format=");
2049     if (ofputil_nx_flow_format_is_valid(format)) {
2050         ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
2051     } else {
2052         ds_put_format(string, "%"PRIu32, format);
2053     }
2054 }
2055
2056 static void
2057 ofp_print_nxt_set_packet_in_format(struct ds *string,
2058                                    const struct nx_set_packet_in_format *nspf)
2059 {
2060     uint32_t format = ntohl(nspf->format);
2061
2062     ds_put_cstr(string, " format=");
2063     if (ofputil_packet_in_format_is_valid(format)) {
2064         ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
2065     } else {
2066         ds_put_format(string, "%"PRIu32, format);
2067     }
2068 }
2069
2070 /* Returns a string form of 'reason'.  The return value is either a statically
2071  * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
2072  * 'bufsize' should be at least OFP_PORT_REASON_BUFSIZE. */
2073 #define OFP_PORT_REASON_BUFSIZE (INT_STRLEN(int) + 1)
2074 static const char *
2075 ofp_port_reason_to_string(enum ofp_port_reason reason,
2076                           char *reasonbuf, size_t bufsize)
2077 {
2078     switch (reason) {
2079     case OFPPR_ADD:
2080         return "add";
2081
2082     case OFPPR_DELETE:
2083         return "delete";
2084
2085     case OFPPR_MODIFY:
2086         return "modify";
2087
2088     default:
2089         snprintf(reasonbuf, bufsize, "%d", (int) reason);
2090         return reasonbuf;
2091     }
2092 }
2093
2094 static void
2095 ofp_print_nxt_set_async_config(struct ds *string,
2096                                const struct nx_async_config *nac)
2097 {
2098     int i;
2099
2100     for (i = 0; i < 2; i++) {
2101         int j;
2102
2103         ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
2104
2105         ds_put_cstr(string, "       PACKET_IN:");
2106         for (j = 0; j < 32; j++) {
2107             if (nac->packet_in_mask[i] & htonl(1u << j)) {
2108                 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
2109                 const char *reason;
2110
2111                 reason = ofputil_packet_in_reason_to_string(j, reasonbuf,
2112                                                             sizeof reasonbuf);
2113                 ds_put_format(string, " %s", reason);
2114             }
2115         }
2116         if (!nac->packet_in_mask[i]) {
2117             ds_put_cstr(string, " (off)");
2118         }
2119         ds_put_char(string, '\n');
2120
2121         ds_put_cstr(string, "     PORT_STATUS:");
2122         for (j = 0; j < 32; j++) {
2123             if (nac->port_status_mask[i] & htonl(1u << j)) {
2124                 char reasonbuf[OFP_PORT_REASON_BUFSIZE];
2125                 const char *reason;
2126
2127                 reason = ofp_port_reason_to_string(j, reasonbuf,
2128                                                    sizeof reasonbuf);
2129                 ds_put_format(string, " %s", reason);
2130             }
2131         }
2132         if (!nac->port_status_mask[i]) {
2133             ds_put_cstr(string, " (off)");
2134         }
2135         ds_put_char(string, '\n');
2136
2137         ds_put_cstr(string, "    FLOW_REMOVED:");
2138         for (j = 0; j < 32; j++) {
2139             if (nac->flow_removed_mask[i] & htonl(1u << j)) {
2140                 char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
2141                 const char *reason;
2142
2143                 reason = ofp_flow_removed_reason_to_string(j, reasonbuf,
2144                                                            sizeof reasonbuf);
2145                 ds_put_format(string, " %s", reason);
2146             }
2147         }
2148         if (!nac->flow_removed_mask[i]) {
2149             ds_put_cstr(string, " (off)");
2150         }
2151         ds_put_char(string, '\n');
2152     }
2153 }
2154
2155 static void
2156 ofp_print_nxt_set_controller_id(struct ds *string,
2157                                 const struct nx_controller_id *nci)
2158 {
2159     ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
2160 }
2161
2162 static void
2163 ofp_print_nxt_flow_monitor_cancel(struct ds *string,
2164                                   const struct ofp_header *oh)
2165 {
2166     ds_put_format(string, " id=%"PRIu32,
2167                   ofputil_decode_flow_monitor_cancel(oh));
2168 }
2169
2170 static const char *
2171 nx_flow_monitor_flags_to_name(uint32_t bit)
2172 {
2173     enum nx_flow_monitor_flags fmf = bit;
2174
2175     switch (fmf) {
2176     case NXFMF_INITIAL: return "initial";
2177     case NXFMF_ADD: return "add";
2178     case NXFMF_DELETE: return "delete";
2179     case NXFMF_MODIFY: return "modify";
2180     case NXFMF_ACTIONS: return "actions";
2181     case NXFMF_OWN: return "own";
2182     }
2183
2184     return NULL;
2185 }
2186
2187 static void
2188 ofp_print_nxst_flow_monitor_request(struct ds *string,
2189                                     const struct ofp_header *oh)
2190 {
2191     struct ofpbuf b;
2192
2193     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2194     for (;;) {
2195         struct ofputil_flow_monitor_request request;
2196         int retval;
2197
2198         retval = ofputil_decode_flow_monitor_request(&request, &b);
2199         if (retval) {
2200             if (retval != EOF) {
2201                 ofp_print_error(string, retval);
2202             }
2203             return;
2204         }
2205
2206         ds_put_format(string, "\n id=%"PRIu32" flags=", request.id);
2207         ofp_print_bit_names(string, request.flags,
2208                             nx_flow_monitor_flags_to_name, ',');
2209
2210         if (request.out_port != OFPP_NONE) {
2211             ds_put_cstr(string, " out_port=");
2212             ofputil_format_port(request.out_port, string);
2213         }
2214
2215         if (request.table_id != 0xff) {
2216             ds_put_format(string, " table=%"PRIu8, request.table_id);
2217         }
2218
2219         ds_put_char(string, ' ');
2220         match_format(&request.match, string, OFP_DEFAULT_PRIORITY);
2221         ds_chomp(string, ' ');
2222     }
2223 }
2224
2225 static void
2226 ofp_print_nxst_flow_monitor_reply(struct ds *string,
2227                                   const struct ofp_header *oh)
2228 {
2229     uint64_t ofpacts_stub[1024 / 8];
2230     struct ofpbuf ofpacts;
2231     struct ofpbuf b;
2232
2233     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2234     ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2235     for (;;) {
2236         char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
2237         struct ofputil_flow_update update;
2238         struct match match;
2239         int retval;
2240
2241         update.match = &match;
2242         retval = ofputil_decode_flow_update(&update, &b, &ofpacts);
2243         if (retval) {
2244             if (retval != EOF) {
2245                 ofp_print_error(string, retval);
2246             }
2247             ofpbuf_uninit(&ofpacts);
2248             return;
2249         }
2250
2251         ds_put_cstr(string, "\n event=");
2252         switch (update.event) {
2253         case NXFME_ADDED:
2254             ds_put_cstr(string, "ADDED");
2255             break;
2256
2257         case NXFME_DELETED:
2258             ds_put_format(string, "DELETED reason=%s",
2259                           ofp_flow_removed_reason_to_string(update.reason,
2260                                                             reasonbuf,
2261                                                             sizeof reasonbuf));
2262             break;
2263
2264         case NXFME_MODIFIED:
2265             ds_put_cstr(string, "MODIFIED");
2266             break;
2267
2268         case NXFME_ABBREV:
2269             ds_put_format(string, "ABBREV xid=0x%"PRIx32, ntohl(update.xid));
2270             continue;
2271         }
2272
2273         ds_put_format(string, " table=%"PRIu8, update.table_id);
2274         if (update.idle_timeout != OFP_FLOW_PERMANENT) {
2275             ds_put_format(string, " idle_timeout=%"PRIu16,
2276                           update.idle_timeout);
2277         }
2278         if (update.hard_timeout != OFP_FLOW_PERMANENT) {
2279             ds_put_format(string, " hard_timeout=%"PRIu16,
2280                           update.hard_timeout);
2281         }
2282         ds_put_format(string, " cookie=%#"PRIx64, ntohll(update.cookie));
2283
2284         ds_put_char(string, ' ');
2285         match_format(update.match, string, OFP_DEFAULT_PRIORITY);
2286
2287         if (update.ofpacts_len) {
2288             if (string->string[string->length - 1] != ' ') {
2289                 ds_put_char(string, ' ');
2290             }
2291             ds_put_cstr(string, "actions=");
2292             ofpacts_format(update.ofpacts, update.ofpacts_len, string);
2293         }
2294     }
2295 }
2296
2297 void
2298 ofp_print_version(const struct ofp_header *oh,
2299                   struct ds *string)
2300 {
2301     switch (oh->version) {
2302     case OFP10_VERSION:
2303         break;
2304     case OFP11_VERSION:
2305         ds_put_cstr(string, " (OF1.1)");
2306         break;
2307     case OFP12_VERSION:
2308         ds_put_cstr(string, " (OF1.2)");
2309         break;
2310     case OFP13_VERSION:
2311         ds_put_cstr(string, " (OF1.3)");
2312         break;
2313     case OFP14_VERSION:
2314         ds_put_cstr(string, " (OF1.4)");
2315         break;
2316     case OFP15_VERSION:
2317         ds_put_cstr(string, " (OF1.5)");
2318         break;
2319     default:
2320         ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
2321         break;
2322     }
2323     ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
2324 }
2325
2326 static void
2327 ofp_header_to_string__(const struct ofp_header *oh, enum ofpraw raw,
2328                        struct ds *string)
2329 {
2330     ds_put_cstr(string, ofpraw_get_name(raw));
2331     ofp_print_version(oh, string);
2332 }
2333
2334 static void
2335 ofp_print_group(struct ds *s, uint32_t group_id, uint8_t type,
2336                 struct list *p_buckets)
2337 {
2338     static const char *type_str[] = { "all", "select", "indirect",
2339                                       "ff", "unknown" };
2340     struct ofputil_bucket *bucket;
2341
2342     ds_put_format(s, "group_id=%"PRIu32",type=%s",
2343                   group_id, type_str[type > 4 ? 4 : type]);
2344     if (!p_buckets) {
2345         return;
2346     }
2347
2348     LIST_FOR_EACH (bucket, list_node, p_buckets) {
2349         ds_put_cstr(s, ",bucket=");
2350
2351         if (bucket->weight != 1) {
2352             ds_put_format(s, "weight:%"PRIu16",", bucket->weight);
2353         }
2354         if (bucket->watch_port != OFPP_NONE) {
2355             ds_put_format(s, "watch_port:%"PRIu32",", bucket->watch_port);
2356         }
2357         if (bucket->watch_group != OFPG11_ANY) {
2358             ds_put_format(s, "watch_group:%"PRIu32",", bucket->watch_group);
2359         }
2360
2361         ds_put_cstr(s, "actions=");
2362         ofpacts_format(bucket->ofpacts, bucket->ofpacts_len, s);
2363     }
2364 }
2365
2366 static void
2367 ofp_print_ofpst_group_desc_request(struct ds *string,
2368                                    const struct ofp_header *oh)
2369 {
2370     uint32_t group_id = ofputil_decode_group_desc_request(oh);
2371     ds_put_cstr(string, " group_id=");
2372     ofputil_format_group(group_id, string);
2373 }
2374
2375 static void
2376 ofp_print_group_desc(struct ds *s, const struct ofp_header *oh)
2377 {
2378     struct ofpbuf b;
2379
2380     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2381     for (;;) {
2382         struct ofputil_group_desc gd;
2383         int retval;
2384
2385         retval = ofputil_decode_group_desc_reply(&gd, &b, oh->version);
2386         if (retval) {
2387             if (retval != EOF) {
2388                 ds_put_cstr(s, " ***parse error***");
2389             }
2390             break;
2391         }
2392
2393         ds_put_char(s, '\n');
2394         ds_put_char(s, ' ');
2395         ofp_print_group(s, gd.group_id, gd.type, &gd.buckets);
2396      }
2397 }
2398
2399 static void
2400 ofp_print_ofpst_group_request(struct ds *string, const struct ofp_header *oh)
2401 {
2402     enum ofperr error;
2403     uint32_t group_id;
2404
2405     error = ofputil_decode_group_stats_request(oh, &group_id);
2406     if (error) {
2407         ofp_print_error(string, error);
2408         return;
2409     }
2410
2411     ds_put_cstr(string, " group_id=");
2412     ofputil_format_group(group_id, string);
2413 }
2414
2415 static void
2416 ofp_print_group_stats(struct ds *s, const struct ofp_header *oh)
2417 {
2418     struct ofpbuf b;
2419     uint32_t bucket_i;
2420
2421     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2422
2423     for (;;) {
2424         struct ofputil_group_stats gs;
2425         int retval;
2426
2427         retval = ofputil_decode_group_stats_reply(&b, &gs);
2428         if (retval) {
2429             if (retval != EOF) {
2430                 ds_put_cstr(s, " ***parse error***");
2431             }
2432             break;
2433         }
2434
2435         ds_put_char(s, '\n');
2436
2437         ds_put_char(s, ' ');
2438         ds_put_format(s, "group_id=%"PRIu32",", gs.group_id);
2439
2440         if (gs.duration_sec != UINT32_MAX) {
2441             ds_put_cstr(s, "duration=");
2442             ofp_print_duration(s, gs.duration_sec, gs.duration_nsec);
2443             ds_put_char(s, ',');
2444         }
2445         ds_put_format(s, "ref_count=%"PRIu32",", gs.ref_count);
2446         ds_put_format(s, "packet_count=%"PRIu64",", gs.packet_count);
2447         ds_put_format(s, "byte_count=%"PRIu64"", gs.byte_count);
2448
2449         for (bucket_i = 0; bucket_i < gs.n_buckets; bucket_i++) {
2450             if (gs.bucket_stats[bucket_i].packet_count != UINT64_MAX) {
2451                 ds_put_format(s, ",bucket%"PRIu32":", bucket_i);
2452                 ds_put_format(s, "packet_count=%"PRIu64",", gs.bucket_stats[bucket_i].packet_count);
2453                 ds_put_format(s, "byte_count=%"PRIu64"", gs.bucket_stats[bucket_i].byte_count);
2454             }
2455         }
2456
2457         free(gs.bucket_stats);
2458      }
2459 }
2460
2461 static void
2462 ofp_print_group_features(struct ds *string, const struct ofp_header *oh)
2463 {
2464     struct ofputil_group_features features;
2465
2466     ofputil_decode_group_features_reply(oh, &features);
2467
2468     ds_put_format(string, "\n Group table:\n");
2469     ds_put_format(string, "    Types:  0x%"PRIx32"\n", features.types);
2470     ds_put_format(string, "    Capabilities:  0x%"PRIx32"\n",
2471                   features.capabilities);
2472
2473     if (features.types & (1u << OFPGT11_ALL)) {
2474         ds_put_format(string, "    All group :\n");
2475         ds_put_format(string,
2476                       "        max_groups = %#"PRIx32" actions=0x%08"PRIx32"\n",
2477                       features.max_groups[0], features.actions[0]);
2478     }
2479
2480     if (features.types & (1u << OFPGT11_SELECT)) {
2481         ds_put_format(string, "    Select group :\n");
2482         ds_put_format(string, "        max_groups = %#"PRIx32" "
2483                       "actions=0x%08"PRIx32"\n",
2484                       features.max_groups[1], features.actions[1]);
2485     }
2486
2487     if (features.types & (1u << OFPGT11_INDIRECT)) {
2488         ds_put_format(string, "    Indirect group :\n");
2489         ds_put_format(string, "        max_groups = %#"PRIx32" "
2490                       "actions=0x%08"PRIx32"\n",
2491                       features.max_groups[2], features.actions[2]);
2492     }
2493
2494     if (features.types & (1u << OFPGT11_FF)) {
2495         ds_put_format(string, "    Fast Failover group :\n");
2496         ds_put_format(string, "        max_groups = %#"PRIx32" "
2497                       "actions=0x%08"PRIx32"\n",
2498                       features.max_groups[3], features.actions[3]);
2499     }
2500 }
2501
2502 static void
2503 ofp_print_group_mod(struct ds *s, const struct ofp_header *oh)
2504 {
2505     struct ofputil_group_mod gm;
2506     int error;
2507
2508     error = ofputil_decode_group_mod(oh, &gm);
2509     if (error) {
2510         ofp_print_error(s, error);
2511         return;
2512     }
2513
2514     ds_put_char(s, '\n');
2515
2516     ds_put_char(s, ' ');
2517     switch (gm.command) {
2518     case OFPGC11_ADD:
2519         ds_put_cstr(s, "ADD");
2520         break;
2521
2522     case OFPGC11_MODIFY:
2523         ds_put_cstr(s, "MOD");
2524         break;
2525
2526     case OFPGC11_DELETE:
2527         ds_put_cstr(s, "DEL");
2528         break;
2529
2530     default:
2531         ds_put_format(s, "cmd:%"PRIu16"", gm.command);
2532     }
2533     ds_put_char(s, ' ');
2534
2535     ofp_print_group(s, gm.group_id, gm.type, &gm.buckets);
2536 }
2537
2538 static const char *
2539 ofp13_action_to_string(uint32_t bit)
2540 {
2541     switch (bit) {
2542 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)  \
2543         case 1u << ENUM: return NAME;
2544 #include "ofp-util.def"
2545     }
2546     return NULL;
2547 }
2548
2549 static void
2550 print_table_action_features(struct ds *s,
2551                             const struct ofputil_table_action_features *taf)
2552 {
2553     ds_put_cstr(s, "        actions: ");
2554     ofp_print_bit_names(s, taf->actions, ofp13_action_to_string, ',');
2555     ds_put_char(s, '\n');
2556
2557     ds_put_cstr(s, "        supported on Set-Field: ");
2558     if (!bitmap_is_all_zeros(taf->set_fields.bm, MFF_N_IDS)) {
2559         int i;
2560
2561         BITMAP_FOR_EACH_1 (i, MFF_N_IDS, taf->set_fields.bm) {
2562             ds_put_format(s, "%s,", mf_from_id(i)->name);
2563         }
2564         ds_chomp(s, ',');
2565     } else {
2566         ds_put_cstr(s, "none");
2567     }
2568     ds_put_char(s, '\n');
2569 }
2570
2571 static bool
2572 table_action_features_equal(const struct ofputil_table_action_features *a,
2573                             const struct ofputil_table_action_features *b)
2574 {
2575     return (a->actions == b->actions
2576             && bitmap_equal(a->set_fields.bm, b->set_fields.bm, MFF_N_IDS));
2577 }
2578
2579 static void
2580 print_table_instruction_features(
2581     struct ds *s, const struct ofputil_table_instruction_features *tif)
2582 {
2583     int start, end;
2584
2585     ds_put_cstr(s, "      next tables: ");
2586     for (start = bitmap_scan(tif->next, 1, 0, 255); start < 255;
2587          start = bitmap_scan(tif->next, 1, end, 255)) {
2588         end = bitmap_scan(tif->next, 0, start + 1, 255);
2589         if (end == start + 1) {
2590             ds_put_format(s, "%d,", start);
2591         } else {
2592             ds_put_format(s, "%d-%d,", start, end - 1);
2593         }
2594     }
2595     ds_chomp(s, ',');
2596     if (ds_last(s) == ' ') {
2597         ds_put_cstr(s, "none");
2598     }
2599     ds_put_char(s, '\n');
2600
2601     ds_put_cstr(s, "      instructions: ");
2602     if (tif->instructions) {
2603         int i;
2604
2605         for (i = 0; i < 32; i++) {
2606             if (tif->instructions & (1u << i)) {
2607                 ds_put_format(s, "%s,", ovs_instruction_name_from_type(i));
2608             }
2609         }
2610         ds_chomp(s, ',');
2611     } else {
2612         ds_put_cstr(s, "none");
2613     }
2614     ds_put_char(s, '\n');
2615
2616     if (table_action_features_equal(&tif->write, &tif->apply)) {
2617         ds_put_cstr(s, "      Write-Actions and Apply-Actions features:\n");
2618         print_table_action_features(s, &tif->write);
2619     } else {
2620         ds_put_cstr(s, "      Write-Actions features:\n");
2621         print_table_action_features(s, &tif->write);
2622         ds_put_cstr(s, "      Apply-Actions features:\n");
2623         print_table_action_features(s, &tif->apply);
2624     }
2625 }
2626
2627 static bool
2628 table_instruction_features_equal(
2629     const struct ofputil_table_instruction_features *a,
2630     const struct ofputil_table_instruction_features *b)
2631 {
2632     return (bitmap_equal(a->next, b->next, 255)
2633             && a->instructions == b->instructions
2634             && table_action_features_equal(&a->write, &b->write)
2635             && table_action_features_equal(&a->apply, &b->apply));
2636 }
2637
2638 static void
2639 ofp_print_table_features(struct ds *s, const struct ofp_header *oh)
2640 {
2641     struct ofpbuf b;
2642
2643     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2644
2645     for (;;) {
2646         struct ofputil_table_features tf;
2647         int retval;
2648         int i;
2649
2650         retval = ofputil_decode_table_features(&b, &tf, true);
2651         if (retval) {
2652             if (retval != EOF) {
2653                 ofp_print_error(s, retval);
2654             }
2655             return;
2656         }
2657
2658         ds_put_format(s, "\n  table %"PRIu8":\n", tf.table_id);
2659         ds_put_format(s, "    name=\"%s\"\n", tf.name);
2660         ds_put_format(s, "    metadata: match=%#"PRIx64" write=%#"PRIx64"\n",
2661                       ntohll(tf.metadata_match), ntohll(tf.metadata_write));
2662
2663         ds_put_cstr(s, "    config=");
2664         ofp_print_table_miss_config(s, tf.config);
2665
2666         ds_put_format(s, "    max_entries=%"PRIu32"\n", tf.max_entries);
2667
2668         if (table_instruction_features_equal(&tf.nonmiss, &tf.miss)) {
2669             ds_put_cstr(s, "    instructions (table miss and others):\n");
2670             print_table_instruction_features(s, &tf.nonmiss);
2671         } else {
2672             ds_put_cstr(s, "    instructions (other than table miss):\n");
2673             print_table_instruction_features(s, &tf.nonmiss);
2674             ds_put_cstr(s, "    instructions (table miss):\n");
2675             print_table_instruction_features(s, &tf.miss);
2676         }
2677
2678         ds_put_cstr(s, "    matching:\n");
2679         BITMAP_FOR_EACH_1 (i, MFF_N_IDS, tf.match.bm) {
2680             const struct mf_field *f = mf_from_id(i);
2681             bool mask = bitmap_is_set(tf.mask.bm, i);
2682             bool wildcard = bitmap_is_set(tf.wildcard.bm, i);
2683
2684             ds_put_format(s, "      %s: %s\n",
2685                           f->name,
2686                           (mask ? "arbitrary mask"
2687                            : wildcard ? "exact match or wildcard"
2688                            : "must exact match"));
2689         }
2690     }
2691 }
2692
2693 static const char *
2694 bundle_flags_to_name(uint32_t bit)
2695 {
2696     switch (bit) {
2697     case OFPBF_ATOMIC:
2698         return "atomic";
2699     case OFPBF_ORDERED:
2700         return "ordered";
2701     default:
2702         return NULL;
2703     }
2704 }
2705
2706 static void
2707 ofp_print_bundle_ctrl(struct ds *s, const struct ofp_header *oh)
2708 {
2709     int error;
2710     struct ofputil_bundle_ctrl_msg bctrl;
2711
2712     error = ofputil_decode_bundle_ctrl(oh, &bctrl);
2713     if (error) {
2714         ofp_print_error(s, error);
2715         return;
2716     }
2717
2718     ds_put_char(s, '\n');
2719
2720     ds_put_format(s, " bundle_id=%#"PRIx32" type=",  bctrl.bundle_id);
2721     switch (bctrl.type) {
2722     case OFPBCT_OPEN_REQUEST:
2723         ds_put_cstr(s, "OPEN_REQUEST");
2724         break;
2725     case OFPBCT_OPEN_REPLY:
2726         ds_put_cstr(s, "OPEN_REPLY");
2727         break;
2728     case OFPBCT_CLOSE_REQUEST:
2729         ds_put_cstr(s, "CLOSE_REQUEST");
2730         break;
2731     case OFPBCT_CLOSE_REPLY:
2732         ds_put_cstr(s, "CLOSE_REPLY");
2733         break;
2734     case OFPBCT_COMMIT_REQUEST:
2735         ds_put_cstr(s, "COMMIT_REQUEST");
2736         break;
2737     case OFPBCT_COMMIT_REPLY:
2738         ds_put_cstr(s, "COMMIT_REPLY");
2739         break;
2740     case OFPBCT_DISCARD_REQUEST:
2741         ds_put_cstr(s, "DISCARD_REQUEST");
2742         break;
2743     case OFPBCT_DISCARD_REPLY:
2744         ds_put_cstr(s, "DISCARD_REPLY");
2745         break;
2746     }
2747
2748     ds_put_cstr(s, " flags=");
2749     ofp_print_bit_names(s, bctrl.flags, bundle_flags_to_name, ' ');
2750 }
2751
2752 static void
2753 ofp_print_bundle_add(struct ds *s, const struct ofp_header *oh, int verbosity)
2754 {
2755     int error;
2756     struct ofputil_bundle_add_msg badd;
2757     char *msg;
2758
2759     error = ofputil_decode_bundle_add(oh, &badd);
2760     if (error) {
2761         ofp_print_error(s, error);
2762         return;
2763     }
2764
2765     ds_put_char(s, '\n');
2766     ds_put_format(s, " bundle_id=%#"PRIx32,  badd.bundle_id);
2767     ds_put_cstr(s, " flags=");
2768     ofp_print_bit_names(s, badd.flags, bundle_flags_to_name, ' ');
2769
2770     ds_put_char(s, '\n');
2771     msg = ofp_to_string(badd.msg, ntohs(badd.msg->length), verbosity);
2772     if (msg) {
2773         ds_put_cstr(s, msg);
2774     }
2775 }
2776
2777 static void
2778 ofp_to_string__(const struct ofp_header *oh, enum ofpraw raw,
2779                 struct ds *string, int verbosity)
2780 {
2781     const void *msg = oh;
2782
2783     ofp_header_to_string__(oh, raw, string);
2784     switch (ofptype_from_ofpraw(raw)) {
2785
2786     case OFPTYPE_GROUP_STATS_REQUEST:
2787         ofp_print_stats(string, oh);
2788         ofp_print_ofpst_group_request(string, oh);
2789         break;
2790
2791     case OFPTYPE_GROUP_STATS_REPLY:
2792         ofp_print_group_stats(string, oh);
2793         break;
2794
2795     case OFPTYPE_GROUP_DESC_STATS_REQUEST:
2796         ofp_print_stats(string, oh);
2797         ofp_print_ofpst_group_desc_request(string, oh);
2798         break;
2799
2800     case OFPTYPE_GROUP_DESC_STATS_REPLY:
2801         ofp_print_group_desc(string, oh);
2802         break;
2803
2804     case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
2805         ofp_print_stats(string, oh);
2806         break;
2807
2808     case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
2809         ofp_print_group_features(string, oh);
2810         break;
2811
2812     case OFPTYPE_GROUP_MOD:
2813         ofp_print_group_mod(string, oh);
2814         break;
2815
2816     case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
2817     case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
2818         ofp_print_table_features(string, oh);
2819         break;
2820
2821     case OFPTYPE_HELLO:
2822         ofp_print_hello(string, oh);
2823         break;
2824
2825     case OFPTYPE_ERROR:
2826         ofp_print_error_msg(string, oh);
2827         break;
2828
2829     case OFPTYPE_ECHO_REQUEST:
2830     case OFPTYPE_ECHO_REPLY:
2831         ofp_print_echo(string, oh, verbosity);
2832         break;
2833
2834     case OFPTYPE_FEATURES_REQUEST:
2835         break;
2836
2837     case OFPTYPE_FEATURES_REPLY:
2838         ofp_print_switch_features(string, oh);
2839         break;
2840
2841     case OFPTYPE_GET_CONFIG_REQUEST:
2842         break;
2843
2844     case OFPTYPE_GET_CONFIG_REPLY:
2845     case OFPTYPE_SET_CONFIG:
2846         ofp_print_switch_config(string, ofpmsg_body(oh));
2847         break;
2848
2849     case OFPTYPE_PACKET_IN:
2850         ofp_print_packet_in(string, oh, verbosity);
2851         break;
2852
2853     case OFPTYPE_FLOW_REMOVED:
2854         ofp_print_flow_removed(string, oh);
2855         break;
2856
2857     case OFPTYPE_PORT_STATUS:
2858         ofp_print_port_status(string, oh);
2859         break;
2860
2861     case OFPTYPE_PACKET_OUT:
2862         ofp_print_packet_out(string, oh, verbosity);
2863         break;
2864
2865     case OFPTYPE_FLOW_MOD:
2866         ofp_print_flow_mod(string, oh, verbosity);
2867         break;
2868
2869     case OFPTYPE_PORT_MOD:
2870         ofp_print_port_mod(string, oh);
2871         break;
2872
2873     case OFPTYPE_TABLE_MOD:
2874         ofp_print_table_mod(string, oh);
2875         break;
2876
2877     case OFPTYPE_METER_MOD:
2878         ofp_print_meter_mod(string, oh);
2879         break;
2880
2881     case OFPTYPE_BARRIER_REQUEST:
2882     case OFPTYPE_BARRIER_REPLY:
2883         break;
2884
2885     case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
2886         ofp_print_queue_get_config_request(string, oh);
2887         break;
2888
2889     case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
2890         ofp_print_queue_get_config_reply(string, oh);
2891         break;
2892
2893     case OFPTYPE_ROLE_REQUEST:
2894     case OFPTYPE_ROLE_REPLY:
2895         ofp_print_role_message(string, oh);
2896         break;
2897     case OFPTYPE_ROLE_STATUS:
2898         ofp_print_role_status_message(string, oh);
2899         break;
2900
2901     case OFPTYPE_METER_STATS_REQUEST:
2902     case OFPTYPE_METER_CONFIG_STATS_REQUEST:
2903         ofp_print_stats(string, oh);
2904         ofp_print_meter_stats_request(string, oh);
2905         break;
2906
2907     case OFPTYPE_METER_STATS_REPLY:
2908         ofp_print_stats(string, oh);
2909         ofp_print_meter_stats_reply(string, oh);
2910         break;
2911
2912     case OFPTYPE_METER_CONFIG_STATS_REPLY:
2913         ofp_print_stats(string, oh);
2914         ofp_print_meter_config_reply(string, oh);
2915         break;
2916
2917     case OFPTYPE_METER_FEATURES_STATS_REPLY:
2918         ofp_print_stats(string, oh);
2919         ofp_print_meter_features_reply(string, oh);
2920         break;
2921
2922     case OFPTYPE_DESC_STATS_REQUEST:
2923     case OFPTYPE_METER_FEATURES_STATS_REQUEST:
2924         ofp_print_stats(string, oh);
2925         break;
2926
2927     case OFPTYPE_FLOW_STATS_REQUEST:
2928     case OFPTYPE_AGGREGATE_STATS_REQUEST:
2929         ofp_print_stats(string, oh);
2930         ofp_print_flow_stats_request(string, oh);
2931         break;
2932
2933     case OFPTYPE_TABLE_STATS_REQUEST:
2934         ofp_print_stats(string, oh);
2935         break;
2936
2937     case OFPTYPE_PORT_STATS_REQUEST:
2938         ofp_print_stats(string, oh);
2939         ofp_print_ofpst_port_request(string, oh);
2940         break;
2941
2942     case OFPTYPE_QUEUE_STATS_REQUEST:
2943         ofp_print_stats(string, oh);
2944         ofp_print_ofpst_queue_request(string, oh);
2945         break;
2946
2947     case OFPTYPE_DESC_STATS_REPLY:
2948         ofp_print_stats(string, oh);
2949         ofp_print_ofpst_desc_reply(string, oh);
2950         break;
2951
2952     case OFPTYPE_FLOW_STATS_REPLY:
2953         ofp_print_stats(string, oh);
2954         ofp_print_flow_stats_reply(string, oh);
2955         break;
2956
2957     case OFPTYPE_QUEUE_STATS_REPLY:
2958         ofp_print_stats(string, oh);
2959         ofp_print_ofpst_queue_reply(string, oh, verbosity);
2960         break;
2961
2962     case OFPTYPE_PORT_STATS_REPLY:
2963         ofp_print_stats(string, oh);
2964         ofp_print_ofpst_port_reply(string, oh, verbosity);
2965         break;
2966
2967     case OFPTYPE_TABLE_STATS_REPLY:
2968         ofp_print_stats(string, oh);
2969         ofp_print_ofpst_table_reply(string, oh, verbosity);
2970         break;
2971
2972     case OFPTYPE_AGGREGATE_STATS_REPLY:
2973         ofp_print_stats(string, oh);
2974         ofp_print_aggregate_stats_reply(string, oh);
2975         break;
2976
2977     case OFPTYPE_PORT_DESC_STATS_REQUEST:
2978         ofp_print_stats(string, oh);
2979         ofp_print_ofpst_port_desc_request(string, oh);
2980         break;
2981
2982     case OFPTYPE_PORT_DESC_STATS_REPLY:
2983         ofp_print_stats(string, oh);
2984         ofp_print_ofpst_port_desc_reply(string, oh);
2985         break;
2986
2987     case OFPTYPE_FLOW_MOD_TABLE_ID:
2988         ofp_print_nxt_flow_mod_table_id(string, ofpmsg_body(oh));
2989         break;
2990
2991     case OFPTYPE_SET_FLOW_FORMAT:
2992         ofp_print_nxt_set_flow_format(string, ofpmsg_body(oh));
2993         break;
2994
2995     case OFPTYPE_SET_PACKET_IN_FORMAT:
2996         ofp_print_nxt_set_packet_in_format(string, ofpmsg_body(oh));
2997         break;
2998
2999     case OFPTYPE_FLOW_AGE:
3000         break;
3001
3002     case OFPTYPE_SET_CONTROLLER_ID:
3003         ofp_print_nxt_set_controller_id(string, ofpmsg_body(oh));
3004         break;
3005
3006     case OFPTYPE_GET_ASYNC_REPLY:
3007     case OFPTYPE_SET_ASYNC_CONFIG:
3008         ofp_print_nxt_set_async_config(string, ofpmsg_body(oh));
3009         break;
3010     case OFPTYPE_GET_ASYNC_REQUEST:
3011         break;
3012     case OFPTYPE_FLOW_MONITOR_CANCEL:
3013         ofp_print_nxt_flow_monitor_cancel(string, msg);
3014         break;
3015
3016     case OFPTYPE_FLOW_MONITOR_PAUSED:
3017     case OFPTYPE_FLOW_MONITOR_RESUMED:
3018         break;
3019
3020     case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
3021         ofp_print_nxst_flow_monitor_request(string, msg);
3022         break;
3023
3024     case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
3025         ofp_print_nxst_flow_monitor_reply(string, msg);
3026         break;
3027
3028     case OFPTYPE_BUNDLE_CONTROL:
3029         ofp_print_bundle_ctrl(string, msg);
3030         break;
3031
3032     case OFPTYPE_BUNDLE_ADD_MESSAGE:
3033         ofp_print_bundle_add(string, msg, verbosity);
3034         break;
3035     }
3036 }
3037
3038 /* Composes and returns a string representing the OpenFlow packet of 'len'
3039  * bytes at 'oh' at the given 'verbosity' level.  0 is a minimal amount of
3040  * verbosity and higher numbers increase verbosity.  The caller is responsible
3041  * for freeing the string. */
3042 char *
3043 ofp_to_string(const void *oh_, size_t len, int verbosity)
3044 {
3045     struct ds string = DS_EMPTY_INITIALIZER;
3046     const struct ofp_header *oh = oh_;
3047
3048     if (!len) {
3049         ds_put_cstr(&string, "OpenFlow message is empty\n");
3050     } else if (len < sizeof(struct ofp_header)) {
3051         ds_put_format(&string, "OpenFlow packet too short (only %"PRIuSIZE" bytes):\n",
3052                       len);
3053     } else if (ntohs(oh->length) > len) {
3054         enum ofperr error;
3055         enum ofpraw raw;
3056
3057         error = ofpraw_decode_partial(&raw, oh, len);
3058         if (!error) {
3059             ofp_header_to_string__(oh, raw, &string);
3060             ds_put_char(&string, '\n');
3061         }
3062
3063         ds_put_format(&string,
3064                       "(***truncated to %"PRIuSIZE" bytes from %"PRIu16"***)\n",
3065                       len, ntohs(oh->length));
3066     } else if (ntohs(oh->length) < len) {
3067         ds_put_format(&string,
3068                       "(***only uses %"PRIu16" bytes out of %"PRIuSIZE"***)\n",
3069                       ntohs(oh->length), len);
3070     } else {
3071         enum ofperr error;
3072         enum ofpraw raw;
3073
3074         error = ofpraw_decode(&raw, oh);
3075         if (!error) {
3076             ofp_to_string__(oh, raw, &string, verbosity);
3077             if (verbosity >= 5) {
3078                 if (ds_last(&string) != '\n') {
3079                     ds_put_char(&string, '\n');
3080                 }
3081                 ds_put_hex_dump(&string, oh, len, 0, true);
3082             }
3083             if (ds_last(&string) != '\n') {
3084                 ds_put_char(&string, '\n');
3085             }
3086             return ds_steal_cstr(&string);
3087         }
3088
3089         ofp_print_error(&string, error);
3090     }
3091     ds_put_hex_dump(&string, oh, len, 0, true);
3092     return ds_steal_cstr(&string);
3093 }
3094 \f
3095 static void
3096 print_and_free(FILE *stream, char *string)
3097 {
3098     fputs(string, stream);
3099     free(string);
3100 }
3101
3102 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
3103  * given 'verbosity' level.  0 is a minimal amount of verbosity and higher
3104  * numbers increase verbosity. */
3105 void
3106 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
3107 {
3108     print_and_free(stream, ofp_to_string(oh, len, verbosity));
3109 }
3110
3111 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
3112  * 'data' to 'stream'. */
3113 void
3114 ofp_print_packet(FILE *stream, const void *data, size_t len)
3115 {
3116     print_and_free(stream, ofp_packet_to_string(data, len));
3117 }