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