{"id":62677,"date":"2026-01-20T14:34:18","date_gmt":"2026-01-20T06:34:18","guid":{"rendered":"https:\/\/www.wsisp.com\/helps\/62677.html"},"modified":"2026-01-20T14:34:18","modified_gmt":"2026-01-20T06:34:18","slug":"dpdk%e4%b9%8barp%e5%9b%9e%e6%98%be%e4%bb%a3%e7%a0%81%e6%a1%88%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.wsisp.com\/helps\/62677.html","title":{"rendered":"dpdk\u4e4barp\u56de\u663e\u4ee3\u7801\u6848\u4f8b"},"content":{"rendered":"<h3><span style=\"color:#fe2c24\">\u4e00\u3001dpdk\u4e4barp\u56de\u663e\u4ee3\u7801\u6848\u4f8b<\/span><\/h3>\n<h4>1.arp.c<\/h4>\n<p>#include &lt;stdio.h&gt;<br \/>\n#include &lt;stdlib.h&gt;<br \/>\n#include &lt;arpa\/inet.h&gt;<br \/>\n#include &lt;netinet\/in.h&gt;<br \/>\n#include &lt;stdint.h&gt;<br \/>\n#include &lt;stddef.h&gt;<br \/>\n#include &lt;string.h&gt;<\/p>\n<p>\/\/ DPDK\u4f7f\u7528\u7684\u5934\u6587\u4ef6<br \/>\n#include &lt;rte_eal.h&gt;<br \/>\n#include &lt;rte_mbuf.h&gt;<br \/>\n#include &lt;rte_ethdev.h&gt;<br \/>\n#include &lt;rte_mempool.h&gt;<br \/>\n#include &lt;rte_ether.h&gt;<br \/>\n#include &lt;rte_ip.h&gt;<br \/>\n#include &lt;rte_udp.h&gt;<br \/>\n#include &lt;rte_arp.h&gt;<\/p>\n<p>#define ENABLE_ARP 1<\/p>\n<p>#define NUM_MBUFS (4096 &#8211; 1)<br \/>\n#define BURST_SIZE 32<br \/>\n#define UDP_PAYLOAD &#034;Hello DPDK! This is a response packet.&#034;<br \/>\n\/\/ \u65b0\u589e&#xff1a;\u5b9a\u4e49\u8d1f\u8f7d\u6253\u5370\u7f13\u51b2\u533a\u5927\u5c0f&#xff08;\u9632\u6b62\u8d85\u957f\u8d1f\u8f7d\u6ea2\u51fa&#xff09;<br \/>\n#define MAX_PAYLOAD_PRINT 1024<\/p>\n<p>int gDpdkPortId &#061; 0;<br \/>\n\/\/\u5168\u5c40\u53d8\u91cf&#xff1a;\u5b58\u50a8\u5f53\u524d\u7a97\u53e3\u7684MAC\u5730\u5740\u548cIP\u5730\u5740&#xff08;\u7528\u4e8earp\u54cd\u5e94&#xff09;<br \/>\nstatic struct rte_ether_addr gPortMacAddr;<br \/>\nstatic uint32_t gPortIpAddr &#061; 0;<\/p>\n<p>static const struct rte_eth_conf port_conf_default &#061; {<br \/>\n    .rxmode &#061; {},<br \/>\n    .txmode &#061; {}<br \/>\n};<\/p>\n<p>static void ng_init_port(struct rte_mempool *mbuf_pool){<br \/>\n    uint16_t nb_sys_ports &#061; rte_eth_dev_count_avail();<br \/>\n    if(nb_sys_ports &#061;&#061; 0){<br \/>\n        rte_exit(EXIT_FAILURE,&#034;No Support eth found\\\\n&#034;);<br \/>\n    }<\/p>\n<p>    struct rte_eth_dev_info dev_info;<br \/>\n    rte_eth_dev_info_get(gDpdkPortId,&amp;dev_info);<\/p>\n<p>    const int num_rx_queues &#061; 1;<br \/>\n    const int num_tx_queues &#061; 1;<br \/>\n    struct rte_eth_conf port_conf &#061; port_conf_default;<br \/>\n    rte_eth_dev_configure(gDpdkPortId,num_rx_queues,num_tx_queues,&amp;port_conf);<\/p>\n<p>    if(rte_eth_rx_queue_setup(gDpdkPortId,0,512,rte_eth_dev_socket_id(gDpdkPortId),NULL,mbuf_pool) &lt; 0){<br \/>\n        rte_exit(EXIT_FAILURE,&#034;Could not setup RX queue\\\\n&#034;);<br \/>\n    }<\/p>\n<p>    struct rte_eth_txconf txq_conf &#061; dev_info.default_txconf;<br \/>\n    if(rte_eth_tx_queue_setup(gDpdkPortId,0,512,rte_eth_dev_socket_id(gDpdkPortId),<br \/>\n       &amp;txq_conf) &lt; 0){<br \/>\n        rte_exit(EXIT_FAILURE,&#034;Could not setup TX queue\\\\n&#034;);<br \/>\n    }<\/p>\n<p>    if(rte_eth_dev_start(gDpdkPortId) &lt; 0){<br \/>\n        rte_exit(EXIT_FAILURE,&#034;Could not start Ethernet port\\\\n&#034;);<br \/>\n    }<\/p>\n<p>    \/\/ \u83b7\u53d6\u5f53\u524d\u7f51\u53e3\u7684MAC\u5730\u5740&#xff08;DPDK 19.08.2&#xff1a;rte_eth_macaddr_get\u4e3avoid\u7c7b\u578b&#xff0c;\u65e0\u8fd4\u56de\u503c&#xff09;<br \/>\n    rte_eth_macaddr_get(gDpdkPortId,&amp;gPortMacAddr);<\/p>\n<p>    \/\/\u8bbe\u7f6e\u5f53\u524d\u7f51\u53e3\u7684ip\u5730\u5740&#xff0c;\u4e5f\u5c31\u662f\u81ea\u5df1\u7f51\u5361ip<br \/>\n     gPortIpAddr &#061; inet_addr(&#034;192.168.3.100&#034;);<br \/>\n    \/\/\u542f\u7528\u6df7\u6742\u6a21\u5f0f<br \/>\n    rte_eth_promiscuous_enable(gDpdkPortId);<\/p>\n<p>    printf(&#034;Port %d initialized successfully, promiscuous mode enabled\\\\n&#034;, gDpdkPortId);<br \/>\n    printf(&#034;Port MAC address: %02x:%02x:%02x:%02x:%02x:%02x\\\\n&#034;,<br \/>\n           gPortMacAddr.addr_bytes[0], gPortMacAddr.addr_bytes[1],<br \/>\n           gPortMacAddr.addr_bytes[2], gPortMacAddr.addr_bytes[3],<br \/>\n           gPortMacAddr.addr_bytes[4], gPortMacAddr.addr_bytes[5]);<br \/>\n    printf(&#034;Port IP address: %s\\\\n&#034;, inet_ntoa(*(struct in_addr *)&amp;gPortIpAddr));<br \/>\n}<\/p>\n<p>static int create_eth_ip_udp(struct rte_mbuf *mbuf,<br \/>\n    uint8_t *dst_mac, uint8_t *src_mac,<br \/>\n    uint32_t src_ip, uint32_t dst_ip,<br \/>\n    uint16_t src_port, uint16_t dst_port,<br \/>\n    const uint8_t *payload, uint32_t payload_len){<br \/>\n    if(mbuf &#061;&#061; NULL || dst_mac &#061;&#061; NULL || src_mac &#061;&#061; NULL || payload &#061;&#061; NULL || payload_len &#061;&#061; 0){<br \/>\n        printf(&#034;Invalid parameters for packet construction\\\\n&#034;);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    struct rte_ether_hdr *ehdr &#061; NULL;<br \/>\n    struct rte_ipv4_hdr *iphdr &#061; NULL;<br \/>\n    struct rte_udp_hdr *udphdr &#061; NULL;<br \/>\n    uint8_t *udp_payload &#061; NULL;<\/p>\n<p>    const uint32_t eth_hdr_len &#061; sizeof(struct rte_ether_hdr);<br \/>\n    const uint32_t ip_hdr_len &#061; sizeof(struct rte_ipv4_hdr);<br \/>\n    const uint32_t udp_hdr_len &#061; sizeof(struct rte_udp_hdr);<br \/>\n    const uint32_t total_pkt_len &#061; eth_hdr_len &#043; ip_hdr_len &#043; udp_hdr_len &#043; payload_len;<\/p>\n<p>    ehdr &#061; rte_pktmbuf_mtod_offset(mbuf, struct rte_ether_hdr *, 0);<br \/>\n    iphdr &#061; rte_pktmbuf_mtod_offset(mbuf, struct rte_ipv4_hdr *, eth_hdr_len);<br \/>\n    udphdr &#061; rte_pktmbuf_mtod_offset(mbuf, struct rte_udp_hdr *, eth_hdr_len &#043; ip_hdr_len);<br \/>\n    udp_payload &#061; rte_pktmbuf_mtod_offset(mbuf, uint8_t *, eth_hdr_len &#043; ip_hdr_len &#043; udp_hdr_len);<\/p>\n<p>    memcpy((uint8_t *)&amp;ehdr-&gt;d_addr, dst_mac, RTE_ETHER_ADDR_LEN); \/\/ \u76ee\u6807MAC<br \/>\n    memcpy((uint8_t *)&amp;ehdr-&gt;s_addr, src_mac, RTE_ETHER_ADDR_LEN); \/\/ \u6e90MAC<br \/>\n    ehdr-&gt;ether_type &#061; rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4);<\/p>\n<p>    iphdr-&gt;version_ihl &#061; RTE_IPV4_VHL_DEF;<br \/>\n    iphdr-&gt;type_of_service &#061; 0;<br \/>\n    iphdr-&gt;total_length &#061; rte_cpu_to_be_16(ip_hdr_len &#043; udp_hdr_len &#043; payload_len);<br \/>\n    iphdr-&gt;packet_id &#061; 0;<br \/>\n    iphdr-&gt;fragment_offset &#061; 0;<br \/>\n    iphdr-&gt;time_to_live &#061; 64;<br \/>\n    iphdr-&gt;next_proto_id &#061; IPPROTO_UDP;<br \/>\n    iphdr-&gt;src_addr &#061; src_ip;<br \/>\n    iphdr-&gt;dst_addr &#061; dst_ip;<br \/>\n    iphdr-&gt;hdr_checksum &#061; 0;<br \/>\n    iphdr-&gt;hdr_checksum &#061; rte_ipv4_cksum(iphdr);<\/p>\n<p>    udphdr-&gt;src_port &#061; rte_cpu_to_be_16(src_port);<br \/>\n    udphdr-&gt;dst_port &#061; rte_cpu_to_be_16(dst_port);<br \/>\n    udphdr-&gt;dgram_len &#061; rte_cpu_to_be_16(udp_hdr_len &#043; payload_len);<br \/>\n    udphdr-&gt;dgram_cksum &#061; 0;<\/p>\n<p>    memcpy(udp_payload, payload, payload_len);<\/p>\n<p>    mbuf-&gt;pkt_len &#061; total_pkt_len;<br \/>\n    mbuf-&gt;data_len &#061; total_pkt_len;<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>static int send_udp_response(struct rte_mempool *mbuf_pool,<br \/>\n    struct rte_ether_hdr *recv_ehdr,<br \/>\n    struct rte_ipv4_hdr *recv_iphdr,<br \/>\n    struct rte_udp_hdr *recv_udphdr){<br \/>\n    struct rte_mbuf *tx_mbuf &#061; rte_pktmbuf_alloc(mbuf_pool);<br \/>\n    if(tx_mbuf &#061;&#061; NULL){<br \/>\n        printf(&#034;Failed to allocate mbuf for TX packet\\\\n&#034;);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    uint8_t src_mac[RTE_ETHER_ADDR_LEN] &#061; {0};<br \/>\n    uint8_t dst_mac[RTE_ETHER_ADDR_LEN] &#061; {0};<\/p>\n<p>    memcpy(src_mac, (uint8_t *)&amp;recv_ehdr-&gt;d_addr, RTE_ETHER_ADDR_LEN);<br \/>\n    memcpy(dst_mac, (uint8_t *)&amp;recv_ehdr-&gt;s_addr, RTE_ETHER_ADDR_LEN);<\/p>\n<p>    uint32_t src_ip &#061; recv_iphdr-&gt;dst_addr;<br \/>\n    uint32_t dst_ip &#061; recv_iphdr-&gt;src_addr;<\/p>\n<p>    uint16_t src_port &#061; ntohs(recv_udphdr-&gt;dst_port);<br \/>\n    uint16_t dst_port &#061; ntohs(recv_udphdr-&gt;src_port);<\/p>\n<p>    const uint8_t *payload &#061; (const uint8_t *)UDP_PAYLOAD;<br \/>\n    uint32_t payload_len &#061; strlen(UDP_PAYLOAD);<\/p>\n<p>    if(create_eth_ip_udp(tx_mbuf,dst_mac,src_mac,src_ip,dst_ip,src_port,dst_port,<br \/>\n        payload,payload_len) &lt; 0){<br \/>\n        rte_pktmbuf_free(tx_mbuf);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    struct rte_mbuf *tx_mbufs[1] &#061; {tx_mbuf};<br \/>\n    uint16_t num_sent &#061; rte_eth_tx_burst(gDpdkPortId,0,tx_mbufs,1);<\/p>\n<p>    if(num_sent &lt; 1){<br \/>\n        printf(&#034;Failed to send response packet, num_sent: %u\\\\n&#034;, num_sent);<br \/>\n        rte_pktmbuf_free(tx_mbuf);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    struct in_addr src_addr, dst_addr;<br \/>\n    src_addr.s_addr &#061; src_ip;<br \/>\n    dst_addr.s_addr &#061; dst_ip;<br \/>\n    printf(&#034;Response sent: %s:%d -&gt; %s:%d, payload: %s\\\\n&#034;,<br \/>\n        inet_ntoa(src_addr), src_port,<br \/>\n        inet_ntoa(dst_addr), dst_port,<br \/>\n        UDP_PAYLOAD);<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>#if ENABLE_ARP<\/p>\n<p>\/\/arp\u6570\u636e\u5185\u5b58\u7f13\u51b2\u533a\u8d77\u59cb\u6307\u9488  \u76ee\u7684MAC\u5730\u5740  \u53d1\u9001\u65b9ip  \u76ee\u7684ip ARP\u64cd\u4f5c\u7801&#xff08;arp\u5305\u7684\u7c7b\u578b&#xff09; \u53d1\u9001\u65b9\u7684MAC\u5730\u5740\u7ed3\u6784\u4f53\u6307\u9488<br \/>\nstatic int ng_encode_arp_pkt(uint8_t *msg, uint8_t *dst_mac, uint32_t sip, uint32_t dip,<br \/>\n                             uint16_t arp_opcode, struct rte_ether_addr *src_eth_addr) {<br \/>\n    if (msg &#061;&#061; NULL || dst_mac &#061;&#061; NULL || src_eth_addr &#061;&#061; NULL) {<br \/>\n        printf(&#034;Invalid parameters for ARP packet encoding\\\\n&#034;);<br \/>\n        return -1;<br \/>\n    }<\/p>\n<p>    struct rte_ether_hdr *ehdr &#061; (struct rte_ether_hdr *)msg;<br \/>\n    struct rte_arp_hdr *ahdr &#061; (struct rte_arp_hdr *)(msg &#043; sizeof(struct rte_ether_hdr));<\/p>\n<p>    \/\/1. \u586b\u5145\u4ee5\u592a\u7f51\u5934\u90e8<br \/>\n    memcpy((uint8_t *)&amp;ehdr-&gt;d_addr, dst_mac, RTE_ETHER_ADDR_LEN); \/\/ \u76ee\u6807MAC<br \/>\n    memcpy((uint8_t *)&amp;ehdr-&gt;s_addr, src_eth_addr, RTE_ETHER_ADDR_LEN); \/\/ \u6e90MAC&#xff08;\u7f51\u53e3MAC&#xff09;<br \/>\n    ehdr-&gt;ether_type &#061; rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP);<\/p>\n<p>    \/\/2. \u586b\u5145arp\u5934\u90e8&#xff08;\u7cbe\u51c6\u9002\u914d DPDK 19.08.2 \u771f\u5b9e\u5b57\u6bb5\u540d&#xff0c;\u6765\u81ea\u547d\u4ee4\u884c\u67e5\u8be2\u7ed3\u679c&#xff09;<br \/>\n    ahdr-&gt;arp_hardware &#061; rte_cpu_to_be_16(RTE_ARP_HRD_ETHER); \/\/ \u786c\u4ef6\u7c7b\u578b&#xff1a;\u4ee5\u592a\u7f51<br \/>\n    ahdr-&gt;arp_protocol &#061; rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4); \/\/ \u534f\u8bae\u7c7b\u578b&#xff1a;IPv4<br \/>\n    ahdr-&gt;arp_hlen &#061; RTE_ETHER_ADDR_LEN; \/\/ \u786c\u4ef6\u5730\u5740\u957f\u5ea6<br \/>\n    ahdr-&gt;arp_plen &#061; sizeof(uint32_t); \/\/ \u534f\u8bae\u5730\u5740\u957f\u5ea6<br \/>\n    ahdr-&gt;arp_opcode &#061; rte_cpu_to_be_16(arp_opcode); \/\/ ARP\u64cd\u4f5c\u7801&#xff1a;\u8bf7\u6c42\/\u54cd\u5e94<\/p>\n<p>    \/\/ 3. \u586b\u5145ARP\u6570\u636e&#xff08;\u53d1\u9001\u65b9\u4fe1\u606f&#xff09;<br \/>\n    memcpy((uint8_t *)&amp;ahdr-&gt;arp_data.arp_sha, src_eth_addr, RTE_ETHER_ADDR_LEN); \/\/ \u53d1\u9001\u65b9MAC<br \/>\n    ahdr-&gt;arp_data.arp_sip &#061; sip; \/\/ \u53d1\u9001\u65b9IP&#xff08;\u7f51\u53e3IP&#xff09;<\/p>\n<p>    \/\/ 4. \u586b\u5145ARP\u6570\u636e&#xff08;\u76ee\u6807\u65b9\u4fe1\u606f&#xff09;<br \/>\n    memcpy((uint8_t *)&amp;ahdr-&gt;arp_data.arp_tha, dst_mac, RTE_ETHER_ADDR_LEN); \/\/ \u76ee\u6807\u65b9MAC<br \/>\n    ahdr-&gt;arp_data.arp_tip &#061; dip; \/\/ \u76ee\u6807\u65b9IP<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<p>\/\/\u6784\u5efa\u5e76\u8fd4\u56deARP\u54cd\u5e94\u6570\u636e\u5305<br \/>\nstatic struct rte_mbuf *send_arp_response(struct rte_mempool *mbuf_pool,<br \/>\n                                          struct rte_arp_hdr *recv_ahdr,<br \/>\n                                          struct rte_ether_hdr *recv_ehdr) {<br \/>\n    if (mbuf_pool &#061;&#061; NULL || recv_ahdr &#061;&#061; NULL || recv_ehdr &#061;&#061; NULL) {<br \/>\n        printf(&#034;Invalid parameters for ARP response creation\\\\n&#034;);<br \/>\n        return NULL;<br \/>\n    }<\/p>\n<p>    \/\/1. \u8ba1\u7b97ARP\u6570\u636e\u5305\u7684\u603b\u957f\u5ea6<br \/>\n    const unsigned total_length &#061; sizeof(struct rte_ether_hdr) &#043; sizeof(struct rte_arp_hdr);<\/p>\n<p>    \/\/2. \u5206\u914dmbuf\u7f13\u51b2\u533a<br \/>\n    struct rte_mbuf *mbuf &#061; rte_pktmbuf_alloc(mbuf_pool);<br \/>\n    if (!mbuf) {<br \/>\n        printf(&#034;Failed to allocate mbuf for ARP response\\\\n&#034;);<br \/>\n        return NULL;<br \/>\n    }<\/p>\n<p>    \/\/ 3. \u521d\u59cb\u5316mbuf\u957f\u5ea6\u4fe1\u606f<br \/>\n    mbuf-&gt;pkt_len &#061; total_length;<br \/>\n    mbuf-&gt;data_len &#061; total_length;<\/p>\n<p>    \/\/4. \u83b7\u53d6mbuf\u6570\u636e\u6307\u9488<br \/>\n    uint8_t *pkt_data &#061; rte_pktmbuf_mtod(mbuf,uint8_t *);<br \/>\n    if (pkt_data &#061;&#061; NULL) {<br \/>\n        printf(&#034;Failed to get mbuf data pointer for ARP response\\\\n&#034;);<br \/>\n        rte_pktmbuf_free(mbuf);<br \/>\n        return NULL;<br \/>\n    }<\/p>\n<p>    \/\/ 5. \u63d0\u53d6\u63a5\u6536\u7684ARP\u8bf7\u6c42\u4e2d\u7684\u5173\u952e\u4fe1\u606f<br \/>\n    uint8_t dst_mac[RTE_ETHER_ADDR_LEN] &#061; {0};<br \/>\n    memcpy(dst_mac, (uint8_t *)&amp;recv_ehdr-&gt;s_addr, RTE_ETHER_ADDR_LEN); \/\/ \u76ee\u6807MAC&#xff1a;\u8bf7\u6c42\u65b9MAC<br \/>\n    uint32_t src_ip &#061; gPortIpAddr; \/\/ \u6e90IP&#xff1a;\u5f53\u524d\u7f51\u53e3IP<br \/>\n    uint32_t dst_ip &#061; recv_ahdr-&gt;arp_data.arp_sip; \/\/ \u76ee\u6807IP&#xff1a;\u8bf7\u6c42\u65b9IP<\/p>\n<p>    \/\/ 6. \u7f16\u7801ARP\u54cd\u5e94\u6570\u636e\u5305&#xff08;\u64cd\u4f5c\u7801&#xff1a;ARP\u54cd\u5e94&#xff09;<br \/>\n    if (ng_encode_arp_pkt(pkt_data, dst_mac, src_ip, dst_ip,<br \/>\n                          RTE_ARP_OP_REPLY, &amp;gPortMacAddr) &lt; 0) {<br \/>\n        printf(&#034;Failed to encode ARP response packet\\\\n&#034;);<br \/>\n        rte_pktmbuf_free(mbuf);<br \/>\n        return NULL;<br \/>\n    }<\/p>\n<p>    \/\/ 7. \u6253\u5370ARP\u54cd\u5e94\u4fe1\u606f<br \/>\n    struct in_addr src_addr, dst_addr;<br \/>\n    src_addr.s_addr &#061; src_ip;<br \/>\n    dst_addr.s_addr &#061; dst_ip;<br \/>\n    printf(&#034;ARP Response prepared: %s (%02x:%02x:%02x:%02x:%02x:%02x) -&gt; %s (%02x:%02x:%02x:%02x:%02x:%02x)\\\\n&#034;,<br \/>\n           inet_ntoa(src_addr), gPortMacAddr.addr_bytes[0], gPortMacAddr.addr_bytes[1],<br \/>\n           gPortMacAddr.addr_bytes[2], gPortMacAddr.addr_bytes[3],<br \/>\n           gPortMacAddr.addr_bytes[4], gPortMacAddr.addr_bytes[5],<br \/>\n           inet_ntoa(dst_addr), dst_mac[0], dst_mac[1], dst_mac[2],<br \/>\n           dst_mac[3], dst_mac[4], dst_mac[5]);<\/p>\n<p>    return mbuf;<br \/>\n}<\/p>\n<p>#endif<\/p>\n<p>int main(int argc,char *argv[]){<br \/>\n    int ret &#061; rte_eal_init(argc,argv);<br \/>\n    if(ret &lt; 0){<br \/>\n        fprintf(stderr, &#034;DPDK EAL\u521d\u59cb\u5316\u5931\u8d25&#xff01;\u9519\u8bef\u7801&#xff1a;%d\\\\n&#034;, ret);<br \/>\n        rte_exit(EXIT_FAILURE,&#034;EAL init error!&#034;);<br \/>\n    }<\/p>\n<p>    argc -&#061; ret;<br \/>\n    argv &#043;&#061; ret;<\/p>\n<p>    struct rte_mempool *mbuf_pool &#061; rte_pktmbuf_pool_create(&#034;mbuf_pool&#034;,NUM_MBUFS,0,0,<br \/>\n        RTE_MBUF_DEFAULT_BUF_SIZE,rte_socket_id());<br \/>\n    if(mbuf_pool &#061;&#061; NULL){<br \/>\n        rte_exit(EXIT_FAILURE,&#034;mbuf pool create error!&#034;);<br \/>\n    }<br \/>\n    printf(&#034;mbuf pool created successfully\\\\n&#034;);<\/p>\n<p>    ng_init_port(mbuf_pool);<\/p>\n<p>    while(1){<br \/>\n        struct rte_mbuf *mbufs[BURST_SIZE];<\/p>\n<p>        unsigned num_recv &#061; rte_eth_rx_burst(gDpdkPortId,0,mbufs,BURST_SIZE);<br \/>\n        if(num_recv &gt; BURST_SIZE){<br \/>\n            rte_exit(EXIT_FAILURE,&#034;Invalid number of received packets&#034;);<br \/>\n        }<\/p>\n<p>        for(unsigned int i &#061; 0;i &lt; num_recv;i&#043;&#043;){<br \/>\n            int need_process &#061; 1;<br \/>\n            struct rte_ether_hdr *ehdr &#061; NULL;<br \/>\n            struct rte_ipv4_hdr *iphdr &#061; NULL;<br \/>\n            struct rte_udp_hdr *udphdr &#061; NULL;<br \/>\n            uint8_t *recv_payload &#061; NULL; \/\/ \u65b0\u589e&#xff1a;\u63a5\u6536\u8d1f\u8f7d\u6307\u9488<br \/>\n            uint32_t recv_payload_len &#061; 0; \/\/ \u65b0\u589e&#xff1a;\u63a5\u6536\u8d1f\u8f7d\u957f\u5ea6<br \/>\n            char payload_buf[MAX_PAYLOAD_PRINT] &#061; {0}; \/\/ \u65b0\u589e&#xff1a;\u8d1f\u8f7d\u6253\u5370\u7f13\u51b2\u533a<\/p>\n<p>            ehdr &#061; rte_pktmbuf_mtod(mbufs[i],struct rte_ether_hdr *);<\/p>\n<p>#if ENABLE_ARP<br \/>\n            \/\/ \u8865\u5168&#xff1a;\u89e3\u6790ARP\u6570\u636e\u5305&#xff08;\u4ec5\u5904\u7406ARP\u8bf7\u6c42&#xff0c;\u7cbe\u51c6\u9002\u914d DPDK 19.08.2 \u771f\u5b9e\u5b57\u6bb5&#xff09;<br \/>\n            if(ehdr-&gt;ether_type &#061;&#061; rte_cpu_to_be_16(RTE_ETHER_TYPE_ARP)){<br \/>\n                struct rte_arp_hdr *ahdr &#061; rte_pktmbuf_mtod_offset(mbufs[i],struct rte_arp_hdr *,<br \/>\n                    sizeof(struct rte_ether_hdr));<\/p>\n<p>                \/\/ \u9a8c\u8bc1ARP\u7c7b\u578b\u548c\u64cd\u4f5c\u7801&#xff08;\u4ec5\u5904\u7406IPv4 ARP\u8bf7\u6c42&#xff0c;\u4f7f\u7528\u67e5\u8be2\u5230\u7684\u6b63\u786e\u5b57\u6bb5&#xff09;<br \/>\n                if (rte_cpu_to_be_16(RTE_ARP_HRD_ETHER) &#061;&#061; ahdr-&gt;arp_hardware &amp;&amp;<br \/>\n                    rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4) &#061;&#061; ahdr-&gt;arp_protocol &amp;&amp;<br \/>\n                    rte_cpu_to_be_16(RTE_ARP_OP_REQUEST) &#061;&#061; ahdr-&gt;arp_opcode &amp;&amp;<br \/>\n                    ahdr-&gt;arp_data.arp_tip &#061;&#061; gPortIpAddr) { \/\/ \u76ee\u6807IP\u662f\u5f53\u524d\u7f51\u53e3IP<\/p>\n<p>                    \/\/\u6784\u5efaARP\u54cd\u5e94\u6570\u636e\u5305<br \/>\n                    struct rte_mbuf *arp_mbuf &#061; send_arp_response(mbuf_pool,ahdr,ehdr);<br \/>\n                    if(arp_mbuf !&#061; NULL){<br \/>\n                        \/\/ \u53d1\u9001ARP\u54cd\u5e94&#xff08;\u6b63\u786eAPI&#xff1a;rte_eth_tx_burst&#xff09;<br \/>\n                        uint16_t num_sent &#061; rte_eth_tx_burst(gDpdkPortId, 0, &amp;arp_mbuf, 1);<br \/>\n                        if (num_sent &lt; 1) {<br \/>\n                            printf(&#034;Failed to send ARP response, num_sent: %u\\\\n&#034;, num_sent);<br \/>\n                            rte_pktmbuf_free(arp_mbuf);<br \/>\n                        }<br \/>\n                    }<br \/>\n                }<\/p>\n<p>                need_process &#061; 0; \/\/ ARP\u5305\u5904\u7406\u5b8c\u6210&#xff0c;\u65e0\u9700\u540e\u7eedUDP\u5904\u7406<br \/>\n            }<br \/>\n#endif<br \/>\n            if(ehdr-&gt;ether_type !&#061; rte_cpu_to_be_16(RTE_ETHER_TYPE_IPV4)){<br \/>\n                need_process &#061; 0;<br \/>\n            }<\/p>\n<p>            if(need_process){<br \/>\n                iphdr &#061; rte_pktmbuf_mtod_offset(mbufs[i], struct rte_ipv4_hdr *,<br \/>\n                    sizeof(struct rte_ether_hdr));<br \/>\n                if(iphdr &#061;&#061; NULL || iphdr-&gt;next_proto_id !&#061; IPPROTO_UDP){<br \/>\n                    need_process &#061; 0;<br \/>\n                }<br \/>\n            }<\/p>\n<p>            if(need_process){<br \/>\n                udphdr &#061; rte_pktmbuf_mtod_offset(mbufs[i], struct rte_udp_hdr *,<br \/>\n                    sizeof(struct rte_ether_hdr) &#043; sizeof(struct rte_ipv4_hdr));<br \/>\n                if(udphdr &#061;&#061; NULL){<br \/>\n                    need_process &#061; 0;<br \/>\n                } else {<br \/>\n                    \/\/ \u65b0\u589e&#xff1a;\u8ba1\u7b97UDP\u8d1f\u8f7d\u8d77\u59cb\u5730\u5740\u548c\u957f\u5ea6<br \/>\n                    const uint32_t hdr_total_len &#061; sizeof(struct rte_ether_hdr) &#043;<br \/>\n                                                    sizeof(struct rte_ipv4_hdr) &#043;<br \/>\n                                                    sizeof(struct rte_udp_hdr);<br \/>\n                    \/\/ 1. \u83b7\u53d6\u8d1f\u8f7d\u8d77\u59cb\u5730\u5740<br \/>\n                    recv_payload &#061; rte_pktmbuf_mtod_offset(mbufs[i], uint8_t *, hdr_total_len);<br \/>\n                    \/\/ 2. \u8ba1\u7b97\u8d1f\u8f7d\u957f\u5ea6&#xff08;\u7f51\u7edc\u5b57\u8282\u5e8f\u8f6c\u4e3b\u673a\u5b57\u8282\u5e8f\u540e&#xff0c;\u51cf\u53bbUDP\u5934\u957f\u5ea6&#xff09;<br \/>\n                    recv_payload_len &#061; ntohs(udphdr-&gt;dgram_len) &#8211; sizeof(struct rte_udp_hdr);<\/p>\n<p>                    \/\/ 3. \u5b89\u5168\u62f7\u8d1d\u8d1f\u8f7d\u5230\u6253\u5370\u7f13\u51b2\u533a&#xff08;\u9632\u6b62\u8d85\u957f\u8d1f\u8f7d\u6ea2\u51fa&#xff09;<br \/>\n                    if(recv_payload_len &gt; 0 &amp;&amp; recv_payload !&#061; NULL){<br \/>\n                        uint32_t copy_len &#061; (recv_payload_len &gt; MAX_PAYLOAD_PRINT &#8211; 1) ?<br \/>\n                                            (MAX_PAYLOAD_PRINT &#8211; 1) : recv_payload_len;<br \/>\n                        memcpy(payload_buf, recv_payload, copy_len);<br \/>\n                        payload_buf[copy_len] &#061; &#039;\\\\0&#039;; \/\/ \u5b57\u7b26\u4e32\u7ed3\u5c3e\u7b26&#xff0c;\u4fdd\u8bc1\u6b63\u5e38\u6253\u5370<br \/>\n                    }<\/p>\n<p>                    \/\/ \u65b0\u589e&#xff1a;\u6253\u5370\u63a5\u6536\u5230\u7684UDP\u8d1f\u8f7d<br \/>\n                    struct in_addr src_addr, dst_addr;<br \/>\n                    src_addr.s_addr &#061; iphdr-&gt;src_addr;<br \/>\n                    dst_addr.s_addr &#061; iphdr-&gt;dst_addr;<br \/>\n                    printf(&#034;Received UDP packet: %s:%d -&gt; %s:%d\\\\n&#034;,<br \/>\n                        inet_ntoa(src_addr), ntohs(udphdr-&gt;src_port),<br \/>\n                        inet_ntoa(dst_addr), ntohs(udphdr-&gt;dst_port));<br \/>\n                    printf(&#034;Received UDP payload length: %u bytes\\\\n&#034;, recv_payload_len);<br \/>\n                    printf(&#034;Received UDP payload content: %s\\\\n&#034;,<br \/>\n                        (recv_payload_len &gt; 0) ? payload_buf : &#034;Empty payload&#034;);<br \/>\n                    printf(&#034;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;-\\\\n&#034;);<\/p>\n<p>                    send_udp_response(mbuf_pool, ehdr, iphdr, udphdr);<br \/>\n                }<br \/>\n            }<\/p>\n<p>            rte_pktmbuf_free(mbufs[i]);<br \/>\n        }<br \/>\n    }<\/p>\n<p>    return 0;<br \/>\n}<\/p>\n<h4>2. Makefile<\/h4>\n<p># SPDX-License-Identifier: BSD-3-Clause<br \/>\n# Copyright(c) 2010-2014 Intel Corporation<\/p>\n<p># binary name<br \/>\nAPP &#061; arp<\/p>\n<p># all source are stored in SRCS-y<br \/>\nSRCS-y :&#061; arp.c<\/p>\n<p># Build using pkg-config variables if possible<br \/>\nifeq ($(shell pkg-config &#8211;exists libdpdk &amp;&amp; echo 0),0)<\/p>\n<p>all: shared<br \/>\n.PHONY: shared static<br \/>\nshared: build\/$(APP)-shared<br \/>\nln -sf $(APP)-shared build\/$(APP)<br \/>\nstatic: build\/$(APP)-static<br \/>\nln -sf $(APP)-static build\/$(APP)<\/p>\n<p>PKGCONF&#061;pkg-config &#8211;define-prefix<\/p>\n<p>PC_FILE :&#061; $(shell $(PKGCONF) &#8211;path libdpdk)<br \/>\nCFLAGS &#043;&#061; -O3 $(shell $(PKGCONF) &#8211;cflags libdpdk)<br \/>\nCFLAGS &#043;&#061; -DALLOW_EXPERIMENTAL_API<br \/>\nLDFLAGS_SHARED &#061; $(shell $(PKGCONF) &#8211;libs libdpdk)<br \/>\nLDFLAGS_STATIC &#061; -Wl,-Bstatic $(shell $(PKGCONF) &#8211;static &#8211;libs libdpdk)<\/p>\n<p>build\/$(APP)-shared: $(SRCS-y) Makefile $(PC_FILE) | build<br \/>\n$(CC) $(CFLAGS) $(SRCS-y) -o $&#064; $(LDFLAGS) $(LDFLAGS_SHARED)<\/p>\n<p>build\/$(APP)-static: $(SRCS-y) Makefile $(PC_FILE) | build<br \/>\n$(CC) $(CFLAGS) $(SRCS-y) -o $&#064; $(LDFLAGS) $(LDFLAGS_STATIC)<\/p>\n<p>build:<br \/>\n&#064;mkdir -p $&#064;<\/p>\n<p>.PHONY: clean<br \/>\nclean:<br \/>\nrm -f build\/$(APP) build\/$(APP)-static build\/$(APP)-shared<br \/>\ntest -d build &amp;&amp; rmdir -p build || true<\/p>\n<p>else # Build using legacy build system<\/p>\n<p>ifeq ($(RTE_SDK),)<br \/>\n$(error &#034;Please define RTE_SDK environment variable&#034;)<br \/>\nendif<\/p>\n<p># Default target, detect a build directory, by looking for a path with a .config<br \/>\nRTE_TARGET ?&#061; $(notdir $(abspath $(dir $(firstword $(wildcard $(RTE_SDK)\/*\/.config)))))<\/p>\n<p>include $(RTE_SDK)\/mk\/rte.vars.mk<\/p>\n<p>ifneq ($(CONFIG_RTE_EXEC_ENV_LINUX),y)<br \/>\n$(error This application can only operate in a linux environment, \\\\<br \/>\nplease change the definition of the RTE_TARGET environment variable)<br \/>\nendif<\/p>\n<p>CFLAGS &#043;&#061; -O3<br \/>\nCFLAGS &#043;&#061; -DALLOW_EXPERIMENTAL_API<br \/>\nCFLAGS &#043;&#061; $(WERROR_FLAGS)<\/p>\n<p>include $(RTE_SDK)\/mk\/rte.extapp.mk<br \/>\nendif <\/p>\n<h4>3.\u4ee3\u7801\u8bb2\u89e3<\/h4>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u662f\u4e00\u4e2a\u652f\u6301 ARP \u534f\u8bae\u54cd\u5e94 &#043; UDP \u6570\u636e\u5305\u56de\u663e\u7684\u8f7b\u91cf\u7ea7 DPDK \u7f51\u7edc\u7a0b\u5e8f&#xff0c;\u76f8\u6bd4\u4e0a\u4e00\u7248\u65b0\u589e\u4e86\u5173\u952e\u7684 ARP \u534f\u8bae\u5904\u7406\u80fd\u529b&#xff1a;<\/p>\n<li>\u521d\u59cb\u5316 DPDK \u73af\u5883\u548c\u7f51\u5361\u540e&#xff0c;\u7a0b\u5e8f\u4ee5\u8f6e\u8be2\u65b9\u5f0f\u6279\u91cf\u63a5\u6536\u7f51\u5361\u6570\u636e\u5305&#xff1b;<\/li>\n<li>\u4f18\u5148\u8bc6\u522b ARP \u6570\u636e\u5305&#xff1a;\u82e5\u6536\u5230\u9488\u5bf9\u672c\u673a IP \u7684 ARP \u8bf7\u6c42&#xff0c;\u6784\u9020\u5e76\u53d1\u9001 ARP \u54cd\u5e94&#xff08;\u8fd4\u56de\u672c\u673a MAC-IP \u6620\u5c04&#xff09;&#xff1b;<\/li>\n<li>\u5176\u6b21\u5904\u7406 UDP \u6570\u636e\u5305&#xff1a;\u89e3\u6790 IPv4 UDP \u5305\u7684\u6e90 \/ \u76ee\u5730\u5740\u3001\u7aef\u53e3\u548c\u8d1f\u8f7d&#xff0c;\u6784\u9020\u53cd\u5411\u7684 UDP \u54cd\u5e94\u5305\u5e76\u53d1\u9001&#xff1b;<\/li>\n<li>\u5168\u7a0b\u91c7\u7528 DPDK \u7684 \u201c\u6279\u91cf\u6536\u53d1\u5305 &#043; \u9884\u5206\u914d mbuf \u5185\u5b58\u6c60\u201d \u6a21\u5f0f&#xff0c;\u4fdd\u8bc1\u9ad8\u6027\u80fd&#xff0c;\u9002\u914d DPDK 19.08.2 \u7248\u672c\u3002<\/li>\n<p>main()  \/\/ \u7a0b\u5e8f\u5165\u53e3<br \/>\n\u251c\u2500\u2500 rte_eal_init()          \/\/ 1. \u521d\u59cb\u5316DPDK EAL\u73af\u5883<br \/>\n\u251c\u2500\u2500 rte_pktmbuf_pool_create()\/\/ 2. \u521b\u5efambuf\u5185\u5b58\u6c60&#xff08;\u5b58\u50a8\u6570\u636e\u5305&#xff09;<br \/>\n\u251c\u2500\u2500 ng_init_port()          \/\/ 3. \u521d\u59cb\u5316\u7f51\u5361\u7aef\u53e3&#xff08;\u65b0\u589e\u83b7\u53d6\u672c\u673aMAC\/IP&#xff09;<br \/>\n\u2502   \u251c\u2500\u2500 rte_eth_dev_count_avail()   \/\/ \u68c0\u6d4b\u53ef\u7528\u7f51\u5361\u6570\u91cf<br \/>\n\u2502   \u251c\u2500\u2500 rte_eth_dev_info_get()      \/\/ \u83b7\u53d6\u7f51\u5361\u8bbe\u5907\u4fe1\u606f<br \/>\n\u2502   \u251c\u2500\u2500 rte_eth_dev_configure()     \/\/ \u914d\u7f6e\u7f51\u5361RX\/TX\u961f\u5217\u6570<br \/>\n\u2502   \u251c\u2500\u2500 rte_eth_rx_queue_setup()    \/\/ \u521d\u59cb\u5316RX\u961f\u5217<br \/>\n\u2502   \u251c\u2500\u2500 rte_eth_tx_queue_setup()    \/\/ \u521d\u59cb\u5316TX\u961f\u5217<br \/>\n\u2502   \u251c\u2500\u2500 rte_eth_dev_start()         \/\/ \u542f\u52a8\u7f51\u5361<br \/>\n\u2502   \u251c\u2500\u2500 rte_eth_macaddr_get()       \/\/ \u65b0\u589e&#xff1a;\u83b7\u53d6\u7f51\u5361MAC\u5730\u5740<br \/>\n\u2502   \u2514\u2500\u2500 rte_eth_promiscuous_enable() \/\/ \u5f00\u542f\u6df7\u6742\u6a21\u5f0f<br \/>\n\u2514\u2500\u2500 \u65e0\u9650\u5faa\u73af&#xff08;\u6536\u5305\u5904\u7406&#xff09;<br \/>\n    \u251c\u2500\u2500 rte_eth_rx_burst()          \/\/ \u6279\u91cf\u63a5\u6536\u6570\u636e\u5305<br \/>\n    \u251c\u2500\u2500 \u6570\u636e\u5305\u7c7b\u578b\u5224\u65ad&#xff08;ARP\/UDP\u5206\u652f&#xff09;<br \/>\n    \u2502   \u251c\u2500\u2500 ARP\u5206\u652f&#xff08;\u65b0\u589e&#xff09;<br \/>\n    \u2502   \u2502   \u251c\u2500\u2500 \u89e3\u6790ARP\u5934&#xff08;rte_arp_hdr&#xff09;<br \/>\n    \u2502   \u2502   \u251c\u2500\u2500 send_arp_response() \/\/ \u6784\u9020ARP\u54cd\u5e94\u5305<br \/>\n    \u2502   \u2502   \u2502   \u251c\u2500\u2500 rte_pktmbuf_alloc()     \/\/ \u5206\u914dmbuf<br \/>\n    \u2502   \u2502   \u2502   \u251c\u2500\u2500 ng_encode_arp_pkt()     \/\/ \u7f16\u7801ARP\u6570\u636e\u5305<br \/>\n    \u2502   \u2502   \u2502   \u2502   \u251c\u2500\u2500 rte_cpu_to_be_16()  \/\/ \u4e3b\u673a\u5e8f\u8f6c\u7f51\u7edc\u5927\u7aef\u5e8f<br \/>\n    \u2502   \u2502   \u2502   \u2502   \u2514\u2500\u2500 memcpy()            \/\/ \u586b\u5145MAC\/IP\u5b57\u6bb5<br \/>\n    \u2502   \u2502   \u2502   \u2514\u2500\u2500 rte_pktmbuf_mtod()      \/\/ \u83b7\u53d6mbuf\u6570\u636e\u6307\u9488<br \/>\n    \u2502   \u2502   \u251c\u2500\u2500 rte_eth_tx_burst()          \/\/ \u53d1\u9001ARP\u54cd\u5e94\u5305<br \/>\n    \u2502   \u2502   \u2514\u2500\u2500 rte_pktmbuf_free()          \/\/ \u53d1\u9001\u5931\u8d25\u65f6\u91ca\u653embuf<br \/>\n    \u2502   \u2514\u2500\u2500 UDP\u5206\u652f&#xff08;\u539f\u6709&#xff09;<br \/>\n    \u2502       \u251c\u2500\u2500 \u89e3\u6790\u4ee5\u592a\u7f51\/IP\/UDP\u5934<br \/>\n    \u2502       \u251c\u2500\u2500 send_udp_response()         \/\/ \u6784\u9020UDP\u54cd\u5e94\u5305<br \/>\n    \u2502       \u2502   \u251c\u2500\u2500 rte_pktmbuf_alloc()     \/\/ \u5206\u914dmbuf<br \/>\n    \u2502       \u2502   \u251c\u2500\u2500 create_eth_ip_udp()     \/\/ \u7f16\u7801UDP\u6570\u636e\u5305<br \/>\n    \u2502       \u2502   \u2502   \u251c\u2500\u2500 rte_pktmbuf_mtod_offset() \/\/ \u83b7\u53d6\u6307\u5b9a\u504f\u79fb\u6307\u9488<br \/>\n    \u2502       \u2502   \u2502   \u251c\u2500\u2500 rte_cpu_to_be_16()  \/\/ \u4e3b\u673a\u5e8f\u8f6c\u7f51\u7edc\u5927\u7aef\u5e8f<br \/>\n    \u2502       \u2502   \u2502   \u2514\u2500\u2500 rte_ipv4_cksum()    \/\/ \u8ba1\u7b97IP\u6821\u9a8c\u548c<br \/>\n    \u2502       \u2502   \u2514\u2500\u2500 rte_eth_tx_burst()      \/\/ \u53d1\u9001UDP\u54cd\u5e94\u5305<br \/>\n    \u2502       \u2514\u2500\u2500 rte_pktmbuf_free()          \/\/ \u53d1\u9001\u5931\u8d25\u65f6\u91ca\u653embuf<br \/>\n    \u2514\u2500\u2500 rte_pktmbuf_free()          \/\/ \u91ca\u653e\u63a5\u6536\u7684mbuf <\/p>\n<h5>\u9636\u6bb5 1&#xff1a;\u521d\u59cb\u5316\u9636\u6bb5&#xff08;\u7a0b\u5e8f\u542f\u52a8\u65f6&#xff09;<\/h5>\n<p>\u8fd9\u4e00\u9636\u6bb5\u5b8c\u6210 DPDK \u73af\u5883\u3001\u5185\u5b58\u6c60\u3001\u7f51\u5361\u7684\u521d\u59cb\u5316&#xff0c;\u5e76\u4fdd\u5b58\u672c\u673a MAC\/IP&#xff08;\u4e3a ARP \u54cd\u5e94\u505a\u51c6\u5907&#xff09;&#xff1a;<\/p>\n<li>EAL \u73af\u5883\u521d\u59cb\u5316\u8c03\u7528rte_eal_init()\u5b8c\u6210 DPDK \u5e95\u5c42\u73af\u5883\u521d\u59cb\u5316&#xff08;CPU \u7ed1\u5b9a\u3001\u5185\u5b58\u5212\u5206\u3001\u8bbe\u5907\u63a2\u6d4b\u7b49&#xff09;&#xff0c;\u5931\u8d25\u5219\u76f4\u63a5\u9000\u51fa\u3002<\/li>\n<li>mbuf \u5185\u5b58\u6c60\u521b\u5efa\u8c03\u7528rte_pktmbuf_pool_create()\u521b\u5efa 4095 \u4e2a mbuf \u7684\u5185\u5b58\u6c60&#xff0c;\u9884\u5206\u914d\u6570\u636e\u5305\u5b58\u50a8\u7ed3\u6784&#xff0c;\u907f\u514d\u8fd0\u884c\u65f6\u52a8\u6001\u5185\u5b58\u5206\u914d\u3002<\/li>\n<li>\u7f51\u5361\u7aef\u53e3\u521d\u59cb\u5316&#xff08;ng_init_port \u51fd\u6570&#xff09;\n<ul>\n<li>\u68c0\u6d4b\u53ef\u7528\u7f51\u5361\u6570\u91cf&#xff0c;\u65e0\u5219\u9000\u51fa&#xff1b;<\/li>\n<li>\u83b7\u53d6\u7f51\u5361\u8bbe\u5907\u4fe1\u606f&#xff0c;\u914d\u7f6e 1 \u4e2a RX \u961f\u5217 &#043; 1 \u4e2a TX \u961f\u5217&#xff1b;<\/li>\n<li>\u521d\u59cb\u5316 RX\/TX \u961f\u5217\u5e76\u542f\u52a8\u7f51\u5361&#xff0c;\u5f00\u542f\u6df7\u6742\u6a21\u5f0f&#xff1b;<\/li>\n<li>\u65b0\u589e\u5173\u952e\u6b65\u9aa4&#xff1a;\n<ul>\n<li>\u8c03\u7528rte_eth_macaddr_get()\u83b7\u53d6\u7f51\u5361\u7684 MAC \u5730\u5740&#xff0c;\u5b58\u5165\u5168\u5c40\u53d8\u91cfgPortMacAddr&#xff1b;<\/li>\n<li>\u624b\u52a8\u8bbe\u7f6e\u7f51\u5361 IP \u5730\u5740&#xff08;192.168.3.100&#xff09;&#xff0c;\u5b58\u5165\u5168\u5c40\u53d8\u91cfgPortIpAddr&#xff1b;<\/li>\n<li>\u6253\u5370\u672c\u673a MAC\/IP&#xff0c;\u65b9\u4fbf\u8c03\u8bd5\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<h5>\u9636\u6bb5 2&#xff1a;\u8fd0\u884c\u9636\u6bb5&#xff08;\u65e0\u9650\u5faa\u73af\u6536\u53d1\u5305&#xff09;<\/h5>\n<p>\u521d\u59cb\u5316\u5b8c\u6210\u540e&#xff0c;\u7a0b\u5e8f\u8fdb\u5165\u65e0\u9650\u5faa\u73af&#xff0c;\u4f18\u5148\u5904\u7406 ARP \u5305&#xff0c;\u518d\u5904\u7406 UDP \u5305&#xff0c;\u6d41\u7a0b\u5982\u4e0b&#xff1a;<\/p>\n<li>\u6279\u91cf\u6536\u5305\u8c03\u7528rte_eth_rx_burst()\u6279\u91cf\u63a5\u6536\u6570\u636e\u5305&#xff08;\u6bcf\u6b21\u6700\u591a 32 \u4e2a&#xff09;&#xff0c;\u8fd4\u56de\u5b9e\u9645\u63a5\u6536\u7684\u5305\u6570\u91cf\u3002<\/li>\n<li>\u9010\u4e2a\u5904\u7406\u6570\u636e\u5305\u5bf9\u6bcf\u4e2a\u63a5\u6536\u7684\u6570\u636e\u5305&#xff0c;\u6309 \u201cARP\u2192UDP\u201d \u7684\u4f18\u5148\u7ea7\u5904\u7406&#xff1a;\n<ul>\n<li>\u6b65\u9aa4 1&#xff1a;\u5224\u65ad\u662f\u5426\u4e3a ARP \u5305&#xff08;\u65b0\u589e&#xff09;\u89e3\u6790\u4ee5\u592a\u7f51\u5934&#xff08;rte_ether_hdr&#xff09;&#xff0c;\u82e5ether_type\u4e3aRTE_ETHER_TYPE_ARP&#xff08;ARP \u534f\u8bae&#xff09;&#xff1a;\u2460 \u89e3\u6790 ARP \u5934&#xff08;rte_arp_hdr&#xff09;&#xff0c;\u9a8c\u8bc1\u662f\u5426\u4e3a \u201cIPv4 ARP \u8bf7\u6c42\u201d \u4e14\u76ee\u6807 IP \u662f\u672c\u673a IP&#xff1b;\u2461 \u8c03\u7528send_arp_response()\u6784\u9020 ARP \u54cd\u5e94\u5305&#xff1a;\n<ul>\n<li>\u5206\u914d\u65b0\u7684 mbuf&#xff0c;\u8bbe\u7f6e\u5305\u957f\u5ea6&#xff1b;<\/li>\n<li>\u8c03\u7528ng_encode_arp_pkt()\u586b\u5145\u4ee5\u592a\u7f51\u5934&#xff08;\u6e90 MAC &#061; \u672c\u673a\u3001\u76ee\u7684 MAC &#061; \u8bf7\u6c42\u65b9&#xff09;\u548c ARP \u5934&#xff08;\u64cd\u4f5c\u7801 &#061; \u54cd\u5e94\u3001\u53d1\u9001\u65b9 IP\/MAC &#061; \u672c\u673a\u3001\u76ee\u6807\u65b9 IP\/MAC &#061; \u8bf7\u6c42\u65b9&#xff09;&#xff1b;\u2462 \u8c03\u7528rte_eth_tx_burst()\u53d1\u9001 ARP \u54cd\u5e94\u5305&#xff0c;\u53d1\u9001\u5931\u8d25\u5219\u91ca\u653e mbuf&#xff1b;\u2463 \u6807\u8bb0need_process&#061;0&#xff0c;\u8df3\u8fc7\u540e\u7eed UDP \u5904\u7406\u3002<\/li>\n<\/ul>\n<\/li>\n<li>\u6b65\u9aa4 2&#xff1a;\u5224\u65ad\u662f\u5426\u4e3a UDP \u5305&#xff08;\u539f\u6709&#xff09;\u82e5\u4e0d\u662f ARP \u5305&#xff0c;\u68c0\u67e5\u662f\u5426\u4e3a IPv4 UDP \u5305&#xff1a;\u2460 \u89e3\u6790\u4ee5\u592a\u7f51\u5934&#xff0c;\u9a8c\u8bc1\u662f\u5426\u4e3a IPv4 \u534f\u8bae&#xff1b;\u2461 \u89e3\u6790 IPv4 \u5934&#xff0c;\u9a8c\u8bc1\u4e0a\u5c42\u534f\u8bae\u662f\u5426\u4e3a UDP&#xff1b;\u2462 \u89e3\u6790 UDP \u5934&#xff0c;\u8ba1\u7b97\u5e76\u6253\u5370 UDP \u8d1f\u8f7d&#xff08;\u5b89\u5168\u62f7\u8d1d\u5230\u7f13\u51b2\u533a&#xff0c;\u9632\u6b62\u6ea2\u51fa&#xff09;&#xff1b;\u2463 \u8c03\u7528send_udp_response()\u6784\u9020\u53cd\u5411 UDP \u54cd\u5e94\u5305&#xff08;\u6e90 \/ \u76ee MAC\/IP\/ \u7aef\u53e3\u4e92\u6362&#xff09;&#xff0c;\u53d1\u9001\u540e\u6253\u5370\u65e5\u5fd7&#xff1b;<\/li>\n<li>\u6b65\u9aa4 3&#xff1a;\u91ca\u653e mbuf\u65e0\u8bba\u662f\u5426\u5904\u7406\u6210\u529f&#xff0c;\u90fd\u8c03\u7528rte_pktmbuf_free()\u91ca\u653e\u63a5\u6536\u7684 mbuf&#xff0c;\u5f52\u8fd8\u5230\u5185\u5b58\u6c60\u590d\u7528\u3002<\/li>\n<\/ul>\n<\/li>\n<h4>4.\u7f16\u8bd1\u8fd0\u884c<\/h4>\n<p><img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"784\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260120063414-696f21e6bae57.png\" width=\"1077\" \/><\/p>\n<p>\u6210\u529f\u6536\u5230\u6570\u636e&#xff1a; <img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1095\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260120063415-696f21e731ab3.png\" width=\"2227\" \/><\/p>\n<p>\u540c\u65f6\u5bbf\u4e3b\u673aarp\u8868\u81ea\u52a8\u66f4\u65b0&#xff1a; <img loading=\"lazy\" decoding=\"async\" alt=\"\" height=\"1212\" src=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260120063416-696f21e8b09de.png\" width=\"748\" \/><\/p>\n<\/p>\n<p>0voice \u00b7 GitHub<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001dpdk\u4e4barp\u56de\u663e\u4ee3\u7801\u6848\u4f8b<br \/>\n1.arp.c<br \/>\n#include<br \/>\n#include<br \/>\n#include<br \/>\n#include<br \/>\n#include<br \/>\n#include<br \/>\n#include \/\/ DPDK\u4f7f\u7528\u7684\u5934\u6587\u4ef6<br \/>\n#include<br \/>\n#include<br \/>\n#include &lt;r<\/p>\n","protected":false},"author":2,"featured_media":62674,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[332,58,103,78,191],"topic":[],"class_list":["post-62677","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-server","tag-ip","tag-linux","tag-ubuntu","tag-78","tag-191"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v20.3 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>dpdk\u4e4barp\u56de\u663e\u4ee3\u7801\u6848\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.wsisp.com\/helps\/62677.html\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"dpdk\u4e4barp\u56de\u663e\u4ee3\u7801\u6848\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"og:description\" content=\"\u4e00\u3001dpdk\u4e4barp\u56de\u663e\u4ee3\u7801\u6848\u4f8b 1.arp.c #include #include #include #include #include #include #include \/\/ DPDK\u4f7f\u7528\u7684\u5934\u6587\u4ef6 #include #include #include &lt;r\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.wsisp.com\/helps\/62677.html\" \/>\n<meta property=\"og:site_name\" content=\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\" \/>\n<meta property=\"article:published_time\" content=\"2026-01-20T06:34:18+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260120063414-696f21e6bae57.png\" \/>\n<meta name=\"author\" content=\"admin\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4\" \/>\n\t<meta name=\"twitter:data2\" content=\"15 \u5206\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/62677.html\",\"url\":\"https:\/\/www.wsisp.com\/helps\/62677.html\",\"name\":\"dpdk\u4e4barp\u56de\u663e\u4ee3\u7801\u6848\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"isPartOf\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\"},\"datePublished\":\"2026-01-20T06:34:18+00:00\",\"dateModified\":\"2026-01-20T06:34:18+00:00\",\"author\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.wsisp.com\/helps\/62677.html#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.wsisp.com\/helps\/62677.html\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/62677.html#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/www.wsisp.com\/helps\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"dpdk\u4e4barp\u56de\u663e\u4ee3\u7801\u6848\u4f8b\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#website\",\"url\":\"https:\/\/www.wsisp.com\/helps\/\",\"name\":\"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3\",\"description\":\"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"zh-Hans\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41\",\"name\":\"admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"contentUrl\":\"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery\",\"caption\":\"admin\"},\"sameAs\":[\"http:\/\/wp.wsisp.com\"],\"url\":\"https:\/\/www.wsisp.com\/helps\/author\/admin\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"dpdk\u4e4barp\u56de\u663e\u4ee3\u7801\u6848\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.wsisp.com\/helps\/62677.html","og_locale":"zh_CN","og_type":"article","og_title":"dpdk\u4e4barp\u56de\u663e\u4ee3\u7801\u6848\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","og_description":"\u4e00\u3001dpdk\u4e4barp\u56de\u663e\u4ee3\u7801\u6848\u4f8b 1.arp.c #include #include #include #include #include #include #include \/\/ DPDK\u4f7f\u7528\u7684\u5934\u6587\u4ef6 #include #include #include &lt;r","og_url":"https:\/\/www.wsisp.com\/helps\/62677.html","og_site_name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","article_published_time":"2026-01-20T06:34:18+00:00","og_image":[{"url":"https:\/\/www.wsisp.com\/helps\/wp-content\/uploads\/2026\/01\/20260120063414-696f21e6bae57.png"}],"author":"admin","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"admin","\u9884\u8ba1\u9605\u8bfb\u65f6\u95f4":"15 \u5206"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.wsisp.com\/helps\/62677.html","url":"https:\/\/www.wsisp.com\/helps\/62677.html","name":"dpdk\u4e4barp\u56de\u663e\u4ee3\u7801\u6848\u4f8b - \u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","isPartOf":{"@id":"https:\/\/www.wsisp.com\/helps\/#website"},"datePublished":"2026-01-20T06:34:18+00:00","dateModified":"2026-01-20T06:34:18+00:00","author":{"@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41"},"breadcrumb":{"@id":"https:\/\/www.wsisp.com\/helps\/62677.html#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.wsisp.com\/helps\/62677.html"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.wsisp.com\/helps\/62677.html#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/www.wsisp.com\/helps"},{"@type":"ListItem","position":2,"name":"dpdk\u4e4barp\u56de\u663e\u4ee3\u7801\u6848\u4f8b"}]},{"@type":"WebSite","@id":"https:\/\/www.wsisp.com\/helps\/#website","url":"https:\/\/www.wsisp.com\/helps\/","name":"\u7f51\u7855\u4e92\u8054\u5e2e\u52a9\u4e2d\u5fc3","description":"\u9999\u6e2f\u670d\u52a1\u5668_\u9999\u6e2f\u4e91\u670d\u52a1\u5668\u8d44\u8baf_\u670d\u52a1\u5668\u5e2e\u52a9\u6587\u6863_\u670d\u52a1\u5668\u6559\u7a0b","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.wsisp.com\/helps\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"zh-Hans"},{"@type":"Person","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/358e386c577a3ab51c4493330a20ad41","name":"admin","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/www.wsisp.com\/helps\/#\/schema\/person\/image\/","url":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","contentUrl":"https:\/\/gravatar.wp-china-yes.net\/avatar\/?s=96&d=mystery","caption":"admin"},"sameAs":["http:\/\/wp.wsisp.com"],"url":"https:\/\/www.wsisp.com\/helps\/author\/admin"}]}},"_links":{"self":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/62677","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/comments?post=62677"}],"version-history":[{"count":0,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/posts\/62677\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media\/62674"}],"wp:attachment":[{"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/media?parent=62677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/categories?post=62677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/tags?post=62677"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.wsisp.com\/helps\/wp-json\/wp\/v2\/topic?post=62677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}