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