Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 1 | /* |
Florin Coras | 5e06257 | 2019-03-14 19:07:51 -0700 | [diff] [blame] | 2 | * Copyright (c) 2017-2019 Cisco and/or its affiliates. |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 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 | |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 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> |
| 24 | #include <sys/stat.h> |
| 25 | #include <fcntl.h> |
| 26 | #include <vcl/vcl_test.h> |
| 27 | #include <sys/epoll.h> |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 28 | #include <vppinfra/mem.h> |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 29 | #include <pthread.h> |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 30 | |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 31 | typedef struct |
| 32 | { |
| 33 | uint8_t is_alloc; |
| 34 | int fd; |
| 35 | uint8_t *buf; |
| 36 | uint32_t buf_size; |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 37 | vcl_test_cfg_t cfg; |
| 38 | vcl_test_stats_t stats; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 39 | vppcom_endpt_t endpt; |
| 40 | uint8_t ip[16]; |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 41 | vppcom_data_segments_t ds; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 42 | } vcl_test_server_conn_t; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 43 | |
| 44 | typedef struct |
| 45 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 46 | uint16_t port; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 47 | uint32_t address_ip6; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 48 | u8 proto; |
| 49 | u8 workers; |
| 50 | vppcom_endpt_t endpt; |
| 51 | } vcl_test_server_cfg_t; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 52 | |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 53 | typedef struct |
| 54 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 55 | uint32_t wrk_index; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 56 | int listen_fd; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 57 | int epfd; |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 58 | struct epoll_event wait_events[VCL_TEST_CFG_MAX_EPOLL_EVENTS]; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 59 | size_t conn_pool_size; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 60 | vcl_test_server_conn_t *conn_pool; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 61 | int nfds; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 62 | pthread_t thread_handle; |
| 63 | } vcl_test_server_worker_t; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 64 | |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 65 | typedef struct |
| 66 | { |
| 67 | vcl_test_server_cfg_t cfg; |
| 68 | vcl_test_server_worker_t *workers; |
| 69 | |
| 70 | struct sockaddr_storage servaddr; |
| 71 | volatile int worker_fails; |
| 72 | volatile int active_workers; |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 73 | u8 use_ds; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 74 | } vcl_test_server_main_t; |
| 75 | |
| 76 | static __thread int __wrk_index = 0; |
| 77 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 78 | static vcl_test_server_main_t vcl_server_main; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 79 | |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 80 | static inline void |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 81 | conn_pool_expand (vcl_test_server_worker_t * wrk, size_t expand_size) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 82 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 83 | vcl_test_server_conn_t *conn_pool; |
| 84 | size_t new_size = wrk->conn_pool_size + expand_size; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 85 | int i; |
| 86 | |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 87 | conn_pool = realloc (wrk->conn_pool, new_size * sizeof (*wrk->conn_pool)); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 88 | if (conn_pool) |
| 89 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 90 | for (i = wrk->conn_pool_size; i < new_size; i++) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 91 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 92 | vcl_test_server_conn_t *conn = &conn_pool[i]; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 93 | memset (conn, 0, sizeof (*conn)); |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 94 | vcl_test_cfg_init (&conn->cfg); |
| 95 | vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ , |
| 96 | &conn->buf, &conn->buf_size); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 97 | conn->cfg.txbuf_size = conn->cfg.rxbuf_size; |
| 98 | } |
| 99 | |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 100 | wrk->conn_pool = conn_pool; |
| 101 | wrk->conn_pool_size = new_size; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 102 | } |
| 103 | else |
| 104 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 105 | vterr ("conn_pool_expand()", -errno); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 109 | static inline vcl_test_server_conn_t * |
| 110 | conn_pool_alloc (vcl_test_server_worker_t * wrk) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 111 | { |
Florin Coras | 539663c | 2018-09-28 14:59:37 -0700 | [diff] [blame] | 112 | int i, expand = 0; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 113 | |
Florin Coras | 539663c | 2018-09-28 14:59:37 -0700 | [diff] [blame] | 114 | again: |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 115 | for (i = 0; i < wrk->conn_pool_size; i++) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 116 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 117 | if (!wrk->conn_pool[i].is_alloc) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 118 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 119 | wrk->conn_pool[i].endpt.ip = wrk->conn_pool[i].ip; |
| 120 | wrk->conn_pool[i].is_alloc = 1; |
| 121 | return (&wrk->conn_pool[i]); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 122 | } |
| 123 | } |
| 124 | |
Florin Coras | 539663c | 2018-09-28 14:59:37 -0700 | [diff] [blame] | 125 | if (expand == 0) |
| 126 | { |
| 127 | conn_pool_expand (wrk, 2 * wrk->conn_pool_size); |
| 128 | expand = 1; |
| 129 | goto again; |
| 130 | } |
| 131 | vtwrn ("Failed to allocate connection even after expand"); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | static inline void |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 136 | conn_pool_free (vcl_test_server_conn_t * conn) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 137 | { |
| 138 | conn->fd = 0; |
| 139 | conn->is_alloc = 0; |
| 140 | } |
| 141 | |
| 142 | static inline void |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 143 | sync_config_and_reply (vcl_test_server_conn_t * conn, vcl_test_cfg_t * rx_cfg) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 144 | { |
| 145 | conn->cfg = *rx_cfg; |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 146 | vcl_test_buf_alloc (&conn->cfg, 1 /* is_rxbuf */ , |
| 147 | &conn->buf, &conn->buf_size); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 148 | conn->cfg.txbuf_size = conn->cfg.rxbuf_size; |
| 149 | |
| 150 | if (conn->cfg.verbose) |
| 151 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 152 | vtinf ("(fd %d): Replying to cfg message!\n", conn->fd); |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 153 | vcl_test_cfg_dump (&conn->cfg, 0 /* is_client */ ); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 154 | } |
| 155 | (void) vcl_test_write (conn->fd, (uint8_t *) & conn->cfg, |
| 156 | sizeof (conn->cfg), NULL, conn->cfg.verbose); |
| 157 | } |
| 158 | |
| 159 | static void |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 160 | vts_server_start_stop (vcl_test_server_worker_t * wrk, |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 161 | vcl_test_server_conn_t * conn, vcl_test_cfg_t * rx_cfg) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 162 | { |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 163 | u8 is_bi = rx_cfg->test == VCL_TEST_TYPE_BI; |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 164 | vcl_test_server_conn_t *tc; |
| 165 | char buf[64]; |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 166 | int i; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 167 | |
| 168 | if (rx_cfg->ctrl_handle == conn->fd) |
| 169 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 170 | for (i = 0; i < wrk->conn_pool_size; i++) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 171 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 172 | tc = &wrk->conn_pool[i]; |
| 173 | if (tc->cfg.ctrl_handle != conn->fd) |
| 174 | continue; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 175 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 176 | vcl_test_stats_accumulate (&conn->stats, &tc->stats); |
| 177 | if (vcl_comp_tspec (&conn->stats.stop, &tc->stats.stop) < 0) |
| 178 | conn->stats.stop = tc->stats.stop; |
| 179 | /* Client delays sending of disconnect */ |
| 180 | conn->stats.stop.tv_sec -= VCL_TEST_DELAY_DISCONNECT; |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 181 | if (conn->cfg.verbose) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 182 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 183 | sprintf (buf, "SERVER (fd %d) RESULTS", tc->fd); |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 184 | vcl_test_stats_dump (buf, &tc->stats, 1 /* show_rx */ , |
| 185 | is_bi /* show tx */ , conn->cfg.verbose); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 189 | vcl_test_stats_dump ("SERVER RESULTS", &conn->stats, 1 /* show_rx */ , |
| 190 | is_bi /* show_tx */ , conn->cfg.verbose); |
| 191 | vcl_test_cfg_dump (&conn->cfg, 0 /* is_client */ ); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 192 | if (conn->cfg.verbose) |
| 193 | { |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 194 | vtinf (" vcl server main\n" |
| 195 | VCL_TEST_SEPARATOR_STRING |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 196 | " buf: %p\n" |
| 197 | " buf size: %u (0x%08x)\n" |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 198 | VCL_TEST_SEPARATOR_STRING, |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 199 | conn->buf, conn->buf_size, conn->buf_size); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | sync_config_and_reply (conn, rx_cfg); |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 203 | memset (&conn->stats, 0, sizeof (conn->stats)); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 204 | } |
| 205 | else |
| 206 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 207 | if (rx_cfg->ctrl_handle == ~0) |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 208 | { |
| 209 | rx_cfg->ctrl_handle = conn->fd; |
| 210 | vtinf ("Set control fd %d for test!", conn->fd); |
| 211 | } |
| 212 | else |
| 213 | { |
| 214 | vtinf ("Starting %s-directional Stream Test (fd %d)!", |
| 215 | is_bi ? "Bi" : "Uni", conn->fd); |
| 216 | } |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 217 | |
| 218 | sync_config_and_reply (conn, rx_cfg); |
| 219 | |
| 220 | /* read the 1st chunk, record start time */ |
| 221 | memset (&conn->stats, 0, sizeof (conn->stats)); |
| 222 | clock_gettime (CLOCK_REALTIME, &conn->stats.start); |
| 223 | } |
| 224 | } |
| 225 | |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 226 | static inline void |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 227 | vts_server_rx (vcl_test_server_conn_t * conn, int rx_bytes) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 228 | { |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 229 | vcl_test_server_main_t *vsm = &vcl_server_main; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 230 | int client_fd = conn->fd; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 231 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 232 | if (conn->cfg.test == VCL_TEST_TYPE_BI) |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 233 | { |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 234 | if (vsm->use_ds) |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 235 | { |
| 236 | (void) vcl_test_write (client_fd, conn->ds[0].data, conn->ds[0].len, |
| 237 | &conn->stats, conn->cfg.verbose); |
| 238 | if (conn->ds[1].len) |
| 239 | (void) vcl_test_write (client_fd, conn->ds[1].data, |
| 240 | conn->ds[1].len, &conn->stats, |
| 241 | conn->cfg.verbose); |
| 242 | } |
| 243 | else |
| 244 | (void) vcl_test_write (client_fd, conn->buf, rx_bytes, &conn->stats, |
| 245 | conn->cfg.verbose); |
| 246 | } |
| 247 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 248 | if (vsm->use_ds) |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 249 | vppcom_session_free_segments (conn->fd, conn->ds); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 250 | |
| 251 | if (conn->stats.rx_bytes >= conn->cfg.total_bytes) |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 252 | clock_gettime (CLOCK_REALTIME, &conn->stats.stop); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 253 | } |
| 254 | |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 255 | static void |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 256 | vts_server_echo (vcl_test_server_conn_t * conn, int rx_bytes) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 257 | { |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 258 | vcl_test_server_main_t *vsm = &vcl_server_main; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 259 | int tx_bytes, nbytes, pos; |
| 260 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 261 | if (vsm->use_ds) |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 262 | vppcom_data_segment_copy (conn->buf, conn->ds, rx_bytes); |
| 263 | |
| 264 | /* If it looks vaguely like a string, make sure it's terminated */ |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 265 | pos = rx_bytes < conn->buf_size ? rx_bytes : conn->buf_size - 1; |
| 266 | ((char *) conn->buf)[pos] = 0; |
| 267 | vtinf ("(fd %d): RX (%d bytes) - '%s'", conn->fd, rx_bytes, conn->buf); |
| 268 | |
| 269 | if (conn->cfg.verbose) |
| 270 | vtinf ("(fd %d): Echoing back", conn->fd); |
| 271 | |
| 272 | nbytes = strlen ((const char *) conn->buf) + 1; |
| 273 | tx_bytes = vcl_test_write (conn->fd, conn->buf, nbytes, &conn->stats, |
| 274 | conn->cfg.verbose); |
| 275 | if (tx_bytes >= 0) |
| 276 | vtinf ("(fd %d): TX (%d bytes) - '%s'", conn->fd, tx_bytes, conn->buf); |
| 277 | } |
| 278 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 279 | static void |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 280 | vts_new_client (vcl_test_server_worker_t * wrk) |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 281 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 282 | vcl_test_server_conn_t *conn; |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 283 | struct epoll_event ev; |
| 284 | int rv, client_fd; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 285 | |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 286 | conn = conn_pool_alloc (wrk); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 287 | if (!conn) |
| 288 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 289 | vtwrn ("No free connections!"); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 290 | return; |
| 291 | } |
| 292 | |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 293 | client_fd = vppcom_session_accept (wrk->listen_fd, &conn->endpt, 0); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 294 | if (client_fd < 0) |
| 295 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 296 | vterr ("vppcom_session_accept()", client_fd); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 297 | return; |
| 298 | } |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 299 | conn->fd = client_fd; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 300 | |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 301 | vtinf ("Got a connection -- fd = %d (0x%08x)!", client_fd, client_fd); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 302 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 303 | ev.events = EPOLLIN; |
| 304 | ev.data.u64 = conn - wrk->conn_pool; |
| 305 | rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, client_fd, &ev); |
| 306 | if (rv < 0) |
| 307 | { |
| 308 | vterr ("vppcom_epoll_ctl()", rv); |
| 309 | return; |
| 310 | } |
| 311 | wrk->nfds++; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 312 | } |
| 313 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 314 | static void |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 315 | print_usage_and_exit (void) |
| 316 | { |
| 317 | fprintf (stderr, |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 318 | "vcl_test_server [OPTIONS] <port>\n" |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 319 | " OPTIONS\n" |
| 320 | " -h Print this message and exit.\n" |
| 321 | " -6 Use IPv6\n" |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 322 | " -w <num> Number of workers\n" |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 323 | " -D Use UDP transport layer\n" |
Dave Wallace | 03dd90a | 2019-03-25 19:34:50 -0400 | [diff] [blame] | 324 | " -L Use TLS transport layer\n"); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 325 | exit (1); |
| 326 | } |
| 327 | |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 328 | static void |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 329 | vcl_test_init_endpoint_addr (vcl_test_server_main_t * vsm) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 330 | { |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 331 | struct sockaddr_storage *servaddr = &vsm->servaddr; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 332 | memset (servaddr, 0, sizeof (*servaddr)); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 333 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 334 | if (vsm->cfg.address_ip6) |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 335 | { |
| 336 | struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr; |
| 337 | server_addr->sin6_family = AF_INET6; |
| 338 | server_addr->sin6_addr = in6addr_any; |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 339 | server_addr->sin6_port = htons (vsm->cfg.port); |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 340 | } |
| 341 | else |
| 342 | { |
| 343 | struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr; |
| 344 | server_addr->sin_family = AF_INET; |
| 345 | server_addr->sin_addr.s_addr = htonl (INADDR_ANY); |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 346 | server_addr->sin_port = htons (vsm->cfg.port); |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 347 | } |
| 348 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 349 | if (vsm->cfg.address_ip6) |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 350 | { |
| 351 | struct sockaddr_in6 *server_addr = (struct sockaddr_in6 *) servaddr; |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 352 | vsm->cfg.endpt.is_ip4 = 0; |
| 353 | vsm->cfg.endpt.ip = (uint8_t *) & server_addr->sin6_addr; |
| 354 | vsm->cfg.endpt.port = (uint16_t) server_addr->sin6_port; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 355 | } |
| 356 | else |
| 357 | { |
| 358 | struct sockaddr_in *server_addr = (struct sockaddr_in *) servaddr; |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 359 | vsm->cfg.endpt.is_ip4 = 1; |
| 360 | vsm->cfg.endpt.ip = (uint8_t *) & server_addr->sin_addr; |
| 361 | vsm->cfg.endpt.port = (uint16_t) server_addr->sin_port; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 362 | } |
| 363 | } |
| 364 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 365 | static void |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 366 | vcl_test_server_process_opts (vcl_test_server_main_t * vsm, int argc, |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 367 | char **argv) |
| 368 | { |
| 369 | int v, c; |
| 370 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 371 | vsm->cfg.proto = VPPCOM_PROTO_TCP; |
Dave Barach | 6a5adc3 | 2018-07-04 10:56:23 -0400 | [diff] [blame] | 372 | |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 373 | opterr = 0; |
Dave Wallace | 03dd90a | 2019-03-25 19:34:50 -0400 | [diff] [blame] | 374 | while ((c = getopt (argc, argv, "6DLsw:")) != -1) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 375 | switch (c) |
| 376 | { |
| 377 | case '6': |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 378 | vsm->cfg.address_ip6 = 1; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 379 | break; |
| 380 | |
| 381 | case 'D': |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 382 | vsm->cfg.proto = VPPCOM_PROTO_UDP; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 383 | break; |
| 384 | |
Dave Wallace | 03dd90a | 2019-03-25 19:34:50 -0400 | [diff] [blame] | 385 | case 'L': |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 386 | vsm->cfg.proto = VPPCOM_PROTO_TLS; |
| 387 | break; |
| 388 | |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 389 | case 'w': |
| 390 | v = atoi (optarg); |
| 391 | if (v > 1) |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 392 | vsm->cfg.workers = v; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 393 | else |
| 394 | vtwrn ("Invalid number of workers %d", v); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 395 | break; |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 396 | case 's': |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 397 | vsm->use_ds = 1; |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 398 | break; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 399 | case '?': |
| 400 | switch (optopt) |
| 401 | { |
| 402 | default: |
| 403 | if (isprint (optopt)) |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 404 | vtwrn ("Unknown option `-%c'.", optopt); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 405 | else |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 406 | vtwrn ("Unknown option character `\\x%x'.", optopt); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 407 | } |
| 408 | /* fall thru */ |
| 409 | case 'h': |
| 410 | default: |
| 411 | print_usage_and_exit (); |
| 412 | } |
| 413 | |
| 414 | if (argc < (optind + 1)) |
| 415 | { |
| 416 | fprintf (stderr, "SERVER: ERROR: Insufficient number of arguments!\n"); |
| 417 | print_usage_and_exit (); |
| 418 | } |
| 419 | |
| 420 | if (sscanf (argv[optind], "%d", &v) == 1) |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 421 | vsm->cfg.port = (uint16_t) v; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 422 | else |
| 423 | { |
| 424 | fprintf (stderr, "SERVER: ERROR: Invalid port (%s)!\n", argv[optind]); |
| 425 | print_usage_and_exit (); |
| 426 | } |
| 427 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 428 | vcl_test_init_endpoint_addr (vsm); |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 429 | } |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 430 | |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 431 | int |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 432 | vts_handle_cfg (vcl_test_server_worker_t * wrk, vcl_test_cfg_t * rx_cfg, |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 433 | vcl_test_server_conn_t * conn, int rx_bytes) |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 434 | { |
| 435 | if (rx_cfg->verbose) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 436 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 437 | vtinf ("(fd %d): Received a cfg msg!", conn->fd); |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 438 | vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ ); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 439 | } |
| 440 | |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 441 | if (rx_bytes != sizeof (*rx_cfg)) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 442 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 443 | vtinf ("(fd %d): Invalid cfg msg size %d expected %lu!", conn->fd, |
| 444 | rx_bytes, sizeof (*rx_cfg)); |
| 445 | conn->cfg.rxbuf_size = 0; |
| 446 | conn->cfg.num_writes = 0; |
| 447 | if (conn->cfg.verbose) |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 448 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 449 | vtinf ("(fd %d): Replying to cfg msg", conn->fd); |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 450 | vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ ); |
Florin Coras | 460dce6 | 2018-07-27 05:45:06 -0700 | [diff] [blame] | 451 | } |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 452 | vcl_test_write (conn->fd, (uint8_t *) & conn->cfg, |
| 453 | sizeof (conn->cfg), NULL, conn->cfg.verbose); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 454 | return -1; |
| 455 | } |
| 456 | |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 457 | switch (rx_cfg->test) |
| 458 | { |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 459 | case VCL_TEST_TYPE_NONE: |
| 460 | case VCL_TEST_TYPE_ECHO: |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 461 | sync_config_and_reply (conn, rx_cfg); |
| 462 | break; |
| 463 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 464 | case VCL_TEST_TYPE_BI: |
| 465 | case VCL_TEST_TYPE_UNI: |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 466 | vts_server_start_stop (wrk, conn, rx_cfg); |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 467 | break; |
| 468 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 469 | case VCL_TEST_TYPE_EXIT: |
| 470 | vtinf ("Session fd %d closing!", conn->fd); |
| 471 | clock_gettime (CLOCK_REALTIME, &conn->stats.stop); |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 472 | vppcom_session_close (conn->fd); |
| 473 | conn_pool_free (conn); |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 474 | wrk->nfds--; |
| 475 | break; |
| 476 | |
| 477 | default: |
| 478 | vtwrn ("Unknown test type %d", rx_cfg->test); |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 479 | vcl_test_cfg_dump (rx_cfg, 0 /* is_client */ ); |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 480 | break; |
| 481 | } |
| 482 | |
| 483 | return 0; |
| 484 | } |
| 485 | |
| 486 | static void |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 487 | vts_worker_init (vcl_test_server_worker_t * wrk) |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 488 | { |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 489 | vcl_test_server_main_t *vsm = &vcl_server_main; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 490 | struct epoll_event listen_ev; |
| 491 | int rv; |
| 492 | |
| 493 | __wrk_index = wrk->wrk_index; |
| 494 | |
| 495 | vtinf ("Initializing worker ..."); |
| 496 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 497 | conn_pool_expand (wrk, VCL_TEST_CFG_MAX_TEST_SESS + 1); |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 498 | if (wrk->wrk_index) |
Florin Coras | de9f08b | 2018-09-07 14:32:58 -0700 | [diff] [blame] | 499 | if (vppcom_worker_register ()) |
| 500 | vtfail ("vppcom_worker_register()", 1); |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 501 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 502 | wrk->listen_fd = vppcom_session_create (vsm->cfg.proto, |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 503 | 0 /* is_nonblocking */ ); |
| 504 | if (wrk->listen_fd < 0) |
| 505 | vtfail ("vppcom_session_create()", wrk->listen_fd); |
| 506 | |
Ping Yu | 34a3a08 | 2018-11-30 19:16:17 -0500 | [diff] [blame] | 507 | |
| 508 | if (vsm->cfg.proto == VPPCOM_PROTO_TLS) |
| 509 | { |
| 510 | vppcom_session_tls_add_cert (wrk->listen_fd, vcl_test_crt_rsa, |
| 511 | vcl_test_crt_rsa_len); |
| 512 | vppcom_session_tls_add_key (wrk->listen_fd, vcl_test_key_rsa, |
| 513 | vcl_test_key_rsa_len); |
| 514 | } |
| 515 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 516 | rv = vppcom_session_bind (wrk->listen_fd, &vsm->cfg.endpt); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 517 | if (rv < 0) |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 518 | vtfail ("vppcom_session_bind()", rv); |
| 519 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 520 | if (!(vsm->cfg.proto == VPPCOM_PROTO_UDP)) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 521 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 522 | rv = vppcom_session_listen (wrk->listen_fd, 10); |
| 523 | if (rv < 0) |
| 524 | vtfail ("vppcom_session_listen()", rv); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 525 | } |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 526 | |
| 527 | wrk->epfd = vppcom_epoll_create (); |
| 528 | if (wrk->epfd < 0) |
| 529 | vtfail ("vppcom_epoll_create()", wrk->epfd); |
| 530 | |
| 531 | listen_ev.events = EPOLLIN; |
| 532 | listen_ev.data.u32 = ~0; |
| 533 | rv = vppcom_epoll_ctl (wrk->epfd, EPOLL_CTL_ADD, wrk->listen_fd, |
| 534 | &listen_ev); |
| 535 | if (rv < 0) |
| 536 | vtfail ("vppcom_epoll_ctl", rv); |
| 537 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 538 | vsm->active_workers += 1; |
| 539 | vtinf ("Waiting for a client to connect on port %d ...", vsm->cfg.port); |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 540 | } |
| 541 | |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 542 | static int |
| 543 | vts_conn_expect_config (vcl_test_server_conn_t * conn) |
| 544 | { |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 545 | if (conn->cfg.test == VCL_TEST_TYPE_ECHO) |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 546 | return 1; |
| 547 | |
| 548 | return (conn->stats.rx_bytes < 128 |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 549 | || conn->stats.rx_bytes > conn->cfg.total_bytes); |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 550 | } |
| 551 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 552 | static vcl_test_cfg_t * |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 553 | vts_conn_read_config (vcl_test_server_conn_t * conn) |
| 554 | { |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 555 | vcl_test_server_main_t *vsm = &vcl_server_main; |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 556 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 557 | if (vsm->use_ds) |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 558 | { |
| 559 | /* We could avoid the copy if the first segment is big enough but this |
| 560 | * just simplifies things */ |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 561 | vppcom_data_segment_copy (conn->buf, conn->ds, sizeof (vcl_test_cfg_t)); |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 562 | } |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 563 | return (vcl_test_cfg_t *) conn->buf; |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | static inline int |
| 567 | vts_conn_read (vcl_test_server_conn_t * conn) |
| 568 | { |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 569 | vcl_test_server_main_t *vsm = &vcl_server_main; |
| 570 | if (vsm->use_ds) |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 571 | return vcl_test_read_ds (conn->fd, conn->ds, &conn->stats); |
| 572 | else |
| 573 | return vcl_test_read (conn->fd, conn->buf, conn->buf_size, &conn->stats); |
| 574 | } |
| 575 | |
| 576 | static inline int |
| 577 | vts_conn_has_ascii (vcl_test_server_conn_t * conn) |
| 578 | { |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 579 | vcl_test_server_main_t *vsm = &vcl_server_main; |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 580 | |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 581 | if (vsm->use_ds) |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 582 | return isascii (conn->ds[0].data[0]); |
| 583 | else |
| 584 | return isascii (conn->buf[0]); |
| 585 | } |
| 586 | |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 587 | static void * |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 588 | vts_worker_loop (void *arg) |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 589 | { |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 590 | vcl_test_server_main_t *vsm = &vcl_server_main; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 591 | vcl_test_server_worker_t *wrk = arg; |
| 592 | vcl_test_server_conn_t *conn; |
| 593 | int i, rx_bytes, num_ev; |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 594 | vcl_test_cfg_t *rx_cfg; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 595 | |
| 596 | if (wrk->wrk_index) |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 597 | vts_worker_init (wrk); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 598 | |
| 599 | while (1) |
| 600 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 601 | num_ev = vppcom_epoll_wait (wrk->epfd, wrk->wait_events, |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 602 | VCL_TEST_CFG_MAX_EPOLL_EVENTS, 60000.0); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 603 | if (num_ev < 0) |
| 604 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 605 | vterr ("vppcom_epoll_wait()", num_ev); |
| 606 | goto fail; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 607 | } |
| 608 | else if (num_ev == 0) |
| 609 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 610 | vtinf ("vppcom_epoll_wait() timeout!"); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 611 | continue; |
| 612 | } |
| 613 | for (i = 0; i < num_ev; i++) |
| 614 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 615 | conn = &wrk->conn_pool[wrk->wait_events[i].data.u32]; |
| 616 | if (wrk->wait_events[i].events & (EPOLLHUP | EPOLLRDHUP)) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 617 | { |
| 618 | vppcom_session_close (conn->fd); |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame] | 619 | wrk->nfds -= 1; |
| 620 | if (!wrk->nfds) |
| 621 | { |
| 622 | vtinf ("All client connections closed\n"); |
| 623 | goto done; |
| 624 | } |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 625 | continue; |
| 626 | } |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 627 | if (wrk->wait_events[i].data.u32 == ~0) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 628 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 629 | vts_new_client (wrk); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 630 | continue; |
| 631 | } |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 632 | |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 633 | if (EPOLLIN & wrk->wait_events[i].events) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 634 | { |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame] | 635 | read_again: |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 636 | rx_bytes = vts_conn_read (conn); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 637 | |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 638 | if (rx_bytes <= 0) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 639 | { |
| 640 | if (errno == ECONNRESET) |
| 641 | { |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 642 | vtinf ("Connection reset by remote peer.\n"); |
| 643 | goto fail; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 644 | } |
| 645 | else |
| 646 | continue; |
| 647 | } |
| 648 | |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 649 | if (vts_conn_expect_config (conn)) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 650 | { |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 651 | rx_cfg = vts_conn_read_config (conn); |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 652 | if (rx_cfg->magic == VCL_TEST_CFG_CTRL_MAGIC) |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 653 | { |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 654 | if (vsm->use_ds) |
| 655 | vppcom_session_free_segments (conn->fd, conn->ds); |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 656 | vts_handle_cfg (wrk, rx_cfg, conn, rx_bytes); |
| 657 | if (!wrk->nfds) |
| 658 | { |
| 659 | vtinf ("All client connections closed\n"); |
| 660 | goto done; |
| 661 | } |
| 662 | continue; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 663 | } |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 664 | } |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 665 | if ((conn->cfg.test == VCL_TEST_TYPE_UNI) |
| 666 | || (conn->cfg.test == VCL_TEST_TYPE_BI)) |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 667 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 668 | vts_server_rx (conn, rx_bytes); |
Florin Coras | 2179513 | 2018-09-09 09:40:51 -0700 | [diff] [blame] | 669 | if (vppcom_session_attr (conn->fd, VPPCOM_ATTR_GET_NREAD, 0, |
| 670 | 0) > 0) |
| 671 | goto read_again; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 672 | continue; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 673 | } |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 674 | if (vts_conn_has_ascii (conn)) |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 675 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 676 | vts_server_echo (conn, rx_bytes); |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 677 | } |
| 678 | else |
| 679 | { |
| 680 | vtwrn ("FIFO not drained! extra bytes %d", rx_bytes); |
| 681 | } |
| 682 | } |
| 683 | else |
| 684 | { |
| 685 | vtwrn ("Unhandled event"); |
| 686 | goto fail; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 687 | } |
| 688 | } |
| 689 | } |
| 690 | |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 691 | fail: |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 692 | vsm->worker_fails -= 1; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 693 | |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 694 | done: |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 695 | vppcom_session_close (wrk->listen_fd); |
| 696 | if (wrk->conn_pool) |
| 697 | free (wrk->conn_pool); |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 698 | vsm->active_workers -= 1; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 699 | return 0; |
| 700 | } |
| 701 | |
| 702 | int |
| 703 | main (int argc, char **argv) |
| 704 | { |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 705 | vcl_test_server_main_t *vsm = &vcl_server_main; |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 706 | int rv, i; |
| 707 | |
| 708 | clib_mem_init_thread_safe (0, 64 << 20); |
Florin Coras | 1502fc3 | 2018-10-05 00:50:30 -0700 | [diff] [blame] | 709 | vsm->cfg.port = VCL_TEST_SERVER_PORT; |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 710 | vsm->cfg.workers = 1; |
Florin Coras | 2cba853 | 2018-09-11 16:33:36 -0700 | [diff] [blame] | 711 | vsm->active_workers = 0; |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 712 | vcl_test_server_process_opts (vsm, argc, argv); |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 713 | |
| 714 | rv = vppcom_app_create ("vcl_test_server"); |
| 715 | if (rv) |
| 716 | vtfail ("vppcom_app_create()", rv); |
| 717 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 718 | vsm->workers = calloc (vsm->cfg.workers, sizeof (*vsm->workers)); |
| 719 | vts_worker_init (&vsm->workers[0]); |
| 720 | for (i = 1; i < vsm->cfg.workers; i++) |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 721 | { |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 722 | vsm->workers[i].wrk_index = i; |
| 723 | rv = pthread_create (&vsm->workers[i].thread_handle, NULL, |
| 724 | vts_worker_loop, (void *) &vsm->workers[i]); |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 725 | } |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 726 | vts_worker_loop (&vsm->workers[0]); |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 727 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 728 | while (vsm->active_workers > 0) |
Florin Coras | 293aa05 | 2018-08-30 18:49:13 -0700 | [diff] [blame] | 729 | ; |
| 730 | |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 731 | vppcom_app_destroy (); |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 732 | free (vsm->workers); |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 733 | |
Florin Coras | 6d4bb42 | 2018-09-04 22:07:27 -0700 | [diff] [blame] | 734 | return vsm->worker_fails; |
Dave Wallace | e4d5a65 | 2018-06-24 21:21:21 -0400 | [diff] [blame] | 735 | } |
Dave Wallace | 543852a | 2017-08-03 02:11:34 -0400 | [diff] [blame] | 736 | |
| 737 | /* |
| 738 | * fd.io coding-style-patch-verification: ON |
| 739 | * |
| 740 | * Local Variables: |
| 741 | * eval: (c-set-style "gnu") |
| 742 | * End: |
| 743 | */ |