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