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