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) {
138 /* Wait until there's something to do. */
139 for (i = 0; i < n_switches; i++) {
140 struct switch_ *this = switches[i];
143 if (vconn_is_passive(this->vconn)) {
144 want = n_switches < MAX_SWITCHES ? WANT_ACCEPT : 0;
152 this->pollfd = &pollfds[i];
153 this->pollfd->fd = -1;
154 this->pollfd->events = 0;
155 vconn_prepoll(this->vconn, want, this->pollfd);
158 pollfds[n_switches].fd = vlog_server_get_fd(vlog_server);
159 pollfds[n_switches].events = POLLIN;
162 retval = poll(pollfds, n_switches + (vlog_server != NULL), -1);
163 } while (retval < 0 && errno == EINTR);
165 fatal(retval < 0 ? errno : 0, "poll");
168 /* Let each connection deal with any pending operations. */
169 for (i = 0; i < n_switches; i++) {
170 struct switch_ *this = switches[i];
171 vconn_postpoll(this->vconn, &this->pollfd->revents);
172 if (this->pollfd->revents & POLLERR) {
173 this->pollfd->revents |= POLLIN | POLLOUT;
176 if (vlog_server && pollfds[n_switches].revents) {
177 vlog_server_poll(vlog_server);
180 for (i = 0; i < n_switches; ) {
181 struct switch_ *this = switches[i];
185 if (vconn_is_passive(this->vconn)) {
186 if (this->pollfd->revents & POLLIN) {
187 struct vconn *new_vconn;
188 while (n_switches < MAX_SWITCHES
189 && (retval = vconn_accept(this->vconn,
191 switches[n_switches++] = new_switch("tcp",
196 bool may_read = this->pollfd->revents & POLLIN;
197 bool may_write = this->pollfd->revents & POLLOUT;
199 retval = do_switch_recv(this);
200 if (!retval || retval == EAGAIN) {
203 /* Enable writing to avoid round trip through poll
208 while ((!retval || retval == EAGAIN) && may_write) {
209 retval = do_switch_send(this);
214 if (retval && retval != EAGAIN) {
216 switches[i] = switches[--n_switches];
220 /* New switch that hasn't been polled yet. */
230 do_switch_recv(struct switch_ *this)
235 retval = vconn_recv(this->vconn, &msg);
237 process_packet(this, msg);
244 do_switch_send(struct switch_ *this)
248 struct buffer *next = this->txq->next;
250 retval = vconn_send(this->vconn, this->txq);
256 if (this->txq == NULL) {
257 this->tx_tail = NULL;
266 connect_switch(const char *name)
271 retval = vconn_open(name, &vconn);
273 VLOG_ERR("%s: connect: %s", name, strerror(retval));
277 return new_switch(name, vconn);
280 static struct switch_ *
281 new_switch(const char *name, struct vconn *vconn)
283 struct switch_ *this = xmalloc(sizeof *this);
284 memset(this, 0, sizeof *this);
285 this->name = xstrdup(name);
290 this->tx_tail = NULL;
291 this->last_control_hello = 0;
292 if (!vconn_is_passive(vconn)) {
293 send_control_hello(this);
299 close_switch(struct switch_ *this)
302 struct buffer *cur, *next;
305 vconn_close(this->vconn);
306 for (cur = this->txq; cur != NULL; cur = next) {
315 send_control_hello(struct switch_ *this)
317 time_t now = time(0);
318 if (now >= this->last_control_hello + 1) {
320 struct ofp_control_hello *och;
323 och = buffer_put_uninit(b, sizeof *och);
324 memset(och, 0, sizeof *och);
325 och->header.version = OFP_VERSION;
326 och->header.length = htons(sizeof *och);
328 och->version = htonl(OFP_VERSION);
329 och->flags = htons(OFP_CHELLO_SEND_FLOW_EXP);
330 och->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
333 this->last_control_hello = now;
338 check_txq(struct switch_ *this UNUSED)
344 assert(this->n_txq == 0
345 ? this->txq == NULL && this->tx_tail == NULL
346 : this->txq != NULL && this->tx_tail != NULL);
349 for (iter = this->txq; iter != NULL; iter = iter->next) {
351 assert((iter->next != NULL) == (iter != this->tx_tail));
353 assert(n == this->n_txq);
358 queue_tx(struct switch_ *this, struct buffer *b)
364 this->tx_tail->next = b;
374 process_packet(struct switch_ *sw, struct buffer *msg)
376 static const size_t min_size[UINT8_MAX + 1] = {
377 [0 ... UINT8_MAX] = SIZE_MAX,
378 [OFPT_CONTROL_HELLO] = sizeof (struct ofp_control_hello),
379 [OFPT_DATA_HELLO] = sizeof (struct ofp_data_hello),
380 [OFPT_PACKET_IN] = offsetof (struct ofp_packet_in, data),
381 [OFPT_PACKET_OUT] = sizeof (struct ofp_packet_out),
382 [OFPT_FLOW_MOD] = sizeof (struct ofp_flow_mod),
383 [OFPT_FLOW_EXPIRED] = sizeof (struct ofp_flow_expired),
384 [OFPT_TABLE] = sizeof (struct ofp_table),
385 [OFPT_PORT_MOD] = sizeof (struct ofp_port_mod),
386 [OFPT_PORT_STATUS] = sizeof (struct ofp_port_status),
387 [OFPT_FLOW_STAT_REQUEST] = sizeof (struct ofp_flow_stat_request),
388 [OFPT_FLOW_STAT_REPLY] = sizeof (struct ofp_flow_stat_reply),
390 struct ofp_header *oh;
393 if (msg->size < min_size[oh->type]) {
394 VLOG_WARN("%s: too short (%zu bytes) for type %"PRIu8" (min %zu)",
395 sw->name, msg->size, oh->type, min_size[oh->type]);
399 if (oh->type == OFPT_DATA_HELLO) {
400 struct ofp_data_hello *odh = msg->data;
401 sw->datapath_id = odh->datapath_id;
402 } else if (sw->datapath_id == 0) {
403 send_control_hello(sw);
407 if (oh->type == OFPT_PACKET_IN) {
408 if (sw->n_txq >= MAX_TXQ) {
409 VLOG_WARN("%s: tx queue overflow", sw->name);
411 process_noflow(sw, msg->data);
413 process_hub(sw, msg->data);
415 process_switch(sw, msg->data);
420 ofp_print(stdout, msg->data, msg->size, 2);
424 process_hub(struct switch_ *sw, struct ofp_packet_in *opi)
426 size_t pkt_ofs, pkt_len;
430 /* Extract flow data from 'opi' into 'flow'. */
431 pkt_ofs = offsetof(struct ofp_packet_in, data);
432 pkt_len = ntohs(opi->header.length) - pkt_ofs;
433 pkt.data = opi->data;
435 flow_extract(&pkt, ntohs(opi->in_port), &flow);
438 queue_tx(sw, make_add_simple_flow(&flow, ntohl(opi->buffer_id),
441 /* If the switch didn't buffer the packet, we need to send a copy. */
442 if (ntohl(opi->buffer_id) == UINT32_MAX) {
443 queue_tx(sw, make_unbuffered_packet_out(&pkt, ntohs(flow.in_port),
449 process_noflow(struct switch_ *sw, struct ofp_packet_in *opi)
451 /* If the switch didn't buffer the packet, we need to send a copy. */
452 if (ntohl(opi->buffer_id) == UINT32_MAX) {
453 size_t pkt_ofs, pkt_len;
456 /* Extract flow data from 'opi' into 'flow'. */
457 pkt_ofs = offsetof(struct ofp_packet_in, data);
458 pkt_len = ntohs(opi->header.length) - pkt_ofs;
459 pkt.data = opi->data;
462 queue_tx(sw, make_unbuffered_packet_out(&pkt, ntohs(opi->in_port),
465 queue_tx(sw, make_buffered_packet_out(ntohl(opi->buffer_id),
466 ntohs(opi->in_port), OFPP_FLOOD));
471 #define MAC_HASH_BITS 10
472 #define MAC_HASH_MASK (MAC_HASH_SIZE - 1)
473 #define MAC_HASH_SIZE (1u << MAC_HASH_BITS)
478 struct list hash_list;
479 struct list lru_list;
480 uint64_t datapath_id;
481 uint8_t mac[ETH_ADDR_LEN];
485 static struct list mac_table[MAC_HASH_SIZE];
486 static struct list lrus;
487 static size_t mac_count;
495 for (i = 0; i < MAC_HASH_SIZE; i++) {
496 list_init(&mac_table[i]);
501 mac_table_bucket(uint64_t datapath_id, const uint8_t mac[ETH_ADDR_LEN])
504 hash = hash_fnv(&datapath_id, sizeof datapath_id, HASH_FNV_BASIS);
505 hash = hash_fnv(mac, ETH_ADDR_LEN, hash);
506 return &mac_table[hash & MAC_HASH_BITS];
510 process_switch(struct switch_ *sw, struct ofp_packet_in *opi)
512 size_t pkt_ofs, pkt_len;
518 /* Extract flow data from 'opi' into 'flow'. */
519 pkt_ofs = offsetof(struct ofp_packet_in, data);
520 pkt_len = ntohs(opi->header.length) - pkt_ofs;
521 pkt.data = opi->data;
523 flow_extract(&pkt, ntohs(opi->in_port), &flow);
525 /* Learn the source. */
526 if (!mac_is_multicast(flow.dl_src)) {
527 struct mac_source *src;
531 bucket = mac_table_bucket(sw->datapath_id, flow.dl_src);
533 LIST_FOR_EACH (src, struct mac_source, hash_list, bucket) {
534 if (src->datapath_id == sw->datapath_id
535 && mac_equals(src->mac, flow.dl_src)) {
542 /* Learn a new address. */
544 if (mac_count >= MAC_MAX) {
545 /* Drop the least recently used mac source. */
546 struct mac_source *lru;
547 lru = CONTAINER_OF(lrus.next, struct mac_source, lru_list);
548 list_remove(&lru->hash_list);
549 list_remove(&lru->lru_list);
555 /* Create new mac source */
556 src = xmalloc(sizeof *src);
557 src->datapath_id = sw->datapath_id;
558 memcpy(src->mac, flow.dl_src, ETH_ADDR_LEN);
560 list_push_front(bucket, &src->hash_list);
561 list_push_back(&lrus, &src->lru_list);
563 /* Make 'src' most-recently-used. */
564 list_remove(&src->lru_list);
565 list_push_back(&lrus, &src->lru_list);
568 if (ntohs(flow.in_port) != src->port) {
569 src->port = ntohs(flow.in_port);
570 VLOG_DBG("learned that "MAC_FMT" is on datapath %"PRIx64" port %d",
571 MAC_ARGS(src->mac), ntohll(src->datapath_id),
575 VLOG_DBG("multicast packet source "MAC_FMT, MAC_ARGS(flow.dl_src));
578 /* Figure out the destination. */
579 out_port = OFPP_FLOOD;
580 if (!mac_is_multicast(flow.dl_dst)) {
581 struct mac_source *dst;
584 bucket = mac_table_bucket(sw->datapath_id, flow.dl_dst);
585 LIST_FOR_EACH (dst, struct mac_source, hash_list, bucket) {
586 if (dst->datapath_id == sw->datapath_id
587 && mac_equals(dst->mac, flow.dl_dst)) {
588 out_port = dst->port;
594 if (out_port != OFPP_FLOOD) {
595 /* The output port is known, so add a new flow. */
596 queue_tx(sw, make_add_simple_flow(&flow, ntohl(opi->buffer_id),
599 /* If the switch didn't buffer the packet, we need to send a copy. */
600 if (ntohl(opi->buffer_id) == UINT32_MAX) {
601 queue_tx(sw, make_unbuffered_packet_out(&pkt, ntohs(flow.in_port),
605 /* We don't know that MAC. Flood the packet. */
607 if (ntohl(opi->buffer_id) == UINT32_MAX) {
608 b = make_unbuffered_packet_out(&pkt, ntohs(flow.in_port), out_port);
610 b = make_buffered_packet_out(ntohl(opi->buffer_id),
611 ntohs(flow.in_port), out_port);
618 parse_options(int argc, char *argv[])
620 static struct option long_options[] = {
621 {"hub", no_argument, 0, 'H'},
622 {"noflow", no_argument, 0, 'n'},
623 {"verbose", optional_argument, 0, 'v'},
624 {"help", no_argument, 0, 'h'},
625 {"version", no_argument, 0, 'V'},
628 char *short_options = long_options_to_short_options(long_options);
634 c = getopt_long(argc, argv, short_options, long_options, &indexptr);
652 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
656 vlog_set_verbosity(optarg);
672 printf("%s: OpenFlow controller\n"
673 "usage: %s [OPTIONS] VCONN\n"
674 "where VCONN is one of the following:\n"
676 " nl:DP_IDX via netlink to local datapath DP_IDX\n"
678 " ptcp:[PORT] listen to TCP PORT (default: %d)\n"
680 " -H, --hub act as hub instead of learning switch\n"
681 " -n, --noflow pass traffic, but don't add flows\n"
682 " -v, --verbose set maximum verbosity level\n"
683 " -h, --help display this help message\n"
684 " -V, --version display version information\n",
685 program_name, program_name, OFP_TCP_PORT);