[8218b6b] | 1 | /*
|
---|
[2f19103] | 2 | * Copyright (c) 2015 Jiri Svoboda
|
---|
[8218b6b] | 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>
|
---|
[2f19103] | 44 | #include <inet/endpoint.h>
|
---|
[8218b6b] | 45 | #include <io/log.h>
|
---|
| 46 | #include <stdlib.h>
|
---|
[88a6819] | 47 | #include <fibril.h>
|
---|
[8218b6b] | 48 | #include "conn.h"
|
---|
| 49 | #include "ncsim.h"
|
---|
| 50 | #include "rqueue.h"
|
---|
[7cf7ded] | 51 | #include "segment.h"
|
---|
[8218b6b] | 52 | #include "tcp_type.h"
|
---|
| 53 |
|
---|
| 54 | static list_t sim_queue;
|
---|
| 55 | static fibril_mutex_t sim_queue_lock;
|
---|
| 56 | static fibril_condvar_t sim_queue_cv;
|
---|
| 57 |
|
---|
| 58 | /** Initialize segment receive queue. */
|
---|
| 59 | void 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 | *
|
---|
[2f19103] | 68 | * @param epp Endpoint pair, oriented for transmission
|
---|
[8218b6b] | 69 | * @param seg Segment
|
---|
| 70 | */
|
---|
[2f19103] | 71 | void tcp_ncsim_bounce_seg(inet_ep2_t *epp, tcp_segment_t *seg)
|
---|
[8218b6b] | 72 | {
|
---|
| 73 | tcp_squeue_entry_t *sqe;
|
---|
| 74 | tcp_squeue_entry_t *old_qe;
|
---|
[9520af7] | 75 | inet_ep2_t rident;
|
---|
[8218b6b] | 76 | link_t *link;
|
---|
| 77 |
|
---|
[a1a101d] | 78 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_ncsim_bounce_seg()");
|
---|
[9520af7] | 79 | tcp_ep2_flipped(epp, &rident);
|
---|
| 80 | tcp_rqueue_insert_seg(&rident, seg);
|
---|
[7cf7ded] | 81 | return;
|
---|
| 82 |
|
---|
[d1582b50] | 83 | if (0 /* rand() % 4 == 3 */) {
|
---|
[7cf7ded] | 84 | /* Drop segment */
|
---|
[a1a101d] | 85 | log_msg(LOG_DEFAULT, LVL_ERROR, "NCSim dropping segment");
|
---|
[7cf7ded] | 86 | tcp_segment_delete(seg);
|
---|
| 87 | return;
|
---|
| 88 | }
|
---|
[8218b6b] | 89 |
|
---|
| 90 | sqe = calloc(1, sizeof(tcp_squeue_entry_t));
|
---|
| 91 | if (sqe == NULL) {
|
---|
[a1a101d] | 92 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating SQE.");
|
---|
[8218b6b] | 93 | return;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[c718bda] | 96 | sqe->delay = rand() % (1000 * 1000);
|
---|
[2f19103] | 97 | sqe->epp = *epp;
|
---|
[8218b6b] | 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 |
|
---|
[88a6819] | 124 | /** Network condition simulator handler fibril. */
|
---|
[b7fd2a0] | 125 | static errno_t tcp_ncsim_fibril(void *arg)
|
---|
[8218b6b] | 126 | {
|
---|
| 127 | link_t *link;
|
---|
| 128 | tcp_squeue_entry_t *sqe;
|
---|
[9520af7] | 129 | inet_ep2_t rident;
|
---|
[b7fd2a0] | 130 | errno_t rc;
|
---|
[8218b6b] | 131 |
|
---|
[a1a101d] | 132 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_ncsim_fibril()");
|
---|
[8218b6b] | 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 |
|
---|
[a1a101d] | 144 | log_msg(LOG_DEFAULT, LVL_DEBUG, "NCSim - Sleep");
|
---|
[8218b6b] | 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 |
|
---|
[a1a101d] | 152 | log_msg(LOG_DEFAULT, LVL_DEBUG, "NCSim - End Sleep");
|
---|
[9520af7] | 153 | tcp_ep2_flipped(&sqe->epp, &rident);
|
---|
| 154 | tcp_rqueue_insert_seg(&rident, sqe->seg);
|
---|
[8218b6b] | 155 | free(sqe);
|
---|
| 156 | }
|
---|
[88a6819] | 157 |
|
---|
| 158 | /* Not reached */
|
---|
| 159 | return 0;
|
---|
[8218b6b] | 160 | }
|
---|
| 161 |
|
---|
[88a6819] | 162 | /** Start simulator handler fibril. */
|
---|
| 163 | void tcp_ncsim_fibril_start(void)
|
---|
[8218b6b] | 164 | {
|
---|
[88a6819] | 165 | fid_t fid;
|
---|
[8218b6b] | 166 |
|
---|
[a1a101d] | 167 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_ncsim_fibril_start()");
|
---|
[8218b6b] | 168 |
|
---|
[88a6819] | 169 | fid = fibril_create(tcp_ncsim_fibril, NULL);
|
---|
| 170 | if (fid == 0) {
|
---|
[a1a101d] | 171 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed creating ncsim fibril.");
|
---|
[8218b6b] | 172 | return;
|
---|
| 173 | }
|
---|
[88a6819] | 174 |
|
---|
| 175 | fibril_add_ready(fid);
|
---|
[8218b6b] | 176 | }
|
---|
| 177 |
|
---|
| 178 | /**
|
---|
| 179 | * @}
|
---|
| 180 | */
|
---|