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