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