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 bundle_format((const struct nx_action_bundle *) a, s);
356 ofp_print_actions(struct ds *string, const union ofp_action *actions,
359 const union ofp_action *a;
362 ds_put_cstr(string, "actions=");
364 ds_put_cstr(string, "drop");
367 OFPUTIL_ACTION_FOR_EACH (a, left, actions, n_actions) {
368 int code = ofputil_decode_action(a);
371 ds_put_cstr(string, ",");
373 ofp_print_action(string, a, code);
375 ofp_print_error(string, -code);
379 ds_put_format(string, " ***%zu leftover bytes following actions",
385 ofp_print_packet_out(struct ds *string, const struct ofp_packet_out *opo,
388 size_t len = ntohs(opo->header.length);
389 size_t actions_len = ntohs(opo->actions_len);
391 ds_put_cstr(string, " in_port=");
392 ofp_print_port_name(string, ntohs(opo->in_port));
394 ds_put_format(string, " actions_len=%zu ", actions_len);
395 if (actions_len > (ntohs(opo->header.length) - sizeof *opo)) {
396 ds_put_format(string, "***packet too short for action length***\n");
399 if (actions_len % sizeof(union ofp_action)) {
400 ds_put_format(string, "***action length not a multiple of %zu***\n",
401 sizeof(union ofp_action));
403 ofp_print_actions(string, (const union ofp_action *) opo->actions,
404 actions_len / sizeof(union ofp_action));
406 if (ntohl(opo->buffer_id) == UINT32_MAX) {
407 int data_len = len - sizeof *opo - actions_len;
408 ds_put_format(string, " data_len=%d", data_len);
409 if (verbosity > 0 && len > sizeof *opo) {
410 char *packet = ofp_packet_to_string(
411 (uint8_t *)opo->actions + actions_len, data_len, data_len);
412 ds_put_char(string, '\n');
413 ds_put_cstr(string, packet);
417 ds_put_format(string, " buffer=0x%08"PRIx32, ntohl(opo->buffer_id));
419 ds_put_char(string, '\n');
422 /* qsort comparison function. */
424 compare_ports(const void *a_, const void *b_)
426 const struct ofp_phy_port *a = a_;
427 const struct ofp_phy_port *b = b_;
428 uint16_t ap = ntohs(a->port_no);
429 uint16_t bp = ntohs(b->port_no);
431 return ap < bp ? -1 : ap > bp;
434 static void ofp_print_port_features(struct ds *string, uint32_t features)
437 ds_put_cstr(string, "Unsupported\n");
440 if (features & OFPPF_10MB_HD) {
441 ds_put_cstr(string, "10MB-HD ");
443 if (features & OFPPF_10MB_FD) {
444 ds_put_cstr(string, "10MB-FD ");
446 if (features & OFPPF_100MB_HD) {
447 ds_put_cstr(string, "100MB-HD ");
449 if (features & OFPPF_100MB_FD) {
450 ds_put_cstr(string, "100MB-FD ");
452 if (features & OFPPF_1GB_HD) {
453 ds_put_cstr(string, "1GB-HD ");
455 if (features & OFPPF_1GB_FD) {
456 ds_put_cstr(string, "1GB-FD ");
458 if (features & OFPPF_10GB_FD) {
459 ds_put_cstr(string, "10GB-FD ");
461 if (features & OFPPF_COPPER) {
462 ds_put_cstr(string, "COPPER ");
464 if (features & OFPPF_FIBER) {
465 ds_put_cstr(string, "FIBER ");
467 if (features & OFPPF_AUTONEG) {
468 ds_put_cstr(string, "AUTO_NEG ");
470 if (features & OFPPF_PAUSE) {
471 ds_put_cstr(string, "AUTO_PAUSE ");
473 if (features & OFPPF_PAUSE_ASYM) {
474 ds_put_cstr(string, "AUTO_PAUSE_ASYM ");
476 ds_put_char(string, '\n');
480 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
482 char name[OFP_MAX_PORT_NAME_LEN];
485 memcpy(name, port->name, sizeof name);
486 for (j = 0; j < sizeof name - 1; j++) {
487 if (!isprint((unsigned char) name[j])) {
493 ds_put_char(string, ' ');
494 ofp_print_port_name(string, ntohs(port->port_no));
495 ds_put_format(string, "(%s): addr:"ETH_ADDR_FMT", config: %#x, state:%#x\n",
496 name, ETH_ADDR_ARGS(port->hw_addr), ntohl(port->config),
499 ds_put_format(string, " current: ");
500 ofp_print_port_features(string, ntohl(port->curr));
502 if (port->advertised) {
503 ds_put_format(string, " advertised: ");
504 ofp_print_port_features(string, ntohl(port->advertised));
506 if (port->supported) {
507 ds_put_format(string, " supported: ");
508 ofp_print_port_features(string, ntohl(port->supported));
511 ds_put_format(string, " peer: ");
512 ofp_print_port_features(string, ntohl(port->peer));
517 ofp_print_switch_features(struct ds *string,
518 const struct ofp_switch_features *osf)
520 size_t len = ntohs(osf->header.length);
521 struct ofp_phy_port *port_list;
525 ds_put_format(string, " ver:0x%x, dpid:%016"PRIx64"\n",
526 osf->header.version, ntohll(osf->datapath_id));
527 ds_put_format(string, "n_tables:%d, n_buffers:%d\n", osf->n_tables,
528 ntohl(osf->n_buffers));
529 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
530 ntohl(osf->capabilities), ntohl(osf->actions));
532 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
534 port_list = xmemdup(osf->ports, len - sizeof *osf);
535 qsort(port_list, n_ports, sizeof *port_list, compare_ports);
536 for (i = 0; i < n_ports; i++) {
537 ofp_print_phy_port(string, &port_list[i]);
543 ofp_print_switch_config(struct ds *string, const struct ofp_switch_config *osc)
547 flags = ntohs(osc->flags);
549 ds_put_cstr(string, " frags=");
550 switch (flags & OFPC_FRAG_MASK) {
551 case OFPC_FRAG_NORMAL:
552 ds_put_cstr(string, "normal");
553 flags &= ~OFPC_FRAG_MASK;
556 ds_put_cstr(string, "drop");
557 flags &= ~OFPC_FRAG_MASK;
559 case OFPC_FRAG_REASM:
560 ds_put_cstr(string, "reassemble");
561 flags &= ~OFPC_FRAG_MASK;
565 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***", flags);
568 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
571 static void print_wild(struct ds *string, const char *leader, int is_wild,
572 int verbosity, const char *format, ...)
573 __attribute__((format(printf, 5, 6)));
575 static void print_wild(struct ds *string, const char *leader, int is_wild,
576 int verbosity, const char *format, ...)
578 if (is_wild && verbosity < 2) {
581 ds_put_cstr(string, leader);
585 va_start(args, format);
586 ds_put_format_valist(string, format, args);
589 ds_put_char(string, '*');
591 ds_put_char(string, ',');
595 print_ip_netmask(struct ds *string, const char *leader, ovs_be32 ip,
596 uint32_t wild_bits, int verbosity)
598 if (wild_bits >= 32 && verbosity < 2) {
601 ds_put_cstr(string, leader);
602 if (wild_bits < 32) {
603 ds_put_format(string, IP_FMT, IP_ARGS(&ip));
605 ds_put_format(string, "/%d", 32 - wild_bits);
608 ds_put_char(string, '*');
610 ds_put_char(string, ',');
614 ofp_print_match(struct ds *f, const struct ofp_match *om, int verbosity)
616 char *s = ofp_match_to_string(om, verbosity);
622 ofp_match_to_string(const struct ofp_match *om, int verbosity)
624 struct ds f = DS_EMPTY_INITIALIZER;
625 uint32_t w = ntohl(om->wildcards);
626 bool skip_type = false;
627 bool skip_proto = false;
629 if (!(w & OFPFW_DL_TYPE)) {
631 if (om->dl_type == htons(ETH_TYPE_IP)) {
632 if (!(w & OFPFW_NW_PROTO)) {
634 if (om->nw_proto == IPPROTO_ICMP) {
635 ds_put_cstr(&f, "icmp,");
636 } else if (om->nw_proto == IPPROTO_TCP) {
637 ds_put_cstr(&f, "tcp,");
638 } else if (om->nw_proto == IPPROTO_UDP) {
639 ds_put_cstr(&f, "udp,");
641 ds_put_cstr(&f, "ip,");
645 ds_put_cstr(&f, "ip,");
647 } else if (om->dl_type == htons(ETH_TYPE_ARP)) {
648 ds_put_cstr(&f, "arp,");
653 print_wild(&f, "in_port=", w & OFPFW_IN_PORT, verbosity,
654 "%d", ntohs(om->in_port));
655 print_wild(&f, "dl_vlan=", w & OFPFW_DL_VLAN, verbosity,
656 "%d", ntohs(om->dl_vlan));
657 print_wild(&f, "dl_vlan_pcp=", w & OFPFW_DL_VLAN_PCP, verbosity,
658 "%d", om->dl_vlan_pcp);
659 print_wild(&f, "dl_src=", w & OFPFW_DL_SRC, verbosity,
660 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
661 print_wild(&f, "dl_dst=", w & OFPFW_DL_DST, verbosity,
662 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
664 print_wild(&f, "dl_type=", w & OFPFW_DL_TYPE, verbosity,
665 "0x%04x", ntohs(om->dl_type));
667 print_ip_netmask(&f, "nw_src=", om->nw_src,
668 (w & OFPFW_NW_SRC_MASK) >> OFPFW_NW_SRC_SHIFT, verbosity);
669 print_ip_netmask(&f, "nw_dst=", om->nw_dst,
670 (w & OFPFW_NW_DST_MASK) >> OFPFW_NW_DST_SHIFT, verbosity);
672 if (om->dl_type == htons(ETH_TYPE_ARP)) {
673 print_wild(&f, "arp_op=", w & OFPFW_NW_PROTO, verbosity,
676 print_wild(&f, "nw_proto=", w & OFPFW_NW_PROTO, verbosity,
680 print_wild(&f, "nw_tos=", w & OFPFW_NW_TOS, verbosity,
682 if (om->nw_proto == IPPROTO_ICMP) {
683 print_wild(&f, "icmp_type=", w & OFPFW_ICMP_TYPE, verbosity,
684 "%d", ntohs(om->icmp_type));
685 print_wild(&f, "icmp_code=", w & OFPFW_ICMP_CODE, verbosity,
686 "%d", ntohs(om->icmp_code));
688 print_wild(&f, "tp_src=", w & OFPFW_TP_SRC, verbosity,
689 "%d", ntohs(om->tp_src));
690 print_wild(&f, "tp_dst=", w & OFPFW_TP_DST, verbosity,
691 "%d", ntohs(om->tp_dst));
693 if (ds_last(&f) == ',') {
700 ofp_print_flow_mod(struct ds *s, const struct ofp_header *oh,
701 enum ofputil_msg_code code, int verbosity)
707 error = ofputil_decode_flow_mod(&fm, oh, true);
709 ofp_print_error(s, error);
714 switch (fm.command) {
716 ds_put_cstr(s, "ADD");
719 ds_put_cstr(s, "MOD");
721 case OFPFC_MODIFY_STRICT:
722 ds_put_cstr(s, "MOD_STRICT");
725 ds_put_cstr(s, "DEL");
727 case OFPFC_DELETE_STRICT:
728 ds_put_cstr(s, "DEL_STRICT");
731 ds_put_format(s, "cmd:%d", fm.command);
733 if (fm.table_id != 0) {
734 ds_put_format(s, " table:%d", fm.table_id);
738 if (verbosity >= 3 && code == OFPUTIL_OFPT_FLOW_MOD) {
739 const struct ofp_flow_mod *ofm = (const struct ofp_flow_mod *) oh;
740 ofp_print_match(s, &ofm->match, verbosity);
742 /* ofp_print_match() doesn't print priority. */
743 need_priority = true;
744 } else if (verbosity >= 3 && code == OFPUTIL_NXT_FLOW_MOD) {
745 const struct nx_flow_mod *nfm = (const struct nx_flow_mod *) oh;
746 const void *nxm = nfm + 1;
749 nxm_s = nx_match_to_string(nxm, ntohs(nfm->match_len));
750 ds_put_cstr(s, nxm_s);
753 /* nx_match_to_string() doesn't print priority. */
754 need_priority = true;
756 cls_rule_format(&fm.cr, s);
758 /* cls_rule_format() does print priority. */
759 need_priority = false;
762 if (ds_last(s) != ' ') {
765 if (fm.cookie != htonll(0)) {
766 ds_put_format(s, "cookie:0x%"PRIx64" ", ntohll(fm.cookie));
768 if (fm.idle_timeout != OFP_FLOW_PERMANENT) {
769 ds_put_format(s, "idle:%"PRIu16" ", fm.idle_timeout);
771 if (fm.hard_timeout != OFP_FLOW_PERMANENT) {
772 ds_put_format(s, "hard:%"PRIu16" ", fm.hard_timeout);
774 if (fm.cr.priority != OFP_DEFAULT_PRIORITY && need_priority) {
775 ds_put_format(s, "pri:%"PRIu16" ", fm.cr.priority);
777 if (fm.buffer_id != UINT32_MAX) {
778 ds_put_format(s, "buf:0x%"PRIx32" ", fm.buffer_id);
781 ds_put_format(s, "flags:0x%"PRIx16" ", fm.flags);
784 ofp_print_actions(s, fm.actions, fm.n_actions);
788 ofp_print_duration(struct ds *string, unsigned int sec, unsigned int nsec)
790 ds_put_format(string, "%u", sec);
792 ds_put_format(string, ".%09u", nsec);
793 while (string->string[string->length - 1] == '0') {
797 ds_put_char(string, 's');
801 ofp_print_flow_removed(struct ds *string, const struct ofp_header *oh)
803 struct ofputil_flow_removed fr;
806 error = ofputil_decode_flow_removed(&fr, oh);
808 ofp_print_error(string, error);
812 ds_put_char(string, ' ');
813 cls_rule_format(&fr.rule, string);
815 ds_put_cstr(string, " reason=");
817 case OFPRR_IDLE_TIMEOUT:
818 ds_put_cstr(string, "idle");
820 case OFPRR_HARD_TIMEOUT:
821 ds_put_cstr(string, "hard");
824 ds_put_cstr(string, "delete");
827 ds_put_format(string, "**%"PRIu8"**", fr.reason);
831 if (fr.cookie != htonll(0)) {
832 ds_put_format(string, " cookie:0x%"PRIx64, ntohll(fr.cookie));
834 ds_put_cstr(string, " duration");
835 ofp_print_duration(string, fr.duration_sec, fr.duration_nsec);
836 ds_put_format(string, " idle%"PRIu16" pkts%"PRIu64" bytes%"PRIu64"\n",
837 fr.idle_timeout, fr.packet_count, fr.byte_count);
841 ofp_print_port_mod(struct ds *string, const struct ofp_port_mod *opm)
843 ds_put_format(string, "port: %d: addr:"ETH_ADDR_FMT", config: %#x, mask:%#x\n",
844 ntohs(opm->port_no), ETH_ADDR_ARGS(opm->hw_addr),
845 ntohl(opm->config), ntohl(opm->mask));
846 ds_put_format(string, " advertise: ");
847 if (opm->advertise) {
848 ofp_print_port_features(string, ntohl(opm->advertise));
850 ds_put_format(string, "UNCHANGED\n");
855 ofp_print_error(struct ds *string, int error)
857 if (string->length) {
858 ds_put_char(string, ' ');
860 ds_put_cstr(string, "***decode error: ");
861 ofputil_format_error(string, error);
862 ds_put_cstr(string, "***\n");
866 ofp_print_error_msg(struct ds *string, const struct ofp_error_msg *oem)
868 size_t len = ntohs(oem->header.length);
869 size_t payload_ofs, payload_len;
874 error = ofputil_decode_error_msg(&oem->header, &payload_ofs);
875 if (!is_ofp_error(error)) {
876 ofp_print_error(string, error);
877 ds_put_hex_dump(string, oem->data, len - sizeof *oem, 0, true);
881 ds_put_char(string, ' ');
882 ofputil_format_error(string, error);
883 ds_put_char(string, '\n');
885 payload = (const uint8_t *) oem + payload_ofs;
886 payload_len = len - payload_ofs;
887 switch (get_ofp_err_type(error)) {
888 case OFPET_HELLO_FAILED:
889 ds_put_printable(string, payload, payload_len);
892 case OFPET_BAD_REQUEST:
893 s = ofp_to_string(payload, payload_len, 1);
894 ds_put_cstr(string, s);
899 ds_put_hex_dump(string, payload, payload_len, 0, true);
905 ofp_print_port_status(struct ds *string, const struct ofp_port_status *ops)
907 if (ops->reason == OFPPR_ADD) {
908 ds_put_format(string, " ADD:");
909 } else if (ops->reason == OFPPR_DELETE) {
910 ds_put_format(string, " DEL:");
911 } else if (ops->reason == OFPPR_MODIFY) {
912 ds_put_format(string, " MOD:");
915 ofp_print_phy_port(string, &ops->desc);
919 ofp_print_ofpst_desc_reply(struct ds *string, const struct ofp_desc_stats *ods)
921 ds_put_char(string, '\n');
922 ds_put_format(string, "Manufacturer: %.*s\n",
923 (int) sizeof ods->mfr_desc, ods->mfr_desc);
924 ds_put_format(string, "Hardware: %.*s\n",
925 (int) sizeof ods->hw_desc, ods->hw_desc);
926 ds_put_format(string, "Software: %.*s\n",
927 (int) sizeof ods->sw_desc, ods->sw_desc);
928 ds_put_format(string, "Serial Num: %.*s\n",
929 (int) sizeof ods->serial_num, ods->serial_num);
930 ds_put_format(string, "DP Description: %.*s\n",
931 (int) sizeof ods->dp_desc, ods->dp_desc);
935 ofp_print_flow_stats_request(struct ds *string,
936 const struct ofp_stats_msg *osm)
938 struct flow_stats_request fsr;
941 error = ofputil_decode_flow_stats_request(&fsr, &osm->header);
943 ofp_print_error(string, error);
947 if (fsr.table_id != 0xff) {
948 ds_put_format(string, " table=%"PRIu8, fsr.table_id);
951 if (fsr.out_port != OFPP_NONE) {
952 ds_put_cstr(string, " out_port=");
953 ofp_print_port_name(string, fsr.out_port);
956 /* A flow stats request doesn't include a priority, but cls_rule_format()
957 * will print one unless it is OFP_DEFAULT_PRIORITY. */
958 fsr.match.priority = OFP_DEFAULT_PRIORITY;
960 ds_put_char(string, ' ');
961 cls_rule_format(&fsr.match, string);
965 ofp_print_flow_stats_reply(struct ds *string, const struct ofp_header *oh)
969 ofpbuf_use_const(&b, oh, ntohs(oh->length));
971 struct ofputil_flow_stats fs;
974 retval = ofputil_decode_flow_stats_reply(&fs, &b);
977 ds_put_cstr(string, " ***parse error***");
982 ds_put_char(string, '\n');
984 ds_put_format(string, " cookie=0x%"PRIx64", duration=",
986 ofp_print_duration(string, fs.duration_sec, fs.duration_nsec);
987 ds_put_format(string, ", table=%"PRIu8", ", fs.table_id);
988 ds_put_format(string, "n_packets=%"PRIu64", ", fs.packet_count);
989 ds_put_format(string, "n_bytes=%"PRIu64", ", fs.byte_count);
990 if (fs.idle_timeout != OFP_FLOW_PERMANENT) {
991 ds_put_format(string, "idle_timeout=%"PRIu16",", fs.idle_timeout);
993 if (fs.hard_timeout != OFP_FLOW_PERMANENT) {
994 ds_put_format(string, "hard_timeout=%"PRIu16",", fs.hard_timeout);
997 cls_rule_format(&fs.rule, string);
998 ds_put_char(string, ' ');
999 ofp_print_actions(string, fs.actions, fs.n_actions);
1004 ofp_print_ofpst_aggregate_reply(struct ds *string,
1005 const struct ofp_aggregate_stats_reply *asr)
1007 ds_put_format(string, " packet_count=%"PRIu64,
1008 ntohll(get_32aligned_be64(&asr->packet_count)));
1009 ds_put_format(string, " byte_count=%"PRIu64,
1010 ntohll(get_32aligned_be64(&asr->byte_count)));
1011 ds_put_format(string, " flow_count=%"PRIu32, ntohl(asr->flow_count));
1015 ofp_print_nxst_aggregate_reply(struct ds *string,
1016 const struct nx_aggregate_stats_reply *nasr)
1018 ds_put_format(string, " packet_count=%"PRIu64, ntohll(nasr->packet_count));
1019 ds_put_format(string, " byte_count=%"PRIu64, ntohll(nasr->byte_count));
1020 ds_put_format(string, " flow_count=%"PRIu32, ntohl(nasr->flow_count));
1023 static void print_port_stat(struct ds *string, const char *leader,
1024 const ovs_32aligned_be64 *statp, int more)
1026 uint64_t stat = ntohll(get_32aligned_be64(statp));
1028 ds_put_cstr(string, leader);
1029 if (stat != UINT64_MAX) {
1030 ds_put_format(string, "%"PRIu64, stat);
1032 ds_put_char(string, '?');
1035 ds_put_cstr(string, ", ");
1037 ds_put_cstr(string, "\n");
1042 ofp_print_ofpst_port_request(struct ds *string,
1043 const struct ofp_port_stats_request *psr)
1045 ds_put_format(string, " port_no=%"PRIu16, ntohs(psr->port_no));
1049 ofp_print_ofpst_port_reply(struct ds *string, const struct ofp_header *oh,
1052 const struct ofp_port_stats *ps = ofputil_stats_body(oh);
1053 size_t n = ofputil_stats_body_len(oh) / sizeof *ps;
1054 ds_put_format(string, " %zu ports\n", n);
1055 if (verbosity < 1) {
1060 ds_put_format(string, " port %2"PRIu16": ", ntohs(ps->port_no));
1062 ds_put_cstr(string, "rx ");
1063 print_port_stat(string, "pkts=", &ps->rx_packets, 1);
1064 print_port_stat(string, "bytes=", &ps->rx_bytes, 1);
1065 print_port_stat(string, "drop=", &ps->rx_dropped, 1);
1066 print_port_stat(string, "errs=", &ps->rx_errors, 1);
1067 print_port_stat(string, "frame=", &ps->rx_frame_err, 1);
1068 print_port_stat(string, "over=", &ps->rx_over_err, 1);
1069 print_port_stat(string, "crc=", &ps->rx_crc_err, 0);
1071 ds_put_cstr(string, " tx ");
1072 print_port_stat(string, "pkts=", &ps->tx_packets, 1);
1073 print_port_stat(string, "bytes=", &ps->tx_bytes, 1);
1074 print_port_stat(string, "drop=", &ps->tx_dropped, 1);
1075 print_port_stat(string, "errs=", &ps->tx_errors, 1);
1076 print_port_stat(string, "coll=", &ps->collisions, 0);
1081 ofp_print_ofpst_table_reply(struct ds *string, const struct ofp_header *oh,
1084 const struct ofp_table_stats *ts = ofputil_stats_body(oh);
1085 size_t n = ofputil_stats_body_len(oh) / sizeof *ts;
1086 ds_put_format(string, " %zu tables\n", n);
1087 if (verbosity < 1) {
1092 char name[OFP_MAX_TABLE_NAME_LEN + 1];
1093 ovs_strlcpy(name, ts->name, sizeof name);
1095 ds_put_format(string, " %d: %-8s: ", ts->table_id, name);
1096 ds_put_format(string, "wild=0x%05"PRIx32", ", ntohl(ts->wildcards));
1097 ds_put_format(string, "max=%6"PRIu32", ", ntohl(ts->max_entries));
1098 ds_put_format(string, "active=%"PRIu32"\n", ntohl(ts->active_count));
1099 ds_put_cstr(string, " ");
1100 ds_put_format(string, "lookup=%"PRIu64", ",
1101 ntohll(get_32aligned_be64(&ts->lookup_count)));
1102 ds_put_format(string, "matched=%"PRIu64"\n",
1103 ntohll(get_32aligned_be64(&ts->matched_count)));
1108 ofp_print_queue_name(struct ds *string, uint32_t queue_id)
1110 if (queue_id == OFPQ_ALL) {
1111 ds_put_cstr(string, "ALL");
1113 ds_put_format(string, "%"PRIu32, queue_id);
1118 ofp_print_ofpst_queue_request(struct ds *string,
1119 const struct ofp_queue_stats_request *qsr)
1121 ds_put_cstr(string, "port=");
1122 ofp_print_port_name(string, ntohs(qsr->port_no));
1124 ds_put_cstr(string, " queue=");
1125 ofp_print_queue_name(string, ntohl(qsr->queue_id));
1129 ofp_print_ofpst_queue_reply(struct ds *string, const struct ofp_header *oh,
1132 const struct ofp_queue_stats *qs = ofputil_stats_body(oh);
1133 size_t n = ofputil_stats_body_len(oh) / sizeof *qs;
1134 ds_put_format(string, " %zu queues\n", n);
1135 if (verbosity < 1) {
1140 ds_put_cstr(string, " port ");
1141 ofp_print_port_name(string, ntohs(qs->port_no));
1142 ds_put_cstr(string, " queue ");
1143 ofp_print_queue_name(string, ntohl(qs->queue_id));
1144 ds_put_cstr(string, ": ");
1146 print_port_stat(string, "bytes=", &qs->tx_bytes, 1);
1147 print_port_stat(string, "pkts=", &qs->tx_packets, 1);
1148 print_port_stat(string, "errors=", &qs->tx_errors, 0);
1153 ofp_print_stats_request(struct ds *string, const struct ofp_header *oh)
1155 const struct ofp_stats_msg *srq = (const struct ofp_stats_msg *) oh;
1158 ds_put_format(string, " ***unknown flags 0x%04"PRIx16"***",
1164 ofp_print_stats_reply(struct ds *string, const struct ofp_header *oh)
1166 const struct ofp_stats_msg *srp = (const struct ofp_stats_msg *) oh;
1169 uint16_t flags = ntohs(srp->flags);
1171 ds_put_cstr(string, " flags=");
1172 if (flags & OFPSF_REPLY_MORE) {
1173 ds_put_cstr(string, "[more]");
1174 flags &= ~OFPSF_REPLY_MORE;
1177 ds_put_format(string, "[***unknown flags 0x%04"PRIx16"***]",
1184 ofp_print_echo(struct ds *string, const struct ofp_header *oh, int verbosity)
1186 size_t len = ntohs(oh->length);
1188 ds_put_format(string, " %zu bytes of payload\n", len - sizeof *oh);
1189 if (verbosity > 1) {
1190 ds_put_hex_dump(string, oh + 1, len - sizeof *oh, 0, true);
1195 ofp_print_nxt_role_message(struct ds *string,
1196 const struct nx_role_request *nrr)
1198 unsigned int role = ntohl(nrr->role);
1200 ds_put_cstr(string, " role=");
1201 if (role == NX_ROLE_OTHER) {
1202 ds_put_cstr(string, "other");
1203 } else if (role == NX_ROLE_MASTER) {
1204 ds_put_cstr(string, "master");
1205 } else if (role == NX_ROLE_SLAVE) {
1206 ds_put_cstr(string, "slave");
1208 ds_put_format(string, "%u", role);
1213 ofp_print_nxt_flow_mod_table_id(struct ds *string,
1214 const struct nxt_flow_mod_table_id *nfmti)
1216 ds_put_format(string, " %s", nfmti->set ? "enable" : "disable");
1220 ofp_print_nxt_set_flow_format(struct ds *string,
1221 const struct nxt_set_flow_format *nsff)
1223 uint32_t format = ntohl(nsff->format);
1225 ds_put_cstr(string, " format=");
1226 if (ofputil_flow_format_is_valid(format)) {
1227 ds_put_cstr(string, ofputil_flow_format_to_string(format));
1229 ds_put_format(string, "%"PRIu32, format);
1234 ofp_to_string__(const struct ofp_header *oh,
1235 const struct ofputil_msg_type *type, struct ds *string,
1238 enum ofputil_msg_code code;
1239 const void *msg = oh;
1241 ds_put_format(string, "%s (xid=0x%"PRIx32"):",
1242 ofputil_msg_type_name(type), ntohl(oh->xid));
1244 code = ofputil_msg_type_code(type);
1246 case OFPUTIL_MSG_INVALID:
1249 case OFPUTIL_OFPT_HELLO:
1250 ds_put_char(string, '\n');
1251 ds_put_hex_dump(string, oh + 1, ntohs(oh->length) - sizeof *oh,
1255 case OFPUTIL_OFPT_ERROR:
1256 ofp_print_error_msg(string, msg);
1259 case OFPUTIL_OFPT_ECHO_REQUEST:
1260 case OFPUTIL_OFPT_ECHO_REPLY:
1261 ofp_print_echo(string, oh, verbosity);
1264 case OFPUTIL_OFPT_FEATURES_REQUEST:
1267 case OFPUTIL_OFPT_FEATURES_REPLY:
1268 ofp_print_switch_features(string, msg);
1271 case OFPUTIL_OFPT_GET_CONFIG_REQUEST:
1274 case OFPUTIL_OFPT_GET_CONFIG_REPLY:
1275 case OFPUTIL_OFPT_SET_CONFIG:
1276 ofp_print_switch_config(string, msg);
1279 case OFPUTIL_OFPT_PACKET_IN:
1280 ofp_print_packet_in(string, msg, verbosity);
1283 case OFPUTIL_OFPT_FLOW_REMOVED:
1284 case OFPUTIL_NXT_FLOW_REMOVED:
1285 ofp_print_flow_removed(string, msg);
1288 case OFPUTIL_OFPT_PORT_STATUS:
1289 ofp_print_port_status(string, msg);
1292 case OFPUTIL_OFPT_PACKET_OUT:
1293 ofp_print_packet_out(string, msg, verbosity);
1296 case OFPUTIL_OFPT_FLOW_MOD:
1297 ofp_print_flow_mod(string, msg, code, verbosity);
1300 case OFPUTIL_OFPT_PORT_MOD:
1301 ofp_print_port_mod(string, msg);
1304 case OFPUTIL_OFPT_BARRIER_REQUEST:
1305 case OFPUTIL_OFPT_BARRIER_REPLY:
1308 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REQUEST:
1309 case OFPUTIL_OFPT_QUEUE_GET_CONFIG_REPLY:
1313 case OFPUTIL_OFPST_DESC_REQUEST:
1314 ofp_print_stats_request(string, oh);
1317 case OFPUTIL_OFPST_FLOW_REQUEST:
1318 case OFPUTIL_NXST_FLOW_REQUEST:
1319 case OFPUTIL_OFPST_AGGREGATE_REQUEST:
1320 case OFPUTIL_NXST_AGGREGATE_REQUEST:
1321 ofp_print_stats_request(string, oh);
1322 ofp_print_flow_stats_request(string, msg);
1325 case OFPUTIL_OFPST_TABLE_REQUEST:
1326 ofp_print_stats_request(string, oh);
1329 case OFPUTIL_OFPST_PORT_REQUEST:
1330 ofp_print_stats_request(string, oh);
1331 ofp_print_ofpst_port_request(string, msg);
1334 case OFPUTIL_OFPST_QUEUE_REQUEST:
1335 ofp_print_stats_request(string, oh);
1336 ofp_print_ofpst_queue_request(string, msg);
1339 case OFPUTIL_OFPST_DESC_REPLY:
1340 ofp_print_stats_reply(string, oh);
1341 ofp_print_ofpst_desc_reply(string, msg);
1344 case OFPUTIL_OFPST_FLOW_REPLY:
1345 case OFPUTIL_NXST_FLOW_REPLY:
1346 ofp_print_stats_reply(string, oh);
1347 ofp_print_flow_stats_reply(string, oh);
1350 case OFPUTIL_OFPST_QUEUE_REPLY:
1351 ofp_print_stats_reply(string, oh);
1352 ofp_print_ofpst_queue_reply(string, oh, verbosity);
1355 case OFPUTIL_OFPST_PORT_REPLY:
1356 ofp_print_stats_reply(string, oh);
1357 ofp_print_ofpst_port_reply(string, oh, verbosity);
1360 case OFPUTIL_OFPST_TABLE_REPLY:
1361 ofp_print_stats_reply(string, oh);
1362 ofp_print_ofpst_table_reply(string, oh, verbosity);
1365 case OFPUTIL_OFPST_AGGREGATE_REPLY:
1366 ofp_print_stats_reply(string, oh);
1367 ofp_print_ofpst_aggregate_reply(string, msg);
1370 case OFPUTIL_NXT_ROLE_REQUEST:
1371 case OFPUTIL_NXT_ROLE_REPLY:
1372 ofp_print_nxt_role_message(string, msg);
1375 case OFPUTIL_NXT_FLOW_MOD_TABLE_ID:
1376 ofp_print_nxt_flow_mod_table_id(string, msg);
1379 case OFPUTIL_NXT_SET_FLOW_FORMAT:
1380 ofp_print_nxt_set_flow_format(string, msg);
1383 case OFPUTIL_NXT_FLOW_MOD:
1384 ofp_print_flow_mod(string, msg, code, verbosity);
1387 case OFPUTIL_NXST_AGGREGATE_REPLY:
1388 ofp_print_stats_reply(string, oh);
1389 ofp_print_nxst_aggregate_reply(string, msg);
1394 /* Composes and returns a string representing the OpenFlow packet of 'len'
1395 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
1396 * verbosity and higher numbers increase verbosity. The caller is responsible
1397 * for freeing the string. */
1399 ofp_to_string(const void *oh_, size_t len, int verbosity)
1401 struct ds string = DS_EMPTY_INITIALIZER;
1402 const struct ofp_header *oh = oh_;
1405 ds_put_cstr(&string, "OpenFlow message is empty\n");
1406 } else if (len < sizeof(struct ofp_header)) {
1407 ds_put_format(&string, "OpenFlow packet too short (only %zu bytes):\n",
1409 } else if (oh->version != OFP_VERSION) {
1410 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n",
1412 } else if (ntohs(oh->length) > len) {
1413 ds_put_format(&string,
1414 "(***truncated to %zu bytes from %"PRIu16"***)\n",
1415 len, ntohs(oh->length));
1416 } else if (ntohs(oh->length) < len) {
1417 ds_put_format(&string,
1418 "(***only uses %"PRIu16" bytes out of %zu***)\n",
1419 ntohs(oh->length), len);
1421 const struct ofputil_msg_type *type;
1424 error = ofputil_decode_msg_type(oh, &type);
1426 ofp_to_string__(oh, type, &string, verbosity);
1427 if (verbosity >= 5) {
1428 if (ds_last(&string) != '\n') {
1429 ds_put_char(&string, '\n');
1431 ds_put_hex_dump(&string, oh, len, 0, true);
1433 if (ds_last(&string) != '\n') {
1434 ds_put_char(&string, '\n');
1436 return ds_steal_cstr(&string);
1439 ofp_print_error(&string, error);
1441 ds_put_hex_dump(&string, oh, len, 0, true);
1442 return ds_steal_cstr(&string);
1445 /* Returns the name for the specified OpenFlow message type as a string,
1446 * e.g. "OFPT_FEATURES_REPLY". If no name is known, the string returned is a
1447 * hex number, e.g. "0x55".
1449 * The caller must free the returned string when it is no longer needed. */
1451 ofp_message_type_to_string(uint8_t type)
1462 case OFPT_ECHO_REQUEST:
1463 name = "ECHO_REQUEST";
1465 case OFPT_ECHO_REPLY:
1466 name = "ECHO_REPLY";
1471 case OFPT_FEATURES_REQUEST:
1472 name = "FEATURES_REQUEST";
1474 case OFPT_FEATURES_REPLY:
1475 name = "FEATURES_REPLY";
1477 case OFPT_GET_CONFIG_REQUEST:
1478 name = "GET_CONFIG_REQUEST";
1480 case OFPT_GET_CONFIG_REPLY:
1481 name = "GET_CONFIG_REPLY";
1483 case OFPT_SET_CONFIG:
1484 name = "SET_CONFIG";
1486 case OFPT_PACKET_IN:
1489 case OFPT_FLOW_REMOVED:
1490 name = "FLOW_REMOVED";
1492 case OFPT_PORT_STATUS:
1493 name = "PORT_STATUS";
1495 case OFPT_PACKET_OUT:
1496 name = "PACKET_OUT";
1504 case OFPT_STATS_REQUEST:
1505 name = "STATS_REQUEST";
1507 case OFPT_STATS_REPLY:
1508 name = "STATS_REPLY";
1510 case OFPT_BARRIER_REQUEST:
1511 name = "BARRIER_REQUEST";
1513 case OFPT_BARRIER_REPLY:
1514 name = "BARRIER_REPLY";
1516 case OFPT_QUEUE_GET_CONFIG_REQUEST:
1517 name = "QUEUE_GET_CONFIG_REQUEST";
1519 case OFPT_QUEUE_GET_CONFIG_REPLY:
1520 name = "QUEUE_GET_CONFIG_REPLY";
1527 return name ? xasprintf("OFPT_%s", name) : xasprintf("0x%02"PRIx8, type);
1531 print_and_free(FILE *stream, char *string)
1533 fputs(string, stream);
1537 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
1538 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
1539 * numbers increase verbosity. */
1541 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
1543 print_and_free(stream, ofp_to_string(oh, len, verbosity));
1546 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
1547 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
1548 * the Ethernet frame (of which 'len' bytes were captured).
1550 * This starts and kills a tcpdump subprocess so it's quite expensive. */
1552 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
1554 print_and_free(stream, ofp_packet_to_string(data, len, total_len));