source: mainline/uspace/srv/net/tl/tcp/ncsim.c@ 8218b6b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8218b6b was 8218b6b, checked in by Jiri Svoboda <jiri@…>, 14 years ago

Simulate variable network latency for TCP testing.

  • Property mode set to 100644
File size: 4.2 KB
RevLine 
[8218b6b]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 <thread.h>
47#include "conn.h"
48#include "ncsim.h"
49#include "rqueue.h"
50#include "tcp_type.h"
51
52static list_t sim_queue;
53static fibril_mutex_t sim_queue_lock;
54static fibril_condvar_t sim_queue_cv;
55
56/** Initialize segment receive queue. */
57void tcp_ncsim_init(void)
58{
59 list_initialize(&sim_queue);
60 fibril_mutex_initialize(&sim_queue_lock);
61 fibril_condvar_initialize(&sim_queue_cv);
62}
63
64/** Bounce segment through simulator into receive queue.
65 *
66 * @param sp Socket pair, oriented for transmission
67 * @param seg Segment
68 */
69void tcp_ncsim_bounce_seg(tcp_sockpair_t *sp, tcp_segment_t *seg)
70{
71 tcp_squeue_entry_t *sqe;
72 tcp_squeue_entry_t *old_qe;
73 link_t *link;
74
75 log_msg(LVL_DEBUG, "tcp_ncsim_insert_seg()");
76
77 sqe = calloc(1, sizeof(tcp_squeue_entry_t));
78 if (sqe == NULL) {
79 log_msg(LVL_ERROR, "Failed allocating SQE.");
80 return;
81 }
82
83 sqe->delay = random() % (1000 * 1000);
84 sqe->sp = *sp;
85 sqe->seg = seg;
86
87 fibril_mutex_lock(&sim_queue_lock);
88
89 link = list_first(&sim_queue);
90 while (link != NULL && sqe->delay > 0) {
91 old_qe = list_get_instance(link, tcp_squeue_entry_t, link);
92 if (sqe->delay < old_qe->delay)
93 break;
94
95 sqe->delay -= old_qe->delay;
96
97 link = link->next;
98 if (link == &sim_queue.head)
99 link = NULL;
100 }
101
102 if (link != NULL)
103 list_insert_after(&sqe->link, link);
104 else
105 list_append(&sqe->link, &sim_queue);
106
107 fibril_condvar_broadcast(&sim_queue_cv);
108 fibril_mutex_unlock(&sim_queue_lock);
109}
110
111/** Network condition simulator handler thread. */
112static void tcp_ncsim_thread(void *arg)
113{
114 link_t *link;
115 tcp_squeue_entry_t *sqe;
116 int rc;
117
118 log_msg(LVL_DEBUG, "tcp_ncsim_thread()");
119
120
121 while (true) {
122 fibril_mutex_lock(&sim_queue_lock);
123
124 while (list_empty(&sim_queue))
125 fibril_condvar_wait(&sim_queue_cv, &sim_queue_lock);
126
127 do {
128 link = list_first(&sim_queue);
129 sqe = list_get_instance(link, tcp_squeue_entry_t, link);
130
131 log_msg(LVL_DEBUG, "NCSim - Sleep");
132 rc = fibril_condvar_wait_timeout(&sim_queue_cv,
133 &sim_queue_lock, sqe->delay);
134 } while (rc != ETIMEOUT);
135
136 list_remove(link);
137 fibril_mutex_unlock(&sim_queue_lock);
138
139 log_msg(LVL_DEBUG, "NCSim - End Sleep");
140 tcp_rqueue_bounce_seg(&sqe->sp, sqe->seg);
141 free(sqe);
142 }
143}
144
145/** Start simulator handler thread. */
146void tcp_ncsim_thread_start(void)
147{
148 thread_id_t tid;
149 int rc;
150
151 log_msg(LVL_DEBUG, "tcp_ncsim_thread_start()");
152
153 rc = thread_create(tcp_ncsim_thread, NULL, "ncsim", &tid);
154 if (rc != EOK) {
155 log_msg(LVL_ERROR, "Failed creating ncsim thread.");
156 return;
157 }
158}
159
160/**
161 * @}
162 */
Note: See TracBrowser for help on using the repository browser.