我刚刚成功地打开了一个RAW套接字,我试图用下面的function导出内核TX和RX环。 但是,当试图告诉内核导出第二个环时,setsockopt()返回EBUSY(设备或资源繁忙)。 这是,执行下面的代码一旦工作正常,所以我可以得到第一个环(TX或RX)。 当我尝试导出第二个环时,问题出现了(对于TX来说,如果第一个是用于RX或反之亦然)。
TX和RX与mmap()ed内存不能使用相同的套接字? 这是,我必须打开TX一个套接字和RX一个套接字?
/* init_ring */ void *init_ring(ll_socket_t *ll_socket, int type) { void *ring = NULL; int ring_len = 0; int ring_access_flags = PROT_READ | PROT_WRITE; struct tpacket_req tp; // 1) tell kernel to export data through mmap()ped ring tp.tp_block_size = FRAMES_PER_RING * getpagesize(); tp.tp_block_nr = 1; tp.tp_frame_size = getpagesize(); tp.tp_frame_nr = FRAMES_PER_RING; ring_len = tp.tp_block_size * tp.tp_block_nr; if ( setsockopt(ll_socket->socket_fd, SOL_PACKET, type, &tp, sizeof(struct tpacket_req) ) < 0 ) { handle_sys_error("Setting socket options for this ring"); } // 2) open ring if ( ( ring = mmap(NULL, ring_len, ring_access_flags, MAP_SHARED, ll_socket->socket_fd, 0) ) == NULL ) { handle_sys_error("mmap()ing error"); } return(ring);
}