3. The testprogs/capturetest provides 6.4Gbps/800,000 pps on Intel 10-Gigabit X540-AT2 with DPDK 18.11.
Limitations:
-1. By default DPDK support is no, unless you explicitly set --enable-dpdk with ./configure or -DDISABLE_DPDK=OFF with cmake.
+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.
2. Only support link libdpdk.so dynamicly, because the libdpdk.a will not work correctly.
3. Only support read operation, and packet injection has not been supported yet.
Usage:
-1. compile DPDK as shared library and install.(https://github.com/DPDK/dpdk.git)
+1. Compile DPDK as shared library and install.(https://github.com/DPDK/dpdk.git)
You shall modify the file $RTE_SDK/$RTE_TARGET/.config and set:
CONFIG_RTE_BUILD_SHARED_LIB=y
By the following command:
sed -i 's/CONFIG_RTE_BUILD_SHARED_LIB=n/CONFIG_RTE_BUILD_SHARED_LIB=y/' $RTE_SDK/$RTE_TARGET/.config
-2. launch l2fwd that is one of DPDK examples correctly, and get device information.
+2. Launch l2fwd that is one of DPDK examples correctly, and get device information.
You shall learn how to bind nic with DPDK-compatible driver by $RTE_SDK/usertools/dpdk-devbind.py, such as igb_uio.
And enable hugepages by dpdk-setup.sh
Then launch the l2fwd with dynamic dirver support. For example:
$RTE_SDK/examples/l2fwd/$RTE_TARGET/l2fwd -dlibrte_pmd_e1000.so -dlibrte_pmd_ixgbe.so -dlibrte_mempool_ring.so -- -p 0x1
-3. compile libpcap with dpdk options.
+3. Compile libpcap with dpdk options.
-In order to find inlucde and lib automatically, you shall export DPDK envionment variable which are used for compiling DPDK.
+If DPDK has not been found automatically, you shall export DPDK envionment variable which are used for compiling DPDK. And then pass $RTE_SDK/$RTE_TARGET to --with-dpdk or -DDPDK_DIR
export RTE_SDK={your DPDK base directory}
export RTE_TARGET={your target name}
-3.1 with configure
+3.1 With configure
-./configure --enable-dpdk --with-dpdk-includes=$RTE_SDK/$RTE_TARGET/include --with-dpdk-libraries=$RTE_SDK/$RTE_TARGET/lib && make -s all && make -s testprogs && make install
+./configure --with-dpdk=$RTE_SDK/$RTE_TARGET && make -s all && make -s testprogs && make install
-3.2 with cmake
+3.2 With cmake
-mkdir -p build && cd build && cmake -DDISABLE_DPDK=OFF -DDPDK_INC_DIR=$RTE_SDK/$RTE_TARGET/include -DDPDK_LIB_DIR=$RTE_SDK/$RTE_TARGET/lib" ../ && make -s all && make -s testprogs && make install
+mkdir -p build && cd build && cmake -DDPDK_DIR=$RTE_SDK/$RTE_TARGET ../ && make -s all && make -s testprogs && make install
-4. link your own program with libpcap, and use DPDK with the device name as dpdk:{portid}, such as dpdk:0.
+4. Link your own program with libpcap, and use DPDK with the device name as dpdk:{portid}, such as dpdk:0.
And you shall set DPDK configure options by environment variable DPDK_CFG
For example, the testprogs/capturetest could be lanched by:
#define DPDK_DEV_NAME_MAX 32
#define DPDK_DEV_DESC_MAX 512
#define DPDK_CFG_ENV_NAME "DPDK_CFG"
+#define DPDK_DEF_MIN_SLEEP_MS 1
static char dpdk_cfg_buf[DPDK_CFG_MAX_LEN];
#define DPDK_MAC_ADDR_SIZE 32
#define DPDK_DEF_MAC_ADDR "00:00:00:00:00:00"
struct pcap_dpdk{
pcap_t * orig;
uint16_t portid; // portid of DPDK
- pcap_handler cb; //callback and argument
- u_char *cb_arg;
- int max_cnt;
int must_clear_promisc;
- int filter_in_userland;
- uint64_t rx_pkts;
uint64_t bpf_drop;
- struct ether_addr eth_addr;
- char mac_addr[DPDK_MAC_ADDR_SIZE];
+ int nonblock;
+ struct timeval required_select_timeout;
struct timeval prev_ts;
struct rte_eth_stats prev_stats;
struct timeval curr_ts;
uint64_t bps;
struct rte_mempool * pktmbuf_pool;
struct dpdk_ts_helper ts_helper;
+ struct ether_addr eth_addr;
+ char mac_addr[DPDK_MAC_ADDR_SIZE];
char pci_addr[DPDK_PCI_ADDR_SIZE];
unsigned char pcap_tmp_buf[RTE_ETH_PCAP_SNAPLEN];
};
return total_len;
}
-static void dpdk_dispatch_internal(void *dpdk_user)
-{
- if (dpdk_user == NULL){
- return;
+
+static int dpdk_read_with_timeout(pcap_t *p, uint16_t portid, uint16_t queueid,struct rte_mbuf **pkts_burst, const uint16_t burst_cnt){
+ struct pcap_dpdk *pd = (struct pcap_dpdk*)(p->priv);
+ int nb_rx = 0;
+ int timeout_ms = p->opt.timeout;
+ int sleep_ms = 0;
+ if (pd->nonblock){
+ // In non-blocking mode, just read once, no mater how many packets are captured.
+ nb_rx = (int)rte_eth_rx_burst(pd->portid, 0, pkts_burst, burst_cnt);
+ }else{
+ // In blocking mode, read many times until packets are captured or timeout or break_loop is setted.
+ // if timeout_ms == 0, it may be blocked forever.
+ while (timeout_ms == 0 || sleep_ms < timeout_ms){
+ nb_rx = (int)rte_eth_rx_burst(pd->portid, 0, pkts_burst, burst_cnt);
+ if (nb_rx){ // got packets within timeout_ms
+ break;
+ }else{ // no packet arrives at this round.
+ if (p->break_loop){
+ break;
+ }
+ // sleep for a very short while.
+ // block sleep is the only choice, since usleep() will impact performance dramatically.
+ rte_delay_us_block(DPDK_DEF_MIN_SLEEP_MS*1000);
+ sleep_ms += DPDK_DEF_MIN_SLEEP_MS;
+ }
+ }
}
- pcap_t *p = dpdk_user;
+ return nb_rx;
+}
+
+static int pcap_dpdk_dispatch(pcap_t *p, int max_cnt, pcap_handler cb, u_char *cb_arg)
+{
struct pcap_dpdk *pd = (struct pcap_dpdk*)(p->priv);
- int max_cnt = pd->max_cnt;
int burst_cnt = 0;
- pcap_handler cb = pd->cb;
- u_char *cb_arg = pd->cb_arg;
- int nb_rx=0;
+ int nb_rx = 0;
struct rte_mbuf *pkts_burst[MAX_PKT_BURST];
struct rte_mbuf *m;
struct pcap_pkthdr pcap_header;
uint16_t portid = pd->portid;
- unsigned lcore_id = rte_lcore_id();
- unsigned master_lcore_id = rte_get_master_lcore();
// In DPDK, pkt_len is sum of lengths for all segments. And data_len is for one segment
- uint16_t data_len = 0;
uint32_t pkt_len = 0;
int caplen = 0;
u_char *bp = NULL;
int pkt_cnt = 0;
int is_accepted=0;
u_char *large_buffer=NULL;
-
- if(lcore_id == master_lcore_id){
- RTE_LOG(DEBUG, USER1, "dpdk: lcoreid=%u runs for portid=%u\n", lcore_id, portid);
- }else{
- RTE_LOG(DEBUG, USER1, "dpdk: lcore %u has nothing to do\n", lcore_id);
- }
- //only use master lcore
- if (lcore_id != master_lcore_id){
- return;
- }
- if (max_cnt>0 && max_cnt < MAX_PKT_BURST){
+ int timeout_ms = p->opt.timeout;
+
+ if ( !PACKET_COUNT_IS_UNLIMITED(max_cnt) && max_cnt < MAX_PKT_BURST){
burst_cnt = max_cnt;
}else{
burst_cnt = MAX_PKT_BURST;
}
- while( max_cnt==-1 || pkt_cnt < max_cnt){
+
+ while( PACKET_COUNT_IS_UNLIMITED(max_cnt) || pkt_cnt < max_cnt){
if (p->break_loop){
- break;
+ p->break_loop = 0;
+ return PCAP_ERROR_BREAK;
+ }
+ // read once in non-blocking mode, or try many times waiting for timeout_ms.
+ // if timeout_ms == 0, it will be blocked until one packet arrives or break_loop is setted.
+ nb_rx = dpdk_read_with_timeout(p, portid, 0, pkts_burst, burst_cnt);
+ if (nb_rx == 0){
+ if (pd->nonblock){
+ RTE_LOG(DEBUG, USER1, "dpdk: no packets available in non-blocking mode.\n");
+ }else{
+ if (p->break_loop){
+ RTE_LOG(DEBUG, USER1, "dpdk: no packets available and break_loop is setted in blocking mode.\n");
+ p->break_loop = 0;
+ return PCAP_ERROR_BREAK;
+
+ }
+ RTE_LOG(DEBUG, USER1, "dpdk: no packets available for timeout %d ms in blocking mode.\n", timeout_ms);
+ }
+ // break if dpdk reads 0 packet, no matter in blocking(timeout) or non-blocking mode.
+ break;
}
- nb_rx = (int)rte_eth_rx_burst(portid, 0, pkts_burst, burst_cnt);
pkt_cnt += nb_rx;
for ( i = 0; i < nb_rx; i++) {
m = pkts_burst[i];
calculate_timestamp(&(pd->ts_helper),&(pcap_header.ts));
- data_len = rte_pktmbuf_data_len(m);
pkt_len = rte_pktmbuf_pkt_len(m);
// caplen = min(pkt_len, p->snapshot);
// caplen will not be changed, no matter how long the rte_pktmbuf
}
if (bp){
- //default accpet all
- is_accepted=1;
- if (pd->filter_in_userland && p->fcode.bf_insns!=NULL)
- {
- if (!pcap_filter(p->fcode.bf_insns, bp, pcap_header.len, pcap_header.caplen)){
- //rejected
- is_accepted=0;
- }
- }
- if (is_accepted){
+ if (p->fcode.bf_insns==NULL || pcap_filter(p->fcode.bf_insns, bp, pcap_header.len, pcap_header.caplen)){
cb(cb_arg, &pcap_header, bp);
}else{
pd->bpf_drop++;
}
}
}
- pd->rx_pkts = pkt_cnt;
-}
-
-static int pcap_dpdk_dispatch(pcap_t *p, int max_cnt, pcap_handler cb, u_char *pcap_user)
-{
- unsigned lcore_id = 0;
- struct pcap_dpdk *pd = (struct pcap_dpdk*)(p->priv);
- pd->rx_pkts=0;
- pd->cb = cb;
- pd->cb_arg = pcap_user;
- pd->max_cnt = max_cnt;
- pd->orig = p;
- void *dpdk_user = p;
- dpdk_dispatch_internal(dpdk_user);
- return pd->rx_pkts;
+ return pkt_cnt;
}
static int pcap_dpdk_inject(pcap_t *p, const void *buf _U_, int size _U_)
{
//not implemented yet
pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
- errno, "dpdk error: Inject function has not be implemented yet");
+ errno, "dpdk error: Inject function has not been implemented yet");
return PCAP_ERROR;
}
}
rte_eth_dev_stop(pd->portid);
rte_eth_dev_close(pd->portid);
- // free pcap_dpdk?
pcap_cleanup_live_common(p);
}
return 0;
}
-static int pcap_dpdk_setnonblock(pcap_t *p, int fd _U_){
- pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
- errno, "dpdk error: setnonblock not support");
+static int pcap_dpdk_setnonblock(pcap_t *p, int nonblock){
+ struct pcap_dpdk *pd = (struct pcap_dpdk*)(p->priv);
+ pd->nonblock = nonblock;
return 0;
}
static int pcap_dpdk_getnonblock(pcap_t *p){
- pcap_fmt_errmsg_for_errno(p->errbuf, PCAP_ERRBUF_SIZE,
- errno, "dpdk error: getnonblock not support");
- return 0;
+ struct pcap_dpdk *pd = (struct pcap_dpdk*)(p->priv);
+ return pd->nonblock;
}
static int check_link_status(uint16_t portid, struct rte_eth_link *plink)
{
}
}
ret_ul = strtoul(&(device[prefix_len]), &pEnd, 10);
+ if (pEnd == &(device[prefix_len]) || *pEnd != '\0'){
+ return ret;
+ }
// too large for portid
if (ret_ul >= DPDK_PORTID_MAX){
return ret;
// find first non space char
// The last opt is NULL
for (i=0;dpdk_cfg[i] && cnt<DPDK_ARGC_MAX-1;i++){
- if (skip_space && dpdk_cfg[i]!=0x20){ // not space
+ if (skip_space && dpdk_cfg[i]!=' '){ // not space
skip_space=!skip_space; // skip normal char
dargv[cnt++] = dpdk_cfg+i;
}
- if (!skip_space && dpdk_cfg[i]==0x20){ // fint a space
+ if (!skip_space && dpdk_cfg[i]==' '){ // fint a space
dpdk_cfg[i]=0x00; // end of this opt
skip_space=!skip_space; // skip space char
}
p->selectable_fd = p->fd;
p->read_op = pcap_dpdk_dispatch;
p->inject_op = pcap_dpdk_inject;
- // DPDK only support filter in userland now
- pd->filter_in_userland = 1;
+ // using pcap_filter currently, though DPDK provides their own BPF function. Because DPDK BPF needs load a ELF file as a filter.
p->setfilter_op = install_bpf_program;
p->setdirection_op = NULL;
p->set_datalink_op = NULL;
p->stats_op = pcap_dpdk_stats;
p->cleanup_op = pcap_dpdk_close;
p->breakloop_op = pcap_breakloop_common;
+ // set default timeout
+ pd->required_select_timeout.tv_sec = 0;
+ pd->required_select_timeout.tv_usec = DPDK_DEF_MIN_SLEEP_MS*1000;
+ p->required_select_timeout = &pd->required_select_timeout;
ret = 0; // OK
}while(0);
- if (ret == PCAP_ERROR)
+ if (ret <= PCAP_ERROR) // all kinds of error code
{
pcap_cleanup_live_common(p);
}else{
int pcap_dpdk_findalldevs(pcap_if_list_t *devlistp, char *ebuf)
{
int ret=0;
- int nb_ports = 0;
+ unsigned int nb_ports = 0;
char dpdk_name[DPDK_DEV_NAME_MAX];
char dpdk_desc[DPDK_DEV_DESC_MAX];
struct ether_addr eth_addr;
ret = PCAP_ERROR;
break;
}
- for (int i=0; i<nb_ports; i++){
- pcap_snprintf(dpdk_name,DPDK_DEV_NAME_MAX-1,"dpdk:%d",i);
+ for (unsigned int i=0; i<nb_ports; i++){
+ pcap_snprintf(dpdk_name, DPDK_DEV_NAME_MAX-1,
+ "%s%u", DPDK_PREFIX, i);
// mac addr
rte_eth_macaddr_get(i, ð_addr);
eth_addr_str(ð_addr,mac_addr,DPDK_MAC_ADDR_SIZE);
// PCI addr
rte_eth_dev_get_name_by_port(i,pci_addr);
pcap_snprintf(dpdk_desc,DPDK_DEV_DESC_MAX-1,"%s %s, MAC:%s, PCI:%s", DPDK_DESC, dpdk_name, mac_addr, pci_addr);
- // continue add all dev, even error happens
- add_dev(devlistp, dpdk_name, 0, dpdk_desc, ebuf);
+ if (add_dev(devlistp, dpdk_name, 0, dpdk_desc, ebuf)==NULL){
+ ret = PCAP_ERROR;
+ break;
+ }
}
}while(0);
return ret;
}
+
+#ifdef DPDK_ONLY
+/*
+ * This libpcap build supports only DPDK, not regular network interfaces.
+ */
+
+/*
+ * There are no regular interfaces, just DPDK interfaces.
+ */
+int
+pcap_platform_finddevs(pcap_if_list_t *devlistp _U_, char *errbuf)
+{
+ return (0);
+}
+
+/*
+ * Attempts to open a regular interface fail.
+ */
+pcap_t *
+pcap_create_interface(const char *device, char *errbuf)
+{
+ pcap_snprintf(errbuf, PCAP_ERRBUF_SIZE,
+ "This version of libpcap only supports DPDK");
+ return NULL;
+}
+
+/*
+ * Libpcap version string.
+ */
+const char *
+pcap_lib_version(void)
+{
+ return (PCAP_VERSION_STRING " (DPDK-only)");
+}
+#endif