| 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 <fibril.h>
|
|---|
| 42 | #include "conn.h"
|
|---|
| 43 | #include "pdu.h"
|
|---|
| 44 | #include "rqueue.h"
|
|---|
| 45 | #include "segment.h"
|
|---|
| 46 | #include "tcp_type.h"
|
|---|
| 47 | #include "ucall.h"
|
|---|
| 48 |
|
|---|
| 49 | /** Transcode bounced segments.
|
|---|
| 50 | *
|
|---|
| 51 | * If defined, segments bounced via the internal debugging loopback will
|
|---|
| 52 | * be encoded to a PDU and the decoded. Otherwise they will be bounced back
|
|---|
| 53 | * directly without passing the encoder-decoder.
|
|---|
| 54 | */
|
|---|
| 55 | #define BOUNCE_TRANSCODE
|
|---|
| 56 |
|
|---|
| 57 | static prodcons_t rqueue;
|
|---|
| 58 |
|
|---|
| 59 | /** Initialize segment receive queue. */
|
|---|
| 60 | void tcp_rqueue_init(void)
|
|---|
| 61 | {
|
|---|
| 62 | prodcons_initialize(&rqueue);
|
|---|
| 63 | }
|
|---|
| 64 |
|
|---|
| 65 | /** Bounce segment directy into receive queue without constructing the PDU.
|
|---|
| 66 | *
|
|---|
| 67 | * This is for testing purposes only.
|
|---|
| 68 | *
|
|---|
| 69 | * @param sp Socket pair, oriented for transmission
|
|---|
| 70 | * @param seg Segment
|
|---|
| 71 | */
|
|---|
| 72 | void tcp_rqueue_bounce_seg(tcp_sockpair_t *sp, tcp_segment_t *seg)
|
|---|
| 73 | {
|
|---|
| 74 | tcp_sockpair_t rident;
|
|---|
| 75 |
|
|---|
| 76 | log_msg(LVL_DEBUG, "tcp_rqueue_bounce_seg()");
|
|---|
| 77 |
|
|---|
| 78 | #ifdef BOUNCE_TRANSCODE
|
|---|
| 79 | tcp_pdu_t *pdu;
|
|---|
| 80 | tcp_segment_t *dseg;
|
|---|
| 81 |
|
|---|
| 82 | if (tcp_pdu_encode(sp, seg, &pdu) != EOK) {
|
|---|
| 83 | log_msg(LVL_WARN, "Not enough memory. Segment dropped.");
|
|---|
| 84 | return;
|
|---|
| 85 | }
|
|---|
| 86 |
|
|---|
| 87 | if (tcp_pdu_decode(pdu, &rident, &dseg) != EOK) {
|
|---|
| 88 | log_msg(LVL_WARN, "Not enough memory. Segment dropped.");
|
|---|
| 89 | return;
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | tcp_pdu_delete(pdu);
|
|---|
| 93 |
|
|---|
| 94 | /** Insert decoded segment into rqueue */
|
|---|
| 95 | tcp_rqueue_insert_seg(&rident, dseg);
|
|---|
| 96 | tcp_segment_delete(seg);
|
|---|
| 97 | #else
|
|---|
| 98 | /* Reverse the identification */
|
|---|
| 99 | tcp_sockpair_flipped(sp, &rident);
|
|---|
| 100 |
|
|---|
| 101 | /* Insert segment back into rqueue */
|
|---|
| 102 | tcp_rqueue_insert_seg(&rident, seg);
|
|---|
| 103 | #endif
|
|---|
| 104 | }
|
|---|
| 105 |
|
|---|
| 106 | /** Insert segment into receive queue.
|
|---|
| 107 | *
|
|---|
| 108 | * @param sp Socket pair, oriented for reception
|
|---|
| 109 | * @param seg Segment
|
|---|
| 110 | */
|
|---|
| 111 | void tcp_rqueue_insert_seg(tcp_sockpair_t *sp, tcp_segment_t *seg)
|
|---|
| 112 | {
|
|---|
| 113 | tcp_rqueue_entry_t *rqe;
|
|---|
| 114 | log_msg(LVL_DEBUG, "tcp_rqueue_insert_seg()");
|
|---|
| 115 |
|
|---|
| 116 | tcp_segment_dump(seg);
|
|---|
| 117 |
|
|---|
| 118 | rqe = calloc(1, sizeof(tcp_rqueue_entry_t));
|
|---|
| 119 | if (rqe == NULL) {
|
|---|
| 120 | log_msg(LVL_ERROR, "Failed allocating RQE.");
|
|---|
| 121 | return;
|
|---|
| 122 | }
|
|---|
| 123 |
|
|---|
| 124 | rqe->sp = *sp;
|
|---|
| 125 | rqe->seg = seg;
|
|---|
| 126 |
|
|---|
| 127 | prodcons_produce(&rqueue, &rqe->link);
|
|---|
| 128 | }
|
|---|
| 129 |
|
|---|
| 130 | /** Receive queue handler fibril. */
|
|---|
| 131 | static int tcp_rqueue_fibril(void *arg)
|
|---|
| 132 | {
|
|---|
| 133 | link_t *link;
|
|---|
| 134 | tcp_rqueue_entry_t *rqe;
|
|---|
| 135 |
|
|---|
| 136 | log_msg(LVL_DEBUG, "tcp_rqueue_fibril()");
|
|---|
| 137 |
|
|---|
| 138 | while (true) {
|
|---|
| 139 | link = prodcons_consume(&rqueue);
|
|---|
| 140 | rqe = list_get_instance(link, tcp_rqueue_entry_t, link);
|
|---|
| 141 |
|
|---|
| 142 | tcp_as_segment_arrived(&rqe->sp, rqe->seg);
|
|---|
| 143 | }
|
|---|
| 144 |
|
|---|
| 145 | /* Not reached */
|
|---|
| 146 | return 0;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /** Start receive queue handler fibril. */
|
|---|
| 150 | void tcp_rqueue_fibril_start(void)
|
|---|
| 151 | {
|
|---|
| 152 | fid_t fid;
|
|---|
| 153 |
|
|---|
| 154 | log_msg(LVL_DEBUG, "tcp_rqueue_fibril_start()");
|
|---|
| 155 |
|
|---|
| 156 | fid = fibril_create(tcp_rqueue_fibril, NULL);
|
|---|
| 157 | if (fid == 0) {
|
|---|
| 158 | log_msg(LVL_ERROR, "Failed creating rqueue fibril.");
|
|---|
| 159 | return;
|
|---|
| 160 | }
|
|---|
| 161 |
|
|---|
| 162 | fibril_add_ready(fid);
|
|---|
| 163 | }
|
|---|
| 164 |
|
|---|
| 165 | /**
|
|---|
| 166 | * @}
|
|---|
| 167 | */
|
|---|