mirror of
https://github.com/torvalds/linux.git
synced 2026-09-22 04:34:03 +02:00
selftests/net: Test PACKET_AUXDATA
Extend the packet socket selftest, adding a recvmsg path, to test PACKET_AUXDATA. Check basic attributes of tpacket_auxdata. Signed-off-by: Joe Damato <joe@dama.to> Reviewed-by: Willem de Bruijn <willemb@google.com> Link: https://patch.msgid.link/20260720122714.759175-4-joe@dama.to Signed-off-by: Jakub Kicinski <kuba@kernel.org>
This commit is contained in:
parent
1451e53029
commit
707f5c2de0
|
|
@ -40,6 +40,7 @@ static bool cfg_use_qdisc_bypass;
|
|||
static bool cfg_use_vlan;
|
||||
static bool cfg_use_vnet;
|
||||
static bool cfg_drop;
|
||||
static bool cfg_aux_data;
|
||||
|
||||
static char *cfg_ifname = "lo";
|
||||
static int cfg_mtu = 1500;
|
||||
|
|
@ -279,11 +280,54 @@ static int setup_rx(void)
|
|||
return fd;
|
||||
}
|
||||
|
||||
static void do_rx(int fd, int expected_len, char *expected)
|
||||
static void check_aux_data(struct cmsghdr *cmsg, int expected_len)
|
||||
{
|
||||
struct tpacket_auxdata *adata;
|
||||
|
||||
if (!cmsg)
|
||||
error(1, 0, "auxdata null");
|
||||
|
||||
if (cmsg->cmsg_level != SOL_PACKET)
|
||||
error(1, 0, "cmsg_level != SOL_PACKET");
|
||||
|
||||
if (cmsg->cmsg_type != PACKET_AUXDATA)
|
||||
error(1, 0, "cmsg_type != PACKET_AUXDATA");
|
||||
|
||||
adata = (struct tpacket_auxdata *)CMSG_DATA(cmsg);
|
||||
|
||||
if (adata->tp_net != ETH_HLEN)
|
||||
error(1, 0, "cmsg tp_net != ETH_HLEN");
|
||||
|
||||
if (adata->tp_len != expected_len)
|
||||
error(1, 0, "cmsg tp_len != %u", expected_len);
|
||||
|
||||
if (adata->tp_snaplen != expected_len)
|
||||
error(1, 0, "cmsg tp_snaplen != %u", expected_len);
|
||||
}
|
||||
|
||||
static void do_rx(int fd, int expected_len, char *expected, bool is_psock)
|
||||
{
|
||||
char cmsg_buf[1024] __attribute__((aligned(8))) = {};
|
||||
bool aux = is_psock && cfg_aux_data;
|
||||
struct msghdr msg = {};
|
||||
struct iovec iov[1];
|
||||
int ret;
|
||||
|
||||
ret = recv(fd, rbuf, sizeof(rbuf), 0);
|
||||
if (aux) {
|
||||
iov[0].iov_base = rbuf;
|
||||
iov[0].iov_len = sizeof(rbuf);
|
||||
|
||||
msg.msg_iov = iov;
|
||||
msg.msg_iovlen = 1;
|
||||
|
||||
msg.msg_control = cmsg_buf;
|
||||
msg.msg_controllen = sizeof(cmsg_buf);
|
||||
|
||||
ret = recvmsg(fd, &msg, 0);
|
||||
} else {
|
||||
ret = recv(fd, rbuf, sizeof(rbuf), 0);
|
||||
}
|
||||
|
||||
if (ret == -1)
|
||||
error(1, errno, "recv");
|
||||
if (ret != expected_len)
|
||||
|
|
@ -292,6 +336,12 @@ static void do_rx(int fd, int expected_len, char *expected)
|
|||
if (memcmp(rbuf, expected, ret))
|
||||
error(1, 0, "recv: data mismatch");
|
||||
|
||||
if (aux) {
|
||||
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
|
||||
|
||||
check_aux_data(cmsg, expected_len);
|
||||
}
|
||||
|
||||
fprintf(stderr, "rx: %u\n", ret);
|
||||
}
|
||||
|
||||
|
|
@ -312,6 +362,10 @@ static int setup_sniffer(void)
|
|||
if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &one, sizeof(one)))
|
||||
error(1, errno, "setsockopt SO_RCVBUF");
|
||||
|
||||
if (cfg_aux_data)
|
||||
if (setsockopt(fd, SOL_PACKET, PACKET_AUXDATA, &one, sizeof(one)))
|
||||
error(1, errno, "setsockopt PACKET_AUXDATA");
|
||||
|
||||
pair_udp_setfilter(fd);
|
||||
do_bind(fd);
|
||||
|
||||
|
|
@ -322,8 +376,11 @@ static void parse_opts(int argc, char **argv)
|
|||
{
|
||||
int c;
|
||||
|
||||
while ((c = getopt(argc, argv, "bcCdDgl:qt:vV")) != -1) {
|
||||
while ((c = getopt(argc, argv, "abcCdDgl:qt:vV")) != -1) {
|
||||
switch (c) {
|
||||
case 'a':
|
||||
cfg_aux_data = true;
|
||||
break;
|
||||
case 'b':
|
||||
cfg_use_bind = true;
|
||||
break;
|
||||
|
|
@ -373,6 +430,9 @@ static void parse_opts(int argc, char **argv)
|
|||
|
||||
if (cfg_use_gso && !cfg_use_csum_off)
|
||||
error(1, 0, "option gso (-g) requires csum offload (-c)");
|
||||
|
||||
if (cfg_aux_data && cfg_drop)
|
||||
error(1, 0, "option aux data (-a) conflicts with drop (-D)");
|
||||
}
|
||||
|
||||
static void check_packet_stats(int fd)
|
||||
|
|
@ -432,11 +492,11 @@ static void run_test(void)
|
|||
/* BPF filter accepts only this length, vlan changes MAC */
|
||||
if (cfg_payload_len == DATA_LEN && !cfg_use_vlan) {
|
||||
do_rx(fds, total_len - sizeof(struct virtio_net_hdr),
|
||||
tbuf + sizeof(struct virtio_net_hdr));
|
||||
tbuf + sizeof(struct virtio_net_hdr), true);
|
||||
check_packet_stats(fds);
|
||||
}
|
||||
|
||||
do_rx(fdr, cfg_payload_len, tbuf + total_len - cfg_payload_len);
|
||||
do_rx(fdr, cfg_payload_len, tbuf + total_len - cfg_payload_len, false);
|
||||
|
||||
out:
|
||||
if (close(fds))
|
||||
|
|
|
|||
|
|
@ -97,4 +97,9 @@ echo "raw gso max size + 1 (expected to fail)"
|
|||
echo "test drops statistics"
|
||||
./in_netns.sh ./psock_snd -D
|
||||
|
||||
# test aux data
|
||||
|
||||
echo "test aux data"
|
||||
./in_netns.sh ./psock_snd -a
|
||||
|
||||
echo "OK. All tests passed"
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user