reduce bufferbloat
[cor.git] / net / cor / cor.h
blob71e2c6ac0c0d08630783f0cf94aa30c8a68d1501
1 /**
2 * Connection oriented routing
3 * Copyright (C) 2007-2019 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
92 * KP_PING[1] cookie[4]
93 * KP_PONG[1] cookie[4] respdelay[4]
95 * This is needed to find out whether the other node is reachable. After a new
96 * neighbor is seen, ping requests are sent and the neighbor is only reachable
97 * after a few pongs are received. These requests are also used to find out
98 * whether a neighber is gone.
100 * respdelay:
101 * The receiver of a ping may delay the sending of the pong e.g. to create
102 * bigger packets. The respdelay is the time in microseconds the packet was
103 * delayed.
105 #define KP_PING 2
106 #define KP_PONG 3
108 /* KP_ACK[1] seqno[6] */
109 #define KP_ACK 4
112 * KP_ACK_CONN[1] conn_id[4] flags[1] seqno[6] window[1] seqno_ooo[6]
113 * length[1-4] priority_seqno[1] priority[1]
115 * conn_id is the conn_id we use if we sent something through this conn and
116 * *not* the conn_id that the neighbor used to send us the data
118 * flags defines which of the following fields are sent
120 * seqno = the seqno which is expected in the next non-out-of-order packet
122 * window = amount of data which can be sent without receiving the next ack
123 * packets with lower seqno do not overwrite the last window size
124 * The window may also be reduced. However, this only indicates a wish.
125 * Packets must be accepted if they exceed the new window, but not the old
126 * one.
128 * decode:
129 * 0 = 0
130 * 1...255 = 64*2^((value-1)/7) end result is rounded down to an integer
132 * seqno_ooo, length = This packet was received out of order. Maybe a previous
133 * packet has been lost. Out of order data should not be retransmitted.
134 * Multiple ooo packets may be merged into a single ack. Ooo packets may be
135 * partially accepted, so that the length does not cover the full packet and/
136 * or the seqno starts in the middle of a packet
138 #define KP_ACK_CONN 5
140 #define KP_ACK_CONN_FLAGS_SEQNO 1
141 #define KP_ACK_CONN_FLAGS_WINDOW 2
142 #define KP_ACK_CONN_FLAGS_OOO 12 /* 4+8 */
143 #define KP_ACK_CONN_FLAGS_PRIORITY 16
145 static inline __u8 ooolen_to_flags(__u32 len)
147 if (len == 0)
148 return 0;
149 if (len < 256)
150 return 4;
151 if (len < 65536)
152 return 8;
153 return 12;
156 static inline int ooolen(__u8 flags)
158 int len = ((flags & KP_ACK_CONN_FLAGS_OOO) >> 2);
159 if (unlikely(len == 3))
160 return 4;
161 return len;
164 static inline int ack_conn_len(__u8 flags)
166 int len = 0;
167 if ((flags & KP_ACK_CONN_FLAGS_SEQNO) != 0) {
168 len += 6;
169 if ((flags & KP_ACK_CONN_FLAGS_WINDOW) != 0)
170 len++;
173 if (ooolen(flags) != 0) {
174 len += 6;
175 len += ooolen(flags);
178 if (flags & KP_ACK_CONN_FLAGS_PRIORITY)
179 len += 2;
181 return len;
185 * NOTE on connection ids:
186 * connection ids we receive with most significant bit 0 have been generated by
187 * us
188 * connection ids we receive with most significant bit 1 have been generated by
189 * the other side
191 * ATTENTION: the priority seqno are reversed:
192 * priority seqnos we send are used when we send updates
193 * priority seqnos we received are used when we receive updates
197 * incoming connection
198 * seqno1... used to ack data sent from the side which initiated the connection
199 * seqno2... used to ack data sent to the side which initiated the connection
200 * KP_CONNECT[1] conn_id[4] seqno1[6] seqno2[6] window[1] priority_seqno[1]
201 * priority[1]
203 #define KP_CONNECT 6
206 * incoming connection successful,
207 * KP_CONNECT_SUCCESS[1] conn_id[4] window[1]
209 #define KP_CONNECT_SUCCESS 7
211 /* KP_CONN_DATA[1] conn_id[4] seqno[6] length[2] data[length] */
212 #define KP_CONN_DATA 8
213 #define KP_CONN_DATA_LOWBUFDELAYED 9
214 #define KP_CONN_DATA_FLUSH 10
215 #define KP_CONN_DATA_LOWBUFDELAYED_FLUSH 11
218 * KP_RESET_CONN[1] conn_id[4]
219 * We send this, if there is an established connection we want to close.
221 #define KP_RESET_CONN 12
224 * KP_SET_MAX_CMSG_DELAY[1] cpacket_ack_delay[4] data_ack_delay[4] cmsg_delay[4]
225 * Sent after connecting and at any change
226 * delay in specifies in microsecs
228 #define KP_SET_MAX_CMSG_DELAY 13
232 * Connection data which in interpreted when connection has no target yet
233 * These commands are sent by the end node.
235 * Format:
236 * cmd[2] length[1-4] parameter[length]
237 * unrecogniced commands are ignored
238 * parameters which are longer than expected are ignored as well
241 #define CD_CONTINUE_ON_ERROR_FLAG 32768
243 /* outgoing connection: CD_CONNECT_NB[2] length[1-4]
244 * addrlen[1-4] addr[addrlen] */
245 #define CD_CONNECT_NB 1
247 /* connection to local open part: CD_CONNECT_PORT[2] length[1-4] port[2] */
248 #define CD_CONNECT_PORT 2
251 * CD_LIST_NEIGH sends CDR_BINDATA if the command was successful. The response
252 * format is:
254 * numneighs[1-4]
255 * numfields[1-4] (field[2] fieldlen[1-4])[numfields]
256 * rows[responserows]:
257 * fieldlen[1-4], only if fieldlen in the header was "0"
258 * fielddata[fieldlen]
260 * Future versions may append data to field definition. Therefore clients must
261 * silently discard data at the end they do not expect.
264 /* list connected neighbors: CD_LIST_NEIGH[2] length[1-4] */
265 #define CD_LIST_NEIGH 3
268 * addr[fieldlen]
270 #define LIST_NEIGH_FIELD_ADDR 1
273 * latency_in_microsecs[1] (64_11 encoding)
274 * Only raw network latency in measured. Delays caused by the priority queues
275 * are *not* included.
277 #define LIST_NEIGH_FIELD_LATENCY 2
279 /* list services: CD_LIST_SERVICES[2] length[1-4] */
280 #define CD_LIST_SERVICES 4
284 * Connection data response
285 * Format is the same as with connection data
289 * CDR_EXECOK[1]
291 #define CDR_EXECOK 1
294 * CDR_EXECFAILED[1] reasoncode[2]
296 #define CDR_EXECFAILED 2
297 #define CDR_EXECFAILED_INVALID_COMMAND 1
298 #define CDR_EXECFAILED_TEMPORARILY_OUT_OF_RESSOURCES 2
299 #define CDR_EXECFAILED_NB_DOESNTEXIST 3
300 #define CDR_EXECFAILED_PORTCLOSED 4
303 * must be sent after CDR_EXEC{OK|FAILED}
304 * CDR_EXEOK_BINDATA[1] bindatalen[1-4] bindata[bindatalen] */
305 #define CDR_BINDATA 3
308 * routing daemon sock
309 * format:
310 * cmdcode[4] length[4] cmddata[length]
314 #define CRD_KTU_SUPPORTEDVERSIONS 1
316 * CRD_KTU_SUPPORTEDVERSIONS[4] length[4] min[4] max[4]
319 #define CRD_KTU_CONNECT 2
321 * CRD_KTU_KTOU_CONNECT[4] length[4] cookie[8] targetlen[4] target[targetlen]
324 #define CRD_UTK_VERSION 1
326 * CRD_UTK_VERSION[4] length[4] version[4]
330 #define CRD_UTK_UP 2
332 * CRD_UTK_UP[4] length[4] flags[8] addrlen[4] addr[addrlen]
337 #define CRD_UTK_CONNECTERROR 3
339 * CRD_UTK_CONNECTERROR[4] length[4] cookie[8] error[4]
342 #define CRD_UTK_CONNECTERROR_ACCES 1
343 #define CRD_UTK_CONNECTERROR_NETUNREACH 2
344 #define CRD_UTK_CONNECTERROR_TIMEDOUT 3
345 #define CRD_UTK_CONNECTERROR_REFUSED 4
347 #define CONN_MNGD_HEADERLEN 2
348 #define CONN_MNGD_CHECKSUMLEN 4
350 #define CONN_MNGD_HASDATA (1 << 15)
351 #define CONN_MNGD_EOF (1 << 0)
352 #define CONN_MNGD_RCVEND (1 << 1)
353 #define CONN_MNGD_DATALEN 4095
355 #define CONN_MNGD_MAX_SEGMENT_SIZE (CONN_MNGD_DATALEN + 1)
357 #define PRIORITY_MAX 15384774
362 /* result codes for rcv.c/proc_packet */
363 #define RC_DROP 0
364 #define RC_FINISHED 1
366 #define RC_RCV1_ANNOUNCE 2
367 #define RC_RCV1_KERNEL 3
368 #define RC_RCV1_CONN 4
371 struct qos_queue {
372 spinlock_t qlock;
374 struct kref ref;
376 struct list_head queue_list;
378 struct net_device *dev; /* may not change while queue is in list */
380 struct timer_list qos_resume_timer;
381 struct tasklet_struct qos_resume_task;
382 int qos_resume_scheduled;
383 unsigned long jiffies_lastprogress;
385 struct list_head kpackets_waiting;
386 struct list_head conn_retrans_waiting;
387 struct list_head announce_waiting;
388 struct list_head neighbors_waiting;
390 __u32 numconns;
391 __u64 priority_sum;
396 * switch to and from RB_INQUEUE_NBCONGWIN is only done with nbcongwin.lock
397 * *and* qlock held
399 #define RB_INQUEUE_FALSE 0
400 #define RB_INQUEUE_TRUE 1
401 #define RB_INQUEUE_NBCONGWIN 2 /* only for nb->rb */
403 struct resume_block{
404 struct list_head lh;
405 __u8 in_queue;
408 #define ANNOUNCE_TYPE_BROADCAST 1
409 #define ANNOUNCE_TYPE_UNICAST 2
411 struct announce_data{
412 struct kref ref;
414 struct list_head lh;
415 __u8 type;
416 __u16 sndcnt;
417 struct net_device *dev;
418 char mac[MAX_ADDR_LEN];
419 struct delayed_work announce_work;
420 struct resume_block rb;
423 struct ping_cookie{
424 ktime_t time_created;
425 ktime_t time_sent;
426 unsigned long jiffies_sent;
428 __u32 cookie;
429 __u8 pongs; /* count of pongs for pings sent after this one */
432 #define NEIGHBOR_STATE_INITIAL 0
433 #define NEIGHBOR_STATE_ACTIVE 1
434 #define NEIGHBOR_STATE_STALLED 2
435 #define NEIGHBOR_STATE_KILLED 3
437 #define NBCONGWIN_SHIFT 16
438 #define NBCONGWIN_MUL (1 << NBCONGWIN_SHIFT)
440 struct neighbor{
441 struct list_head nb_list;
443 struct kref ref;
445 struct net_device *dev;
446 char mac[MAX_ADDR_LEN];
447 struct qos_queue *queue;
449 __be32 sessionid;
451 atomic_t sessionid_rcv_needed;
452 atomic_t sessionid_snd_needed;
454 char *addr;
455 __u16 addrlen;
457 struct timer_list cmsg_timer;
458 struct tasklet_struct cmsg_task;
459 atomic_t cmsg_task_scheduled;
460 atomic_t cmsg_timer_running;
461 spinlock_t cmsg_lock;
462 spinlock_t send_cmsg_lock;
463 struct list_head cmsg_queue_pong;
464 struct list_head cmsg_queue_ack;
465 struct list_head cmsg_queue_ackconn;
466 struct list_head cmsg_queue_conndata;
467 struct list_head cmsg_queue_other;
469 struct rb_root pending_conn_resets_rb;
471 unsigned long timeout;
473 __u32 cmsg_pongslength;
474 __u32 cmsg_otherlength;
476 __u32 cmsg_pongscnt; /* size of queue only, protected by cmsg_lock */
477 atomic_t cmsg_pongs_retrans_cnt; /* number of retransmits only */
478 atomic_t cmsg_othercnt; /* size of queue + retransmits */
480 atomic_t cmsg_bulk_readds;
483 unsigned long jiffies_last_cmsg;
484 __u32 cmsg_interval; /* microsecs */
486 __u8 max_cmsg_delay_sent;
488 /* procected by qos_queue->qlock */
489 struct resume_block rb_kp;
490 struct resume_block rb_cr;
491 struct resume_block rb;
493 struct{
494 spinlock_t lock;
495 struct list_head lh;
496 struct list_head lh_nextpass;
497 __u32 cnt;
498 __u64 priority_sum;
499 }conns_waiting;
501 struct{
502 spinlock_t lock;
503 atomic64_t data_intransit;
504 atomic64_t cwin;
505 __u64 cwin_shrinkto;
506 }nbcongwin;
508 spinlock_t state_lock;
509 unsigned long last_ping_time;
511 struct ping_cookie cookies[PING_COOKIES_PER_NEIGH];
512 __u32 ping_intransit;
513 __u32 lastcookie;
514 __u32 cookie_unsent;
515 __u64 latency_variance_retrans_us; /* microsecs */
516 atomic_t latency_retrans_us; /* microsecs */
517 atomic_t latency_stddev_retrans_us; /* microsecs */
518 atomic_t latency_advertised_us; /* microsecs */
519 atomic_t max_remote_ack_delay_us; /* microsecs */
520 atomic_t max_remote_ackconn_delay_us; /* microsecs */
521 atomic_t max_remote_other_delay_us; /* microsecs */
523 union {
524 unsigned long initial_state_since;/* initial state */
526 * last_roundtrip:
527 * time of the last sent packet which has been acked or
528 * otherwise responded to (e.g. pong)
530 unsigned long last_roundtrip;/* active/stalled state */
531 }state_time;
532 ktime_t last_roundtrip_end;
533 __u16 ping_success;
534 __u8 state;
536 __u8 str_timer_pending;
537 struct delayed_work stalltimeout_timer;
539 spinlock_t connid_lock;
540 struct rb_root connid_rb;
542 spinlock_t connid_reuse_lock;
543 struct rb_root connid_reuse_rb;
544 struct list_head connid_reuse_list;
545 __u16 connid_reuse_pingcnt;
547 spinlock_t kp_retransmits_lock;
548 struct rb_root kp_retransmits_rb;
550 __u64 kpacket_seqno;
552 atomic64_t priority_sum;
555 * connecions which receive data from/send data to this node
556 * used when terminating all connections of a neighbor and terminating
557 * inactive connections
559 spinlock_t conn_list_lock;
560 struct list_head rcv_conn_list;
562 spinlock_t stalledconn_lock;
563 struct work_struct stalledconn_work;
564 __u8 stalledconn_work_scheduled;
565 struct list_head stalledconn_list;
568 * the timer has to be inited when adding the neighbor
569 * timer_setup(...);
570 * add_timer(struct timer_list * timer);
572 spinlock_t retrans_lock;
573 struct timer_list retrans_timer;
574 struct tasklet_struct retrans_task;
575 __u8 retrans_timer_running;
576 struct list_head retrans_list;
578 spinlock_t retrans_conn_lock;
579 struct timer_list retrans_conn_timer;
580 struct tasklet_struct retrans_conn_task;
581 struct list_head retrans_conn_list;
582 __u8 retrans_conn_timer_running;
583 __u8 retrans_conn_running;
586 #define DATABUF_BUF 0
587 #define DATABUF_SKB 1
589 struct data_buf_item{
590 struct list_head buf_list;
592 char *buf;
593 __u16 datalen;
594 __u16 buflen;
596 __u8 type;
599 struct connid_reuse_item{
600 struct rb_node rbn;
602 struct list_head lh;
604 struct kref ref;
605 __u32 conn_id;
606 __u16 pingcnt;
609 #define SNDSPEED_INIT 0
610 #define SNDSPEED_ACTIVE 1
611 struct snd_speed{
612 __u8 state;
613 __u8 flushed;
614 unsigned long jiffies_last_refresh;
615 __u32 bytes_sent;
617 /* bytes per second */
618 __u32 speed;
619 __u32 speed_limited;
622 struct cor_sock;
625 * There are 2 conn objects per bi-directional connection. They refer to each
626 * other with in the reversedir field.
630 * Naming:
632 * cn: conn we have no clue what is inside
633 * src_in, trgt_unconn, trgt_out, ...: A conn with the specified source or
634 * targettype. In the unlocked case the types are actually just a guess,
635 * because they could have changed since the last access. After locking the
636 * source/destination parameters have to be checked whether they still are
637 * what we expect. This includes source/targettype, neighbor, conn_id
639 * Exception: they may not change after they are set to source/target sock
640 * until the socket is released.
643 * Naming suffixes:
644 * no suffix: unlocked
646 * _l: this direction is locked
648 * _ll: both directions are locked
650 * _lx: this direction is locked, the other direction may be locked
652 * _o: unlocked, but source or target is known for sure, because an outside
653 * lock is taken; For variables on the heap this means that an outside lock must
654 * be taken before accessing the struct which points to the conn can be
655 * accessed.
659 * Locking:
660 * The following fields are immutable after the conn has been allocated:
661 * is_client, reversedir
663 * Most fields are protected by rcv_lock. Fields which which control
664 * source and destination of the data flow require both directions to
665 * to be locked and external references to be cleared before the change can
666 * happen. This includes fields like sourcetype, targettype, connid,
667 * list_heads, htab_entries, ???. In this case the side with is_client == 1
668 * needs to be locked first. Changes to conn_id and neighbor also require
669 * removing the conn from the htables first.
671 * Some other fields are locked outside (e.g. at struct neighbor).
673 #define SOURCE_UNCONNECTED 0
674 #define SOURCE_IN 1
675 #define SOURCE_SOCK 2
677 #define TARGET_UNCONNECTED 0
678 #define TARGET_OUT 1
679 #define TARGET_SOCK 2
680 #define TARGET_DISCARD 3
682 #define BUFSIZE_NOACTION 0
683 #define BUFSIZE_DECR 1
684 #define BUFSIZE_DECR_FAST 2
685 #define BUFSIZE_INCR 3
686 #define BUFSIZE_INCR_FAST 4
688 #define BUFSIZE_SHIFT 5
690 #define SOCKTYPE_RAW 0
691 #define SOCKTYPE_MANAGED 1
693 #define RCV_BUF_STATE_OK 0
694 #define RCV_BUF_STATE_INCOMPLETE 1
695 #define RCV_BUF_STATE_RESET 2
697 struct conn{
698 __u8 sourcetype:4,
699 targettype:4;
701 __u8 is_client:1,
702 is_highlatency:1;
705 * isreset values:
706 * 0... connection active
707 * 1... connection is about to be reset, target does not need to be
708 * notified
709 * 2... connection is reset
710 * 3... connection is reset + no pointers to "struct conn *reversedir"
711 * remaining except from this conn
713 __u8 isreset;
715 __u8 flush;
717 struct kref ref;
719 spinlock_t rcv_lock;
721 union{
722 struct{
723 struct neighbor *nb;
724 /* list of all connections from this neighbor */
725 struct list_head nb_list;
727 struct list_head reorder_queue;
728 __u32 reorder_memused;
730 struct rb_node rbn;
731 struct connid_reuse_item *cir;
732 __u32 conn_id;
733 __u64 next_seqno;
735 /* number of ack sent, not data seqno */
736 __u32 ack_seqno;
738 __u16 small_ooo_packets;
740 __u32 priority;
741 __u8 priority_seqno;
742 __u8 inorder_ack_needed;
744 __u8 established;
746 __u64 window_seqnolimit;
747 __u64 window_seqnolimit_remote;
749 /* protected by nb->cmsg_lock */
750 struct list_head acks_pending;
752 unsigned long jiffies_last_act;
753 }in;
755 struct{
756 struct cor_sock *cs;
758 struct list_head cl_list;
760 __u32 priority;
762 struct snd_speed snd_speed;
763 }sock;
764 }source;
766 union{
767 struct{
768 __u32 paramlen;
769 __u32 cmdread;
770 __u16 cmd;
771 __u8 paramlen_read;
772 char *cmdparams;
773 char paramlen_buf[4];
774 }unconnected;
776 struct{
777 struct neighbor *nb;
778 /* protected by nb->retrans_conn_lock, sorted by seqno
780 struct list_head retrans_list;
782 /* protected by nb->stalledconn_lock */
783 struct list_head nbstalled_lh;
785 __u32 conn_id;
786 __u64 seqno_nextsend;
787 __u64 seqno_acked;
788 __u64 seqno_windowlimit;
790 struct resume_block rb;
792 __u32 rb_priority;
793 __u16 maxsend_extra;
795 __u16 burst_bytes;
797 __u8 priority_last;
798 __u8 priority_seqno;
799 __u8 priority_send_allowed;
801 __u8 windowlimit_reached;
803 __u8 established;
805 /* protected by nb->retrans_conn_lock */
806 __u16 retrans_lowwindow;
807 }out;
809 struct{
810 __u8 waiting_for_userspace;
811 unsigned long waiting_for_userspace_since;
813 struct cor_sock *cs;
815 __u8 socktype;
817 __u8 rcv_buf_state;
818 char rcv_hdr[CONN_MNGD_HEADERLEN];
819 char rcv_chksum[CONN_MNGD_CHECKSUMLEN];
820 char *rcv_buf;
821 __u16 rcvd;
822 __u16 rcv_hdr_flags;
823 __u16 rcv_data_len;
824 }sock;
825 }target;
827 struct{
828 struct list_head items;
829 struct data_buf_item *nextread;
830 __u64 first_offset;
832 __u32 datasize;
833 __u32 overhead;
834 __u32 read_remaining;
836 __u16 next_read_offset;
837 }data_buf;
839 __u32 bufspace_accounted;
841 struct{
842 __u32 bufsize; /* 32 ==> 1 byte, see BUFSIZE_SHIFT */
843 __u32 ignore_rcv_lowbuf;
844 union{
845 struct{
846 __u32 bytesleft;
847 }noact;
849 struct{
850 __u32 size_start;
851 }decr;
853 struct{
854 __u32 size_start;
855 __u32 size_end;
856 }incr;
857 }act;
859 __u8 state;
860 }bufsize;
862 struct conn *reversedir;
865 #define CONN_RETRANS_INITIAL 0
866 #define CONN_RETRANS_SCHEDULED 1
867 #define CONN_RETRANS_LOWWINDOW 2
868 #define CONN_RETRANS_SENDING 3
869 #define CONN_RETRANS_ACKED 4
870 struct conn_retrans {
871 /* timeout_list and conn_list share a single ref */
872 struct kref ref;
873 /* only in timeout_list if state == CONN_RETRANS_SCHEDULED */
874 struct list_head timeout_list;
875 struct list_head conn_list;
876 struct conn *trgt_out_o;
877 __u64 seqno;
878 __u32 length;
880 __u8 snd_delayed_lowbuf;
881 __u8 state;
882 unsigned long timeout;
885 #define RCVOOO_BUF 0
886 #define RCVOOO_SKB 1
887 struct rcvooo{
888 struct list_head lh;
889 __u64 seqno;
890 __u8 type;
891 __u8 flush;
894 struct rcvooo_buf{
895 struct rcvooo r;
896 char *data;
897 __u32 len;
900 /* inside skb->cb */
901 struct skb_procstate{
902 union{
903 struct{
904 struct work_struct work;
905 }announce1;
907 struct{
908 __u32 offset;
909 }announce2;
911 struct{
912 __u32 skb_memused;
913 struct rcvooo r;
914 }rcv_ooo;
916 struct{
917 struct data_buf_item dbi;
918 }rcv;
919 }funcstate;
922 #define CS_TYPE_UNCONNECTED 0
923 #define CS_TYPE_LISTENER 1
924 #define CS_TYPE_CONN_RAW 2
925 #define CS_TYPE_CONN_MANAGED 3
927 #define CS_CONNECTSTATE_UNCONNECTED 0
928 #define CS_CONNECTSTATE_CONNECTING 1
929 #define CS_CONNECTSTATE_CONNECTED 2
930 #define CS_CONNECTSTATE_ERROR 3
932 #define CS_SHUTDOWN_SHUTDOWN_RD (1 << 0)
933 #define CS_SHUTDOWN_SHUTDOWN_WR (1 << 1)
934 #define CS_SHUTDOWN_SENT_EOF (1 << 2)
935 #define CS_SHUTDOWN_SENT_RCVEND (1 << 3)
936 #define CS_SHUTDOWN_RCVD_EOF (1 << 4)
937 #define CS_SHUTDOWN_RCVD_RCVEND (1 << 5)
939 struct cor_sock {
940 struct sock sk; /* must be first */
942 struct mutex lock;
943 struct kref ref;
945 /* type may not change once it is set to != CS_TYPE_UNCONNECTED */
946 __u8 type;
947 __u8 isreleased;
949 __u8 publish_service;
951 union {
952 struct {
953 /* listener is protected by cor_bindnodes */
954 struct list_head lh;
955 __be16 port;
956 __u8 publish_service;
957 __u32 queue_maxlen;
958 __u32 queue_len;
959 struct list_head conn_queue;
960 }listener;
962 struct {
963 struct conn *src_sock;
964 struct conn *trgt_sock;
966 struct data_buf_item *rcvitem;
967 __u16 rcvoffset;
969 __u8 snd_delayed_lowbuf;
971 struct cor_sock *pass_on_close;
972 }conn_raw;
974 struct {
975 struct cor_sockaddr remoteaddr;
977 struct list_head rd_msgs;
978 struct list_head crd_lh;
979 __u8 in_crd_list;
981 __u8 connect_state;
983 __u8 is_reset;
985 __u8 flush;
987 __u8 shutdownflags;
989 __u8 snd_delayed_lowbuf;
991 __be64 cookie;
992 struct rb_node rbn;
994 struct conn *src_sock;
995 struct conn *trgt_sock;
997 char snd_hdr[CONN_MNGD_HEADERLEN];
998 char snd_chksum[CONN_MNGD_CHECKSUMLEN];
999 char *snd_buf;
1000 __u16 snd_segment_size;
1001 __u16 snd_hdr_flags;
1002 __u16 snd_data_len;
1003 __u16 sent;
1004 /* protected by cor_sock->lock */
1005 __u8 in_flushtoconn_oom_list;
1006 /* protected by flushtoconn_oom_lock */
1007 struct list_head flushtoconn_oom_lh;
1009 /* receiving */
1010 char rcv_hdr[CONN_MNGD_HEADERLEN];
1011 char rcv_chksum[CONN_MNGD_CHECKSUMLEN];
1012 char *rcv_buf;
1013 __u16 rcv_hdr_flags;
1014 __u16 rcv_data_len;
1015 __u16 rcvbuf_consumed;
1016 __u8 rcv_buf_state;
1017 }conn_managed;
1018 }data;
1020 struct work_struct readfromconn_work;
1021 atomic_t readfromconn_work_scheduled;
1023 atomic_t ready_to_read;
1024 atomic_t ready_to_write;
1025 atomic_t ready_to_accept;
1030 /* common.c */
1031 extern atomic_t num_conns;
1033 extern spinlock_t cor_bindnodes;
1035 extern struct conn *get_conn(struct neighbor *nb, __u32 conn_id);
1037 extern void delete_connid_reuse_items(struct neighbor *nb);
1039 extern void connid_used_pingsuccess(struct neighbor *nb);
1041 extern void _set_last_act(struct conn *src_in_l);
1043 extern void free_conn(struct kref *ref);
1045 extern int conn_init_out(struct conn *trgt_unconn_ll, struct neighbor *nb,
1046 __u32 rcvd_connid, int use_rcvd_connid);
1048 extern void conn_init_sock_source(struct conn *cn);
1050 extern void conn_init_sock_target(struct conn *cn);
1052 extern __u32 list_services(char *buf, __u32 buflen);
1054 extern void set_publish_service(struct cor_sock *cs, __u8 value);
1056 extern void close_port(struct cor_sock *cs);
1058 extern int open_port(struct cor_sock *cs_l, __be16 port);
1060 extern int connect_port(struct conn *trgt_unconn_ll, __be16 port);
1062 extern int connect_neigh(struct conn *trgt_unconn_ll, char *addr,
1063 __u16 addrlen);
1065 extern struct conn* alloc_conn(gfp_t allocflags);
1067 extern void reset_conn_locked(struct conn *cn_ll);
1069 extern void reset_conn(struct conn *cn);
1071 /* util.c */
1072 extern __u8 __attribute__((const)) enc_log_256_16(__u32 value);
1074 extern __u32 __attribute__((const)) dec_log_256_16(__u8 value);
1076 extern __u8 __attribute__((const)) enc_log_64_11(__u32 value);
1078 extern __u32 __attribute__((const)) dec_log_64_11(__u8 value);
1080 extern __u8 __attribute__((const)) enc_log_64_7(__u64 value);
1082 extern __u64 __attribute__((const)) dec_log_64_7(__u8 value);
1084 extern void kreffree_bug(struct kref *ref);
1086 extern int __init cor_util_init(void);
1088 /* credits.c */
1089 extern int newconn_checkpriority(struct neighbor *nb, __u8 priority);
1091 extern __u32 refresh_conn_priority(struct conn *cn, int locked);
1093 extern void set_conn_in_priority(struct neighbor *nb, __u32 conn_id,
1094 struct conn *src_in, __u8 priority_seqno, __u8 priority);
1096 extern void connreset_priority(struct conn *cn);
1098 extern int __init credits_init(void);
1100 /* neighbor.c */
1101 extern void neighbor_free(struct kref *ref);
1103 extern int is_from_nb(struct sk_buff *skb, struct neighbor *nb);
1105 extern struct neighbor *get_neigh_by_mac(struct sk_buff *skb);
1107 extern struct neighbor *find_neigh(char *addr, __u16 addrlen);
1109 extern __u32 generate_neigh_list(char *buf, __u32 buflen);
1111 extern int get_neigh_state(struct neighbor *nb);
1113 extern void ping_resp(struct neighbor *nb, __u32 cookie, __u32 respdelay);
1115 extern __u32 add_ping_req(struct neighbor *nb, unsigned long *last_ping_time,
1116 ktime_t now);
1118 extern void ping_sent(struct neighbor *nb, __u32 cookie);
1120 extern void unadd_ping_req(struct neighbor *nb, __u32 cookie,
1121 unsigned long last_ping_time, int congested);
1123 #define TIMETOSENDPING_NO 0
1124 #define TIMETOSENDPING_YES 1
1125 #define TIMETOSENDPING_FORCE 2
1126 extern int time_to_send_ping(struct neighbor *nb);
1128 extern unsigned long get_next_ping_time(struct neighbor *nb);
1130 extern int force_ping(struct neighbor *nb);
1132 extern int rcv_announce(struct sk_buff *skb);
1134 extern int _send_announce(struct announce_data *ann, int fromqos, int *sent);
1136 extern void announce_data_free(struct kref *ref);
1138 extern void announce_send_stop(struct net_device *dev, char *mac, int type);
1140 extern void cor_neighbor_down(void);
1142 extern int cor_neighbor_up(char *addr2, __u32 addrlen2);
1144 extern int is_clientmode(void);
1146 extern int __init cor_neighbor_init(void);
1148 /* rcv.c */
1149 extern void reset_ooo_queue(struct conn *src_in_lx);
1151 extern void drain_ooo_queue(struct conn *src_in_l);
1153 extern void conn_rcv(struct neighbor *nb, struct sk_buff *skb, char *data, __u32 len,
1154 __u32 conn_id, __u64 seqno, int rcv_delayed_lowbuf, __u8 flush);
1156 extern void cor_rcv_down(void);
1158 extern void cor_rcv_up(void);
1160 extern int __init cor_rcv_init(void);
1162 /* kpacket_parse.c */
1163 extern void kernel_packet(struct neighbor *nb, struct sk_buff *skb,
1164 __u64 seqno);
1166 /* kpacket_gen.c */
1167 struct control_msg_out;
1169 #define ACM_PRIORITY_LOW 1 /* oom recovery easy */
1170 #define ACM_PRIORITY_MED 2 /* oom may cause timeouts */
1171 #define ACM_PRIORITY_HIGH 3 /* cm acks - needed for freeing old cms */
1173 extern struct control_msg_out *alloc_control_msg(struct neighbor *nb,
1174 int priority);
1176 extern void free_control_msg(struct control_msg_out *cm);
1178 extern void retransmit_taskfunc(unsigned long arg);
1180 extern void retransmit_timerfunc(struct timer_list *retrans_timer);
1182 extern void kern_ack_rcvd(struct neighbor *nb, __u64 seqno);
1184 extern int send_messages(struct neighbor *nb, int resume, int *sent);
1186 extern void controlmsg_taskfunc(unsigned long nb);
1188 extern void controlmsg_timerfunc(struct timer_list *cmsg_timer);
1190 extern void schedule_controlmsg_timer(struct neighbor *nb_cmsglocked);
1192 extern void send_pong(struct neighbor *nb, __u32 cookie);
1194 extern int send_reset_conn(struct neighbor *nb, __u32 conn_id, int lowprio);
1196 extern void send_ack(struct neighbor *nb, __u64 seqno);
1198 extern void send_ack_conn_ifneeded(struct conn *src_in_l, __u64 seqno_ooo,
1199 __u32 ooo_length);
1201 extern void send_priority(struct conn *trgt_out_ll, int force,
1202 __u8 priority);
1204 extern void free_ack_conns(struct conn *src_in_lx);
1206 extern void send_connect_success(struct control_msg_out *cm, __u32 conn_id,
1207 struct conn *src_in);
1209 extern void send_connect_nb(struct control_msg_out *cm, __u32 conn_id,
1210 __u64 seqno1, __u64 seqno2, struct conn *src_in_ll);
1212 extern void send_conndata(struct control_msg_out *cm, __u32 conn_id,
1213 __u64 seqno, char *data_orig, char *data, __u32 datalen,
1214 __u8 snd_delayed_lowbuf, __u8 flush,
1215 struct conn_retrans *cr);
1217 extern int __init cor_kgen_init(void);
1219 /* cpacket_parse.c */
1220 extern int encode_len(char *buf, int buflen, __u32 len);
1222 extern void proc_cpacket(struct conn *trgt_unconn);
1224 extern int __init cor_cpacket_init(void);
1226 /* snd.c */
1227 #ifdef DEBUG_QOS_SLOWSEND
1228 extern int cor_dev_queue_xmit(struct sk_buff *skb, int caller);
1229 #else
1230 static inline int cor_dev_queue_xmit(struct sk_buff *skb, int caller)
1232 return dev_queue_xmit(skb);
1234 #endif
1236 extern void free_qos(struct kref *ref);
1238 #ifdef COR_NBCONGWIN
1239 extern void nbcongwin_data_acked(struct neighbor *nb, __u64 bytes_acked);
1240 #endif
1242 extern struct qos_queue *get_queue(struct net_device *dev);
1244 extern int destroy_queue(struct net_device *dev);
1246 extern int create_queue(struct net_device *dev);
1248 #define QOS_RESUME_DONE 0
1249 #define QOS_RESUME_CONG 1
1250 #define QOS_RESUME_NEXTNEIGHBOR 2 /* resume_neighbors() internal */
1252 #define QOS_CALLER_KPACKET 0
1253 #define QOS_CALLER_CONN_RETRANS 1
1254 #define QOS_CALLER_ANNOUNCE 2
1255 #define QOS_CALLER_NEIGHBOR 3
1257 extern void qos_enqueue(struct qos_queue *q, struct resume_block *rb,
1258 int caller);
1260 extern void qos_remove_conn(struct conn *trgt_out_l);
1262 extern int may_send_announce(struct net_device *dev);
1264 extern struct sk_buff *create_packet_cmsg(struct neighbor *nb, int size,
1265 gfp_t alloc_flags, __u64 seqno);
1267 extern void reschedule_conn_retrans_timer(struct neighbor *nb_retranslocked);
1269 extern void cancel_all_conn_retrans(struct conn *trgt_out_l);
1271 extern void retransmit_conn_taskfunc(unsigned long nb);
1273 extern void retransmit_conn_timerfunc(struct timer_list *retrans_timer_conn);
1275 extern void conn_ack_ooo_rcvd(struct neighbor *nb, __u32 conn_id,
1276 struct conn *trgt_out, __u64 seqno_ooo, __u32 length,
1277 __u64 *bytes_acked);
1279 extern void conn_ack_rcvd(struct neighbor *nb, __u32 conn_id,
1280 struct conn *trgt_out, __u64 seqno, int setwindow, __u8 window,
1281 __u64 *bytes_acked);
1283 extern void schedule_retransmit_conn(struct conn_retrans *cr, int connlocked,
1284 int nbretrans_locked);
1286 /* RC_FLUSH_CONN_OUT_SENT | RC_FLUSH_CONN_OUT_{^SENT} */
1287 #define RC_FLUSH_CONN_OUT_OK 1
1288 #define RC_FLUSH_CONN_OUT_SENT_CONG 2 /* flush_out internal only */
1289 #define RC_FLUSH_CONN_OUT_NBNOTACTIVE 3
1290 #define RC_FLUSH_CONN_OUT_CONG 4
1291 #define RC_FLUSH_CONN_OUT_MAXSENT 5
1292 #define RC_FLUSH_CONN_OUT_OOM 6
1294 extern int flush_out(struct conn *trgt_out_lx, __u32 *sent);
1296 extern void resume_nbstalled_conns(struct work_struct *work);
1298 extern int __init cor_snd_init(void);
1300 /* forward.c */
1301 extern struct kmem_cache *data_buf_item_slab;
1303 extern void databuf_init(struct conn *cn_init);
1305 extern void bufsize_init(struct conn *cn_l, __u32 bufsize);
1307 extern int account_bufspace(struct conn *cn_lx);
1309 extern int cpacket_write_allowed(struct conn *src_unconn_lx);
1311 extern void update_windowlimit(struct conn *src_in_lx);
1313 extern void bufsize_read_to_sock(struct conn *trgt_sock_lx);
1315 extern void databuf_ackdiscard(struct conn *cn_lx);
1317 extern void reset_seqno(struct conn *cn_l, __u64 initseqno);
1319 extern void databuf_pull(struct conn *cn_lx, char *dst, __u32 len);
1321 static inline __u32 databuf_trypull(struct conn *cn_l, char *dst, __u32 len)
1323 if (len > cn_l->data_buf.read_remaining)
1324 len = cn_l->data_buf.read_remaining;
1325 databuf_pull(cn_l, dst, len);
1326 return len;
1329 extern void databuf_unpull_dpi(struct conn *trgt_sock, struct cor_sock *cs,
1330 struct data_buf_item *item, __u16 next_read_offset);
1332 extern void databuf_pull_dbi(struct cor_sock *cs_rl, struct conn *trgt_sock_l);
1334 extern void databuf_unpull(struct conn *trgt_out_l, __u32 bytes);
1336 extern void databuf_pullold(struct conn *trgt_out_l, __u64 startpos, char *dst,
1337 int len);
1339 extern void databuf_ack(struct conn *trgt_out_l, __u64 pos);
1341 extern void databuf_ackread(struct conn *cn_lx);
1343 extern __u32 receive_buf(struct conn *cn_lx, char *buf, __u32 datalen,
1344 int rcv_delayed_lowbuf, __u8 flush);
1346 extern __u32 receive_skb(struct conn *src_in_l, struct sk_buff *skb,
1347 int rcv_delayed_lowbuf, __u8 flush);
1349 extern void wake_sender(struct conn *cn);
1351 extern int __init forward_init(void);
1353 /* sock_rdaemon.c */
1354 extern int cor_create_rdaemon_sock(struct net *net, struct socket *sock,
1355 int protocol, int kern);
1357 extern int rdreq_connect(struct cor_sock *cs);
1359 extern void cor_usersock_release(struct cor_sock *cs);
1361 extern int __init cor_rd_init1(void);
1363 extern int __init cor_rd_init2(void);
1365 /* sock_raw.c */
1366 extern int cor_create_raw_sock(struct net *net, struct socket *sock,
1367 int protocol, int kern);
1369 /* sock_managed.c */
1370 extern struct cor_sock *get_corsock_by_cookie(__be64 cookie);
1372 extern void __set_sock_connecterror(struct cor_sock *cs_m_l, int errorno);
1374 extern void _set_sock_connecterror(struct cor_sock *cs, int errorno);
1376 static inline void set_sock_connecterror(__be64 cookie, int errorno)
1378 struct cor_sock *cs = get_corsock_by_cookie(cookie);
1379 _set_sock_connecterror(cs, errorno);
1382 extern void flush_sock_managed(struct conn *trgt_sock_lx, int from_recvmsg,
1383 __u8 *do_wake_sender);
1385 extern void cor_mngdsocket_readfromconn_fromatomic(struct cor_sock *cs);
1387 extern void cor_mngdsocket_readfromconn_wq(struct work_struct *work);
1389 extern int cor_create_managed_sock(struct net *net, struct socket *sock,
1390 int protocol, int kern);
1392 extern int __init cor_sock_managed_init1(void);
1394 /* sock.c */
1395 extern void free_sock(struct kref *ref);
1397 extern void flush_sock(struct conn *trgt_sock_lx);
1399 extern void update_src_sock_sndspeed(struct conn *src_sock_l, __u32 bytes_sent);
1401 extern int cor_sock_sndbufavailable(struct conn *src_sock_lx);
1403 extern int cor_socket_socketpair(struct socket *sock1, struct socket *sock2);
1405 extern int cor_socket_getname(struct socket *sock, struct sockaddr *addr,
1406 int peer);
1408 extern int cor_socket_mmap(struct file *file, struct socket *sock,
1409 struct vm_area_struct *vma);
1411 extern int _cor_createsock(struct net *net, struct socket *sock, int protocol,
1412 int kern);
1414 extern int __init cor_sock_init1(void);
1416 extern int __init cor_sock_init2(void);
1419 static inline struct skb_procstate *skb_pstate(struct sk_buff *skb)
1421 return (struct skb_procstate *) &(skb->cb[0]);
1424 static inline struct sk_buff *skb_from_pstate(struct skb_procstate *ps)
1426 return (struct sk_buff *) (((char *)ps) - offsetof(struct sk_buff,cb));
1429 static inline __u32 mss(struct neighbor *nb, __u32 l3overhead)
1431 __u32 mtu = (nb->dev->mtu > 4096) ? 4096 : nb->dev->mtu;
1432 return mtu - LL_RESERVED_SPACE(nb->dev) - l3overhead;
1435 static inline __u32 mss_cmsg(struct neighbor *nb)
1437 return mss(nb, 7);
1440 static inline __u32 mss_conndata(struct neighbor *nb)
1442 __u32 mss_tmp = mss(nb, 11);
1443 __u32 i;
1445 if (mss_tmp < 256)
1446 return mss_tmp;
1448 for (i=256;i<4096;i*=2) {
1449 if (i*2 > mss_tmp)
1450 return i;
1453 return mss_tmp - mss_tmp%4096;
1456 static inline __u32 send_conndata_as_skb(struct neighbor *nb, __u32 size)
1458 return size >= mss_conndata(nb)/2;
1461 static inline long calc_timeout(__u32 latency_us, __u32 latency_stddev_us,
1462 __u32 max_remote_ack_delay_us)
1464 unsigned long addto;
1465 if (unlikely(unlikely(latency_us > 1000000000) ||
1466 unlikely(latency_stddev_us > 500000000) ||
1467 unlikely(max_remote_ack_delay_us > 1000000000))) {
1468 addto = msecs_to_jiffies(latency_us/1000 + latency_us/4000 +
1469 latency_stddev_us/333 +
1470 max_remote_ack_delay_us/1000);
1471 } else {
1472 addto = usecs_to_jiffies(latency_us + latency_us/4 +
1473 latency_stddev_us*3 + max_remote_ack_delay_us);
1477 * 2 is added because
1478 * 1) _to_jiffies rounds down, but should round up, so add 1 to
1479 * compensate
1480 * 2) even if latency is 0, we never want to schedule the retransmit
1481 * to run right now, so add 1 more
1483 return jiffies + 2 + addto;
1486 static inline void put_be64(char *dst, __be64 value)
1488 char *p_value = (char *) &value;
1490 dst[0] = p_value[0];
1491 dst[1] = p_value[1];
1492 dst[2] = p_value[2];
1493 dst[3] = p_value[3];
1494 dst[4] = p_value[4];
1495 dst[5] = p_value[5];
1496 dst[6] = p_value[6];
1497 dst[7] = p_value[7];
1500 static inline void put_u64(char *dst, __u64 value)
1502 put_be64(dst, cpu_to_be64(value));
1505 static inline void put_u48(char *dst, __u64 value)
1507 char *p_value = (char *) &value;
1509 value = cpu_to_be64(value);
1511 dst[0] = p_value[2];
1512 dst[1] = p_value[3];
1513 dst[2] = p_value[4];
1514 dst[3] = p_value[5];
1515 dst[4] = p_value[6];
1516 dst[5] = p_value[7];
1519 static inline void put_be32(char *dst, __be32 value)
1521 char *p_value = (char *) &value;
1522 dst[0] = p_value[0];
1523 dst[1] = p_value[1];
1524 dst[2] = p_value[2];
1525 dst[3] = p_value[3];
1528 static inline void put_u32(char *dst, __u32 value)
1530 put_be32(dst, cpu_to_be32(value));
1533 static inline void put_be16(char *dst, __be16 value)
1535 char *p_value = (char *) &value;
1536 dst[0] = p_value[0];
1537 dst[1] = p_value[1];
1540 static inline void put_u16(char *dst, __u16 value)
1542 put_be16(dst, cpu_to_be16(value));
1545 static inline char *cor_pull_skb(struct sk_buff *skb, unsigned int len)
1547 char *ptr = skb_pull(skb, len);
1549 if (unlikely(ptr == 0))
1550 return 0;
1552 return ptr - len;
1555 static inline __be64 parse_be64(char *buf)
1557 __u64 ret = 0;
1559 BUG_ON(buf == 0);
1561 ((char *)&ret)[0] = buf[0];
1562 ((char *)&ret)[1] = buf[1];
1563 ((char *)&ret)[2] = buf[2];
1564 ((char *)&ret)[3] = buf[3];
1565 ((char *)&ret)[4] = buf[4];
1566 ((char *)&ret)[5] = buf[5];
1567 ((char *)&ret)[6] = buf[6];
1568 ((char *)&ret)[7] = buf[7];
1570 return ret;
1573 static inline __u64 parse_u64(char *buf)
1575 return be64_to_cpu(parse_be64(buf));
1578 static inline __u64 parse_u48(char *ptr)
1580 __u64 ret = 0;
1582 ((char *)&ret)[0] = 0;
1583 ((char *)&ret)[1] = 0;
1584 ((char *)&ret)[2] = ptr[0];
1585 ((char *)&ret)[3] = ptr[1];
1586 ((char *)&ret)[4] = ptr[2];
1587 ((char *)&ret)[5] = ptr[3];
1588 ((char *)&ret)[6] = ptr[4];
1589 ((char *)&ret)[7] = ptr[5];
1591 return be64_to_cpu(ret);
1594 static inline __be32 parse_be32(char *ptr)
1596 __u32 ret = 0;
1598 BUG_ON(ptr == 0);
1600 ((char *)&ret)[0] = ptr[0];
1601 ((char *)&ret)[1] = ptr[1];
1602 ((char *)&ret)[2] = ptr[2];
1603 ((char *)&ret)[3] = ptr[3];
1605 return ret;
1608 static inline __u32 parse_u32(char *ptr)
1610 return be32_to_cpu(parse_be32(ptr));
1613 static inline __be16 parse_be16(char *ptr)
1615 __u16 ret = 0;
1617 BUG_ON(ptr == 0);
1619 ((char *)&ret)[0] = ptr[0];
1620 ((char *)&ret)[1] = ptr[1];
1622 return ret;
1625 static inline __u16 parse_u16(char *ptr)
1627 return be16_to_cpu(parse_be16(ptr));
1630 static inline __u64 pull_u48(struct sk_buff *skb)
1632 return parse_u48(cor_pull_skb(skb, 6));
1635 static inline __be32 pull_be32(struct sk_buff *skb)
1637 return parse_be32(cor_pull_skb(skb, 4));
1640 static inline __u32 pull_u32(struct sk_buff *skb)
1642 return parse_u32(cor_pull_skb(skb, 4));
1645 static inline __u16 pull_u16(struct sk_buff *skb)
1647 return parse_u16(cor_pull_skb(skb, 2));
1650 static inline __u8 pull_u8(struct sk_buff *skb)
1652 char *ptr = cor_pull_skb(skb, 1);
1653 BUG_ON(ptr == 0);
1654 return *ptr;
1657 static inline int is_conn_in(struct conn *cn_l, struct neighbor *nb,
1658 __u32 conn_id)
1660 if (unlikely(unlikely(cn_l->sourcetype != SOURCE_IN) ||
1661 unlikely(cn_l->source.in.nb != nb) ||
1662 unlikely(cn_l->source.in.conn_id != conn_id) ||
1663 unlikely(cn_l->isreset != 0)))
1664 return 0;
1665 return 1;
1668 static inline int is_src_sock(struct conn *cn_l, struct cor_sock *cs)
1670 if (unlikely(unlikely(cn_l->sourcetype != SOURCE_SOCK) ||
1671 unlikely(cn_l->source.sock.cs != cs)))
1672 return 0;
1673 return 1;
1676 static inline int is_trgt_sock(struct conn *cn_l, struct cor_sock *cs)
1678 if (unlikely(unlikely(cn_l->targettype != TARGET_SOCK) ||
1679 unlikely(cn_l->target.sock.cs != cs)))
1680 return 0;
1681 return 1;
1684 static inline void set_last_act(struct conn *src_in_l)
1686 unsigned long jiffies_tmp = jiffies;
1688 BUG_ON(src_in_l->sourcetype != SOURCE_IN);
1690 if (unlikely(time_after(jiffies_tmp,
1691 src_in_l->source.in.jiffies_last_act +
1692 HZ * CONN_ACTIVITY_UPDATEINTERVAL_SEC)))
1693 _set_last_act(src_in_l);
1696 #define BUFLEN_MIN 128
1697 #define BUFLEN_MAX 4096
1698 #define PAGESIZE (1 << PAGE_SHIFT)
1700 static inline __u32 buf_optlen(__u32 datalen)
1702 __u32 optlen = BUFLEN_MIN;
1703 while (optlen < datalen && optlen < PAGESIZE && optlen < BUFLEN_MAX)
1704 optlen = (optlen << 1);
1705 return optlen;
1709 inline static void databuf_item_free(struct data_buf_item *item)
1711 if (item->type == DATABUF_BUF) {
1712 kfree(item->buf);
1713 kmem_cache_free(data_buf_item_slab, item);
1714 } else if (item->type == DATABUF_SKB) {
1715 struct sk_buff *skb = skb_from_pstate(container_of(item,
1716 struct skb_procstate, funcstate.rcv.dbi));
1717 kfree_skb(skb);
1718 } else {
1719 BUG();
1723 static inline __u64 seqno_clean(__u64 seqno)
1725 return seqno & ((1LL << 48) - 1);
1728 static inline int seqno_eq(__u64 seqno1, __u64 seqno2)
1730 seqno1 = seqno1 << 16;
1731 seqno2 = seqno2 << 16;
1732 return seqno1 == seqno2;
1735 static inline int seqno_before(__u64 seqno1, __u64 seqno2)
1737 seqno1 = seqno1 << 16;
1738 seqno2 = seqno2 << 16;
1739 return (seqno1 - seqno2) >= (1LL << 63);
1742 static inline int seqno_before_eq(__u64 seqno1, __u64 seqno2)
1744 return seqno_eq(seqno1, seqno2) || seqno_before(seqno1, seqno2);
1747 static inline int seqno_after(__u64 seqno1, __u64 seqno2)
1749 return seqno_before_eq(seqno1, seqno2) ? 0 : 1;
1752 static inline int seqno_after_eq(__u64 seqno1, __u64 seqno2)
1754 return seqno_before(seqno1, seqno2) ? 0 : 1;
1757 static inline int ktime_before_eq(ktime_t time1, ktime_t time2)
1759 return ktime_after(time1, time2) ? 0 : 1;
1762 static inline int ktime_after_eq(ktime_t time1, ktime_t time2)
1764 return ktime_before(time1, time2) ? 0 : 1;
1767 static inline __u64 update_atomic_sum(atomic64_t *atomic_sum, __u32 oldvalue,
1768 __u32 newvalue)
1770 __u64 sum_old = atomic64_read(atomic_sum);
1771 __u64 sum;
1773 while (1) {
1774 __u64 cmpxchg_ret;
1776 sum = sum_old;
1778 BUG_ON(sum < oldvalue);
1779 sum -= oldvalue;
1781 BUG_ON(sum + newvalue < sum);
1782 sum += newvalue;
1784 cmpxchg_ret = atomic64_cmpxchg(atomic_sum, sum_old, sum);
1786 if (likely(cmpxchg_ret == sum_old))
1787 break;
1789 sum_old = cmpxchg_ret;
1792 return sum;
1795 static inline void cor_sk_write_space(struct cor_sock *cs)
1797 atomic_set(&(cs->ready_to_write), 1);
1798 barrier();
1799 cs->sk.sk_write_space(&(cs->sk));
1802 static inline void cor_sk_data_ready(struct cor_sock *cs)
1804 atomic_set(&(cs->ready_to_read), 1);
1805 barrier();
1806 cs->sk.sk_data_ready(&(cs->sk));
1809 /* the other direction may be locked only if called from proc_cpacket */
1810 static inline void flush_buf(struct conn *cn_lx)
1812 if (unlikely(cn_lx->targettype == TARGET_UNCONNECTED)) {
1813 proc_cpacket(cn_lx);
1814 } else if (cn_lx->targettype == TARGET_SOCK) {
1815 flush_sock(cn_lx);
1816 } else if (cn_lx->targettype == TARGET_OUT) {
1817 __u32 bytessent = 0;
1818 flush_out(cn_lx, &bytessent);
1819 } else if (unlikely(cn_lx->targettype == TARGET_DISCARD)) {
1820 databuf_ackdiscard(cn_lx);
1821 } else {
1822 BUG();