1150970Srwatson/*- 2150970Srwatson * Copyright (c) 2005 Robert N. M. Watson 3150970Srwatson * All rights reserved. 4150970Srwatson * 5150970Srwatson * Redistribution and use in source and binary forms, with or without 6150970Srwatson * modification, are permitted provided that the following conditions 7150970Srwatson * are met: 8150970Srwatson * 1. Redistributions of source code must retain the above copyright 9150970Srwatson * notice, this list of conditions and the following disclaimer. 10150970Srwatson * 2. Redistributions in binary form must reproduce the above copyright 11150970Srwatson * notice, this list of conditions and the following disclaimer in the 12150970Srwatson * documentation and/or other materials provided with the distribution. 13150970Srwatson * 14150970Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15150970Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16150970Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17150970Srwatson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18150970Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19150970Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20150970Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21150970Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22150970Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23150970Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24150970Srwatson * SUCH DAMAGE. 25150970Srwatson * 26150970Srwatson * $FreeBSD$ 27150970Srwatson */ 28150970Srwatson 29150970Srwatson/* 30150970Srwatson * Back end to a variety of TCP-related benchmarks. This program accepts TCP 31150970Srwatson * connections on a port, and echoes all received data back to the sender. 32150970Srwatson * It is capable of handling only one connection at a time out of a single 33150970Srwatson * thread. 34150970Srwatson */ 35150970Srwatson#include <sys/types.h> 36150970Srwatson#include <sys/socket.h> 37150970Srwatson 38150970Srwatson#include <netinet/in.h> 39150970Srwatson 40150970Srwatson#include <arpa/inet.h> 41150970Srwatson 42150970Srwatson#include <err.h> 43150970Srwatson#include <stdlib.h> 44150970Srwatson#include <string.h> 45150970Srwatson#include <unistd.h> 46150970Srwatson 47150970Srwatson/* 48150970Srwatson * Simple micro-benchmark to see how many connections/second can be created 49150970Srwatson * in a serialized fashion against a given server. A timer signal is used 50150970Srwatson * to interrupt the loop and assess the cost, and uses a fixed maximum 51150970Srwatson * buffer size. It makes no attempt to time out old connections. 52150970Srwatson */ 53150970Srwatson#define BUFFERSIZE 128*1024 54150970Srwatson#define PORT 6060 55150970Srwatson 56150970Srwatsonstatic void 57150970Srwatsonhandle_connection(int accept_sock) 58150970Srwatson{ 59150970Srwatson u_char buffer[BUFFERSIZE]; 60150970Srwatson ssize_t len, recvlen, sofar; 61150970Srwatson int s; 62150970Srwatson 63150970Srwatson s = accept(accept_sock, NULL, NULL); 64150970Srwatson if (s < 0) { 65150970Srwatson warn("accept"); 66150970Srwatson return; 67150970Srwatson } 68150970Srwatson 69150970Srwatson while (1) { 70150970Srwatson recvlen = recv(s, buffer, BUFFERSIZE, 0); 71150970Srwatson if (recvlen < 0 || recvlen == 0) { 72150970Srwatson close(s); 73150970Srwatson return; 74150970Srwatson } 75150970Srwatson sofar = 0; 76150970Srwatson while (sofar < recvlen) { 77150970Srwatson len = send(s, buffer + sofar, recvlen - sofar, 0); 78150970Srwatson if (len < 0) { 79150970Srwatson close(s); 80150970Srwatson return; 81150970Srwatson } 82150970Srwatson sofar += len; 83150970Srwatson } 84150970Srwatson } 85150970Srwatson} 86150970Srwatson 87150970Srwatsonint 88150970Srwatsonmain(int argc, char *argv[]) 89150970Srwatson{ 90150970Srwatson struct sockaddr_in sin; 91150970Srwatson int accept_sock; 92150970Srwatson 93150970Srwatson accept_sock = socket(PF_INET, SOCK_STREAM, 0); 94150970Srwatson if (accept_sock < 0) 95150970Srwatson err(-1, "socket(PF_INET, SOCKET_STREAM, 0)"); 96150970Srwatson 97150970Srwatson bzero(&sin, sizeof(sin)); 98150970Srwatson sin.sin_family = AF_INET; 99150970Srwatson sin.sin_len = sizeof(sin); 100150970Srwatson sin.sin_addr.s_addr = INADDR_ANY; 101150970Srwatson sin.sin_port = htons(PORT); 102150970Srwatson 103150970Srwatson if (bind(accept_sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) 104150970Srwatson err(-1, "bind"); 105150970Srwatson 106150970Srwatson if (listen(accept_sock, -1) < 0) 107150970Srwatson err(-1, "listen"); 108150970Srwatson 109150970Srwatson while (1) 110150970Srwatson handle_connection(accept_sock); 111150970Srwatson 112150970Srwatson return (0); 113150970Srwatson} 114