aboutsummaryrefslogtreecommitdiffstats
path: root/tools/testing/selftests/net/psock_fanout.c
diff options
context:
space:
mode:
authorDaniel Borkmann <dborkman@redhat.com>2013-04-02 13:00:51 +0000
committerDavid S. Miller <davem@davemloft.net>2013-04-07 17:02:24 -0400
commit23a9544206dd91dfe048fcf67abec3f3104c42b9 (patch)
tree144beb514dd0a62c106732a403061f8dc7791508 /tools/testing/selftests/net/psock_fanout.c
parentvxlan: Bypass encapsulation if the destination is local (diff)
downloadlinux-dev-23a9544206dd91dfe048fcf67abec3f3104c42b9.tar.xz
linux-dev-23a9544206dd91dfe048fcf67abec3f3104c42b9.zip
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>
Diffstat (limited to 'tools/testing/selftests/net/psock_fanout.c')
-rw-r--r--tools/testing/selftests/net/psock_fanout.c88
1 files changed, 3 insertions, 85 deletions
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 @@
#include <sys/types.h>
#include <unistd.h>
-#define DATA_LEN 100
-#define DATA_CHAR 'a'
-#define RING_NUM_FRAMES 20
-#define PORT_BASE 8000
-
-static void pair_udp_open(int fds[], uint16_t port)
-{
- struct sockaddr_in saddr, daddr;
-
- fds[0] = socket(PF_INET, SOCK_DGRAM, 0);
- fds[1] = socket(PF_INET, SOCK_DGRAM, 0);
- if (fds[0] == -1 || fds[1] == -1) {
- fprintf(stderr, "ERROR: socket dgram\n");
- exit(1);
- }
-
- memset(&saddr, 0, sizeof(saddr));
- saddr.sin_family = AF_INET;
- saddr.sin_port = htons(port);
- saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
-
- memset(&daddr, 0, sizeof(daddr));
- daddr.sin_family = AF_INET;
- daddr.sin_port = htons(port + 1);
- daddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+#include "psock_lib.h"
- /* must bind both to get consistent hash result */
- if (bind(fds[1], (void *) &daddr, sizeof(daddr))) {
- perror("bind");
- exit(1);
- }
- if (bind(fds[0], (void *) &saddr, sizeof(saddr))) {
- perror("bind");
- exit(1);
- }
- if (connect(fds[0], (void *) &daddr, sizeof(daddr))) {
- perror("connect");
- exit(1);
- }
-}
-
-static void pair_udp_send(int fds[], int num)
-{
- char buf[DATA_LEN], rbuf[DATA_LEN];
-
- memset(buf, DATA_CHAR, sizeof(buf));
- while (num--) {
- /* Should really handle EINTR and EAGAIN */
- if (write(fds[0], buf, sizeof(buf)) != sizeof(buf)) {
- fprintf(stderr, "ERROR: send failed left=%d\n", num);
- exit(1);
- }
- if (read(fds[1], rbuf, sizeof(rbuf)) != sizeof(rbuf)) {
- fprintf(stderr, "ERROR: recv failed left=%d\n", num);
- exit(1);
- }
- if (memcmp(buf, rbuf, sizeof(buf))) {
- fprintf(stderr, "ERROR: data failed left=%d\n", num);
- exit(1);
- }
- }
-}
-
-static void sock_fanout_setfilter(int fd)
-{
- struct sock_filter bpf_filter[] = {
- { 0x80, 0, 0, 0x00000000 }, /* LD pktlen */
- { 0x35, 0, 5, DATA_LEN }, /* JGE DATA_LEN [f goto nomatch]*/
- { 0x30, 0, 0, 0x00000050 }, /* LD ip[80] */
- { 0x15, 0, 3, DATA_CHAR }, /* JEQ DATA_CHAR [f goto nomatch]*/
- { 0x30, 0, 0, 0x00000051 }, /* LD ip[81] */
- { 0x15, 0, 1, DATA_CHAR }, /* JEQ DATA_CHAR [f goto nomatch]*/
- { 0x6, 0, 0, 0x00000060 }, /* RET match */
-/* nomatch */ { 0x6, 0, 0, 0x00000000 }, /* RET no match */
- };
- struct sock_fprog bpf_prog;
-
- bpf_prog.filter = bpf_filter;
- bpf_prog.len = sizeof(bpf_filter) / sizeof(struct sock_filter);
- if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_FILTER, &bpf_prog,
- sizeof(bpf_prog))) {
- perror("setsockopt SO_ATTACH_FILTER");
- exit(1);
- }
-}
+#define RING_NUM_FRAMES 20
/* Open a socket in a given fanout mode.
* @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)
return -1;
}
- sock_fanout_setfilter(fd);
+ pair_udp_setfilter(fd);
return fd;
}