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 */
111 struct control_retrans
{
117 unsigned long timeout
;
119 struct list_head msgs
;
121 struct htab_entry htab_entry
;
122 struct list_head timeout_list
;
125 struct kmem_cache
*controlmsg_slab
;
126 struct kmem_cache
*controlretrans_slab
;
128 static struct htable retransmits
;
130 atomic_t cmcnt
= ATOMIC_INIT(0);
132 static void add_control_msg(struct control_msg_out
*msg
, int retrans
);
134 static inline int isurgent(struct control_msg_out
*cm
)
136 if (unlikely(cm
->type
== MSGTYPE_PONG
|| cm
->type
== MSGTYPE_ACK
))
141 static struct control_msg_out
*__alloc_control_msg(void)
143 struct control_msg_out
*cm
= kmem_cache_alloc(controlmsg_slab
,
145 if (unlikely(cm
== 0))
147 cm
->lh
.next
= LIST_POISON1
;
148 cm
->lh
.prev
= LIST_POISON2
;
152 static int calc_limit(int limit
, int priority
)
154 if (priority
== ACM_PRIORITY_LOW
)
156 else if (priority
== ACM_PRIORITY_MED
)
157 return (limit
*3 + 3)/4;
158 else if (priority
== ACM_PRIORITY_HIGH
)
164 static struct control_msg_out
*_alloc_control_msg(struct neighbor
*nb
,
165 int priority
, int urgent
)
167 struct control_msg_out
*cm
= 0;
172 long packets1
= atomic_inc_return(&(nb
->cmcnt
));
173 long packets2
= atomic_inc_return(&(cmcnt
));
175 BUG_ON(packets1
<= 0);
176 BUG_ON(packets2
<= 0);
178 if (packets1
<= calc_limit(GUARANTEED_CMSGS_PER_NEIGH
,
182 if (unlikely(unlikely(packets2
> calc_limit(MAX_CMSGS_PER_NEIGH
,
183 priority
)) || unlikely(packets1
> (
184 calc_limit(MAX_CMSGS_PER_NEIGH
, priority
) *
185 (MAX_CMSGS
- packets2
) / MAX_CMSGS
))))
190 cm
= __alloc_control_msg();
191 if (unlikely(cm
== 0))
198 atomic_dec(&(nb
->cmcnt
));
199 atomic_dec(&(cmcnt
));
205 struct control_msg_out
*alloc_control_msg(struct neighbor
*nb
, int priority
)
207 return _alloc_control_msg(nb
, priority
, 0);
210 void free_control_msg(struct control_msg_out
*cm
)
212 if (isurgent(cm
) == 0) {
213 atomic_dec(&(cm
->nb
->cmcnt
));
214 atomic_dec(&(cmcnt
));
217 if (cm
->type
== MSGTYPE_ACK_CONN
) {
218 BUG_ON(cm
->msg
.ack_conn
.rconn
== 0);
219 kref_put(&(cm
->msg
.ack_conn
.rconn
->ref
), free_conn
);
220 cm
->msg
.ack_conn
.rconn
= 0;
221 } else if (cm
->type
== MSGTYPE_ACK_CONN_OOO
) {
222 BUG_ON(cm
->msg
.ack_conn_ooo
.rconn
== 0);
223 kref_put(&(cm
->msg
.ack_conn_ooo
.rconn
->ref
), free_conn
);
224 cm
->msg
.ack_conn_ooo
.rconn
= 0;
225 } else if (cm
->type
== MSGTYPE_CONNECT
) {
226 BUG_ON(cm
->msg
.connect
.sconn
== 0);
227 kref_put(&(cm
->msg
.connect
.sconn
->ref
), free_conn
);
228 cm
->msg
.connect
.sconn
= 0;
229 } else if (cm
->type
== MSGTYPE_CONNECT_SUCCESS
) {
230 BUG_ON(cm
->msg
.connect_success
.rconn
== 0);
231 kref_put(&(cm
->msg
.connect_success
.rconn
->ref
), free_conn
);
232 cm
->msg
.connect_success
.rconn
= 0;
235 kmem_cache_free(controlmsg_slab
, cm
);
238 static void free_control_retrans(struct kref
*ref
)
240 struct control_retrans
*cr
= container_of(ref
, struct control_retrans
,
243 while (list_empty(&(cr
->msgs
)) == 0) {
244 struct control_msg_out
*cm
= container_of(cr
->msgs
.next
,
245 struct control_msg_out
, lh
);
247 free_control_msg(cm
);
250 kmem_cache_free(controlretrans_slab
, cr
);
253 struct retransmit_matchparam
{
258 static __u32
rm_to_key(struct retransmit_matchparam
*rm
)
260 return ((__u32
)((long) rm
->nb
)) ^ rm
->seqno
;
263 static void set_retrans_timeout(struct control_retrans
*cr
, struct neighbor
*nb
)
265 cr
->timeout
= jiffies
+ usecs_to_jiffies(100000 +
266 ((__u32
) atomic_read(&(nb
->latency
))) * 2 +
267 ((__u32
) atomic_read(&(nb
->max_remote_cmsg_delay
))));
270 void retransmit_timerfunc(struct work_struct
*work
)
272 unsigned long iflags
;
274 struct neighbor
*nb
= container_of(to_delayed_work(work
),
275 struct neighbor
, retrans_timer
);
280 spin_lock_irqsave( &(nb
->state_lock
), iflags
);
282 spin_unlock_irqrestore( &(nb
->state_lock
), iflags
);
285 struct control_retrans
*cr
= 0;
286 struct retransmit_matchparam rm
;
288 spin_lock_irqsave( &(nb
->retrans_lock
), iflags
);
290 if (list_empty(&(nb
->retrans_list
))) {
291 nb
->retrans_timer_running
= 0;
296 cr
= container_of(nb
->retrans_list
.next
,
297 struct control_retrans
, timeout_list
);
299 BUG_ON(cr
->nb
!= nb
);
301 rm
.seqno
= cr
->seqno
;
304 list_del(&(cr
->timeout_list
));
306 if (unlikely(nbstate
== NEIGHBOR_STATE_KILLED
)) {
307 spin_unlock_irqrestore( &(nb
->retrans_lock
), iflags
);
309 htable_delete(&retransmits
, rm_to_key(&rm
), &rm
,
310 free_control_retrans
);
311 kref_put(&(cr
->ref
), free_control_retrans
);
315 if (time_after(cr
->timeout
, jiffies
)) {
316 list_add(&(cr
->timeout_list
), &(nb
->retrans_list
));
317 schedule_delayed_work(&(nb
->retrans_timer
),
318 cr
->timeout
- jiffies
);
322 if (unlikely(htable_delete(&retransmits
, rm_to_key(&rm
), &rm
,
323 free_control_retrans
)))
326 spin_unlock_irqrestore( &(nb
->retrans_lock
), iflags
);
328 while (list_empty(&(cr
->msgs
)) == 0) {
329 struct control_msg_out
*cm
= container_of(cr
->msgs
.next
,
330 struct control_msg_out
, lh
);
332 add_control_msg(cm
, 1);
335 kref_put(&(cr
->ref
), free_control_retrans
);
338 spin_unlock_irqrestore( &(nb
->retrans_lock
), iflags
);
341 kref_put(&(nb
->ref
), neighbor_free
);
344 static void schedule_retransmit(struct control_retrans
*cr
, struct neighbor
*nb
)
346 unsigned long iflags
;
348 struct retransmit_matchparam rm
;
351 rm
.seqno
= cr
->seqno
;
354 set_retrans_timeout(cr
, nb
);
356 spin_lock_irqsave( &(nb
->retrans_lock
), iflags
);
357 htable_insert(&retransmits
, (char *) cr
, rm_to_key(&rm
));
358 first
= list_empty(&(nb
->retrans_list
));
359 list_add_tail(&(cr
->timeout_list
), &(nb
->retrans_list
));
361 if (first
&& nb
->retrans_timer_running
== 0) {
362 schedule_delayed_work(&(nb
->retrans_timer
),
363 cr
->timeout
- jiffies
);
364 nb
->retrans_timer_running
= 1;
365 kref_get(&(nb
->ref
));
368 spin_unlock_irqrestore( &(nb
->retrans_lock
), iflags
);
371 void kern_ack_rcvd(struct neighbor
*nb
, __u32 seqno
)
373 unsigned long iflags
;
375 struct control_retrans
*cr
= 0;
376 struct retransmit_matchparam rm
;
381 spin_lock_irqsave( &(nb
->retrans_lock
), iflags
);
383 cr
= (struct control_retrans
*) htable_get(&retransmits
, rm_to_key(&rm
),
387 printk(KERN_ERR
"bogus/duplicate ack received");
391 if (unlikely(htable_delete(&retransmits
, rm_to_key(&rm
), &rm
,
392 free_control_retrans
)))
395 BUG_ON(cr
->nb
!= nb
);
397 list_del(&(cr
->timeout_list
));
400 spin_unlock_irqrestore( &(nb
->retrans_lock
), iflags
);
403 static void padding(struct sk_buff
*skb
, int length
)
408 dst
= skb_put(skb
, length
);
410 memset(dst
, KP_PADDING
, length
);
413 static int add_ack(struct sk_buff
*skb
, struct control_retrans
*cr
,
414 struct control_msg_out
*cm
, int spaceleft
)
418 if (unlikely(spaceleft
< 5))
421 dst
= skb_put(skb
, 5);
425 put_u32(dst
+ 1, cm
->msg
.ack
.seqno
, 1);
427 atomic_dec(&(cm
->nb
->ucmcnt
));
428 free_control_msg(cm
);
433 static int add_ack_conn(struct sk_buff
*skb
, struct control_retrans
*cr
,
434 struct control_msg_out
*cm
, int spaceleft
)
438 if (unlikely(spaceleft
< 10))
441 dst
= skb_put(skb
, 10);
444 dst
[0] = KP_ACK_CONN
;
445 put_u32(dst
+ 1, cm
->msg
.ack_conn
.conn_id
, 1);
446 put_u32(dst
+ 5, cm
->msg
.ack_conn
.seqno
, 1);
447 BUG_ON(cm
->msg
.ack_conn
.rconn
== 0);
448 dst
[9] = enc_window(get_window(cm
->msg
.ack_conn
.rconn
));
450 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
455 static int add_ack_conn_ooo(struct sk_buff
*skb
, struct control_retrans
*cr
,
456 struct control_msg_out
*cm
, int spaceleft
)
460 if (unlikely(spaceleft
< 18))
463 dst
= skb_put(skb
, 18);
466 dst
[0] = KP_ACK_CONN_OOO
;
467 put_u32(dst
+ 1, cm
->msg
.ack_conn_ooo
.conn_id
, 1);
468 put_u32(dst
+ 5, cm
->msg
.ack_conn_ooo
.seqno
, 1);
469 BUG_ON(cm
->msg
.ack_conn_ooo
.rconn
== 0);
470 dst
[9] = enc_window(get_window(cm
->msg
.ack_conn_ooo
.rconn
));
471 put_u32(dst
+ 10, cm
->msg
.ack_conn_ooo
.seqno_ooo
, 1);
472 put_u32(dst
+ 14, cm
->msg
.ack_conn_ooo
.length
, 1);
474 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
479 static int add_ping(struct sk_buff
*skb
, __u32 cookie
,
484 if (unlikely(spaceleft
< 5))
487 dst
= skb_put(skb
, 5);
491 put_u32(dst
+ 1, cookie
, 0);
496 static int add_pong(struct sk_buff
*skb
, struct control_retrans
*cr
,
497 struct control_msg_out
*cm
, int spaceleft
)
501 if (unlikely(spaceleft
< 9))
504 dst
= skb_put(skb
, 9);
508 put_u32(dst
+ 1, cm
->msg
.pong
.cookie
, 0);
509 put_u32(dst
+ 5, 1000 * jiffies_to_msecs(jiffies
-
510 cm
->msg
.pong
.time_enqueued
), 1);
513 atomic_dec(&(cm
->nb
->ucmcnt
));
514 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
519 static int add_connect(struct sk_buff
*skb
, struct control_retrans
*cr
,
520 struct control_msg_out
*cm
, int spaceleft
)
524 if (unlikely(spaceleft
< 10))
527 dst
= skb_put(skb
, 10);
531 put_u32(dst
+ 1, cm
->msg
.connect
.conn_id
, 1);
532 put_u32(dst
+ 5, cm
->msg
.connect
.init_seqno
, 1);
533 BUG_ON(cm
->msg
.connect
.sconn
== 0);
534 dst
[9] = enc_window(get_window(cm
->msg
.connect
.sconn
));
536 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
541 static int add_connect_success(struct sk_buff
*skb
, struct control_retrans
*cr
,
542 struct control_msg_out
*cm
, int spaceleft
)
546 if (unlikely(spaceleft
< 14))
549 dst
= skb_put(skb
, 14);
552 dst
[0] = KP_CONNECT_SUCCESS
;
553 put_u32(dst
+ 1, cm
->msg
.connect_success
.rcvd_conn_id
, 1);
554 put_u32(dst
+ 5, cm
->msg
.connect_success
.gen_conn_id
, 1);
555 put_u32(dst
+ 9, cm
->msg
.connect_success
.init_seqno
, 1);
556 BUG_ON(cm
->msg
.connect_success
.rconn
== 0);
557 dst
[13] = enc_window(get_window(cm
->msg
.connect_success
.rconn
));
559 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
564 static int add_reset_conn(struct sk_buff
*skb
, struct control_retrans
*cr
,
565 struct control_msg_out
*cm
, int spaceleft
)
569 if (unlikely(spaceleft
< 5))
572 dst
= skb_put(skb
, 5);
575 dst
[0] = KP_RESET_CONN
;
576 put_u32(dst
+ 1, cm
->msg
.reset
.conn_id
, 1);
578 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
583 static int add_conndata(struct sk_buff
*skb
, struct control_retrans
*cr
,
584 struct control_msg_out
*cm
, int spaceleft
,
585 struct control_msg_out
**split_conndata
, __u32
*sc_sendlen
)
589 int totallen
= cm
->msg
.conn_data
.datalen
+ 11;
590 int putlen
= min(totallen
, spaceleft
);
591 int dataputlen
= putlen
- 11;
593 BUG_ON(split_conndata
== 0);
594 BUG_ON(sc_sendlen
== 0);
596 if (dataputlen
< 1 || (spaceleft
< 25 && spaceleft
< totallen
))
599 dst
= skb_put(skb
, putlen
);
602 dst
[0] = KP_CONN_DATA
;
603 put_u32(dst
+ 1, cm
->msg
.conn_data
.conn_id
, 1);
604 put_u32(dst
+ 5, cm
->msg
.conn_data
.seqno
, 1);
605 put_u16(dst
+ 9, dataputlen
, 1);
607 memcpy(dst
+ 11, cm
->msg
.conn_data
.data
, dataputlen
);
609 if (cm
->msg
.conn_data
.datalen
== dataputlen
) {
610 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
612 *split_conndata
= cm
;
613 *sc_sendlen
= dataputlen
;
619 static int add_ping_conn(struct sk_buff
*skb
, struct control_retrans
*cr
,
620 struct control_msg_out
*cm
, int spaceleft
)
624 if (unlikely(spaceleft
< 5))
627 dst
= skb_put(skb
, 5);
630 dst
[0] = KP_PING_CONN
;
631 put_u32(dst
+ 1, cm
->msg
.ping_conn
.conn_id
, 1);
633 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
638 static int add_connid_unknown(struct sk_buff
*skb
, struct control_retrans
*cr
,
639 struct control_msg_out
*cm
, int spaceleft
)
643 if (unlikely(spaceleft
< 5))
646 dst
= skb_put(skb
, 5);
649 dst
[0] = KP_CONNID_UNKNOWN
;
650 put_u32(dst
+ 1, cm
->msg
.connid_unknown
.conn_id
, 1);
652 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
657 static int add_ping_all_conns(struct sk_buff
*skb
, struct control_retrans
*cr
,
658 struct control_msg_out
*cm
, int spaceleft
)
662 if (unlikely(spaceleft
< 1))
665 dst
= skb_put(skb
, 1);
668 dst
[0] = KP_PING_ALL_CONNS
;
670 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
675 static int add_set_max_cmsg_dly(struct sk_buff
*skb
, struct control_retrans
*cr
,
676 struct control_msg_out
*cm
, int spaceleft
)
680 if (unlikely(spaceleft
< 5))
683 dst
= skb_put(skb
, 5);
686 dst
[0] = KP_SET_MAX_CMSG_DELAY
;
687 put_u32(dst
+ 1, cm
->msg
.set_max_cmsg_delay
.delay
, 1);
689 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
694 static int add_credits(struct sk_buff
*skb
, struct control_retrans
*cr
,
695 struct control_msg_out
*cm
, int spaceleft
)
697 unsigned long iflags
;
700 if (unlikely(spaceleft
< 21))
703 dst
= skb_put(skb
, 21);
706 dst
[0] = KP_SET_CREDITS
;
708 spin_lock_irqsave( &(cm
->nb
->credits_lock
), iflags
);
710 refresh_credits_state(cm
->nb
);
712 put_u64(dst
+ 1, cm
->nb
->debits
, 1);
713 put_u32(dst
+ 9, cm
->nb
->debitrate_initial
+
714 cm
->nb
->debitrate_initial_adj
, 1);
715 put_u32(dst
+ 13, cm
->nb
->debitrate_earning
, 1);
716 put_u32(dst
+ 17, cm
->nb
->debitrate_spending
, 1);
718 spin_unlock_irqrestore( &(cm
->nb
->credits_lock
), iflags
);
720 list_add_tail(&(cm
->lh
), &(cr
->msgs
));
725 static int add_message(struct sk_buff
*skb
, struct control_retrans
*cr
,
726 struct control_msg_out
*cm
, int spaceleft
,
727 struct control_msg_out
**split_conndata
, __u32
*sc_sendlen
)
729 BUG_ON(split_conndata
!= 0 && *split_conndata
!= 0);
730 BUG_ON(sc_sendlen
!= 0 && *sc_sendlen
!= 0);
734 return add_ack(skb
, cr
, cm
, spaceleft
);
735 case MSGTYPE_ACK_CONN
:
736 return add_ack_conn(skb
, cr
, cm
, spaceleft
);
737 case MSGTYPE_ACK_CONN_OOO
:
738 return add_ack_conn_ooo(skb
, cr
, cm
, spaceleft
);
740 return add_pong(skb
, cr
, cm
, spaceleft
);
741 case MSGTYPE_CONNECT
:
742 return add_connect(skb
, cr
, cm
, spaceleft
);
743 case MSGTYPE_CONNECT_SUCCESS
:
744 return add_connect_success(skb
, cr
, cm
, spaceleft
);
745 case MSGTYPE_RESET_CONN
:
746 return add_reset_conn(skb
, cr
, cm
, spaceleft
);
747 case MSGTYPE_CONNDATA
:
748 return add_conndata(skb
, cr
, cm
, spaceleft
, split_conndata
,
750 case MSGTYPE_PING_CONN
:
751 return add_ping_conn(skb
, cr
, cm
, spaceleft
);
752 case MSGTYPE_CONNID_UNKNOWN
:
753 return add_connid_unknown(skb
, cr
, cm
, spaceleft
);
754 case MSGTYPE_PING_ALL_CONNS
:
755 return add_ping_all_conns(skb
, cr
, cm
, spaceleft
);
756 case MSGTYPE_SET_MAX_CMSG_DELAY
:
757 return add_set_max_cmsg_dly(skb
, cr
, cm
, spaceleft
);
758 case MSGTYPE_SET_CREDITS
:
759 return add_credits(skb
, cr
, cm
, spaceleft
);
767 static __u32
recount_ping_conns(struct neighbor
*nb
)
770 struct list_head
*curr
= nb
->next_ping_conn
->target
.out
.nb_list
.next
;
771 while (curr
!= &(nb
->snd_conn_list
)) {
773 BUG_ON(cnt
> 1000000000);
778 static __u32
__send_messages_pc(struct neighbor
*nb
, struct sk_buff
*skb
,
779 struct control_retrans
*cr
, int spaceleft
)
782 mutex_lock(&(nb
->conn_list_lock
));
783 while (nb
->next_ping_conn
!= 0) {
786 struct list_head
*next
;
787 struct control_msg_out
*cm
;
790 rconn
= nb
->next_ping_conn
;
791 sconn
= rconn
->reversedir
;
793 BUG_ON(rconn
->targettype
!= TARGET_OUT
);
794 BUG_ON(sconn
->sourcetype
!= SOURCE_IN
);
796 if (unlikely(rconn
->target
.out
.conn_id
))
799 if (nb
->ping_conns_remaining
== 0) {
800 atomic_set(&(sconn
->source
.in
.pong_awaiting
), 1);
801 nb
->pong_conns_expected
++;
802 nb
->ping_conns_remaining
--;
803 if (unlikely(nb
->ping_conns_remaining
== 0))
804 nb
->ping_conns_remaining
=
805 recount_ping_conns(nb
);
807 if (likely(atomic_read(&(
808 sconn
->source
.in
.pong_awaiting
)) == 0))
810 nb
->ping_conns_remaining
--;
811 if (unlikely(nb
->ping_conns_retrans_remaining
== 0))
812 nb
->ping_conns_retrans_remaining
=
813 recount_ping_conns(nb
);
816 cm
= alloc_control_msg(nb
, ACM_PRIORITY_LOW
);
818 cm
->type
= MSGTYPE_PING_CONN
;
819 cm
->msg
.ping_conn
.conn_id
= rconn
->target
.out
.conn_id
;
820 rc
= add_message(skb
, cr
, cm
, spaceleft
- length
, 0, 0);
826 next
= rconn
->target
.out
.nb_list
.next
;
827 nb
->next_ping_conn
= container_of(next
, struct conn
,
829 if (next
== &(nb
->snd_conn_list
)) {
830 nb
->next_ping_conn
= 0;
831 nb
->ping_conns_remaining
= 0;
834 if (unlikely(length
!= 0)) {
835 nb
->ping_conn_completed
= jiffies
;
837 mutex_unlock(&(nb
->conn_list_lock
));
841 static __u32
__send_messages(struct neighbor
*nb
, struct sk_buff
*skb
,
842 struct control_retrans
*cr
, int spaceleft
, int urgentonly
,
843 struct control_msg_out
**split_conndata
, __u32
*sc_sendlen
)
846 while (!list_empty(&(nb
->ucontrol_msgs_out
)) || (!urgentonly
&&
847 !list_empty(&(nb
->control_msgs_out
)))) {
850 int urgent
= !list_empty(&(nb
->ucontrol_msgs_out
));
852 struct control_msg_out
*cm
;
855 cm
= container_of(nb
->ucontrol_msgs_out
.next
,
856 struct control_msg_out
, lh
);
858 cm
= container_of(nb
->control_msgs_out
.next
,
859 struct control_msg_out
, lh
);
863 nb
->ucmlength
-= cm
->length
;
865 nb
->cmlength
-= cm
->length
;
866 mutex_unlock(&(nb
->cmsg_lock
));
867 rc
= add_message(skb
, cr
, cm
, spaceleft
- length
,
868 split_conndata
, sc_sendlen
);
869 mutex_lock(&(nb
->cmsg_lock
));
873 list_add(&(cm
->lh
), &(nb
->ucontrol_msgs_out
));
874 nb
->ucmlength
+= cm
->length
;
876 list_add(&(cm
->lh
), &(nb
->control_msgs_out
));
877 nb
->cmlength
+= cm
->length
;
888 static int msgtype_present(struct neighbor
*nb
, __u8 type
)
890 struct list_head
*curr
;
892 curr
= nb
->control_msgs_out
.next
;
893 while (curr
!= &(nb
->control_msgs_out
)) {
894 struct control_msg_out
*cm
= container_of(curr
,
895 struct control_msg_out
, lh
);
897 if (cm
->type
== MSGTYPE_PING_ALL_CONNS
)
906 static int ping_all_conns_needed(struct neighbor
*nb
)
908 if (likely(nb
->ping_all_conns
== 0))
911 if (msgtype_present(nb
, MSGTYPE_PING_ALL_CONNS
))
917 static int __send_messages_cred(struct neighbor
*nb
, struct sk_buff
*skb
,
918 struct control_retrans
*cr
, int spaceleft
)
920 struct control_msg_out
*cm
;
923 cm
= alloc_control_msg(nb
, ACM_PRIORITY_MED
);
925 if (unlikely(cm
== 0))
928 cm
->type
= MSGTYPE_SET_CREDITS
;
931 rc
= add_message(skb
, cr
, cm
, spaceleft
, 0, 0);
933 nb
->send_credits
= 0;
937 static int __send_messages_smcd(struct neighbor
*nb
, struct sk_buff
*skb
,
938 struct control_retrans
*cr
, int spaceleft
)
940 struct control_msg_out
*cm
;
943 cm
= alloc_control_msg(nb
, ACM_PRIORITY_MED
);
945 if (unlikely(cm
== 0))
948 cm
->type
= MSGTYPE_SET_MAX_CMSG_DELAY
;
949 cm
->msg
.set_max_cmsg_delay
.delay
= CMSG_INTERVAL_MS
* 10;
952 rc
= add_message(skb
, cr
, cm
, spaceleft
, 0, 0);
954 nb
->max_cmsg_delay_sent
= 1;
958 static int __send_messages_pac(struct neighbor
*nb
, struct sk_buff
*skb
,
959 struct control_retrans
*cr
, int spaceleft
)
961 struct control_msg_out
*cm
;
964 cm
= alloc_control_msg(nb
, ACM_PRIORITY_MED
);
966 if (unlikely(cm
== 0))
969 cm
->type
= MSGTYPE_PING_ALL_CONNS
;
972 rc
= add_message(skb
, cr
, cm
, spaceleft
, 0, 0);
974 nb
->ping_all_conns
= 0;
979 static int _send_messages(struct neighbor
*nb
, struct sk_buff
*skb
, int ping
,
980 struct control_retrans
*cr
, int spaceleft
, int urgentonly
)
984 __u32 pingcookie
= 0;
985 struct control_msg_out
*split_conndata
= 0;
986 __u32 sc_sendlen
= 0;
988 mutex_lock(&(nb
->cmsg_lock
));
992 pingcookie
= add_ping_req(nb
);
993 rc
= add_ping(skb
, pingcookie
, spaceleft
- length
);
998 if (likely(urgentonly
== 0) && unlikely(ping_all_conns_needed(nb
) != 0))
999 length
+= __send_messages_pac(nb
, skb
, cr
, spaceleft
- length
);
1001 if (likely(urgentonly
== 0) && unlikely(nb
->max_cmsg_delay_sent
== 0))
1002 length
+= __send_messages_smcd(nb
, skb
, cr
, spaceleft
- length
);
1004 if (likely(urgentonly
== 0) && unlikely(nb
->send_credits
!= 0) &&
1005 msgtype_present(nb
, MSGTYPE_SET_CREDITS
) == 0)
1006 length
+= __send_messages_cred(nb
, skb
, cr
, spaceleft
- length
);
1009 length
+= __send_messages(nb
, skb
, cr
, spaceleft
- length
, urgentonly
,
1010 &split_conndata
, &sc_sendlen
);
1012 if (likely(urgentonly
== 0))
1013 length
+= __send_messages_pc(nb
, skb
, cr
, spaceleft
- length
);
1015 mutex_unlock(&(nb
->cmsg_lock
));
1017 if (unlikely(length
> spaceleft
))
1018 printk(KERN_ERR
"error cor/kpacket_gen: length > spaceleft!?");
1020 padding(skb
, spaceleft
- length
);
1022 rc
= dev_queue_xmit(skb
);
1025 unadd_ping_req(nb
, pingcookie
);
1027 while (list_empty(&(cr
->msgs
)) == 0) {
1028 struct control_msg_out
*cm
= container_of(cr
->msgs
.prev
,
1029 struct control_msg_out
, lh
);
1030 list_del(&(cm
->lh
));
1031 add_control_msg(cm
, 1);
1034 if (split_conndata
!= 0) {
1035 add_control_msg(split_conndata
, 1);
1038 kref_put(&(cr
->ref
), free_control_retrans
);
1040 struct list_head
*curr
= cr
->msgs
.next
;
1042 while(curr
!= &(cr
->msgs
)) {
1043 struct control_msg_out
*cm
= container_of(curr
,
1044 struct control_msg_out
, lh
);
1048 if (cm
->type
== MSGTYPE_CONNDATA
) {
1049 list_del(&(cm
->lh
));
1050 kfree(cm
->msg
.conn_data
.data_orig
);
1051 free_control_msg(cm
);
1055 if (split_conndata
!= 0) {
1056 BUG_ON(sc_sendlen
== 0);
1057 BUG_ON(sc_sendlen
>=
1058 split_conndata
->msg
.conn_data
.datalen
);
1060 split_conndata
->msg
.conn_data
.data
+= sc_sendlen
;
1061 split_conndata
->msg
.conn_data
.datalen
-= sc_sendlen
;
1063 send_conndata(split_conndata
,
1064 split_conndata
->msg
.conn_data
.conn_id
,
1065 split_conndata
->msg
.conn_data
.seqno
,
1066 split_conndata
->msg
.conn_data
.data_orig
,
1067 split_conndata
->msg
.conn_data
.data
,
1068 split_conndata
->msg
.conn_data
.datalen
);
1072 if (list_empty(&(cr
->msgs
)))
1073 kref_put(&(cr
->ref
), free_control_retrans
);
1075 schedule_retransmit(cr
, nb
);
1081 static __u32
get_total_messages_length(struct neighbor
*nb
, int ping
,
1084 __u32 length
= nb
->ucmlength
;
1085 if (likely(nb
->send_credits
== 0) && unlikely(debit_adj_needed(nb
)))
1086 nb
->send_credits
= 1;
1088 if (likely(urgentonly
== 0)) {
1089 length
+= nb
->cmlength
+ nb
->ping_conns_remaining
* 5;
1090 if (likely(nb
->ping_conns_remaining
== 0)) {
1091 if (likely(nb
->ping_conns_retrans_remaining
== 0) &&
1092 unlikely(nb
->pong_conns_expected
!=0) &&
1093 time_before(nb
->ping_conn_completed
,
1094 jiffies
+ msecs_to_jiffies(
1095 PING_ALL_CONNS_TIMEOUT
) +
1096 usecs_to_jiffies(((__u32
) atomic_read(&(
1097 nb
->latency
))) * 2 + ((__u32
)
1098 atomic_read(&(nb
->max_remote_cmsg_delay
)
1100 nb
->ping_conns_retrans_remaining
=
1101 nb
->pong_conns_expected
;
1103 if (unlikely(nb
->ping_conns_retrans_remaining
>
1104 nb
->pong_conns_expected
))
1105 nb
->ping_conns_retrans_remaining
=
1106 nb
->pong_conns_expected
;
1108 length
+= nb
->ping_conns_retrans_remaining
* 5;
1110 if (unlikely(ping_all_conns_needed(nb
) != 0))
1112 if (unlikely(nb
->max_cmsg_delay_sent
== 0))
1114 if (unlikely(nb
->send_credits
== 2) &&
1115 msgtype_present(nb
, MSGTYPE_SET_CREDITS
) == 0)
1118 if (ping
== 2 || (length
> 0 && ping
!= 0))
1120 if (likely(urgentonly
== 0) && length
> 0 &&
1121 unlikely(nb
->send_credits
== 1) &&
1122 msgtype_present(nb
, MSGTYPE_SET_CREDITS
) == 0)
1128 static int send_messages(struct neighbor
*nb
, int allmsgs
, int resume
)
1132 int targetmss
= mss(nb
);
1134 int nbstate
= get_neigh_state(nb
);
1135 int urgentonly
= (nbstate
!= NEIGHBOR_STATE_ACTIVE
);
1137 check_credit_state(nb
);
1139 mutex_lock(&(nb
->cmsg_lock
));
1142 allmsgs
= nb
->kp_allmsgs
;
1144 ping
= time_to_send_ping(nb
);
1150 struct sk_buff
*skb
;
1151 struct control_retrans
*cr
;
1153 BUG_ON(list_empty(&(nb
->control_msgs_out
)) &&
1154 (nb
->cmlength
!= 0));
1155 BUG_ON((list_empty(&(nb
->control_msgs_out
)) == 0) &&
1156 (nb
->cmlength
== 0));
1157 BUG_ON(list_empty(&(nb
->ucontrol_msgs_out
)) &&
1158 (nb
->ucmlength
!= 0));
1159 BUG_ON((list_empty(&(nb
->ucontrol_msgs_out
)) == 0) &&
1160 (nb
->ucmlength
== 0));
1161 BUG_ON(nb
->cmlength
< 0);
1162 BUG_ON(nb
->ucmlength
< 0);
1164 length
= get_total_messages_length(nb
, ping
, urgentonly
);
1169 if (length
< targetmss
&& allmsgs
== 0)
1172 seqno
= atomic_add_return(1, &(nb
->kpacket_seqno
));
1174 if (length
> targetmss
)
1177 mutex_unlock(&(nb
->cmsg_lock
));
1178 skb
= create_packet(nb
, length
, GFP_KERNEL
, 0, seqno
);
1179 if (unlikely(skb
== 0)) {
1180 printk(KERN_ERR
"cor: send_messages: cannot allocate "
1181 "skb (out of memory?)");
1185 cr
= kmem_cache_alloc(controlretrans_slab
, GFP_KERNEL
);
1186 if (unlikely(cr
== 0)) {
1188 printk(KERN_ERR
"cor: send_messages: cannot allocate "
1189 "control_retrans (out of memory?)");
1192 memset(cr
, 0, sizeof(struct control_retrans
));
1193 kref_init(&(cr
->ref
));
1196 INIT_LIST_HEAD(&(cr
->msgs
));
1198 rc
= _send_messages(nb
, skb
, ping
, cr
, length
, urgentonly
);
1201 mutex_lock(&(nb
->cmsg_lock
));
1209 mutex_lock(&(nb
->cmsg_lock
));
1214 nb
->kp_allmsgs
= nb
->kp_allmsgs
|| allmsgs
;
1215 qos_enqueue_kpacket(nb
);
1217 } else if (allmsgs
) {
1221 mutex_unlock(&(nb
->cmsg_lock
));
1224 schedule_controlmsg_timerfunc(nb
);
1229 int resume_send_messages(struct neighbor
*nb
)
1231 return send_messages(nb
, 0, 1);
1234 static void controlmsg_timerfunc(struct work_struct
*work
)
1236 struct neighbor
*nb
= container_of(to_delayed_work(work
),
1237 struct neighbor
, cmsg_timer
);
1238 __u64 jiffies
= get_jiffies_64();
1240 mutex_lock(&(nb
->cmsg_lock
));
1242 if (nb
->timeout
> jiffies
) {
1243 INIT_DELAYED_WORK(&(nb
->cmsg_timer
), controlmsg_timerfunc
);
1244 schedule_delayed_work(&(nb
->cmsg_timer
), nb
->timeout
- jiffies
);
1245 mutex_unlock(&(nb
->cmsg_lock
));
1249 mutex_unlock(&(nb
->cmsg_lock
));
1251 send_messages(nb
, 1, 0);
1252 kref_put(&(nb
->ref
), neighbor_free
);
1255 void schedule_controlmsg_timerfunc(struct neighbor
*nb
)
1257 __u64 jiffies
= get_jiffies_64();
1260 int state
= get_neigh_state(nb
);
1262 if (unlikely(state
== NEIGHBOR_STATE_KILLED
))
1265 mutex_lock(&(nb
->cmsg_lock
));
1266 nb
->timeout
+= msecs_to_jiffies(CMSG_INTERVAL_MS
);
1268 delay
= nb
->timeout
- jiffies
;
1271 nb
->timeout
= jiffies
;
1274 INIT_DELAYED_WORK(&(nb
->cmsg_timer
), controlmsg_timerfunc
);
1275 schedule_delayed_work(&(nb
->cmsg_timer
), delay
);
1276 mutex_unlock(&(nb
->cmsg_lock
));
1277 kref_get(&(nb
->ref
));
1280 static void free_oldest_ucm(struct neighbor
*nb
)
1282 struct control_msg_out
*cm
= container_of(nb
->ucontrol_msgs_out
.next
,
1283 struct control_msg_out
, lh
);
1285 BUG_ON(list_empty(&(nb
->ucontrol_msgs_out
)));
1286 BUG_ON(isurgent(cm
) == 0);
1288 list_del(&(cm
->lh
));
1289 nb
->ucmlength
-= cm
->length
;
1290 atomic_dec(&(nb
->ucmcnt
));
1291 free_control_msg(cm
);
1294 static void add_control_msg(struct control_msg_out
*cm
, int retrans
)
1298 BUG_ON(cm
->nb
== 0);
1300 nbstate
= get_neigh_state(cm
->nb
);
1303 BUG_ON(cm
->lh
.next
!= LIST_POISON1
|| cm
->lh
.prev
!= LIST_POISON2
);
1305 mutex_lock(&(cm
->nb
->cmsg_lock
));
1310 msgs
= atomic_inc_return(&(cm
->nb
->ucmcnt
));
1313 if (unlikely(retrans
)) {
1314 if (msgs
> MAX_URGENT_CMSGS_PER_NEIGH_RETRANSALLOW
||
1315 msgs
> MAX_URGENT_CMSGS_PER_NEIGH
) {
1316 atomic_dec(&(cm
->nb
->ucmcnt
));
1317 free_control_msg(cm
);
1321 cm
->nb
->ucmlength
+= cm
->length
;
1322 list_add(&(cm
->lh
), &(cm
->nb
->ucontrol_msgs_out
));
1324 if (msgs
> MAX_URGENT_CMSGS_PER_NEIGH
) {
1325 free_oldest_ucm(cm
->nb
);
1328 cm
->nb
->ucmlength
+= cm
->length
;
1329 list_add_tail(&(cm
->lh
), &(cm
->nb
->ucontrol_msgs_out
));
1332 cm
->nb
->cmlength
+= cm
->length
;
1333 list_add_tail(&(cm
->lh
), &(cm
->nb
->control_msgs_out
));
1336 if (unlikely((nbstate
== NEIGHBOR_STATE_ACTIVE
? cm
->nb
->cmlength
: 0)+
1337 cm
->nb
->ucmlength
>= mss(cm
->nb
)))
1338 send_messages(cm
->nb
, 0, 0);
1341 mutex_unlock(&(cm
->nb
->cmsg_lock
));
1344 void send_pong(struct neighbor
*nb
, __u32 cookie
)
1346 struct control_msg_out
*cm
= _alloc_control_msg(nb
, 0, 1);
1348 if (unlikely(cm
== 0))
1352 cm
->type
= MSGTYPE_PONG
;
1353 cm
->msg
.pong
.cookie
= cookie
;
1354 cm
->msg
.pong
.time_enqueued
= jiffies
;
1356 add_control_msg(cm
, 0);
1359 void send_reset_conn(struct control_msg_out
*cm
, __u32 conn_id
)
1361 cm
->type
= MSGTYPE_RESET_CONN
;
1362 cm
->msg
.reset
.conn_id
= conn_id
;
1364 add_control_msg(cm
, 0);
1367 void send_ack(struct neighbor
*nb
, __u32 seqno
)
1369 struct control_msg_out
*cm
= _alloc_control_msg(nb
, 0, 1);
1371 if (unlikely(cm
== 0))
1375 cm
->type
= MSGTYPE_ACK
;
1376 cm
->msg
.ack
.seqno
= seqno
;
1378 add_control_msg(cm
, 0);
1381 void send_ack_conn(struct control_msg_out
*cm
, struct conn
*rconn
,
1382 __u32 conn_id
, __u32 seqno
)
1384 cm
->type
= MSGTYPE_ACK_CONN
;
1385 kref_get(&(rconn
->ref
));
1386 cm
->msg
.ack_conn
.rconn
= rconn
;
1387 cm
->msg
.ack_conn
.conn_id
= conn_id
;
1388 cm
->msg
.ack_conn
.seqno
= seqno
;
1391 add_control_msg(cm
, 0);
1394 void send_ack_conn_ooo(struct control_msg_out
*cm
, struct conn
*rconn
,
1395 __u32 conn_id
, __u32 seqno
, __u32 seqno_ooo
, __u32 length
)
1397 cm
->type
= MSGTYPE_ACK_CONN_OOO
;
1398 kref_get(&(rconn
->ref
));
1399 cm
->msg
.ack_conn_ooo
.rconn
= rconn
;
1400 cm
->msg
.ack_conn_ooo
.conn_id
= conn_id
;
1401 cm
->msg
.ack_conn_ooo
.seqno
= seqno
;
1402 cm
->msg
.ack_conn_ooo
.seqno_ooo
= seqno_ooo
;
1403 cm
->msg
.ack_conn_ooo
.length
= length
;
1405 add_control_msg(cm
, 0);
1408 void send_connect_success(struct control_msg_out
*cm
, __u32 rcvd_conn_id
,
1409 __u32 gen_conn_id
, __u32 init_seqno
, struct conn
*rconn
)
1411 cm
->type
= MSGTYPE_CONNECT_SUCCESS
;
1412 cm
->msg
.connect_success
.rcvd_conn_id
= rcvd_conn_id
;
1413 cm
->msg
.connect_success
.gen_conn_id
= gen_conn_id
;
1414 cm
->msg
.connect_success
.init_seqno
= init_seqno
;
1415 kref_get(&(rconn
->ref
));
1416 cm
->msg
.connect_success
.rconn
= rconn
;
1418 add_control_msg(cm
, 0);
1421 void send_connect_nb(struct control_msg_out
*cm
, __u32 conn_id
,
1422 __u32 init_seqno
, struct conn
*sconn
)
1424 cm
->type
= MSGTYPE_CONNECT
;
1425 cm
->msg
.connect
.conn_id
= conn_id
;
1426 cm
->msg
.connect
.init_seqno
= init_seqno
;
1427 kref_get(&(sconn
->ref
));
1428 cm
->msg
.connect
.sconn
= sconn
;
1430 add_control_msg(cm
, 0);
1433 void send_conndata(struct control_msg_out
*cm
, __u32 conn_id
, __u32 seqno
,
1434 char *data_orig
, char *data
, __u32 datalen
)
1436 cm
->type
= MSGTYPE_CONNDATA
;
1437 cm
->msg
.conn_data
.conn_id
= conn_id
;
1438 cm
->msg
.conn_data
.seqno
= seqno
;
1439 cm
->msg
.conn_data
.data_orig
= data_orig
;
1440 cm
->msg
.conn_data
.data
= data
;
1441 cm
->msg
.conn_data
.datalen
= datalen
;
1442 cm
->length
= 11 + datalen
;
1443 add_control_msg(cm
, 0);
1446 void send_ping_conn(struct control_msg_out
*cm
, __u32 conn_id
)
1448 cm
->type
= MSGTYPE_PING_CONN
;
1449 cm
->msg
.ping_conn
.conn_id
= conn_id
;
1451 add_control_msg(cm
, 0);
1455 void send_connid_unknown(struct control_msg_out
*cm
, __u32 conn_id
)
1457 cm
->type
= MSGTYPE_CONNID_UNKNOWN
;
1458 cm
->msg
.connid_unknown
.conn_id
= conn_id
;
1460 add_control_msg(cm
, 0);
1463 void send_ping_all_conns(struct neighbor
*nb
)
1465 mutex_lock(&(nb
->cmsg_lock
));
1466 nb
->ping_all_conns
= 1;
1467 mutex_unlock(&(nb
->cmsg_lock
));
1470 void send_credits(struct neighbor
*nb
)
1472 mutex_lock(&(nb
->cmsg_lock
));
1473 nb
->send_credits
= 2;
1474 mutex_unlock(&(nb
->cmsg_lock
));
1477 static int matches_connretrans(void *htentry
, void *searcheditem
)
1479 struct control_retrans
*cr
= (struct control_retrans
*) htentry
;
1480 struct retransmit_matchparam
*rm
= (struct retransmit_matchparam
*)
1483 return rm
->nb
== cr
->nb
&& rm
->seqno
== cr
->seqno
;
1486 void __init
cor_kgen_init(void)
1488 controlmsg_slab
= kmem_cache_create("cor_controlmsg",
1489 sizeof(struct control_msg_out
), 8, 0, 0);
1490 controlretrans_slab
= kmem_cache_create("cor_controlretransmsg",
1491 sizeof(struct control_retrans
), 8, 0, 0);
1492 htable_init(&retransmits
, matches_connretrans
,
1493 offsetof(struct control_retrans
, htab_entry
),
1494 offsetof(struct control_retrans
, ref
));