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