2 * Copyright (c) 2008, 2009, 2010 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 "dynamic-string.h"
33 #include "openflow/openflow.h"
34 #include "openflow/nicira-ext.h"
40 static void ofp_print_port_name(struct ds *string, uint16_t port);
41 static void ofp_print_queue_name(struct ds *string, uint32_t port);
43 /* Returns a string that represents the contents of the Ethernet frame in the
44 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
45 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
46 * bytes were captured).
48 * The caller must free the returned string.
50 * This starts and kills a tcpdump subprocess so it's quite expensive. */
52 ofp_packet_to_string(const void *data, size_t len, size_t total_len OVS_UNUSED)
54 struct ds ds = DS_EMPTY_INITIALIZER;
63 buf.data = (void *) data;
68 ovs_error(errno, "tmpfile");
69 return xstrdup("<error>");
71 pcap_write_header(pcap);
72 pcap_write(pcap, &buf);
75 ovs_error(errno, "error writing temporary file");
79 snprintf(command, sizeof command, "/usr/sbin/tcpdump -e -n -r /dev/fd/%d 2>/dev/null",
81 tcpdump = popen(command, "r");
84 ovs_error(errno, "exec(\"%s\")", command);
85 return xstrdup("<error>");
88 while ((c = getc(tcpdump)) != EOF) {
92 status = pclose(tcpdump);
93 if (WIFEXITED(status)) {
94 if (WEXITSTATUS(status))
95 ovs_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
96 } else if (WIFSIGNALED(status)) {
97 ovs_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
102 /* Pretty-print the OFPT_PACKET_IN packet of 'len' bytes at 'oh' to 'stream'
103 * at the given 'verbosity' level. */
105 ofp_packet_in(struct ds *string, const void *oh, size_t len, int verbosity)
107 const struct ofp_packet_in *op = oh;
110 ds_put_format(string, " total_len=%"PRIu16" in_port=",
111 ntohs(op->total_len));
112 ofp_print_port_name(string, ntohs(op->in_port));
114 if (op->reason == OFPR_ACTION)
115 ds_put_cstr(string, " (via action)");
116 else if (op->reason != OFPR_NO_MATCH)
117 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
119 data_len = len - offsetof(struct ofp_packet_in, data);
120 ds_put_format(string, " data_len=%zu", data_len);
121 if (htonl(op->buffer_id) == UINT32_MAX) {
122 ds_put_format(string, " (unbuffered)");
123 if (ntohs(op->total_len) != data_len)
124 ds_put_format(string, " (***total_len != data_len***)");
126 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
127 if (ntohs(op->total_len) < data_len)
128 ds_put_format(string, " (***total_len < data_len***)");
130 ds_put_char(string, '\n');
134 struct ofpbuf packet;
135 struct ofp_match match;
136 packet.data = (void *) op->data;
137 packet.size = data_len;
138 flow_extract(&packet, 0, ntohs(op->in_port), &flow);
139 flow_to_match(&flow, false, &match);
140 ofp_print_match(string, &match, verbosity);
141 ds_put_char(string, '\n');
144 char *packet = ofp_packet_to_string(op->data, data_len,
145 ntohs(op->total_len));
146 ds_put_cstr(string, packet);
151 static void ofp_print_port_name(struct ds *string, uint16_t port)
170 case OFPP_CONTROLLER:
180 ds_put_format(string, "%"PRIu16, port);
183 ds_put_cstr(string, name);
187 ofp_print_nx_action(struct ds *string, const struct nx_action_header *nah)
189 switch (ntohs(nah->subtype)) {
190 case NXAST_RESUBMIT: {
191 const struct nx_action_resubmit *nar = (struct nx_action_resubmit *)nah;
192 ds_put_format(string, "resubmit:");
193 ofp_print_port_name(string, ntohs(nar->in_port));
197 case NXAST_SET_TUNNEL: {
198 const struct nx_action_set_tunnel *nast =
199 (struct nx_action_set_tunnel *)nah;
200 ds_put_format(string, "set_tunnel:0x%08"PRIx32, ntohl(nast->tun_id));
204 case NXAST_DROP_SPOOFED_ARP:
205 ds_put_cstr(string, "drop_spoofed_arp");
208 case NXAST_SET_QUEUE: {
209 const struct nx_action_set_queue *nasq =
210 (struct nx_action_set_queue *)nah;
211 ds_put_format(string, "set_queue:%u", ntohl(nasq->queue_id));
215 case NXAST_POP_QUEUE:
216 ds_put_cstr(string, "pop_queue");
220 ds_put_format(string, "***unknown Nicira action:%d***",
221 ntohs(nah->subtype));
226 ofp_print_action(struct ds *string, const struct ofp_action_header *ah,
232 struct openflow_action {
237 const struct openflow_action of_actions[] = {
239 sizeof(struct ofp_action_output),
240 sizeof(struct ofp_action_output),
242 [OFPAT_SET_VLAN_VID] = {
243 sizeof(struct ofp_action_vlan_vid),
244 sizeof(struct ofp_action_vlan_vid),
246 [OFPAT_SET_VLAN_PCP] = {
247 sizeof(struct ofp_action_vlan_pcp),
248 sizeof(struct ofp_action_vlan_pcp),
250 [OFPAT_STRIP_VLAN] = {
251 sizeof(struct ofp_action_header),
252 sizeof(struct ofp_action_header),
254 [OFPAT_SET_DL_SRC] = {
255 sizeof(struct ofp_action_dl_addr),
256 sizeof(struct ofp_action_dl_addr),
258 [OFPAT_SET_DL_DST] = {
259 sizeof(struct ofp_action_dl_addr),
260 sizeof(struct ofp_action_dl_addr),
262 [OFPAT_SET_NW_SRC] = {
263 sizeof(struct ofp_action_nw_addr),
264 sizeof(struct ofp_action_nw_addr),
266 [OFPAT_SET_NW_DST] = {
267 sizeof(struct ofp_action_nw_addr),
268 sizeof(struct ofp_action_nw_addr),
270 [OFPAT_SET_NW_TOS] = {
271 sizeof(struct ofp_action_nw_tos),
272 sizeof(struct ofp_action_nw_tos),
274 [OFPAT_SET_TP_SRC] = {
275 sizeof(struct ofp_action_tp_port),
276 sizeof(struct ofp_action_tp_port),
278 [OFPAT_SET_TP_DST] = {
279 sizeof(struct ofp_action_tp_port),
280 sizeof(struct ofp_action_tp_port),
282 /* OFPAT_VENDOR is not here, since it would blow up the array size. */
285 if (actions_len < sizeof *ah) {
286 ds_put_format(string, "***action array too short for next action***\n");
290 type = ntohs(ah->type);
291 len = ntohs(ah->len);
292 if (actions_len < len) {
293 ds_put_format(string, "***truncated action %"PRIu16"***\n", type);
297 if ((len % 8) != 0) {
298 ds_put_format(string,
299 "***action %"PRIu16" length not a multiple of 8***\n",
304 if (type < ARRAY_SIZE(of_actions)) {
305 const struct openflow_action *act = &of_actions[type];
306 if ((len < act->min_size) || (len > act->max_size)) {
307 ds_put_format(string,
308 "***action %"PRIu16" wrong length: %zu***\n", type, len);
315 struct ofp_action_output *oa = (struct ofp_action_output *)ah;
316 uint16_t port = ntohs(oa->port);
317 if (port < OFPP_MAX) {
318 ds_put_format(string, "output:%"PRIu16, port);
320 ofp_print_port_name(string, port);
321 if (port == OFPP_CONTROLLER) {
323 ds_put_format(string, ":%"PRIu16, ntohs(oa->max_len));
325 ds_put_cstr(string, ":all");
332 case OFPAT_ENQUEUE: {
333 struct ofp_action_enqueue *ea = (struct ofp_action_enqueue *)ah;
334 unsigned int port = ntohs(ea->port);
335 unsigned int queue_id = ntohl(ea->queue_id);
336 ds_put_format(string, "enqueue:");
337 if (port != OFPP_IN_PORT) {
338 ds_put_format(string, "%u", port);
340 ds_put_cstr(string, "IN_PORT");
342 ds_put_format(string, "q%u", queue_id);
346 case OFPAT_SET_VLAN_VID: {
347 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
348 ds_put_format(string, "mod_vlan_vid:%"PRIu16, ntohs(va->vlan_vid));
352 case OFPAT_SET_VLAN_PCP: {
353 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
354 ds_put_format(string, "mod_vlan_pcp:%"PRIu8, va->vlan_pcp);
358 case OFPAT_STRIP_VLAN:
359 ds_put_cstr(string, "strip_vlan");
362 case OFPAT_SET_DL_SRC: {
363 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
364 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
365 ETH_ADDR_ARGS(da->dl_addr));
369 case OFPAT_SET_DL_DST: {
370 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
371 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
372 ETH_ADDR_ARGS(da->dl_addr));
376 case OFPAT_SET_NW_SRC: {
377 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
378 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&na->nw_addr));
382 case OFPAT_SET_NW_DST: {
383 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
384 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&na->nw_addr));
388 case OFPAT_SET_NW_TOS: {
389 struct ofp_action_nw_tos *nt = (struct ofp_action_nw_tos *)ah;
390 ds_put_format(string, "mod_nw_tos:%d", nt->nw_tos);
394 case OFPAT_SET_TP_SRC: {
395 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
396 ds_put_format(string, "mod_tp_src:%d", ntohs(ta->tp_port));
400 case OFPAT_SET_TP_DST: {
401 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
402 ds_put_format(string, "mod_tp_dst:%d", ntohs(ta->tp_port));
407 struct ofp_action_vendor_header *avh
408 = (struct ofp_action_vendor_header *)ah;
409 if (len < sizeof *avh) {
410 ds_put_format(string, "***ofpat_vendor truncated***\n");
413 if (avh->vendor == htonl(NX_VENDOR_ID)) {
414 ofp_print_nx_action(string, (struct nx_action_header *)avh);
416 ds_put_format(string, "vendor action:0x%x", ntohl(avh->vendor));
422 ds_put_format(string, "(decoder %"PRIu16" not implemented)", type);
430 ofp_print_actions(struct ds *string, const struct ofp_action_header *action,
433 uint8_t *p = (uint8_t *)action;
436 ds_put_cstr(string, "actions=");
438 ds_put_cstr(string, "drop");
440 while (actions_len > 0) {
442 ds_put_cstr(string, ",");
444 len = ofp_print_action(string, (struct ofp_action_header *)p,
454 /* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'string'
455 * at the given 'verbosity' level. */
456 static void ofp_packet_out(struct ds *string, const void *oh, size_t len,
459 const struct ofp_packet_out *opo = oh;
460 size_t actions_len = ntohs(opo->actions_len);
462 ds_put_cstr(string, " in_port=");
463 ofp_print_port_name(string, ntohs(opo->in_port));
465 ds_put_format(string, " actions_len=%zu ", actions_len);
466 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
467 ds_put_format(string, "***packet too short for action length***\n");
470 ofp_print_actions(string, opo->actions, actions_len);
472 if (ntohl(opo->buffer_id) == UINT32_MAX) {
473 int data_len = len - sizeof *opo - actions_len;
474 ds_put_format(string, " data_len=%d", data_len);
475 if (verbosity > 0 && len > sizeof *opo) {
476 char *packet = ofp_packet_to_string(
477 (uint8_t *)opo->actions + actions_len, data_len, data_len);
478 ds_put_char(string, '\n');
479 ds_put_cstr(string, packet);
483 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
485 ds_put_char(string, '\n');
488 /* qsort comparison function. */
490 compare_ports(const void *a_, const void *b_)
492 const struct ofp_phy_port *a = a_;
493 const struct ofp_phy_port *b = b_;
494 uint16_t ap = ntohs(a->port_no);
495 uint16_t bp = ntohs(b->port_no);
497 return ap < bp ? -1 : ap > bp;
500 static void ofp_print_port_features(struct ds *string, uint32_t features)
503 ds_put_cstr(string, "Unsupported\n");
506 if (features & OFPPF_10MB_HD) {
507 ds_put_cstr(string, "10MB-HD ");
509 if (features & OFPPF_10MB_FD) {
510 ds_put_cstr(string, "10MB-FD ");
512 if (features & OFPPF_100MB_HD) {
513 ds_put_cstr(string, "100MB-HD ");
515 if (features & OFPPF_100MB_FD) {
516 ds_put_cstr(string, "100MB-FD ");
518 if (features & OFPPF_1GB_HD) {
519 ds_put_cstr(string, "1GB-HD ");
521 if (features & OFPPF_1GB_FD) {
522 ds_put_cstr(string, "1GB-FD ");
524 if (features & OFPPF_10GB_FD) {
525 ds_put_cstr(string, "10GB-FD ");
527 if (features & OFPPF_COPPER) {
528 ds_put_cstr(string, "COPPER ");
530 if (features & OFPPF_FIBER) {
531 ds_put_cstr(string, "FIBER ");
533 if (features & OFPPF_AUTONEG) {
534 ds_put_cstr(string, "AUTO_NEG ");
536 if (features & OFPPF_PAUSE) {
537 ds_put_cstr(string, "AUTO_PAUSE ");
539 if (features & OFPPF_PAUSE_ASYM) {
540 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
542 ds_put_char(string, '\n');
546 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
548 uint8_t name[OFP_MAX_PORT_NAME_LEN];
551 memcpy(name, port->name, sizeof name);
552 for (j = 0; j < sizeof name - 1; j++) {
553 if (!isprint(name[j])) {
559 ds_put_char(string, ' ');
560 ofp_print_port_name(string, ntohs(port->port_no));
561 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
562 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
565 ds_put_format(string, " current: ");
566 ofp_print_port_features(string, ntohl(port->curr));
568 if (port->advertised) {
569 ds_put_format(string, " advertised: ");
570 ofp_print_port_features(string, ntohl(port->advertised));
572 if (port->supported) {
573 ds_put_format(string, " supported: ");
574 ofp_print_port_features(string, ntohl(port->supported));
577 ds_put_format(string, " peer: ");
578 ofp_print_port_features(string, ntohl(port->peer));
582 /* Pretty-print the struct ofp_switch_features of 'len' bytes at 'oh' to
583 * 'string' at the given 'verbosity' level. */
585 ofp_print_switch_features(struct ds *string, const void *oh, size_t len,
586 int verbosity OVS_UNUSED)
588 const struct ofp_switch_features *osf = oh;
589 struct ofp_phy_port *port_list;
593 ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
594 osf->header.version, ntohll(osf->datapath_id));
595 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
596 ntohl(osf->n_buffers));
597 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
598 ntohl(osf->capabilities), ntohl(osf->actions));
600 if (ntohs(osf->header.length) >= sizeof *osf) {
601 len = MIN(len, ntohs(osf->header.length));
603 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
605 port_list = xmemdup(osf->ports, len - sizeof *osf);
606 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
607 for (i = 0; i < n_ports; i++) {
608 ofp_print_phy_port(string, &port_list[i]);
613 /* Pretty-print the struct ofp_switch_config of 'len' bytes at 'oh' to 'string'
614 * at the given 'verbosity' level. */
616 ofp_print_switch_config(struct ds *string, const void *oh,
617 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
619 const struct ofp_switch_config *osc = oh;
622 flags = ntohs(osc->flags);
624 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
627 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
630 static void print_wild(struct ds *string, const char *leader, int is_wild,
631 int verbosity, const char *format, ...)
632 __attribute__((format(printf, 5, 6)));
634 static void print_wild(struct ds *string, const char *leader, int is_wild,
635 int verbosity, const char *format, ...)
637 if (is_wild && verbosity < 2) {
640 ds_put_cstr(string, leader);
644 va_start(args, format);
645 ds_put_format_valist(string, format, args);
648 ds_put_char(string, '*');
650 ds_put_char(string, ',');
654 print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
655 uint32_t wild_bits, int verbosity)
657 if (wild_bits >= 32 && verbosity < 2) {
660 ds_put_cstr(string, leader);
661 if (wild_bits < 32) {
662 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
664 ds_put_format(string, "/%d", 32 - wild_bits);
667 ds_put_char(string, '*');
669 ds_put_char(string, ',');
673 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
675 char *s = ofp_match_to_string(om, verbosity);
681 ofp_match_to_string(const struct ofp_match *om, int verbosity)
683 struct ds f = DS_EMPTY_INITIALIZER;
684 uint32_t w = ntohl(om->wildcards);
685 bool skip_type = false;
686 bool skip_proto = false;
688 if (!(w & OFPFW_DL_TYPE)) {
690 if (om->dl_type == htons(ETH_TYPE_IP)) {
691 if (!(w & OFPFW_NW_PROTO)) {
693 if (om->nw_proto == IP_TYPE_ICMP) {
694 ds_put_cstr(&f, "icmp,");
695 } else if (om->nw_proto == IP_TYPE_TCP) {
696 ds_put_cstr(&f, "tcp,");
697 } else if (om->nw_proto == IP_TYPE_UDP) {
698 ds_put_cstr(&f, "udp,");
700 ds_put_cstr(&f, "ip,");
704 ds_put_cstr(&f, "ip,");
706 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
707 ds_put_cstr(&f, "arp,");
712 if (w & NXFW_TUN_ID) {
713 ds_put_cstr(&f, "tun_id_wild,");
715 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
716 "%d", ntohs(om->in_port));
717 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
718 "%d", ntohs(om->dl_vlan));
719 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
720 "%d", om->dl_vlan_pcp);
721 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
722 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
723 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
724 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
726 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
727 "0x%04x", ntohs(om->dl_type));
729 print_ip_netmask(&f, "nw_src=", om->nw_src,
730 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
731 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
732 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
734 if (om->dl_type == htons(ETH_TYPE_ARP)) {
735 print_wild(&f, "opcode=", w & OFPFW_NW_PROTO, verbosity,
738 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
740 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
744 if (om->nw_proto == IP_TYPE_ICMP) {
745 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
746 "%d", ntohs(om->icmp_type));
747 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
748 "%d", ntohs(om->icmp_code));
750 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
751 "%d", ntohs(om->tp_src));
752 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
753 "%d", ntohs(om->tp_dst));
758 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
759 * at the given 'verbosity' level. */
761 ofp_print_flow_mod(struct ds *string, const void *oh, size_t len,
764 const struct ofp_flow_mod *ofm = oh;
765 unsigned int command = ntohs(ofm->command);
767 ds_put_char(string, ' ');
768 ofp_print_match(string, &ofm->match, verbosity);
769 if (ds_last(string) != ' ') {
770 ds_put_char(string, ' ');
773 switch (command & 0xff) {
775 ds_put_cstr(string, "ADD:");
778 ds_put_cstr(string, "MOD:");
780 case OFPFC_MODIFY_STRICT:
781 ds_put_cstr(string, "MOD_STRICT:");
784 ds_put_cstr(string, "DEL:");
786 case OFPFC_DELETE_STRICT:
787 ds_put_cstr(string, "DEL_STRICT:");
790 ds_put_format(string, "cmd:%u", command & 0xff);
792 if (command & 0xff00) {
793 ds_put_format(string, " table_id:%u", command >> 8);
795 if (ofm->cookie != htonll(0)) {
796 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(ofm->cookie));
798 if (ofm->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
799 ds_put_format(string, " idle:%d", ntohs(ofm->idle_timeout));
801 if (ofm->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
802 ds_put_format(string, " hard:%d", ntohs(ofm->hard_timeout));
804 if (ofm->priority != htons(32768)) {
805 ds_put_format(string, " pri:%"PRIu16, ntohs(ofm->priority));
807 if (ofm->buffer_id != htonl(UINT32_MAX)) {
808 ds_put_format(string, " buf:%#"PRIx32, ntohl(ofm->buffer_id));
810 if (ofm->flags != htons(0)) {
811 ds_put_format(string, " flags:%"PRIx16, ntohs(ofm->flags));
813 ds_put_cstr(string, " ");
814 ofp_print_actions(string, ofm->actions,
815 len - offsetof(struct ofp_flow_mod, actions));
816 ds_put_char(string, '\n');
819 /* Pretty-print the OFPT_FLOW_REMOVED packet of 'len' bytes at 'oh' to 'string'
820 * at the given 'verbosity' level. */
822 ofp_print_flow_removed(struct ds *string, const void *oh,
823 size_t len OVS_UNUSED, int verbosity)
825 const struct ofp_flow_removed *ofr = oh;
827 ofp_print_match(string, &ofr->match, verbosity);
828 ds_put_cstr(string, " reason=");
829 switch (ofr->reason) {
830 case OFPRR_IDLE_TIMEOUT:
831 ds_put_cstr(string, "idle");
833 case OFPRR_HARD_TIMEOUT:
834 ds_put_cstr(string, "hard");
837 ds_put_cstr(string, "delete");
840 ds_put_format(string, "**%"PRIu8"**", ofr->reason);
844 if (ofr->cookie != htonll(0)) {
845 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(ofr->cookie));
847 if (ofr->priority != htons(32768)) {
848 ds_put_format(string, " pri:%"PRIu16, ntohs(ofr->priority));
850 ds_put_format(string, " secs%"PRIu32" nsecs%"PRIu32
851 " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
852 ntohl(ofr->duration_sec), ntohl(ofr->duration_nsec),
853 ntohs(ofr->idle_timeout), ntohll(ofr->packet_count),
854 ntohll(ofr->byte_count));
858 ofp_print_port_mod(struct ds *string, const void *oh, size_t len OVS_UNUSED,
859 int verbosity OVS_UNUSED)
861 const struct ofp_port_mod *opm = oh;
863 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
864 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
865 ntohl(opm->config), ntohl(opm->mask));
866 ds_put_format(string, " advertise: ");
867 if (opm->advertise) {
868 ofp_print_port_features(string, ntohl(opm->advertise));
870 ds_put_format(string, "UNCHANGED\n");
880 static const struct error_type error_types[] = {
881 #define ERROR_TYPE(TYPE) {TYPE, -1, #TYPE}
882 #define ERROR_CODE(TYPE, CODE) {TYPE, CODE, #CODE}
883 ERROR_TYPE(OFPET_HELLO_FAILED),
884 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_INCOMPATIBLE),
885 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_EPERM),
887 ERROR_TYPE(OFPET_BAD_REQUEST),
888 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
889 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE),
890 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT),
891 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR),
892 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE),
893 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_EPERM),
894 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN),
895 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_EMPTY),
896 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_UNKNOWN),
898 ERROR_TYPE(OFPET_BAD_ACTION),
899 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE),
900 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_LEN),
901 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR),
902 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE),
903 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT),
904 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT),
905 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_EPERM),
906 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_TOO_MANY),
908 ERROR_TYPE(OFPET_FLOW_MOD_FAILED),
909 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL),
910 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP),
911 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_EPERM),
912 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_EMERG_TIMEOUT),
913 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND),
915 ERROR_TYPE(OFPET_PORT_MOD_FAILED),
916 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT),
917 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR)
919 #define N_ERROR_TYPES ARRAY_SIZE(error_types)
922 lookup_error_type(int type)
924 const struct error_type *t;
926 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
927 if (t->type == type && t->code == -1) {
935 lookup_error_code(int type, int code)
937 const struct error_type *t;
939 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
940 if (t->type == type && t->code == code) {
947 /* Pretty-print the OFPT_ERROR packet of 'len' bytes at 'oh' to 'string'
948 * at the given 'verbosity' level. */
950 ofp_print_error_msg(struct ds *string, const void *oh, size_t len,
951 int verbosity OVS_UNUSED)
953 const struct ofp_error_msg *oem = oh;
954 int type = ntohs(oem->type);
955 int code = ntohs(oem->code);
958 ds_put_format(string, " type%d(%s) code%d(%s) payload:\n",
959 type, lookup_error_type(type),
960 code, lookup_error_code(type, code));
963 case OFPET_HELLO_FAILED:
964 ds_put_printable(string, (char *) oem->data, len - sizeof *oem);
967 case OFPET_BAD_REQUEST:
968 s = ofp_to_string(oem->data, len - sizeof *oem, 1);
969 ds_put_cstr(string, s);
974 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
979 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'string'
980 * at the given 'verbosity' level. */
982 ofp_print_port_status(struct ds *string, const void *oh, size_t len OVS_UNUSED,
983 int verbosity OVS_UNUSED)
985 const struct ofp_port_status *ops = oh;
987 if (ops->reason == OFPPR_ADD) {
988 ds_put_format(string, " ADD:");
989 } else if (ops->reason == OFPPR_DELETE) {
990 ds_put_format(string, " DEL:");
991 } else if (ops->reason == OFPPR_MODIFY) {
992 ds_put_format(string, " MOD:");
995 ofp_print_phy_port(string, &ops->desc);
999 ofp_desc_stats_reply(struct ds *string, const void *body,
1000 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
1002 const struct ofp_desc_stats *ods = body;
1004 ds_put_format(string, "Manufacturer: %.*s\n",
1005 (int) sizeof ods->mfr_desc, ods->mfr_desc);
1006 ds_put_format(string, "Hardware: %.*s\n",
1007 (int) sizeof ods->hw_desc, ods->hw_desc);
1008 ds_put_format(string, "Software: %.*s\n",
1009 (int) sizeof ods->sw_desc, ods->sw_desc);
1010 ds_put_format(string, "Serial Num: %.*s\n",
1011 (int) sizeof ods->serial_num, ods->serial_num);
1012 ds_put_format(string, "DP Description: %.*s\n",
1013 (int) sizeof ods->dp_desc, ods->dp_desc);
1017 ofp_flow_stats_request(struct ds *string, const void *oh,
1018 size_t len OVS_UNUSED, int verbosity)
1020 const struct ofp_flow_stats_request *fsr = oh;
1022 if (fsr->table_id == 0xff) {
1023 ds_put_format(string, " table_id=any, ");
1025 ds_put_format(string, " table_id=%"PRIu8", ", fsr->table_id);
1028 ofp_print_match(string, &fsr->match, verbosity);
1032 ofp_flow_stats_reply(struct ds *string, const void *body_, size_t len,
1035 const char *body = body_;
1036 const char *pos = body;
1038 const struct ofp_flow_stats *fs;
1039 ptrdiff_t bytes_left = body + len - pos;
1042 if (bytes_left < sizeof *fs) {
1043 if (bytes_left != 0) {
1044 ds_put_format(string, " ***%td leftover bytes at end***",
1050 fs = (const void *) pos;
1051 length = ntohs(fs->length);
1052 if (length < sizeof *fs) {
1053 ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
1054 length, sizeof *fs);
1056 } else if (length > bytes_left) {
1057 ds_put_format(string,
1058 " ***length=%zu but only %td bytes left***",
1059 length, bytes_left);
1061 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
1062 ds_put_format(string,
1063 " ***length=%zu has %zu bytes leftover in "
1066 (length - sizeof *fs) % sizeof fs->actions[0]);
1070 ds_put_format(string, " cookie=0x%"PRIx64", ", ntohll(fs->cookie));
1071 ds_put_format(string, "duration_sec=%"PRIu32"s, ",
1072 ntohl(fs->duration_sec));
1073 ds_put_format(string, "duration_nsec=%"PRIu32"ns, ",
1074 ntohl(fs->duration_nsec));
1075 ds_put_format(string, "table_id=%"PRIu8", ", fs->table_id);
1076 ds_put_format(string, "priority=%"PRIu16", ",
1077 fs->match.wildcards ? ntohs(fs->priority) : (uint16_t)-1);
1078 ds_put_format(string, "n_packets=%"PRIu64", ",
1079 ntohll(fs->packet_count));
1080 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
1081 if (fs->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
1082 ds_put_format(string, "idle_timeout=%"PRIu16",",
1083 ntohs(fs->idle_timeout));
1085 if (fs->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
1086 ds_put_format(string, "hard_timeout=%"PRIu16",",
1087 ntohs(fs->hard_timeout));
1089 ofp_print_match(string, &fs->match, verbosity);
1090 ofp_print_actions(string, fs->actions, length - sizeof *fs);
1091 ds_put_char(string, '\n');
1098 ofp_aggregate_stats_request(struct ds *string, const void *oh,
1099 size_t len OVS_UNUSED, int verbosity)
1101 const struct ofp_aggregate_stats_request *asr = oh;
1103 if (asr->table_id == 0xff) {
1104 ds_put_format(string, " table_id=any, ");
1106 ds_put_format(string, " table_id=%"PRIu8", ", asr->table_id);
1109 ofp_print_match(string, &asr->match, verbosity);
1113 ofp_aggregate_stats_reply(struct ds *string, const void *body_,
1114 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
1116 const struct ofp_aggregate_stats_reply *asr = body_;
1118 ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
1119 ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
1120 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1123 static void print_port_stat(struct ds *string, const char *leader,
1124 uint64_t stat, int more)
1126 ds_put_cstr(string, leader);
1128 ds_put_format(string, "%"PRIu64, stat);
1130 ds_put_char(string, '?');
1133 ds_put_cstr(string, ", ");
1135 ds_put_cstr(string, "\n");
1140 ofp_port_stats_request(struct ds *string, const void *body_,
1141 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
1143 const struct ofp_port_stats_request *psr = body_;
1144 ds_put_format(string, "port_no=%"PRIu16, ntohs(psr->port_no));
1148 ofp_port_stats_reply(struct ds *string, const void *body, size_t len,
1151 const struct ofp_port_stats *ps = body;
1152 size_t n = len / sizeof *ps;
1153 ds_put_format(string, " %zu ports\n", n);
1154 if (verbosity < 1) {
1159 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1161 ds_put_cstr(string, "rx ");
1162 print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
1163 print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
1164 print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
1165 print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
1166 print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
1167 print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
1168 print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
1170 ds_put_cstr(string, " tx ");
1171 print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
1172 print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
1173 print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
1174 print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
1175 print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
1180 ofp_table_stats_reply(struct ds *string, const void *body, size_t len,
1183 const struct ofp_table_stats *ts = body;
1184 size_t n = len / sizeof *ts;
1185 ds_put_format(string, " %zu tables\n", n);
1186 if (verbosity < 1) {
1191 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1192 strncpy(name, ts->name, sizeof name);
1193 name[OFP_MAX_TABLE_NAME_LEN] = '\0';
1195 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1196 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1197 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1198 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1199 ds_put_cstr(string, " ");
1200 ds_put_format(string, "lookup=%"PRIu64", ",
1201 ntohll(ts->lookup_count));
1202 ds_put_format(string, "matched=%"PRIu64"\n",
1203 ntohll(ts->matched_count));
1208 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1210 if (queue_id == OFPQ_ALL) {
1211 ds_put_cstr(string, "ALL");
1213 ds_put_format(string, "%"PRIu32, queue_id);
1218 ofp_queue_stats_request(struct ds *string, const void *body_,
1219 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
1221 const struct ofp_queue_stats_request *qsr = body_;
1223 ds_put_cstr(string, "port=");
1224 ofp_print_port_name(string, ntohs(qsr->port_no));
1226 ds_put_cstr(string, " queue=");
1227 ofp_print_queue_name(string, ntohl(qsr->queue_id));
1231 ofp_queue_stats_reply(struct ds *string, const void *body, size_t len,
1234 const struct ofp_queue_stats *qs = body;
1235 size_t n = len / sizeof *qs;
1236 ds_put_format(string, " %zu queues\n", n);
1237 if (verbosity < 1) {
1242 ds_put_cstr(string, " port ");
1243 ofp_print_port_name(string, ntohs(qs->port_no));
1244 ds_put_cstr(string, " queue ");
1245 ofp_print_queue_name(string, ntohl(qs->queue_id));
1246 ds_put_cstr(string, ": ");
1248 print_port_stat(string, "bytes=", ntohll(qs->tx_bytes), 1);
1249 print_port_stat(string, "pkts=", ntohll(qs->tx_packets), 1);
1250 print_port_stat(string, "errors=", ntohll(qs->tx_errors), 0);
1255 vendor_stat(struct ds *string, const void *body, size_t len,
1256 int verbosity OVS_UNUSED)
1258 ds_put_format(string, " vendor=%08"PRIx32, ntohl(*(uint32_t *) body));
1259 ds_put_format(string, " %zu bytes additional data",
1260 len - sizeof(uint32_t));
1263 enum stats_direction {
1269 print_stats(struct ds *string, int type, const void *body, size_t body_len,
1270 int verbosity, enum stats_direction direction)
1273 size_t min_body, max_body;
1274 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1280 struct stats_msg request;
1281 struct stats_msg reply;
1284 static const struct stats_type stats_types[] = {
1289 { 0, SIZE_MAX, ofp_desc_stats_reply },
1294 { sizeof(struct ofp_flow_stats_request),
1295 sizeof(struct ofp_flow_stats_request),
1296 ofp_flow_stats_request },
1297 { 0, SIZE_MAX, ofp_flow_stats_reply },
1302 { sizeof(struct ofp_aggregate_stats_request),
1303 sizeof(struct ofp_aggregate_stats_request),
1304 ofp_aggregate_stats_request },
1305 { sizeof(struct ofp_aggregate_stats_reply),
1306 sizeof(struct ofp_aggregate_stats_reply),
1307 ofp_aggregate_stats_reply },
1313 { 0, SIZE_MAX, ofp_table_stats_reply },
1318 { sizeof(struct ofp_port_stats_request),
1319 sizeof(struct ofp_port_stats_request),
1320 ofp_port_stats_request },
1321 { 0, SIZE_MAX, ofp_port_stats_reply },
1326 { sizeof(struct ofp_queue_stats_request),
1327 sizeof(struct ofp_queue_stats_request),
1328 ofp_queue_stats_request },
1329 { 0, SIZE_MAX, ofp_queue_stats_reply },
1334 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1335 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1345 const struct stats_type *s;
1346 const struct stats_msg *m;
1348 if (type >= ARRAY_SIZE(stats_types) || !stats_types[type].name) {
1349 ds_put_format(string, " ***unknown type %d***", type);
1352 for (s = stats_types; s->type >= 0; s++) {
1353 if (s->type == type) {
1357 ds_put_format(string, " type=%d(%s)\n", type, s->name);
1359 m = direction == REQUEST ? &s->request : &s->reply;
1360 if (body_len < m->min_body || body_len > m->max_body) {
1361 ds_put_format(string, " ***body_len=%zu not in %zu...%zu***",
1362 body_len, m->min_body, m->max_body);
1366 m->printer(string, body, body_len, verbosity);
1371 ofp_stats_request(struct ds *string, const void *oh, size_t len, int verbosity)
1373 const struct ofp_stats_request *srq = oh;
1376 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1380 print_stats(string, ntohs(srq->type), srq->body,
1381 len - offsetof(struct ofp_stats_request, body),
1382 verbosity, REQUEST);
1386 ofp_stats_reply(struct ds *string, const void *oh, size_t len, int verbosity)
1388 const struct ofp_stats_reply *srp = oh;
1390 ds_put_cstr(string, " flags=");
1392 ds_put_cstr(string, "none");
1394 uint16_t flags = ntohs(srp->flags);
1395 if (flags & OFPSF_REPLY_MORE) {
1396 ds_put_cstr(string, "[more]");
1397 flags &= ~OFPSF_REPLY_MORE;
1400 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]", flags);
1404 print_stats(string, ntohs(srp->type), srp->body,
1405 len - offsetof(struct ofp_stats_reply, body),
1410 ofp_echo(struct ds *string, const void *oh, size_t len, int verbosity)
1412 const struct ofp_header *hdr = oh;
1414 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *hdr);
1415 if (verbosity > 1) {
1416 ds_put_hex_dump(string, hdr, len - sizeof *hdr, 0, true);
1420 struct openflow_packet {
1424 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1427 static const struct openflow_packet packets[] = {
1431 sizeof (struct ofp_header),
1435 OFPT_FEATURES_REQUEST,
1437 sizeof (struct ofp_header),
1441 OFPT_FEATURES_REPLY,
1443 sizeof (struct ofp_switch_features),
1444 ofp_print_switch_features,
1447 OFPT_GET_CONFIG_REQUEST,
1448 "get_config_request",
1449 sizeof (struct ofp_header),
1453 OFPT_GET_CONFIG_REPLY,
1455 sizeof (struct ofp_switch_config),
1456 ofp_print_switch_config,
1461 sizeof (struct ofp_switch_config),
1462 ofp_print_switch_config,
1467 offsetof(struct ofp_packet_in, data),
1473 sizeof (struct ofp_packet_out),
1479 sizeof (struct ofp_flow_mod),
1485 sizeof (struct ofp_flow_removed),
1486 ofp_print_flow_removed,
1491 sizeof (struct ofp_port_mod),
1497 sizeof (struct ofp_port_status),
1498 ofp_print_port_status
1503 sizeof (struct ofp_error_msg),
1504 ofp_print_error_msg,
1509 sizeof (struct ofp_stats_request),
1515 sizeof (struct ofp_stats_reply),
1521 sizeof (struct ofp_header),
1527 sizeof (struct ofp_header),
1533 sizeof (struct ofp_vendor_header),
1537 OFPT_BARRIER_REQUEST,
1539 sizeof (struct ofp_header),
1545 sizeof (struct ofp_header),
1550 /* Composes and returns a string representing the OpenFlow packet of 'len'
1551 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1552 * verbosity and higher numbers increase verbosity. The caller is responsible
1553 * for freeing the string. */
1555 ofp_to_string(const void *oh_, size_t len, int verbosity)
1557 struct ds string = DS_EMPTY_INITIALIZER;
1558 const struct ofp_header *oh = oh_;
1559 const struct openflow_packet *pkt;
1561 if (len < sizeof(struct ofp_header)) {
1562 ds_put_cstr(&string, "OpenFlow packet too short:\n");
1563 ds_put_hex_dump(&string, oh, len, 0, true);
1564 return ds_cstr(&string);
1565 } else if (oh->version != OFP_VERSION) {
1566 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", oh->version);
1567 ds_put_hex_dump(&string, oh, len, 0, true);
1568 return ds_cstr(&string);
1571 for (pkt = packets; ; pkt++) {
1572 if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1573 ds_put_format(&string, "Unknown OpenFlow packet type %"PRIu8":\n",
1575 ds_put_hex_dump(&string, oh, len, 0, true);
1576 return ds_cstr(&string);
1577 } else if (oh->type == pkt->type) {
1582 ds_put_format(&string, "%s (xid=0x%"PRIx32"):", pkt->name, oh->xid);
1584 if (ntohs(oh->length) > len)
1585 ds_put_format(&string, " (***truncated to %zu bytes from %"PRIu16"***)",
1586 len, ntohs(oh->length));
1587 else if (ntohs(oh->length) < len) {
1588 ds_put_format(&string, " (***only uses %"PRIu16" bytes out of %zu***)\n",
1589 ntohs(oh->length), len);
1590 len = ntohs(oh->length);
1593 if (len < pkt->min_size) {
1594 ds_put_format(&string, " (***length=%zu < min_size=%zu***)\n",
1595 len, pkt->min_size);
1596 } else if (!pkt->printer) {
1597 if (len > sizeof *oh) {
1598 ds_put_format(&string, " length=%"PRIu16" (decoder not implemented)\n",
1602 pkt->printer(&string, oh, len, verbosity);
1604 if (verbosity >= 3) {
1605 ds_put_hex_dump(&string, oh, len, 0, true);
1607 if (string.string[string.length - 1] != '\n') {
1608 ds_put_char(&string, '\n');
1610 return ds_cstr(&string);
1613 /* Returns the name for the specified OpenFlow message type as a string,
1614 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1615 * hex number, e.g. "0x55".
1617 * The caller must free the returned string when it is no longer needed. */
1619 ofp_message_type_to_string(uint8_t type)
1621 struct ds s = DS_EMPTY_INITIALIZER;
1622 const struct openflow_packet *pkt;
1623 for (pkt = packets; ; pkt++) {
1624 if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1625 ds_put_format(&s, "0x%02"PRIx8, type);
1627 } else if (type == pkt->type) {
1630 ds_put_cstr(&s, "OFPT_");
1631 for (p = pkt->name; *p; p++) {
1632 ds_put_char(&s, toupper((unsigned char) *p));
1641 print_and_free(FILE *stream, char *string)
1643 fputs(string, stream);
1647 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1648 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1649 * numbers increase verbosity. */
1651 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1653 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1656 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1657 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1658 * the Ethernet frame (of which 'len' bytes were captured).
1660 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1662 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1664 print_and_free(stream, ofp_packet_to_string(data, len, total_len));