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