2 * Connection oriented routing
3 * Copyright (C) 2007-2008 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 */
27 #define MSGTYPE_CONNECT 2
28 #define MSGTYPE_CONNECT_SUCCESS 3
29 #define MSGTYPE_RESET_CONN 4
30 #define MSGTYPE_CONNDATA 5
35 struct control_msg_out
{
75 struct control_msg_out
*alloc_control_msg(void)
77 return kmalloc(sizeof(struct control_msg_out
), GFP_KERNEL
);
80 void free_control_msg(struct control_msg_out
*cm
)
85 static int add_ack(struct sk_buff
*skb
, struct control_msg_out
*cm
,
93 dst
= skb_put(skb
, 9);
97 put_u32(dst
+ 1, cm
->msg
.ack
.conn_id
, 1);
98 put_u32(dst
+ 5, cm
->msg
.ack
.seqno
, 1);
103 static int add_connect(struct sk_buff
*skb
, struct control_msg_out
*cm
,
111 dst
= skb_put(skb
, 5);
115 put_u32(dst
+ 1, cm
->msg
.connect
.conn_id
, 1);
120 static int add_connect_success(struct sk_buff
*skb
, struct control_msg_out
*cm
,
128 dst
= skb_put(skb
, 9);
131 dst
[0] = KP_CONNECT_SUCCESS
;
132 put_u32(dst
+ 1, cm
->msg
.connect_success
.rcvd_conn_id
, 1);
133 put_u32(dst
+ 5, cm
->msg
.connect_success
.rcvd_conn_id
, 1);
138 static int add_reset_conn(struct sk_buff
*skb
, struct control_msg_out
*cm
,
146 dst
= skb_put(skb
, 5);
149 dst
[0] = KP_RESET_CONN
;
150 put_u32(dst
+ 1, cm
->msg
.reset
.conn_id
, 1);
155 static int add_conndata(struct sk_buff
*skb
, struct control_msg_out
*cm
,
160 int totallen
= cm
->msg
.conn_data
.datalen
+ 5;
161 int putlen
= min(totallen
, spaceleft
);
163 if (spaceleft
< 25 && totallen
> 25)
166 dst
= skb_put(skb
, putlen
);
169 dst
[0] = KP_RESET_CONN
;
170 put_u32(dst
+ 1, cm
->msg
.conn_data
.conn_id
, 1);
171 put_u32(dst
+ 5, cm
->msg
.conn_data
.seqno
, 1);
173 memcpy(dst
+ 9, cm
->msg
.conn_data
.data
, putlen
- 9);
174 cm
->msg
.conn_data
.datalen
-= (putlen
- 9);
176 if (cm
->msg
.conn_data
.datalen
== 0) {
177 kfree(cm
->msg
.conn_data
.data_orig
);
178 free_control_msg(cm
);
180 send_conndata(cm
, cm
->nb
, cm
->msg
.conn_data
.conn_id
,
181 cm
->msg
.conn_data
.seqno
,
182 cm
->msg
.conn_data
.data_orig
,
183 cm
->msg
.conn_data
.data
,
184 cm
->msg
.conn_data
.datalen
);
191 static int add_message(struct sk_buff
*skb
, struct control_msg_out
*cm
,
198 rc
= add_ack(skb
, cm
, spaceleft
);
200 case MSGTYPE_CONNECT
:
201 rc
= add_connect(skb
, cm
, spaceleft
);
203 case MSGTYPE_CONNECT_SUCCESS
:
204 rc
= add_connect_success(skb
, cm
, spaceleft
);
206 case MSGTYPE_RESET_CONN
:
207 rc
= add_reset_conn(skb
, cm
, spaceleft
);
209 case MSGTYPE_CONNDATA
:
211 rc
= add_conndata(skb
, cm
, spaceleft
);
217 free_control_msg(cm
);
222 static void padding(struct sk_buff
*skb
, int length
)
224 char *dst
= skb_put(skb
, length
);
226 memset(dst
, KP_PADDING
, length
);
229 static void _send_messages(struct neighbor
*nb
, struct sk_buff
*skb
,
234 while (!list_empty((struct list_head
*) &(nb
->control_msgs_out
))) {
237 struct control_msg_out
*cm
= (struct control_msg_out
*)
238 nb
->control_msgs_out
.next
;
239 list_del((struct list_head
*) cm
);
241 rc
= add_message(skb
, cm
, spaceleft
- length
);
244 length
+= cm
->length
;
248 nb
->length
-= length
;
250 padding(skb
, spaceleft
- length
);
251 send_packet(skb
, nb
);
254 static void send_messages(struct neighbor
*nb
, int allmsgs
)
256 int targetmss
= mss(nb
);
258 BUG_ON(list_empty(&(nb
->control_msgs_out
)) && (nb
->length
!= 0));
259 BUG_ON((list_empty(&(nb
->control_msgs_out
)) == 0) && (nb
->length
== 0));
261 while (likely(list_empty(&(nb
->control_msgs_out
)) == 0) &&
262 (nb
->length
>= targetmss
|| allmsgs
)) {
264 int size
= targetmss
;
265 if (size
> nb
->length
)
267 skb
= create_packet_kernel(nb
, size
, GFP_KERNEL
);
269 printk(KERN_ERR
"cor: send_messages: cannot allocate "
270 "skb (out of memory?)");
273 _send_messages(nb
, skb
, size
);
276 BUG_ON(nb
->length
< 0);
279 static void controlmsg_timerfunc(struct work_struct
*work
)
281 struct neighbor
*nb
= container_of(to_delayed_work(work
),
282 struct neighbor
, cmsg_timer
);
283 __u64 jiffies
= get_jiffies_64();
285 mutex_lock(&(nb
->cmsg_lock
));
287 if (nb
->timeout
> jiffies
) {
288 INIT_DELAYED_WORK(&(nb
->cmsg_timer
), controlmsg_timerfunc
);
289 schedule_delayed_work(&(nb
->cmsg_timer
), nb
->timeout
- jiffies
);
293 send_messages(nb
, 1);
294 schedule_controlmsg_timerfunc(nb
);
297 mutex_unlock(&(nb
->cmsg_lock
));
300 void schedule_controlmsg_timerfunc(struct neighbor
*nb
)
302 int target_delay_ms
= 1000;
303 int target_delay_jiffies
= msecs_to_jiffies(target_delay_ms
);
304 __u64 jiffies
= get_jiffies_64();
307 nb
->timeout
+= target_delay_jiffies
;
309 delay
= nb
->timeout
- jiffies
;
312 nb
->timeout
= jiffies
;
315 INIT_DELAYED_WORK(&(nb
->cmsg_timer
), controlmsg_timerfunc
);
316 schedule_delayed_work(&(nb
->cmsg_timer
), delay
);
319 static void add_control_msg(struct control_msg_out
*msg
, struct neighbor
*nb
)
323 printk(KERN_ERR
"add_msg %d", msg
->type
);
325 mutex_lock(&(nb
->cmsg_lock
));
328 nb
->length
+= msg
->length
;
329 list_add_tail((struct list_head
*) msg
,
330 (struct list_head
*) &(nb
->control_msgs_out
));
332 if (unlikely(nb
->length
>= mss(nb
)))
333 send_messages(nb
, 0);
335 mutex_unlock(&(nb
->cmsg_lock
));
338 void send_reset_conn(struct control_msg_out
*cm
, struct neighbor
*nb
,
341 cm
->type
= MSGTYPE_RESET_CONN
;
342 cm
->msg
.reset
.conn_id
= conn_id
;
344 add_control_msg(cm
, nb
);
347 void send_ack(struct control_msg_out
*cm
, struct neighbor
*nb
, __u32 conn_id
,
350 cm
->type
= MSGTYPE_ACK
;
351 cm
->msg
.ack
.conn_id
= conn_id
;
352 cm
->msg
.ack
.seqno
= seqno
;
354 add_control_msg(cm
, nb
);
357 void send_connect_success(struct control_msg_out
*cm
, struct neighbor
*nb
,
358 __u32 rcvd_conn_id
, __u32 gen_conn_id
)
360 cm
->type
= MSGTYPE_CONNECT_SUCCESS
;
361 cm
->msg
.connect_success
.rcvd_conn_id
= rcvd_conn_id
;
362 cm
->msg
.connect_success
.gen_conn_id
= gen_conn_id
;
364 add_control_msg(cm
, nb
);
367 void send_connect_nb(struct control_msg_out
*cm
, struct neighbor
*nb
,
370 cm
->type
= MSGTYPE_CONNECT
;
371 cm
->msg
.connect
.conn_id
= conn_id
;
373 add_control_msg(cm
, nb
);
376 void send_conndata(struct control_msg_out
*cm
, struct neighbor
*nb
,
377 __u32 conn_id
, __u32 seqno
, char *data_orig
, char *data
,
380 printk(KERN_ERR
"send_conndata");
381 cm
->type
= MSGTYPE_CONNDATA
;
382 cm
->msg
.conn_data
.conn_id
= conn_id
;
383 cm
->msg
.conn_data
.seqno
= seqno
;
384 cm
->msg
.conn_data
.data_orig
= data_orig
;
385 cm
->msg
.conn_data
.data
= data
;
386 cm
->msg
.conn_data
.datalen
= datalen
;
387 cm
->length
= 9 + datalen
;
388 add_control_msg(cm
, nb
);