458f621faa818f24ee23d2aaeff4a06305579f34
[sliver-openvswitch.git] / controller / controller.c
1 /* Copyright (C) 2007 Board of Trustees, Leland Stanford Jr. University.
2  *
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:
9  *
10  * The above copyright notice and this permission notice shall be included in
11  * all copies or substantial portions of the Software.
12  *
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
19  * IN THE SOFTWARE.
20  */
21
22 #include <assert.h>
23 #include <errno.h>
24 #include <getopt.h>
25 #include <inttypes.h>
26 #include <netinet/in.h>
27 #include <poll.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31
32 #include "buffer.h"
33 #include "command-line.h"
34 #include "compiler.h"
35 #include "fault.h"
36 #include "flow.h"
37 #include "hash.h"
38 #include "list.h"
39 #include "mac.h"
40 #include "ofp-print.h"
41 #include "openflow.h"
42 #include "poll-loop.h"
43 #include "queue.h"
44 #include "time.h"
45 #include "util.h"
46 #include "vconn-ssl.h"
47 #include "vconn.h"
48 #include "vlog-socket.h"
49 #include "xtoxll.h"
50
51 #include "vlog.h"
52 #define THIS_MODULE VLM_controller
53
54 #define MAX_SWITCHES 16
55 #define MAX_TXQ 128
56
57 struct switch_ {
58     char *name;
59     struct vconn *vconn;
60
61     uint64_t datapath_id;
62     time_t last_control_hello;
63
64     struct queue txq;
65 };
66
67 /* -H, --hub: Use dumb hub instead of learning switch? */
68 static bool hub = false;
69
70 /* -n, --noflow: Pass traffic, but don't setup flows in switch */
71 static bool noflow = false;
72
73 static void parse_options(int argc, char *argv[]);
74 static void usage(void) NO_RETURN;
75
76 static struct switch_ *connect_switch(const char *name);
77 static struct switch_ *new_switch(const char *name, struct vconn *);
78 static void close_switch(struct switch_ *);
79
80 static void queue_tx(struct switch_ *, struct buffer *);
81
82 static void send_control_hello(struct switch_ *);
83
84 static int do_switch_recv(struct switch_ *this);
85 static int do_switch_send(struct switch_ *this);
86
87 static void process_packet(struct switch_ *, struct buffer *);
88 static void process_hub(struct switch_ *, struct ofp_packet_in *);
89 static void process_noflow(struct switch_ *, struct ofp_packet_in *);
90
91 static void switch_init(void);
92 static void process_switch(struct switch_ *, struct ofp_packet_in *);
93
94 int
95 main(int argc, char *argv[])
96 {
97     struct switch_ *switches[MAX_SWITCHES];
98     int n_switches;
99     int retval;
100     int i;
101
102     set_program_name(argv[0]);
103     register_fault_handlers();
104     vlog_init();
105     parse_options(argc, argv);
106
107     if (!hub && !noflow) {
108         switch_init();
109     }
110
111     if (argc - optind < 1) {
112         fatal(0, "at least one vconn argument required; use --help for usage");
113     }
114
115     retval = vlog_server_listen(NULL, NULL);
116     if (retval) {
117         fatal(retval, "Could not listen for vlog connections");
118     }
119
120     n_switches = 0;
121     for (i = 0; i < argc - optind; i++) {
122         struct switch_ *this = connect_switch(argv[optind + i]);
123         if (this) {
124             if (n_switches >= MAX_SWITCHES) {
125                 fatal(0, "max %d switch connections", n_switches);
126             }
127             switches[n_switches++] = this;
128         }
129     }
130     if (n_switches == 0) {
131         fatal(0, "could not connect to any switches");
132     }
133
134     while (n_switches > 0) {
135         /* Do some work.  Limit the number of iterations so that callbacks
136          * registered with the poll loop don't starve. */
137         int iteration;
138         int i;
139         for (iteration = 0; iteration < 50; iteration++) {
140             bool progress = false;
141             for (i = 0; i < n_switches; ) {
142                 struct switch_ *this = switches[i];
143                 int retval;
144
145                 if (vconn_is_passive(this->vconn)) {
146                     retval = 0;
147                     while (n_switches < MAX_SWITCHES) {
148                         struct vconn *new_vconn;
149                         retval = vconn_accept(this->vconn, &new_vconn);
150                         if (retval) {
151                             break;
152                         }
153                         switches[n_switches++] = new_switch("tcp", new_vconn);
154                     }
155                 } else {
156                     retval = do_switch_recv(this);
157                     if (!retval || retval == EAGAIN) {
158                         do {
159                             retval = do_switch_send(this);
160                             if (!retval) {
161                                 progress = true;
162                             }
163                         } while (!retval);
164                     }
165                 }
166
167                 if (retval && retval != EAGAIN) {
168                     close_switch(this);
169                     switches[i] = switches[--n_switches];
170                 } else {
171                     i++;
172                 }
173             }
174             if (!progress) {
175                 break;
176             }
177         }
178
179         /* Wait for something to happen. */
180         for (i = 0; i < n_switches; i++) {
181             struct switch_ *this = switches[i];
182             if (vconn_is_passive(this->vconn)) {
183                 if (n_switches < MAX_SWITCHES) {
184                     vconn_accept_wait(this->vconn);
185                 }
186             } else {
187                 vconn_recv_wait(this->vconn);
188                 if (this->txq.n) {
189                     vconn_send_wait(this->vconn);
190                 }
191             }
192         }
193         poll_block();
194     }
195
196     return 0;
197 }
198
199 static int
200 do_switch_recv(struct switch_ *this) 
201 {
202     struct buffer *msg;
203     int retval;
204
205     retval = vconn_recv(this->vconn, &msg);
206     if (!retval) {
207         process_packet(this, msg);
208         buffer_delete(msg);
209     }
210     return retval;
211 }
212
213 static int
214 do_switch_send(struct switch_ *this) 
215 {
216     int retval = 0;
217     if (this->txq.n) {
218         struct buffer *next = this->txq.head->next;
219         retval = vconn_send(this->vconn, this->txq.head);
220         if (retval) {
221             return retval;
222         }
223         queue_advance_head(&this->txq, next);
224         return 0;
225     }
226     return EAGAIN;
227 }
228
229 struct switch_ *
230 connect_switch(const char *name) 
231 {
232     struct vconn *vconn;
233     int retval;
234
235     retval = vconn_open(name, &vconn);
236     if (retval) {
237         VLOG_ERR("%s: connect: %s", name, strerror(retval));
238         return NULL;
239     }
240
241     return new_switch(name, vconn);
242 }
243
244 static struct switch_ *
245 new_switch(const char *name, struct vconn *vconn) 
246 {
247     struct switch_ *this = xmalloc(sizeof *this);
248     memset(this, 0, sizeof *this);
249     this->name = xstrdup(name);
250     this->vconn = vconn;
251     queue_init(&this->txq);
252     this->last_control_hello = 0;
253     if (!vconn_is_passive(vconn)) {
254         send_control_hello(this);
255     }
256     return this;
257 }
258
259 static void
260 close_switch(struct switch_ *this) 
261 {
262     if (this) {
263         free(this->name);
264         vconn_close(this->vconn);
265         queue_destroy(&this->txq);
266         free(this);
267     }
268 }
269
270 static void
271 send_control_hello(struct switch_ *this)
272 {
273     time_t now = time(0);
274     if (now >= this->last_control_hello + 1) {
275         struct buffer *b;
276         struct ofp_control_hello *och;
277
278         b = buffer_new(0);
279         och = buffer_put_uninit(b, sizeof *och);
280         memset(och, 0, sizeof *och);
281         och->header.version = OFP_VERSION;
282         och->header.length = htons(sizeof *och);
283
284         och->version = htonl(OFP_VERSION);
285         och->flags = htons(OFP_CHELLO_SEND_FLOW_EXP);
286         och->miss_send_len = htons(OFP_DEFAULT_MISS_SEND_LEN);
287         queue_tx(this, b);
288
289         this->last_control_hello = now;
290     }
291 }
292
293 static void
294 queue_tx(struct switch_ *this, struct buffer *b) 
295 {
296     queue_push_tail(&this->txq, b);
297 }
298
299 static void
300 process_packet(struct switch_ *sw, struct buffer *msg) 
301 {
302     static const size_t min_size[UINT8_MAX + 1] = {
303         [0 ... UINT8_MAX] = SIZE_MAX,
304         [OFPT_CONTROL_HELLO] = sizeof (struct ofp_control_hello),
305         [OFPT_DATA_HELLO] = sizeof (struct ofp_data_hello),
306         [OFPT_PACKET_IN] = offsetof (struct ofp_packet_in, data),
307         [OFPT_PACKET_OUT] = sizeof (struct ofp_packet_out),
308         [OFPT_FLOW_MOD] = sizeof (struct ofp_flow_mod),
309         [OFPT_FLOW_EXPIRED] = sizeof (struct ofp_flow_expired),
310         [OFPT_TABLE] = sizeof (struct ofp_table),
311         [OFPT_PORT_MOD] = sizeof (struct ofp_port_mod),
312         [OFPT_PORT_STATUS] = sizeof (struct ofp_port_status),
313         [OFPT_FLOW_STAT_REQUEST] = sizeof (struct ofp_flow_stat_request),
314         [OFPT_FLOW_STAT_REPLY] = sizeof (struct ofp_flow_stat_reply),
315     };
316     struct ofp_header *oh;
317
318     oh = msg->data;
319     if (msg->size < min_size[oh->type]) {
320         VLOG_WARN("%s: too short (%zu bytes) for type %"PRIu8" (min %zu)",
321                   sw->name, msg->size, oh->type, min_size[oh->type]);
322         return;
323     }
324
325     if (oh->type == OFPT_DATA_HELLO) {
326         struct ofp_data_hello *odh = msg->data;
327         sw->datapath_id = odh->datapath_id;
328     } else if (sw->datapath_id == 0) {
329         send_control_hello(sw);
330         return;
331     }
332
333     if (oh->type == OFPT_PACKET_IN) {
334         if (sw->txq.n >= MAX_TXQ) {
335             /* FIXME: ratelimit. */
336             VLOG_WARN("%s: tx queue overflow", sw->name);
337         } else if (noflow) {
338             process_noflow(sw, msg->data);
339         } else if (hub) {
340             process_hub(sw, msg->data);
341         } else {
342             process_switch(sw, msg->data);
343         }
344         return;
345     }
346
347     ofp_print(stdout, msg->data, msg->size, 2);
348 }
349
350 static void
351 process_hub(struct switch_ *sw, struct ofp_packet_in *opi)
352 {
353     size_t pkt_ofs, pkt_len;
354     struct buffer pkt;
355     struct flow flow;
356
357     /* Extract flow data from 'opi' into 'flow'. */
358     pkt_ofs = offsetof(struct ofp_packet_in, data);
359     pkt_len = ntohs(opi->header.length) - pkt_ofs;
360     pkt.data = opi->data;
361     pkt.size = pkt_len;
362     flow_extract(&pkt, ntohs(opi->in_port), &flow);
363
364     /* Add new flow. */
365     queue_tx(sw, make_add_simple_flow(&flow, ntohl(opi->buffer_id),
366                                       OFPP_FLOOD));
367
368     /* If the switch didn't buffer the packet, we need to send a copy. */
369     if (ntohl(opi->buffer_id) == UINT32_MAX) {
370         queue_tx(sw, make_unbuffered_packet_out(&pkt, ntohs(flow.in_port),
371                                                 OFPP_FLOOD));
372     }
373 }
374
375 static void
376 process_noflow(struct switch_ *sw, struct ofp_packet_in *opi)
377 {
378     /* If the switch didn't buffer the packet, we need to send a copy. */
379     if (ntohl(opi->buffer_id) == UINT32_MAX) {
380         size_t pkt_ofs, pkt_len;
381         struct buffer pkt;
382
383         /* Extract flow data from 'opi' into 'flow'. */
384         pkt_ofs = offsetof(struct ofp_packet_in, data);
385         pkt_len = ntohs(opi->header.length) - pkt_ofs;
386         pkt.data = opi->data;
387         pkt.size = pkt_len;
388
389         queue_tx(sw, make_unbuffered_packet_out(&pkt, ntohs(opi->in_port),
390                     OFPP_FLOOD));
391     } else {
392         queue_tx(sw, make_buffered_packet_out(ntohl(opi->buffer_id), 
393                     ntohs(opi->in_port), OFPP_FLOOD));
394     }
395 }
396
397
398 #define MAC_HASH_BITS 10
399 #define MAC_HASH_MASK (MAC_HASH_SIZE - 1)
400 #define MAC_HASH_SIZE (1u << MAC_HASH_BITS)
401
402 #define MAC_MAX 1024
403
404 struct mac_source {
405     struct list hash_list;
406     struct list lru_list;
407     uint64_t datapath_id;
408     uint8_t mac[ETH_ADDR_LEN];
409     uint16_t port;
410 };
411
412 static struct list mac_table[MAC_HASH_SIZE];
413 static struct list lrus;
414 static size_t mac_count;
415
416 static void
417 switch_init(void)
418 {
419     int i;
420
421     list_init(&lrus);
422     for (i = 0; i < MAC_HASH_SIZE; i++) {
423         list_init(&mac_table[i]);
424     }
425 }
426
427 static struct list *
428 mac_table_bucket(uint64_t datapath_id, const uint8_t mac[ETH_ADDR_LEN]) 
429 {
430     uint32_t hash;
431     hash = hash_fnv(&datapath_id, sizeof datapath_id, HASH_FNV_BASIS);
432     hash = hash_fnv(mac, ETH_ADDR_LEN, hash);
433     return &mac_table[hash & MAC_HASH_BITS];
434 }
435
436 static void
437 process_switch(struct switch_ *sw, struct ofp_packet_in *opi)
438 {
439     size_t pkt_ofs, pkt_len;
440     struct buffer pkt;
441     struct flow flow;
442
443     uint16_t out_port;
444
445     /* Extract flow data from 'opi' into 'flow'. */
446     pkt_ofs = offsetof(struct ofp_packet_in, data);
447     pkt_len = ntohs(opi->header.length) - pkt_ofs;
448     pkt.data = opi->data;
449     pkt.size = pkt_len;
450     flow_extract(&pkt, ntohs(opi->in_port), &flow);
451
452     /* Learn the source. */
453     if (!mac_is_multicast(flow.dl_src)) {
454         struct mac_source *src;
455         struct list *bucket;
456         bool found;
457
458         bucket = mac_table_bucket(sw->datapath_id, flow.dl_src);
459         found = false;
460         LIST_FOR_EACH (src, struct mac_source, hash_list, bucket) {
461             if (src->datapath_id == sw->datapath_id
462                 && mac_equals(src->mac, flow.dl_src)) {
463                 found = true;
464                 break;
465             }
466         }
467
468         if (!found) {
469             /* Learn a new address. */
470
471             if (mac_count >= MAC_MAX) {
472                 /* Drop the least recently used mac source. */
473                 struct mac_source *lru;
474                 lru = CONTAINER_OF(lrus.next, struct mac_source, lru_list);
475                 list_remove(&lru->hash_list);
476                 list_remove(&lru->lru_list);
477                 free(lru);
478             } else {
479                 mac_count++;
480             }
481
482             /* Create new mac source */
483             src = xmalloc(sizeof *src);
484             src->datapath_id = sw->datapath_id;
485             memcpy(src->mac, flow.dl_src, ETH_ADDR_LEN);
486             src->port = -1;
487             list_push_front(bucket, &src->hash_list);
488             list_push_back(&lrus, &src->lru_list);
489         } else {
490             /* Make 'src' most-recently-used.  */
491             list_remove(&src->lru_list);
492             list_push_back(&lrus, &src->lru_list);
493         }
494
495         if (ntohs(flow.in_port) != src->port) {
496             src->port = ntohs(flow.in_port);
497             VLOG_DBG("learned that "MAC_FMT" is on datapath %"PRIx64" port %d",
498                      MAC_ARGS(src->mac), ntohll(src->datapath_id),
499                      src->port);
500         }
501     } else {
502         VLOG_DBG("multicast packet source "MAC_FMT, MAC_ARGS(flow.dl_src));
503     }
504
505     /* Figure out the destination. */
506     out_port = OFPP_FLOOD;
507     if (!mac_is_multicast(flow.dl_dst)) {
508         struct mac_source *dst;
509         struct list *bucket;
510
511         bucket = mac_table_bucket(sw->datapath_id, flow.dl_dst);
512         LIST_FOR_EACH (dst, struct mac_source, hash_list, bucket) {
513             if (dst->datapath_id == sw->datapath_id
514                 && mac_equals(dst->mac, flow.dl_dst)) {
515                 out_port = dst->port;
516                 break;
517             }
518         }
519     }
520
521     if (out_port != OFPP_FLOOD) {
522         /* The output port is known, so add a new flow. */
523         queue_tx(sw, make_add_simple_flow(&flow, ntohl(opi->buffer_id), 
524                     out_port));
525
526         /* If the switch didn't buffer the packet, we need to send a copy. */
527         if (ntohl(opi->buffer_id) == UINT32_MAX) {
528             queue_tx(sw, make_unbuffered_packet_out(&pkt, ntohs(flow.in_port),
529                                                     out_port));
530         }
531     } else {
532         /* We don't know that MAC.  Flood the packet. */
533         struct buffer *b;
534         if (ntohl(opi->buffer_id) == UINT32_MAX) {
535             b = make_unbuffered_packet_out(&pkt, ntohs(flow.in_port), out_port);
536         } else {
537             b = make_buffered_packet_out(ntohl(opi->buffer_id), 
538                         ntohs(flow.in_port), out_port);
539         }
540         queue_tx(sw, b);
541     }
542 }
543
544 static void
545 parse_options(int argc, char *argv[])
546 {
547     static struct option long_options[] = {
548         {"hub",         no_argument, 0, 'H'},
549         {"noflow",      no_argument, 0, 'n'},
550         {"verbose",     optional_argument, 0, 'v'},
551         {"help",        no_argument, 0, 'h'},
552         {"version",     no_argument, 0, 'V'},
553 #ifdef HAVE_OPENSSL
554         {"private-key", required_argument, 0, 'p'},
555         {"certificate", required_argument, 0, 'c'},
556         {"ca-cert",     required_argument, 0, 'C'},
557 #endif
558         {0, 0, 0, 0},
559     };
560     char *short_options = long_options_to_short_options(long_options);
561
562     for (;;) {
563         int indexptr;
564         int c;
565
566         c = getopt_long(argc, argv, short_options, long_options, &indexptr);
567         if (c == -1) {
568             break;
569         }
570
571         switch (c) {
572         case 'H':
573             hub = true;
574             break;
575
576         case 'n':
577             noflow = true;
578             break;
579
580         case 'h':
581             usage();
582
583         case 'V':
584             printf("%s "VERSION" compiled "__DATE__" "__TIME__"\n", argv[0]);
585             exit(EXIT_SUCCESS);
586
587         case 'v':
588             vlog_set_verbosity(optarg);
589             break;
590
591 #ifdef HAVE_OPENSSL
592         case 'p':
593             vconn_ssl_set_private_key_file(optarg);
594             break;
595
596         case 'c':
597             vconn_ssl_set_certificate_file(optarg);
598             break;
599
600         case 'C':
601             vconn_ssl_set_ca_cert_file(optarg);
602             break;
603 #endif
604
605         case '?':
606             exit(EXIT_FAILURE);
607
608         default:
609             abort();
610         }
611     }
612     free(short_options);
613 }
614
615 static void
616 usage(void)
617 {
618     printf("%s: OpenFlow controller\n"
619            "usage: %s [OPTIONS] VCONN\n"
620            "where VCONN is one of the following:\n"
621            "  ptcp:[PORT]             listen to TCP PORT (default: %d)\n",
622            program_name, program_name, OFP_TCP_PORT);
623 #ifdef HAVE_NETLINK
624     printf("  nl:DP_IDX               via netlink to local datapath DP_IDX\n");
625 #endif
626 #ifdef HAVE_OPENSSL
627     printf("  pssl:[PORT]             listen for SSL on PORT (default: %d)\n"
628            "\nPKI configuration (required to use SSL):\n"
629            "  -p, --private-key=FILE  file with private key\n"
630            "  -c, --certificate=FILE  file with certificate for private key\n"
631            "  -C, --ca-cert=FILE      file with peer CA certificate\n",
632            OFP_SSL_PORT);
633 #endif
634     printf("\nOther options:\n"
635            "  -H, --hub               act as hub instead of learning switch\n"
636            "  -n, --noflow            pass traffic, but don't add flows\n"
637            "  -v, --verbose           set maximum verbosity level\n"
638            "  -h, --help              display this help message\n"
639            "  -V, --version           display version information\n");
640     exit(EXIT_SUCCESS);
641 }