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