790d5867a6d563a92f4b7ca65f8eaaeeb4b6515e
[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_request(struct ds *string,
1904                                   const struct ofp_header *oh)
1905 {
1906     enum ofperr error;
1907     ofp_port_t port;
1908
1909     error = ofputil_decode_port_desc_stats_request(oh, &port);
1910     if (error) {
1911         ofp_print_error(string, error);
1912         return;
1913     }
1914
1915     ds_put_cstr(string, " port=");
1916     ofputil_format_port(port, string);
1917 }
1918
1919 static void
1920 ofp_print_ofpst_port_desc_reply(struct ds *string,
1921                                 const struct ofp_header *oh)
1922 {
1923     struct ofpbuf b;
1924
1925     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1926     ofpraw_pull_assert(&b);
1927     ds_put_char(string, '\n');
1928     ofp_print_phy_ports(string, oh->version, &b);
1929 }
1930
1931 static void
1932 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1933 {
1934     uint16_t flags = ofpmp_flags(oh);
1935
1936     if (flags) {
1937         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
1938     }
1939 }
1940
1941 static void
1942 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1943 {
1944     uint16_t flags = ofpmp_flags(oh);
1945
1946     if (flags) {
1947         ds_put_cstr(string, " flags=");
1948         if (flags & OFPSF_REPLY_MORE) {
1949             ds_put_cstr(string, "[more]");
1950             flags &= ~OFPSF_REPLY_MORE;
1951         }
1952         if (flags) {
1953             ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1954                           flags);
1955         }
1956     }
1957 }
1958
1959 static void
1960 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1961 {
1962     size_t len = ntohs(oh->length);
1963
1964     ds_put_format(string, " %"PRIuSIZE" bytes of payload\n", len - sizeof *oh);
1965     if (verbosity > 1) {
1966         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1967     }
1968 }
1969
1970 static void
1971 ofp_print_role_generic(struct ds *string, enum ofp12_controller_role role,
1972                        uint64_t generation_id)
1973 {
1974     ds_put_cstr(string, " role=");
1975
1976     switch (role) {
1977     case OFPCR12_ROLE_NOCHANGE:
1978         ds_put_cstr(string, "nochange");
1979         break;
1980     case OFPCR12_ROLE_EQUAL:
1981         ds_put_cstr(string, "equal"); /* OF 1.2 wording */
1982         break;
1983     case OFPCR12_ROLE_MASTER:
1984         ds_put_cstr(string, "master");
1985         break;
1986     case OFPCR12_ROLE_SLAVE:
1987         ds_put_cstr(string, "slave");
1988         break;
1989     default:
1990         OVS_NOT_REACHED();
1991     }
1992
1993     if (generation_id != UINT64_MAX) {
1994         ds_put_format(string, " generation_id=%"PRIu64, generation_id);
1995     }
1996 }
1997
1998 static void
1999 ofp_print_role_message(struct ds *string, const struct ofp_header *oh)
2000 {
2001     struct ofputil_role_request rr;
2002     enum ofperr error;
2003
2004     error = ofputil_decode_role_message(oh, &rr);
2005     if (error) {
2006         ofp_print_error(string, error);
2007         return;
2008     }
2009
2010     ofp_print_role_generic(string, rr.role, rr.have_generation_id ? rr.generation_id : UINT64_MAX);
2011 }
2012
2013 static void
2014 ofp_print_role_status_message(struct ds *string, const struct ofp_header *oh)
2015 {
2016     struct ofputil_role_status rs;
2017     enum ofperr error;
2018
2019     error = ofputil_decode_role_status(oh, &rs);
2020     if (error) {
2021         ofp_print_error(string, error);
2022         return;
2023     }
2024
2025     ofp_print_role_generic(string, rs.role, rs.generation_id);
2026
2027     ds_put_cstr(string, " reason=");
2028
2029     switch (rs.reason) {
2030     case OFPCRR_MASTER_REQUEST:
2031         ds_put_cstr(string, "master_request");
2032         break;
2033     case OFPCRR_CONFIG:
2034         ds_put_cstr(string, "configuration_changed");
2035         break;
2036     case OFPCRR_EXPERIMENTER:
2037         ds_put_cstr(string, "experimenter_data_changed");
2038         break;
2039     default:
2040         OVS_NOT_REACHED();
2041     }
2042 }
2043
2044 static void
2045 ofp_print_nxt_flow_mod_table_id(struct ds *string,
2046                                 const struct nx_flow_mod_table_id *nfmti)
2047 {
2048     ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
2049 }
2050
2051 static void
2052 ofp_print_nxt_set_flow_format(struct ds *string,
2053                               const struct nx_set_flow_format *nsff)
2054 {
2055     uint32_t format = ntohl(nsff->format);
2056
2057     ds_put_cstr(string, " format=");
2058     if (ofputil_nx_flow_format_is_valid(format)) {
2059         ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
2060     } else {
2061         ds_put_format(string, "%"PRIu32, format);
2062     }
2063 }
2064
2065 static void
2066 ofp_print_nxt_set_packet_in_format(struct ds *string,
2067                                    const struct nx_set_packet_in_format *nspf)
2068 {
2069     uint32_t format = ntohl(nspf->format);
2070
2071     ds_put_cstr(string, " format=");
2072     if (ofputil_packet_in_format_is_valid(format)) {
2073         ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
2074     } else {
2075         ds_put_format(string, "%"PRIu32, format);
2076     }
2077 }
2078
2079 /* Returns a string form of 'reason'.  The return value is either a statically
2080  * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
2081  * 'bufsize' should be at least OFP_PORT_REASON_BUFSIZE. */
2082 #define OFP_PORT_REASON_BUFSIZE (INT_STRLEN(int) + 1)
2083 static const char *
2084 ofp_port_reason_to_string(enum ofp_port_reason reason,
2085                           char *reasonbuf, size_t bufsize)
2086 {
2087     switch (reason) {
2088     case OFPPR_ADD:
2089         return "add";
2090
2091     case OFPPR_DELETE:
2092         return "delete";
2093
2094     case OFPPR_MODIFY:
2095         return "modify";
2096
2097     default:
2098         snprintf(reasonbuf, bufsize, "%d", (int) reason);
2099         return reasonbuf;
2100     }
2101 }
2102
2103 static void
2104 ofp_print_nxt_set_async_config(struct ds *string,
2105                                const struct nx_async_config *nac)
2106 {
2107     int i;
2108
2109     for (i = 0; i < 2; i++) {
2110         int j;
2111
2112         ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
2113
2114         ds_put_cstr(string, "       PACKET_IN:");
2115         for (j = 0; j < 32; j++) {
2116             if (nac->packet_in_mask[i] & htonl(1u << j)) {
2117                 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
2118                 const char *reason;
2119
2120                 reason = ofputil_packet_in_reason_to_string(j, reasonbuf,
2121                                                             sizeof reasonbuf);
2122                 ds_put_format(string, " %s", reason);
2123             }
2124         }
2125         if (!nac->packet_in_mask[i]) {
2126             ds_put_cstr(string, " (off)");
2127         }
2128         ds_put_char(string, '\n');
2129
2130         ds_put_cstr(string, "     PORT_STATUS:");
2131         for (j = 0; j < 32; j++) {
2132             if (nac->port_status_mask[i] & htonl(1u << j)) {
2133                 char reasonbuf[OFP_PORT_REASON_BUFSIZE];
2134                 const char *reason;
2135
2136                 reason = ofp_port_reason_to_string(j, reasonbuf,
2137                                                    sizeof reasonbuf);
2138                 ds_put_format(string, " %s", reason);
2139             }
2140         }
2141         if (!nac->port_status_mask[i]) {
2142             ds_put_cstr(string, " (off)");
2143         }
2144         ds_put_char(string, '\n');
2145
2146         ds_put_cstr(string, "    FLOW_REMOVED:");
2147         for (j = 0; j < 32; j++) {
2148             if (nac->flow_removed_mask[i] & htonl(1u << j)) {
2149                 char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
2150                 const char *reason;
2151
2152                 reason = ofp_flow_removed_reason_to_string(j, reasonbuf,
2153                                                            sizeof reasonbuf);
2154                 ds_put_format(string, " %s", reason);
2155             }
2156         }
2157         if (!nac->flow_removed_mask[i]) {
2158             ds_put_cstr(string, " (off)");
2159         }
2160         ds_put_char(string, '\n');
2161     }
2162 }
2163
2164 static void
2165 ofp_print_nxt_set_controller_id(struct ds *string,
2166                                 const struct nx_controller_id *nci)
2167 {
2168     ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
2169 }
2170
2171 static void
2172 ofp_print_nxt_flow_monitor_cancel(struct ds *string,
2173                                   const struct ofp_header *oh)
2174 {
2175     ds_put_format(string, " id=%"PRIu32,
2176                   ofputil_decode_flow_monitor_cancel(oh));
2177 }
2178
2179 static const char *
2180 nx_flow_monitor_flags_to_name(uint32_t bit)
2181 {
2182     enum nx_flow_monitor_flags fmf = bit;
2183
2184     switch (fmf) {
2185     case NXFMF_INITIAL: return "initial";
2186     case NXFMF_ADD: return "add";
2187     case NXFMF_DELETE: return "delete";
2188     case NXFMF_MODIFY: return "modify";
2189     case NXFMF_ACTIONS: return "actions";
2190     case NXFMF_OWN: return "own";
2191     }
2192
2193     return NULL;
2194 }
2195
2196 static void
2197 ofp_print_nxst_flow_monitor_request(struct ds *string,
2198                                     const struct ofp_header *oh)
2199 {
2200     struct ofpbuf b;
2201
2202     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2203     for (;;) {
2204         struct ofputil_flow_monitor_request request;
2205         int retval;
2206
2207         retval = ofputil_decode_flow_monitor_request(&request, &b);
2208         if (retval) {
2209             if (retval != EOF) {
2210                 ofp_print_error(string, retval);
2211             }
2212             return;
2213         }
2214
2215         ds_put_format(string, "\n id=%"PRIu32" flags=", request.id);
2216         ofp_print_bit_names(string, request.flags,
2217                             nx_flow_monitor_flags_to_name, ',');
2218
2219         if (request.out_port != OFPP_NONE) {
2220             ds_put_cstr(string, " out_port=");
2221             ofputil_format_port(request.out_port, string);
2222         }
2223
2224         if (request.table_id != 0xff) {
2225             ds_put_format(string, " table=%"PRIu8, request.table_id);
2226         }
2227
2228         ds_put_char(string, ' ');
2229         match_format(&request.match, string, OFP_DEFAULT_PRIORITY);
2230         ds_chomp(string, ' ');
2231     }
2232 }
2233
2234 static void
2235 ofp_print_nxst_flow_monitor_reply(struct ds *string,
2236                                   const struct ofp_header *oh)
2237 {
2238     uint64_t ofpacts_stub[1024 / 8];
2239     struct ofpbuf ofpacts;
2240     struct ofpbuf b;
2241
2242     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2243     ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2244     for (;;) {
2245         char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
2246         struct ofputil_flow_update update;
2247         struct match match;
2248         int retval;
2249
2250         update.match = &match;
2251         retval = ofputil_decode_flow_update(&update, &b, &ofpacts);
2252         if (retval) {
2253             if (retval != EOF) {
2254                 ofp_print_error(string, retval);
2255             }
2256             ofpbuf_uninit(&ofpacts);
2257             return;
2258         }
2259
2260         ds_put_cstr(string, "\n event=");
2261         switch (update.event) {
2262         case NXFME_ADDED:
2263             ds_put_cstr(string, "ADDED");
2264             break;
2265
2266         case NXFME_DELETED:
2267             ds_put_format(string, "DELETED reason=%s",
2268                           ofp_flow_removed_reason_to_string(update.reason,
2269                                                             reasonbuf,
2270                                                             sizeof reasonbuf));
2271             break;
2272
2273         case NXFME_MODIFIED:
2274             ds_put_cstr(string, "MODIFIED");
2275             break;
2276
2277         case NXFME_ABBREV:
2278             ds_put_format(string, "ABBREV xid=0x%"PRIx32, ntohl(update.xid));
2279             continue;
2280         }
2281
2282         ds_put_format(string, " table=%"PRIu8, update.table_id);
2283         if (update.idle_timeout != OFP_FLOW_PERMANENT) {
2284             ds_put_format(string, " idle_timeout=%"PRIu16,
2285                           update.idle_timeout);
2286         }
2287         if (update.hard_timeout != OFP_FLOW_PERMANENT) {
2288             ds_put_format(string, " hard_timeout=%"PRIu16,
2289                           update.hard_timeout);
2290         }
2291         ds_put_format(string, " cookie=%#"PRIx64, ntohll(update.cookie));
2292
2293         ds_put_char(string, ' ');
2294         match_format(update.match, string, OFP_DEFAULT_PRIORITY);
2295
2296         if (update.ofpacts_len) {
2297             if (string->string[string->length - 1] != ' ') {
2298                 ds_put_char(string, ' ');
2299             }
2300             ds_put_cstr(string, "actions=");
2301             ofpacts_format(update.ofpacts, update.ofpacts_len, string);
2302         }
2303     }
2304 }
2305
2306 void
2307 ofp_print_version(const struct ofp_header *oh,
2308                   struct ds *string)
2309 {
2310     switch (oh->version) {
2311     case OFP10_VERSION:
2312         break;
2313     case OFP11_VERSION:
2314         ds_put_cstr(string, " (OF1.1)");
2315         break;
2316     case OFP12_VERSION:
2317         ds_put_cstr(string, " (OF1.2)");
2318         break;
2319     case OFP13_VERSION:
2320         ds_put_cstr(string, " (OF1.3)");
2321         break;
2322     case OFP14_VERSION:
2323         ds_put_cstr(string, " (OF1.4)");
2324         break;
2325     case OFP15_VERSION:
2326         ds_put_cstr(string, " (OF1.5)");
2327         break;
2328     default:
2329         ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
2330         break;
2331     }
2332     ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
2333 }
2334
2335 static void
2336 ofp_header_to_string__(const struct ofp_header *oh, enum ofpraw raw,
2337                        struct ds *string)
2338 {
2339     ds_put_cstr(string, ofpraw_get_name(raw));
2340     ofp_print_version(oh, string);
2341 }
2342
2343 static void
2344 ofp_print_group(struct ds *s, uint32_t group_id, uint8_t type,
2345                 struct list *p_buckets)
2346 {
2347     static const char *type_str[] = { "all", "select", "indirect",
2348                                       "ff", "unknown" };
2349     struct ofputil_bucket *bucket;
2350
2351     ds_put_format(s, "group_id=%"PRIu32",type=%s",
2352                   group_id, type_str[type > 4 ? 4 : type]);
2353     if (!p_buckets) {
2354         return;
2355     }
2356
2357     LIST_FOR_EACH (bucket, list_node, p_buckets) {
2358         ds_put_cstr(s, ",bucket=");
2359
2360         if (bucket->weight != 1) {
2361             ds_put_format(s, "weight:%"PRIu16",", bucket->weight);
2362         }
2363         if (bucket->watch_port != OFPP_NONE) {
2364             ds_put_format(s, "watch_port:%"PRIu32",", bucket->watch_port);
2365         }
2366         if (bucket->watch_group != OFPG11_ANY) {
2367             ds_put_format(s, "watch_group:%"PRIu32",", bucket->watch_group);
2368         }
2369
2370         ds_put_cstr(s, "actions=");
2371         ofpacts_format(bucket->ofpacts, bucket->ofpacts_len, s);
2372     }
2373 }
2374
2375 static void
2376 ofp_print_ofpst_group_desc_request(struct ds *string,
2377                                    const struct ofp_header *oh)
2378 {
2379     uint32_t group_id = ofputil_decode_group_desc_request(oh);
2380     ds_put_cstr(string, " group_id=");
2381     ofputil_format_group(group_id, string);
2382 }
2383
2384 static void
2385 ofp_print_group_desc(struct ds *s, const struct ofp_header *oh)
2386 {
2387     struct ofpbuf b;
2388
2389     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2390     for (;;) {
2391         struct ofputil_group_desc gd;
2392         int retval;
2393
2394         retval = ofputil_decode_group_desc_reply(&gd, &b, oh->version);
2395         if (retval) {
2396             if (retval != EOF) {
2397                 ds_put_cstr(s, " ***parse error***");
2398             }
2399             break;
2400         }
2401
2402         ds_put_char(s, '\n');
2403         ds_put_char(s, ' ');
2404         ofp_print_group(s, gd.group_id, gd.type, &gd.buckets);
2405      }
2406 }
2407
2408 static void
2409 ofp_print_ofpst_group_request(struct ds *string, const struct ofp_header *oh)
2410 {
2411     enum ofperr error;
2412     uint32_t group_id;
2413
2414     error = ofputil_decode_group_stats_request(oh, &group_id);
2415     if (error) {
2416         ofp_print_error(string, error);
2417         return;
2418     }
2419
2420     ds_put_cstr(string, " group_id=");
2421     ofputil_format_group(group_id, string);
2422 }
2423
2424 static void
2425 ofp_print_group_stats(struct ds *s, const struct ofp_header *oh)
2426 {
2427     struct ofpbuf b;
2428     uint32_t bucket_i;
2429
2430     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2431
2432     for (;;) {
2433         struct ofputil_group_stats gs;
2434         int retval;
2435
2436         retval = ofputil_decode_group_stats_reply(&b, &gs);
2437         if (retval) {
2438             if (retval != EOF) {
2439                 ds_put_cstr(s, " ***parse error***");
2440             }
2441             break;
2442         }
2443
2444         ds_put_char(s, '\n');
2445
2446         ds_put_char(s, ' ');
2447         ds_put_format(s, "group_id=%"PRIu32",", gs.group_id);
2448
2449         if (gs.duration_sec != UINT32_MAX) {
2450             ds_put_cstr(s, "duration=");
2451             ofp_print_duration(s, gs.duration_sec, gs.duration_nsec);
2452             ds_put_char(s, ',');
2453         }
2454         ds_put_format(s, "ref_count=%"PRIu32",", gs.ref_count);
2455         ds_put_format(s, "packet_count=%"PRIu64",", gs.packet_count);
2456         ds_put_format(s, "byte_count=%"PRIu64"", gs.byte_count);
2457
2458         for (bucket_i = 0; bucket_i < gs.n_buckets; bucket_i++) {
2459             if (gs.bucket_stats[bucket_i].packet_count != UINT64_MAX) {
2460                 ds_put_format(s, ",bucket%"PRIu32":", bucket_i);
2461                 ds_put_format(s, "packet_count=%"PRIu64",", gs.bucket_stats[bucket_i].packet_count);
2462                 ds_put_format(s, "byte_count=%"PRIu64"", gs.bucket_stats[bucket_i].byte_count);
2463             }
2464         }
2465
2466         free(gs.bucket_stats);
2467      }
2468 }
2469
2470 static void
2471 ofp_print_group_features(struct ds *string, const struct ofp_header *oh)
2472 {
2473     struct ofputil_group_features features;
2474
2475     ofputil_decode_group_features_reply(oh, &features);
2476
2477     ds_put_format(string, "\n Group table:\n");
2478     ds_put_format(string, "    Types:  0x%"PRIx32"\n", features.types);
2479     ds_put_format(string, "    Capabilities:  0x%"PRIx32"\n",
2480                   features.capabilities);
2481
2482     if (features.types & (1u << OFPGT11_ALL)) {
2483         ds_put_format(string, "    All group :\n");
2484         ds_put_format(string,
2485                       "        max_groups = %#"PRIx32" actions=0x%08"PRIx32"\n",
2486                       features.max_groups[0], features.actions[0]);
2487     }
2488
2489     if (features.types & (1u << OFPGT11_SELECT)) {
2490         ds_put_format(string, "    Select group :\n");
2491         ds_put_format(string, "        max_groups = %#"PRIx32" "
2492                       "actions=0x%08"PRIx32"\n",
2493                       features.max_groups[1], features.actions[1]);
2494     }
2495
2496     if (features.types & (1u << OFPGT11_INDIRECT)) {
2497         ds_put_format(string, "    Indirect group :\n");
2498         ds_put_format(string, "        max_groups = %#"PRIx32" "
2499                       "actions=0x%08"PRIx32"\n",
2500                       features.max_groups[2], features.actions[2]);
2501     }
2502
2503     if (features.types & (1u << OFPGT11_FF)) {
2504         ds_put_format(string, "    Fast Failover group :\n");
2505         ds_put_format(string, "        max_groups = %#"PRIx32" "
2506                       "actions=0x%08"PRIx32"\n",
2507                       features.max_groups[3], features.actions[3]);
2508     }
2509 }
2510
2511 static void
2512 ofp_print_group_mod(struct ds *s, const struct ofp_header *oh)
2513 {
2514     struct ofputil_group_mod gm;
2515     int error;
2516
2517     error = ofputil_decode_group_mod(oh, &gm);
2518     if (error) {
2519         ofp_print_error(s, error);
2520         return;
2521     }
2522
2523     ds_put_char(s, '\n');
2524
2525     ds_put_char(s, ' ');
2526     switch (gm.command) {
2527     case OFPGC11_ADD:
2528         ds_put_cstr(s, "ADD");
2529         break;
2530
2531     case OFPGC11_MODIFY:
2532         ds_put_cstr(s, "MOD");
2533         break;
2534
2535     case OFPGC11_DELETE:
2536         ds_put_cstr(s, "DEL");
2537         break;
2538
2539     default:
2540         ds_put_format(s, "cmd:%"PRIu16"", gm.command);
2541     }
2542     ds_put_char(s, ' ');
2543
2544     ofp_print_group(s, gm.group_id, gm.type, &gm.buckets);
2545 }
2546
2547 static const char *
2548 ofp13_action_to_string(uint32_t bit)
2549 {
2550     switch (bit) {
2551 #define OFPAT13_ACTION(ENUM, STRUCT, EXTENSIBLE, NAME)  \
2552         case 1u << ENUM: return NAME;
2553 #include "ofp-util.def"
2554     }
2555     return NULL;
2556 }
2557
2558 static void
2559 print_table_action_features(struct ds *s,
2560                             const struct ofputil_table_action_features *taf)
2561 {
2562     ds_put_cstr(s, "        actions: ");
2563     ofp_print_bit_names(s, taf->actions, ofp13_action_to_string, ',');
2564     ds_put_char(s, '\n');
2565
2566     ds_put_cstr(s, "        supported on Set-Field: ");
2567     if (taf->set_fields) {
2568         int i;
2569
2570         for (i = 0; i < MFF_N_IDS; i++) {
2571             uint64_t bit = UINT64_C(1) << i;
2572
2573             if (taf->set_fields & bit) {
2574                 ds_put_format(s, "%s,", mf_from_id(i)->name);
2575             }
2576         }
2577         ds_chomp(s, ',');
2578     } else {
2579         ds_put_cstr(s, "none");
2580     }
2581     ds_put_char(s, '\n');
2582 }
2583
2584 static bool
2585 table_action_features_equal(const struct ofputil_table_action_features *a,
2586                             const struct ofputil_table_action_features *b)
2587 {
2588     return a->actions == b->actions && a->set_fields == b->set_fields;
2589 }
2590
2591 static void
2592 print_table_instruction_features(
2593     struct ds *s, const struct ofputil_table_instruction_features *tif)
2594 {
2595     int start, end;
2596
2597     ds_put_cstr(s, "      next tables: ");
2598     for (start = bitmap_scan(tif->next, 1, 0, 255); start < 255;
2599          start = bitmap_scan(tif->next, 1, end, 255)) {
2600         end = bitmap_scan(tif->next, 0, start + 1, 255);
2601         if (end == start + 1) {
2602             ds_put_format(s, "%d,", start);
2603         } else {
2604             ds_put_format(s, "%d-%d,", start, end - 1);
2605         }
2606     }
2607     ds_chomp(s, ',');
2608     if (ds_last(s) == ' ') {
2609         ds_put_cstr(s, "none");
2610     }
2611     ds_put_char(s, '\n');
2612
2613     ds_put_cstr(s, "      instructions: ");
2614     if (tif->instructions) {
2615         int i;
2616
2617         for (i = 0; i < 32; i++) {
2618             if (tif->instructions & (1u << i)) {
2619                 ds_put_format(s, "%s,", ovs_instruction_name_from_type(i));
2620             }
2621         }
2622         ds_chomp(s, ',');
2623     } else {
2624         ds_put_cstr(s, "none");
2625     }
2626     ds_put_char(s, '\n');
2627
2628     if (table_action_features_equal(&tif->write, &tif->apply)) {
2629         ds_put_cstr(s, "      Write-Actions and Apply-Actions features:\n");
2630         print_table_action_features(s, &tif->write);
2631     } else {
2632         ds_put_cstr(s, "      Write-Actions features:\n");
2633         print_table_action_features(s, &tif->write);
2634         ds_put_cstr(s, "      Apply-Actions features:\n");
2635         print_table_action_features(s, &tif->apply);
2636     }
2637 }
2638
2639 static bool
2640 table_instruction_features_equal(
2641     const struct ofputil_table_instruction_features *a,
2642     const struct ofputil_table_instruction_features *b)
2643 {
2644     return (bitmap_equal(a->next, b->next, 255)
2645             && a->instructions == b->instructions
2646             && table_action_features_equal(&a->write, &b->write)
2647             && table_action_features_equal(&a->apply, &b->apply));
2648 }
2649
2650 static void
2651 ofp_print_table_features(struct ds *s, const struct ofp_header *oh)
2652 {
2653     struct ofpbuf b;
2654
2655     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2656
2657     for (;;) {
2658         struct ofputil_table_features tf;
2659         int retval;
2660         int i;
2661
2662         retval = ofputil_decode_table_features(&b, &tf, true);
2663         if (retval) {
2664             if (retval != EOF) {
2665                 ofp_print_error(s, retval);
2666             }
2667             return;
2668         }
2669
2670         ds_put_format(s, "\n  table %"PRIu8":\n", tf.table_id);
2671         ds_put_format(s, "    name=\"%s\"\n", tf.name);
2672         ds_put_format(s, "    metadata: match=%#"PRIx64" write=%#"PRIx64"\n",
2673                       ntohll(tf.metadata_match), ntohll(tf.metadata_write));
2674
2675         ds_put_cstr(s, "    config=");
2676         ofp_print_table_miss_config(s, tf.config);
2677
2678         ds_put_format(s, "    max_entries=%"PRIu32"\n", tf.max_entries);
2679
2680         if (table_instruction_features_equal(&tf.nonmiss, &tf.miss)) {
2681             ds_put_cstr(s, "    instructions (table miss and others):\n");
2682             print_table_instruction_features(s, &tf.nonmiss);
2683         } else {
2684             ds_put_cstr(s, "    instructions (other than table miss):\n");
2685             print_table_instruction_features(s, &tf.nonmiss);
2686             ds_put_cstr(s, "    instructions (table miss):\n");
2687             print_table_instruction_features(s, &tf.miss);
2688         }
2689
2690         ds_put_cstr(s, "    matching:\n");
2691         for (i = 0; i < MFF_N_IDS; i++) {
2692             uint64_t bit = UINT64_C(1) << i;
2693
2694             if (tf.match & bit) {
2695                 const struct mf_field *f = mf_from_id(i);
2696
2697                 ds_put_format(s, "      %s: %s\n",
2698                               f->name,
2699                               (tf.mask ? "arbitrary mask"
2700                                : tf.wildcard ? "exact match or wildcard"
2701                                : "must exact match"));
2702             }
2703         }
2704     }
2705 }
2706
2707 static const char *
2708 bundle_flags_to_name(uint32_t bit)
2709 {
2710     switch (bit) {
2711     case OFPBF_ATOMIC:
2712         return "atomic";
2713     case OFPBF_ORDERED:
2714         return "ordered";
2715     default:
2716         return NULL;
2717     }
2718 }
2719
2720 static void
2721 ofp_print_bundle_ctrl(struct ds *s, const struct ofp_header *oh)
2722 {
2723     int error;
2724     struct ofputil_bundle_ctrl_msg bctrl;
2725
2726     error = ofputil_decode_bundle_ctrl(oh, &bctrl);
2727     if (error) {
2728         ofp_print_error(s, error);
2729         return;
2730     }
2731
2732     ds_put_char(s, '\n');
2733
2734     ds_put_format(s, " bundle_id=%#"PRIx32" type=",  bctrl.bundle_id);
2735     switch (bctrl.type) {
2736     case OFPBCT_OPEN_REQUEST:
2737         ds_put_cstr(s, "OPEN_REQUEST");
2738         break;
2739     case OFPBCT_OPEN_REPLY:
2740         ds_put_cstr(s, "OPEN_REPLY");
2741         break;
2742     case OFPBCT_CLOSE_REQUEST:
2743         ds_put_cstr(s, "CLOSE_REQUEST");
2744         break;
2745     case OFPBCT_CLOSE_REPLY:
2746         ds_put_cstr(s, "CLOSE_REPLY");
2747         break;
2748     case OFPBCT_COMMIT_REQUEST:
2749         ds_put_cstr(s, "COMMIT_REQUEST");
2750         break;
2751     case OFPBCT_COMMIT_REPLY:
2752         ds_put_cstr(s, "COMMIT_REPLY");
2753         break;
2754     case OFPBCT_DISCARD_REQUEST:
2755         ds_put_cstr(s, "DISCARD_REQUEST");
2756         break;
2757     case OFPBCT_DISCARD_REPLY:
2758         ds_put_cstr(s, "DISCARD_REPLY");
2759         break;
2760     }
2761
2762     ds_put_cstr(s, " flags=");
2763     ofp_print_bit_names(s, bctrl.flags, bundle_flags_to_name, ' ');
2764 }
2765
2766 static void
2767 ofp_print_bundle_add(struct ds *s, const struct ofp_header *oh, int verbosity)
2768 {
2769     int error;
2770     struct ofputil_bundle_add_msg badd;
2771     char *msg;
2772
2773     error = ofputil_decode_bundle_add(oh, &badd);
2774     if (error) {
2775         ofp_print_error(s, error);
2776         return;
2777     }
2778
2779     ds_put_char(s, '\n');
2780     ds_put_format(s, " bundle_id=%#"PRIx32,  badd.bundle_id);
2781     ds_put_cstr(s, " flags=");
2782     ofp_print_bit_names(s, badd.flags, bundle_flags_to_name, ' ');
2783
2784     ds_put_char(s, '\n');
2785     msg = ofp_to_string(badd.msg, ntohs(badd.msg->length), verbosity);
2786     if (msg) {
2787         ds_put_cstr(s, msg);
2788     }
2789 }
2790
2791 static void
2792 ofp_to_string__(const struct ofp_header *oh, enum ofpraw raw,
2793                 struct ds *string, int verbosity)
2794 {
2795     const void *msg = oh;
2796
2797     ofp_header_to_string__(oh, raw, string);
2798     switch (ofptype_from_ofpraw(raw)) {
2799
2800     case OFPTYPE_GROUP_STATS_REQUEST:
2801         ofp_print_stats_request(string, oh);
2802         ofp_print_ofpst_group_request(string, oh);
2803         break;
2804
2805     case OFPTYPE_GROUP_STATS_REPLY:
2806         ofp_print_group_stats(string, oh);
2807         break;
2808
2809     case OFPTYPE_GROUP_DESC_STATS_REQUEST:
2810         ofp_print_stats_request(string, oh);
2811         ofp_print_ofpst_group_desc_request(string, oh);
2812         break;
2813
2814     case OFPTYPE_GROUP_DESC_STATS_REPLY:
2815         ofp_print_group_desc(string, oh);
2816         break;
2817
2818     case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
2819         ofp_print_stats_request(string, oh);
2820         break;
2821
2822     case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
2823         ofp_print_group_features(string, oh);
2824         break;
2825
2826     case OFPTYPE_GROUP_MOD:
2827         ofp_print_group_mod(string, oh);
2828         break;
2829
2830     case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
2831     case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
2832         ofp_print_table_features(string, oh);
2833         break;
2834
2835     case OFPTYPE_HELLO:
2836         ofp_print_hello(string, oh);
2837         break;
2838
2839     case OFPTYPE_ERROR:
2840         ofp_print_error_msg(string, oh);
2841         break;
2842
2843     case OFPTYPE_ECHO_REQUEST:
2844     case OFPTYPE_ECHO_REPLY:
2845         ofp_print_echo(string, oh, verbosity);
2846         break;
2847
2848     case OFPTYPE_FEATURES_REQUEST:
2849         break;
2850
2851     case OFPTYPE_FEATURES_REPLY:
2852         ofp_print_switch_features(string, oh);
2853         break;
2854
2855     case OFPTYPE_GET_CONFIG_REQUEST:
2856         break;
2857
2858     case OFPTYPE_GET_CONFIG_REPLY:
2859     case OFPTYPE_SET_CONFIG:
2860         ofp_print_switch_config(string, ofpmsg_body(oh));
2861         break;
2862
2863     case OFPTYPE_PACKET_IN:
2864         ofp_print_packet_in(string, oh, verbosity);
2865         break;
2866
2867     case OFPTYPE_FLOW_REMOVED:
2868         ofp_print_flow_removed(string, oh);
2869         break;
2870
2871     case OFPTYPE_PORT_STATUS:
2872         ofp_print_port_status(string, oh);
2873         break;
2874
2875     case OFPTYPE_PACKET_OUT:
2876         ofp_print_packet_out(string, oh, verbosity);
2877         break;
2878
2879     case OFPTYPE_FLOW_MOD:
2880         ofp_print_flow_mod(string, oh, verbosity);
2881         break;
2882
2883     case OFPTYPE_PORT_MOD:
2884         ofp_print_port_mod(string, oh);
2885         break;
2886
2887     case OFPTYPE_TABLE_MOD:
2888         ofp_print_table_mod(string, oh);
2889         break;
2890
2891     case OFPTYPE_METER_MOD:
2892         ofp_print_meter_mod(string, oh);
2893         break;
2894
2895     case OFPTYPE_BARRIER_REQUEST:
2896     case OFPTYPE_BARRIER_REPLY:
2897         break;
2898
2899     case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
2900         ofp_print_queue_get_config_request(string, oh);
2901         break;
2902
2903     case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
2904         ofp_print_queue_get_config_reply(string, oh);
2905         break;
2906
2907     case OFPTYPE_ROLE_REQUEST:
2908     case OFPTYPE_ROLE_REPLY:
2909         ofp_print_role_message(string, oh);
2910         break;
2911     case OFPTYPE_ROLE_STATUS:
2912         ofp_print_role_status_message(string, oh);
2913         break;
2914
2915     case OFPTYPE_METER_STATS_REQUEST:
2916     case OFPTYPE_METER_CONFIG_STATS_REQUEST:
2917         ofp_print_stats_request(string, oh);
2918         ofp_print_meter_stats_request(string, oh);
2919         break;
2920
2921     case OFPTYPE_METER_STATS_REPLY:
2922         ofp_print_stats_reply(string, oh);
2923         ofp_print_meter_stats_reply(string, oh);
2924         break;
2925
2926     case OFPTYPE_METER_CONFIG_STATS_REPLY:
2927         ofp_print_stats_reply(string, oh);
2928         ofp_print_meter_config_reply(string, oh);
2929         break;
2930
2931     case OFPTYPE_METER_FEATURES_STATS_REPLY:
2932         ofp_print_stats_reply(string, oh);
2933         ofp_print_meter_features_reply(string, oh);
2934         break;
2935
2936     case OFPTYPE_DESC_STATS_REQUEST:
2937     case OFPTYPE_METER_FEATURES_STATS_REQUEST:
2938         ofp_print_stats_request(string, oh);
2939         break;
2940
2941     case OFPTYPE_FLOW_STATS_REQUEST:
2942     case OFPTYPE_AGGREGATE_STATS_REQUEST:
2943         ofp_print_stats_request(string, oh);
2944         ofp_print_flow_stats_request(string, oh);
2945         break;
2946
2947     case OFPTYPE_TABLE_STATS_REQUEST:
2948         ofp_print_stats_request(string, oh);
2949         break;
2950
2951     case OFPTYPE_PORT_STATS_REQUEST:
2952         ofp_print_stats_request(string, oh);
2953         ofp_print_ofpst_port_request(string, oh);
2954         break;
2955
2956     case OFPTYPE_QUEUE_STATS_REQUEST:
2957         ofp_print_stats_request(string, oh);
2958         ofp_print_ofpst_queue_request(string, oh);
2959         break;
2960
2961     case OFPTYPE_DESC_STATS_REPLY:
2962         ofp_print_stats_reply(string, oh);
2963         ofp_print_ofpst_desc_reply(string, oh);
2964         break;
2965
2966     case OFPTYPE_FLOW_STATS_REPLY:
2967         ofp_print_stats_reply(string, oh);
2968         ofp_print_flow_stats_reply(string, oh);
2969         break;
2970
2971     case OFPTYPE_QUEUE_STATS_REPLY:
2972         ofp_print_stats_reply(string, oh);
2973         ofp_print_ofpst_queue_reply(string, oh, verbosity);
2974         break;
2975
2976     case OFPTYPE_PORT_STATS_REPLY:
2977         ofp_print_stats_reply(string, oh);
2978         ofp_print_ofpst_port_reply(string, oh, verbosity);
2979         break;
2980
2981     case OFPTYPE_TABLE_STATS_REPLY:
2982         ofp_print_stats_reply(string, oh);
2983         ofp_print_ofpst_table_reply(string, oh, verbosity);
2984         break;
2985
2986     case OFPTYPE_AGGREGATE_STATS_REPLY:
2987         ofp_print_stats_reply(string, oh);
2988         ofp_print_aggregate_stats_reply(string, oh);
2989         break;
2990
2991     case OFPTYPE_PORT_DESC_STATS_REQUEST:
2992         ofp_print_stats_request(string, oh);
2993         ofp_print_ofpst_port_desc_request(string, oh);
2994         break;
2995
2996     case OFPTYPE_PORT_DESC_STATS_REPLY:
2997         ofp_print_stats_reply(string, oh);
2998         ofp_print_ofpst_port_desc_reply(string, oh);
2999         break;
3000
3001     case OFPTYPE_FLOW_MOD_TABLE_ID:
3002         ofp_print_nxt_flow_mod_table_id(string, ofpmsg_body(oh));
3003         break;
3004
3005     case OFPTYPE_SET_FLOW_FORMAT:
3006         ofp_print_nxt_set_flow_format(string, ofpmsg_body(oh));
3007         break;
3008
3009     case OFPTYPE_SET_PACKET_IN_FORMAT:
3010         ofp_print_nxt_set_packet_in_format(string, ofpmsg_body(oh));
3011         break;
3012
3013     case OFPTYPE_FLOW_AGE:
3014         break;
3015
3016     case OFPTYPE_SET_CONTROLLER_ID:
3017         ofp_print_nxt_set_controller_id(string, ofpmsg_body(oh));
3018         break;
3019
3020     case OFPTYPE_GET_ASYNC_REPLY:
3021     case OFPTYPE_SET_ASYNC_CONFIG:
3022         ofp_print_nxt_set_async_config(string, ofpmsg_body(oh));
3023         break;
3024     case OFPTYPE_GET_ASYNC_REQUEST:
3025         break;
3026     case OFPTYPE_FLOW_MONITOR_CANCEL:
3027         ofp_print_nxt_flow_monitor_cancel(string, msg);
3028         break;
3029
3030     case OFPTYPE_FLOW_MONITOR_PAUSED:
3031     case OFPTYPE_FLOW_MONITOR_RESUMED:
3032         break;
3033
3034     case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
3035         ofp_print_nxst_flow_monitor_request(string, msg);
3036         break;
3037
3038     case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
3039         ofp_print_nxst_flow_monitor_reply(string, msg);
3040         break;
3041
3042     case OFPTYPE_BUNDLE_CONTROL:
3043         ofp_print_bundle_ctrl(string, msg);
3044         break;
3045
3046     case OFPTYPE_BUNDLE_ADD_MESSAGE:
3047         ofp_print_bundle_add(string, msg, verbosity);
3048         break;
3049     }
3050 }
3051
3052 /* Composes and returns a string representing the OpenFlow packet of 'len'
3053  * bytes at 'oh' at the given 'verbosity' level.  0 is a minimal amount of
3054  * verbosity and higher numbers increase verbosity.  The caller is responsible
3055  * for freeing the string. */
3056 char *
3057 ofp_to_string(const void *oh_, size_t len, int verbosity)
3058 {
3059     struct ds string = DS_EMPTY_INITIALIZER;
3060     const struct ofp_header *oh = oh_;
3061
3062     if (!len) {
3063         ds_put_cstr(&string, "OpenFlow message is empty\n");
3064     } else if (len < sizeof(struct ofp_header)) {
3065         ds_put_format(&string, "OpenFlow packet too short (only %"PRIuSIZE" bytes):\n",
3066                       len);
3067     } else if (ntohs(oh->length) > len) {
3068         enum ofperr error;
3069         enum ofpraw raw;
3070
3071         error = ofpraw_decode_partial(&raw, oh, len);
3072         if (!error) {
3073             ofp_header_to_string__(oh, raw, &string);
3074             ds_put_char(&string, '\n');
3075         }
3076
3077         ds_put_format(&string,
3078                       "(***truncated to %"PRIuSIZE" bytes from %"PRIu16"***)\n",
3079                       len, ntohs(oh->length));
3080     } else if (ntohs(oh->length) < len) {
3081         ds_put_format(&string,
3082                       "(***only uses %"PRIu16" bytes out of %"PRIuSIZE"***)\n",
3083                       ntohs(oh->length), len);
3084     } else {
3085         enum ofperr error;
3086         enum ofpraw raw;
3087
3088         error = ofpraw_decode(&raw, oh);
3089         if (!error) {
3090             ofp_to_string__(oh, raw, &string, verbosity);
3091             if (verbosity >= 5) {
3092                 if (ds_last(&string) != '\n') {
3093                     ds_put_char(&string, '\n');
3094                 }
3095                 ds_put_hex_dump(&string, oh, len, 0, true);
3096             }
3097             if (ds_last(&string) != '\n') {
3098                 ds_put_char(&string, '\n');
3099             }
3100             return ds_steal_cstr(&string);
3101         }
3102
3103         ofp_print_error(&string, error);
3104     }
3105     ds_put_hex_dump(&string, oh, len, 0, true);
3106     return ds_steal_cstr(&string);
3107 }
3108 \f
3109 static void
3110 print_and_free(FILE *stream, char *string)
3111 {
3112     fputs(string, stream);
3113     free(string);
3114 }
3115
3116 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
3117  * given 'verbosity' level.  0 is a minimal amount of verbosity and higher
3118  * numbers increase verbosity. */
3119 void
3120 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
3121 {
3122     print_and_free(stream, ofp_to_string(oh, len, verbosity));
3123 }
3124
3125 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
3126  * 'data' to 'stream'. */
3127 void
3128 ofp_print_packet(FILE *stream, const void *data, size_t len)
3129 {
3130     print_and_free(stream, ofp_packet_to_string(data, len));
3131 }