Subject: Re: [PATCH bpf-next v8 17/18] selftests/bpf: Add AF_INET packet generation to xdp_metadata

From: Stanislav Fomichev <[email-masked]>
Date: Tue, 5 Dec 2023 14:59:07 -0800

On 12/05, Larysa Zaremba wrote:
> The easiest way to simulate stripped VLAN tag in veth is to send a packet
> from VLAN interface, attached to veth. Unfortunately, this approach is
> incompatible with AF_XDP on TX side, because VLAN interfaces do not have
> such feature.
> 
> Check both packets sent via AF_XDP TX and regular socket.
> 
> AF_INET packet will also have a filled-in hash type (XDP_RSS_TYPE_L4),
> unlike AF_XDP packet, so more values can be checked.
> 
> Signed-off-by: Larysa Zaremba <[email-masked]>
> ---
>  .../selftests/bpf/prog_tests/xdp_metadata.c   | 116 +++++++++++++++---
>  1 file changed, 97 insertions(+), 19 deletions(-)
> 
> diff --git a/tools/testing/selftests/bpf/prog_tests/xdp_metadata.c b/tools/testing/selftests/bpf/prog_tests/xdp_metadata.c
> index 33cdf88efa6b..e7f06cbdd845 100644
> --- a/tools/testing/selftests/bpf/prog_tests/xdp_metadata.c
> +++ b/tools/testing/selftests/bpf/prog_tests/xdp_metadata.c
> @@ -20,7 +20,7 @@
>  
>  #define UDP_PAYLOAD_BYTES 4
>  
> -#define AF_XDP_SOURCE_PORT 1234
> +#define UDP_SOURCE_PORT 1234
>  #define AF_XDP_CONSUMER_PORT 8080
>  
>  #define UMEM_NUM 16
> @@ -33,6 +33,12 @@
>  #define RX_ADDR "10.0.0.2"
>  #define PREFIX_LEN "8"
>  #define FAMILY AF_INET
> +#define TX_NETNS_NAME "xdp_metadata_tx"
> +#define RX_NETNS_NAME "xdp_metadata_rx"
> +#define TX_MAC "00:00:00:00:00:01"
> +#define RX_MAC "00:00:00:00:00:02"
> +
> +#define XDP_RSS_TYPE_L4 BIT(3)
>  
>  struct xsk {
>  	void *umem_area;
> @@ -181,7 +187,7 @@ static int generate_packet(struct xsk *xsk, __u16 dst_port)
>  	ASSERT_EQ(inet_pton(FAMILY, RX_ADDR, &iph->daddr), 1, "inet_pton(RX_ADDR)");
>  	ip_csum(iph);
>  
> -	udph->source = htons(AF_XDP_SOURCE_PORT);
> +	udph->source = htons(UDP_SOURCE_PORT);
>  	udph->dest = htons(dst_port);
>  	udph->len = htons(sizeof(*udph) + UDP_PAYLOAD_BYTES);
>  	udph->check = ~csum_tcpudp_magic(iph->saddr, iph->daddr,
> @@ -204,6 +210,30 @@ static int generate_packet(struct xsk *xsk, __u16 dst_port)
>  	return 0;
>  }
>  
> +static int generate_packet_inet(void)
> +{
> +	char udp_payload[UDP_PAYLOAD_BYTES];
> +	struct sockaddr_in rx_addr;
> +	int sock_fd, err = 0;
> +
> +	/* Build a packet */
> +	memset(udp_payload, 0xAA, UDP_PAYLOAD_BYTES);
> +	rx_addr.sin_addr.s_addr = inet_addr(RX_ADDR);
> +	rx_addr.sin_family = AF_INET;
> +	rx_addr.sin_port = htons(AF_XDP_CONSUMER_PORT);
> +
> +	sock_fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
> +	if (!ASSERT_GE(sock_fd, 0, "socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)"))
> +		return sock_fd;
> +
> +	err = sendto(sock_fd, udp_payload, UDP_PAYLOAD_BYTES, MSG_DONTWAIT,
> +		     (void *)&rx_addr, sizeof(rx_addr));
> +	ASSERT_GE(err, 0, "sendto");
> +
> +	close(sock_fd);
> +	return err;
> +}
> +
>  static void complete_tx(struct xsk *xsk)
>  {
>  	struct xsk_tx_metadata *meta;
> @@ -236,7 +266,7 @@ static void refill_rx(struct xsk *xsk, __u64 addr)
>  	}
>  }
>  
> -static int verify_xsk_metadata(struct xsk *xsk)
> +static int verify_xsk_metadata(struct xsk *xsk, bool sent_from_af_xdp)
>  {
>  	const struct xdp_desc *rx_desc;
>  	struct pollfd fds = {};
> @@ -290,17 +320,36 @@ static int verify_xsk_metadata(struct xsk *xsk)
>  	if (!ASSERT_NEQ(meta->rx_hash, 0, "rx_hash"))
>  		return -1;
>  
> +	if (!sent_from_af_xdp) {
> +		if (!ASSERT_NEQ(meta->rx_hash_type & XDP_RSS_TYPE_L4, 0, "rx_hash_type"))
> +			return -1;
> +		goto done;
> +	}
> +
>  	ASSERT_EQ(meta->rx_hash_type, 0, "rx_hash_type");
>  
>  	/* checksum offload */
>  	ASSERT_EQ(udph->check, htons(0x721c), "csum");
>  
> +done:
>  	xsk_ring_cons__release(&xsk->rx, 1);
>  	refill_rx(xsk, comp_addr);
>  
>  	return 0;
>  }
>  
> +static void switch_ns_to_rx(struct nstoken **tok)
> +{
> +	close_netns(*tok);
> +	*tok = open_netns(RX_NETNS_NAME);
> +}
> +
> +static void switch_ns_to_tx(struct nstoken **tok)
> +{
> +	close_netns(*tok);
> +	*tok = open_netns(TX_NETNS_NAME);
> +}
> +
>  void test_xdp_metadata(void)
>  {
>  	struct xdp_metadata2 *bpf_obj2 = NULL;
> @@ -318,27 +367,31 @@ void test_xdp_metadata(void)
>  	int sock_fd;
>  	int ret;
>  
> -	/* Setup new networking namespace, with a veth pair. */
> +	/* Setup new networking namespaces, with a veth pair. */
> +	SYS(out, "ip netns add " TX_NETNS_NAME);
> +	SYS(out, "ip netns add " RX_NETNS_NAME);
>  
> -	SYS(out, "ip netns add xdp_metadata");
> -	tok = open_netns("xdp_metadata");
> +	tok = open_netns(TX_NETNS_NAME);
>  	SYS(out, "ip link add numtxqueues 1 numrxqueues 1 " TX_NAME
>  	    " type veth peer " RX_NAME " numtxqueues 1 numrxqueues 1");
> -	SYS(out, "ip link set dev " TX_NAME " address 00:00:00:00:00:01");
> -	SYS(out, "ip link set dev " RX_NAME " address 00:00:00:00:00:02");
> +	SYS(out, "ip link set " RX_NAME " netns " RX_NETNS_NAME);
> +
> +	SYS(out, "ip link set dev " TX_NAME " address " TX_MAC);
>  	SYS(out, "ip link set dev " TX_NAME " up");
> -	SYS(out, "ip link set dev " RX_NAME " up");
>  	SYS(out, "ip addr add " TX_ADDR "/" PREFIX_LEN " dev " TX_NAME);
> +
> +	/* Avoid ARP calls */
> +	SYS(out, "ip -4 neigh add " RX_ADDR " lladdr " RX_MAC " dev " TX_NAME);
> +
> +	switch_ns_to_rx(&tok);
> +
> +	SYS(out, "ip link set dev " RX_NAME " address " RX_MAC);
> +	SYS(out, "ip link set dev " RX_NAME " up");
>  	SYS(out, "ip addr add " RX_ADDR "/" PREFIX_LEN " dev " RX_NAME);
>  
>  	rx_ifindex = if_nametoindex(RX_NAME);
> -	tx_ifindex = if_nametoindex(TX_NAME);
>  
> -	/* Setup separate AF_XDP for TX and RX interfaces. */
> -
> -	ret = open_xsk(tx_ifindex, &tx_xsk);
> -	if (!ASSERT_OK(ret, "open_xsk(TX_NAME)"))
> -		goto out;
> +	/* Setup separate AF_XDP for RX interface. */
>  
>  	ret = open_xsk(rx_ifindex, &rx_xsk);
>  	if (!ASSERT_OK(ret, "open_xsk(RX_NAME)"))
> @@ -379,18 +432,38 @@ void test_xdp_metadata(void)
>  	if (!ASSERT_GE(ret, 0, "bpf_map_update_elem"))
>  		goto out;
>  
> -	/* Send packet destined to RX AF_XDP socket. */
> +	switch_ns_to_tx(&tok);
> +
> +	/* Setup separate AF_XDP for TX interface nad send packet to the RX socket. */

Not sure we care, but s/nad/and/ if you happen to do another respin..

Acked-by: Stanislav Fomichev <[email-masked]>