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 <asm/atomic.h>
23 #include <linux/types.h>
24 #include <linux/netdevice.h>
25 #include <linux/skbuff.h>
26 /* #include <asm-sh/kgdb.h> */
27 #include <linux/spinlock.h>
28 #include <linux/workqueue.h>
31 #include <net/pkt_sched.h>
35 #define PIDOUT_NEWCONN 16
36 #define PIDOUT_SENDDEF_THRES 8
37 #define PIDOUT_SENDDEF_COUNT 16
41 #define ETH_P_COR 0x1022
45 #define SOCKADDRTYPE_PORT 1
54 #define MAX_CONN_CMD_LEN 4096
57 #define PACKET_TYPE_ANNOUNCE 1
58 #define PACKET_TYPE_DATA 2
61 * Kernel packet data - these commands are sent by the neighbor
62 * The end nodes may cause these commands to be sent, but they see them beyond
69 /* KP_[N]ACK[1] sent_conn_id[4] seqno[4]
71 * sent_conn_id means that this is *not* the conn_id we use if we sent something
72 * through this conn, but the conn_id that the neighbor used to send us the
79 * KP_SPEED[1] conn_id[4] speedinfo[2]
82 * buffer_state_value = speedinfo % 181
83 * speed_value = speedinfo / 181
85 * buffer_state = 1024 * pow(2, buffer_state_value/3.0)
86 * speed = 1024 * pow(2, speed_value/12.0)
89 * This has to be done either with floating points (which is no so nice) or
91 * buffer_state = pow(2, value/3) *
92 * 1024 * pow(pow(2, 1.0/3), buffer_state_value%3)
93 * where 1024 * pow(pow(2, 1.0/4), value%3) can be just a table lookup
94 * (the "1024" should be part of the value in the table, because it increases
97 * you can do the same with the speed
100 * Some values have special meanings:
101 * if speedinfo is the highest possible value(65535), it means both values
103 * if buffer_state_value if > 91, you have to subtract 90 and make the
104 * resulting buffer_state negative
108 /* NOTE on connection ids:
109 * connection ids we send are used for the receive channel
110 * connection ids we receive are used for the send channel
114 * incoming connection
115 * KP_CONNECT[1] conn_id[4]
117 #define KP_CONNECT 10
120 * incoming connection successful,
121 * the first conn_id is the same as previously sent/received in KP_CONNECT
122 * the second conn_id is generated by us and used for the other direction
123 * KP_CONNECT_SUCCESS[1] conn_id[4] conn_id[4]
125 #define KP_CONNECT_SUCCESS 11
127 /* KP_CONNECT_FAILED_*[1] conn_id[4] */
128 #define KP_CONNECT_FAILED_TEMP 12
129 #define KP_CONNECT_FAILED_PERM 13
131 /* KP_CONN_DATA[1] conn_id[4] seqno[4] length[4] data[length] */
132 #define KP_CONN_DATA 14
135 * { KP_RESET_CONN[1] conn_id[4] }
136 * We send this, if there is an established connection we want to close.
138 #define KP_RESET_CONN 15
142 * Connection data which in interpreted when connection has no target yet
143 * These commands are sent by the end node.
146 * cmd[2] length[4] parameter[length]
147 * unrecogniced commands are ignored
148 * parameters which are longer than expected are ignored as well
151 /* outgoing connection: CD_CONNECT_NB[2] addrlen[2][2] addr[addrlen][2] */
152 #define CD_CONNECT_NB 1
154 /* connection to local open part: CD_CONNECT_PORT[2] port[8] */
155 #define CD_CONNECT_PORT 2
158 * Connection data response
159 * Format is the same as with connection data
163 * {CDR_OK[2] || CDR_EXECUTION_FAILED[2]}
164 * reasoncode[2] reasontextlength[2] reasontext[reasontextlength]
165 * reasontextlength may be 0
167 #define CDR_EXECOK 32768
168 #define CDR_EXECOK_OK 32769
170 #define CDR_EXECFAILED 32770
171 #define CDR_EXECFAILED_UNKNOWN_COMMAND 32771
172 #define CDR_EXECFAILED_PERMISSION_DENIED 32772
173 #define CDR_TEMPORARILY_OUT_OF_RESSOURCES 32773
174 #define CDR_EXECFAILED_CMD_TOO_SHORT 32774
175 #define CDR_EXECFAILED_CMD_TOO_LONG 32775
177 #define CDR_EXECFAILED_TARGETADDRTYPE_UNKNOWN 32776
178 #define CDR_EXECFAILED_TARGETADDR_DOESNTEXIST 32777
179 #define CDR_EXECFAILED_LISTENERQUEUE_FULL 32778
183 /* result codes for rcv.c/proc_packet */
185 #define RC_FINISHED 1
187 #define RC_RCV1_ANNOUNCE 2
188 #define RC_RCV1_KERNEL 3
189 #define RC_RCV1_CONN 4
193 struct ref_counter_def
{
195 * implemented by user, frees struct containing the mem_counter when
198 void (*free
)(struct ref_counter
*cnt
);
204 /* initialised with 1 */
207 /* initialised with 1, if 0 deny mem_counter_incr */
210 /* should be a pointer to a global variable */
211 struct ref_counter_def
*def
;
215 struct htab_entry
*next
;
219 struct htab_entry
**htable
;
224 int (*matches
)(void *htentry
, void *searcheditem
);
227 __u32 ref_counter_offset
;
231 struct list_head nb_list
;
233 struct ref_counter refs
;
235 struct net_device
*dev
;
236 char mac
[MAX_ADDR_LEN
];
241 struct timer_list cmsg_timer
;
242 spinlock_t cmsg_lock
;
243 struct list_head control_msgs_out
;
244 unsigned long timedue
;
252 * connecions which receive data from/send data to this node
253 * used when terminating all connections of a neighbor
255 struct mutex conn_list_lock
;
256 struct list_head rcv_conn_list
;
257 struct list_head snd_conn_list
;
260 * the timer has to be inited when adding the neighbor
261 * init_timer(struct timer_list * timer);
262 * add_timer(struct timer_list * timer);
264 spinlock_t retrans_lock
;
265 struct timer_list retrans_timer
;
268 * next_retransmit are linked with
269 * skb_procstate->funcstate.retransmit_queue
270 * because the sk_buff next/prev fields are needed by the hashtable
272 struct sk_buff_head retrans_list
;
274 struct conn
*firstboundconn
;
277 struct cor_sched_data
{
279 struct list_head conn_list
;
280 struct sk_buff_head requeue_queue
;
283 /* these are the non sent, non resized packets */
287 struct sk_buff_head buf
;
290 #define RINGBUFFER_PAGES 8
292 char *buffers
[RINGBUFFER_PAGES
];
298 /* The first 2 members of connlistener/conn (see sock.c) */
299 struct ref_counter refs
;
307 struct connlistener
*owner
;
311 #define SOCKSTATE_LISTENER 1
312 #define SOCKSTATE_CONN 2
314 struct connlistener
{
315 /* The first 2 members have to be the same as in conn (see sock.c) */
316 struct ref_counter refs
;
322 struct list_head conn_queue
;
323 wait_queue_head_t wait
;
328 * There are 2 conn objects per bi-directional connection. They refer to each
329 * other with in the reversedir field. To distinguish them, the variables on
330 * the stack are usually called rconn and sconn. rconn refers to the conn object
331 * which has received a command. sconn is the other conn object. This means that
332 * in send functions rconn means the connection we want to send the command to.
337 * The first 2 members have to be the same as in connlistener (see
340 struct ref_counter refs
;
343 #define SOURCE_NONE 0
345 #define SOURCE_SOCK 2
347 #define TARGET_UNCONNECTED 0
349 #define TARGET_SOCK 2
350 #define TARGET_KERNEL 3
357 spinlock_t credit_lock
;
359 psched_time_t last_rcv_time
;
360 psched_time_t last_snd_time
;
361 // PSCHED_GET_TIME(now);
364 __u32 bytes_queued_avg
;
373 /* credit status of the sender/recp (for balancing credits)*/
385 /* list of all connections from this neighbor */
386 struct list_head nb_list
;
388 struct mutex rcv_lock
;
389 struct sk_buff_head reorder_queue
;
391 struct htab_entry htab_entry
;
399 wait_queue_head_t wait
;
403 struct list_head cl_list
;
416 /* has to be first (because it is first in target
420 /* list of all connections to this neighbor */
421 struct list_head nb_list
;
423 struct mutex seqno_lock
;
426 struct list_head allconnids
;
430 /* see packet id definition */
433 struct resize_buf rb
;/* see rcv.c */
434 struct sk_buff
*skb
;/* see sock.c */
436 atomic_t inflight_packets
;
438 spinlock_t qdisc_lock
;
439 struct conn
*nextactive
;
440 struct conn
*prevactive
;
441 struct sk_buff_head queue
;
442 struct list_head queue_list
;
448 wait_queue_head_t wait
;
452 struct ringbuffer rbuf
;
454 struct sk_buff_head queue
;
458 /* kernel packet - no connection */
463 struct conn
*reversedir
;
467 struct skb_procstate
{
472 struct work_struct work
;
485 struct htab_entry htab_entry
;
486 struct ref_counter refs
;
487 unsigned long timeout
;
507 static inline __u32
data_len(struct data
*data
) {
508 if (data
->type
== TYPE_BUF
) {
509 return data
->data
.buf
.len
;
510 } else if (data
->type
== TYPE_SKB
) {
511 BUG_ON(data
->data
.skb
->len
< 0);
512 return data
->data
.skb
->len
;
518 static inline char * data_pull(struct data
*data
, int len
) {
519 if (data
->type
== TYPE_BUF
) {
520 char *ret
= data
->data
.buf
.buf
;
521 if (unlikely(data
->data
.skb
->len
< len
))
524 data
->data
.buf
.buf
+= len
;
525 data
->data
.skb
->len
-= len
;
527 } else if (data
->type
== TYPE_SKB
) {
528 return skb_pull(data
->data
.skb
, len
);
536 extern void ref_counter_decr(struct ref_counter
*cnt
);
538 extern int ref_counter_incr(struct ref_counter
*cnt
);
540 extern void ref_counter_deactivate(struct ref_counter
*cnt
);
542 extern void ref_counter_init(struct ref_counter
*cnt
,
543 struct ref_counter_def
*def
);
545 extern char *htable_get(struct htable
*ht
, __u32 key
, void *searcheditem
);
547 extern int htable_delete(struct htable
*ht
, char *oldelement
, __u32 key
,
550 extern void htable_insert(struct htable
*ht
, char *newelement
, __u32 key
);
552 extern void htable_init(struct htable
*ht
, int (*matches
)(void *htentry
,
553 void *searcheditem
), __u32 entry_offset
,
554 __u32 ref_counter_offset
);
563 extern struct conn
*get_conn(__u32 conn_id
);
565 extern int ringbuffer_put(struct ringbuffer
*buf
, char *src
, int len
,
568 extern int ringbuffer_maypush(struct ringbuffer
*buf
);
570 extern int ringbuffer_pull(struct ringbuffer
*buf
, char *dest
, int len
,
573 extern int ringbuffer_maypull(struct ringbuffer
*buf
);
575 extern int conn_init_out(struct conn
*rconn
, struct neighbor
*nb
);
577 extern void close_port(struct connlistener
*listener
);
579 extern struct connlistener
*open_port(__be64 port
);
581 extern int connect_port(struct conn
*rconn
, __be64 port
);
583 extern int connect_neigh(struct conn
*rconn
,
584 __u16 addrtypelen
, __u8
*addrtype
,
585 __u16 addrlen
, __u8
*addr
);
587 extern struct conn
* alloc_conn(gfp_t allocflags
);
589 extern void reset_conn(struct conn
*conn
);
592 extern struct neighbor
*find_neigh(__u16 addrtypelen
, __u8
*addrtype
,
593 __u16 addrlen
, __u8
*addr
);
595 extern void rcv_announce(struct sk_buff
*skb
);
597 extern void blacklist_neighbor(struct neighbor
*nb
);
600 extern void drain_ooo_queue(struct conn
*rconn
);
602 extern void conn_rcv_buildskb(char *data
, __u32 datalen
, __u32 conn_id
,
605 /* kpacket_parse.c */
606 extern void kernel_packet(struct sk_buff
*skb
, __u32 seqno
);
609 extern void send_reset_conn(struct neighbor
*nb
, __u32 conn_id
);
611 extern void send_ack(struct neighbor
*nb
, __u32 conn_id
, __u32 seqno
);
613 extern void send_connect_failed_temp(struct neighbor
*nb
, __u32 conn_id
);
615 extern void send_connect_success(struct conn
*rconn
);
617 extern void send_connect_nb(struct conn
*rconn
);
619 extern void controlmsg_timerfunc(unsigned long arg
);
621 /* cpacket_parse.c */
622 extern void parse(struct data
*data
, struct conn
*conn
);
625 extern void retransmit_timerfunc(unsigned long arg
);
627 extern struct sk_buff
*create_packet_conn(struct conn
*target
, int size
,
630 extern struct sk_buff
*create_packet_kernel(struct neighbor
*nb
, int size
,
633 extern void send_packet(struct sk_buff
*skb
, struct neighbor
*nb
);
635 extern void ack_received(struct neighbor
*nb
, __u32 conn_id
, __u32 seqno
,
638 static inline struct skb_procstate
*skb_pstate(struct sk_buff
*skb
)
640 return (struct skb_procstate
*) &(skb
->cb
[0]);
643 static inline struct sk_buff
*skb_from_pstate(struct skb_procstate
*ps
)
645 return ((struct sk_buff
*) ((char *)ps
) - offsetof(struct sk_buff
,cb
));
649 static inline __u32
mss(struct neighbor
*nb
)
651 return nb
->dev
->mtu
- LL_RESERVED_SPACE(nb
->dev
) - 17;