kernel packet sending bugfixes
[cor_2_6_31.git] / net / cor / cor.h
blob15ed3be69a626a92f3267f9c9bda98fbd213016c
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
74 #define KP_NACK 3
77 * KP_SPEED[1] conn_id[4] speedinfo[2]
79 * speedinfo[2] =
80 * buffer_state_value = speedinfo % 181
81 * speed_value = speedinfo / 181
83 * buffer_state = 1024 * pow(2, buffer_state_value/3.0)
84 * speed = 1024 * pow(2, speed_value/12.0)
85 * see the .0 ...
87 * This has to be done either with floating points (which is no so nice) or
88 * you can calculate:
89 * buffer_state = pow(2, value/3) *
90 * 1024 * pow(pow(2, 1.0/3), buffer_state_value%3)
91 * where 1024 * pow(pow(2, 1.0/4), value%3) can be just a table lookup
92 * (the "1024" should be part of the value in the table, because it increases
93 * the accuracy)
95 * you can do the same with the speed
98 * Some values have special meanings:
99 * if speedinfo is the highest possible value(65535), it means both values
100 * are inifinite
101 * if buffer_state_value if > 91, you have to subtract 90 and make the
102 * resulting buffer_state negative
104 #define KP_SPEED 5
106 /* NOTE on connection ids:
107 * connection ids we send are used for the receive channel
108 * connection ids we receive are used for the send channel
112 * incoming connection
113 * KP_CONNECT[1] conn_id[4]
115 #define KP_CONNECT 10
118 * incoming connection successful,
119 * the first conn_id is the same as previously sent/received in KP_CONNECT
120 * the second conn_id is generated by us and used for the other direction
121 * KP_CONNECT_SUCCESS[1] conn_id[4] conn_id[4]
123 #define KP_CONNECT_SUCCESS 11
125 /* KP_CONNECT_FAILED_*[1] conn_id[4] */
126 #define KP_CONNECT_FAILED_TEMP 12
127 #define KP_CONNECT_FAILED_PERM 13
129 /* KP_CONN_DATA[1] conn_id[4] seqno[4] length[4] data[length] */
130 #define KP_CONN_DATA 14
133 * { KP_RESET_CONN[1] conn_id[4] }
134 * We send this, if there is an established connection we want to close.
136 #define KP_RESET_CONN 15
140 * Connection data which in interpreted when connection has no target yet
141 * These commands are sent by the end node.
143 * Format:
144 * cmd[2] length[4] parameter[length]
145 * unrecogniced commands are ignored
146 * parameters which are longer than expected are ignored as well
149 /* outgoing connection: CD_CONNECT_NB[2] length[4]
150 * addrtypelen[2] addrlen[2] addrtype[addrtypelen] addr[addrlen] */
151 #define CD_CONNECT_NB 1
153 /* connection to local open part: CD_CONNECT_PORT[2] length[4] port[8] */
154 #define CD_CONNECT_PORT 2
157 * CD_LIST_NEIGH sends CDR_BINDATA if the command was successful. The response
158 * format is:
160 * totalneighs[4] response_rows[4]
161 * for every row:
162 * numaddr[2] (addrtypelen[2] addrlen[2] addrtype[addrtypelen] addr[addrlen]
163 * )[numaddr]
165 * Neighbors have to be sorted by uptime, new neighbors first. This is so that
166 * the routing daemon can easily find out whether there are new neighbors. It
167 * only needs to send a query with offset 0. If the totalneighs stays the same
168 * while new were added, a connection to another neighbor was lost.
171 /* list connected neighbors: CD_LIST_NEIGH[2] length[4] limit[4] offset[4] */
172 #define CD_LIST_NEIGH 3
175 * Connection data response
176 * Format is the same as with connection data
180 * {CDR_EXECOK[2] || CDR_EXECFAILED[2]}
181 * reasoncode[2] reasontextlength[2] reasontext[reasontextlength]
182 * reasontextlength may be 0
184 #define CDR_EXECOK 32768
185 #define CDR_EXECOK_OK 33024
187 #define CDR_EXECFAILED 32769
188 #define CDR_EXECFAILED_UNKNOWN_COMMAND 33280
189 #define CDR_EXECFAILED_PERMISSION_DENIED 33281
190 #define CDR_EXECFAILED_TEMPORARILY_OUT_OF_RESSOURCES 33282
191 #define CDR_EXECFAILED_CMD_TOO_SHORT 33283
192 #define CDR_EXECFAILED_CMD_TOO_LONG 33284
193 #define CDR_EXECFAILED_TARGETADDRTYPE_UNKNOWN 33285
194 #define CDR_EXECFAILED_TARGETADDR_DOESNTEXIST 33286
195 #define CDR_EXECFAILED_TARGETADDR_PORTCLOSED 33287
196 #define CDR_EXECFAILED_LISTENERQUEUE_FULL 33288
199 * must be sent after CDR_EXEC{OK|FAILED}
200 * CDR_EXEOK_BINDATA[2] bindatalen[4] bindata[bindatalen] */
201 #define CDR_BINDATA 32770
204 /* result codes for rcv.c/proc_packet */
205 #define RC_DROP 0
206 #define RC_FINISHED 1
208 #define RC_RCV1_ANNOUNCE 2
209 #define RC_RCV1_KERNEL 3
210 #define RC_RCV1_CONN 4
212 struct ref_counter;
214 struct ref_counter_def{
216 * implemented by user, frees struct containing the mem_counter when
217 * refs == 0
219 void (*free)(struct ref_counter *cnt);
222 struct ref_counter{
223 spinlock_t lock;
225 /* initialised with 1 */
226 int refs;
228 /* initialised with 1, if 0 deny mem_counter_incr */
229 int is_active;
231 /* should be a pointer to a global variable */
232 struct ref_counter_def *def;
235 struct htab_entry{
236 struct htab_entry *next;
239 struct htable{
240 struct htab_entry **htable;
241 __u32 htable_size;
242 __u32 cell_size;
243 __u32 num_elements;
245 int (*matches)(void *htentry, void *searcheditem);
246 __u32 key_offset;
247 __u32 entry_offset;
248 __u32 ref_counter_offset;
251 struct neighbor{
252 struct list_head nb_list;
254 struct ref_counter refs;
256 struct net_device *dev;
257 char mac[MAX_ADDR_LEN];
259 char *addr;
260 __u16 addrlen;
262 struct delayed_work cmsg_timer;
263 struct mutex cmsg_lock;
264 struct list_head control_msgs_out;
265 __u64 timeout;
266 __u32 length;
267 atomic_t kpacket_seqno;
269 atomic_t ooo_packets;
271 __u32 latency;
274 * connecions which receive data from/send data to this node
275 * used when terminating all connections of a neighbor
277 struct mutex conn_list_lock;
278 struct list_head rcv_conn_list;
279 struct list_head snd_conn_list;
282 * the timer has to be inited when adding the neighbor
283 * init_timer(struct timer_list * timer);
284 * add_timer(struct timer_list * timer);
286 spinlock_t retrans_lock;
287 struct timer_list retrans_timer;
290 * next_retransmit are linked with
291 * skb_procstate->funcstate.retransmit_queue
292 * because the sk_buff next/prev fields are needed by the hashtable
294 struct sk_buff_head retrans_list;
296 struct conn *firstboundconn;
299 struct cor_sched_data{
300 spinlock_t lock;
301 struct list_head conn_list;
302 struct sk_buff_head requeue_queue;
305 #define TYPE_BUF 0
306 #define TYPE_SKB 1
308 struct data_buf_item{
309 struct list_head buf_list;
311 union {
312 struct {
313 char *buf;
314 __u32 datalen;
316 }buf;
318 struct sk_buff *skb;
319 }data;
321 __u8 type;
324 struct data_buf{
325 struct list_head items;
326 struct data_buf_item *lastread;
327 __u64 first_offset;
328 __u64 read_offset;
330 __u32 totalsize;
331 __u32 read_remaining;
333 __u32 last_read_offset;
335 __u32 last_buflen;
338 struct connlistener;
340 struct bindnode{
341 struct list_head lh;
342 struct connlistener *owner;
343 __be64 port;
346 #define SOCKSTATE_LISTENER 1
347 #define SOCKSTATE_CONN 2
349 struct sock_hdr {
350 /* The first member of connlistener/conn (see sock.c) */
351 __u8 sockstate;
354 struct connlistener {
355 /* The first member has to be the same as in conn (see sock.c) */
356 __u8 sockstate;
357 struct bindnode *bn;
358 struct mutex lock;
359 int queue_maxlen;
360 int queue_len;
361 struct list_head conn_queue;
362 wait_queue_head_t wait;
367 * There are 2 conn objects per bi-directional connection. They refer to each
368 * other with in the reversedir field. To distinguish them, the variables on
369 * the stack are usually called rconn and sconn. rconn refers to the conn object
370 * which has received a command. sconn is the other conn object. This means that
371 * in send functions rconn means the connection we want to send the command to.
374 struct conn{
375 /* The first member has to be the same as in connlistener (see sock.c)*/
376 __u8 sockstate;
378 #define SOURCE_NONE 0
379 #define SOURCE_IN 1
380 #define SOURCE_SOCK 2
382 #define TARGET_UNCONNECTED 0
383 #define TARGET_OUT 1
384 #define TARGET_SOCK 2
386 __u8 sourcetype:4,
387 targettype:4;
388 __u8 isreset;
389 __u8 qdisc_active;
390 struct list_head queue_list;
392 /* todo: convert to kref */
393 struct ref_counter refs;
395 struct mutex rcv_lock;
397 /* state */
398 __u32 credits;
399 /* credit rate */
400 __s32 sender_crate;
401 __s32 resp_crate;
403 union{
404 struct{
405 struct neighbor *nb;
406 /* list of all connections from this neighbor */
407 struct list_head nb_list;
409 struct sk_buff_head reorder_queue;
411 struct htab_entry htab_entry;
412 __u32 conn_id;
413 __u32 next_seqno;
414 __u32 ooo_packets;
415 }in;
417 struct{
418 struct list_head cl_list;
419 wait_queue_head_t wait;
420 struct socket *sock;
421 int flags;
422 }sock;
423 }source;
425 union{
426 struct{
427 __u32 paramlen;
428 __u32 cmdread;
429 __u16 cmd;
430 __u8 *cmdparams;
431 }unconnected;
433 struct{
434 /* has to be first (because it is first in target
435 * kernel too)
437 struct neighbor *nb;
438 /* list of all connections to this neighbor */
439 struct list_head nb_list;
441 __u32 conn_id;
442 __u32 seqno;
443 }out;
445 struct{
446 wait_queue_head_t wait;
447 }sock;
448 }target;
450 struct data_buf buf;
452 struct conn *reversedir;
455 /* inside skb->cb */
456 struct skb_procstate{
457 struct conn *rconn;
459 union{
460 struct{
461 struct work_struct work;
462 }rcv;
464 struct{
465 __u32 offset;
466 }announce;
468 struct{
469 __u32 conn_id;
470 __u32 seqno;
471 }rcv2;
473 struct{
474 struct htab_entry htab_entry;
475 struct ref_counter refs;
476 unsigned long timeout;
477 __u32 conn_id;
478 __u32 seqno;
479 }retransmit_queue;
480 }funcstate;
484 /* common.c */
485 extern void ref_counter_decr(struct ref_counter *cnt);
487 extern int ref_counter_incr(struct ref_counter *cnt);
489 extern void ref_counter_init(struct ref_counter *cnt,
490 struct ref_counter_def *def);
492 extern char *htable_get(struct htable *ht, __u32 key, void *searcheditem);
494 extern int htable_delete(struct htable *ht, __u32 key, void *searcheditem);
496 extern void htable_insert(struct htable *ht, char *newelement, __u32 key);
498 extern void htable_init(struct htable *ht, int (*matches)(void *htentry,
499 void *searcheditem), __u32 entry_offset,
500 __u32 ref_counter_offset);
502 extern struct conn *get_conn(__u32 conn_id);
504 extern int conn_init_out(struct conn *rconn, struct neighbor *nb);
506 extern void conn_init_sock_source(struct conn *conn);
507 extern void conn_init_sock_target(struct conn *conn);
509 extern void close_port(struct connlistener *listener);
511 extern struct connlistener *open_port(__be64 port);
513 extern int connect_port(struct conn *rconn, __be64 port);
515 extern int connect_neigh(struct conn *rconn,
516 __u16 addrtypelen, __u8 *addrtype,
517 __u16 addrlen, __u8 *addr);
519 extern struct conn* alloc_conn(gfp_t allocflags);
521 extern void reset_conn(struct conn *conn);
523 /* neighbor.c */
524 extern struct neighbor *find_neigh(__u16 addrtypelen, __u8 *addrtype,
525 __u16 addrlen, __u8 *addr);
527 extern __u32 generate_neigh_list(char *buf, __u32 buflen, __u32 limit,
528 __u32 offset);
530 extern void rcv_announce(struct sk_buff *skb);
532 extern int __init cor_neighbor_init(void);
534 /* rcv.c */
535 extern void drain_ooo_queue(struct conn *rconn);
537 extern void conn_rcv_buildskb(char *data, __u32 datalen, __u32 conn_id,
538 __u32 seqno);
540 extern int __init cor_rcv_init(void);
542 /* kpacket_parse.c */
543 extern void kernel_packet(struct neighbor *nb, struct sk_buff *skb, __u32 seqno);
545 /* kpacket_gen.c */
546 extern void schedule_controlmsg_timerfunc(struct neighbor *nb);
548 struct control_msg_out;
550 extern struct control_msg_out *alloc_control_msg(void);
552 extern void free_control_msg(struct control_msg_out *cm);
554 extern void send_reset_conn(struct control_msg_out *cm, struct neighbor *nb,
555 __u32 conn_id);
557 extern void send_ack(struct control_msg_out *cm, struct neighbor *nb,
558 __u32 conn_id, __u32 seqno);
560 extern void send_connect_failed_temp(struct control_msg_out *cm,
561 struct neighbor *nb, __u32 conn_id);
563 extern void send_connect_success(struct control_msg_out *cm,
564 struct conn *rconn);
566 extern void send_connect_nb(struct control_msg_out *cm, struct conn *rconn);
568 extern void send_conndata(struct control_msg_out *cm, struct neighbor *nb,
569 __u32 connid, __u32 seqno, char *data_orig, char *data,
570 __u32 datalen);
572 /* cpacket_parse.c */
573 extern void parse(struct conn *rconn);
575 /* snd.c */
576 extern void retransmit_timerfunc(unsigned long arg);
578 extern struct sk_buff *create_packet_conn(struct conn *target, int size,
579 gfp_t alloc_flags);
581 extern struct sk_buff *create_packet_kernel(struct neighbor *nb, int size,
582 gfp_t alloc_flags);
584 extern void send_conn_flushdata(struct conn *rconn, char *data, __u32 datalen);
586 extern void send_packet(struct sk_buff *skb, struct neighbor *nb);
588 extern void ack_received(struct neighbor *nb, __u32 conn_id, __u32 seqno,
589 int nack);
591 extern void flush_out(struct conn *rconn);
593 extern int __init cor_snd_init(void);
595 /* forward.c */
596 extern void databuf_pull(struct data_buf *data, char *dst, int len);
598 extern size_t databuf_pulluser(struct conn *sconn, struct msghdr *msg);
600 extern void databuf_ack(struct data_buf *buf, __u64 pos);
602 extern void databuf_ackread(struct data_buf *buf);
604 extern int databuf_maypush(struct data_buf *buf);
606 extern void databuf_free(struct data_buf *data);
608 extern void databuf_init(struct data_buf *data);
610 extern int receive_userbuf(struct conn *rconn, struct msghdr *msg);
612 extern void receive_buf(struct conn *rconn, char *buf, int len);
614 extern int receive_skb(struct conn *rconn, struct sk_buff *skb);
616 extern void wake_sender(struct conn *rconn);
618 extern void forward_init(void);
621 static inline struct skb_procstate *skb_pstate(struct sk_buff *skb)
623 return (struct skb_procstate *) &(skb->cb[0]);
626 static inline struct sk_buff *skb_from_pstate(struct skb_procstate *ps)
628 return (struct sk_buff *) (((char *)ps) - offsetof(struct sk_buff,cb));
632 static inline __u32 mss(struct neighbor *nb)
634 return nb->dev->mtu - LL_RESERVED_SPACE(nb->dev) - 17;
638 static inline void put_u64(char *dst, __u64 value, int convbo)
640 char *p_value = (char *) &value;
642 if (convbo)
643 value = cpu_to_be64(value);
645 dst[0] = p_value[0];
646 dst[1] = p_value[1];
647 dst[2] = p_value[2];
648 dst[3] = p_value[3];
649 dst[4] = p_value[4];
650 dst[5] = p_value[5];
651 dst[6] = p_value[6];
652 dst[7] = p_value[7];
655 static inline void put_u32(char *dst, __u32 value, int convbo)
657 char *p_value = (char *) &value;
659 if (convbo)
660 value = cpu_to_be32(value);
662 dst[0] = p_value[0];
663 dst[1] = p_value[1];
664 dst[2] = p_value[2];
665 dst[3] = p_value[3];
668 static inline void put_u16(char *dst, __u16 value, int convbo)
670 char *p_value = (char *) &value;
672 if (convbo)
673 value = cpu_to_be16(value);
675 dst[0] = p_value[0];
676 dst[1] = p_value[1];
679 static inline char *cor_pull_skb(struct sk_buff *skb, unsigned int len)
681 char *ptr = skb_pull(skb, len);
683 if(ptr == 0)
684 return 0;
686 return ptr - len;