Commit a581ed81 authored by Jakub Kicinski's avatar Jakub Kicinski
Browse files

Merge branch 'net-af_packet-allow-joining-a-fanout-when-link-is-down'

Gur Stavi says:

====================
net: af_packet: allow joining a fanout when link is down

PACKET socket can retain its fanout membership through link down and up
and leave a fanout while closed regardless of link state.
However, socket was forbidden from joining a fanout while it was not
RUNNING.

This scenario was identified while studying DPDK pmd_af_packet_drv.
Since sockets are only created during initialization, there is no reason
to fail the initialization if a single link is temporarily down.

This patch allows PACKET socket to join a fanout while not RUNNING.

Selftest psock_fanout is extended to test this "fanout while link down"
scenario.

Selftest psock_fanout is also extended to test fanout create/join by
socket that did not bind or specified a protocol, which carries an
implicit bind.

v3: https://lore.kernel.org/cover.1728555449.git.gur.stavi@huawei.com
v2: https://lore.kernel.org/cover.1728382839.git.gur.stavi@huawei.com
v1: https://lore.kernel.org/cover.1728303615.git.gur.stavi@huawei.com
====================

Link: https://patch.msgid.link/cover.1728802323.git.gur.stavi@huawei.com


Signed-off-by: default avatarJakub Kicinski <kuba@kernel.org>
parents e3fc5139 7ec02a3a
Loading
Loading
Loading
Loading
+5 −4
Original line number Diff line number Diff line
@@ -1846,21 +1846,22 @@ static int fanout_add(struct sock *sk, struct fanout_args *args)
	err = -EINVAL;

	spin_lock(&po->bind_lock);
	if (packet_sock_flag(po, PACKET_SOCK_RUNNING) &&
	if (po->num &&
	    match->type == type &&
	    match->prot_hook.type == po->prot_hook.type &&
	    match->prot_hook.dev == po->prot_hook.dev) {
		err = -ENOSPC;
		if (refcount_read(&match->sk_ref) < match->max_num_members) {
			__dev_remove_pack(&po->prot_hook);

			/* Paired with packet_setsockopt(PACKET_FANOUT_DATA) */
			WRITE_ONCE(po->fanout, match);

			po->rollover = rollover;
			rollover = NULL;
			refcount_set(&match->sk_ref, refcount_read(&match->sk_ref) + 1);
			if (packet_sock_flag(po, PACKET_SOCK_RUNNING)) {
				__dev_remove_pack(&po->prot_hook);
				__fanout_link(sk, po);
			}
			err = 0;
		}
	}
+75 −3
Original line number Diff line number Diff line
@@ -48,6 +48,7 @@
#include <string.h>
#include <sys/mman.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
@@ -59,6 +60,33 @@

static uint32_t cfg_max_num_members;

static void loopback_set_up_down(int state_up)
{
	struct ifreq ifreq = {};
	int fd, err;

	fd = socket(AF_PACKET, SOCK_RAW, 0);
	if (fd < 0) {
		perror("socket loopback");
		exit(1);
	}
	strcpy(ifreq.ifr_name, "lo");
	err = ioctl(fd, SIOCGIFFLAGS, &ifreq);
	if (err) {
		perror("SIOCGIFFLAGS");
		exit(1);
	}
	if (state_up != !!(ifreq.ifr_flags & IFF_UP)) {
		ifreq.ifr_flags ^= IFF_UP;
		err = ioctl(fd, SIOCSIFFLAGS, &ifreq);
		if (err) {
			perror("SIOCSIFFLAGS");
			exit(1);
		}
	}
	close(fd);
}

/* Open a socket in a given fanout mode.
 * @return -1 if mode is bad, a valid socket otherwise */
static int sock_fanout_open(uint16_t typeflags, uint16_t group_id)
@@ -251,6 +279,41 @@ static int sock_fanout_read(int fds[], char *rings[], const int expect[])
	return 0;
}

/* Test that creating/joining a fanout group fails for unbound socket without
 * a specified protocol
 */
static void test_unbound_fanout(void)
{
	int val, fd0, fd1, err;

	fprintf(stderr, "test: unbound fanout\n");
	fd0 = socket(PF_PACKET, SOCK_RAW, 0);
	if (fd0 < 0) {
		perror("socket packet");
		exit(1);
	}
	/* Try to create a new fanout group. Should fail. */
	val = (PACKET_FANOUT_HASH << 16) | 1;
	err = setsockopt(fd0, SOL_PACKET, PACKET_FANOUT, &val, sizeof(val));
	if (!err) {
		fprintf(stderr, "ERROR: unbound socket fanout create\n");
		exit(1);
	}
	fd1 = sock_fanout_open(PACKET_FANOUT_HASH, 1);
	if (fd1 == -1) {
		fprintf(stderr, "ERROR: failed to open HASH socket\n");
		exit(1);
	}
	/* Try to join an existing fanout group. Should fail. */
	err = setsockopt(fd0, SOL_PACKET, PACKET_FANOUT, &val, sizeof(val));
	if (!err) {
		fprintf(stderr, "ERROR: unbound socket fanout join\n");
		exit(1);
	}
	close(fd0);
	close(fd1);
}

/* Test illegal mode + flag combination */
static void test_control_single(void)
{
@@ -264,10 +327,13 @@ static void test_control_single(void)
}

/* Test illegal group with different modes or flags */
static void test_control_group(void)
static void test_control_group(int toggle)
{
	int fds[2];

	if (toggle)
		fprintf(stderr, "test: control multiple sockets with link down toggle\n");
	else
		fprintf(stderr, "test: control multiple sockets\n");

	fds[0] = sock_fanout_open(PACKET_FANOUT_HASH, 0);
@@ -275,6 +341,8 @@ static void test_control_group(void)
		fprintf(stderr, "ERROR: failed to open HASH socket\n");
		exit(1);
	}
	if (toggle)
		loopback_set_up_down(0);
	if (sock_fanout_open(PACKET_FANOUT_HASH |
			       PACKET_FANOUT_FLAG_DEFRAG, 0) != -1) {
		fprintf(stderr, "ERROR: joined group with wrong flag defrag\n");
@@ -294,6 +362,8 @@ static void test_control_group(void)
		fprintf(stderr, "ERROR: failed to join group\n");
		exit(1);
	}
	if (toggle)
		loopback_set_up_down(1);
	if (close(fds[1]) || close(fds[0])) {
		fprintf(stderr, "ERROR: closing sockets\n");
		exit(1);
@@ -488,8 +558,10 @@ int main(int argc, char **argv)
	const int expect_uniqueid[2][2] = { { 20, 20},  { 20, 20 } };
	int port_off = 2, tries = 20, ret;

	test_unbound_fanout();
	test_control_single();
	test_control_group();
	test_control_group(0);
	test_control_group(1);
	test_control_group_max_num_members();
	test_unique_fanout_group_ids();