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 Network condition simulator
|
---|
35 | *
|
---|
36 | * Simulate network conditions for testing the reliability implementation:
|
---|
37 | * - variable latency
|
---|
38 | * - frame drop
|
---|
39 | */
|
---|
40 |
|
---|
41 | #include <adt/list.h>
|
---|
42 | #include <async.h>
|
---|
43 | #include <errno.h>
|
---|
44 | #include <io/log.h>
|
---|
45 | #include <stdlib.h>
|
---|
46 | #include <fibril.h>
|
---|
47 | #include "conn.h"
|
---|
48 | #include "ncsim.h"
|
---|
49 | #include "rqueue.h"
|
---|
50 | #include "segment.h"
|
---|
51 | #include "tcp_type.h"
|
---|
52 |
|
---|
53 | static list_t sim_queue;
|
---|
54 | static fibril_mutex_t sim_queue_lock;
|
---|
55 | static fibril_condvar_t sim_queue_cv;
|
---|
56 |
|
---|
57 | /** Initialize segment receive queue. */
|
---|
58 | void tcp_ncsim_init(void)
|
---|
59 | {
|
---|
60 | list_initialize(&sim_queue);
|
---|
61 | fibril_mutex_initialize(&sim_queue_lock);
|
---|
62 | fibril_condvar_initialize(&sim_queue_cv);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /** Bounce segment through simulator into receive queue.
|
---|
66 | *
|
---|
67 | * @param sp Socket pair, oriented for transmission
|
---|
68 | * @param seg Segment
|
---|
69 | */
|
---|
70 | void tcp_ncsim_bounce_seg(tcp_sockpair_t *sp, tcp_segment_t *seg)
|
---|
71 | {
|
---|
72 | tcp_squeue_entry_t *sqe;
|
---|
73 | tcp_squeue_entry_t *old_qe;
|
---|
74 | link_t *link;
|
---|
75 |
|
---|
76 | log_msg(LVL_DEBUG, "tcp_ncsim_bounce_seg()");
|
---|
77 | tcp_rqueue_bounce_seg(sp, seg);
|
---|
78 | return;
|
---|
79 |
|
---|
80 | if (0 /*random() % 4 == 3*/) {
|
---|
81 | /* Drop segment */
|
---|
82 | log_msg(LVL_ERROR, "NCSim dropping segment");
|
---|
83 | tcp_segment_delete(seg);
|
---|
84 | return;
|
---|
85 | }
|
---|
86 |
|
---|
87 | sqe = calloc(1, sizeof(tcp_squeue_entry_t));
|
---|
88 | if (sqe == NULL) {
|
---|
89 | log_msg(LVL_ERROR, "Failed allocating SQE.");
|
---|
90 | return;
|
---|
91 | }
|
---|
92 |
|
---|
93 | sqe->delay = random() % (1000 * 1000);
|
---|
94 | sqe->sp = *sp;
|
---|
95 | sqe->seg = seg;
|
---|
96 |
|
---|
97 | fibril_mutex_lock(&sim_queue_lock);
|
---|
98 |
|
---|
99 | link = list_first(&sim_queue);
|
---|
100 | while (link != NULL && sqe->delay > 0) {
|
---|
101 | old_qe = list_get_instance(link, tcp_squeue_entry_t, link);
|
---|
102 | if (sqe->delay < old_qe->delay)
|
---|
103 | break;
|
---|
104 |
|
---|
105 | sqe->delay -= old_qe->delay;
|
---|
106 |
|
---|
107 | link = link->next;
|
---|
108 | if (link == &sim_queue.head)
|
---|
109 | link = NULL;
|
---|
110 | }
|
---|
111 |
|
---|
112 | if (link != NULL)
|
---|
113 | list_insert_after(&sqe->link, link);
|
---|
114 | else
|
---|
115 | list_append(&sqe->link, &sim_queue);
|
---|
116 |
|
---|
117 | fibril_condvar_broadcast(&sim_queue_cv);
|
---|
118 | fibril_mutex_unlock(&sim_queue_lock);
|
---|
119 | }
|
---|
120 |
|
---|
121 | /** Network condition simulator handler fibril. */
|
---|
122 | static int tcp_ncsim_fibril(void *arg)
|
---|
123 | {
|
---|
124 | link_t *link;
|
---|
125 | tcp_squeue_entry_t *sqe;
|
---|
126 | int rc;
|
---|
127 |
|
---|
128 | log_msg(LVL_DEBUG, "tcp_ncsim_fibril()");
|
---|
129 |
|
---|
130 |
|
---|
131 | while (true) {
|
---|
132 | fibril_mutex_lock(&sim_queue_lock);
|
---|
133 |
|
---|
134 | while (list_empty(&sim_queue))
|
---|
135 | fibril_condvar_wait(&sim_queue_cv, &sim_queue_lock);
|
---|
136 |
|
---|
137 | do {
|
---|
138 | link = list_first(&sim_queue);
|
---|
139 | sqe = list_get_instance(link, tcp_squeue_entry_t, link);
|
---|
140 |
|
---|
141 | log_msg(LVL_DEBUG, "NCSim - Sleep");
|
---|
142 | rc = fibril_condvar_wait_timeout(&sim_queue_cv,
|
---|
143 | &sim_queue_lock, sqe->delay);
|
---|
144 | } while (rc != ETIMEOUT);
|
---|
145 |
|
---|
146 | list_remove(link);
|
---|
147 | fibril_mutex_unlock(&sim_queue_lock);
|
---|
148 |
|
---|
149 | log_msg(LVL_DEBUG, "NCSim - End Sleep");
|
---|
150 | tcp_rqueue_bounce_seg(&sqe->sp, sqe->seg);
|
---|
151 | free(sqe);
|
---|
152 | }
|
---|
153 |
|
---|
154 | /* Not reached */
|
---|
155 | return 0;
|
---|
156 | }
|
---|
157 |
|
---|
158 | /** Start simulator handler fibril. */
|
---|
159 | void tcp_ncsim_fibril_start(void)
|
---|
160 | {
|
---|
161 | fid_t fid;
|
---|
162 |
|
---|
163 | log_msg(LVL_DEBUG, "tcp_ncsim_fibril_start()");
|
---|
164 |
|
---|
165 | fid = fibril_create(tcp_ncsim_fibril, NULL);
|
---|
166 | if (fid == 0) {
|
---|
167 | log_msg(LVL_ERROR, "Failed creating ncsim fibril.");
|
---|
168 | return;
|
---|
169 | }
|
---|
170 |
|
---|
171 | fibril_add_ready(fid);
|
---|
172 | }
|
---|
173 |
|
---|
174 | /**
|
---|
175 | * @}
|
---|
176 | */
|
---|