1 /* Copyright (c) 2008 The Board of Trustees of The Leland Stanford
4 * We are making the OpenFlow specification and associated documentation
5 * (Software) available for public use and benefit with the expectation
6 * that others will use, modify and enhance the Software and contribute
7 * those enhancements back to the community. However, since we would
8 * like to make the Software available for broadest use, with as few
9 * restrictions as possible permission is hereby granted, free of
10 * charge, to any person obtaining a copy of this Software to deal in
11 * the Software under the copyrights without restriction, including
12 * without limitation the rights to use, copy, modify, merge, publish,
13 * distribute, sublicense, and/or sell copies of the Software, and to
14 * permit persons to whom the Software is furnished to do so, subject to
15 * the following conditions:
17 * The above copyright notice and this permission notice shall be
18 * included in all copies or substantial portions of the Software.
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
24 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
25 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
26 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 * The name and trademarks of copyright holder(s) may NOT be used in
30 * advertising or publicity pertaining to the Software or any
31 * derivatives without specific, written prior permission.
34 #include "ofp-print.h"
39 #include <netinet/in.h>
51 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
52 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
53 * the Ethernet frame (of which 'len' bytes were captured).
55 * This starts and kills a tcpdump subprocess so it's quite expensive. */
56 void ofp_print_packet(FILE *stream, const void *data, size_t len,
60 uint32_t magic_number; /* magic number */
61 uint16_t version_major; /* major version number */
62 uint16_t version_minor; /* minor version number */
63 int32_t thiszone; /* GMT to local correction */
64 uint32_t sigfigs; /* accuracy of timestamps */
65 uint32_t snaplen; /* max length of captured packets */
66 uint32_t network; /* data link type */
70 uint32_t ts_sec; /* timestamp seconds */
71 uint32_t ts_usec; /* timestamp microseconds */
72 uint32_t incl_len; /* number of octets of packet saved in file */
73 uint32_t orig_len; /* actual length of packet */
77 struct pcaprec_hdr prh;
84 snprintf(command, sizeof command, "tcpdump -n -r - %d>&1 2>/dev/null",
86 tcpdump = popen(command, "w");
88 error(errno, "exec(\"%s\")", command);
92 /* The pcap reader is responsible for figuring out endianness based on the
93 * magic number, so the lack of htonX calls here is intentional. */
94 ph.magic_number = 0xa1b2c3d4;
100 ph.network = 1; /* Ethernet */
105 prh.orig_len = total_len;
107 fwrite(&ph, 1, sizeof ph, tcpdump);
108 fwrite(&prh, 1, sizeof prh, tcpdump);
109 fwrite(data, 1, len, tcpdump);
113 error(errno, "error writing \"%s\" subprocess", command);
115 status = pclose(tcpdump);
116 if (WIFEXITED(status)) {
117 if (WEXITSTATUS(status))
118 error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
119 } else if (WIFSIGNALED(status)) {
120 error(0, "tcpdump exited with signal %d", WTERMSIG(status));
124 /* Pretty-print the OFPT_PACKET_IN packet of 'len' bytes at 'oh' to 'stream'
125 * at the given 'verbosity' level. */
126 static void ofp_packet_in(FILE *stream, const void *oh, size_t len,
129 const struct ofp_packet_in *op = oh;
132 fprintf(stream, " total_len=%"PRIu16" in_port=%"PRIu8,
133 ntohs(op->total_len), ntohs(op->in_port));
135 if (op->reason == OFPR_ACTION)
136 fputs(" (via action)", stream);
137 else if (op->reason != OFPR_NO_MATCH)
138 fprintf(stream, " (***reason %"PRIu8"***)", op->reason);
140 data_len = len - offsetof(struct ofp_packet_in, data);
141 fprintf(stream, " data_len=%zu", data_len);
142 if (htonl(op->buffer_id) == UINT32_MAX) {
143 fprintf(stream, " (unbuffered)");
144 if (ntohs(op->total_len) != data_len)
145 fprintf(stream, " (***total_len != data_len***)");
147 fprintf(stream, " buffer=%08"PRIx32, ntohl(op->buffer_id));
148 if (ntohs(op->total_len) < data_len)
149 fprintf(stream, " (***total_len < data_len***)");
154 ofp_print_packet(stream, op->data, data_len, ntohs(op->total_len));
157 static void ofp_print_port_name(FILE *stream, uint16_t port)
159 if (port == UINT16_MAX) {
160 fputs("none", stream);
161 } else if (port == OFPP_FLOOD) {
162 fputs("flood", stream);
163 } else if (port == OFPP_CONTROLLER) {
164 fputs("controller", stream);
166 fprintf(stream, "%"PRIu16, port);
170 static void ofp_print_action(FILE *stream, const struct ofp_action *a)
172 switch (ntohs(a->type)) {
174 fputs("output(", stream);
175 ofp_print_port_name(stream, ntohs(a->arg.output.port));
176 if (a->arg.output.port == htons(OFPP_CONTROLLER)) {
177 fprintf(stream, ", max %"PRIu16" bytes", ntohs(a->arg.output.max_len));
183 fprintf(stream, "(decoder %"PRIu16" not implemented)", ntohs(a->type));
188 static void ofp_print_actions(FILE *stream,
189 const struct ofp_action actions[],
194 fputs(" actions[", stream);
195 for (i = 0; i < n_bytes / sizeof *actions; i++) {
199 ofp_print_action(stream, &actions[i]);
201 if (n_bytes % sizeof *actions) {
205 fputs("; ***trailing garbage***", stream);
210 /* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'stream'
211 * at the given 'verbosity' level. */
212 static void ofp_packet_out(FILE *stream, const void *oh, size_t len,
215 const struct ofp_packet_out *opo = oh;
217 fputs(" in_port=", stream);
218 ofp_print_port_name(stream, ntohs(opo->in_port));
220 if (ntohl(opo->buffer_id) == UINT32_MAX) {
221 fputs(" out_port=", stream);
222 ofp_print_port_name(stream, ntohs(opo->out_port));
223 if (verbosity > 0 && len > sizeof *opo) {
224 ofp_print_packet(stream, opo->u.data, len - sizeof *opo,
228 fprintf(stream, " buffer=%08"PRIx32, ntohl(opo->buffer_id));
229 ofp_print_actions(stream, opo->u.actions, len - sizeof *opo);
234 /* qsort comparison function. */
236 compare_ports(const void *a_, const void *b_)
238 const struct ofp_phy_port *a = a_;
239 const struct ofp_phy_port *b = b_;
240 uint16_t ap = ntohs(a->port_no);
241 uint16_t bp = ntohs(b->port_no);
243 return ap < bp ? -1 : ap > bp;
247 void ofp_print_phy_port(FILE *stream, const struct ofp_phy_port *port)
249 uint8_t name[OFP_MAX_PORT_NAME_LEN];
252 memcpy(name, port->name, sizeof name);
253 for (j = 0; j < sizeof name - 1; j++) {
254 if (!isprint(name[j])) {
260 fprintf(stream, " %2d(%s): addr:"ETH_ADDR_FMT", speed:%d, flags:%#x, "
261 "feat:%#x\n", ntohs(port->port_no), name,
262 ETH_ADDR_ARGS(port->hw_addr), ntohl(port->speed),
263 ntohl(port->flags), ntohl(port->features));
266 /* Pretty-print the OFPT_DATA_HELLO packet of 'len' bytes at 'oh' to 'stream'
267 * at the given 'verbosity' level. */
268 void ofp_print_data_hello(FILE *stream, const void *oh, size_t len,
271 const struct ofp_data_hello *odh = oh;
272 struct ofp_phy_port port_list[OFPP_MAX];
277 fprintf(stream, "dp id:%"PRIx64"\n", ntohll(odh->datapath_id));
278 fprintf(stream, "tables: exact:%d, mac:%d, compressed:%d, general:%d\n",
279 ntohl(odh->n_exact), ntohl(odh->n_mac_only),
280 ntohl(odh->n_compression), ntohl(odh->n_general));
281 fprintf(stream, "buffers: size:%d, number:%d, miss_len:%d\n",
282 ntohl(odh->buffer_mb), ntohl(odh->n_buffers),
283 ntohs(odh->miss_send_len));
284 fprintf(stream, "features: capabilities:%#x, actions:%#x\n",
285 ntohl(odh->capabilities), ntohl(odh->actions));
287 if (ntohs(odh->header.length) >= sizeof *odh) {
288 len = MIN(len, ntohs(odh->header.length));
290 n_ports = (len - sizeof *odh) / sizeof *odh->ports;
292 memcpy(port_list, odh->ports, (len - sizeof *odh));
293 qsort(port_list, n_ports, sizeof port_list[0], compare_ports);
294 for (i = 0; i < n_ports; i++) {
295 ofp_print_phy_port(stream, &port_list[i]);
299 static void print_wild(FILE *stream, const char *leader, int is_wild,
300 const char *format, ...) __attribute__((format(printf, 4, 5)));
302 static void print_wild(FILE *stream, const char *leader, int is_wild,
303 const char *format, ...)
305 fputs(leader, stream);
309 va_start(args, format);
310 vfprintf(stream, format, args);
317 /* Pretty-print the ofp_match structure */
318 static void ofp_print_match(FILE *f, const struct ofp_match *om)
320 uint16_t w = ntohs(om->wildcards);
322 print_wild(f, "inport", w & OFPFW_IN_PORT, "%04x", ntohs(om->in_port));
323 print_wild(f, ":vlan", w & OFPFW_DL_VLAN, "%04x", ntohs(om->dl_vlan));
324 print_wild(f, " mac[", w & OFPFW_DL_SRC,
325 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
326 print_wild(f, "->", w & OFPFW_DL_DST,
327 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
328 print_wild(f, "] type", w & OFPFW_DL_TYPE, "%04x", ntohs(om->dl_type));
329 print_wild(f, " ip[", w & OFPFW_NW_SRC, IP_FMT, IP_ARGS(&om->nw_src));
330 print_wild(f, "->", w & OFPFW_NW_DST, IP_FMT, IP_ARGS(&om->nw_dst));
331 print_wild(f, "] proto", w & OFPFW_NW_PROTO, "%u", om->nw_proto);
332 print_wild(f, " tport[", w & OFPFW_TP_SRC, "%d", ntohs(om->tp_src));
333 print_wild(f, "->", w & OFPFW_TP_DST, "%d", ntohs(om->tp_dst));
337 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'stream'
338 * at the given 'verbosity' level. */
339 void ofp_print_flow_mod(FILE *stream, const void *oh, size_t len,
342 const struct ofp_flow_mod *ofm = oh;
344 ofp_print_match(stream, &ofm->match);
345 fprintf(stream, " cmd:%d idle:%d buf:%#x grp:%d\n", ntohs(ofm->command),
346 ntohs(ofm->max_idle), ntohl(ofm->buffer_id), ntohl(ofm->group_id));
349 /* Pretty-print the OFPT_FLOW_EXPIRED packet of 'len' bytes at 'oh' to 'stream'
350 * at the given 'verbosity' level. */
351 void ofp_print_flow_expired(FILE *stream, const void *oh, size_t len,
354 const struct ofp_flow_expired *ofe = oh;
356 ofp_print_match(stream, &ofe->match);
358 " secs%d pkts%lld bytes%lld\n", ntohl(ofe->duration),
359 ntohll(ofe->packet_count), ntohll(ofe->byte_count));
362 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'stream'
363 * at the given 'verbosity' level. */
364 void ofp_print_port_status(FILE *stream, const void *oh, size_t len,
367 const struct ofp_port_status *ops = oh;
369 if (ops->reason == OFPPR_ADD) {
370 fprintf(stream, "add:");
371 } else if (ops->reason == OFPPR_DELETE) {
372 fprintf(stream, "del:");
373 } else if (ops->reason == OFPPR_MOD) {
374 fprintf(stream, "mod:");
376 fprintf(stream, "err:");
379 ofp_print_phy_port(stream, &ops->desc);
382 struct openflow_packet {
385 void (*printer)(FILE *, const void *, size_t len, int verbosity);
388 static const struct openflow_packet packets[] = {
389 [OFPT_CONTROL_HELLO] = {
391 sizeof (struct ofp_control_hello),
394 [OFPT_DATA_HELLO] = {
396 sizeof (struct ofp_data_hello),
397 ofp_print_data_hello,
401 offsetof(struct ofp_packet_in, data),
404 [OFPT_PACKET_OUT] = {
406 sizeof (struct ofp_packet_out),
411 sizeof (struct ofp_flow_mod),
414 [OFPT_FLOW_EXPIRED] = {
416 sizeof (struct ofp_flow_expired),
417 ofp_print_flow_expired,
421 sizeof (struct ofp_port_mod),
424 [OFPT_PORT_STATUS] = {
426 sizeof (struct ofp_port_status),
427 ofp_print_port_status
431 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
432 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
433 * numbers increase verbosity. */
434 void ofp_print(FILE *stream, const void *oh_, size_t len, int verbosity)
436 const struct ofp_header *oh = oh_;
437 const struct openflow_packet *pkt;
439 if (len < sizeof(struct ofp_header)) {
440 fprintf(stream, "OpenFlow packet too short:\n");
441 hex_dump(stream, oh, len, 0, true);
443 } else if (oh->version != 1) {
444 fprintf(stream, "Bad OpenFlow version %"PRIu8":\n", oh->version);
445 hex_dump(stream, oh, len, 0, true);
447 } else if (oh->type >= ARRAY_SIZE(packets) || !packets[oh->type].name) {
448 fprintf(stream, "Unknown OpenFlow packet type %"PRIu8":\n",
450 hex_dump(stream, oh, len, 0, true);
454 pkt = &packets[oh->type];
455 fprintf(stream, "%s (xid=%"PRIx32"):", pkt->name, oh->xid);
457 if (ntohs(oh->length) > len)
458 fprintf(stream, " (***truncated to %zu bytes from %"PRIu16"***)",
459 len, ntohs(oh->length));
460 else if (ntohs(oh->length) < len) {
461 fprintf(stream, " (***only uses %"PRIu16" bytes out of %zu***)\n",
462 ntohs(oh->length), len);
463 len = ntohs(oh->length);
466 if (len < pkt->min_size) {
467 fprintf(stream, " (***length=%zu < min_size=%zu***)\n",
469 } else if (!pkt->printer) {
470 fprintf(stream, " length=%zu (decoder not implemented)\n",
473 pkt->printer(stream, oh, len, verbosity);
476 hex_dump(stream, oh, len, 0, true);
479 /* Pretty print a openflow table */
480 void ofp_print_table(FILE *stream, const struct ofp_table* ot)
482 fprintf(stream, "id: %d name: %-8s n_flows: %6d max_flows: %6d",
483 ntohs(ot->table_id), ot->name, ntohl(ot->n_flows),
484 ntohl(ot->max_flows));