⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 uip-doc.txt

📁 spi口的超小型网络连接器
💻 TXT
📖 第 1 页 / 共 4 页
字号:
/**\mainpage The uIP TCP/IP stack\author Adam Dunkels, http://www.sics.se/~adam/The uIP TCP/IP stack is intended to make it possible to communicateusing the TCP/IP protocol suite even on small 8-bitmicro-controllers. Despite being small and simple, uIP do not requiretheir peers to have complex, full-size stacks, but can communicatewith peers running a similarly light-weight stack. The code size is onthe order of a few kilobytes and RAM usage can be configured to be aslow as a few hundred bytes.uIP can be found at the uIP web page: http://www.sics.se/~adam/uip/\sa \ref apps "Application programs"\sa \ref uipopt "Compile-time configuration options"\sa \ref uipconffunc "Run-time configuration functions"\sa \ref uipinit "Initialization functions"\sa \ref uipdevfunc "Device driver interface" and    \ref uipdrivervars "variables used by device drivers"\sa \ref uipappfunc "uIP functions called from application programs"(see below) and the \ref psock "protosockets API" and their underlying\ref pt "protothreads" \section uIPIntroduction IntroductionWith the success of the Internet, the TCP/IP protocol suite has becomea global standard for communication. TCP/IP is the underlying protocolused for web page transfers, e-mail transmissions, file transfers, andpeer-to-peer networking over the Internet. For embedded systems, beingable to run native TCP/IP makes it possible to connect the systemdirectly to an intranet or even the global Internet. Embedded deviceswith full TCP/IP support will be first-class network citizens, thusbeing able to fully communicate with other hosts in the network.Traditional TCP/IP implementations have required far too muchresources both in terms of code size and memory usage to be useful insmall 8 or 16-bit systems. Code size of a few hundred kilobytes andRAM requirements of several hundreds of kilobytes have made itimpossible to fit the full TCP/IP stack into systems with a few tensof kilobytes of RAM and room for less than 100 kilobytes ofcode.The uIP implementation is designed to have only the absolute minimalset of features needed for a full TCP/IP stack. It can only handle asingle network interface and contains the IP, ICMP, UDP and TCPprotocols. uIP is written in the C programming language.Many other TCP/IP implementations for small systems assume that theembedded device always will communicate with a full-scale TCP/IPimplementation running on a workstation-class machine. Under thisassumption, it is possible to remove certain TCP/IP mechanisms thatare very rarely used in such situations. Many of those mechanisms areessential, however, if the embedded device is to communicate withanother equally limited device, e.g., when running distributedpeer-to-peer services and protocols. uIP is designed to be RFCcompliant in order to let the embedded devices to act as first-classnetwork citizens. The uIP TCP/IP implementation that is not tailoredfor any specific application.\section tcpip TCP/IP CommunicationThe full TCP/IP suite consists of numerous protocols, ranging from lowlevel protocols such as ARP which translates IP addresses to MACaddresses, to application level protocols such as SMTP that is used totransfer e-mail. The uIP is mostly concerned with the TCP and IPprotocols and upper layer protocols will be referred to as "theapplication". Lower layer protocols are often implemented in hardwareor firmware and will be referred to as "the network device" that arecontrolled by the network device driver.TCP provides a reliable byte stream to the upper layer protocols. Itbreaks the byte stream into appropriately sized segments and eachsegment is sent in its own IP packet. The IP packets are sent out onthe network by the network device driver. If the destination is not onthe physically connected network, the IP packet is forwarded ontoanother network by a router that is situated between the twonetworks. If the maximum packet size of the other network is smallerthan the size of the IP packet, the packet is fragmented into smallerpackets by the router. If possible, the size of the TCP segments arechosen so that fragmentation is minimized. The final recipient of thepacket will have to reassemble any fragmented IP packets before theycan be passed to higher layers.The formal requirements for the protocols in the TCP/IP stack isspecified in a number of RFC documents published by the InternetEngineering Task Force, IETF. Each of the protocols in the stack isdefined in one more RFC documents and RFC1122 collectsall requirements and updates the previous RFCs. The RFC1122 requirements can be divided into two categories; thosethat deal with the host to host communication and those that deal withcommunication between the application and the networking stack. Anexample of the first kind is "A TCP MUST be able to receive a TCPoption in any segment" and an example of the second kind is "ThereMUST be a mechanism for reporting soft TCP error conditions to theapplication." A TCP/IP implementation that violates requirements ofthe first kind may not be able to communicate with other TCP/IPimplementations and may even lead to network failures. Violation ofthe second kind of requirements will only affect the communicationwithin the system and will not affect host-to-host communication.In uIP, all RFC requirements that affect host-to-host communicationare implemented. However, in order to reduce code size, we haveremoved certain mechanisms in the interface between the applicationand the stack, such as the soft error reporting mechanism anddynamically configurable type-of-service bits for TCPconnections. Since there are only very few applications that make useof those features they can be removed without loss of generality.\section mainloop Main Control LoopThe uIP stack can be run either as a task in a multitasking system, oras the main program in a singletasking system. In both cases, the maincontrol loop does two things repeatedly: - Check if a packet has arrived from the network. - Check if a periodic timeout has occurred.If a packet has arrived, the input handler function, uip_input(),should be invoked by the main control loop. The input handler functionwill never block, but will return at once. When it returns, the stackor the application for which the incoming packet was intended may haveproduced one or more reply packets which should be sent out. If so,the network device driver should be called to send out these packets.Periodic timeouts are used to drive TCP mechanisms that depend ontimers, such as delayed acknowledgments, retransmissions andround-trip time estimations. When the main control loop infers thatthe periodic timer should fire, it should invoke the timer handlerfunction uip_periodic(). Because the TCP/IP stack may performretransmissions when dealing with a timer event, the network devicedriver should called to send out the packets that may have been produced.\section arch Architecture Specific FunctionsuIP requires a few functions to be implemented specifically for thearchitecture on which uIP is intended to run. These functions shouldbe hand-tuned for the particular architecture, but generic Cimplementations are given as part of the uIP distribution.\subsection checksums Checksum CalculationThe TCP and IP protocols implement a checksum that covers the data andheader portions of the TCP and IP packets. Since the calculation ofthis checksum is made over all bytes in every packet being sent andreceived it is important that the function that calculates thechecksum is efficient. Most often, this means that the checksumcalculation must be fine-tuned for the particular architecture onwhich the uIP stack runs.While uIP includes a generic checksum function, it also leaves it openfor an architecture specific implementation of the two functionsuip_ipchksum() and uip_tcpchksum(). The checksum calculations in thosefunctions can be written in highly optimized assembler rather thangeneric C code.\subsection longarith 32-bit ArithmeticThe TCP protocol uses 32-bit sequence numbers, and a TCPimplementation will have to do a number of 32-bit additions as part ofthe normal protocol processing. Since 32-bit arithmetic is notnatively available on many of the platforms for which uIP is intended,uIP leaves the 32-bit additions to be implemented by the architecturespecific module and does not make use of any 32-bit arithmetic in themain code base.While uIP implements a generic 32-bit addition, there is support forhaving an architecture specific implementation of the uip_add32()function.\section memory Memory ManagementIn the architectures for which uIP is intended, RAM is the mostscarce resource. With only a few kilobytes of RAM available for theTCP/IP stack to use, mechanisms used in traditional TCP/IP cannot bedirectly applied.The uIP stack does not use explicit dynamic memoryallocation. Instead, it uses a single global buffer for holdingpackets and has a fixed table for holding connection state. The globalpacket buffer is large enough to contain one packet of maximumsize. When a packet arrives from the network, the device driver placesit in the global buffer and calls the TCP/IP stack. If the packetcontains data, the TCP/IP stack will notify the correspondingapplication. Because the data in the buffer will be overwritten by thenext incoming packet, the application will either have to actimmediately on the data or copy the data into a secondary buffer forlater processing. The packet buffer will not be overwritten by newpackets before the application has processed the data. Packets thatarrive when the application is processing the data must be queued,either by the network device or by the device driver. Most single-chipEthernet controllers have on-chip buffers that are large enough tocontain at least 4 maximum sized Ethernet frames. Devices that arehandled by the processor, such as RS-232 ports, can copy incomingbytes to a separate buffer during application processing. If thebuffers are full, the incoming packet is dropped. This will causeperformance degradation, but only when multiple connections arerunning in parallel. This is because uIP advertises a very smallreceiver window, which means that only a single TCP segment will be inthe network per connection.In uIP, the same global packet buffer that is used for incomingpackets is also used for the TCP/IP headers of outgoing data. If theapplication sends dynamic data, it may use the parts of the globalpacket buffer that are not used for headers as a temporary storagebuffer. To send the data, the application passes a pointer to the dataas well as the length of the data to the stack. The TCP/IP headers arewritten into the global buffer and once the headers have beenproduced, the device driver sends the headers and the application dataout on the network. The data is not queued forretransmissions. Instead, the application will have to reproduce thedata if a retransmission is necessary.The total amount of memory usage for uIP depends heavily on theapplications of the particular device in which the implementations areto be run. The memory configuration determines both the amount oftraffic the system should be able to handle and the maximum amount ofsimultaneous connections. A device that will be sending large e-mailswhile at the same time running a web server with highly dynamic webpages and multiple simultaneous clients, will require more RAM than asimple Telnet server. It is possible to run the uIP implementationwith as little as 200 bytes of RAM, but such a configuration willprovide extremely low throughput and will only allow a small number ofsimultaneous connections.\section api Application Program Interface (API)The Application Program Interface (API) defines the way theapplication program interacts with the TCP/IP stack. The most commonlyused API for TCP/IP is the BSD socket API which is used in most Unixsystems and has heavily influenced the Microsoft Windows WinSockAPI. Because the socket API uses stop-and-wait semantics, it requiressupport from an underlying multitasking operating system. Since theoverhead of task management, context switching and allocation of stackspace for the tasks might be too high in the intended uIP targetarchitectures, the BSD socket interface is not suitable for ourpurposes.uIP provides two APIs to programmers: protosockets, a BSD socket-likeAPI without the overhead of full multi-threading, and a "raw"event-based API that is nore low-level than protosockets but uses lessmemory.\sa \ref psock\sa \ref pt\subsection rawapi The uIP raw APIThe "raw" uIP API uses an event driven interface where the application isinvoked in response to certain events. An application running on topof uIP is implemented as a C function that is called by uIP inresponse to certain events. uIP calls the application when data isreceived, when data has been successfully delivered to the other endof the connection, when a new connection has been set up, or when datahas to be retransmitted. The application is also periodically polledfor new data. The application program provides only one callbackfunction; it is up to the application to deal with mapping differentnetwork services to different ports and connections. Because theapplication is able to act on incoming data and connection requests assoon as the TCP/IP stack receives the packet, low response times canbe achieved even in low-end systems.uIP is different from other TCP/IP stacks in that it requires helpfrom the application when doing retransmissions. Other TCP/IP stacksbuffer the transmitted data in memory until the data is known to besuccessfully delivered to the remote end of the connection. If thedata needs to be retransmitted, the stack takes care of theretransmission without notifying the application. With this approach,the data has to be buffered in memory while waiting for anacknowledgment even if the application might be able to quicklyregenerate the data if a retransmission has to be made.In order to reduce memory usage, uIP utilizes the fact that theapplication may be able to regenerate sent data and lets theapplication take part in retransmissions. uIP does not keep track ofpacket contents after they have been sent by the device driver, anduIP requires that the application takes an active part in performingthe retransmission. When uIP decides that a segment should beretransmitted, it calls the application with a flag set indicatingthat a retransmission is required. The application checks theretransmission flag and produces the same data that was previouslysent. From the application's standpoint, performing a retransmissionis not different from how the data originally was sent. Therefore theapplication can be written in such a way that the same code is usedboth for sending data and retransmitting data. Also, it is importantto note that even though the actual retransmission operation iscarried out by the application, it is the responsibility of the stackto know when the retransmission should be made. Thus the complexity of

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -