Lwip Swedish Institute of Computer Science February 20, 2001 Adam Dunkels adam@sics.se (QQ: 10205001) (QQ: 329147) (QQ:3232253) (QQ:3232253) QQ ARM TCPIP LCD10988210
LWIP TCP/IP LWIP LWIP lwip API lwip LWIP APILWIP 1 Introduction..1 2 Protocol layering..1 3 Overview...2 4 Process model...2 5 The operating system emulation layer...3 6 Buffer and memory management.......3 6.1 Packet buffers pbufs 3 6.2 Memory management..5 7 Network interfaces...5 8 IP processing. 7 8.1 Receiving packets.7 8.2 Sending packets 7 8.3 Forwarding packets..8 8.4 ICMP processing.8 9 UDP processing.....8 10 TCP processing...9 10.1 Overview 9 10.2 Data structures..10
10.3 Sequence number calculations.12 10.4 Queuing and transmitting data.12 10.4.1 Silly window avoidance 13 10.5 Receiving segments..13 10.5.1 Demultiplexing..13 10.5.2 Receiving data...14 10.6 Accepting new connections.14 10.7 Fast retransmit..14 10.8 Timers..14 10.9 Round-trip time estimation..15 10.10Congestion control..15 11 Interfacing the stack 15 12 Application Program Interface...16 12.1 Basic concepts..16 12.2 Implementation of the API.. 17 13 Statistical code analysis 17 13.1 Lines of code 18 13.2 Object code size...19 14 Performance analysis...20 15 API reference....21 15.1 Data types.21 15.1.1 Netbufs..21 15.2 Bu er functions..21 15.2.1 netbuf new()..21 15.2.2 netbuf delete()...21 15.2.3 netbuf alloc().22 15.2.4 netbuf free()..22 15.2.5 netbuf ref() 22 15.2.6 netbuf len()... 23 15.2.7 netbuf data(). 23 15.2.8 netbuf next(). 23 15.2.9 netbuf rst()..24 15.2.10 netbuf copy()...24 15.2.11 netbuf chain()..24 15.2.12 netbuf fromaddr() 24 15.2.13 netbuf fromport() 25 16 Network connection functions.25 16.0.14 netconn new()..25 16.0.15 netconn delete()...25 16.0.16 netconn type()..25 16.0.17 netconn peer(). 25 16.0.18 netconn addr() 26 16.0.19 netconn bind() 26 16.0.20 netconn connect(.26 16.0.21 netconn listen() 26 16.0.22 netconn accept()..26 16.0.23 netconn recv().27 16.0.24 netconn write() 28 16.0.25 netconn send().29 16.0.26 netconn close() 30 17 BSD socket library...30 17.1 The representation of a socket.30 17.2 Allocating a socket..30 17.2.1 The socket() call 30 17.3 Connection setup..31 17.3.1 The bind() call...31
17.3.2 The connect() call. 31 17.3.3 The listen() call.32 17.3.4 The accept() call.. 32 17.4 Sending and receiving data..33 17.4.1 The send() call.33 17.4.2 The sendto() and sendmsg() calls 34 17.4.3 The write() call. 34 17.4.4 The recv() and read() calls 35 17.4.5 The recvfrom() and recvmsg() calls.36 18 Code examples.36 18.1 Using the API.36 18.2 Directly interfacing the stack.. 39 Bibliography..41 1 2 Protocol layering
3 Overview 4 Process model 5 lwip lwip
6 Pbuf lwip Pbufs BSD mbufs pbuf Pbufs pbuf pbufs Pbufs PBUF RAM PBUF ROMPBUF POOL 1 pbuf PBUF RAMpbuf 2pbuf pbuf pbufpbuf RAM where the first pbufin the chain is of the PBUF RAM typepbuf ROMpbuf pbuf PBUF POOL3pbufs pbufs consists of fixed size pbufs allocated from a pool of fixedsize pbufs. pbuf pbufs PBUF_POOL pbuf suitable foruse in an interrupt handler PBUF_ROM pbuftcp/ip pbufrom ( PBUF_ROM )
16 161 alignment9
9 UDP UDP checksumming UDP
12. API 1
13 2. LWIP Intel x86
14. LWIP X86 3. LWIP 6502
15. LWIP 6502
15.2.1 netbuf new() 15.2.2 netbuf delete() netbufs int main() { struct netbuf *buf; buf = netbuf_new(); /* netbuf */ netbuf_alloc(buf, 100); /* 100 bytes buffer */ struct netbuf * netbuf new(void) void netbuf delete(struct netbuf *) /* netbuf */ /* [...] */ netbuf_delete(buf); /* netbuf */ } 15.2.3 netbuf alloc()
15.2.4 netbuf free() netbuf buf buffer buffer netbuf buf 15.2.5 netbuf ref() netbuf buf size netbuf netbuf_alloc() netbufmalloc() netbuf_ref()buffer netbuf_ref() int main() { struct netbuf *buf; char string[] = " "; /* netbuf */ buf = netbuf_new(); void * netbuf alloc(struct netbuf *buf, int size) int netbuf free(struct netbuf *buf) int netbuf ref(struct netbuf *buf, void *data, int size) /* */ netbuf_ref(buf, string, sizeof(string)); /* netbuf */ /* [...] */ /* netbuf */ netbuf_delete(buf); }
15.2.6 netbuf len() netbuf buf netbufnetbuf netbuf 15.2.7 netbuf data() netbuf data len netbuf netbuf netbuf_first() netbuf_next() netbuf netbuf_next() netbuf_data() 15.2.8 netbuf next() netbuf buf netbuf netbuf>0 <0. netbuf_next() buf netbuf /* [...] */ do { char *data; int len; int netbuf len(struct netbuf *buf) int netbuf data(struct netbuf *buf, void **data, int *len) int netbuf next(struct netbuf *buf) /* */ netbuf_data(buf, &data, &len); /* */ do_something(data, len); } while(netbuf_next(buf) >= 0); /* [...] */ 15.2.9 netbuf first()
netbuf buf 15.2.10 netbuf copy() netbuf bufdatanetbuf buflen data netbuf_copy()200 netbuf buf 200200data void example_function(struct netbuf *buf) { char data[200]; netbuf_copy(buf, data, 200); /* */ } 15.2.11 netbuf chain() netbufs netbuf 15.2.12 netbuf fromaddr() void netbuf first(struct netbuf *buf) void netbuf copy(struct netbuf *buf, void *data, int len) void netbuf chain(struct netbuf *head, struct netbuf *tail) struct ip addr * netbuf fromaddr(struct netbuf *buf) netbuf buf IPnetbuf netbuf_fromport() 15.2.13 netbuf fromport()
netbuf bufnetbuf netbuf_fromaddr() IP 16 16.0.14 netconn new() TCP UDP NETCONN_TCP NETCONN_UDP 16.0.15 netconn delete() netconn conn 16.0.16 netconn type() connnetconn_new() NETCONN_TCP NETCONN_UDP 16.0.17 netconn peer() unsigned short netbuf fromport(struct netbuf *buf) struct netconn * netconn new(enum netconn type type) void netconn delete(struct netconn *conn) enum netconn type netconn type(struct netconn *conn) int netconn peer(struct netconn *conn, struct ip addr **addr, unsigned short port) IPaddr port conn
conn IP 16.0.19 netconn bind() conn IP addr TCPUDP addr IP 16.0.20 netconn connect() UDP UDP remote_addr remote_port TCP netconn_connect() 16.0.21 netconn listen() TCPconnTCP 16.0.22 netconn accept() TCPconn netconn_listen() netconn_accept() 2000 TCP int main() { struct netconn *conn, *newconn; /* */ conn = netconn_new(netconn_tcp); /* IP 2000*/ netconn_bind(conn, NULL, 2000); /* */ netconn_listen(conn);
/* block*/ newconn = netconn_accept(conn); /* newconn */ process_connection(newconn); /* */ netconn_delete(newconn); netconn_delete(conn); } 16.0.23 netconn recv() conn NULL netbuf netconn_ recv() example_function() void example_function(struct netconn *conn) { struct netbuf *buf; /* */ while((buf = netconn_recv(conn))!= NULL) { do_something(buf); } /* */ netconn_close(conn); } 16.0.24 netconn write() TCP TCP conn bu &-#174;ers TCP It puts the data pointed to by data on the output queue for the TCP connection conn. len flags #define NETCONN_NOCOPY 0x00 #define NETCONN_COPY 0x01
NETCONN_COPY flag flag NETCONN_NOCOPY stay there for an indeterminate amount of time. ROM netconn_write() flag NETCONN_COPY text int main() { struct netconn *conn; char data[10]; char text[] = "Static text"; int i; /*conn */ /* [...] */ /* */ for(i = 0; i < 10; i++) data[i] = i; netconn_write(conn, data, 10, NETCONN_COPY); netconn_write(conn, text, sizeof(text), NETCONN_NOCOPY); /* */ for(i = 0; i < 10; i++) data[i] = 10 - i; /* take down the connection conn */ netconn_close(conn); } 16.0.25 netconn send() UDPconn netbuf bufnetbuf IP MTU netbuf1000 netbuf IP10.10.0.1UDP7000 UDP
int main() { struct netconn *conn; struct netbuf *buf; struct ip_addr addr; char *data; char text[] = "A static text"; int i; /* */ conn = netconn_new(netconn_udp); /* IP*/ addr.addr = htonl(0x0a000001); /* */ netconn_connect(conn, &addr, 7000); /* netbuf */ buf = netbuf_new(); data = netbuf_alloc(buf, 10); /* */ for(i = 0; i < 10; i++) data[i] = i; /* */ netconn_send(conn, buf); netbuf_ref(buf, text, sizeof(text)); /* text */ netconn_send(conn, buf); /*connection netbuf */ netconn_delete(conn); netconn_delete(buf); 16.0.26 netconn close() conn 17 BSD socket BSD socket APILWIP API BSD socket API BSD socket APInetconn BSD socketsnetconn BSD socket
socketbsdsocket socket API UDP (SOCK DGRAM) TCP (SOCK STREAM)