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") \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 50 | _(TIMER, "Timer events") |
| 51 | |
| 52 | typedef enum |
| 53 | { |
| 54 | #define _(sym,str) SESSION_QUEUE_ERROR_##sym, |
| 55 | foreach_session_queue_error |
| 56 | #undef _ |
| 57 | SESSION_QUEUE_N_ERROR, |
| 58 | } session_queue_error_t; |
| 59 | |
| 60 | static char *session_queue_error_strings[] = { |
| 61 | #define _(sym,string) string, |
| 62 | foreach_session_queue_error |
| 63 | #undef _ |
| 64 | }; |
| 65 | |
| 66 | static u32 session_type_to_next[] = { |
| 67 | SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT, |
| 68 | SESSION_QUEUE_NEXT_IP4_LOOKUP, |
| 69 | SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT, |
| 70 | SESSION_QUEUE_NEXT_IP6_LOOKUP, |
| 71 | }; |
| 72 | |
| 73 | always_inline int |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 74 | session_tx_fifo_read_and_snd_i (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 75 | session_manager_main_t * smm, |
| 76 | session_fifo_event_t * e0, |
| 77 | stream_session_t * s0, u32 thread_index, |
| 78 | int *n_tx_packets, u8 peek_data) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 79 | { |
| 80 | u32 n_trace = vlib_get_trace_count (vm, node); |
| 81 | u32 left_to_snd0, max_len_to_snd0, len_to_deq0, n_bufs, snd_space0; |
| 82 | u32 n_frame_bytes, n_frames_per_evt; |
| 83 | transport_connection_t *tc0; |
| 84 | transport_proto_vft_t *transport_vft; |
| 85 | u32 next_index, next0, *to_next, n_left_to_next, bi0; |
| 86 | vlib_buffer_t *b0; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 87 | u32 rx_offset = 0, max_dequeue0; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 88 | u16 snd_mss0; |
| 89 | u8 *data0; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 90 | int i, n_bytes_read; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 91 | |
| 92 | next_index = next0 = session_type_to_next[s0->session_type]; |
| 93 | |
| 94 | transport_vft = session_get_transport_vft (s0->session_type); |
| 95 | tc0 = transport_vft->get_connection (s0->connection_index, thread_index); |
| 96 | |
| 97 | /* Make sure we have space to send and there's something to dequeue */ |
| 98 | snd_space0 = transport_vft->send_space (tc0); |
| 99 | snd_mss0 = transport_vft->send_mss (tc0); |
| 100 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 101 | /* Can't make any progress */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 102 | if (snd_space0 == 0 || snd_mss0 == 0) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 103 | { |
| 104 | vec_add1 (smm->evts_partially_read[thread_index], *e0); |
| 105 | return 0; |
| 106 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 107 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 108 | if (peek_data) |
| 109 | { |
| 110 | /* Offset in rx fifo from where to peek data */ |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 111 | rx_offset = transport_vft->tx_fifo_offset (tc0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 114 | /* Check how much we can pull. If buffering, subtract the offset */ |
| 115 | max_dequeue0 = svm_fifo_max_dequeue (s0->server_tx_fifo) - rx_offset; |
| 116 | |
| 117 | /* Allow enqueuing of a new event */ |
| 118 | svm_fifo_unset_event (s0->server_tx_fifo); |
| 119 | |
| 120 | /* Nothing to read return */ |
| 121 | if (max_dequeue0 == 0) |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 122 | return 0; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 123 | |
| 124 | /* Ensure we're not writing more than transport window allows */ |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 125 | if (max_dequeue0 < snd_space0) |
| 126 | { |
| 127 | /* Constrained by tx queue. Try to send only fully formed segments */ |
| 128 | max_len_to_snd0 = (max_dequeue0 > snd_mss0) ? |
| 129 | max_dequeue0 - max_dequeue0 % snd_mss0 : max_dequeue0; |
| 130 | /* TODO Nagle ? */ |
| 131 | } |
| 132 | else |
| 133 | { |
| 134 | max_len_to_snd0 = snd_space0; |
| 135 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 136 | |
| 137 | n_frame_bytes = snd_mss0 * VLIB_FRAME_SIZE; |
| 138 | n_frames_per_evt = ceil ((double) max_len_to_snd0 / n_frame_bytes); |
| 139 | |
| 140 | n_bufs = vec_len (smm->tx_buffers[thread_index]); |
| 141 | left_to_snd0 = max_len_to_snd0; |
| 142 | for (i = 0; i < n_frames_per_evt; i++) |
| 143 | { |
| 144 | /* Make sure we have at least one full frame of buffers ready */ |
| 145 | if (PREDICT_FALSE (n_bufs < VLIB_FRAME_SIZE)) |
| 146 | { |
| 147 | vec_validate (smm->tx_buffers[thread_index], |
| 148 | n_bufs + VLIB_FRAME_SIZE - 1); |
| 149 | n_bufs += |
| 150 | vlib_buffer_alloc (vm, &smm->tx_buffers[thread_index][n_bufs], |
| 151 | VLIB_FRAME_SIZE); |
| 152 | |
| 153 | /* buffer shortage |
| 154 | * XXX 0.9 because when debugging we might not get a full frame */ |
| 155 | if (PREDICT_FALSE (n_bufs < 0.9 * VLIB_FRAME_SIZE)) |
| 156 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 157 | if (svm_fifo_set_event (s0->server_tx_fifo)) |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 158 | { |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 159 | vec_add1 (smm->evts_partially_read[thread_index], *e0); |
| 160 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 161 | return -1; |
| 162 | } |
| 163 | |
| 164 | _vec_len (smm->tx_buffers[thread_index]) = n_bufs; |
| 165 | } |
| 166 | |
| 167 | vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next); |
| 168 | while (left_to_snd0 && n_left_to_next) |
| 169 | { |
| 170 | /* Get free buffer */ |
| 171 | n_bufs--; |
| 172 | bi0 = smm->tx_buffers[thread_index][n_bufs]; |
| 173 | _vec_len (smm->tx_buffers[thread_index]) = n_bufs; |
| 174 | |
| 175 | b0 = vlib_get_buffer (vm, bi0); |
| 176 | b0->error = 0; |
| 177 | b0->flags = VLIB_BUFFER_TOTAL_LENGTH_VALID |
| 178 | | VNET_BUFFER_LOCALLY_ORIGINATED; |
| 179 | b0->current_data = 0; |
| 180 | |
| 181 | /* RX on the local interface. tx in default fib */ |
| 182 | vnet_buffer (b0)->sw_if_index[VLIB_RX] = 0; |
| 183 | vnet_buffer (b0)->sw_if_index[VLIB_TX] = (u32) ~ 0; |
| 184 | |
| 185 | /* usual speculation, or the enqueue_x1 macro will barf */ |
| 186 | to_next[0] = bi0; |
| 187 | to_next += 1; |
| 188 | n_left_to_next -= 1; |
| 189 | |
| 190 | VLIB_BUFFER_TRACE_TRAJECTORY_INIT (b0); |
| 191 | if (PREDICT_FALSE (n_trace > 0)) |
| 192 | { |
| 193 | session_queue_trace_t *t0; |
| 194 | vlib_trace_buffer (vm, node, next_index, b0, |
| 195 | 1 /* follow_chain */ ); |
| 196 | vlib_set_trace_count (vm, node, --n_trace); |
| 197 | t0 = vlib_add_trace (vm, node, b0, sizeof (*t0)); |
| 198 | t0->session_index = s0->session_index; |
| 199 | t0->server_thread_index = s0->thread_index; |
| 200 | } |
| 201 | |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 202 | len_to_deq0 = (left_to_snd0 < snd_mss0) ? left_to_snd0 : snd_mss0; |
| 203 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 204 | /* *INDENT-OFF* */ |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 205 | SESSION_EVT_DBG(SESSION_EVT_DEQ, s0, ({ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 206 | ed->data[0] = e0->event_id; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 207 | ed->data[1] = max_dequeue0; |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 208 | ed->data[2] = len_to_deq0; |
| 209 | ed->data[3] = left_to_snd0; |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 210 | })); |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 211 | /* *INDENT-ON* */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 212 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 213 | /* Make room for headers */ |
| 214 | data0 = vlib_buffer_make_headroom (b0, MAX_HDRS_LEN); |
| 215 | |
| 216 | /* Dequeue the data |
| 217 | * TODO 1) peek instead of dequeue |
| 218 | * 2) buffer chains */ |
| 219 | if (peek_data) |
| 220 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 221 | n_bytes_read = svm_fifo_peek (s0->server_tx_fifo, rx_offset, |
| 222 | len_to_deq0, data0); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 223 | if (n_bytes_read <= 0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 224 | goto dequeue_fail; |
| 225 | |
| 226 | /* Keep track of progress locally, transport is also supposed to |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 227 | * increment it independently when pushing the header */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 228 | rx_offset += n_bytes_read; |
| 229 | } |
| 230 | else |
| 231 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 232 | n_bytes_read = svm_fifo_dequeue_nowait (s0->server_tx_fifo, |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 233 | len_to_deq0, data0); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 234 | if (n_bytes_read <= 0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 235 | goto dequeue_fail; |
| 236 | } |
| 237 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 238 | b0->current_length = n_bytes_read; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 239 | |
| 240 | /* Ask transport to push header */ |
| 241 | transport_vft->push_header (tc0, b0); |
| 242 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 243 | left_to_snd0 -= n_bytes_read; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 244 | *n_tx_packets = *n_tx_packets + 1; |
| 245 | |
| 246 | vlib_validate_buffer_enqueue_x1 (vm, node, next_index, |
| 247 | to_next, n_left_to_next, |
| 248 | bi0, next0); |
| 249 | } |
| 250 | vlib_put_next_frame (vm, node, next_index, n_left_to_next); |
| 251 | } |
| 252 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 253 | /* If we couldn't dequeue all bytes mark as partially read */ |
| 254 | if (max_len_to_snd0 < max_dequeue0) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 255 | { |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 256 | /* If we don't already have new event */ |
| 257 | if (svm_fifo_set_event (s0->server_tx_fifo)) |
| 258 | { |
| 259 | vec_add1 (smm->evts_partially_read[thread_index], *e0); |
| 260 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 261 | } |
| 262 | return 0; |
| 263 | |
| 264 | dequeue_fail: |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 265 | /* |
| 266 | * Can't read from fifo. If we don't already have an event, save as partially |
| 267 | * read, return buff to free list and return |
| 268 | */ |
| 269 | clib_warning ("dequeue fail"); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 270 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 271 | if (svm_fifo_set_event (s0->server_tx_fifo)) |
| 272 | { |
| 273 | vec_add1 (smm->evts_partially_read[thread_index], *e0); |
| 274 | } |
| 275 | 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] | 276 | _vec_len (smm->tx_buffers[thread_index]) += 1; |
| 277 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 278 | return 0; |
| 279 | } |
| 280 | |
| 281 | int |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 282 | session_tx_fifo_peek_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 283 | session_manager_main_t * smm, |
| 284 | session_fifo_event_t * e0, |
| 285 | stream_session_t * s0, u32 thread_index, |
| 286 | int *n_tx_pkts) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 287 | { |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 288 | return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index, |
| 289 | n_tx_pkts, 1); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | int |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 293 | session_tx_fifo_dequeue_and_snd (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 294 | session_manager_main_t * smm, |
| 295 | session_fifo_event_t * e0, |
| 296 | stream_session_t * s0, u32 thread_index, |
| 297 | int *n_tx_pkts) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 298 | { |
Florin Coras | d79b41e | 2017-03-04 05:37:52 -0800 | [diff] [blame] | 299 | return session_tx_fifo_read_and_snd_i (vm, node, smm, e0, s0, thread_index, |
| 300 | n_tx_pkts, 0); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 301 | } |
| 302 | |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 303 | stream_session_t * |
| 304 | session_event_get_session (session_fifo_event_t * e0, u8 thread_index) |
| 305 | { |
| 306 | svm_fifo_t *f0; |
| 307 | stream_session_t *s0; |
| 308 | u32 session_index0; |
| 309 | |
| 310 | f0 = e0->fifo; |
| 311 | session_index0 = f0->master_session_index; |
| 312 | |
| 313 | /* $$$ add multiple event queues, per vpp worker thread */ |
| 314 | ASSERT (f0->master_thread_index == thread_index); |
| 315 | |
| 316 | s0 = stream_session_get_if_valid (session_index0, thread_index); |
| 317 | |
| 318 | ASSERT (s0->thread_index == thread_index); |
| 319 | |
| 320 | return s0; |
| 321 | } |
| 322 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 323 | static uword |
| 324 | session_queue_node_fn (vlib_main_t * vm, vlib_node_runtime_t * node, |
| 325 | vlib_frame_t * frame) |
| 326 | { |
| 327 | session_manager_main_t *smm = vnet_get_session_manager_main (); |
| 328 | session_fifo_event_t *my_fifo_events, *e; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 329 | u32 n_to_dequeue, n_events; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 330 | unix_shared_memory_queue_t *q; |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 331 | application_t *app; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 332 | int n_tx_packets = 0; |
Damjan Marion | 586afd7 | 2017-04-05 19:18:20 +0200 | [diff] [blame] | 333 | u32 my_thread_index = vm->thread_index; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 334 | int i, rv; |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 335 | f64 now = vlib_time_now (vm); |
| 336 | |
| 337 | SESSION_EVT_DBG (SESSION_EVT_POLL_GAP_TRACK, smm, my_thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 338 | |
| 339 | /* |
| 340 | * Update TCP time |
| 341 | */ |
Florin Coras | 3e350af | 2017-03-30 02:54:28 -0700 | [diff] [blame] | 342 | tcp_update_time (now, my_thread_index); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 343 | |
| 344 | /* |
| 345 | * Get vpp queue events |
| 346 | */ |
| 347 | q = smm->vpp_event_queues[my_thread_index]; |
| 348 | if (PREDICT_FALSE (q == 0)) |
| 349 | return 0; |
| 350 | |
| 351 | /* min number of events we can dequeue without blocking */ |
| 352 | n_to_dequeue = q->cursize; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 353 | my_fifo_events = smm->fifo_events[my_thread_index]; |
| 354 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 355 | if (n_to_dequeue == 0 && vec_len (my_fifo_events) == 0) |
| 356 | return 0; |
| 357 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 358 | SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 0); |
| 359 | |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 360 | /* |
| 361 | * If we didn't manage to process previous events try going |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 362 | * over them again without dequeuing new ones. |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 363 | */ |
| 364 | /* XXX: Block senders to sessions that can't keep up */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 365 | if (vec_len (my_fifo_events) >= 100) |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 366 | { |
| 367 | clib_warning ("too many fifo events unsolved"); |
| 368 | goto skip_dequeue; |
| 369 | } |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 370 | |
| 371 | /* See you in the next life, don't be late */ |
| 372 | if (pthread_mutex_trylock (&q->mutex)) |
| 373 | return 0; |
| 374 | |
| 375 | for (i = 0; i < n_to_dequeue; i++) |
| 376 | { |
| 377 | vec_add2 (my_fifo_events, e, 1); |
| 378 | unix_shared_memory_queue_sub_raw (q, (u8 *) e); |
| 379 | } |
| 380 | |
| 381 | /* The other side of the connection is not polling */ |
| 382 | if (q->cursize < (q->maxsize / 8)) |
| 383 | (void) pthread_cond_broadcast (&q->condvar); |
| 384 | pthread_mutex_unlock (&q->mutex); |
| 385 | |
| 386 | smm->fifo_events[my_thread_index] = my_fifo_events; |
| 387 | |
| 388 | skip_dequeue: |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 389 | n_events = vec_len (my_fifo_events); |
| 390 | for (i = 0; i < n_events; i++) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 391 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 392 | stream_session_t *s0; /* $$$ prefetch 1 ahead maybe */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 393 | session_fifo_event_t *e0; |
| 394 | |
| 395 | e0 = &my_fifo_events[i]; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 396 | |
| 397 | switch (e0->event_type) |
| 398 | { |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 399 | case FIFO_EVENT_APP_TX: |
| 400 | s0 = session_event_get_session (e0, my_thread_index); |
| 401 | |
| 402 | if (CLIB_DEBUG && !s0) |
| 403 | { |
| 404 | clib_warning ("It's dead, Jim!"); |
| 405 | continue; |
| 406 | } |
| 407 | |
| 408 | if (PREDICT_FALSE (s0->session_state == SESSION_STATE_CLOSED)) |
| 409 | continue; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 410 | /* Spray packets in per session type frames, since they go to |
| 411 | * different nodes */ |
Florin Coras | e69f495 | 2017-03-07 10:06:24 -0800 | [diff] [blame] | 412 | 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] | 413 | my_thread_index, |
| 414 | &n_tx_packets); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 415 | /* Out of buffers */ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 416 | if (rv < 0) |
| 417 | goto done; |
| 418 | |
| 419 | break; |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 420 | case FIFO_EVENT_DISCONNECT: |
| 421 | s0 = stream_session_get_from_handle (e0->session_handle); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 422 | stream_session_disconnect (s0); |
| 423 | break; |
| 424 | case FIFO_EVENT_BUILTIN_RX: |
Florin Coras | a546481 | 2017-04-19 13:00:05 -0700 | [diff] [blame] | 425 | s0 = session_event_get_session (e0, my_thread_index); |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 426 | svm_fifo_unset_event (s0->server_rx_fifo); |
| 427 | /* Get session's server */ |
| 428 | app = application_get (s0->app_index); |
| 429 | app->cb_fns.builtin_server_rx_callback (s0); |
| 430 | break; |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 431 | default: |
| 432 | clib_warning ("unhandled event type %d", e0->event_type); |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | done: |
| 437 | |
| 438 | /* Couldn't process all events. Probably out of buffers */ |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 439 | if (PREDICT_FALSE (i < n_events)) |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 440 | { |
| 441 | session_fifo_event_t *partially_read = |
| 442 | smm->evts_partially_read[my_thread_index]; |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 443 | vec_add (partially_read, &my_fifo_events[i], n_events - i); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 444 | vec_free (my_fifo_events); |
| 445 | smm->fifo_events[my_thread_index] = partially_read; |
| 446 | smm->evts_partially_read[my_thread_index] = 0; |
| 447 | } |
| 448 | else |
| 449 | { |
| 450 | vec_free (smm->fifo_events[my_thread_index]); |
| 451 | smm->fifo_events[my_thread_index] = |
| 452 | smm->evts_partially_read[my_thread_index]; |
| 453 | smm->evts_partially_read[my_thread_index] = 0; |
| 454 | } |
| 455 | |
| 456 | vlib_node_increment_counter (vm, session_queue_node.index, |
| 457 | SESSION_QUEUE_ERROR_TX, n_tx_packets); |
| 458 | |
Florin Coras | 6792ec0 | 2017-03-13 03:49:51 -0700 | [diff] [blame] | 459 | SESSION_EVT_DBG (SESSION_EVT_DEQ_NODE, 1); |
| 460 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 461 | return n_tx_packets; |
| 462 | } |
| 463 | |
| 464 | /* *INDENT-OFF* */ |
| 465 | VLIB_REGISTER_NODE (session_queue_node) = |
| 466 | { |
| 467 | .function = session_queue_node_fn, |
| 468 | .name = "session-queue", |
| 469 | .format_trace = format_session_queue_trace, |
| 470 | .type = VLIB_NODE_TYPE_INPUT, |
| 471 | .n_errors = ARRAY_LEN (session_queue_error_strings), |
| 472 | .error_strings = session_queue_error_strings, |
| 473 | .n_next_nodes = SESSION_QUEUE_N_NEXT, |
Florin Coras | e04c299 | 2017-03-01 08:17:34 -0800 | [diff] [blame] | 474 | .state = VLIB_NODE_STATE_DISABLED, |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 475 | .next_nodes = |
| 476 | { |
| 477 | [SESSION_QUEUE_NEXT_DROP] = "error-drop", |
| 478 | [SESSION_QUEUE_NEXT_IP4_LOOKUP] = "ip4-lookup", |
| 479 | [SESSION_QUEUE_NEXT_IP6_LOOKUP] = "ip6-lookup", |
| 480 | [SESSION_QUEUE_NEXT_TCP_IP4_OUTPUT] = "tcp4-output", |
| 481 | [SESSION_QUEUE_NEXT_TCP_IP6_OUTPUT] = "tcp6-output", |
| 482 | }, |
| 483 | }; |
| 484 | /* *INDENT-ON* */ |
| 485 | |
| 486 | /* |
| 487 | * fd.io coding-style-patch-verification: ON |
| 488 | * |
| 489 | * Local Variables: |
| 490 | * eval: (c-set-style "gnu") |
| 491 | * End: |
| 492 | */ |