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