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