Free (GPLv2) TCP/IP stack developed by TASS Belgium

Dependents:   lpc1768-picotcp-demo ZeroMQ_PicoTCP_Publisher_demo TCPSocket_HelloWorld_PicoTCP Pico_TCP_UDP_Test ... more

PicoTCP. Copyright (c) 2013 TASS Belgium NV.

Released under the GNU General Public License, version 2.

Different licensing models may exist, at the sole discretion of the Copyright holders.

Official homepage: http://www.picotcp.com

Bug tracker: https://github.com/tass-belgium/picotcp/issues

Development steps:

  • initial integration with mbed RTOS
  • generic mbed Ethernet driver
  • high performance NXP LPC1768 specific Ethernet driver
  • Multi-threading support for mbed RTOS
  • Berkeley sockets and integration with the New Socket API
  • Fork of the apps running on top of the New Socket API
  • Scheduling optimizations
  • Debugging/benchmarking/testing

Demo application (measuring TCP sender performance):

Import programlpc1768-picotcp-demo

A PicoTCP demo app testing the ethernet throughput on the lpc1768 mbed board.

Revision:
45:ca30069e49bb
Parent:
40:c8ab0d2bba0b
Child:
48:40fc4462265c
--- a/modules/pico_tcp.c	Wed Jul 17 11:55:09 2013 +0000
+++ b/modules/pico_tcp.c	Tue Jul 23 13:22:24 2013 +0000
@@ -43,6 +43,10 @@
 #define IS_TCP_HOLDQ_EMPTY(t)   (t->tcpq_hold.size == 0)
 
 #ifdef PICO_SUPPORT_TCP
+#define tcp_dbg_nagle(...) do{}while(0)
+#define tcp_dbg_options(...) do{}while(0)
+
+
 #define tcp_dbg(...) do{}while(0)
 //#define tcp_dbg dbg
 
@@ -521,6 +525,7 @@
   struct pico_socket_tcp *t = (struct pico_socket_tcp *)f->sock;
   uint8_t *opt = f->transport_hdr + PICO_SIZE_TCPHDR;
   int i = 0;
+  f->timestamp = 0;
   while (i < (f->transport_len - PICO_SIZE_TCPHDR)) {
     uint8_t type =  opt[i++];
     uint8_t len;
@@ -530,23 +535,23 @@
       len = 1;
     if (f->payload && ((opt + i) > f->payload))
       break;
-    tcp_dbg("Received option '%d', len = %d \n", type, len);
+    tcp_dbg_options("Received option '%d', len = %d \n", type, len);
     switch (type) {
       case PICO_TCP_OPTION_NOOP:
       case PICO_TCP_OPTION_END:
         break;
       case PICO_TCP_OPTION_WS:
         if (len != PICO_TCPOPTLEN_WS) {
-          tcp_dbg("TCP Window scale: bad len received (%d).\n", len);
+          tcp_dbg_options("TCP Window scale: bad len received (%d).\n", len);
           i += len - 2;
           break;
         }
         t->recv_wnd_scale = opt[i++];
-        tcp_dbg("TCP Window scale: received %d\n", t->recv_wnd_scale);
+        tcp_dbg_options("TCP Window scale: received %d\n", t->recv_wnd_scale);
         break;
       case PICO_TCP_OPTION_SACK_OK:
         if (len != PICO_TCPOPTLEN_SACK_OK) {
-          tcp_dbg("TCP option sack: bad len received.\n");
+          tcp_dbg_options("TCP option sack: bad len received.\n");
           i += len - 2;
           break;
         }
@@ -555,7 +560,7 @@
       case PICO_TCP_OPTION_MSS: {
         uint16_t mss;
         if (len != PICO_TCPOPTLEN_MSS) {
-          tcp_dbg("TCP option mss: bad len received.\n");
+          tcp_dbg_options("TCP option mss: bad len received.\n");
           i += len - 2;
           break;
         }
@@ -569,7 +574,7 @@
       case PICO_TCP_OPTION_TIMESTAMP: {
         uint32_t tsval, tsecr;
         if (len != PICO_TCPOPTLEN_TIMESTAMP) {
-          tcp_dbg("TCP option timestamp: bad len received.\n");
+          tcp_dbg_options("TCP option timestamp: bad len received.\n");
           i += len - 2;
           break;
         }
@@ -579,7 +584,6 @@
         tsecr = long_from(opt + i);
         f->timestamp = long_be(tsecr);
         i += sizeof(uint32_t);
-
         t->ts_nxt = long_be(tsval);
         break;
       }
@@ -590,7 +594,7 @@
         break;
       }
       default:
-        tcp_dbg("TCP: received unsupported option %u\n", type);
+        tcp_dbg_options("TCP: received unsupported option %u\n", type);
         i += len - 2;
     }
   }
