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