Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
| 16 | #include <stdio.h> |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/socket.h> |
| 19 | #include <netinet/in.h> |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 20 | #include <arpa/inet.h> |
Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 21 | #include <netdb.h> |
| 22 | #include <vppinfra/format.h> |
| 23 | #include <signal.h> |
| 24 | #include <sys/ucontext.h> |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 25 | #include <sys/time.h> |
Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 26 | |
| 27 | volatile int signal_received; |
| 28 | |
| 29 | static void |
| 30 | unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc) |
| 31 | { |
| 32 | signal_received = 1; |
| 33 | } |
| 34 | |
| 35 | static void |
| 36 | setup_signal_handler (void) |
| 37 | { |
| 38 | uword i; |
| 39 | struct sigaction sa; |
| 40 | |
| 41 | for (i = 1; i < 32; i++) |
| 42 | { |
| 43 | memset (&sa, 0, sizeof (sa)); |
| 44 | sa.sa_sigaction = (void *) unix_signal_handler; |
| 45 | sa.sa_flags = SA_SIGINFO; |
| 46 | |
| 47 | switch (i) |
| 48 | { |
| 49 | /* these signals take the default action */ |
| 50 | case SIGABRT: |
| 51 | case SIGKILL: |
| 52 | case SIGSTOP: |
| 53 | case SIGUSR1: |
| 54 | case SIGUSR2: |
| 55 | continue; |
| 56 | |
| 57 | /* ignore SIGPIPE, SIGCHLD */ |
| 58 | case SIGPIPE: |
| 59 | case SIGCHLD: |
| 60 | sa.sa_sigaction = (void *) SIG_IGN; |
| 61 | break; |
| 62 | |
| 63 | /* catch and handle all other signals */ |
| 64 | default: |
| 65 | break; |
| 66 | } |
| 67 | |
| 68 | if (sigaction (i, &sa, 0) < 0) |
| 69 | clib_unix_warning ("sigaction %U", format_signal, i); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | |
| 74 | int |
| 75 | main (int argc, char *argv[]) |
| 76 | { |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 77 | int sockfd, portno, n, sent, accfd, reuse; |
| 78 | socklen_t client_addr_len; |
Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 79 | struct sockaddr_in serv_addr; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 80 | struct sockaddr_in client; |
Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 81 | struct hostent *server; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 82 | u8 *rx_buffer = 0, no_echo = 0; |
| 83 | struct timeval start, end; |
| 84 | long rcvd = 0; |
| 85 | double deltat; |
Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 86 | |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 87 | if (argc > 1 && argc < 3) |
Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 88 | { |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 89 | fformat (stderr, "usage %s host port\n", argv[0]); |
Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 90 | exit (0); |
| 91 | } |
| 92 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 93 | if (argc >= 4) |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 94 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 95 | no_echo = atoi (argv[3]); |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 96 | portno = atoi (argv[2]); |
| 97 | server = gethostbyname (argv[1]); |
| 98 | if (server == NULL) |
| 99 | { |
| 100 | clib_unix_warning ("gethostbyname"); |
| 101 | exit (1); |
| 102 | } |
| 103 | } |
| 104 | else |
| 105 | { |
| 106 | /* Defaults */ |
| 107 | portno = 1234; |
| 108 | server = gethostbyname ("6.0.1.1"); |
| 109 | if (server == NULL) |
| 110 | { |
| 111 | clib_unix_warning ("gethostbyname"); |
| 112 | exit (1); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | |
Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 117 | setup_signal_handler (); |
| 118 | |
Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 119 | sockfd = socket (AF_INET, SOCK_STREAM, 0); |
| 120 | if (sockfd < 0) |
| 121 | { |
| 122 | clib_unix_error ("socket"); |
| 123 | exit (1); |
| 124 | } |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 125 | |
| 126 | reuse = 1; |
| 127 | if (setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR, (const char *) &reuse, |
| 128 | sizeof (reuse)) < 0) |
Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 129 | { |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 130 | clib_unix_error ("setsockopt(SO_REUSEADDR) failed"); |
Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 131 | exit (1); |
| 132 | } |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 133 | |
Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 134 | bzero ((char *) &serv_addr, sizeof (serv_addr)); |
| 135 | serv_addr.sin_family = AF_INET; |
| 136 | bcopy ((char *) server->h_addr, |
| 137 | (char *) &serv_addr.sin_addr.s_addr, server->h_length); |
| 138 | serv_addr.sin_port = htons (portno); |
| 139 | if (bind (sockfd, (const void *) &serv_addr, sizeof (serv_addr)) < 0) |
| 140 | { |
| 141 | clib_unix_warning ("bind"); |
| 142 | exit (1); |
| 143 | } |
| 144 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 145 | vec_validate (rx_buffer, 128 << 10); |
Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 146 | |
| 147 | if (listen (sockfd, 5 /* backlog */ ) < 0) |
| 148 | { |
| 149 | clib_unix_warning ("listen"); |
| 150 | close (sockfd); |
| 151 | return 1; |
| 152 | } |
| 153 | |
| 154 | while (1) |
| 155 | { |
| 156 | if (signal_received) |
| 157 | break; |
| 158 | |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 159 | client_addr_len = sizeof (struct sockaddr); |
| 160 | accfd = accept (sockfd, (struct sockaddr *) &client, &client_addr_len); |
Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 161 | if (accfd < 0) |
| 162 | { |
| 163 | clib_unix_warning ("accept"); |
| 164 | continue; |
| 165 | } |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 166 | fformat (stderr, "Accepted connection from: %s : %d\n", |
| 167 | inet_ntoa (client.sin_addr), client.sin_port); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 168 | gettimeofday (&start, NULL); |
| 169 | |
Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 170 | while (1) |
| 171 | { |
| 172 | n = recv (accfd, rx_buffer, vec_len (rx_buffer), 0 /* flags */ ); |
| 173 | if (n == 0) |
| 174 | { |
| 175 | /* Graceful exit */ |
| 176 | close (accfd); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 177 | gettimeofday (&end, NULL); |
| 178 | deltat = (end.tv_sec - start.tv_sec); |
| 179 | deltat += (end.tv_usec - start.tv_usec) / 1000000.0; |
| 180 | clib_warning ("Finished in %.6f", deltat); |
| 181 | clib_warning ("%.4f Gbit/second %s", |
| 182 | (((f64) rcvd * 8.0) / deltat / 1e9), |
| 183 | no_echo ? "half" : "full"); |
| 184 | rcvd = 0; |
Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 185 | break; |
| 186 | } |
| 187 | if (n < 0) |
| 188 | { |
| 189 | clib_unix_warning ("recv"); |
| 190 | close (accfd); |
| 191 | break; |
| 192 | } |
| 193 | |
| 194 | if (signal_received) |
| 195 | break; |
| 196 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 197 | rcvd += n; |
| 198 | if (no_echo) |
| 199 | continue; |
| 200 | |
Dave Barach | fb5b2af | 2017-04-17 15:56:17 -0400 | [diff] [blame] | 201 | sent = send (accfd, rx_buffer, n, 0 /* flags */ ); |
| 202 | if (n < 0) |
| 203 | { |
| 204 | clib_unix_warning ("send"); |
| 205 | close (accfd); |
| 206 | break; |
| 207 | } |
| 208 | |
| 209 | if (sent != n) |
| 210 | { |
| 211 | clib_warning ("sent %d not %d", sent, n); |
| 212 | } |
| 213 | |
| 214 | if (signal_received) |
| 215 | break; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | close (sockfd); |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | |
| 225 | /* |
| 226 | * fd.io coding-style-patch-verification: ON |
| 227 | * |
| 228 | * Local Variables: |
| 229 | * eval: (c-set-style "gnu") |
| 230 | * End: |
| 231 | */ |