Path: blob/main/tools/regression/netinet/arphold/arphold.c
39491 views
/*1* Copyright (c) 2010 Hudson River Trading LLC2* Written by George Neville-Neil [email protected]3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8* 1. Redistributions of source code must retain the above copyright9* notice, this list of conditions and the following disclaimer.10* 2. Redistributions in binary form must reproduce the above copyright11* notice, this list of conditions and the following disclaimer in the12* documentation and/or other materials provided with the distribution.13*14* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND15* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE16* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE17* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE18* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL19* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS20* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)21* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT22* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY23* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF24* SUCH DAMAGE.25*26* Description: The following is a test of the arp entry packet queues27* which replaced the single packet hold entry that existed in the BSDs28* since time immemorial. The test process is:29*30* 1) Find out the current system limit (maxhold)31* 2) Using an IP address for which we do not yet have an entry32* load up an ARP entry packet queue with exactly that many packets.33* 3) Check the arp dropped stat to make sure that we have not dropped34* any packets as yet.35* 4) Add one more packet to the queue.36* 5) Make sure that only one packet was dropped.37*38* CAVEAT: The ARP timer will flush the queue after 1 second so it is39* important not to run this code in a fast loop or the test will40* fail.41*/4243#include <unistd.h>44#include <stdio.h>45#include <stdlib.h>46#include <string.h>47#include <sys/types.h>48#include <sys/sysctl.h>49#include <sys/socket.h>50#include <netinet/in.h>51#include <arpa/inet.h>52#include <net/if_arp.h>5354#define MSG_SIZE 102455#define PORT 69695657int58main(int argc, char **argv)59{6061int sock;62int maxhold;63size_t size = sizeof(maxhold);64struct sockaddr_in dest;65char message[MSG_SIZE];66struct arpstat arpstat;67size_t len = sizeof(arpstat);68unsigned long dropped = 0;6970memset(&message, 1, sizeof(message));7172if (sysctlbyname("net.link.ether.inet.maxhold", &maxhold, &size,73NULL, 0) < 0) {74perror("not ok 1 - sysctlbyname failed");75exit(1);76}7778#ifdef DEBUG79printf("maxhold is %d\n", maxhold);80#endif /* DEBUG */8182if ((sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {83perror("not ok 1 - could not open socket");84exit(1);85}8687bzero(&dest, sizeof(dest));88if (inet_pton(AF_INET, argv[1], &dest.sin_addr.s_addr) != 1) {89perror("not ok 1 - could not parse address");90exit(1);91}92dest.sin_len = sizeof(dest);93dest.sin_family = AF_INET;94dest.sin_port = htons(PORT);9596if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len,97NULL, 0) < 0) {98perror("not ok 1 - could not get initial arp stats");99exit(1);100}101102dropped = arpstat.dropped;103#ifdef DEBUG104printf("dropped before %ld\n", dropped);105#endif /* DEBUG */106107/*108* Load up the queue in the ARP entry to the maximum.109* We should not drop any packets at this point.110*/111112while (maxhold > 0) {113if (sendto(sock, message, sizeof(message), 0,114(struct sockaddr *)&dest, sizeof(dest)) < 0) {115perror("not ok 1 - could not send packet");116exit(1);117}118maxhold--;119}120121if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len,122NULL, 0) < 0) {123perror("not ok 1 - could not get new arp stats");124exit(1);125}126127#ifdef DEBUG128printf("dropped after %ld\n", arpstat.dropped);129#endif /* DEBUG */130131if (arpstat.dropped != dropped) {132printf("not ok 1 - Failed, drops changed:"133"before %ld after %ld\n", dropped, arpstat.dropped);134exit(1);135}136137dropped = arpstat.dropped;138139/* Now add one extra and make sure it is dropped. */140if (sendto(sock, message, sizeof(message), 0,141(struct sockaddr *)&dest, sizeof(dest)) < 0) {142perror("not ok 1 - could not send packet");143exit(1);144}145146if (sysctlbyname("net.link.ether.arp.stats", &arpstat, &len,147NULL, 0) < 0) {148perror("not ok 1 - could not get new arp stats");149exit(1);150}151152if (arpstat.dropped != (dropped + 1)) {153printf("not ok 1 - Failed to drop one packet: before"154" %ld after %ld\n", dropped, arpstat.dropped);155exit(1);156}157158printf("ok\n");159return (0);160}161162163