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_ACK_CONN_OOO 4
30 #define MSGTYPE_CONNECT 5
31 #define MSGTYPE_CONNECT_SUCCESS 6
32 #define MSGTYPE_RESET_CONN 7
33 #define MSGTYPE_CONNDATA 8
34 #define MSGTYPE_PING_CONN 9
35 #define MSGTYPE_CONNID_UNKNOWN 10
36 #define MSGTYPE_PING_ALL_CONNS 11
37 #define MSGTYPE_SET_MAX_CMSG_DELAY 12
38 #define MSGTYPE_SET_CREDITS 13
40 struct control_msg_out
{
41 struct list_head lh
; /* either neighbor or control_retrans_packet */
50 unsigned long time_enqueued
; /* jiffies */
109 struct control_retrans
{
115 unsigned long timeout
;
117 struct list_head msgs
;
119 struct htab_entry htab_entry
;
120 struct list_head timeout_list
;
123 struct kmem_cache
*controlmsg_slab
;
124 struct kmem_cache
*controlretrans_slab
;
126 static struct htable retransmits
;
128 atomic_t cmcnt
= ATOMIC_INIT(0);
130 static void add_control_msg(struct control_msg_out
*msg
, int retrans
);
132 static inline int isurgent(struct control_msg_out
*cm
)
134 if (unlikely(cm
->type
== MSGTYPE_PONG
|| cm
->type
== MSGTYPE_ACK
))
139 static struct control_msg_out
*__alloc_control_msg(void)
141 struct control_msg_out
*cm
= kmem_cache_alloc(controlmsg_slab
,
143 if (unlikely(cm
== 0))
145 cm
->lh
.next
= LIST_POISON1
;
146 cm
->lh
.prev
= LIST_POISON2
;
150 static int calc_limit(int limit
, int priority
)
152 if (priority
== ACM_PRIORITY_LOW
)
154 else if (priority
== ACM_PRIORITY_MED
)
155 return (limit
*3 + 3)/4;
156 else if (priority
== ACM_PRIORITY_HIGH
)
162 static struct control_msg_out
*_alloc_control_msg(struct neighbor
*nb
,
163 int priority
, int urgent
)
165 struct control_msg_out
*cm
= 0;
170 long packets1
= atomic_inc_return(&(nb
->cmcnt
));
171 long packets2
= atomic_inc_return(&(cmcnt
));
173 BUG_ON(packets1
<= 0);
174 BUG_ON(packets2
<= 0);
176 if (packets1
<= calc_limit(GUARANTEED_CMSGS_PER_NEIGH
,
180 if (unlikely(unlikely(packets2
> calc_limit(MAX_CMSGS_PER_NEIGH
,
181 priority
)) || unlikely(packets1
> (
182 calc_limit(MAX_CMSGS_PER_NEIGH
, priority
) *
183 (MAX_CMSGS
- packets2
) / MAX_CMSGS
))))
188 cm
= __alloc_control_msg();
189 if (unlikely(cm
== 0))
196 atomic_dec(&(nb
->cmcnt
));
197 atomic_dec(&(cmcnt
));
203 struct control_msg_out
*alloc_control_msg(struct neighbor
*nb
, int priority
)
205 return _alloc_control_msg(nb
, priority
, 0);
208 void free_control_msg(struct control_msg_out
*cm
)
210 if (isurgent(cm
) == 0) {
211 atomic_dec(&(cm
->nb
->cmcnt
));
212 atomic_dec(&(cmcnt
));
215 if (cm
->type
== MSGTYPE_ACK_CONN
)
216 kref_put(&(cm
->msg
.ack_conn
.rconn
->ref
), free_conn
);
217 else if (cm
->type
== MSGTYPE_ACK_CONN_OOO
)
218 kref_put(&(cm
->msg
.ack_conn_ooo
.rconn
->ref
), free_conn
);
219 else if (cm
->type
== MSGTYPE_CONNECT
)
220 kref_put(&(cm
->msg
.connect
.sconn
->ref
), free_conn
);
221 else if (cm
->type
== MSGTYPE_CONNECT_SUCCESS
)
222 kref_put(&(cm
->msg
.connect_success
.rconn
->ref
), free_conn
);
224 kmem_cache_free(controlmsg_slab
, cm
);
227 static void free_control_retrans(struct kref
*ref
)
229 struct control_retrans
*cr
= container_of(ref
, struct control_retrans
,
232 while (list_empty(&(cr
->msgs
)) == 0) {
233 struct control_msg_out
*cm
= container_of(cr
->msgs
.next
,
234 struct control_msg_out
, lh
);
236 free_control_msg(cm
);
239 kmem_cache_free(controlretrans_slab
, cr
);
242 struct retransmit_matchparam
{
247 static __u32
rm_to_key(struct retransmit_matchparam
*rm
)
249 return ((__u32
)((long) rm
->nb
)) ^ rm
->seqno
;
252 static void set_retrans_timeout(struct control_retrans
*cr
, struct neighbor
*nb
)
254 cr
->timeout
= jiffies
+ usecs_to_jiffies(100000 +
255 ((__u32
) atomic_read(&(nb
->latency
))) * 2 +
256 ((__u32
) atomic_read(&(nb
->max_remote_cmsg_delay
))));
259 void retransmit_timerfunc(struct work_struct
*work
)
261 unsigned long iflags
;
263 struct neighbor
*nb
= container_of(to_delayed_work(work
),
264 struct neighbor
, retrans_timer
);
269 spin_lock_irqsave( &(nb
->state_lock
), iflags
);
271 spin_unlock_irqrestore( &(nb
->state_lock
), iflags
);
274 struct control_retrans
*cr
= 0;
275 struct retransmit_matchparam rm
;
277 spin_lock_irqsave( &(nb
->retrans_lock
), iflags
);
279 if (list_empty(&(nb
->retrans_list
))) {
280 nb
->retrans_timer_running
= 0;
285 cr
= container_of(nb
->retrans_list
.next
,
286 struct control_retrans
, timeout_list
);
288 BUG_ON(cr
->nb
!= nb
);
290 rm
.seqno
= cr
->seqno
;
293 list_del(&(cr
->timeout_list
));
295 if (unlikely(nbstate
== NEIGHBOR_STATE_KILLED
)) {
296 spin_unlock_irqrestore( &(nb
->retrans_lock
), iflags
);
298 htable_delete(&retransmits
, rm_to_key(&rm
), &rm
,
299 free_control_retrans
);
300 kref_put(&(cr
->ref
), free_control_retrans
);
304 if (time_after(cr
->timeout
, jiffies
)) {
305 list_add(&(cr
->timeout_list
), &(nb
->retrans_list
));
306 schedule_delayed_work(&(nb
->retrans_timer
),
307 cr
->timeout
- jiffies
);
311 if (unlikely(htable_delete(&retransmits
, rm_to_key(&rm
), &rm
,
312 free_control_retrans
)))
315 spin_unlock_irqrestore( &(nb
->retrans_lock
), iflags
);
317 while (list_empty(&(cr
->msgs
)) == 0) {
318 struct control_msg_out
*cm
= container_of(cr
->msgs
.next
,
319 struct control_msg_out
, lh
);
321 add_control_msg(cm
, 1);
324 kref_put(&(cr
->ref
), free_control_retrans
);
327 spin_unlock_irqrestore( &(nb
->retrans_lock
), iflags
);
330 kref_put(&(nb
->ref
), neighbor_free
);
333 static void schedule_retransmit(struct control_retrans
*cr
, struct neighbor
*nb
)
335 unsigned long iflags
;
337 struct retransmit_matchparam rm
;
340 rm
.seqno
= cr
->seqno
;
343 set_retrans_timeout(cr
, nb
);
345 spin_lock_irqsave( &(nb
->retrans_lock
), iflags
);
346 htable_insert(&retransmits
, (char *) cr
, rm_to_key(&rm
));
347 first
= list_empty(&(nb
->retrans_list
));
348 list_add_tail(&(cr
->timeout_list
), &(nb
->retrans_list
));
350 if (first
&& nb
->retrans_timer_running
== 0) {
351 schedule_delayed_work(&(nb
->retrans_timer
),
352 cr
->timeout
- jiffies
);
353 nb
->retrans_timer_running
= 1;
354 kref_get(&(nb
->ref
));
357 spin_unlock_irqrestore( &(nb
->retrans_lock
), iflags
);
360 void kern_ack_rcvd(struct neighbor
*nb
, __u32 seqno
)
362 unsigned long iflags
;
364 struct control_retrans
*cr
= 0;
365 struct retransmit_matchparam rm
;
370 spin_lock_irqsave( &(nb
->retrans_lock
), iflags
);
372 cr
= (struct control_retrans
*) htable_get(&retransmits
, rm_to_key(&rm
),
376 printk(KERN_ERR
"bogus/duplicate ack received");
380 if (unlikely(htable_delete(&retransmits
, rm_to_key(&rm
), &rm
,
381 free_control_retrans
)))
384 BUG_ON(cr
->nb
!= nb
);
386 list_del(&(cr
->timeout_list
));
389 spin_unlock_irqrestore( &(nb
->retrans_lock
), iflags
);
392 static void padding(struct sk_buff
*skb
, int length
)
397 dst
= skb_put(skb
, length
);
399 memset(dst
, KP_PADDING
, length
);
402 static int add_ack(struct sk_buff
*skb
, struct control_retrans
*cr
,
403 struct control_msg_out
*cm
, int spaceleft
)
407 if (unlikely(spaceleft
< 5))
410 dst
= skb_put(skb
, 5);
414 put_u32(dst
+ 1, cm
->msg
.ack
.seqno
, 1);
416 atomic_dec(&(cm
->nb
->ucmcnt
));
417 free_control_msg(cm
);
422 static int add_ack_conn(struct sk_buff
*skb
, struct control_retrans
*cr
,
423 struct control_msg_out
*cm
, int spaceleft
)
427 if (unlikely(spaceleft
< 10))
430 dst
= skb_put(skb
, 10);
433 dst
[0] = KP_ACK_CONN
;
434 put_u32(dst
+ 1, cm
->msg
.ack_conn
.conn_id
, 1);
435 put_u32(dst
+ 5, cm
->msg
.ack_conn
.seqno
, 1);
436 dst
[9] = enc_window(get_window(cm
->msg
.ack_conn
.rconn
));
438 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
443 static int add_ack_conn_ooo(struct sk_buff
*skb
, struct control_retrans
*cr
,
444 struct control_msg_out
*cm
, int spaceleft
)
448 if (unlikely(spaceleft
< 18))
451 dst
= skb_put(skb
, 18);
454 dst
[0] = KP_ACK_CONN_OOO
;
455 put_u32(dst
+ 1, cm
->msg
.ack_conn_ooo
.conn_id
, 1);
456 put_u32(dst
+ 5, cm
->msg
.ack_conn_ooo
.seqno
, 1);
457 dst
[9] = enc_window(get_window(cm
->msg
.ack_conn_ooo
.rconn
));
458 put_u32(dst
+ 10, cm
->msg
.ack_conn_ooo
.seqno_ooo
, 1);
459 put_u32(dst
+ 14, cm
->msg
.ack_conn_ooo
.length
, 1);
461 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
466 static int add_ping(struct sk_buff
*skb
, __u32 cookie
,
471 if (unlikely(spaceleft
< 5))
474 dst
= skb_put(skb
, 5);
478 put_u32(dst
+ 1, cookie
, 0);
483 static int add_pong(struct sk_buff
*skb
, struct control_retrans
*cr
,
484 struct control_msg_out
*cm
, int spaceleft
)
488 if (unlikely(spaceleft
< 9))
491 dst
= skb_put(skb
, 9);
495 put_u32(dst
+ 1, cm
->msg
.pong
.cookie
, 0);
496 put_u32(dst
+ 5, 1000 * jiffies_to_msecs(jiffies
-
497 cm
->msg
.pong
.time_enqueued
), 1);
500 atomic_dec(&(cm
->nb
->ucmcnt
));
501 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
506 #warning todo initial seqno
507 static int add_connect(struct sk_buff
*skb
, struct control_retrans
*cr
,
508 struct control_msg_out
*cm
, int spaceleft
)
512 if (unlikely(spaceleft
< 6))
515 dst
= skb_put(skb
, 6);
519 put_u32(dst
+ 1, cm
->msg
.connect
.conn_id
, 1);
520 dst
[5] = enc_window(get_window(cm
->msg
.connect
.sconn
));
522 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
527 static int add_connect_success(struct sk_buff
*skb
, struct control_retrans
*cr
,
528 struct control_msg_out
*cm
, int spaceleft
)
532 if (unlikely(spaceleft
< 10))
535 dst
= skb_put(skb
, 10);
538 dst
[0] = KP_CONNECT_SUCCESS
;
539 put_u32(dst
+ 1, cm
->msg
.connect_success
.rcvd_conn_id
, 1);
540 put_u32(dst
+ 5, cm
->msg
.connect_success
.gen_conn_id
, 1);
541 dst
[9] = enc_window(get_window(cm
->msg
.connect_success
.rconn
));
543 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
548 static int add_reset_conn(struct sk_buff
*skb
, struct control_retrans
*cr
,
549 struct control_msg_out
*cm
, int spaceleft
)
553 if (unlikely(spaceleft
< 5))
556 dst
= skb_put(skb
, 5);
559 dst
[0] = KP_RESET_CONN
;
560 put_u32(dst
+ 1, cm
->msg
.reset
.conn_id
, 1);
562 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
567 static int add_conndata(struct sk_buff
*skb
, struct control_retrans
*cr
,
568 struct control_msg_out
*cm
, int spaceleft
,
569 struct control_msg_out
**split_conndata
, __u32
*sc_sendlen
)
573 int totallen
= cm
->msg
.conn_data
.datalen
+ 11;
574 int putlen
= min(totallen
, spaceleft
);
575 int dataputlen
= putlen
- 11;
577 BUG_ON(split_conndata
== 0);
578 BUG_ON(sc_sendlen
== 0);
580 if (dataputlen
< 1 || (spaceleft
< 25 && spaceleft
< totallen
))
583 dst
= skb_put(skb
, putlen
);
586 dst
[0] = KP_CONN_DATA
;
587 put_u32(dst
+ 1, cm
->msg
.conn_data
.conn_id
, 1);
588 put_u32(dst
+ 5, cm
->msg
.conn_data
.seqno
, 1);
589 put_u16(dst
+ 9, dataputlen
, 1);
591 memcpy(dst
+ 11, cm
->msg
.conn_data
.data
, dataputlen
);
593 if (cm
->msg
.conn_data
.datalen
== dataputlen
) {
594 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
596 *split_conndata
= cm
;
597 *sc_sendlen
= dataputlen
;
603 static int add_ping_conn(struct sk_buff
*skb
, struct control_retrans
*cr
,
604 struct control_msg_out
*cm
, int spaceleft
)
608 if (unlikely(spaceleft
< 5))
611 dst
= skb_put(skb
, 5);
614 dst
[0] = KP_PING_CONN
;
615 put_u32(dst
+ 1, cm
->msg
.ping_conn
.conn_id
, 1);
617 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
622 static int add_connid_unknown(struct sk_buff
*skb
, struct control_retrans
*cr
,
623 struct control_msg_out
*cm
, int spaceleft
)
627 if (unlikely(spaceleft
< 5))
630 dst
= skb_put(skb
, 5);
633 dst
[0] = KP_CONNID_UNKNOWN
;
634 put_u32(dst
+ 1, cm
->msg
.connid_unknown
.conn_id
, 1);
636 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
641 static int add_ping_all_conns(struct sk_buff
*skb
, struct control_retrans
*cr
,
642 struct control_msg_out
*cm
, int spaceleft
)
646 if (unlikely(spaceleft
< 1))
649 dst
= skb_put(skb
, 1);
652 dst
[0] = KP_PING_ALL_CONNS
;
654 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
659 static int add_set_max_cmsg_dly(struct sk_buff
*skb
, struct control_retrans
*cr
,
660 struct control_msg_out
*cm
, int spaceleft
)
664 if (unlikely(spaceleft
< 5))
667 dst
= skb_put(skb
, 5);
670 dst
[0] = KP_SET_MAX_CMSG_DELAY
;
671 put_u32(dst
+ 1, cm
->msg
.set_max_cmsg_delay
.delay
, 1);
673 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
678 static int add_credits(struct sk_buff
*skb
, struct control_retrans
*cr
,
679 struct control_msg_out
*cm
, int spaceleft
)
681 unsigned long iflags
;
684 if (unlikely(spaceleft
< 21))
687 dst
= skb_put(skb
, 21);
690 dst
[0] = KP_SET_CREDITS
;
692 spin_lock_irqsave( &(cm
->nb
->credits_lock
), iflags
);
694 refresh_credits_state(cm
->nb
);
696 put_u64(dst
+ 1, cm
->nb
->debits
, 1);
697 put_u32(dst
+ 9, cm
->nb
->debitrate_initial
+
698 cm
->nb
->debitrate_initial_adj
, 1);
699 put_u32(dst
+ 13, cm
->nb
->debitrate_earning
, 1);
700 put_u32(dst
+ 17, cm
->nb
->debitrate_spending
, 1);
702 spin_unlock_irqrestore( &(cm
->nb
->credits_lock
), iflags
);
704 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
709 static int add_message(struct sk_buff
*skb
, struct control_retrans
*cr
,
710 struct control_msg_out
*cm
, int spaceleft
,
711 struct control_msg_out
**split_conndata
, __u32
*sc_sendlen
)
713 BUG_ON(split_conndata
!= 0 && *split_conndata
!= 0);
714 BUG_ON(sc_sendlen
!= 0 && *sc_sendlen
!= 0);
718 return add_ack(skb
, cr
, cm
, spaceleft
);
719 case MSGTYPE_ACK_CONN
:
720 return add_ack_conn(skb
, cr
, cm
, spaceleft
);
721 case MSGTYPE_ACK_CONN_OOO
:
722 return add_ack_conn_ooo(skb
, cr
, cm
, spaceleft
);
724 return add_pong(skb
, cr
, cm
, spaceleft
);
725 case MSGTYPE_CONNECT
:
726 return add_connect(skb
, cr
, cm
, spaceleft
);
727 case MSGTYPE_CONNECT_SUCCESS
:
728 return add_connect_success(skb
, cr
, cm
, spaceleft
);
729 case MSGTYPE_RESET_CONN
:
730 return add_reset_conn(skb
, cr
, cm
, spaceleft
);
731 case MSGTYPE_CONNDATA
:
732 return add_conndata(skb
, cr
, cm
, spaceleft
, split_conndata
,
734 case MSGTYPE_PING_CONN
:
735 return add_ping_conn(skb
, cr
, cm
, spaceleft
);
736 case MSGTYPE_CONNID_UNKNOWN
:
737 return add_connid_unknown(skb
, cr
, cm
, spaceleft
);
738 case MSGTYPE_PING_ALL_CONNS
:
739 return add_ping_all_conns(skb
, cr
, cm
, spaceleft
);
740 case MSGTYPE_SET_MAX_CMSG_DELAY
:
741 return add_set_max_cmsg_dly(skb
, cr
, cm
, spaceleft
);
742 case MSGTYPE_SET_CREDITS
:
743 return add_credits(skb
, cr
, cm
, spaceleft
);
751 static __u32
recount_ping_conns(struct neighbor
*nb
)
754 struct list_head
*curr
= nb
->next_ping_conn
->target
.out
.nb_list
.next
;
755 while (curr
!= &(nb
->snd_conn_list
)) {
757 BUG_ON(cnt
> 1000000000);
762 static __u32
__send_messages_pc(struct neighbor
*nb
, struct sk_buff
*skb
,
763 struct control_retrans
*cr
, int spaceleft
)
766 mutex_lock(&(nb
->conn_list_lock
));
767 while (nb
->next_ping_conn
!= 0) {
770 struct list_head
*next
;
771 struct control_msg_out
*cm
;
774 rconn
= nb
->next_ping_conn
;
775 sconn
= rconn
->reversedir
;
777 BUG_ON(rconn
->targettype
!= TARGET_OUT
);
778 BUG_ON(sconn
->sourcetype
!= SOURCE_IN
);
780 if (unlikely(rconn
->target
.out
.conn_id
))
783 if (nb
->ping_conns_remaining
== 0) {
784 atomic_set(&(sconn
->source
.in
.pong_awaiting
), 1);
785 nb
->pong_conns_expected
++;
786 nb
->ping_conns_remaining
--;
787 if (unlikely(nb
->ping_conns_remaining
== 0))
788 nb
->ping_conns_remaining
=
789 recount_ping_conns(nb
);
791 if (likely(atomic_read(&(
792 sconn
->source
.in
.pong_awaiting
)) == 0))
794 nb
->ping_conns_remaining
--;
795 if (unlikely(nb
->ping_conns_retrans_remaining
== 0))
796 nb
->ping_conns_retrans_remaining
=
797 recount_ping_conns(nb
);
800 cm
= alloc_control_msg(nb
, ACM_PRIORITY_LOW
);
802 cm
->type
= MSGTYPE_PING_CONN
;
803 cm
->msg
.ping_conn
.conn_id
= rconn
->target
.out
.conn_id
;
804 rc
= add_message(skb
, cr
, cm
, spaceleft
- length
, 0, 0);
810 next
= rconn
->target
.out
.nb_list
.next
;
811 nb
->next_ping_conn
= container_of(next
, struct conn
,
813 if (next
== &(nb
->snd_conn_list
)) {
814 nb
->next_ping_conn
= 0;
815 nb
->ping_conns_remaining
= 0;
818 if (unlikely(length
!= 0)) {
819 nb
->ping_conn_completed
= jiffies
;
821 mutex_unlock(&(nb
->conn_list_lock
));
825 static __u32
__send_messages(struct neighbor
*nb
, struct sk_buff
*skb
,
826 struct control_retrans
*cr
, int spaceleft
, int urgentonly
,
827 struct control_msg_out
**split_conndata
, __u32
*sc_sendlen
)
830 while (!list_empty(&(nb
->ucontrol_msgs_out
)) || (!urgentonly
&&
831 !list_empty(&(nb
->control_msgs_out
)))) {
834 int urgent
= !list_empty(&(nb
->ucontrol_msgs_out
));
836 struct control_msg_out
*cm
;
839 cm
= container_of(nb
->ucontrol_msgs_out
.next
,
840 struct control_msg_out
, lh
);
842 cm
= container_of(nb
->control_msgs_out
.next
,
843 struct control_msg_out
, lh
);
847 nb
->ucmlength
-= cm
->length
;
849 nb
->cmlength
-= cm
->length
;
850 mutex_unlock(&(nb
->cmsg_lock
));
851 rc
= add_message(skb
, cr
, cm
, spaceleft
- length
,
852 split_conndata
, sc_sendlen
);
853 mutex_lock(&(nb
->cmsg_lock
));
857 list_add(&(cm
->lh
), &(nb
->ucontrol_msgs_out
));
858 nb
->ucmlength
+= cm
->length
;
860 list_add(&(cm
->lh
), &(nb
->control_msgs_out
));
861 nb
->cmlength
+= cm
->length
;
872 static int msgtype_present(struct neighbor
*nb
, __u8 type
)
874 struct list_head
*curr
;
876 curr
= nb
->control_msgs_out
.next
;
877 while (curr
!= &(nb
->control_msgs_out
)) {
878 struct control_msg_out
*cm
= container_of(curr
,
879 struct control_msg_out
, lh
);
881 if (cm
->type
== MSGTYPE_PING_ALL_CONNS
)
890 static int ping_all_conns_needed(struct neighbor
*nb
)
892 if (likely(nb
->ping_all_conns
== 0))
895 if (msgtype_present(nb
, MSGTYPE_PING_ALL_CONNS
))
901 static int __send_messages_cred(struct neighbor
*nb
, struct sk_buff
*skb
,
902 struct control_retrans
*cr
, int spaceleft
)
904 struct control_msg_out
*cm
;
907 cm
= alloc_control_msg(nb
, ACM_PRIORITY_MED
);
909 if (unlikely(cm
== 0))
912 cm
->type
= MSGTYPE_SET_CREDITS
;
915 rc
= add_message(skb
, cr
, cm
, spaceleft
, 0, 0);
917 nb
->send_credits
= 0;
921 static int __send_messages_smcd(struct neighbor
*nb
, struct sk_buff
*skb
,
922 struct control_retrans
*cr
, int spaceleft
)
924 struct control_msg_out
*cm
;
927 cm
= alloc_control_msg(nb
, ACM_PRIORITY_MED
);
929 if (unlikely(cm
== 0))
932 cm
->type
= MSGTYPE_SET_MAX_CMSG_DELAY
;
933 cm
->msg
.set_max_cmsg_delay
.delay
= CMSG_INTERVAL_MS
* 10;
936 rc
= add_message(skb
, cr
, cm
, spaceleft
, 0, 0);
938 nb
->max_cmsg_delay_sent
= 1;
942 static int __send_messages_pac(struct neighbor
*nb
, struct sk_buff
*skb
,
943 struct control_retrans
*cr
, int spaceleft
)
945 struct control_msg_out
*cm
;
948 cm
= alloc_control_msg(nb
, ACM_PRIORITY_MED
);
950 if (unlikely(cm
== 0))
953 cm
->type
= MSGTYPE_PING_ALL_CONNS
;
956 rc
= add_message(skb
, cr
, cm
, spaceleft
, 0, 0);
958 nb
->ping_all_conns
= 0;
963 static int _send_messages(struct neighbor
*nb
, struct sk_buff
*skb
, int ping
,
964 struct control_retrans
*cr
, int spaceleft
, int urgentonly
)
968 __u32 pingcookie
= 0;
969 struct control_msg_out
*split_conndata
= 0;
970 __u32 sc_sendlen
= 0;
972 mutex_lock(&(nb
->cmsg_lock
));
976 pingcookie
= add_ping_req(nb
);
977 rc
= add_ping(skb
, pingcookie
, spaceleft
- length
);
982 if (likely(urgentonly
== 0) && unlikely(ping_all_conns_needed(nb
) != 0))
983 length
+= __send_messages_pac(nb
, skb
, cr
, spaceleft
- length
);
985 if (likely(urgentonly
== 0) && unlikely(nb
->max_cmsg_delay_sent
== 0))
986 length
+= __send_messages_smcd(nb
, skb
, cr
, spaceleft
- length
);
988 if (likely(urgentonly
== 0) && unlikely(nb
->send_credits
!= 0) &&
989 msgtype_present(nb
, MSGTYPE_SET_CREDITS
) == 0)
990 length
+= __send_messages_cred(nb
, skb
, cr
, spaceleft
- length
);
993 length
+= __send_messages(nb
, skb
, cr
, spaceleft
- length
, urgentonly
,
994 &split_conndata
, &sc_sendlen
);
996 if (likely(urgentonly
== 0))
997 length
+= __send_messages_pc(nb
, skb
, cr
, spaceleft
- length
);
999 mutex_unlock(&(nb
->cmsg_lock
));
1001 if (unlikely(length
> spaceleft
))
1002 printk(KERN_ERR
"error cor/kpacket_gen: length > spaceleft!?");
1004 padding(skb
, spaceleft
- length
);
1006 rc
= dev_queue_xmit(skb
);
1009 unadd_ping_req(nb
, pingcookie
);
1011 while (list_empty(&(cr
->msgs
)) == 0) {
1012 struct control_msg_out
*cm
= container_of(cr
->msgs
.prev
,
1013 struct control_msg_out
, lh
);
1014 list_del(&(cm
->lh
));
1015 add_control_msg(cm
, 1);
1018 if (split_conndata
!= 0) {
1019 add_control_msg(split_conndata
, 1);
1022 kref_put(&(cr
->ref
), free_control_retrans
);
1024 struct list_head
*curr
= cr
->msgs
.next
;
1026 while(curr
!= &(cr
->msgs
)) {
1027 struct control_msg_out
*cm
= container_of(curr
,
1028 struct control_msg_out
, lh
);
1032 if (cm
->type
== MSGTYPE_CONNDATA
) {
1033 list_del(&(cm
->lh
));
1034 kfree(cm
->msg
.conn_data
.data_orig
);
1035 free_control_msg(cm
);
1039 if (split_conndata
!= 0) {
1040 BUG_ON(sc_sendlen
== 0);
1041 BUG_ON(sc_sendlen
>=
1042 split_conndata
->msg
.conn_data
.datalen
);
1044 split_conndata
->msg
.conn_data
.data
+= sc_sendlen
;
1045 split_conndata
->msg
.conn_data
.datalen
-= sc_sendlen
;
1047 send_conndata(split_conndata
,
1048 split_conndata
->msg
.conn_data
.conn_id
,
1049 split_conndata
->msg
.conn_data
.seqno
,
1050 split_conndata
->msg
.conn_data
.data_orig
,
1051 split_conndata
->msg
.conn_data
.data
,
1052 split_conndata
->msg
.conn_data
.datalen
);
1056 if (list_empty(&(cr
->msgs
)))
1057 kref_put(&(cr
->ref
), free_control_retrans
);
1059 schedule_retransmit(cr
, nb
);
1065 static __u32
get_total_messages_length(struct neighbor
*nb
, int ping
,
1068 __u32 length
= nb
->ucmlength
;
1069 if (likely(nb
->send_credits
== 0) && unlikely(debit_adj_needed(nb
)))
1070 nb
->send_credits
= 1;
1072 if (likely(urgentonly
== 0)) {
1073 length
+= nb
->cmlength
+ nb
->ping_conns_remaining
* 5;
1074 if (likely(nb
->ping_conns_remaining
== 0)) {
1075 if (likely(nb
->ping_conns_retrans_remaining
== 0) &&
1076 unlikely(nb
->pong_conns_expected
!=0) &&
1077 time_before(nb
->ping_conn_completed
,
1078 jiffies
+ msecs_to_jiffies(
1079 PING_ALL_CONNS_TIMEOUT
) +
1080 usecs_to_jiffies(((__u32
) atomic_read(&(
1081 nb
->latency
))) * 2 + ((__u32
)
1082 atomic_read(&(nb
->max_remote_cmsg_delay
)
1084 nb
->ping_conns_retrans_remaining
=
1085 nb
->pong_conns_expected
;
1087 if (unlikely(nb
->ping_conns_retrans_remaining
>
1088 nb
->pong_conns_expected
))
1089 nb
->ping_conns_retrans_remaining
=
1090 nb
->pong_conns_expected
;
1092 length
+= nb
->ping_conns_retrans_remaining
* 5;
1094 if (unlikely(ping_all_conns_needed(nb
) != 0))
1096 if (unlikely(nb
->max_cmsg_delay_sent
== 0))
1098 if (unlikely(nb
->send_credits
== 2) &&
1099 msgtype_present(nb
, MSGTYPE_SET_CREDITS
) == 0)
1102 if (ping
== 2 || (length
> 0 && ping
!= 0))
1104 if (likely(urgentonly
== 0) && length
> 0 &&
1105 unlikely(nb
->send_credits
== 1) &&
1106 msgtype_present(nb
, MSGTYPE_SET_CREDITS
) == 0)
1112 static int send_messages(struct neighbor
*nb
, int allmsgs
, int resume
)
1116 int targetmss
= mss(nb
);
1118 int nbstate
= get_neigh_state(nb
);
1119 int urgentonly
= (nbstate
!= NEIGHBOR_STATE_ACTIVE
);
1121 check_credit_state(nb
);
1123 mutex_lock(&(nb
->cmsg_lock
));
1126 allmsgs
= nb
->kp_allmsgs
;
1128 ping
= time_to_send_ping(nb
);
1134 struct sk_buff
*skb
;
1135 struct control_retrans
*cr
;
1137 BUG_ON(list_empty(&(nb
->control_msgs_out
)) &&
1138 (nb
->cmlength
!= 0));
1139 BUG_ON((list_empty(&(nb
->control_msgs_out
)) == 0) &&
1140 (nb
->cmlength
== 0));
1141 BUG_ON(list_empty(&(nb
->ucontrol_msgs_out
)) &&
1142 (nb
->ucmlength
!= 0));
1143 BUG_ON((list_empty(&(nb
->ucontrol_msgs_out
)) == 0) &&
1144 (nb
->ucmlength
== 0));
1145 BUG_ON(nb
->cmlength
< 0);
1146 BUG_ON(nb
->ucmlength
< 0);
1148 length
= get_total_messages_length(nb
, ping
, urgentonly
);
1153 if (length
< targetmss
&& allmsgs
== 0)
1156 seqno
= atomic_add_return(1, &(nb
->kpacket_seqno
));
1158 if (length
> targetmss
)
1161 mutex_unlock(&(nb
->cmsg_lock
));
1162 skb
= create_packet(nb
, length
, GFP_KERNEL
, 0, seqno
);
1163 if (unlikely(skb
== 0)) {
1164 printk(KERN_ERR
"cor: send_messages: cannot allocate "
1165 "skb (out of memory?)");
1169 cr
= kmem_cache_alloc(controlretrans_slab
, GFP_KERNEL
);
1170 if (unlikely(cr
== 0)) {
1172 printk(KERN_ERR
"cor: send_messages: cannot allocate "
1173 "control_retrans (out of memory?)");
1176 memset(cr
, 0, sizeof(struct control_retrans
));
1177 kref_init(&(cr
->ref
));
1180 INIT_LIST_HEAD(&(cr
->msgs
));
1182 rc
= _send_messages(nb
, skb
, ping
, cr
, length
, urgentonly
);
1185 mutex_lock(&(nb
->cmsg_lock
));
1193 mutex_lock(&(nb
->cmsg_lock
));
1198 nb
->kp_allmsgs
= nb
->kp_allmsgs
|| allmsgs
;
1199 qos_enqueue_kpacket(nb
);
1201 } else if (allmsgs
) {
1205 mutex_unlock(&(nb
->cmsg_lock
));
1208 schedule_controlmsg_timerfunc(nb
);
1213 int resume_send_messages(struct neighbor
*nb
)
1215 return send_messages(nb
, 0, 1);
1218 static void controlmsg_timerfunc(struct work_struct
*work
)
1220 struct neighbor
*nb
= container_of(to_delayed_work(work
),
1221 struct neighbor
, cmsg_timer
);
1222 __u64 jiffies
= get_jiffies_64();
1224 mutex_lock(&(nb
->cmsg_lock
));
1226 if (nb
->timeout
> jiffies
) {
1227 INIT_DELAYED_WORK(&(nb
->cmsg_timer
), controlmsg_timerfunc
);
1228 schedule_delayed_work(&(nb
->cmsg_timer
), nb
->timeout
- jiffies
);
1229 mutex_unlock(&(nb
->cmsg_lock
));
1233 mutex_unlock(&(nb
->cmsg_lock
));
1235 send_messages(nb
, 1, 0);
1236 kref_put(&(nb
->ref
), neighbor_free
);
1239 void schedule_controlmsg_timerfunc(struct neighbor
*nb
)
1241 __u64 jiffies
= get_jiffies_64();
1244 int state
= get_neigh_state(nb
);
1246 if (unlikely(state
== NEIGHBOR_STATE_KILLED
))
1249 mutex_lock(&(nb
->cmsg_lock
));
1250 nb
->timeout
+= msecs_to_jiffies(CMSG_INTERVAL_MS
);
1252 delay
= nb
->timeout
- jiffies
;
1255 nb
->timeout
= jiffies
;
1258 INIT_DELAYED_WORK(&(nb
->cmsg_timer
), controlmsg_timerfunc
);
1259 schedule_delayed_work(&(nb
->cmsg_timer
), delay
);
1260 mutex_unlock(&(nb
->cmsg_lock
));
1261 kref_get(&(nb
->ref
));
1264 static void free_oldest_ucm(struct neighbor
*nb
)
1266 struct control_msg_out
*cm
= container_of(nb
->ucontrol_msgs_out
.next
,
1267 struct control_msg_out
, lh
);
1269 BUG_ON(list_empty(&(nb
->ucontrol_msgs_out
)));
1270 BUG_ON(isurgent(cm
) == 0);
1272 list_del(&(cm
->lh
));
1273 nb
->ucmlength
-= cm
->length
;
1274 atomic_dec(&(nb
->ucmcnt
));
1275 free_control_msg(cm
);
1278 static void add_control_msg(struct control_msg_out
*cm
, int retrans
)
1282 BUG_ON(cm
->nb
== 0);
1284 nbstate
= get_neigh_state(cm
->nb
);
1287 BUG_ON(cm
->lh
.next
!= LIST_POISON1
|| cm
->lh
.prev
!= LIST_POISON2
);
1289 mutex_lock(&(cm
->nb
->cmsg_lock
));
1294 msgs
= atomic_inc_return(&(cm
->nb
->ucmcnt
));
1297 if (unlikely(retrans
)) {
1298 if (msgs
> MAX_URGENT_CMSGS_PER_NEIGH
) {
1299 atomic_dec(&(cm
->nb
->ucmcnt
));
1300 free_control_msg(cm
);
1304 cm
->nb
->ucmlength
+= cm
->length
;
1305 list_add(&(cm
->lh
), &(cm
->nb
->ucontrol_msgs_out
));
1307 if (msgs
> MAX_URGENT_CMSGS_PER_NEIGH
) {
1308 free_oldest_ucm(cm
->nb
);
1311 cm
->nb
->ucmlength
+= cm
->length
;
1312 list_add_tail(&(cm
->lh
), &(cm
->nb
->ucontrol_msgs_out
));
1315 cm
->nb
->cmlength
+= cm
->length
;
1316 list_add_tail(&(cm
->lh
), &(cm
->nb
->control_msgs_out
));
1319 if (unlikely((nbstate
== NEIGHBOR_STATE_ACTIVE
? cm
->nb
->cmlength
: 0)+
1320 cm
->nb
->ucmlength
>= mss(cm
->nb
)))
1321 send_messages(cm
->nb
, 0, 0);
1324 mutex_unlock(&(cm
->nb
->cmsg_lock
));
1327 void send_pong(struct neighbor
*nb
, __u32 cookie
)
1329 struct control_msg_out
*cm
= _alloc_control_msg(nb
, 0, 1);
1331 if (unlikely(cm
== 0))
1335 cm
->type
= MSGTYPE_PONG
;
1336 cm
->msg
.pong
.cookie
= cookie
;
1337 cm
->msg
.pong
.time_enqueued
= jiffies
;
1339 add_control_msg(cm
, 0);
1342 void send_reset_conn(struct control_msg_out
*cm
, __u32 conn_id
)
1344 cm
->type
= MSGTYPE_RESET_CONN
;
1345 cm
->msg
.reset
.conn_id
= conn_id
;
1347 add_control_msg(cm
, 0);
1350 void send_ack(struct neighbor
*nb
, __u32 seqno
)
1352 struct control_msg_out
*cm
= _alloc_control_msg(nb
, 0, 1);
1354 if (unlikely(cm
== 0))
1358 cm
->type
= MSGTYPE_ACK
;
1359 cm
->msg
.ack
.seqno
= seqno
;
1361 add_control_msg(cm
, 0);
1364 void send_ack_conn(struct control_msg_out
*cm
, struct conn
*rconn
,
1365 __u32 conn_id
, __u32 seqno
)
1367 cm
->type
= MSGTYPE_ACK_CONN
;
1368 kref_get(&(rconn
->ref
));
1369 cm
->msg
.ack_conn
.rconn
= rconn
;
1370 cm
->msg
.ack_conn
.conn_id
= conn_id
;
1371 cm
->msg
.ack_conn
.seqno
= seqno
;
1374 add_control_msg(cm
, 0);
1377 void send_ack_conn_ooo(struct control_msg_out
*cm
, struct conn
*rconn
,
1378 __u32 conn_id
, __u32 seqno
, __u32 seqno_ooo
, __u32 length
)
1380 cm
->type
= MSGTYPE_ACK_CONN_OOO
;
1381 kref_get(&(rconn
->ref
));
1382 cm
->msg
.ack_conn_ooo
.rconn
= rconn
;
1383 cm
->msg
.ack_conn_ooo
.conn_id
= conn_id
;
1384 cm
->msg
.ack_conn_ooo
.seqno
= seqno
;
1385 cm
->msg
.ack_conn_ooo
.seqno_ooo
= seqno_ooo
;
1386 cm
->msg
.ack_conn_ooo
.length
= length
;
1388 add_control_msg(cm
, 0);
1391 void send_connect_success(struct control_msg_out
*cm
, __u32 rcvd_conn_id
,
1392 __u32 gen_conn_id
, struct conn
*rconn
)
1394 cm
->type
= MSGTYPE_CONNECT_SUCCESS
;
1395 cm
->msg
.connect_success
.rcvd_conn_id
= rcvd_conn_id
;
1396 cm
->msg
.connect_success
.gen_conn_id
= gen_conn_id
;
1397 kref_get(&(rconn
->ref
));
1398 cm
->msg
.connect_success
.rconn
= rconn
;
1400 add_control_msg(cm
, 0);
1403 void send_connect_nb(struct control_msg_out
*cm
, __u32 conn_id
,
1406 cm
->type
= MSGTYPE_CONNECT
;
1407 cm
->msg
.connect
.conn_id
= conn_id
;
1408 kref_get(&(sconn
->ref
));
1409 cm
->msg
.connect
.sconn
= sconn
;
1411 add_control_msg(cm
, 0);
1414 void send_conndata(struct control_msg_out
*cm
, __u32 conn_id
, __u32 seqno
,
1415 char *data_orig
, char *data
, __u32 datalen
)
1417 cm
->type
= MSGTYPE_CONNDATA
;
1418 cm
->msg
.conn_data
.conn_id
= conn_id
;
1419 cm
->msg
.conn_data
.seqno
= seqno
;
1420 cm
->msg
.conn_data
.data_orig
= data_orig
;
1421 cm
->msg
.conn_data
.data
= data
;
1422 cm
->msg
.conn_data
.datalen
= datalen
;
1423 cm
->length
= 11 + datalen
;
1424 add_control_msg(cm
, 0);
1427 void send_ping_conn(struct control_msg_out
*cm
, __u32 conn_id
)
1429 cm
->type
= MSGTYPE_PING_CONN
;
1430 cm
->msg
.ping_conn
.conn_id
= conn_id
;
1432 add_control_msg(cm
, 0);
1436 void send_connid_unknown(struct control_msg_out
*cm
, __u32 conn_id
)
1438 cm
->type
= MSGTYPE_CONNID_UNKNOWN
;
1439 cm
->msg
.connid_unknown
.conn_id
= conn_id
;
1441 add_control_msg(cm
, 0);
1444 void send_ping_all_conns(struct neighbor
*nb
)
1446 mutex_lock(&(nb
->cmsg_lock
));
1447 nb
->ping_all_conns
= 1;
1448 mutex_unlock(&(nb
->cmsg_lock
));
1451 void send_credits(struct neighbor
*nb
)
1453 mutex_lock(&(nb
->cmsg_lock
));
1454 nb
->send_credits
= 2;
1455 mutex_unlock(&(nb
->cmsg_lock
));
1458 static int matches_connretrans(void *htentry
, void *searcheditem
)
1460 struct control_retrans
*cr
= (struct control_retrans
*) htentry
;
1461 struct retransmit_matchparam
*rm
= (struct retransmit_matchparam
*)
1464 return rm
->nb
== cr
->nb
&& rm
->seqno
== cr
->seqno
;
1467 void __init
cor_kgen_init(void)
1469 controlmsg_slab
= kmem_cache_create("cor_controlmsg",
1470 sizeof(struct control_msg_out
), 8, 0, 0);
1471 controlretrans_slab
= kmem_cache_create("cor_controlretransmsg",
1472 sizeof(struct control_retrans
), 8, 0, 0);
1473 htable_init(&retransmits
, matches_connretrans
,
1474 offsetof(struct control_retrans
, htab_entry
),
1475 offsetof(struct control_retrans
, ref
));