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