buffering bugfixes
[cor_2_6_31.git] / net / cor / kpacket_gen.c
bloba83ee53c6f23a29f24c49ff9ae87ca9fcea76ba9
1 /*
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
18 * 02110-1301, USA.
21 #include <asm/byteorder.h>
23 #include "cor.h"
25 /* not sent over the network - internal meaning only */
26 #define MSGTYPE_PONG 1
27 #define MSGTYPE_ACK 2
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 */
42 struct neighbor *nb;
44 __u32 length;
46 __u8 type;
47 union{
48 struct{
49 __u32 cookie;
50 unsigned long time_enqueued; /* jiffies */
51 int sent;
52 }pong;
54 struct{
55 __u32 seqno;
56 }ack;
58 struct{
59 struct conn *rconn;
60 __u32 conn_id;
61 __u32 seqno;
62 }ack_conn;
64 struct{
65 struct conn *rconn;
66 __u32 conn_id;
67 __u32 seqno;
68 __u32 seqno_ooo;
69 __u32 length;
70 }ack_conn_ooo;
72 struct{
73 __u32 conn_id;
74 struct conn *sconn;
75 }connect;
77 struct{
78 __u32 rcvd_conn_id;
79 __u32 gen_conn_id;
80 struct conn *rconn;
81 }connect_success;
83 struct{
84 __u32 conn_id;
85 }reset;
87 struct{
88 __u32 conn_id;
89 __u32 seqno;
90 char *data_orig;
91 char *data;
92 __u32 datalen;
93 }conn_data;
95 struct{
96 __u32 conn_id;
97 }ping_conn;
99 struct{
100 __u32 conn_id;
101 }connid_unknown;
103 struct{
104 __u32 delay;
105 }set_max_cmsg_delay;
106 }msg;
109 struct control_retrans {
110 struct kref ref;
112 struct neighbor *nb;
113 __u32 seqno;
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))
135 return 1;
136 return 0;
139 static struct control_msg_out *__alloc_control_msg(void)
141 struct control_msg_out *cm = kmem_cache_alloc(controlmsg_slab,
142 GFP_KERNEL);
143 if (unlikely(cm == 0))
144 return 0;
145 cm->lh.next = LIST_POISON1;
146 cm->lh.prev = LIST_POISON2;
147 return cm;
150 static int calc_limit(int limit, int priority)
152 if (priority == ACM_PRIORITY_LOW)
153 return (limit+1)/2;
154 else if (priority == ACM_PRIORITY_MED)
155 return (limit*3 + 3)/4;
156 else if (priority == ACM_PRIORITY_HIGH)
157 return limit;
158 else
159 BUG();
162 static struct control_msg_out *_alloc_control_msg(struct neighbor *nb,
163 int priority, int urgent)
165 struct control_msg_out *cm = 0;
167 BUG_ON(nb == 0);
169 if (urgent == 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,
177 priority))
178 goto alloc;
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))))
184 goto full;
187 alloc:
188 cm = __alloc_control_msg();
189 if (unlikely(cm == 0))
190 goto full;
191 cm->nb = nb;
193 if (0) {
194 full:
195 if (urgent == 0) {
196 atomic_dec(&(nb->cmcnt));
197 atomic_dec(&(cmcnt));
200 return cm;
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,
230 ref);
232 while (list_empty(&(cr->msgs)) == 0) {
233 struct control_msg_out *cm = container_of(cr->msgs.next,
234 struct control_msg_out, lh);
235 list_del(&(cm->lh));
236 free_control_msg(cm);
239 kmem_cache_free(controlretrans_slab, cr);
242 struct retransmit_matchparam {
243 struct neighbor *nb;
244 __u32 seqno;
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);
266 int nbstate;
267 int nbput = 0;
269 spin_lock_irqsave( &(nb->state_lock), iflags );
270 nbstate = nb->state;
271 spin_unlock_irqrestore( &(nb->state_lock), iflags );
273 while (1) {
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;
281 nbput = 1;
282 break;
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;
291 rm.nb = nb;
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);
301 continue;
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);
308 break;
311 if (unlikely(htable_delete(&retransmits, rm_to_key(&rm), &rm,
312 free_control_retrans)))
313 BUG();
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);
320 list_del(&(cm->lh));
321 add_control_msg(cm, 1);
324 kref_put(&(cr->ref), free_control_retrans);
327 spin_unlock_irqrestore( &(nb->retrans_lock), iflags );
329 if (nbput)
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;
338 int first;
340 rm.seqno = cr->seqno;
341 rm.nb = nb;
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;
367 rm.seqno = seqno;
368 rm.nb = nb;
370 spin_lock_irqsave( &(nb->retrans_lock), iflags );
372 cr = (struct control_retrans *) htable_get(&retransmits, rm_to_key(&rm),
373 &rm);
375 if (cr == 0) {
376 printk(KERN_ERR "bogus/duplicate ack received");
377 goto out;
380 if (unlikely(htable_delete(&retransmits, rm_to_key(&rm), &rm,
381 free_control_retrans)))
382 BUG();
384 BUG_ON(cr->nb != nb);
386 list_del(&(cr->timeout_list));
388 out:
389 spin_unlock_irqrestore( &(nb->retrans_lock), iflags );
392 static void padding(struct sk_buff *skb, int length)
394 char *dst;
395 if (length <= 0)
396 return;
397 dst = skb_put(skb, length);
398 BUG_ON(dst == 0);
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)
405 char *dst;
407 if (unlikely(spaceleft < 5))
408 return 0;
410 dst = skb_put(skb, 5);
411 BUG_ON(dst == 0);
413 dst[0] = KP_ACK;
414 put_u32(dst + 1, cm->msg.ack.seqno, 1);
416 atomic_dec(&(cm->nb->ucmcnt));
417 free_control_msg(cm);
419 return 5;
422 static int add_ack_conn(struct sk_buff *skb, struct control_retrans *cr,
423 struct control_msg_out *cm, int spaceleft)
425 char *dst;
427 if (unlikely(spaceleft < 10))
428 return 0;
430 dst = skb_put(skb, 10);
431 BUG_ON(dst == 0);
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));
440 return 10;
443 static int add_ack_conn_ooo(struct sk_buff *skb, struct control_retrans *cr,
444 struct control_msg_out *cm, int spaceleft)
446 char *dst;
448 if (unlikely(spaceleft < 18))
449 return 0;
451 dst = skb_put(skb, 18);
452 BUG_ON(dst == 0);
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));
463 return 18;
466 static int add_ping(struct sk_buff *skb, __u32 cookie,
467 int spaceleft)
469 char *dst;
471 if (unlikely(spaceleft < 5))
472 return 0;
474 dst = skb_put(skb, 5);
475 BUG_ON(dst == 0);
477 dst[0] = KP_PING;
478 put_u32(dst + 1, cookie, 0);
480 return 5;
483 static int add_pong(struct sk_buff *skb, struct control_retrans *cr,
484 struct control_msg_out *cm, int spaceleft)
486 char *dst;
488 if (unlikely(spaceleft < 9))
489 return 0;
491 dst = skb_put(skb, 9);
492 BUG_ON(dst == 0);
494 dst[0] = KP_PONG;
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));
503 return 9;
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)
510 char *dst;
512 if (unlikely(spaceleft < 6))
513 return 0;
515 dst = skb_put(skb, 6);
516 BUG_ON(dst == 0);
518 dst[0] = KP_CONNECT;
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));
524 return 5;
527 static int add_connect_success(struct sk_buff *skb, struct control_retrans *cr,
528 struct control_msg_out *cm, int spaceleft)
530 char *dst;
532 if (unlikely(spaceleft < 10))
533 return 0;
535 dst = skb_put(skb, 10);
536 BUG_ON(dst == 0);
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));
545 return 9;
548 static int add_reset_conn(struct sk_buff *skb, struct control_retrans *cr,
549 struct control_msg_out *cm, int spaceleft)
551 char *dst;
553 if (unlikely(spaceleft < 5))
554 return 0;
556 dst = skb_put(skb, 5);
557 BUG_ON(dst == 0);
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));
564 return 5;
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)
571 char *dst;
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))
581 return 0;
583 dst = skb_put(skb, putlen);
584 BUG_ON(dst == 0);
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));
595 } else {
596 *split_conndata = cm;
597 *sc_sendlen = dataputlen;
600 return putlen;
603 static int add_ping_conn(struct sk_buff *skb, struct control_retrans *cr,
604 struct control_msg_out *cm, int spaceleft)
606 char *dst;
608 if (unlikely(spaceleft < 5))
609 return 0;
611 dst = skb_put(skb, 5);
612 BUG_ON(dst == 0);
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));
619 return 5;
622 static int add_connid_unknown(struct sk_buff *skb, struct control_retrans *cr,
623 struct control_msg_out *cm, int spaceleft)
625 char *dst;
627 if (unlikely(spaceleft < 5))
628 return 0;
630 dst = skb_put(skb, 5);
631 BUG_ON(dst == 0);
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));
638 return 5;
641 static int add_ping_all_conns(struct sk_buff *skb, struct control_retrans *cr,
642 struct control_msg_out *cm, int spaceleft)
644 char *dst;
646 if (unlikely(spaceleft < 1))
647 return 0;
649 dst = skb_put(skb, 1);
650 BUG_ON(dst == 0);
652 dst[0] = KP_PING_ALL_CONNS;
654 list_add_tail(&(cm->lh), &(cr->msgs));
656 return 1;
659 static int add_set_max_cmsg_dly(struct sk_buff *skb, struct control_retrans *cr,
660 struct control_msg_out *cm, int spaceleft)
662 char *dst;
664 if (unlikely(spaceleft < 5))
665 return 0;
667 dst = skb_put(skb, 5);
668 BUG_ON(dst == 0);
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));
675 return 5;
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;
682 char *dst;
684 if (unlikely(spaceleft < 21))
685 return 0;
687 dst = skb_put(skb, 21);
688 BUG_ON(dst == 0);
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));
706 return 21;
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);
716 switch (cm->type) {
717 case MSGTYPE_ACK:
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);
723 case MSGTYPE_PONG:
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,
733 sc_sendlen);
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);
744 default:
745 BUG();
747 BUG();
748 return 0;
751 static __u32 recount_ping_conns(struct neighbor *nb)
753 __u32 cnt;
754 struct list_head *curr = nb->next_ping_conn->target.out.nb_list.next;
755 while (curr != &(nb->snd_conn_list)) {
756 cnt++;
757 BUG_ON(cnt > 1000000000);
759 return cnt;
762 static __u32 __send_messages_pc(struct neighbor *nb, struct sk_buff *skb,
763 struct control_retrans *cr, int spaceleft)
765 __u32 length = 0;
766 mutex_lock(&(nb->conn_list_lock));
767 while (nb->next_ping_conn != 0) {
768 struct conn *rconn;
769 struct conn *sconn;
770 struct list_head *next;
771 struct control_msg_out *cm;
772 int rc;
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))
781 goto next;
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);
790 } else {
791 if (likely(atomic_read(&(
792 sconn->source.in.pong_awaiting)) == 0))
793 goto next;
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);
801 cm->length = 5;
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);
805 if (rc == 0)
806 break;
808 length = rc;
809 next:
810 next = rconn->target.out.nb_list.next;
811 nb->next_ping_conn = container_of(next, struct conn,
812 target.out.nb_list);
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));
822 return length;
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)
829 __u32 length = 0;
830 while (!list_empty(&(nb->ucontrol_msgs_out)) || (!urgentonly &&
831 !list_empty(&(nb->control_msgs_out)))) {
832 int rc;
834 int urgent = !list_empty(&(nb->ucontrol_msgs_out));
836 struct control_msg_out *cm;
838 if (urgent)
839 cm = container_of(nb->ucontrol_msgs_out.next,
840 struct control_msg_out, lh);
841 else
842 cm = container_of(nb->control_msgs_out.next,
843 struct control_msg_out, lh);
845 list_del(&(cm->lh));
846 if (urgent)
847 nb->ucmlength -= cm->length;
848 else
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));
855 if (rc == 0) {
856 if (urgent) {
857 list_add(&(cm->lh), &(nb->ucontrol_msgs_out));
858 nb->ucmlength += cm->length;
859 } else {
860 list_add(&(cm->lh), &(nb->control_msgs_out));
861 nb->cmlength += cm->length;
863 break;
866 length += rc;
869 return 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)
882 return 1;
884 curr = curr->next;
887 return 0;
890 static int ping_all_conns_needed(struct neighbor *nb)
892 if (likely(nb->ping_all_conns == 0))
893 return 0;
895 if (msgtype_present(nb, MSGTYPE_PING_ALL_CONNS))
896 return 0;
898 return 1;
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;
905 int rc;
907 cm = alloc_control_msg(nb, ACM_PRIORITY_MED);
909 if (unlikely(cm == 0))
910 return 0;
912 cm->type = MSGTYPE_SET_CREDITS;
913 cm->length = 21;
915 rc = add_message(skb, cr, cm, spaceleft, 0, 0);
917 nb->send_credits = 0;
918 return rc;
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;
925 int rc;
927 cm = alloc_control_msg(nb, ACM_PRIORITY_MED);
929 if (unlikely(cm == 0))
930 return 0;
932 cm->type = MSGTYPE_SET_MAX_CMSG_DELAY;
933 cm->msg.set_max_cmsg_delay.delay = CMSG_INTERVAL_MS * 10;
934 cm->length = 5;
936 rc = add_message(skb, cr, cm, spaceleft, 0, 0);
938 nb->max_cmsg_delay_sent = 1;
939 return rc;
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;
946 int rc;
948 cm = alloc_control_msg(nb, ACM_PRIORITY_MED);
950 if (unlikely(cm == 0))
951 return 0;
953 cm->type = MSGTYPE_PING_ALL_CONNS;
954 cm->length = 1;
956 rc = add_message(skb, cr, cm, spaceleft, 0, 0);
958 nb->ping_all_conns = 0;
959 return rc;
963 static int _send_messages(struct neighbor *nb, struct sk_buff *skb, int ping,
964 struct control_retrans *cr, int spaceleft, int urgentonly)
966 int rc;
967 int length = 0;
968 __u32 pingcookie = 0;
969 struct control_msg_out *split_conndata = 0;
970 __u32 sc_sendlen = 0;
972 mutex_lock(&(nb->cmsg_lock));
974 if (ping != 0) {
975 int rc;
976 pingcookie = add_ping_req(nb);
977 rc = add_ping(skb, pingcookie, spaceleft - length);
978 BUG_ON(rc == 0);
979 length += rc;
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);
1008 if (rc != 0) {
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);
1023 } else {
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);
1030 curr = curr->next;
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);
1058 else
1059 schedule_retransmit(cr, nb);
1062 return rc;
1065 static __u32 get_total_messages_length(struct neighbor *nb, int ping,
1066 int urgentonly)
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)
1083 )))))
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))
1095 length += 1;
1096 if (unlikely(nb->max_cmsg_delay_sent == 0))
1097 length += 5;
1098 if (unlikely(nb->send_credits == 2) &&
1099 msgtype_present(nb, MSGTYPE_SET_CREDITS) == 0)
1100 length += 21;
1102 if (ping == 2 || (length > 0 && ping != 0))
1103 length += 5;
1104 if (likely(urgentonly == 0) && length > 0 &&
1105 unlikely(nb->send_credits == 1) &&
1106 msgtype_present(nb, MSGTYPE_SET_CREDITS) == 0)
1107 length += 21;
1109 return length;
1112 static int send_messages(struct neighbor *nb, int allmsgs, int resume)
1114 int rc = 0;
1115 int ping;
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));
1125 if (resume)
1126 allmsgs = nb->kp_allmsgs;
1128 ping = time_to_send_ping(nb);
1130 while (1) {
1131 __u32 length;
1133 __u32 seqno;
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);
1150 if (length == 0)
1151 break;
1153 if (length < targetmss && allmsgs == 0)
1154 break;
1156 seqno = atomic_add_return(1, &(nb->kpacket_seqno));
1158 if (length > targetmss)
1159 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?)");
1166 goto oom;
1169 cr = kmem_cache_alloc(controlretrans_slab, GFP_KERNEL);
1170 if (unlikely(cr == 0)) {
1171 kfree_skb(skb);
1172 printk(KERN_ERR "cor: send_messages: cannot allocate "
1173 "control_retrans (out of memory?)");
1174 goto oom;
1176 memset(cr, 0, sizeof(struct control_retrans));
1177 kref_init(&(cr->ref));
1178 cr->nb = nb;
1179 cr->seqno = seqno;
1180 INIT_LIST_HEAD(&(cr->msgs));
1182 rc = _send_messages(nb, skb, ping, cr, length, urgentonly);
1183 ping = 0;
1185 mutex_lock(&(nb->cmsg_lock));
1187 if (rc != 0)
1188 break;
1191 if (0) {
1192 oom:
1193 mutex_lock(&(nb->cmsg_lock));
1196 if (rc != 0) {
1197 if (resume == 0) {
1198 nb->kp_allmsgs = nb->kp_allmsgs || allmsgs;
1199 qos_enqueue_kpacket(nb);
1201 } else if (allmsgs) {
1202 nb->kp_allmsgs = 0;
1205 mutex_unlock(&(nb->cmsg_lock));
1207 if (allmsgs)
1208 schedule_controlmsg_timerfunc(nb);
1210 return rc;
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));
1230 return;
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();
1242 long long delay;
1244 int state = get_neigh_state(nb);
1246 if (unlikely(state == NEIGHBOR_STATE_KILLED))
1247 return;
1249 mutex_lock(&(nb->cmsg_lock));
1250 nb->timeout += msecs_to_jiffies(CMSG_INTERVAL_MS);
1252 delay = nb->timeout - jiffies;
1253 if (delay < 0) {
1254 delay = 1;
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)
1280 int nbstate;
1282 BUG_ON(cm->nb == 0);
1284 nbstate = get_neigh_state(cm->nb);
1286 BUG_ON(cm == 0);
1287 BUG_ON(cm->lh.next != LIST_POISON1 || cm->lh.prev != LIST_POISON2);
1289 mutex_lock(&(cm->nb->cmsg_lock));
1291 if (isurgent(cm)) {
1292 long msgs;
1294 msgs = atomic_inc_return(&(cm->nb->ucmcnt));
1295 BUG_ON(msgs <= 0);
1297 if (unlikely(retrans)) {
1298 if (msgs > MAX_URGENT_CMSGS_PER_NEIGH) {
1299 atomic_dec(&(cm->nb->ucmcnt));
1300 free_control_msg(cm);
1301 goto out;
1304 cm->nb->ucmlength += cm->length;
1305 list_add(&(cm->lh), &(cm->nb->ucontrol_msgs_out));
1306 } else {
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));
1314 } else {
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);
1323 out:
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))
1332 return;
1334 cm->nb = nb;
1335 cm->type = MSGTYPE_PONG;
1336 cm->msg.pong.cookie = cookie;
1337 cm->msg.pong.time_enqueued = jiffies;
1338 cm->length = 9;
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;
1346 cm->length = 5;
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))
1355 return;
1357 cm->nb = nb;
1358 cm->type = MSGTYPE_ACK;
1359 cm->msg.ack.seqno = seqno;
1360 cm->length = 5;
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;
1373 cm->length = 10;
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;
1387 cm->length = 18;
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;
1399 cm->length = 10;
1400 add_control_msg(cm, 0);
1403 void send_connect_nb(struct control_msg_out *cm, __u32 conn_id,
1404 struct conn *sconn)
1406 cm->type = MSGTYPE_CONNECT;
1407 cm->msg.connect.conn_id = conn_id;
1408 kref_get(&(sconn->ref));
1409 cm->msg.connect.sconn = sconn;
1410 cm->length = 6;
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;
1431 cm->length = 5;
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;
1440 cm->length = 5;
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 *)
1462 searcheditem;
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));