1 | /*
|
---|
2 | * Copyright (c) 2011 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup tcp
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 |
|
---|
33 | /**
|
---|
34 | * @file Global segment receive queue
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <adt/prodcons.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <io/log.h>
|
---|
40 | #include <stdlib.h>
|
---|
41 | #include <thread.h>
|
---|
42 | #include "conn.h"
|
---|
43 | #include "rqueue.h"
|
---|
44 | #include "state.h"
|
---|
45 | #include "tcp_type.h"
|
---|
46 |
|
---|
47 | static prodcons_t rqueue;
|
---|
48 |
|
---|
49 | /** Initialize segment receive queue. */
|
---|
50 | void tcp_rqueue_init(void)
|
---|
51 | {
|
---|
52 | prodcons_initialize(&rqueue);
|
---|
53 | }
|
---|
54 |
|
---|
55 | /** Bounce segment directy into receive queue without constructing the PDU.
|
---|
56 | *
|
---|
57 | * This is for testing purposes only.
|
---|
58 | *
|
---|
59 | * @param sp Socket pair, oriented for transmission
|
---|
60 | * @param seg Segment
|
---|
61 | */
|
---|
62 | void tcp_rqueue_bounce_seg(tcp_sockpair_t *sp, tcp_segment_t *seg)
|
---|
63 | {
|
---|
64 | tcp_sockpair_t rident;
|
---|
65 |
|
---|
66 | log_msg(LVL_DEBUG, "tcp_rqueue_bounce_seg()");
|
---|
67 |
|
---|
68 | /* Reverse the identification */
|
---|
69 | tcp_sockpair_flipped(sp, &rident);
|
---|
70 |
|
---|
71 | tcp_rqueue_insert_seg(&rident, seg);
|
---|
72 | }
|
---|
73 |
|
---|
74 | /** Insert segment into receive queue.
|
---|
75 | *
|
---|
76 | * @param sp Socket pair, oriented for reception
|
---|
77 | * @param seg Segment
|
---|
78 | */
|
---|
79 | void tcp_rqueue_insert_seg(tcp_sockpair_t *sp, tcp_segment_t *seg)
|
---|
80 | {
|
---|
81 | tcp_rqueue_entry_t *rqe;
|
---|
82 | log_msg(LVL_DEBUG, "tcp_rqueue_insert_seg()");
|
---|
83 |
|
---|
84 | rqe = calloc(1, sizeof(tcp_rqueue_entry_t));
|
---|
85 | if (rqe == NULL) {
|
---|
86 | log_msg(LVL_ERROR, "Failed allocating RQE.");
|
---|
87 | return;
|
---|
88 | }
|
---|
89 |
|
---|
90 | rqe->sp = *sp;
|
---|
91 | rqe->seg = seg;
|
---|
92 |
|
---|
93 | prodcons_produce(&rqueue, &rqe->link);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /** Receive queue handler thread. */
|
---|
97 | static void tcp_rqueue_thread(void *arg)
|
---|
98 | {
|
---|
99 | link_t *link;
|
---|
100 | tcp_rqueue_entry_t *rqe;
|
---|
101 |
|
---|
102 | log_msg(LVL_DEBUG, "tcp_rqueue_thread()");
|
---|
103 |
|
---|
104 | while (true) {
|
---|
105 | link = prodcons_consume(&rqueue);
|
---|
106 | rqe = list_get_instance(link, tcp_rqueue_entry_t, link);
|
---|
107 |
|
---|
108 | tcp_as_segment_arrived(&rqe->sp, rqe->seg);
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | /** Start receive queue handler thread. */
|
---|
113 | void tcp_rqueue_thread_start(void)
|
---|
114 | {
|
---|
115 | thread_id_t tid;
|
---|
116 | int rc;
|
---|
117 |
|
---|
118 | log_msg(LVL_DEBUG, "tcp_rqueue_thread_start()");
|
---|
119 |
|
---|
120 | rc = thread_create(tcp_rqueue_thread, NULL, "rqueue", &tid);
|
---|
121 | if (rc != EOK) {
|
---|
122 | log_msg(LVL_ERROR, "Failed creating rqueue thread.");
|
---|
123 | return;
|
---|
124 | }
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * @}
|
---|
129 | */
|
---|