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