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>
29 #include "byte-order.h"
31 #include "dynamic-string.h"
36 #include "openflow/openflow.h"
37 #include "openflow/nicira-ext.h"
40 #include "type-props.h"
43 static void ofp_print_port_name(struct ds *string, uint16_t port);
44 static void ofp_print_queue_name(struct ds *string, uint32_t port);
45 static void ofp_print_error(struct ds *, int error);
48 /* Returns a string that represents the contents of the Ethernet frame in the
49 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
50 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
51 * bytes were captured).
53 * The caller must free the returned string.
55 * This starts and kills a tcpdump subprocess so it's quite expensive. */
57 ofp_packet_to_string(const void *data, size_t len, size_t total_len OVS_UNUSED)
59 struct ds ds = DS_EMPTY_INITIALIZER;
68 ofpbuf_use_const(&buf, data, len);
72 ovs_error(errno, "tmpfile");
73 return xstrdup("<error>");
75 pcap_write_header(pcap);
76 pcap_write(pcap, &buf);
79 ovs_error(errno, "error writing temporary file");
83 snprintf(command, sizeof command, "/usr/sbin/tcpdump -e -n -r /dev/fd/%d 2>/dev/null",
85 tcpdump = popen(command, "r");
88 ovs_error(errno, "exec(\"%s\")", command);
89 return xstrdup("<error>");
92 while ((c = getc(tcpdump)) != EOF) {
96 status = pclose(tcpdump);
97 if (WIFEXITED(status)) {
98 if (WEXITSTATUS(status))
99 ovs_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
100 } else if (WIFSIGNALED(status)) {
101 ovs_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
107 ofp_print_packet_in(struct ds *string, const struct ofp_packet_in *op,
110 size_t len = ntohs(op->header.length);
113 ds_put_format(string, " total_len=%"PRIu16" in_port=",
114 ntohs(op->total_len));
115 ofp_print_port_name(string, ntohs(op->in_port));
117 if (op->reason == OFPR_ACTION)
118 ds_put_cstr(string, " (via action)");
119 else if (op->reason != OFPR_NO_MATCH)
120 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
122 data_len = len - offsetof(struct ofp_packet_in, data);
123 ds_put_format(string, " data_len=%zu", data_len);
124 if (htonl(op->buffer_id) == UINT32_MAX) {
125 ds_put_format(string, " (unbuffered)");
126 if (ntohs(op->total_len) != data_len)
127 ds_put_format(string, " (***total_len != data_len***)");
129 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
130 if (ntohs(op->total_len) < data_len)
131 ds_put_format(string, " (***total_len < data_len***)");
133 ds_put_char(string, '\n');
137 struct ofpbuf packet;
139 ofpbuf_use_const(&packet, op->data, data_len);
140 flow_extract(&packet, 0, ntohs(op->in_port), &flow);
141 flow_format(string, &flow);
142 ds_put_char(string, '\n');
145 char *packet = ofp_packet_to_string(op->data, data_len,
146 ntohs(op->total_len));
147 ds_put_cstr(string, packet);
152 static void ofp_print_port_name(struct ds *string, uint16_t port)
171 case OFPP_CONTROLLER:
181 ds_put_format(string, "%"PRIu16, port);
184 ds_put_cstr(string, name);
189 print_note(struct ds *string, const struct nx_action_note *nan)
194 ds_put_cstr(string, "note:");
195 len = ntohs(nan->len) - offsetof(struct nx_action_note, note);
196 for (i = 0; i < len; i++) {
198 ds_put_char(string, '.');
200 ds_put_format(string, "%02"PRIx8, nan->note[i]);
205 nx_action_len(enum nx_action_subtype subtype)
208 case NXAST_SNAT__OBSOLETE: return -1;
209 case NXAST_RESUBMIT: return sizeof(struct nx_action_resubmit);
210 case NXAST_SET_TUNNEL: return sizeof(struct nx_action_set_tunnel);
211 case NXAST_DROP_SPOOFED_ARP:
212 return sizeof(struct nx_action_drop_spoofed_arp);
213 case NXAST_SET_QUEUE: return sizeof(struct nx_action_set_queue);
214 case NXAST_POP_QUEUE: return sizeof(struct nx_action_pop_queue);
215 case NXAST_REG_MOVE: return sizeof(struct nx_action_reg_move);
216 case NXAST_REG_LOAD: return sizeof(struct nx_action_reg_load);
217 case NXAST_NOTE: return -1;
223 ofp_print_nx_action(struct ds *string, const struct nx_action_header *nah)
225 uint16_t subtype = ntohs(nah->subtype);
226 int required_len = nx_action_len(subtype);
227 int len = ntohs(nah->len);
229 if (required_len != -1 && required_len != len) {
230 ds_put_format(string, "***Nicira action %"PRIu16" wrong length: %d***",
235 if (subtype <= TYPE_MAXIMUM(enum nx_action_subtype)) {
236 const struct nx_action_set_tunnel *nast;
237 const struct nx_action_set_queue *nasq;
238 const struct nx_action_resubmit *nar;
239 const struct nx_action_reg_move *move;
240 const struct nx_action_reg_load *load;
242 switch ((enum nx_action_subtype) subtype) {
244 nar = (struct nx_action_resubmit *)nah;
245 ds_put_format(string, "resubmit:");
246 ofp_print_port_name(string, ntohs(nar->in_port));
249 case NXAST_SET_TUNNEL:
250 nast = (struct nx_action_set_tunnel *)nah;
251 ds_put_format(string, "set_tunnel:%#"PRIx32, ntohl(nast->tun_id));
254 case NXAST_DROP_SPOOFED_ARP:
255 ds_put_cstr(string, "drop_spoofed_arp");
258 case NXAST_SET_QUEUE:
259 nasq = (struct nx_action_set_queue *)nah;
260 ds_put_format(string, "set_queue:%u", ntohl(nasq->queue_id));
263 case NXAST_POP_QUEUE:
264 ds_put_cstr(string, "pop_queue");
268 print_note(string, (const struct nx_action_note *) nah);
272 move = (const struct nx_action_reg_move *) nah;
273 nxm_format_reg_move(move, string);
277 load = (const struct nx_action_reg_load *) nah;
278 nxm_format_reg_load(load, string);
281 case NXAST_SNAT__OBSOLETE:
287 ds_put_format(string, "***unknown Nicira action:%"PRIu16"***", subtype);
291 ofp_action_len(enum ofp_action_type type)
294 case OFPAT_OUTPUT: return sizeof(struct ofp_action_output);
295 case OFPAT_SET_VLAN_VID: return sizeof(struct ofp_action_vlan_vid);
296 case OFPAT_SET_VLAN_PCP: return sizeof(struct ofp_action_vlan_pcp);
297 case OFPAT_STRIP_VLAN: return sizeof(struct ofp_action_header);
298 case OFPAT_SET_DL_SRC: return sizeof(struct ofp_action_dl_addr);
299 case OFPAT_SET_DL_DST: return sizeof(struct ofp_action_dl_addr);
300 case OFPAT_SET_NW_SRC: return sizeof(struct ofp_action_nw_addr);
301 case OFPAT_SET_NW_DST: return sizeof(struct ofp_action_nw_addr);
302 case OFPAT_SET_NW_TOS: return sizeof(struct ofp_action_nw_tos);
303 case OFPAT_SET_TP_SRC: return sizeof(struct ofp_action_tp_port);
304 case OFPAT_SET_TP_DST: return sizeof(struct ofp_action_tp_port);
305 case OFPAT_ENQUEUE: return sizeof(struct ofp_action_enqueue);
306 case OFPAT_VENDOR: return -1;
312 ofp_print_action(struct ds *string, const struct ofp_action_header *ah,
315 enum ofp_action_type type;
319 if (actions_len < sizeof *ah) {
320 ds_put_format(string, "***action array too short for next action***\n");
324 type = ntohs(ah->type);
325 len = ntohs(ah->len);
326 if (actions_len < len) {
327 ds_put_format(string, "***truncated action %d***\n", (int) type);
332 ds_put_format(string, "***zero-length action***\n");
336 if ((len % OFP_ACTION_ALIGN) != 0) {
337 ds_put_format(string,
338 "***action %d length not a multiple of %d***\n",
339 (int) type, OFP_ACTION_ALIGN);
343 required_len = ofp_action_len(type);
344 if (required_len >= 0 && len != required_len) {
345 ds_put_format(string,
346 "***action %d wrong length: %zu***\n", (int) type, len);
352 struct ofp_action_output *oa = (struct ofp_action_output *)ah;
353 uint16_t port = ntohs(oa->port);
354 if (port < OFPP_MAX) {
355 ds_put_format(string, "output:%"PRIu16, port);
357 ofp_print_port_name(string, port);
358 if (port == OFPP_CONTROLLER) {
360 ds_put_format(string, ":%"PRIu16, ntohs(oa->max_len));
362 ds_put_cstr(string, ":all");
369 case OFPAT_ENQUEUE: {
370 struct ofp_action_enqueue *ea = (struct ofp_action_enqueue *)ah;
371 unsigned int port = ntohs(ea->port);
372 unsigned int queue_id = ntohl(ea->queue_id);
373 ds_put_format(string, "enqueue:");
374 if (port != OFPP_IN_PORT) {
375 ds_put_format(string, "%u", port);
377 ds_put_cstr(string, "IN_PORT");
379 ds_put_format(string, "q%u", queue_id);
383 case OFPAT_SET_VLAN_VID: {
384 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
385 ds_put_format(string, "mod_vlan_vid:%"PRIu16, ntohs(va->vlan_vid));
389 case OFPAT_SET_VLAN_PCP: {
390 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
391 ds_put_format(string, "mod_vlan_pcp:%"PRIu8, va->vlan_pcp);
395 case OFPAT_STRIP_VLAN:
396 ds_put_cstr(string, "strip_vlan");
399 case OFPAT_SET_DL_SRC: {
400 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
401 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
402 ETH_ADDR_ARGS(da->dl_addr));
406 case OFPAT_SET_DL_DST: {
407 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
408 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
409 ETH_ADDR_ARGS(da->dl_addr));
413 case OFPAT_SET_NW_SRC: {
414 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
415 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&na->nw_addr));
419 case OFPAT_SET_NW_DST: {
420 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
421 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&na->nw_addr));
425 case OFPAT_SET_NW_TOS: {
426 struct ofp_action_nw_tos *nt = (struct ofp_action_nw_tos *)ah;
427 ds_put_format(string, "mod_nw_tos:%d", nt->nw_tos);
431 case OFPAT_SET_TP_SRC: {
432 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
433 ds_put_format(string, "mod_tp_src:%d", ntohs(ta->tp_port));
437 case OFPAT_SET_TP_DST: {
438 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
439 ds_put_format(string, "mod_tp_dst:%d", ntohs(ta->tp_port));
444 struct ofp_action_vendor_header *avh
445 = (struct ofp_action_vendor_header *)ah;
446 if (len < sizeof *avh) {
447 ds_put_format(string, "***ofpat_vendor truncated***\n");
450 if (avh->vendor == htonl(NX_VENDOR_ID)) {
451 ofp_print_nx_action(string, (struct nx_action_header *)avh);
453 ds_put_format(string, "vendor action:0x%x", ntohl(avh->vendor));
459 ds_put_format(string, "(decoder %d not implemented)", (int) type);
467 ofp_print_actions(struct ds *string, const struct ofp_action_header *action,
470 uint8_t *p = (uint8_t *)action;
473 ds_put_cstr(string, "actions=");
475 ds_put_cstr(string, "drop");
477 while (actions_len > 0) {
479 ds_put_cstr(string, ",");
481 len = ofp_print_action(string, (struct ofp_action_header *)p,
492 ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
495 size_t len = ntohs(opo->header.length);
496 size_t actions_len = ntohs(opo->actions_len);
498 ds_put_cstr(string, " in_port=");
499 ofp_print_port_name(string, ntohs(opo->in_port));
501 ds_put_format(string, " actions_len=%zu ", actions_len);
502 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
503 ds_put_format(string, "***packet too short for action length***\n");
506 ofp_print_actions(string, opo->actions, actions_len);
508 if (ntohl(opo->buffer_id) == UINT32_MAX) {
509 int data_len = len - sizeof *opo - actions_len;
510 ds_put_format(string, " data_len=%d", data_len);
511 if (verbosity > 0 && len > sizeof *opo) {
512 char *packet = ofp_packet_to_string(
513 (uint8_t *)opo->actions + actions_len, data_len, data_len);
514 ds_put_char(string, '\n');
515 ds_put_cstr(string, packet);
519 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
521 ds_put_char(string, '\n');
524 /* qsort comparison function. */
526 compare_ports(const void *a_, const void *b_)
528 const struct ofp_phy_port *a = a_;
529 const struct ofp_phy_port *b = b_;
530 uint16_t ap = ntohs(a->port_no);
531 uint16_t bp = ntohs(b->port_no);
533 return ap < bp ? -1 : ap > bp;
536 static void ofp_print_port_features(struct ds *string, uint32_t features)
539 ds_put_cstr(string, "Unsupported\n");
542 if (features & OFPPF_10MB_HD) {
543 ds_put_cstr(string, "10MB-HD ");
545 if (features & OFPPF_10MB_FD) {
546 ds_put_cstr(string, "10MB-FD ");
548 if (features & OFPPF_100MB_HD) {
549 ds_put_cstr(string, "100MB-HD ");
551 if (features & OFPPF_100MB_FD) {
552 ds_put_cstr(string, "100MB-FD ");
554 if (features & OFPPF_1GB_HD) {
555 ds_put_cstr(string, "1GB-HD ");
557 if (features & OFPPF_1GB_FD) {
558 ds_put_cstr(string, "1GB-FD ");
560 if (features & OFPPF_10GB_FD) {
561 ds_put_cstr(string, "10GB-FD ");
563 if (features & OFPPF_COPPER) {
564 ds_put_cstr(string, "COPPER ");
566 if (features & OFPPF_FIBER) {
567 ds_put_cstr(string, "FIBER ");
569 if (features & OFPPF_AUTONEG) {
570 ds_put_cstr(string, "AUTO_NEG ");
572 if (features & OFPPF_PAUSE) {
573 ds_put_cstr(string, "AUTO_PAUSE ");
575 if (features & OFPPF_PAUSE_ASYM) {
576 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
578 ds_put_char(string, '\n');
582 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
584 char name[OFP_MAX_PORT_NAME_LEN];
587 memcpy(name, port->name, sizeof name);
588 for (j = 0; j < sizeof name - 1; j++) {
589 if (!isprint(name[j])) {
595 ds_put_char(string, ' ');
596 ofp_print_port_name(string, ntohs(port->port_no));
597 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
598 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
601 ds_put_format(string, " current: ");
602 ofp_print_port_features(string, ntohl(port->curr));
604 if (port->advertised) {
605 ds_put_format(string, " advertised: ");
606 ofp_print_port_features(string, ntohl(port->advertised));
608 if (port->supported) {
609 ds_put_format(string, " supported: ");
610 ofp_print_port_features(string, ntohl(port->supported));
613 ds_put_format(string, " peer: ");
614 ofp_print_port_features(string, ntohl(port->peer));
619 ofp_print_switch_features(struct ds *string,
620 const struct ofp_switch_features *osf)
622 size_t len = ntohs(osf->header.length);
623 struct ofp_phy_port *port_list;
627 ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
628 osf->header.version, ntohll(osf->datapath_id));
629 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
630 ntohl(osf->n_buffers));
631 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
632 ntohl(osf->capabilities), ntohl(osf->actions));
634 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
636 port_list = xmemdup(osf->ports, len - sizeof *osf);
637 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
638 for (i = 0; i < n_ports; i++) {
639 ofp_print_phy_port(string, &port_list[i]);
645 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
649 flags = ntohs(osc->flags);
651 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
654 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
657 static void print_wild(struct ds *string, const char *leader, int is_wild,
658 int verbosity, const char *format, ...)
659 __attribute__((format(printf, 5, 6)));
661 static void print_wild(struct ds *string, const char *leader, int is_wild,
662 int verbosity, const char *format, ...)
664 if (is_wild && verbosity < 2) {
667 ds_put_cstr(string, leader);
671 va_start(args, format);
672 ds_put_format_valist(string, format, args);
675 ds_put_char(string, '*');
677 ds_put_char(string, ',');
681 print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
682 uint32_t wild_bits, int verbosity)
684 if (wild_bits >= 32 && verbosity < 2) {
687 ds_put_cstr(string, leader);
688 if (wild_bits < 32) {
689 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
691 ds_put_format(string, "/%d", 32 - wild_bits);
694 ds_put_char(string, '*');
696 ds_put_char(string, ',');
700 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
702 char *s = ofp_match_to_string(om, verbosity);
708 ofp_match_to_string(const struct ofp_match *om, int verbosity)
710 struct ds f = DS_EMPTY_INITIALIZER;
711 uint32_t w = ntohl(om->wildcards);
712 bool skip_type = false;
713 bool skip_proto = false;
715 if (!(w & OFPFW_DL_TYPE)) {
717 if (om->dl_type == htons(ETH_TYPE_IP)) {
718 if (!(w & OFPFW_NW_PROTO)) {
720 if (om->nw_proto == IP_TYPE_ICMP) {
721 ds_put_cstr(&f, "icmp,");
722 } else if (om->nw_proto == IP_TYPE_TCP) {
723 ds_put_cstr(&f, "tcp,");
724 } else if (om->nw_proto == IP_TYPE_UDP) {
725 ds_put_cstr(&f, "udp,");
727 ds_put_cstr(&f, "ip,");
731 ds_put_cstr(&f, "ip,");
733 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
734 ds_put_cstr(&f, "arp,");
739 if (w & NXFW_TUN_ID) {
740 ds_put_cstr(&f, "tun_id_wild,");
742 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
743 "%d", ntohs(om->in_port));
744 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
745 "%d", ntohs(om->dl_vlan));
746 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
747 "%d", om->dl_vlan_pcp);
748 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
749 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
750 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
751 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
753 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
754 "0x%04x", ntohs(om->dl_type));
756 print_ip_netmask(&f, "nw_src=", om->nw_src,
757 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
758 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
759 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
761 if (om->dl_type == htons(ETH_TYPE_ARP)) {
762 print_wild(&f, "opcode=", w & OFPFW_NW_PROTO, verbosity,
765 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
769 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
771 if (om->nw_proto == IP_TYPE_ICMP) {
772 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
773 "%d", ntohs(om->icmp_type));
774 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
775 "%d", ntohs(om->icmp_code));
777 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
778 "%d", ntohs(om->tp_src));
779 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
780 "%d", ntohs(om->tp_dst));
782 if (ds_last(&f) == ',') {
789 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
790 enum ofputil_msg_code code, int verbosity)
795 error = ofputil_decode_flow_mod(&fm, oh, NXFF_OPENFLOW10);
797 ofp_print_error(s, error);
802 switch (fm.command) {
804 ds_put_cstr(s, "ADD");
807 ds_put_cstr(s, "MOD");
809 case OFPFC_MODIFY_STRICT:
810 ds_put_cstr(s, "MOD_STRICT");
813 ds_put_cstr(s, "DEL");
815 case OFPFC_DELETE_STRICT:
816 ds_put_cstr(s, "DEL_STRICT");
819 ds_put_format(s, "cmd:%d", fm.command);
823 if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
824 const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
826 ofp_print_match(s, &ofm->match, verbosity);
827 } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
828 const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
829 const void *nxm = nfm + 1;
830 char *nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
831 ds_put_cstr(s, nxm_s);
834 cls_rule_format(&fm.cr, s);
837 if (ds_last(s) != ' ') {
840 if (fm.cookie != htonll(0)) {
841 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.cookie));
843 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
844 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
846 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
847 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
849 if (fm.cr.priority != OFP_DEFAULT_PRIORITY && verbosity >= 3) {
850 ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
852 if (fm.buffer_id != UINT32_MAX) {
853 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
856 ds_put_format(s, "flags:0x%"PRIx16" ", fm.flags);
859 ofp_print_actions(s, (const struct ofp_action_header *) fm.actions,
860 fm.n_actions * sizeof *fm.actions);
864 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
866 ds_put_format(string, "%u", sec);
868 ds_put_format(string, ".%09u", nsec);
869 while (string->string[string->length - 1] == '0') {
873 ds_put_char(string, 's');
877 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
879 struct ofputil_flow_removed fr;
882 error = ofputil_decode_flow_removed(&fr, oh, NXFF_OPENFLOW10);
884 ofp_print_error(string, error);
888 cls_rule_format(&fr.rule, string);
890 ds_put_cstr(string, " reason=");
892 case OFPRR_IDLE_TIMEOUT:
893 ds_put_cstr(string, "idle");
895 case OFPRR_HARD_TIMEOUT:
896 ds_put_cstr(string, "hard");
899 ds_put_cstr(string, "delete");
902 ds_put_format(string, "**%"PRIu8"**", fr.reason);
906 if (fr.cookie != htonll(0)) {
907 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
909 ds_put_cstr(string, " duration");
910 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
911 ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
912 fr.idle_timeout, fr.packet_count, fr.byte_count);
916 ofp_print_port_mod(struct ds *string, const struct ofp_port_mod *opm)
918 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
919 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
920 ntohl(opm->config), ntohl(opm->mask));
921 ds_put_format(string, " advertise: ");
922 if (opm->advertise) {
923 ofp_print_port_features(string, ntohl(opm->advertise));
925 ds_put_format(string, "UNCHANGED\n");
935 static const struct error_type error_types[] = {
936 #define ERROR_TYPE(TYPE) {TYPE, -1, #TYPE}
937 #define ERROR_CODE(TYPE, CODE) {TYPE, CODE, #CODE}
938 ERROR_TYPE(OFPET_HELLO_FAILED),
939 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_INCOMPATIBLE),
940 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_EPERM),
942 ERROR_TYPE(OFPET_BAD_REQUEST),
943 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
944 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE),
945 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT),
946 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR),
947 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE),
948 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_EPERM),
949 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN),
950 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_EMPTY),
951 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_UNKNOWN),
952 /* Nicira error extensions. */
953 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_INVALID),
954 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_TYPE),
955 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_VALUE),
956 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_MASK),
957 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_PREREQ),
958 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_DUP_TYPE),
960 ERROR_TYPE(OFPET_BAD_ACTION),
961 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE),
962 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_LEN),
963 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR),
964 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE),
965 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT),
966 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT),
967 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_EPERM),
968 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_TOO_MANY),
970 ERROR_TYPE(OFPET_FLOW_MOD_FAILED),
971 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL),
972 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP),
973 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_EPERM),
974 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_EMERG_TIMEOUT),
975 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND),
976 /* Nicira error extenstions. */
977 ERROR_CODE(OFPET_FLOW_MOD_FAILED, NXFMFC_HARDWARE),
978 ERROR_CODE(OFPET_FLOW_MOD_FAILED, NXFMFC_BAD_TABLE_ID),
980 ERROR_TYPE(OFPET_PORT_MOD_FAILED),
981 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT),
982 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR)
984 #define N_ERROR_TYPES ARRAY_SIZE(error_types)
987 lookup_error_type(int type)
989 const struct error_type *t;
991 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
992 if (t->type == type && t->code == -1) {
1000 lookup_error_code(int type, int code)
1002 const struct error_type *t;
1004 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
1005 if (t->type == type && t->code == code) {
1013 ofp_print_error(struct ds *string, int error)
1015 int type = get_ofp_err_type(error);
1016 int code = get_ofp_err_code(error);
1017 if (string->length) {
1018 ds_put_char(string, ' ');
1020 ds_put_format(string, " ***decode error type:%d(%s) code:%d(%s)***",
1021 type, lookup_error_type(type),
1022 code, lookup_error_code(type, code));
1026 ofp_print_nx_error_msg(struct ds *string, const struct ofp_error_msg *oem)
1028 size_t len = ntohs(oem->header.length);
1029 const struct nx_vendor_error *nve = (struct nx_vendor_error *)oem->data;
1030 int vendor = ntohl(nve->vendor);
1031 int type = ntohs(nve->type);
1032 int code = ntohs(nve->code);
1034 ds_put_format(string, " vendor:%x type:%d(%s) code:%d(%s) payload:\n",
1036 type, lookup_error_type(type),
1037 code, lookup_error_code(type, code));
1039 ds_put_hex_dump(string, nve + 1, len - sizeof *oem - sizeof *nve,
1044 ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
1046 size_t len = ntohs(oem->header.length);
1047 int type = ntohs(oem->type);
1048 int code = ntohs(oem->code);
1052 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
1053 if (len < sizeof *oem + sizeof(struct nx_vendor_error)) {
1054 ds_put_format(string,
1055 "(***truncated extended error message is %zu bytes "
1056 "when it should be at least %zu***)\n",
1057 len, sizeof(struct nx_vendor_error));
1061 return ofp_print_nx_error_msg(string, oem);
1064 ds_put_format(string, " type:%d(%s) code:%d(%s) payload:\n",
1065 type, lookup_error_type(type),
1066 code, lookup_error_code(type, code));
1069 case OFPET_HELLO_FAILED:
1070 ds_put_printable(string, (char *) oem->data, len - sizeof *oem);
1073 case OFPET_BAD_REQUEST:
1074 s = ofp_to_string(oem->data, len - sizeof *oem, 1);
1075 ds_put_cstr(string, s);
1080 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
1086 ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
1088 if (ops->reason == OFPPR_ADD) {
1089 ds_put_format(string, " ADD:");
1090 } else if (ops->reason == OFPPR_DELETE) {
1091 ds_put_format(string, " DEL:");
1092 } else if (ops->reason == OFPPR_MODIFY) {
1093 ds_put_format(string, " MOD:");
1096 ofp_print_phy_port(string, &ops->desc);
1100 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
1102 const struct ofp_desc_stats *ods = ofputil_stats_body(oh);
1104 ds_put_format(string, "Manufacturer: %.*s\n",
1105 (int) sizeof ods->mfr_desc, ods->mfr_desc);
1106 ds_put_format(string, "Hardware: %.*s\n",
1107 (int) sizeof ods->hw_desc, ods->hw_desc);
1108 ds_put_format(string, "Software: %.*s\n",
1109 (int) sizeof ods->sw_desc, ods->sw_desc);
1110 ds_put_format(string, "Serial Num: %.*s\n",
1111 (int) sizeof ods->serial_num, ods->serial_num);
1112 ds_put_format(string, "DP Description: %.*s\n",
1113 (int) sizeof ods->dp_desc, ods->dp_desc);
1117 ofp_print_flow_stats_request(struct ds *string, const struct ofp_header *oh)
1119 struct flow_stats_request fsr;
1122 error = ofputil_decode_flow_stats_request(&fsr, oh, NXFF_OPENFLOW10);
1124 ofp_print_error(string, error);
1128 if (fsr.table_id != 0xff) {
1129 ds_put_format(string, " table_id=%"PRIu8, fsr.table_id);
1132 if (fsr.out_port != OFPP_NONE) {
1133 ds_put_cstr(string, " out_port=");
1134 ofp_print_port_name(string, fsr.out_port);
1137 ds_put_char(string, ' ');
1138 cls_rule_format(&fsr.match, string);
1142 ofp_print_ofpst_flow_reply(struct ds *string, const struct ofp_header *oh,
1145 size_t len = ofputil_stats_body_len(oh);
1146 const char *body = ofputil_stats_body(oh);
1147 const char *pos = body;
1149 const struct ofp_flow_stats *fs;
1150 ptrdiff_t bytes_left = body + len - pos;
1153 ds_put_char(string, '\n');
1155 if (bytes_left < sizeof *fs) {
1156 if (bytes_left != 0) {
1157 ds_put_format(string, " ***%td leftover bytes at end***",
1163 fs = (const void *) pos;
1164 length = ntohs(fs->length);
1165 if (length < sizeof *fs) {
1166 ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
1167 length, sizeof *fs);
1169 } else if (length > bytes_left) {
1170 ds_put_format(string,
1171 " ***length=%zu but only %td bytes left***",
1172 length, bytes_left);
1174 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
1175 ds_put_format(string,
1176 " ***length=%zu has %zu bytes leftover in "
1179 (length - sizeof *fs) % sizeof fs->actions[0]);
1183 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1184 ntohll(fs->cookie));
1185 ofp_print_duration(string, ntohl(fs->duration_sec),
1186 ntohl(fs->duration_nsec));
1187 ds_put_format(string, ", table_id=%"PRIu8", ", fs->table_id);
1188 ds_put_format(string, "priority=%"PRIu16", ", ntohs(fs->priority));
1189 ds_put_format(string, "n_packets=%"PRIu64", ",
1190 ntohll(fs->packet_count));
1191 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
1192 if (fs->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
1193 ds_put_format(string, "idle_timeout=%"PRIu16",",
1194 ntohs(fs->idle_timeout));
1196 if (fs->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
1197 ds_put_format(string, "hard_timeout=%"PRIu16",",
1198 ntohs(fs->hard_timeout));
1200 ofp_print_match(string, &fs->match, verbosity);
1201 ds_put_char(string, ' ');
1202 ofp_print_actions(string, fs->actions, length - sizeof *fs);
1209 ofp_print_nxst_flow_reply(struct ds *string, const struct ofp_header *oh)
1213 ofpbuf_use_const(&b, ofputil_nxstats_body(oh),
1214 ofputil_nxstats_body_len(oh));
1215 while (b.size > 0) {
1216 const struct nx_flow_stats *fs;
1217 union ofp_action *actions;
1218 struct cls_rule rule;
1219 size_t actions_len, n_actions;
1224 ds_put_char(string, '\n');
1226 fs = ofpbuf_try_pull(&b, sizeof *fs);
1228 ds_put_format(string, " ***%td leftover bytes at end***", b.size);
1232 length = ntohs(fs->length);
1233 if (length < sizeof *fs) {
1234 ds_put_format(string, " ***nx_flow_stats claims length %zu***",
1239 match_len = ntohs(fs->match_len);
1240 if (match_len > length - sizeof *fs) {
1241 ds_put_format(string, " ***length=%zu match_len=%d***",
1246 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
1247 ntohll(fs->cookie));
1248 ofp_print_duration(string, ntohl(fs->duration_sec),
1249 ntohl(fs->duration_nsec));
1250 ds_put_format(string, ", table_id=%"PRIu8", ", fs->table_id);
1251 ds_put_format(string, "priority=%"PRIu16", ", ntohs(fs->priority));
1252 ds_put_format(string, "n_packets=%"PRIu64", ",
1253 ntohll(fs->packet_count));
1254 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
1255 if (fs->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
1256 ds_put_format(string, "idle_timeout=%"PRIu16",",
1257 ntohs(fs->idle_timeout));
1259 if (fs->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
1260 ds_put_format(string, "hard_timeout=%"PRIu16",",
1261 ntohs(fs->hard_timeout));
1264 error = nx_pull_match(&b, match_len, ntohs(fs->priority), &rule);
1266 ofp_print_error(string, error);
1270 actions_len = length - sizeof *fs - ROUND_UP(match_len, 8);
1271 error = ofputil_pull_actions(&b, actions_len, &actions, &n_actions);
1273 ofp_print_error(string, error);
1277 cls_rule_format(&rule, string);
1278 ds_put_char(string, ' ');
1279 ofp_print_actions(string, (const struct ofp_action_header *) actions,
1280 n_actions * sizeof *actions);
1285 ofp_print_ofp_aggregate_stats_reply (
1286 struct ds *string, const struct ofp_aggregate_stats_reply *asr)
1288 ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
1289 ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
1290 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1294 ofp_print_ofpst_aggregate_reply(struct ds *string, const struct ofp_header *oh)
1296 ofp_print_ofp_aggregate_stats_reply(string, ofputil_stats_body(oh));
1300 ofp_print_nxst_aggregate_reply(struct ds *string,
1301 const struct nx_aggregate_stats_reply *nasr)
1303 ofp_print_ofp_aggregate_stats_reply(string, &nasr->asr);
1306 static void print_port_stat(struct ds *string, const char *leader,
1307 uint64_t stat, int more)
1309 ds_put_cstr(string, leader);
1311 ds_put_format(string, "%"PRIu64, stat);
1313 ds_put_char(string, '?');
1316 ds_put_cstr(string, ", ");
1318 ds_put_cstr(string, "\n");
1323 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1325 const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
1326 ds_put_format(string, "port_no=%"PRIu16, ntohs(psr->port_no));
1330 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1333 const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1334 size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
1335 ds_put_format(string, " %zu ports\n", n);
1336 if (verbosity < 1) {
1341 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1343 ds_put_cstr(string, "rx ");
1344 print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
1345 print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
1346 print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
1347 print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
1348 print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
1349 print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
1350 print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
1352 ds_put_cstr(string, " tx ");
1353 print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
1354 print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
1355 print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
1356 print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
1357 print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
1362 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1365 const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1366 size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
1367 ds_put_format(string, " %zu tables\n", n);
1368 if (verbosity < 1) {
1373 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1374 strncpy(name, ts->name, sizeof name);
1375 name[OFP_MAX_TABLE_NAME_LEN] = '\0';
1377 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1378 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1379 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1380 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1381 ds_put_cstr(string, " ");
1382 ds_put_format(string, "lookup=%"PRIu64", ",
1383 ntohll(ts->lookup_count));
1384 ds_put_format(string, "matched=%"PRIu64"\n",
1385 ntohll(ts->matched_count));
1390 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1392 if (queue_id == OFPQ_ALL) {
1393 ds_put_cstr(string, "ALL");
1395 ds_put_format(string, "%"PRIu32, queue_id);
1400 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1402 const struct ofp_queue_stats_request *qsr = ofputil_stats_body(oh);
1404 ds_put_cstr(string, "port=");
1405 ofp_print_port_name(string, ntohs(qsr->port_no));
1407 ds_put_cstr(string, " queue=");
1408 ofp_print_queue_name(string, ntohl(qsr->queue_id));
1412 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1415 const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1416 size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
1417 ds_put_format(string, " %zu queues\n", n);
1418 if (verbosity < 1) {
1423 ds_put_cstr(string, " port ");
1424 ofp_print_port_name(string, ntohs(qs->port_no));
1425 ds_put_cstr(string, " queue ");
1426 ofp_print_queue_name(string, ntohl(qs->queue_id));
1427 ds_put_cstr(string, ": ");
1429 print_port_stat(string, "bytes=", ntohll(qs->tx_bytes), 1);
1430 print_port_stat(string, "pkts=", ntohll(qs->tx_packets), 1);
1431 print_port_stat(string, "errors=", ntohll(qs->tx_errors), 0);
1436 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1438 const struct ofp_stats_request *srq
1439 = (const struct ofp_stats_request *) oh;
1442 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1448 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1450 const struct ofp_stats_reply *srp = (const struct ofp_stats_reply *) oh;
1453 uint16_t flags = ntohs(srp->flags);
1455 ds_put_cstr(string, " flags=");
1456 if (flags & OFPSF_REPLY_MORE) {
1457 ds_put_cstr(string, "[more]");
1458 flags &= ~OFPSF_REPLY_MORE;
1461 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1468 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1470 size_t len = ntohs(oh->length);
1472 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1473 if (verbosity > 1) {
1474 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1479 ofp_print_nxt_status_message(struct ds *string, const struct ofp_header *oh)
1483 ofpbuf_use_const(&b, oh, ntohs(oh->length));
1484 ofpbuf_pull(&b, sizeof *oh);
1485 ds_put_char(string, '"');
1486 ds_put_printable(string, b.data, b.size);
1487 ds_put_char(string, '"');
1491 ofp_print_nxt_tun_id_from_cookie(struct ds *string,
1492 const struct nxt_tun_id_cookie *ntic)
1494 ds_put_format(string, " set=%"PRIu8, ntic->set);
1498 ofp_print_nxt_role_message(struct ds *string,
1499 const struct nx_role_request *nrr)
1501 unsigned int role = ntohl(nrr->role);
1503 ds_put_cstr(string, " role=");
1504 if (role == NX_ROLE_OTHER) {
1505 ds_put_cstr(string, "other");
1506 } else if (role == NX_ROLE_MASTER) {
1507 ds_put_cstr(string, "master");
1508 } else if (role == NX_ROLE_SLAVE) {
1509 ds_put_cstr(string, "slave");
1511 ds_put_format(string, "%u", role);
1516 ofp_print_nxt_set_flow_format(struct ds *string,
1517 const struct nxt_set_flow_format *nsff)
1519 uint32_t format = ntohl(nsff->format);
1521 ds_put_cstr(string, " format=");
1522 if (ofputil_flow_format_is_valid(format)) {
1523 ds_put_cstr(string, ofputil_flow_format_to_string(format));
1525 ds_put_format(string, "%"PRIu32, format);
1530 ofp_to_string__(const struct ofp_header *oh,
1531 const struct ofputil_msg_type *type, struct ds *string,
1534 enum ofputil_msg_code code;
1535 const void *msg = oh;
1537 ds_put_format(string, "%s (xid=0x%"PRIx32"):",
1538 ofputil_msg_type_name(type), ntohl(oh->xid));
1540 code = ofputil_msg_type_code(type);
1542 case OFPUTIL_INVALID:
1545 case OFPUTIL_OFPT_HELLO:
1548 case OFPUTIL_OFPT_ERROR:
1549 ofp_print_error_msg(string, msg);
1552 case OFPUTIL_OFPT_ECHO_REQUEST:
1553 case OFPUTIL_OFPT_ECHO_REPLY:
1554 ofp_print_echo(string, oh, verbosity);
1557 case OFPUTIL_OFPT_FEATURES_REQUEST:
1560 case OFPUTIL_OFPT_FEATURES_REPLY:
1561 ofp_print_switch_features(string, msg);
1564 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1567 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1568 case OFPUTIL_OFPT_SET_CONFIG:
1569 ofp_print_switch_config(string, msg);
1572 case OFPUTIL_OFPT_PACKET_IN:
1573 ofp_print_packet_in(string, msg, verbosity);
1576 case OFPUTIL_OFPT_FLOW_REMOVED:
1577 case OFPUTIL_NXT_FLOW_REMOVED:
1578 ofp_print_flow_removed(string, msg);
1581 case OFPUTIL_OFPT_PORT_STATUS:
1582 ofp_print_port_status(string, msg);
1585 case OFPUTIL_OFPT_PACKET_OUT:
1586 ofp_print_packet_out(string, msg, verbosity);
1589 case OFPUTIL_OFPT_FLOW_MOD:
1590 ofp_print_flow_mod(string, msg, code, verbosity);
1593 case OFPUTIL_OFPT_PORT_MOD:
1594 ofp_print_port_mod(string, msg);
1597 case OFPUTIL_OFPT_BARRIER_REQUEST:
1598 case OFPUTIL_OFPT_BARRIER_REPLY:
1601 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1602 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1606 case OFPUTIL_OFPST_DESC_REQUEST:
1607 ofp_print_stats_request(string, oh);
1610 case OFPUTIL_OFPST_FLOW_REQUEST:
1611 case OFPUTIL_NXST_FLOW_REQUEST:
1612 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1613 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1614 ofp_print_stats_request(string, oh);
1615 ofp_print_flow_stats_request(string, oh);
1618 case OFPUTIL_OFPST_TABLE_REQUEST:
1619 ofp_print_stats_request(string, oh);
1622 case OFPUTIL_OFPST_PORT_REQUEST:
1623 ofp_print_stats_request(string, oh);
1624 ofp_print_ofpst_port_request(string, oh);
1627 case OFPUTIL_OFPST_QUEUE_REQUEST:
1628 ofp_print_stats_request(string, oh);
1629 ofp_print_ofpst_queue_request(string, oh);
1632 case OFPUTIL_OFPST_DESC_REPLY:
1633 ofp_print_stats_reply(string, oh);
1634 ofp_print_ofpst_desc_reply(string, oh);
1637 case OFPUTIL_OFPST_FLOW_REPLY:
1638 ofp_print_stats_reply(string, oh);
1639 ofp_print_ofpst_flow_reply(string, oh, verbosity);
1642 case OFPUTIL_OFPST_QUEUE_REPLY:
1643 ofp_print_stats_reply(string, oh);
1644 ofp_print_ofpst_queue_reply(string, oh, verbosity);
1647 case OFPUTIL_OFPST_PORT_REPLY:
1648 ofp_print_stats_reply(string, oh);
1649 ofp_print_ofpst_port_reply(string, oh, verbosity);
1652 case OFPUTIL_OFPST_TABLE_REPLY:
1653 ofp_print_stats_reply(string, oh);
1654 ofp_print_ofpst_table_reply(string, oh, verbosity);
1657 case OFPUTIL_OFPST_AGGREGATE_REPLY:
1658 ofp_print_stats_reply(string, oh);
1659 ofp_print_ofpst_aggregate_reply(string, oh);
1662 case OFPUTIL_NXT_STATUS_REQUEST:
1663 case OFPUTIL_NXT_STATUS_REPLY:
1664 ofp_print_nxt_status_message(string, oh);
1667 case OFPUTIL_NXT_TUN_ID_FROM_COOKIE:
1668 ofp_print_nxt_tun_id_from_cookie(string, msg);
1671 case OFPUTIL_NXT_ROLE_REQUEST:
1672 case OFPUTIL_NXT_ROLE_REPLY:
1673 ofp_print_nxt_role_message(string, msg);
1676 case OFPUTIL_NXT_SET_FLOW_FORMAT:
1677 ofp_print_nxt_set_flow_format(string, msg);
1680 case OFPUTIL_NXT_FLOW_MOD:
1681 ofp_print_flow_mod(string, msg, code, verbosity);
1684 case OFPUTIL_NXST_FLOW_REPLY:
1685 ofp_print_nxst_flow_reply(string, oh);
1688 case OFPUTIL_NXST_AGGREGATE_REPLY:
1689 ofp_print_stats_reply(string, oh);
1690 ofp_print_nxst_aggregate_reply(string, msg);
1695 /* Composes and returns a string representing the OpenFlow packet of 'len'
1696 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1697 * verbosity and higher numbers increase verbosity. The caller is responsible
1698 * for freeing the string. */
1700 ofp_to_string(const void *oh_, size_t len, int verbosity)
1702 struct ds string = DS_EMPTY_INITIALIZER;
1703 const struct ofp_header *oh = oh_;
1705 if (len < sizeof(struct ofp_header)) {
1706 ds_put_cstr(&string, "OpenFlow packet too short:\n");
1707 } else if (oh->version != OFP_VERSION) {
1708 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n",
1710 } else if (ntohs(oh->length) > len) {
1711 ds_put_format(&string,
1712 "(***truncated to %zu bytes from %"PRIu16"***)",
1713 len, ntohs(oh->length));
1714 } else if (ntohs(oh->length) < len) {
1715 ds_put_format(&string,
1716 "(***only uses %"PRIu16" bytes out of %zu***)\n",
1717 ntohs(oh->length), len);
1719 const struct ofputil_msg_type *type;
1722 error = ofputil_decode_msg_type(oh, &type);
1724 ofp_to_string__(oh, type, &string, verbosity);
1725 if (verbosity >= 5) {
1726 if (ds_last(&string) != '\n') {
1727 ds_put_char(&string, '\n');
1729 ds_put_hex_dump(&string, oh, len, 0, true);
1731 if (ds_last(&string) != '\n') {
1732 ds_put_char(&string, '\n');
1734 return ds_steal_cstr(&string);
1737 ofp_print_error(&string, error);
1739 ds_put_hex_dump(&string, oh, len, 0, true);
1740 return ds_steal_cstr(&string);
1743 /* Returns the name for the specified OpenFlow message type as a string,
1744 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1745 * hex number, e.g. "0x55".
1747 * The caller must free the returned string when it is no longer needed. */
1749 ofp_message_type_to_string(uint8_t type)
1760 case OFPT_ECHO_REQUEST:
1761 name = "ECHO_REQUEST";
1763 case OFPT_ECHO_REPLY:
1764 name = "ECHO_REPLY";
1769 case OFPT_FEATURES_REQUEST:
1770 name = "FEATURES_REQUEST";
1772 case OFPT_FEATURES_REPLY:
1773 name = "FEATURES_REPLY";
1775 case OFPT_GET_CONFIG_REQUEST:
1776 name = "GET_CONFIG_REQUEST";
1778 case OFPT_GET_CONFIG_REPLY:
1779 name = "GET_CONFIG_REPLY";
1781 case OFPT_SET_CONFIG:
1782 name = "SET_CONFIG";
1784 case OFPT_PACKET_IN:
1787 case OFPT_FLOW_REMOVED:
1788 name = "FLOW_REMOVED";
1790 case OFPT_PORT_STATUS:
1791 name = "PORT_STATUS";
1793 case OFPT_PACKET_OUT:
1794 name = "PACKET_OUT";
1802 case OFPT_STATS_REQUEST:
1803 name = "STATS_REQUEST";
1805 case OFPT_STATS_REPLY:
1806 name = "STATS_REPLY";
1808 case OFPT_BARRIER_REQUEST:
1809 name = "BARRIER_REQUEST";
1811 case OFPT_BARRIER_REPLY:
1812 name = "BARRIER_REPLY";
1814 case OFPT_QUEUE_GET_CONFIG_REQUEST:
1815 name = "QUEUE_GET_CONFIG_REQUEST";
1817 case OFPT_QUEUE_GET_CONFIG_REPLY:
1818 name = "QUEUE_GET_CONFIG_REPLY";
1825 return name ? xasprintf("OFPT_%s", name) : xasprintf("0x%02"PRIx8, type);
1829 print_and_free(FILE *stream, char *string)
1831 fputs(string, stream);
1835 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1836 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1837 * numbers increase verbosity. */
1839 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1841 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1844 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1845 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1846 * the Ethernet frame (of which 'len' bytes were captured).
1848 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1850 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1852 print_and_free(stream, ofp_packet_to_string(data, len, total_len));