/* * Copyright (c) 2017-2019 Cisco and/or its affiliates. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at: * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include #include #include #include #include typedef struct { /* * Server app parameters */ svm_msg_q_t **vpp_queue; svm_queue_t *vl_input_queue; /**< Sever's event queue */ u32 app_index; /**< Server app index */ u32 my_client_index; /**< API client handle */ u32 node_index; /**< process node index for event scheduling */ /* * Config params */ u8 no_echo; /**< Don't echo traffic */ u32 fifo_size; /**< Fifo size */ u32 rcv_buffer_size; /**< Rcv buffer size */ u32 prealloc_fifos; /**< Preallocate fifos */ u32 private_segment_count; /**< Number of private segments */ u32 private_segment_size; /**< Size of private segments */ char *server_uri; /**< Server URI */ u32 tls_engine; /**< TLS engine: mbedtls/openssl */ u8 is_dgram; /**< set if transport is dgram */ /* * Test state */ u8 **rx_buf; /**< Per-thread RX buffer */ u64 byte_index; u32 **rx_retries; vlib_main_t *vlib_main; } echo_server_main_t; echo_server_main_t echo_server_main; int echo_server_session_accept_callback (session_t * s) { echo_server_main_t *esm = &echo_server_main; esm->vpp_queue[s->thread_index] = session_main_get_vpp_event_queue (s->thread_index); s->session_state = SESSION_STATE_READY; esm->byte_index = 0; ASSERT (vec_len (esm->rx_retries) > s->thread_index); vec_validate (esm->rx_retries[s->thread_index], s->session_index); esm->rx_retries[s->thread_index][s->session_index] = 0; return 0; } void echo_server_session_disconnect_callback (session_t * s) { echo_server_main_t *esm = &echo_server_main; vnet_disconnect_args_t _a = { 0 }, *a = &_a; a->handle = session_handle (s); a->app_index = esm->app_index; vnet_disconnect_session (a); } void echo_server_session_reset_callback (session_t * s) { echo_server_main_t *esm = &echo_server_main; vnet_disconnect_args_t _a = { 0 }, *a = &_a; clib_warning ("Reset session %U", format_session, s, 2); a->handle = session_handle (s); a->app_index = esm->app_index; vnet_disconnect_session (a); } int echo_server_session_connected_callback (u32 app_index, u32 api_context, session_t * s, u8 is_fail) { clib_warning ("called..."); return -1; } int echo_server_add_segment_callback (u32 client_index, u64 segment_handle) { /* New heaps may be added */ return 0; } int echo_server_redirect_connect_callback (u32 client_index, void *mp) { clib_warning ("called..."); return -1; } void test_bytes (echo_server_main_t * esm, int actual_transfer) { int i; u32 my_thread_id = vlib_get_thread_index (); for (i = 0; i < actual_transfer; i++) { if (esm->rx_buf[my_thread_id][i] != ((esm->byte_index + i) & 0xff)) { clib_warning ("at %lld expected %d got %d", esm->byte_index + i, (esm->byte_index + i) & 0xff, esm->rx_buf[my_thread_id][i]); } } esm->byte_index += actual_transfer; } /* * If no-echo, just drop the data and be done with it. */ int echo_server_builtin_server_rx_callback_no_echo (session_t * s) { svm_fifo_t *rx_fifo = s->rx_fifo; svm_fifo_dequeue_drop (rx_fifo, svm_fifo_max_dequeue (rx_fifo)); return 0; } int echo_server_rx_callback (session_t * s) { u32 n_written, max_dequeue, max_enqueue, max_transfer; int actual_transfer; svm_fifo_t *tx_fifo, *rx_fifo; echo_server_main_t *esm = &echo_server_main; u32 thread_index = vlib_get_thread_index (); app_session_transport_t at; ASSERT (s->thread_index == thread_index); rx_fifo = s->rx_fifo; tx_fifo = s->tx_fifo; ASSERT (rx_fifo->master_thread_index == thread_index); ASSERT (tx_fifo->master_thread_index == thread_index); max_enqueue = svm_fifo_max_enqueue (tx_fifo); if (!esm->is_dgram) { max_dequeue = svm_fifo_max_dequeue (rx_fifo); } else { session_dgram_pre_hdr_t ph; svm_fifo_peek (rx_fifo, 0, sizeof (ph), (u8 *) & ph); max_dequeue = ph.data_length - ph.data_offset; if (!esm->vpp_queue[s->thread_index]) { svm_msg_q_t *mq; mq = session_main_get_vpp_event_queue (s->thread_index); esm->vpp_queue[s->thread_index] = mq; } max_enqueue -= sizeof (session_dgram_hdr_t); } if (PREDICT_FALSE (max_dequeue == 0)) return 0; /* Number of bytes we're going to copy */ max_transfer = clib_min (max_dequeue, max_enqueue); /* No space in tx fifo */ if (PREDICT_FALSE (max_transfer == 0)) { /* XXX timeout for session that are stuck */ rx_event: /* Program self-tap to retry */ if (svm_fifo_set_event (rx_fifo)) { if (session_send_io_evt_to_thread (rx_fifo, SESSION_IO_EVT_BUILTIN_RX)) clib_warning ("failed to enqueue self-tap"); vec_validate (esm->rx_retries[s->thread_index], s->session_index); if (esm->rx_retries[thread_index][s->session_index] == 500000) { clib_warning ("session stuck: %U", format_session, s, 2); } if (esm->rx_retries[thread_index][s->session_index] < 500001) esm->rx_retries[thread_index][s->session_index]++; } return 0; } vec_validate (esm->rx_buf[thread_index], max_transfer); if (!esm->is_dgram) { actual_transfer = app_recv_stream_raw (rx_fifo, esm->rx_buf[thread_index], max_transfer, 0 /* don't clear event */ , 0 /* peek */ ); } else { actual_transfer = app_recv_dgram_raw (rx_fifo, esm->rx_buf[thread_index], max_transfer, &at, 0 /* don't clear event */ , 0 /* peek */ ); } ASSERT (actual_transfer == max_transfer); /* test_bytes (esm, actual_transfer); */ /* * Echo back */ if (!esm->is_dgram) { n_written = app_send_stream_raw (tx_fifo, esm->vpp_queue[thread_index], esm->rx_buf[thread_index], actual_transfer, SESSION_IO_EVT_TX, 1 /* do_evt */ , 0);
/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 * Licensed