Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 16 | #include <math.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 17 | #include <vlib/vlib.h> |
| 18 | #include <vnet/vnet.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 19 | #include <vnet/tcp/tcp.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 20 | #include <vppinfra/elog.h> |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 21 | #include <vnet/session/application.h> |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 22 | #include <vnet/session/session_debug.h> |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 23 | #include <vlibmemory/unix_shared_memory_queue.h> |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 24 | |
| 25 | vlib_node_registration_t session_queue_node; |
| 26 | |
| 27 | typedef struct |
| 28 | { |
| 29 | u32 session_index; |
| 30 | u32 server_thread_index; |
| 31 | } session_queue_trace_t; |
| 32 | |
| 33 | /* packet trace format function */ |
| 34 | static u8 * |
| 35 | format_session_queue_trace (u8 * s, va_list * args) |
| 36 | { |
| 37 | CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *); |
| 38 | CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *); |
| 39 | session_queue_trace_t *t = va_arg (*args, session_queue_trace_t *); |
| 40 | |
| 41 | s = format (s, "SESSION_QUEUE: session index %d, server thread index %d", |
| 42 | t->session_index, t->server_thread_index); |
| 43 | return s; |
| 44 | } |
| 45 | |
| 46 | vlib_node_registration_t session_queue_node; |
| 47 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 48 | #define foreach_session_queue_error \ |
| 49 | _(TX, "Packets transmitted") \ |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 50 | _(TIMER, "Timer events") \ |
| 51 | _(NO_BUFFER, "Out of buffers") |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 52 | |
| 53 | typedef enum |
| 54 | { |
| 55 | #define _(sym,str) SESSION_QUEUE_ERROR_##sym, |
| 56 | foreach_session_queue_error |
| 57 | #undef _ |
| 58 | SESSION_QUEUE_N_ERROR, |
| 59 | } session_queue_error_t; |
| 60 | |
| 61 | static char *session_queue_error_strings[] = { |
| 62 | #define _(sym,string) string, |
| 63 | foreach_session_queue_error |
| 64 | #undef _ |
| 65 | }; |
| 66 | |
| 67 | static u32 session_type_to_next[] = { |
| 68 | SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT, |
| 69 | SESSION_QUEUE_NEXT_IP4_LOOKUP, |
| 70 | SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT, |
| 71 | SESSION_QUEUE_NEXT_IP6_LOOKUP, |
| 72 | }; |
| 73 | |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 74 | always_inline void |
| 75 | session_tx_fifo_chain_tail (session_manager_main_t * smm, vlib_main_t * vm, |
| 76 | u8 thread_index, svm_fifo_t * fifo, |
| 77 | vlib_buffer_t * b0, u32 bi0, u8 n_bufs_per_seg, |
| 78 | u32 * left_to_snd0, u16 * n_bufs, u32 * rx_offset, |
| 79 | u16 deq_per_buf, u8 peek_data) |
| 80 | { |
| 81 | vlib_buffer_t *chain_b0, *prev_b0; |
| 82 | u32 chain_bi0; |
| 83 | u16 len_to_deq0, n_bytes_read; |
| 84 | u8 *data0, j; |
| 85 | |
| 86 | chain_bi0 = bi0; |
| 87 | chain_b0 = b0; |
| 88 | for (j = 1; j < n_bufs_per_seg; j++) |
| 89 | { |
| 90 | prev_b0 = chain_b0; |
| 91 | len_to_deq0 = clib_min (*left_to_snd0, deq_per_buf); |
| 92 | |
| 93 | *n_bufs -= 1; |
| 94 | chain_bi0 = smm->tx_buffers[thread_index][*n_bufs]; |
| 95 | _vec_len (smm->tx_buffers[thread_index]) = *n_bufs; |
| 96 | |
| 97 | chain_b0 = vlib_get_buffer (vm, chain_bi0); |
| 98 | chain_b0->current_data = 0; |
| 99 | data0 = vlib_buffer_get_current (chain_b0); |
| 100 | if (peek_data) |
| 101 | { |
| 102 | n_bytes_read = svm_fifo_peek (fifo, *rx_offset, len_to_deq0, data0); |
| 103 | *rx_offset += n_bytes_read; |
| 104 | } |
| 105 | else |
| 106 | { |
| 107 | n_bytes_read = svm_fifo_dequeue_nowait (fifo, len_to_deq0, data0); |
| 108 | } |
| 109 | ASSERT (n_bytes_read == len_to_deq0); |
| 110 | chain_b0->current_length = n_bytes_read; |
| 111 | b0->total_length_not_including_first_buffer += chain_b0->current_length; |
| 112 | |
| 113 | /* update previous buffer */ |
| 114 | prev_b0->next_buffer = chain_bi0; |
| 115 | prev_b0->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 116 | |
| 117 | /* update current buffer */ |
| 118 | chain_b0->next_buffer = 0; |
| 119 | |
| 120 | *left_to_snd0 -= n_bytes_read; |
| 121 | if (*left_to_snd0 == 0) |
| 122 | break; |
| 123 | } |
| 124 | } |
| 125 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 126 | always_inline int |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 127 | session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 128 | session_manager_main_t * smm, |
| 129 | session_fifo_event_t * e0, |
| 130 | stream_session_t * s0, u32 thread_index, |
| 131 | int *n_tx_packets, u8 peek_data) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 132 | { |
| 133 | u32 n_trace = vlib_get_trace_count (vm, node); |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 134 | u32 left_to_snd0, max_len_to_snd0, len_to_deq0, snd_space0; |
| 135 | u32 n_bufs_per_evt, n_frames_per_evt; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 136 | transport_connection_t *tc0; |
| 137 | transport_proto_vft_t *transport_vft; |
| 138 | u32 next_index, next0, *to_next, n_left_to_next, bi0; |
| 139 | vlib_buffer_t *b0; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 140 | u32 rx_offset = 0, max_dequeue0, n_bytes_per_seg; |
| 141 | u16 snd_mss0, n_bufs_per_seg, n_bufs; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 142 | u8 *data0; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 143 | int i, n_bytes_read; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 144 | u32 n_bytes_per_buf, deq_per_buf; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 145 | u32 buffers_allocated, buffers_allocated_this_call; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 146 | |
| 147 | next_index = next0 = session_type_to_next[s0->session_type]; |
| 148 | |
| 149 | transport_vft = session_get_transport_vft (s0->session_type); |
| 150 | tc0 = transport_vft->get_connection (s0->connection_index, thread_index); |
| 151 | |
| 152 | /* Make sure we have space to send and there's something to dequeue */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 153 | snd_mss0 = transport_vft->send_mss (tc0); |
Florin Coras | c834341 | 2017-05-04 14:25:50 -0700 | [diff] [blame] | 154 | snd_space0 = transport_vft->send_space (tc0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 155 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 156 | /* Can't make any progress */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 157 | if (snd_space0 == 0 || snd_mss0 == 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 158 | { |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 159 | vec_add1 (smm->pending_event_vector[thread_index], *e0); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 160 | return 0; |
| 161 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 162 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 163 | if (peek_data) |
| 164 | { |
| 165 | /* Offset in rx fifo from where to peek data */ |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 166 | rx_offset = transport_vft->tx_fifo_offset (tc0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 167 | } |
| 168 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 169 | /* Check how much we can pull. If buffering, subtract the offset */ |
| 170 | max_dequeue0 = svm_fifo_max_dequeue (s0->server_tx_fifo) - rx_offset; |
| 171 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 172 | /* Nothing to read return */ |
| 173 | if (max_dequeue0 == 0) |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame^] | 174 | { |
| 175 | svm_fifo_unset_event (s0->server_tx_fifo); |
| 176 | return 0; |
| 177 | } |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 178 | |
| 179 | /* Ensure we're not writing more than transport window allows */ |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 180 | if (max_dequeue0 < snd_space0) |
| 181 | { |
| 182 | /* Constrained by tx queue. Try to send only fully formed segments */ |
| 183 | max_len_to_snd0 = (max_dequeue0 > snd_mss0) ? |
| 184 | max_dequeue0 - max_dequeue0 % snd_mss0 : max_dequeue0; |
| 185 | /* TODO Nagle ? */ |
| 186 | } |
| 187 | else |
| 188 | { |
| 189 | max_len_to_snd0 = snd_space0; |
| 190 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 191 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 192 | n_bytes_per_buf = vlib_buffer_free_list_buffer_size |
| 193 | (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 194 | n_bytes_per_seg = MAX_HDRS_LEN + snd_mss0; |
| 195 | n_bufs_per_seg = ceil ((double) n_bytes_per_seg / n_bytes_per_buf); |
| 196 | n_bufs_per_evt = (ceil ((double) max_len_to_snd0 / n_bytes_per_seg)) |
| 197 | * n_bufs_per_seg; |
| 198 | n_frames_per_evt = ceil ((double) n_bufs_per_evt / VLIB_FRAME_SIZE); |
| 199 | |
| 200 | deq_per_buf = clib_min (snd_mss0, n_bytes_per_buf); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 201 | |
| 202 | n_bufs = vec_len (smm->tx_buffers[thread_index]); |
| 203 | left_to_snd0 = max_len_to_snd0; |
| 204 | for (i = 0; i < n_frames_per_evt; i++) |
| 205 | { |
| 206 | /* Make sure we have at least one full frame of buffers ready */ |
| 207 | if (PREDICT_FALSE (n_bufs < VLIB_FRAME_SIZE)) |
| 208 | { |
| 209 | vec_validate (smm->tx_buffers[thread_index], |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 210 | n_bufs + 2 * VLIB_FRAME_SIZE - 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 211 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 212 | buffers_allocated = 0; |
| 213 | do |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 214 | { |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 215 | buffers_allocated_this_call = |
| 216 | vlib_buffer_alloc |
| 217 | (vm, |
| 218 | &smm->tx_buffers[thread_index][n_bufs + buffers_allocated], |
| 219 | 2 * VLIB_FRAME_SIZE - buffers_allocated); |
| 220 | buffers_allocated += buffers_allocated_this_call; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 221 | } |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 222 | while (buffers_allocated_this_call > 0 |
| 223 | && ((buffers_allocated + n_bufs < VLIB_FRAME_SIZE))); |
| 224 | |
| 225 | n_bufs += buffers_allocated; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 226 | |
| 227 | _vec_len (smm->tx_buffers[thread_index]) = n_bufs; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 228 | |
| 229 | if (PREDICT_FALSE (n_bufs < VLIB_FRAME_SIZE)) |
| 230 | { |
| 231 | vec_add1 (smm->pending_event_vector[thread_index], *e0); |
| 232 | return -1; |
| 233 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 234 | } |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 235 | /* Allow enqueuing of a new event */ |
| 236 | svm_fifo_unset_event (s0->server_tx_fifo); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 237 | |
| 238 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 239 | while (left_to_snd0 && n_left_to_next >= n_bufs_per_seg) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 240 | { |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 241 | /* |
| 242 | * Handle first buffer in chain separately |
| 243 | */ |
| 244 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 245 | /* Get free buffer */ |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 246 | ASSERT (n_bufs >= 1); |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 247 | bi0 = smm->tx_buffers[thread_index][--n_bufs]; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 248 | ASSERT (bi0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 249 | _vec_len (smm->tx_buffers[thread_index]) = n_bufs; |
| 250 | |
| 251 | b0 = vlib_get_buffer (vm, bi0); |
| 252 | b0->error = 0; |
| 253 | b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID |
| 254 | | VNET_BUFFER_LOCALLY_ORIGINATED; |
| 255 | b0->current_data = 0; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 256 | b0->total_length_not_including_first_buffer = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 257 | |
| 258 | /* RX on the local interface. tx in default fib */ |
| 259 | vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0; |
| 260 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
| 261 | |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 262 | len_to_deq0 = clib_min (left_to_snd0, deq_per_buf); |
| 263 | |
| 264 | data0 = vlib_buffer_make_headroom (b0, MAX_HDRS_LEN); |
| 265 | if (peek_data) |
| 266 | { |
| 267 | n_bytes_read = svm_fifo_peek (s0->server_tx_fifo, rx_offset, |
| 268 | len_to_deq0, data0); |
| 269 | /* Keep track of progress locally, transport is also supposed to |
| 270 | * increment it independently when pushing the header */ |
| 271 | rx_offset += n_bytes_read; |
| 272 | } |
| 273 | else |
| 274 | { |
| 275 | n_bytes_read = svm_fifo_dequeue_nowait (s0->server_tx_fifo, |
| 276 | len_to_deq0, data0); |
| 277 | } |
| 278 | |
| 279 | if (n_bytes_read <= 0) |
| 280 | goto dequeue_fail; |
| 281 | |
| 282 | b0->current_length = n_bytes_read; |
| 283 | |
| 284 | left_to_snd0 -= n_bytes_read; |
| 285 | *n_tx_packets = *n_tx_packets + 1; |
| 286 | |
| 287 | /* |
| 288 | * Fill in the remaining buffers in the chain, if any |
| 289 | */ |
| 290 | if (PREDICT_FALSE (n_bufs_per_seg > 1)) |
| 291 | session_tx_fifo_chain_tail (smm, vm, thread_index, |
| 292 | s0->server_tx_fifo, b0, bi0, |
| 293 | n_bufs_per_seg, &left_to_snd0, |
| 294 | &n_bufs, &rx_offset, deq_per_buf, |
| 295 | peek_data); |
| 296 | |
| 297 | /* Ask transport to push header after current_length and |
| 298 | * total_length_not_including_first_buffer are updated */ |
| 299 | transport_vft->push_header (tc0, b0); |
| 300 | |
| 301 | /* *INDENT-OFF* */ |
| 302 | SESSION_EVT_DBG(SESSION_EVT_DEQ, s0, ({ |
| 303 | ed->data[0] = e0->event_id; |
| 304 | ed->data[1] = max_dequeue0; |
| 305 | ed->data[2] = len_to_deq0; |
| 306 | ed->data[3] = left_to_snd0; |
| 307 | })); |
| 308 | /* *INDENT-ON* */ |
| 309 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 310 | /* usual speculation, or the enqueue_x1 macro will barf */ |
| 311 | to_next[0] = bi0; |
| 312 | to_next += 1; |
| 313 | n_left_to_next -= 1; |
| 314 | |
| 315 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0); |
| 316 | if (PREDICT_FALSE (n_trace > 0)) |
| 317 | { |
| 318 | session_queue_trace_t *t0; |
| 319 | vlib_trace_buffer (vm, node, next_index, b0, |
| 320 | 1 /* follow_chain */ ); |
| 321 | vlib_set_trace_count (vm, node, --n_trace); |
| 322 | t0 = vlib_add_trace (vm, node, b0, sizeof (*t0)); |
| 323 | t0->session_index = s0->session_index; |
| 324 | t0->server_thread_index = s0->thread_index; |
| 325 | } |
| 326 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 327 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 328 | to_next, n_left_to_next, |
| 329 | bi0, next0); |
| 330 | } |
| 331 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 332 | } |
| 333 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 334 | /* If we couldn't dequeue all bytes mark as partially read */ |
| 335 | if (max_len_to_snd0 < max_dequeue0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 336 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 337 | /* If we don't already have new event */ |
| 338 | if (svm_fifo_set_event (s0->server_tx_fifo)) |
| 339 | { |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 340 | vec_add1 (smm->pending_event_vector[thread_index], *e0); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 341 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 342 | } |
| 343 | return 0; |
| 344 | |
| 345 | dequeue_fail: |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 346 | /* |
| 347 | * Can't read from fifo. If we don't already have an event, save as partially |
| 348 | * read, return buff to free list and return |
| 349 | */ |
| 350 | clib_warning ("dequeue fail"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 351 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 352 | if (svm_fifo_set_event (s0->server_tx_fifo)) |
| 353 | { |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 354 | vec_add1 (smm->pending_event_vector[thread_index], *e0); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 355 | } |
| 356 | vlib_put_next_frame (vm, node, next_index, n_left_to_next + 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 357 | _vec_len (smm->tx_buffers[thread_index]) += 1; |
| 358 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | int |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 363 | session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 364 | session_manager_main_t * smm, |
| 365 | session_fifo_event_t * e0, |
| 366 | stream_session_t * s0, u32 thread_index, |
| 367 | int *n_tx_pkts) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 368 | { |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 369 | return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index, |
| 370 | n_tx_pkts, 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | int |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 374 | session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 375 | session_manager_main_t * smm, |
| 376 | session_fifo_event_t * e0, |
| 377 | stream_session_t * s0, u32 thread_index, |
| 378 | int *n_tx_pkts) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 379 | { |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 380 | return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index, |
| 381 | n_tx_pkts, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 382 | } |
| 383 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 384 | stream_session_t * |
| 385 | session_event_get_session (session_fifo_event_t * e0, u8 thread_index) |
| 386 | { |
| 387 | svm_fifo_t *f0; |
| 388 | stream_session_t *s0; |
| 389 | u32 session_index0; |
| 390 | |
| 391 | f0 = e0->fifo; |
| 392 | session_index0 = f0->master_session_index; |
| 393 | |
| 394 | /* $$$ add multiple event queues, per vpp worker thread */ |
| 395 | ASSERT (f0->master_thread_index == thread_index); |
| 396 | |
| 397 | s0 = stream_session_get_if_valid (session_index0, thread_index); |
| 398 | |
Florin Coras | f03a59a | 2017-06-09 21:07:32 -0700 | [diff] [blame^] | 399 | ASSERT (s0 == 0 || s0->thread_index == thread_index); |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 400 | |
| 401 | return s0; |
| 402 | } |
| 403 | |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 404 | void |
| 405 | dump_thread_0_event_queue (void) |
| 406 | { |
| 407 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
| 408 | vlib_main_t *vm = &vlib_global_main; |
| 409 | u32 my_thread_index = vm->thread_index; |
| 410 | session_fifo_event_t _e, *e = &_e; |
| 411 | stream_session_t *s0; |
| 412 | int i, index; |
| 413 | i8 *headp; |
| 414 | |
| 415 | unix_shared_memory_queue_t *q; |
| 416 | q = smm->vpp_event_queues[my_thread_index]; |
| 417 | |
| 418 | index = q->head; |
| 419 | |
| 420 | for (i = 0; i < q->cursize; i++) |
| 421 | { |
| 422 | headp = (i8 *) (&q->data[0] + q->elsize * index); |
| 423 | clib_memcpy (e, headp, q->elsize); |
| 424 | |
| 425 | switch (e->event_type) |
| 426 | { |
| 427 | case FIFO_EVENT_APP_TX: |
| 428 | s0 = session_event_get_session (e, my_thread_index); |
| 429 | fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index); |
| 430 | break; |
| 431 | |
| 432 | case FIFO_EVENT_DISCONNECT: |
| 433 | s0 = stream_session_get_from_handle (e->session_handle); |
| 434 | fformat (stdout, "[%04d] disconnect session %d\n", i, |
| 435 | s0->session_index); |
| 436 | break; |
| 437 | |
| 438 | case FIFO_EVENT_BUILTIN_RX: |
| 439 | s0 = session_event_get_session (e, my_thread_index); |
| 440 | fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index); |
| 441 | break; |
| 442 | |
| 443 | case FIFO_EVENT_RPC: |
| 444 | fformat (stdout, "[%04d] RPC call %llx with %llx\n", |
| 445 | i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg)); |
| 446 | break; |
| 447 | |
| 448 | default: |
| 449 | fformat (stdout, "[%04d] unhandled event type %d\n", |
| 450 | i, e->event_type); |
| 451 | break; |
| 452 | } |
| 453 | |
| 454 | index++; |
| 455 | |
| 456 | if (index == q->maxsize) |
| 457 | index = 0; |
| 458 | } |
| 459 | } |
| 460 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 461 | static uword |
| 462 | session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 463 | vlib_frame_t * frame) |
| 464 | { |
| 465 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 466 | session_fifo_event_t *my_pending_event_vector, *e; |
| 467 | session_fifo_event_t *my_fifo_events; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 468 | u32 n_to_dequeue, n_events; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 469 | unix_shared_memory_queue_t *q; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 470 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 471 | int n_tx_packets = 0; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 472 | u32 my_thread_index = vm->thread_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 473 | int i, rv; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 474 | f64 now = vlib_time_now (vm); |
Dave Barach | e5f1d27 | 2017-05-10 13:34:04 -0400 | [diff] [blame] | 475 | void (*fp) (void *); |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 476 | |
| 477 | SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, my_thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 478 | |
| 479 | /* |
| 480 | * Update TCP time |
| 481 | */ |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 482 | tcp_update_time (now, my_thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 483 | |
| 484 | /* |
| 485 | * Get vpp queue events |
| 486 | */ |
| 487 | q = smm->vpp_event_queues[my_thread_index]; |
| 488 | if (PREDICT_FALSE (q == 0)) |
| 489 | return 0; |
| 490 | |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 491 | my_fifo_events = smm->free_event_vector[my_thread_index]; |
| 492 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 493 | /* min number of events we can dequeue without blocking */ |
| 494 | n_to_dequeue = q->cursize; |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 495 | my_pending_event_vector = smm->pending_event_vector[my_thread_index]; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 496 | |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 497 | if (n_to_dequeue == 0 && vec_len (my_pending_event_vector) == 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 498 | return 0; |
| 499 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 500 | SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0); |
| 501 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 502 | /* |
| 503 | * If we didn't manage to process previous events try going |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 504 | * over them again without dequeuing new ones. |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 505 | */ |
| 506 | /* XXX: Block senders to sessions that can't keep up */ |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 507 | if (0 && vec_len (my_pending_event_vector) >= 100) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 508 | { |
| 509 | clib_warning ("too many fifo events unsolved"); |
| 510 | goto skip_dequeue; |
| 511 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 512 | |
| 513 | /* See you in the next life, don't be late */ |
| 514 | if (pthread_mutex_trylock (&q->mutex)) |
| 515 | return 0; |
| 516 | |
| 517 | for (i = 0; i < n_to_dequeue; i++) |
| 518 | { |
| 519 | vec_add2 (my_fifo_events, e, 1); |
| 520 | unix_shared_memory_queue_sub_raw (q, (u8 *) e); |
| 521 | } |
| 522 | |
| 523 | /* The other side of the connection is not polling */ |
| 524 | if (q->cursize < (q->maxsize / 8)) |
| 525 | (void) pthread_cond_broadcast (&q->condvar); |
| 526 | pthread_mutex_unlock (&q->mutex); |
| 527 | |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 528 | vec_append (my_fifo_events, my_pending_event_vector); |
| 529 | |
| 530 | _vec_len (my_pending_event_vector) = 0; |
| 531 | smm->pending_event_vector[my_thread_index] = my_pending_event_vector; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 532 | |
| 533 | skip_dequeue: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 534 | n_events = vec_len (my_fifo_events); |
| 535 | for (i = 0; i < n_events; i++) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 536 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 537 | stream_session_t *s0; /* $$$ prefetch 1 ahead maybe */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 538 | session_fifo_event_t *e0; |
| 539 | |
| 540 | e0 = &my_fifo_events[i]; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 541 | |
| 542 | switch (e0->event_type) |
| 543 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 544 | case FIFO_EVENT_APP_TX: |
| 545 | s0 = session_event_get_session (e0, my_thread_index); |
| 546 | |
| 547 | if (CLIB_DEBUG && !s0) |
| 548 | { |
| 549 | clib_warning ("It's dead, Jim!"); |
| 550 | continue; |
| 551 | } |
| 552 | |
| 553 | if (PREDICT_FALSE (s0->session_state == SESSION_STATE_CLOSED)) |
| 554 | continue; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 555 | /* Spray packets in per session type frames, since they go to |
| 556 | * different nodes */ |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 557 | rv = (smm->session_tx_fns[s0->session_type]) (vm, node, smm, e0, s0, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 558 | my_thread_index, |
| 559 | &n_tx_packets); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 560 | /* Out of buffers */ |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 561 | if (PREDICT_FALSE (rv < 0)) |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 562 | { |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 563 | vlib_node_increment_counter (vm, node->node_index, |
| 564 | SESSION_QUEUE_ERROR_NO_BUFFER, 1); |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 565 | continue; |
| 566 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 567 | break; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 568 | case FIFO_EVENT_DISCONNECT: |
| 569 | s0 = stream_session_get_from_handle (e0->session_handle); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 570 | stream_session_disconnect (s0); |
| 571 | break; |
| 572 | case FIFO_EVENT_BUILTIN_RX: |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 573 | s0 = session_event_get_session (e0, my_thread_index); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 574 | svm_fifo_unset_event (s0->server_rx_fifo); |
| 575 | /* Get session's server */ |
| 576 | app = application_get (s0->app_index); |
| 577 | app->cb_fns.builtin_server_rx_callback (s0); |
| 578 | break; |
Dave Barach | e5f1d27 | 2017-05-10 13:34:04 -0400 | [diff] [blame] | 579 | case FIFO_EVENT_RPC: |
| 580 | fp = e0->rpc_args.fp; |
| 581 | (*fp) (e0->rpc_args.arg); |
| 582 | break; |
| 583 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 584 | default: |
| 585 | clib_warning ("unhandled event type %d", e0->event_type); |
| 586 | } |
| 587 | } |
| 588 | |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 589 | _vec_len (my_fifo_events) = 0; |
| 590 | smm->free_event_vector[my_thread_index] = my_fifo_events; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 591 | |
| 592 | vlib_node_increment_counter (vm, session_queue_node.index, |
| 593 | SESSION_QUEUE_ERROR_TX, n_tx_packets); |
| 594 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 595 | SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 1); |
| 596 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 597 | return n_tx_packets; |
| 598 | } |
| 599 | |
| 600 | /* *INDENT-OFF* */ |
| 601 | VLIB_REGISTER_NODE (session_queue_node) = |
| 602 | { |
| 603 | .function = session_queue_node_fn, |
| 604 | .name = "session-queue", |
| 605 | .format_trace = format_session_queue_trace, |
| 606 | .type = VLIB_NODE_TYPE_INPUT, |
| 607 | .n_errors = ARRAY_LEN (session_queue_error_strings), |
| 608 | .error_strings = session_queue_error_strings, |
| 609 | .n_next_nodes = SESSION_QUEUE_N_NEXT, |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 610 | .state = VLIB_NODE_STATE_DISABLED, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 611 | .next_nodes = |
| 612 | { |
| 613 | [SESSION_QUEUE_NEXT_DROP] = "error-drop", |
| 614 | [SESSION_QUEUE_NEXT_IP4_LOOKUP] = "ip4-lookup", |
| 615 | [SESSION_QUEUE_NEXT_IP6_LOOKUP] = "ip6-lookup", |
| 616 | [SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT] = "tcp4-output", |
| 617 | [SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT] = "tcp6-output", |
| 618 | }, |
| 619 | }; |
| 620 | /* *INDENT-ON* */ |
| 621 | |
| 622 | /* |
| 623 | * fd.io coding-style-patch-verification: ON |
| 624 | * |
| 625 | * Local Variables: |
| 626 | * eval: (c-set-style "gnu") |
| 627 | * End: |
| 628 | */ |