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 | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 174 | return 0; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 175 | |
| 176 | /* Ensure we're not writing more than transport window allows */ |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 177 | if (max_dequeue0 < snd_space0) |
| 178 | { |
| 179 | /* Constrained by tx queue. Try to send only fully formed segments */ |
| 180 | max_len_to_snd0 = (max_dequeue0 > snd_mss0) ? |
| 181 | max_dequeue0 - max_dequeue0 % snd_mss0 : max_dequeue0; |
| 182 | /* TODO Nagle ? */ |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | max_len_to_snd0 = snd_space0; |
| 187 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 188 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 189 | n_bytes_per_buf = vlib_buffer_free_list_buffer_size |
| 190 | (vm, VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX); |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 191 | n_bytes_per_seg = MAX_HDRS_LEN + snd_mss0; |
| 192 | n_bufs_per_seg = ceil ((double) n_bytes_per_seg / n_bytes_per_buf); |
| 193 | n_bufs_per_evt = (ceil ((double) max_len_to_snd0 / n_bytes_per_seg)) |
| 194 | * n_bufs_per_seg; |
| 195 | n_frames_per_evt = ceil ((double) n_bufs_per_evt / VLIB_FRAME_SIZE); |
| 196 | |
| 197 | deq_per_buf = clib_min (snd_mss0, n_bytes_per_buf); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 198 | |
| 199 | n_bufs = vec_len (smm->tx_buffers[thread_index]); |
| 200 | left_to_snd0 = max_len_to_snd0; |
| 201 | for (i = 0; i < n_frames_per_evt; i++) |
| 202 | { |
| 203 | /* Make sure we have at least one full frame of buffers ready */ |
| 204 | if (PREDICT_FALSE (n_bufs < VLIB_FRAME_SIZE)) |
| 205 | { |
| 206 | vec_validate (smm->tx_buffers[thread_index], |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 207 | n_bufs + 2 * VLIB_FRAME_SIZE - 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 208 | |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 209 | buffers_allocated = 0; |
| 210 | do |
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_this_call = |
| 213 | vlib_buffer_alloc |
| 214 | (vm, |
| 215 | &smm->tx_buffers[thread_index][n_bufs + buffers_allocated], |
| 216 | 2 * VLIB_FRAME_SIZE - buffers_allocated); |
| 217 | buffers_allocated += buffers_allocated_this_call; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 218 | } |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 219 | while (buffers_allocated_this_call > 0 |
| 220 | && ((buffers_allocated + n_bufs < VLIB_FRAME_SIZE))); |
| 221 | |
| 222 | n_bufs += buffers_allocated; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 223 | |
| 224 | _vec_len (smm->tx_buffers[thread_index]) = n_bufs; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 225 | |
| 226 | if (PREDICT_FALSE (n_bufs < VLIB_FRAME_SIZE)) |
| 227 | { |
| 228 | vec_add1 (smm->pending_event_vector[thread_index], *e0); |
| 229 | return -1; |
| 230 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 231 | } |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 232 | /* Allow enqueuing of a new event */ |
| 233 | svm_fifo_unset_event (s0->server_tx_fifo); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 234 | |
| 235 | 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] | 236 | while (left_to_snd0 && n_left_to_next >= n_bufs_per_seg) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 237 | { |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 238 | /* |
| 239 | * Handle first buffer in chain separately |
| 240 | */ |
| 241 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 242 | /* Get free buffer */ |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 243 | ASSERT (n_bufs >= 1); |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 244 | bi0 = smm->tx_buffers[thread_index][--n_bufs]; |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 245 | ASSERT (bi0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 246 | _vec_len (smm->tx_buffers[thread_index]) = n_bufs; |
| 247 | |
| 248 | b0 = vlib_get_buffer (vm, bi0); |
| 249 | b0->error = 0; |
| 250 | b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID |
| 251 | | VNET_BUFFER_LOCALLY_ORIGINATED; |
| 252 | b0->current_data = 0; |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 253 | b0->total_length_not_including_first_buffer = 0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 254 | |
| 255 | /* RX on the local interface. tx in default fib */ |
| 256 | vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0; |
| 257 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
| 258 | |
Florin Coras | f6d68ed | 2017-05-07 19:12:02 -0700 | [diff] [blame] | 259 | len_to_deq0 = clib_min (left_to_snd0, deq_per_buf); |
| 260 | |
| 261 | data0 = vlib_buffer_make_headroom (b0, MAX_HDRS_LEN); |
| 262 | if (peek_data) |
| 263 | { |
| 264 | n_bytes_read = svm_fifo_peek (s0->server_tx_fifo, rx_offset, |
| 265 | len_to_deq0, data0); |
| 266 | /* Keep track of progress locally, transport is also supposed to |
| 267 | * increment it independently when pushing the header */ |
| 268 | rx_offset += n_bytes_read; |
| 269 | } |
| 270 | else |
| 271 | { |
| 272 | n_bytes_read = svm_fifo_dequeue_nowait (s0->server_tx_fifo, |
| 273 | len_to_deq0, data0); |
| 274 | } |
| 275 | |
| 276 | if (n_bytes_read <= 0) |
| 277 | goto dequeue_fail; |
| 278 | |
| 279 | b0->current_length = n_bytes_read; |
| 280 | |
| 281 | left_to_snd0 -= n_bytes_read; |
| 282 | *n_tx_packets = *n_tx_packets + 1; |
| 283 | |
| 284 | /* |
| 285 | * Fill in the remaining buffers in the chain, if any |
| 286 | */ |
| 287 | if (PREDICT_FALSE (n_bufs_per_seg > 1)) |
| 288 | session_tx_fifo_chain_tail (smm, vm, thread_index, |
| 289 | s0->server_tx_fifo, b0, bi0, |
| 290 | n_bufs_per_seg, &left_to_snd0, |
| 291 | &n_bufs, &rx_offset, deq_per_buf, |
| 292 | peek_data); |
| 293 | |
| 294 | /* Ask transport to push header after current_length and |
| 295 | * total_length_not_including_first_buffer are updated */ |
| 296 | transport_vft->push_header (tc0, b0); |
| 297 | |
| 298 | /* *INDENT-OFF* */ |
| 299 | SESSION_EVT_DBG(SESSION_EVT_DEQ, s0, ({ |
| 300 | ed->data[0] = e0->event_id; |
| 301 | ed->data[1] = max_dequeue0; |
| 302 | ed->data[2] = len_to_deq0; |
| 303 | ed->data[3] = left_to_snd0; |
| 304 | })); |
| 305 | /* *INDENT-ON* */ |
| 306 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 307 | /* usual speculation, or the enqueue_x1 macro will barf */ |
| 308 | to_next[0] = bi0; |
| 309 | to_next += 1; |
| 310 | n_left_to_next -= 1; |
| 311 | |
| 312 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0); |
| 313 | if (PREDICT_FALSE (n_trace > 0)) |
| 314 | { |
| 315 | session_queue_trace_t *t0; |
| 316 | vlib_trace_buffer (vm, node, next_index, b0, |
| 317 | 1 /* follow_chain */ ); |
| 318 | vlib_set_trace_count (vm, node, --n_trace); |
| 319 | t0 = vlib_add_trace (vm, node, b0, sizeof (*t0)); |
| 320 | t0->session_index = s0->session_index; |
| 321 | t0->server_thread_index = s0->thread_index; |
| 322 | } |
| 323 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 324 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 325 | to_next, n_left_to_next, |
| 326 | bi0, next0); |
| 327 | } |
| 328 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 329 | } |
| 330 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 331 | /* If we couldn't dequeue all bytes mark as partially read */ |
| 332 | if (max_len_to_snd0 < max_dequeue0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 333 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 334 | /* If we don't already have new event */ |
| 335 | if (svm_fifo_set_event (s0->server_tx_fifo)) |
| 336 | { |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 337 | vec_add1 (smm->pending_event_vector[thread_index], *e0); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 338 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 339 | } |
| 340 | return 0; |
| 341 | |
| 342 | dequeue_fail: |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 343 | /* |
| 344 | * Can't read from fifo. If we don't already have an event, save as partially |
| 345 | * read, return buff to free list and return |
| 346 | */ |
| 347 | clib_warning ("dequeue fail"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 348 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 349 | if (svm_fifo_set_event (s0->server_tx_fifo)) |
| 350 | { |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 351 | vec_add1 (smm->pending_event_vector[thread_index], *e0); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 352 | } |
| 353 | 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] | 354 | _vec_len (smm->tx_buffers[thread_index]) += 1; |
| 355 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 356 | return 0; |
| 357 | } |
| 358 | |
| 359 | int |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 360 | session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 361 | session_manager_main_t * smm, |
| 362 | session_fifo_event_t * e0, |
| 363 | stream_session_t * s0, u32 thread_index, |
| 364 | int *n_tx_pkts) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 365 | { |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 366 | return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index, |
| 367 | n_tx_pkts, 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | int |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 371 | session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 372 | session_manager_main_t * smm, |
| 373 | session_fifo_event_t * e0, |
| 374 | stream_session_t * s0, u32 thread_index, |
| 375 | int *n_tx_pkts) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 376 | { |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 377 | return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index, |
| 378 | n_tx_pkts, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 379 | } |
| 380 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 381 | stream_session_t * |
| 382 | session_event_get_session (session_fifo_event_t * e0, u8 thread_index) |
| 383 | { |
| 384 | svm_fifo_t *f0; |
| 385 | stream_session_t *s0; |
| 386 | u32 session_index0; |
| 387 | |
| 388 | f0 = e0->fifo; |
| 389 | session_index0 = f0->master_session_index; |
| 390 | |
| 391 | /* $$$ add multiple event queues, per vpp worker thread */ |
| 392 | ASSERT (f0->master_thread_index == thread_index); |
| 393 | |
| 394 | s0 = stream_session_get_if_valid (session_index0, thread_index); |
| 395 | |
| 396 | ASSERT (s0->thread_index == thread_index); |
| 397 | |
| 398 | return s0; |
| 399 | } |
| 400 | |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 401 | void |
| 402 | dump_thread_0_event_queue (void) |
| 403 | { |
| 404 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
| 405 | vlib_main_t *vm = &vlib_global_main; |
| 406 | u32 my_thread_index = vm->thread_index; |
| 407 | session_fifo_event_t _e, *e = &_e; |
| 408 | stream_session_t *s0; |
| 409 | int i, index; |
| 410 | i8 *headp; |
| 411 | |
| 412 | unix_shared_memory_queue_t *q; |
| 413 | q = smm->vpp_event_queues[my_thread_index]; |
| 414 | |
| 415 | index = q->head; |
| 416 | |
| 417 | for (i = 0; i < q->cursize; i++) |
| 418 | { |
| 419 | headp = (i8 *) (&q->data[0] + q->elsize * index); |
| 420 | clib_memcpy (e, headp, q->elsize); |
| 421 | |
| 422 | switch (e->event_type) |
| 423 | { |
| 424 | case FIFO_EVENT_APP_TX: |
| 425 | s0 = session_event_get_session (e, my_thread_index); |
| 426 | fformat (stdout, "[%04d] TX session %d\n", i, s0->session_index); |
| 427 | break; |
| 428 | |
| 429 | case FIFO_EVENT_DISCONNECT: |
| 430 | s0 = stream_session_get_from_handle (e->session_handle); |
| 431 | fformat (stdout, "[%04d] disconnect session %d\n", i, |
| 432 | s0->session_index); |
| 433 | break; |
| 434 | |
| 435 | case FIFO_EVENT_BUILTIN_RX: |
| 436 | s0 = session_event_get_session (e, my_thread_index); |
| 437 | fformat (stdout, "[%04d] builtin_rx %d\n", i, s0->session_index); |
| 438 | break; |
| 439 | |
| 440 | case FIFO_EVENT_RPC: |
| 441 | fformat (stdout, "[%04d] RPC call %llx with %llx\n", |
| 442 | i, (u64) (e->rpc_args.fp), (u64) (e->rpc_args.arg)); |
| 443 | break; |
| 444 | |
| 445 | default: |
| 446 | fformat (stdout, "[%04d] unhandled event type %d\n", |
| 447 | i, e->event_type); |
| 448 | break; |
| 449 | } |
| 450 | |
| 451 | index++; |
| 452 | |
| 453 | if (index == q->maxsize) |
| 454 | index = 0; |
| 455 | } |
| 456 | } |
| 457 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 458 | static uword |
| 459 | session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 460 | vlib_frame_t * frame) |
| 461 | { |
| 462 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 463 | session_fifo_event_t *my_pending_event_vector, *e; |
| 464 | session_fifo_event_t *my_fifo_events; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 465 | u32 n_to_dequeue, n_events; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 466 | unix_shared_memory_queue_t *q; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 467 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 468 | int n_tx_packets = 0; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 469 | u32 my_thread_index = vm->thread_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 470 | int i, rv; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 471 | f64 now = vlib_time_now (vm); |
Dave Barach | e5f1d27 | 2017-05-10 13:34:04 -0400 | [diff] [blame] | 472 | void (*fp) (void *); |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 473 | |
| 474 | SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, my_thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 475 | |
| 476 | /* |
| 477 | * Update TCP time |
| 478 | */ |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 479 | tcp_update_time (now, my_thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 480 | |
| 481 | /* |
| 482 | * Get vpp queue events |
| 483 | */ |
| 484 | q = smm->vpp_event_queues[my_thread_index]; |
| 485 | if (PREDICT_FALSE (q == 0)) |
| 486 | return 0; |
| 487 | |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 488 | my_fifo_events = smm->free_event_vector[my_thread_index]; |
| 489 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 490 | /* min number of events we can dequeue without blocking */ |
| 491 | n_to_dequeue = q->cursize; |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 492 | my_pending_event_vector = smm->pending_event_vector[my_thread_index]; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 493 | |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 494 | if (n_to_dequeue == 0 && vec_len (my_pending_event_vector) == 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 495 | return 0; |
| 496 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 497 | SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0); |
| 498 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 499 | /* |
| 500 | * If we didn't manage to process previous events try going |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 501 | * over them again without dequeuing new ones. |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 502 | */ |
| 503 | /* XXX: Block senders to sessions that can't keep up */ |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 504 | if (0 && vec_len (my_pending_event_vector) >= 100) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 505 | { |
| 506 | clib_warning ("too many fifo events unsolved"); |
| 507 | goto skip_dequeue; |
| 508 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 509 | |
| 510 | /* See you in the next life, don't be late */ |
| 511 | if (pthread_mutex_trylock (&q->mutex)) |
| 512 | return 0; |
| 513 | |
| 514 | for (i = 0; i < n_to_dequeue; i++) |
| 515 | { |
| 516 | vec_add2 (my_fifo_events, e, 1); |
| 517 | unix_shared_memory_queue_sub_raw (q, (u8 *) e); |
| 518 | } |
| 519 | |
| 520 | /* The other side of the connection is not polling */ |
| 521 | if (q->cursize < (q->maxsize / 8)) |
| 522 | (void) pthread_cond_broadcast (&q->condvar); |
| 523 | pthread_mutex_unlock (&q->mutex); |
| 524 | |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 525 | vec_append (my_fifo_events, my_pending_event_vector); |
| 526 | |
| 527 | _vec_len (my_pending_event_vector) = 0; |
| 528 | smm->pending_event_vector[my_thread_index] = my_pending_event_vector; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 529 | |
| 530 | skip_dequeue: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 531 | n_events = vec_len (my_fifo_events); |
| 532 | for (i = 0; i < n_events; i++) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 533 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 534 | stream_session_t *s0; /* $$$ prefetch 1 ahead maybe */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 535 | session_fifo_event_t *e0; |
| 536 | |
| 537 | e0 = &my_fifo_events[i]; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 538 | |
| 539 | switch (e0->event_type) |
| 540 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 541 | case FIFO_EVENT_APP_TX: |
| 542 | s0 = session_event_get_session (e0, my_thread_index); |
| 543 | |
| 544 | if (CLIB_DEBUG && !s0) |
| 545 | { |
| 546 | clib_warning ("It's dead, Jim!"); |
| 547 | continue; |
| 548 | } |
| 549 | |
| 550 | if (PREDICT_FALSE (s0->session_state == SESSION_STATE_CLOSED)) |
| 551 | continue; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 552 | /* Spray packets in per session type frames, since they go to |
| 553 | * different nodes */ |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 554 | 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] | 555 | my_thread_index, |
| 556 | &n_tx_packets); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 557 | /* Out of buffers */ |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 558 | if (PREDICT_FALSE (rv < 0)) |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 559 | { |
Florin Coras | 93992a9 | 2017-05-24 18:03:56 -0700 | [diff] [blame] | 560 | vlib_node_increment_counter (vm, node->node_index, |
| 561 | SESSION_QUEUE_ERROR_NO_BUFFER, 1); |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 562 | continue; |
| 563 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 564 | break; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 565 | case FIFO_EVENT_DISCONNECT: |
| 566 | s0 = stream_session_get_from_handle (e0->session_handle); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 567 | stream_session_disconnect (s0); |
| 568 | break; |
| 569 | case FIFO_EVENT_BUILTIN_RX: |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 570 | s0 = session_event_get_session (e0, my_thread_index); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 571 | svm_fifo_unset_event (s0->server_rx_fifo); |
| 572 | /* Get session's server */ |
| 573 | app = application_get (s0->app_index); |
| 574 | app->cb_fns.builtin_server_rx_callback (s0); |
| 575 | break; |
Dave Barach | e5f1d27 | 2017-05-10 13:34:04 -0400 | [diff] [blame] | 576 | case FIFO_EVENT_RPC: |
| 577 | fp = e0->rpc_args.fp; |
| 578 | (*fp) (e0->rpc_args.arg); |
| 579 | break; |
| 580 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 581 | default: |
| 582 | clib_warning ("unhandled event type %d", e0->event_type); |
| 583 | } |
| 584 | } |
| 585 | |
Dave Barach | acd2a6a | 2017-05-16 17:41:34 -0400 | [diff] [blame] | 586 | _vec_len (my_fifo_events) = 0; |
| 587 | smm->free_event_vector[my_thread_index] = my_fifo_events; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 588 | |
| 589 | vlib_node_increment_counter (vm, session_queue_node.index, |
| 590 | SESSION_QUEUE_ERROR_TX, n_tx_packets); |
| 591 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 592 | SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 1); |
| 593 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 594 | return n_tx_packets; |
| 595 | } |
| 596 | |
| 597 | /* *INDENT-OFF* */ |
| 598 | VLIB_REGISTER_NODE (session_queue_node) = |
| 599 | { |
| 600 | .function = session_queue_node_fn, |
| 601 | .name = "session-queue", |
| 602 | .format_trace = format_session_queue_trace, |
| 603 | .type = VLIB_NODE_TYPE_INPUT, |
| 604 | .n_errors = ARRAY_LEN (session_queue_error_strings), |
| 605 | .error_strings = session_queue_error_strings, |
| 606 | .n_next_nodes = SESSION_QUEUE_N_NEXT, |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 607 | .state = VLIB_NODE_STATE_DISABLED, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 608 | .next_nodes = |
| 609 | { |
| 610 | [SESSION_QUEUE_NEXT_DROP] = "error-drop", |
| 611 | [SESSION_QUEUE_NEXT_IP4_LOOKUP] = "ip4-lookup", |
| 612 | [SESSION_QUEUE_NEXT_IP6_LOOKUP] = "ip6-lookup", |
| 613 | [SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT] = "tcp4-output", |
| 614 | [SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT] = "tcp6-output", |
| 615 | }, |
| 616 | }; |
| 617 | /* *INDENT-ON* */ |
| 618 | |
| 619 | /* |
| 620 | * fd.io coding-style-patch-verification: ON |
| 621 | * |
| 622 | * Local Variables: |
| 623 | * eval: (c-set-style "gnu") |
| 624 | * End: |
| 625 | */ |