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