tools/testing/selftests/net/tap.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/net/tap.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/net/tap.c- Extension
.c- Size
- 9861 bytes
- Lines
- 429
- 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.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
errno.hfcntl.hstdio.hstdlib.hstring.hunistd.hnet/if.hlinux/if_tun.hlinux/netlink.hlinux/rtnetlink.hsys/ioctl.hsys/socket.hlinux/virtio_net.hnetinet/ip.hnetinet/udp.hkselftest_harness.h
Detected Declarations
function rtattr_endfunction dev_createfunction dev_deletefunction macvtap_fill_rtattrfunction opentapfunction build_ethfunction add_csumfunction finish_ip_csumfunction build_ip_csumfunction build_ipv4_headerfunction build_udp_packetfunction build_test_packet_valid_udp_gsofunction build_test_packet_valid_udp_csumfunction build_test_packet_crash_tap_invalid_eth_proto
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <net/if.h>
#include <linux/if_tun.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <linux/virtio_net.h>
#include <netinet/ip.h>
#include <netinet/udp.h>
#include "kselftest_harness.h"
static const char param_dev_tap_name[] = "xmacvtap0";
static const char param_dev_dummy_name[] = "xdummy0";
static unsigned char param_hwaddr_src[] = { 0x00, 0xfe, 0x98, 0x14, 0x22, 0x42 };
static unsigned char param_hwaddr_dest[] = {
0x00, 0xfe, 0x98, 0x94, 0xd2, 0x43
};
#define MAX_RTNL_PAYLOAD (2048)
#define PKT_DATA 0xCB
#define TEST_PACKET_SZ (sizeof(struct virtio_net_hdr) + ETH_HLEN + ETH_MAX_MTU)
static struct rtattr *rtattr_add(struct nlmsghdr *nh, unsigned short type,
unsigned short len)
{
struct rtattr *rta =
(struct rtattr *)((uint8_t *)nh + RTA_ALIGN(nh->nlmsg_len));
rta->rta_type = type;
rta->rta_len = RTA_LENGTH(len);
nh->nlmsg_len = RTA_ALIGN(nh->nlmsg_len) + RTA_ALIGN(rta->rta_len);
return rta;
}
static struct rtattr *rtattr_begin(struct nlmsghdr *nh, unsigned short type)
{
return rtattr_add(nh, type, 0);
}
static void rtattr_end(struct nlmsghdr *nh, struct rtattr *attr)
{
uint8_t *end = (uint8_t *)nh + nh->nlmsg_len;
attr->rta_len = end - (uint8_t *)attr;
}
static struct rtattr *rtattr_add_str(struct nlmsghdr *nh, unsigned short type,
const char *s)
{
unsigned int strsz = strlen(s) + 1;
struct rtattr *rta;
rta = rtattr_add(nh, type, strsz);
memcpy(RTA_DATA(rta), s, strsz);
return rta;
}
static struct rtattr *rtattr_add_any(struct nlmsghdr *nh, unsigned short type,
const void *arr, size_t len)
{
struct rtattr *rta = rtattr_add(nh, type, len);
memcpy(RTA_DATA(rta), arr, len);
return rta;
}
static int dev_create(const char *dev, const char *link_type,
int (*fill_rtattr)(struct nlmsghdr *nh),
int (*fill_info_data)(struct nlmsghdr *nh))
{
struct {
struct nlmsghdr nh;
struct ifinfomsg info;
unsigned char data[MAX_RTNL_PAYLOAD];
} req;
struct rtattr *link_info, *info_data;
int ret, rtnl;
rtnl = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE);
if (rtnl < 0) {
Annotation
- Immediate include surface: `errno.h`, `fcntl.h`, `stdio.h`, `stdlib.h`, `string.h`, `unistd.h`, `net/if.h`, `linux/if_tun.h`.
- Detected declarations: `function rtattr_end`, `function dev_create`, `function dev_delete`, `function macvtap_fill_rtattr`, `function opentap`, `function build_eth`, `function add_csum`, `function finish_ip_csum`, `function build_ip_csum`, `function build_ipv4_header`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.