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