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