kernel message sending bugfixes, connection specific timeouts
[cor_2_6_31.git] / net / cor / cor.h
blob600b88e4ccc48f7371ffb84757ebaa6453abf6f1
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 <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>
30 #include "settings.h"
33 /* options */
34 #define PIDOUT_NEWCONN 16
35 #define PIDOUT_SENDDEF_THRES 8
36 #define PIDOUT_SENDDEF_COUNT 16
40 #define ETH_P_COR 0x1022
41 #define AF_COR 37
42 #define PF_COR AF_COR
44 #define SOCKADDRTYPE_PORT 1
45 struct cor_sockaddr {
46 int type;
48 union {
49 __be64 port;
50 } addr;
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
62 * the first hop.
65 /* KP_PADDING[1] */
66 #define KP_PADDING 1
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.
77 * respdelay:
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
80 * was delayed.
82 #define KP_PING 2
83 #define KP_PONG 3
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
89 * packet
91 #define KP_ACK 4
94 * KP_SPEED[1] conn_id[4] speedinfo[2]
96 * 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)
102 * see the .0 ...
104 * This has to be done either with floating points (which is no so nice) or
105 * you can calculate:
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
110 * the accuracy)
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
117 * are inifinite
118 * if buffer_state_value if > 91, you have to subtract 90 and make the
119 * resulting buffer_state negative
121 #define KP_SPEED 5
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]
132 #define KP_CONNECT 6
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.
156 * Format:
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
171 * format is:
173 * totalneighs[4] response_rows[4]
174 * for every row:
175 * numaddr[2] (addrtypelen[2] addrlen[2] addrtype[addrtypelen] addr[addrlen]
176 * )[numaddr]
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 * CD_SET_(FORWARD|BACKWARD)_TIMEOUT[2] length[4] timeout_ms[4]
190 * If there is no successful communication with the previous or neighbor for
191 * this period, the connection will be reset. This value must be between
192 * NB_STALL_TIME and NB_KILL_TIME. Otherwise it will silently behave as if it
193 * was set to exactly one of these limits.
195 #define CD_SET_FORWARD_TIMEOUT 4
196 #define CD_SET_BACKWARD_TIMEOUT 5
199 * Connection data response
200 * Format is the same as with connection data
204 * {CDR_EXECOK[2] || CDR_EXECFAILED[2]}
205 * reasoncode[2] reasontextlength[2] reasontext[reasontextlength]
206 * reasontextlength may be 0
208 #define CDR_EXECOK 32768
209 #define CDR_EXECOK_OK 33024
212 #define CDR_EXECFAILED 32769
213 #define CDR_EXECFAILED_UNKNOWN_COMMAND 33280
214 #define CDR_EXECFAILED_PERMISSION_DENIED 33281
215 #define CDR_EXECFAILED_TEMPORARILY_OUT_OF_RESSOURCES 33282
216 #define CDR_EXECFAILED_CMD_TOO_SHORT 33283
217 #define CDR_EXECFAILED_CMD_TOO_LONG 33284
218 #define CDR_EXECFAILED_TARGETADDRTYPE_UNKNOWN 33285
219 #define CDR_EXECFAILED_TARGETADDR_DOESNTEXIST 33286
220 #define CDR_EXECFAILED_TARGETADDR_PORTCLOSED 33287
221 #define CDR_EXECFAILED_LISTENERQUEUE_FULL 33288
222 #define CDR_EXECFAILED_ILLEGAL_COMMAND 33289
225 * must be sent after CDR_EXEC{OK|FAILED}
226 * CDR_EXEOK_BINDATA[2] bindatalen[4] bindata[bindatalen] */
227 #define CDR_BINDATA 32770
230 /* result codes for rcv.c/proc_packet */
231 #define RC_DROP 0
232 #define RC_FINISHED 1
234 #define RC_RCV1_ANNOUNCE 2
235 #define RC_RCV1_KERNEL 3
236 #define RC_RCV1_CONN 4
238 struct htab_entry{
239 /* start of next element, *not* next htab_entry */
240 void *next;
243 struct htable{
244 struct htab_entry **htable;
245 __u32 htable_size;
246 __u32 cell_size;
247 __u32 num_elements;
249 int (*matches)(void *htentry, void *searcheditem);
250 __u32 key_offset;
251 __u32 entry_offset;
252 __u32 kref_offset;
255 struct ping_cookie{
256 unsigned long time;
257 __u32 cookie;
258 __u8 pongs; /* count of pongs for pings sent after this one */
261 #define NEIGHBOR_STATE_INITIAL 0
262 #define NEIGHBOR_STATE_ACTIVE 1
263 #define NEIGHBOR_STATE_STALLED 2
264 #define NEIGHBOR_STATE_KILLED 3
266 struct neighbor{
267 struct list_head nb_list;
269 struct kref ref;
271 struct net_device *dev;
272 char mac[MAX_ADDR_LEN];
274 char *addr;
275 __u16 addrlen;
277 struct delayed_work cmsg_timer;
278 struct mutex cmsg_lock;
279 struct list_head control_msgs_out;
281 * urgent messages; These are sent even if the neighbor state is not
282 * active. If the queue gets full, the oldest ones are dropped. It thus
283 * may only contain messages which are allowed to be dropped.
285 struct list_head ucontrol_msgs_out;
286 __u64 timeout;
287 __u32 cmlength;
288 __u32 ucmlength;
291 unsigned long last_ping_time; /* protected by cmsg_lock */
292 __u32 noping_cnt;/* protected by cmsg_lock */
294 struct mutex pingcookie_lock;
295 __u32 ping_intransit;
296 struct ping_cookie cookies[PING_COOKIES_PER_NEIGH];
297 __u32 lastcookie;
298 atomic_t latency; /* microsecs */
300 spinlock_t state_lock;
301 union {
302 __u64 last_state_change;/* initial state */
304 * last_roundtrip:
305 * time of the last sent packet which has been acked or
306 * otherwise responded to (e.g. pong)
308 unsigned long last_roundtrip;/* active/stalled state */
309 }state_time;
310 __u8 state;
311 __u16 ping_success;
313 struct delayed_work stalltimeout_timer;
314 __u8 str_timer_pending;
317 atomic_t kpacket_seqno;
318 atomic_t ooo_packets;
321 * connecions which receive data from/send data to this node
322 * used when terminating all connections of a neighbor
324 struct mutex conn_list_lock;
325 struct list_head rcv_conn_list;
326 struct list_head snd_conn_list;
329 * the timer has to be inited when adding the neighbor
330 * init_timer(struct timer_list * timer);
331 * add_timer(struct timer_list * timer);
333 spinlock_t retrans_lock;
334 struct timer_list retrans_timer;
335 __u8 retrans_timer_running;
338 * next_retransmit are linked with
339 * skb_procstate->funcstate.retransmit_queue
340 * because the sk_buff next/prev fields are needed by the hashtable
342 struct sk_buff_head retrans_list;
344 struct conn *firstboundconn;
347 struct cor_sched_data{
348 spinlock_t lock;
349 struct list_head conn_list;
350 struct sk_buff_head requeue_queue;
353 #define TYPE_BUF 0
354 #define TYPE_SKB 1
356 struct data_buf_item{
357 struct list_head buf_list;
359 union {
360 struct {
361 char *buf;
362 __u32 datalen;
364 }buf;
366 struct sk_buff *skb;
367 }data;
369 __u8 type;
372 struct data_buf{
373 struct list_head items;
374 struct data_buf_item *lastread;
375 __u64 first_offset;
376 __u64 read_offset;
378 __u32 totalsize;
379 __u32 read_remaining;
381 __u16 last_read_offset;
383 __u16 last_buflen;
386 struct connlistener;
388 struct bindnode{
389 struct list_head lh;
390 struct connlistener *owner;
391 __be64 port;
394 #define SOCKSTATE_LISTENER 1
395 #define SOCKSTATE_CONN 2
397 struct sock_hdr {
398 /* The first member of connlistener/conn (see sock.c) */
399 __u8 sockstate;
402 struct connlistener {
403 /* The first member has to be the same as in conn (see sock.c) */
404 __u8 sockstate;
405 struct bindnode *bn;
406 struct mutex lock;
407 int queue_maxlen;
408 int queue_len;
409 struct list_head conn_queue;
410 wait_queue_head_t wait;
415 * There are 2 conn objects per bi-directional connection. They refer to each
416 * other with in the reversedir field. To distinguish them, the variables on
417 * the stack are usually called rconn and sconn. rconn refers to the conn object
418 * which has received a command. sconn is the other conn object. This means that
419 * in send functions rconn means the connection we want to send the command to.
422 struct conn{
423 /* The first member has to be the same as in connlistener (see sock.c)*/
424 __u8 sockstate;
426 #define SOURCE_NONE 0
427 #define SOURCE_IN 1
428 #define SOURCE_SOCK 2
430 #define TARGET_UNCONNECTED 0
431 #define TARGET_OUT 1
432 #define TARGET_SOCK 2
434 __u8 sourcetype:4,
435 targettype:4;
436 __u8 isreset;
437 __u8 qdisc_active;
438 struct list_head queue_list;
440 struct kref ref;
442 struct mutex rcv_lock;
444 /* state */
445 __u32 credits;
446 /* credit rate */
447 __s32 sender_crate;
448 __s32 resp_crate;
450 union{
451 struct{
452 struct neighbor *nb;
453 /* list of all connections from this neighbor */
454 struct list_head nb_list;
456 struct sk_buff_head reorder_queue;
458 struct htab_entry htab_entry;
459 __u32 conn_id;
460 __u32 next_seqno;
461 __u32 ooo_packets;
462 }in;
464 struct{
465 struct list_head cl_list;
466 wait_queue_head_t wait;
467 struct socket *sock;
468 int flags;
469 }sock;
470 }source;
472 union{
473 struct{
474 __u32 paramlen;
475 __u32 cmdread;
476 __u16 cmd;
477 __u8 *cmdparams;
479 __u32 stall_timeout_ms;
480 }unconnected;
482 struct{
483 /* has to be first (because it is first in target
484 * kernel too)
486 struct neighbor *nb;
487 /* list of all connections to this neighbor */
488 struct list_head nb_list;
490 __u32 conn_id;
491 __u32 seqno;
493 __u32 stall_timeout_ms;
494 }out;
496 struct{
497 wait_queue_head_t wait;
498 }sock;
499 }target;
501 struct data_buf buf;
503 struct conn *reversedir;
506 /* inside skb->cb */
507 struct skb_procstate{
508 struct conn *rconn;
510 union{
511 struct{
512 struct work_struct work;
513 }rcv;
515 struct{
516 __u32 offset;
517 }announce;
519 struct{
520 __u32 conn_id;
521 __u32 seqno;
522 }rcv2;
524 struct{
525 struct htab_entry htab_entry;
526 struct kref ref;
527 unsigned long timeout;
528 __u32 conn_id;
529 __u32 seqno;
530 struct neighbor *nb;
531 }retransmit_queue;
532 }funcstate;
536 /* common.c */
537 extern char *htable_get(struct htable *ht, __u32 key, void *searcheditem);
539 extern int htable_delete(struct htable *ht, __u32 key, void *searcheditem,
540 void (*free) (struct kref *ref));
542 extern void htable_insert(struct htable *ht, char *newelement, __u32 key);
544 extern void htable_init(struct htable *ht, int (*matches)(void *htentry,
545 void *searcheditem), __u32 entry_offset,
546 __u32 kref_offset);
548 extern struct conn *get_conn(__u32 conn_id);
550 extern void free_conn(struct kref *ref);
552 extern int conn_init_out(struct conn *rconn, struct neighbor *nb);
554 extern void conn_init_sock_source(struct conn *conn);
555 extern void conn_init_sock_target(struct conn *conn);
557 extern void close_port(struct connlistener *listener);
559 extern struct connlistener *open_port(__be64 port);
561 extern int connect_port(struct conn *rconn, __be64 port);
563 extern int connect_neigh(struct conn *rconn,
564 __u16 addrtypelen, __u8 *addrtype,
565 __u16 addrlen, __u8 *addr);
567 extern struct conn* alloc_conn(gfp_t allocflags);
569 extern void reset_conn(struct conn *conn);
571 /* neighbor.c */
572 extern void neighbor_free(struct kref *ref);
574 extern struct neighbor *get_neigh_by_mac(struct sk_buff *skb);
576 extern struct neighbor *find_neigh(__u16 addrtypelen, __u8 *addrtype,
577 __u16 addrlen, __u8 *addr);
579 extern __u32 generate_neigh_list(char *buf, __u32 buflen, __u32 limit,
580 __u32 offset);
582 extern int get_neigh_state(struct neighbor *nb);
584 extern void ping_resp(struct neighbor *nb, __u32 cookie, __u32 respdelay);
586 extern __u32 add_ping_req(struct neighbor *nb);
588 extern int time_to_send_ping(struct neighbor *nb);
590 extern int force_ping(struct neighbor *nb);
592 extern void rcv_announce(struct sk_buff *skb);
594 extern int __init cor_neighbor_init(void);
596 /* rcv.c */
597 extern void drain_ooo_queue(struct conn *rconn);
599 extern void conn_rcv_buildskb(char *data, __u32 datalen, __u32 conn_id,
600 __u32 seqno);
602 extern int __init cor_rcv_init(void);
604 /* kpacket_parse.c */
605 extern void kernel_packet(struct neighbor *nb, struct sk_buff *skb, __u32 seqno);
607 /* kpacket_gen.c */
608 extern void schedule_controlmsg_timerfunc(struct neighbor *nb);
610 struct control_msg_out;
612 extern struct control_msg_out *alloc_control_msg(void);
614 extern void free_control_msg(struct control_msg_out *cm);
616 extern void send_pong(struct control_msg_out *cm, struct neighbor *nb,
617 __u32 cookie);
619 extern void send_reset_conn(struct control_msg_out *cm, struct neighbor *nb,
620 __u32 conn_id);
622 extern void send_ack(struct control_msg_out *cm, struct neighbor *nb,
623 __u32 conn_id, __u32 seqno);
625 extern void send_connect_success(struct control_msg_out *cm,
626 struct neighbor *nb, __u32 rcvd_conn_id, __u32 gen_conn_id);
628 extern void send_connect_nb(struct control_msg_out *cm, struct neighbor *nb,
629 __u32 conn_id);
631 extern void send_conndata(struct control_msg_out *cm, struct neighbor *nb,
632 __u32 connid, __u32 seqno, char *data_orig, char *data,
633 __u32 datalen);
635 /* cpacket_parse.c */
636 extern void parse(struct conn *rconn);
638 /* snd.c */
639 extern void retransmit_timerfunc(unsigned long arg);
641 extern struct sk_buff *create_packet_conn(struct conn *target, int size,
642 gfp_t alloc_flags);
644 extern struct sk_buff *create_packet_kernel(struct neighbor *nb, int size,
645 gfp_t alloc_flags);
647 extern void send_conn_flushdata(struct conn *rconn, char *data, __u32 datalen);
649 extern void send_packet(struct sk_buff *skb, struct neighbor *nb,
650 int retransmit);
652 extern void ack_received(struct neighbor *nb, __u32 conn_id, __u32 seqno);
654 extern void flush_out(struct conn *rconn);
656 extern int __init cor_snd_init(void);
658 /* forward.c */
659 extern void databuf_pull(struct data_buf *data, char *dst, int len);
661 extern size_t databuf_pulluser(struct conn *sconn, struct msghdr *msg);
663 extern void databuf_ack(struct data_buf *buf, __u64 pos);
665 extern void databuf_ackread(struct data_buf *buf);
667 extern int databuf_maypush(struct data_buf *buf);
669 extern void databuf_free(struct data_buf *data);
671 extern void databuf_init(struct data_buf *data);
673 extern int receive_userbuf(struct conn *rconn, struct msghdr *msg);
675 extern void receive_buf(struct conn *rconn, char *buf, int len);
677 extern int receive_skb(struct conn *rconn, struct sk_buff *skb);
679 extern void wake_sender(struct conn *rconn);
681 extern void forward_init(void);
685 static inline struct skb_procstate *skb_pstate(struct sk_buff *skb)
687 return (struct skb_procstate *) &(skb->cb[0]);
690 static inline struct sk_buff *skb_from_pstate(struct skb_procstate *ps)
692 return (struct sk_buff *) (((char *)ps) - offsetof(struct sk_buff,cb));
696 static inline __u32 mss(struct neighbor *nb)
698 return nb->dev->mtu - LL_RESERVED_SPACE(nb->dev) - 9;
702 static inline void put_u64(char *dst, __u64 value, int convbo)
704 char *p_value = (char *) &value;
706 if (convbo)
707 value = cpu_to_be64(value);
709 dst[0] = p_value[0];
710 dst[1] = p_value[1];
711 dst[2] = p_value[2];
712 dst[3] = p_value[3];
713 dst[4] = p_value[4];
714 dst[5] = p_value[5];
715 dst[6] = p_value[6];
716 dst[7] = p_value[7];
719 static inline void put_u32(char *dst, __u32 value, int convbo)
721 char *p_value = (char *) &value;
723 if (convbo)
724 value = cpu_to_be32(value);
726 dst[0] = p_value[0];
727 dst[1] = p_value[1];
728 dst[2] = p_value[2];
729 dst[3] = p_value[3];
732 static inline void put_u16(char *dst, __u16 value, int convbo)
734 char *p_value = (char *) &value;
736 if (convbo)
737 value = cpu_to_be16(value);
739 dst[0] = p_value[0];
740 dst[1] = p_value[1];
743 static inline char *cor_pull_skb(struct sk_buff *skb, unsigned int len)
745 char *ptr = skb_pull(skb, len);
747 if(ptr == 0)
748 return 0;
750 return ptr - len;