tools/testing/selftests/bpf/prog_tests/vrf_socket_lookup.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/vrf_socket_lookup.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/prog_tests/vrf_socket_lookup.c
Extension
.c
Size
8913 bytes
Lines
313
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause

/*
 * Topology:
 * ---------
 *     NS0 namespace         |   NS1 namespace
 *			     |
 *     +--------------+      |   +--------------+
 *     |    veth01    |----------|    veth10    |
 *     | 172.16.1.100 |      |   | 172.16.1.200 |
 *     |     bpf      |      |   +--------------+
 *     +--------------+      |
 *      server(UDP/TCP)      |
 *  +-------------------+    |
 *  |        vrf1       |    |
 *  |  +--------------+ |    |   +--------------+
 *  |  |    veth02    |----------|    veth20    |
 *  |  | 172.16.2.100 | |    |   | 172.16.2.200 |
 *  |  |     bpf      | |    |   +--------------+
 *  |  +--------------+ |    |
 *  |   server(UDP/TCP) |    |
 *  +-------------------+    |
 *
 * Test flow
 * -----------
 *  The tests verifies that socket lookup via TC is VRF aware:
 *  1) Creates two veth pairs between NS0 and NS1:
 *     a) veth01 <-> veth10 outside the VRF
 *     b) veth02 <-> veth20 in the VRF
 *  2) Attaches to veth01 and veth02 a program that calls:
 *     a) bpf_skc_lookup_tcp() with TCP and tcp_skc is true
 *     b) bpf_sk_lookup_tcp() with TCP and tcp_skc is false
 *     c) bpf_sk_lookup_udp() with UDP
 *     The program stores the lookup result in bss->lookup_status.
 *  3) Creates a socket TCP/UDP server in/outside the VRF.
 *  4) The test expects lookup_status to be:
 *     a) 0 from device in VRF to server outside VRF
 *     b) 0 from device outside VRF to server in VRF
 *     c) 1 from device in VRF to server in VRF
 *     d) 1 from device outside VRF to server outside VRF
 */

#include <net/if.h>

#include "test_progs.h"
#include "network_helpers.h"
#include "vrf_socket_lookup.skel.h"

#define NS0 "vrf_socket_lookup_0"
#define NS1 "vrf_socket_lookup_1"

#define IP4_ADDR_VETH01 "172.16.1.100"
#define IP4_ADDR_VETH10 "172.16.1.200"
#define IP4_ADDR_VETH02 "172.16.2.100"
#define IP4_ADDR_VETH20 "172.16.2.200"

#define NON_VRF_PORT 5000
#define IN_VRF_PORT 5001

#define TIMEOUT_MS 3000

static int make_socket(int sotype, const char *ip, int port,
		       struct sockaddr_storage *addr)
{
	int err, fd;

	err = make_sockaddr(AF_INET, ip, port, addr, NULL);
	if (!ASSERT_OK(err, "make_address"))
		return -1;

	fd = socket(AF_INET, sotype, 0);
	if (!ASSERT_GE(fd, 0, "socket"))
		return -1;

	if (!ASSERT_OK(settimeo(fd, TIMEOUT_MS), "settimeo"))
		goto fail;

	return fd;
fail:
	close(fd);
	return -1;
}

static int make_server(int sotype, const char *ip, int port, const char *ifname)
{
	int err, fd = -1;

	fd = start_server(AF_INET, sotype, ip, port, TIMEOUT_MS);
	if (!ASSERT_GE(fd, 0, "start_server"))
		return -1;

Annotation

Implementation Notes