flow: Enable matching on new field 'pkt_mark'.
[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     /*
1110      * Note: Meter band types start from 1.  We assume that the lowest bit
1111      * in the band_types corresponds to DROP band type (1).
1112      */
1113     switch (bit) {
1114     case 1 << (OFPMBT13_DROP - 1):          return "drop";
1115     case 1 << (OFPMBT13_DSCP_REMARK - 1):   return "dscp_remark";
1116     }
1117
1118     return NULL;
1119 }
1120
1121 static void
1122 ofp_print_meter_features_reply(struct ds *s, const struct ofp_header *oh)
1123 {
1124     struct ofputil_meter_features mf;
1125
1126     ofputil_decode_meter_features(oh, &mf);
1127
1128     ds_put_format(s, "\nmax_meter:%"PRIu32, mf.max_meters);
1129     ds_put_format(s, " max_bands:%"PRIu8, mf.max_bands);
1130     ds_put_format(s, " max_color:%"PRIu8"\n", mf.max_color);
1131
1132     ds_put_cstr(s, "band_types: ");
1133     ofp_print_bit_names(s, mf.band_types,
1134                         ofputil_meter_band_types_to_name, ' ');
1135     ds_put_char(s, '\n');
1136
1137     ds_put_cstr(s, "capabilities: ");
1138     ofp_print_bit_names(s, mf.capabilities,
1139                         ofputil_meter_capabilities_to_name, ' ');
1140     ds_put_char(s, '\n');
1141 }
1142
1143 static void
1144 ofp_print_meter_config_reply(struct ds *s, const struct ofp_header *oh)
1145 {
1146     struct ofpbuf bands;
1147     struct ofpbuf b;
1148
1149     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1150     ofpbuf_init(&bands, 64);
1151     for (;;) {
1152         struct ofputil_meter_config mc;
1153         int retval;
1154
1155         retval = ofputil_decode_meter_config(&b, &mc, &bands);
1156         if (retval) {
1157             if (retval != EOF) {
1158                 ofp_print_error(s, retval);
1159             }
1160             break;
1161         }
1162         ds_put_char(s, '\n');
1163         ofp_print_meter_config(s, &mc);
1164     }
1165     ofpbuf_uninit(&bands);
1166 }
1167
1168 static void
1169 ofp_print_meter_stats_reply(struct ds *s, const struct ofp_header *oh)
1170 {
1171     struct ofpbuf bands;
1172     struct ofpbuf b;
1173
1174     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1175     ofpbuf_init(&bands, 64);
1176     for (;;) {
1177         struct ofputil_meter_stats ms;
1178         int retval;
1179
1180         retval = ofputil_decode_meter_stats(&b, &ms, &bands);
1181         if (retval) {
1182             if (retval != EOF) {
1183                 ofp_print_error(s, retval);
1184             }
1185             break;
1186         }
1187         ds_put_char(s, '\n');
1188         ofp_print_meter_stats(s, &ms);
1189     }
1190     ofpbuf_uninit(&bands);
1191 }
1192
1193 static void
1194 ofp_print_error(struct ds *string, enum ofperr error)
1195 {
1196     if (string->length) {
1197         ds_put_char(string, ' ');
1198     }
1199     ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1200 }
1201
1202 static void
1203 ofp_print_hello(struct ds *string, const struct ofp_header *oh)
1204 {
1205     uint32_t allowed_versions;
1206     bool ok;
1207
1208     ok = ofputil_decode_hello(oh, &allowed_versions);
1209
1210     ds_put_cstr(string, "\n version bitmap: ");
1211     ofputil_format_version_bitmap(string, allowed_versions);
1212
1213     if (!ok) {
1214         ds_put_cstr(string, "\n unknown data in hello:\n");
1215         ds_put_hex_dump(string, oh, ntohs(oh->length), 0, true);
1216     }
1217 }
1218
1219 static void
1220 ofp_print_error_msg(struct ds *string, const struct ofp_header *oh)
1221 {
1222     size_t len = ntohs(oh->length);
1223     struct ofpbuf payload;
1224     enum ofperr error;
1225     char *s;
1226
1227     error = ofperr_decode_msg(oh, &payload);
1228     if (!error) {
1229         ds_put_cstr(string, "***decode error***");
1230         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1231         return;
1232     }
1233
1234     ds_put_format(string, " %s\n", ofperr_get_name(error));
1235
1236     if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
1237         ds_put_printable(string, payload.data, payload.size);
1238     } else {
1239         s = ofp_to_string(payload.data, payload.size, 1);
1240         ds_put_cstr(string, s);
1241         free(s);
1242     }
1243 }
1244
1245 static void
1246 ofp_print_port_status(struct ds *string, const struct ofp_header *oh)
1247 {
1248     struct ofputil_port_status ps;
1249     enum ofperr error;
1250
1251     error = ofputil_decode_port_status(oh, &ps);
1252     if (error) {
1253         ofp_print_error(string, error);
1254         return;
1255     }
1256
1257     if (ps.reason == OFPPR_ADD) {
1258         ds_put_format(string, " ADD:");
1259     } else if (ps.reason == OFPPR_DELETE) {
1260         ds_put_format(string, " DEL:");
1261     } else if (ps.reason == OFPPR_MODIFY) {
1262         ds_put_format(string, " MOD:");
1263     }
1264
1265     ofp_print_phy_port(string, &ps.desc);
1266 }
1267
1268 static void
1269 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
1270 {
1271     const struct ofp_desc_stats *ods = ofpmsg_body(oh);
1272
1273     ds_put_char(string, '\n');
1274     ds_put_format(string, "Manufacturer: %.*s\n",
1275             (int) sizeof ods->mfr_desc, ods->mfr_desc);
1276     ds_put_format(string, "Hardware: %.*s\n",
1277             (int) sizeof ods->hw_desc, ods->hw_desc);
1278     ds_put_format(string, "Software: %.*s\n",
1279             (int) sizeof ods->sw_desc, ods->sw_desc);
1280     ds_put_format(string, "Serial Num: %.*s\n",
1281             (int) sizeof ods->serial_num, ods->serial_num);
1282     ds_put_format(string, "DP Description: %.*s\n",
1283             (int) sizeof ods->dp_desc, ods->dp_desc);
1284 }
1285
1286 static void
1287 ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
1288 {
1289     struct ofputil_flow_stats_request fsr;
1290     enum ofperr error;
1291
1292     error = ofputil_decode_flow_stats_request(&fsr, oh);
1293     if (error) {
1294         ofp_print_error(string, error);
1295         return;
1296     }
1297
1298     if (fsr.table_id != 0xff) {
1299         ds_put_format(string, " table=%"PRIu8, fsr.table_id);
1300     }
1301
1302     if (fsr.out_port != OFPP_ANY) {
1303         ds_put_cstr(string, " out_port=");
1304         ofputil_format_port(fsr.out_port, string);
1305     }
1306
1307     ds_put_char(string, ' ');
1308     match_format(&fsr.match, string, OFP_DEFAULT_PRIORITY);
1309 }
1310
1311 void
1312 ofp_print_flow_stats(struct ds *string, struct ofputil_flow_stats *fs)
1313 {
1314     ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1315                   ntohll(fs->cookie));
1316
1317     ofp_print_duration(string, fs->duration_sec, fs->duration_nsec);
1318     ds_put_format(string, ", table=%"PRIu8", ", fs->table_id);
1319     ds_put_format(string, "n_packets=%"PRIu64", ", fs->packet_count);
1320     ds_put_format(string, "n_bytes=%"PRIu64", ", fs->byte_count);
1321     if (fs->idle_timeout != OFP_FLOW_PERMANENT) {
1322         ds_put_format(string, "idle_timeout=%"PRIu16", ", fs->idle_timeout);
1323     }
1324     if (fs->hard_timeout != OFP_FLOW_PERMANENT) {
1325         ds_put_format(string, "hard_timeout=%"PRIu16", ", fs->hard_timeout);
1326     }
1327     if (fs->flags) {
1328         ofp_print_flow_flags(string, fs->flags);
1329     }
1330     if (fs->idle_age >= 0) {
1331         ds_put_format(string, "idle_age=%d, ", fs->idle_age);
1332     }
1333     if (fs->hard_age >= 0 && fs->hard_age != fs->duration_sec) {
1334         ds_put_format(string, "hard_age=%d, ", fs->hard_age);
1335     }
1336
1337     match_format(&fs->match, string, fs->priority);
1338     if (string->string[string->length - 1] != ' ') {
1339         ds_put_char(string, ' ');
1340     }
1341
1342     ofpacts_format(fs->ofpacts, fs->ofpacts_len, string);
1343 }
1344
1345 static void
1346 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1347 {
1348     struct ofpbuf ofpacts;
1349     struct ofpbuf b;
1350
1351     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1352     ofpbuf_init(&ofpacts, 64);
1353     for (;;) {
1354         struct ofputil_flow_stats fs;
1355         int retval;
1356
1357         retval = ofputil_decode_flow_stats_reply(&fs, &b, true, &ofpacts);
1358         if (retval) {
1359             if (retval != EOF) {
1360                 ds_put_cstr(string, " ***parse error***");
1361             }
1362             break;
1363         }
1364         ds_put_char(string, '\n');
1365         ofp_print_flow_stats(string, &fs);
1366      }
1367     ofpbuf_uninit(&ofpacts);
1368 }
1369
1370 static void
1371 ofp_print_aggregate_stats_reply(struct ds *string, const struct ofp_header *oh)
1372 {
1373     struct ofputil_aggregate_stats as;
1374     enum ofperr error;
1375
1376     error = ofputil_decode_aggregate_stats_reply(&as, oh);
1377     if (error) {
1378         ofp_print_error(string, error);
1379         return;
1380     }
1381
1382     ds_put_format(string, " packet_count=%"PRIu64, as.packet_count);
1383     ds_put_format(string, " byte_count=%"PRIu64, as.byte_count);
1384     ds_put_format(string, " flow_count=%"PRIu32, as.flow_count);
1385 }
1386
1387 static void
1388 print_port_stat(struct ds *string, const char *leader, uint64_t stat, int more)
1389 {
1390     ds_put_cstr(string, leader);
1391     if (stat != UINT64_MAX) {
1392         ds_put_format(string, "%"PRIu64, stat);
1393     } else {
1394         ds_put_char(string, '?');
1395     }
1396     if (more) {
1397         ds_put_cstr(string, ", ");
1398     } else {
1399         ds_put_cstr(string, "\n");
1400     }
1401 }
1402
1403 static void
1404 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1405 {
1406     ofp_port_t ofp10_port;
1407     enum ofperr error;
1408
1409     error = ofputil_decode_port_stats_request(oh, &ofp10_port);
1410     if (error) {
1411         ofp_print_error(string, error);
1412         return;
1413     }
1414
1415     ds_put_cstr(string, " port_no=");
1416     ofputil_format_port(ofp10_port, string);
1417 }
1418
1419 static void
1420 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1421                            int verbosity)
1422 {
1423     struct ofpbuf b;
1424
1425     ds_put_format(string, " %zu ports\n", ofputil_count_port_stats(oh));
1426     if (verbosity < 1) {
1427         return;
1428     }
1429
1430     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1431     for (;;) {
1432         struct ofputil_port_stats ps;
1433         int retval;
1434
1435         retval = ofputil_decode_port_stats(&ps, &b);
1436         if (retval) {
1437             if (retval != EOF) {
1438                 ds_put_cstr(string, " ***parse error***");
1439             }
1440             return;
1441         }
1442
1443         ds_put_cstr(string, "  port ");
1444         if (ofp_to_u16(ps.port_no) < 10) {
1445             ds_put_char(string, ' ');
1446         }
1447         ofputil_format_port(ps.port_no, string);
1448
1449         ds_put_cstr(string, ": rx ");
1450         print_port_stat(string, "pkts=", ps.stats.rx_packets, 1);
1451         print_port_stat(string, "bytes=", ps.stats.rx_bytes, 1);
1452         print_port_stat(string, "drop=", ps.stats.rx_dropped, 1);
1453         print_port_stat(string, "errs=", ps.stats.rx_errors, 1);
1454         print_port_stat(string, "frame=", ps.stats.rx_frame_errors, 1);
1455         print_port_stat(string, "over=", ps.stats.rx_over_errors, 1);
1456         print_port_stat(string, "crc=", ps.stats.rx_crc_errors, 0);
1457
1458         ds_put_cstr(string, "           tx ");
1459         print_port_stat(string, "pkts=", ps.stats.tx_packets, 1);
1460         print_port_stat(string, "bytes=", ps.stats.tx_bytes, 1);
1461         print_port_stat(string, "drop=", ps.stats.tx_dropped, 1);
1462         print_port_stat(string, "errs=", ps.stats.tx_errors, 1);
1463         print_port_stat(string, "coll=", ps.stats.collisions, 0);
1464
1465         if (ps.duration_sec != UINT32_MAX) {
1466             ds_put_cstr(string, "           duration=");
1467             ofp_print_duration(string, ps.duration_sec, ps.duration_nsec);
1468             ds_put_char(string, '\n');
1469         }
1470     }
1471 }
1472
1473 static void
1474 ofp_print_one_ofpst_table_reply(struct ds *string, enum ofp_version ofp_version,
1475                                 const char *name, struct ofp12_table_stats *ts)
1476 {
1477     char name_[OFP_MAX_TABLE_NAME_LEN + 1];
1478
1479     /* ofp13_table_stats is different */
1480     if (ofp_version > OFP12_VERSION) {
1481         return;
1482     }
1483
1484     ovs_strlcpy(name_, name, sizeof name_);
1485
1486     ds_put_format(string, "  %d: %-8s: ", ts->table_id, name_);
1487     ds_put_format(string, "wild=0x%05"PRIx64", ", ntohll(ts->wildcards));
1488     ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1489     ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1490     ds_put_cstr(string, "               ");
1491     ds_put_format(string, "lookup=%"PRIu64", ", ntohll(ts->lookup_count));
1492     ds_put_format(string, "matched=%"PRIu64"\n", ntohll(ts->matched_count));
1493
1494     if (ofp_version < OFP11_VERSION) {
1495         return;
1496     }
1497
1498     ds_put_cstr(string, "               ");
1499     ds_put_format(string, "match=0x%08"PRIx64", ", ntohll(ts->match));
1500     ds_put_format(string, "instructions=0x%08"PRIx32", ",
1501                   ntohl(ts->instructions));
1502     ds_put_format(string, "config=0x%08"PRIx32"\n", ntohl(ts->config));
1503     ds_put_cstr(string, "               ");
1504     ds_put_format(string, "write_actions=0x%08"PRIx32", ",
1505                   ntohl(ts->write_actions));
1506     ds_put_format(string, "apply_actions=0x%08"PRIx32"\n",
1507                   ntohl(ts->apply_actions));
1508
1509     if (ofp_version < OFP12_VERSION) {
1510         return;
1511     }
1512
1513     ds_put_cstr(string, "               ");
1514     ds_put_format(string, "write_setfields=0x%016"PRIx64"\n",
1515                   ntohll(ts->write_setfields));
1516     ds_put_cstr(string, "               ");
1517     ds_put_format(string, "apply_setfields=0x%016"PRIx64"\n",
1518                   ntohll(ts->apply_setfields));
1519     ds_put_cstr(string, "               ");
1520     ds_put_format(string, "metadata_match=0x%016"PRIx64"\n",
1521                   ntohll(ts->metadata_match));
1522     ds_put_cstr(string, "               ");
1523     ds_put_format(string, "metadata_write=0x%016"PRIx64"\n",
1524                   ntohll(ts->metadata_write));
1525 }
1526
1527 static void
1528 ofp_print_ofpst_table_reply13(struct ds *string, const struct ofp_header *oh,
1529                               int verbosity)
1530 {
1531     struct ofp13_table_stats *ts;
1532     struct ofpbuf b;
1533     size_t n;
1534
1535     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1536     ofpraw_pull_assert(&b);
1537
1538     n = b.size / sizeof *ts;
1539     ds_put_format(string, " %zu tables\n", n);
1540     if (verbosity < 1) {
1541         return;
1542     }
1543
1544     for (;;) {
1545         ts = ofpbuf_try_pull(&b, sizeof *ts);
1546         if (!ts) {
1547             return;
1548         }
1549         ds_put_format(string,
1550                       "  %d: active=%"PRIu32", lookup=%"PRIu64  \
1551                       ", matched=%"PRIu64"\n",
1552                       ts->table_id, ntohl(ts->active_count),
1553                       ntohll(ts->lookup_count), ntohll(ts->matched_count));
1554     }
1555 }
1556
1557 static void
1558 ofp_print_ofpst_table_reply12(struct ds *string, const struct ofp_header *oh,
1559                               int verbosity)
1560 {
1561     struct ofp12_table_stats *ts;
1562     struct ofpbuf b;
1563     size_t n;
1564
1565     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1566     ofpraw_pull_assert(&b);
1567
1568     n = b.size / sizeof *ts;
1569     ds_put_format(string, " %zu tables\n", n);
1570     if (verbosity < 1) {
1571         return;
1572     }
1573
1574     for (;;) {
1575         ts = ofpbuf_try_pull(&b, sizeof *ts);
1576         if (!ts) {
1577             return;
1578         }
1579
1580         ofp_print_one_ofpst_table_reply(string, OFP12_VERSION, ts->name, ts);
1581      }
1582 }
1583
1584 static void
1585 ofp_print_ofpst_table_reply11(struct ds *string, const struct ofp_header *oh,
1586                               int verbosity)
1587 {
1588     struct ofp11_table_stats *ts;
1589     struct ofpbuf b;
1590     size_t n;
1591
1592     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1593     ofpraw_pull_assert(&b);
1594
1595     n = b.size / sizeof *ts;
1596     ds_put_format(string, " %zu tables\n", n);
1597     if (verbosity < 1) {
1598         return;
1599     }
1600
1601     for (;;) {
1602         struct ofp12_table_stats ts12;
1603
1604         ts = ofpbuf_try_pull(&b, sizeof *ts);
1605         if (!ts) {
1606             return;
1607         }
1608
1609         ts12.table_id = ts->table_id;
1610         ts12.wildcards = htonll(ntohl(ts->wildcards));
1611         ts12.max_entries = ts->max_entries;
1612         ts12.active_count = ts->active_count;
1613         ts12.lookup_count = ts->lookup_count;
1614         ts12.matched_count = ts->matched_count;
1615         ts12.match = htonll(ntohl(ts->match));
1616         ts12.instructions = ts->instructions;
1617         ts12.config = ts->config;
1618         ts12.write_actions = ts->write_actions;
1619         ts12.apply_actions = ts->apply_actions;
1620         ofp_print_one_ofpst_table_reply(string, OFP11_VERSION, ts->name, &ts12);
1621      }
1622 }
1623
1624 static void
1625 ofp_print_ofpst_table_reply10(struct ds *string, const struct ofp_header *oh,
1626                               int verbosity)
1627 {
1628     struct ofp10_table_stats *ts;
1629     struct ofpbuf b;
1630     size_t n;
1631
1632     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1633     ofpraw_pull_assert(&b);
1634
1635     n = b.size / sizeof *ts;
1636     ds_put_format(string, " %zu tables\n", n);
1637     if (verbosity < 1) {
1638         return;
1639     }
1640
1641     for (;;) {
1642         struct ofp12_table_stats ts12;
1643
1644         ts = ofpbuf_try_pull(&b, sizeof *ts);
1645         if (!ts) {
1646             return;
1647         }
1648
1649         ts12.table_id = ts->table_id;
1650         ts12.wildcards = htonll(ntohl(ts->wildcards));
1651         ts12.max_entries = ts->max_entries;
1652         ts12.active_count = ts->active_count;
1653         ts12.lookup_count = get_32aligned_be64(&ts->lookup_count);
1654         ts12.matched_count = get_32aligned_be64(&ts->matched_count);
1655         ofp_print_one_ofpst_table_reply(string, OFP10_VERSION, ts->name, &ts12);
1656      }
1657 }
1658
1659 static void
1660 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1661                             int verbosity)
1662 {
1663     switch ((enum ofp_version)oh->version) {
1664     case OFP13_VERSION:
1665         ofp_print_ofpst_table_reply13(string, oh, verbosity);
1666         break;
1667
1668     case OFP12_VERSION:
1669         ofp_print_ofpst_table_reply12(string, oh, verbosity);
1670         break;
1671
1672     case OFP11_VERSION:
1673         ofp_print_ofpst_table_reply11(string, oh, verbosity);
1674         break;
1675
1676     case OFP10_VERSION:
1677         ofp_print_ofpst_table_reply10(string, oh, verbosity);
1678         break;
1679
1680     default:
1681         NOT_REACHED();
1682     }
1683 }
1684
1685 static void
1686 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1687 {
1688     if (queue_id == OFPQ_ALL) {
1689         ds_put_cstr(string, "ALL");
1690     } else {
1691         ds_put_format(string, "%"PRIu32, queue_id);
1692     }
1693 }
1694
1695 static void
1696 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1697 {
1698     struct ofputil_queue_stats_request oqsr;
1699     enum ofperr error;
1700
1701     error = ofputil_decode_queue_stats_request(oh, &oqsr);
1702     if (error) {
1703         ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1704         return;
1705     }
1706
1707     ds_put_cstr(string, "port=");
1708     ofputil_format_port(oqsr.port_no, string);
1709
1710     ds_put_cstr(string, " queue=");
1711     ofp_print_queue_name(string, oqsr.queue_id);
1712 }
1713
1714 static void
1715 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1716                             int verbosity)
1717 {
1718     struct ofpbuf b;
1719
1720     ds_put_format(string, " %zu queues\n", ofputil_count_queue_stats(oh));
1721     if (verbosity < 1) {
1722         return;
1723     }
1724
1725     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1726     for (;;) {
1727         struct ofputil_queue_stats qs;
1728         int retval;
1729
1730         retval = ofputil_decode_queue_stats(&qs, &b);
1731         if (retval) {
1732             if (retval != EOF) {
1733                 ds_put_cstr(string, " ***parse error***");
1734             }
1735             return;
1736         }
1737
1738         ds_put_cstr(string, "  port ");
1739         ofputil_format_port(qs.port_no, string);
1740         ds_put_cstr(string, " queue ");
1741         ofp_print_queue_name(string, qs.queue_id);
1742         ds_put_cstr(string, ": ");
1743
1744         print_port_stat(string, "bytes=", qs.tx_bytes, 1);
1745         print_port_stat(string, "pkts=", qs.tx_packets, 1);
1746         print_port_stat(string, "errors=", qs.tx_errors, 1);
1747
1748         ds_put_cstr(string, "duration=");
1749         if (qs.duration_sec != UINT32_MAX) {
1750             ofp_print_duration(string, qs.duration_sec, qs.duration_nsec);
1751         } else {
1752             ds_put_char(string, '?');
1753         }
1754         ds_put_char(string, '\n');
1755     }
1756 }
1757
1758 static void
1759 ofp_print_ofpst_port_desc_reply(struct ds *string,
1760                                 const struct ofp_header *oh)
1761 {
1762     struct ofpbuf b;
1763
1764     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1765     ofpraw_pull_assert(&b);
1766     ds_put_char(string, '\n');
1767     ofp_print_phy_ports(string, oh->version, &b);
1768 }
1769
1770 static void
1771 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1772 {
1773     uint16_t flags = ofpmp_flags(oh);
1774
1775     if (flags) {
1776         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
1777     }
1778 }
1779
1780 static void
1781 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1782 {
1783     uint16_t flags = ofpmp_flags(oh);
1784
1785     if (flags) {
1786         ds_put_cstr(string, " flags=");
1787         if (flags & OFPSF_REPLY_MORE) {
1788             ds_put_cstr(string, "[more]");
1789             flags &= ~OFPSF_REPLY_MORE;
1790         }
1791         if (flags) {
1792             ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1793                           flags);
1794         }
1795     }
1796 }
1797
1798 static void
1799 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1800 {
1801     size_t len = ntohs(oh->length);
1802
1803     ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1804     if (verbosity > 1) {
1805         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1806     }
1807 }
1808
1809 static void
1810 ofp_print_role_message(struct ds *string, const struct ofp_header *oh)
1811 {
1812     struct ofputil_role_request rr;
1813     enum ofperr error;
1814
1815     error = ofputil_decode_role_message(oh, &rr);
1816     if (error) {
1817         ofp_print_error(string, error);
1818         return;
1819     }
1820
1821     ds_put_cstr(string, " role=");
1822
1823     switch (rr.role) {
1824     case OFPCR12_ROLE_NOCHANGE:
1825         ds_put_cstr(string, "nochange");
1826         break;
1827     case OFPCR12_ROLE_EQUAL:
1828         ds_put_cstr(string, "equal"); /* OF 1.2 wording */
1829         break;
1830     case OFPCR12_ROLE_MASTER:
1831         ds_put_cstr(string, "master");
1832         break;
1833     case OFPCR12_ROLE_SLAVE:
1834         ds_put_cstr(string, "slave");
1835         break;
1836     default:
1837         NOT_REACHED();
1838     }
1839
1840     if (rr.have_generation_id) {
1841         ds_put_format(string, " generation_id=%"PRIu64, rr.generation_id);
1842     }
1843 }
1844
1845 static void
1846 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1847                                 const struct nx_flow_mod_table_id *nfmti)
1848 {
1849     ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1850 }
1851
1852 static void
1853 ofp_print_nxt_set_flow_format(struct ds *string,
1854                               const struct nx_set_flow_format *nsff)
1855 {
1856     uint32_t format = ntohl(nsff->format);
1857
1858     ds_put_cstr(string, " format=");
1859     if (ofputil_nx_flow_format_is_valid(format)) {
1860         ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
1861     } else {
1862         ds_put_format(string, "%"PRIu32, format);
1863     }
1864 }
1865
1866 static void
1867 ofp_print_nxt_set_packet_in_format(struct ds *string,
1868                                    const struct nx_set_packet_in_format *nspf)
1869 {
1870     uint32_t format = ntohl(nspf->format);
1871
1872     ds_put_cstr(string, " format=");
1873     if (ofputil_packet_in_format_is_valid(format)) {
1874         ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1875     } else {
1876         ds_put_format(string, "%"PRIu32, format);
1877     }
1878 }
1879
1880 /* Returns a string form of 'reason'.  The return value is either a statically
1881  * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
1882  * 'bufsize' should be at least OFP_PORT_REASON_BUFSIZE. */
1883 #define OFP_PORT_REASON_BUFSIZE (INT_STRLEN(int) + 1)
1884 static const char *
1885 ofp_port_reason_to_string(enum ofp_port_reason reason,
1886                           char *reasonbuf, size_t bufsize)
1887 {
1888     switch (reason) {
1889     case OFPPR_ADD:
1890         return "add";
1891
1892     case OFPPR_DELETE:
1893         return "delete";
1894
1895     case OFPPR_MODIFY:
1896         return "modify";
1897
1898     default:
1899         snprintf(reasonbuf, bufsize, "%d", (int) reason);
1900         return reasonbuf;
1901     }
1902 }
1903
1904 static void
1905 ofp_print_nxt_set_async_config(struct ds *string,
1906                                const struct nx_async_config *nac)
1907 {
1908     int i;
1909
1910     for (i = 0; i < 2; i++) {
1911         int j;
1912
1913         ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1914
1915         ds_put_cstr(string, "       PACKET_IN:");
1916         for (j = 0; j < 32; j++) {
1917             if (nac->packet_in_mask[i] & htonl(1u << j)) {
1918                 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
1919                 const char *reason;
1920
1921                 reason = ofputil_packet_in_reason_to_string(j, reasonbuf,
1922                                                             sizeof reasonbuf);
1923                 ds_put_format(string, " %s", reason);
1924             }
1925         }
1926         if (!nac->packet_in_mask[i]) {
1927             ds_put_cstr(string, " (off)");
1928         }
1929         ds_put_char(string, '\n');
1930
1931         ds_put_cstr(string, "     PORT_STATUS:");
1932         for (j = 0; j < 32; j++) {
1933             if (nac->port_status_mask[i] & htonl(1u << j)) {
1934                 char reasonbuf[OFP_PORT_REASON_BUFSIZE];
1935                 const char *reason;
1936
1937                 reason = ofp_port_reason_to_string(j, reasonbuf,
1938                                                    sizeof reasonbuf);
1939                 ds_put_format(string, " %s", reason);
1940             }
1941         }
1942         if (!nac->port_status_mask[i]) {
1943             ds_put_cstr(string, " (off)");
1944         }
1945         ds_put_char(string, '\n');
1946
1947         ds_put_cstr(string, "    FLOW_REMOVED:");
1948         for (j = 0; j < 32; j++) {
1949             if (nac->flow_removed_mask[i] & htonl(1u << j)) {
1950                 char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
1951                 const char *reason;
1952
1953                 reason = ofp_flow_removed_reason_to_string(j, reasonbuf,
1954                                                            sizeof reasonbuf);
1955                 ds_put_format(string, " %s", reason);
1956             }
1957         }
1958         if (!nac->flow_removed_mask[i]) {
1959             ds_put_cstr(string, " (off)");
1960         }
1961         ds_put_char(string, '\n');
1962     }
1963 }
1964
1965 static void
1966 ofp_print_nxt_set_controller_id(struct ds *string,
1967                                 const struct nx_controller_id *nci)
1968 {
1969     ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
1970 }
1971
1972 static void
1973 ofp_print_nxt_flow_monitor_cancel(struct ds *string,
1974                                   const struct ofp_header *oh)
1975 {
1976     ds_put_format(string, " id=%"PRIu32,
1977                   ofputil_decode_flow_monitor_cancel(oh));
1978 }
1979
1980 static const char *
1981 nx_flow_monitor_flags_to_name(uint32_t bit)
1982 {
1983     enum nx_flow_monitor_flags fmf = bit;
1984
1985     switch (fmf) {
1986     case NXFMF_INITIAL: return "initial";
1987     case NXFMF_ADD: return "add";
1988     case NXFMF_DELETE: return "delete";
1989     case NXFMF_MODIFY: return "modify";
1990     case NXFMF_ACTIONS: return "actions";
1991     case NXFMF_OWN: return "own";
1992     }
1993
1994     return NULL;
1995 }
1996
1997 static void
1998 ofp_print_nxst_flow_monitor_request(struct ds *string,
1999                                     const struct ofp_header *oh)
2000 {
2001     struct ofpbuf b;
2002
2003     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2004     for (;;) {
2005         struct ofputil_flow_monitor_request request;
2006         int retval;
2007
2008         retval = ofputil_decode_flow_monitor_request(&request, &b);
2009         if (retval) {
2010             if (retval != EOF) {
2011                 ofp_print_error(string, retval);
2012             }
2013             return;
2014         }
2015
2016         ds_put_format(string, "\n id=%"PRIu32" flags=", request.id);
2017         ofp_print_bit_names(string, request.flags,
2018                             nx_flow_monitor_flags_to_name, ',');
2019
2020         if (request.out_port != OFPP_NONE) {
2021             ds_put_cstr(string, " out_port=");
2022             ofputil_format_port(request.out_port, string);
2023         }
2024
2025         if (request.table_id != 0xff) {
2026             ds_put_format(string, " table=%"PRIu8, request.table_id);
2027         }
2028
2029         ds_put_char(string, ' ');
2030         match_format(&request.match, string, OFP_DEFAULT_PRIORITY);
2031         ds_chomp(string, ' ');
2032     }
2033 }
2034
2035 static void
2036 ofp_print_nxst_flow_monitor_reply(struct ds *string,
2037                                   const struct ofp_header *oh)
2038 {
2039     uint64_t ofpacts_stub[1024 / 8];
2040     struct ofpbuf ofpacts;
2041     struct ofpbuf b;
2042
2043     ofpbuf_use_const(&b, oh, ntohs(oh->length));
2044     ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2045     for (;;) {
2046         char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
2047         struct ofputil_flow_update update;
2048         struct match match;
2049         int retval;
2050
2051         update.match = &match;
2052         retval = ofputil_decode_flow_update(&update, &b, &ofpacts);
2053         if (retval) {
2054             if (retval != EOF) {
2055                 ofp_print_error(string, retval);
2056             }
2057             ofpbuf_uninit(&ofpacts);
2058             return;
2059         }
2060
2061         ds_put_cstr(string, "\n event=");
2062         switch (update.event) {
2063         case NXFME_ADDED:
2064             ds_put_cstr(string, "ADDED");
2065             break;
2066
2067         case NXFME_DELETED:
2068             ds_put_format(string, "DELETED reason=%s",
2069                           ofp_flow_removed_reason_to_string(update.reason,
2070                                                             reasonbuf,
2071                                                             sizeof reasonbuf));
2072             break;
2073
2074         case NXFME_MODIFIED:
2075             ds_put_cstr(string, "MODIFIED");
2076             break;
2077
2078         case NXFME_ABBREV:
2079             ds_put_format(string, "ABBREV xid=0x%"PRIx32, ntohl(update.xid));
2080             continue;
2081         }
2082
2083         ds_put_format(string, " table=%"PRIu8, update.table_id);
2084         if (update.idle_timeout != OFP_FLOW_PERMANENT) {
2085             ds_put_format(string, " idle_timeout=%"PRIu16,
2086                           update.idle_timeout);
2087         }
2088         if (update.hard_timeout != OFP_FLOW_PERMANENT) {
2089             ds_put_format(string, " hard_timeout=%"PRIu16,
2090                           update.hard_timeout);
2091         }
2092         ds_put_format(string, " cookie=%#"PRIx64, ntohll(update.cookie));
2093
2094         ds_put_char(string, ' ');
2095         match_format(update.match, string, OFP_DEFAULT_PRIORITY);
2096
2097         if (update.ofpacts_len) {
2098             if (string->string[string->length - 1] != ' ') {
2099                 ds_put_char(string, ' ');
2100             }
2101             ofpacts_format(update.ofpacts, update.ofpacts_len, string);
2102         }
2103     }
2104 }
2105
2106 void
2107 ofp_print_version(const struct ofp_header *oh,
2108                   struct ds *string)
2109 {
2110     switch (oh->version) {
2111     case OFP10_VERSION:
2112         break;
2113     case OFP11_VERSION:
2114         ds_put_cstr(string, " (OF1.1)");
2115         break;
2116     case OFP12_VERSION:
2117         ds_put_cstr(string, " (OF1.2)");
2118         break;
2119     case OFP13_VERSION:
2120         ds_put_cstr(string, " (OF1.3)");
2121         break;
2122     default:
2123         ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
2124         break;
2125     }
2126     ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
2127 }
2128
2129 static void
2130 ofp_header_to_string__(const struct ofp_header *oh, enum ofpraw raw,
2131                        struct ds *string)
2132 {
2133     ds_put_cstr(string, ofpraw_get_name(raw));
2134     ofp_print_version(oh, string);
2135 }
2136
2137 static void
2138 ofp_print_not_implemented(struct ds *string)
2139 {
2140     ds_put_cstr(string, "NOT IMPLEMENTED YET!\n");
2141 }
2142
2143 static void
2144 ofp_to_string__(const struct ofp_header *oh, enum ofpraw raw,
2145                 struct ds *string, int verbosity)
2146 {
2147     const void *msg = oh;
2148
2149     ofp_header_to_string__(oh, raw, string);
2150     switch (ofptype_from_ofpraw(raw)) {
2151
2152         /* FIXME: Change the following once they are implemented: */
2153     case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
2154     case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
2155     case OFPTYPE_GET_ASYNC_REQUEST:
2156     case OFPTYPE_GET_ASYNC_REPLY:
2157     case OFPTYPE_GROUP_STATS_REQUEST:
2158     case OFPTYPE_GROUP_STATS_REPLY:
2159     case OFPTYPE_GROUP_DESC_STATS_REQUEST:
2160     case OFPTYPE_GROUP_DESC_STATS_REPLY:
2161     case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
2162     case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
2163     case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
2164     case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
2165         ofp_print_not_implemented(string);
2166         break;
2167
2168     case OFPTYPE_HELLO:
2169         ofp_print_hello(string, oh);
2170         break;
2171
2172     case OFPTYPE_ERROR:
2173         ofp_print_error_msg(string, oh);
2174         break;
2175
2176     case OFPTYPE_ECHO_REQUEST:
2177     case OFPTYPE_ECHO_REPLY:
2178         ofp_print_echo(string, oh, verbosity);
2179         break;
2180
2181     case OFPTYPE_FEATURES_REQUEST:
2182         break;
2183
2184     case OFPTYPE_FEATURES_REPLY:
2185         ofp_print_switch_features(string, oh);
2186         break;
2187
2188     case OFPTYPE_GET_CONFIG_REQUEST:
2189         break;
2190
2191     case OFPTYPE_GET_CONFIG_REPLY:
2192     case OFPTYPE_SET_CONFIG:
2193         ofp_print_switch_config(string, ofpmsg_body(oh));
2194         break;
2195
2196     case OFPTYPE_PACKET_IN:
2197         ofp_print_packet_in(string, oh, verbosity);
2198         break;
2199
2200     case OFPTYPE_FLOW_REMOVED:
2201         ofp_print_flow_removed(string, oh);
2202         break;
2203
2204     case OFPTYPE_PORT_STATUS:
2205         ofp_print_port_status(string, oh);
2206         break;
2207
2208     case OFPTYPE_PACKET_OUT:
2209         ofp_print_packet_out(string, oh, verbosity);
2210         break;
2211
2212     case OFPTYPE_FLOW_MOD:
2213         ofp_print_flow_mod(string, oh, verbosity);
2214         break;
2215
2216     case OFPTYPE_PORT_MOD:
2217         ofp_print_port_mod(string, oh);
2218         break;
2219
2220     case OFPTYPE_METER_MOD:
2221         ofp_print_meter_mod(string, oh);
2222         break;
2223
2224     case OFPTYPE_BARRIER_REQUEST:
2225     case OFPTYPE_BARRIER_REPLY:
2226         break;
2227
2228     case OFPTYPE_ROLE_REQUEST:
2229     case OFPTYPE_ROLE_REPLY:
2230         ofp_print_role_message(string, oh);
2231         break;
2232
2233     case OFPTYPE_METER_STATS_REQUEST:
2234     case OFPTYPE_METER_CONFIG_STATS_REQUEST:
2235         ofp_print_stats_request(string, oh);
2236         ofp_print_meter_stats_request(string, oh);
2237         break;
2238
2239     case OFPTYPE_METER_STATS_REPLY:
2240         ofp_print_stats_reply(string, oh);
2241         ofp_print_meter_stats_reply(string, oh);
2242         break;
2243
2244     case OFPTYPE_METER_CONFIG_STATS_REPLY:
2245         ofp_print_stats_reply(string, oh);
2246         ofp_print_meter_config_reply(string, oh);
2247         break;
2248
2249     case OFPTYPE_METER_FEATURES_STATS_REPLY:
2250         ofp_print_stats_reply(string, oh);
2251         ofp_print_meter_features_reply(string, oh);
2252         break;
2253
2254     case OFPTYPE_DESC_STATS_REQUEST:
2255     case OFPTYPE_PORT_DESC_STATS_REQUEST:
2256     case OFPTYPE_METER_FEATURES_STATS_REQUEST:
2257         ofp_print_stats_request(string, oh);
2258         break;
2259
2260     case OFPTYPE_FLOW_STATS_REQUEST:
2261     case OFPTYPE_AGGREGATE_STATS_REQUEST:
2262         ofp_print_stats_request(string, oh);
2263         ofp_print_flow_stats_request(string, oh);
2264         break;
2265
2266     case OFPTYPE_TABLE_STATS_REQUEST:
2267         ofp_print_stats_request(string, oh);
2268         break;
2269
2270     case OFPTYPE_PORT_STATS_REQUEST:
2271         ofp_print_stats_request(string, oh);
2272         ofp_print_ofpst_port_request(string, oh);
2273         break;
2274
2275     case OFPTYPE_QUEUE_STATS_REQUEST:
2276         ofp_print_stats_request(string, oh);
2277         ofp_print_ofpst_queue_request(string, oh);
2278         break;
2279
2280     case OFPTYPE_DESC_STATS_REPLY:
2281         ofp_print_stats_reply(string, oh);
2282         ofp_print_ofpst_desc_reply(string, oh);
2283         break;
2284
2285     case OFPTYPE_FLOW_STATS_REPLY:
2286         ofp_print_stats_reply(string, oh);
2287         ofp_print_flow_stats_reply(string, oh);
2288         break;
2289
2290     case OFPTYPE_QUEUE_STATS_REPLY:
2291         ofp_print_stats_reply(string, oh);
2292         ofp_print_ofpst_queue_reply(string, oh, verbosity);
2293         break;
2294
2295     case OFPTYPE_PORT_STATS_REPLY:
2296         ofp_print_stats_reply(string, oh);
2297         ofp_print_ofpst_port_reply(string, oh, verbosity);
2298         break;
2299
2300     case OFPTYPE_TABLE_STATS_REPLY:
2301         ofp_print_stats_reply(string, oh);
2302         ofp_print_ofpst_table_reply(string, oh, verbosity);
2303         break;
2304
2305     case OFPTYPE_AGGREGATE_STATS_REPLY:
2306         ofp_print_stats_reply(string, oh);
2307         ofp_print_aggregate_stats_reply(string, oh);
2308         break;
2309
2310     case OFPTYPE_PORT_DESC_STATS_REPLY:
2311         ofp_print_stats_reply(string, oh);
2312         ofp_print_ofpst_port_desc_reply(string, oh);
2313         break;
2314
2315     case OFPTYPE_FLOW_MOD_TABLE_ID:
2316         ofp_print_nxt_flow_mod_table_id(string, ofpmsg_body(oh));
2317         break;
2318
2319     case OFPTYPE_SET_FLOW_FORMAT:
2320         ofp_print_nxt_set_flow_format(string, ofpmsg_body(oh));
2321         break;
2322
2323     case OFPTYPE_SET_PACKET_IN_FORMAT:
2324         ofp_print_nxt_set_packet_in_format(string, ofpmsg_body(oh));
2325         break;
2326
2327     case OFPTYPE_FLOW_AGE:
2328         break;
2329
2330     case OFPTYPE_SET_CONTROLLER_ID:
2331         ofp_print_nxt_set_controller_id(string, ofpmsg_body(oh));
2332         break;
2333
2334     case OFPTYPE_SET_ASYNC_CONFIG:
2335         ofp_print_nxt_set_async_config(string, ofpmsg_body(oh));
2336         break;
2337
2338     case OFPTYPE_FLOW_MONITOR_CANCEL:
2339         ofp_print_nxt_flow_monitor_cancel(string, msg);
2340         break;
2341
2342     case OFPTYPE_FLOW_MONITOR_PAUSED:
2343     case OFPTYPE_FLOW_MONITOR_RESUMED:
2344         break;
2345
2346     case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
2347         ofp_print_nxst_flow_monitor_request(string, msg);
2348         break;
2349
2350     case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
2351         ofp_print_nxst_flow_monitor_reply(string, msg);
2352         break;
2353     }
2354 }
2355
2356 /* Composes and returns a string representing the OpenFlow packet of 'len'
2357  * bytes at 'oh' at the given 'verbosity' level.  0 is a minimal amount of
2358  * verbosity and higher numbers increase verbosity.  The caller is responsible
2359  * for freeing the string. */
2360 char *
2361 ofp_to_string(const void *oh_, size_t len, int verbosity)
2362 {
2363     struct ds string = DS_EMPTY_INITIALIZER;
2364     const struct ofp_header *oh = oh_;
2365
2366     if (!len) {
2367         ds_put_cstr(&string, "OpenFlow message is empty\n");
2368     } else if (len < sizeof(struct ofp_header)) {
2369         ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
2370                       len);
2371     } else if (ntohs(oh->length) > len) {
2372         enum ofperr error;
2373         enum ofpraw raw;
2374
2375         error = ofpraw_decode_partial(&raw, oh, len);
2376         if (!error) {
2377             ofp_header_to_string__(oh, raw, &string);
2378             ds_put_char(&string, '\n');
2379         }
2380
2381         ds_put_format(&string,
2382                       "(***truncated to %zu bytes from %"PRIu16"***)\n",
2383                       len, ntohs(oh->length));
2384     } else if (ntohs(oh->length) < len) {
2385         ds_put_format(&string,
2386                       "(***only uses %"PRIu16" bytes out of %zu***)\n",
2387                       ntohs(oh->length), len);
2388     } else {
2389         enum ofperr error;
2390         enum ofpraw raw;
2391
2392         error = ofpraw_decode(&raw, oh);
2393         if (!error) {
2394             ofp_to_string__(oh, raw, &string, verbosity);
2395             if (verbosity >= 5) {
2396                 if (ds_last(&string) != '\n') {
2397                     ds_put_char(&string, '\n');
2398                 }
2399                 ds_put_hex_dump(&string, oh, len, 0, true);
2400             }
2401             if (ds_last(&string) != '\n') {
2402                 ds_put_char(&string, '\n');
2403             }
2404             return ds_steal_cstr(&string);
2405         }
2406
2407         ofp_print_error(&string, error);
2408     }
2409     ds_put_hex_dump(&string, oh, len, 0, true);
2410     return ds_steal_cstr(&string);
2411 }
2412 \f
2413 static void
2414 print_and_free(FILE *stream, char *string)
2415 {
2416     fputs(string, stream);
2417     free(string);
2418 }
2419
2420 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
2421  * given 'verbosity' level.  0 is a minimal amount of verbosity and higher
2422  * numbers increase verbosity. */
2423 void
2424 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
2425 {
2426     print_and_free(stream, ofp_to_string(oh, len, verbosity));
2427 }
2428
2429 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
2430  * 'data' to 'stream'. */
2431 void
2432 ofp_print_packet(FILE *stream, const void *data, size_t len)
2433 {
2434     print_and_free(stream, ofp_packet_to_string(data, len));
2435 }