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