Initial OpenFlow 1.3 support
[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, NULL, 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 != htonll(0)) {
112         ds_put_format(string, " tun_id=0x%"PRIx64, ntohll(pin.fmd.tun_id));
113     }
114
115     if (pin.fmd.metadata != htonll(0)) {
116         ds_put_format(string, " metadata=0x%"PRIx64, ntohll(pin.fmd.metadata));
117     }
118
119     for (i = 0; i < FLOW_N_REGS; i++) {
120         if (pin.fmd.regs[i]) {
121             ds_put_format(string, " reg%d=0x%"PRIx32, i, pin.fmd.regs[i]);
122         }
123     }
124
125     ds_put_format(string, " (via %s)",
126                   ofputil_packet_in_reason_to_string(pin.reason));
127
128     ds_put_format(string, " data_len=%zu", pin.packet_len);
129     if (pin.buffer_id == UINT32_MAX) {
130         ds_put_format(string, " (unbuffered)");
131         if (pin.total_len != pin.packet_len) {
132             ds_put_format(string, " (***total_len != data_len***)");
133         }
134     } else {
135         ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
136         if (pin.total_len < pin.packet_len) {
137             ds_put_format(string, " (***total_len < data_len***)");
138         }
139     }
140     ds_put_char(string, '\n');
141
142     if (verbosity > 0) {
143         char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
144         ds_put_cstr(string, packet);
145         free(packet);
146     }
147 }
148
149 static void
150 ofp_print_packet_out(struct ds *string, const struct ofp_header *oh,
151                      int verbosity)
152 {
153     struct ofputil_packet_out po;
154     struct ofpbuf ofpacts;
155     enum ofperr error;
156
157     ofpbuf_init(&ofpacts, 64);
158     error = ofputil_decode_packet_out(&po, oh, &ofpacts);
159     if (error) {
160         ofpbuf_uninit(&ofpacts);
161         ofp_print_error(string, error);
162         return;
163     }
164
165     ds_put_cstr(string, " in_port=");
166     ofputil_format_port(po.in_port, string);
167
168     ds_put_char(string, ' ');
169     ofpacts_format(po.ofpacts, po.ofpacts_len, string);
170
171     if (po.buffer_id == UINT32_MAX) {
172         ds_put_format(string, " data_len=%zu", po.packet_len);
173         if (verbosity > 0 && po.packet_len > 0) {
174             char *packet = ofp_packet_to_string(po.packet, po.packet_len);
175             ds_put_char(string, '\n');
176             ds_put_cstr(string, packet);
177             free(packet);
178         }
179     } else {
180         ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id);
181     }
182     ds_put_char(string, '\n');
183
184     ofpbuf_uninit(&ofpacts);
185 }
186
187 /* qsort comparison function. */
188 static int
189 compare_ports(const void *a_, const void *b_)
190 {
191     const struct ofputil_phy_port *a = a_;
192     const struct ofputil_phy_port *b = b_;
193     uint16_t ap = a->port_no;
194     uint16_t bp = b->port_no;
195
196     return ap < bp ? -1 : ap > bp;
197 }
198
199 static void
200 ofp_print_bit_names(struct ds *string, uint32_t bits,
201                     const char *(*bit_to_name)(uint32_t bit),
202                     char separator)
203 {
204     int n = 0;
205     int i;
206
207     if (!bits) {
208         ds_put_cstr(string, "0");
209         return;
210     }
211
212     for (i = 0; i < 32; i++) {
213         uint32_t bit = UINT32_C(1) << i;
214
215         if (bits & bit) {
216             const char *name = bit_to_name(bit);
217             if (name) {
218                 if (n++) {
219                     ds_put_char(string, separator);
220                 }
221                 ds_put_cstr(string, name);
222                 bits &= ~bit;
223             }
224         }
225     }
226
227     if (bits) {
228         if (n) {
229             ds_put_char(string, separator);
230         }
231         ds_put_format(string, "0x%"PRIx32, bits);
232     }
233 }
234
235 static const char *
236 netdev_feature_to_name(uint32_t bit)
237 {
238     enum netdev_features f = bit;
239
240     switch (f) {
241     case NETDEV_F_10MB_HD:    return "10MB-HD";
242     case NETDEV_F_10MB_FD:    return "10MB-FD";
243     case NETDEV_F_100MB_HD:   return "100MB-HD";
244     case NETDEV_F_100MB_FD:   return "100MB-FD";
245     case NETDEV_F_1GB_HD:     return "1GB-HD";
246     case NETDEV_F_1GB_FD:     return "1GB-FD";
247     case NETDEV_F_10GB_FD:    return "10GB-FD";
248     case NETDEV_F_40GB_FD:    return "40GB-FD";
249     case NETDEV_F_100GB_FD:   return "100GB-FD";
250     case NETDEV_F_1TB_FD:     return "1TB-FD";
251     case NETDEV_F_OTHER:      return "OTHER";
252     case NETDEV_F_COPPER:     return "COPPER";
253     case NETDEV_F_FIBER:      return "FIBER";
254     case NETDEV_F_AUTONEG:    return "AUTO_NEG";
255     case NETDEV_F_PAUSE:      return "AUTO_PAUSE";
256     case NETDEV_F_PAUSE_ASYM: return "AUTO_PAUSE_ASYM";
257     }
258
259     return NULL;
260 }
261
262 static void
263 ofp_print_port_features(struct ds *string, enum netdev_features features)
264 {
265     ofp_print_bit_names(string, features, netdev_feature_to_name, ' ');
266     ds_put_char(string, '\n');
267 }
268
269 static const char *
270 ofputil_port_config_to_name(uint32_t bit)
271 {
272     enum ofputil_port_config pc = bit;
273
274     switch (pc) {
275     case OFPUTIL_PC_PORT_DOWN:    return "PORT_DOWN";
276     case OFPUTIL_PC_NO_STP:       return "NO_STP";
277     case OFPUTIL_PC_NO_RECV:      return "NO_RECV";
278     case OFPUTIL_PC_NO_RECV_STP:  return "NO_RECV_STP";
279     case OFPUTIL_PC_NO_FLOOD:     return "NO_FLOOD";
280     case OFPUTIL_PC_NO_FWD:       return "NO_FWD";
281     case OFPUTIL_PC_NO_PACKET_IN: return "NO_PACKET_IN";
282     }
283
284     return NULL;
285 }
286
287 static void
288 ofp_print_port_config(struct ds *string, enum ofputil_port_config config)
289 {
290     ofp_print_bit_names(string, config, ofputil_port_config_to_name, ' ');
291     ds_put_char(string, '\n');
292 }
293
294 static const char *
295 ofputil_port_state_to_name(uint32_t bit)
296 {
297     enum ofputil_port_state ps = bit;
298
299     switch (ps) {
300     case OFPUTIL_PS_LINK_DOWN: return "LINK_DOWN";
301     case OFPUTIL_PS_BLOCKED:   return "BLOCKED";
302     case OFPUTIL_PS_LIVE:      return "LIVE";
303
304     case OFPUTIL_PS_STP_LISTEN:
305     case OFPUTIL_PS_STP_LEARN:
306     case OFPUTIL_PS_STP_FORWARD:
307     case OFPUTIL_PS_STP_BLOCK:
308         /* Handled elsewhere. */
309         return NULL;
310     }
311
312     return NULL;
313 }
314
315 static void
316 ofp_print_port_state(struct ds *string, enum ofputil_port_state state)
317 {
318     enum ofputil_port_state stp_state;
319
320     /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
321      * pattern.  We have to special case it.
322      *
323      * OVS doesn't support STP, so this field will always be 0 if we are
324      * talking to OVS, so we'd always print STP_LISTEN in that case.
325      * Therefore, we don't print anything at all if the value is STP_LISTEN, to
326      * avoid confusing users. */
327     stp_state = state & OFPUTIL_PS_STP_MASK;
328     if (stp_state) {
329         ds_put_cstr(string,
330                     (stp_state == OFPUTIL_PS_STP_LEARN ? "STP_LEARN"
331                      : stp_state == OFPUTIL_PS_STP_FORWARD ? "STP_FORWARD"
332                      : "STP_BLOCK"));
333         state &= ~OFPUTIL_PS_STP_MASK;
334         if (state) {
335             ofp_print_bit_names(string, state, ofputil_port_state_to_name,
336                                 ' ');
337         }
338     } else {
339         ofp_print_bit_names(string, state, ofputil_port_state_to_name, ' ');
340     }
341     ds_put_char(string, '\n');
342 }
343
344 static void
345 ofp_print_phy_port(struct ds *string, const struct ofputil_phy_port *port)
346 {
347     char name[sizeof port->name];
348     int j;
349
350     memcpy(name, port->name, sizeof name);
351     for (j = 0; j < sizeof name - 1; j++) {
352         if (!isprint((unsigned char) name[j])) {
353             break;
354         }
355     }
356     name[j] = '\0';
357
358     ds_put_char(string, ' ');
359     ofputil_format_port(port->port_no, string);
360     ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
361                   name, ETH_ADDR_ARGS(port->hw_addr));
362
363     ds_put_cstr(string, "     config:     ");
364     ofp_print_port_config(string, port->config);
365
366     ds_put_cstr(string, "     state:      ");
367     ofp_print_port_state(string, port->state);
368
369     if (port->curr) {
370         ds_put_format(string, "     current:    ");
371         ofp_print_port_features(string, port->curr);
372     }
373     if (port->advertised) {
374         ds_put_format(string, "     advertised: ");
375         ofp_print_port_features(string, port->advertised);
376     }
377     if (port->supported) {
378         ds_put_format(string, "     supported:  ");
379         ofp_print_port_features(string, port->supported);
380     }
381     if (port->peer) {
382         ds_put_format(string, "     peer:       ");
383         ofp_print_port_features(string, port->peer);
384     }
385     ds_put_format(string, "     speed: %"PRIu32" Mbps now, "
386                   "%"PRIu32" Mbps max\n",
387                   port->curr_speed / UINT32_C(1000),
388                   port->max_speed / UINT32_C(1000));
389 }
390
391 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
392  * 'ofp_version', writes a detailed description of each port into
393  * 'string'. */
394 static void
395 ofp_print_phy_ports(struct ds *string, uint8_t ofp_version,
396                     struct ofpbuf *b)
397 {
398     size_t n_ports;
399     struct ofputil_phy_port *ports;
400     enum ofperr error;
401     size_t i;
402
403     n_ports = ofputil_count_phy_ports(ofp_version, b);
404
405     ports = xmalloc(n_ports * sizeof *ports);
406     for (i = 0; i < n_ports; i++) {
407         error = ofputil_pull_phy_port(ofp_version, b, &ports[i]);
408         if (error) {
409             ofp_print_error(string, error);
410             goto exit;
411         }
412     }
413     qsort(ports, n_ports, sizeof *ports, compare_ports);
414     for (i = 0; i < n_ports; i++) {
415         ofp_print_phy_port(string, &ports[i]);
416     }
417
418 exit:
419     free(ports);
420 }
421
422 static const char *
423 ofputil_capabilities_to_name(uint32_t bit)
424 {
425     enum ofputil_capabilities capabilities = bit;
426
427     switch (capabilities) {
428     case OFPUTIL_C_FLOW_STATS:   return "FLOW_STATS";
429     case OFPUTIL_C_TABLE_STATS:  return "TABLE_STATS";
430     case OFPUTIL_C_PORT_STATS:   return "PORT_STATS";
431     case OFPUTIL_C_IP_REASM:     return "IP_REASM";
432     case OFPUTIL_C_QUEUE_STATS:  return "QUEUE_STATS";
433     case OFPUTIL_C_ARP_MATCH_IP: return "ARP_MATCH_IP";
434     case OFPUTIL_C_STP:          return "STP";
435     case OFPUTIL_C_GROUP_STATS:  return "GROUP_STATS";
436     case OFPUTIL_C_PORT_BLOCKED: return "PORT_BLOCKED";
437     }
438
439     return NULL;
440 }
441
442 static const char *
443 ofputil_action_bitmap_to_name(uint32_t bit)
444 {
445     enum ofputil_action_bitmap action = bit;
446
447     switch (action) {
448     case OFPUTIL_A_OUTPUT:         return "OUTPUT";
449     case OFPUTIL_A_SET_VLAN_VID:   return "SET_VLAN_VID";
450     case OFPUTIL_A_SET_VLAN_PCP:   return "SET_VLAN_PCP";
451     case OFPUTIL_A_STRIP_VLAN:     return "STRIP_VLAN";
452     case OFPUTIL_A_SET_DL_SRC:     return "SET_DL_SRC";
453     case OFPUTIL_A_SET_DL_DST:     return "SET_DL_DST";
454     case OFPUTIL_A_SET_NW_SRC:     return "SET_NW_SRC";
455     case OFPUTIL_A_SET_NW_DST:     return "SET_NW_DST";
456     case OFPUTIL_A_SET_NW_ECN:     return "SET_NW_ECN";
457     case OFPUTIL_A_SET_NW_TOS:     return "SET_NW_TOS";
458     case OFPUTIL_A_SET_TP_SRC:     return "SET_TP_SRC";
459     case OFPUTIL_A_SET_TP_DST:     return "SET_TP_DST";
460     case OFPUTIL_A_SET_FIELD:      return "SET_FIELD";
461     case OFPUTIL_A_ENQUEUE:        return "ENQUEUE";
462     case OFPUTIL_A_COPY_TTL_OUT:   return "COPY_TTL_OUT";
463     case OFPUTIL_A_COPY_TTL_IN:    return "COPY_TTL_IN";
464     case OFPUTIL_A_SET_MPLS_LABEL: return "SET_MPLS_LABEL";
465     case OFPUTIL_A_SET_MPLS_TC:    return "SET_MPLS_TC";
466     case OFPUTIL_A_SET_MPLS_TTL:   return "SET_MPLS_TTL";
467     case OFPUTIL_A_DEC_MPLS_TTL:   return "DEC_MPLS_TTL";
468     case OFPUTIL_A_PUSH_VLAN:      return "PUSH_VLAN";
469     case OFPUTIL_A_POP_VLAN:       return "POP_VLAN";
470     case OFPUTIL_A_PUSH_MPLS:      return "PUSH_MPLS";
471     case OFPUTIL_A_POP_MPLS:       return "POP_MPLS";
472     case OFPUTIL_A_SET_QUEUE:      return "SET_QUEUE";
473     case OFPUTIL_A_GROUP:          return "GROUP";
474     case OFPUTIL_A_SET_NW_TTL:     return "SET_NW_TTL";
475     case OFPUTIL_A_DEC_NW_TTL:     return "DEC_NW_TTL";
476     }
477
478     return NULL;
479 }
480
481 static void
482 ofp_print_switch_features(struct ds *string, const struct ofp_header *oh)
483 {
484     struct ofputil_switch_features features;
485     enum ofperr error;
486     struct ofpbuf b;
487
488     error = ofputil_decode_switch_features(oh, &features, &b);
489     if (error) {
490         ofp_print_error(string, error);
491         return;
492     }
493
494     ds_put_format(string, " dpid:%016"PRIx64"\n", features.datapath_id);
495
496     ds_put_format(string, "n_tables:%"PRIu8", n_buffers:%"PRIu32,
497                   features.n_tables, features.n_buffers);
498     if (features.auxiliary_id) {
499         ds_put_format(string, ", auxiliary_id:%"PRIu8, features.auxiliary_id);
500     }
501     ds_put_char(string, '\n');
502
503     ds_put_cstr(string, "capabilities: ");
504     ofp_print_bit_names(string, features.capabilities,
505                         ofputil_capabilities_to_name, ' ');
506     ds_put_char(string, '\n');
507
508     switch ((enum ofp_version)oh->version) {
509     case OFP10_VERSION:
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         break;
515     case OFP11_VERSION:
516     case OFP12_VERSION:
517         break;
518     case OFP13_VERSION:
519         return; /* no ports in ofp13_switch_features */
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 if (om->dl_type == htons(ETH_TYPE_RARP)){
628             ds_put_cstr(&f, "rarp,");
629         } else {
630             skip_type = false;
631         }
632     }
633     print_wild(&f, "in_port=", w & OFPFW10_IN_PORT, verbosity,
634                "%d", ntohs(om->in_port));
635     print_wild(&f, "dl_vlan=", w & OFPFW10_DL_VLAN, verbosity,
636                "%d", ntohs(om->dl_vlan));
637     print_wild(&f, "dl_vlan_pcp=", w & OFPFW10_DL_VLAN_PCP, verbosity,
638                "%d", om->dl_vlan_pcp);
639     print_wild(&f, "dl_src=", w & OFPFW10_DL_SRC, verbosity,
640                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
641     print_wild(&f, "dl_dst=", w & OFPFW10_DL_DST, verbosity,
642                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
643     if (!skip_type) {
644         print_wild(&f, "dl_type=", w & OFPFW10_DL_TYPE, verbosity,
645                    "0x%04x", ntohs(om->dl_type));
646     }
647     print_ip_netmask(&f, "nw_src=", om->nw_src,
648                      (w & OFPFW10_NW_SRC_MASK) >> OFPFW10_NW_SRC_SHIFT,
649                      verbosity);
650     print_ip_netmask(&f, "nw_dst=", om->nw_dst,
651                      (w & OFPFW10_NW_DST_MASK) >> OFPFW10_NW_DST_SHIFT,
652                      verbosity);
653     if (!skip_proto) {
654         if (om->dl_type == htons(ETH_TYPE_ARP) ||
655             om->dl_type == htons(ETH_TYPE_RARP)) {
656             print_wild(&f, "arp_op=", w & OFPFW10_NW_PROTO, verbosity,
657                        "%u", om->nw_proto);
658         } else {
659             print_wild(&f, "nw_proto=", w & OFPFW10_NW_PROTO, verbosity,
660                        "%u", om->nw_proto);
661         }
662     }
663     print_wild(&f, "nw_tos=", w & OFPFW10_NW_TOS, verbosity,
664                "%u", om->nw_tos);
665     if (om->nw_proto == IPPROTO_ICMP) {
666         print_wild(&f, "icmp_type=", w & OFPFW10_ICMP_TYPE, verbosity,
667                    "%d", ntohs(om->tp_src));
668         print_wild(&f, "icmp_code=", w & OFPFW10_ICMP_CODE, verbosity,
669                    "%d", ntohs(om->tp_dst));
670     } else {
671         print_wild(&f, "tp_src=", w & OFPFW10_TP_SRC, verbosity,
672                    "%d", ntohs(om->tp_src));
673         print_wild(&f, "tp_dst=", w & OFPFW10_TP_DST, verbosity,
674                    "%d", ntohs(om->tp_dst));
675     }
676     if (ds_last(&f) == ',') {
677         f.length--;
678     }
679     return ds_cstr(&f);
680 }
681
682 static void
683 ofp_print_flow_flags(struct ds *s, uint16_t flags)
684 {
685     if (flags & OFPFF_SEND_FLOW_REM) {
686         ds_put_cstr(s, "send_flow_rem ");
687     }
688     if (flags & OFPFF_CHECK_OVERLAP) {
689         ds_put_cstr(s, "check_overlap ");
690     }
691     if (flags & OFPFF12_RESET_COUNTS) {
692         ds_put_cstr(s, "reset_counts ");
693     }
694     if (flags & OFPFF13_NO_PKT_COUNTS) {
695         ds_put_cstr(s, "no_packet_counts ");
696     }
697     if (flags & OFPFF13_NO_BYT_COUNTS) {
698         ds_put_cstr(s, "no_byte_counts ");
699     }
700
701     flags &= ~(OFPFF_SEND_FLOW_REM | OFPFF_CHECK_OVERLAP
702                | OFPFF12_RESET_COUNTS
703                | OFPFF13_NO_PKT_COUNTS | OFPFF13_NO_BYT_COUNTS);
704     if (flags) {
705         ds_put_format(s, "flags:0x%"PRIx16" ", flags);
706     }
707 }
708
709 static void
710 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh, int verbosity)
711 {
712     struct ofputil_flow_mod fm;
713     struct ofpbuf ofpacts;
714     bool need_priority;
715     enum ofperr error;
716     enum ofpraw raw;
717     enum ofputil_protocol protocol;
718
719     protocol = ofputil_protocol_from_ofp_version(oh->version);
720     protocol = ofputil_protocol_set_tid(protocol, true);
721
722     ofpbuf_init(&ofpacts, 64);
723     error = ofputil_decode_flow_mod(&fm, oh, protocol, &ofpacts);
724     if (error) {
725         ofpbuf_uninit(&ofpacts);
726         ofp_print_error(s, error);
727         return;
728     }
729
730     ds_put_char(s, ' ');
731     switch (fm.command) {
732     case OFPFC_ADD:
733         ds_put_cstr(s, "ADD");
734         break;
735     case OFPFC_MODIFY:
736         ds_put_cstr(s, "MOD");
737         break;
738     case OFPFC_MODIFY_STRICT:
739         ds_put_cstr(s, "MOD_STRICT");
740         break;
741     case OFPFC_DELETE:
742         ds_put_cstr(s, "DEL");
743         break;
744     case OFPFC_DELETE_STRICT:
745         ds_put_cstr(s, "DEL_STRICT");
746         break;
747     default:
748         ds_put_format(s, "cmd:%d", fm.command);
749     }
750     if (fm.table_id != 0) {
751         ds_put_format(s, " table:%d", fm.table_id);
752     }
753
754     ds_put_char(s, ' ');
755     ofpraw_decode(&raw, oh);
756     if (verbosity >= 3 && raw == OFPRAW_OFPT10_FLOW_MOD) {
757         const struct ofp10_flow_mod *ofm = ofpmsg_body(oh);
758         ofp10_match_print(s, &ofm->match, verbosity);
759
760         /* ofp_print_match() doesn't print priority. */
761         need_priority = true;
762     } else if (verbosity >= 3 && raw == OFPRAW_NXT_FLOW_MOD) {
763         const struct nx_flow_mod *nfm = ofpmsg_body(oh);
764         const void *nxm = nfm + 1;
765         char *nxm_s;
766
767         nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
768         ds_put_cstr(s, nxm_s);
769         free(nxm_s);
770
771         /* nx_match_to_string() doesn't print priority. */
772         need_priority = true;
773     } else {
774         match_format(&fm.match, s, fm.priority);
775
776         /* match_format() does print priority. */
777         need_priority = false;
778     }
779
780     if (ds_last(s) != ' ') {
781         ds_put_char(s, ' ');
782     }
783     if (fm.new_cookie != htonll(0) && fm.new_cookie != htonll(UINT64_MAX)) {
784         ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.new_cookie));
785     }
786     if (fm.cookie_mask != htonll(0)) {
787         ds_put_format(s, "cookie:0x%"PRIx64"/0x%"PRIx64" ",
788                 ntohll(fm.cookie), ntohll(fm.cookie_mask));
789     }
790     if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
791         ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
792     }
793     if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
794         ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
795     }
796     if (fm.priority != OFP_DEFAULT_PRIORITY && need_priority) {
797         ds_put_format(s, "pri:%"PRIu16" ", fm.priority);
798     }
799     if (fm.buffer_id != UINT32_MAX) {
800         ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
801     }
802     if (fm.out_port != OFPP_ANY) {
803         ds_put_format(s, "out_port:");
804         ofputil_format_port(fm.out_port, s);
805         ds_put_char(s, ' ');
806     }
807     if (fm.flags != 0) {
808         ofp_print_flow_flags(s, fm.flags);
809     }
810
811     ofpacts_format(fm.ofpacts, fm.ofpacts_len, s);
812     ofpbuf_uninit(&ofpacts);
813 }
814
815 static void
816 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
817 {
818     ds_put_format(string, "%u", sec);
819     if (nsec > 0) {
820         ds_put_format(string, ".%09u", nsec);
821         while (string->string[string->length - 1] == '0') {
822             string->length--;
823         }
824     }
825     ds_put_char(string, 's');
826 }
827
828 static const char *
829 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason)
830 {
831     static char s[32];
832
833     switch (reason) {
834     case OFPRR_IDLE_TIMEOUT:
835         return "idle";
836     case OFPRR_HARD_TIMEOUT:
837         return "hard";
838     case OFPRR_DELETE:
839         return "delete";
840     case OFPRR_GROUP_DELETE:
841         return "group_delete";
842     case OFPRR_EVICTION:
843         return "eviction";
844     default:
845         sprintf(s, "%d", (int) reason);
846         return s;
847     }
848 }
849
850 static void
851 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
852 {
853     struct ofputil_flow_removed fr;
854     enum ofperr error;
855
856     error = ofputil_decode_flow_removed(&fr, oh);
857     if (error) {
858         ofp_print_error(string, error);
859         return;
860     }
861
862     ds_put_char(string, ' ');
863     match_format(&fr.match, string, fr.priority);
864
865     ds_put_format(string, " reason=%s",
866                   ofp_flow_removed_reason_to_string(fr.reason));
867
868     if (fr.table_id != 255) {
869         ds_put_format(string, " table_id=%"PRIu8, fr.table_id);
870     }
871
872     if (fr.cookie != htonll(0)) {
873         ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
874     }
875     ds_put_cstr(string, " duration");
876     ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
877     ds_put_format(string, " idle%"PRIu16, fr.idle_timeout);
878     if (fr.hard_timeout) {
879         /* The hard timeout was only added in OF1.2, so only print it if it is
880          * actually in use to avoid gratuitous change to the formatting. */
881         ds_put_format(string, " hard%"PRIu16, fr.hard_timeout);
882     }
883     ds_put_format(string, " pkts%"PRIu64" bytes%"PRIu64"\n",
884                   fr.packet_count, fr.byte_count);
885 }
886
887 static void
888 ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
889 {
890     struct ofputil_port_mod pm;
891     enum ofperr error;
892
893     error = ofputil_decode_port_mod(oh, &pm);
894     if (error) {
895         ofp_print_error(string, error);
896         return;
897     }
898
899     ds_put_format(string, "port: %"PRIu16": addr:"ETH_ADDR_FMT"\n",
900                   pm.port_no, ETH_ADDR_ARGS(pm.hw_addr));
901
902     ds_put_format(string, "     config: ");
903     ofp_print_port_config(string, pm.config);
904
905     ds_put_format(string, "     mask:   ");
906     ofp_print_port_config(string, pm.mask);
907
908     ds_put_format(string, "     advertise: ");
909     if (pm.advertise) {
910         ofp_print_port_features(string, pm.advertise);
911     } else {
912         ds_put_format(string, "UNCHANGED\n");
913     }
914 }
915
916 static void
917 ofp_print_error(struct ds *string, enum ofperr error)
918 {
919     if (string->length) {
920         ds_put_char(string, ' ');
921     }
922     ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
923 }
924
925 static void
926 ofp_print_hello(struct ds *string, const struct ofp_header *oh)
927 {
928     uint32_t allowed_versions;
929     bool ok;
930
931     ok = ofputil_decode_hello(oh, &allowed_versions);
932
933     ds_put_cstr(string, "\n version bitmap: ");
934     ofputil_format_version_bitmap(string, allowed_versions);
935
936     if (!ok) {
937         ds_put_cstr(string, "\n unknown data in hello:\n");
938         ds_put_hex_dump(string, oh, ntohs(oh->length), 0, true);
939     }
940 }
941
942 static void
943 ofp_print_error_msg(struct ds *string, const struct ofp_header *oh)
944 {
945     size_t len = ntohs(oh->length);
946     struct ofpbuf payload;
947     enum ofperr error;
948     char *s;
949
950     error = ofperr_decode_msg(oh, &payload);
951     if (!error) {
952         ds_put_cstr(string, "***decode error***");
953         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
954         return;
955     }
956
957     ds_put_format(string, " %s\n", ofperr_get_name(error));
958
959     if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
960         ds_put_printable(string, payload.data, payload.size);
961     } else {
962         s = ofp_to_string(payload.data, payload.size, 1);
963         ds_put_cstr(string, s);
964         free(s);
965     }
966 }
967
968 static void
969 ofp_print_port_status(struct ds *string, const struct ofp_header *oh)
970 {
971     struct ofputil_port_status ps;
972     enum ofperr error;
973
974     error = ofputil_decode_port_status(oh, &ps);
975     if (error) {
976         ofp_print_error(string, error);
977         return;
978     }
979
980     if (ps.reason == OFPPR_ADD) {
981         ds_put_format(string, " ADD:");
982     } else if (ps.reason == OFPPR_DELETE) {
983         ds_put_format(string, " DEL:");
984     } else if (ps.reason == OFPPR_MODIFY) {
985         ds_put_format(string, " MOD:");
986     }
987
988     ofp_print_phy_port(string, &ps.desc);
989 }
990
991 static void
992 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
993 {
994     const struct ofp_desc_stats *ods = ofpmsg_body(oh);
995
996     ds_put_char(string, '\n');
997     ds_put_format(string, "Manufacturer: %.*s\n",
998             (int) sizeof ods->mfr_desc, ods->mfr_desc);
999     ds_put_format(string, "Hardware: %.*s\n",
1000             (int) sizeof ods->hw_desc, ods->hw_desc);
1001     ds_put_format(string, "Software: %.*s\n",
1002             (int) sizeof ods->sw_desc, ods->sw_desc);
1003     ds_put_format(string, "Serial Num: %.*s\n",
1004             (int) sizeof ods->serial_num, ods->serial_num);
1005     ds_put_format(string, "DP Description: %.*s\n",
1006             (int) sizeof ods->dp_desc, ods->dp_desc);
1007 }
1008
1009 static void
1010 ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
1011 {
1012     struct ofputil_flow_stats_request fsr;
1013     enum ofperr error;
1014
1015     error = ofputil_decode_flow_stats_request(&fsr, oh);
1016     if (error) {
1017         ofp_print_error(string, error);
1018         return;
1019     }
1020
1021     if (fsr.table_id != 0xff) {
1022         ds_put_format(string, " table=%"PRIu8, fsr.table_id);
1023     }
1024
1025     if (fsr.out_port != OFPP_ANY) {
1026         ds_put_cstr(string, " out_port=");
1027         ofputil_format_port(fsr.out_port, string);
1028     }
1029
1030     ds_put_char(string, ' ');
1031     match_format(&fsr.match, string, OFP_DEFAULT_PRIORITY);
1032 }
1033
1034 void
1035 ofp_print_flow_stats(struct ds *string, struct ofputil_flow_stats *fs)
1036 {
1037     ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1038                   ntohll(fs->cookie));
1039
1040     ofp_print_duration(string, fs->duration_sec, fs->duration_nsec);
1041     ds_put_format(string, ", table=%"PRIu8", ", fs->table_id);
1042     ds_put_format(string, "n_packets=%"PRIu64", ", fs->packet_count);
1043     ds_put_format(string, "n_bytes=%"PRIu64", ", fs->byte_count);
1044     if (fs->idle_timeout != OFP_FLOW_PERMANENT) {
1045         ds_put_format(string, "idle_timeout=%"PRIu16", ", fs->idle_timeout);
1046     }
1047     if (fs->hard_timeout != OFP_FLOW_PERMANENT) {
1048         ds_put_format(string, "hard_timeout=%"PRIu16", ", fs->hard_timeout);
1049     }
1050     if (fs->flags) {
1051         ofp_print_flow_flags(string, fs->flags);
1052     }
1053     if (fs->idle_age >= 0) {
1054         ds_put_format(string, "idle_age=%d, ", fs->idle_age);
1055     }
1056     if (fs->hard_age >= 0 && fs->hard_age != fs->duration_sec) {
1057         ds_put_format(string, "hard_age=%d, ", fs->hard_age);
1058     }
1059
1060     match_format(&fs->match, string, fs->priority);
1061     if (string->string[string->length - 1] != ' ') {
1062         ds_put_char(string, ' ');
1063     }
1064
1065     ofpacts_format(fs->ofpacts, fs->ofpacts_len, string);
1066 }
1067
1068 static void
1069 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1070 {
1071     struct ofpbuf ofpacts;
1072     struct ofpbuf b;
1073
1074     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1075     ofpbuf_init(&ofpacts, 64);
1076     for (;;) {
1077         struct ofputil_flow_stats fs;
1078         int retval;
1079
1080         retval = ofputil_decode_flow_stats_reply(&fs, &b, true, &ofpacts);
1081         if (retval) {
1082             if (retval != EOF) {
1083                 ds_put_cstr(string, " ***parse error***");
1084             }
1085             break;
1086         }
1087         ds_put_char(string, '\n');
1088         ofp_print_flow_stats(string, &fs);
1089      }
1090     ofpbuf_uninit(&ofpacts);
1091 }
1092
1093 static void
1094 ofp_print_aggregate_stats_reply(struct ds *string, const struct ofp_header *oh)
1095 {
1096     struct ofputil_aggregate_stats as;
1097     enum ofperr error;
1098
1099     error = ofputil_decode_aggregate_stats_reply(&as, oh);
1100     if (error) {
1101         ofp_print_error(string, error);
1102         return;
1103     }
1104
1105     ds_put_format(string, " packet_count=%"PRIu64, as.packet_count);
1106     ds_put_format(string, " byte_count=%"PRIu64, as.byte_count);
1107     ds_put_format(string, " flow_count=%"PRIu32, as.flow_count);
1108 }
1109
1110 static void
1111 print_port_stat(struct ds *string, const char *leader, uint64_t stat, int more)
1112 {
1113     ds_put_cstr(string, leader);
1114     if (stat != UINT64_MAX) {
1115         ds_put_format(string, "%"PRIu64, stat);
1116     } else {
1117         ds_put_char(string, '?');
1118     }
1119     if (more) {
1120         ds_put_cstr(string, ", ");
1121     } else {
1122         ds_put_cstr(string, "\n");
1123     }
1124 }
1125
1126 static void
1127 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1128 {
1129     uint16_t ofp10_port;
1130     enum ofperr error;
1131
1132     error = ofputil_decode_port_stats_request(oh, &ofp10_port);
1133     if (error) {
1134         ofp_print_error(string, error);
1135         return;
1136     }
1137
1138     ds_put_format(string, " port_no=%2"PRIu16, ofp10_port);
1139 }
1140
1141 static void
1142 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1143                            int verbosity)
1144 {
1145     struct ofpbuf b;
1146
1147     ds_put_format(string, " %zu ports\n", ofputil_count_port_stats(oh));
1148     if (verbosity < 1) {
1149         return;
1150     }
1151
1152     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1153     for (;;) {
1154         struct ofputil_port_stats ps;
1155         int retval;
1156
1157         retval = ofputil_decode_port_stats(&ps, &b);
1158         if (retval) {
1159             if (retval != EOF) {
1160                 ds_put_cstr(string, " ***parse error***");
1161             }
1162             return;
1163         }
1164
1165         ds_put_format(string, "  port %2"PRIu16, ps.port_no);
1166
1167         ds_put_cstr(string, ": rx ");
1168         print_port_stat(string, "pkts=", ps.stats.rx_packets, 1);
1169         print_port_stat(string, "bytes=", ps.stats.rx_bytes, 1);
1170         print_port_stat(string, "drop=", ps.stats.rx_dropped, 1);
1171         print_port_stat(string, "errs=", ps.stats.rx_errors, 1);
1172         print_port_stat(string, "frame=", ps.stats.rx_frame_errors, 1);
1173         print_port_stat(string, "over=", ps.stats.rx_over_errors, 1);
1174         print_port_stat(string, "crc=", ps.stats.rx_crc_errors, 0);
1175
1176         ds_put_cstr(string, "           tx ");
1177         print_port_stat(string, "pkts=", ps.stats.tx_packets, 1);
1178         print_port_stat(string, "bytes=", ps.stats.tx_bytes, 1);
1179         print_port_stat(string, "drop=", ps.stats.tx_dropped, 1);
1180         print_port_stat(string, "errs=", ps.stats.tx_errors, 1);
1181         print_port_stat(string, "coll=", ps.stats.collisions, 0);
1182     }
1183 }
1184
1185 static void
1186 ofp_print_one_ofpst_table_reply(struct ds *string, enum ofp_version ofp_version,
1187                                 const char *name, struct ofp12_table_stats *ts)
1188 {
1189     char name_[OFP_MAX_TABLE_NAME_LEN + 1];
1190
1191     /* ofp13_table_stats is different */
1192     if (ofp_version > OFP12_VERSION) {
1193         return;
1194     }
1195
1196     ovs_strlcpy(name_, name, sizeof name_);
1197
1198     ds_put_format(string, "  %d: %-8s: ", ts->table_id, name_);
1199     ds_put_format(string, "wild=0x%05"PRIx64", ", ntohll(ts->wildcards));
1200     ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1201     ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1202     ds_put_cstr(string, "               ");
1203     ds_put_format(string, "lookup=%"PRIu64", ", ntohll(ts->lookup_count));
1204     ds_put_format(string, "matched=%"PRIu64"\n", ntohll(ts->matched_count));
1205
1206     if (ofp_version < OFP11_VERSION) {
1207         return;
1208     }
1209
1210     ds_put_cstr(string, "               ");
1211     ds_put_format(string, "match=0x%08"PRIx64", ", ntohll(ts->match));
1212     ds_put_format(string, "instructions=0x%08"PRIx32", ",
1213                   ntohl(ts->instructions));
1214     ds_put_format(string, "config=0x%08"PRIx32"\n", ntohl(ts->config));
1215     ds_put_cstr(string, "               ");
1216     ds_put_format(string, "write_actions=0x%08"PRIx32", ",
1217                   ntohl(ts->write_actions));
1218     ds_put_format(string, "apply_actions=0x%08"PRIx32"\n",
1219                   ntohl(ts->apply_actions));
1220
1221     if (ofp_version < OFP12_VERSION) {
1222         return;
1223     }
1224
1225     ds_put_cstr(string, "               ");
1226     ds_put_format(string, "write_setfields=0x%016"PRIx64"\n",
1227                   ntohll(ts->write_setfields));
1228     ds_put_cstr(string, "               ");
1229     ds_put_format(string, "apply_setfields=0x%016"PRIx64"\n",
1230                   ntohll(ts->apply_setfields));
1231     ds_put_cstr(string, "               ");
1232     ds_put_format(string, "metadata_match=0x%016"PRIx64"\n",
1233                   ntohll(ts->metadata_match));
1234     ds_put_cstr(string, "               ");
1235     ds_put_format(string, "metadata_write=0x%016"PRIx64"\n",
1236                   ntohll(ts->metadata_write));
1237 }
1238
1239 static void
1240 ofp_print_ofpst_table_reply13(struct ds *string, const struct ofp_header *oh,
1241                               int verbosity)
1242 {
1243     struct ofp13_table_stats *ts;
1244     struct ofpbuf b;
1245     size_t n;
1246
1247     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1248     ofpraw_pull_assert(&b);
1249
1250     n = b.size / sizeof *ts;
1251     ds_put_format(string, " %zu tables\n", n);
1252     if (verbosity < 1) {
1253         return;
1254     }
1255
1256     for (;;) {
1257         ts = ofpbuf_try_pull(&b, sizeof *ts);
1258         if (!ts) {
1259             return;
1260         }
1261         ds_put_format(string,
1262                       "  %d: active=%"PRIu32", lookup=%"PRIu64  \
1263                       ", matched=%"PRIu64"\n",
1264                       ts->table_id, ntohl(ts->active_count),
1265                       ntohll(ts->lookup_count), ntohll(ts->matched_count));
1266     }
1267 }
1268
1269 static void
1270 ofp_print_ofpst_table_reply12(struct ds *string, const struct ofp_header *oh,
1271                               int verbosity)
1272 {
1273     struct ofp12_table_stats *ts;
1274     struct ofpbuf b;
1275     size_t n;
1276
1277     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1278     ofpraw_pull_assert(&b);
1279
1280     n = b.size / sizeof *ts;
1281     ds_put_format(string, " %zu tables\n", n);
1282     if (verbosity < 1) {
1283         return;
1284     }
1285
1286     for (;;) {
1287         ts = ofpbuf_try_pull(&b, sizeof *ts);
1288         if (!ts) {
1289             return;
1290         }
1291
1292         ofp_print_one_ofpst_table_reply(string, OFP12_VERSION, ts->name, ts);
1293      }
1294 }
1295
1296 static void
1297 ofp_print_ofpst_table_reply11(struct ds *string, const struct ofp_header *oh,
1298                               int verbosity)
1299 {
1300     struct ofp11_table_stats *ts;
1301     struct ofpbuf b;
1302     size_t n;
1303
1304     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1305     ofpraw_pull_assert(&b);
1306
1307     n = b.size / sizeof *ts;
1308     ds_put_format(string, " %zu tables\n", n);
1309     if (verbosity < 1) {
1310         return;
1311     }
1312
1313     for (;;) {
1314         struct ofp12_table_stats ts12;
1315
1316         ts = ofpbuf_try_pull(&b, sizeof *ts);
1317         if (!ts) {
1318             return;
1319         }
1320
1321         ts12.table_id = ts->table_id;
1322         ts12.wildcards = htonll(ntohl(ts->wildcards));
1323         ts12.max_entries = ts->max_entries;
1324         ts12.active_count = ts->active_count;
1325         ts12.lookup_count = ts->lookup_count;
1326         ts12.matched_count = ts->matched_count;
1327         ts12.match = htonll(ntohl(ts->match));
1328         ts12.instructions = ts->instructions;
1329         ts12.config = ts->config;
1330         ts12.write_actions = ts->write_actions;
1331         ts12.apply_actions = ts->apply_actions;
1332         ofp_print_one_ofpst_table_reply(string, OFP11_VERSION, ts->name, &ts12);
1333      }
1334 }
1335
1336 static void
1337 ofp_print_ofpst_table_reply10(struct ds *string, const struct ofp_header *oh,
1338                               int verbosity)
1339 {
1340     struct ofp10_table_stats *ts;
1341     struct ofpbuf b;
1342     size_t n;
1343
1344     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1345     ofpraw_pull_assert(&b);
1346
1347     n = b.size / sizeof *ts;
1348     ds_put_format(string, " %zu tables\n", n);
1349     if (verbosity < 1) {
1350         return;
1351     }
1352
1353     for (;;) {
1354         struct ofp12_table_stats ts12;
1355
1356         ts = ofpbuf_try_pull(&b, sizeof *ts);
1357         if (!ts) {
1358             return;
1359         }
1360
1361         ts12.table_id = ts->table_id;
1362         ts12.wildcards = htonll(ntohl(ts->wildcards));
1363         ts12.max_entries = ts->max_entries;
1364         ts12.active_count = ts->active_count;
1365         ts12.lookup_count = get_32aligned_be64(&ts->lookup_count);
1366         ts12.matched_count = get_32aligned_be64(&ts->matched_count);
1367         ofp_print_one_ofpst_table_reply(string, OFP10_VERSION, ts->name, &ts12);
1368      }
1369 }
1370
1371 static void
1372 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1373                             int verbosity)
1374 {
1375     switch ((enum ofp_version)oh->version) {
1376     case OFP13_VERSION:
1377         ofp_print_ofpst_table_reply13(string, oh, verbosity);
1378         break;
1379
1380     case OFP12_VERSION:
1381         ofp_print_ofpst_table_reply12(string, oh, verbosity);
1382         break;
1383
1384     case OFP11_VERSION:
1385         ofp_print_ofpst_table_reply11(string, oh, verbosity);
1386         break;
1387
1388     case OFP10_VERSION:
1389         ofp_print_ofpst_table_reply10(string, oh, verbosity);
1390         break;
1391
1392     default:
1393         NOT_REACHED();
1394     }
1395 }
1396
1397 static void
1398 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1399 {
1400     if (queue_id == OFPQ_ALL) {
1401         ds_put_cstr(string, "ALL");
1402     } else {
1403         ds_put_format(string, "%"PRIu32, queue_id);
1404     }
1405 }
1406
1407 static void
1408 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1409 {
1410     struct ofputil_queue_stats_request oqsr;
1411     enum ofperr error;
1412
1413     error = ofputil_decode_queue_stats_request(oh, &oqsr);
1414     if (error) {
1415         ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1416         return;
1417     }
1418
1419     ds_put_cstr(string, "port=");
1420     ofputil_format_port(oqsr.port_no, string);
1421
1422     ds_put_cstr(string, " queue=");
1423     ofp_print_queue_name(string, oqsr.queue_id);
1424 }
1425
1426 static void
1427 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1428                             int verbosity)
1429 {
1430     struct ofpbuf b;
1431
1432     ds_put_format(string, " %zu queues\n", ofputil_count_queue_stats(oh));
1433     if (verbosity < 1) {
1434         return;
1435     }
1436
1437     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1438     for (;;) {
1439         struct ofputil_queue_stats qs;
1440         int retval;
1441
1442         retval = ofputil_decode_queue_stats(&qs, &b);
1443         if (retval) {
1444             if (retval != EOF) {
1445                 ds_put_cstr(string, " ***parse error***");
1446             }
1447             return;
1448         }
1449
1450         ds_put_cstr(string, "  port ");
1451         ofputil_format_port(qs.port_no, string);
1452         ds_put_cstr(string, " queue ");
1453         ofp_print_queue_name(string, qs.queue_id);
1454         ds_put_cstr(string, ": ");
1455
1456         print_port_stat(string, "bytes=", qs.stats.tx_bytes, 1);
1457         print_port_stat(string, "pkts=", qs.stats.tx_packets, 1);
1458         print_port_stat(string, "errors=", qs.stats.tx_errors, 0);
1459     }
1460 }
1461
1462 static void
1463 ofp_print_ofpst_port_desc_reply(struct ds *string,
1464                                 const struct ofp_header *oh)
1465 {
1466     struct ofpbuf b;
1467
1468     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1469     ofpraw_pull_assert(&b);
1470     ds_put_char(string, '\n');
1471     ofp_print_phy_ports(string, oh->version, &b);
1472 }
1473
1474 static void
1475 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1476 {
1477     uint16_t flags = ofpmp_flags(oh);
1478
1479     if (flags) {
1480         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
1481     }
1482 }
1483
1484 static void
1485 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1486 {
1487     uint16_t flags = ofpmp_flags(oh);
1488
1489     if (flags) {
1490         ds_put_cstr(string, " flags=");
1491         if (flags & OFPSF_REPLY_MORE) {
1492             ds_put_cstr(string, "[more]");
1493             flags &= ~OFPSF_REPLY_MORE;
1494         }
1495         if (flags) {
1496             ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1497                           flags);
1498         }
1499     }
1500 }
1501
1502 static void
1503 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1504 {
1505     size_t len = ntohs(oh->length);
1506
1507     ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1508     if (verbosity > 1) {
1509         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1510     }
1511 }
1512
1513 static void
1514 ofp_print_nxt_role_message(struct ds *string,
1515                            const struct nx_role_request *nrr)
1516 {
1517     unsigned int role = ntohl(nrr->role);
1518
1519     ds_put_cstr(string, " role=");
1520     if (role == NX_ROLE_OTHER) {
1521         ds_put_cstr(string, "other");
1522     } else if (role == NX_ROLE_MASTER) {
1523         ds_put_cstr(string, "master");
1524     } else if (role == NX_ROLE_SLAVE) {
1525         ds_put_cstr(string, "slave");
1526     } else {
1527         ds_put_format(string, "%u", role);
1528     }
1529 }
1530
1531 static void
1532 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1533                                 const struct nx_flow_mod_table_id *nfmti)
1534 {
1535     ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1536 }
1537
1538 static void
1539 ofp_print_nxt_set_flow_format(struct ds *string,
1540                               const struct nx_set_flow_format *nsff)
1541 {
1542     uint32_t format = ntohl(nsff->format);
1543
1544     ds_put_cstr(string, " format=");
1545     if (ofputil_nx_flow_format_is_valid(format)) {
1546         ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
1547     } else {
1548         ds_put_format(string, "%"PRIu32, format);
1549     }
1550 }
1551
1552 static void
1553 ofp_print_nxt_set_packet_in_format(struct ds *string,
1554                                    const struct nx_set_packet_in_format *nspf)
1555 {
1556     uint32_t format = ntohl(nspf->format);
1557
1558     ds_put_cstr(string, " format=");
1559     if (ofputil_packet_in_format_is_valid(format)) {
1560         ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1561     } else {
1562         ds_put_format(string, "%"PRIu32, format);
1563     }
1564 }
1565
1566 static const char *
1567 ofp_port_reason_to_string(enum ofp_port_reason reason)
1568 {
1569     static char s[32];
1570
1571     switch (reason) {
1572     case OFPPR_ADD:
1573         return "add";
1574
1575     case OFPPR_DELETE:
1576         return "delete";
1577
1578     case OFPPR_MODIFY:
1579         return "modify";
1580
1581     default:
1582         sprintf(s, "%d", (int) reason);
1583         return s;
1584     }
1585 }
1586
1587 static void
1588 ofp_print_nxt_set_async_config(struct ds *string,
1589                                const struct nx_async_config *nac)
1590 {
1591     int i;
1592
1593     for (i = 0; i < 2; i++) {
1594         int j;
1595
1596         ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1597
1598         ds_put_cstr(string, "       PACKET_IN:");
1599         for (j = 0; j < 32; j++) {
1600             if (nac->packet_in_mask[i] & htonl(1u << j)) {
1601                 ds_put_format(string, " %s",
1602                               ofputil_packet_in_reason_to_string(j));
1603             }
1604         }
1605         if (!nac->packet_in_mask[i]) {
1606             ds_put_cstr(string, " (off)");
1607         }
1608         ds_put_char(string, '\n');
1609
1610         ds_put_cstr(string, "     PORT_STATUS:");
1611         for (j = 0; j < 32; j++) {
1612             if (nac->port_status_mask[i] & htonl(1u << j)) {
1613                 ds_put_format(string, " %s", ofp_port_reason_to_string(j));
1614             }
1615         }
1616         if (!nac->port_status_mask[i]) {
1617             ds_put_cstr(string, " (off)");
1618         }
1619         ds_put_char(string, '\n');
1620
1621         ds_put_cstr(string, "    FLOW_REMOVED:");
1622         for (j = 0; j < 32; j++) {
1623             if (nac->flow_removed_mask[i] & htonl(1u << j)) {
1624                 ds_put_format(string, " %s",
1625                               ofp_flow_removed_reason_to_string(j));
1626             }
1627         }
1628         if (!nac->flow_removed_mask[i]) {
1629             ds_put_cstr(string, " (off)");
1630         }
1631         ds_put_char(string, '\n');
1632     }
1633 }
1634
1635 static void
1636 ofp_print_nxt_set_controller_id(struct ds *string,
1637                                 const struct nx_controller_id *nci)
1638 {
1639     ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
1640 }
1641
1642 static void
1643 ofp_print_nxt_flow_monitor_cancel(struct ds *string,
1644                                   const struct ofp_header *oh)
1645 {
1646     ds_put_format(string, " id=%"PRIu32,
1647                   ofputil_decode_flow_monitor_cancel(oh));
1648 }
1649
1650 static const char *
1651 nx_flow_monitor_flags_to_name(uint32_t bit)
1652 {
1653     enum nx_flow_monitor_flags fmf = bit;
1654
1655     switch (fmf) {
1656     case NXFMF_INITIAL: return "initial";
1657     case NXFMF_ADD: return "add";
1658     case NXFMF_DELETE: return "delete";
1659     case NXFMF_MODIFY: return "modify";
1660     case NXFMF_ACTIONS: return "actions";
1661     case NXFMF_OWN: return "own";
1662     }
1663
1664     return NULL;
1665 }
1666
1667 static void
1668 ofp_print_nxst_flow_monitor_request(struct ds *string,
1669                                     const struct ofp_header *oh)
1670 {
1671     struct ofpbuf b;
1672
1673     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1674     for (;;) {
1675         struct ofputil_flow_monitor_request request;
1676         int retval;
1677
1678         retval = ofputil_decode_flow_monitor_request(&request, &b);
1679         if (retval) {
1680             if (retval != EOF) {
1681                 ofp_print_error(string, retval);
1682             }
1683             return;
1684         }
1685
1686         ds_put_format(string, "\n id=%"PRIu32" flags=", request.id);
1687         ofp_print_bit_names(string, request.flags,
1688                             nx_flow_monitor_flags_to_name, ',');
1689
1690         if (request.out_port != OFPP_NONE) {
1691             ds_put_cstr(string, " out_port=");
1692             ofputil_format_port(request.out_port, string);
1693         }
1694
1695         if (request.table_id != 0xff) {
1696             ds_put_format(string, " table=%"PRIu8, request.table_id);
1697         }
1698
1699         ds_put_char(string, ' ');
1700         match_format(&request.match, string, OFP_DEFAULT_PRIORITY);
1701         ds_chomp(string, ' ');
1702     }
1703 }
1704
1705 static void
1706 ofp_print_nxst_flow_monitor_reply(struct ds *string,
1707                                   const struct ofp_header *oh)
1708 {
1709     uint64_t ofpacts_stub[1024 / 8];
1710     struct ofpbuf ofpacts;
1711     struct ofpbuf b;
1712
1713     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1714     ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1715     for (;;) {
1716         struct ofputil_flow_update update;
1717         struct match match;
1718         int retval;
1719
1720         update.match = &match;
1721         retval = ofputil_decode_flow_update(&update, &b, &ofpacts);
1722         if (retval) {
1723             if (retval != EOF) {
1724                 ofp_print_error(string, retval);
1725             }
1726             ofpbuf_uninit(&ofpacts);
1727             return;
1728         }
1729
1730         ds_put_cstr(string, "\n event=");
1731         switch (update.event) {
1732         case NXFME_ADDED:
1733             ds_put_cstr(string, "ADDED");
1734             break;
1735
1736         case NXFME_DELETED:
1737             ds_put_format(string, "DELETED reason=%s",
1738                           ofp_flow_removed_reason_to_string(update.reason));
1739             break;
1740
1741         case NXFME_MODIFIED:
1742             ds_put_cstr(string, "MODIFIED");
1743             break;
1744
1745         case NXFME_ABBREV:
1746             ds_put_format(string, "ABBREV xid=0x%"PRIx32, ntohl(update.xid));
1747             continue;
1748         }
1749
1750         ds_put_format(string, " table=%"PRIu8, update.table_id);
1751         if (update.idle_timeout != OFP_FLOW_PERMANENT) {
1752             ds_put_format(string, " idle_timeout=%"PRIu16,
1753                           update.idle_timeout);
1754         }
1755         if (update.hard_timeout != OFP_FLOW_PERMANENT) {
1756             ds_put_format(string, " hard_timeout=%"PRIu16,
1757                           update.hard_timeout);
1758         }
1759         ds_put_format(string, " cookie=%#"PRIx64, ntohll(update.cookie));
1760
1761         ds_put_char(string, ' ');
1762         match_format(update.match, string, OFP_DEFAULT_PRIORITY);
1763
1764         if (update.ofpacts_len) {
1765             if (string->string[string->length - 1] != ' ') {
1766                 ds_put_char(string, ' ');
1767             }
1768             ofpacts_format(update.ofpacts, update.ofpacts_len, string);
1769         }
1770     }
1771 }
1772
1773 void
1774 ofp_print_version(const struct ofp_header *oh,
1775                   struct ds *string)
1776 {
1777     switch (oh->version) {
1778     case OFP10_VERSION:
1779         break;
1780     case OFP11_VERSION:
1781         ds_put_cstr(string, " (OF1.1)");
1782         break;
1783     case OFP12_VERSION:
1784         ds_put_cstr(string, " (OF1.2)");
1785         break;
1786     case OFP13_VERSION:
1787         ds_put_cstr(string, " (OF1.3)");
1788         break;
1789     default:
1790         ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
1791         break;
1792     }
1793     ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
1794 }
1795
1796 static void
1797 ofp_header_to_string__(const struct ofp_header *oh, enum ofpraw raw,
1798                        struct ds *string)
1799 {
1800     ds_put_cstr(string, ofpraw_get_name(raw));
1801     ofp_print_version(oh, string);
1802 }
1803
1804 static void
1805 ofp_print_not_implemented(struct ds *string)
1806 {
1807     ds_put_cstr(string, "NOT IMPLEMENTED YET!\n");
1808 }
1809
1810 static void
1811 ofp_to_string__(const struct ofp_header *oh, enum ofpraw raw,
1812                 struct ds *string, int verbosity)
1813 {
1814     const void *msg = oh;
1815
1816     ofp_header_to_string__(oh, raw, string);
1817     switch (ofptype_from_ofpraw(raw)) {
1818
1819         /* FIXME: Change the following once they are implemented: */
1820     case OFPTYPE_GET_ASYNC_REQUEST:
1821     case OFPTYPE_GET_ASYNC_REPLY:
1822     case OFPTYPE_METER_MOD:
1823     case OFPTYPE_GROUP_REQUEST:
1824     case OFPTYPE_GROUP_REPLY:
1825     case OFPTYPE_GROUP_DESC_REQUEST:
1826     case OFPTYPE_GROUP_DESC_REPLY:
1827     case OFPTYPE_GROUP_FEATURES_REQUEST:
1828     case OFPTYPE_GROUP_FEATURES_REPLY:
1829     case OFPTYPE_METER_REQUEST:
1830     case OFPTYPE_METER_REPLY:
1831     case OFPTYPE_METER_CONFIG_REQUEST:
1832     case OFPTYPE_METER_CONFIG_REPLY:
1833     case OFPTYPE_METER_FEATURES_REQUEST:
1834     case OFPTYPE_METER_FEATURES_REPLY:
1835     case OFPTYPE_TABLE_FEATURES_REQUEST:
1836     case OFPTYPE_TABLE_FEATURES_REPLY:
1837         ofp_print_not_implemented(string);
1838         break;
1839
1840     case OFPTYPE_HELLO:
1841         ofp_print_hello(string, oh);
1842         break;
1843
1844     case OFPTYPE_ERROR:
1845         ofp_print_error_msg(string, oh);
1846         break;
1847
1848     case OFPTYPE_ECHO_REQUEST:
1849     case OFPTYPE_ECHO_REPLY:
1850         ofp_print_echo(string, oh, verbosity);
1851         break;
1852
1853     case OFPTYPE_FEATURES_REQUEST:
1854         break;
1855
1856     case OFPTYPE_FEATURES_REPLY:
1857         ofp_print_switch_features(string, oh);
1858         break;
1859
1860     case OFPTYPE_GET_CONFIG_REQUEST:
1861         break;
1862
1863     case OFPTYPE_GET_CONFIG_REPLY:
1864     case OFPTYPE_SET_CONFIG:
1865         ofp_print_switch_config(string, ofpmsg_body(oh));
1866         break;
1867
1868     case OFPTYPE_PACKET_IN:
1869         ofp_print_packet_in(string, oh, verbosity);
1870         break;
1871
1872     case OFPTYPE_FLOW_REMOVED:
1873         ofp_print_flow_removed(string, oh);
1874         break;
1875
1876     case OFPTYPE_PORT_STATUS:
1877         ofp_print_port_status(string, oh);
1878         break;
1879
1880     case OFPTYPE_PACKET_OUT:
1881         ofp_print_packet_out(string, oh, verbosity);
1882         break;
1883
1884     case OFPTYPE_FLOW_MOD:
1885         ofp_print_flow_mod(string, oh, verbosity);
1886         break;
1887
1888     case OFPTYPE_PORT_MOD:
1889         ofp_print_port_mod(string, oh);
1890         break;
1891
1892     case OFPTYPE_BARRIER_REQUEST:
1893     case OFPTYPE_BARRIER_REPLY:
1894         break;
1895
1896     case OFPTYPE_DESC_STATS_REQUEST:
1897     case OFPTYPE_PORT_DESC_STATS_REQUEST:
1898         ofp_print_stats_request(string, oh);
1899         break;
1900
1901     case OFPTYPE_FLOW_STATS_REQUEST:
1902     case OFPTYPE_AGGREGATE_STATS_REQUEST:
1903         ofp_print_stats_request(string, oh);
1904         ofp_print_flow_stats_request(string, oh);
1905         break;
1906
1907     case OFPTYPE_TABLE_STATS_REQUEST:
1908         ofp_print_stats_request(string, oh);
1909         break;
1910
1911     case OFPTYPE_PORT_STATS_REQUEST:
1912         ofp_print_stats_request(string, oh);
1913         ofp_print_ofpst_port_request(string, oh);
1914         break;
1915
1916     case OFPTYPE_QUEUE_STATS_REQUEST:
1917         ofp_print_stats_request(string, oh);
1918         ofp_print_ofpst_queue_request(string, oh);
1919         break;
1920
1921     case OFPTYPE_DESC_STATS_REPLY:
1922         ofp_print_stats_reply(string, oh);
1923         ofp_print_ofpst_desc_reply(string, oh);
1924         break;
1925
1926     case OFPTYPE_FLOW_STATS_REPLY:
1927         ofp_print_stats_reply(string, oh);
1928         ofp_print_flow_stats_reply(string, oh);
1929         break;
1930
1931     case OFPTYPE_QUEUE_STATS_REPLY:
1932         ofp_print_stats_reply(string, oh);
1933         ofp_print_ofpst_queue_reply(string, oh, verbosity);
1934         break;
1935
1936     case OFPTYPE_PORT_STATS_REPLY:
1937         ofp_print_stats_reply(string, oh);
1938         ofp_print_ofpst_port_reply(string, oh, verbosity);
1939         break;
1940
1941     case OFPTYPE_TABLE_STATS_REPLY:
1942         ofp_print_stats_reply(string, oh);
1943         ofp_print_ofpst_table_reply(string, oh, verbosity);
1944         break;
1945
1946     case OFPTYPE_AGGREGATE_STATS_REPLY:
1947         ofp_print_stats_reply(string, oh);
1948         ofp_print_aggregate_stats_reply(string, oh);
1949         break;
1950
1951     case OFPTYPE_PORT_DESC_STATS_REPLY:
1952         ofp_print_stats_reply(string, oh);
1953         ofp_print_ofpst_port_desc_reply(string, oh);
1954         break;
1955
1956     case OFPTYPE_ROLE_REQUEST:
1957     case OFPTYPE_ROLE_REPLY:
1958         ofp_print_nxt_role_message(string, ofpmsg_body(oh));
1959         break;
1960
1961     case OFPTYPE_FLOW_MOD_TABLE_ID:
1962         ofp_print_nxt_flow_mod_table_id(string, ofpmsg_body(oh));
1963         break;
1964
1965     case OFPTYPE_SET_FLOW_FORMAT:
1966         ofp_print_nxt_set_flow_format(string, ofpmsg_body(oh));
1967         break;
1968
1969     case OFPTYPE_SET_PACKET_IN_FORMAT:
1970         ofp_print_nxt_set_packet_in_format(string, ofpmsg_body(oh));
1971         break;
1972
1973     case OFPTYPE_FLOW_AGE:
1974         break;
1975
1976     case OFPTYPE_SET_CONTROLLER_ID:
1977         ofp_print_nxt_set_controller_id(string, ofpmsg_body(oh));
1978         break;
1979
1980     case OFPTYPE_SET_ASYNC_CONFIG:
1981         ofp_print_nxt_set_async_config(string, ofpmsg_body(oh));
1982         break;
1983
1984     case OFPTYPE_FLOW_MONITOR_CANCEL:
1985         ofp_print_nxt_flow_monitor_cancel(string, msg);
1986         break;
1987
1988     case OFPTYPE_FLOW_MONITOR_PAUSED:
1989     case OFPTYPE_FLOW_MONITOR_RESUMED:
1990         break;
1991
1992     case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
1993         ofp_print_nxst_flow_monitor_request(string, msg);
1994         break;
1995
1996     case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
1997         ofp_print_nxst_flow_monitor_reply(string, msg);
1998         break;
1999     }
2000 }
2001
2002 /* Composes and returns a string representing the OpenFlow packet of 'len'
2003  * bytes at 'oh' at the given 'verbosity' level.  0 is a minimal amount of
2004  * verbosity and higher numbers increase verbosity.  The caller is responsible
2005  * for freeing the string. */
2006 char *
2007 ofp_to_string(const void *oh_, size_t len, int verbosity)
2008 {
2009     struct ds string = DS_EMPTY_INITIALIZER;
2010     const struct ofp_header *oh = oh_;
2011
2012     if (!len) {
2013         ds_put_cstr(&string, "OpenFlow message is empty\n");
2014     } else if (len < sizeof(struct ofp_header)) {
2015         ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
2016                       len);
2017     } else if (ntohs(oh->length) > len) {
2018         enum ofperr error;
2019         enum ofpraw raw;
2020
2021         error = ofpraw_decode_partial(&raw, oh, len);
2022         if (!error) {
2023             ofp_header_to_string__(oh, raw, &string);
2024             ds_put_char(&string, '\n');
2025         }
2026
2027         ds_put_format(&string,
2028                       "(***truncated to %zu bytes from %"PRIu16"***)\n",
2029                       len, ntohs(oh->length));
2030     } else if (ntohs(oh->length) < len) {
2031         ds_put_format(&string,
2032                       "(***only uses %"PRIu16" bytes out of %zu***)\n",
2033                       ntohs(oh->length), len);
2034     } else {
2035         enum ofperr error;
2036         enum ofpraw raw;
2037
2038         error = ofpraw_decode(&raw, oh);
2039         if (!error) {
2040             ofp_to_string__(oh, raw, &string, verbosity);
2041             if (verbosity >= 5) {
2042                 if (ds_last(&string) != '\n') {
2043                     ds_put_char(&string, '\n');
2044                 }
2045                 ds_put_hex_dump(&string, oh, len, 0, true);
2046             }
2047             if (ds_last(&string) != '\n') {
2048                 ds_put_char(&string, '\n');
2049             }
2050             return ds_steal_cstr(&string);
2051         }
2052
2053         ofp_print_error(&string, error);
2054     }
2055     ds_put_hex_dump(&string, oh, len, 0, true);
2056     return ds_steal_cstr(&string);
2057 }
2058 \f
2059 static void
2060 print_and_free(FILE *stream, char *string)
2061 {
2062     fputs(string, stream);
2063     free(string);
2064 }
2065
2066 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
2067  * given 'verbosity' level.  0 is a minimal amount of verbosity and higher
2068  * numbers increase verbosity. */
2069 void
2070 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
2071 {
2072     print_and_free(stream, ofp_to_string(oh, len, verbosity));
2073 }
2074
2075 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
2076  * 'data' to 'stream'. */
2077 void
2078 ofp_print_packet(FILE *stream, const void *data, size_t len)
2079 {
2080     print_and_free(stream, ofp_packet_to_string(data, len));
2081 }