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