2 * Copyright (C) 2018 jingle YANG. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS''AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1. Pcap-dpdk provides libpcap the ability to use DPDK with the device name as dpdk:{portid}, such as dpdk:0.
32 2. DPDK is a set of libraries and drivers for fast packet processing. (https://www.dpdk.org/)
33 3. The testprogs/capturetest provides 6.4Gbps/800,000 pps on Intel 10-Gigabit X540-AT2 with DPDK 18.11.
36 1. DPDK support will be on if DPDK is available. Please set DIR for --with-dpdk[=DIR] with ./configure or -DDPDK_DIR[=DIR] with cmake if DPDK is installed manually.
37 2. Only support link libdpdk.so dynamically, because the libdpdk.a will not work correctly.
38 3. Only support read operation, and packet injection has not been supported yet.
41 1. Compile DPDK as shared library and install.(https://github.com/DPDK/dpdk.git)
43 You shall modify the file $RTE_SDK/$RTE_TARGET/.config and set:
44 CONFIG_RTE_BUILD_SHARED_LIB=y
45 By the following command:
46 sed -i 's/CONFIG_RTE_BUILD_SHARED_LIB=n/CONFIG_RTE_BUILD_SHARED_LIB=y/' $RTE_SDK/$RTE_TARGET/.config
48 2. Launch l2fwd that is one of DPDK examples correctly, and get device information.
50 You shall learn how to bind nic with DPDK-compatible driver by $RTE_SDK/usertools/dpdk-devbind.py, such as igb_uio.
51 And enable hugepages by dpdk-setup.sh
53 Then launch the l2fwd with dynamic driver support. For example:
54 $RTE_SDK/examples/l2fwd/$RTE_TARGET/l2fwd -dlibrte_pmd_e1000.so -dlibrte_pmd_ixgbe.so -dlibrte_mempool_ring.so -- -p 0x1
56 3. Compile libpcap with dpdk options.
58 If DPDK has not been found automatically, you shall export DPDK environment variable which are used for compiling DPDK. And then pass $RTE_SDK/$RTE_TARGET to --with-dpdk or -DDPDK_DIR
60 export RTE_SDK={your DPDK base directory}
61 export RTE_TARGET={your target name}
65 ./configure --with-dpdk=$RTE_SDK/$RTE_TARGET && make -s all && make -s testprogs && make install
69 mkdir -p build && cd build && cmake -DDPDK_DIR=$RTE_SDK/$RTE_TARGET ../ && make -s all && make -s testprogs && make install
71 4. Link your own program with libpcap, and use DPDK with the device name as dpdk:{portid}, such as dpdk:0.
72 And you shall set DPDK configure options by environment variable DPDK_CFG
73 For example, the testprogs/capturetest could be launched by:
75 env DPDK_CFG="--log-level=debug -l0 -dlibrte_pmd_e1000.so -dlibrte_pmd_ixgbe.so -dlibrte_mempool_ring.so" ./capturetest -i dpdk:0
88 #include <limits.h> /* for INT_MAX */
93 //header for calling dpdk
94 #include <rte_config.h>
95 #include <rte_common.h>
96 #include <rte_errno.h>
98 #include <rte_malloc.h>
99 #include <rte_memory.h>
101 #include <rte_launch.h>
102 #include <rte_atomic.h>
103 #include <rte_cycles.h>
104 #include <rte_lcore.h>
105 #include <rte_per_lcore.h>
106 #include <rte_branch_prediction.h>
107 #include <rte_interrupts.h>
108 #include <rte_random.h>
109 #include <rte_debug.h>
110 #include <rte_ether.h>
111 #include <rte_ethdev.h>
112 #include <rte_mempool.h>
113 #include <rte_mbuf.h>
116 #include "pcap-int.h"
117 #include "pcap-dpdk.h"
120 * Deal with API changes that break source compatibility.
123 #ifdef HAVE_STRUCT_RTE_ETHER_ADDR
124 #define ETHER_ADDR_TYPE struct rte_ether_addr
126 #define ETHER_ADDR_TYPE struct ether_addr
129 #define DPDK_DEF_LOG_LEV RTE_LOG_ERR
131 // This is set to 0 if we haven't initialized DPDK yet, 1 if we've
132 // successfully initialized it, a negative value, which is the negative
133 // of the rte_errno from rte_eal_init(), if we tried to initialize it
136 static int is_dpdk_pre_inited
=0;
137 #define DPDK_LIB_NAME "libpcap_dpdk"
138 #define DPDK_DESC "Data Plane Development Kit (DPDK) Interface"
139 #define DPDK_ERR_PERM_MSG "permission denied, DPDK needs root permission"
140 #define DPDK_ARGC_MAX 64
141 #define DPDK_CFG_MAX_LEN 1024
142 #define DPDK_DEV_NAME_MAX 32
143 #define DPDK_DEV_DESC_MAX 512
144 #define DPDK_CFG_ENV_NAME "DPDK_CFG"
145 #define DPDK_DEF_MIN_SLEEP_MS 1
146 static char dpdk_cfg_buf
[DPDK_CFG_MAX_LEN
];
147 #define DPDK_MAC_ADDR_SIZE 32
148 #define DPDK_DEF_MAC_ADDR "00:00:00:00:00:00"
149 #define DPDK_PCI_ADDR_SIZE 16
150 #define DPDK_DEF_CFG "--log-level=error -l0 -dlibrte_pmd_e1000.so -dlibrte_pmd_ixgbe.so -dlibrte_mempool_ring.so"
151 #define DPDK_PREFIX "dpdk:"
152 #define DPDK_PORTID_MAX 65535U
153 #define MBUF_POOL_NAME "mbuf_pool"
154 #define DPDK_TX_BUF_NAME "tx_buffer"
155 //The number of elements in the mbuf pool.
156 #define DPDK_NB_MBUFS 8192U
157 #define MEMPOOL_CACHE_SIZE 256
158 #define MAX_PKT_BURST 32
159 // Configurable number of RX/TX ring descriptors
160 #define RTE_TEST_RX_DESC_DEFAULT 1024
161 #define RTE_TEST_TX_DESC_DEFAULT 1024
163 static uint16_t nb_rxd
= RTE_TEST_RX_DESC_DEFAULT
;
164 static uint16_t nb_txd
= RTE_TEST_TX_DESC_DEFAULT
;
166 #ifdef RTE_ETHER_MAX_JUMBO_FRAME_LEN
167 #define RTE_ETH_PCAP_SNAPLEN RTE_ETHER_MAX_JUMBO_FRAME_LEN
169 #define RTE_ETH_PCAP_SNAPLEN ETHER_MAX_JUMBO_FRAME_LEN
172 static struct rte_eth_dev_tx_buffer
*tx_buffer
;
174 struct dpdk_ts_helper
{
175 struct timeval start_time
;
176 uint64_t start_cycles
;
181 uint16_t portid
; // portid of DPDK
182 int must_clear_promisc
;
185 struct timeval required_select_timeout
;
186 struct timeval prev_ts
;
187 struct rte_eth_stats prev_stats
;
188 struct timeval curr_ts
;
189 struct rte_eth_stats curr_stats
;
192 struct rte_mempool
* pktmbuf_pool
;
193 struct dpdk_ts_helper ts_helper
;
194 ETHER_ADDR_TYPE eth_addr
;
195 char mac_addr
[DPDK_MAC_ADDR_SIZE
];
196 char pci_addr
[DPDK_PCI_ADDR_SIZE
];
197 unsigned char pcap_tmp_buf
[RTE_ETH_PCAP_SNAPLEN
];
200 static struct rte_eth_conf port_conf
= {
205 .mq_mode
= ETH_MQ_TX_NONE
,
209 static void dpdk_fmt_errmsg_for_rte_errno(char *, size_t, int,
210 PCAP_FORMAT_STRING(const char *), ...) PCAP_PRINTFLIKE(4, 5);
213 * Generate an error message based on a format, arguments, and an
214 * rte_errno, with a message for the rte_errno after the formatted output.
216 static void dpdk_fmt_errmsg_for_rte_errno(char *errbuf
, size_t errbuflen
,
217 int errnum
, const char *fmt
, ...)
222 size_t errbuflen_remaining
;
225 vsnprintf(errbuf
, errbuflen
, fmt
, ap
);
227 msglen
= strlen(errbuf
);
230 * Do we have enough space to append ": "?
231 * Including the terminating '\0', that's 3 bytes.
233 if (msglen
+ 3 > errbuflen
) {
234 /* No - just give them what we've produced. */
238 errbuflen_remaining
= errbuflen
- msglen
;
243 errbuflen_remaining
-= 2;
246 * Now append the string for the error code.
247 * rte_strerror() is thread-safe, at least as of dpdk 18.11,
248 * unlike strerror() - it uses strerror_r() rather than strerror()
249 * for UN*X errno values, and prints to what I assume is a per-thread
250 * buffer (based on the "PER_LCORE" in "RTE_DEFINE_PER_LCORE" used
251 * to declare the buffers statically) for DPDK errors.
253 snprintf(p
, errbuflen_remaining
, "%s", rte_strerror(errnum
));
256 static int dpdk_init_timer(struct pcap_dpdk
*pd
){
257 gettimeofday(&(pd
->ts_helper
.start_time
),NULL
);
258 pd
->ts_helper
.start_cycles
= rte_get_timer_cycles();
259 pd
->ts_helper
.hz
= rte_get_timer_hz();
260 if (pd
->ts_helper
.hz
== 0){
265 static inline void calculate_timestamp(struct dpdk_ts_helper
*helper
,struct timeval
*ts
)
269 struct timeval cur_time
;
270 cycles
= rte_get_timer_cycles() - helper
->start_cycles
;
271 cur_time
.tv_sec
= (time_t)(cycles
/helper
->hz
);
272 cur_time
.tv_usec
= (suseconds_t
)((cycles
%helper
->hz
)*1e6
/helper
->hz
);
273 timeradd(&(helper
->start_time
), &cur_time
, ts
);
276 static uint32_t dpdk_gather_data(unsigned char *data
, uint32_t len
, struct rte_mbuf
*mbuf
)
278 uint32_t total_len
= 0;
279 while (mbuf
&& (total_len
+mbuf
->data_len
) < len
){
280 rte_memcpy(data
+total_len
, rte_pktmbuf_mtod(mbuf
,void *),mbuf
->data_len
);
281 total_len
+=mbuf
->data_len
;
288 static int dpdk_read_with_timeout(pcap_t
*p
, struct rte_mbuf
**pkts_burst
, const uint16_t burst_cnt
){
289 struct pcap_dpdk
*pd
= (struct pcap_dpdk
*)(p
->priv
);
291 int timeout_ms
= p
->opt
.timeout
;
294 // In non-blocking mode, just read once, no matter how many packets are captured.
295 nb_rx
= (int)rte_eth_rx_burst(pd
->portid
, 0, pkts_burst
, burst_cnt
);
297 // In blocking mode, read many times until packets are captured or timeout or break_loop is set.
298 // if timeout_ms == 0, it may be blocked forever.
299 while (timeout_ms
== 0 || sleep_ms
< timeout_ms
){
300 nb_rx
= (int)rte_eth_rx_burst(pd
->portid
, 0, pkts_burst
, burst_cnt
);
301 if (nb_rx
){ // got packets within timeout_ms
303 }else{ // no packet arrives at this round.
307 // sleep for a very short while.
308 // block sleep is the only choice, since usleep() will impact performance dramatically.
309 rte_delay_us_block(DPDK_DEF_MIN_SLEEP_MS
*1000);
310 sleep_ms
+= DPDK_DEF_MIN_SLEEP_MS
;
317 static int pcap_dpdk_dispatch(pcap_t
*p
, int max_cnt
, pcap_handler cb
, u_char
*cb_arg
)
319 struct pcap_dpdk
*pd
= (struct pcap_dpdk
*)(p
->priv
);
322 struct rte_mbuf
*pkts_burst
[MAX_PKT_BURST
];
324 struct pcap_pkthdr pcap_header
;
325 // In DPDK, pkt_len is sum of lengths for all segments. And data_len is for one segment
326 uint32_t pkt_len
= 0;
330 unsigned int gather_len
=0;
332 u_char
*large_buffer
=NULL
;
333 int timeout_ms
= p
->opt
.timeout
;
336 * This can conceivably process more than INT_MAX packets,
337 * which would overflow the packet count, causing it either
338 * to look like a negative number, and thus cause us to
339 * return a value that looks like an error, or overflow
340 * back into positive territory, and thus cause us to
341 * return a too-low count.
343 * Therefore, if the packet count is unlimited, we clip
344 * it at INT_MAX; this routine is not expected to
345 * process packets indefinitely, so that's not an issue.
347 if (PACKET_COUNT_IS_UNLIMITED(max_cnt
))
350 if (max_cnt
< MAX_PKT_BURST
){
353 burst_cnt
= MAX_PKT_BURST
;
356 while( pkt_cnt
< max_cnt
){
359 return PCAP_ERROR_BREAK
;
361 // read once in non-blocking mode, or try many times waiting for timeout_ms.
362 // if timeout_ms == 0, it will be blocked until one packet arrives or break_loop is set.
363 nb_rx
= dpdk_read_with_timeout(p
, pkts_burst
, burst_cnt
);
366 RTE_LOG(DEBUG
, USER1
, "dpdk: no packets available in non-blocking mode.\n");
369 RTE_LOG(DEBUG
, USER1
, "dpdk: no packets available and break_loop is set in blocking mode.\n");
371 return PCAP_ERROR_BREAK
;
374 RTE_LOG(DEBUG
, USER1
, "dpdk: no packets available for timeout %d ms in blocking mode.\n", timeout_ms
);
376 // break if dpdk reads 0 packet, no matter in blocking(timeout) or non-blocking mode.
380 for ( i
= 0; i
< nb_rx
; i
++) {
382 calculate_timestamp(&(pd
->ts_helper
),&(pcap_header
.ts
));
383 pkt_len
= rte_pktmbuf_pkt_len(m
);
384 // caplen = min(pkt_len, p->snapshot);
385 // caplen will not be changed, no matter how long the rte_pktmbuf
386 caplen
= pkt_len
< (uint32_t)p
->snapshot
? pkt_len
: (uint32_t)p
->snapshot
;
387 pcap_header
.caplen
= caplen
;
388 pcap_header
.len
= pkt_len
;
390 rte_prefetch0(rte_pktmbuf_mtod(m
, void *));
394 bp
= rte_pktmbuf_mtod(m
, u_char
*);
396 // use fast buffer pcap_tmp_buf if pkt_len is small, no need to call malloc and free
397 if ( pkt_len
<= RTE_ETH_PCAP_SNAPLEN
)
399 gather_len
= dpdk_gather_data(pd
->pcap_tmp_buf
, RTE_ETH_PCAP_SNAPLEN
, m
);
400 bp
= pd
->pcap_tmp_buf
;
402 // need call free later
403 large_buffer
= (u_char
*)malloc(caplen
*sizeof(u_char
));
404 gather_len
= dpdk_gather_data(large_buffer
, caplen
, m
);
410 if (p
->fcode
.bf_insns
==NULL
|| pcapint_filter(p
->fcode
.bf_insns
, bp
, pcap_header
.len
, pcap_header
.caplen
)){
411 cb(cb_arg
, &pcap_header
, bp
);
427 static int pcap_dpdk_inject(pcap_t
*p
, const void *buf _U_
, int size _U_
)
429 //not implemented yet
430 pcapint_strlcpy(p
->errbuf
,
431 "dpdk error: Inject function has not been implemented yet",
436 static void pcap_dpdk_close(pcap_t
*p
)
438 struct pcap_dpdk
*pd
= p
->priv
;
443 if (pd
->must_clear_promisc
)
445 rte_eth_promiscuous_disable(pd
->portid
);
447 rte_eth_dev_stop(pd
->portid
);
448 rte_eth_dev_close(pd
->portid
);
449 pcapint_cleanup_live_common(p
);
452 static void nic_stats_display(struct pcap_dpdk
*pd
)
454 uint16_t portid
= pd
->portid
;
455 struct rte_eth_stats stats
;
456 rte_eth_stats_get(portid
, &stats
);
457 RTE_LOG(INFO
,USER1
, "portid:%d, RX-packets: %-10"PRIu64
" RX-errors: %-10"PRIu64
458 " RX-bytes: %-10"PRIu64
" RX-Imissed: %-10"PRIu64
"\n", portid
, stats
.ipackets
, stats
.ierrors
,
459 stats
.ibytes
,stats
.imissed
);
460 RTE_LOG(INFO
,USER1
, "portid:%d, RX-PPS: %-10"PRIu64
" RX-Mbps: %.2lf\n", portid
, pd
->pps
, pd
->bps
/1e6f
);
463 static int pcap_dpdk_stats(pcap_t
*p
, struct pcap_stat
*ps
)
465 struct pcap_dpdk
*pd
= p
->priv
;
466 calculate_timestamp(&(pd
->ts_helper
), &(pd
->curr_ts
));
467 rte_eth_stats_get(pd
->portid
,&(pd
->curr_stats
));
469 ps
->ps_recv
= pd
->curr_stats
.ipackets
;
470 ps
->ps_drop
= pd
->curr_stats
.ierrors
;
471 ps
->ps_drop
+= pd
->bpf_drop
;
472 ps
->ps_ifdrop
= pd
->curr_stats
.imissed
;
474 uint64_t delta_pkt
= pd
->curr_stats
.ipackets
- pd
->prev_stats
.ipackets
;
475 struct timeval delta_tm
;
476 timersub(&(pd
->curr_ts
),&(pd
->prev_ts
), &delta_tm
);
477 uint64_t delta_usec
= delta_tm
.tv_sec
*1e6
+delta_tm
.tv_usec
;
478 uint64_t delta_bit
= (pd
->curr_stats
.ibytes
-pd
->prev_stats
.ibytes
)*8;
479 RTE_LOG(DEBUG
, USER1
, "delta_usec: %-10"PRIu64
" delta_pkt: %-10"PRIu64
" delta_bit: %-10"PRIu64
"\n", delta_usec
, delta_pkt
, delta_bit
);
480 pd
->pps
= (uint64_t)(delta_pkt
*1e6f
/delta_usec
);
481 pd
->bps
= (uint64_t)(delta_bit
*1e6f
/delta_usec
);
482 nic_stats_display(pd
);
483 pd
->prev_stats
= pd
->curr_stats
;
484 pd
->prev_ts
= pd
->curr_ts
;
488 static int pcap_dpdk_setnonblock(pcap_t
*p
, int nonblock
){
489 struct pcap_dpdk
*pd
= (struct pcap_dpdk
*)(p
->priv
);
490 pd
->nonblock
= nonblock
;
494 static int pcap_dpdk_getnonblock(pcap_t
*p
){
495 struct pcap_dpdk
*pd
= (struct pcap_dpdk
*)(p
->priv
);
498 static int check_link_status(uint16_t portid
, struct rte_eth_link
*plink
)
500 // wait up to 9 seconds to get link status
501 rte_eth_link_get(portid
, plink
);
502 return plink
->link_status
== ETH_LINK_UP
;
504 static void eth_addr_str(ETHER_ADDR_TYPE
*addrp
, char* mac_str
, int len
)
508 snprintf(mac_str
, len
-1, DPDK_DEF_MAC_ADDR
);
511 for (int i
=0; i
<6; i
++)
519 snprintf(mac_str
+offset
, len
-1-offset
, "%02X",addrp
->addr_bytes
[i
]);
522 snprintf(mac_str
+offset
, len
-1-offset
, ":%02X", addrp
->addr_bytes
[i
]);
528 // return portid by device name, otherwise return -1
529 static uint16_t portid_by_device(char * device
)
531 uint16_t ret
= DPDK_PORTID_MAX
;
532 size_t len
= strlen(device
);
533 size_t prefix_len
= strlen(DPDK_PREFIX
);
534 unsigned long ret_ul
= 0L;
536 if (len
<=prefix_len
|| strncmp(device
, DPDK_PREFIX
, prefix_len
)) // check prefix dpdk:
540 //check all chars are digital
541 for (int i
=prefix_len
; device
[i
]; i
++){
542 if (device
[i
]<'0' || device
[i
]>'9'){
546 ret_ul
= strtoul(&(device
[prefix_len
]), &pEnd
, 10);
547 if (pEnd
== &(device
[prefix_len
]) || *pEnd
!= '\0'){
550 // too large for portid
551 if (ret_ul
>= DPDK_PORTID_MAX
){
554 ret
= (uint16_t)ret_ul
;
558 static int parse_dpdk_cfg(char* dpdk_cfg
,char** dargv
)
561 memset(dargv
,0,sizeof(dargv
[0])*DPDK_ARGC_MAX
);
562 //current process name
565 RTE_LOG(INFO
, USER1
,"dpdk cfg: %s\n",dpdk_cfg
);
566 // find first non space char
567 // The last opt is NULL
568 for (i
=0;dpdk_cfg
[i
] && cnt
<DPDK_ARGC_MAX
-1;i
++){
569 if (skip_space
&& dpdk_cfg
[i
]!=' '){ // not space
570 skip_space
=!skip_space
; // skip normal char
571 dargv
[cnt
++] = dpdk_cfg
+i
;
573 if (!skip_space
&& dpdk_cfg
[i
]==' '){ // find a space
574 dpdk_cfg
[i
]=0x00; // end of this opt
575 skip_space
=!skip_space
; // skip space char
587 // 0 if "the EAL cannot initialize on this system", which we treat as
588 // meaning "DPDK isn't available";
590 // a PCAP_ERROR_ code for other errors.
592 // If eaccess_not_fatal is non-zero, treat "a permissions issue" the way
593 // we treat "the EAL cannot initialize on this system". We use that
594 // when trying to find DPDK devices, as we don't want to fail to return
595 // *any* devices just because we can't support DPDK; when we're trying
596 // to open a device, we need to return a permissions error in that case.
597 static int dpdk_pre_init(char * ebuf
, int eaccess_not_fatal
)
600 char *dargv
[DPDK_ARGC_MAX
];
601 char *ptr_dpdk_cfg
= NULL
;
604 if (is_dpdk_pre_inited
!= 0)
606 // already inited; did that succeed?
607 if (is_dpdk_pre_inited
< 0)
619 ptr_dpdk_cfg
= getenv(DPDK_CFG_ENV_NAME
);
620 // set default log level to debug
621 rte_log_set_global_level(DPDK_DEF_LOG_LEV
);
622 if (ptr_dpdk_cfg
== NULL
)
624 RTE_LOG(INFO
,USER1
,"env $DPDK_CFG is unset, so using default: %s\n",DPDK_DEF_CFG
);
625 ptr_dpdk_cfg
= DPDK_DEF_CFG
;
627 memset(dpdk_cfg_buf
,0,sizeof(dpdk_cfg_buf
));
628 snprintf(dpdk_cfg_buf
,DPDK_CFG_MAX_LEN
-1,"%s %s",DPDK_LIB_NAME
,ptr_dpdk_cfg
);
629 dargv_cnt
= parse_dpdk_cfg(dpdk_cfg_buf
,dargv
);
630 ret
= rte_eal_init(dargv_cnt
,dargv
);
633 // Indicate that we've called rte_eal_init() by setting
634 // is_dpdk_pre_inited to the negative of the error code,
635 // and process the error.
636 is_dpdk_pre_inited
= -rte_errno
;
639 // init succeeded, so we do not need to do it again later.
640 is_dpdk_pre_inited
= 1;
644 switch (-is_dpdk_pre_inited
)
647 // This "indicates a permissions issue.".
648 RTE_LOG(ERR
, USER1
, "%s\n", DPDK_ERR_PERM_MSG
);
649 // If we were told to treat this as just meaning
650 // DPDK isn't available, do so.
651 if (eaccess_not_fatal
)
653 // Otherwise report a fatal error.
654 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
655 "DPDK requires that it run as root");
656 return PCAP_ERROR_PERM_DENIED
;
659 // This "indicates either a bus or system
660 // resource was not available, setup may
661 // be attempted again."
662 // There's no such error in pcap, so I'm
663 // not sure what we should do here.
664 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
665 "Bus or system resource was not available");
669 // This "indicates that the rte_eal_init
670 // function has already been called, and
671 // cannot be called again."
672 // That's not an error; set the "we've
673 // been here before" flag and return
675 is_dpdk_pre_inited
= 1;
679 // This "indicates the tailq configuration
680 // name was not found in memory configuration."
681 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
682 "The tailq configuration name was not found in the memory configuration");
686 // This "indicates invalid parameters were
687 // passed as argv/argc." Those came from
688 // the configuration file.
689 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
690 "The configuration file has invalid parameters");
694 // This "indicates failure likely caused by
695 // an out-of-memory condition."
696 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
701 // This "indicates memory setup issues."
702 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
703 "An error occurred setting up memory");
707 // This "indicates that the EAL cannot
708 // initialize on this system." We treat
709 // that as meaning DPDK isn't available
710 // on this machine, rather than as a
711 // fatal error, and let our caller decide
712 // whether that's a fatal error (if trying
713 // to activate a DPDK device) or not (if
714 // trying to enumerate devices).
718 // This "indicates that the PCI bus is
719 // either not present, or is not readable
720 // by the eal." Does "the PCI bus is not
721 // present" mean "this machine has no PCI
722 // bus", which strikes me as a "not available"
723 // case? If so, should "is not readable by
724 // the EAL" also something we should treat
725 // as a "not available" case? If not, we
726 // can't distinguish between the two, so
728 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
729 "PCI bus is not present or not readable by the EAL");
733 // This "indicates that a service core
734 // failed to launch successfully."
735 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
736 "A service core failed to launch successfully");
741 // That's not in the list of errors in
742 // the documentation; let it be reported
745 dpdk_fmt_errmsg_for_rte_errno(ebuf
,
746 PCAP_ERRBUF_SIZE
, -is_dpdk_pre_inited
,
747 "dpdk error: dpdk_pre_init failed");
754 static int pcap_dpdk_activate(pcap_t
*p
)
756 struct pcap_dpdk
*pd
= p
->priv
;
758 int ret
= PCAP_ERROR
;
760 uint16_t portid
= DPDK_PORTID_MAX
;
761 unsigned nb_mbufs
= DPDK_NB_MBUFS
;
762 struct rte_eth_rxconf rxq_conf
;
763 struct rte_eth_txconf txq_conf
;
764 struct rte_eth_conf local_port_conf
= port_conf
;
765 struct rte_eth_dev_info dev_info
;
767 struct rte_eth_link link
;
769 //init EAL; fail if we have insufficient permission
770 char dpdk_pre_init_errbuf
[PCAP_ERRBUF_SIZE
];
771 ret
= dpdk_pre_init(dpdk_pre_init_errbuf
, 0);
774 // This returns a negative value on an error.
775 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
776 "Can't open device %s: %s",
777 p
->opt
.device
, dpdk_pre_init_errbuf
);
778 // ret is set to the correct error
783 // This means DPDK isn't available on this machine.
784 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
785 "Can't open device %s: DPDK is not available on this machine",
787 return PCAP_ERROR_NO_SUCH_DEVICE
;
790 ret
= dpdk_init_timer(pd
);
793 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
794 "dpdk error: Init timer is zero with device %s",
800 nb_ports
= rte_eth_dev_count_avail();
803 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
804 "dpdk error: No Ethernet ports");
809 portid
= portid_by_device(p
->opt
.device
);
810 if (portid
== DPDK_PORTID_MAX
){
811 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
812 "dpdk error: portid is invalid. device %s",
814 ret
= PCAP_ERROR_NO_SUCH_DEVICE
;
820 if (p
->snapshot
<= 0 || p
->snapshot
> MAXIMUM_SNAPLEN
)
822 p
->snapshot
= MAXIMUM_SNAPLEN
;
824 // create the mbuf pool
825 pd
->pktmbuf_pool
= rte_pktmbuf_pool_create(MBUF_POOL_NAME
, nb_mbufs
,
826 MEMPOOL_CACHE_SIZE
, 0, RTE_MBUF_DEFAULT_BUF_SIZE
,
828 if (pd
->pktmbuf_pool
== NULL
)
830 dpdk_fmt_errmsg_for_rte_errno(p
->errbuf
,
831 PCAP_ERRBUF_SIZE
, rte_errno
,
832 "dpdk error: Cannot init mbuf pool");
837 rte_eth_dev_info_get(portid
, &dev_info
);
838 if (dev_info
.tx_offload_capa
& DEV_TX_OFFLOAD_MBUF_FAST_FREE
)
840 local_port_conf
.txmode
.offloads
|=DEV_TX_OFFLOAD_MBUF_FAST_FREE
;
842 // only support 1 queue
843 ret
= rte_eth_dev_configure(portid
, 1, 1, &local_port_conf
);
846 dpdk_fmt_errmsg_for_rte_errno(p
->errbuf
,
847 PCAP_ERRBUF_SIZE
, -ret
,
848 "dpdk error: Cannot configure device: port=%u",
854 ret
= rte_eth_dev_adjust_nb_rx_tx_desc(portid
, &nb_rxd
, &nb_txd
);
857 dpdk_fmt_errmsg_for_rte_errno(p
->errbuf
,
858 PCAP_ERRBUF_SIZE
, -ret
,
859 "dpdk error: Cannot adjust number of descriptors: port=%u",
865 rte_eth_macaddr_get(portid
, &(pd
->eth_addr
));
866 eth_addr_str(&(pd
->eth_addr
), pd
->mac_addr
, DPDK_MAC_ADDR_SIZE
-1);
869 rxq_conf
= dev_info
.default_rxconf
;
870 rxq_conf
.offloads
= local_port_conf
.rxmode
.offloads
;
871 ret
= rte_eth_rx_queue_setup(portid
, 0, nb_rxd
,
872 rte_eth_dev_socket_id(portid
),
877 dpdk_fmt_errmsg_for_rte_errno(p
->errbuf
,
878 PCAP_ERRBUF_SIZE
, -ret
,
879 "dpdk error: rte_eth_rx_queue_setup:port=%u",
886 txq_conf
= dev_info
.default_txconf
;
887 txq_conf
.offloads
= local_port_conf
.txmode
.offloads
;
888 ret
= rte_eth_tx_queue_setup(portid
, 0, nb_txd
,
889 rte_eth_dev_socket_id(portid
),
893 dpdk_fmt_errmsg_for_rte_errno(p
->errbuf
,
894 PCAP_ERRBUF_SIZE
, -ret
,
895 "dpdk error: rte_eth_tx_queue_setup:port=%u",
900 // Initialize TX buffers
901 tx_buffer
= rte_zmalloc_socket(DPDK_TX_BUF_NAME
,
902 RTE_ETH_TX_BUFFER_SIZE(MAX_PKT_BURST
), 0,
903 rte_eth_dev_socket_id(portid
));
904 if (tx_buffer
== NULL
)
906 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
907 "dpdk error: Cannot allocate buffer for tx on port %u", portid
);
911 rte_eth_tx_buffer_init(tx_buffer
, MAX_PKT_BURST
);
913 ret
= rte_eth_dev_start(portid
);
916 dpdk_fmt_errmsg_for_rte_errno(p
->errbuf
,
917 PCAP_ERRBUF_SIZE
, -ret
,
918 "dpdk error: rte_eth_dev_start:port=%u",
923 // set promiscuous mode
925 pd
->must_clear_promisc
=1;
926 rte_eth_promiscuous_enable(portid
);
929 is_port_up
= check_link_status(portid
, &link
);
931 snprintf(p
->errbuf
, PCAP_ERRBUF_SIZE
,
932 "dpdk error: link is down, port=%u",portid
);
933 ret
= PCAP_ERROR_IFACE_NOT_UP
;
937 rte_eth_stats_reset(pd
->portid
);
938 calculate_timestamp(&(pd
->ts_helper
), &(pd
->prev_ts
));
939 rte_eth_stats_get(pd
->portid
,&(pd
->prev_stats
));
943 if (p
->snapshot
<=0 || p
->snapshot
> MAXIMUM_SNAPLEN
)
945 p
->snapshot
= MAXIMUM_SNAPLEN
;
947 p
->linktype
= DLT_EN10MB
; // Ethernet, the 10MB is historical.
948 p
->selectable_fd
= p
->fd
;
949 p
->read_op
= pcap_dpdk_dispatch
;
950 p
->inject_op
= pcap_dpdk_inject
;
951 // using pcapint_filter currently, though DPDK provides their own BPF function. Because DPDK BPF needs load a ELF file as a filter.
952 p
->setfilter_op
= pcapint_install_bpf_program
;
953 p
->setdirection_op
= NULL
;
954 p
->set_datalink_op
= NULL
;
955 p
->getnonblock_op
= pcap_dpdk_getnonblock
;
956 p
->setnonblock_op
= pcap_dpdk_setnonblock
;
957 p
->stats_op
= pcap_dpdk_stats
;
958 p
->cleanup_op
= pcap_dpdk_close
;
959 p
->breakloop_op
= pcapint_breakloop_common
;
960 // set default timeout
961 pd
->required_select_timeout
.tv_sec
= 0;
962 pd
->required_select_timeout
.tv_usec
= DPDK_DEF_MIN_SLEEP_MS
*1000;
963 p
->required_select_timeout
= &pd
->required_select_timeout
;
967 if (ret
<= PCAP_ERROR
) // all kinds of error code
969 pcapint_cleanup_live_common(p
);
971 rte_eth_dev_get_name_by_port(portid
,pd
->pci_addr
);
972 RTE_LOG(INFO
, USER1
,"Port %d device: %s, MAC:%s, PCI:%s\n", portid
, p
->opt
.device
, pd
->mac_addr
, pd
->pci_addr
);
973 RTE_LOG(INFO
, USER1
,"Port %d Link Up. Speed %u Mbps - %s\n",
974 portid
, link
.link_speed
,
975 (link
.link_duplex
== ETH_LINK_FULL_DUPLEX
) ?
976 ("full-duplex") : ("half-duplex\n"));
981 // device name for dpdk should be in the form as dpdk:number, such as dpdk:0
982 pcap_t
* pcap_dpdk_create(const char *device
, char *ebuf
, int *is_ours
)
987 *is_ours
= !strncmp(device
, "dpdk:", 5);
991 p
= PCAP_CREATE_COMMON(ebuf
, struct pcap_dpdk
);
995 p
->activate_op
= pcap_dpdk_activate
;
999 int pcap_dpdk_findalldevs(pcap_if_list_t
*devlistp
, char *ebuf
)
1002 unsigned int nb_ports
= 0;
1003 char dpdk_name
[DPDK_DEV_NAME_MAX
];
1004 char dpdk_desc
[DPDK_DEV_DESC_MAX
];
1005 ETHER_ADDR_TYPE eth_addr
;
1006 char mac_addr
[DPDK_MAC_ADDR_SIZE
];
1007 char pci_addr
[DPDK_PCI_ADDR_SIZE
];
1009 // init EAL; return "DPDK not available" if we
1010 // have insufficient permission
1011 char dpdk_pre_init_errbuf
[PCAP_ERRBUF_SIZE
];
1012 ret
= dpdk_pre_init(dpdk_pre_init_errbuf
, 1);
1015 // This returns a negative value on an error.
1016 snprintf(ebuf
, PCAP_ERRBUF_SIZE
,
1017 "Can't look for DPDK devices: %s",
1018 dpdk_pre_init_errbuf
);
1024 // This means DPDK isn't available on this machine.
1025 // That just means "don't return any devices".
1028 nb_ports
= rte_eth_dev_count_avail();
1031 // That just means "don't return any devices".
1035 for (unsigned int i
=0; i
<nb_ports
; i
++){
1036 snprintf(dpdk_name
, DPDK_DEV_NAME_MAX
-1,
1037 "%s%u", DPDK_PREFIX
, i
);
1039 rte_eth_macaddr_get(i
, ð_addr
);
1040 eth_addr_str(ð_addr
,mac_addr
,DPDK_MAC_ADDR_SIZE
);
1042 rte_eth_dev_get_name_by_port(i
,pci_addr
);
1043 snprintf(dpdk_desc
,DPDK_DEV_DESC_MAX
-1,"%s %s, MAC:%s, PCI:%s", DPDK_DESC
, dpdk_name
, mac_addr
, pci_addr
);
1044 if (pcapint_add_dev(devlistp
, dpdk_name
, 0, dpdk_desc
, ebuf
)==NULL
){
1055 * This libpcap build supports only DPDK, not regular network interfaces.
1059 * There are no regular interfaces, just DPDK interfaces.
1062 pcapint_platform_finddevs(pcap_if_list_t
*devlistp _U_
, char *errbuf
)
1068 * Attempts to open a regular interface fail.
1071 pcapint_create_interface(const char *device
, char *errbuf
)
1073 snprintf(errbuf
, PCAP_ERRBUF_SIZE
,
1074 "This version of libpcap only supports DPDK");
1079 * Libpcap version string.
1082 pcap_lib_version(void)
1084 return (PCAP_VERSION_STRING
" (DPDK-only)");