Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015-2017 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 <vnet/vnet.h> |
| 17 | #include <vlibmemory/api.h> |
| 18 | #include <vnet/session/application.h> |
| 19 | #include <vnet/session/application_interface.h> |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 20 | #include <vnet/session-apps/proxy.h> |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 21 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 22 | proxy_main_t proxy_main; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 23 | |
| 24 | static void |
| 25 | delete_proxy_session (stream_session_t * s, int is_active_open) |
| 26 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 27 | proxy_main_t *pm = &proxy_main; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 28 | proxy_session_t *ps = 0; |
| 29 | vnet_disconnect_args_t _a, *a = &_a; |
| 30 | stream_session_t *active_open_session = 0; |
| 31 | stream_session_t *server_session = 0; |
| 32 | uword *p; |
| 33 | u64 handle; |
| 34 | |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 35 | handle = session_handle (s); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 36 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 37 | clib_spinlock_lock_if_init (&pm->sessions_lock); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 38 | if (is_active_open) |
| 39 | { |
| 40 | active_open_session = s; |
| 41 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 42 | p = hash_get (pm->proxy_session_by_active_open_handle, handle); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 43 | if (p == 0) |
| 44 | { |
| 45 | clib_warning ("proxy session for %s handle %lld (%llx) AWOL", |
| 46 | is_active_open ? "active open" : "server", |
| 47 | handle, handle); |
| 48 | } |
| 49 | else |
| 50 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 51 | ps = pool_elt_at_index (pm->sessions, p[0]); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 52 | if (ps->vpp_server_handle != ~0) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 53 | server_session = session_get_from_handle (ps->vpp_server_handle); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 54 | else |
| 55 | server_session = 0; |
| 56 | } |
| 57 | } |
| 58 | else |
| 59 | { |
| 60 | server_session = s; |
| 61 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 62 | p = hash_get (pm->proxy_session_by_server_handle, handle); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 63 | if (p == 0) |
| 64 | { |
| 65 | clib_warning ("proxy session for %s handle %lld (%llx) AWOL", |
| 66 | is_active_open ? "active open" : "server", |
| 67 | handle, handle); |
| 68 | } |
| 69 | else |
| 70 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 71 | ps = pool_elt_at_index (pm->sessions, p[0]); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 72 | if (ps->vpp_server_handle != ~0) |
Florin Coras | cea194d | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 73 | active_open_session = session_get_from_handle |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 74 | (ps->vpp_server_handle); |
| 75 | else |
| 76 | active_open_session = 0; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if (ps) |
| 81 | { |
| 82 | if (CLIB_DEBUG > 0) |
| 83 | memset (ps, 0xFE, sizeof (*ps)); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 84 | pool_put (pm->sessions, ps); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 85 | } |
| 86 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 87 | clib_spinlock_unlock_if_init (&pm->sessions_lock); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 88 | |
| 89 | if (active_open_session) |
| 90 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 91 | a->handle = session_handle (active_open_session); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 92 | a->app_index = pm->active_open_app_index; |
| 93 | hash_unset (pm->proxy_session_by_active_open_handle, |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 94 | session_handle (active_open_session)); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 95 | vnet_disconnect_session (a); |
| 96 | } |
| 97 | |
| 98 | if (server_session) |
| 99 | { |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 100 | a->handle = session_handle (server_session); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 101 | a->app_index = pm->server_app_index; |
| 102 | hash_unset (pm->proxy_session_by_server_handle, |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 103 | session_handle (server_session)); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 104 | vnet_disconnect_session (a); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 109 | proxy_accept_callback (stream_session_t * s) |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 110 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 111 | proxy_main_t *pm = &proxy_main; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 112 | |
| 113 | s->session_state = SESSION_STATE_READY; |
| 114 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 115 | clib_spinlock_lock_if_init (&pm->sessions_lock); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 116 | |
| 117 | return 0; |
| 118 | } |
| 119 | |
| 120 | static void |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 121 | proxy_disconnect_callback (stream_session_t * s) |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 122 | { |
| 123 | delete_proxy_session (s, 0 /* is_active_open */ ); |
| 124 | } |
| 125 | |
| 126 | static void |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 127 | proxy_reset_callback (stream_session_t * s) |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 128 | { |
| 129 | clib_warning ("Reset session %U", format_stream_session, s, 2); |
| 130 | delete_proxy_session (s, 0 /* is_active_open */ ); |
| 131 | } |
| 132 | |
| 133 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 134 | proxy_connected_callback (u32 app_index, u32 api_context, |
| 135 | stream_session_t * s, u8 is_fail) |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 136 | { |
| 137 | clib_warning ("called..."); |
| 138 | return -1; |
| 139 | } |
| 140 | |
| 141 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 142 | proxy_add_segment_callback (u32 client_index, const ssvm_private_t * sp) |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 143 | { |
| 144 | clib_warning ("called..."); |
| 145 | return -1; |
| 146 | } |
| 147 | |
| 148 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 149 | proxy_redirect_connect_callback (u32 client_index, void *mp) |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 150 | { |
| 151 | clib_warning ("called..."); |
| 152 | return -1; |
| 153 | } |
| 154 | |
| 155 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 156 | proxy_rx_callback (stream_session_t * s) |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 157 | { |
| 158 | u32 max_dequeue; |
| 159 | int actual_transfer __attribute__ ((unused)); |
| 160 | svm_fifo_t *tx_fifo, *rx_fifo; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 161 | proxy_main_t *pm = &proxy_main; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 162 | u32 thread_index = vlib_get_thread_index (); |
| 163 | vnet_connect_args_t _a, *a = &_a; |
| 164 | proxy_session_t *ps; |
| 165 | int proxy_index; |
| 166 | uword *p; |
| 167 | svm_fifo_t *active_open_tx_fifo; |
| 168 | session_fifo_event_t evt; |
| 169 | |
| 170 | ASSERT (s->thread_index == thread_index); |
| 171 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 172 | clib_spinlock_lock_if_init (&pm->sessions_lock); |
| 173 | p = hash_get (pm->proxy_session_by_server_handle, session_handle (s)); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 174 | |
| 175 | if (PREDICT_TRUE (p != 0)) |
| 176 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 177 | clib_spinlock_unlock_if_init (&pm->sessions_lock); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 178 | active_open_tx_fifo = s->server_rx_fifo; |
| 179 | |
| 180 | /* |
| 181 | * Send event for active open tx fifo |
| 182 | */ |
| 183 | if (svm_fifo_set_event (active_open_tx_fifo)) |
| 184 | { |
| 185 | evt.fifo = active_open_tx_fifo; |
| 186 | evt.event_type = FIFO_EVENT_APP_TX; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 187 | if (svm_queue_add |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 188 | (pm->active_open_event_queue[thread_index], (u8 *) & evt, |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 189 | 0 /* do wait for mutex */ )) |
| 190 | clib_warning ("failed to enqueue tx evt"); |
| 191 | } |
| 192 | } |
| 193 | else |
| 194 | { |
| 195 | rx_fifo = s->server_rx_fifo; |
| 196 | tx_fifo = s->server_tx_fifo; |
| 197 | |
| 198 | ASSERT (rx_fifo->master_thread_index == thread_index); |
| 199 | ASSERT (tx_fifo->master_thread_index == thread_index); |
| 200 | |
| 201 | max_dequeue = svm_fifo_max_dequeue (s->server_rx_fifo); |
| 202 | |
| 203 | if (PREDICT_FALSE (max_dequeue == 0)) |
| 204 | return 0; |
| 205 | |
| 206 | actual_transfer = svm_fifo_peek (rx_fifo, 0 /* relative_offset */ , |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 207 | max_dequeue, pm->rx_buf[thread_index]); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 208 | |
| 209 | /* $$$ your message in this space: parse url, etc. */ |
| 210 | |
| 211 | memset (a, 0, sizeof (*a)); |
| 212 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 213 | clib_spinlock_lock_if_init (&pm->sessions_lock); |
| 214 | pool_get (pm->sessions, ps); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 215 | memset (ps, 0, sizeof (*ps)); |
| 216 | ps->server_rx_fifo = rx_fifo; |
| 217 | ps->server_tx_fifo = tx_fifo; |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 218 | ps->vpp_server_handle = session_handle (s); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 219 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 220 | proxy_index = ps - pm->sessions; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 221 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 222 | hash_set (pm->proxy_session_by_server_handle, ps->vpp_server_handle, |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 223 | proxy_index); |
| 224 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 225 | clib_spinlock_unlock_if_init (&pm->sessions_lock); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 226 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 227 | a->uri = (char *) pm->client_uri; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 228 | a->api_context = proxy_index; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 229 | a->app_index = pm->active_open_app_index; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 230 | a->mp = 0; |
| 231 | vnet_connect_uri (a); |
| 232 | } |
| 233 | |
| 234 | return 0; |
| 235 | } |
| 236 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 237 | static session_cb_vft_t proxy_session_cb_vft = { |
| 238 | .session_accept_callback = proxy_accept_callback, |
| 239 | .session_disconnect_callback = proxy_disconnect_callback, |
| 240 | .session_connected_callback = proxy_connected_callback, |
| 241 | .add_segment_callback = proxy_add_segment_callback, |
| 242 | .redirect_connect_callback = proxy_redirect_connect_callback, |
| 243 | .builtin_server_rx_callback = proxy_rx_callback, |
| 244 | .session_reset_callback = proxy_reset_callback |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 245 | }; |
| 246 | |
| 247 | static int |
| 248 | active_open_connected_callback (u32 app_index, u32 opaque, |
| 249 | stream_session_t * s, u8 is_fail) |
| 250 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 251 | proxy_main_t *pm = &proxy_main; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 252 | proxy_session_t *ps; |
| 253 | u8 thread_index = vlib_get_thread_index (); |
| 254 | session_fifo_event_t evt; |
| 255 | |
| 256 | if (is_fail) |
| 257 | { |
| 258 | clib_warning ("connection %d failed!", opaque); |
| 259 | return 0; |
| 260 | } |
| 261 | |
| 262 | /* |
| 263 | * Setup proxy session handle. |
| 264 | */ |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 265 | clib_spinlock_lock_if_init (&pm->sessions_lock); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 266 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 267 | ps = pool_elt_at_index (pm->sessions, opaque); |
Florin Coras | 3cbc04b | 2017-10-02 00:18:51 -0700 | [diff] [blame] | 268 | ps->vpp_active_open_handle = session_handle (s); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 269 | |
| 270 | s->server_tx_fifo = ps->server_rx_fifo; |
| 271 | s->server_rx_fifo = ps->server_tx_fifo; |
| 272 | |
| 273 | /* |
| 274 | * Reset the active-open tx-fifo master indices so the active-open session |
| 275 | * will receive data, etc. |
| 276 | */ |
| 277 | s->server_tx_fifo->master_session_index = s->session_index; |
| 278 | s->server_tx_fifo->master_thread_index = s->thread_index; |
| 279 | |
| 280 | /* |
| 281 | * Account for the active-open session's use of the fifos |
| 282 | * so they won't disappear until the last session which uses |
| 283 | * them disappears |
| 284 | */ |
| 285 | s->server_tx_fifo->refcnt++; |
| 286 | s->server_rx_fifo->refcnt++; |
| 287 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 288 | hash_set (pm->proxy_session_by_active_open_handle, |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 289 | ps->vpp_active_open_handle, opaque); |
| 290 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 291 | clib_spinlock_unlock_if_init (&pm->sessions_lock); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 292 | |
| 293 | /* |
| 294 | * Send event for active open tx fifo |
| 295 | */ |
| 296 | if (svm_fifo_set_event (s->server_tx_fifo)) |
| 297 | { |
| 298 | evt.fifo = s->server_tx_fifo; |
| 299 | evt.event_type = FIFO_EVENT_APP_TX; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 300 | if (svm_queue_add |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 301 | (pm->active_open_event_queue[thread_index], (u8 *) & evt, |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 302 | 0 /* do wait for mutex */ )) |
| 303 | clib_warning ("failed to enqueue tx evt"); |
| 304 | } |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | static void |
| 310 | active_open_reset_callback (stream_session_t * s) |
| 311 | { |
| 312 | delete_proxy_session (s, 1 /* is_active_open */ ); |
| 313 | } |
| 314 | |
| 315 | static int |
| 316 | active_open_create_callback (stream_session_t * s) |
| 317 | { |
| 318 | return 0; |
| 319 | } |
| 320 | |
| 321 | static void |
| 322 | active_open_disconnect_callback (stream_session_t * s) |
| 323 | { |
| 324 | delete_proxy_session (s, 1 /* is_active_open */ ); |
| 325 | } |
| 326 | |
| 327 | static int |
| 328 | active_open_rx_callback (stream_session_t * s) |
| 329 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 330 | proxy_main_t *pm = &proxy_main; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 331 | session_fifo_event_t evt; |
| 332 | svm_fifo_t *server_rx_fifo; |
| 333 | u32 thread_index = vlib_get_thread_index (); |
| 334 | |
| 335 | server_rx_fifo = s->server_rx_fifo; |
| 336 | |
| 337 | /* |
| 338 | * Send event for server tx fifo |
| 339 | */ |
| 340 | if (svm_fifo_set_event (server_rx_fifo)) |
| 341 | { |
| 342 | evt.fifo = server_rx_fifo; |
| 343 | evt.event_type = FIFO_EVENT_APP_TX; |
Florin Coras | e86a8ed | 2018-01-05 03:20:25 -0800 | [diff] [blame] | 344 | if (svm_queue_add |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 345 | (pm->server_event_queue[thread_index], (u8 *) & evt, |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 346 | 0 /* do wait for mutex */ )) |
| 347 | clib_warning ("failed to enqueue server rx evt"); |
| 348 | } |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | |
| 353 | /* *INDENT-OFF* */ |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 354 | static session_cb_vft_t active_open_clients = { |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 355 | .session_reset_callback = active_open_reset_callback, |
| 356 | .session_connected_callback = active_open_connected_callback, |
| 357 | .session_accept_callback = active_open_create_callback, |
| 358 | .session_disconnect_callback = active_open_disconnect_callback, |
| 359 | .builtin_server_rx_callback = active_open_rx_callback |
| 360 | }; |
| 361 | /* *INDENT-ON* */ |
| 362 | |
| 363 | |
| 364 | static void |
| 365 | create_api_loopbacks (vlib_main_t * vm) |
| 366 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 367 | proxy_main_t *pm = &proxy_main; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 368 | api_main_t *am = &api_main; |
| 369 | vl_shmem_hdr_t *shmem_hdr; |
| 370 | |
| 371 | shmem_hdr = am->shmem_hdr; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 372 | pm->vl_input_queue = shmem_hdr->vl_input_queue; |
| 373 | pm->server_client_index = |
| 374 | vl_api_memclnt_create_internal ("proxy_server", pm->vl_input_queue); |
| 375 | pm->active_open_client_index = |
| 376 | vl_api_memclnt_create_internal ("proxy_active_open", pm->vl_input_queue); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 380 | proxy_server_attach () |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 381 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 382 | proxy_main_t *pm = &proxy_main; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 383 | u64 options[APP_OPTIONS_N_OPTIONS]; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 384 | vnet_app_attach_args_t _a, *a = &_a; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 385 | u32 segment_size = 512 << 20; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 386 | |
| 387 | memset (a, 0, sizeof (*a)); |
| 388 | memset (options, 0, sizeof (options)); |
| 389 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 390 | if (pm->private_segment_size) |
| 391 | segment_size = pm->private_segment_size; |
| 392 | a->api_client_index = pm->server_client_index; |
| 393 | a->session_cb_vft = &proxy_session_cb_vft; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 394 | a->options = options; |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 395 | a->options[APP_OPTIONS_SEGMENT_SIZE] = segment_size; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 396 | a->options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size; |
| 397 | a->options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size; |
| 398 | a->options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 399 | a->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 400 | pm->prealloc_fifos ? pm->prealloc_fifos : 1; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 401 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 402 | a->options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 403 | |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 404 | if (vnet_application_attach (a)) |
| 405 | { |
| 406 | clib_warning ("failed to attach server"); |
| 407 | return -1; |
| 408 | } |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 409 | pm->server_app_index = a->app_index; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 410 | |
| 411 | return 0; |
| 412 | } |
| 413 | |
| 414 | static int |
| 415 | active_open_attach (void) |
| 416 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 417 | proxy_main_t *pm = &proxy_main; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 418 | vnet_app_attach_args_t _a, *a = &_a; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 419 | u64 options[16]; |
| 420 | |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 421 | memset (a, 0, sizeof (*a)); |
| 422 | memset (options, 0, sizeof (options)); |
| 423 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 424 | a->api_client_index = pm->active_open_client_index; |
| 425 | a->session_cb_vft = &active_open_clients; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 426 | |
Florin Coras | ff6e769 | 2017-12-11 04:59:01 -0800 | [diff] [blame] | 427 | options[APP_OPTIONS_ACCEPT_COOKIE] = 0x12345678; |
| 428 | options[APP_OPTIONS_SEGMENT_SIZE] = 512 << 20; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 429 | options[APP_OPTIONS_RX_FIFO_SIZE] = pm->fifo_size; |
| 430 | options[APP_OPTIONS_TX_FIFO_SIZE] = pm->fifo_size; |
| 431 | options[APP_OPTIONS_PRIVATE_SEGMENT_COUNT] = pm->private_segment_count; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 432 | options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] = |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 433 | pm->prealloc_fifos ? pm->prealloc_fifos : 1; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 434 | |
Florin Coras | 7999e83 | 2017-10-31 01:51:04 -0700 | [diff] [blame] | 435 | options[APP_OPTIONS_FLAGS] = APP_OPTIONS_FLAGS_IS_BUILTIN |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 436 | | APP_OPTIONS_FLAGS_IS_PROXY; |
| 437 | |
| 438 | a->options = options; |
| 439 | |
| 440 | if (vnet_application_attach (a)) |
| 441 | return -1; |
| 442 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 443 | pm->active_open_app_index = a->app_index; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 444 | |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 449 | proxy_server_listen () |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 450 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 451 | proxy_main_t *pm = &proxy_main; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 452 | vnet_bind_args_t _a, *a = &_a; |
| 453 | memset (a, 0, sizeof (*a)); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 454 | a->app_index = pm->server_app_index; |
| 455 | a->uri = (char *) pm->server_uri; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 456 | return vnet_bind_uri (a); |
| 457 | } |
| 458 | |
| 459 | static int |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 460 | proxy_server_create (vlib_main_t * vm) |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 461 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 462 | proxy_main_t *pm = &proxy_main; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 463 | vlib_thread_main_t *vtm = vlib_get_thread_main (); |
| 464 | u32 num_threads; |
| 465 | int i; |
| 466 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 467 | if (pm->server_client_index == (u32) ~ 0) |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 468 | create_api_loopbacks (vm); |
| 469 | |
| 470 | num_threads = 1 /* main thread */ + vtm->n_threads; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 471 | vec_validate (proxy_main.server_event_queue, num_threads - 1); |
| 472 | vec_validate (proxy_main.active_open_event_queue, num_threads - 1); |
| 473 | vec_validate (pm->rx_buf, num_threads - 1); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 474 | |
| 475 | for (i = 0; i < num_threads; i++) |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 476 | vec_validate (pm->rx_buf[i], pm->rcv_buffer_size); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 477 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 478 | if (proxy_server_attach ()) |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 479 | { |
| 480 | clib_warning ("failed to attach server app"); |
| 481 | return -1; |
| 482 | } |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 483 | if (proxy_server_listen ()) |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 484 | { |
| 485 | clib_warning ("failed to start listening"); |
| 486 | return -1; |
| 487 | } |
| 488 | if (active_open_attach ()) |
| 489 | { |
| 490 | clib_warning ("failed to attach active open app"); |
| 491 | return -1; |
| 492 | } |
| 493 | |
| 494 | for (i = 0; i < num_threads; i++) |
| 495 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 496 | pm->active_open_event_queue[i] = |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 497 | session_manager_get_vpp_event_queue (i); |
| 498 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 499 | ASSERT (pm->active_open_event_queue[i]); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 500 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 501 | pm->server_event_queue[i] = session_manager_get_vpp_event_queue (i); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | return 0; |
| 505 | } |
| 506 | |
| 507 | static clib_error_t * |
| 508 | proxy_server_create_command_fn (vlib_main_t * vm, unformat_input_t * input, |
| 509 | vlib_cli_command_t * cmd) |
| 510 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 511 | proxy_main_t *pm = &proxy_main; |
| 512 | char *default_server_uri = "tcp://0.0.0.0/23"; |
| 513 | char *default_client_uri = "tcp://6.0.2.2/23"; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 514 | int rv; |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 515 | u64 tmp; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 516 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 517 | pm->fifo_size = 64 << 10; |
| 518 | pm->rcv_buffer_size = 1024; |
| 519 | pm->prealloc_fifos = 0; |
| 520 | pm->private_segment_count = 0; |
| 521 | pm->private_segment_size = 0; |
| 522 | pm->server_uri = 0; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 523 | |
| 524 | while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) |
| 525 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 526 | if (unformat (input, "fifo-size %d", &pm->fifo_size)) |
| 527 | pm->fifo_size <<= 10; |
| 528 | else if (unformat (input, "rcv-buf-size %d", &pm->rcv_buffer_size)) |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 529 | ; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 530 | else if (unformat (input, "prealloc-fifos %d", &pm->prealloc_fifos)) |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 531 | ; |
| 532 | else if (unformat (input, "private-segment-count %d", |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 533 | &pm->private_segment_count)) |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 534 | ; |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 535 | else if (unformat (input, "private-segment-size %U", |
| 536 | unformat_memory_size, &tmp)) |
| 537 | { |
| 538 | if (tmp >= 0x100000000ULL) |
| 539 | return clib_error_return |
| 540 | (0, "private segment size %lld (%llu) too large", tmp, tmp); |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 541 | pm->private_segment_size = tmp; |
Dave Barach | 91f3e74 | 2017-09-01 19:12:11 -0400 | [diff] [blame] | 542 | } |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 543 | else if (unformat (input, "server-uri %s", &pm->server_uri)) |
Florin Coras | ee7e1f5 | 2018-01-11 02:23:35 -0800 | [diff] [blame] | 544 | ; |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 545 | else if (unformat (input, "client-uri %s", &pm->client_uri)) |
Florin Coras | ee7e1f5 | 2018-01-11 02:23:35 -0800 | [diff] [blame] | 546 | ; |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 547 | else |
| 548 | return clib_error_return (0, "unknown input `%U'", |
| 549 | format_unformat_error, input); |
| 550 | } |
| 551 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 552 | if (!pm->server_uri) |
| 553 | { |
| 554 | clib_warning ("No server-uri provided, Using default: %s", |
| 555 | default_server_uri); |
| 556 | pm->server_uri = format (0, "%s%c", default_server_uri, 0); |
| 557 | } |
| 558 | if (!pm->client_uri) |
| 559 | { |
| 560 | clib_warning ("No client-uri provided, Using default: %s", |
| 561 | default_client_uri); |
| 562 | pm->client_uri = format (0, "%s%c", default_client_uri, 0); |
| 563 | } |
Florin Coras | ee7e1f5 | 2018-01-11 02:23:35 -0800 | [diff] [blame] | 564 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 565 | vnet_session_enable_disable (vm, 1 /* turn on session and transport */ ); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 566 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 567 | rv = proxy_server_create (vm); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 568 | switch (rv) |
| 569 | { |
| 570 | case 0: |
| 571 | break; |
| 572 | default: |
| 573 | return clib_error_return (0, "server_create returned %d", rv); |
| 574 | } |
| 575 | |
| 576 | return 0; |
| 577 | } |
| 578 | |
| 579 | /* *INDENT-OFF* */ |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 580 | VLIB_CLI_COMMAND (proxy_create_command, static) = |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 581 | { |
| 582 | .path = "test proxy server", |
Florin Coras | ee7e1f5 | 2018-01-11 02:23:35 -0800 | [diff] [blame] | 583 | .short_help = "test proxy server [server-uri <tcp://ip/port>]" |
| 584 | "[client-uri <tcp://ip/port>][fifo-size <nn>][rcv-buf-size <nn>]" |
| 585 | "[prealloc-fifos <nn>][private-segment-size <mem>]" |
| 586 | "[private-segment-count <nn>]", |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 587 | .function = proxy_server_create_command_fn, |
| 588 | }; |
| 589 | /* *INDENT-ON* */ |
| 590 | |
| 591 | clib_error_t * |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 592 | proxy_main_init (vlib_main_t * vm) |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 593 | { |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 594 | proxy_main_t *pm = &proxy_main; |
| 595 | pm->server_client_index = ~0; |
| 596 | pm->active_open_client_index = ~0; |
| 597 | pm->proxy_session_by_active_open_handle = hash_create (0, sizeof (uword)); |
| 598 | pm->proxy_session_by_server_handle = hash_create (0, sizeof (uword)); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 599 | |
| 600 | return 0; |
| 601 | } |
| 602 | |
Florin Coras | 4399c2e | 2018-01-25 06:34:42 -0800 | [diff] [blame] | 603 | VLIB_INIT_FUNCTION (proxy_main_init); |
Dave Barach | 52851e6 | 2017-08-07 09:35:25 -0400 | [diff] [blame] | 604 | |
| 605 | /* |
| 606 | * fd.io coding-style-patch-verification: ON |
| 607 | * |
| 608 | * Local Variables: |
| 609 | * eval: (c-set-style "gnu") |
| 610 | * End: |
| 611 | */ |