Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2019 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/session/application_local.h> |
| 17 | #include <vnet/session/session.h> |
| 18 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 19 | static ct_connection_t *connections; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 20 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 21 | static void |
| 22 | ct_enable_disable_main_pre_input_node (u8 is_add) |
| 23 | { |
| 24 | u32 n_conns; |
| 25 | |
Florin Coras | 6575a9a | 2019-04-02 17:06:05 -0700 | [diff] [blame] | 26 | if (!vlib_num_workers ()) |
| 27 | return; |
| 28 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 29 | n_conns = pool_elts (connections); |
| 30 | if (n_conns > 2) |
| 31 | return; |
| 32 | |
| 33 | if (n_conns > 0 && is_add) |
| 34 | vlib_node_set_state (vlib_get_main (), |
| 35 | session_queue_pre_input_node.index, |
| 36 | VLIB_NODE_STATE_POLLING); |
| 37 | else if (n_conns == 0) |
| 38 | vlib_node_set_state (vlib_get_main (), |
| 39 | session_queue_pre_input_node.index, |
| 40 | VLIB_NODE_STATE_DISABLED); |
| 41 | } |
| 42 | |
| 43 | static ct_connection_t * |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 44 | ct_connection_alloc (void) |
| 45 | { |
| 46 | ct_connection_t *ct; |
| 47 | |
| 48 | pool_get_zero (connections, ct); |
| 49 | ct->c_c_index = ct - connections; |
| 50 | ct->c_thread_index = 0; |
| 51 | ct->client_wrk = ~0; |
| 52 | ct->server_wrk = ~0; |
| 53 | return ct; |
| 54 | } |
| 55 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 56 | static ct_connection_t * |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 57 | ct_connection_get (u32 ct_index) |
| 58 | { |
| 59 | if (pool_is_free_index (connections, ct_index)) |
| 60 | return 0; |
| 61 | return pool_elt_at_index (connections, ct_index); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 62 | } |
| 63 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 64 | static void |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 65 | ct_connection_free (ct_connection_t * ct) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 66 | { |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 67 | if (CLIB_DEBUG) |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 68 | memset (ct, 0xfc, sizeof (*ct)); |
| 69 | pool_put (connections, ct); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 70 | } |
| 71 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 72 | session_t * |
| 73 | ct_session_get_peer (session_t * s) |
| 74 | { |
| 75 | ct_connection_t *ct, *peer_ct; |
| 76 | ct = ct_connection_get (s->connection_index); |
| 77 | peer_ct = ct_connection_get (ct->peer_index); |
| 78 | return session_get (peer_ct->c_s_index, 0); |
| 79 | } |
| 80 | |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 81 | void |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 82 | ct_session_endpoint (session_t * ll, session_endpoint_t * sep) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 83 | { |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 84 | ct_connection_t *ct; |
| 85 | ct = (ct_connection_t *) session_get_transport (ll); |
| 86 | sep->transport_proto = ct->actual_tp; |
| 87 | sep->port = ct->c_lcl_port; |
| 88 | sep->is_ip4 = ct->c_is_ip4; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 89 | } |
| 90 | |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 91 | int |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 92 | ct_session_connect_notify (session_t * ss) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 93 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 94 | ct_connection_t *sct, *cct; |
| 95 | app_worker_t *client_wrk; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 96 | segment_manager_t *sm; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 97 | fifo_segment_t *seg; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 98 | u64 segment_handle; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 99 | int is_fail = 0; |
| 100 | session_t *cs; |
| 101 | u32 ss_index; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 102 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 103 | ss_index = ss->session_index; |
| 104 | sct = (ct_connection_t *) session_get_transport (ss); |
| 105 | client_wrk = app_worker_get (sct->client_wrk); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 106 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 107 | sm = segment_manager_get (ss->rx_fifo->segment_manager); |
| 108 | seg = segment_manager_get_segment_w_lock (sm, ss->rx_fifo->segment_index); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 109 | segment_handle = segment_manager_segment_handle (sm, seg); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 110 | |
| 111 | if (app_worker_add_segment_notify (client_wrk, segment_handle)) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 112 | { |
| 113 | clib_warning ("failed to notify client %u of new segment", |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 114 | sct->client_wrk); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 115 | segment_manager_segment_reader_unlock (sm); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 116 | session_close (ss); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 117 | is_fail = 1; |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | segment_manager_segment_reader_unlock (sm); |
| 122 | } |
| 123 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 124 | /* Alloc client session */ |
| 125 | cct = ct_connection_get (sct->peer_index); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 126 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 127 | cs = session_alloc (0); |
| 128 | ss = session_get (ss_index, 0); |
| 129 | cs->session_type = ss->session_type; |
| 130 | cs->connection_index = sct->c_c_index; |
Nathan Skrzypczak | 2f0f96b | 2019-06-13 10:14:28 +0200 | [diff] [blame] | 131 | cs->listener_handle = SESSION_INVALID_HANDLE; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 132 | cs->session_state = SESSION_STATE_CONNECTING; |
| 133 | cs->app_wrk_index = client_wrk->wrk_index; |
| 134 | cs->connection_index = cct->c_c_index; |
| 135 | |
| 136 | cct->c_s_index = cs->session_index; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 137 | cct->client_rx_fifo = ss->tx_fifo; |
| 138 | cct->client_tx_fifo = ss->rx_fifo; |
| 139 | |
| 140 | cct->client_rx_fifo->refcnt++; |
| 141 | cct->client_tx_fifo->refcnt++; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 142 | |
| 143 | /* This will allocate fifos for the session. They won't be used for |
| 144 | * exchanging data but they will be used to close the connection if |
| 145 | * the segment manager/worker is freed */ |
| 146 | if (app_worker_init_connected (client_wrk, cs)) |
| 147 | { |
| 148 | session_close (ss); |
| 149 | return -1; |
| 150 | } |
| 151 | |
| 152 | if (app_worker_connect_notify (client_wrk, is_fail ? 0 : cs, |
| 153 | sct->client_opaque)) |
| 154 | { |
| 155 | session_close (ss); |
| 156 | return -1; |
| 157 | } |
| 158 | |
| 159 | cs = session_get (cct->c_s_index, 0); |
| 160 | cs->session_state = SESSION_STATE_READY; |
| 161 | |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 162 | return 0; |
| 163 | } |
| 164 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 165 | static int |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 166 | ct_init_local_session (app_worker_t * client_wrk, app_worker_t * server_wrk, |
| 167 | ct_connection_t * ct, session_t * ls, session_t * ll) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 168 | { |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 169 | u32 round_rx_fifo_sz, round_tx_fifo_sz, sm_index, seg_size; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 170 | segment_manager_props_t *props; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 171 | application_t *server; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 172 | segment_manager_t *sm; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 173 | u32 margin = 16 << 10; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 174 | fifo_segment_t *seg; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 175 | u64 segment_handle; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 176 | int seg_index, rv; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 177 | |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 178 | server = application_get (server_wrk->app_index); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 179 | |
| 180 | props = application_segment_manager_properties (server); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 181 | round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size); |
| 182 | round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 183 | seg_size = round_rx_fifo_sz + round_tx_fifo_sz + margin; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 184 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 185 | sm = app_worker_get_listen_segment_manager (server_wrk, ll); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 186 | seg_index = segment_manager_add_segment (sm, seg_size); |
| 187 | if (seg_index < 0) |
| 188 | { |
| 189 | clib_warning ("failed to add new cut-through segment"); |
| 190 | return seg_index; |
| 191 | } |
| 192 | seg = segment_manager_get_segment_w_lock (sm, seg_index); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 193 | |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 194 | rv = segment_manager_try_alloc_fifos (seg, props->rx_fifo_size, |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 195 | props->tx_fifo_size, &ls->rx_fifo, |
| 196 | &ls->tx_fifo); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 197 | if (rv) |
| 198 | { |
| 199 | clib_warning ("failed to add fifos in cut-through segment"); |
| 200 | segment_manager_segment_reader_unlock (sm); |
| 201 | goto failed; |
| 202 | } |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 203 | |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 204 | sm_index = segment_manager_index (sm); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 205 | ls->rx_fifo->master_session_index = ls->session_index; |
| 206 | ls->tx_fifo->master_session_index = ls->session_index; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 207 | ls->rx_fifo->segment_manager = sm_index; |
| 208 | ls->tx_fifo->segment_manager = sm_index; |
| 209 | ls->rx_fifo->segment_index = seg_index; |
| 210 | ls->tx_fifo->segment_index = seg_index; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 211 | |
| 212 | segment_handle = segment_manager_segment_handle (sm, seg); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 213 | if ((rv = app_worker_add_segment_notify (server_wrk, segment_handle))) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 214 | { |
| 215 | clib_warning ("failed to notify server of new segment"); |
| 216 | segment_manager_segment_reader_unlock (sm); |
| 217 | goto failed; |
| 218 | } |
| 219 | segment_manager_segment_reader_unlock (sm); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 220 | ct->segment_handle = segment_handle; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 221 | |
| 222 | return 0; |
| 223 | |
| 224 | failed: |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 225 | segment_manager_del_segment (sm, seg); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 226 | return rv; |
| 227 | } |
| 228 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 229 | static int |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 230 | ct_connect (app_worker_t * client_wrk, session_t * ll, |
| 231 | session_endpoint_cfg_t * sep) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 232 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 233 | u32 cct_index, ll_index, ll_ct_index; |
| 234 | ct_connection_t *sct, *cct, *ll_ct; |
| 235 | app_worker_t *server_wrk; |
| 236 | session_t *ss; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 237 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 238 | ll_index = ll->session_index; |
| 239 | ll_ct_index = ll->connection_index; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 240 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 241 | cct = ct_connection_alloc (); |
| 242 | cct_index = cct->c_c_index; |
| 243 | sct = ct_connection_alloc (); |
| 244 | ll_ct = ct_connection_get (ll_ct_index); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 245 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 246 | /* |
| 247 | * Alloc and init client transport |
| 248 | */ |
| 249 | cct = ct_connection_get (cct_index); |
| 250 | cct->c_thread_index = 0; |
| 251 | cct->c_rmt_port = sep->port; |
| 252 | cct->c_lcl_port = 0; |
| 253 | cct->c_is_ip4 = sep->is_ip4; |
| 254 | clib_memcpy (&cct->c_rmt_ip, &sep->ip, sizeof (sep->ip)); |
| 255 | cct->actual_tp = ll_ct->actual_tp; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 256 | cct->is_client = 1; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 257 | |
| 258 | /* |
| 259 | * Init server transport |
| 260 | */ |
| 261 | sct->c_thread_index = 0; |
| 262 | sct->c_rmt_port = 0; |
| 263 | sct->c_lcl_port = ll_ct->c_lcl_port; |
| 264 | sct->c_is_ip4 = sep->is_ip4; |
| 265 | clib_memcpy (&sct->c_lcl_ip, &ll_ct->c_lcl_ip, sizeof (ll_ct->c_lcl_ip)); |
| 266 | sct->client_wrk = client_wrk->wrk_index; |
| 267 | sct->c_proto = TRANSPORT_PROTO_NONE; |
| 268 | sct->client_opaque = sep->opaque; |
| 269 | sct->actual_tp = ll_ct->actual_tp; |
| 270 | |
| 271 | sct->peer_index = cct->c_c_index; |
| 272 | cct->peer_index = sct->c_c_index; |
| 273 | |
| 274 | /* |
| 275 | * Accept server session. Client session is created only after |
| 276 | * server confirms accept. |
| 277 | */ |
| 278 | ss = session_alloc (0); |
| 279 | ll = listen_session_get (ll_index); |
Florin Coras | 9f1a543 | 2019-03-10 21:03:20 -0700 | [diff] [blame] | 280 | ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, |
| 281 | sct->c_is_ip4); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 282 | ss->connection_index = sct->c_c_index; |
Nathan Skrzypczak | 2f0f96b | 2019-06-13 10:14:28 +0200 | [diff] [blame] | 283 | ss->listener_handle = listen_session_get_handle (ll); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 284 | ss->session_state = SESSION_STATE_CREATED; |
| 285 | |
| 286 | server_wrk = application_listener_select_worker (ll); |
| 287 | ss->app_wrk_index = server_wrk->wrk_index; |
| 288 | |
| 289 | sct->c_s_index = ss->session_index; |
| 290 | sct->server_wrk = ss->app_wrk_index; |
| 291 | |
| 292 | if (ct_init_local_session (client_wrk, server_wrk, sct, ss, ll)) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 293 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 294 | clib_warning ("failed"); |
| 295 | ct_connection_free (sct); |
| 296 | session_free (ss); |
| 297 | return -1; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 298 | } |
| 299 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 300 | ss->session_state = SESSION_STATE_ACCEPTING; |
| 301 | if (app_worker_accept_notify (server_wrk, ss)) |
| 302 | { |
| 303 | clib_warning ("failed"); |
| 304 | ct_connection_free (sct); |
| 305 | session_free_w_fifos (ss); |
| 306 | return -1; |
| 307 | } |
| 308 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 309 | cct->segment_handle = sct->segment_handle; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 310 | ct_enable_disable_main_pre_input_node (1 /* is_add */ ); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 311 | return 0; |
| 312 | } |
| 313 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 314 | static u32 |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 315 | ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep) |
| 316 | { |
| 317 | session_endpoint_cfg_t *sep; |
| 318 | ct_connection_t *ct; |
| 319 | |
| 320 | sep = (session_endpoint_cfg_t *) tep; |
| 321 | ct = ct_connection_alloc (); |
| 322 | ct->server_wrk = sep->app_wrk_index; |
| 323 | ct->c_is_ip4 = sep->is_ip4; |
| 324 | clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip)); |
| 325 | ct->c_lcl_port = sep->port; |
| 326 | ct->actual_tp = sep->transport_proto; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 327 | ct_enable_disable_main_pre_input_node (1 /* is_add */ ); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 328 | return ct->c_c_index; |
| 329 | } |
| 330 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 331 | static u32 |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 332 | ct_stop_listen (u32 ct_index) |
| 333 | { |
| 334 | ct_connection_t *ct; |
| 335 | ct = ct_connection_get (ct_index); |
| 336 | ct_connection_free (ct); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 337 | ct_enable_disable_main_pre_input_node (0 /* is_add */ ); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 338 | return 0; |
| 339 | } |
| 340 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 341 | static transport_connection_t * |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 342 | ct_listener_get (u32 ct_index) |
| 343 | { |
| 344 | return (transport_connection_t *) ct_connection_get (ct_index); |
| 345 | } |
| 346 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 347 | static int |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 348 | ct_session_connect (transport_endpoint_cfg_t * tep) |
| 349 | { |
| 350 | session_endpoint_cfg_t *sep_ext; |
| 351 | session_endpoint_t *sep; |
| 352 | app_worker_t *app_wrk; |
| 353 | session_handle_t lh; |
| 354 | application_t *app; |
| 355 | app_listener_t *al; |
| 356 | u32 table_index; |
| 357 | session_t *ll; |
| 358 | u8 fib_proto; |
| 359 | |
| 360 | sep_ext = (session_endpoint_cfg_t *) tep; |
| 361 | sep = (session_endpoint_t *) tep; |
| 362 | app_wrk = app_worker_get (sep_ext->app_wrk_index); |
| 363 | app = application_get (app_wrk->app_index); |
| 364 | |
| 365 | sep->transport_proto = sep_ext->original_tp; |
| 366 | table_index = application_local_session_table (app); |
| 367 | lh = session_lookup_local_endpoint (table_index, sep); |
| 368 | if (lh == SESSION_DROP_HANDLE) |
| 369 | return VNET_API_ERROR_APP_CONNECT_FILTERED; |
| 370 | |
| 371 | if (lh == SESSION_INVALID_HANDLE) |
| 372 | goto global_scope; |
| 373 | |
| 374 | ll = listen_session_get_from_handle (lh); |
| 375 | al = app_listener_get_w_session (ll); |
| 376 | |
| 377 | /* |
| 378 | * Break loop if rule in local table points to connecting app. This |
| 379 | * can happen if client is a generic proxy. Route connect through |
| 380 | * global table instead. |
| 381 | */ |
| 382 | if (al->app_index == app->app_index) |
| 383 | goto global_scope; |
| 384 | |
| 385 | return ct_connect (app_wrk, ll, sep_ext); |
| 386 | |
| 387 | /* |
| 388 | * If nothing found, check the global scope for locally attached |
| 389 | * destinations. Make sure first that we're allowed to. |
| 390 | */ |
| 391 | |
| 392 | global_scope: |
| 393 | if (session_endpoint_is_local (sep)) |
| 394 | return VNET_API_ERROR_SESSION_CONNECT; |
| 395 | |
| 396 | if (!application_has_global_scope (app)) |
| 397 | return VNET_API_ERROR_APP_CONNECT_SCOPE; |
| 398 | |
| 399 | fib_proto = session_endpoint_fib_proto (sep); |
| 400 | table_index = application_session_table (app, fib_proto); |
Florin Coras | 9f1a543 | 2019-03-10 21:03:20 -0700 | [diff] [blame] | 401 | ll = session_lookup_listener_wildcard (table_index, sep); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 402 | |
| 403 | if (ll) |
| 404 | return ct_connect (app_wrk, ll, sep_ext); |
| 405 | |
| 406 | /* Failed to connect but no error */ |
| 407 | return 1; |
| 408 | } |
| 409 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 410 | static void |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 411 | ct_session_close (u32 ct_index, u32 thread_index) |
| 412 | { |
| 413 | ct_connection_t *ct, *peer_ct; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 414 | app_worker_t *app_wrk; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 415 | session_t *s; |
| 416 | |
| 417 | ct = ct_connection_get (ct_index); |
| 418 | peer_ct = ct_connection_get (ct->peer_index); |
| 419 | if (peer_ct) |
| 420 | { |
| 421 | peer_ct->peer_index = ~0; |
| 422 | session_transport_closing_notify (&peer_ct->connection); |
| 423 | } |
| 424 | |
| 425 | s = session_get (ct->c_s_index, 0); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 426 | app_wrk = app_worker_get_if_valid (s->app_wrk_index); |
| 427 | if (app_wrk) |
| 428 | app_worker_del_segment_notify (app_wrk, ct->segment_handle); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 429 | session_free_w_fifos (s); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 430 | if (ct->is_client) |
| 431 | segment_manager_dealloc_fifos (ct->client_rx_fifo, ct->client_tx_fifo); |
| 432 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 433 | ct_connection_free (ct); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 434 | ct_enable_disable_main_pre_input_node (0 /* is_add */ ); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 435 | } |
| 436 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 437 | static transport_connection_t * |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 438 | ct_session_get (u32 ct_index, u32 thread_index) |
| 439 | { |
| 440 | return (transport_connection_t *) ct_connection_get (ct_index); |
| 441 | } |
| 442 | |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 443 | static u8 * |
| 444 | format_ct_connection_id (u8 * s, va_list * args) |
| 445 | { |
| 446 | ct_connection_t *ct = va_arg (*args, ct_connection_t *); |
| 447 | if (!ct) |
| 448 | return s; |
| 449 | if (ct->c_is_ip4) |
| 450 | { |
| 451 | s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index, |
| 452 | ct->c_s_index, format_transport_proto_short, ct->actual_tp, |
| 453 | format_ip4_address, &ct->c_lcl_ip4, |
| 454 | clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address, |
| 455 | &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port)); |
| 456 | } |
| 457 | else |
| 458 | { |
| 459 | s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index, |
| 460 | ct->c_s_index, format_transport_proto_short, ct->actual_tp, |
| 461 | format_ip6_address, &ct->c_lcl_ip6, |
| 462 | clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address, |
| 463 | &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port)); |
| 464 | } |
| 465 | |
| 466 | return s; |
| 467 | } |
| 468 | |
Florin Coras | f940f8a | 2019-03-06 10:44:38 -0800 | [diff] [blame] | 469 | static int |
| 470 | ct_custom_tx (void *session) |
| 471 | { |
| 472 | session_t *s = (session_t *) session; |
| 473 | if (session_has_transport (s)) |
| 474 | return 0; |
| 475 | return ct_session_tx (s); |
| 476 | } |
| 477 | |
Florin Coras | 317b8e0 | 2019-04-17 09:57:46 -0700 | [diff] [blame] | 478 | static int |
| 479 | ct_app_rx_evt (transport_connection_t * tc) |
| 480 | { |
| 481 | ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct; |
| 482 | session_t *ps; |
| 483 | |
| 484 | peer_ct = ct_connection_get (ct->peer_index); |
| 485 | if (!peer_ct) |
| 486 | return -1; |
| 487 | ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index); |
| 488 | return session_dequeue_notify (ps); |
| 489 | } |
| 490 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 491 | static u8 * |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 492 | format_ct_listener (u8 * s, va_list * args) |
| 493 | { |
| 494 | u32 tc_index = va_arg (*args, u32); |
Aloys Augustin | a0abbff | 2019-07-12 12:16:16 +0200 | [diff] [blame] | 495 | u32 __clib_unused thread_index = va_arg (*args, u32); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 496 | u32 __clib_unused verbose = va_arg (*args, u32); |
| 497 | ct_connection_t *ct = ct_connection_get (tc_index); |
| 498 | s = format (s, "%-50U", format_ct_connection_id, ct); |
| 499 | if (verbose) |
| 500 | s = format (s, "%-15s", "LISTEN"); |
| 501 | return s; |
| 502 | } |
| 503 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 504 | static u8 * |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 505 | format_ct_connection (u8 * s, va_list * args) |
| 506 | { |
| 507 | ct_connection_t *ct = va_arg (*args, ct_connection_t *); |
| 508 | u32 verbose = va_arg (*args, u32); |
| 509 | |
| 510 | if (!ct) |
| 511 | return s; |
| 512 | s = format (s, "%-50U", format_ct_connection_id, ct); |
| 513 | if (verbose) |
| 514 | { |
| 515 | s = format (s, "%-15s", "ESTABLISHED"); |
| 516 | if (verbose > 1) |
| 517 | { |
| 518 | s = format (s, "\n"); |
| 519 | } |
| 520 | } |
| 521 | return s; |
| 522 | } |
| 523 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 524 | static u8 * |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 525 | format_ct_session (u8 * s, va_list * args) |
| 526 | { |
| 527 | u32 ct_index = va_arg (*args, u32); |
| 528 | u32 __clib_unused thread_index = va_arg (*args, u32); |
| 529 | u32 verbose = va_arg (*args, u32); |
| 530 | ct_connection_t *ct; |
| 531 | |
| 532 | ct = ct_connection_get (ct_index); |
| 533 | if (!ct) |
| 534 | { |
| 535 | s = format (s, "empty\n"); |
| 536 | return s; |
| 537 | } |
| 538 | |
| 539 | s = format (s, "%U", format_ct_connection, ct, verbose); |
| 540 | return s; |
| 541 | } |
| 542 | |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 543 | /* *INDENT-OFF* */ |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 544 | static const transport_proto_vft_t cut_thru_proto = { |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 545 | .start_listen = ct_start_listen, |
| 546 | .stop_listen = ct_stop_listen, |
| 547 | .get_listener = ct_listener_get, |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 548 | .connect = ct_session_connect, |
| 549 | .close = ct_session_close, |
| 550 | .get_connection = ct_session_get, |
Florin Coras | f940f8a | 2019-03-06 10:44:38 -0800 | [diff] [blame] | 551 | .custom_tx = ct_custom_tx, |
Florin Coras | 317b8e0 | 2019-04-17 09:57:46 -0700 | [diff] [blame] | 552 | .app_rx_evt = ct_app_rx_evt, |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 553 | .format_listener = format_ct_listener, |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 554 | .format_connection = format_ct_session, |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 555 | .transport_options = { |
| 556 | .tx_type = TRANSPORT_TX_INTERNAL, |
| 557 | .service_type = TRANSPORT_SERVICE_APP, |
| 558 | }, |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 559 | }; |
| 560 | /* *INDENT-ON* */ |
| 561 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 562 | int |
| 563 | ct_session_tx (session_t * s) |
| 564 | { |
| 565 | ct_connection_t *ct, *peer_ct; |
| 566 | session_t *peer_s; |
| 567 | |
| 568 | ct = (ct_connection_t *) session_get_transport (s); |
| 569 | peer_ct = ct_connection_get (ct->peer_index); |
| 570 | if (!peer_ct) |
| 571 | return -1; |
| 572 | peer_s = session_get (peer_ct->c_s_index, 0); |
| 573 | if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING) |
| 574 | return 0; |
| 575 | return session_enqueue_notify (peer_s); |
| 576 | } |
| 577 | |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 578 | static clib_error_t * |
| 579 | ct_transport_init (vlib_main_t * vm) |
| 580 | { |
| 581 | transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto, |
| 582 | FIB_PROTOCOL_IP4, ~0); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 583 | transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto, |
| 584 | FIB_PROTOCOL_IP6, ~0); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 585 | return 0; |
| 586 | } |
| 587 | |
| 588 | VLIB_INIT_FUNCTION (ct_transport_init); |
| 589 | |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 590 | /* |
| 591 | * fd.io coding-style-patch-verification: ON |
| 592 | * |
| 593 | * Local Variables: |
| 594 | * eval: (c-set-style "gnu") |
| 595 | * End: |
| 596 | */ |