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