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