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"
42 static void ofp_print_port_name(struct ds *string, uint16_t port);
43 static void ofp_print_queue_name(struct ds *string, uint32_t port);
44 static void ofp_print_error(struct ds *, int error);
47 /* Returns a string that represents the contents of the Ethernet frame in the
48 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
49 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
50 * bytes were captured).
52 * The caller must free the returned string.
54 * This starts and kills a tcpdump subprocess so it's quite expensive. */
56 ofp_packet_to_string(const void *data, size_t len, size_t total_len OVS_UNUSED)
58 struct ds ds = DS_EMPTY_INITIALIZER;
67 ofpbuf_use_const(&buf, data, len);
71 ovs_error(errno, "tmpfile");
72 return xstrdup("<error>");
74 pcap_write_header(pcap);
75 pcap_write(pcap, &buf);
78 ovs_error(errno, "error writing temporary file");
82 snprintf(command, sizeof command, "/usr/sbin/tcpdump -e -n -r /dev/fd/%d 2>/dev/null",
84 tcpdump = popen(command, "r");
87 ovs_error(errno, "exec(\"%s\")", command);
88 return xstrdup("<error>");
91 while ((c = getc(tcpdump)) != EOF) {
95 status = pclose(tcpdump);
96 if (WIFEXITED(status)) {
97 if (WEXITSTATUS(status))
98 ovs_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
99 } else if (WIFSIGNALED(status)) {
100 ovs_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
106 ofp_print_packet_in(struct ds *string, const struct ofp_packet_in *op,
109 size_t len = ntohs(op->header.length);
112 ds_put_format(string, " total_len=%"PRIu16" in_port=",
113 ntohs(op->total_len));
114 ofp_print_port_name(string, ntohs(op->in_port));
116 if (op->reason == OFPR_ACTION)
117 ds_put_cstr(string, " (via action)");
118 else if (op->reason != OFPR_NO_MATCH)
119 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
121 data_len = len - offsetof(struct ofp_packet_in, data);
122 ds_put_format(string, " data_len=%zu", data_len);
123 if (htonl(op->buffer_id) == UINT32_MAX) {
124 ds_put_format(string, " (unbuffered)");
125 if (ntohs(op->total_len) != data_len)
126 ds_put_format(string, " (***total_len != data_len***)");
128 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
129 if (ntohs(op->total_len) < data_len)
130 ds_put_format(string, " (***total_len < data_len***)");
132 ds_put_char(string, '\n');
136 struct ofpbuf packet;
138 ofpbuf_use_const(&packet, op->data, data_len);
139 flow_extract(&packet, 0, ntohs(op->in_port), &flow);
140 flow_format(string, &flow);
141 ds_put_char(string, '\n');
144 char *packet = ofp_packet_to_string(op->data, data_len,
145 ntohs(op->total_len));
146 ds_put_cstr(string, packet);
151 static void ofp_print_port_name(struct ds *string, uint16_t port)
170 case OFPP_CONTROLLER:
180 ds_put_format(string, "%"PRIu16, port);
183 ds_put_cstr(string, name);
187 print_note(struct ds *string, const struct nx_action_note *nan)
192 ds_put_cstr(string, "note:");
193 len = ntohs(nan->len) - offsetof(struct nx_action_note, note);
194 for (i = 0; i < len; i++) {
196 ds_put_char(string, '.');
198 ds_put_format(string, "%02"PRIx8, nan->note[i]);
203 ofp_print_nx_action(struct ds *string, const struct nx_action_header *nah)
205 switch (ntohs(nah->subtype)) {
206 case NXAST_RESUBMIT: {
207 const struct nx_action_resubmit *nar = (struct nx_action_resubmit *)nah;
208 ds_put_format(string, "resubmit:");
209 ofp_print_port_name(string, ntohs(nar->in_port));
213 case NXAST_SET_TUNNEL: {
214 const struct nx_action_set_tunnel *nast =
215 (struct nx_action_set_tunnel *)nah;
216 ds_put_format(string, "set_tunnel:%#"PRIx32, ntohl(nast->tun_id));
220 case NXAST_DROP_SPOOFED_ARP:
221 ds_put_cstr(string, "drop_spoofed_arp");
224 case NXAST_SET_QUEUE: {
225 const struct nx_action_set_queue *nasq =
226 (struct nx_action_set_queue *)nah;
227 ds_put_format(string, "set_queue:%u", ntohl(nasq->queue_id));
231 case NXAST_POP_QUEUE:
232 ds_put_cstr(string, "pop_queue");
236 print_note(string, (const struct nx_action_note *) nah);
240 ds_put_format(string, "***unknown Nicira action:%d***",
241 ntohs(nah->subtype));
246 ofp_print_action(struct ds *string, const struct ofp_action_header *ah,
252 struct openflow_action {
257 const struct openflow_action of_actions[] = {
259 sizeof(struct ofp_action_output),
260 sizeof(struct ofp_action_output),
262 [OFPAT_SET_VLAN_VID] = {
263 sizeof(struct ofp_action_vlan_vid),
264 sizeof(struct ofp_action_vlan_vid),
266 [OFPAT_SET_VLAN_PCP] = {
267 sizeof(struct ofp_action_vlan_pcp),
268 sizeof(struct ofp_action_vlan_pcp),
270 [OFPAT_STRIP_VLAN] = {
271 sizeof(struct ofp_action_header),
272 sizeof(struct ofp_action_header),
274 [OFPAT_SET_DL_SRC] = {
275 sizeof(struct ofp_action_dl_addr),
276 sizeof(struct ofp_action_dl_addr),
278 [OFPAT_SET_DL_DST] = {
279 sizeof(struct ofp_action_dl_addr),
280 sizeof(struct ofp_action_dl_addr),
282 [OFPAT_SET_NW_SRC] = {
283 sizeof(struct ofp_action_nw_addr),
284 sizeof(struct ofp_action_nw_addr),
286 [OFPAT_SET_NW_DST] = {
287 sizeof(struct ofp_action_nw_addr),
288 sizeof(struct ofp_action_nw_addr),
290 [OFPAT_SET_NW_TOS] = {
291 sizeof(struct ofp_action_nw_tos),
292 sizeof(struct ofp_action_nw_tos),
294 [OFPAT_SET_TP_SRC] = {
295 sizeof(struct ofp_action_tp_port),
296 sizeof(struct ofp_action_tp_port),
298 [OFPAT_SET_TP_DST] = {
299 sizeof(struct ofp_action_tp_port),
300 sizeof(struct ofp_action_tp_port),
302 /* OFPAT_VENDOR is not here, since it would blow up the array size. */
305 if (actions_len < sizeof *ah) {
306 ds_put_format(string, "***action array too short for next action***\n");
310 type = ntohs(ah->type);
311 len = ntohs(ah->len);
312 if (actions_len < len) {
313 ds_put_format(string, "***truncated action %"PRIu16"***\n", type);
318 ds_put_format(string, "***zero-length action***\n");
322 if ((len % OFP_ACTION_ALIGN) != 0) {
323 ds_put_format(string,
324 "***action %"PRIu16" length not a multiple of %d***\n",
325 type, OFP_ACTION_ALIGN);
329 if (type < ARRAY_SIZE(of_actions)) {
330 const struct openflow_action *act = &of_actions[type];
331 if ((len < act->min_size) || (len > act->max_size)) {
332 ds_put_format(string,
333 "***action %"PRIu16" wrong length: %zu***\n", type, len);
340 struct ofp_action_output *oa = (struct ofp_action_output *)ah;
341 uint16_t port = ntohs(oa->port);
342 if (port < OFPP_MAX) {
343 ds_put_format(string, "output:%"PRIu16, port);
345 ofp_print_port_name(string, port);
346 if (port == OFPP_CONTROLLER) {
348 ds_put_format(string, ":%"PRIu16, ntohs(oa->max_len));
350 ds_put_cstr(string, ":all");
357 case OFPAT_ENQUEUE: {
358 struct ofp_action_enqueue *ea = (struct ofp_action_enqueue *)ah;
359 unsigned int port = ntohs(ea->port);
360 unsigned int queue_id = ntohl(ea->queue_id);
361 ds_put_format(string, "enqueue:");
362 if (port != OFPP_IN_PORT) {
363 ds_put_format(string, "%u", port);
365 ds_put_cstr(string, "IN_PORT");
367 ds_put_format(string, "q%u", queue_id);
371 case OFPAT_SET_VLAN_VID: {
372 struct ofp_action_vlan_vid *va = (struct ofp_action_vlan_vid *)ah;
373 ds_put_format(string, "mod_vlan_vid:%"PRIu16, ntohs(va->vlan_vid));
377 case OFPAT_SET_VLAN_PCP: {
378 struct ofp_action_vlan_pcp *va = (struct ofp_action_vlan_pcp *)ah;
379 ds_put_format(string, "mod_vlan_pcp:%"PRIu8, va->vlan_pcp);
383 case OFPAT_STRIP_VLAN:
384 ds_put_cstr(string, "strip_vlan");
387 case OFPAT_SET_DL_SRC: {
388 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
389 ds_put_format(string, "mod_dl_src:"ETH_ADDR_FMT,
390 ETH_ADDR_ARGS(da->dl_addr));
394 case OFPAT_SET_DL_DST: {
395 struct ofp_action_dl_addr *da = (struct ofp_action_dl_addr *)ah;
396 ds_put_format(string, "mod_dl_dst:"ETH_ADDR_FMT,
397 ETH_ADDR_ARGS(da->dl_addr));
401 case OFPAT_SET_NW_SRC: {
402 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
403 ds_put_format(string, "mod_nw_src:"IP_FMT, IP_ARGS(&na->nw_addr));
407 case OFPAT_SET_NW_DST: {
408 struct ofp_action_nw_addr *na = (struct ofp_action_nw_addr *)ah;
409 ds_put_format(string, "mod_nw_dst:"IP_FMT, IP_ARGS(&na->nw_addr));
413 case OFPAT_SET_NW_TOS: {
414 struct ofp_action_nw_tos *nt = (struct ofp_action_nw_tos *)ah;
415 ds_put_format(string, "mod_nw_tos:%d", nt->nw_tos);
419 case OFPAT_SET_TP_SRC: {
420 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
421 ds_put_format(string, "mod_tp_src:%d", ntohs(ta->tp_port));
425 case OFPAT_SET_TP_DST: {
426 struct ofp_action_tp_port *ta = (struct ofp_action_tp_port *)ah;
427 ds_put_format(string, "mod_tp_dst:%d", ntohs(ta->tp_port));
432 struct ofp_action_vendor_header *avh
433 = (struct ofp_action_vendor_header *)ah;
434 if (len < sizeof *avh) {
435 ds_put_format(string, "***ofpat_vendor truncated***\n");
438 if (avh->vendor == htonl(NX_VENDOR_ID)) {
439 ofp_print_nx_action(string, (struct nx_action_header *)avh);
441 ds_put_format(string, "vendor action:0x%x", ntohl(avh->vendor));
447 ds_put_format(string, "(decoder %"PRIu16" not implemented)", type);
455 ofp_print_actions(struct ds *string, const struct ofp_action_header *action,
458 uint8_t *p = (uint8_t *)action;
461 ds_put_cstr(string, "actions=");
463 ds_put_cstr(string, "drop");
465 while (actions_len > 0) {
467 ds_put_cstr(string, ",");
469 len = ofp_print_action(string, (struct ofp_action_header *)p,
480 ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
483 size_t len = ntohs(opo->header.length);
484 size_t actions_len = ntohs(opo->actions_len);
486 ds_put_cstr(string, " in_port=");
487 ofp_print_port_name(string, ntohs(opo->in_port));
489 ds_put_format(string, " actions_len=%zu ", actions_len);
490 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
491 ds_put_format(string, "***packet too short for action length***\n");
494 ofp_print_actions(string, opo->actions, actions_len);
496 if (ntohl(opo->buffer_id) == UINT32_MAX) {
497 int data_len = len - sizeof *opo - actions_len;
498 ds_put_format(string, " data_len=%d", data_len);
499 if (verbosity > 0 && len > sizeof *opo) {
500 char *packet = ofp_packet_to_string(
501 (uint8_t *)opo->actions + actions_len, data_len, data_len);
502 ds_put_char(string, '\n');
503 ds_put_cstr(string, packet);
507 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
509 ds_put_char(string, '\n');
512 /* qsort comparison function. */
514 compare_ports(const void *a_, const void *b_)
516 const struct ofp_phy_port *a = a_;
517 const struct ofp_phy_port *b = b_;
518 uint16_t ap = ntohs(a->port_no);
519 uint16_t bp = ntohs(b->port_no);
521 return ap < bp ? -1 : ap > bp;
524 static void ofp_print_port_features(struct ds *string, uint32_t features)
527 ds_put_cstr(string, "Unsupported\n");
530 if (features & OFPPF_10MB_HD) {
531 ds_put_cstr(string, "10MB-HD ");
533 if (features & OFPPF_10MB_FD) {
534 ds_put_cstr(string, "10MB-FD ");
536 if (features & OFPPF_100MB_HD) {
537 ds_put_cstr(string, "100MB-HD ");
539 if (features & OFPPF_100MB_FD) {
540 ds_put_cstr(string, "100MB-FD ");
542 if (features & OFPPF_1GB_HD) {
543 ds_put_cstr(string, "1GB-HD ");
545 if (features & OFPPF_1GB_FD) {
546 ds_put_cstr(string, "1GB-FD ");
548 if (features & OFPPF_10GB_FD) {
549 ds_put_cstr(string, "10GB-FD ");
551 if (features & OFPPF_COPPER) {
552 ds_put_cstr(string, "COPPER ");
554 if (features & OFPPF_FIBER) {
555 ds_put_cstr(string, "FIBER ");
557 if (features & OFPPF_AUTONEG) {
558 ds_put_cstr(string, "AUTO_NEG ");
560 if (features & OFPPF_PAUSE) {
561 ds_put_cstr(string, "AUTO_PAUSE ");
563 if (features & OFPPF_PAUSE_ASYM) {
564 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
566 ds_put_char(string, '\n');
570 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
572 char name[OFP_MAX_PORT_NAME_LEN];
575 memcpy(name, port->name, sizeof name);
576 for (j = 0; j < sizeof name - 1; j++) {
577 if (!isprint(name[j])) {
583 ds_put_char(string, ' ');
584 ofp_print_port_name(string, ntohs(port->port_no));
585 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
586 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
589 ds_put_format(string, " current: ");
590 ofp_print_port_features(string, ntohl(port->curr));
592 if (port->advertised) {
593 ds_put_format(string, " advertised: ");
594 ofp_print_port_features(string, ntohl(port->advertised));
596 if (port->supported) {
597 ds_put_format(string, " supported: ");
598 ofp_print_port_features(string, ntohl(port->supported));
601 ds_put_format(string, " peer: ");
602 ofp_print_port_features(string, ntohl(port->peer));
607 ofp_print_switch_features(struct ds *string,
608 const struct ofp_switch_features *osf)
610 size_t len = ntohs(osf->header.length);
611 struct ofp_phy_port *port_list;
615 ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
616 osf->header.version, ntohll(osf->datapath_id));
617 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
618 ntohl(osf->n_buffers));
619 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
620 ntohl(osf->capabilities), ntohl(osf->actions));
622 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
624 port_list = xmemdup(osf->ports, len - sizeof *osf);
625 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
626 for (i = 0; i < n_ports; i++) {
627 ofp_print_phy_port(string, &port_list[i]);
633 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
637 flags = ntohs(osc->flags);
639 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
642 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
645 static void print_wild(struct ds *string, const char *leader, int is_wild,
646 int verbosity, const char *format, ...)
647 __attribute__((format(printf, 5, 6)));
649 static void print_wild(struct ds *string, const char *leader, int is_wild,
650 int verbosity, const char *format, ...)
652 if (is_wild && verbosity < 2) {
655 ds_put_cstr(string, leader);
659 va_start(args, format);
660 ds_put_format_valist(string, format, args);
663 ds_put_char(string, '*');
665 ds_put_char(string, ',');
669 print_ip_netmask(struct ds *string, const char *leader, uint32_t ip,
670 uint32_t wild_bits, int verbosity)
672 if (wild_bits >= 32 && verbosity < 2) {
675 ds_put_cstr(string, leader);
676 if (wild_bits < 32) {
677 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
679 ds_put_format(string, "/%d", 32 - wild_bits);
682 ds_put_char(string, '*');
684 ds_put_char(string, ',');
688 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
690 char *s = ofp_match_to_string(om, verbosity);
696 ofp_match_to_string(const struct ofp_match *om, int verbosity)
698 struct ds f = DS_EMPTY_INITIALIZER;
699 uint32_t w = ntohl(om->wildcards);
700 bool skip_type = false;
701 bool skip_proto = false;
703 if (!(w & OFPFW_DL_TYPE)) {
705 if (om->dl_type == htons(ETH_TYPE_IP)) {
706 if (!(w & OFPFW_NW_PROTO)) {
708 if (om->nw_proto == IP_TYPE_ICMP) {
709 ds_put_cstr(&f, "icmp,");
710 } else if (om->nw_proto == IP_TYPE_TCP) {
711 ds_put_cstr(&f, "tcp,");
712 } else if (om->nw_proto == IP_TYPE_UDP) {
713 ds_put_cstr(&f, "udp,");
715 ds_put_cstr(&f, "ip,");
719 ds_put_cstr(&f, "ip,");
721 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
722 ds_put_cstr(&f, "arp,");
727 if (w & NXFW_TUN_ID) {
728 ds_put_cstr(&f, "tun_id_wild,");
730 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
731 "%d", ntohs(om->in_port));
732 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
733 "%d", ntohs(om->dl_vlan));
734 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
735 "%d", om->dl_vlan_pcp);
736 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
737 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
738 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
739 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
741 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
742 "0x%04x", ntohs(om->dl_type));
744 print_ip_netmask(&f, "nw_src=", om->nw_src,
745 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
746 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
747 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
749 if (om->dl_type == htons(ETH_TYPE_ARP)) {
750 print_wild(&f, "opcode=", w & OFPFW_NW_PROTO, verbosity,
753 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
757 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
759 if (om->nw_proto == IP_TYPE_ICMP) {
760 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
761 "%d", ntohs(om->icmp_type));
762 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
763 "%d", ntohs(om->icmp_code));
765 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
766 "%d", ntohs(om->tp_src));
767 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
768 "%d", ntohs(om->tp_dst));
770 if (ds_last(&f) == ',') {
777 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
778 enum ofputil_msg_code code, int verbosity)
783 error = ofputil_decode_flow_mod(&fm, oh, NXFF_OPENFLOW10);
785 ofp_print_error(s, error);
790 switch (fm.command) {
792 ds_put_cstr(s, "ADD");
795 ds_put_cstr(s, "MOD");
797 case OFPFC_MODIFY_STRICT:
798 ds_put_cstr(s, "MOD_STRICT");
801 ds_put_cstr(s, "DEL");
803 case OFPFC_DELETE_STRICT:
804 ds_put_cstr(s, "DEL_STRICT");
807 ds_put_format(s, "cmd:%d", fm.command);
811 if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
812 const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
814 ofp_print_match(s, &ofm->match, verbosity);
815 } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
816 const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
817 const void *nxm = nfm + 1;
818 char *nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
819 ds_put_cstr(s, nxm_s);
822 cls_rule_format(&fm.cr, s);
825 if (ds_last(s) != ' ') {
828 if (fm.cookie != htonll(0)) {
829 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.cookie));
831 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
832 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
834 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
835 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
837 if (fm.cr.priority != OFP_DEFAULT_PRIORITY && verbosity >= 3) {
838 ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
840 if (fm.buffer_id != UINT32_MAX) {
841 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
844 ds_put_format(s, "flags:0x%"PRIx16" ", fm.flags);
847 ofp_print_actions(s, (const struct ofp_action_header *) fm.actions,
848 fm.n_actions * sizeof *fm.actions);
852 ofp_print_flow_removed(struct ds *string, const struct ofp_flow_removed *ofr,
855 ofp_print_match(string, &ofr->match, verbosity);
856 ds_put_cstr(string, " reason=");
857 switch (ofr->reason) {
858 case OFPRR_IDLE_TIMEOUT:
859 ds_put_cstr(string, "idle");
861 case OFPRR_HARD_TIMEOUT:
862 ds_put_cstr(string, "hard");
865 ds_put_cstr(string, "delete");
868 ds_put_format(string, "**%"PRIu8"**", ofr->reason);
872 if (ofr->cookie != htonll(0)) {
873 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(ofr->cookie));
875 if (ofr->priority != htons(32768)) {
876 ds_put_format(string, " pri:%"PRIu16, ntohs(ofr->priority));
878 ds_put_format(string, " secs%"PRIu32" nsecs%"PRIu32
879 " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
880 ntohl(ofr->duration_sec), ntohl(ofr->duration_nsec),
881 ntohs(ofr->idle_timeout), ntohll(ofr->packet_count),
882 ntohll(ofr->byte_count));
886 ofp_print_port_mod(struct ds *string, const struct ofp_port_mod *opm)
888 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
889 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
890 ntohl(opm->config), ntohl(opm->mask));
891 ds_put_format(string, " advertise: ");
892 if (opm->advertise) {
893 ofp_print_port_features(string, ntohl(opm->advertise));
895 ds_put_format(string, "UNCHANGED\n");
905 static const struct error_type error_types[] = {
906 #define ERROR_TYPE(TYPE) {TYPE, -1, #TYPE}
907 #define ERROR_CODE(TYPE, CODE) {TYPE, CODE, #CODE}
908 ERROR_TYPE(OFPET_HELLO_FAILED),
909 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_INCOMPATIBLE),
910 ERROR_CODE(OFPET_HELLO_FAILED, OFPHFC_EPERM),
912 ERROR_TYPE(OFPET_BAD_REQUEST),
913 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VERSION),
914 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_TYPE),
915 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_STAT),
916 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_VENDOR),
917 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_SUBTYPE),
918 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_EPERM),
919 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BAD_LEN),
920 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_EMPTY),
921 ERROR_CODE(OFPET_BAD_REQUEST, OFPBRC_BUFFER_UNKNOWN),
922 /* Nicira error extensions. */
923 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_INVALID),
924 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_TYPE),
925 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_VALUE),
926 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_MASK),
927 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_BAD_PREREQ),
928 ERROR_CODE(OFPET_BAD_REQUEST, NXBRC_NXM_DUP_TYPE),
930 ERROR_TYPE(OFPET_BAD_ACTION),
931 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_TYPE),
932 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_LEN),
933 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR),
934 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_VENDOR_TYPE),
935 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_OUT_PORT),
936 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_BAD_ARGUMENT),
937 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_EPERM),
938 ERROR_CODE(OFPET_BAD_ACTION, OFPBAC_TOO_MANY),
940 ERROR_TYPE(OFPET_FLOW_MOD_FAILED),
941 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_ALL_TABLES_FULL),
942 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_OVERLAP),
943 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_EPERM),
944 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_EMERG_TIMEOUT),
945 ERROR_CODE(OFPET_FLOW_MOD_FAILED, OFPFMFC_BAD_COMMAND),
946 /* Nicira error extenstions. */
947 ERROR_CODE(OFPET_FLOW_MOD_FAILED, NXFMFC_HARDWARE),
948 ERROR_CODE(OFPET_FLOW_MOD_FAILED, NXFMFC_BAD_TABLE_ID),
950 ERROR_TYPE(OFPET_PORT_MOD_FAILED),
951 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_PORT),
952 ERROR_CODE(OFPET_PORT_MOD_FAILED, OFPPMFC_BAD_HW_ADDR)
954 #define N_ERROR_TYPES ARRAY_SIZE(error_types)
957 lookup_error_type(int type)
959 const struct error_type *t;
961 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
962 if (t->type == type && t->code == -1) {
970 lookup_error_code(int type, int code)
972 const struct error_type *t;
974 for (t = error_types; t < &error_types[N_ERROR_TYPES]; t++) {
975 if (t->type == type && t->code == code) {
983 ofp_print_error(struct ds *string, int error)
985 int type = get_ofp_err_type(error);
986 int code = get_ofp_err_code(error);
987 if (string->length) {
988 ds_put_char(string, ' ');
990 ds_put_format(string, " ***decode error type:%d(%s) code:%d(%s)***",
991 type, lookup_error_type(type),
992 code, lookup_error_code(type, code));
996 ofp_print_nx_error_msg(struct ds *string, const struct ofp_error_msg *oem)
998 size_t len = ntohs(oem->header.length);
999 const struct nx_vendor_error *nve = (struct nx_vendor_error *)oem->data;
1000 int vendor = ntohl(nve->vendor);
1001 int type = ntohs(nve->type);
1002 int code = ntohs(nve->code);
1004 ds_put_format(string, " vendor:%x type:%d(%s) code:%d(%s) payload:\n",
1006 type, lookup_error_type(type),
1007 code, lookup_error_code(type, code));
1009 ds_put_hex_dump(string, nve + 1, len - sizeof *oem - sizeof *nve,
1014 ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
1016 size_t len = ntohs(oem->header.length);
1017 int type = ntohs(oem->type);
1018 int code = ntohs(oem->code);
1022 if (type == NXET_VENDOR && code == NXVC_VENDOR_ERROR) {
1023 if (len < sizeof *oem + sizeof(struct nx_vendor_error)) {
1024 ds_put_format(&string,
1025 "(***truncated extended error message is %zu bytes "
1026 "when it should be at least %zu***)\n"
1027 len, sizeof(struct nx_vendor_error));
1031 return ofp_print_nx_error_msg(string, oem);
1034 ds_put_format(string, " type:%d(%s) code:%d(%s) payload:\n",
1035 type, lookup_error_type(type),
1036 code, lookup_error_code(type, code));
1039 case OFPET_HELLO_FAILED:
1040 ds_put_printable(string, (char *) oem->data, len - sizeof *oem);
1043 case OFPET_BAD_REQUEST:
1044 s = ofp_to_string(oem->data, len - sizeof *oem, 1);
1045 ds_put_cstr(string, s);
1050 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
1056 ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
1058 if (ops->reason == OFPPR_ADD) {
1059 ds_put_format(string, " ADD:");
1060 } else if (ops->reason == OFPPR_DELETE) {
1061 ds_put_format(string, " DEL:");
1062 } else if (ops->reason == OFPPR_MODIFY) {
1063 ds_put_format(string, " MOD:");
1066 ofp_print_phy_port(string, &ops->desc);
1070 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_header *oh)
1072 const struct ofp_desc_stats *ods = ofputil_stats_body(oh);
1074 ds_put_format(string, "Manufacturer: %.*s\n",
1075 (int) sizeof ods->mfr_desc, ods->mfr_desc);
1076 ds_put_format(string, "Hardware: %.*s\n",
1077 (int) sizeof ods->hw_desc, ods->hw_desc);
1078 ds_put_format(string, "Software: %.*s\n",
1079 (int) sizeof ods->sw_desc, ods->sw_desc);
1080 ds_put_format(string, "Serial Num: %.*s\n",
1081 (int) sizeof ods->serial_num, ods->serial_num);
1082 ds_put_format(string, "DP Description: %.*s\n",
1083 (int) sizeof ods->dp_desc, ods->dp_desc);
1087 ofp_print_ofpst_flow_request(struct ds *string, const struct ofp_header *oh,
1090 const struct ofp_flow_stats_request *fsr = ofputil_stats_body(oh);
1092 if (fsr->table_id == 0xff) {
1093 ds_put_format(string, " table_id=any, ");
1095 ds_put_format(string, " table_id=%"PRIu8", ", fsr->table_id);
1098 ofp_print_match(string, &fsr->match, verbosity);
1102 ofp_print_ofpst_flow_reply(struct ds *string, const struct ofp_header *oh,
1105 size_t len = ofputil_stats_body_len(oh);
1106 const char *body = ofputil_stats_body(oh);
1107 const char *pos = body;
1109 const struct ofp_flow_stats *fs;
1110 ptrdiff_t bytes_left = body + len - pos;
1113 ds_put_char(string, '\n');
1115 if (bytes_left < sizeof *fs) {
1116 if (bytes_left != 0) {
1117 ds_put_format(string, " ***%td leftover bytes at end***",
1123 fs = (const void *) pos;
1124 length = ntohs(fs->length);
1125 if (length < sizeof *fs) {
1126 ds_put_format(string, " ***length=%zu shorter than minimum %zu***",
1127 length, sizeof *fs);
1129 } else if (length > bytes_left) {
1130 ds_put_format(string,
1131 " ***length=%zu but only %td bytes left***",
1132 length, bytes_left);
1134 } else if ((length - sizeof *fs) % sizeof fs->actions[0]) {
1135 ds_put_format(string,
1136 " ***length=%zu has %zu bytes leftover in "
1139 (length - sizeof *fs) % sizeof fs->actions[0]);
1143 ds_put_format(string, " cookie=0x%"PRIx64", ", ntohll(fs->cookie));
1144 ds_put_format(string, "duration_sec=%"PRIu32"s, ",
1145 ntohl(fs->duration_sec));
1146 ds_put_format(string, "duration_nsec=%"PRIu32"ns, ",
1147 ntohl(fs->duration_nsec));
1148 ds_put_format(string, "table_id=%"PRIu8", ", fs->table_id);
1149 ds_put_format(string, "priority=%"PRIu16", ",
1150 fs->match.wildcards ? ntohs(fs->priority) : (uint16_t)-1);
1151 ds_put_format(string, "n_packets=%"PRIu64", ",
1152 ntohll(fs->packet_count));
1153 ds_put_format(string, "n_bytes=%"PRIu64", ", ntohll(fs->byte_count));
1154 if (fs->idle_timeout != htons(OFP_FLOW_PERMANENT)) {
1155 ds_put_format(string, "idle_timeout=%"PRIu16",",
1156 ntohs(fs->idle_timeout));
1158 if (fs->hard_timeout != htons(OFP_FLOW_PERMANENT)) {
1159 ds_put_format(string, "hard_timeout=%"PRIu16",",
1160 ntohs(fs->hard_timeout));
1162 ofp_print_match(string, &fs->match, verbosity);
1163 ofp_print_actions(string, fs->actions, length - sizeof *fs);
1170 ofp_print_ofpst_aggregate_request(struct ds *string,
1171 const struct ofp_header *oh, int verbosity)
1173 const struct ofp_aggregate_stats_request *asr = ofputil_stats_body(oh);
1175 if (asr->table_id == 0xff) {
1176 ds_put_format(string, " table_id=any, ");
1178 ds_put_format(string, " table_id=%"PRIu8", ", asr->table_id);
1181 ofp_print_match(string, &asr->match, verbosity);
1185 ofp_print_ofpst_aggregate_reply(struct ds *string, const struct ofp_header *oh)
1187 const struct ofp_aggregate_stats_reply *asr = ofputil_stats_body(oh);
1189 ds_put_format(string, " packet_count=%"PRIu64, ntohll(asr->packet_count));
1190 ds_put_format(string, " byte_count=%"PRIu64, ntohll(asr->byte_count));
1191 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1194 static void print_port_stat(struct ds *string, const char *leader,
1195 uint64_t stat, int more)
1197 ds_put_cstr(string, leader);
1199 ds_put_format(string, "%"PRIu64, stat);
1201 ds_put_char(string, '?');
1204 ds_put_cstr(string, ", ");
1206 ds_put_cstr(string, "\n");
1211 ofp_print_ofpst_port_request(struct ds *string, const struct ofp_header *oh)
1213 const struct ofp_port_stats_request *psr = ofputil_stats_body(oh);
1214 ds_put_format(string, "port_no=%"PRIu16, ntohs(psr->port_no));
1218 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1221 const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1222 size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
1223 ds_put_format(string, " %zu ports\n", n);
1224 if (verbosity < 1) {
1229 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1231 ds_put_cstr(string, "rx ");
1232 print_port_stat(string, "pkts=", ntohll(ps->rx_packets), 1);
1233 print_port_stat(string, "bytes=", ntohll(ps->rx_bytes), 1);
1234 print_port_stat(string, "drop=", ntohll(ps->rx_dropped), 1);
1235 print_port_stat(string, "errs=", ntohll(ps->rx_errors), 1);
1236 print_port_stat(string, "frame=", ntohll(ps->rx_frame_err), 1);
1237 print_port_stat(string, "over=", ntohll(ps->rx_over_err), 1);
1238 print_port_stat(string, "crc=", ntohll(ps->rx_crc_err), 0);
1240 ds_put_cstr(string, " tx ");
1241 print_port_stat(string, "pkts=", ntohll(ps->tx_packets), 1);
1242 print_port_stat(string, "bytes=", ntohll(ps->tx_bytes), 1);
1243 print_port_stat(string, "drop=", ntohll(ps->tx_dropped), 1);
1244 print_port_stat(string, "errs=", ntohll(ps->tx_errors), 1);
1245 print_port_stat(string, "coll=", ntohll(ps->collisions), 0);
1250 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1253 const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1254 size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
1255 ds_put_format(string, " %zu tables\n", n);
1256 if (verbosity < 1) {
1261 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1262 strncpy(name, ts->name, sizeof name);
1263 name[OFP_MAX_TABLE_NAME_LEN] = '\0';
1265 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1266 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1267 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1268 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1269 ds_put_cstr(string, " ");
1270 ds_put_format(string, "lookup=%"PRIu64", ",
1271 ntohll(ts->lookup_count));
1272 ds_put_format(string, "matched=%"PRIu64"\n",
1273 ntohll(ts->matched_count));
1278 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1280 if (queue_id == OFPQ_ALL) {
1281 ds_put_cstr(string, "ALL");
1283 ds_put_format(string, "%"PRIu32, queue_id);
1288 ofp_print_ofpst_queue_request(struct ds *string, const struct ofp_header *oh)
1290 const struct ofp_queue_stats_request *qsr = ofputil_stats_body(oh);
1292 ds_put_cstr(string, "port=");
1293 ofp_print_port_name(string, ntohs(qsr->port_no));
1295 ds_put_cstr(string, " queue=");
1296 ofp_print_queue_name(string, ntohl(qsr->queue_id));
1300 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1303 const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1304 size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
1305 ds_put_format(string, " %zu queues\n", n);
1306 if (verbosity < 1) {
1311 ds_put_cstr(string, " port ");
1312 ofp_print_port_name(string, ntohs(qs->port_no));
1313 ds_put_cstr(string, " queue ");
1314 ofp_print_queue_name(string, ntohl(qs->queue_id));
1315 ds_put_cstr(string, ": ");
1317 print_port_stat(string, "bytes=", ntohll(qs->tx_bytes), 1);
1318 print_port_stat(string, "pkts=", ntohll(qs->tx_packets), 1);
1319 print_port_stat(string, "errors=", ntohll(qs->tx_errors), 0);
1324 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1326 const struct ofp_stats_request *srq
1327 = (const struct ofp_stats_request *) oh;
1330 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1336 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1338 const struct ofp_stats_reply *srp = (const struct ofp_stats_reply *) oh;
1341 uint16_t flags = ntohs(srp->flags);
1343 ds_put_cstr(string, " flags=");
1344 if (flags & OFPSF_REPLY_MORE) {
1345 ds_put_cstr(string, "[more]");
1346 flags &= ~OFPSF_REPLY_MORE;
1349 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1356 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1358 size_t len = ntohs(oh->length);
1360 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1361 if (verbosity > 1) {
1362 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1367 ofp_print_nxt_tun_id_from_cookie(struct ds *string,
1368 const struct nxt_tun_id_cookie *ntic)
1370 ds_put_format(string, " set=%"PRIu8, ntic->set);
1374 ofp_print_nxt_set_flow_format(struct ds *string,
1375 const struct nxt_set_flow_format *nsff)
1377 uint32_t format = ntohl(nsff->format);
1379 ds_put_cstr(string, " format=");
1380 if (ofputil_flow_format_is_valid(format)) {
1381 ds_put_cstr(string, ofputil_flow_format_to_string(format));
1383 ds_put_format(string, "%"PRIu32, format);
1388 ofp_to_string__(const struct ofp_header *oh,
1389 const struct ofputil_msg_type *type, struct ds *string,
1392 enum ofputil_msg_code code;
1393 const void *msg = oh;
1395 ds_put_format(string, "%s (xid=0x%"PRIx32"):",
1396 ofputil_msg_type_name(type), ntohl(oh->xid));
1398 code = ofputil_msg_type_code(type);
1400 case OFPUTIL_INVALID:
1403 case OFPUTIL_OFPT_HELLO:
1406 case OFPUTIL_OFPT_ERROR:
1407 ofp_print_error_msg(string, msg);
1410 case OFPUTIL_OFPT_ECHO_REQUEST:
1411 case OFPUTIL_OFPT_ECHO_REPLY:
1412 ofp_print_echo(string, oh, verbosity);
1415 case OFPUTIL_OFPT_FEATURES_REQUEST:
1418 case OFPUTIL_OFPT_FEATURES_REPLY:
1419 ofp_print_switch_features(string, msg);
1422 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1425 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1426 case OFPUTIL_OFPT_SET_CONFIG:
1427 ofp_print_switch_config(string, msg);
1430 case OFPUTIL_OFPT_PACKET_IN:
1431 ofp_print_packet_in(string, msg, verbosity);
1434 case OFPUTIL_OFPT_FLOW_REMOVED:
1435 ofp_print_flow_removed(string, msg, verbosity);
1438 case OFPUTIL_OFPT_PORT_STATUS:
1439 ofp_print_port_status(string, msg);
1442 case OFPUTIL_OFPT_PACKET_OUT:
1443 ofp_print_packet_out(string, msg, verbosity);
1446 case OFPUTIL_OFPT_FLOW_MOD:
1447 ofp_print_flow_mod(string, msg, code, verbosity);
1450 case OFPUTIL_OFPT_PORT_MOD:
1451 ofp_print_port_mod(string, msg);
1454 case OFPUTIL_OFPT_BARRIER_REQUEST:
1455 case OFPUTIL_OFPT_BARRIER_REPLY:
1458 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1459 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1463 case OFPUTIL_OFPST_DESC_REQUEST:
1464 ofp_print_stats_request(string, oh);
1467 case OFPUTIL_OFPST_FLOW_REQUEST:
1468 ofp_print_stats_request(string, oh);
1469 ofp_print_ofpst_flow_request(string, oh, verbosity);
1472 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1473 ofp_print_stats_request(string, oh);
1474 ofp_print_ofpst_aggregate_request(string, oh, verbosity);
1477 case OFPUTIL_OFPST_TABLE_REQUEST:
1478 ofp_print_stats_request(string, oh);
1481 case OFPUTIL_OFPST_PORT_REQUEST:
1482 ofp_print_stats_request(string, oh);
1483 ofp_print_ofpst_port_request(string, oh);
1486 case OFPUTIL_OFPST_QUEUE_REQUEST:
1487 ofp_print_stats_request(string, oh);
1488 ofp_print_ofpst_queue_request(string, oh);
1491 case OFPUTIL_OFPST_DESC_REPLY:
1492 ofp_print_stats_reply(string, oh);
1493 ofp_print_ofpst_desc_reply(string, oh);
1496 case OFPUTIL_OFPST_FLOW_REPLY:
1497 ofp_print_stats_reply(string, oh);
1498 ofp_print_ofpst_flow_reply(string, oh, verbosity);
1501 case OFPUTIL_OFPST_QUEUE_REPLY:
1502 ofp_print_stats_reply(string, oh);
1503 ofp_print_ofpst_queue_reply(string, oh, verbosity);
1506 case OFPUTIL_OFPST_PORT_REPLY:
1507 ofp_print_stats_reply(string, oh);
1508 ofp_print_ofpst_port_reply(string, oh, verbosity);
1511 case OFPUTIL_OFPST_TABLE_REPLY:
1512 ofp_print_stats_reply(string, oh);
1513 ofp_print_ofpst_table_reply(string, oh, verbosity);
1516 case OFPUTIL_OFPST_AGGREGATE_REPLY:
1517 ofp_print_stats_reply(string, oh);
1518 ofp_print_ofpst_aggregate_reply(string, oh);
1521 case OFPUTIL_NXT_STATUS_REQUEST:
1522 case OFPUTIL_NXT_STATUS_REPLY:
1526 case OFPUTIL_NXT_TUN_ID_FROM_COOKIE:
1527 ofp_print_nxt_tun_id_from_cookie(string, msg);
1530 case OFPUTIL_NXT_ROLE_REQUEST:
1531 case OFPUTIL_NXT_ROLE_REPLY:
1535 case OFPUTIL_NXT_SET_FLOW_FORMAT:
1536 ofp_print_nxt_set_flow_format(string, msg);
1539 case OFPUTIL_NXT_FLOW_MOD:
1540 ofp_print_flow_mod(string, msg, code, verbosity);
1543 case OFPUTIL_NXT_FLOW_REMOVED:
1544 case OFPUTIL_NXST_FLOW_REQUEST:
1545 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1546 case OFPUTIL_NXST_FLOW_REPLY:
1547 case OFPUTIL_NXST_AGGREGATE_REPLY:
1553 /* Composes and returns a string representing the OpenFlow packet of 'len'
1554 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1555 * verbosity and higher numbers increase verbosity. The caller is responsible
1556 * for freeing the string. */
1558 ofp_to_string(const void *oh_, size_t len, int verbosity)
1560 struct ds string = DS_EMPTY_INITIALIZER;
1561 const struct ofp_header *oh = oh_;
1563 if (len < sizeof(struct ofp_header)) {
1564 ds_put_cstr(&string, "OpenFlow packet too short:\n");
1565 } else if (oh->version != OFP_VERSION) {
1566 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n",
1568 } else if (ntohs(oh->length) > len) {
1569 ds_put_format(&string,
1570 "(***truncated to %zu bytes from %"PRIu16"***)",
1571 len, ntohs(oh->length));
1572 } else if (ntohs(oh->length) < len) {
1573 ds_put_format(&string,
1574 "(***only uses %"PRIu16" bytes out of %zu***)\n",
1575 ntohs(oh->length), len);
1577 const struct ofputil_msg_type *type;
1580 error = ofputil_decode_msg_type(oh, &type);
1582 ofp_to_string__(oh, type, &string, verbosity);
1583 if (verbosity >= 5) {
1584 if (ds_last(&string) != '\n') {
1585 ds_put_char(&string, '\n');
1587 ds_put_hex_dump(&string, oh, len, 0, true);
1589 if (ds_last(&string) != '\n') {
1590 ds_put_char(&string, '\n');
1592 return ds_steal_cstr(&string);
1595 ofp_print_error(&string, error);
1597 ds_put_hex_dump(&string, oh, len, 0, true);
1598 return ds_steal_cstr(&string);
1601 /* Returns the name for the specified OpenFlow message type as a string,
1602 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1603 * hex number, e.g. "0x55".
1605 * The caller must free the returned string when it is no longer needed. */
1607 ofp_message_type_to_string(uint8_t type)
1618 case OFPT_ECHO_REQUEST:
1619 name = "ECHO_REQUEST";
1621 case OFPT_ECHO_REPLY:
1622 name = "ECHO_REPLY";
1627 case OFPT_FEATURES_REQUEST:
1628 name = "FEATURES_REQUEST";
1630 case OFPT_FEATURES_REPLY:
1631 name = "FEATURES_REPLY";
1633 case OFPT_GET_CONFIG_REQUEST:
1634 name = "GET_CONFIG_REQUEST";
1636 case OFPT_GET_CONFIG_REPLY:
1637 name = "GET_CONFIG_REPLY";
1639 case OFPT_SET_CONFIG:
1640 name = "SET_CONFIG";
1642 case OFPT_PACKET_IN:
1645 case OFPT_FLOW_REMOVED:
1646 name = "FLOW_REMOVED";
1648 case OFPT_PORT_STATUS:
1649 name = "PORT_STATUS";
1651 case OFPT_PACKET_OUT:
1652 name = "PACKET_OUT";
1660 case OFPT_STATS_REQUEST:
1661 name = "STATS_REQUEST";
1663 case OFPT_STATS_REPLY:
1664 name = "STATS_REPLY";
1666 case OFPT_BARRIER_REQUEST:
1667 name = "BARRIER_REQUEST";
1669 case OFPT_BARRIER_REPLY:
1670 name = "BARRIER_REPLY";
1672 case OFPT_QUEUE_GET_CONFIG_REQUEST:
1673 name = "QUEUE_GET_CONFIG_REQUEST";
1675 case OFPT_QUEUE_GET_CONFIG_REPLY:
1676 name = "QUEUE_GET_CONFIG_REPLY";
1683 return name ? xasprintf("OFPT_%s", name) : xasprintf("0x%02"PRIx8, type);
1687 print_and_free(FILE *stream, char *string)
1689 fputs(string, stream);
1693 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1694 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1695 * numbers increase verbosity. */
1697 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1699 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1702 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1703 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1704 * the Ethernet frame (of which 'len' bytes were captured).
1706 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1708 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1710 print_and_free(stream, ofp_packet_to_string(data, len, total_len));