ofp-util: Clean up cookie handling.
[sliver-openvswitch.git] / lib / ofp-print.c
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira, Inc.
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 "netdev.h"
38 #include "nx-match.h"
39 #include "ofp-errors.h"
40 #include "ofp-util.h"
41 #include "ofpbuf.h"
42 #include "openflow/openflow.h"
43 #include "openflow/nicira-ext.h"
44 #include "packets.h"
45 #include "pcap.h"
46 #include "type-props.h"
47 #include "unaligned.h"
48 #include "util.h"
49
50 static void ofp_print_queue_name(struct ds *string, uint32_t port);
51 static void ofp_print_error(struct ds *, enum ofperr);
52
53
54 /* Returns a string that represents the contents of the Ethernet frame in the
55  * 'len' bytes starting at 'data'.  The caller must free the returned string.*/
56 char *
57 ofp_packet_to_string(const void *data, size_t len)
58 {
59     struct ds ds = DS_EMPTY_INITIALIZER;
60     struct ofpbuf buf;
61     struct flow flow;
62
63     ofpbuf_use_const(&buf, data, len);
64     flow_extract(&buf, 0, 0, 0, &flow);
65     flow_format(&ds, &flow);
66
67     if (buf.l7) {
68         if (flow.nw_proto == IPPROTO_TCP) {
69             struct tcp_header *th = buf.l4;
70             ds_put_format(&ds, " tcp_csum:%"PRIx16,
71                           ntohs(th->tcp_csum));
72         } else if (flow.nw_proto == IPPROTO_UDP) {
73             struct udp_header *uh = buf.l4;
74             ds_put_format(&ds, " udp_csum:%"PRIx16,
75                           ntohs(uh->udp_csum));
76         }
77     }
78
79     ds_put_char(&ds, '\n');
80
81     return ds_cstr(&ds);
82 }
83
84 static void
85 ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
86                     int verbosity)
87 {
88     struct ofputil_packet_in pin;
89     int error;
90     int i;
91
92     error = ofputil_decode_packet_in(&pin, oh);
93     if (error) {
94         ofp_print_error(string, error);
95         return;
96     }
97
98     if (pin.table_id) {
99         ds_put_format(string, " table_id=%"PRIu8, pin.table_id);
100     }
101
102     if (pin.cookie) {
103         ds_put_format(string, " cookie=0x%"PRIx64, ntohll(pin.cookie));
104     }
105
106     ds_put_format(string, " total_len=%"PRIu16" in_port=", pin.total_len);
107     ofputil_format_port(pin.fmd.in_port, string);
108
109     if (pin.fmd.tun_id_mask) {
110         ds_put_format(string, " tun_id=0x%"PRIx64, ntohll(pin.fmd.tun_id));
111         if (pin.fmd.tun_id_mask != htonll(UINT64_MAX)) {
112             ds_put_format(string, "/0x%"PRIx64, ntohll(pin.fmd.tun_id_mask));
113         }
114     }
115
116     for (i = 0; i < FLOW_N_REGS; i++) {
117         if (pin.fmd.reg_masks[i]) {
118             ds_put_format(string, " reg%d=0x%"PRIx32, i, pin.fmd.regs[i]);
119             if (pin.fmd.reg_masks[i] != UINT32_MAX) {
120                 ds_put_format(string, "/0x%"PRIx32, pin.fmd.reg_masks[i]);
121             }
122         }
123     }
124
125     ds_put_format(string, " (via %s)",
126                   ofputil_packet_in_reason_to_string(pin.reason));
127
128     ds_put_format(string, " data_len=%zu", pin.packet_len);
129     if (pin.buffer_id == UINT32_MAX) {
130         ds_put_format(string, " (unbuffered)");
131         if (pin.total_len != pin.packet_len) {
132             ds_put_format(string, " (***total_len != data_len***)");
133         }
134     } else {
135         ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
136         if (pin.total_len < pin.packet_len) {
137             ds_put_format(string, " (***total_len < data_len***)");
138         }
139     }
140     ds_put_char(string, '\n');
141
142     if (verbosity > 0) {
143         char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
144         ds_put_cstr(string, packet);
145         free(packet);
146     }
147 }
148
149 static void
150 print_note(struct ds *string, const struct nx_action_note *nan)
151 {
152     size_t len;
153     size_t i;
154
155     ds_put_cstr(string, "note:");
156     len = ntohs(nan->len) - offsetof(struct nx_action_note, note);
157     for (i = 0; i < len; i++) {
158         if (i) {
159             ds_put_char(string, '.');
160         }
161         ds_put_format(string, "%02"PRIx8, nan->note[i]);
162     }
163 }
164
165 static void
166 ofp_print_action(struct ds *s, const union ofp_action *a,
167                  enum ofputil_action_code code)
168 {
169     const struct ofp_action_enqueue *oae;
170     const struct ofp_action_dl_addr *oada;
171     const struct nx_action_set_tunnel64 *nast64;
172     const struct nx_action_set_tunnel *nast;
173     const struct nx_action_set_queue *nasq;
174     const struct nx_action_resubmit *nar;
175     const struct nx_action_reg_move *move;
176     const struct nx_action_reg_load *load;
177     const struct nx_action_multipath *nam;
178     const struct nx_action_autopath *naa;
179     const struct nx_action_output_reg *naor;
180     const struct nx_action_fin_timeout *naft;
181     const struct nx_action_controller *nac;
182     struct mf_subfield subfield;
183     uint16_t port;
184
185     switch (code) {
186     case OFPUTIL_OFPAT10_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_OFPAT10_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_OFPAT10_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_OFPAT10_SET_VLAN_PCP:
215         ds_put_format(s, "mod_vlan_pcp:%"PRIu8, a->vlan_pcp.vlan_pcp);
216         break;
217
218     case OFPUTIL_OFPAT10_STRIP_VLAN:
219         ds_put_cstr(s, "strip_vlan");
220         break;
221
222     case OFPUTIL_OFPAT10_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_OFPAT10_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_OFPAT10_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_OFPAT10_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_OFPAT10_SET_NW_TOS:
243         ds_put_format(s, "mod_nw_tos:%d", a->nw_tos.nw_tos);
244         break;
245
246     case OFPUTIL_OFPAT10_SET_TP_SRC:
247         ds_put_format(s, "mod_tp_src:%d", ntohs(a->tp_port.tp_port));
248         break;
249
250     case OFPUTIL_OFPAT10_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_decode(&subfield, naa->dst, naa->ofs_nbits);
316         mf_format_subfield(&subfield, s);
317         ds_put_char(s, ')');
318         break;
319
320     case OFPUTIL_NXAST_BUNDLE:
321     case OFPUTIL_NXAST_BUNDLE_LOAD:
322         bundle_format((const struct nx_action_bundle *) a, s);
323         break;
324
325     case OFPUTIL_NXAST_OUTPUT_REG:
326         naor = (const struct nx_action_output_reg *) a;
327         ds_put_cstr(s, "output:");
328         nxm_decode(&subfield, naor->src, naor->ofs_nbits);
329         mf_format_subfield(&subfield, s);
330         break;
331
332     case OFPUTIL_NXAST_LEARN:
333         learn_format((const struct nx_action_learn *) a, s);
334         break;
335
336     case OFPUTIL_NXAST_DEC_TTL:
337         ds_put_cstr(s, "dec_ttl");
338         break;
339
340     case OFPUTIL_NXAST_EXIT:
341         ds_put_cstr(s, "exit");
342         break;
343
344     case OFPUTIL_NXAST_FIN_TIMEOUT:
345         naft = (const struct nx_action_fin_timeout *) a;
346         ds_put_cstr(s, "fin_timeout(");
347         if (naft->fin_idle_timeout) {
348             ds_put_format(s, "idle_timeout=%"PRIu16",",
349                           ntohs(naft->fin_idle_timeout));
350         }
351         if (naft->fin_hard_timeout) {
352             ds_put_format(s, "hard_timeout=%"PRIu16",",
353                           ntohs(naft->fin_hard_timeout));
354         }
355         ds_chomp(s, ',');
356         ds_put_char(s, ')');
357         break;
358
359     case OFPUTIL_NXAST_CONTROLLER:
360         nac = (const struct nx_action_controller *) a;
361         ds_put_cstr(s, "controller(");
362         if (nac->reason != OFPR_ACTION) {
363             ds_put_format(s, "reason=%s,",
364                           ofputil_packet_in_reason_to_string(nac->reason));
365         }
366         if (nac->max_len != htons(UINT16_MAX)) {
367             ds_put_format(s, "max_len=%"PRIu16",", ntohs(nac->max_len));
368         }
369         if (nac->controller_id != htons(0)) {
370             ds_put_format(s, "id=%"PRIu16",", ntohs(nac->controller_id));
371         }
372         ds_chomp(s, ',');
373         ds_put_char(s, ')');
374         break;
375
376     default:
377         break;
378     }
379 }
380
381 void
382 ofp_print_actions(struct ds *string, const union ofp_action *actions,
383                   size_t n_actions)
384 {
385     const union ofp_action *a;
386     size_t left;
387
388     ds_put_cstr(string, "actions=");
389     if (!n_actions) {
390         ds_put_cstr(string, "drop");
391     }
392
393     OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
394         int code = ofputil_decode_action(a);
395         if (code >= 0) {
396             if (a != actions) {
397                 ds_put_cstr(string, ",");
398             }
399             ofp_print_action(string, a, code);
400         } else {
401             ofp_print_error(string, -code);
402         }
403     }
404     if (left > 0) {
405         ds_put_format(string, " ***%zu leftover bytes following actions",
406                       left * sizeof *a);
407     }
408 }
409
410 static void
411 ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
412                      int verbosity)
413 {
414     struct ofputil_packet_out po;
415     enum ofperr error;
416
417     error = ofputil_decode_packet_out(&po, opo);
418     if (error) {
419         ofp_print_error(string, error);
420         return;
421     }
422
423     ds_put_cstr(string, " in_port=");
424     ofputil_format_port(po.in_port, string);
425
426     ds_put_char(string, ' ');
427     ofp_print_actions(string, po.actions, po.n_actions);
428
429     if (po.buffer_id == UINT32_MAX) {
430         ds_put_format(string, " data_len=%zu", po.packet_len);
431         if (verbosity > 0 && po.packet_len > 0) {
432             char *packet = ofp_packet_to_string(po.packet, po.packet_len);
433             ds_put_char(string, '\n');
434             ds_put_cstr(string, packet);
435             free(packet);
436         }
437     } else {
438         ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id);
439     }
440     ds_put_char(string, '\n');
441 }
442
443 /* qsort comparison function. */
444 static int
445 compare_ports(const void *a_, const void *b_)
446 {
447     const struct ofputil_phy_port *a = a_;
448     const struct ofputil_phy_port *b = b_;
449     uint16_t ap = a->port_no;
450     uint16_t bp = b->port_no;
451
452     return ap < bp ? -1 : ap > bp;
453 }
454
455 static void
456 ofp_print_bit_names(struct ds *string, uint32_t bits,
457                     const char *(*bit_to_name)(uint32_t bit))
458 {
459     int n = 0;
460     int i;
461
462     if (!bits) {
463         ds_put_cstr(string, "0");
464         return;
465     }
466
467     for (i = 0; i < 32; i++) {
468         uint32_t bit = UINT32_C(1) << i;
469
470         if (bits & bit) {
471             const char *name = bit_to_name(bit);
472             if (name) {
473                 if (n++) {
474                     ds_put_char(string, ' ');
475                 }
476                 ds_put_cstr(string, name);
477                 bits &= ~bit;
478             }
479         }
480     }
481
482     if (bits) {
483         if (n) {
484             ds_put_char(string, ' ');
485         }
486         ds_put_format(string, "0x%"PRIx32, bits);
487     }
488 }
489
490 static const char *
491 netdev_feature_to_name(uint32_t bit)
492 {
493     enum netdev_features f = bit;
494
495     switch (f) {
496     case NETDEV_F_10MB_HD:    return "10MB-HD";
497     case NETDEV_F_10MB_FD:    return "10MB-FD";
498     case NETDEV_F_100MB_HD:   return "100MB-HD";
499     case NETDEV_F_100MB_FD:   return "100MB-FD";
500     case NETDEV_F_1GB_HD:     return "1GB-HD";
501     case NETDEV_F_1GB_FD:     return "1GB-FD";
502     case NETDEV_F_10GB_FD:    return "10GB-FD";
503     case NETDEV_F_40GB_FD:    return "40GB-FD";
504     case NETDEV_F_100GB_FD:   return "100GB-FD";
505     case NETDEV_F_1TB_FD:     return "1TB-FD";
506     case NETDEV_F_OTHER:      return "OTHER";
507     case NETDEV_F_COPPER:     return "COPPER";
508     case NETDEV_F_FIBER:      return "FIBER";
509     case NETDEV_F_AUTONEG:    return "AUTO_NEG";
510     case NETDEV_F_PAUSE:      return "AUTO_PAUSE";
511     case NETDEV_F_PAUSE_ASYM: return "AUTO_PAUSE_ASYM";
512     }
513
514     return NULL;
515 }
516
517 static void
518 ofp_print_port_features(struct ds *string, enum netdev_features features)
519 {
520     ofp_print_bit_names(string, features, netdev_feature_to_name);
521     ds_put_char(string, '\n');
522 }
523
524 static const char *
525 ofputil_port_config_to_name(uint32_t bit)
526 {
527     enum ofputil_port_config pc = bit;
528
529     switch (pc) {
530     case OFPUTIL_PC_PORT_DOWN:    return "PORT_DOWN";
531     case OFPUTIL_PC_NO_STP:       return "NO_STP";
532     case OFPUTIL_PC_NO_RECV:      return "NO_RECV";
533     case OFPUTIL_PC_NO_RECV_STP:  return "NO_RECV_STP";
534     case OFPUTIL_PC_NO_FLOOD:     return "NO_FLOOD";
535     case OFPUTIL_PC_NO_FWD:       return "NO_FWD";
536     case OFPUTIL_PC_NO_PACKET_IN: return "NO_PACKET_IN";
537     }
538
539     return NULL;
540 }
541
542 static void
543 ofp_print_port_config(struct ds *string, enum ofputil_port_config config)
544 {
545     ofp_print_bit_names(string, config, ofputil_port_config_to_name);
546     ds_put_char(string, '\n');
547 }
548
549 static const char *
550 ofputil_port_state_to_name(uint32_t bit)
551 {
552     enum ofputil_port_state ps = bit;
553
554     switch (ps) {
555     case OFPUTIL_PS_LINK_DOWN: return "LINK_DOWN";
556     case OFPUTIL_PS_BLOCKED:   return "BLOCKED";
557     case OFPUTIL_PS_LIVE:      return "LIVE";
558
559     case OFPUTIL_PS_STP_LISTEN:
560     case OFPUTIL_PS_STP_LEARN:
561     case OFPUTIL_PS_STP_FORWARD:
562     case OFPUTIL_PS_STP_BLOCK:
563         /* Handled elsewhere. */
564         return NULL;
565     }
566
567     return NULL;
568 }
569
570 static void
571 ofp_print_port_state(struct ds *string, enum ofputil_port_state state)
572 {
573     enum ofputil_port_state stp_state;
574
575     /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
576      * pattern.  We have to special case it.
577      *
578      * OVS doesn't support STP, so this field will always be 0 if we are
579      * talking to OVS, so we'd always print STP_LISTEN in that case.
580      * Therefore, we don't print anything at all if the value is STP_LISTEN, to
581      * avoid confusing users. */
582     stp_state = state & OFPUTIL_PS_STP_MASK;
583     if (stp_state) {
584         ds_put_cstr(string,
585                     (stp_state == OFPUTIL_PS_STP_LEARN ? "STP_LEARN"
586                      : stp_state == OFPUTIL_PS_STP_FORWARD ? "STP_FORWARD"
587                      : "STP_BLOCK"));
588         state &= ~OFPUTIL_PS_STP_MASK;
589         if (state) {
590             ofp_print_bit_names(string, state, ofputil_port_state_to_name);
591         }
592     } else {
593         ofp_print_bit_names(string, state, ofputil_port_state_to_name);
594     }
595     ds_put_char(string, '\n');
596 }
597
598 static void
599 ofp_print_phy_port(struct ds *string, const struct ofputil_phy_port *port)
600 {
601     char name[sizeof port->name];
602     int j;
603
604     memcpy(name, port->name, sizeof name);
605     for (j = 0; j < sizeof name - 1; j++) {
606         if (!isprint((unsigned char) name[j])) {
607             break;
608         }
609     }
610     name[j] = '\0';
611
612     ds_put_char(string, ' ');
613     ofputil_format_port(port->port_no, string);
614     ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
615                   name, ETH_ADDR_ARGS(port->hw_addr));
616
617     ds_put_cstr(string, "     config:     ");
618     ofp_print_port_config(string, port->config);
619
620     ds_put_cstr(string, "     state:      ");
621     ofp_print_port_state(string, port->state);
622
623     if (port->curr) {
624         ds_put_format(string, "     current:    ");
625         ofp_print_port_features(string, port->curr);
626     }
627     if (port->advertised) {
628         ds_put_format(string, "     advertised: ");
629         ofp_print_port_features(string, port->advertised);
630     }
631     if (port->supported) {
632         ds_put_format(string, "     supported:  ");
633         ofp_print_port_features(string, port->supported);
634     }
635     if (port->peer) {
636         ds_put_format(string, "     peer:       ");
637         ofp_print_port_features(string, port->peer);
638     }
639     ds_put_format(string, "     speed: %"PRIu32" Mbps now, "
640                   "%"PRIu32" Mbps max\n",
641                   port->curr_speed / UINT32_C(1000),
642                   port->max_speed / UINT32_C(1000));
643 }
644
645 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
646  * 'ofp_version', writes a detailed description of each port into
647  * 'string'. */
648 static void
649 ofp_print_phy_ports(struct ds *string, uint8_t ofp_version,
650                     struct ofpbuf *b)
651 {
652     size_t n_ports;
653     struct ofputil_phy_port *ports;
654     enum ofperr error;
655     size_t i;
656
657     n_ports = ofputil_count_phy_ports(ofp_version, b);
658
659     ports = xmalloc(n_ports * sizeof *ports);
660     for (i = 0; i < n_ports; i++) {
661         error = ofputil_pull_phy_port(ofp_version, b, &ports[i]);
662         if (error) {
663             ofp_print_error(string, error);
664             goto exit;
665         }
666     }
667     qsort(ports, n_ports, sizeof *ports, compare_ports);
668     for (i = 0; i < n_ports; i++) {
669         ofp_print_phy_port(string, &ports[i]);
670     }
671
672 exit:
673     free(ports);
674 }
675
676 static const char *
677 ofputil_capabilities_to_name(uint32_t bit)
678 {
679     enum ofputil_capabilities capabilities = bit;
680
681     switch (capabilities) {
682     case OFPUTIL_C_FLOW_STATS:   return "FLOW_STATS";
683     case OFPUTIL_C_TABLE_STATS:  return "TABLE_STATS";
684     case OFPUTIL_C_PORT_STATS:   return "PORT_STATS";
685     case OFPUTIL_C_IP_REASM:     return "IP_REASM";
686     case OFPUTIL_C_QUEUE_STATS:  return "QUEUE_STATS";
687     case OFPUTIL_C_ARP_MATCH_IP: return "ARP_MATCH_IP";
688     case OFPUTIL_C_STP:          return "STP";
689     case OFPUTIL_C_GROUP_STATS:  return "GROUP_STATS";
690     }
691
692     return NULL;
693 }
694
695 static const char *
696 ofputil_action_bitmap_to_name(uint32_t bit)
697 {
698     enum ofputil_action_bitmap action = bit;
699
700     switch (action) {
701     case OFPUTIL_A_OUTPUT:         return "OUTPUT";
702     case OFPUTIL_A_SET_VLAN_VID:   return "SET_VLAN_VID";
703     case OFPUTIL_A_SET_VLAN_PCP:   return "SET_VLAN_PCP";
704     case OFPUTIL_A_STRIP_VLAN:     return "STRIP_VLAN";
705     case OFPUTIL_A_SET_DL_SRC:     return "SET_DL_SRC";
706     case OFPUTIL_A_SET_DL_DST:     return "SET_DL_DST";
707     case OFPUTIL_A_SET_NW_SRC:     return "SET_NW_SRC";
708     case OFPUTIL_A_SET_NW_DST:     return "SET_NW_DST";
709     case OFPUTIL_A_SET_NW_ECN:     return "SET_NW_ECN";
710     case OFPUTIL_A_SET_NW_TOS:     return "SET_NW_TOS";
711     case OFPUTIL_A_SET_TP_SRC:     return "SET_TP_SRC";
712     case OFPUTIL_A_SET_TP_DST:     return "SET_TP_DST";
713     case OFPUTIL_A_ENQUEUE:        return "ENQUEUE";
714     case OFPUTIL_A_COPY_TTL_OUT:   return "COPY_TTL_OUT";
715     case OFPUTIL_A_COPY_TTL_IN:    return "COPY_TTL_IN";
716     case OFPUTIL_A_SET_MPLS_LABEL: return "SET_MPLS_LABEL";
717     case OFPUTIL_A_SET_MPLS_TC:    return "SET_MPLS_TC";
718     case OFPUTIL_A_SET_MPLS_TTL:   return "SET_MPLS_TTL";
719     case OFPUTIL_A_DEC_MPLS_TTL:   return "DEC_MPLS_TTL";
720     case OFPUTIL_A_PUSH_VLAN:      return "PUSH_VLAN";
721     case OFPUTIL_A_POP_VLAN:       return "POP_VLAN";
722     case OFPUTIL_A_PUSH_MPLS:      return "PUSH_MPLS";
723     case OFPUTIL_A_POP_MPLS:       return "POP_MPLS";
724     case OFPUTIL_A_SET_QUEUE:      return "SET_QUEUE";
725     case OFPUTIL_A_GROUP:          return "GROUP";
726     case OFPUTIL_A_SET_NW_TTL:     return "SET_NW_TTL";
727     case OFPUTIL_A_DEC_NW_TTL:     return "DEC_NW_TTL";
728     }
729
730     return NULL;
731 }
732
733 static void
734 ofp_print_switch_features(struct ds *string,
735                           const struct ofp_switch_features *osf)
736 {
737     struct ofputil_switch_features features;
738     enum ofperr error;
739     struct ofpbuf b;
740
741     error = ofputil_decode_switch_features(osf, &features, &b);
742     if (error) {
743         ofp_print_error(string, error);
744         return;
745     }
746
747     ds_put_format(string, " dpid:%016"PRIx64"\n", features.datapath_id);
748     ds_put_format(string, "n_tables:%"PRIu8", n_buffers:%"PRIu32"\n",
749                   features.n_tables, features.n_buffers);
750
751     ds_put_cstr(string, "capabilities: ");
752     ofp_print_bit_names(string, features.capabilities,
753                         ofputil_capabilities_to_name);
754     ds_put_char(string, '\n');
755
756     ds_put_cstr(string, "actions: ");
757     ofp_print_bit_names(string, features.actions,
758                         ofputil_action_bitmap_to_name);
759     ds_put_char(string, '\n');
760
761     ofp_print_phy_ports(string, osf->header.version, &b);
762 }
763
764 static void
765 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
766 {
767     enum ofp_config_flags flags;
768
769     flags = ntohs(osc->flags);
770
771     ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
772     flags &= ~OFPC_FRAG_MASK;
773
774     if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) {
775         ds_put_format(string, " invalid_ttl_to_controller");
776         flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER;
777     }
778
779     if (flags) {
780         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
781     }
782
783     ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
784 }
785
786 static void print_wild(struct ds *string, const char *leader, int is_wild,
787             int verbosity, const char *format, ...)
788             __attribute__((format(printf, 5, 6)));
789
790 static void print_wild(struct ds *string, const char *leader, int is_wild,
791                        int verbosity, const char *format, ...)
792 {
793     if (is_wild && verbosity < 2) {
794         return;
795     }
796     ds_put_cstr(string, leader);
797     if (!is_wild) {
798         va_list args;
799
800         va_start(args, format);
801         ds_put_format_valist(string, format, args);
802         va_end(args);
803     } else {
804         ds_put_char(string, '*');
805     }
806     ds_put_char(string, ',');
807 }
808
809 static void
810 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
811                  uint32_t wild_bits, int verbosity)
812 {
813     if (wild_bits >= 32 && verbosity < 2) {
814         return;
815     }
816     ds_put_cstr(string, leader);
817     if (wild_bits < 32) {
818         ds_put_format(string, IP_FMT, IP_ARGS(&ip));
819         if (wild_bits) {
820             ds_put_format(string, "/%d", 32 - wild_bits);
821         }
822     } else {
823         ds_put_char(string, '*');
824     }
825     ds_put_char(string, ',');
826 }
827
828 void
829 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
830 {
831     char *s = ofp_match_to_string(om, verbosity);
832     ds_put_cstr(f, s);
833     free(s);
834 }
835
836 char *
837 ofp_match_to_string(const struct ofp_match *om, int verbosity)
838 {
839     struct ds f = DS_EMPTY_INITIALIZER;
840     uint32_t w = ntohl(om->wildcards);
841     bool skip_type = false;
842     bool skip_proto = false;
843
844     if (!(w & OFPFW_DL_TYPE)) {
845         skip_type = true;
846         if (om->dl_type == htons(ETH_TYPE_IP)) {
847             if (!(w & OFPFW_NW_PROTO)) {
848                 skip_proto = true;
849                 if (om->nw_proto == IPPROTO_ICMP) {
850                     ds_put_cstr(&f, "icmp,");
851                 } else if (om->nw_proto == IPPROTO_TCP) {
852                     ds_put_cstr(&f, "tcp,");
853                 } else if (om->nw_proto == IPPROTO_UDP) {
854                     ds_put_cstr(&f, "udp,");
855                 } else {
856                     ds_put_cstr(&f, "ip,");
857                     skip_proto = false;
858                 }
859             } else {
860                 ds_put_cstr(&f, "ip,");
861             }
862         } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
863             ds_put_cstr(&f, "arp,");
864         } else {
865             skip_type = false;
866         }
867     }
868     print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
869                "%d", ntohs(om->in_port));
870     print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
871                "%d", ntohs(om->dl_vlan));
872     print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
873                "%d", om->dl_vlan_pcp);
874     print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
875                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
876     print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
877                ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
878     if (!skip_type) {
879         print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
880                    "0x%04x", ntohs(om->dl_type));
881     }
882     print_ip_netmask(&f, "nw_src=", om->nw_src,
883                      (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
884     print_ip_netmask(&f, "nw_dst=", om->nw_dst,
885                      (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
886     if (!skip_proto) {
887         if (om->dl_type == htons(ETH_TYPE_ARP)) {
888             print_wild(&f, "arp_op=", w & OFPFW_NW_PROTO, verbosity,
889                        "%u", om->nw_proto);
890         } else {
891             print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
892                        "%u", om->nw_proto);
893         }
894     }
895     print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
896                "%u", om->nw_tos);
897     if (om->nw_proto == IPPROTO_ICMP) {
898         print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
899                    "%d", ntohs(om->tp_src));
900         print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
901                    "%d", ntohs(om->tp_dst));
902     } else {
903         print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
904                    "%d", ntohs(om->tp_src));
905         print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
906                    "%d", ntohs(om->tp_dst));
907     }
908     if (ds_last(&f) == ',') {
909         f.length--;
910     }
911     return ds_cstr(&f);
912 }
913
914 static void
915 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
916                    enum ofputil_msg_code code, int verbosity)
917 {
918     struct ofputil_flow_mod fm;
919     bool need_priority;
920     enum ofperr error;
921
922     error = ofputil_decode_flow_mod(&fm, oh, OFPUTIL_P_OF10_TID);
923     if (error) {
924         ofp_print_error(s, error);
925         return;
926     }
927
928     ds_put_char(s, ' ');
929     switch (fm.command) {
930     case OFPFC_ADD:
931         ds_put_cstr(s, "ADD");
932         break;
933     case OFPFC_MODIFY:
934         ds_put_cstr(s, "MOD");
935         break;
936     case OFPFC_MODIFY_STRICT:
937         ds_put_cstr(s, "MOD_STRICT");
938         break;
939     case OFPFC_DELETE:
940         ds_put_cstr(s, "DEL");
941         break;
942     case OFPFC_DELETE_STRICT:
943         ds_put_cstr(s, "DEL_STRICT");
944         break;
945     default:
946         ds_put_format(s, "cmd:%d", fm.command);
947     }
948     if (fm.table_id != 0) {
949         ds_put_format(s, " table:%d", fm.table_id);
950     }
951
952     ds_put_char(s, ' ');
953     if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
954         const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
955         ofp_print_match(s, &ofm->match, verbosity);
956
957         /* ofp_print_match() doesn't print priority. */
958         need_priority = true;
959     } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
960         const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
961         const void *nxm = nfm + 1;
962         char *nxm_s;
963
964         nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
965         ds_put_cstr(s, nxm_s);
966         free(nxm_s);
967
968         /* nx_match_to_string() doesn't print priority. */
969         need_priority = true;
970     } else {
971         cls_rule_format(&fm.cr, s);
972
973         /* cls_rule_format() does print priority. */
974         need_priority = false;
975     }
976
977     if (ds_last(s) != ' ') {
978         ds_put_char(s, ' ');
979     }
980     if (fm.new_cookie != htonll(0)) {
981         ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.new_cookie));
982     }
983     if (fm.cookie_mask != htonll(0)) {
984         ds_put_format(s, "cookie:0x%"PRIx64"/0x%"PRIx64" ",
985                 ntohll(fm.cookie), ntohll(fm.cookie_mask));
986     }
987     if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
988         ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
989     }
990     if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
991         ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
992     }
993     if (fm.cr.priority != OFP_DEFAULT_PRIORITY && need_priority) {
994         ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
995     }
996     if (fm.buffer_id != UINT32_MAX) {
997         ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
998     }
999     if (fm.flags != 0) {
1000         uint16_t flags = fm.flags;
1001
1002         if (flags & OFPFF_SEND_FLOW_REM) {
1003             ds_put_cstr(s, "send_flow_rem ");
1004         }
1005         if (flags & OFPFF_CHECK_OVERLAP) {
1006             ds_put_cstr(s, "check_overlap ");
1007         }
1008         if (flags & OFPFF_EMERG) {
1009             ds_put_cstr(s, "emerg ");
1010         }
1011
1012         flags &= ~(OFPFF_SEND_FLOW_REM | OFPFF_CHECK_OVERLAP | OFPFF_EMERG);
1013         if (flags) {
1014             ds_put_format(s, "flags:0x%"PRIx16" ", flags);
1015         }
1016     }
1017
1018     ofp_print_actions(s, fm.actions, fm.n_actions);
1019 }
1020
1021 static void
1022 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
1023 {
1024     ds_put_format(string, "%u", sec);
1025     if (nsec > 0) {
1026         ds_put_format(string, ".%09u", nsec);
1027         while (string->string[string->length - 1] == '0') {
1028             string->length--;
1029         }
1030     }
1031     ds_put_char(string, 's');
1032 }
1033
1034 static const char *
1035 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason)
1036 {
1037     static char s[32];
1038
1039     switch (reason) {
1040     case OFPRR_IDLE_TIMEOUT:
1041         return "idle";
1042     case OFPRR_HARD_TIMEOUT:
1043         return "hard";
1044     case OFPRR_DELETE:
1045         return "delete";
1046     case OFPRR_GROUP_DELETE:
1047         return "group_delete";
1048     default:
1049         sprintf(s, "%d", (int) reason);
1050         return s;
1051     }
1052 }
1053
1054 static void
1055 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
1056 {
1057     struct ofputil_flow_removed fr;
1058     enum ofperr error;
1059
1060     error = ofputil_decode_flow_removed(&fr, oh);
1061     if (error) {
1062         ofp_print_error(string, error);
1063         return;
1064     }
1065
1066     ds_put_char(string, ' ');
1067     cls_rule_format(&fr.rule, string);
1068
1069     ds_put_format(string, " reason=%s",
1070                   ofp_flow_removed_reason_to_string(fr.reason));
1071
1072     if (fr.cookie != htonll(0)) {
1073         ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
1074     }
1075     ds_put_cstr(string, " duration");
1076     ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
1077     ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
1078          fr.idle_timeout, fr.packet_count, fr.byte_count);
1079 }
1080
1081 static void
1082 ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
1083 {
1084     struct ofputil_port_mod pm;
1085     enum ofperr error;
1086
1087     error = ofputil_decode_port_mod(oh, &pm);
1088     if (error) {
1089         ofp_print_error(string, error);
1090         return;
1091     }
1092
1093     ds_put_format(string, "port: %"PRIu16": addr:"ETH_ADDR_FMT"\n",
1094                   pm.port_no, ETH_ADDR_ARGS(pm.hw_addr));
1095
1096     ds_put_format(string, "     config: ");
1097     ofp_print_port_config(string, pm.config);
1098
1099     ds_put_format(string, "     mask:   ");
1100     ofp_print_port_config(string, pm.mask);
1101
1102     ds_put_format(string, "     advertise: ");
1103     if (pm.advertise) {
1104         ofp_print_port_features(string, pm.advertise);
1105     } else {
1106         ds_put_format(string, "UNCHANGED\n");
1107     }
1108 }
1109
1110 static void
1111 ofp_print_error(struct ds *string, enum ofperr error)
1112 {
1113     if (string->length) {
1114         ds_put_char(string, ' ');
1115     }
1116     ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1117 }
1118
1119 static void
1120 ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
1121 {
1122     size_t len = ntohs(oem->header.length);
1123     size_t payload_ofs, payload_len;
1124     const void *payload;
1125     enum ofperr error;
1126     char *s;
1127
1128     error = ofperr_decode_msg(&oem->header, &payload_ofs);
1129     if (!error) {
1130         ds_put_cstr(string, "***decode error***");
1131         ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
1132         return;
1133     }
1134
1135     ds_put_format(string, " %s\n", ofperr_get_name(error));
1136
1137     payload = (const uint8_t *) oem + payload_ofs;
1138     payload_len = len - payload_ofs;
1139     if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
1140         ds_put_printable(string, payload, payload_len);
1141     } else {
1142         s = ofp_to_string(payload, payload_len, 1);
1143         ds_put_cstr(string, s);
1144         free(s);
1145     }
1146 }
1147
1148 static void
1149 ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
1150 {
1151     struct ofputil_port_status ps;
1152     enum ofperr error;
1153
1154     error = ofputil_decode_port_status(ops, &ps);
1155     if (error) {
1156         ofp_print_error(string, error);
1157         return;
1158     }
1159
1160     if (ps.reason == OFPPR_ADD) {
1161         ds_put_format(string, " ADD:");
1162     } else if (ps.reason == OFPPR_DELETE) {
1163         ds_put_format(string, " DEL:");
1164     } else if (ps.reason == OFPPR_MODIFY) {
1165         ds_put_format(string, " MOD:");
1166     }
1167
1168     ofp_print_phy_port(string, &ps.desc);
1169 }
1170
1171 static void
1172 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_desc_stats *ods)
1173 {
1174     ds_put_char(string, '\n');
1175     ds_put_format(string, "Manufacturer: %.*s\n",
1176             (int) sizeof ods->mfr_desc, ods->mfr_desc);
1177     ds_put_format(string, "Hardware: %.*s\n",
1178             (int) sizeof ods->hw_desc, ods->hw_desc);
1179     ds_put_format(string, "Software: %.*s\n",
1180             (int) sizeof ods->sw_desc, ods->sw_desc);
1181     ds_put_format(string, "Serial Num: %.*s\n",
1182             (int) sizeof ods->serial_num, ods->serial_num);
1183     ds_put_format(string, "DP Description: %.*s\n",
1184             (int) sizeof ods->dp_desc, ods->dp_desc);
1185 }
1186
1187 static void
1188 ofp_print_flow_stats_request(struct ds *string,
1189                              const struct ofp_stats_msg *osm)
1190 {
1191     struct ofputil_flow_stats_request fsr;
1192     enum ofperr error;
1193
1194     error = ofputil_decode_flow_stats_request(&fsr, &osm->header);
1195     if (error) {
1196         ofp_print_error(string, error);
1197         return;
1198     }
1199
1200     if (fsr.table_id != 0xff) {
1201         ds_put_format(string, " table=%"PRIu8, fsr.table_id);
1202     }
1203
1204     if (fsr.out_port != OFPP_NONE) {
1205         ds_put_cstr(string, " out_port=");
1206         ofputil_format_port(fsr.out_port, string);
1207     }
1208
1209     /* A flow stats request doesn't include a priority, but cls_rule_format()
1210      * will print one unless it is OFP_DEFAULT_PRIORITY. */
1211     fsr.match.priority = OFP_DEFAULT_PRIORITY;
1212
1213     ds_put_char(string, ' ');
1214     cls_rule_format(&fsr.match, string);
1215 }
1216
1217 static void
1218 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1219 {
1220     struct ofpbuf b;
1221
1222     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1223     for (;;) {
1224         struct ofputil_flow_stats fs;
1225         int retval;
1226
1227         retval = ofputil_decode_flow_stats_reply(&fs, &b, true);
1228         if (retval) {
1229             if (retval != EOF) {
1230                 ds_put_cstr(string, " ***parse error***");
1231             }
1232             break;
1233         }
1234
1235         ds_put_char(string, '\n');
1236
1237         ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1238                       ntohll(fs.cookie));
1239         ofp_print_duration(string, fs.duration_sec, fs.duration_nsec);
1240         ds_put_format(string, ", table=%"PRIu8", ", fs.table_id);
1241         ds_put_format(string, "n_packets=%"PRIu64", ", fs.packet_count);
1242         ds_put_format(string, "n_bytes=%"PRIu64", ", fs.byte_count);
1243         if (fs.idle_timeout != OFP_FLOW_PERMANENT) {
1244             ds_put_format(string, "idle_timeout=%"PRIu16", ", fs.idle_timeout);
1245         }
1246         if (fs.hard_timeout != OFP_FLOW_PERMANENT) {
1247             ds_put_format(string, "hard_timeout=%"PRIu16", ", fs.hard_timeout);
1248         }
1249         if (fs.idle_age >= 0) {
1250             ds_put_format(string, "idle_age=%d, ", fs.idle_age);
1251         }
1252         if (fs.hard_age >= 0 && fs.hard_age != fs.duration_sec) {
1253             ds_put_format(string, "hard_age=%d, ", fs.hard_age);
1254         }
1255
1256         cls_rule_format(&fs.rule, string);
1257         if (string->string[string->length - 1] != ' ') {
1258             ds_put_char(string, ' ');
1259         }
1260         ofp_print_actions(string, fs.actions, fs.n_actions);
1261      }
1262 }
1263
1264 static void
1265 ofp_print_ofpst_aggregate_reply(struct ds *string,
1266                                 const struct ofp_aggregate_stats_reply *asr)
1267 {
1268     ds_put_format(string, " packet_count=%"PRIu64,
1269                   ntohll(get_32aligned_be64(&asr->packet_count)));
1270     ds_put_format(string, " byte_count=%"PRIu64,
1271                   ntohll(get_32aligned_be64(&asr->byte_count)));
1272     ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1273 }
1274
1275 static void
1276 ofp_print_nxst_aggregate_reply(struct ds *string,
1277                                const struct nx_aggregate_stats_reply *nasr)
1278 {
1279     ds_put_format(string, " packet_count=%"PRIu64, ntohll(nasr->packet_count));
1280     ds_put_format(string, " byte_count=%"PRIu64, ntohll(nasr->byte_count));
1281     ds_put_format(string, " flow_count=%"PRIu32, ntohl(nasr->flow_count));
1282 }
1283
1284 static void print_port_stat(struct ds *string, const char *leader,
1285                             const ovs_32aligned_be64 *statp, int more)
1286 {
1287     uint64_t stat = ntohll(get_32aligned_be64(statp));
1288
1289     ds_put_cstr(string, leader);
1290     if (stat != UINT64_MAX) {
1291         ds_put_format(string, "%"PRIu64, stat);
1292     } else {
1293         ds_put_char(string, '?');
1294     }
1295     if (more) {
1296         ds_put_cstr(string, ", ");
1297     } else {
1298         ds_put_cstr(string, "\n");
1299     }
1300 }
1301
1302 static void
1303 ofp_print_ofpst_port_request(struct ds *string,
1304                              const struct ofp_port_stats_request *psr)
1305 {
1306     ds_put_format(string, " port_no=%"PRIu16, ntohs(psr->port_no));
1307 }
1308
1309 static void
1310 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1311                            int verbosity)
1312 {
1313     const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1314     size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
1315     ds_put_format(string, " %zu ports\n", n);
1316     if (verbosity < 1) {
1317         return;
1318     }
1319
1320     for (; n--; ps++) {
1321         ds_put_format(string, "  port %2"PRIu16": ", ntohs(ps->port_no));
1322
1323         ds_put_cstr(string, "rx ");
1324         print_port_stat(string, "pkts=", &ps->rx_packets, 1);
1325         print_port_stat(string, "bytes=", &ps->rx_bytes, 1);
1326         print_port_stat(string, "drop=", &ps->rx_dropped, 1);
1327         print_port_stat(string, "errs=", &ps->rx_errors, 1);
1328         print_port_stat(string, "frame=", &ps->rx_frame_err, 1);
1329         print_port_stat(string, "over=", &ps->rx_over_err, 1);
1330         print_port_stat(string, "crc=", &ps->rx_crc_err, 0);
1331
1332         ds_put_cstr(string, "           tx ");
1333         print_port_stat(string, "pkts=", &ps->tx_packets, 1);
1334         print_port_stat(string, "bytes=", &ps->tx_bytes, 1);
1335         print_port_stat(string, "drop=", &ps->tx_dropped, 1);
1336         print_port_stat(string, "errs=", &ps->tx_errors, 1);
1337         print_port_stat(string, "coll=", &ps->collisions, 0);
1338     }
1339 }
1340
1341 static void
1342 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1343                             int verbosity)
1344 {
1345     const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1346     size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
1347     ds_put_format(string, " %zu tables\n", n);
1348     if (verbosity < 1) {
1349         return;
1350     }
1351
1352     for (; n--; ts++) {
1353         char name[OFP_MAX_TABLE_NAME_LEN + 1];
1354         ovs_strlcpy(name, ts->name, sizeof name);
1355
1356         ds_put_format(string, "  %d: %-8s: ", ts->table_id, name);
1357         ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1358         ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1359         ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1360         ds_put_cstr(string, "               ");
1361         ds_put_format(string, "lookup=%"PRIu64", ",
1362                       ntohll(get_32aligned_be64(&ts->lookup_count)));
1363         ds_put_format(string, "matched=%"PRIu64"\n",
1364                       ntohll(get_32aligned_be64(&ts->matched_count)));
1365      }
1366 }
1367
1368 static void
1369 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1370 {
1371     if (queue_id == OFPQ_ALL) {
1372         ds_put_cstr(string, "ALL");
1373     } else {
1374         ds_put_format(string, "%"PRIu32, queue_id);
1375     }
1376 }
1377
1378 static void
1379 ofp_print_ofpst_queue_request(struct ds *string,
1380                               const struct ofp_queue_stats_request *qsr)
1381 {
1382     ds_put_cstr(string, "port=");
1383     ofputil_format_port(ntohs(qsr->port_no), string);
1384
1385     ds_put_cstr(string, " queue=");
1386     ofp_print_queue_name(string, ntohl(qsr->queue_id));
1387 }
1388
1389 static void
1390 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1391                             int verbosity)
1392 {
1393     const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1394     size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
1395     ds_put_format(string, " %zu queues\n", n);
1396     if (verbosity < 1) {
1397         return;
1398     }
1399
1400     for (; n--; qs++) {
1401         ds_put_cstr(string, "  port ");
1402         ofputil_format_port(ntohs(qs->port_no), string);
1403         ds_put_cstr(string, " queue ");
1404         ofp_print_queue_name(string, ntohl(qs->queue_id));
1405         ds_put_cstr(string, ": ");
1406
1407         print_port_stat(string, "bytes=", &qs->tx_bytes, 1);
1408         print_port_stat(string, "pkts=", &qs->tx_packets, 1);
1409         print_port_stat(string, "errors=", &qs->tx_errors, 0);
1410     }
1411 }
1412
1413 static void
1414 ofp_print_ofpst_port_desc_reply(struct ds *string,
1415                                 const struct ofp_header *oh)
1416 {
1417     struct ofpbuf b;
1418
1419     ofpbuf_use_const(&b, oh, ntohs(oh->length));
1420     ofpbuf_pull(&b, sizeof(struct ofp_stats_msg));
1421     ds_put_char(string, '\n');
1422     ofp_print_phy_ports(string, oh->version, &b);
1423 }
1424
1425 static void
1426 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1427 {
1428     const struct ofp_stats_msg *srq = (const struct ofp_stats_msg *) oh;
1429
1430     if (srq->flags) {
1431         ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1432                       ntohs(srq->flags));
1433     }
1434 }
1435
1436 static void
1437 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1438 {
1439     const struct ofp_stats_msg *srp = (const struct ofp_stats_msg *) oh;
1440
1441     if (srp->flags) {
1442         uint16_t flags = ntohs(srp->flags);
1443
1444         ds_put_cstr(string, " flags=");
1445         if (flags & OFPSF_REPLY_MORE) {
1446             ds_put_cstr(string, "[more]");
1447             flags &= ~OFPSF_REPLY_MORE;
1448         }
1449         if (flags) {
1450             ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1451                           flags);
1452         }
1453     }
1454 }
1455
1456 static void
1457 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1458 {
1459     size_t len = ntohs(oh->length);
1460
1461     ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1462     if (verbosity > 1) {
1463         ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1464     }
1465 }
1466
1467 static void
1468 ofp_print_nxt_role_message(struct ds *string,
1469                            const struct nx_role_request *nrr)
1470 {
1471     unsigned int role = ntohl(nrr->role);
1472
1473     ds_put_cstr(string, " role=");
1474     if (role == NX_ROLE_OTHER) {
1475         ds_put_cstr(string, "other");
1476     } else if (role == NX_ROLE_MASTER) {
1477         ds_put_cstr(string, "master");
1478     } else if (role == NX_ROLE_SLAVE) {
1479         ds_put_cstr(string, "slave");
1480     } else {
1481         ds_put_format(string, "%u", role);
1482     }
1483 }
1484
1485 static void
1486 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1487                                 const struct nx_flow_mod_table_id *nfmti)
1488 {
1489     ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1490 }
1491
1492 static void
1493 ofp_print_nxt_set_flow_format(struct ds *string,
1494                               const struct nx_set_flow_format *nsff)
1495 {
1496     uint32_t format = ntohl(nsff->format);
1497
1498     ds_put_cstr(string, " format=");
1499     if (ofputil_nx_flow_format_is_valid(format)) {
1500         ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
1501     } else {
1502         ds_put_format(string, "%"PRIu32, format);
1503     }
1504 }
1505
1506 static void
1507 ofp_print_nxt_set_packet_in_format(struct ds *string,
1508                                    const struct nx_set_packet_in_format *nspf)
1509 {
1510     uint32_t format = ntohl(nspf->format);
1511
1512     ds_put_cstr(string, " format=");
1513     if (ofputil_packet_in_format_is_valid(format)) {
1514         ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1515     } else {
1516         ds_put_format(string, "%"PRIu32, format);
1517     }
1518 }
1519
1520 static const char *
1521 ofp_port_reason_to_string(enum ofp_port_reason reason)
1522 {
1523     static char s[32];
1524
1525     switch (reason) {
1526     case OFPPR_ADD:
1527         return "add";
1528
1529     case OFPPR_DELETE:
1530         return "delete";
1531
1532     case OFPPR_MODIFY:
1533         return "modify";
1534
1535     default:
1536         sprintf(s, "%d", (int) reason);
1537         return s;
1538     }
1539 }
1540
1541 static void
1542 ofp_print_nxt_set_async_config(struct ds *string,
1543                                const struct nx_async_config *nac)
1544 {
1545     int i;
1546
1547     for (i = 0; i < 2; i++) {
1548         int j;
1549
1550         ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1551
1552         ds_put_cstr(string, "       PACKET_IN:");
1553         for (j = 0; j < 32; j++) {
1554             if (nac->packet_in_mask[i] & htonl(1u << j)) {
1555                 ds_put_format(string, " %s",
1556                               ofputil_packet_in_reason_to_string(j));
1557             }
1558         }
1559         if (!nac->packet_in_mask[i]) {
1560             ds_put_cstr(string, " (off)");
1561         }
1562         ds_put_char(string, '\n');
1563
1564         ds_put_cstr(string, "     PORT_STATUS:");
1565         for (j = 0; j < 32; j++) {
1566             if (nac->port_status_mask[i] & htonl(1u << j)) {
1567                 ds_put_format(string, " %s", ofp_port_reason_to_string(j));
1568             }
1569         }
1570         if (!nac->port_status_mask[i]) {
1571             ds_put_cstr(string, " (off)");
1572         }
1573         ds_put_char(string, '\n');
1574
1575         ds_put_cstr(string, "    FLOW_REMOVED:");
1576         for (j = 0; j < 32; j++) {
1577             if (nac->flow_removed_mask[i] & htonl(1u << j)) {
1578                 ds_put_format(string, " %s",
1579                               ofp_flow_removed_reason_to_string(j));
1580             }
1581         }
1582         if (!nac->flow_removed_mask[i]) {
1583             ds_put_cstr(string, " (off)");
1584         }
1585         ds_put_char(string, '\n');
1586     }
1587 }
1588
1589 static void
1590 ofp_print_nxt_set_controller_id(struct ds *string,
1591                                 const struct nx_controller_id *nci)
1592 {
1593     ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
1594 }
1595
1596 static void
1597 ofp_to_string__(const struct ofp_header *oh,
1598                 const struct ofputil_msg_type *type, struct ds *string,
1599                 int verbosity)
1600 {
1601     enum ofputil_msg_code code;
1602     const void *msg = oh;
1603
1604     ds_put_cstr(string, ofputil_msg_type_name(type));
1605     switch (oh->version) {
1606     case OFP10_VERSION:
1607         break;
1608     case OFP11_VERSION:
1609         ds_put_cstr(string, " (OF1.1)");
1610         break;
1611     default:
1612         ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
1613         break;
1614     }
1615     ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
1616
1617     code = ofputil_msg_type_code(type);
1618     switch (code) {
1619     case OFPUTIL_MSG_INVALID:
1620         break;
1621
1622     case OFPUTIL_OFPT_HELLO:
1623         ds_put_char(string, '\n');
1624         ds_put_hex_dump(string, oh + 1, ntohs(oh->length) - sizeof *oh,
1625                         0, true);
1626         break;
1627
1628     case OFPUTIL_OFPT_ERROR:
1629         ofp_print_error_msg(string, msg);
1630         break;
1631
1632     case OFPUTIL_OFPT_ECHO_REQUEST:
1633     case OFPUTIL_OFPT_ECHO_REPLY:
1634         ofp_print_echo(string, oh, verbosity);
1635         break;
1636
1637     case OFPUTIL_OFPT_FEATURES_REQUEST:
1638         break;
1639
1640     case OFPUTIL_OFPT_FEATURES_REPLY:
1641         ofp_print_switch_features(string, msg);
1642         break;
1643
1644     case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1645         break;
1646
1647     case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1648     case OFPUTIL_OFPT_SET_CONFIG:
1649         ofp_print_switch_config(string, msg);
1650         break;
1651
1652     case OFPUTIL_OFPT_PACKET_IN:
1653     case OFPUTIL_NXT_PACKET_IN:
1654         ofp_print_packet_in(string, msg, verbosity);
1655         break;
1656
1657     case OFPUTIL_OFPT_FLOW_REMOVED:
1658     case OFPUTIL_NXT_FLOW_REMOVED:
1659         ofp_print_flow_removed(string, msg);
1660         break;
1661
1662     case OFPUTIL_OFPT_PORT_STATUS:
1663         ofp_print_port_status(string, msg);
1664         break;
1665
1666     case OFPUTIL_OFPT_PACKET_OUT:
1667         ofp_print_packet_out(string, msg, verbosity);
1668         break;
1669
1670     case OFPUTIL_OFPT_FLOW_MOD:
1671     case OFPUTIL_NXT_FLOW_MOD:
1672         ofp_print_flow_mod(string, msg, code, verbosity);
1673         break;
1674
1675     case OFPUTIL_OFPT_PORT_MOD:
1676         ofp_print_port_mod(string, msg);
1677         break;
1678
1679     case OFPUTIL_OFPT_BARRIER_REQUEST:
1680     case OFPUTIL_OFPT_BARRIER_REPLY:
1681         break;
1682
1683     case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1684     case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1685         /* XXX */
1686         break;
1687
1688     case OFPUTIL_OFPST_DESC_REQUEST:
1689     case OFPUTIL_OFPST_PORT_DESC_REQUEST:
1690         ofp_print_stats_request(string, oh);
1691         break;
1692
1693     case OFPUTIL_OFPST_FLOW_REQUEST:
1694     case OFPUTIL_NXST_FLOW_REQUEST:
1695     case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1696     case OFPUTIL_NXST_AGGREGATE_REQUEST:
1697         ofp_print_stats_request(string, oh);
1698         ofp_print_flow_stats_request(string, msg);
1699         break;
1700
1701     case OFPUTIL_OFPST_TABLE_REQUEST:
1702         ofp_print_stats_request(string, oh);
1703         break;
1704
1705     case OFPUTIL_OFPST_PORT_REQUEST:
1706         ofp_print_stats_request(string, oh);
1707         ofp_print_ofpst_port_request(string, msg);
1708         break;
1709
1710     case OFPUTIL_OFPST_QUEUE_REQUEST:
1711         ofp_print_stats_request(string, oh);
1712         ofp_print_ofpst_queue_request(string, msg);
1713         break;
1714
1715     case OFPUTIL_OFPST_DESC_REPLY:
1716         ofp_print_stats_reply(string, oh);
1717         ofp_print_ofpst_desc_reply(string, msg);
1718         break;
1719
1720     case OFPUTIL_OFPST_FLOW_REPLY:
1721     case OFPUTIL_NXST_FLOW_REPLY:
1722         ofp_print_stats_reply(string, oh);
1723         ofp_print_flow_stats_reply(string, oh);
1724         break;
1725
1726     case OFPUTIL_OFPST_QUEUE_REPLY:
1727         ofp_print_stats_reply(string, oh);
1728         ofp_print_ofpst_queue_reply(string, oh, verbosity);
1729         break;
1730
1731     case OFPUTIL_OFPST_PORT_REPLY:
1732         ofp_print_stats_reply(string, oh);
1733         ofp_print_ofpst_port_reply(string, oh, verbosity);
1734         break;
1735
1736     case OFPUTIL_OFPST_TABLE_REPLY:
1737         ofp_print_stats_reply(string, oh);
1738         ofp_print_ofpst_table_reply(string, oh, verbosity);
1739         break;
1740
1741     case OFPUTIL_OFPST_AGGREGATE_REPLY:
1742         ofp_print_stats_reply(string, oh);
1743         ofp_print_ofpst_aggregate_reply(string, msg);
1744         break;
1745
1746     case OFPUTIL_OFPST_PORT_DESC_REPLY:
1747         ofp_print_stats_reply(string, oh);
1748         ofp_print_ofpst_port_desc_reply(string, oh);
1749         break;
1750
1751     case OFPUTIL_NXT_ROLE_REQUEST:
1752     case OFPUTIL_NXT_ROLE_REPLY:
1753         ofp_print_nxt_role_message(string, msg);
1754         break;
1755
1756     case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
1757         ofp_print_nxt_flow_mod_table_id(string, msg);
1758         break;
1759
1760     case OFPUTIL_NXT_SET_FLOW_FORMAT:
1761         ofp_print_nxt_set_flow_format(string, msg);
1762         break;
1763
1764     case OFPUTIL_NXT_SET_PACKET_IN_FORMAT:
1765         ofp_print_nxt_set_packet_in_format(string, msg);
1766         break;
1767
1768     case OFPUTIL_NXT_FLOW_AGE:
1769         break;
1770
1771     case OFPUTIL_NXT_SET_CONTROLLER_ID:
1772         ofp_print_nxt_set_controller_id(string, msg);
1773         break;
1774
1775     case OFPUTIL_NXT_SET_ASYNC_CONFIG:
1776         ofp_print_nxt_set_async_config(string, msg);
1777         break;
1778
1779     case OFPUTIL_NXST_AGGREGATE_REPLY:
1780         ofp_print_stats_reply(string, oh);
1781         ofp_print_nxst_aggregate_reply(string, msg);
1782         break;
1783     }
1784 }
1785
1786 /* Composes and returns a string representing the OpenFlow packet of 'len'
1787  * bytes at 'oh' at the given 'verbosity' level.  0 is a minimal amount of
1788  * verbosity and higher numbers increase verbosity.  The caller is responsible
1789  * for freeing the string. */
1790 char *
1791 ofp_to_string(const void *oh_, size_t len, int verbosity)
1792 {
1793     struct ds string = DS_EMPTY_INITIALIZER;
1794     const struct ofp_header *oh = oh_;
1795
1796     if (!len) {
1797         ds_put_cstr(&string, "OpenFlow message is empty\n");
1798     } else if (len < sizeof(struct ofp_header)) {
1799         ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
1800                       len);
1801     } else if (ntohs(oh->length) > len) {
1802         ds_put_format(&string,
1803                       "(***truncated to %zu bytes from %"PRIu16"***)\n",
1804                       len, ntohs(oh->length));
1805     } else if (ntohs(oh->length) < len) {
1806         ds_put_format(&string,
1807                       "(***only uses %"PRIu16" bytes out of %zu***)\n",
1808                       ntohs(oh->length), len);
1809     } else {
1810         const struct ofputil_msg_type *type;
1811         enum ofperr error;
1812
1813         error = ofputil_decode_msg_type(oh, &type);
1814         if (!error) {
1815             ofp_to_string__(oh, type, &string, verbosity);
1816             if (verbosity >= 5) {
1817                 if (ds_last(&string) != '\n') {
1818                     ds_put_char(&string, '\n');
1819                 }
1820                 ds_put_hex_dump(&string, oh, len, 0, true);
1821             }
1822             if (ds_last(&string) != '\n') {
1823                 ds_put_char(&string, '\n');
1824             }
1825             return ds_steal_cstr(&string);
1826         }
1827
1828         ofp_print_error(&string, error);
1829     }
1830     ds_put_hex_dump(&string, oh, len, 0, true);
1831     return ds_steal_cstr(&string);
1832 }
1833 \f
1834 static void
1835 print_and_free(FILE *stream, char *string)
1836 {
1837     fputs(string, stream);
1838     free(string);
1839 }
1840
1841 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1842  * given 'verbosity' level.  0 is a minimal amount of verbosity and higher
1843  * numbers increase verbosity. */
1844 void
1845 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1846 {
1847     print_and_free(stream, ofp_to_string(oh, len, verbosity));
1848 }
1849
1850 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1851  * 'data' to 'stream'. */
1852 void
1853 ofp_print_packet(FILE *stream, const void *data, size_t len)
1854 {
1855     print_and_free(stream, ofp_packet_to_string(data, len));
1856 }