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