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
26 #include <netinet/in.h>
33 #include "command-line.h"
40 #include "ofp-print.h"
45 #include "vlog-socket.h"
49 #define THIS_MODULE VLM_controller
51 #define MAX_SWITCHES 16
57 struct pollfd *pollfd;
60 time_t last_control_hello;
63 struct buffer *txq, *tx_tail;
66 /* -H, --hub: Use dumb hub instead of learning switch? */
67 static bool hub = false;
69 /* -n, --noflow: Pass traffic, but don't setup flows in switch */
70 static bool noflow = false;
72 static void parse_options(int argc, char *argv[]);
73 static void usage(void) NO_RETURN;
75 static struct switch_ *connect_switch(const char *name);
76 static struct switch_ *new_switch(const char *name, struct vconn *);
77 static void close_switch(struct switch_ *);
79 static void queue_tx(struct switch_ *, struct buffer *);
81 static void send_control_hello(struct switch_ *);
83 static int do_switch_recv(struct switch_ *this);
84 static int do_switch_send(struct switch_ *this);
86 static void process_packet(struct switch_ *, struct buffer *);
87 static void process_hub(struct switch_ *, struct ofp_packet_in *);
88 static void process_noflow(struct switch_ *, struct ofp_packet_in *);
90 static void switch_init(void);
91 static void process_switch(struct switch_ *, struct ofp_packet_in *);
94 main(int argc, char *argv[])
96 struct switch_ *switches[MAX_SWITCHES];
97 struct pollfd pollfds[MAX_SWITCHES + 1];
98 struct vlog_server *vlog_server;
103 set_program_name(argv[0]);
104 register_fault_handlers();
106 parse_options(argc, argv);
108 if (!hub && !noflow) {
112 if (argc - optind < 1) {
113 fatal(0, "at least one vconn argument required; use --help for usage");
116 retval = vlog_server_listen(NULL, &vlog_server);
118 fatal(retval, "Could not listen for vlog connections");
122 for (i = 0; i < argc - optind; i++) {
123 struct switch_ *this = connect_switch(argv[optind + i]);
125 if (n_switches >= MAX_SWITCHES) {
126 fatal(0, "max %d switch connections", n_switches);
128 switches[n_switches++] = this;
131 if (n_switches == 0) {
132 fatal(0, "could not connect to any switches");
135 while (n_switches > 0) {
139 /* Wait until there's something to do. */
141 for (i = 0; i < n_switches; i++) {
142 struct switch_ *this = switches[i];
145 if (vconn_is_passive(this->vconn)) {
146 want = n_switches < MAX_SWITCHES ? WANT_ACCEPT : 0;
154 this->pollfd = &pollfds[i];
155 this->pollfd->fd = -1;
156 this->pollfd->events = 0;
157 n_ready += vconn_prepoll(this->vconn, want, this->pollfd);
160 pollfds[n_switches].fd = vlog_server_get_fd(vlog_server);
161 pollfds[n_switches].events = POLLIN;
164 retval = poll(pollfds, n_switches + (vlog_server != NULL),
166 } while (retval < 0 && errno == EINTR);
167 if (retval < 0 || (retval == 0 && !n_ready)) {
168 fatal(retval < 0 ? errno : 0, "poll");
171 /* Let each connection deal with any pending operations. */
172 for (i = 0; i < n_switches; i++) {
173 struct switch_ *this = switches[i];
174 vconn_postpoll(this->vconn, &this->pollfd->revents);
175 if (this->pollfd->revents & POLLERR) {
176 this->pollfd->revents |= POLLIN | POLLOUT;
179 if (vlog_server && pollfds[n_switches].revents) {
180 vlog_server_poll(vlog_server);
183 for (i = 0; i < n_switches; ) {
184 struct switch_ *this = switches[i];
188 if (vconn_is_passive(this->vconn)) {
189 if (this->pollfd->revents & POLLIN) {
190 struct vconn *new_vconn;
191 while (n_switches < MAX_SWITCHES
192 && (retval = vconn_accept(this->vconn,
194 switches[n_switches++] = new_switch("tcp",
199 bool may_read = this->pollfd->revents & POLLIN;
200 bool may_write = this->pollfd->revents & POLLOUT;
202 retval = do_switch_recv(this);
203 if (!retval || retval == EAGAIN) {
206 /* Enable writing to avoid round trip through poll
211 while ((!retval || retval == EAGAIN) && may_write) {
212 retval = do_switch_send(this);
217 if (retval && retval != EAGAIN) {
219 switches[i] = switches[--n_switches];
223 /* New switch that hasn't been polled yet. */
233 do_switch_recv(struct switch_ *this)
238 retval = vconn_recv(this->vconn, &msg);
240 process_packet(this, msg);
247 do_switch_send(struct switch_ *this)
251 struct buffer *next = this->txq->next;
253 retval = vconn_send(this->vconn, this->txq);
259 if (this->txq == NULL) {
260 this->tx_tail = NULL;
269 connect_switch(const char *name)
274 retval = vconn_open(name, &vconn);
276 VLOG_ERR("%s: connect: %s", name, strerror(retval));
280 return new_switch(name, vconn);
283 static struct switch_ *
284 new_switch(const char *name, struct vconn *vconn)
286 struct switch_ *this = xmalloc(sizeof *this);
287 memset(this, 0, sizeof *this);
288 this->name = xstrdup(name);
293 this->tx_tail = NULL;
294 this->last_control_hello = 0;
295 if (!vconn_is_passive(vconn)) {
296 send_control_hello(this);
302 close_switch(struct switch_ *this)
305 struct buffer *cur, *next;
308 vconn_close(this->vconn);
309 for (cur = this->txq; cur != NULL; cur = next) {
318 send_control_hello(struct switch_ *this)
320 time_t now = time(0);
321 if (now >= this->last_control_hello + 1) {
323 struct ofp_control_hello *och;
326 och = buffer_put_uninit(b, sizeof *och);
327 memset(och, 0, sizeof *och);
328 och->header.version = OFP_VERSION;
329 och->header.length = htons(sizeof *och);
331 och->version = htonl(OFP_VERSION);
332 och->flags = htons(OFP_CHELLO_SEND_FLOW_EXP);
333 och->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
336 this->last_control_hello = now;
341 check_txq(struct switch_ *this UNUSED)
347 assert(this->n_txq == 0
348 ? this->txq == NULL && this->tx_tail == NULL
349 : this->txq != NULL && this->tx_tail != NULL);
352 for (iter = this->txq; iter != NULL; iter = iter->next) {
354 assert((iter->next != NULL) == (iter != this->tx_tail));
356 assert(n == this->n_txq);
361 queue_tx(struct switch_ *this, struct buffer *b)
367 this->tx_tail->next = b;
377 process_packet(struct switch_ *sw, struct buffer *msg)
379 static const size_t min_size[UINT8_MAX + 1] = {
380 [0 ... UINT8_MAX] = SIZE_MAX,
381 [OFPT_CONTROL_HELLO] = sizeof (struct ofp_control_hello),
382 [OFPT_DATA_HELLO] = sizeof (struct ofp_data_hello),
383 [OFPT_PACKET_IN] = offsetof (struct ofp_packet_in, data),
384 [OFPT_PACKET_OUT] = sizeof (struct ofp_packet_out),
385 [OFPT_FLOW_MOD] = sizeof (struct ofp_flow_mod),
386 [OFPT_FLOW_EXPIRED] = sizeof (struct ofp_flow_expired),
387 [OFPT_TABLE] = sizeof (struct ofp_table),
388 [OFPT_PORT_MOD] = sizeof (struct ofp_port_mod),
389 [OFPT_PORT_STATUS] = sizeof (struct ofp_port_status),
390 [OFPT_FLOW_STAT_REQUEST] = sizeof (struct ofp_flow_stat_request),
391 [OFPT_FLOW_STAT_REPLY] = sizeof (struct ofp_flow_stat_reply),
393 struct ofp_header *oh;
396 if (msg->size < min_size[oh->type]) {
397 VLOG_WARN("%s: too short (%zu bytes) for type %"PRIu8" (min %zu)",
398 sw->name, msg->size, oh->type, min_size[oh->type]);
402 if (oh->type == OFPT_DATA_HELLO) {
403 struct ofp_data_hello *odh = msg->data;
404 sw->datapath_id = odh->datapath_id;
405 } else if (sw->datapath_id == 0) {
406 send_control_hello(sw);
410 if (oh->type == OFPT_PACKET_IN) {
411 if (sw->n_txq >= MAX_TXQ) {
412 VLOG_WARN("%s: tx queue overflow", sw->name);
414 process_noflow(sw, msg->data);
416 process_hub(sw, msg->data);
418 process_switch(sw, msg->data);
423 ofp_print(stdout, msg->data, msg->size, 2);
427 process_hub(struct switch_ *sw, struct ofp_packet_in *opi)
429 size_t pkt_ofs, pkt_len;
433 /* Extract flow data from 'opi' into 'flow'. */
434 pkt_ofs = offsetof(struct ofp_packet_in, data);
435 pkt_len = ntohs(opi->header.length) - pkt_ofs;
436 pkt.data = opi->data;
438 flow_extract(&pkt, ntohs(opi->in_port), &flow);
441 queue_tx(sw, make_add_simple_flow(&flow, ntohl(opi->buffer_id),
444 /* If the switch didn't buffer the packet, we need to send a copy. */
445 if (ntohl(opi->buffer_id) == UINT32_MAX) {
446 queue_tx(sw, make_unbuffered_packet_out(&pkt, ntohs(flow.in_port),
452 process_noflow(struct switch_ *sw, struct ofp_packet_in *opi)
454 /* If the switch didn't buffer the packet, we need to send a copy. */
455 if (ntohl(opi->buffer_id) == UINT32_MAX) {
456 size_t pkt_ofs, pkt_len;
459 /* Extract flow data from 'opi' into 'flow'. */
460 pkt_ofs = offsetof(struct ofp_packet_in, data);
461 pkt_len = ntohs(opi->header.length) - pkt_ofs;
462 pkt.data = opi->data;
465 queue_tx(sw, make_unbuffered_packet_out(&pkt, ntohs(opi->in_port),
468 queue_tx(sw, make_buffered_packet_out(ntohl(opi->buffer_id),
469 ntohs(opi->in_port), OFPP_FLOOD));
474 #define MAC_HASH_BITS 10
475 #define MAC_HASH_MASK (MAC_HASH_SIZE - 1)
476 #define MAC_HASH_SIZE (1u << MAC_HASH_BITS)
481 struct list hash_list;
482 struct list lru_list;
483 uint64_t datapath_id;
484 uint8_t mac[ETH_ADDR_LEN];
488 static struct list mac_table[MAC_HASH_SIZE];
489 static struct list lrus;
490 static size_t mac_count;
498 for (i = 0; i < MAC_HASH_SIZE; i++) {
499 list_init(&mac_table[i]);
504 mac_table_bucket(uint64_t datapath_id, const uint8_t mac[ETH_ADDR_LEN])
507 hash = hash_fnv(&datapath_id, sizeof datapath_id, HASH_FNV_BASIS);
508 hash = hash_fnv(mac, ETH_ADDR_LEN, hash);
509 return &mac_table[hash & MAC_HASH_BITS];
513 process_switch(struct switch_ *sw, struct ofp_packet_in *opi)
515 size_t pkt_ofs, pkt_len;
521 /* Extract flow data from 'opi' into 'flow'. */
522 pkt_ofs = offsetof(struct ofp_packet_in, data);
523 pkt_len = ntohs(opi->header.length) - pkt_ofs;
524 pkt.data = opi->data;
526 flow_extract(&pkt, ntohs(opi->in_port), &flow);
528 /* Learn the source. */
529 if (!mac_is_multicast(flow.dl_src)) {
530 struct mac_source *src;
534 bucket = mac_table_bucket(sw->datapath_id, flow.dl_src);
536 LIST_FOR_EACH (src, struct mac_source, hash_list, bucket) {
537 if (src->datapath_id == sw->datapath_id
538 && mac_equals(src->mac, flow.dl_src)) {
545 /* Learn a new address. */
547 if (mac_count >= MAC_MAX) {
548 /* Drop the least recently used mac source. */
549 struct mac_source *lru;
550 lru = CONTAINER_OF(lrus.next, struct mac_source, lru_list);
551 list_remove(&lru->hash_list);
552 list_remove(&lru->lru_list);
558 /* Create new mac source */
559 src = xmalloc(sizeof *src);
560 src->datapath_id = sw->datapath_id;
561 memcpy(src->mac, flow.dl_src, ETH_ADDR_LEN);
563 list_push_front(bucket, &src->hash_list);
564 list_push_back(&lrus, &src->lru_list);
566 /* Make 'src' most-recently-used. */
567 list_remove(&src->lru_list);
568 list_push_back(&lrus, &src->lru_list);
571 if (ntohs(flow.in_port) != src->port) {
572 src->port = ntohs(flow.in_port);
573 VLOG_DBG("learned that "MAC_FMT" is on datapath %"PRIx64" port %d",
574 MAC_ARGS(src->mac), ntohll(src->datapath_id),
578 VLOG_DBG("multicast packet source "MAC_FMT, MAC_ARGS(flow.dl_src));
581 /* Figure out the destination. */
582 out_port = OFPP_FLOOD;
583 if (!mac_is_multicast(flow.dl_dst)) {
584 struct mac_source *dst;
587 bucket = mac_table_bucket(sw->datapath_id, flow.dl_dst);
588 LIST_FOR_EACH (dst, struct mac_source, hash_list, bucket) {
589 if (dst->datapath_id == sw->datapath_id
590 && mac_equals(dst->mac, flow.dl_dst)) {
591 out_port = dst->port;
597 if (out_port != OFPP_FLOOD) {
598 /* The output port is known, so add a new flow. */
599 queue_tx(sw, make_add_simple_flow(&flow, ntohl(opi->buffer_id),
602 /* If the switch didn't buffer the packet, we need to send a copy. */
603 if (ntohl(opi->buffer_id) == UINT32_MAX) {
604 queue_tx(sw, make_unbuffered_packet_out(&pkt, ntohs(flow.in_port),
608 /* We don't know that MAC. Flood the packet. */
610 if (ntohl(opi->buffer_id) == UINT32_MAX) {
611 b = make_unbuffered_packet_out(&pkt, ntohs(flow.in_port), out_port);
613 b = make_buffered_packet_out(ntohl(opi->buffer_id),
614 ntohs(flow.in_port), out_port);
621 parse_options(int argc, char *argv[])
623 static struct option long_options[] = {
624 {"hub", no_argument, 0, 'H'},
625 {"noflow", no_argument, 0, 'n'},
626 {"verbose", optional_argument, 0, 'v'},
627 {"help", no_argument, 0, 'h'},
628 {"version", no_argument, 0, 'V'},
630 {"private-key", required_argument, 0, 'p'},
631 {"certificate", required_argument, 0, 'c'},
632 {"ca-cert", required_argument, 0, 'C'},
636 char *short_options = long_options_to_short_options(long_options);
642 c = getopt_long(argc, argv, short_options, long_options, &indexptr);
660 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
664 vlog_set_verbosity(optarg);
669 vconn_ssl_set_private_key_file(optarg);
673 vconn_ssl_set_certificate_file(optarg);
677 vconn_ssl_set_ca_cert_file(optarg);
694 printf("%s: OpenFlow controller\n"
695 "usage: %s [OPTIONS] VCONN\n"
696 "where VCONN is one of the following:\n"
697 " ptcp:[PORT] listen to TCP PORT (default: %d)\n",
698 program_name, program_name);
700 printf(" nl:DP_IDX via netlink to local datapath DP_IDX\n");
703 printf(" pssl:[PORT] listen for SSL on PORT (default: %d)\n"
704 "\nPKI configuration (required to use SSL):\n"
705 " -p, --private-key=FILE file with private key\n"
706 " -c, --certificate=FILE file with certificate for private key\n"
707 " -C, --ca-cert=FILE file with peer CA certificate\n",
710 printf("\nOther options:\n"
711 " -H, --hub act as hub instead of learning switch\n"
712 " -n, --noflow pass traffic, but don't add flows\n"
713 " -v, --verbose set maximum verbosity level\n"
714 " -h, --help display this help message\n"
715 " -V, --version display version information\n",