First cut of pretty printer of ofp_port_mod messages.
[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_DL_VLAN:
270         ds_put_cstr(string, "mod_vlan:");
271         if (ntohs(a->arg.vlan_id) == OFP_VLAN_NONE) {
272             ds_put_cstr(string, "strip");
273         } else {
274             ds_put_format(string, "%"PRIu16, ntohs(a->arg.vlan_id));
275         }
276         break;
277
278     case OFPAT_SET_DL_SRC:
279         ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT, 
280                 ETH_ADDR_ARGS(a->arg.dl_addr));
281         break;
282
283     case OFPAT_SET_DL_DST:
284         ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT, 
285                 ETH_ADDR_ARGS(a->arg.dl_addr));
286         break;
287
288     case OFPAT_SET_NW_SRC:
289         ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&a->arg.nw_addr));
290         break;
291
292     case OFPAT_SET_NW_DST:
293         ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&a->arg.nw_addr));
294         break;
295
296     case OFPAT_SET_TP_SRC:
297         ds_put_format(string, "mod_tp_src:%d", ntohs(a->arg.tp));
298         break;
299
300     case OFPAT_SET_TP_DST:
301         ds_put_format(string, "mod_tp_dst:%d", ntohs(a->arg.tp));
302         break;
303
304     default:
305         ds_put_format(string, "(decoder %"PRIu16" not implemented)", 
306                 ntohs(a->type));
307         break;
308     }
309 }
310
311 static void ofp_print_actions(struct ds *string,
312                               const struct ofp_action actions[],
313                               size_t n_bytes) 
314 {
315     size_t i;
316     int n_actions = n_bytes / sizeof *actions;
317
318     ds_put_format(string, "action%s=", n_actions == 1 ? "" : "s");
319     for (i = 0; i < n_actions; i++) {
320         if (i) {
321             ds_put_cstr(string, ",");
322         }
323         ofp_print_action(string, &actions[i]);
324     }
325     if (n_bytes % sizeof *actions) {
326         if (i) {
327             ds_put_cstr(string, ",");
328         }
329         ds_put_cstr(string, ", ***trailing garbage***");
330     }
331 }
332
333 /* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'string'
334  * at the given 'verbosity' level. */
335 static void ofp_packet_out(struct ds *string, const void *oh, size_t len,
336                            int verbosity) 
337 {
338     const struct ofp_packet_out *opo = oh;
339     int n_actions = ntohs(opo->n_actions);
340     int act_len = n_actions * sizeof opo->actions[0];
341
342     ds_put_cstr(string, " in_port=");
343     ofp_print_port_name(string, ntohs(opo->in_port));
344
345     ds_put_format(string, " n_actions=%d ", n_actions);
346     if (act_len > (ntohs(opo->header.length) - sizeof *opo)) {
347         ds_put_format(string, "***packet too short for number of actions***\n");
348         return;
349     }
350     ofp_print_actions(string, opo->actions, act_len);
351
352     if (ntohl(opo->buffer_id) == UINT32_MAX) {
353         int data_len = len - sizeof *opo - act_len;
354         ds_put_format(string, " data_len=%d", data_len);
355         if (verbosity > 0 && len > sizeof *opo) {
356             char *packet = ofp_packet_to_string(&opo->actions[n_actions], 
357                                                 data_len, data_len);
358             ds_put_char(string, '\n');
359             ds_put_cstr(string, packet);
360             free(packet);
361         }
362     } else {
363         ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
364     }
365     ds_put_char(string, '\n');
366 }
367
368 /* qsort comparison function. */
369 static int
370 compare_ports(const void *a_, const void *b_)
371 {
372     const struct ofp_phy_port *a = a_;
373     const struct ofp_phy_port *b = b_;
374     uint16_t ap = ntohs(a->port_no);
375     uint16_t bp = ntohs(b->port_no);
376
377     return ap < bp ? -1 : ap > bp;
378 }
379
380 static void ofp_print_port_features(struct ds *string, uint32_t features)
381 {
382     if (features == 0) {
383         ds_put_cstr(string, "Unsupported\n");
384         return;
385     }
386     if (features & OFPPF_10MB_HD) {
387         ds_put_cstr(string, "10MB-HD ");
388     }
389     if (features & OFPPF_10MB_FD) {
390         ds_put_cstr(string, "10MB-FD ");
391     }
392     if (features & OFPPF_100MB_HD) {
393         ds_put_cstr(string, "100MB-HD ");
394     }
395     if (features & OFPPF_100MB_FD) {
396         ds_put_cstr(string, "100MB-FD ");
397     }
398     if (features & OFPPF_1GB_HD) {
399         ds_put_cstr(string, "1GB-HD ");
400     }
401     if (features & OFPPF_1GB_FD) {
402         ds_put_cstr(string, "1GB-FD ");
403     }
404     if (features & OFPPF_10GB_FD) {
405         ds_put_cstr(string, "10GB-FD ");
406     }
407     if (features & OFPPF_COPPER) {
408         ds_put_cstr(string, "COPPER ");
409     }
410     if (features & OFPPF_FIBER) {
411         ds_put_cstr(string, "FIBER ");
412     }
413     if (features & OFPPF_AUTONEG) {
414         ds_put_cstr(string, "AUTO_NEG ");
415     }
416     if (features & OFPPF_PAUSE) {
417         ds_put_cstr(string, "AUTO_PAUSE ");
418     }
419     if (features & OFPPF_PAUSE_ASYM) {
420         ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
421     }
422     ds_put_char(string, '\n');
423 }
424
425 static void
426 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
427 {
428     uint8_t name[OFP_MAX_PORT_NAME_LEN];
429     int j;
430
431     memcpy(name, port->name, sizeof name);
432     for (j = 0; j < sizeof name - 1; j++) {
433         if (!isprint(name[j])) {
434             break;
435         }
436     }
437     name[j] = '\0';
438
439     ds_put_char(string, ' ');
440     ofp_print_port_name(string, ntohs(port->port_no));
441     ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
442             name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
443             ntohl(port->state));
444     if (port->curr) {
445         ds_put_format(string, "     current:    ");
446         ofp_print_port_features(string, ntohl(port->curr));
447     }
448     if (port->advertised) {
449         ds_put_format(string, "     advertised: ");
450         ofp_print_port_features(string, ntohl(port->advertised));
451     }
452     if (port->supported) {
453         ds_put_format(string, "     supported:  ");
454         ofp_print_port_features(string, ntohl(port->supported));
455     }
456     if (port->peer) {
457         ds_put_format(string, "     peer:       ");
458         ofp_print_port_features(string, ntohl(port->peer));
459     }
460 }
461
462 /* Pretty-print the struct ofp_switch_features of 'len' bytes at 'oh' to
463  * 'string' at the given 'verbosity' level. */
464 static void
465 ofp_print_switch_features(struct ds *string, const void *oh, size_t len,
466                           int verbosity)
467 {
468     const struct ofp_switch_features *osf = oh;
469     struct ofp_phy_port port_list[OFPP_MAX];
470     int n_ports;
471     int i;
472
473     ds_put_format(string, "dp id:%"PRIx64"\n", ntohll(osf->datapath_id));
474     ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
475             ntohl(osf->n_buffers));
476     ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
477            ntohl(osf->capabilities), ntohl(osf->actions));
478
479     if (ntohs(osf->header.length) >= sizeof *osf) {
480         len = MIN(len, ntohs(osf->header.length));
481     }
482     n_ports = (len - sizeof *osf) / sizeof *osf->ports;
483
484     memcpy(port_list, osf->ports, (len - sizeof *osf));
485     qsort(port_list, n_ports, sizeof port_list[0], compare_ports);
486     for (i = 0; i < n_ports; i++) {
487         ofp_print_phy_port(string, &port_list[i]);
488     }
489 }
490
491 /* Pretty-print the struct ofp_switch_config of 'len' bytes at 'oh' to 'string'
492  * at the given 'verbosity' level. */
493 static void
494 ofp_print_switch_config(struct ds *string, const void *oh, size_t len,
495                         int verbosity)
496 {
497     const struct ofp_switch_config *osc = oh;
498     uint16_t flags;
499
500     flags = ntohs(osc->flags);
501     if (flags & OFPC_SEND_FLOW_EXP) {
502         flags &= ~OFPC_SEND_FLOW_EXP;
503         ds_put_format(string, " (sending flow expirations)");
504     }
505     if (flags) {
506         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
507     }
508
509     ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
510 }
511
512 static void print_wild(struct ds *string, const char *leader, int is_wild,
513             int verbosity, const char *format, ...) 
514             __attribute__((format(printf, 5, 6)));
515
516 static void print_wild(struct ds *string, const char *leader, int is_wild,
517                        int verbosity, const char *format, ...) 
518 {
519     if (is_wild && verbosity < 2) {
520         return;
521     }
522     ds_put_cstr(string, leader);
523     if (!is_wild) {
524         va_list args;
525
526         va_start(args, format);
527         ds_put_format_valist(string, format, args);
528         va_end(args);
529     } else {
530         ds_put_char(string, '*');
531     }
532     ds_put_char(string, ',');
533 }
534
535 static void
536 print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
537                  uint32_t wild_bits, int verbosity)
538 {
539     if (wild_bits >= 32 && verbosity < 2) {
540         return;
541     }
542     ds_put_cstr(string, leader);
543     if (wild_bits < 32) {
544         ds_put_format(string, IP_FMT, IP_ARGS(&ip));
545         if (wild_bits) {
546             ds_put_format(string, "/%d", 32 - wild_bits);
547         }
548     } else {
549         ds_put_char(string, '*');
550     }
551     ds_put_char(string, ',');
552 }
553
554 /* Pretty-print the ofp_match structure */
555 static void ofp_print_match(struct ds *f, const struct ofp_match *om, 
556         int verbosity)
557 {
558     uint32_t w = ntohl(om->wildcards);
559     bool skip_type = false;
560     bool skip_proto = false;
561
562     if (!(w & OFPFW_DL_TYPE)) {
563         skip_type = true;
564         if (om->dl_type == htons(ETH_TYPE_IP)) {
565             if (!(w & OFPFW_NW_PROTO)) {
566                 skip_proto = true;
567                 if (om->nw_proto == IP_TYPE_ICMP) {
568                     ds_put_cstr(f, "icmp,");
569                 } else if (om->nw_proto == IP_TYPE_TCP) {
570                     ds_put_cstr(f, "tcp,");
571                 } else if (om->nw_proto == IP_TYPE_UDP) {
572                     ds_put_cstr(f, "udp,");
573                 } else {
574                     ds_put_cstr(f, "ip,");
575                     skip_proto = false;
576                 }
577             } else {
578                 ds_put_cstr(f, "ip,");
579             }
580         } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
581             ds_put_cstr(f, "arp,");
582         } else {
583             skip_type = false;
584         }
585     }
586     print_wild(f, "in_port=", w & OFPFW_IN_PORT, verbosity,
587                "%d", ntohs(om->in_port));
588     print_wild(f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
589                "0x%04x", ntohs(om->dl_vlan));
590     print_wild(f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
591                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
592     print_wild(f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
593                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
594     if (!skip_type) {
595         print_wild(f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
596                    "0x%04x", ntohs(om->dl_type));
597     }
598     print_ip_netmask(f, "nw_src=", om->nw_src,
599                      (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
600     print_ip_netmask(f, "nw_dst=", om->nw_dst,
601                      (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
602     if (!skip_proto) {
603         print_wild(f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
604                    "%u", om->nw_proto);
605     }
606     print_wild(f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
607                "%d", ntohs(om->tp_src));
608     print_wild(f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
609                "%d", ntohs(om->tp_dst));
610 }
611
612 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
613  * at the given 'verbosity' level. */
614 static void
615 ofp_print_flow_mod(struct ds *string, const void *oh, size_t len, 
616                    int verbosity)
617 {
618     const struct ofp_flow_mod *ofm = oh;
619
620     ofp_print_match(string, &ofm->match, verbosity);
621     switch (ntohs(ofm->command)) {
622     case OFPFC_ADD:
623         ds_put_cstr(string, " ADD: ");
624         break;
625     case OFPFC_MODIFY:
626         ds_put_cstr(string, " MOD: ");
627         break;
628     case OFPFC_MODIFY_STRICT:
629         ds_put_cstr(string, " MOD_STRICT: ");
630         break;
631     case OFPFC_DELETE:
632         ds_put_cstr(string, " DEL: ");
633         break;
634     case OFPFC_DELETE_STRICT:
635         ds_put_cstr(string, " DEL_STRICT: ");
636         break;
637     default:
638         ds_put_format(string, " cmd:%d ", ntohs(ofm->command));
639     }
640     ds_put_format(string, "idle:%d hard:%d pri:%d buf:%#x", 
641             ntohs(ofm->idle_timeout), ntohs(ofm->hard_timeout),
642             ofm->match.wildcards ? ntohs(ofm->priority) : (uint16_t)-1,
643             ntohl(ofm->buffer_id));
644     ofp_print_actions(string, ofm->actions,
645                       len - offsetof(struct ofp_flow_mod, actions));
646     ds_put_char(string, '\n');
647 }
648
649 /* Pretty-print the OFPT_FLOW_EXPIRED packet of 'len' bytes at 'oh' to 'string'
650  * at the given 'verbosity' level. */
651 static void
652 ofp_print_flow_expired(struct ds *string, const void *oh, size_t len, 
653                        int verbosity)
654 {
655     const struct ofp_flow_expired *ofe = oh;
656
657     ofp_print_match(string, &ofe->match, verbosity);
658     ds_put_cstr(string, " reason=");
659     switch (ofe->reason) {
660     case OFPER_IDLE_TIMEOUT:
661         ds_put_cstr(string, "idle");
662         break;
663     case OFPER_HARD_TIMEOUT:
664         ds_put_cstr(string, "hard");
665         break;
666     default:
667         ds_put_format(string, "**%"PRIu8"**", ofe->reason);
668         break;
669     }
670     ds_put_format(string, 
671          " pri%"PRIu16" secs%"PRIu32" pkts%"PRIu64" bytes%"PRIu64"\n", 
672          ofe->match.wildcards ? ntohs(ofe->priority) : (uint16_t)-1,
673          ntohl(ofe->duration), ntohll(ofe->packet_count), 
674          ntohll(ofe->byte_count));
675 }
676
677 static void
678 ofp_print_port_mod(struct ds *string, const void *oh, size_t len,
679                    int verbosity)
680 {
681     const struct ofp_port_mod *opm = oh;
682
683     ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
684             ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr), 
685             ntohl(opm->config), ntohl(opm->mask));
686     ds_put_format(string, " advertised: ");
687     if (opm->advertise) {
688         ofp_print_port_features(string, ntohl(opm->advertise));
689     } else {
690         ds_put_format(string, "Unchanged\n");
691     }
692 }
693
694 struct error_type {
695     int type;
696     int code;
697     const char *name;
698 };
699
700 static const struct error_type error_types[] = {
701 #define ERROR_TYPE(TYPE) {TYPE, -1, #TYPE}
702 #define ERROR_CODE(TYPE, CODE) {TYPE, CODE, #CODE}
703     ERROR_TYPE(OFPET_HELLO_FAILED),
704     ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_INCOMPATIBLE),
705
706     ERROR_TYPE(OFPET_BAD_REQUEST),
707     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
708     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE),
709     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT),
710     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
711 };
712 #define N_ERROR_TYPES ARRAY_SIZE(error_types)
713
714 static const char *
715 lookup_error_type(int type)
716 {
717     const struct error_type *t;
718
719     for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
720         if (t->type == type && t->code == -1) {
721             return t->name;
722         }
723     }
724     return "?";
725 }
726
727 static const char *
728 lookup_error_code(int type, int code)
729 {
730     const struct error_type *t;
731
732     for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
733         if (t->type == type && t->code == code) {
734             return t->name;
735         }
736     }
737     return "?";
738 }
739
740 /* Pretty-print the OFPT_ERROR packet of 'len' bytes at 'oh' to 'string'
741  * at the given 'verbosity' level. */
742 static void
743 ofp_print_error_msg(struct ds *string, const void *oh, size_t len, 
744                        int verbosity)
745 {
746     const struct ofp_error_msg *oem = oh;
747     int type = ntohs(oem->type);
748     int code = ntohs(oem->code);
749     char *s;
750
751     ds_put_format(string, " type%d(%s) code%d(%s) payload:\n",
752                   type, lookup_error_type(type),
753                   code, lookup_error_code(type, code));
754
755     switch (type) {
756     case OFPET_HELLO_FAILED:
757         ds_put_printable(string, (char *) oem->data, len - sizeof *oem);
758         break;
759
760     case OFPET_BAD_REQUEST:
761         s = ofp_to_string(oem->data, len - sizeof *oem, 1);
762         ds_put_cstr(string, s);
763         free(s);
764         break;
765
766     default:
767         ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
768         break;
769     }
770 }
771
772 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'string'
773  * at the given 'verbosity' level. */
774 static void
775 ofp_print_port_status(struct ds *string, const void *oh, size_t len, 
776                       int verbosity)
777 {
778     const struct ofp_port_status *ops = oh;
779
780     if (ops->reason == OFPPR_ADD) {
781         ds_put_format(string, " ADD:");
782     } else if (ops->reason == OFPPR_DELETE) {
783         ds_put_format(string, " DEL:");
784     } else if (ops->reason == OFPPR_MODIFY) {
785         ds_put_format(string, " MOD:");
786     }
787
788     ofp_print_phy_port(string, &ops->desc);
789 }
790
791 static void
792 ofp_desc_stats_reply(struct ds *string, const void *body, size_t len,
793                      int verbosity)
794 {
795     const struct ofp_desc_stats *ods = body;
796
797     ds_put_format(string, "Manufacturer: %s\n", ods->mfr_desc);
798     ds_put_format(string, "Hardware: %s\n", ods->hw_desc);
799     ds_put_format(string, "Software: %s\n", ods->sw_desc);
800     ds_put_format(string, "Serial Num: %s\n", ods->serial_num);
801 }
802
803 static void
804 ofp_flow_stats_request(struct ds *string, const void *oh, size_t len,
805                       int verbosity) 
806 {
807     const struct ofp_flow_stats_request *fsr = oh;
808
809     if (fsr->table_id == 0xff) {
810         ds_put_format(string, " table_id=any, ");
811     } else {
812         ds_put_format(string, " table_id=%"PRIu8", ", fsr->table_id);
813     }
814
815     ofp_print_match(string, &fsr->match, verbosity);
816 }
817
818 static void
819 ofp_flow_stats_reply(struct ds *string, const void *body_, size_t len,
820                      int verbosity)
821 {
822     const char *body = body_;
823     const char *pos = body;
824     for (;;) {
825         const struct ofp_flow_stats *fs;
826         ptrdiff_t bytes_left = body + len - pos;
827         size_t length;
828
829         if (bytes_left < sizeof *fs) {
830             if (bytes_left != 0) {
831                 ds_put_format(string, " ***%td leftover bytes at end***",
832                               bytes_left);
833             }
834             break;
835         }
836
837         fs = (const void *) pos;
838         length = ntohs(fs->length);
839         if (length < sizeof *fs) {
840             ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
841                           length, sizeof *fs);
842             break;
843         } else if (length > bytes_left) {
844             ds_put_format(string,
845                           " ***length=%zu but only %td bytes left***",
846                           length, bytes_left);
847             break;
848         } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
849             ds_put_format(string,
850                           " ***length=%zu has %zu bytes leftover in "
851                           "final action***",
852                           length,
853                           (length - sizeof *fs) % sizeof fs->actions[0]);
854             break;
855         }
856
857         ds_put_format(string, "  duration=%"PRIu32"s, ", ntohl(fs->duration));
858         ds_put_format(string, "table_id=%"PRIu8", ", fs->table_id);
859         ds_put_format(string, "priority=%"PRIu16", ", 
860                     fs->match.wildcards ? ntohs(fs->priority) : (uint16_t)-1);
861         ds_put_format(string, "n_packets=%"PRIu64", ",
862                     ntohll(fs->packet_count));
863         ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
864         ds_put_format(string, "idle_timeout=%"PRIu16",",
865                       ntohs(fs->idle_timeout));
866         ds_put_format(string, "hard_timeout=%"PRIu16",",
867                       ntohs(fs->hard_timeout));
868         ofp_print_match(string, &fs->match, verbosity);
869         ofp_print_actions(string, fs->actions, length - sizeof *fs);
870         ds_put_char(string, '\n');
871
872         pos += length;
873      }
874 }
875
876 static void
877 ofp_aggregate_stats_request(struct ds *string, const void *oh, size_t len,
878                             int verbosity) 
879 {
880     const struct ofp_aggregate_stats_request *asr = oh;
881
882     if (asr->table_id == 0xff) {
883         ds_put_format(string, " table_id=any, ");
884     } else {
885         ds_put_format(string, " table_id=%"PRIu8", ", asr->table_id);
886     }
887
888     ofp_print_match(string, &asr->match, verbosity);
889 }
890
891 static void
892 ofp_aggregate_stats_reply(struct ds *string, const void *body_, size_t len,
893                           int verbosity)
894 {
895     const struct ofp_aggregate_stats_reply *asr = body_;
896
897     ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
898     ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
899     ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
900 }
901
902 static void print_port_stat(struct ds *string, const char *leader, 
903                             uint64_t stat, int more)
904 {
905     ds_put_cstr(string, leader);
906     if (stat != -1) {
907         ds_put_format(string, "%"PRIu64, stat);
908     } else {
909         ds_put_char(string, '?');
910     }
911     if (more) {
912         ds_put_cstr(string, ", ");
913     } else {
914         ds_put_cstr(string, "\n");
915     }
916 }
917
918 static void
919 ofp_port_stats_reply(struct ds *string, const void *body, size_t len,
920                      int verbosity)
921 {
922     const struct ofp_port_stats *ps = body;
923     size_t n = len / sizeof *ps;
924     ds_put_format(string, " %zu ports\n", n);
925     if (verbosity < 1) {
926         return;
927     }
928
929     for (; n--; ps++) {
930         ds_put_format(string, "  port %2"PRIu16": ", ntohs(ps->port_no));
931
932         ds_put_cstr(string, "rx ");
933         print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
934         print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
935         print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
936         print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
937         print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
938         print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
939         print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
940
941         ds_put_cstr(string, "           tx ");
942         print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
943         print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
944         print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
945         print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
946         print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
947     }
948 }
949
950 static void
951 ofp_table_stats_reply(struct ds *string, const void *body, size_t len,
952                      int verbosity)
953 {
954     const struct ofp_table_stats *ts = body;
955     size_t n = len / sizeof *ts;
956     ds_put_format(string, " %zu tables\n", n);
957     if (verbosity < 1) {
958         return;
959     }
960
961     for (; n--; ts++) {
962         char name[OFP_MAX_TABLE_NAME_LEN + 1];
963         strncpy(name, ts->name, sizeof name);
964         name[OFP_MAX_TABLE_NAME_LEN] = '\0';
965
966         ds_put_format(string, "  %d: %-8s: ", ts->table_id, name);
967         ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
968         ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
969         ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
970         ds_put_cstr(string, "               ");
971         ds_put_format(string, "lookup=%"PRIu64", ", 
972                     ntohll(ts->lookup_count));
973         ds_put_format(string, "matched=%"PRIu64"\n",
974                     ntohll(ts->matched_count));
975      }
976 }
977
978 static void
979 vendor_stat(struct ds *string, const void *body, size_t len,
980             int verbosity UNUSED)
981 {
982     ds_put_format(string, " vendor=%08"PRIx32, ntohl(*(uint32_t *) body));
983     ds_put_format(string, " %zu bytes additional data",
984                   len - sizeof(uint32_t));
985 }
986
987 enum stats_direction {
988     REQUEST,
989     REPLY
990 };
991
992 static void
993 print_stats(struct ds *string, int type, const void *body, size_t body_len,
994             int verbosity, enum stats_direction direction)
995 {
996     struct stats_msg {
997         size_t min_body, max_body;
998         void (*printer)(struct ds *, const void *, size_t len, int verbosity);
999     };
1000
1001     struct stats_type {
1002         int type;
1003         const char *name;
1004         struct stats_msg request;
1005         struct stats_msg reply;
1006     };
1007
1008     static const struct stats_type stats_types[] = {
1009         {
1010             OFPST_DESC,
1011             "description",
1012             { 0, 0, NULL },
1013             { 0, SIZE_MAX, ofp_desc_stats_reply },
1014         },
1015         {
1016             OFPST_FLOW,
1017             "flow",
1018             { sizeof(struct ofp_flow_stats_request),
1019               sizeof(struct ofp_flow_stats_request),
1020               ofp_flow_stats_request },
1021             { 0, SIZE_MAX, ofp_flow_stats_reply },
1022         },
1023         {
1024             OFPST_AGGREGATE,
1025             "aggregate",
1026             { sizeof(struct ofp_aggregate_stats_request),
1027               sizeof(struct ofp_aggregate_stats_request),
1028               ofp_aggregate_stats_request },
1029             { sizeof(struct ofp_aggregate_stats_reply),
1030               sizeof(struct ofp_aggregate_stats_reply),
1031               ofp_aggregate_stats_reply },
1032         },
1033         {
1034             OFPST_TABLE,
1035             "table",
1036             { 0, 0, NULL },
1037             { 0, SIZE_MAX, ofp_table_stats_reply },
1038         },
1039         {
1040             OFPST_PORT,
1041             "port",
1042             { 0, 0, NULL, },
1043             { 0, SIZE_MAX, ofp_port_stats_reply },
1044         },
1045         {
1046             OFPST_VENDOR,
1047             "vendor-specific",
1048             { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1049             { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1050         },
1051         {
1052             -1,
1053             "unknown",
1054             { 0, 0, NULL, },
1055             { 0, 0, NULL, },
1056         },
1057     };
1058
1059     const struct stats_type *s;
1060     const struct stats_msg *m;
1061
1062     if (type >= ARRAY_SIZE(stats_types) || !stats_types[type].name) {
1063         ds_put_format(string, " ***unknown type %d***", type);
1064         return;
1065     }
1066     for (s = stats_types; s->type >= 0; s++) {
1067         if (s->type == type) {
1068             break;
1069         }
1070     }
1071     ds_put_format(string, " type=%d(%s)\n", type, s->name);
1072
1073     m = direction == REQUEST ? &s->request : &s->reply;
1074     if (body_len < m->min_body || body_len > m->max_body) {
1075         ds_put_format(string, " ***body_len=%zu not in %zu...%zu***",
1076                       body_len, m->min_body, m->max_body);
1077         return;
1078     }
1079     if (m->printer) {
1080         m->printer(string, body, body_len, verbosity);
1081     }
1082 }
1083
1084 static void
1085 ofp_stats_request(struct ds *string, const void *oh, size_t len, int verbosity)
1086 {
1087     const struct ofp_stats_request *srq = oh;
1088
1089     if (srq->flags) {
1090         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1091                       ntohs(srq->flags));
1092     }
1093
1094     print_stats(string, ntohs(srq->type), srq->body,
1095                 len - offsetof(struct ofp_stats_request, body),
1096                 verbosity, REQUEST);
1097 }
1098
1099 static void
1100 ofp_stats_reply(struct ds *string, const void *oh, size_t len, int verbosity)
1101 {
1102     const struct ofp_stats_reply *srp = oh;
1103
1104     ds_put_cstr(string, " flags=");
1105     if (!srp->flags) {
1106         ds_put_cstr(string, "none");
1107     } else {
1108         uint16_t flags = ntohs(srp->flags);
1109         if (flags & OFPSF_REPLY_MORE) {
1110             ds_put_cstr(string, "[more]");
1111             flags &= ~OFPSF_REPLY_MORE;
1112         }
1113         if (flags) {
1114             ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]", flags);
1115         }
1116     }
1117
1118     print_stats(string, ntohs(srp->type), srp->body,
1119                 len - offsetof(struct ofp_stats_reply, body),
1120                 verbosity, REPLY);
1121 }
1122
1123 static void
1124 ofp_echo(struct ds *string, const void *oh, size_t len, int verbosity)
1125 {
1126     const struct ofp_header *hdr = oh;
1127
1128     ds_put_format(string, " %zu bytes of payload\n", len - sizeof *hdr);
1129     if (verbosity > 1) {
1130         ds_put_hex_dump(string, hdr, len - sizeof *hdr, 0, true); 
1131     }
1132 }
1133
1134 struct openflow_packet {
1135     uint8_t type;
1136     const char *name;
1137     size_t min_size;
1138     void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1139 };
1140
1141 static const struct openflow_packet packets[] = {
1142     {
1143         OFPT_HELLO,
1144         "hello",
1145         sizeof (struct ofp_header),
1146         NULL,
1147     },
1148     {
1149         OFPT_FEATURES_REQUEST,
1150         "features_request",
1151         sizeof (struct ofp_header),
1152         NULL,
1153     },
1154     {
1155         OFPT_FEATURES_REPLY,
1156         "features_reply",
1157         sizeof (struct ofp_switch_features),
1158         ofp_print_switch_features,
1159     },
1160     {
1161         OFPT_GET_CONFIG_REQUEST,
1162         "get_config_request",
1163         sizeof (struct ofp_header),
1164         NULL,
1165     },
1166     {
1167         OFPT_GET_CONFIG_REPLY,
1168         "get_config_reply",
1169         sizeof (struct ofp_switch_config),
1170         ofp_print_switch_config,
1171     },
1172     {
1173         OFPT_SET_CONFIG,
1174         "set_config",
1175         sizeof (struct ofp_switch_config),
1176         ofp_print_switch_config,
1177     },
1178     {
1179         OFPT_PACKET_IN,
1180         "packet_in",
1181         offsetof(struct ofp_packet_in, data),
1182         ofp_packet_in,
1183     },
1184     {
1185         OFPT_PACKET_OUT,
1186         "packet_out",
1187         sizeof (struct ofp_packet_out),
1188         ofp_packet_out,
1189     },
1190     {
1191         OFPT_FLOW_MOD,
1192         "flow_mod",
1193         sizeof (struct ofp_flow_mod),
1194         ofp_print_flow_mod,
1195     },
1196     {
1197         OFPT_FLOW_EXPIRED,
1198         "flow_expired",
1199         sizeof (struct ofp_flow_expired),
1200         ofp_print_flow_expired,
1201     },
1202     {
1203         OFPT_PORT_MOD,
1204         "port_mod",
1205         sizeof (struct ofp_port_mod),
1206         ofp_print_port_mod,
1207     },
1208     {
1209         OFPT_PORT_STATUS,
1210         "port_status",
1211         sizeof (struct ofp_port_status),
1212         ofp_print_port_status
1213     },
1214     {
1215         OFPT_ERROR,
1216         "error_msg",
1217         sizeof (struct ofp_error_msg),
1218         ofp_print_error_msg,
1219     },
1220     {
1221         OFPT_STATS_REQUEST,
1222         "stats_request",
1223         sizeof (struct ofp_stats_request),
1224         ofp_stats_request,
1225     },
1226     {
1227         OFPT_STATS_REPLY,
1228         "stats_reply",
1229         sizeof (struct ofp_stats_reply),
1230         ofp_stats_reply,
1231     },
1232     {
1233         OFPT_ECHO_REQUEST,
1234         "echo_request",
1235         sizeof (struct ofp_header),
1236         ofp_echo,
1237     },
1238     {
1239         OFPT_ECHO_REPLY,
1240         "echo_reply",
1241         sizeof (struct ofp_header),
1242         ofp_echo,
1243     },
1244 };
1245
1246 /* Composes and returns a string representing the OpenFlow packet of 'len'
1247  * bytes at 'oh' at the given 'verbosity' level.  0 is a minimal amount of
1248  * verbosity and higher numbers increase verbosity.  The caller is responsible
1249  * for freeing the string. */
1250 char *
1251 ofp_to_string(const void *oh_, size_t len, int verbosity)
1252 {
1253     struct ds string = DS_EMPTY_INITIALIZER;
1254     const struct ofp_header *oh = oh_;
1255     const struct openflow_packet *pkt;
1256
1257     if (len < sizeof(struct ofp_header)) {
1258         ds_put_cstr(&string, "OpenFlow packet too short:\n");
1259         ds_put_hex_dump(&string, oh, len, 0, true);
1260         return ds_cstr(&string);
1261     } else if (oh->version != OFP_VERSION) {
1262         ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", oh->version);
1263         ds_put_hex_dump(&string, oh, len, 0, true);
1264         return ds_cstr(&string);
1265     }
1266
1267     for (pkt = packets; ; pkt++) {
1268         if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1269             ds_put_format(&string, "Unknown OpenFlow packet type %"PRIu8":\n",
1270                           oh->type);
1271             ds_put_hex_dump(&string, oh, len, 0, true);
1272             return ds_cstr(&string);
1273         } else if (oh->type == pkt->type) {
1274             break;
1275         }
1276     }
1277
1278     ds_put_format(&string, "%s (xid=0x%"PRIx32"):", pkt->name, oh->xid);
1279
1280     if (ntohs(oh->length) > len)
1281         ds_put_format(&string, " (***truncated to %zu bytes from %"PRIu16"***)",
1282                 len, ntohs(oh->length));
1283     else if (ntohs(oh->length) < len) {
1284         ds_put_format(&string, " (***only uses %"PRIu16" bytes out of %zu***)\n",
1285                 ntohs(oh->length), len);
1286         len = ntohs(oh->length);
1287     }
1288
1289     if (len < pkt->min_size) {
1290         ds_put_format(&string, " (***length=%zu < min_size=%zu***)\n",
1291                 len, pkt->min_size);
1292     } else if (!pkt->printer) {
1293         if (len > sizeof *oh) {
1294             ds_put_format(&string, " length=%"PRIu16" (decoder not implemented)\n",
1295                           ntohs(oh->length)); 
1296         }
1297     } else {
1298         pkt->printer(&string, oh, len, verbosity);
1299     }
1300     if (verbosity >= 3) {
1301         ds_put_hex_dump(&string, oh, len, 0, true);
1302     }
1303     if (string.string[string.length - 1] != '\n') {
1304         ds_put_char(&string, '\n');
1305     }
1306     return ds_cstr(&string);
1307 }
1308 \f
1309 static void
1310 print_and_free(FILE *stream, char *string) 
1311 {
1312     fputs(string, stream);
1313     free(string);
1314 }
1315
1316 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1317  * given 'verbosity' level.  0 is a minimal amount of verbosity and higher
1318  * numbers increase verbosity. */
1319 void
1320 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1321 {
1322     print_and_free(stream, ofp_to_string(oh, len, verbosity));
1323 }
1324
1325 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1326  * 'data' to 'stream' using tcpdump.  'total_len' specifies the full length of
1327  * the Ethernet frame (of which 'len' bytes were captured).
1328  *
1329  * This starts and kills a tcpdump subprocess so it's quite expensive. */
1330 void
1331 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1332 {
1333     print_and_free(stream, ofp_packet_to_string(data, len, total_len));
1334 }