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