#include <linux/if_ether.h>
#include <linux/if_packet.h>
+#include <sys/ioctl.h>
+#include <net/if.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
+#include <vppinfra/linux/sysfs.h>
#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
-#include <vlib/linux/sysfs.h>
#include <vnet/ip/ip.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/devices/af_packet/af_packet.h>
+af_packet_main_t af_packet_main;
+
#define AF_PACKET_DEBUG_SOCKET 0
#define AF_PACKET_TX_FRAMES_PER_BLOCK 1024
{
s = format (0, "/sys/class/net/%s/mtu%c", apif->host_if_name, 0);
- error = vlib_sysfs_write ((char *) s, "%d", hi->max_packet_bytes);
+ error = clib_sysfs_write ((char *) s, "%d", hi->max_packet_bytes);
vec_free (s);
if (error)
}
static clib_error_t *
-af_packet_fd_read_ready (unix_file_t * uf)
+af_packet_fd_read_ready (clib_file_t * uf)
{
af_packet_main_t *apm = &af_packet_main;
vnet_main_t *vnm = vnet_get_main ();
goto error;
}
+ /* bind before rx ring is cfged so we don't receive packets from other interfaces */
+ memset (&sll, 0, sizeof (sll));
+ sll.sll_family = PF_PACKET;
+ sll.sll_protocol = htons (ETH_P_ALL);
+ sll.sll_ifindex = host_if_index;
+ if ((err = bind (*fd, (struct sockaddr *) &sll, sizeof (sll))) < 0)
+ {
+ DBG_SOCK ("Failed to bind rx packet socket (error %d)", err);
+ ret = VNET_API_ERROR_SYSCALL_ERROR_1;
+ goto error;
+ }
+
if ((err =
setsockopt (*fd, SOL_PACKET, PACKET_VERSION, &ver, sizeof (ver))) < 0)
{
if ((err =
setsockopt (*fd, SOL_PACKET, PACKET_TX_RING, tx_req, req_sz)) < 0)
{
- DBG_SOCK ("Failed to set packet rx ring options");
+ DBG_SOCK ("Failed to set packet tx ring options");
ret = VNET_API_ERROR_SYSCALL_ERROR_1;
goto error;
}
goto error;
}
- memset (&sll, 0, sizeof (sll));
- sll.sll_family = PF_PACKET;
- sll.sll_protocol = htons (ETH_P_ALL);
- sll.sll_ifindex = host_if_index;
-
- if ((err = bind (*fd, (struct sockaddr *) &sll, sizeof (sll))) < 0)
- {
- DBG_SOCK ("Failed to bind rx packet socket (error %d)", err);
- ret = VNET_API_ERROR_SYSCALL_ERROR_1;
- goto error;
- }
-
return 0;
error:
if (*fd >= 0)
u32 * sw_if_index)
{
af_packet_main_t *apm = &af_packet_main;
- int ret, fd = -1;
+ int ret, fd = -1, fd2 = -1;
struct tpacket_req *rx_req = 0;
struct tpacket_req *tx_req = 0;
+ struct ifreq ifr;
u8 *ring = 0;
af_packet_if_t *apif = 0;
u8 hw_addr[6];
p = mhash_get (&apm->if_index_by_host_if_name, host_if_name);
if (p)
{
- return VNET_API_ERROR_SUBIF_ALREADY_EXISTS;
+ apif = vec_elt_at_index (apm->interfaces, p[0]);
+ *sw_if_index = apif->sw_if_index;
+ return VNET_API_ERROR_IF_ALREADY_EXISTS;
}
vec_validate (rx_req, 0);
tx_req->tp_block_nr = AF_PACKET_TX_BLOCK_NR;
tx_req->tp_frame_nr = AF_PACKET_TX_FRAME_NR;
- host_if_index = if_nametoindex ((const char *) host_if_name);
+ /*
+ * make sure host side of interface is 'UP' before binding AF_PACKET
+ * socket on it.
+ */
+ if ((fd2 = socket (AF_UNIX, SOCK_DGRAM, 0)) < 0)
+ {
+ DBG_SOCK ("Failed to create socket");
+ ret = VNET_API_ERROR_SYSCALL_ERROR_1;
+ goto error;
+ }
- if (!host_if_index)
+ clib_memcpy (ifr.ifr_name, (const char *) host_if_name,
+ vec_len (host_if_name));
+ if ((ret = ioctl (fd2, SIOCGIFINDEX, &ifr)) < 0)
{
- DBG_SOCK ("Wrong host interface name");
+ clib_unix_warning ("af_packet_create error: %d", ret);
+ close (fd2);
return VNET_API_ERROR_INVALID_INTERFACE;
}
+ host_if_index = ifr.ifr_ifindex;
+ if ((ret = ioctl (fd2, SIOCGIFFLAGS, &ifr)) < 0)
+ {
+ clib_unix_warning ("af_packet_create error: %d", ret);
+ goto error;
+ }
+
+ if (!(ifr.ifr_flags & IFF_UP))
+ {
+ ifr.ifr_flags |= IFF_UP;
+ if ((ret = ioctl (fd2, SIOCSIFFLAGS, &ifr)) < 0)
+ {
+ clib_unix_warning ("af_packet_create error: %d", ret);
+ goto error;
+ }
+ }
+
+ if (fd2 > -1)
+ close (fd2);
+
ret = create_packet_v2_sock (host_if_index, rx_req, tx_req, &fd, &ring);
if (ret != 0)
clib_spinlock_init (&apif->lockp);
{
- unix_file_t template = { 0 };
+ clib_file_t template = { 0 };
template.read_function = af_packet_fd_read_ready;
template.file_descriptor = fd;
template.private_data = if_index;
template.flags = UNIX_FILE_EVENT_EDGE_TRIGGERED;
- apif->unix_file_index = unix_file_add (&unix_main, &template);
+ template.description = format (0, "%U", format_af_packet_device_name,
+ if_index);
+ apif->clib_file_index = clib_file_add (&file_main, &template);
}
/*use configured or generate random MAC address */
return 0;
error:
+ if (fd2 > -1)
+ close (fd2);
vec_free (host_if_name_dup);
vec_free (rx_req);
vec_free (tx_req);
vnet_hw_interface_unassign_rx_thread (vnm, apif->hw_if_index, 0);
/* clean up */
- if (apif->unix_file_index != ~0)
+ if (apif->clib_file_index != ~0)
{
- unix_file_del (&unix_main, unix_main.file_pool + apif->unix_file_index);
- apif->unix_file_index = ~0;
+ clib_file_del (&file_main, file_main.file_pool + apif->clib_file_index);
+ apif->clib_file_index = ~0;
}
else
close (apif->fd);
return 0;
}
+int
+af_packet_set_l4_cksum_offload (vlib_main_t * vm, u32 sw_if_index, u8 set)
+{
+ vnet_main_t *vnm = vnet_get_main ();
+ vnet_hw_interface_t *hw;
+
+ hw = vnet_get_sup_hw_interface (vnm, sw_if_index);
+
+ if (hw->dev_class_index != af_packet_device_class.index)
+ return VNET_API_ERROR_INVALID_INTERFACE;
+
+ if (set)
+ hw->flags &= ~VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
+ else
+ hw->flags |= VNET_HW_INTERFACE_FLAG_SUPPORTS_TX_L4_CKSUM_OFFLOAD;
+
+ return 0;
+}
+
static clib_error_t *
af_packet_init (vlib_main_t * vm)
{