aboutsummaryrefslogtreecommitdiff
path: root/tools/regression/netinet/arphold/arphold.c
blob: 417a2d8c15d5e5469400b29d7fe4c796c49e6a30 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*
 * Copyright (c) 2010 Hudson River Trading LLC
 * Written by George Neville-Neil gnn@freebsd.org
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Description: The following is a test of the arp entry packet queues
 * which replaced the single packet hold entry that existed in the BSDs
 * since time immemorial.  The test process is:
 *
 * 1) Find out the current system limit (maxhold)
 * 2) Using an IP address for which we do not yet have an entry
 *    load up an ARP entry packet queue with exactly that many packets.
 * 3) Check the arp dropped stat to make sure that we have not dropped
 *    any packets as yet.
 * 4) Add one more packet to the queue.
 * 5) Make sure that only one packet was dropped.
 *
 * CAVEAT: The ARP timer will flush the queue after 1 second so it is
 * important not to run this code in a fast loop or the test will
 * fail.
 *
 * $FreeBSD$
 */

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <net/if_arp.h>

#define MSG_SIZE 1024
#define PORT 6969

int
main(int argc, char **argv)
{

	int sock;
	int maxhold;
	size_t size = sizeof(maxhold);
	struct sockaddr_in dest;
	char message[MSG_SIZE];
	struct arpstat arpstat;
	size_t len = sizeof(arpstat);
	unsigned long dropped = 0;

	memset(&message, 1, sizeof(message));

	if (sysctlbyname("net.link.ether.inet.maxhold", &maxhold, &size,
			 NULL, 0) < 0) {
		perror("not ok 1 - sysctlbyname failed");
		exit(1);
	}
	    
#ifdef DEBUG
	printf("maxhold is %d\n", maxhold);
#endif /* DEBUG */

	if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
		perror("not ok 1 - could not open socket");
		exit(1);
	}

	bzero(&dest, sizeof(dest));
	if (inet_pton(AF_INET, argv[1], &dest.sin_addr.s_addr) != 1) {
		perror("not ok 1 - could not parse address");
		exit(1);
	}
	dest.sin_len = sizeof(dest);
	dest.sin_family = AF_INET;
	dest.sin_port = htons(PORT);

	if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len,
			 NULL, 0) < 0) {
		perror("not ok 1 - could not get initial arp stats");
		exit(1);
	}

	dropped = arpstat.dropped;
#ifdef DEBUG
	printf("dropped before %ld\n", dropped);
#endif /* DEBUG */

	/* 
	 * Load up the queue in the ARP entry to the maximum.
	 * We should not drop any packets at this point. 
	 */

	while (maxhold > 0) {
		if (sendto(sock, message, sizeof(message), 0,
			   (struct sockaddr *)&dest, sizeof(dest)) < 0) {
			perror("not ok 1 - could not send packet");
			exit(1);
		}
		maxhold--;
	}

	if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len,
			 NULL, 0) < 0) {
		perror("not ok 1 - could not get new arp stats");
		exit(1);
	}

#ifdef DEBUG
	printf("dropped after %ld\n", arpstat.dropped);
#endif /* DEBUG */

	if (arpstat.dropped != dropped) {
		printf("not ok 1 - Failed, drops changed:"
		       "before %ld after %ld\n", dropped, arpstat.dropped);
		exit(1);
	}
	
	dropped = arpstat.dropped;

	/* Now add one extra and make sure it is dropped. */
	if (sendto(sock, message, sizeof(message), 0,
		   (struct sockaddr *)&dest, sizeof(dest)) < 0) {
		perror("not ok 1 - could not send packet");
		exit(1);
	}

	if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len,
			 NULL, 0) < 0) {
		perror("not ok 1 - could not get new arp stats");
		exit(1);
	}

	if (arpstat.dropped != (dropped + 1)) {
		printf("not ok 1 - Failed to drop one packet: before"
		       " %ld after %ld\n", dropped, arpstat.dropped);
		exit(1);
	}

	printf("ok\n");
	return (0);
}