Openvswitch (ovs) Source Code Analysis workflow (send and receive packets)

Source: Internet
Author: User
Tags openvswitch

The data structure of openvswitch source code analysis (Data Structure of openvswitch (ovs) source code analysis) has been clearly written. Although there are few visitors, therefore, I have seen a phenomenon: Article 1: Introduction to openvswitch (ovs) source code analysis is actually an introduction to the current situation of cloud computing and various modules of openvswitch, I also gave a general introduction to the workflow. I personally feel that there is not much gold in learning the openvswitch source code. The cloud computing status quo is based on my personal experience gained from the company's development. It does not actually help me to learn the source code of openvswitch. Each component module of openvswitch is searched on the internet, let alone the amount of gold; finally, the only thing that has been computed is the workflow of openvswitch, which is helpful for you to understand the entire openvswitch from a macro perspective. But the overall feeling is that there is not much substantial help for learning openvswitch source code, but there are many people accessing it. On the contrary, in the second article, the data structure of openvswitch source code analysis analyzes the main data structures involved in the entire openvswitch source code, which takes a lot of effort. It is also an important basis for analyzing the entire source code, or you can use it as a dictionary tool for analyzing the openvswitch source code. However, the number of people accessing it is very small. Why?

There are many blogs on the Internet about openvswitch, but most of them only introduce openvswitch and how to install and configure it, or explain some commands. The source code analysis is very rare. At least I will search for information on the Internet when I start to learn about openvswitch. Therefore, it is very difficult for beginners to get started with learning the openvswitch source code. No information is available (of course, there are some materials on the official website. If your English is good enough, reading the official website materials is also a good choice). I had to analyze it from the beginning, but I think openvswitch was designed by a world-class distinguished team for several years, if you want to learn and analyze it from scratch, it will be the Year of the Monkey. Fortunately, when I started learning, the company's predecessors provided some learning experiences and structural materials, so I also share my learning experience and some understanding with you. If anything is incorrect, please correct me. Thank you !!!

Now that you have learned the basics, let's analyze the source code of the openvswitch workflow.

The first is the data packet receiving function. This is to bind the network adapter to the openvswitch port (ovs-vsctl add-port br0 eth0) when the network adapter is loaded, this function is called to send data packets to this function for processing. Instead of sending data packets to the kernel network protocol stack as you did (before binding), let the kernel protocol stack process them. The packet receiving function in openvswitch is void ovs_vport_receive (struct vport * vport, struct sk_buff * SKB). The function is located in datapath/vport. C. The implementation is as follows:

// The data packet receiving function. After the network adapter is bound, all data packets are transmitted from this function as the entry to openvswitch for processing. // This is the entry point of openvswitch. Parameter vport: port from which the data packet comes in; parameter SKB: Packet address pointer void ovs_vport_receive (struct vport * vport, struct sk_buff * SKB) {struct pcpu_tstats * stats; // In fact, this thing has not been clearly understood. The general function is to maintain the CPU lock status stats = this_cpu_ptr (vport-> percpu_stats); // start to get the CPU lock status, this is similar to the spin lock in the Linux kernel. u64_stats_update_begin (& stats-> syncp); // start to lock stats-> rx_packets ++; // stats-> rx_bytes + = SKB-> Len; // record the data size in the data packet u64_stats_update_end (& stats-> syncp ); // end the lock status if ( ! (Vport-> OPS-> flags & vport_f_tun_id) // This is a kind of status processing ovs_cb (SKB)-> tun_key = NULL; // In fact, the following line of code in this function is the key. If openvswitch is not studied but for work, I personally think it is unnecessary (probably impossible) // to figure out the role of each code. As long as you know what it means, what is the role of the key code? If you want to add your own code, you can add it somewhere. // The following line of code is the function call for processing data packets, which is the core part of the entire openvswitch. The input parameters are the same as the function for receiving data packets. Ovs_dp_process_received_packet (vport, SKB );}

