blob: 4f4c5f30803ee207607fb806f8a51e781d95e2d1 [file] [log] [blame]
Dave Barachfb5b2af2017-04-17 15:56:17 -04001/*
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 Corasf6d68ed2017-05-07 19:12:02 -070020#include <arpa/inet.h>
Dave Barachfb5b2af2017-04-17 15:56:17 -040021#include <netdb.h>
22#include <vppinfra/format.h>
23#include <signal.h>
24#include <sys/ucontext.h>
Florin Corasf03a59a2017-06-09 21:07:32 -070025#include <sys/time.h>
Dave Barachfb5b2af2017-04-17 15:56:17 -040026
27volatile int signal_received;
28
29static void
30unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
31{
32 signal_received = 1;
33}
34
35static void
36setup_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
74int
75main (int argc, char *argv[])
76{
Florin Corasf6d68ed2017-05-07 19:12:02 -070077 int sockfd, portno, n, sent, accfd, reuse;
78 socklen_t client_addr_len;
Dave Barachfb5b2af2017-04-17 15:56:17 -040079 struct sockaddr_in serv_addr;
Florin Corasf6d68ed2017-05-07 19:12:02 -070080 struct sockaddr_in client;
Dave Barachfb5b2af2017-04-17 15:56:17 -040081 struct hostent *server;
Florin Corasf03a59a2017-06-09 21:07:32 -070082 u8 *rx_buffer = 0, no_echo = 0;
83 struct timeval start, end;
84 long rcvd = 0;
85 double deltat;
Dave Barachfb5b2af2017-04-17 15:56:17 -040086
Florin Corasf6d68ed2017-05-07 19:12:02 -070087 if (argc > 1 && argc < 3)
Dave Barachfb5b2af2017-04-17 15:56:17 -040088 {
Florin Corasf6d68ed2017-05-07 19:12:02 -070089 fformat (stderr, "usage %s host port\n", argv[0]);
Dave Barachfb5b2af2017-04-17 15:56:17 -040090 exit (0);
91 }
92
Florin Corasf03a59a2017-06-09 21:07:32 -070093 if (argc >= 4)
Florin Corasf6d68ed2017-05-07 19:12:02 -070094 {
Florin Corasf03a59a2017-06-09 21:07:32 -070095 no_echo = atoi (argv[3]);
Florin Corasf6d68ed2017-05-07 19:12:02 -070096 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 Barachfb5b2af2017-04-17 15:56:17 -0400117 setup_signal_handler ();
118
Dave Barachfb5b2af2017-04-17 15:56:17 -0400119 sockfd = socket (AF_INET, SOCK_STREAM, 0);
120 if (sockfd < 0)
121 {
122 clib_unix_error ("socket");
123 exit (1);
124 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700125
126 reuse = 1;
127 if (setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR, (const char *) &reuse,
128 sizeof (reuse)) < 0)
Dave Barachfb5b2af2017-04-17 15:56:17 -0400129 {
Florin Corasf6d68ed2017-05-07 19:12:02 -0700130 clib_unix_error ("setsockopt(SO_REUSEADDR) failed");
Dave Barachfb5b2af2017-04-17 15:56:17 -0400131 exit (1);
132 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700133
Dave Barachfb5b2af2017-04-17 15:56:17 -0400134 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 Corasf03a59a2017-06-09 21:07:32 -0700145 vec_validate (rx_buffer, 128 << 10);
Dave Barachfb5b2af2017-04-17 15:56:17 -0400146
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 Corasf6d68ed2017-05-07 19:12:02 -0700159 client_addr_len = sizeof (struct sockaddr);
160 accfd = accept (sockfd, (struct sockaddr *) &client, &client_addr_len);
Dave Barachfb5b2af2017-04-17 15:56:17 -0400161 if (accfd < 0)
162 {
163 clib_unix_warning ("accept");
164 continue;
165 }
Florin Corasf6d68ed2017-05-07 19:12:02 -0700166 fformat (stderr, "Accepted connection from: %s : %d\n",
167 inet_ntoa (client.sin_addr), client.sin_port);
Florin Corasf03a59a2017-06-09 21:07:32 -0700168 gettimeofday (&start, NULL);
169
Dave Barachfb5b2af2017-04-17 15:56:17 -0400170 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 Corasf03a59a2017-06-09 21:07:32 -0700177 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 Barachfb5b2af2017-04-17 15:56:17 -0400185 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 Corasf03a59a2017-06-09 21:07:32 -0700197 rcvd += n;
198 if (no_echo)
199 continue;
200
Dave Barachfb5b2af2017-04-17 15:56:17 -0400201 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 */