2 * Copyright (c) 2010 Advanced Computing Technologies LLC
3 * Written by George Neville-Neil gnn@freebsd.org
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * Description: The following is a test of the arp entry packet queues
28 * which replaced the single packet hold entry that existed in the BSDs
29 * since time immemorial. The test process is:
31 * 1) Find out the current system limit (maxhold)
32 * 2) Using an IP address for which we do not yet have an entry
33 * load up an ARP entry packet queue with exactly that many packets.
34 * 3) Check the arp dropped stat to make sure that we have not dropped
36 * 4) Add one more packet to the queue.
37 * 5) Make sure that only one packet was dropped.
39 * CAVEAT: The ARP timer will flush the queue after 1 second so it is
40 * important not to run this code in a fast loop or the test will
50 #include <sys/types.h>
51 #include <sys/sysctl.h>
52 #include <sys/socket.h>
53 #include <netinet/in.h>
54 #include <arpa/inet.h>
55 #include <net/if_arp.h>
61 main(int argc, char **argv)
66 size_t size = sizeof(maxhold);
67 struct sockaddr_in dest;
68 char message[MSG_SIZE];
69 struct arpstat arpstat;
70 size_t len = sizeof(arpstat);
71 unsigned long dropped = 0;
73 memset(&message, 1, sizeof(message));
75 if (sysctlbyname("net.link.ether.inet.maxhold", &maxhold, &size,
77 perror("not ok 1 - sysctlbyname failed");
82 printf("maxhold is %d\n", maxhold);
85 if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
86 perror("not ok 1 - could not open socket");
90 bzero(&dest, sizeof(dest));
91 if (inet_pton(AF_INET, argv[1], &dest.sin_addr.s_addr) != 1) {
92 perror("not ok 1 - could not parse address");
95 dest.sin_len = sizeof(dest);
96 dest.sin_family = AF_INET;
97 dest.sin_port = htons(PORT);
99 if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len,
101 perror("not ok 1 - could not get initial arp stats");
105 dropped = arpstat.dropped;
107 printf("dropped before %ld\n", dropped);
111 * Load up the queue in the ARP entry to the maximum.
112 * We should not drop any packets at this point.
115 while (maxhold > 0) {
116 if (sendto(sock, message, sizeof(message), 0,
117 (struct sockaddr *)&dest, sizeof(dest)) < 0) {
118 perror("not ok 1 - could not send packet");
124 if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len,
126 perror("not ok 1 - could not get new arp stats");
131 printf("dropped after %ld\n", arpstat.dropped);
134 if (arpstat.dropped != dropped) {
135 printf("not ok 1 - Failed, drops changed:"
136 "before %ld after %ld\n", dropped, arpstat.dropped);
140 dropped = arpstat.dropped;
142 /* Now add one extra and make sure it is dropped. */
143 if (sendto(sock, message, sizeof(message), 0,
144 (struct sockaddr *)&dest, sizeof(dest)) < 0) {
145 perror("not ok 1 - could not send packet");
149 if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len,
151 perror("not ok 1 - could not get new arp stats");
155 if (arpstat.dropped != (dropped + 1)) {
156 printf("not ok 1 - Failed to drop one packet: before"
157 " %ld after %ld\n", dropped, arpstat.dropped);