As the saying goes, there must be more and more. The above is the function for data packets to enter openvswitch, so there must be a function corresponding to openvswitch. After a data packet enters openvswitch, The ovs_dp_process_received_packet (vport, SKB) function will be called to process the data packet. It will be analyzed later. This function matches the data packet in the stream table, then execute the corresponding action. The action will modify the data packet and then send the data packet. Then, the vport is called. function for sending data packets in C: ovs_vport_send (struct vport * vport, struct sk_buff * SKB); to send data packets to the NIC device bound to the port, then, the NIC Driver sends the data in the data packet. Of course, some actions will send data packets directly to the application layer. The following describes the implementation of the data packet sending function. The function is located in datapath/vport. C.

// This is the data packet sending function. Parameter vport: Specifies the port from which the data is sent; parameter SKB: Specifies the packet to which the data is sent out int ovs_vport_send (struct vport * vport, struct sk_buff * SKB) {// This is my own code, to filter out ARP packets. Here, you can insert an additional sentence. Whenever you add your own code to any source code, you must mark it at the beginning of the Code, this not only facilitates modification, debugging, and maintenance, but also makes it easy for others to understand/* ============== yuzhihui: ================= */If (0x806 === ntohs (SKB-> protocol) {arp_proc_send (vport, SKB ); // customize a function to process ARP packets} // In the previous data structure, Ops is the function pointer set structure of some operation functions in the vport structure. // Therefore, vport-> OPS-> send () is the function pointer to call the function, send the data packet out int sent = vport-> OPS-> send (vport, SKB); If (likely (sent )) {// defines a macro likely (). If the message is sent successfully, execute the following struct pcpu_tstats * stats; // do the following code look very familiar, yes, it is to accept the code stats = this_cpu_ptr (vport-> percpu_stats) in the function; u64_stats_update_begin (& stats-> syncp); stats-> tx_packets ++; stats-> tx_bytes + = sent; u64_stats_update_end (& stats-> syncp);} return sent; // The returned sent is the data length that has been sent successfully}

These two functions are the functions used to send and receive data packets in openvswitch. They do not fully analyze all its code. This is not my intention, I just want beginners to know that this is a function for data packets to enter and exit openvswitch. Actually, it is very useful to know that, no matter what data packet you are, as long as it is to the host (of course, including various virtual machines and servers in the host ), all of them will pass through these two functions (the accept function must be input for the accepted data, but sometimes the function cannot be sent to send data packets ), the operations on the data packets are all dependent on the operations you want.

An example of data packet operations in these two functions:

Operations in the data packet reception function: If you want to block communication with an IP host (or perform special processing on an IP host data packet ), you can process the data in the openvswitch entry function (ovs_vport_receive (struct vport * vport, struct sk_buff * SKB);) to determine the IP addresses extracted from the data packets, if the IP address is specified, the data packet is directly destroyed (you can also define a function to perform some special operations ). In this way, you can control the entire data.

Operations in the packet sending function: like the code I wrote in the above function, extract the data packet type in the data packet for determination. When it is determined that it is an ARP data packet, then, call the custom arp_proc_send (vport, SKB) function to process it, instead of sending it immediately, because you don't know what type of port the data packet sends. If it is a public IP port, then the user-defined function will directly throw the packet (ARP packets work in the LAN, and will be processed even if it is sent to the public network ); if the packet is sent to an outer LAN or connected server, the destination MAC address in the packet is modified for sending. If the packet is an ARP request packet, the packet is changed to a response packet, send the data back to the original path, and so on. These operation controls are implemented in the function of sending data packets.

The above is the data packet sending and receiving function in the openvswitch (ovs) workflow. After rough analysis and application examples, I think Beginners should know where to add their own code, to meet your functional requirements.

Reprinted please indicate the original source, original address: http://blog.csdn.net/yuzhihui_no1/article/details/39298321

If anything is incorrect, please correct me. Thank you !!!

Openvswitch (ovs) Source Code Analysis workflow (send and receive packets)

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.