ofputil: New function ofputil_decode_packet_in().
[openvswitch] / lib / ofp-print.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011 Nicira Networks.
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 "nx-match.h"
37 #include "ofp-util.h"
38 #include "ofpbuf.h"
39 #include "openflow/openflow.h"
40 #include "openflow/nicira-ext.h"
41 #include "packets.h"
42 #include "pcap.h"
43 #include "type-props.h"
44 #include "unaligned.h"
45 #include "util.h"
46
47 static void ofp_print_queue_name(struct ds *string, uint32_t port);
48 static void ofp_print_error(struct ds *, int error);
49
50
51 /* Returns a string that represents the contents of the Ethernet frame in the
52  * 'len' bytes starting at 'data'.  The caller must free the returned string.*/
53 char *
54 ofp_packet_to_string(const void *data, size_t len)
55 {
56     struct ds ds = DS_EMPTY_INITIALIZER;
57     struct ofpbuf buf;
58     struct flow flow;
59
60     ofpbuf_use_const(&buf, data, len);
61     flow_extract(&buf, 0, 0, 0, &flow);
62     flow_format(&ds, &flow);
63
64     if (buf.l7) {
65         if (flow.nw_proto == IPPROTO_TCP) {
66             struct tcp_header *th = buf.l4;
67             ds_put_format(&ds, " tcp_csum:%"PRIx16,
68                           ntohs(th->tcp_csum));
69         } else if (flow.nw_proto == IPPROTO_UDP) {
70             struct udp_header *uh = buf.l4;
71             ds_put_format(&ds, " udp_csum:%"PRIx16,
72                           ntohs(uh->udp_csum));
73         }
74     }
75
76     ds_put_char(&ds, '\n');
77
78     return ds_cstr(&ds);
79 }
80
81 static void
82 ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
83                     int verbosity)
84 {
85     struct ofputil_packet_in pin;
86     int error;
87
88     error = ofputil_decode_packet_in(&pin, oh);
89     if (error) {
90         ofp_print_error(string, error);
91         return;
92     }
93
94     ds_put_format(string, " total_len=%"PRIu16" in_port=", pin.total_len);
95     ofputil_format_port(pin.in_port, string);
96
97     if (pin.reason == OFPR_ACTION) {
98         ds_put_cstr(string, " (via action)");
99     } else if (pin.reason != OFPR_NO_MATCH) {
100         ds_put_format(string, " (***reason %"PRIu8"***)", pin.reason);
101     }
102
103     ds_put_format(string, " data_len=%zu", pin.packet_len);
104     if (pin.buffer_id == UINT32_MAX) {
105         ds_put_format(string, " (unbuffered)");
106         if (pin.total_len != pin.packet_len) {
107             ds_put_format(string, " (***total_len != data_len***)");
108         }
109     } else {
110         ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
111         if (pin.total_len < pin.packet_len) {
112             ds_put_format(string, " (***total_len < data_len***)");
113         }
114     }
115     ds_put_char(string, '\n');
116
117     if (verbosity > 0) {
118         char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
119         ds_put_cstr(string, packet);
120         free(packet);
121     }
122 }
123
124 static void
125 print_note(struct ds *string, const struct nx_action_note *nan)
126 {
127     size_t len;
128     size_t i;
129
130     ds_put_cstr(string, "note:");
131     len = ntohs(nan->len) - offsetof(struct nx_action_note, note);
132     for (i = 0; i < len; i++) {
133         if (i) {
134             ds_put_char(string, '.');
135         }
136         ds_put_format(string, "%02"PRIx8, nan->note[i]);
137     }
138 }
139
140 static void
141 ofp_print_action(struct ds *s, const union ofp_action *a,
142                  enum ofputil_action_code code)
143 {
144     const struct ofp_action_enqueue *oae;
145     const struct ofp_action_dl_addr *oada;
146     const struct nx_action_set_tunnel64 *nast64;
147     const struct nx_action_set_tunnel *nast;
148     const struct nx_action_set_queue *nasq;
149     const struct nx_action_resubmit *nar;
150     const struct nx_action_reg_move *move;
151     const struct nx_action_reg_load *load;
152     const struct nx_action_multipath *nam;
153     const struct nx_action_autopath *naa;
154     const struct nx_action_output_reg *naor;
155     uint16_t port;
156
157     switch (code) {
158     case OFPUTIL_OFPAT_OUTPUT:
159         port = ntohs(a->output.port);
160         if (port < OFPP_MAX) {
161             ds_put_format(s, "output:%"PRIu16, port);
162         } else {
163             ofputil_format_port(port, s);
164             if (port == OFPP_CONTROLLER) {
165                 if (a->output.max_len != htons(0)) {
166                     ds_put_format(s, ":%"PRIu16, ntohs(a->output.max_len));
167                 } else {
168                     ds_put_cstr(s, ":all");
169                 }
170             }
171         }
172         break;
173
174     case OFPUTIL_OFPAT_ENQUEUE:
175         oae = (const struct ofp_action_enqueue *) a;
176         ds_put_format(s, "enqueue:");
177         ofputil_format_port(ntohs(oae->port), s);
178         ds_put_format(s, "q%"PRIu32, ntohl(oae->queue_id));
179         break;
180
181     case OFPUTIL_OFPAT_SET_VLAN_VID:
182         ds_put_format(s, "mod_vlan_vid:%"PRIu16,
183                       ntohs(a->vlan_vid.vlan_vid));
184         break;
185
186     case OFPUTIL_OFPAT_SET_VLAN_PCP:
187         ds_put_format(s, "mod_vlan_pcp:%"PRIu8, a->vlan_pcp.vlan_pcp);
188         break;
189
190     case OFPUTIL_OFPAT_STRIP_VLAN:
191         ds_put_cstr(s, "strip_vlan");
192         break;
193
194     case OFPUTIL_OFPAT_SET_DL_SRC:
195         oada = (const struct ofp_action_dl_addr *) a;
196         ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
197                       ETH_ADDR_ARGS(oada->dl_addr));
198         break;
199
200     case OFPUTIL_OFPAT_SET_DL_DST:
201         oada = (const struct ofp_action_dl_addr *) a;
202         ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
203                       ETH_ADDR_ARGS(oada->dl_addr));
204         break;
205
206     case OFPUTIL_OFPAT_SET_NW_SRC:
207         ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(&a->nw_addr.nw_addr));
208         break;
209
210     case OFPUTIL_OFPAT_SET_NW_DST:
211         ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(&a->nw_addr.nw_addr));
212         break;
213
214     case OFPUTIL_OFPAT_SET_NW_TOS:
215         ds_put_format(s, "mod_nw_tos:%d", a->nw_tos.nw_tos);
216         break;
217
218     case OFPUTIL_OFPAT_SET_TP_SRC:
219         ds_put_format(s, "mod_tp_src:%d", ntohs(a->tp_port.tp_port));
220         break;
221
222     case OFPUTIL_OFPAT_SET_TP_DST:
223         ds_put_format(s, "mod_tp_dst:%d", ntohs(a->tp_port.tp_port));
224         break;
225
226     case OFPUTIL_NXAST_RESUBMIT:
227         nar = (struct nx_action_resubmit *)a;
228         ds_put_format(s, "resubmit:");
229         ofputil_format_port(ntohs(nar->in_port), s);
230         break;
231
232     case OFPUTIL_NXAST_RESUBMIT_TABLE:
233         nar = (struct nx_action_resubmit *)a;
234         ds_put_format(s, "resubmit(");
235         if (nar->in_port != htons(OFPP_IN_PORT)) {
236             ofputil_format_port(ntohs(nar->in_port), s);
237         }
238         ds_put_char(s, ',');
239         if (nar->table != 255) {
240             ds_put_format(s, "%"PRIu8, nar->table);
241         }
242         ds_put_char(s, ')');
243         break;
244
245     case OFPUTIL_NXAST_SET_TUNNEL:
246         nast = (struct nx_action_set_tunnel *)a;
247         ds_put_format(s, "set_tunnel:%#"PRIx32, ntohl(nast->tun_id));
248         break;
249
250     case OFPUTIL_NXAST_SET_QUEUE:
251         nasq = (struct nx_action_set_queue *)a;
252         ds_put_format(s, "set_queue:%u", ntohl(nasq->queue_id));
253         break;
254
255     case OFPUTIL_NXAST_POP_QUEUE:
256         ds_put_cstr(s, "pop_queue");
257         break;
258
259     case OFPUTIL_NXAST_NOTE:
260         print_note(s, (const struct nx_action_note *) a);
261         break;
262
263     case OFPUTIL_NXAST_REG_MOVE:
264         move = (const struct nx_action_reg_move *) a;
265         nxm_format_reg_move(move, s);
266         break;
267
268     case OFPUTIL_NXAST_REG_LOAD:
269         load = (const struct nx_action_reg_load *) a;
270         nxm_format_reg_load(load, s);
271         break;
272
273     case OFPUTIL_NXAST_SET_TUNNEL64:
274         nast64 = (const struct nx_action_set_tunnel64 *) a;
275         ds_put_format(s, "set_tunnel64:%#"PRIx64,
276                       ntohll(nast64->tun_id));
277         break;
278
279     case OFPUTIL_NXAST_MULTIPATH:
280         nam = (const struct nx_action_multipath *) a;
281         multipath_format(nam, s);
282         break;
283
284     case OFPUTIL_NXAST_AUTOPATH:
285         naa = (const struct nx_action_autopath *)a;
286         ds_put_format(s, "autopath(%u,", ntohl(naa->id));
287         nxm_format_field_bits(s, ntohl(naa->dst),
288                               nxm_decode_ofs(naa->ofs_nbits),
289                               nxm_decode_n_bits(naa->ofs_nbits));
290         ds_put_char(s, ')');
291         break;
292
293     case OFPUTIL_NXAST_BUNDLE:
294     case OFPUTIL_NXAST_BUNDLE_LOAD:
295         bundle_format((const struct nx_action_bundle *) a, s);
296         break;
297
298     case OFPUTIL_NXAST_OUTPUT_REG:
299         naor = (const struct nx_action_output_reg *) a;
300         ds_put_cstr(s, "output:");
301         nxm_format_field_bits(s, ntohl(naor->src),
302                               nxm_decode_ofs(naor->ofs_nbits),
303                               nxm_decode_n_bits(naor->ofs_nbits));
304         break;
305
306     case OFPUTIL_NXAST_LEARN:
307         learn_format((const struct nx_action_learn *) a, s);
308         break;
309
310     case OFPUTIL_NXAST_EXIT:
311         ds_put_cstr(s, "exit");
312         break;
313
314     default:
315         break;
316     }
317 }
318
319 void
320 ofp_print_actions(struct ds *string, const union ofp_action *actions,
321                   size_t n_actions)
322 {
323     const union ofp_action *a;
324     size_t left;
325
326     ds_put_cstr(string, "actions=");
327     if (!n_actions) {
328         ds_put_cstr(string, "drop");
329     }
330
331     OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
332         int code = ofputil_decode_action(a);
333         if (code >= 0) {
334             if (a != actions) {
335                 ds_put_cstr(string, ",");
336             }
337             ofp_print_action(string, a, code);
338         } else {
339             ofp_print_error(string, -code);
340         }
341     }
342     if (left > 0) {
343         ds_put_format(string, " ***%zu leftover bytes following actions",
344                       left * sizeof *a);
345     }
346 }
347
348 static void
349 ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
350                      int verbosity)
351 {
352     size_t len = ntohs(opo->header.length);
353     size_t actions_len = ntohs(opo->actions_len);
354
355     ds_put_cstr(string, " in_port=");
356     ofputil_format_port(ntohs(opo->in_port), string);
357
358     ds_put_format(string, " actions_len=%zu ", actions_len);
359     if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
360         ds_put_format(string, "***packet too short for action length***\n");
361         return;
362     }
363     if (actions_len % sizeof(union ofp_action)) {
364         ds_put_format(string, "***action length not a multiple of %zu***\n",
365                       sizeof(union ofp_action));
366     }
367     ofp_print_actions(string, (const union ofp_action *) opo->actions,
368                       actions_len / sizeof(union ofp_action));
369
370     if (ntohl(opo->buffer_id) == UINT32_MAX) {
371         int data_len = len - sizeof *opo - actions_len;
372         ds_put_format(string, " data_len=%d", data_len);
373         if (verbosity > 0 && len > sizeof *opo) {
374             char *packet = ofp_packet_to_string(
375                     (uint8_t *) opo->actions + actions_len, data_len);
376             ds_put_char(string, '\n');
377             ds_put_cstr(string, packet);
378             free(packet);
379         }
380     } else {
381         ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
382     }
383     ds_put_char(string, '\n');
384 }
385
386 /* qsort comparison function. */
387 static int
388 compare_ports(const void *a_, const void *b_)
389 {
390     const struct ofp_phy_port *a = a_;
391     const struct ofp_phy_port *b = b_;
392     uint16_t ap = ntohs(a->port_no);
393     uint16_t bp = ntohs(b->port_no);
394
395     return ap < bp ? -1 : ap > bp;
396 }
397
398 struct bit_name {
399     uint32_t bit;
400     const char *name;
401 };
402
403 static void
404 ofp_print_bit_names(struct ds *string, uint32_t bits,
405                     const struct bit_name bit_names[])
406 {
407     int n = 0;
408
409     if (!bits) {
410         ds_put_cstr(string, "0");
411         return;
412     }
413
414     for (; bits && bit_names->name; bit_names++) {
415         if (bits & bit_names->bit) {
416             if (n++) {
417                 ds_put_char(string, ' ');
418             }
419             ds_put_cstr(string, bit_names->name);
420             bits &= ~bit_names->bit;
421         }
422     }
423
424     if (bits) {
425         if (n++) {
426             ds_put_char(string, ' ');
427         }
428         ds_put_format(string, "0x%"PRIx32, bits);
429     }
430 }
431
432 static void
433 ofp_print_port_features(struct ds *string, uint32_t features)
434 {
435     static const struct bit_name feature_bits[] = {
436         { OFPPF_10MB_HD,    "10MB-HD" },
437         { OFPPF_10MB_FD,    "10MB-FD" },
438         { OFPPF_100MB_HD,   "100MB-HD" },
439         { OFPPF_100MB_FD,   "100MB-FD" },
440         { OFPPF_1GB_HD,     "1GB-HD" },
441         { OFPPF_1GB_FD,     "1GB-FD" },
442         { OFPPF_10GB_FD,    "10GB-FD" },
443         { OFPPF_COPPER,     "COPPER" },
444         { OFPPF_FIBER,      "FIBER" },
445         { OFPPF_AUTONEG,    "AUTO_NEG" },
446         { OFPPF_PAUSE,      "AUTO_PAUSE" },
447         { OFPPF_PAUSE_ASYM, "AUTO_PAUSE_ASYM" },
448         { 0,                NULL },
449     };
450
451     ofp_print_bit_names(string, features, feature_bits);
452     ds_put_char(string, '\n');
453 }
454
455 static void
456 ofp_print_port_config(struct ds *string, uint32_t config)
457 {
458     static const struct bit_name config_bits[] = {
459         { OFPPC_PORT_DOWN,    "PORT_DOWN" },
460         { OFPPC_NO_STP,       "NO_STP" },
461         { OFPPC_NO_RECV,      "NO_RECV" },
462         { OFPPC_NO_RECV_STP,  "NO_RECV_STP" },
463         { OFPPC_NO_FLOOD,     "NO_FLOOD" },
464         { OFPPC_NO_FWD,       "NO_FWD" },
465         { OFPPC_NO_PACKET_IN, "NO_PACKET_IN" },
466         { 0,                  NULL },
467     };
468
469     ofp_print_bit_names(string, config, config_bits);
470     ds_put_char(string, '\n');
471 }
472
473 static void
474 ofp_print_port_state(struct ds *string, uint32_t state)
475 {
476     static const struct bit_name state_bits[] = {
477         { OFPPS_LINK_DOWN, "LINK_DOWN" },
478         { 0,               NULL },
479     };
480     uint32_t stp_state;
481
482     /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
483      * pattern.  We have to special case it.
484      *
485      * OVS doesn't support STP, so this field will always be 0 if we are
486      * talking to OVS, so we'd always print STP_LISTEN in that case.
487      * Therefore, we don't print anything at all if the value is STP_LISTEN, to
488      * avoid confusing users. */
489     stp_state = state & OFPPS_STP_MASK;
490     if (stp_state) {
491         ds_put_cstr(string, (stp_state == OFPPS_STP_LEARN ? "STP_LEARN"
492                              : stp_state == OFPPS_STP_FORWARD ? "STP_FORWARD"
493                              : "STP_BLOCK"));
494         state &= ~OFPPS_STP_MASK;
495         if (state) {
496             ofp_print_bit_names(string, state, state_bits);
497         }
498     } else {
499         ofp_print_bit_names(string, state, state_bits);
500     }
501     ds_put_char(string, '\n');
502 }
503
504 static void
505 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
506 {
507     char name[OFP_MAX_PORT_NAME_LEN];
508     int j;
509
510     memcpy(name, port->name, sizeof name);
511     for (j = 0; j < sizeof name - 1; j++) {
512         if (!isprint((unsigned char) name[j])) {
513             break;
514         }
515     }
516     name[j] = '\0';
517
518     ds_put_char(string, ' ');
519     ofputil_format_port(ntohs(port->port_no), string);
520     ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
521                   name, ETH_ADDR_ARGS(port->hw_addr));
522
523     ds_put_cstr(string, "     config:     ");
524     ofp_print_port_config(string, ntohl(port->config));
525
526     ds_put_cstr(string, "     state:      ");
527     ofp_print_port_state(string, ntohl(port->state));
528
529     if (port->curr) {
530         ds_put_format(string, "     current:    ");
531         ofp_print_port_features(string, ntohl(port->curr));
532     }
533     if (port->advertised) {
534         ds_put_format(string, "     advertised: ");
535         ofp_print_port_features(string, ntohl(port->advertised));
536     }
537     if (port->supported) {
538         ds_put_format(string, "     supported:  ");
539         ofp_print_port_features(string, ntohl(port->supported));
540     }
541     if (port->peer) {
542         ds_put_format(string, "     peer:       ");
543         ofp_print_port_features(string, ntohl(port->peer));
544     }
545 }
546
547 static void
548 ofp_print_switch_features(struct ds *string,
549                           const struct ofp_switch_features *osf)
550 {
551     size_t len = ntohs(osf->header.length);
552     struct ofp_phy_port *port_list;
553     int n_ports;
554     int i;
555
556     ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
557             osf->header.version, ntohll(osf->datapath_id));
558     ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
559             ntohl(osf->n_buffers));
560     ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
561            ntohl(osf->capabilities), ntohl(osf->actions));
562
563     n_ports = (len - sizeof *osf) / sizeof *osf->ports;
564
565     port_list = xmemdup(osf->ports, len - sizeof *osf);
566     qsort(port_list, n_ports, sizeof *port_list, compare_ports);
567     for (i = 0; i < n_ports; i++) {
568         ofp_print_phy_port(string, &port_list[i]);
569     }
570     free(port_list);
571 }
572
573 static void
574 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
575 {
576     uint16_t flags;
577
578     flags = ntohs(osc->flags);
579
580     ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
581     flags &= ~OFPC_FRAG_MASK;
582
583     if (flags) {
584         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
585     }
586
587     ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
588 }
589
590 static void print_wild(struct ds *string, const char *leader, int is_wild,
591             int verbosity, const char *format, ...)
592             __attribute__((format(printf, 5, 6)));
593
594 static void print_wild(struct ds *string, const char *leader, int is_wild,
595                        int verbosity, const char *format, ...)
596 {
597     if (is_wild && verbosity < 2) {
598         return;
599     }
600     ds_put_cstr(string, leader);
601     if (!is_wild) {
602         va_list args;
603
604         va_start(args, format);
605         ds_put_format_valist(string, format, args);
606         va_end(args);
607     } else {
608         ds_put_char(string, '*');
609     }
610     ds_put_char(string, ',');
611 }
612
613 static void
614 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
615                  uint32_t wild_bits, int verbosity)
616 {
617     if (wild_bits >= 32 && verbosity < 2) {
618         return;
619     }
620     ds_put_cstr(string, leader);
621     if (wild_bits < 32) {
622         ds_put_format(string, IP_FMT, IP_ARGS(&ip));
623         if (wild_bits) {
624             ds_put_format(string, "/%d", 32 - wild_bits);
625         }
626     } else {
627         ds_put_char(string, '*');
628     }
629     ds_put_char(string, ',');
630 }
631
632 void
633 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
634 {
635     char *s = ofp_match_to_string(om, verbosity);
636     ds_put_cstr(f, s);
637     free(s);
638 }
639
640 char *
641 ofp_match_to_string(const struct ofp_match *om, int verbosity)
642 {
643     struct ds f = DS_EMPTY_INITIALIZER;
644     uint32_t w = ntohl(om->wildcards);
645     bool skip_type = false;
646     bool skip_proto = false;
647
648     if (!(w & OFPFW_DL_TYPE)) {
649         skip_type = true;
650         if (om->dl_type == htons(ETH_TYPE_IP)) {
651             if (!(w & OFPFW_NW_PROTO)) {
652                 skip_proto = true;
653                 if (om->nw_proto == IPPROTO_ICMP) {
654                     ds_put_cstr(&f, "icmp,");
655                 } else if (om->nw_proto == IPPROTO_TCP) {
656                     ds_put_cstr(&f, "tcp,");
657                 } else if (om->nw_proto == IPPROTO_UDP) {
658                     ds_put_cstr(&f, "udp,");
659                 } else {
660                     ds_put_cstr(&f, "ip,");
661                     skip_proto = false;
662                 }
663             } else {
664                 ds_put_cstr(&f, "ip,");
665             }
666         } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
667             ds_put_cstr(&f, "arp,");
668         } else {
669             skip_type = false;
670         }
671     }
672     print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
673                "%d", ntohs(om->in_port));
674     print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
675                "%d", ntohs(om->dl_vlan));
676     print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
677                "%d", om->dl_vlan_pcp);
678     print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
679                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
680     print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
681                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
682     if (!skip_type) {
683         print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
684                    "0x%04x", ntohs(om->dl_type));
685     }
686     print_ip_netmask(&f, "nw_src=", om->nw_src,
687                      (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
688     print_ip_netmask(&f, "nw_dst=", om->nw_dst,
689                      (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
690     if (!skip_proto) {
691         if (om->dl_type == htons(ETH_TYPE_ARP)) {
692             print_wild(&f, "arp_op=", w & OFPFW_NW_PROTO, verbosity,
693                        "%u", om->nw_proto);
694         } else {
695             print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
696                        "%u", om->nw_proto);
697         }
698     }
699     print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
700                "%u", om->nw_tos);
701     if (om->nw_proto == IPPROTO_ICMP) {
702         print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
703                    "%d", ntohs(om->tp_src));
704         print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
705                    "%d", ntohs(om->tp_dst));
706     } else {
707         print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
708                    "%d", ntohs(om->tp_src));
709         print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
710                    "%d", ntohs(om->tp_dst));
711     }
712     if (ds_last(&f) == ',') {
713         f.length--;
714     }
715     return ds_cstr(&f);
716 }
717
718 static void
719 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
720                    enum ofputil_msg_code code, int verbosity)
721 {
722     struct ofputil_flow_mod fm;
723     bool need_priority;
724     int error;
725
726     error = ofputil_decode_flow_mod(&fm, oh, true);
727     if (error) {
728         ofp_print_error(s, error);
729         return;
730     }
731
732     ds_put_char(s, ' ');
733     switch (fm.command) {
734     case OFPFC_ADD:
735         ds_put_cstr(s, "ADD");
736         break;
737     case OFPFC_MODIFY:
738         ds_put_cstr(s, "MOD");
739         break;
740     case OFPFC_MODIFY_STRICT:
741         ds_put_cstr(s, "MOD_STRICT");
742         break;
743     case OFPFC_DELETE:
744         ds_put_cstr(s, "DEL");
745         break;
746     case OFPFC_DELETE_STRICT:
747         ds_put_cstr(s, "DEL_STRICT");
748         break;
749     default:
750         ds_put_format(s, "cmd:%d", fm.command);
751     }
752     if (fm.table_id != 0) {
753         ds_put_format(s, " table:%d", fm.table_id);
754     }
755
756     ds_put_char(s, ' ');
757     if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
758         const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
759         ofp_print_match(s, &ofm->match, verbosity);
760
761         /* ofp_print_match() doesn't print priority. */
762         need_priority = true;
763     } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
764         const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
765         const void *nxm = nfm + 1;
766         char *nxm_s;
767
768         nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
769         ds_put_cstr(s, nxm_s);
770         free(nxm_s);
771
772         /* nx_match_to_string() doesn't print priority. */
773         need_priority = true;
774     } else {
775         cls_rule_format(&fm.cr, s);
776
777         /* cls_rule_format() does print priority. */
778         need_priority = false;
779     }
780
781     if (ds_last(s) != ' ') {
782         ds_put_char(s, ' ');
783     }
784     if (fm.cookie != htonll(0)) {
785         ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.cookie));
786     }
787     if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
788         ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
789     }
790     if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
791         ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
792     }
793     if (fm.cr.priority != OFP_DEFAULT_PRIORITY && need_priority) {
794         ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
795     }
796     if (fm.buffer_id != UINT32_MAX) {
797         ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
798     }
799     if (fm.flags != 0) {
800         ds_put_format(s, "flags:0x%"PRIx16" ", fm.flags);
801     }
802
803     ofp_print_actions(s, fm.actions, fm.n_actions);
804 }
805
806 static void
807 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
808 {
809     ds_put_format(string, "%u", sec);
810     if (nsec > 0) {
811         ds_put_format(string, ".%09u", nsec);
812         while (string->string[string->length - 1] == '0') {
813             string->length--;
814         }
815     }
816     ds_put_char(string, 's');
817 }
818
819 static void
820 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
821 {
822     struct ofputil_flow_removed fr;
823     int error;
824
825     error = ofputil_decode_flow_removed(&fr, oh);
826     if (error) {
827         ofp_print_error(string, error);
828         return;
829     }
830
831     ds_put_char(string, ' ');
832     cls_rule_format(&fr.rule, string);
833
834     ds_put_cstr(string, " reason=");
835     switch (fr.reason) {
836     case OFPRR_IDLE_TIMEOUT:
837         ds_put_cstr(string, "idle");
838         break;
839     case OFPRR_HARD_TIMEOUT:
840         ds_put_cstr(string, "hard");
841         break;
842     case OFPRR_DELETE:
843         ds_put_cstr(string, "delete");
844         break;
845     default:
846         ds_put_format(string, "**%"PRIu8"**", fr.reason);
847         break;
848     }
849
850     if (fr.cookie != htonll(0)) {
851         ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
852     }
853     ds_put_cstr(string, " duration");
854     ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
855     ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
856          fr.idle_timeout, fr.packet_count, fr.byte_count);
857 }
858
859 static void
860 ofp_print_port_mod(struct ds *string, const struct ofp_port_mod *opm)
861 {
862     ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
863             ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
864             ntohl(opm->config), ntohl(opm->mask));
865     ds_put_format(string, "     advertise: ");
866     if (opm->advertise) {
867         ofp_print_port_features(string, ntohl(opm->advertise));
868     } else {
869         ds_put_format(string, "UNCHANGED\n");
870     }
871 }
872
873 static void
874 ofp_print_error(struct ds *string, int error)
875 {
876     if (string->length) {
877         ds_put_char(string, ' ');
878     }
879     ds_put_cstr(string, "***decode error: ");
880     ofputil_format_error(string, error);
881     ds_put_cstr(string, "***\n");
882 }
883
884 static void
885 ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
886 {
887     size_t len = ntohs(oem->header.length);
888     size_t payload_ofs, payload_len;
889     const void *payload;
890     int error;
891     char *s;
892
893     error = ofputil_decode_error_msg(&oem->header, &payload_ofs);
894     if (!is_ofp_error(error)) {
895         ofp_print_error(string, error);
896         ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
897         return;
898     }
899
900     ds_put_char(string, ' ');
901     ofputil_format_error(string, error);
902     ds_put_char(string, '\n');
903
904     payload = (const uint8_t *) oem + payload_ofs;
905     payload_len = len - payload_ofs;
906     switch (get_ofp_err_type(error)) {
907     case OFPET_HELLO_FAILED:
908         ds_put_printable(string, payload, payload_len);
909         break;
910
911     default:
912         s = ofp_to_string(payload, payload_len, 1);
913         ds_put_cstr(string, s);
914         free(s);
915         break;
916     }
917 }
918
919 static void
920 ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
921 {
922     if (ops->reason == OFPPR_ADD) {
923         ds_put_format(string, " ADD:");
924     } else if (ops->reason == OFPPR_DELETE) {
925         ds_put_format(string, " DEL:");
926     } else if (ops->reason == OFPPR_MODIFY) {
927         ds_put_format(string, " MOD:");
928     }
929
930     ofp_print_phy_port(string, &ops->desc);
931 }
932
933 static void
934 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_desc_stats *ods)
935 {
936     ds_put_char(string, '\n');
937     ds_put_format(string, "Manufacturer: %.*s\n",
938             (int) sizeof ods->mfr_desc, ods->mfr_desc);
939     ds_put_format(string, "Hardware: %.*s\n",
940             (int) sizeof ods->hw_desc, ods->hw_desc);
941     ds_put_format(string, "Software: %.*s\n",
942             (int) sizeof ods->sw_desc, ods->sw_desc);
943     ds_put_format(string, "Serial Num: %.*s\n",
944             (int) sizeof ods->serial_num, ods->serial_num);
945     ds_put_format(string, "DP Description: %.*s\n",
946             (int) sizeof ods->dp_desc, ods->dp_desc);
947 }
948
949 static void
950 ofp_print_flow_stats_request(struct ds *string,
951                              const struct ofp_stats_msg *osm)
952 {
953     struct ofputil_flow_stats_request fsr;
954     int error;
955
956     error = ofputil_decode_flow_stats_request(&fsr, &osm->header);
957     if (error) {
958         ofp_print_error(string, error);
959         return;
960     }
961
962     if (fsr.table_id != 0xff) {
963         ds_put_format(string, " table=%"PRIu8, fsr.table_id);
964     }
965
966     if (fsr.out_port != OFPP_NONE) {
967         ds_put_cstr(string, " out_port=");
968         ofputil_format_port(fsr.out_port, string);
969     }
970
971     /* A flow stats request doesn't include a priority, but cls_rule_format()
972      * will print one unless it is OFP_DEFAULT_PRIORITY. */
973     fsr.match.priority = OFP_DEFAULT_PRIORITY;
974
975     ds_put_char(string, ' ');
976     cls_rule_format(&fsr.match, string);
977 }
978
979 static void
980 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
981 {
982     struct ofpbuf b;
983
984     ofpbuf_use_const(&b, oh, ntohs(oh->length));
985     for (;;) {
986         struct ofputil_flow_stats fs;
987         int retval;
988
989         retval = ofputil_decode_flow_stats_reply(&fs, &b);
990         if (retval) {
991             if (retval != EOF) {
992                 ds_put_cstr(string, " ***parse error***");
993             }
994             break;
995         }
996
997         ds_put_char(string, '\n');
998
999         ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1000                       ntohll(fs.cookie));
1001         ofp_print_duration(string, fs.duration_sec, fs.duration_nsec);
1002         ds_put_format(string, ", table=%"PRIu8", ", fs.table_id);
1003         ds_put_format(string, "n_packets=%"PRIu64", ", fs.packet_count);
1004         ds_put_format(string, "n_bytes=%"PRIu64", ", fs.byte_count);
1005         if (fs.idle_timeout != OFP_FLOW_PERMANENT) {
1006             ds_put_format(string, "idle_timeout=%"PRIu16",", fs.idle_timeout);
1007         }
1008         if (fs.hard_timeout != OFP_FLOW_PERMANENT) {
1009             ds_put_format(string, "hard_timeout=%"PRIu16",", fs.hard_timeout);
1010         }
1011
1012         cls_rule_format(&fs.rule, string);
1013         if (string->string[string->length - 1] != ' ') {
1014             ds_put_char(string, ' ');
1015         }
1016         ofp_print_actions(string, fs.actions, fs.n_actions);
1017      }
1018 }
1019
1020 static void
1021 ofp_print_ofpst_aggregate_reply(struct ds *string,
1022                                 const struct ofp_aggregate_stats_reply *asr)
1023 {
1024     ds_put_format(string, " packet_count=%"PRIu64,
1025                   ntohll(get_32aligned_be64(&asr->packet_count)));
1026     ds_put_format(string, " byte_count=%"PRIu64,
1027                   ntohll(get_32aligned_be64(&asr->byte_count)));
1028     ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1029 }
1030
1031 static void
1032 ofp_print_nxst_aggregate_reply(struct ds *string,
1033                                const struct nx_aggregate_stats_reply *nasr)
1034 {
1035     ds_put_format(string, " packet_count=%"PRIu64, ntohll(nasr->packet_count));
1036     ds_put_format(string, " byte_count=%"PRIu64, ntohll(nasr->byte_count));
1037     ds_put_format(string, " flow_count=%"PRIu32, ntohl(nasr->flow_count));
1038 }
1039
1040 static void print_port_stat(struct ds *string, const char *leader,
1041                             const ovs_32aligned_be64 *statp, int more)
1042 {
1043     uint64_t stat = ntohll(get_32aligned_be64(statp));
1044
1045     ds_put_cstr(string, leader);
1046     if (stat != UINT64_MAX) {
1047         ds_put_format(string, "%"PRIu64, stat);
1048     } else {
1049         ds_put_char(string, '?');
1050     }
1051     if (more) {
1052         ds_put_cstr(string, ", ");
1053     } else {
1054         ds_put_cstr(string, "\n");
1055     }
1056 }
1057
1058 static void
1059 ofp_print_ofpst_port_request(struct ds *string,
1060                              const struct ofp_port_stats_request *psr)
1061 {
1062     ds_put_format(string, " port_no=%"PRIu16, ntohs(psr->port_no));
1063 }
1064
1065 static void
1066 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1067                            int verbosity)
1068 {
1069     const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1070     size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
1071     ds_put_format(string, " %zu ports\n", n);
1072     if (verbosity < 1) {
1073         return;
1074     }
1075
1076     for (; n--; ps++) {
1077         ds_put_format(string, "  port %2"PRIu16": ", ntohs(ps->port_no));
1078
1079         ds_put_cstr(string, "rx ");
1080         print_port_stat(string, "pkts=", &ps->rx_packets, 1);
1081         print_port_stat(string, "bytes=", &ps->rx_bytes, 1);
1082         print_port_stat(string, "drop=", &ps->rx_dropped, 1);
1083         print_port_stat(string, "errs=", &ps->rx_errors, 1);
1084         print_port_stat(string, "frame=", &ps->rx_frame_err, 1);
1085         print_port_stat(string, "over=", &ps->rx_over_err, 1);
1086         print_port_stat(string, "crc=", &ps->rx_crc_err, 0);
1087
1088         ds_put_cstr(string, "           tx ");
1089         print_port_stat(string, "pkts=", &ps->tx_packets, 1);
1090         print_port_stat(string, "bytes=", &ps->tx_bytes, 1);
1091         print_port_stat(string, "drop=", &ps->tx_dropped, 1);
1092         print_port_stat(string, "errs=", &ps->tx_errors, 1);
1093         print_port_stat(string, "coll=", &ps->collisions, 0);
1094     }
1095 }
1096
1097 static void
1098 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1099                             int verbosity)
1100 {
1101     const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1102     size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
1103     ds_put_format(string, " %zu tables\n", n);
1104     if (verbosity < 1) {
1105         return;
1106     }
1107
1108     for (; n--; ts++) {
1109         char name[OFP_MAX_TABLE_NAME_LEN + 1];
1110         ovs_strlcpy(name, ts->name, sizeof name);
1111
1112         ds_put_format(string, "  %d: %-8s: ", ts->table_id, name);
1113         ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1114         ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1115         ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1116         ds_put_cstr(string, "               ");
1117         ds_put_format(string, "lookup=%"PRIu64", ",
1118                       ntohll(get_32aligned_be64(&ts->lookup_count)));
1119         ds_put_format(string, "matched=%"PRIu64"\n",
1120                       ntohll(get_32aligned_be64(&ts->matched_count)));
1121      }
1122 }
1123
1124 static void
1125 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1126 {
1127     if (queue_id == OFPQ_ALL) {
1128         ds_put_cstr(string, "ALL");
1129     } else {
1130         ds_put_format(string, "%"PRIu32, queue_id);
1131     }
1132 }
1133
1134 static void
1135 ofp_print_ofpst_queue_request(struct ds *string,
1136                               const struct ofp_queue_stats_request *qsr)
1137 {
1138     ds_put_cstr(string, "port=");
1139     ofputil_format_port(ntohs(qsr->port_no), string);
1140
1141     ds_put_cstr(string, " queue=");
1142     ofp_print_queue_name(string, ntohl(qsr->queue_id));
1143 }
1144
1145 static void
1146 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1147                             int verbosity)
1148 {
1149     const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1150     size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
1151     ds_put_format(string, " %zu queues\n", n);
1152     if (verbosity < 1) {
1153         return;
1154     }
1155
1156     for (; n--; qs++) {
1157         ds_put_cstr(string, "  port ");
1158         ofputil_format_port(ntohs(qs->port_no), string);
1159         ds_put_cstr(string, " queue ");
1160         ofp_print_queue_name(string, ntohl(qs->queue_id));
1161         ds_put_cstr(string, ": ");
1162
1163         print_port_stat(string, "bytes=", &qs->tx_bytes, 1);
1164         print_port_stat(string, "pkts=", &qs->tx_packets, 1);
1165         print_port_stat(string, "errors=", &qs->tx_errors, 0);
1166     }
1167 }
1168
1169 static void
1170 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1171 {
1172     const struct ofp_stats_msg *srq = (const struct ofp_stats_msg *) oh;
1173
1174     if (srq->flags) {
1175         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1176                       ntohs(srq->flags));
1177     }
1178 }
1179
1180 static void
1181 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1182 {
1183     const struct ofp_stats_msg *srp = (const struct ofp_stats_msg *) oh;
1184
1185     if (srp->flags) {
1186         uint16_t flags = ntohs(srp->flags);
1187
1188         ds_put_cstr(string, " flags=");
1189         if (flags & OFPSF_REPLY_MORE) {
1190             ds_put_cstr(string, "[more]");
1191             flags &= ~OFPSF_REPLY_MORE;
1192         }
1193         if (flags) {
1194             ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1195                           flags);
1196         }
1197     }
1198 }
1199
1200 static void
1201 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1202 {
1203     size_t len = ntohs(oh->length);
1204
1205     ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1206     if (verbosity > 1) {
1207         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1208     }
1209 }
1210
1211 static void
1212 ofp_print_nxt_role_message(struct ds *string,
1213                            const struct nx_role_request *nrr)
1214 {
1215     unsigned int role = ntohl(nrr->role);
1216
1217     ds_put_cstr(string, " role=");
1218     if (role == NX_ROLE_OTHER) {
1219         ds_put_cstr(string, "other");
1220     } else if (role == NX_ROLE_MASTER) {
1221         ds_put_cstr(string, "master");
1222     } else if (role == NX_ROLE_SLAVE) {
1223         ds_put_cstr(string, "slave");
1224     } else {
1225         ds_put_format(string, "%u", role);
1226     }
1227 }
1228
1229 static void
1230 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1231                                 const struct nxt_flow_mod_table_id *nfmti)
1232 {
1233     ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1234 }
1235
1236 static void
1237 ofp_print_nxt_set_flow_format(struct ds *string,
1238                               const struct nxt_set_flow_format *nsff)
1239 {
1240     uint32_t format = ntohl(nsff->format);
1241
1242     ds_put_cstr(string, " format=");
1243     if (ofputil_flow_format_is_valid(format)) {
1244         ds_put_cstr(string, ofputil_flow_format_to_string(format));
1245     } else {
1246         ds_put_format(string, "%"PRIu32, format);
1247     }
1248 }
1249
1250 static void
1251 ofp_to_string__(const struct ofp_header *oh,
1252                 const struct ofputil_msg_type *type, struct ds *string,
1253                 int verbosity)
1254 {
1255     enum ofputil_msg_code code;
1256     const void *msg = oh;
1257
1258     ds_put_format(string, "%s (xid=0x%"PRIx32"):",
1259                   ofputil_msg_type_name(type), ntohl(oh->xid));
1260
1261     code = ofputil_msg_type_code(type);
1262     switch (code) {
1263     case OFPUTIL_MSG_INVALID:
1264         break;
1265
1266     case OFPUTIL_OFPT_HELLO:
1267         ds_put_char(string, '\n');
1268         ds_put_hex_dump(string, oh + 1, ntohs(oh->length) - sizeof *oh,
1269                         0, true);
1270         break;
1271
1272     case OFPUTIL_OFPT_ERROR:
1273         ofp_print_error_msg(string, msg);
1274         break;
1275
1276     case OFPUTIL_OFPT_ECHO_REQUEST:
1277     case OFPUTIL_OFPT_ECHO_REPLY:
1278         ofp_print_echo(string, oh, verbosity);
1279         break;
1280
1281     case OFPUTIL_OFPT_FEATURES_REQUEST:
1282         break;
1283
1284     case OFPUTIL_OFPT_FEATURES_REPLY:
1285         ofp_print_switch_features(string, msg);
1286         break;
1287
1288     case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1289         break;
1290
1291     case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1292     case OFPUTIL_OFPT_SET_CONFIG:
1293         ofp_print_switch_config(string, msg);
1294         break;
1295
1296     case OFPUTIL_OFPT_PACKET_IN:
1297         ofp_print_packet_in(string, msg, verbosity);
1298         break;
1299
1300     case OFPUTIL_OFPT_FLOW_REMOVED:
1301     case OFPUTIL_NXT_FLOW_REMOVED:
1302         ofp_print_flow_removed(string, msg);
1303         break;
1304
1305     case OFPUTIL_OFPT_PORT_STATUS:
1306         ofp_print_port_status(string, msg);
1307         break;
1308
1309     case OFPUTIL_OFPT_PACKET_OUT:
1310         ofp_print_packet_out(string, msg, verbosity);
1311         break;
1312
1313     case OFPUTIL_OFPT_FLOW_MOD:
1314         ofp_print_flow_mod(string, msg, code, verbosity);
1315         break;
1316
1317     case OFPUTIL_OFPT_PORT_MOD:
1318         ofp_print_port_mod(string, msg);
1319         break;
1320
1321     case OFPUTIL_OFPT_BARRIER_REQUEST:
1322     case OFPUTIL_OFPT_BARRIER_REPLY:
1323         break;
1324
1325     case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1326     case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1327         /* XXX */
1328         break;
1329
1330     case OFPUTIL_OFPST_DESC_REQUEST:
1331         ofp_print_stats_request(string, oh);
1332         break;
1333
1334     case OFPUTIL_OFPST_FLOW_REQUEST:
1335     case OFPUTIL_NXST_FLOW_REQUEST:
1336     case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1337     case OFPUTIL_NXST_AGGREGATE_REQUEST:
1338         ofp_print_stats_request(string, oh);
1339         ofp_print_flow_stats_request(string, msg);
1340         break;
1341
1342     case OFPUTIL_OFPST_TABLE_REQUEST:
1343         ofp_print_stats_request(string, oh);
1344         break;
1345
1346     case OFPUTIL_OFPST_PORT_REQUEST:
1347         ofp_print_stats_request(string, oh);
1348         ofp_print_ofpst_port_request(string, msg);
1349         break;
1350
1351     case OFPUTIL_OFPST_QUEUE_REQUEST:
1352         ofp_print_stats_request(string, oh);
1353         ofp_print_ofpst_queue_request(string, msg);
1354         break;
1355
1356     case OFPUTIL_OFPST_DESC_REPLY:
1357         ofp_print_stats_reply(string, oh);
1358         ofp_print_ofpst_desc_reply(string, msg);
1359         break;
1360
1361     case OFPUTIL_OFPST_FLOW_REPLY:
1362     case OFPUTIL_NXST_FLOW_REPLY:
1363         ofp_print_stats_reply(string, oh);
1364         ofp_print_flow_stats_reply(string, oh);
1365         break;
1366
1367     case OFPUTIL_OFPST_QUEUE_REPLY:
1368         ofp_print_stats_reply(string, oh);
1369         ofp_print_ofpst_queue_reply(string, oh, verbosity);
1370         break;
1371
1372     case OFPUTIL_OFPST_PORT_REPLY:
1373         ofp_print_stats_reply(string, oh);
1374         ofp_print_ofpst_port_reply(string, oh, verbosity);
1375         break;
1376
1377     case OFPUTIL_OFPST_TABLE_REPLY:
1378         ofp_print_stats_reply(string, oh);
1379         ofp_print_ofpst_table_reply(string, oh, verbosity);
1380         break;
1381
1382     case OFPUTIL_OFPST_AGGREGATE_REPLY:
1383         ofp_print_stats_reply(string, oh);
1384         ofp_print_ofpst_aggregate_reply(string, msg);
1385         break;
1386
1387     case OFPUTIL_NXT_ROLE_REQUEST:
1388     case OFPUTIL_NXT_ROLE_REPLY:
1389         ofp_print_nxt_role_message(string, msg);
1390         break;
1391
1392     case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
1393         ofp_print_nxt_flow_mod_table_id(string, msg);
1394         break;
1395
1396     case OFPUTIL_NXT_SET_FLOW_FORMAT:
1397         ofp_print_nxt_set_flow_format(string, msg);
1398         break;
1399
1400     case OFPUTIL_NXT_FLOW_MOD:
1401         ofp_print_flow_mod(string, msg, code, verbosity);
1402         break;
1403
1404     case OFPUTIL_NXST_AGGREGATE_REPLY:
1405         ofp_print_stats_reply(string, oh);
1406         ofp_print_nxst_aggregate_reply(string, msg);
1407         break;
1408     }
1409 }
1410
1411 /* Composes and returns a string representing the OpenFlow packet of 'len'
1412  * bytes at 'oh' at the given 'verbosity' level.  0 is a minimal amount of
1413  * verbosity and higher numbers increase verbosity.  The caller is responsible
1414  * for freeing the string. */
1415 char *
1416 ofp_to_string(const void *oh_, size_t len, int verbosity)
1417 {
1418     struct ds string = DS_EMPTY_INITIALIZER;
1419     const struct ofp_header *oh = oh_;
1420
1421     if (!len) {
1422         ds_put_cstr(&string, "OpenFlow message is empty\n");
1423     } else if (len < sizeof(struct ofp_header)) {
1424         ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
1425                       len);
1426     } else if (oh->version != OFP_VERSION) {
1427         ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n",
1428                       oh->version);
1429     } else if (ntohs(oh->length) > len) {
1430         ds_put_format(&string,
1431                       "(***truncated to %zu bytes from %"PRIu16"***)\n",
1432                       len, ntohs(oh->length));
1433     } else if (ntohs(oh->length) < len) {
1434         ds_put_format(&string,
1435                       "(***only uses %"PRIu16" bytes out of %zu***)\n",
1436                       ntohs(oh->length), len);
1437     } else {
1438         const struct ofputil_msg_type *type;
1439         int error;
1440
1441         error = ofputil_decode_msg_type(oh, &type);
1442         if (!error) {
1443             ofp_to_string__(oh, type, &string, verbosity);
1444             if (verbosity >= 5) {
1445                 if (ds_last(&string) != '\n') {
1446                     ds_put_char(&string, '\n');
1447                 }
1448                 ds_put_hex_dump(&string, oh, len, 0, true);
1449             }
1450             if (ds_last(&string) != '\n') {
1451                 ds_put_char(&string, '\n');
1452             }
1453             return ds_steal_cstr(&string);
1454         }
1455
1456         ofp_print_error(&string, error);
1457     }
1458     ds_put_hex_dump(&string, oh, len, 0, true);
1459     return ds_steal_cstr(&string);
1460 }
1461
1462 /* Returns the name for the specified OpenFlow message type as a string,
1463  * e.g. "OFPT_FEATURES_REPLY".  If no name is known, the string returned is a
1464  * hex number, e.g. "0x55".
1465  *
1466  * The caller must free the returned string when it is no longer needed. */
1467 char *
1468 ofp_message_type_to_string(uint8_t type)
1469 {
1470     const char *name;
1471
1472     switch (type) {
1473     case OFPT_HELLO:
1474         name = "HELLO";
1475         break;
1476     case OFPT_ERROR:
1477         name = "ERROR";
1478         break;
1479     case OFPT_ECHO_REQUEST:
1480         name = "ECHO_REQUEST";
1481         break;
1482     case OFPT_ECHO_REPLY:
1483         name = "ECHO_REPLY";
1484         break;
1485     case OFPT_VENDOR:
1486         name = "VENDOR";
1487         break;
1488     case OFPT_FEATURES_REQUEST:
1489         name = "FEATURES_REQUEST";
1490         break;
1491     case OFPT_FEATURES_REPLY:
1492         name = "FEATURES_REPLY";
1493         break;
1494     case OFPT_GET_CONFIG_REQUEST:
1495         name = "GET_CONFIG_REQUEST";
1496         break;
1497     case OFPT_GET_CONFIG_REPLY:
1498         name = "GET_CONFIG_REPLY";
1499         break;
1500     case OFPT_SET_CONFIG:
1501         name = "SET_CONFIG";
1502         break;
1503     case OFPT_PACKET_IN:
1504         name = "PACKET_IN";
1505         break;
1506     case OFPT_FLOW_REMOVED:
1507         name = "FLOW_REMOVED";
1508         break;
1509     case OFPT_PORT_STATUS:
1510         name = "PORT_STATUS";
1511         break;
1512     case OFPT_PACKET_OUT:
1513         name = "PACKET_OUT";
1514         break;
1515     case OFPT_FLOW_MOD:
1516         name = "FLOW_MOD";
1517         break;
1518     case OFPT_PORT_MOD:
1519         name = "PORT_MOD";
1520         break;
1521     case OFPT_STATS_REQUEST:
1522         name = "STATS_REQUEST";
1523         break;
1524     case OFPT_STATS_REPLY:
1525         name = "STATS_REPLY";
1526         break;
1527     case OFPT_BARRIER_REQUEST:
1528         name = "BARRIER_REQUEST";
1529         break;
1530     case OFPT_BARRIER_REPLY:
1531         name = "BARRIER_REPLY";
1532         break;
1533     case OFPT_QUEUE_GET_CONFIG_REQUEST:
1534         name = "QUEUE_GET_CONFIG_REQUEST";
1535         break;
1536     case OFPT_QUEUE_GET_CONFIG_REPLY:
1537         name = "QUEUE_GET_CONFIG_REPLY";
1538         break;
1539     default:
1540         name = NULL;
1541         break;
1542     }
1543
1544     return name ? xasprintf("OFPT_%s", name) : xasprintf("0x%02"PRIx8, type);
1545 }
1546 \f
1547 static void
1548 print_and_free(FILE *stream, char *string)
1549 {
1550     fputs(string, stream);
1551     free(string);
1552 }
1553
1554 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1555  * given 'verbosity' level.  0 is a minimal amount of verbosity and higher
1556  * numbers increase verbosity. */
1557 void
1558 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1559 {
1560     print_and_free(stream, ofp_to_string(oh, len, verbosity));
1561 }
1562
1563 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1564  * 'data' to 'stream'. */
1565 void
1566 ofp_print_packet(FILE *stream, const void *data, size_t len)
1567 {
1568     print_and_free(stream, ofp_packet_to_string(data, len));
1569 }