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