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.
38 #include <netinet/in.h>
45 #include "command-line.h"
52 #include "ofp-print.h"
54 #include "poll-loop.h"
58 #include "vconn-ssl.h"
60 #include "vlog-socket.h"
64 #define THIS_MODULE VLM_controller
66 #define MAX_SWITCHES 16
74 time_t last_control_hello;
79 /* -H, --hub: Use dumb hub instead of learning switch? */
80 static bool hub = false;
82 /* -n, --noflow: Pass traffic, but don't setup flows in switch */
83 static bool noflow = false;
85 static void parse_options(int argc, char *argv[]);
86 static void usage(void) NO_RETURN;
88 static struct switch_ *connect_switch(const char *name);
89 static struct switch_ *new_switch(const char *name, struct vconn *);
90 static void close_switch(struct switch_ *);
92 static void queue_tx(struct switch_ *, struct buffer *);
94 static void send_control_hello(struct switch_ *);
96 static int do_switch_recv(struct switch_ *this);
97 static int do_switch_send(struct switch_ *this);
99 static void process_packet(struct switch_ *, struct buffer *);
100 static void process_hub(struct switch_ *, struct ofp_packet_in *);
101 static void process_noflow(struct switch_ *, struct ofp_packet_in *);
103 static void switch_init(void);
104 static void process_switch(struct switch_ *, struct ofp_packet_in *);
107 main(int argc, char *argv[])
109 struct switch_ *switches[MAX_SWITCHES];
114 set_program_name(argv[0]);
115 register_fault_handlers();
117 parse_options(argc, argv);
119 if (!hub && !noflow) {
123 if (argc - optind < 1) {
124 fatal(0, "at least one vconn argument required; use --help for usage");
127 retval = vlog_server_listen(NULL, NULL);
129 fatal(retval, "Could not listen for vlog connections");
133 for (i = 0; i < argc - optind; i++) {
134 struct switch_ *this = connect_switch(argv[optind + i]);
136 if (n_switches >= MAX_SWITCHES) {
137 fatal(0, "max %d switch connections", n_switches);
139 switches[n_switches++] = this;
142 if (n_switches == 0) {
143 fatal(0, "could not connect to any switches");
146 while (n_switches > 0) {
147 /* Do some work. Limit the number of iterations so that callbacks
148 * registered with the poll loop don't starve. */
151 for (iteration = 0; iteration < 50; iteration++) {
152 bool progress = false;
153 for (i = 0; i < n_switches; ) {
154 struct switch_ *this = switches[i];
157 if (vconn_is_passive(this->vconn)) {
159 while (n_switches < MAX_SWITCHES) {
160 struct vconn *new_vconn;
161 retval = vconn_accept(this->vconn, &new_vconn);
165 switches[n_switches++] = new_switch("tcp", new_vconn);
168 retval = do_switch_recv(this);
169 if (!retval || retval == EAGAIN) {
171 retval = do_switch_send(this);
179 if (retval && retval != EAGAIN) {
181 switches[i] = switches[--n_switches];
191 /* Wait for something to happen. */
192 for (i = 0; i < n_switches; i++) {
193 struct switch_ *this = switches[i];
194 if (vconn_is_passive(this->vconn)) {
195 if (n_switches < MAX_SWITCHES) {
196 vconn_accept_wait(this->vconn);
199 vconn_recv_wait(this->vconn);
201 vconn_send_wait(this->vconn);
212 do_switch_recv(struct switch_ *this)
217 retval = vconn_recv(this->vconn, &msg);
219 process_packet(this, msg);
226 do_switch_send(struct switch_ *this)
230 struct buffer *next = this->txq.head->next;
231 retval = vconn_send(this->vconn, this->txq.head);
235 queue_advance_head(&this->txq, next);
242 connect_switch(const char *name)
247 retval = vconn_open(name, &vconn);
249 VLOG_ERR("%s: connect: %s", name, strerror(retval));
253 return new_switch(name, vconn);
256 static struct switch_ *
257 new_switch(const char *name, struct vconn *vconn)
259 struct switch_ *this = xmalloc(sizeof *this);
260 memset(this, 0, sizeof *this);
261 this->name = xstrdup(name);
263 queue_init(&this->txq);
264 this->last_control_hello = 0;
265 if (!vconn_is_passive(vconn)) {
266 send_control_hello(this);
272 close_switch(struct switch_ *this)
276 vconn_close(this->vconn);
277 queue_destroy(&this->txq);
283 send_control_hello(struct switch_ *this)
285 time_t now = time(0);
286 if (now >= this->last_control_hello + 1) {
288 struct ofp_control_hello *och;
291 och = buffer_put_uninit(b, sizeof *och);
292 memset(och, 0, sizeof *och);
293 och->header.version = OFP_VERSION;
294 och->header.length = htons(sizeof *och);
296 och->version = htonl(OFP_VERSION);
297 och->flags = htons(OFP_CHELLO_SEND_FLOW_EXP);
298 och->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
301 this->last_control_hello = now;
306 queue_tx(struct switch_ *this, struct buffer *b)
308 queue_push_tail(&this->txq, b);
312 process_packet(struct switch_ *sw, struct buffer *msg)
314 static const size_t min_size[UINT8_MAX + 1] = {
315 [0 ... UINT8_MAX] = SIZE_MAX,
316 [OFPT_CONTROL_HELLO] = sizeof (struct ofp_control_hello),
317 [OFPT_DATA_HELLO] = sizeof (struct ofp_data_hello),
318 [OFPT_PACKET_IN] = offsetof (struct ofp_packet_in, data),
319 [OFPT_PACKET_OUT] = sizeof (struct ofp_packet_out),
320 [OFPT_FLOW_MOD] = sizeof (struct ofp_flow_mod),
321 [OFPT_FLOW_EXPIRED] = sizeof (struct ofp_flow_expired),
322 [OFPT_TABLE] = sizeof (struct ofp_table),
323 [OFPT_PORT_MOD] = sizeof (struct ofp_port_mod),
324 [OFPT_PORT_STATUS] = sizeof (struct ofp_port_status),
325 [OFPT_FLOW_STAT_REQUEST] = sizeof (struct ofp_flow_stat_request),
326 [OFPT_FLOW_STAT_REPLY] = sizeof (struct ofp_flow_stat_reply),
328 struct ofp_header *oh;
331 if (msg->size < min_size[oh->type]) {
332 VLOG_WARN("%s: too short (%zu bytes) for type %"PRIu8" (min %zu)",
333 sw->name, msg->size, oh->type, min_size[oh->type]);
337 if (oh->type == OFPT_DATA_HELLO) {
338 struct ofp_data_hello *odh = msg->data;
339 sw->datapath_id = odh->datapath_id;
340 } else if (sw->datapath_id == 0) {
341 send_control_hello(sw);
345 if (oh->type == OFPT_PACKET_IN) {
346 if (sw->txq.n >= MAX_TXQ) {
347 /* FIXME: ratelimit. */
348 VLOG_WARN("%s: tx queue overflow", sw->name);
350 process_noflow(sw, msg->data);
352 process_hub(sw, msg->data);
354 process_switch(sw, msg->data);
359 ofp_print(stdout, msg->data, msg->size, 2);
363 process_hub(struct switch_ *sw, struct ofp_packet_in *opi)
365 size_t pkt_ofs, pkt_len;
369 /* Extract flow data from 'opi' into 'flow'. */
370 pkt_ofs = offsetof(struct ofp_packet_in, data);
371 pkt_len = ntohs(opi->header.length) - pkt_ofs;
372 pkt.data = opi->data;
374 flow_extract(&pkt, ntohs(opi->in_port), &flow);
377 queue_tx(sw, make_add_simple_flow(&flow, ntohl(opi->buffer_id),
380 /* If the switch didn't buffer the packet, we need to send a copy. */
381 if (ntohl(opi->buffer_id) == UINT32_MAX) {
382 queue_tx(sw, make_unbuffered_packet_out(&pkt, ntohs(flow.in_port),
388 process_noflow(struct switch_ *sw, struct ofp_packet_in *opi)
390 /* If the switch didn't buffer the packet, we need to send a copy. */
391 if (ntohl(opi->buffer_id) == UINT32_MAX) {
392 size_t pkt_ofs, pkt_len;
395 /* Extract flow data from 'opi' into 'flow'. */
396 pkt_ofs = offsetof(struct ofp_packet_in, data);
397 pkt_len = ntohs(opi->header.length) - pkt_ofs;
398 pkt.data = opi->data;
401 queue_tx(sw, make_unbuffered_packet_out(&pkt, ntohs(opi->in_port),
404 queue_tx(sw, make_buffered_packet_out(ntohl(opi->buffer_id),
405 ntohs(opi->in_port), OFPP_FLOOD));
410 #define MAC_HASH_BITS 10
411 #define MAC_HASH_MASK (MAC_HASH_SIZE - 1)
412 #define MAC_HASH_SIZE (1u << MAC_HASH_BITS)
417 struct list hash_list;
418 struct list lru_list;
419 uint64_t datapath_id;
420 uint8_t mac[ETH_ADDR_LEN];
424 static struct list mac_table[MAC_HASH_SIZE];
425 static struct list lrus;
426 static size_t mac_count;
434 for (i = 0; i < MAC_HASH_SIZE; i++) {
435 list_init(&mac_table[i]);
440 mac_table_bucket(uint64_t datapath_id, const uint8_t mac[ETH_ADDR_LEN])
443 hash = hash_fnv(&datapath_id, sizeof datapath_id, HASH_FNV_BASIS);
444 hash = hash_fnv(mac, ETH_ADDR_LEN, hash);
445 return &mac_table[hash & MAC_HASH_BITS];
449 process_switch(struct switch_ *sw, struct ofp_packet_in *opi)
451 size_t pkt_ofs, pkt_len;
457 /* Extract flow data from 'opi' into 'flow'. */
458 pkt_ofs = offsetof(struct ofp_packet_in, data);
459 pkt_len = ntohs(opi->header.length) - pkt_ofs;
460 pkt.data = opi->data;
462 flow_extract(&pkt, ntohs(opi->in_port), &flow);
464 /* Learn the source. */
465 if (!mac_is_multicast(flow.dl_src)) {
466 struct mac_source *src;
470 bucket = mac_table_bucket(sw->datapath_id, flow.dl_src);
472 LIST_FOR_EACH (src, struct mac_source, hash_list, bucket) {
473 if (src->datapath_id == sw->datapath_id
474 && mac_equals(src->mac, flow.dl_src)) {
481 /* Learn a new address. */
483 if (mac_count >= MAC_MAX) {
484 /* Drop the least recently used mac source. */
485 struct mac_source *lru;
486 lru = CONTAINER_OF(lrus.next, struct mac_source, lru_list);
487 list_remove(&lru->hash_list);
488 list_remove(&lru->lru_list);
494 /* Create new mac source */
495 src = xmalloc(sizeof *src);
496 src->datapath_id = sw->datapath_id;
497 memcpy(src->mac, flow.dl_src, ETH_ADDR_LEN);
499 list_push_front(bucket, &src->hash_list);
500 list_push_back(&lrus, &src->lru_list);
502 /* Make 'src' most-recently-used. */
503 list_remove(&src->lru_list);
504 list_push_back(&lrus, &src->lru_list);
507 if (ntohs(flow.in_port) != src->port) {
508 src->port = ntohs(flow.in_port);
509 VLOG_DBG("learned that "MAC_FMT" is on datapath %"PRIx64" port %d",
510 MAC_ARGS(src->mac), ntohll(src->datapath_id),
514 VLOG_DBG("multicast packet source "MAC_FMT, MAC_ARGS(flow.dl_src));
517 /* Figure out the destination. */
518 out_port = OFPP_FLOOD;
519 if (!mac_is_multicast(flow.dl_dst)) {
520 struct mac_source *dst;
523 bucket = mac_table_bucket(sw->datapath_id, flow.dl_dst);
524 LIST_FOR_EACH (dst, struct mac_source, hash_list, bucket) {
525 if (dst->datapath_id == sw->datapath_id
526 && mac_equals(dst->mac, flow.dl_dst)) {
527 out_port = dst->port;
533 if (out_port != OFPP_FLOOD) {
534 /* The output port is known, so add a new flow. */
535 queue_tx(sw, make_add_simple_flow(&flow, ntohl(opi->buffer_id),
538 /* If the switch didn't buffer the packet, we need to send a copy. */
539 if (ntohl(opi->buffer_id) == UINT32_MAX) {
540 queue_tx(sw, make_unbuffered_packet_out(&pkt, ntohs(flow.in_port),
544 /* We don't know that MAC. Flood the packet. */
546 if (ntohl(opi->buffer_id) == UINT32_MAX) {
547 b = make_unbuffered_packet_out(&pkt, ntohs(flow.in_port), out_port);
549 b = make_buffered_packet_out(ntohl(opi->buffer_id),
550 ntohs(flow.in_port), out_port);
557 parse_options(int argc, char *argv[])
559 static struct option long_options[] = {
560 {"hub", no_argument, 0, 'H'},
561 {"noflow", no_argument, 0, 'n'},
562 {"verbose", optional_argument, 0, 'v'},
563 {"help", no_argument, 0, 'h'},
564 {"version", no_argument, 0, 'V'},
566 {"private-key", required_argument, 0, 'p'},
567 {"certificate", required_argument, 0, 'c'},
568 {"ca-cert", required_argument, 0, 'C'},
572 char *short_options = long_options_to_short_options(long_options);
578 c = getopt_long(argc, argv, short_options, long_options, &indexptr);
596 printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
600 vlog_set_verbosity(optarg);
605 vconn_ssl_set_private_key_file(optarg);
609 vconn_ssl_set_certificate_file(optarg);
613 vconn_ssl_set_ca_cert_file(optarg);
630 printf("%s: OpenFlow controller\n"
631 "usage: %s [OPTIONS] VCONN\n"
632 "where VCONN is one of the following:\n"
633 " ptcp:[PORT] listen to TCP PORT (default: %d)\n",
634 program_name, program_name, OFP_TCP_PORT);
636 printf(" nl:DP_IDX via netlink to local datapath DP_IDX\n");
639 printf(" pssl:[PORT] listen for SSL on PORT (default: %d)\n"
640 "\nPKI configuration (required to use SSL):\n"
641 " -p, --private-key=FILE file with private key\n"
642 " -c, --certificate=FILE file with certificate for private key\n"
643 " -C, --ca-cert=FILE file with peer CA certificate\n",
646 printf("\nOther options:\n"
647 " -H, --hub act as hub instead of learning switch\n"
648 " -n, --noflow pass traffic, but don't add flows\n"
649 " -v, --verbose set maximum verbosity level\n"
650 " -h, --help display this help message\n"
651 " -V, --version display version information\n");