20 #include <linux/if_ether.h> 21 #include <linux/if_packet.h> 24 #include <sys/types.h> 37 #define AF_PACKET_DEBUG_SOCKET 0 39 #define AF_PACKET_TX_FRAMES_PER_BLOCK 1024 40 #define AF_PACKET_TX_FRAME_SIZE (2048 * 5) 41 #define AF_PACKET_TX_BLOCK_NR 1 42 #define AF_PACKET_TX_FRAME_NR (AF_PACKET_TX_BLOCK_NR * \ 43 AF_PACKET_TX_FRAMES_PER_BLOCK) 44 #define AF_PACKET_TX_BLOCK_SIZE (AF_PACKET_TX_FRAME_SIZE * \ 45 AF_PACKET_TX_FRAMES_PER_BLOCK) 47 #define AF_PACKET_RX_FRAMES_PER_BLOCK 1024 48 #define AF_PACKET_RX_FRAME_SIZE (2048 * 5) 49 #define AF_PACKET_RX_BLOCK_NR 1 50 #define AF_PACKET_RX_FRAME_NR (AF_PACKET_RX_BLOCK_NR * \ 51 AF_PACKET_RX_FRAMES_PER_BLOCK) 52 #define AF_PACKET_RX_BLOCK_SIZE (AF_PACKET_RX_FRAME_SIZE * \ 53 AF_PACKET_RX_FRAMES_PER_BLOCK) 55 #if AF_PACKET_DEBUG_SOCKET == 1 56 #define DBG_SOCK(args...) clib_warning(args); 58 #define DBG_SOCK(args...) 86 return VNET_API_ERROR_SYSCALL_ERROR_1;
116 s =
format (0,
"/sys/class/net/%s/bridge%c", host_if_name, 0);
117 dir = opendir ((
char *) s);
134 struct sockaddr_ll sll;
135 int ver = TPACKET_V2;
136 socklen_t req_sz =
sizeof (
struct tpacket_req);
137 u32 ring_sz = rx_req->tp_block_size * rx_req->tp_block_nr +
138 tx_req->tp_block_size * tx_req->tp_block_nr;
140 if ((*fd = socket (AF_PACKET, SOCK_RAW, htons (ETH_P_ALL))) < 0)
142 DBG_SOCK (
"Failed to create socket");
143 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
148 setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver,
sizeof (ver))) < 0)
150 DBG_SOCK (
"Failed to set rx packet interface version");
151 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
157 setsockopt (*fd, SOL_PACKET, PACKET_LOSS, &opt,
sizeof (opt))) < 0)
159 DBG_SOCK (
"Failed to set packet tx ring error handling option");
160 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
165 setsockopt (*fd, SOL_PACKET, PACKET_RX_RING, rx_req, req_sz)) < 0)
167 DBG_SOCK (
"Failed to set packet rx ring options");
168 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
173 setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz)) < 0)
175 DBG_SOCK (
"Failed to set packet rx ring options");
176 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
181 mmap (
NULL, ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_LOCKED, *fd,
183 if (*ring == MAP_FAILED)
186 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
190 memset (&sll, 0,
sizeof (sll));
191 sll.sll_family = PF_PACKET;
192 sll.sll_protocol = htons (ETH_P_ALL);
193 sll.sll_ifindex = host_if_index;
195 if ((err = bind (*fd, (
struct sockaddr *) &sll,
sizeof (sll))) < 0)
197 DBG_SOCK (
"Failed to bind rx packet socket (error %d)", err);
198 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
216 struct tpacket_req *rx_req = 0;
217 struct tpacket_req *tx_req = 0;
228 u8 *host_if_name_dup =
vec_dup (host_if_name);
229 int host_if_index = -1;
234 return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
253 DBG_SOCK (
"Wrong host interface name");
254 return VNET_API_ERROR_INVALID_INTERFACE;
274 apif->
tx_ring = ring + rx_req->tp_block_size * rx_req->tp_block_nr;
288 template.file_descriptor = fd;
289 template.private_data = if_index;
301 rnd = (
u32) (now * 1e6);
315 memset (apif, 0,
sizeof (*apif));
318 ret = VNET_API_ERROR_SYSCALL_ERROR_1;
365 clib_warning (
"Host interface %s does not exist", host_if_name);
366 return VNET_API_ERROR_SYSCALL_ERROR_1;
384 ring_sz = apif->
rx_req->tp_block_size * apif->
rx_req->tp_block_nr +
385 apif->
tx_req->tp_block_size * apif->
tx_req->tp_block_nr;
386 if (munmap (apif->
rx_ring, ring_sz))
387 clib_warning (
"Host interface %s could not free rx/tx ring",
420 return VNET_API_ERROR_INVALID_INTERFACE;
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
#define UNIX_FILE_EVENT_EDGE_TRIGGERED
static void clib_file_del(clib_file_main_t *um, clib_file_t *f)
clib_error_t * vnet_hw_interface_set_flags(vnet_main_t *vnm, u32 hw_if_index, u32 flags)
uword * pending_input_bitmap
void ethernet_delete_interface(vnet_main_t *vnm, u32 hw_if_index)
int af_packet_set_l4_cksum_offload(vlib_main_t *vm, u32 sw_if_index, u8 set)
vnet_main_t * vnet_get_main(void)
static vnet_hw_interface_t * vnet_get_sup_hw_interface(vnet_main_t *vnm, u32 sw_if_index)
#define AF_PACKET_TX_BLOCK_NR
uword mhash_unset(mhash_t *h, void *key, uword *old_value)
static f64 vlib_time_now(vlib_main_t *vm)
struct tpacket_req * tx_req
static vnet_hw_interface_t * vnet_get_hw_interface(vnet_main_t *vnm, u32 hw_if_index)
#define AF_PACKET_RX_FRAME_NR
static void mhash_init_vec_string(mhash_t *h, uword n_value_bytes)
static uword * clib_bitmap_set(uword *ai, uword i, uword value)
Sets the ith bit of a bitmap to new_value Removes trailing zeros from the bitmap. ...
struct tpacket_req * rx_req
#define VNET_HW_INTERFACE_FLAG_LINK_UP
#define vec_validate_aligned(V, I, A)
Make sure vector is long enough for given index (no header, specified alignment)
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
clib_error_t * clib_sysfs_write(char *file_name, char *fmt,...)
clib_file_function_t * read_function
static vnet_sw_interface_t * vnet_get_hw_sw_interface(vnet_main_t *vnm, u32 hw_if_index)
af_packet_if_t * interfaces
#define AF_PACKET_TX_BLOCK_SIZE
#define VLIB_INIT_FUNCTION(x)
static_always_inline void vnet_device_input_set_interrupt_pending(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id)
clib_file_main_t file_main
static void clib_spinlock_init(clib_spinlock_t *p)
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
uword mhash_set_mem(mhash_t *h, void *key, uword *new_value, uword *old_value)
#define pool_put(P, E)
Free an object E in pool P.
#define vec_dup(V)
Return copy of vector (no header, no alignment)
#define AF_PACKET_RX_BLOCK_NR
#define VNET_HW_INTERFACE_FLAG_SUPPORTS_INT_MODE
struct tpacket_req tpacket_req_t
#define vec_free(V)
Free vector's memory (no header).
#define clib_warning(format, args...)
#define clib_memcpy(a, b, c)
#define AF_PACKET_RX_BLOCK_SIZE
#define ETHERNET_INTERFACE_FLAG_MTU
#define VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD
u32 per_interface_next_index
vlib_node_registration_t af_packet_input_node
(constructor) VLIB_REGISTER_NODE (af_packet_input_node)
af_packet_main_t af_packet_main
void vnet_hw_interface_assign_rx_thread(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, uword thread_index)
#define DBG_SOCK(args...)
static uword * mhash_get(mhash_t *h, const void *key)
static uword clib_file_add(clib_file_main_t *um, clib_file_t *template)
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)
#define clib_error_report(e)
mhash_t if_index_by_host_if_name
unsigned int if_nametoindex(const char *ifname)
vnet_device_class_t af_packet_device_class
static clib_error_t * af_packet_fd_read_ready(clib_file_t *uf)
#define AF_PACKET_TX_FRAME_SIZE
static int create_packet_v2_sock(int host_if_index, tpacket_req_t *rx_req, tpacket_req_t *tx_req, int *fd, u8 **ring)
#define AF_PACKET_RX_FRAME_SIZE
int vnet_hw_interface_unassign_rx_thread(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id)
static u32 random_u32(u32 *seed)
32-bit random number generator
static vlib_thread_main_t * vlib_get_thread_main()
static clib_error_t * af_packet_init(vlib_main_t *vm)
static int is_bridge(const u8 *host_if_name)
int vnet_hw_interface_set_rx_mode(vnet_main_t *vnm, u32 hw_if_index, u16 queue_id, vnet_hw_interface_rx_mode mode)
static u32 af_packet_eth_flag_change(vnet_main_t *vnm, vnet_hw_interface_t *hi, u32 flags)
int af_packet_create_if(vlib_main_t *vm, u8 *host_if_name, u8 *hw_addr_set, u32 *sw_if_index)
#define CLIB_CACHE_LINE_BYTES
static void vnet_hw_interface_set_input_node(vnet_main_t *vnm, u32 hw_if_index, u32 node_index)
#define AF_PACKET_TX_FRAME_NR
int af_packet_delete_if(vlib_main_t *vm, u8 *host_if_name)