* [PATCH net-next 0/5] Add PACKET_IGNORE_OUTGOING selftest for packet sockets
@ 2026-07-28 16:09 Joe Damato
2026-07-28 16:09 ` [PATCH net-next 1/5] selftests/net: psock: Generalize psock bind Joe Damato
` (4 more replies)
0 siblings, 5 replies; 6+ messages in thread
From: Joe Damato @ 2026-07-28 16:09 UTC (permalink / raw)
To: netdev
Cc: andrew+netdev, davem, edumazet, kuba, pabeni, horms, willemb,
linux-kselftest, shuah, Joe Damato, linux-kernel
Greetings:
Following up on my previous series [1] and further extending the selftests
for packet sockets to test PACKET_IGNORE_OUTGOING.
I re-ran the tests and the existing and new tests all pass.
Thanks,
Joe
[1]: https://lore.kernel.org/netdev/20260720122714.759175-1-joe@dama.to/
Joe Damato (5):
selftests/net: psock: Generalize psock bind
selftests/net: psock: Generalize stats check
selftests/net: psock_snd: unify on recvmsg()
selftests/net: psock_snd: Verify sll_pkttype
selftests/net: test PACKET_IGNORE_OUTGOING
tools/testing/selftests/net/psock_snd.c | 147 ++++++++++++++++++-----
tools/testing/selftests/net/psock_snd.sh | 5 +
2 files changed, 124 insertions(+), 28 deletions(-)
base-commit: b515dc54795ef370be3cb396e7c12ad91686b6d1
--
2.53.0-Meta
^ permalink raw reply [flat|nested] 6+ messages in thread
* [PATCH net-next 1/5] selftests/net: psock: Generalize psock bind
2026-07-28 16:09 [PATCH net-next 0/5] Add PACKET_IGNORE_OUTGOING selftest for packet sockets Joe Damato
@ 2026-07-28 16:09 ` Joe Damato
2026-07-28 16:09 ` [PATCH net-next 2/5] selftests/net: psock: Generalize stats check Joe Damato
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Joe Damato @ 2026-07-28 16:09 UTC (permalink / raw)
To: netdev, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Shuah Khan
Cc: andrew+netdev, willemb, linux-kselftest, Joe Damato, linux-kernel
Generalize packet socket binding so that types other than ETH_P_IP can
be used.
Signed-off-by: Joe Damato <joe@dama.to>
---
tools/testing/selftests/net/psock_snd.c | 9 +++++++--
1 file changed, 7 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/net/psock_snd.c b/tools/testing/selftests/net/psock_snd.c
index 3313a15e0ca1..bb9a9cfdbe95 100644
--- a/tools/testing/selftests/net/psock_snd.c
+++ b/tools/testing/selftests/net/psock_snd.c
@@ -171,12 +171,12 @@ static int build_packet(int payload_len)
return off + payload_len;
}
-static void do_bind(int fd)
+static void do_bind_proto(int fd, uint16_t proto)
{
struct sockaddr_ll laddr = {0};
laddr.sll_family = AF_PACKET;
- laddr.sll_protocol = htons(ETH_P_IP);
+ laddr.sll_protocol = htons(proto);
laddr.sll_ifindex = if_nametoindex(cfg_ifname);
if (!laddr.sll_ifindex)
error(1, errno, "if_nametoindex");
@@ -185,6 +185,11 @@ static void do_bind(int fd)
error(1, errno, "bind");
}
+static void do_bind(int fd)
+{
+ do_bind_proto(fd, ETH_P_IP);
+}
+
static void do_send(int fd, char *buf, int len)
{
int ret;
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH net-next 2/5] selftests/net: psock: Generalize stats check
2026-07-28 16:09 [PATCH net-next 0/5] Add PACKET_IGNORE_OUTGOING selftest for packet sockets Joe Damato
2026-07-28 16:09 ` [PATCH net-next 1/5] selftests/net: psock: Generalize psock bind Joe Damato
@ 2026-07-28 16:09 ` Joe Damato
2026-07-28 16:09 ` [PATCH net-next 3/5] selftests/net: psock_snd: unify on recvmsg() Joe Damato
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: Joe Damato @ 2026-07-28 16:09 UTC (permalink / raw)
To: netdev, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Shuah Khan
Cc: andrew+netdev, willemb, linux-kselftest, Joe Damato, linux-kernel
Generalize stats check code so that callers can specify the expected
packets.
Signed-off-by: Joe Damato <joe@dama.to>
---
tools/testing/selftests/net/psock_snd.c | 11 ++++++-----
1 file changed, 6 insertions(+), 5 deletions(-)
diff --git a/tools/testing/selftests/net/psock_snd.c b/tools/testing/selftests/net/psock_snd.c
index bb9a9cfdbe95..d506ea19491b 100644
--- a/tools/testing/selftests/net/psock_snd.c
+++ b/tools/testing/selftests/net/psock_snd.c
@@ -440,7 +440,7 @@ static void parse_opts(int argc, char **argv)
error(1, 0, "option aux data (-a) conflicts with drop (-D)");
}
-static void check_packet_stats(int fd)
+static void check_packet_stats(int fd, unsigned int expected_packets)
{
struct tpacket_stats st = {};
socklen_t len = sizeof(st);
@@ -459,8 +459,9 @@ static void check_packet_stats(int fd)
if (st.tp_drops == 0)
error(1, 0, "stats: expected drops but tp_drops == 0");
} else {
- if (st.tp_packets != 1)
- error(1, 0, "stats: tp_packets %u != 1", st.tp_packets);
+ if (st.tp_packets != expected_packets)
+ error(1, 0, "stats: tp_packets %u != %u",
+ st.tp_packets, expected_packets);
if (st.tp_drops != 0)
error(1, 0, "stats: tp_drops %u != 0", st.tp_drops);
@@ -490,7 +491,7 @@ static void run_test(void)
total_len = do_tx();
if (cfg_drop) {
- check_packet_stats(fds);
+ check_packet_stats(fds, 0);
goto out;
}
@@ -498,7 +499,7 @@ static void run_test(void)
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), true);
- check_packet_stats(fds);
+ check_packet_stats(fds, 1);
}
do_rx(fdr, cfg_payload_len, tbuf + total_len - cfg_payload_len, false);
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH net-next 3/5] selftests/net: psock_snd: unify on recvmsg()
2026-07-28 16:09 [PATCH net-next 0/5] Add PACKET_IGNORE_OUTGOING selftest for packet sockets Joe Damato
2026-07-28 16:09 ` [PATCH net-next 1/5] selftests/net: psock: Generalize psock bind Joe Damato
2026-07-28 16:09 ` [PATCH net-next 2/5] selftests/net: psock: Generalize stats check Joe Damato
@ 2026-07-28 16:09 ` Joe Damato
2026-07-28 16:09 ` [PATCH net-next 4/5] selftests/net: psock_snd: Verify sll_pkttype Joe Damato
2026-07-28 16:09 ` [PATCH net-next 5/5] selftests/net: test PACKET_IGNORE_OUTGOING Joe Damato
4 siblings, 0 replies; 6+ messages in thread
From: Joe Damato @ 2026-07-28 16:09 UTC (permalink / raw)
To: netdev, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Shuah Khan
Cc: andrew+netdev, willemb, linux-kselftest, Joe Damato, linux-kernel
Don't maintain two RX paths in do_rx(). Unify on recvmsg() and pass aux
data only when needed.
No functional change.
Signed-off-by: Joe Damato <joe@dama.to>
---
tools/testing/selftests/net/psock_snd.c | 28 ++++++++++---------------
1 file changed, 11 insertions(+), 17 deletions(-)
diff --git a/tools/testing/selftests/net/psock_snd.c b/tools/testing/selftests/net/psock_snd.c
index d506ea19491b..b2593daca603 100644
--- a/tools/testing/selftests/net/psock_snd.c
+++ b/tools/testing/selftests/net/psock_snd.c
@@ -314,25 +314,22 @@ 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];
+ struct iovec iov = {
+ .iov_base = rbuf,
+ .iov_len = sizeof(rbuf),
+ };
+ struct msghdr msg = {
+ .msg_iov = &iov,
+ .msg_iovlen = 1,
+ };
int ret;
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);
}
+ ret = recvmsg(fd, &msg, 0);
if (ret == -1)
error(1, errno, "recv");
if (ret != expected_len)
@@ -341,11 +338,8 @@ static void do_rx(int fd, int expected_len, char *expected, bool is_psock)
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);
- }
+ if (aux)
+ check_aux_data(CMSG_FIRSTHDR(&msg), expected_len);
fprintf(stderr, "rx: %u\n", ret);
}
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH net-next 4/5] selftests/net: psock_snd: Verify sll_pkttype
2026-07-28 16:09 [PATCH net-next 0/5] Add PACKET_IGNORE_OUTGOING selftest for packet sockets Joe Damato
` (2 preceding siblings ...)
2026-07-28 16:09 ` [PATCH net-next 3/5] selftests/net: psock_snd: unify on recvmsg() Joe Damato
@ 2026-07-28 16:09 ` Joe Damato
2026-07-28 16:09 ` [PATCH net-next 5/5] selftests/net: test PACKET_IGNORE_OUTGOING Joe Damato
4 siblings, 0 replies; 6+ messages in thread
From: Joe Damato @ 2026-07-28 16:09 UTC (permalink / raw)
To: netdev, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Shuah Khan
Cc: andrew+netdev, willemb, linux-kselftest, Joe Damato, linux-kernel
Extend do_rx() to take an expected packet type. Existing callers pass -1
to skip the check.
Signed-off-by: Joe Damato <joe@dama.to>
---
tools/testing/selftests/net/psock_snd.c | 17 ++++++++++++++---
1 file changed, 14 insertions(+), 3 deletions(-)
diff --git a/tools/testing/selftests/net/psock_snd.c b/tools/testing/selftests/net/psock_snd.c
index b2593daca603..f23877942ea3 100644
--- a/tools/testing/selftests/net/psock_snd.c
+++ b/tools/testing/selftests/net/psock_snd.c
@@ -310,10 +310,13 @@ static void check_aux_data(struct cmsghdr *cmsg, int 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)
+/* expected_pkttype < 0 skips the sll_pkttype check. */
+static void do_rx(int fd, int expected_len, char *expected, bool is_psock,
+ int expected_pkttype)
{
char cmsg_buf[1024] __attribute__((aligned(8))) = {};
bool aux = is_psock && cfg_aux_data;
+ struct sockaddr_ll saddr = {};
struct iovec iov = {
.iov_base = rbuf,
.iov_len = sizeof(rbuf),
@@ -328,6 +331,10 @@ static void do_rx(int fd, int expected_len, char *expected, bool is_psock)
msg.msg_control = cmsg_buf;
msg.msg_controllen = sizeof(cmsg_buf);
}
+ if (is_psock) {
+ msg.msg_name = &saddr;
+ msg.msg_namelen = sizeof(saddr);
+ }
ret = recvmsg(fd, &msg, 0);
if (ret == -1)
@@ -341,6 +348,10 @@ static void do_rx(int fd, int expected_len, char *expected, bool is_psock)
if (aux)
check_aux_data(CMSG_FIRSTHDR(&msg), expected_len);
+ if (expected_pkttype >= 0 && saddr.sll_pkttype != expected_pkttype)
+ error(1, 0, "recv: sll_pkttype %d != %d",
+ saddr.sll_pkttype, expected_pkttype);
+
fprintf(stderr, "rx: %u\n", ret);
}
@@ -492,11 +503,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), true);
+ tbuf + sizeof(struct virtio_net_hdr), true, -1);
check_packet_stats(fds, 1);
}
- do_rx(fdr, cfg_payload_len, tbuf + total_len - cfg_payload_len, false);
+ do_rx(fdr, cfg_payload_len, tbuf + total_len - cfg_payload_len, false, -1);
out:
if (close(fds))
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 6+ messages in thread
* [PATCH net-next 5/5] selftests/net: test PACKET_IGNORE_OUTGOING
2026-07-28 16:09 [PATCH net-next 0/5] Add PACKET_IGNORE_OUTGOING selftest for packet sockets Joe Damato
` (3 preceding siblings ...)
2026-07-28 16:09 ` [PATCH net-next 4/5] selftests/net: psock_snd: Verify sll_pkttype Joe Damato
@ 2026-07-28 16:09 ` Joe Damato
4 siblings, 0 replies; 6+ messages in thread
From: Joe Damato @ 2026-07-28 16:09 UTC (permalink / raw)
To: netdev, David S. Miller, Eric Dumazet, Jakub Kicinski,
Paolo Abeni, Simon Horman, Shuah Khan
Cc: andrew+netdev, willemb, linux-kselftest, Joe Damato, linux-kernel
Test that setsockopt PACKET_IGNORE_OUTGOING:
- works with valid values (0 and 1),
- rejects values outside of that range, and
When enabled for a loopback sniffer, it only produces 1 copy of the
packet (rx) instead of two copies (tx and rx). Use sll_pkttype to check
the direction of the packet to ensure the correct packet is sniffed.
Signed-off-by: Joe Damato <joe@dama.to>
---
tools/testing/selftests/net/psock_snd.c | 84 +++++++++++++++++++++++-
tools/testing/selftests/net/psock_snd.sh | 5 ++
2 files changed, 87 insertions(+), 2 deletions(-)
diff --git a/tools/testing/selftests/net/psock_snd.c b/tools/testing/selftests/net/psock_snd.c
index f23877942ea3..0f6a30b26912 100644
--- a/tools/testing/selftests/net/psock_snd.c
+++ b/tools/testing/selftests/net/psock_snd.c
@@ -41,6 +41,7 @@ static bool cfg_use_vlan;
static bool cfg_use_vnet;
static bool cfg_drop;
static bool cfg_aux_data;
+static bool cfg_ignore_outgoing;
static char *cfg_ifname = "lo";
static int cfg_mtu = 1500;
@@ -377,7 +378,14 @@ static int setup_sniffer(void)
error(1, errno, "setsockopt PACKET_AUXDATA");
pair_udp_setfilter(fd);
- do_bind(fd);
+
+ /* binding to ETH_P_ALL adds the sniffer to ptype_all, which will see
+ * the dev_queue_xmit_nit copy. ignore_outgoing should suppress this.
+ */
+ if (cfg_ignore_outgoing)
+ do_bind_proto(fd, ETH_P_ALL);
+ else
+ do_bind(fd);
return fd;
}
@@ -386,7 +394,7 @@ static void parse_opts(int argc, char **argv)
{
int c;
- while ((c = getopt(argc, argv, "abcCdDgl:qt:vV")) != -1) {
+ while ((c = getopt(argc, argv, "abcCdDgil:qt:vV")) != -1) {
switch (c) {
case 'a':
cfg_aux_data = true;
@@ -409,6 +417,9 @@ static void parse_opts(int argc, char **argv)
case 'g':
cfg_use_gso = true;
break;
+ case 'i':
+ cfg_ignore_outgoing = true;
+ break;
case 'l':
cfg_payload_len = strtoul(optarg, NULL, 0);
break;
@@ -443,6 +454,10 @@ static void parse_opts(int argc, char **argv)
if (cfg_aux_data && cfg_drop)
error(1, 0, "option aux data (-a) conflicts with drop (-D)");
+
+ if (cfg_ignore_outgoing && (cfg_drop || cfg_aux_data))
+ error(1, 0,
+ "option ignore outgoing (-i) conflicts with -D and -a");
}
static void check_packet_stats(int fd, unsigned int expected_packets)
@@ -486,6 +501,66 @@ static void check_packet_stats(int fd, unsigned int expected_packets)
error(1, 0, "stats: tp_drops %u != 0 after clear", st.tp_drops);
}
+static void set_ignore_outgoing(int fd, int val)
+{
+ socklen_t len = sizeof(int);
+ int got = -1;
+
+ if (setsockopt(fd, SOL_PACKET, PACKET_IGNORE_OUTGOING,
+ &val, sizeof(val)))
+ error(1, errno, "setsockopt PACKET_IGNORE_OUTGOING %d", val);
+
+ if (getsockopt(fd, SOL_PACKET, PACKET_IGNORE_OUTGOING, &got, &len))
+ error(1, errno, "getsockopt PACKET_IGNORE_OUTGOING");
+ if (got != val)
+ error(1, 0, "getsockopt: expected %d got %d", val, got);
+}
+
+static void check_ignore_outgoing_range(int fd)
+{
+ int val;
+
+ /* Values outside [0, 1] must be rejected with -EINVAL. */
+ val = 2;
+ if (setsockopt(fd, SOL_PACKET, PACKET_IGNORE_OUTGOING,
+ &val, sizeof(val)) != -1 || errno != EINVAL)
+ error(1, errno,
+ "setsockopt PACKET_IGNORE_OUTGOING val=2: expected EINVAL");
+
+ val = -1;
+ if (setsockopt(fd, SOL_PACKET, PACKET_IGNORE_OUTGOING,
+ &val, sizeof(val)) != -1 || errno != EINVAL)
+ error(1, errno,
+ "setsockopt PACKET_IGNORE_OUTGOING val=-1: expected EINVAL");
+}
+
+static void test_ignore_outgoing(int fds)
+{
+ char *expected = tbuf + sizeof(struct virtio_net_hdr);
+ int expected_len;
+
+ /* ptype_all sniffer on loopback should produce two copies per packet
+ * (RX and TX).
+ */
+ expected_len = do_tx();
+ expected_len -= sizeof(struct virtio_net_hdr);
+ do_rx(fds, expected_len, expected, true, PACKET_OUTGOING);
+ do_rx(fds, expected_len, expected, true, PACKET_HOST);
+ check_packet_stats(fds, 2);
+
+ /* 0 and 1 accepted; anything else rejected. */
+ set_ignore_outgoing(fds, 0);
+ set_ignore_outgoing(fds, 1);
+ check_ignore_outgoing_range(fds);
+
+ /* With PACKET_IGNORE_OUTGOING set, only the rx copy survives. */
+ do_tx();
+ do_rx(fds, expected_len, expected, true, PACKET_HOST);
+ if (recv(fds, rbuf, sizeof(rbuf), 0) != -1 || errno != EAGAIN)
+ error(1, errno, "expected EAGAIN, got extra packet");
+ check_packet_stats(fds, 1);
+}
+
static void run_test(void)
{
int fdr, fds, total_len;
@@ -493,6 +568,11 @@ static void run_test(void)
fdr = setup_rx();
fds = setup_sniffer();
+ if (cfg_ignore_outgoing) {
+ test_ignore_outgoing(fds);
+ goto out;
+ }
+
total_len = do_tx();
if (cfg_drop) {
diff --git a/tools/testing/selftests/net/psock_snd.sh b/tools/testing/selftests/net/psock_snd.sh
index 111c9e2f0d21..7fa0a3297988 100755
--- a/tools/testing/selftests/net/psock_snd.sh
+++ b/tools/testing/selftests/net/psock_snd.sh
@@ -102,4 +102,9 @@ echo "test drops statistics"
echo "test aux data"
./in_netns.sh ./psock_snd -a
+# test ignore outgoing
+
+echo "test ignore outgoing"
+./in_netns.sh ./psock_snd -i
+
echo "OK. All tests passed"
--
2.53.0-Meta
^ permalink raw reply related [flat|nested] 6+ messages in thread
end of thread, other threads:[~2026-07-28 16:10 UTC | newest]
Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-28 16:09 [PATCH net-next 0/5] Add PACKET_IGNORE_OUTGOING selftest for packet sockets Joe Damato
2026-07-28 16:09 ` [PATCH net-next 1/5] selftests/net: psock: Generalize psock bind Joe Damato
2026-07-28 16:09 ` [PATCH net-next 2/5] selftests/net: psock: Generalize stats check Joe Damato
2026-07-28 16:09 ` [PATCH net-next 3/5] selftests/net: psock_snd: unify on recvmsg() Joe Damato
2026-07-28 16:09 ` [PATCH net-next 4/5] selftests/net: psock_snd: Verify sll_pkttype Joe Damato
2026-07-28 16:09 ` [PATCH net-next 5/5] selftests/net: test PACKET_IGNORE_OUTGOING Joe Damato
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox