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