20 #include <linux/if_ether.h> 21 #include <linux/if_packet.h> 30 #define AF_PACKET_DEBUG_SOCKET 0 32 #define AF_PACKET_TX_FRAMES_PER_BLOCK 1024 33 #define AF_PACKET_TX_FRAME_SIZE (2048 * 5) 34 #define AF_PACKET_TX_BLOCK_NR 1 35 #define AF_PACKET_TX_FRAME_NR (AF_PACKET_TX_BLOCK_NR * \ 36 AF_PACKET_TX_FRAMES_PER_BLOCK) 37 #define AF_PACKET_TX_BLOCK_SIZE (AF_PACKET_TX_FRAME_SIZE * \ 38 AF_PACKET_TX_FRAMES_PER_BLOCK) 40 #define AF_PACKET_RX_FRAMES_PER_BLOCK 1024 41 #define AF_PACKET_RX_FRAME_SIZE (2048 * 5) 42 #define AF_PACKET_RX_BLOCK_NR 1 43 #define AF_PACKET_RX_FRAME_NR (AF_PACKET_RX_BLOCK_NR * \ 44 AF_PACKET_RX_FRAMES_PER_BLOCK) 45 #define AF_PACKET_RX_BLOCK_SIZE (AF_PACKET_RX_FRAME_SIZE * \ 46 AF_PACKET_RX_FRAMES_PER_BLOCK) 48 #if AF_PACKET_DEBUG_SOCKET == 1 49 #define DBG_SOCK(args...) clib_warning(args); 51 #define DBG_SOCK(args...) 85 struct sockaddr_ll sll;
88 socklen_t req_sz =
sizeof(
struct tpacket_req);
89 u32 ring_sz = rx_req->tp_block_size * rx_req->tp_block_nr +
90 tx_req->tp_block_size * tx_req->tp_block_nr;
96 DBG_SOCK(
"Wrong host interface name");
97 ret = VNET_API_ERROR_INVALID_INTERFACE;
101 if ((*fd = socket(AF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) < 0)
103 DBG_SOCK(
"Failed to create socket");
104 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
108 if ((err = setsockopt(*fd, SOL_PACKET, PACKET_VERSION, &ver,
sizeof(ver))) < 0)
110 DBG_SOCK(
"Failed to set rx packet interface version");
111 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
116 if ((err = setsockopt(*fd, SOL_PACKET, PACKET_LOSS, &opt,
sizeof(opt))) < 0)
118 DBG_SOCK(
"Failed to set rx packet interface version");
119 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
123 if ((err = setsockopt(*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz)) < 0)
125 DBG_SOCK(
"Failed to set packet rx ring options");
126 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
130 if ((err = setsockopt(*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz)) < 0)
132 DBG_SOCK(
"Failed to set packet rx ring options");
133 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
137 *ring = mmap(
NULL, ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, *fd, 0);
138 if (*ring == MAP_FAILED)
141 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
145 memset(&sll, 0,
sizeof(sll));
146 sll.sll_family = PF_PACKET;
147 sll.sll_protocol = htons(ETH_P_ALL);
148 sll.sll_ifindex = host_if_index;
150 if ((err = bind(*fd, (
struct sockaddr *) &sll,
sizeof(sll))) < 0)
152 DBG_SOCK(
"Failed to bind rx packet socket (error %d)", err);
153 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
169 struct tpacket_req * rx_req = 0;
170 struct tpacket_req * tx_req = 0;
183 return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
209 apif->
tx_ring = ring + rx_req->tp_block_size * rx_req->tp_block_nr;
220 template.file_descriptor = fd;
221 template.private_data = if_index;
233 rnd = (
u32) (now * 1e6);
247 memset(apif, 0,
sizeof(*apif));
250 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
283 clib_warning(
"Host interface %s does not exist", host_if_name);
284 return VNET_API_ERROR_SYSCALL_ERROR_1;
297 ring_sz = apif->
rx_req->tp_block_size * apif->
rx_req->tp_block_nr +
298 apif->
tx_req->tp_block_size * apif->
tx_req->tp_block_nr;
299 if (munmap(apif->
rx_ring, ring_sz))
300 clib_warning(
"Host interface %s could not free rx/tx ring", host_if_name);
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
unix_file_function_t * read_function
uword * pending_input_bitmap
always_inline void mhash_init_vec_string(mhash_t *h, uword n_value_bytes)
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
int af_packet_create_if(vlib_main_t *vm, u8 *host_if_name, u8 *hw_addr_set)
#define AF_PACKET_TX_BLOCK_NR
uword mhash_unset(mhash_t *h, void *key, uword *old_value)
struct tpacket_req * tx_req
always_inline uword unix_file_add(unix_main_t *um, unix_file_t *template)
#define AF_PACKET_RX_FRAME_NR
always_inline void vlib_node_set_interrupt_pending(vlib_main_t *vm, u32 node_index)
static clib_error_t * af_packet_fd_read_ready(unix_file_t *uf)
struct tpacket_req * rx_req
#define clib_error_report(e)
#define VNET_HW_INTERFACE_FLAG_LINK_UP
always_inline vlib_main_t * vlib_get_main(void)
af_packet_if_t * interfaces
vnet_main_t * vnet_get_main(void)
#define AF_PACKET_TX_BLOCK_SIZE
#define VLIB_INIT_FUNCTION(x)
#define clib_warning(format, args...)
always_inline u32 random_u32(u32 *seed)
32-bit random number generator
#define pool_elt_at_index(p, i)
uword mhash_set_mem(mhash_t *h, void *key, uword *new_value, uword *old_value)
#define UNIX_FILE_EVENT_EDGE_TRIGGERED
#define AF_PACKET_RX_BLOCK_NR
always_inline uword * clib_bitmap_set(uword *ai, uword i, uword value)
struct tpacket_req tpacket_req_t
#define vec_free(V)
Free vector's memory (no header).
#define clib_memcpy(a, b, c)
#define AF_PACKET_RX_BLOCK_SIZE
u32 per_interface_next_index
vlib_node_registration_t af_packet_input_node
(constructor) VLIB_REGISTER_NODE (af_packet_input_node)
always_inline void unix_file_del(unix_main_t *um, unix_file_t *f)
#define DBG_SOCK(args...)
clib_error_t * ethernet_register_interface(vnet_main_t *vnm, u32 dev_class_index, u32 dev_instance, u8 *address, u32 *hw_if_index_return, ethernet_flag_change_function_t flag_change)
af_packet_main_t af_packet_main
mhash_t if_index_by_host_if_name
unsigned int if_nametoindex(const char *ifname)
vnet_device_class_t af_packet_device_class
#define AF_PACKET_TX_FRAME_SIZE
static int create_packet_v2_sock(u8 *name, tpacket_req_t *rx_req, tpacket_req_t *tx_req, int *fd, u8 **ring)
#define AF_PACKET_RX_FRAME_SIZE
static clib_error_t * af_packet_init(vlib_main_t *vm)
always_inline f64 vlib_time_now(vlib_main_t *vm)
static u32 af_packet_eth_flag_change(vnet_main_t *vnm, vnet_hw_interface_t *hi, u32 flags)
#define AF_PACKET_TX_FRAME_NR
always_inline uword * mhash_get(mhash_t *h, void *key)
int af_packet_delete_if(vlib_main_t *vm, u8 *host_if_name)
always_inline vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)