]> git.proxmox.com Git - ovs.git/blame - lib/ofp-print.c
lib: Move compiler.h to <openvswitch/compiler.h>
[ovs.git] / lib / ofp-print.c
CommitLineData
064af421 1/*
5deff5aa 2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014 Nicira, Inc.
064af421 3 *
a14bc59f
BP
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:
064af421 7 *
a14bc59f
BP
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.
064af421
BP
15 */
16
17#include <config.h>
18#include "ofp-print.h"
064af421
BP
19
20#include <errno.h>
21#include <inttypes.h>
7f3adc00 22#include <sys/types.h>
064af421
BP
23#include <netinet/in.h>
24#include <sys/wait.h>
25#include <stdarg.h>
26#include <stdlib.h>
27#include <ctype.h>
28
daff3353 29#include "bundle.h"
10a24935 30#include "byte-order.h"
064af421
BP
31#include "compiler.h"
32#include "dynamic-string.h"
33#include "flow.h"
75a75043 34#include "learn.h"
53ddd40a 35#include "multipath.h"
816fd533 36#include "meta-flow.h"
9e1fd49b 37#include "netdev.h"
7fa91113 38#include "nx-match.h"
f25d0cf3 39#include "ofp-actions.h"
90bf1e07 40#include "ofp-errors.h"
982697a4 41#include "ofp-msgs.h"
69b6be19 42#include "ofp-util.h"
064af421
BP
43#include "ofpbuf.h"
44#include "openflow/openflow.h"
45#include "openflow/nicira-ext.h"
46#include "packets.h"
e41a9130 47#include "type-props.h"
c4617b3c 48#include "unaligned.h"
b5e7e61a 49#include "odp-util.h"
064af421
BP
50#include "util.h"
51
d2805da2 52static void ofp_print_queue_name(struct ds *string, uint32_t port);
90bf1e07 53static void ofp_print_error(struct ds *, enum ofperr);
3c1bb396
BP
54static void ofp_print_table_features(struct ds *,
55 const struct ofputil_table_features *,
56 const struct ofputil_table_stats *);
064af421
BP
57
58/* Returns a string that represents the contents of the Ethernet frame in the
897a8e07 59 * 'len' bytes starting at 'data'. The caller must free the returned string.*/
064af421 60char *
c499c75d 61ofp_packet_to_string(const void *data, size_t len)
064af421
BP
62{
63 struct ds ds = DS_EMPTY_INITIALIZER;
03fbdf8d 64 const struct pkt_metadata md = PKT_METADATA_INITIALIZER(0);
064af421 65 struct ofpbuf buf;
897a8e07 66 struct flow flow;
5a51b2cd 67 size_t l4_size;
064af421 68
0bc9407d 69 ofpbuf_use_const(&buf, data, len);
b5e7e61a 70 flow_extract(&buf, &md, &flow);
897a8e07 71 flow_format(&ds, &flow);
e50abca5 72
6b8c377a 73 l4_size = ofpbuf_l4_size(&buf);
5a51b2cd
JR
74
75 if (flow.nw_proto == IPPROTO_TCP && l4_size >= TCP_HEADER_LEN) {
6b8c377a 76 struct tcp_header *th = ofpbuf_l4(&buf);
5a51b2cd
JR
77 ds_put_format(&ds, " tcp_csum:%"PRIx16, ntohs(th->tcp_csum));
78 } else if (flow.nw_proto == IPPROTO_UDP && l4_size >= UDP_HEADER_LEN) {
6b8c377a 79 struct udp_header *uh = ofpbuf_l4(&buf);
5a51b2cd
JR
80 ds_put_format(&ds, " udp_csum:%"PRIx16, ntohs(uh->udp_csum));
81 } else if (flow.nw_proto == IPPROTO_SCTP && l4_size >= SCTP_HEADER_LEN) {
6b8c377a 82 struct sctp_header *sh = ofpbuf_l4(&buf);
5fa008d4
BP
83 ds_put_format(&ds, " sctp_csum:%"PRIx32,
84 ntohl(get_16aligned_be32(&sh->sctp_csum)));
e50abca5
EJ
85 }
86
897a8e07 87 ds_put_char(&ds, '\n');
064af421 88
064af421
BP
89 return ds_cstr(&ds);
90}
91
064af421 92static void
65120a8a 93ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
d1e2cf21 94 int verbosity)
064af421 95{
5014a89d 96 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
65120a8a
EJ
97 struct ofputil_packet_in pin;
98 int error;
5d6c3af0 99 int i;
064af421 100
65120a8a
EJ
101 error = ofputil_decode_packet_in(&pin, oh);
102 if (error) {
103 ofp_print_error(string, error);
104 return;
105 }
106
54834960
EJ
107 if (pin.table_id) {
108 ds_put_format(string, " table_id=%"PRIu8, pin.table_id);
109 }
110
d4fa4e79 111 if (pin.cookie != OVS_BE64_MAX) {
54834960
EJ
112 ds_put_format(string, " cookie=0x%"PRIx64, ntohll(pin.cookie));
113 }
114
34582733 115 ds_put_format(string, " total_len=%"PRIuSIZE" in_port=", pin.total_len);
5d6c3af0
EJ
116 ofputil_format_port(pin.fmd.in_port, string);
117
42edbe39 118 if (pin.fmd.tun_id != htonll(0)) {
5d6c3af0 119 ds_put_format(string, " tun_id=0x%"PRIx64, ntohll(pin.fmd.tun_id));
5d6c3af0
EJ
120 }
121
0ad90c84
JR
122 if (pin.fmd.tun_src != htonl(0)) {
123 ds_put_format(string, " tun_src="IP_FMT, IP_ARGS(pin.fmd.tun_src));
124 }
125
126 if (pin.fmd.tun_dst != htonl(0)) {
127 ds_put_format(string, " tun_dst="IP_FMT, IP_ARGS(pin.fmd.tun_dst));
128 }
129
42edbe39 130 if (pin.fmd.metadata != htonll(0)) {
969fc56c 131 ds_put_format(string, " metadata=0x%"PRIx64, ntohll(pin.fmd.metadata));
969fc56c
JS
132 }
133
5d6c3af0 134 for (i = 0; i < FLOW_N_REGS; i++) {
42edbe39 135 if (pin.fmd.regs[i]) {
5d6c3af0 136 ds_put_format(string, " reg%d=0x%"PRIx32, i, pin.fmd.regs[i]);
5d6c3af0
EJ
137 }
138 }
064af421 139
ac923e91
JG
140 if (pin.fmd.pkt_mark != 0) {
141 ds_put_format(string, " pkt_mark=0x%"PRIx32, pin.fmd.pkt_mark);
142 }
143
80d5aefd 144 ds_put_format(string, " (via %s)",
5014a89d
BP
145 ofputil_packet_in_reason_to_string(pin.reason, reasonbuf,
146 sizeof reasonbuf));
064af421 147
34582733 148 ds_put_format(string, " data_len=%"PRIuSIZE, pin.packet_len);
65120a8a 149 if (pin.buffer_id == UINT32_MAX) {
064af421 150 ds_put_format(string, " (unbuffered)");
65120a8a 151 if (pin.total_len != pin.packet_len) {
064af421 152 ds_put_format(string, " (***total_len != data_len***)");
65120a8a 153 }
064af421 154 } else {
65120a8a
EJ
155 ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
156 if (pin.total_len < pin.packet_len) {
064af421 157 ds_put_format(string, " (***total_len < data_len***)");
65120a8a 158 }
064af421
BP
159 }
160 ds_put_char(string, '\n');
161
162 if (verbosity > 0) {
65120a8a 163 char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
064af421
BP
164 ds_put_cstr(string, packet);
165 free(packet);
166 }
b4ccee75
SH
167 if (verbosity > 2) {
168 ds_put_hex_dump(string, pin.packet, pin.packet_len, 0, false);
169 }
064af421
BP
170}
171
d1e2cf21 172static void
982697a4 173ofp_print_packet_out(struct ds *string, const struct ofp_header *oh,
d1e2cf21 174 int verbosity)
064af421 175{
c6a93eb7 176 struct ofputil_packet_out po;
f25d0cf3 177 struct ofpbuf ofpacts;
c6a93eb7 178 enum ofperr error;
064af421 179
f25d0cf3 180 ofpbuf_init(&ofpacts, 64);
982697a4 181 error = ofputil_decode_packet_out(&po, oh, &ofpacts);
c6a93eb7 182 if (error) {
f25d0cf3 183 ofpbuf_uninit(&ofpacts);
c6a93eb7 184 ofp_print_error(string, error);
064af421
BP
185 return;
186 }
c6a93eb7
BP
187
188 ds_put_cstr(string, " in_port=");
189 ofputil_format_port(po.in_port, string);
190
455ecd77 191 ds_put_cstr(string, " actions=");
f25d0cf3 192 ofpacts_format(po.ofpacts, po.ofpacts_len, string);
c6a93eb7
BP
193
194 if (po.buffer_id == UINT32_MAX) {
34582733 195 ds_put_format(string, " data_len=%"PRIuSIZE, po.packet_len);
c6a93eb7
BP
196 if (verbosity > 0 && po.packet_len > 0) {
197 char *packet = ofp_packet_to_string(po.packet, po.packet_len);
064af421
BP
198 ds_put_char(string, '\n');
199 ds_put_cstr(string, packet);
200 free(packet);
201 }
b4ccee75
SH
202 if (verbosity > 2) {
203 ds_put_hex_dump(string, po.packet, po.packet_len, 0, false);
204 }
064af421 205 } else {
c6a93eb7 206 ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id);
064af421 207 }
f25d0cf3
BP
208
209 ofpbuf_uninit(&ofpacts);
064af421
BP
210}
211
212/* qsort comparison function. */
213static int
214compare_ports(const void *a_, const void *b_)
215{
9e1fd49b
BP
216 const struct ofputil_phy_port *a = a_;
217 const struct ofputil_phy_port *b = b_;
4e022ec0
AW
218 uint16_t ap = ofp_to_u16(a->port_no);
219 uint16_t bp = ofp_to_u16(b->port_no);
064af421
BP
220
221 return ap < bp ? -1 : ap > bp;
222}
223
0ab14c8e
BP
224static void
225ofp_print_bit_names(struct ds *string, uint32_t bits,
e8fa940e
BP
226 const char *(*bit_to_name)(uint32_t bit),
227 char separator)
064af421 228{
0ab14c8e 229 int n = 0;
9e1fd49b 230 int i;
0ab14c8e
BP
231
232 if (!bits) {
233 ds_put_cstr(string, "0");
064af421
BP
234 return;
235 }
0ab14c8e 236
9e1fd49b
BP
237 for (i = 0; i < 32; i++) {
238 uint32_t bit = UINT32_C(1) << i;
239
240 if (bits & bit) {
241 const char *name = bit_to_name(bit);
242 if (name) {
243 if (n++) {
e8fa940e 244 ds_put_char(string, separator);
9e1fd49b
BP
245 }
246 ds_put_cstr(string, name);
247 bits &= ~bit;
0ab14c8e 248 }
0ab14c8e 249 }
064af421 250 }
0ab14c8e
BP
251
252 if (bits) {
f5cd6874 253 if (n) {
e8fa940e 254 ds_put_char(string, separator);
0ab14c8e
BP
255 }
256 ds_put_format(string, "0x%"PRIx32, bits);
064af421 257 }
0ab14c8e
BP
258}
259
9e1fd49b
BP
260static const char *
261netdev_feature_to_name(uint32_t bit)
262{
263 enum netdev_features f = bit;
264
265 switch (f) {
266 case NETDEV_F_10MB_HD: return "10MB-HD";
267 case NETDEV_F_10MB_FD: return "10MB-FD";
268 case NETDEV_F_100MB_HD: return "100MB-HD";
269 case NETDEV_F_100MB_FD: return "100MB-FD";
270 case NETDEV_F_1GB_HD: return "1GB-HD";
271 case NETDEV_F_1GB_FD: return "1GB-FD";
272 case NETDEV_F_10GB_FD: return "10GB-FD";
273 case NETDEV_F_40GB_FD: return "40GB-FD";
274 case NETDEV_F_100GB_FD: return "100GB-FD";
275 case NETDEV_F_1TB_FD: return "1TB-FD";
276 case NETDEV_F_OTHER: return "OTHER";
277 case NETDEV_F_COPPER: return "COPPER";
278 case NETDEV_F_FIBER: return "FIBER";
279 case NETDEV_F_AUTONEG: return "AUTO_NEG";
280 case NETDEV_F_PAUSE: return "AUTO_PAUSE";
281 case NETDEV_F_PAUSE_ASYM: return "AUTO_PAUSE_ASYM";
282 }
283
284 return NULL;
285}
286
0ab14c8e 287static void
9e1fd49b 288ofp_print_port_features(struct ds *string, enum netdev_features features)
0ab14c8e 289{
e8fa940e 290 ofp_print_bit_names(string, features, netdev_feature_to_name, ' ');
0ab14c8e
BP
291 ds_put_char(string, '\n');
292}
293
9e1fd49b
BP
294static const char *
295ofputil_port_config_to_name(uint32_t bit)
296{
297 enum ofputil_port_config pc = bit;
298
299 switch (pc) {
300 case OFPUTIL_PC_PORT_DOWN: return "PORT_DOWN";
301 case OFPUTIL_PC_NO_STP: return "NO_STP";
302 case OFPUTIL_PC_NO_RECV: return "NO_RECV";
303 case OFPUTIL_PC_NO_RECV_STP: return "NO_RECV_STP";
304 case OFPUTIL_PC_NO_FLOOD: return "NO_FLOOD";
305 case OFPUTIL_PC_NO_FWD: return "NO_FWD";
306 case OFPUTIL_PC_NO_PACKET_IN: return "NO_PACKET_IN";
307 }
308
309 return NULL;
310}
311
0ab14c8e 312static void
9e1fd49b 313ofp_print_port_config(struct ds *string, enum ofputil_port_config config)
0ab14c8e 314{
e8fa940e 315 ofp_print_bit_names(string, config, ofputil_port_config_to_name, ' ');
0ab14c8e
BP
316 ds_put_char(string, '\n');
317}
318
9e1fd49b
BP
319static const char *
320ofputil_port_state_to_name(uint32_t bit)
321{
322 enum ofputil_port_state ps = bit;
323
324 switch (ps) {
325 case OFPUTIL_PS_LINK_DOWN: return "LINK_DOWN";
326 case OFPUTIL_PS_BLOCKED: return "BLOCKED";
327 case OFPUTIL_PS_LIVE: return "LIVE";
328
329 case OFPUTIL_PS_STP_LISTEN:
330 case OFPUTIL_PS_STP_LEARN:
331 case OFPUTIL_PS_STP_FORWARD:
332 case OFPUTIL_PS_STP_BLOCK:
333 /* Handled elsewhere. */
334 return NULL;
335 }
336
337 return NULL;
338}
339
0ab14c8e 340static void
9e1fd49b 341ofp_print_port_state(struct ds *string, enum ofputil_port_state state)
0ab14c8e 342{
9e1fd49b 343 enum ofputil_port_state stp_state;
0ab14c8e
BP
344
345 /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
346 * pattern. We have to special case it.
347 *
348 * OVS doesn't support STP, so this field will always be 0 if we are
349 * talking to OVS, so we'd always print STP_LISTEN in that case.
350 * Therefore, we don't print anything at all if the value is STP_LISTEN, to
351 * avoid confusing users. */
9e1fd49b 352 stp_state = state & OFPUTIL_PS_STP_MASK;
0ab14c8e 353 if (stp_state) {
9e1fd49b
BP
354 ds_put_cstr(string,
355 (stp_state == OFPUTIL_PS_STP_LEARN ? "STP_LEARN"
356 : stp_state == OFPUTIL_PS_STP_FORWARD ? "STP_FORWARD"
357 : "STP_BLOCK"));
358 state &= ~OFPUTIL_PS_STP_MASK;
0ab14c8e 359 if (state) {
e8fa940e
BP
360 ofp_print_bit_names(string, state, ofputil_port_state_to_name,
361 ' ');
0ab14c8e
BP
362 }
363 } else {
e8fa940e 364 ofp_print_bit_names(string, state, ofputil_port_state_to_name, ' ');
064af421
BP
365 }
366 ds_put_char(string, '\n');
367}
368
369static void
9e1fd49b 370ofp_print_phy_port(struct ds *string, const struct ofputil_phy_port *port)
064af421 371{
9e1fd49b 372 char name[sizeof port->name];
064af421
BP
373 int j;
374
375 memcpy(name, port->name, sizeof name);
376 for (j = 0; j < sizeof name - 1; j++) {
858f2852 377 if (!isprint((unsigned char) name[j])) {
064af421
BP
378 break;
379 }
380 }
381 name[j] = '\0';
382
383 ds_put_char(string, ' ');
9e1fd49b 384 ofputil_format_port(port->port_no, string);
0ab14c8e
BP
385 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
386 name, ETH_ADDR_ARGS(port->hw_addr));
387
388 ds_put_cstr(string, " config: ");
9e1fd49b 389 ofp_print_port_config(string, port->config);
0ab14c8e
BP
390
391 ds_put_cstr(string, " state: ");
9e1fd49b 392 ofp_print_port_state(string, port->state);
0ab14c8e 393
064af421
BP
394 if (port->curr) {
395 ds_put_format(string, " current: ");
9e1fd49b 396 ofp_print_port_features(string, port->curr);
064af421
BP
397 }
398 if (port->advertised) {
399 ds_put_format(string, " advertised: ");
9e1fd49b 400 ofp_print_port_features(string, port->advertised);
064af421
BP
401 }
402 if (port->supported) {
403 ds_put_format(string, " supported: ");
9e1fd49b 404 ofp_print_port_features(string, port->supported);
064af421
BP
405 }
406 if (port->peer) {
407 ds_put_format(string, " peer: ");
9e1fd49b
BP
408 ofp_print_port_features(string, port->peer);
409 }
410 ds_put_format(string, " speed: %"PRIu32" Mbps now, "
411 "%"PRIu32" Mbps max\n",
412 port->curr_speed / UINT32_C(1000),
413 port->max_speed / UINT32_C(1000));
414}
415
2be393ed
JP
416/* Given a buffer 'b' that contains an array of OpenFlow ports of type
417 * 'ofp_version', writes a detailed description of each port into
418 * 'string'. */
419static void
420ofp_print_phy_ports(struct ds *string, uint8_t ofp_version,
421 struct ofpbuf *b)
422{
2be393ed 423 struct ofputil_phy_port *ports;
9b77a336
BP
424 size_t allocated_ports, n_ports;
425 int retval;
2be393ed
JP
426 size_t i;
427
9b77a336
BP
428 ports = NULL;
429 allocated_ports = 0;
430 for (n_ports = 0; ; n_ports++) {
431 if (n_ports >= allocated_ports) {
432 ports = x2nrealloc(ports, &allocated_ports, sizeof *ports);
433 }
2be393ed 434
9b77a336
BP
435 retval = ofputil_pull_phy_port(ofp_version, b, &ports[n_ports]);
436 if (retval) {
437 break;
2be393ed
JP
438 }
439 }
9b77a336 440
2be393ed
JP
441 qsort(ports, n_ports, sizeof *ports, compare_ports);
442 for (i = 0; i < n_ports; i++) {
443 ofp_print_phy_port(string, &ports[i]);
444 }
2be393ed 445 free(ports);
9b77a336
BP
446
447 if (retval != EOF) {
448 ofp_print_error(string, retval);
449 }
2be393ed
JP
450}
451
9e1fd49b
BP
452static const char *
453ofputil_capabilities_to_name(uint32_t bit)
454{
455 enum ofputil_capabilities capabilities = bit;
456
457 switch (capabilities) {
458 case OFPUTIL_C_FLOW_STATS: return "FLOW_STATS";
459 case OFPUTIL_C_TABLE_STATS: return "TABLE_STATS";
460 case OFPUTIL_C_PORT_STATS: return "PORT_STATS";
461 case OFPUTIL_C_IP_REASM: return "IP_REASM";
462 case OFPUTIL_C_QUEUE_STATS: return "QUEUE_STATS";
463 case OFPUTIL_C_ARP_MATCH_IP: return "ARP_MATCH_IP";
464 case OFPUTIL_C_STP: return "STP";
465 case OFPUTIL_C_GROUP_STATS: return "GROUP_STATS";
60202987 466 case OFPUTIL_C_PORT_BLOCKED: return "PORT_BLOCKED";
9e1fd49b
BP
467 }
468
469 return NULL;
470}
471
064af421 472static void
982697a4 473ofp_print_switch_features(struct ds *string, const struct ofp_header *oh)
064af421 474{
9e1fd49b 475 struct ofputil_switch_features features;
9e1fd49b
BP
476 enum ofperr error;
477 struct ofpbuf b;
064af421 478
982697a4 479 error = ofputil_decode_switch_features(oh, &features, &b);
9e1fd49b
BP
480 if (error) {
481 ofp_print_error(string, error);
482 return;
483 }
064af421 484
9e1fd49b 485 ds_put_format(string, " dpid:%016"PRIx64"\n", features.datapath_id);
2e1ae200
JR
486
487 ds_put_format(string, "n_tables:%"PRIu8", n_buffers:%"PRIu32,
9e1fd49b 488 features.n_tables, features.n_buffers);
2e1ae200
JR
489 if (features.auxiliary_id) {
490 ds_put_format(string, ", auxiliary_id:%"PRIu8, features.auxiliary_id);
491 }
492 ds_put_char(string, '\n');
064af421 493
9e1fd49b
BP
494 ds_put_cstr(string, "capabilities: ");
495 ofp_print_bit_names(string, features.capabilities,
e8fa940e 496 ofputil_capabilities_to_name, ' ');
9e1fd49b
BP
497 ds_put_char(string, '\n');
498
210d8d9c
SH
499 switch ((enum ofp_version)oh->version) {
500 case OFP10_VERSION:
501 ds_put_cstr(string, "actions: ");
08d1e234 502 ofpact_bitmap_format(features.ofpacts, string);
210d8d9c
SH
503 ds_put_char(string, '\n');
504 break;
505 case OFP11_VERSION:
506 case OFP12_VERSION:
507 break;
2e1ae200 508 case OFP13_VERSION:
c37c0382 509 case OFP14_VERSION:
42dccab5 510 case OFP15_VERSION:
2e1ae200 511 return; /* no ports in ofp13_switch_features */
210d8d9c 512 default:
428b2edd 513 OVS_NOT_REACHED();
210d8d9c 514 }
9e1fd49b 515
982697a4 516 ofp_print_phy_ports(string, oh->version, &b);
064af421
BP
517}
518
064af421 519static void
d1e2cf21 520ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
064af421 521{
f0fd1a17 522 enum ofp_config_flags flags;
064af421
BP
523
524 flags = ntohs(osc->flags);
3b62feba 525
7257b535
BP
526 ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
527 flags &= ~OFPC_FRAG_MASK;
528
f0fd1a17
PS
529 if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) {
530 ds_put_format(string, " invalid_ttl_to_controller");
531 flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER;
532 }
533
064af421
BP
534 if (flags) {
535 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
536 }
537
538 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
539}
540
541static void print_wild(struct ds *string, const char *leader, int is_wild,
d295e8e9 542 int verbosity, const char *format, ...)
cab50449 543 OVS_PRINTF_FORMAT(5, 6);
064af421
BP
544
545static void print_wild(struct ds *string, const char *leader, int is_wild,
d295e8e9 546 int verbosity, const char *format, ...)
064af421
BP
547{
548 if (is_wild && verbosity < 2) {
549 return;
550 }
551 ds_put_cstr(string, leader);
552 if (!is_wild) {
553 va_list args;
554
555 va_start(args, format);
556 ds_put_format_valist(string, format, args);
557 va_end(args);
558 } else {
559 ds_put_char(string, '*');
560 }
561 ds_put_char(string, ',');
562}
563
e1db42d6
JR
564static void
565print_wild_port(struct ds *string, const char *leader, int is_wild,
4e022ec0 566 int verbosity, ofp_port_t port)
e1db42d6
JR
567{
568 if (is_wild && verbosity < 2) {
569 return;
570 }
571 ds_put_cstr(string, leader);
572 if (!is_wild) {
573 ofputil_format_port(port, string);
574 } else {
575 ds_put_char(string, '*');
576 }
577 ds_put_char(string, ',');
578}
579
064af421 580static void
dbba996b 581print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
064af421
BP
582 uint32_t wild_bits, int verbosity)
583{
584 if (wild_bits >= 32 && verbosity < 2) {
585 return;
586 }
587 ds_put_cstr(string, leader);
588 if (wild_bits < 32) {
ed36537e 589 ds_put_format(string, IP_FMT, IP_ARGS(ip));
064af421
BP
590 if (wild_bits) {
591 ds_put_format(string, "/%d", 32 - wild_bits);
592 }
593 } else {
594 ds_put_char(string, '*');
595 }
596 ds_put_char(string, ',');
597}
598
4f2cad2c 599void
eec25dc1 600ofp10_match_print(struct ds *f, const struct ofp10_match *om, int verbosity)
064af421 601{
eec25dc1 602 char *s = ofp10_match_to_string(om, verbosity);
064af421
BP
603 ds_put_cstr(f, s);
604 free(s);
605}
606
607char *
eec25dc1 608ofp10_match_to_string(const struct ofp10_match *om, int verbosity)
064af421
BP
609{
610 struct ds f = DS_EMPTY_INITIALIZER;
611 uint32_t w = ntohl(om->wildcards);
612 bool skip_type = false;
613 bool skip_proto = false;
614
eec25dc1 615 if (!(w & OFPFW10_DL_TYPE)) {
064af421
BP
616 skip_type = true;
617 if (om->dl_type == htons(ETH_TYPE_IP)) {
eec25dc1 618 if (!(w & OFPFW10_NW_PROTO)) {
064af421 619 skip_proto = true;
6767a2cc 620 if (om->nw_proto == IPPROTO_ICMP) {
064af421 621 ds_put_cstr(&f, "icmp,");
6767a2cc 622 } else if (om->nw_proto == IPPROTO_TCP) {
064af421 623 ds_put_cstr(&f, "tcp,");
6767a2cc 624 } else if (om->nw_proto == IPPROTO_UDP) {
064af421 625 ds_put_cstr(&f, "udp,");
0d56eaf2
JS
626 } else if (om->nw_proto == IPPROTO_SCTP) {
627 ds_put_cstr(&f, "sctp,");
064af421
BP
628 } else {
629 ds_put_cstr(&f, "ip,");
630 skip_proto = false;
631 }
632 } else {
633 ds_put_cstr(&f, "ip,");
634 }
635 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
636 ds_put_cstr(&f, "arp,");
8087f5ff
MM
637 } else if (om->dl_type == htons(ETH_TYPE_RARP)){
638 ds_put_cstr(&f, "rarp,");
b02475c5
SH
639 } else if (om->dl_type == htons(ETH_TYPE_MPLS)) {
640 ds_put_cstr(&f, "mpls,");
641 } else if (om->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
642 ds_put_cstr(&f, "mplsm,");
064af421
BP
643 } else {
644 skip_type = false;
645 }
646 }
e1db42d6 647 print_wild_port(&f, "in_port=", w & OFPFW10_IN_PORT, verbosity,
4e022ec0 648 u16_to_ofp(ntohs(om->in_port)));
eec25dc1 649 print_wild(&f, "dl_vlan=", w & OFPFW10_DL_VLAN, verbosity,
6a1f89c8 650 "%d", ntohs(om->dl_vlan));
eec25dc1 651 print_wild(&f, "dl_vlan_pcp=", w & OFPFW10_DL_VLAN_PCP, verbosity,
959a2ecd 652 "%d", om->dl_vlan_pcp);
eec25dc1 653 print_wild(&f, "dl_src=", w & OFPFW10_DL_SRC, verbosity,
064af421 654 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
eec25dc1 655 print_wild(&f, "dl_dst=", w & OFPFW10_DL_DST, verbosity,
064af421
BP
656 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
657 if (!skip_type) {
eec25dc1 658 print_wild(&f, "dl_type=", w & OFPFW10_DL_TYPE, verbosity,
064af421
BP
659 "0x%04x", ntohs(om->dl_type));
660 }
661 print_ip_netmask(&f, "nw_src=", om->nw_src,
eec25dc1
BP
662 (w & OFPFW10_NW_SRC_MASK) >> OFPFW10_NW_SRC_SHIFT,
663 verbosity);
064af421 664 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
eec25dc1
BP
665 (w & OFPFW10_NW_DST_MASK) >> OFPFW10_NW_DST_SHIFT,
666 verbosity);
064af421 667 if (!skip_proto) {
8087f5ff
MM
668 if (om->dl_type == htons(ETH_TYPE_ARP) ||
669 om->dl_type == htons(ETH_TYPE_RARP)) {
eec25dc1 670 print_wild(&f, "arp_op=", w & OFPFW10_NW_PROTO, verbosity,
fb892732
JP
671 "%u", om->nw_proto);
672 } else {
eec25dc1 673 print_wild(&f, "nw_proto=", w & OFPFW10_NW_PROTO, verbosity,
fb892732
JP
674 "%u", om->nw_proto);
675 }
064af421 676 }
eec25dc1 677 print_wild(&f, "nw_tos=", w & OFPFW10_NW_TOS, verbosity,
1a960c80 678 "%u", om->nw_tos);
6767a2cc 679 if (om->nw_proto == IPPROTO_ICMP) {
eec25dc1 680 print_wild(&f, "icmp_type=", w & OFPFW10_ICMP_TYPE, verbosity,
3ee8a9f0 681 "%d", ntohs(om->tp_src));
eec25dc1 682 print_wild(&f, "icmp_code=", w & OFPFW10_ICMP_CODE, verbosity,
3ee8a9f0 683 "%d", ntohs(om->tp_dst));
064af421 684 } else {
eec25dc1 685 print_wild(&f, "tp_src=", w & OFPFW10_TP_SRC, verbosity,
064af421 686 "%d", ntohs(om->tp_src));
eec25dc1 687 print_wild(&f, "tp_dst=", w & OFPFW10_TP_DST, verbosity,
064af421
BP
688 "%d", ntohs(om->tp_dst));
689 }
dbf9e6d9 690 ds_chomp(&f, ',');
064af421
BP
691 return ds_cstr(&f);
692}
693
2e1ae200 694static void
0fb88c18 695ofp_print_flow_flags(struct ds *s, enum ofputil_flow_mod_flags flags)
2e1ae200 696{
0fb88c18 697 if (flags & OFPUTIL_FF_SEND_FLOW_REM) {
2e1ae200
JR
698 ds_put_cstr(s, "send_flow_rem ");
699 }
0fb88c18 700 if (flags & OFPUTIL_FF_CHECK_OVERLAP) {
2e1ae200
JR
701 ds_put_cstr(s, "check_overlap ");
702 }
0fb88c18 703 if (flags & OFPUTIL_FF_RESET_COUNTS) {
2e1ae200
JR
704 ds_put_cstr(s, "reset_counts ");
705 }
0fb88c18 706 if (flags & OFPUTIL_FF_NO_PKT_COUNTS) {
2e1ae200
JR
707 ds_put_cstr(s, "no_packet_counts ");
708 }
0fb88c18 709 if (flags & OFPUTIL_FF_NO_BYT_COUNTS) {
2e1ae200
JR
710 ds_put_cstr(s, "no_byte_counts ");
711 }
adcf00ba
AZ
712 if (flags & OFPUTIL_FF_HIDDEN_FIELDS) {
713 ds_put_cstr(s, "allow_hidden_fields ");
714 }
715 if (flags & OFPUTIL_FF_NO_READONLY) {
716 ds_put_cstr(s, "no_readonly_table ");
717 }
2e1ae200
JR
718}
719
064af421 720static void
982697a4 721ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh, int verbosity)
064af421 722{
a9a2da38 723 struct ofputil_flow_mod fm;
f25d0cf3 724 struct ofpbuf ofpacts;
f904747b 725 bool need_priority;
90bf1e07 726 enum ofperr error;
982697a4 727 enum ofpraw raw;
981c5fdd
SH
728 enum ofputil_protocol protocol;
729
730 protocol = ofputil_protocol_from_ofp_version(oh->version);
731 protocol = ofputil_protocol_set_tid(protocol, true);
064af421 732
f25d0cf3 733 ofpbuf_init(&ofpacts, 64);
7e9f8266
BP
734 error = ofputil_decode_flow_mod(&fm, oh, protocol, &ofpacts,
735 OFPP_MAX, 255);
7fa91113 736 if (error) {
f25d0cf3 737 ofpbuf_uninit(&ofpacts);
7fa91113
BP
738 ofp_print_error(s, error);
739 return;
3ff4f871
BP
740 }
741
7fa91113
BP
742 ds_put_char(s, ' ');
743 switch (fm.command) {
064af421 744 case OFPFC_ADD:
7fa91113 745 ds_put_cstr(s, "ADD");
064af421
BP
746 break;
747 case OFPFC_MODIFY:
7fa91113 748 ds_put_cstr(s, "MOD");
064af421
BP
749 break;
750 case OFPFC_MODIFY_STRICT:
7fa91113 751 ds_put_cstr(s, "MOD_STRICT");
064af421
BP
752 break;
753 case OFPFC_DELETE:
7fa91113 754 ds_put_cstr(s, "DEL");
064af421
BP
755 break;
756 case OFPFC_DELETE_STRICT:
7fa91113 757 ds_put_cstr(s, "DEL_STRICT");
064af421
BP
758 break;
759 default:
7fa91113
BP
760 ds_put_format(s, "cmd:%d", fm.command);
761 }
6c1491fb 762 if (fm.table_id != 0) {
e896c2d4 763 ds_put_format(s, " table:%d", fm.table_id);
6c1491fb 764 }
7fa91113
BP
765
766 ds_put_char(s, ' ');
982697a4
BP
767 ofpraw_decode(&raw, oh);
768 if (verbosity >= 3 && raw == OFPRAW_OFPT10_FLOW_MOD) {
35805806 769 const struct ofp10_flow_mod *ofm = ofpmsg_body(oh);
eec25dc1 770 ofp10_match_print(s, &ofm->match, verbosity);
f904747b
BP
771
772 /* ofp_print_match() doesn't print priority. */
773 need_priority = true;
982697a4
BP
774 } else if (verbosity >= 3 && raw == OFPRAW_NXT_FLOW_MOD) {
775 const struct nx_flow_mod *nfm = ofpmsg_body(oh);
7fa91113 776 const void *nxm = nfm + 1;
f904747b
BP
777 char *nxm_s;
778
779 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
7fa91113
BP
780 ds_put_cstr(s, nxm_s);
781 free(nxm_s);
f904747b
BP
782
783 /* nx_match_to_string() doesn't print priority. */
784 need_priority = true;
7fa91113 785 } else {
81a76618 786 match_format(&fm.match, s, fm.priority);
f904747b 787
81a76618 788 /* match_format() does print priority. */
f904747b 789 need_priority = false;
3ff4f871 790 }
7fa91113
BP
791
792 if (ds_last(s) != ' ') {
793 ds_put_char(s, ' ');
3ff4f871 794 }
b8266395 795 if (fm.new_cookie != htonll(0) && fm.new_cookie != OVS_BE64_MAX) {
623e1caf
JP
796 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.new_cookie));
797 }
798 if (fm.cookie_mask != htonll(0)) {
799 ds_put_format(s, "cookie:0x%"PRIx64"/0x%"PRIx64" ",
800 ntohll(fm.cookie), ntohll(fm.cookie_mask));
3ff4f871 801 }
7fa91113
BP
802 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
803 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
064af421 804 }
7fa91113
BP
805 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
806 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
3ff4f871 807 }
ca26eb44
RB
808 if (fm.importance != 0) {
809 ds_put_format(s, "importance:%"PRIu16" ", fm.importance);
810 }
81a76618
BP
811 if (fm.priority != OFP_DEFAULT_PRIORITY && need_priority) {
812 ds_put_format(s, "pri:%"PRIu16" ", fm.priority);
3ff4f871 813 }
7fa91113
BP
814 if (fm.buffer_id != UINT32_MAX) {
815 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
3ff4f871 816 }
7f05e7ab 817 if (fm.out_port != OFPP_ANY) {
de0f16bc
BP
818 ds_put_format(s, "out_port:");
819 ofputil_format_port(fm.out_port, s);
820 ds_put_char(s, ' ');
821 }
0fb88c18
BP
822
823 if (oh->version == OFP10_VERSION || oh->version == OFP11_VERSION) {
824 /* Don't print the reset_counts flag for OF1.0 and OF1.1 because those
825 * versions don't really have such a flag and printing one is likely to
826 * confuse people. */
827 fm.flags &= ~OFPUTIL_FF_RESET_COUNTS;
7fa91113 828 }
0fb88c18 829 ofp_print_flow_flags(s, fm.flags);
7fa91113 830
455ecd77 831 ds_put_cstr(s, "actions=");
f25d0cf3
BP
832 ofpacts_format(fm.ofpacts, fm.ofpacts_len, s);
833 ofpbuf_uninit(&ofpacts);
064af421
BP
834}
835
09862ec6
BP
836static void
837ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
838{
839 ds_put_format(string, "%u", sec);
b1634591
BP
840
841 /* If there are no fractional seconds, don't print any decimals.
842 *
843 * If the fractional seconds can be expressed exactly as milliseconds,
844 * print 3 decimals. Open vSwitch provides millisecond precision for most
845 * time measurements, so printing 3 decimals every time makes it easier to
846 * spot real changes in flow dumps that refresh themselves quickly.
847 *
848 * If the fractional seconds are more precise than milliseconds, print the
849 * number of decimals needed to express them exactly.
850 */
09862ec6 851 if (nsec > 0) {
b1634591
BP
852 unsigned int msec = nsec / 1000000;
853 if (msec * 1000000 == nsec) {
854 ds_put_format(string, ".%03u", msec);
855 } else {
856 ds_put_format(string, ".%09u", nsec);
857 while (string->string[string->length - 1] == '0') {
858 string->length--;
859 }
09862ec6
BP
860 }
861 }
862 ds_put_char(string, 's');
863}
864
8961699e
BP
865/* Returns a string form of 'reason'. The return value is either a statically
866 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
867 * 'bufsize' should be at least OFP_FLOW_REMOVED_REASON_BUFSIZE. */
868#define OFP_FLOW_REMOVED_REASON_BUFSIZE (INT_STRLEN(int) + 1)
80d5aefd 869static const char *
8961699e
BP
870ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason,
871 char *reasonbuf, size_t bufsize)
80d5aefd 872{
80d5aefd
BP
873 switch (reason) {
874 case OFPRR_IDLE_TIMEOUT:
875 return "idle";
876 case OFPRR_HARD_TIMEOUT:
877 return "hard";
878 case OFPRR_DELETE:
879 return "delete";
04f68eb2
SH
880 case OFPRR_GROUP_DELETE:
881 return "group_delete";
1d31ece9
BP
882 case OFPRR_EVICTION:
883 return "eviction";
638a19b0
JR
884 case OFPRR_METER_DELETE:
885 return "meter_delete";
80d5aefd 886 default:
8961699e
BP
887 snprintf(reasonbuf, bufsize, "%d", (int) reason);
888 return reasonbuf;
80d5aefd
BP
889 }
890}
891
064af421 892static void
9b045a0c 893ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
064af421 894{
8961699e 895 char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
9b045a0c 896 struct ofputil_flow_removed fr;
90bf1e07 897 enum ofperr error;
9b045a0c 898
b78f6b77 899 error = ofputil_decode_flow_removed(&fr, oh);
9b045a0c
BP
900 if (error) {
901 ofp_print_error(string, error);
902 return;
903 }
904
fbd76b2e 905 ds_put_char(string, ' ');
81a76618 906 match_format(&fr.match, string, fr.priority);
9b045a0c 907
80d5aefd 908 ds_put_format(string, " reason=%s",
8961699e
BP
909 ofp_flow_removed_reason_to_string(fr.reason, reasonbuf,
910 sizeof reasonbuf));
3ff4f871 911
95216219
SH
912 if (fr.table_id != 255) {
913 ds_put_format(string, " table_id=%"PRIu8, fr.table_id);
914 }
915
9b045a0c
BP
916 if (fr.cookie != htonll(0)) {
917 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
3ff4f871 918 }
09862ec6 919 ds_put_cstr(string, " duration");
9b045a0c 920 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
fa2bad0f
BP
921 ds_put_format(string, " idle%"PRIu16, fr.idle_timeout);
922 if (fr.hard_timeout) {
923 /* The hard timeout was only added in OF1.2, so only print it if it is
924 * actually in use to avoid gratuitous change to the formatting. */
925 ds_put_format(string, " hard%"PRIu16, fr.hard_timeout);
926 }
927 ds_put_format(string, " pkts%"PRIu64" bytes%"PRIu64"\n",
928 fr.packet_count, fr.byte_count);
064af421
BP
929}
930
931static void
9e1fd49b 932ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
064af421 933{
9e1fd49b
BP
934 struct ofputil_port_mod pm;
935 enum ofperr error;
936
18cc69d9 937 error = ofputil_decode_port_mod(oh, &pm, true);
9e1fd49b
BP
938 if (error) {
939 ofp_print_error(string, error);
940 return;
941 }
942
e1db42d6
JR
943 ds_put_cstr(string, "port: ");
944 ofputil_format_port(pm.port_no, string);
945 ds_put_format(string, ": addr:"ETH_ADDR_FMT"\n",
946 ETH_ADDR_ARGS(pm.hw_addr));
9e1fd49b 947
e1db42d6 948 ds_put_cstr(string, " config: ");
9e1fd49b
BP
949 ofp_print_port_config(string, pm.config);
950
e1db42d6 951 ds_put_cstr(string, " mask: ");
9e1fd49b
BP
952 ofp_print_port_config(string, pm.mask);
953
e1db42d6 954 ds_put_cstr(string, " advertise: ");
9e1fd49b
BP
955 if (pm.advertise) {
956 ofp_print_port_features(string, pm.advertise);
064af421 957 } else {
e1db42d6 958 ds_put_cstr(string, "UNCHANGED\n");
064af421
BP
959 }
960}
961
918f2b82 962static void
3c1bb396 963ofp_print_table_miss_config(struct ds *string, enum ofputil_table_miss miss)
918f2b82 964{
3c1bb396
BP
965 switch (miss) {
966 case OFPUTIL_TABLE_MISS_CONTROLLER:
918f2b82
AZ
967 ds_put_cstr(string, "controller\n");
968 break;
3c1bb396 969 case OFPUTIL_TABLE_MISS_CONTINUE:
918f2b82
AZ
970 ds_put_cstr(string, "continue\n");
971 break;
3c1bb396 972 case OFPUTIL_TABLE_MISS_DROP:
918f2b82
AZ
973 ds_put_cstr(string, "drop\n");
974 break;
3c1bb396 975 case OFPUTIL_TABLE_MISS_DEFAULT:
918f2b82 976 default:
3c1bb396 977 ds_put_format(string, "Unknown (%d)\n", miss);
918f2b82
AZ
978 break;
979 }
980}
981
982static void
983ofp_print_table_mod(struct ds *string, const struct ofp_header *oh)
984{
985 struct ofputil_table_mod pm;
986 enum ofperr error;
987
988 error = ofputil_decode_table_mod(oh, &pm);
989 if (error) {
990 ofp_print_error(string, error);
991 return;
992 }
993
994 if (pm.table_id == 0xff) {
995 ds_put_cstr(string, " table_id: ALL_TABLES");
996 } else {
997 ds_put_format(string, " table_id=%"PRIu8, pm.table_id);
998 }
999
3c1bb396
BP
1000 if (pm.miss_config != OFPUTIL_TABLE_MISS_DEFAULT) {
1001 ds_put_cstr(string, ", flow_miss_config=");
1002 ofp_print_table_miss_config(string, pm.miss_config);
1003 }
918f2b82
AZ
1004}
1005
e8f9a7bb
VG
1006static void
1007ofp_print_queue_get_config_request(struct ds *string,
1008 const struct ofp_header *oh)
1009{
1010 enum ofperr error;
1011 ofp_port_t port;
1012
1013 error = ofputil_decode_queue_get_config_request(oh, &port);
1014 if (error) {
1015 ofp_print_error(string, error);
1016 return;
1017 }
1018
1019 ds_put_cstr(string, " port=");
1020 ofputil_format_port(port, string);
1021}
1022
1023static void
1024print_queue_rate(struct ds *string, const char *name, unsigned int rate)
1025{
1026 if (rate <= 1000) {
1027 ds_put_format(string, " %s:%u.%u%%", name, rate / 10, rate % 10);
1028 } else if (rate < UINT16_MAX) {
1029 ds_put_format(string, " %s:(disabled)", name);
1030 }
1031}
1032
1033static void
1034ofp_print_queue_get_config_reply(struct ds *string,
1035 const struct ofp_header *oh)
1036{
1037 enum ofperr error;
1038 struct ofpbuf b;
1039 ofp_port_t port;
1040
1041 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1042 error = ofputil_decode_queue_get_config_reply(&b, &port);
1043 if (error) {
1044 ofp_print_error(string, error);
1045 return;
1046 }
1047
1048 ds_put_cstr(string, " port=");
1049 ofputil_format_port(port, string);
1050 ds_put_char(string, '\n');
1051
1052 for (;;) {
1053 struct ofputil_queue_config queue;
1054 int retval;
1055
1056 retval = ofputil_pull_queue_get_config_reply(&b, &queue);
1057 if (retval) {
1058 if (retval != EOF) {
1059 ofp_print_error(string, retval);
1060 }
1061 break;
1062 }
1063
1064 ds_put_format(string, "queue %"PRIu32":", queue.queue_id);
1065 print_queue_rate(string, "min_rate", queue.min_rate);
1066 print_queue_rate(string, "max_rate", queue.max_rate);
1067 ds_put_char(string, '\n');
1068 }
1069}
1070
638a19b0
JR
1071static void
1072ofp_print_meter_flags(struct ds *s, uint16_t flags)
1073{
1074 if (flags & OFPMF13_KBPS) {
1075 ds_put_cstr(s, "kbps ");
1076 }
1077 if (flags & OFPMF13_PKTPS) {
1078 ds_put_cstr(s, "pktps ");
1079 }
1080 if (flags & OFPMF13_BURST) {
1081 ds_put_cstr(s, "burst ");
1082 }
1083 if (flags & OFPMF13_STATS) {
1084 ds_put_cstr(s, "stats ");
1085 }
1086
1087 flags &= ~(OFPMF13_KBPS | OFPMF13_PKTPS | OFPMF13_BURST | OFPMF13_STATS);
1088 if (flags) {
1089 ds_put_format(s, "flags:0x%"PRIx16" ", flags);
1090 }
1091}
1092
1093static void
1094ofp_print_meter_band(struct ds *s, uint16_t flags,
1095 const struct ofputil_meter_band *mb)
1096{
1097 ds_put_cstr(s, "\ntype=");
1098 switch (mb->type) {
1099 case OFPMBT13_DROP:
1100 ds_put_cstr(s, "drop");
1101 break;
1102 case OFPMBT13_DSCP_REMARK:
1103 ds_put_cstr(s, "dscp_remark");
1104 break;
1105 default:
1106 ds_put_format(s, "%u", mb->type);
1107 }
1108
1109 ds_put_format(s, " rate=%"PRIu32, mb->rate);
1110
1111 if (flags & OFPMF13_BURST) {
1112 ds_put_format(s, " burst_size=%"PRIu32, mb->burst_size);
1113 }
1114 if (mb->type == OFPMBT13_DSCP_REMARK) {
1115 ds_put_format(s, " prec_level=%"PRIu8, mb->prec_level);
1116 }
1117}
1118
1119static void
1120ofp_print_meter_stats(struct ds *s, const struct ofputil_meter_stats *ms)
1121{
1122 uint16_t i;
1123
1124 ds_put_format(s, "meter:%"PRIu32" ", ms->meter_id);
1125 ds_put_format(s, "flow_count:%"PRIu32" ", ms->flow_count);
1126 ds_put_format(s, "packet_in_count:%"PRIu64" ", ms->packet_in_count);
1127 ds_put_format(s, "byte_in_count:%"PRIu64" ", ms->byte_in_count);
1128 ds_put_cstr(s, "duration:");
1129 ofp_print_duration(s, ms->duration_sec, ms->duration_nsec);
1130 ds_put_char(s, ' ');
1131
1132 ds_put_cstr(s, "bands:\n");
1133 for (i = 0; i < ms->n_bands; ++i) {
1134 ds_put_format(s, "%d: ", i);
1135 ds_put_format(s, "packet_count:%"PRIu64" ", ms->bands[i].packet_count);
1136 ds_put_format(s, "byte_count:%"PRIu64"\n", ms->bands[i].byte_count);
1137 }
1138}
1139
1140static void
1141ofp_print_meter_config(struct ds *s, const struct ofputil_meter_config *mc)
1142{
1143 uint16_t i;
1144
1145 ds_put_format(s, "meter=%"PRIu32" ", mc->meter_id);
1146
1147 ofp_print_meter_flags(s, mc->flags);
1148
1149 ds_put_cstr(s, "bands=");
1150 for (i = 0; i < mc->n_bands; ++i) {
1151 ofp_print_meter_band(s, mc->flags, &mc->bands[i]);
1152 }
1153 ds_put_char(s, '\n');
1154}
1155
1156static void
1157ofp_print_meter_mod(struct ds *s, const struct ofp_header *oh)
1158{
1159 struct ofputil_meter_mod mm;
1160 struct ofpbuf bands;
1161 enum ofperr error;
1162
1163 ofpbuf_init(&bands, 64);
1164 error = ofputil_decode_meter_mod(oh, &mm, &bands);
1165 if (error) {
1166 ofpbuf_uninit(&bands);
1167 ofp_print_error(s, error);
1168 return;
1169 }
1170
1171 switch (mm.command) {
1172 case OFPMC13_ADD:
1173 ds_put_cstr(s, " ADD ");
1174 break;
1175 case OFPMC13_MODIFY:
1176 ds_put_cstr(s, " MOD ");
1177 break;
1178 case OFPMC13_DELETE:
1179 ds_put_cstr(s, " DEL ");
1180 break;
1181 default:
1182 ds_put_format(s, " cmd:%d ", mm.command);
1183 }
1184
1185 ofp_print_meter_config(s, &mm.meter);
1186 ofpbuf_uninit(&bands);
1187}
1188
1189static void
1190ofp_print_meter_stats_request(struct ds *s, const struct ofp_header *oh)
1191{
1192 uint32_t meter_id;
1193
1194 ofputil_decode_meter_request(oh, &meter_id);
1195
1196 ds_put_format(s, " meter=%"PRIu32, meter_id);
1197}
1198
1199static const char *
1200ofputil_meter_capabilities_to_name(uint32_t bit)
1201{
1202 enum ofp13_meter_flags flag = bit;
1203
1204 switch (flag) {
1205 case OFPMF13_KBPS: return "kbps";
1206 case OFPMF13_PKTPS: return "pktps";
1207 case OFPMF13_BURST: return "burst";
1208 case OFPMF13_STATS: return "stats";
1209 }
1210
1211 return NULL;
1212}
1213
1214static const char *
1215ofputil_meter_band_types_to_name(uint32_t bit)
1216{
638a19b0 1217 switch (bit) {
6e055a6c
BP
1218 case 1 << OFPMBT13_DROP: return "drop";
1219 case 1 << OFPMBT13_DSCP_REMARK: return "dscp_remark";
638a19b0
JR
1220 }
1221
1222 return NULL;
1223}
1224
1225static void
1226ofp_print_meter_features_reply(struct ds *s, const struct ofp_header *oh)
1227{
1228 struct ofputil_meter_features mf;
1229
1230 ofputil_decode_meter_features(oh, &mf);
1231
1232 ds_put_format(s, "\nmax_meter:%"PRIu32, mf.max_meters);
1233 ds_put_format(s, " max_bands:%"PRIu8, mf.max_bands);
1234 ds_put_format(s, " max_color:%"PRIu8"\n", mf.max_color);
1235
1236 ds_put_cstr(s, "band_types: ");
1237 ofp_print_bit_names(s, mf.band_types,
1238 ofputil_meter_band_types_to_name, ' ');
1239 ds_put_char(s, '\n');
1240
1241 ds_put_cstr(s, "capabilities: ");
1242 ofp_print_bit_names(s, mf.capabilities,
1243 ofputil_meter_capabilities_to_name, ' ');
1244 ds_put_char(s, '\n');
1245}
1246
1247static void
1248ofp_print_meter_config_reply(struct ds *s, const struct ofp_header *oh)
1249{
1250 struct ofpbuf bands;
1251 struct ofpbuf b;
1252
1253 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1254 ofpbuf_init(&bands, 64);
1255 for (;;) {
1256 struct ofputil_meter_config mc;
1257 int retval;
1258
1259 retval = ofputil_decode_meter_config(&b, &mc, &bands);
1260 if (retval) {
1261 if (retval != EOF) {
1262 ofp_print_error(s, retval);
1263 }
1264 break;
1265 }
1266 ds_put_char(s, '\n');
1267 ofp_print_meter_config(s, &mc);
1268 }
1269 ofpbuf_uninit(&bands);
1270}
1271
1272static void
1273ofp_print_meter_stats_reply(struct ds *s, const struct ofp_header *oh)
1274{
1275 struct ofpbuf bands;
1276 struct ofpbuf b;
1277
1278 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1279 ofpbuf_init(&bands, 64);
1280 for (;;) {
1281 struct ofputil_meter_stats ms;
1282 int retval;
1283
1284 retval = ofputil_decode_meter_stats(&b, &ms, &bands);
1285 if (retval) {
1286 if (retval != EOF) {
1287 ofp_print_error(s, retval);
1288 }
1289 break;
1290 }
1291 ds_put_char(s, '\n');
1292 ofp_print_meter_stats(s, &ms);
1293 }
1294 ofpbuf_uninit(&bands);
1295}
1296
7fa91113 1297static void
90bf1e07 1298ofp_print_error(struct ds *string, enum ofperr error)
7fa91113 1299{
7fa91113
BP
1300 if (string->length) {
1301 ds_put_char(string, ' ');
1302 }
90bf1e07 1303 ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
f74be05a
JP
1304}
1305
de6c85b0
SH
1306static void
1307ofp_print_hello(struct ds *string, const struct ofp_header *oh)
1308{
1309 uint32_t allowed_versions;
1310 bool ok;
1311
1312 ok = ofputil_decode_hello(oh, &allowed_versions);
1313
1314 ds_put_cstr(string, "\n version bitmap: ");
1315 ofputil_format_version_bitmap(string, allowed_versions);
1316
1317 if (!ok) {
1318 ds_put_cstr(string, "\n unknown data in hello:\n");
1319 ds_put_hex_dump(string, oh, ntohs(oh->length), 0, true);
1320 }
1321}
1322
064af421 1323static void
982697a4 1324ofp_print_error_msg(struct ds *string, const struct ofp_header *oh)
064af421 1325{
982697a4
BP
1326 size_t len = ntohs(oh->length);
1327 struct ofpbuf payload;
90bf1e07 1328 enum ofperr error;
064af421
BP
1329 char *s;
1330
982697a4 1331 error = ofperr_decode_msg(oh, &payload);
90bf1e07
BP
1332 if (!error) {
1333 ds_put_cstr(string, "***decode error***");
982697a4 1334 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
dc4762ed 1335 return;
f74be05a
JP
1336 }
1337
90bf1e07 1338 ds_put_format(string, " %s\n", ofperr_get_name(error));
064af421 1339
90bf1e07 1340 if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
1f317cb5 1341 ds_put_printable(string, ofpbuf_data(&payload), ofpbuf_size(&payload));
90bf1e07 1342 } else {
1f317cb5 1343 s = ofp_to_string(ofpbuf_data(&payload), ofpbuf_size(&payload), 1);
064af421
BP
1344 ds_put_cstr(string, s);
1345 free(s);
064af421 1346 }
dea241f1 1347 ofpbuf_uninit(&payload);
064af421
BP
1348}
1349
064af421 1350static void
982697a4 1351ofp_print_port_status(struct ds *string, const struct ofp_header *oh)
064af421 1352{
9e1fd49b
BP
1353 struct ofputil_port_status ps;
1354 enum ofperr error;
1355
982697a4 1356 error = ofputil_decode_port_status(oh, &ps);
9e1fd49b
BP
1357 if (error) {
1358 ofp_print_error(string, error);
1359 return;
1360 }
1361
1362 if (ps.reason == OFPPR_ADD) {
064af421 1363 ds_put_format(string, " ADD:");
9e1fd49b 1364 } else if (ps.reason == OFPPR_DELETE) {
064af421 1365 ds_put_format(string, " DEL:");
9e1fd49b 1366 } else if (ps.reason == OFPPR_MODIFY) {
064af421
BP
1367 ds_put_format(string, " MOD:");
1368 }
1369
9e1fd49b 1370 ofp_print_phy_port(string, &ps.desc);
064af421
BP
1371}
1372
1373static void
982697a4 1374ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
064af421 1375{
982697a4
BP
1376 const struct ofp_desc_stats *ods = ofpmsg_body(oh);
1377
fbd76b2e 1378 ds_put_char(string, '\n');
d295e8e9 1379 ds_put_format(string, "Manufacturer: %.*s\n",
dd70b475
JP
1380 (int) sizeof ods->mfr_desc, ods->mfr_desc);
1381 ds_put_format(string, "Hardware: %.*s\n",
1382 (int) sizeof ods->hw_desc, ods->hw_desc);
1383 ds_put_format(string, "Software: %.*s\n",
1384 (int) sizeof ods->sw_desc, ods->sw_desc);
1385 ds_put_format(string, "Serial Num: %.*s\n",
1386 (int) sizeof ods->serial_num, ods->serial_num);
1387 ds_put_format(string, "DP Description: %.*s\n",
1388 (int) sizeof ods->dp_desc, ods->dp_desc);
064af421
BP
1389}
1390
1391static void
982697a4 1392ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
064af421 1393{
81d1ea94 1394 struct ofputil_flow_stats_request fsr;
90bf1e07 1395 enum ofperr error;
064af421 1396
982697a4 1397 error = ofputil_decode_flow_stats_request(&fsr, oh);
2af0c0ef
BP
1398 if (error) {
1399 ofp_print_error(string, error);
1400 return;
1401 }
1402
1403 if (fsr.table_id != 0xff) {
e896c2d4 1404 ds_put_format(string, " table=%"PRIu8, fsr.table_id);
064af421
BP
1405 }
1406
7f05e7ab 1407 if (fsr.out_port != OFPP_ANY) {
2af0c0ef 1408 ds_put_cstr(string, " out_port=");
39dc9082 1409 ofputil_format_port(fsr.out_port, string);
2af0c0ef
BP
1410 }
1411
1412 ds_put_char(string, ' ');
81a76618 1413 match_format(&fsr.match, string, OFP_DEFAULT_PRIORITY);
064af421
BP
1414}
1415
bdcc5925
BP
1416void
1417ofp_print_flow_stats(struct ds *string, struct ofputil_flow_stats *fs)
1418{
1419 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1420 ntohll(fs->cookie));
1421
1422 ofp_print_duration(string, fs->duration_sec, fs->duration_nsec);
1423 ds_put_format(string, ", table=%"PRIu8", ", fs->table_id);
1424 ds_put_format(string, "n_packets=%"PRIu64", ", fs->packet_count);
1425 ds_put_format(string, "n_bytes=%"PRIu64", ", fs->byte_count);
1426 if (fs->idle_timeout != OFP_FLOW_PERMANENT) {
1427 ds_put_format(string, "idle_timeout=%"PRIu16", ", fs->idle_timeout);
1428 }
1429 if (fs->hard_timeout != OFP_FLOW_PERMANENT) {
1430 ds_put_format(string, "hard_timeout=%"PRIu16", ", fs->hard_timeout);
1431 }
2e1ae200
JR
1432 if (fs->flags) {
1433 ofp_print_flow_flags(string, fs->flags);
1434 }
ca26eb44
RB
1435 if (fs->importance != 0) {
1436 ds_put_format(string, "importance=%"PRIu16", ", fs->importance);
1437 }
bdcc5925
BP
1438 if (fs->idle_age >= 0) {
1439 ds_put_format(string, "idle_age=%d, ", fs->idle_age);
1440 }
1441 if (fs->hard_age >= 0 && fs->hard_age != fs->duration_sec) {
1442 ds_put_format(string, "hard_age=%d, ", fs->hard_age);
1443 }
1444
81a76618 1445 match_format(&fs->match, string, fs->priority);
bdcc5925
BP
1446 if (string->string[string->length - 1] != ' ') {
1447 ds_put_char(string, ' ');
1448 }
1449
455ecd77 1450 ds_put_cstr(string, "actions=");
bdcc5925
BP
1451 ofpacts_format(fs->ofpacts, fs->ofpacts_len, string);
1452}
1453
064af421 1454static void
4ffd1b43 1455ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
064af421 1456{
f25d0cf3 1457 struct ofpbuf ofpacts;
4ffd1b43 1458 struct ofpbuf b;
064af421 1459
4ffd1b43 1460 ofpbuf_use_const(&b, oh, ntohs(oh->length));
f25d0cf3 1461 ofpbuf_init(&ofpacts, 64);
4ffd1b43
BP
1462 for (;;) {
1463 struct ofputil_flow_stats fs;
1464 int retval;
fab8fadb 1465
f25d0cf3 1466 retval = ofputil_decode_flow_stats_reply(&fs, &b, true, &ofpacts);
4ffd1b43
BP
1467 if (retval) {
1468 if (retval != EOF) {
1469 ds_put_cstr(string, " ***parse error***");
064af421
BP
1470 }
1471 break;
1472 }
8961de6a 1473 ds_put_char(string, '\n');
bdcc5925
BP
1474 ofp_print_flow_stats(string, &fs);
1475 }
cb80d803 1476 ofpbuf_uninit(&ofpacts);
c6430da5
BP
1477}
1478
064af421 1479static void
982697a4 1480ofp_print_aggregate_stats_reply(struct ds *string, const struct ofp_header *oh)
064af421 1481{
982697a4
BP
1482 struct ofputil_aggregate_stats as;
1483 enum ofperr error;
064af421 1484
982697a4
BP
1485 error = ofputil_decode_aggregate_stats_reply(&as, oh);
1486 if (error) {
1487 ofp_print_error(string, error);
1488 return;
1489 }
1490
1491 ds_put_format(string, " packet_count=%"PRIu64, as.packet_count);
1492 ds_put_format(string, " byte_count=%"PRIu64, as.byte_count);
1493 ds_put_format(string, " flow_count=%"PRIu32, as.flow_count);
a2ad9ecd
BP
1494}
1495
f8e4867e
SH
1496static void
1497print_port_stat(struct ds *string, const char *leader, uint64_t stat, int more)
064af421
BP
1498{
1499 ds_put_cstr(string, leader);
c4617b3c 1500 if (stat != UINT64_MAX) {
064af421
BP
1501 ds_put_format(string, "%"PRIu64, stat);
1502 } else {
1503 ds_put_char(string, '?');
1504 }
1505 if (more) {
1506 ds_put_cstr(string, ", ");
1507 } else {
1508 ds_put_cstr(string, "\n");
1509 }
1510}
1511
abaad8cf 1512static void
982697a4 1513ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
abaad8cf 1514{
4e022ec0 1515 ofp_port_t ofp10_port;
f8e4867e
SH
1516 enum ofperr error;
1517
1518 error = ofputil_decode_port_stats_request(oh, &ofp10_port);
1519 if (error) {
1520 ofp_print_error(string, error);
1521 return;
1522 }
1523
e1db42d6
JR
1524 ds_put_cstr(string, " port_no=");
1525 ofputil_format_port(ofp10_port, string);
abaad8cf
JP
1526}
1527
064af421 1528static void
d1e2cf21
BP
1529ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1530 int verbosity)
064af421 1531{
982697a4 1532 struct ofpbuf b;
982697a4 1533
34582733 1534 ds_put_format(string, " %"PRIuSIZE" ports\n", ofputil_count_port_stats(oh));
064af421
BP
1535 if (verbosity < 1) {
1536 return;
1537 }
1538
f8e4867e 1539 ofpbuf_use_const(&b, oh, ntohs(oh->length));
982697a4 1540 for (;;) {
f8e4867e
SH
1541 struct ofputil_port_stats ps;
1542 int retval;
1543
1544 retval = ofputil_decode_port_stats(&ps, &b);
1545 if (retval) {
1546 if (retval != EOF) {
1547 ds_put_cstr(string, " ***parse error***");
1548 }
982697a4
BP
1549 return;
1550 }
1551
e1db42d6 1552 ds_put_cstr(string, " port ");
4e022ec0 1553 if (ofp_to_u16(ps.port_no) < 10) {
e1db42d6
JR
1554 ds_put_char(string, ' ');
1555 }
1556 ofputil_format_port(ps.port_no, string);
064af421 1557
f8e4867e
SH
1558 ds_put_cstr(string, ": rx ");
1559 print_port_stat(string, "pkts=", ps.stats.rx_packets, 1);
1560 print_port_stat(string, "bytes=", ps.stats.rx_bytes, 1);
1561 print_port_stat(string, "drop=", ps.stats.rx_dropped, 1);
1562 print_port_stat(string, "errs=", ps.stats.rx_errors, 1);
1563 print_port_stat(string, "frame=", ps.stats.rx_frame_errors, 1);
1564 print_port_stat(string, "over=", ps.stats.rx_over_errors, 1);
1565 print_port_stat(string, "crc=", ps.stats.rx_crc_errors, 0);
064af421
BP
1566
1567 ds_put_cstr(string, " tx ");
f8e4867e
SH
1568 print_port_stat(string, "pkts=", ps.stats.tx_packets, 1);
1569 print_port_stat(string, "bytes=", ps.stats.tx_bytes, 1);
1570 print_port_stat(string, "drop=", ps.stats.tx_dropped, 1);
1571 print_port_stat(string, "errs=", ps.stats.tx_errors, 1);
1572 print_port_stat(string, "coll=", ps.stats.collisions, 0);
65e0be10
BP
1573
1574 if (ps.duration_sec != UINT32_MAX) {
1575 ds_put_cstr(string, " duration=");
1576 ofp_print_duration(string, ps.duration_sec, ps.duration_nsec);
1577 ds_put_char(string, '\n');
1578 }
064af421
BP
1579 }
1580}
1581
1582static void
3c1bb396 1583ofp_print_table_stats_reply(struct ds *string, const struct ofp_header *oh)
26df8b3e 1584{
26df8b3e 1585 struct ofpbuf b;
26df8b3e
SH
1586
1587 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1588 ofpraw_pull_assert(&b);
1589
26df8b3e 1590 for (;;) {
3c1bb396
BP
1591 struct ofputil_table_features features;
1592 struct ofputil_table_stats stats;
1593 int retval;
982697a4 1594
3c1bb396
BP
1595 retval = ofputil_decode_table_stats_reply(&b, &stats, &features);
1596 if (retval) {
1597 if (retval != EOF) {
1598 ofp_print_error(string, retval);
1599 }
982697a4
BP
1600 return;
1601 }
1602
3c1bb396 1603 ofp_print_table_features(string, &features, &stats);
26df8b3e
SH
1604 }
1605}
1606
d2805da2
BP
1607static void
1608ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1609{
1610 if (queue_id == OFPQ_ALL) {
1611 ds_put_cstr(string, "ALL");
1612 } else {
1613 ds_put_format(string, "%"PRIu32, queue_id);
1614 }
1615}
1616
1617static void
982697a4 1618ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
d2805da2 1619{
64626975
SH
1620 struct ofputil_queue_stats_request oqsr;
1621 enum ofperr error;
1622
1623 error = ofputil_decode_queue_stats_request(oh, &oqsr);
1624 if (error) {
1625 ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1626 return;
1627 }
982697a4 1628
d2805da2 1629 ds_put_cstr(string, "port=");
64626975 1630 ofputil_format_port(oqsr.port_no, string);
d2805da2
BP
1631
1632 ds_put_cstr(string, " queue=");
64626975 1633 ofp_print_queue_name(string, oqsr.queue_id);
f8e4867e
SH
1634}
1635
d2805da2 1636static void
d1e2cf21
BP
1637ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1638 int verbosity)
d2805da2 1639{
982697a4 1640 struct ofpbuf b;
982697a4 1641
34582733 1642 ds_put_format(string, " %"PRIuSIZE" queues\n", ofputil_count_queue_stats(oh));
d2805da2
BP
1643 if (verbosity < 1) {
1644 return;
1645 }
1646
64626975 1647 ofpbuf_use_const(&b, oh, ntohs(oh->length));
982697a4 1648 for (;;) {
64626975
SH
1649 struct ofputil_queue_stats qs;
1650 int retval;
1651
1652 retval = ofputil_decode_queue_stats(&qs, &b);
1653 if (retval) {
1654 if (retval != EOF) {
1655 ds_put_cstr(string, " ***parse error***");
1656 }
982697a4
BP
1657 return;
1658 }
1659
d2805da2 1660 ds_put_cstr(string, " port ");
64626975 1661 ofputil_format_port(qs.port_no, string);
d2805da2 1662 ds_put_cstr(string, " queue ");
64626975 1663 ofp_print_queue_name(string, qs.queue_id);
d2805da2
BP
1664 ds_put_cstr(string, ": ");
1665
6dc34a0d
BP
1666 print_port_stat(string, "bytes=", qs.tx_bytes, 1);
1667 print_port_stat(string, "pkts=", qs.tx_packets, 1);
1668 print_port_stat(string, "errors=", qs.tx_errors, 1);
1669
1670 ds_put_cstr(string, "duration=");
1671 if (qs.duration_sec != UINT32_MAX) {
1672 ofp_print_duration(string, qs.duration_sec, qs.duration_nsec);
1673 } else {
1674 ds_put_char(string, '?');
1675 }
1676 ds_put_char(string, '\n');
d2805da2
BP
1677 }
1678}
1679
70ae4f93
BP
1680static void
1681ofp_print_ofpst_port_desc_request(struct ds *string,
1682 const struct ofp_header *oh)
1683{
1684 enum ofperr error;
1685 ofp_port_t port;
1686
1687 error = ofputil_decode_port_desc_stats_request(oh, &port);
1688 if (error) {
1689 ofp_print_error(string, error);
1690 return;
1691 }
1692
1693 ds_put_cstr(string, " port=");
1694 ofputil_format_port(port, string);
1695}
1696
2be393ed
JP
1697static void
1698ofp_print_ofpst_port_desc_reply(struct ds *string,
1699 const struct ofp_header *oh)
1700{
1701 struct ofpbuf b;
1702
1703 ofpbuf_use_const(&b, oh, ntohs(oh->length));
982697a4 1704 ofpraw_pull_assert(&b);
2be393ed
JP
1705 ds_put_char(string, '\n');
1706 ofp_print_phy_ports(string, oh->version, &b);
1707}
1708
064af421 1709static void
5d40fc57 1710ofp_print_stats(struct ds *string, const struct ofp_header *oh)
064af421 1711{
982697a4 1712 uint16_t flags = ofpmp_flags(oh);
d1e2cf21 1713
982697a4 1714 if (flags) {
d1e2cf21 1715 ds_put_cstr(string, " flags=");
5d40fc57
SH
1716 if ((!ofpmsg_is_stat_request(oh) || oh->version >= OFP13_VERSION)
1717 && (flags & OFPSF_REPLY_MORE)) {
064af421
BP
1718 ds_put_cstr(string, "[more]");
1719 flags &= ~OFPSF_REPLY_MORE;
1720 }
1721 if (flags) {
d1e2cf21
BP
1722 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1723 flags);
064af421
BP
1724 }
1725 }
064af421
BP
1726}
1727
1728static void
d1e2cf21 1729ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
064af421 1730{
d1e2cf21 1731 size_t len = ntohs(oh->length);
064af421 1732
34582733 1733 ds_put_format(string, " %"PRIuSIZE" bytes of payload\n", len - sizeof *oh);
064af421 1734 if (verbosity > 1) {
d1e2cf21 1735 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
064af421
BP
1736 }
1737}
1738
61fe3a7b 1739static void
00467f73
AC
1740ofp_print_role_generic(struct ds *string, enum ofp12_controller_role role,
1741 uint64_t generation_id)
61fe3a7b 1742{
61fe3a7b 1743 ds_put_cstr(string, " role=");
6ea4776b 1744
00467f73 1745 switch (role) {
f4f1ea7e
BP
1746 case OFPCR12_ROLE_NOCHANGE:
1747 ds_put_cstr(string, "nochange");
1748 break;
1749 case OFPCR12_ROLE_EQUAL:
6ea4776b
JR
1750 ds_put_cstr(string, "equal"); /* OF 1.2 wording */
1751 break;
f4f1ea7e 1752 case OFPCR12_ROLE_MASTER:
61fe3a7b 1753 ds_put_cstr(string, "master");
6ea4776b 1754 break;
f4f1ea7e 1755 case OFPCR12_ROLE_SLAVE:
61fe3a7b 1756 ds_put_cstr(string, "slave");
6ea4776b
JR
1757 break;
1758 default:
428b2edd 1759 OVS_NOT_REACHED();
6ea4776b
JR
1760 }
1761
00467f73
AC
1762 if (generation_id != UINT64_MAX) {
1763 ds_put_format(string, " generation_id=%"PRIu64, generation_id);
1764 }
1765}
1766
1767static void
1768ofp_print_role_message(struct ds *string, const struct ofp_header *oh)
1769{
1770 struct ofputil_role_request rr;
1771 enum ofperr error;
1772
1773 error = ofputil_decode_role_message(oh, &rr);
1774 if (error) {
1775 ofp_print_error(string, error);
1776 return;
1777 }
1778
1779 ofp_print_role_generic(string, rr.role, rr.have_generation_id ? rr.generation_id : UINT64_MAX);
1780}
1781
1782static void
1783ofp_print_role_status_message(struct ds *string, const struct ofp_header *oh)
1784{
1785 struct ofputil_role_status rs;
1786 enum ofperr error;
1787
1788 error = ofputil_decode_role_status(oh, &rs);
1789 if (error) {
1790 ofp_print_error(string, error);
1791 return;
1792 }
1793
1794 ofp_print_role_generic(string, rs.role, rs.generation_id);
1795
1796 ds_put_cstr(string, " reason=");
1797
1798 switch (rs.reason) {
1799 case OFPCRR_MASTER_REQUEST:
1800 ds_put_cstr(string, "master_request");
1801 break;
1802 case OFPCRR_CONFIG:
1803 ds_put_cstr(string, "configuration_changed");
1804 break;
1805 case OFPCRR_EXPERIMENTER:
1806 ds_put_cstr(string, "experimenter_data_changed");
1807 break;
1808 default:
428b2edd 1809 OVS_NOT_REACHED();
61fe3a7b
BP
1810 }
1811}
1812
6c1491fb
BP
1813static void
1814ofp_print_nxt_flow_mod_table_id(struct ds *string,
73dbf4ab 1815 const struct nx_flow_mod_table_id *nfmti)
6c1491fb
BP
1816{
1817 ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1818}
1819
7fa91113
BP
1820static void
1821ofp_print_nxt_set_flow_format(struct ds *string,
73dbf4ab 1822 const struct nx_set_flow_format *nsff)
7fa91113
BP
1823{
1824 uint32_t format = ntohl(nsff->format);
1825
1826 ds_put_cstr(string, " format=");
27527aa0
BP
1827 if (ofputil_nx_flow_format_is_valid(format)) {
1828 ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
7fa91113
BP
1829 } else {
1830 ds_put_format(string, "%"PRIu32, format);
1831 }
1832}
1833
54834960
EJ
1834static void
1835ofp_print_nxt_set_packet_in_format(struct ds *string,
73dbf4ab 1836 const struct nx_set_packet_in_format *nspf)
54834960
EJ
1837{
1838 uint32_t format = ntohl(nspf->format);
1839
1840 ds_put_cstr(string, " format=");
1841 if (ofputil_packet_in_format_is_valid(format)) {
1842 ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1843 } else {
1844 ds_put_format(string, "%"PRIu32, format);
1845 }
1846}
1847
8961699e
BP
1848/* Returns a string form of 'reason'. The return value is either a statically
1849 * allocated constant string or the 'bufsize'-byte buffer 'reasonbuf'.
1850 * 'bufsize' should be at least OFP_PORT_REASON_BUFSIZE. */
1851#define OFP_PORT_REASON_BUFSIZE (INT_STRLEN(int) + 1)
80d5aefd 1852static const char *
8961699e
BP
1853ofp_port_reason_to_string(enum ofp_port_reason reason,
1854 char *reasonbuf, size_t bufsize)
80d5aefd 1855{
80d5aefd
BP
1856 switch (reason) {
1857 case OFPPR_ADD:
1858 return "add";
1859
1860 case OFPPR_DELETE:
1861 return "delete";
1862
1863 case OFPPR_MODIFY:
1864 return "modify";
1865
1866 default:
8961699e
BP
1867 snprintf(reasonbuf, bufsize, "%d", (int) reason);
1868 return reasonbuf;
80d5aefd
BP
1869 }
1870}
1871
1872static void
1873ofp_print_nxt_set_async_config(struct ds *string,
1874 const struct nx_async_config *nac)
1875{
1876 int i;
1877
1878 for (i = 0; i < 2; i++) {
1879 int j;
1880
1881 ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1882
1883 ds_put_cstr(string, " PACKET_IN:");
1884 for (j = 0; j < 32; j++) {
1885 if (nac->packet_in_mask[i] & htonl(1u << j)) {
5014a89d
BP
1886 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
1887 const char *reason;
1888
1889 reason = ofputil_packet_in_reason_to_string(j, reasonbuf,
1890 sizeof reasonbuf);
1891 ds_put_format(string, " %s", reason);
80d5aefd
BP
1892 }
1893 }
1894 if (!nac->packet_in_mask[i]) {
1895 ds_put_cstr(string, " (off)");
1896 }
1897 ds_put_char(string, '\n');
1898
1899 ds_put_cstr(string, " PORT_STATUS:");
1900 for (j = 0; j < 32; j++) {
1901 if (nac->port_status_mask[i] & htonl(1u << j)) {
8961699e
BP
1902 char reasonbuf[OFP_PORT_REASON_BUFSIZE];
1903 const char *reason;
1904
1905 reason = ofp_port_reason_to_string(j, reasonbuf,
1906 sizeof reasonbuf);
1907 ds_put_format(string, " %s", reason);
80d5aefd
BP
1908 }
1909 }
1910 if (!nac->port_status_mask[i]) {
1911 ds_put_cstr(string, " (off)");
1912 }
1913 ds_put_char(string, '\n');
1914
1915 ds_put_cstr(string, " FLOW_REMOVED:");
1916 for (j = 0; j < 32; j++) {
1917 if (nac->flow_removed_mask[i] & htonl(1u << j)) {
8961699e
BP
1918 char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
1919 const char *reason;
1920
1921 reason = ofp_flow_removed_reason_to_string(j, reasonbuf,
1922 sizeof reasonbuf);
1923 ds_put_format(string, " %s", reason);
80d5aefd
BP
1924 }
1925 }
1926 if (!nac->flow_removed_mask[i]) {
1927 ds_put_cstr(string, " (off)");
1928 }
1929 ds_put_char(string, '\n');
1930 }
1931}
1932
a7349929
BP
1933static void
1934ofp_print_nxt_set_controller_id(struct ds *string,
1935 const struct nx_controller_id *nci)
1936{
1937 ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
1938}
1939
2b07c8b1
BP
1940static void
1941ofp_print_nxt_flow_monitor_cancel(struct ds *string,
1942 const struct ofp_header *oh)
1943{
1944 ds_put_format(string, " id=%"PRIu32,
1945 ofputil_decode_flow_monitor_cancel(oh));
1946}
1947
1948static const char *
1949nx_flow_monitor_flags_to_name(uint32_t bit)
1950{
1951 enum nx_flow_monitor_flags fmf = bit;
1952
1953 switch (fmf) {
1954 case NXFMF_INITIAL: return "initial";
1955 case NXFMF_ADD: return "add";
1956 case NXFMF_DELETE: return "delete";
1957 case NXFMF_MODIFY: return "modify";
1958 case NXFMF_ACTIONS: return "actions";
1959 case NXFMF_OWN: return "own";
1960 }
1961
1962 return NULL;
1963}
1964
1965static void
1966ofp_print_nxst_flow_monitor_request(struct ds *string,
1967 const struct ofp_header *oh)
1968{
1969 struct ofpbuf b;
1970
1971 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1972 for (;;) {
1973 struct ofputil_flow_monitor_request request;
1974 int retval;
1975
1976 retval = ofputil_decode_flow_monitor_request(&request, &b);
1977 if (retval) {
1978 if (retval != EOF) {
1979 ofp_print_error(string, retval);
1980 }
1981 return;
1982 }
1983
1984 ds_put_format(string, "\n id=%"PRIu32" flags=", request.id);
1985 ofp_print_bit_names(string, request.flags,
1986 nx_flow_monitor_flags_to_name, ',');
1987
1988 if (request.out_port != OFPP_NONE) {
1989 ds_put_cstr(string, " out_port=");
1990 ofputil_format_port(request.out_port, string);
1991 }
1992
1993 if (request.table_id != 0xff) {
1994 ds_put_format(string, " table=%"PRIu8, request.table_id);
1995 }
1996
1997 ds_put_char(string, ' ');
81a76618 1998 match_format(&request.match, string, OFP_DEFAULT_PRIORITY);
2b07c8b1
BP
1999 ds_chomp(string, ' ');
2000 }
2001}
2002
2003static void
2004ofp_print_nxst_flow_monitor_reply(struct ds *string,
2005 const struct ofp_header *oh)
2006{
2007 uint64_t ofpacts_stub[1024 / 8];
2008 struct ofpbuf ofpacts;
2009 struct ofpbuf b;
2010
2011 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2012 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
2013 for (;;) {
8961699e 2014 char reasonbuf[OFP_FLOW_REMOVED_REASON_BUFSIZE];
2b07c8b1 2015 struct ofputil_flow_update update;
81a76618 2016 struct match match;
2b07c8b1
BP
2017 int retval;
2018
2019 update.match = &match;
2020 retval = ofputil_decode_flow_update(&update, &b, &ofpacts);
2021 if (retval) {
2022 if (retval != EOF) {
2023 ofp_print_error(string, retval);
2024 }
2025 ofpbuf_uninit(&ofpacts);
2026 return;
2027 }
2028
2029 ds_put_cstr(string, "\n event=");
2030 switch (update.event) {
2031 case NXFME_ADDED:
2032 ds_put_cstr(string, "ADDED");
2033 break;
2034
2035 case NXFME_DELETED:
2036 ds_put_format(string, "DELETED reason=%s",
8961699e
BP
2037 ofp_flow_removed_reason_to_string(update.reason,
2038 reasonbuf,
2039 sizeof reasonbuf));
2b07c8b1
BP
2040 break;
2041
2042 case NXFME_MODIFIED:
2043 ds_put_cstr(string, "MODIFIED");
2044 break;
2045
2046 case NXFME_ABBREV:
2047 ds_put_format(string, "ABBREV xid=0x%"PRIx32, ntohl(update.xid));
2048 continue;
2049 }
2050
2051 ds_put_format(string, " table=%"PRIu8, update.table_id);
2052 if (update.idle_timeout != OFP_FLOW_PERMANENT) {
2053 ds_put_format(string, " idle_timeout=%"PRIu16,
2054 update.idle_timeout);
2055 }
2056 if (update.hard_timeout != OFP_FLOW_PERMANENT) {
2057 ds_put_format(string, " hard_timeout=%"PRIu16,
2058 update.hard_timeout);
2059 }
2060 ds_put_format(string, " cookie=%#"PRIx64, ntohll(update.cookie));
2061
2062 ds_put_char(string, ' ');
81a76618 2063 match_format(update.match, string, OFP_DEFAULT_PRIORITY);
2b07c8b1
BP
2064
2065 if (update.ofpacts_len) {
2066 if (string->string[string->length - 1] != ' ') {
2067 ds_put_char(string, ' ');
2068 }
455ecd77 2069 ds_put_cstr(string, "actions=");
2b07c8b1
BP
2070 ofpacts_format(update.ofpacts, update.ofpacts_len, string);
2071 }
2072 }
2073}
2074
bdcc5925
BP
2075void
2076ofp_print_version(const struct ofp_header *oh,
2077 struct ds *string)
d1e2cf21 2078{
3811e66b
BP
2079 switch (oh->version) {
2080 case OFP10_VERSION:
2081 break;
2082 case OFP11_VERSION:
2083 ds_put_cstr(string, " (OF1.1)");
4232ef77
SH
2084 break;
2085 case OFP12_VERSION:
2086 ds_put_cstr(string, " (OF1.2)");
3811e66b 2087 break;
2e1ae200
JR
2088 case OFP13_VERSION:
2089 ds_put_cstr(string, " (OF1.3)");
2090 break;
9620f50c
AC
2091 case OFP14_VERSION:
2092 ds_put_cstr(string, " (OF1.4)");
2093 break;
42dccab5
BP
2094 case OFP15_VERSION:
2095 ds_put_cstr(string, " (OF1.5)");
2096 break;
3811e66b
BP
2097 default:
2098 ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
2099 break;
2100 }
2101 ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
bdcc5925 2102}
d1e2cf21 2103
f25b4a81 2104static void
982697a4
BP
2105ofp_header_to_string__(const struct ofp_header *oh, enum ofpraw raw,
2106 struct ds *string)
f25b4a81 2107{
982697a4 2108 ds_put_cstr(string, ofpraw_get_name(raw));
f25b4a81
BP
2109 ofp_print_version(oh, string);
2110}
2111
1c4c05af 2112static void
76c41209 2113ofp_print_bucket_id(struct ds *s, const char *label, uint32_t bucket_id,
1c4c05af
SH
2114 enum ofp_version ofp_version)
2115{
2116 if (ofp_version < OFP15_VERSION) {
2117 return;
2118 }
2119
76c41209 2120 ds_put_cstr(s, label);
1c4c05af
SH
2121
2122 switch (bucket_id) {
2123 case OFPG15_BUCKET_FIRST:
2124 ds_put_cstr(s, "first");
2125 break;
2126 case OFPG15_BUCKET_LAST:
2127 ds_put_cstr(s, "last");
2128 break;
2129 case OFPG15_BUCKET_ALL:
2130 ds_put_cstr(s, "all");
2131 break;
2132 default:
2133 ds_put_format(s, "%"PRIu32, bucket_id);
2134 break;
2135 }
2136
2137 ds_put_char(s, ',');
2138}
2139
7395c052
NZ
2140static void
2141ofp_print_group(struct ds *s, uint32_t group_id, uint8_t type,
76c41209
SH
2142 struct list *p_buckets, enum ofp_version ofp_version,
2143 bool suppress_type)
7395c052 2144{
7395c052
NZ
2145 struct ofputil_bucket *bucket;
2146
76c41209
SH
2147 ds_put_format(s, "group_id=%"PRIu32, group_id);
2148
2149 if (!suppress_type) {
2150 static const char *type_str[] = { "all", "select", "indirect",
2151 "ff", "unknown" };
2152 ds_put_format(s, ",type=%s", type_str[type > 4 ? 4 : type]);
2153 }
2154
7395c052
NZ
2155 if (!p_buckets) {
2156 return;
2157 }
2158
76c41209
SH
2159 ds_put_char(s, ',');
2160
7395c052 2161 LIST_FOR_EACH (bucket, list_node, p_buckets) {
76c41209 2162 ds_put_cstr(s, "bucket=");
7395c052 2163
76c41209 2164 ofp_print_bucket_id(s, "bucket_id:", bucket->bucket_id, ofp_version);
7395c052
NZ
2165 if (bucket->weight != 1) {
2166 ds_put_format(s, "weight:%"PRIu16",", bucket->weight);
2167 }
2168 if (bucket->watch_port != OFPP_NONE) {
2169 ds_put_format(s, "watch_port:%"PRIu32",", bucket->watch_port);
2170 }
2171 if (bucket->watch_group != OFPG11_ANY) {
2172 ds_put_format(s, "watch_group:%"PRIu32",", bucket->watch_group);
2173 }
2174
455ecd77 2175 ds_put_cstr(s, "actions=");
7395c052 2176 ofpacts_format(bucket->ofpacts, bucket->ofpacts_len, s);
76c41209 2177 ds_put_char(s, ',');
7395c052 2178 }
76c41209
SH
2179
2180 ds_chomp(s, ',');
7395c052
NZ
2181}
2182
19187a71
BP
2183static void
2184ofp_print_ofpst_group_desc_request(struct ds *string,
2185 const struct ofp_header *oh)
2186{
2187 uint32_t group_id = ofputil_decode_group_desc_request(oh);
2188 ds_put_cstr(string, " group_id=");
2189 ofputil_format_group(group_id, string);
2190}
2191
7395c052
NZ
2192static void
2193ofp_print_group_desc(struct ds *s, const struct ofp_header *oh)
2194{
2195 struct ofpbuf b;
2196
2197 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2198 for (;;) {
2199 struct ofputil_group_desc gd;
2200 int retval;
2201
a7a2d006 2202 retval = ofputil_decode_group_desc_reply(&gd, &b, oh->version);
7395c052
NZ
2203 if (retval) {
2204 if (retval != EOF) {
2205 ds_put_cstr(s, " ***parse error***");
2206 }
2207 break;
2208 }
2209
2210 ds_put_char(s, '\n');
2211 ds_put_char(s, ' ');
76c41209
SH
2212 ofp_print_group(s, gd.group_id, gd.type, &gd.buckets, oh->version,
2213 false);
fa03bb9e 2214 ofputil_bucket_list_destroy(&gd.buckets);
7395c052
NZ
2215 }
2216}
2217
2218static void
2219ofp_print_ofpst_group_request(struct ds *string, const struct ofp_header *oh)
2220{
2221 enum ofperr error;
2222 uint32_t group_id;
2223
2224 error = ofputil_decode_group_stats_request(oh, &group_id);
2225 if (error) {
2226 ofp_print_error(string, error);
2227 return;
2228 }
2229
2230 ds_put_cstr(string, " group_id=");
2231 ofputil_format_group(group_id, string);
2232}
2233
2234static void
2235ofp_print_group_stats(struct ds *s, const struct ofp_header *oh)
2236{
2237 struct ofpbuf b;
2238 uint32_t bucket_i;
2239
2240 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2241
2242 for (;;) {
2243 struct ofputil_group_stats gs;
2244 int retval;
2245
2246 retval = ofputil_decode_group_stats_reply(&b, &gs);
2247 if (retval) {
2248 if (retval != EOF) {
2249 ds_put_cstr(s, " ***parse error***");
2250 }
2251 break;
2252 }
2253
2254 ds_put_char(s, '\n');
2255
2256 ds_put_char(s, ' ');
2257 ds_put_format(s, "group_id=%"PRIu32",", gs.group_id);
2258
2259 if (gs.duration_sec != UINT32_MAX) {
2260 ds_put_cstr(s, "duration=");
2261 ofp_print_duration(s, gs.duration_sec, gs.duration_nsec);
2262 ds_put_char(s, ',');
2263 }
2264 ds_put_format(s, "ref_count=%"PRIu32",", gs.ref_count);
2265 ds_put_format(s, "packet_count=%"PRIu64",", gs.packet_count);
2266 ds_put_format(s, "byte_count=%"PRIu64"", gs.byte_count);
2267
2268 for (bucket_i = 0; bucket_i < gs.n_buckets; bucket_i++) {
2269 if (gs.bucket_stats[bucket_i].packet_count != UINT64_MAX) {
2270 ds_put_format(s, ",bucket%"PRIu32":", bucket_i);
2271 ds_put_format(s, "packet_count=%"PRIu64",", gs.bucket_stats[bucket_i].packet_count);
2272 ds_put_format(s, "byte_count=%"PRIu64"", gs.bucket_stats[bucket_i].byte_count);
2273 }
2274 }
646b2a9c
SH
2275
2276 free(gs.bucket_stats);
7395c052
NZ
2277 }
2278}
2279
08d1e234
BP
2280static const char *
2281group_type_to_string(enum ofp11_group_type type)
2282{
2283 switch (type) {
2284 case OFPGT11_ALL: return "all";
2285 case OFPGT11_SELECT: return "select";
2286 case OFPGT11_INDIRECT: return "indirect";
2287 case OFPGT11_FF: return "fast failover";
2288 default: OVS_NOT_REACHED();
2289 }
2290}
2291
7395c052
NZ
2292static void
2293ofp_print_group_features(struct ds *string, const struct ofp_header *oh)
2294{
2295 struct ofputil_group_features features;
08d1e234 2296 int i;
7395c052
NZ
2297
2298 ofputil_decode_group_features_reply(oh, &features);
2299
2300 ds_put_format(string, "\n Group table:\n");
2301 ds_put_format(string, " Types: 0x%"PRIx32"\n", features.types);
2302 ds_put_format(string, " Capabilities: 0x%"PRIx32"\n",
2303 features.capabilities);
2304
08d1e234
BP
2305 for (i = 0; i < OFPGT12_N_TYPES; i++) {
2306 if (features.types & (1u << i)) {
2307 ds_put_format(string, " %s group:\n", group_type_to_string(i));
2308 ds_put_format(string, " max_groups=%#"PRIx32"\n",
2309 features.max_groups[i]);
2310 ds_put_format(string, " actions: ");
2311 ofpact_bitmap_format(features.ofpacts[i], string);
2312 ds_put_char(string, '\n');
2313 }
7395c052
NZ
2314 }
2315}
2316
2317static void
2318ofp_print_group_mod(struct ds *s, const struct ofp_header *oh)
2319{
2320 struct ofputil_group_mod gm;
2321 int error;
76c41209 2322 bool bucket_command = false;
7395c052
NZ
2323
2324 error = ofputil_decode_group_mod(oh, &gm);
2325 if (error) {
2326 ofp_print_error(s, error);
2327 return;
2328 }
2329
2330 ds_put_char(s, '\n');
2331
2332 ds_put_char(s, ' ');
2333 switch (gm.command) {
2334 case OFPGC11_ADD:
2335 ds_put_cstr(s, "ADD");
2336 break;
2337
2338 case OFPGC11_MODIFY:
2339 ds_put_cstr(s, "MOD");
2340 break;
2341
2342 case OFPGC11_DELETE:
2343 ds_put_cstr(s, "DEL");
2344 break;
2345
76c41209
SH
2346 case OFPGC15_INSERT_BUCKET:
2347 ds_put_cstr(s, "INSERT_BUCKET");
2348 bucket_command = true;
2349 break;
2350
2351 case OFPGC15_REMOVE_BUCKET:
2352 ds_put_cstr(s, "REMOVE_BUCKET");
2353 bucket_command = true;
2354 break;
2355
7395c052
NZ
2356 default:
2357 ds_put_format(s, "cmd:%"PRIu16"", gm.command);
2358 }
2359 ds_put_char(s, ' ');
2360
76c41209
SH
2361 if (bucket_command) {
2362 ofp_print_bucket_id(s, "command_bucket_id:",
2363 gm.command_bucket_id, oh->version);
2364 }
2365
2366 ofp_print_group(s, gm.group_id, gm.type, &gm.buckets, oh->version,
2367 bucket_command);
fa03bb9e 2368 ofputil_bucket_list_destroy(&gm.buckets);
7395c052
NZ
2369}
2370
5deff5aa
AW
2371static void
2372print_table_action_features(struct ds *s,
2373 const struct ofputil_table_action_features *taf)
2374{
3c1bb396
BP
2375 if (taf->ofpacts) {
2376 ds_put_cstr(s, " actions: ");
2377 ofpact_bitmap_format(taf->ofpacts, s);
2378 ds_put_char(s, '\n');
2379 }
5deff5aa 2380
abadfcb0 2381 if (!bitmap_is_all_zeros(taf->set_fields.bm, MFF_N_IDS)) {
5deff5aa
AW
2382 int i;
2383
3c1bb396 2384 ds_put_cstr(s, " supported on Set-Field:");
abadfcb0 2385 BITMAP_FOR_EACH_1 (i, MFF_N_IDS, taf->set_fields.bm) {
3c1bb396 2386 ds_put_format(s, " %s", mf_from_id(i)->name);
5deff5aa 2387 }
3c1bb396 2388 ds_put_char(s, '\n');
5deff5aa 2389 }
5deff5aa
AW
2390}
2391
2392static bool
2393table_action_features_equal(const struct ofputil_table_action_features *a,
2394 const struct ofputil_table_action_features *b)
2395{
08d1e234 2396 return (a->ofpacts == b->ofpacts
abadfcb0 2397 && bitmap_equal(a->set_fields.bm, b->set_fields.bm, MFF_N_IDS));
5deff5aa
AW
2398}
2399
3c1bb396
BP
2400static bool
2401table_action_features_empty(const struct ofputil_table_action_features *taf)
2402{
2403 return !taf->ofpacts && bitmap_is_all_zeros(taf->set_fields.bm, MFF_N_IDS);
2404}
2405
5deff5aa
AW
2406static void
2407print_table_instruction_features(
2408 struct ds *s, const struct ofputil_table_instruction_features *tif)
2409{
2410 int start, end;
2411
3c1bb396
BP
2412 if (!bitmap_is_all_zeros(tif->next, 255)) {
2413 ds_put_cstr(s, " next tables: ");
2414 for (start = bitmap_scan(tif->next, 1, 0, 255); start < 255;
2415 start = bitmap_scan(tif->next, 1, end, 255)) {
2416 end = bitmap_scan(tif->next, 0, start + 1, 255);
2417 if (end == start + 1) {
2418 ds_put_format(s, "%d,", start);
2419 } else {
2420 ds_put_format(s, "%d-%d,", start, end - 1);
2421 }
5deff5aa 2422 }
3c1bb396
BP
2423 ds_chomp(s, ',');
2424 if (ds_last(s) == ' ') {
2425 ds_put_cstr(s, "none");
2426 }
2427 ds_put_char(s, '\n');
5deff5aa 2428 }
5deff5aa 2429
5deff5aa 2430 if (tif->instructions) {
3c1bb396 2431 ds_put_cstr(s, " instructions: ");
5deff5aa
AW
2432 int i;
2433
2434 for (i = 0; i < 32; i++) {
2435 if (tif->instructions & (1u << i)) {
2436 ds_put_format(s, "%s,", ovs_instruction_name_from_type(i));
2437 }
2438 }
2439 ds_chomp(s, ',');
3c1bb396 2440 ds_put_char(s, '\n');
5deff5aa 2441 }
5deff5aa 2442
3c1bb396 2443 if (!table_action_features_equal(&tif->write, &tif->apply)) {
5deff5aa
AW
2444 ds_put_cstr(s, " Write-Actions features:\n");
2445 print_table_action_features(s, &tif->write);
2446 ds_put_cstr(s, " Apply-Actions features:\n");
2447 print_table_action_features(s, &tif->apply);
3c1bb396
BP
2448 } else if (tif->write.ofpacts
2449 || !bitmap_is_all_zeros(tif->write.set_fields.bm, MFF_N_IDS)) {
2450 ds_put_cstr(s, " Write-Actions and Apply-Actions features:\n");
2451 print_table_action_features(s, &tif->write);
5deff5aa
AW
2452 }
2453}
2454
2455static bool
2456table_instruction_features_equal(
2457 const struct ofputil_table_instruction_features *a,
2458 const struct ofputil_table_instruction_features *b)
2459{
2460 return (bitmap_equal(a->next, b->next, 255)
2461 && a->instructions == b->instructions
2462 && table_action_features_equal(&a->write, &b->write)
2463 && table_action_features_equal(&a->apply, &b->apply));
2464}
2465
3c1bb396
BP
2466static bool
2467table_instruction_features_empty(
2468 const struct ofputil_table_instruction_features *tif)
5deff5aa 2469{
3c1bb396
BP
2470 return (bitmap_is_all_zeros(tif->next, 255)
2471 && !tif->instructions
2472 && table_action_features_empty(&tif->write)
2473 && table_action_features_empty(&tif->apply));
2474}
5deff5aa 2475
3c1bb396
BP
2476static void
2477ofp_print_table_features(struct ds *s,
2478 const struct ofputil_table_features *features,
2479 const struct ofputil_table_stats *stats)
2480{
2481 int i;
5deff5aa 2482
3c1bb396
BP
2483 ds_put_format(s, "\n table %"PRIu8, features->table_id);
2484 if (features->name[0]) {
2485 ds_put_format(s, " (\"%s\")", features->name);
2486 }
2487 ds_put_cstr(s, ":\n");
2488 if (stats) {
2489 ds_put_format(s, " active=%"PRIu32", ", stats->active_count);
2490 ds_put_format(s, "lookup=%"PRIu64", ", stats->lookup_count);
2491 ds_put_format(s, "matched=%"PRIu64"\n", stats->matched_count);
2492 }
2493 if (features->metadata_match || features->metadata_match) {
5deff5aa 2494 ds_put_format(s, " metadata: match=%#"PRIx64" write=%#"PRIx64"\n",
3c1bb396
BP
2495 ntohll(features->metadata_match),
2496 ntohll(features->metadata_write));
2497 }
5deff5aa 2498
3c1bb396 2499 if (features->miss_config != OFPUTIL_TABLE_MISS_DEFAULT) {
5deff5aa 2500 ds_put_cstr(s, " config=");
3c1bb396
BP
2501 ofp_print_table_miss_config(s, features->miss_config);
2502 }
5deff5aa 2503
3c1bb396
BP
2504 if (features->max_entries) {
2505 ds_put_format(s, " max_entries=%"PRIu32"\n", features->max_entries);
2506 }
5deff5aa 2507
3c1bb396
BP
2508 if (!table_instruction_features_equal(&features->nonmiss,
2509 &features->miss)) {
2510 ds_put_cstr(s, " instructions (other than table miss):\n");
2511 print_table_instruction_features(s, &features->nonmiss);
2512 ds_put_cstr(s, " instructions (table miss):\n");
2513 print_table_instruction_features(s, &features->miss);
2514 } else if (!table_instruction_features_empty(&features->nonmiss)) {
2515 ds_put_cstr(s, " instructions (table miss and others):\n");
2516 print_table_instruction_features(s, &features->nonmiss);
2517 }
5deff5aa 2518
3c1bb396 2519 if (!bitmap_is_all_zeros(features->match.bm, MFF_N_IDS)){
5deff5aa 2520 ds_put_cstr(s, " matching:\n");
3c1bb396 2521 BITMAP_FOR_EACH_1 (i, MFF_N_IDS, features->match.bm) {
abadfcb0 2522 const struct mf_field *f = mf_from_id(i);
3c1bb396
BP
2523 bool mask = bitmap_is_set(features->mask.bm, i);
2524 bool wildcard = bitmap_is_set(features->wildcard.bm, i);
abadfcb0
BP
2525
2526 ds_put_format(s, " %s: %s\n",
2527 f->name,
2528 (mask ? "arbitrary mask"
2529 : wildcard ? "exact match or wildcard"
2530 : "must exact match"));
5deff5aa
AW
2531 }
2532 }
2533}
2534
3c1bb396
BP
2535static void
2536ofp_print_table_features_reply(struct ds *s, const struct ofp_header *oh)
2537{
2538 struct ofpbuf b;
2539
2540 ofpbuf_use_const(&b, oh, ntohs(oh->length));
2541
2542 for (;;) {
2543 struct ofputil_table_features tf;
2544 int retval;
2545
2546 retval = ofputil_decode_table_features(&b, &tf, true);
2547 if (retval) {
2548 if (retval != EOF) {
2549 ofp_print_error(s, retval);
2550 }
2551 return;
2552 }
2553 ofp_print_table_features(s, &tf, NULL);
2554 }
2555}
2556
777af88d
AC
2557static const char *
2558bundle_flags_to_name(uint32_t bit)
2559{
2560 switch (bit) {
2561 case OFPBF_ATOMIC:
2562 return "atomic";
2563 case OFPBF_ORDERED:
2564 return "ordered";
2565 default:
2566 return NULL;
2567 }
2568}
2569
2570static void
2571ofp_print_bundle_ctrl(struct ds *s, const struct ofp_header *oh)
2572{
2573 int error;
2574 struct ofputil_bundle_ctrl_msg bctrl;
2575
2576 error = ofputil_decode_bundle_ctrl(oh, &bctrl);
2577 if (error) {
2578 ofp_print_error(s, error);
2579 return;
2580 }
2581
2582 ds_put_char(s, '\n');
2583
2584 ds_put_format(s, " bundle_id=%#"PRIx32" type=", bctrl.bundle_id);
2585 switch (bctrl.type) {
2586 case OFPBCT_OPEN_REQUEST:
2587 ds_put_cstr(s, "OPEN_REQUEST");
2588 break;
2589 case OFPBCT_OPEN_REPLY:
2590 ds_put_cstr(s, "OPEN_REPLY");
2591 break;
2592 case OFPBCT_CLOSE_REQUEST:
2593 ds_put_cstr(s, "CLOSE_REQUEST");
2594 break;
2595 case OFPBCT_CLOSE_REPLY:
2596 ds_put_cstr(s, "CLOSE_REPLY");
2597 break;
2598 case OFPBCT_COMMIT_REQUEST:
2599 ds_put_cstr(s, "COMMIT_REQUEST");
2600 break;
2601 case OFPBCT_COMMIT_REPLY:
2602 ds_put_cstr(s, "COMMIT_REPLY");
2603 break;
2604 case OFPBCT_DISCARD_REQUEST:
2605 ds_put_cstr(s, "DISCARD_REQUEST");
2606 break;
2607 case OFPBCT_DISCARD_REPLY:
2608 ds_put_cstr(s, "DISCARD_REPLY");
2609 break;
2610 }
2611
2612 ds_put_cstr(s, " flags=");
2613 ofp_print_bit_names(s, bctrl.flags, bundle_flags_to_name, ' ');
2614}
2615
2616static void
2617ofp_print_bundle_add(struct ds *s, const struct ofp_header *oh, int verbosity)
2618{
2619 int error;
2620 struct ofputil_bundle_add_msg badd;
2621 char *msg;
2622
2623 error = ofputil_decode_bundle_add(oh, &badd);
2624 if (error) {
2625 ofp_print_error(s, error);
2626 return;
2627 }
2628
2629 ds_put_char(s, '\n');
2630 ds_put_format(s, " bundle_id=%#"PRIx32, badd.bundle_id);
2631 ds_put_cstr(s, " flags=");
2632 ofp_print_bit_names(s, badd.flags, bundle_flags_to_name, ' ');
2633
2634 ds_put_char(s, '\n');
2635 msg = ofp_to_string(badd.msg, ntohs(badd.msg->length), verbosity);
2636 if (msg) {
2637 ds_put_cstr(s, msg);
2638 }
2639}
2640
bdcc5925 2641static void
982697a4
BP
2642ofp_to_string__(const struct ofp_header *oh, enum ofpraw raw,
2643 struct ds *string, int verbosity)
bdcc5925 2644{
bdcc5925
BP
2645 const void *msg = oh;
2646
982697a4
BP
2647 ofp_header_to_string__(oh, raw, string);
2648 switch (ofptype_from_ofpraw(raw)) {
2e1ae200 2649
261bd854 2650 case OFPTYPE_GROUP_STATS_REQUEST:
5d40fc57 2651 ofp_print_stats(string, oh);
7395c052
NZ
2652 ofp_print_ofpst_group_request(string, oh);
2653 break;
2654
261bd854 2655 case OFPTYPE_GROUP_STATS_REPLY:
7395c052
NZ
2656 ofp_print_group_stats(string, oh);
2657 break;
2658
261bd854 2659 case OFPTYPE_GROUP_DESC_STATS_REQUEST:
5d40fc57 2660 ofp_print_stats(string, oh);
19187a71 2661 ofp_print_ofpst_group_desc_request(string, oh);
7395c052
NZ
2662 break;
2663
261bd854 2664 case OFPTYPE_GROUP_DESC_STATS_REPLY:
7395c052
NZ
2665 ofp_print_group_desc(string, oh);
2666 break;
2667
261bd854 2668 case OFPTYPE_GROUP_FEATURES_STATS_REQUEST:
5d40fc57 2669 ofp_print_stats(string, oh);
7395c052
NZ
2670 break;
2671
261bd854 2672 case OFPTYPE_GROUP_FEATURES_STATS_REPLY:
7395c052
NZ
2673 ofp_print_group_features(string, oh);
2674 break;
2675
2676 case OFPTYPE_GROUP_MOD:
2677 ofp_print_group_mod(string, oh);
2678 break;
2679
261bd854
BP
2680 case OFPTYPE_TABLE_FEATURES_STATS_REQUEST:
2681 case OFPTYPE_TABLE_FEATURES_STATS_REPLY:
3c1bb396 2682 ofp_print_table_features_reply(string, oh);
2e1ae200
JR
2683 break;
2684
982697a4 2685 case OFPTYPE_HELLO:
de6c85b0 2686 ofp_print_hello(string, oh);
d1e2cf21
BP
2687 break;
2688
982697a4
BP
2689 case OFPTYPE_ERROR:
2690 ofp_print_error_msg(string, oh);
d1e2cf21
BP
2691 break;
2692
982697a4
BP
2693 case OFPTYPE_ECHO_REQUEST:
2694 case OFPTYPE_ECHO_REPLY:
d1e2cf21
BP
2695 ofp_print_echo(string, oh, verbosity);
2696 break;
2697
982697a4 2698 case OFPTYPE_FEATURES_REQUEST:
d1e2cf21
BP
2699 break;
2700
982697a4
BP
2701 case OFPTYPE_FEATURES_REPLY:
2702 ofp_print_switch_features(string, oh);
d1e2cf21
BP
2703 break;
2704
982697a4 2705 case OFPTYPE_GET_CONFIG_REQUEST:
d1e2cf21
BP
2706 break;
2707
982697a4
BP
2708 case OFPTYPE_GET_CONFIG_REPLY:
2709 case OFPTYPE_SET_CONFIG:
2710 ofp_print_switch_config(string, ofpmsg_body(oh));
d1e2cf21
BP
2711 break;
2712
982697a4
BP
2713 case OFPTYPE_PACKET_IN:
2714 ofp_print_packet_in(string, oh, verbosity);
d1e2cf21
BP
2715 break;
2716
982697a4
BP
2717 case OFPTYPE_FLOW_REMOVED:
2718 ofp_print_flow_removed(string, oh);
d1e2cf21
BP
2719 break;
2720
982697a4
BP
2721 case OFPTYPE_PORT_STATUS:
2722 ofp_print_port_status(string, oh);
d1e2cf21
BP
2723 break;
2724
982697a4
BP
2725 case OFPTYPE_PACKET_OUT:
2726 ofp_print_packet_out(string, oh, verbosity);
d1e2cf21
BP
2727 break;
2728
982697a4
BP
2729 case OFPTYPE_FLOW_MOD:
2730 ofp_print_flow_mod(string, oh, verbosity);
d1e2cf21
BP
2731 break;
2732
982697a4
BP
2733 case OFPTYPE_PORT_MOD:
2734 ofp_print_port_mod(string, oh);
d1e2cf21
BP
2735 break;
2736
918f2b82
AZ
2737 case OFPTYPE_TABLE_MOD:
2738 ofp_print_table_mod(string, oh);
2739 break;
2740
638a19b0
JR
2741 case OFPTYPE_METER_MOD:
2742 ofp_print_meter_mod(string, oh);
2743 break;
2744
982697a4
BP
2745 case OFPTYPE_BARRIER_REQUEST:
2746 case OFPTYPE_BARRIER_REPLY:
d1e2cf21
BP
2747 break;
2748
e8f9a7bb
VG
2749 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
2750 ofp_print_queue_get_config_request(string, oh);
2751 break;
2752
2753 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
2754 ofp_print_queue_get_config_reply(string, oh);
2755 break;
2756
6ea4776b
JR
2757 case OFPTYPE_ROLE_REQUEST:
2758 case OFPTYPE_ROLE_REPLY:
2759 ofp_print_role_message(string, oh);
2760 break;
252f3411 2761 case OFPTYPE_ROLE_STATUS:
00467f73 2762 ofp_print_role_status_message(string, oh);
252f3411 2763 break;
6ea4776b 2764
261bd854
BP
2765 case OFPTYPE_METER_STATS_REQUEST:
2766 case OFPTYPE_METER_CONFIG_STATS_REQUEST:
5d40fc57 2767 ofp_print_stats(string, oh);
638a19b0
JR
2768 ofp_print_meter_stats_request(string, oh);
2769 break;
2770
261bd854 2771 case OFPTYPE_METER_STATS_REPLY:
5d40fc57 2772 ofp_print_stats(string, oh);
638a19b0
JR
2773 ofp_print_meter_stats_reply(string, oh);
2774 break;
2775
261bd854 2776 case OFPTYPE_METER_CONFIG_STATS_REPLY:
5d40fc57 2777 ofp_print_stats(string, oh);
638a19b0
JR
2778 ofp_print_meter_config_reply(string, oh);
2779 break;
2780
261bd854 2781 case OFPTYPE_METER_FEATURES_STATS_REPLY:
5d40fc57 2782 ofp_print_stats(string, oh);
638a19b0
JR
2783 ofp_print_meter_features_reply(string, oh);
2784 break;
2785
982697a4 2786 case OFPTYPE_DESC_STATS_REQUEST:
261bd854 2787 case OFPTYPE_METER_FEATURES_STATS_REQUEST:
5d40fc57 2788 ofp_print_stats(string, oh);
d1e2cf21
BP
2789 break;
2790
982697a4
BP
2791 case OFPTYPE_FLOW_STATS_REQUEST:
2792 case OFPTYPE_AGGREGATE_STATS_REQUEST:
5d40fc57 2793 ofp_print_stats(string, oh);
982697a4 2794 ofp_print_flow_stats_request(string, oh);
d1e2cf21
BP
2795 break;
2796
982697a4 2797 case OFPTYPE_TABLE_STATS_REQUEST:
5d40fc57 2798 ofp_print_stats(string, oh);
d1e2cf21
BP
2799 break;
2800
982697a4 2801 case OFPTYPE_PORT_STATS_REQUEST:
5d40fc57 2802 ofp_print_stats(string, oh);
982697a4 2803 ofp_print_ofpst_port_request(string, oh);
d1e2cf21
BP
2804 break;
2805
982697a4 2806 case OFPTYPE_QUEUE_STATS_REQUEST:
5d40fc57 2807 ofp_print_stats(string, oh);
982697a4 2808 ofp_print_ofpst_queue_request(string, oh);
d1e2cf21
BP
2809 break;
2810
982697a4 2811 case OFPTYPE_DESC_STATS_REPLY:
5d40fc57 2812 ofp_print_stats(string, oh);
982697a4 2813 ofp_print_ofpst_desc_reply(string, oh);
d1e2cf21
BP
2814 break;
2815
982697a4 2816 case OFPTYPE_FLOW_STATS_REPLY:
5d40fc57 2817 ofp_print_stats(string, oh);
4ffd1b43 2818 ofp_print_flow_stats_reply(string, oh);
d1e2cf21
BP
2819 break;
2820
982697a4 2821 case OFPTYPE_QUEUE_STATS_REPLY:
5d40fc57 2822 ofp_print_stats(string, oh);
d1e2cf21
BP
2823 ofp_print_ofpst_queue_reply(string, oh, verbosity);
2824 break;
2825
982697a4 2826 case OFPTYPE_PORT_STATS_REPLY:
5d40fc57 2827 ofp_print_stats(string, oh);
d1e2cf21
BP
2828 ofp_print_ofpst_port_reply(string, oh, verbosity);
2829 break;
2830
982697a4 2831 case OFPTYPE_TABLE_STATS_REPLY:
5d40fc57 2832 ofp_print_stats(string, oh);
3c1bb396 2833 ofp_print_table_stats_reply(string, oh);
d1e2cf21
BP
2834 break;
2835
982697a4 2836 case OFPTYPE_AGGREGATE_STATS_REPLY:
5d40fc57 2837 ofp_print_stats(string, oh);
982697a4 2838 ofp_print_aggregate_stats_reply(string, oh);
d1e2cf21 2839 break;
064af421 2840
70ae4f93 2841 case OFPTYPE_PORT_DESC_STATS_REQUEST:
5d40fc57 2842 ofp_print_stats(string, oh);
70ae4f93
BP
2843 ofp_print_ofpst_port_desc_request(string, oh);
2844 break;
2845
982697a4 2846 case OFPTYPE_PORT_DESC_STATS_REPLY:
5d40fc57 2847 ofp_print_stats(string, oh);
2be393ed
JP
2848 ofp_print_ofpst_port_desc_reply(string, oh);
2849 break;
2850
982697a4
BP
2851 case OFPTYPE_FLOW_MOD_TABLE_ID:
2852 ofp_print_nxt_flow_mod_table_id(string, ofpmsg_body(oh));
6c1491fb
BP
2853 break;
2854
982697a4
BP
2855 case OFPTYPE_SET_FLOW_FORMAT:
2856 ofp_print_nxt_set_flow_format(string, ofpmsg_body(oh));
7fa91113
BP
2857 break;
2858
982697a4
BP
2859 case OFPTYPE_SET_PACKET_IN_FORMAT:
2860 ofp_print_nxt_set_packet_in_format(string, ofpmsg_body(oh));
54834960
EJ
2861 break;
2862
982697a4 2863 case OFPTYPE_FLOW_AGE:
f27f2134
BP
2864 break;
2865
982697a4
BP
2866 case OFPTYPE_SET_CONTROLLER_ID:
2867 ofp_print_nxt_set_controller_id(string, ofpmsg_body(oh));
a7349929
BP
2868 break;
2869
db09e430 2870 case OFPTYPE_GET_ASYNC_REPLY:
982697a4
BP
2871 case OFPTYPE_SET_ASYNC_CONFIG:
2872 ofp_print_nxt_set_async_config(string, ofpmsg_body(oh));
d1e2cf21 2873 break;
db09e430
AC
2874 case OFPTYPE_GET_ASYNC_REQUEST:
2875 break;
982697a4 2876 case OFPTYPE_FLOW_MONITOR_CANCEL:
2b07c8b1
BP
2877 ofp_print_nxt_flow_monitor_cancel(string, msg);
2878 break;
2879
982697a4
BP
2880 case OFPTYPE_FLOW_MONITOR_PAUSED:
2881 case OFPTYPE_FLOW_MONITOR_RESUMED:
2b07c8b1
BP
2882 break;
2883
982697a4 2884 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
2b07c8b1
BP
2885 ofp_print_nxst_flow_monitor_request(string, msg);
2886 break;
2887
982697a4 2888 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
2b07c8b1
BP
2889 ofp_print_nxst_flow_monitor_reply(string, msg);
2890 break;
777af88d
AC
2891
2892 case OFPTYPE_BUNDLE_CONTROL:
2893 ofp_print_bundle_ctrl(string, msg);
2894 break;
2895
2896 case OFPTYPE_BUNDLE_ADD_MESSAGE:
2897 ofp_print_bundle_add(string, msg, verbosity);
2898 break;
246e61ea 2899 }
d1e2cf21 2900}
064af421
BP
2901
2902/* Composes and returns a string representing the OpenFlow packet of 'len'
2903 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
2904 * verbosity and higher numbers increase verbosity. The caller is responsible
2905 * for freeing the string. */
2906char *
2907ofp_to_string(const void *oh_, size_t len, int verbosity)
2908{
2909 struct ds string = DS_EMPTY_INITIALIZER;
2910 const struct ofp_header *oh = oh_;
064af421 2911
49ad0403
BP
2912 if (!len) {
2913 ds_put_cstr(&string, "OpenFlow message is empty\n");
2914 } else if (len < sizeof(struct ofp_header)) {
34582733 2915 ds_put_format(&string, "OpenFlow packet too short (only %"PRIuSIZE" bytes):\n",
49ad0403 2916 len);
d1e2cf21 2917 } else if (ntohs(oh->length) > len) {
f25b4a81 2918 enum ofperr error;
982697a4 2919 enum ofpraw raw;
f25b4a81 2920
982697a4 2921 error = ofpraw_decode_partial(&raw, oh, len);
f25b4a81 2922 if (!error) {
982697a4 2923 ofp_header_to_string__(oh, raw, &string);
f25b4a81
BP
2924 ds_put_char(&string, '\n');
2925 }
2926
d1e2cf21 2927 ds_put_format(&string,
34582733 2928 "(***truncated to %"PRIuSIZE" bytes from %"PRIu16"***)\n",
d1e2cf21
BP
2929 len, ntohs(oh->length));
2930 } else if (ntohs(oh->length) < len) {
2931 ds_put_format(&string,
34582733 2932 "(***only uses %"PRIu16" bytes out of %"PRIuSIZE"***)\n",
d1e2cf21
BP
2933 ntohs(oh->length), len);
2934 } else {
90bf1e07 2935 enum ofperr error;
982697a4 2936 enum ofpraw raw;
d1e2cf21 2937
982697a4 2938 error = ofpraw_decode(&raw, oh);
d1e2cf21 2939 if (!error) {
982697a4 2940 ofp_to_string__(oh, raw, &string, verbosity);
7fa91113
BP
2941 if (verbosity >= 5) {
2942 if (ds_last(&string) != '\n') {
2943 ds_put_char(&string, '\n');
2944 }
d1e2cf21
BP
2945 ds_put_hex_dump(&string, oh, len, 0, true);
2946 }
7fa91113 2947 if (ds_last(&string) != '\n') {
d1e2cf21
BP
2948 ds_put_char(&string, '\n');
2949 }
2950 return ds_steal_cstr(&string);
064af421 2951 }
064af421 2952
7fa91113 2953 ofp_print_error(&string, error);
064af421 2954 }
d1e2cf21
BP
2955 ds_put_hex_dump(&string, oh, len, 0, true);
2956 return ds_steal_cstr(&string);
064af421 2957}
064af421
BP
2958\f
2959static void
d295e8e9 2960print_and_free(FILE *stream, char *string)
064af421
BP
2961{
2962 fputs(string, stream);
2963 free(string);
2964}
2965
2966/* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
2967 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
2968 * numbers increase verbosity. */
2969void
2970ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
2971{
2972 print_and_free(stream, ofp_to_string(oh, len, verbosity));
2973}
2974
2975/* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
897a8e07 2976 * 'data' to 'stream'. */
064af421 2977void
c499c75d 2978ofp_print_packet(FILE *stream, const void *data, size_t len)
064af421 2979{
c499c75d 2980 print_and_free(stream, ofp_packet_to_string(data, len));
064af421 2981}