@@ -1115,16 +1119,16 @@
     tcp_parse_options(f);
     f->payload = f->transport_hdr + ((hdr->len & 0xf0) >>2);
     f->payload_len = f->transport_len - ((hdr->len & 0xf0) >>2);
+    tcp_dbg("TCP> Received segment. (exp: %x got: %x)\n", t->rcv_nxt, SEQN(f));
 
-    if (seq_compare(SEQN(f), t->rcv_nxt) >= 0) {
-      struct pico_frame *cpy = pico_frame_copy(f);
+    if (seq_compare(SEQN(f), t->rcv_nxt) <= 0) {
       struct pico_frame *nxt;
-      /* Enqueue: try to put into RCV buffer */
-      if (pico_enqueue_segment(&t->tcpq_in, cpy) <= 0) {
-        pico_frame_discard(cpy);
-        return -1;
-      }
       if (seq_compare(SEQN(f), t->rcv_nxt) == 0) { /* Exactly what we expected */
+        struct pico_frame *cpy = pico_frame_copy(f);
+        /* Enqueue: try to put into RCV buffer */
+        if(pico_enqueue_segment(&t->tcpq_in, cpy) <= 0) {
+          pico_frame_discard(cpy);
+        }
         t->rcv_nxt = SEQN(f) + f->payload_len;
         nxt = peek_segment(&t->tcpq_in, t->rcv_nxt);
         while(nxt) {
@@ -1135,7 +1139,7 @@
         t->sock.ev_pending |= PICO_SOCK_EV_RD;
         t->rcv_nxt = SEQN(f) + f->payload_len;
       } else {
-        tcp_dbg("TCP> lo segment. Possible retransmission. (exp: %x got: %x)\n", t->rcv_nxt, SEQN(f));
+        tcp_dbg("TCP> lo segment. Uninteresting retransmission. (exp: %x got: %x)\n", t->rcv_nxt, SEQN(f));
       }
     } else {
       tcp_dbg("TCP> hi segment. Possible packet loss. I'll dupack this. (exp: %x got: %x)\n", t->rcv_nxt, SEQN(f));
@@ -1210,7 +1214,7 @@
     /* Finally, assign a new value for the RTO, as specified in the RFC, with K=4 */
     t->rto = t->avg_rtt + (t->rttvar << 2);
   }
-  tcp_dbg(" -----=============== RTT AVG: %u RTTVAR: %u RTO: %u ======================----\n", t->avg_rtt, t->rttvar, t->rto);
+  tcp_dbg(" -----=============== RTT CUR: %u AVG: %u RTTVAR: %u RTO: %u ======================----\n", rtt, t->avg_rtt, t->rttvar, t->rto);
 }
 
 static void tcp_congestion_control(struct pico_socket_tcp *t)
@@ -1219,6 +1223,8 @@
     return;
   if (t->cwnd > t->tcpq_out.frames) {
     tcp_dbg("Limited by app: %d\n", t->cwnd);
+    if (t->sock.wakeup)
+      t->sock.wakeup(PICO_SOCK_EV_WR, &t->sock);
     return;
   }
   tcp_dbg("Doing congestion control\n");
@@ -1332,7 +1338,7 @@
     tcp_add_options(t, f, 0, f->transport_len - f->payload_len - PICO_SIZE_TCPHDR);
     hdr->rwnd = short_be(t->wnd);
     hdr->flags |= PICO_TCP_PSH;
-    //hdr->ack = long_be(t->rcv_nxt);  /* XXX TODO check: setting ack field with no ACK flag ?? */
+    hdr->ack = long_be(t->rcv_nxt);
     hdr->crc = 0;
     hdr->crc = short_be(pico_tcp_checksum_ipv4(f));
     /* TCP: ENQUEUE to PROTO ( retransmit )*/
@@ -1435,10 +1441,16 @@
     t->backoff = 0;
 
     /* Do rtt/rttvar/rto calculations */
-    if(una && (una->timestamp != 0)) {
-      rtt = time_diff(pico_tick, una->timestamp);
+    /* First, try with timestamps, using the value from options */
+    if(f && (f->timestamp != 0)) {
+      rtt = time_diff(pico_tick, f->timestamp);
       if (rtt)
         tcp_rtt(t, rtt);
+    } else if(una && (una->timestamp != 0)) {
+      /* If no timestamps are there, use conservatve estimation on the una */
+        rtt = time_diff(pico_tick, una->timestamp);
+        if (rtt)
+          tcp_rtt(t, rtt);
     }
 
     tcp_dbg("TCP ACK> FRESH ACK %08x (acked %d) Queue size: %u/%u frames: %u cwnd: %u in_flight: %u snd_una: %u\n", ACKN(f), acked, t->tcpq_out.size, t->tcpq_out.max_size, t->tcpq_out.frames, t->cwnd, t->in_flight, SEQN(una));
@@ -1515,13 +1527,13 @@
   /* if Nagle enabled, check if no unack'ed data and fill out queue (till window) */
   if (IS_NAGLE_ENABLED((&(t->sock)))) {
     while (!IS_TCP_HOLDQ_EMPTY(t) && ((t->tcpq_out.max_size - t->tcpq_out.size) >= PICO_TCP_DEFAULT_MSS)) {
-      tcp_dbg("TCP_ACK - NAGLE add new segment\n");
+      tcp_dbg_nagle("TCP_ACK - NAGLE add new segment\n");
       f_new = pico_hold_segment_make(t);
       if (f_new == NULL)
         break;            /* XXX corrupt !!! (or no memory) */
       if (pico_enqueue_segment(&t->tcpq_out,f_new) <= 0)
         // handle error
-        tcp_dbg("TCP_ACK - NAGLE FAILED to enqueue in out\n");
+        tcp_dbg_nagle("TCP_ACK - NAGLE FAILED to enqueue in out\n");
     }
   }
 
@@ -1643,8 +1655,8 @@
 #endif
 #ifdef PICO_SUPPORT_IPV6
   if (IS_IPV6(f)) {
-    memcpy(new->sock.remote_addr.ip6.addr, ((struct pico_ipv6_hdr *)(f->net_hdr))->src, PICO_SIZE_IP6);
-    memcpy(new->sock.remote_addr.ip6.addr, ((struct pico_ipv6_hdr *)(f->net_hdr))->src, PICO_SIZE_IP6);
+    new->sock.remote_addr.ip6 = ((struct pico_ipv6_hdr *)(f->net_hdr))->src;
+    new->sock.local_addr.ip6 = ((struct pico_ipv6_hdr *)(f->net_hdr))->dst;
   }
 #endif
 
@@ -1899,9 +1911,9 @@
   { PICO_SOCKET_STATE_TCP_CLOSED,       NULL,            NULL,              NULL,              NULL,            NULL,            NULL,            NULL     },
   { PICO_SOCKET_STATE_TCP_LISTEN,       &tcp_syn,        &tcp_nosync_rst,   &tcp_nosync_rst,   &tcp_nosync_rst, &tcp_nosync_rst, &tcp_nosync_rst, NULL     },
   { PICO_SOCKET_STATE_TCP_SYN_SENT,     &tcp_nosync_rst, &tcp_synack,       &tcp_nosync_rst,   &tcp_nosync_rst, &tcp_nosync_rst, &tcp_nosync_rst, &tcp_rst },
-  { PICO_SOCKET_STATE_TCP_SYN_RECV,     &tcp_nosync_rst, &tcp_nosync_rst,   &tcp_first_ack,    &tcp_nosync_rst, &tcp_nosync_rst, &tcp_nosync_rst, &tcp_rst },
+  { PICO_SOCKET_STATE_TCP_SYN_RECV,     NULL,            &tcp_nosync_rst,   &tcp_first_ack,    &tcp_nosync_rst, &tcp_nosync_rst, &tcp_nosync_rst, &tcp_rst },
   { PICO_SOCKET_STATE_TCP_ESTABLISHED,  NULL,            &tcp_ack,          &tcp_ack,          &tcp_data_in,    &tcp_closewait,  &tcp_closewait,  &tcp_rst },
-  { PICO_SOCKET_STATE_TCP_CLOSE_WAIT,   NULL,            &tcp_ack,          &tcp_ack,          &tcp_send_rst,   &tcp_closewait,  &tcp_closewait,   &tcp_rst },
+  { PICO_SOCKET_STATE_TCP_CLOSE_WAIT,   NULL,            &tcp_ack,          &tcp_ack,          &tcp_send_rst,   &tcp_closewait,  &tcp_closewait,  &tcp_rst },
   { PICO_SOCKET_STATE_TCP_LAST_ACK,     NULL,            &tcp_ack,          &tcp_lastackwait,  &tcp_send_rst,   &tcp_send_rst,   &tcp_send_rst,   &tcp_rst },
   { PICO_SOCKET_STATE_TCP_FIN_WAIT1,    NULL,            &tcp_ack,          &tcp_finwaitack,   &tcp_data_in,    &tcp_rcvfin,     &tcp_finack,     &tcp_rst },
   { PICO_SOCKET_STATE_TCP_FIN_WAIT2,    NULL,            &tcp_ack,          &tcp_ack,          &tcp_data_in,    &tcp_finwaitfin, &tcp_finack,     &tcp_rst },
@@ -2008,7 +2020,7 @@
       if (t->x_mode != PICO_TCP_WINDOW_FULL) {
         tcp_dbg("TCP> RIGHT SIZING (rwnd: %d, frame len: %d\n",t->recv_wnd << t->recv_wnd_scale, f->payload_len);
         tcp_dbg("In window full...\n");
-        //t->snd_nxt = SEQN(una);   /* XXX prevent out-of-order-packets ! */
+        t->snd_nxt = SEQN(una);   /* XXX prevent out-of-order-packets ! */ /*DLA re-enabled.*/
         t->snd_retry = SEQN(una);   /* XXX replace by retry pointer? */
 
         /* Alternative to the line above:  (better performance, but seems to lock anyway with larger buffers)
@@ -2040,9 +2052,11 @@
   if (sent > 0) {
     if (t->rto < PICO_TCP_RTO_MIN)
       t->rto = PICO_TCP_RTO_MIN;
+    //if (s->wakeup)
+    //  t->sock.wakeup(PICO_SOCK_EV_WR, &t->sock);
     add_retransmission_timer(t, pico_tick + t->rto);
   } else {
-    // no packets in queue ??
+    /* Nothing to transmit. */
   }
 
   if ((t->tcpq_out.frames == 0) && (s->state & PICO_SOCKET_STATE_SHUT_LOCAL)) {    /* if no more packets in queue, XXX replacled !f by tcpq check */
@@ -2118,7 +2132,7 @@
 
   hdr->len = (f_new->payload - f_new->transport_hdr) << 2 | t->jumbo;
 
-  tcp_dbg("NAGLE make - joined %d segments, len %d bytes\n",test,total_payload_len);
+  tcp_dbg_nagle("NAGLE make - joined %d segments, len %d bytes\n",test,total_payload_len);
 
   return f_new;
 }
@@ -2145,7 +2159,7 @@
   if (!IS_NAGLE_ENABLED((&(t->sock)))) {
     /* TCP_NODELAY enabled, original behavior */
     if (pico_enqueue_segment(&t->tcpq_out,f) > 0) {
-      tcp_dbg("TCP_PUSH - NO NAGLE - Pushing segment %08x, len %08x to socket %p\n", t->snd_last + 1, f->payload_len, t);
+      tcp_dbg_nagle("TCP_PUSH - NO NAGLE - Pushing segment %08x, len %08x to socket %p\n", t->snd_last + 1, f->payload_len, t);
       t->snd_last += f->payload_len;
       return f->payload_len;
     } else {
@@ -2158,7 +2172,7 @@
     /* Nagle's algorithm enabled, check if ready to send, or put frame in hold queue */
     if (IS_TCP_IDLE(t) && IS_TCP_HOLDQ_EMPTY(t)) {  /* opt 1. send frame */
       if (pico_enqueue_segment(&t->tcpq_out,f) > 0) {
-        tcp_dbg("TCP_PUSH - NAGLE - Pushing segment %08x, len %08x to socket %p\n", t->snd_last + 1, f->payload_len, t);
+        tcp_dbg_nagle("TCP_PUSH - NAGLE - Pushing segment %08x, len %08x to socket %p\n", t->snd_last + 1, f->payload_len, t);
         t->snd_last += f->payload_len;
         return f->payload_len;
       } else {
@@ -2171,28 +2185,28 @@
         /* IF enough data in hold (>mss) AND space in out queue (>mss) */
         /* add current frame in hold and make new segment */
         if (pico_enqueue_segment(&t->tcpq_hold,f) > 0 ) {
-          tcp_dbg("TCP_PUSH - NAGLE - Pushed into hold, make new (enqueued frames out %d)\n",t->tcpq_out.frames);
+          tcp_dbg_nagle("TCP_PUSH - NAGLE - Pushed into hold, make new (enqueued frames out %d)\n",t->tcpq_out.frames);
           t->snd_last += f->payload_len;    /* XXX  WATCH OUT */
           f_new = pico_hold_segment_make(t);
         } else {
-          tcp_dbg("TCP_PUSH - NAGLE - enqueue hold failed 1\n");
+          tcp_dbg_nagle("TCP_PUSH - NAGLE - enqueue hold failed 1\n");
           return 0;
         }
         /* and put new frame in out queue */
         if ((f_new != NULL) && (pico_enqueue_segment(&t->tcpq_out,f_new) > 0)) {
           return f_new->payload_len;
         } else {
-          tcp_dbg("TCP_PUSH - NAGLE - enqueue out failed, f_new = %p\n",f_new);
+          tcp_dbg_nagle("TCP_PUSH - NAGLE - enqueue out failed, f_new = %p\n",f_new);
           return -1;                        /* XXX something seriously wrong */
         }
       } else {
         /* ELSE put frame in hold queue */
         if (pico_enqueue_segment(&t->tcpq_hold,f) > 0) {
-          tcp_dbg("TCP_PUSH - NAGLE - Pushed into hold (enqueued frames out %d)\n",t->tcpq_out.frames);
+          tcp_dbg_nagle("TCP_PUSH - NAGLE - Pushed into hold (enqueued frames out %d)\n",t->tcpq_out.frames);
           t->snd_last += f->payload_len;    /* XXX  WATCH OUT */
           return f->payload_len;
         } else {
-          tcp_dbg("TCP_PUSH - NAGLE - enqueue hold failed 2\n");
+          tcp_dbg_nagle("TCP_PUSH - NAGLE - enqueue hold failed 2\n");
           return 0;
         }
       }