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