Home | History | Annotate | Line # | Download | only in icmp
t_ping.c revision 1.3
      1  1.3  pooka /*	$NetBSD: t_ping.c,v 1.3 2010/08/18 16:39:22 pooka Exp $	*/
      2  1.1  pooka 
      3  1.1  pooka /*-
      4  1.1  pooka  * Copyright (c) 2010 The NetBSD Foundation, Inc.
      5  1.1  pooka  * All rights reserved.
      6  1.1  pooka  *
      7  1.1  pooka  * Redistribution and use in source and binary forms, with or without
      8  1.1  pooka  * modification, are permitted provided that the following conditions
      9  1.1  pooka  * are met:
     10  1.1  pooka  * 1. Redistributions of source code must retain the above copyright
     11  1.1  pooka  *    notice, this list of conditions and the following disclaimer.
     12  1.1  pooka  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  pooka  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  pooka  *    documentation and/or other materials provided with the distribution.
     15  1.1  pooka  *
     16  1.1  pooka  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     17  1.1  pooka  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     18  1.1  pooka  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19  1.1  pooka  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1  pooka  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
     21  1.1  pooka  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22  1.1  pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     23  1.1  pooka  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1  pooka  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
     25  1.1  pooka  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26  1.1  pooka  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27  1.1  pooka  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1  pooka  */
     29  1.1  pooka 
     30  1.1  pooka #include <sys/cdefs.h>
     31  1.1  pooka #ifndef lint
     32  1.3  pooka __RCSID("$NetBSD: t_ping.c,v 1.3 2010/08/18 16:39:22 pooka Exp $");
     33  1.1  pooka #endif /* not lint */
     34  1.1  pooka 
     35  1.1  pooka #include <sys/types.h>
     36  1.2  pooka #include <sys/resource.h>
     37  1.1  pooka 
     38  1.1  pooka #include <atf-c.h>
     39  1.2  pooka #include <fcntl.h>
     40  1.1  pooka #include <stdio.h>
     41  1.1  pooka #include <stdlib.h>
     42  1.1  pooka #include <string.h>
     43  1.1  pooka #include <unistd.h>
     44  1.1  pooka 
     45  1.1  pooka #include <rump/rump.h>
     46  1.1  pooka 
     47  1.1  pooka #include "../../h_macros.h"
     48  1.1  pooka #include "../config/netconfig.c"
     49  1.1  pooka 
     50  1.1  pooka ATF_TC(simpleping);
     51  1.1  pooka ATF_TC_HEAD(simpleping, tc)
     52  1.1  pooka {
     53  1.1  pooka 
     54  1.1  pooka 	atf_tc_set_md_var(tc, "descr", "check that kernel responds to ping");
     55  1.1  pooka 	atf_tc_set_md_var(tc, "use.fs", "true");
     56  1.1  pooka 	atf_tc_set_md_var(tc, "timeout", "2");
     57  1.1  pooka }
     58  1.1  pooka 
     59  1.1  pooka ATF_TC_BODY(simpleping, tc)
     60  1.1  pooka {
     61  1.1  pooka 	char ifname[IFNAMSIZ];
     62  1.1  pooka 	pid_t cpid;
     63  1.1  pooka 	bool win, win2;
     64  1.1  pooka 
     65  1.1  pooka 	cpid = fork();
     66  1.1  pooka 	rump_init();
     67  1.1  pooka 	netcfg_rump_makeshmif("but-can-i-buy-your-ether-bus", ifname);
     68  1.1  pooka 
     69  1.1  pooka 	switch (cpid) {
     70  1.1  pooka 	case -1:
     71  1.1  pooka 		atf_tc_fail_errno("fork failed");
     72  1.1  pooka 	case 0:
     73  1.1  pooka 		netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
     74  1.1  pooka 		pause();
     75  1.1  pooka 		break;
     76  1.1  pooka 	default:
     77  1.1  pooka 		break;
     78  1.1  pooka 	}
     79  1.1  pooka 
     80  1.1  pooka 	netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
     81  1.1  pooka 
     82  1.1  pooka 	/*
     83  1.1  pooka 	 * The beauty of shmif is that we don't have races here.
     84  1.1  pooka 	 */
     85  1.1  pooka 	win = netcfg_rump_pingtest("1.1.1.10", 500);
     86  1.1  pooka 	win2 = netcfg_rump_pingtest("1.1.1.30", 500);
     87  1.1  pooka 
     88  1.1  pooka 	kill(cpid, SIGKILL);
     89  1.1  pooka 
     90  1.1  pooka 	if (!win)
     91  1.1  pooka 		atf_tc_fail("ping failed");
     92  1.1  pooka 	if (win2)
     93  1.1  pooka 		atf_tc_fail("non-existent host responded");
     94  1.1  pooka }
     95  1.1  pooka 
     96  1.2  pooka ATF_TC(floodping);
     97  1.2  pooka ATF_TC_HEAD(floodping, tc)
     98  1.2  pooka {
     99  1.2  pooka 
    100  1.2  pooka 	atf_tc_set_md_var(tc, "descr", "see how kernel responds to floodping");
    101  1.2  pooka 	atf_tc_set_md_var(tc, "use.fs", "true");
    102  1.2  pooka }
    103  1.2  pooka 
    104  1.3  pooka /* why the hell isn't this available in userspace??? */
    105  1.3  pooka static uint16_t
    106  1.3  pooka in_cksum(void *data, size_t len)
    107  1.3  pooka {
    108  1.3  pooka 	uint16_t *buf = data;
    109  1.3  pooka 	unsigned sum;
    110  1.3  pooka 
    111  1.3  pooka 	for (sum = 0; len > 1; len -= 2)
    112  1.3  pooka 		sum += *buf++;
    113  1.3  pooka 	if (len)
    114  1.3  pooka 		sum += *(uint8_t *)buf;
    115  1.3  pooka 
    116  1.3  pooka 	sum = (sum >> 16) + (sum & 0xffff);
    117  1.3  pooka 	sum += (sum >> 16);
    118  1.3  pooka 
    119  1.3  pooka 	return ~sum;
    120  1.3  pooka }
    121  1.3  pooka 
    122  1.3  pooka static int
    123  1.3  pooka doping(const char *target, int loops, size_t pktsize)
    124  1.2  pooka {
    125  1.3  pooka 	char sndbuf[IP_MAXPACKET - sizeof(struct ip)];
    126  1.3  pooka 	char recvbuf[IP_MAXPACKET];
    127  1.2  pooka 	struct sockaddr_in dst, pingee;
    128  1.3  pooka 	struct icmp *icmp;
    129  1.2  pooka 	socklen_t slen;
    130  1.2  pooka 	ssize_t n;
    131  1.3  pooka 	int loop, i, succ;
    132  1.2  pooka 	int x, xnon, s;
    133  1.2  pooka 
    134  1.3  pooka 	RL(s = rump_sys_socket(PF_INET, SOCK_RAW, IPPROTO_ICMP));
    135  1.3  pooka 	RL(x = rump_sys_fcntl(s, F_GETFL, 0));
    136  1.3  pooka 	xnon = x | O_NONBLOCK;
    137  1.3  pooka 
    138  1.3  pooka 	memset(&dst, 0, sizeof(dst));
    139  1.3  pooka 	dst.sin_len = sizeof(dst);
    140  1.3  pooka 	dst.sin_family = AF_INET;
    141  1.3  pooka 	dst.sin_addr.s_addr = inet_addr(target);
    142  1.3  pooka 
    143  1.3  pooka 	icmp = (struct icmp *)sndbuf;
    144  1.3  pooka 	memset(icmp, 0, sizeof(*icmp));
    145  1.3  pooka 	icmp->icmp_type = ICMP_ECHO;
    146  1.3  pooka 	icmp->icmp_id = htons(37);
    147  1.3  pooka 
    148  1.3  pooka 	if (pktsize < sizeof(*icmp))
    149  1.3  pooka 		pktsize = sizeof(*icmp);
    150  1.3  pooka 	if (pktsize > sizeof(sndbuf))
    151  1.3  pooka 		pktsize = sizeof(sndbuf);
    152  1.3  pooka 
    153  1.3  pooka 	RL(rump_sys_setsockopt(s, SOL_SOCKET, SO_SNDBUF,
    154  1.3  pooka 	    &pktsize, sizeof(pktsize)));
    155  1.3  pooka 	RL(rump_sys_setsockopt(s, SOL_SOCKET, SO_RCVBUF,
    156  1.3  pooka 	    &pktsize, sizeof(pktsize)));
    157  1.3  pooka 
    158  1.3  pooka 	slen = sizeof(pingee);
    159  1.3  pooka 	succ = 0;
    160  1.3  pooka 	for (loop = 0; loop < loops; loop++) {
    161  1.3  pooka 		RL(rump_sys_fcntl(s, F_SETFL, x));
    162  1.3  pooka 		icmp->icmp_seq = htons(loop);
    163  1.3  pooka 		icmp->icmp_cksum = 0;
    164  1.3  pooka 		icmp->icmp_cksum = in_cksum(icmp, pktsize);
    165  1.3  pooka 		RL(rump_sys_sendto(s, icmp, pktsize, 0,
    166  1.3  pooka 		    (struct sockaddr *)&dst, sizeof(dst)));
    167  1.3  pooka 
    168  1.3  pooka 		RL(rump_sys_fcntl(s, F_SETFL, xnon));
    169  1.3  pooka 		while ((n = rump_sys_recvfrom(s, recvbuf, sizeof(recvbuf), 0,
    170  1.3  pooka 		    (struct sockaddr *)&pingee, &slen)) > 0) {
    171  1.3  pooka 			succ++;
    172  1.3  pooka 		}
    173  1.3  pooka 		if (n == -1 && errno == EAGAIN)
    174  1.3  pooka 			continue;
    175  1.3  pooka 		atf_tc_fail_errno("recv failed");
    176  1.3  pooka 	}
    177  1.3  pooka 
    178  1.3  pooka 	rump_sys_close(s);
    179  1.3  pooka 	return succ;
    180  1.3  pooka }
    181  1.3  pooka 
    182  1.3  pooka #define LOOPS 10000
    183  1.3  pooka 
    184  1.3  pooka ATF_TC_BODY(floodping, tc)
    185  1.3  pooka {
    186  1.3  pooka 	char ifname[IFNAMSIZ];
    187  1.3  pooka 	pid_t cpid;
    188  1.3  pooka 	int succ;
    189  1.3  pooka 
    190  1.2  pooka 	cpid = fork();
    191  1.2  pooka 	rump_init();
    192  1.2  pooka 	netcfg_rump_makeshmif("thank-you-driver-for-getting-me-here", ifname);
    193  1.2  pooka 
    194  1.2  pooka 	switch (cpid) {
    195  1.2  pooka 	case -1:
    196  1.2  pooka 		atf_tc_fail_errno("fork failed");
    197  1.2  pooka 	case 0:
    198  1.2  pooka 		netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
    199  1.2  pooka 		pause();
    200  1.2  pooka 		break;
    201  1.2  pooka 	default:
    202  1.2  pooka 		break;
    203  1.2  pooka 	}
    204  1.2  pooka 
    205  1.2  pooka 	netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
    206  1.2  pooka 
    207  1.3  pooka 	succ = doping("1.1.1.10", LOOPS, 56);
    208  1.3  pooka 	printf("got %d/%d\n", succ, LOOPS);
    209  1.3  pooka 
    210  1.3  pooka 	kill(cpid, SIGKILL);
    211  1.3  pooka }
    212  1.3  pooka 
    213  1.3  pooka ATF_TC(floodping2);
    214  1.3  pooka ATF_TC_HEAD(floodping2, tc)
    215  1.3  pooka {
    216  1.3  pooka 
    217  1.3  pooka 	atf_tc_set_md_var(tc, "descr", "two hosts floodpinging each other");
    218  1.3  pooka 	atf_tc_set_md_var(tc, "use.fs", "true");
    219  1.3  pooka }
    220  1.3  pooka 
    221  1.3  pooka ATF_TC_BODY(floodping2, tc)
    222  1.3  pooka {
    223  1.3  pooka 	char ifname[IFNAMSIZ];
    224  1.3  pooka 	pid_t cpid;
    225  1.3  pooka 	int succ;
    226  1.3  pooka 
    227  1.3  pooka 	cpid = fork();
    228  1.3  pooka 	rump_init();
    229  1.3  pooka 	netcfg_rump_makeshmif("floodping2", ifname);
    230  1.3  pooka 
    231  1.3  pooka 	switch (cpid) {
    232  1.3  pooka 	case -1:
    233  1.3  pooka 		atf_tc_fail_errno("fork failed");
    234  1.3  pooka 	case 0:
    235  1.3  pooka 		netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
    236  1.3  pooka 		succ = doping("1.1.1.20", LOOPS, 56);
    237  1.3  pooka 		break;
    238  1.3  pooka 	default:
    239  1.3  pooka 		netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
    240  1.3  pooka 		succ = doping("1.1.1.10", LOOPS, 56);
    241  1.3  pooka 		break;
    242  1.3  pooka 	}
    243  1.3  pooka 
    244  1.3  pooka 	printf("got %d/%d\n", succ, LOOPS);
    245  1.3  pooka }
    246  1.3  pooka 
    247  1.3  pooka ATF_TC(pingsize);
    248  1.3  pooka ATF_TC_HEAD(pingsize, tc)
    249  1.3  pooka {
    250  1.3  pooka 
    251  1.3  pooka 	atf_tc_set_md_var(tc, "descr", "ping with packets min <= size <= max");
    252  1.3  pooka 	atf_tc_set_md_var(tc, "use.fs", "true");
    253  1.3  pooka }
    254  1.3  pooka 
    255  1.3  pooka ATF_TC_BODY(pingsize, tc)
    256  1.3  pooka {
    257  1.3  pooka 	char ifname[IFNAMSIZ];
    258  1.3  pooka 	pid_t cpid;
    259  1.3  pooka 	int succ, i;
    260  1.3  pooka 
    261  1.3  pooka 	cpid = fork();
    262  1.3  pooka 	rump_init();
    263  1.3  pooka 	netcfg_rump_makeshmif("jippikaiee", ifname);
    264  1.2  pooka 
    265  1.3  pooka 	switch (cpid) {
    266  1.3  pooka 	case -1:
    267  1.3  pooka 		atf_tc_fail_errno("fork failed");
    268  1.3  pooka 	case 0:
    269  1.3  pooka 		netcfg_rump_if(ifname, "1.1.1.10", "255.255.255.0");
    270  1.3  pooka 		pause();
    271  1.3  pooka 		break;
    272  1.3  pooka 	default:
    273  1.3  pooka 		break;
    274  1.3  pooka 	}
    275  1.2  pooka 
    276  1.3  pooka 	netcfg_rump_if(ifname, "1.1.1.20", "255.255.255.0");
    277  1.2  pooka 
    278  1.2  pooka 	succ = 0;
    279  1.2  pooka 
    280  1.3  pooka 	/* small sizes */
    281  1.3  pooka 	for (i = IP_MAXPACKET - 60000; i > 0; i--)
    282  1.3  pooka 		succ += doping("1.1.1.10", 1, i);
    283  1.3  pooka 
    284  1.3  pooka 	/* medium sizes */
    285  1.3  pooka 	for (i = IP_MAXPACKET - 100; i > IP_MAXPACKET - 60000; i -= 1000)
    286  1.3  pooka 		succ += doping("1.1.1.10", 1, i);
    287  1.3  pooka 
    288  1.3  pooka 	/* big sizes */
    289  1.3  pooka 	for (i = IP_MAXPACKET; i > IP_MAXPACKET - 100; i -= 10)
    290  1.3  pooka 		succ += doping("1.1.1.10", 1, i);
    291  1.2  pooka 
    292  1.3  pooka 	printf("got %d/%d\n", succ, IP_MAXPACKET);
    293  1.2  pooka 	kill(cpid, SIGKILL);
    294  1.2  pooka }
    295  1.2  pooka 
    296  1.1  pooka ATF_TP_ADD_TCS(tp)
    297  1.1  pooka {
    298  1.1  pooka 
    299  1.1  pooka 	ATF_TP_ADD_TC(tp, simpleping);
    300  1.2  pooka 	ATF_TP_ADD_TC(tp, floodping);
    301  1.3  pooka 	ATF_TP_ADD_TC(tp, floodping2);
    302  1.3  pooka 	ATF_TP_ADD_TC(tp, pingsize);
    303  1.1  pooka 
    304  1.1  pooka 	return atf_no_error();
    305  1.1  pooka }
    306