retransmit loop bugfix
[cor_2_6_31.git] / net / cor / cor.h
blobcbf8aeee6518ff114033cf703d068d99275bcd38
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>
29 #include "settings.h"
32 /* options */
33 #define PIDOUT_NEWCONN 16
34 #define PIDOUT_SENDDEF_THRES 8
35 #define PIDOUT_SENDDEF_COUNT 16
39 #define ETH_P_COR 0x1022
40 #define AF_COR 37
41 #define PF_COR AF_COR
43 #define SOCKADDRTYPE_PORT 1
44 struct cor_sockaddr {
45 int type;
47 union {
48 __be64 port;
49 } addr;
52 #define MAX_CONN_CMD_LEN 4096
55 #define PACKET_TYPE_ANNOUNCE 1
56 #define PACKET_TYPE_DATA 2
59 * Kernel packet data - these commands are sent by the neighbor
60 * The end nodes may cause these commands to be sent, but they see them beyond
61 * the first hop.
64 /* KP_PADDING[1] */
65 #define KP_PADDING 1
67 /* KP_[N]ACK[1] sent_conn_id[4] seqno[4]
69 * sent_conn_id means that this is *not* the conn_id we use if we sent something
70 * through this conn, but the conn_id that the neighbor used to send us the
71 * packet
73 #define KP_ACK 2
76 * KP_SPEED[1] conn_id[4] speedinfo[2]
78 * speedinfo[2] =
79 * buffer_state_value = speedinfo % 181
80 * speed_value = speedinfo / 181
82 * buffer_state = 1024 * pow(2, buffer_state_value/3.0)
83 * speed = 1024 * pow(2, speed_value/12.0)
84 * see the .0 ...
86 * This has to be done either with floating points (which is no so nice) or
87 * you can calculate:
88 * buffer_state = pow(2, value/3) *
89 * 1024 * pow(pow(2, 1.0/3), buffer_state_value%3)
90 * where 1024 * pow(pow(2, 1.0/4), value%3) can be just a table lookup
91 * (the "1024" should be part of the value in the table, because it increases
92 * the accuracy)
94 * you can do the same with the speed
97 * Some values have special meanings:
98 * if speedinfo is the highest possible value(65535), it means both values
99 * are inifinite
100 * if buffer_state_value if > 91, you have to subtract 90 and make the
101 * resulting buffer_state negative
103 #define KP_SPEED 3
105 /* NOTE on connection ids:
106 * connection ids we send are used for the receive channel
107 * connection ids we receive are used for the send channel
111 * incoming connection
112 * KP_CONNECT[1] conn_id[4]
114 #define KP_CONNECT 4
117 * incoming connection successful,
118 * the first conn_id is the same as previously sent/received in KP_CONNECT
119 * the second conn_id is generated by us and used for the other direction
120 * KP_CONNECT_SUCCESS[1] conn_id[4] conn_id[4]
122 #define KP_CONNECT_SUCCESS 5
124 /* KP_CONN_DATA[1] conn_id[4] seqno[4] length[4] data[length] */
125 #define KP_CONN_DATA 6
128 * { KP_RESET_CONN[1] conn_id[4] }
129 * We send this, if there is an established connection we want to close.
131 #define KP_RESET_CONN 7
135 * Connection data which in interpreted when connection has no target yet
136 * These commands are sent by the end node.
138 * Format:
139 * cmd[2] length[4] parameter[length]
140 * unrecogniced commands are ignored
141 * parameters which are longer than expected are ignored as well
144 /* outgoing connection: CD_CONNECT_NB[2] length[4]
145 * addrtypelen[2] addrlen[2] addrtype[addrtypelen] addr[addrlen] */
146 #define CD_CONNECT_NB 1
148 /* connection to local open part: CD_CONNECT_PORT[2] length[4] port[8] */
149 #define CD_CONNECT_PORT 2
152 * CD_LIST_NEIGH sends CDR_BINDATA if the command was successful. The response
153 * format is:
155 * totalneighs[4] response_rows[4]
156 * for every row:
157 * numaddr[2] (addrtypelen[2] addrlen[2] addrtype[addrtypelen] addr[addrlen]
158 * )[numaddr]
160 * Neighbors have to be sorted by uptime, new neighbors first. This is so that
161 * the routing daemon can easily find out whether there are new neighbors. It
162 * only needs to send a query with offset 0. If the totalneighs stays the same
163 * while new were added, a connection to another neighbor was lost.
166 /* list connected neighbors: CD_LIST_NEIGH[2] length[4] limit[4] offset[4] */
167 #define CD_LIST_NEIGH 3
170 * Connection data response
171 * Format is the same as with connection data
175 * {CDR_EXECOK[2] || CDR_EXECFAILED[2]}
176 * reasoncode[2] reasontextlength[2] reasontext[reasontextlength]
177 * reasontextlength may be 0
179 #define CDR_EXECOK 32768
180 #define CDR_EXECOK_OK 33024
182 #define CDR_EXECFAILED 32769
183 #define CDR_EXECFAILED_UNKNOWN_COMMAND 33280
184 #define CDR_EXECFAILED_PERMISSION_DENIED 33281
185 #define CDR_EXECFAILED_TEMPORARILY_OUT_OF_RESSOURCES 33282
186 #define CDR_EXECFAILED_CMD_TOO_SHORT 33283
187 #define CDR_EXECFAILED_CMD_TOO_LONG 33284
188 #define CDR_EXECFAILED_TARGETADDRTYPE_UNKNOWN 33285
189 #define CDR_EXECFAILED_TARGETADDR_DOESNTEXIST 33286
190 #define CDR_EXECFAILED_TARGETADDR_PORTCLOSED 33287
191 #define CDR_EXECFAILED_LISTENERQUEUE_FULL 33288
194 * must be sent after CDR_EXEC{OK|FAILED}
195 * CDR_EXEOK_BINDATA[2] bindatalen[4] bindata[bindatalen] */
196 #define CDR_BINDATA 32770
199 /* result codes for rcv.c/proc_packet */
200 #define RC_DROP 0
201 #define RC_FINISHED 1
203 #define RC_RCV1_ANNOUNCE 2
204 #define RC_RCV1_KERNEL 3
205 #define RC_RCV1_CONN 4
207 struct ref_counter;
209 struct ref_counter_def{
211 * implemented by user, frees struct containing the mem_counter when
212 * refs == 0
214 void (*free)(struct ref_counter *cnt);
217 struct ref_counter{
218 spinlock_t lock;
220 /* initialised with 1 */
221 int refs;
223 /* should be a pointer to a global variable */
224 struct ref_counter_def *def;
227 struct htab_entry{
228 /* start of next element, *not* next htab_entry */
229 void *next;
232 struct htable{
233 struct htab_entry **htable;
234 __u32 htable_size;
235 __u32 cell_size;
236 __u32 num_elements;
238 int (*matches)(void *htentry, void *searcheditem);
239 __u32 key_offset;
240 __u32 entry_offset;
241 __u32 ref_counter_offset;
244 struct neighbor{
245 struct list_head nb_list;
247 struct ref_counter refs;
249 struct net_device *dev;
250 char mac[MAX_ADDR_LEN];
252 char *addr;
253 __u16 addrlen;
255 struct delayed_work cmsg_timer;
256 struct mutex cmsg_lock;
257 struct list_head control_msgs_out;
258 __u64 timeout;
259 __u32 length;
260 atomic_t kpacket_seqno;
262 atomic_t ooo_packets;
264 __u32 latency;
267 * connecions which receive data from/send data to this node
268 * used when terminating all connections of a neighbor
270 struct mutex conn_list_lock;
271 struct list_head rcv_conn_list;
272 struct list_head snd_conn_list;
275 * the timer has to be inited when adding the neighbor
276 * init_timer(struct timer_list * timer);
277 * add_timer(struct timer_list * timer);
279 spinlock_t retrans_lock;
280 struct timer_list retrans_timer;
283 * next_retransmit are linked with
284 * skb_procstate->funcstate.retransmit_queue
285 * because the sk_buff next/prev fields are needed by the hashtable
287 struct sk_buff_head retrans_list;
289 struct conn *firstboundconn;
292 struct cor_sched_data{
293 spinlock_t lock;
294 struct list_head conn_list;
295 struct sk_buff_head requeue_queue;
298 #define TYPE_BUF 0
299 #define TYPE_SKB 1
301 struct data_buf_item{
302 struct list_head buf_list;
304 union {
305 struct {
306 char *buf;
307 __u32 datalen;
309 }buf;
311 struct sk_buff *skb;
312 }data;
314 __u8 type;
317 struct data_buf{
318 struct list_head items;
319 struct data_buf_item *lastread;
320 __u64 first_offset;
321 __u64 read_offset;
323 __u32 totalsize;
324 __u32 read_remaining;
326 __u32 last_read_offset;
328 __u32 last_buflen;
331 struct connlistener;
333 struct bindnode{
334 struct list_head lh;
335 struct connlistener *owner;
336 __be64 port;
339 #define SOCKSTATE_LISTENER 1
340 #define SOCKSTATE_CONN 2
342 struct sock_hdr {
343 /* The first member of connlistener/conn (see sock.c) */
344 __u8 sockstate;
347 struct connlistener {
348 /* The first member has to be the same as in conn (see sock.c) */
349 __u8 sockstate;
350 struct bindnode *bn;
351 struct mutex lock;
352 int queue_maxlen;
353 int queue_len;
354 struct list_head conn_queue;
355 wait_queue_head_t wait;
360 * There are 2 conn objects per bi-directional connection. They refer to each
361 * other with in the reversedir field. To distinguish them, the variables on
362 * the stack are usually called rconn and sconn. rconn refers to the conn object
363 * which has received a command. sconn is the other conn object. This means that
364 * in send functions rconn means the connection we want to send the command to.
367 struct conn{
368 /* The first member has to be the same as in connlistener (see sock.c)*/
369 __u8 sockstate;
371 #define SOURCE_NONE 0
372 #define SOURCE_IN 1
373 #define SOURCE_SOCK 2
375 #define TARGET_UNCONNECTED 0
376 #define TARGET_OUT 1
377 #define TARGET_SOCK 2
379 __u8 sourcetype:4,
380 targettype:4;
381 __u8 isreset;
382 __u8 qdisc_active;
383 struct list_head queue_list;
385 /* todo: convert to kref */
386 struct ref_counter refs;
388 struct mutex rcv_lock;
390 /* state */
391 __u32 credits;
392 /* credit rate */
393 __s32 sender_crate;
394 __s32 resp_crate;
396 union{
397 struct{
398 struct neighbor *nb;
399 /* list of all connections from this neighbor */
400 struct list_head nb_list;
402 struct sk_buff_head reorder_queue;
404 struct htab_entry htab_entry;
405 __u32 conn_id;
406 __u32 next_seqno;
407 __u32 ooo_packets;
408 }in;
410 struct{
411 struct list_head cl_list;
412 wait_queue_head_t wait;
413 struct socket *sock;
414 int flags;
415 }sock;
416 }source;
418 union{
419 struct{
420 __u32 paramlen;
421 __u32 cmdread;
422 __u16 cmd;
423 __u8 *cmdparams;
424 }unconnected;
426 struct{
427 /* has to be first (because it is first in target
428 * kernel too)
430 struct neighbor *nb;
431 /* list of all connections to this neighbor */
432 struct list_head nb_list;
434 __u32 conn_id;
435 __u32 seqno;
436 }out;
438 struct{
439 wait_queue_head_t wait;
440 }sock;
441 }target;
443 struct data_buf buf;
445 struct conn *reversedir;
448 /* inside skb->cb */
449 struct skb_procstate{
450 struct conn *rconn;
452 union{
453 struct{
454 struct work_struct work;
455 }rcv;
457 struct{
458 __u32 offset;
459 }announce;
461 struct{
462 __u32 conn_id;
463 __u32 seqno;
464 }rcv2;
466 struct{
467 struct htab_entry htab_entry;
468 struct ref_counter refs;
469 unsigned long timeout;
470 __u32 conn_id;
471 __u32 seqno;
472 struct neighbor *nb;
473 }retransmit_queue;
474 }funcstate;
478 /* common.c */
479 extern void ref_counter_decr(struct ref_counter *cnt);
481 extern int ref_counter_incr(struct ref_counter *cnt);
483 extern void ref_counter_init(struct ref_counter *cnt,
484 struct ref_counter_def *def);
486 extern char *htable_get(struct htable *ht, __u32 key, void *searcheditem);
488 extern int htable_delete(struct htable *ht, __u32 key, void *searcheditem);
490 extern void htable_insert(struct htable *ht, char *newelement, __u32 key);
492 extern void htable_init(struct htable *ht, int (*matches)(void *htentry,
493 void *searcheditem), __u32 entry_offset,
494 __u32 ref_counter_offset);
496 extern struct conn *get_conn(__u32 conn_id);
498 extern int conn_init_out(struct conn *rconn, struct neighbor *nb);
500 extern void conn_init_sock_source(struct conn *conn);
501 extern void conn_init_sock_target(struct conn *conn);
503 extern void close_port(struct connlistener *listener);
505 extern struct connlistener *open_port(__be64 port);
507 extern int connect_port(struct conn *rconn, __be64 port);
509 extern int connect_neigh(struct conn *rconn,
510 __u16 addrtypelen, __u8 *addrtype,
511 __u16 addrlen, __u8 *addr);
513 extern struct conn* alloc_conn(gfp_t allocflags);
515 extern void reset_conn(struct conn *conn);
517 /* neighbor.c */
518 extern struct neighbor *get_neigh_by_mac(struct sk_buff *skb);
520 extern struct neighbor *find_neigh(__u16 addrtypelen, __u8 *addrtype,
521 __u16 addrlen, __u8 *addr);
523 extern __u32 generate_neigh_list(char *buf, __u32 buflen, __u32 limit,
524 __u32 offset);
526 extern void rcv_announce(struct sk_buff *skb);
528 extern int __init cor_neighbor_init(void);
530 /* rcv.c */
531 extern void drain_ooo_queue(struct conn *rconn);
533 extern void conn_rcv_buildskb(char *data, __u32 datalen, __u32 conn_id,
534 __u32 seqno);
536 extern int __init cor_rcv_init(void);
538 /* kpacket_parse.c */
539 extern void kernel_packet(struct neighbor *nb, struct sk_buff *skb, __u32 seqno);
541 /* kpacket_gen.c */
542 extern void schedule_controlmsg_timerfunc(struct neighbor *nb);
544 struct control_msg_out;
546 extern struct control_msg_out *alloc_control_msg(void);
548 extern void free_control_msg(struct control_msg_out *cm);
550 extern void send_reset_conn(struct control_msg_out *cm, struct neighbor *nb,
551 __u32 conn_id);
553 extern void send_ack(struct control_msg_out *cm, struct neighbor *nb,
554 __u32 conn_id, __u32 seqno);
556 extern void send_connect_success(struct control_msg_out *cm,
557 struct neighbor *nb, __u32 rcvd_conn_id, __u32 gen_conn_id);
559 extern void send_connect_nb(struct control_msg_out *cm, struct neighbor *nb,
560 __u32 conn_id);
562 extern void send_conndata(struct control_msg_out *cm, struct neighbor *nb,
563 __u32 connid, __u32 seqno, char *data_orig, char *data,
564 __u32 datalen);
566 /* cpacket_parse.c */
567 extern void parse(struct conn *rconn);
569 /* snd.c */
570 extern void retransmit_timerfunc(unsigned long arg);
572 extern struct sk_buff *create_packet_conn(struct conn *target, int size,
573 gfp_t alloc_flags);
575 extern struct sk_buff *create_packet_kernel(struct neighbor *nb, int size,
576 gfp_t alloc_flags);
578 extern void send_conn_flushdata(struct conn *rconn, char *data, __u32 datalen);
580 extern void send_packet(struct sk_buff *skb, struct neighbor *nb,
581 int retransmit);
583 extern void ack_received(struct neighbor *nb, __u32 conn_id, __u32 seqno);
585 extern void flush_out(struct conn *rconn);
587 extern int __init cor_snd_init(void);
589 /* forward.c */
590 extern void databuf_pull(struct data_buf *data, char *dst, int len);
592 extern size_t databuf_pulluser(struct conn *sconn, struct msghdr *msg);
594 extern void databuf_ack(struct data_buf *buf, __u64 pos);
596 extern void databuf_ackread(struct data_buf *buf);
598 extern int databuf_maypush(struct data_buf *buf);
600 extern void databuf_free(struct data_buf *data);
602 extern void databuf_init(struct data_buf *data);
604 extern int receive_userbuf(struct conn *rconn, struct msghdr *msg);
606 extern void receive_buf(struct conn *rconn, char *buf, int len);
608 extern int receive_skb(struct conn *rconn, struct sk_buff *skb);
610 extern void wake_sender(struct conn *rconn);
612 extern void forward_init(void);
616 static inline struct skb_procstate *skb_pstate(struct sk_buff *skb)
618 return (struct skb_procstate *) &(skb->cb[0]);
621 static inline struct sk_buff *skb_from_pstate(struct skb_procstate *ps)
623 return (struct sk_buff *) (((char *)ps) - offsetof(struct sk_buff,cb));
627 static inline __u32 mss(struct neighbor *nb)
629 return nb->dev->mtu - LL_RESERVED_SPACE(nb->dev) - 9;
633 static inline void put_u64(char *dst, __u64 value, int convbo)
635 char *p_value = (char *) &value;
637 if (convbo)
638 value = cpu_to_be64(value);
640 dst[0] = p_value[0];
641 dst[1] = p_value[1];
642 dst[2] = p_value[2];
643 dst[3] = p_value[3];
644 dst[4] = p_value[4];
645 dst[5] = p_value[5];
646 dst[6] = p_value[6];
647 dst[7] = p_value[7];
650 static inline void put_u32(char *dst, __u32 value, int convbo)
652 char *p_value = (char *) &value;
654 if (convbo)
655 value = cpu_to_be32(value);
657 dst[0] = p_value[0];
658 dst[1] = p_value[1];
659 dst[2] = p_value[2];
660 dst[3] = p_value[3];
663 static inline void put_u16(char *dst, __u16 value, int convbo)
665 char *p_value = (char *) &value;
667 if (convbo)
668 value = cpu_to_be16(value);
670 dst[0] = p_value[0];
671 dst[1] = p_value[1];
674 static inline char *cor_pull_skb(struct sk_buff *skb, unsigned int len)
676 char *ptr = skb_pull(skb, len);
678 if(ptr == 0)
679 return 0;
681 return ptr - len;