| 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 | */
|
|---|
| 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 |
|
|---|
| 59 | /** Insert segment into incoming queue.
|
|---|
| 60 | *
|
|---|
| 61 | * @param iqueue Incoming queue
|
|---|
| 62 | * @param seg Segment
|
|---|
| 63 | */
|
|---|
| 64 | void 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(LVL_DEBUG, "tcp_iqueue_insert_seg()");
|
|---|
| 70 |
|
|---|
| 71 | iqe = calloc(1, sizeof(tcp_iqueue_entry_t));
|
|---|
| 72 | if (iqe == NULL) {
|
|---|
| 73 | log_msg(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 |
|
|---|
| 90 | if (link != NULL)
|
|---|
| 91 | list_insert_before(&iqe->link, &qe->link);
|
|---|
| 92 | else
|
|---|
| 93 | list_append(&iqe->link, &iqueue->list);
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | /** Get next ready segment from incoming queue.
|
|---|
| 97 | *
|
|---|
| 98 | * Return the segment with the earliest sequence number if it is ready.
|
|---|
| 99 | * A segment is ready if its SEG.SEQ is earlier or equal to RCV.NXT.
|
|---|
| 100 | *
|
|---|
| 101 | * @param iqueue Incoming queue
|
|---|
| 102 | * @param seg Place to store pointer to segment
|
|---|
| 103 | * @return EOK on success, ENOENT if no segment is ready
|
|---|
| 104 | */
|
|---|
| 105 | int tcp_iqueue_get_ready_seg(tcp_iqueue_t *iqueue, tcp_segment_t **seg)
|
|---|
| 106 | {
|
|---|
| 107 | tcp_iqueue_entry_t *iqe;
|
|---|
| 108 | link_t *link;
|
|---|
| 109 |
|
|---|
| 110 | log_msg(LVL_DEBUG, "tcp_get_ready_seg()");
|
|---|
| 111 |
|
|---|
| 112 | link = list_first(&iqueue->list);
|
|---|
| 113 | if (link == NULL) {
|
|---|
| 114 | log_msg(LVL_DEBUG, "iqueue is empty");
|
|---|
| 115 | return ENOENT;
|
|---|
| 116 | }
|
|---|
| 117 |
|
|---|
| 118 | iqe = list_get_instance(link, tcp_iqueue_entry_t, link);
|
|---|
| 119 |
|
|---|
| 120 | while (!seq_no_segment_acceptable(iqueue->conn, iqe->seg)) {
|
|---|
| 121 | log_msg(LVL_DEBUG, "Skipping unacceptable segment (RCV.NXT=%"
|
|---|
| 122 | PRIu32 ", RCV.NXT+RCV.WND=%" PRIu32 ", SEG.SEQ=%" PRIu32
|
|---|
| 123 | ", SEG.LEN=%" PRIu32 ")", iqueue->conn->rcv_nxt,
|
|---|
| 124 | iqueue->conn->rcv_nxt + iqueue->conn->rcv_wnd,
|
|---|
| 125 | iqe->seg->seq, iqe->seg->len);
|
|---|
| 126 |
|
|---|
| 127 | list_remove(&iqe->link);
|
|---|
| 128 | tcp_segment_delete(iqe->seg);
|
|---|
| 129 |
|
|---|
| 130 | link = list_first(&iqueue->list);
|
|---|
| 131 | if (link == NULL) {
|
|---|
| 132 | log_msg(LVL_DEBUG, "iqueue is empty");
|
|---|
| 133 | return ENOENT;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | iqe = list_get_instance(link, tcp_iqueue_entry_t, link);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /* Do not return segments that are not ready for processing */
|
|---|
| 140 | if (!seq_no_segment_ready(iqueue->conn, iqe->seg)) {
|
|---|
| 141 | log_msg(LVL_DEBUG, "Next segment not ready: SEG.SEQ=%u, "
|
|---|
| 142 | "RCV.NXT=%u, SEG.LEN=%u", iqe->seg->seq,
|
|---|
| 143 | iqueue->conn->rcv_nxt, iqe->seg->len);
|
|---|
| 144 | return ENOENT;
|
|---|
| 145 | }
|
|---|
| 146 |
|
|---|
| 147 | log_msg(LVL_DEBUG, "Returning ready segment %p", iqe->seg);
|
|---|
| 148 | list_remove(&iqe->link);
|
|---|
| 149 | *seg = iqe->seg;
|
|---|
| 150 |
|
|---|
| 151 | return EOK;
|
|---|
| 152 | }
|
|---|
| 153 |
|
|---|
| 154 | /**
|
|---|
| 155 | * @}
|
|---|
| 156 | */
|
|---|