191094Sdes/*- 2115619Sdes * Copyright (c) 2005 Robert N. M. Watson 3228690Sdes * All rights reserved. 491094Sdes * 591094Sdes * Redistribution and use in source and binary forms, with or without 691094Sdes * modification, are permitted provided that the following conditions 799158Sdes * are met: 899158Sdes * 1. Redistributions of source code must retain the above copyright 999158Sdes * notice, this list of conditions and the following disclaimer. 1091094Sdes * 2. Redistributions in binary form must reproduce the above copyright 1191094Sdes * notice, this list of conditions and the following disclaimer in the 1291094Sdes * documentation and/or other materials provided with the distribution. 1391094Sdes * 1491094Sdes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1591094Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1691094Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1791094Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1891094Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 1991094Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2091094Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2191094Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2291094Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2391094Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2491094Sdes * SUCH DAMAGE. 2591094Sdes */ 2691094Sdes 2791094Sdes/* 2891094Sdes * Back end to a variety of TCP-related benchmarks. This program accepts TCP 2991094Sdes * connections on a port, and echoes all received data back to the sender. 3091094Sdes * It is capable of handling only one connection at a time out of a single 3191094Sdes * thread. 3291094Sdes */ 3391094Sdes#include <sys/types.h> 3491094Sdes#include <sys/socket.h> 35263421Sdes 3691094Sdes#include <netinet/in.h> 3791094Sdes 38228690Sdes#include <arpa/inet.h> 39228690Sdes 40228690Sdes#include <err.h> 41228690Sdes#include <stdlib.h> 4291094Sdes#include <string.h> 43263421Sdes#include <unistd.h> 44263421Sdes 4591094Sdes/* 46117610Sdes * Simple micro-benchmark to see how many connections/second can be created 47263421Sdes * in a serialized fashion against a given server. A timer signal is used 4891100Sdes * to interrupt the loop and assess the cost, and uses a fixed maximum 4991094Sdes * buffer size. It makes no attempt to time out old connections. 5091094Sdes */ 5191094Sdes#define BUFFERSIZE 128*1024 5291094Sdes#define PORT 6060 5391100Sdes 5491094Sdesstatic void 5591094Sdeshandle_connection(int accept_sock) 5691094Sdes{ 57107937Sdes u_char buffer[BUFFERSIZE]; 58107937Sdes ssize_t len, recvlen, sofar; 5991100Sdes int s; 6091100Sdes 61263421Sdes s = accept(accept_sock, NULL, NULL); 62263421Sdes if (s < 0) { 63263421Sdes warn("accept"); 64263421Sdes return; 65263421Sdes } 6691100Sdes 67263421Sdes while (1) { 6891100Sdes recvlen = recv(s, buffer, BUFFERSIZE, 0); 69117610Sdes if (recvlen < 0 || recvlen == 0) { 70263421Sdes close(s); 71263421Sdes return; 72263421Sdes } 73263421Sdes sofar = 0; 74263421Sdes while (sofar < recvlen) { 75263421Sdes len = send(s, buffer + sofar, recvlen - sofar, 0); 76263421Sdes if (len < 0) { 7791100Sdes close(s); 7891100Sdes return; 79263421Sdes } 80263421Sdes sofar += len; 81263421Sdes } 82263421Sdes } 83263421Sdes} 8491100Sdes 85263421Sdesint 86263421Sdesmain(int argc, char *argv[]) 87263421Sdes{ 88263421Sdes struct sockaddr_in sin; 89263421Sdes int accept_sock; 90263421Sdes 91263421Sdes accept_sock = socket(PF_INET, SOCK_STREAM, 0); 92263421Sdes if (accept_sock < 0) 93263421Sdes err(-1, "socket(PF_INET, SOCKET_STREAM, 0)"); 94117610Sdes 9591100Sdes bzero(&sin, sizeof(sin)); 96263421Sdes sin.sin_family = AF_INET; 97263421Sdes sin.sin_len = sizeof(sin); 98263421Sdes sin.sin_addr.s_addr = INADDR_ANY; 99263421Sdes sin.sin_port = htons(PORT); 100263421Sdes 101263421Sdes if (bind(accept_sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) 102263421Sdes err(-1, "bind"); 103263421Sdes 104263421Sdes if (listen(accept_sock, -1) < 0) 105263421Sdes err(-1, "listen"); 106263421Sdes 107263421Sdes while (1) 108263421Sdes handle_connection(accept_sock); 109263421Sdes 110263421Sdes return (0); 111263421Sdes} 112263421Sdes