1 /* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
3 * Permission is hereby granted, free of charge, to any person obtaining a copy
4 * of this software and associated documentation files (the "Software"), to
5 * deal in the Software without restriction, including without limitation the
6 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7 * sell copies of the Software, and to permit persons to whom the Software is
8 * furnished to do so, subject to the following conditions:
10 * The above copyright notice and this permission notice shall be included in
11 * all copies or substantial portions of the Software.
13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 #include "ofp-print.h"
27 #include <netinet/in.h>
39 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
40 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
41 * the Ethernet frame (of which 'len' bytes were captured).
43 * This starts and kills a tcpdump subprocess so it's quite expensive. */
44 void ofp_print_packet(FILE *stream, const void *data, size_t len,
48 uint32_t magic_number; /* magic number */
49 uint16_t version_major; /* major version number */
50 uint16_t version_minor; /* minor version number */
51 int32_t thiszone; /* GMT to local correction */
52 uint32_t sigfigs; /* accuracy of timestamps */
53 uint32_t snaplen; /* max length of captured packets */
54 uint32_t network; /* data link type */
58 uint32_t ts_sec; /* timestamp seconds */
59 uint32_t ts_usec; /* timestamp microseconds */
60 uint32_t incl_len; /* number of octets of packet saved in file */
61 uint32_t orig_len; /* actual length of packet */
65 struct pcaprec_hdr prh;
72 snprintf(command, sizeof command, "tcpdump -n -r - %d>&1 2>/dev/null",
74 tcpdump = popen(command, "w");
76 error(errno, "exec(\"%s\")", command);
80 /* The pcap reader is responsible for figuring out endianness based on the
81 * magic number, so the lack of htonX calls here is intentional. */
82 ph.magic_number = 0xa1b2c3d4;
88 ph.network = 1; /* Ethernet */
93 prh.orig_len = total_len;
95 fwrite(&ph, 1, sizeof ph, tcpdump);
96 fwrite(&prh, 1, sizeof prh, tcpdump);
97 fwrite(data, 1, len, tcpdump);
101 error(errno, "error writing \"%s\" subprocess", command);
103 status = pclose(tcpdump);
104 if (WIFEXITED(status)) {
105 if (WEXITSTATUS(status))
106 error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
107 } else if (WIFSIGNALED(status)) {
108 error(0, "tcpdump exited with signal %d", WTERMSIG(status));
112 /* Pretty-print the OFPT_PACKET_IN packet of 'len' bytes at 'oh' to 'stream'
113 * at the given 'verbosity' level. */
114 static void ofp_packet_in(FILE *stream, const void *oh, size_t len,
117 const struct ofp_packet_in *op = oh;
120 fprintf(stream, " total_len=%"PRIu16" in_port=%"PRIu8,
121 ntohs(op->total_len), ntohs(op->in_port));
123 if (op->reason == OFPR_ACTION)
124 fputs(" (via action)", stream);
125 else if (op->reason != OFPR_NO_MATCH)
126 fprintf(stream, " (***reason %"PRIu8"***)", op->reason);
128 data_len = len - offsetof(struct ofp_packet_in, data);
129 fprintf(stream, " data_len=%zu", data_len);
130 if (htonl(op->buffer_id) == UINT32_MAX) {
131 fprintf(stream, " (unbuffered)");
132 if (ntohs(op->total_len) != data_len)
133 fprintf(stream, " (***total_len != data_len***)");
135 fprintf(stream, " buffer=%08"PRIx32, ntohl(op->buffer_id));
136 if (ntohs(op->total_len) < data_len)
137 fprintf(stream, " (***total_len < data_len***)");
142 ofp_print_packet(stream, op->data, data_len, ntohs(op->total_len));
145 static void ofp_print_port_name(FILE *stream, uint16_t port)
147 if (port == UINT16_MAX) {
148 fputs("none", stream);
149 } else if (port == OFPP_FLOOD) {
150 fputs("flood", stream);
151 } else if (port == OFPP_CONTROLLER) {
152 fputs("controller", stream);
154 fprintf(stream, "%"PRIu16, port);
158 static void ofp_print_action(FILE *stream, const struct ofp_action *a)
160 switch (ntohs(a->type)) {
162 fputs("output(", stream);
163 ofp_print_port_name(stream, ntohs(a->arg.output.port));
164 if (a->arg.output.port == htons(OFPP_CONTROLLER)) {
165 fprintf(stream, ", max %"PRIu16" bytes", ntohs(a->arg.output.max_len));
171 fprintf(stream, "(decoder %"PRIu16" not implemented)", ntohs(a->type));
176 static void ofp_print_actions(FILE *stream,
177 const struct ofp_action actions[],
182 fputs(" actions[", stream);
183 for (i = 0; i < n_bytes / sizeof *actions; i++) {
187 ofp_print_action(stream, &actions[i]);
189 if (n_bytes % sizeof *actions) {
193 fputs("; ***trailing garbage***", stream);
198 /* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'stream'
199 * at the given 'verbosity' level. */
200 static void ofp_packet_out(FILE *stream, const void *oh, size_t len,
203 const struct ofp_packet_out *opo = oh;
205 fputs(" in_port=", stream);
206 ofp_print_port_name(stream, ntohs(opo->in_port));
208 if (ntohl(opo->buffer_id) == UINT32_MAX) {
209 fputs(" out_port=", stream);
210 ofp_print_port_name(stream, ntohs(opo->out_port));
211 if (verbosity > 0 && len > sizeof *opo) {
212 ofp_print_packet(stream, opo->u.data, len - sizeof *opo,
216 fprintf(stream, " buffer=%08"PRIx32, ntohl(opo->buffer_id));
217 ofp_print_actions(stream, opo->u.actions, len - sizeof *opo);
222 /* qsort comparison function. */
224 compare_ports(const void *a_, const void *b_)
226 const struct ofp_phy_port *a = a_;
227 const struct ofp_phy_port *b = b_;
228 uint16_t ap = ntohs(a->port_no);
229 uint16_t bp = ntohs(b->port_no);
231 return ap < bp ? -1 : ap > bp;
235 void ofp_print_phy_port(FILE *stream, const struct ofp_phy_port *port)
237 uint8_t name[OFP_MAX_PORT_NAME_LEN];
240 memcpy(name, port->name, sizeof name);
241 for (j = 0; j < sizeof name - 1; j++) {
242 if (!isprint(name[j])) {
248 fprintf(stream, " %2d(%s): addr:"MAC_FMT", speed:%d, flags:%#x, "
249 "feat:%#x\n", ntohs(port->port_no), name,
250 MAC_ARGS(port->hw_addr), ntohl(port->speed), ntohl(port->flags),
251 ntohl(port->features));
254 /* Pretty-print the OFPT_DATA_HELLO packet of 'len' bytes at 'oh' to 'stream'
255 * at the given 'verbosity' level. */
256 void ofp_print_data_hello(FILE *stream, const void *oh, size_t len,
259 const struct ofp_data_hello *odh = oh;
260 struct ofp_phy_port port_list[OFPP_MAX];
265 fprintf(stream, "dp id:%"PRIx64"\n", ntohll(odh->datapath_id));
266 fprintf(stream, "tables: exact:%d, mac:%d, compressed:%d, general:%d\n",
267 ntohl(odh->n_exact), ntohl(odh->n_mac_only),
268 ntohl(odh->n_compression), ntohl(odh->n_general));
269 fprintf(stream, "buffers: size:%d, number:%d, miss_len:%d\n",
270 ntohl(odh->buffer_mb), ntohl(odh->n_buffers),
271 ntohs(odh->miss_send_len));
272 fprintf(stream, "features: capabilities:%#x, actions:%#x\n",
273 ntohl(odh->capabilities), ntohl(odh->actions));
275 if (ntohs(odh->header.length) >= sizeof *odh) {
276 len = MIN(len, ntohs(odh->header.length));
278 n_ports = (len - sizeof *odh) / sizeof *odh->ports;
280 memcpy(port_list, odh->ports, (len - sizeof *odh));
281 qsort(port_list, n_ports, sizeof port_list[0], compare_ports);
282 for (i = 0; i < n_ports; i++) {
283 ofp_print_phy_port(stream, &port_list[i]);
287 static void print_wild(FILE *stream, const char *leader, int is_wild,
288 const char *format, ...) __attribute__((format(printf, 4, 5)));
290 static void print_wild(FILE *stream, const char *leader, int is_wild,
291 const char *format, ...)
293 fputs(leader, stream);
297 va_start(args, format);
298 vfprintf(stream, format, args);
305 /* Pretty-print the ofp_match structure */
306 static void ofp_print_match(FILE *f, const struct ofp_match *om)
308 uint16_t w = ntohs(om->wildcards);
310 print_wild(f, "inport", w & OFPFW_IN_PORT, "%04x", ntohs(om->in_port));
311 print_wild(f, ":vlan", w & OFPFW_DL_VLAN, "%04x", ntohs(om->dl_vlan));
312 print_wild(f, " mac[", w & OFPFW_DL_SRC, MAC_FMT, MAC_ARGS(om->dl_src));
313 print_wild(f, "->", w & OFPFW_DL_DST, MAC_FMT, MAC_ARGS(om->dl_dst));
314 print_wild(f, "] type", w & OFPFW_DL_TYPE, "%04x", ntohs(om->dl_type));
315 print_wild(f, " ip[", w & OFPFW_NW_SRC, IP_FMT, IP_ARGS(&om->nw_src));
316 print_wild(f, "->", w & OFPFW_NW_DST, IP_FMT, IP_ARGS(&om->nw_dst));
317 print_wild(f, "] proto", w & OFPFW_NW_PROTO, "%u", om->nw_proto);
318 print_wild(f, " tport[", w & OFPFW_TP_SRC, "%d", ntohs(om->tp_src));
319 print_wild(f, "->", w & OFPFW_TP_DST, "%d", ntohs(om->tp_dst));
323 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'stream'
324 * at the given 'verbosity' level. */
325 void ofp_print_flow_mod(FILE *stream, const void *oh, size_t len,
328 const struct ofp_flow_mod *ofm = oh;
330 ofp_print_match(stream, &ofm->match);
331 fprintf(stream, " cmd:%d idle:%d buf:%#x grp:%d\n", ntohs(ofm->command),
332 ntohs(ofm->max_idle), ntohl(ofm->buffer_id), ntohl(ofm->group_id));
335 /* Pretty-print the OFPT_FLOW_EXPIRED packet of 'len' bytes at 'oh' to 'stream'
336 * at the given 'verbosity' level. */
337 void ofp_print_flow_expired(FILE *stream, const void *oh, size_t len,
340 const struct ofp_flow_expired *ofe = oh;
342 ofp_print_match(stream, &ofe->match);
344 " secs%d pkts%lld bytes%lld\n", ntohl(ofe->duration),
345 ntohll(ofe->packet_count), ntohll(ofe->byte_count));
348 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'stream'
349 * at the given 'verbosity' level. */
350 void ofp_print_port_status(FILE *stream, const void *oh, size_t len,
353 const struct ofp_port_status *ops = oh;
355 if (ops->reason == OFPPR_ADD) {
356 fprintf(stream, "add:");
357 } else if (ops->reason == OFPPR_DELETE) {
358 fprintf(stream, "del:");
359 } else if (ops->reason == OFPPR_MOD) {
360 fprintf(stream, "mod:");
362 fprintf(stream, "err:");
365 ofp_print_phy_port(stream, &ops->desc);
368 struct openflow_packet {
371 void (*printer)(FILE *, const void *, size_t len, int verbosity);
374 static const struct openflow_packet packets[] = {
375 [OFPT_CONTROL_HELLO] = {
377 sizeof (struct ofp_control_hello),
380 [OFPT_DATA_HELLO] = {
382 sizeof (struct ofp_data_hello),
383 ofp_print_data_hello,
387 offsetof(struct ofp_packet_in, data),
390 [OFPT_PACKET_OUT] = {
392 sizeof (struct ofp_packet_out),
397 sizeof (struct ofp_flow_mod),
400 [OFPT_FLOW_EXPIRED] = {
402 sizeof (struct ofp_flow_expired),
403 ofp_print_flow_expired,
407 sizeof (struct ofp_port_mod),
410 [OFPT_PORT_STATUS] = {
412 sizeof (struct ofp_port_status),
413 ofp_print_port_status
417 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
418 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
419 * numbers increase verbosity. */
420 void ofp_print(FILE *stream, const void *oh_, size_t len, int verbosity)
422 const struct ofp_header *oh = oh_;
423 const struct openflow_packet *pkt;
425 if (len < sizeof(struct ofp_header)) {
426 fprintf(stream, "OpenFlow packet too short:\n");
427 hex_dump(stream, oh, len, 0, true);
429 } else if (oh->version != 1) {
430 fprintf(stream, "Bad OpenFlow version %"PRIu8":\n", oh->version);
431 hex_dump(stream, oh, len, 0, true);
433 } else if (oh->type >= ARRAY_SIZE(packets) || !packets[oh->type].name) {
434 fprintf(stream, "Unknown OpenFlow packet type %"PRIu8":\n",
436 hex_dump(stream, oh, len, 0, true);
440 pkt = &packets[oh->type];
441 fprintf(stream, "%s (xid=%"PRIx32"):", pkt->name, oh->xid);
443 if (ntohs(oh->length) > len)
444 fprintf(stream, " (***truncated to %zu bytes from %"PRIu16"***)",
445 len, ntohs(oh->length));
446 else if (ntohs(oh->length) < len) {
447 fprintf(stream, " (***only uses %"PRIu16" bytes out of %zu***)\n",
448 ntohs(oh->length), len);
449 len = ntohs(oh->length);
452 if (len < pkt->min_size) {
453 fprintf(stream, " (***length=%zu < min_size=%zu***)\n",
455 } else if (!pkt->printer) {
456 fprintf(stream, " length=%zu (decoder not implemented)\n",
459 pkt->printer(stream, oh, len, verbosity);
462 hex_dump(stream, oh, len, 0, true);
465 /* Pretty print a openflow table */
466 void ofp_print_table(FILE *stream, const struct ofp_table* ot)
468 fprintf(stream, "id: %d name: %-8s n_flows: %6d max_flows: %6d",
469 ntohs(ot->table_id), ot->name, ntohl(ot->n_flows),
470 ntohl(ot->max_flows));