Introduce ofpacts, an abstraction of OpenFlow actions.
[cascardo/ovs.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 static void
976 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
977 {
978     struct ofpbuf ofpacts;
979     struct ofpbuf b;
980
981     ofpbuf_use_const(&b, oh, ntohs(oh->length));
982     ofpbuf_init(&ofpacts, 64);
983     for (;;) {
984         struct ofputil_flow_stats fs;
985         int retval;
986
987         retval = ofputil_decode_flow_stats_reply(&fs, &b, true, &ofpacts);
988         if (retval) {
989             if (retval != EOF) {
990                 ds_put_cstr(string, " ***parse error***");
991             }
992             break;
993         }
994
995         ds_put_char(string, '\n');
996
997         ds_put_format(string, " cookie=0x%"PRIx64", duration=",
998                       ntohll(fs.cookie));
999         ofp_print_duration(string, fs.duration_sec, fs.duration_nsec);
1000         ds_put_format(string, ", table=%"PRIu8", ", fs.table_id);
1001         ds_put_format(string, "n_packets=%"PRIu64", ", fs.packet_count);
1002         ds_put_format(string, "n_bytes=%"PRIu64", ", fs.byte_count);
1003         if (fs.idle_timeout != OFP_FLOW_PERMANENT) {
1004             ds_put_format(string, "idle_timeout=%"PRIu16", ", fs.idle_timeout);
1005         }
1006         if (fs.hard_timeout != OFP_FLOW_PERMANENT) {
1007             ds_put_format(string, "hard_timeout=%"PRIu16", ", fs.hard_timeout);
1008         }
1009         if (fs.idle_age >= 0) {
1010             ds_put_format(string, "idle_age=%d, ", fs.idle_age);
1011         }
1012         if (fs.hard_age >= 0 && fs.hard_age != fs.duration_sec) {
1013             ds_put_format(string, "hard_age=%d, ", fs.hard_age);
1014         }
1015
1016         cls_rule_format(&fs.rule, string);
1017         if (string->string[string->length - 1] != ' ') {
1018             ds_put_char(string, ' ');
1019         }
1020         ofpacts_format(fs.ofpacts, fs.ofpacts_len, string);
1021     }
1022     ofpbuf_uninit(&ofpacts);
1023 }
1024
1025 static void
1026 ofp_print_ofpst_aggregate_reply(struct ds *string,
1027                                 const struct ofp_aggregate_stats_reply *asr)
1028 {
1029     ds_put_format(string, " packet_count=%"PRIu64,
1030                   ntohll(get_32aligned_be64(&asr->packet_count)));
1031     ds_put_format(string, " byte_count=%"PRIu64,
1032                   ntohll(get_32aligned_be64(&asr->byte_count)));
1033     ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1034 }
1035
1036 static void
1037 ofp_print_nxst_aggregate_reply(struct ds *string,
1038                                const struct nx_aggregate_stats_reply *nasr)
1039 {
1040     ds_put_format(string, " packet_count=%"PRIu64, ntohll(nasr->packet_count));
1041     ds_put_format(string, " byte_count=%"PRIu64, ntohll(nasr->byte_count));
1042     ds_put_format(string, " flow_count=%"PRIu32, ntohl(nasr->flow_count));
1043 }
1044
1045 static void print_port_stat(struct ds *string, const char *leader,
1046                             const ovs_32aligned_be64 *statp, int more)
1047 {
1048     uint64_t stat = ntohll(get_32aligned_be64(statp));
1049
1050     ds_put_cstr(string, leader);
1051     if (stat != UINT64_MAX) {
1052         ds_put_format(string, "%"PRIu64, stat);
1053     } else {
1054         ds_put_char(string, '?');
1055     }
1056     if (more) {
1057         ds_put_cstr(string, ", ");
1058     } else {
1059         ds_put_cstr(string, "\n");
1060     }
1061 }
1062
1063 static void
1064 ofp_print_ofpst_port_request(struct ds *string,
1065                              const struct ofp_port_stats_request *psr)
1066 {
1067     ds_put_format(string, " port_no=%"PRIu16, ntohs(psr->port_no));
1068 }
1069
1070 static void
1071 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1072                            int verbosity)
1073 {
1074     const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1075     size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
1076     ds_put_format(string, " %zu ports\n", n);
1077     if (verbosity < 1) {
1078         return;
1079     }
1080
1081     for (; n--; ps++) {
1082         ds_put_format(string, "  port %2"PRIu16": ", ntohs(ps->port_no));
1083
1084         ds_put_cstr(string, "rx ");
1085         print_port_stat(string, "pkts=", &ps->rx_packets, 1);
1086         print_port_stat(string, "bytes=", &ps->rx_bytes, 1);
1087         print_port_stat(string, "drop=", &ps->rx_dropped, 1);
1088         print_port_stat(string, "errs=", &ps->rx_errors, 1);
1089         print_port_stat(string, "frame=", &ps->rx_frame_err, 1);
1090         print_port_stat(string, "over=", &ps->rx_over_err, 1);
1091         print_port_stat(string, "crc=", &ps->rx_crc_err, 0);
1092
1093         ds_put_cstr(string, "           tx ");
1094         print_port_stat(string, "pkts=", &ps->tx_packets, 1);
1095         print_port_stat(string, "bytes=", &ps->tx_bytes, 1);
1096         print_port_stat(string, "drop=", &ps->tx_dropped, 1);
1097         print_port_stat(string, "errs=", &ps->tx_errors, 1);
1098         print_port_stat(string, "coll=", &ps->collisions, 0);
1099     }
1100 }
1101
1102 static void
1103 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1104                             int verbosity)
1105 {
1106     const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1107     size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
1108     ds_put_format(string, " %zu tables\n", n);
1109     if (verbosity < 1) {
1110         return;
1111     }
1112
1113     for (; n--; ts++) {
1114         char name[OFP_MAX_TABLE_NAME_LEN + 1];
1115         ovs_strlcpy(name, ts->name, sizeof name);
1116
1117         ds_put_format(string, "  %d: %-8s: ", ts->table_id, name);
1118         ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1119         ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1120         ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1121         ds_put_cstr(string, "               ");
1122         ds_put_format(string, "lookup=%"PRIu64", ",
1123                       ntohll(get_32aligned_be64(&ts->lookup_count)));
1124         ds_put_format(string, "matched=%"PRIu64"\n",
1125                       ntohll(get_32aligned_be64(&ts->matched_count)));
1126      }
1127 }
1128
1129 static void
1130 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1131 {
1132     if (queue_id == OFPQ_ALL) {
1133         ds_put_cstr(string, "ALL");
1134     } else {
1135         ds_put_format(string, "%"PRIu32, queue_id);
1136     }
1137 }
1138
1139 static void
1140 ofp_print_ofpst_queue_request(struct ds *string,
1141                               const struct ofp_queue_stats_request *qsr)
1142 {
1143     ds_put_cstr(string, "port=");
1144     ofputil_format_port(ntohs(qsr->port_no), string);
1145
1146     ds_put_cstr(string, " queue=");
1147     ofp_print_queue_name(string, ntohl(qsr->queue_id));
1148 }
1149
1150 static void
1151 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1152                             int verbosity)
1153 {
1154     const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1155     size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
1156     ds_put_format(string, " %zu queues\n", n);
1157     if (verbosity < 1) {
1158         return;
1159     }
1160
1161     for (; n--; qs++) {
1162         ds_put_cstr(string, "  port ");
1163         ofputil_format_port(ntohs(qs->port_no), string);
1164         ds_put_cstr(string, " queue ");
1165         ofp_print_queue_name(string, ntohl(qs->queue_id));
1166         ds_put_cstr(string, ": ");
1167
1168         print_port_stat(string, "bytes=", &qs->tx_bytes, 1);
1169         print_port_stat(string, "pkts=", &qs->tx_packets, 1);
1170         print_port_stat(string, "errors=", &qs->tx_errors, 0);
1171     }
1172 }
1173
1174 static void
1175 ofp_print_ofpst_port_desc_reply(struct ds *string,
1176                                 const struct ofp_header *oh)
1177 {
1178     struct ofpbuf b;
1179
1180     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1181     ofpbuf_pull(&b, sizeof(struct ofp_stats_msg));
1182     ds_put_char(string, '\n');
1183     ofp_print_phy_ports(string, oh->version, &b);
1184 }
1185
1186 static void
1187 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1188 {
1189     const struct ofp_stats_msg *srq = (const struct ofp_stats_msg *) oh;
1190
1191     if (srq->flags) {
1192         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1193                       ntohs(srq->flags));
1194     }
1195 }
1196
1197 static void
1198 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1199 {
1200     const struct ofp_stats_msg *srp = (const struct ofp_stats_msg *) oh;
1201
1202     if (srp->flags) {
1203         uint16_t flags = ntohs(srp->flags);
1204
1205         ds_put_cstr(string, " flags=");
1206         if (flags & OFPSF_REPLY_MORE) {
1207             ds_put_cstr(string, "[more]");
1208             flags &= ~OFPSF_REPLY_MORE;
1209         }
1210         if (flags) {
1211             ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1212                           flags);
1213         }
1214     }
1215 }
1216
1217 static void
1218 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1219 {
1220     size_t len = ntohs(oh->length);
1221
1222     ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1223     if (verbosity > 1) {
1224         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1225     }
1226 }
1227
1228 static void
1229 ofp_print_nxt_role_message(struct ds *string,
1230                            const struct nx_role_request *nrr)
1231 {
1232     unsigned int role = ntohl(nrr->role);
1233
1234     ds_put_cstr(string, " role=");
1235     if (role == NX_ROLE_OTHER) {
1236         ds_put_cstr(string, "other");
1237     } else if (role == NX_ROLE_MASTER) {
1238         ds_put_cstr(string, "master");
1239     } else if (role == NX_ROLE_SLAVE) {
1240         ds_put_cstr(string, "slave");
1241     } else {
1242         ds_put_format(string, "%u", role);
1243     }
1244 }
1245
1246 static void
1247 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1248                                 const struct nx_flow_mod_table_id *nfmti)
1249 {
1250     ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1251 }
1252
1253 static void
1254 ofp_print_nxt_set_flow_format(struct ds *string,
1255                               const struct nx_set_flow_format *nsff)
1256 {
1257     uint32_t format = ntohl(nsff->format);
1258
1259     ds_put_cstr(string, " format=");
1260     if (ofputil_nx_flow_format_is_valid(format)) {
1261         ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
1262     } else {
1263         ds_put_format(string, "%"PRIu32, format);
1264     }
1265 }
1266
1267 static void
1268 ofp_print_nxt_set_packet_in_format(struct ds *string,
1269                                    const struct nx_set_packet_in_format *nspf)
1270 {
1271     uint32_t format = ntohl(nspf->format);
1272
1273     ds_put_cstr(string, " format=");
1274     if (ofputil_packet_in_format_is_valid(format)) {
1275         ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1276     } else {
1277         ds_put_format(string, "%"PRIu32, format);
1278     }
1279 }
1280
1281 static const char *
1282 ofp_port_reason_to_string(enum ofp_port_reason reason)
1283 {
1284     static char s[32];
1285
1286     switch (reason) {
1287     case OFPPR_ADD:
1288         return "add";
1289
1290     case OFPPR_DELETE:
1291         return "delete";
1292
1293     case OFPPR_MODIFY:
1294         return "modify";
1295
1296     default:
1297         sprintf(s, "%d", (int) reason);
1298         return s;
1299     }
1300 }
1301
1302 static void
1303 ofp_print_nxt_set_async_config(struct ds *string,
1304                                const struct nx_async_config *nac)
1305 {
1306     int i;
1307
1308     for (i = 0; i < 2; i++) {
1309         int j;
1310
1311         ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1312
1313         ds_put_cstr(string, "       PACKET_IN:");
1314         for (j = 0; j < 32; j++) {
1315             if (nac->packet_in_mask[i] & htonl(1u << j)) {
1316                 ds_put_format(string, " %s",
1317                               ofputil_packet_in_reason_to_string(j));
1318             }
1319         }
1320         if (!nac->packet_in_mask[i]) {
1321             ds_put_cstr(string, " (off)");
1322         }
1323         ds_put_char(string, '\n');
1324
1325         ds_put_cstr(string, "     PORT_STATUS:");
1326         for (j = 0; j < 32; j++) {
1327             if (nac->port_status_mask[i] & htonl(1u << j)) {
1328                 ds_put_format(string, " %s", ofp_port_reason_to_string(j));
1329             }
1330         }
1331         if (!nac->port_status_mask[i]) {
1332             ds_put_cstr(string, " (off)");
1333         }
1334         ds_put_char(string, '\n');
1335
1336         ds_put_cstr(string, "    FLOW_REMOVED:");
1337         for (j = 0; j < 32; j++) {
1338             if (nac->flow_removed_mask[i] & htonl(1u << j)) {
1339                 ds_put_format(string, " %s",
1340                               ofp_flow_removed_reason_to_string(j));
1341             }
1342         }
1343         if (!nac->flow_removed_mask[i]) {
1344             ds_put_cstr(string, " (off)");
1345         }
1346         ds_put_char(string, '\n');
1347     }
1348 }
1349
1350 static void
1351 ofp_print_nxt_set_controller_id(struct ds *string,
1352                                 const struct nx_controller_id *nci)
1353 {
1354     ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
1355 }
1356
1357 static void
1358 ofp_to_string__(const struct ofp_header *oh,
1359                 const struct ofputil_msg_type *type, struct ds *string,
1360                 int verbosity)
1361 {
1362     enum ofputil_msg_code code;
1363     const void *msg = oh;
1364
1365     ds_put_cstr(string, ofputil_msg_type_name(type));
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     code = ofputil_msg_type_code(type);
1382     switch (code) {
1383     case OFPUTIL_MSG_INVALID:
1384         break;
1385
1386     case OFPUTIL_OFPT_HELLO:
1387         ds_put_char(string, '\n');
1388         ds_put_hex_dump(string, oh + 1, ntohs(oh->length) - sizeof *oh,
1389                         0, true);
1390         break;
1391
1392     case OFPUTIL_OFPT_ERROR:
1393         ofp_print_error_msg(string, msg);
1394         break;
1395
1396     case OFPUTIL_OFPT_ECHO_REQUEST:
1397     case OFPUTIL_OFPT_ECHO_REPLY:
1398         ofp_print_echo(string, oh, verbosity);
1399         break;
1400
1401     case OFPUTIL_OFPT_FEATURES_REQUEST:
1402         break;
1403
1404     case OFPUTIL_OFPT_FEATURES_REPLY:
1405         ofp_print_switch_features(string, msg);
1406         break;
1407
1408     case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1409         break;
1410
1411     case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1412     case OFPUTIL_OFPT_SET_CONFIG:
1413         ofp_print_switch_config(string, msg);
1414         break;
1415
1416     case OFPUTIL_OFPT_PACKET_IN:
1417     case OFPUTIL_NXT_PACKET_IN:
1418         ofp_print_packet_in(string, msg, verbosity);
1419         break;
1420
1421     case OFPUTIL_OFPT_FLOW_REMOVED:
1422     case OFPUTIL_NXT_FLOW_REMOVED:
1423         ofp_print_flow_removed(string, msg);
1424         break;
1425
1426     case OFPUTIL_OFPT_PORT_STATUS:
1427         ofp_print_port_status(string, msg);
1428         break;
1429
1430     case OFPUTIL_OFPT_PACKET_OUT:
1431         ofp_print_packet_out(string, msg, verbosity);
1432         break;
1433
1434     case OFPUTIL_OFPT_FLOW_MOD:
1435     case OFPUTIL_NXT_FLOW_MOD:
1436         ofp_print_flow_mod(string, msg, code, verbosity);
1437         break;
1438
1439     case OFPUTIL_OFPT_PORT_MOD:
1440         ofp_print_port_mod(string, msg);
1441         break;
1442
1443     case OFPUTIL_OFPT_BARRIER_REQUEST:
1444     case OFPUTIL_OFPT_BARRIER_REPLY:
1445         break;
1446
1447     case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1448     case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1449         /* XXX */
1450         break;
1451
1452     case OFPUTIL_OFPST_DESC_REQUEST:
1453     case OFPUTIL_OFPST_PORT_DESC_REQUEST:
1454         ofp_print_stats_request(string, oh);
1455         break;
1456
1457     case OFPUTIL_OFPST_FLOW_REQUEST:
1458     case OFPUTIL_NXST_FLOW_REQUEST:
1459     case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1460     case OFPUTIL_NXST_AGGREGATE_REQUEST:
1461         ofp_print_stats_request(string, oh);
1462         ofp_print_flow_stats_request(string, msg);
1463         break;
1464
1465     case OFPUTIL_OFPST_TABLE_REQUEST:
1466         ofp_print_stats_request(string, oh);
1467         break;
1468
1469     case OFPUTIL_OFPST_PORT_REQUEST:
1470         ofp_print_stats_request(string, oh);
1471         ofp_print_ofpst_port_request(string, msg);
1472         break;
1473
1474     case OFPUTIL_OFPST_QUEUE_REQUEST:
1475         ofp_print_stats_request(string, oh);
1476         ofp_print_ofpst_queue_request(string, msg);
1477         break;
1478
1479     case OFPUTIL_OFPST_DESC_REPLY:
1480         ofp_print_stats_reply(string, oh);
1481         ofp_print_ofpst_desc_reply(string, msg);
1482         break;
1483
1484     case OFPUTIL_OFPST_FLOW_REPLY:
1485     case OFPUTIL_NXST_FLOW_REPLY:
1486         ofp_print_stats_reply(string, oh);
1487         ofp_print_flow_stats_reply(string, oh);
1488         break;
1489
1490     case OFPUTIL_OFPST_QUEUE_REPLY:
1491         ofp_print_stats_reply(string, oh);
1492         ofp_print_ofpst_queue_reply(string, oh, verbosity);
1493         break;
1494
1495     case OFPUTIL_OFPST_PORT_REPLY:
1496         ofp_print_stats_reply(string, oh);
1497         ofp_print_ofpst_port_reply(string, oh, verbosity);
1498         break;
1499
1500     case OFPUTIL_OFPST_TABLE_REPLY:
1501         ofp_print_stats_reply(string, oh);
1502         ofp_print_ofpst_table_reply(string, oh, verbosity);
1503         break;
1504
1505     case OFPUTIL_OFPST_AGGREGATE_REPLY:
1506         ofp_print_stats_reply(string, oh);
1507         ofp_print_ofpst_aggregate_reply(string, msg);
1508         break;
1509
1510     case OFPUTIL_OFPST_PORT_DESC_REPLY:
1511         ofp_print_stats_reply(string, oh);
1512         ofp_print_ofpst_port_desc_reply(string, oh);
1513         break;
1514
1515     case OFPUTIL_NXT_ROLE_REQUEST:
1516     case OFPUTIL_NXT_ROLE_REPLY:
1517         ofp_print_nxt_role_message(string, msg);
1518         break;
1519
1520     case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
1521         ofp_print_nxt_flow_mod_table_id(string, msg);
1522         break;
1523
1524     case OFPUTIL_NXT_SET_FLOW_FORMAT:
1525         ofp_print_nxt_set_flow_format(string, msg);
1526         break;
1527
1528     case OFPUTIL_NXT_SET_PACKET_IN_FORMAT:
1529         ofp_print_nxt_set_packet_in_format(string, msg);
1530         break;
1531
1532     case OFPUTIL_NXT_FLOW_AGE:
1533         break;
1534
1535     case OFPUTIL_NXT_SET_CONTROLLER_ID:
1536         ofp_print_nxt_set_controller_id(string, msg);
1537         break;
1538
1539     case OFPUTIL_NXT_SET_ASYNC_CONFIG:
1540         ofp_print_nxt_set_async_config(string, msg);
1541         break;
1542
1543     case OFPUTIL_NXST_AGGREGATE_REPLY:
1544         ofp_print_stats_reply(string, oh);
1545         ofp_print_nxst_aggregate_reply(string, msg);
1546         break;
1547     }
1548 }
1549
1550 /* Composes and returns a string representing the OpenFlow packet of 'len'
1551  * bytes at 'oh' at the given 'verbosity' level.  0 is a minimal amount of
1552  * verbosity and higher numbers increase verbosity.  The caller is responsible
1553  * for freeing the string. */
1554 char *
1555 ofp_to_string(const void *oh_, size_t len, int verbosity)
1556 {
1557     struct ds string = DS_EMPTY_INITIALIZER;
1558     const struct ofp_header *oh = oh_;
1559
1560     if (!len) {
1561         ds_put_cstr(&string, "OpenFlow message is empty\n");
1562     } else if (len < sizeof(struct ofp_header)) {
1563         ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
1564                       len);
1565     } else if (ntohs(oh->length) > len) {
1566         ds_put_format(&string,
1567                       "(***truncated to %zu bytes from %"PRIu16"***)\n",
1568                       len, ntohs(oh->length));
1569     } else if (ntohs(oh->length) < len) {
1570         ds_put_format(&string,
1571                       "(***only uses %"PRIu16" bytes out of %zu***)\n",
1572                       ntohs(oh->length), len);
1573     } else {
1574         const struct ofputil_msg_type *type;
1575         enum ofperr error;
1576
1577         error = ofputil_decode_msg_type(oh, &type);
1578         if (!error) {
1579             ofp_to_string__(oh, type, &string, verbosity);
1580             if (verbosity >= 5) {
1581                 if (ds_last(&string) != '\n') {
1582                     ds_put_char(&string, '\n');
1583                 }
1584                 ds_put_hex_dump(&string, oh, len, 0, true);
1585             }
1586             if (ds_last(&string) != '\n') {
1587                 ds_put_char(&string, '\n');
1588             }
1589             return ds_steal_cstr(&string);
1590         }
1591
1592         ofp_print_error(&string, error);
1593     }
1594     ds_put_hex_dump(&string, oh, len, 0, true);
1595     return ds_steal_cstr(&string);
1596 }
1597 \f
1598 static void
1599 print_and_free(FILE *stream, char *string)
1600 {
1601     fputs(string, stream);
1602     free(string);
1603 }
1604
1605 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1606  * given 'verbosity' level.  0 is a minimal amount of verbosity and higher
1607  * numbers increase verbosity. */
1608 void
1609 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1610 {
1611     print_and_free(stream, ofp_to_string(oh, len, verbosity));
1612 }
1613
1614 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1615  * 'data' to 'stream'. */
1616 void
1617 ofp_print_packet(FILE *stream, const void *data, size_t len)
1618 {
1619     print_and_free(stream, ofp_packet_to_string(data, len));
1620 }