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>
29 #include "byte-order.h"
31 #include "dynamic-string.h"
33 #include "multipath.h"
37 #include "openflow/openflow.h"
38 #include "openflow/nicira-ext.h"
41 #include "type-props.h"
42 #include "unaligned.h"
45 static void ofp_print_port_name(struct ds *string, uint16_t port);
46 static void ofp_print_queue_name(struct ds *string, uint32_t port);
47 static void ofp_print_error(struct ds *, int error);
50 /* Returns a string that represents the contents of the Ethernet frame in the
51 * 'len' bytes starting at 'data' to 'stream' as output by tcpdump.
52 * 'total_len' specifies the full length of the Ethernet frame (of which 'len'
53 * bytes were captured).
55 * The caller must free the returned string.
57 * This starts and kills a tcpdump subprocess so it's quite expensive. */
59 ofp_packet_to_string(const void *data, size_t len, size_t total_len OVS_UNUSED)
61 struct ds ds = DS_EMPTY_INITIALIZER;
70 ofpbuf_use_const(&buf, data, len);
74 ovs_error(errno, "tmpfile");
75 return xstrdup("<error>");
77 pcap_write_header(pcap);
78 pcap_write(pcap, &buf);
81 ovs_error(errno, "error writing temporary file");
85 snprintf(command, sizeof command, "/usr/sbin/tcpdump -t -e -n -r /dev/fd/%d 2>/dev/null",
87 tcpdump = popen(command, "r");
90 ovs_error(errno, "exec(\"%s\")", command);
91 return xstrdup("<error>");
94 while ((c = getc(tcpdump)) != EOF) {
98 status = pclose(tcpdump);
99 if (WIFEXITED(status)) {
100 if (WEXITSTATUS(status))
101 ovs_error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
102 } else if (WIFSIGNALED(status)) {
103 ovs_error(0, "tcpdump exited with signal %d", WTERMSIG(status));
109 ofp_print_packet_in(struct ds *string, const struct ofp_packet_in *op,
112 size_t len = ntohs(op->header.length);
115 ds_put_format(string, " total_len=%"PRIu16" in_port=",
116 ntohs(op->total_len));
117 ofp_print_port_name(string, ntohs(op->in_port));
119 if (op->reason == OFPR_ACTION)
120 ds_put_cstr(string, " (via action)");
121 else if (op->reason != OFPR_NO_MATCH)
122 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
124 data_len = len - offsetof(struct ofp_packet_in, data);
125 ds_put_format(string, " data_len=%zu", data_len);
126 if (op->buffer_id == htonl(UINT32_MAX)) {
127 ds_put_format(string, " (unbuffered)");
128 if (ntohs(op->total_len) != data_len)
129 ds_put_format(string, " (***total_len != data_len***)");
131 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(op->buffer_id));
132 if (ntohs(op->total_len) < data_len)
133 ds_put_format(string, " (***total_len < data_len***)");
135 ds_put_char(string, '\n');
139 struct ofpbuf packet;
141 ofpbuf_use_const(&packet, op->data, data_len);
142 flow_extract(&packet, 0, ntohs(op->in_port), &flow);
143 flow_format(string, &flow);
144 ds_put_char(string, '\n');
147 char *packet = ofp_packet_to_string(op->data, data_len,
148 ntohs(op->total_len));
149 ds_put_cstr(string, packet);
154 static void ofp_print_port_name(struct ds *string, uint16_t port)
173 case OFPP_CONTROLLER:
183 ds_put_format(string, "%"PRIu16, port);
186 ds_put_cstr(string, name);
191 print_note(struct ds *string, const struct nx_action_note *nan)
196 ds_put_cstr(string, "note:");
197 len = ntohs(nan->len) - offsetof(struct nx_action_note, note);
198 for (i = 0; i < len; i++) {
200 ds_put_char(string, '.');
202 ds_put_format(string, "%02"PRIx8, nan->note[i]);
207 ofp_print_action(struct ds *s, const union ofp_action *a,
208 enum ofputil_action_code code)
210 const struct ofp_action_enqueue *oae;
211 const struct ofp_action_dl_addr *oada;
212 const struct nx_action_set_tunnel64 *nast64;
213 const struct nx_action_set_tunnel *nast;
214 const struct nx_action_set_queue *nasq;
215 const struct nx_action_resubmit *nar;
216 const struct nx_action_reg_move *move;
217 const struct nx_action_reg_load *load;
218 const struct nx_action_multipath *nam;
219 const struct nx_action_autopath *naa;
223 case OFPUTIL_OFPAT_OUTPUT:
224 port = ntohs(a->output.port);
225 if (port < OFPP_MAX) {
226 ds_put_format(s, "output:%"PRIu16, port);
228 ofp_print_port_name(s, port);
229 if (port == OFPP_CONTROLLER) {
230 if (a->output.max_len != htons(0)) {
231 ds_put_format(s, ":%"PRIu16, ntohs(a->output.max_len));
233 ds_put_cstr(s, ":all");
239 case OFPUTIL_OFPAT_ENQUEUE:
240 oae = (const struct ofp_action_enqueue *) a;
241 ds_put_format(s, "enqueue:");
242 ofp_print_port_name(s, ntohs(oae->port));
243 ds_put_format(s, "q%"PRIu32, ntohl(oae->queue_id));
246 case OFPUTIL_OFPAT_SET_VLAN_VID:
247 ds_put_format(s, "mod_vlan_vid:%"PRIu16,
248 ntohs(a->vlan_vid.vlan_vid));
251 case OFPUTIL_OFPAT_SET_VLAN_PCP:
252 ds_put_format(s, "mod_vlan_pcp:%"PRIu8, a->vlan_pcp.vlan_pcp);
255 case OFPUTIL_OFPAT_STRIP_VLAN:
256 ds_put_cstr(s, "strip_vlan");
259 case OFPUTIL_OFPAT_SET_DL_SRC:
260 oada = (const struct ofp_action_dl_addr *) a;
261 ds_put_format(s, "mod_dl_src:"ETH_ADDR_FMT,
262 ETH_ADDR_ARGS(oada->dl_addr));
265 case OFPUTIL_OFPAT_SET_DL_DST:
266 oada = (const struct ofp_action_dl_addr *) a;
267 ds_put_format(s, "mod_dl_dst:"ETH_ADDR_FMT,
268 ETH_ADDR_ARGS(oada->dl_addr));
271 case OFPUTIL_OFPAT_SET_NW_SRC:
272 ds_put_format(s, "mod_nw_src:"IP_FMT, IP_ARGS(&a->nw_addr.nw_addr));
275 case OFPUTIL_OFPAT_SET_NW_DST:
276 ds_put_format(s, "mod_nw_dst:"IP_FMT, IP_ARGS(&a->nw_addr.nw_addr));
279 case OFPUTIL_OFPAT_SET_NW_TOS:
280 ds_put_format(s, "mod_nw_tos:%d", a->nw_tos.nw_tos);
283 case OFPUTIL_OFPAT_SET_TP_SRC:
284 ds_put_format(s, "mod_tp_src:%d", ntohs(a->tp_port.tp_port));
287 case OFPUTIL_OFPAT_SET_TP_DST:
288 ds_put_format(s, "mod_tp_dst:%d", ntohs(a->tp_port.tp_port));
291 case OFPUTIL_NXAST_RESUBMIT:
292 nar = (struct nx_action_resubmit *)a;
293 ds_put_format(s, "resubmit:");
294 ofp_print_port_name(s, ntohs(nar->in_port));
297 case OFPUTIL_NXAST_SET_TUNNEL:
298 nast = (struct nx_action_set_tunnel *)a;
299 ds_put_format(s, "set_tunnel:%#"PRIx32, ntohl(nast->tun_id));
302 case OFPUTIL_NXAST_SET_QUEUE:
303 nasq = (struct nx_action_set_queue *)a;
304 ds_put_format(s, "set_queue:%u", ntohl(nasq->queue_id));
307 case OFPUTIL_NXAST_POP_QUEUE:
308 ds_put_cstr(s, "pop_queue");
311 case OFPUTIL_NXAST_NOTE:
312 print_note(s, (const struct nx_action_note *) a);
315 case OFPUTIL_NXAST_REG_MOVE:
316 move = (const struct nx_action_reg_move *) a;
317 nxm_format_reg_move(move, s);
320 case OFPUTIL_NXAST_REG_LOAD:
321 load = (const struct nx_action_reg_load *) a;
322 nxm_format_reg_load(load, s);
325 case OFPUTIL_NXAST_SET_TUNNEL64:
326 nast64 = (const struct nx_action_set_tunnel64 *) a;
327 ds_put_format(s, "set_tunnel64:%#"PRIx64,
328 ntohll(nast64->tun_id));
331 case OFPUTIL_NXAST_MULTIPATH:
332 nam = (const struct nx_action_multipath *) a;
333 multipath_format(nam, s);
336 case OFPUTIL_NXAST_AUTOPATH:
337 naa = (const struct nx_action_autopath *)a;
338 ds_put_format(s, "autopath(%u,", ntohl(naa->id));
339 nxm_format_field_bits(s, ntohl(naa->dst),
340 nxm_decode_ofs(naa->ofs_nbits),
341 nxm_decode_n_bits(naa->ofs_nbits));
351 ofp_print_actions(struct ds *string, const union ofp_action *actions,
354 const union ofp_action *a;
357 ds_put_cstr(string, "actions=");
359 ds_put_cstr(string, "drop");
362 OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
363 int code = ofputil_decode_action(a);
366 ds_put_cstr(string, ",");
368 ofp_print_action(string, a, code);
370 ofp_print_error(string, -code);
374 ds_put_format(string, " ***%zu leftover bytes following actions",
380 ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
383 size_t len = ntohs(opo->header.length);
384 size_t actions_len = ntohs(opo->actions_len);
386 ds_put_cstr(string, " in_port=");
387 ofp_print_port_name(string, ntohs(opo->in_port));
389 ds_put_format(string, " actions_len=%zu ", actions_len);
390 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
391 ds_put_format(string, "***packet too short for action length***\n");
394 if (actions_len % sizeof(union ofp_action)) {
395 ds_put_format(string, "***action length not a multiple of %zu***\n",
396 sizeof(union ofp_action));
398 ofp_print_actions(string, (const union ofp_action *) opo->actions,
399 actions_len / sizeof(union ofp_action));
401 if (ntohl(opo->buffer_id) == UINT32_MAX) {
402 int data_len = len - sizeof *opo - actions_len;
403 ds_put_format(string, " data_len=%d", data_len);
404 if (verbosity > 0 && len > sizeof *opo) {
405 char *packet = ofp_packet_to_string(
406 (uint8_t *)opo->actions + actions_len, data_len, data_len);
407 ds_put_char(string, '\n');
408 ds_put_cstr(string, packet);
412 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
414 ds_put_char(string, '\n');
417 /* qsort comparison function. */
419 compare_ports(const void *a_, const void *b_)
421 const struct ofp_phy_port *a = a_;
422 const struct ofp_phy_port *b = b_;
423 uint16_t ap = ntohs(a->port_no);
424 uint16_t bp = ntohs(b->port_no);
426 return ap < bp ? -1 : ap > bp;
429 static void ofp_print_port_features(struct ds *string, uint32_t features)
432 ds_put_cstr(string, "Unsupported\n");
435 if (features & OFPPF_10MB_HD) {
436 ds_put_cstr(string, "10MB-HD ");
438 if (features & OFPPF_10MB_FD) {
439 ds_put_cstr(string, "10MB-FD ");
441 if (features & OFPPF_100MB_HD) {
442 ds_put_cstr(string, "100MB-HD ");
444 if (features & OFPPF_100MB_FD) {
445 ds_put_cstr(string, "100MB-FD ");
447 if (features & OFPPF_1GB_HD) {
448 ds_put_cstr(string, "1GB-HD ");
450 if (features & OFPPF_1GB_FD) {
451 ds_put_cstr(string, "1GB-FD ");
453 if (features & OFPPF_10GB_FD) {
454 ds_put_cstr(string, "10GB-FD ");
456 if (features & OFPPF_COPPER) {
457 ds_put_cstr(string, "COPPER ");
459 if (features & OFPPF_FIBER) {
460 ds_put_cstr(string, "FIBER ");
462 if (features & OFPPF_AUTONEG) {
463 ds_put_cstr(string, "AUTO_NEG ");
465 if (features & OFPPF_PAUSE) {
466 ds_put_cstr(string, "AUTO_PAUSE ");
468 if (features & OFPPF_PAUSE_ASYM) {
469 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
471 ds_put_char(string, '\n');
475 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
477 char name[OFP_MAX_PORT_NAME_LEN];
480 memcpy(name, port->name, sizeof name);
481 for (j = 0; j < sizeof name - 1; j++) {
482 if (!isprint((unsigned char) name[j])) {
488 ds_put_char(string, ' ');
489 ofp_print_port_name(string, ntohs(port->port_no));
490 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
491 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
494 ds_put_format(string, " current: ");
495 ofp_print_port_features(string, ntohl(port->curr));
497 if (port->advertised) {
498 ds_put_format(string, " advertised: ");
499 ofp_print_port_features(string, ntohl(port->advertised));
501 if (port->supported) {
502 ds_put_format(string, " supported: ");
503 ofp_print_port_features(string, ntohl(port->supported));
506 ds_put_format(string, " peer: ");
507 ofp_print_port_features(string, ntohl(port->peer));
512 ofp_print_switch_features(struct ds *string,
513 const struct ofp_switch_features *osf)
515 size_t len = ntohs(osf->header.length);
516 struct ofp_phy_port *port_list;
520 ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
521 osf->header.version, ntohll(osf->datapath_id));
522 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
523 ntohl(osf->n_buffers));
524 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
525 ntohl(osf->capabilities), ntohl(osf->actions));
527 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
529 port_list = xmemdup(osf->ports, len - sizeof *osf);
530 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
531 for (i = 0; i < n_ports; i++) {
532 ofp_print_phy_port(string, &port_list[i]);
538 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
542 flags = ntohs(osc->flags);
544 ds_put_cstr(string, " frags=");
545 switch (flags & OFPC_FRAG_MASK) {
546 case OFPC_FRAG_NORMAL:
547 ds_put_cstr(string, "normal");
548 flags &= ~OFPC_FRAG_MASK;
551 ds_put_cstr(string, "drop");
552 flags &= ~OFPC_FRAG_MASK;
554 case OFPC_FRAG_REASM:
555 ds_put_cstr(string, "reassemble");
556 flags &= ~OFPC_FRAG_MASK;
560 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
563 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
566 static void print_wild(struct ds *string, const char *leader, int is_wild,
567 int verbosity, const char *format, ...)
568 __attribute__((format(printf, 5, 6)));
570 static void print_wild(struct ds *string, const char *leader, int is_wild,
571 int verbosity, const char *format, ...)
573 if (is_wild && verbosity < 2) {
576 ds_put_cstr(string, leader);
580 va_start(args, format);
581 ds_put_format_valist(string, format, args);
584 ds_put_char(string, '*');
586 ds_put_char(string, ',');
590 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
591 uint32_t wild_bits, int verbosity)
593 if (wild_bits >= 32 && verbosity < 2) {
596 ds_put_cstr(string, leader);
597 if (wild_bits < 32) {
598 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
600 ds_put_format(string, "/%d", 32 - wild_bits);
603 ds_put_char(string, '*');
605 ds_put_char(string, ',');
609 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
611 char *s = ofp_match_to_string(om, verbosity);
617 ofp_match_to_string(const struct ofp_match *om, int verbosity)
619 struct ds f = DS_EMPTY_INITIALIZER;
620 uint32_t w = ntohl(om->wildcards);
621 bool skip_type = false;
622 bool skip_proto = false;
624 if (!(w & OFPFW_DL_TYPE)) {
626 if (om->dl_type == htons(ETH_TYPE_IP)) {
627 if (!(w & OFPFW_NW_PROTO)) {
629 if (om->nw_proto == IPPROTO_ICMP) {
630 ds_put_cstr(&f, "icmp,");
631 } else if (om->nw_proto == IPPROTO_TCP) {
632 ds_put_cstr(&f, "tcp,");
633 } else if (om->nw_proto == IPPROTO_UDP) {
634 ds_put_cstr(&f, "udp,");
636 ds_put_cstr(&f, "ip,");
640 ds_put_cstr(&f, "ip,");
642 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
643 ds_put_cstr(&f, "arp,");
648 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
649 "%d", ntohs(om->in_port));
650 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
651 "%d", ntohs(om->dl_vlan));
652 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
653 "%d", om->dl_vlan_pcp);
654 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
655 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
656 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
657 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
659 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
660 "0x%04x", ntohs(om->dl_type));
662 print_ip_netmask(&f, "nw_src=", om->nw_src,
663 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
664 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
665 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
667 if (om->dl_type == htons(ETH_TYPE_ARP)) {
668 print_wild(&f, "arp_op=", w & OFPFW_NW_PROTO, verbosity,
671 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
675 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
677 if (om->nw_proto == IPPROTO_ICMP) {
678 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
679 "%d", ntohs(om->icmp_type));
680 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
681 "%d", ntohs(om->icmp_code));
683 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
684 "%d", ntohs(om->tp_src));
685 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
686 "%d", ntohs(om->tp_dst));
688 if (ds_last(&f) == ',') {
695 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
696 enum ofputil_msg_code code, int verbosity)
702 error = ofputil_decode_flow_mod(&fm, oh, true);
704 ofp_print_error(s, error);
709 switch (fm.command) {
711 ds_put_cstr(s, "ADD");
714 ds_put_cstr(s, "MOD");
716 case OFPFC_MODIFY_STRICT:
717 ds_put_cstr(s, "MOD_STRICT");
720 ds_put_cstr(s, "DEL");
722 case OFPFC_DELETE_STRICT:
723 ds_put_cstr(s, "DEL_STRICT");
726 ds_put_format(s, "cmd:%d", fm.command);
728 if (fm.table_id != 0) {
729 ds_put_format(s, " table:%d", fm.table_id);
733 if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
734 const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
735 ofp_print_match(s, &ofm->match, verbosity);
737 /* ofp_print_match() doesn't print priority. */
738 need_priority = true;
739 } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
740 const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
741 const void *nxm = nfm + 1;
744 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
745 ds_put_cstr(s, nxm_s);
748 /* nx_match_to_string() doesn't print priority. */
749 need_priority = true;
751 cls_rule_format(&fm.cr, s);
753 /* cls_rule_format() does print priority. */
754 need_priority = false;
757 if (ds_last(s) != ' ') {
760 if (fm.cookie != htonll(0)) {
761 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.cookie));
763 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
764 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
766 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
767 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
769 if (fm.cr.priority != OFP_DEFAULT_PRIORITY && need_priority) {
770 ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
772 if (fm.buffer_id != UINT32_MAX) {
773 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
776 ds_put_format(s, "flags:0x%"PRIx16" ", fm.flags);
779 ofp_print_actions(s, fm.actions, fm.n_actions);
783 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
785 ds_put_format(string, "%u", sec);
787 ds_put_format(string, ".%09u", nsec);
788 while (string->string[string->length - 1] == '0') {
792 ds_put_char(string, 's');
796 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
798 struct ofputil_flow_removed fr;
801 error = ofputil_decode_flow_removed(&fr, oh);
803 ofp_print_error(string, error);
807 ds_put_char(string, ' ');
808 cls_rule_format(&fr.rule, string);
810 ds_put_cstr(string, " reason=");
812 case OFPRR_IDLE_TIMEOUT:
813 ds_put_cstr(string, "idle");
815 case OFPRR_HARD_TIMEOUT:
816 ds_put_cstr(string, "hard");
819 ds_put_cstr(string, "delete");
822 ds_put_format(string, "**%"PRIu8"**", fr.reason);
826 if (fr.cookie != htonll(0)) {
827 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
829 ds_put_cstr(string, " duration");
830 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
831 ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
832 fr.idle_timeout, fr.packet_count, fr.byte_count);
836 ofp_print_port_mod(struct ds *string, const struct ofp_port_mod *opm)
838 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
839 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
840 ntohl(opm->config), ntohl(opm->mask));
841 ds_put_format(string, " advertise: ");
842 if (opm->advertise) {
843 ofp_print_port_features(string, ntohl(opm->advertise));
845 ds_put_format(string, "UNCHANGED\n");
850 ofp_print_error(struct ds *string, int error)
852 if (string->length) {
853 ds_put_char(string, ' ');
855 ds_put_cstr(string, "***decode error: ");
856 ofputil_format_error(string, error);
857 ds_put_cstr(string, "***\n");
861 ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
863 size_t len = ntohs(oem->header.length);
864 size_t payload_ofs, payload_len;
869 error = ofputil_decode_error_msg(&oem->header, &payload_ofs);
870 if (!is_ofp_error(error)) {
871 ofp_print_error(string, error);
872 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
876 ds_put_char(string, ' ');
877 ofputil_format_error(string, error);
878 ds_put_char(string, '\n');
880 payload = (const uint8_t *) oem + payload_ofs;
881 payload_len = len - payload_ofs;
882 switch (get_ofp_err_type(error)) {
883 case OFPET_HELLO_FAILED:
884 ds_put_printable(string, payload, payload_len);
887 case OFPET_BAD_REQUEST:
888 s = ofp_to_string(payload, payload_len, 1);
889 ds_put_cstr(string, s);
894 ds_put_hex_dump(string, payload, payload_len, 0, true);
900 ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
902 if (ops->reason == OFPPR_ADD) {
903 ds_put_format(string, " ADD:");
904 } else if (ops->reason == OFPPR_DELETE) {
905 ds_put_format(string, " DEL:");
906 } else if (ops->reason == OFPPR_MODIFY) {
907 ds_put_format(string, " MOD:");
910 ofp_print_phy_port(string, &ops->desc);
914 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_desc_stats *ods)
916 ds_put_char(string, '\n');
917 ds_put_format(string, "Manufacturer: %.*s\n",
918 (int) sizeof ods->mfr_desc, ods->mfr_desc);
919 ds_put_format(string, "Hardware: %.*s\n",
920 (int) sizeof ods->hw_desc, ods->hw_desc);
921 ds_put_format(string, "Software: %.*s\n",
922 (int) sizeof ods->sw_desc, ods->sw_desc);
923 ds_put_format(string, "Serial Num: %.*s\n",
924 (int) sizeof ods->serial_num, ods->serial_num);
925 ds_put_format(string, "DP Description: %.*s\n",
926 (int) sizeof ods->dp_desc, ods->dp_desc);
930 ofp_print_flow_stats_request(struct ds *string,
931 const struct ofp_stats_msg *osm)
933 struct flow_stats_request fsr;
936 error = ofputil_decode_flow_stats_request(&fsr, &osm->header);
938 ofp_print_error(string, error);
942 if (fsr.table_id != 0xff) {
943 ds_put_format(string, " table=%"PRIu8, fsr.table_id);
946 if (fsr.out_port != OFPP_NONE) {
947 ds_put_cstr(string, " out_port=");
948 ofp_print_port_name(string, fsr.out_port);
951 /* A flow stats request doesn't include a priority, but cls_rule_format()
952 * will print one unless it is OFP_DEFAULT_PRIORITY. */
953 fsr.match.priority = OFP_DEFAULT_PRIORITY;
955 ds_put_char(string, ' ');
956 cls_rule_format(&fsr.match, string);
960 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
964 ofpbuf_use_const(&b, oh, ntohs(oh->length));
966 struct ofputil_flow_stats fs;
969 retval = ofputil_decode_flow_stats_reply(&fs, &b);
972 ds_put_cstr(string, " ***parse error***");
977 ds_put_char(string, '\n');
979 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
981 ofp_print_duration(string, fs.duration_sec, fs.duration_nsec);
982 ds_put_format(string, ", table=%"PRIu8", ", fs.table_id);
983 ds_put_format(string, "n_packets=%"PRIu64", ", fs.packet_count);
984 ds_put_format(string, "n_bytes=%"PRIu64", ", fs.byte_count);
985 if (fs.idle_timeout != OFP_FLOW_PERMANENT) {
986 ds_put_format(string, "idle_timeout=%"PRIu16",", fs.idle_timeout);
988 if (fs.hard_timeout != OFP_FLOW_PERMANENT) {
989 ds_put_format(string, "hard_timeout=%"PRIu16",", fs.hard_timeout);
992 cls_rule_format(&fs.rule, string);
993 ds_put_char(string, ' ');
994 ofp_print_actions(string, fs.actions, fs.n_actions);
999 ofp_print_ofpst_aggregate_reply(struct ds *string,
1000 const struct ofp_aggregate_stats_reply *asr)
1002 ds_put_format(string, " packet_count=%"PRIu64,
1003 ntohll(get_32aligned_be64(&asr->packet_count)));
1004 ds_put_format(string, " byte_count=%"PRIu64,
1005 ntohll(get_32aligned_be64(&asr->byte_count)));
1006 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1010 ofp_print_nxst_aggregate_reply(struct ds *string,
1011 const struct nx_aggregate_stats_reply *nasr)
1013 ds_put_format(string, " packet_count=%"PRIu64, ntohll(nasr->packet_count));
1014 ds_put_format(string, " byte_count=%"PRIu64, ntohll(nasr->byte_count));
1015 ds_put_format(string, " flow_count=%"PRIu32, ntohl(nasr->flow_count));
1018 static void print_port_stat(struct ds *string, const char *leader,
1019 const ovs_32aligned_be64 *statp, int more)
1021 uint64_t stat = ntohll(get_32aligned_be64(statp));
1023 ds_put_cstr(string, leader);
1024 if (stat != UINT64_MAX) {
1025 ds_put_format(string, "%"PRIu64, stat);
1027 ds_put_char(string, '?');
1030 ds_put_cstr(string, ", ");
1032 ds_put_cstr(string, "\n");
1037 ofp_print_ofpst_port_request(struct ds *string,
1038 const struct ofp_port_stats_request *psr)
1040 ds_put_format(string, " port_no=%"PRIu16, ntohs(psr->port_no));
1044 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1047 const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1048 size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
1049 ds_put_format(string, " %zu ports\n", n);
1050 if (verbosity < 1) {
1055 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1057 ds_put_cstr(string, "rx ");
1058 print_port_stat(string, "pkts=", &ps->rx_packets, 1);
1059 print_port_stat(string, "bytes=", &ps->rx_bytes, 1);
1060 print_port_stat(string, "drop=", &ps->rx_dropped, 1);
1061 print_port_stat(string, "errs=", &ps->rx_errors, 1);
1062 print_port_stat(string, "frame=", &ps->rx_frame_err, 1);
1063 print_port_stat(string, "over=", &ps->rx_over_err, 1);
1064 print_port_stat(string, "crc=", &ps->rx_crc_err, 0);
1066 ds_put_cstr(string, " tx ");
1067 print_port_stat(string, "pkts=", &ps->tx_packets, 1);
1068 print_port_stat(string, "bytes=", &ps->tx_bytes, 1);
1069 print_port_stat(string, "drop=", &ps->tx_dropped, 1);
1070 print_port_stat(string, "errs=", &ps->tx_errors, 1);
1071 print_port_stat(string, "coll=", &ps->collisions, 0);
1076 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1079 const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1080 size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
1081 ds_put_format(string, " %zu tables\n", n);
1082 if (verbosity < 1) {
1087 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1088 ovs_strlcpy(name, ts->name, sizeof name);
1090 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1091 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1092 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1093 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1094 ds_put_cstr(string, " ");
1095 ds_put_format(string, "lookup=%"PRIu64", ",
1096 ntohll(get_32aligned_be64(&ts->lookup_count)));
1097 ds_put_format(string, "matched=%"PRIu64"\n",
1098 ntohll(get_32aligned_be64(&ts->matched_count)));
1103 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1105 if (queue_id == OFPQ_ALL) {
1106 ds_put_cstr(string, "ALL");
1108 ds_put_format(string, "%"PRIu32, queue_id);
1113 ofp_print_ofpst_queue_request(struct ds *string,
1114 const struct ofp_queue_stats_request *qsr)
1116 ds_put_cstr(string, "port=");
1117 ofp_print_port_name(string, ntohs(qsr->port_no));
1119 ds_put_cstr(string, " queue=");
1120 ofp_print_queue_name(string, ntohl(qsr->queue_id));
1124 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1127 const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1128 size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
1129 ds_put_format(string, " %zu queues\n", n);
1130 if (verbosity < 1) {
1135 ds_put_cstr(string, " port ");
1136 ofp_print_port_name(string, ntohs(qs->port_no));
1137 ds_put_cstr(string, " queue ");
1138 ofp_print_queue_name(string, ntohl(qs->queue_id));
1139 ds_put_cstr(string, ": ");
1141 print_port_stat(string, "bytes=", &qs->tx_bytes, 1);
1142 print_port_stat(string, "pkts=", &qs->tx_packets, 1);
1143 print_port_stat(string, "errors=", &qs->tx_errors, 0);
1148 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1150 const struct ofp_stats_msg *srq = (const struct ofp_stats_msg *) oh;
1153 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1159 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1161 const struct ofp_stats_msg *srp = (const struct ofp_stats_msg *) oh;
1164 uint16_t flags = ntohs(srp->flags);
1166 ds_put_cstr(string, " flags=");
1167 if (flags & OFPSF_REPLY_MORE) {
1168 ds_put_cstr(string, "[more]");
1169 flags &= ~OFPSF_REPLY_MORE;
1172 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1179 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1181 size_t len = ntohs(oh->length);
1183 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1184 if (verbosity > 1) {
1185 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1190 ofp_print_nxt_role_message(struct ds *string,
1191 const struct nx_role_request *nrr)
1193 unsigned int role = ntohl(nrr->role);
1195 ds_put_cstr(string, " role=");
1196 if (role == NX_ROLE_OTHER) {
1197 ds_put_cstr(string, "other");
1198 } else if (role == NX_ROLE_MASTER) {
1199 ds_put_cstr(string, "master");
1200 } else if (role == NX_ROLE_SLAVE) {
1201 ds_put_cstr(string, "slave");
1203 ds_put_format(string, "%u", role);
1208 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1209 const struct nxt_flow_mod_table_id *nfmti)
1211 ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1215 ofp_print_nxt_set_flow_format(struct ds *string,
1216 const struct nxt_set_flow_format *nsff)
1218 uint32_t format = ntohl(nsff->format);
1220 ds_put_cstr(string, " format=");
1221 if (ofputil_flow_format_is_valid(format)) {
1222 ds_put_cstr(string, ofputil_flow_format_to_string(format));
1224 ds_put_format(string, "%"PRIu32, format);
1229 ofp_to_string__(const struct ofp_header *oh,
1230 const struct ofputil_msg_type *type, struct ds *string,
1233 enum ofputil_msg_code code;
1234 const void *msg = oh;
1236 ds_put_format(string, "%s (xid=0x%"PRIx32"):",
1237 ofputil_msg_type_name(type), ntohl(oh->xid));
1239 code = ofputil_msg_type_code(type);
1241 case OFPUTIL_MSG_INVALID:
1244 case OFPUTIL_OFPT_HELLO:
1245 ds_put_char(string, '\n');
1246 ds_put_hex_dump(string, oh + 1, ntohs(oh->length) - sizeof *oh,
1250 case OFPUTIL_OFPT_ERROR:
1251 ofp_print_error_msg(string, msg);
1254 case OFPUTIL_OFPT_ECHO_REQUEST:
1255 case OFPUTIL_OFPT_ECHO_REPLY:
1256 ofp_print_echo(string, oh, verbosity);
1259 case OFPUTIL_OFPT_FEATURES_REQUEST:
1262 case OFPUTIL_OFPT_FEATURES_REPLY:
1263 ofp_print_switch_features(string, msg);
1266 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1269 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1270 case OFPUTIL_OFPT_SET_CONFIG:
1271 ofp_print_switch_config(string, msg);
1274 case OFPUTIL_OFPT_PACKET_IN:
1275 ofp_print_packet_in(string, msg, verbosity);
1278 case OFPUTIL_OFPT_FLOW_REMOVED:
1279 case OFPUTIL_NXT_FLOW_REMOVED:
1280 ofp_print_flow_removed(string, msg);
1283 case OFPUTIL_OFPT_PORT_STATUS:
1284 ofp_print_port_status(string, msg);
1287 case OFPUTIL_OFPT_PACKET_OUT:
1288 ofp_print_packet_out(string, msg, verbosity);
1291 case OFPUTIL_OFPT_FLOW_MOD:
1292 ofp_print_flow_mod(string, msg, code, verbosity);
1295 case OFPUTIL_OFPT_PORT_MOD:
1296 ofp_print_port_mod(string, msg);
1299 case OFPUTIL_OFPT_BARRIER_REQUEST:
1300 case OFPUTIL_OFPT_BARRIER_REPLY:
1303 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1304 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1308 case OFPUTIL_OFPST_DESC_REQUEST:
1309 ofp_print_stats_request(string, oh);
1312 case OFPUTIL_OFPST_FLOW_REQUEST:
1313 case OFPUTIL_NXST_FLOW_REQUEST:
1314 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1315 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1316 ofp_print_stats_request(string, oh);
1317 ofp_print_flow_stats_request(string, msg);
1320 case OFPUTIL_OFPST_TABLE_REQUEST:
1321 ofp_print_stats_request(string, oh);
1324 case OFPUTIL_OFPST_PORT_REQUEST:
1325 ofp_print_stats_request(string, oh);
1326 ofp_print_ofpst_port_request(string, msg);
1329 case OFPUTIL_OFPST_QUEUE_REQUEST:
1330 ofp_print_stats_request(string, oh);
1331 ofp_print_ofpst_queue_request(string, msg);
1334 case OFPUTIL_OFPST_DESC_REPLY:
1335 ofp_print_stats_reply(string, oh);
1336 ofp_print_ofpst_desc_reply(string, msg);
1339 case OFPUTIL_OFPST_FLOW_REPLY:
1340 case OFPUTIL_NXST_FLOW_REPLY:
1341 ofp_print_stats_reply(string, oh);
1342 ofp_print_flow_stats_reply(string, oh);
1345 case OFPUTIL_OFPST_QUEUE_REPLY:
1346 ofp_print_stats_reply(string, oh);
1347 ofp_print_ofpst_queue_reply(string, oh, verbosity);
1350 case OFPUTIL_OFPST_PORT_REPLY:
1351 ofp_print_stats_reply(string, oh);
1352 ofp_print_ofpst_port_reply(string, oh, verbosity);
1355 case OFPUTIL_OFPST_TABLE_REPLY:
1356 ofp_print_stats_reply(string, oh);
1357 ofp_print_ofpst_table_reply(string, oh, verbosity);
1360 case OFPUTIL_OFPST_AGGREGATE_REPLY:
1361 ofp_print_stats_reply(string, oh);
1362 ofp_print_ofpst_aggregate_reply(string, msg);
1365 case OFPUTIL_NXT_ROLE_REQUEST:
1366 case OFPUTIL_NXT_ROLE_REPLY:
1367 ofp_print_nxt_role_message(string, msg);
1370 case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
1371 ofp_print_nxt_flow_mod_table_id(string, msg);
1374 case OFPUTIL_NXT_SET_FLOW_FORMAT:
1375 ofp_print_nxt_set_flow_format(string, msg);
1378 case OFPUTIL_NXT_FLOW_MOD:
1379 ofp_print_flow_mod(string, msg, code, verbosity);
1382 case OFPUTIL_NXST_AGGREGATE_REPLY:
1383 ofp_print_stats_reply(string, oh);
1384 ofp_print_nxst_aggregate_reply(string, msg);
1389 /* Composes and returns a string representing the OpenFlow packet of 'len'
1390 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1391 * verbosity and higher numbers increase verbosity. The caller is responsible
1392 * for freeing the string. */
1394 ofp_to_string(const void *oh_, size_t len, int verbosity)
1396 struct ds string = DS_EMPTY_INITIALIZER;
1397 const struct ofp_header *oh = oh_;
1400 ds_put_cstr(&string, "OpenFlow message is empty\n");
1401 } else if (len < sizeof(struct ofp_header)) {
1402 ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
1404 } else if (oh->version != OFP_VERSION) {
1405 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n",
1407 } else if (ntohs(oh->length) > len) {
1408 ds_put_format(&string,
1409 "(***truncated to %zu bytes from %"PRIu16"***)\n",
1410 len, ntohs(oh->length));
1411 } else if (ntohs(oh->length) < len) {
1412 ds_put_format(&string,
1413 "(***only uses %"PRIu16" bytes out of %zu***)\n",
1414 ntohs(oh->length), len);
1416 const struct ofputil_msg_type *type;
1419 error = ofputil_decode_msg_type(oh, &type);
1421 ofp_to_string__(oh, type, &string, verbosity);
1422 if (verbosity >= 5) {
1423 if (ds_last(&string) != '\n') {
1424 ds_put_char(&string, '\n');
1426 ds_put_hex_dump(&string, oh, len, 0, true);
1428 if (ds_last(&string) != '\n') {
1429 ds_put_char(&string, '\n');
1431 return ds_steal_cstr(&string);
1434 ofp_print_error(&string, error);
1436 ds_put_hex_dump(&string, oh, len, 0, true);
1437 return ds_steal_cstr(&string);
1440 /* Returns the name for the specified OpenFlow message type as a string,
1441 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1442 * hex number, e.g. "0x55".
1444 * The caller must free the returned string when it is no longer needed. */
1446 ofp_message_type_to_string(uint8_t type)
1457 case OFPT_ECHO_REQUEST:
1458 name = "ECHO_REQUEST";
1460 case OFPT_ECHO_REPLY:
1461 name = "ECHO_REPLY";
1466 case OFPT_FEATURES_REQUEST:
1467 name = "FEATURES_REQUEST";
1469 case OFPT_FEATURES_REPLY:
1470 name = "FEATURES_REPLY";
1472 case OFPT_GET_CONFIG_REQUEST:
1473 name = "GET_CONFIG_REQUEST";
1475 case OFPT_GET_CONFIG_REPLY:
1476 name = "GET_CONFIG_REPLY";
1478 case OFPT_SET_CONFIG:
1479 name = "SET_CONFIG";
1481 case OFPT_PACKET_IN:
1484 case OFPT_FLOW_REMOVED:
1485 name = "FLOW_REMOVED";
1487 case OFPT_PORT_STATUS:
1488 name = "PORT_STATUS";
1490 case OFPT_PACKET_OUT:
1491 name = "PACKET_OUT";
1499 case OFPT_STATS_REQUEST:
1500 name = "STATS_REQUEST";
1502 case OFPT_STATS_REPLY:
1503 name = "STATS_REPLY";
1505 case OFPT_BARRIER_REQUEST:
1506 name = "BARRIER_REQUEST";
1508 case OFPT_BARRIER_REPLY:
1509 name = "BARRIER_REPLY";
1511 case OFPT_QUEUE_GET_CONFIG_REQUEST:
1512 name = "QUEUE_GET_CONFIG_REQUEST";
1514 case OFPT_QUEUE_GET_CONFIG_REPLY:
1515 name = "QUEUE_GET_CONFIG_REPLY";
1522 return name ? xasprintf("OFPT_%s", name) : xasprintf("0x%02"PRIx8, type);
1526 print_and_free(FILE *stream, char *string)
1528 fputs(string, stream);
1532 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1533 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1534 * numbers increase verbosity. */
1536 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1538 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1541 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1542 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1543 * the Ethernet frame (of which 'len' bytes were captured).
1545 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1547 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1549 print_and_free(stream, ofp_packet_to_string(data, len, total_len));