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