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