source: mainline/uspace/srv/net/tcp/ncsim.c@ c718bda

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since c718bda was c718bda, checked in by Jiří Zárevúcky <zarevucky.jiri@…>, 7 years ago

Use standard names for rand() and srand().

  • Property mode set to 100644
File size: 4.6 KB
Line 
1/*
2 * Copyright (c) 2015 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 <inet/endpoint.h>
45#include <io/log.h>
46#include <stdlib.h>
47#include <fibril.h>
48#include "conn.h"
49#include "ncsim.h"
50#include "rqueue.h"
51#include "segment.h"
52#include "tcp_type.h"
53
54static list_t sim_queue;
55static fibril_mutex_t sim_queue_lock;
56static fibril_condvar_t sim_queue_cv;
57
58/** Initialize segment receive queue. */
59void tcp_ncsim_init(void)
60{
61 list_initialize(&sim_queue);
62 fibril_mutex_initialize(&sim_queue_lock);
63 fibril_condvar_initialize(&sim_queue_cv);
64}
65
66/** Bounce segment through simulator into receive queue.
67 *
68 * @param epp Endpoint pair, oriented for transmission
69 * @param seg Segment
70 */
71void tcp_ncsim_bounce_seg(inet_ep2_t *epp, tcp_segment_t *seg)
72{
73 tcp_squeue_entry_t *sqe;
74 tcp_squeue_entry_t *old_qe;
75 inet_ep2_t rident;
76 link_t *link;
77
78 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_ncsim_bounce_seg()");
79 tcp_ep2_flipped(epp, &rident);
80 tcp_rqueue_insert_seg(&rident, seg);
81 return;
82
83 if (0 /*rand() % 4 == 3*/) {
84 /* Drop segment */
85 log_msg(LOG_DEFAULT, LVL_ERROR, "NCSim dropping segment");
86 tcp_segment_delete(seg);
87 return;
88 }
89
90 sqe = calloc(1, sizeof(tcp_squeue_entry_t));
91 if (sqe == NULL) {
92 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating SQE.");
93 return;
94 }
95
96 sqe->delay = rand() % (1000 * 1000);
97 sqe->epp = *epp;
98 sqe->seg = seg;
99
100 fibril_mutex_lock(&sim_queue_lock);
101
102 link = list_first(&sim_queue);
103 while (link != NULL && sqe->delay > 0) {
104 old_qe = list_get_instance(link, tcp_squeue_entry_t, link);
105 if (sqe->delay < old_qe->delay)
106 break;
107
108 sqe->delay -= old_qe->delay;
109
110 link = link->next;
111 if (link == &sim_queue.head)
112 link = NULL;
113 }
114
115 if (link != NULL)
116 list_insert_after(&sqe->link, link);
117 else
118 list_append(&sqe->link, &sim_queue);
119
120 fibril_condvar_broadcast(&sim_queue_cv);
121 fibril_mutex_unlock(&sim_queue_lock);
122}
123
124/** Network condition simulator handler fibril. */
125static errno_t tcp_ncsim_fibril(void *arg)
126{
127 link_t *link;
128 tcp_squeue_entry_t *sqe;
129 inet_ep2_t rident;
130 errno_t rc;
131
132 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_ncsim_fibril()");
133
134 while (true) {
135 fibril_mutex_lock(&sim_queue_lock);
136
137 while (list_empty(&sim_queue))
138 fibril_condvar_wait(&sim_queue_cv, &sim_queue_lock);
139
140 do {
141 link = list_first(&sim_queue);
142 sqe = list_get_instance(link, tcp_squeue_entry_t, link);
143
144 log_msg(LOG_DEFAULT, LVL_DEBUG, "NCSim - Sleep");
145 rc = fibril_condvar_wait_timeout(&sim_queue_cv,
146 &sim_queue_lock, sqe->delay);
147 } while (rc != ETIMEOUT);
148
149 list_remove(link);
150 fibril_mutex_unlock(&sim_queue_lock);
151
152 log_msg(LOG_DEFAULT, LVL_DEBUG, "NCSim - End Sleep");
153 tcp_ep2_flipped(&sqe->epp, &rident);
154 tcp_rqueue_insert_seg(&rident, sqe->seg);
155 free(sqe);
156 }
157
158 /* Not reached */
159 return 0;
160}
161
162/** Start simulator handler fibril. */
163void tcp_ncsim_fibril_start(void)
164{
165 fid_t fid;
166
167 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_ncsim_fibril_start()");
168
169 fid = fibril_create(tcp_ncsim_fibril, NULL);
170 if (fid == 0) {
171 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed creating ncsim fibril.");
172 return;
173 }
174
175 fibril_add_ready(fid);
176}
177
178/**
179 * @}
180 */
Note: See TracBrowser for help on using the repository browser.