Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: selftests: export packet creation helpers for driver use

Export the network selftest packet creation infrastructure to allow
network drivers to reuse the existing selftest framework instead of
duplicating packet creation code.

Signed-off-by: Raju Rangoju <Raju.Rangoju@amd.com>
Reviewed-by: Maxime Chevallier <maxime.chevallier@bootlin.com>
Link: https://patch.msgid.link/20251031111811.775434-1-Raju.Rangoju@amd.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>

authored by

Raju Rangoju and committed by
Paolo Abeni
6b47af35 0567c84d

+52 -41
+45
include/net/selftests.h
··· 3 3 #define _NET_SELFTESTS 4 4 5 5 #include <linux/ethtool.h> 6 + #include <linux/netdevice.h> 7 + 8 + struct net_packet_attrs { 9 + const unsigned char *src; 10 + const unsigned char *dst; 11 + u32 ip_src; 12 + u32 ip_dst; 13 + bool tcp; 14 + u16 sport; 15 + u16 dport; 16 + int timeout; 17 + int size; 18 + int max_size; 19 + u8 id; 20 + u16 queue_mapping; 21 + bool bad_csum; 22 + }; 23 + 24 + struct net_test_priv { 25 + struct net_packet_attrs *packet; 26 + struct packet_type pt; 27 + struct completion comp; 28 + int double_vlan; 29 + int vlan_id; 30 + int ok; 31 + }; 32 + 33 + struct netsfhdr { 34 + __be32 version; 35 + __be64 magic; 36 + u8 id; 37 + } __packed; 38 + 39 + #define NET_TEST_PKT_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \ 40 + sizeof(struct netsfhdr)) 41 + #define NET_TEST_PKT_MAGIC 0xdeadcafecafedeadULL 42 + #define NET_LB_TIMEOUT msecs_to_jiffies(200) 6 43 7 44 #if IS_ENABLED(CONFIG_NET_SELFTESTS) 8 45 46 + struct sk_buff *net_test_get_skb(struct net_device *ndev, u8 id, 47 + struct net_packet_attrs *attr); 9 48 void net_selftest(struct net_device *ndev, struct ethtool_test *etest, 10 49 u64 *buf); 11 50 int net_selftest_get_count(void); 12 51 void net_selftest_get_strings(u8 *data); 13 52 14 53 #else 54 + 55 + static inline struct sk_buff *net_test_get_skb(struct net_device *ndev, u8 id, 56 + struct net_packet_attrs *attr) 57 + { 58 + return NULL; 59 + } 15 60 16 61 static inline void net_selftest(struct net_device *ndev, struct ethtool_test *etest, 17 62 u64 *buf)
+7 -41
net/core/selftests.c
··· 14 14 #include <net/tcp.h> 15 15 #include <net/udp.h> 16 16 17 - struct net_packet_attrs { 18 - const unsigned char *src; 19 - const unsigned char *dst; 20 - u32 ip_src; 21 - u32 ip_dst; 22 - bool tcp; 23 - u16 sport; 24 - u16 dport; 25 - int timeout; 26 - int size; 27 - int max_size; 28 - u8 id; 29 - u16 queue_mapping; 30 - bool bad_csum; 31 - }; 32 - 33 - struct net_test_priv { 34 - struct net_packet_attrs *packet; 35 - struct packet_type pt; 36 - struct completion comp; 37 - int double_vlan; 38 - int vlan_id; 39 - int ok; 40 - }; 41 - 42 - struct netsfhdr { 43 - __be32 version; 44 - __be64 magic; 45 - u8 id; 46 - } __packed; 47 - 48 17 static u8 net_test_next_id; 49 18 50 - #define NET_TEST_PKT_SIZE (sizeof(struct ethhdr) + sizeof(struct iphdr) + \ 51 - sizeof(struct netsfhdr)) 52 - #define NET_TEST_PKT_MAGIC 0xdeadcafecafedeadULL 53 - #define NET_LB_TIMEOUT msecs_to_jiffies(200) 54 - 55 - static struct sk_buff *net_test_get_skb(struct net_device *ndev, 56 - struct net_packet_attrs *attr) 19 + struct sk_buff *net_test_get_skb(struct net_device *ndev, u8 id, 20 + struct net_packet_attrs *attr) 57 21 { 58 22 struct sk_buff *skb = NULL; 59 23 struct udphdr *uhdr = NULL; ··· 106 142 shdr = skb_put(skb, sizeof(*shdr)); 107 143 shdr->version = 0; 108 144 shdr->magic = cpu_to_be64(NET_TEST_PKT_MAGIC); 109 - attr->id = net_test_next_id; 110 - shdr->id = net_test_next_id++; 145 + attr->id = id; 146 + shdr->id = id; 111 147 112 148 if (attr->size) { 113 149 void *payload = skb_put(skb, attr->size); ··· 154 190 155 191 return skb; 156 192 } 193 + EXPORT_SYMBOL_GPL(net_test_get_skb); 157 194 158 195 static int net_test_loopback_validate(struct sk_buff *skb, 159 196 struct net_device *ndev, ··· 251 286 tpriv->packet = attr; 252 287 dev_add_pack(&tpriv->pt); 253 288 254 - skb = net_test_get_skb(ndev, attr); 289 + skb = net_test_get_skb(ndev, net_test_next_id, attr); 255 290 if (!skb) { 256 291 ret = -ENOMEM; 257 292 goto cleanup; 258 293 } 259 294 295 + net_test_next_id++; 260 296 ret = dev_direct_xmit(skb, attr->queue_mapping); 261 297 if (ret < 0) { 262 298 goto cleanup;