FD.io VPP  v18.04-17-g3a0d853
Vector Packet Processing
socket_echo_server.c
Go to the documentation of this file.
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>
20 #include <arpa/inet.h>
21 #include <netdb.h>
22 #include <vppinfra/format.h>
23 #include <signal.h>
24 #include <sys/ucontext.h>
25 #include <sys/time.h>
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
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 {
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;
84  long rcvd = 0;
85  double deltat;
86 
87  if (argc > 1 && argc < 3)
88  {
89  fformat (stderr, "usage %s host port\n", argv[0]);
90  exit (0);
91  }
92 
93  if (argc >= 4)
94  {
95  no_echo = atoi (argv[3]);
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 
118 
119  sockfd = socket (AF_INET, SOCK_STREAM, 0);
120  if (sockfd < 0)
121  {
122  clib_unix_error ("socket");
123  exit (1);
124  }
125 
126  reuse = 1;
127  if (setsockopt (sockfd, SOL_SOCKET, SO_REUSEADDR, (const char *) &reuse,
128  sizeof (reuse)) < 0)
129  {
130  clib_unix_error ("setsockopt(SO_REUSEADDR) failed");
131  exit (1);
132  }
133 
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 
145  vec_validate (rx_buffer, 128 << 10);
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 
159  client_addr_len = sizeof (struct sockaddr);
160  accfd = accept (sockfd, (struct sockaddr *) &client, &client_addr_len);
161  if (accfd < 0)
162  {
163  clib_unix_warning ("accept");
164  continue;
165  }
166  fformat (stderr, "Accepted connection from: %s : %d\n",
167  inet_ntoa (client.sin_addr), client.sin_port);
168  gettimeofday (&start, NULL);
169 
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);
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;
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 
197  rcvd += n;
198  if (no_echo)
199  continue;
200 
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  */
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:434
static void setup_signal_handler(void)
#define NULL
Definition: clib.h:55
int i
int main(int argc, char *argv[])
#define clib_unix_error(format, args...)
Definition: error.h:65
volatile int signal_received
word fformat(FILE *f, char *fmt,...)
Definition: format.c:453
#define clib_warning(format, args...)
Definition: error.h:59
u64 uword
Definition: types.h:112
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
double f64
Definition: types.h:142
unsigned char u8
Definition: types.h:56
u8 * format_signal(u8 *s, va_list *args)
Definition: unix-formats.c:847
#define clib_unix_warning(format, args...)
Definition: error.h:68
static void unix_signal_handler(int signum, siginfo_t *si, ucontext_t *uc)