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>
46 #include "dynamic-string.h"
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)
63 uint32_t magic_number; /* magic number */
64 uint16_t version_major; /* major version number */
65 uint16_t version_minor; /* minor version number */
66 int32_t thiszone; /* GMT to local correction */
67 uint32_t sigfigs; /* accuracy of timestamps */
68 uint32_t snaplen; /* max length of captured packets */
69 uint32_t network; /* data link type */
73 uint32_t ts_sec; /* timestamp seconds */
74 uint32_t ts_usec; /* timestamp microseconds */
75 uint32_t incl_len; /* number of octets of packet saved in file */
76 uint32_t orig_len; /* actual length of packet */
80 struct pcaprec_hdr prh;
82 struct ds ds = DS_EMPTY_INITIALIZER;
92 error(errno, "tmpfile");
93 return xstrdup("<error>");
96 /* The pcap reader is responsible for figuring out endianness based on the
97 * magic number, so the lack of htonX calls here is intentional. */
98 ph.magic_number = 0xa1b2c3d4;
100 ph.version_minor = 4;
104 ph.network = 1; /* Ethernet */
109 prh.orig_len = total_len;
111 fwrite(&ph, 1, sizeof ph, pcap);
112 fwrite(&prh, 1, sizeof prh, pcap);
113 fwrite(data, 1, len, pcap);
117 error(errno, "error writing temporary file");
121 snprintf(command, sizeof command, "tcpdump -n -r /dev/fd/%d 2>/dev/null",
123 tcpdump = popen(command, "r");
126 error(errno, "exec(\"%s\")", command);
127 return xstrdup("<error>");
130 while ((c = getc(tcpdump)) != EOF) {
134 status = pclose(tcpdump);
135 if (WIFEXITED(status)) {
136 if (WEXITSTATUS(status))
137 error(0, "tcpdump exited with status %d", WEXITSTATUS(status));
138 } else if (WIFSIGNALED(status)) {
139 error(0, "tcpdump exited with signal %d", WTERMSIG(status));
144 /* Pretty-print the OFPT_PACKET_IN packet of 'len' bytes at 'oh' to 'stream'
145 * at the given 'verbosity' level. */
147 ofp_packet_in(struct ds *string, const void *oh, size_t len, int verbosity)
149 const struct ofp_packet_in *op = oh;
152 ds_put_format(string, " total_len=%"PRIu16" in_port=%"PRIu8,
153 ntohs(op->total_len), ntohs(op->in_port));
155 if (op->reason == OFPR_ACTION)
156 ds_put_cstr(string, " (via action)");
157 else if (op->reason != OFPR_NO_MATCH)
158 ds_put_format(string, " (***reason %"PRIu8"***)", op->reason);
160 data_len = len - offsetof(struct ofp_packet_in, data);
161 ds_put_format(string, " data_len=%zu", data_len);
162 if (htonl(op->buffer_id) == UINT32_MAX) {
163 ds_put_format(string, " (unbuffered)");
164 if (ntohs(op->total_len) != data_len)
165 ds_put_format(string, " (***total_len != data_len***)");
167 ds_put_format(string, " buffer=%08"PRIx32, ntohl(op->buffer_id));
168 if (ntohs(op->total_len) < data_len)
169 ds_put_format(string, " (***total_len < data_len***)");
171 ds_put_char(string, '\n');
174 char *packet = ofp_packet_to_string(op->data, data_len,
175 ntohs(op->total_len));
176 ds_put_cstr(string, packet);
181 static void ofp_print_port_name(struct ds *string, uint16_t port)
183 if (port == UINT16_MAX) {
184 ds_put_cstr(string, "none");
185 } else if (port == OFPP_FLOOD) {
186 ds_put_cstr(string, "flood");
187 } else if (port == OFPP_CONTROLLER) {
188 ds_put_cstr(string, "controller");
190 ds_put_format(string, "%"PRIu16, port);
195 ofp_print_action(struct ds *string, const struct ofp_action *a)
197 switch (ntohs(a->type)) {
199 ds_put_cstr(string, "output(");
200 ofp_print_port_name(string, ntohs(a->arg.output.port));
201 if (a->arg.output.port == htons(OFPP_CONTROLLER)) {
202 ds_put_format(string, ", max %"PRIu16" bytes", ntohs(a->arg.output.max_len));
204 ds_put_cstr(string, ")");
208 ds_put_format(string, "(decoder %"PRIu16" not implemented)", ntohs(a->type));
213 static void ofp_print_actions(struct ds *string,
214 const struct ofp_action actions[],
219 ds_put_cstr(string, " actions[");
220 for (i = 0; i < n_bytes / sizeof *actions; i++) {
222 ds_put_cstr(string, "; ");
224 ofp_print_action(string, &actions[i]);
226 if (n_bytes % sizeof *actions) {
228 ds_put_cstr(string, "; ");
230 ds_put_cstr(string, "; ***trailing garbage***");
232 ds_put_cstr(string, "]");
235 /* Pretty-print the OFPT_PACKET_OUT packet of 'len' bytes at 'oh' to 'string'
236 * at the given 'verbosity' level. */
237 static void ofp_packet_out(struct ds *string, const void *oh, size_t len,
240 const struct ofp_packet_out *opo = oh;
242 ds_put_cstr(string, " in_port=");
243 ofp_print_port_name(string, ntohs(opo->in_port));
245 if (ntohl(opo->buffer_id) == UINT32_MAX) {
246 ds_put_cstr(string, " out_port=");
247 ofp_print_port_name(string, ntohs(opo->out_port));
248 if (verbosity > 0 && len > sizeof *opo) {
249 char *packet = ofp_packet_to_string(opo->u.data, len - sizeof *opo,
251 ds_put_cstr(string, packet);
255 ds_put_format(string, " buffer=%08"PRIx32, ntohl(opo->buffer_id));
256 ofp_print_actions(string, opo->u.actions, len - sizeof *opo);
258 ds_put_char(string, '\n');
261 /* qsort comparison function. */
263 compare_ports(const void *a_, const void *b_)
265 const struct ofp_phy_port *a = a_;
266 const struct ofp_phy_port *b = b_;
267 uint16_t ap = ntohs(a->port_no);
268 uint16_t bp = ntohs(b->port_no);
270 return ap < bp ? -1 : ap > bp;
274 ofp_print_phy_port(struct ds *string, const struct ofp_phy_port *port)
276 uint8_t name[OFP_MAX_PORT_NAME_LEN];
279 memcpy(name, port->name, sizeof name);
280 for (j = 0; j < sizeof name - 1; j++) {
281 if (!isprint(name[j])) {
287 ds_put_format(string, " %2d(%s): addr:"ETH_ADDR_FMT", speed:%d, flags:%#x, "
288 "feat:%#x\n", ntohs(port->port_no), name,
289 ETH_ADDR_ARGS(port->hw_addr), ntohl(port->speed),
290 ntohl(port->flags), ntohl(port->features));
293 /* Pretty-print the struct ofp_switch_features of 'len' bytes at 'oh' to
294 * 'string' at the given 'verbosity' level. */
296 ofp_print_switch_features(struct ds *string, const void *oh, size_t len,
299 const struct ofp_switch_features *osf = oh;
300 struct ofp_phy_port port_list[OFPP_MAX];
304 ds_put_format(string, "dp id:%"PRIx64"\n", ntohll(osf->datapath_id));
305 ds_put_format(string, "tables: exact:%d, mac:%d, compressed:%d, general:%d\n",
306 ntohl(osf->n_exact), ntohl(osf->n_mac_only),
307 ntohl(osf->n_compression), ntohl(osf->n_general));
308 ds_put_format(string, "buffers: size:%d, number:%d\n",
309 ntohl(osf->buffer_mb), ntohl(osf->n_buffers));
310 ds_put_format(string, "features: capabilities:%#x, actions:%#x\n",
311 ntohl(osf->capabilities), ntohl(osf->actions));
313 if (ntohs(osf->header.length) >= sizeof *osf) {
314 len = MIN(len, ntohs(osf->header.length));
316 n_ports = (len - sizeof *osf) / sizeof *osf->ports;
318 memcpy(port_list, osf->ports, (len - sizeof *osf));
319 qsort(port_list, n_ports, sizeof port_list[0], compare_ports);
320 for (i = 0; i < n_ports; i++) {
321 ofp_print_phy_port(string, &port_list[i]);
325 /* Pretty-print the struct ofp_switch_config of 'len' bytes at 'oh' to 'string'
326 * at the given 'verbosity' level. */
328 ofp_print_switch_config(struct ds *string, const void *oh, size_t len,
331 const struct ofp_switch_config *osc = oh;
334 flags = ntohs(osc->flags);
335 if (flags & OFPC_SEND_FLOW_EXP) {
336 flags &= ~OFPC_SEND_FLOW_EXP;
337 ds_put_format(string, " (sending flow expirations)");
340 ds_put_format(string, " ***unknown flags %04"PRIx16"***", flags);
343 ds_put_format(string, " miss_send_len=%"PRIu16"\n", ntohs(osc->miss_send_len));
346 static void print_wild(struct ds *string, const char *leader, int is_wild,
347 const char *format, ...) __attribute__((format(printf, 4, 5)));
349 static void print_wild(struct ds *string, const char *leader, int is_wild,
350 const char *format, ...)
352 ds_put_cstr(string, leader);
356 va_start(args, format);
357 ds_put_format_valist(string, format, args);
360 ds_put_char(string, '?');
364 /* Pretty-print the ofp_match structure */
365 static void ofp_print_match(struct ds *f, const struct ofp_match *om)
367 uint16_t w = ntohs(om->wildcards);
369 print_wild(f, "inport", w & OFPFW_IN_PORT, "%04x", ntohs(om->in_port));
370 print_wild(f, ":vlan", w & OFPFW_DL_VLAN, "%04x", ntohs(om->dl_vlan));
371 print_wild(f, " mac[", w & OFPFW_DL_SRC,
372 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_src));
373 print_wild(f, "->", w & OFPFW_DL_DST,
374 ETH_ADDR_FMT, ETH_ADDR_ARGS(om->dl_dst));
375 print_wild(f, "] type", w & OFPFW_DL_TYPE, "%04x", ntohs(om->dl_type));
376 print_wild(f, " ip[", w & OFPFW_NW_SRC, IP_FMT, IP_ARGS(&om->nw_src));
377 print_wild(f, "->", w & OFPFW_NW_DST, IP_FMT, IP_ARGS(&om->nw_dst));
378 print_wild(f, "] proto", w & OFPFW_NW_PROTO, "%u", om->nw_proto);
379 print_wild(f, " tport[", w & OFPFW_TP_SRC, "%d", ntohs(om->tp_src));
380 print_wild(f, "->", w & OFPFW_TP_DST, "%d", ntohs(om->tp_dst));
381 ds_put_cstr(f, "]\n");
384 /* Pretty-print the OFPT_FLOW_MOD packet of 'len' bytes at 'oh' to 'string'
385 * at the given 'verbosity' level. */
387 ofp_print_flow_mod(struct ds *string, const void *oh, size_t len,
390 const struct ofp_flow_mod *ofm = oh;
392 ofp_print_match(string, &ofm->match);
393 ds_put_format(string, " cmd:%d idle:%d buf:%#x grp:%d\n", ntohs(ofm->command),
394 ntohs(ofm->max_idle), ntohl(ofm->buffer_id), ntohl(ofm->group_id));
397 /* Pretty-print the OFPT_FLOW_EXPIRED packet of 'len' bytes at 'oh' to 'string'
398 * at the given 'verbosity' level. */
399 void ofp_print_flow_expired(struct ds *string, const void *oh, size_t len,
402 const struct ofp_flow_expired *ofe = oh;
404 ofp_print_match(string, &ofe->match);
405 ds_put_format(string,
406 " secs%d pkts%lld bytes%lld\n", ntohl(ofe->duration),
407 ntohll(ofe->packet_count), ntohll(ofe->byte_count));
410 /* Pretty-print the OFPT_PORT_STATUS packet of 'len' bytes at 'oh' to 'string'
411 * at the given 'verbosity' level. */
412 void ofp_print_port_status(struct ds *string, const void *oh, size_t len,
415 const struct ofp_port_status *ops = oh;
417 if (ops->reason == OFPPR_ADD) {
418 ds_put_format(string, "add:");
419 } else if (ops->reason == OFPPR_DELETE) {
420 ds_put_format(string, "del:");
421 } else if (ops->reason == OFPPR_MOD) {
422 ds_put_format(string, "mod:");
424 ds_put_format(string, "err:");
427 ofp_print_phy_port(string, &ops->desc);
430 struct openflow_packet {
433 void (*printer)(struct ds *, const void *, size_t len, int verbosity);
436 static const struct openflow_packet packets[] = {
437 [OFPT_FEATURES_REQUEST] = {
439 sizeof (struct ofp_header),
442 [OFPT_FEATURES_REPLY] = {
444 sizeof (struct ofp_switch_features),
445 ofp_print_switch_features,
447 [OFPT_GET_CONFIG_REQUEST] = {
448 "get_config_request",
449 sizeof (struct ofp_header),
452 [OFPT_GET_CONFIG_REPLY] = {
454 sizeof (struct ofp_switch_config),
455 ofp_print_switch_config,
457 [OFPT_SET_CONFIG] = {
459 sizeof (struct ofp_switch_config),
460 ofp_print_switch_config,
464 offsetof(struct ofp_packet_in, data),
467 [OFPT_PACKET_OUT] = {
469 sizeof (struct ofp_packet_out),
474 sizeof (struct ofp_flow_mod),
477 [OFPT_FLOW_EXPIRED] = {
479 sizeof (struct ofp_flow_expired),
480 ofp_print_flow_expired,
484 sizeof (struct ofp_port_mod),
487 [OFPT_PORT_STATUS] = {
489 sizeof (struct ofp_port_status),
490 ofp_print_port_status
494 /* Composes and returns a string representing the OpenFlow packet of 'len'
495 * bytes at 'oh' at the given 'verbosity' level. 0 is a minimal amount of
496 * verbosity and higher numbers increase verbosity. The caller is responsible
497 * for freeing the string. */
499 ofp_to_string(const void *oh_, size_t len, int verbosity)
501 struct ds string = DS_EMPTY_INITIALIZER;
502 const struct ofp_header *oh = oh_;
503 const struct openflow_packet *pkt;
505 if (len < sizeof(struct ofp_header)) {
506 ds_put_cstr(&string, "OpenFlow packet too short:\n");
507 ds_put_hex_dump(&string, oh, len, 0, true);
508 return ds_cstr(&string);
509 } else if (oh->version != 1) {
510 ds_put_format(&string, "Bad OpenFlow version %"PRIu8":\n", oh->version);
511 ds_put_hex_dump(&string, oh, len, 0, true);
512 return ds_cstr(&string);
513 } else if (oh->type >= ARRAY_SIZE(packets) || !packets[oh->type].name) {
514 ds_put_format(&string, "Unknown OpenFlow packet type %"PRIu8":\n",
516 ds_put_hex_dump(&string, oh, len, 0, true);
517 return ds_cstr(&string);
520 pkt = &packets[oh->type];
521 ds_put_format(&string, "%s (xid=%"PRIx32"):", pkt->name, oh->xid);
523 if (ntohs(oh->length) > len)
524 ds_put_format(&string, " (***truncated to %zu bytes from %"PRIu16"***)",
525 len, ntohs(oh->length));
526 else if (ntohs(oh->length) < len) {
527 ds_put_format(&string, " (***only uses %"PRIu16" bytes out of %zu***)\n",
528 ntohs(oh->length), len);
529 len = ntohs(oh->length);
532 if (len < pkt->min_size) {
533 ds_put_format(&string, " (***length=%zu < min_size=%zu***)\n",
535 } else if (!pkt->printer) {
536 ds_put_format(&string, " length=%zu (decoder not implemented)\n",
539 pkt->printer(&string, oh, len, verbosity);
541 if (verbosity >= 3) {
542 ds_put_hex_dump(&string, oh, len, 0, true);
544 return ds_cstr(&string);
548 ofp_table_to_string(const struct ofp_table* ot)
550 return xasprintf("id: %d name: %-8s n_flows: %6d max_flows: %6d",
551 ntohs(ot->table_id), ot->name, ntohl(ot->n_flows),
552 ntohl(ot->max_flows));
556 print_and_free(FILE *stream, char *string)
558 fputs(string, stream);
562 /* Pretty-print the OpenFlow packet of 'len' bytes at 'oh' to 'stream' at the
563 * given 'verbosity' level. 0 is a minimal amount of verbosity and higher
564 * numbers increase verbosity. */
566 ofp_print(FILE *stream, const void *oh, size_t len, int verbosity)
568 print_and_free(stream, ofp_to_string(oh, len, verbosity));
571 /* Pretty print a openflow table */
573 ofp_print_table(FILE *stream, const struct ofp_table *ot)
575 print_and_free(stream, ofp_table_to_string(ot));
578 /* Dumps the contents of the Ethernet frame in the 'len' bytes starting at
579 * 'data' to 'stream' using tcpdump. 'total_len' specifies the full length of
580 * the Ethernet frame (of which 'len' bytes were captured).
582 * This starts and kills a tcpdump subprocess so it's quite expensive. */
584 ofp_print_packet(FILE *stream, const void *data, size_t len, size_t total_len)
586 print_and_free(stream, ofp_packet_to_string(data, len, total_len));