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