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, ntohs(op->in_port), &flow);
138 flow_to_match(&flow, 0, &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));
197 ds_put_format(string, "***unknown Nicira action:%d***\n",
198 ntohs(nah->subtype));
203 ofp_print_action(struct ds *string, const struct ofp_action_header *ah,
209 struct openflow_action {
214 const struct openflow_action of_actions[] = {
216 sizeof(struct ofp_action_output),
217 sizeof(struct ofp_action_output),
219 [OFPAT_SET_VLAN_VID] = {
220 sizeof(struct ofp_action_vlan_vid),
221 sizeof(struct ofp_action_vlan_vid),
223 [OFPAT_SET_VLAN_PCP] = {
224 sizeof(struct ofp_action_vlan_pcp),
225 sizeof(struct ofp_action_vlan_pcp),
227 [OFPAT_STRIP_VLAN] = {
228 sizeof(struct ofp_action_header),
229 sizeof(struct ofp_action_header),
231 [OFPAT_SET_DL_SRC] = {
232 sizeof(struct ofp_action_dl_addr),
233 sizeof(struct ofp_action_dl_addr),
235 [OFPAT_SET_DL_DST] = {
236 sizeof(struct ofp_action_dl_addr),
237 sizeof(struct ofp_action_dl_addr),
239 [OFPAT_SET_NW_SRC] = {
240 sizeof(struct ofp_action_nw_addr),
241 sizeof(struct ofp_action_nw_addr),
243 [OFPAT_SET_NW_DST] = {
244 sizeof(struct ofp_action_nw_addr),
245 sizeof(struct ofp_action_nw_addr),
247 [OFPAT_SET_TP_SRC] = {
248 sizeof(struct ofp_action_tp_port),
249 sizeof(struct ofp_action_tp_port),
251 [OFPAT_SET_TP_DST] = {
252 sizeof(struct ofp_action_tp_port),
253 sizeof(struct ofp_action_tp_port),
255 /* OFPAT_VENDOR is not here, since it would blow up the array size. */
258 if (actions_len < sizeof *ah) {
259 ds_put_format(string, "***action array too short for next action***\n");
263 type = ntohs(ah->type);
264 len = ntohs(ah->len);
265 if (actions_len < len) {
266 ds_put_format(string, "***truncated action %"PRIu16"***\n", type);
270 if ((len % 8) != 0) {
271 ds_put_format(string,
272 "***action %"PRIu16" length not a multiple of 8***\n",
277 if (type < ARRAY_SIZE(of_actions)) {
278 const struct openflow_action *act = &of_actions[type];
279 if ((len < act->min_size) || (len > act->max_size)) {
280 ds_put_format(string,
281 "***action %"PRIu16" wrong length: %zu***\n", type, len);
288 struct ofp_action_output *oa = (struct ofp_action_output *)ah;
289 uint16_t port = ntohs(oa->port);
290 if (port < OFPP_MAX) {
291 ds_put_format(string, "output:%"PRIu16, port);
293 ofp_print_port_name(string, port);
294 if (port == OFPP_CONTROLLER) {
296 ds_put_format(string, ":%"PRIu16, ntohs(oa->max_len));
298 ds_put_cstr(string, ":all");
305 case OFPAT_SET_VLAN_VID: {
306 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
307 ds_put_format(string, "mod_vlan_vid:%"PRIu16, ntohs(va->vlan_vid));
311 case OFPAT_SET_VLAN_PCP: {
312 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
313 ds_put_format(string, "mod_vlan_pcp:%"PRIu8, va->vlan_pcp);
317 case OFPAT_STRIP_VLAN:
318 ds_put_cstr(string, "strip_vlan");
321 case OFPAT_SET_DL_SRC: {
322 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
323 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
324 ETH_ADDR_ARGS(da->dl_addr));
328 case OFPAT_SET_DL_DST: {
329 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
330 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
331 ETH_ADDR_ARGS(da->dl_addr));
335 case OFPAT_SET_NW_SRC: {
336 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
337 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&na->nw_addr));
341 case OFPAT_SET_NW_DST: {
342 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
343 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&na->nw_addr));
347 case OFPAT_SET_TP_SRC: {
348 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
349 ds_put_format(string, "mod_tp_src:%d", ntohs(ta->tp_port));
353 case OFPAT_SET_TP_DST: {
354 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
355 ds_put_format(string, "mod_tp_dst:%d", ntohs(ta->tp_port));
360 struct ofp_action_vendor_header *avh
361 = (struct ofp_action_vendor_header *)ah;
362 if (len < sizeof *avh) {
363 ds_put_format(string, "***ofpat_vendor truncated***\n");
366 if (avh->vendor == htonl(NX_VENDOR_ID)) {
367 ofp_print_nx_action(string, (struct nx_action_header *)avh);
369 ds_put_format(string, "vendor action:0x%x", ntohl(avh->vendor));
375 ds_put_format(string, "(decoder %"PRIu16" not implemented)", type);
383 ofp_print_actions(struct ds *string, const struct ofp_action_header *action,
386 uint8_t *p = (uint8_t *)action;
389 ds_put_cstr(string, "actions=");
391 ds_put_cstr(string, "drop");
393 while (actions_len > 0) {
395 ds_put_cstr(string, ",");
397 len = ofp_print_action(string, (struct ofp_action_header *)p,
407 /* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'string'
408 * at the given 'verbosity' level. */
409 static void ofp_packet_out(struct ds *string, const void *oh, size_t len,
412 const struct ofp_packet_out *opo = oh;
413 size_t actions_len = ntohs(opo->actions_len);
415 ds_put_cstr(string, " in_port=");
416 ofp_print_port_name(string, ntohs(opo->in_port));
418 ds_put_format(string, " actions_len=%zu ", actions_len);
419 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
420 ds_put_format(string, "***packet too short for action length***\n");
423 ofp_print_actions(string, opo->actions, actions_len);
425 if (ntohl(opo->buffer_id) == UINT32_MAX) {
426 int data_len = len - sizeof *opo - actions_len;
427 ds_put_format(string, " data_len=%d", data_len);
428 if (verbosity > 0 && len > sizeof *opo) {
429 char *packet = ofp_packet_to_string(
430 (uint8_t *)opo->actions + actions_len, data_len, data_len);
431 ds_put_char(string, '\n');
432 ds_put_cstr(string, packet);
436 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
438 ds_put_char(string, '\n');
441 /* qsort comparison function. */
443 compare_ports(const void *a_, const void *b_)
445 const struct ofp_phy_port *a = a_;
446 const struct ofp_phy_port *b = b_;
447 uint16_t ap = ntohs(a->port_no);
448 uint16_t bp = ntohs(b->port_no);
450 return ap < bp ? -1 : ap > bp;
453 static void ofp_print_port_features(struct ds *string, uint32_t features)
456 ds_put_cstr(string, "Unsupported\n");
459 if (features & OFPPF_10MB_HD) {
460 ds_put_cstr(string, "10MB-HD ");
462 if (features & OFPPF_10MB_FD) {
463 ds_put_cstr(string, "10MB-FD ");
465 if (features & OFPPF_100MB_HD) {
466 ds_put_cstr(string, "100MB-HD ");
468 if (features & OFPPF_100MB_FD) {
469 ds_put_cstr(string, "100MB-FD ");
471 if (features & OFPPF_1GB_HD) {
472 ds_put_cstr(string, "1GB-HD ");
474 if (features & OFPPF_1GB_FD) {
475 ds_put_cstr(string, "1GB-FD ");
477 if (features & OFPPF_10GB_FD) {
478 ds_put_cstr(string, "10GB-FD ");
480 if (features & OFPPF_COPPER) {
481 ds_put_cstr(string, "COPPER ");
483 if (features & OFPPF_FIBER) {
484 ds_put_cstr(string, "FIBER ");
486 if (features & OFPPF_AUTONEG) {
487 ds_put_cstr(string, "AUTO_NEG ");
489 if (features & OFPPF_PAUSE) {
490 ds_put_cstr(string, "AUTO_PAUSE ");
492 if (features & OFPPF_PAUSE_ASYM) {
493 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
495 ds_put_char(string, '\n');
499 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
501 uint8_t name[OFP_MAX_PORT_NAME_LEN];
504 memcpy(name, port->name, sizeof name);
505 for (j = 0; j < sizeof name - 1; j++) {
506 if (!isprint(name[j])) {
512 ds_put_char(string, ' ');
513 ofp_print_port_name(string, ntohs(port->port_no));
514 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
515 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
518 ds_put_format(string, " current: ");
519 ofp_print_port_features(string, ntohl(port->curr));
521 if (port->advertised) {
522 ds_put_format(string, " advertised: ");
523 ofp_print_port_features(string, ntohl(port->advertised));
525 if (port->supported) {
526 ds_put_format(string, " supported: ");
527 ofp_print_port_features(string, ntohl(port->supported));
530 ds_put_format(string, " peer: ");
531 ofp_print_port_features(string, ntohl(port->peer));
535 /* Pretty-print the struct ofp_switch_features of 'len' bytes at 'oh' to
536 * 'string' at the given 'verbosity' level. */
538 ofp_print_switch_features(struct ds *string, const void *oh, size_t len,
539 int verbosity OVS_UNUSED)
541 const struct ofp_switch_features *osf = oh;
542 struct ofp_phy_port *port_list;
546 ds_put_format(string, " ver:0x%x, dpid:%"PRIx64"\n",
547 osf->header.version, ntohll(osf->datapath_id));
548 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
549 ntohl(osf->n_buffers));
550 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
551 ntohl(osf->capabilities), ntohl(osf->actions));
553 if (ntohs(osf->header.length) >= sizeof *osf) {
554 len = MIN(len, ntohs(osf->header.length));
556 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
558 port_list = xmemdup(osf->ports, len - sizeof *osf);
559 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
560 for (i = 0; i < n_ports; i++) {
561 ofp_print_phy_port(string, &port_list[i]);
566 /* Pretty-print the struct ofp_switch_config of 'len' bytes at 'oh' to 'string'
567 * at the given 'verbosity' level. */
569 ofp_print_switch_config(struct ds *string, const void *oh,
570 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
572 const struct ofp_switch_config *osc = oh;
575 flags = ntohs(osc->flags);
576 if (flags & OFPC_SEND_FLOW_EXP) {
577 flags &= ~OFPC_SEND_FLOW_EXP;
578 ds_put_format(string, " (sending flow expirations)");
581 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
584 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
587 static void print_wild(struct ds *string, const char *leader, int is_wild,
588 int verbosity, const char *format, ...)
589 __attribute__((format(printf, 5, 6)));
591 static void print_wild(struct ds *string, const char *leader, int is_wild,
592 int verbosity, const char *format, ...)
594 if (is_wild && verbosity < 2) {
597 ds_put_cstr(string, leader);
601 va_start(args, format);
602 ds_put_format_valist(string, format, args);
605 ds_put_char(string, '*');
607 ds_put_char(string, ',');
611 print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
612 uint32_t wild_bits, int verbosity)
614 if (wild_bits >= 32 && verbosity < 2) {
617 ds_put_cstr(string, leader);
618 if (wild_bits < 32) {
619 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
621 ds_put_format(string, "/%d", 32 - wild_bits);
624 ds_put_char(string, '*');
626 ds_put_char(string, ',');
630 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
632 char *s = ofp_match_to_string(om, verbosity);
638 ofp_match_to_string(const struct ofp_match *om, int verbosity)
640 struct ds f = DS_EMPTY_INITIALIZER;
641 uint32_t w = ntohl(om->wildcards);
642 bool skip_type = false;
643 bool skip_proto = false;
645 if (!(w & OFPFW_DL_TYPE)) {
647 if (om->dl_type == htons(ETH_TYPE_IP)) {
648 if (!(w & OFPFW_NW_PROTO)) {
650 if (om->nw_proto == IP_TYPE_ICMP) {
651 ds_put_cstr(&f, "icmp,");
652 } else if (om->nw_proto == IP_TYPE_TCP) {
653 ds_put_cstr(&f, "tcp,");
654 } else if (om->nw_proto == IP_TYPE_UDP) {
655 ds_put_cstr(&f, "udp,");
657 ds_put_cstr(&f, "ip,");
661 ds_put_cstr(&f, "ip,");
663 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
664 ds_put_cstr(&f, "arp,");
669 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
670 "%d", ntohs(om->in_port));
671 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
672 "0x%04x", ntohs(om->dl_vlan));
673 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
674 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
675 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
676 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
678 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
679 "0x%04x", ntohs(om->dl_type));
681 print_ip_netmask(&f, "nw_src=", om->nw_src,
682 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
683 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
684 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
686 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
689 if (om->nw_proto == IP_TYPE_ICMP) {
690 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
691 "%d", ntohs(om->icmp_type));
692 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
693 "%d", ntohs(om->icmp_code));
695 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
696 "%d", ntohs(om->tp_src));
697 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
698 "%d", ntohs(om->tp_dst));
703 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
704 * at the given 'verbosity' level. */
706 ofp_print_flow_mod(struct ds *string, const void *oh, size_t len,
709 const struct ofp_flow_mod *ofm = oh;
711 ofp_print_match(string, &ofm->match, verbosity);
712 switch (ntohs(ofm->command)) {
714 ds_put_cstr(string, " ADD: ");
717 ds_put_cstr(string, " MOD: ");
719 case OFPFC_MODIFY_STRICT:
720 ds_put_cstr(string, " MOD_STRICT: ");
723 ds_put_cstr(string, " DEL: ");
725 case OFPFC_DELETE_STRICT:
726 ds_put_cstr(string, " DEL_STRICT: ");
729 ds_put_format(string, " cmd:%d ", ntohs(ofm->command));
731 ds_put_format(string, "idle:%d hard:%d pri:%d buf:%#x",
732 ntohs(ofm->idle_timeout), ntohs(ofm->hard_timeout),
733 ofm->match.wildcards ? ntohs(ofm->priority) : (uint16_t)-1,
734 ntohl(ofm->buffer_id));
735 ofp_print_actions(string, ofm->actions,
736 len - offsetof(struct ofp_flow_mod, actions));
737 ds_put_char(string, '\n');
740 /* Pretty-print the OFPT_FLOW_EXPIRED packet of 'len' bytes at 'oh' to 'string'
741 * at the given 'verbosity' level. */
743 ofp_print_flow_expired(struct ds *string, const void *oh,
744 size_t len OVS_UNUSED, int verbosity)
746 const struct ofp_flow_expired *ofe = oh;
748 ofp_print_match(string, &ofe->match, verbosity);
749 ds_put_cstr(string, " reason=");
750 switch (ofe->reason) {
751 case OFPER_IDLE_TIMEOUT:
752 ds_put_cstr(string, "idle");
754 case OFPER_HARD_TIMEOUT:
755 ds_put_cstr(string, "hard");
758 ds_put_format(string, "**%"PRIu8"**", ofe->reason);
761 ds_put_format(string,
762 " pri%"PRIu16" secs%"PRIu32" pkts%"PRIu64" bytes%"PRIu64"\n",
763 ofe->match.wildcards ? ntohs(ofe->priority) : (uint16_t)-1,
764 ntohl(ofe->duration), ntohll(ofe->packet_count),
765 ntohll(ofe->byte_count));
769 ofp_print_port_mod(struct ds *string, const void *oh, size_t len OVS_UNUSED,
770 int verbosity OVS_UNUSED)
772 const struct ofp_port_mod *opm = oh;
774 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
775 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
776 ntohl(opm->config), ntohl(opm->mask));
777 ds_put_format(string, " advertise: ");
778 if (opm->advertise) {
779 ofp_print_port_features(string, ntohl(opm->advertise));
781 ds_put_format(string, "UNCHANGED\n");
791 static const struct error_type error_types[] = {
792 #define ERROR_TYPE(TYPE) {TYPE, -1, #TYPE}
793 #define ERROR_CODE(TYPE, CODE) {TYPE, CODE, #CODE}
794 ERROR_TYPE(OFPET_HELLO_FAILED),
795 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_INCOMPATIBLE),
797 ERROR_TYPE(OFPET_BAD_REQUEST),
798 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
799 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE),
800 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT),
801 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR),
802 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE),
803 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_LENGTH),
804 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_EMPTY),
805 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_COOKIE),
807 ERROR_TYPE(OFPET_BAD_ACTION),
808 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE),
809 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_LEN),
810 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR),
811 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE),
812 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT),
813 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT),
814 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_TOO_MANY),
816 ERROR_TYPE(OFPET_FLOW_MOD_FAILED),
817 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL),
818 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND),
820 ERROR_TYPE(OFPET_PORT_MOD_FAILED),
821 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT),
822 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR)
824 #define N_ERROR_TYPES ARRAY_SIZE(error_types)
827 lookup_error_type(int type)
829 const struct error_type *t;
831 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
832 if (t->type == type && t->code == -1) {
840 lookup_error_code(int type, int code)
842 const struct error_type *t;
844 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
845 if (t->type == type && t->code == code) {
852 /* Pretty-print the OFPT_ERROR packet of 'len' bytes at 'oh' to 'string'
853 * at the given 'verbosity' level. */
855 ofp_print_error_msg(struct ds *string, const void *oh, size_t len,
856 int verbosity OVS_UNUSED)
858 const struct ofp_error_msg *oem = oh;
859 int type = ntohs(oem->type);
860 int code = ntohs(oem->code);
863 ds_put_format(string, " type%d(%s) code%d(%s) payload:\n",
864 type, lookup_error_type(type),
865 code, lookup_error_code(type, code));
868 case OFPET_HELLO_FAILED:
869 ds_put_printable(string, (char *) oem->data, len - sizeof *oem);
872 case OFPET_BAD_REQUEST:
873 s = ofp_to_string(oem->data, len - sizeof *oem, 1);
874 ds_put_cstr(string, s);
879 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
884 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'string'
885 * at the given 'verbosity' level. */
887 ofp_print_port_status(struct ds *string, const void *oh, size_t len OVS_UNUSED,
888 int verbosity OVS_UNUSED)
890 const struct ofp_port_status *ops = oh;
892 if (ops->reason == OFPPR_ADD) {
893 ds_put_format(string, " ADD:");
894 } else if (ops->reason == OFPPR_DELETE) {
895 ds_put_format(string, " DEL:");
896 } else if (ops->reason == OFPPR_MODIFY) {
897 ds_put_format(string, " MOD:");
900 ofp_print_phy_port(string, &ops->desc);
904 ofp_desc_stats_reply(struct ds *string, const void *body,
905 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
907 const struct ofp_desc_stats *ods = body;
909 ds_put_format(string, "Manufacturer: %s\n", ods->mfr_desc);
910 ds_put_format(string, "Hardware: %s\n", ods->hw_desc);
911 ds_put_format(string, "Software: %s\n", ods->sw_desc);
912 ds_put_format(string, "Serial Num: %s\n", ods->serial_num);
916 ofp_flow_stats_request(struct ds *string, const void *oh,
917 size_t len OVS_UNUSED, int verbosity)
919 const struct ofp_flow_stats_request *fsr = oh;
921 if (fsr->table_id == 0xff) {
922 ds_put_format(string, " table_id=any, ");
924 ds_put_format(string, " table_id=%"PRIu8", ", fsr->table_id);
927 ofp_print_match(string, &fsr->match, verbosity);
931 ofp_flow_stats_reply(struct ds *string, const void *body_, size_t len,
934 const char *body = body_;
935 const char *pos = body;
937 const struct ofp_flow_stats *fs;
938 ptrdiff_t bytes_left = body + len - pos;
941 if (bytes_left < sizeof *fs) {
942 if (bytes_left != 0) {
943 ds_put_format(string, " ***%td leftover bytes at end***",
949 fs = (const void *) pos;
950 length = ntohs(fs->length);
951 if (length < sizeof *fs) {
952 ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
955 } else if (length > bytes_left) {
956 ds_put_format(string,
957 " ***length=%zu but only %td bytes left***",
960 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
961 ds_put_format(string,
962 " ***length=%zu has %zu bytes leftover in "
965 (length - sizeof *fs) % sizeof fs->actions[0]);
969 ds_put_format(string, " duration=%"PRIu32"s, ", ntohl(fs->duration));
970 ds_put_format(string, "table_id=%"PRIu8", ", fs->table_id);
971 ds_put_format(string, "priority=%"PRIu16", ",
972 fs->match.wildcards ? ntohs(fs->priority) : (uint16_t)-1);
973 ds_put_format(string, "n_packets=%"PRIu64", ",
974 ntohll(fs->packet_count));
975 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
976 if (fs->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
977 ds_put_format(string, "idle_timeout=%"PRIu16",",
978 ntohs(fs->idle_timeout));
980 if (fs->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
981 ds_put_format(string, "hard_timeout=%"PRIu16",",
982 ntohs(fs->hard_timeout));
984 ofp_print_match(string, &fs->match, verbosity);
985 ofp_print_actions(string, fs->actions, length - sizeof *fs);
986 ds_put_char(string, '\n');
993 ofp_aggregate_stats_request(struct ds *string, const void *oh,
994 size_t len OVS_UNUSED, int verbosity)
996 const struct ofp_aggregate_stats_request *asr = oh;
998 if (asr->table_id == 0xff) {
999 ds_put_format(string, " table_id=any, ");
1001 ds_put_format(string, " table_id=%"PRIu8", ", asr->table_id);
1004 ofp_print_match(string, &asr->match, verbosity);
1008 ofp_aggregate_stats_reply(struct ds *string, const void *body_,
1009 size_t len OVS_UNUSED, int verbosity OVS_UNUSED)
1011 const struct ofp_aggregate_stats_reply *asr = body_;
1013 ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
1014 ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
1015 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1018 static void print_port_stat(struct ds *string, const char *leader,
1019 uint64_t stat, int more)
1021 ds_put_cstr(string, leader);
1023 ds_put_format(string, "%"PRIu64, stat);
1025 ds_put_char(string, '?');
1028 ds_put_cstr(string, ", ");
1030 ds_put_cstr(string, "\n");
1035 ofp_port_stats_reply(struct ds *string, const void *body, size_t len,
1038 const struct ofp_port_stats *ps = body;
1039 size_t n = len / sizeof *ps;
1040 ds_put_format(string, " %zu ports\n", n);
1041 if (verbosity < 1) {
1046 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1048 ds_put_cstr(string, "rx ");
1049 print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
1050 print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
1051 print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
1052 print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
1053 print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
1054 print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
1055 print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
1057 ds_put_cstr(string, " tx ");
1058 print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
1059 print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
1060 print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
1061 print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
1062 print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
1067 ofp_table_stats_reply(struct ds *string, const void *body, size_t len,
1070 const struct ofp_table_stats *ts = body;
1071 size_t n = len / sizeof *ts;
1072 ds_put_format(string, " %zu tables\n", n);
1073 if (verbosity < 1) {
1078 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1079 strncpy(name, ts->name, sizeof name);
1080 name[OFP_MAX_TABLE_NAME_LEN] = '\0';
1082 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1083 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1084 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1085 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1086 ds_put_cstr(string, " ");
1087 ds_put_format(string, "lookup=%"PRIu64", ",
1088 ntohll(ts->lookup_count));
1089 ds_put_format(string, "matched=%"PRIu64"\n",
1090 ntohll(ts->matched_count));
1095 vendor_stat(struct ds *string, const void *body, size_t len,
1096 int verbosity OVS_UNUSED)
1098 ds_put_format(string, " vendor=%08"PRIx32, ntohl(*(uint32_t *) body));
1099 ds_put_format(string, " %zu bytes additional data",
1100 len - sizeof(uint32_t));
1103 enum stats_direction {
1109 print_stats(struct ds *string, int type, const void *body, size_t body_len,
1110 int verbosity, enum stats_direction direction)
1113 size_t min_body, max_body;
1114 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1120 struct stats_msg request;
1121 struct stats_msg reply;
1124 static const struct stats_type stats_types[] = {
1129 { 0, SIZE_MAX, ofp_desc_stats_reply },
1134 { sizeof(struct ofp_flow_stats_request),
1135 sizeof(struct ofp_flow_stats_request),
1136 ofp_flow_stats_request },
1137 { 0, SIZE_MAX, ofp_flow_stats_reply },
1142 { sizeof(struct ofp_aggregate_stats_request),
1143 sizeof(struct ofp_aggregate_stats_request),
1144 ofp_aggregate_stats_request },
1145 { sizeof(struct ofp_aggregate_stats_reply),
1146 sizeof(struct ofp_aggregate_stats_reply),
1147 ofp_aggregate_stats_reply },
1153 { 0, SIZE_MAX, ofp_table_stats_reply },
1159 { 0, SIZE_MAX, ofp_port_stats_reply },
1164 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1165 { sizeof(uint32_t), SIZE_MAX, vendor_stat },
1175 const struct stats_type *s;
1176 const struct stats_msg *m;
1178 if (type >= ARRAY_SIZE(stats_types) || !stats_types[type].name) {
1179 ds_put_format(string, " ***unknown type %d***", type);
1182 for (s = stats_types; s->type >= 0; s++) {
1183 if (s->type == type) {
1187 ds_put_format(string, " type=%d(%s)\n", type, s->name);
1189 m = direction == REQUEST ? &s->request : &s->reply;
1190 if (body_len < m->min_body || body_len > m->max_body) {
1191 ds_put_format(string, " ***body_len=%zu not in %zu...%zu***",
1192 body_len, m->min_body, m->max_body);
1196 m->printer(string, body, body_len, verbosity);
1201 ofp_stats_request(struct ds *string, const void *oh, size_t len, int verbosity)
1203 const struct ofp_stats_request *srq = oh;
1206 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1210 print_stats(string, ntohs(srq->type), srq->body,
1211 len - offsetof(struct ofp_stats_request, body),
1212 verbosity, REQUEST);
1216 ofp_stats_reply(struct ds *string, const void *oh, size_t len, int verbosity)
1218 const struct ofp_stats_reply *srp = oh;
1220 ds_put_cstr(string, " flags=");
1222 ds_put_cstr(string, "none");
1224 uint16_t flags = ntohs(srp->flags);
1225 if (flags & OFPSF_REPLY_MORE) {
1226 ds_put_cstr(string, "[more]");
1227 flags &= ~OFPSF_REPLY_MORE;
1230 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]", flags);
1234 print_stats(string, ntohs(srp->type), srp->body,
1235 len - offsetof(struct ofp_stats_reply, body),
1240 ofp_echo(struct ds *string, const void *oh, size_t len, int verbosity)
1242 const struct ofp_header *hdr = oh;
1244 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *hdr);
1245 if (verbosity > 1) {
1246 ds_put_hex_dump(string, hdr, len - sizeof *hdr, 0, true);
1250 struct openflow_packet {
1254 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
1257 static const struct openflow_packet packets[] = {
1261 sizeof (struct ofp_header),
1265 OFPT_FEATURES_REQUEST,
1267 sizeof (struct ofp_header),
1271 OFPT_FEATURES_REPLY,
1273 sizeof (struct ofp_switch_features),
1274 ofp_print_switch_features,
1277 OFPT_GET_CONFIG_REQUEST,
1278 "get_config_request",
1279 sizeof (struct ofp_header),
1283 OFPT_GET_CONFIG_REPLY,
1285 sizeof (struct ofp_switch_config),
1286 ofp_print_switch_config,
1291 sizeof (struct ofp_switch_config),
1292 ofp_print_switch_config,
1297 offsetof(struct ofp_packet_in, data),
1303 sizeof (struct ofp_packet_out),
1309 sizeof (struct ofp_flow_mod),
1315 sizeof (struct ofp_flow_expired),
1316 ofp_print_flow_expired,
1321 sizeof (struct ofp_port_mod),
1327 sizeof (struct ofp_port_status),
1328 ofp_print_port_status
1333 sizeof (struct ofp_error_msg),
1334 ofp_print_error_msg,
1339 sizeof (struct ofp_stats_request),
1345 sizeof (struct ofp_stats_reply),
1351 sizeof (struct ofp_header),
1357 sizeof (struct ofp_header),
1363 sizeof (struct ofp_vendor_header),
1368 /* Composes and returns a string representing the OpenFlow packet of 'len'
1369 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1370 * verbosity and higher numbers increase verbosity. The caller is responsible
1371 * for freeing the string. */
1373 ofp_to_string(const void *oh_, size_t len, int verbosity)
1375 struct ds string = DS_EMPTY_INITIALIZER;
1376 const struct ofp_header *oh = oh_;
1377 const struct openflow_packet *pkt;
1379 if (len < sizeof(struct ofp_header)) {
1380 ds_put_cstr(&string, "OpenFlow packet too short:\n");
1381 ds_put_hex_dump(&string, oh, len, 0, true);
1382 return ds_cstr(&string);
1383 } else if (oh->version != OFP_VERSION) {
1384 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", oh->version);
1385 ds_put_hex_dump(&string, oh, len, 0, true);
1386 return ds_cstr(&string);
1389 for (pkt = packets; ; pkt++) {
1390 if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1391 ds_put_format(&string, "Unknown OpenFlow packet type %"PRIu8":\n",
1393 ds_put_hex_dump(&string, oh, len, 0, true);
1394 return ds_cstr(&string);
1395 } else if (oh->type == pkt->type) {
1400 ds_put_format(&string, "%s (xid=0x%"PRIx32"):", pkt->name, oh->xid);
1402 if (ntohs(oh->length) > len)
1403 ds_put_format(&string, " (***truncated to %zu bytes from %"PRIu16"***)",
1404 len, ntohs(oh->length));
1405 else if (ntohs(oh->length) < len) {
1406 ds_put_format(&string, " (***only uses %"PRIu16" bytes out of %zu***)\n",
1407 ntohs(oh->length), len);
1408 len = ntohs(oh->length);
1411 if (len < pkt->min_size) {
1412 ds_put_format(&string, " (***length=%zu < min_size=%zu***)\n",
1413 len, pkt->min_size);
1414 } else if (!pkt->printer) {
1415 if (len > sizeof *oh) {
1416 ds_put_format(&string, " length=%"PRIu16" (decoder not implemented)\n",
1420 pkt->printer(&string, oh, len, verbosity);
1422 if (verbosity >= 3) {
1423 ds_put_hex_dump(&string, oh, len, 0, true);
1425 if (string.string[string.length - 1] != '\n') {
1426 ds_put_char(&string, '\n');
1428 return ds_cstr(&string);
1431 /* Returns the name for the specified OpenFlow message type as a string,
1432 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1433 * hex number, e.g. "0x55".
1435 * The caller must free the returned string when it is no longer needed. */
1437 ofp_message_type_to_string(uint8_t type)
1439 struct ds s = DS_EMPTY_INITIALIZER;
1440 const struct openflow_packet *pkt;
1441 for (pkt = packets; ; pkt++) {
1442 if (pkt >= &packets[ARRAY_SIZE(packets)]) {
1443 ds_put_format(&s, "0x%02"PRIx8, type);
1445 } else if (type == pkt->type) {
1448 ds_put_cstr(&s, "OFPT_");
1449 for (p = pkt->name; *p; p++) {
1450 ds_put_char(&s, toupper((unsigned char) *p));
1459 print_and_free(FILE *stream, char *string)
1461 fputs(string, stream);
1465 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1466 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1467 * numbers increase verbosity. */
1469 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1471 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1474 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1475 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1476 * the Ethernet frame (of which 'len' bytes were captured).
1478 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1480 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1482 print_and_free(stream, ofp_packet_to_string(data, len, total_len));