DPDK Basic Module Rte_mbuf detailed

Source: Internet
Author: User

Rte_mbuf

This article assumes that the length of the message does not exceed the length of Rte_mbuf->buf_len.

Memory structure

assigning operations

Mbuf is managed by the buffer pool Rte_mempool, Rte_mempool requests multiple mbuf at the time of initialization, and the number and length of MBUF requested are specified by the user. Macro Mbuf_size is the mbuf length used in the example program:

#define MBUF_SIZE (2048 + sizeof (struct rte_mbuf) + rte_pktmbuf_headroom)

Use the following function to apply a mbuf to Rte_mempool:

struct Rte_mbuf *rte_pktmbuf_alloc (struct rte_mempool *mp);

Copy operation

DPDK receive the message and send the message to the upper application process, the message transmission is "0 copies", that is, do not need to copy the message content, only need to transfer mbuf address. However, when a message is sent to multiple applications, it is still necessary to copy the messages and send them to different applications. Librte_mbuf uses "Copy rte_mbuf, share data domain" to implement the message copy function Rte_pktmbuf_clone (), the function prototype is as follows:

struct Rte_mbuf *rte_pktmbuf_clone (struct rte_mbuf *md, struct rte_mempool *mp)

The Rte_pktmbuf_clone () function first applies for a new rte_mbuf, which we call the MBUF as the indirect buffer, denoted by MI, the parameter MD is called direct buffer. The function copies the MD's struct members (except the reference count refcnt) one by one to MI, while the MD reference count refcnt is increased by 1. At this point, Mi->pkt.data points to the MD data field.

Rte_pktmbuf_clone () requires the parameter MD to be direct buffer, and we can determine if the MD is direct by judging whether the md->buf_addr–sizeof (struct rte_mbuf) = = MD is True Buffer, this function is implemented by macro Rte_mbuf_direct (MB).

Release action

Release a mbuf with the following function, and the release process will return the MBUF to Rte_mempool:

void Rte_pktmbuf_free (struct rte_mbuf *m);

Depending on the reference count of M and the indirect/direct type of M, Rte_pktmbuf_free () releases m in the following ways:

If the reference count of M is greater than 1, only the reference count of M is reduced by 1, and the function returns;

If the reference count of M is 1 and M is the direct type, then the reference count of M is set to 0, then the M is returned to Mempool, and the function returns;

If the reference count for M is 1 and M is the indirect type, then Rte_pktmbuf_free () will set the M reference count to 0, while the reference count for m corresponding to direct buffer is reduced by 1 (minus 1, the reference count is 0, and the direct Buffer return Mempool), return m to Mempool, function returns;

Rte_pktmbuf_free () find m corresponding to direct buffer via macro rte_mbuf_from_baddr (M->BUF_ADDR), the macro is implemented as follows:

#define RTE_MBUF_FROM_BADDR (BA) ((struct rte_mbuf *) (BA))-1)

Rte_pktmbuf_free () determines whether m! = rte_mbuf_from_baddr (M->BUF_ADDR) is true to determine the Indirect/direct type of M.

Unpacking operations

The structure of the RTE_MBUF is similar to the skb_buf of the Linux kernel protocol stack, preserving headroom and tailroom respectively before and after the memory block of the message is saved to facilitate the application of the message. Headroom default 128 bytes, can be adjusted by macro Rte_pktmbuf_headroom.

We can calculate the headroom length by m->pkt.data–m->buf_addr and calculate the tailroom length by M->buf_len–m->pkt.data_len–headroom_size. These calculation procedures are implemented by the following functions:

uint16_t rte_pktmbuf_headroom (const struct RTE_MBUF *m)

uint16_t rte_pktmbuf_tailroom (const struct RTE_MBUF *m)

Assuming that m->pkt.data points to the two-layer header of the message, we can strip the two-layer header of the message by following a series of actions:

M->pkt.data + = 14;

M->pkt.data_len-= 14;

M->pkt.pkt_len-= 14;

These operations have been implemented by RTE_PKTMBUF_ADJ () and the function prototypes are as follows:

Char *rte_pktmbuf_adj (struct rte_mbuf *m, uint16_t len)

We can encapsulate a two-layer header for IP packets through the following series of actions:

M->pkt.data-= 14;

M->pkt.data_len + = 14;

M->pkt.pkt_len + = 14;

These operations are implemented by Rte_pktmbuf_prepend (), and the function prototypes are as follows:

Char *rte_pktmbuf_prepend (struct rte_mbuf *m, uint16_t len)

If you need to add n bytes of data to the tailroom, we can do the following:

Tail = m->pkt.data + m->pkt.data_len; Tail record Tailroom first address

M->pkt.data_len + = N;

M->pkt.pkt_len + = N;

These operations are implemented by Rte_pktmbuf_append (), and the function prototypes are as follows:

Char *rte_pktmbuf_append (struct rte_mbuf *m, uint16_t len)

LIBRTE_MBUF also provides the Rte_pktmbuf_trim () function to remove the last n bytes of the data field in Mbuf, which is implemented as follows:

M->pkt.data_len-= N;

M->pkt.pkt_len-= N;

The function prototypes are as follows:

int Rte_pktmbuf_trim (struct rte_mbuf *m, uint16_t len)

DPDK Basic Module Rte_mbuf detailed

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.