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