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 <linux/spinlock.h>
27 #include <linux/workqueue.h>
28 #include <linux/kref.h>
34 #define PIDOUT_NEWCONN 16
35 #define PIDOUT_SENDDEF_THRES 8
36 #define PIDOUT_SENDDEF_COUNT 16
40 #define ETH_P_COR 0x1022
44 #define SOCKADDRTYPE_PORT 1
53 #define MAX_CONN_CMD_LEN 4096
56 #define PACKET_TYPE_ANNOUNCE 1
57 #define PACKET_TYPE_DATA 2
60 * Kernel packet data - these commands are sent by the neighbor
61 * The end nodes may cause these commands to be sent, but they see them beyond
69 * KP_PING[1] cookie[4]
70 * KP_PONG[1] cookie[4] respdelay[4]
72 * This is needed to find out whether the other node is reachable. After a new
73 * neighbor is seen, ping requests are sent and the neighbor is only reachable
74 * after a few pongs are received. These requests are also used to find out
75 * whether a neighber is gone.
78 * The receiver of a ping may delay the sending of the pong e.g. to create
79 * bigger kernel packets. The respdelay is the time in microseconds the packet
85 /* KP_ACK[1] sent_conn_id[4] seqno[4]
87 * sent_conn_id means that this is *not* the conn_id we use if we sent something
88 * through this conn, but the conn_id that the neighbor used to send us the
94 * KP_SPEED[1] conn_id[4] speedinfo[2]
97 * buffer_state_value = speedinfo % 181
98 * speed_value = speedinfo / 181
100 * buffer_state = 1024 * pow(2, buffer_state_value/3.0)
101 * speed = 1024 * pow(2, speed_value/12.0)
104 * This has to be done either with floating points (which is no so nice) or
106 * buffer_state = pow(2, value/3) *
107 * 1024 * pow(pow(2, 1.0/3), buffer_state_value%3)
108 * where 1024 * pow(pow(2, 1.0/4), value%3) can be just a table lookup
109 * (the "1024" should be part of the value in the table, because it increases
112 * you can do the same with the speed
115 * Some values have special meanings:
116 * if speedinfo is the highest possible value(65535), it means both values
118 * if buffer_state_value if > 91, you have to subtract 90 and make the
119 * resulting buffer_state negative
123 /* NOTE on connection ids:
124 * connection ids we send are used for the receive channel
125 * connection ids we receive are used for the send channel
129 * incoming connection
130 * KP_CONNECT[1] conn_id[4]
135 * incoming connection successful,
136 * the first conn_id is the same as previously sent/received in KP_CONNECT
137 * the second conn_id is generated by us and used for the other direction
138 * KP_CONNECT_SUCCESS[1] conn_id[4] conn_id[4]
140 #define KP_CONNECT_SUCCESS 7
142 /* KP_CONN_DATA[1] conn_id[4] seqno[4] length[2] data[length] */
143 #define KP_CONN_DATA 8
146 * { KP_RESET_CONN[1] conn_id[4] }
147 * We send this, if there is an established connection we want to close.
149 #define KP_RESET_CONN 9
153 * Connection data which in interpreted when connection has no target yet
154 * These commands are sent by the end node.
157 * cmd[2] length[4] parameter[length]
158 * unrecogniced commands are ignored
159 * parameters which are longer than expected are ignored as well
162 /* outgoing connection: CD_CONNECT_NB[2] length[4]
163 * addrtypelen[2] addrlen[2] addrtype[addrtypelen] addr[addrlen] */
164 #define CD_CONNECT_NB 1
166 /* connection to local open part: CD_CONNECT_PORT[2] length[4] port[8] */
167 #define CD_CONNECT_PORT 2
170 * CD_LIST_NEIGH sends CDR_BINDATA if the command was successful. The response
173 * totalneighs[4] response_rows[4]
175 * numaddr[2] (addrtypelen[2] addrlen[2] addrtype[addrtypelen] addr[addrlen]
178 * Neighbors have to be sorted by uptime, new neighbors first. This is so that
179 * the routing daemon can easily find out whether there are new neighbors. It
180 * only needs to send a query with offset 0. If the totalneighs stays the same
181 * while new were added, a connection to another neighbor was lost.
184 /* list connected neighbors: CD_LIST_NEIGH[2] length[4] limit[4] offset[4] */
185 #define CD_LIST_NEIGH 3
188 * Connection data response
189 * Format is the same as with connection data
193 * {CDR_EXECOK[2] || CDR_EXECFAILED[2]}
194 * reasoncode[2] reasontextlength[2] reasontext[reasontextlength]
195 * reasontextlength may be 0
197 #define CDR_EXECOK 32768
198 #define CDR_EXECOK_OK 33024
200 #define CDR_EXECFAILED 32769
201 #define CDR_EXECFAILED_UNKNOWN_COMMAND 33280
202 #define CDR_EXECFAILED_PERMISSION_DENIED 33281
203 #define CDR_EXECFAILED_TEMPORARILY_OUT_OF_RESSOURCES 33282
204 #define CDR_EXECFAILED_CMD_TOO_SHORT 33283
205 #define CDR_EXECFAILED_CMD_TOO_LONG 33284
206 #define CDR_EXECFAILED_TARGETADDRTYPE_UNKNOWN 33285
207 #define CDR_EXECFAILED_TARGETADDR_DOESNTEXIST 33286
208 #define CDR_EXECFAILED_TARGETADDR_PORTCLOSED 33287
209 #define CDR_EXECFAILED_LISTENERQUEUE_FULL 33288
212 * must be sent after CDR_EXEC{OK|FAILED}
213 * CDR_EXEOK_BINDATA[2] bindatalen[4] bindata[bindatalen] */
214 #define CDR_BINDATA 32770
217 /* result codes for rcv.c/proc_packet */
219 #define RC_FINISHED 1
221 #define RC_RCV1_ANNOUNCE 2
222 #define RC_RCV1_KERNEL 3
223 #define RC_RCV1_CONN 4
226 /* start of next element, *not* next htab_entry */
231 struct htab_entry
**htable
;
236 int (*matches
)(void *htentry
, void *searcheditem
);
245 __u8 pongs
; /* count of pongs for pings sent after this one */
248 #define NEIGHBOR_STATE_INITIAL 0
249 #define NEIGHBOR_STATE_ACTIVE 1
250 #define NEIGHBOR_STATE_STALLED 2
253 struct list_head nb_list
;
257 struct net_device
*dev
;
258 char mac
[MAX_ADDR_LEN
];
263 struct delayed_work cmsg_timer
;
264 struct mutex cmsg_lock
;
265 struct list_head control_msgs_out
;
270 unsigned long last_ping_time
; /* protected by cmsg_lock */
271 __u32 noping_cnt
;/* protected by cmsg_lock */
273 struct mutex pingcookie_lock
;
274 __u32 ping_intransit
;
275 struct ping_cookie cookies
[PING_COOKIES_PER_NEIGH
];
277 atomic_t latency
; /* microsecs */
279 struct mutex state_lock
;
281 __u64 last_state_change
;/* initial state */
284 * time of the last sent packet which has been acked or
285 * otherwise responded to (e.g. pong)
287 unsigned long last_roundtrip
;/* active/stalled state */
292 struct delayed_work stalltimeout_timer
;
293 __u8 str_timer_pending
;
296 atomic_t kpacket_seqno
;
297 atomic_t ooo_packets
;
300 * connecions which receive data from/send data to this node
301 * used when terminating all connections of a neighbor
303 struct mutex conn_list_lock
;
304 struct list_head rcv_conn_list
;
305 struct list_head snd_conn_list
;
308 * the timer has to be inited when adding the neighbor
309 * init_timer(struct timer_list * timer);
310 * add_timer(struct timer_list * timer);
312 spinlock_t retrans_lock
;
313 struct timer_list retrans_timer
;
316 * next_retransmit are linked with
317 * skb_procstate->funcstate.retransmit_queue
318 * because the sk_buff next/prev fields are needed by the hashtable
320 struct sk_buff_head retrans_list
;
322 struct conn
*firstboundconn
;
325 struct cor_sched_data
{
327 struct list_head conn_list
;
328 struct sk_buff_head requeue_queue
;
334 struct data_buf_item
{
335 struct list_head buf_list
;
351 struct list_head items
;
352 struct data_buf_item
*lastread
;
357 __u32 read_remaining
;
359 __u16 last_read_offset
;
368 struct connlistener
*owner
;
372 #define SOCKSTATE_LISTENER 1
373 #define SOCKSTATE_CONN 2
376 /* The first member of connlistener/conn (see sock.c) */
380 struct connlistener
{
381 /* The first member has to be the same as in conn (see sock.c) */
387 struct list_head conn_queue
;
388 wait_queue_head_t wait
;
393 * There are 2 conn objects per bi-directional connection. They refer to each
394 * other with in the reversedir field. To distinguish them, the variables on
395 * the stack are usually called rconn and sconn. rconn refers to the conn object
396 * which has received a command. sconn is the other conn object. This means that
397 * in send functions rconn means the connection we want to send the command to.
401 /* The first member has to be the same as in connlistener (see sock.c)*/
404 #define SOURCE_NONE 0
406 #define SOURCE_SOCK 2
408 #define TARGET_UNCONNECTED 0
410 #define TARGET_SOCK 2
416 struct list_head queue_list
;
420 struct mutex rcv_lock
;
431 /* list of all connections from this neighbor */
432 struct list_head nb_list
;
434 struct sk_buff_head reorder_queue
;
436 struct htab_entry htab_entry
;
443 struct list_head cl_list
;
444 wait_queue_head_t wait
;
457 __u32 stall_timeout_ms
;
461 /* has to be first (because it is first in target
465 /* list of all connections to this neighbor */
466 struct list_head nb_list
;
471 __u32 stall_timeout_ms
;
475 wait_queue_head_t wait
;
481 struct conn
*reversedir
;
485 struct skb_procstate
{
490 struct work_struct work
;
503 struct htab_entry htab_entry
;
505 unsigned long timeout
;
515 extern char *htable_get(struct htable
*ht
, __u32 key
, void *searcheditem
);
517 extern int htable_delete(struct htable
*ht
, __u32 key
, void *searcheditem
,
518 void (*free
) (struct kref
*ref
));
520 extern void htable_insert(struct htable
*ht
, char *newelement
, __u32 key
);
522 extern void htable_init(struct htable
*ht
, int (*matches
)(void *htentry
,
523 void *searcheditem
), __u32 entry_offset
,
526 extern struct conn
*get_conn(__u32 conn_id
);
528 extern void free_conn(struct kref
*ref
);
530 extern int conn_init_out(struct conn
*rconn
, struct neighbor
*nb
);
532 extern void conn_init_sock_source(struct conn
*conn
);
533 extern void conn_init_sock_target(struct conn
*conn
);
535 extern void close_port(struct connlistener
*listener
);
537 extern struct connlistener
*open_port(__be64 port
);
539 extern int connect_port(struct conn
*rconn
, __be64 port
);
541 extern int connect_neigh(struct conn
*rconn
,
542 __u16 addrtypelen
, __u8
*addrtype
,
543 __u16 addrlen
, __u8
*addr
);
545 extern struct conn
* alloc_conn(gfp_t allocflags
);
547 extern void reset_conn(struct conn
*conn
);
550 extern void neighbor_free(struct kref
*ref
);
552 extern struct neighbor
*get_neigh_by_mac(struct sk_buff
*skb
);
554 extern struct neighbor
*find_neigh(__u16 addrtypelen
, __u8
*addrtype
,
555 __u16 addrlen
, __u8
*addr
);
557 extern __u32
generate_neigh_list(char *buf
, __u32 buflen
, __u32 limit
,
560 extern int get_neigh_state(struct neighbor
*nb
);
562 extern void ping_resp(struct neighbor
*nb
, __u32 cookie
, __u32 respdelay
);
564 extern __u32
add_ping_req(struct neighbor
*nb
);
566 extern int time_to_send_ping(struct neighbor
*nb
);
568 extern int force_ping(struct neighbor
*nb
);
570 extern void rcv_announce(struct sk_buff
*skb
);
572 extern int __init
cor_neighbor_init(void);
575 extern void drain_ooo_queue(struct conn
*rconn
);
577 extern void conn_rcv_buildskb(char *data
, __u32 datalen
, __u32 conn_id
,
580 extern int __init
cor_rcv_init(void);
582 /* kpacket_parse.c */
583 extern void kernel_packet(struct neighbor
*nb
, struct sk_buff
*skb
, __u32 seqno
);
586 extern void schedule_controlmsg_timerfunc(struct neighbor
*nb
);
588 struct control_msg_out
;
590 extern struct control_msg_out
*alloc_control_msg(void);
592 extern void free_control_msg(struct control_msg_out
*cm
);
594 extern void send_pong(struct control_msg_out
*cm
, struct neighbor
*nb
,
597 extern void send_reset_conn(struct control_msg_out
*cm
, struct neighbor
*nb
,
600 extern void send_ack(struct control_msg_out
*cm
, struct neighbor
*nb
,
601 __u32 conn_id
, __u32 seqno
);
603 extern void send_connect_success(struct control_msg_out
*cm
,
604 struct neighbor
*nb
, __u32 rcvd_conn_id
, __u32 gen_conn_id
);
606 extern void send_connect_nb(struct control_msg_out
*cm
, struct neighbor
*nb
,
609 extern void send_conndata(struct control_msg_out
*cm
, struct neighbor
*nb
,
610 __u32 connid
, __u32 seqno
, char *data_orig
, char *data
,
613 /* cpacket_parse.c */
614 extern void parse(struct conn
*rconn
);
617 extern void retransmit_timerfunc(unsigned long arg
);
619 extern struct sk_buff
*create_packet_conn(struct conn
*target
, int size
,
622 extern struct sk_buff
*create_packet_kernel(struct neighbor
*nb
, int size
,
625 extern void send_conn_flushdata(struct conn
*rconn
, char *data
, __u32 datalen
);
627 extern void send_packet(struct sk_buff
*skb
, struct neighbor
*nb
,
630 extern void ack_received(struct neighbor
*nb
, __u32 conn_id
, __u32 seqno
);
632 extern void flush_out(struct conn
*rconn
);
634 extern int __init
cor_snd_init(void);
637 extern void databuf_pull(struct data_buf
*data
, char *dst
, int len
);
639 extern size_t databuf_pulluser(struct conn
*sconn
, struct msghdr
*msg
);
641 extern void databuf_ack(struct data_buf
*buf
, __u64 pos
);
643 extern void databuf_ackread(struct data_buf
*buf
);
645 extern int databuf_maypush(struct data_buf
*buf
);
647 extern void databuf_free(struct data_buf
*data
);
649 extern void databuf_init(struct data_buf
*data
);
651 extern int receive_userbuf(struct conn
*rconn
, struct msghdr
*msg
);
653 extern void receive_buf(struct conn
*rconn
, char *buf
, int len
);
655 extern int receive_skb(struct conn
*rconn
, struct sk_buff
*skb
);
657 extern void wake_sender(struct conn
*rconn
);
659 extern void forward_init(void);
663 static inline struct skb_procstate
*skb_pstate(struct sk_buff
*skb
)
665 return (struct skb_procstate
*) &(skb
->cb
[0]);
668 static inline struct sk_buff
*skb_from_pstate(struct skb_procstate
*ps
)
670 return (struct sk_buff
*) (((char *)ps
) - offsetof(struct sk_buff
,cb
));
674 static inline __u32
mss(struct neighbor
*nb
)
676 return nb
->dev
->mtu
- LL_RESERVED_SPACE(nb
->dev
) - 9;
680 static inline void put_u64(char *dst
, __u64 value
, int convbo
)
682 char *p_value
= (char *) &value
;
685 value
= cpu_to_be64(value
);
697 static inline void put_u32(char *dst
, __u32 value
, int convbo
)
699 char *p_value
= (char *) &value
;
702 value
= cpu_to_be32(value
);
710 static inline void put_u16(char *dst
, __u16 value
, int convbo
)
712 char *p_value
= (char *) &value
;
715 value
= cpu_to_be16(value
);
721 static inline char *cor_pull_skb(struct sk_buff
*skb
, unsigned int len
)
723 char *ptr
= skb_pull(skb
, len
);