2 * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013 Nicira, Inc.
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:
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "ofp-print.h"
22 #include <sys/types.h>
23 #include <netinet/in.h>
30 #include "byte-order.h"
32 #include "dynamic-string.h"
35 #include "multipath.h"
36 #include "meta-flow.h"
39 #include "ofp-actions.h"
40 #include "ofp-errors.h"
44 #include "openflow/openflow.h"
45 #include "openflow/nicira-ext.h"
47 #include "type-props.h"
48 #include "unaligned.h"
51 static void ofp_print_queue_name(struct ds *string, uint32_t port);
52 static void ofp_print_error(struct ds *, enum ofperr);
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.*/
58 ofp_packet_to_string(const void *data, size_t len)
60 struct ds ds = DS_EMPTY_INITIALIZER;
64 ofpbuf_use_const(&buf, data, len);
65 flow_extract(&buf, 0, 0, NULL, 0, &flow);
66 flow_format(&ds, &flow);
69 if (flow.nw_proto == IPPROTO_TCP) {
70 struct tcp_header *th = buf.l4;
71 ds_put_format(&ds, " tcp_csum:%"PRIx16,
73 } else if (flow.nw_proto == IPPROTO_UDP) {
74 struct udp_header *uh = buf.l4;
75 ds_put_format(&ds, " udp_csum:%"PRIx16,
80 ds_put_char(&ds, '\n');
86 ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
89 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
90 struct ofputil_packet_in pin;
94 error = ofputil_decode_packet_in(&pin, oh);
96 ofp_print_error(string, error);
101 ds_put_format(string, " table_id=%"PRIu8, pin.table_id);
105 ds_put_format(string, " cookie=0x%"PRIx64, ntohll(pin.cookie));
108 ds_put_format(string, " total_len=%"PRIu16" in_port=", pin.total_len);
109 ofputil_format_port(pin.fmd.in_port, string);
111 if (pin.fmd.tun_id != htonll(0)) {
112 ds_put_format(string, " tun_id=0x%"PRIx64, ntohll(pin.fmd.tun_id));
115 if (pin.fmd.tun_src != htonl(0)) {
116 ds_put_format(string, " tun_src="IP_FMT, IP_ARGS(pin.fmd.tun_src));
119 if (pin.fmd.tun_dst != htonl(0)) {
120 ds_put_format(string, " tun_dst="IP_FMT, IP_ARGS(pin.fmd.tun_dst));
123 if (pin.fmd.metadata != htonll(0)) {
124 ds_put_format(string, " metadata=0x%"PRIx64, ntohll(pin.fmd.metadata));
127 for (i = 0; i < FLOW_N_REGS; i++) {
128 if (pin.fmd.regs[i]) {
129 ds_put_format(string, " reg%d=0x%"PRIx32, i, pin.fmd.regs[i]);
133 ds_put_format(string, " (via %s)",
134 ofputil_packet_in_reason_to_string(pin.reason, reasonbuf,
137 ds_put_format(string, " data_len=%zu", pin.packet_len);
138 if (pin.buffer_id == UINT32_MAX) {
139 ds_put_format(string, " (unbuffered)");
140 if (pin.total_len != pin.packet_len) {
141 ds_put_format(string, " (***total_len != data_len***)");
144 ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
145 if (pin.total_len < pin.packet_len) {
146 ds_put_format(string, " (***total_len < data_len***)");
149 ds_put_char(string, '\n');
152 char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
153 ds_put_cstr(string, packet);
157 ds_put_hex_dump(string, pin.packet, pin.packet_len, 0, false);
162 ofp_print_packet_out(struct ds *string, const struct ofp_header *oh,
165 struct ofputil_packet_out po;
166 struct ofpbuf ofpacts;
169 ofpbuf_init(&ofpacts, 64);
170 error = ofputil_decode_packet_out(&po, oh, &ofpacts);
172 ofpbuf_uninit(&ofpacts);
173 ofp_print_error(string, error);
177 ds_put_cstr(string, " in_port=");
178 ofputil_format_port(po.in_port, string);
180 ds_put_char(string, ' ');
181 ofpacts_format(po.ofpacts, po.ofpacts_len, string);
183 if (po.buffer_id == UINT32_MAX) {
184 ds_put_format(string, " data_len=%zu", po.packet_len);
185 if (verbosity > 0 && po.packet_len > 0) {
186 char *packet = ofp_packet_to_string(po.packet, po.packet_len);
187 ds_put_char(string, '\n');
188 ds_put_cstr(string, packet);
192 ds_put_hex_dump(string, po.packet, po.packet_len, 0, false);
195 ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id);
198 ofpbuf_uninit(&ofpacts);
201 /* qsort comparison function. */
203 compare_ports(const void *a_, const void *b_)
205 const struct ofputil_phy_port *a = a_;
206 const struct ofputil_phy_port *b = b_;
207 uint16_t ap = a->port_no;
208 uint16_t bp = b->port_no;
210 return ap < bp ? -1 : ap > bp;
214 ofp_print_bit_names(struct ds *string, uint32_t bits,
215 const char *(*bit_to_name)(uint32_t bit),
222 ds_put_cstr(string, "0");
226 for (i = 0; i < 32; i++) {
227 uint32_t bit = UINT32_C(1) << i;
230 const char *name = bit_to_name(bit);
233 ds_put_char(string, separator);
235 ds_put_cstr(string, name);
243 ds_put_char(string, separator);
245 ds_put_format(string, "0x%"PRIx32, bits);
250 netdev_feature_to_name(uint32_t bit)
252 enum netdev_features f = bit;
255 case NETDEV_F_10MB_HD: return "10MB-HD";
256 case NETDEV_F_10MB_FD: return "10MB-FD";
257 case NETDEV_F_100MB_HD: return "100MB-HD";
258 case NETDEV_F_100MB_FD: return "100MB-FD";
259 case NETDEV_F_1GB_HD: return "1GB-HD";
260 case NETDEV_F_1GB_FD: return "1GB-FD";
261 case NETDEV_F_10GB_FD: return "10GB-FD";
262 case NETDEV_F_40GB_FD: return "40GB-FD";
263 case NETDEV_F_100GB_FD: return "100GB-FD";
264 case NETDEV_F_1TB_FD: return "1TB-FD";
265 case NETDEV_F_OTHER: return "OTHER";
266 case NETDEV_F_COPPER: return "COPPER";
267 case NETDEV_F_FIBER: return "FIBER";
268 case NETDEV_F_AUTONEG: return "AUTO_NEG";
269 case NETDEV_F_PAUSE: return "AUTO_PAUSE";
270 case NETDEV_F_PAUSE_ASYM: return "AUTO_PAUSE_ASYM";
277 ofp_print_port_features(struct ds *string, enum netdev_features features)
279 ofp_print_bit_names(string, features, netdev_feature_to_name, ' ');
280 ds_put_char(string, '\n');
284 ofputil_port_config_to_name(uint32_t bit)
286 enum ofputil_port_config pc = bit;
289 case OFPUTIL_PC_PORT_DOWN: return "PORT_DOWN";
290 case OFPUTIL_PC_NO_STP: return "NO_STP";
291 case OFPUTIL_PC_NO_RECV: return "NO_RECV";
292 case OFPUTIL_PC_NO_RECV_STP: return "NO_RECV_STP";
293 case OFPUTIL_PC_NO_FLOOD: return "NO_FLOOD";
294 case OFPUTIL_PC_NO_FWD: return "NO_FWD";
295 case OFPUTIL_PC_NO_PACKET_IN: return "NO_PACKET_IN";
302 ofp_print_port_config(struct ds *string, enum ofputil_port_config config)
304 ofp_print_bit_names(string, config, ofputil_port_config_to_name, ' ');
305 ds_put_char(string, '\n');
309 ofputil_port_state_to_name(uint32_t bit)
311 enum ofputil_port_state ps = bit;
314 case OFPUTIL_PS_LINK_DOWN: return "LINK_DOWN";
315 case OFPUTIL_PS_BLOCKED: return "BLOCKED";
316 case OFPUTIL_PS_LIVE: return "LIVE";
318 case OFPUTIL_PS_STP_LISTEN:
319 case OFPUTIL_PS_STP_LEARN:
320 case OFPUTIL_PS_STP_FORWARD:
321 case OFPUTIL_PS_STP_BLOCK:
322 /* Handled elsewhere. */
330 ofp_print_port_state(struct ds *string, enum ofputil_port_state state)
332 enum ofputil_port_state stp_state;
334 /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
335 * pattern. We have to special case it.
337 * OVS doesn't support STP, so this field will always be 0 if we are
338 * talking to OVS, so we'd always print STP_LISTEN in that case.
339 * Therefore, we don't print anything at all if the value is STP_LISTEN, to
340 * avoid confusing users. */
341 stp_state = state & OFPUTIL_PS_STP_MASK;
344 (stp_state == OFPUTIL_PS_STP_LEARN ? "STP_LEARN"
345 : stp_state == OFPUTIL_PS_STP_FORWARD ? "STP_FORWARD"
347 state &= ~OFPUTIL_PS_STP_MASK;
349 ofp_print_bit_names(string, state, ofputil_port_state_to_name,
353 ofp_print_bit_names(string, state, ofputil_port_state_to_name, ' ');
355 ds_put_char(string, '\n');
359 ofp_print_phy_port(struct ds *string, const struct ofputil_phy_port *port)
361 char name[sizeof port->name];
364 memcpy(name, port->name, sizeof name);
365 for (j = 0; j < sizeof name - 1; j++) {
366 if (!isprint((unsigned char) name[j])) {
372 ds_put_char(string, ' ');
373 ofputil_format_port(port->port_no, string);
374 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
375 name, ETH_ADDR_ARGS(port->hw_addr));
377 ds_put_cstr(string, " config: ");
378 ofp_print_port_config(string, port->config);
380 ds_put_cstr(string, " state: ");
381 ofp_print_port_state(string, port->state);
384 ds_put_format(string, " current: ");
385 ofp_print_port_features(string, port->curr);
387 if (port->advertised) {
388 ds_put_format(string, " advertised: ");
389 ofp_print_port_features(string, port->advertised);
391 if (port->supported) {
392 ds_put_format(string, " supported: ");
393 ofp_print_port_features(string, port->supported);
396 ds_put_format(string, " peer: ");
397 ofp_print_port_features(string, port->peer);
399 ds_put_format(string, " speed: %"PRIu32" Mbps now, "
400 "%"PRIu32" Mbps max\n",
401 port->curr_speed / UINT32_C(1000),
402 port->max_speed / UINT32_C(1000));
405 /* Given a buffer 'b' that contains an array of OpenFlow ports of type
406 * 'ofp_version', writes a detailed description of each port into
409 ofp_print_phy_ports(struct ds *string, uint8_t ofp_version,
413 struct ofputil_phy_port *ports;
417 n_ports = ofputil_count_phy_ports(ofp_version, b);
419 ports = xmalloc(n_ports * sizeof *ports);
420 for (i = 0; i < n_ports; i++) {
421 error = ofputil_pull_phy_port(ofp_version, b, &ports[i]);
423 ofp_print_error(string, error);
427 qsort(ports, n_ports, sizeof *ports, compare_ports);
428 for (i = 0; i < n_ports; i++) {
429 ofp_print_phy_port(string, &ports[i]);
437 ofputil_capabilities_to_name(uint32_t bit)
439 enum ofputil_capabilities capabilities = bit;
441 switch (capabilities) {
442 case OFPUTIL_C_FLOW_STATS: return "FLOW_STATS";
443 case OFPUTIL_C_TABLE_STATS: return "TABLE_STATS";
444 case OFPUTIL_C_PORT_STATS: return "PORT_STATS";
445 case OFPUTIL_C_IP_REASM: return "IP_REASM";
446 case OFPUTIL_C_QUEUE_STATS: return "QUEUE_STATS";
447 case OFPUTIL_C_ARP_MATCH_IP: return "ARP_MATCH_IP";
448 case OFPUTIL_C_STP: return "STP";
449 case OFPUTIL_C_GROUP_STATS: return "GROUP_STATS";
450 case OFPUTIL_C_PORT_BLOCKED: return "PORT_BLOCKED";
457 ofputil_action_bitmap_to_name(uint32_t bit)
459 enum ofputil_action_bitmap action = bit;
462 case OFPUTIL_A_OUTPUT: return "OUTPUT";
463 case OFPUTIL_A_SET_VLAN_VID: return "SET_VLAN_VID";
464 case OFPUTIL_A_SET_VLAN_PCP: return "SET_VLAN_PCP";
465 case OFPUTIL_A_STRIP_VLAN: return "STRIP_VLAN";
466 case OFPUTIL_A_SET_DL_SRC: return "SET_DL_SRC";
467 case OFPUTIL_A_SET_DL_DST: return "SET_DL_DST";
468 case OFPUTIL_A_SET_NW_SRC: return "SET_NW_SRC";
469 case OFPUTIL_A_SET_NW_DST: return "SET_NW_DST";
470 case OFPUTIL_A_SET_NW_ECN: return "SET_NW_ECN";
471 case OFPUTIL_A_SET_NW_TOS: return "SET_NW_TOS";
472 case OFPUTIL_A_SET_TP_SRC: return "SET_TP_SRC";
473 case OFPUTIL_A_SET_TP_DST: return "SET_TP_DST";
474 case OFPUTIL_A_SET_FIELD: return "SET_FIELD";
475 case OFPUTIL_A_ENQUEUE: return "ENQUEUE";
476 case OFPUTIL_A_COPY_TTL_OUT: return "COPY_TTL_OUT";
477 case OFPUTIL_A_COPY_TTL_IN: return "COPY_TTL_IN";
478 case OFPUTIL_A_SET_MPLS_LABEL: return "SET_MPLS_LABEL";
479 case OFPUTIL_A_SET_MPLS_TC: return "SET_MPLS_TC";
480 case OFPUTIL_A_SET_MPLS_TTL: return "SET_MPLS_TTL";
481 case OFPUTIL_A_DEC_MPLS_TTL: return "DEC_MPLS_TTL";
482 case OFPUTIL_A_PUSH_VLAN: return "PUSH_VLAN";
483 case OFPUTIL_A_POP_VLAN: return "POP_VLAN";
484 case OFPUTIL_A_PUSH_MPLS: return "PUSH_MPLS";
485 case OFPUTIL_A_POP_MPLS: return "POP_MPLS";
486 case OFPUTIL_A_SET_QUEUE: return "SET_QUEUE";
487 case OFPUTIL_A_GROUP: return "GROUP";
488 case OFPUTIL_A_SET_NW_TTL: return "SET_NW_TTL";
489 case OFPUTIL_A_DEC_NW_TTL: return "DEC_NW_TTL";
496 ofp_print_switch_features(struct ds *string, const struct ofp_header *oh)
498 struct ofputil_switch_features features;
502 error = ofputil_decode_switch_features(oh, &features, &b);
504 ofp_print_error(string, error);
508 ds_put_format(string, " dpid:%016"PRIx64"\n", features.datapath_id);
510 ds_put_format(string, "n_tables:%"PRIu8", n_buffers:%"PRIu32,
511 features.n_tables, features.n_buffers);
512 if (features.auxiliary_id) {
513 ds_put_format(string, ", auxiliary_id:%"PRIu8, features.auxiliary_id);
515 ds_put_char(string, '\n');
517 ds_put_cstr(string, "capabilities: ");
518 ofp_print_bit_names(string, features.capabilities,
519 ofputil_capabilities_to_name, ' ');
520 ds_put_char(string, '\n');
522 switch ((enum ofp_version)oh->version) {
524 ds_put_cstr(string, "actions: ");
525 ofp_print_bit_names(string, features.actions,
526 ofputil_action_bitmap_to_name, ' ');
527 ds_put_char(string, '\n');
533 return; /* no ports in ofp13_switch_features */
538 ofp_print_phy_ports(string, oh->version, &b);
542 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
544 enum ofp_config_flags flags;
546 flags = ntohs(osc->flags);
548 ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
549 flags &= ~OFPC_FRAG_MASK;
551 if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) {
552 ds_put_format(string, " invalid_ttl_to_controller");
553 flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER;
557 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
560 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
563 static void print_wild(struct ds *string, const char *leader, int is_wild,
564 int verbosity, const char *format, ...)
565 __attribute__((format(printf, 5, 6)));
567 static void print_wild(struct ds *string, const char *leader, int is_wild,
568 int verbosity, const char *format, ...)
570 if (is_wild && verbosity < 2) {
573 ds_put_cstr(string, leader);
577 va_start(args, format);
578 ds_put_format_valist(string, format, args);
581 ds_put_char(string, '*');
583 ds_put_char(string, ',');
587 print_wild_port(struct ds *string, const char *leader, int is_wild,
588 int verbosity, uint16_t port)
590 if (is_wild && verbosity < 2) {
593 ds_put_cstr(string, leader);
595 ofputil_format_port(port, string);
597 ds_put_char(string, '*');
599 ds_put_char(string, ',');
603 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
604 uint32_t wild_bits, int verbosity)
606 if (wild_bits >= 32 && verbosity < 2) {
609 ds_put_cstr(string, leader);
610 if (wild_bits < 32) {
611 ds_put_format(string, IP_FMT, IP_ARGS(ip));
613 ds_put_format(string, "/%d", 32 - wild_bits);
616 ds_put_char(string, '*');
618 ds_put_char(string, ',');
622 ofp10_match_print(struct ds *f, const struct ofp10_match *om, int verbosity)
624 char *s = ofp10_match_to_string(om, verbosity);
630 ofp10_match_to_string(const struct ofp10_match *om, int verbosity)
632 struct ds f = DS_EMPTY_INITIALIZER;
633 uint32_t w = ntohl(om->wildcards);
634 bool skip_type = false;
635 bool skip_proto = false;
637 if (!(w & OFPFW10_DL_TYPE)) {
639 if (om->dl_type == htons(ETH_TYPE_IP)) {
640 if (!(w & OFPFW10_NW_PROTO)) {
642 if (om->nw_proto == IPPROTO_ICMP) {
643 ds_put_cstr(&f, "icmp,");
644 } else if (om->nw_proto == IPPROTO_TCP) {
645 ds_put_cstr(&f, "tcp,");
646 } else if (om->nw_proto == IPPROTO_UDP) {
647 ds_put_cstr(&f, "udp,");
649 ds_put_cstr(&f, "ip,");
653 ds_put_cstr(&f, "ip,");
655 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
656 ds_put_cstr(&f, "arp,");
657 } else if (om->dl_type == htons(ETH_TYPE_RARP)){
658 ds_put_cstr(&f, "rarp,");
659 } else if (om->dl_type == htons(ETH_TYPE_MPLS)) {
660 ds_put_cstr(&f, "mpls,");
661 } else if (om->dl_type == htons(ETH_TYPE_MPLS_MCAST)) {
662 ds_put_cstr(&f, "mplsm,");
667 print_wild_port(&f, "in_port=", w & OFPFW10_IN_PORT, verbosity,
669 print_wild(&f, "dl_vlan=", w & OFPFW10_DL_VLAN, verbosity,
670 "%d", ntohs(om->dl_vlan));
671 print_wild(&f, "dl_vlan_pcp=", w & OFPFW10_DL_VLAN_PCP, verbosity,
672 "%d", om->dl_vlan_pcp);
673 print_wild(&f, "dl_src=", w & OFPFW10_DL_SRC, verbosity,
674 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
675 print_wild(&f, "dl_dst=", w & OFPFW10_DL_DST, verbosity,
676 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
678 print_wild(&f, "dl_type=", w & OFPFW10_DL_TYPE, verbosity,
679 "0x%04x", ntohs(om->dl_type));
681 print_ip_netmask(&f, "nw_src=", om->nw_src,
682 (w & OFPFW10_NW_SRC_MASK) >> OFPFW10_NW_SRC_SHIFT,
684 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
685 (w & OFPFW10_NW_DST_MASK) >> OFPFW10_NW_DST_SHIFT,
688 if (om->dl_type == htons(ETH_TYPE_ARP) ||
689 om->dl_type == htons(ETH_TYPE_RARP)) {
690 print_wild(&f, "arp_op=", w & OFPFW10_NW_PROTO, verbosity,
693 print_wild(&f, "nw_proto=", w & OFPFW10_NW_PROTO, verbosity,
697 print_wild(&f, "nw_tos=", w & OFPFW10_NW_TOS, verbosity,
699 if (om->nw_proto == IPPROTO_ICMP) {
700 print_wild(&f, "icmp_type=", w & OFPFW10_ICMP_TYPE, verbosity,
701 "%d", ntohs(om->tp_src));
702 print_wild(&f, "icmp_code=", w & OFPFW10_ICMP_CODE, verbosity,
703 "%d", ntohs(om->tp_dst));
705 print_wild(&f, "tp_src=", w & OFPFW10_TP_SRC, verbosity,
706 "%d", ntohs(om->tp_src));
707 print_wild(&f, "tp_dst=", w & OFPFW10_TP_DST, verbosity,
708 "%d", ntohs(om->tp_dst));
710 if (ds_last(&f) == ',') {
717 ofp_print_flow_flags(struct ds *s, uint16_t flags)
719 if (flags & OFPFF_SEND_FLOW_REM) {
720 ds_put_cstr(s, "send_flow_rem ");
722 if (flags & OFPFF_CHECK_OVERLAP) {
723 ds_put_cstr(s, "check_overlap ");
725 if (flags & OFPFF12_RESET_COUNTS) {
726 ds_put_cstr(s, "reset_counts ");
728 if (flags & OFPFF13_NO_PKT_COUNTS) {
729 ds_put_cstr(s, "no_packet_counts ");
731 if (flags & OFPFF13_NO_BYT_COUNTS) {
732 ds_put_cstr(s, "no_byte_counts ");
735 flags &= ~(OFPFF_SEND_FLOW_REM | OFPFF_CHECK_OVERLAP
736 | OFPFF12_RESET_COUNTS
737 | OFPFF13_NO_PKT_COUNTS | OFPFF13_NO_BYT_COUNTS);
739 ds_put_format(s, "flags:0x%"PRIx16" ", flags);
744 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh, int verbosity)
746 struct ofputil_flow_mod fm;
747 struct ofpbuf ofpacts;
751 enum ofputil_protocol protocol;
753 protocol = ofputil_protocol_from_ofp_version(oh->version);
754 protocol = ofputil_protocol_set_tid(protocol, true);
756 ofpbuf_init(&ofpacts, 64);
757 error = ofputil_decode_flow_mod(&fm, oh, protocol, &ofpacts);
759 ofpbuf_uninit(&ofpacts);
760 ofp_print_error(s, error);
765 switch (fm.command) {
767 ds_put_cstr(s, "ADD");
770 ds_put_cstr(s, "MOD");
772 case OFPFC_MODIFY_STRICT:
773 ds_put_cstr(s, "MOD_STRICT");
776 ds_put_cstr(s, "DEL");
778 case OFPFC_DELETE_STRICT:
779 ds_put_cstr(s, "DEL_STRICT");
782 ds_put_format(s, "cmd:%d", fm.command);
784 if (fm.table_id != 0) {
785 ds_put_format(s, " table:%d", fm.table_id);
789 ofpraw_decode(&raw, oh);
790 if (verbosity >= 3 && raw == OFPRAW_OFPT10_FLOW_MOD) {
791 const struct ofp10_flow_mod *ofm = ofpmsg_body(oh);
792 ofp10_match_print(s, &ofm->match, verbosity);
794 /* ofp_print_match() doesn't print priority. */
795 need_priority = true;
796 } else if (verbosity >= 3 && raw == OFPRAW_NXT_FLOW_MOD) {
797 const struct nx_flow_mod *nfm = ofpmsg_body(oh);
798 const void *nxm = nfm + 1;
801 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
802 ds_put_cstr(s, nxm_s);
805 /* nx_match_to_string() doesn't print priority. */
806 need_priority = true;
808 match_format(&fm.match, s, fm.priority);
810 /* match_format() does print priority. */
811 need_priority = false;
814 if (ds_last(s) != ' ') {
817 if (fm.new_cookie != htonll(0) && fm.new_cookie != htonll(UINT64_MAX)) {
818 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.new_cookie));
820 if (fm.cookie_mask != htonll(0)) {
821 ds_put_format(s, "cookie:0x%"PRIx64"/0x%"PRIx64" ",
822 ntohll(fm.cookie), ntohll(fm.cookie_mask));
824 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
825 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
827 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
828 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
830 if (fm.priority != OFP_DEFAULT_PRIORITY && need_priority) {
831 ds_put_format(s, "pri:%"PRIu16" ", fm.priority);
833 if (fm.buffer_id != UINT32_MAX) {
834 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
836 if (fm.out_port != OFPP_ANY) {
837 ds_put_format(s, "out_port:");
838 ofputil_format_port(fm.out_port, s);
842 ofp_print_flow_flags(s, fm.flags);
845 ofpacts_format(fm.ofpacts, fm.ofpacts_len, s);
846 ofpbuf_uninit(&ofpacts);
850 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
852 ds_put_format(string, "%u", sec);
854 ds_put_format(string, ".%09u", nsec);
855 while (string->string[string->length - 1] == '0') {
859 ds_put_char(string, 's');
863 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason)
868 case OFPRR_IDLE_TIMEOUT:
870 case OFPRR_HARD_TIMEOUT:
874 case OFPRR_GROUP_DELETE:
875 return "group_delete";
879 sprintf(s, "%d", (int) reason);
885 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
887 struct ofputil_flow_removed fr;
890 error = ofputil_decode_flow_removed(&fr, oh);
892 ofp_print_error(string, error);
896 ds_put_char(string, ' ');
897 match_format(&fr.match, string, fr.priority);
899 ds_put_format(string, " reason=%s",
900 ofp_flow_removed_reason_to_string(fr.reason));
902 if (fr.table_id != 255) {
903 ds_put_format(string, " table_id=%"PRIu8, fr.table_id);
906 if (fr.cookie != htonll(0)) {
907 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
909 ds_put_cstr(string, " duration");
910 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
911 ds_put_format(string, " idle%"PRIu16, fr.idle_timeout);
912 if (fr.hard_timeout) {
913 /* The hard timeout was only added in OF1.2, so only print it if it is
914 * actually in use to avoid gratuitous change to the formatting. */
915 ds_put_format(string, " hard%"PRIu16, fr.hard_timeout);
917 ds_put_format(string, " pkts%"PRIu64" bytes%"PRIu64"\n",
918 fr.packet_count, fr.byte_count);
922 ofp_print_port_mod(struct ds *string, const struct ofp_header *oh)
924 struct ofputil_port_mod pm;
927 error = ofputil_decode_port_mod(oh, &pm);
929 ofp_print_error(string, error);
933 ds_put_cstr(string, "port: ");
934 ofputil_format_port(pm.port_no, string);
935 ds_put_format(string, ": addr:"ETH_ADDR_FMT"\n",
936 ETH_ADDR_ARGS(pm.hw_addr));
938 ds_put_cstr(string, " config: ");
939 ofp_print_port_config(string, pm.config);
941 ds_put_cstr(string, " mask: ");
942 ofp_print_port_config(string, pm.mask);
944 ds_put_cstr(string, " advertise: ");
946 ofp_print_port_features(string, pm.advertise);
948 ds_put_cstr(string, "UNCHANGED\n");
953 ofp_print_error(struct ds *string, enum ofperr error)
955 if (string->length) {
956 ds_put_char(string, ' ');
958 ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
962 ofp_print_hello(struct ds *string, const struct ofp_header *oh)
964 uint32_t allowed_versions;
967 ok = ofputil_decode_hello(oh, &allowed_versions);
969 ds_put_cstr(string, "\n version bitmap: ");
970 ofputil_format_version_bitmap(string, allowed_versions);
973 ds_put_cstr(string, "\n unknown data in hello:\n");
974 ds_put_hex_dump(string, oh, ntohs(oh->length), 0, true);
979 ofp_print_error_msg(struct ds *string, const struct ofp_header *oh)
981 size_t len = ntohs(oh->length);
982 struct ofpbuf payload;
986 error = ofperr_decode_msg(oh, &payload);
988 ds_put_cstr(string, "***decode error***");
989 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
993 ds_put_format(string, " %s\n", ofperr_get_name(error));
995 if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
996 ds_put_printable(string, payload.data, payload.size);
998 s = ofp_to_string(payload.data, payload.size, 1);
999 ds_put_cstr(string, s);
1005 ofp_print_port_status(struct ds *string, const struct ofp_header *oh)
1007 struct ofputil_port_status ps;
1010 error = ofputil_decode_port_status(oh, &ps);
1012 ofp_print_error(string, error);
1016 if (ps.reason == OFPPR_ADD) {
1017 ds_put_format(string, " ADD:");
1018 } else if (ps.reason == OFPPR_DELETE) {
1019 ds_put_format(string, " DEL:");
1020 } else if (ps.reason == OFPPR_MODIFY) {
1021 ds_put_format(string, " MOD:");
1024 ofp_print_phy_port(string, &ps.desc);
1028 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
1030 const struct ofp_desc_stats *ods = ofpmsg_body(oh);
1032 ds_put_char(string, '\n');
1033 ds_put_format(string, "Manufacturer: %.*s\n",
1034 (int) sizeof ods->mfr_desc, ods->mfr_desc);
1035 ds_put_format(string, "Hardware: %.*s\n",
1036 (int) sizeof ods->hw_desc, ods->hw_desc);
1037 ds_put_format(string, "Software: %.*s\n",
1038 (int) sizeof ods->sw_desc, ods->sw_desc);
1039 ds_put_format(string, "Serial Num: %.*s\n",
1040 (int) sizeof ods->serial_num, ods->serial_num);
1041 ds_put_format(string, "DP Description: %.*s\n",
1042 (int) sizeof ods->dp_desc, ods->dp_desc);
1046 ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
1048 struct ofputil_flow_stats_request fsr;
1051 error = ofputil_decode_flow_stats_request(&fsr, oh);
1053 ofp_print_error(string, error);
1057 if (fsr.table_id != 0xff) {
1058 ds_put_format(string, " table=%"PRIu8, fsr.table_id);
1061 if (fsr.out_port != OFPP_ANY) {
1062 ds_put_cstr(string, " out_port=");
1063 ofputil_format_port(fsr.out_port, string);
1066 ds_put_char(string, ' ');
1067 match_format(&fsr.match, string, OFP_DEFAULT_PRIORITY);
1071 ofp_print_flow_stats(struct ds *string, struct ofputil_flow_stats *fs)
1073 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1074 ntohll(fs->cookie));
1076 ofp_print_duration(string, fs->duration_sec, fs->duration_nsec);
1077 ds_put_format(string, ", table=%"PRIu8", ", fs->table_id);
1078 ds_put_format(string, "n_packets=%"PRIu64", ", fs->packet_count);
1079 ds_put_format(string, "n_bytes=%"PRIu64", ", fs->byte_count);
1080 if (fs->idle_timeout != OFP_FLOW_PERMANENT) {
1081 ds_put_format(string, "idle_timeout=%"PRIu16", ", fs->idle_timeout);
1083 if (fs->hard_timeout != OFP_FLOW_PERMANENT) {
1084 ds_put_format(string, "hard_timeout=%"PRIu16", ", fs->hard_timeout);
1087 ofp_print_flow_flags(string, fs->flags);
1089 if (fs->idle_age >= 0) {
1090 ds_put_format(string, "idle_age=%d, ", fs->idle_age);
1092 if (fs->hard_age >= 0 && fs->hard_age != fs->duration_sec) {
1093 ds_put_format(string, "hard_age=%d, ", fs->hard_age);
1096 match_format(&fs->match, string, fs->priority);
1097 if (string->string[string->length - 1] != ' ') {
1098 ds_put_char(string, ' ');
1101 ofpacts_format(fs->ofpacts, fs->ofpacts_len, string);
1105 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1107 struct ofpbuf ofpacts;
1110 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1111 ofpbuf_init(&ofpacts, 64);
1113 struct ofputil_flow_stats fs;
1116 retval = ofputil_decode_flow_stats_reply(&fs, &b, true, &ofpacts);
1118 if (retval != EOF) {
1119 ds_put_cstr(string, " ***parse error***");
1123 ds_put_char(string, '\n');
1124 ofp_print_flow_stats(string, &fs);
1126 ofpbuf_uninit(&ofpacts);
1130 ofp_print_aggregate_stats_reply(struct ds *string, const struct ofp_header *oh)
1132 struct ofputil_aggregate_stats as;
1135 error = ofputil_decode_aggregate_stats_reply(&as, oh);
1137 ofp_print_error(string, error);
1141 ds_put_format(string, " packet_count=%"PRIu64, as.packet_count);
1142 ds_put_format(string, " byte_count=%"PRIu64, as.byte_count);
1143 ds_put_format(string, " flow_count=%"PRIu32, as.flow_count);
1147 print_port_stat(struct ds *string, const char *leader, uint64_t stat, int more)
1149 ds_put_cstr(string, leader);
1150 if (stat != UINT64_MAX) {
1151 ds_put_format(string, "%"PRIu64, stat);
1153 ds_put_char(string, '?');
1156 ds_put_cstr(string, ", ");
1158 ds_put_cstr(string, "\n");
1163 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1165 uint16_t ofp10_port;
1168 error = ofputil_decode_port_stats_request(oh, &ofp10_port);
1170 ofp_print_error(string, error);
1174 ds_put_cstr(string, " port_no=");
1175 ofputil_format_port(ofp10_port, string);
1179 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1184 ds_put_format(string, " %zu ports\n", ofputil_count_port_stats(oh));
1185 if (verbosity < 1) {
1189 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1191 struct ofputil_port_stats ps;
1194 retval = ofputil_decode_port_stats(&ps, &b);
1196 if (retval != EOF) {
1197 ds_put_cstr(string, " ***parse error***");
1202 ds_put_cstr(string, " port ");
1203 if (ps.port_no < 10) {
1204 ds_put_char(string, ' ');
1206 ofputil_format_port(ps.port_no, string);
1208 ds_put_cstr(string, ": rx ");
1209 print_port_stat(string, "pkts=", ps.stats.rx_packets, 1);
1210 print_port_stat(string, "bytes=", ps.stats.rx_bytes, 1);
1211 print_port_stat(string, "drop=", ps.stats.rx_dropped, 1);
1212 print_port_stat(string, "errs=", ps.stats.rx_errors, 1);
1213 print_port_stat(string, "frame=", ps.stats.rx_frame_errors, 1);
1214 print_port_stat(string, "over=", ps.stats.rx_over_errors, 1);
1215 print_port_stat(string, "crc=", ps.stats.rx_crc_errors, 0);
1217 ds_put_cstr(string, " tx ");
1218 print_port_stat(string, "pkts=", ps.stats.tx_packets, 1);
1219 print_port_stat(string, "bytes=", ps.stats.tx_bytes, 1);
1220 print_port_stat(string, "drop=", ps.stats.tx_dropped, 1);
1221 print_port_stat(string, "errs=", ps.stats.tx_errors, 1);
1222 print_port_stat(string, "coll=", ps.stats.collisions, 0);
1224 if (ps.duration_sec != UINT32_MAX) {
1225 ds_put_cstr(string, " duration=");
1226 ofp_print_duration(string, ps.duration_sec, ps.duration_nsec);
1227 ds_put_char(string, '\n');
1233 ofp_print_one_ofpst_table_reply(struct ds *string, enum ofp_version ofp_version,
1234 const char *name, struct ofp12_table_stats *ts)
1236 char name_[OFP_MAX_TABLE_NAME_LEN + 1];
1238 /* ofp13_table_stats is different */
1239 if (ofp_version > OFP12_VERSION) {
1243 ovs_strlcpy(name_, name, sizeof name_);
1245 ds_put_format(string, " %d: %-8s: ", ts->table_id, name_);
1246 ds_put_format(string, "wild=0x%05"PRIx64", ", ntohll(ts->wildcards));
1247 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1248 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1249 ds_put_cstr(string, " ");
1250 ds_put_format(string, "lookup=%"PRIu64", ", ntohll(ts->lookup_count));
1251 ds_put_format(string, "matched=%"PRIu64"\n", ntohll(ts->matched_count));
1253 if (ofp_version < OFP11_VERSION) {
1257 ds_put_cstr(string, " ");
1258 ds_put_format(string, "match=0x%08"PRIx64", ", ntohll(ts->match));
1259 ds_put_format(string, "instructions=0x%08"PRIx32", ",
1260 ntohl(ts->instructions));
1261 ds_put_format(string, "config=0x%08"PRIx32"\n", ntohl(ts->config));
1262 ds_put_cstr(string, " ");
1263 ds_put_format(string, "write_actions=0x%08"PRIx32", ",
1264 ntohl(ts->write_actions));
1265 ds_put_format(string, "apply_actions=0x%08"PRIx32"\n",
1266 ntohl(ts->apply_actions));
1268 if (ofp_version < OFP12_VERSION) {
1272 ds_put_cstr(string, " ");
1273 ds_put_format(string, "write_setfields=0x%016"PRIx64"\n",
1274 ntohll(ts->write_setfields));
1275 ds_put_cstr(string, " ");
1276 ds_put_format(string, "apply_setfields=0x%016"PRIx64"\n",
1277 ntohll(ts->apply_setfields));
1278 ds_put_cstr(string, " ");
1279 ds_put_format(string, "metadata_match=0x%016"PRIx64"\n",
1280 ntohll(ts->metadata_match));
1281 ds_put_cstr(string, " ");
1282 ds_put_format(string, "metadata_write=0x%016"PRIx64"\n",
1283 ntohll(ts->metadata_write));
1287 ofp_print_ofpst_table_reply13(struct ds *string, const struct ofp_header *oh,
1290 struct ofp13_table_stats *ts;
1294 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1295 ofpraw_pull_assert(&b);
1297 n = b.size / sizeof *ts;
1298 ds_put_format(string, " %zu tables\n", n);
1299 if (verbosity < 1) {
1304 ts = ofpbuf_try_pull(&b, sizeof *ts);
1308 ds_put_format(string,
1309 " %d: active=%"PRIu32", lookup=%"PRIu64 \
1310 ", matched=%"PRIu64"\n",
1311 ts->table_id, ntohl(ts->active_count),
1312 ntohll(ts->lookup_count), ntohll(ts->matched_count));
1317 ofp_print_ofpst_table_reply12(struct ds *string, const struct ofp_header *oh,
1320 struct ofp12_table_stats *ts;
1324 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1325 ofpraw_pull_assert(&b);
1327 n = b.size / sizeof *ts;
1328 ds_put_format(string, " %zu tables\n", n);
1329 if (verbosity < 1) {
1334 ts = ofpbuf_try_pull(&b, sizeof *ts);
1339 ofp_print_one_ofpst_table_reply(string, OFP12_VERSION, ts->name, ts);
1344 ofp_print_ofpst_table_reply11(struct ds *string, const struct ofp_header *oh,
1347 struct ofp11_table_stats *ts;
1351 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1352 ofpraw_pull_assert(&b);
1354 n = b.size / sizeof *ts;
1355 ds_put_format(string, " %zu tables\n", n);
1356 if (verbosity < 1) {
1361 struct ofp12_table_stats ts12;
1363 ts = ofpbuf_try_pull(&b, sizeof *ts);
1368 ts12.table_id = ts->table_id;
1369 ts12.wildcards = htonll(ntohl(ts->wildcards));
1370 ts12.max_entries = ts->max_entries;
1371 ts12.active_count = ts->active_count;
1372 ts12.lookup_count = ts->lookup_count;
1373 ts12.matched_count = ts->matched_count;
1374 ts12.match = htonll(ntohl(ts->match));
1375 ts12.instructions = ts->instructions;
1376 ts12.config = ts->config;
1377 ts12.write_actions = ts->write_actions;
1378 ts12.apply_actions = ts->apply_actions;
1379 ofp_print_one_ofpst_table_reply(string, OFP11_VERSION, ts->name, &ts12);
1384 ofp_print_ofpst_table_reply10(struct ds *string, const struct ofp_header *oh,
1387 struct ofp10_table_stats *ts;
1391 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1392 ofpraw_pull_assert(&b);
1394 n = b.size / sizeof *ts;
1395 ds_put_format(string, " %zu tables\n", n);
1396 if (verbosity < 1) {
1401 struct ofp12_table_stats ts12;
1403 ts = ofpbuf_try_pull(&b, sizeof *ts);
1408 ts12.table_id = ts->table_id;
1409 ts12.wildcards = htonll(ntohl(ts->wildcards));
1410 ts12.max_entries = ts->max_entries;
1411 ts12.active_count = ts->active_count;
1412 ts12.lookup_count = get_32aligned_be64(&ts->lookup_count);
1413 ts12.matched_count = get_32aligned_be64(&ts->matched_count);
1414 ofp_print_one_ofpst_table_reply(string, OFP10_VERSION, ts->name, &ts12);
1419 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1422 switch ((enum ofp_version)oh->version) {
1424 ofp_print_ofpst_table_reply13(string, oh, verbosity);
1428 ofp_print_ofpst_table_reply12(string, oh, verbosity);
1432 ofp_print_ofpst_table_reply11(string, oh, verbosity);
1436 ofp_print_ofpst_table_reply10(string, oh, verbosity);
1445 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1447 if (queue_id == OFPQ_ALL) {
1448 ds_put_cstr(string, "ALL");
1450 ds_put_format(string, "%"PRIu32, queue_id);
1455 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1457 struct ofputil_queue_stats_request oqsr;
1460 error = ofputil_decode_queue_stats_request(oh, &oqsr);
1462 ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
1466 ds_put_cstr(string, "port=");
1467 ofputil_format_port(oqsr.port_no, string);
1469 ds_put_cstr(string, " queue=");
1470 ofp_print_queue_name(string, oqsr.queue_id);
1474 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1479 ds_put_format(string, " %zu queues\n", ofputil_count_queue_stats(oh));
1480 if (verbosity < 1) {
1484 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1486 struct ofputil_queue_stats qs;
1489 retval = ofputil_decode_queue_stats(&qs, &b);
1491 if (retval != EOF) {
1492 ds_put_cstr(string, " ***parse error***");
1497 ds_put_cstr(string, " port ");
1498 ofputil_format_port(qs.port_no, string);
1499 ds_put_cstr(string, " queue ");
1500 ofp_print_queue_name(string, qs.queue_id);
1501 ds_put_cstr(string, ": ");
1503 print_port_stat(string, "bytes=", qs.stats.tx_bytes, 1);
1504 print_port_stat(string, "pkts=", qs.stats.tx_packets, 1);
1505 print_port_stat(string, "errors=", qs.stats.tx_errors, 0);
1510 ofp_print_ofpst_port_desc_reply(struct ds *string,
1511 const struct ofp_header *oh)
1515 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1516 ofpraw_pull_assert(&b);
1517 ds_put_char(string, '\n');
1518 ofp_print_phy_ports(string, oh->version, &b);
1522 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1524 uint16_t flags = ofpmp_flags(oh);
1527 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
1532 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1534 uint16_t flags = ofpmp_flags(oh);
1537 ds_put_cstr(string, " flags=");
1538 if (flags & OFPSF_REPLY_MORE) {
1539 ds_put_cstr(string, "[more]");
1540 flags &= ~OFPSF_REPLY_MORE;
1543 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1550 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1552 size_t len = ntohs(oh->length);
1554 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1555 if (verbosity > 1) {
1556 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1561 ofp_print_role_message(struct ds *string, const struct ofp_header *oh)
1563 struct ofputil_role_request rr;
1566 error = ofputil_decode_role_message(oh, &rr);
1568 ofp_print_error(string, error);
1572 ds_put_cstr(string, " role=");
1575 case OFPCR12_ROLE_NOCHANGE:
1576 ds_put_cstr(string, "nochange");
1578 case OFPCR12_ROLE_EQUAL:
1579 ds_put_cstr(string, "equal"); /* OF 1.2 wording */
1581 case OFPCR12_ROLE_MASTER:
1582 ds_put_cstr(string, "master");
1584 case OFPCR12_ROLE_SLAVE:
1585 ds_put_cstr(string, "slave");
1591 if (rr.have_generation_id) {
1592 ds_put_format(string, " generation_id=%"PRIu64, rr.generation_id);
1597 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1598 const struct nx_flow_mod_table_id *nfmti)
1600 ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1604 ofp_print_nxt_set_flow_format(struct ds *string,
1605 const struct nx_set_flow_format *nsff)
1607 uint32_t format = ntohl(nsff->format);
1609 ds_put_cstr(string, " format=");
1610 if (ofputil_nx_flow_format_is_valid(format)) {
1611 ds_put_cstr(string, ofputil_nx_flow_format_to_string(format));
1613 ds_put_format(string, "%"PRIu32, format);
1618 ofp_print_nxt_set_packet_in_format(struct ds *string,
1619 const struct nx_set_packet_in_format *nspf)
1621 uint32_t format = ntohl(nspf->format);
1623 ds_put_cstr(string, " format=");
1624 if (ofputil_packet_in_format_is_valid(format)) {
1625 ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1627 ds_put_format(string, "%"PRIu32, format);
1632 ofp_port_reason_to_string(enum ofp_port_reason reason)
1647 sprintf(s, "%d", (int) reason);
1653 ofp_print_nxt_set_async_config(struct ds *string,
1654 const struct nx_async_config *nac)
1658 for (i = 0; i < 2; i++) {
1661 ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1663 ds_put_cstr(string, " PACKET_IN:");
1664 for (j = 0; j < 32; j++) {
1665 if (nac->packet_in_mask[i] & htonl(1u << j)) {
1666 char reasonbuf[OFPUTIL_PACKET_IN_REASON_BUFSIZE];
1669 reason = ofputil_packet_in_reason_to_string(j, reasonbuf,
1671 ds_put_format(string, " %s", reason);
1674 if (!nac->packet_in_mask[i]) {
1675 ds_put_cstr(string, " (off)");
1677 ds_put_char(string, '\n');
1679 ds_put_cstr(string, " PORT_STATUS:");
1680 for (j = 0; j < 32; j++) {
1681 if (nac->port_status_mask[i] & htonl(1u << j)) {
1682 ds_put_format(string, " %s", ofp_port_reason_to_string(j));
1685 if (!nac->port_status_mask[i]) {
1686 ds_put_cstr(string, " (off)");
1688 ds_put_char(string, '\n');
1690 ds_put_cstr(string, " FLOW_REMOVED:");
1691 for (j = 0; j < 32; j++) {
1692 if (nac->flow_removed_mask[i] & htonl(1u << j)) {
1693 ds_put_format(string, " %s",
1694 ofp_flow_removed_reason_to_string(j));
1697 if (!nac->flow_removed_mask[i]) {
1698 ds_put_cstr(string, " (off)");
1700 ds_put_char(string, '\n');
1705 ofp_print_nxt_set_controller_id(struct ds *string,
1706 const struct nx_controller_id *nci)
1708 ds_put_format(string, " id=%"PRIu16, ntohs(nci->controller_id));
1712 ofp_print_nxt_flow_monitor_cancel(struct ds *string,
1713 const struct ofp_header *oh)
1715 ds_put_format(string, " id=%"PRIu32,
1716 ofputil_decode_flow_monitor_cancel(oh));
1720 nx_flow_monitor_flags_to_name(uint32_t bit)
1722 enum nx_flow_monitor_flags fmf = bit;
1725 case NXFMF_INITIAL: return "initial";
1726 case NXFMF_ADD: return "add";
1727 case NXFMF_DELETE: return "delete";
1728 case NXFMF_MODIFY: return "modify";
1729 case NXFMF_ACTIONS: return "actions";
1730 case NXFMF_OWN: return "own";
1737 ofp_print_nxst_flow_monitor_request(struct ds *string,
1738 const struct ofp_header *oh)
1742 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1744 struct ofputil_flow_monitor_request request;
1747 retval = ofputil_decode_flow_monitor_request(&request, &b);
1749 if (retval != EOF) {
1750 ofp_print_error(string, retval);
1755 ds_put_format(string, "\n id=%"PRIu32" flags=", request.id);
1756 ofp_print_bit_names(string, request.flags,
1757 nx_flow_monitor_flags_to_name, ',');
1759 if (request.out_port != OFPP_NONE) {
1760 ds_put_cstr(string, " out_port=");
1761 ofputil_format_port(request.out_port, string);
1764 if (request.table_id != 0xff) {
1765 ds_put_format(string, " table=%"PRIu8, request.table_id);
1768 ds_put_char(string, ' ');
1769 match_format(&request.match, string, OFP_DEFAULT_PRIORITY);
1770 ds_chomp(string, ' ');
1775 ofp_print_nxst_flow_monitor_reply(struct ds *string,
1776 const struct ofp_header *oh)
1778 uint64_t ofpacts_stub[1024 / 8];
1779 struct ofpbuf ofpacts;
1782 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1783 ofpbuf_use_stub(&ofpacts, ofpacts_stub, sizeof ofpacts_stub);
1785 struct ofputil_flow_update update;
1789 update.match = &match;
1790 retval = ofputil_decode_flow_update(&update, &b, &ofpacts);
1792 if (retval != EOF) {
1793 ofp_print_error(string, retval);
1795 ofpbuf_uninit(&ofpacts);
1799 ds_put_cstr(string, "\n event=");
1800 switch (update.event) {
1802 ds_put_cstr(string, "ADDED");
1806 ds_put_format(string, "DELETED reason=%s",
1807 ofp_flow_removed_reason_to_string(update.reason));
1810 case NXFME_MODIFIED:
1811 ds_put_cstr(string, "MODIFIED");
1815 ds_put_format(string, "ABBREV xid=0x%"PRIx32, ntohl(update.xid));
1819 ds_put_format(string, " table=%"PRIu8, update.table_id);
1820 if (update.idle_timeout != OFP_FLOW_PERMANENT) {
1821 ds_put_format(string, " idle_timeout=%"PRIu16,
1822 update.idle_timeout);
1824 if (update.hard_timeout != OFP_FLOW_PERMANENT) {
1825 ds_put_format(string, " hard_timeout=%"PRIu16,
1826 update.hard_timeout);
1828 ds_put_format(string, " cookie=%#"PRIx64, ntohll(update.cookie));
1830 ds_put_char(string, ' ');
1831 match_format(update.match, string, OFP_DEFAULT_PRIORITY);
1833 if (update.ofpacts_len) {
1834 if (string->string[string->length - 1] != ' ') {
1835 ds_put_char(string, ' ');
1837 ofpacts_format(update.ofpacts, update.ofpacts_len, string);
1843 ofp_print_version(const struct ofp_header *oh,
1846 switch (oh->version) {
1850 ds_put_cstr(string, " (OF1.1)");
1853 ds_put_cstr(string, " (OF1.2)");
1856 ds_put_cstr(string, " (OF1.3)");
1859 ds_put_format(string, " (OF 0x%02"PRIx8")", oh->version);
1862 ds_put_format(string, " (xid=0x%"PRIx32"):", ntohl(oh->xid));
1866 ofp_header_to_string__(const struct ofp_header *oh, enum ofpraw raw,
1869 ds_put_cstr(string, ofpraw_get_name(raw));
1870 ofp_print_version(oh, string);
1874 ofp_print_not_implemented(struct ds *string)
1876 ds_put_cstr(string, "NOT IMPLEMENTED YET!\n");
1880 ofp_to_string__(const struct ofp_header *oh, enum ofpraw raw,
1881 struct ds *string, int verbosity)
1883 const void *msg = oh;
1885 ofp_header_to_string__(oh, raw, string);
1886 switch (ofptype_from_ofpraw(raw)) {
1888 /* FIXME: Change the following once they are implemented: */
1889 case OFPTYPE_QUEUE_GET_CONFIG_REQUEST:
1890 case OFPTYPE_QUEUE_GET_CONFIG_REPLY:
1891 case OFPTYPE_GET_ASYNC_REQUEST:
1892 case OFPTYPE_GET_ASYNC_REPLY:
1893 case OFPTYPE_METER_MOD:
1894 case OFPTYPE_GROUP_REQUEST:
1895 case OFPTYPE_GROUP_REPLY:
1896 case OFPTYPE_GROUP_DESC_REQUEST:
1897 case OFPTYPE_GROUP_DESC_REPLY:
1898 case OFPTYPE_GROUP_FEATURES_REQUEST:
1899 case OFPTYPE_GROUP_FEATURES_REPLY:
1900 case OFPTYPE_METER_REQUEST:
1901 case OFPTYPE_METER_REPLY:
1902 case OFPTYPE_METER_CONFIG_REQUEST:
1903 case OFPTYPE_METER_CONFIG_REPLY:
1904 case OFPTYPE_METER_FEATURES_REQUEST:
1905 case OFPTYPE_METER_FEATURES_REPLY:
1906 case OFPTYPE_TABLE_FEATURES_REQUEST:
1907 case OFPTYPE_TABLE_FEATURES_REPLY:
1908 ofp_print_not_implemented(string);
1912 ofp_print_hello(string, oh);
1916 ofp_print_error_msg(string, oh);
1919 case OFPTYPE_ECHO_REQUEST:
1920 case OFPTYPE_ECHO_REPLY:
1921 ofp_print_echo(string, oh, verbosity);
1924 case OFPTYPE_FEATURES_REQUEST:
1927 case OFPTYPE_FEATURES_REPLY:
1928 ofp_print_switch_features(string, oh);
1931 case OFPTYPE_GET_CONFIG_REQUEST:
1934 case OFPTYPE_GET_CONFIG_REPLY:
1935 case OFPTYPE_SET_CONFIG:
1936 ofp_print_switch_config(string, ofpmsg_body(oh));
1939 case OFPTYPE_PACKET_IN:
1940 ofp_print_packet_in(string, oh, verbosity);
1943 case OFPTYPE_FLOW_REMOVED:
1944 ofp_print_flow_removed(string, oh);
1947 case OFPTYPE_PORT_STATUS:
1948 ofp_print_port_status(string, oh);
1951 case OFPTYPE_PACKET_OUT:
1952 ofp_print_packet_out(string, oh, verbosity);
1955 case OFPTYPE_FLOW_MOD:
1956 ofp_print_flow_mod(string, oh, verbosity);
1959 case OFPTYPE_PORT_MOD:
1960 ofp_print_port_mod(string, oh);
1963 case OFPTYPE_BARRIER_REQUEST:
1964 case OFPTYPE_BARRIER_REPLY:
1967 case OFPTYPE_ROLE_REQUEST:
1968 case OFPTYPE_ROLE_REPLY:
1969 ofp_print_role_message(string, oh);
1972 case OFPTYPE_DESC_STATS_REQUEST:
1973 case OFPTYPE_PORT_DESC_STATS_REQUEST:
1974 ofp_print_stats_request(string, oh);
1977 case OFPTYPE_FLOW_STATS_REQUEST:
1978 case OFPTYPE_AGGREGATE_STATS_REQUEST:
1979 ofp_print_stats_request(string, oh);
1980 ofp_print_flow_stats_request(string, oh);
1983 case OFPTYPE_TABLE_STATS_REQUEST:
1984 ofp_print_stats_request(string, oh);
1987 case OFPTYPE_PORT_STATS_REQUEST:
1988 ofp_print_stats_request(string, oh);
1989 ofp_print_ofpst_port_request(string, oh);
1992 case OFPTYPE_QUEUE_STATS_REQUEST:
1993 ofp_print_stats_request(string, oh);
1994 ofp_print_ofpst_queue_request(string, oh);
1997 case OFPTYPE_DESC_STATS_REPLY:
1998 ofp_print_stats_reply(string, oh);
1999 ofp_print_ofpst_desc_reply(string, oh);
2002 case OFPTYPE_FLOW_STATS_REPLY:
2003 ofp_print_stats_reply(string, oh);
2004 ofp_print_flow_stats_reply(string, oh);
2007 case OFPTYPE_QUEUE_STATS_REPLY:
2008 ofp_print_stats_reply(string, oh);
2009 ofp_print_ofpst_queue_reply(string, oh, verbosity);
2012 case OFPTYPE_PORT_STATS_REPLY:
2013 ofp_print_stats_reply(string, oh);
2014 ofp_print_ofpst_port_reply(string, oh, verbosity);
2017 case OFPTYPE_TABLE_STATS_REPLY:
2018 ofp_print_stats_reply(string, oh);
2019 ofp_print_ofpst_table_reply(string, oh, verbosity);
2022 case OFPTYPE_AGGREGATE_STATS_REPLY:
2023 ofp_print_stats_reply(string, oh);
2024 ofp_print_aggregate_stats_reply(string, oh);
2027 case OFPTYPE_PORT_DESC_STATS_REPLY:
2028 ofp_print_stats_reply(string, oh);
2029 ofp_print_ofpst_port_desc_reply(string, oh);
2032 case OFPTYPE_FLOW_MOD_TABLE_ID:
2033 ofp_print_nxt_flow_mod_table_id(string, ofpmsg_body(oh));
2036 case OFPTYPE_SET_FLOW_FORMAT:
2037 ofp_print_nxt_set_flow_format(string, ofpmsg_body(oh));
2040 case OFPTYPE_SET_PACKET_IN_FORMAT:
2041 ofp_print_nxt_set_packet_in_format(string, ofpmsg_body(oh));
2044 case OFPTYPE_FLOW_AGE:
2047 case OFPTYPE_SET_CONTROLLER_ID:
2048 ofp_print_nxt_set_controller_id(string, ofpmsg_body(oh));
2051 case OFPTYPE_SET_ASYNC_CONFIG:
2052 ofp_print_nxt_set_async_config(string, ofpmsg_body(oh));
2055 case OFPTYPE_FLOW_MONITOR_CANCEL:
2056 ofp_print_nxt_flow_monitor_cancel(string, msg);
2059 case OFPTYPE_FLOW_MONITOR_PAUSED:
2060 case OFPTYPE_FLOW_MONITOR_RESUMED:
2063 case OFPTYPE_FLOW_MONITOR_STATS_REQUEST:
2064 ofp_print_nxst_flow_monitor_request(string, msg);
2067 case OFPTYPE_FLOW_MONITOR_STATS_REPLY:
2068 ofp_print_nxst_flow_monitor_reply(string, msg);
2073 /* Composes and returns a string representing the OpenFlow packet of 'len'
2074 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
2075 * verbosity and higher numbers increase verbosity. The caller is responsible
2076 * for freeing the string. */
2078 ofp_to_string(const void *oh_, size_t len, int verbosity)
2080 struct ds string = DS_EMPTY_INITIALIZER;
2081 const struct ofp_header *oh = oh_;
2084 ds_put_cstr(&string, "OpenFlow message is empty\n");
2085 } else if (len < sizeof(struct ofp_header)) {
2086 ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
2088 } else if (ntohs(oh->length) > len) {
2092 error = ofpraw_decode_partial(&raw, oh, len);
2094 ofp_header_to_string__(oh, raw, &string);
2095 ds_put_char(&string, '\n');
2098 ds_put_format(&string,
2099 "(***truncated to %zu bytes from %"PRIu16"***)\n",
2100 len, ntohs(oh->length));
2101 } else if (ntohs(oh->length) < len) {
2102 ds_put_format(&string,
2103 "(***only uses %"PRIu16" bytes out of %zu***)\n",
2104 ntohs(oh->length), len);
2109 error = ofpraw_decode(&raw, oh);
2111 ofp_to_string__(oh, raw, &string, verbosity);
2112 if (verbosity >= 5) {
2113 if (ds_last(&string) != '\n') {
2114 ds_put_char(&string, '\n');
2116 ds_put_hex_dump(&string, oh, len, 0, true);
2118 if (ds_last(&string) != '\n') {
2119 ds_put_char(&string, '\n');
2121 return ds_steal_cstr(&string);
2124 ofp_print_error(&string, error);
2126 ds_put_hex_dump(&string, oh, len, 0, true);
2127 return ds_steal_cstr(&string);
2131 print_and_free(FILE *stream, char *string)
2133 fputs(string, stream);
2137 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
2138 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
2139 * numbers increase verbosity. */
2141 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
2143 print_and_free(stream, ofp_to_string(oh, len, verbosity));
2146 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
2147 * 'data' to 'stream'. */
2149 ofp_print_packet(FILE *stream, const void *data, size_t len)
2151 print_and_free(stream, ofp_packet_to_string(data, len));