]>
git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - tools/testing/selftests/net/psock_lib.h
2 * Copyright 2013 Google Inc.
3 * Author: Willem de Bruijn <willemb@google.com>
4 * Daniel Borkmann <dborkman@redhat.com>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms and conditions of the GNU General Public License,
10 * version 2, as published by the Free Software Foundation.
12 * This program is distributed in the hope it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. * See the GNU General Public License for
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
25 #include <sys/types.h>
26 #include <sys/socket.h>
28 #include <arpa/inet.h>
33 #define DATA_CHAR_1 'b'
35 #define PORT_BASE 8000
37 #ifndef __maybe_unused
38 # define __maybe_unused __attribute__ ((__unused__))
41 static __maybe_unused
void sock_setfilter(int fd
, int lvl
, int optnum
)
43 /* the filter below checks for all of the following conditions that
44 * are based on the contents of create_payload()
45 * ether type 0x800 and
47 * skb->len == DATA_LEN and
48 * udp[38] == 'a' or udp[38] == 'b'
49 * It can be generated from the following bpf_asm input:
51 * jne #0x800, drop ; ETH_P_IP
53 * jneq #17, drop ; IPPROTO_UDP
54 * ld len ; ld skb->len
55 * jlt #100, drop ; DATA_LEN
57 * jeq #97, pass ; DATA_CHAR
58 * jne #98, drop ; DATA_CHAR_1
64 struct sock_filter bpf_filter
[] = {
65 { 0x28, 0, 0, 0x0000000c },
66 { 0x15, 0, 8, 0x00000800 },
67 { 0x30, 0, 0, 0x00000017 },
68 { 0x15, 0, 6, 0x00000011 },
69 { 0x80, 0, 0, 0000000000 },
70 { 0x35, 0, 4, 0x00000064 },
71 { 0x30, 0, 0, 0x00000050 },
72 { 0x15, 1, 0, 0x00000061 },
73 { 0x15, 0, 1, 0x00000062 },
74 { 0x06, 0, 0, 0xffffffff },
75 { 0x06, 0, 0, 0000000000 },
77 struct sock_fprog bpf_prog
;
79 if (lvl
== SOL_PACKET
&& optnum
== PACKET_FANOUT_DATA
)
80 bpf_filter
[5].code
= 0x16; /* RET A */
82 bpf_prog
.filter
= bpf_filter
;
83 bpf_prog
.len
= sizeof(bpf_filter
) / sizeof(struct sock_filter
);
84 if (setsockopt(fd
, lvl
, optnum
, &bpf_prog
,
86 perror("setsockopt SO_ATTACH_FILTER");
91 static __maybe_unused
void pair_udp_setfilter(int fd
)
93 sock_setfilter(fd
, SOL_SOCKET
, SO_ATTACH_FILTER
);
96 static __maybe_unused
void pair_udp_open(int fds
[], uint16_t port
)
98 struct sockaddr_in saddr
, daddr
;
100 fds
[0] = socket(PF_INET
, SOCK_DGRAM
, 0);
101 fds
[1] = socket(PF_INET
, SOCK_DGRAM
, 0);
102 if (fds
[0] == -1 || fds
[1] == -1) {
103 fprintf(stderr
, "ERROR: socket dgram\n");
107 memset(&saddr
, 0, sizeof(saddr
));
108 saddr
.sin_family
= AF_INET
;
109 saddr
.sin_port
= htons(port
);
110 saddr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
112 memset(&daddr
, 0, sizeof(daddr
));
113 daddr
.sin_family
= AF_INET
;
114 daddr
.sin_port
= htons(port
+ 1);
115 daddr
.sin_addr
.s_addr
= htonl(INADDR_LOOPBACK
);
117 /* must bind both to get consistent hash result */
118 if (bind(fds
[1], (void *) &daddr
, sizeof(daddr
))) {
122 if (bind(fds
[0], (void *) &saddr
, sizeof(saddr
))) {
126 if (connect(fds
[0], (void *) &daddr
, sizeof(daddr
))) {
132 static __maybe_unused
void pair_udp_send_char(int fds
[], int num
, char payload
)
134 char buf
[DATA_LEN
], rbuf
[DATA_LEN
];
136 memset(buf
, payload
, sizeof(buf
));
138 /* Should really handle EINTR and EAGAIN */
139 if (write(fds
[0], buf
, sizeof(buf
)) != sizeof(buf
)) {
140 fprintf(stderr
, "ERROR: send failed left=%d\n", num
);
143 if (read(fds
[1], rbuf
, sizeof(rbuf
)) != sizeof(rbuf
)) {
144 fprintf(stderr
, "ERROR: recv failed left=%d\n", num
);
147 if (memcmp(buf
, rbuf
, sizeof(buf
))) {
148 fprintf(stderr
, "ERROR: data failed left=%d\n", num
);
154 static __maybe_unused
void pair_udp_send(int fds
[], int num
)
156 return pair_udp_send_char(fds
, num
, DATA_CHAR
);
159 static __maybe_unused
void pair_udp_close(int fds
[])
165 #endif /* PSOCK_LIB_H */