xref: /linux/tools/testing/selftests/net/nsh_gso.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Send one TCPv4 GSO packet encapsulated in a stack of NSH headers,
3  * through a packet socket with PACKET_VNET_HDR.
4  *
5  * Usage: nsh_gso <ifname> <number of NSH headers>
6  *
7  * The packet is made of 4 segments. The send() result is not checked:
8  * the caller looks at the device counters.
9  */
10 
11 #include <arpa/inet.h>
12 #include <errno.h>
13 #include <error.h>
14 #include <linux/if_ether.h>
15 #include <linux/if_packet.h>
16 #include <linux/ip.h>
17 #include <linux/tcp.h>
18 #include <linux/virtio_net.h>
19 #include <net/if.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <sys/socket.h>
24 #include <unistd.h>
25 
26 #ifndef ETH_P_NSH
27 #define ETH_P_NSH	0x894F
28 #endif
29 
30 #define NSH_HLEN	8	/* base header + service path header */
31 #define NSH_MD_TYPE2	2	/* no mandatory context header */
32 #define NSH_NP_IPV4	1
33 #define NSH_NP_NSH	4
34 
35 #define GSO_SIZE	500
36 #define NR_SEGS		4
37 
38 struct nsh_base {
39 	__be16	ver_flags_ttl_len;
40 	__u8	mdtype;
41 	__u8	np;
42 	__be32	path_hdr;
43 };
44 
45 int main(int argc, char **argv)
46 {
47 	unsigned int depth, hlen, len, i;
48 	struct sockaddr_ll addr = {};
49 	struct virtio_net_hdr *vnet;
50 	struct nsh_base *nsh;
51 	struct ethhdr *eth;
52 	struct tcphdr *tcp;
53 	int fd, one = 1;
54 	struct iphdr *ip;
55 	char *buf;
56 
57 	if (argc != 3)
58 		error(1, 0, "usage: %s <ifname> <depth>", argv[0]);
59 
60 	depth = strtoul(argv[2], NULL, 0);
61 	hlen = sizeof(*eth) + depth * NSH_HLEN + sizeof(*ip) + sizeof(*tcp);
62 	if (!depth || hlen > UINT16_MAX)
63 		error(1, 0, "invalid depth %u", depth);
64 	len = sizeof(*vnet) + hlen + NR_SEGS * GSO_SIZE;
65 
66 	buf = calloc(1, len);
67 	if (!buf)
68 		error(1, errno, "calloc");
69 
70 	vnet = (void *)buf;
71 	vnet->flags = VIRTIO_NET_HDR_F_NEEDS_CSUM;
72 	vnet->gso_type = VIRTIO_NET_HDR_GSO_TCPV4;
73 	vnet->hdr_len = hlen;
74 	vnet->gso_size = GSO_SIZE;
75 	vnet->csum_start = hlen - sizeof(*tcp);
76 	vnet->csum_offset = __builtin_offsetof(struct tcphdr, check);
77 
78 	eth = (void *)(vnet + 1);
79 	memset(eth->h_dest, 0xff, ETH_ALEN);
80 	eth->h_source[0] = 0x02;
81 	eth->h_proto = htons(ETH_P_NSH);
82 
83 	nsh = (void *)(eth + 1);
84 	for (i = 0; i < depth; i++, nsh++) {
85 		/* version 0, ttl 63, length in 4 byte words */
86 		nsh->ver_flags_ttl_len = htons(63 << 6 | NSH_HLEN / 4);
87 		nsh->mdtype = NSH_MD_TYPE2;
88 		nsh->np = i == depth - 1 ? NSH_NP_IPV4 : NSH_NP_NSH;
89 		nsh->path_hdr = htonl(1 << 8 | 255);
90 	}
91 
92 	ip = (void *)nsh;
93 	ip->version = 4;
94 	ip->ihl = 5;
95 	ip->ttl = 64;
96 	ip->protocol = IPPROTO_TCP;
97 	ip->saddr = htonl(0xc0000201);	/* 192.0.2.1 */
98 	ip->daddr = htonl(0xc0000202);	/* 192.0.2.2 */
99 
100 	tcp = (void *)(ip + 1);
101 	tcp->source = htons(1234);
102 	tcp->dest = htons(5678);
103 	tcp->doff = 5;
104 	tcp->ack = 1;
105 
106 	fd = socket(AF_PACKET, SOCK_RAW, 0);
107 	if (fd < 0)
108 		error(1, errno, "socket");
109 	if (setsockopt(fd, SOL_PACKET, PACKET_VNET_HDR, &one, sizeof(one)))
110 		error(1, errno, "setsockopt PACKET_VNET_HDR");
111 
112 	addr.sll_family = AF_PACKET;
113 	addr.sll_protocol = htons(ETH_P_NSH);
114 	addr.sll_ifindex = if_nametoindex(argv[1]);
115 	if (!addr.sll_ifindex)
116 		error(1, errno, "if_nametoindex %s", argv[1]);
117 
118 	/* A packet dropped at segmentation time is reported as an error,
119 	 * this is expected for deep stacks.
120 	 */
121 	sendto(fd, buf, len, 0, (void *)&addr, sizeof(addr));
122 
123 	close(fd);
124 	free(buf);
125 	return 0;
126 }
127