Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2016 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 <signal.h> |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 18 | |
| 19 | #include <vnet/session/application_interface.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 20 | #include <svm/svm_fifo_segment.h> |
| 21 | #include <vlibmemory/api.h> |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 22 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 23 | #include <vpp/api/vpe_msg_enum.h> |
| 24 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 25 | #define vl_typedefs /* define message structures */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 26 | #include <vpp/api/vpe_all_api_h.h> |
| 27 | #undef vl_typedefs |
| 28 | |
| 29 | /* declare message handlers for each api */ |
| 30 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 31 | #define vl_endianfun /* define message structures */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 32 | #include <vpp/api/vpe_all_api_h.h> |
| 33 | #undef vl_endianfun |
| 34 | |
| 35 | /* instantiate all the print functions we know about */ |
| 36 | #define vl_print(handle, ...) |
| 37 | #define vl_printfun |
| 38 | #include <vpp/api/vpe_all_api_h.h> |
| 39 | #undef vl_printfun |
| 40 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 41 | typedef struct |
| 42 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 43 | svm_fifo_t *server_rx_fifo; |
| 44 | svm_fifo_t *server_tx_fifo; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 45 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 46 | u64 vpp_session_handle; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 47 | u64 bytes_received; |
| 48 | f64 start; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 49 | } session_t; |
| 50 | |
| 51 | typedef enum |
| 52 | { |
| 53 | STATE_START, |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 54 | STATE_ATTACHED, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 55 | STATE_READY, |
| 56 | STATE_DISCONNECTING, |
| 57 | STATE_FAILED |
| 58 | } connection_state_t; |
| 59 | |
| 60 | typedef struct |
| 61 | { |
| 62 | /* vpe input queue */ |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 63 | svm_queue_t *vl_input_queue; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 64 | |
| 65 | /* API client handle */ |
| 66 | u32 my_client_index; |
| 67 | |
| 68 | /* The URI we're playing with */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 69 | u8 *uri; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 70 | |
| 71 | /* Session pool */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 72 | session_t *sessions; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 73 | |
| 74 | /* Hash table for disconnect processing */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 75 | uword *session_index_by_vpp_handles; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 76 | |
| 77 | /* intermediate rx buffer */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 78 | u8 *rx_buf; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 79 | |
| 80 | /* URI for slave's connect */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 81 | u8 *connect_uri; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 82 | |
| 83 | u32 connected_session_index; |
| 84 | |
| 85 | int i_am_master; |
| 86 | |
| 87 | /* drop all packets */ |
| 88 | int drop_packets; |
| 89 | |
| 90 | /* Our event queue */ |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 91 | svm_queue_t *our_event_queue; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 92 | |
| 93 | /* $$$ single thread only for the moment */ |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 94 | svm_queue_t *vpp_event_queue; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 95 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 96 | u8 *socket_name; |
| 97 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 98 | pid_t my_pid; |
| 99 | |
| 100 | /* For deadman timers */ |
| 101 | clib_time_t clib_time; |
| 102 | |
| 103 | /* State of the connection, shared between msg RX thread and main thread */ |
| 104 | volatile connection_state_t state; |
| 105 | |
| 106 | /* Signal variables */ |
| 107 | volatile int time_to_stop; |
| 108 | volatile int time_to_print_stats; |
| 109 | |
| 110 | u32 configured_segment_size; |
| 111 | |
| 112 | /* VNET_API_ERROR_FOO -> "Foo" hash table */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 113 | uword *error_string_by_error_number; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 114 | |
| 115 | u8 *connect_test_data; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 116 | pthread_t client_rx_thread_handle; |
| 117 | u32 client_bytes_received; |
| 118 | u8 test_return_packets; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 119 | u64 bytes_to_send; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 120 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 121 | /** Flag that decides if socket, instead of svm, api is used to connect to |
| 122 | * vpp. If sock api is used, shm binary api is subsequently bootstrapped |
| 123 | * and all other messages are exchanged using shm IPC. */ |
| 124 | u8 use_sock_api; |
| 125 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 126 | /* convenience */ |
| 127 | svm_fifo_segment_main_t *segment_main; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 128 | } echo_main_t; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 129 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 130 | echo_main_t echo_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 131 | |
| 132 | #if CLIB_DEBUG > 0 |
| 133 | #define NITER 10000 |
| 134 | #else |
| 135 | #define NITER 4000000 |
| 136 | #endif |
| 137 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 138 | static u8 * |
| 139 | format_api_error (u8 * s, va_list * args) |
| 140 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 141 | echo_main_t *em = &echo_main; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 142 | i32 error = va_arg (*args, u32); |
| 143 | uword *p; |
| 144 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 145 | p = hash_get (em->error_string_by_error_number, -error); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 146 | |
| 147 | if (p) |
| 148 | s = format (s, "%s", p[0]); |
| 149 | else |
| 150 | s = format (s, "%d", error); |
| 151 | return s; |
| 152 | } |
| 153 | |
| 154 | static void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 155 | init_error_string_table (echo_main_t * em) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 156 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 157 | em->error_string_by_error_number = hash_create (0, sizeof (uword)); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 158 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 159 | #define _(n,v,s) hash_set (em->error_string_by_error_number, -v, s); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 160 | foreach_vnet_api_error; |
| 161 | #undef _ |
| 162 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 163 | hash_set (em->error_string_by_error_number, 99, "Misc"); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 164 | } |
| 165 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 166 | int |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 167 | wait_for_state_change (echo_main_t * em, connection_state_t state) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 168 | { |
| 169 | #if CLIB_DEBUG > 0 |
| 170 | #define TIMEOUT 600.0 |
| 171 | #else |
| 172 | #define TIMEOUT 600.0 |
| 173 | #endif |
| 174 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 175 | f64 timeout = clib_time_now (&em->clib_time) + TIMEOUT; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 176 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 177 | while (clib_time_now (&em->clib_time) < timeout) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 178 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 179 | if (em->state == state) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 180 | return 0; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 181 | if (em->state == STATE_FAILED) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 182 | return -1; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 183 | if (em->time_to_stop == 1) |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 184 | return 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 185 | } |
| 186 | clib_warning ("timeout waiting for STATE_READY"); |
| 187 | return -1; |
| 188 | } |
| 189 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 190 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 191 | application_send_attach (echo_main_t * em) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 192 | { |
| 193 | vl_api_application_attach_t *bmp; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 194 | u32 fifo_size = 4 << 20; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 195 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 196 | memset (bmp, 0, sizeof (*bmp)); |
| 197 | |
| 198 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 199 | bmp->client_index = em->my_client_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 200 | bmp->context = ntohl (0xfeedface); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 201 | bmp->options[APP_OPTIONS_FLAGS] = |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 202 | APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT; |
Dave Barach | 10d8cc6 | 2017-05-30 09:30:07 -0400 | [diff] [blame] | 203 | bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = 16; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 204 | bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = fifo_size; |
| 205 | bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = fifo_size; |
| 206 | bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = 128 << 20; |
| 207 | bmp->options[APP_OPTIONS_SEGMENT_SIZE] = 256 << 20; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 208 | vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 211 | int |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 212 | application_attach (echo_main_t * em) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 213 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 214 | application_send_attach (em); |
| 215 | if (wait_for_state_change (em, STATE_ATTACHED)) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 216 | { |
| 217 | clib_warning ("timeout waiting for STATE_ATTACHED"); |
| 218 | return -1; |
| 219 | } |
| 220 | return 0; |
| 221 | } |
| 222 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 223 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 224 | application_detach (echo_main_t * em) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 225 | { |
| 226 | vl_api_application_detach_t *bmp; |
| 227 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 228 | memset (bmp, 0, sizeof (*bmp)); |
| 229 | |
| 230 | bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 231 | bmp->client_index = em->my_client_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 232 | bmp->context = ntohl (0xfeedface); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 233 | vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp); |
| 234 | |
| 235 | clib_warning ("Sent detach"); |
| 236 | } |
| 237 | |
| 238 | static int |
| 239 | memfd_segment_attach (void) |
| 240 | { |
| 241 | ssvm_private_t _ssvm = { 0 }, *ssvm = &_ssvm; |
| 242 | clib_error_t *error; |
| 243 | int rv; |
| 244 | |
| 245 | if ((error = vl_socket_client_recv_fd_msg (&ssvm->fd, 5))) |
| 246 | { |
| 247 | clib_error_report (error); |
| 248 | return -1; |
| 249 | } |
| 250 | |
| 251 | if ((rv = ssvm_slave_init_memfd (ssvm))) |
| 252 | return rv; |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static int |
| 258 | fifo_segment_attach (char *name, u32 size, ssvm_segment_type_t type) |
| 259 | { |
| 260 | svm_fifo_segment_create_args_t _a, *a = &_a; |
| 261 | clib_error_t *error; |
| 262 | int rv; |
| 263 | |
| 264 | memset (a, 0, sizeof (*a)); |
| 265 | a->segment_name = (char *) name; |
| 266 | a->segment_size = size; |
| 267 | a->segment_type = type; |
| 268 | |
| 269 | if (type == SSVM_SEGMENT_MEMFD) |
| 270 | { |
| 271 | if ((error = vl_socket_client_recv_fd_msg (&a->memfd_fd, 5))) |
| 272 | { |
| 273 | clib_error_report (error); |
| 274 | return -1; |
| 275 | } |
| 276 | } |
| 277 | |
| 278 | if ((rv = svm_fifo_segment_attach (a))) |
| 279 | { |
| 280 | clib_warning ("svm_fifo_segment_attach ('%s') failed", name); |
| 281 | return rv; |
| 282 | } |
| 283 | |
| 284 | return 0; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 285 | } |
| 286 | |
| 287 | static void |
| 288 | vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t * |
| 289 | mp) |
| 290 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 291 | echo_main_t *em = &echo_main; |
| 292 | ssvm_segment_type_t seg_type; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 293 | |
| 294 | if (mp->retval) |
| 295 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 296 | clib_warning ("attach failed: %U", format_api_error, |
| 297 | clib_net_to_host_u32 (mp->retval)); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 298 | em->state = STATE_FAILED; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 299 | return; |
| 300 | } |
| 301 | |
| 302 | if (mp->segment_name_length == 0) |
| 303 | { |
| 304 | clib_warning ("segment_name_length zero"); |
| 305 | return; |
| 306 | } |
| 307 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 308 | seg_type = em->use_sock_api ? SSVM_SEGMENT_MEMFD : SSVM_SEGMENT_SHM; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 309 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 310 | /* Attach to fifo segment */ |
| 311 | if (fifo_segment_attach ((char *) mp->segment_name, mp->segment_size, |
| 312 | seg_type)) |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 313 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 314 | em->state = STATE_FAILED; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 315 | return; |
| 316 | } |
| 317 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 318 | /* If we're using memfd segments, read and attach to event qs segment */ |
| 319 | if (seg_type == SSVM_SEGMENT_MEMFD) |
| 320 | { |
| 321 | if (memfd_segment_attach ()) |
| 322 | { |
| 323 | clib_warning ("failed to attach to evt q segment"); |
| 324 | em->state = STATE_FAILED; |
| 325 | return; |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | ASSERT (mp->app_event_queue_address); |
| 330 | em->our_event_queue = uword_to_pointer (mp->app_event_queue_address, |
| 331 | svm_queue_t *); |
| 332 | em->state = STATE_ATTACHED; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | static void |
| 336 | vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t * |
| 337 | mp) |
| 338 | { |
| 339 | if (mp->retval) |
| 340 | clib_warning ("detach returned with err: %d", mp->retval); |
| 341 | } |
| 342 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 343 | static void |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 344 | stop_signal (int signum) |
| 345 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 346 | echo_main_t *um = &echo_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 347 | |
| 348 | um->time_to_stop = 1; |
| 349 | } |
| 350 | |
| 351 | static void |
| 352 | stats_signal (int signum) |
| 353 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 354 | echo_main_t *um = &echo_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 355 | |
| 356 | um->time_to_print_stats = 1; |
| 357 | } |
| 358 | |
| 359 | static clib_error_t * |
| 360 | setup_signal_handlers (void) |
| 361 | { |
| 362 | signal (SIGINT, stats_signal); |
| 363 | signal (SIGQUIT, stop_signal); |
| 364 | signal (SIGTERM, stop_signal); |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | |
| 369 | void |
| 370 | vlib_cli_output (struct vlib_main_t *vm, char *fmt, ...) |
| 371 | { |
| 372 | clib_warning ("BUG"); |
| 373 | } |
| 374 | |
| 375 | int |
| 376 | connect_to_vpp (char *name) |
| 377 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 378 | echo_main_t *em = &echo_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 379 | api_main_t *am = &api_main; |
| 380 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 381 | if (em->use_sock_api) |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 382 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 383 | if (vl_socket_client_connect ((char *) em->socket_name, name, |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 384 | 0 /* default rx, tx buffer */ )) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 385 | { |
| 386 | clib_warning ("socket connect failed"); |
| 387 | return -1; |
| 388 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 389 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 390 | if (vl_socket_client_init_shm (0)) |
| 391 | { |
| 392 | clib_warning ("init shm api failed"); |
| 393 | return -1; |
| 394 | } |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 395 | } |
| 396 | else |
| 397 | { |
| 398 | if (vl_client_connect_to_vlib ("/vpe-api", name, 32) < 0) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 399 | { |
| 400 | clib_warning ("shmem connect failed"); |
| 401 | return -1; |
| 402 | } |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 403 | } |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 404 | em->vl_input_queue = am->shmem_hdr->vl_input_queue; |
| 405 | em->my_client_index = am->my_client_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 406 | return 0; |
| 407 | } |
| 408 | |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 409 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 410 | disconnect_from_vpp (echo_main_t * em) |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 411 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 412 | if (em->use_sock_api) |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 413 | vl_socket_client_disconnect (); |
| 414 | else |
| 415 | vl_client_disconnect_from_vlib (); |
| 416 | } |
| 417 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 418 | static void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 419 | vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 420 | { |
| 421 | svm_fifo_segment_create_args_t _a, *a = &_a; |
| 422 | int rv; |
| 423 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 424 | memset (a, 0, sizeof (*a)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 425 | a->segment_name = (char *) mp->segment_name; |
| 426 | a->segment_size = mp->segment_size; |
| 427 | /* Attach to the segment vpp created */ |
| 428 | rv = svm_fifo_segment_attach (a); |
| 429 | if (rv) |
| 430 | { |
| 431 | clib_warning ("svm_fifo_segment_attach ('%s') failed", |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 432 | mp->segment_name); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 433 | return; |
| 434 | } |
| 435 | clib_warning ("Mapped new segment '%s' size %d", mp->segment_name, |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 436 | mp->segment_size); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | static void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 440 | session_print_stats (echo_main_t * em, session_t * session) |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 441 | { |
| 442 | f64 deltat; |
| 443 | u64 bytes; |
| 444 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 445 | deltat = clib_time_now (&em->clib_time) - session->start; |
| 446 | bytes = em->i_am_master ? session->bytes_received : em->bytes_to_send; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 447 | fformat (stdout, "Finished in %.6f\n", deltat); |
| 448 | fformat (stdout, "%.4f Gbit/second\n", (bytes * 8.0) / deltat / 1e9); |
| 449 | } |
| 450 | |
| 451 | static void |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 452 | vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp) |
| 453 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 454 | echo_main_t *em = &echo_main; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 455 | session_t *session = 0; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 456 | vl_api_disconnect_session_reply_t *rmp; |
| 457 | uword *p; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 458 | int rv = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 459 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 460 | p = hash_get (em->session_index_by_vpp_handles, mp->handle); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 461 | |
| 462 | if (p) |
| 463 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 464 | session = pool_elt_at_index (em->sessions, p[0]); |
| 465 | hash_unset (em->session_index_by_vpp_handles, mp->handle); |
| 466 | pool_put (em->sessions, session); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 467 | } |
| 468 | else |
| 469 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 470 | clib_warning ("couldn't find session key %llx", mp->handle); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 471 | rv = -11; |
| 472 | } |
| 473 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 474 | // em->time_to_stop = 1; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 475 | |
| 476 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 477 | memset (rmp, 0, sizeof (*rmp)); |
| 478 | |
| 479 | rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY); |
| 480 | rmp->retval = rv; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 481 | rmp->handle = mp->handle; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 482 | vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & rmp); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 483 | |
| 484 | if (session) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 485 | session_print_stats (em, session); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | static void |
| 489 | vl_api_reset_session_t_handler (vl_api_reset_session_t * mp) |
| 490 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 491 | echo_main_t *em = &echo_main; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 492 | vl_api_reset_session_reply_t *rmp; |
| 493 | uword *p; |
| 494 | int rv = 0; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 495 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 496 | p = hash_get (em->session_index_by_vpp_handles, mp->handle); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 497 | |
| 498 | if (p) |
| 499 | { |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 500 | clib_warning ("got reset"); |
| 501 | /* Cleanup later */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 502 | em->time_to_stop = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 503 | } |
| 504 | else |
| 505 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 506 | clib_warning ("couldn't find session key %llx", mp->handle); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 507 | rv = -11; |
| 508 | } |
| 509 | |
| 510 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 511 | memset (rmp, 0, sizeof (*rmp)); |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 512 | rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 513 | rmp->retval = rv; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 514 | rmp->handle = mp->handle; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 515 | vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & rmp); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 519 | client_handle_fifo_event_rx (echo_main_t * em, session_fifo_event_t * e) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 520 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 521 | svm_fifo_t *rx_fifo; |
| 522 | int n_read, bytes, i; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 523 | |
| 524 | rx_fifo = e->fifo; |
| 525 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 526 | bytes = svm_fifo_max_dequeue (rx_fifo); |
| 527 | /* Allow enqueuing of new event */ |
| 528 | svm_fifo_unset_event (rx_fifo); |
| 529 | |
| 530 | /* Read the bytes */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 531 | do |
| 532 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 533 | n_read = svm_fifo_dequeue_nowait (rx_fifo, |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 534 | clib_min (vec_len (em->rx_buf), |
| 535 | bytes), em->rx_buf); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 536 | if (n_read > 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 537 | { |
| 538 | bytes -= n_read; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 539 | if (em->test_return_packets) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 540 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 541 | for (i = 0; i < n_read; i++) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 542 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 543 | if (em->rx_buf[i] |
| 544 | != ((em->client_bytes_received + i) & 0xff)) |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 545 | { |
| 546 | clib_warning ("error at byte %lld, 0x%x not 0x%x", |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 547 | em->client_bytes_received + i, |
| 548 | em->rx_buf[i], |
| 549 | ((em->client_bytes_received + i) & 0xff)); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 550 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 551 | } |
| 552 | } |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 553 | em->client_bytes_received += n_read; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 554 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 555 | else |
| 556 | { |
| 557 | if (n_read == -2) |
| 558 | { |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 559 | // clib_warning ("weird!"); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 560 | break; |
| 561 | } |
| 562 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 563 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 564 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 565 | while (bytes > 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 566 | } |
| 567 | |
| 568 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 569 | client_handle_event_queue (echo_main_t * em) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 570 | { |
| 571 | session_fifo_event_t _e, *e = &_e;; |
| 572 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 573 | svm_queue_sub (em->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 574 | switch (e->event_type) |
| 575 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 576 | case FIFO_EVENT_APP_RX: |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 577 | client_handle_fifo_event_rx (em, e); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 578 | break; |
| 579 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 580 | case FIFO_EVENT_DISCONNECT: |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 581 | return; |
| 582 | |
| 583 | default: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 584 | clib_warning ("unknown event type %d", e->event_type); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 585 | break; |
| 586 | } |
| 587 | } |
| 588 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 589 | static void * |
| 590 | client_rx_thread_fn (void *arg) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 591 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 592 | session_fifo_event_t _e, *e = &_e; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 593 | echo_main_t *em = &echo_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 594 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 595 | em->client_bytes_received = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 596 | while (1) |
| 597 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 598 | svm_queue_sub (em->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 599 | switch (e->event_type) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 600 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 601 | case FIFO_EVENT_APP_RX: |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 602 | client_handle_fifo_event_rx (em, e); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 603 | break; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 604 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 605 | case FIFO_EVENT_DISCONNECT: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 606 | return 0; |
| 607 | default: |
| 608 | clib_warning ("unknown event type %d", e->event_type); |
| 609 | break; |
| 610 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 611 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 612 | if (PREDICT_FALSE (em->time_to_stop == 1)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 613 | break; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 614 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 615 | pthread_exit (0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 616 | } |
| 617 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 618 | |
| 619 | static void |
Dave Wallace | 33e002b | 2017-09-06 01:20:02 -0400 | [diff] [blame] | 620 | vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 621 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 622 | echo_main_t *em = &echo_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 623 | session_t *session; |
| 624 | u32 session_index; |
| 625 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 626 | int rv; |
| 627 | |
| 628 | if (mp->retval) |
| 629 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 630 | clib_warning ("connection failed with code: %U", format_api_error, |
| 631 | clib_net_to_host_u32 (mp->retval)); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 632 | em->state = STATE_FAILED; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 633 | return; |
| 634 | } |
Florin Coras | ade70e4 | 2017-10-14 18:56:41 -0700 | [diff] [blame] | 635 | else |
| 636 | { |
| 637 | clib_warning ("connected with local ip %U port %d", format_ip46_address, |
| 638 | mp->lcl_ip, mp->is_ip4, |
| 639 | clib_net_to_host_u16 (mp->lcl_port)); |
| 640 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 641 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 642 | em->vpp_event_queue = |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 643 | uword_to_pointer (mp->vpp_event_queue_address, svm_queue_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 644 | |
| 645 | /* |
| 646 | * Setup session |
| 647 | */ |
| 648 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 649 | pool_get (em->sessions, session); |
| 650 | session_index = session - em->sessions; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 651 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 652 | rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 653 | rx_fifo->client_session_index = session_index; |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 654 | tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 655 | tx_fifo->client_session_index = session_index; |
| 656 | |
| 657 | session->server_rx_fifo = rx_fifo; |
| 658 | session->server_tx_fifo = tx_fifo; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 659 | session->vpp_session_handle = mp->handle; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 660 | session->start = clib_time_now (&em->clib_time); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 661 | |
| 662 | /* Save handle */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 663 | em->connected_session_index = session_index; |
| 664 | em->state = STATE_READY; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 665 | |
| 666 | /* Add it to lookup table */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 667 | hash_set (em->session_index_by_vpp_handles, mp->handle, session_index); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 668 | |
| 669 | /* Start RX thread */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 670 | rv = pthread_create (&em->client_rx_thread_handle, |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 671 | NULL /*attr */ , client_rx_thread_fn, 0); |
| 672 | if (rv) |
| 673 | { |
| 674 | clib_warning ("pthread_create returned %d", rv); |
| 675 | rv = VNET_API_ERROR_SYSCALL_ERROR_1; |
| 676 | } |
| 677 | } |
| 678 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 679 | static void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 680 | send_test_chunk (echo_main_t * em, svm_fifo_t * tx_fifo, int mypid, u32 bytes) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 681 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 682 | u8 *test_data = em->connect_test_data; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 683 | u64 bytes_sent = 0; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 684 | int test_buf_offset = 0; |
| 685 | u32 bytes_to_snd; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 686 | u32 queue_max_chunk = 128 << 10, actual_write; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 687 | session_fifo_event_t evt; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 688 | int rv; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 689 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 690 | bytes_to_snd = (bytes == 0) ? vec_len (test_data) : bytes; |
| 691 | if (bytes_to_snd > vec_len (test_data)) |
| 692 | bytes_to_snd = vec_len (test_data); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 693 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 694 | while (bytes_to_snd > 0 && !em->time_to_stop) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 695 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 696 | actual_write = (bytes_to_snd > queue_max_chunk) ? |
| 697 | queue_max_chunk : bytes_to_snd; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 698 | rv = svm_fifo_enqueue_nowait (tx_fifo, actual_write, |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 699 | test_data + test_buf_offset); |
| 700 | |
| 701 | if (rv > 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 702 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 703 | bytes_to_snd -= rv; |
| 704 | test_buf_offset += rv; |
| 705 | bytes_sent += rv; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 706 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 707 | if (svm_fifo_set_event (tx_fifo)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 708 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 709 | /* Fabricate TX event, send to vpp */ |
| 710 | evt.fifo = tx_fifo; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 711 | evt.event_type = FIFO_EVENT_APP_TX; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 712 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 713 | svm_queue_add (em->vpp_event_queue, |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 714 | (u8 *) & evt, 0 /* do wait for mutex */ ); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 715 | } |
| 716 | } |
| 717 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 718 | } |
| 719 | |
| 720 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 721 | client_send_data (echo_main_t * em) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 722 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 723 | u8 *test_data = em->connect_test_data; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 724 | int mypid = getpid (); |
| 725 | session_t *session; |
| 726 | svm_fifo_t *tx_fifo; |
| 727 | u32 n_iterations, leftover; |
| 728 | int i; |
| 729 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 730 | session = pool_elt_at_index (em->sessions, em->connected_session_index); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 731 | tx_fifo = session->server_tx_fifo; |
| 732 | |
Florin Coras | 82b13a8 | 2017-04-25 11:58:06 -0700 | [diff] [blame] | 733 | ASSERT (vec_len (test_data) > 0); |
| 734 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 735 | vec_validate (em->rx_buf, vec_len (test_data) - 1); |
| 736 | n_iterations = em->bytes_to_send / vec_len (test_data); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 737 | |
| 738 | for (i = 0; i < n_iterations; i++) |
| 739 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 740 | send_test_chunk (em, tx_fifo, mypid, 0); |
| 741 | if (em->time_to_stop) |
Florin Coras | f6359c8 | 2017-06-19 12:26:09 -0400 | [diff] [blame] | 742 | break; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 743 | } |
| 744 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 745 | leftover = em->bytes_to_send % vec_len (test_data); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 746 | if (leftover) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 747 | send_test_chunk (em, tx_fifo, mypid, leftover); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 748 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 749 | if (!em->drop_packets) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 750 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 751 | f64 timeout = clib_time_now (&em->clib_time) + 10; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 752 | |
| 753 | /* Wait for the outstanding packets */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 754 | while (em->client_bytes_received < |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 755 | vec_len (test_data) * n_iterations + leftover) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 756 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 757 | if (clib_time_now (&em->clib_time) > timeout) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 758 | { |
| 759 | clib_warning ("timed out waiting for the missing packets"); |
| 760 | break; |
| 761 | } |
| 762 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 763 | } |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 764 | em->time_to_stop = 1; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 768 | client_send_connect (echo_main_t * em) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 769 | { |
| 770 | vl_api_connect_uri_t *cmp; |
| 771 | cmp = vl_msg_api_alloc (sizeof (*cmp)); |
| 772 | memset (cmp, 0, sizeof (*cmp)); |
| 773 | |
| 774 | cmp->_vl_msg_id = ntohs (VL_API_CONNECT_URI); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 775 | cmp->client_index = em->my_client_index; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 776 | cmp->context = ntohl (0xfeedface); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 777 | memcpy (cmp->uri, em->connect_uri, vec_len (em->connect_uri)); |
| 778 | vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & cmp); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 779 | } |
| 780 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 781 | int |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 782 | client_connect (echo_main_t * em) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 783 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 784 | client_send_connect (em); |
| 785 | if (wait_for_state_change (em, STATE_READY)) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 786 | { |
| 787 | clib_warning ("Connect failed"); |
| 788 | return -1; |
| 789 | } |
| 790 | return 0; |
| 791 | } |
| 792 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 793 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 794 | client_send_disconnect (echo_main_t * em) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 795 | { |
| 796 | session_t *connected_session; |
| 797 | vl_api_disconnect_session_t *dmp; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 798 | connected_session = pool_elt_at_index (em->sessions, |
| 799 | em->connected_session_index); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 800 | dmp = vl_msg_api_alloc (sizeof (*dmp)); |
| 801 | memset (dmp, 0, sizeof (*dmp)); |
| 802 | dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 803 | dmp->client_index = em->my_client_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 804 | dmp->handle = connected_session->vpp_session_handle; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 805 | vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & dmp); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 806 | } |
| 807 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 808 | int |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 809 | client_disconnect (echo_main_t * em) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 810 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 811 | client_send_disconnect (em); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 812 | clib_warning ("Sent disconnect"); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 813 | if (wait_for_state_change (em, STATE_START)) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 814 | { |
| 815 | clib_warning ("Disconnect failed"); |
| 816 | return -1; |
| 817 | } |
| 818 | return 0; |
| 819 | } |
| 820 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 821 | static void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 822 | client_run (echo_main_t * em) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 823 | { |
| 824 | int i; |
| 825 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 826 | if (application_attach (em)) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 827 | return; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 828 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 829 | if (client_connect (em)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 830 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 831 | application_detach (em); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 832 | return; |
| 833 | } |
| 834 | |
| 835 | /* Init test data */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 836 | vec_validate (em->connect_test_data, 128 * 1024 - 1); |
| 837 | for (i = 0; i < vec_len (em->connect_test_data); i++) |
| 838 | em->connect_test_data[i] = i & 0xff; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 839 | |
| 840 | /* Start send */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 841 | client_send_data (em); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 842 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 843 | /* Disconnect and detach */ |
| 844 | client_disconnect (em); |
| 845 | application_detach (em); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 846 | } |
| 847 | |
| 848 | static void |
| 849 | vl_api_bind_uri_reply_t_handler (vl_api_bind_uri_reply_t * mp) |
| 850 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 851 | echo_main_t *em = &echo_main; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 852 | |
| 853 | if (mp->retval) |
| 854 | { |
flyingeagle23 | a07779f | 2017-04-26 20:22:04 +0800 | [diff] [blame] | 855 | clib_warning ("bind failed: %U", format_api_error, |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 856 | clib_net_to_host_u32 (mp->retval)); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 857 | em->state = STATE_FAILED; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 858 | return; |
| 859 | } |
| 860 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 861 | em->state = STATE_READY; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 862 | } |
| 863 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 864 | static void |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 865 | vl_api_unbind_uri_reply_t_handler (vl_api_unbind_uri_reply_t * mp) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 866 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 867 | echo_main_t *em = &echo_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 868 | |
| 869 | if (mp->retval != 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 870 | clib_warning ("returned %d", ntohl (mp->retval)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 871 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 872 | em->state = STATE_START; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 873 | } |
| 874 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 875 | u8 * |
| 876 | format_ip4_address (u8 * s, va_list * args) |
| 877 | { |
| 878 | u8 *a = va_arg (*args, u8 *); |
| 879 | return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]); |
| 880 | } |
| 881 | |
| 882 | u8 * |
| 883 | format_ip6_address (u8 * s, va_list * args) |
| 884 | { |
| 885 | ip6_address_t *a = va_arg (*args, ip6_address_t *); |
| 886 | u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon; |
| 887 | |
| 888 | i_max_n_zero = ARRAY_LEN (a->as_u16); |
| 889 | max_n_zeros = 0; |
| 890 | i_first_zero = i_max_n_zero; |
| 891 | n_zeros = 0; |
| 892 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 893 | { |
| 894 | u32 is_zero = a->as_u16[i] == 0; |
| 895 | if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16)) |
| 896 | { |
| 897 | i_first_zero = i; |
| 898 | n_zeros = 0; |
| 899 | } |
| 900 | n_zeros += is_zero; |
| 901 | if ((!is_zero && n_zeros > max_n_zeros) |
| 902 | || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros)) |
| 903 | { |
| 904 | i_max_n_zero = i_first_zero; |
| 905 | max_n_zeros = n_zeros; |
| 906 | i_first_zero = ARRAY_LEN (a->as_u16); |
| 907 | n_zeros = 0; |
| 908 | } |
| 909 | } |
| 910 | |
| 911 | last_double_colon = 0; |
| 912 | for (i = 0; i < ARRAY_LEN (a->as_u16); i++) |
| 913 | { |
| 914 | if (i == i_max_n_zero && max_n_zeros > 1) |
| 915 | { |
| 916 | s = format (s, "::"); |
| 917 | i += max_n_zeros - 1; |
| 918 | last_double_colon = 1; |
| 919 | } |
| 920 | else |
| 921 | { |
| 922 | s = format (s, "%s%x", |
| 923 | (last_double_colon || i == 0) ? "" : ":", |
| 924 | clib_net_to_host_u16 (a->as_u16[i])); |
| 925 | last_double_colon = 0; |
| 926 | } |
| 927 | } |
| 928 | |
| 929 | return s; |
| 930 | } |
| 931 | |
| 932 | /* Format an IP46 address. */ |
| 933 | u8 * |
| 934 | format_ip46_address (u8 * s, va_list * args) |
| 935 | { |
| 936 | ip46_address_t *ip46 = va_arg (*args, ip46_address_t *); |
| 937 | ip46_type_t type = va_arg (*args, ip46_type_t); |
| 938 | int is_ip4 = 1; |
| 939 | |
| 940 | switch (type) |
| 941 | { |
| 942 | case IP46_TYPE_ANY: |
| 943 | is_ip4 = ip46_address_is_ip4 (ip46); |
| 944 | break; |
| 945 | case IP46_TYPE_IP4: |
| 946 | is_ip4 = 1; |
| 947 | break; |
| 948 | case IP46_TYPE_IP6: |
| 949 | is_ip4 = 0; |
| 950 | break; |
| 951 | } |
| 952 | |
| 953 | return is_ip4 ? |
| 954 | format (s, "%U", format_ip4_address, &ip46->ip4) : |
| 955 | format (s, "%U", format_ip6_address, &ip46->ip6); |
| 956 | } |
| 957 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 958 | static void |
| 959 | vl_api_accept_session_t_handler (vl_api_accept_session_t * mp) |
| 960 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 961 | echo_main_t *em = &echo_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 962 | vl_api_accept_session_reply_t *rmp; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 963 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 964 | session_t *session; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 965 | static f64 start_time; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 966 | u32 session_index; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 967 | u8 *ip_str; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 968 | |
| 969 | if (start_time == 0.0) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 970 | start_time = clib_time_now (&em->clib_time); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 971 | |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 972 | ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4); |
| 973 | clib_warning ("Accepted session from: %s:%d", ip_str, |
| 974 | clib_net_to_host_u16 (mp->port)); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 975 | em->vpp_event_queue = |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 976 | uword_to_pointer (mp->vpp_event_queue_address, svm_queue_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 977 | |
| 978 | /* Allocate local session and set it up */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 979 | pool_get (em->sessions, session); |
| 980 | session_index = session - em->sessions; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 981 | |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 982 | rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 983 | rx_fifo->client_session_index = session_index; |
Damjan Marion | 7bee80c | 2017-04-26 15:32:12 +0200 | [diff] [blame] | 984 | tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 985 | tx_fifo->client_session_index = session_index; |
| 986 | |
| 987 | session->server_rx_fifo = rx_fifo; |
| 988 | session->server_tx_fifo = tx_fifo; |
| 989 | |
| 990 | /* Add it to lookup table */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 991 | hash_set (em->session_index_by_vpp_handles, mp->handle, session_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 992 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 993 | em->state = STATE_READY; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 994 | |
| 995 | /* Stats printing */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 996 | if (pool_elts (em->sessions) && (pool_elts (em->sessions) % 20000) == 0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 997 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 998 | f64 now = clib_time_now (&em->clib_time); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 999 | fformat (stdout, "%d active sessions in %.2f seconds, %.2f/sec...\n", |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1000 | pool_elts (em->sessions), now - start_time, |
| 1001 | (f64) pool_elts (em->sessions) / (now - start_time)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1002 | } |
| 1003 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1004 | /* |
| 1005 | * Send accept reply to vpp |
| 1006 | */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1007 | rmp = vl_msg_api_alloc (sizeof (*rmp)); |
| 1008 | memset (rmp, 0, sizeof (*rmp)); |
| 1009 | rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1010 | rmp->handle = mp->handle; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1011 | rmp->context = mp->context; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1012 | vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & rmp); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1013 | |
| 1014 | session->bytes_received = 0; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1015 | session->start = clib_time_now (&em->clib_time); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1016 | } |
| 1017 | |
| 1018 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1019 | server_handle_fifo_event_rx (echo_main_t * em, session_fifo_event_t * e) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1020 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1021 | svm_fifo_t *rx_fifo, *tx_fifo; |
| 1022 | int n_read; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1023 | session_fifo_event_t evt; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1024 | svm_queue_t *q; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1025 | session_t *session; |
| 1026 | int rv; |
| 1027 | u32 max_dequeue, offset, max_transfer, rx_buf_len; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1028 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1029 | rx_buf_len = vec_len (em->rx_buf); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1030 | rx_fifo = e->fifo; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1031 | session = &em->sessions[rx_fifo->client_session_index]; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1032 | tx_fifo = session->server_tx_fifo; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1033 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1034 | max_dequeue = svm_fifo_max_dequeue (rx_fifo); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1035 | /* Allow enqueuing of a new event */ |
| 1036 | svm_fifo_unset_event (rx_fifo); |
| 1037 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1038 | if (PREDICT_FALSE (max_dequeue == 0)) |
| 1039 | { |
| 1040 | return; |
| 1041 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1042 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1043 | /* Read the max_dequeue */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1044 | do |
| 1045 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1046 | max_transfer = clib_min (rx_buf_len, max_dequeue); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1047 | n_read = svm_fifo_dequeue_nowait (rx_fifo, max_transfer, em->rx_buf); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1048 | if (n_read > 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1049 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1050 | max_dequeue -= n_read; |
| 1051 | session->bytes_received += n_read; |
| 1052 | } |
| 1053 | |
| 1054 | /* Reflect if a non-drop session */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1055 | if (!em->drop_packets && n_read > 0) |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1056 | { |
| 1057 | offset = 0; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1058 | do |
| 1059 | { |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1060 | rv = svm_fifo_enqueue_nowait (tx_fifo, n_read, |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1061 | &em->rx_buf[offset]); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1062 | if (rv > 0) |
| 1063 | { |
| 1064 | n_read -= rv; |
| 1065 | offset += rv; |
| 1066 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1067 | } |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1068 | while ((rv <= 0 || n_read > 0) && !em->time_to_stop); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1069 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1070 | /* If event wasn't set, add one */ |
| 1071 | if (svm_fifo_set_event (tx_fifo)) |
| 1072 | { |
| 1073 | /* Fabricate TX event, send to vpp */ |
| 1074 | evt.fifo = tx_fifo; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1075 | evt.event_type = FIFO_EVENT_APP_TX; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1076 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1077 | q = em->vpp_event_queue; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 1078 | svm_queue_add (q, (u8 *) & evt, 1 /* do wait for mutex */ ); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1079 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1080 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1081 | } |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1082 | while ((n_read < 0 || max_dequeue > 0) && !em->time_to_stop); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1083 | } |
| 1084 | |
| 1085 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1086 | server_handle_event_queue (echo_main_t * em) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1087 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 1088 | session_fifo_event_t _e, *e = &_e; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1089 | |
| 1090 | while (1) |
| 1091 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1092 | svm_queue_sub (em->our_event_queue, (u8 *) e, SVM_Q_WAIT, 0); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1093 | switch (e->event_type) |
| 1094 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1095 | case FIFO_EVENT_APP_RX: |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1096 | server_handle_fifo_event_rx (em, e); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1097 | break; |
| 1098 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1099 | case FIFO_EVENT_DISCONNECT: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1100 | return; |
| 1101 | |
| 1102 | default: |
| 1103 | clib_warning ("unknown event type %d", e->event_type); |
| 1104 | break; |
| 1105 | } |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1106 | if (PREDICT_FALSE (em->time_to_stop == 1)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1107 | break; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1108 | if (PREDICT_FALSE (em->time_to_print_stats == 1)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1109 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1110 | em->time_to_print_stats = 0; |
| 1111 | fformat (stdout, "%d connections\n", pool_elts (em->sessions)); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1112 | } |
| 1113 | } |
| 1114 | } |
| 1115 | |
| 1116 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1117 | server_send_listen (echo_main_t * em) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1118 | { |
| 1119 | vl_api_bind_uri_t *bmp; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1120 | bmp = vl_msg_api_alloc (sizeof (*bmp)); |
| 1121 | memset (bmp, 0, sizeof (*bmp)); |
| 1122 | |
| 1123 | bmp->_vl_msg_id = ntohs (VL_API_BIND_URI); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1124 | bmp->client_index = em->my_client_index; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1125 | bmp->context = ntohl (0xfeedface); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1126 | memcpy (bmp->uri, em->uri, vec_len (em->uri)); |
| 1127 | vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & bmp); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1128 | } |
| 1129 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1130 | int |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1131 | server_listen (echo_main_t * em) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1132 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1133 | server_send_listen (em); |
| 1134 | if (wait_for_state_change (em, STATE_READY)) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1135 | { |
| 1136 | clib_warning ("timeout waiting for STATE_READY"); |
| 1137 | return -1; |
| 1138 | } |
| 1139 | return 0; |
| 1140 | } |
| 1141 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1142 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1143 | server_send_unbind (echo_main_t * em) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1144 | { |
| 1145 | vl_api_unbind_uri_t *ump; |
| 1146 | |
| 1147 | ump = vl_msg_api_alloc (sizeof (*ump)); |
| 1148 | memset (ump, 0, sizeof (*ump)); |
| 1149 | |
| 1150 | ump->_vl_msg_id = ntohs (VL_API_UNBIND_URI); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1151 | ump->client_index = em->my_client_index; |
| 1152 | memcpy (ump->uri, em->uri, vec_len (em->uri)); |
| 1153 | vl_msg_api_send_shmem (em->vl_input_queue, (u8 *) & ump); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1154 | } |
| 1155 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1156 | int |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1157 | server_unbind (echo_main_t * em) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1158 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1159 | server_send_unbind (em); |
| 1160 | if (wait_for_state_change (em, STATE_START)) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1161 | { |
| 1162 | clib_warning ("timeout waiting for STATE_START"); |
| 1163 | return -1; |
| 1164 | } |
| 1165 | return 0; |
| 1166 | } |
| 1167 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1168 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1169 | server_run (echo_main_t * em) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1170 | { |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 1171 | session_t *session; |
| 1172 | int i; |
| 1173 | |
| 1174 | /* $$$$ hack preallocation */ |
| 1175 | for (i = 0; i < 200000; i++) |
| 1176 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1177 | pool_get (em->sessions, session); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 1178 | memset (session, 0, sizeof (*session)); |
| 1179 | } |
| 1180 | for (i = 0; i < 200000; i++) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1181 | pool_put_index (em->sessions, i); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 1182 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1183 | if (application_attach (em)) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1184 | return; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1185 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1186 | /* Bind to uri */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1187 | if (server_listen (em)) |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 1188 | return; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1189 | |
| 1190 | /* Enter handle event loop */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1191 | server_handle_event_queue (em); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1192 | |
| 1193 | /* Cleanup */ |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1194 | server_send_unbind (em); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1195 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1196 | application_detach (em); |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1197 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1198 | fformat (stdout, "Test complete...\n"); |
| 1199 | } |
| 1200 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1201 | static void |
| 1202 | vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t * |
| 1203 | mp) |
| 1204 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1205 | echo_main_t *em = &echo_main; |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1206 | session_t *session; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1207 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1208 | if (mp->retval) |
| 1209 | { |
| 1210 | clib_warning ("vpp complained about disconnect: %d", |
| 1211 | ntohl (mp->retval)); |
| 1212 | } |
| 1213 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1214 | em->state = STATE_START; |
| 1215 | session = pool_elt_at_index (em->sessions, em->connected_session_index); |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame] | 1216 | if (session) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1217 | session_print_stats (em, session); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1218 | } |
| 1219 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1220 | #define foreach_tcp_echo_msg \ |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 1221 | _(BIND_URI_REPLY, bind_uri_reply) \ |
| 1222 | _(UNBIND_URI_REPLY, unbind_uri_reply) \ |
| 1223 | _(ACCEPT_SESSION, accept_session) \ |
| 1224 | _(CONNECT_SESSION_REPLY, connect_session_reply) \ |
| 1225 | _(DISCONNECT_SESSION, disconnect_session) \ |
| 1226 | _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \ |
| 1227 | _(RESET_SESSION, reset_session) \ |
| 1228 | _(APPLICATION_ATTACH_REPLY, application_attach_reply) \ |
| 1229 | _(APPLICATION_DETACH_REPLY, application_detach_reply) \ |
| 1230 | _(MAP_ANOTHER_SEGMENT, map_another_segment) \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1231 | |
| 1232 | void |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1233 | tcp_echo_api_hookup (echo_main_t * em) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1234 | { |
| 1235 | #define _(N,n) \ |
| 1236 | vl_msg_api_set_handlers(VL_API_##N, #n, \ |
| 1237 | vl_api_##n##_t_handler, \ |
| 1238 | vl_noop_handler, \ |
| 1239 | vl_api_##n##_t_endian, \ |
| 1240 | vl_api_##n##_t_print, \ |
| 1241 | sizeof(vl_api_##n##_t), 1); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1242 | foreach_tcp_echo_msg; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1243 | #undef _ |
| 1244 | } |
| 1245 | |
| 1246 | int |
| 1247 | main (int argc, char **argv) |
| 1248 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1249 | int i_am_master = 1, drop_packets = 0, test_return_packets = 0; |
| 1250 | echo_main_t *em = &echo_main; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1251 | unformat_input_t _argv, *a = &_argv; |
| 1252 | u8 *chroot_prefix; |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1253 | u8 *heap, *uri = 0; |
| 1254 | u8 *bind_uri = (u8 *) "tcp://0.0.0.0/1234"; |
| 1255 | u8 *connect_uri = (u8 *) "tcp://6.0.1.2/1234"; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1256 | u64 bytes_to_send = 64 << 10, mbytes; |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1257 | char *app_name; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1258 | u32 tmp; |
| 1259 | mheap_t *h; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1260 | |
| 1261 | clib_mem_init (0, 256 << 20); |
| 1262 | |
| 1263 | heap = clib_mem_get_per_cpu_heap (); |
| 1264 | h = mheap_header (heap); |
| 1265 | |
| 1266 | /* make the main heap thread-safe */ |
| 1267 | h->flags |= MHEAP_FLAG_THREAD_SAFE; |
| 1268 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1269 | vec_validate (em->rx_buf, 128 << 10); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1270 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1271 | em->session_index_by_vpp_handles = hash_create (0, sizeof (uword)); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1272 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1273 | em->my_pid = getpid (); |
| 1274 | em->configured_segment_size = 1 << 20; |
| 1275 | em->socket_name = 0; |
| 1276 | em->use_sock_api = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1277 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1278 | clib_time_init (&em->clib_time); |
| 1279 | init_error_string_table (em); |
Florin Coras | a332c46 | 2018-01-31 06:52:17 -0800 | [diff] [blame^] | 1280 | svm_fifo_segment_main_init (0x200000000ULL, 20); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1281 | unformat_init_command_line (a, argv); |
| 1282 | |
| 1283 | while (unformat_check_input (a) != UNFORMAT_END_OF_INPUT) |
| 1284 | { |
| 1285 | if (unformat (a, "chroot prefix %s", &chroot_prefix)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1286 | { |
| 1287 | vl_set_memory_root_path ((char *) chroot_prefix); |
| 1288 | } |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1289 | else if (unformat (a, "uri %s", &uri)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1290 | ; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1291 | else if (unformat (a, "segment-size %dM", &tmp)) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1292 | em->configured_segment_size = tmp << 20; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1293 | else if (unformat (a, "segment-size %dG", &tmp)) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1294 | em->configured_segment_size = tmp << 30; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1295 | else if (unformat (a, "master")) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1296 | i_am_master = 1; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1297 | else if (unformat (a, "slave")) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1298 | i_am_master = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1299 | else if (unformat (a, "drop")) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1300 | drop_packets = 1; |
| 1301 | else if (unformat (a, "test")) |
| 1302 | test_return_packets = 1; |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1303 | else if (unformat (a, "mbytes %lld", &mbytes)) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 1304 | { |
| 1305 | bytes_to_send = mbytes << 20; |
| 1306 | } |
Florin Coras | 6cf30ad | 2017-04-04 23:08:23 -0700 | [diff] [blame] | 1307 | else if (unformat (a, "gbytes %lld", &mbytes)) |
| 1308 | { |
| 1309 | bytes_to_send = mbytes << 30; |
| 1310 | } |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1311 | else if (unformat (a, "socket-name %s", &em->socket_name)) |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 1312 | ; |
| 1313 | else if (unformat (a, "use-svm-api")) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1314 | em->use_sock_api = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1315 | else |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1316 | { |
| 1317 | fformat (stderr, "%s: usage [master|slave]\n"); |
| 1318 | exit (1); |
| 1319 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1320 | } |
| 1321 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1322 | if (!em->socket_name) |
| 1323 | em->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 1324 | |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1325 | if (uri) |
| 1326 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1327 | em->uri = format (0, "%s%c", uri, 0); |
| 1328 | em->connect_uri = format (0, "%s%c", uri, 0); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1329 | } |
| 1330 | else |
| 1331 | { |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1332 | em->uri = format (0, "%s%c", bind_uri, 0); |
| 1333 | em->connect_uri = format (0, "%s%c", connect_uri, 0); |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 1334 | } |
| 1335 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1336 | em->i_am_master = i_am_master; |
| 1337 | em->segment_main = &svm_fifo_segment_main; |
| 1338 | em->drop_packets = drop_packets; |
| 1339 | em->test_return_packets = test_return_packets; |
| 1340 | em->bytes_to_send = bytes_to_send; |
| 1341 | em->time_to_stop = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1342 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1343 | setup_signal_handlers (); |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1344 | tcp_echo_api_hookup (em); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1345 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1346 | app_name = i_am_master ? "tcp_echo_server" : "tcp_echo_client"; |
| 1347 | if (connect_to_vpp (app_name) < 0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1348 | { |
| 1349 | svm_region_exit (); |
| 1350 | fformat (stderr, "Couldn't connect to vpe, exiting...\n"); |
| 1351 | exit (1); |
| 1352 | } |
| 1353 | |
| 1354 | if (i_am_master == 0) |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1355 | client_run (em); |
Florin Coras | 90a6398 | 2017-12-19 04:50:01 -0800 | [diff] [blame] | 1356 | else |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1357 | server_run (em); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1358 | |
Florin Coras | b384b54 | 2018-01-15 01:08:33 -0800 | [diff] [blame] | 1359 | disconnect_from_vpp (em); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1360 | exit (0); |
| 1361 | } |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 1362 | |
| 1363 | /* |
| 1364 | * fd.io coding-style-patch-verification: ON |
| 1365 | * |
| 1366 | * Local Variables: |
| 1367 | * eval: (c-set-style "gnu") |
| 1368 | * End: |
| 1369 | */ |