source: mainline/uspace/srv/net/tcp/rqueue.c@ 7c15d6f

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

TCP and UDP servers can make use of inet/endpoint.h types internally.

  • Property mode set to 100644
File size: 4.3 KB
RevLine 
[c5808b41]1/*
[2f19103]2 * Copyright (c) 2015 Jiri Svoboda
[c5808b41]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/**
[032bbe7]34 * @file Global segment receive queue
[c5808b41]35 */
36
37#include <adt/prodcons.h>
38#include <errno.h>
39#include <io/log.h>
40#include <stdlib.h>
[88a6819]41#include <fibril.h>
[032bbe7]42#include "conn.h"
[762b48a]43#include "pdu.h"
[c5808b41]44#include "rqueue.h"
[eea65f4]45#include "segment.h"
[c5808b41]46#include "tcp_type.h"
[762b48a]47#include "ucall.h"
[c5808b41]48
[eea65f4]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
[c5808b41]57static prodcons_t rqueue;
58
[032bbe7]59/** Initialize segment receive queue. */
[c5808b41]60void 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.
[032bbe7]68 *
[2f19103]69 * @param sp Endpoint pair, oriented for transmission
[032bbe7]70 * @param seg Segment
[c5808b41]71 */
[2f19103]72void tcp_rqueue_bounce_seg(inet_ep2_t *epp, tcp_segment_t *seg)
[c5808b41]73{
[2f19103]74 inet_ep2_t rident;
[c5808b41]75
[a1a101d]76 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_rqueue_bounce_seg()");
[c5808b41]77
[eea65f4]78#ifdef BOUNCE_TRANSCODE
79 tcp_pdu_t *pdu;
80 tcp_segment_t *dseg;
81
[2f19103]82 if (tcp_pdu_encode(epp, seg, &pdu) != EOK) {
[a1a101d]83 log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. Segment dropped.");
[eea65f4]84 return;
85 }
86
87 if (tcp_pdu_decode(pdu, &rident, &dseg) != EOK) {
[a1a101d]88 log_msg(LOG_DEFAULT, LVL_WARN, "Not enough memory. Segment dropped.");
[eea65f4]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
[c5808b41]98 /* Reverse the identification */
[2f19103]99 tcp_ep2_flipped(epp, &rident);
[c5808b41]100
[eea65f4]101 /* Insert segment back into rqueue */
[c5808b41]102 tcp_rqueue_insert_seg(&rident, seg);
[eea65f4]103#endif
[c5808b41]104}
105
[032bbe7]106/** Insert segment into receive queue.
107 *
[2f19103]108 * @param epp Endpoint pair, oriented for reception
[032bbe7]109 * @param seg Segment
110 */
[2f19103]111void tcp_rqueue_insert_seg(inet_ep2_t *epp, tcp_segment_t *seg)
[c5808b41]112{
113 tcp_rqueue_entry_t *rqe;
[a1a101d]114 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_rqueue_insert_seg()");
[c5808b41]115
[6896409c]116 tcp_segment_dump(seg);
117
[c5808b41]118 rqe = calloc(1, sizeof(tcp_rqueue_entry_t));
119 if (rqe == NULL) {
[a1a101d]120 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating RQE.");
[c5808b41]121 return;
122 }
123
[2f19103]124 rqe->epp = *epp;
[c5808b41]125 rqe->seg = seg;
126
127 prodcons_produce(&rqueue, &rqe->link);
128}
129
[88a6819]130/** Receive queue handler fibril. */
131static int tcp_rqueue_fibril(void *arg)
[c5808b41]132{
133 link_t *link;
134 tcp_rqueue_entry_t *rqe;
135
[a1a101d]136 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_rqueue_fibril()");
[c5808b41]137
138 while (true) {
139 link = prodcons_consume(&rqueue);
140 rqe = list_get_instance(link, tcp_rqueue_entry_t, link);
141
[2f19103]142 tcp_as_segment_arrived(&rqe->epp, rqe->seg);
[2cb32f9]143 free(rqe);
[c5808b41]144 }
[88a6819]145
146 /* Not reached */
147 return 0;
[c5808b41]148}
149
[88a6819]150/** Start receive queue handler fibril. */
151void tcp_rqueue_fibril_start(void)
[c5808b41]152{
[88a6819]153 fid_t fid;
[c5808b41]154
[a1a101d]155 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_rqueue_fibril_start()");
[c5808b41]156
[88a6819]157 fid = fibril_create(tcp_rqueue_fibril, NULL);
158 if (fid == 0) {
[a1a101d]159 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed creating rqueue fibril.");
[c5808b41]160 return;
161 }
[88a6819]162
163 fibril_add_ready(fid);
[c5808b41]164}
165
166/**
167 * @}
168 */
Note: See TracBrowser for help on using the repository browser.