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