[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 TCP transmission queue
|
---|
[c5808b41] | 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <byteorder.h>
|
---|
| 38 | #include <io/log.h>
|
---|
[32105348] | 39 | #include <macros.h>
|
---|
| 40 | #include <mem.h>
|
---|
| 41 | #include "conn.h"
|
---|
[c5808b41] | 42 | #include "header.h"
|
---|
| 43 | #include "rqueue.h"
|
---|
| 44 | #include "segment.h"
|
---|
| 45 | #include "tqueue.h"
|
---|
| 46 | #include "tcp_type.h"
|
---|
| 47 |
|
---|
| 48 | void tcp_tqueue_ctrl_seg(tcp_conn_t *conn, tcp_control_t ctrl)
|
---|
| 49 | {
|
---|
| 50 | tcp_segment_t *seg;
|
---|
| 51 |
|
---|
| 52 | log_msg(LVL_DEBUG, "tcp_tqueue_ctrl_seg(%p, %u)", conn, ctrl);
|
---|
| 53 |
|
---|
| 54 | seg = tcp_segment_make_ctrl(ctrl);
|
---|
| 55 | tcp_tqueue_seg(conn, seg);
|
---|
| 56 | }
|
---|
| 57 |
|
---|
| 58 | void tcp_tqueue_seg(tcp_conn_t *conn, tcp_segment_t *seg)
|
---|
| 59 | {
|
---|
| 60 | log_msg(LVL_DEBUG, "tcp_tqueue_seg(%p, %p)", conn, seg);
|
---|
| 61 | /* XXX queue */
|
---|
| 62 |
|
---|
[32105348] | 63 | /*
|
---|
| 64 | * Always send ACK once we have received SYN, except for RST segments.
|
---|
| 65 | * (Spec says we should always send ACK once connection has been
|
---|
| 66 | * established.)
|
---|
| 67 | */
|
---|
| 68 | if (tcp_conn_got_syn(conn) && (seg->ctrl & CTL_RST) == 0)
|
---|
| 69 | seg->ctrl |= CTL_ACK;
|
---|
| 70 |
|
---|
| 71 | seg->seq = conn->snd_nxt;
|
---|
| 72 | seg->wnd = conn->rcv_wnd;
|
---|
| 73 |
|
---|
[d9ce049] | 74 | log_msg(LVL_DEBUG, "SEG.SEQ=%" PRIu32 ", SEG.WND=%" PRIu32,
|
---|
| 75 | seg->seq, seg->wnd);
|
---|
| 76 |
|
---|
[32105348] | 77 | if ((seg->ctrl & CTL_ACK) != 0)
|
---|
| 78 | seg->ack = conn->rcv_nxt;
|
---|
| 79 | else
|
---|
| 80 | seg->ack = 0;
|
---|
| 81 |
|
---|
[c5808b41] | 82 | conn->snd_nxt += seg->len;
|
---|
[32105348] | 83 |
|
---|
[c5808b41] | 84 | tcp_transmit_segment(&conn->ident, seg);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
[32105348] | 87 | /** Transmit data from the send buffer.
|
---|
| 88 | *
|
---|
| 89 | * @param conn Connection
|
---|
| 90 | */
|
---|
| 91 | void tcp_tqueue_new_data(tcp_conn_t *conn)
|
---|
| 92 | {
|
---|
[d9ce049] | 93 | size_t avail_wnd;
|
---|
[8c7a054] | 94 | size_t xfer_seqlen;
|
---|
| 95 | size_t snd_buf_seqlen;
|
---|
[32105348] | 96 | size_t data_size;
|
---|
[8c7a054] | 97 | tcp_control_t ctrl;
|
---|
| 98 |
|
---|
[32105348] | 99 | tcp_segment_t *seg;
|
---|
| 100 |
|
---|
| 101 | log_msg(LVL_DEBUG, "tcp_tqueue_new_data()");
|
---|
| 102 |
|
---|
[d9ce049] | 103 | /* Number of free sequence numbers in send window */
|
---|
| 104 | avail_wnd = (conn->snd_una + conn->snd_wnd) - conn->snd_nxt;
|
---|
[8c7a054] | 105 | snd_buf_seqlen = conn->snd_buf_used + (conn->snd_buf_fin ? 1 : 0);
|
---|
[d9ce049] | 106 |
|
---|
[8c7a054] | 107 | xfer_seqlen = min(snd_buf_seqlen, avail_wnd);
|
---|
| 108 | log_msg(LVL_DEBUG, "snd_buf_seqlen = %zu, SND.WND = %zu, "
|
---|
| 109 | "xfer_seqlen = %zu", snd_buf_seqlen, conn->snd_wnd,
|
---|
| 110 | xfer_seqlen);
|
---|
[32105348] | 111 |
|
---|
[8c7a054] | 112 | if (xfer_seqlen == 0)
|
---|
[32105348] | 113 | return;
|
---|
| 114 |
|
---|
| 115 | /* XXX Do not always send immediately */
|
---|
| 116 |
|
---|
[8c7a054] | 117 | data_size = xfer_seqlen - (conn->snd_buf_fin ? 1 : 0);
|
---|
| 118 | if (conn->snd_buf_fin && data_size + 1 == xfer_seqlen) {
|
---|
| 119 | /* We are sending out FIN */
|
---|
| 120 | ctrl = CTL_FIN;
|
---|
| 121 | } else {
|
---|
| 122 | ctrl = 0;
|
---|
| 123 | }
|
---|
| 124 |
|
---|
| 125 | seg = tcp_segment_make_data(ctrl, conn->snd_buf, data_size);
|
---|
[32105348] | 126 | if (seg == NULL) {
|
---|
| 127 | log_msg(LVL_ERROR, "Memory allocation failure.");
|
---|
| 128 | return;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | /* Remove data from send buffer */
|
---|
| 132 | memmove(conn->snd_buf, conn->snd_buf + data_size,
|
---|
| 133 | conn->snd_buf_used - data_size);
|
---|
| 134 | conn->snd_buf_used -= data_size;
|
---|
[8c7a054] | 135 | conn->snd_buf_fin = false;
|
---|
[32105348] | 136 |
|
---|
| 137 | tcp_tqueue_seg(conn, seg);
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[d9ce049] | 140 | /** Remove ACKed segments from retransmission queue and possibly transmit
|
---|
| 141 | * more data.
|
---|
[4c55a64] | 142 | *
|
---|
| 143 | * This should be called when SND.UNA is updated due to incoming ACK.
|
---|
| 144 | */
|
---|
[d9ce049] | 145 | void tcp_tqueue_ack_received(tcp_conn_t *conn)
|
---|
[4c55a64] | 146 | {
|
---|
| 147 | (void) conn;
|
---|
[d9ce049] | 148 |
|
---|
| 149 | tcp_tqueue_new_data(conn);
|
---|
[4c55a64] | 150 | }
|
---|
| 151 |
|
---|
[c5808b41] | 152 | void tcp_transmit_segment(tcp_sockpair_t *sp, tcp_segment_t *seg)
|
---|
| 153 | {
|
---|
| 154 | log_msg(LVL_DEBUG, "tcp_transmit_segment(%p, %p)", sp, seg);
|
---|
| 155 | /*
|
---|
| 156 | tcp_pdu_prepare(conn, seg, &data, &len);
|
---|
| 157 | tcp_pdu_transmit(data, len);
|
---|
| 158 | */
|
---|
| 159 | tcp_rqueue_bounce_seg(sp, seg);
|
---|
| 160 | }
|
---|
| 161 |
|
---|
| 162 | /**
|
---|
| 163 | * @}
|
---|
| 164 | */
|
---|