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);
42 /* Returns a string that represents the contents of the Ethernet frame in the
43 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
44 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
45 * bytes were captured).
47 * The caller must free the returned string.
49 * This starts and kills a tcpdump subprocess so it's quite expensive. */
51 ofp_packet_to_string(const void *data, size_t len, size_t total_len OVS_UNUSED)
53 struct ds ds = DS_EMPTY_INITIALIZER;
62 buf.data = (void *) data;
67 ovs_error(errno, "tmpfile");
68 return xstrdup("<error>");
70 pcap_write_header(pcap);
71 pcap_write(pcap, &buf);
74 ovs_error(errno, "error writing temporary file");
78 snprintf(command, sizeof command, "/usr/sbin/tcpdump -e -n -r /dev/fd/%d 2>/dev/null",
80 tcpdump = popen(command, "r");
83 ovs_error(errno, "exec(\"%s\")", command);
84 return xstrdup("<error>");
87 while ((c = getc(tcpdump)) != EOF) {
91 status = pclose(tcpdump);
92 if (WIFEXITED(status)) {
93 if (WEXITSTATUS(status))
94 ovs_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
95 } else if (WIFSIGNALED(status)) {
96 ovs_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
101 /* Pretty-print the OFPT_PACKET_IN packet of 'len' bytes at 'oh' to 'stream'
102 * at the given 'verbosity' level. */
104 ofp_packet_in(struct ds *string, const void *oh, size_t len, int verbosity)
106 const struct ofp_packet_in *op = oh;
109 ds_put_format(string, " total_len=%"PRIu16" in_port=",
110 ntohs(op->total_len));
111 ofp_print_port_name(string, ntohs(op->in_port));
113 if (op->reason == OFPR_ACTION)
114 ds_put_cstr(string, " (via action)");
115 else if (op->reason != OFPR_NO_MATCH)
116 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
118 data_len = len - offsetof(struct ofp_packet_in, data);
119 ds_put_format(string, " data_len=%zu", data_len);
120 if (htonl(op->buffer_id) == UINT32_MAX) {
121 ds_put_format(string, " (unbuffered)");
122 if (ntohs(op->total_len) != data_len)
123 ds_put_format(string, " (***total_len != data_len***)");
125 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
126 if (ntohs(op->total_len) < data_len)
127 ds_put_format(string, " (***total_len < data_len***)");
129 ds_put_char(string, '\n');
133 struct ofpbuf packet;
134 struct ofp_match match;
135 packet.data = (void *) op->data;
136 packet.size = data_len;
137 flow_extract(&packet, 0, ntohs(op->in_port), &flow);
138 flow_to_match(&flow, 0, false, &match);
139 ofp_print_match(string, &match, verbosity);
140 ds_put_char(string, '\n');
143 char *packet = ofp_packet_to_string(op->data, data_len,
144 ntohs(op->total_len));
145 ds_put_cstr(string, packet);
150 static void ofp_print_port_name(struct ds *string, uint16_t port)
169 case OFPP_CONTROLLER:
179 ds_put_format(string, "%"PRIu16, port);
182 ds_put_cstr(string, name);
186 ofp_print_nx_action(struct ds *string, const struct nx_action_header *nah)
188 switch (ntohs(nah->subtype)) {
189 case NXAST_RESUBMIT: {
190 const struct nx_action_resubmit *nar = (struct nx_action_resubmit *)nah;
191 ds_put_format(string, "resubmit:");
192 ofp_print_port_name(string, ntohs(nar->in_port));
196 case NXAST_SET_TUNNEL: {
197 const struct nx_action_set_tunnel *nast =
198 (struct nx_action_set_tunnel *)nah;
199 ds_put_format(string, "set_tunnel:0x%08"PRIx32, ntohl(nast->tun_id));
204 ds_put_format(string, "***unknown Nicira action:%d***\n",
205 ntohs(nah->subtype));
210 ofp_print_action(struct ds *string, const struct ofp_action_header *ah,
216 struct openflow_action {
221 const struct openflow_action of_actions[] = {
223 sizeof(struct ofp_action_output),
224 sizeof(struct ofp_action_output),
226 [OFPAT_SET_VLAN_VID] = {
227 sizeof(struct ofp_action_vlan_vid),
228 sizeof(struct ofp_action_vlan_vid),
230 [OFPAT_SET_VLAN_PCP] = {
231 sizeof(struct ofp_action_vlan_pcp),
232 sizeof(struct ofp_action_vlan_pcp),
234 [OFPAT_STRIP_VLAN] = {
235 sizeof(struct ofp_action_header),
236 sizeof(struct ofp_action_header),
238 [OFPAT_SET_DL_SRC] = {
239 sizeof(struct ofp_action_dl_addr),
240 sizeof(struct ofp_action_dl_addr),
242 [OFPAT_SET_DL_DST] = {
243 sizeof(struct ofp_action_dl_addr),
244 sizeof(struct ofp_action_dl_addr),
246 [OFPAT_SET_NW_SRC] = {
247 sizeof(struct ofp_action_nw_addr),
248 sizeof(struct ofp_action_nw_addr),
250 [OFPAT_SET_NW_DST] = {
251 sizeof(struct ofp_action_nw_addr),
252 sizeof(struct ofp_action_nw_addr),
254 [OFPAT_SET_NW_TOS] = {
255 sizeof(struct ofp_action_nw_tos),
256 sizeof(struct ofp_action_nw_tos),
258 [OFPAT_SET_TP_SRC] = {
259 sizeof(struct ofp_action_tp_port),
260 sizeof(struct ofp_action_tp_port),
262 [OFPAT_SET_TP_DST] = {
263 sizeof(struct ofp_action_tp_port),
264 sizeof(struct ofp_action_tp_port),
266 /* OFPAT_VENDOR is not here, since it would blow up the array size. */
269 if (actions_len < sizeof *ah) {
270 ds_put_format(string, "***action array too short for next action***\n");
274 type = ntohs(ah->type);
275 len = ntohs(ah->len);
276 if (actions_len < len) {
277 ds_put_format(string, "***truncated action %"PRIu16"***\n", type);
281 if ((len % 8) != 0) {
282 ds_put_format(string,
283 "***action %"PRIu16" length not a multiple of 8***\n",
288 if (type < ARRAY_SIZE(of_actions)) {
289 const struct openflow_action *act = &of_actions[type];
290 if ((len < act->min_size) || (len > act->max_size)) {
291 ds_put_format(string,
292 "***action %"PRIu16" wrong length: %zu***\n", type, len);
299 struct ofp_action_output *oa = (struct ofp_action_output *)ah;
300 uint16_t port = ntohs(oa->port);
301 if (port < OFPP_MAX) {
302 ds_put_format(string, "output:%"PRIu16, port);
304 ofp_print_port_name(string, port);
305 if (port == OFPP_CONTROLLER) {
307 ds_put_format(string, ":%"PRIu16, ntohs(oa->max_len));
309 ds_put_cstr(string, ":all");
316 case OFPAT_ENQUEUE: {
317 struct ofp_action_enqueue *ea = (struct ofp_action_enqueue *)ah;
318 unsigned int port = ntohs(ea->port);
319 unsigned int queue_id = ntohl(ea->queue_id);
320 ds_put_format(string, "enqueue:");
321 if (port != OFPP_IN_PORT) {
322 ds_put_format(string, "%u", port);
324 ds_put_cstr(string, "IN_PORT");
326 ds_put_format(string, "q%u", queue_id);
330 case OFPAT_SET_VLAN_VID: {
331 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
332 ds_put_format(string, "mod_vlan_vid:%"PRIu16, ntohs(va->vlan_vid));
336 case OFPAT_SET_VLAN_PCP: {
337 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
338 ds_put_format(string, "mod_vlan_pcp:%"PRIu8, va->vlan_pcp);
342 case OFPAT_STRIP_VLAN:
343 ds_put_cstr(string, "strip_vlan");
346 case OFPAT_SET_DL_SRC: {
347 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
348 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
349 ETH_ADDR_ARGS(da->dl_addr));
353 case OFPAT_SET_DL_DST: {
354 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
355 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
356 ETH_ADDR_ARGS(da->dl_addr));
360 case OFPAT_SET_NW_SRC: {
361 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
362 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&na->nw_addr));
366 case OFPAT_SET_NW_DST: {
367 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
368 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&na->nw_addr));
372 case OFPAT_SET_NW_TOS: {
373 struct ofp_action_nw_tos *nt = (struct ofp_action_nw_tos *)ah;
374 ds_put_format(string, "mod_nw_tos:%d", nt->nw_tos);
378 case OFPAT_SET_TP_SRC: {
379 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
380 ds_put_format(string, "mod_tp_src:%d", ntohs(ta->tp_port));
384 case OFPAT_SET_TP_DST: {
385 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
386 ds_put_format(string, "mod_tp_dst:%d", ntohs(ta->tp_port));
391 struct ofp_action_vendor_header *avh
392 = (struct ofp_action_vendor_header *)ah;
393 if (len < sizeof *avh) {
394 ds_put_format(string, "***ofpat_vendor truncated***\n");
397 if (avh->vendor == htonl(NX_VENDOR_ID)) {
398 ofp_print_nx_action(string, (struct nx_action_header *)avh);
400 ds_put_format(string, "vendor action:0x%x", ntohl(avh->vendor));
406 ds_put_format(string, "(decoder %"PRIu16" not implemented)", type);
414 ofp_print_actions(struct ds *string, const struct ofp_action_header *action,
417 uint8_t *p = (uint8_t *)action;
420 ds_put_cstr(string, "actions=");
422 ds_put_cstr(string, "drop");
424 while (actions_len > 0) {
426 ds_put_cstr(string, ",");
428 len = ofp_print_action(string, (struct ofp_action_header *)p,
438 /* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'string'
439 * at the given 'verbosity' level. */
440 static void ofp_packet_out(struct ds *string, const void *oh, size_t len,
443 const struct ofp_packet_out *opo = oh;
444 size_t actions_len = ntohs(opo->actions_len);
446 ds_put_cstr(string, " in_port=");
447 ofp_print_port_name(string, ntohs(opo->in_port));
449 ds_put_format(string, " actions_len=%zu ", actions_len);
450 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
451 ds_put_format(string, "***packet too short for action length***\n");
454 ofp_print_actions(string, opo->actions, actions_len);
456 if (ntohl(opo->buffer_id) == UINT32_MAX) {
457 int data_len = len - sizeof *opo - actions_len;
458 ds_put_format(string, " data_len=%d", data_len);
459 if (verbosity > 0 && len > sizeof *opo) {
460 char *packet = ofp_packet_to_string(
461 (uint8_t *)opo->actions + actions_len, data_len, data_len);
462 ds_put_char(string, '\n');
463 ds_put_cstr(string, packet);
467 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
469 ds_put_char(string, '\n');
472 /* qsort comparison function. */
474 compare_ports(const void *a_, const void *b_)
476 const struct ofp_phy_port *a = a_;
477 const struct ofp_phy_port *b = b_;
478 uint16_t ap = ntohs(a->port_no);
479 uint16_t bp = ntohs(b->port_no);
481 return ap < bp ? -1 : ap > bp;
484 static void ofp_print_port_features(struct ds *string, uint32_t features)
487 ds_put_cstr(string, "Unsupported\n");
490 if (features & OFPPF_10MB_HD) {
491 ds_put_cstr(string, "10MB-HD ");
493 if (features & OFPPF_10MB_FD) {
494 ds_put_cstr(string, "10MB-FD ");
496 if (features & OFPPF_100MB_HD) {
497 ds_put_cstr(string, "100MB-HD ");
499 if (features & OFPPF_100MB_FD) {
500 ds_put_cstr(string, "100MB-FD ");
502 if (features & OFPPF_1GB_HD) {
503 ds_put_cstr(string, "1GB-HD ");
505 if (features & OFPPF_1GB_FD) {
506 ds_put_cstr(string, "1GB-FD ");
508 if (features & OFPPF_10GB_FD) {
509 ds_put_cstr(string, "10GB-FD ");
511 if (features & OFPPF_COPPER) {
512 ds_put_cstr(string, "COPPER ");
514 if (features & OFPPF_FIBER) {
515 ds_put_cstr(string, "FIBER ");
517 if (features & OFPPF_AUTONEG) {
518 ds_put_cstr(string, "AUTO_NEG ");
520 if (features & OFPPF_PAUSE) {
521 ds_put_cstr(string, "AUTO_PAUSE ");
523 if (features & OFPPF_PAUSE_ASYM) {
524 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
526 ds_put_char(string, '\n');
530 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
532 uint8_t name[OFP_MAX_PORT_NAME_LEN];
535 memcpy(name, port->name, sizeof name);
536 for (j = 0; j < sizeof name - 1; j++) {
537 if (!isprint(name[j])) {
543 ds_put_char(string, ' ');
544 ofp_print_port_name(string, ntohs(port->port_no));
545 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
546 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
549 ds_put_format(string, " current: ");
550 ofp_print_port_features(string, ntohl(port->curr));
552 if (port->advertised) {
553 ds_put_format(string, " advertised: ");
554 ofp_print_port_features(string, ntohl(port->advertised));
556 if (port->supported) {
557 ds_put_format(string, " supported: ");
558 ofp_print_port_features(string, ntohl(port->supported));
561 ds_put_format(string, " peer: ");
562 ofp_print_port_features(string, ntohl(port->peer));
566 /* Pretty-print the struct ofp_switch_features of 'len' bytes at 'oh' to
567 * 'string' at the given 'verbosity' level. */
569 ofp_print_switch_features(struct ds *string, const void *oh, size_t len,
570 int verbosity OVS_UNUSED)
572 const struct ofp_switch_features *osf = oh;
573 struct ofp_phy_port *port_list;
577 ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
578 osf->header.version, ntohll(osf->datapath_id));
579 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
580 ntohl(osf->n_buffers));
581 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
582 ntohl(osf->capabilities), ntohl(osf->actions));
584 if (ntohs(osf->header.length) >= sizeof *osf) {
585 len = MIN(len, ntohs(osf->header.length));
587 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
589 port_list = xmemdup(osf->ports, len - sizeof *osf);
590 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
591 for (i = 0; i < n_ports; i++) {
592 ofp_print_phy_port(string, &port_list[i]);
597 /* Pretty-print the struct ofp_switch_config of 'len' bytes at 'oh' to 'string'
598 * at the given 'verbosity' level. */
600 ofp_print_switch_config(struct ds *string, const void *oh,
601 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
603 const struct ofp_switch_config *osc = oh;
606 flags = ntohs(osc->flags);
608 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
611 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
614 static void print_wild(struct ds *string, const char *leader, int is_wild,
615 int verbosity, const char *format, ...)
616 __attribute__((format(printf, 5, 6)));
618 static void print_wild(struct ds *string, const char *leader, int is_wild,
619 int verbosity, const char *format, ...)
621 if (is_wild && verbosity < 2) {
624 ds_put_cstr(string, leader);
628 va_start(args, format);
629 ds_put_format_valist(string, format, args);
632 ds_put_char(string, '*');
634 ds_put_char(string, ',');
638 print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
639 uint32_t wild_bits, int verbosity)
641 if (wild_bits >= 32 && verbosity < 2) {
644 ds_put_cstr(string, leader);
645 if (wild_bits < 32) {
646 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
648 ds_put_format(string, "/%d", 32 - wild_bits);
651 ds_put_char(string, '*');
653 ds_put_char(string, ',');
657 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
659 char *s = ofp_match_to_string(om, verbosity);
665 ofp_match_to_string(const struct ofp_match *om, int verbosity)
667 struct ds f = DS_EMPTY_INITIALIZER;
668 uint32_t w = ntohl(om->wildcards);
669 bool skip_type = false;
670 bool skip_proto = false;
672 if (!(w & OFPFW_DL_TYPE)) {
674 if (om->dl_type == htons(ETH_TYPE_IP)) {
675 if (!(w & OFPFW_NW_PROTO)) {
677 if (om->nw_proto == IP_TYPE_ICMP) {
678 ds_put_cstr(&f, "icmp,");
679 } else if (om->nw_proto == IP_TYPE_TCP) {
680 ds_put_cstr(&f, "tcp,");
681 } else if (om->nw_proto == IP_TYPE_UDP) {
682 ds_put_cstr(&f, "udp,");
684 ds_put_cstr(&f, "ip,");
688 ds_put_cstr(&f, "ip,");
690 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
691 ds_put_cstr(&f, "arp,");
696 if (w & NXFW_TUN_ID) {
697 ds_put_cstr(&f, "tun_id_wild,");
699 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
700 "%d", ntohs(om->in_port));
701 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
702 "%d", ntohs(om->dl_vlan));
703 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
704 "%d", om->dl_vlan_pcp);
705 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
706 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
707 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
708 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
710 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
711 "0x%04x", ntohs(om->dl_type));
713 print_ip_netmask(&f, "nw_src=", om->nw_src,
714 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
715 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
716 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
718 if (om->dl_type == htons(ETH_TYPE_ARP)) {
719 print_wild(&f, "opcode=", w & OFPFW_NW_PROTO, verbosity,
722 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
724 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
728 if (om->nw_proto == IP_TYPE_ICMP) {
729 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
730 "%d", ntohs(om->icmp_type));
731 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
732 "%d", ntohs(om->icmp_code));
734 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
735 "%d", ntohs(om->tp_src));
736 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
737 "%d", ntohs(om->tp_dst));
742 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
743 * at the given 'verbosity' level. */
745 ofp_print_flow_mod(struct ds *string, const void *oh, size_t len,
748 const struct ofp_flow_mod *ofm = oh;
750 ofp_print_match(string, &ofm->match, verbosity);
751 switch (ntohs(ofm->command)) {
753 ds_put_cstr(string, " ADD: ");
756 ds_put_cstr(string, " MOD: ");
758 case OFPFC_MODIFY_STRICT:
759 ds_put_cstr(string, " MOD_STRICT: ");
762 ds_put_cstr(string, " DEL: ");
764 case OFPFC_DELETE_STRICT:
765 ds_put_cstr(string, " DEL_STRICT: ");
768 ds_put_format(string, " cmd:%d ", ntohs(ofm->command));
770 ds_put_format(string, "cookie:0x%"PRIx64" idle:%d hard:%d pri:%d "
771 "buf:%#x flags:%"PRIx16" ", ntohll(ofm->cookie),
772 ntohs(ofm->idle_timeout), ntohs(ofm->hard_timeout),
773 ofm->match.wildcards ? ntohs(ofm->priority) : (uint16_t)-1,
774 ntohl(ofm->buffer_id), ntohs(ofm->flags));
775 ofp_print_actions(string, ofm->actions,
776 len - offsetof(struct ofp_flow_mod, actions));
777 ds_put_char(string, '\n');
780 /* Pretty-print the OFPT_FLOW_REMOVED packet of 'len' bytes at 'oh' to 'string'
781 * at the given 'verbosity' level. */
783 ofp_print_flow_removed(struct ds *string, const void *oh,
784 size_t len OVS_UNUSED, int verbosity)
786 const struct ofp_flow_removed *ofr = oh;
788 ofp_print_match(string, &ofr->match, verbosity);
789 ds_put_cstr(string, " reason=");
790 switch (ofr->reason) {
791 case OFPRR_IDLE_TIMEOUT:
792 ds_put_cstr(string, "idle");
794 case OFPRR_HARD_TIMEOUT:
795 ds_put_cstr(string, "hard");
798 ds_put_cstr(string, "delete");
801 ds_put_format(string, "**%"PRIu8"**", ofr->reason);
804 ds_put_format(string,
805 " cookie0x%"PRIx64" pri%"PRIu16" secs%"PRIu32" nsecs%"PRIu32
806 " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
808 ofr->match.wildcards ? ntohs(ofr->priority) : (uint16_t)-1,
809 ntohl(ofr->duration_sec), ntohl(ofr->duration_nsec),
810 ntohs(ofr->idle_timeout), ntohll(ofr->packet_count),
811 ntohll(ofr->byte_count));
815 ofp_print_port_mod(struct ds *string, const void *oh, size_t len OVS_UNUSED,
816 int verbosity OVS_UNUSED)
818 const struct ofp_port_mod *opm = oh;
820 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
821 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
822 ntohl(opm->config), ntohl(opm->mask));
823 ds_put_format(string, " advertise: ");
824 if (opm->advertise) {
825 ofp_print_port_features(string, ntohl(opm->advertise));
827 ds_put_format(string, "UNCHANGED\n");
837 static const struct error_type error_types[] = {
838 #define ERROR_TYPE(TYPE) {TYPE, -1, #TYPE}
839 #define ERROR_CODE(TYPE, CODE) {TYPE, CODE, #CODE}
840 ERROR_TYPE(OFPET_HELLO_FAILED),
841 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_INCOMPATIBLE),
842 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_EPERM),
844 ERROR_TYPE(OFPET_BAD_REQUEST),
845 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
846 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE),
847 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT),
848 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR),
849 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE),
850 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_EPERM),
851 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN),
852 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_EMPTY),
853 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_UNKNOWN),
855 ERROR_TYPE(OFPET_BAD_ACTION),
856 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE),
857 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_LEN),
858 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR),
859 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE),
860 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT),
861 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT),
862 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_EPERM),
863 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_TOO_MANY),
865 ERROR_TYPE(OFPET_FLOW_MOD_FAILED),
866 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL),
867 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP),
868 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_EPERM),
869 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_EMERG_TIMEOUT),
870 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND),
872 ERROR_TYPE(OFPET_PORT_MOD_FAILED),
873 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT),
874 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR)
876 #define N_ERROR_TYPES ARRAY_SIZE(error_types)
879 lookup_error_type(int type)
881 const struct error_type *t;
883 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
884 if (t->type == type && t->code == -1) {
892 lookup_error_code(int type, int code)
894 const struct error_type *t;
896 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
897 if (t->type == type && t->code == code) {
904 /* Pretty-print the OFPT_ERROR packet of 'len' bytes at 'oh' to 'string'
905 * at the given 'verbosity' level. */
907 ofp_print_error_msg(struct ds *string, const void *oh, size_t len,
908 int verbosity OVS_UNUSED)
910 const struct ofp_error_msg *oem = oh;
911 int type = ntohs(oem->type);
912 int code = ntohs(oem->code);
915 ds_put_format(string, " type%d(%s) code%d(%s) payload:\n",
916 type, lookup_error_type(type),
917 code, lookup_error_code(type, code));
920 case OFPET_HELLO_FAILED:
921 ds_put_printable(string, (char *) oem->data, len - sizeof *oem);
924 case OFPET_BAD_REQUEST:
925 s = ofp_to_string(oem->data, len - sizeof *oem, 1);
926 ds_put_cstr(string, s);
931 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
936 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'string'
937 * at the given 'verbosity' level. */
939 ofp_print_port_status(struct ds *string, const void *oh, size_t len OVS_UNUSED,
940 int verbosity OVS_UNUSED)
942 const struct ofp_port_status *ops = oh;
944 if (ops->reason == OFPPR_ADD) {
945 ds_put_format(string, " ADD:");
946 } else if (ops->reason == OFPPR_DELETE) {
947 ds_put_format(string, " DEL:");
948 } else if (ops->reason == OFPPR_MODIFY) {
949 ds_put_format(string, " MOD:");
952 ofp_print_phy_port(string, &ops->desc);
956 ofp_desc_stats_reply(struct ds *string, const void *body,
957 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
959 const struct ofp_desc_stats *ods = body;
961 ds_put_format(string, "Manufacturer: %.*s\n",
962 (int) sizeof ods->mfr_desc, ods->mfr_desc);
963 ds_put_format(string, "Hardware: %.*s\n",
964 (int) sizeof ods->hw_desc, ods->hw_desc);
965 ds_put_format(string, "Software: %.*s\n",
966 (int) sizeof ods->sw_desc, ods->sw_desc);
967 ds_put_format(string, "Serial Num: %.*s\n",
968 (int) sizeof ods->serial_num, ods->serial_num);
969 ds_put_format(string, "DP Description: %.*s\n",
970 (int) sizeof ods->dp_desc, ods->dp_desc);
974 ofp_flow_stats_request(struct ds *string, const void *oh,
975 size_t len OVS_UNUSED, int verbosity)
977 const struct ofp_flow_stats_request *fsr = oh;
979 if (fsr->table_id == 0xff) {
980 ds_put_format(string, " table_id=any, ");
982 ds_put_format(string, " table_id=%"PRIu8", ", fsr->table_id);
985 ofp_print_match(string, &fsr->match, verbosity);
989 ofp_flow_stats_reply(struct ds *string, const void *body_, size_t len,
992 const char *body = body_;
993 const char *pos = body;
995 const struct ofp_flow_stats *fs;
996 ptrdiff_t bytes_left = body + len - pos;
999 if (bytes_left < sizeof *fs) {
1000 if (bytes_left != 0) {
1001 ds_put_format(string, " ***%td leftover bytes at end***",
1007 fs = (const void *) pos;
1008 length = ntohs(fs->length);
1009 if (length < sizeof *fs) {
1010 ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
1011 length, sizeof *fs);
1013 } else if (length > bytes_left) {
1014 ds_put_format(string,
1015 " ***length=%zu but only %td bytes left***",
1016 length, bytes_left);
1018 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
1019 ds_put_format(string,
1020 " ***length=%zu has %zu bytes leftover in "
1023 (length - sizeof *fs) % sizeof fs->actions[0]);
1027 ds_put_format(string, " cookie=0x%"PRIx64", ", ntohll(fs->cookie));
1028 ds_put_format(string, "duration_sec=%"PRIu32"s, ",
1029 ntohl(fs->duration_sec));
1030 ds_put_format(string, "duration_nsec=%"PRIu32"ns, ",
1031 ntohl(fs->duration_nsec));
1032 ds_put_format(string, "table_id=%"PRIu8", ", fs->table_id);
1033 ds_put_format(string, "priority=%"PRIu16", ",
1034 fs->match.wildcards ? ntohs(fs->priority) : (uint16_t)-1);
1035 ds_put_format(string, "n_packets=%"PRIu64", ",
1036 ntohll(fs->packet_count));
1037 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
1038 if (fs->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
1039 ds_put_format(string, "idle_timeout=%"PRIu16",",
1040 ntohs(fs->idle_timeout));
1042 if (fs->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
1043 ds_put_format(string, "hard_timeout=%"PRIu16",",
1044 ntohs(fs->hard_timeout));
1046 ofp_print_match(string, &fs->match, verbosity);
1047 ofp_print_actions(string, fs->actions, length - sizeof *fs);
1048 ds_put_char(string, '\n');
1055 ofp_aggregate_stats_request(struct ds *string, const void *oh,
1056 size_t len OVS_UNUSED, int verbosity)
1058 const struct ofp_aggregate_stats_request *asr = oh;
1060 if (asr->table_id == 0xff) {
1061 ds_put_format(string, " table_id=any, ");
1063 ds_put_format(string, " table_id=%"PRIu8", ", asr->table_id);
1066 ofp_print_match(string, &asr->match, verbosity);
1070 ofp_aggregate_stats_reply(struct ds *string, const void *body_,
1071 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
1073 const struct ofp_aggregate_stats_reply *asr = body_;
1075 ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
1076 ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
1077 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1080 static void print_port_stat(struct ds *string, const char *leader,
1081 uint64_t stat, int more)
1083 ds_put_cstr(string, leader);
1085 ds_put_format(string, "%"PRIu64, stat);
1087 ds_put_char(string, '?');
1090 ds_put_cstr(string, ", ");
1092 ds_put_cstr(string, "\n");
1097 ofp_port_stats_request(struct ds *string, const void *body_,
1098 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
1100 const struct ofp_port_stats_request *psr = body_;
1101 ds_put_format(string, "port_no=%"PRIu16, ntohs(psr->port_no));
1105 ofp_port_stats_reply(struct ds *string, const void *body, size_t len,
1108 const struct ofp_port_stats *ps = body;
1109 size_t n = len / sizeof *ps;
1110 ds_put_format(string, " %zu ports\n", n);
1111 if (verbosity < 1) {
1116 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1118 ds_put_cstr(string, "rx ");
1119 print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
1120 print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
1121 print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
1122 print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
1123 print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
1124 print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
1125 print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
1127 ds_put_cstr(string, " tx ");
1128 print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
1129 print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
1130 print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
1131 print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
1132 print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
1137 ofp_table_stats_reply(struct ds *string, const void *body, size_t len,
1140 const struct ofp_table_stats *ts = body;
1141 size_t n = len / sizeof *ts;
1142 ds_put_format(string, " %zu tables\n", n);
1143 if (verbosity < 1) {
1148 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1149 strncpy(name, ts->name, sizeof name);
1150 name[OFP_MAX_TABLE_NAME_LEN] = '\0';
1152 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1153 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1154 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1155 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1156 ds_put_cstr(string, " ");
1157 ds_put_format(string, "lookup=%"PRIu64", ",
1158 ntohll(ts->lookup_count));
1159 ds_put_format(string, "matched=%"PRIu64"\n",
1160 ntohll(ts->matched_count));
1165 vendor_stat(struct ds *string, const void *body, size_t len,
1166 int verbosity OVS_UNUSED)
1168 ds_put_format(string, " vendor=%08"PRIx32, ntohl(*(uint32_t *) body));
1169 ds_put_format(string, " %zu bytes additional data",
1170 len - sizeof(uint32_t));
1173 enum stats_direction {
1179 print_stats(struct ds *string, int type, const void *body, size_t body_len,
1180 int verbosity, enum stats_direction direction)
1183 size_t min_body, max_body;
1184 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1190 struct stats_msg request;
1191 struct stats_msg reply;
1194 static const struct stats_type stats_types[] = {
1199 { 0, SIZE_MAX, ofp_desc_stats_reply },
1204 { sizeof(struct ofp_flow_stats_request),
1205 sizeof(struct ofp_flow_stats_request),
1206 ofp_flow_stats_request },
1207 { 0, SIZE_MAX, ofp_flow_stats_reply },
1212 { sizeof(struct ofp_aggregate_stats_request),
1213 sizeof(struct ofp_aggregate_stats_request),
1214 ofp_aggregate_stats_request },
1215 { sizeof(struct ofp_aggregate_stats_reply),
1216 sizeof(struct ofp_aggregate_stats_reply),
1217 ofp_aggregate_stats_reply },
1223 { 0, SIZE_MAX, ofp_table_stats_reply },
1228 { sizeof(struct ofp_port_stats_request),
1229 sizeof(struct ofp_port_stats_request),
1230 ofp_port_stats_request },
1231 { 0, SIZE_MAX, ofp_port_stats_reply },
1236 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1237 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1247 const struct stats_type *s;
1248 const struct stats_msg *m;
1250 if (type >= ARRAY_SIZE(stats_types) || !stats_types[type].name) {
1251 ds_put_format(string, " ***unknown type %d***", type);
1254 for (s = stats_types; s->type >= 0; s++) {
1255 if (s->type == type) {
1259 ds_put_format(string, " type=%d(%s)\n", type, s->name);
1261 m = direction == REQUEST ? &s->request : &s->reply;
1262 if (body_len < m->min_body || body_len > m->max_body) {
1263 ds_put_format(string, " ***body_len=%zu not in %zu...%zu***",
1264 body_len, m->min_body, m->max_body);
1268 m->printer(string, body, body_len, verbosity);
1273 ofp_stats_request(struct ds *string, const void *oh, size_t len, int verbosity)
1275 const struct ofp_stats_request *srq = oh;
1278 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1282 print_stats(string, ntohs(srq->type), srq->body,
1283 len - offsetof(struct ofp_stats_request, body),
1284 verbosity, REQUEST);
1288 ofp_stats_reply(struct ds *string, const void *oh, size_t len, int verbosity)
1290 const struct ofp_stats_reply *srp = oh;
1292 ds_put_cstr(string, " flags=");
1294 ds_put_cstr(string, "none");
1296 uint16_t flags = ntohs(srp->flags);
1297 if (flags & OFPSF_REPLY_MORE) {
1298 ds_put_cstr(string, "[more]");
1299 flags &= ~OFPSF_REPLY_MORE;
1302 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]", flags);
1306 print_stats(string, ntohs(srp->type), srp->body,
1307 len - offsetof(struct ofp_stats_reply, body),
1312 ofp_echo(struct ds *string, const void *oh, size_t len, int verbosity)
1314 const struct ofp_header *hdr = oh;
1316 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *hdr);
1317 if (verbosity > 1) {
1318 ds_put_hex_dump(string, hdr, len - sizeof *hdr, 0, true);
1322 struct openflow_packet {
1326 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1329 static const struct openflow_packet packets[] = {
1333 sizeof (struct ofp_header),
1337 OFPT_FEATURES_REQUEST,
1339 sizeof (struct ofp_header),
1343 OFPT_FEATURES_REPLY,
1345 sizeof (struct ofp_switch_features),
1346 ofp_print_switch_features,
1349 OFPT_GET_CONFIG_REQUEST,
1350 "get_config_request",
1351 sizeof (struct ofp_header),
1355 OFPT_GET_CONFIG_REPLY,
1357 sizeof (struct ofp_switch_config),
1358 ofp_print_switch_config,
1363 sizeof (struct ofp_switch_config),
1364 ofp_print_switch_config,
1369 offsetof(struct ofp_packet_in, data),
1375 sizeof (struct ofp_packet_out),
1381 sizeof (struct ofp_flow_mod),
1387 sizeof (struct ofp_flow_removed),
1388 ofp_print_flow_removed,
1393 sizeof (struct ofp_port_mod),
1399 sizeof (struct ofp_port_status),
1400 ofp_print_port_status
1405 sizeof (struct ofp_error_msg),
1406 ofp_print_error_msg,
1411 sizeof (struct ofp_stats_request),
1417 sizeof (struct ofp_stats_reply),
1423 sizeof (struct ofp_header),
1429 sizeof (struct ofp_header),
1435 sizeof (struct ofp_vendor_header),
1439 OFPT_BARRIER_REQUEST,
1441 sizeof (struct ofp_header),
1447 sizeof (struct ofp_header),
1452 /* Composes and returns a string representing the OpenFlow packet of 'len'
1453 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1454 * verbosity and higher numbers increase verbosity. The caller is responsible
1455 * for freeing the string. */
1457 ofp_to_string(const void *oh_, size_t len, int verbosity)
1459 struct ds string = DS_EMPTY_INITIALIZER;
1460 const struct ofp_header *oh = oh_;
1461 const struct openflow_packet *pkt;
1463 if (len < sizeof(struct ofp_header)) {
1464 ds_put_cstr(&string, "OpenFlow packet too short:\n");
1465 ds_put_hex_dump(&string, oh, len, 0, true);
1466 return ds_cstr(&string);
1467 } else if (oh->version != OFP_VERSION) {
1468 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", oh->version);
1469 ds_put_hex_dump(&string, oh, len, 0, true);
1470 return ds_cstr(&string);
1473 for (pkt = packets; ; pkt++) {
1474 if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1475 ds_put_format(&string, "Unknown OpenFlow packet type %"PRIu8":\n",
1477 ds_put_hex_dump(&string, oh, len, 0, true);
1478 return ds_cstr(&string);
1479 } else if (oh->type == pkt->type) {
1484 ds_put_format(&string, "%s (xid=0x%"PRIx32"):", pkt->name, oh->xid);
1486 if (ntohs(oh->length) > len)
1487 ds_put_format(&string, " (***truncated to %zu bytes from %"PRIu16"***)",
1488 len, ntohs(oh->length));
1489 else if (ntohs(oh->length) < len) {
1490 ds_put_format(&string, " (***only uses %"PRIu16" bytes out of %zu***)\n",
1491 ntohs(oh->length), len);
1492 len = ntohs(oh->length);
1495 if (len < pkt->min_size) {
1496 ds_put_format(&string, " (***length=%zu < min_size=%zu***)\n",
1497 len, pkt->min_size);
1498 } else if (!pkt->printer) {
1499 if (len > sizeof *oh) {
1500 ds_put_format(&string, " length=%"PRIu16" (decoder not implemented)\n",
1504 pkt->printer(&string, oh, len, verbosity);
1506 if (verbosity >= 3) {
1507 ds_put_hex_dump(&string, oh, len, 0, true);
1509 if (string.string[string.length - 1] != '\n') {
1510 ds_put_char(&string, '\n');
1512 return ds_cstr(&string);
1515 /* Returns the name for the specified OpenFlow message type as a string,
1516 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1517 * hex number, e.g. "0x55".
1519 * The caller must free the returned string when it is no longer needed. */
1521 ofp_message_type_to_string(uint8_t type)
1523 struct ds s = DS_EMPTY_INITIALIZER;
1524 const struct openflow_packet *pkt;
1525 for (pkt = packets; ; pkt++) {
1526 if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1527 ds_put_format(&s, "0x%02"PRIx8, type);
1529 } else if (type == pkt->type) {
1532 ds_put_cstr(&s, "OFPT_");
1533 for (p = pkt->name; *p; p++) {
1534 ds_put_char(&s, toupper((unsigned char) *p));
1543 print_and_free(FILE *stream, char *string)
1545 fputs(string, stream);
1549 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1550 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1551 * numbers increase verbosity. */
1553 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1555 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1558 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1559 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1560 * the Ethernet frame (of which 'len' bytes were captured).
1562 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1564 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1566 print_and_free(stream, ofp_packet_to_string(data, len, total_len));