credit system, reverse_connid_table insert bugfix
[cor_2_6_31.git] / net / cor / kpacket_parse.c
blobd58c511e587a61296fbea9dafe969c263698280d
1 /*
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
18 * 02110-1301, USA.
21 #include <asm/byteorder.h>
23 #include "cor.h"
25 static __u64 pull_u64(struct sk_buff *skb, int convbo)
27 char *ptr = cor_pull_skb(skb, 8);
29 __u64 ret = 0;
31 BUG_ON(0 == ptr);
33 ((char *)&ret)[0] = ptr[0];
34 ((char *)&ret)[1] = ptr[1];
35 ((char *)&ret)[2] = ptr[2];
36 ((char *)&ret)[3] = ptr[3];
37 ((char *)&ret)[4] = ptr[4];
38 ((char *)&ret)[5] = ptr[5];
39 ((char *)&ret)[6] = ptr[6];
40 ((char *)&ret)[7] = ptr[7];
42 if (convbo)
43 return be64_to_cpu(ret);
44 return ret;
47 static __u32 pull_u32(struct sk_buff *skb, int convbo)
49 char *ptr = cor_pull_skb(skb, 4);
51 __u32 ret = 0;
53 BUG_ON(0 == ptr);
55 ((char *)&ret)[0] = ptr[0];
56 ((char *)&ret)[1] = ptr[1];
57 ((char *)&ret)[2] = ptr[2];
58 ((char *)&ret)[3] = ptr[3];
60 if (convbo)
61 return be32_to_cpu(ret);
62 return ret;
65 static __u16 pull_u16(struct sk_buff *skb, int convbo)
67 char *ptr = cor_pull_skb(skb, 2);
69 __u16 ret = 0;
71 BUG_ON(0 == ptr);
73 ((char *)&ret)[0] = ptr[0];
74 ((char *)&ret)[1] = ptr[1];
76 if (convbo)
77 return be16_to_cpu(ret);
78 return ret;
81 static __u8 pull_u8(struct sk_buff *skb)
83 char *ptr = cor_pull_skb(skb, 1);
84 BUG_ON(0 == ptr);
85 return *ptr;
88 static void pong_rcvd(struct conn *rconn)
90 struct neighbor *nb = rconn->source.in.nb;
91 if (atomic_read(&(rconn->source.in.pong_awaiting)) != 0) {
92 mutex_lock(&(nb->conn_list_lock));
93 if (atomic_read(&(rconn->source.in.pong_awaiting)) == 0)
94 goto unlock;
96 atomic_set(&(rconn->source.in.pong_awaiting), 0);
97 nb->pong_conns_expected--;
98 unlock:
99 mutex_unlock(&(nb->conn_list_lock));
103 static void ping_all_conns(struct neighbor *nb)
105 struct conn *rconn;
107 mutex_lock(&(nb->conn_list_lock));
109 if (list_empty(&(nb->snd_conn_list))) {
110 BUG_ON(nb->num_send_conns != 0);
111 goto out;
114 rconn = container_of(nb->snd_conn_list.next, struct conn,
115 target.out.nb_list);
117 BUG_ON(rconn->targettype != TARGET_OUT);
119 nb->next_ping_conn = rconn;
120 nb->ping_conns_remaining = nb->num_send_conns;
121 kref_get(&(rconn->ref));
123 out:
124 mutex_unlock(&(nb->conn_list_lock));
127 static void discard_ack_conn(struct neighbor *nb, struct sk_buff *skb)
129 pull_u32(skb, 1); /* seqno */
130 pull_u8(skb); /* window */
133 static void discard_set_conn_credits(struct neighbor *nb, struct sk_buff *skb)
135 pull_u32(skb, 1); /* credit_rate */
138 static void parse_ack_conn(struct neighbor *nb, struct sk_buff *skb,
139 __u32 kpacket_seqno, struct conn *rconn)
141 __u32 seqno = pull_u32(skb, 1);
142 __u8 window = pull_u8(skb);
144 pong_rcvd(rconn);
146 conn_ack_rcvd(kpacket_seqno, rconn->reversedir, seqno, window, 0, 0);
149 static void discard_ack_conn_ooo(struct neighbor *nb, struct sk_buff *skb)
151 pull_u32(skb, 1); /* seqno */
152 pull_u8(skb); /* window */
153 pull_u32(skb, 1); /* seqno_ooo */
154 pull_u32(skb, 1); /* length */
157 static void parse_ack_conn_ooo(struct neighbor *nb, struct sk_buff *skb,
158 __u32 kpacket_seqno, struct conn *rconn)
160 __u32 seqno = pull_u32(skb, 1);
161 __u8 window = pull_u8(skb);
162 __u32 seqno_ooo = pull_u32(skb, 1);
163 __u32 length = pull_u32(skb, 1);
165 pong_rcvd(rconn);
167 conn_ack_rcvd(kpacket_seqno, rconn->reversedir, seqno, window,
168 seqno_ooo, length);
171 static void discard_conn_success(struct neighbor *nb, struct sk_buff *skb)
173 __u32 conn_id = pull_u32(skb, 1);
174 struct control_msg_out *cm = alloc_control_msg(nb, ACM_PRIORITY_HIGH);
176 if (unlikely(cm == 0))
177 send_ping_all_conns(nb);
178 else
179 send_reset_conn(cm, conn_id);
182 static void parse_conn_success(struct neighbor *nb, struct sk_buff *skb,
183 __u32 seqno, struct conn *rconn)
185 struct conn *sconn = rconn->reversedir;
187 __u32 conn_id = pull_u32(skb, 1);
189 BUG_ON(sconn == 0);
191 mutex_lock(&(sconn->rcv_lock));
193 if (unlikely(unlikely(sconn->targettype != TARGET_OUT) ||
194 unlikely(sconn->target.out.nb != nb) ||
195 unlikely(sconn->target.out.conn_id != 0 &&
196 sconn->target.out.conn_id != conn_id))) {
197 goto reset;
200 if (likely(sconn->target.out.conn_id == 0)) {
201 sconn->target.out.conn_id = conn_id;
203 if (unlikely(atomic_read(&(sconn->isreset)) != 0))
204 goto reset;
206 insert_reverse_connid(sconn);
209 flush_out(sconn);
211 if (0) {
212 struct control_msg_out *cm;
213 reset:
214 cm = alloc_control_msg(nb, ACM_PRIORITY_HIGH);
215 if (unlikely(cm == 0))
216 send_ping_all_conns(nb);
217 else
218 send_reset_conn(cm, conn_id);
220 mutex_unlock(&(sconn->rcv_lock));
223 static void parse_reset(struct neighbor *nb, struct sk_buff *skb, __u32 seqno,
224 struct conn *rconn)
226 #warning todo ping conn waiting?
227 atomic_cmpxchg(&(rconn->reversedir->isreset), 0, 1);
228 reset_conn(rconn);
231 static void parse_ping_conn(struct neighbor *nb, struct sk_buff *skb,
232 __u32 seqno, struct conn *rconn)
234 struct control_msg_out *cm = alloc_control_msg(nb, ACM_PRIORITY_MED);
236 if (unlikely(cm == 0))
237 return;
239 mutex_lock(&(rconn->rcv_lock));
240 #warning todo set window
241 send_ack_conn(cm, rconn->reversedir->target.out.conn_id,
242 rconn->source.in.next_seqno, enc_window(65536));
243 mutex_unlock(&(rconn->rcv_lock));
246 static void parse_set_conn_credits(struct neighbor *nb, struct sk_buff *skb,
247 __u32 seqno, struct conn *rconn)
249 __u32 crate_in = pull_u32(skb, 1);
250 set_conn_in_crate(rconn, crate_in);
253 static void conn_cmd(struct neighbor *nb, struct sk_buff *skb, __u32 seqno,
254 __u8 code, void (*parsefunc)(struct neighbor *nb,
255 struct sk_buff *skb, __u32 seqno, struct conn *rconn),
256 void (*readdiscardfunc)(struct neighbor *nb,
257 struct sk_buff *skb))
259 __u32 conn_id = pull_u32(skb, 1);
260 struct conn *rconn = get_conn(conn_id);
262 if (unlikely(rconn == 0))
263 goto err;
265 BUG_ON(rconn->sourcetype != SOURCE_IN);
266 BUG_ON(rconn->reversedir == 0);
268 if (unlikely(rconn->source.in.nb != nb))
269 goto err;
271 parsefunc(nb, skb, seqno, rconn);
272 if (0) {
273 struct control_msg_out *cm;
274 err:
275 cm = alloc_control_msg(nb, ACM_PRIORITY_MED);
276 if (likely(cm != 0))
277 send_connid_unknown(cm, conn_id);
279 if (readdiscardfunc != 0)
280 readdiscardfunc(nb, skb);
282 if (likely(rconn != 0))
283 kref_put(&(rconn->ref), free_conn);
286 static void parse_conndata(struct neighbor *nb, struct sk_buff *skb)
288 __u32 conn_id = pull_u32(skb, 1);
289 __u32 seqno = pull_u32(skb, 1);
290 __u16 datalength = pull_u16(skb, 1);
291 char *data = cor_pull_skb(skb, datalength);
293 BUG_ON(data == 0);
295 conn_rcv_buildskb(data, datalength, conn_id, seqno);
298 static void parse_connect(struct neighbor *nb, struct sk_buff *skb)
300 struct conn *rconn;
301 __u32 conn_id = pull_u32(skb, 1);
302 struct control_msg_out *cm = alloc_control_msg(nb, ACM_PRIORITY_HIGH);
304 if (unlikely(cm == 0))
305 return;
307 rconn = alloc_conn(GFP_KERNEL);
309 if (unlikely(rconn == 0))
310 goto err;
312 if (unlikely(conn_init_out(rconn->reversedir, nb)))
313 goto err;
315 rconn->reversedir->target.out.conn_id = conn_id;
316 insert_reverse_connid(rconn->reversedir);
318 send_connect_success(cm, rconn->reversedir->target.out.conn_id,
319 rconn->source.in.conn_id);
321 if (0) {
322 err:
323 send_reset_conn(cm, conn_id);
327 static void parse_set_credits(struct neighbor *nb, struct sk_buff *skb)
329 __u64 credits = pull_u64(skb, 1);
330 __s32 creditrate_initial = pull_u32(skb, 1);
331 __u32 creditrate_earning = pull_u32(skb, 1);
332 __u32 creditrate_spending = pull_u32(skb, 1);
334 set_credits(nb, credits, creditrate_initial, creditrate_earning,
335 creditrate_spending);
338 #warning todo set window on connect/connect_success
339 static void kernel_packet2(struct neighbor *nb, struct sk_buff *skb,
340 __u32 seqno1)
342 int ack = 0;
344 while (1) {
345 struct conn *conn;
346 __u32 seqno2;
348 __u32 conn_id;
350 __u32 cookie;
351 __u32 respdelay;
353 __u32 max_cmsg_dly;
355 __u8 *codeptr = cor_pull_skb(skb, 1);
356 __u8 code;
358 if (codeptr == 0)
359 break;
361 code = *codeptr;
363 switch (code) {
364 case KP_PADDING:
365 break;
366 case KP_PING:
367 cookie = pull_u32(skb, 0);
368 send_pong(nb, cookie);
369 break;
370 case KP_PONG:
371 cookie = pull_u32(skb, 0);
372 respdelay = pull_u32(skb, 1);
373 ping_resp(nb, cookie, respdelay);
374 ack = 1;
375 break;
376 case KP_ACK:
377 seqno2 = pull_u32(skb, 1);
378 kern_ack_rcvd(nb, seqno2);
379 break;
380 case KP_ACK_CONN:
381 conn_cmd(nb, skb, seqno1, code, parse_ack_conn,
382 discard_ack_conn);
383 ack = 1;
384 break;
385 case KP_ACK_CONN_OOO:
386 conn_cmd(nb, skb, seqno1, code, parse_ack_conn_ooo,
387 discard_ack_conn_ooo);
388 ack = 1;
389 break;
390 case KP_CONNECT:
391 parse_connect(nb, skb);
392 ack = 1;
393 break;
394 case KP_CONNECT_SUCCESS:
395 conn_cmd(nb, skb, seqno1, code, parse_conn_success,
396 discard_conn_success);
397 ack = 1;
398 break;
399 case KP_CONN_DATA:
400 parse_conndata(nb, skb);
401 break;
402 case KP_PING_CONN:
403 conn_id = pull_u32(skb, 1);
404 conn_cmd(nb, skb, seqno1, code, parse_ping_conn, 0);
405 ack = 1;
406 break;
407 case KP_RESET_CONN:
408 conn_cmd(nb, skb, seqno1, code, parse_reset, 0);
409 ack = 1;
410 break;
411 case KP_CONNID_UNKNOWN:
412 conn_id = pull_u32(skb, 1);
413 conn = get_conn_reverse(nb, conn_id);
414 if (conn != 0) {
415 BUG_ON(conn->reversedir->sourcetype !=
416 SOURCE_IN);
417 pong_rcvd(conn->reversedir);
418 atomic_cmpxchg(&(conn->isreset), 0, 1);
419 reset_conn(conn);
420 conn = 0;
422 ack = 1;
423 break;
424 case KP_PING_ALL_CONNS:
425 ping_all_conns(nb);
426 ack = 1;
427 break;
428 case KP_SET_MAX_CMSG_DELAY:
429 max_cmsg_dly = pull_u32(skb, 1);
430 if (((__u64) max_cmsg_dly) * 1000 > ((__u64)
431 (max_cmsg_dly * 1000)))
432 max_cmsg_dly = 400000000;
433 else
434 max_cmsg_dly *= 100;
435 atomic_set(&(nb->max_remote_cmsg_delay), max_cmsg_dly);
436 ack = 1;
437 break;
438 case KP_SET_CREDITS:
439 parse_set_credits(nb, skb);
440 ack = 1;
441 break;
442 case KP_SET_CONN_CREDITS:
443 conn_cmd(nb, skb, seqno1, code, parse_set_conn_credits,
444 discard_set_conn_credits);
445 ack = 1;
446 break;
447 default:
448 BUG();
452 if (ack)
453 send_ack(nb, seqno1);
456 void kernel_packet(struct neighbor *nb, struct sk_buff *skb, __u32 seqno)
458 struct sk_buff *skb2 = skb_clone(skb, __GFP_DMA | GFP_KERNEL);
460 while (1) {
461 __u8 *codeptr = cor_pull_skb(skb2, 1);
462 __u8 code;
464 char *lengthptr;
465 __u32 length;
467 if (codeptr == 0)
468 break;
469 code = *codeptr;
471 switch (code) {
472 case KP_PADDING:
473 break;
474 case KP_PING:
475 if (cor_pull_skb(skb2, 4) == 0)
476 goto discard;
477 break;
478 case KP_PONG:
479 if (cor_pull_skb(skb2, 8) == 0)
480 goto discard;
481 break;
482 case KP_ACK:
483 if (cor_pull_skb(skb2, 4) == 0)
484 goto discard;
485 break;
486 case KP_ACK_CONN:
487 if (cor_pull_skb(skb2, 9) == 0)
488 goto discard;
489 break;
490 case KP_ACK_CONN_OOO:
491 if (cor_pull_skb(skb2, 17) == 0)
492 goto discard;
493 break;
494 case KP_CONNECT:
495 if (cor_pull_skb(skb2, 4) == 0)
496 goto discard;
497 break;
498 case KP_CONNECT_SUCCESS:
499 if (cor_pull_skb(skb2, 8) == 0)
500 goto discard;
501 break;
502 case KP_CONN_DATA:
503 if (cor_pull_skb(skb2, 8) == 0)
504 goto discard;
505 lengthptr = cor_pull_skb(skb2, 2);
506 if (lengthptr == 0)
507 goto discard;
508 length = ntohs(*((__u16 *)lengthptr));
509 if (cor_pull_skb(skb2, length) == 0)
510 goto discard;
511 break;
512 case KP_PING_CONN:
513 case KP_RESET_CONN:
514 case KP_CONNID_UNKNOWN:
515 if (cor_pull_skb(skb2, 4) == 0)
516 goto discard;
517 break;
518 case KP_PING_ALL_CONNS:
519 break;
520 case KP_SET_MAX_CMSG_DELAY:
521 if (cor_pull_skb(skb2, 4) == 0)
522 goto discard;
523 break;
524 case KP_SET_CREDITS:
525 if (cor_pull_skb(skb2, 20) == 0)
526 goto discard;
527 break;
528 case KP_SET_CONN_CREDITS:
529 if (cor_pull_skb(skb2, 8) == 0)
530 goto discard;
531 break;
532 default:
533 goto discard;
536 kfree_skb(skb2);
537 kernel_packet2(nb, skb, seqno);
538 kfree_skb(skb);
539 return;
540 discard:
541 kfree_skb(skb2);
542 kfree_skb(skb);