2 * Connection oriented routing
3 * Copyright (C) 2007-2010 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
21 #include <asm/byteorder.h>
25 /* not sent over the network - internal meaning only */
26 #define MSGTYPE_PONG 1
28 #define MSGTYPE_ACK_CONN 3
29 #define MSGTYPE_CONNECT 4
30 #define MSGTYPE_CONNECT_SUCCESS 5
31 #define MSGTYPE_RESET_CONN 6
32 #define MSGTYPE_CONNDATA 7
33 #define MSGTYPE_PING_CONN 8
34 #define MSGTYPE_CONNID_UNKNOWN 9
35 #define MSGTYPE_PING_ALL_CONNS 10
36 #define MSGTYPE_SET_MAX_CMSG_DELAY 11
38 struct control_msg_out
{
39 struct list_head lh
; /* either neighbor or control_retrans_packet */
48 unsigned long time_enqueued
; /* jiffies */
105 struct control_retrans
{
111 unsigned long timeout
;
113 struct list_head msgs
;
115 struct htab_entry htab_entry
;
116 struct list_head timeout_list
;
119 struct kmem_cache
*controlmsg_slab
;
120 struct kmem_cache
*controlretrans_slab
;
122 static struct htable retransmits
;
124 atomic_t cmcnt
= ATOMIC_INIT(0);
126 static void add_control_msg(struct control_msg_out
*msg
, int retrans
);
128 static inline int isurgent(struct control_msg_out
*cm
)
130 if (unlikely(cm
->type
== MSGTYPE_PONG
|| cm
->type
== MSGTYPE_ACK
))
135 static struct control_msg_out
*__alloc_control_msg(void)
137 struct control_msg_out
*cm
= kmem_cache_alloc(controlmsg_slab
,
139 if (unlikely(cm
== 0))
141 cm
->lh
.next
= LIST_POISON1
;
142 cm
->lh
.prev
= LIST_POISON2
;
146 static int calc_limit(int limit
, int priority
)
148 if (priority
== ACM_PRIORITY_LOW
)
150 else if (priority
== ACM_PRIORITY_MED
)
151 return (limit
*3 + 3)/4;
152 else if (priority
== ACM_PRIORITY_HIGH
)
158 int may_alloc_control_msg(struct neighbor
*nb
, int priority
)
160 long packets1
= atomic_read(&(nb
->cmcnt
));
161 long packets2
= atomic_read(&(cmcnt
));
163 BUG_ON(packets1
< 0);
164 BUG_ON(packets2
< 0);
166 if (packets1
< calc_limit(GUARANTEED_CMSGS_PER_NEIGH
, priority
))
169 if (unlikely(unlikely(packets2
>= calc_limit(MAX_CMSGS_PER_NEIGH
,
170 priority
)) || unlikely(packets1
>= (
171 calc_limit(MAX_CMSGS_PER_NEIGH
, priority
) *
172 (MAX_CMSGS
- packets2
) / MAX_CMSGS
))))
177 static struct control_msg_out
*_alloc_control_msg(struct neighbor
*nb
,
178 int priority
, int urgent
)
180 struct control_msg_out
*cm
= 0;
185 long packets1
= atomic_inc_return(&(nb
->cmcnt
));
186 long packets2
= atomic_inc_return(&(cmcnt
));
188 BUG_ON(packets1
<= 0);
189 BUG_ON(packets2
<= 0);
191 if (packets1
<= calc_limit(GUARANTEED_CMSGS_PER_NEIGH
,
195 if (unlikely(unlikely(packets2
> calc_limit(MAX_CMSGS_PER_NEIGH
,
196 priority
)) || unlikely(packets1
> (
197 calc_limit(MAX_CMSGS_PER_NEIGH
, priority
) *
198 (MAX_CMSGS
- packets2
) / MAX_CMSGS
))))
203 cm
= __alloc_control_msg();
204 if (unlikely(cm
== 0))
211 atomic_dec(&(nb
->cmcnt
));
212 atomic_dec(&(cmcnt
));
218 struct control_msg_out
*alloc_control_msg(struct neighbor
*nb
, int priority
)
220 return _alloc_control_msg(nb
, priority
, 0);
223 void free_control_msg(struct control_msg_out
*cm
)
225 if (isurgent(cm
) == 0) {
226 atomic_dec(&(cm
->nb
->cmcnt
));
227 atomic_dec(&(cmcnt
));
230 if (cm
->type
== MSGTYPE_ACK_CONN
) {
231 BUG_ON(cm
->msg
.ack_conn
.rconn
== 0);
232 kref_put(&(cm
->msg
.ack_conn
.rconn
->ref
), free_conn
);
233 cm
->msg
.ack_conn
.rconn
= 0;
234 } else if (cm
->type
== MSGTYPE_CONNECT
) {
235 BUG_ON(cm
->msg
.connect
.sconn
== 0);
236 kref_put(&(cm
->msg
.connect
.sconn
->ref
), free_conn
);
237 cm
->msg
.connect
.sconn
= 0;
238 } else if (cm
->type
== MSGTYPE_CONNECT_SUCCESS
) {
239 BUG_ON(cm
->msg
.connect_success
.rconn
== 0);
240 kref_put(&(cm
->msg
.connect_success
.rconn
->ref
), free_conn
);
241 cm
->msg
.connect_success
.rconn
= 0;
244 kmem_cache_free(controlmsg_slab
, cm
);
247 static void free_control_retrans(struct kref
*ref
)
249 struct control_retrans
*cr
= container_of(ref
, struct control_retrans
,
252 while (list_empty(&(cr
->msgs
)) == 0) {
253 struct control_msg_out
*cm
= container_of(cr
->msgs
.next
,
254 struct control_msg_out
, lh
);
256 free_control_msg(cm
);
259 kmem_cache_free(controlretrans_slab
, cr
);
262 struct retransmit_matchparam
{
267 static __u32
rm_to_key(struct retransmit_matchparam
*rm
)
269 return ((__u32
)((long) rm
->nb
)) ^ rm
->seqno
;
272 static void set_retrans_timeout(struct control_retrans
*cr
, struct neighbor
*nb
)
274 cr
->timeout
= jiffies
+ usecs_to_jiffies(100000 +
275 ((__u32
) atomic_read(&(nb
->latency
))) * 2 +
276 ((__u32
) atomic_read(&(nb
->max_remote_cmsg_delay
))));
279 void retransmit_timerfunc(struct work_struct
*work
)
281 unsigned long iflags
;
283 struct neighbor
*nb
= container_of(to_delayed_work(work
),
284 struct neighbor
, retrans_timer
);
289 spin_lock_irqsave( &(nb
->state_lock
), iflags
);
291 spin_unlock_irqrestore( &(nb
->state_lock
), iflags
);
294 struct control_retrans
*cr
= 0;
295 struct retransmit_matchparam rm
;
297 spin_lock_irqsave( &(nb
->retrans_lock
), iflags
);
299 if (list_empty(&(nb
->retrans_list
))) {
300 nb
->retrans_timer_running
= 0;
305 cr
= container_of(nb
->retrans_list
.next
,
306 struct control_retrans
, timeout_list
);
308 BUG_ON(cr
->nb
!= nb
);
310 rm
.seqno
= cr
->seqno
;
313 list_del(&(cr
->timeout_list
));
315 if (unlikely(nbstate
== NEIGHBOR_STATE_KILLED
)) {
316 spin_unlock_irqrestore( &(nb
->retrans_lock
), iflags
);
318 htable_delete(&retransmits
, rm_to_key(&rm
), &rm
,
319 free_control_retrans
);
320 kref_put(&(cr
->ref
), free_control_retrans
);
324 if (time_after(cr
->timeout
, jiffies
)) {
325 list_add(&(cr
->timeout_list
), &(nb
->retrans_list
));
326 schedule_delayed_work(&(nb
->retrans_timer
),
327 cr
->timeout
- jiffies
);
331 if (unlikely(htable_delete(&retransmits
, rm_to_key(&rm
), &rm
,
332 free_control_retrans
)))
335 spin_unlock_irqrestore( &(nb
->retrans_lock
), iflags
);
337 while (list_empty(&(cr
->msgs
)) == 0) {
338 struct control_msg_out
*cm
= container_of(cr
->msgs
.next
,
339 struct control_msg_out
, lh
);
341 add_control_msg(cm
, 1);
344 kref_put(&(cr
->ref
), free_control_retrans
);
347 spin_unlock_irqrestore( &(nb
->retrans_lock
), iflags
);
350 kref_put(&(nb
->ref
), neighbor_free
);
353 static void schedule_retransmit(struct control_retrans
*cr
, struct neighbor
*nb
)
355 unsigned long iflags
;
357 struct retransmit_matchparam rm
;
360 rm
.seqno
= cr
->seqno
;
363 set_retrans_timeout(cr
, nb
);
365 spin_lock_irqsave( &(nb
->retrans_lock
), iflags
);
366 htable_insert(&retransmits
, (char *) cr
, rm_to_key(&rm
));
367 first
= list_empty(&(nb
->retrans_list
));
368 list_add_tail(&(cr
->timeout_list
), &(nb
->retrans_list
));
370 if (first
&& nb
->retrans_timer_running
== 0) {
371 schedule_delayed_work(&(nb
->retrans_timer
),
372 cr
->timeout
- jiffies
);
373 nb
->retrans_timer_running
= 1;
374 kref_get(&(nb
->ref
));
377 spin_unlock_irqrestore( &(nb
->retrans_lock
), iflags
);
380 void kern_ack_rcvd(struct neighbor
*nb
, __u32 seqno
)
382 unsigned long iflags
;
384 struct control_retrans
*cr
= 0;
385 struct retransmit_matchparam rm
;
390 spin_lock_irqsave( &(nb
->retrans_lock
), iflags
);
392 cr
= (struct control_retrans
*) htable_get(&retransmits
, rm_to_key(&rm
),
396 printk(KERN_ERR
"bogus/duplicate ack received");
400 if (unlikely(htable_delete(&retransmits
, rm_to_key(&rm
), &rm
,
401 free_control_retrans
)))
404 BUG_ON(cr
->nb
!= nb
);
406 list_del(&(cr
->timeout_list
));
409 spin_unlock_irqrestore( &(nb
->retrans_lock
), iflags
);
412 kref_put(&(cr
->ref
), free_control_retrans
); /* htable_get */
413 kref_put(&(cr
->ref
), free_control_retrans
); /* list */
417 static void padding(struct sk_buff
*skb
, int length
)
422 dst
= skb_put(skb
, length
);
424 memset(dst
, KP_PADDING
, length
);
427 static int add_ack(struct sk_buff
*skb
, struct control_retrans
*cr
,
428 struct control_msg_out
*cm
, int spaceleft
)
432 if (unlikely(spaceleft
< 5))
435 dst
= skb_put(skb
, 5);
439 put_u32(dst
+ 1, cm
->msg
.ack
.seqno
, 1);
441 atomic_dec(&(cm
->nb
->ucmcnt
));
442 free_control_msg(cm
);
447 static int add_ack_conn(struct sk_buff
*skb
, struct control_retrans
*cr
,
448 struct control_msg_out
*cm
, int spaceleft
)
453 if (unlikely(spaceleft
< cm
->length
))
456 dst
= skb_put(skb
, cm
->length
);
459 dst
[offset
] = KP_ACK_CONN
;
461 put_u32(dst
+ offset
, cm
->msg
.ack_conn
.conn_id
, 1);
463 dst
[offset
] = cm
->msg
.ack_conn
.flags
;
466 if ((cm
->msg
.ack_conn
.flags
& KP_ACK_CONN_FLAGS_SEQNO
) != 0) {
467 put_u32(dst
+ offset
, cm
->msg
.ack_conn
.seqno
, 1);
470 if ((cm
->msg
.ack_conn
.flags
& KP_ACK_CONN_FLAGS_WINDOW
) != 0) {
471 BUG_ON(cm
->msg
.ack_conn
.rconn
== 0);
472 dst
[offset
] = get_window(cm
->msg
.ack_conn
.rconn
);
477 if (ooolen(cm
->msg
.ack_conn
.flags
) != 0) {
478 put_u32(dst
+ offset
, cm
->msg
.ack_conn
.seqno_ooo
, 1);
480 if (ooolen(cm
->msg
.ack_conn
.flags
) == 1) {
481 BUG_ON(cm
->msg
.ack_conn
.length
> 255);
482 dst
[offset
] = cm
->msg
.ack_conn
.length
;
484 } else if (ooolen(cm
->msg
.ack_conn
.flags
) == 2) {
485 BUG_ON(cm
->msg
.ack_conn
.length
<= 255);
486 BUG_ON(cm
->msg
.ack_conn
.length
> 65535);
487 put_u16(dst
+ offset
, cm
->msg
.ack_conn
.length
, 1);
489 } else if (ooolen(cm
->msg
.ack_conn
.flags
) == 4) {
490 BUG_ON(cm
->msg
.ack_conn
.length
<= 65535);
491 put_u32(dst
+ offset
, cm
->msg
.ack_conn
.length
, 1);
499 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
501 BUG_ON(offset
!= cm
->length
);
505 static int add_ping(struct sk_buff
*skb
, __u32 cookie
,
510 if (unlikely(spaceleft
< 5))
513 dst
= skb_put(skb
, 5);
517 put_u32(dst
+ 1, cookie
, 0);
522 static int add_pong(struct sk_buff
*skb
, struct control_retrans
*cr
,
523 struct control_msg_out
*cm
, int spaceleft
)
527 if (unlikely(spaceleft
< 9))
530 dst
= skb_put(skb
, 9);
534 put_u32(dst
+ 1, cm
->msg
.pong
.cookie
, 0);
535 put_u32(dst
+ 5, 1000 * jiffies_to_msecs(jiffies
-
536 cm
->msg
.pong
.time_enqueued
), 1);
539 atomic_dec(&(cm
->nb
->ucmcnt
));
540 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
545 static int add_connect(struct sk_buff
*skb
, struct control_retrans
*cr
,
546 struct control_msg_out
*cm
, int spaceleft
)
550 if (unlikely(spaceleft
< 10))
553 dst
= skb_put(skb
, 10);
557 put_u32(dst
+ 1, cm
->msg
.connect
.conn_id
, 1);
558 put_u32(dst
+ 5, cm
->msg
.connect
.init_seqno
, 1);
559 BUG_ON(cm
->msg
.connect
.sconn
== 0);
560 dst
[9] = get_window(cm
->msg
.connect
.sconn
);
562 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
567 static int add_connect_success(struct sk_buff
*skb
, struct control_retrans
*cr
,
568 struct control_msg_out
*cm
, int spaceleft
)
572 if (unlikely(spaceleft
< 14))
575 dst
= skb_put(skb
, 14);
578 dst
[0] = KP_CONNECT_SUCCESS
;
579 put_u32(dst
+ 1, cm
->msg
.connect_success
.rcvd_conn_id
, 1);
580 put_u32(dst
+ 5, cm
->msg
.connect_success
.gen_conn_id
, 1);
581 put_u32(dst
+ 9, cm
->msg
.connect_success
.init_seqno
, 1);
582 BUG_ON(cm
->msg
.connect_success
.rconn
== 0);
583 dst
[13] = get_window(cm
->msg
.connect_success
.rconn
);
585 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
590 static int add_reset_conn(struct sk_buff
*skb
, struct control_retrans
*cr
,
591 struct control_msg_out
*cm
, int spaceleft
)
595 if (unlikely(spaceleft
< 5))
598 dst
= skb_put(skb
, 5);
601 dst
[0] = KP_RESET_CONN
;
602 put_u32(dst
+ 1, cm
->msg
.reset
.conn_id
, 1);
604 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
609 static int add_conndata(struct sk_buff
*skb
, struct control_retrans
*cr
,
610 struct control_msg_out
*cm
, int spaceleft
,
611 struct control_msg_out
**split_conndata
, __u32
*sc_sendlen
)
615 int totallen
= cm
->msg
.conn_data
.datalen
+ 11;
616 int putlen
= min(totallen
, spaceleft
);
617 int dataputlen
= putlen
- 11;
619 BUG_ON(split_conndata
== 0);
620 BUG_ON(sc_sendlen
== 0);
622 if (dataputlen
< 1 || (spaceleft
< 25 && spaceleft
< totallen
))
625 dst
= skb_put(skb
, putlen
);
628 dst
[0] = KP_CONN_DATA
;
629 put_u32(dst
+ 1, cm
->msg
.conn_data
.conn_id
, 1);
630 put_u32(dst
+ 5, cm
->msg
.conn_data
.seqno
, 1);
631 put_u16(dst
+ 9, dataputlen
, 1);
633 memcpy(dst
+ 11, cm
->msg
.conn_data
.data
, dataputlen
);
635 if (cm
->msg
.conn_data
.datalen
== dataputlen
) {
636 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
638 *split_conndata
= cm
;
639 *sc_sendlen
= dataputlen
;
645 static int add_ping_conn(struct sk_buff
*skb
, struct control_retrans
*cr
,
646 struct control_msg_out
*cm
, int spaceleft
)
650 if (unlikely(spaceleft
< 5))
653 dst
= skb_put(skb
, 5);
656 dst
[0] = KP_PING_CONN
;
657 put_u32(dst
+ 1, cm
->msg
.ping_conn
.conn_id
, 1);
659 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
664 static int add_connid_unknown(struct sk_buff
*skb
, struct control_retrans
*cr
,
665 struct control_msg_out
*cm
, int spaceleft
)
669 if (unlikely(spaceleft
< 5))
672 dst
= skb_put(skb
, 5);
675 dst
[0] = KP_CONNID_UNKNOWN
;
676 put_u32(dst
+ 1, cm
->msg
.connid_unknown
.conn_id
, 1);
678 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
683 static int add_ping_all_conns(struct sk_buff
*skb
, struct control_retrans
*cr
,
684 struct control_msg_out
*cm
, int spaceleft
)
688 if (unlikely(spaceleft
< 1))
691 dst
= skb_put(skb
, 1);
694 dst
[0] = KP_PING_ALL_CONNS
;
696 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
701 static int add_set_max_cmsg_dly(struct sk_buff
*skb
, struct control_retrans
*cr
,
702 struct control_msg_out
*cm
, int spaceleft
)
706 if (unlikely(spaceleft
< 5))
709 dst
= skb_put(skb
, 5);
712 dst
[0] = KP_SET_MAX_CMSG_DELAY
;
713 put_u32(dst
+ 1, cm
->msg
.set_max_cmsg_delay
.delay
, 1);
715 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
720 static int add_message(struct sk_buff
*skb
, struct control_retrans
*cr
,
721 struct control_msg_out
*cm
, int spaceleft
,
722 struct control_msg_out
**split_conndata
, __u32
*sc_sendlen
)
724 BUG_ON(split_conndata
!= 0 && *split_conndata
!= 0);
725 BUG_ON(sc_sendlen
!= 0 && *sc_sendlen
!= 0);
729 return add_ack(skb
, cr
, cm
, spaceleft
);
730 case MSGTYPE_ACK_CONN
:
731 return add_ack_conn(skb
, cr
, cm
, spaceleft
);
733 return add_pong(skb
, cr
, cm
, spaceleft
);
734 case MSGTYPE_CONNECT
:
735 return add_connect(skb
, cr
, cm
, spaceleft
);
736 case MSGTYPE_CONNECT_SUCCESS
:
737 return add_connect_success(skb
, cr
, cm
, spaceleft
);
738 case MSGTYPE_RESET_CONN
:
739 return add_reset_conn(skb
, cr
, cm
, spaceleft
);
740 case MSGTYPE_CONNDATA
:
741 return add_conndata(skb
, cr
, cm
, spaceleft
, split_conndata
,
743 case MSGTYPE_PING_CONN
:
744 return add_ping_conn(skb
, cr
, cm
, spaceleft
);
745 case MSGTYPE_CONNID_UNKNOWN
:
746 return add_connid_unknown(skb
, cr
, cm
, spaceleft
);
747 case MSGTYPE_PING_ALL_CONNS
:
748 return add_ping_all_conns(skb
, cr
, cm
, spaceleft
);
749 case MSGTYPE_SET_MAX_CMSG_DELAY
:
750 return add_set_max_cmsg_dly(skb
, cr
, cm
, spaceleft
);
758 static __u32
recount_ping_conns(struct neighbor
*nb
)
761 struct list_head
*curr
= nb
->next_ping_conn
->target
.out
.nb_list
.next
;
762 while (curr
!= &(nb
->snd_conn_list
)) {
764 BUG_ON(cnt
> 1000000000);
769 static __u32
__send_messages_pc(struct neighbor
*nb
, struct sk_buff
*skb
,
770 struct control_retrans
*cr
, int spaceleft
)
773 mutex_lock(&(nb
->conn_list_lock
));
774 while (nb
->next_ping_conn
!= 0) {
777 struct list_head
*next
;
778 struct control_msg_out
*cm
;
781 rconn
= nb
->next_ping_conn
;
782 sconn
= rconn
->reversedir
;
784 BUG_ON(rconn
->targettype
!= TARGET_OUT
);
785 BUG_ON(sconn
->sourcetype
!= SOURCE_IN
);
787 if (unlikely(rconn
->target
.out
.conn_id
))
790 if (nb
->ping_conns_remaining
== 0) {
791 atomic_set(&(sconn
->source
.in
.pong_awaiting
), 1);
792 nb
->pong_conns_expected
++;
793 nb
->ping_conns_remaining
--;
794 if (unlikely(nb
->ping_conns_remaining
== 0))
795 nb
->ping_conns_remaining
=
796 recount_ping_conns(nb
);
798 if (likely(atomic_read(&(
799 sconn
->source
.in
.pong_awaiting
)) == 0))
801 nb
->ping_conns_remaining
--;
802 if (unlikely(nb
->ping_conns_retrans_remaining
== 0))
803 nb
->ping_conns_retrans_remaining
=
804 recount_ping_conns(nb
);
807 cm
= alloc_control_msg(nb
, ACM_PRIORITY_LOW
);
809 cm
->type
= MSGTYPE_PING_CONN
;
810 cm
->msg
.ping_conn
.conn_id
= rconn
->target
.out
.conn_id
;
811 rc
= add_message(skb
, cr
, cm
, spaceleft
- length
, 0, 0);
817 next
= rconn
->target
.out
.nb_list
.next
;
818 nb
->next_ping_conn
= container_of(next
, struct conn
,
820 if (next
== &(nb
->snd_conn_list
)) {
821 nb
->next_ping_conn
= 0;
822 nb
->ping_conns_remaining
= 0;
825 if (unlikely(length
!= 0)) {
826 nb
->ping_conn_completed
= jiffies
;
828 mutex_unlock(&(nb
->conn_list_lock
));
832 static __u32
__send_messages(struct neighbor
*nb
, struct sk_buff
*skb
,
833 struct control_retrans
*cr
, int spaceleft
, int urgentonly
,
834 struct control_msg_out
**split_conndata
, __u32
*sc_sendlen
)
837 while (!list_empty(&(nb
->ucontrol_msgs_out
)) || (!urgentonly
&&
838 !list_empty(&(nb
->control_msgs_out
)))) {
841 int urgent
= !list_empty(&(nb
->ucontrol_msgs_out
));
843 struct control_msg_out
*cm
;
846 cm
= container_of(nb
->ucontrol_msgs_out
.next
,
847 struct control_msg_out
, lh
);
849 cm
= container_of(nb
->control_msgs_out
.next
,
850 struct control_msg_out
, lh
);
854 nb
->ucmlength
-= cm
->length
;
856 nb
->cmlength
-= cm
->length
;
857 mutex_unlock(&(nb
->cmsg_lock
));
858 rc
= add_message(skb
, cr
, cm
, spaceleft
- length
,
859 split_conndata
, sc_sendlen
);
860 mutex_lock(&(nb
->cmsg_lock
));
864 list_add(&(cm
->lh
), &(nb
->ucontrol_msgs_out
));
865 nb
->ucmlength
+= cm
->length
;
867 list_add(&(cm
->lh
), &(nb
->control_msgs_out
));
868 nb
->cmlength
+= cm
->length
;
879 static int msgtype_present(struct neighbor
*nb
, __u8 type
)
881 struct list_head
*curr
;
883 curr
= nb
->control_msgs_out
.next
;
884 while (curr
!= &(nb
->control_msgs_out
)) {
885 struct control_msg_out
*cm
= container_of(curr
,
886 struct control_msg_out
, lh
);
888 if (cm
->type
== MSGTYPE_PING_ALL_CONNS
)
897 static int ping_all_conns_needed(struct neighbor
*nb
)
899 if (likely(nb
->ping_all_conns
== 0))
902 if (msgtype_present(nb
, MSGTYPE_PING_ALL_CONNS
))
908 static int __send_messages_smcd(struct neighbor
*nb
, struct sk_buff
*skb
,
909 struct control_retrans
*cr
, int spaceleft
)
911 struct control_msg_out
*cm
;
914 cm
= alloc_control_msg(nb
, ACM_PRIORITY_MED
);
916 if (unlikely(cm
== 0))
919 cm
->type
= MSGTYPE_SET_MAX_CMSG_DELAY
;
920 cm
->msg
.set_max_cmsg_delay
.delay
= CMSG_INTERVAL_MS
* 10;
923 rc
= add_message(skb
, cr
, cm
, spaceleft
, 0, 0);
925 nb
->max_cmsg_delay_sent
= 1;
930 static int __send_messages_pac(struct neighbor
*nb
, struct sk_buff
*skb
,
931 struct control_retrans
*cr
, int spaceleft
)
933 struct control_msg_out
*cm
;
936 cm
= alloc_control_msg(nb
, ACM_PRIORITY_MED
);
938 if (unlikely(cm
== 0))
941 cm
->type
= MSGTYPE_PING_ALL_CONNS
;
944 rc
= add_message(skb
, cr
, cm
, spaceleft
, 0, 0);
946 nb
->ping_all_conns
= 0;
951 static int _send_messages(struct neighbor
*nb
, struct sk_buff
*skb
, int ping
,
952 struct control_retrans
*cr
, int spaceleft
, int urgentonly
)
956 __u32 pingcookie
= 0;
957 struct control_msg_out
*split_conndata
= 0;
958 __u32 sc_sendlen
= 0;
960 mutex_lock(&(nb
->cmsg_lock
));
964 pingcookie
= add_ping_req(nb
);
965 rc
= add_ping(skb
, pingcookie
, spaceleft
- length
);
970 if (likely(urgentonly
== 0) && unlikely(ping_all_conns_needed(nb
) != 0))
971 length
+= __send_messages_pac(nb
, skb
, cr
, spaceleft
- length
);
973 if (likely(urgentonly
== 0) && unlikely(nb
->max_cmsg_delay_sent
== 0))
974 length
+= __send_messages_smcd(nb
, skb
, cr
, spaceleft
- length
);
977 length
+= __send_messages(nb
, skb
, cr
, spaceleft
- length
, urgentonly
,
978 &split_conndata
, &sc_sendlen
);
980 if (likely(urgentonly
== 0))
981 length
+= __send_messages_pc(nb
, skb
, cr
, spaceleft
- length
);
983 mutex_unlock(&(nb
->cmsg_lock
));
985 if (unlikely(length
> spaceleft
))
986 printk(KERN_ERR
"error cor/kpacket_gen: length > spaceleft!?");
988 padding(skb
, spaceleft
- length
);
990 rc
= dev_queue_xmit(skb
);
993 unadd_ping_req(nb
, pingcookie
);
995 while (list_empty(&(cr
->msgs
)) == 0) {
996 struct control_msg_out
*cm
= container_of(cr
->msgs
.prev
,
997 struct control_msg_out
, lh
);
999 add_control_msg(cm
, 1);
1002 if (split_conndata
!= 0) {
1003 add_control_msg(split_conndata
, 1);
1006 kref_put(&(cr
->ref
), free_control_retrans
);
1008 struct list_head
*curr
= cr
->msgs
.next
;
1010 while(curr
!= &(cr
->msgs
)) {
1011 struct control_msg_out
*cm
= container_of(curr
,
1012 struct control_msg_out
, lh
);
1016 if (cm
->type
== MSGTYPE_CONNDATA
) {
1017 list_del(&(cm
->lh
));
1018 kfree(cm
->msg
.conn_data
.data_orig
);
1019 free_control_msg(cm
);
1023 if (split_conndata
!= 0) {
1024 BUG_ON(sc_sendlen
== 0);
1025 BUG_ON(sc_sendlen
>=
1026 split_conndata
->msg
.conn_data
.datalen
);
1028 split_conndata
->msg
.conn_data
.data
+= sc_sendlen
;
1029 split_conndata
->msg
.conn_data
.datalen
-= sc_sendlen
;
1031 send_conndata(split_conndata
,
1032 split_conndata
->msg
.conn_data
.conn_id
,
1033 split_conndata
->msg
.conn_data
.seqno
,
1034 split_conndata
->msg
.conn_data
.data_orig
,
1035 split_conndata
->msg
.conn_data
.data
,
1036 split_conndata
->msg
.conn_data
.datalen
);
1040 if (list_empty(&(cr
->msgs
)))
1041 kref_put(&(cr
->ref
), free_control_retrans
);
1043 schedule_retransmit(cr
, nb
);
1049 static __u32
get_total_messages_length(struct neighbor
*nb
, int ping
,
1052 __u32 length
= nb
->ucmlength
;
1054 if (likely(urgentonly
== 0)) {
1055 length
+= nb
->cmlength
+ nb
->ping_conns_remaining
* 5;
1056 if (likely(nb
->ping_conns_remaining
== 0)) {
1057 if (likely(nb
->ping_conns_retrans_remaining
== 0) &&
1058 unlikely(nb
->pong_conns_expected
!=0) &&
1059 time_before(nb
->ping_conn_completed
,
1060 jiffies
+ msecs_to_jiffies(
1061 PING_ALL_CONNS_TIMEOUT
) +
1062 usecs_to_jiffies(((__u32
) atomic_read(&(
1063 nb
->latency
))) * 2 + ((__u32
)
1064 atomic_read(&(nb
->max_remote_cmsg_delay
)
1066 nb
->ping_conns_retrans_remaining
=
1067 nb
->pong_conns_expected
;
1069 if (unlikely(nb
->ping_conns_retrans_remaining
>
1070 nb
->pong_conns_expected
))
1071 nb
->ping_conns_retrans_remaining
=
1072 nb
->pong_conns_expected
;
1074 length
+= nb
->ping_conns_retrans_remaining
* 5;
1076 if (unlikely(ping_all_conns_needed(nb
) != 0))
1078 if (unlikely(nb
->max_cmsg_delay_sent
== 0))
1081 if (ping
== 2 || (length
> 0 && ping
!= 0))
1087 int send_messages(struct neighbor
*nb
, int allmsgs
, int resume
)
1091 int targetmss
= mss(nb
);
1093 int nbstate
= get_neigh_state(nb
);
1094 int urgentonly
= (nbstate
!= NEIGHBOR_STATE_ACTIVE
);
1096 mutex_lock(&(nb
->cmsg_lock
));
1099 allmsgs
= nb
->kp_allmsgs
;
1101 ping
= time_to_send_ping(nb
);
1107 struct sk_buff
*skb
;
1108 struct control_retrans
*cr
;
1110 BUG_ON(list_empty(&(nb
->control_msgs_out
)) &&
1111 (nb
->cmlength
!= 0));
1112 BUG_ON((list_empty(&(nb
->control_msgs_out
)) == 0) &&
1113 (nb
->cmlength
== 0));
1114 BUG_ON(list_empty(&(nb
->ucontrol_msgs_out
)) &&
1115 (nb
->ucmlength
!= 0));
1116 BUG_ON((list_empty(&(nb
->ucontrol_msgs_out
)) == 0) &&
1117 (nb
->ucmlength
== 0));
1118 BUG_ON(nb
->cmlength
< 0);
1119 BUG_ON(nb
->ucmlength
< 0);
1121 length
= get_total_messages_length(nb
, ping
, urgentonly
);
1126 if (length
< targetmss
&& allmsgs
== 0)
1129 seqno
= atomic_add_return(1, &(nb
->kpacket_seqno
));
1131 if (length
> targetmss
)
1134 mutex_unlock(&(nb
->cmsg_lock
));
1135 skb
= create_packet(nb
, length
, GFP_KERNEL
, 0, seqno
);
1136 if (unlikely(skb
== 0)) {
1137 printk(KERN_ERR
"cor: send_messages: cannot allocate "
1138 "skb (out of memory?)");
1142 cr
= kmem_cache_alloc(controlretrans_slab
, GFP_KERNEL
);
1143 if (unlikely(cr
== 0)) {
1145 printk(KERN_ERR
"cor: send_messages: cannot allocate "
1146 "control_retrans (out of memory?)");
1149 memset(cr
, 0, sizeof(struct control_retrans
));
1150 kref_init(&(cr
->ref
));
1153 INIT_LIST_HEAD(&(cr
->msgs
));
1155 rc
= _send_messages(nb
, skb
, ping
, cr
, length
, urgentonly
);
1158 mutex_lock(&(nb
->cmsg_lock
));
1166 mutex_lock(&(nb
->cmsg_lock
));
1171 nb
->kp_allmsgs
= nb
->kp_allmsgs
|| allmsgs
;
1172 qos_enqueue(nb
->dev
, &(nb
->rb_kp
), QOS_CALLER_KPACKET
);
1174 } else if (allmsgs
) {
1178 mutex_unlock(&(nb
->cmsg_lock
));
1181 schedule_controlmsg_timerfunc(nb
);
1186 static void controlmsg_timerfunc(struct work_struct
*work
)
1188 struct neighbor
*nb
= container_of(to_delayed_work(work
),
1189 struct neighbor
, cmsg_timer
);
1190 unsigned long jiffies_tmp
= jiffies
;
1192 mutex_lock(&(nb
->cmsg_lock
));
1194 if (time_after(nb
->timeout
, jiffies_tmp
)) {
1195 INIT_DELAYED_WORK(&(nb
->cmsg_timer
), controlmsg_timerfunc
);
1196 schedule_delayed_work(&(nb
->cmsg_timer
), nb
->timeout
-
1198 mutex_unlock(&(nb
->cmsg_lock
));
1202 mutex_unlock(&(nb
->cmsg_lock
));
1204 send_messages(nb
, 1, 0);
1205 kref_put(&(nb
->ref
), neighbor_free
);
1208 void schedule_controlmsg_timerfunc(struct neighbor
*nb
)
1210 __u64 jiffies
= get_jiffies_64();
1213 int state
= get_neigh_state(nb
);
1215 if (unlikely(state
== NEIGHBOR_STATE_KILLED
))
1218 mutex_lock(&(nb
->cmsg_lock
));
1219 nb
->timeout
+= msecs_to_jiffies(CMSG_INTERVAL_MS
);
1221 delay
= nb
->timeout
- jiffies
;
1224 nb
->timeout
= jiffies
;
1227 INIT_DELAYED_WORK(&(nb
->cmsg_timer
), controlmsg_timerfunc
);
1228 schedule_delayed_work(&(nb
->cmsg_timer
), delay
);
1229 mutex_unlock(&(nb
->cmsg_lock
));
1230 kref_get(&(nb
->ref
));
1233 static void free_oldest_ucm(struct neighbor
*nb
)
1235 struct control_msg_out
*cm
= container_of(nb
->ucontrol_msgs_out
.next
,
1236 struct control_msg_out
, lh
);
1238 BUG_ON(list_empty(&(nb
->ucontrol_msgs_out
)));
1239 BUG_ON(isurgent(cm
) == 0);
1241 list_del(&(cm
->lh
));
1242 nb
->ucmlength
-= cm
->length
;
1243 atomic_dec(&(nb
->ucmcnt
));
1244 free_control_msg(cm
);
1247 static void add_control_msg(struct control_msg_out
*cm
, int retrans
)
1251 BUG_ON(cm
->nb
== 0);
1253 nbstate
= get_neigh_state(cm
->nb
);
1256 BUG_ON(cm
->lh
.next
!= LIST_POISON1
|| cm
->lh
.prev
!= LIST_POISON2
);
1258 mutex_lock(&(cm
->nb
->cmsg_lock
));
1263 msgs
= atomic_inc_return(&(cm
->nb
->ucmcnt
));
1266 if (unlikely(retrans
)) {
1267 if (msgs
> MAX_URGENT_CMSGS_PER_NEIGH_RETRANSALLOW
||
1268 msgs
> MAX_URGENT_CMSGS_PER_NEIGH
) {
1269 atomic_dec(&(cm
->nb
->ucmcnt
));
1270 free_control_msg(cm
);
1274 cm
->nb
->ucmlength
+= cm
->length
;
1275 list_add(&(cm
->lh
), &(cm
->nb
->ucontrol_msgs_out
));
1277 if (msgs
> MAX_URGENT_CMSGS_PER_NEIGH
) {
1278 free_oldest_ucm(cm
->nb
);
1281 cm
->nb
->ucmlength
+= cm
->length
;
1282 list_add_tail(&(cm
->lh
), &(cm
->nb
->ucontrol_msgs_out
));
1285 cm
->nb
->cmlength
+= cm
->length
;
1286 list_add_tail(&(cm
->lh
), &(cm
->nb
->control_msgs_out
));
1289 #warning todo measure inter message interval
1290 if (unlikely((nbstate
== NEIGHBOR_STATE_ACTIVE
? cm
->nb
->cmlength
: 0) +
1291 cm
->nb
->ucmlength
>= mss(cm
->nb
)))
1292 send_messages(cm
->nb
, 0, 0);
1295 mutex_unlock(&(cm
->nb
->cmsg_lock
));
1298 void send_pong(struct neighbor
*nb
, __u32 cookie
)
1300 struct control_msg_out
*cm
= _alloc_control_msg(nb
, 0, 1);
1302 if (unlikely(cm
== 0))
1306 cm
->type
= MSGTYPE_PONG
;
1307 cm
->msg
.pong
.cookie
= cookie
;
1308 cm
->msg
.pong
.time_enqueued
= jiffies
;
1310 add_control_msg(cm
, 0);
1313 void send_reset_conn(struct control_msg_out
*cm
, __u32 conn_id
)
1315 cm
->type
= MSGTYPE_RESET_CONN
;
1316 cm
->msg
.reset
.conn_id
= conn_id
;
1318 add_control_msg(cm
, 0);
1321 void send_ack(struct neighbor
*nb
, __u32 seqno
)
1323 struct control_msg_out
*cm
= _alloc_control_msg(nb
, 0, 1);
1325 if (unlikely(cm
== 0))
1329 cm
->type
= MSGTYPE_ACK
;
1330 cm
->msg
.ack
.seqno
= seqno
;
1332 add_control_msg(cm
, 0);
1335 void send_ack_conn(struct control_msg_out
*cm
, struct conn
*rconn
,
1336 __u32 conn_id
, __u32 seqno
)
1338 cm
->type
= MSGTYPE_ACK_CONN
;
1339 kref_get(&(rconn
->ref
));
1340 BUG_ON(rconn
->sourcetype
!= SOURCE_IN
);
1341 cm
->msg
.ack_conn
.flags
= KP_ACK_CONN_FLAGS_SEQNO
|
1342 KP_ACK_CONN_FLAGS_WINDOW
;
1343 cm
->msg
.ack_conn
.rconn
= rconn
;
1344 cm
->msg
.ack_conn
.conn_id
= conn_id
;
1345 cm
->msg
.ack_conn
.seqno
= seqno
;
1346 cm
->length
= 6 + ack_conn_len(cm
->msg
.ack_conn
.flags
);
1347 add_control_msg(cm
, 0);
1350 void send_ack_conn_ooo(struct control_msg_out
*cm
, struct conn
*rconn
,
1351 __u32 conn_id
, __u32 seqno_ooo
, __u32 length
)
1353 cm
->type
= MSGTYPE_ACK_CONN
;
1354 kref_get(&(rconn
->ref
));
1355 BUG_ON(rconn
->sourcetype
!= SOURCE_IN
);
1356 cm
->msg
.ack_conn
.flags
= ooolen_to_flags(length
);
1357 cm
->msg
.ack_conn
.rconn
= rconn
;
1358 cm
->msg
.ack_conn
.conn_id
= conn_id
;
1359 cm
->msg
.ack_conn
.seqno_ooo
= seqno_ooo
;
1360 cm
->msg
.ack_conn
.length
= length
;
1361 cm
->length
= 6 + ack_conn_len(cm
->msg
.ack_conn
.flags
);
1362 add_control_msg(cm
, 0);
1365 void send_connect_success(struct control_msg_out
*cm
, __u32 rcvd_conn_id
,
1366 __u32 gen_conn_id
, __u32 init_seqno
, struct conn
*rconn
)
1368 cm
->type
= MSGTYPE_CONNECT_SUCCESS
;
1369 cm
->msg
.connect_success
.rcvd_conn_id
= rcvd_conn_id
;
1370 cm
->msg
.connect_success
.gen_conn_id
= gen_conn_id
;
1371 cm
->msg
.connect_success
.init_seqno
= init_seqno
;
1372 kref_get(&(rconn
->ref
));
1373 BUG_ON(rconn
->sourcetype
!= SOURCE_IN
);
1374 cm
->msg
.connect_success
.rconn
= rconn
;
1376 add_control_msg(cm
, 0);
1379 void send_connect_nb(struct control_msg_out
*cm
, __u32 conn_id
,
1380 __u32 init_seqno
, struct conn
*sconn
)
1382 cm
->type
= MSGTYPE_CONNECT
;
1383 cm
->msg
.connect
.conn_id
= conn_id
;
1384 cm
->msg
.connect
.init_seqno
= init_seqno
;
1385 kref_get(&(sconn
->ref
));
1386 BUG_ON(sconn
->sourcetype
!= SOURCE_IN
);
1387 cm
->msg
.connect
.sconn
= sconn
;
1389 add_control_msg(cm
, 0);
1392 void send_conndata(struct control_msg_out
*cm
, __u32 conn_id
, __u32 seqno
,
1393 char *data_orig
, char *data
, __u32 datalen
)
1395 cm
->type
= MSGTYPE_CONNDATA
;
1396 cm
->msg
.conn_data
.conn_id
= conn_id
;
1397 cm
->msg
.conn_data
.seqno
= seqno
;
1398 cm
->msg
.conn_data
.data_orig
= data_orig
;
1399 cm
->msg
.conn_data
.data
= data
;
1400 cm
->msg
.conn_data
.datalen
= datalen
;
1401 cm
->length
= 11 + datalen
;
1402 add_control_msg(cm
, 0);
1405 void send_ping_conn(struct control_msg_out
*cm
, __u32 conn_id
)
1407 cm
->type
= MSGTYPE_PING_CONN
;
1408 cm
->msg
.ping_conn
.conn_id
= conn_id
;
1410 add_control_msg(cm
, 0);
1414 void send_connid_unknown(struct control_msg_out
*cm
, __u32 conn_id
)
1416 cm
->type
= MSGTYPE_CONNID_UNKNOWN
;
1417 cm
->msg
.connid_unknown
.conn_id
= conn_id
;
1419 add_control_msg(cm
, 0);
1422 void send_ping_all_conns(struct neighbor
*nb
)
1424 mutex_lock(&(nb
->cmsg_lock
));
1425 nb
->ping_all_conns
= 1;
1426 mutex_unlock(&(nb
->cmsg_lock
));
1430 void set_creditrate_out(struct control_msg_out
*cm
, __u32 conn_id
,
1433 free_control_msg(cm
);
1438 static int matches_connretrans(void *htentry
, void *searcheditem
)
1440 struct control_retrans
*cr
= (struct control_retrans
*) htentry
;
1441 struct retransmit_matchparam
*rm
= (struct retransmit_matchparam
*)
1444 return rm
->nb
== cr
->nb
&& rm
->seqno
== cr
->seqno
;
1447 void __init
cor_kgen_init(void)
1449 controlmsg_slab
= kmem_cache_create("cor_controlmsg",
1450 sizeof(struct control_msg_out
), 8, 0, 0);
1451 controlretrans_slab
= kmem_cache_create("cor_controlretransmsg",
1452 sizeof(struct control_retrans
), 8, 0, 0);
1453 htable_init(&retransmits
, matches_connretrans
,
1454 offsetof(struct control_retrans
, htab_entry
),
1455 offsetof(struct control_retrans
, ref
));
1458 MODULE_LICENSE("GPL");