ping cookies
[cor_2_6_31.git] / net / cor / cor.h
blobcac396aefe15decce08b2c13a3b29194a99760e8
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
68 * KP_PING[1] cookie[4]
69 * KP_PONG[1] cookie[4] respdelay[4]
71 * This is needed to find out whether the other node is reachable. After a new
72 * neighbor is seen, ping requests are sent and the neighbor is only reachable
73 * after a few pongs are received. These requests are also used to find out
74 * whether a neighber is gone.
76 * respdelay:
77 * The receiver of a ping may delay the sending of the pong e.g. to create
78 * bigger kernel packets. The respdelay is the time in microseconds the packet
79 * was delayed.
81 #define KP_PING 2
82 #define KP_PONG 3
84 /* KP_ACK[1] sent_conn_id[4] seqno[4]
86 * sent_conn_id means that this is *not* the conn_id we use if we sent something
87 * through this conn, but the conn_id that the neighbor used to send us the
88 * packet
90 #define KP_ACK 4
93 * KP_SPEED[1] conn_id[4] speedinfo[2]
95 * speedinfo[2] =
96 * buffer_state_value = speedinfo % 181
97 * speed_value = speedinfo / 181
99 * buffer_state = 1024 * pow(2, buffer_state_value/3.0)
100 * speed = 1024 * pow(2, speed_value/12.0)
101 * see the .0 ...
103 * This has to be done either with floating points (which is no so nice) or
104 * you can calculate:
105 * buffer_state = pow(2, value/3) *
106 * 1024 * pow(pow(2, 1.0/3), buffer_state_value%3)
107 * where 1024 * pow(pow(2, 1.0/4), value%3) can be just a table lookup
108 * (the "1024" should be part of the value in the table, because it increases
109 * the accuracy)
111 * you can do the same with the speed
114 * Some values have special meanings:
115 * if speedinfo is the highest possible value(65535), it means both values
116 * are inifinite
117 * if buffer_state_value if > 91, you have to subtract 90 and make the
118 * resulting buffer_state negative
120 #define KP_SPEED 5
122 /* NOTE on connection ids:
123 * connection ids we send are used for the receive channel
124 * connection ids we receive are used for the send channel
128 * incoming connection
129 * KP_CONNECT[1] conn_id[4]
131 #define KP_CONNECT 6
134 * incoming connection successful,
135 * the first conn_id is the same as previously sent/received in KP_CONNECT
136 * the second conn_id is generated by us and used for the other direction
137 * KP_CONNECT_SUCCESS[1] conn_id[4] conn_id[4]
139 #define KP_CONNECT_SUCCESS 7
141 /* KP_CONN_DATA[1] conn_id[4] seqno[4] length[2] data[length] */
142 #define KP_CONN_DATA 8
145 * { KP_RESET_CONN[1] conn_id[4] }
146 * We send this, if there is an established connection we want to close.
148 #define KP_RESET_CONN 9
152 * Connection data which in interpreted when connection has no target yet
153 * These commands are sent by the end node.
155 * Format:
156 * cmd[2] length[4] parameter[length]
157 * unrecogniced commands are ignored
158 * parameters which are longer than expected are ignored as well
161 /* outgoing connection: CD_CONNECT_NB[2] length[4]
162 * addrtypelen[2] addrlen[2] addrtype[addrtypelen] addr[addrlen] */
163 #define CD_CONNECT_NB 1
165 /* connection to local open part: CD_CONNECT_PORT[2] length[4] port[8] */
166 #define CD_CONNECT_PORT 2
169 * CD_LIST_NEIGH sends CDR_BINDATA if the command was successful. The response
170 * format is:
172 * totalneighs[4] response_rows[4]
173 * for every row:
174 * numaddr[2] (addrtypelen[2] addrlen[2] addrtype[addrtypelen] addr[addrlen]
175 * )[numaddr]
177 * Neighbors have to be sorted by uptime, new neighbors first. This is so that
178 * the routing daemon can easily find out whether there are new neighbors. It
179 * only needs to send a query with offset 0. If the totalneighs stays the same
180 * while new were added, a connection to another neighbor was lost.
183 /* list connected neighbors: CD_LIST_NEIGH[2] length[4] limit[4] offset[4] */
184 #define CD_LIST_NEIGH 3
187 * Connection data response
188 * Format is the same as with connection data
192 * {CDR_EXECOK[2] || CDR_EXECFAILED[2]}
193 * reasoncode[2] reasontextlength[2] reasontext[reasontextlength]
194 * reasontextlength may be 0
196 #define CDR_EXECOK 32768
197 #define CDR_EXECOK_OK 33024
199 #define CDR_EXECFAILED 32769
200 #define CDR_EXECFAILED_UNKNOWN_COMMAND 33280
201 #define CDR_EXECFAILED_PERMISSION_DENIED 33281
202 #define CDR_EXECFAILED_TEMPORARILY_OUT_OF_RESSOURCES 33282
203 #define CDR_EXECFAILED_CMD_TOO_SHORT 33283
204 #define CDR_EXECFAILED_CMD_TOO_LONG 33284
205 #define CDR_EXECFAILED_TARGETADDRTYPE_UNKNOWN 33285
206 #define CDR_EXECFAILED_TARGETADDR_DOESNTEXIST 33286
207 #define CDR_EXECFAILED_TARGETADDR_PORTCLOSED 33287
208 #define CDR_EXECFAILED_LISTENERQUEUE_FULL 33288
211 * must be sent after CDR_EXEC{OK|FAILED}
212 * CDR_EXEOK_BINDATA[2] bindatalen[4] bindata[bindatalen] */
213 #define CDR_BINDATA 32770
216 /* result codes for rcv.c/proc_packet */
217 #define RC_DROP 0
218 #define RC_FINISHED 1
220 #define RC_RCV1_ANNOUNCE 2
221 #define RC_RCV1_KERNEL 3
222 #define RC_RCV1_CONN 4
224 struct ref_counter;
226 struct ref_counter_def{
228 * implemented by user, frees struct containing the mem_counter when
229 * refs == 0
231 void (*free)(struct ref_counter *cnt);
234 struct ref_counter{
235 spinlock_t lock;
237 /* initialised with 1 */
238 int refs;
240 /* should be a pointer to a global variable */
241 struct ref_counter_def *def;
244 struct htab_entry{
245 /* start of next element, *not* next htab_entry */
246 void *next;
249 struct htable{
250 struct htab_entry **htable;
251 __u32 htable_size;
252 __u32 cell_size;
253 __u32 num_elements;
255 int (*matches)(void *htentry, void *searcheditem);
256 __u32 key_offset;
257 __u32 entry_offset;
258 __u32 ref_counter_offset;
261 struct ping_cookie{
262 unsigned long time;
263 __u32 cookie;
264 __u8 pongs; /* count of pongs for pings sent after this one */
267 struct neighbor{
268 struct list_head nb_list;
270 struct ref_counter refs;
272 struct net_device *dev;
273 char mac[MAX_ADDR_LEN];
275 char *addr;
276 __u16 addrlen;
278 struct delayed_work cmsg_timer;
279 struct mutex cmsg_lock;
280 struct list_head control_msgs_out;
281 __u64 timeout;
282 __u32 length;
285 unsigned long last_ping_time;
286 __u32 noping_cnt;
287 __u32 ping_intransit;
288 struct ping_cookie cookies[PING_COOKIES_PER_NEIGH];
289 __u32 lastcookie;
292 atomic_t kpacket_seqno;
294 atomic_t ooo_packets;
296 atomic_t latency; /* microsecs */
299 * connecions which receive data from/send data to this node
300 * used when terminating all connections of a neighbor
302 struct mutex conn_list_lock;
303 struct list_head rcv_conn_list;
304 struct list_head snd_conn_list;
307 * the timer has to be inited when adding the neighbor
308 * init_timer(struct timer_list * timer);
309 * add_timer(struct timer_list * timer);
311 spinlock_t retrans_lock;
312 struct timer_list retrans_timer;
315 * next_retransmit are linked with
316 * skb_procstate->funcstate.retransmit_queue
317 * because the sk_buff next/prev fields are needed by the hashtable
319 struct sk_buff_head retrans_list;
321 struct conn *firstboundconn;
324 struct cor_sched_data{
325 spinlock_t lock;
326 struct list_head conn_list;
327 struct sk_buff_head requeue_queue;
330 #define TYPE_BUF 0
331 #define TYPE_SKB 1
333 struct data_buf_item{
334 struct list_head buf_list;
336 union {
337 struct {
338 char *buf;
339 __u32 datalen;
341 }buf;
343 struct sk_buff *skb;
344 }data;
346 __u8 type;
349 struct data_buf{
350 struct list_head items;
351 struct data_buf_item *lastread;
352 __u64 first_offset;
353 __u64 read_offset;
355 __u32 totalsize;
356 __u32 read_remaining;
358 __u32 last_read_offset;
360 __u32 last_buflen;
363 struct connlistener;
365 struct bindnode{
366 struct list_head lh;
367 struct connlistener *owner;
368 __be64 port;
371 #define SOCKSTATE_LISTENER 1
372 #define SOCKSTATE_CONN 2
374 struct sock_hdr {
375 /* The first member of connlistener/conn (see sock.c) */
376 __u8 sockstate;
379 struct connlistener {
380 /* The first member has to be the same as in conn (see sock.c) */
381 __u8 sockstate;
382 struct bindnode *bn;
383 struct mutex lock;
384 int queue_maxlen;
385 int queue_len;
386 struct list_head conn_queue;
387 wait_queue_head_t wait;
392 * There are 2 conn objects per bi-directional connection. They refer to each
393 * other with in the reversedir field. To distinguish them, the variables on
394 * the stack are usually called rconn and sconn. rconn refers to the conn object
395 * which has received a command. sconn is the other conn object. This means that
396 * in send functions rconn means the connection we want to send the command to.
399 struct conn{
400 /* The first member has to be the same as in connlistener (see sock.c)*/
401 __u8 sockstate;
403 #define SOURCE_NONE 0
404 #define SOURCE_IN 1
405 #define SOURCE_SOCK 2
407 #define TARGET_UNCONNECTED 0
408 #define TARGET_OUT 1
409 #define TARGET_SOCK 2
411 __u8 sourcetype:4,
412 targettype:4;
413 __u8 isreset;
414 __u8 qdisc_active;
415 struct list_head queue_list;
417 /* todo: convert to kref */
418 struct ref_counter refs;
420 struct mutex rcv_lock;
422 /* state */
423 __u32 credits;
424 /* credit rate */
425 __s32 sender_crate;
426 __s32 resp_crate;
428 union{
429 struct{
430 struct neighbor *nb;
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;
437 __u32 conn_id;
438 __u32 next_seqno;
439 __u32 ooo_packets;
440 }in;
442 struct{
443 struct list_head cl_list;
444 wait_queue_head_t wait;
445 struct socket *sock;
446 int flags;
447 }sock;
448 }source;
450 union{
451 struct{
452 __u32 paramlen;
453 __u32 cmdread;
454 __u16 cmd;
455 __u8 *cmdparams;
456 }unconnected;
458 struct{
459 /* has to be first (because it is first in target
460 * kernel too)
462 struct neighbor *nb;
463 /* list of all connections to this neighbor */
464 struct list_head nb_list;
466 __u32 conn_id;
467 __u32 seqno;
468 }out;
470 struct{
471 wait_queue_head_t wait;
472 }sock;
473 }target;
475 struct data_buf buf;
477 struct conn *reversedir;
480 /* inside skb->cb */
481 struct skb_procstate{
482 struct conn *rconn;
484 union{
485 struct{
486 struct work_struct work;
487 }rcv;
489 struct{
490 __u32 offset;
491 }announce;
493 struct{
494 __u32 conn_id;
495 __u32 seqno;
496 }rcv2;
498 struct{
499 struct htab_entry htab_entry;
500 struct ref_counter refs;
501 unsigned long timeout;
502 __u32 conn_id;
503 __u32 seqno;
504 struct neighbor *nb;
505 }retransmit_queue;
506 }funcstate;
510 /* common.c */
511 extern void ref_counter_decr(struct ref_counter *cnt);
513 extern int ref_counter_incr(struct ref_counter *cnt);
515 extern void ref_counter_init(struct ref_counter *cnt,
516 struct ref_counter_def *def);
518 extern char *htable_get(struct htable *ht, __u32 key, void *searcheditem);
520 extern int htable_delete(struct htable *ht, __u32 key, void *searcheditem);
522 extern void htable_insert(struct htable *ht, char *newelement, __u32 key);
524 extern void htable_init(struct htable *ht, int (*matches)(void *htentry,
525 void *searcheditem), __u32 entry_offset,
526 __u32 ref_counter_offset);
528 extern struct conn *get_conn(__u32 conn_id);
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);
549 /* neighbor.c */
550 extern struct neighbor *get_neigh_by_mac(struct sk_buff *skb);
552 extern struct neighbor *find_neigh(__u16 addrtypelen, __u8 *addrtype,
553 __u16 addrlen, __u8 *addr);
555 extern __u32 generate_neigh_list(char *buf, __u32 buflen, __u32 limit,
556 __u32 offset);
558 extern void ping_resp(struct neighbor *nb, __u32 cookie, __u32 respdelay);
560 extern __u32 add_ping_req(struct neighbor *nb);
562 extern int time_to_send_ping(struct neighbor *nb);
564 extern int force_ping(struct neighbor *nb);
566 extern void rcv_announce(struct sk_buff *skb);
568 extern int __init cor_neighbor_init(void);
570 /* rcv.c */
571 extern void drain_ooo_queue(struct conn *rconn);
573 extern void conn_rcv_buildskb(char *data, __u32 datalen, __u32 conn_id,
574 __u32 seqno);
576 extern int __init cor_rcv_init(void);
578 /* kpacket_parse.c */
579 extern void kernel_packet(struct neighbor *nb, struct sk_buff *skb, __u32 seqno);
581 /* kpacket_gen.c */
582 extern void schedule_controlmsg_timerfunc(struct neighbor *nb);
584 struct control_msg_out;
586 extern struct control_msg_out *alloc_control_msg(void);
588 extern void free_control_msg(struct control_msg_out *cm);
590 extern void send_pong(struct control_msg_out *cm, struct neighbor *nb,
591 __u32 cookie);
593 extern void send_reset_conn(struct control_msg_out *cm, struct neighbor *nb,
594 __u32 conn_id);
596 extern void send_ack(struct control_msg_out *cm, struct neighbor *nb,
597 __u32 conn_id, __u32 seqno);
599 extern void send_connect_success(struct control_msg_out *cm,
600 struct neighbor *nb, __u32 rcvd_conn_id, __u32 gen_conn_id);
602 extern void send_connect_nb(struct control_msg_out *cm, struct neighbor *nb,
603 __u32 conn_id);
605 extern void send_conndata(struct control_msg_out *cm, struct neighbor *nb,
606 __u32 connid, __u32 seqno, char *data_orig, char *data,
607 __u32 datalen);
609 /* cpacket_parse.c */
610 extern void parse(struct conn *rconn);
612 /* snd.c */
613 extern void retransmit_timerfunc(unsigned long arg);
615 extern struct sk_buff *create_packet_conn(struct conn *target, int size,
616 gfp_t alloc_flags);
618 extern struct sk_buff *create_packet_kernel(struct neighbor *nb, int size,
619 gfp_t alloc_flags);
621 extern void send_conn_flushdata(struct conn *rconn, char *data, __u32 datalen);
623 extern void send_packet(struct sk_buff *skb, struct neighbor *nb,
624 int retransmit);
626 extern void ack_received(struct neighbor *nb, __u32 conn_id, __u32 seqno);
628 extern void flush_out(struct conn *rconn);
630 extern int __init cor_snd_init(void);
632 /* forward.c */
633 extern void databuf_pull(struct data_buf *data, char *dst, int len);
635 extern size_t databuf_pulluser(struct conn *sconn, struct msghdr *msg);
637 extern void databuf_ack(struct data_buf *buf, __u64 pos);
639 extern void databuf_ackread(struct data_buf *buf);
641 extern int databuf_maypush(struct data_buf *buf);
643 extern void databuf_free(struct data_buf *data);
645 extern void databuf_init(struct data_buf *data);
647 extern int receive_userbuf(struct conn *rconn, struct msghdr *msg);
649 extern void receive_buf(struct conn *rconn, char *buf, int len);
651 extern int receive_skb(struct conn *rconn, struct sk_buff *skb);
653 extern void wake_sender(struct conn *rconn);
655 extern void forward_init(void);
659 static inline struct skb_procstate *skb_pstate(struct sk_buff *skb)
661 return (struct skb_procstate *) &(skb->cb[0]);
664 static inline struct sk_buff *skb_from_pstate(struct skb_procstate *ps)
666 return (struct sk_buff *) (((char *)ps) - offsetof(struct sk_buff,cb));
670 static inline __u32 mss(struct neighbor *nb)
672 return nb->dev->mtu - LL_RESERVED_SPACE(nb->dev) - 9;
676 static inline void put_u64(char *dst, __u64 value, int convbo)
678 char *p_value = (char *) &value;
680 if (convbo)
681 value = cpu_to_be64(value);
683 dst[0] = p_value[0];
684 dst[1] = p_value[1];
685 dst[2] = p_value[2];
686 dst[3] = p_value[3];
687 dst[4] = p_value[4];
688 dst[5] = p_value[5];
689 dst[6] = p_value[6];
690 dst[7] = p_value[7];
693 static inline void put_u32(char *dst, __u32 value, int convbo)
695 char *p_value = (char *) &value;
697 if (convbo)
698 value = cpu_to_be32(value);
700 dst[0] = p_value[0];
701 dst[1] = p_value[1];
702 dst[2] = p_value[2];
703 dst[3] = p_value[3];
706 static inline void put_u16(char *dst, __u16 value, int convbo)
708 char *p_value = (char *) &value;
710 if (convbo)
711 value = cpu_to_be16(value);
713 dst[0] = p_value[0];
714 dst[1] = p_value[1];
717 static inline char *cor_pull_skb(struct sk_buff *skb, unsigned int len)
719 char *ptr = skb_pull(skb, len);
721 if(ptr == 0)
722 return 0;
724 return ptr - len;