X-Git-Url: https://gerrit.fd.io/r/gitweb?a=blobdiff_plain;f=src%2Fvnet%2Fsession%2Fsession.c;h=1c8b7fb4be449d2b65c15994df3cc2a20122430a;hb=26dd6de91b4d36ac04154c7eb6339684db6684a0;hp=cd8da50a5c9bec0312d5acc34b03f59028d1c2f9;hpb=d5c604d6872bdb5576a900d51b96f2e6736b63a5;p=vpp.git diff --git a/src/vnet/session/session.c b/src/vnet/session/session.c index cd8da50a5c9..1c8b7fb4be4 100644 --- a/src/vnet/session/session.c +++ b/src/vnet/session/session.c @@ -32,18 +32,10 @@ session_send_evt_to_thread (void *data, void *args, u32 thread_index, session_event_t *evt; svm_msg_q_msg_t msg; svm_msg_q_t *mq; - u32 tries = 0, max_tries; mq = session_main_get_vpp_event_queue (thread_index); - while (svm_msg_q_try_lock (mq)) - { - max_tries = vlib_get_current_process (vlib_get_main ())? 1e6 : 3; - if (tries++ == max_tries) - { - SESSION_DBG ("failed to enqueue evt"); - return -1; - } - } + if (PREDICT_FALSE (svm_msg_q_lock (mq))) + return -1; if (PREDICT_FALSE (svm_msg_q_ring_is_full (mq, SESSION_MQ_IO_EVT_RING))) { svm_msg_q_unlock (mq); @@ -63,6 +55,7 @@ session_send_evt_to_thread (void *data, void *args, u32 thread_index, evt->rpc_args.fp = data; evt->rpc_args.arg = args; break; + case SESSION_IO_EVT_RX: case SESSION_IO_EVT_TX: case SESSION_IO_EVT_TX_FLUSH: case SESSION_IO_EVT_BUILTIN_RX: @@ -105,12 +98,19 @@ session_send_ctrl_evt_to_thread (session_t * s, session_evt_type_t evt_type) SESSION_CTRL_EVT_CLOSE); } +void +session_send_rpc_evt_to_thread_force (u32 thread_index, void *fp, + void *rpc_args) +{ + session_send_evt_to_thread (fp, rpc_args, thread_index, + SESSION_CTRL_EVT_RPC); +} + void session_send_rpc_evt_to_thread (u32 thread_index, void *fp, void *rpc_args) { if (thread_index != vlib_get_thread_index ()) - session_send_evt_to_thread (fp, rpc_args, thread_index, - SESSION_CTRL_EVT_RPC); + session_send_rpc_evt_to_thread_force (thread_index, fp, rpc_args); else { void (*fnp) (void *) = fp; @@ -118,22 +118,48 @@ session_send_rpc_evt_to_thread (u32 thread_index, void *fp, void *rpc_args) } } +void +session_add_self_custom_tx_evt (transport_connection_t * tc, u8 has_prio) +{ + session_t *s; + + s = session_get (tc->s_index, tc->thread_index); + ASSERT (s->thread_index == vlib_get_thread_index ()); + if (!(s->flags & SESSION_F_CUSTOM_TX)) + { + s->flags |= SESSION_F_CUSTOM_TX; + if (svm_fifo_set_event (s->tx_fifo)) + { + session_worker_t *wrk; + session_evt_elt_t *elt; + wrk = session_main_get_worker (tc->thread_index); + if (has_prio) + elt = session_evt_alloc_new (wrk); + else + elt = session_evt_alloc_old (wrk); + elt->evt.session_index = tc->s_index; + elt->evt.event_type = SESSION_IO_EVT_TX; + } + } +} + static void session_program_transport_close (session_t * s) { u32 thread_index = vlib_get_thread_index (); + session_evt_elt_t *elt; session_worker_t *wrk; - session_event_t *evt; /* If we are in the handler thread, or being called with the worker barrier * held, just append a new event to pending disconnects vector. */ if (vlib_thread_is_main_w_barrier () || thread_index == s->thread_index) { wrk = session_main_get_worker (s->thread_index); - vec_add2 (wrk->pending_disconnects, evt, 1); - clib_memset (evt, 0, sizeof (*evt)); - evt->session_handle = session_handle (s); - evt->event_type = SESSION_CTRL_EVT_CLOSE; + elt = session_evt_elt_alloc (wrk); + clib_memset (&elt->evt, 0, sizeof (session_event_t)); + elt->evt.session_handle = session_handle (s); + elt->evt.event_type = SESSION_CTRL_EVT_CLOSE; + session_evt_add_pending_disconnects (wrk, elt); } else session_send_ctrl_evt_to_thread (s, SESSION_CTRL_EVT_CLOSE); @@ -161,20 +187,39 @@ session_alloc (u32 thread_index) clib_memset (s, 0, sizeof (*s)); s->session_index = s - wrk->sessions; s->thread_index = thread_index; + s->app_index = APP_INVALID_INDEX; return s; } void session_free (session_t * s) { - pool_put (session_main.wrk[s->thread_index].sessions, s); if (CLIB_DEBUG) - clib_memset (s, 0xFA, sizeof (*s)); + { + u8 thread_index = s->thread_index; + clib_memset (s, 0xFA, sizeof (*s)); + pool_put (session_main.wrk[thread_index].sessions, s); + return; + } + SESSION_EVT (SESSION_EVT_FREE, s); + pool_put (session_main.wrk[s->thread_index].sessions, s); +} + +static void +session_cleanup_notify (session_t * s, session_cleanup_ntf_t ntf) +{ + app_worker_t *app_wrk; + + app_wrk = app_worker_get_if_valid (s->app_wrk_index); + if (!app_wrk) + return; + app_worker_cleanup_notify (app_wrk, s, ntf); } void session_free_w_fifos (session_t * s) { + session_cleanup_notify (s, SESSION_CLEANUP_SESSION); segment_manager_dealloc_fifos (s->rx_fifo, s->tx_fifo); session_free (s); } @@ -283,7 +328,7 @@ session_enqueue_chain_tail (session_t * s, vlib_buffer_t * b, continue; if (is_in_order) { - rv = svm_fifo_enqueue_nowait (s->rx_fifo, len, data); + rv = svm_fifo_enqueue (s->rx_fifo, len, data); if (rv == len) { written += rv; @@ -350,9 +395,9 @@ session_enqueue_stream_connection (transport_connection_t * tc, if (is_in_order) { - enqueued = svm_fifo_enqueue_nowait (s->rx_fifo, - b->current_length, - vlib_buffer_get_current (b)); + enqueued = svm_fifo_enqueue (s->rx_fifo, + b->current_length, + vlib_buffer_get_current (b)); if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && enqueued >= 0)) { @@ -398,13 +443,12 @@ session_enqueue_dgram_connection (session_t * s, { int enqueued = 0, rv, in_order_off; - ASSERT (svm_fifo_max_enqueue (s->rx_fifo) + ASSERT (svm_fifo_max_enqueue_prod (s->rx_fifo) >= b->current_length + sizeof (*hdr)); - svm_fifo_enqueue_nowait (s->rx_fifo, sizeof (session_dgram_hdr_t), - (u8 *) hdr); - enqueued = svm_fifo_enqueue_nowait (s->rx_fifo, b->current_length, - vlib_buffer_get_current (b)); + svm_fifo_enqueue (s->rx_fifo, sizeof (session_dgram_hdr_t), (u8 *) hdr); + enqueued = svm_fifo_enqueue (s->rx_fifo, b->current_length, + vlib_buffer_get_current (b)); if (PREDICT_FALSE ((b->flags & VLIB_BUFFER_NEXT_PRESENT) && enqueued >= 0)) { in_order_off = enqueued > b->current_length ? enqueued : 0; @@ -440,7 +484,14 @@ u32 session_tx_fifo_dequeue_drop (transport_connection_t * tc, u32 max_bytes) { session_t *s = session_get (tc->s_index, tc->thread_index); - return svm_fifo_dequeue_drop (s->tx_fifo, max_bytes); + u32 rv; + + rv = svm_fifo_dequeue_drop (s->tx_fifo, max_bytes); + + if (svm_fifo_needs_deq_ntf (s->tx_fifo, max_bytes)) + session_dequeue_notify (s); + + return rv; } static inline int @@ -479,6 +530,11 @@ static inline int session_enqueue_notify_inline (session_t * s) { app_worker_t *app_wrk; + u32 session_index; + u8 n_subscribers; + + session_index = s->session_index; + n_subscribers = svm_fifo_n_subscribers (s->rx_fifo); app_wrk = app_worker_get_if_valid (s->app_wrk_index); if (PREDICT_FALSE (!app_wrk)) @@ -487,21 +543,19 @@ session_enqueue_notify_inline (session_t * s) return 0; } - /* *INDENT-OFF* */ - SESSION_EVT_DBG(SESSION_EVT_ENQ, s, ({ - ed->data[0] = SESSION_IO_EVT_RX; - ed->data[1] = svm_fifo_max_dequeue (s->rx_fifo); - })); - /* *INDENT-ON* */ + SESSION_EVT (SESSION_EVT_ENQ, s, svm_fifo_max_dequeue_prod (s->rx_fifo)); s->flags &= ~SESSION_F_RX_EVT; if (PREDICT_FALSE (app_worker_lock_and_send_event (app_wrk, s, SESSION_IO_EVT_RX))) return -1; - if (PREDICT_FALSE (svm_fifo_n_subscribers (s->rx_fifo))) - return session_notify_subscribers (app_wrk->app_index, s, - s->rx_fifo, SESSION_IO_EVT_RX); + if (PREDICT_FALSE (n_subscribers)) + { + s = session_get (session_index, vlib_get_thread_index ()); + return session_notify_subscribers (app_wrk->app_index, s, + s->rx_fifo, SESSION_IO_EVT_RX); + } return 0; } @@ -529,7 +583,7 @@ session_dequeue_notify (session_t * s) return session_notify_subscribers (app_wrk->app_index, s, s->tx_fifo, SESSION_IO_EVT_TX); - svm_fifo_clear_tx_ntf (s->tx_fifo); + svm_fifo_clear_deq_ntf (s->tx_fifo); return 0; } @@ -561,9 +615,6 @@ session_main_flush_enqueue_events (u8 transport_proto, u32 thread_index) continue; } - if (svm_fifo_has_event (s->rx_fifo) || svm_fifo_is_empty (s->rx_fifo)) - continue; - if (PREDICT_FALSE (session_enqueue_notify_inline (s))) errors++; } @@ -584,8 +635,9 @@ session_main_flush_all_enqueue_events (u8 transport_proto) return errors; } -int -session_stream_connect_notify (transport_connection_t * tc, u8 is_fail) +static inline int +session_stream_connect_notify_inline (transport_connection_t * tc, u8 is_fail, + session_state_t opened_state) { u32 opaque = 0, new_ti, new_si; app_worker_t *app_wrk; @@ -628,6 +680,10 @@ session_stream_connect_notify (transport_connection_t * tc, u8 is_fail) return -1; } + s = session_get (new_si, new_ti); + s->session_state = opened_state; + session_lookup_add_connection (tc, session_handle (s)); + if (app_worker_connect_notify (app_wrk, s, opaque)) { s = session_get (new_si, new_ti); @@ -635,13 +691,23 @@ session_stream_connect_notify (transport_connection_t * tc, u8 is_fail) return -1; } - s = session_get (new_si, new_ti); - s->session_state = SESSION_STATE_READY; - session_lookup_add_connection (tc, session_handle (s)); - return 0; } +int +session_stream_connect_notify (transport_connection_t * tc, u8 is_fail) +{ + return session_stream_connect_notify_inline (tc, is_fail, + SESSION_STATE_READY); +} + +int +session_ho_stream_connect_notify (transport_connection_t * tc, u8 is_fail) +{ + return session_stream_connect_notify_inline (tc, is_fail, + SESSION_STATE_OPENED); +} + typedef struct _session_switch_pool_args { u32 session_index; @@ -741,9 +807,6 @@ session_transport_delete_notify (transport_connection_t * tc) if (!(s = session_get_if_valid (tc->s_index, tc->thread_index))) return; - /* Make sure we don't try to send anything more */ - svm_fifo_dequeue_drop_all (s->tx_fifo); - switch (s->session_state) { case SESSION_STATE_CREATED: @@ -754,62 +817,82 @@ session_transport_delete_notify (transport_connection_t * tc) break; case SESSION_STATE_ACCEPTING: case SESSION_STATE_TRANSPORT_CLOSING: + case SESSION_STATE_CLOSING: + case SESSION_STATE_TRANSPORT_CLOSED: /* If transport finishes or times out before we get a reply * from the app, mark transport as closed and wait for reply * before removing the session. Cleanup session table in advance * because transport will soon be closed and closed sessions * are assumed to have been removed from the lookup table */ session_lookup_del_session (s); - s->session_state = SESSION_STATE_TRANSPORT_CLOSED; + s->session_state = SESSION_STATE_TRANSPORT_DELETED; + session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT); + svm_fifo_dequeue_drop_all (s->tx_fifo); break; - case SESSION_STATE_CLOSING: - case SESSION_STATE_CLOSED_WAITING: + case SESSION_STATE_APP_CLOSED: /* Cleanup lookup table as transport needs to still be valid. * Program transport close to ensure that all session events * have been cleaned up. Once transport close is called, the * session is just removed because both transport and app have * confirmed the close*/ session_lookup_del_session (s); - s->session_state = SESSION_STATE_TRANSPORT_CLOSED; + s->session_state = SESSION_STATE_CLOSED; + session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT); + svm_fifo_dequeue_drop_all (s->tx_fifo); session_program_transport_close (s); break; - case SESSION_STATE_TRANSPORT_CLOSED: + case SESSION_STATE_TRANSPORT_DELETED: break; case SESSION_STATE_CLOSED: + session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT); session_delete (s); break; default: clib_warning ("session state %u", s->session_state); + session_cleanup_notify (s, SESSION_CLEANUP_TRANSPORT); session_delete (s); break; } } /** - * Notification from transport that session can be closed + * Notification from transport that it is closed * - * Should be called by transport only if it was closed with non-empty - * tx fifo and once it decides to begin the closing procedure prior to - * issuing a delete notify. This gives the chance to the session layer - * to cleanup any outstanding events. + * Should be called by transport, prior to calling delete notify, once it + * knows that no more data will be exchanged. This could serve as an + * early acknowledgment of an active close especially if transport delete + * can be delayed a long time, e.g., tcp time-wait. */ void session_transport_closed_notify (transport_connection_t * tc) { + app_worker_t *app_wrk; session_t *s; if (!(s = session_get_if_valid (tc->s_index, tc->thread_index))) return; + /* Transport thinks that app requested close but it actually didn't. + * Can happen for tcp if fin and rst are received in close succession. */ + if (s->session_state == SESSION_STATE_READY) + { + session_transport_closing_notify (tc); + svm_fifo_dequeue_drop_all (s->tx_fifo); + s->session_state = SESSION_STATE_TRANSPORT_CLOSED; + } /* If app close has not been received or has not yet resulted in * a transport close, only mark the session transport as closed */ - if (s->session_state <= SESSION_STATE_CLOSING) + else if (s->session_state <= SESSION_STATE_CLOSING) { - session_lookup_del_session (s); s->session_state = SESSION_STATE_TRANSPORT_CLOSED; } - else + /* If app also closed, switch to closed */ + else if (s->session_state == SESSION_STATE_APP_CLOSED) s->session_state = SESSION_STATE_CLOSED; + + app_wrk = app_worker_get_if_valid (s->app_wrk_index); + if (app_wrk) + app_worker_transport_closed_notify (app_wrk, s); } /** @@ -818,17 +901,16 @@ session_transport_closed_notify (transport_connection_t * tc) void session_transport_reset_notify (transport_connection_t * tc) { - session_t *s; app_worker_t *app_wrk; - application_t *app; + session_t *s; + s = session_get (tc->s_index, tc->thread_index); svm_fifo_dequeue_drop_all (s->tx_fifo); if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSING) return; s->session_state = SESSION_STATE_TRANSPORT_CLOSING; app_wrk = app_worker_get (s->app_wrk_index); - app = application_get (app_wrk->app_index); - app->cb_fns.session_reset_callback (s); + app_worker_reset_notify (app_wrk, s); } int @@ -850,13 +932,13 @@ session_stream_accept_notify (transport_connection_t * tc) */ int session_stream_accept (transport_connection_t * tc, u32 listener_index, - u8 notify) + u32 thread_index, u8 notify) { session_t *s; int rv; s = session_alloc_for_connection (tc); - s->listener_index = listener_index; + s->listener_handle = ((u64) thread_index << 32) | (u64) listener_index; s->session_state = SESSION_STATE_CREATED; if ((rv = app_worker_init_accepted (s))) @@ -880,6 +962,7 @@ session_open_cl (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque) transport_connection_t *tc; transport_endpoint_cfg_t *tep; app_worker_t *app_wrk; + session_handle_t sh; session_t *s; int rv; @@ -904,6 +987,8 @@ session_open_cl (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque) return -1; } + sh = session_handle (s); + session_lookup_add_connection (tc, sh); return app_worker_connect_notify (app_wrk, s, opaque); } @@ -939,6 +1024,8 @@ session_open_vc (u32 app_wrk_index, session_endpoint_t * rmt, u32 opaque) * thing but better than allocating a separate half-open pool. */ tc->s_index = opaque; + if (transport_half_open_has_fifos (rmt->transport_proto)) + return session_ho_stream_connect_notify (tc, 0 /* is_fail */ ); return 0; } @@ -1034,7 +1121,8 @@ session_stop_listen (session_t * s) if (!tc) return VNET_API_ERROR_ADDRESS_NOT_IN_USE; - session_lookup_del_connection (tc); + if (!(tc->flags & TRANSPORT_CONNECTION_F_NO_LOOKUP)) + session_lookup_del_connection (tc); transport_stop_listen (tp, s->connection_index); return 0; } @@ -1055,12 +1143,9 @@ session_close (session_t * s) { /* Session will only be removed once both app and transport * acknowledge the close */ - if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED) + if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED + || s->session_state == SESSION_STATE_TRANSPORT_DELETED) session_program_transport_close (s); - - /* Session already closed. Clear the tx fifo */ - if (s->session_state == SESSION_STATE_CLOSED) - svm_fifo_dequeue_drop_all (s->tx_fifo); return; } @@ -1078,23 +1163,23 @@ session_close (session_t * s) void session_transport_close (session_t * s) { - /* If transport is already closed, just free the session */ - if (s->session_state >= SESSION_STATE_TRANSPORT_CLOSED) + if (s->session_state >= SESSION_STATE_APP_CLOSED) { - session_free_w_fifos (s); + if (s->session_state == SESSION_STATE_TRANSPORT_CLOSED) + s->session_state = SESSION_STATE_CLOSED; + /* If transport is already deleted, just free the session */ + else if (s->session_state >= SESSION_STATE_TRANSPORT_DELETED) + session_free_w_fifos (s); return; } - /* If tx queue wasn't drained, change state to closed waiting for transport. - * This way, the transport, if it so wishes, can continue to try sending the - * outstanding data (in closed state it cannot). It MUST however at one - * point, either after sending everything or after a timeout, call delete - * notify. This will finally lead to the complete cleanup of the session. + /* If the tx queue wasn't drained, the transport can continue to try + * sending the outstanding data (in closed state it cannot). It MUST however + * at one point, either after sending everything or after a timeout, call + * delete notify. This will finally lead to the complete cleanup of the + * session. */ - if (svm_fifo_max_dequeue (s->tx_fifo)) - s->session_state = SESSION_STATE_CLOSED_WAITING; - else - s->session_state = SESSION_STATE_CLOSED; + s->session_state = SESSION_STATE_APP_CLOSED; transport_close (session_get_transport_proto (s), s->connection_index, s->thread_index); @@ -1207,7 +1292,7 @@ session_segment_handle (session_t * s) { svm_fifo_t *f; - if (s->session_state == SESSION_STATE_LISTENING) + if (!s->rx_fifo) return SESSION_INVALID_HANDLE; f = s->rx_fifo; @@ -1256,7 +1341,8 @@ session_register_transport (transport_proto_t transport_proto, /* *INDENT-ON* */ smm->session_type_to_next[session_type] = next_index; - smm->session_tx_fns[session_type] = session_tx_fns[vft->tx_type]; + smm->session_tx_fns[session_type] = + session_tx_fns[vft->transport_options.tx_type]; } transport_connection_t * @@ -1270,6 +1356,18 @@ session_get_transport (session_t * s) s->connection_index); } +void +session_get_endpoint (session_t * s, transport_endpoint_t * tep, u8 is_lcl) +{ + if (s->session_state != SESSION_STATE_LISTENING) + return transport_get_endpoint (session_get_transport_proto (s), + s->connection_index, s->thread_index, tep, + is_lcl); + else + return transport_get_listener_endpoint (session_get_transport_proto (s), + s->connection_index, tep, is_lcl); +} + transport_connection_t * listen_session_get_transport (session_t * s) { @@ -1306,26 +1404,18 @@ session_manager_main_enable (vlib_main_t * vm) for (i = 0; i < num_threads; i++) { wrk = &smm->wrk[i]; - vec_validate (wrk->free_event_vector, 128); - _vec_len (wrk->free_event_vector) = 0; - vec_validate (wrk->pending_event_vector, 128); - _vec_len (wrk->pending_event_vector) = 0; - vec_validate (wrk->pending_disconnects, 128); - _vec_len (wrk->pending_disconnects) = 0; - vec_validate (wrk->postponed_event_vector, 128); - _vec_len (wrk->postponed_event_vector) = 0; - + wrk->new_head = clib_llist_make_head (wrk->event_elts, evt_list); + wrk->old_head = clib_llist_make_head (wrk->event_elts, evt_list); + wrk->postponed_head = clib_llist_make_head (wrk->event_elts, evt_list); + wrk->disconnects_head = clib_llist_make_head (wrk->event_elts, + evt_list); + wrk->vm = vlib_mains[i]; wrk->last_vlib_time = vlib_time_now (vlib_mains[i]); - wrk->dispatch_period = 500e-6; if (num_threads > 1) clib_rwlock_init (&smm->wrk[i].peekers_rw_locks); } -#if SESSION_DEBUG - vec_validate (smm->last_event_poll_by_thread, num_threads - 1); -#endif - /* Allocate vpp event queues segment and queue */ session_vpp_event_queues_allocate (smm); @@ -1411,8 +1501,8 @@ vnet_session_enable_disable (vlib_main_t * vm, u8 is_en) if (session_main.is_enabled) return 0; - session_node_enable_disable (is_en); error = session_manager_main_enable (vm); + session_node_enable_disable (is_en); } else {