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