UDP: User Datagram
UDP is a simple datagram-oriented transport layer protocol. Each output operation of a process usually generates a UDP datagram and assembles it into an IP datagram to be sent. This is different from the stream-oriented protocol, such as TCP. The data generated by an application is not directly associated with a single IP datagram.
UDP input and output and lightweight UDP involve the following files:
Include/net/udplite. h defines lightweight UDP-specific functions.
Include/linux/udp. h defines UDP transmission control blocks, etc.
Net/ipv4/udp. c UDP protocol implementation
Net/ipv4/udplite. c lightweight UDP implementation
Net/core/sock. c to implement common functions at the transport layer
Net/ipv4/datemedia.c Implement UDP connect call
Net/ipv4/af_inet.c network layer and transport layer Interfaces
UDP Input and Output
The call relationship between UDP functions is much simpler than TCP. Like TCP, The sk_receive_queue member in the sock structure is the UDP receiving queue. Generally, the received UDP datagram is cached here, wait for the user process to read. The process after UDP receives the datagram is much simpler than that after TCP. The UDP datagram that passes the verification is processed according to the type and then added to the receiving queue.
UDP Transmission Control Block
- struct udp_sock {
- /* inet_sock has to be the first member */
- struct inet_sock inet;
- int pending; /* Any pending frames ? */
- unsigned int corkflag; /* Cork is required */
- __u16 encap_type; /* Is this an Encapsulation socket? */
- /*
- * Following member retains the information to create a UDP header
- * when the socket is uncorked.
- */
- __u16 len; /* total length of pending frames */
- /*
- * Fields specific to UDP-Lite.
- */
- __u16 pcslen;
- __u16 pcrlen;
- /* indicator bits used by pcflag: */
- #define UDPLITE_BIT 0x1 /* set by udplite proto init function */
- #define UDPLITE_SEND_CC 0x2 /* set via udplite setsockopt */
- #define UDPLITE_RECV_CC 0x4 /* set via udplite setsocktopt */
- __u8 pcflag; /* marks socket as UDP-Lite if > 0 */
- __u8 unused[3];
- /*
- * For encapsulation sockets.
- */
- int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);
- };
Struct inet_sock inet
Udp_sock is extended by the inet_sock Structure
Int pending
Sending status. The value can only be 0 or AF_INET. 0 indicates that the data has been sent from the UDP interface to the IP layer. You can continue to call sendmsg () to send the data, AF_INET indicates that UDP is processing the data sent by calling sendmsg (). It does not need to process the destination address, route, and other information, and directly processes UDP data.
Unsigned int corkflag
0. When data needs to be sent, it is sent immediately.
If the value is not 0, UDP data is formed into a single 64 kB UDP datagram and then sent out, resulting in latency.
_ 2010len
Identifies the length of the data to be sent when the data is sent from the UDP interface to the IP layer
_ U16pcslen
_ 2010pcrlen
Lightweight UDP, Which is set through the UDPLITE_SEND_CSCOV and UDPLITE_RECV_CSCOV options to control sending and receiving checksum execution
0 indicates that the entire UDP-Lite packet sent/received is verified.
>>= 8 indicates verifying the first pcslen/pcrlen bytes of the UDP-Lite package sent/received
Other values are invalid.
UDP status
UDP transmission is stateless, but in fact, UDP and RAW also borrow some TCP values: When a set of interfaces is created, the status is TCP_CLOSE, after the UDP set interface calls connect (), the status changes to TCP_ESTABLISHED. Finally, when the set interface is closed, TCP_CLOSE is returned, as is RAW.
Lightweight UDP
Linux 2.6.20 supports UDP-Lite. The UDP-Lite protocol is relatively new, similar to the UDP protocol, but it is more suitable for situations where the network error rate is large and applications are not sensitive to minor errors, such as real-time video playback. So what is the difference between UDP-Lite and traditional UDP? The traditional UDP protocol performs a complete verification on its load (Payload). If only one of them changes, the entire packet may be discarded. In some cases, the cost of dropping a packet is very high, especially when the packet size is large. In UDP-Lite protocol, users do not need to verify the load of a data packet, or the number of bits to be verified is controlled by users,
In Linux, The UDP-Lite protocol is also supported by adding a setsockopt option on the basis of the original UDP protocol to control sending/receiving Checksum Coverage.
Int val = 20;
Setsockopt (s, SOL_UDPLITE, UDPLITE_SEND_CSCOV, & val, sizeof (int ));
Int min = 20;
Setsockopt (s, SOL_UDPLITE, UDPLITE_RECV_CSCOV, & min, sizeof (int ));
It is easy to create a lightweight UDP set Interface
S = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDPLITE );