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