Dave Wallace | 543852a | 2017-08-03 02:11:34 -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 <unistd.h> |
| 17 | #include <errno.h> |
| 18 | #include <sys/types.h> |
| 19 | #include <sys/socket.h> |
| 20 | #include <stdio.h> |
| 21 | #include <string.h> |
| 22 | #include <time.h> |
| 23 | #include <ctype.h> |
Dave Wallace | 5c7cf1c | 2017-10-24 04:12:18 -0400 | [diff] [blame] | 24 | #include <vcl/sock_test.h> |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 25 | #include <sys/stat.h> |
| 26 | #include <fcntl.h> |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 27 | |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 28 | #define SOCK_SERVER_USE_EPOLL 1 |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 29 | #define VPPCOM_SESSION_ATTR_UNIT_TEST 0 |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 30 | |
| 31 | #if SOCK_SERVER_USE_EPOLL |
| 32 | #include <sys/epoll.h> |
| 33 | #endif |
| 34 | |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 35 | #ifdef VCL_TEST |
| 36 | #if VPPCOM_SESSION_ATTR_UNIT_TEST |
| 37 | #define BUFLEN sizeof (uint64_t) * 16 |
| 38 | uint64_t buffer[16]; |
| 39 | uint32_t buflen = BUFLEN; |
| 40 | uint32_t *flags = (uint32_t *) buffer; |
| 41 | #endif |
| 42 | #endif |
| 43 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 44 | typedef struct |
| 45 | { |
| 46 | uint8_t is_alloc; |
| 47 | int fd; |
| 48 | uint8_t *buf; |
| 49 | uint32_t buf_size; |
| 50 | sock_test_cfg_t cfg; |
| 51 | sock_test_stats_t stats; |
| 52 | #ifdef VCL_TEST |
| 53 | vppcom_endpt_t endpt; |
| 54 | uint8_t ip[16]; |
| 55 | #endif |
| 56 | } sock_server_conn_t; |
| 57 | |
| 58 | #define SOCK_SERVER_MAX_TEST_CONN 10 |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 59 | #define SOCK_SERVER_MAX_EPOLL_EVENTS 10 |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 60 | typedef struct |
| 61 | { |
| 62 | int listen_fd; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 63 | #if SOCK_SERVER_USE_EPOLL |
| 64 | int epfd; |
| 65 | struct epoll_event listen_ev; |
| 66 | struct epoll_event wait_events[SOCK_SERVER_MAX_EPOLL_EVENTS]; |
| 67 | #endif |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 68 | size_t num_conn; |
| 69 | size_t conn_pool_size; |
| 70 | sock_server_conn_t *conn_pool; |
| 71 | int nfds; |
| 72 | fd_set rd_fdset; |
| 73 | fd_set wr_fdset; |
| 74 | struct timeval timeout; |
| 75 | } sock_server_main_t; |
| 76 | |
| 77 | sock_server_main_t sock_server_main; |
| 78 | |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 79 | #if ! SOCK_SERVER_USE_EPOLL |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 80 | static inline int |
| 81 | get_nfds (void) |
| 82 | { |
| 83 | sock_server_main_t *ssm = &sock_server_main; |
| 84 | int i, nfds; |
| 85 | |
| 86 | for (nfds = i = 0; i < FD_SETSIZE; i++) |
| 87 | { |
| 88 | if (FD_ISSET (i, &ssm->rd_fdset) || FD_ISSET (i, &ssm->wr_fdset)) |
| 89 | nfds = i + 1; |
| 90 | } |
| 91 | return nfds; |
| 92 | } |
| 93 | |
| 94 | static inline void |
| 95 | conn_fdset_set (sock_server_conn_t * conn, fd_set * fdset) |
| 96 | { |
| 97 | sock_server_main_t *ssm = &sock_server_main; |
| 98 | |
| 99 | FD_SET (conn->fd, fdset); |
| 100 | ssm->nfds = get_nfds (); |
| 101 | } |
| 102 | |
| 103 | static inline void |
| 104 | conn_fdset_clr (sock_server_conn_t * conn, fd_set * fdset) |
| 105 | { |
| 106 | sock_server_main_t *ssm = &sock_server_main; |
| 107 | |
| 108 | FD_CLR (conn->fd, fdset); |
| 109 | ssm->nfds = get_nfds (); |
| 110 | } |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 111 | #endif |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 112 | |
| 113 | static inline void |
| 114 | conn_pool_expand (size_t expand_size) |
| 115 | { |
| 116 | sock_server_main_t *ssm = &sock_server_main; |
| 117 | sock_server_conn_t *conn_pool; |
| 118 | size_t new_size = ssm->conn_pool_size + expand_size; |
| 119 | int i; |
| 120 | |
| 121 | conn_pool = realloc (ssm->conn_pool, new_size * sizeof (*ssm->conn_pool)); |
| 122 | if (conn_pool) |
| 123 | { |
| 124 | for (i = ssm->conn_pool_size; i < new_size; i++) |
| 125 | { |
| 126 | sock_server_conn_t *conn = &conn_pool[i]; |
| 127 | memset (conn, 0, sizeof (*conn)); |
| 128 | sock_test_cfg_init (&conn->cfg); |
| 129 | sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ , |
| 130 | &conn->buf, &conn->buf_size); |
| 131 | conn->cfg.txbuf_size = conn->cfg.rxbuf_size; |
| 132 | } |
| 133 | |
| 134 | ssm->conn_pool = conn_pool; |
| 135 | ssm->conn_pool_size = new_size; |
| 136 | } |
| 137 | else |
| 138 | { |
| 139 | int errno_val = errno; |
| 140 | perror ("ERROR in conn_pool_expand()"); |
| 141 | fprintf (stderr, "ERROR: Memory allocation failed (errno = %d)!\n", |
| 142 | errno_val); |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | static inline sock_server_conn_t * |
| 147 | conn_pool_alloc (void) |
| 148 | { |
| 149 | sock_server_main_t *ssm = &sock_server_main; |
| 150 | int i; |
| 151 | |
| 152 | for (i = 0; i < ssm->conn_pool_size; i++) |
| 153 | { |
| 154 | if (!ssm->conn_pool[i].is_alloc) |
| 155 | { |
| 156 | #ifdef VCL_TEST |
| 157 | ssm->conn_pool[i].endpt.ip = ssm->conn_pool[i].ip; |
| 158 | #endif |
| 159 | ssm->conn_pool[i].is_alloc = 1; |
| 160 | return (&ssm->conn_pool[i]); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static inline void |
| 168 | conn_pool_free (sock_server_conn_t * conn) |
| 169 | { |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 170 | #if ! SOCK_SERVER_USE_EPOLL |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 171 | sock_server_main_t *ssm = &sock_server_main; |
| 172 | |
| 173 | conn_fdset_clr (conn, &ssm->rd_fdset); |
| 174 | conn_fdset_clr (conn, &ssm->wr_fdset); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 175 | #endif |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 176 | conn->fd = 0; |
| 177 | conn->is_alloc = 0; |
| 178 | } |
| 179 | |
| 180 | static inline void |
| 181 | sync_config_and_reply (sock_server_conn_t * conn, sock_test_cfg_t * rx_cfg) |
| 182 | { |
| 183 | conn->cfg = *rx_cfg; |
| 184 | sock_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ , |
| 185 | &conn->buf, &conn->buf_size); |
| 186 | conn->cfg.txbuf_size = conn->cfg.rxbuf_size; |
| 187 | |
| 188 | if (conn->cfg.verbose) |
| 189 | { |
| 190 | printf ("\nSERVER (fd %d): Replying to cfg message!\n", conn->fd); |
| 191 | sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ ); |
| 192 | } |
| 193 | (void) sock_test_write (conn->fd, (uint8_t *) & conn->cfg, |
| 194 | sizeof (conn->cfg), NULL, conn->cfg.verbose); |
| 195 | } |
| 196 | |
| 197 | static void |
| 198 | stream_test_server_start_stop (sock_server_conn_t * conn, |
| 199 | sock_test_cfg_t * rx_cfg) |
| 200 | { |
| 201 | sock_server_main_t *ssm = &sock_server_main; |
| 202 | int client_fd = conn->fd; |
| 203 | sock_test_t test = rx_cfg->test; |
| 204 | |
| 205 | if (rx_cfg->ctrl_handle == conn->fd) |
| 206 | { |
| 207 | int i; |
| 208 | clock_gettime (CLOCK_REALTIME, &conn->stats.stop); |
| 209 | |
| 210 | for (i = 0; i < ssm->conn_pool_size; i++) |
| 211 | { |
| 212 | sock_server_conn_t *tc = &ssm->conn_pool[i]; |
| 213 | |
| 214 | if (tc->cfg.ctrl_handle == conn->fd) |
| 215 | { |
| 216 | sock_test_stats_accumulate (&conn->stats, &tc->stats); |
| 217 | |
| 218 | if (conn->cfg.verbose) |
| 219 | { |
| 220 | static char buf[64]; |
| 221 | |
| 222 | sprintf (buf, "SERVER (fd %d) RESULTS", tc->fd); |
| 223 | sock_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ , |
| 224 | test == SOCK_TEST_TYPE_BI |
| 225 | /* show tx */ , |
| 226 | conn->cfg.verbose); |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | sock_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ , |
| 232 | (test == SOCK_TEST_TYPE_BI) /* show_tx */ , |
| 233 | conn->cfg.verbose); |
| 234 | sock_test_cfg_dump (&conn->cfg, 0 /* is_client */ ); |
| 235 | if (conn->cfg.verbose) |
| 236 | { |
| 237 | printf (" sock server main\n" |
| 238 | SOCK_TEST_SEPARATOR_STRING |
| 239 | " buf: %p\n" |
| 240 | " buf size: %u (0x%08x)\n" |
| 241 | SOCK_TEST_SEPARATOR_STRING, |
| 242 | conn->buf, conn->buf_size, conn->buf_size); |
| 243 | } |
| 244 | |
| 245 | sync_config_and_reply (conn, rx_cfg); |
| 246 | printf ("\nSERVER (fd %d): %s-directional Stream Test Complete!\n" |
| 247 | SOCK_TEST_BANNER_STRING "\n", conn->fd, |
| 248 | test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni"); |
| 249 | } |
| 250 | else |
| 251 | { |
| 252 | printf ("\n" SOCK_TEST_BANNER_STRING |
| 253 | "SERVER (fd %d): %s-directional Stream Test!\n" |
| 254 | " Sending client the test cfg to start streaming data...\n", |
| 255 | client_fd, test == SOCK_TEST_TYPE_BI ? "Bi" : "Uni"); |
| 256 | |
| 257 | rx_cfg->ctrl_handle = (rx_cfg->ctrl_handle == ~0) ? conn->fd : |
| 258 | rx_cfg->ctrl_handle; |
| 259 | |
| 260 | sync_config_and_reply (conn, rx_cfg); |
| 261 | |
| 262 | /* read the 1st chunk, record start time */ |
| 263 | memset (&conn->stats, 0, sizeof (conn->stats)); |
| 264 | clock_gettime (CLOCK_REALTIME, &conn->stats.start); |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | |
| 269 | static inline void |
| 270 | stream_test_server (sock_server_conn_t * conn, int rx_bytes) |
| 271 | { |
| 272 | int client_fd = conn->fd; |
| 273 | sock_test_t test = conn->cfg.test; |
| 274 | |
| 275 | if (test == SOCK_TEST_TYPE_BI) |
| 276 | (void) sock_test_write (client_fd, conn->buf, rx_bytes, &conn->stats, |
| 277 | conn->cfg.verbose); |
| 278 | |
| 279 | if (conn->stats.rx_bytes >= conn->cfg.total_bytes) |
| 280 | { |
| 281 | clock_gettime (CLOCK_REALTIME, &conn->stats.stop); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | static inline void |
| 286 | new_client (void) |
| 287 | { |
| 288 | sock_server_main_t *ssm = &sock_server_main; |
| 289 | int client_fd; |
| 290 | sock_server_conn_t *conn; |
| 291 | |
| 292 | if (ssm->conn_pool_size < (ssm->num_conn + SOCK_SERVER_MAX_TEST_CONN + 1)) |
| 293 | conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1); |
| 294 | |
| 295 | conn = conn_pool_alloc (); |
| 296 | if (!conn) |
| 297 | { |
| 298 | fprintf (stderr, "\nERROR: No free connections!\n"); |
| 299 | return; |
| 300 | } |
| 301 | |
| 302 | #ifdef VCL_TEST |
| 303 | client_fd = vppcom_session_accept (ssm->listen_fd, &conn->endpt, |
| 304 | -1.0 /* wait forever */ ); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 305 | if (client_fd < 0) |
| 306 | errno = -client_fd; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 307 | #else |
| 308 | client_fd = accept (ssm->listen_fd, (struct sockaddr *) NULL, NULL); |
| 309 | #endif |
| 310 | if (client_fd < 0) |
| 311 | { |
| 312 | int errno_val; |
| 313 | errno_val = errno; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 314 | perror ("ERROR in new_client()"); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 315 | fprintf (stderr, "ERROR: accept failed (errno = %d)!\n", errno_val); |
| 316 | } |
| 317 | |
| 318 | printf ("SERVER: Got a connection -- fd = %d (0x%08x)!\n", |
| 319 | client_fd, client_fd); |
| 320 | |
| 321 | conn->fd = client_fd; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 322 | |
| 323 | #if ! SOCK_SERVER_USE_EPOLL |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 324 | conn_fdset_set (conn, &ssm->rd_fdset); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 325 | ssm->nfds++; |
| 326 | #else |
| 327 | { |
| 328 | struct epoll_event ev; |
| 329 | int rv; |
| 330 | |
| 331 | ev.events = EPOLLIN; |
| 332 | ev.data.u64 = conn - ssm->conn_pool; |
| 333 | #ifdef VCL_TEST |
| 334 | rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev); |
| 335 | if (rv) |
| 336 | errno = -rv; |
| 337 | #else |
| 338 | rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, client_fd, &ev); |
| 339 | #endif |
| 340 | if (rv < 0) |
| 341 | { |
| 342 | int errno_val; |
| 343 | errno_val = errno; |
| 344 | perror ("ERROR in new_client()"); |
| 345 | fprintf (stderr, "ERROR: epoll_ctl failed (errno = %d)!\n", |
| 346 | errno_val); |
| 347 | } |
| 348 | else |
| 349 | ssm->nfds++; |
| 350 | } |
| 351 | #endif |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | int |
| 355 | main (int argc, char **argv) |
| 356 | { |
| 357 | sock_server_main_t *ssm = &sock_server_main; |
| 358 | int client_fd, rv, main_rv = 0; |
| 359 | int tx_bytes, rx_bytes, nbytes; |
| 360 | sock_server_conn_t *conn; |
| 361 | sock_test_cfg_t *rx_cfg; |
| 362 | uint32_t xtra = 0; |
| 363 | uint64_t xtra_bytes = 0; |
| 364 | struct sockaddr_in servaddr; |
| 365 | int errno_val; |
| 366 | int v, i; |
| 367 | uint16_t port = SOCK_TEST_SERVER_PORT; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 368 | #if ! SOCK_SERVER_USE_EPOLL |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 369 | fd_set _rfdset, *rfdset = &_rfdset; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 370 | #endif |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 371 | #ifdef VCL_TEST |
| 372 | vppcom_endpt_t endpt; |
| 373 | #else |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 374 | #if ! SOCK_SERVER_USE_EPOLL |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 375 | fd_set _wfdset, *wfdset = &_wfdset; |
| 376 | #endif |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 377 | #endif |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 378 | |
| 379 | if ((argc == 2) && (sscanf (argv[1], "%d", &v) == 1)) |
| 380 | port = (uint16_t) v; |
| 381 | |
| 382 | conn_pool_expand (SOCK_SERVER_MAX_TEST_CONN + 1); |
| 383 | |
| 384 | #ifdef VCL_TEST |
| 385 | rv = vppcom_app_create ("vcl_test_server"); |
| 386 | if (rv) |
| 387 | { |
| 388 | errno = -rv; |
| 389 | ssm->listen_fd = -1; |
| 390 | } |
| 391 | else |
| 392 | { |
| 393 | ssm->listen_fd = |
| 394 | vppcom_session_create (VPPCOM_VRF_DEFAULT, VPPCOM_PROTO_TCP, |
| 395 | 0 /* is_nonblocking */ ); |
| 396 | } |
| 397 | #else |
| 398 | ssm->listen_fd = socket (AF_INET, SOCK_STREAM, 0); |
| 399 | #endif |
| 400 | if (ssm->listen_fd < 0) |
| 401 | { |
| 402 | errno_val = errno; |
| 403 | perror ("ERROR in main()"); |
| 404 | fprintf (stderr, "ERROR: socket() failed (errno = %d)!\n", errno_val); |
| 405 | return ssm->listen_fd; |
| 406 | } |
| 407 | |
| 408 | memset (&servaddr, 0, sizeof (servaddr)); |
| 409 | |
| 410 | servaddr.sin_family = AF_INET; |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 411 | servaddr.sin_addr.s_addr = htonl (INADDR_ANY); |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 412 | servaddr.sin_port = htons (port); |
| 413 | |
| 414 | #ifdef VCL_TEST |
| 415 | endpt.vrf = VPPCOM_VRF_DEFAULT; |
| 416 | endpt.is_ip4 = (servaddr.sin_family == AF_INET); |
| 417 | endpt.ip = (uint8_t *) & servaddr.sin_addr; |
| 418 | endpt.port = (uint16_t) servaddr.sin_port; |
| 419 | |
| 420 | rv = vppcom_session_bind (ssm->listen_fd, &endpt); |
| 421 | if (rv) |
| 422 | { |
| 423 | errno = -rv; |
| 424 | rv = -1; |
| 425 | } |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 426 | |
| 427 | #if VPPCOM_SESSION_ATTR_UNIT_TEST |
| 428 | buflen = BUFLEN; |
| 429 | if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_FLAGS, |
| 430 | buffer, &buflen) != VPPCOM_OK) |
| 431 | printf ("\nGET_FLAGS0: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n"); |
| 432 | buflen = BUFLEN; |
| 433 | *flags = O_RDWR | O_NONBLOCK; |
| 434 | if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_SET_FLAGS, |
| 435 | buffer, &buflen) != VPPCOM_OK) |
| 436 | printf ("\nSET_FLAGS1: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n"); |
| 437 | buflen = BUFLEN; |
| 438 | if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_FLAGS, |
| 439 | buffer, &buflen) != VPPCOM_OK) |
| 440 | printf ("\nGET_FLAGS1:Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n"); |
| 441 | *flags = O_RDWR; |
| 442 | buflen = BUFLEN; |
| 443 | if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_SET_FLAGS, |
| 444 | buffer, &buflen) != VPPCOM_OK) |
| 445 | printf ("\nSET_FLAGS2: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n"); |
| 446 | buflen = BUFLEN; |
| 447 | if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_FLAGS, |
| 448 | buffer, &buflen) != VPPCOM_OK) |
| 449 | printf ("\nGET_FLAGS2:Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n"); |
| 450 | |
| 451 | buflen = BUFLEN; |
| 452 | if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_PEER_ADDR, |
| 453 | buffer, &buflen) != VPPCOM_OK) |
| 454 | printf ("\nGET_PEER_ADDR: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n"); |
| 455 | buflen = BUFLEN; |
| 456 | if (vppcom_session_attr (ssm->listen_fd, VPPCOM_ATTR_GET_LCL_ADDR, |
| 457 | buffer, &buflen) != VPPCOM_OK) |
| 458 | printf ("\nGET_LCL_ADDR: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n"); |
| 459 | #endif |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 460 | #else |
| 461 | rv = |
| 462 | bind (ssm->listen_fd, (struct sockaddr *) &servaddr, sizeof (servaddr)); |
| 463 | #endif |
| 464 | if (rv < 0) |
| 465 | { |
| 466 | errno_val = errno; |
| 467 | perror ("ERROR in main()"); |
| 468 | fprintf (stderr, "ERROR: bind failed (errno = %d)!\n", errno_val); |
| 469 | return rv; |
| 470 | } |
| 471 | |
| 472 | #ifdef VCL_TEST |
| 473 | rv = vppcom_session_listen (ssm->listen_fd, 10); |
| 474 | if (rv) |
| 475 | { |
| 476 | errno = -rv; |
| 477 | rv = -1; |
| 478 | } |
| 479 | #else |
| 480 | rv = listen (ssm->listen_fd, 10); |
| 481 | #endif |
| 482 | if (rv < 0) |
| 483 | { |
| 484 | errno_val = errno; |
| 485 | perror ("ERROR in main()"); |
| 486 | fprintf (stderr, "ERROR: listen failed (errno = %d)!\n", errno_val); |
| 487 | return rv; |
| 488 | } |
| 489 | |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 490 | printf ("\nSERVER: Waiting for a client to connect on port %d...\n", port); |
| 491 | |
| 492 | #if ! SOCK_SERVER_USE_EPOLL |
| 493 | |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 494 | FD_ZERO (&ssm->wr_fdset); |
| 495 | FD_ZERO (&ssm->rd_fdset); |
| 496 | |
| 497 | FD_SET (ssm->listen_fd, &ssm->rd_fdset); |
| 498 | ssm->nfds = ssm->listen_fd + 1; |
| 499 | |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 500 | #else |
| 501 | #ifdef VCL_TEST |
| 502 | ssm->epfd = vppcom_epoll_create (); |
| 503 | if (ssm->epfd < 0) |
| 504 | errno = -ssm->epfd; |
| 505 | #else |
| 506 | ssm->epfd = epoll_create (1); |
| 507 | #endif |
| 508 | if (ssm->epfd < 0) |
| 509 | { |
| 510 | errno_val = errno; |
| 511 | perror ("ERROR in main()"); |
| 512 | fprintf (stderr, "ERROR: epoll_create failed (errno = %d)!\n", |
| 513 | errno_val); |
| 514 | return ssm->epfd; |
| 515 | } |
| 516 | |
| 517 | ssm->listen_ev.events = EPOLLIN; |
| 518 | ssm->listen_ev.data.u32 = ~0; |
| 519 | #ifdef VCL_TEST |
| 520 | rv = vppcom_epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd, |
| 521 | &ssm->listen_ev); |
| 522 | if (rv < 0) |
| 523 | errno = -rv; |
| 524 | #else |
| 525 | rv = epoll_ctl (ssm->epfd, EPOLL_CTL_ADD, ssm->listen_fd, &ssm->listen_ev); |
| 526 | #endif |
| 527 | if (rv < 0) |
| 528 | { |
| 529 | errno_val = errno; |
| 530 | perror ("ERROR in main()"); |
| 531 | fprintf (stderr, "ERROR: epoll_ctl failed (errno = %d)!\n", errno_val); |
| 532 | return rv; |
| 533 | } |
| 534 | #endif |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 535 | |
| 536 | while (1) |
| 537 | { |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 538 | #if ! SOCK_SERVER_USE_EPOLL |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 539 | _rfdset = ssm->rd_fdset; |
| 540 | |
| 541 | #ifdef VCL_TEST |
| 542 | rv = vppcom_select (ssm->nfds, (uint64_t *) rfdset, NULL, NULL, 0); |
| 543 | #else |
| 544 | { |
| 545 | struct timeval timeout; |
| 546 | timeout = ssm->timeout; |
| 547 | _wfdset = ssm->wr_fdset; |
| 548 | rv = select (ssm->nfds, rfdset, wfdset, NULL, &timeout); |
| 549 | } |
| 550 | #endif |
| 551 | if (rv < 0) |
| 552 | { |
| 553 | perror ("select()"); |
| 554 | fprintf (stderr, "\nERROR: select() failed -- aborting!\n"); |
| 555 | main_rv = -1; |
| 556 | goto done; |
| 557 | } |
| 558 | else if (rv == 0) |
| 559 | continue; |
| 560 | |
| 561 | if (FD_ISSET (ssm->listen_fd, rfdset)) |
| 562 | new_client (); |
| 563 | |
| 564 | for (i = 0; i < ssm->conn_pool_size; i++) |
| 565 | { |
| 566 | if (!ssm->conn_pool[i].is_alloc) |
| 567 | continue; |
| 568 | |
| 569 | conn = &ssm->conn_pool[i]; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 570 | #else |
| 571 | int num_ev; |
| 572 | #ifdef VCL_TEST |
| 573 | num_ev = vppcom_epoll_wait (ssm->epfd, ssm->wait_events, |
| 574 | SOCK_SERVER_MAX_EPOLL_EVENTS, 60.0); |
Dave Wallace | 0fd6ad6 | 2017-10-20 13:23:40 -0400 | [diff] [blame] | 575 | if (num_ev < 0) |
| 576 | errno = -num_ev; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 577 | #else |
| 578 | num_ev = epoll_wait (ssm->epfd, ssm->wait_events, |
| 579 | SOCK_SERVER_MAX_EPOLL_EVENTS, 60000); |
| 580 | #endif |
| 581 | if (num_ev < 0) |
| 582 | { |
| 583 | perror ("epoll_wait()"); |
| 584 | fprintf (stderr, "\nERROR: epoll_wait() failed -- aborting!\n"); |
| 585 | main_rv = -1; |
| 586 | goto done; |
| 587 | } |
| 588 | if (num_ev == 0) |
| 589 | { |
| 590 | fprintf (stderr, "\nepoll_wait() timeout!\n"); |
| 591 | continue; |
| 592 | } |
| 593 | for (i = 0; i < num_ev; i++) |
| 594 | { |
| 595 | if (ssm->wait_events[i].data.u32 == ~0) |
| 596 | { |
| 597 | new_client (); |
| 598 | continue; |
| 599 | } |
| 600 | conn = &ssm->conn_pool[ssm->wait_events[i].data.u32]; |
| 601 | #endif |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 602 | client_fd = conn->fd; |
| 603 | |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 604 | #if ! SOCK_SERVER_USE_EPOLL |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 605 | if (FD_ISSET (client_fd, rfdset)) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 606 | #else |
| 607 | if (EPOLLIN & ssm->wait_events[i].events) |
| 608 | #endif |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 609 | { |
Dave Wallace | 35830af | 2017-10-09 01:43:42 -0400 | [diff] [blame] | 610 | #ifdef VCL_TEST |
| 611 | #if VPPCOM_SESSION_ATTR_UNIT_TEST |
| 612 | buflen = BUFLEN; |
| 613 | if (vppcom_session_attr (client_fd, VPPCOM_ATTR_GET_NREAD, |
| 614 | buffer, &buflen) < VPPCOM_OK) |
| 615 | printf ("\nNREAD: Oh no, Mr. Biiiiiiiiiiiilllllll ! ! ! !\n"); |
| 616 | if (vppcom_session_attr (client_fd, |
| 617 | VPPCOM_ATTR_GET_PEER_ADDR, |
| 618 | buffer, &buflen) != VPPCOM_OK) |
| 619 | printf ("\nGET_PEER_ADDR: Oh no, Mr. " |
| 620 | "Biiiiiiiiiiiilllllll ! ! ! !\n"); |
| 621 | buflen = BUFLEN; |
| 622 | if (vppcom_session_attr (client_fd, VPPCOM_ATTR_GET_LCL_ADDR, |
| 623 | buffer, &buflen) != VPPCOM_OK) |
| 624 | printf ("\nGET_LCL_ADDR: Oh no, Mr. " |
| 625 | "Biiiiiiiiiiiilllllll ! ! ! !\n"); |
| 626 | #endif |
| 627 | #endif |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 628 | rx_bytes = sock_test_read (client_fd, conn->buf, |
| 629 | conn->buf_size, &conn->stats); |
| 630 | if (rx_bytes > 0) |
| 631 | { |
| 632 | rx_cfg = (sock_test_cfg_t *) conn->buf; |
| 633 | if (rx_cfg->magic == SOCK_TEST_CFG_CTRL_MAGIC) |
| 634 | { |
| 635 | if (rx_cfg->verbose) |
| 636 | { |
| 637 | printf ("SERVER (fd %d): Received a cfg message!\n", |
| 638 | client_fd); |
| 639 | sock_test_cfg_dump (rx_cfg, 0 /* is_client */ ); |
| 640 | } |
| 641 | |
| 642 | if (rx_bytes != sizeof (*rx_cfg)) |
| 643 | { |
| 644 | printf ("SERVER (fd %d): Invalid cfg message " |
| 645 | "size (%d)!\n Should be %lu bytes.\n", |
| 646 | client_fd, rx_bytes, sizeof (*rx_cfg)); |
| 647 | conn->cfg.rxbuf_size = 0; |
| 648 | conn->cfg.num_writes = 0; |
| 649 | if (conn->cfg.verbose) |
| 650 | { |
| 651 | printf ("SERVER (fd %d): Replying to " |
| 652 | "cfg message!\n", client_fd); |
| 653 | sock_test_cfg_dump (rx_cfg, 0 /* is_client */ ); |
| 654 | } |
| 655 | sock_test_write (client_fd, (uint8_t *) & conn->cfg, |
| 656 | sizeof (conn->cfg), NULL, |
| 657 | conn->cfg.verbose); |
| 658 | continue; |
| 659 | } |
| 660 | |
| 661 | switch (rx_cfg->test) |
| 662 | { |
| 663 | case SOCK_TEST_TYPE_NONE: |
| 664 | case SOCK_TEST_TYPE_ECHO: |
| 665 | sync_config_and_reply (conn, rx_cfg); |
| 666 | break; |
| 667 | |
| 668 | case SOCK_TEST_TYPE_BI: |
| 669 | case SOCK_TEST_TYPE_UNI: |
| 670 | stream_test_server_start_stop (conn, rx_cfg); |
| 671 | break; |
| 672 | |
| 673 | case SOCK_TEST_TYPE_EXIT: |
| 674 | printf ("SERVER: Have a great day, " |
| 675 | "connection %d!\n", client_fd); |
| 676 | #ifdef VCL_TEST |
| 677 | vppcom_session_close (client_fd); |
| 678 | #else |
| 679 | close (client_fd); |
| 680 | #endif |
| 681 | conn_pool_free (conn); |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 682 | #if ! SOCK_SERVER_USE_EPOLL |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 683 | if (ssm->nfds == (ssm->listen_fd + 1)) |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 684 | #else |
| 685 | ssm->nfds--; |
| 686 | if (!ssm->nfds) |
| 687 | #endif |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 688 | { |
| 689 | printf ("SERVER: All client connections " |
| 690 | "closed.\n\nSERVER: " |
| 691 | "May the force be with you!\n\n"); |
| 692 | goto done; |
| 693 | } |
| 694 | break; |
| 695 | |
| 696 | default: |
| 697 | fprintf (stderr, "ERROR: Unknown test type!\n"); |
| 698 | sock_test_cfg_dump (rx_cfg, 0 /* is_client */ ); |
| 699 | break; |
| 700 | } |
| 701 | continue; |
| 702 | } |
| 703 | |
| 704 | else if ((conn->cfg.test == SOCK_TEST_TYPE_UNI) || |
| 705 | (conn->cfg.test == SOCK_TEST_TYPE_BI)) |
| 706 | { |
| 707 | stream_test_server (conn, rx_bytes); |
| 708 | continue; |
| 709 | } |
| 710 | |
Chris Luke | 879ace3 | 2017-09-26 13:15:16 -0400 | [diff] [blame] | 711 | else if (isascii (conn->buf[0])) |
Chris Luke | ab7b8d9 | 2017-09-07 07:40:13 -0400 | [diff] [blame] | 712 | { |
| 713 | // If it looks vaguely like a string, make sure it's terminated |
| 714 | ((char *) conn->buf)[rx_bytes < |
| 715 | conn->buf_size ? rx_bytes : |
| 716 | conn->buf_size - 1] = 0; |
Dave Wallace | f7f809c | 2017-10-03 01:48:42 -0400 | [diff] [blame] | 717 | printf ("SERVER (fd %d): RX (%d bytes) - '%s'\n", |
Chris Luke | ab7b8d9 | 2017-09-07 07:40:13 -0400 | [diff] [blame] | 718 | conn->fd, rx_bytes, conn->buf); |
| 719 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 720 | } |
| 721 | else // rx_bytes < 0 |
| 722 | { |
| 723 | if (errno == ECONNRESET) |
| 724 | { |
| 725 | printf ("\nSERVER: Connection reset by remote peer.\n" |
| 726 | " Y'all have a great day now!\n\n"); |
| 727 | break; |
| 728 | } |
| 729 | else |
| 730 | continue; |
| 731 | } |
| 732 | |
Chris Luke | 879ace3 | 2017-09-26 13:15:16 -0400 | [diff] [blame] | 733 | if (isascii (conn->buf[0])) |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 734 | { |
Chris Luke | 879ace3 | 2017-09-26 13:15:16 -0400 | [diff] [blame] | 735 | // If it looks vaguely like a string, make sure it's terminated |
| 736 | ((char *) conn->buf)[rx_bytes < |
| 737 | conn->buf_size ? rx_bytes : |
| 738 | conn->buf_size - 1] = 0; |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 739 | if (xtra) |
| 740 | fprintf (stderr, |
| 741 | "ERROR: FIFO not drained in previous test!\n" |
| 742 | " extra chunks %u (0x%x)\n" |
| 743 | " extra bytes %lu (0x%lx)\n", |
| 744 | xtra, xtra, xtra_bytes, xtra_bytes); |
| 745 | |
| 746 | xtra = 0; |
| 747 | xtra_bytes = 0; |
| 748 | |
| 749 | if (conn->cfg.verbose) |
| 750 | printf ("SERVER (fd %d): Echoing back\n", client_fd); |
| 751 | |
| 752 | nbytes = strlen ((const char *) conn->buf) + 1; |
| 753 | |
| 754 | tx_bytes = sock_test_write (client_fd, conn->buf, |
| 755 | nbytes, &conn->stats, |
| 756 | conn->cfg.verbose); |
| 757 | if (tx_bytes >= 0) |
| 758 | printf ("SERVER (fd %d): TX (%d bytes) - '%s'\n", |
| 759 | conn->fd, tx_bytes, conn->buf); |
| 760 | } |
| 761 | |
| 762 | else // Extraneous read data from non-echo tests??? |
| 763 | { |
| 764 | xtra++; |
| 765 | xtra_bytes += rx_bytes; |
| 766 | } |
| 767 | } |
| 768 | } |
| 769 | } |
| 770 | |
| 771 | done: |
| 772 | #ifdef VCL_TEST |
| 773 | vppcom_session_close (ssm->listen_fd); |
| 774 | vppcom_app_destroy (); |
| 775 | #else |
| 776 | close (ssm->listen_fd); |
| 777 | #endif |
| 778 | if (ssm->conn_pool) |
| 779 | free (ssm->conn_pool); |
| 780 | |
| 781 | return main_rv; |
| 782 | } |
| 783 | |
| 784 | /* |
| 785 | * fd.io coding-style-patch-verification: ON |
| 786 | * |
| 787 | * Local Variables: |
| 788 | * eval: (c-set-style "gnu") |
| 789 | * End: |
| 790 | */ |