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