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 | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 19 | typedef struct ct_main_ |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 20 | { |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 21 | ct_connection_t **connections; /**< Per-worker connection pools */ |
| 22 | u32 n_workers; /**< Number of vpp workers */ |
| 23 | u32 n_sessions; /**< Cumulative sessions counter */ |
| 24 | } ct_main_t; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 25 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 26 | static ct_main_t ct_main; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 27 | |
| 28 | static ct_connection_t * |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 29 | ct_connection_alloc (u32 thread_index) |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 30 | { |
| 31 | ct_connection_t *ct; |
| 32 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 33 | pool_get_zero (ct_main.connections[thread_index], ct); |
| 34 | ct->c_c_index = ct - ct_main.connections[thread_index]; |
| 35 | ct->c_thread_index = thread_index; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 36 | ct->client_wrk = ~0; |
| 37 | ct->server_wrk = ~0; |
| 38 | return ct; |
| 39 | } |
| 40 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 41 | static ct_connection_t * |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 42 | ct_connection_get (u32 ct_index, u32 thread_index) |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 43 | { |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 44 | if (pool_is_free_index (ct_main.connections[thread_index], ct_index)) |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 45 | return 0; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 46 | return pool_elt_at_index (ct_main.connections[thread_index], ct_index); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 49 | static void |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 50 | ct_connection_free (ct_connection_t * ct) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 51 | { |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 52 | if (CLIB_DEBUG) |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 53 | { |
| 54 | u32 thread_index = ct->c_thread_index; |
| 55 | memset (ct, 0xfc, sizeof (*ct)); |
| 56 | pool_put (ct_main.connections[thread_index], ct); |
| 57 | return; |
| 58 | } |
| 59 | pool_put (ct_main.connections[ct->c_thread_index], ct); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 60 | } |
| 61 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 62 | session_t * |
| 63 | ct_session_get_peer (session_t * s) |
| 64 | { |
| 65 | ct_connection_t *ct, *peer_ct; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 66 | ct = ct_connection_get (s->connection_index, s->thread_index); |
| 67 | peer_ct = ct_connection_get (ct->peer_index, s->thread_index); |
| 68 | return session_get (peer_ct->c_s_index, s->thread_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 69 | } |
| 70 | |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 71 | void |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 72 | ct_session_endpoint (session_t * ll, session_endpoint_t * sep) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 73 | { |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 74 | ct_connection_t *ct; |
| 75 | ct = (ct_connection_t *) session_get_transport (ll); |
| 76 | sep->transport_proto = ct->actual_tp; |
| 77 | sep->port = ct->c_lcl_port; |
| 78 | sep->is_ip4 = ct->c_is_ip4; |
Florin Coras | ea7e708 | 2020-07-07 17:57:28 -0700 | [diff] [blame] | 79 | ip_copy (&sep->ip, &ct->c_lcl_ip, ct->c_is_ip4); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 80 | } |
| 81 | |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 82 | int |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 83 | ct_session_connect_notify (session_t * ss) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 84 | { |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 85 | u32 ss_index, opaque, thread_index; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 86 | ct_connection_t *sct, *cct; |
| 87 | app_worker_t *client_wrk; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 88 | segment_manager_t *sm; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 89 | fifo_segment_t *seg; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 90 | u64 segment_handle; |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 91 | int err = 0; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 92 | session_t *cs; |
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 | ss_index = ss->session_index; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 95 | thread_index = ss->thread_index; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 96 | sct = (ct_connection_t *) session_get_transport (ss); |
| 97 | client_wrk = app_worker_get (sct->client_wrk); |
Florin Coras | dd7d24b | 2020-08-14 17:51:13 -0700 | [diff] [blame] | 98 | opaque = sct->client_opaque; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 99 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 100 | sm = segment_manager_get (ss->rx_fifo->segment_manager); |
| 101 | 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] | 102 | segment_handle = segment_manager_segment_handle (sm, seg); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 103 | |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 104 | if ((err = app_worker_add_segment_notify (client_wrk, segment_handle))) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 105 | { |
| 106 | clib_warning ("failed to notify client %u of new segment", |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 107 | sct->client_wrk); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 108 | segment_manager_segment_reader_unlock (sm); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 109 | session_close (ss); |
Florin Coras | dd7d24b | 2020-08-14 17:51:13 -0700 | [diff] [blame] | 110 | goto error; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 111 | } |
| 112 | else |
| 113 | { |
| 114 | segment_manager_segment_reader_unlock (sm); |
| 115 | } |
| 116 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 117 | /* Alloc client session */ |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 118 | cct = ct_connection_get (sct->peer_index, thread_index); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 119 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 120 | cs = session_alloc (thread_index); |
| 121 | ss = session_get (ss_index, thread_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 122 | cs->session_type = ss->session_type; |
Nathan Skrzypczak | 2f0f96b | 2019-06-13 10:14:28 +0200 | [diff] [blame] | 123 | cs->listener_handle = SESSION_INVALID_HANDLE; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 124 | cs->session_state = SESSION_STATE_CONNECTING; |
| 125 | cs->app_wrk_index = client_wrk->wrk_index; |
| 126 | cs->connection_index = cct->c_c_index; |
| 127 | |
| 128 | cct->c_s_index = cs->session_index; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 129 | cct->client_rx_fifo = ss->tx_fifo; |
| 130 | cct->client_tx_fifo = ss->rx_fifo; |
| 131 | |
| 132 | cct->client_rx_fifo->refcnt++; |
| 133 | cct->client_tx_fifo->refcnt++; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 134 | |
| 135 | /* This will allocate fifos for the session. They won't be used for |
| 136 | * exchanging data but they will be used to close the connection if |
| 137 | * the segment manager/worker is freed */ |
Florin Coras | dd7d24b | 2020-08-14 17:51:13 -0700 | [diff] [blame] | 138 | if ((err = app_worker_init_connected (client_wrk, cs))) |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 139 | { |
| 140 | session_close (ss); |
Florin Coras | dd7d24b | 2020-08-14 17:51:13 -0700 | [diff] [blame] | 141 | session_free (cs); |
| 142 | goto error; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 143 | } |
| 144 | |
Florin Coras | dd7d24b | 2020-08-14 17:51:13 -0700 | [diff] [blame] | 145 | cs->session_state = SESSION_STATE_CONNECTING; |
| 146 | |
| 147 | if (app_worker_connect_notify (client_wrk, cs, err, opaque)) |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 148 | { |
| 149 | session_close (ss); |
Florin Coras | dd7d24b | 2020-08-14 17:51:13 -0700 | [diff] [blame] | 150 | segment_manager_dealloc_fifos (cs->rx_fifo, cs->tx_fifo); |
| 151 | session_free (cs); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 152 | return -1; |
| 153 | } |
| 154 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 155 | cs = session_get (cct->c_s_index, cct->c_thread_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 156 | cs->session_state = SESSION_STATE_READY; |
| 157 | |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 158 | return 0; |
Florin Coras | dd7d24b | 2020-08-14 17:51:13 -0700 | [diff] [blame] | 159 | |
| 160 | error: |
| 161 | app_worker_connect_notify (client_wrk, 0, err, opaque); |
| 162 | return -1; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 165 | static int |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 166 | ct_init_accepted_session (app_worker_t * server_wrk, |
| 167 | ct_connection_t * ct, session_t * ls, |
| 168 | session_t * ll) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 169 | { |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 170 | 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] | 171 | segment_manager_props_t *props; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 172 | application_t *server; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 173 | segment_manager_t *sm; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 174 | u32 margin = 16 << 10; |
Florin Coras | 88001c6 | 2019-04-24 14:44:46 -0700 | [diff] [blame] | 175 | fifo_segment_t *seg; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 176 | u64 segment_handle; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 177 | int seg_index, rv; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 178 | |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 179 | server = application_get (server_wrk->app_index); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 180 | |
| 181 | props = application_segment_manager_properties (server); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 182 | round_rx_fifo_sz = 1 << max_log2 (props->rx_fifo_size); |
| 183 | round_tx_fifo_sz = 1 << max_log2 (props->tx_fifo_size); |
Florin Coras | f22f4e5 | 2019-12-19 16:10:58 -0800 | [diff] [blame] | 184 | /* Increase size because of inefficient chunk allocations. Depending on |
| 185 | * how data is consumed, it may happen that more chunks than needed are |
| 186 | * allocated. |
| 187 | * TODO should remove once allocations are done more efficiently */ |
| 188 | seg_size = 4 * (round_rx_fifo_sz + round_tx_fifo_sz + margin); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 189 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 190 | sm = app_worker_get_listen_segment_manager (server_wrk, ll); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 191 | seg_index = segment_manager_add_segment (sm, seg_size); |
| 192 | if (seg_index < 0) |
| 193 | { |
| 194 | clib_warning ("failed to add new cut-through segment"); |
| 195 | return seg_index; |
| 196 | } |
| 197 | seg = segment_manager_get_segment_w_lock (sm, seg_index); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 198 | |
Florin Coras | 62ddc03 | 2019-12-08 18:30:42 -0800 | [diff] [blame] | 199 | rv = segment_manager_try_alloc_fifos (seg, ls->thread_index, |
| 200 | props->rx_fifo_size, |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 201 | props->tx_fifo_size, &ls->rx_fifo, |
| 202 | &ls->tx_fifo); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 203 | if (rv) |
| 204 | { |
| 205 | clib_warning ("failed to add fifos in cut-through segment"); |
| 206 | segment_manager_segment_reader_unlock (sm); |
| 207 | goto failed; |
| 208 | } |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 209 | |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 210 | sm_index = segment_manager_index (sm); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 211 | ls->rx_fifo->master_session_index = ls->session_index; |
| 212 | ls->tx_fifo->master_session_index = ls->session_index; |
Florin Coras | ce252ca | 2020-11-04 13:08:35 -0800 | [diff] [blame] | 213 | ls->rx_fifo->master_thread_index = ls->thread_index; |
| 214 | ls->tx_fifo->master_thread_index = ls->thread_index; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 215 | ls->rx_fifo->segment_manager = sm_index; |
| 216 | ls->tx_fifo->segment_manager = sm_index; |
| 217 | ls->rx_fifo->segment_index = seg_index; |
| 218 | ls->tx_fifo->segment_index = seg_index; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 219 | |
Florin Coras | 97d39e3 | 2020-09-04 08:57:27 -0700 | [diff] [blame] | 220 | /* Disable ooo lookups on the cut-through fifos. TODO remove once init of |
| 221 | * chunk lookup rbtrees is delegated to transports */ |
| 222 | svm_fifo_free_chunk_lookup (ls->tx_fifo); |
| 223 | |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 224 | segment_handle = segment_manager_segment_handle (sm, seg); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 225 | if ((rv = app_worker_add_segment_notify (server_wrk, segment_handle))) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 226 | { |
| 227 | clib_warning ("failed to notify server of new segment"); |
| 228 | segment_manager_segment_reader_unlock (sm); |
| 229 | goto failed; |
| 230 | } |
| 231 | segment_manager_segment_reader_unlock (sm); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 232 | ct->segment_handle = segment_handle; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 233 | |
| 234 | return 0; |
| 235 | |
| 236 | failed: |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 237 | segment_manager_lock_and_del_segment (sm, seg_index); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 238 | return rv; |
| 239 | } |
| 240 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 241 | typedef struct ct_accept_rpc_args |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 242 | { |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 243 | u32 ll_s_index; |
| 244 | u32 thread_index; |
| 245 | ip46_address_t ip; |
| 246 | u16 port; |
| 247 | u8 is_ip4; |
| 248 | u32 opaque; |
| 249 | u32 client_wrk_index; |
| 250 | } ct_accept_rpc_args_t; |
| 251 | |
| 252 | static void |
| 253 | ct_accept_rpc_wrk_handler (void *accept_args) |
| 254 | { |
| 255 | ct_accept_rpc_args_t *args = (ct_accept_rpc_args_t *) accept_args; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 256 | ct_connection_t *sct, *cct, *ll_ct; |
| 257 | app_worker_t *server_wrk; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 258 | session_t *ss, *ll; |
| 259 | u32 cct_index; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 260 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 261 | ll = listen_session_get (args->ll_s_index); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 262 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 263 | cct = ct_connection_alloc (args->thread_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 264 | cct_index = cct->c_c_index; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 265 | sct = ct_connection_alloc (args->thread_index); |
| 266 | ll_ct = ct_connection_get (ll->connection_index, 0 /* listener thread */ ); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 267 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 268 | /* |
| 269 | * Alloc and init client transport |
| 270 | */ |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 271 | cct = ct_connection_get (cct_index, args->thread_index); |
| 272 | cct->c_rmt_port = args->port; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 273 | cct->c_lcl_port = 0; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 274 | cct->c_is_ip4 = args->is_ip4; |
| 275 | clib_memcpy (&cct->c_rmt_ip, &args->ip, sizeof (args->ip)); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 276 | cct->actual_tp = ll_ct->actual_tp; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 277 | cct->is_client = 1; |
Florin Coras | 06ac4bb | 2020-10-28 16:41:26 -0700 | [diff] [blame] | 278 | cct->c_s_index = ~0; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 279 | |
| 280 | /* |
| 281 | * Init server transport |
| 282 | */ |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 283 | sct->c_rmt_port = 0; |
| 284 | sct->c_lcl_port = ll_ct->c_lcl_port; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 285 | sct->c_is_ip4 = args->is_ip4; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 286 | clib_memcpy (&sct->c_lcl_ip, &ll_ct->c_lcl_ip, sizeof (ll_ct->c_lcl_ip)); |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 287 | sct->client_wrk = args->client_wrk_index; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 288 | sct->c_proto = TRANSPORT_PROTO_NONE; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 289 | sct->client_opaque = args->opaque; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 290 | sct->actual_tp = ll_ct->actual_tp; |
| 291 | |
| 292 | sct->peer_index = cct->c_c_index; |
| 293 | cct->peer_index = sct->c_c_index; |
| 294 | |
| 295 | /* |
| 296 | * Accept server session. Client session is created only after |
| 297 | * server confirms accept. |
| 298 | */ |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 299 | ss = session_alloc (args->thread_index); |
| 300 | ll = listen_session_get (args->ll_s_index); |
Florin Coras | 9f1a543 | 2019-03-10 21:03:20 -0700 | [diff] [blame] | 301 | ss->session_type = session_type_from_proto_and_ip (TRANSPORT_PROTO_NONE, |
| 302 | sct->c_is_ip4); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 303 | ss->connection_index = sct->c_c_index; |
Nathan Skrzypczak | 2f0f96b | 2019-06-13 10:14:28 +0200 | [diff] [blame] | 304 | ss->listener_handle = listen_session_get_handle (ll); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 305 | ss->session_state = SESSION_STATE_CREATED; |
| 306 | |
| 307 | server_wrk = application_listener_select_worker (ll); |
| 308 | ss->app_wrk_index = server_wrk->wrk_index; |
| 309 | |
| 310 | sct->c_s_index = ss->session_index; |
| 311 | sct->server_wrk = ss->app_wrk_index; |
| 312 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 313 | if (ct_init_accepted_session (server_wrk, sct, ss, ll)) |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 314 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 315 | ct_connection_free (sct); |
| 316 | session_free (ss); |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 317 | return; |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 320 | ss->session_state = SESSION_STATE_ACCEPTING; |
| 321 | if (app_worker_accept_notify (server_wrk, ss)) |
| 322 | { |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 323 | ct_connection_free (sct); |
Florin Coras | 12813d5 | 2020-04-09 20:59:20 +0000 | [diff] [blame] | 324 | segment_manager_dealloc_fifos (ss->rx_fifo, ss->tx_fifo); |
| 325 | session_free (ss); |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 326 | return; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 327 | } |
| 328 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 329 | cct->segment_handle = sct->segment_handle; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 330 | |
| 331 | clib_mem_free (args); |
| 332 | } |
| 333 | |
| 334 | static int |
| 335 | ct_connect (app_worker_t * client_wrk, session_t * ll, |
| 336 | session_endpoint_cfg_t * sep) |
| 337 | { |
| 338 | ct_accept_rpc_args_t *args; |
| 339 | ct_main_t *cm = &ct_main; |
| 340 | u32 thread_index; |
| 341 | |
| 342 | /* Simple round-robin policy for spreading sessions over workers. We skip |
| 343 | * thread index 0, i.e., offset the index by 1, when we have workers as it |
| 344 | * is the one dedicated to main thread. Note that n_workers does not include |
| 345 | * main thread */ |
| 346 | cm->n_sessions += 1; |
| 347 | thread_index = cm->n_workers ? (cm->n_sessions % cm->n_workers) + 1 : 0; |
| 348 | |
| 349 | args = clib_mem_alloc (sizeof (*args)); |
| 350 | args->ll_s_index = ll->session_index; |
| 351 | args->thread_index = thread_index; |
| 352 | clib_memcpy_fast (&args->ip, &sep->ip, sizeof (ip46_address_t)); |
| 353 | args->port = sep->port; |
| 354 | args->is_ip4 = sep->is_ip4; |
| 355 | args->opaque = sep->opaque; |
| 356 | args->client_wrk_index = client_wrk->wrk_index; |
| 357 | |
| 358 | session_send_rpc_evt_to_thread (thread_index, ct_accept_rpc_wrk_handler, |
| 359 | args); |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 360 | return 0; |
| 361 | } |
| 362 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 363 | static u32 |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 364 | ct_start_listen (u32 app_listener_index, transport_endpoint_t * tep) |
| 365 | { |
| 366 | session_endpoint_cfg_t *sep; |
| 367 | ct_connection_t *ct; |
| 368 | |
| 369 | sep = (session_endpoint_cfg_t *) tep; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 370 | ct = ct_connection_alloc (0); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 371 | ct->server_wrk = sep->app_wrk_index; |
| 372 | ct->c_is_ip4 = sep->is_ip4; |
| 373 | clib_memcpy (&ct->c_lcl_ip, &sep->ip, sizeof (sep->ip)); |
| 374 | ct->c_lcl_port = sep->port; |
Florin Coras | a837508 | 2020-10-25 19:06:57 -0700 | [diff] [blame] | 375 | ct->c_s_index = app_listener_index; |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 376 | ct->actual_tp = sep->transport_proto; |
| 377 | return ct->c_c_index; |
| 378 | } |
| 379 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 380 | static u32 |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 381 | ct_stop_listen (u32 ct_index) |
| 382 | { |
| 383 | ct_connection_t *ct; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 384 | ct = ct_connection_get (ct_index, 0); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 385 | ct_connection_free (ct); |
| 386 | return 0; |
| 387 | } |
| 388 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 389 | static transport_connection_t * |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 390 | ct_listener_get (u32 ct_index) |
| 391 | { |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 392 | return (transport_connection_t *) ct_connection_get (ct_index, 0); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 393 | } |
| 394 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 395 | static int |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 396 | ct_session_connect (transport_endpoint_cfg_t * tep) |
| 397 | { |
| 398 | session_endpoint_cfg_t *sep_ext; |
| 399 | session_endpoint_t *sep; |
| 400 | app_worker_t *app_wrk; |
| 401 | session_handle_t lh; |
| 402 | application_t *app; |
| 403 | app_listener_t *al; |
| 404 | u32 table_index; |
| 405 | session_t *ll; |
| 406 | u8 fib_proto; |
| 407 | |
| 408 | sep_ext = (session_endpoint_cfg_t *) tep; |
| 409 | sep = (session_endpoint_t *) tep; |
| 410 | app_wrk = app_worker_get (sep_ext->app_wrk_index); |
| 411 | app = application_get (app_wrk->app_index); |
| 412 | |
| 413 | sep->transport_proto = sep_ext->original_tp; |
| 414 | table_index = application_local_session_table (app); |
| 415 | lh = session_lookup_local_endpoint (table_index, sep); |
| 416 | if (lh == SESSION_DROP_HANDLE) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 417 | return SESSION_E_FILTERED; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 418 | |
| 419 | if (lh == SESSION_INVALID_HANDLE) |
| 420 | goto global_scope; |
| 421 | |
| 422 | ll = listen_session_get_from_handle (lh); |
| 423 | al = app_listener_get_w_session (ll); |
| 424 | |
| 425 | /* |
| 426 | * Break loop if rule in local table points to connecting app. This |
| 427 | * can happen if client is a generic proxy. Route connect through |
| 428 | * global table instead. |
| 429 | */ |
| 430 | if (al->app_index == app->app_index) |
| 431 | goto global_scope; |
| 432 | |
| 433 | return ct_connect (app_wrk, ll, sep_ext); |
| 434 | |
| 435 | /* |
| 436 | * If nothing found, check the global scope for locally attached |
| 437 | * destinations. Make sure first that we're allowed to. |
| 438 | */ |
| 439 | |
| 440 | global_scope: |
| 441 | if (session_endpoint_is_local (sep)) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 442 | return SESSION_E_NOROUTE; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 443 | |
| 444 | if (!application_has_global_scope (app)) |
Florin Coras | 00e01d3 | 2019-10-21 16:07:46 -0700 | [diff] [blame] | 445 | return SESSION_E_SCOPE; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 446 | |
| 447 | fib_proto = session_endpoint_fib_proto (sep); |
Florin Coras | 95cd864 | 2019-12-30 21:53:19 -0800 | [diff] [blame] | 448 | 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] | 449 | ll = session_lookup_listener_wildcard (table_index, sep); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 450 | |
| 451 | if (ll) |
| 452 | return ct_connect (app_wrk, ll, sep_ext); |
| 453 | |
| 454 | /* Failed to connect but no error */ |
| 455 | return 1; |
| 456 | } |
| 457 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 458 | static void |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 459 | ct_session_close (u32 ct_index, u32 thread_index) |
| 460 | { |
| 461 | ct_connection_t *ct, *peer_ct; |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 462 | app_worker_t *app_wrk; |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 463 | session_t *s; |
| 464 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 465 | ct = ct_connection_get (ct_index, thread_index); |
| 466 | peer_ct = ct_connection_get (ct->peer_index, thread_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 467 | if (peer_ct) |
| 468 | { |
| 469 | peer_ct->peer_index = ~0; |
Florin Coras | 06ac4bb | 2020-10-28 16:41:26 -0700 | [diff] [blame] | 470 | /* Make sure session was allocated */ |
| 471 | if (peer_ct->c_s_index != ~0) |
| 472 | session_transport_closing_notify (&peer_ct->connection); |
| 473 | else |
| 474 | ct_connection_free (peer_ct); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 475 | } |
| 476 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 477 | s = session_get (ct->c_s_index, ct->c_thread_index); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 478 | app_wrk = app_worker_get_if_valid (s->app_wrk_index); |
| 479 | if (app_wrk) |
| 480 | app_worker_del_segment_notify (app_wrk, ct->segment_handle); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 481 | session_free_w_fifos (s); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 482 | if (ct->is_client) |
| 483 | segment_manager_dealloc_fifos (ct->client_rx_fifo, ct->client_tx_fifo); |
| 484 | |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 485 | ct_connection_free (ct); |
| 486 | } |
| 487 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 488 | static transport_connection_t * |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 489 | ct_session_get (u32 ct_index, u32 thread_index) |
| 490 | { |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 491 | return (transport_connection_t *) ct_connection_get (ct_index, |
| 492 | thread_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 493 | } |
| 494 | |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 495 | static u8 * |
| 496 | format_ct_connection_id (u8 * s, va_list * args) |
| 497 | { |
| 498 | ct_connection_t *ct = va_arg (*args, ct_connection_t *); |
| 499 | if (!ct) |
| 500 | return s; |
| 501 | if (ct->c_is_ip4) |
| 502 | { |
| 503 | s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index, |
| 504 | ct->c_s_index, format_transport_proto_short, ct->actual_tp, |
| 505 | format_ip4_address, &ct->c_lcl_ip4, |
| 506 | clib_net_to_host_u16 (ct->c_lcl_port), format_ip4_address, |
| 507 | &ct->c_rmt_ip4, clib_net_to_host_u16 (ct->c_rmt_port)); |
| 508 | } |
| 509 | else |
| 510 | { |
| 511 | s = format (s, "[%d:%d][CT:%U] %U:%d->%U:%d", ct->c_thread_index, |
| 512 | ct->c_s_index, format_transport_proto_short, ct->actual_tp, |
| 513 | format_ip6_address, &ct->c_lcl_ip6, |
| 514 | clib_net_to_host_u16 (ct->c_lcl_port), format_ip6_address, |
| 515 | &ct->c_rmt_ip6, clib_net_to_host_u16 (ct->c_rmt_port)); |
| 516 | } |
| 517 | |
| 518 | return s; |
| 519 | } |
| 520 | |
Florin Coras | f940f8a | 2019-03-06 10:44:38 -0800 | [diff] [blame] | 521 | static int |
Florin Coras | 9f86d22 | 2020-03-23 15:34:22 +0000 | [diff] [blame] | 522 | ct_custom_tx (void *session, transport_send_params_t * sp) |
Florin Coras | f940f8a | 2019-03-06 10:44:38 -0800 | [diff] [blame] | 523 | { |
| 524 | session_t *s = (session_t *) session; |
| 525 | if (session_has_transport (s)) |
| 526 | return 0; |
Florin Coras | d689456 | 2020-10-28 00:37:15 -0700 | [diff] [blame] | 527 | /* If event enqueued towards peer, remove from scheduler and |
| 528 | * remove session tx flag, i.e., accept new tx events */ |
| 529 | if (!ct_session_tx (s)) |
| 530 | { |
| 531 | sp->flags = TRANSPORT_SND_F_DESCHED; |
| 532 | svm_fifo_unset_event (s->tx_fifo); |
| 533 | } |
| 534 | /* The scheduler uses packet count as a means of upper bounding the amount |
| 535 | * of work done per dispatch. So make it look like we have sent something */ |
| 536 | return 1; |
Florin Coras | f940f8a | 2019-03-06 10:44:38 -0800 | [diff] [blame] | 537 | } |
| 538 | |
Florin Coras | 317b8e0 | 2019-04-17 09:57:46 -0700 | [diff] [blame] | 539 | static int |
| 540 | ct_app_rx_evt (transport_connection_t * tc) |
| 541 | { |
| 542 | ct_connection_t *ct = (ct_connection_t *) tc, *peer_ct; |
| 543 | session_t *ps; |
| 544 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 545 | peer_ct = ct_connection_get (ct->peer_index, tc->thread_index); |
Florin Coras | 317b8e0 | 2019-04-17 09:57:46 -0700 | [diff] [blame] | 546 | if (!peer_ct) |
| 547 | return -1; |
| 548 | ps = session_get (peer_ct->c_s_index, peer_ct->c_thread_index); |
| 549 | return session_dequeue_notify (ps); |
| 550 | } |
| 551 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 552 | static u8 * |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 553 | format_ct_listener (u8 * s, va_list * args) |
| 554 | { |
| 555 | u32 tc_index = va_arg (*args, u32); |
Aloys Augustin | a0abbff | 2019-07-12 12:16:16 +0200 | [diff] [blame] | 556 | u32 __clib_unused thread_index = va_arg (*args, u32); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 557 | u32 __clib_unused verbose = va_arg (*args, u32); |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 558 | ct_connection_t *ct = ct_connection_get (tc_index, 0); |
Florin Coras | 7bf6ed6 | 2020-09-23 12:02:08 -0700 | [diff] [blame] | 559 | s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 560 | if (verbose) |
Florin Coras | 7bf6ed6 | 2020-09-23 12:02:08 -0700 | [diff] [blame] | 561 | s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "LISTEN"); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 562 | return s; |
| 563 | } |
| 564 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 565 | static u8 * |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 566 | format_ct_connection (u8 * s, va_list * args) |
| 567 | { |
| 568 | ct_connection_t *ct = va_arg (*args, ct_connection_t *); |
| 569 | u32 verbose = va_arg (*args, u32); |
| 570 | |
| 571 | if (!ct) |
| 572 | return s; |
Florin Coras | 7bf6ed6 | 2020-09-23 12:02:08 -0700 | [diff] [blame] | 573 | s = format (s, "%-" SESSION_CLI_ID_LEN "U", format_ct_connection_id, ct); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 574 | if (verbose) |
| 575 | { |
Florin Coras | 7bf6ed6 | 2020-09-23 12:02:08 -0700 | [diff] [blame] | 576 | s = format (s, "%-" SESSION_CLI_STATE_LEN "s", "ESTABLISHED"); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 577 | if (verbose > 1) |
| 578 | { |
| 579 | s = format (s, "\n"); |
| 580 | } |
| 581 | } |
| 582 | return s; |
| 583 | } |
| 584 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 585 | static u8 * |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 586 | format_ct_session (u8 * s, va_list * args) |
| 587 | { |
| 588 | u32 ct_index = va_arg (*args, u32); |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 589 | u32 thread_index = va_arg (*args, u32); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 590 | u32 verbose = va_arg (*args, u32); |
| 591 | ct_connection_t *ct; |
| 592 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 593 | ct = ct_connection_get (ct_index, thread_index); |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 594 | if (!ct) |
| 595 | { |
| 596 | s = format (s, "empty\n"); |
| 597 | return s; |
| 598 | } |
| 599 | |
| 600 | s = format (s, "%U", format_ct_connection, ct, verbose); |
| 601 | return s; |
| 602 | } |
| 603 | |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 604 | clib_error_t * |
| 605 | ct_enable_disable (vlib_main_t * vm, u8 is_en) |
| 606 | { |
| 607 | ct_main.n_workers = vlib_num_workers (); |
| 608 | vec_validate (ct_main.connections, ct_main.n_workers); |
| 609 | return 0; |
| 610 | } |
| 611 | |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 612 | /* *INDENT-OFF* */ |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 613 | static const transport_proto_vft_t cut_thru_proto = { |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 614 | .enable = ct_enable_disable, |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 615 | .start_listen = ct_start_listen, |
| 616 | .stop_listen = ct_stop_listen, |
| 617 | .get_listener = ct_listener_get, |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 618 | .connect = ct_session_connect, |
| 619 | .close = ct_session_close, |
| 620 | .get_connection = ct_session_get, |
Florin Coras | f940f8a | 2019-03-06 10:44:38 -0800 | [diff] [blame] | 621 | .custom_tx = ct_custom_tx, |
Florin Coras | 317b8e0 | 2019-04-17 09:57:46 -0700 | [diff] [blame] | 622 | .app_rx_evt = ct_app_rx_evt, |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 623 | .format_listener = format_ct_listener, |
Florin Coras | 2b81e3c | 2019-02-27 07:55:46 -0800 | [diff] [blame] | 624 | .format_connection = format_ct_session, |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 625 | .transport_options = { |
Florin Coras | 07063b8 | 2020-03-13 04:44:51 +0000 | [diff] [blame] | 626 | .name = "ct", |
| 627 | .short_name = "C", |
Nathan Skrzypczak | e971bc9 | 2019-06-19 13:42:37 +0200 | [diff] [blame] | 628 | .tx_type = TRANSPORT_TX_INTERNAL, |
| 629 | .service_type = TRANSPORT_SERVICE_APP, |
| 630 | }, |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 631 | }; |
| 632 | /* *INDENT-ON* */ |
| 633 | |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 634 | int |
| 635 | ct_session_tx (session_t * s) |
| 636 | { |
| 637 | ct_connection_t *ct, *peer_ct; |
| 638 | session_t *peer_s; |
| 639 | |
| 640 | ct = (ct_connection_t *) session_get_transport (s); |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 641 | peer_ct = ct_connection_get (ct->peer_index, ct->c_thread_index); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 642 | if (!peer_ct) |
Florin Coras | bcdc50d | 2020-08-25 19:07:02 -0700 | [diff] [blame] | 643 | return 0; |
Florin Coras | 2d0e3de | 2020-10-23 16:31:40 -0700 | [diff] [blame] | 644 | peer_s = session_get (peer_ct->c_s_index, peer_ct->c_thread_index); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 645 | if (peer_s->session_state >= SESSION_STATE_TRANSPORT_CLOSING) |
| 646 | return 0; |
Florin Coras | d689456 | 2020-10-28 00:37:15 -0700 | [diff] [blame] | 647 | return session_enqueue_notify (peer_s); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 648 | } |
| 649 | |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 650 | static clib_error_t * |
| 651 | ct_transport_init (vlib_main_t * vm) |
| 652 | { |
| 653 | transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto, |
| 654 | FIB_PROTOCOL_IP4, ~0); |
Florin Coras | 653e43f | 2019-03-04 10:56:23 -0800 | [diff] [blame] | 655 | transport_register_protocol (TRANSPORT_PROTO_NONE, &cut_thru_proto, |
| 656 | FIB_PROTOCOL_IP6, ~0); |
Florin Coras | d4295e6 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 657 | return 0; |
| 658 | } |
| 659 | |
| 660 | VLIB_INIT_FUNCTION (ct_transport_init); |
| 661 | |
Florin Coras | ba7d8f5 | 2019-02-22 13:11:38 -0800 | [diff] [blame] | 662 | /* |
| 663 | * fd.io coding-style-patch-verification: ON |
| 664 | * |
| 665 | * Local Variables: |
| 666 | * eval: (c-set-style "gnu") |
| 667 | * End: |
| 668 | */ |