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