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