fast initial bufsize increase
[cor.git] / net / cor / cor.h
bloba2160355a26e39504f3f0b3e5541170031e83438
1 /**
2 * Connection oriented routing
3 * Copyright (C) 2007-2020 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/module.h>
24 #include <linux/types.h>
25 #include <linux/interrupt.h>
26 #include <linux/sched.h>
27 #include <linux/netdevice.h>
28 #include <linux/skbuff.h>
29 #include <linux/spinlock.h>
30 #include <linux/workqueue.h>
31 #include <linux/kref.h>
32 #include <linux/ktime.h>
33 #include <linux/rbtree.h>
35 #include <linux/socket.h>
36 #include <net/sock.h>
38 #include <linux/math64.h>
40 #include "settings.h"
43 #define ETH_P_COR 0x1022
44 #define AF_COR 99
45 #define PF_COR AF_COR
47 #define PROTO_COR_RAW 0
48 #define PROTO_COR_RDEAMON 1
50 #define SOCKADDRTYPE_PORT 1
51 #define SOCKADDRTYPE_ADDRPORT 2
52 struct cor_sockaddr {
53 __u16 sin_family;
55 __be16 port;
56 char addr[64];
59 #define COR_PASS_ON_CLOSE 1
60 #define COR_PUBLISH_SERVICE 2
62 #define MAX_CONN_CMD_LEN 64
65 #define PACKET_TYPE_ANNOUNCE 1
66 #define PACKET_TYPE_CMSG 2
67 #define PACKET_TYPE_CONNDATA 4
68 #define PACKET_TYPE_CONNDATA_LOWBUFDELAYED 5
69 #define PACKET_TYPE_CONNDATA_FLUSH 6
70 #define PACKET_TYPE_CONNDATA_LOWBUFDELAYED_FLUSH 7
73 * Kernel packet data - these commands are sent by the neighbor
74 * The end nodes may cause these commands to be sent, but they see them beyond
75 * the first hop.
78 /* KP_PADDING[1] */
79 #define KP_PADDING 0
82 * KP_INIT_SESSION[1] sessionid[4]
84 * finishes neighbor discovery and starts a session
86 * Before this is received all other commands are ignored. The sessionid is used
87 * to prevent usage of old neighbor discovery data (e.g. addresses)
89 #define KP_INIT_SESSION 1
91 #define KP_INIT_SESSION_CMDLEN 5
94 * KP_PING[1] cookie[4]
95 * KP_PONG[1] cookie[4] respdelay[4]
97 * This is needed to find out whether the other node is reachable. After a new
98 * neighbor is seen, ping requests are sent and the neighbor is only reachable
99 * after a few pongs are received. These requests are also used to find out
100 * whether a neighber is gone.
102 * respdelay:
103 * The receiver of a ping may delay the sending of the pong e.g. to create
104 * bigger packets. The respdelay is the time in microseconds the packet was
105 * delayed.
107 #define KP_PING 2
109 #define KP_PING_CMDLEN 5
111 #define KP_PONG 3
113 /* KP_ACK[1] seqno[6] */
114 #define KP_ACK 4
117 * KP_ACK_CONN[1] conn_id[4] flags[1] seqno[6] window[1] seqno_ooo[6]
118 * length[1-4] priority_seqno[1] priority[1]
120 * conn_id is the conn_id we use if we sent something through this conn and
121 * *not* the conn_id that the neighbor used to send us the data
123 * flags defines which of the following fields are sent
125 * seqno = the seqno which is expected in the next non-out-of-order packet
127 * window = amount of data which can be sent without receiving the next ack
128 * packets with lower seqno do not overwrite the last window size
129 * The window may also be reduced. However, this only indicates a wish.
130 * Packets must be accepted if they exceed the new window, but not the old
131 * one.
133 * decode:
134 * 0 = 0
135 * 1...255 = 64*2^((value-1)/7) end result is rounded down to an integer
137 * seqno_ooo, length = This packet was received out of order. Maybe a previous
138 * packet has been lost. Out of order data should not be retransmitted.
139 * Multiple ooo packets may be merged into a single ack. Ooo packets may be
140 * partially accepted, so that the length does not cover the full packet and/
141 * or the seqno starts in the middle of a packet
143 #define KP_ACK_CONN 5
145 #define KP_ACK_CONN_FLAGS_SEQNO 1
146 #define KP_ACK_CONN_FLAGS_WINDOW 2
147 #define KP_ACK_CONN_FLAGS_OOO 12 /* 4+8 */
148 #define KP_ACK_CONN_FLAGS_PRIORITY 16
150 static inline __u8 ooolen_to_flags(__u32 len)
152 if (len == 0)
153 return 0;
154 if (len < 256)
155 return 4;
156 if (len < 65536)
157 return 8;
158 return 12;
161 static inline int ooolen(__u8 flags)
163 int len = ((flags & KP_ACK_CONN_FLAGS_OOO) >> 2);
164 if (unlikely(len == 3))
165 return 4;
166 return len;
169 static inline int ack_conn_len(__u8 flags)
171 int len = 0;
172 if ((flags & KP_ACK_CONN_FLAGS_SEQNO) != 0) {
173 len += 6;
174 if ((flags & KP_ACK_CONN_FLAGS_WINDOW) != 0)
175 len++;
178 if (ooolen(flags) != 0) {
179 len += 6;
180 len += ooolen(flags);
183 if (flags & KP_ACK_CONN_FLAGS_PRIORITY)
184 len += 2;
186 return len;
190 * NOTE on connection ids:
191 * connection ids we receive with most significant bit 0 have been generated by
192 * us
193 * connection ids we receive with most significant bit 1 have been generated by
194 * the other side
196 * ATTENTION: the priority seqno are reversed:
197 * priority seqnos we send are used when we send updates
198 * priority seqnos we received are used when we receive updates
202 * incoming connection
203 * seqno1... used to ack data sent from the side which initiated the connection
204 * seqno2... used to ack data sent to the side which initiated the connection
205 * KP_CONNECT[1] conn_id[4] seqno1[6] seqno2[6] window[1] priority_seqno[1]
206 * priority[1]
208 #define KP_CONNECT 6
211 * incoming connection successful,
212 * KP_CONNECT_SUCCESS[1] conn_id[4] window[1]
214 #define KP_CONNECT_SUCCESS 7
216 /* KP_CONN_DATA[1] conn_id[4] seqno[6] length[2] data[length] */
217 #define KP_CONN_DATA 8
218 #define KP_CONN_DATA_LOWBUFDELAYED 9
219 #define KP_CONN_DATA_FLUSH 10
220 #define KP_CONN_DATA_LOWBUFDELAYED_FLUSH 11
222 #define KP_CONN_DATA_CMDLEN 13
225 * KP_RESET_CONN[1] conn_id[4]
226 * We send this, if there is an established connection we want to close.
228 #define KP_RESET_CONN 12
231 * KP_SET_MAX_CMSG_DELAY[1] cpacket_ack_delay[4] data_ack_delay[4] cmsg_delay[4]
232 * Sent after connecting and at any change
233 * delay in specifies in microsecs
235 #define KP_SET_MAX_CMSG_DELAY 13
237 #define KP_SET_MAX_CMSG_DELAY_CMDLEN 13
241 * Connection data which in interpreted when connection has no target yet
242 * These commands are sent by the end node.
244 * Format:
245 * cmd[2] length[1-4] parameter[length]
246 * unrecogniced commands are ignored
247 * parameters which are longer than expected are ignored as well
250 #define CD_CONTINUE_ON_ERROR_FLAG 32768
252 /* outgoing connection: CD_CONNECT_NB[2] length[1-4]
253 * addrlen[1-4] addr[addrlen] */
254 #define CD_CONNECT_NB 1
256 /* connection to local open part: CD_CONNECT_PORT[2] length[1-4] port[2] */
257 #define CD_CONNECT_PORT 2
260 * CD_LIST_NEIGH sends CDR_BINDATA if the command was successful. The response
261 * format is:
263 * numneighs[1-4]
264 * numfields[1-4] (field[2] fieldlen[1-4])[numfields]
265 * rows[responserows]:
266 * fieldlen[1-4], only if fieldlen in the header was "0"
267 * fielddata[fieldlen]
269 * Future versions may append data to field definition. Therefore clients must
270 * silently discard data at the end they do not expect.
273 /* list connected neighbors: CD_LIST_NEIGH[2] length[1-4] */
274 #define CD_LIST_NEIGH 3
277 * addr[fieldlen]
279 #define LIST_NEIGH_FIELD_ADDR 1
282 * latency_in_microsecs[1] (64_11 encoding)
283 * Only raw network latency in measured. Delays caused by the priority queues
284 * are *not* included.
286 #define LIST_NEIGH_FIELD_LATENCY 2
288 /* list services: CD_LIST_SERVICES[2] length[1-4] */
289 #define CD_LIST_SERVICES 4
293 * Connection data response
294 * Format is the same as with connection data
298 * CDR_EXECOK[1]
300 #define CDR_EXECOK 1
303 * CDR_EXECFAILED[1] reasoncode[2]
305 #define CDR_EXECFAILED 2
306 #define CDR_EXECFAILED_INVALID_COMMAND 1
307 #define CDR_EXECFAILED_TEMPORARILY_OUT_OF_RESSOURCES 2
308 #define CDR_EXECFAILED_NB_DOESNTEXIST 3
309 #define CDR_EXECFAILED_PORTCLOSED 4
312 * must be sent after CDR_EXEC{OK|FAILED}
313 * CDR_EXEOK_BINDATA[1] bindatalen[1-4] bindata[bindatalen] */
314 #define CDR_BINDATA 3
317 * routing daemon sock
318 * format:
319 * cmdcode[4] length[4] cmddata[length]
323 #define CRD_KTU_SUPPORTEDVERSIONS 1
325 * CRD_KTU_SUPPORTEDVERSIONS[4] length[4] min[4] max[4]
328 #define CRD_KTU_CONNECT 2
330 * CRD_KTU_KTOU_CONNECT[4] length[4] cookie[8] targetlen[4] target[targetlen]
333 #define CRD_UTK_VERSION 1
335 * CRD_UTK_VERSION[4] length[4] version[4]
339 #define CRD_UTK_UP 2
341 * CRD_UTK_UP[4] length[4] flags[8] addrlen[4] addr[addrlen]
346 #define CRD_UTK_CONNECTERROR 3
348 * CRD_UTK_CONNECTERROR[4] length[4] cookie[8] error[4]
351 #define CRD_UTK_CONNECTERROR_ACCES 1
352 #define CRD_UTK_CONNECTERROR_NETUNREACH 2
353 #define CRD_UTK_CONNECTERROR_TIMEDOUT 3
354 #define CRD_UTK_CONNECTERROR_REFUSED 4
356 #define CONN_MNGD_HEADERLEN 2
357 #define CONN_MNGD_CHECKSUMLEN 4
359 #define CONN_MNGD_HASDATA (1 << 15)
360 #define CONN_MNGD_EOF (1 << 0)
361 #define CONN_MNGD_RCVEND (1 << 1)
362 #define CONN_MNGD_DATALEN 4095
364 #define CONN_MNGD_MAX_SEGMENT_SIZE (CONN_MNGD_DATALEN + 1)
366 #define PRIORITY_MAX 15384774
371 /* result codes for rcv.c/proc_packet */
372 #define RC_DROP 0
373 #define RC_FINISHED 1
375 #define RC_RCV1_ANNOUNCE 2
376 #define RC_RCV1_KERNEL 3
377 #define RC_RCV1_CONN 4
380 #define CONGSTATUS_NONE 0
381 #define CONGSTATUS_CONNDATA 1
382 #define CONGSTATUS_ANNOUNCE 2
383 #define CONGSTATUS_RETRANS 3
384 #define CONGSTATUS_KPACKETS 4
386 struct qos_queue {
387 spinlock_t qlock;
389 struct kref ref;
391 struct list_head queue_list;
393 struct net_device *dev; /* may not change while queue is in list */
395 struct timer_list qos_resume_timer;
396 struct tasklet_struct qos_resume_task;
397 int qos_resume_scheduled;
398 unsigned long jiffies_lastprogress;
400 struct list_head kpackets_waiting;
401 struct list_head conn_retrans_waiting;
402 struct list_head announce_waiting;
403 struct list_head neighbors_waiting;
405 unsigned long jiffies_lastdrop;
407 __u32 numconns;
408 __u64 priority_sum;
410 atomic_t cong_status;
415 * switch to and from RB_INQUEUE_NBCONGWIN is only done with nbcongwin.lock
416 * *and* qlock held
418 #define RB_INQUEUE_FALSE 0
419 #define RB_INQUEUE_TRUE 1
420 #define RB_INQUEUE_NBCONGWIN 2 /* only for nb->rb */
422 struct resume_block{
423 struct list_head lh;
424 __u8 in_queue;
427 #define ANNOUNCE_TYPE_BROADCAST 1
428 #define ANNOUNCE_TYPE_UNICAST 2
430 struct announce_data{
431 struct kref ref;
433 struct list_head lh;
434 __u8 type;
435 __u16 sndcnt;
436 struct net_device *dev;
437 char mac[MAX_ADDR_LEN];
438 struct delayed_work announce_work;
439 struct resume_block rb;
442 struct ping_cookie{
443 ktime_t time_created;
444 ktime_t time_sent;
445 unsigned long jiffies_sent;
447 __u32 cookie;
448 __u8 pongs; /* count of pongs for pings sent after this one */
451 #define NEIGHBOR_STATE_INITIAL 0
452 #define NEIGHBOR_STATE_ACTIVE 1
453 #define NEIGHBOR_STATE_STALLED 2
454 #define NEIGHBOR_STATE_KILLED 3
456 #define NBCONGWIN_SHIFT 16
457 #define NBCONGWIN_MUL (1 << NBCONGWIN_SHIFT)
459 struct neighbor{
460 struct list_head nb_list;
462 struct kref ref;
464 struct net_device *dev;
465 char mac[MAX_ADDR_LEN];
466 struct qos_queue *queue;
468 __be32 sessionid;
470 atomic_t sessionid_rcv_needed;
471 atomic_t sessionid_snd_needed;
473 char *addr;
474 __u16 addrlen;
476 struct timer_list cmsg_timer;
477 spinlock_t cmsg_lock;
478 struct list_head cmsg_queue_pong;
479 struct list_head cmsg_queue_ack;
480 struct list_head cmsg_queue_ackconn;
481 struct list_head cmsg_queue_conndata_lowlat;
482 struct list_head cmsg_queue_conndata_highlat;
483 struct list_head cmsg_queue_other;
484 __u8 add_retrans_needed;
485 __u64 kpacket_seqno; /* not locked, only accessed by single tasklet */
487 struct rb_root pending_conn_resets_rb;
489 unsigned long timeout;
491 __u32 cmsg_pongslength;
492 __u32 cmsg_otherlength;
494 __u32 cmsg_pongscnt; /* size of queue only, protected by cmsg_lock */
495 atomic_t cmsg_pongs_retrans_cnt; /* number of retransmits only */
496 atomic_t cmsg_othercnt; /* size of queue + retransmits */
498 atomic_t cmsg_bulk_readds;
500 atomic_t cmsg_delay_conndata;
502 /* not locked, only accessed by single tasklet */
503 __u8 max_cmsg_delay_sent;
505 /* procected by qos_queue->qlock */
506 struct resume_block rb_kp;
507 struct resume_block rb_cr;
508 struct resume_block rb;
510 struct{
511 spinlock_t lock;
512 struct list_head lh;
513 struct list_head lh_nextpass;
514 __u32 cnt;
515 __u64 priority_sum;
516 }conns_waiting;
518 struct{
519 spinlock_t lock;
520 atomic64_t data_intransit;
521 atomic64_t cwin;
522 __u64 cwin_shrinkto;
523 }nbcongwin;
525 spinlock_t state_lock;
526 unsigned long last_ping_time;
528 struct ping_cookie cookies[PING_COOKIES_PER_NEIGH];
529 __u32 ping_intransit;
530 __u32 lastcookie;
531 __u32 cookie_unsent;
532 __u64 latency_variance_retrans_us; /* microsecs */
533 atomic_t latency_retrans_us; /* microsecs */
534 atomic_t latency_stddev_retrans_us; /* microsecs */
535 atomic_t latency_advertised_us; /* microsecs */
536 atomic_t max_remote_ack_delay_us; /* microsecs */
537 atomic_t max_remote_ackconn_delay_us; /* microsecs */
538 atomic_t max_remote_other_delay_us; /* microsecs */
540 union {
541 unsigned long initial_state_since;/* initial state */
543 * last_roundtrip:
544 * time of the last sent packet which has been acked or
545 * otherwise responded to (e.g. pong)
547 unsigned long last_roundtrip;/* active/stalled state */
548 }state_time;
549 ktime_t last_roundtrip_end;
550 __u16 ping_success;
551 __u8 state;
553 __u8 str_timer_pending;
554 struct delayed_work stalltimeout_timer;
556 spinlock_t connid_lock;
557 struct rb_root connid_rb;
559 spinlock_t connid_reuse_lock;
560 struct rb_root connid_reuse_rb;
561 struct list_head connid_reuse_list;
562 __u16 connid_reuse_pingcnt;
564 atomic64_t priority_sum;
567 * connecions which receive data from/send data to this node
568 * used when terminating all connections of a neighbor and terminating
569 * inactive connections
571 spinlock_t conn_list_lock;
572 struct list_head rcv_conn_list;
574 spinlock_t stalledconn_lock;
575 struct work_struct stalledconn_work;
576 __u8 stalledconn_work_scheduled;
577 struct list_head stalledconn_list;
580 * the timer has to be inited when adding the neighbor
581 * timer_setup(...);
582 * add_timer(struct timer_list * timer);
584 spinlock_t retrans_lock;
585 struct timer_list retrans_timer;
586 struct list_head retrans_list;
587 struct rb_root kp_retransmits_rb;
589 spinlock_t retrans_conn_lock;
590 struct timer_list retrans_conn_timer;
591 struct list_head retrans_conn_list;
594 #define DATABUF_BUF 0
595 #define DATABUF_SKB 1
597 struct data_buf_item{
598 struct list_head buf_list;
600 char *buf;
601 __u16 datalen;
602 __u16 buflen;
604 __u8 type;
607 struct connid_reuse_item{
608 struct rb_node rbn;
610 struct list_head lh;
612 struct kref ref;
613 __u32 conn_id;
614 __u16 pingcnt;
617 #define SNDSPEED_INIT 0
618 #define SNDSPEED_ACTIVE 1
619 struct snd_speed{
620 __u8 state;
621 __u8 flushed;
622 unsigned long jiffies_last_refresh;
623 __u32 bytes_sent;
625 /* bytes per second */
626 __u32 speed;
627 __u32 speed_limited;
630 struct cor_sock;
633 * There are 2 conn objects per bi-directional connection. They refer to each
634 * other with in the reversedir field.
638 * Naming:
640 * cn: conn we have no clue what is inside
641 * src_in, trgt_unconn, trgt_out, ...: A conn with the specified source or
642 * targettype. In the unlocked case the types are actually just a guess,
643 * because they could have changed since the last access. After locking the
644 * source/destination parameters have to be checked whether they still are
645 * what we expect. This includes source/targettype, neighbor, conn_id
647 * Exception: they may not change after they are set to source/target sock
648 * until the socket is released.
651 * Naming suffixes:
652 * no suffix: unlocked
654 * _l: this direction is locked
656 * _ll: both directions are locked
658 * _lx: this direction is locked, the other direction may be locked
660 * _o: unlocked, but source or target is known for sure, because an outside
661 * lock is taken; For variables on the heap this means that an outside lock must
662 * be taken before accessing the struct which points to the conn can be
663 * accessed.
667 * Locking:
668 * The following fields are immutable after the conn has been allocated:
669 * is_client, reversedir
671 * Most fields are protected by rcv_lock. Fields which which control
672 * source and destination of the data flow require both directions to
673 * to be locked and external references to be cleared before the change can
674 * happen. This includes fields like sourcetype, targettype, connid,
675 * list_heads, htab_entries, ???. In this case the side with is_client == 1
676 * needs to be locked first. Changes to conn_id and neighbor also require
677 * removing the conn from the htables first.
679 * Some other fields are locked outside (e.g. at struct neighbor).
681 #define SOURCE_UNCONNECTED 0
682 #define SOURCE_IN 1
683 #define SOURCE_SOCK 2
685 #define TARGET_UNCONNECTED 0
686 #define TARGET_OUT 1
687 #define TARGET_SOCK 2
688 #define TARGET_DISCARD 3
690 #define BUFSIZE_NOACTION 0
691 #define BUFSIZE_DECR 1
692 #define BUFSIZE_DECR_FAST 2
693 #define BUFSIZE_INCR 3
694 #define BUFSIZE_INCR_FAST 4
696 #define BUFSIZE_SHIFT 5
698 #define SOCKTYPE_RAW 0
699 #define SOCKTYPE_MANAGED 1
701 #define RCV_BUF_STATE_OK 0
702 #define RCV_BUF_STATE_INCOMPLETE 1
703 #define RCV_BUF_STATE_RESET 2
705 struct conn{
706 __u8 sourcetype:4,
707 targettype:4;
709 __u8 is_client:1,
710 is_highlatency:1;
713 * isreset values:
714 * 0... connection active
715 * 1... connection is about to be reset, target does not need to be
716 * notified
717 * 2... connection is reset
718 * 3... connection is reset + no pointers to "struct conn *reversedir"
719 * remaining except from this conn
721 __u8 isreset;
723 __u8 flush;
725 struct kref ref;
727 spinlock_t rcv_lock;
729 union{
730 struct{
731 struct neighbor *nb;
732 /* list of all connections from this neighbor */
733 struct list_head nb_list;
735 struct list_head reorder_queue;
736 __u32 reorder_memused;
738 struct rb_node rbn;
739 struct connid_reuse_item *cir;
740 __u32 conn_id;
741 __u64 next_seqno;
743 /* number of ack sent, not data seqno */
744 __u32 ack_seqno;
746 __u16 small_ooo_packets;
748 __u32 priority;
749 __u8 priority_seqno;
750 __u8 inorder_ack_needed;
752 __u8 established;
754 __u64 window_seqnolimit;
755 __u64 window_seqnolimit_remote;
757 /* protected by nb->cmsg_lock */
758 struct list_head acks_pending;
760 unsigned long jiffies_last_act;
761 }in;
763 struct{
764 struct cor_sock *cs;
766 struct list_head cl_list;
768 __u32 priority;
770 struct snd_speed snd_speed;
771 }sock;
772 }source;
774 union{
775 struct{
776 __u32 paramlen;
777 __u32 cmdread;
778 __u16 cmd;
779 __u8 paramlen_read;
780 char *cmdparams;
781 char paramlen_buf[4];
782 }unconnected;
784 struct{
785 struct neighbor *nb;
786 /* protected by nb->retrans_conn_lock, sorted by seqno
788 struct list_head retrans_list;
790 /* protected by nb->stalledconn_lock */
791 struct list_head nbstalled_lh;
793 __u32 conn_id;
794 __u64 seqno_nextsend;
795 __u64 seqno_acked;
796 __u64 seqno_windowlimit;
798 struct resume_block rb;
800 __u32 rb_priority;
801 __u16 maxsend_extra;
803 __u16 burst_bytes;
805 __u8 priority_last;
806 __u8 priority_seqno;
807 __u8 priority_send_allowed;
809 __u8 windowlimit_reached;
811 __u8 established;
813 /* protected by nb->retrans_conn_lock */
814 __u16 retrans_lowwindow;
815 }out;
817 struct{
818 __u8 waiting_for_userspace;
819 unsigned long waiting_for_userspace_since;
821 struct cor_sock *cs;
823 __u8 socktype;
825 __u8 rcv_buf_state;
826 char rcv_hdr[CONN_MNGD_HEADERLEN];
827 char rcv_chksum[CONN_MNGD_CHECKSUMLEN];
828 char *rcv_buf;
829 __u16 rcvd;
830 __u16 rcv_hdr_flags;
831 __u16 rcv_data_len;
832 }sock;
833 }target;
835 struct{
836 struct list_head items;
837 struct data_buf_item *nextread;
838 __u64 first_offset;
840 __u32 datasize;
841 __u32 overhead;
842 __u32 read_remaining;
844 __u16 next_read_offset;
845 }data_buf;
847 __u32 bufspace_accounted;
849 struct{
850 __u32 bufsize; /* 32 ==> 1 byte, see BUFSIZE_SHIFT */
851 __u32 ignore_rcv_lowbuf;
852 union{
853 struct{
854 __u32 bytesleft;
855 }noact;
857 struct{
858 __u32 size_start;
859 }decr;
861 struct{
862 __u32 size_start;
863 __u32 size_end;
864 }incr;
865 }act;
867 __u32 state:3,
868 bytes_rcvd:24;
869 }bufsize;
871 struct conn *reversedir;
874 #define CONN_RETRANS_INITIAL 0
875 #define CONN_RETRANS_SCHEDULED 1
876 #define CONN_RETRANS_LOWWINDOW 2
877 #define CONN_RETRANS_SENDING 3
878 #define CONN_RETRANS_ACKED 4
879 struct conn_retrans {
880 /* timeout_list and conn_list share a single ref */
881 struct kref ref;
882 /* only in timeout_list if state == CONN_RETRANS_SCHEDULED */
883 struct list_head timeout_list;
884 struct list_head conn_list;
885 struct conn *trgt_out_o;
886 __u64 seqno;
887 __u32 length;
889 __u8 snd_delayed_lowbuf;
890 __u8 state;
891 unsigned long timeout;
894 #define RCVOOO_BUF 0
895 #define RCVOOO_SKB 1
896 struct rcvooo{
897 struct list_head lh;
898 __u64 seqno;
899 __u8 type;
900 __u8 flush;
903 struct rcvooo_buf{
904 struct rcvooo r;
905 char *data;
906 __u32 len;
909 /* inside skb->cb */
910 struct skb_procstate{
911 union{
912 struct{
913 struct work_struct work;
914 }announce1;
916 struct{
917 __u32 offset;
918 }announce2;
920 struct{
921 __u32 skb_memused;
922 struct rcvooo r;
923 }rcv_ooo;
925 struct{
926 struct data_buf_item dbi;
927 }rcv;
928 }funcstate;
931 #define CS_TYPE_UNCONNECTED 0
932 #define CS_TYPE_LISTENER 1
933 #define CS_TYPE_CONN_RAW 2
934 #define CS_TYPE_CONN_MANAGED 3
936 #define CS_CONNECTSTATE_UNCONNECTED 0
937 #define CS_CONNECTSTATE_CONNECTING 1
938 #define CS_CONNECTSTATE_CONNECTED 2
939 #define CS_CONNECTSTATE_ERROR 3
941 #define CS_SHUTDOWN_SHUTDOWN_RD (1 << 0)
942 #define CS_SHUTDOWN_SHUTDOWN_WR (1 << 1)
943 #define CS_SHUTDOWN_SENT_EOF (1 << 2)
944 #define CS_SHUTDOWN_SENT_RCVEND (1 << 3)
945 #define CS_SHUTDOWN_RCVD_EOF (1 << 4)
946 #define CS_SHUTDOWN_RCVD_RCVEND (1 << 5)
948 struct cor_sock {
949 struct sock sk; /* must be first */
951 struct mutex lock;
952 struct kref ref;
954 /* type may not change once it is set to != CS_TYPE_UNCONNECTED */
955 __u8 type;
956 __u8 isreleased;
958 __u8 publish_service;
960 union {
961 struct {
962 /* listener is protected by cor_bindnodes */
963 struct list_head lh;
964 __be16 port;
965 __u8 publish_service;
966 __u32 queue_maxlen;
967 __u32 queue_len;
968 struct list_head conn_queue;
969 }listener;
971 struct {
972 struct conn *src_sock;
973 struct conn *trgt_sock;
975 struct data_buf_item *rcvitem;
976 __u16 rcvoffset;
978 __u8 snd_delayed_lowbuf;
980 struct cor_sock *pass_on_close;
981 }conn_raw;
983 struct {
984 struct cor_sockaddr remoteaddr;
986 struct list_head rd_msgs;
987 struct list_head crd_lh;
988 __u8 in_crd_list;
990 __u8 connect_state;
992 __u8 is_reset;
994 __u8 flush;
996 __u8 shutdownflags;
998 __u8 snd_delayed_lowbuf;
1000 __be64 cookie;
1001 struct rb_node rbn;
1003 struct conn *src_sock;
1004 struct conn *trgt_sock;
1006 char snd_hdr[CONN_MNGD_HEADERLEN];
1007 char snd_chksum[CONN_MNGD_CHECKSUMLEN];
1008 char *snd_buf;
1009 __u16 snd_segment_size;
1010 __u16 snd_hdr_flags;
1011 __u16 snd_data_len;
1012 __u16 sent;
1013 /* protected by cor_sock->lock */
1014 __u8 in_flushtoconn_oom_list;
1015 /* protected by flushtoconn_oom_lock */
1016 struct list_head flushtoconn_oom_lh;
1018 /* receiving */
1019 char rcv_hdr[CONN_MNGD_HEADERLEN];
1020 char rcv_chksum[CONN_MNGD_CHECKSUMLEN];
1021 char *rcv_buf;
1022 __u16 rcv_hdr_flags;
1023 __u16 rcv_data_len;
1024 __u16 rcvbuf_consumed;
1025 __u8 rcv_buf_state;
1026 }conn_managed;
1027 }data;
1029 struct work_struct readfromconn_work;
1030 atomic_t readfromconn_work_scheduled;
1032 atomic_t ready_to_read;
1033 atomic_t ready_to_write;
1034 atomic_t ready_to_accept;
1039 /* common.c */
1040 extern atomic_t num_conns;
1042 extern spinlock_t cor_bindnodes;
1044 extern struct conn *get_conn(struct neighbor *nb, __u32 conn_id);
1046 extern void delete_connid_reuse_items(struct neighbor *nb);
1048 extern void connid_used_pingsuccess(struct neighbor *nb);
1050 extern void _set_last_act(struct conn *src_in_l);
1052 extern void free_conn(struct kref *ref);
1054 extern int conn_init_out(struct conn *trgt_unconn_ll, struct neighbor *nb,
1055 __u32 rcvd_connid, int use_rcvd_connid);
1057 extern void conn_init_sock_source(struct conn *cn);
1059 extern void conn_init_sock_target(struct conn *cn);
1061 extern __u32 list_services(char *buf, __u32 buflen);
1063 extern void set_publish_service(struct cor_sock *cs, __u8 value);
1065 extern void close_port(struct cor_sock *cs);
1067 extern int open_port(struct cor_sock *cs_l, __be16 port);
1069 extern int connect_port(struct conn *trgt_unconn_ll, __be16 port);
1071 extern int connect_neigh(struct conn *trgt_unconn_ll, char *addr,
1072 __u16 addrlen);
1074 extern struct conn* alloc_conn(gfp_t allocflags);
1076 extern void reset_conn_locked(struct conn *cn_ll);
1078 extern void reset_conn(struct conn *cn);
1080 /* util.c */
1081 extern __u8 __attribute__((const)) enc_log_256_16(__u32 value);
1083 extern __u32 __attribute__((const)) dec_log_256_16(__u8 value);
1085 extern __u8 __attribute__((const)) enc_log_64_11(__u32 value);
1087 extern __u32 __attribute__((const)) dec_log_64_11(__u8 value);
1089 extern __u8 __attribute__((const)) enc_log_64_7(__u64 value);
1091 extern __u64 __attribute__((const)) dec_log_64_7(__u8 value);
1093 extern void kreffree_bug(struct kref *ref);
1095 extern int __init cor_util_init(void);
1097 /* credits.c */
1098 extern int newconn_checkpriority(struct neighbor *nb, __u8 priority);
1100 extern __u32 refresh_conn_priority(struct conn *cn, int locked);
1102 extern void set_conn_in_priority(struct neighbor *nb, __u32 conn_id,
1103 struct conn *src_in, __u8 priority_seqno, __u8 priority);
1105 extern void connreset_priority(struct conn *cn);
1107 extern int __init credits_init(void);
1109 /* neighbor.c */
1110 extern void neighbor_free(struct kref *ref);
1112 extern int is_from_nb(struct sk_buff *skb, struct neighbor *nb);
1114 extern struct neighbor *get_neigh_by_mac(struct sk_buff *skb);
1116 extern struct neighbor *find_neigh(char *addr, __u16 addrlen);
1118 extern __u32 generate_neigh_list(char *buf, __u32 buflen);
1120 extern int get_neigh_state(struct neighbor *nb);
1122 extern void ping_resp(struct neighbor *nb, __u32 cookie, __u32 respdelay);
1124 extern __u32 add_ping_req(struct neighbor *nb, unsigned long *last_ping_time,
1125 ktime_t now);
1127 extern void ping_sent(struct neighbor *nb, __u32 cookie);
1129 extern void unadd_ping_req(struct neighbor *nb, __u32 cookie,
1130 unsigned long last_ping_time, int congested);
1132 #define TIMETOSENDPING_NO 0
1133 #define TIMETOSENDPING_YES 1
1134 #define TIMETOSENDPING_FORCE 2
1135 extern int time_to_send_ping(struct neighbor *nb);
1137 extern unsigned long get_next_ping_time(struct neighbor *nb);
1139 extern int force_ping(struct neighbor *nb);
1141 extern int rcv_announce(struct sk_buff *skb);
1143 extern int _send_announce(struct announce_data *ann, int fromqos, int *sent);
1145 extern void announce_data_free(struct kref *ref);
1147 extern void announce_send_stop(struct net_device *dev, char *mac, int type);
1149 extern void cor_neighbor_down(void);
1151 extern int cor_neighbor_up(char *addr2, __u32 addrlen2);
1153 extern int is_clientmode(void);
1155 extern int __init cor_neighbor_init(void);
1157 /* rcv.c */
1158 extern void reset_ooo_queue(struct conn *src_in_lx);
1160 extern void drain_ooo_queue(struct conn *src_in_l);
1162 extern void conn_rcv(struct neighbor *nb, struct sk_buff *skb, char *data, __u32 len,
1163 __u32 conn_id, __u64 seqno, int rcv_delayed_lowbuf, __u8 flush);
1165 extern void cor_rcv_down(void);
1167 extern void cor_rcv_up(void);
1169 extern int __init cor_rcv_init(void);
1171 /* kpacket_parse.c */
1172 extern void kernel_packet(struct neighbor *nb, struct sk_buff *skb,
1173 __u64 seqno);
1175 /* kpacket_gen.c */
1176 struct control_msg_out;
1178 #define ACM_PRIORITY_LOW 1 /* oom recovery easy */
1179 #define ACM_PRIORITY_MED 2 /* oom may cause timeouts */
1180 #define ACM_PRIORITY_HIGH 3 /* cm acks - needed for freeing old cms */
1182 extern struct control_msg_out *alloc_control_msg(struct neighbor *nb,
1183 int priority);
1185 extern void free_control_msg(struct control_msg_out *cm);
1187 extern void retransmit_timerfunc(struct timer_list *retrans_timer);
1189 extern void kern_ack_rcvd(struct neighbor *nb, __u64 seqno);
1191 extern int send_messages(struct neighbor *nb, int *sent);
1193 extern void controlmsg_timerfunc(struct timer_list *cmsg_timer);
1195 extern void schedule_controlmsg_timer(struct neighbor *nb_cmsglocked);
1197 extern void send_pong(struct neighbor *nb, __u32 cookie);
1199 extern int send_reset_conn(struct neighbor *nb, __u32 conn_id, int lowprio);
1201 extern void send_ack(struct neighbor *nb, __u64 seqno);
1203 extern void send_ack_conn_ifneeded(struct conn *src_in_l, __u64 seqno_ooo,
1204 __u32 ooo_length);
1206 extern void send_priority(struct conn *trgt_out_ll, int force,
1207 __u8 priority);
1209 extern void free_ack_conns(struct conn *src_in_lx);
1211 extern void send_connect_success(struct control_msg_out *cm, __u32 conn_id,
1212 struct conn *src_in);
1214 extern void send_connect_nb(struct control_msg_out *cm, __u32 conn_id,
1215 __u64 seqno1, __u64 seqno2, struct conn *src_in_ll);
1217 extern void send_conndata(struct control_msg_out *cm, __u32 conn_id,
1218 __u64 seqno, char *data_orig, char *data, __u32 datalen,
1219 __u8 snd_delayed_lowbuf, __u8 flush, __u8 highlatency,
1220 struct conn_retrans *cr);
1222 extern int __init cor_kgen_init(void);
1224 /* cpacket_parse.c */
1225 extern int encode_len(char *buf, int buflen, __u32 len);
1227 extern void proc_cpacket(struct conn *trgt_unconn);
1229 extern int __init cor_cpacket_init(void);
1231 /* snd.c */
1232 extern void qos_set_lastdrop(struct qos_queue *q);
1234 #ifdef DEBUG_QOS_SLOWSEND
1235 extern int _cor_dev_queue_xmit(struct sk_buff *skb, int caller);
1236 #else
1237 static inline int _cor_dev_queue_xmit(struct sk_buff *skb, int caller)
1239 return dev_queue_xmit(skb);
1241 #endif
1243 static inline int cor_dev_queue_xmit(struct sk_buff *skb, struct qos_queue *q,
1244 int caller)
1246 int rc = _cor_dev_queue_xmit(skb, caller);
1247 if (unlikely(rc != NET_XMIT_SUCCESS))
1248 qos_set_lastdrop(q);
1249 return rc;
1252 extern void free_qos(struct kref *ref);
1254 #ifdef COR_NBCONGWIN
1255 extern void nbcongwin_data_acked(struct neighbor *nb, __u64 bytes_acked);
1256 #endif
1258 extern struct qos_queue *get_queue(struct net_device *dev);
1260 extern int destroy_queue(struct net_device *dev);
1262 extern int create_queue(struct net_device *dev);
1264 #define QOS_RESUME_DONE 0
1265 #define QOS_RESUME_CONG 1
1266 #define QOS_RESUME_NEXTNEIGHBOR 2 /* resume_neighbors() internal */
1268 #define QOS_CALLER_KPACKET 0
1269 #define QOS_CALLER_CONN_RETRANS 1
1270 #define QOS_CALLER_ANNOUNCE 2
1271 #define QOS_CALLER_NEIGHBOR 3
1273 extern void qos_enqueue(struct qos_queue *q, struct resume_block *rb,
1274 int caller);
1276 extern void qos_remove_conn(struct conn *trgt_out_l);
1278 extern int may_send_announce(struct net_device *dev);
1280 extern struct sk_buff *create_packet_cmsg(struct neighbor *nb, int size,
1281 gfp_t alloc_flags, __u64 seqno);
1283 extern void reschedule_conn_retrans_timer(struct neighbor *nb_retranslocked);
1285 extern void cancel_all_conn_retrans(struct conn *trgt_out_l);
1287 extern void retransmit_conn_taskfunc(unsigned long nb);
1289 extern void retransmit_conn_timerfunc(struct timer_list *retrans_timer_conn);
1291 extern void conn_ack_ooo_rcvd(struct neighbor *nb, __u32 conn_id,
1292 struct conn *trgt_out, __u64 seqno_ooo, __u32 length,
1293 __u64 *bytes_acked);
1295 extern void conn_ack_rcvd(struct neighbor *nb, __u32 conn_id,
1296 struct conn *trgt_out, __u64 seqno, int setwindow, __u8 window,
1297 __u64 *bytes_acked);
1299 extern void schedule_retransmit_conn(struct conn_retrans *cr, int connlocked,
1300 int nbretrans_locked);
1302 extern int srcin_buflimit_reached(struct conn *src_in_lx);
1304 /* RC_FLUSH_CONN_OUT_SENT | RC_FLUSH_CONN_OUT_{^SENT} */
1305 #define RC_FLUSH_CONN_OUT_OK 1
1306 #define RC_FLUSH_CONN_OUT_SENT_CONG 2 /* flush_out internal only */
1307 #define RC_FLUSH_CONN_OUT_NBNOTACTIVE 3
1308 #define RC_FLUSH_CONN_OUT_CONG 4
1309 #define RC_FLUSH_CONN_OUT_MAXSENT 5
1310 #define RC_FLUSH_CONN_OUT_OOM 6
1312 extern int flush_out(struct conn *trgt_out_lx, __u32 *sent);
1314 extern void resume_nbstalled_conns(struct work_struct *work);
1316 extern int __init cor_snd_init(void);
1318 /* forward.c */
1319 extern struct kmem_cache *data_buf_item_slab;
1321 extern void databuf_init(struct conn *cn_init);
1323 extern void bufsize_init(struct conn *cn_l, __u32 bufsize);
1325 extern int account_bufspace(struct conn *cn_lx);
1327 extern int cpacket_write_allowed(struct conn *src_unconn_lx);
1329 extern void update_windowlimit(struct conn *src_in_lx);
1331 extern void bufsize_read_to_sock(struct conn *trgt_sock_lx);
1333 extern void databuf_ackdiscard(struct conn *cn_lx);
1335 extern void reset_seqno(struct conn *cn_l, __u64 initseqno);
1337 extern void databuf_pull(struct conn *cn_lx, char *dst, __u32 len);
1339 static inline __u32 databuf_trypull(struct conn *cn_l, char *dst, __u32 len)
1341 if (len > cn_l->data_buf.read_remaining)
1342 len = cn_l->data_buf.read_remaining;
1343 databuf_pull(cn_l, dst, len);
1344 return len;
1347 extern void databuf_unpull_dpi(struct conn *trgt_sock, struct cor_sock *cs,
1348 struct data_buf_item *item, __u16 next_read_offset);
1350 extern void databuf_pull_dbi(struct cor_sock *cs_rl, struct conn *trgt_sock_l);
1352 extern void databuf_unpull(struct conn *trgt_out_l, __u32 bytes);
1354 extern void databuf_pullold(struct conn *trgt_out_l, __u64 startpos, char *dst,
1355 int len);
1357 extern void databuf_ack(struct conn *trgt_out_l, __u64 pos);
1359 extern void databuf_ackread(struct conn *cn_lx);
1361 extern __u32 receive_buf(struct conn *cn_lx, char *buf, __u32 datalen,
1362 int rcv_delayed_lowbuf, __u8 flush);
1364 extern __u32 receive_skb(struct conn *src_in_l, struct sk_buff *skb,
1365 int rcv_delayed_lowbuf, __u8 flush);
1367 extern void wake_sender(struct conn *cn);
1369 extern int __init forward_init(void);
1371 /* sock_rdaemon.c */
1372 extern int cor_create_rdaemon_sock(struct net *net, struct socket *sock,
1373 int protocol, int kern);
1375 extern int rdreq_connect(struct cor_sock *cs);
1377 extern void cor_usersock_release(struct cor_sock *cs);
1379 extern int __init cor_rd_init1(void);
1381 extern int __init cor_rd_init2(void);
1383 /* sock_raw.c */
1384 extern int cor_create_raw_sock(struct net *net, struct socket *sock,
1385 int protocol, int kern);
1387 /* sock_managed.c */
1388 extern struct cor_sock *get_corsock_by_cookie(__be64 cookie);
1390 extern void __set_sock_connecterror(struct cor_sock *cs_m_l, int errorno);
1392 extern void _set_sock_connecterror(struct cor_sock *cs, int errorno);
1394 static inline void set_sock_connecterror(__be64 cookie, int errorno)
1396 struct cor_sock *cs = get_corsock_by_cookie(cookie);
1397 _set_sock_connecterror(cs, errorno);
1400 extern void flush_sock_managed(struct conn *trgt_sock_lx, int from_recvmsg,
1401 __u8 *do_wake_sender);
1403 extern void cor_mngdsocket_readfromconn_fromatomic(struct cor_sock *cs);
1405 extern void cor_mngdsocket_readfromconn_wq(struct work_struct *work);
1407 extern int cor_create_managed_sock(struct net *net, struct socket *sock,
1408 int protocol, int kern);
1410 extern int __init cor_sock_managed_init1(void);
1412 /* sock.c */
1413 extern void free_sock(struct kref *ref);
1415 extern void flush_sock(struct conn *trgt_sock_lx);
1417 extern void update_src_sock_sndspeed(struct conn *src_sock_l, __u32 bytes_sent);
1419 extern int cor_sock_sndbufavailable(struct conn *src_sock_lx);
1421 extern int cor_socket_socketpair(struct socket *sock1, struct socket *sock2);
1423 extern int cor_socket_getname(struct socket *sock, struct sockaddr *addr,
1424 int peer);
1426 extern int cor_socket_mmap(struct file *file, struct socket *sock,
1427 struct vm_area_struct *vma);
1429 extern int _cor_createsock(struct net *net, struct socket *sock, int protocol,
1430 int kern);
1432 extern int __init cor_sock_init1(void);
1434 extern int __init cor_sock_init2(void);
1437 static inline struct skb_procstate *skb_pstate(struct sk_buff *skb)
1439 return (struct skb_procstate *) &(skb->cb[0]);
1442 static inline struct sk_buff *skb_from_pstate(struct skb_procstate *ps)
1444 return (struct sk_buff *) (((char *)ps) - offsetof(struct sk_buff,cb));
1447 static inline int qos_fastsend_allowed_conn_retrans(struct neighbor *nb)
1449 return atomic_read(&(nb->queue->cong_status)) < CONGSTATUS_RETRANS;
1452 static inline int qos_fastsend_allowed_announce(struct net_device *dev)
1454 int rc;
1455 struct qos_queue *q = get_queue(dev);
1457 if (q == 0)
1458 return 0;
1460 rc = atomic_read(&(q->cong_status)) < CONGSTATUS_ANNOUNCE;
1462 kref_put(&(q->ref), free_qos);
1464 return rc;
1467 static inline int qos_fastsend_allowed_conn(struct conn *trgt_out_lx)
1469 struct qos_queue *q = trgt_out_lx->target.out.nb->queue;
1470 return atomic_read(&(q->cong_status)) < CONGSTATUS_CONNDATA;
1473 static inline __u32 mss(struct neighbor *nb, __u32 l3overhead)
1475 __u32 mtu = (nb->dev->mtu > 4096) ? 4096 : nb->dev->mtu;
1476 return mtu - LL_RESERVED_SPACE(nb->dev) - l3overhead;
1479 static inline __u32 mss_cmsg(struct neighbor *nb)
1481 return mss(nb, 7);
1484 static inline __u32 mss_conndata(struct neighbor *nb)
1486 __u32 mss_tmp = mss(nb, 11);
1487 __u32 i;
1489 if (mss_tmp < 256)
1490 return mss_tmp;
1492 for (i=256;i<4096;i*=2) {
1493 if (i*2 > mss_tmp)
1494 return i;
1497 return mss_tmp - mss_tmp%4096;
1500 static inline __u32 send_conndata_as_skb(struct neighbor *nb, __u32 size)
1502 return size >= mss_conndata(nb)/2;
1505 static inline long calc_timeout(__u32 latency_us, __u32 latency_stddev_us,
1506 __u32 max_remote_ack_delay_us)
1508 unsigned long addto;
1509 if (unlikely(unlikely(latency_us > 1000000000) ||
1510 unlikely(latency_stddev_us > 500000000) ||
1511 unlikely(max_remote_ack_delay_us > 1000000000))) {
1512 addto = msecs_to_jiffies(latency_us/1000 + latency_us/4000 +
1513 latency_stddev_us/333 +
1514 max_remote_ack_delay_us/1000);
1515 } else {
1516 addto = usecs_to_jiffies(latency_us + latency_us/4 +
1517 latency_stddev_us*3 + max_remote_ack_delay_us);
1521 * 2 is added because
1522 * 1) _to_jiffies rounds down, but should round up, so add 1 to
1523 * compensate
1524 * 2) even if latency is 0, we never want to schedule the retransmit
1525 * to run right now, so add 1 more
1527 return jiffies + 2 + addto;
1530 static inline void put_be64(char *dst, __be64 value)
1532 char *p_value = (char *) &value;
1534 dst[0] = p_value[0];
1535 dst[1] = p_value[1];
1536 dst[2] = p_value[2];
1537 dst[3] = p_value[3];
1538 dst[4] = p_value[4];
1539 dst[5] = p_value[5];
1540 dst[6] = p_value[6];
1541 dst[7] = p_value[7];
1544 static inline void put_u64(char *dst, __u64 value)
1546 put_be64(dst, cpu_to_be64(value));
1549 static inline void put_u48(char *dst, __u64 value)
1551 char *p_value = (char *) &value;
1553 value = cpu_to_be64(value);
1555 dst[0] = p_value[2];
1556 dst[1] = p_value[3];
1557 dst[2] = p_value[4];
1558 dst[3] = p_value[5];
1559 dst[4] = p_value[6];
1560 dst[5] = p_value[7];
1563 static inline void put_be32(char *dst, __be32 value)
1565 char *p_value = (char *) &value;
1566 dst[0] = p_value[0];
1567 dst[1] = p_value[1];
1568 dst[2] = p_value[2];
1569 dst[3] = p_value[3];
1572 static inline void put_u32(char *dst, __u32 value)
1574 put_be32(dst, cpu_to_be32(value));
1577 static inline void put_be16(char *dst, __be16 value)
1579 char *p_value = (char *) &value;
1580 dst[0] = p_value[0];
1581 dst[1] = p_value[1];
1584 static inline void put_u16(char *dst, __u16 value)
1586 put_be16(dst, cpu_to_be16(value));
1589 static inline char *cor_pull_skb(struct sk_buff *skb, unsigned int len)
1591 char *ptr = skb_pull(skb, len);
1593 if (unlikely(ptr == 0))
1594 return 0;
1596 return ptr - len;
1599 static inline __be64 parse_be64(char *buf)
1601 __u64 ret = 0;
1603 BUG_ON(buf == 0);
1605 ((char *)&ret)[0] = buf[0];
1606 ((char *)&ret)[1] = buf[1];
1607 ((char *)&ret)[2] = buf[2];
1608 ((char *)&ret)[3] = buf[3];
1609 ((char *)&ret)[4] = buf[4];
1610 ((char *)&ret)[5] = buf[5];
1611 ((char *)&ret)[6] = buf[6];
1612 ((char *)&ret)[7] = buf[7];
1614 return ret;
1617 static inline __u64 parse_u64(char *buf)
1619 return be64_to_cpu(parse_be64(buf));
1622 static inline __u64 parse_u48(char *ptr)
1624 __u64 ret = 0;
1626 ((char *)&ret)[0] = 0;
1627 ((char *)&ret)[1] = 0;
1628 ((char *)&ret)[2] = ptr[0];
1629 ((char *)&ret)[3] = ptr[1];
1630 ((char *)&ret)[4] = ptr[2];
1631 ((char *)&ret)[5] = ptr[3];
1632 ((char *)&ret)[6] = ptr[4];
1633 ((char *)&ret)[7] = ptr[5];
1635 return be64_to_cpu(ret);
1638 static inline __be32 parse_be32(char *ptr)
1640 __u32 ret = 0;
1642 BUG_ON(ptr == 0);
1644 ((char *)&ret)[0] = ptr[0];
1645 ((char *)&ret)[1] = ptr[1];
1646 ((char *)&ret)[2] = ptr[2];
1647 ((char *)&ret)[3] = ptr[3];
1649 return ret;
1652 static inline __u32 parse_u32(char *ptr)
1654 return be32_to_cpu(parse_be32(ptr));
1657 static inline __be16 parse_be16(char *ptr)
1659 __u16 ret = 0;
1661 BUG_ON(ptr == 0);
1663 ((char *)&ret)[0] = ptr[0];
1664 ((char *)&ret)[1] = ptr[1];
1666 return ret;
1669 static inline __u16 parse_u16(char *ptr)
1671 return be16_to_cpu(parse_be16(ptr));
1674 static inline __u64 pull_u48(struct sk_buff *skb)
1676 return parse_u48(cor_pull_skb(skb, 6));
1679 static inline __be32 pull_be32(struct sk_buff *skb)
1681 return parse_be32(cor_pull_skb(skb, 4));
1684 static inline __u32 pull_u32(struct sk_buff *skb)
1686 return parse_u32(cor_pull_skb(skb, 4));
1689 static inline __u16 pull_u16(struct sk_buff *skb)
1691 return parse_u16(cor_pull_skb(skb, 2));
1694 static inline __u8 pull_u8(struct sk_buff *skb)
1696 char *ptr = cor_pull_skb(skb, 1);
1697 BUG_ON(ptr == 0);
1698 return *ptr;
1701 static inline int is_conn_in(struct conn *cn_l, struct neighbor *nb,
1702 __u32 conn_id)
1704 if (unlikely(unlikely(cn_l->sourcetype != SOURCE_IN) ||
1705 unlikely(cn_l->source.in.nb != nb) ||
1706 unlikely(cn_l->source.in.conn_id != conn_id) ||
1707 unlikely(cn_l->isreset != 0)))
1708 return 0;
1709 return 1;
1712 static inline int is_src_sock(struct conn *cn_l, struct cor_sock *cs)
1714 if (unlikely(unlikely(cn_l->sourcetype != SOURCE_SOCK) ||
1715 unlikely(cn_l->source.sock.cs != cs)))
1716 return 0;
1717 return 1;
1720 static inline int is_trgt_sock(struct conn *cn_l, struct cor_sock *cs)
1722 if (unlikely(unlikely(cn_l->targettype != TARGET_SOCK) ||
1723 unlikely(cn_l->target.sock.cs != cs)))
1724 return 0;
1725 return 1;
1728 static inline void set_last_act(struct conn *src_in_l)
1730 unsigned long jiffies_tmp = jiffies;
1732 BUG_ON(src_in_l->sourcetype != SOURCE_IN);
1734 if (unlikely(time_after(jiffies_tmp,
1735 src_in_l->source.in.jiffies_last_act +
1736 HZ * CONN_ACTIVITY_UPDATEINTERVAL_SEC)))
1737 _set_last_act(src_in_l);
1740 #define BUFLEN_MIN 128
1741 #define BUFLEN_MAX 4096
1742 #define PAGESIZE (1 << PAGE_SHIFT)
1744 static inline __u32 buf_optlen(__u32 datalen)
1746 __u32 optlen = BUFLEN_MIN;
1747 while (optlen < datalen && optlen < PAGESIZE && optlen < BUFLEN_MAX)
1748 optlen = (optlen << 1);
1749 return optlen;
1753 inline static void databuf_item_free(struct data_buf_item *item)
1755 if (item->type == DATABUF_BUF) {
1756 kfree(item->buf);
1757 kmem_cache_free(data_buf_item_slab, item);
1758 } else if (item->type == DATABUF_SKB) {
1759 struct sk_buff *skb = skb_from_pstate(container_of(item,
1760 struct skb_procstate, funcstate.rcv.dbi));
1761 kfree_skb(skb);
1762 } else {
1763 BUG();
1767 static inline __u64 seqno_clean(__u64 seqno)
1769 return seqno & ((1LL << 48) - 1);
1772 static inline int seqno_eq(__u64 seqno1, __u64 seqno2)
1774 seqno1 = seqno1 << 16;
1775 seqno2 = seqno2 << 16;
1776 return seqno1 == seqno2;
1779 static inline int seqno_before(__u64 seqno1, __u64 seqno2)
1781 seqno1 = seqno1 << 16;
1782 seqno2 = seqno2 << 16;
1783 return (seqno1 - seqno2) >= (1LL << 63);
1786 static inline int seqno_before_eq(__u64 seqno1, __u64 seqno2)
1788 return seqno_eq(seqno1, seqno2) || seqno_before(seqno1, seqno2);
1791 static inline int seqno_after(__u64 seqno1, __u64 seqno2)
1793 return seqno_before_eq(seqno1, seqno2) ? 0 : 1;
1796 static inline int seqno_after_eq(__u64 seqno1, __u64 seqno2)
1798 return seqno_before(seqno1, seqno2) ? 0 : 1;
1801 static inline int ktime_before_eq(ktime_t time1, ktime_t time2)
1803 return ktime_after(time1, time2) ? 0 : 1;
1806 static inline int ktime_after_eq(ktime_t time1, ktime_t time2)
1808 return ktime_before(time1, time2) ? 0 : 1;
1811 static inline __u64 update_atomic_sum(atomic64_t *atomic_sum, __u32 oldvalue,
1812 __u32 newvalue)
1814 __u64 sum_old = atomic64_read(atomic_sum);
1815 __u64 sum;
1817 while (1) {
1818 __u64 cmpxchg_ret;
1820 sum = sum_old;
1822 BUG_ON(sum < oldvalue);
1823 sum -= oldvalue;
1825 BUG_ON(sum + newvalue < sum);
1826 sum += newvalue;
1828 cmpxchg_ret = atomic64_cmpxchg(atomic_sum, sum_old, sum);
1830 if (likely(cmpxchg_ret == sum_old))
1831 break;
1833 sum_old = cmpxchg_ret;
1836 return sum;
1839 static inline void cor_sk_write_space(struct cor_sock *cs)
1841 atomic_set(&(cs->ready_to_write), 1);
1842 barrier();
1843 cs->sk.sk_write_space(&(cs->sk));
1846 static inline void cor_sk_data_ready(struct cor_sock *cs)
1848 atomic_set(&(cs->ready_to_read), 1);
1849 barrier();
1850 cs->sk.sk_data_ready(&(cs->sk));
1853 /* the other direction may be locked only if called from proc_cpacket */
1854 static inline void flush_buf(struct conn *cn_lx)
1856 if (unlikely(cn_lx->targettype == TARGET_UNCONNECTED)) {
1857 proc_cpacket(cn_lx);
1858 } else if (cn_lx->targettype == TARGET_SOCK) {
1859 flush_sock(cn_lx);
1860 } else if (cn_lx->targettype == TARGET_OUT) {
1861 __u32 bytessent = 0;
1862 flush_out(cn_lx, &bytessent);
1863 } else if (unlikely(cn_lx->targettype == TARGET_DISCARD)) {
1864 databuf_ackdiscard(cn_lx);
1865 } else {
1866 BUG();