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