ofp-util: Convert flow_metadata to match structure.
[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     default:
869         snprintf(reasonbuf, bufsize, "%d", (int) reason);
870         return reasonbuf;
871     }
872 }
873
874 static void
875 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
876 {
877     char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
878     struct ofputil_flow_removed fr;
879     enum ofperr error;
880
881     error = ofputil_decode_flow_removed(&fr, oh);
882     if (error) {
883         ofp_print_error(string, error);
884         return;
885     }
886
887     ds_put_char(string, ' ');
888     match_format(&fr.match, string, fr.priority);
889
890     ds_put_format(string, " reason=%s",
891                   ofp_flow_removed_reason_to_string(fr.reason, reasonbuf,
892                                                     sizeof reasonbuf));
893
894     if (fr.table_id != 255) {
895         ds_put_format(string, " table_id=%"PRIu8, fr.table_id);
896     }
897
898     if (fr.cookie != htonll(0)) {
899         ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
900     }
901     ds_put_cstr(string, " duration");
902     ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
903     ds_put_format(string, " idle%"PRIu16, fr.idle_timeout);
904     if (fr.hard_timeout) {
905         /* The hard timeout was only added in OF1.2, so only print it if it is
906          * actually in use to avoid gratuitous change to the formatting. */
907         ds_put_format(string, " hard%"PRIu16, fr.hard_timeout);
908     }
909     ds_put_format(string, " pkts%"PRIu64" bytes%"PRIu64"\n",
910                   fr.packet_count, fr.byte_count);
911 }
912
913 static void
914 ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
915 {
916     struct ofputil_port_mod pm;
917     enum ofperr error;
918
919     error = ofputil_decode_port_mod(oh, &pm, true);
920     if (error) {
921         ofp_print_error(string, error);
922         return;
923     }
924
925     ds_put_cstr(string, "port: ");
926     ofputil_format_port(pm.port_no, string);
927     ds_put_format(string, ": addr:"ETH_ADDR_FMT"\n",
928                   ETH_ADDR_ARGS(pm.hw_addr));
929
930     ds_put_cstr(string, "     config: ");
931     ofp_print_port_config(string, pm.config);
932
933     ds_put_cstr(string, "     mask:   ");
934     ofp_print_port_config(string, pm.mask);
935
936     ds_put_cstr(string, "     advertise: ");
937     if (pm.advertise) {
938         ofp_print_port_features(string, pm.advertise);
939     } else {
940         ds_put_cstr(string, "UNCHANGED\n");
941     }
942 }
943
944 static void
945 ofp_print_table_miss_config(struct ds *string, enum ofputil_table_miss miss)
946 {
947     switch (miss) {
948     case OFPUTIL_TABLE_MISS_CONTROLLER:
949         ds_put_cstr(string, "controller\n");
950         break;
951     case OFPUTIL_TABLE_MISS_CONTINUE:
952         ds_put_cstr(string, "continue\n");
953         break;
954     case OFPUTIL_TABLE_MISS_DROP:
955         ds_put_cstr(string, "drop\n");
956         break;
957     case OFPUTIL_TABLE_MISS_DEFAULT:
958     default:
959         ds_put_format(string, "Unknown (%d)\n", miss);
960         break;
961     }
962 }
963
964 static void
965 ofp_print_table_mod(struct ds *string, const struct ofp_header *oh)
966 {
967     struct ofputil_table_mod pm;
968     enum ofperr error;
969
970     error = ofputil_decode_table_mod(oh, &pm);
971     if (error) {
972         ofp_print_error(string, error);
973         return;
974     }
975
976     if (pm.table_id == 0xff) {
977         ds_put_cstr(string, " table_id: ALL_TABLES");
978     } else {
979         ds_put_format(string, " table_id=%"PRIu8, pm.table_id);
980     }
981
982     if (pm.miss_config != OFPUTIL_TABLE_MISS_DEFAULT) {
983         ds_put_cstr(string, ", flow_miss_config=");
984         ofp_print_table_miss_config(string, pm.miss_config);
985     }
986 }
987
988 static void
989 ofp_print_queue_get_config_request(struct ds *string,
990                                    const struct ofp_header *oh)
991 {
992     enum ofperr error;
993     ofp_port_t port;
994
995     error = ofputil_decode_queue_get_config_request(oh, &port);
996     if (error) {
997         ofp_print_error(string, error);
998         return;
999     }
1000
1001     ds_put_cstr(string, " port=");
1002     ofputil_format_port(port, string);
1003 }
1004
1005 static void
1006 print_queue_rate(struct ds *string, const char *name, unsigned int rate)
1007 {
1008     if (rate <= 1000) {
1009         ds_put_format(string, " %s:%u.%u%%", name, rate / 10, rate % 10);
1010     } else if (rate < UINT16_MAX) {
1011         ds_put_format(string, " %s:(disabled)", name);
1012     }
1013 }
1014
1015 static void
1016 ofp_print_queue_get_config_reply(struct ds *string,
1017                                  const struct ofp_header *oh)
1018 {
1019     enum ofperr error;
1020     struct ofpbuf b;
1021     ofp_port_t port;
1022
1023     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1024     error = ofputil_decode_queue_get_config_reply(&b, &port);
1025     if (error) {
1026         ofp_print_error(string, error);
1027         return;
1028     }
1029
1030     ds_put_cstr(string, " port=");
1031     ofputil_format_port(port, string);
1032     ds_put_char(string, '\n');
1033
1034     for (;;) {
1035         struct ofputil_queue_config queue;
1036         int retval;
1037
1038         retval = ofputil_pull_queue_get_config_reply(&b, &queue);
1039         if (retval) {
1040             if (retval != EOF) {
1041                 ofp_print_error(string, retval);
1042             }
1043             break;
1044         }
1045
1046         ds_put_format(string, "queue %"PRIu32":", queue.queue_id);
1047         print_queue_rate(string, "min_rate", queue.min_rate);
1048         print_queue_rate(string, "max_rate", queue.max_rate);
1049         ds_put_char(string, '\n');
1050     }
1051 }
1052
1053 static void
1054 ofp_print_meter_flags(struct ds *s, uint16_t flags)
1055 {
1056     if (flags & OFPMF13_KBPS) {
1057         ds_put_cstr(s, "kbps ");
1058     }
1059     if (flags & OFPMF13_PKTPS) {
1060         ds_put_cstr(s, "pktps ");
1061     }
1062     if (flags & OFPMF13_BURST) {
1063         ds_put_cstr(s, "burst ");
1064     }
1065     if (flags & OFPMF13_STATS) {
1066         ds_put_cstr(s, "stats ");
1067     }
1068
1069     flags &= ~(OFPMF13_KBPS | OFPMF13_PKTPS | OFPMF13_BURST | OFPMF13_STATS);
1070     if (flags) {
1071         ds_put_format(s, "flags:0x%"PRIx16" ", flags);
1072     }
1073 }
1074
1075 static void
1076 ofp_print_meter_band(struct ds *s, uint16_t flags,
1077                      const struct ofputil_meter_band *mb)
1078 {
1079     ds_put_cstr(s, "\ntype=");
1080     switch (mb->type) {
1081     case OFPMBT13_DROP:
1082         ds_put_cstr(s, "drop");
1083         break;
1084     case OFPMBT13_DSCP_REMARK:
1085         ds_put_cstr(s, "dscp_remark");
1086         break;
1087     default:
1088         ds_put_format(s, "%u", mb->type);
1089     }
1090
1091     ds_put_format(s, " rate=%"PRIu32, mb->rate);
1092
1093     if (flags & OFPMF13_BURST) {
1094         ds_put_format(s, " burst_size=%"PRIu32, mb->burst_size);
1095     }
1096     if (mb->type == OFPMBT13_DSCP_REMARK) {
1097         ds_put_format(s, " prec_level=%"PRIu8, mb->prec_level);
1098     }
1099 }
1100
1101 static void
1102 ofp_print_meter_stats(struct ds *s, const struct ofputil_meter_stats *ms)
1103 {
1104     uint16_t i;
1105
1106     ds_put_format(s, "meter:%"PRIu32" ", ms->meter_id);
1107     ds_put_format(s, "flow_count:%"PRIu32" ", ms->flow_count);
1108     ds_put_format(s, "packet_in_count:%"PRIu64" ", ms->packet_in_count);
1109     ds_put_format(s, "byte_in_count:%"PRIu64" ", ms->byte_in_count);
1110     ds_put_cstr(s, "duration:");
1111     ofp_print_duration(s, ms->duration_sec, ms->duration_nsec);
1112     ds_put_char(s, ' ');
1113
1114     ds_put_cstr(s, "bands:\n");
1115     for (i = 0; i < ms->n_bands; ++i) {
1116         ds_put_format(s, "%d: ", i);
1117         ds_put_format(s, "packet_count:%"PRIu64" ", ms->bands[i].packet_count);
1118         ds_put_format(s, "byte_count:%"PRIu64"\n", ms->bands[i].byte_count);
1119     }
1120 }
1121
1122 static void
1123 ofp_print_meter_config(struct ds *s, const struct ofputil_meter_config *mc)
1124 {
1125     uint16_t i;
1126
1127     ds_put_format(s, "meter=%"PRIu32" ", mc->meter_id);
1128
1129     ofp_print_meter_flags(s, mc->flags);
1130
1131     ds_put_cstr(s, "bands=");
1132     for (i = 0; i < mc->n_bands; ++i) {
1133         ofp_print_meter_band(s, mc->flags, &mc->bands[i]);
1134     }
1135     ds_put_char(s, '\n');
1136 }
1137
1138 static void
1139 ofp_print_meter_mod(struct ds *s, const struct ofp_header *oh)
1140 {
1141     struct ofputil_meter_mod mm;
1142     struct ofpbuf bands;
1143     enum ofperr error;
1144
1145     ofpbuf_init(&bands, 64);
1146     error = ofputil_decode_meter_mod(oh, &mm, &bands);
1147     if (error) {
1148         ofpbuf_uninit(&bands);
1149         ofp_print_error(s, error);
1150         return;
1151     }
1152
1153     switch (mm.command) {
1154     case OFPMC13_ADD:
1155         ds_put_cstr(s, " ADD ");
1156         break;
1157     case OFPMC13_MODIFY:
1158         ds_put_cstr(s, " MOD ");
1159         break;
1160     case OFPMC13_DELETE:
1161         ds_put_cstr(s, " DEL ");
1162         break;
1163     default:
1164         ds_put_format(s, " cmd:%d ", mm.command);
1165     }
1166
1167     ofp_print_meter_config(s, &mm.meter);
1168     ofpbuf_uninit(&bands);
1169 }
1170
1171 static void
1172 ofp_print_meter_stats_request(struct ds *s, const struct ofp_header *oh)
1173 {
1174     uint32_t meter_id;
1175
1176     ofputil_decode_meter_request(oh, &meter_id);
1177
1178     ds_put_format(s, " meter=%"PRIu32, meter_id);
1179 }
1180
1181 static const char *
1182 ofputil_meter_capabilities_to_name(uint32_t bit)
1183 {
1184     enum ofp13_meter_flags flag = bit;
1185
1186     switch (flag) {
1187     case OFPMF13_KBPS:    return "kbps";
1188     case OFPMF13_PKTPS:   return "pktps";
1189     case OFPMF13_BURST:   return "burst";
1190     case OFPMF13_STATS:   return "stats";
1191     }
1192
1193     return NULL;
1194 }
1195
1196 static const char *
1197 ofputil_meter_band_types_to_name(uint32_t bit)
1198 {
1199     switch (bit) {
1200     case 1 << OFPMBT13_DROP:          return "drop";
1201     case 1 << OFPMBT13_DSCP_REMARK:   return "dscp_remark";
1202     }
1203
1204     return NULL;
1205 }
1206
1207 static void
1208 ofp_print_meter_features_reply(struct ds *s, const struct ofp_header *oh)
1209 {
1210     struct ofputil_meter_features mf;
1211
1212     ofputil_decode_meter_features(oh, &mf);
1213
1214     ds_put_format(s, "\nmax_meter:%"PRIu32, mf.max_meters);
1215     ds_put_format(s, " max_bands:%"PRIu8, mf.max_bands);
1216     ds_put_format(s, " max_color:%"PRIu8"\n", mf.max_color);
1217
1218     ds_put_cstr(s, "band_types: ");
1219     ofp_print_bit_names(s, mf.band_types,
1220                         ofputil_meter_band_types_to_name, ' ');
1221     ds_put_char(s, '\n');
1222
1223     ds_put_cstr(s, "capabilities: ");
1224     ofp_print_bit_names(s, mf.capabilities,
1225                         ofputil_meter_capabilities_to_name, ' ');
1226     ds_put_char(s, '\n');
1227 }
1228
1229 static void
1230 ofp_print_meter_config_reply(struct ds *s, const struct ofp_header *oh)
1231 {
1232     struct ofpbuf bands;
1233     struct ofpbuf b;
1234
1235     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1236     ofpbuf_init(&bands, 64);
1237     for (;;) {
1238         struct ofputil_meter_config mc;
1239         int retval;
1240
1241         retval = ofputil_decode_meter_config(&b, &mc, &bands);
1242         if (retval) {
1243             if (retval != EOF) {
1244                 ofp_print_error(s, retval);
1245             }
1246             break;
1247         }
1248         ds_put_char(s, '\n');
1249         ofp_print_meter_config(s, &mc);
1250     }
1251     ofpbuf_uninit(&bands);
1252 }
1253
1254 static void
1255 ofp_print_meter_stats_reply(struct ds *s, const struct ofp_header *oh)
1256 {
1257     struct ofpbuf bands;
1258     struct ofpbuf b;
1259
1260     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1261     ofpbuf_init(&bands, 64);
1262     for (;;) {
1263         struct ofputil_meter_stats ms;
1264         int retval;
1265
1266         retval = ofputil_decode_meter_stats(&b, &ms, &bands);
1267         if (retval) {
1268             if (retval != EOF) {
1269                 ofp_print_error(s, retval);
1270             }
1271             break;
1272         }
1273         ds_put_char(s, '\n');
1274         ofp_print_meter_stats(s, &ms);
1275     }
1276     ofpbuf_uninit(&bands);
1277 }
1278
1279 static void
1280 ofp_print_error(struct ds *string, enum ofperr error)
1281 {
1282     if (string->length) {
1283         ds_put_char(string, ' ');
1284     }
1285     ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1286 }
1287
1288 static void
1289 ofp_print_hello(struct ds *string, const struct ofp_header *oh)
1290 {
1291     uint32_t allowed_versions;
1292     bool ok;
1293
1294     ok = ofputil_decode_hello(oh, &allowed_versions);
1295
1296     ds_put_cstr(string, "\n version bitmap: ");
1297     ofputil_format_version_bitmap(string, allowed_versions);
1298
1299     if (!ok) {
1300         ds_put_cstr(string, "\n unknown data in hello:\n");
1301         ds_put_hex_dump(string, oh, ntohs(oh->length), 0, true);
1302     }
1303 }
1304
1305 static void
1306 ofp_print_error_msg(struct ds *string, const struct ofp_header *oh)
1307 {
1308     size_t len = ntohs(oh->length);
1309     struct ofpbuf payload;
1310     enum ofperr error;
1311     char *s;
1312
1313     error = ofperr_decode_msg(oh, &payload);
1314     if (!error) {
1315         ds_put_cstr(string, "***decode error***");
1316         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1317         return;
1318     }
1319
1320     ds_put_format(string, " %s\n", ofperr_get_name(error));
1321
1322     if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
1323         ds_put_printable(string, payload.data, payload.size);
1324     } else {
1325         s = ofp_to_string(payload.data, payload.size, 1);
1326         ds_put_cstr(string, s);
1327         free(s);
1328     }
1329     ofpbuf_uninit(&payload);
1330 }
1331
1332 static void
1333 ofp_print_port_status(struct ds *string, const struct ofp_header *oh)
1334 {
1335     struct ofputil_port_status ps;
1336     enum ofperr error;
1337
1338     error = ofputil_decode_port_status(oh, &ps);
1339     if (error) {
1340         ofp_print_error(string, error);
1341         return;
1342     }
1343
1344     if (ps.reason == OFPPR_ADD) {
1345         ds_put_format(string, " ADD:");
1346     } else if (ps.reason == OFPPR_DELETE) {
1347         ds_put_format(string, " DEL:");
1348     } else if (ps.reason == OFPPR_MODIFY) {
1349         ds_put_format(string, " MOD:");
1350     }
1351
1352     ofp_print_phy_port(string, &ps.desc);
1353 }
1354
1355 static void
1356 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
1357 {
1358     const struct ofp_desc_stats *ods = ofpmsg_body(oh);
1359
1360     ds_put_char(string, '\n');
1361     ds_put_format(string, "Manufacturer: %.*s\n",
1362             (int) sizeof ods->mfr_desc, ods->mfr_desc);
1363     ds_put_format(string, "Hardware: %.*s\n",
1364             (int) sizeof ods->hw_desc, ods->hw_desc);
1365     ds_put_format(string, "Software: %.*s\n",
1366             (int) sizeof ods->sw_desc, ods->sw_desc);
1367     ds_put_format(string, "Serial Num: %.*s\n",
1368             (int) sizeof ods->serial_num, ods->serial_num);
1369     ds_put_format(string, "DP Description: %.*s\n",
1370             (int) sizeof ods->dp_desc, ods->dp_desc);
1371 }
1372
1373 static void
1374 ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
1375 {
1376     struct ofputil_flow_stats_request fsr;
1377     enum ofperr error;
1378
1379     error = ofputil_decode_flow_stats_request(&fsr, oh);
1380     if (error) {
1381         ofp_print_error(string, error);
1382         return;
1383     }
1384
1385     if (fsr.table_id != 0xff) {
1386         ds_put_format(string, " table=%"PRIu8, fsr.table_id);
1387     }
1388
1389     if (fsr.out_port != OFPP_ANY) {
1390         ds_put_cstr(string, " out_port=");
1391         ofputil_format_port(fsr.out_port, string);
1392     }
1393
1394     ds_put_char(string, ' ');
1395     match_format(&fsr.match, string, OFP_DEFAULT_PRIORITY);
1396 }
1397
1398 void
1399 ofp_print_flow_stats(struct ds *string, struct ofputil_flow_stats *fs)
1400 {
1401     ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1402                   ntohll(fs->cookie));
1403
1404     ofp_print_duration(string, fs->duration_sec, fs->duration_nsec);
1405     ds_put_format(string, ", table=%"PRIu8", ", fs->table_id);
1406     ds_put_format(string, "n_packets=%"PRIu64", ", fs->packet_count);
1407     ds_put_format(string, "n_bytes=%"PRIu64", ", fs->byte_count);
1408     if (fs->idle_timeout != OFP_FLOW_PERMANENT) {
1409         ds_put_format(string, "idle_timeout=%"PRIu16", ", fs->idle_timeout);
1410     }
1411     if (fs->hard_timeout != OFP_FLOW_PERMANENT) {
1412         ds_put_format(string, "hard_timeout=%"PRIu16", ", fs->hard_timeout);
1413     }
1414     if (fs->flags) {
1415         ofp_print_flow_flags(string, fs->flags);
1416     }
1417     if (fs->importance != 0) {
1418         ds_put_format(string, "importance=%"PRIu16", ", fs->importance);
1419     }
1420     if (fs->idle_age >= 0) {
1421         ds_put_format(string, "idle_age=%d, ", fs->idle_age);
1422     }
1423     if (fs->hard_age >= 0 && fs->hard_age != fs->duration_sec) {
1424         ds_put_format(string, "hard_age=%d, ", fs->hard_age);
1425     }
1426
1427     match_format(&fs->match, string, fs->priority);
1428     if (string->string[string->length - 1] != ' ') {
1429         ds_put_char(string, ' ');
1430     }
1431
1432     ds_put_cstr(string, "actions=");
1433     ofpacts_format(fs->ofpacts, fs->ofpacts_len, string);
1434 }
1435
1436 static void
1437 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1438 {
1439     struct ofpbuf ofpacts;
1440     struct ofpbuf b;
1441
1442     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1443     ofpbuf_init(&ofpacts, 64);
1444     for (;;) {
1445         struct ofputil_flow_stats fs;
1446         int retval;
1447
1448         retval = ofputil_decode_flow_stats_reply(&fs, &b, true, &ofpacts);
1449         if (retval) {
1450             if (retval != EOF) {
1451                 ds_put_cstr(string, " ***parse error***");
1452             }
1453             break;
1454         }
1455         ds_put_char(string, '\n');
1456         ofp_print_flow_stats(string, &fs);
1457      }
1458     ofpbuf_uninit(&ofpacts);
1459 }
1460
1461 static void
1462 ofp_print_aggregate_stats_reply(struct ds *string, const struct ofp_header *oh)
1463 {
1464     struct ofputil_aggregate_stats as;
1465     enum ofperr error;
1466
1467     error = ofputil_decode_aggregate_stats_reply(&as, oh);
1468     if (error) {
1469         ofp_print_error(string, error);
1470         return;
1471     }
1472
1473     ds_put_format(string, " packet_count=%"PRIu64, as.packet_count);
1474     ds_put_format(string, " byte_count=%"PRIu64, as.byte_count);
1475     ds_put_format(string, " flow_count=%"PRIu32, as.flow_count);
1476 }
1477
1478 static void
1479 print_port_stat(struct ds *string, const char *leader, uint64_t stat, int more)
1480 {
1481     ds_put_cstr(string, leader);
1482     if (stat != UINT64_MAX) {
1483         ds_put_format(string, "%"PRIu64, stat);
1484     } else {
1485         ds_put_char(string, '?');
1486     }
1487     if (more) {
1488         ds_put_cstr(string, ", ");
1489     } else {
1490         ds_put_cstr(string, "\n");
1491     }
1492 }
1493
1494 static void
1495 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1496 {
1497     ofp_port_t ofp10_port;
1498     enum ofperr error;
1499
1500     error = ofputil_decode_port_stats_request(oh, &ofp10_port);
1501     if (error) {
1502         ofp_print_error(string, error);
1503         return;
1504     }
1505
1506     ds_put_cstr(string, " port_no=");
1507     ofputil_format_port(ofp10_port, string);
1508 }
1509
1510 static void
1511 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1512                            int verbosity)
1513 {
1514     struct ofpbuf b;
1515
1516     ds_put_format(string, " %"PRIuSIZE" ports\n", ofputil_count_port_stats(oh));
1517     if (verbosity < 1) {
1518         return;
1519     }
1520
1521     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1522     for (;;) {
1523         struct ofputil_port_stats ps;
1524         int retval;
1525
1526         retval = ofputil_decode_port_stats(&ps, &b);
1527         if (retval) {
1528             if (retval != EOF) {
1529                 ds_put_cstr(string, " ***parse error***");
1530             }
1531             return;
1532         }
1533
1534         ds_put_cstr(string, "  port ");
1535         if (ofp_to_u16(ps.port_no) < 10) {
1536             ds_put_char(string, ' ');
1537         }
1538         ofputil_format_port(ps.port_no, string);
1539
1540         ds_put_cstr(string, ": rx ");
1541         print_port_stat(string, "pkts=", ps.stats.rx_packets, 1);
1542         print_port_stat(string, "bytes=", ps.stats.rx_bytes, 1);
1543         print_port_stat(string, "drop=", ps.stats.rx_dropped, 1);
1544         print_port_stat(string, "errs=", ps.stats.rx_errors, 1);
1545         print_port_stat(string, "frame=", ps.stats.rx_frame_errors, 1);
1546         print_port_stat(string, "over=", ps.stats.rx_over_errors, 1);
1547         print_port_stat(string, "crc=", ps.stats.rx_crc_errors, 0);
1548
1549         ds_put_cstr(string, "           tx ");
1550         print_port_stat(string, "pkts=", ps.stats.tx_packets, 1);
1551         print_port_stat(string, "bytes=", ps.stats.tx_bytes, 1);
1552         print_port_stat(string, "drop=", ps.stats.tx_dropped, 1);
1553         print_port_stat(string, "errs=", ps.stats.tx_errors, 1);
1554         print_port_stat(string, "coll=", ps.stats.collisions, 0);
1555
1556         if (ps.duration_sec != UINT32_MAX) {
1557             ds_put_cstr(string, "           duration=");
1558             ofp_print_duration(string, ps.duration_sec, ps.duration_nsec);
1559             ds_put_char(string, '\n');
1560         }
1561     }
1562 }
1563
1564 static void
1565 ofp_print_table_stats_reply(struct ds *string, const struct ofp_header *oh)
1566 {
1567     struct ofpbuf b;
1568
1569     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1570     ofpraw_pull_assert(&b);
1571
1572     for (;;) {
1573         struct ofputil_table_features features;
1574         struct ofputil_table_stats stats;
1575         int retval;
1576
1577         retval = ofputil_decode_table_stats_reply(&b, &stats, &features);
1578         if (retval) {
1579             if (retval != EOF) {
1580                 ofp_print_error(string, retval);
1581             }
1582             return;
1583         }
1584
1585         ofp_print_table_features(string, &features, &stats);
1586     }
1587 }
1588
1589 static void
1590 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1591 {
1592     if (queue_id == OFPQ_ALL) {
1593         ds_put_cstr(string, "ALL");
1594     } else {
1595         ds_put_format(string, "%"PRIu32, queue_id);
1596     }
1597 }
1598
1599 static void
1600 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1601 {
1602     struct ofputil_queue_stats_request oqsr;
1603     enum ofperr error;
1604
1605     error = ofputil_decode_queue_stats_request(oh, &oqsr);
1606     if (error) {
1607         ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1608         return;
1609     }
1610
1611     ds_put_cstr(string, "port=");
1612     ofputil_format_port(oqsr.port_no, string);
1613
1614     ds_put_cstr(string, " queue=");
1615     ofp_print_queue_name(string, oqsr.queue_id);
1616 }
1617
1618 static void
1619 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1620                             int verbosity)
1621 {
1622     struct ofpbuf b;
1623
1624     ds_put_format(string, " %"PRIuSIZE" queues\n", ofputil_count_queue_stats(oh));
1625     if (verbosity < 1) {
1626         return;
1627     }
1628
1629     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1630     for (;;) {
1631         struct ofputil_queue_stats qs;
1632         int retval;
1633
1634         retval = ofputil_decode_queue_stats(&qs, &b);
1635         if (retval) {
1636             if (retval != EOF) {
1637                 ds_put_cstr(string, " ***parse error***");
1638             }
1639             return;
1640         }
1641
1642         ds_put_cstr(string, "  port ");
1643         ofputil_format_port(qs.port_no, string);
1644         ds_put_cstr(string, " queue ");
1645         ofp_print_queue_name(string, qs.queue_id);
1646         ds_put_cstr(string, ": ");
1647
1648         print_port_stat(string, "bytes=", qs.tx_bytes, 1);
1649         print_port_stat(string, "pkts=", qs.tx_packets, 1);
1650         print_port_stat(string, "errors=", qs.tx_errors, 1);
1651
1652         ds_put_cstr(string, "duration=");
1653         if (qs.duration_sec != UINT32_MAX) {
1654             ofp_print_duration(string, qs.duration_sec, qs.duration_nsec);
1655         } else {
1656             ds_put_char(string, '?');
1657         }
1658         ds_put_char(string, '\n');
1659     }
1660 }
1661
1662 static void
1663 ofp_print_ofpst_port_desc_request(struct ds *string,
1664                                   const struct ofp_header *oh)
1665 {
1666     enum ofperr error;
1667     ofp_port_t port;
1668
1669     error = ofputil_decode_port_desc_stats_request(oh, &port);
1670     if (error) {
1671         ofp_print_error(string, error);
1672         return;
1673     }
1674
1675     ds_put_cstr(string, " port=");
1676     ofputil_format_port(port, string);
1677 }
1678
1679 static void
1680 ofp_print_ofpst_port_desc_reply(struct ds *string,
1681                                 const struct ofp_header *oh)
1682 {
1683     struct ofpbuf b;
1684
1685     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1686     ofpraw_pull_assert(&b);
1687     ds_put_char(string, '\n');
1688     ofp_print_phy_ports(string, oh->version, &b);
1689 }
1690
1691 static void
1692 ofp_print_stats(struct ds *string, const struct ofp_header *oh)
1693 {
1694     uint16_t flags = ofpmp_flags(oh);
1695
1696     if (flags) {
1697         ds_put_cstr(string, " flags=");
1698         if ((!ofpmsg_is_stat_request(oh) || oh->version >= OFP13_VERSION)
1699             && (flags & OFPSF_REPLY_MORE)) {
1700             ds_put_cstr(string, "[more]");
1701             flags &= ~OFPSF_REPLY_MORE;
1702         }
1703         if (flags) {
1704             ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1705                           flags);
1706         }
1707     }
1708 }
1709
1710 static void
1711 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1712 {
1713     size_t len = ntohs(oh->length);
1714
1715     ds_put_format(string, " %"PRIuSIZE" bytes of payload\n", len - sizeof *oh);
1716     if (verbosity > 1) {
1717         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1718     }
1719 }
1720
1721 static void
1722 ofp_print_role_generic(struct ds *string, enum ofp12_controller_role role,
1723                        uint64_t generation_id)
1724 {
1725     ds_put_cstr(string, " role=");
1726
1727     switch (role) {
1728     case OFPCR12_ROLE_NOCHANGE:
1729         ds_put_cstr(string, "nochange");
1730         break;
1731     case OFPCR12_ROLE_EQUAL:
1732         ds_put_cstr(string, "equal"); /* OF 1.2 wording */
1733         break;
1734     case OFPCR12_ROLE_MASTER:
1735         ds_put_cstr(string, "master");
1736         break;
1737     case OFPCR12_ROLE_SLAVE:
1738         ds_put_cstr(string, "slave");
1739         break;
1740     default:
1741         OVS_NOT_REACHED();
1742     }
1743
1744     if (generation_id != UINT64_MAX) {
1745         ds_put_format(string, " generation_id=%"PRIu64, generation_id);
1746     }
1747 }
1748
1749 static void
1750 ofp_print_role_message(struct ds *string, const struct ofp_header *oh)
1751 {
1752     struct ofputil_role_request rr;
1753     enum ofperr error;
1754
1755     error = ofputil_decode_role_message(oh, &rr);
1756     if (error) {
1757         ofp_print_error(string, error);
1758         return;
1759     }
1760
1761     ofp_print_role_generic(string, rr.role, rr.have_generation_id ? rr.generation_id : UINT64_MAX);
1762 }
1763
1764 static void
1765 ofp_print_role_status_message(struct ds *string, const struct ofp_header *oh)
1766 {
1767     struct ofputil_role_status rs;
1768     enum ofperr error;
1769
1770     error = ofputil_decode_role_status(oh, &rs);
1771     if (error) {
1772         ofp_print_error(string, error);
1773         return;
1774     }
1775
1776     ofp_print_role_generic(string, rs.role, rs.generation_id);
1777
1778     ds_put_cstr(string, " reason=");
1779
1780     switch (rs.reason) {
1781     case OFPCRR_MASTER_REQUEST:
1782         ds_put_cstr(string, "master_request");
1783         break;
1784     case OFPCRR_CONFIG:
1785         ds_put_cstr(string, "configuration_changed");
1786         break;
1787     case OFPCRR_EXPERIMENTER:
1788         ds_put_cstr(string, "experimenter_data_changed");
1789         break;
1790     default:
1791         OVS_NOT_REACHED();
1792     }
1793 }
1794
1795 static void
1796 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1797                                 const struct nx_flow_mod_table_id *nfmti)
1798 {
1799     ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1800 }
1801
1802 static void
1803 ofp_print_nxt_set_flow_format(struct ds *string,
1804                               const struct nx_set_flow_format *nsff)
1805 {
1806     uint32_t format = ntohl(nsff->format);
1807
1808     ds_put_cstr(string, " format=");
1809     if (ofputil_nx_flow_format_is_valid(format)) {
1810         ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
1811     } else {
1812         ds_put_format(string, "%"PRIu32, format);
1813     }
1814 }
1815
1816 static void
1817 ofp_print_nxt_set_packet_in_format(struct ds *string,
1818                                    const struct nx_set_packet_in_format *nspf)
1819 {
1820     uint32_t format = ntohl(nspf->format);
1821
1822     ds_put_cstr(string, " format=");
1823     if (ofputil_packet_in_format_is_valid(format)) {
1824         ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1825     } else {
1826         ds_put_format(string, "%"PRIu32, format);
1827     }
1828 }
1829
1830 /* Returns a string form of 'reason'.  The return value is either a statically
1831  * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
1832  * 'bufsize' should be at least OFP_PORT_REASON_BUFSIZE. */
1833 #define OFP_PORT_REASON_BUFSIZE (INT_STRLEN(int) + 1)
1834 static const char *
1835 ofp_port_reason_to_string(enum ofp_port_reason reason,
1836                           char *reasonbuf, size_t bufsize)
1837 {
1838     switch (reason) {
1839     case OFPPR_ADD:
1840         return "add";
1841
1842     case OFPPR_DELETE:
1843         return "delete";
1844
1845     case OFPPR_MODIFY:
1846         return "modify";
1847
1848     default:
1849         snprintf(reasonbuf, bufsize, "%d", (int) reason);
1850         return reasonbuf;
1851     }
1852 }
1853
1854 static void
1855 ofp_print_nxt_set_async_config(struct ds *string,
1856                                const struct nx_async_config *nac)
1857 {
1858     int i;
1859
1860     for (i = 0; i < 2; i++) {
1861         int j;
1862
1863         ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1864
1865         ds_put_cstr(string, "       PACKET_IN:");
1866         for (j = 0; j < 32; j++) {
1867             if (nac->packet_in_mask[i] & htonl(1u << j)) {
1868                 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
1869                 const char *reason;
1870
1871                 reason = ofputil_packet_in_reason_to_string(j, reasonbuf,
1872                                                             sizeof reasonbuf);
1873                 ds_put_format(string, " %s", reason);
1874             }
1875         }
1876         if (!nac->packet_in_mask[i]) {
1877             ds_put_cstr(string, " (off)");
1878         }
1879         ds_put_char(string, '\n');
1880
1881         ds_put_cstr(string, "     PORT_STATUS:");
1882         for (j = 0; j < 32; j++) {
1883             if (nac->port_status_mask[i] & htonl(1u << j)) {
1884                 char reasonbuf[OFP_PORT_REASON_BUFSIZE];
1885                 const char *reason;
1886
1887                 reason = ofp_port_reason_to_string(j, reasonbuf,
1888                                                    sizeof reasonbuf);
1889                 ds_put_format(string, " %s", reason);
1890             }
1891         }
1892         if (!nac->port_status_mask[i]) {
1893             ds_put_cstr(string, " (off)");
1894         }
1895         ds_put_char(string, '\n');
1896
1897         ds_put_cstr(string, "    FLOW_REMOVED:");
1898         for (j = 0; j < 32; j++) {
1899             if (nac->flow_removed_mask[i] & htonl(1u << j)) {
1900                 char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
1901                 const char *reason;
1902
1903                 reason = ofp_flow_removed_reason_to_string(j, reasonbuf,
1904                                                            sizeof reasonbuf);
1905                 ds_put_format(string, " %s", reason);
1906             }
1907         }
1908         if (!nac->flow_removed_mask[i]) {
1909             ds_put_cstr(string, " (off)");
1910         }
1911         ds_put_char(string, '\n');
1912     }
1913 }
1914
1915 static void
1916 ofp_print_nxt_set_controller_id(struct ds *string,
1917                                 const struct nx_controller_id *nci)
1918 {
1919     ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
1920 }
1921
1922 static void
1923 ofp_print_nxt_flow_monitor_cancel(struct ds *string,
1924                                   const struct ofp_header *oh)
1925 {
1926     ds_put_format(string, " id=%"PRIu32,
1927                   ofputil_decode_flow_monitor_cancel(oh));
1928 }
1929
1930 static const char *
1931 nx_flow_monitor_flags_to_name(uint32_t bit)
1932 {
1933     enum nx_flow_monitor_flags fmf = bit;
1934
1935     switch (fmf) {
1936     case NXFMF_INITIAL: return "initial";
1937     case NXFMF_ADD: return "add";
1938     case NXFMF_DELETE: return "delete";
1939     case NXFMF_MODIFY: return "modify";
1940     case NXFMF_ACTIONS: return "actions";
1941     case NXFMF_OWN: return "own";
1942     }
1943
1944     return NULL;
1945 }
1946
1947 static void
1948 ofp_print_nxst_flow_monitor_request(struct ds *string,
1949                                     const struct ofp_header *oh)
1950 {
1951     struct ofpbuf b;
1952
1953     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1954     for (;;) {
1955         struct ofputil_flow_monitor_request request;
1956         int retval;
1957
1958         retval = ofputil_decode_flow_monitor_request(&request, &b);
1959         if (retval) {
1960             if (retval != EOF) {
1961                 ofp_print_error(string, retval);
1962             }
1963             return;
1964         }
1965
1966         ds_put_format(string, "\n id=%"PRIu32" flags=", request.id);
1967         ofp_print_bit_names(string, request.flags,
1968                             nx_flow_monitor_flags_to_name, ',');
1969
1970         if (request.out_port != OFPP_NONE) {
1971             ds_put_cstr(string, " out_port=");
1972             ofputil_format_port(request.out_port, string);
1973         }
1974
1975         if (request.table_id != 0xff) {
1976             ds_put_format(string, " table=%"PRIu8, request.table_id);
1977         }
1978
1979         ds_put_char(string, ' ');
1980         match_format(&request.match, string, OFP_DEFAULT_PRIORITY);
1981         ds_chomp(string, ' ');
1982     }
1983 }
1984
1985 static void
1986 ofp_print_nxst_flow_monitor_reply(struct ds *string,
1987                                   const struct ofp_header *oh)
1988 {
1989     uint64_t ofpacts_stub[1024 / 8];
1990     struct ofpbuf ofpacts;
1991     struct ofpbuf b;
1992
1993     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1994     ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1995     for (;;) {
1996         char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
1997         struct ofputil_flow_update update;
1998         struct match match;
1999         int retval;
2000
2001         update.match = &match;
2002         retval = ofputil_decode_flow_update(&update, &b, &ofpacts);
2003         if (retval) {
2004             if (retval != EOF) {
2005                 ofp_print_error(string, retval);
2006             }
2007             ofpbuf_uninit(&ofpacts);
2008             return;
2009         }
2010
2011         ds_put_cstr(string, "\n event=");
2012         switch (update.event) {
2013         case NXFME_ADDED:
2014             ds_put_cstr(string, "ADDED");
2015             break;
2016
2017         case NXFME_DELETED:
2018             ds_put_format(string, "DELETED reason=%s",
2019                           ofp_flow_removed_reason_to_string(update.reason,
2020                                                             reasonbuf,
2021                                                             sizeof reasonbuf));
2022             break;
2023
2024         case NXFME_MODIFIED:
2025             ds_put_cstr(string, "MODIFIED");
2026             break;
2027
2028         case NXFME_ABBREV:
2029             ds_put_format(string, "ABBREV xid=0x%"PRIx32, ntohl(update.xid));
2030             continue;
2031         }
2032
2033         ds_put_format(string, " table=%"PRIu8, update.table_id);
2034         if (update.idle_timeout != OFP_FLOW_PERMANENT) {
2035             ds_put_format(string, " idle_timeout=%"PRIu16,
2036                           update.idle_timeout);
2037         }
2038         if (update.hard_timeout != OFP_FLOW_PERMANENT) {
2039             ds_put_format(string, " hard_timeout=%"PRIu16,
2040                           update.hard_timeout);
2041         }
2042         ds_put_format(string, " cookie=%#"PRIx64, ntohll(update.cookie));
2043
2044         ds_put_char(string, ' ');
2045         match_format(update.match, string, OFP_DEFAULT_PRIORITY);
2046
2047         if (update.ofpacts_len) {
2048             if (string->string[string->length - 1] != ' ') {
2049                 ds_put_char(string, ' ');
2050             }
2051             ds_put_cstr(string, "actions=");
2052             ofpacts_format(update.ofpacts, update.ofpacts_len, string);
2053         }
2054     }
2055 }
2056
2057 void
2058 ofp_print_version(const struct ofp_header *oh,
2059                   struct ds *string)
2060 {
2061     switch (oh->version) {
2062     case OFP10_VERSION:
2063         break;
2064     case OFP11_VERSION:
2065         ds_put_cstr(string, " (OF1.1)");
2066         break;
2067     case OFP12_VERSION:
2068         ds_put_cstr(string, " (OF1.2)");
2069         break;
2070     case OFP13_VERSION:
2071         ds_put_cstr(string, " (OF1.3)");
2072         break;
2073     case OFP14_VERSION:
2074         ds_put_cstr(string, " (OF1.4)");
2075         break;
2076     case OFP15_VERSION:
2077         ds_put_cstr(string, " (OF1.5)");
2078         break;
2079     default:
2080         ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
2081         break;
2082     }
2083     ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
2084 }
2085
2086 static void
2087 ofp_header_to_string__(const struct ofp_header *oh, enum ofpraw raw,
2088                        struct ds *string)
2089 {
2090     ds_put_cstr(string, ofpraw_get_name(raw));
2091     ofp_print_version(oh, string);
2092 }
2093
2094 static void
2095 ofp_print_bucket_id(struct ds *s, const char *label, uint32_t bucket_id,
2096                     enum ofp_version ofp_version)
2097 {
2098     if (ofp_version < OFP15_VERSION) {
2099         return;
2100     }
2101
2102     ds_put_cstr(s, label);
2103
2104     switch (bucket_id) {
2105     case OFPG15_BUCKET_FIRST:
2106         ds_put_cstr(s, "first");
2107         break;
2108     case OFPG15_BUCKET_LAST:
2109         ds_put_cstr(s, "last");
2110         break;
2111     case OFPG15_BUCKET_ALL:
2112         ds_put_cstr(s, "all");
2113         break;
2114     default:
2115         ds_put_format(s, "%"PRIu32, bucket_id);
2116         break;
2117     }
2118
2119     ds_put_char(s, ',');
2120 }
2121
2122 static void
2123 ofp_print_group(struct ds *s, uint32_t group_id, uint8_t type,
2124                 struct ovs_list *p_buckets, struct ofputil_group_props *props,
2125                 enum ofp_version ofp_version, bool suppress_type)
2126 {
2127     struct ofputil_bucket *bucket;
2128
2129     ds_put_format(s, "group_id=%"PRIu32, group_id);
2130
2131     if (!suppress_type) {
2132         static const char *type_str[] = { "all", "select", "indirect",
2133                                           "ff", "unknown" };
2134         ds_put_format(s, ",type=%s", type_str[type > 4 ? 4 : type]);
2135     }
2136
2137     if (props->selection_method[0]) {
2138         size_t mark, start;
2139
2140         ds_put_format(s, ",selection_method=%s,", props->selection_method);
2141         if (props->selection_method_param) {
2142             ds_put_format(s, "selection_method_param=%"PRIu64",",
2143                           props->selection_method_param);
2144         }
2145
2146         /* Allow rewinding to immediately before the trailing ',' */
2147         mark = s->length - 1;
2148
2149         ds_put_cstr(s, "fields=");
2150         start = s->length;
2151         oxm_format_field_array(s, &props->fields);
2152         if (s->length == start) {
2153             ds_truncate(s, mark);
2154         }
2155     }
2156
2157     if (!p_buckets) {
2158         return;
2159     }
2160
2161     ds_put_char(s, ',');
2162
2163     LIST_FOR_EACH (bucket, list_node, p_buckets) {
2164         ds_put_cstr(s, "bucket=");
2165
2166         ofp_print_bucket_id(s, "bucket_id:", bucket->bucket_id, ofp_version);
2167         if (bucket->weight != 1) {
2168             ds_put_format(s, "weight:%"PRIu16",", bucket->weight);
2169         }
2170         if (bucket->watch_port != OFPP_NONE) {
2171             ds_put_format(s, "watch_port:%"PRIu32",", bucket->watch_port);
2172         }
2173         if (bucket->watch_group != OFPG11_ANY) {
2174             ds_put_format(s, "watch_group:%"PRIu32",", bucket->watch_group);
2175         }
2176
2177         ds_put_cstr(s, "actions=");
2178         ofpacts_format(bucket->ofpacts, bucket->ofpacts_len, s);
2179         ds_put_char(s, ',');
2180     }
2181
2182     ds_chomp(s, ',');
2183 }
2184
2185 static void
2186 ofp_print_ofpst_group_desc_request(struct ds *string,
2187                                    const struct ofp_header *oh)
2188 {
2189     uint32_t group_id = ofputil_decode_group_desc_request(oh);
2190     ds_put_cstr(string, " group_id=");
2191     ofputil_format_group(group_id, string);
2192 }
2193
2194 static void
2195 ofp_print_group_desc(struct ds *s, const struct ofp_header *oh)
2196 {
2197     struct ofpbuf b;
2198
2199     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2200     for (;;) {
2201         struct ofputil_group_desc gd;
2202         int retval;
2203
2204         retval = ofputil_decode_group_desc_reply(&gd, &b, oh->version);
2205         if (retval) {
2206             if (retval != EOF) {
2207                 ds_put_cstr(s, " ***parse error***");
2208             }
2209             break;
2210         }
2211
2212         ds_put_char(s, '\n');
2213         ds_put_char(s, ' ');
2214         ofp_print_group(s, gd.group_id, gd.type, &gd.buckets, &gd.props,
2215                         oh->version, false);
2216         ofputil_bucket_list_destroy(&gd.buckets);
2217      }
2218 }
2219
2220 static void
2221 ofp_print_ofpst_group_request(struct ds *string, const struct ofp_header *oh)
2222 {
2223     enum ofperr error;
2224     uint32_t group_id;
2225
2226     error = ofputil_decode_group_stats_request(oh, &group_id);
2227     if (error) {
2228         ofp_print_error(string, error);
2229         return;
2230     }
2231
2232     ds_put_cstr(string, " group_id=");
2233     ofputil_format_group(group_id, string);
2234 }
2235
2236 static void
2237 ofp_print_group_stats(struct ds *s, const struct ofp_header *oh)
2238 {
2239     struct ofpbuf b;
2240     uint32_t bucket_i;
2241
2242     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2243
2244     for (;;) {
2245         struct ofputil_group_stats gs;
2246         int retval;
2247
2248         retval = ofputil_decode_group_stats_reply(&b, &gs);
2249         if (retval) {
2250             if (retval != EOF) {
2251                 ds_put_cstr(s, " ***parse error***");
2252             }
2253             break;
2254         }
2255
2256         ds_put_char(s, '\n');
2257
2258         ds_put_char(s, ' ');
2259         ds_put_format(s, "group_id=%"PRIu32",", gs.group_id);
2260
2261         if (gs.duration_sec != UINT32_MAX) {
2262             ds_put_cstr(s, "duration=");
2263             ofp_print_duration(s, gs.duration_sec, gs.duration_nsec);
2264             ds_put_char(s, ',');
2265         }
2266         ds_put_format(s, "ref_count=%"PRIu32",", gs.ref_count);
2267         ds_put_format(s, "packet_count=%"PRIu64",", gs.packet_count);
2268         ds_put_format(s, "byte_count=%"PRIu64"", gs.byte_count);
2269
2270         for (bucket_i = 0; bucket_i < gs.n_buckets; bucket_i++) {
2271             if (gs.bucket_stats[bucket_i].packet_count != UINT64_MAX) {
2272                 ds_put_format(s, ",bucket%"PRIu32":", bucket_i);
2273                 ds_put_format(s, "packet_count=%"PRIu64",", gs.bucket_stats[bucket_i].packet_count);
2274                 ds_put_format(s, "byte_count=%"PRIu64"", gs.bucket_stats[bucket_i].byte_count);
2275             }
2276         }
2277
2278         free(gs.bucket_stats);
2279      }
2280 }
2281
2282 static const char *
2283 group_type_to_string(enum ofp11_group_type type)
2284 {
2285     switch (type) {
2286     case OFPGT11_ALL: return "all";
2287     case OFPGT11_SELECT: return "select";
2288     case OFPGT11_INDIRECT: return "indirect";
2289     case OFPGT11_FF: return "fast failover";
2290     default: OVS_NOT_REACHED();
2291     }
2292 }
2293
2294 static void
2295 ofp_print_group_features(struct ds *string, const struct ofp_header *oh)
2296 {
2297     struct ofputil_group_features features;
2298     int i;
2299
2300     ofputil_decode_group_features_reply(oh, &features);
2301
2302     ds_put_format(string, "\n Group table:\n");
2303     ds_put_format(string, "    Types:  0x%"PRIx32"\n", features.types);
2304     ds_put_format(string, "    Capabilities:  0x%"PRIx32"\n",
2305                   features.capabilities);
2306
2307     for (i = 0; i < OFPGT12_N_TYPES; i++) {
2308         if (features.types & (1u << i)) {
2309             ds_put_format(string, "    %s group:\n", group_type_to_string(i));
2310             ds_put_format(string, "       max_groups=%#"PRIx32"\n",
2311                           features.max_groups[i]);
2312             ds_put_format(string, "       actions: ");
2313             ofpact_bitmap_format(features.ofpacts[i], string);
2314             ds_put_char(string, '\n');
2315         }
2316     }
2317 }
2318
2319 static void
2320 ofp_print_group_mod(struct ds *s, const struct ofp_header *oh)
2321 {
2322     struct ofputil_group_mod gm;
2323     int error;
2324     bool bucket_command = false;
2325
2326     error = ofputil_decode_group_mod(oh, &gm);
2327     if (error) {
2328         ofp_print_error(s, error);
2329         return;
2330     }
2331
2332     ds_put_char(s, '\n');
2333
2334     ds_put_char(s, ' ');
2335     switch (gm.command) {
2336     case OFPGC11_ADD:
2337         ds_put_cstr(s, "ADD");
2338         break;
2339
2340     case OFPGC11_MODIFY:
2341         ds_put_cstr(s, "MOD");
2342         break;
2343
2344     case OFPGC11_DELETE:
2345         ds_put_cstr(s, "DEL");
2346         break;
2347
2348     case OFPGC15_INSERT_BUCKET:
2349         ds_put_cstr(s, "INSERT_BUCKET");
2350         bucket_command = true;
2351         break;
2352
2353     case OFPGC15_REMOVE_BUCKET:
2354         ds_put_cstr(s, "REMOVE_BUCKET");
2355         bucket_command = true;
2356         break;
2357
2358     default:
2359         ds_put_format(s, "cmd:%"PRIu16"", gm.command);
2360     }
2361     ds_put_char(s, ' ');
2362
2363     if (bucket_command) {
2364         ofp_print_bucket_id(s, "command_bucket_id:",
2365                             gm.command_bucket_id, oh->version);
2366     }
2367
2368     ofp_print_group(s, gm.group_id, gm.type, &gm.buckets, &gm.props,
2369                     oh->version, bucket_command);
2370     ofputil_bucket_list_destroy(&gm.buckets);
2371 }
2372
2373 static void
2374 print_table_action_features(struct ds *s,
2375                             const struct ofputil_table_action_features *taf)
2376 {
2377     if (taf->ofpacts) {
2378         ds_put_cstr(s, "        actions: ");
2379         ofpact_bitmap_format(taf->ofpacts, s);
2380         ds_put_char(s, '\n');
2381     }
2382
2383     if (!bitmap_is_all_zeros(taf->set_fields.bm, MFF_N_IDS)) {
2384         int i;
2385
2386         ds_put_cstr(s, "        supported on Set-Field:");
2387         BITMAP_FOR_EACH_1 (i, MFF_N_IDS, taf->set_fields.bm) {
2388             ds_put_format(s, " %s", mf_from_id(i)->name);
2389         }
2390         ds_put_char(s, '\n');
2391     }
2392 }
2393
2394 static bool
2395 table_action_features_equal(const struct ofputil_table_action_features *a,
2396                             const struct ofputil_table_action_features *b)
2397 {
2398     return (a->ofpacts == b->ofpacts
2399             && bitmap_equal(a->set_fields.bm, b->set_fields.bm, MFF_N_IDS));
2400 }
2401
2402 static bool
2403 table_action_features_empty(const struct ofputil_table_action_features *taf)
2404 {
2405     return !taf->ofpacts && bitmap_is_all_zeros(taf->set_fields.bm, MFF_N_IDS);
2406 }
2407
2408 static void
2409 print_table_instruction_features(
2410     struct ds *s, const struct ofputil_table_instruction_features *tif)
2411 {
2412     int start, end;
2413
2414     if (!bitmap_is_all_zeros(tif->next, 255)) {
2415         ds_put_cstr(s, "      next tables: ");
2416         for (start = bitmap_scan(tif->next, 1, 0, 255); start < 255;
2417              start = bitmap_scan(tif->next, 1, end, 255)) {
2418             end = bitmap_scan(tif->next, 0, start + 1, 255);
2419             if (end == start + 1) {
2420                 ds_put_format(s, "%d,", start);
2421             } else {
2422                 ds_put_format(s, "%d-%d,", start, end - 1);
2423             }
2424         }
2425         ds_chomp(s, ',');
2426         if (ds_last(s) == ' ') {
2427             ds_put_cstr(s, "none");
2428         }
2429         ds_put_char(s, '\n');
2430     }
2431
2432     if (tif->instructions) {
2433         ds_put_cstr(s, "      instructions: ");
2434         int i;
2435
2436         for (i = 0; i < 32; i++) {
2437             if (tif->instructions & (1u << i)) {
2438                 ds_put_format(s, "%s,", ovs_instruction_name_from_type(i));
2439             }
2440         }
2441         ds_chomp(s, ',');
2442         ds_put_char(s, '\n');
2443     }
2444
2445     if (!table_action_features_equal(&tif->write, &tif->apply)) {
2446         ds_put_cstr(s, "      Write-Actions features:\n");
2447         print_table_action_features(s, &tif->write);
2448         ds_put_cstr(s, "      Apply-Actions features:\n");
2449         print_table_action_features(s, &tif->apply);
2450     } else if (tif->write.ofpacts
2451                || !bitmap_is_all_zeros(tif->write.set_fields.bm, MFF_N_IDS)) {
2452         ds_put_cstr(s, "      Write-Actions and Apply-Actions features:\n");
2453         print_table_action_features(s, &tif->write);
2454     }
2455 }
2456
2457 static bool
2458 table_instruction_features_equal(
2459     const struct ofputil_table_instruction_features *a,
2460     const struct ofputil_table_instruction_features *b)
2461 {
2462     return (bitmap_equal(a->next, b->next, 255)
2463             && a->instructions == b->instructions
2464             && table_action_features_equal(&a->write, &b->write)
2465             && table_action_features_equal(&a->apply, &b->apply));
2466 }
2467
2468 static bool
2469 table_instruction_features_empty(
2470     const struct ofputil_table_instruction_features *tif)
2471 {
2472     return (bitmap_is_all_zeros(tif->next, 255)
2473             && !tif->instructions
2474             && table_action_features_empty(&tif->write)
2475             && table_action_features_empty(&tif->apply));
2476 }
2477
2478 static void
2479 ofp_print_table_features(struct ds *s,
2480                          const struct ofputil_table_features *features,
2481                          const struct ofputil_table_stats *stats)
2482 {
2483     int i;
2484
2485     ds_put_format(s, "\n  table %"PRIu8, features->table_id);
2486     if (features->name[0]) {
2487         ds_put_format(s, " (\"%s\")", features->name);
2488     }
2489     ds_put_cstr(s, ":\n");
2490     if (stats) {
2491         ds_put_format(s, "    active=%"PRIu32", ", stats->active_count);
2492         ds_put_format(s, "lookup=%"PRIu64", ", stats->lookup_count);
2493         ds_put_format(s, "matched=%"PRIu64"\n", stats->matched_count);
2494     }
2495     if (features->metadata_match || features->metadata_match) {
2496         ds_put_format(s, "    metadata: match=%#"PRIx64" write=%#"PRIx64"\n",
2497                       ntohll(features->metadata_match),
2498                       ntohll(features->metadata_write));
2499     }
2500
2501     if (features->miss_config != OFPUTIL_TABLE_MISS_DEFAULT) {
2502         ds_put_cstr(s, "    config=");
2503         ofp_print_table_miss_config(s, features->miss_config);
2504     }
2505
2506     if (features->max_entries) {
2507         ds_put_format(s, "    max_entries=%"PRIu32"\n", features->max_entries);
2508     }
2509
2510     if (!table_instruction_features_equal(&features->nonmiss,
2511                                           &features->miss)) {
2512         ds_put_cstr(s, "    instructions (other than table miss):\n");
2513         print_table_instruction_features(s, &features->nonmiss);
2514         ds_put_cstr(s, "    instructions (table miss):\n");
2515         print_table_instruction_features(s, &features->miss);
2516     } else if (!table_instruction_features_empty(&features->nonmiss)) {
2517         ds_put_cstr(s, "    instructions (table miss and others):\n");
2518         print_table_instruction_features(s, &features->nonmiss);
2519     }
2520
2521     if (!bitmap_is_all_zeros(features->match.bm, MFF_N_IDS)){
2522         ds_put_cstr(s, "    matching:\n");
2523         BITMAP_FOR_EACH_1 (i, MFF_N_IDS, features->match.bm) {
2524             const struct mf_field *f = mf_from_id(i);
2525             bool mask = bitmap_is_set(features->mask.bm, i);
2526             bool wildcard = bitmap_is_set(features->wildcard.bm, i);
2527
2528             ds_put_format(s, "      %s: %s\n",
2529                           f->name,
2530                           (mask ? "arbitrary mask"
2531                            : wildcard ? "exact match or wildcard"
2532                            : "must exact match"));
2533         }
2534     }
2535 }
2536
2537 static void
2538 ofp_print_table_features_reply(struct ds *s, const struct ofp_header *oh)
2539 {
2540     struct ofpbuf b;
2541
2542     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2543
2544     for (;;) {
2545         struct ofputil_table_features tf;
2546         int retval;
2547
2548         retval = ofputil_decode_table_features(&b, &tf, true);
2549         if (retval) {
2550             if (retval != EOF) {
2551                 ofp_print_error(s, retval);
2552             }
2553             return;
2554         }
2555         ofp_print_table_features(s, &tf, NULL);
2556     }
2557 }
2558
2559 static const char *
2560 bundle_flags_to_name(uint32_t bit)
2561 {
2562     switch (bit) {
2563     case OFPBF_ATOMIC:
2564         return "atomic";
2565     case OFPBF_ORDERED:
2566         return "ordered";
2567     default:
2568         return NULL;
2569     }
2570 }
2571
2572 static void
2573 ofp_print_bundle_ctrl(struct ds *s, const struct ofp_header *oh)
2574 {
2575     int error;
2576     struct ofputil_bundle_ctrl_msg bctrl;
2577
2578     error = ofputil_decode_bundle_ctrl(oh, &bctrl);
2579     if (error) {
2580         ofp_print_error(s, error);
2581         return;
2582     }
2583
2584     ds_put_char(s, '\n');
2585
2586     ds_put_format(s, " bundle_id=%#"PRIx32" type=",  bctrl.bundle_id);
2587     switch (bctrl.type) {
2588     case OFPBCT_OPEN_REQUEST:
2589         ds_put_cstr(s, "OPEN_REQUEST");
2590         break;
2591     case OFPBCT_OPEN_REPLY:
2592         ds_put_cstr(s, "OPEN_REPLY");
2593         break;
2594     case OFPBCT_CLOSE_REQUEST:
2595         ds_put_cstr(s, "CLOSE_REQUEST");
2596         break;
2597     case OFPBCT_CLOSE_REPLY:
2598         ds_put_cstr(s, "CLOSE_REPLY");
2599         break;
2600     case OFPBCT_COMMIT_REQUEST:
2601         ds_put_cstr(s, "COMMIT_REQUEST");
2602         break;
2603     case OFPBCT_COMMIT_REPLY:
2604         ds_put_cstr(s, "COMMIT_REPLY");
2605         break;
2606     case OFPBCT_DISCARD_REQUEST:
2607         ds_put_cstr(s, "DISCARD_REQUEST");
2608         break;
2609     case OFPBCT_DISCARD_REPLY:
2610         ds_put_cstr(s, "DISCARD_REPLY");
2611         break;
2612     }
2613
2614     ds_put_cstr(s, " flags=");
2615     ofp_print_bit_names(s, bctrl.flags, bundle_flags_to_name, ' ');
2616 }
2617
2618 static void
2619 ofp_print_bundle_add(struct ds *s, const struct ofp_header *oh, int verbosity)
2620 {
2621     int error;
2622     struct ofputil_bundle_add_msg badd;
2623     char *msg;
2624
2625     error = ofputil_decode_bundle_add(oh, &badd, NULL);
2626     if (error) {
2627         ofp_print_error(s, error);
2628         return;
2629     }
2630
2631     ds_put_char(s, '\n');
2632     ds_put_format(s, " bundle_id=%#"PRIx32,  badd.bundle_id);
2633     ds_put_cstr(s, " flags=");
2634     ofp_print_bit_names(s, badd.flags, bundle_flags_to_name, ' ');
2635
2636     ds_put_char(s, '\n');
2637     msg = ofp_to_string(badd.msg, ntohs(badd.msg->length), verbosity);
2638     if (msg) {
2639         ds_put_cstr(s, msg);
2640     }
2641 }
2642
2643 static void
2644 ofp_to_string__(const struct ofp_header *oh, enum ofpraw raw,
2645                 struct ds *string, int verbosity)
2646 {
2647     const void *msg = oh;
2648
2649     ofp_header_to_string__(oh, raw, string);
2650     switch (ofptype_from_ofpraw(raw)) {
2651
2652     case OFPTYPE_GROUP_STATS_REQUEST:
2653         ofp_print_stats(string, oh);
2654         ofp_print_ofpst_group_request(string, oh);
2655         break;
2656
2657     case OFPTYPE_GROUP_STATS_REPLY:
2658         ofp_print_group_stats(string, oh);
2659         break;
2660
2661     case OFPTYPE_GROUP_DESC_STATS_REQUEST:
2662         ofp_print_stats(string, oh);
2663         ofp_print_ofpst_group_desc_request(string, oh);
2664         break;
2665
2666     case OFPTYPE_GROUP_DESC_STATS_REPLY:
2667         ofp_print_group_desc(string, oh);
2668         break;
2669
2670     case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
2671         ofp_print_stats(string, oh);
2672         break;
2673
2674     case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
2675         ofp_print_group_features(string, oh);
2676         break;
2677
2678     case OFPTYPE_GROUP_MOD:
2679         ofp_print_group_mod(string, oh);
2680         break;
2681
2682     case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
2683     case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
2684         ofp_print_table_features_reply(string, oh);
2685         break;
2686
2687     case OFPTYPE_HELLO:
2688         ofp_print_hello(string, oh);
2689         break;
2690
2691     case OFPTYPE_ERROR:
2692         ofp_print_error_msg(string, oh);
2693         break;
2694
2695     case OFPTYPE_ECHO_REQUEST:
2696     case OFPTYPE_ECHO_REPLY:
2697         ofp_print_echo(string, oh, verbosity);
2698         break;
2699
2700     case OFPTYPE_FEATURES_REQUEST:
2701         break;
2702
2703     case OFPTYPE_FEATURES_REPLY:
2704         ofp_print_switch_features(string, oh);
2705         break;
2706
2707     case OFPTYPE_GET_CONFIG_REQUEST:
2708         break;
2709
2710     case OFPTYPE_GET_CONFIG_REPLY:
2711     case OFPTYPE_SET_CONFIG:
2712         ofp_print_switch_config(string, ofpmsg_body(oh));
2713         break;
2714
2715     case OFPTYPE_PACKET_IN:
2716         ofp_print_packet_in(string, oh, verbosity);
2717         break;
2718
2719     case OFPTYPE_FLOW_REMOVED:
2720         ofp_print_flow_removed(string, oh);
2721         break;
2722
2723     case OFPTYPE_PORT_STATUS:
2724         ofp_print_port_status(string, oh);
2725         break;
2726
2727     case OFPTYPE_PACKET_OUT:
2728         ofp_print_packet_out(string, oh, verbosity);
2729         break;
2730
2731     case OFPTYPE_FLOW_MOD:
2732         ofp_print_flow_mod(string, oh, verbosity);
2733         break;
2734
2735     case OFPTYPE_PORT_MOD:
2736         ofp_print_port_mod(string, oh);
2737         break;
2738
2739     case OFPTYPE_TABLE_MOD:
2740         ofp_print_table_mod(string, oh);
2741         break;
2742
2743     case OFPTYPE_METER_MOD:
2744         ofp_print_meter_mod(string, oh);
2745         break;
2746
2747     case OFPTYPE_BARRIER_REQUEST:
2748     case OFPTYPE_BARRIER_REPLY:
2749         break;
2750
2751     case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
2752         ofp_print_queue_get_config_request(string, oh);
2753         break;
2754
2755     case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
2756         ofp_print_queue_get_config_reply(string, oh);
2757         break;
2758
2759     case OFPTYPE_ROLE_REQUEST:
2760     case OFPTYPE_ROLE_REPLY:
2761         ofp_print_role_message(string, oh);
2762         break;
2763     case OFPTYPE_ROLE_STATUS:
2764         ofp_print_role_status_message(string, oh);
2765         break;
2766
2767     case OFPTYPE_METER_STATS_REQUEST:
2768     case OFPTYPE_METER_CONFIG_STATS_REQUEST:
2769         ofp_print_stats(string, oh);
2770         ofp_print_meter_stats_request(string, oh);
2771         break;
2772
2773     case OFPTYPE_METER_STATS_REPLY:
2774         ofp_print_stats(string, oh);
2775         ofp_print_meter_stats_reply(string, oh);
2776         break;
2777
2778     case OFPTYPE_METER_CONFIG_STATS_REPLY:
2779         ofp_print_stats(string, oh);
2780         ofp_print_meter_config_reply(string, oh);
2781         break;
2782
2783     case OFPTYPE_METER_FEATURES_STATS_REPLY:
2784         ofp_print_stats(string, oh);
2785         ofp_print_meter_features_reply(string, oh);
2786         break;
2787
2788     case OFPTYPE_DESC_STATS_REQUEST:
2789     case OFPTYPE_METER_FEATURES_STATS_REQUEST:
2790         ofp_print_stats(string, oh);
2791         break;
2792
2793     case OFPTYPE_FLOW_STATS_REQUEST:
2794     case OFPTYPE_AGGREGATE_STATS_REQUEST:
2795         ofp_print_stats(string, oh);
2796         ofp_print_flow_stats_request(string, oh);
2797         break;
2798
2799     case OFPTYPE_TABLE_STATS_REQUEST:
2800         ofp_print_stats(string, oh);
2801         break;
2802
2803     case OFPTYPE_PORT_STATS_REQUEST:
2804         ofp_print_stats(string, oh);
2805         ofp_print_ofpst_port_request(string, oh);
2806         break;
2807
2808     case OFPTYPE_QUEUE_STATS_REQUEST:
2809         ofp_print_stats(string, oh);
2810         ofp_print_ofpst_queue_request(string, oh);
2811         break;
2812
2813     case OFPTYPE_DESC_STATS_REPLY:
2814         ofp_print_stats(string, oh);
2815         ofp_print_ofpst_desc_reply(string, oh);
2816         break;
2817
2818     case OFPTYPE_FLOW_STATS_REPLY:
2819         ofp_print_stats(string, oh);
2820         ofp_print_flow_stats_reply(string, oh);
2821         break;
2822
2823     case OFPTYPE_QUEUE_STATS_REPLY:
2824         ofp_print_stats(string, oh);
2825         ofp_print_ofpst_queue_reply(string, oh, verbosity);
2826         break;
2827
2828     case OFPTYPE_PORT_STATS_REPLY:
2829         ofp_print_stats(string, oh);
2830         ofp_print_ofpst_port_reply(string, oh, verbosity);
2831         break;
2832
2833     case OFPTYPE_TABLE_STATS_REPLY:
2834         ofp_print_stats(string, oh);
2835         ofp_print_table_stats_reply(string, oh);
2836         break;
2837
2838     case OFPTYPE_AGGREGATE_STATS_REPLY:
2839         ofp_print_stats(string, oh);
2840         ofp_print_aggregate_stats_reply(string, oh);
2841         break;
2842
2843     case OFPTYPE_PORT_DESC_STATS_REQUEST:
2844         ofp_print_stats(string, oh);
2845         ofp_print_ofpst_port_desc_request(string, oh);
2846         break;
2847
2848     case OFPTYPE_PORT_DESC_STATS_REPLY:
2849         ofp_print_stats(string, oh);
2850         ofp_print_ofpst_port_desc_reply(string, oh);
2851         break;
2852
2853     case OFPTYPE_FLOW_MOD_TABLE_ID:
2854         ofp_print_nxt_flow_mod_table_id(string, ofpmsg_body(oh));
2855         break;
2856
2857     case OFPTYPE_SET_FLOW_FORMAT:
2858         ofp_print_nxt_set_flow_format(string, ofpmsg_body(oh));
2859         break;
2860
2861     case OFPTYPE_SET_PACKET_IN_FORMAT:
2862         ofp_print_nxt_set_packet_in_format(string, ofpmsg_body(oh));
2863         break;
2864
2865     case OFPTYPE_FLOW_AGE:
2866         break;
2867
2868     case OFPTYPE_SET_CONTROLLER_ID:
2869         ofp_print_nxt_set_controller_id(string, ofpmsg_body(oh));
2870         break;
2871
2872     case OFPTYPE_GET_ASYNC_REPLY:
2873     case OFPTYPE_SET_ASYNC_CONFIG:
2874         ofp_print_nxt_set_async_config(string, ofpmsg_body(oh));
2875         break;
2876     case OFPTYPE_GET_ASYNC_REQUEST:
2877         break;
2878     case OFPTYPE_FLOW_MONITOR_CANCEL:
2879         ofp_print_nxt_flow_monitor_cancel(string, msg);
2880         break;
2881
2882     case OFPTYPE_FLOW_MONITOR_PAUSED:
2883     case OFPTYPE_FLOW_MONITOR_RESUMED:
2884         break;
2885
2886     case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
2887         ofp_print_nxst_flow_monitor_request(string, msg);
2888         break;
2889
2890     case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
2891         ofp_print_nxst_flow_monitor_reply(string, msg);
2892         break;
2893
2894     case OFPTYPE_BUNDLE_CONTROL:
2895         ofp_print_bundle_ctrl(string, msg);
2896         break;
2897
2898     case OFPTYPE_BUNDLE_ADD_MESSAGE:
2899         ofp_print_bundle_add(string, msg, verbosity);
2900         break;
2901     }
2902 }
2903
2904 /* Composes and returns a string representing the OpenFlow packet of 'len'
2905  * bytes at 'oh' at the given 'verbosity' level.  0 is a minimal amount of
2906  * verbosity and higher numbers increase verbosity.  The caller is responsible
2907  * for freeing the string. */
2908 char *
2909 ofp_to_string(const void *oh_, size_t len, int verbosity)
2910 {
2911     struct ds string = DS_EMPTY_INITIALIZER;
2912     const struct ofp_header *oh = oh_;
2913
2914     if (!len) {
2915         ds_put_cstr(&string, "OpenFlow message is empty\n");
2916     } else if (len < sizeof(struct ofp_header)) {
2917         ds_put_format(&string, "OpenFlow packet too short (only %"PRIuSIZE" bytes):\n",
2918                       len);
2919     } else if (ntohs(oh->length) > len) {
2920         enum ofperr error;
2921         enum ofpraw raw;
2922
2923         error = ofpraw_decode_partial(&raw, oh, len);
2924         if (!error) {
2925             ofp_header_to_string__(oh, raw, &string);
2926             ds_put_char(&string, '\n');
2927         }
2928
2929         ds_put_format(&string,
2930                       "(***truncated to %"PRIuSIZE" bytes from %"PRIu16"***)\n",
2931                       len, ntohs(oh->length));
2932     } else if (ntohs(oh->length) < len) {
2933         ds_put_format(&string,
2934                       "(***only uses %"PRIu16" bytes out of %"PRIuSIZE"***)\n",
2935                       ntohs(oh->length), len);
2936     } else {
2937         enum ofperr error;
2938         enum ofpraw raw;
2939
2940         error = ofpraw_decode(&raw, oh);
2941         if (!error) {
2942             ofp_to_string__(oh, raw, &string, verbosity);
2943             if (verbosity >= 5) {
2944                 if (ds_last(&string) != '\n') {
2945                     ds_put_char(&string, '\n');
2946                 }
2947                 ds_put_hex_dump(&string, oh, len, 0, true);
2948             }
2949             if (ds_last(&string) != '\n') {
2950                 ds_put_char(&string, '\n');
2951             }
2952             return ds_steal_cstr(&string);
2953         }
2954
2955         ofp_print_error(&string, error);
2956     }
2957     ds_put_hex_dump(&string, oh, len, 0, true);
2958     return ds_steal_cstr(&string);
2959 }
2960 \f
2961 static void
2962 print_and_free(FILE *stream, char *string)
2963 {
2964     fputs(string, stream);
2965     free(string);
2966 }
2967
2968 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
2969  * given 'verbosity' level.  0 is a minimal amount of verbosity and higher
2970  * numbers increase verbosity. */
2971 void
2972 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
2973 {
2974     print_and_free(stream, ofp_to_string(oh, len, verbosity));
2975 }
2976
2977 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
2978  * 'data' to 'stream'. */
2979 void
2980 ofp_print_packet(FILE *stream, const void *data, size_t len)
2981 {
2982     print_and_free(stream, ofp_packet_to_string(data, len));
2983 }