buffer flushing
[cor_2_6_31.git] / net / cor / cor.h
blobab09fb9e68492371c3f7256f3725244d5e67668a
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 <asm-sh/kgdb.h> */
27 #include <linux/spinlock.h>
28 #include <linux/workqueue.h>
30 /* psched_time_t */
31 #include <net/pkt_sched.h>
34 /* options */
35 #define PIDOUT_NEWCONN 16
36 #define PIDOUT_SENDDEF_THRES 8
37 #define PIDOUT_SENDDEF_COUNT 16
41 #define ETH_P_COR 0x1022
42 #define AF_COR 36
43 #define PF_COR AF_COR
45 #define SOCKADDRTYPE_PORT 1
46 struct cor_sockaddr {
47 int type;
49 union {
50 __be64 port;
51 } addr;
54 #define MAX_CONN_CMD_LEN 4096
57 #define PACKET_TYPE_ANNOUNCE 1
58 #define PACKET_TYPE_DATA 2
61 * Kernel packet data - these commands are sent by the neighbor
62 * The end nodes may cause these commands to be sent, but they see them beyond
63 * the first hop.
66 /* KP_PADDING[1] */
67 #define KP_PADDING 1
69 /* KP_[N]ACK[1] sent_conn_id[4] seqno[4]
71 * sent_conn_id means that this is *not* the conn_id we use if we sent something
72 * through this conn, but the conn_id that the neighbor used to send us the
73 * packet
75 #define KP_ACK 2
76 #define KP_NACK 3
79 * KP_SPEED[1] conn_id[4] speedinfo[2]
81 * speedinfo[2] =
82 * buffer_state_value = speedinfo % 181
83 * speed_value = speedinfo / 181
85 * buffer_state = 1024 * pow(2, buffer_state_value/3.0)
86 * speed = 1024 * pow(2, speed_value/12.0)
87 * see the .0 ...
89 * This has to be done either with floating points (which is no so nice) or
90 * you can calculate:
91 * buffer_state = pow(2, value/3) *
92 * 1024 * pow(pow(2, 1.0/3), buffer_state_value%3)
93 * where 1024 * pow(pow(2, 1.0/4), value%3) can be just a table lookup
94 * (the "1024" should be part of the value in the table, because it increases
95 * the accuracy)
97 * you can do the same with the speed
100 * Some values have special meanings:
101 * if speedinfo is the highest possible value(65535), it means both values
102 * are inifinite
103 * if buffer_state_value if > 91, you have to subtract 90 and make the
104 * resulting buffer_state negative
106 #define KP_SPEED 5
108 /* NOTE on connection ids:
109 * connection ids we send are used for the receive channel
110 * connection ids we receive are used for the send channel
114 * incoming connection
115 * KP_CONNECT[1] conn_id[4]
117 #define KP_CONNECT 10
120 * incoming connection successful,
121 * the first conn_id is the same as previously sent/received in KP_CONNECT
122 * the second conn_id is generated by us and used for the other direction
123 * KP_CONNECT_SUCCESS[1] conn_id[4] conn_id[4]
125 #define KP_CONNECT_SUCCESS 11
127 /* KP_CONNECT_FAILED_*[1] conn_id[4] */
128 #define KP_CONNECT_FAILED_TEMP 12
129 #define KP_CONNECT_FAILED_PERM 13
131 /* KP_CONN_DATA[1] conn_id[4] seqno[4] length[4] data[length] */
132 #define KP_CONN_DATA 14
135 * { KP_RESET_CONN[1] conn_id[4] }
136 * We send this, if there is an established connection we want to close.
138 #define KP_RESET_CONN 15
142 * Connection data which in interpreted when connection has no target yet
143 * These commands are sent by the end node.
145 * Format:
146 * cmd[2] length[4] parameter[length]
147 * unrecogniced commands are ignored
148 * parameters which are longer than expected are ignored as well
151 /* outgoing connection: CD_CONNECT_NB[2] addrlen[2][2] addr[addrlen][2] */
152 #define CD_CONNECT_NB 1
154 /* connection to local open part: CD_CONNECT_PORT[2] port[8] */
155 #define CD_CONNECT_PORT 2
158 * Connection data response
159 * Format is the same as with connection data
163 * {CDR_OK[2] || CDR_EXECUTION_FAILED[2]}
164 * reasoncode[2] reasontextlength[2] reasontext[reasontextlength]
165 * reasontextlength may be 0
167 #define CDR_EXECOK 32768
168 #define CDR_EXECOK_OK 32769
170 #define CDR_EXECFAILED 32770
171 #define CDR_EXECFAILED_UNKNOWN_COMMAND 32771
172 #define CDR_EXECFAILED_PERMISSION_DENIED 32772
173 #define CDR_TEMPORARILY_OUT_OF_RESSOURCES 32773
174 #define CDR_EXECFAILED_CMD_TOO_SHORT 32774
175 #define CDR_EXECFAILED_CMD_TOO_LONG 32775
177 #define CDR_EXECFAILED_TARGETADDRTYPE_UNKNOWN 32776
178 #define CDR_EXECFAILED_TARGETADDR_DOESNTEXIST 32777
179 #define CDR_EXECFAILED_LISTENERQUEUE_FULL 32778
183 /* result codes for rcv.c/proc_packet */
184 #define RC_DROP 0
185 #define RC_FINISHED 1
187 #define RC_RCV1_ANNOUNCE 2
188 #define RC_RCV1_KERNEL 3
189 #define RC_RCV1_CONN 4
191 struct ref_counter;
193 struct ref_counter_def{
195 * implemented by user, frees struct containing the mem_counter when
196 * refs == 0
198 void (*free)(struct ref_counter *cnt);
201 struct ref_counter{
202 spinlock_t lock;
204 /* initialised with 1 */
205 int refs;
207 /* initialised with 1, if 0 deny mem_counter_incr */
208 int is_active;
210 /* should be a pointer to a global variable */
211 struct ref_counter_def *def;
214 struct htab_entry{
215 struct htab_entry *next;
218 struct htable{
219 struct htab_entry **htable;
220 __u32 htable_size;
221 __u32 cell_size;
222 __u32 num_elements;
224 int (*matches)(void *htentry, void *searcheditem);
225 __u32 key_offset;
226 __u32 entry_offset;
227 __u32 ref_counter_offset;
230 struct neighbor{
231 struct list_head nb_list;
233 struct ref_counter refs;
235 struct net_device *dev;
236 char mac[MAX_ADDR_LEN];
238 char *addr;
239 __u32 addrlen;
241 struct timer_list cmsg_timer;
242 spinlock_t cmsg_lock;
243 struct list_head control_msgs_out;
244 unsigned long timedue;
245 __u32 length;
247 __u32 ooo_packets;
249 __u32 latency;
252 * connecions which receive data from/send data to this node
253 * used when terminating all connections of a neighbor
255 struct mutex conn_list_lock;
256 struct list_head rcv_conn_list;
257 struct list_head snd_conn_list;
260 * the timer has to be inited when adding the neighbor
261 * init_timer(struct timer_list * timer);
262 * add_timer(struct timer_list * timer);
264 spinlock_t retrans_lock;
265 struct timer_list retrans_timer;
268 * next_retransmit are linked with
269 * skb_procstate->funcstate.retransmit_queue
270 * because the sk_buff next/prev fields are needed by the hashtable
272 struct sk_buff_head retrans_list;
274 struct conn *firstboundconn;
277 struct cor_sched_data{
278 spinlock_t lock;
279 struct list_head conn_list;
280 struct sk_buff_head requeue_queue;
283 /* these are the non sent, non resized packets */
284 struct resize_buf{
285 spinlock_t lock;
286 __u32 totalsize;
287 struct sk_buff_head buf;
290 #define RINGBUFFER_PAGES 8
291 struct ringbuffer {
292 char *buffers[RINGBUFFER_PAGES];
293 __u32 read_offset;
294 __u32 write_offset;
297 struct sock_hdr {
298 /* The first 2 members of connlistener/conn (see sock.c) */
299 struct ref_counter refs;
300 __u8 sockstate;
303 struct connlistener;
305 struct bindnode{
306 struct list_head lh;
307 struct connlistener *owner;
308 __be64 port;
311 #define SOCKSTATE_LISTENER 1
312 #define SOCKSTATE_CONN 2
314 struct connlistener {
315 /* The first 2 members have to be the same as in conn (see sock.c) */
316 struct ref_counter refs;
317 __u8 sockstate;
318 struct bindnode *bn;
319 struct mutex lock;
320 int backlog;
321 int queue_len;
322 struct list_head conn_queue;
323 wait_queue_head_t wait;
328 * There are 2 conn objects per bi-directional connection. They refer to each
329 * other with in the reversedir field. To distinguish them, the variables on
330 * the stack are usually called rconn and sconn. rconn refers to the conn object
331 * which has received a command. sconn is the other conn object. This means that
332 * in send functions rconn means the connection we want to send the command to.
335 struct conn{
337 * The first 2 members have to be the same as in connlistener (see
338 * sock.c)
340 struct ref_counter refs;
341 __u8 sockstate;
343 #define SOURCE_NONE 0
344 #define SOURCE_IN 1
345 #define SOURCE_SOCK 2
347 #define TARGET_UNCONNECTED 0
348 #define TARGET_OUT 1
349 #define TARGET_SOCK 2
350 #define TARGET_KERNEL 3
352 __u8 sourcetype:4,
353 targettype:4;
355 __u8 qdisc_active:1;
357 spinlock_t credit_lock;
359 psched_time_t last_rcv_time;
360 psched_time_t last_snd_time;
361 // PSCHED_GET_TIME(now);
363 __u32 bytes_queued;
364 __u32 bytes_queued_avg;
366 /* state */
367 __u32 credits;
369 /* credit rate */
370 __s32 sender_crate;
371 __s32 resp_crate;
373 /* credit status of the sender/recp (for balancing credits)*/
374 __u32 credit_sender;
375 __u32 credit_recp;
377 __u32 avg_snd_cost;
379 __u32 avg_rate;
382 union{
383 struct{
384 struct neighbor *nb;
385 /* list of all connections from this neighbor */
386 struct list_head nb_list;
388 struct mutex rcv_lock;
389 struct sk_buff_head reorder_queue;
391 struct htab_entry htab_entry;
392 __u32 conn_id;
393 __u32 next_seqno;
394 __u32 ooo_packets;
395 }in;
397 struct{
398 struct mutex lock;
399 wait_queue_head_t wait;
400 struct socket *sock;
401 int flags;
403 struct list_head cl_list;
404 }sock;
405 }source;
407 union{
408 struct{
409 __u32 paramlen;
410 __u32 cmdread;
411 __u16 cmd;
412 __u8 *cmdparams;
413 }unconnected;
415 struct{
416 /* has to be first (because it is first in target
417 * kernel too)
419 struct neighbor *nb;
420 /* list of all connections to this neighbor */
421 struct list_head nb_list;
423 struct mutex seqno_lock;
424 __u32 seqno;
426 struct list_head allconnids;
428 __u32 conn_id;
430 /* see packet id definition */
431 __u8 connected:1;
433 struct resize_buf rb;/* see rcv.c */
434 struct sk_buff *skb;/* see sock.c */
436 atomic_t inflight_packets;
438 spinlock_t qdisc_lock;
439 struct conn *nextactive;
440 struct conn *prevactive;
441 struct sk_buff_head queue;
442 struct list_head queue_list;
443 __u8 qdisc_active;
444 }out;
446 struct{
447 struct mutex lock;
448 wait_queue_head_t wait;
450 struct socket *sock;
452 struct ringbuffer rbuf;
454 struct sk_buff_head queue;
455 }sock;
457 struct{
458 /* kernel packet - no connection */
459 struct neighbor *nb;
460 }kernel;
461 }target;
463 struct conn *reversedir;
466 /* inside skb->cb */
467 struct skb_procstate{
468 struct conn *rconn;
470 union{
471 struct{
472 struct work_struct work;
473 }rcv;
475 struct{
476 __u32 offset;
477 }announce;
479 struct{
480 __u32 conn_id;
481 __u32 seqno;
482 }rcv2;
484 struct{
485 struct htab_entry htab_entry;
486 struct ref_counter refs;
487 unsigned long timeout;
488 __u32 conn_id;
489 __u32 seqno;
490 }retransmit_queue;
491 }funcstate;
494 struct data{
495 __u8 type;
496 #define TYPE_BUF 0
497 #define TYPE_SKB 1
498 union {
499 struct {
500 char *buf;
501 __u32 len;
502 }buf;
503 struct sk_buff *skb;
504 }data;
507 static inline __u32 data_len(struct data *data) {
508 if (data->type == TYPE_BUF) {
509 return data->data.buf.len;
510 } else if (data->type == TYPE_SKB) {
511 BUG_ON(data->data.skb->len < 0);
512 return data->data.skb->len;
513 } else {
514 BUG();
518 static inline char * data_pull(struct data *data, int len) {
519 if (data->type == TYPE_BUF) {
520 char *ret = data->data.buf.buf;
521 if (unlikely(data->data.skb->len < len))
522 return 0;
524 data->data.buf.buf += len;
525 data->data.skb->len -= len;
526 return ret;
527 } else if (data->type == TYPE_SKB) {
528 return skb_pull(data->data.skb, len);
529 } else {
530 BUG();
535 /* common.c */
536 extern void ref_counter_decr(struct ref_counter *cnt);
538 extern int ref_counter_incr(struct ref_counter *cnt);
540 extern void ref_counter_deactivate(struct ref_counter *cnt);
542 extern void ref_counter_init(struct ref_counter *cnt,
543 struct ref_counter_def *def);
545 extern char *htable_get(struct htable *ht, __u32 key, void *searcheditem);
547 extern int htable_delete(struct htable *ht, char *oldelement, __u32 key,
548 void *searcheditem);
550 extern void htable_insert(struct htable *ht, char *newelement, __u32 key);
552 extern void htable_init(struct htable *ht, int (*matches)(void *htentry,
553 void *searcheditem), __u32 entry_offset,
554 __u32 ref_counter_offset);
556 /*enum bind_rc {
558 NOTFOUND,
559 HEARTS,
560 SPADES
561 };*/
563 extern struct conn *get_conn(__u32 conn_id);
565 extern int ringbuffer_put(struct ringbuffer *buf, char *src, int len,
566 int usercopy);
568 extern int ringbuffer_maypush(struct ringbuffer *buf);
570 extern int ringbuffer_pull(struct ringbuffer *buf, char *dest, int len,
571 int usercopy);
573 extern int ringbuffer_maypull(struct ringbuffer *buf);
575 extern int conn_init_out(struct conn *rconn, struct neighbor *nb);
577 extern void close_port(struct connlistener *listener);
579 extern struct connlistener *open_port(__be64 port);
581 extern int connect_port(struct conn *rconn, __be64 port);
583 extern int connect_neigh(struct conn *rconn,
584 __u16 addrtypelen, __u8 *addrtype,
585 __u16 addrlen, __u8 *addr);
587 extern struct conn* alloc_conn(gfp_t allocflags);
589 extern void reset_conn(struct conn *conn);
591 /* neighbor.c */
592 extern struct neighbor *find_neigh(__u16 addrtypelen, __u8 *addrtype,
593 __u16 addrlen, __u8 *addr);
595 extern void rcv_announce(struct sk_buff *skb);
597 extern void blacklist_neighbor(struct neighbor *nb);
599 /* rcv.c */
600 extern void drain_ooo_queue(struct conn *rconn);
602 extern void conn_rcv_buildskb(char *data, __u32 datalen, __u32 conn_id,
603 __u32 seqno);
605 /* kpacket_parse.c */
606 extern void kernel_packet(struct sk_buff *skb, __u32 seqno);
608 /* kpacket_gen.c */
609 extern void send_reset_conn(struct neighbor *nb, __u32 conn_id);
611 extern void send_ack(struct neighbor *nb, __u32 conn_id, __u32 seqno);
613 extern void send_connect_failed_temp(struct neighbor *nb, __u32 conn_id);
615 extern void send_connect_success(struct conn *rconn);
617 extern void send_connect_nb(struct conn *rconn);
619 extern void controlmsg_timerfunc(unsigned long arg);
621 /* cpacket_parse.c */
622 extern void parse(struct data *data, struct conn *conn);
624 /* snd.c */
625 extern void retransmit_timerfunc(unsigned long arg);
627 extern struct sk_buff *create_packet_conn(struct conn *target, int size,
628 gfp_t alloc_flags);
630 extern struct sk_buff *create_packet_kernel(struct neighbor *nb, int size,
631 gfp_t alloc_flags);
633 extern void send_packet(struct sk_buff *skb, struct neighbor *nb);
635 extern void ack_received(struct neighbor *nb, __u32 conn_id, __u32 seqno,
636 int nack);
638 static inline struct skb_procstate *skb_pstate(struct sk_buff *skb)
640 return (struct skb_procstate *) &(skb->cb[0]);
643 static inline struct sk_buff *skb_from_pstate(struct skb_procstate *ps)
645 return ((struct sk_buff *) ((char *)ps) - offsetof(struct sk_buff,cb));
649 static inline __u32 mss(struct neighbor *nb)
651 return nb->dev->mtu - LL_RESERVED_SPACE(nb->dev) - 17;