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