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