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