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