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