Implement a new Nicira extension action for multipath link selection.
[sliver-openvswitch.git] / lib / ofp-print.c
1 /*
2  * Copyright (c) 2008, 2009, 2010 Nicira Networks.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <config.h>
18 #include "ofp-print.h"
19
20 #include <errno.h>
21 #include <inttypes.h>
22 #include <sys/types.h>
23 #include <netinet/in.h>
24 #include <sys/wait.h>
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <ctype.h>
28
29 #include "byte-order.h"
30 #include "compiler.h"
31 #include "dynamic-string.h"
32 #include "flow.h"
33 #include "multipath.h"
34 #include "nx-match.h"
35 #include "ofp-util.h"
36 #include "ofpbuf.h"
37 #include "openflow/openflow.h"
38 #include "openflow/nicira-ext.h"
39 #include "packets.h"
40 #include "pcap.h"
41 #include "type-props.h"
42 #include "util.h"
43
44 static void ofp_print_port_name(struct ds *string, uint16_t port);
45 static void ofp_print_queue_name(struct ds *string, uint32_t port);
46 static void ofp_print_error(struct ds *, int error);
47
48
49 /* Returns a string that represents the contents of the Ethernet frame in the
50  * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
51  * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
52  * bytes were captured).
53  *
54  * The caller must free the returned string.
55  *
56  * This starts and kills a tcpdump subprocess so it's quite expensive. */
57 char *
58 ofp_packet_to_string(const void *data, size_t len, size_t total_len OVS_UNUSED)
59 {
60     struct ds ds = DS_EMPTY_INITIALIZER;
61     struct ofpbuf buf;
62
63     char command[128];
64     FILE *pcap;
65     FILE *tcpdump;
66     int status;
67     int c;
68
69     ofpbuf_use_const(&buf, data, len);
70
71     pcap = tmpfile();
72     if (!pcap) {
73         ovs_error(errno, "tmpfile");
74         return xstrdup("<error>");
75     }
76     pcap_write_header(pcap);
77     pcap_write(pcap, &buf);
78     fflush(pcap);
79     if (ferror(pcap)) {
80         ovs_error(errno, "error writing temporary file");
81     }
82     rewind(pcap);
83
84     snprintf(command, sizeof command, "/usr/sbin/tcpdump -e -n -r /dev/fd/%d 2>/dev/null",
85              fileno(pcap));
86     tcpdump = popen(command, "r");
87     fclose(pcap);
88     if (!tcpdump) {
89         ovs_error(errno, "exec(\"%s\")", command);
90         return xstrdup("<error>");
91     }
92
93     while ((c = getc(tcpdump)) != EOF) {
94         ds_put_char(&ds, c);
95     }
96
97     status = pclose(tcpdump);
98     if (WIFEXITED(status)) {
99         if (WEXITSTATUS(status))
100             ovs_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
101     } else if (WIFSIGNALED(status)) {
102         ovs_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
103     }
104     return ds_cstr(&ds);
105 }
106
107 static void
108 ofp_print_packet_in(struct ds *string, const struct ofp_packet_in *op,
109                     int verbosity)
110 {
111     size_t len = ntohs(op->header.length);
112     size_t data_len;
113
114     ds_put_format(string, " total_len=%"PRIu16" in_port=",
115                   ntohs(op->total_len));
116     ofp_print_port_name(string, ntohs(op->in_port));
117
118     if (op->reason == OFPR_ACTION)
119         ds_put_cstr(string, " (via action)");
120     else if (op->reason != OFPR_NO_MATCH)
121         ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
122
123     data_len = len - offsetof(struct ofp_packet_in, data);
124     ds_put_format(string, " data_len=%zu", data_len);
125     if (htonl(op->buffer_id) == UINT32_MAX) {
126         ds_put_format(string, " (unbuffered)");
127         if (ntohs(op->total_len) != data_len)
128             ds_put_format(string, " (***total_len != data_len***)");
129     } else {
130         ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
131         if (ntohs(op->total_len) < data_len)
132             ds_put_format(string, " (***total_len < data_len***)");
133     }
134     ds_put_char(string, '\n');
135
136     if (verbosity > 0) {
137         struct flow flow;
138         struct ofpbuf packet;
139
140         ofpbuf_use_const(&packet, op->data, data_len);
141         flow_extract(&packet, 0, ntohs(op->in_port), &flow);
142         flow_format(string, &flow);
143         ds_put_char(string, '\n');
144     }
145     if (verbosity > 1) {
146         char *packet = ofp_packet_to_string(op->data, data_len,
147                                             ntohs(op->total_len));
148         ds_put_cstr(string, packet);
149         free(packet);
150     }
151 }
152
153 static void ofp_print_port_name(struct ds *string, uint16_t port)
154 {
155     const char *name;
156     switch (port) {
157     case OFPP_IN_PORT:
158         name = "IN_PORT";
159         break;
160     case OFPP_TABLE:
161         name = "TABLE";
162         break;
163     case OFPP_NORMAL:
164         name = "NORMAL";
165         break;
166     case OFPP_FLOOD:
167         name = "FLOOD";
168         break;
169     case OFPP_ALL:
170         name = "ALL";
171         break;
172     case OFPP_CONTROLLER:
173         name = "CONTROLLER";
174         break;
175     case OFPP_LOCAL:
176         name = "LOCAL";
177         break;
178     case OFPP_NONE:
179         name = "NONE";
180         break;
181     default:
182         ds_put_format(string, "%"PRIu16, port);
183         return;
184     }
185     ds_put_cstr(string, name);
186 }
187
188
189 static void
190 print_note(struct ds *string, const struct nx_action_note *nan)
191 {
192     size_t len;
193     size_t i;
194
195     ds_put_cstr(string, "note:");
196     len = ntohs(nan->len) - offsetof(struct nx_action_note, note);
197     for (i = 0; i < len; i++) {
198         if (i) {
199             ds_put_char(string, '.');
200         }
201         ds_put_format(string, "%02"PRIx8, nan->note[i]);
202     }
203 }
204
205 static int
206 nx_action_len(enum nx_action_subtype subtype)
207 {
208     switch (subtype) {
209     case NXAST_SNAT__OBSOLETE: return -1;
210     case NXAST_RESUBMIT: return sizeof(struct nx_action_resubmit);
211     case NXAST_SET_TUNNEL: return sizeof(struct nx_action_set_tunnel);
212     case NXAST_DROP_SPOOFED_ARP:
213         return sizeof(struct nx_action_drop_spoofed_arp);
214     case NXAST_SET_QUEUE: return sizeof(struct nx_action_set_queue);
215     case NXAST_POP_QUEUE: return sizeof(struct nx_action_pop_queue);
216     case NXAST_REG_MOVE: return sizeof(struct nx_action_reg_move);
217     case NXAST_REG_LOAD: return sizeof(struct nx_action_reg_load);
218     case NXAST_NOTE: return -1;
219     case NXAST_SET_TUNNEL64: return sizeof(struct nx_action_set_tunnel64);
220     case NXAST_MULTIPATH: return sizeof(struct nx_action_multipath);
221     default: return -1;
222     }
223 }
224
225 static void
226 ofp_print_nx_action(struct ds *string, const struct nx_action_header *nah)
227 {
228     uint16_t subtype = ntohs(nah->subtype);
229     int required_len = nx_action_len(subtype);
230     int len = ntohs(nah->len);
231
232     if (required_len != -1 && required_len != len) {
233         ds_put_format(string, "***Nicira action %"PRIu16" wrong length: %d***",
234                       subtype, len);
235         return;
236     }
237
238     if (subtype <= TYPE_MAXIMUM(enum nx_action_subtype)) {
239         const struct nx_action_set_tunnel64 *nast64;
240         const struct nx_action_set_tunnel *nast;
241         const struct nx_action_set_queue *nasq;
242         const struct nx_action_resubmit *nar;
243         const struct nx_action_reg_move *move;
244         const struct nx_action_reg_load *load;
245         const struct nx_action_multipath *nam;
246
247         switch ((enum nx_action_subtype) subtype) {
248         case NXAST_RESUBMIT:
249             nar = (struct nx_action_resubmit *)nah;
250             ds_put_format(string, "resubmit:");
251             ofp_print_port_name(string, ntohs(nar->in_port));
252             return;
253
254         case NXAST_SET_TUNNEL:
255             nast = (struct nx_action_set_tunnel *)nah;
256             ds_put_format(string, "set_tunnel:%#"PRIx32, ntohl(nast->tun_id));
257             return;
258
259         case NXAST_DROP_SPOOFED_ARP:
260             ds_put_cstr(string, "drop_spoofed_arp");
261             return;
262
263         case NXAST_SET_QUEUE:
264             nasq = (struct nx_action_set_queue *)nah;
265             ds_put_format(string, "set_queue:%u", ntohl(nasq->queue_id));
266             return;
267
268         case NXAST_POP_QUEUE:
269             ds_put_cstr(string, "pop_queue");
270             return;
271
272         case NXAST_NOTE:
273             print_note(string, (const struct nx_action_note *) nah);
274             return;
275
276         case NXAST_REG_MOVE:
277             move = (const struct nx_action_reg_move *) nah;
278             nxm_format_reg_move(move, string);
279             return;
280
281         case NXAST_REG_LOAD:
282             load = (const struct nx_action_reg_load *) nah;
283             nxm_format_reg_load(load, string);
284             return;
285
286         case NXAST_SET_TUNNEL64:
287             nast64 = (const struct nx_action_set_tunnel64 *) nah;
288             ds_put_format(string, "set_tunnel64:%#"PRIx64,
289                           ntohll(nast64->tun_id));
290             return;
291
292         case NXAST_MULTIPATH:
293             nam = (const struct nx_action_multipath *) nah;
294             multipath_format(nam, string);
295             return;
296
297         case NXAST_SNAT__OBSOLETE:
298         default:
299             break;
300         }
301     }
302
303     ds_put_format(string, "***unknown Nicira action:%"PRIu16"***", subtype);
304 }
305
306 static int
307 ofp_action_len(enum ofp_action_type type)
308 {
309     switch (type) {
310     case OFPAT_OUTPUT: return sizeof(struct ofp_action_output);
311     case OFPAT_SET_VLAN_VID: return sizeof(struct ofp_action_vlan_vid);
312     case OFPAT_SET_VLAN_PCP: return sizeof(struct ofp_action_vlan_pcp);
313     case OFPAT_STRIP_VLAN: return sizeof(struct ofp_action_header);
314     case OFPAT_SET_DL_SRC: return sizeof(struct ofp_action_dl_addr);
315     case OFPAT_SET_DL_DST: return sizeof(struct ofp_action_dl_addr);
316     case OFPAT_SET_NW_SRC: return sizeof(struct ofp_action_nw_addr);
317     case OFPAT_SET_NW_DST: return sizeof(struct ofp_action_nw_addr);
318     case OFPAT_SET_NW_TOS: return sizeof(struct ofp_action_nw_tos);
319     case OFPAT_SET_TP_SRC: return sizeof(struct ofp_action_tp_port);
320     case OFPAT_SET_TP_DST: return sizeof(struct ofp_action_tp_port);
321     case OFPAT_ENQUEUE: return sizeof(struct ofp_action_enqueue);
322     case OFPAT_VENDOR: return -1;
323     default: return -1;
324     }
325 }
326
327 static int
328 ofp_print_action(struct ds *string, const struct ofp_action_header *ah,
329         size_t actions_len)
330 {
331     enum ofp_action_type type;
332     int required_len;
333     size_t len;
334
335     if (actions_len < sizeof *ah) {
336         ds_put_format(string, "***action array too short for next action***\n");
337         return -1;
338     }
339
340     type = ntohs(ah->type);
341     len = ntohs(ah->len);
342     if (actions_len < len) {
343         ds_put_format(string, "***truncated action %d***\n", (int) type);
344         return -1;
345     }
346
347     if (!len) {
348         ds_put_format(string, "***zero-length action***\n");
349         return 8;
350     }
351
352     if ((len % OFP_ACTION_ALIGN) != 0) {
353         ds_put_format(string,
354                       "***action %d length not a multiple of %d***\n",
355                       (int) type, OFP_ACTION_ALIGN);
356         return -1;
357     }
358
359     required_len = ofp_action_len(type);
360     if (required_len >= 0 && len != required_len) {
361         ds_put_format(string,
362                       "***action %d wrong length: %zu***\n", (int) type, len);
363         return -1;
364     }
365
366     switch (type) {
367     case OFPAT_OUTPUT: {
368         struct ofp_action_output *oa = (struct ofp_action_output *)ah;
369         uint16_t port = ntohs(oa->port);
370         if (port < OFPP_MAX) {
371             ds_put_format(string, "output:%"PRIu16, port);
372         } else {
373             ofp_print_port_name(string, port);
374             if (port == OFPP_CONTROLLER) {
375                 if (oa->max_len) {
376                     ds_put_format(string, ":%"PRIu16, ntohs(oa->max_len));
377                 } else {
378                     ds_put_cstr(string, ":all");
379                 }
380             }
381         }
382         break;
383     }
384
385     case OFPAT_ENQUEUE: {
386         struct ofp_action_enqueue *ea = (struct ofp_action_enqueue *)ah;
387         unsigned int port = ntohs(ea->port);
388         unsigned int queue_id = ntohl(ea->queue_id);
389         ds_put_format(string, "enqueue:");
390         if (port != OFPP_IN_PORT) {
391             ds_put_format(string, "%u", port);
392         } else {
393             ds_put_cstr(string, "IN_PORT");
394         }
395         ds_put_format(string, "q%u", queue_id);
396         break;
397     }
398
399     case OFPAT_SET_VLAN_VID: {
400         struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
401         ds_put_format(string, "mod_vlan_vid:%"PRIu16, ntohs(va->vlan_vid));
402         break;
403     }
404
405     case OFPAT_SET_VLAN_PCP: {
406         struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
407         ds_put_format(string, "mod_vlan_pcp:%"PRIu8, va->vlan_pcp);
408         break;
409     }
410
411     case OFPAT_STRIP_VLAN:
412         ds_put_cstr(string, "strip_vlan");
413         break;
414
415     case OFPAT_SET_DL_SRC: {
416         struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
417         ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
418                 ETH_ADDR_ARGS(da->dl_addr));
419         break;
420     }
421
422     case OFPAT_SET_DL_DST: {
423         struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
424         ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
425                 ETH_ADDR_ARGS(da->dl_addr));
426         break;
427     }
428
429     case OFPAT_SET_NW_SRC: {
430         struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
431         ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&na->nw_addr));
432         break;
433     }
434
435     case OFPAT_SET_NW_DST: {
436         struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
437         ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&na->nw_addr));
438         break;
439     }
440
441     case OFPAT_SET_NW_TOS: {
442         struct ofp_action_nw_tos *nt = (struct ofp_action_nw_tos *)ah;
443         ds_put_format(string, "mod_nw_tos:%d", nt->nw_tos);
444         break;
445     }
446
447     case OFPAT_SET_TP_SRC: {
448         struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
449         ds_put_format(string, "mod_tp_src:%d", ntohs(ta->tp_port));
450         break;
451     }
452
453     case OFPAT_SET_TP_DST: {
454         struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
455         ds_put_format(string, "mod_tp_dst:%d", ntohs(ta->tp_port));
456         break;
457     }
458
459     case OFPAT_VENDOR: {
460         struct ofp_action_vendor_header *avh
461                 = (struct ofp_action_vendor_header *)ah;
462         if (len < sizeof *avh) {
463             ds_put_format(string, "***ofpat_vendor truncated***\n");
464             return -1;
465         }
466         if (avh->vendor == htonl(NX_VENDOR_ID)) {
467             ofp_print_nx_action(string, (struct nx_action_header *)avh);
468         } else {
469             ds_put_format(string, "vendor action:0x%x", ntohl(avh->vendor));
470         }
471         break;
472     }
473
474     default:
475         ds_put_format(string, "(decoder %d not implemented)", (int) type);
476         break;
477     }
478
479     return len;
480 }
481
482 void
483 ofp_print_actions(struct ds *string, const struct ofp_action_header *action,
484                   size_t actions_len)
485 {
486     uint8_t *p = (uint8_t *)action;
487     int len = 0;
488
489     ds_put_cstr(string, "actions=");
490     if (!actions_len) {
491         ds_put_cstr(string, "drop");
492     }
493     while (actions_len > 0) {
494         if (len) {
495             ds_put_cstr(string, ",");
496         }
497         len = ofp_print_action(string, (struct ofp_action_header *)p,
498                 actions_len);
499         if (len < 0) {
500             return;
501         }
502         p += len;
503         actions_len -= len;
504     }
505 }
506
507 static void
508 ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
509                      int verbosity)
510 {
511     size_t len = ntohs(opo->header.length);
512     size_t actions_len = ntohs(opo->actions_len);
513
514     ds_put_cstr(string, " in_port=");
515     ofp_print_port_name(string, ntohs(opo->in_port));
516
517     ds_put_format(string, " actions_len=%zu ", actions_len);
518     if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
519         ds_put_format(string, "***packet too short for action length***\n");
520         return;
521     }
522     ofp_print_actions(string, opo->actions, actions_len);
523
524     if (ntohl(opo->buffer_id) == UINT32_MAX) {
525         int data_len = len - sizeof *opo - actions_len;
526         ds_put_format(string, " data_len=%d", data_len);
527         if (verbosity > 0 && len > sizeof *opo) {
528             char *packet = ofp_packet_to_string(
529                     (uint8_t *)opo->actions + actions_len, data_len, data_len);
530             ds_put_char(string, '\n');
531             ds_put_cstr(string, packet);
532             free(packet);
533         }
534     } else {
535         ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
536     }
537     ds_put_char(string, '\n');
538 }
539
540 /* qsort comparison function. */
541 static int
542 compare_ports(const void *a_, const void *b_)
543 {
544     const struct ofp_phy_port *a = a_;
545     const struct ofp_phy_port *b = b_;
546     uint16_t ap = ntohs(a->port_no);
547     uint16_t bp = ntohs(b->port_no);
548
549     return ap < bp ? -1 : ap > bp;
550 }
551
552 static void ofp_print_port_features(struct ds *string, uint32_t features)
553 {
554     if (features == 0) {
555         ds_put_cstr(string, "Unsupported\n");
556         return;
557     }
558     if (features & OFPPF_10MB_HD) {
559         ds_put_cstr(string, "10MB-HD ");
560     }
561     if (features & OFPPF_10MB_FD) {
562         ds_put_cstr(string, "10MB-FD ");
563     }
564     if (features & OFPPF_100MB_HD) {
565         ds_put_cstr(string, "100MB-HD ");
566     }
567     if (features & OFPPF_100MB_FD) {
568         ds_put_cstr(string, "100MB-FD ");
569     }
570     if (features & OFPPF_1GB_HD) {
571         ds_put_cstr(string, "1GB-HD ");
572     }
573     if (features & OFPPF_1GB_FD) {
574         ds_put_cstr(string, "1GB-FD ");
575     }
576     if (features & OFPPF_10GB_FD) {
577         ds_put_cstr(string, "10GB-FD ");
578     }
579     if (features & OFPPF_COPPER) {
580         ds_put_cstr(string, "COPPER ");
581     }
582     if (features & OFPPF_FIBER) {
583         ds_put_cstr(string, "FIBER ");
584     }
585     if (features & OFPPF_AUTONEG) {
586         ds_put_cstr(string, "AUTO_NEG ");
587     }
588     if (features & OFPPF_PAUSE) {
589         ds_put_cstr(string, "AUTO_PAUSE ");
590     }
591     if (features & OFPPF_PAUSE_ASYM) {
592         ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
593     }
594     ds_put_char(string, '\n');
595 }
596
597 static void
598 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
599 {
600     char name[OFP_MAX_PORT_NAME_LEN];
601     int j;
602
603     memcpy(name, port->name, sizeof name);
604     for (j = 0; j < sizeof name - 1; j++) {
605         if (!isprint(name[j])) {
606             break;
607         }
608     }
609     name[j] = '\0';
610
611     ds_put_char(string, ' ');
612     ofp_print_port_name(string, ntohs(port->port_no));
613     ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
614             name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
615             ntohl(port->state));
616     if (port->curr) {
617         ds_put_format(string, "     current:    ");
618         ofp_print_port_features(string, ntohl(port->curr));
619     }
620     if (port->advertised) {
621         ds_put_format(string, "     advertised: ");
622         ofp_print_port_features(string, ntohl(port->advertised));
623     }
624     if (port->supported) {
625         ds_put_format(string, "     supported:  ");
626         ofp_print_port_features(string, ntohl(port->supported));
627     }
628     if (port->peer) {
629         ds_put_format(string, "     peer:       ");
630         ofp_print_port_features(string, ntohl(port->peer));
631     }
632 }
633
634 static void
635 ofp_print_switch_features(struct ds *string,
636                           const struct ofp_switch_features *osf)
637 {
638     size_t len = ntohs(osf->header.length);
639     struct ofp_phy_port *port_list;
640     int n_ports;
641     int i;
642
643     ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
644             osf->header.version, ntohll(osf->datapath_id));
645     ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
646             ntohl(osf->n_buffers));
647     ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
648            ntohl(osf->capabilities), ntohl(osf->actions));
649
650     n_ports = (len - sizeof *osf) / sizeof *osf->ports;
651
652     port_list = xmemdup(osf->ports, len - sizeof *osf);
653     qsort(port_list, n_ports, sizeof *port_list, compare_ports);
654     for (i = 0; i < n_ports; i++) {
655         ofp_print_phy_port(string, &port_list[i]);
656     }
657     free(port_list);
658 }
659
660 static void
661 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
662 {
663     uint16_t flags;
664
665     flags = ntohs(osc->flags);
666     if (flags) {
667         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
668     }
669
670     ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
671 }
672
673 static void print_wild(struct ds *string, const char *leader, int is_wild,
674             int verbosity, const char *format, ...)
675             __attribute__((format(printf, 5, 6)));
676
677 static void print_wild(struct ds *string, const char *leader, int is_wild,
678                        int verbosity, const char *format, ...)
679 {
680     if (is_wild && verbosity < 2) {
681         return;
682     }
683     ds_put_cstr(string, leader);
684     if (!is_wild) {
685         va_list args;
686
687         va_start(args, format);
688         ds_put_format_valist(string, format, args);
689         va_end(args);
690     } else {
691         ds_put_char(string, '*');
692     }
693     ds_put_char(string, ',');
694 }
695
696 static void
697 print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
698                  uint32_t wild_bits, int verbosity)
699 {
700     if (wild_bits >= 32 && verbosity < 2) {
701         return;
702     }
703     ds_put_cstr(string, leader);
704     if (wild_bits < 32) {
705         ds_put_format(string, IP_FMT, IP_ARGS(&ip));
706         if (wild_bits) {
707             ds_put_format(string, "/%d", 32 - wild_bits);
708         }
709     } else {
710         ds_put_char(string, '*');
711     }
712     ds_put_char(string, ',');
713 }
714
715 void
716 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
717 {
718     char *s = ofp_match_to_string(om, verbosity);
719     ds_put_cstr(f, s);
720     free(s);
721 }
722
723 char *
724 ofp_match_to_string(const struct ofp_match *om, int verbosity)
725 {
726     struct ds f = DS_EMPTY_INITIALIZER;
727     uint32_t w = ntohl(om->wildcards);
728     bool skip_type = false;
729     bool skip_proto = false;
730
731     if (!(w & OFPFW_DL_TYPE)) {
732         skip_type = true;
733         if (om->dl_type == htons(ETH_TYPE_IP)) {
734             if (!(w & OFPFW_NW_PROTO)) {
735                 skip_proto = true;
736                 if (om->nw_proto == IP_TYPE_ICMP) {
737                     ds_put_cstr(&f, "icmp,");
738                 } else if (om->nw_proto == IP_TYPE_TCP) {
739                     ds_put_cstr(&f, "tcp,");
740                 } else if (om->nw_proto == IP_TYPE_UDP) {
741                     ds_put_cstr(&f, "udp,");
742                 } else {
743                     ds_put_cstr(&f, "ip,");
744                     skip_proto = false;
745                 }
746             } else {
747                 ds_put_cstr(&f, "ip,");
748             }
749         } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
750             ds_put_cstr(&f, "arp,");
751         } else {
752             skip_type = false;
753         }
754     }
755     if (w & NXFW_TUN_ID) {
756         ds_put_cstr(&f, "tun_id_wild,");
757     }
758     print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
759                "%d", ntohs(om->in_port));
760     print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
761                "%d", ntohs(om->dl_vlan));
762     print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
763                "%d", om->dl_vlan_pcp);
764     print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
765                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
766     print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
767                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
768     if (!skip_type) {
769         print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
770                    "0x%04x", ntohs(om->dl_type));
771     }
772     print_ip_netmask(&f, "nw_src=", om->nw_src,
773                      (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
774     print_ip_netmask(&f, "nw_dst=", om->nw_dst,
775                      (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
776     if (!skip_proto) {
777         if (om->dl_type == htons(ETH_TYPE_ARP)) {
778             print_wild(&f, "opcode=", w & OFPFW_NW_PROTO, verbosity,
779                        "%u", om->nw_proto);
780         } else {
781             print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
782                        "%u", om->nw_proto);
783         }
784     }
785     print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
786                "%u", om->nw_tos);
787     if (om->nw_proto == IP_TYPE_ICMP) {
788         print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
789                    "%d", ntohs(om->icmp_type));
790         print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
791                    "%d", ntohs(om->icmp_code));
792     } else {
793         print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
794                    "%d", ntohs(om->tp_src));
795         print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
796                    "%d", ntohs(om->tp_dst));
797     }
798     if (ds_last(&f) == ',') {
799         f.length--;
800     }
801     return ds_cstr(&f);
802 }
803
804 static void
805 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
806                    enum ofputil_msg_code code, int verbosity)
807 {
808     struct flow_mod fm;
809     bool need_priority;
810     int error;
811
812     error = ofputil_decode_flow_mod(&fm, oh, NXFF_OPENFLOW10);
813     if (error) {
814         ofp_print_error(s, error);
815         return;
816     }
817
818     ds_put_char(s, ' ');
819     switch (fm.command) {
820     case OFPFC_ADD:
821         ds_put_cstr(s, "ADD");
822         break;
823     case OFPFC_MODIFY:
824         ds_put_cstr(s, "MOD");
825         break;
826     case OFPFC_MODIFY_STRICT:
827         ds_put_cstr(s, "MOD_STRICT");
828         break;
829     case OFPFC_DELETE:
830         ds_put_cstr(s, "DEL");
831         break;
832     case OFPFC_DELETE_STRICT:
833         ds_put_cstr(s, "DEL_STRICT");
834         break;
835     default:
836         ds_put_format(s, "cmd:%d", fm.command);
837     }
838
839     ds_put_char(s, ' ');
840     if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
841         const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
842         ofp_print_match(s, &ofm->match, verbosity);
843
844         /* ofp_print_match() doesn't print priority. */
845         need_priority = true;
846     } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
847         const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
848         const void *nxm = nfm + 1;
849         char *nxm_s;
850
851         nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
852         ds_put_cstr(s, nxm_s);
853         free(nxm_s);
854
855         /* nx_match_to_string() doesn't print priority. */
856         need_priority = true;
857     } else {
858         cls_rule_format(&fm.cr, s);
859
860         /* cls_rule_format() does print priority. */
861         need_priority = false;
862     }
863
864     if (ds_last(s) != ' ') {
865         ds_put_char(s, ' ');
866     }
867     if (fm.cookie != htonll(0)) {
868         ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.cookie));
869     }
870     if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
871         ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
872     }
873     if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
874         ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
875     }
876     if (fm.cr.priority != OFP_DEFAULT_PRIORITY && need_priority) {
877         ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
878     }
879     if (fm.buffer_id != UINT32_MAX) {
880         ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
881     }
882     if (fm.flags != 0) {
883         ds_put_format(s, "flags:0x%"PRIx16" ", fm.flags);
884     }
885
886     ofp_print_actions(s, (const struct ofp_action_header *) fm.actions,
887                       fm.n_actions * sizeof *fm.actions);
888 }
889
890 static void
891 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
892 {
893     ds_put_format(string, "%u", sec);
894     if (nsec > 0) {
895         ds_put_format(string, ".%09u", nsec);
896         while (string->string[string->length - 1] == '0') {
897             string->length--;
898         }
899     }
900     ds_put_char(string, 's');
901 }
902
903 static void
904 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
905 {
906     struct ofputil_flow_removed fr;
907     int error;
908
909     error = ofputil_decode_flow_removed(&fr, oh, NXFF_OPENFLOW10);
910     if (error) {
911         ofp_print_error(string, error);
912         return;
913     }
914
915     cls_rule_format(&fr.rule, string);
916
917     ds_put_cstr(string, " reason=");
918     switch (fr.reason) {
919     case OFPRR_IDLE_TIMEOUT:
920         ds_put_cstr(string, "idle");
921         break;
922     case OFPRR_HARD_TIMEOUT:
923         ds_put_cstr(string, "hard");
924         break;
925     case OFPRR_DELETE:
926         ds_put_cstr(string, "delete");
927         break;
928     default:
929         ds_put_format(string, "**%"PRIu8"**", fr.reason);
930         break;
931     }
932
933     if (fr.cookie != htonll(0)) {
934         ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
935     }
936     ds_put_cstr(string, " duration");
937     ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
938     ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
939          fr.idle_timeout, fr.packet_count, fr.byte_count);
940 }
941
942 static void
943 ofp_print_port_mod(struct ds *string, const struct ofp_port_mod *opm)
944 {
945     ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
946             ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
947             ntohl(opm->config), ntohl(opm->mask));
948     ds_put_format(string, "     advertise: ");
949     if (opm->advertise) {
950         ofp_print_port_features(string, ntohl(opm->advertise));
951     } else {
952         ds_put_format(string, "UNCHANGED\n");
953     }
954 }
955
956 struct error_type {
957     int type;
958     int code;
959     const char *name;
960 };
961
962 static const struct error_type error_types[] = {
963 #define ERROR_TYPE(TYPE) {TYPE, -1, #TYPE}
964 #define ERROR_CODE(TYPE, CODE) {TYPE, CODE, #CODE}
965     ERROR_TYPE(OFPET_HELLO_FAILED),
966     ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_INCOMPATIBLE),
967     ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_EPERM),
968
969     ERROR_TYPE(OFPET_BAD_REQUEST),
970     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
971     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE),
972     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT),
973     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR),
974     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE),
975     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_EPERM),
976     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN),
977     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_EMPTY),
978     ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_UNKNOWN),
979     /* Nicira error extensions. */
980     ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_INVALID),
981     ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_TYPE),
982     ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_VALUE),
983     ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_MASK),
984     ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_PREREQ),
985     ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_DUP_TYPE),
986
987     ERROR_TYPE(OFPET_BAD_ACTION),
988     ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE),
989     ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_LEN),
990     ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR),
991     ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE),
992     ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT),
993     ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT),
994     ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_EPERM),
995     ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_TOO_MANY),
996
997     ERROR_TYPE(OFPET_FLOW_MOD_FAILED),
998     ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL),
999     ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP),
1000     ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_EPERM),
1001     ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_EMERG_TIMEOUT),
1002     ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND),
1003     /* Nicira error extenstions. */
1004     ERROR_CODE(OFPET_FLOW_MOD_FAILED, NXFMFC_HARDWARE),
1005     ERROR_CODE(OFPET_FLOW_MOD_FAILED, NXFMFC_BAD_TABLE_ID),
1006
1007     ERROR_TYPE(OFPET_PORT_MOD_FAILED),
1008     ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT),
1009     ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR)
1010 };
1011 #define N_ERROR_TYPES ARRAY_SIZE(error_types)
1012
1013 static const char *
1014 lookup_error_type(int type)
1015 {
1016     const struct error_type *t;
1017
1018     for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
1019         if (t->type == type && t->code == -1) {
1020             return t->name;
1021         }
1022     }
1023     return "?";
1024 }
1025
1026 static const char *
1027 lookup_error_code(int type, int code)
1028 {
1029     const struct error_type *t;
1030
1031     for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
1032         if (t->type == type && t->code == code) {
1033             return t->name;
1034         }
1035     }
1036     return "?";
1037 }
1038
1039 static void
1040 ofp_print_error(struct ds *string, int error)
1041 {
1042     int type = get_ofp_err_type(error);
1043     int code = get_ofp_err_code(error);
1044     if (string->length) {
1045         ds_put_char(string, ' ');
1046     }
1047     ds_put_format(string, " ***decode error type:%d(%s) code:%d(%s)***\n",
1048                   type, lookup_error_type(type),
1049                   code, lookup_error_code(type, code));
1050 }
1051
1052 static void
1053 ofp_print_nx_error_msg(struct ds *string, const struct ofp_error_msg *oem)
1054 {
1055     size_t len = ntohs(oem->header.length);
1056     const struct nx_vendor_error *nve = (struct nx_vendor_error *)oem->data;
1057     int vendor = ntohl(nve->vendor);
1058     int type = ntohs(nve->type);
1059     int code = ntohs(nve->code);
1060
1061     ds_put_format(string, " vendor:%x type:%d(%s) code:%d(%s) payload:\n",
1062                   vendor,
1063                   type, lookup_error_type(type),
1064                   code, lookup_error_code(type, code));
1065
1066     ds_put_hex_dump(string, nve + 1, len - sizeof *oem - sizeof *nve,
1067                     0, true);
1068 }
1069
1070 static void
1071 ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
1072 {
1073     size_t len = ntohs(oem->header.length);
1074     int type = ntohs(oem->type);
1075     int code = ntohs(oem->code);
1076     char *s;
1077
1078
1079     if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
1080         if (len < sizeof *oem + sizeof(struct nx_vendor_error)) {
1081             ds_put_format(string,
1082                           "(***truncated extended error message is %zu bytes "
1083                           "when it should be at least %zu***)\n",
1084                           len, sizeof(struct nx_vendor_error));
1085             return;
1086         }
1087
1088         return ofp_print_nx_error_msg(string, oem);
1089     }
1090
1091     ds_put_format(string, " type:%d(%s) code:%d(%s) payload:\n",
1092                   type, lookup_error_type(type),
1093                   code, lookup_error_code(type, code));
1094
1095     switch (type) {
1096     case OFPET_HELLO_FAILED:
1097         ds_put_printable(string, (char *) oem->data, len - sizeof *oem);
1098         break;
1099
1100     case OFPET_BAD_REQUEST:
1101         s = ofp_to_string(oem->data, len - sizeof *oem, 1);
1102         ds_put_cstr(string, s);
1103         free(s);
1104         break;
1105
1106     default:
1107         ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
1108         break;
1109     }
1110 }
1111
1112 static void
1113 ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
1114 {
1115     if (ops->reason == OFPPR_ADD) {
1116         ds_put_format(string, " ADD:");
1117     } else if (ops->reason == OFPPR_DELETE) {
1118         ds_put_format(string, " DEL:");
1119     } else if (ops->reason == OFPPR_MODIFY) {
1120         ds_put_format(string, " MOD:");
1121     }
1122
1123     ofp_print_phy_port(string, &ops->desc);
1124 }
1125
1126 static void
1127 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
1128 {
1129     const struct ofp_desc_stats *ods = ofputil_stats_body(oh);
1130
1131     ds_put_format(string, "Manufacturer: %.*s\n",
1132             (int) sizeof ods->mfr_desc, ods->mfr_desc);
1133     ds_put_format(string, "Hardware: %.*s\n",
1134             (int) sizeof ods->hw_desc, ods->hw_desc);
1135     ds_put_format(string, "Software: %.*s\n",
1136             (int) sizeof ods->sw_desc, ods->sw_desc);
1137     ds_put_format(string, "Serial Num: %.*s\n",
1138             (int) sizeof ods->serial_num, ods->serial_num);
1139     ds_put_format(string, "DP Description: %.*s\n",
1140             (int) sizeof ods->dp_desc, ods->dp_desc);
1141 }
1142
1143 static void
1144 ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
1145 {
1146     struct flow_stats_request fsr;
1147     int error;
1148
1149     error = ofputil_decode_flow_stats_request(&fsr, oh, NXFF_OPENFLOW10);
1150     if (error) {
1151         ofp_print_error(string, error);
1152         return;
1153     }
1154
1155     if (fsr.table_id != 0xff) {
1156         ds_put_format(string, " table_id=%"PRIu8, fsr.table_id);
1157     }
1158
1159     if (fsr.out_port != OFPP_NONE) {
1160         ds_put_cstr(string, " out_port=");
1161         ofp_print_port_name(string, fsr.out_port);
1162     }
1163
1164     ds_put_char(string, ' ');
1165     cls_rule_format(&fsr.match, string);
1166 }
1167
1168 static void
1169 ofp_print_ofpst_flow_reply(struct ds *string, const struct ofp_header *oh,
1170                            int verbosity)
1171 {
1172     size_t len = ofputil_stats_body_len(oh);
1173     const char *body = ofputil_stats_body(oh);
1174     const char *pos = body;
1175     for (;;) {
1176         const struct ofp_flow_stats *fs;
1177         ptrdiff_t bytes_left = body + len - pos;
1178         size_t length;
1179
1180         ds_put_char(string, '\n');
1181
1182         if (bytes_left < sizeof *fs) {
1183             if (bytes_left != 0) {
1184                 ds_put_format(string, " ***%td leftover bytes at end***",
1185                               bytes_left);
1186             }
1187             break;
1188         }
1189
1190         fs = (const void *) pos;
1191         length = ntohs(fs->length);
1192         if (length < sizeof *fs) {
1193             ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
1194                           length, sizeof *fs);
1195             break;
1196         } else if (length > bytes_left) {
1197             ds_put_format(string,
1198                           " ***length=%zu but only %td bytes left***",
1199                           length, bytes_left);
1200             break;
1201         } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
1202             ds_put_format(string,
1203                           " ***length=%zu has %zu bytes leftover in "
1204                           "final action***",
1205                           length,
1206                           (length - sizeof *fs) % sizeof fs->actions[0]);
1207             break;
1208         }
1209
1210         ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1211                       ntohll(fs->cookie));
1212         ofp_print_duration(string, ntohl(fs->duration_sec),
1213                            ntohl(fs->duration_nsec));
1214         ds_put_format(string, ", table_id=%"PRIu8", ", fs->table_id);
1215         ds_put_format(string, "priority=%"PRIu16", ", ntohs(fs->priority));
1216         ds_put_format(string, "n_packets=%"PRIu64", ",
1217                     ntohll(fs->packet_count));
1218         ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
1219         if (fs->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
1220             ds_put_format(string, "idle_timeout=%"PRIu16",",
1221                           ntohs(fs->idle_timeout));
1222         }
1223         if (fs->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
1224             ds_put_format(string, "hard_timeout=%"PRIu16",",
1225                           ntohs(fs->hard_timeout));
1226         }
1227         ofp_print_match(string, &fs->match, verbosity);
1228         ds_put_char(string, ' ');
1229         ofp_print_actions(string, fs->actions, length - sizeof *fs);
1230
1231         pos += length;
1232      }
1233 }
1234
1235 static void
1236 ofp_print_nxst_flow_reply(struct ds *string, const struct ofp_header *oh)
1237 {
1238     struct ofpbuf b;
1239
1240     ofpbuf_use_const(&b, ofputil_nxstats_body(oh),
1241                      ofputil_nxstats_body_len(oh));
1242     while (b.size > 0) {
1243         const struct nx_flow_stats *fs;
1244         union ofp_action *actions;
1245         struct cls_rule rule;
1246         size_t actions_len, n_actions;
1247         size_t length;
1248         int match_len;
1249         int error;
1250
1251         ds_put_char(string, '\n');
1252
1253         fs = ofpbuf_try_pull(&b, sizeof *fs);
1254         if (!fs) {
1255             ds_put_format(string, " ***%td leftover bytes at end***", b.size);
1256             break;
1257         }
1258
1259         length = ntohs(fs->length);
1260         if (length < sizeof *fs) {
1261             ds_put_format(string, " ***nx_flow_stats claims length %zu***",
1262                           length);
1263             break;
1264         }
1265
1266         match_len = ntohs(fs->match_len);
1267         if (match_len > length - sizeof *fs) {
1268             ds_put_format(string, " ***length=%zu match_len=%d***",
1269                           length, match_len);
1270             break;
1271         }
1272
1273         ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1274                       ntohll(fs->cookie));
1275         ofp_print_duration(string, ntohl(fs->duration_sec),
1276                            ntohl(fs->duration_nsec));
1277         ds_put_format(string, ", table_id=%"PRIu8", ", fs->table_id);
1278         ds_put_format(string, "n_packets=%"PRIu64", ",
1279                     ntohll(fs->packet_count));
1280         ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
1281         if (fs->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
1282             ds_put_format(string, "idle_timeout=%"PRIu16",",
1283                           ntohs(fs->idle_timeout));
1284         }
1285         if (fs->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
1286             ds_put_format(string, "hard_timeout=%"PRIu16",",
1287                           ntohs(fs->hard_timeout));
1288         }
1289
1290         error = nx_pull_match(&b, match_len, ntohs(fs->priority), &rule);
1291         if (error) {
1292             ofp_print_error(string, error);
1293             break;
1294         }
1295
1296         actions_len = length - sizeof *fs - ROUND_UP(match_len, 8);
1297         error = ofputil_pull_actions(&b, actions_len, &actions, &n_actions);
1298         if (error) {
1299             ofp_print_error(string, error);
1300             break;
1301         }
1302
1303         cls_rule_format(&rule, string);
1304         ds_put_char(string, ' ');
1305         ofp_print_actions(string, (const struct ofp_action_header *) actions,
1306                           n_actions * sizeof *actions);
1307      }
1308 }
1309
1310 static void
1311 ofp_print_ofp_aggregate_stats_reply (
1312     struct ds *string, const struct ofp_aggregate_stats_reply *asr)
1313 {
1314     ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
1315     ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
1316     ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1317 }
1318
1319 static void
1320 ofp_print_ofpst_aggregate_reply(struct ds *string, const struct ofp_header *oh)
1321 {
1322     ofp_print_ofp_aggregate_stats_reply(string, ofputil_stats_body(oh));
1323 }
1324
1325 static void
1326 ofp_print_nxst_aggregate_reply(struct ds *string,
1327                                const struct nx_aggregate_stats_reply *nasr)
1328 {
1329     ofp_print_ofp_aggregate_stats_reply(string, &nasr->asr);
1330 }
1331
1332 static void print_port_stat(struct ds *string, const char *leader,
1333                             uint64_t stat, int more)
1334 {
1335     ds_put_cstr(string, leader);
1336     if (stat != -1) {
1337         ds_put_format(string, "%"PRIu64, stat);
1338     } else {
1339         ds_put_char(string, '?');
1340     }
1341     if (more) {
1342         ds_put_cstr(string, ", ");
1343     } else {
1344         ds_put_cstr(string, "\n");
1345     }
1346 }
1347
1348 static void
1349 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1350 {
1351     const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
1352     ds_put_format(string, "port_no=%"PRIu16, ntohs(psr->port_no));
1353 }
1354
1355 static void
1356 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1357                            int verbosity)
1358 {
1359     const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1360     size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
1361     ds_put_format(string, " %zu ports\n", n);
1362     if (verbosity < 1) {
1363         return;
1364     }
1365
1366     for (; n--; ps++) {
1367         ds_put_format(string, "  port %2"PRIu16": ", ntohs(ps->port_no));
1368
1369         ds_put_cstr(string, "rx ");
1370         print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
1371         print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
1372         print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
1373         print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
1374         print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
1375         print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
1376         print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
1377
1378         ds_put_cstr(string, "           tx ");
1379         print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
1380         print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
1381         print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
1382         print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
1383         print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
1384     }
1385 }
1386
1387 static void
1388 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1389                             int verbosity)
1390 {
1391     const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1392     size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
1393     ds_put_format(string, " %zu tables\n", n);
1394     if (verbosity < 1) {
1395         return;
1396     }
1397
1398     for (; n--; ts++) {
1399         char name[OFP_MAX_TABLE_NAME_LEN + 1];
1400         strncpy(name, ts->name, sizeof name);
1401         name[OFP_MAX_TABLE_NAME_LEN] = '\0';
1402
1403         ds_put_format(string, "  %d: %-8s: ", ts->table_id, name);
1404         ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1405         ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1406         ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1407         ds_put_cstr(string, "               ");
1408         ds_put_format(string, "lookup=%"PRIu64", ",
1409                     ntohll(ts->lookup_count));
1410         ds_put_format(string, "matched=%"PRIu64"\n",
1411                     ntohll(ts->matched_count));
1412      }
1413 }
1414
1415 static void
1416 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1417 {
1418     if (queue_id == OFPQ_ALL) {
1419         ds_put_cstr(string, "ALL");
1420     } else {
1421         ds_put_format(string, "%"PRIu32, queue_id);
1422     }
1423 }
1424
1425 static void
1426 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1427 {
1428     const struct ofp_queue_stats_request *qsr = ofputil_stats_body(oh);
1429
1430     ds_put_cstr(string, "port=");
1431     ofp_print_port_name(string, ntohs(qsr->port_no));
1432
1433     ds_put_cstr(string, " queue=");
1434     ofp_print_queue_name(string, ntohl(qsr->queue_id));
1435 }
1436
1437 static void
1438 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1439                             int verbosity)
1440 {
1441     const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1442     size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
1443     ds_put_format(string, " %zu queues\n", n);
1444     if (verbosity < 1) {
1445         return;
1446     }
1447
1448     for (; n--; qs++) {
1449         ds_put_cstr(string, "  port ");
1450         ofp_print_port_name(string, ntohs(qs->port_no));
1451         ds_put_cstr(string, " queue ");
1452         ofp_print_queue_name(string, ntohl(qs->queue_id));
1453         ds_put_cstr(string, ": ");
1454
1455         print_port_stat(string, "bytes=", ntohll(qs->tx_bytes), 1);
1456         print_port_stat(string, "pkts=", ntohll(qs->tx_packets), 1);
1457         print_port_stat(string, "errors=", ntohll(qs->tx_errors), 0);
1458     }
1459 }
1460
1461 static void
1462 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1463 {
1464     const struct ofp_stats_request *srq
1465         = (const struct ofp_stats_request *) oh;
1466
1467     if (srq->flags) {
1468         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1469                       ntohs(srq->flags));
1470     }
1471 }
1472
1473 static void
1474 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1475 {
1476     const struct ofp_stats_reply *srp = (const struct ofp_stats_reply *) oh;
1477
1478     if (srp->flags) {
1479         uint16_t flags = ntohs(srp->flags);
1480
1481         ds_put_cstr(string, " flags=");
1482         if (flags & OFPSF_REPLY_MORE) {
1483             ds_put_cstr(string, "[more]");
1484             flags &= ~OFPSF_REPLY_MORE;
1485         }
1486         if (flags) {
1487             ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1488                           flags);
1489         }
1490     }
1491 }
1492
1493 static void
1494 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1495 {
1496     size_t len = ntohs(oh->length);
1497
1498     ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1499     if (verbosity > 1) {
1500         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1501     }
1502 }
1503
1504 static void
1505 ofp_print_nxt_status_message(struct ds *string, const struct ofp_header *oh)
1506 {
1507     struct ofpbuf b;
1508
1509     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1510     ofpbuf_pull(&b, sizeof *oh);
1511     ds_put_char(string, '"');
1512     ds_put_printable(string, b.data, b.size);
1513     ds_put_char(string, '"');
1514 }
1515
1516 static void
1517 ofp_print_nxt_tun_id_from_cookie(struct ds *string,
1518                                  const struct nxt_tun_id_cookie *ntic)
1519 {
1520     ds_put_format(string, " set=%"PRIu8, ntic->set);
1521 }
1522
1523 static void
1524 ofp_print_nxt_role_message(struct ds *string,
1525                            const struct nx_role_request *nrr)
1526 {
1527     unsigned int role = ntohl(nrr->role);
1528
1529     ds_put_cstr(string, " role=");
1530     if (role == NX_ROLE_OTHER) {
1531         ds_put_cstr(string, "other");
1532     } else if (role == NX_ROLE_MASTER) {
1533         ds_put_cstr(string, "master");
1534     } else if (role == NX_ROLE_SLAVE) {
1535         ds_put_cstr(string, "slave");
1536     } else {
1537         ds_put_format(string, "%u", role);
1538     }
1539 }
1540
1541 static void
1542 ofp_print_nxt_set_flow_format(struct ds *string,
1543                               const struct nxt_set_flow_format *nsff)
1544 {
1545     uint32_t format = ntohl(nsff->format);
1546
1547     ds_put_cstr(string, " format=");
1548     if (ofputil_flow_format_is_valid(format)) {
1549         ds_put_cstr(string, ofputil_flow_format_to_string(format));
1550     } else {
1551         ds_put_format(string, "%"PRIu32, format);
1552     }
1553 }
1554
1555 static void
1556 ofp_to_string__(const struct ofp_header *oh,
1557                 const struct ofputil_msg_type *type, struct ds *string,
1558                 int verbosity)
1559 {
1560     enum ofputil_msg_code code;
1561     const void *msg = oh;
1562
1563     ds_put_format(string, "%s (xid=0x%"PRIx32"):",
1564                   ofputil_msg_type_name(type), ntohl(oh->xid));
1565
1566     code = ofputil_msg_type_code(type);
1567     switch (code) {
1568     case OFPUTIL_INVALID:
1569         break;
1570
1571     case OFPUTIL_OFPT_HELLO:
1572         break;
1573
1574     case OFPUTIL_OFPT_ERROR:
1575         ofp_print_error_msg(string, msg);
1576         break;
1577
1578     case OFPUTIL_OFPT_ECHO_REQUEST:
1579     case OFPUTIL_OFPT_ECHO_REPLY:
1580         ofp_print_echo(string, oh, verbosity);
1581         break;
1582
1583     case OFPUTIL_OFPT_FEATURES_REQUEST:
1584         break;
1585
1586     case OFPUTIL_OFPT_FEATURES_REPLY:
1587         ofp_print_switch_features(string, msg);
1588         break;
1589
1590     case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1591         break;
1592
1593     case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1594     case OFPUTIL_OFPT_SET_CONFIG:
1595         ofp_print_switch_config(string, msg);
1596         break;
1597
1598     case OFPUTIL_OFPT_PACKET_IN:
1599         ofp_print_packet_in(string, msg, verbosity);
1600         break;
1601
1602     case OFPUTIL_OFPT_FLOW_REMOVED:
1603     case OFPUTIL_NXT_FLOW_REMOVED:
1604         ofp_print_flow_removed(string, msg);
1605         break;
1606
1607     case OFPUTIL_OFPT_PORT_STATUS:
1608         ofp_print_port_status(string, msg);
1609         break;
1610
1611     case OFPUTIL_OFPT_PACKET_OUT:
1612         ofp_print_packet_out(string, msg, verbosity);
1613         break;
1614
1615     case OFPUTIL_OFPT_FLOW_MOD:
1616         ofp_print_flow_mod(string, msg, code, verbosity);
1617         break;
1618
1619     case OFPUTIL_OFPT_PORT_MOD:
1620         ofp_print_port_mod(string, msg);
1621         break;
1622
1623     case OFPUTIL_OFPT_BARRIER_REQUEST:
1624     case OFPUTIL_OFPT_BARRIER_REPLY:
1625         break;
1626
1627     case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1628     case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1629         /* XXX */
1630         break;
1631
1632     case OFPUTIL_OFPST_DESC_REQUEST:
1633         ofp_print_stats_request(string, oh);
1634         break;
1635
1636     case OFPUTIL_OFPST_FLOW_REQUEST:
1637     case OFPUTIL_NXST_FLOW_REQUEST:
1638     case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1639     case OFPUTIL_NXST_AGGREGATE_REQUEST:
1640         ofp_print_stats_request(string, oh);
1641         ofp_print_flow_stats_request(string, oh);
1642         break;
1643
1644     case OFPUTIL_OFPST_TABLE_REQUEST:
1645         ofp_print_stats_request(string, oh);
1646         break;
1647
1648     case OFPUTIL_OFPST_PORT_REQUEST:
1649         ofp_print_stats_request(string, oh);
1650         ofp_print_ofpst_port_request(string, oh);
1651         break;
1652
1653     case OFPUTIL_OFPST_QUEUE_REQUEST:
1654         ofp_print_stats_request(string, oh);
1655         ofp_print_ofpst_queue_request(string, oh);
1656         break;
1657
1658     case OFPUTIL_OFPST_DESC_REPLY:
1659         ofp_print_stats_reply(string, oh);
1660         ofp_print_ofpst_desc_reply(string, oh);
1661         break;
1662
1663     case OFPUTIL_OFPST_FLOW_REPLY:
1664         ofp_print_stats_reply(string, oh);
1665         ofp_print_ofpst_flow_reply(string, oh, verbosity);
1666         break;
1667
1668     case OFPUTIL_OFPST_QUEUE_REPLY:
1669         ofp_print_stats_reply(string, oh);
1670         ofp_print_ofpst_queue_reply(string, oh, verbosity);
1671         break;
1672
1673     case OFPUTIL_OFPST_PORT_REPLY:
1674         ofp_print_stats_reply(string, oh);
1675         ofp_print_ofpst_port_reply(string, oh, verbosity);
1676         break;
1677
1678     case OFPUTIL_OFPST_TABLE_REPLY:
1679         ofp_print_stats_reply(string, oh);
1680         ofp_print_ofpst_table_reply(string, oh, verbosity);
1681         break;
1682
1683     case OFPUTIL_OFPST_AGGREGATE_REPLY:
1684         ofp_print_stats_reply(string, oh);
1685         ofp_print_ofpst_aggregate_reply(string, oh);
1686         break;
1687
1688     case OFPUTIL_NXT_STATUS_REQUEST:
1689     case OFPUTIL_NXT_STATUS_REPLY:
1690         ofp_print_nxt_status_message(string, oh);
1691         break;
1692
1693     case OFPUTIL_NXT_TUN_ID_FROM_COOKIE:
1694         ofp_print_nxt_tun_id_from_cookie(string, msg);
1695         break;
1696
1697     case OFPUTIL_NXT_ROLE_REQUEST:
1698     case OFPUTIL_NXT_ROLE_REPLY:
1699         ofp_print_nxt_role_message(string, msg);
1700         break;
1701
1702     case OFPUTIL_NXT_SET_FLOW_FORMAT:
1703         ofp_print_nxt_set_flow_format(string, msg);
1704         break;
1705
1706     case OFPUTIL_NXT_FLOW_MOD:
1707         ofp_print_flow_mod(string, msg, code, verbosity);
1708         break;
1709
1710     case OFPUTIL_NXST_FLOW_REPLY:
1711         ofp_print_nxst_flow_reply(string, oh);
1712         break;
1713
1714     case OFPUTIL_NXST_AGGREGATE_REPLY:
1715         ofp_print_stats_reply(string, oh);
1716         ofp_print_nxst_aggregate_reply(string, msg);
1717         break;
1718     }
1719 }
1720
1721 /* Composes and returns a string representing the OpenFlow packet of 'len'
1722  * bytes at 'oh' at the given 'verbosity' level.  0 is a minimal amount of
1723  * verbosity and higher numbers increase verbosity.  The caller is responsible
1724  * for freeing the string. */
1725 char *
1726 ofp_to_string(const void *oh_, size_t len, int verbosity)
1727 {
1728     struct ds string = DS_EMPTY_INITIALIZER;
1729     const struct ofp_header *oh = oh_;
1730
1731     if (!len) {
1732         ds_put_cstr(&string, "OpenFlow message is empty\n");
1733     } else if (len < sizeof(struct ofp_header)) {
1734         ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
1735                       len);
1736     } else if (oh->version != OFP_VERSION) {
1737         ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n",
1738                       oh->version);
1739     } else if (ntohs(oh->length) > len) {
1740         ds_put_format(&string,
1741                       "(***truncated to %zu bytes from %"PRIu16"***)\n",
1742                       len, ntohs(oh->length));
1743     } else if (ntohs(oh->length) < len) {
1744         ds_put_format(&string,
1745                       "(***only uses %"PRIu16" bytes out of %zu***)\n",
1746                       ntohs(oh->length), len);
1747     } else {
1748         const struct ofputil_msg_type *type;
1749         int error;
1750
1751         error = ofputil_decode_msg_type(oh, &type);
1752         if (!error) {
1753             ofp_to_string__(oh, type, &string, verbosity);
1754             if (verbosity >= 5) {
1755                 if (ds_last(&string) != '\n') {
1756                     ds_put_char(&string, '\n');
1757                 }
1758                 ds_put_hex_dump(&string, oh, len, 0, true);
1759             }
1760             if (ds_last(&string) != '\n') {
1761                 ds_put_char(&string, '\n');
1762             }
1763             return ds_steal_cstr(&string);
1764         }
1765
1766         ofp_print_error(&string, error);
1767     }
1768     ds_put_hex_dump(&string, oh, len, 0, true);
1769     return ds_steal_cstr(&string);
1770 }
1771
1772 /* Returns the name for the specified OpenFlow message type as a string,
1773  * e.g. "OFPT_FEATURES_REPLY".  If no name is known, the string returned is a
1774  * hex number, e.g. "0x55".
1775  *
1776  * The caller must free the returned string when it is no longer needed. */
1777 char *
1778 ofp_message_type_to_string(uint8_t type)
1779 {
1780     const char *name;
1781
1782     switch (type) {
1783     case OFPT_HELLO:
1784         name = "HELLO";
1785         break;
1786     case OFPT_ERROR:
1787         name = "ERROR";
1788         break;
1789     case OFPT_ECHO_REQUEST:
1790         name = "ECHO_REQUEST";
1791         break;
1792     case OFPT_ECHO_REPLY:
1793         name = "ECHO_REPLY";
1794         break;
1795     case OFPT_VENDOR:
1796         name = "VENDOR";
1797         break;
1798     case OFPT_FEATURES_REQUEST:
1799         name = "FEATURES_REQUEST";
1800         break;
1801     case OFPT_FEATURES_REPLY:
1802         name = "FEATURES_REPLY";
1803         break;
1804     case OFPT_GET_CONFIG_REQUEST:
1805         name = "GET_CONFIG_REQUEST";
1806         break;
1807     case OFPT_GET_CONFIG_REPLY:
1808         name = "GET_CONFIG_REPLY";
1809         break;
1810     case OFPT_SET_CONFIG:
1811         name = "SET_CONFIG";
1812         break;
1813     case OFPT_PACKET_IN:
1814         name = "PACKET_IN";
1815         break;
1816     case OFPT_FLOW_REMOVED:
1817         name = "FLOW_REMOVED";
1818         break;
1819     case OFPT_PORT_STATUS:
1820         name = "PORT_STATUS";
1821         break;
1822     case OFPT_PACKET_OUT:
1823         name = "PACKET_OUT";
1824         break;
1825     case OFPT_FLOW_MOD:
1826         name = "FLOW_MOD";
1827         break;
1828     case OFPT_PORT_MOD:
1829         name = "PORT_MOD";
1830         break;
1831     case OFPT_STATS_REQUEST:
1832         name = "STATS_REQUEST";
1833         break;
1834     case OFPT_STATS_REPLY:
1835         name = "STATS_REPLY";
1836         break;
1837     case OFPT_BARRIER_REQUEST:
1838         name = "BARRIER_REQUEST";
1839         break;
1840     case OFPT_BARRIER_REPLY:
1841         name = "BARRIER_REPLY";
1842         break;
1843     case OFPT_QUEUE_GET_CONFIG_REQUEST:
1844         name = "QUEUE_GET_CONFIG_REQUEST";
1845         break;
1846     case OFPT_QUEUE_GET_CONFIG_REPLY:
1847         name = "QUEUE_GET_CONFIG_REPLY";
1848         break;
1849     default:
1850         name = NULL;
1851         break;
1852     }
1853
1854     return name ? xasprintf("OFPT_%s", name) : xasprintf("0x%02"PRIx8, type);
1855 }
1856 \f
1857 static void
1858 print_and_free(FILE *stream, char *string)
1859 {
1860     fputs(string, stream);
1861     free(string);
1862 }
1863
1864 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1865  * given 'verbosity' level.  0 is a minimal amount of verbosity and higher
1866  * numbers increase verbosity. */
1867 void
1868 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1869 {
1870     print_and_free(stream, ofp_to_string(oh, len, verbosity));
1871 }
1872
1873 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1874  * 'data' to 'stream' using tcpdump.  'total_len' specifies the full length of
1875  * the Ethernet frame (of which 'len' bytes were captured).
1876  *
1877  * This starts and kills a tcpdump subprocess so it's quite expensive. */
1878 void
1879 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1880 {
1881     print_and_free(stream, ofp_packet_to_string(data, len, total_len));
1882 }