UDP: User Datagram

Source: Internet
Author: User
Tags sendmsg

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

 
 
  1. struct udp_sock {   
  2.     /* inet_sock has to be the first member */   
  3.     struct inet_sock inet;   
  4.     int      pending;   /* Any pending frames ? */   
  5.     unsigned int     corkflag;  /* Cork is required */   
  6.     __u16        encap_type;    /* Is this an Encapsulation socket? */   
  7.     /*  
  8.      * Following member retains the information to create a UDP header  
  9.      * when the socket is uncorked.  
  10.      */   
  11.     __u16        len;       /* total length of pending frames */   
  12.     /*  
  13.      * Fields specific to UDP-Lite.  
  14.      */   
  15.     __u16        pcslen;   
  16.     __u16        pcrlen;   
  17. /* indicator bits used by pcflag: */   
  18. #define UDPLITE_BIT      0x1        /* set by udplite proto init function */   
  19. #define UDPLITE_SEND_CC  0x2        /* set via udplite setsockopt         */   
  20. #define UDPLITE_RECV_CC  0x4        /* set via udplite setsocktopt        */   
  21.     __u8         pcflag;        /* marks socket as UDP-Lite if > 0    */   
  22.     __u8         unused[3];   
  23.     /*  
  24.      * For encapsulation sockets.  
  25.      */   
  26.     int (*encap_rcv)(struct sock *sk, struct sk_buff *skb);   
  27. };   

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 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.