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:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <sys/types.h>
18 #include <sys/socket.h>
19 #include <netinet/in.h>
20 #include <arpa/inet.h>
22 #include <vppinfra/format.h>
24 #include <sys/ucontext.h>
27 volatile int signal_received;
30 unix_signal_handler (int signum, siginfo_t * si, ucontext_t * uc)
36 setup_signal_handler (void)
41 for (i = 1; i < 32; i++)
43 memset (&sa, 0, sizeof (sa));
44 sa.sa_sigaction = (void *) unix_signal_handler;
45 sa.sa_flags = SA_SIGINFO;
49 /* these signals take the default action */
57 /* ignore SIGPIPE, SIGCHLD */
60 sa.sa_sigaction = (void *) SIG_IGN;
63 /* catch and handle all other signals */
68 if (sigaction (i, &sa, 0) < 0)
69 clib_unix_warning ("sigaction %U", format_signal, i);
75 main (int argc, char *argv[])
77 int sockfd, portno, n, sent, accfd, reuse;
78 socklen_t client_addr_len;
79 struct sockaddr_in serv_addr;
80 struct sockaddr_in client;
81 struct hostent *server;
82 u8 *rx_buffer = 0, no_echo = 0;
83 struct timeval start, end;
87 if (argc > 1 && argc < 3)
89 fformat (stderr, "usage %s host port\n", argv[0]);
95 no_echo = atoi (argv[3]);
96 portno = atoi (argv[2]);
97 server = gethostbyname (argv[1]);
100 clib_unix_warning ("gethostbyname");
108 server = gethostbyname ("6.0.1.1");
111 clib_unix_warning ("gethostbyname");
117 setup_signal_handler ();
119 sockfd = socket (AF_INET, SOCK_STREAM, 0);
122 clib_unix_error ("socket");
127 if (setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR, (const char *) &reuse,
130 clib_unix_error ("setsockopt(SO_REUSEADDR) failed");
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)
141 clib_unix_warning ("bind");
145 vec_validate (rx_buffer, 128 << 10);
147 if (listen (sockfd, 5 /* backlog */ ) < 0)
149 clib_unix_warning ("listen");
159 client_addr_len = sizeof (struct sockaddr);
160 accfd = accept (sockfd, (struct sockaddr *) &client, &client_addr_len);
163 clib_unix_warning ("accept");
166 fformat (stderr, "Accepted connection from: %s : %d\n",
167 inet_ntoa (client.sin_addr), client.sin_port);
168 gettimeofday (&start, NULL);
172 n = recv (accfd, rx_buffer, vec_len (rx_buffer), 0 /* flags */ );
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");
189 clib_unix_warning ("recv");
201 sent = send (accfd, rx_buffer, n, 0 /* flags */ );
204 clib_unix_warning ("send");
211 clib_warning ("sent %d not %d", sent, n);
226 * fd.io coding-style-patch-verification: ON
229 * eval: (c-set-style "gnu")