2 * Copyright (c) 2008, 2009, 2010, 2011, 2012 Nicira Networks.
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"
38 #include "ofp-errors.h"
41 #include "openflow/openflow.h"
42 #include "openflow/nicira-ext.h"
45 #include "type-props.h"
46 #include "unaligned.h"
49 static void ofp_print_queue_name(struct ds *string, uint32_t port);
50 static void ofp_print_error(struct ds *, enum ofperr);
53 /* Returns a string that represents the contents of the Ethernet frame in the
54 * 'len' bytes starting at 'data'. The caller must free the returned string.*/
56 ofp_packet_to_string(const void *data, size_t len)
58 struct ds ds = DS_EMPTY_INITIALIZER;
62 ofpbuf_use_const(&buf, data, len);
63 flow_extract(&buf, 0, 0, 0, &flow);
64 flow_format(&ds, &flow);
67 if (flow.nw_proto == IPPROTO_TCP) {
68 struct tcp_header *th = buf.l4;
69 ds_put_format(&ds, " tcp_csum:%"PRIx16,
71 } else if (flow.nw_proto == IPPROTO_UDP) {
72 struct udp_header *uh = buf.l4;
73 ds_put_format(&ds, " udp_csum:%"PRIx16,
78 ds_put_char(&ds, '\n');
84 ofp_packet_in_reason_to_string(enum ofp_packet_in_reason reason)
93 case OFPR_INVALID_TTL:
96 sprintf(s, "%d", (int) reason);
102 ofp_print_packet_in(struct ds *string, const struct ofp_header *oh,
105 struct ofputil_packet_in pin;
109 error = ofputil_decode_packet_in(&pin, oh);
111 ofp_print_error(string, error);
116 ds_put_format(string, " table_id=%"PRIu8, pin.table_id);
120 ds_put_format(string, " cookie=0x%"PRIx64, ntohll(pin.cookie));
123 ds_put_format(string, " total_len=%"PRIu16" in_port=", pin.total_len);
124 ofputil_format_port(pin.fmd.in_port, string);
126 if (pin.fmd.tun_id_mask) {
127 ds_put_format(string, " tun_id=0x%"PRIx64, ntohll(pin.fmd.tun_id));
128 if (pin.fmd.tun_id_mask != htonll(UINT64_MAX)) {
129 ds_put_format(string, "/0x%"PRIx64, ntohll(pin.fmd.tun_id_mask));
133 for (i = 0; i < FLOW_N_REGS; i++) {
134 if (pin.fmd.reg_masks[i]) {
135 ds_put_format(string, " reg%d=0x%"PRIx32, i, pin.fmd.regs[i]);
136 if (pin.fmd.reg_masks[i] != UINT32_MAX) {
137 ds_put_format(string, "/0x%"PRIx32, pin.fmd.reg_masks[i]);
142 ds_put_format(string, " (via %s)",
143 ofp_packet_in_reason_to_string(pin.reason));
145 ds_put_format(string, " data_len=%zu", pin.packet_len);
146 if (pin.buffer_id == UINT32_MAX) {
147 ds_put_format(string, " (unbuffered)");
148 if (pin.total_len != pin.packet_len) {
149 ds_put_format(string, " (***total_len != data_len***)");
152 ds_put_format(string, " buffer=0x%08"PRIx32, pin.buffer_id);
153 if (pin.total_len < pin.packet_len) {
154 ds_put_format(string, " (***total_len < data_len***)");
157 ds_put_char(string, '\n');
160 char *packet = ofp_packet_to_string(pin.packet, pin.packet_len);
161 ds_put_cstr(string, packet);
167 print_note(struct ds *string, const struct nx_action_note *nan)
172 ds_put_cstr(string, "note:");
173 len = ntohs(nan->len) - offsetof(struct nx_action_note, note);
174 for (i = 0; i < len; i++) {
176 ds_put_char(string, '.');
178 ds_put_format(string, "%02"PRIx8, nan->note[i]);
183 ofp_print_action(struct ds *s, const union ofp_action *a,
184 enum ofputil_action_code code)
186 const struct ofp_action_enqueue *oae;
187 const struct ofp_action_dl_addr *oada;
188 const struct nx_action_set_tunnel64 *nast64;
189 const struct nx_action_set_tunnel *nast;
190 const struct nx_action_set_queue *nasq;
191 const struct nx_action_resubmit *nar;
192 const struct nx_action_reg_move *move;
193 const struct nx_action_reg_load *load;
194 const struct nx_action_multipath *nam;
195 const struct nx_action_autopath *naa;
196 const struct nx_action_output_reg *naor;
197 const struct nx_action_fin_timeout *naft;
198 struct mf_subfield subfield;
202 case OFPUTIL_OFPAT_OUTPUT:
203 port = ntohs(a->output.port);
204 if (port < OFPP_MAX) {
205 ds_put_format(s, "output:%"PRIu16, port);
207 ofputil_format_port(port, s);
208 if (port == OFPP_CONTROLLER) {
209 if (a->output.max_len != htons(0)) {
210 ds_put_format(s, ":%"PRIu16, ntohs(a->output.max_len));
212 ds_put_cstr(s, ":all");
218 case OFPUTIL_OFPAT_ENQUEUE:
219 oae = (const struct ofp_action_enqueue *) a;
220 ds_put_format(s, "enqueue:");
221 ofputil_format_port(ntohs(oae->port), s);
222 ds_put_format(s, "q%"PRIu32, ntohl(oae->queue_id));
225 case OFPUTIL_OFPAT_SET_VLAN_VID:
226 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
227 ntohs(a->vlan_vid.vlan_vid));
230 case OFPUTIL_OFPAT_SET_VLAN_PCP:
231 ds_put_format(s, "mod_vlan_pcp:%"PRIu8, a->vlan_pcp.vlan_pcp);
234 case OFPUTIL_OFPAT_STRIP_VLAN:
235 ds_put_cstr(s, "strip_vlan");
238 case OFPUTIL_OFPAT_SET_DL_SRC:
239 oada = (const struct ofp_action_dl_addr *) a;
240 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
241 ETH_ADDR_ARGS(oada->dl_addr));
244 case OFPUTIL_OFPAT_SET_DL_DST:
245 oada = (const struct ofp_action_dl_addr *) a;
246 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
247 ETH_ADDR_ARGS(oada->dl_addr));
250 case OFPUTIL_OFPAT_SET_NW_SRC:
251 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(&a->nw_addr.nw_addr));
254 case OFPUTIL_OFPAT_SET_NW_DST:
255 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(&a->nw_addr.nw_addr));
258 case OFPUTIL_OFPAT_SET_NW_TOS:
259 ds_put_format(s, "mod_nw_tos:%d", a->nw_tos.nw_tos);
262 case OFPUTIL_OFPAT_SET_TP_SRC:
263 ds_put_format(s, "mod_tp_src:%d", ntohs(a->tp_port.tp_port));
266 case OFPUTIL_OFPAT_SET_TP_DST:
267 ds_put_format(s, "mod_tp_dst:%d", ntohs(a->tp_port.tp_port));
270 case OFPUTIL_NXAST_RESUBMIT:
271 nar = (struct nx_action_resubmit *)a;
272 ds_put_format(s, "resubmit:");
273 ofputil_format_port(ntohs(nar->in_port), s);
276 case OFPUTIL_NXAST_RESUBMIT_TABLE:
277 nar = (struct nx_action_resubmit *)a;
278 ds_put_format(s, "resubmit(");
279 if (nar->in_port != htons(OFPP_IN_PORT)) {
280 ofputil_format_port(ntohs(nar->in_port), s);
283 if (nar->table != 255) {
284 ds_put_format(s, "%"PRIu8, nar->table);
289 case OFPUTIL_NXAST_SET_TUNNEL:
290 nast = (struct nx_action_set_tunnel *)a;
291 ds_put_format(s, "set_tunnel:%#"PRIx32, ntohl(nast->tun_id));
294 case OFPUTIL_NXAST_SET_QUEUE:
295 nasq = (struct nx_action_set_queue *)a;
296 ds_put_format(s, "set_queue:%u", ntohl(nasq->queue_id));
299 case OFPUTIL_NXAST_POP_QUEUE:
300 ds_put_cstr(s, "pop_queue");
303 case OFPUTIL_NXAST_NOTE:
304 print_note(s, (const struct nx_action_note *) a);
307 case OFPUTIL_NXAST_REG_MOVE:
308 move = (const struct nx_action_reg_move *) a;
309 nxm_format_reg_move(move, s);
312 case OFPUTIL_NXAST_REG_LOAD:
313 load = (const struct nx_action_reg_load *) a;
314 nxm_format_reg_load(load, s);
317 case OFPUTIL_NXAST_SET_TUNNEL64:
318 nast64 = (const struct nx_action_set_tunnel64 *) a;
319 ds_put_format(s, "set_tunnel64:%#"PRIx64,
320 ntohll(nast64->tun_id));
323 case OFPUTIL_NXAST_MULTIPATH:
324 nam = (const struct nx_action_multipath *) a;
325 multipath_format(nam, s);
328 case OFPUTIL_NXAST_AUTOPATH:
329 naa = (const struct nx_action_autopath *)a;
330 ds_put_format(s, "autopath(%u,", ntohl(naa->id));
331 nxm_decode(&subfield, naa->dst, naa->ofs_nbits);
332 mf_format_subfield(&subfield, s);
336 case OFPUTIL_NXAST_BUNDLE:
337 case OFPUTIL_NXAST_BUNDLE_LOAD:
338 bundle_format((const struct nx_action_bundle *) a, s);
341 case OFPUTIL_NXAST_OUTPUT_REG:
342 naor = (const struct nx_action_output_reg *) a;
343 ds_put_cstr(s, "output:");
344 nxm_decode(&subfield, naor->src, naor->ofs_nbits);
345 mf_format_subfield(&subfield, s);
348 case OFPUTIL_NXAST_LEARN:
349 learn_format((const struct nx_action_learn *) a, s);
352 case OFPUTIL_NXAST_DEC_TTL:
353 ds_put_cstr(s, "dec_ttl");
356 case OFPUTIL_NXAST_EXIT:
357 ds_put_cstr(s, "exit");
360 case OFPUTIL_NXAST_FIN_TIMEOUT:
361 naft = (const struct nx_action_fin_timeout *) a;
362 ds_put_cstr(s, "fin_timeout(");
363 if (naft->fin_idle_timeout) {
364 ds_put_format(s, "idle_timeout=%"PRIu16",",
365 ntohs(naft->fin_idle_timeout));
367 if (naft->fin_hard_timeout) {
368 ds_put_format(s, "hard_timeout=%"PRIu16",",
369 ntohs(naft->fin_hard_timeout));
381 ofp_print_actions(struct ds *string, const union ofp_action *actions,
384 const union ofp_action *a;
387 ds_put_cstr(string, "actions=");
389 ds_put_cstr(string, "drop");
392 OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
393 int code = ofputil_decode_action(a);
396 ds_put_cstr(string, ",");
398 ofp_print_action(string, a, code);
400 ofp_print_error(string, -code);
404 ds_put_format(string, " ***%zu leftover bytes following actions",
410 ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
413 struct ofputil_packet_out po;
416 error = ofputil_decode_packet_out(&po, opo);
418 ofp_print_error(string, error);
422 ds_put_cstr(string, " in_port=");
423 ofputil_format_port(po.in_port, string);
425 ds_put_char(string, ' ');
426 ofp_print_actions(string, po.actions, po.n_actions);
428 if (po.buffer_id == UINT32_MAX) {
429 ds_put_format(string, " data_len=%zu", po.packet_len);
430 if (verbosity > 0 && po.packet_len > 0) {
431 char *packet = ofp_packet_to_string(po.packet, po.packet_len);
432 ds_put_char(string, '\n');
433 ds_put_cstr(string, packet);
437 ds_put_format(string, " buffer=0x%08"PRIx32, po.buffer_id);
439 ds_put_char(string, '\n');
442 /* qsort comparison function. */
444 compare_ports(const void *a_, const void *b_)
446 const struct ofp_phy_port *a = a_;
447 const struct ofp_phy_port *b = b_;
448 uint16_t ap = ntohs(a->port_no);
449 uint16_t bp = ntohs(b->port_no);
451 return ap < bp ? -1 : ap > bp;
460 ofp_print_bit_names(struct ds *string, uint32_t bits,
461 const struct bit_name bit_names[])
466 ds_put_cstr(string, "0");
470 for (; bits && bit_names->name; bit_names++) {
471 if (bits & bit_names->bit) {
473 ds_put_char(string, ' ');
475 ds_put_cstr(string, bit_names->name);
476 bits &= ~bit_names->bit;
482 ds_put_char(string, ' ');
484 ds_put_format(string, "0x%"PRIx32, bits);
489 ofp_print_port_features(struct ds *string, uint32_t features)
491 static const struct bit_name feature_bits[] = {
492 { OFPPF_10MB_HD, "10MB-HD" },
493 { OFPPF_10MB_FD, "10MB-FD" },
494 { OFPPF_100MB_HD, "100MB-HD" },
495 { OFPPF_100MB_FD, "100MB-FD" },
496 { OFPPF_1GB_HD, "1GB-HD" },
497 { OFPPF_1GB_FD, "1GB-FD" },
498 { OFPPF_10GB_FD, "10GB-FD" },
499 { OFPPF_COPPER, "COPPER" },
500 { OFPPF_FIBER, "FIBER" },
501 { OFPPF_AUTONEG, "AUTO_NEG" },
502 { OFPPF_PAUSE, "AUTO_PAUSE" },
503 { OFPPF_PAUSE_ASYM, "AUTO_PAUSE_ASYM" },
507 ofp_print_bit_names(string, features, feature_bits);
508 ds_put_char(string, '\n');
512 ofp_print_port_config(struct ds *string, uint32_t config)
514 static const struct bit_name config_bits[] = {
515 { OFPPC_PORT_DOWN, "PORT_DOWN" },
516 { OFPPC_NO_STP, "NO_STP" },
517 { OFPPC_NO_RECV, "NO_RECV" },
518 { OFPPC_NO_RECV_STP, "NO_RECV_STP" },
519 { OFPPC_NO_FLOOD, "NO_FLOOD" },
520 { OFPPC_NO_FWD, "NO_FWD" },
521 { OFPPC_NO_PACKET_IN, "NO_PACKET_IN" },
525 ofp_print_bit_names(string, config, config_bits);
526 ds_put_char(string, '\n');
530 ofp_print_port_state(struct ds *string, uint32_t state)
532 static const struct bit_name state_bits[] = {
533 { OFPPS_LINK_DOWN, "LINK_DOWN" },
538 /* The STP state is a 2-bit field so it doesn't fit in with the bitmask
539 * pattern. We have to special case it.
541 * OVS doesn't support STP, so this field will always be 0 if we are
542 * talking to OVS, so we'd always print STP_LISTEN in that case.
543 * Therefore, we don't print anything at all if the value is STP_LISTEN, to
544 * avoid confusing users. */
545 stp_state = state & OFPPS_STP_MASK;
547 ds_put_cstr(string, (stp_state == OFPPS_STP_LEARN ? "STP_LEARN"
548 : stp_state == OFPPS_STP_FORWARD ? "STP_FORWARD"
550 state &= ~OFPPS_STP_MASK;
552 ofp_print_bit_names(string, state, state_bits);
555 ofp_print_bit_names(string, state, state_bits);
557 ds_put_char(string, '\n');
561 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
563 char name[OFP_MAX_PORT_NAME_LEN];
566 memcpy(name, port->name, sizeof name);
567 for (j = 0; j < sizeof name - 1; j++) {
568 if (!isprint((unsigned char) name[j])) {
574 ds_put_char(string, ' ');
575 ofputil_format_port(ntohs(port->port_no), string);
576 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT"\n",
577 name, ETH_ADDR_ARGS(port->hw_addr));
579 ds_put_cstr(string, " config: ");
580 ofp_print_port_config(string, ntohl(port->config));
582 ds_put_cstr(string, " state: ");
583 ofp_print_port_state(string, ntohl(port->state));
586 ds_put_format(string, " current: ");
587 ofp_print_port_features(string, ntohl(port->curr));
589 if (port->advertised) {
590 ds_put_format(string, " advertised: ");
591 ofp_print_port_features(string, ntohl(port->advertised));
593 if (port->supported) {
594 ds_put_format(string, " supported: ");
595 ofp_print_port_features(string, ntohl(port->supported));
598 ds_put_format(string, " peer: ");
599 ofp_print_port_features(string, ntohl(port->peer));
604 ofp_print_switch_features(struct ds *string,
605 const struct ofp_switch_features *osf)
607 size_t len = ntohs(osf->header.length);
608 struct ofp_phy_port *port_list;
612 ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
613 osf->header.version, ntohll(osf->datapath_id));
614 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
615 ntohl(osf->n_buffers));
616 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
617 ntohl(osf->capabilities), ntohl(osf->actions));
619 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
621 port_list = xmemdup(osf->ports, len - sizeof *osf);
622 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
623 for (i = 0; i < n_ports; i++) {
624 ofp_print_phy_port(string, &port_list[i]);
630 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
632 enum ofp_config_flags flags;
634 flags = ntohs(osc->flags);
636 ds_put_format(string, " frags=%s", ofputil_frag_handling_to_string(flags));
637 flags &= ~OFPC_FRAG_MASK;
639 if (flags & OFPC_INVALID_TTL_TO_CONTROLLER) {
640 ds_put_format(string, " invalid_ttl_to_controller");
641 flags &= ~OFPC_INVALID_TTL_TO_CONTROLLER;
645 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
648 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
651 static void print_wild(struct ds *string, const char *leader, int is_wild,
652 int verbosity, const char *format, ...)
653 __attribute__((format(printf, 5, 6)));
655 static void print_wild(struct ds *string, const char *leader, int is_wild,
656 int verbosity, const char *format, ...)
658 if (is_wild && verbosity < 2) {
661 ds_put_cstr(string, leader);
665 va_start(args, format);
666 ds_put_format_valist(string, format, args);
669 ds_put_char(string, '*');
671 ds_put_char(string, ',');
675 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
676 uint32_t wild_bits, int verbosity)
678 if (wild_bits >= 32 && verbosity < 2) {
681 ds_put_cstr(string, leader);
682 if (wild_bits < 32) {
683 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
685 ds_put_format(string, "/%d", 32 - wild_bits);
688 ds_put_char(string, '*');
690 ds_put_char(string, ',');
694 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
696 char *s = ofp_match_to_string(om, verbosity);
702 ofp_match_to_string(const struct ofp_match *om, int verbosity)
704 struct ds f = DS_EMPTY_INITIALIZER;
705 uint32_t w = ntohl(om->wildcards);
706 bool skip_type = false;
707 bool skip_proto = false;
709 if (!(w & OFPFW_DL_TYPE)) {
711 if (om->dl_type == htons(ETH_TYPE_IP)) {
712 if (!(w & OFPFW_NW_PROTO)) {
714 if (om->nw_proto == IPPROTO_ICMP) {
715 ds_put_cstr(&f, "icmp,");
716 } else if (om->nw_proto == IPPROTO_TCP) {
717 ds_put_cstr(&f, "tcp,");
718 } else if (om->nw_proto == IPPROTO_UDP) {
719 ds_put_cstr(&f, "udp,");
721 ds_put_cstr(&f, "ip,");
725 ds_put_cstr(&f, "ip,");
727 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
728 ds_put_cstr(&f, "arp,");
733 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
734 "%d", ntohs(om->in_port));
735 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
736 "%d", ntohs(om->dl_vlan));
737 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
738 "%d", om->dl_vlan_pcp);
739 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
740 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
741 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
742 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
744 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
745 "0x%04x", ntohs(om->dl_type));
747 print_ip_netmask(&f, "nw_src=", om->nw_src,
748 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
749 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
750 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
752 if (om->dl_type == htons(ETH_TYPE_ARP)) {
753 print_wild(&f, "arp_op=", w & OFPFW_NW_PROTO, verbosity,
756 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
760 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
762 if (om->nw_proto == IPPROTO_ICMP) {
763 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
764 "%d", ntohs(om->tp_src));
765 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
766 "%d", ntohs(om->tp_dst));
768 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
769 "%d", ntohs(om->tp_src));
770 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
771 "%d", ntohs(om->tp_dst));
773 if (ds_last(&f) == ',') {
780 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
781 enum ofputil_msg_code code, int verbosity)
783 struct ofputil_flow_mod fm;
787 error = ofputil_decode_flow_mod(&fm, oh, true);
789 ofp_print_error(s, error);
794 switch (fm.command) {
796 ds_put_cstr(s, "ADD");
799 ds_put_cstr(s, "MOD");
801 case OFPFC_MODIFY_STRICT:
802 ds_put_cstr(s, "MOD_STRICT");
805 ds_put_cstr(s, "DEL");
807 case OFPFC_DELETE_STRICT:
808 ds_put_cstr(s, "DEL_STRICT");
811 ds_put_format(s, "cmd:%d", fm.command);
813 if (fm.table_id != 0) {
814 ds_put_format(s, " table:%d", fm.table_id);
818 if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
819 const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
820 ofp_print_match(s, &ofm->match, verbosity);
822 /* ofp_print_match() doesn't print priority. */
823 need_priority = true;
824 } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
825 const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
826 const void *nxm = nfm + 1;
829 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
830 ds_put_cstr(s, nxm_s);
833 /* nx_match_to_string() doesn't print priority. */
834 need_priority = true;
836 cls_rule_format(&fm.cr, s);
838 /* cls_rule_format() does print priority. */
839 need_priority = false;
842 if (ds_last(s) != ' ') {
845 if (fm.cookie != htonll(0)) {
846 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.cookie));
848 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
849 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
851 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
852 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
854 if (fm.cr.priority != OFP_DEFAULT_PRIORITY && need_priority) {
855 ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
857 if (fm.buffer_id != UINT32_MAX) {
858 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
861 uint16_t flags = fm.flags;
863 if (flags & OFPFF_SEND_FLOW_REM) {
864 ds_put_cstr(s, "send_flow_rem ");
866 if (flags & OFPFF_CHECK_OVERLAP) {
867 ds_put_cstr(s, "check_overlap ");
869 if (flags & OFPFF_EMERG) {
870 ds_put_cstr(s, "emerg ");
873 flags &= ~(OFPFF_SEND_FLOW_REM | OFPFF_CHECK_OVERLAP | OFPFF_EMERG);
875 ds_put_format(s, "flags:0x%"PRIx16" ", flags);
879 ofp_print_actions(s, fm.actions, fm.n_actions);
883 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
885 ds_put_format(string, "%u", sec);
887 ds_put_format(string, ".%09u", nsec);
888 while (string->string[string->length - 1] == '0') {
892 ds_put_char(string, 's');
896 ofp_flow_removed_reason_to_string(enum ofp_flow_removed_reason reason)
901 case OFPRR_IDLE_TIMEOUT:
903 case OFPRR_HARD_TIMEOUT:
908 sprintf(s, "%d", (int) reason);
914 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
916 struct ofputil_flow_removed fr;
919 error = ofputil_decode_flow_removed(&fr, oh);
921 ofp_print_error(string, error);
925 ds_put_char(string, ' ');
926 cls_rule_format(&fr.rule, string);
928 ds_put_format(string, " reason=%s",
929 ofp_flow_removed_reason_to_string(fr.reason));
931 if (fr.cookie != htonll(0)) {
932 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
934 ds_put_cstr(string, " duration");
935 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
936 ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
937 fr.idle_timeout, fr.packet_count, fr.byte_count);
941 ofp_print_port_mod(struct ds *string, const struct ofp_port_mod *opm)
943 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
944 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
945 ntohl(opm->config), ntohl(opm->mask));
946 ds_put_format(string, " advertise: ");
947 if (opm->advertise) {
948 ofp_print_port_features(string, ntohl(opm->advertise));
950 ds_put_format(string, "UNCHANGED\n");
955 ofp_print_error(struct ds *string, enum ofperr error)
957 if (string->length) {
958 ds_put_char(string, ' ');
960 ds_put_format(string, "***decode error: %s***\n", ofperr_get_name(error));
964 ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
966 size_t len = ntohs(oem->header.length);
967 size_t payload_ofs, payload_len;
972 error = ofperr_decode_msg(&oem->header, &payload_ofs);
974 ds_put_cstr(string, "***decode error***");
975 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
979 ds_put_format(string, " %s\n", ofperr_get_name(error));
981 payload = (const uint8_t *) oem + payload_ofs;
982 payload_len = len - payload_ofs;
983 if (error == OFPERR_OFPHFC_INCOMPATIBLE || error == OFPERR_OFPHFC_EPERM) {
984 ds_put_printable(string, payload, payload_len);
986 s = ofp_to_string(payload, payload_len, 1);
987 ds_put_cstr(string, s);
993 ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
995 if (ops->reason == OFPPR_ADD) {
996 ds_put_format(string, " ADD:");
997 } else if (ops->reason == OFPPR_DELETE) {
998 ds_put_format(string, " DEL:");
999 } else if (ops->reason == OFPPR_MODIFY) {
1000 ds_put_format(string, " MOD:");
1003 ofp_print_phy_port(string, &ops->desc);
1007 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_desc_stats *ods)
1009 ds_put_char(string, '\n');
1010 ds_put_format(string, "Manufacturer: %.*s\n",
1011 (int) sizeof ods->mfr_desc, ods->mfr_desc);
1012 ds_put_format(string, "Hardware: %.*s\n",
1013 (int) sizeof ods->hw_desc, ods->hw_desc);
1014 ds_put_format(string, "Software: %.*s\n",
1015 (int) sizeof ods->sw_desc, ods->sw_desc);
1016 ds_put_format(string, "Serial Num: %.*s\n",
1017 (int) sizeof ods->serial_num, ods->serial_num);
1018 ds_put_format(string, "DP Description: %.*s\n",
1019 (int) sizeof ods->dp_desc, ods->dp_desc);
1023 ofp_print_flow_stats_request(struct ds *string,
1024 const struct ofp_stats_msg *osm)
1026 struct ofputil_flow_stats_request fsr;
1029 error = ofputil_decode_flow_stats_request(&fsr, &osm->header);
1031 ofp_print_error(string, error);
1035 if (fsr.table_id != 0xff) {
1036 ds_put_format(string, " table=%"PRIu8, fsr.table_id);
1039 if (fsr.out_port != OFPP_NONE) {
1040 ds_put_cstr(string, " out_port=");
1041 ofputil_format_port(fsr.out_port, string);
1044 /* A flow stats request doesn't include a priority, but cls_rule_format()
1045 * will print one unless it is OFP_DEFAULT_PRIORITY. */
1046 fsr.match.priority = OFP_DEFAULT_PRIORITY;
1048 ds_put_char(string, ' ');
1049 cls_rule_format(&fsr.match, string);
1053 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
1057 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1059 struct ofputil_flow_stats fs;
1062 retval = ofputil_decode_flow_stats_reply(&fs, &b, true);
1064 if (retval != EOF) {
1065 ds_put_cstr(string, " ***parse error***");
1070 ds_put_char(string, '\n');
1072 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
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);
1081 if (fs.hard_timeout != OFP_FLOW_PERMANENT) {
1082 ds_put_format(string, "hard_timeout=%"PRIu16",", fs.hard_timeout);
1084 if (fs.idle_age >= 0) {
1085 ds_put_format(string, "idle_age=%d,", fs.idle_age);
1087 if (fs.hard_age >= 0 && fs.hard_age != fs.duration_sec) {
1088 ds_put_format(string, "hard_age=%d,", fs.hard_age);
1091 cls_rule_format(&fs.rule, string);
1092 if (string->string[string->length - 1] != ' ') {
1093 ds_put_char(string, ' ');
1095 ofp_print_actions(string, fs.actions, fs.n_actions);
1100 ofp_print_ofpst_aggregate_reply(struct ds *string,
1101 const struct ofp_aggregate_stats_reply *asr)
1103 ds_put_format(string, " packet_count=%"PRIu64,
1104 ntohll(get_32aligned_be64(&asr->packet_count)));
1105 ds_put_format(string, " byte_count=%"PRIu64,
1106 ntohll(get_32aligned_be64(&asr->byte_count)));
1107 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1111 ofp_print_nxst_aggregate_reply(struct ds *string,
1112 const struct nx_aggregate_stats_reply *nasr)
1114 ds_put_format(string, " packet_count=%"PRIu64, ntohll(nasr->packet_count));
1115 ds_put_format(string, " byte_count=%"PRIu64, ntohll(nasr->byte_count));
1116 ds_put_format(string, " flow_count=%"PRIu32, ntohl(nasr->flow_count));
1119 static void print_port_stat(struct ds *string, const char *leader,
1120 const ovs_32aligned_be64 *statp, int more)
1122 uint64_t stat = ntohll(get_32aligned_be64(statp));
1124 ds_put_cstr(string, leader);
1125 if (stat != UINT64_MAX) {
1126 ds_put_format(string, "%"PRIu64, stat);
1128 ds_put_char(string, '?');
1131 ds_put_cstr(string, ", ");
1133 ds_put_cstr(string, "\n");
1138 ofp_print_ofpst_port_request(struct ds *string,
1139 const struct ofp_port_stats_request *psr)
1141 ds_put_format(string, " port_no=%"PRIu16, ntohs(psr->port_no));
1145 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1148 const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1149 size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
1150 ds_put_format(string, " %zu ports\n", n);
1151 if (verbosity < 1) {
1156 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1158 ds_put_cstr(string, "rx ");
1159 print_port_stat(string, "pkts=", &ps->rx_packets, 1);
1160 print_port_stat(string, "bytes=", &ps->rx_bytes, 1);
1161 print_port_stat(string, "drop=", &ps->rx_dropped, 1);
1162 print_port_stat(string, "errs=", &ps->rx_errors, 1);
1163 print_port_stat(string, "frame=", &ps->rx_frame_err, 1);
1164 print_port_stat(string, "over=", &ps->rx_over_err, 1);
1165 print_port_stat(string, "crc=", &ps->rx_crc_err, 0);
1167 ds_put_cstr(string, " tx ");
1168 print_port_stat(string, "pkts=", &ps->tx_packets, 1);
1169 print_port_stat(string, "bytes=", &ps->tx_bytes, 1);
1170 print_port_stat(string, "drop=", &ps->tx_dropped, 1);
1171 print_port_stat(string, "errs=", &ps->tx_errors, 1);
1172 print_port_stat(string, "coll=", &ps->collisions, 0);
1177 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1180 const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1181 size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
1182 ds_put_format(string, " %zu tables\n", n);
1183 if (verbosity < 1) {
1188 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1189 ovs_strlcpy(name, ts->name, sizeof name);
1191 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1192 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1193 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1194 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1195 ds_put_cstr(string, " ");
1196 ds_put_format(string, "lookup=%"PRIu64", ",
1197 ntohll(get_32aligned_be64(&ts->lookup_count)));
1198 ds_put_format(string, "matched=%"PRIu64"\n",
1199 ntohll(get_32aligned_be64(&ts->matched_count)));
1204 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1206 if (queue_id == OFPQ_ALL) {
1207 ds_put_cstr(string, "ALL");
1209 ds_put_format(string, "%"PRIu32, queue_id);
1214 ofp_print_ofpst_queue_request(struct ds *string,
1215 const struct ofp_queue_stats_request *qsr)
1217 ds_put_cstr(string, "port=");
1218 ofputil_format_port(ntohs(qsr->port_no), string);
1220 ds_put_cstr(string, " queue=");
1221 ofp_print_queue_name(string, ntohl(qsr->queue_id));
1225 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1228 const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1229 size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
1230 ds_put_format(string, " %zu queues\n", n);
1231 if (verbosity < 1) {
1236 ds_put_cstr(string, " port ");
1237 ofputil_format_port(ntohs(qs->port_no), string);
1238 ds_put_cstr(string, " queue ");
1239 ofp_print_queue_name(string, ntohl(qs->queue_id));
1240 ds_put_cstr(string, ": ");
1242 print_port_stat(string, "bytes=", &qs->tx_bytes, 1);
1243 print_port_stat(string, "pkts=", &qs->tx_packets, 1);
1244 print_port_stat(string, "errors=", &qs->tx_errors, 0);
1249 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1251 const struct ofp_stats_msg *srq = (const struct ofp_stats_msg *) oh;
1254 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1260 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1262 const struct ofp_stats_msg *srp = (const struct ofp_stats_msg *) oh;
1265 uint16_t flags = ntohs(srp->flags);
1267 ds_put_cstr(string, " flags=");
1268 if (flags & OFPSF_REPLY_MORE) {
1269 ds_put_cstr(string, "[more]");
1270 flags &= ~OFPSF_REPLY_MORE;
1273 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1280 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1282 size_t len = ntohs(oh->length);
1284 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1285 if (verbosity > 1) {
1286 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1291 ofp_print_nxt_role_message(struct ds *string,
1292 const struct nx_role_request *nrr)
1294 unsigned int role = ntohl(nrr->role);
1296 ds_put_cstr(string, " role=");
1297 if (role == NX_ROLE_OTHER) {
1298 ds_put_cstr(string, "other");
1299 } else if (role == NX_ROLE_MASTER) {
1300 ds_put_cstr(string, "master");
1301 } else if (role == NX_ROLE_SLAVE) {
1302 ds_put_cstr(string, "slave");
1304 ds_put_format(string, "%u", role);
1309 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1310 const struct nx_flow_mod_table_id *nfmti)
1312 ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1316 ofp_print_nxt_set_flow_format(struct ds *string,
1317 const struct nx_set_flow_format *nsff)
1319 uint32_t format = ntohl(nsff->format);
1321 ds_put_cstr(string, " format=");
1322 if (ofputil_flow_format_is_valid(format)) {
1323 ds_put_cstr(string, ofputil_flow_format_to_string(format));
1325 ds_put_format(string, "%"PRIu32, format);
1330 ofp_print_nxt_set_packet_in_format(struct ds *string,
1331 const struct nx_set_packet_in_format *nspf)
1333 uint32_t format = ntohl(nspf->format);
1335 ds_put_cstr(string, " format=");
1336 if (ofputil_packet_in_format_is_valid(format)) {
1337 ds_put_cstr(string, ofputil_packet_in_format_to_string(format));
1339 ds_put_format(string, "%"PRIu32, format);
1344 ofp_port_reason_to_string(enum ofp_port_reason reason)
1359 sprintf(s, "%d", (int) reason);
1365 ofp_print_nxt_set_async_config(struct ds *string,
1366 const struct nx_async_config *nac)
1370 for (i = 0; i < 2; i++) {
1373 ds_put_format(string, "\n %s:\n", i == 0 ? "master" : "slave");
1375 ds_put_cstr(string, " PACKET_IN:");
1376 for (j = 0; j < 32; j++) {
1377 if (nac->packet_in_mask[i] & htonl(1u << j)) {
1378 ds_put_format(string, " %s",
1379 ofp_packet_in_reason_to_string(j));
1382 if (!nac->packet_in_mask[i]) {
1383 ds_put_cstr(string, " (off)");
1385 ds_put_char(string, '\n');
1387 ds_put_cstr(string, " PORT_STATUS:");
1388 for (j = 0; j < 32; j++) {
1389 if (nac->port_status_mask[i] & htonl(1u << j)) {
1390 ds_put_format(string, " %s", ofp_port_reason_to_string(j));
1393 if (!nac->port_status_mask[i]) {
1394 ds_put_cstr(string, " (off)");
1396 ds_put_char(string, '\n');
1398 ds_put_cstr(string, " FLOW_REMOVED:");
1399 for (j = 0; j < 32; j++) {
1400 if (nac->flow_removed_mask[i] & htonl(1u << j)) {
1401 ds_put_format(string, " %s",
1402 ofp_flow_removed_reason_to_string(j));
1405 if (!nac->flow_removed_mask[i]) {
1406 ds_put_cstr(string, " (off)");
1408 ds_put_char(string, '\n');
1413 ofp_to_string__(const struct ofp_header *oh,
1414 const struct ofputil_msg_type *type, struct ds *string,
1417 enum ofputil_msg_code code;
1418 const void *msg = oh;
1420 ds_put_format(string, "%s (xid=0x%"PRIx32"):",
1421 ofputil_msg_type_name(type), ntohl(oh->xid));
1423 code = ofputil_msg_type_code(type);
1425 case OFPUTIL_MSG_INVALID:
1428 case OFPUTIL_OFPT_HELLO:
1429 ds_put_char(string, '\n');
1430 ds_put_hex_dump(string, oh + 1, ntohs(oh->length) - sizeof *oh,
1434 case OFPUTIL_OFPT_ERROR:
1435 ofp_print_error_msg(string, msg);
1438 case OFPUTIL_OFPT_ECHO_REQUEST:
1439 case OFPUTIL_OFPT_ECHO_REPLY:
1440 ofp_print_echo(string, oh, verbosity);
1443 case OFPUTIL_OFPT_FEATURES_REQUEST:
1446 case OFPUTIL_OFPT_FEATURES_REPLY:
1447 ofp_print_switch_features(string, msg);
1450 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1453 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1454 case OFPUTIL_OFPT_SET_CONFIG:
1455 ofp_print_switch_config(string, msg);
1458 case OFPUTIL_OFPT_PACKET_IN:
1459 case OFPUTIL_NXT_PACKET_IN:
1460 ofp_print_packet_in(string, msg, verbosity);
1463 case OFPUTIL_OFPT_FLOW_REMOVED:
1464 case OFPUTIL_NXT_FLOW_REMOVED:
1465 ofp_print_flow_removed(string, msg);
1468 case OFPUTIL_OFPT_PORT_STATUS:
1469 ofp_print_port_status(string, msg);
1472 case OFPUTIL_OFPT_PACKET_OUT:
1473 ofp_print_packet_out(string, msg, verbosity);
1476 case OFPUTIL_OFPT_FLOW_MOD:
1477 case OFPUTIL_NXT_FLOW_MOD:
1478 ofp_print_flow_mod(string, msg, code, verbosity);
1481 case OFPUTIL_OFPT_PORT_MOD:
1482 ofp_print_port_mod(string, msg);
1485 case OFPUTIL_OFPT_BARRIER_REQUEST:
1486 case OFPUTIL_OFPT_BARRIER_REPLY:
1489 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1490 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1494 case OFPUTIL_OFPST_DESC_REQUEST:
1495 ofp_print_stats_request(string, oh);
1498 case OFPUTIL_OFPST_FLOW_REQUEST:
1499 case OFPUTIL_NXST_FLOW_REQUEST:
1500 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1501 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1502 ofp_print_stats_request(string, oh);
1503 ofp_print_flow_stats_request(string, msg);
1506 case OFPUTIL_OFPST_TABLE_REQUEST:
1507 ofp_print_stats_request(string, oh);
1510 case OFPUTIL_OFPST_PORT_REQUEST:
1511 ofp_print_stats_request(string, oh);
1512 ofp_print_ofpst_port_request(string, msg);
1515 case OFPUTIL_OFPST_QUEUE_REQUEST:
1516 ofp_print_stats_request(string, oh);
1517 ofp_print_ofpst_queue_request(string, msg);
1520 case OFPUTIL_OFPST_DESC_REPLY:
1521 ofp_print_stats_reply(string, oh);
1522 ofp_print_ofpst_desc_reply(string, msg);
1525 case OFPUTIL_OFPST_FLOW_REPLY:
1526 case OFPUTIL_NXST_FLOW_REPLY:
1527 ofp_print_stats_reply(string, oh);
1528 ofp_print_flow_stats_reply(string, oh);
1531 case OFPUTIL_OFPST_QUEUE_REPLY:
1532 ofp_print_stats_reply(string, oh);
1533 ofp_print_ofpst_queue_reply(string, oh, verbosity);
1536 case OFPUTIL_OFPST_PORT_REPLY:
1537 ofp_print_stats_reply(string, oh);
1538 ofp_print_ofpst_port_reply(string, oh, verbosity);
1541 case OFPUTIL_OFPST_TABLE_REPLY:
1542 ofp_print_stats_reply(string, oh);
1543 ofp_print_ofpst_table_reply(string, oh, verbosity);
1546 case OFPUTIL_OFPST_AGGREGATE_REPLY:
1547 ofp_print_stats_reply(string, oh);
1548 ofp_print_ofpst_aggregate_reply(string, msg);
1551 case OFPUTIL_NXT_ROLE_REQUEST:
1552 case OFPUTIL_NXT_ROLE_REPLY:
1553 ofp_print_nxt_role_message(string, msg);
1556 case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
1557 ofp_print_nxt_flow_mod_table_id(string, msg);
1560 case OFPUTIL_NXT_SET_FLOW_FORMAT:
1561 ofp_print_nxt_set_flow_format(string, msg);
1564 case OFPUTIL_NXT_SET_PACKET_IN_FORMAT:
1565 ofp_print_nxt_set_packet_in_format(string, msg);
1568 case OFPUTIL_NXT_FLOW_AGE:
1571 case OFPUTIL_NXT_SET_ASYNC_CONFIG:
1572 ofp_print_nxt_set_async_config(string, msg);
1575 case OFPUTIL_NXST_AGGREGATE_REPLY:
1576 ofp_print_stats_reply(string, oh);
1577 ofp_print_nxst_aggregate_reply(string, msg);
1582 /* Composes and returns a string representing the OpenFlow packet of 'len'
1583 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1584 * verbosity and higher numbers increase verbosity. The caller is responsible
1585 * for freeing the string. */
1587 ofp_to_string(const void *oh_, size_t len, int verbosity)
1589 struct ds string = DS_EMPTY_INITIALIZER;
1590 const struct ofp_header *oh = oh_;
1593 ds_put_cstr(&string, "OpenFlow message is empty\n");
1594 } else if (len < sizeof(struct ofp_header)) {
1595 ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
1597 } else if (ntohs(oh->length) > len) {
1598 ds_put_format(&string,
1599 "(***truncated to %zu bytes from %"PRIu16"***)\n",
1600 len, ntohs(oh->length));
1601 } else if (ntohs(oh->length) < len) {
1602 ds_put_format(&string,
1603 "(***only uses %"PRIu16" bytes out of %zu***)\n",
1604 ntohs(oh->length), len);
1606 const struct ofputil_msg_type *type;
1609 error = ofputil_decode_msg_type(oh, &type);
1611 ofp_to_string__(oh, type, &string, verbosity);
1612 if (verbosity >= 5) {
1613 if (ds_last(&string) != '\n') {
1614 ds_put_char(&string, '\n');
1616 ds_put_hex_dump(&string, oh, len, 0, true);
1618 if (ds_last(&string) != '\n') {
1619 ds_put_char(&string, '\n');
1621 return ds_steal_cstr(&string);
1624 ofp_print_error(&string, error);
1626 ds_put_hex_dump(&string, oh, len, 0, true);
1627 return ds_steal_cstr(&string);
1630 /* Returns the name for the specified OpenFlow message type as a string,
1631 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1632 * hex number, e.g. "0x55".
1634 * The caller must free the returned string when it is no longer needed. */
1636 ofp_message_type_to_string(uint8_t type)
1647 case OFPT_ECHO_REQUEST:
1648 name = "ECHO_REQUEST";
1650 case OFPT_ECHO_REPLY:
1651 name = "ECHO_REPLY";
1656 case OFPT_FEATURES_REQUEST:
1657 name = "FEATURES_REQUEST";
1659 case OFPT_FEATURES_REPLY:
1660 name = "FEATURES_REPLY";
1662 case OFPT_GET_CONFIG_REQUEST:
1663 name = "GET_CONFIG_REQUEST";
1665 case OFPT_GET_CONFIG_REPLY:
1666 name = "GET_CONFIG_REPLY";
1668 case OFPT_SET_CONFIG:
1669 name = "SET_CONFIG";
1671 case OFPT_PACKET_IN:
1674 case OFPT_FLOW_REMOVED:
1675 name = "FLOW_REMOVED";
1677 case OFPT_PORT_STATUS:
1678 name = "PORT_STATUS";
1680 case OFPT_PACKET_OUT:
1681 name = "PACKET_OUT";
1689 case OFPT_STATS_REQUEST:
1690 name = "STATS_REQUEST";
1692 case OFPT_STATS_REPLY:
1693 name = "STATS_REPLY";
1695 case OFPT_BARRIER_REQUEST:
1696 name = "BARRIER_REQUEST";
1698 case OFPT_BARRIER_REPLY:
1699 name = "BARRIER_REPLY";
1701 case OFPT_QUEUE_GET_CONFIG_REQUEST:
1702 name = "QUEUE_GET_CONFIG_REQUEST";
1704 case OFPT_QUEUE_GET_CONFIG_REPLY:
1705 name = "QUEUE_GET_CONFIG_REPLY";
1712 return name ? xasprintf("OFPT_%s", name) : xasprintf("0x%02"PRIx8, type);
1716 print_and_free(FILE *stream, char *string)
1718 fputs(string, stream);
1722 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1723 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1724 * numbers increase verbosity. */
1726 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1728 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1731 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1732 * 'data' to 'stream'. */
1734 ofp_print_packet(FILE *stream, const void *data, size_t len)
1736 print_and_free(stream, ofp_packet_to_string(data, len));