[c5808b41] | 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 | /**
|
---|
[032bbe7] | 34 | * @file Connection incoming segments queue
|
---|
| 35 | *
|
---|
| 36 | * Segments are sorted in order of their sequence number.
|
---|
[c5808b41] | 37 | */
|
---|
| 38 |
|
---|
| 39 | #include <adt/list.h>
|
---|
| 40 | #include <errno.h>
|
---|
| 41 | #include <io/log.h>
|
---|
| 42 | #include <stdlib.h>
|
---|
| 43 | #include "iqueue.h"
|
---|
[4c55a64] | 44 | #include "segment.h"
|
---|
[c5808b41] | 45 | #include "seq_no.h"
|
---|
| 46 | #include "tcp_type.h"
|
---|
| 47 |
|
---|
[032bbe7] | 48 | /** Initialize incoming segments queue.
|
---|
| 49 | *
|
---|
| 50 | * @param iqueue Incoming queue
|
---|
| 51 | * @param conn Connection the queue is associated with
|
---|
| 52 | */
|
---|
[c5808b41] | 53 | void tcp_iqueue_init(tcp_iqueue_t *iqueue, tcp_conn_t *conn)
|
---|
| 54 | {
|
---|
| 55 | list_initialize(&iqueue->list);
|
---|
| 56 | iqueue->conn = conn;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[032bbe7] | 59 | /** Insert segment into incoming queue.
|
---|
| 60 | *
|
---|
| 61 | * @param iqueue Incoming queue
|
---|
| 62 | * @param seg Segment
|
---|
| 63 | */
|
---|
[c5808b41] | 64 | void tcp_iqueue_insert_seg(tcp_iqueue_t *iqueue, tcp_segment_t *seg)
|
---|
| 65 | {
|
---|
| 66 | tcp_iqueue_entry_t *iqe;
|
---|
[32aea9f4] | 67 | tcp_iqueue_entry_t *qe;
|
---|
| 68 | link_t *link;
|
---|
[a1a101d] | 69 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_iqueue_insert_seg()");
|
---|
[c5808b41] | 70 |
|
---|
| 71 | iqe = calloc(1, sizeof(tcp_iqueue_entry_t));
|
---|
| 72 | if (iqe == NULL) {
|
---|
[a1a101d] | 73 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating IQE.");
|
---|
[c5808b41] | 74 | return;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | iqe->seg = seg;
|
---|
| 78 |
|
---|
[32aea9f4] | 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 |
|
---|
[1ddbf81] | 86 | if (seq_no_seg_cmp(iqueue->conn, iqe->seg, qe->seg) < 0)
|
---|
[32aea9f4] | 87 | break;
|
---|
[1ddbf81] | 88 |
|
---|
| 89 | link = list_next(link, &iqueue->list);
|
---|
[32aea9f4] | 90 | }
|
---|
| 91 |
|
---|
| 92 | if (link != NULL)
|
---|
| 93 | list_insert_before(&iqe->link, &qe->link);
|
---|
| 94 | else
|
---|
| 95 | list_append(&iqe->link, &iqueue->list);
|
---|
[c5808b41] | 96 | }
|
---|
| 97 |
|
---|
[1ddbf81] | 98 | /** Remove segment from incoming queue.
|
---|
| 99 | *
|
---|
| 100 | * @param iqueue Incoming queue
|
---|
| 101 | * @param seg Segment
|
---|
| 102 | */
|
---|
| 103 | void 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) {
|
---|
[1433ecda] | 112 | log_msg(LOG_DEFAULT, LVL_NOTE, "tcp_iqueue_remove_seg() - next");
|
---|
[1ddbf81] | 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 |
|
---|
[032bbe7] | 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 | */
|
---|
[b7fd2a0] | 139 | errno_t tcp_iqueue_get_ready_seg(tcp_iqueue_t *iqueue, tcp_segment_t **seg)
|
---|
[c5808b41] | 140 | {
|
---|
| 141 | tcp_iqueue_entry_t *iqe;
|
---|
| 142 | link_t *link;
|
---|
| 143 |
|
---|
[a1a101d] | 144 | log_msg(LOG_DEFAULT, LVL_DEBUG, "tcp_get_ready_seg()");
|
---|
[c5808b41] | 145 |
|
---|
| 146 | link = list_first(&iqueue->list);
|
---|
| 147 | if (link == NULL) {
|
---|
[a1a101d] | 148 | log_msg(LOG_DEFAULT, LVL_DEBUG, "iqueue is empty");
|
---|
[c5808b41] | 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)) {
|
---|
[a1a101d] | 155 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Skipping unacceptable segment (RCV.NXT=%"
|
---|
[d9ce049] | 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);
|
---|
[c5808b41] | 160 |
|
---|
| 161 | list_remove(&iqe->link);
|
---|
[4c55a64] | 162 | tcp_segment_delete(iqe->seg);
|
---|
[c5808b41] | 163 |
|
---|
[1433ecda] | 164 | link = list_first(&iqueue->list);
|
---|
[c5808b41] | 165 | if (link == NULL) {
|
---|
[a1a101d] | 166 | log_msg(LOG_DEFAULT, LVL_DEBUG, "iqueue is empty");
|
---|
[c5808b41] | 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)) {
|
---|
[a1a101d] | 175 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Next segment not ready: SEG.SEQ=%u, "
|
---|
[c5808b41] | 176 | "RCV.NXT=%u, SEG.LEN=%u", iqe->seg->seq,
|
---|
| 177 | iqueue->conn->rcv_nxt, iqe->seg->len);
|
---|
| 178 | return ENOENT;
|
---|
| 179 | }
|
---|
| 180 |
|
---|
[a1a101d] | 181 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Returning ready segment %p", iqe->seg);
|
---|
[c5808b41] | 182 | list_remove(&iqe->link);
|
---|
| 183 | *seg = iqe->seg;
|
---|
[ef1ddad] | 184 | free(iqe);
|
---|
[c5808b41] | 185 |
|
---|
| 186 | return EOK;
|
---|
| 187 | }
|
---|
| 188 |
|
---|
| 189 | /**
|
---|
| 190 | * @}
|
---|
| 191 | */
|
---|