Files
linux/lib/tests/blackhole_dev_kunit.c
Alice Mikityanska 5329647dad net: Use helpers to get/set UDP len tree-wide
Since BIG TCP for UDP tunnels will start using len=0 in the UDP header
as an indicator of a GSO packet bigger than 65535 bytes, this commit
introduces the following getter and setters to use tree-wide, in order
to explicitly mark places where len=0 may be expected, and handle them
properly:

1. udp_set_len() sets uh->len to its real value if it's not bigger than
65535, and to 0 otherwise: to be used in GSO context with aggregated
packets.

2. udp_set_len_short() is to be used when the length is known to fit 16
bits. It WARNs when the caller tries to assign a bigger value if
CONFIG_DEBUG_NET=y.

3. udp_get_len_short() returns len in host byte order: to be used on the
RX side to deal with non-aggregated packets, or to access the raw value
of the len field.

4. udp_get_len() decodes uh->len set by udp_set_len(). It checks whether
the packet is GSO to guard from malformed packets.

At the moment udp_set_len() is not used, a following commit will start
using it after enabling len>65535 for GSO.

Raw uh->len (in network byte order) is still accessed in a few places
for checksum calculation purposes, and to decode len=0 in udpv6_rcv for
jumbograms. udp_rcv and udpv6_rcv will be addressed by the commit that
starts using udp_set_len() to set UDP len=0 for BIG TCP packets in UDP
tunnels.

Signed-off-by: Alice Mikityanska <alice@isovalent.com>
Reviewed-by: Willem de Bruijn <willemb@google.com>
Acked-by: Jason A. Donenfeld <Jason@zx2c4.com>
Link: https://patch.msgid.link/20260710134242.216538-2-alice.kernel@fastmail.im
Reviewed-by: Nikolay Aleksandrov <razor@blackwall.org>
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
2026-07-22 13:47:02 +02:00

86 lines
2.3 KiB
C

// SPDX-License-Identifier: GPL-2.0
/*
* This tests the blackhole_dev that is created during the
* net subsystem initialization. The test this module performs is
* by injecting an skb into the stack with skb->dev as the
* blackhole_dev and expects kernel to behave in a sane manner
* (in other words, *not crash*)!
*
* Copyright (c) 2018, Mahesh Bandewar <maheshb@google.com>
*/
#include <kunit/test.h>
#include <linux/module.h>
#include <linux/skbuff.h>
#include <linux/netdevice.h>
#include <linux/udp.h>
#include <linux/ipv6.h>
#include <net/dst.h>
#define SKB_SIZE 256
#define HEAD_SIZE (14+40+8) /* Ether + IPv6 + UDP */
#define TAIL_SIZE 32 /* random tail-room */
#define UDP_PORT 1234
static void test_blackholedev(struct kunit *test)
{
struct ipv6hdr *ip6h;
struct sk_buff *skb;
struct udphdr *uh;
int data_len;
skb = alloc_skb(SKB_SIZE, GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, skb);
/* Reserve head-room for the headers */
skb_reserve(skb, HEAD_SIZE);
/* Add data to the skb */
data_len = SKB_SIZE - (HEAD_SIZE + TAIL_SIZE);
memset(__skb_put(skb, data_len), 0xf, data_len);
/* Add protocol data */
/* (Transport) UDP */
uh = (struct udphdr *)skb_push(skb, sizeof(struct udphdr));
skb_set_transport_header(skb, 0);
uh->source = uh->dest = htons(UDP_PORT);
udp_set_len_short(uh, data_len);
uh->check = 0;
/* (Network) IPv6 */
ip6h = (struct ipv6hdr *)skb_push(skb, sizeof(struct ipv6hdr));
skb_set_network_header(skb, 0);
ip6h->hop_limit = 32;
ip6h->payload_len = htons(data_len + sizeof(struct udphdr));
ip6h->nexthdr = IPPROTO_UDP;
ip6h->saddr = in6addr_loopback;
ip6h->daddr = in6addr_loopback;
/* Ether */
skb_push(skb, sizeof(struct ethhdr));
skb_set_mac_header(skb, 0);
skb->protocol = htons(ETH_P_IPV6);
skb->pkt_type = PACKET_HOST;
skb->dev = blackhole_netdev;
/* Now attempt to send the packet */
KUNIT_EXPECT_EQ(test, dev_queue_xmit(skb), NET_XMIT_SUCCESS);
}
static struct kunit_case blackholedev_cases[] = {
KUNIT_CASE(test_blackholedev),
{},
};
static struct kunit_suite blackholedev_suite = {
.name = "blackholedev",
.test_cases = blackholedev_cases,
};
kunit_test_suite(blackholedev_suite);
MODULE_AUTHOR("Mahesh Bandewar <maheshb@google.com>");
MODULE_DESCRIPTION("module test of the blackhole_dev");
MODULE_LICENSE("GPL");