Format tunnel IDs consistently.
[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 "ofp-util.h"
34 #include "ofpbuf.h"
35 #include "openflow/openflow.h"
36 #include "openflow/nicira-ext.h"
37 #include "packets.h"
38 #include "pcap.h"
39 #include "util.h"
40
41 static void ofp_print_port_name(struct ds *string, uint16_t port);
42 static void ofp_print_queue_name(struct ds *string, uint32_t port);
43
44 /* Returns a string that represents the contents of the Ethernet frame in the
45  * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
46  * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
47  * bytes were captured).
48  *
49  * The caller must free the returned string.
50  *
51  * This starts and kills a tcpdump subprocess so it's quite expensive. */
52 char *
53 ofp_packet_to_string(const void *data, size_t len, size_t total_len OVS_UNUSED)
54 {
55     struct ds ds = DS_EMPTY_INITIALIZER;
56     struct ofpbuf buf;
57
58     char command[128];
59     FILE *pcap;
60     FILE *tcpdump;
61     int status;
62     int c;
63
64     buf.data = (void *) data;
65     buf.size = len;
66
67     pcap = tmpfile();
68     if (!pcap) {
69         ovs_error(errno, "tmpfile");
70         return xstrdup("<error>");
71     }
72     pcap_write_header(pcap);
73     pcap_write(pcap, &buf);
74     fflush(pcap);
75     if (ferror(pcap)) {
76         ovs_error(errno, "error writing temporary file");
77     }
78     rewind(pcap);
79
80     snprintf(command, sizeof command, "/usr/sbin/tcpdump -e -n -r /dev/fd/%d 2>/dev/null",
81              fileno(pcap));
82     tcpdump = popen(command, "r");
83     fclose(pcap);
84     if (!tcpdump) {
85         ovs_error(errno, "exec(\"%s\")", command);
86         return xstrdup("<error>");
87     }
88
89     while ((c = getc(tcpdump)) != EOF) {
90         ds_put_char(&ds, c);
91     }
92
93     status = pclose(tcpdump);
94     if (WIFEXITED(status)) {
95         if (WEXITSTATUS(status))
96             ovs_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
97     } else if (WIFSIGNALED(status)) {
98         ovs_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
99     }
100     return ds_cstr(&ds);
101 }
102
103 /* Pretty-print the OFPT_PACKET_IN packet of 'len' bytes at 'oh' to 'stream'
104  * at the given 'verbosity' level. */
105 static void
106 ofp_packet_in(struct ds *string, const void *oh, size_t len, int verbosity)
107 {
108     const struct ofp_packet_in *op = oh;
109     size_t data_len;
110
111     ds_put_format(string, " total_len=%"PRIu16" in_port=",
112                   ntohs(op->total_len));
113     ofp_print_port_name(string, ntohs(op->in_port));
114
115     if (op->reason == OFPR_ACTION)
116         ds_put_cstr(string, " (via action)");
117     else if (op->reason != OFPR_NO_MATCH)
118         ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
119
120     data_len = len - offsetof(struct ofp_packet_in, data);
121     ds_put_format(string, " data_len=%zu", data_len);
122     if (htonl(op->buffer_id) == UINT32_MAX) {
123         ds_put_format(string, " (unbuffered)");
124         if (ntohs(op->total_len) != data_len)
125             ds_put_format(string, " (***total_len != data_len***)");
126     } else {
127         ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
128         if (ntohs(op->total_len) < data_len)
129             ds_put_format(string, " (***total_len < data_len***)");
130     }
131     ds_put_char(string, '\n');
132
133     if (verbosity > 0) {
134         struct flow flow;
135         struct ofpbuf packet;
136
137         packet.data = (void *) op->data;
138         packet.size = 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 /* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'string'
480  * at the given 'verbosity' level. */
481 static void ofp_packet_out(struct ds *string, const void *oh, size_t len,
482                            int verbosity)
483 {
484     const struct ofp_packet_out *opo = oh;
485     size_t actions_len = ntohs(opo->actions_len);
486
487     ds_put_cstr(string, " in_port=");
488     ofp_print_port_name(string, ntohs(opo->in_port));
489
490     ds_put_format(string, " actions_len=%zu ", actions_len);
491     if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
492         ds_put_format(string, "***packet too short for action length***\n");
493         return;
494     }
495     ofp_print_actions(string, opo->actions, actions_len);
496
497     if (ntohl(opo->buffer_id) == UINT32_MAX) {
498         int data_len = len - sizeof *opo - actions_len;
499         ds_put_format(string, " data_len=%d", data_len);
500         if (verbosity > 0 && len > sizeof *opo) {
501             char *packet = ofp_packet_to_string(
502                     (uint8_t *)opo->actions + actions_len, data_len, data_len);
503             ds_put_char(string, '\n');
504             ds_put_cstr(string, packet);
505             free(packet);
506         }
507     } else {
508         ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
509     }
510     ds_put_char(string, '\n');
511 }
512
513 /* qsort comparison function. */
514 static int
515 compare_ports(const void *a_, const void *b_)
516 {
517     const struct ofp_phy_port *a = a_;
518     const struct ofp_phy_port *b = b_;
519     uint16_t ap = ntohs(a->port_no);
520     uint16_t bp = ntohs(b->port_no);
521
522     return ap < bp ? -1 : ap > bp;
523 }
524
525 static void ofp_print_port_features(struct ds *string, uint32_t features)
526 {
527     if (features == 0) {
528         ds_put_cstr(string, "Unsupported\n");
529         return;
530     }
531     if (features & OFPPF_10MB_HD) {
532         ds_put_cstr(string, "10MB-HD ");
533     }
534     if (features & OFPPF_10MB_FD) {
535         ds_put_cstr(string, "10MB-FD ");
536     }
537     if (features & OFPPF_100MB_HD) {
538         ds_put_cstr(string, "100MB-HD ");
539     }
540     if (features & OFPPF_100MB_FD) {
541         ds_put_cstr(string, "100MB-FD ");
542     }
543     if (features & OFPPF_1GB_HD) {
544         ds_put_cstr(string, "1GB-HD ");
545     }
546     if (features & OFPPF_1GB_FD) {
547         ds_put_cstr(string, "1GB-FD ");
548     }
549     if (features & OFPPF_10GB_FD) {
550         ds_put_cstr(string, "10GB-FD ");
551     }
552     if (features & OFPPF_COPPER) {
553         ds_put_cstr(string, "COPPER ");
554     }
555     if (features & OFPPF_FIBER) {
556         ds_put_cstr(string, "FIBER ");
557     }
558     if (features & OFPPF_AUTONEG) {
559         ds_put_cstr(string, "AUTO_NEG ");
560     }
561     if (features & OFPPF_PAUSE) {
562         ds_put_cstr(string, "AUTO_PAUSE ");
563     }
564     if (features & OFPPF_PAUSE_ASYM) {
565         ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
566     }
567     ds_put_char(string, '\n');
568 }
569
570 static void
571 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
572 {
573     char name[OFP_MAX_PORT_NAME_LEN];
574     int j;
575
576     memcpy(name, port->name, sizeof name);
577     for (j = 0; j < sizeof name - 1; j++) {
578         if (!isprint(name[j])) {
579             break;
580         }
581     }
582     name[j] = '\0';
583
584     ds_put_char(string, ' ');
585     ofp_print_port_name(string, ntohs(port->port_no));
586     ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
587             name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
588             ntohl(port->state));
589     if (port->curr) {
590         ds_put_format(string, "     current:    ");
591         ofp_print_port_features(string, ntohl(port->curr));
592     }
593     if (port->advertised) {
594         ds_put_format(string, "     advertised: ");
595         ofp_print_port_features(string, ntohl(port->advertised));
596     }
597     if (port->supported) {
598         ds_put_format(string, "     supported:  ");
599         ofp_print_port_features(string, ntohl(port->supported));
600     }
601     if (port->peer) {
602         ds_put_format(string, "     peer:       ");
603         ofp_print_port_features(string, ntohl(port->peer));
604     }
605 }
606
607 /* Pretty-print the struct ofp_switch_features of 'len' bytes at 'oh' to
608  * 'string' at the given 'verbosity' level. */
609 static void
610 ofp_print_switch_features(struct ds *string, const void *oh, size_t len,
611                           int verbosity OVS_UNUSED)
612 {
613     const struct ofp_switch_features *osf = oh;
614     struct ofp_phy_port *port_list;
615     int n_ports;
616     int i;
617
618     ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
619             osf->header.version, ntohll(osf->datapath_id));
620     ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
621             ntohl(osf->n_buffers));
622     ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
623            ntohl(osf->capabilities), ntohl(osf->actions));
624
625     if (ntohs(osf->header.length) >= sizeof *osf) {
626         len = MIN(len, ntohs(osf->header.length));
627     }
628     n_ports = (len - sizeof *osf) / sizeof *osf->ports;
629
630     port_list = xmemdup(osf->ports, len - sizeof *osf);
631     qsort(port_list, n_ports, sizeof *port_list, compare_ports);
632     for (i = 0; i < n_ports; i++) {
633         ofp_print_phy_port(string, &port_list[i]);
634     }
635     free(port_list);
636 }
637
638 /* Pretty-print the struct ofp_switch_config of 'len' bytes at 'oh' to 'string'
639  * at the given 'verbosity' level. */
640 static void
641 ofp_print_switch_config(struct ds *string, const void *oh,
642                         size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
643 {
644     const struct ofp_switch_config *osc = oh;
645     uint16_t flags;
646
647     flags = ntohs(osc->flags);
648     if (flags) {
649         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
650     }
651
652     ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
653 }
654
655 static void print_wild(struct ds *string, const char *leader, int is_wild,
656             int verbosity, const char *format, ...)
657             __attribute__((format(printf, 5, 6)));
658
659 static void print_wild(struct ds *string, const char *leader, int is_wild,
660                        int verbosity, const char *format, ...)
661 {
662     if (is_wild && verbosity < 2) {
663         return;
664     }
665     ds_put_cstr(string, leader);
666     if (!is_wild) {
667         va_list args;
668
669         va_start(args, format);
670         ds_put_format_valist(string, format, args);
671         va_end(args);
672     } else {
673         ds_put_char(string, '*');
674     }
675     ds_put_char(string, ',');
676 }
677
678 static void
679 print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
680                  uint32_t wild_bits, int verbosity)
681 {
682     if (wild_bits >= 32 && verbosity < 2) {
683         return;
684     }
685     ds_put_cstr(string, leader);
686     if (wild_bits < 32) {
687         ds_put_format(string, IP_FMT, IP_ARGS(&ip));
688         if (wild_bits) {
689             ds_put_format(string, "/%d", 32 - wild_bits);
690         }
691     } else {
692         ds_put_char(string, '*');
693     }
694     ds_put_char(string, ',');
695 }
696
697 void
698 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
699 {
700     char *s = ofp_match_to_string(om, verbosity);
701     ds_put_cstr(f, s);
702     free(s);
703 }
704
705 char *
706 ofp_match_to_string(const struct ofp_match *om, int verbosity)
707 {
708     struct ds f = DS_EMPTY_INITIALIZER;
709     uint32_t w = ntohl(om->wildcards);
710     bool skip_type = false;
711     bool skip_proto = false;
712
713     if (!(w & OFPFW_DL_TYPE)) {
714         skip_type = true;
715         if (om->dl_type == htons(ETH_TYPE_IP)) {
716             if (!(w & OFPFW_NW_PROTO)) {
717                 skip_proto = true;
718                 if (om->nw_proto == IP_TYPE_ICMP) {
719                     ds_put_cstr(&f, "icmp,");
720                 } else if (om->nw_proto == IP_TYPE_TCP) {
721                     ds_put_cstr(&f, "tcp,");
722                 } else if (om->nw_proto == IP_TYPE_UDP) {
723                     ds_put_cstr(&f, "udp,");
724                 } else {
725                     ds_put_cstr(&f, "ip,");
726                     skip_proto = false;
727                 }
728             } else {
729                 ds_put_cstr(&f, "ip,");
730             }
731         } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
732             ds_put_cstr(&f, "arp,");
733         } else {
734             skip_type = false;
735         }
736     }
737     if (w & NXFW_TUN_ID) {
738         ds_put_cstr(&f, "tun_id_wild,");
739     }
740     print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
741                "%d", ntohs(om->in_port));
742     print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
743                "%d", ntohs(om->dl_vlan));
744     print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
745                "%d", om->dl_vlan_pcp);
746     print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
747                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
748     print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
749                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
750     if (!skip_type) {
751         print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
752                    "0x%04x", ntohs(om->dl_type));
753     }
754     print_ip_netmask(&f, "nw_src=", om->nw_src,
755                      (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
756     print_ip_netmask(&f, "nw_dst=", om->nw_dst,
757                      (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
758     if (!skip_proto) {
759         if (om->dl_type == htons(ETH_TYPE_ARP)) {
760             print_wild(&f, "opcode=", w & OFPFW_NW_PROTO, verbosity,
761                        "%u", om->nw_proto);
762         } else {
763             print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
764                        "%u", om->nw_proto);
765         }
766     }
767     print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
768                "%u", om->nw_tos);
769     if (om->nw_proto == IP_TYPE_ICMP) {
770         print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
771                    "%d", ntohs(om->icmp_type));
772         print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
773                    "%d", ntohs(om->icmp_code));
774     } else {
775         print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
776                    "%d", ntohs(om->tp_src));
777         print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
778                    "%d", ntohs(om->tp_dst));
779     }
780     return ds_cstr(&f);
781 }
782
783 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
784  * at the given 'verbosity' level. */
785 static void
786 ofp_print_flow_mod(struct ds *string, const void *oh, size_t len,
787                    int verbosity)
788 {
789     const struct ofp_flow_mod *ofm = oh;
790
791     ds_put_char(string, ' ');
792     ofp_print_match(string, &ofm->match, verbosity);
793     if (ds_last(string) != ' ') {
794         ds_put_char(string, ' ');
795     }
796
797     switch (ntohs(ofm->command)) {
798     case OFPFC_ADD:
799         ds_put_cstr(string, "ADD:");
800         break;
801     case OFPFC_MODIFY:
802         ds_put_cstr(string, "MOD:");
803         break;
804     case OFPFC_MODIFY_STRICT:
805         ds_put_cstr(string, "MOD_STRICT:");
806         break;
807     case OFPFC_DELETE:
808         ds_put_cstr(string, "DEL:");
809         break;
810     case OFPFC_DELETE_STRICT:
811         ds_put_cstr(string, "DEL_STRICT:");
812         break;
813     default:
814         ds_put_format(string, "cmd:%d", ntohs(ofm->command));
815     }
816     if (ofm->cookie != htonll(0)) {
817         ds_put_format(string, " cookie:0x%"PRIx64, ntohll(ofm->cookie));
818     }
819     if (ofm->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
820         ds_put_format(string, " idle:%d", ntohs(ofm->idle_timeout));
821     }
822     if (ofm->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
823         ds_put_format(string, " hard:%d", ntohs(ofm->hard_timeout));
824     }
825     if (ofm->priority != htons(32768)) {
826         ds_put_format(string, " pri:%"PRIu16, ntohs(ofm->priority));
827     }
828     if (ofm->buffer_id != htonl(UINT32_MAX)) {
829         ds_put_format(string, " buf:%#"PRIx32, ntohl(ofm->buffer_id));
830     }
831     if (ofm->flags != htons(0)) {
832         ds_put_format(string, " flags:%"PRIx16, ntohs(ofm->flags));
833     }
834     ds_put_cstr(string, " ");
835     ofp_print_actions(string, ofm->actions,
836                       len - offsetof(struct ofp_flow_mod, actions));
837     ds_put_char(string, '\n');
838 }
839
840 /* Pretty-print the OFPT_FLOW_REMOVED packet of 'len' bytes at 'oh' to 'string'
841  * at the given 'verbosity' level. */
842 static void
843 ofp_print_flow_removed(struct ds *string, const void *oh,
844                        size_t len OVS_UNUSED, int verbosity)
845 {
846     const struct ofp_flow_removed *ofr = oh;
847
848     ofp_print_match(string, &ofr->match, verbosity);
849     ds_put_cstr(string, " reason=");
850     switch (ofr->reason) {
851     case OFPRR_IDLE_TIMEOUT:
852         ds_put_cstr(string, "idle");
853         break;
854     case OFPRR_HARD_TIMEOUT:
855         ds_put_cstr(string, "hard");
856         break;
857     case OFPRR_DELETE:
858         ds_put_cstr(string, "delete");
859         break;
860     default:
861         ds_put_format(string, "**%"PRIu8"**", ofr->reason);
862         break;
863     }
864
865     if (ofr->cookie != htonll(0)) {
866         ds_put_format(string, " cookie:0x%"PRIx64, ntohll(ofr->cookie));
867     }
868     if (ofr->priority != htons(32768)) {
869         ds_put_format(string, " pri:%"PRIu16, ntohs(ofr->priority));
870     }
871     ds_put_format(string, " secs%"PRIu32" nsecs%"PRIu32
872          " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
873          ntohl(ofr->duration_sec), ntohl(ofr->duration_nsec),
874          ntohs(ofr->idle_timeout), ntohll(ofr->packet_count),
875          ntohll(ofr->byte_count));
876 }
877
878 static void
879 ofp_print_port_mod(struct ds *string, const void *oh, size_t len OVS_UNUSED,
880                    int verbosity OVS_UNUSED)
881 {
882     const struct ofp_port_mod *opm = oh;
883
884     ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
885             ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
886             ntohl(opm->config), ntohl(opm->mask));
887     ds_put_format(string, "     advertise: ");
888     if (opm->advertise) {
889         ofp_print_port_features(string, ntohl(opm->advertise));
890     } else {
891         ds_put_format(string, "UNCHANGED\n");
892     }
893 }
894
895 struct error_type {
896     int type;
897     int code;
898     const char *name;
899 };
900
901 static const struct error_type error_types[] = {
902 #define ERROR_TYPE(TYPE) {TYPE, -1, #TYPE}
903 #define ERROR_CODE(TYPE, CODE) {TYPE, CODE, #CODE}
904     ERROR_TYPE(OFPET_HELLO_FAILED),
905     ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_INCOMPATIBLE),
906     ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_EPERM),
907
908     ERROR_TYPE(OFPET_BAD_REQUEST),
909     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
910     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE),
911     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT),
912     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR),
913     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE),
914     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_EPERM),
915     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN),
916     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_EMPTY),
917     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_UNKNOWN),
918
919     ERROR_TYPE(OFPET_BAD_ACTION),
920     ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE),
921     ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_LEN),
922     ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR),
923     ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE),
924     ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT),
925     ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT),
926     ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_EPERM),
927     ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_TOO_MANY),
928
929     ERROR_TYPE(OFPET_FLOW_MOD_FAILED),
930     ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL),
931     ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP),
932     ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_EPERM),
933     ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_EMERG_TIMEOUT),
934     ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND),
935
936     ERROR_TYPE(OFPET_PORT_MOD_FAILED),
937     ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT),
938     ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR)
939 };
940 #define N_ERROR_TYPES ARRAY_SIZE(error_types)
941
942 static const char *
943 lookup_error_type(int type)
944 {
945     const struct error_type *t;
946
947     for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
948         if (t->type == type && t->code == -1) {
949             return t->name;
950         }
951     }
952     return "?";
953 }
954
955 static const char *
956 lookup_error_code(int type, int code)
957 {
958     const struct error_type *t;
959
960     for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
961         if (t->type == type && t->code == code) {
962             return t->name;
963         }
964     }
965     return "?";
966 }
967
968 /* Pretty-print the OFPT_ERROR packet of 'len' bytes at 'oh' to 'string'
969  * at the given 'verbosity' level. */
970 static void
971 ofp_print_error_msg(struct ds *string, const void *oh, size_t len,
972                        int verbosity OVS_UNUSED)
973 {
974     const struct ofp_error_msg *oem = oh;
975     int type = ntohs(oem->type);
976     int code = ntohs(oem->code);
977     char *s;
978
979     ds_put_format(string, " type%d(%s) code%d(%s) payload:\n",
980                   type, lookup_error_type(type),
981                   code, lookup_error_code(type, code));
982
983     switch (type) {
984     case OFPET_HELLO_FAILED:
985         ds_put_printable(string, (char *) oem->data, len - sizeof *oem);
986         break;
987
988     case OFPET_BAD_REQUEST:
989         s = ofp_to_string(oem->data, len - sizeof *oem, 1);
990         ds_put_cstr(string, s);
991         free(s);
992         break;
993
994     default:
995         ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
996         break;
997     }
998 }
999
1000 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'string'
1001  * at the given 'verbosity' level. */
1002 static void
1003 ofp_print_port_status(struct ds *string, const void *oh, size_t len OVS_UNUSED,
1004                       int verbosity OVS_UNUSED)
1005 {
1006     const struct ofp_port_status *ops = oh;
1007
1008     if (ops->reason == OFPPR_ADD) {
1009         ds_put_format(string, " ADD:");
1010     } else if (ops->reason == OFPPR_DELETE) {
1011         ds_put_format(string, " DEL:");
1012     } else if (ops->reason == OFPPR_MODIFY) {
1013         ds_put_format(string, " MOD:");
1014     }
1015
1016     ofp_print_phy_port(string, &ops->desc);
1017 }
1018
1019 static void
1020 ofp_desc_stats_reply(struct ds *string, const void *body,
1021                      size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
1022 {
1023     const struct ofp_desc_stats *ods = body;
1024
1025     ds_put_format(string, "Manufacturer: %.*s\n",
1026             (int) sizeof ods->mfr_desc, ods->mfr_desc);
1027     ds_put_format(string, "Hardware: %.*s\n",
1028             (int) sizeof ods->hw_desc, ods->hw_desc);
1029     ds_put_format(string, "Software: %.*s\n",
1030             (int) sizeof ods->sw_desc, ods->sw_desc);
1031     ds_put_format(string, "Serial Num: %.*s\n",
1032             (int) sizeof ods->serial_num, ods->serial_num);
1033     ds_put_format(string, "DP Description: %.*s\n",
1034             (int) sizeof ods->dp_desc, ods->dp_desc);
1035 }
1036
1037 static void
1038 ofp_flow_stats_request(struct ds *string, const void *oh,
1039                        size_t len OVS_UNUSED, int verbosity)
1040 {
1041     const struct ofp_flow_stats_request *fsr = oh;
1042
1043     if (fsr->table_id == 0xff) {
1044         ds_put_format(string, " table_id=any, ");
1045     } else {
1046         ds_put_format(string, " table_id=%"PRIu8", ", fsr->table_id);
1047     }
1048
1049     ofp_print_match(string, &fsr->match, verbosity);
1050 }
1051
1052 static void
1053 ofp_flow_stats_reply(struct ds *string, const void *body_, size_t len,
1054                      int verbosity)
1055 {
1056     const char *body = body_;
1057     const char *pos = body;
1058     for (;;) {
1059         const struct ofp_flow_stats *fs;
1060         ptrdiff_t bytes_left = body + len - pos;
1061         size_t length;
1062
1063         if (bytes_left < sizeof *fs) {
1064             if (bytes_left != 0) {
1065                 ds_put_format(string, " ***%td leftover bytes at end***",
1066                               bytes_left);
1067             }
1068             break;
1069         }
1070
1071         fs = (const void *) pos;
1072         length = ntohs(fs->length);
1073         if (length < sizeof *fs) {
1074             ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
1075                           length, sizeof *fs);
1076             break;
1077         } else if (length > bytes_left) {
1078             ds_put_format(string,
1079                           " ***length=%zu but only %td bytes left***",
1080                           length, bytes_left);
1081             break;
1082         } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
1083             ds_put_format(string,
1084                           " ***length=%zu has %zu bytes leftover in "
1085                           "final action***",
1086                           length,
1087                           (length - sizeof *fs) % sizeof fs->actions[0]);
1088             break;
1089         }
1090
1091         ds_put_format(string, "  cookie=0x%"PRIx64", ", ntohll(fs->cookie));
1092         ds_put_format(string, "duration_sec=%"PRIu32"s, ",
1093                     ntohl(fs->duration_sec));
1094         ds_put_format(string, "duration_nsec=%"PRIu32"ns, ",
1095                     ntohl(fs->duration_nsec));
1096         ds_put_format(string, "table_id=%"PRIu8", ", fs->table_id);
1097         ds_put_format(string, "priority=%"PRIu16", ",
1098                     fs->match.wildcards ? ntohs(fs->priority) : (uint16_t)-1);
1099         ds_put_format(string, "n_packets=%"PRIu64", ",
1100                     ntohll(fs->packet_count));
1101         ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
1102         if (fs->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
1103             ds_put_format(string, "idle_timeout=%"PRIu16",",
1104                           ntohs(fs->idle_timeout));
1105         }
1106         if (fs->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
1107             ds_put_format(string, "hard_timeout=%"PRIu16",",
1108                           ntohs(fs->hard_timeout));
1109         }
1110         ofp_print_match(string, &fs->match, verbosity);
1111         ofp_print_actions(string, fs->actions, length - sizeof *fs);
1112         ds_put_char(string, '\n');
1113
1114         pos += length;
1115      }
1116 }
1117
1118 static void
1119 ofp_aggregate_stats_request(struct ds *string, const void *oh,
1120                             size_t len OVS_UNUSED, int verbosity)
1121 {
1122     const struct ofp_aggregate_stats_request *asr = oh;
1123
1124     if (asr->table_id == 0xff) {
1125         ds_put_format(string, " table_id=any, ");
1126     } else {
1127         ds_put_format(string, " table_id=%"PRIu8", ", asr->table_id);
1128     }
1129
1130     ofp_print_match(string, &asr->match, verbosity);
1131 }
1132
1133 static void
1134 ofp_aggregate_stats_reply(struct ds *string, const void *body_,
1135                           size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
1136 {
1137     const struct ofp_aggregate_stats_reply *asr = body_;
1138
1139     ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
1140     ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
1141     ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1142 }
1143
1144 static void print_port_stat(struct ds *string, const char *leader,
1145                             uint64_t stat, int more)
1146 {
1147     ds_put_cstr(string, leader);
1148     if (stat != -1) {
1149         ds_put_format(string, "%"PRIu64, stat);
1150     } else {
1151         ds_put_char(string, '?');
1152     }
1153     if (more) {
1154         ds_put_cstr(string, ", ");
1155     } else {
1156         ds_put_cstr(string, "\n");
1157     }
1158 }
1159
1160 static void
1161 ofp_port_stats_request(struct ds *string, const void *body_,
1162                        size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
1163 {
1164     const struct ofp_port_stats_request *psr = body_;
1165     ds_put_format(string, "port_no=%"PRIu16, ntohs(psr->port_no));
1166 }
1167
1168 static void
1169 ofp_port_stats_reply(struct ds *string, const void *body, size_t len,
1170                      int verbosity)
1171 {
1172     const struct ofp_port_stats *ps = body;
1173     size_t n = len / sizeof *ps;
1174     ds_put_format(string, " %zu ports\n", n);
1175     if (verbosity < 1) {
1176         return;
1177     }
1178
1179     for (; n--; ps++) {
1180         ds_put_format(string, "  port %2"PRIu16": ", ntohs(ps->port_no));
1181
1182         ds_put_cstr(string, "rx ");
1183         print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
1184         print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
1185         print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
1186         print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
1187         print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
1188         print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
1189         print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
1190
1191         ds_put_cstr(string, "           tx ");
1192         print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
1193         print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
1194         print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
1195         print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
1196         print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
1197     }
1198 }
1199
1200 static void
1201 ofp_table_stats_reply(struct ds *string, const void *body, size_t len,
1202                      int verbosity)
1203 {
1204     const struct ofp_table_stats *ts = body;
1205     size_t n = len / sizeof *ts;
1206     ds_put_format(string, " %zu tables\n", n);
1207     if (verbosity < 1) {
1208         return;
1209     }
1210
1211     for (; n--; ts++) {
1212         char name[OFP_MAX_TABLE_NAME_LEN + 1];
1213         strncpy(name, ts->name, sizeof name);
1214         name[OFP_MAX_TABLE_NAME_LEN] = '\0';
1215
1216         ds_put_format(string, "  %d: %-8s: ", ts->table_id, name);
1217         ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1218         ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1219         ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1220         ds_put_cstr(string, "               ");
1221         ds_put_format(string, "lookup=%"PRIu64", ",
1222                     ntohll(ts->lookup_count));
1223         ds_put_format(string, "matched=%"PRIu64"\n",
1224                     ntohll(ts->matched_count));
1225      }
1226 }
1227
1228 static void
1229 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1230 {
1231     if (queue_id == OFPQ_ALL) {
1232         ds_put_cstr(string, "ALL");
1233     } else {
1234         ds_put_format(string, "%"PRIu32, queue_id);
1235     }
1236 }
1237
1238 static void
1239 ofp_queue_stats_request(struct ds *string, const void *body_,
1240                        size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
1241 {
1242     const struct ofp_queue_stats_request *qsr = body_;
1243
1244     ds_put_cstr(string, "port=");
1245     ofp_print_port_name(string, ntohs(qsr->port_no));
1246
1247     ds_put_cstr(string, " queue=");
1248     ofp_print_queue_name(string, ntohl(qsr->queue_id));
1249 }
1250
1251 static void
1252 ofp_queue_stats_reply(struct ds *string, const void *body, size_t len,
1253                      int verbosity)
1254 {
1255     const struct ofp_queue_stats *qs = body;
1256     size_t n = len / sizeof *qs;
1257     ds_put_format(string, " %zu queues\n", n);
1258     if (verbosity < 1) {
1259         return;
1260     }
1261
1262     for (; n--; qs++) {
1263         ds_put_cstr(string, "  port ");
1264         ofp_print_port_name(string, ntohs(qs->port_no));
1265         ds_put_cstr(string, " queue ");
1266         ofp_print_queue_name(string, ntohl(qs->queue_id));
1267         ds_put_cstr(string, ": ");
1268
1269         print_port_stat(string, "bytes=", ntohll(qs->tx_bytes), 1);
1270         print_port_stat(string, "pkts=", ntohll(qs->tx_packets), 1);
1271         print_port_stat(string, "errors=", ntohll(qs->tx_errors), 0);
1272     }
1273 }
1274
1275 static void
1276 vendor_stat(struct ds *string, const void *body, size_t len,
1277             int verbosity OVS_UNUSED)
1278 {
1279     ds_put_format(string, " vendor=%08"PRIx32, ntohl(*(uint32_t *) body));
1280     ds_put_format(string, " %zu bytes additional data",
1281                   len - sizeof(uint32_t));
1282 }
1283
1284 enum stats_direction {
1285     REQUEST,
1286     REPLY
1287 };
1288
1289 static void
1290 print_stats(struct ds *string, int type, const void *body, size_t body_len,
1291             int verbosity, enum stats_direction direction)
1292 {
1293     struct stats_msg {
1294         size_t min_body, max_body;
1295         void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1296     };
1297
1298     struct stats_type {
1299         int type;
1300         const char *name;
1301         struct stats_msg request;
1302         struct stats_msg reply;
1303     };
1304
1305     static const struct stats_type stats_types[] = {
1306         {
1307             OFPST_DESC,
1308             "description",
1309             { 0, 0, NULL },
1310             { 0, SIZE_MAX, ofp_desc_stats_reply },
1311         },
1312         {
1313             OFPST_FLOW,
1314             "flow",
1315             { sizeof(struct ofp_flow_stats_request),
1316               sizeof(struct ofp_flow_stats_request),
1317               ofp_flow_stats_request },
1318             { 0, SIZE_MAX, ofp_flow_stats_reply },
1319         },
1320         {
1321             OFPST_AGGREGATE,
1322             "aggregate",
1323             { sizeof(struct ofp_aggregate_stats_request),
1324               sizeof(struct ofp_aggregate_stats_request),
1325               ofp_aggregate_stats_request },
1326             { sizeof(struct ofp_aggregate_stats_reply),
1327               sizeof(struct ofp_aggregate_stats_reply),
1328               ofp_aggregate_stats_reply },
1329         },
1330         {
1331             OFPST_TABLE,
1332             "table",
1333             { 0, 0, NULL },
1334             { 0, SIZE_MAX, ofp_table_stats_reply },
1335         },
1336         {
1337             OFPST_PORT,
1338             "port",
1339             { sizeof(struct ofp_port_stats_request),
1340               sizeof(struct ofp_port_stats_request),
1341               ofp_port_stats_request },
1342             { 0, SIZE_MAX, ofp_port_stats_reply },
1343         },
1344         {
1345             OFPST_QUEUE,
1346             "queue",
1347             { sizeof(struct ofp_queue_stats_request),
1348               sizeof(struct ofp_queue_stats_request),
1349               ofp_queue_stats_request },
1350             { 0, SIZE_MAX, ofp_queue_stats_reply },
1351         },
1352         {
1353             OFPST_VENDOR,
1354             "vendor-specific",
1355             { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1356             { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1357         },
1358         {
1359             -1,
1360             "unknown",
1361             { 0, 0, NULL, },
1362             { 0, 0, NULL, },
1363         },
1364     };
1365
1366     const struct stats_type *s;
1367     const struct stats_msg *m;
1368
1369     if (type >= ARRAY_SIZE(stats_types) || !stats_types[type].name) {
1370         ds_put_format(string, " ***unknown type %d***", type);
1371         return;
1372     }
1373     for (s = stats_types; s->type >= 0; s++) {
1374         if (s->type == type) {
1375             break;
1376         }
1377     }
1378     ds_put_format(string, " type=%d(%s)\n", type, s->name);
1379
1380     m = direction == REQUEST ? &s->request : &s->reply;
1381     if (body_len < m->min_body || body_len > m->max_body) {
1382         ds_put_format(string, " ***body_len=%zu not in %zu...%zu***",
1383                       body_len, m->min_body, m->max_body);
1384         return;
1385     }
1386     if (m->printer) {
1387         m->printer(string, body, body_len, verbosity);
1388     }
1389 }
1390
1391 static void
1392 ofp_stats_request(struct ds *string, const void *oh, size_t len, int verbosity)
1393 {
1394     const struct ofp_stats_request *srq = oh;
1395
1396     if (srq->flags) {
1397         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1398                       ntohs(srq->flags));
1399     }
1400
1401     print_stats(string, ntohs(srq->type), srq->body,
1402                 len - offsetof(struct ofp_stats_request, body),
1403                 verbosity, REQUEST);
1404 }
1405
1406 static void
1407 ofp_stats_reply(struct ds *string, const void *oh, size_t len, int verbosity)
1408 {
1409     const struct ofp_stats_reply *srp = oh;
1410
1411     ds_put_cstr(string, " flags=");
1412     if (!srp->flags) {
1413         ds_put_cstr(string, "none");
1414     } else {
1415         uint16_t flags = ntohs(srp->flags);
1416         if (flags & OFPSF_REPLY_MORE) {
1417             ds_put_cstr(string, "[more]");
1418             flags &= ~OFPSF_REPLY_MORE;
1419         }
1420         if (flags) {
1421             ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]", flags);
1422         }
1423     }
1424
1425     print_stats(string, ntohs(srp->type), srp->body,
1426                 len - offsetof(struct ofp_stats_reply, body),
1427                 verbosity, REPLY);
1428 }
1429
1430 static void
1431 ofp_echo(struct ds *string, const void *oh, size_t len, int verbosity)
1432 {
1433     const struct ofp_header *hdr = oh;
1434
1435     ds_put_format(string, " %zu bytes of payload\n", len - sizeof *hdr);
1436     if (verbosity > 1) {
1437         ds_put_hex_dump(string, hdr, len - sizeof *hdr, 0, true);
1438     }
1439 }
1440
1441 struct openflow_packet {
1442     uint8_t type;
1443     const char *name;
1444     size_t min_size;
1445     void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1446 };
1447
1448 static const struct openflow_packet packets[] = {
1449     {
1450         OFPT_HELLO,
1451         "hello",
1452         sizeof (struct ofp_header),
1453         NULL,
1454     },
1455     {
1456         OFPT_FEATURES_REQUEST,
1457         "features_request",
1458         sizeof (struct ofp_header),
1459         NULL,
1460     },
1461     {
1462         OFPT_FEATURES_REPLY,
1463         "features_reply",
1464         sizeof (struct ofp_switch_features),
1465         ofp_print_switch_features,
1466     },
1467     {
1468         OFPT_GET_CONFIG_REQUEST,
1469         "get_config_request",
1470         sizeof (struct ofp_header),
1471         NULL,
1472     },
1473     {
1474         OFPT_GET_CONFIG_REPLY,
1475         "get_config_reply",
1476         sizeof (struct ofp_switch_config),
1477         ofp_print_switch_config,
1478     },
1479     {
1480         OFPT_SET_CONFIG,
1481         "set_config",
1482         sizeof (struct ofp_switch_config),
1483         ofp_print_switch_config,
1484     },
1485     {
1486         OFPT_PACKET_IN,
1487         "packet_in",
1488         offsetof(struct ofp_packet_in, data),
1489         ofp_packet_in,
1490     },
1491     {
1492         OFPT_PACKET_OUT,
1493         "packet_out",
1494         sizeof (struct ofp_packet_out),
1495         ofp_packet_out,
1496     },
1497     {
1498         OFPT_FLOW_MOD,
1499         "flow_mod",
1500         sizeof (struct ofp_flow_mod),
1501         ofp_print_flow_mod,
1502     },
1503     {
1504         OFPT_FLOW_REMOVED,
1505         "flow_removed",
1506         sizeof (struct ofp_flow_removed),
1507         ofp_print_flow_removed,
1508     },
1509     {
1510         OFPT_PORT_MOD,
1511         "port_mod",
1512         sizeof (struct ofp_port_mod),
1513         ofp_print_port_mod,
1514     },
1515     {
1516         OFPT_PORT_STATUS,
1517         "port_status",
1518         sizeof (struct ofp_port_status),
1519         ofp_print_port_status
1520     },
1521     {
1522         OFPT_ERROR,
1523         "error_msg",
1524         sizeof (struct ofp_error_msg),
1525         ofp_print_error_msg,
1526     },
1527     {
1528         OFPT_STATS_REQUEST,
1529         "stats_request",
1530         sizeof (struct ofp_stats_request),
1531         ofp_stats_request,
1532     },
1533     {
1534         OFPT_STATS_REPLY,
1535         "stats_reply",
1536         sizeof (struct ofp_stats_reply),
1537         ofp_stats_reply,
1538     },
1539     {
1540         OFPT_ECHO_REQUEST,
1541         "echo_request",
1542         sizeof (struct ofp_header),
1543         ofp_echo,
1544     },
1545     {
1546         OFPT_ECHO_REPLY,
1547         "echo_reply",
1548         sizeof (struct ofp_header),
1549         ofp_echo,
1550     },
1551     {
1552         OFPT_VENDOR,
1553         "vendor",
1554         sizeof (struct ofp_vendor_header),
1555         NULL,
1556     },
1557     {
1558         OFPT_BARRIER_REQUEST,
1559         "barrier_request",
1560         sizeof (struct ofp_header),
1561         NULL,
1562     },
1563     {
1564         OFPT_BARRIER_REPLY,
1565         "barrier_reply",
1566         sizeof (struct ofp_header),
1567         NULL,
1568     }
1569 };
1570
1571 /* Composes and returns a string representing the OpenFlow packet of 'len'
1572  * bytes at 'oh' at the given 'verbosity' level.  0 is a minimal amount of
1573  * verbosity and higher numbers increase verbosity.  The caller is responsible
1574  * for freeing the string. */
1575 char *
1576 ofp_to_string(const void *oh_, size_t len, int verbosity)
1577 {
1578     struct ds string = DS_EMPTY_INITIALIZER;
1579     const struct ofp_header *oh = oh_;
1580     const struct openflow_packet *pkt;
1581
1582     if (len < sizeof(struct ofp_header)) {
1583         ds_put_cstr(&string, "OpenFlow packet too short:\n");
1584         ds_put_hex_dump(&string, oh, len, 0, true);
1585         return ds_cstr(&string);
1586     } else if (oh->version != OFP_VERSION) {
1587         ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", oh->version);
1588         ds_put_hex_dump(&string, oh, len, 0, true);
1589         return ds_cstr(&string);
1590     }
1591
1592     for (pkt = packets; ; pkt++) {
1593         if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1594             ds_put_format(&string, "Unknown OpenFlow packet type %"PRIu8":\n",
1595                           oh->type);
1596             ds_put_hex_dump(&string, oh, len, 0, true);
1597             return ds_cstr(&string);
1598         } else if (oh->type == pkt->type) {
1599             break;
1600         }
1601     }
1602
1603     ds_put_format(&string, "%s (xid=0x%"PRIx32"):", pkt->name, ntohl(oh->xid));
1604
1605     if (ntohs(oh->length) > len)
1606         ds_put_format(&string, " (***truncated to %zu bytes from %"PRIu16"***)",
1607                 len, ntohs(oh->length));
1608     else if (ntohs(oh->length) < len) {
1609         ds_put_format(&string, " (***only uses %"PRIu16" bytes out of %zu***)\n",
1610                 ntohs(oh->length), len);
1611         len = ntohs(oh->length);
1612     }
1613
1614     if (len < pkt->min_size) {
1615         ds_put_format(&string, " (***length=%zu < min_size=%zu***)\n",
1616                 len, pkt->min_size);
1617     } else if (!pkt->printer) {
1618         if (len > sizeof *oh) {
1619             ds_put_format(&string, " length=%"PRIu16" (decoder not implemented)\n",
1620                           ntohs(oh->length));
1621         }
1622     } else {
1623         pkt->printer(&string, oh, len, verbosity);
1624     }
1625     if (verbosity >= 3) {
1626         ds_put_hex_dump(&string, oh, len, 0, true);
1627     }
1628     if (string.string[string.length - 1] != '\n') {
1629         ds_put_char(&string, '\n');
1630     }
1631     return ds_cstr(&string);
1632 }
1633
1634 /* Returns the name for the specified OpenFlow message type as a string,
1635  * e.g. "OFPT_FEATURES_REPLY".  If no name is known, the string returned is a
1636  * hex number, e.g. "0x55".
1637  *
1638  * The caller must free the returned string when it is no longer needed. */
1639 char *
1640 ofp_message_type_to_string(uint8_t type)
1641 {
1642     struct ds s = DS_EMPTY_INITIALIZER;
1643     const struct openflow_packet *pkt;
1644     for (pkt = packets; ; pkt++) {
1645         if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1646             ds_put_format(&s, "0x%02"PRIx8, type);
1647             break;
1648         } else if (type == pkt->type) {
1649             const char *p;
1650
1651             ds_put_cstr(&s, "OFPT_");
1652             for (p = pkt->name; *p; p++) {
1653                 ds_put_char(&s, toupper((unsigned char) *p));
1654             }
1655             break;
1656         }
1657     }
1658     return ds_cstr(&s);
1659 }
1660 \f
1661 static void
1662 print_and_free(FILE *stream, char *string)
1663 {
1664     fputs(string, stream);
1665     free(string);
1666 }
1667
1668 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1669  * given 'verbosity' level.  0 is a minimal amount of verbosity and higher
1670  * numbers increase verbosity. */
1671 void
1672 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1673 {
1674     print_and_free(stream, ofp_to_string(oh, len, verbosity));
1675 }
1676
1677 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1678  * 'data' to 'stream' using tcpdump.  'total_len' specifies the full length of
1679  * the Ethernet frame (of which 'len' bytes were captured).
1680  *
1681  * This starts and kills a tcpdump subprocess so it's quite expensive. */
1682 void
1683 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1684 {
1685     print_and_free(stream, ofp_packet_to_string(data, len, total_len));
1686 }