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