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