diff options
| author | Daniel Borkmann <dborkman@redhat.com> | 2013-04-02 09:00:51 -0400 |
|---|---|---|
| committer | David S. Miller <davem@davemloft.net> | 2013-04-07 17:02:24 -0400 |
| commit | 23a9544206dd91dfe048fcf67abec3f3104c42b9 (patch) | |
| tree | 144beb514dd0a62c106732a403061f8dc7791508 | |
| parent | 9dcc71e1fdbb7aa10d92a3d35e8a201adc84abd0 (diff) | |
selftests: net: add PF_PACKET TPACKET v1/v2/v3 selftests
This patch adds a simple test case that probes the packet socket's
TPACKET_V1, TPACKET_V2 and TPACKET_V3 behavior regarding mmap(2)'ed
I/O for a small burst of 100 packets. The test currently runs for ...
TPACKET_V1: RX_RING, TX_RING
TPACKET_V2: RX_RING, TX_RING
TPACKET_V3: RX_RING
... and will output on success:
test: TPACKET_V1 with PACKET_RX_RING .................... 100 pkts (9600 bytes)
test: TPACKET_V1 with PACKET_TX_RING .................... 100 pkts (9600 bytes)
test: TPACKET_V2 with PACKET_RX_RING .................... 100 pkts (9600 bytes)
test: TPACKET_V2 with PACKET_TX_RING .................... 100 pkts (9600 bytes)
test: TPACKET_V3 with PACKET_RX_RING .................... 100 pkts (9600 bytes)
OK. All tests passed
Reusable parts of psock_fanout.c have been put into a psock_lib.h
file for common usage. Test case successfully tested on x86_64.
Signed-off-by: Daniel Borkmann <dborkman@redhat.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
| -rw-r--r-- | tools/testing/selftests/net/Makefile | 4 | ||||
| -rw-r--r-- | tools/testing/selftests/net/psock_fanout.c | 88 | ||||
| -rw-r--r-- | tools/testing/selftests/net/psock_lib.h | 127 | ||||
| -rw-r--r-- | tools/testing/selftests/net/psock_tpacket.c | 824 | ||||
| -rw-r--r-- | tools/testing/selftests/net/run_afpackettests | 10 |
5 files changed, 966 insertions, 87 deletions
diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile index bd6e272bab87..750512ba2c88 100644 --- a/tools/testing/selftests/net/Makefile +++ b/tools/testing/selftests/net/Makefile | |||
| @@ -1,11 +1,11 @@ | |||
| 1 | # Makefile for net selftests | 1 | # Makefile for net selftests |
| 2 | 2 | ||
| 3 | CC = $(CROSS_COMPILE)gcc | 3 | CC = $(CROSS_COMPILE)gcc |
| 4 | CFLAGS = -Wall | 4 | CFLAGS = -Wall -O2 -g |
| 5 | 5 | ||
| 6 | CFLAGS += -I../../../../usr/include/ | 6 | CFLAGS += -I../../../../usr/include/ |
| 7 | 7 | ||
| 8 | NET_PROGS = socket psock_fanout | 8 | NET_PROGS = socket psock_fanout psock_tpacket |
| 9 | 9 | ||
| 10 | all: $(NET_PROGS) | 10 | all: $(NET_PROGS) |
| 11 | %: %.c | 11 | %: %.c |
diff --git a/tools/testing/selftests/net/psock_fanout.c b/tools/testing/selftests/net/psock_fanout.c index 59bd6367af71..57b9c2b7c4ff 100644 --- a/tools/testing/selftests/net/psock_fanout.c +++ b/tools/testing/selftests/net/psock_fanout.c | |||
| @@ -61,91 +61,9 @@ | |||
| 61 | #include <sys/types.h> | 61 | #include <sys/types.h> |
| 62 | #include <unistd.h> | 62 | #include <unistd.h> |
| 63 | 63 | ||
| 64 | #define DATA_LEN 100 | 64 | #include "psock_lib.h" |
| 65 | #define DATA_CHAR 'a' | ||
| 66 | #define RING_NUM_FRAMES 20 | ||
| 67 | #define PORT_BASE 8000 | ||
| 68 | |||
| 69 | static void pair_udp_open(int fds[], uint16_t port) | ||
| 70 | { | ||
| 71 | struct sockaddr_in saddr, daddr; | ||
| 72 | |||
| 73 | fds[0] = socket(PF_INET, SOCK_DGRAM, 0); | ||
| 74 | fds[1] = socket(PF_INET, SOCK_DGRAM, 0); | ||
| 75 | if (fds[0] == -1 || fds[1] == -1) { | ||
| 76 | fprintf(stderr, "ERROR: socket dgram\n"); | ||
| 77 | exit(1); | ||
| 78 | } | ||
| 79 | |||
| 80 | memset(&saddr, 0, sizeof(saddr)); | ||
| 81 | saddr.sin_family = AF_INET; | ||
| 82 | saddr.sin_port = htons(port); | ||
| 83 | saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | ||
| 84 | |||
| 85 | memset(&daddr, 0, sizeof(daddr)); | ||
| 86 | daddr.sin_family = AF_INET; | ||
| 87 | daddr.sin_port = htons(port + 1); | ||
| 88 | daddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK); | ||
| 89 | 65 | ||
| 90 | /* must bind both to get consistent hash result */ | 66 | #define RING_NUM_FRAMES 20 |
| 91 | if (bind(fds[1], (void *) &daddr, sizeof(daddr))) { | ||
| 92 | perror("bind"); | ||
| 93 | exit(1); | ||
| 94 | } | ||
| 95 | if (bind(fds[0], (void *) &saddr, sizeof(saddr))) { | ||
| 96 | perror("bind"); | ||
| 97 | exit(1); | ||
| 98 | } | ||
| 99 | if (connect(fds[0], (void *) &daddr, sizeof(daddr))) { | ||
| 100 | perror("connect"); | ||
| 101 | exit(1); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | static void pair_udp_send(int fds[], int num) | ||
| 106 | { | ||
| 107 | char buf[DATA_LEN], rbuf[DATA_LEN]; | ||
| 108 | |||
| 109 | memset(buf, DATA_CHAR, sizeof(buf)); | ||
| 110 | while (num--) { | ||
| 111 | /* Should really handle EINTR and EAGAIN */ | ||
| 112 | if (write(fds[0], buf, sizeof(buf)) != sizeof(buf)) { | ||
| 113 | fprintf(stderr, "ERROR: send failed left=%d\n", num); | ||
| 114 | exit(1); | ||
| 115 | } | ||
| 116 | if (read(fds[1], rbuf, sizeof(rbuf)) != sizeof(rbuf)) { | ||
| 117 | fprintf(stderr, "ERROR: recv failed left=%d\n", num); | ||
| 118 | exit(1); | ||
| 119 | } | ||
| 120 | if (memcmp(buf, rbuf, sizeof(buf))) { | ||
| 121 | fprintf(stderr, "ERROR: data failed left=%d\n", num); | ||
| 122 | exit(1); | ||
| 123 | } | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | static void sock_fanout_setfilter(int fd) | ||
| 128 | { | ||
| 129 | struct sock_filter bpf_filter[] = { | ||
| 130 | { 0x80, 0, 0, 0x00000000 }, /* LD pktlen */ | ||
| 131 | { 0x35, 0, 5, DATA_LEN }, /* JGE DATA_LEN [f goto nomatch]*/ | ||
| 132 | { 0x30, 0, 0, 0x00000050 }, /* LD ip[80] */ | ||
| 133 | { 0x15, 0, 3, DATA_CHAR }, /* JEQ DATA_CHAR [f goto nomatch]*/ | ||
| 134 | { 0x30, 0, 0, 0x00000051 }, /* LD ip[81] */ | ||
| 135 | { 0x15, 0, 1, DATA_CHAR }, /* JEQ DATA_CHAR [f goto nomatch]*/ | ||
| 136 | { 0x6, 0, 0, 0x00000060 }, /* RET match */ | ||
| 137 | /* nomatch */ { 0x6, 0, 0, 0x00000000 }, /* RET no match */ | ||
| 138 | }; | ||
| 139 | struct sock_fprog bpf_prog; | ||
| 140 | |||
| 141 | bpf_prog.filter = bpf_filter; | ||
| 142 | bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter); | ||
| 143 | if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf_prog, | ||
| 144 | sizeof(bpf_prog))) { | ||
| 145 | perror("setsockopt SO_ATTACH_FILTER"); | ||
| 146 | exit(1); | ||
| 147 | } | ||
| 148 | } | ||
| 149 | 67 | ||
| 150 | /* Open a socket in a given fanout mode. | 68 | /* Open a socket in a given fanout mode. |
| 151 | * @return -1 if mode is bad, a valid socket otherwise */ | 69 | * @return -1 if mode is bad, a valid socket otherwise */ |
| @@ -169,7 +87,7 @@ static int sock_fanout_open(uint16_t typeflags, int num_packets) | |||
| 169 | return -1; | 87 | return -1; |
| 170 | } | 88 | } |
| 171 | 89 | ||
| 172 | sock_fanout_setfilter(fd); | 90 | pair_udp_setfilter(fd); |
| 173 | return fd; | 91 | return fd; |
| 174 | } | 92 | } |
| 175 | 93 | ||
diff --git a/tools/testing/selftests/net/psock_lib.h b/tools/testing/selftests/net/psock_lib.h new file mode 100644 index 000000000000..37da54ac85a9 --- /dev/null +++ b/tools/testing/selftests/net/psock_lib.h | |||
| @@ -0,0 +1,127 @@ | |||
| 1 | /* | ||
| 2 | * Copyright 2013 Google Inc. | ||
| 3 | * Author: Willem de Bruijn <willemb@google.com> | ||
| 4 | * Daniel Borkmann <dborkman@redhat.com> | ||
| 5 | * | ||
| 6 | * License (GPLv2): | ||
| 7 | * | ||
| 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. | ||
| 11 | * | ||
| 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 | ||
| 15 | * more details. | ||
| 16 | * | ||
| 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. | ||
| 20 | */ | ||
| 21 | |||
| 22 | #ifndef PSOCK_LIB_H | ||
| 23 | #define PSOCK_LIB_H | ||
| 24 | |||
| 25 | #include <sys/types.h> | ||
| 26 | #include <sys/socket.h> | ||
| 27 | #include <string.h> | ||
| 28 | #include <arpa/inet.h> | ||
| 29 | #include <unistd.h> | ||
| 30 | |||
| 31 | #define DATA_LEN 100 | ||
| 32 | #define DATA_CHAR 'a' | ||
| 33 | |||
| 34 | #define PORT_BASE 8000 | ||
| 35 | |||
| 36 | #ifndef __maybe_unused | ||
| 37 | # define __maybe_unused __attribute__ ((__unused__)) | ||
| 38 | #endif | ||
| 39 | |||
| 40 | static __maybe_unused void pair_udp_setfilter(int fd) | ||
| 41 | { | ||
| 42 | struct sock_filter bpf_filter[] = { | ||
| 43 | { 0x80, 0, 0, 0x00000000 }, /* LD pktlen */ | ||
| 44 | { 0x35, 0, 5, DATA_LEN }, /* JGE DATA_LEN [f goto nomatch]*/ | ||
| 45 | { 0x30, 0, 0, 0x00000050 }, /* LD ip[80] */ | ||
| 46 | { 0x15, 0, 3, DATA_CHAR }, /* JEQ DATA_CHAR [f goto nomatch]*/ | ||
