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.

Committer:
tass
Date:
Mon Sep 02 08:02:21 2013 +0000
Revision:
51:ab4529a384a6
Parent:
46:c35c84e301e0
Child:
63:97f481e33cb2
Updated from masterbranch

Who changed what in which revision?

UserRevisionLine numberNew contents of line
daniele 29:1a47b7151851 1 /*********************************************************************
daniele 29:1a47b7151851 2 PicoTCP. Copyright (c) 2012 TASS Belgium NV. Some rights reserved.
daniele 29:1a47b7151851 3 See LICENSE and COPYING for usage.
daniele 29:1a47b7151851 4
daniele 29:1a47b7151851 5 .
daniele 29:1a47b7151851 6
daniele 29:1a47b7151851 7 Authors: Daniele Lacamera
daniele 29:1a47b7151851 8 *********************************************************************/
daniele 29:1a47b7151851 9
daniele 29:1a47b7151851 10
daniele 29:1a47b7151851 11 #include "pico_icmp4.h"
daniele 29:1a47b7151851 12 #include "pico_config.h"
daniele 29:1a47b7151851 13 #include "pico_ipv4.h"
daniele 29:1a47b7151851 14 #include "pico_eth.h"
daniele 29:1a47b7151851 15 #include "pico_device.h"
daniele 29:1a47b7151851 16 #include "pico_stack.h"
daniele 29:1a47b7151851 17 #include "pico_tree.h"
daniele 29:1a47b7151851 18
daniele 29:1a47b7151851 19 /* Queues */
tass 51:ab4529a384a6 20 static struct pico_queue icmp_in = {0};
tass 51:ab4529a384a6 21 static struct pico_queue icmp_out = {0};
daniele 29:1a47b7151851 22
daniele 29:1a47b7151851 23
daniele 29:1a47b7151851 24 /* Functions */
daniele 29:1a47b7151851 25
daniele 29:1a47b7151851 26 static int pico_icmp4_checksum(struct pico_frame *f)
daniele 29:1a47b7151851 27 {
daniele 29:1a47b7151851 28 struct pico_icmp4_hdr *hdr = (struct pico_icmp4_hdr *) f->transport_hdr;
daniele 29:1a47b7151851 29 if (!hdr) {
daniele 29:1a47b7151851 30 pico_err = PICO_ERR_EINVAL;
daniele 29:1a47b7151851 31 return -1;
daniele 29:1a47b7151851 32 }
daniele 29:1a47b7151851 33 hdr->crc = 0;
daniele 29:1a47b7151851 34 hdr->crc = short_be(pico_checksum(hdr, f->transport_len));
daniele 29:1a47b7151851 35 return 0;
daniele 29:1a47b7151851 36 }
daniele 29:1a47b7151851 37
daniele 29:1a47b7151851 38 #ifdef PICO_SUPPORT_PING
daniele 29:1a47b7151851 39 static void ping_recv_reply(struct pico_frame *f);
daniele 29:1a47b7151851 40 #endif
daniele 29:1a47b7151851 41
daniele 29:1a47b7151851 42 static int pico_icmp4_process_in(struct pico_protocol *self, struct pico_frame *f)
daniele 29:1a47b7151851 43 {
tass 51:ab4529a384a6 44 struct pico_icmp4_hdr *hdr = (struct pico_icmp4_hdr *) f->transport_hdr;
tass 51:ab4529a384a6 45 IGNORE_PARAMETER(self);
tass 51:ab4529a384a6 46
daniele 29:1a47b7151851 47 if (hdr->type == PICO_ICMP_ECHO) {
daniele 29:1a47b7151851 48 hdr->type = PICO_ICMP_ECHOREPLY;
daniele 46:c35c84e301e0 49 /* outgoing frames require a f->len without the ethernet header len */
daniele 29:1a47b7151851 50 if (f->dev->eth)
daniele 29:1a47b7151851 51 f->len -= PICO_SIZE_ETHHDR;
daniele 46:c35c84e301e0 52 pico_icmp4_checksum(f);
daniele 29:1a47b7151851 53 pico_ipv4_rebound(f);
daniele 29:1a47b7151851 54 } else if (hdr->type == PICO_ICMP_UNREACH) {
daniele 29:1a47b7151851 55 f->net_hdr = f->transport_hdr + PICO_ICMPHDR_UN_SIZE;
daniele 29:1a47b7151851 56 pico_ipv4_unreachable(f, hdr->code);
daniele 29:1a47b7151851 57 } else if (hdr->type == PICO_ICMP_ECHOREPLY) {
daniele 29:1a47b7151851 58 #ifdef PICO_SUPPORT_PING
daniele 29:1a47b7151851 59 ping_recv_reply(f);
daniele 29:1a47b7151851 60 #endif
daniele 29:1a47b7151851 61 pico_frame_discard(f);
daniele 29:1a47b7151851 62 } else {
daniele 29:1a47b7151851 63 pico_frame_discard(f);
daniele 29:1a47b7151851 64 }
daniele 29:1a47b7151851 65 return 0;
daniele 29:1a47b7151851 66 }
daniele 29:1a47b7151851 67
daniele 29:1a47b7151851 68 static int pico_icmp4_process_out(struct pico_protocol *self, struct pico_frame *f)
daniele 29:1a47b7151851 69 {
tass 51:ab4529a384a6 70 IGNORE_PARAMETER(self);
tass 51:ab4529a384a6 71 IGNORE_PARAMETER(f);
tass 51:ab4529a384a6 72 dbg("Called %s\n", __FUNCTION__);
daniele 29:1a47b7151851 73 return 0;
daniele 29:1a47b7151851 74 }
daniele 29:1a47b7151851 75
daniele 29:1a47b7151851 76 /* Interface: protocol definition */
daniele 29:1a47b7151851 77 struct pico_protocol pico_proto_icmp4 = {
daniele 29:1a47b7151851 78 .name = "icmp4",
daniele 29:1a47b7151851 79 .proto_number = PICO_PROTO_ICMP4,
daniele 29:1a47b7151851 80 .layer = PICO_LAYER_TRANSPORT,
daniele 29:1a47b7151851 81 .process_in = pico_icmp4_process_in,
daniele 29:1a47b7151851 82 .process_out = pico_icmp4_process_out,
daniele 29:1a47b7151851 83 .q_in = &icmp_in,
daniele 29:1a47b7151851 84 .q_out = &icmp_out,
daniele 29:1a47b7151851 85 };
daniele 29:1a47b7151851 86
daniele 29:1a47b7151851 87 static int pico_icmp4_notify(struct pico_frame *f, uint8_t type, uint8_t code)
daniele 29:1a47b7151851 88 {
daniele 29:1a47b7151851 89 struct pico_frame *reply;
daniele 29:1a47b7151851 90 struct pico_icmp4_hdr *hdr;
daniele 29:1a47b7151851 91 struct pico_ipv4_hdr *info;
daniele 29:1a47b7151851 92 if (f == NULL) {
daniele 29:1a47b7151851 93 pico_err = PICO_ERR_EINVAL;
daniele 29:1a47b7151851 94 return -1;
daniele 29:1a47b7151851 95 }
daniele 29:1a47b7151851 96 reply = pico_proto_ipv4.alloc(&pico_proto_ipv4, 8 + sizeof(struct pico_ipv4_hdr) + PICO_ICMPHDR_UN_SIZE);
daniele 29:1a47b7151851 97 info = (struct pico_ipv4_hdr*)(f->net_hdr);
daniele 29:1a47b7151851 98 hdr = (struct pico_icmp4_hdr *) reply->transport_hdr;
daniele 29:1a47b7151851 99 hdr->type = type;
daniele 29:1a47b7151851 100 hdr->code = code;
daniele 29:1a47b7151851 101 hdr->hun.ih_pmtu.ipm_nmtu = short_be(1500);
daniele 29:1a47b7151851 102 hdr->hun.ih_pmtu.ipm_void = 0;
daniele 29:1a47b7151851 103 reply->transport_len = 8 + sizeof(struct pico_ipv4_hdr) + PICO_ICMPHDR_UN_SIZE;
daniele 29:1a47b7151851 104 reply->payload = reply->transport_hdr + PICO_ICMPHDR_UN_SIZE;
daniele 29:1a47b7151851 105 memcpy(reply->payload, f->net_hdr, 8 + sizeof(struct pico_ipv4_hdr));
daniele 29:1a47b7151851 106 pico_icmp4_checksum(reply);
daniele 29:1a47b7151851 107 pico_ipv4_frame_push(reply, &info->src, PICO_PROTO_ICMP4);
daniele 29:1a47b7151851 108 return 0;
daniele 29:1a47b7151851 109 }
daniele 29:1a47b7151851 110
daniele 29:1a47b7151851 111 int pico_icmp4_port_unreachable(struct pico_frame *f)
daniele 29:1a47b7151851 112 {
daniele 29:1a47b7151851 113 /*Parameter check executed in pico_icmp4_notify*/
daniele 29:1a47b7151851 114 return pico_icmp4_notify(f, PICO_ICMP_UNREACH, PICO_ICMP_UNREACH_PORT);
daniele 29:1a47b7151851 115 }
daniele 29:1a47b7151851 116
daniele 29:1a47b7151851 117 int pico_icmp4_proto_unreachable(struct pico_frame *f)
daniele 29:1a47b7151851 118 {
daniele 29:1a47b7151851 119 /*Parameter check executed in pico_icmp4_notify*/
daniele 29:1a47b7151851 120 return pico_icmp4_notify(f, PICO_ICMP_UNREACH, PICO_ICMP_UNREACH_PROTOCOL);
daniele 29:1a47b7151851 121 }
daniele 29:1a47b7151851 122
daniele 29:1a47b7151851 123 int pico_icmp4_dest_unreachable(struct pico_frame *f)
daniele 29:1a47b7151851 124 {
daniele 29:1a47b7151851 125 /*Parameter check executed in pico_icmp4_notify*/
daniele 29:1a47b7151851 126 return pico_icmp4_notify(f, PICO_ICMP_UNREACH, PICO_ICMP_UNREACH_HOST);
daniele 29:1a47b7151851 127 }
daniele 29:1a47b7151851 128
daniele 29:1a47b7151851 129 int pico_icmp4_ttl_expired(struct pico_frame *f)
daniele 29:1a47b7151851 130 {
daniele 29:1a47b7151851 131 /*Parameter check executed in pico_icmp4_notify*/
daniele 29:1a47b7151851 132 return pico_icmp4_notify(f, PICO_ICMP_TIME_EXCEEDED, PICO_ICMP_TIMXCEED_INTRANS);
daniele 29:1a47b7151851 133 }
daniele 29:1a47b7151851 134
daniele 29:1a47b7151851 135 int pico_icmp4_packet_filtered(struct pico_frame *f)
daniele 29:1a47b7151851 136 {
daniele 29:1a47b7151851 137 /*Parameter check executed in pico_icmp4_notify*/
daniele 29:1a47b7151851 138 /*Packet Filtered: type 3, code 13 (Communication Administratively Prohibited)*/
daniele 29:1a47b7151851 139 return pico_icmp4_notify(f, PICO_ICMP_UNREACH, PICO_ICMP_UNREACH_FILTER_PROHIB);
daniele 29:1a47b7151851 140 }
daniele 29:1a47b7151851 141
daniele 29:1a47b7151851 142 /***********************/
daniele 29:1a47b7151851 143 /* Ping implementation */
daniele 29:1a47b7151851 144 /***********************/
daniele 29:1a47b7151851 145 /***********************/
daniele 29:1a47b7151851 146 /***********************/
daniele 29:1a47b7151851 147 /***********************/
daniele 29:1a47b7151851 148
daniele 29:1a47b7151851 149
daniele 29:1a47b7151851 150 #ifdef PICO_SUPPORT_PING
daniele 29:1a47b7151851 151
daniele 29:1a47b7151851 152
daniele 29:1a47b7151851 153 struct pico_icmp4_ping_cookie
daniele 29:1a47b7151851 154 {
daniele 29:1a47b7151851 155 struct pico_ip4 dst;
daniele 29:1a47b7151851 156 uint16_t err;
daniele 29:1a47b7151851 157 uint16_t id;
daniele 29:1a47b7151851 158 uint16_t seq;
daniele 29:1a47b7151851 159 uint16_t size;
daniele 29:1a47b7151851 160 int count;
daniele 29:1a47b7151851 161 unsigned long timestamp;
daniele 29:1a47b7151851 162 int interval;
daniele 29:1a47b7151851 163 int timeout;
daniele 29:1a47b7151851 164 void (*cb)(struct pico_icmp4_stats*);
daniele 29:1a47b7151851 165
daniele 29:1a47b7151851 166 };
daniele 29:1a47b7151851 167
daniele 29:1a47b7151851 168 static int cookie_compare(void *ka, void *kb)
daniele 29:1a47b7151851 169 {
tass 51:ab4529a384a6 170 struct pico_icmp4_ping_cookie *a = ka, *b = kb;
daniele 29:1a47b7151851 171 if (a->id < b->id)
daniele 29:1a47b7151851 172 return -1;
daniele 29:1a47b7151851 173 if (a->id > b->id)
daniele 29:1a47b7151851 174 return 1;
daniele 29:1a47b7151851 175 return (a->seq - b->seq);
daniele 29:1a47b7151851 176 }
daniele 29:1a47b7151851 177
daniele 29:1a47b7151851 178 PICO_TREE_DECLARE(Pings,cookie_compare);
daniele 29:1a47b7151851 179
daniele 29:1a47b7151851 180 static int pico_icmp4_send_echo(struct pico_icmp4_ping_cookie *cookie)
daniele 29:1a47b7151851 181 {
daniele 29:1a47b7151851 182 struct pico_frame *echo = pico_proto_ipv4.alloc(&pico_proto_ipv4, PICO_ICMPHDR_UN_SIZE + cookie->size);
daniele 29:1a47b7151851 183 struct pico_icmp4_hdr *hdr;
daniele 29:1a47b7151851 184
daniele 29:1a47b7151851 185 hdr = (struct pico_icmp4_hdr *) echo->transport_hdr;
daniele 29:1a47b7151851 186
daniele 29:1a47b7151851 187 hdr->type = PICO_ICMP_ECHO;
daniele 29:1a47b7151851 188 hdr->code = 0;
daniele 29:1a47b7151851 189 hdr->hun.ih_idseq.idseq_id = short_be(cookie->id);
daniele 29:1a47b7151851 190 hdr->hun.ih_idseq.idseq_seq = short_be(cookie->seq);
daniele 29:1a47b7151851 191 echo->transport_len = PICO_ICMPHDR_UN_SIZE + cookie->size;
daniele 29:1a47b7151851 192 echo->payload = echo->transport_hdr + PICO_ICMPHDR_UN_SIZE;
daniele 29:1a47b7151851 193 echo->payload_len = cookie->size;
daniele 29:1a47b7151851 194 /* XXX: Fill payload */
daniele 29:1a47b7151851 195 pico_icmp4_checksum(echo);
daniele 29:1a47b7151851 196 pico_ipv4_frame_push(echo, &cookie->dst, PICO_PROTO_ICMP4);
daniele 29:1a47b7151851 197 return 0;
daniele 29:1a47b7151851 198 }
daniele 29:1a47b7151851 199
daniele 29:1a47b7151851 200
daniele 29:1a47b7151851 201 static void ping_timeout(unsigned long now, void *arg)
daniele 29:1a47b7151851 202 {
daniele 29:1a47b7151851 203 struct pico_icmp4_ping_cookie *cookie = (struct pico_icmp4_ping_cookie *)arg;
tass 51:ab4529a384a6 204 IGNORE_PARAMETER(now);
tass 51:ab4529a384a6 205
daniele 29:1a47b7151851 206 if(pico_tree_findKey(&Pings,cookie)){
daniele 29:1a47b7151851 207 if (cookie->err == PICO_PING_ERR_PENDING) {
daniele 29:1a47b7151851 208 struct pico_icmp4_stats stats;
daniele 29:1a47b7151851 209 stats.dst = cookie->dst;
daniele 29:1a47b7151851 210 stats.seq = cookie->seq;
daniele 29:1a47b7151851 211 stats.time = 0;
daniele 29:1a47b7151851 212 stats.size = cookie->size;
daniele 29:1a47b7151851 213 stats.err = PICO_PING_ERR_TIMEOUT;
daniele 29:1a47b7151851 214 dbg(" ---- Ping timeout!!!\n");
daniele 29:1a47b7151851 215 cookie->cb(&stats);
daniele 29:1a47b7151851 216 }
daniele 29:1a47b7151851 217
daniele 29:1a47b7151851 218 pico_tree_delete(&Pings,cookie);
daniele 29:1a47b7151851 219 pico_free(cookie);
daniele 29:1a47b7151851 220 }
daniele 29:1a47b7151851 221 }
daniele 29:1a47b7151851 222
daniele 29:1a47b7151851 223 static void next_ping(unsigned long now, void *arg);
daniele 29:1a47b7151851 224 static inline void send_ping(struct pico_icmp4_ping_cookie *cookie)
daniele 29:1a47b7151851 225 {
daniele 29:1a47b7151851 226 pico_icmp4_send_echo(cookie);
daniele 29:1a47b7151851 227 cookie->timestamp = pico_tick;
daniele 29:1a47b7151851 228 pico_timer_add(cookie->timeout, ping_timeout, cookie);
daniele 29:1a47b7151851 229 if (cookie->seq < cookie->count)
daniele 29:1a47b7151851 230 pico_timer_add(cookie->interval, next_ping, cookie);
daniele 29:1a47b7151851 231 }
daniele 29:1a47b7151851 232
daniele 29:1a47b7151851 233 static void next_ping(unsigned long now, void *arg)
daniele 29:1a47b7151851 234 {
daniele 29:1a47b7151851 235 struct pico_icmp4_ping_cookie *newcookie, *cookie = (struct pico_icmp4_ping_cookie *)arg;
tass 51:ab4529a384a6 236 IGNORE_PARAMETER(now);
daniele 29:1a47b7151851 237
tass 51:ab4529a384a6 238 if(pico_tree_findKey(&Pings,cookie)){
daniele 29:1a47b7151851 239 if (cookie->seq < cookie->count) {
daniele 29:1a47b7151851 240 newcookie = pico_zalloc(sizeof(struct pico_icmp4_ping_cookie));
daniele 29:1a47b7151851 241 if (!newcookie)
daniele 29:1a47b7151851 242 return;
daniele 29:1a47b7151851 243 memcpy(newcookie, cookie, sizeof(struct pico_icmp4_ping_cookie));
daniele 29:1a47b7151851 244 newcookie->seq++;
daniele 29:1a47b7151851 245
tass 51:ab4529a384a6 246 pico_tree_insert(&Pings,newcookie);
daniele 29:1a47b7151851 247 send_ping(newcookie);
daniele 29:1a47b7151851 248 }
daniele 29:1a47b7151851 249 }
daniele 29:1a47b7151851 250 }
daniele 29:1a47b7151851 251
daniele 29:1a47b7151851 252
daniele 29:1a47b7151851 253 static void ping_recv_reply(struct pico_frame *f)
daniele 29:1a47b7151851 254 {
daniele 29:1a47b7151851 255 struct pico_icmp4_ping_cookie test, *cookie;
daniele 29:1a47b7151851 256 struct pico_icmp4_hdr *hdr = (struct pico_icmp4_hdr *) f->transport_hdr;
daniele 29:1a47b7151851 257 test.id = short_be(hdr->hun.ih_idseq.idseq_id );
daniele 29:1a47b7151851 258 test.seq = short_be(hdr->hun.ih_idseq.idseq_seq);
daniele 29:1a47b7151851 259
daniele 29:1a47b7151851 260 cookie = pico_tree_findKey(&Pings, &test);
daniele 29:1a47b7151851 261 if (cookie) {
daniele 29:1a47b7151851 262 struct pico_icmp4_stats stats;
daniele 29:1a47b7151851 263 cookie->err = PICO_PING_ERR_REPLIED;
daniele 29:1a47b7151851 264 stats.dst = cookie->dst;
daniele 29:1a47b7151851 265 stats.seq = cookie->seq;
daniele 29:1a47b7151851 266 stats.size = cookie->size;
daniele 29:1a47b7151851 267 stats.time = pico_tick - cookie->timestamp;
daniele 29:1a47b7151851 268 stats.err = cookie->err;
daniele 29:1a47b7151851 269 stats.ttl = ((struct pico_ipv4_hdr *)f->net_hdr)->ttl;
tass 51:ab4529a384a6 270 if(cookie->cb != NULL)
tass 51:ab4529a384a6 271 cookie->cb(&stats);
daniele 29:1a47b7151851 272 } else {
daniele 29:1a47b7151851 273 dbg("Reply for seq=%d, not found.\n", test.seq);
daniele 29:1a47b7151851 274 }
daniele 29:1a47b7151851 275 }
daniele 29:1a47b7151851 276
daniele 29:1a47b7151851 277 int pico_icmp4_ping(char *dst, int count, int interval, int timeout, int size, void (*cb)(struct pico_icmp4_stats *))
daniele 29:1a47b7151851 278 {
daniele 29:1a47b7151851 279 static uint16_t next_id = 0x91c0;
daniele 29:1a47b7151851 280 struct pico_icmp4_ping_cookie *cookie;
daniele 29:1a47b7151851 281
daniele 29:1a47b7151851 282 if((dst == NULL) || (interval == 0) || (timeout == 0) || (count == 0)){
daniele 29:1a47b7151851 283 pico_err = PICO_ERR_EINVAL;
daniele 29:1a47b7151851 284 return -1;
daniele 29:1a47b7151851 285 }
daniele 29:1a47b7151851 286
daniele 29:1a47b7151851 287 cookie = pico_zalloc(sizeof(struct pico_icmp4_ping_cookie));
daniele 29:1a47b7151851 288 if (!cookie) {
daniele 29:1a47b7151851 289 pico_err = PICO_ERR_ENOMEM;
daniele 29:1a47b7151851 290 return -1;
daniele 29:1a47b7151851 291 }
daniele 29:1a47b7151851 292
daniele 29:1a47b7151851 293 if (pico_string_to_ipv4(dst, &cookie->dst.addr) < 0) {
daniele 29:1a47b7151851 294 pico_err = PICO_ERR_EINVAL;
daniele 29:1a47b7151851 295 pico_free(cookie);
daniele 29:1a47b7151851 296 return -1;
daniele 29:1a47b7151851 297 }
daniele 29:1a47b7151851 298 cookie->seq = 1;
daniele 29:1a47b7151851 299 cookie->id = next_id++;
daniele 29:1a47b7151851 300 cookie->err = PICO_PING_ERR_PENDING;
daniele 29:1a47b7151851 301 cookie->size = size;
daniele 29:1a47b7151851 302 cookie->interval = interval;
daniele 29:1a47b7151851 303 cookie->timeout = timeout;
daniele 29:1a47b7151851 304 cookie->cb = cb;
daniele 29:1a47b7151851 305 cookie->count = count;
daniele 29:1a47b7151851 306
daniele 29:1a47b7151851 307 pico_tree_insert(&Pings,cookie);
daniele 29:1a47b7151851 308 send_ping(cookie);
daniele 29:1a47b7151851 309
daniele 29:1a47b7151851 310 return 0;
daniele 29:1a47b7151851 311
daniele 29:1a47b7151851 312 }
daniele 29:1a47b7151851 313
daniele 29:1a47b7151851 314 #endif