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
21 #include <linux/mutex.h>
25 DEFINE_MUTEX(cor_bindnodes
);
26 DEFINE_MUTEX(conn_free
);
28 DEFINE_MUTEX(connid_gen
);
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
)
49 BUG_ON(0 == cnt
->def
);
51 spin_lock_irqsave(&(cnt
->lock
), iflags
);
53 if (unlikely(cnt
->refs
== 0)) {
55 local_irq_restore(iflags
);
58 spin_unlock_irqrestore(&(cnt
->lock
), iflags
);
61 int ref_counter_incr(struct ref_counter
*cnt
)
66 spin_lock_irqsave(&(cnt
->lock
), iflags
);
69 spin_unlock_irqrestore(&(cnt
->lock
), iflags
);
74 void ref_counter_init(struct ref_counter
*cnt
, struct ref_counter_def
*def
)
79 spin_lock_init(&(cnt
->lock
));
85 static inline int hdr_size(void)
87 return ((sizeof(struct cell_hdr
) + sizeof(void *) - 1) / 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
));
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
,
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
) );
142 if (searcheditem
!= 0 && ht
->matches(*element
, searcheditem
))
144 element
= next_element(ht
, *element
);
150 char *htable_get(struct htable
*ht
, __u32 key
, void *searcheditem
)
152 unsigned long iflags
;
158 local_irq_save(iflags
);
159 element
= *(get_element_nounlock(ht
, key
, searcheditem
));
161 ref_counter_incr(element_refcnt(ht
, element
));
162 unlock_element(ht
, key
);
163 local_irq_restore(iflags
);
168 int htable_delete(struct htable
*ht
, __u32 key
,
171 unsigned long iflags
;
179 local_irq_save(iflags
);
181 element
= get_element_nounlock(ht
, key
, searcheditem
);
182 BUG_ON(0 == element
);
185 /* key not in table */
190 next
= next_element(ht
, *element
);
191 ref_counter_decr(element_refcnt(ht
, *element
));
195 unlock_element(ht
, key
);
196 local_irq_restore(iflags
);
201 void htable_insert(struct htable
*ht
, char *newelement
, __u32 key
)
203 unsigned long iflags
;
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
)
233 ht
->htable
= kmalloc(PAGE_SIZE
, GFP_KERNEL
);
234 if (ht
->htable
== 0) {
235 printk(KERN_CRIT
"cor: error allocating htable (out of "
239 memset(ht
->htable
, 0, PAGE_SIZE
);
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
)
268 BUG_ON(sconn
->sourcetype
!= SOURCE_IN
);
270 mutex_lock(&connid_gen
);
275 get_random_bytes((char *) &conn_id
, sizeof(conn_id
));
280 tmp
= get_conn(conn_id
);
282 ref_counter_decr(&(tmp
->refs
));
288 mutex_unlock(&connid_gen
);
293 sconn
->source
.in
.conn_id
= conn_id
;
294 htable_insert(&connid_table
, (char *) sconn
, conn_id
);
295 mutex_unlock(&connid_gen
);
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;
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
);
333 mutex_unlock(&conn_free
);
336 static struct ref_counter_def conn_refcnt
= {
341 * rconn ==> the connection we received the commend from
342 * ==> init rconn->target.out + rconn->reversedir->source.in
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
);
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
))
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
));
378 ref_counter_incr(&(rconn
->refs
));
379 ref_counter_incr(&(sconn
->refs
));
384 void conn_init_sock_source(struct conn
*conn
)
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
)
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
))
411 sconn
= kmem_cache_alloc(conn_slab
, allocflags
);
412 if (unlikely(0 == sconn
))
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
));
436 kmem_cache_free(conn_slab
, rconn
);
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
;
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
);
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;
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)
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
;
503 /* refcounter is not actually used */
504 listener
->sockstate
= SOCKSTATE_LISTENER
;
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
);
513 mutex_unlock(&cor_bindnodes
);
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
;
529 mutex_lock(&cor_bindnodes
);
531 listener
= get_connlistener(port
);
537 mutex_lock(&(listener
->lock
));
539 if (listener
->queue_len
>= listener
->queue_maxlen
) {
540 if (listener
->queue_maxlen
<= 0)
548 if (ref_counter_incr(&(rconn
->reversedir
->refs
)))
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
));
560 mutex_unlock(&(listener
->lock
));
563 mutex_unlock(&cor_bindnodes
);
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
)
579 struct control_msg_out
*cm
;
580 struct neighbor
*nb
= find_neigh(addrtypelen
, addrtype
, addrlen
, addr
);
583 if (conn_init_out(rconn
, nb
)) {
588 cm
= alloc_control_msg();
589 if (unlikely(cm
== 0)) {
594 send_connect_nb(cm
, nb
, rconn
->reversedir
->source
.in
.conn_id
);
598 ref_counter_decr(&(nb
->refs
));
604 static void _reset_conn(struct conn
*conn
)
606 if (conn
->isreset
== 1)
609 if (conn
->isreset
== 2 || conn
->isreset
== 3)
612 if (conn
->targettype
== TARGET_OUT
&& conn
->target
.out
.conn_id
!= 0) {
613 struct control_msg_out
*cm
= alloc_control_msg();
615 send_reset_conn(cm
, conn
->target
.out
.nb
,
616 conn
->target
.out
.conn_id
);
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
));
643 /* warning: do not hold the rcv_lock while calling this! */
644 void reset_conn(struct 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)
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);
691 rc
= cor_neighbor_init();
702 module_init(cor_common_init
);
703 MODULE_LICENSE("GPL");