ping cookies
[cor_2_6_31.git] / net / cor / common.c
blobc205b73fdcee6cff64e72fbea158ef9193d05c5b
1 /*
2 * Connection oriented routing
3 * Copyright (C) 2007-2008 Michael Blizek
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 * 02110-1301, USA.
21 #include <linux/mutex.h>
23 #include "cor.h"
25 DEFINE_MUTEX(cor_bindnodes);
26 DEFINE_MUTEX(conn_free);
28 DEFINE_MUTEX(connid_gen);
30 LIST_HEAD(openports);
33 struct cell_hdr{
34 spinlock_t lock;
37 struct kmem_cache *conn_slab;
39 struct htable connid_table;
41 struct kmem_cache *bindnode_slab;
42 struct kmem_cache *connlistener_slab;
44 void ref_counter_decr(struct ref_counter *cnt)
46 unsigned long iflags;
48 BUG_ON(0 == cnt);
49 BUG_ON(0 == cnt->def);
51 spin_lock_irqsave(&(cnt->lock), iflags);
52 cnt->refs--;
53 if (unlikely(cnt->refs == 0)) {
54 cnt->def->free(cnt);
55 local_irq_restore(iflags);
56 return;
58 spin_unlock_irqrestore(&(cnt->lock), iflags);
61 int ref_counter_incr(struct ref_counter *cnt)
63 unsigned long iflags;
64 int ret = 1;
66 spin_lock_irqsave(&(cnt->lock), iflags);
67 ret = 0;
68 cnt->refs++;
69 spin_unlock_irqrestore(&(cnt->lock), iflags);
71 return 0;
74 void ref_counter_init(struct ref_counter *cnt, struct ref_counter_def *def)
76 BUG_ON(0 == cnt);
77 BUG_ON(0 == def);
79 spin_lock_init(&(cnt->lock));
80 cnt->refs = 1;
81 cnt->def = def;
85 static inline int hdr_size(void)
87 return ((sizeof(struct cell_hdr) + sizeof(void *) - 1) / sizeof(void *)
88 ) * sizeof(void *);
91 static inline int elements_per_cell(int cell_size)
93 return (cell_size - hdr_size())/sizeof(void *);
96 static inline struct cell_hdr *cell_addr(struct htable *ht, __u32 id)
98 int idx = (id%ht->htable_size) / (elements_per_cell(ht->cell_size));
99 return (struct cell_hdr *) (((char *)ht->htable) + ht->cell_size * idx);
102 static inline char **element_addr(struct htable *ht, __u32 id)
104 int idx = (id%ht->htable_size) % (elements_per_cell(ht->cell_size));
105 return (char **)
106 ( ((char *)cell_addr(ht, id)) +
107 hdr_size() + idx*sizeof(void *) );
111 static inline char **next_element(struct htable *ht, char *element)
113 return (char **)(element + ht->entry_offset);
116 static inline struct ref_counter *element_refcnt(struct htable *ht, char *element)
118 return (struct ref_counter *)(element + ht->ref_counter_offset);
122 static inline void unlock_element(struct htable *ht, __u32 key)
124 struct cell_hdr *hdr = cell_addr(ht, key);
125 spin_unlock( &(hdr->lock) );
129 static char **get_element_nounlock(struct htable *ht, __u32 key,
130 void *searcheditem)
132 struct cell_hdr *hdr = cell_addr(ht, key);
133 char **element = element_addr(ht, key);
135 BUG_ON(0 == element);
137 spin_lock( &(hdr->lock) );
139 while (1) {
140 if (*element == 0)
141 break;
142 if (searcheditem != 0 && ht->matches(*element, searcheditem))
143 break;
144 element = next_element(ht, *element);
147 return element;
150 char *htable_get(struct htable *ht, __u32 key, void *searcheditem)
152 unsigned long iflags;
153 char *element;
155 if (ht->htable == 0)
156 return 0;
158 local_irq_save(iflags);
159 element = *(get_element_nounlock(ht, key, searcheditem));
160 if (element != 0)
161 ref_counter_incr(element_refcnt(ht, element));
162 unlock_element(ht, key);
163 local_irq_restore(iflags);
165 return element;
168 int htable_delete(struct htable *ht, __u32 key,
169 void *searcheditem)
171 unsigned long iflags;
172 char **element;
173 char **next;
174 int rc = 0;
176 if (ht->htable == 0)
177 return 1;
179 local_irq_save(iflags);
181 element = get_element_nounlock(ht, key, searcheditem);
182 BUG_ON(0 == element);
184 if (0 == *element) {
185 /* key not in table */
186 rc = 1;
187 goto out;
190 next = next_element(ht, *element);
191 ref_counter_decr(element_refcnt(ht, *element));
192 *element = *next;
194 out:
195 unlock_element(ht, key);
196 local_irq_restore(iflags);
198 return rc;
201 void htable_insert(struct htable *ht, char *newelement, __u32 key)
203 unsigned long iflags;
204 char **element;
206 if (ht->htable == 0)
207 return;
209 BUG_ON(*next_element(ht, newelement) != 0);
210 local_irq_save(iflags);
212 element = get_element_nounlock(ht, key, 0);
214 BUG_ON(element == 0);
215 BUG_ON(*element != 0);
217 *element = newelement;
218 ref_counter_incr(element_refcnt(ht, newelement));
220 unlock_element(ht, key);
221 local_irq_restore(iflags);
225 void htable_init(struct htable *ht, int (*matches)(void *htentry,
226 void *searcheditem), __u32 entry_offset, __u32 ref_counter_offset)
228 int num_cells;
229 int j;
231 BUG_ON(0 == ht);
233 ht->htable = kmalloc(PAGE_SIZE, GFP_KERNEL);
234 if (ht->htable == 0) {
235 printk(KERN_CRIT "cor: error allocating htable (out of "
236 "memory?)");
237 return;
239 memset(ht->htable, 0, PAGE_SIZE);
240 ht->cell_size = 256;
242 num_cells = PAGE_SIZE/ht->cell_size;
244 for (j=0;j<num_cells;j++) {
245 struct cell_hdr *hdr = (struct cell_hdr *)
246 ( ((char *) ht->htable) + j * ht->cell_size );
247 spin_lock_init(&(hdr->lock));
250 ht->htable_size = num_cells * elements_per_cell(ht->cell_size);
251 ht->num_elements = 0;
253 ht->matches = matches;
254 ht->entry_offset = entry_offset;
255 ht->ref_counter_offset = ref_counter_offset;
258 struct conn *get_conn(__u32 conn_id)
260 return (struct conn *) htable_get(&connid_table, conn_id, &conn_id);
263 static int connid_alloc(struct conn *sconn)
265 __u32 conn_id;
266 int i;
268 BUG_ON(sconn->sourcetype != SOURCE_IN);
270 mutex_lock(&connid_gen);
271 for(i=0;i<16;i++) {
272 struct conn *tmp;
274 conn_id = 0;
275 get_random_bytes((char *) &conn_id, sizeof(conn_id));
277 if (conn_id == 0)
278 continue;
280 tmp = get_conn(conn_id);
281 if (tmp != 0) {
282 ref_counter_decr(&(tmp->refs));
283 continue;
286 goto found;
288 mutex_unlock(&connid_gen);
290 return 1;
292 found:
293 sconn->source.in.conn_id = conn_id;
294 htable_insert(&connid_table, (char *) sconn, conn_id);
295 mutex_unlock(&connid_gen);
296 return 0;
299 static void free_conn(struct ref_counter *cnt)
301 struct conn *conn = container_of(cnt, struct conn, refs);
303 BUG_ON(conn->isreset == 0);
305 mutex_lock(&conn_free);
307 if (conn->isreset != 3) {
308 conn->reversedir->isreset = 3;
309 goto out;
312 if (conn->reversedir != 0) {
313 conn->reversedir->reversedir = 0;
314 free_conn(&(conn->reversedir->refs));
315 conn->reversedir = 0;
318 if (conn->sourcetype == SOURCE_IN) {
319 ref_counter_decr(&(conn->source.in.nb->refs));
320 conn->source.in.nb = 0;
323 if (conn->targettype == TARGET_OUT) {
324 ref_counter_decr(&(conn->target.out.nb->refs));
325 conn->target.out.nb = 0;
328 databuf_free(&(conn->buf));
330 kmem_cache_free(conn_slab, conn);
332 out:
333 mutex_unlock(&conn_free);
336 static struct ref_counter_def conn_refcnt = {
337 .free = free_conn
341 * rconn ==> the connection we received the commend from
342 * ==> init rconn->target.out + rconn->reversedir->source.in
343 * rc == 0 ==> ok
344 * rc == 1 ==> connid allocation failed
346 int conn_init_out(struct conn *rconn, struct neighbor *nb)
348 struct conn *sconn = rconn->reversedir;
350 BUG_ON(TARGET_UNCONNECTED != rconn->targettype);
351 BUG_ON(0 == sconn);
352 BUG_ON(SOURCE_NONE != sconn->sourcetype);
354 memset(&(rconn->target.out), 0, sizeof(rconn->target.out));
355 memset(&(sconn->source.in), 0, sizeof(sconn->source.in));
357 rconn->targettype = TARGET_OUT;
358 sconn->sourcetype = SOURCE_IN;
360 rconn->target.out.nb = nb;
361 sconn->source.in.nb = nb;
363 skb_queue_head_init(&(sconn->source.in.reorder_queue));
366 * connid_alloc has to be called last, because packets may be received
367 * immediately after its execution
369 if (connid_alloc(sconn))
370 return 1;
372 mutex_lock(&(nb->conn_list_lock));
373 list_add_tail(&(sconn->source.in.nb_list), &(nb->rcv_conn_list));
374 list_add_tail(&(rconn->target.out.nb_list), &(nb->snd_conn_list));
375 mutex_unlock(&(nb->conn_list_lock));
377 /* neighbor lists */
378 ref_counter_incr(&(rconn->refs));
379 ref_counter_incr(&(sconn->refs));
381 return 0;
384 void conn_init_sock_source(struct conn *conn)
386 BUG_ON(conn == 0);
388 conn->sourcetype = SOURCE_SOCK;
390 memset(&(conn->source.sock), 0, sizeof(conn->source.sock));
392 init_waitqueue_head(&(conn->source.sock.wait));
395 void conn_init_sock_target(struct conn *conn)
397 BUG_ON(conn == 0);
398 conn->targettype = TARGET_SOCK;
399 init_waitqueue_head(&(conn->target.sock.wait));
402 struct conn* alloc_conn(gfp_t allocflags)
404 struct conn *rconn = 0;
405 struct conn *sconn = 0;
407 rconn = kmem_cache_alloc(conn_slab, allocflags);
408 if (unlikely(0 == rconn))
409 goto out_err0;
411 sconn = kmem_cache_alloc(conn_slab, allocflags);
412 if (unlikely(0 == sconn))
413 goto out_err1;
415 memset(rconn, 0, sizeof(struct conn));
416 memset(sconn, 0, sizeof(struct conn));
418 rconn->reversedir = sconn;
419 sconn->reversedir = rconn;
421 ref_counter_init(&(rconn->refs), &conn_refcnt);
422 ref_counter_init(&(sconn->refs), &conn_refcnt);
424 mutex_init(&(rconn->rcv_lock));
425 mutex_init(&(sconn->rcv_lock));
427 rconn->sockstate = SOCKSTATE_CONN;
428 sconn->sockstate = SOCKSTATE_CONN;
430 databuf_init(&(rconn->buf));
431 databuf_init(&(sconn->buf));
433 return rconn;
435 out_err1:
436 kmem_cache_free(conn_slab, rconn);
437 out_err0:
438 return 0;
441 static struct connlistener *get_connlistener(__be64 port)
443 struct list_head *curr = openports.next;
445 while (curr != &openports) {
446 struct bindnode *currnode = ((struct bindnode *)
447 (((char *)curr) - offsetof(struct bindnode, lh)));
448 if (currnode->port == port) {
449 BUG_ON(currnode->owner == 0);
450 return currnode->owner;
453 curr = curr->next;
456 return 0;
459 void close_port(struct connlistener *listener)
461 mutex_lock(&cor_bindnodes);
463 if (listener->bn != 0) {
464 list_del(&(listener->bn->lh));
465 kmem_cache_free(bindnode_slab, listener->bn);
466 listener->bn = 0;
469 while (list_empty(&(listener->conn_queue)) == 0) {
470 struct conn *rconn = container_of(listener->conn_queue.next,
471 struct conn, source.sock.cl_list);
472 rconn->reversedir->isreset = 1;
473 reset_conn(rconn);
474 list_del(&(rconn->source.sock.cl_list));
475 ref_counter_decr(&(rconn->refs));
478 kmem_cache_free(connlistener_slab, listener);
480 mutex_unlock(&cor_bindnodes);
483 struct connlistener *open_port(__be64 port)
486 struct bindnode *bn = 0;
487 struct connlistener *listener = 0;
489 mutex_lock(&cor_bindnodes);
490 if (get_connlistener(port) != 0)
491 goto out;
494 bn = kmem_cache_alloc(bindnode_slab, GFP_KERNEL);
495 listener = kmem_cache_alloc(connlistener_slab, GFP_KERNEL);
497 memset(bn, 0, sizeof(struct bindnode));
498 memset(listener, 0, sizeof(struct connlistener));
500 bn->owner = listener;
501 bn->port = port;
503 /* refcounter is not actually used */
504 listener->sockstate = SOCKSTATE_LISTENER;
505 listener->bn = bn;
506 mutex_init(&(listener->lock));
507 INIT_LIST_HEAD(&(listener->conn_queue));
508 init_waitqueue_head(&(listener->wait));
510 list_add_tail((struct list_head *) &(bn->lh), &openports);
512 out:
513 mutex_unlock(&cor_bindnodes);
515 return listener;
519 * rc == 0 connected
520 * rc == 2 port not open
521 * rc == 3 listener queue full
523 int connect_port(struct conn *rconn, __be64 port)
526 struct connlistener *listener;
527 int rc = 0;
529 mutex_lock(&cor_bindnodes);
531 listener = get_connlistener(port);
532 if (listener == 0) {
533 rc = 2;
534 goto out;
537 mutex_lock(&(listener->lock));
539 if (listener->queue_len >= listener->queue_maxlen) {
540 if (listener->queue_maxlen <= 0)
541 rc = 2;
542 else
543 rc = 3;
545 goto out2;
548 if (ref_counter_incr(&(rconn->reversedir->refs)))
549 BUG();
551 conn_init_sock_target(rconn);
552 conn_init_sock_source(rconn->reversedir);
554 list_add_tail(&(rconn->reversedir->source.sock.cl_list),
555 &(listener->conn_queue));
556 listener->queue_len++;
557 wake_up_interruptible(&(listener->wait));
559 out2:
560 mutex_unlock(&(listener->lock));
562 out:
563 mutex_unlock(&cor_bindnodes);
564 return rc;
568 * rc == 0 connected
569 * rc == 2 addrtype not found
570 * rc == 3 addr not found
571 * rc == 4 ==> connid allocation failed
572 * rc == 5 ==> control msg alloc failed
574 int connect_neigh(struct conn *rconn,
575 __u16 addrtypelen, __u8 *addrtype,
576 __u16 addrlen, __u8 *addr)
578 int rc = 0;
579 struct control_msg_out *cm;
580 struct neighbor *nb = find_neigh(addrtypelen, addrtype, addrlen, addr);
581 if (nb == 0)
582 return 3;
583 if (conn_init_out(rconn, nb)) {
584 rc = 4;
585 goto neigh_refcnt;
588 cm = alloc_control_msg();
589 if (unlikely(cm == 0)) {
590 rc = 5;
591 goto neigh_refcnt;
594 send_connect_nb(cm, nb, rconn->reversedir->source.in.conn_id);
596 if (0) {
597 neigh_refcnt:
598 ref_counter_decr(&(nb->refs));
601 return rc;
604 static void _reset_conn(struct conn *conn)
606 if (conn->isreset == 1)
607 goto free;
609 if (conn->isreset == 2 || conn->isreset == 3)
610 return;
612 if (conn->targettype == TARGET_OUT && conn->target.out.conn_id != 0) {
613 struct control_msg_out *cm = alloc_control_msg();
614 if (likely(cm != 0))
615 send_reset_conn(cm, conn->target.out.nb,
616 conn->target.out.conn_id);
619 free:
621 if (conn->sourcetype == SOURCE_IN) {
622 mutex_lock(&(conn->source.in.nb->conn_list_lock));
623 list_del(&(conn->source.in.nb_list));
624 mutex_unlock(&(conn->source.in.nb->conn_list_lock));
626 ref_counter_decr(&(conn->refs));
628 if (conn->source.in.conn_id != 0)
629 htable_delete(&connid_table, conn->source.in.conn_id,
630 &(conn->source.in.conn_id));
633 if (conn->targettype == TARGET_OUT) {
634 mutex_lock(&(conn->target.out.nb->conn_list_lock));
635 list_del(&(conn->target.out.nb_list));
636 mutex_unlock(&(conn->target.out.nb->conn_list_lock));
637 ref_counter_decr(&(conn->refs));
640 conn->isreset = 2;
643 /* warning: do not hold the rcv_lock while calling this! */
644 void reset_conn(struct conn *conn)
646 _reset_conn(conn);
647 _reset_conn(conn->reversedir);
650 static int matches_connid_in(void *htentry, void *searcheditem)
652 struct conn *conn = (struct conn *) htentry;
653 __u32 conn_id = *((__u32 *) searcheditem);
654 BUG_ON(conn->sourcetype != SOURCE_IN);
655 return (conn->source.in.conn_id == conn_id);
658 static int __init cor_common_init(void)
660 int rc;
662 struct conn c;
664 printk(KERN_ERR "sizeof conn: %d", sizeof(c));
665 printk(KERN_ERR " conn.source: %d", sizeof(c.source));
666 printk(KERN_ERR " conn.target: %d", sizeof(c.target));
667 printk(KERN_ERR " conn.target.out: %d", sizeof(c.target.out));
668 printk(KERN_ERR " conn.target.buf: %d", sizeof(c.buf));
670 printk(KERN_ERR " mutex: %d", sizeof(struct mutex));
671 printk(KERN_ERR " spinlock: %d", sizeof(spinlock_t));
672 printk(KERN_ERR " kref: %d", sizeof(struct kref));
675 conn_slab = kmem_cache_create("cor_conn", sizeof(struct conn), 8, 0, 0);
676 htable_init(&connid_table, matches_connid_in,
677 offsetof(struct conn, source.in.htab_entry),
678 offsetof(struct conn, refs));
680 bindnode_slab = kmem_cache_create("cor_bindnode",
681 sizeof(struct bindnode), 8, 0, 0);
682 connlistener_slab = kmem_cache_create("cor_connlistener",
683 sizeof(struct connlistener), 8, 0, 0);
685 forward_init();
687 rc = cor_snd_init();
688 if (rc != 0)
689 return rc;
691 rc = cor_neighbor_init();
692 if (rc != 0)
693 return rc;
695 rc = cor_rcv_init();
696 if (rc != 0)
697 return rc;
699 return 0;
702 module_init(cor_common_init);
703 MODULE_LICENSE("GPL");