2 * Copyright (c) 2008, 2009, 2010, 2011 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 "byte-order.h"
32 #include "dynamic-string.h"
34 #include "multipath.h"
38 #include "openflow/openflow.h"
39 #include "openflow/nicira-ext.h"
42 #include "type-props.h"
43 #include "unaligned.h"
46 static void ofp_print_port_name(struct ds *string, uint16_t port);
47 static void ofp_print_queue_name(struct ds *string, uint32_t port);
48 static void ofp_print_error(struct ds *, int error);
51 /* Returns a string that represents the contents of the Ethernet frame in the
52 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
53 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
54 * bytes were captured).
56 * The caller must free the returned string.
58 * This starts and kills a tcpdump subprocess so it's quite expensive. */
60 ofp_packet_to_string(const void *data, size_t len, size_t total_len OVS_UNUSED)
62 struct ds ds = DS_EMPTY_INITIALIZER;
71 ofpbuf_use_const(&buf, data, len);
75 ovs_error(errno, "tmpfile");
76 return xstrdup("<error>");
78 pcap_write_header(pcap);
79 pcap_write(pcap, &buf);
82 ovs_error(errno, "error writing temporary file");
86 snprintf(command, sizeof command, "/usr/sbin/tcpdump -t -e -n -r /dev/fd/%d 2>/dev/null",
88 tcpdump = popen(command, "r");
91 ovs_error(errno, "exec(\"%s\")", command);
92 return xstrdup("<error>");
95 while ((c = getc(tcpdump)) != EOF) {
99 status = pclose(tcpdump);
100 if (WIFEXITED(status)) {
101 if (WEXITSTATUS(status))
102 ovs_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
103 } else if (WIFSIGNALED(status)) {
104 ovs_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
110 ofp_print_packet_in(struct ds *string, const struct ofp_packet_in *op,
113 size_t len = ntohs(op->header.length);
116 ds_put_format(string, " total_len=%"PRIu16" in_port=",
117 ntohs(op->total_len));
118 ofp_print_port_name(string, ntohs(op->in_port));
120 if (op->reason == OFPR_ACTION)
121 ds_put_cstr(string, " (via action)");
122 else if (op->reason != OFPR_NO_MATCH)
123 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
125 data_len = len - offsetof(struct ofp_packet_in, data);
126 ds_put_format(string, " data_len=%zu", data_len);
127 if (op->buffer_id == htonl(UINT32_MAX)) {
128 ds_put_format(string, " (unbuffered)");
129 if (ntohs(op->total_len) != data_len)
130 ds_put_format(string, " (***total_len != data_len***)");
132 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
133 if (ntohs(op->total_len) < data_len)
134 ds_put_format(string, " (***total_len < data_len***)");
136 ds_put_char(string, '\n');
140 struct ofpbuf packet;
142 ofpbuf_use_const(&packet, op->data, data_len);
143 flow_extract(&packet, 0, ntohs(op->in_port), &flow);
144 flow_format(string, &flow);
145 ds_put_char(string, '\n');
148 char *packet = ofp_packet_to_string(op->data, data_len,
149 ntohs(op->total_len));
150 ds_put_cstr(string, packet);
155 static void ofp_print_port_name(struct ds *string, uint16_t port)
174 case OFPP_CONTROLLER:
184 ds_put_format(string, "%"PRIu16, port);
187 ds_put_cstr(string, name);
192 print_note(struct ds *string, const struct nx_action_note *nan)
197 ds_put_cstr(string, "note:");
198 len = ntohs(nan->len) - offsetof(struct nx_action_note, note);
199 for (i = 0; i < len; i++) {
201 ds_put_char(string, '.');
203 ds_put_format(string, "%02"PRIx8, nan->note[i]);
208 ofp_print_action(struct ds *s, const union ofp_action *a,
209 enum ofputil_action_code code)
211 const struct ofp_action_enqueue *oae;
212 const struct ofp_action_dl_addr *oada;
213 const struct nx_action_set_tunnel64 *nast64;
214 const struct nx_action_set_tunnel *nast;
215 const struct nx_action_set_queue *nasq;
216 const struct nx_action_resubmit *nar;
217 const struct nx_action_reg_move *move;
218 const struct nx_action_reg_load *load;
219 const struct nx_action_multipath *nam;
220 const struct nx_action_autopath *naa;
224 case OFPUTIL_OFPAT_OUTPUT:
225 port = ntohs(a->output.port);
226 if (port < OFPP_MAX) {
227 ds_put_format(s, "output:%"PRIu16, port);
229 ofp_print_port_name(s, port);
230 if (port == OFPP_CONTROLLER) {
231 if (a->output.max_len != htons(0)) {
232 ds_put_format(s, ":%"PRIu16, ntohs(a->output.max_len));
234 ds_put_cstr(s, ":all");
240 case OFPUTIL_OFPAT_ENQUEUE:
241 oae = (const struct ofp_action_enqueue *) a;
242 ds_put_format(s, "enqueue:");
243 ofp_print_port_name(s, ntohs(oae->port));
244 ds_put_format(s, "q%"PRIu32, ntohl(oae->queue_id));
247 case OFPUTIL_OFPAT_SET_VLAN_VID:
248 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
249 ntohs(a->vlan_vid.vlan_vid));
252 case OFPUTIL_OFPAT_SET_VLAN_PCP:
253 ds_put_format(s, "mod_vlan_pcp:%"PRIu8, a->vlan_pcp.vlan_pcp);
256 case OFPUTIL_OFPAT_STRIP_VLAN:
257 ds_put_cstr(s, "strip_vlan");
260 case OFPUTIL_OFPAT_SET_DL_SRC:
261 oada = (const struct ofp_action_dl_addr *) a;
262 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
263 ETH_ADDR_ARGS(oada->dl_addr));
266 case OFPUTIL_OFPAT_SET_DL_DST:
267 oada = (const struct ofp_action_dl_addr *) a;
268 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
269 ETH_ADDR_ARGS(oada->dl_addr));
272 case OFPUTIL_OFPAT_SET_NW_SRC:
273 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(&a->nw_addr.nw_addr));
276 case OFPUTIL_OFPAT_SET_NW_DST:
277 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(&a->nw_addr.nw_addr));
280 case OFPUTIL_OFPAT_SET_NW_TOS:
281 ds_put_format(s, "mod_nw_tos:%d", a->nw_tos.nw_tos);
284 case OFPUTIL_OFPAT_SET_TP_SRC:
285 ds_put_format(s, "mod_tp_src:%d", ntohs(a->tp_port.tp_port));
288 case OFPUTIL_OFPAT_SET_TP_DST:
289 ds_put_format(s, "mod_tp_dst:%d", ntohs(a->tp_port.tp_port));
292 case OFPUTIL_NXAST_RESUBMIT:
293 nar = (struct nx_action_resubmit *)a;
294 ds_put_format(s, "resubmit:");
295 ofp_print_port_name(s, ntohs(nar->in_port));
298 case OFPUTIL_NXAST_SET_TUNNEL:
299 nast = (struct nx_action_set_tunnel *)a;
300 ds_put_format(s, "set_tunnel:%#"PRIx32, ntohl(nast->tun_id));
303 case OFPUTIL_NXAST_SET_QUEUE:
304 nasq = (struct nx_action_set_queue *)a;
305 ds_put_format(s, "set_queue:%u", ntohl(nasq->queue_id));
308 case OFPUTIL_NXAST_POP_QUEUE:
309 ds_put_cstr(s, "pop_queue");
312 case OFPUTIL_NXAST_NOTE:
313 print_note(s, (const struct nx_action_note *) a);
316 case OFPUTIL_NXAST_REG_MOVE:
317 move = (const struct nx_action_reg_move *) a;
318 nxm_format_reg_move(move, s);
321 case OFPUTIL_NXAST_REG_LOAD:
322 load = (const struct nx_action_reg_load *) a;
323 nxm_format_reg_load(load, s);
326 case OFPUTIL_NXAST_SET_TUNNEL64:
327 nast64 = (const struct nx_action_set_tunnel64 *) a;
328 ds_put_format(s, "set_tunnel64:%#"PRIx64,
329 ntohll(nast64->tun_id));
332 case OFPUTIL_NXAST_MULTIPATH:
333 nam = (const struct nx_action_multipath *) a;
334 multipath_format(nam, s);
337 case OFPUTIL_NXAST_AUTOPATH:
338 naa = (const struct nx_action_autopath *)a;
339 ds_put_format(s, "autopath(%u,", ntohl(naa->id));
340 nxm_format_field_bits(s, ntohl(naa->dst),
341 nxm_decode_ofs(naa->ofs_nbits),
342 nxm_decode_n_bits(naa->ofs_nbits));
346 case OFPUTIL_NXAST_BUNDLE:
347 case OFPUTIL_NXAST_BUNDLE_LOAD:
348 bundle_format((const struct nx_action_bundle *) a, s);
357 ofp_print_actions(struct ds *string, const union ofp_action *actions,
360 const union ofp_action *a;
363 ds_put_cstr(string, "actions=");
365 ds_put_cstr(string, "drop");
368 OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
369 int code = ofputil_decode_action(a);
372 ds_put_cstr(string, ",");
374 ofp_print_action(string, a, code);
376 ofp_print_error(string, -code);
380 ds_put_format(string, " ***%zu leftover bytes following actions",
386 ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
389 size_t len = ntohs(opo->header.length);
390 size_t actions_len = ntohs(opo->actions_len);
392 ds_put_cstr(string, " in_port=");
393 ofp_print_port_name(string, ntohs(opo->in_port));
395 ds_put_format(string, " actions_len=%zu ", actions_len);
396 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
397 ds_put_format(string, "***packet too short for action length***\n");
400 if (actions_len % sizeof(union ofp_action)) {
401 ds_put_format(string, "***action length not a multiple of %zu***\n",
402 sizeof(union ofp_action));
404 ofp_print_actions(string, (const union ofp_action *) opo->actions,
405 actions_len / sizeof(union ofp_action));
407 if (ntohl(opo->buffer_id) == UINT32_MAX) {
408 int data_len = len - sizeof *opo - actions_len;
409 ds_put_format(string, " data_len=%d", data_len);
410 if (verbosity > 0 && len > sizeof *opo) {
411 char *packet = ofp_packet_to_string(
412 (uint8_t *)opo->actions + actions_len, data_len, data_len);
413 ds_put_char(string, '\n');
414 ds_put_cstr(string, packet);
418 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
420 ds_put_char(string, '\n');
423 /* qsort comparison function. */
425 compare_ports(const void *a_, const void *b_)
427 const struct ofp_phy_port *a = a_;
428 const struct ofp_phy_port *b = b_;
429 uint16_t ap = ntohs(a->port_no);
430 uint16_t bp = ntohs(b->port_no);
432 return ap < bp ? -1 : ap > bp;
435 static void ofp_print_port_features(struct ds *string, uint32_t features)
438 ds_put_cstr(string, "Unsupported\n");
441 if (features & OFPPF_10MB_HD) {
442 ds_put_cstr(string, "10MB-HD ");
444 if (features & OFPPF_10MB_FD) {
445 ds_put_cstr(string, "10MB-FD ");
447 if (features & OFPPF_100MB_HD) {
448 ds_put_cstr(string, "100MB-HD ");
450 if (features & OFPPF_100MB_FD) {
451 ds_put_cstr(string, "100MB-FD ");
453 if (features & OFPPF_1GB_HD) {
454 ds_put_cstr(string, "1GB-HD ");
456 if (features & OFPPF_1GB_FD) {
457 ds_put_cstr(string, "1GB-FD ");
459 if (features & OFPPF_10GB_FD) {
460 ds_put_cstr(string, "10GB-FD ");
462 if (features & OFPPF_COPPER) {
463 ds_put_cstr(string, "COPPER ");
465 if (features & OFPPF_FIBER) {
466 ds_put_cstr(string, "FIBER ");
468 if (features & OFPPF_AUTONEG) {
469 ds_put_cstr(string, "AUTO_NEG ");
471 if (features & OFPPF_PAUSE) {
472 ds_put_cstr(string, "AUTO_PAUSE ");
474 if (features & OFPPF_PAUSE_ASYM) {
475 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
477 ds_put_char(string, '\n');
481 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
483 char name[OFP_MAX_PORT_NAME_LEN];
486 memcpy(name, port->name, sizeof name);
487 for (j = 0; j < sizeof name - 1; j++) {
488 if (!isprint((unsigned char) name[j])) {
494 ds_put_char(string, ' ');
495 ofp_print_port_name(string, ntohs(port->port_no));
496 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
497 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
500 ds_put_format(string, " current: ");
501 ofp_print_port_features(string, ntohl(port->curr));
503 if (port->advertised) {
504 ds_put_format(string, " advertised: ");
505 ofp_print_port_features(string, ntohl(port->advertised));
507 if (port->supported) {
508 ds_put_format(string, " supported: ");
509 ofp_print_port_features(string, ntohl(port->supported));
512 ds_put_format(string, " peer: ");
513 ofp_print_port_features(string, ntohl(port->peer));
518 ofp_print_switch_features(struct ds *string,
519 const struct ofp_switch_features *osf)
521 size_t len = ntohs(osf->header.length);
522 struct ofp_phy_port *port_list;
526 ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
527 osf->header.version, ntohll(osf->datapath_id));
528 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
529 ntohl(osf->n_buffers));
530 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
531 ntohl(osf->capabilities), ntohl(osf->actions));
533 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
535 port_list = xmemdup(osf->ports, len - sizeof *osf);
536 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
537 for (i = 0; i < n_ports; i++) {
538 ofp_print_phy_port(string, &port_list[i]);
544 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
548 flags = ntohs(osc->flags);
550 ds_put_cstr(string, " frags=");
551 switch (flags & OFPC_FRAG_MASK) {
552 case OFPC_FRAG_NORMAL:
553 ds_put_cstr(string, "normal");
554 flags &= ~OFPC_FRAG_MASK;
557 ds_put_cstr(string, "drop");
558 flags &= ~OFPC_FRAG_MASK;
560 case OFPC_FRAG_REASM:
561 ds_put_cstr(string, "reassemble");
562 flags &= ~OFPC_FRAG_MASK;
566 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
569 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
572 static void print_wild(struct ds *string, const char *leader, int is_wild,
573 int verbosity, const char *format, ...)
574 __attribute__((format(printf, 5, 6)));
576 static void print_wild(struct ds *string, const char *leader, int is_wild,
577 int verbosity, const char *format, ...)
579 if (is_wild && verbosity < 2) {
582 ds_put_cstr(string, leader);
586 va_start(args, format);
587 ds_put_format_valist(string, format, args);
590 ds_put_char(string, '*');
592 ds_put_char(string, ',');
596 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
597 uint32_t wild_bits, int verbosity)
599 if (wild_bits >= 32 && verbosity < 2) {
602 ds_put_cstr(string, leader);
603 if (wild_bits < 32) {
604 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
606 ds_put_format(string, "/%d", 32 - wild_bits);
609 ds_put_char(string, '*');
611 ds_put_char(string, ',');
615 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
617 char *s = ofp_match_to_string(om, verbosity);
623 ofp_match_to_string(const struct ofp_match *om, int verbosity)
625 struct ds f = DS_EMPTY_INITIALIZER;
626 uint32_t w = ntohl(om->wildcards);
627 bool skip_type = false;
628 bool skip_proto = false;
630 if (!(w & OFPFW_DL_TYPE)) {
632 if (om->dl_type == htons(ETH_TYPE_IP)) {
633 if (!(w & OFPFW_NW_PROTO)) {
635 if (om->nw_proto == IPPROTO_ICMP) {
636 ds_put_cstr(&f, "icmp,");
637 } else if (om->nw_proto == IPPROTO_TCP) {
638 ds_put_cstr(&f, "tcp,");
639 } else if (om->nw_proto == IPPROTO_UDP) {
640 ds_put_cstr(&f, "udp,");
642 ds_put_cstr(&f, "ip,");
646 ds_put_cstr(&f, "ip,");
648 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
649 ds_put_cstr(&f, "arp,");
654 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
655 "%d", ntohs(om->in_port));
656 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
657 "%d", ntohs(om->dl_vlan));
658 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
659 "%d", om->dl_vlan_pcp);
660 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
661 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
662 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
663 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
665 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
666 "0x%04x", ntohs(om->dl_type));
668 print_ip_netmask(&f, "nw_src=", om->nw_src,
669 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
670 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
671 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
673 if (om->dl_type == htons(ETH_TYPE_ARP)) {
674 print_wild(&f, "arp_op=", w & OFPFW_NW_PROTO, verbosity,
677 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
681 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
683 if (om->nw_proto == IPPROTO_ICMP) {
684 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
685 "%d", ntohs(om->icmp_type));
686 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
687 "%d", ntohs(om->icmp_code));
689 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
690 "%d", ntohs(om->tp_src));
691 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
692 "%d", ntohs(om->tp_dst));
694 if (ds_last(&f) == ',') {
701 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
702 enum ofputil_msg_code code, int verbosity)
708 error = ofputil_decode_flow_mod(&fm, oh, true);
710 ofp_print_error(s, error);
715 switch (fm.command) {
717 ds_put_cstr(s, "ADD");
720 ds_put_cstr(s, "MOD");
722 case OFPFC_MODIFY_STRICT:
723 ds_put_cstr(s, "MOD_STRICT");
726 ds_put_cstr(s, "DEL");
728 case OFPFC_DELETE_STRICT:
729 ds_put_cstr(s, "DEL_STRICT");
732 ds_put_format(s, "cmd:%d", fm.command);
734 if (fm.table_id != 0) {
735 ds_put_format(s, " table:%d", fm.table_id);
739 if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
740 const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
741 ofp_print_match(s, &ofm->match, verbosity);
743 /* ofp_print_match() doesn't print priority. */
744 need_priority = true;
745 } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
746 const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
747 const void *nxm = nfm + 1;
750 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
751 ds_put_cstr(s, nxm_s);
754 /* nx_match_to_string() doesn't print priority. */
755 need_priority = true;
757 cls_rule_format(&fm.cr, s);
759 /* cls_rule_format() does print priority. */
760 need_priority = false;
763 if (ds_last(s) != ' ') {
766 if (fm.cookie != htonll(0)) {
767 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.cookie));
769 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
770 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
772 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
773 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
775 if (fm.cr.priority != OFP_DEFAULT_PRIORITY && need_priority) {
776 ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
778 if (fm.buffer_id != UINT32_MAX) {
779 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
782 ds_put_format(s, "flags:0x%"PRIx16" ", fm.flags);
785 ofp_print_actions(s, fm.actions, fm.n_actions);
789 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
791 ds_put_format(string, "%u", sec);
793 ds_put_format(string, ".%09u", nsec);
794 while (string->string[string->length - 1] == '0') {
798 ds_put_char(string, 's');
802 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
804 struct ofputil_flow_removed fr;
807 error = ofputil_decode_flow_removed(&fr, oh);
809 ofp_print_error(string, error);
813 ds_put_char(string, ' ');
814 cls_rule_format(&fr.rule, string);
816 ds_put_cstr(string, " reason=");
818 case OFPRR_IDLE_TIMEOUT:
819 ds_put_cstr(string, "idle");
821 case OFPRR_HARD_TIMEOUT:
822 ds_put_cstr(string, "hard");
825 ds_put_cstr(string, "delete");
828 ds_put_format(string, "**%"PRIu8"**", fr.reason);
832 if (fr.cookie != htonll(0)) {
833 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
835 ds_put_cstr(string, " duration");
836 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
837 ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
838 fr.idle_timeout, fr.packet_count, fr.byte_count);
842 ofp_print_port_mod(struct ds *string, const struct ofp_port_mod *opm)
844 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
845 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
846 ntohl(opm->config), ntohl(opm->mask));
847 ds_put_format(string, " advertise: ");
848 if (opm->advertise) {
849 ofp_print_port_features(string, ntohl(opm->advertise));
851 ds_put_format(string, "UNCHANGED\n");
856 ofp_print_error(struct ds *string, int error)
858 if (string->length) {
859 ds_put_char(string, ' ');
861 ds_put_cstr(string, "***decode error: ");
862 ofputil_format_error(string, error);
863 ds_put_cstr(string, "***\n");
867 ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
869 size_t len = ntohs(oem->header.length);
870 size_t payload_ofs, payload_len;
875 error = ofputil_decode_error_msg(&oem->header, &payload_ofs);
876 if (!is_ofp_error(error)) {
877 ofp_print_error(string, error);
878 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
882 ds_put_char(string, ' ');
883 ofputil_format_error(string, error);
884 ds_put_char(string, '\n');
886 payload = (const uint8_t *) oem + payload_ofs;
887 payload_len = len - payload_ofs;
888 switch (get_ofp_err_type(error)) {
889 case OFPET_HELLO_FAILED:
890 ds_put_printable(string, payload, payload_len);
893 case OFPET_BAD_REQUEST:
894 s = ofp_to_string(payload, payload_len, 1);
895 ds_put_cstr(string, s);
900 ds_put_hex_dump(string, payload, payload_len, 0, true);
906 ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
908 if (ops->reason == OFPPR_ADD) {
909 ds_put_format(string, " ADD:");
910 } else if (ops->reason == OFPPR_DELETE) {
911 ds_put_format(string, " DEL:");
912 } else if (ops->reason == OFPPR_MODIFY) {
913 ds_put_format(string, " MOD:");
916 ofp_print_phy_port(string, &ops->desc);
920 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_desc_stats *ods)
922 ds_put_char(string, '\n');
923 ds_put_format(string, "Manufacturer: %.*s\n",
924 (int) sizeof ods->mfr_desc, ods->mfr_desc);
925 ds_put_format(string, "Hardware: %.*s\n",
926 (int) sizeof ods->hw_desc, ods->hw_desc);
927 ds_put_format(string, "Software: %.*s\n",
928 (int) sizeof ods->sw_desc, ods->sw_desc);
929 ds_put_format(string, "Serial Num: %.*s\n",
930 (int) sizeof ods->serial_num, ods->serial_num);
931 ds_put_format(string, "DP Description: %.*s\n",
932 (int) sizeof ods->dp_desc, ods->dp_desc);
936 ofp_print_flow_stats_request(struct ds *string,
937 const struct ofp_stats_msg *osm)
939 struct flow_stats_request fsr;
942 error = ofputil_decode_flow_stats_request(&fsr, &osm->header);
944 ofp_print_error(string, error);
948 if (fsr.table_id != 0xff) {
949 ds_put_format(string, " table=%"PRIu8, fsr.table_id);
952 if (fsr.out_port != OFPP_NONE) {
953 ds_put_cstr(string, " out_port=");
954 ofp_print_port_name(string, fsr.out_port);
957 /* A flow stats request doesn't include a priority, but cls_rule_format()
958 * will print one unless it is OFP_DEFAULT_PRIORITY. */
959 fsr.match.priority = OFP_DEFAULT_PRIORITY;
961 ds_put_char(string, ' ');
962 cls_rule_format(&fsr.match, string);
966 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
970 ofpbuf_use_const(&b, oh, ntohs(oh->length));
972 struct ofputil_flow_stats fs;
975 retval = ofputil_decode_flow_stats_reply(&fs, &b);
978 ds_put_cstr(string, " ***parse error***");
983 ds_put_char(string, '\n');
985 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
987 ofp_print_duration(string, fs.duration_sec, fs.duration_nsec);
988 ds_put_format(string, ", table=%"PRIu8", ", fs.table_id);
989 ds_put_format(string, "n_packets=%"PRIu64", ", fs.packet_count);
990 ds_put_format(string, "n_bytes=%"PRIu64", ", fs.byte_count);
991 if (fs.idle_timeout != OFP_FLOW_PERMANENT) {
992 ds_put_format(string, "idle_timeout=%"PRIu16",", fs.idle_timeout);
994 if (fs.hard_timeout != OFP_FLOW_PERMANENT) {
995 ds_put_format(string, "hard_timeout=%"PRIu16",", fs.hard_timeout);
998 cls_rule_format(&fs.rule, string);
999 ds_put_char(string, ' ');
1000 ofp_print_actions(string, fs.actions, fs.n_actions);
1005 ofp_print_ofpst_aggregate_reply(struct ds *string,
1006 const struct ofp_aggregate_stats_reply *asr)
1008 ds_put_format(string, " packet_count=%"PRIu64,
1009 ntohll(get_32aligned_be64(&asr->packet_count)));
1010 ds_put_format(string, " byte_count=%"PRIu64,
1011 ntohll(get_32aligned_be64(&asr->byte_count)));
1012 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1016 ofp_print_nxst_aggregate_reply(struct ds *string,
1017 const struct nx_aggregate_stats_reply *nasr)
1019 ds_put_format(string, " packet_count=%"PRIu64, ntohll(nasr->packet_count));
1020 ds_put_format(string, " byte_count=%"PRIu64, ntohll(nasr->byte_count));
1021 ds_put_format(string, " flow_count=%"PRIu32, ntohl(nasr->flow_count));
1024 static void print_port_stat(struct ds *string, const char *leader,
1025 const ovs_32aligned_be64 *statp, int more)
1027 uint64_t stat = ntohll(get_32aligned_be64(statp));
1029 ds_put_cstr(string, leader);
1030 if (stat != UINT64_MAX) {
1031 ds_put_format(string, "%"PRIu64, stat);
1033 ds_put_char(string, '?');
1036 ds_put_cstr(string, ", ");
1038 ds_put_cstr(string, "\n");
1043 ofp_print_ofpst_port_request(struct ds *string,
1044 const struct ofp_port_stats_request *psr)
1046 ds_put_format(string, " port_no=%"PRIu16, ntohs(psr->port_no));
1050 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1053 const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1054 size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
1055 ds_put_format(string, " %zu ports\n", n);
1056 if (verbosity < 1) {
1061 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1063 ds_put_cstr(string, "rx ");
1064 print_port_stat(string, "pkts=", &ps->rx_packets, 1);
1065 print_port_stat(string, "bytes=", &ps->rx_bytes, 1);
1066 print_port_stat(string, "drop=", &ps->rx_dropped, 1);
1067 print_port_stat(string, "errs=", &ps->rx_errors, 1);
1068 print_port_stat(string, "frame=", &ps->rx_frame_err, 1);
1069 print_port_stat(string, "over=", &ps->rx_over_err, 1);
1070 print_port_stat(string, "crc=", &ps->rx_crc_err, 0);
1072 ds_put_cstr(string, " tx ");
1073 print_port_stat(string, "pkts=", &ps->tx_packets, 1);
1074 print_port_stat(string, "bytes=", &ps->tx_bytes, 1);
1075 print_port_stat(string, "drop=", &ps->tx_dropped, 1);
1076 print_port_stat(string, "errs=", &ps->tx_errors, 1);
1077 print_port_stat(string, "coll=", &ps->collisions, 0);
1082 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1085 const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1086 size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
1087 ds_put_format(string, " %zu tables\n", n);
1088 if (verbosity < 1) {
1093 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1094 ovs_strlcpy(name, ts->name, sizeof name);
1096 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1097 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1098 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1099 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1100 ds_put_cstr(string, " ");
1101 ds_put_format(string, "lookup=%"PRIu64", ",
1102 ntohll(get_32aligned_be64(&ts->lookup_count)));
1103 ds_put_format(string, "matched=%"PRIu64"\n",
1104 ntohll(get_32aligned_be64(&ts->matched_count)));
1109 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1111 if (queue_id == OFPQ_ALL) {
1112 ds_put_cstr(string, "ALL");
1114 ds_put_format(string, "%"PRIu32, queue_id);
1119 ofp_print_ofpst_queue_request(struct ds *string,
1120 const struct ofp_queue_stats_request *qsr)
1122 ds_put_cstr(string, "port=");
1123 ofp_print_port_name(string, ntohs(qsr->port_no));
1125 ds_put_cstr(string, " queue=");
1126 ofp_print_queue_name(string, ntohl(qsr->queue_id));
1130 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1133 const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1134 size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
1135 ds_put_format(string, " %zu queues\n", n);
1136 if (verbosity < 1) {
1141 ds_put_cstr(string, " port ");
1142 ofp_print_port_name(string, ntohs(qs->port_no));
1143 ds_put_cstr(string, " queue ");
1144 ofp_print_queue_name(string, ntohl(qs->queue_id));
1145 ds_put_cstr(string, ": ");
1147 print_port_stat(string, "bytes=", &qs->tx_bytes, 1);
1148 print_port_stat(string, "pkts=", &qs->tx_packets, 1);
1149 print_port_stat(string, "errors=", &qs->tx_errors, 0);
1154 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1156 const struct ofp_stats_msg *srq = (const struct ofp_stats_msg *) oh;
1159 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1165 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1167 const struct ofp_stats_msg *srp = (const struct ofp_stats_msg *) oh;
1170 uint16_t flags = ntohs(srp->flags);
1172 ds_put_cstr(string, " flags=");
1173 if (flags & OFPSF_REPLY_MORE) {
1174 ds_put_cstr(string, "[more]");
1175 flags &= ~OFPSF_REPLY_MORE;
1178 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1185 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1187 size_t len = ntohs(oh->length);
1189 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1190 if (verbosity > 1) {
1191 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1196 ofp_print_nxt_role_message(struct ds *string,
1197 const struct nx_role_request *nrr)
1199 unsigned int role = ntohl(nrr->role);
1201 ds_put_cstr(string, " role=");
1202 if (role == NX_ROLE_OTHER) {
1203 ds_put_cstr(string, "other");
1204 } else if (role == NX_ROLE_MASTER) {
1205 ds_put_cstr(string, "master");
1206 } else if (role == NX_ROLE_SLAVE) {
1207 ds_put_cstr(string, "slave");
1209 ds_put_format(string, "%u", role);
1214 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1215 const struct nxt_flow_mod_table_id *nfmti)
1217 ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1221 ofp_print_nxt_set_flow_format(struct ds *string,
1222 const struct nxt_set_flow_format *nsff)
1224 uint32_t format = ntohl(nsff->format);
1226 ds_put_cstr(string, " format=");
1227 if (ofputil_flow_format_is_valid(format)) {
1228 ds_put_cstr(string, ofputil_flow_format_to_string(format));
1230 ds_put_format(string, "%"PRIu32, format);
1235 ofp_to_string__(const struct ofp_header *oh,
1236 const struct ofputil_msg_type *type, struct ds *string,
1239 enum ofputil_msg_code code;
1240 const void *msg = oh;
1242 ds_put_format(string, "%s (xid=0x%"PRIx32"):",
1243 ofputil_msg_type_name(type), ntohl(oh->xid));
1245 code = ofputil_msg_type_code(type);
1247 case OFPUTIL_MSG_INVALID:
1250 case OFPUTIL_OFPT_HELLO:
1251 ds_put_char(string, '\n');
1252 ds_put_hex_dump(string, oh + 1, ntohs(oh->length) - sizeof *oh,
1256 case OFPUTIL_OFPT_ERROR:
1257 ofp_print_error_msg(string, msg);
1260 case OFPUTIL_OFPT_ECHO_REQUEST:
1261 case OFPUTIL_OFPT_ECHO_REPLY:
1262 ofp_print_echo(string, oh, verbosity);
1265 case OFPUTIL_OFPT_FEATURES_REQUEST:
1268 case OFPUTIL_OFPT_FEATURES_REPLY:
1269 ofp_print_switch_features(string, msg);
1272 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1275 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1276 case OFPUTIL_OFPT_SET_CONFIG:
1277 ofp_print_switch_config(string, msg);
1280 case OFPUTIL_OFPT_PACKET_IN:
1281 ofp_print_packet_in(string, msg, verbosity);
1284 case OFPUTIL_OFPT_FLOW_REMOVED:
1285 case OFPUTIL_NXT_FLOW_REMOVED:
1286 ofp_print_flow_removed(string, msg);
1289 case OFPUTIL_OFPT_PORT_STATUS:
1290 ofp_print_port_status(string, msg);
1293 case OFPUTIL_OFPT_PACKET_OUT:
1294 ofp_print_packet_out(string, msg, verbosity);
1297 case OFPUTIL_OFPT_FLOW_MOD:
1298 ofp_print_flow_mod(string, msg, code, verbosity);
1301 case OFPUTIL_OFPT_PORT_MOD:
1302 ofp_print_port_mod(string, msg);
1305 case OFPUTIL_OFPT_BARRIER_REQUEST:
1306 case OFPUTIL_OFPT_BARRIER_REPLY:
1309 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1310 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1314 case OFPUTIL_OFPST_DESC_REQUEST:
1315 ofp_print_stats_request(string, oh);
1318 case OFPUTIL_OFPST_FLOW_REQUEST:
1319 case OFPUTIL_NXST_FLOW_REQUEST:
1320 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1321 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1322 ofp_print_stats_request(string, oh);
1323 ofp_print_flow_stats_request(string, msg);
1326 case OFPUTIL_OFPST_TABLE_REQUEST:
1327 ofp_print_stats_request(string, oh);
1330 case OFPUTIL_OFPST_PORT_REQUEST:
1331 ofp_print_stats_request(string, oh);
1332 ofp_print_ofpst_port_request(string, msg);
1335 case OFPUTIL_OFPST_QUEUE_REQUEST:
1336 ofp_print_stats_request(string, oh);
1337 ofp_print_ofpst_queue_request(string, msg);
1340 case OFPUTIL_OFPST_DESC_REPLY:
1341 ofp_print_stats_reply(string, oh);
1342 ofp_print_ofpst_desc_reply(string, msg);
1345 case OFPUTIL_OFPST_FLOW_REPLY:
1346 case OFPUTIL_NXST_FLOW_REPLY:
1347 ofp_print_stats_reply(string, oh);
1348 ofp_print_flow_stats_reply(string, oh);
1351 case OFPUTIL_OFPST_QUEUE_REPLY:
1352 ofp_print_stats_reply(string, oh);
1353 ofp_print_ofpst_queue_reply(string, oh, verbosity);
1356 case OFPUTIL_OFPST_PORT_REPLY:
1357 ofp_print_stats_reply(string, oh);
1358 ofp_print_ofpst_port_reply(string, oh, verbosity);
1361 case OFPUTIL_OFPST_TABLE_REPLY:
1362 ofp_print_stats_reply(string, oh);
1363 ofp_print_ofpst_table_reply(string, oh, verbosity);
1366 case OFPUTIL_OFPST_AGGREGATE_REPLY:
1367 ofp_print_stats_reply(string, oh);
1368 ofp_print_ofpst_aggregate_reply(string, msg);
1371 case OFPUTIL_NXT_ROLE_REQUEST:
1372 case OFPUTIL_NXT_ROLE_REPLY:
1373 ofp_print_nxt_role_message(string, msg);
1376 case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
1377 ofp_print_nxt_flow_mod_table_id(string, msg);
1380 case OFPUTIL_NXT_SET_FLOW_FORMAT:
1381 ofp_print_nxt_set_flow_format(string, msg);
1384 case OFPUTIL_NXT_FLOW_MOD:
1385 ofp_print_flow_mod(string, msg, code, verbosity);
1388 case OFPUTIL_NXST_AGGREGATE_REPLY:
1389 ofp_print_stats_reply(string, oh);
1390 ofp_print_nxst_aggregate_reply(string, msg);
1395 /* Composes and returns a string representing the OpenFlow packet of 'len'
1396 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1397 * verbosity and higher numbers increase verbosity. The caller is responsible
1398 * for freeing the string. */
1400 ofp_to_string(const void *oh_, size_t len, int verbosity)
1402 struct ds string = DS_EMPTY_INITIALIZER;
1403 const struct ofp_header *oh = oh_;
1406 ds_put_cstr(&string, "OpenFlow message is empty\n");
1407 } else if (len < sizeof(struct ofp_header)) {
1408 ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
1410 } else if (oh->version != OFP_VERSION) {
1411 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n",
1413 } else if (ntohs(oh->length) > len) {
1414 ds_put_format(&string,
1415 "(***truncated to %zu bytes from %"PRIu16"***)\n",
1416 len, ntohs(oh->length));
1417 } else if (ntohs(oh->length) < len) {
1418 ds_put_format(&string,
1419 "(***only uses %"PRIu16" bytes out of %zu***)\n",
1420 ntohs(oh->length), len);
1422 const struct ofputil_msg_type *type;
1425 error = ofputil_decode_msg_type(oh, &type);
1427 ofp_to_string__(oh, type, &string, verbosity);
1428 if (verbosity >= 5) {
1429 if (ds_last(&string) != '\n') {
1430 ds_put_char(&string, '\n');
1432 ds_put_hex_dump(&string, oh, len, 0, true);
1434 if (ds_last(&string) != '\n') {
1435 ds_put_char(&string, '\n');
1437 return ds_steal_cstr(&string);
1440 ofp_print_error(&string, error);
1442 ds_put_hex_dump(&string, oh, len, 0, true);
1443 return ds_steal_cstr(&string);
1446 /* Returns the name for the specified OpenFlow message type as a string,
1447 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1448 * hex number, e.g. "0x55".
1450 * The caller must free the returned string when it is no longer needed. */
1452 ofp_message_type_to_string(uint8_t type)
1463 case OFPT_ECHO_REQUEST:
1464 name = "ECHO_REQUEST";
1466 case OFPT_ECHO_REPLY:
1467 name = "ECHO_REPLY";
1472 case OFPT_FEATURES_REQUEST:
1473 name = "FEATURES_REQUEST";
1475 case OFPT_FEATURES_REPLY:
1476 name = "FEATURES_REPLY";
1478 case OFPT_GET_CONFIG_REQUEST:
1479 name = "GET_CONFIG_REQUEST";
1481 case OFPT_GET_CONFIG_REPLY:
1482 name = "GET_CONFIG_REPLY";
1484 case OFPT_SET_CONFIG:
1485 name = "SET_CONFIG";
1487 case OFPT_PACKET_IN:
1490 case OFPT_FLOW_REMOVED:
1491 name = "FLOW_REMOVED";
1493 case OFPT_PORT_STATUS:
1494 name = "PORT_STATUS";
1496 case OFPT_PACKET_OUT:
1497 name = "PACKET_OUT";
1505 case OFPT_STATS_REQUEST:
1506 name = "STATS_REQUEST";
1508 case OFPT_STATS_REPLY:
1509 name = "STATS_REPLY";
1511 case OFPT_BARRIER_REQUEST:
1512 name = "BARRIER_REQUEST";
1514 case OFPT_BARRIER_REPLY:
1515 name = "BARRIER_REPLY";
1517 case OFPT_QUEUE_GET_CONFIG_REQUEST:
1518 name = "QUEUE_GET_CONFIG_REQUEST";
1520 case OFPT_QUEUE_GET_CONFIG_REPLY:
1521 name = "QUEUE_GET_CONFIG_REPLY";
1528 return name ? xasprintf("OFPT_%s", name) : xasprintf("0x%02"PRIx8, type);
1532 print_and_free(FILE *stream, char *string)
1534 fputs(string, stream);
1538 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1539 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1540 * numbers increase verbosity. */
1542 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1544 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1547 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1548 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1549 * the Ethernet frame (of which 'len' bytes were captured).
1551 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1553 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1555 print_and_free(stream, ofp_packet_to_string(data, len, total_len));