source: mainline/uspace/srv/net/tcp/iqueue.c@ 2c4e1cc

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

Fix cstyle: make ccheck-fix and commit only files where all the changes are good.

  • Property mode set to 100644
File size: 5.3 KB
Line 
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 Connection incoming segments queue
35 *
36 * Segments are sorted in order of their sequence number.
37 */
38
39#include <adt/list.h>
40#include <errno.h>
41#include <io/log.h>
42#include <stdlib.h>
43#include "iqueue.h"
44#include "segment.h"
45#include "seq_no.h"
46#include "tcp_type.h"
47
48/** Initialize incoming segments queue.
49 *
50 * @param iqueue Incoming queue
51 * @param conn Connection the queue is associated with
52 */
53void tcp_iqueue_init(tcp_iqueue_t *iqueue, tcp_conn_t *conn)
54{
55 list_initialize(&iqueue->list);
56 iqueue->conn = conn;
57}
58
59/** Insert segment into incoming queue.
60 *
61 * @param iqueue Incoming queue
62 * @param seg Segment
63 */
64void tcp_iqueue_insert_seg(tcp_iqueue_t *iqueue, tcp_segment_t *seg)
65{
66 tcp_iqueue_entry_t *iqe;
67 tcp_iqueue_entry_t *qe;
68 link_t *link;
69 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_iqueue_insert_seg()");
70
71 iqe = calloc(1, sizeof(tcp_iqueue_entry_t));
72 if (iqe == NULL) {
73 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating IQE.");
74 return;
75 }
76
77 iqe->seg = seg;
78
79 /* Sort by sequence number */
80
81 link = list_first(&iqueue->list);
82 while (link != NULL) {
83 qe = list_get_instance(link,
84 tcp_iqueue_entry_t, link);
85
86 if (seq_no_seg_cmp(iqueue->conn, iqe->seg, qe->seg) < 0)
87 break;
88
89 link = list_next(link, &iqueue->list);
90 }
91
92 if (link != NULL)
93 list_insert_before(&iqe->link, &qe->link);
94 else
95 list_append(&iqe->link, &iqueue->list);
96}
97
98/** Remove segment from incoming queue.
99 *
100 * @param iqueue Incoming queue
101 * @param seg Segment
102 */
103void tcp_iqueue_remove_seg(tcp_iqueue_t *iqueue, tcp_segment_t *seg)
104{
105 tcp_iqueue_entry_t *qe;
106 link_t *link;
107
108 log_msg(LOG_DEFAULT, LVL_NOTE, "tcp_iqueue_remove_seg()");
109
110 link = list_first(&iqueue->list);
111 while (link != NULL) {
112 log_msg(LOG_DEFAULT, LVL_NOTE, "tcp_iqueue_remove_seg() - next");
113 qe = list_get_instance(link,
114 tcp_iqueue_entry_t, link);
115
116 if (qe->seg == seg) {
117 log_msg(LOG_DEFAULT, LVL_NOTE, "tcp_iqueue_remove_seg() - found, DONE");
118 list_remove(&qe->link);
119 free(qe);
120 return;
121 }
122
123 link = list_next(link, &iqueue->list);
124 }
125
126 log_msg(LOG_DEFAULT, LVL_NOTE, "tcp_iqueue_remove_seg() - not found");
127 assert(false);
128}
129
130/** Get next ready segment from incoming queue.
131 *
132 * Return the segment with the earliest sequence number if it is ready.
133 * A segment is ready if its SEG.SEQ is earlier or equal to RCV.NXT.
134 *
135 * @param iqueue Incoming queue
136 * @param seg Place to store pointer to segment
137 * @return EOK on success, ENOENT if no segment is ready
138 */
139errno_t tcp_iqueue_get_ready_seg(tcp_iqueue_t *iqueue, tcp_segment_t **seg)
140{
141 tcp_iqueue_entry_t *iqe;
142 link_t *link;
143
144 log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_get_ready_seg()");
145
146 link = list_first(&iqueue->list);
147 if (link == NULL) {
148 log_msg(LOG_DEFAULT, LVL_DEBUG, "iqueue is empty");
149 return ENOENT;
150 }
151
152 iqe = list_get_instance(link, tcp_iqueue_entry_t, link);
153
154 while (!seq_no_segment_acceptable(iqueue->conn, iqe->seg)) {
155 log_msg(LOG_DEFAULT, LVL_DEBUG, "Skipping unacceptable segment (RCV.NXT=%"
156 PRIu32 ", RCV.NXT+RCV.WND=%" PRIu32 ", SEG.SEQ=%" PRIu32
157 ", SEG.LEN=%" PRIu32 ")", iqueue->conn->rcv_nxt,
158 iqueue->conn->rcv_nxt + iqueue->conn->rcv_wnd,
159 iqe->seg->seq, iqe->seg->len);
160
161 list_remove(&iqe->link);
162 tcp_segment_delete(iqe->seg);
163
164 link = list_first(&iqueue->list);
165 if (link == NULL) {
166 log_msg(LOG_DEFAULT, LVL_DEBUG, "iqueue is empty");
167 return ENOENT;
168 }
169
170 iqe = list_get_instance(link, tcp_iqueue_entry_t, link);
171 }
172
173 /* Do not return segments that are not ready for processing */
174 if (!seq_no_segment_ready(iqueue->conn, iqe->seg)) {
175 log_msg(LOG_DEFAULT, LVL_DEBUG, "Next segment not ready: SEG.SEQ=%u, "
176 "RCV.NXT=%u, SEG.LEN=%u", iqe->seg->seq,
177 iqueue->conn->rcv_nxt, iqe->seg->len);
178 return ENOENT;
179 }
180
181 log_msg(LOG_DEFAULT, LVL_DEBUG, "Returning ready segment %p", iqe->seg);
182 list_remove(&iqe->link);
183 *seg = iqe->seg;
184 free(iqe);
185
186 return EOK;
187}
188
189/**
190 * @}
191 */
Note: See TracBrowser for help on using the repository browser.