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