Version of http://mbed.org/cookbook/NetServicesTribute with setting set the same for LPC2368

Dependents:   UDPSocketExample 24LCxx_I2CApp WeatherPlatform_pachube HvZServerLib ... more

Committer:
simon
Date:
Tue Nov 23 14:15:36 2010 +0000
Revision:
0:350011bf8be7
Experimental version for testing UDP

Who changed what in which revision?

UserRevisionLine numberNew contents of line
simon 0:350011bf8be7 1 /**
simon 0:350011bf8be7 2 * @file
simon 0:350011bf8be7 3 * Transmission Control Protocol, outgoing traffic
simon 0:350011bf8be7 4 *
simon 0:350011bf8be7 5 * The output functions of TCP.
simon 0:350011bf8be7 6 *
simon 0:350011bf8be7 7 */
simon 0:350011bf8be7 8
simon 0:350011bf8be7 9 /*
simon 0:350011bf8be7 10 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
simon 0:350011bf8be7 11 * All rights reserved.
simon 0:350011bf8be7 12 *
simon 0:350011bf8be7 13 * Redistribution and use in source and binary forms, with or without modification,
simon 0:350011bf8be7 14 * are permitted provided that the following conditions are met:
simon 0:350011bf8be7 15 *
simon 0:350011bf8be7 16 * 1. Redistributions of source code must retain the above copyright notice,
simon 0:350011bf8be7 17 * this list of conditions and the following disclaimer.
simon 0:350011bf8be7 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
simon 0:350011bf8be7 19 * this list of conditions and the following disclaimer in the documentation
simon 0:350011bf8be7 20 * and/or other materials provided with the distribution.
simon 0:350011bf8be7 21 * 3. The name of the author may not be used to endorse or promote products
simon 0:350011bf8be7 22 * derived from this software without specific prior written permission.
simon 0:350011bf8be7 23 *
simon 0:350011bf8be7 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
simon 0:350011bf8be7 25 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
simon 0:350011bf8be7 26 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
simon 0:350011bf8be7 27 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
simon 0:350011bf8be7 28 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
simon 0:350011bf8be7 29 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
simon 0:350011bf8be7 30 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
simon 0:350011bf8be7 31 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
simon 0:350011bf8be7 32 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
simon 0:350011bf8be7 33 * OF SUCH DAMAGE.
simon 0:350011bf8be7 34 *
simon 0:350011bf8be7 35 * This file is part of the lwIP TCP/IP stack.
simon 0:350011bf8be7 36 *
simon 0:350011bf8be7 37 * Author: Adam Dunkels <adam@sics.se>
simon 0:350011bf8be7 38 *
simon 0:350011bf8be7 39 */
simon 0:350011bf8be7 40
simon 0:350011bf8be7 41 #include "lwip/opt.h"
simon 0:350011bf8be7 42
simon 0:350011bf8be7 43 #if LWIP_TCP /* don't build if not configured for use in lwipopts.h */
simon 0:350011bf8be7 44
simon 0:350011bf8be7 45 #include "lwip/tcp_impl.h"
simon 0:350011bf8be7 46 #include "lwip/def.h"
simon 0:350011bf8be7 47 #include "lwip/mem.h"
simon 0:350011bf8be7 48 #include "lwip/memp.h"
simon 0:350011bf8be7 49 #include "lwip/sys.h"
simon 0:350011bf8be7 50 #include "lwip/ip_addr.h"
simon 0:350011bf8be7 51 #include "lwip/netif.h"
simon 0:350011bf8be7 52 #include "lwip/inet_chksum.h"
simon 0:350011bf8be7 53 #include "lwip/stats.h"
simon 0:350011bf8be7 54 #include "lwip/snmp.h"
simon 0:350011bf8be7 55
simon 0:350011bf8be7 56 #include <string.h>
simon 0:350011bf8be7 57
simon 0:350011bf8be7 58 /* Define some copy-macros for checksum-on-copy so that the code looks
simon 0:350011bf8be7 59 nicer by preventing too many ifdef's. */
simon 0:350011bf8be7 60 #if TCP_CHECKSUM_ON_COPY
simon 0:350011bf8be7 61 #define TCP_DATA_COPY(dst, src, len, seg) do { \
simon 0:350011bf8be7 62 tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), \
simon 0:350011bf8be7 63 len, &seg->chksum, &seg->chksum_swapped); \
simon 0:350011bf8be7 64 seg->flags |= TF_SEG_DATA_CHECKSUMMED; } while(0)
simon 0:350011bf8be7 65 #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped) \
simon 0:350011bf8be7 66 tcp_seg_add_chksum(LWIP_CHKSUM_COPY(dst, src, len), len, chksum, chksum_swapped);
simon 0:350011bf8be7 67 #else /* TCP_CHECKSUM_ON_COPY*/
simon 0:350011bf8be7 68 #define TCP_DATA_COPY(dst, src, len, seg) MEMCPY(dst, src, len)
simon 0:350011bf8be7 69 #define TCP_DATA_COPY2(dst, src, len, chksum, chksum_swapped) MEMCPY(dst, src, len)
simon 0:350011bf8be7 70 #endif /* TCP_CHECKSUM_ON_COPY*/
simon 0:350011bf8be7 71
simon 0:350011bf8be7 72 /** Define this to 1 for an extra check that the output checksum is valid
simon 0:350011bf8be7 73 * (usefule when the checksum is generated by the application, not the stack) */
simon 0:350011bf8be7 74 #ifndef TCP_CHECKSUM_ON_COPY_SANITY_CHECK
simon 0:350011bf8be7 75 #define TCP_CHECKSUM_ON_COPY_SANITY_CHECK 0
simon 0:350011bf8be7 76 #endif
simon 0:350011bf8be7 77
simon 0:350011bf8be7 78 /* Forward declarations.*/
simon 0:350011bf8be7 79 static void tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb);
simon 0:350011bf8be7 80
simon 0:350011bf8be7 81 /** Allocate a pbuf and create a tcphdr at p->payload, used for output
simon 0:350011bf8be7 82 * functions other than the default tcp_output -> tcp_output_segment
simon 0:350011bf8be7 83 * (e.g. tcp_send_empty_ack, etc.)
simon 0:350011bf8be7 84 *
simon 0:350011bf8be7 85 * @param pcb tcp pcb for which to send a packet (used to initialize tcp_hdr)
simon 0:350011bf8be7 86 * @param optlen length of header-options
simon 0:350011bf8be7 87 * @param datalen length of tcp data to reserve in pbuf
simon 0:350011bf8be7 88 * @param seqno_be seqno in network byte order (big-endian)
simon 0:350011bf8be7 89 * @return pbuf with p->payload being the tcp_hdr
simon 0:350011bf8be7 90 */
simon 0:350011bf8be7 91 static struct pbuf *
simon 0:350011bf8be7 92 tcp_output_alloc_header(struct tcp_pcb *pcb, u16_t optlen, u16_t datalen,
simon 0:350011bf8be7 93 u32_t seqno_be /* already in network byte order */)
simon 0:350011bf8be7 94 {
simon 0:350011bf8be7 95 struct tcp_hdr *tcphdr;
simon 0:350011bf8be7 96 struct pbuf *p = pbuf_alloc(PBUF_IP, TCP_HLEN + optlen + datalen, PBUF_RAM);
simon 0:350011bf8be7 97 if (p != NULL) {
simon 0:350011bf8be7 98 LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
simon 0:350011bf8be7 99 (p->len >= TCP_HLEN + optlen));
simon 0:350011bf8be7 100 tcphdr = (struct tcp_hdr *)p->payload;
simon 0:350011bf8be7 101 tcphdr->src = htons(pcb->local_port);
simon 0:350011bf8be7 102 tcphdr->dest = htons(pcb->remote_port);
simon 0:350011bf8be7 103 tcphdr->seqno = seqno_be;
simon 0:350011bf8be7 104 tcphdr->ackno = htonl(pcb->rcv_nxt);
simon 0:350011bf8be7 105 TCPH_HDRLEN_FLAGS_SET(tcphdr, (5 + optlen / 4), TCP_ACK);
simon 0:350011bf8be7 106 tcphdr->wnd = htons(pcb->rcv_ann_wnd);
simon 0:350011bf8be7 107 tcphdr->chksum = 0;
simon 0:350011bf8be7 108 tcphdr->urgp = 0;
simon 0:350011bf8be7 109
simon 0:350011bf8be7 110 /* If we're sending a packet, update the announced right window edge */
simon 0:350011bf8be7 111 pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
simon 0:350011bf8be7 112 }
simon 0:350011bf8be7 113 return p;
simon 0:350011bf8be7 114 }
simon 0:350011bf8be7 115
simon 0:350011bf8be7 116 /**
simon 0:350011bf8be7 117 * Called by tcp_close() to send a segment including FIN flag but not data.
simon 0:350011bf8be7 118 *
simon 0:350011bf8be7 119 * @param pcb the tcp_pcb over which to send a segment
simon 0:350011bf8be7 120 * @return ERR_OK if sent, another err_t otherwise
simon 0:350011bf8be7 121 */
simon 0:350011bf8be7 122 err_t
simon 0:350011bf8be7 123 tcp_send_fin(struct tcp_pcb *pcb)
simon 0:350011bf8be7 124 {
simon 0:350011bf8be7 125 /* first, try to add the fin to the last unsent segment */
simon 0:350011bf8be7 126 if (pcb->unsent != NULL) {
simon 0:350011bf8be7 127 struct tcp_seg *last_unsent;
simon 0:350011bf8be7 128 for (last_unsent = pcb->unsent; last_unsent->next != NULL;
simon 0:350011bf8be7 129 last_unsent = last_unsent->next);
simon 0:350011bf8be7 130
simon 0:350011bf8be7 131 if ((TCPH_FLAGS(last_unsent->tcphdr) & (TCP_SYN | TCP_FIN | TCP_RST)) == 0) {
simon 0:350011bf8be7 132 /* no SYN/FIN/RST flag in the header, we can add the FIN flag */
simon 0:350011bf8be7 133 TCPH_SET_FLAG(last_unsent->tcphdr, TCP_FIN);
simon 0:350011bf8be7 134 return ERR_OK;
simon 0:350011bf8be7 135 }
simon 0:350011bf8be7 136 }
simon 0:350011bf8be7 137 /* no data, no length, flags, copy=1, no optdata */
simon 0:350011bf8be7 138 return tcp_enqueue_flags(pcb, TCP_FIN);
simon 0:350011bf8be7 139 }
simon 0:350011bf8be7 140
simon 0:350011bf8be7 141 /**
simon 0:350011bf8be7 142 * Create a TCP segment with prefilled header.
simon 0:350011bf8be7 143 *
simon 0:350011bf8be7 144 * Called by tcp_write and tcp_enqueue_flags.
simon 0:350011bf8be7 145 *
simon 0:350011bf8be7 146 * @param pcb Protocol control block for the TCP connection.
simon 0:350011bf8be7 147 * @param p pbuf that is used to hold the TCP header.
simon 0:350011bf8be7 148 * @param flags TCP flags for header.
simon 0:350011bf8be7 149 * @param seqno TCP sequence number of this packet
simon 0:350011bf8be7 150 * @param optflags options to include in TCP header
simon 0:350011bf8be7 151 * @return a new tcp_seg pointing to p, or NULL.
simon 0:350011bf8be7 152 * The TCP header is filled in except ackno and wnd.
simon 0:350011bf8be7 153 * p is freed on failure.
simon 0:350011bf8be7 154 */
simon 0:350011bf8be7 155 static struct tcp_seg *
simon 0:350011bf8be7 156 tcp_create_segment(struct tcp_pcb *pcb, struct pbuf *p, u8_t flags, u32_t seqno, u8_t optflags)
simon 0:350011bf8be7 157 {
simon 0:350011bf8be7 158 struct tcp_seg *seg;
simon 0:350011bf8be7 159 u8_t optlen = LWIP_TCP_OPT_LENGTH(optflags);
simon 0:350011bf8be7 160
simon 0:350011bf8be7 161 if ((seg = (struct tcp_seg *)memp_malloc(MEMP_TCP_SEG)) == NULL) {
simon 0:350011bf8be7 162 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_create_segment: no memory.\n"));
simon 0:350011bf8be7 163 pbuf_free(p);
simon 0:350011bf8be7 164 return NULL;
simon 0:350011bf8be7 165 }
simon 0:350011bf8be7 166 seg->flags = optflags;
simon 0:350011bf8be7 167 seg->next = NULL;
simon 0:350011bf8be7 168 seg->p = p;
simon 0:350011bf8be7 169 seg->dataptr = p->payload;
simon 0:350011bf8be7 170 seg->len = p->tot_len - optlen;
simon 0:350011bf8be7 171 #if TCP_OVERSIZE_DBGCHECK
simon 0:350011bf8be7 172 seg->oversize_left = 0;
simon 0:350011bf8be7 173 #endif /* TCP_OVERSIZE_DBGCHECK */
simon 0:350011bf8be7 174 #if TCP_CHECKSUM_ON_COPY
simon 0:350011bf8be7 175 seg->chksum = 0;
simon 0:350011bf8be7 176 seg->chksum_swapped = 0;
simon 0:350011bf8be7 177 /* check optflags */
simon 0:350011bf8be7 178 LWIP_ASSERT("invalid optflags passed: TF_SEG_DATA_CHECKSUMMED",
simon 0:350011bf8be7 179 (optflags & TF_SEG_DATA_CHECKSUMMED) == 0);
simon 0:350011bf8be7 180 #endif /* TCP_CHECKSUM_ON_COPY */
simon 0:350011bf8be7 181
simon 0:350011bf8be7 182 /* build TCP header */
simon 0:350011bf8be7 183 if (pbuf_header(p, TCP_HLEN)) {
simon 0:350011bf8be7 184 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_create_segment: no room for TCP header in pbuf.\n"));
simon 0:350011bf8be7 185 TCP_STATS_INC(tcp.err);
simon 0:350011bf8be7 186 tcp_seg_free(seg);
simon 0:350011bf8be7 187 return NULL;
simon 0:350011bf8be7 188 }
simon 0:350011bf8be7 189 seg->tcphdr = (struct tcp_hdr *)seg->p->payload;
simon 0:350011bf8be7 190 seg->tcphdr->src = htons(pcb->local_port);
simon 0:350011bf8be7 191 seg->tcphdr->dest = htons(pcb->remote_port);
simon 0:350011bf8be7 192 seg->tcphdr->seqno = htonl(seqno);
simon 0:350011bf8be7 193 /* ackno is set in tcp_output */
simon 0:350011bf8be7 194 TCPH_HDRLEN_FLAGS_SET(seg->tcphdr, (5 + optlen / 4), flags);
simon 0:350011bf8be7 195 /* wnd and chksum are set in tcp_output */
simon 0:350011bf8be7 196 seg->tcphdr->urgp = 0;
simon 0:350011bf8be7 197 return seg;
simon 0:350011bf8be7 198 }
simon 0:350011bf8be7 199
simon 0:350011bf8be7 200 /**
simon 0:350011bf8be7 201 * Allocate a PBUF_RAM pbuf, perhaps with extra space at the end.
simon 0:350011bf8be7 202 *
simon 0:350011bf8be7 203 * This function is like pbuf_alloc(layer, length, PBUF_RAM) except
simon 0:350011bf8be7 204 * there may be extra bytes available at the end.
simon 0:350011bf8be7 205 *
simon 0:350011bf8be7 206 * @param layer flag to define header size.
simon 0:350011bf8be7 207 * @param length size of the pbuf's payload.
simon 0:350011bf8be7 208 * @param max_length maximum usable size of payload+oversize.
simon 0:350011bf8be7 209 * @param oversize pointer to a u16_t that will receive the number of usable tail bytes.
simon 0:350011bf8be7 210 * @param pcb The TCP connection that willo enqueue the pbuf.
simon 0:350011bf8be7 211 * @param apiflags API flags given to tcp_write.
simon 0:350011bf8be7 212 * @param first_seg true when this pbuf will be used in the first enqueued segment.
simon 0:350011bf8be7 213 * @param
simon 0:350011bf8be7 214 */
simon 0:350011bf8be7 215 #if TCP_OVERSIZE
simon 0:350011bf8be7 216 static struct pbuf *
simon 0:350011bf8be7 217 tcp_pbuf_prealloc(pbuf_layer layer, u16_t length, u16_t max_length,
simon 0:350011bf8be7 218 u16_t *oversize, struct tcp_pcb *pcb, u8_t apiflags,
simon 0:350011bf8be7 219 u8_t first_seg)
simon 0:350011bf8be7 220 {
simon 0:350011bf8be7 221 struct pbuf *p;
simon 0:350011bf8be7 222 u16_t alloc = length;
simon 0:350011bf8be7 223
simon 0:350011bf8be7 224 #if LWIP_NETIF_TX_SINGLE_PBUF
simon 0:350011bf8be7 225 LWIP_UNUSED_ARG(max_length);
simon 0:350011bf8be7 226 LWIP_UNUSED_ARG(pcb);
simon 0:350011bf8be7 227 LWIP_UNUSED_ARG(apiflags);
simon 0:350011bf8be7 228 LWIP_UNUSED_ARG(first_seg);
simon 0:350011bf8be7 229 /* always create MSS-sized pbufs */
simon 0:350011bf8be7 230 alloc = TCP_MSS;
simon 0:350011bf8be7 231 #else /* LWIP_NETIF_TX_SINGLE_PBUF */
simon 0:350011bf8be7 232 if (length < max_length) {
simon 0:350011bf8be7 233 /* Should we allocate an oversized pbuf, or just the minimum
simon 0:350011bf8be7 234 * length required? If tcp_write is going to be called again
simon 0:350011bf8be7 235 * before this segment is transmitted, we want the oversized
simon 0:350011bf8be7 236 * buffer. If the segment will be transmitted immediately, we can
simon 0:350011bf8be7 237 * save memory by allocating only length. We use a simple
simon 0:350011bf8be7 238 * heuristic based on the following information:
simon 0:350011bf8be7 239 *
simon 0:350011bf8be7 240 * Did the user set TCP_WRITE_FLAG_MORE?
simon 0:350011bf8be7 241 *
simon 0:350011bf8be7 242 * Will the Nagle algorithm defer transmission of this segment?
simon 0:350011bf8be7 243 */
simon 0:350011bf8be7 244 if ((apiflags & TCP_WRITE_FLAG_MORE) ||
simon 0:350011bf8be7 245 (!(pcb->flags & TF_NODELAY) &&
simon 0:350011bf8be7 246 (!first_seg ||
simon 0:350011bf8be7 247 pcb->unsent != NULL ||
simon 0:350011bf8be7 248 pcb->unacked != NULL))) {
simon 0:350011bf8be7 249 alloc = LWIP_MIN(max_length, LWIP_MEM_ALIGN_SIZE(length + TCP_OVERSIZE));
simon 0:350011bf8be7 250 }
simon 0:350011bf8be7 251 }
simon 0:350011bf8be7 252 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
simon 0:350011bf8be7 253 p = pbuf_alloc(layer, alloc, PBUF_RAM);
simon 0:350011bf8be7 254 if (p == NULL) {
simon 0:350011bf8be7 255 return NULL;
simon 0:350011bf8be7 256 }
simon 0:350011bf8be7 257 LWIP_ASSERT("need unchained pbuf", p->next == NULL);
simon 0:350011bf8be7 258 *oversize = p->len - length;
simon 0:350011bf8be7 259 /* trim p->len to the currently used size */
simon 0:350011bf8be7 260 p->len = p->tot_len = length;
simon 0:350011bf8be7 261 return p;
simon 0:350011bf8be7 262 }
simon 0:350011bf8be7 263 #else /* TCP_OVERSIZE */
simon 0:350011bf8be7 264 #define tcp_pbuf_prealloc(layer, length, mx, os, pcb, api, fst) pbuf_alloc((layer), (length), PBUF_RAM)
simon 0:350011bf8be7 265 #endif /* TCP_OVERSIZE */
simon 0:350011bf8be7 266
simon 0:350011bf8be7 267 #if TCP_CHECKSUM_ON_COPY
simon 0:350011bf8be7 268 /** Add a checksum of newly added data to the segment */
simon 0:350011bf8be7 269 static void
simon 0:350011bf8be7 270 tcp_seg_add_chksum(u16_t chksum, u16_t len, u16_t *seg_chksum,
simon 0:350011bf8be7 271 u8_t *seg_chksum_swapped)
simon 0:350011bf8be7 272 {
simon 0:350011bf8be7 273 u32_t helper;
simon 0:350011bf8be7 274 /* add chksum to old chksum and fold to u16_t */
simon 0:350011bf8be7 275 helper = chksum + *seg_chksum;
simon 0:350011bf8be7 276 chksum = FOLD_U32T(helper);
simon 0:350011bf8be7 277 if ((len & 1) != 0) {
simon 0:350011bf8be7 278 *seg_chksum_swapped = 1 - *seg_chksum_swapped;
simon 0:350011bf8be7 279 chksum = SWAP_BYTES_IN_WORD(chksum);
simon 0:350011bf8be7 280 }
simon 0:350011bf8be7 281 *seg_chksum = chksum;
simon 0:350011bf8be7 282 }
simon 0:350011bf8be7 283 #endif /* TCP_CHECKSUM_ON_COPY */
simon 0:350011bf8be7 284
simon 0:350011bf8be7 285 /** Checks if tcp_write is allowed or not (checks state, snd_buf and snd_queuelen).
simon 0:350011bf8be7 286 *
simon 0:350011bf8be7 287 * @param pcb the tcp pcb to check for
simon 0:350011bf8be7 288 * @param len length of data to send (checked agains snd_buf)
simon 0:350011bf8be7 289 * @return ERR_OK if tcp_write is allowed to proceed, another err_t otherwise
simon 0:350011bf8be7 290 */
simon 0:350011bf8be7 291 static err_t
simon 0:350011bf8be7 292 tcp_write_checks(struct tcp_pcb *pcb, u16_t len)
simon 0:350011bf8be7 293 {
simon 0:350011bf8be7 294 /* connection is in invalid state for data transmission? */
simon 0:350011bf8be7 295 if ((pcb->state != ESTABLISHED) &&
simon 0:350011bf8be7 296 (pcb->state != CLOSE_WAIT) &&
simon 0:350011bf8be7 297 (pcb->state != SYN_SENT) &&
simon 0:350011bf8be7 298 (pcb->state != SYN_RCVD)) {
simon 0:350011bf8be7 299 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_STATE | LWIP_DBG_LEVEL_SEVERE, ("tcp_write() called in invalid state\n"));
simon 0:350011bf8be7 300 return ERR_CONN;
simon 0:350011bf8be7 301 } else if (len == 0) {
simon 0:350011bf8be7 302 return ERR_OK;
simon 0:350011bf8be7 303 }
simon 0:350011bf8be7 304
simon 0:350011bf8be7 305 /* fail on too much data */
simon 0:350011bf8be7 306 if (len > pcb->snd_buf) {
simon 0:350011bf8be7 307 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_write: too much data (len=%"U16_F" > snd_buf=%"U16_F")\n",
simon 0:350011bf8be7 308 len, pcb->snd_buf));
simon 0:350011bf8be7 309 pcb->flags |= TF_NAGLEMEMERR;
simon 0:350011bf8be7 310 return ERR_MEM;
simon 0:350011bf8be7 311 }
simon 0:350011bf8be7 312
simon 0:350011bf8be7 313 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen));
simon 0:350011bf8be7 314
simon 0:350011bf8be7 315 /* If total number of pbufs on the unsent/unacked queues exceeds the
simon 0:350011bf8be7 316 * configured maximum, return an error */
simon 0:350011bf8be7 317 /* check for configured max queuelen and possible overflow */
simon 0:350011bf8be7 318 if ((pcb->snd_queuelen >= TCP_SND_QUEUELEN) || (pcb->snd_queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
simon 0:350011bf8be7 319 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_write: too long queue %"U16_F" (max %"U16_F")\n",
simon 0:350011bf8be7 320 pcb->snd_queuelen, TCP_SND_QUEUELEN));
simon 0:350011bf8be7 321 TCP_STATS_INC(tcp.memerr);
simon 0:350011bf8be7 322 pcb->flags |= TF_NAGLEMEMERR;
simon 0:350011bf8be7 323 return ERR_MEM;
simon 0:350011bf8be7 324 }
simon 0:350011bf8be7 325 if (pcb->snd_queuelen != 0) {
simon 0:350011bf8be7 326 LWIP_ASSERT("tcp_write: pbufs on queue => at least one queue non-empty",
simon 0:350011bf8be7 327 pcb->unacked != NULL || pcb->unsent != NULL);
simon 0:350011bf8be7 328 } else {
simon 0:350011bf8be7 329 LWIP_ASSERT("tcp_write: no pbufs on queue => both queues empty",
simon 0:350011bf8be7 330 pcb->unacked == NULL && pcb->unsent == NULL);
simon 0:350011bf8be7 331 }
simon 0:350011bf8be7 332 return ERR_OK;
simon 0:350011bf8be7 333 }
simon 0:350011bf8be7 334
simon 0:350011bf8be7 335 /**
simon 0:350011bf8be7 336 * Write data for sending (but does not send it immediately).
simon 0:350011bf8be7 337 *
simon 0:350011bf8be7 338 * It waits in the expectation of more data being sent soon (as
simon 0:350011bf8be7 339 * it can send them more efficiently by combining them together).
simon 0:350011bf8be7 340 * To prompt the system to send data now, call tcp_output() after
simon 0:350011bf8be7 341 * calling tcp_write().
simon 0:350011bf8be7 342 *
simon 0:350011bf8be7 343 * @param pcb Protocol control block for the TCP connection to enqueue data for.
simon 0:350011bf8be7 344 * @param arg Pointer to the data to be enqueued for sending.
simon 0:350011bf8be7 345 * @param len Data length in bytes
simon 0:350011bf8be7 346 * @param apiflags combination of following flags :
simon 0:350011bf8be7 347 * - TCP_WRITE_FLAG_COPY (0x01) data will be copied into memory belonging to the stack
simon 0:350011bf8be7 348 * - TCP_WRITE_FLAG_MORE (0x02) for TCP connection, PSH flag will be set on last segment sent,
simon 0:350011bf8be7 349 * @return ERR_OK if enqueued, another err_t on error
simon 0:350011bf8be7 350 */
simon 0:350011bf8be7 351 err_t
simon 0:350011bf8be7 352 tcp_write(struct tcp_pcb *pcb, const void *arg, u16_t len, u8_t apiflags)
simon 0:350011bf8be7 353 {
simon 0:350011bf8be7 354 struct pbuf *concat_p = NULL;
simon 0:350011bf8be7 355 struct tcp_seg *last_unsent = NULL, *seg = NULL, *prev_seg = NULL, *queue = NULL;
simon 0:350011bf8be7 356 u16_t pos = 0; /* position in 'arg' data */
simon 0:350011bf8be7 357 u16_t queuelen;
simon 0:350011bf8be7 358 u8_t optlen = 0;
simon 0:350011bf8be7 359 u8_t optflags = 0;
simon 0:350011bf8be7 360 #if TCP_OVERSIZE
simon 0:350011bf8be7 361 u16_t oversize = 0;
simon 0:350011bf8be7 362 u16_t oversize_used = 0;
simon 0:350011bf8be7 363 #endif /* TCP_OVERSIZE */
simon 0:350011bf8be7 364 #if TCP_CHECKSUM_ON_COPY
simon 0:350011bf8be7 365 u16_t concat_chksum = 0;
simon 0:350011bf8be7 366 u8_t concat_chksum_swapped = 0;
simon 0:350011bf8be7 367 u16_t concat_chksummed = 0;
simon 0:350011bf8be7 368 #endif /* TCP_CHECKSUM_ON_COPY */
simon 0:350011bf8be7 369 err_t err;
simon 0:350011bf8be7 370
simon 0:350011bf8be7 371 #if LWIP_NETIF_TX_SINGLE_PBUF
simon 0:350011bf8be7 372 /* Always copy to try to create single pbufs for TX */
simon 0:350011bf8be7 373 apiflags |= TCP_WRITE_FLAG_COPY;
simon 0:350011bf8be7 374 #endif /* LWIP_NETIF_TX_SINGLE_PBUF */
simon 0:350011bf8be7 375
simon 0:350011bf8be7 376 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_write(pcb=%p, data=%p, len=%"U16_F", apiflags=%"U16_F")\n",
simon 0:350011bf8be7 377 (void *)pcb, arg, len, (u16_t)apiflags));
simon 0:350011bf8be7 378 LWIP_ERROR("tcp_write: arg == NULL (programmer violates API)",
simon 0:350011bf8be7 379 arg != NULL, return ERR_ARG;);
simon 0:350011bf8be7 380
simon 0:350011bf8be7 381 err = tcp_write_checks(pcb, len);
simon 0:350011bf8be7 382 if (err != ERR_OK) {
simon 0:350011bf8be7 383 return err;
simon 0:350011bf8be7 384 }
simon 0:350011bf8be7 385 queuelen = pcb->snd_queuelen;
simon 0:350011bf8be7 386
simon 0:350011bf8be7 387 #if LWIP_TCP_TIMESTAMPS
simon 0:350011bf8be7 388 if ((pcb->flags & TF_TIMESTAMP)) {
simon 0:350011bf8be7 389 optflags = TF_SEG_OPTS_TS;
simon 0:350011bf8be7 390 optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS);
simon 0:350011bf8be7 391 }
simon 0:350011bf8be7 392 #endif /* LWIP_TCP_TIMESTAMPS */
simon 0:350011bf8be7 393
simon 0:350011bf8be7 394
simon 0:350011bf8be7 395 /*
simon 0:350011bf8be7 396 * TCP segmentation is done in three phases with increasing complexity:
simon 0:350011bf8be7 397 *
simon 0:350011bf8be7 398 * 1. Copy data directly into an oversized pbuf.
simon 0:350011bf8be7 399 * 2. Chain a new pbuf to the end of pcb->unsent.
simon 0:350011bf8be7 400 * 3. Create new segments.
simon 0:350011bf8be7 401 *
simon 0:350011bf8be7 402 * We may run out of memory at any point. In that case we must
simon 0:350011bf8be7 403 * return ERR_MEM and not change anything in pcb. Therefore, all
simon 0:350011bf8be7 404 * changes are recorded in local variables and committed at the end
simon 0:350011bf8be7 405 * of the function. Some pcb fields are maintained in local copies:
simon 0:350011bf8be7 406 *
simon 0:350011bf8be7 407 * queuelen = pcb->snd_queuelen
simon 0:350011bf8be7 408 * oversize = pcb->unsent_oversize
simon 0:350011bf8be7 409 *
simon 0:350011bf8be7 410 * These variables are set consistently by the phases:
simon 0:350011bf8be7 411 *
simon 0:350011bf8be7 412 * seg points to the last segment tampered with.
simon 0:350011bf8be7 413 *
simon 0:350011bf8be7 414 * pos records progress as data is segmented.
simon 0:350011bf8be7 415 */
simon 0:350011bf8be7 416
simon 0:350011bf8be7 417 /* Find the tail of the unsent queue. */
simon 0:350011bf8be7 418 if (pcb->unsent != NULL) {
simon 0:350011bf8be7 419 u16_t space;
simon 0:350011bf8be7 420 u16_t unsent_optlen;
simon 0:350011bf8be7 421
simon 0:350011bf8be7 422 /* @todo: this could be sped up by keeping last_unsent in the pcb */
simon 0:350011bf8be7 423 for (last_unsent = pcb->unsent; last_unsent->next != NULL;
simon 0:350011bf8be7 424 last_unsent = last_unsent->next);
simon 0:350011bf8be7 425
simon 0:350011bf8be7 426 /* Usable space at the end of the last unsent segment */
simon 0:350011bf8be7 427 unsent_optlen = LWIP_TCP_OPT_LENGTH(last_unsent->flags);
simon 0:350011bf8be7 428 space = pcb->mss - (last_unsent->len + unsent_optlen);
simon 0:350011bf8be7 429
simon 0:350011bf8be7 430 /*
simon 0:350011bf8be7 431 * Phase 1: Copy data directly into an oversized pbuf.
simon 0:350011bf8be7 432 *
simon 0:350011bf8be7 433 * The number of bytes copied is recorded in the oversize_used
simon 0:350011bf8be7 434 * variable. The actual copying is done at the bottom of the
simon 0:350011bf8be7 435 * function.
simon 0:350011bf8be7 436 */
simon 0:350011bf8be7 437 #if TCP_OVERSIZE
simon 0:350011bf8be7 438 #if TCP_OVERSIZE_DBGCHECK
simon 0:350011bf8be7 439 /* check that pcb->unsent_oversize matches last_unsent->unsent_oversize */
simon 0:350011bf8be7 440 LWIP_ASSERT("unsent_oversize mismatch (pcb vs. last_unsent)",
simon 0:350011bf8be7 441 pcb->unsent_oversize == last_unsent->oversize_left);
simon 0:350011bf8be7 442 #endif /* TCP_OVERSIZE_DBGCHECK */
simon 0:350011bf8be7 443 oversize = pcb->unsent_oversize;
simon 0:350011bf8be7 444 if (oversize > 0) {
simon 0:350011bf8be7 445 LWIP_ASSERT("inconsistent oversize vs. space", oversize_used <= space);
simon 0:350011bf8be7 446 seg = last_unsent;
simon 0:350011bf8be7 447 oversize_used = oversize < len ? oversize : len;
simon 0:350011bf8be7 448 pos += oversize_used;
simon 0:350011bf8be7 449 oversize -= oversize_used;
simon 0:350011bf8be7 450 space -= oversize_used;
simon 0:350011bf8be7 451 }
simon 0:350011bf8be7 452 /* now we are either finished or oversize is zero */
simon 0:350011bf8be7 453 LWIP_ASSERT("inconsistend oversize vs. len", (oversize == 0) || (pos == len));
simon 0:350011bf8be7 454 #endif /* TCP_OVERSIZE */
simon 0:350011bf8be7 455
simon 0:350011bf8be7 456 /*
simon 0:350011bf8be7 457 * Phase 2: Chain a new pbuf to the end of pcb->unsent.
simon 0:350011bf8be7 458 *
simon 0:350011bf8be7 459 * We don't extend segments containing SYN/FIN flags or options
simon 0:350011bf8be7 460 * (len==0). The new pbuf is kept in concat_p and pbuf_cat'ed at
simon 0:350011bf8be7 461 * the end.
simon 0:350011bf8be7 462 */
simon 0:350011bf8be7 463 if ((pos < len) && (space > 0) && (last_unsent->len > 0)) {
simon 0:350011bf8be7 464 u16_t seglen = space < len - pos ? space : len - pos;
simon 0:350011bf8be7 465 seg = last_unsent;
simon 0:350011bf8be7 466
simon 0:350011bf8be7 467 /* Create a pbuf with a copy or reference to seglen bytes. We
simon 0:350011bf8be7 468 * can use PBUF_RAW here since the data appears in the middle of
simon 0:350011bf8be7 469 * a segment. A header will never be prepended. */
simon 0:350011bf8be7 470 if (apiflags & TCP_WRITE_FLAG_COPY) {
simon 0:350011bf8be7 471 /* Data is copied */
simon 0:350011bf8be7 472 if ((concat_p = tcp_pbuf_prealloc(PBUF_RAW, seglen, space, &oversize, pcb, apiflags, 1)) == NULL) {
simon 0:350011bf8be7 473 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
simon 0:350011bf8be7 474 ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n",
simon 0:350011bf8be7 475 seglen));
simon 0:350011bf8be7 476 goto memerr;
simon 0:350011bf8be7 477 }
simon 0:350011bf8be7 478 #if TCP_OVERSIZE_DBGCHECK
simon 0:350011bf8be7 479 last_unsent->oversize_left = oversize;
simon 0:350011bf8be7 480 #endif /* TCP_OVERSIZE_DBGCHECK */
simon 0:350011bf8be7 481 TCP_DATA_COPY2(concat_p->payload, (u8_t*)arg + pos, seglen, &concat_chksum, &concat_chksum_swapped);
simon 0:350011bf8be7 482 #if TCP_CHECKSUM_ON_COPY
simon 0:350011bf8be7 483 concat_chksummed += seglen;
simon 0:350011bf8be7 484 #endif /* TCP_CHECKSUM_ON_COPY */
simon 0:350011bf8be7 485 } else {
simon 0:350011bf8be7 486 /* Data is not copied */
simon 0:350011bf8be7 487 if ((concat_p = pbuf_alloc(PBUF_RAW, seglen, PBUF_ROM)) == NULL) {
simon 0:350011bf8be7 488 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2,
simon 0:350011bf8be7 489 ("tcp_write: could not allocate memory for zero-copy pbuf\n"));
simon 0:350011bf8be7 490 goto memerr;
simon 0:350011bf8be7 491 }
simon 0:350011bf8be7 492 #if TCP_CHECKSUM_ON_COPY
simon 0:350011bf8be7 493 /* calculate the checksum of nocopy-data */
simon 0:350011bf8be7 494 tcp_seg_add_chksum(~inet_chksum((u8_t*)arg + pos, seglen), seglen,
simon 0:350011bf8be7 495 &concat_chksum, &concat_chksum_swapped);
simon 0:350011bf8be7 496 concat_chksummed += seglen;
simon 0:350011bf8be7 497 #endif /* TCP_CHECKSUM_ON_COPY */
simon 0:350011bf8be7 498 /* reference the non-volatile payload data */
simon 0:350011bf8be7 499 concat_p->payload = (u8_t*)arg + pos;
simon 0:350011bf8be7 500 }
simon 0:350011bf8be7 501
simon 0:350011bf8be7 502 pos += seglen;
simon 0:350011bf8be7 503 queuelen += pbuf_clen(concat_p);
simon 0:350011bf8be7 504 }
simon 0:350011bf8be7 505 } else {
simon 0:350011bf8be7 506 #if TCP_OVERSIZE
simon 0:350011bf8be7 507 LWIP_ASSERT("unsent_oversize mismatch (pcb->unsent is NULL)",
simon 0:350011bf8be7 508 pcb->unsent_oversize == 0);
simon 0:350011bf8be7 509 #endif /* TCP_OVERSIZE */
simon 0:350011bf8be7 510 }
simon 0:350011bf8be7 511
simon 0:350011bf8be7 512 /*
simon 0:350011bf8be7 513 * Phase 3: Create new segments.
simon 0:350011bf8be7 514 *
simon 0:350011bf8be7 515 * The new segments are chained together in the local 'queue'
simon 0:350011bf8be7 516 * variable, ready to be appended to pcb->unsent.
simon 0:350011bf8be7 517 */
simon 0:350011bf8be7 518 while (pos < len) {
simon 0:350011bf8be7 519 struct pbuf *p;
simon 0:350011bf8be7 520 u16_t left = len - pos;
simon 0:350011bf8be7 521 u16_t max_len = pcb->mss - optlen;
simon 0:350011bf8be7 522 u16_t seglen = left > max_len ? max_len : left;
simon 0:350011bf8be7 523 #if TCP_CHECKSUM_ON_COPY
simon 0:350011bf8be7 524 u16_t chksum = 0;
simon 0:350011bf8be7 525 u8_t chksum_swapped = 0;
simon 0:350011bf8be7 526 #endif /* TCP_CHECKSUM_ON_COPY */
simon 0:350011bf8be7 527
simon 0:350011bf8be7 528 if (apiflags & TCP_WRITE_FLAG_COPY) {
simon 0:350011bf8be7 529 /* If copy is set, memory should be allocated and data copied
simon 0:350011bf8be7 530 * into pbuf */
simon 0:350011bf8be7 531 if ((p = tcp_pbuf_prealloc(PBUF_TRANSPORT, seglen + optlen, pcb->mss, &oversize, pcb, apiflags, queue == NULL)) == NULL) {
simon 0:350011bf8be7 532 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write : could not allocate memory for pbuf copy size %"U16_F"\n", seglen));
simon 0:350011bf8be7 533 goto memerr;
simon 0:350011bf8be7 534 }
simon 0:350011bf8be7 535 LWIP_ASSERT("tcp_write: check that first pbuf can hold the complete seglen",
simon 0:350011bf8be7 536 (p->len >= seglen));
simon 0:350011bf8be7 537 TCP_DATA_COPY2((char *)p->payload + optlen, (u8_t*)arg + pos, seglen, &chksum, &chksum_swapped);
simon 0:350011bf8be7 538 } else {
simon 0:350011bf8be7 539 /* Copy is not set: First allocate a pbuf for holding the data.
simon 0:350011bf8be7 540 * Since the referenced data is available at least until it is
simon 0:350011bf8be7 541 * sent out on the link (as it has to be ACKed by the remote
simon 0:350011bf8be7 542 * party) we can safely use PBUF_ROM instead of PBUF_REF here.
simon 0:350011bf8be7 543 */
simon 0:350011bf8be7 544 struct pbuf *p2;
simon 0:350011bf8be7 545 #if TCP_OVERSIZE
simon 0:350011bf8be7 546 LWIP_ASSERT("oversize == 0", oversize == 0);
simon 0:350011bf8be7 547 #endif /* TCP_OVERSIZE */
simon 0:350011bf8be7 548 if ((p2 = pbuf_alloc(PBUF_TRANSPORT, seglen, PBUF_ROM)) == NULL) {
simon 0:350011bf8be7 549 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: could not allocate memory for zero-copy pbuf\n"));
simon 0:350011bf8be7 550 goto memerr;
simon 0:350011bf8be7 551 }
simon 0:350011bf8be7 552 #if TCP_CHECKSUM_ON_COPY
simon 0:350011bf8be7 553 /* calculate the checksum of nocopy-data */
simon 0:350011bf8be7 554 chksum = ~inet_chksum((u8_t*)arg + pos, seglen);
simon 0:350011bf8be7 555 #endif /* TCP_CHECKSUM_ON_COPY */
simon 0:350011bf8be7 556 /* reference the non-volatile payload data */
simon 0:350011bf8be7 557 p2->payload = (u8_t*)arg + pos;
simon 0:350011bf8be7 558
simon 0:350011bf8be7 559 /* Second, allocate a pbuf for the headers. */
simon 0:350011bf8be7 560 if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
simon 0:350011bf8be7 561 /* If allocation fails, we have to deallocate the data pbuf as
simon 0:350011bf8be7 562 * well. */
simon 0:350011bf8be7 563 pbuf_free(p2);
simon 0:350011bf8be7 564 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: could not allocate memory for header pbuf\n"));
simon 0:350011bf8be7 565 goto memerr;
simon 0:350011bf8be7 566 }
simon 0:350011bf8be7 567 /* Concatenate the headers and data pbufs together. */
simon 0:350011bf8be7 568 pbuf_cat(p/*header*/, p2/*data*/);
simon 0:350011bf8be7 569 }
simon 0:350011bf8be7 570
simon 0:350011bf8be7 571 queuelen += pbuf_clen(p);
simon 0:350011bf8be7 572
simon 0:350011bf8be7 573 /* Now that there are more segments queued, we check again if the
simon 0:350011bf8be7 574 * length of the queue exceeds the configured maximum or
simon 0:350011bf8be7 575 * overflows. */
simon 0:350011bf8be7 576 if ((queuelen > TCP_SND_QUEUELEN) || (queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
simon 0:350011bf8be7 577 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 2, ("tcp_write: queue too long %"U16_F" (%"U16_F")\n", queuelen, TCP_SND_QUEUELEN));
simon 0:350011bf8be7 578 pbuf_free(p);
simon 0:350011bf8be7 579 goto memerr;
simon 0:350011bf8be7 580 }
simon 0:350011bf8be7 581
simon 0:350011bf8be7 582 if ((seg = tcp_create_segment(pcb, p, 0, pcb->snd_lbb + pos, optflags)) == NULL) {
simon 0:350011bf8be7 583 goto memerr;
simon 0:350011bf8be7 584 }
simon 0:350011bf8be7 585 #if TCP_OVERSIZE_DBGCHECK
simon 0:350011bf8be7 586 seg->oversize_left = oversize;
simon 0:350011bf8be7 587 #endif /* TCP_OVERSIZE_DBGCHECK */
simon 0:350011bf8be7 588 #if TCP_CHECKSUM_ON_COPY
simon 0:350011bf8be7 589 seg->chksum = chksum;
simon 0:350011bf8be7 590 seg->chksum_swapped = chksum_swapped;
simon 0:350011bf8be7 591 seg->flags |= TF_SEG_DATA_CHECKSUMMED;
simon 0:350011bf8be7 592 #endif /* TCP_CHECKSUM_ON_COPY */
simon 0:350011bf8be7 593 /* Fix dataptr for the nocopy case */
simon 0:350011bf8be7 594 if ((apiflags & TCP_WRITE_FLAG_COPY) == 0) {
simon 0:350011bf8be7 595 seg->dataptr = (u8_t*)arg + pos;
simon 0:350011bf8be7 596 }
simon 0:350011bf8be7 597
simon 0:350011bf8be7 598 /* first segment of to-be-queued data? */
simon 0:350011bf8be7 599 if (queue == NULL) {
simon 0:350011bf8be7 600 queue = seg;
simon 0:350011bf8be7 601 } else {
simon 0:350011bf8be7 602 /* Attach the segment to the end of the queued segments */
simon 0:350011bf8be7 603 LWIP_ASSERT("prev_seg != NULL", prev_seg != NULL);
simon 0:350011bf8be7 604 prev_seg->next = seg;
simon 0:350011bf8be7 605 }
simon 0:350011bf8be7 606 /* remember last segment of to-be-queued data for next iteration */
simon 0:350011bf8be7 607 prev_seg = seg;
simon 0:350011bf8be7 608
simon 0:350011bf8be7 609 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE, ("tcp_write: queueing %"U32_F":%"U32_F"\n",
simon 0:350011bf8be7 610 ntohl(seg->tcphdr->seqno),
simon 0:350011bf8be7 611 ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg)));
simon 0:350011bf8be7 612
simon 0:350011bf8be7 613 pos += seglen;
simon 0:350011bf8be7 614 }
simon 0:350011bf8be7 615
simon 0:350011bf8be7 616 /*
simon 0:350011bf8be7 617 * All three segmentation phases were successful. We can commit the
simon 0:350011bf8be7 618 * transaction.
simon 0:350011bf8be7 619 */
simon 0:350011bf8be7 620
simon 0:350011bf8be7 621 /*
simon 0:350011bf8be7 622 * Phase 1: If data has been added to the preallocated tail of
simon 0:350011bf8be7 623 * last_unsent, we update the length fields of the pbuf chain.
simon 0:350011bf8be7 624 */
simon 0:350011bf8be7 625 #if TCP_OVERSIZE
simon 0:350011bf8be7 626 if (oversize_used > 0) {
simon 0:350011bf8be7 627 struct pbuf *p;
simon 0:350011bf8be7 628 /* Bump tot_len of whole chain, len of tail */
simon 0:350011bf8be7 629 for (p = last_unsent->p; p; p = p->next) {
simon 0:350011bf8be7 630 p->tot_len += oversize_used;
simon 0:350011bf8be7 631 if (p->next == NULL) {
simon 0:350011bf8be7 632 TCP_DATA_COPY((char *)p->payload + p->len, arg, oversize_used, last_unsent);
simon 0:350011bf8be7 633 p->len += oversize_used;
simon 0:350011bf8be7 634 }
simon 0:350011bf8be7 635 }
simon 0:350011bf8be7 636 last_unsent->len += oversize_used;
simon 0:350011bf8be7 637 #if TCP_OVERSIZE_DBGCHECK
simon 0:350011bf8be7 638 last_unsent->oversize_left -= oversize_used;
simon 0:350011bf8be7 639 #endif /* TCP_OVERSIZE_DBGCHECK */
simon 0:350011bf8be7 640 }
simon 0:350011bf8be7 641 pcb->unsent_oversize = oversize;
simon 0:350011bf8be7 642 #endif /* TCP_OVERSIZE */
simon 0:350011bf8be7 643
simon 0:350011bf8be7 644 /*
simon 0:350011bf8be7 645 * Phase 2: concat_p can be concatenated onto last_unsent->p
simon 0:350011bf8be7 646 */
simon 0:350011bf8be7 647 if (concat_p != NULL) {
simon 0:350011bf8be7 648 LWIP_ASSERT("tcp_write: cannot concatenate when pcb->unsent is empty",
simon 0:350011bf8be7 649 (last_unsent != NULL));
simon 0:350011bf8be7 650 pbuf_cat(last_unsent->p, concat_p);
simon 0:350011bf8be7 651 last_unsent->len += concat_p->tot_len;
simon 0:350011bf8be7 652 #if TCP_CHECKSUM_ON_COPY
simon 0:350011bf8be7 653 if (concat_chksummed) {
simon 0:350011bf8be7 654 tcp_seg_add_chksum(concat_chksum, concat_chksummed, &last_unsent->chksum,
simon 0:350011bf8be7 655 &last_unsent->chksum_swapped);
simon 0:350011bf8be7 656 last_unsent->flags |= TF_SEG_DATA_CHECKSUMMED;
simon 0:350011bf8be7 657 }
simon 0:350011bf8be7 658 #endif /* TCP_CHECKSUM_ON_COPY */
simon 0:350011bf8be7 659 }
simon 0:350011bf8be7 660
simon 0:350011bf8be7 661 /*
simon 0:350011bf8be7 662 * Phase 3: Append queue to pcb->unsent. Queue may be NULL, but that
simon 0:350011bf8be7 663 * is harmless
simon 0:350011bf8be7 664 */
simon 0:350011bf8be7 665 if (last_unsent == NULL) {
simon 0:350011bf8be7 666 pcb->unsent = queue;
simon 0:350011bf8be7 667 } else {
simon 0:350011bf8be7 668 last_unsent->next = queue;
simon 0:350011bf8be7 669 }
simon 0:350011bf8be7 670
simon 0:350011bf8be7 671 /*
simon 0:350011bf8be7 672 * Finally update the pcb state.
simon 0:350011bf8be7 673 */
simon 0:350011bf8be7 674 pcb->snd_lbb += len;
simon 0:350011bf8be7 675 pcb->snd_buf -= len;
simon 0:350011bf8be7 676 pcb->snd_queuelen = queuelen;
simon 0:350011bf8be7 677
simon 0:350011bf8be7 678 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_write: %"S16_F" (after enqueued)\n",
simon 0:350011bf8be7 679 pcb->snd_queuelen));
simon 0:350011bf8be7 680 if (pcb->snd_queuelen != 0) {
simon 0:350011bf8be7 681 LWIP_ASSERT("tcp_write: valid queue length",
simon 0:350011bf8be7 682 pcb->unacked != NULL || pcb->unsent != NULL);
simon 0:350011bf8be7 683 }
simon 0:350011bf8be7 684
simon 0:350011bf8be7 685 /* Set the PSH flag in the last segment that we enqueued. */
simon 0:350011bf8be7 686 if (seg != NULL && seg->tcphdr != NULL && ((apiflags & TCP_WRITE_FLAG_MORE)==0)) {
simon 0:350011bf8be7 687 TCPH_SET_FLAG(seg->tcphdr, TCP_PSH);
simon 0:350011bf8be7 688 }
simon 0:350011bf8be7 689
simon 0:350011bf8be7 690 return ERR_OK;
simon 0:350011bf8be7 691 memerr:
simon 0:350011bf8be7 692 pcb->flags |= TF_NAGLEMEMERR;
simon 0:350011bf8be7 693 TCP_STATS_INC(tcp.memerr);
simon 0:350011bf8be7 694
simon 0:350011bf8be7 695 if (concat_p != NULL) {
simon 0:350011bf8be7 696 pbuf_free(concat_p);
simon 0:350011bf8be7 697 }
simon 0:350011bf8be7 698 if (queue != NULL) {
simon 0:350011bf8be7 699 tcp_segs_free(queue);
simon 0:350011bf8be7 700 }
simon 0:350011bf8be7 701 if (pcb->snd_queuelen != 0) {
simon 0:350011bf8be7 702 LWIP_ASSERT("tcp_write: valid queue length", pcb->unacked != NULL ||
simon 0:350011bf8be7 703 pcb->unsent != NULL);
simon 0:350011bf8be7 704 }
simon 0:350011bf8be7 705 LWIP_DEBUGF(TCP_QLEN_DEBUG | LWIP_DBG_STATE, ("tcp_write: %"S16_F" (with mem err)\n", pcb->snd_queuelen));
simon 0:350011bf8be7 706 return ERR_MEM;
simon 0:350011bf8be7 707 }
simon 0:350011bf8be7 708
simon 0:350011bf8be7 709 /**
simon 0:350011bf8be7 710 * Enqueue TCP options for transmission.
simon 0:350011bf8be7 711 *
simon 0:350011bf8be7 712 * Called by tcp_connect(), tcp_listen_input(), and tcp_send_ctrl().
simon 0:350011bf8be7 713 *
simon 0:350011bf8be7 714 * @param pcb Protocol control block for the TCP connection.
simon 0:350011bf8be7 715 * @param flags TCP header flags to set in the outgoing segment.
simon 0:350011bf8be7 716 * @param optdata pointer to TCP options, or NULL.
simon 0:350011bf8be7 717 * @param optlen length of TCP options in bytes.
simon 0:350011bf8be7 718 */
simon 0:350011bf8be7 719 err_t
simon 0:350011bf8be7 720 tcp_enqueue_flags(struct tcp_pcb *pcb, u8_t flags)
simon 0:350011bf8be7 721 {
simon 0:350011bf8be7 722 struct pbuf *p;
simon 0:350011bf8be7 723 struct tcp_seg *seg;
simon 0:350011bf8be7 724 u8_t optflags = 0;
simon 0:350011bf8be7 725 u8_t optlen = 0;
simon 0:350011bf8be7 726
simon 0:350011bf8be7 727 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: queuelen: %"U16_F"\n", (u16_t)pcb->snd_queuelen));
simon 0:350011bf8be7 728
simon 0:350011bf8be7 729 LWIP_ASSERT("tcp_enqueue_flags: need either TCP_SYN or TCP_FIN in flags (programmer violates API)",
simon 0:350011bf8be7 730 (flags & (TCP_SYN | TCP_FIN)) != 0);
simon 0:350011bf8be7 731
simon 0:350011bf8be7 732 /* check for configured max queuelen and possible overflow */
simon 0:350011bf8be7 733 if ((pcb->snd_queuelen >= TCP_SND_QUEUELEN) || (pcb->snd_queuelen > TCP_SNDQUEUELEN_OVERFLOW)) {
simon 0:350011bf8be7 734 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue_flags: too long queue %"U16_F" (max %"U16_F")\n",
simon 0:350011bf8be7 735 pcb->snd_queuelen, TCP_SND_QUEUELEN));
simon 0:350011bf8be7 736 TCP_STATS_INC(tcp.memerr);
simon 0:350011bf8be7 737 pcb->flags |= TF_NAGLEMEMERR;
simon 0:350011bf8be7 738 return ERR_MEM;
simon 0:350011bf8be7 739 }
simon 0:350011bf8be7 740
simon 0:350011bf8be7 741 if (flags & TCP_SYN) {
simon 0:350011bf8be7 742 optflags = TF_SEG_OPTS_MSS;
simon 0:350011bf8be7 743 }
simon 0:350011bf8be7 744 #if LWIP_TCP_TIMESTAMPS
simon 0:350011bf8be7 745 if ((pcb->flags & TF_TIMESTAMP)) {
simon 0:350011bf8be7 746 optflags |= TF_SEG_OPTS_TS;
simon 0:350011bf8be7 747 }
simon 0:350011bf8be7 748 #endif /* LWIP_TCP_TIMESTAMPS */
simon 0:350011bf8be7 749 optlen = LWIP_TCP_OPT_LENGTH(optflags);
simon 0:350011bf8be7 750
simon 0:350011bf8be7 751 /* tcp_enqueue_flags is always called with either SYN or FIN in flags.
simon 0:350011bf8be7 752 * We need one available snd_buf byte to do that.
simon 0:350011bf8be7 753 * This means we can't send FIN while snd_buf==0. A better fix would be to
simon 0:350011bf8be7 754 * not include SYN and FIN sequence numbers in the snd_buf count. */
simon 0:350011bf8be7 755 if (pcb->snd_buf == 0) {
simon 0:350011bf8be7 756 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | 3, ("tcp_enqueue_flags: no send buffer available\n"));
simon 0:350011bf8be7 757 TCP_STATS_INC(tcp.memerr);
simon 0:350011bf8be7 758 return ERR_MEM;
simon 0:350011bf8be7 759 }
simon 0:350011bf8be7 760
simon 0:350011bf8be7 761 /* Allocate pbuf with room for TCP header + options */
simon 0:350011bf8be7 762 if ((p = pbuf_alloc(PBUF_TRANSPORT, optlen, PBUF_RAM)) == NULL) {
simon 0:350011bf8be7 763 pcb->flags |= TF_NAGLEMEMERR;
simon 0:350011bf8be7 764 TCP_STATS_INC(tcp.memerr);
simon 0:350011bf8be7 765 return ERR_MEM;
simon 0:350011bf8be7 766 }
simon 0:350011bf8be7 767 LWIP_ASSERT("tcp_enqueue_flags: check that first pbuf can hold optlen",
simon 0:350011bf8be7 768 (p->len >= optlen));
simon 0:350011bf8be7 769
simon 0:350011bf8be7 770 /* Allocate memory for tcp_seg, and fill in fields. */
simon 0:350011bf8be7 771 if ((seg = tcp_create_segment(pcb, p, flags, pcb->snd_lbb, optflags)) == NULL) {
simon 0:350011bf8be7 772 pcb->flags |= TF_NAGLEMEMERR;
simon 0:350011bf8be7 773 TCP_STATS_INC(tcp.memerr);
simon 0:350011bf8be7 774 return ERR_MEM;
simon 0:350011bf8be7 775 }
simon 0:350011bf8be7 776 LWIP_ASSERT("tcp_enqueue_flags: invalid segment length", seg->len == 0);
simon 0:350011bf8be7 777
simon 0:350011bf8be7 778 LWIP_DEBUGF(TCP_OUTPUT_DEBUG | LWIP_DBG_TRACE,
simon 0:350011bf8be7 779 ("tcp_enqueue_flags: queueing %"U32_F":%"U32_F" (0x%"X16_F")\n",
simon 0:350011bf8be7 780 ntohl(seg->tcphdr->seqno),
simon 0:350011bf8be7 781 ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg),
simon 0:350011bf8be7 782 (u16_t)flags));
simon 0:350011bf8be7 783
simon 0:350011bf8be7 784 /* Now append seg to pcb->unsent queue */
simon 0:350011bf8be7 785 if (pcb->unsent == NULL) {
simon 0:350011bf8be7 786 pcb->unsent = seg;
simon 0:350011bf8be7 787 } else {
simon 0:350011bf8be7 788 struct tcp_seg *useg;
simon 0:350011bf8be7 789 for (useg = pcb->unsent; useg->next != NULL; useg = useg->next);
simon 0:350011bf8be7 790 useg->next = seg;
simon 0:350011bf8be7 791 }
simon 0:350011bf8be7 792 #if TCP_OVERSIZE
simon 0:350011bf8be7 793 /* The new unsent tail has no space */
simon 0:350011bf8be7 794 pcb->unsent_oversize = 0;
simon 0:350011bf8be7 795 #endif /* TCP_OVERSIZE */
simon 0:350011bf8be7 796
simon 0:350011bf8be7 797 /* SYN and FIN bump the sequence number */
simon 0:350011bf8be7 798 if ((flags & TCP_SYN) || (flags & TCP_FIN)) {
simon 0:350011bf8be7 799 pcb->snd_lbb++;
simon 0:350011bf8be7 800 /* optlen does not influence snd_buf */
simon 0:350011bf8be7 801 pcb->snd_buf--;
simon 0:350011bf8be7 802 }
simon 0:350011bf8be7 803 if (flags & TCP_FIN) {
simon 0:350011bf8be7 804 pcb->flags |= TF_FIN;
simon 0:350011bf8be7 805 }
simon 0:350011bf8be7 806
simon 0:350011bf8be7 807 /* update number of segments on the queues */
simon 0:350011bf8be7 808 pcb->snd_queuelen += pbuf_clen(seg->p);
simon 0:350011bf8be7 809 LWIP_DEBUGF(TCP_QLEN_DEBUG, ("tcp_enqueue_flags: %"S16_F" (after enqueued)\n", pcb->snd_queuelen));
simon 0:350011bf8be7 810 if (pcb->snd_queuelen != 0) {
simon 0:350011bf8be7 811 LWIP_ASSERT("tcp_enqueue_flags: invalid queue length",
simon 0:350011bf8be7 812 pcb->unacked != NULL || pcb->unsent != NULL);
simon 0:350011bf8be7 813 }
simon 0:350011bf8be7 814
simon 0:350011bf8be7 815 return ERR_OK;
simon 0:350011bf8be7 816 }
simon 0:350011bf8be7 817
simon 0:350011bf8be7 818
simon 0:350011bf8be7 819 #if LWIP_TCP_TIMESTAMPS
simon 0:350011bf8be7 820 /* Build a timestamp option (12 bytes long) at the specified options pointer)
simon 0:350011bf8be7 821 *
simon 0:350011bf8be7 822 * @param pcb tcp_pcb
simon 0:350011bf8be7 823 * @param opts option pointer where to store the timestamp option
simon 0:350011bf8be7 824 */
simon 0:350011bf8be7 825 static void
simon 0:350011bf8be7 826 tcp_build_timestamp_option(struct tcp_pcb *pcb, u32_t *opts)
simon 0:350011bf8be7 827 {
simon 0:350011bf8be7 828 /* Pad with two NOP options to make everything nicely aligned */
simon 0:350011bf8be7 829 opts[0] = PP_HTONL(0x0101080A);
simon 0:350011bf8be7 830 opts[1] = htonl(sys_now());
simon 0:350011bf8be7 831 opts[2] = htonl(pcb->ts_recent);
simon 0:350011bf8be7 832 }
simon 0:350011bf8be7 833 #endif
simon 0:350011bf8be7 834
simon 0:350011bf8be7 835 /** Send an ACK without data.
simon 0:350011bf8be7 836 *
simon 0:350011bf8be7 837 * @param pcb Protocol control block for the TCP connection to send the ACK
simon 0:350011bf8be7 838 */
simon 0:350011bf8be7 839 err_t
simon 0:350011bf8be7 840 tcp_send_empty_ack(struct tcp_pcb *pcb)
simon 0:350011bf8be7 841 {
simon 0:350011bf8be7 842 struct pbuf *p;
simon 0:350011bf8be7 843 struct tcp_hdr *tcphdr;
simon 0:350011bf8be7 844 u8_t optlen = 0;
simon 0:350011bf8be7 845
simon 0:350011bf8be7 846 #if LWIP_TCP_TIMESTAMPS
simon 0:350011bf8be7 847 if (pcb->flags & TF_TIMESTAMP) {
simon 0:350011bf8be7 848 optlen = LWIP_TCP_OPT_LENGTH(TF_SEG_OPTS_TS);
simon 0:350011bf8be7 849 }
simon 0:350011bf8be7 850 #endif
simon 0:350011bf8be7 851
simon 0:350011bf8be7 852 p = tcp_output_alloc_header(pcb, optlen, 0, htonl(pcb->snd_nxt));
simon 0:350011bf8be7 853 if (p == NULL) {
simon 0:350011bf8be7 854 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: (ACK) could not allocate pbuf\n"));
simon 0:350011bf8be7 855 return ERR_BUF;
simon 0:350011bf8be7 856 }
simon 0:350011bf8be7 857 tcphdr = (struct tcp_hdr *)p->payload;
simon 0:350011bf8be7 858 LWIP_DEBUGF(TCP_OUTPUT_DEBUG,
simon 0:350011bf8be7 859 ("tcp_output: sending ACK for %"U32_F"\n", pcb->rcv_nxt));
simon 0:350011bf8be7 860 /* remove ACK flags from the PCB, as we send an empty ACK now */
simon 0:350011bf8be7 861 pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
simon 0:350011bf8be7 862
simon 0:350011bf8be7 863 /* NB. MSS option is only sent on SYNs, so ignore it here */
simon 0:350011bf8be7 864 #if LWIP_TCP_TIMESTAMPS
simon 0:350011bf8be7 865 pcb->ts_lastacksent = pcb->rcv_nxt;
simon 0:350011bf8be7 866
simon 0:350011bf8be7 867 if (pcb->flags & TF_TIMESTAMP) {
simon 0:350011bf8be7 868 tcp_build_timestamp_option(pcb, (u32_t *)(tcphdr + 1));
simon 0:350011bf8be7 869 }
simon 0:350011bf8be7 870 #endif
simon 0:350011bf8be7 871
simon 0:350011bf8be7 872 #if CHECKSUM_GEN_TCP
simon 0:350011bf8be7 873 tcphdr->chksum = inet_chksum_pseudo(p, &(pcb->local_ip), &(pcb->remote_ip),
simon 0:350011bf8be7 874 IP_PROTO_TCP, p->tot_len);
simon 0:350011bf8be7 875 #endif
simon 0:350011bf8be7 876 #if LWIP_NETIF_HWADDRHINT
simon 0:350011bf8be7 877 ip_output_hinted(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
simon 0:350011bf8be7 878 IP_PROTO_TCP, &(pcb->addr_hint));
simon 0:350011bf8be7 879 #else /* LWIP_NETIF_HWADDRHINT*/
simon 0:350011bf8be7 880 ip_output(p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
simon 0:350011bf8be7 881 IP_PROTO_TCP);
simon 0:350011bf8be7 882 #endif /* LWIP_NETIF_HWADDRHINT*/
simon 0:350011bf8be7 883 pbuf_free(p);
simon 0:350011bf8be7 884
simon 0:350011bf8be7 885 return ERR_OK;
simon 0:350011bf8be7 886 }
simon 0:350011bf8be7 887
simon 0:350011bf8be7 888 /**
simon 0:350011bf8be7 889 * Find out what we can send and send it
simon 0:350011bf8be7 890 *
simon 0:350011bf8be7 891 * @param pcb Protocol control block for the TCP connection to send data
simon 0:350011bf8be7 892 * @return ERR_OK if data has been sent or nothing to send
simon 0:350011bf8be7 893 * another err_t on error
simon 0:350011bf8be7 894 */
simon 0:350011bf8be7 895 err_t
simon 0:350011bf8be7 896 tcp_output(struct tcp_pcb *pcb)
simon 0:350011bf8be7 897 {
simon 0:350011bf8be7 898 struct tcp_seg *seg, *useg;
simon 0:350011bf8be7 899 u32_t wnd, snd_nxt;
simon 0:350011bf8be7 900 #if TCP_CWND_DEBUG
simon 0:350011bf8be7 901 s16_t i = 0;
simon 0:350011bf8be7 902 #endif /* TCP_CWND_DEBUG */
simon 0:350011bf8be7 903
simon 0:350011bf8be7 904 /* First, check if we are invoked by the TCP input processing
simon 0:350011bf8be7 905 code. If so, we do not output anything. Instead, we rely on the
simon 0:350011bf8be7 906 input processing code to call us when input processing is done
simon 0:350011bf8be7 907 with. */
simon 0:350011bf8be7 908 if (tcp_input_pcb == pcb) {
simon 0:350011bf8be7 909 return ERR_OK;
simon 0:350011bf8be7 910 }
simon 0:350011bf8be7 911
simon 0:350011bf8be7 912 wnd = LWIP_MIN(pcb->snd_wnd, pcb->cwnd);
simon 0:350011bf8be7 913
simon 0:350011bf8be7 914 seg = pcb->unsent;
simon 0:350011bf8be7 915
simon 0:350011bf8be7 916 /* If the TF_ACK_NOW flag is set and no data will be sent (either
simon 0:350011bf8be7 917 * because the ->unsent queue is empty or because the window does
simon 0:350011bf8be7 918 * not allow it), construct an empty ACK segment and send it.
simon 0:350011bf8be7 919 *
simon 0:350011bf8be7 920 * If data is to be sent, we will just piggyback the ACK (see below).
simon 0:350011bf8be7 921 */
simon 0:350011bf8be7 922 if (pcb->flags & TF_ACK_NOW &&
simon 0:350011bf8be7 923 (seg == NULL ||
simon 0:350011bf8be7 924 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > wnd)) {
simon 0:350011bf8be7 925 return tcp_send_empty_ack(pcb);
simon 0:350011bf8be7 926 }
simon 0:350011bf8be7 927
simon 0:350011bf8be7 928 /* useg should point to last segment on unacked queue */
simon 0:350011bf8be7 929 useg = pcb->unacked;
simon 0:350011bf8be7 930 if (useg != NULL) {
simon 0:350011bf8be7 931 for (; useg->next != NULL; useg = useg->next);
simon 0:350011bf8be7 932 }
simon 0:350011bf8be7 933
simon 0:350011bf8be7 934 #if TCP_OUTPUT_DEBUG
simon 0:350011bf8be7 935 if (seg == NULL) {
simon 0:350011bf8be7 936 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: nothing to send (%p)\n",
simon 0:350011bf8be7 937 (void*)pcb->unsent));
simon 0:350011bf8be7 938 }
simon 0:350011bf8be7 939 #endif /* TCP_OUTPUT_DEBUG */
simon 0:350011bf8be7 940 #if TCP_CWND_DEBUG
simon 0:350011bf8be7 941 if (seg == NULL) {
simon 0:350011bf8be7 942 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"U16_F
simon 0:350011bf8be7 943 ", cwnd %"U16_F", wnd %"U32_F
simon 0:350011bf8be7 944 ", seg == NULL, ack %"U32_F"\n",
simon 0:350011bf8be7 945 pcb->snd_wnd, pcb->cwnd, wnd, pcb->lastack));
simon 0:350011bf8be7 946 } else {
simon 0:350011bf8be7 947 LWIP_DEBUGF(TCP_CWND_DEBUG,
simon 0:350011bf8be7 948 ("tcp_output: snd_wnd %"U16_F", cwnd %"U16_F", wnd %"U32_F
simon 0:350011bf8be7 949 ", effwnd %"U32_F", seq %"U32_F", ack %"U32_F"\n",
simon 0:350011bf8be7 950 pcb->snd_wnd, pcb->cwnd, wnd,
simon 0:350011bf8be7 951 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len,
simon 0:350011bf8be7 952 ntohl(seg->tcphdr->seqno), pcb->lastack));
simon 0:350011bf8be7 953 }
simon 0:350011bf8be7 954 #endif /* TCP_CWND_DEBUG */
simon 0:350011bf8be7 955 /* data available and window allows it to be sent? */
simon 0:350011bf8be7 956 while (seg != NULL &&
simon 0:350011bf8be7 957 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len <= wnd) {
simon 0:350011bf8be7 958 LWIP_ASSERT("RST not expected here!",
simon 0:350011bf8be7 959 (TCPH_FLAGS(seg->tcphdr) & TCP_RST) == 0);
simon 0:350011bf8be7 960 /* Stop sending if the nagle algorithm would prevent it
simon 0:350011bf8be7 961 * Don't stop:
simon 0:350011bf8be7 962 * - if tcp_write had a memory error before (prevent delayed ACK timeout) or
simon 0:350011bf8be7 963 * - if FIN was already enqueued for this PCB (SYN is always alone in a segment -
simon 0:350011bf8be7 964 * either seg->next != NULL or pcb->unacked == NULL;
simon 0:350011bf8be7 965 * RST is no sent using tcp_write/tcp_output.
simon 0:350011bf8be7 966 */
simon 0:350011bf8be7 967 if((tcp_do_output_nagle(pcb) == 0) &&
simon 0:350011bf8be7 968 ((pcb->flags & (TF_NAGLEMEMERR | TF_FIN)) == 0)){
simon 0:350011bf8be7 969 break;
simon 0:350011bf8be7 970 }
simon 0:350011bf8be7 971 #if TCP_CWND_DEBUG
simon 0:350011bf8be7 972 LWIP_DEBUGF(TCP_CWND_DEBUG, ("tcp_output: snd_wnd %"U16_F", cwnd %"U16_F", wnd %"U32_F", effwnd %"U32_F", seq %"U32_F", ack %"U32_F", i %"S16_F"\n",
simon 0:350011bf8be7 973 pcb->snd_wnd, pcb->cwnd, wnd,
simon 0:350011bf8be7 974 ntohl(seg->tcphdr->seqno) + seg->len -
simon 0:350011bf8be7 975 pcb->lastack,
simon 0:350011bf8be7 976 ntohl(seg->tcphdr->seqno), pcb->lastack, i));
simon 0:350011bf8be7 977 ++i;
simon 0:350011bf8be7 978 #endif /* TCP_CWND_DEBUG */
simon 0:350011bf8be7 979
simon 0:350011bf8be7 980 pcb->unsent = seg->next;
simon 0:350011bf8be7 981
simon 0:350011bf8be7 982 if (pcb->state != SYN_SENT) {
simon 0:350011bf8be7 983 TCPH_SET_FLAG(seg->tcphdr, TCP_ACK);
simon 0:350011bf8be7 984 pcb->flags &= ~(TF_ACK_DELAY | TF_ACK_NOW);
simon 0:350011bf8be7 985 }
simon 0:350011bf8be7 986
simon 0:350011bf8be7 987 tcp_output_segment(seg, pcb);
simon 0:350011bf8be7 988
simon 0:350011bf8be7 989 snd_nxt = ntohl(seg->tcphdr->seqno) + TCP_TCPLEN(seg);
simon 0:350011bf8be7 990
simon 0:350011bf8be7 991 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output: pcb->snd_nxt was %"U32_F" seqno=%"U32_F" len=%"U32_F" new snd_nxt=%"U32_F"\n", //DG
simon 0:350011bf8be7 992 pcb->snd_nxt, ntohl(seg->tcphdr->seqno), TCP_TCPLEN(seg), snd_nxt));
simon 0:350011bf8be7 993
simon 0:350011bf8be7 994 if (TCP_SEQ_LT(pcb->snd_nxt, snd_nxt)) {
simon 0:350011bf8be7 995 pcb->snd_nxt = snd_nxt;
simon 0:350011bf8be7 996 }
simon 0:350011bf8be7 997 /* put segment on unacknowledged list if length > 0 */
simon 0:350011bf8be7 998 if (TCP_TCPLEN(seg) > 0) {
simon 0:350011bf8be7 999 seg->next = NULL;
simon 0:350011bf8be7 1000 /* unacked list is empty? */
simon 0:350011bf8be7 1001 if (pcb->unacked == NULL) {
simon 0:350011bf8be7 1002 pcb->unacked = seg;
simon 0:350011bf8be7 1003 useg = seg;
simon 0:350011bf8be7 1004 /* unacked list is not empty? */
simon 0:350011bf8be7 1005 } else {
simon 0:350011bf8be7 1006 /* In the case of fast retransmit, the packet should not go to the tail
simon 0:350011bf8be7 1007 * of the unacked queue, but rather somewhere before it. We need to check for
simon 0:350011bf8be7 1008 * this case. -STJ Jul 27, 2004 */
simon 0:350011bf8be7 1009 if (TCP_SEQ_LT(ntohl(seg->tcphdr->seqno), ntohl(useg->tcphdr->seqno))) {
simon 0:350011bf8be7 1010 /* add segment to before tail of unacked list, keeping the list sorted */
simon 0:350011bf8be7 1011 struct tcp_seg **cur_seg = &(pcb->unacked);
simon 0:350011bf8be7 1012 while (*cur_seg &&
simon 0:350011bf8be7 1013 TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno), ntohl(seg->tcphdr->seqno))) {
simon 0:350011bf8be7 1014 cur_seg = &((*cur_seg)->next );
simon 0:350011bf8be7 1015 }
simon 0:350011bf8be7 1016 seg->next = (*cur_seg);
simon 0:350011bf8be7 1017 (*cur_seg) = seg;
simon 0:350011bf8be7 1018 } else {
simon 0:350011bf8be7 1019 /* add segment to tail of unacked list */
simon 0:350011bf8be7 1020 useg->next = seg;
simon 0:350011bf8be7 1021 useg = useg->next;
simon 0:350011bf8be7 1022 }
simon 0:350011bf8be7 1023 }
simon 0:350011bf8be7 1024 /* do not queue empty segments on the unacked list */
simon 0:350011bf8be7 1025 } else {
simon 0:350011bf8be7 1026 tcp_seg_free(seg);
simon 0:350011bf8be7 1027 }
simon 0:350011bf8be7 1028 seg = pcb->unsent;
simon 0:350011bf8be7 1029 }
simon 0:350011bf8be7 1030 #if TCP_OVERSIZE
simon 0:350011bf8be7 1031 if (pcb->unsent == NULL) {
simon 0:350011bf8be7 1032 /* last unsent has been removed, reset unsent_oversize */
simon 0:350011bf8be7 1033 pcb->unsent_oversize = 0;
simon 0:350011bf8be7 1034 }
simon 0:350011bf8be7 1035 #endif /* TCP_OVERSIZE */
simon 0:350011bf8be7 1036
simon 0:350011bf8be7 1037 if (seg != NULL && pcb->persist_backoff == 0 &&
simon 0:350011bf8be7 1038 ntohl(seg->tcphdr->seqno) - pcb->lastack + seg->len > pcb->snd_wnd) {
simon 0:350011bf8be7 1039 /* prepare for persist timer */
simon 0:350011bf8be7 1040 pcb->persist_cnt = 0;
simon 0:350011bf8be7 1041 pcb->persist_backoff = 1;
simon 0:350011bf8be7 1042 }
simon 0:350011bf8be7 1043
simon 0:350011bf8be7 1044 pcb->flags &= ~TF_NAGLEMEMERR;
simon 0:350011bf8be7 1045 return ERR_OK;
simon 0:350011bf8be7 1046 }
simon 0:350011bf8be7 1047
simon 0:350011bf8be7 1048 /**
simon 0:350011bf8be7 1049 * Called by tcp_output() to actually send a TCP segment over IP.
simon 0:350011bf8be7 1050 *
simon 0:350011bf8be7 1051 * @param seg the tcp_seg to send
simon 0:350011bf8be7 1052 * @param pcb the tcp_pcb for the TCP connection used to send the segment
simon 0:350011bf8be7 1053 */
simon 0:350011bf8be7 1054 static void
simon 0:350011bf8be7 1055 tcp_output_segment(struct tcp_seg *seg, struct tcp_pcb *pcb)
simon 0:350011bf8be7 1056 {
simon 0:350011bf8be7 1057 u16_t len;
simon 0:350011bf8be7 1058 struct netif *netif;
simon 0:350011bf8be7 1059 u32_t *opts;
simon 0:350011bf8be7 1060
simon 0:350011bf8be7 1061 /** @bug Exclude retransmitted segments from this count. */
simon 0:350011bf8be7 1062 snmp_inc_tcpoutsegs();
simon 0:350011bf8be7 1063
simon 0:350011bf8be7 1064 /* The TCP header has already been constructed, but the ackno and
simon 0:350011bf8be7 1065 wnd fields remain. */
simon 0:350011bf8be7 1066 seg->tcphdr->ackno = htonl(pcb->rcv_nxt);
simon 0:350011bf8be7 1067
simon 0:350011bf8be7 1068 /* advertise our receive window size in this TCP segment */
simon 0:350011bf8be7 1069 seg->tcphdr->wnd = htons(pcb->rcv_ann_wnd);
simon 0:350011bf8be7 1070
simon 0:350011bf8be7 1071 pcb->rcv_ann_right_edge = pcb->rcv_nxt + pcb->rcv_ann_wnd;
simon 0:350011bf8be7 1072
simon 0:350011bf8be7 1073 /* Add any requested options. NB MSS option is only set on SYN
simon 0:350011bf8be7 1074 packets, so ignore it here */
simon 0:350011bf8be7 1075 LWIP_ASSERT("seg->tcphdr not aligned", ((mem_ptr_t)(seg->tcphdr + 1) % 4) == 0);
simon 0:350011bf8be7 1076 opts = (u32_t *)(void *)(seg->tcphdr + 1);
simon 0:350011bf8be7 1077 if (seg->flags & TF_SEG_OPTS_MSS) {
simon 0:350011bf8be7 1078 TCP_BUILD_MSS_OPTION(*opts);
simon 0:350011bf8be7 1079 opts += 1;
simon 0:350011bf8be7 1080 }
simon 0:350011bf8be7 1081 #if LWIP_TCP_TIMESTAMPS
simon 0:350011bf8be7 1082 pcb->ts_lastacksent = pcb->rcv_nxt;
simon 0:350011bf8be7 1083
simon 0:350011bf8be7 1084 if (seg->flags & TF_SEG_OPTS_TS) {
simon 0:350011bf8be7 1085 tcp_build_timestamp_option(pcb, opts);
simon 0:350011bf8be7 1086 opts += 3;
simon 0:350011bf8be7 1087 }
simon 0:350011bf8be7 1088 #endif
simon 0:350011bf8be7 1089
simon 0:350011bf8be7 1090 /* If we don't have a local IP address, we get one by
simon 0:350011bf8be7 1091 calling ip_route(). */
simon 0:350011bf8be7 1092 if (ip_addr_isany(&(pcb->local_ip))) {
simon 0:350011bf8be7 1093 netif = ip_route(&(pcb->remote_ip));
simon 0:350011bf8be7 1094 if (netif == NULL) {
simon 0:350011bf8be7 1095 return;
simon 0:350011bf8be7 1096 }
simon 0:350011bf8be7 1097 ip_addr_copy(pcb->local_ip, netif->ip_addr);
simon 0:350011bf8be7 1098 }
simon 0:350011bf8be7 1099
simon 0:350011bf8be7 1100 /* Set retransmission timer running if it is not currently enabled */
simon 0:350011bf8be7 1101 if(pcb->rtime == -1) {
simon 0:350011bf8be7 1102 pcb->rtime = 0;
simon 0:350011bf8be7 1103 }
simon 0:350011bf8be7 1104
simon 0:350011bf8be7 1105 if (pcb->rttest == 0) {
simon 0:350011bf8be7 1106 pcb->rttest = tcp_ticks;
simon 0:350011bf8be7 1107 pcb->rtseq = ntohl(seg->tcphdr->seqno);
simon 0:350011bf8be7 1108
simon 0:350011bf8be7 1109 LWIP_DEBUGF(TCP_RTO_DEBUG, ("tcp_output_segment: rtseq %"U32_F"\n", pcb->rtseq));
simon 0:350011bf8be7 1110 }
simon 0:350011bf8be7 1111 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: %"U32_F":%"U32_F"\n",
simon 0:350011bf8be7 1112 htonl(seg->tcphdr->seqno), htonl(seg->tcphdr->seqno) +
simon 0:350011bf8be7 1113 seg->len));
simon 0:350011bf8be7 1114
simon 0:350011bf8be7 1115 len = (u16_t)((u8_t *)seg->tcphdr - (u8_t *)seg->p->payload);
simon 0:350011bf8be7 1116
simon 0:350011bf8be7 1117 seg->p->len -= len;
simon 0:350011bf8be7 1118 seg->p->tot_len -= len;
simon 0:350011bf8be7 1119
simon 0:350011bf8be7 1120 seg->p->payload = seg->tcphdr;
simon 0:350011bf8be7 1121
simon 0:350011bf8be7 1122 seg->tcphdr->chksum = 0;
simon 0:350011bf8be7 1123 #if CHECKSUM_GEN_TCP
simon 0:350011bf8be7 1124 #if TCP_CHECKSUM_ON_COPY
simon 0:350011bf8be7 1125 {
simon 0:350011bf8be7 1126 u32_t acc;
simon 0:350011bf8be7 1127 #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
simon 0:350011bf8be7 1128 u16_t chksum_slow = inet_chksum_pseudo(seg->p, &(pcb->local_ip),
simon 0:350011bf8be7 1129 &(pcb->remote_ip),
simon 0:350011bf8be7 1130 IP_PROTO_TCP, seg->p->tot_len);
simon 0:350011bf8be7 1131 #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */
simon 0:350011bf8be7 1132 if ((seg->flags & TF_SEG_DATA_CHECKSUMMED) == 0) {
simon 0:350011bf8be7 1133 LWIP_ASSERT("data included but not checksummed",
simon 0:350011bf8be7 1134 seg->p->tot_len == (TCPH_HDRLEN(seg->tcphdr) * 4));
simon 0:350011bf8be7 1135 }
simon 0:350011bf8be7 1136
simon 0:350011bf8be7 1137 /* rebuild TCP header checksum (TCP header changes for retransmissions!) */
simon 0:350011bf8be7 1138 acc = inet_chksum_pseudo_partial(seg->p, &(pcb->local_ip),
simon 0:350011bf8be7 1139 &(pcb->remote_ip),
simon 0:350011bf8be7 1140 IP_PROTO_TCP, seg->p->tot_len, TCPH_HDRLEN(seg->tcphdr) * 4);
simon 0:350011bf8be7 1141 /* add payload checksum */
simon 0:350011bf8be7 1142 if (seg->chksum_swapped) {
simon 0:350011bf8be7 1143 seg->chksum = SWAP_BYTES_IN_WORD(seg->chksum);
simon 0:350011bf8be7 1144 seg->chksum_swapped = 0;
simon 0:350011bf8be7 1145 }
simon 0:350011bf8be7 1146 acc += (u16_t)~(seg->chksum);
simon 0:350011bf8be7 1147 seg->tcphdr->chksum = FOLD_U32T(acc);
simon 0:350011bf8be7 1148 #if TCP_CHECKSUM_ON_COPY_SANITY_CHECK
simon 0:350011bf8be7 1149 if (chksum_slow != seg->tcphdr->chksum) {
simon 0:350011bf8be7 1150 LWIP_DEBUGF(TCP_DEBUG | LWIP_DBG_LEVEL_WARNING,
simon 0:350011bf8be7 1151 ("tcp_output_segment: calculated checksum is %"X16_F" instead of %"X16_F"\n",
simon 0:350011bf8be7 1152 seg->tcphdr->chksum, chksum_slow));
simon 0:350011bf8be7 1153 seg->tcphdr->chksum = chksum_slow;
simon 0:350011bf8be7 1154 }
simon 0:350011bf8be7 1155 #endif /* TCP_CHECKSUM_ON_COPY_SANITY_CHECK */
simon 0:350011bf8be7 1156 }
simon 0:350011bf8be7 1157 #else /* TCP_CHECKSUM_ON_COPY */
simon 0:350011bf8be7 1158 seg->tcphdr->chksum = inet_chksum_pseudo(seg->p, &(pcb->local_ip),
simon 0:350011bf8be7 1159 &(pcb->remote_ip),
simon 0:350011bf8be7 1160 IP_PROTO_TCP, seg->p->tot_len);
simon 0:350011bf8be7 1161 #endif /* TCP_CHECKSUM_ON_COPY */
simon 0:350011bf8be7 1162 #endif /* CHECKSUM_GEN_TCP */
simon 0:350011bf8be7 1163 TCP_STATS_INC(tcp.xmit);
simon 0:350011bf8be7 1164
simon 0:350011bf8be7 1165 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: Before ip_out seqno = %"U32_F"\n",
simon 0:350011bf8be7 1166 htonl(seg->tcphdr->seqno))); //DG
simon 0:350011bf8be7 1167
simon 0:350011bf8be7 1168 #if LWIP_NETIF_HWADDRHINT
simon 0:350011bf8be7 1169 ip_output_hinted(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
simon 0:350011bf8be7 1170 IP_PROTO_TCP, &(pcb->addr_hint));
simon 0:350011bf8be7 1171 #else /* LWIP_NETIF_HWADDRHINT*/
simon 0:350011bf8be7 1172 ip_output(seg->p, &(pcb->local_ip), &(pcb->remote_ip), pcb->ttl, pcb->tos,
simon 0:350011bf8be7 1173 IP_PROTO_TCP);
simon 0:350011bf8be7 1174 #endif /* LWIP_NETIF_HWADDRHINT*/
simon 0:350011bf8be7 1175
simon 0:350011bf8be7 1176 LWIP_DEBUGF(TCP_OUTPUT_DEBUG, ("tcp_output_segment: After ip_out seqno = %"U32_F"\n",
simon 0:350011bf8be7 1177 htonl(seg->tcphdr->seqno))); //DG
simon 0:350011bf8be7 1178
simon 0:350011bf8be7 1179
simon 0:350011bf8be7 1180 }
simon 0:350011bf8be7 1181
simon 0:350011bf8be7 1182 /**
simon 0:350011bf8be7 1183 * Send a TCP RESET packet (empty segment with RST flag set) either to
simon 0:350011bf8be7 1184 * abort a connection or to show that there is no matching local connection
simon 0:350011bf8be7 1185 * for a received segment.
simon 0:350011bf8be7 1186 *
simon 0:350011bf8be7 1187 * Called by tcp_abort() (to abort a local connection), tcp_input() (if no
simon 0:350011bf8be7 1188 * matching local pcb was found), tcp_listen_input() (if incoming segment
simon 0:350011bf8be7 1189 * has ACK flag set) and tcp_process() (received segment in the wrong state)
simon 0:350011bf8be7 1190 *
simon 0:350011bf8be7 1191 * Since a RST segment is in most cases not sent for an active connection,
simon 0:350011bf8be7 1192 * tcp_rst() has a number of arguments that are taken from a tcp_pcb for
simon 0:350011bf8be7 1193 * most other segment output functions.
simon 0:350011bf8be7 1194 *
simon 0:350011bf8be7 1195 * @param seqno the sequence number to use for the outgoing segment
simon 0:350011bf8be7 1196 * @param ackno the acknowledge number to use for the outgoing segment
simon 0:350011bf8be7 1197 * @param local_ip the local IP address to send the segment from
simon 0:350011bf8be7 1198 * @param remote_ip the remote IP address to send the segment to
simon 0:350011bf8be7 1199 * @param local_port the local TCP port to send the segment from
simon 0:350011bf8be7 1200 * @param remote_port the remote TCP port to send the segment to
simon 0:350011bf8be7 1201 */
simon 0:350011bf8be7 1202 void
simon 0:350011bf8be7 1203 tcp_rst(u32_t seqno, u32_t ackno,
simon 0:350011bf8be7 1204 ip_addr_t *local_ip, ip_addr_t *remote_ip,
simon 0:350011bf8be7 1205 u16_t local_port, u16_t remote_port)
simon 0:350011bf8be7 1206 {
simon 0:350011bf8be7 1207 struct pbuf *p;
simon 0:350011bf8be7 1208 struct tcp_hdr *tcphdr;
simon 0:350011bf8be7 1209 p = pbuf_alloc(PBUF_IP, TCP_HLEN, PBUF_RAM);
simon 0:350011bf8be7 1210 if (p == NULL) {
simon 0:350011bf8be7 1211 LWIP_DEBUGF(TCP_DEBUG, ("tcp_rst: could not allocate memory for pbuf\n"));
simon 0:350011bf8be7 1212 return;
simon 0:350011bf8be7 1213 }
simon 0:350011bf8be7 1214 LWIP_ASSERT("check that first pbuf can hold struct tcp_hdr",
simon 0:350011bf8be7 1215 (p->len >= sizeof(struct tcp_hdr)));
simon 0:350011bf8be7 1216
simon 0:350011bf8be7 1217 tcphdr = (struct tcp_hdr *)p->payload;
simon 0:350011bf8be7 1218 tcphdr->src = htons(local_port);
simon 0:350011bf8be7 1219 tcphdr->dest = htons(remote_port);
simon 0:350011bf8be7 1220 tcphdr->seqno = htonl(seqno);
simon 0:350011bf8be7 1221 tcphdr->ackno = htonl(ackno);
simon 0:350011bf8be7 1222 TCPH_HDRLEN_FLAGS_SET(tcphdr, TCP_HLEN/4, TCP_RST | TCP_ACK);
simon 0:350011bf8be7 1223 tcphdr->wnd = PP_HTONS(TCP_WND);
simon 0:350011bf8be7 1224 tcphdr->chksum = 0;
simon 0:350011bf8be7 1225 tcphdr->urgp = 0;
simon 0:350011bf8be7 1226
simon 0:350011bf8be7 1227 #if CHECKSUM_GEN_TCP
simon 0:350011bf8be7 1228 tcphdr->chksum = inet_chksum_pseudo(p, local_ip, remote_ip,
simon 0:350011bf8be7 1229 IP_PROTO_TCP, p->tot_len);
simon 0:350011bf8be7 1230 #endif
simon 0:350011bf8be7 1231 TCP_STATS_INC(tcp.xmit);
simon 0:350011bf8be7 1232 snmp_inc_tcpoutrsts();
simon 0:350011bf8be7 1233 /* Send output with hardcoded TTL since we have no access to the pcb */
simon 0:350011bf8be7 1234 ip_output(p, local_ip, remote_ip, TCP_TTL, 0, IP_PROTO_TCP);
simon 0:350011bf8be7 1235 pbuf_free(p);
simon 0:350011bf8be7 1236 LWIP_DEBUGF(TCP_RST_DEBUG, ("tcp_rst: seqno %"U32_F" ackno %"U32_F".\n", seqno, ackno));
simon 0:350011bf8be7 1237 }
simon 0:350011bf8be7 1238
simon 0:350011bf8be7 1239 /**
simon 0:350011bf8be7 1240 * Requeue all unacked segments for retransmission
simon 0:350011bf8be7 1241 *
simon 0:350011bf8be7 1242 * Called by tcp_slowtmr() for slow retransmission.
simon 0:350011bf8be7 1243 *
simon 0:350011bf8be7 1244 * @param pcb the tcp_pcb for which to re-enqueue all unacked segments
simon 0:350011bf8be7 1245 */
simon 0:350011bf8be7 1246 void
simon 0:350011bf8be7 1247 tcp_rexmit_rto(struct tcp_pcb *pcb)
simon 0:350011bf8be7 1248 {
simon 0:350011bf8be7 1249 struct tcp_seg *seg;
simon 0:350011bf8be7 1250
simon 0:350011bf8be7 1251 if (pcb->unacked == NULL) {
simon 0:350011bf8be7 1252 return;
simon 0:350011bf8be7 1253 }
simon 0:350011bf8be7 1254
simon 0:350011bf8be7 1255 /* Move all unacked segments to the head of the unsent queue */
simon 0:350011bf8be7 1256 for (seg = pcb->unacked; seg->next != NULL; seg = seg->next);
simon 0:350011bf8be7 1257 /* concatenate unsent queue after unacked queue */
simon 0:350011bf8be7 1258 seg->next = pcb->unsent;
simon 0:350011bf8be7 1259 /* unsent queue is the concatenated queue (of unacked, unsent) */
simon 0:350011bf8be7 1260 pcb->unsent = pcb->unacked;
simon 0:350011bf8be7 1261 /* unacked queue is now empty */
simon 0:350011bf8be7 1262 pcb->unacked = NULL;
simon 0:350011bf8be7 1263
simon 0:350011bf8be7 1264 /* increment number of retransmissions */
simon 0:350011bf8be7 1265 ++pcb->nrtx;
simon 0:350011bf8be7 1266
simon 0:350011bf8be7 1267 /* Don't take any RTT measurements after retransmitting. */
simon 0:350011bf8be7 1268 pcb->rttest = 0;
simon 0:350011bf8be7 1269
simon 0:350011bf8be7 1270 /* Do the actual retransmission */
simon 0:350011bf8be7 1271 tcp_output(pcb);
simon 0:350011bf8be7 1272 }
simon 0:350011bf8be7 1273
simon 0:350011bf8be7 1274 /**
simon 0:350011bf8be7 1275 * Requeue the first unacked segment for retransmission
simon 0:350011bf8be7 1276 *
simon 0:350011bf8be7 1277 * Called by tcp_receive() for fast retramsmit.
simon 0:350011bf8be7 1278 *
simon 0:350011bf8be7 1279 * @param pcb the tcp_pcb for which to retransmit the first unacked segment
simon 0:350011bf8be7 1280 */
simon 0:350011bf8be7 1281 void
simon 0:350011bf8be7 1282 tcp_rexmit(struct tcp_pcb *pcb)
simon 0:350011bf8be7 1283 {
simon 0:350011bf8be7 1284 struct tcp_seg *seg;
simon 0:350011bf8be7 1285 struct tcp_seg **cur_seg;
simon 0:350011bf8be7 1286
simon 0:350011bf8be7 1287 if (pcb->unacked == NULL) {
simon 0:350011bf8be7 1288 return;
simon 0:350011bf8be7 1289 }
simon 0:350011bf8be7 1290
simon 0:350011bf8be7 1291 /* Move the first unacked segment to the unsent queue */
simon 0:350011bf8be7 1292 /* Keep the unsent queue sorted. */
simon 0:350011bf8be7 1293 seg = pcb->unacked;
simon 0:350011bf8be7 1294 pcb->unacked = seg->next;
simon 0:350011bf8be7 1295
simon 0:350011bf8be7 1296 cur_seg = &(pcb->unsent);
simon 0:350011bf8be7 1297 while (*cur_seg &&
simon 0:350011bf8be7 1298 TCP_SEQ_LT(ntohl((*cur_seg)->tcphdr->seqno), ntohl(seg->tcphdr->seqno))) {
simon 0:350011bf8be7 1299 cur_seg = &((*cur_seg)->next );
simon 0:350011bf8be7 1300 }
simon 0:350011bf8be7 1301 seg->next = *cur_seg;
simon 0:350011bf8be7 1302 *cur_seg = seg;
simon 0:350011bf8be7 1303
simon 0:350011bf8be7 1304 ++pcb->nrtx;
simon 0:350011bf8be7 1305
simon 0:350011bf8be7 1306 /* Don't take any rtt measurements after retransmitting. */
simon 0:350011bf8be7 1307 pcb->rttest = 0;
simon 0:350011bf8be7 1308
simon 0:350011bf8be7 1309 /* Do the actual retransmission. */
simon 0:350011bf8be7 1310 snmp_inc_tcpretranssegs();
simon 0:350011bf8be7 1311 /* No need to call tcp_output: we are always called from tcp_input()
simon 0:350011bf8be7 1312 and thus tcp_output directly returns. */
simon 0:350011bf8be7 1313 }
simon 0:350011bf8be7 1314
simon 0:350011bf8be7 1315
simon 0:350011bf8be7 1316 /**
simon 0:350011bf8be7 1317 * Handle retransmission after three dupacks received
simon 0:350011bf8be7 1318 *
simon 0:350011bf8be7 1319 * @param pcb the tcp_pcb for which to retransmit the first unacked segment
simon 0:350011bf8be7 1320 */
simon 0:350011bf8be7 1321 void
simon 0:350011bf8be7 1322 tcp_rexmit_fast(struct tcp_pcb *pcb)
simon 0:350011bf8be7 1323 {
simon 0:350011bf8be7 1324 if (pcb->unacked != NULL && !(pcb->flags & TF_INFR)) {
simon 0:350011bf8be7 1325 /* This is fast retransmit. Retransmit the first unacked segment. */
simon 0:350011bf8be7 1326 LWIP_DEBUGF(TCP_FR_DEBUG,
simon 0:350011bf8be7 1327 ("tcp_receive: dupacks %"U16_F" (%"U32_F
simon 0:350011bf8be7 1328 "), fast retransmit %"U32_F"\n",
simon 0:350011bf8be7 1329 (u16_t)pcb->dupacks, pcb->lastack,
simon 0:350011bf8be7 1330 ntohl(pcb->unacked->tcphdr->seqno)));
simon 0:350011bf8be7 1331 tcp_rexmit(pcb);
simon 0:350011bf8be7 1332
simon 0:350011bf8be7 1333 /* Set ssthresh to half of the minimum of the current
simon 0:350011bf8be7 1334 * cwnd and the advertised window */
simon 0:350011bf8be7 1335 if (pcb->cwnd > pcb->snd_wnd) {
simon 0:350011bf8be7 1336 pcb->ssthresh = pcb->snd_wnd / 2;
simon 0:350011bf8be7 1337 } else {
simon 0:350011bf8be7 1338 pcb->ssthresh = pcb->cwnd / 2;
simon 0:350011bf8be7 1339 }
simon 0:350011bf8be7 1340
simon 0:350011bf8be7 1341 /* The minimum value for ssthresh should be 2 MSS */
simon 0:350011bf8be7 1342 if (pcb->ssthresh < 2*pcb->mss) {
simon 0:350011bf8be7 1343 LWIP_DEBUGF(TCP_FR_DEBUG,
simon 0:350011bf8be7 1344 ("tcp_receive: The minimum value for ssthresh %"U16_F
simon 0:350011bf8be7 1345 " should be min 2 mss %"U16_F"...\n",
simon 0:350011bf8be7 1346 pcb->ssthresh, 2*pcb->mss));
simon 0:350011bf8be7 1347 pcb->ssthresh = 2*pcb->mss;
simon 0:350011bf8be7 1348 }
simon 0:350011bf8be7 1349
simon 0:350011bf8be7 1350 pcb->cwnd = pcb->ssthresh + 3 * pcb->mss;
simon 0:350011bf8be7 1351 pcb->flags |= TF_INFR;
simon 0:350011bf8be7 1352 }
simon 0:350011bf8be7 1353 }
simon 0:350011bf8be7 1354
simon 0:350011bf8be7 1355
simon 0:350011bf8be7 1356 /**
simon 0:350011bf8be7 1357 * Send keepalive packets to keep a connection active although
simon 0:350011bf8be7 1358 * no data is sent over it.
simon 0:350011bf8be7 1359 *
simon 0:350011bf8be7 1360 * Called by tcp_slowtmr()
simon 0:350011bf8be7 1361 *
simon 0:350011bf8be7 1362 * @param pcb the tcp_pcb for which to send a keepalive packet
simon 0:350011bf8be7 1363 */
simon 0:350011bf8be7 1364 void
simon 0:350011bf8be7 1365 tcp_keepalive(struct tcp_pcb *pcb)
simon 0:350011bf8be7 1366 {
simon 0:350011bf8be7 1367 struct pbuf *p;
simon 0:350011bf8be7 1368 struct tcp_hdr *tcphdr;
simon 0:350011bf8be7 1369
simon 0:350011bf8be7 1370 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: sending KEEPALIVE probe to %"U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
simon 0:350011bf8be7 1371 ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip),
simon 0:350011bf8be7 1372 ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip)));
simon 0:350011bf8be7 1373
simon 0:350011bf8be7 1374 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: tcp_ticks %"U32_F" pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",
simon 0:350011bf8be7 1375 tcp_ticks, pcb->tmr, pcb->keep_cnt_sent));
simon 0:350011bf8be7 1376
simon 0:350011bf8be7 1377 p = tcp_output_alloc_header(pcb, 0, 0, htonl(pcb->snd_nxt - 1));
simon 0:350011bf8be7 1378 if(p == NULL) {
simon 0:350011bf8be7 1379 LWIP_DEBUGF(TCP_DEBUG,
simon 0:350011bf8be7 1380 ("tcp_keepalive: could not allocate memory for pbuf\n"));
simon 0:350011bf8be7 1381 return;
simon 0:350011bf8be7 1382 }
simon 0:350011bf8be7 1383 tcphdr = (struct tcp_hdr *)p->payload;
simon 0:350011bf8be7 1384
simon 0:350011bf8be7 1385 #if CHECKSUM_GEN_TCP
simon 0:350011bf8be7 1386 tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip,
simon 0:350011bf8be7 1387 IP_PROTO_TCP, p->tot_len);
simon 0:350011bf8be7 1388 #endif
simon 0:350011bf8be7 1389 TCP_STATS_INC(tcp.xmit);
simon 0:350011bf8be7 1390
simon 0:350011bf8be7 1391 /* Send output to IP */
simon 0:350011bf8be7 1392 #if LWIP_NETIF_HWADDRHINT
simon 0:350011bf8be7 1393 ip_output_hinted(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP,
simon 0:350011bf8be7 1394 &(pcb->addr_hint));
simon 0:350011bf8be7 1395 #else /* LWIP_NETIF_HWADDRHINT*/
simon 0:350011bf8be7 1396 ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);
simon 0:350011bf8be7 1397 #endif /* LWIP_NETIF_HWADDRHINT*/
simon 0:350011bf8be7 1398
simon 0:350011bf8be7 1399 pbuf_free(p);
simon 0:350011bf8be7 1400
simon 0:350011bf8be7 1401 LWIP_DEBUGF(TCP_DEBUG, ("tcp_keepalive: seqno %"U32_F" ackno %"U32_F".\n",
simon 0:350011bf8be7 1402 pcb->snd_nxt - 1, pcb->rcv_nxt));
simon 0:350011bf8be7 1403 }
simon 0:350011bf8be7 1404
simon 0:350011bf8be7 1405
simon 0:350011bf8be7 1406 /**
simon 0:350011bf8be7 1407 * Send persist timer zero-window probes to keep a connection active
simon 0:350011bf8be7 1408 * when a window update is lost.
simon 0:350011bf8be7 1409 *
simon 0:350011bf8be7 1410 * Called by tcp_slowtmr()
simon 0:350011bf8be7 1411 *
simon 0:350011bf8be7 1412 * @param pcb the tcp_pcb for which to send a zero-window probe packet
simon 0:350011bf8be7 1413 */
simon 0:350011bf8be7 1414 void
simon 0:350011bf8be7 1415 tcp_zero_window_probe(struct tcp_pcb *pcb)
simon 0:350011bf8be7 1416 {
simon 0:350011bf8be7 1417 struct pbuf *p;
simon 0:350011bf8be7 1418 struct tcp_hdr *tcphdr;
simon 0:350011bf8be7 1419 struct tcp_seg *seg;
simon 0:350011bf8be7 1420 u16_t len;
simon 0:350011bf8be7 1421 u8_t is_fin;
simon 0:350011bf8be7 1422
simon 0:350011bf8be7 1423 LWIP_DEBUGF(TCP_DEBUG,
simon 0:350011bf8be7 1424 ("tcp_zero_window_probe: sending ZERO WINDOW probe to %"
simon 0:350011bf8be7 1425 U16_F".%"U16_F".%"U16_F".%"U16_F"\n",
simon 0:350011bf8be7 1426 ip4_addr1_16(&pcb->remote_ip), ip4_addr2_16(&pcb->remote_ip),
simon 0:350011bf8be7 1427 ip4_addr3_16(&pcb->remote_ip), ip4_addr4_16(&pcb->remote_ip)));
simon 0:350011bf8be7 1428
simon 0:350011bf8be7 1429 LWIP_DEBUGF(TCP_DEBUG,
simon 0:350011bf8be7 1430 ("tcp_zero_window_probe: tcp_ticks %"U32_F
simon 0:350011bf8be7 1431 " pcb->tmr %"U32_F" pcb->keep_cnt_sent %"U16_F"\n",
simon 0:350011bf8be7 1432 tcp_ticks, pcb->tmr, pcb->keep_cnt_sent));
simon 0:350011bf8be7 1433
simon 0:350011bf8be7 1434 seg = pcb->unacked;
simon 0:350011bf8be7 1435
simon 0:350011bf8be7 1436 if(seg == NULL) {
simon 0:350011bf8be7 1437 seg = pcb->unsent;
simon 0:350011bf8be7 1438 }
simon 0:350011bf8be7 1439 if(seg == NULL) {
simon 0:350011bf8be7 1440 return;
simon 0:350011bf8be7 1441 }
simon 0:350011bf8be7 1442
simon 0:350011bf8be7 1443 is_fin = ((TCPH_FLAGS(seg->tcphdr) & TCP_FIN) != 0) && (seg->len == 0);
simon 0:350011bf8be7 1444 /* we want to send one seqno: either FIN or data (no options) */
simon 0:350011bf8be7 1445 len = is_fin ? 0 : 1;
simon 0:350011bf8be7 1446
simon 0:350011bf8be7 1447 p = tcp_output_alloc_header(pcb, 0, len, seg->tcphdr->seqno);
simon 0:350011bf8be7 1448 if(p == NULL) {
simon 0:350011bf8be7 1449 LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: no memory for pbuf\n"));
simon 0:350011bf8be7 1450 return;
simon 0:350011bf8be7 1451 }
simon 0:350011bf8be7 1452 tcphdr = (struct tcp_hdr *)p->payload;
simon 0:350011bf8be7 1453
simon 0:350011bf8be7 1454 if (is_fin) {
simon 0:350011bf8be7 1455 /* FIN segment, no data */
simon 0:350011bf8be7 1456 TCPH_FLAGS_SET(tcphdr, TCP_ACK | TCP_FIN);
simon 0:350011bf8be7 1457 } else {
simon 0:350011bf8be7 1458 /* Data segment, copy in one byte from the head of the unacked queue */
simon 0:350011bf8be7 1459 *((char *)p->payload + TCP_HLEN) = *(char *)seg->dataptr;
simon 0:350011bf8be7 1460 }
simon 0:350011bf8be7 1461
simon 0:350011bf8be7 1462 #if CHECKSUM_GEN_TCP
simon 0:350011bf8be7 1463 tcphdr->chksum = inet_chksum_pseudo(p, &pcb->local_ip, &pcb->remote_ip,
simon 0:350011bf8be7 1464 IP_PROTO_TCP, p->tot_len);
simon 0:350011bf8be7 1465 #endif
simon 0:350011bf8be7 1466 TCP_STATS_INC(tcp.xmit);
simon 0:350011bf8be7 1467
simon 0:350011bf8be7 1468 /* Send output to IP */
simon 0:350011bf8be7 1469 #if LWIP_NETIF_HWADDRHINT
simon 0:350011bf8be7 1470 ip_output_hinted(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP,
simon 0:350011bf8be7 1471 &(pcb->addr_hint));
simon 0:350011bf8be7 1472 #else /* LWIP_NETIF_HWADDRHINT*/
simon 0:350011bf8be7 1473 ip_output(p, &pcb->local_ip, &pcb->remote_ip, pcb->ttl, 0, IP_PROTO_TCP);
simon 0:350011bf8be7 1474 #endif /* LWIP_NETIF_HWADDRHINT*/
simon 0:350011bf8be7 1475
simon 0:350011bf8be7 1476 pbuf_free(p);
simon 0:350011bf8be7 1477
simon 0:350011bf8be7 1478 LWIP_DEBUGF(TCP_DEBUG, ("tcp_zero_window_probe: seqno %"U32_F
simon 0:350011bf8be7 1479 " ackno %"U32_F".\n",
simon 0:350011bf8be7 1480 pcb->snd_nxt - 1, pcb->rcv_nxt));
simon 0:350011bf8be7 1481 }
simon 0:350011bf8be7 1482 #endif /* LWIP_TCP */