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