qos_queue locking+content free, conn_ack_rcvd reset check, neighbor_operations lock...
[cor_2_6_31.git] / net / cor / kpacket_gen.c
blob3892b729bed34569aca95c139b021b4cf3485650
1 /**
2 * Connection oriented routing
3 * Copyright (C) 2007-2011 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_CONNECT 4
30 #define MSGTYPE_CONNECT_SUCCESS 5
31 #define MSGTYPE_RESET_CONN 6
32 #define MSGTYPE_CONNDATA 7
33 #define MSGTYPE_CONNID_UNKNOWN 8
34 #define MSGTYPE_SET_MAX_CMSG_DELAY 9
36 #define MSGTYPE_PONG_TIMEENQUEUED 1
37 #define MSGTYPE_PONG_RESPDELAY 2
39 struct control_msg_out{
40 struct list_head lh; /* either neighbor or control_retrans_packet */
41 struct neighbor *nb;
43 struct kref ref;
45 unsigned long timeout;
47 __u32 length;
49 __u8 type;
50 union{
51 struct{
53 __u32 cookie;
54 __u8 type;
56 union {
57 ktime_t time_enqueued;
58 __u32 respdelay;
59 } delaycomp;
60 }pong;
62 struct{
63 __u32 seqno;
64 }ack;
66 struct{
67 struct conn *src_in;
68 struct list_head conn_acks;
69 __u32 conn_id;
70 __u32 seqno;
71 __u32 seqno_ooo;
72 __u32 length;
74 __u16 decaytime;
75 __u8 decaytime_seqno;
77 __u8 flags;
79 __u32 ack_seqno;
80 }ack_conn;
82 struct{
83 __u32 conn_id;
84 __u32 init_seqno;
85 struct conn *src_in;
86 }connect;
88 struct{
89 __u32 rcvd_conn_id;
90 __u32 gen_conn_id;
91 __u32 init_seqno;
92 struct conn *src_in;
93 }connect_success;
95 struct{
96 struct htab_entry htab_entry;
97 __u32 conn_id_reset;
98 __u32 conn_id_unknown;
99 }reset_connidunknown;
101 struct{
102 __u32 conn_id;
103 __u32 seqno;
104 char *data_orig;
105 char *data;
106 __u32 datalen;
107 }conn_data;
109 struct{
110 __u32 delay;
111 }set_max_cmsg_delay;
112 }msg;
115 struct control_retrans {
116 struct kref ref;
118 struct neighbor *nb;
119 __u32 seqno;
121 unsigned long timeout;
123 struct list_head msgs;
125 struct htab_entry htab_entry;
126 struct list_head timeout_list;
129 struct unknownconnid_matchparam {
130 struct neighbor *nb;
131 __u32 conn_id;
134 struct retransmit_matchparam {
135 struct neighbor *nb;
136 __u32 seqno;
140 struct kmem_cache *controlmsg_slab;
141 struct kmem_cache *controlretrans_slab;
143 static struct htable retransmits;
145 DEFINE_SPINLOCK(unknown_connids_lock);
146 static struct htable unknown_connids;
148 atomic_t cmcnt = ATOMIC_INIT(0);
151 static void add_control_msg(struct control_msg_out *msg, int retrans);
153 static void try_merge_ackconns(struct conn *src_in_l,
154 struct control_msg_out *cm);
156 static void mergeadd_ackconn(struct conn *src_in_l, struct control_msg_out *cm);
159 static __u32 ucm_to_key(struct unknownconnid_matchparam *ucm)
161 return ((__u32)((long) ucm->nb)) ^ ucm->conn_id;
164 static __u32 rm_to_key(struct retransmit_matchparam *rm)
166 return ((__u32)((long) rm->nb)) ^ rm->seqno;
169 static inline int isurgent(struct control_msg_out *cm)
171 if (unlikely(cm->type == MSGTYPE_PONG || cm->type == MSGTYPE_ACK))
172 return 1;
173 return 0;
176 static struct control_msg_out *__alloc_control_msg(void)
178 struct control_msg_out *cm = kmem_cache_alloc(controlmsg_slab,
179 GFP_KERNEL);
180 if (unlikely(cm == 0))
181 return 0;
182 memset(cm, 0, sizeof(struct control_msg_out));
183 cm->lh.next = LIST_POISON1;
184 cm->lh.prev = LIST_POISON2;
185 kref_init(&(cm->ref));
186 return cm;
189 static int calc_limit(int limit, int priority)
191 if (priority == ACM_PRIORITY_LOW)
192 return (limit+2)/3;
193 else if (priority == ACM_PRIORITY_MED)
194 return (limit * 2 + 1)/3;
195 else if (priority == ACM_PRIORITY_HIGH)
196 return limit;
197 else
198 BUG();
201 int may_alloc_control_msg(struct neighbor *nb, int priority)
203 long packets1 = atomic_read(&(nb->cmcnt));
204 long packets2 = atomic_read(&(cmcnt));
206 BUG_ON(packets1 < 0);
207 BUG_ON(packets2 < 0);
209 if (packets1 < calc_limit(GUARANTEED_CMSGS_PER_NEIGH, priority))
210 return 1;
212 if (unlikely(unlikely(packets2 >= calc_limit(MAX_CMSGS_PER_NEIGH,
213 priority)) || unlikely(packets1 >= (
214 calc_limit(MAX_CMSGS_PER_NEIGH, priority) *
215 (MAX_CMSGS - packets2) / MAX_CMSGS))))
216 return 0;
217 return 1;
220 static struct control_msg_out *_alloc_control_msg(struct neighbor *nb,
221 int priority, int urgent)
223 struct control_msg_out *cm = 0;
225 BUG_ON(nb == 0);
227 if (urgent == 0) {
228 long packets1 = atomic_inc_return(&(nb->cmcnt));
229 long packets2 = atomic_inc_return(&(cmcnt));
231 BUG_ON(packets1 <= 0);
232 BUG_ON(packets2 <= 0);
234 if (packets1 <= calc_limit(GUARANTEED_CMSGS_PER_NEIGH,
235 priority))
236 goto alloc;
238 if (unlikely(unlikely(packets2 > calc_limit(MAX_CMSGS_PER_NEIGH,
239 priority)) || unlikely(packets1 > (
240 calc_limit(MAX_CMSGS_PER_NEIGH, priority) *
241 (MAX_CMSGS - packets2) / MAX_CMSGS))))
242 goto full;
245 alloc:
246 cm = __alloc_control_msg();
247 if (unlikely(cm == 0))
248 goto full;
249 cm->nb = nb;
251 if (0) {
252 full:
253 if (urgent == 0) {
254 atomic_dec(&(nb->cmcnt));
255 atomic_dec(&(cmcnt));
258 return cm;
261 struct control_msg_out *alloc_control_msg(struct neighbor *nb, int priority)
263 return _alloc_control_msg(nb, priority, 0);
266 static void cmsg_kref_free(struct kref *ref)
268 struct control_msg_out *cm = container_of(ref, struct control_msg_out,
269 ref);
270 kmem_cache_free(controlmsg_slab, cm);
273 void free_control_msg(struct control_msg_out *cm)
275 if (isurgent(cm) == 0) {
276 atomic_dec(&(cm->nb->cmcnt));
277 atomic_dec(&(cmcnt));
280 if (cm->type == MSGTYPE_ACK_CONN) {
281 struct conn *trgt_out = cm->msg.ack_conn.src_in->reversedir;
282 BUG_ON(cm->msg.ack_conn.src_in == 0);
283 mutex_lock(&(trgt_out->rcv_lock));
284 BUG_ON(trgt_out->targettype != TARGET_OUT);
285 if ((cm->msg.ack_conn.flags & KP_ACK_CONN_FLAGS_CREDITS) != 0 &&
286 trgt_out->target.out.decaytime_send_allowed !=
287 0) {
288 trgt_out->target.out.decaytime_send_allowed = 1;
289 mutex_unlock(&(trgt_out->rcv_lock));
290 refresh_conn_credits(trgt_out, 0, 0);
291 } else {
292 mutex_unlock(&(trgt_out->rcv_lock));
295 kref_put(&(cm->msg.ack_conn.src_in->ref), free_conn);
296 cm->msg.ack_conn.src_in = 0;
297 } else if (cm->type == MSGTYPE_CONNECT) {
298 BUG_ON(cm->msg.connect.src_in == 0);
299 kref_put(&(cm->msg.connect.src_in->ref), free_conn);
300 cm->msg.connect.src_in = 0;
301 } else if (cm->type == MSGTYPE_CONNECT_SUCCESS) {
302 BUG_ON(cm->msg.connect_success.src_in == 0);
303 kref_put(&(cm->msg.connect_success.src_in->ref), free_conn);
304 cm->msg.connect_success.src_in = 0;
305 } else if (cm->type == MSGTYPE_RESET_CONN ||
306 cm->type == MSGTYPE_CONNID_UNKNOWN) {
307 struct unknownconnid_matchparam ucm;
309 ucm.nb = cm->nb;
310 ucm.conn_id = cm->msg.reset_connidunknown.conn_id_unknown;
312 htable_delete(&unknown_connids, ucm_to_key(&ucm), &ucm,
313 cmsg_kref_free);
316 kref_put(&(cm->ref), cmsg_kref_free);
319 static void free_control_retrans(struct kref *ref)
321 struct control_retrans *cr = container_of(ref, struct control_retrans,
322 ref);
324 while (list_empty(&(cr->msgs)) == 0) {
325 struct control_msg_out *cm = container_of(cr->msgs.next,
326 struct control_msg_out, lh);
327 list_del(&(cm->lh));
328 free_control_msg(cm);
331 kmem_cache_free(controlretrans_slab, cr);
335 static void set_retrans_timeout(struct control_retrans *cr, struct neighbor *nb)
337 cr->timeout = jiffies + usecs_to_jiffies(100000 +
338 ((__u32) atomic_read(&(nb->latency))) * 2 +
339 ((__u32) atomic_read(&(nb->max_remote_cmsg_delay))));
342 static void remove_connack_oooflag_ifold(struct conn *src_in_l,
343 struct control_msg_out *cm)
345 if (ooolen(cm->msg.ack_conn.flags) != 0 && seqno_before_eq(
346 cm->msg.ack_conn.seqno_ooo +
347 cm->msg.ack_conn.length,
348 src_in_l->source.in.next_seqno)) {
349 cm->msg.ack_conn.length = 0;
350 cm->msg.ack_conn.flags = (cm->msg.ack_conn.flags &
351 (~KP_ACK_CONN_FLAGS_OOO));
355 static int ackconn_prepare_readd(struct conn *cn_l,
356 struct control_msg_out *cm)
358 if (unlikely(unlikely(cn_l->sourcetype != SOURCE_IN) ||
359 unlikely(cn_l->source.in.nb != cm->nb) ||
360 unlikely(cn_l->reversedir->target.out.conn_id !=
361 cm->msg.ack_conn.conn_id) ||
362 unlikely(cn_l->isreset != 0)))
363 return 0;
365 remove_connack_oooflag_ifold(cn_l, cm);
367 if (cm->msg.ack_conn.ack_seqno != cn_l->source.in.ack_seqno)
368 cm->msg.ack_conn.flags = (cm->msg.ack_conn.flags &
369 (~KP_ACK_CONN_FLAGS_SEQNO) &
370 (~KP_ACK_CONN_FLAGS_WINDOW));
372 if (cm->msg.ack_conn.flags == 0)
373 return 0;
375 cm->length = 6 + ack_conn_len(cm->msg.ack_conn.flags);
377 return 1;
380 static void readd_control_retrans(struct control_retrans *cr)
382 while (list_empty(&(cr->msgs)) == 0) {
383 struct control_msg_out *cm = container_of(cr->msgs.next,
384 struct control_msg_out, lh);
385 list_del(&(cm->lh));
386 if (cm->type == MSGTYPE_ACK_CONN) {
387 struct conn *cn_l = cm->msg.ack_conn.src_in;
388 mutex_lock(&(cn_l->rcv_lock));
389 if (unlikely(ackconn_prepare_readd(cn_l, cm) == 0)) {
390 free_control_msg(cm);
391 } else {
392 mergeadd_ackconn(cn_l, cm);
395 mutex_unlock(&(cn_l->rcv_lock));
396 } else {
397 add_control_msg(cm, 1);
402 void retransmit_timerfunc(struct work_struct *work)
404 unsigned long iflags;
406 struct neighbor *nb = container_of(to_delayed_work(work),
407 struct neighbor, retrans_timer);
409 int nbstate;
410 int nbput = 0;
412 spin_lock_irqsave(&(nb->state_lock), iflags);
413 nbstate = nb->state;
414 spin_unlock_irqrestore(&(nb->state_lock), iflags);
416 while (1) {
417 struct control_retrans *cr = 0;
418 struct retransmit_matchparam rm;
420 spin_lock_irqsave(&(nb->retrans_lock), iflags);
422 if (list_empty(&(nb->retrans_list))) {
423 nb->retrans_timer_running = 0;
424 nbput = 1;
425 break;
428 cr = container_of(nb->retrans_list.next,
429 struct control_retrans, timeout_list);
431 BUG_ON(cr->nb != nb);
433 rm.seqno = cr->seqno;
434 rm.nb = nb;
436 list_del(&(cr->timeout_list));
438 if (unlikely(nbstate == NEIGHBOR_STATE_KILLED)) {
439 spin_unlock_irqrestore(&(nb->retrans_lock), iflags);
441 htable_delete(&retransmits, rm_to_key(&rm), &rm,
442 free_control_retrans);
443 kref_put(&(cr->ref), free_control_retrans);
444 continue;
447 if (time_after(cr->timeout, jiffies)) {
448 list_add(&(cr->timeout_list), &(nb->retrans_list));
449 schedule_delayed_work(&(nb->retrans_timer),
450 cr->timeout - jiffies);
451 break;
454 if (unlikely(htable_delete(&retransmits, rm_to_key(&rm), &rm,
455 free_control_retrans)))
456 BUG();
458 spin_unlock_irqrestore(&(nb->retrans_lock), iflags);
460 readd_control_retrans(cr);
462 kref_put(&(cr->ref), free_control_retrans);
465 spin_unlock_irqrestore(&(nb->retrans_lock), iflags);
467 if (nbput)
468 kref_put(&(nb->ref), neighbor_free);
471 static void schedule_retransmit(struct control_retrans *cr, struct neighbor *nb)
473 unsigned long iflags;
475 struct retransmit_matchparam rm;
476 int first;
478 rm.seqno = cr->seqno;
479 rm.nb = nb;
481 set_retrans_timeout(cr, nb);
483 spin_lock_irqsave(&(nb->retrans_lock), iflags);
484 htable_insert(&retransmits, (char *) cr, rm_to_key(&rm));
485 first = list_empty(&(nb->retrans_list));
486 list_add_tail(&(cr->timeout_list), &(nb->retrans_list));
488 if (first && nb->retrans_timer_running == 0) {
489 schedule_delayed_work(&(nb->retrans_timer),
490 cr->timeout - jiffies);
491 nb->retrans_timer_running = 1;
492 kref_get(&(nb->ref));
495 spin_unlock_irqrestore(&(nb->retrans_lock), iflags);
498 void kern_ack_rcvd(struct neighbor *nb, __u32 seqno)
500 unsigned long iflags;
502 struct control_retrans *cr = 0;
503 struct retransmit_matchparam rm;
505 rm.seqno = seqno;
506 rm.nb = nb;
508 spin_lock_irqsave(&(nb->retrans_lock), iflags);
510 cr = (struct control_retrans *) htable_get(&retransmits, rm_to_key(&rm),
511 &rm);
513 if (cr == 0) {
514 printk(KERN_ERR "bogus/duplicate ack received");
515 goto out;
518 if (unlikely(htable_delete(&retransmits, rm_to_key(&rm), &rm,
519 free_control_retrans)))
520 BUG();
522 BUG_ON(cr->nb != nb);
524 list_del(&(cr->timeout_list));
526 out:
527 spin_unlock_irqrestore(&(nb->retrans_lock), iflags);
529 if (cr != 0) {
530 kref_put(&(cr->ref), free_control_retrans); /* htable_get */
531 kref_put(&(cr->ref), free_control_retrans); /* list */
535 static void padding(struct sk_buff *skb, int length)
537 char *dst;
538 if (length <= 0)
539 return;
540 dst = skb_put(skb, length);
541 BUG_ON(dst == 0);
542 memset(dst, KP_PADDING, length);
545 static int add_ack(struct sk_buff *skb, struct control_retrans *cr,
546 struct control_msg_out *cm, int spaceleft)
548 char *dst;
550 if (unlikely(spaceleft < 5))
551 return 0;
553 dst = skb_put(skb, 5);
554 BUG_ON(dst == 0);
556 dst[0] = KP_ACK;
557 put_u32(dst + 1, cm->msg.ack.seqno, 1);
559 atomic_dec(&(cm->nb->ucmcnt));
560 free_control_msg(cm);
562 return 5;
565 static int add_ack_conn(struct sk_buff *skb, struct control_retrans *cr,
566 struct control_msg_out *cm, int spaceleft)
568 char *dst;
569 int offset = 0;
571 if (unlikely(spaceleft < cm->length))
572 return 0;
574 dst = skb_put(skb, cm->length);
575 BUG_ON(dst == 0);
577 dst[offset] = KP_ACK_CONN;
578 offset++;
579 put_u32(dst + offset, cm->msg.ack_conn.conn_id, 1);
580 offset += 4;
581 dst[offset] = cm->msg.ack_conn.flags;
582 offset++;
584 if ((cm->msg.ack_conn.flags & KP_ACK_CONN_FLAGS_SEQNO) != 0) {
585 put_u32(dst + offset, cm->msg.ack_conn.seqno, 1);
586 offset += 4;
588 if ((cm->msg.ack_conn.flags & KP_ACK_CONN_FLAGS_WINDOW) != 0) {
589 BUG_ON(cm->msg.ack_conn.src_in == 0);
590 dst[offset] = get_window(cm->msg.ack_conn.src_in,
591 cm->nb, cm->msg.ack_conn.conn_id, 1);
592 offset++;
596 if (ooolen(cm->msg.ack_conn.flags) != 0) {
597 put_u32(dst + offset, cm->msg.ack_conn.seqno_ooo, 1);
598 offset += 4;
599 if (ooolen(cm->msg.ack_conn.flags) == 1) {
600 BUG_ON(cm->msg.ack_conn.length > 255);
601 dst[offset] = cm->msg.ack_conn.length;
602 offset += 1;
603 } else if (ooolen(cm->msg.ack_conn.flags) == 2) {
604 BUG_ON(cm->msg.ack_conn.length <= 255);
605 BUG_ON(cm->msg.ack_conn.length > 65535);
606 put_u16(dst + offset, cm->msg.ack_conn.length, 1);
607 offset += 2;
608 } else if (ooolen(cm->msg.ack_conn.flags) == 4) {
609 BUG_ON(cm->msg.ack_conn.length <= 65535);
610 put_u32(dst + offset, cm->msg.ack_conn.length, 1);
611 offset += 4;
612 } else {
613 BUG();
617 if ((cm->msg.ack_conn.flags & KP_ACK_CONN_FLAGS_CREDITS) != 0) {
618 __u16 value = cm->msg.ack_conn.decaytime + (
619 cm->msg.ack_conn.decaytime_seqno << 10);
621 BUG_ON(cm->msg.ack_conn.decaytime >= 1024);
622 BUG_ON(cm->msg.ack_conn.decaytime_seqno >= 64);
624 put_u16(dst + offset, value, 1);
625 offset += 2;
628 list_add_tail(&(cm->lh), &(cr->msgs));
630 BUG_ON(offset != cm->length);
631 return offset;
634 static int add_ping(struct sk_buff *skb, __u32 cookie,
635 int spaceleft)
637 char *dst;
639 if (unlikely(spaceleft < 5))
640 return 0;
642 dst = skb_put(skb, 5);
643 BUG_ON(dst == 0);
645 dst[0] = KP_PING;
646 put_u32(dst + 1, cookie, 0);
648 return 5;
651 static int add_pong(struct sk_buff *skb, struct control_retrans *cr,
652 struct control_msg_out *cm, int spaceleft)
654 char *dst;
656 if (unlikely(spaceleft < 9))
657 return 0;
659 if (cm->msg.pong.type == MSGTYPE_PONG_TIMEENQUEUED) {
660 __s64 now = ktime_to_ns(ktime_get());
661 __s64 enq = ktime_to_ns(cm->msg.pong.delaycomp.time_enqueued);
662 __s64 respdelay = (now - enq + 500) / 1000;
663 if (unlikely(respdelay >= (1LL << 32)))
664 respdelay = (1LL << 32) - 1;
665 cm->msg.pong.type = MSGTYPE_PONG_RESPDELAY;
666 cm->msg.pong.delaycomp.respdelay = (__u32) respdelay;
669 BUG_ON(cm->msg.pong.type != MSGTYPE_PONG_RESPDELAY);
671 dst = skb_put(skb, 9);
672 BUG_ON(dst == 0);
674 dst[0] = KP_PONG;
675 put_u32(dst + 1, cm->msg.pong.cookie, 0);
676 put_u32(dst + 5, cm->msg.pong.delaycomp.respdelay, 1);
679 atomic_dec(&(cm->nb->ucmcnt));
680 list_add_tail(&(cm->lh), &(cr->msgs));
682 return 9;
685 static __u16 get_credits(struct conn *sconn)
687 __u16 ret;
688 mutex_lock(&(sconn->reversedir->rcv_lock));
689 BUG_ON(sconn->reversedir->targettype != TARGET_OUT);
691 BUG_ON(sconn->reversedir->target.out.decaytime_last >= 1024);
692 BUG_ON(sconn->reversedir->target.out.decaytime_seqno >= 64);
693 ret = sconn->reversedir->target.out.decaytime_last + (
694 sconn->reversedir->target.out.decaytime_seqno <<
695 10);
696 mutex_unlock(&(sconn->reversedir->rcv_lock));
698 return ret;
701 static int add_connect(struct sk_buff *skb, struct control_retrans *cr,
702 struct control_msg_out *cm, int spaceleft)
704 char *dst;
706 if (unlikely(spaceleft < 12))
707 return 0;
709 dst = skb_put(skb, 12);
710 BUG_ON(dst == 0);
712 dst[0] = KP_CONNECT;
713 put_u32(dst + 1, cm->msg.connect.conn_id, 1);
714 put_u32(dst + 5, cm->msg.connect.init_seqno, 1);
715 BUG_ON(cm->msg.connect.src_in == 0);
716 dst[9] = get_window(cm->msg.connect.src_in, cm->nb, 0, 1);
717 put_u16(dst + 10, get_credits(cm->msg.connect.src_in), 1);
719 list_add_tail(&(cm->lh), &(cr->msgs));
721 return 12;
724 static int add_connect_success(struct sk_buff *skb, struct control_retrans *cr,
725 struct control_msg_out *cm, int spaceleft)
727 char *dst;
729 if (unlikely(spaceleft < 16))
730 return 0;
732 dst = skb_put(skb, 16);
733 BUG_ON(dst == 0);
735 dst[0] = KP_CONNECT_SUCCESS;
736 put_u32(dst + 1, cm->msg.connect_success.rcvd_conn_id, 1);
737 put_u32(dst + 5, cm->msg.connect_success.gen_conn_id, 1);
738 put_u32(dst + 9, cm->msg.connect_success.init_seqno, 1);
739 BUG_ON(cm->msg.connect_success.src_in == 0);
740 dst[13] = get_window(cm->msg.connect_success.src_in, cm->nb,
741 cm->msg.connect_success.rcvd_conn_id, 1);
742 put_u16(dst + 14, get_credits(cm->msg.connect_success.src_in), 1);
744 list_add_tail(&(cm->lh), &(cr->msgs));
746 return 16;
749 static int add_reset_conn(struct sk_buff *skb, struct control_retrans *cr,
750 struct control_msg_out *cm, int spaceleft)
752 char *dst;
754 if (unlikely(spaceleft < 5))
755 return 0;
757 dst = skb_put(skb, 5);
758 BUG_ON(dst == 0);
760 dst[0] = KP_RESET_CONN;
761 put_u32(dst + 1, cm->msg.reset_connidunknown.conn_id_reset, 1);
763 list_add_tail(&(cm->lh), &(cr->msgs));
765 return 5;
768 static int add_conndata(struct sk_buff *skb, struct control_retrans *cr,
769 struct control_msg_out *cm, int spaceleft,
770 struct control_msg_out **split_conndata, __u32 *sc_sendlen)
772 char *dst;
774 int totallen = cm->msg.conn_data.datalen + 11;
775 int putlen = min(totallen, spaceleft);
776 int dataputlen = putlen - 11;
778 BUG_ON(split_conndata == 0);
779 BUG_ON(sc_sendlen == 0);
781 if (dataputlen < 1 || (spaceleft < 25 && spaceleft < totallen))
782 return 0;
784 dst = skb_put(skb, putlen);
785 BUG_ON(dst == 0);
787 dst[0] = KP_CONN_DATA;
788 put_u32(dst + 1, cm->msg.conn_data.conn_id, 1);
789 put_u32(dst + 5, cm->msg.conn_data.seqno, 1);
790 put_u16(dst + 9, dataputlen, 1);
792 memcpy(dst + 11, cm->msg.conn_data.data, dataputlen);
794 if (cm->msg.conn_data.datalen == dataputlen) {
795 list_add_tail(&(cm->lh), &(cr->msgs));
796 } else {
797 *split_conndata = cm;
798 *sc_sendlen = dataputlen;
801 return putlen;
804 static int add_connid_unknown(struct sk_buff *skb, struct control_retrans *cr,
805 struct control_msg_out *cm, int spaceleft)
807 char *dst;
809 if (unlikely(spaceleft < 5))
810 return 0;
812 dst = skb_put(skb, 5);
813 BUG_ON(dst == 0);
815 dst[0] = KP_CONNID_UNKNOWN;
816 put_u32(dst + 1, cm->msg.reset_connidunknown.conn_id_unknown, 1);
818 list_add_tail(&(cm->lh), &(cr->msgs));
820 return 5;
823 static int add_set_max_cmsg_dly(struct sk_buff *skb, struct control_retrans *cr,
824 struct control_msg_out *cm, int spaceleft)
826 char *dst;
828 if (unlikely(spaceleft < 5))
829 return 0;
831 dst = skb_put(skb, 5);
832 BUG_ON(dst == 0);
834 dst[0] = KP_SET_MAX_CMSG_DELAY;
835 put_u32(dst + 1, cm->msg.set_max_cmsg_delay.delay, 1);
837 list_add_tail(&(cm->lh), &(cr->msgs));
839 return 5;
842 static int add_message(struct sk_buff *skb, struct control_retrans *cr,
843 struct control_msg_out *cm, int spaceleft,
844 struct control_msg_out **split_conndata, __u32 *sc_sendlen)
846 BUG_ON(split_conndata != 0 && *split_conndata != 0);
847 BUG_ON(sc_sendlen != 0 && *sc_sendlen != 0);
849 switch (cm->type) {
850 case MSGTYPE_ACK:
851 return add_ack(skb, cr, cm, spaceleft);
852 case MSGTYPE_ACK_CONN:
853 return add_ack_conn(skb, cr, cm, spaceleft);
854 case MSGTYPE_PONG:
855 return add_pong(skb, cr, cm, spaceleft);
856 case MSGTYPE_CONNECT:
857 return add_connect(skb, cr, cm, spaceleft);
858 case MSGTYPE_CONNECT_SUCCESS:
859 return add_connect_success(skb, cr, cm, spaceleft);
860 case MSGTYPE_RESET_CONN:
861 return add_reset_conn(skb, cr, cm, spaceleft);
862 case MSGTYPE_CONNDATA:
863 return add_conndata(skb, cr, cm, spaceleft, split_conndata,
864 sc_sendlen);
865 case MSGTYPE_CONNID_UNKNOWN:
866 return add_connid_unknown(skb, cr, cm, spaceleft);
867 case MSGTYPE_SET_MAX_CMSG_DELAY:
868 return add_set_max_cmsg_dly(skb, cr, cm, spaceleft);
869 default:
870 BUG();
872 BUG();
873 return 0;
876 static void requeue_message(struct control_msg_out *cm)
878 if (cm->type == MSGTYPE_ACK_CONN) {
879 struct conn *cn_l = cm->msg.ack_conn.src_in;
881 mutex_lock(&(cn_l->rcv_lock));
882 if (unlikely(ackconn_prepare_readd(cn_l, cm) == 0)) {
883 free_control_msg(cm);
884 } else {
885 mutex_lock(&(cm->nb->cmsg_lock));
887 list_add(&(cm->lh), &(cm->nb->control_msgs_out));
888 cm->nb->cmlength += cm->length;
890 list_add(&(cm->msg.ack_conn.conn_acks),
891 &(cn_l->source.in.acks_pending));
892 try_merge_ackconns(cn_l, cm);
894 mutex_unlock(&(cm->nb->cmsg_lock));
896 mutex_unlock(&(cn_l->rcv_lock));
897 return;
900 if (isurgent(cm)) {
901 list_add(&(cm->lh), &(cm->nb->ucontrol_msgs_out));
902 cm->nb->ucmlength += cm->length;
903 } else {
904 list_add(&(cm->lh), &(cm->nb->control_msgs_out));
905 cm->nb->cmlength += cm->length;
909 static struct control_msg_out *dequeue_message(struct neighbor *nb,
910 int urgentonly)
912 struct control_msg_out *cm;
914 if (list_empty(&(nb->ucontrol_msgs_out)) == 0) {
915 cm = container_of(nb->ucontrol_msgs_out.next,
916 struct control_msg_out, lh);
917 nb->ucmlength -= cm->length;
918 } else if (urgentonly) {
919 return 0;
920 } else {
921 if (list_empty(&(nb->control_msgs_out)))
922 return 0;
924 cm = container_of(nb->control_msgs_out.next,
925 struct control_msg_out, lh);
926 nb->cmlength -= cm->length;
929 BUG_ON(cm->nb != nb);
931 list_del(&(cm->lh));
932 if (cm->type == MSGTYPE_ACK_CONN)
933 list_del(&(cm->msg.ack_conn.conn_acks));
935 return cm;
938 static __u32 __send_messages(struct neighbor *nb, struct sk_buff *skb,
939 struct control_retrans *cr, int spaceleft, int urgentonly,
940 struct control_msg_out **split_conndata, __u32 *sc_sendlen)
942 __u32 length = 0;
943 while (1) {
944 int rc;
945 struct control_msg_out *cm;
947 mutex_lock(&(nb->cmsg_lock));
948 cm = dequeue_message(nb, urgentonly);
949 mutex_unlock(&(nb->cmsg_lock));
951 if (cm == 0)
952 break;
954 rc = add_message(skb, cr, cm, spaceleft - length,
955 split_conndata, sc_sendlen);
956 if (rc == 0) {
957 requeue_message(cm);
958 break;
961 length += rc;
964 return length;
967 static __u32 __send_messages_smcd(struct neighbor *nb, struct sk_buff *skb,
968 struct control_retrans *cr, int spaceleft)
970 struct control_msg_out *cm;
971 int rc;
973 cm = alloc_control_msg(nb, ACM_PRIORITY_LOW);
975 if (unlikely(cm == 0))
976 return 0;
978 cm->type = MSGTYPE_SET_MAX_CMSG_DELAY;
979 cm->msg.set_max_cmsg_delay.delay = CMSG_INTERVAL_MS * 10;
980 cm->length = 5;
982 rc = add_message(skb, cr, cm, spaceleft, 0, 0);
984 nb->max_cmsg_delay_sent = 1;
986 return rc;
989 static int _send_messages(struct neighbor *nb, struct sk_buff *skb, int ping,
990 struct control_retrans *cr, int spaceleft, int urgentonly)
992 int rc;
993 __u32 length = 0;
994 __u32 pinglen = 0;
995 __u32 pingcookie = 0;
996 unsigned long last_ping_time;
997 struct control_msg_out *split_conndata = 0;
998 __u32 sc_sendlen = 0;
1000 mutex_lock(&(nb->cmsg_lock));
1002 if (ping != 0) {
1003 int rc;
1004 pingcookie = add_ping_req(nb, &last_ping_time);
1005 rc = add_ping(skb, pingcookie, spaceleft - length);
1006 BUG_ON(rc == 0);
1007 pinglen = rc;
1008 length += rc;
1011 if (likely(urgentonly == 0) && unlikely(nb->max_cmsg_delay_sent == 0))
1012 length += __send_messages_smcd(nb, skb, cr, spaceleft - length);
1014 mutex_unlock(&(nb->cmsg_lock));
1016 length += __send_messages(nb, skb, cr, spaceleft - length, urgentonly,
1017 &split_conndata, &sc_sendlen);
1019 BUG_ON(length > spaceleft);
1021 if (likely(ping != 2) && unlikely(length == pinglen)) {
1022 unadd_ping_req(nb, pingcookie, last_ping_time);
1023 kfree_skb(skb);
1025 BUG_ON(list_empty(&(cr->msgs)) == 0);
1026 kref_put(&(cr->ref), free_control_retrans);
1028 atomic_sub(1, &(nb->kpacket_seqno));
1029 return 0;
1032 padding(skb, spaceleft - length);
1034 rc = dev_queue_xmit(skb);
1036 if (rc != 0) {
1037 unadd_ping_req(nb, pingcookie, last_ping_time);
1039 while (list_empty(&(cr->msgs)) == 0) {
1040 struct control_msg_out *cm = container_of(cr->msgs.prev,
1041 struct control_msg_out, lh);
1042 list_del(&(cm->lh));
1043 add_control_msg(cm, 1);
1046 if (split_conndata != 0) {
1047 add_control_msg(split_conndata, 1);
1050 kref_put(&(cr->ref), free_control_retrans);
1051 } else {
1052 struct list_head *curr = cr->msgs.next;
1054 while(curr != &(cr->msgs)) {
1055 struct control_msg_out *cm = container_of(curr,
1056 struct control_msg_out, lh);
1058 curr = curr->next;
1060 if (cm->type == MSGTYPE_CONNDATA) {
1061 list_del(&(cm->lh));
1062 kfree(cm->msg.conn_data.data_orig);
1063 free_control_msg(cm);
1067 if (split_conndata != 0) {
1068 BUG_ON(sc_sendlen == 0);
1069 BUG_ON(sc_sendlen >=
1070 split_conndata->msg.conn_data.datalen);
1072 split_conndata->msg.conn_data.data += sc_sendlen;
1073 split_conndata->msg.conn_data.datalen -= sc_sendlen;
1075 send_conndata(split_conndata,
1076 split_conndata->msg.conn_data.conn_id,
1077 split_conndata->msg.conn_data.seqno,
1078 split_conndata->msg.conn_data.data_orig,
1079 split_conndata->msg.conn_data.data,
1080 split_conndata->msg.conn_data.datalen);
1084 if (list_empty(&(cr->msgs)))
1085 kref_put(&(cr->ref), free_control_retrans);
1086 else
1087 schedule_retransmit(cr, nb);
1090 return rc;
1093 static __u32 get_total_messages_length(struct neighbor *nb, int ping,
1094 int urgentonly)
1096 __u32 length = nb->ucmlength;
1098 if (likely(urgentonly == 0)) {
1099 length += nb->cmlength;
1101 if (unlikely(nb->max_cmsg_delay_sent == 0))
1102 length += 5;
1104 if (ping == 2 || (length > 0 && ping != 0))
1105 length += 5;
1107 return length;
1110 static int reset_timeouted_conn_needed(struct neighbor *nb,
1111 struct conn *src_in_l)
1113 if (unlikely(unlikely(src_in_l->sourcetype != SOURCE_IN) ||
1114 unlikely(src_in_l->source.in.nb != nb) ||
1115 unlikely(src_in_l->isreset != 0)))
1116 return 0;
1117 else if (likely(time_after(src_in_l->source.in.jiffies_last_act +
1118 CONN_ACTIVITY_UPDATEINTERVAL_SEC * HZ +
1119 CONN_INACTIVITY_TIMEOUT_SEC * HZ, jiffies)))
1120 return 0;
1122 return 1;
1125 static int reset_timeouted_conn(struct neighbor *nb, struct conn *src_in)
1127 int resetted = 0;
1129 if (src_in->is_client) {
1130 mutex_lock(&(src_in->rcv_lock));
1131 mutex_lock(&(src_in->reversedir->rcv_lock));
1132 } else {
1133 mutex_lock(&(src_in->reversedir->rcv_lock));
1134 mutex_lock(&(src_in->rcv_lock));
1137 resetted = reset_timeouted_conn_needed(nb, src_in);
1138 if (unlikely(resetted == 0))
1139 goto unlock;
1141 resetted = (send_reset_conn(nb, src_in->reversedir->target.out.conn_id,
1142 src_in->source.in.conn_id, 1) == 0);
1143 if (unlikely(resetted == 0))
1144 goto unlock;
1147 BUG_ON(src_in->reversedir->isreset != 0);
1148 src_in->reversedir->isreset = 1;
1150 unlock:
1151 if (src_in->is_client) {
1152 mutex_unlock(&(src_in->rcv_lock));
1153 mutex_unlock(&(src_in->reversedir->rcv_lock));
1154 } else {
1155 mutex_unlock(&(src_in->reversedir->rcv_lock));
1156 mutex_unlock(&(src_in->rcv_lock));
1159 if (resetted)
1160 reset_conn(src_in);
1162 return resetted;
1165 static void reset_timeouted_conns(struct neighbor *nb)
1167 int i;
1168 for (i=0;i<10000;i++) {
1169 unsigned long iflags;
1170 struct conn *src_in;
1172 int resetted = 1;
1174 spin_lock_irqsave(&(nb->conn_list_lock), iflags);
1176 if (list_empty(&(nb->rcv_conn_list))) {
1177 spin_unlock_irqrestore(&(nb->conn_list_lock), iflags);
1178 break;
1181 src_in = container_of(nb->rcv_conn_list.next, struct conn,
1182 source.in.nb_list);
1183 kref_get(&(src_in->ref));
1185 spin_unlock_irqrestore(&(nb->conn_list_lock), iflags);
1188 mutex_lock(&(src_in->rcv_lock));
1189 resetted = reset_timeouted_conn_needed(nb, src_in);
1190 mutex_unlock(&(src_in->rcv_lock));
1191 if (likely(resetted == 0))
1192 goto put;
1194 resetted = reset_timeouted_conn(nb, src_in);
1196 put:
1197 kref_put(&(src_in->ref), free_conn);
1199 if (likely(resetted == 0))
1200 break;
1204 int send_messages(struct neighbor *nb, int resume)
1206 int i;
1207 int rc = 0;
1208 int ping;
1209 int targetmss = mss(nb);
1211 int nbstate = get_neigh_state(nb);
1212 int urgentonly = (nbstate != NEIGHBOR_STATE_ACTIVE);
1214 if (likely(urgentonly == 0))
1215 reset_timeouted_conns(nb);
1217 mutex_lock(&(nb->send_cmsg_lock));
1218 mutex_lock(&(nb->cmsg_lock));
1220 ping = time_to_send_ping(nb);
1222 for (i=0;1;i++) {
1223 __u32 length;
1225 __u32 seqno;
1226 struct sk_buff *skb;
1227 struct control_retrans *cr;
1229 BUG_ON(list_empty(&(nb->control_msgs_out)) &&
1230 (nb->cmlength != 0));
1231 BUG_ON((list_empty(&(nb->control_msgs_out)) == 0) &&
1232 (nb->cmlength == 0));
1233 BUG_ON(list_empty(&(nb->ucontrol_msgs_out)) &&
1234 (nb->ucmlength != 0));
1235 BUG_ON((list_empty(&(nb->ucontrol_msgs_out)) == 0) &&
1236 (nb->ucmlength == 0));
1237 BUG_ON(nb->cmlength < 0);
1238 BUG_ON(nb->ucmlength < 0);
1240 length = get_total_messages_length(nb, ping, urgentonly);
1242 if (length == 0)
1243 break;
1245 if (length < targetmss && i > 0)
1246 break;
1248 seqno = atomic_add_return(1, &(nb->kpacket_seqno));
1250 if (length > targetmss)
1251 length = targetmss;
1253 mutex_unlock(&(nb->cmsg_lock));
1254 skb = create_packet(nb, length, GFP_KERNEL, 0, seqno);
1255 if (unlikely(skb == 0)) {
1256 printk(KERN_ERR "cor: send_messages: cannot allocate "
1257 "skb (out of memory?)");
1258 goto oom;
1261 cr = kmem_cache_alloc(controlretrans_slab, GFP_KERNEL);
1262 if (unlikely(cr == 0)) {
1263 kfree_skb(skb);
1264 printk(KERN_ERR "cor: send_messages: cannot allocate "
1265 "control_retrans (out of memory?)");
1266 goto oom;
1268 memset(cr, 0, sizeof(struct control_retrans));
1269 kref_init(&(cr->ref));
1270 cr->nb = nb;
1271 cr->seqno = seqno;
1272 INIT_LIST_HEAD(&(cr->msgs));
1274 rc = _send_messages(nb, skb, ping, cr, length, urgentonly);
1275 ping = 0;
1277 mutex_lock(&(nb->cmsg_lock));
1279 if (rc != 0)
1280 break;
1283 if (0) {
1284 oom:
1285 mutex_lock(&(nb->cmsg_lock));
1288 if (rc != 0) {
1289 if (resume == 0)
1290 qos_enqueue(nb->queue, &(nb->rb_kp),
1291 QOS_CALLER_KPACKET);
1292 } else {
1293 atomic_set(&(nb->cmsg_work_scheduled), 0);
1294 schedule_controlmsg_timer(nb);
1297 mutex_unlock(&(nb->cmsg_lock));
1298 mutex_unlock(&(nb->send_cmsg_lock));
1300 if (resume == 0)
1301 kref_put(&(nb->ref), neighbor_free);
1303 return rc;
1306 void controlmsg_workfunc(struct work_struct *work)
1308 struct neighbor *nb = container_of(work, struct neighbor, cmsg_work);
1309 send_messages(nb, 0);
1312 static void schedule_cmsg_work(struct neighbor *nb)
1314 if (atomic_cmpxchg(&(nb->cmsg_work_scheduled), 0, 1) == 0) {
1315 kref_get(&(nb->ref));
1316 atomic_cmpxchg(&(nb->cmsg_timer_running), 1, 2);
1317 schedule_work(&(nb->cmsg_work));
1321 void controlmsg_timerfunc(unsigned long arg)
1323 struct neighbor *nb = (struct neighbor *) arg;
1325 int oldval = atomic_xchg(&(nb->cmsg_timer_running), 0);
1327 BUG_ON(oldval == 0);
1329 if (likely(oldval == 1))
1330 schedule_cmsg_work(nb);
1331 kref_put(&(nb->ref), neighbor_free);
1334 static unsigned long get_cmsg_timeout(struct neighbor *nb, int nbstate)
1336 unsigned long timeout = get_next_ping_time(nb);
1338 if (likely(nbstate == NEIGHBOR_STATE_ACTIVE) &&
1339 list_empty(&(nb->control_msgs_out)) == 0) {
1340 struct control_msg_out *first = container_of(
1341 nb->control_msgs_out.next,
1342 struct control_msg_out, lh);
1343 if (time_before(first->timeout, jiffies +
1344 usecs_to_jiffies(nb->cmsg_interval)))
1345 timeout = jiffies;
1346 else if (time_before(first->timeout, timeout))
1347 timeout = first->timeout;
1350 if (list_empty(&(nb->ucontrol_msgs_out)) == 0) {
1351 struct control_msg_out *first = container_of(
1352 nb->ucontrol_msgs_out.next,
1353 struct control_msg_out, lh);
1354 if (time_before(first->timeout, jiffies +
1355 usecs_to_jiffies(nb->cmsg_interval)))
1356 timeout = jiffies;
1357 else if (time_before(first->timeout, timeout))
1358 timeout = first->timeout;
1361 return timeout;
1364 static int cmsg_full_packet(struct neighbor *nb, int nbstate)
1366 int ping = time_to_send_ping(nb);
1367 int urgentonly = (nbstate != NEIGHBOR_STATE_ACTIVE);
1368 __u32 len = get_total_messages_length(nb, ping, urgentonly);
1370 if (len == 0)
1371 return 0;
1372 if (len < mss(nb))
1373 return 0;
1375 return 1;
1378 void schedule_controlmsg_timer(struct neighbor *nb)
1380 unsigned long timeout;
1381 int state = get_neigh_state(nb);
1383 if (unlikely(state == NEIGHBOR_STATE_KILLED)) {
1384 atomic_cmpxchg(&(nb->cmsg_timer_running), 1, 2);
1385 return;
1388 if (unlikely(atomic_read(&(nb->cmsg_work_scheduled)) == 1))
1389 return;
1391 if (cmsg_full_packet(nb, state))
1392 goto now;
1394 timeout = get_cmsg_timeout(nb, state);
1396 if (time_before_eq(timeout, jiffies)) {
1397 now:
1398 schedule_cmsg_work(nb);
1399 } else {
1400 if (atomic_xchg(&(nb->cmsg_timer_running), 1) == 0)
1401 kref_get(&(nb->ref));
1402 mod_timer(&(nb->cmsg_timer), timeout);
1406 static void free_oldest_ucm(struct neighbor *nb)
1408 struct control_msg_out *cm = container_of(nb->ucontrol_msgs_out.next,
1409 struct control_msg_out, lh);
1411 BUG_ON(list_empty(&(nb->ucontrol_msgs_out)));
1412 BUG_ON(isurgent(cm) == 0);
1414 list_del(&(cm->lh));
1415 nb->ucmlength -= cm->length;
1416 atomic_dec(&(nb->ucmcnt));
1417 free_control_msg(cm);
1420 static void add_control_msg(struct control_msg_out *cm, int retrans)
1422 int nbstate;
1423 __u64 newinterval;
1424 unsigned long jiffies_tmp;
1426 BUG_ON(cm->nb == 0);
1428 nbstate = get_neigh_state(cm->nb);
1430 BUG_ON(cm == 0);
1431 BUG_ON(cm->lh.next != LIST_POISON1 || cm->lh.prev != LIST_POISON2);
1433 cm->timeout = jiffies + msecs_to_jiffies(CMSG_INTERVAL_MS);
1435 mutex_lock(&(cm->nb->cmsg_lock));
1437 if (isurgent(cm)) {
1438 long msgs;
1440 msgs = atomic_inc_return(&(cm->nb->ucmcnt));
1441 BUG_ON(msgs <= 0);
1443 if (unlikely(retrans)) {
1444 if (msgs > MAX_URGENT_CMSGS_PER_NEIGH_RETRANSALLOW ||
1445 msgs > MAX_URGENT_CMSGS_PER_NEIGH) {
1446 atomic_dec(&(cm->nb->ucmcnt));
1447 free_control_msg(cm);
1448 goto out;
1451 cm->nb->ucmlength += cm->length;
1452 list_add(&(cm->lh), &(cm->nb->ucontrol_msgs_out));
1453 } else {
1454 if (msgs > MAX_URGENT_CMSGS_PER_NEIGH) {
1455 free_oldest_ucm(cm->nb);
1458 cm->nb->ucmlength += cm->length;
1459 list_add_tail(&(cm->lh), &(cm->nb->ucontrol_msgs_out));
1461 } else {
1462 cm->nb->cmlength += cm->length;
1463 list_add_tail(&(cm->lh), &(cm->nb->control_msgs_out));
1466 jiffies_tmp = jiffies;
1467 newinterval = (((__u64) cm->nb->cmsg_interval) * 255 +
1468 jiffies_to_usecs(jiffies_tmp -
1469 cm->nb->jiffies_last_cmsg)) / 256;
1470 cm->nb->jiffies_last_cmsg = jiffies_tmp;
1471 if (unlikely(newinterval > (1LL << 32) - 1))
1472 cm->nb->cmsg_interval = (__u32) ((1LL << 32) - 1);
1473 else
1474 cm->nb->cmsg_interval = newinterval;
1476 schedule_controlmsg_timer(cm->nb);
1478 out:
1479 mutex_unlock(&(cm->nb->cmsg_lock));
1483 void send_pong(struct neighbor *nb, __u32 cookie)
1485 struct control_msg_out *cm = _alloc_control_msg(nb, 0, 1);
1487 if (unlikely(cm == 0))
1488 return;
1490 cm->nb = nb;
1491 cm->type = MSGTYPE_PONG;
1492 cm->msg.pong.cookie = cookie;
1493 cm->msg.pong.type = MSGTYPE_PONG_TIMEENQUEUED;
1494 cm->msg.pong.delaycomp.time_enqueued = ktime_get();
1495 cm->length = 9;
1496 add_control_msg(cm, 0);
1499 int send_reset_conn(struct neighbor *nb, __u32 conn_id_reset,
1500 __u32 conn_id_unknown, int lowprio)
1502 unsigned long iflags;
1503 int killed;
1504 struct control_msg_out *cm;
1505 struct unknownconnid_matchparam ucm;
1507 spin_lock_irqsave(&(nb->state_lock), iflags);
1508 killed = (nb->state == NEIGHBOR_STATE_KILLED);
1509 spin_unlock_irqrestore(&(nb->state_lock), iflags);
1511 if (unlikely(killed))
1512 return 0;
1514 cm = alloc_control_msg(nb, lowprio ?
1515 ACM_PRIORITY_LOW : ACM_PRIORITY_HIGH);
1517 if (unlikely(cm == 0))
1518 return 1;
1520 cm->type = MSGTYPE_RESET_CONN;
1521 cm->msg.reset_connidunknown.conn_id_reset = conn_id_reset;
1522 cm->msg.reset_connidunknown.conn_id_unknown = conn_id_unknown;
1523 cm->length = 5;
1525 if (conn_id_unknown != 0) {
1526 ucm.nb = nb;
1527 ucm.conn_id = conn_id_unknown;
1529 spin_lock_irqsave(&unknown_connids_lock, iflags);
1530 BUG_ON(htable_get(&unknown_connids, ucm_to_key(&ucm), &ucm) !=
1532 htable_insert(&unknown_connids, (char *) cm, ucm_to_key(&ucm));
1533 spin_unlock_irqrestore(&unknown_connids_lock, iflags);
1536 add_control_msg(cm, 0);
1538 return 0;
1541 void send_ack(struct neighbor *nb, __u32 seqno)
1543 struct control_msg_out *cm = _alloc_control_msg(nb, 0, 1);
1545 if (unlikely(cm == 0))
1546 return;
1548 cm->nb = nb;
1549 cm->type = MSGTYPE_ACK;
1550 cm->msg.ack.seqno = seqno;
1551 cm->length = 5;
1552 add_control_msg(cm, 0);
1555 static void set_ooolen_flags(struct control_msg_out *cm)
1557 cm->msg.ack_conn.flags = (cm->msg.ack_conn.flags &
1558 (~KP_ACK_CONN_FLAGS_OOO));
1559 cm->msg.ack_conn.flags = (cm->msg.ack_conn.flags |
1560 ooolen_to_flags(cm->msg.ack_conn.length));
1563 /* cmsg_lock must be held */
1564 static void remove_pending_ackconn(struct control_msg_out *cm)
1566 cm->nb->cmlength -= cm->length;
1567 list_del(&(cm->lh));
1569 list_del(&(cm->msg.ack_conn.conn_acks));
1570 kref_put(&(cm->msg.ack_conn.src_in->ref), free_conn);
1571 cm->msg.ack_conn.src_in = 0;
1573 cm->type = 0;
1574 free_control_msg(cm);
1577 /* cmsg_lock must be held */
1578 static void recalc_scheduled_ackconn_size(struct control_msg_out *cm)
1580 cm->nb->cmlength -= cm->length;
1581 cm->length = 6 + ack_conn_len(cm->msg.ack_conn.flags);
1582 cm->nb->cmlength += cm->length;
1585 /* cmsg_lock must be held */
1586 static int _try_merge_ackconn(struct conn *src_in_l,
1587 struct control_msg_out *fromcm, struct control_msg_out *tocm,
1588 int from_newack)
1590 if (ooolen(fromcm->msg.ack_conn.flags) != 0 &&
1591 ooolen(tocm->msg.ack_conn.flags) != 0) {
1592 __u32 tocmseqno = tocm->msg.ack_conn.seqno_ooo;
1593 __u32 tocmlength = tocm->msg.ack_conn.length;
1594 __u32 fromcmseqno = fromcm->msg.ack_conn.seqno_ooo;
1595 __u32 fromcmlength = fromcm->msg.ack_conn.length;
1597 if (tocmseqno == fromcmseqno) {
1598 if (fromcmlength > tocmlength)
1599 tocm->msg.ack_conn.length = fromcmlength;
1600 } else if (seqno_after(fromcmseqno, tocmseqno) &&
1601 seqno_before_eq(fromcmseqno, tocmseqno +
1602 tocmlength)) {
1603 tocm->msg.ack_conn.length = fromcmseqno + fromcmlength -
1604 tocmseqno;
1605 } else if (seqno_before(fromcmseqno, tocmseqno) &&
1606 seqno_after_eq(fromcmseqno, tocmseqno)) {
1607 tocm->msg.ack_conn.seqno_ooo = fromcmseqno;
1608 tocm->msg.ack_conn.length = tocmseqno + tocmlength -
1609 fromcmseqno;
1610 } else {
1611 return 1;
1613 set_ooolen_flags(tocm);
1616 if ((fromcm->msg.ack_conn.flags &
1617 KP_ACK_CONN_FLAGS_SEQNO) != 0) {
1618 if ((tocm->msg.ack_conn.flags & KP_ACK_CONN_FLAGS_SEQNO) == 0)
1619 goto setseqno;
1621 BUG_ON(fromcm->msg.ack_conn.ack_seqno ==
1622 tocm->msg.ack_conn.ack_seqno);
1623 if ((tocm->msg.ack_conn.ack_seqno -
1624 fromcm->msg.ack_conn.ack_seqno) < (1 << 31)) {
1625 BUG_ON(seqno_after(fromcm->msg.ack_conn.seqno,
1626 tocm->msg.ack_conn.seqno));
1627 goto skipseqno;
1630 BUG_ON(seqno_before(fromcm->msg.ack_conn.seqno,
1631 tocm->msg.ack_conn.seqno));
1633 setseqno:
1634 tocm->msg.ack_conn.flags = (tocm->msg.ack_conn.flags |
1635 KP_ACK_CONN_FLAGS_SEQNO);
1636 tocm->msg.ack_conn.seqno = fromcm->msg.ack_conn.seqno;
1637 tocm->msg.ack_conn.ack_seqno = fromcm->msg.ack_conn.ack_seqno;
1639 skipseqno:
1640 if ((fromcm->msg.ack_conn.flags &
1641 KP_ACK_CONN_FLAGS_WINDOW) != 0)
1642 tocm->msg.ack_conn.flags = (tocm->msg.ack_conn.flags |
1643 KP_ACK_CONN_FLAGS_WINDOW);
1647 if (ooolen(fromcm->msg.ack_conn.flags) != 0) {
1648 tocm->msg.ack_conn.seqno_ooo = fromcm->msg.ack_conn.seqno_ooo;
1649 tocm->msg.ack_conn.length = fromcm->msg.ack_conn.length;
1650 set_ooolen_flags(tocm);
1653 if ((fromcm->msg.ack_conn.flags & KP_ACK_CONN_FLAGS_CREDITS) != 0) {
1654 BUG_ON((tocm->msg.ack_conn.flags &
1655 KP_ACK_CONN_FLAGS_CREDITS) != 0);
1656 tocm->msg.ack_conn.decaytime_seqno =
1657 fromcm->msg.ack_conn.decaytime_seqno;
1658 tocm->msg.ack_conn.decaytime =
1659 fromcm->msg.ack_conn.decaytime;
1662 recalc_scheduled_ackconn_size(tocm);
1663 if (from_newack)
1664 kref_put(&(fromcm->msg.ack_conn.src_in->ref), free_conn);
1665 else
1666 remove_pending_ackconn(fromcm);
1668 return 0;
1671 /* cmsg_lock must be held */
1672 static void try_merge_ackconns(struct conn *src_in_l,
1673 struct control_msg_out *cm)
1675 struct list_head *currlh = cm->msg.ack_conn.conn_acks.next;
1677 while (currlh != &(src_in_l->source.in.acks_pending)) {
1678 struct control_msg_out *currcm = container_of(currlh,
1679 struct control_msg_out,
1680 msg.ack_conn.conn_acks);
1681 currlh = currlh->next;
1682 remove_connack_oooflag_ifold(src_in_l, currcm);
1683 _try_merge_ackconn(src_in_l, currcm, cm, 0);
1687 static void mergeadd_ackconn(struct conn *src_in_l, struct control_msg_out *cm)
1689 struct list_head *currlh;
1691 BUG_ON(src_in_l->sourcetype != SOURCE_IN);
1693 mutex_lock(&(cm->nb->cmsg_lock));
1695 currlh = src_in_l->source.in.acks_pending.next;
1697 while (currlh != &(src_in_l->source.in.acks_pending)) {
1698 struct control_msg_out *currcm = container_of(currlh,
1699 struct control_msg_out,
1700 msg.ack_conn.conn_acks);
1702 BUG_ON(currcm->nb != cm->nb);
1703 BUG_ON(currcm->type != MSGTYPE_ACK_CONN);
1704 BUG_ON(cm->msg.ack_conn.src_in != src_in_l);
1705 BUG_ON(currcm->msg.ack_conn.conn_id !=
1706 cm->msg.ack_conn.conn_id);
1708 if (_try_merge_ackconn(src_in_l, cm, currcm, 1) == 0) {
1709 try_merge_ackconns(src_in_l, currcm);
1710 schedule_controlmsg_timer(currcm->nb);
1711 mutex_unlock(&(currcm->nb->cmsg_lock));
1712 return;
1715 currlh = currlh->next;
1718 list_add_tail(&(cm->msg.ack_conn.conn_acks),
1719 &(src_in_l->source.in.acks_pending));
1721 mutex_unlock(&(cm->nb->cmsg_lock));
1723 add_control_msg(cm, 0);
1726 static int try_update_ackconn_seqno(struct conn *src_in_l)
1728 int rc = 1;
1730 mutex_lock(&(src_in_l->source.in.nb->cmsg_lock));
1732 if (list_empty(&(src_in_l->source.in.acks_pending)) == 0) {
1733 struct control_msg_out *cm = container_of(
1734 src_in_l->source.in.acks_pending.next,
1735 struct control_msg_out,
1736 msg.ack_conn.conn_acks);
1737 BUG_ON(cm->nb != src_in_l->source.in.nb);
1738 BUG_ON(cm->type != MSGTYPE_ACK_CONN);
1739 BUG_ON(cm->msg.ack_conn.src_in != src_in_l);
1740 BUG_ON(cm->msg.ack_conn.conn_id !=
1741 src_in_l->reversedir->target.out.conn_id);
1743 cm->msg.ack_conn.flags = (cm->msg.ack_conn.flags |
1744 KP_ACK_CONN_FLAGS_SEQNO |
1745 KP_ACK_CONN_FLAGS_WINDOW);
1746 cm->msg.ack_conn.seqno = src_in_l->source.in.next_seqno;
1748 src_in_l->source.in.ack_seqno++;
1749 cm->msg.ack_conn.ack_seqno = src_in_l->source.in.ack_seqno;
1751 remove_connack_oooflag_ifold(src_in_l, cm);
1752 recalc_scheduled_ackconn_size(cm);
1754 try_merge_ackconns(src_in_l, cm);
1756 rc = 0;
1759 mutex_unlock(&(src_in_l->source.in.nb->cmsg_lock));
1761 return rc;
1764 void send_ack_conn_ifneeded(struct conn *src_in_l)
1766 struct control_msg_out *cm;
1768 BUG_ON(src_in_l->sourcetype != SOURCE_IN);
1770 if (src_in_l->source.in.inorder_ack_needed == 0 &&
1771 ((src_in_l->source.in.window_seqnolimit -
1772 src_in_l->source.in.next_seqno)/2) <
1773 (src_in_l->source.in.window_seqnolimit_remote -
1774 src_in_l->source.in.next_seqno))
1775 return;
1777 if (try_update_ackconn_seqno(src_in_l) == 0)
1778 goto out;
1780 cm = alloc_control_msg(src_in_l->source.in.nb, ACM_PRIORITY_LOW);
1781 if (cm == 0)
1782 return;
1784 cm->type = MSGTYPE_ACK_CONN;
1785 cm->msg.ack_conn.flags = KP_ACK_CONN_FLAGS_SEQNO |
1786 KP_ACK_CONN_FLAGS_WINDOW;
1787 kref_get(&(src_in_l->ref));
1788 cm->msg.ack_conn.src_in = src_in_l;
1789 cm->msg.ack_conn.conn_id = src_in_l->reversedir->target.out.conn_id;
1790 cm->msg.ack_conn.seqno = src_in_l->source.in.next_seqno;
1791 src_in_l->source.in.ack_seqno++;
1792 cm->msg.ack_conn.ack_seqno = src_in_l->source.in.ack_seqno;
1793 cm->length = 6 + ack_conn_len(cm->msg.ack_conn.flags);
1795 mergeadd_ackconn(src_in_l, cm);
1797 out:
1798 src_in_l->source.in.inorder_ack_needed = 0;
1799 src_in_l->source.in.window_seqnolimit_remote =
1800 src_in_l->source.in.window_seqnolimit;
1803 void send_ack_conn_ooo(struct control_msg_out *cm, struct conn *src_in_l,
1804 __u32 conn_id, __u32 seqno_ooo, __u32 length)
1806 cm->type = MSGTYPE_ACK_CONN;
1807 kref_get(&(src_in_l->ref));
1808 BUG_ON(src_in_l->sourcetype != SOURCE_IN);
1809 cm->msg.ack_conn.flags = 0;
1810 cm->msg.ack_conn.src_in = src_in_l;
1811 cm->msg.ack_conn.conn_id = conn_id;
1812 cm->msg.ack_conn.seqno_ooo = seqno_ooo;
1813 cm->msg.ack_conn.length = length;
1814 set_ooolen_flags(cm);
1815 cm->length = 6 + ack_conn_len(cm->msg.ack_conn.flags);
1817 mergeadd_ackconn(src_in_l, cm);
1820 static int try_add_decaytime(struct conn *trgt_out_l, __u16 decaytime)
1822 int rc = 1;
1823 struct conn *src_in = trgt_out_l->reversedir;
1825 mutex_lock(&(trgt_out_l->target.out.nb->cmsg_lock));
1827 if (list_empty(&(src_in->source.in.acks_pending)) == 0) {
1828 struct control_msg_out *cm = container_of(
1829 src_in->source.in.acks_pending.next,
1830 struct control_msg_out,
1831 msg.ack_conn.conn_acks);
1832 BUG_ON(cm->nb != trgt_out_l->target.out.nb);
1833 BUG_ON(cm->type != MSGTYPE_ACK_CONN);
1834 BUG_ON(cm->msg.ack_conn.src_in != trgt_out_l->reversedir);
1835 BUG_ON(cm->msg.ack_conn.conn_id !=
1836 trgt_out_l->target.out.conn_id);
1838 BUG_ON((cm->msg.ack_conn.flags & KP_ACK_CONN_FLAGS_CREDITS) !=
1840 cm->msg.ack_conn.flags = (cm->msg.ack_conn.flags |
1841 KP_ACK_CONN_FLAGS_CREDITS);
1842 cm->msg.ack_conn.decaytime_seqno =
1843 trgt_out_l->target.out.decaytime_seqno;
1844 cm->msg.ack_conn.decaytime = decaytime;
1845 recalc_scheduled_ackconn_size(cm);
1847 rc = 0;
1850 mutex_unlock(&(trgt_out_l->target.out.nb->cmsg_lock));
1852 return rc;
1855 void send_decaytime(struct conn *trgt_out_l, int force, __u16 decaytime)
1857 struct control_msg_out *cm;
1859 if (try_add_decaytime(trgt_out_l, decaytime) == 0)
1860 goto out;
1862 if (force == 0)
1863 return;
1865 cm = alloc_control_msg(trgt_out_l->target.out.nb, ACM_PRIORITY_LOW);
1867 if (cm == 0)
1868 return;
1870 cm->type = MSGTYPE_ACK_CONN;
1871 cm->msg.ack_conn.flags = KP_ACK_CONN_FLAGS_CREDITS;
1872 kref_get(&(trgt_out_l->reversedir->ref));
1873 BUG_ON(trgt_out_l->targettype != TARGET_OUT);
1874 cm->msg.ack_conn.src_in = trgt_out_l->reversedir;
1875 cm->msg.ack_conn.conn_id = trgt_out_l->target.out.conn_id;
1876 cm->msg.ack_conn.decaytime_seqno =
1877 trgt_out_l->target.out.decaytime_seqno;
1878 cm->msg.ack_conn.decaytime = decaytime;
1880 cm->length = 6 + ack_conn_len(cm->msg.ack_conn.flags);
1881 mergeadd_ackconn(trgt_out_l, cm);
1883 out:
1884 trgt_out_l->target.out.decaytime_last = decaytime;
1885 trgt_out_l->target.out.decaytime_seqno =
1886 (trgt_out_l->target.out.decaytime_seqno + 1) % 64;
1887 trgt_out_l->target.out.decaytime_send_allowed = 0;
1890 void free_ack_conns(struct conn *src_in_l)
1892 int changed = 0;
1893 mutex_lock(&(src_in_l->source.in.nb->cmsg_lock));
1894 while (list_empty(&(src_in_l->source.in.acks_pending)) == 0) {
1895 struct list_head *currlh =
1896 src_in_l->source.in.acks_pending.next;
1897 struct control_msg_out *currcm = container_of(currlh,
1898 struct control_msg_out,
1899 msg.ack_conn.conn_acks);
1901 remove_pending_ackconn(currcm);
1902 changed = 1;
1904 if (changed)
1905 schedule_controlmsg_timer(src_in_l->source.in.nb);
1906 mutex_unlock(&(src_in_l->source.in.nb->cmsg_lock));
1909 void send_connect_success(struct control_msg_out *cm, __u32 rcvd_conn_id,
1910 __u32 gen_conn_id, __u32 init_seqno, struct conn *src_in)
1912 cm->type = MSGTYPE_CONNECT_SUCCESS;
1913 cm->msg.connect_success.rcvd_conn_id = rcvd_conn_id;
1914 cm->msg.connect_success.gen_conn_id = gen_conn_id;
1915 cm->msg.connect_success.init_seqno = init_seqno;
1916 kref_get(&(src_in->ref));
1917 cm->msg.connect_success.src_in = src_in;
1918 cm->length = 16;
1919 add_control_msg(cm, 0);
1922 void send_connect_nb(struct control_msg_out *cm, __u32 conn_id,
1923 __u32 init_seqno, struct conn *src_in)
1925 cm->type = MSGTYPE_CONNECT;
1926 cm->msg.connect.conn_id = conn_id;
1927 cm->msg.connect.init_seqno = init_seqno;
1928 kref_get(&(src_in->ref));
1929 BUG_ON(src_in->sourcetype != SOURCE_IN);
1930 cm->msg.connect.src_in = src_in;
1931 cm->length = 12;
1932 add_control_msg(cm, 0);
1935 #warning todo ref to buf instead
1936 void send_conndata(struct control_msg_out *cm, __u32 conn_id, __u32 seqno,
1937 char *data_orig, char *data, __u32 datalen)
1939 cm->type = MSGTYPE_CONNDATA;
1940 cm->msg.conn_data.conn_id = conn_id;
1941 cm->msg.conn_data.seqno = seqno;
1942 cm->msg.conn_data.data_orig = data_orig;
1943 cm->msg.conn_data.data = data;
1944 cm->msg.conn_data.datalen = datalen;
1945 cm->length = 11 + datalen;
1946 add_control_msg(cm, 0);
1949 void send_connid_unknown(struct neighbor *nb, __u32 conn_id)
1951 unsigned long iflags;
1952 char *ret;
1953 struct unknownconnid_matchparam ucm;
1955 struct control_msg_out *cm = alloc_control_msg(nb, ACM_PRIORITY_HIGH);
1957 if (unlikely(cm == 0))
1958 return;
1960 cm->type = MSGTYPE_CONNID_UNKNOWN;
1961 cm->msg.reset_connidunknown.conn_id_unknown = conn_id;
1962 cm->length = 5;
1964 ucm.nb = nb;
1965 ucm.conn_id = conn_id;
1967 spin_lock_irqsave(&unknown_connids_lock, iflags);
1968 ret = htable_get(&unknown_connids, ucm_to_key(&ucm), &ucm);
1969 if (ret == 0)
1970 htable_insert(&unknown_connids, (char *) cm, ucm_to_key(&ucm));
1971 spin_unlock_irqrestore(&unknown_connids_lock, iflags);
1973 if (ret != 0) {
1974 struct control_msg_out *cm2 = (struct control_msg_out *) ret;
1976 BUG_ON(cm2->type != MSGTYPE_RESET_CONN &&
1977 cm2->type != MSGTYPE_CONNID_UNKNOWN);
1979 kref_put(&(cm2->ref), cmsg_kref_free);
1981 cm->type = 0;
1982 free_control_msg(cm);
1983 } else {
1984 add_control_msg(cm, 0);
1989 static int matches_connretrans(void *htentry, void *searcheditem)
1991 struct control_retrans *cr = (struct control_retrans *) htentry;
1992 struct retransmit_matchparam *rm = (struct retransmit_matchparam *)
1993 searcheditem;
1995 return rm->nb == cr->nb && rm->seqno == cr->seqno;
1998 static int matches_unknownconnid(void *htentry, void *searcheditem)
2000 struct control_msg_out *cm = (struct control_msg_out *) htentry;
2002 struct unknownconnid_matchparam *ucm =
2003 (struct unknownconnid_matchparam *)searcheditem;
2005 BUG_ON(cm->type != MSGTYPE_RESET_CONN &&
2006 cm->type != MSGTYPE_CONNID_UNKNOWN);
2008 return ucm->nb == cm->nb && ucm->conn_id ==
2009 cm->msg.reset_connidunknown.conn_id_unknown;
2012 void __init cor_kgen_init(void)
2014 controlmsg_slab = kmem_cache_create("cor_controlmsg",
2015 sizeof(struct control_msg_out), 8, 0, 0);
2016 controlretrans_slab = kmem_cache_create("cor_controlretransmsg",
2017 sizeof(struct control_retrans), 8, 0, 0);
2018 htable_init(&retransmits, matches_connretrans,
2019 offsetof(struct control_retrans, htab_entry),
2020 offsetof(struct control_retrans, ref));
2021 htable_init(&unknown_connids, matches_unknownconnid,
2022 offsetof(struct control_msg_out,
2023 msg.reset_connidunknown.htab_entry),
2024 offsetof(struct control_msg_out, ref));
2027 MODULE_LICENSE("GPL");