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