ofp-print: Slightly extend ofp_print_bit_names().
[openvswitch] / 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-util.h"
42 #include "ofpbuf.h"
43 #include "openflow/openflow.h"
44 #include "openflow/nicira-ext.h"
45 #include "packets.h"
46 #include "pcap.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, 0, &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     struct ofputil_packet_in pin;
90     int error;
91     int i;
92
93     error = ofputil_decode_packet_in(&pin, oh);
94     if (error) {
95         ofp_print_error(string, error);
96         return;
97     }
98
99     if (pin.table_id) {
100         ds_put_format(string, " table_id=%"PRIu8, pin.table_id);
101     }
102
103     if (pin.cookie) {
104         ds_put_format(string, " cookie=0x%"PRIx64, ntohll(pin.cookie));
105     }
106
107     ds_put_format(string, " total_len=%"PRIu16" in_port=", pin.total_len);
108     ofputil_format_port(pin.fmd.in_port, string);
109
110     if (pin.fmd.tun_id_mask) {
111         ds_put_format(string, " tun_id=0x%"PRIx64, ntohll(pin.fmd.tun_id));
112         if (pin.fmd.tun_id_mask != htonll(UINT64_MAX)) {
113             ds_put_format(string, "/0x%"PRIx64, ntohll(pin.fmd.tun_id_mask));
114         }
115     }
116
117     if (pin.fmd.metadata_mask) {
118         ds_put_format(string, " metadata=0x%"PRIx64, ntohll(pin.fmd.metadata));
119         if (pin.fmd.metadata_mask != htonll(UINT64_MAX)) {
120             ds_put_format(string, "/0x%"PRIx64, ntohll(pin.fmd.metadata_mask));
121         }
122     }
123
124     for (i = 0; i < FLOW_N_REGS; i++) {
125         if (pin.fmd.reg_masks[i]) {
126             ds_put_format(string, " reg%d=0x%"PRIx32, i, pin.fmd.regs[i]);
127             if (pin.fmd.reg_masks[i] != UINT32_MAX) {
128                 ds_put_format(string, "/0x%"PRIx32, pin.fmd.reg_masks[i]);
129             }
130         }
131     }
132
133     ds_put_format(string, " (via %s)",
134                   ofputil_packet_in_reason_to_string(pin.reason));
135
136     ds_put_format(string, " data_len=%zu", pin.packet_len);
137     if (pin.buffer_id == UINT32_MAX) {
138         ds_put_format(string, " (unbuffered)");
139         if (pin.total_len != pin.packet_len) {
140             ds_put_format(string, " (***total_len != data_len***)");
141         }
142     } else {
143         ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
144         if (pin.total_len < pin.packet_len) {
145             ds_put_format(string, " (***total_len < data_len***)");
146         }
147     }
148     ds_put_char(string, '\n');
149
150     if (verbosity > 0) {
151         char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
152         ds_put_cstr(string, packet);
153         free(packet);
154     }
155 }
156
157 static void
158 ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
159                      int verbosity)
160 {
161     struct ofputil_packet_out po;
162     struct ofpbuf ofpacts;
163     enum ofperr error;
164
165     ofpbuf_init(&ofpacts, 64);
166     error = ofputil_decode_packet_out(&po, opo, &ofpacts);
167     if (error) {
168         ofpbuf_uninit(&ofpacts);
169         ofp_print_error(string, error);
170         return;
171     }
172
173     ds_put_cstr(string, " in_port=");
174     ofputil_format_port(po.in_port, string);
175
176     ds_put_char(string, ' ');
177     ofpacts_format(po.ofpacts, po.ofpacts_len, string);
178
179     if (po.buffer_id == UINT32_MAX) {
180         ds_put_format(string, " data_len=%zu", po.packet_len);
181         if (verbosity > 0 && po.packet_len > 0) {
182             char *packet = ofp_packet_to_string(po.packet, po.packet_len);
183             ds_put_char(string, '\n');
184             ds_put_cstr(string, packet);
185             free(packet);
186         }
187     } else {
188         ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id);
189     }
190     ds_put_char(string, '\n');
191
192     ofpbuf_uninit(&ofpacts);
193 }
194
195 /* qsort comparison function. */
196 static int
197 compare_ports(const void *a_, const void *b_)
198 {
199     const struct ofputil_phy_port *a = a_;
200     const struct ofputil_phy_port *b = b_;
201     uint16_t ap = a->port_no;
202     uint16_t bp = b->port_no;
203
204     return ap < bp ? -1 : ap > bp;
205 }
206
207 static void
208 ofp_print_bit_names(struct ds *string, uint32_t bits,
209                     const char *(*bit_to_name)(uint32_t bit),
210                     char separator)
211 {
212     int n = 0;
213     int i;
214
215     if (!bits) {
216         ds_put_cstr(string, "0");
217         return;
218     }
219
220     for (i = 0; i < 32; i++) {
221         uint32_t bit = UINT32_C(1) << i;
222
223         if (bits & bit) {
224             const char *name = bit_to_name(bit);
225             if (name) {
226                 if (n++) {
227                     ds_put_char(string, separator);
228                 }
229                 ds_put_cstr(string, name);
230                 bits &= ~bit;
231             }
232         }
233     }
234
235     if (bits) {
236         if (n) {
237             ds_put_char(string, separator);
238         }
239         ds_put_format(string, "0x%"PRIx32, bits);
240     }
241 }
242
243 static const char *
244 netdev_feature_to_name(uint32_t bit)
245 {
246     enum netdev_features f = bit;
247
248     switch (f) {
249     case NETDEV_F_10MB_HD:    return "10MB-HD";
250     case NETDEV_F_10MB_FD:    return "10MB-FD";
251     case NETDEV_F_100MB_HD:   return "100MB-HD";
252     case NETDEV_F_100MB_FD:   return "100MB-FD";
253     case NETDEV_F_1GB_HD:     return "1GB-HD";
254     case NETDEV_F_1GB_FD:     return "1GB-FD";
255     case NETDEV_F_10GB_FD:    return "10GB-FD";
256     case NETDEV_F_40GB_FD:    return "40GB-FD";
257     case NETDEV_F_100GB_FD:   return "100GB-FD";
258     case NETDEV_F_1TB_FD:     return "1TB-FD";
259     case NETDEV_F_OTHER:      return "OTHER";
260     case NETDEV_F_COPPER:     return "COPPER";
261     case NETDEV_F_FIBER:      return "FIBER";
262     case NETDEV_F_AUTONEG:    return "AUTO_NEG";
263     case NETDEV_F_PAUSE:      return "AUTO_PAUSE";
264     case NETDEV_F_PAUSE_ASYM: return "AUTO_PAUSE_ASYM";
265     }
266
267     return NULL;
268 }
269
270 static void
271 ofp_print_port_features(struct ds *string, enum netdev_features features)
272 {
273     ofp_print_bit_names(string, features, netdev_feature_to_name, ' ');
274     ds_put_char(string, '\n');
275 }
276
277 static const char *
278 ofputil_port_config_to_name(uint32_t bit)
279 {
280     enum ofputil_port_config pc = bit;
281
282     switch (pc) {
283     case OFPUTIL_PC_PORT_DOWN:    return "PORT_DOWN";
284     case OFPUTIL_PC_NO_STP:       return "NO_STP";
285     case OFPUTIL_PC_NO_RECV:      return "NO_RECV";
286     case OFPUTIL_PC_NO_RECV_STP:  return "NO_RECV_STP";
287     case OFPUTIL_PC_NO_FLOOD:     return "NO_FLOOD";
288     case OFPUTIL_PC_NO_FWD:       return "NO_FWD";
289     case OFPUTIL_PC_NO_PACKET_IN: return "NO_PACKET_IN";
290     }
291
292     return NULL;
293 }
294
295 static void
296 ofp_print_port_config(struct ds *string, enum ofputil_port_config config)
297 {
298     ofp_print_bit_names(string, config, ofputil_port_config_to_name, ' ');
299     ds_put_char(string, '\n');
300 }
301
302 static const char *
303 ofputil_port_state_to_name(uint32_t bit)
304 {
305     enum ofputil_port_state ps = bit;
306
307     switch (ps) {
308     case OFPUTIL_PS_LINK_DOWN: return "LINK_DOWN";
309     case OFPUTIL_PS_BLOCKED:   return "BLOCKED";
310     case OFPUTIL_PS_LIVE:      return "LIVE";
311
312     case OFPUTIL_PS_STP_LISTEN:
313     case OFPUTIL_PS_STP_LEARN:
314     case OFPUTIL_PS_STP_FORWARD:
315     case OFPUTIL_PS_STP_BLOCK:
316         /* Handled elsewhere. */
317         return NULL;
318     }
319
320     return NULL;
321 }
322
323 static void
324 ofp_print_port_state(struct ds *string, enum ofputil_port_state state)
325 {
326     enum ofputil_port_state stp_state;
327
328     /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
329      * pattern.  We have to special case it.
330      *
331      * OVS doesn't support STP, so this field will always be 0 if we are
332      * talking to OVS, so we'd always print STP_LISTEN in that case.
333      * Therefore, we don't print anything at all if the value is STP_LISTEN, to
334      * avoid confusing users. */
335     stp_state = state & OFPUTIL_PS_STP_MASK;
336     if (stp_state) {
337         ds_put_cstr(string,
338                     (stp_state == OFPUTIL_PS_STP_LEARN ? "STP_LEARN"
339                      : stp_state == OFPUTIL_PS_STP_FORWARD ? "STP_FORWARD"
340                      : "STP_BLOCK"));
341         state &= ~OFPUTIL_PS_STP_MASK;
342         if (state) {
343             ofp_print_bit_names(string, state, ofputil_port_state_to_name,
344                                 ' ');
345         }
346     } else {
347         ofp_print_bit_names(string, state, ofputil_port_state_to_name, ' ');
348     }
349     ds_put_char(string, '\n');
350 }
351
352 static void
353 ofp_print_phy_port(struct ds *string, const struct ofputil_phy_port *port)
354 {
355     char name[sizeof port->name];
356     int j;
357
358     memcpy(name, port->name, sizeof name);
359     for (j = 0; j < sizeof name - 1; j++) {
360         if (!isprint((unsigned char) name[j])) {
361             break;
362         }
363     }
364     name[j] = '\0';
365
366     ds_put_char(string, ' ');
367     ofputil_format_port(port->port_no, string);
368     ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
369                   name, ETH_ADDR_ARGS(port->hw_addr));
370
371     ds_put_cstr(string, "     config:     ");
372     ofp_print_port_config(string, port->config);
373
374     ds_put_cstr(string, "     state:      ");
375     ofp_print_port_state(string, port->state);
376
377     if (port->curr) {
378         ds_put_format(string, "     current:    ");
379         ofp_print_port_features(string, port->curr);
380     }
381     if (port->advertised) {
382         ds_put_format(string, "     advertised: ");
383         ofp_print_port_features(string, port->advertised);
384     }
385     if (port->supported) {
386         ds_put_format(string, "     supported:  ");
387         ofp_print_port_features(string, port->supported);
388     }
389     if (port->peer) {
390         ds_put_format(string, "     peer:       ");
391         ofp_print_port_features(string, port->peer);
392     }
393     ds_put_format(string, "     speed: %"PRIu32" Mbps now, "
394                   "%"PRIu32" Mbps max\n",
395                   port->curr_speed / UINT32_C(1000),
396                   port->max_speed / UINT32_C(1000));
397 }
398
399 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
400  * 'ofp_version', writes a detailed description of each port into
401  * 'string'. */
402 static void
403 ofp_print_phy_ports(struct ds *string, uint8_t ofp_version,
404                     struct ofpbuf *b)
405 {
406     size_t n_ports;
407     struct ofputil_phy_port *ports;
408     enum ofperr error;
409     size_t i;
410
411     n_ports = ofputil_count_phy_ports(ofp_version, b);
412
413     ports = xmalloc(n_ports * sizeof *ports);
414     for (i = 0; i < n_ports; i++) {
415         error = ofputil_pull_phy_port(ofp_version, b, &ports[i]);
416         if (error) {
417             ofp_print_error(string, error);
418             goto exit;
419         }
420     }
421     qsort(ports, n_ports, sizeof *ports, compare_ports);
422     for (i = 0; i < n_ports; i++) {
423         ofp_print_phy_port(string, &ports[i]);
424     }
425
426 exit:
427     free(ports);
428 }
429
430 static const char *
431 ofputil_capabilities_to_name(uint32_t bit)
432 {
433     enum ofputil_capabilities capabilities = bit;
434
435     switch (capabilities) {
436     case OFPUTIL_C_FLOW_STATS:   return "FLOW_STATS";
437     case OFPUTIL_C_TABLE_STATS:  return "TABLE_STATS";
438     case OFPUTIL_C_PORT_STATS:   return "PORT_STATS";
439     case OFPUTIL_C_IP_REASM:     return "IP_REASM";
440     case OFPUTIL_C_QUEUE_STATS:  return "QUEUE_STATS";
441     case OFPUTIL_C_ARP_MATCH_IP: return "ARP_MATCH_IP";
442     case OFPUTIL_C_STP:          return "STP";
443     case OFPUTIL_C_GROUP_STATS:  return "GROUP_STATS";
444     }
445
446     return NULL;
447 }
448
449 static const char *
450 ofputil_action_bitmap_to_name(uint32_t bit)
451 {
452     enum ofputil_action_bitmap action = bit;
453
454     switch (action) {
455     case OFPUTIL_A_OUTPUT:         return "OUTPUT";
456     case OFPUTIL_A_SET_VLAN_VID:   return "SET_VLAN_VID";
457     case OFPUTIL_A_SET_VLAN_PCP:   return "SET_VLAN_PCP";
458     case OFPUTIL_A_STRIP_VLAN:     return "STRIP_VLAN";
459     case OFPUTIL_A_SET_DL_SRC:     return "SET_DL_SRC";
460     case OFPUTIL_A_SET_DL_DST:     return "SET_DL_DST";
461     case OFPUTIL_A_SET_NW_SRC:     return "SET_NW_SRC";
462     case OFPUTIL_A_SET_NW_DST:     return "SET_NW_DST";
463     case OFPUTIL_A_SET_NW_ECN:     return "SET_NW_ECN";
464     case OFPUTIL_A_SET_NW_TOS:     return "SET_NW_TOS";
465     case OFPUTIL_A_SET_TP_SRC:     return "SET_TP_SRC";
466     case OFPUTIL_A_SET_TP_DST:     return "SET_TP_DST";
467     case OFPUTIL_A_ENQUEUE:        return "ENQUEUE";
468     case OFPUTIL_A_COPY_TTL_OUT:   return "COPY_TTL_OUT";
469     case OFPUTIL_A_COPY_TTL_IN:    return "COPY_TTL_IN";
470     case OFPUTIL_A_SET_MPLS_LABEL: return "SET_MPLS_LABEL";
471     case OFPUTIL_A_SET_MPLS_TC:    return "SET_MPLS_TC";
472     case OFPUTIL_A_SET_MPLS_TTL:   return "SET_MPLS_TTL";
473     case OFPUTIL_A_DEC_MPLS_TTL:   return "DEC_MPLS_TTL";
474     case OFPUTIL_A_PUSH_VLAN:      return "PUSH_VLAN";
475     case OFPUTIL_A_POP_VLAN:       return "POP_VLAN";
476     case OFPUTIL_A_PUSH_MPLS:      return "PUSH_MPLS";
477     case OFPUTIL_A_POP_MPLS:       return "POP_MPLS";
478     case OFPUTIL_A_SET_QUEUE:      return "SET_QUEUE";
479     case OFPUTIL_A_GROUP:          return "GROUP";
480     case OFPUTIL_A_SET_NW_TTL:     return "SET_NW_TTL";
481     case OFPUTIL_A_DEC_NW_TTL:     return "DEC_NW_TTL";
482     }
483
484     return NULL;
485 }
486
487 static void
488 ofp_print_switch_features(struct ds *string,
489                           const struct ofp_switch_features *osf)
490 {
491     struct ofputil_switch_features features;
492     enum ofperr error;
493     struct ofpbuf b;
494
495     error = ofputil_decode_switch_features(osf, &features, &b);
496     if (error) {
497         ofp_print_error(string, error);
498         return;
499     }
500
501     ds_put_format(string, " dpid:%016"PRIx64"\n", features.datapath_id);
502     ds_put_format(string, "n_tables:%"PRIu8", n_buffers:%"PRIu32"\n",
503                   features.n_tables, features.n_buffers);
504
505     ds_put_cstr(string, "capabilities: ");
506     ofp_print_bit_names(string, features.capabilities,
507                         ofputil_capabilities_to_name, ' ');
508     ds_put_char(string, '\n');
509
510     ds_put_cstr(string, "actions: ");
511     ofp_print_bit_names(string, features.actions,
512                         ofputil_action_bitmap_to_name, ' ');
513     ds_put_char(string, '\n');
514
515     ofp_print_phy_ports(string, osf->header.version, &b);
516 }
517
518 static void
519 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
520 {
521     enum ofp_config_flags flags;
522
523     flags = ntohs(osc->flags);
524
525     ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
526     flags &= ~OFPC_FRAG_MASK;
527
528     if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) {
529         ds_put_format(string, " invalid_ttl_to_controller");
530         flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER;
531     }
532
533     if (flags) {
534         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
535     }
536
537     ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
538 }
539
540 static void print_wild(struct ds *string, const char *leader, int is_wild,
541             int verbosity, const char *format, ...)
542             __attribute__((format(printf, 5, 6)));
543
544 static void print_wild(struct ds *string, const char *leader, int is_wild,
545                        int verbosity, const char *format, ...)
546 {
547     if (is_wild && verbosity < 2) {
548         return;
549     }
550     ds_put_cstr(string, leader);
551     if (!is_wild) {
552         va_list args;
553
554         va_start(args, format);
555         ds_put_format_valist(string, format, args);
556         va_end(args);
557     } else {
558         ds_put_char(string, '*');
559     }
560     ds_put_char(string, ',');
561 }
562
563 static void
564 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
565                  uint32_t wild_bits, int verbosity)
566 {
567     if (wild_bits >= 32 && verbosity < 2) {
568         return;
569     }
570     ds_put_cstr(string, leader);
571     if (wild_bits < 32) {
572         ds_put_format(string, IP_FMT, IP_ARGS(&ip));
573         if (wild_bits) {
574             ds_put_format(string, "/%d", 32 - wild_bits);
575         }
576     } else {
577         ds_put_char(string, '*');
578     }
579     ds_put_char(string, ',');
580 }
581
582 void
583 ofp10_match_print(struct ds *f, const struct ofp10_match *om, int verbosity)
584 {
585     char *s = ofp10_match_to_string(om, verbosity);
586     ds_put_cstr(f, s);
587     free(s);
588 }
589
590 char *
591 ofp10_match_to_string(const struct ofp10_match *om, int verbosity)
592 {
593     struct ds f = DS_EMPTY_INITIALIZER;
594     uint32_t w = ntohl(om->wildcards);
595     bool skip_type = false;
596     bool skip_proto = false;
597
598     if (!(w & OFPFW10_DL_TYPE)) {
599         skip_type = true;
600         if (om->dl_type == htons(ETH_TYPE_IP)) {
601             if (!(w & OFPFW10_NW_PROTO)) {
602                 skip_proto = true;
603                 if (om->nw_proto == IPPROTO_ICMP) {
604                     ds_put_cstr(&f, "icmp,");
605                 } else if (om->nw_proto == IPPROTO_TCP) {
606                     ds_put_cstr(&f, "tcp,");
607                 } else if (om->nw_proto == IPPROTO_UDP) {
608                     ds_put_cstr(&f, "udp,");
609                 } else {
610                     ds_put_cstr(&f, "ip,");
611                     skip_proto = false;
612                 }
613             } else {
614                 ds_put_cstr(&f, "ip,");
615             }
616         } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
617             ds_put_cstr(&f, "arp,");
618         } else {
619             skip_type = false;
620         }
621     }
622     print_wild(&f, "in_port=", w & OFPFW10_IN_PORT, verbosity,
623                "%d", ntohs(om->in_port));
624     print_wild(&f, "dl_vlan=", w & OFPFW10_DL_VLAN, verbosity,
625                "%d", ntohs(om->dl_vlan));
626     print_wild(&f, "dl_vlan_pcp=", w & OFPFW10_DL_VLAN_PCP, verbosity,
627                "%d", om->dl_vlan_pcp);
628     print_wild(&f, "dl_src=", w & OFPFW10_DL_SRC, verbosity,
629                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
630     print_wild(&f, "dl_dst=", w & OFPFW10_DL_DST, verbosity,
631                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
632     if (!skip_type) {
633         print_wild(&f, "dl_type=", w & OFPFW10_DL_TYPE, verbosity,
634                    "0x%04x", ntohs(om->dl_type));
635     }
636     print_ip_netmask(&f, "nw_src=", om->nw_src,
637                      (w & OFPFW10_NW_SRC_MASK) >> OFPFW10_NW_SRC_SHIFT,
638                      verbosity);
639     print_ip_netmask(&f, "nw_dst=", om->nw_dst,
640                      (w & OFPFW10_NW_DST_MASK) >> OFPFW10_NW_DST_SHIFT,
641                      verbosity);
642     if (!skip_proto) {
643         if (om->dl_type == htons(ETH_TYPE_ARP)) {
644             print_wild(&f, "arp_op=", w & OFPFW10_NW_PROTO, verbosity,
645                        "%u", om->nw_proto);
646         } else {
647             print_wild(&f, "nw_proto=", w & OFPFW10_NW_PROTO, verbosity,
648                        "%u", om->nw_proto);
649         }
650     }
651     print_wild(&f, "nw_tos=", w & OFPFW10_NW_TOS, verbosity,
652                "%u", om->nw_tos);
653     if (om->nw_proto == IPPROTO_ICMP) {
654         print_wild(&f, "icmp_type=", w & OFPFW10_ICMP_TYPE, verbosity,
655                    "%d", ntohs(om->tp_src));
656         print_wild(&f, "icmp_code=", w & OFPFW10_ICMP_CODE, verbosity,
657                    "%d", ntohs(om->tp_dst));
658     } else {
659         print_wild(&f, "tp_src=", w & OFPFW10_TP_SRC, verbosity,
660                    "%d", ntohs(om->tp_src));
661         print_wild(&f, "tp_dst=", w & OFPFW10_TP_DST, verbosity,
662                    "%d", ntohs(om->tp_dst));
663     }
664     if (ds_last(&f) == ',') {
665         f.length--;
666     }
667     return ds_cstr(&f);
668 }
669
670 static void
671 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
672                    enum ofputil_msg_code code, int verbosity)
673 {
674     struct ofputil_flow_mod fm;
675     struct ofpbuf ofpacts;
676     bool need_priority;
677     enum ofperr error;
678
679     ofpbuf_init(&ofpacts, 64);
680     error = ofputil_decode_flow_mod(&fm, oh, OFPUTIL_P_OF10_TID, &ofpacts);
681     if (error) {
682         ofpbuf_uninit(&ofpacts);
683         ofp_print_error(s, error);
684         return;
685     }
686
687     ds_put_char(s, ' ');
688     switch (fm.command) {
689     case OFPFC_ADD:
690         ds_put_cstr(s, "ADD");
691         break;
692     case OFPFC_MODIFY:
693         ds_put_cstr(s, "MOD");
694         break;
695     case OFPFC_MODIFY_STRICT:
696         ds_put_cstr(s, "MOD_STRICT");
697         break;
698     case OFPFC_DELETE:
699         ds_put_cstr(s, "DEL");
700         break;
701     case OFPFC_DELETE_STRICT:
702         ds_put_cstr(s, "DEL_STRICT");
703         break;
704     default:
705         ds_put_format(s, "cmd:%d", fm.command);
706     }
707     if (fm.table_id != 0) {
708         ds_put_format(s, " table:%d", fm.table_id);
709     }
710
711     ds_put_char(s, ' ');
712     if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
713         const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
714         ofp10_match_print(s, &ofm->match, verbosity);
715
716         /* ofp_print_match() doesn't print priority. */
717         need_priority = true;
718     } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
719         const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
720         const void *nxm = nfm + 1;
721         char *nxm_s;
722
723         nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
724         ds_put_cstr(s, nxm_s);
725         free(nxm_s);
726
727         /* nx_match_to_string() doesn't print priority. */
728         need_priority = true;
729     } else {
730         cls_rule_format(&fm.cr, s);
731
732         /* cls_rule_format() does print priority. */
733         need_priority = false;
734     }
735
736     if (ds_last(s) != ' ') {
737         ds_put_char(s, ' ');
738     }
739     if (fm.new_cookie != htonll(0)) {
740         ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.new_cookie));
741     }
742     if (fm.cookie_mask != htonll(0)) {
743         ds_put_format(s, "cookie:0x%"PRIx64"/0x%"PRIx64" ",
744                 ntohll(fm.cookie), ntohll(fm.cookie_mask));
745     }
746     if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
747         ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
748     }
749     if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
750         ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
751     }
752     if (fm.cr.priority != OFP_DEFAULT_PRIORITY && need_priority) {
753         ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
754     }
755     if (fm.buffer_id != UINT32_MAX) {
756         ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
757     }
758     if (fm.out_port != OFPP_NONE) {
759         ds_put_format(s, "out_port:");
760         ofputil_format_port(fm.out_port, s);
761         ds_put_char(s, ' ');
762     }
763     if (fm.flags != 0) {
764         uint16_t flags = fm.flags;
765
766         if (flags & OFPFF_SEND_FLOW_REM) {
767             ds_put_cstr(s, "send_flow_rem ");
768         }
769         if (flags & OFPFF_CHECK_OVERLAP) {
770             ds_put_cstr(s, "check_overlap ");
771         }
772         if (flags & OFPFF_EMERG) {
773             ds_put_cstr(s, "emerg ");
774         }
775
776         flags &= ~(OFPFF_SEND_FLOW_REM | OFPFF_CHECK_OVERLAP | OFPFF_EMERG);
777         if (flags) {
778             ds_put_format(s, "flags:0x%"PRIx16" ", flags);
779         }
780     }
781
782     ofpacts_format(fm.ofpacts, fm.ofpacts_len, s);
783     ofpbuf_uninit(&ofpacts);
784 }
785
786 static void
787 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
788 {
789     ds_put_format(string, "%u", sec);
790     if (nsec > 0) {
791         ds_put_format(string, ".%09u", nsec);
792         while (string->string[string->length - 1] == '0') {
793             string->length--;
794         }
795     }
796     ds_put_char(string, 's');
797 }
798
799 static const char *
800 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason)
801 {
802     static char s[32];
803
804     switch (reason) {
805     case OFPRR_IDLE_TIMEOUT:
806         return "idle";
807     case OFPRR_HARD_TIMEOUT:
808         return "hard";
809     case OFPRR_DELETE:
810         return "delete";
811     case OFPRR_GROUP_DELETE:
812         return "group_delete";
813     default:
814         sprintf(s, "%d", (int) reason);
815         return s;
816     }
817 }
818
819 static void
820 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
821 {
822     struct ofputil_flow_removed fr;
823     enum ofperr error;
824
825     error = ofputil_decode_flow_removed(&fr, oh);
826     if (error) {
827         ofp_print_error(string, error);
828         return;
829     }
830
831     ds_put_char(string, ' ');
832     cls_rule_format(&fr.rule, string);
833
834     ds_put_format(string, " reason=%s",
835                   ofp_flow_removed_reason_to_string(fr.reason));
836
837     if (fr.cookie != htonll(0)) {
838         ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
839     }
840     ds_put_cstr(string, " duration");
841     ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
842     ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
843          fr.idle_timeout, fr.packet_count, fr.byte_count);
844 }
845
846 static void
847 ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
848 {
849     struct ofputil_port_mod pm;
850     enum ofperr error;
851
852     error = ofputil_decode_port_mod(oh, &pm);
853     if (error) {
854         ofp_print_error(string, error);
855         return;
856     }
857
858     ds_put_format(string, "port: %"PRIu16": addr:"ETH_ADDR_FMT"\n",
859                   pm.port_no, ETH_ADDR_ARGS(pm.hw_addr));
860
861     ds_put_format(string, "     config: ");
862     ofp_print_port_config(string, pm.config);
863
864     ds_put_format(string, "     mask:   ");
865     ofp_print_port_config(string, pm.mask);
866
867     ds_put_format(string, "     advertise: ");
868     if (pm.advertise) {
869         ofp_print_port_features(string, pm.advertise);
870     } else {
871         ds_put_format(string, "UNCHANGED\n");
872     }
873 }
874
875 static void
876 ofp_print_error(struct ds *string, enum ofperr error)
877 {
878     if (string->length) {
879         ds_put_char(string, ' ');
880     }
881     ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
882 }
883
884 static void
885 ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
886 {
887     size_t len = ntohs(oem->header.length);
888     size_t payload_ofs, payload_len;
889     const void *payload;
890     enum ofperr error;
891     char *s;
892
893     error = ofperr_decode_msg(&oem->header, &payload_ofs);
894     if (!error) {
895         ds_put_cstr(string, "***decode error***");
896         ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
897         return;
898     }
899
900     ds_put_format(string, " %s\n", ofperr_get_name(error));
901
902     payload = (const uint8_t *) oem + payload_ofs;
903     payload_len = len - payload_ofs;
904     if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
905         ds_put_printable(string, payload, payload_len);
906     } else {
907         s = ofp_to_string(payload, payload_len, 1);
908         ds_put_cstr(string, s);
909         free(s);
910     }
911 }
912
913 static void
914 ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
915 {
916     struct ofputil_port_status ps;
917     enum ofperr error;
918
919     error = ofputil_decode_port_status(ops, &ps);
920     if (error) {
921         ofp_print_error(string, error);
922         return;
923     }
924
925     if (ps.reason == OFPPR_ADD) {
926         ds_put_format(string, " ADD:");
927     } else if (ps.reason == OFPPR_DELETE) {
928         ds_put_format(string, " DEL:");
929     } else if (ps.reason == OFPPR_MODIFY) {
930         ds_put_format(string, " MOD:");
931     }
932
933     ofp_print_phy_port(string, &ps.desc);
934 }
935
936 static void
937 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_desc_stats *ods)
938 {
939     ds_put_char(string, '\n');
940     ds_put_format(string, "Manufacturer: %.*s\n",
941             (int) sizeof ods->mfr_desc, ods->mfr_desc);
942     ds_put_format(string, "Hardware: %.*s\n",
943             (int) sizeof ods->hw_desc, ods->hw_desc);
944     ds_put_format(string, "Software: %.*s\n",
945             (int) sizeof ods->sw_desc, ods->sw_desc);
946     ds_put_format(string, "Serial Num: %.*s\n",
947             (int) sizeof ods->serial_num, ods->serial_num);
948     ds_put_format(string, "DP Description: %.*s\n",
949             (int) sizeof ods->dp_desc, ods->dp_desc);
950 }
951
952 static void
953 ofp_print_flow_stats_request(struct ds *string,
954                              const struct ofp_stats_msg *osm)
955 {
956     struct ofputil_flow_stats_request fsr;
957     enum ofperr error;
958
959     error = ofputil_decode_flow_stats_request(&fsr, &osm->header);
960     if (error) {
961         ofp_print_error(string, error);
962         return;
963     }
964
965     if (fsr.table_id != 0xff) {
966         ds_put_format(string, " table=%"PRIu8, fsr.table_id);
967     }
968
969     if (fsr.out_port != OFPP_NONE) {
970         ds_put_cstr(string, " out_port=");
971         ofputil_format_port(fsr.out_port, string);
972     }
973
974     /* A flow stats request doesn't include a priority, but cls_rule_format()
975      * will print one unless it is OFP_DEFAULT_PRIORITY. */
976     fsr.match.priority = OFP_DEFAULT_PRIORITY;
977
978     ds_put_char(string, ' ');
979     cls_rule_format(&fsr.match, string);
980 }
981
982 void
983 ofp_print_flow_stats(struct ds *string, struct ofputil_flow_stats *fs)
984 {
985     ds_put_format(string, " cookie=0x%"PRIx64", duration=",
986                   ntohll(fs->cookie));
987
988     ofp_print_duration(string, fs->duration_sec, fs->duration_nsec);
989     ds_put_format(string, ", table=%"PRIu8", ", fs->table_id);
990     ds_put_format(string, "n_packets=%"PRIu64", ", fs->packet_count);
991     ds_put_format(string, "n_bytes=%"PRIu64", ", fs->byte_count);
992     if (fs->idle_timeout != OFP_FLOW_PERMANENT) {
993         ds_put_format(string, "idle_timeout=%"PRIu16", ", fs->idle_timeout);
994     }
995     if (fs->hard_timeout != OFP_FLOW_PERMANENT) {
996         ds_put_format(string, "hard_timeout=%"PRIu16", ", fs->hard_timeout);
997     }
998     if (fs->idle_age >= 0) {
999         ds_put_format(string, "idle_age=%d, ", fs->idle_age);
1000     }
1001     if (fs->hard_age >= 0 && fs->hard_age != fs->duration_sec) {
1002         ds_put_format(string, "hard_age=%d, ", fs->hard_age);
1003     }
1004
1005     cls_rule_format(&fs->rule, string);
1006     if (string->string[string->length - 1] != ' ') {
1007         ds_put_char(string, ' ');
1008     }
1009
1010     ofpacts_format(fs->ofpacts, fs->ofpacts_len, string);
1011 }
1012
1013 static void
1014 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1015 {
1016     struct ofpbuf ofpacts;
1017     struct ofpbuf b;
1018
1019     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1020     ofpbuf_init(&ofpacts, 64);
1021     for (;;) {
1022         struct ofputil_flow_stats fs;
1023         int retval;
1024
1025         retval = ofputil_decode_flow_stats_reply(&fs, &b, true, &ofpacts);
1026         if (retval) {
1027             if (retval != EOF) {
1028                 ds_put_cstr(string, " ***parse error***");
1029             }
1030             break;
1031         }
1032         ds_put_char(string, '\n');
1033         ofp_print_flow_stats(string, &fs);
1034      }
1035 }
1036
1037 static void
1038 ofp_print_ofpst_aggregate_reply(struct ds *string,
1039                                 const struct ofp_aggregate_stats_reply *asr)
1040 {
1041     ds_put_format(string, " packet_count=%"PRIu64,
1042                   ntohll(get_32aligned_be64(&asr->packet_count)));
1043     ds_put_format(string, " byte_count=%"PRIu64,
1044                   ntohll(get_32aligned_be64(&asr->byte_count)));
1045     ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1046 }
1047
1048 static void
1049 ofp_print_nxst_aggregate_reply(struct ds *string,
1050                                const struct nx_aggregate_stats_reply *nasr)
1051 {
1052     ds_put_format(string, " packet_count=%"PRIu64, ntohll(nasr->packet_count));
1053     ds_put_format(string, " byte_count=%"PRIu64, ntohll(nasr->byte_count));
1054     ds_put_format(string, " flow_count=%"PRIu32, ntohl(nasr->flow_count));
1055 }
1056
1057 static void print_port_stat(struct ds *string, const char *leader,
1058                             const ovs_32aligned_be64 *statp, int more)
1059 {
1060     uint64_t stat = ntohll(get_32aligned_be64(statp));
1061
1062     ds_put_cstr(string, leader);
1063     if (stat != UINT64_MAX) {
1064         ds_put_format(string, "%"PRIu64, stat);
1065     } else {
1066         ds_put_char(string, '?');
1067     }
1068     if (more) {
1069         ds_put_cstr(string, ", ");
1070     } else {
1071         ds_put_cstr(string, "\n");
1072     }
1073 }
1074
1075 static void
1076 ofp_print_ofpst_port_request(struct ds *string,
1077                              const struct ofp_port_stats_request *psr)
1078 {
1079     ds_put_format(string, " port_no=%"PRIu16, ntohs(psr->port_no));
1080 }
1081
1082 static void
1083 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1084                            int verbosity)
1085 {
1086     const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1087     size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
1088     ds_put_format(string, " %zu ports\n", n);
1089     if (verbosity < 1) {
1090         return;
1091     }
1092
1093     for (; n--; ps++) {
1094         ds_put_format(string, "  port %2"PRIu16": ", ntohs(ps->port_no));
1095
1096         ds_put_cstr(string, "rx ");
1097         print_port_stat(string, "pkts=", &ps->rx_packets, 1);
1098         print_port_stat(string, "bytes=", &ps->rx_bytes, 1);
1099         print_port_stat(string, "drop=", &ps->rx_dropped, 1);
1100         print_port_stat(string, "errs=", &ps->rx_errors, 1);
1101         print_port_stat(string, "frame=", &ps->rx_frame_err, 1);
1102         print_port_stat(string, "over=", &ps->rx_over_err, 1);
1103         print_port_stat(string, "crc=", &ps->rx_crc_err, 0);
1104
1105         ds_put_cstr(string, "           tx ");
1106         print_port_stat(string, "pkts=", &ps->tx_packets, 1);
1107         print_port_stat(string, "bytes=", &ps->tx_bytes, 1);
1108         print_port_stat(string, "drop=", &ps->tx_dropped, 1);
1109         print_port_stat(string, "errs=", &ps->tx_errors, 1);
1110         print_port_stat(string, "coll=", &ps->collisions, 0);
1111     }
1112 }
1113
1114 static void
1115 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1116                             int verbosity)
1117 {
1118     const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1119     size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
1120     ds_put_format(string, " %zu tables\n", n);
1121     if (verbosity < 1) {
1122         return;
1123     }
1124
1125     for (; n--; ts++) {
1126         char name[OFP_MAX_TABLE_NAME_LEN + 1];
1127         ovs_strlcpy(name, ts->name, sizeof name);
1128
1129         ds_put_format(string, "  %d: %-8s: ", ts->table_id, name);
1130         ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1131         ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1132         ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1133         ds_put_cstr(string, "               ");
1134         ds_put_format(string, "lookup=%"PRIu64", ",
1135                       ntohll(get_32aligned_be64(&ts->lookup_count)));
1136         ds_put_format(string, "matched=%"PRIu64"\n",
1137                       ntohll(get_32aligned_be64(&ts->matched_count)));
1138      }
1139 }
1140
1141 static void
1142 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1143 {
1144     if (queue_id == OFPQ_ALL) {
1145         ds_put_cstr(string, "ALL");
1146     } else {
1147         ds_put_format(string, "%"PRIu32, queue_id);
1148     }
1149 }
1150
1151 static void
1152 ofp_print_ofpst_queue_request(struct ds *string,
1153                               const struct ofp_queue_stats_request *qsr)
1154 {
1155     ds_put_cstr(string, "port=");
1156     ofputil_format_port(ntohs(qsr->port_no), string);
1157
1158     ds_put_cstr(string, " queue=");
1159     ofp_print_queue_name(string, ntohl(qsr->queue_id));
1160 }
1161
1162 static void
1163 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1164                             int verbosity)
1165 {
1166     const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1167     size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
1168     ds_put_format(string, " %zu queues\n", n);
1169     if (verbosity < 1) {
1170         return;
1171     }
1172
1173     for (; n--; qs++) {
1174         ds_put_cstr(string, "  port ");
1175         ofputil_format_port(ntohs(qs->port_no), string);
1176         ds_put_cstr(string, " queue ");
1177         ofp_print_queue_name(string, ntohl(qs->queue_id));
1178         ds_put_cstr(string, ": ");
1179
1180         print_port_stat(string, "bytes=", &qs->tx_bytes, 1);
1181         print_port_stat(string, "pkts=", &qs->tx_packets, 1);
1182         print_port_stat(string, "errors=", &qs->tx_errors, 0);
1183     }
1184 }
1185
1186 static void
1187 ofp_print_ofpst_port_desc_reply(struct ds *string,
1188                                 const struct ofp_header *oh)
1189 {
1190     struct ofpbuf b;
1191
1192     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1193     ofpbuf_pull(&b, sizeof(struct ofp_stats_msg));
1194     ds_put_char(string, '\n');
1195     ofp_print_phy_ports(string, oh->version, &b);
1196 }
1197
1198 static void
1199 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1200 {
1201     const struct ofp_stats_msg *srq = (const struct ofp_stats_msg *) oh;
1202
1203     if (srq->flags) {
1204         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1205                       ntohs(srq->flags));
1206     }
1207 }
1208
1209 static void
1210 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1211 {
1212     const struct ofp_stats_msg *srp = (const struct ofp_stats_msg *) oh;
1213
1214     if (srp->flags) {
1215         uint16_t flags = ntohs(srp->flags);
1216
1217         ds_put_cstr(string, " flags=");
1218         if (flags & OFPSF_REPLY_MORE) {
1219             ds_put_cstr(string, "[more]");
1220             flags &= ~OFPSF_REPLY_MORE;
1221         }
1222         if (flags) {
1223             ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1224                           flags);
1225         }
1226     }
1227 }
1228
1229 static void
1230 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1231 {
1232     size_t len = ntohs(oh->length);
1233
1234     ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1235     if (verbosity > 1) {
1236         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1237     }
1238 }
1239
1240 static void
1241 ofp_print_nxt_role_message(struct ds *string,
1242                            const struct nx_role_request *nrr)
1243 {
1244     unsigned int role = ntohl(nrr->role);
1245
1246     ds_put_cstr(string, " role=");
1247     if (role == NX_ROLE_OTHER) {
1248         ds_put_cstr(string, "other");
1249     } else if (role == NX_ROLE_MASTER) {
1250         ds_put_cstr(string, "master");
1251     } else if (role == NX_ROLE_SLAVE) {
1252         ds_put_cstr(string, "slave");
1253     } else {
1254         ds_put_format(string, "%u", role);
1255     }
1256 }
1257
1258 static void
1259 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1260                                 const struct nx_flow_mod_table_id *nfmti)
1261 {
1262     ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1263 }
1264
1265 static void
1266 ofp_print_nxt_set_flow_format(struct ds *string,
1267                               const struct nx_set_flow_format *nsff)
1268 {
1269     uint32_t format = ntohl(nsff->format);
1270
1271     ds_put_cstr(string, " format=");
1272     if (ofputil_nx_flow_format_is_valid(format)) {
1273         ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
1274     } else {
1275         ds_put_format(string, "%"PRIu32, format);
1276     }
1277 }
1278
1279 static void
1280 ofp_print_nxt_set_packet_in_format(struct ds *string,
1281                                    const struct nx_set_packet_in_format *nspf)
1282 {
1283     uint32_t format = ntohl(nspf->format);
1284
1285     ds_put_cstr(string, " format=");
1286     if (ofputil_packet_in_format_is_valid(format)) {
1287         ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1288     } else {
1289         ds_put_format(string, "%"PRIu32, format);
1290     }
1291 }
1292
1293 static const char *
1294 ofp_port_reason_to_string(enum ofp_port_reason reason)
1295 {
1296     static char s[32];
1297
1298     switch (reason) {
1299     case OFPPR_ADD:
1300         return "add";
1301
1302     case OFPPR_DELETE:
1303         return "delete";
1304
1305     case OFPPR_MODIFY:
1306         return "modify";
1307
1308     default:
1309         sprintf(s, "%d", (int) reason);
1310         return s;
1311     }
1312 }
1313
1314 static void
1315 ofp_print_nxt_set_async_config(struct ds *string,
1316                                const struct nx_async_config *nac)
1317 {
1318     int i;
1319
1320     for (i = 0; i < 2; i++) {
1321         int j;
1322
1323         ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1324
1325         ds_put_cstr(string, "       PACKET_IN:");
1326         for (j = 0; j < 32; j++) {
1327             if (nac->packet_in_mask[i] & htonl(1u << j)) {
1328                 ds_put_format(string, " %s",
1329                               ofputil_packet_in_reason_to_string(j));
1330             }
1331         }
1332         if (!nac->packet_in_mask[i]) {
1333             ds_put_cstr(string, " (off)");
1334         }
1335         ds_put_char(string, '\n');
1336
1337         ds_put_cstr(string, "     PORT_STATUS:");
1338         for (j = 0; j < 32; j++) {
1339             if (nac->port_status_mask[i] & htonl(1u << j)) {
1340                 ds_put_format(string, " %s", ofp_port_reason_to_string(j));
1341             }
1342         }
1343         if (!nac->port_status_mask[i]) {
1344             ds_put_cstr(string, " (off)");
1345         }
1346         ds_put_char(string, '\n');
1347
1348         ds_put_cstr(string, "    FLOW_REMOVED:");
1349         for (j = 0; j < 32; j++) {
1350             if (nac->flow_removed_mask[i] & htonl(1u << j)) {
1351                 ds_put_format(string, " %s",
1352                               ofp_flow_removed_reason_to_string(j));
1353             }
1354         }
1355         if (!nac->flow_removed_mask[i]) {
1356             ds_put_cstr(string, " (off)");
1357         }
1358         ds_put_char(string, '\n');
1359     }
1360 }
1361
1362 static void
1363 ofp_print_nxt_set_controller_id(struct ds *string,
1364                                 const struct nx_controller_id *nci)
1365 {
1366     ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
1367 }
1368
1369 void
1370 ofp_print_version(const struct ofp_header *oh,
1371                   struct ds *string)
1372 {
1373     switch (oh->version) {
1374     case OFP10_VERSION:
1375         break;
1376     case OFP11_VERSION:
1377         ds_put_cstr(string, " (OF1.1)");
1378         break;
1379     case OFP12_VERSION:
1380         ds_put_cstr(string, " (OF1.2)");
1381         break;
1382     default:
1383         ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
1384         break;
1385     }
1386     ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
1387 }
1388
1389 static void
1390 ofp_to_string__(const struct ofp_header *oh,
1391                 const struct ofputil_msg_type *type, struct ds *string,
1392                 int verbosity)
1393 {
1394     enum ofputil_msg_code code;
1395     const void *msg = oh;
1396
1397     ds_put_cstr(string, ofputil_msg_type_name(type));
1398     ofp_print_version(oh, string);
1399     code = ofputil_msg_type_code(type);
1400     switch (code) {
1401     case OFPUTIL_MSG_INVALID:
1402         break;
1403
1404     case OFPUTIL_OFPT_HELLO:
1405         ds_put_char(string, '\n');
1406         ds_put_hex_dump(string, oh + 1, ntohs(oh->length) - sizeof *oh,
1407                         0, true);
1408         break;
1409
1410     case OFPUTIL_OFPT_ERROR:
1411         ofp_print_error_msg(string, msg);
1412         break;
1413
1414     case OFPUTIL_OFPT_ECHO_REQUEST:
1415     case OFPUTIL_OFPT_ECHO_REPLY:
1416         ofp_print_echo(string, oh, verbosity);
1417         break;
1418
1419     case OFPUTIL_OFPT_FEATURES_REQUEST:
1420         break;
1421
1422     case OFPUTIL_OFPT_FEATURES_REPLY:
1423         ofp_print_switch_features(string, msg);
1424         break;
1425
1426     case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1427         break;
1428
1429     case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1430     case OFPUTIL_OFPT_SET_CONFIG:
1431         ofp_print_switch_config(string, msg);
1432         break;
1433
1434     case OFPUTIL_OFPT_PACKET_IN:
1435     case OFPUTIL_NXT_PACKET_IN:
1436         ofp_print_packet_in(string, msg, verbosity);
1437         break;
1438
1439     case OFPUTIL_OFPT_FLOW_REMOVED:
1440     case OFPUTIL_NXT_FLOW_REMOVED:
1441         ofp_print_flow_removed(string, msg);
1442         break;
1443
1444     case OFPUTIL_OFPT_PORT_STATUS:
1445         ofp_print_port_status(string, msg);
1446         break;
1447
1448     case OFPUTIL_OFPT_PACKET_OUT:
1449         ofp_print_packet_out(string, msg, verbosity);
1450         break;
1451
1452     case OFPUTIL_OFPT_FLOW_MOD:
1453     case OFPUTIL_NXT_FLOW_MOD:
1454         ofp_print_flow_mod(string, msg, code, verbosity);
1455         break;
1456
1457     case OFPUTIL_OFPT_PORT_MOD:
1458         ofp_print_port_mod(string, msg);
1459         break;
1460
1461     case OFPUTIL_OFPT_BARRIER_REQUEST:
1462     case OFPUTIL_OFPT_BARRIER_REPLY:
1463         break;
1464
1465     case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1466     case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1467         /* XXX */
1468         break;
1469
1470     case OFPUTIL_OFPST_DESC_REQUEST:
1471     case OFPUTIL_OFPST_PORT_DESC_REQUEST:
1472         ofp_print_stats_request(string, oh);
1473         break;
1474
1475     case OFPUTIL_OFPST_FLOW_REQUEST:
1476     case OFPUTIL_NXST_FLOW_REQUEST:
1477     case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1478     case OFPUTIL_NXST_AGGREGATE_REQUEST:
1479         ofp_print_stats_request(string, oh);
1480         ofp_print_flow_stats_request(string, msg);
1481         break;
1482
1483     case OFPUTIL_OFPST_TABLE_REQUEST:
1484         ofp_print_stats_request(string, oh);
1485         break;
1486
1487     case OFPUTIL_OFPST_PORT_REQUEST:
1488         ofp_print_stats_request(string, oh);
1489         ofp_print_ofpst_port_request(string, msg);
1490         break;
1491
1492     case OFPUTIL_OFPST_QUEUE_REQUEST:
1493         ofp_print_stats_request(string, oh);
1494         ofp_print_ofpst_queue_request(string, msg);
1495         break;
1496
1497     case OFPUTIL_OFPST_DESC_REPLY:
1498         ofp_print_stats_reply(string, oh);
1499         ofp_print_ofpst_desc_reply(string, msg);
1500         break;
1501
1502     case OFPUTIL_OFPST_FLOW_REPLY:
1503     case OFPUTIL_NXST_FLOW_REPLY:
1504         ofp_print_stats_reply(string, oh);
1505         ofp_print_flow_stats_reply(string, oh);
1506         break;
1507
1508     case OFPUTIL_OFPST_QUEUE_REPLY:
1509         ofp_print_stats_reply(string, oh);
1510         ofp_print_ofpst_queue_reply(string, oh, verbosity);
1511         break;
1512
1513     case OFPUTIL_OFPST_PORT_REPLY:
1514         ofp_print_stats_reply(string, oh);
1515         ofp_print_ofpst_port_reply(string, oh, verbosity);
1516         break;
1517
1518     case OFPUTIL_OFPST_TABLE_REPLY:
1519         ofp_print_stats_reply(string, oh);
1520         ofp_print_ofpst_table_reply(string, oh, verbosity);
1521         break;
1522
1523     case OFPUTIL_OFPST_AGGREGATE_REPLY:
1524         ofp_print_stats_reply(string, oh);
1525         ofp_print_ofpst_aggregate_reply(string, msg);
1526         break;
1527
1528     case OFPUTIL_OFPST_PORT_DESC_REPLY:
1529         ofp_print_stats_reply(string, oh);
1530         ofp_print_ofpst_port_desc_reply(string, oh);
1531         break;
1532
1533     case OFPUTIL_NXT_ROLE_REQUEST:
1534     case OFPUTIL_NXT_ROLE_REPLY:
1535         ofp_print_nxt_role_message(string, msg);
1536         break;
1537
1538     case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
1539         ofp_print_nxt_flow_mod_table_id(string, msg);
1540         break;
1541
1542     case OFPUTIL_NXT_SET_FLOW_FORMAT:
1543         ofp_print_nxt_set_flow_format(string, msg);
1544         break;
1545
1546     case OFPUTIL_NXT_SET_PACKET_IN_FORMAT:
1547         ofp_print_nxt_set_packet_in_format(string, msg);
1548         break;
1549
1550     case OFPUTIL_NXT_FLOW_AGE:
1551         break;
1552
1553     case OFPUTIL_NXT_SET_CONTROLLER_ID:
1554         ofp_print_nxt_set_controller_id(string, msg);
1555         break;
1556
1557     case OFPUTIL_NXT_SET_ASYNC_CONFIG:
1558         ofp_print_nxt_set_async_config(string, msg);
1559         break;
1560
1561     case OFPUTIL_NXST_AGGREGATE_REPLY:
1562         ofp_print_stats_reply(string, oh);
1563         ofp_print_nxst_aggregate_reply(string, msg);
1564         break;
1565     }
1566 }
1567
1568 /* Composes and returns a string representing the OpenFlow packet of 'len'
1569  * bytes at 'oh' at the given 'verbosity' level.  0 is a minimal amount of
1570  * verbosity and higher numbers increase verbosity.  The caller is responsible
1571  * for freeing the string. */
1572 char *
1573 ofp_to_string(const void *oh_, size_t len, int verbosity)
1574 {
1575     struct ds string = DS_EMPTY_INITIALIZER;
1576     const struct ofp_header *oh = oh_;
1577
1578     if (!len) {
1579         ds_put_cstr(&string, "OpenFlow message is empty\n");
1580     } else if (len < sizeof(struct ofp_header)) {
1581         ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
1582                       len);
1583     } else if (ntohs(oh->length) > len) {
1584         ds_put_format(&string,
1585                       "(***truncated to %zu bytes from %"PRIu16"***)\n",
1586                       len, ntohs(oh->length));
1587     } else if (ntohs(oh->length) < len) {
1588         ds_put_format(&string,
1589                       "(***only uses %"PRIu16" bytes out of %zu***)\n",
1590                       ntohs(oh->length), len);
1591     } else {
1592         const struct ofputil_msg_type *type;
1593         enum ofperr error;
1594
1595         error = ofputil_decode_msg_type(oh, &type);
1596         if (!error) {
1597             ofp_to_string__(oh, type, &string, verbosity);
1598             if (verbosity >= 5) {
1599                 if (ds_last(&string) != '\n') {
1600                     ds_put_char(&string, '\n');
1601                 }
1602                 ds_put_hex_dump(&string, oh, len, 0, true);
1603             }
1604             if (ds_last(&string) != '\n') {
1605                 ds_put_char(&string, '\n');
1606             }
1607             return ds_steal_cstr(&string);
1608         }
1609
1610         ofp_print_error(&string, error);
1611     }
1612     ds_put_hex_dump(&string, oh, len, 0, true);
1613     return ds_steal_cstr(&string);
1614 }
1615 \f
1616 static void
1617 print_and_free(FILE *stream, char *string)
1618 {
1619     fputs(string, stream);
1620     free(string);
1621 }
1622
1623 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1624  * given 'verbosity' level.  0 is a minimal amount of verbosity and higher
1625  * numbers increase verbosity. */
1626 void
1627 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1628 {
1629     print_and_free(stream, ofp_to_string(oh, len, verbosity));
1630 }
1631
1632 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1633  * 'data' to 'stream'. */
1634 void
1635 ofp_print_packet(FILE *stream, const void *data, size_t len)
1636 {
1637     print_and_free(stream, ofp_packet_to_string(data, len));
1638 }