blob: cc7a27642a8c7143780b1d0e9a05e138ec183314 [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Dave Wallace33e002b2017-09-06 01:20:02 -04002 * Copyright (c) 2017 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
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
16#include <stdio.h>
17#include <stdlib.h>
Dave Wallace543852a2017-08-03 02:11:34 -040018#include <svm/svm_fifo_segment.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040019#include <vcl/vppcom.h>
Florin Coras0d427d82018-06-27 03:24:07 -070020#include <vcl/vcl_debug.h>
Florin Coras697faea2018-06-27 17:10:49 -070021#include <vcl/vcl_private.h>
Dave Wallace7e607a72018-06-18 18:41:32 -040022
Florin Coras134a9962018-08-28 11:32:04 -070023__thread uword __vcl_worker_index = ~0;
24
Florin Corasd85de682018-11-29 17:02:29 -080025static int
26vcl_wait_for_segment (u64 segment_handle)
Florin Coras54693d22018-07-17 10:46:29 -070027{
Florin Corasd85de682018-11-29 17:02:29 -080028 vcl_worker_t *wrk = vcl_worker_get_current ();
29 u32 wait_for_seconds = 10, segment_index;
30 f64 timeout;
Florin Coras54693d22018-07-17 10:46:29 -070031
Florin Corasd85de682018-11-29 17:02:29 -080032 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
Florin Coras5f45e012019-01-23 09:21:30 -080033 return 0;
Florin Coras54693d22018-07-17 10:46:29 -070034
Florin Corasd85de682018-11-29 17:02:29 -080035 timeout = clib_time_now (&wrk->clib_time) + wait_for_seconds;
36 while (clib_time_now (&wrk->clib_time) < timeout)
Florin Coras54693d22018-07-17 10:46:29 -070037 {
Florin Corasd85de682018-11-29 17:02:29 -080038 segment_index = vcl_segment_table_lookup (segment_handle);
39 if (segment_index != VCL_INVALID_SEGMENT_INDEX)
40 return 0;
41 usleep (10);
Florin Coras54693d22018-07-17 10:46:29 -070042 }
Florin Corasd85de682018-11-29 17:02:29 -080043 return 1;
Florin Coras54693d22018-07-17 10:46:29 -070044}
45
Florin Coras30e79c22019-01-02 19:31:22 -080046static inline int
47vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq)
48{
49 svm_msg_q_msg_t *msg;
50 u32 n_msgs;
51 int i;
52
53 n_msgs = svm_msg_q_size (mq);
54 for (i = 0; i < n_msgs; i++)
55 {
56 vec_add2 (wrk->mq_msg_vector, msg, 1);
57 svm_msg_q_sub_w_lock (mq, msg);
58 }
59 return n_msgs;
60}
61
Florin Coras697faea2018-06-27 17:10:49 -070062const char *
Florin Coras288eaab2019-02-03 15:26:14 -080063vppcom_session_state_str (vcl_session_state_t state)
Dave Wallace543852a2017-08-03 02:11:34 -040064{
65 char *st;
66
67 switch (state)
68 {
69 case STATE_START:
70 st = "STATE_START";
71 break;
72
73 case STATE_CONNECT:
74 st = "STATE_CONNECT";
75 break;
76
77 case STATE_LISTEN:
78 st = "STATE_LISTEN";
79 break;
80
81 case STATE_ACCEPT:
82 st = "STATE_ACCEPT";
83 break;
84
Florin Coras3c7d4f92018-12-14 11:28:43 -080085 case STATE_VPP_CLOSING:
86 st = "STATE_VPP_CLOSING";
Dave Wallace4878cbe2017-11-21 03:45:09 -050087 break;
88
Dave Wallace543852a2017-08-03 02:11:34 -040089 case STATE_DISCONNECT:
90 st = "STATE_DISCONNECT";
91 break;
92
93 case STATE_FAILED:
94 st = "STATE_FAILED";
95 break;
96
Florin Coras2d675d72019-01-28 15:54:27 -080097 case STATE_UPDATED:
98 st = "STATE_UPDATED";
99 break;
100
101 case STATE_LISTEN_NO_MQ:
102 st = "STATE_LISTEN_NO_MQ";
103 break;
104
Dave Wallace543852a2017-08-03 02:11:34 -0400105 default:
106 st = "UNKNOWN_STATE";
107 break;
108 }
109
110 return st;
111}
112
Dave Wallace543852a2017-08-03 02:11:34 -0400113u8 *
114format_ip4_address (u8 * s, va_list * args)
115{
116 u8 *a = va_arg (*args, u8 *);
117 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
118}
119
120u8 *
121format_ip6_address (u8 * s, va_list * args)
122{
123 ip6_address_t *a = va_arg (*args, ip6_address_t *);
124 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
125
126 i_max_n_zero = ARRAY_LEN (a->as_u16);
127 max_n_zeros = 0;
128 i_first_zero = i_max_n_zero;
129 n_zeros = 0;
130 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
131 {
132 u32 is_zero = a->as_u16[i] == 0;
133 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
134 {
135 i_first_zero = i;
136 n_zeros = 0;
137 }
138 n_zeros += is_zero;
139 if ((!is_zero && n_zeros > max_n_zeros)
140 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
141 {
142 i_max_n_zero = i_first_zero;
143 max_n_zeros = n_zeros;
144 i_first_zero = ARRAY_LEN (a->as_u16);
145 n_zeros = 0;
146 }
147 }
148
149 last_double_colon = 0;
150 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
151 {
152 if (i == i_max_n_zero && max_n_zeros > 1)
153 {
154 s = format (s, "::");
155 i += max_n_zeros - 1;
156 last_double_colon = 1;
157 }
158 else
159 {
160 s = format (s, "%s%x",
161 (last_double_colon || i == 0) ? "" : ":",
162 clib_net_to_host_u16 (a->as_u16[i]));
163 last_double_colon = 0;
164 }
165 }
166
167 return s;
168}
169
170/* Format an IP46 address. */
171u8 *
172format_ip46_address (u8 * s, va_list * args)
173{
174 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
175 ip46_type_t type = va_arg (*args, ip46_type_t);
176 int is_ip4 = 1;
177
178 switch (type)
179 {
180 case IP46_TYPE_ANY:
181 is_ip4 = ip46_address_is_ip4 (ip46);
182 break;
183 case IP46_TYPE_IP4:
184 is_ip4 = 1;
185 break;
186 case IP46_TYPE_IP6:
187 is_ip4 = 0;
188 break;
189 }
190
191 return is_ip4 ?
192 format (s, "%U", format_ip4_address, &ip46->ip4) :
193 format (s, "%U", format_ip6_address, &ip46->ip6);
194}
195
Florin Coras697faea2018-06-27 17:10:49 -0700196/*
197 * VPPCOM Utility Functions
198 */
199
Florin Coras697faea2018-06-27 17:10:49 -0700200
Florin Coras54693d22018-07-17 10:46:29 -0700201static void
202vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
203 session_handle_t handle, int retval)
204{
205 app_session_evt_t _app_evt, *app_evt = &_app_evt;
206 session_accepted_reply_msg_t *rmp;
207 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
208 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
209 rmp->handle = handle;
210 rmp->context = context;
211 rmp->retval = retval;
212 app_send_ctrl_evt_to_vpp (mq, app_evt);
213}
214
Florin Coras99368312018-08-02 10:45:44 -0700215static void
216vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
217 session_handle_t handle, int retval)
218{
219 app_session_evt_t _app_evt, *app_evt = &_app_evt;
220 session_disconnected_reply_msg_t *rmp;
221 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
222 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
223 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
224 rmp->handle = handle;
225 rmp->context = context;
226 rmp->retval = retval;
227 app_send_ctrl_evt_to_vpp (mq, app_evt);
228}
229
Florin Corasc9fbd662018-08-24 12:59:56 -0700230static void
231vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
232 session_handle_t handle, int retval)
233{
234 app_session_evt_t _app_evt, *app_evt = &_app_evt;
235 session_reset_reply_msg_t *rmp;
236 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
237 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
238 rmp->handle = handle;
239 rmp->context = context;
240 rmp->retval = retval;
241 app_send_ctrl_evt_to_vpp (mq, app_evt);
242}
243
Florin Coras30e79c22019-01-02 19:31:22 -0800244void
245vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
246 u32 wrk_index)
247{
248 app_session_evt_t _app_evt, *app_evt = &_app_evt;
249 session_worker_update_msg_t *mp;
250 svm_msg_q_t *mq;
251
252 mq = vcl_session_vpp_evt_q (wrk, s);
253 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_WORKER_UPDATE);
254 mp = (session_worker_update_msg_t *) app_evt->evt->data;
255 mp->client_index = wrk->my_client_index;
256 mp->handle = s->vpp_handle;
257 mp->req_wrk_index = wrk->vpp_wrk_index;
258 mp->wrk_index = wrk_index;
259 app_send_ctrl_evt_to_vpp (mq, app_evt);
260}
261
Florin Coras54693d22018-07-17 10:46:29 -0700262static u32
Florin Coras134a9962018-08-28 11:32:04 -0700263vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700264{
265 vcl_session_t *session, *listen_session;
266 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras134a9962018-08-28 11:32:04 -0700267 u32 vpp_wrk_index;
Florin Coras99368312018-08-02 10:45:44 -0700268 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700269
Florin Coras134a9962018-08-28 11:32:04 -0700270 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700271
Florin Coras134a9962018-08-28 11:32:04 -0700272 listen_session = vcl_session_table_lookup_listener (wrk,
273 mp->listener_handle);
Florin Coras54693d22018-07-17 10:46:29 -0700274 if (!listen_session)
275 {
276 svm_msg_q_t *evt_q;
277 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
278 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
279 "unknown vpp listener handle %llx",
280 getpid (), mp->listener_handle);
281 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
282 VNET_API_ERROR_INVALID_ARGUMENT);
Florin Coras134a9962018-08-28 11:32:04 -0700283 vcl_session_free (wrk, session);
Florin Coras54693d22018-07-17 10:46:29 -0700284 return VCL_INVALID_SESSION_INDEX;
285 }
286
Florin Coras54693d22018-07-17 10:46:29 -0700287 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
288 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
289
290 if (mp->server_event_queue_address)
291 {
292 session->vpp_evt_q = uword_to_pointer (mp->client_event_queue_address,
293 svm_msg_q_t *);
294 session->our_evt_q = uword_to_pointer (mp->server_event_queue_address,
295 svm_msg_q_t *);
Florin Corasd85de682018-11-29 17:02:29 -0800296 if (vcl_wait_for_segment (mp->segment_handle))
297 {
298 clib_warning ("segment for session %u couldn't be mounted!",
299 session->session_index);
300 return VCL_INVALID_SESSION_INDEX;
301 }
Florin Coras134a9962018-08-28 11:32:04 -0700302 rx_fifo->master_session_index = session->session_index;
303 tx_fifo->master_session_index = session->session_index;
Florin Coras21795132018-09-09 09:40:51 -0700304 rx_fifo->master_thread_index = vcl_get_worker_index ();
305 tx_fifo->master_thread_index = vcl_get_worker_index ();
Florin Coras134a9962018-08-28 11:32:04 -0700306 vec_validate (wrk->vpp_event_queues, 0);
Florin Coras99368312018-08-02 10:45:44 -0700307 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
Florin Coras134a9962018-08-28 11:32:04 -0700308 wrk->vpp_event_queues[0] = evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700309 }
310 else
311 {
312 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
313 svm_msg_q_t *);
Florin Coras134a9962018-08-28 11:32:04 -0700314 rx_fifo->client_session_index = session->session_index;
315 tx_fifo->client_session_index = session->session_index;
Florin Coras21795132018-09-09 09:40:51 -0700316 rx_fifo->client_thread_index = vcl_get_worker_index ();
317 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras99368312018-08-02 10:45:44 -0700318 vpp_wrk_index = tx_fifo->master_thread_index;
Florin Coras134a9962018-08-28 11:32:04 -0700319 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
320 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700321 }
322
323 session->vpp_handle = mp->handle;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800324 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras54693d22018-07-17 10:46:29 -0700325 session->client_context = mp->context;
326 session->rx_fifo = rx_fifo;
327 session->tx_fifo = tx_fifo;
328
329 session->session_state = STATE_ACCEPT;
330 session->transport.rmt_port = mp->port;
331 session->transport.is_ip4 = mp->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500332 clib_memcpy_fast (&session->transport.rmt_ip, mp->ip,
333 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700334
Florin Coras134a9962018-08-28 11:32:04 -0700335 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700336 session->transport.lcl_port = listen_session->transport.lcl_port;
337 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700338 session->session_type = listen_session->session_type;
339 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
Florin Coras54693d22018-07-17 10:46:29 -0700340
341 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
Florin Coras134a9962018-08-28 11:32:04 -0700342 " address %U port %d queue %p!", getpid (), mp->handle,
343 session->session_index,
Florin Coras54693d22018-07-17 10:46:29 -0700344 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
345 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
346 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
347 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
348
Florin Coras134a9962018-08-28 11:32:04 -0700349 return session->session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700350}
351
352static u32
Florin Coras134a9962018-08-28 11:32:04 -0700353vcl_session_connected_handler (vcl_worker_t * wrk,
354 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700355{
Florin Coras99368312018-08-02 10:45:44 -0700356 u32 session_index, vpp_wrk_index;
Florin Coras54693d22018-07-17 10:46:29 -0700357 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700358 vcl_session_t *session = 0;
359 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700360
361 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700362 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700363 if (!session)
364 {
365 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
366 "Invalid session index (%u)!",
367 getpid (), mp->handle, session_index);
368 return VCL_INVALID_SESSION_INDEX;
369 }
Florin Coras54693d22018-07-17 10:46:29 -0700370 if (mp->retval)
371 {
Florin Coras070453d2018-08-24 17:04:27 -0700372 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed! %U", getpid (),
Florin Coras21795132018-09-09 09:40:51 -0700373 session_index, format_api_error, ntohl (mp->retval));
Florin Coras070453d2018-08-24 17:04:27 -0700374 session->session_state = STATE_FAILED;
375 session->vpp_handle = mp->handle;
376 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700377 }
378
Florin Coras460dce62018-07-27 05:45:06 -0700379 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
380 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Corasd85de682018-11-29 17:02:29 -0800381 if (vcl_wait_for_segment (mp->segment_handle))
382 {
383 clib_warning ("segment for session %u couldn't be mounted!",
384 session->session_index);
385 return VCL_INVALID_SESSION_INDEX;
386 }
387
Florin Coras460dce62018-07-27 05:45:06 -0700388 rx_fifo->client_session_index = session_index;
389 tx_fifo->client_session_index = session_index;
Florin Coras21795132018-09-09 09:40:51 -0700390 rx_fifo->client_thread_index = vcl_get_worker_index ();
391 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras460dce62018-07-27 05:45:06 -0700392
Florin Coras54693d22018-07-17 10:46:29 -0700393 if (mp->client_event_queue_address)
394 {
395 session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address,
396 svm_msg_q_t *);
397 session->our_evt_q = uword_to_pointer (mp->client_event_queue_address,
398 svm_msg_q_t *);
Florin Coras99368312018-08-02 10:45:44 -0700399
Florin Coras134a9962018-08-28 11:32:04 -0700400 vec_validate (wrk->vpp_event_queues, 0);
Florin Coras99368312018-08-02 10:45:44 -0700401 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
Florin Coras134a9962018-08-28 11:32:04 -0700402 wrk->vpp_event_queues[0] = evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700403 }
404 else
Florin Coras99368312018-08-02 10:45:44 -0700405 {
406 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
407 svm_msg_q_t *);
408 vpp_wrk_index = tx_fifo->master_thread_index;
Florin Coras134a9962018-08-28 11:32:04 -0700409 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
410 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -0700411 }
Florin Coras54693d22018-07-17 10:46:29 -0700412
Florin Coras54693d22018-07-17 10:46:29 -0700413 session->rx_fifo = rx_fifo;
414 session->tx_fifo = tx_fifo;
415 session->vpp_handle = mp->handle;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800416 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras54693d22018-07-17 10:46:29 -0700417 session->transport.is_ip4 = mp->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500418 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
419 sizeof (session->transport.lcl_ip));
Florin Coras54693d22018-07-17 10:46:29 -0700420 session->transport.lcl_port = mp->lcl_port;
421 session->session_state = STATE_CONNECT;
422
423 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800424 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700425
426 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
427 "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
428 getpid (), mp->handle, session_index, session->rx_fifo,
429 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700430
Florin Coras54693d22018-07-17 10:46:29 -0700431 return session_index;
432}
433
Florin Coras3c7d4f92018-12-14 11:28:43 -0800434static int
435vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
436{
437 vcl_session_msg_t *accepted_msg;
438 int i;
439
440 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
441 {
442 accepted_msg = &session->accept_evts_fifo[i];
443 if (accepted_msg->accepted_msg.handle == handle)
444 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800445 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800446 return 1;
447 }
448 }
449 return 0;
450}
451
Florin Corasc9fbd662018-08-24 12:59:56 -0700452static u32
Florin Coras134a9962018-08-28 11:32:04 -0700453vcl_session_reset_handler (vcl_worker_t * wrk,
454 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700455{
456 vcl_session_t *session;
457 u32 sid;
458
Florin Coras134a9962018-08-28 11:32:04 -0700459 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
460 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700461 if (!session)
462 {
463 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
464 return VCL_INVALID_SESSION_INDEX;
465 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800466
467 /* Caught a reset before actually accepting the session */
468 if (session->session_state == STATE_LISTEN)
469 {
470
471 if (!vcl_flag_accepted_session (session, reset_msg->handle,
472 VCL_ACCEPTED_F_RESET))
473 VDBG (0, "session was not accepted!");
474 return VCL_INVALID_SESSION_INDEX;
475 }
476
477 session->session_state = STATE_DISCONNECT;
478 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700479 return sid;
480}
481
Florin Coras60116992018-08-27 09:52:18 -0700482static u32
Florin Coras134a9962018-08-28 11:32:04 -0700483vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700484{
485 vcl_session_t *session;
486 u32 sid = mp->context;
487
Florin Coras134a9962018-08-28 11:32:04 -0700488 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700489 if (mp->retval)
490 {
Florin Corasd85de682018-11-29 17:02:29 -0800491 VERR ("vpp handle 0x%llx, sid %u: bind failed: %U", mp->handle, sid,
492 format_api_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700493 if (session)
494 {
495 session->session_state = STATE_FAILED;
496 session->vpp_handle = mp->handle;
497 return sid;
498 }
499 else
500 {
501 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
502 "Invalid session index (%u)!",
503 getpid (), mp->handle, sid);
504 return VCL_INVALID_SESSION_INDEX;
505 }
506 }
507
508 session->vpp_handle = mp->handle;
509 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500510 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
511 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700512 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700513 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Coras60116992018-08-27 09:52:18 -0700514 session->session_state = STATE_LISTEN;
515
Florin Coras5f45e012019-01-23 09:21:30 -0800516 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
517 vec_validate (wrk->vpp_event_queues, 0);
518 wrk->vpp_event_queues[0] = session->vpp_evt_q;
519
Florin Coras60116992018-08-27 09:52:18 -0700520 if (session->is_dgram)
521 {
522 svm_fifo_t *rx_fifo, *tx_fifo;
523 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
524 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
525 rx_fifo->client_session_index = sid;
526 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
527 tx_fifo->client_session_index = sid;
528 session->rx_fifo = rx_fifo;
529 session->tx_fifo = tx_fifo;
530 }
531
Florin Coras05ecfcc2018-12-12 18:19:39 -0800532 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700533 return sid;
534}
535
Florin Corasdfae9f92019-02-20 19:48:31 -0800536static void
537vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
538{
539 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
540 vcl_session_t *s;
541
542 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
543 if (!s || s->session_state != STATE_DISCONNECT)
544 {
545 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
546 return;
547 }
548
549 if (mp->retval)
550 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
551 s->session_index, mp->handle, format_api_error, ntohl (mp->retval));
552
553 if (mp->context != wrk->wrk_index)
554 VDBG (0, "wrong context");
555
556 vcl_session_table_del_vpp_handle (wrk, mp->handle);
557 vcl_session_free (wrk, s);
558}
559
Florin Coras3c7d4f92018-12-14 11:28:43 -0800560static vcl_session_t *
561vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
562{
563 vcl_session_msg_t *vcl_msg;
564 vcl_session_t *session;
565
566 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
567 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800568 VWRN ("session overlap handle %lu state %u!", msg->handle,
569 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800570
571 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
572 if (!session)
573 {
574 VERR ("couldn't find listen session: listener handle %llx",
575 msg->listener_handle);
576 return 0;
577 }
578
579 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
580 vcl_msg->accepted_msg = *msg;
581 /* Session handle points to listener until fully accepted by app */
582 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
583
584 return session;
585}
586
587static vcl_session_t *
588vcl_session_disconnected_handler (vcl_worker_t * wrk,
589 session_disconnected_msg_t * msg)
590{
591 vcl_session_t *session;
592
593 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
594 if (!session)
595 {
596 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
597 return 0;
598 }
599
600 /* Caught a disconnect before actually accepting the session */
601 if (session->session_state == STATE_LISTEN)
602 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800603 if (!vcl_flag_accepted_session (session, msg->handle,
604 VCL_ACCEPTED_F_CLOSED))
605 VDBG (0, "session was not accepted!");
606 return 0;
607 }
608
609 session->session_state = STATE_VPP_CLOSING;
610 return session;
611}
612
Florin Coras30e79c22019-01-02 19:31:22 -0800613static void
614vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
615{
616 session_req_worker_update_msg_t *msg;
617 vcl_session_t *s;
618
619 msg = (session_req_worker_update_msg_t *) data;
620 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
621 if (!s)
622 return;
623
624 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
625}
626
627static void
628vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
629{
630 session_worker_update_reply_msg_t *msg;
631 vcl_session_t *s;
632
633 msg = (session_worker_update_reply_msg_t *) data;
634 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
635 if (!s)
636 {
637 VDBG (0, "unknown handle 0x%llx", msg->handle);
638 return;
639 }
640 if (vcl_wait_for_segment (msg->segment_handle))
641 {
642 clib_warning ("segment for session %u couldn't be mounted!",
643 s->session_index);
644 return;
645 }
Florin Coras30e79c22019-01-02 19:31:22 -0800646
Florin Coras5f45e012019-01-23 09:21:30 -0800647 if (s->rx_fifo)
648 {
649 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
650 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
651 s->rx_fifo->client_session_index = s->session_index;
652 s->tx_fifo->client_session_index = s->session_index;
653 s->rx_fifo->client_thread_index = wrk->wrk_index;
654 s->tx_fifo->client_thread_index = wrk->wrk_index;
655 }
Florin Coras30e79c22019-01-02 19:31:22 -0800656 s->session_state = STATE_UPDATED;
657
Florin Coras30e79c22019-01-02 19:31:22 -0800658 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
659 s->vpp_handle, wrk->wrk_index);
660}
661
Florin Coras86f04502018-09-12 16:08:01 -0700662static int
663vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700664{
Florin Coras54693d22018-07-17 10:46:29 -0700665 session_disconnected_msg_t *disconnected_msg;
Florin Coras54693d22018-07-17 10:46:29 -0700666 vcl_session_t *session;
Florin Coras54693d22018-07-17 10:46:29 -0700667
668 switch (e->event_type)
669 {
670 case FIFO_EVENT_APP_RX:
Florin Coras86f04502018-09-12 16:08:01 -0700671 case FIFO_EVENT_APP_TX:
672 case SESSION_IO_EVT_CT_RX:
673 case SESSION_IO_EVT_CT_TX:
674 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -0700675 break;
676 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -0800677 vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700678 break;
679 case SESSION_CTRL_EVT_CONNECTED:
Florin Coras134a9962018-08-28 11:32:04 -0700680 vcl_session_connected_handler (wrk,
681 (session_connected_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700682 break;
683 case SESSION_CTRL_EVT_DISCONNECTED:
684 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800685 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
Florin Corasc9fbd662018-08-24 12:59:56 -0700686 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800687 break;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800688 VDBG (0, "disconnected session %u [0x%llx]", session->session_index,
689 session->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700690 break;
691 case SESSION_CTRL_EVT_RESET:
Florin Coras134a9962018-08-28 11:32:04 -0700692 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -0700693 break;
694 case SESSION_CTRL_EVT_BOUND:
Florin Coras134a9962018-08-28 11:32:04 -0700695 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700696 break;
Florin Corasdfae9f92019-02-20 19:48:31 -0800697 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
698 vcl_session_unlisten_reply_handler (wrk, e->data);
699 break;
Florin Coras30e79c22019-01-02 19:31:22 -0800700 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
701 vcl_session_req_worker_update_handler (wrk, e->data);
702 break;
703 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
704 vcl_session_worker_update_reply_handler (wrk, e->data);
705 break;
Florin Coras54693d22018-07-17 10:46:29 -0700706 default:
707 clib_warning ("unhandled %u", e->event_type);
708 }
709 return VPPCOM_OK;
710}
711
Florin Coras30e79c22019-01-02 19:31:22 -0800712static int
Florin Coras697faea2018-06-27 17:10:49 -0700713vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -0800714 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -0700715 f64 wait_for_time)
716{
Florin Coras134a9962018-08-28 11:32:04 -0700717 vcl_worker_t *wrk = vcl_worker_get_current ();
718 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -0700719 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -0700720 svm_msg_q_msg_t msg;
721 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -0400722
Florin Coras697faea2018-06-27 17:10:49 -0700723 do
Dave Wallace543852a2017-08-03 02:11:34 -0400724 {
Florin Coras134a9962018-08-28 11:32:04 -0700725 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700726 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -0700727 {
Florin Coras070453d2018-08-24 17:04:27 -0700728 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -0700729 }
730 if (session->session_state & state)
731 {
Florin Coras697faea2018-06-27 17:10:49 -0700732 return VPPCOM_OK;
733 }
734 if (session->session_state & STATE_FAILED)
735 {
Florin Coras697faea2018-06-27 17:10:49 -0700736 return VPPCOM_ECONNREFUSED;
737 }
Florin Coras54693d22018-07-17 10:46:29 -0700738
Florin Coras134a9962018-08-28 11:32:04 -0700739 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -0800740 {
741 usleep (100);
742 continue;
743 }
Florin Coras134a9962018-08-28 11:32:04 -0700744 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -0700745 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -0700746 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800747 }
Florin Coras134a9962018-08-28 11:32:04 -0700748 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800749
Florin Coras05ecfcc2018-12-12 18:19:39 -0800750 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -0700751 vppcom_session_state_str (state));
752 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -0400753
Florin Coras697faea2018-06-27 17:10:49 -0700754 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -0500755}
756
Florin Coras30e79c22019-01-02 19:31:22 -0800757static void
758vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
759{
Florin Coras288eaab2019-02-03 15:26:14 -0800760 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -0800761 vcl_session_t *s;
762 u32 *sip;
763
764 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
765 return;
766
767 vec_foreach (sip, wrk->pending_session_wrk_updates)
768 {
769 s = vcl_session_get (wrk, *sip);
770 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
771 state = s->session_state;
772 vppcom_wait_for_session_state_change (s->session_index, STATE_UPDATED, 5);
773 s->session_state = state;
774 }
775 vec_reset_length (wrk->pending_session_wrk_updates);
776}
777
Florin Corasf9240dc2019-01-15 08:03:17 -0800778void
Florin Coras30e79c22019-01-02 19:31:22 -0800779vcl_flush_mq_events (void)
780{
781 vcl_worker_t *wrk = vcl_worker_get_current ();
782 svm_msg_q_msg_t *msg;
783 session_event_t *e;
784 svm_msg_q_t *mq;
785 int i;
786
787 mq = wrk->app_event_queue;
788 svm_msg_q_lock (mq);
789 vcl_mq_dequeue_batch (wrk, mq);
790 svm_msg_q_unlock (mq);
791
792 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
793 {
794 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
795 e = svm_msg_q_msg_data (mq, msg);
796 vcl_handle_mq_event (wrk, e);
797 svm_msg_q_free_msg (mq, msg);
798 }
799 vec_reset_length (wrk->mq_msg_vector);
800 vcl_handle_pending_wrk_updates (wrk);
801}
802
Florin Coras697faea2018-06-27 17:10:49 -0700803static int
804vppcom_app_session_enable (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400805{
Florin Coras697faea2018-06-27 17:10:49 -0700806 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400807
Florin Coras697faea2018-06-27 17:10:49 -0700808 if (vcm->app_state != STATE_APP_ENABLED)
809 {
810 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
Florin Coras134a9962018-08-28 11:32:04 -0700811 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
Florin Coras697faea2018-06-27 17:10:49 -0700812 if (PREDICT_FALSE (rv))
813 {
814 VDBG (0, "VCL<%d>: application session enable timed out! "
815 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
816 return rv;
817 }
818 }
819 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400820}
821
Florin Coras697faea2018-06-27 17:10:49 -0700822static int
823vppcom_app_attach (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400824{
Florin Coras697faea2018-06-27 17:10:49 -0700825 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400826
Florin Coras697faea2018-06-27 17:10:49 -0700827 vppcom_app_send_attach ();
Florin Coras134a9962018-08-28 11:32:04 -0700828 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
Florin Coras697faea2018-06-27 17:10:49 -0700829 if (PREDICT_FALSE (rv))
830 {
831 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
832 getpid (), rv, vppcom_retval_str (rv));
833 return rv;
834 }
Dave Wallace543852a2017-08-03 02:11:34 -0400835
Florin Coras697faea2018-06-27 17:10:49 -0700836 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400837}
838
839static int
Florin Corasab2f6db2018-08-31 14:31:41 -0700840vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400841{
Florin Coras134a9962018-08-28 11:32:04 -0700842 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -0700843 vcl_session_t *session = 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500844 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400845
Florin Corasab2f6db2018-08-31 14:31:41 -0700846 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700847 if (!session)
848 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500849
850 vpp_handle = session->vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500851 session->vpp_handle = ~0;
Florin Coras7e12d942018-06-27 14:32:43 -0700852 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500853
Florin Coras2d675d72019-01-28 15:54:27 -0800854 VDBG (1, "vpp handle 0x%llx, sid %u: sending unbind msg! new state"
855 " 0x%x (%s)", vpp_handle, session_handle, STATE_DISCONNECT,
Florin Coras0d427d82018-06-27 03:24:07 -0700856 vppcom_session_state_str (STATE_DISCONNECT));
857 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras2d675d72019-01-28 15:54:27 -0800858 vppcom_send_unbind_sock (wrk, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500859
Florin Coras070453d2018-08-24 17:04:27 -0700860 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400861}
862
Florin Coras697faea2018-06-27 17:10:49 -0700863static int
Florin Corasab2f6db2018-08-31 14:31:41 -0700864vppcom_session_disconnect (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400865{
Florin Coras134a9962018-08-28 11:32:04 -0700866 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -0700867 svm_msg_q_t *vpp_evt_q;
Florin Coras7e12d942018-06-27 14:32:43 -0700868 vcl_session_t *session;
Florin Coras288eaab2019-02-03 15:26:14 -0800869 vcl_session_state_t state;
Florin Coras99368312018-08-02 10:45:44 -0700870 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400871
Florin Corasab2f6db2018-08-31 14:31:41 -0700872 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras21795132018-09-09 09:40:51 -0700873 if (!session)
874 return VPPCOM_EBADFD;
875
Dave Wallace4878cbe2017-11-21 03:45:09 -0500876 vpp_handle = session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -0700877 state = session->session_state;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500878
Florin Coras0d427d82018-06-27 03:24:07 -0700879 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
Florin Corasab2f6db2018-08-31 14:31:41 -0700880 vpp_handle, session_handle, state, vppcom_session_state_str (state));
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400881
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800882 if (PREDICT_FALSE (state & STATE_LISTEN))
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400883 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500884 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500885 "Cannot disconnect a listen socket!",
Florin Corasab2f6db2018-08-31 14:31:41 -0700886 getpid (), vpp_handle, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700887 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500888 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400889
Florin Coras3c7d4f92018-12-14 11:28:43 -0800890 if (state & STATE_VPP_CLOSING)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500891 {
Florin Coras134a9962018-08-28 11:32:04 -0700892 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
Florin Coras47c40e22018-11-26 17:01:36 -0800893 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index,
Florin Coras99368312018-08-02 10:45:44 -0700894 vpp_handle, 0);
Florin Coras0d427d82018-06-27 03:24:07 -0700895 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
Florin Corasab2f6db2018-08-31 14:31:41 -0700896 "REPLY...", getpid (), vpp_handle, session_handle);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400897 }
898 else
Dave Wallace227867f2017-11-13 21:21:53 -0500899 {
Florin Coras0d427d82018-06-27 03:24:07 -0700900 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
Florin Corasab2f6db2018-08-31 14:31:41 -0700901 getpid (), vpp_handle, session_handle);
902 vppcom_send_disconnect_session (vpp_handle);
Dave Wallace227867f2017-11-13 21:21:53 -0500903 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400904
Florin Coras070453d2018-08-24 17:04:27 -0700905 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400906}
907
Florin Coras940f78f2018-11-30 12:11:20 -0800908/**
909 * Handle app exit
910 *
911 * Notify vpp of the disconnect and mark the worker as free. If we're the
912 * last worker, do a full cleanup otherwise, since we're probably a forked
913 * child, avoid syscalls as much as possible. We might've lost privileges.
914 */
915void
916vppcom_app_exit (void)
917{
918 if (!pool_elts (vcm->workers))
919 return;
Florin Coras01f3f892018-12-02 12:45:53 -0800920 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
921 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -0800922 vcl_elog_stop (vcm);
923 if (vec_len (vcm->workers) == 1)
924 vl_client_disconnect_from_vlib ();
925 else
Florin Coraseaec2a62018-12-04 16:34:05 -0800926 vl_client_send_disconnect (1 /* vpp should cleanup */ );
Florin Coras940f78f2018-11-30 12:11:20 -0800927}
928
Dave Wallace543852a2017-08-03 02:11:34 -0400929/*
930 * VPPCOM Public API functions
931 */
932int
933vppcom_app_create (char *app_name)
934{
Dave Wallace543852a2017-08-03 02:11:34 -0400935 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -0400936 int rv;
937
Florin Coras47c40e22018-11-26 17:01:36 -0800938 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -0400939 {
Florin Coras955bfbb2018-12-04 13:43:45 -0800940 VDBG (1, "already initialized");
941 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -0400942 }
943
Florin Coras47c40e22018-11-26 17:01:36 -0800944 vcm->is_init = 1;
945 vppcom_cfg (&vcm->cfg);
946 vcl_cfg = &vcm->cfg;
947
948 vcm->main_cpu = pthread_self ();
949 vcm->main_pid = getpid ();
950 vcm->app_name = format (0, "%s", app_name);
951 vppcom_init_error_string_table ();
Florin Corasadc74d72018-12-02 13:36:00 -0800952 svm_fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
Florin Coras47c40e22018-11-26 17:01:36 -0800953 20 /* timeout in secs */ );
954 pool_alloc (vcm->workers, vcl_cfg->max_workers);
955 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -0800956 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -0800957 atexit (vppcom_app_exit);
Florin Coras47c40e22018-11-26 17:01:36 -0800958
959 /* Allocate default worker */
960 vcl_worker_alloc_and_init ();
961
962 /* API hookup and connect to VPP */
963 vppcom_api_hookup ();
964 vcl_elog_init (vcm);
965 vcm->app_state = STATE_APP_START;
966 rv = vppcom_connect_to_vpp (app_name);
967 if (rv)
Dave Wallace543852a2017-08-03 02:11:34 -0400968 {
Florin Coras47c40e22018-11-26 17:01:36 -0800969 VERR ("couldn't connect to VPP!");
970 return rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400971 }
Florin Coras47c40e22018-11-26 17:01:36 -0800972 VDBG (0, "sending session enable");
973 rv = vppcom_app_session_enable ();
974 if (rv)
975 {
976 VERR ("vppcom_app_session_enable() failed!");
977 return rv;
978 }
979
980 VDBG (0, "sending app attach");
981 rv = vppcom_app_attach ();
982 if (rv)
983 {
984 VERR ("vppcom_app_attach() failed!");
985 return rv;
986 }
987
988 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
989 vcm->workers[0].my_client_index, vcm->workers[0].my_client_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400990
991 return VPPCOM_OK;
992}
993
994void
995vppcom_app_destroy (void)
996{
Dave Wallace543852a2017-08-03 02:11:34 -0400997 int rv;
Dave Wallacede910062018-03-20 09:22:13 -0400998 f64 orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -0400999
Florin Coras940f78f2018-11-30 12:11:20 -08001000 if (!pool_elts (vcm->workers))
1001 return;
1002
Florin Coras0d427d82018-06-27 03:24:07 -07001003 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001004
Florin Coras940f78f2018-11-30 12:11:20 -08001005 if (pool_elts (vcm->workers) == 1)
Florin Coras47c40e22018-11-26 17:01:36 -08001006 {
1007 vppcom_app_send_detach ();
1008 orig_app_timeout = vcm->cfg.app_timeout;
1009 vcm->cfg.app_timeout = 2.0;
1010 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
1011 vcm->cfg.app_timeout = orig_app_timeout;
1012 if (PREDICT_FALSE (rv))
1013 VDBG (0, "application detach timed out! returning %d (%s)", rv,
1014 vppcom_retval_str (rv));
Florin Coras940f78f2018-11-30 12:11:20 -08001015 vec_free (vcm->app_name);
Florin Coras01f3f892018-12-02 12:45:53 -08001016 vcl_worker_cleanup (vcl_worker_get_current (), 0 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001017 }
1018 else
1019 {
Florin Coras01f3f892018-12-02 12:45:53 -08001020 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001021 }
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001022
Florin Coras01f3f892018-12-02 12:45:53 -08001023 vcl_set_worker_index (~0);
Florin Coras0d427d82018-06-27 03:24:07 -07001024 vcl_elog_stop (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -04001025 vl_client_disconnect_from_vlib ();
Dave Wallace543852a2017-08-03 02:11:34 -04001026}
1027
1028int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001029vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001030{
Florin Coras134a9962018-08-28 11:32:04 -07001031 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001032 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001033
Florin Coras134a9962018-08-28 11:32:04 -07001034 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001035
Florin Coras7e12d942018-06-27 14:32:43 -07001036 session->session_type = proto;
1037 session->session_state = STATE_START;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001038 session->vpp_handle = ~0;
Florin Coras460dce62018-07-27 05:45:06 -07001039 session->is_dgram = proto == VPPCOM_PROTO_UDP;
Dave Wallace543852a2017-08-03 02:11:34 -04001040
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001041 if (is_nonblocking)
1042 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001043
Florin Coras7e12d942018-06-27 14:32:43 -07001044 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1045 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001046
Florin Coras053a0e42018-11-13 15:52:38 -08001047 VDBG (0, "created sid %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001048
Florin Coras134a9962018-08-28 11:32:04 -07001049 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001050}
1051
1052int
Florin Corasf9240dc2019-01-15 08:03:17 -08001053vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session,
1054 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001055{
Florin Coras288eaab2019-02-03 15:26:14 -08001056 vcl_session_state_t state;
Florin Coras134a9962018-08-28 11:32:04 -07001057 u32 next_sh, vep_sh;
1058 int rv = VPPCOM_OK;
1059 u64 vpp_handle;
Florin Corasf9240dc2019-01-15 08:03:17 -08001060 u8 is_vep;
Florin Coras47c40e22018-11-26 17:01:36 -08001061
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001062 is_vep = session->is_vep;
Florin Coras134a9962018-08-28 11:32:04 -07001063 next_sh = session->vep.next_sh;
1064 vep_sh = session->vep.vep_sh;
Florin Coras7e12d942018-06-27 14:32:43 -07001065 state = session->session_state;
Dave Wallaceee45d412017-11-24 21:44:06 -05001066 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001067
Florin Corasf9240dc2019-01-15 08:03:17 -08001068 VDBG (1, "session %u [0x%llx] closing", session->session_index, vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001069
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001070 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04001071 {
Florin Coras134a9962018-08-28 11:32:04 -07001072 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001073 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001074 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001075 if (PREDICT_FALSE (rv < 0))
Florin Coras47c40e22018-11-26 17:01:36 -08001076 VDBG (0, "vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL vep_idx %u"
1077 " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv,
1078 vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001079
Florin Coras134a9962018-08-28 11:32:04 -07001080 next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001081 }
1082 }
1083 else
1084 {
Florin Coras47c40e22018-11-26 17:01:36 -08001085 if (session->is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001086 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001087 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001088 if (rv < 0)
Florin Corasf9240dc2019-01-15 08:03:17 -08001089 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
1090 "failed! rv %d (%s)", session->session_index, vpp_handle,
1091 vep_sh, rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001092 }
1093
Florin Coras47c40e22018-11-26 17:01:36 -08001094 if (!do_disconnect)
Florin Coras30e79c22019-01-02 19:31:22 -08001095 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08001096 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Corasf9240dc2019-01-15 08:03:17 -08001097 session->session_index, vpp_handle);
Florin Coras30e79c22019-01-02 19:31:22 -08001098 goto cleanup;
1099 }
Florin Coras47c40e22018-11-26 17:01:36 -08001100
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001101 if (state & STATE_LISTEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001102 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001103 rv = vppcom_session_unbind (sh);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001104 if (PREDICT_FALSE (rv < 0))
Florin Corasf9240dc2019-01-15 08:03:17 -08001105 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
1106 "rv %d (%s)", session->session_index, vpp_handle, rv,
Florin Coras47c40e22018-11-26 17:01:36 -08001107 vppcom_retval_str (rv));
Florin Corasdfae9f92019-02-20 19:48:31 -08001108 return rv;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001109 }
Florin Coras070453d2018-08-24 17:04:27 -07001110 else if (state & STATE_OPEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001111 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001112 rv = vppcom_session_disconnect (sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001113 if (PREDICT_FALSE (rv < 0))
Florin Corasf9240dc2019-01-15 08:03:17 -08001114 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
1115 " rv %d (%s)", session->session_index, vpp_handle,
1116 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001117 }
Florin Corasb0f662f2018-12-27 14:51:46 -08001118 else if (state == STATE_DISCONNECT)
1119 {
1120 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, session);
1121 vcl_send_session_reset_reply (mq, wrk->my_client_index,
1122 session->vpp_handle, 0);
1123 }
Dave Wallace19481612017-09-15 18:47:44 -04001124 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001125
Florin Coras99368312018-08-02 10:45:44 -07001126 if (vcl_session_is_ct (session))
1127 {
1128 vcl_cut_through_registration_t *ctr;
1129 uword mq_addr;
1130
1131 mq_addr = pointer_to_uword (session->our_evt_q);
Florin Coras134a9962018-08-28 11:32:04 -07001132 ctr = vcl_ct_registration_lock_and_lookup (wrk, mq_addr);
Florin Coras99368312018-08-02 10:45:44 -07001133 ASSERT (ctr);
1134 if (ctr->epoll_evt_conn_index != ~0)
Florin Coras134a9962018-08-28 11:32:04 -07001135 vcl_mq_epoll_del_evfd (wrk, ctr->epoll_evt_conn_index);
Florin Coras99368312018-08-02 10:45:44 -07001136 VDBG (0, "Removing ct registration %u",
Florin Coras134a9962018-08-28 11:32:04 -07001137 vcl_ct_registration_index (wrk, ctr));
1138 vcl_ct_registration_del (wrk, ctr);
1139 vcl_ct_registration_lookup_del (wrk, mq_addr);
1140 vcl_ct_registration_unlock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07001141 }
Florin Coras54693d22018-07-17 10:46:29 -07001142
Florin Coras0ef8ef22019-01-18 08:37:13 -08001143 VDBG (0, "session %u [0x%llx] removed", session->session_index, vpp_handle);
1144
Florin Corasf9240dc2019-01-15 08:03:17 -08001145cleanup:
Florin Coras30e79c22019-01-02 19:31:22 -08001146 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
Florin Coras134a9962018-08-28 11:32:04 -07001147 vcl_session_free (wrk, session);
Florin Coras0d427d82018-06-27 03:24:07 -07001148 vcl_evt (VCL_EVT_CLOSE, session, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001149
Dave Wallace543852a2017-08-03 02:11:34 -04001150 return rv;
1151}
1152
1153int
Florin Corasf9240dc2019-01-15 08:03:17 -08001154vppcom_session_close (uint32_t session_handle)
1155{
1156 vcl_worker_t *wrk = vcl_worker_get_current ();
1157 vcl_session_t *session;
1158
1159 session = vcl_session_get_w_handle (wrk, session_handle);
1160 if (!session)
1161 return VPPCOM_EBADFD;
1162 return vcl_session_cleanup (wrk, session, session_handle,
1163 1 /* do_disconnect */ );
1164}
1165
1166int
Florin Coras134a9962018-08-28 11:32:04 -07001167vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001168{
Florin Coras134a9962018-08-28 11:32:04 -07001169 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001170 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001171
1172 if (!ep || !ep->ip)
1173 return VPPCOM_EINVAL;
1174
Florin Coras134a9962018-08-28 11:32:04 -07001175 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001176 if (!session)
1177 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001178
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001179 if (session->is_vep)
1180 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001181 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
Florin Coras134a9962018-08-28 11:32:04 -07001182 "bind to an epoll session!", getpid (), session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001183 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001184 }
1185
Florin Coras7e12d942018-06-27 14:32:43 -07001186 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001187 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001188 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1189 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001190 else
Dave Barach178cf492018-11-13 16:34:13 -05001191 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1192 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001193 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001194
Florin Coras0d427d82018-06-27 03:24:07 -07001195 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
Florin Coras134a9962018-08-28 11:32:04 -07001196 "proto %s", getpid (), session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001197 session->transport.is_ip4 ? "IPv4" : "IPv6",
1198 format_ip46_address, &session->transport.lcl_ip,
1199 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1200 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001201 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001202 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001203
1204 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001205 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001206
Florin Coras070453d2018-08-24 17:04:27 -07001207 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001208}
1209
1210int
Florin Coras134a9962018-08-28 11:32:04 -07001211vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001212{
Florin Coras134a9962018-08-28 11:32:04 -07001213 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001214 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001215 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001216 int rv;
1217
Florin Coras134a9962018-08-28 11:32:04 -07001218 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001219 if (!listen_session || listen_session->is_vep)
Florin Coras070453d2018-08-24 17:04:27 -07001220 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001221
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001222 if (q_len == 0 || q_len == ~0)
1223 q_len = vcm->cfg.listen_queue_size;
1224
Dave Wallaceee45d412017-11-24 21:44:06 -05001225 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -07001226 if (listen_session->session_state & STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001227 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001228 VDBG (0, "session %u [0x%llx]: already in listen state!",
1229 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001230 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001231 }
1232
Florin Coras05ecfcc2018-12-12 18:19:39 -08001233 VDBG (0, "session %u [0x%llx]: sending vpp listen request...",
1234 listen_sh, listen_vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001235
Florin Coras070453d2018-08-24 17:04:27 -07001236 /*
1237 * Send listen request to vpp and wait for reply
1238 */
Florin Coras134a9962018-08-28 11:32:04 -07001239 vppcom_send_bind_sock (listen_session);
1240 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1241 STATE_LISTEN,
1242 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001243
Florin Coras070453d2018-08-24 17:04:27 -07001244 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001245 {
Florin Coras134a9962018-08-28 11:32:04 -07001246 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001247 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1248 listen_sh, listen_session->vpp_handle, rv,
1249 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001250 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001251 }
1252
Florin Coras070453d2018-08-24 17:04:27 -07001253 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001254}
1255
Ping Yu34a3a082018-11-30 19:16:17 -05001256int
1257vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
1258 uint32_t cert_len)
1259{
1260
1261 vcl_worker_t *wrk = vcl_worker_get_current ();
1262 vcl_session_t *session = 0;
1263
1264 session = vcl_session_get_w_handle (wrk, session_handle);
1265 if (!session)
1266 return VPPCOM_EBADFD;
1267
1268 if (cert_len == 0 || cert_len == ~0)
1269 return VPPCOM_EBADFD;
1270
1271 /*
1272 * Send listen request to vpp and wait for reply
1273 */
1274 vppcom_send_application_tls_cert_add (session, cert, cert_len);
1275
1276 return VPPCOM_OK;
1277
1278}
1279
1280int
1281vppcom_session_tls_add_key (uint32_t session_handle, char *key,
1282 uint32_t key_len)
1283{
1284
1285 vcl_worker_t *wrk = vcl_worker_get_current ();
1286 vcl_session_t *session = 0;
1287
1288 session = vcl_session_get_w_handle (wrk, session_handle);
1289 if (!session)
1290 return VPPCOM_EBADFD;
1291
1292 if (key_len == 0 || key_len == ~0)
1293 return VPPCOM_EBADFD;
1294
1295 /*
1296 * Send listen request to vpp and wait for reply
1297 */
1298 vppcom_send_application_tls_key_add (session, key, key_len);
1299
1300 return VPPCOM_OK;
1301
1302
1303}
1304
Florin Coras134a9962018-08-28 11:32:04 -07001305static int
1306validate_args_session_accept_ (vcl_worker_t * wrk,
1307 vcl_session_t * listen_session)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001308{
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001309 /* Input validation - expects spinlock on sessions_lockp */
1310 if (listen_session->is_vep)
1311 {
1312 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
Florin Coras134a9962018-08-28 11:32:04 -07001313 "epoll session!", getpid (),
1314 listen_session->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001315 return VPPCOM_EBADFD;
1316 }
1317
Florin Coras7e12d942018-06-27 14:32:43 -07001318 if (listen_session->session_state != STATE_LISTEN)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001319 {
1320 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1321 "not in listen state! state 0x%x (%s)", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07001322 listen_session->vpp_handle, listen_session->session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07001323 listen_session->session_state,
1324 vppcom_session_state_str (listen_session->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001325 return VPPCOM_EBADFD;
1326 }
1327 return VPPCOM_OK;
1328}
1329
1330int
Florin Coras134a9962018-08-28 11:32:04 -07001331vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001332 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001333{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001334 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001335 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001336 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001337 vcl_session_t *listen_session = 0;
1338 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001339 svm_msg_q_t *vpp_evt_q;
1340 vcl_session_msg_t *evt;
Dave Wallaceee45d412017-11-24 21:44:06 -05001341 u64 listen_vpp_handle;
Florin Coras54693d22018-07-17 10:46:29 -07001342 svm_msg_q_msg_t msg;
1343 session_event_t *e;
1344 u8 is_nonblocking;
1345 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001346
Florin Coras134a9962018-08-28 11:32:04 -07001347 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001348 if (!listen_session)
1349 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001350
Florin Coras134a9962018-08-28 11:32:04 -07001351 listen_session_index = listen_session->session_index;
1352 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001353 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001354
Florin Coras54693d22018-07-17 10:46:29 -07001355 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001356 {
Florin Coras54693d22018-07-17 10:46:29 -07001357 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001358 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001359 accepted_msg = evt->accepted_msg;
1360 goto handle;
1361 }
1362
1363 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1364 VCL_SESS_ATTR_NONBLOCK);
Florin Coras134a9962018-08-28 11:32:04 -07001365 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
Florin Coras54693d22018-07-17 10:46:29 -07001366 return VPPCOM_EAGAIN;
1367
1368 while (1)
1369 {
Florin Coras134a9962018-08-28 11:32:04 -07001370 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -07001371 return VPPCOM_EAGAIN;
1372
Florin Coras134a9962018-08-28 11:32:04 -07001373 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001374 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001375 {
Florin Coras54693d22018-07-17 10:46:29 -07001376 clib_warning ("discarded event: %u", e->event_type);
Florin Coras134a9962018-08-28 11:32:04 -07001377 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001378 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001379 }
Dave Barach178cf492018-11-13 16:34:13 -05001380 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
Florin Coras134a9962018-08-28 11:32:04 -07001381 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001382 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001383 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001384
Florin Coras54693d22018-07-17 10:46:29 -07001385handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001386
Florin Coras134a9962018-08-28 11:32:04 -07001387 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg);
1388 listen_session = vcl_session_get (wrk, listen_session_index);
1389 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001390
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001391 if (flags & O_NONBLOCK)
1392 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001393
Florin Coras54693d22018-07-17 10:46:29 -07001394 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras05ecfcc2018-12-12 18:19:39 -08001395 VDBG (1, "vpp handle 0x%llx, sid %u: Got a client request! "
Florin Coras0d427d82018-06-27 03:24:07 -07001396 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
Florin Corasdc2e2512018-12-03 17:47:26 -08001397 listen_vpp_handle, listen_session_handle,
Florin Coras0d427d82018-06-27 03:24:07 -07001398 client_session->vpp_handle, client_session_index,
1399 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1400 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001401
Dave Wallace048b1d62018-01-03 22:24:41 -05001402 if (ep)
1403 {
Florin Coras7e12d942018-06-27 14:32:43 -07001404 ep->is_ip4 = client_session->transport.is_ip4;
1405 ep->port = client_session->transport.rmt_port;
1406 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001407 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1408 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001409 else
Dave Barach178cf492018-11-13 16:34:13 -05001410 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1411 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001412 }
Dave Wallace60caa062017-11-10 17:07:13 -05001413
Florin Coras54693d22018-07-17 10:46:29 -07001414 if (accepted_msg.server_event_queue_address)
1415 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1416 svm_msg_q_t *);
1417 else
1418 vpp_evt_q = client_session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -07001419
Florin Coras54693d22018-07-17 10:46:29 -07001420 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1421 client_session->vpp_handle, 0);
Dave Wallace60caa062017-11-10 17:07:13 -05001422
Florin Coras05ecfcc2018-12-12 18:19:39 -08001423 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1424 "local: %U:%u", listen_session_handle, listen_vpp_handle,
1425 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001426 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001427 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001428 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001429 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001430 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001431 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001432 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1433 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001434
Florin Coras3c7d4f92018-12-14 11:28:43 -08001435 /*
1436 * Session might have been closed already
1437 */
1438 if (accept_flags)
1439 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001440 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasb0f662f2018-12-27 14:51:46 -08001441 client_session->session_state = STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001442 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasb0f662f2018-12-27 14:51:46 -08001443 client_session->session_state = STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001444 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001445 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001446}
1447
1448int
Florin Coras134a9962018-08-28 11:32:04 -07001449vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001450{
Florin Coras134a9962018-08-28 11:32:04 -07001451 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001452 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001453 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001454 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001455
Florin Coras134a9962018-08-28 11:32:04 -07001456 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001457 if (!session)
1458 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001459 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001460
1461 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001462 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001463 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
Florin Coras134a9962018-08-28 11:32:04 -07001464 "connect on an epoll session!", getpid (),
1465 session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001466 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001467 }
1468
Florin Coras7e12d942018-06-27 14:32:43 -07001469 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
Dave Wallace543852a2017-08-03 02:11:34 -04001470 {
Florin Coras7baeb712019-01-04 17:05:43 -08001471 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001472 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001473 session_handle, session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001474 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001475 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001476 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001477 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001478 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001479 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001480 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001481 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001482 }
1483
Florin Coras7e12d942018-06-27 14:32:43 -07001484 session->transport.is_ip4 = server_ep->is_ip4;
1485 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001486 clib_memcpy_fast (&session->transport.rmt_ip.ip4, server_ep->ip,
1487 sizeof (ip4_address_t));
Dave Wallaced239f8d2018-06-19 13:37:30 -04001488 else
Dave Barach178cf492018-11-13 16:34:13 -05001489 clib_memcpy_fast (&session->transport.rmt_ip.ip6, server_ep->ip,
1490 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001491 session->transport.rmt_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04001492
Florin Coras7baeb712019-01-04 17:05:43 -08001493 VDBG (0, "session handle %u [0x%llx]: connecting to server %s %U "
1494 "port %d proto %s", session_handle, session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001495 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001496 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001497 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001498 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001499 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001500 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001501
Florin Coras070453d2018-08-24 17:04:27 -07001502 /*
1503 * Send connect request and wait for reply from vpp
1504 */
Florin Coras134a9962018-08-28 11:32:04 -07001505 vppcom_send_connect_sock (session);
Florin Coras070453d2018-08-24 17:04:27 -07001506 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1507 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001508
Florin Coras134a9962018-08-28 11:32:04 -07001509 session = vcl_session_get (wrk, session_index);
Dave Wallace7876d392017-10-19 03:53:57 -04001510
Florin Coras070453d2018-08-24 17:04:27 -07001511 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001512 {
Dave Wallaceee45d412017-11-24 21:44:06 -05001513 if (VPPCOM_DEBUG > 0)
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001514 {
1515 if (session)
Florin Coras0d427d82018-06-27 03:24:07 -07001516 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
Florin Coras134a9962018-08-28 11:32:04 -07001517 "failed! returning %d (%s)", getpid (),
1518 session->vpp_handle, session_handle, rv,
1519 vppcom_retval_str (rv));
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001520 else
1521 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1522 "returning %d (%s)", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07001523 session_handle, rv, vppcom_retval_str (rv));
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001524 }
Dave Wallaceee45d412017-11-24 21:44:06 -05001525 }
Florin Coras0d427d82018-06-27 03:24:07 -07001526 else
1527 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
Florin Coras134a9962018-08-28 11:32:04 -07001528 getpid (), session->vpp_handle, session_handle);
Dave Wallaceee45d412017-11-24 21:44:06 -05001529
Dave Wallace4878cbe2017-11-21 03:45:09 -05001530 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001531}
1532
Florin Coras54693d22018-07-17 10:46:29 -07001533static u8
1534vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1535{
1536 if (!is_ct)
1537 return (e->event_type == FIFO_EVENT_APP_RX
1538 && e->fifo->client_session_index == sid);
1539 else
1540 return (e->event_type == SESSION_IO_EVT_CT_TX);
1541}
1542
Steven58f464e2017-10-25 12:33:12 -07001543static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001544vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001545 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001546{
Florin Coras134a9962018-08-28 11:32:04 -07001547 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001548 int n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001549 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001550 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001551 svm_msg_q_msg_t msg;
1552 session_event_t *e;
1553 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001554 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001555
Florin Coras070453d2018-08-24 17:04:27 -07001556 if (PREDICT_FALSE (!buf))
1557 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001558
Florin Coras134a9962018-08-28 11:32:04 -07001559 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras2cba8532018-09-11 16:33:36 -07001560 if (PREDICT_FALSE (!s || s->is_vep))
Florin Coras070453d2018-08-24 17:04:27 -07001561 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001562
Florin Coras6d0106e2019-01-29 20:11:58 -08001563 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001564 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001565 VDBG (0, "session handle %u[0x%llx] is not open! state 0x%x (%s)",
1566 s->session_index, s->vpp_handle, s->session_state,
1567 vppcom_session_state_str (s->session_state));
1568 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001569 }
1570
Florin Coras2cba8532018-09-11 16:33:36 -07001571 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001572 is_ct = vcl_session_is_ct (s);
1573 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
Florin Coras2cba8532018-09-11 16:33:36 -07001574 rx_fifo = s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001575 s->has_rx_evt = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001576
Florin Coras54693d22018-07-17 10:46:29 -07001577 if (svm_fifo_is_empty (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001578 {
Florin Coras54693d22018-07-17 10:46:29 -07001579 if (is_nonblocking)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001580 {
Florin Coras41c9e042018-09-11 00:10:41 -07001581 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001582 return VPPCOM_EWOULDBLOCK;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001583 }
Florin Coras41c9e042018-09-11 00:10:41 -07001584 while (svm_fifo_is_empty (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001585 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001586 if (vcl_session_is_closing (s))
1587 return vcl_session_closing_error (s);
1588
Florin Coras41c9e042018-09-11 00:10:41 -07001589 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001590 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001591 if (svm_msg_q_is_empty (mq))
1592 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001593
Florin Coras54693d22018-07-17 10:46:29 -07001594 svm_msg_q_sub_w_lock (mq, &msg);
1595 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001596 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001597 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001598 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001599 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001600 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001601 }
Florin Coras54693d22018-07-17 10:46:29 -07001602
Florin Coras460dce62018-07-27 05:45:06 -07001603 if (s->is_dgram)
Florin Coras99368312018-08-02 10:45:44 -07001604 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001605 else
Florin Coras99368312018-08-02 10:45:44 -07001606 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
Florin Coras54693d22018-07-17 10:46:29 -07001607
Florin Coras41c9e042018-09-11 00:10:41 -07001608 if (svm_fifo_is_empty (rx_fifo))
1609 svm_fifo_unset_event (rx_fifo);
1610
Florin Coras1bcad5c2019-01-09 20:04:38 -08001611 if (is_ct && svm_fifo_needs_tx_ntf (rx_fifo, n_read))
Florin Coras99368312018-08-02 10:45:44 -07001612 {
Florin Coras1bcad5c2019-01-09 20:04:38 -08001613 svm_fifo_clear_tx_ntf (s->rx_fifo);
Florin Coras58c101a2018-10-06 13:49:16 -07001614 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo, SESSION_IO_EVT_CT_RX,
1615 SVM_Q_WAIT);
Florin Coras99368312018-08-02 10:45:44 -07001616 }
Florin Coras54693d22018-07-17 10:46:29 -07001617
Florin Corasa7a1a222018-12-30 17:11:31 -08001618 VDBG (2, "vpp handle 0x%llx, sid %u: read %d bytes from (%p)",
1619 s->vpp_handle, session_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001620
Florin Coras54693d22018-07-17 10:46:29 -07001621 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001622}
1623
Steven58f464e2017-10-25 12:33:12 -07001624int
Florin Coras134a9962018-08-28 11:32:04 -07001625vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001626{
Florin Coras134a9962018-08-28 11:32:04 -07001627 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001628}
1629
1630static int
Florin Coras134a9962018-08-28 11:32:04 -07001631vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001632{
Florin Coras134a9962018-08-28 11:32:04 -07001633 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001634}
1635
Florin Coras2cba8532018-09-11 16:33:36 -07001636int
1637vppcom_session_read_segments (uint32_t session_handle,
1638 vppcom_data_segments_t ds)
1639{
1640 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001641 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001642 vcl_session_t *s = 0;
1643 svm_fifo_t *rx_fifo;
1644 svm_msg_q_msg_t msg;
1645 session_event_t *e;
1646 svm_msg_q_t *mq;
1647 u8 is_ct;
1648
1649 s = vcl_session_get_w_handle (wrk, session_handle);
1650 if (PREDICT_FALSE (!s || s->is_vep))
1651 return VPPCOM_EBADFD;
1652
Florin Coras6d0106e2019-01-29 20:11:58 -08001653 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1654 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001655
1656 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1657 is_ct = vcl_session_is_ct (s);
1658 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1659 rx_fifo = s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001660 s->has_rx_evt = 0;
Florin Coras2cba8532018-09-11 16:33:36 -07001661
1662 if (svm_fifo_is_empty (rx_fifo))
1663 {
1664 if (is_nonblocking)
1665 {
1666 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001667 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001668 }
1669 while (svm_fifo_is_empty (rx_fifo))
1670 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001671 if (vcl_session_is_closing (s))
1672 return vcl_session_closing_error (s);
1673
Florin Coras2cba8532018-09-11 16:33:36 -07001674 svm_fifo_unset_event (rx_fifo);
1675 svm_msg_q_lock (mq);
1676 if (svm_msg_q_is_empty (mq))
1677 svm_msg_q_wait (mq);
1678
1679 svm_msg_q_sub_w_lock (mq, &msg);
1680 e = svm_msg_q_msg_data (mq, &msg);
1681 svm_msg_q_unlock (mq);
1682 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001683 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001684 svm_msg_q_free_msg (mq, &msg);
Florin Coras2cba8532018-09-11 16:33:36 -07001685 }
1686 }
1687
1688 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_segment_t *) ds);
1689 svm_fifo_unset_event (rx_fifo);
1690
1691 if (is_ct && n_read + svm_fifo_max_dequeue (rx_fifo) == rx_fifo->nitems)
1692 {
1693 /* If the peer is not polling send notification */
1694 if (!svm_fifo_has_event (s->rx_fifo))
1695 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1696 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1697 }
1698
1699 return n_read;
1700}
1701
1702void
1703vppcom_session_free_segments (uint32_t session_handle,
1704 vppcom_data_segments_t ds)
1705{
1706 vcl_worker_t *wrk = vcl_worker_get_current ();
1707 vcl_session_t *s;
1708
1709 s = vcl_session_get_w_handle (wrk, session_handle);
1710 if (PREDICT_FALSE (!s || s->is_vep))
1711 return;
1712
1713 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_segment_t *) ds);
1714}
1715
Florin Coras2cba8532018-09-11 16:33:36 -07001716int
1717vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1718{
1719 u32 first_copy = clib_min (ds[0].len, max_bytes);
Dave Barach178cf492018-11-13 16:34:13 -05001720 clib_memcpy_fast (buf, ds[0].data, first_copy);
Florin Coras2cba8532018-09-11 16:33:36 -07001721 if (first_copy < max_bytes)
1722 {
Dave Barach178cf492018-11-13 16:34:13 -05001723 clib_memcpy_fast (buf + first_copy, ds[1].data,
1724 clib_min (ds[1].len, max_bytes - first_copy));
Florin Coras2cba8532018-09-11 16:33:36 -07001725 }
1726 return 0;
1727}
1728
Florin Coras54693d22018-07-17 10:46:29 -07001729static u8
1730vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1731{
1732 if (!is_ct)
1733 return (e->event_type == FIFO_EVENT_APP_TX
1734 && e->fifo->client_session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04001735 else
Florin Coras54693d22018-07-17 10:46:29 -07001736 return (e->event_type == SESSION_IO_EVT_CT_RX);
Dave Wallace543852a2017-08-03 02:11:34 -04001737}
1738
Florin Coras42ceddb2018-12-12 10:56:01 -08001739static inline int
1740vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
1741 u8 is_flush)
Dave Wallace543852a2017-08-03 02:11:34 -04001742{
Florin Coras134a9962018-08-28 11:32:04 -07001743 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001744 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001745 vcl_session_t *s = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001746 svm_fifo_t *tx_fifo = 0;
Florin Coras460dce62018-07-27 05:45:06 -07001747 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07001748 svm_msg_q_msg_t msg;
1749 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001750 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07001751 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001752
Florin Coras070453d2018-08-24 17:04:27 -07001753 if (PREDICT_FALSE (!buf))
1754 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001755
Florin Coras134a9962018-08-28 11:32:04 -07001756 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001757 if (PREDICT_FALSE (!s))
1758 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001759
Florin Coras460dce62018-07-27 05:45:06 -07001760 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001761 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001762 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
1763 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001764 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001765 }
1766
Florin Coras6d0106e2019-01-29 20:11:58 -08001767 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001768 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001769 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
1770 s->session_index, s->vpp_handle, s->session_state,
1771 vppcom_session_state_str (s->session_state));
1772 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001773 }
1774
Florin Coras0e88e852018-09-17 22:09:02 -07001775 tx_fifo = s->tx_fifo;
1776 is_ct = vcl_session_is_ct (s);
1777 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1778 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
Florin Coras54693d22018-07-17 10:46:29 -07001779 if (svm_fifo_is_full (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04001780 {
Florin Coras54693d22018-07-17 10:46:29 -07001781 if (is_nonblocking)
1782 {
Florin Coras070453d2018-08-24 17:04:27 -07001783 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07001784 }
Florin Coras60f1fc12018-08-16 14:57:31 -07001785 while (svm_fifo_is_full (tx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001786 {
Florin Coras1bcad5c2019-01-09 20:04:38 -08001787 svm_fifo_add_want_tx_ntf (tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08001788 if (vcl_session_is_closing (s))
1789 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07001790 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07001791 if (svm_msg_q_is_empty (mq))
1792 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07001793
Florin Coras54693d22018-07-17 10:46:29 -07001794 svm_msg_q_sub_w_lock (mq, &msg);
1795 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001796 svm_msg_q_unlock (mq);
1797
Florin Coras0e88e852018-09-17 22:09:02 -07001798 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07001799 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001800 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001801 }
Dave Wallace543852a2017-08-03 02:11:34 -04001802 }
Dave Wallace543852a2017-08-03 02:11:34 -04001803
Florin Coras460dce62018-07-27 05:45:06 -07001804 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1805 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
Florin Coras42ceddb2018-12-12 10:56:01 -08001806 if (is_flush && !vcl_session_is_ct (s))
1807 et = SESSION_IO_EVT_TX_FLUSH;
1808
Florin Coras460dce62018-07-27 05:45:06 -07001809 if (s->is_dgram)
1810 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1811 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1812 else
1813 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1814 SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001815
Florin Coras460dce62018-07-27 05:45:06 -07001816 ASSERT (n_write > 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001817
Florin Coras6d0106e2019-01-29 20:11:58 -08001818 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
1819 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07001820
Florin Coras54693d22018-07-17 10:46:29 -07001821 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04001822}
1823
Florin Coras42ceddb2018-12-12 10:56:01 -08001824int
1825vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1826{
1827 return vppcom_session_write_inline (session_handle, buf, n,
1828 0 /* is_flush */ );
1829}
1830
Florin Corasb0f662f2018-12-27 14:51:46 -08001831int
1832vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
1833{
1834 return vppcom_session_write_inline (session_handle, buf, n,
1835 1 /* is_flush */ );
1836}
1837
1838
Florin Coras99368312018-08-02 10:45:44 -07001839static vcl_session_t *
Florin Coras134a9962018-08-28 11:32:04 -07001840vcl_ct_session_get_from_fifo (vcl_worker_t * wrk, svm_fifo_t * f, u8 type)
Florin Coras99368312018-08-02 10:45:44 -07001841{
1842 vcl_session_t *s;
Florin Coras134a9962018-08-28 11:32:04 -07001843 s = vcl_session_get (wrk, f->client_session_index);
Florin Coras99368312018-08-02 10:45:44 -07001844 if (s)
1845 {
1846 /* rx fifo */
1847 if (type == 0 && s->rx_fifo == f)
1848 return s;
1849 /* tx fifo */
1850 if (type == 1 && s->tx_fifo == f)
1851 return s;
1852 }
Florin Coras134a9962018-08-28 11:32:04 -07001853 s = vcl_session_get (wrk, f->master_session_index);
Florin Coras99368312018-08-02 10:45:44 -07001854 if (s)
1855 {
1856 if (type == 0 && s->rx_fifo == f)
1857 return s;
1858 if (type == 1 && s->tx_fifo == f)
1859 return s;
1860 }
1861 return 0;
1862}
1863
Florin Coras6d4bb422018-09-04 22:07:27 -07001864#define vcl_fifo_rx_evt_valid_or_break(_fifo) \
1865if (PREDICT_FALSE (svm_fifo_is_empty (_fifo))) \
1866 { \
1867 svm_fifo_unset_event (_fifo); \
1868 if (svm_fifo_is_empty (_fifo)) \
Florin Coras41c9e042018-09-11 00:10:41 -07001869 break; \
Florin Coras6d4bb422018-09-04 22:07:27 -07001870 } \
1871
Florin Coras86f04502018-09-12 16:08:01 -07001872static void
1873vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1874 unsigned long n_bits, unsigned long *read_map,
1875 unsigned long *write_map,
1876 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07001877{
1878 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07001879 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07001880 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07001881 u32 sid;
1882
1883 switch (e->event_type)
1884 {
1885 case FIFO_EVENT_APP_RX:
1886 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1887 sid = e->fifo->client_session_index;
1888 session = vcl_session_get (wrk, sid);
1889 if (!session)
1890 break;
1891 if (sid < n_bits && read_map)
1892 {
David Johnsond9818dd2018-12-14 14:53:41 -05001893 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001894 *bits_set += 1;
1895 }
1896 break;
1897 case FIFO_EVENT_APP_TX:
1898 sid = e->fifo->client_session_index;
1899 session = vcl_session_get (wrk, sid);
1900 if (!session)
1901 break;
1902 if (sid < n_bits && write_map)
1903 {
David Johnsond9818dd2018-12-14 14:53:41 -05001904 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001905 *bits_set += 1;
1906 }
1907 break;
1908 case SESSION_IO_EVT_CT_TX:
1909 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1910 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
1911 if (!session)
1912 break;
1913 sid = session->session_index;
1914 if (sid < n_bits && read_map)
1915 {
David Johnsond9818dd2018-12-14 14:53:41 -05001916 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001917 *bits_set += 1;
1918 }
1919 break;
1920 case SESSION_IO_EVT_CT_RX:
1921 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
1922 if (!session)
1923 break;
1924 sid = session->session_index;
1925 if (sid < n_bits && write_map)
1926 {
David Johnsond9818dd2018-12-14 14:53:41 -05001927 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001928 *bits_set += 1;
1929 }
1930 break;
1931 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08001932 session = vcl_session_accepted (wrk,
1933 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07001934 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08001935 break;
Florin Coras86f04502018-09-12 16:08:01 -07001936 sid = session->session_index;
1937 if (sid < n_bits && read_map)
1938 {
David Johnsond9818dd2018-12-14 14:53:41 -05001939 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001940 *bits_set += 1;
1941 }
1942 break;
1943 case SESSION_CTRL_EVT_CONNECTED:
1944 connected_msg = (session_connected_msg_t *) e->data;
1945 vcl_session_connected_handler (wrk, connected_msg);
1946 break;
1947 case SESSION_CTRL_EVT_DISCONNECTED:
1948 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001949 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
1950 if (!session)
1951 break;
1952 sid = session->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001953 if (sid < n_bits && except_map)
1954 {
David Johnsond9818dd2018-12-14 14:53:41 -05001955 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001956 *bits_set += 1;
1957 }
1958 break;
1959 case SESSION_CTRL_EVT_RESET:
1960 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1961 if (sid < n_bits && except_map)
1962 {
David Johnsond9818dd2018-12-14 14:53:41 -05001963 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001964 *bits_set += 1;
1965 }
1966 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001967 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1968 vcl_session_unlisten_reply_handler (wrk, e->data);
1969 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001970 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1971 vcl_session_worker_update_reply_handler (wrk, e->data);
1972 break;
1973 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1974 vcl_session_req_worker_update_handler (wrk, e->data);
1975 break;
Florin Coras86f04502018-09-12 16:08:01 -07001976 default:
1977 clib_warning ("unhandled: %u", e->event_type);
1978 break;
1979 }
1980}
1981
1982static int
1983vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1984 unsigned long n_bits, unsigned long *read_map,
1985 unsigned long *write_map, unsigned long *except_map,
1986 double time_to_wait, u32 * bits_set)
1987{
Florin Coras99368312018-08-02 10:45:44 -07001988 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07001989 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07001990 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07001991
1992 svm_msg_q_lock (mq);
1993 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001994 {
Florin Coras54693d22018-07-17 10:46:29 -07001995 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001996 {
Florin Coras54693d22018-07-17 10:46:29 -07001997 svm_msg_q_unlock (mq);
1998 return 0;
1999 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002000
Florin Coras54693d22018-07-17 10:46:29 -07002001 if (!time_to_wait)
2002 {
2003 svm_msg_q_unlock (mq);
2004 return 0;
2005 }
2006 else if (time_to_wait < 0)
2007 {
2008 svm_msg_q_wait (mq);
2009 }
2010 else
2011 {
2012 if (svm_msg_q_timedwait (mq, time_to_wait))
2013 {
2014 svm_msg_q_unlock (mq);
2015 return 0;
2016 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002017 }
2018 }
Florin Coras134a9962018-08-28 11:32:04 -07002019 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07002020 svm_msg_q_unlock (mq);
2021
Florin Coras134a9962018-08-28 11:32:04 -07002022 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002023 {
Florin Coras134a9962018-08-28 11:32:04 -07002024 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002025 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002026 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2027 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002028 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002029 }
Florin Coras134a9962018-08-28 11:32:04 -07002030 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002031 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002032 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002033}
2034
Florin Coras99368312018-08-02 10:45:44 -07002035static int
Florin Coras294afe22019-01-07 17:49:17 -08002036vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
2037 vcl_si_set * read_map, vcl_si_set * write_map,
2038 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002039 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002040{
2041 double total_wait = 0, wait_slice;
2042 vcl_cut_through_registration_t *cr;
2043
Florin Coras294afe22019-01-07 17:49:17 -08002044 time_to_wait = (time_to_wait == -1) ? 1e6 : time_to_wait;
Florin Coras134a9962018-08-28 11:32:04 -07002045 wait_slice = wrk->cut_through_registrations ? 10e-6 : time_to_wait;
Florin Coras99368312018-08-02 10:45:44 -07002046 do
2047 {
Florin Coras134a9962018-08-28 11:32:04 -07002048 vcl_ct_registration_lock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07002049 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002050 pool_foreach (cr, wrk->cut_through_registrations, ({
2051 vcl_select_handle_mq (wrk, cr->mq, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002052 0, bits_set);
2053 }));
2054 /* *INDENT-ON* */
Florin Coras134a9962018-08-28 11:32:04 -07002055 vcl_ct_registration_unlock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07002056
Florin Coras134a9962018-08-28 11:32:04 -07002057 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002058 write_map, except_map, wait_slice, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002059 total_wait += wait_slice;
2060 if (*bits_set)
2061 return *bits_set;
2062 }
2063 while (total_wait < time_to_wait);
2064
2065 return 0;
2066}
2067
2068static int
Florin Coras294afe22019-01-07 17:49:17 -08002069vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
2070 vcl_si_set * read_map, vcl_si_set * write_map,
2071 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07002072 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002073{
2074 vcl_mq_evt_conn_t *mqc;
2075 int __clib_unused n_read;
2076 int n_mq_evts, i;
2077 u64 buf;
2078
Florin Coras134a9962018-08-28 11:32:04 -07002079 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2080 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2081 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002082 for (i = 0; i < n_mq_evts; i++)
2083 {
Florin Coras134a9962018-08-28 11:32:04 -07002084 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002085 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002086 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002087 except_map, 0, bits_set);
2088 }
2089
2090 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2091}
2092
Dave Wallace543852a2017-08-03 02:11:34 -04002093int
Florin Coras294afe22019-01-07 17:49:17 -08002094vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
2095 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04002096{
Florin Coras54693d22018-07-17 10:46:29 -07002097 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002098 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002099 vcl_session_t *session = 0;
Florin Coras86f04502018-09-12 16:08:01 -07002100 int rv, i;
Dave Wallace543852a2017-08-03 02:11:34 -04002101
Dave Wallace7876d392017-10-19 03:53:57 -04002102 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002103 {
Florin Coras134a9962018-08-28 11:32:04 -07002104 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002105 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08002106 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
2107 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002108 }
Dave Wallace7876d392017-10-19 03:53:57 -04002109 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002110 {
Florin Coras134a9962018-08-28 11:32:04 -07002111 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002112 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08002113 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
2114 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002115 }
Dave Wallace7876d392017-10-19 03:53:57 -04002116 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002117 {
Florin Coras134a9962018-08-28 11:32:04 -07002118 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002119 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08002120 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
2121 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04002122 }
2123
Florin Coras54693d22018-07-17 10:46:29 -07002124 if (!n_bits)
2125 return 0;
2126
2127 if (!write_map)
2128 goto check_rd;
2129
2130 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002131 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2132 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002133 {
Florin Coras47c40e22018-11-26 17:01:36 -08002134 if (except_map && sid < minbits)
2135 clib_bitmap_set_no_check (except_map, sid, 1);
2136 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002137 }
2138
2139 rv = svm_fifo_is_full (session->tx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07002140 if (!rv)
2141 {
David Johnsond9818dd2018-12-14 14:53:41 -05002142 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002143 bits_set++;
2144 }
Florin Coras294afe22019-01-07 17:49:17 -08002145 else
Florin Coras1bcad5c2019-01-09 20:04:38 -08002146 svm_fifo_add_want_tx_ntf (session->tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
Florin Coras54693d22018-07-17 10:46:29 -07002147 }));
2148
2149check_rd:
2150 if (!read_map)
2151 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002152
Florin Coras134a9962018-08-28 11:32:04 -07002153 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2154 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002155 {
Florin Coras47c40e22018-11-26 17:01:36 -08002156 if (except_map && sid < minbits)
2157 clib_bitmap_set_no_check (except_map, sid, 1);
2158 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002159 }
2160
Florin Coras0ef8ef22019-01-18 08:37:13 -08002161 rv = vcl_session_read_ready (session);
Florin Coras54693d22018-07-17 10:46:29 -07002162 if (rv)
2163 {
David Johnsond9818dd2018-12-14 14:53:41 -05002164 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002165 bits_set++;
2166 }
2167 }));
2168 /* *INDENT-ON* */
2169
2170check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002171
Florin Coras86f04502018-09-12 16:08:01 -07002172 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2173 {
2174 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2175 read_map, write_map, except_map, &bits_set);
2176 }
2177 vec_reset_length (wrk->unhandled_evts_vector);
2178
Florin Coras99368312018-08-02 10:45:44 -07002179 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002180 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002181 time_to_wait, &bits_set);
2182 else
Florin Coras134a9962018-08-28 11:32:04 -07002183 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002184 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002185
Dave Wallace543852a2017-08-03 02:11:34 -04002186 return (bits_set);
2187}
2188
Dave Wallacef7f809c2017-10-03 01:48:42 -04002189static inline void
Florin Coras134a9962018-08-28 11:32:04 -07002190vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002191{
Florin Coras7e12d942018-06-27 14:32:43 -07002192 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002193 vppcom_epoll_t *vep;
Dave Wallace498b3a52017-11-09 13:00:34 -05002194 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002195
Dave Wallace498b3a52017-11-09 13:00:34 -05002196 if (VPPCOM_DEBUG <= 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002197 return;
2198
2199 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Florin Coras134a9962018-08-28 11:32:04 -07002200 session = vcl_session_get (wrk, vep_idx);
Florin Coras070453d2018-08-24 17:04:27 -07002201 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002202 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002203 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
2204 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002205 goto done;
2206 }
2207 if (PREDICT_FALSE (!session->is_vep))
2208 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002209 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2210 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002211 goto done;
2212 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002213 vep = &session->vep;
Dave Wallace048b1d62018-01-03 22:24:41 -05002214 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05002215 "{\n"
2216 " is_vep = %u\n"
2217 " is_vep_session = %u\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05002218 " next_sid = 0x%x (%u)\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05002219 " wait_cont_idx = 0x%x (%u)\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05002220 "}\n", getpid (), vep_idx,
2221 session->is_vep, session->is_vep_session,
Florin Coras134a9962018-08-28 11:32:04 -07002222 vep->next_sh, vep->next_sh,
Dave Wallace498b3a52017-11-09 13:00:34 -05002223 session->wait_cont_idx, session->wait_cont_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002224
Florin Coras134a9962018-08-28 11:32:04 -07002225 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002226 {
Florin Coras134a9962018-08-28 11:32:04 -07002227 session = vcl_session_get (wrk, sid);
Florin Coras070453d2018-08-24 17:04:27 -07002228 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002229 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002230 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002231 goto done;
2232 }
2233 if (PREDICT_FALSE (session->is_vep))
Dave Wallace048b1d62018-01-03 22:24:41 -05002234 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
2235 getpid (), vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002236 else if (PREDICT_FALSE (!session->is_vep_session))
2237 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002238 clib_warning ("VCL<%d>: ERROR: session (%u) "
2239 "is not a vep session!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002240 goto done;
2241 }
2242 vep = &session->vep;
Florin Coras134a9962018-08-28 11:32:04 -07002243 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
Dave Wallace048b1d62018-01-03 22:24:41 -05002244 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002245 "vep_idx (%u)!", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002246 sid, session->vep.vep_sh, vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002247 if (session->is_vep_session)
2248 {
2249 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2250 "{\n"
2251 " next_sid = 0x%x (%u)\n"
2252 " prev_sid = 0x%x (%u)\n"
2253 " vep_idx = 0x%x (%u)\n"
2254 " ev.events = 0x%x\n"
2255 " ev.data.u64 = 0x%llx\n"
2256 " et_mask = 0x%x\n"
2257 "}\n",
2258 vep_idx, sid, sid,
Florin Coras134a9962018-08-28 11:32:04 -07002259 vep->next_sh, vep->next_sh,
2260 vep->prev_sh, vep->prev_sh,
2261 vep->vep_sh, vep->vep_sh,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002262 vep->ev.events, vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002263 }
2264 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002265
2266done:
Dave Wallace048b1d62018-01-03 22:24:41 -05002267 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
2268 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002269}
2270
2271int
2272vppcom_epoll_create (void)
2273{
Florin Coras134a9962018-08-28 11:32:04 -07002274 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002275 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002276
Florin Coras134a9962018-08-28 11:32:04 -07002277 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002278
2279 vep_session->is_vep = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002280 vep_session->vep.vep_sh = ~0;
2281 vep_session->vep.next_sh = ~0;
2282 vep_session->vep.prev_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002283 vep_session->wait_cont_idx = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002284 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002285
Florin Corasa7a1a222018-12-30 17:11:31 -08002286 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2287 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002288
Florin Corasab2f6db2018-08-31 14:31:41 -07002289 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002290}
2291
2292int
Florin Coras134a9962018-08-28 11:32:04 -07002293vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002294 struct epoll_event *event)
2295{
Florin Coras134a9962018-08-28 11:32:04 -07002296 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002297 vcl_session_t *vep_session;
2298 vcl_session_t *session;
Florin Coras070453d2018-08-24 17:04:27 -07002299 int rv = VPPCOM_OK;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002300
Florin Coras134a9962018-08-28 11:32:04 -07002301 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002302 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002303 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002304 return VPPCOM_EINVAL;
2305 }
2306
Florin Coras134a9962018-08-28 11:32:04 -07002307 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002308 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002309 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002310 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002311 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002312 }
2313 if (PREDICT_FALSE (!vep_session->is_vep))
2314 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002315 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002316 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002317 }
2318
Florin Coras134a9962018-08-28 11:32:04 -07002319 ASSERT (vep_session->vep.vep_sh == ~0);
2320 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002321
Florin Coras134a9962018-08-28 11:32:04 -07002322 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002323 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002324 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002325 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002326 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002327 }
2328 if (PREDICT_FALSE (session->is_vep))
2329 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002330 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002331 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002332 }
2333
2334 switch (op)
2335 {
2336 case EPOLL_CTL_ADD:
2337 if (PREDICT_FALSE (!event))
2338 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002339 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002340 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002341 }
Florin Coras134a9962018-08-28 11:32:04 -07002342 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002343 {
Florin Coras7e12d942018-06-27 14:32:43 -07002344 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002345 next_session = vcl_session_get_w_handle (wrk,
2346 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002347 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002348 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002349 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sid (%u) on "
2350 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002351 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002352 }
Florin Coras134a9962018-08-28 11:32:04 -07002353 ASSERT (next_session->vep.prev_sh == vep_handle);
2354 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002355 }
Florin Coras134a9962018-08-28 11:32:04 -07002356 session->vep.next_sh = vep_session->vep.next_sh;
2357 session->vep.prev_sh = vep_handle;
2358 session->vep.vep_sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002359 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2360 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002361 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002362 session->is_vep_session = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002363 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002364
Florin Coras1bcad5c2019-01-09 20:04:38 -08002365 if (session->tx_fifo)
2366 svm_fifo_add_want_tx_ntf (session->tx_fifo,
2367 SVM_FIFO_WANT_TX_NOTIF_IF_FULL);
2368
Florin Corasa7a1a222018-12-30 17:11:31 -08002369 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2370 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras0d427d82018-06-27 03:24:07 -07002371 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002372 break;
2373
2374 case EPOLL_CTL_MOD:
2375 if (PREDICT_FALSE (!event))
2376 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002377 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002378 rv = VPPCOM_EINVAL;
2379 goto done;
2380 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002381 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002382 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002383 VDBG (0, "sid %u EPOLL_CTL_MOD: not a vep session!",
2384 session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002385 rv = VPPCOM_EINVAL;
2386 goto done;
2387 }
Florin Coras134a9962018-08-28 11:32:04 -07002388 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002389 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002390 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2391 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002392 rv = VPPCOM_EINVAL;
2393 goto done;
2394 }
2395 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2396 session->vep.ev = *event;
Florin Corasa7a1a222018-12-30 17:11:31 -08002397 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2398 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002399 break;
2400
2401 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05002402 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002403 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002404 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002405 rv = VPPCOM_EINVAL;
2406 goto done;
2407 }
Florin Coras134a9962018-08-28 11:32:04 -07002408 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002409 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002410 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2411 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002412 rv = VPPCOM_EINVAL;
2413 goto done;
2414 }
2415
2416 vep_session->wait_cont_idx =
Florin Coras134a9962018-08-28 11:32:04 -07002417 (vep_session->wait_cont_idx == session_handle) ?
2418 session->vep.next_sh : vep_session->wait_cont_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002419
Florin Coras134a9962018-08-28 11:32:04 -07002420 if (session->vep.prev_sh == vep_handle)
2421 vep_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002422 else
2423 {
Florin Coras7e12d942018-06-27 14:32:43 -07002424 vcl_session_t *prev_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002425 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002426 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002427 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002428 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sid (%u) on sid (%u)!",
2429 session->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002430 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002431 }
Florin Coras134a9962018-08-28 11:32:04 -07002432 ASSERT (prev_session->vep.next_sh == session_handle);
2433 prev_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002434 }
Florin Coras134a9962018-08-28 11:32:04 -07002435 if (session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002436 {
Florin Coras7e12d942018-06-27 14:32:43 -07002437 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002438 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002439 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002440 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002441 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sid (%u) on sid (%u)!",
2442 session->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002443 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002444 }
Florin Coras134a9962018-08-28 11:32:04 -07002445 ASSERT (next_session->vep.prev_sh == session_handle);
2446 next_session->vep.prev_sh = session->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002447 }
2448
2449 memset (&session->vep, 0, sizeof (session->vep));
Florin Coras134a9962018-08-28 11:32:04 -07002450 session->vep.next_sh = ~0;
2451 session->vep.prev_sh = ~0;
2452 session->vep.vep_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002453 session->is_vep_session = 0;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002454
2455 if (session->tx_fifo)
2456 svm_fifo_del_want_tx_ntf (session->tx_fifo, SVM_FIFO_NO_TX_NOTIF);
2457
Florin Corasa7a1a222018-12-30 17:11:31 -08002458 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sid %u!", vep_handle,
2459 session_handle);
Florin Coras134a9962018-08-28 11:32:04 -07002460 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002461 break;
2462
2463 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002464 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002465 rv = VPPCOM_EINVAL;
2466 }
2467
Florin Coras134a9962018-08-28 11:32:04 -07002468 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002469
2470done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002471 return rv;
2472}
2473
Florin Coras86f04502018-09-12 16:08:01 -07002474static inline void
2475vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2476 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002477{
2478 session_disconnected_msg_t *disconnected_msg;
2479 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002480 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002481 u64 session_evt_data = ~0;
Florin Coras54693d22018-07-17 10:46:29 -07002482 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002483 u8 add_event = 0;
2484
2485 switch (e->event_type)
2486 {
2487 case FIFO_EVENT_APP_RX:
2488 ASSERT (e->fifo->client_thread_index == vcl_get_worker_index ());
2489 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2490 sid = e->fifo->client_session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002491 if (!(session = vcl_session_get (wrk, sid)))
2492 break;
Florin Coras86f04502018-09-12 16:08:01 -07002493 session_events = session->vep.ev.events;
Florin Corasaa27eb92018-10-13 12:20:01 -07002494 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
Florin Coras86f04502018-09-12 16:08:01 -07002495 break;
2496 add_event = 1;
2497 events[*num_ev].events |= EPOLLIN;
2498 session_evt_data = session->vep.ev.data.u64;
Florin Corasaa27eb92018-10-13 12:20:01 -07002499 session->has_rx_evt = 1;
Florin Coras86f04502018-09-12 16:08:01 -07002500 break;
2501 case FIFO_EVENT_APP_TX:
2502 sid = e->fifo->client_session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002503 if (!(session = vcl_session_get (wrk, sid)))
2504 break;
Florin Coras86f04502018-09-12 16:08:01 -07002505 session_events = session->vep.ev.events;
2506 if (!(EPOLLOUT & session_events))
2507 break;
2508 add_event = 1;
2509 events[*num_ev].events |= EPOLLOUT;
2510 session_evt_data = session->vep.ev.data.u64;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002511 svm_fifo_reset_tx_ntf (session->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002512 break;
2513 case SESSION_IO_EVT_CT_TX:
2514 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2515 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
Florin Corasfa915f82018-12-26 16:29:06 -08002516 if (PREDICT_FALSE (!session))
2517 break;
Florin Coras86f04502018-09-12 16:08:01 -07002518 sid = session->session_index;
2519 session_events = session->vep.ev.events;
Florin Corasaa27eb92018-10-13 12:20:01 -07002520 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
Florin Coras86f04502018-09-12 16:08:01 -07002521 break;
2522 add_event = 1;
2523 events[*num_ev].events |= EPOLLIN;
2524 session_evt_data = session->vep.ev.data.u64;
Florin Corasaa27eb92018-10-13 12:20:01 -07002525 session->has_rx_evt = 1;
Florin Coras86f04502018-09-12 16:08:01 -07002526 break;
2527 case SESSION_IO_EVT_CT_RX:
2528 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
Florin Corasfa915f82018-12-26 16:29:06 -08002529 if (PREDICT_FALSE (!session))
2530 break;
Florin Coras86f04502018-09-12 16:08:01 -07002531 sid = session->session_index;
2532 session_events = session->vep.ev.events;
2533 if (!(EPOLLOUT & session_events))
2534 break;
2535 add_event = 1;
2536 events[*num_ev].events |= EPOLLOUT;
2537 session_evt_data = session->vep.ev.data.u64;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002538 svm_fifo_reset_tx_ntf (session->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002539 break;
2540 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002541 session = vcl_session_accepted (wrk,
2542 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002543 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002544 break;
Florin Coras86f04502018-09-12 16:08:01 -07002545
Florin Coras86f04502018-09-12 16:08:01 -07002546 session_events = session->vep.ev.events;
2547 if (!(EPOLLIN & session_events))
2548 break;
2549
2550 add_event = 1;
2551 events[*num_ev].events |= EPOLLIN;
2552 session_evt_data = session->vep.ev.data.u64;
2553 break;
2554 case SESSION_CTRL_EVT_CONNECTED:
2555 connected_msg = (session_connected_msg_t *) e->data;
2556 vcl_session_connected_handler (wrk, connected_msg);
2557 /* Generate EPOLLOUT because there's no connected event */
2558 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
Florin Corasfa915f82018-12-26 16:29:06 -08002559 if (!(session = vcl_session_get (wrk, sid)))
2560 break;
Florin Coras86f04502018-09-12 16:08:01 -07002561 session_events = session->vep.ev.events;
Florin Coras72f77822019-01-22 19:05:52 -08002562 if (!(EPOLLOUT & session_events))
2563 break;
2564 add_event = 1;
2565 events[*num_ev].events |= EPOLLOUT;
2566 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002567 break;
2568 case SESSION_CTRL_EVT_DISCONNECTED:
2569 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002570 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2571 if (!session)
Florin Coras86f04502018-09-12 16:08:01 -07002572 break;
Florin Coras72f77822019-01-22 19:05:52 -08002573 session_events = session->vep.ev.events;
2574 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2575 break;
Florin Coras86f04502018-09-12 16:08:01 -07002576 add_event = 1;
2577 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2578 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002579 break;
2580 case SESSION_CTRL_EVT_RESET:
2581 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2582 if (!(session = vcl_session_get (wrk, sid)))
2583 break;
Florin Coras72f77822019-01-22 19:05:52 -08002584 session_events = session->vep.ev.events;
2585 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2586 break;
Florin Coras86f04502018-09-12 16:08:01 -07002587 add_event = 1;
2588 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2589 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002590 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002591 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2592 vcl_session_unlisten_reply_handler (wrk, e->data);
2593 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002594 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2595 vcl_session_req_worker_update_handler (wrk, e->data);
2596 break;
2597 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2598 vcl_session_worker_update_reply_handler (wrk, e->data);
2599 break;
Florin Coras86f04502018-09-12 16:08:01 -07002600 default:
2601 VDBG (0, "unhandled: %u", e->event_type);
2602 break;
2603 }
2604
2605 if (add_event)
2606 {
2607 events[*num_ev].data.u64 = session_evt_data;
2608 if (EPOLLONESHOT & session_events)
2609 {
2610 session = vcl_session_get (wrk, sid);
2611 session->vep.ev.events = 0;
2612 }
2613 *num_ev += 1;
2614 }
2615}
2616
2617static int
2618vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2619 struct epoll_event *events, u32 maxevents,
2620 double wait_for_time, u32 * num_ev)
2621{
Florin Coras99368312018-08-02 10:45:44 -07002622 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002623 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002624 int i;
2625
Florin Coras539663c2018-09-28 14:59:37 -07002626 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2627 goto handle_dequeued;
2628
Florin Coras54693d22018-07-17 10:46:29 -07002629 svm_msg_q_lock (mq);
2630 if (svm_msg_q_is_empty (mq))
2631 {
2632 if (!wait_for_time)
2633 {
2634 svm_msg_q_unlock (mq);
2635 return 0;
2636 }
2637 else if (wait_for_time < 0)
2638 {
2639 svm_msg_q_wait (mq);
2640 }
2641 else
2642 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002643 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002644 {
2645 svm_msg_q_unlock (mq);
2646 return 0;
2647 }
2648 }
2649 }
Florin Coras134a9962018-08-28 11:32:04 -07002650 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07002651 svm_msg_q_unlock (mq);
2652
Florin Coras539663c2018-09-28 14:59:37 -07002653handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002654 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002655 {
Florin Coras134a9962018-08-28 11:32:04 -07002656 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002657 e = svm_msg_q_msg_data (mq, msg);
Florin Corasaa27eb92018-10-13 12:20:01 -07002658 if (*num_ev < maxevents)
2659 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2660 else
2661 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras99368312018-08-02 10:45:44 -07002662 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002663 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002664 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002665 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002666 return *num_ev;
2667}
2668
Florin Coras99368312018-08-02 10:45:44 -07002669static int
Florin Coras134a9962018-08-28 11:32:04 -07002670vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002671 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002672{
2673 vcl_cut_through_registration_t *cr;
2674 double total_wait = 0, wait_slice;
Florin Coras99368312018-08-02 10:45:44 -07002675 int rv;
2676
Florin Coras72f77822019-01-22 19:05:52 -08002677 wait_for_time = (wait_for_time == -1) ? (double) 1e6 : wait_for_time;
Florin Coras134a9962018-08-28 11:32:04 -07002678 wait_slice = wrk->cut_through_registrations ? 10e-6 : wait_for_time;
Florin Coras99368312018-08-02 10:45:44 -07002679
2680 do
2681 {
Florin Coras134a9962018-08-28 11:32:04 -07002682 vcl_ct_registration_lock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07002683 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002684 pool_foreach (cr, wrk->cut_through_registrations, ({
Florin Coras86f04502018-09-12 16:08:01 -07002685 vcl_epoll_wait_handle_mq (wrk, cr->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002686 }));
2687 /* *INDENT-ON* */
Florin Coras134a9962018-08-28 11:32:04 -07002688 vcl_ct_registration_unlock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07002689
Florin Coras134a9962018-08-28 11:32:04 -07002690 rv = vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events,
Florin Coras86f04502018-09-12 16:08:01 -07002691 maxevents, n_evts ? 0 : wait_slice,
2692 &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002693 if (rv)
2694 total_wait += wait_slice;
Florin Coras86f04502018-09-12 16:08:01 -07002695 if (n_evts)
2696 return n_evts;
Florin Coras99368312018-08-02 10:45:44 -07002697 }
2698 while (total_wait < wait_for_time);
Florin Coras86f04502018-09-12 16:08:01 -07002699 return n_evts;
Florin Coras99368312018-08-02 10:45:44 -07002700}
2701
2702static int
Florin Coras134a9962018-08-28 11:32:04 -07002703vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002704 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002705{
2706 vcl_mq_evt_conn_t *mqc;
2707 int __clib_unused n_read;
2708 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07002709 u64 buf;
2710
Florin Coras134a9962018-08-28 11:32:04 -07002711 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07002712again:
Florin Coras134a9962018-08-28 11:32:04 -07002713 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2714 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07002715 for (i = 0; i < n_mq_evts; i++)
2716 {
Florin Coras134a9962018-08-28 11:32:04 -07002717 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002718 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002719 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002720 }
Florin Corasa4878ed2018-10-12 13:09:36 -07002721 if (!n_evts && n_mq_evts > 0)
2722 goto again;
Florin Coras99368312018-08-02 10:45:44 -07002723
2724 return (int) n_evts;
2725}
2726
Dave Wallacef7f809c2017-10-03 01:48:42 -04002727int
Florin Coras134a9962018-08-28 11:32:04 -07002728vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002729 int maxevents, double wait_for_time)
2730{
Florin Coras134a9962018-08-28 11:32:04 -07002731 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002732 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07002733 u32 n_evts = 0;
2734 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002735
2736 if (PREDICT_FALSE (maxevents <= 0))
2737 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002738 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05002739 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002740 return VPPCOM_EINVAL;
2741 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002742
Florin Coras134a9962018-08-28 11:32:04 -07002743 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07002744 if (!vep_session)
2745 return VPPCOM_EBADFD;
2746
Florin Coras54693d22018-07-17 10:46:29 -07002747 if (PREDICT_FALSE (!vep_session->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002748 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002749 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Florin Coras134a9962018-08-28 11:32:04 -07002750 getpid (), vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07002751 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002752 }
Florin Coras54693d22018-07-17 10:46:29 -07002753
2754 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002755
Florin Coras86f04502018-09-12 16:08:01 -07002756 if (vec_len (wrk->unhandled_evts_vector))
2757 {
2758 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2759 {
2760 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2761 events, &n_evts);
2762 if (n_evts == maxevents)
2763 {
2764 i += 1;
2765 break;
2766 }
2767 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002768
Florin Coras86f04502018-09-12 16:08:01 -07002769 vec_delete (wrk->unhandled_evts_vector, i, 0);
2770 }
2771
2772 if (vcm->cfg.use_mq_eventfd)
2773 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2774 wait_for_time);
2775
2776 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2777 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002778}
2779
Dave Wallace35830af2017-10-09 01:43:42 -04002780int
Florin Coras134a9962018-08-28 11:32:04 -07002781vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04002782 void *buffer, uint32_t * buflen)
2783{
Florin Coras134a9962018-08-28 11:32:04 -07002784 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002785 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04002786 int rv = VPPCOM_OK;
Florin Coras7baeb712019-01-04 17:05:43 -08002787 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07002788 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04002789
Florin Coras134a9962018-08-28 11:32:04 -07002790 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002791 if (!session)
2792 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002793
Dave Wallace35830af2017-10-09 01:43:42 -04002794 switch (op)
2795 {
2796 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002797 rv = vcl_session_read_ready (session);
Florin Coras7baeb712019-01-04 17:05:43 -08002798 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d", rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002799 break;
2800
Dave Wallace227867f2017-11-13 21:21:53 -05002801 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002802 rv = vcl_session_write_ready (session);
Florin Coras0d427d82018-06-27 03:24:07 -07002803 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
Florin Coras134a9962018-08-28 11:32:04 -07002804 getpid (), session_handle, rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002805 break;
2806
2807 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002808 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002809 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002810 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2811 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002812 *buflen = sizeof (*flags);
Florin Coras0d427d82018-06-27 03:24:07 -07002813 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2814 "is_nonblocking = %u", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002815 session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002816 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002817 }
2818 else
2819 rv = VPPCOM_EINVAL;
2820 break;
2821
2822 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002823 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002824 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002825 if (*flags & O_NONBLOCK)
2826 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2827 else
2828 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2829
Florin Coras0d427d82018-06-27 03:24:07 -07002830 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2831 " is_nonblocking = %u",
Florin Coras134a9962018-08-28 11:32:04 -07002832 getpid (), session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002833 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002834 }
2835 else
2836 rv = VPPCOM_EINVAL;
2837 break;
2838
2839 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002840 if (PREDICT_TRUE (buffer && buflen &&
2841 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002842 {
Florin Coras7e12d942018-06-27 14:32:43 -07002843 ep->is_ip4 = session->transport.is_ip4;
2844 ep->port = session->transport.rmt_port;
2845 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002846 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2847 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002848 else
Dave Barach178cf492018-11-13 16:34:13 -05002849 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2850 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002851 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002852 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2853 "addr = %U, port %u", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002854 session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002855 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002856 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2857 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002858 }
2859 else
2860 rv = VPPCOM_EINVAL;
2861 break;
2862
2863 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002864 if (PREDICT_TRUE (buffer && buflen &&
2865 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002866 {
Florin Coras7e12d942018-06-27 14:32:43 -07002867 ep->is_ip4 = session->transport.is_ip4;
2868 ep->port = session->transport.lcl_port;
2869 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002870 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2871 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002872 else
Dave Barach178cf492018-11-13 16:34:13 -05002873 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2874 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002875 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002876 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2877 " addr = %U port %d", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002878 session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002879 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002880 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2881 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002882 }
2883 else
2884 rv = VPPCOM_EINVAL;
2885 break;
Stevenb5a11602017-10-11 09:59:30 -07002886
Dave Wallace048b1d62018-01-03 22:24:41 -05002887 case VPPCOM_ATTR_GET_LIBC_EPFD:
2888 rv = session->libc_epfd;
Florin Coras0d427d82018-06-27 03:24:07 -07002889 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2890 getpid (), rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002891 break;
2892
2893 case VPPCOM_ATTR_SET_LIBC_EPFD:
2894 if (PREDICT_TRUE (buffer && buflen &&
2895 (*buflen == sizeof (session->libc_epfd))))
2896 {
2897 session->libc_epfd = *(int *) buffer;
2898 *buflen = sizeof (session->libc_epfd);
2899
Florin Coras0d427d82018-06-27 03:24:07 -07002900 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2901 "buflen %d", getpid (), session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002902 }
2903 else
2904 rv = VPPCOM_EINVAL;
2905 break;
2906
2907 case VPPCOM_ATTR_GET_PROTOCOL:
2908 if (buffer && buflen && (*buflen >= sizeof (int)))
2909 {
Florin Coras7e12d942018-06-27 14:32:43 -07002910 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05002911 *buflen = sizeof (int);
2912
Florin Coras0d427d82018-06-27 03:24:07 -07002913 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2914 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2915 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002916 }
2917 else
2918 rv = VPPCOM_EINVAL;
2919 break;
2920
2921 case VPPCOM_ATTR_GET_LISTEN:
2922 if (buffer && buflen && (*buflen >= sizeof (int)))
2923 {
2924 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2925 VCL_SESS_ATTR_LISTEN);
2926 *buflen = sizeof (int);
2927
Florin Coras0d427d82018-06-27 03:24:07 -07002928 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2929 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002930 }
2931 else
2932 rv = VPPCOM_EINVAL;
2933 break;
2934
2935 case VPPCOM_ATTR_GET_ERROR:
2936 if (buffer && buflen && (*buflen >= sizeof (int)))
2937 {
2938 *(int *) buffer = 0;
2939 *buflen = sizeof (int);
2940
Florin Coras0d427d82018-06-27 03:24:07 -07002941 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2942 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002943 }
2944 else
2945 rv = VPPCOM_EINVAL;
2946 break;
2947
2948 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2949 if (buffer && buflen && (*buflen >= sizeof (u32)))
2950 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002951
2952 /* VPP-TBD */
2953 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002954 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002955 vcm->cfg.tx_fifo_size);
2956 *buflen = sizeof (u32);
2957
Florin Coras0d427d82018-06-27 03:24:07 -07002958 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2959 "buflen %d, #VPP-TBD#", getpid (),
2960 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002961 }
2962 else
2963 rv = VPPCOM_EINVAL;
2964 break;
2965
2966 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2967 if (buffer && buflen && (*buflen == sizeof (u32)))
2968 {
2969 /* VPP-TBD */
2970 session->sndbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07002971 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2972 "buflen %d, #VPP-TBD#", getpid (),
2973 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002974 }
2975 else
2976 rv = VPPCOM_EINVAL;
2977 break;
2978
2979 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2980 if (buffer && buflen && (*buflen >= sizeof (u32)))
2981 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002982
2983 /* VPP-TBD */
2984 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002985 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002986 vcm->cfg.rx_fifo_size);
2987 *buflen = sizeof (u32);
2988
Florin Coras0d427d82018-06-27 03:24:07 -07002989 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2990 "buflen %d, #VPP-TBD#", getpid (),
2991 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002992 }
2993 else
2994 rv = VPPCOM_EINVAL;
2995 break;
2996
2997 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2998 if (buffer && buflen && (*buflen == sizeof (u32)))
2999 {
3000 /* VPP-TBD */
3001 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07003002 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
3003 "buflen %d, #VPP-TBD#", getpid (),
3004 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003005 }
3006 else
3007 rv = VPPCOM_EINVAL;
3008 break;
3009
3010 case VPPCOM_ATTR_GET_REUSEADDR:
3011 if (buffer && buflen && (*buflen >= sizeof (int)))
3012 {
3013 /* VPP-TBD */
3014 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3015 VCL_SESS_ATTR_REUSEADDR);
3016 *buflen = sizeof (int);
3017
Florin Coras0d427d82018-06-27 03:24:07 -07003018 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
3019 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003020 }
3021 else
3022 rv = VPPCOM_EINVAL;
3023 break;
3024
Stevenb5a11602017-10-11 09:59:30 -07003025 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003026 if (buffer && buflen && (*buflen == sizeof (int)) &&
3027 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3028 {
3029 /* VPP-TBD */
3030 if (*(int *) buffer)
3031 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
3032 else
3033 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
3034
Florin Coras0d427d82018-06-27 03:24:07 -07003035 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
3036 " #VPP-TBD#", getpid (),
3037 VCL_SESS_ATTR_TEST (session->attr,
3038 VCL_SESS_ATTR_REUSEADDR), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003039 }
3040 else
3041 rv = VPPCOM_EINVAL;
3042 break;
3043
3044 case VPPCOM_ATTR_GET_REUSEPORT:
3045 if (buffer && buflen && (*buflen >= sizeof (int)))
3046 {
3047 /* VPP-TBD */
3048 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3049 VCL_SESS_ATTR_REUSEPORT);
3050 *buflen = sizeof (int);
3051
Florin Coras0d427d82018-06-27 03:24:07 -07003052 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
3053 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003054 }
3055 else
3056 rv = VPPCOM_EINVAL;
3057 break;
3058
3059 case VPPCOM_ATTR_SET_REUSEPORT:
3060 if (buffer && buflen && (*buflen == sizeof (int)) &&
3061 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3062 {
3063 /* VPP-TBD */
3064 if (*(int *) buffer)
3065 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
3066 else
3067 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
3068
Florin Coras0d427d82018-06-27 03:24:07 -07003069 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
3070 " #VPP-TBD#", getpid (),
3071 VCL_SESS_ATTR_TEST (session->attr,
3072 VCL_SESS_ATTR_REUSEPORT), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003073 }
3074 else
3075 rv = VPPCOM_EINVAL;
3076 break;
3077
3078 case VPPCOM_ATTR_GET_BROADCAST:
3079 if (buffer && buflen && (*buflen >= sizeof (int)))
3080 {
3081 /* VPP-TBD */
3082 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3083 VCL_SESS_ATTR_BROADCAST);
3084 *buflen = sizeof (int);
3085
Florin Coras0d427d82018-06-27 03:24:07 -07003086 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
3087 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003088 }
3089 else
3090 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003091 break;
3092
3093 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003094 if (buffer && buflen && (*buflen == sizeof (int)))
3095 {
3096 /* VPP-TBD */
3097 if (*(int *) buffer)
3098 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
3099 else
3100 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
3101
Florin Coras0d427d82018-06-27 03:24:07 -07003102 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
3103 "#VPP-TBD#", getpid (),
3104 VCL_SESS_ATTR_TEST (session->attr,
3105 VCL_SESS_ATTR_BROADCAST), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003106 }
3107 else
3108 rv = VPPCOM_EINVAL;
3109 break;
3110
3111 case VPPCOM_ATTR_GET_V6ONLY:
3112 if (buffer && buflen && (*buflen >= sizeof (int)))
3113 {
3114 /* VPP-TBD */
3115 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3116 VCL_SESS_ATTR_V6ONLY);
3117 *buflen = sizeof (int);
3118
Florin Coras0d427d82018-06-27 03:24:07 -07003119 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
3120 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003121 }
3122 else
3123 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003124 break;
3125
3126 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003127 if (buffer && buflen && (*buflen == sizeof (int)))
3128 {
3129 /* VPP-TBD */
3130 if (*(int *) buffer)
3131 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
3132 else
3133 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
3134
Florin Coras0d427d82018-06-27 03:24:07 -07003135 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
3136 "#VPP-TBD#", getpid (),
3137 VCL_SESS_ATTR_TEST (session->attr,
3138 VCL_SESS_ATTR_V6ONLY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003139 }
3140 else
3141 rv = VPPCOM_EINVAL;
3142 break;
3143
3144 case VPPCOM_ATTR_GET_KEEPALIVE:
3145 if (buffer && buflen && (*buflen >= sizeof (int)))
3146 {
3147 /* VPP-TBD */
3148 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3149 VCL_SESS_ATTR_KEEPALIVE);
3150 *buflen = sizeof (int);
3151
Florin Coras0d427d82018-06-27 03:24:07 -07003152 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
3153 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003154 }
3155 else
3156 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003157 break;
Stevenbccd3392017-10-12 20:42:21 -07003158
3159 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003160 if (buffer && buflen && (*buflen == sizeof (int)))
3161 {
3162 /* VPP-TBD */
3163 if (*(int *) buffer)
3164 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3165 else
3166 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3167
Florin Coras0d427d82018-06-27 03:24:07 -07003168 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
3169 "#VPP-TBD#", getpid (),
3170 VCL_SESS_ATTR_TEST (session->attr,
3171 VCL_SESS_ATTR_KEEPALIVE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003172 }
3173 else
3174 rv = VPPCOM_EINVAL;
3175 break;
3176
3177 case VPPCOM_ATTR_GET_TCP_NODELAY:
3178 if (buffer && buflen && (*buflen >= sizeof (int)))
3179 {
3180 /* VPP-TBD */
3181 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3182 VCL_SESS_ATTR_TCP_NODELAY);
3183 *buflen = sizeof (int);
3184
Florin Coras0d427d82018-06-27 03:24:07 -07003185 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
3186 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003187 }
3188 else
3189 rv = VPPCOM_EINVAL;
3190 break;
3191
3192 case VPPCOM_ATTR_SET_TCP_NODELAY:
3193 if (buffer && buflen && (*buflen == sizeof (int)))
3194 {
3195 /* VPP-TBD */
3196 if (*(int *) buffer)
3197 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3198 else
3199 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3200
Florin Coras0d427d82018-06-27 03:24:07 -07003201 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
3202 "#VPP-TBD#", getpid (),
3203 VCL_SESS_ATTR_TEST (session->attr,
3204 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003205 }
3206 else
3207 rv = VPPCOM_EINVAL;
3208 break;
3209
3210 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3211 if (buffer && buflen && (*buflen >= sizeof (int)))
3212 {
3213 /* VPP-TBD */
3214 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3215 VCL_SESS_ATTR_TCP_KEEPIDLE);
3216 *buflen = sizeof (int);
3217
Florin Coras0d427d82018-06-27 03:24:07 -07003218 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
3219 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003220 }
3221 else
3222 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003223 break;
3224
3225 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003226 if (buffer && buflen && (*buflen == sizeof (int)))
3227 {
3228 /* VPP-TBD */
3229 if (*(int *) buffer)
3230 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3231 else
3232 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3233
Florin Coras0d427d82018-06-27 03:24:07 -07003234 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
3235 "#VPP-TBD#", getpid (),
3236 VCL_SESS_ATTR_TEST (session->attr,
3237 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003238 }
3239 else
3240 rv = VPPCOM_EINVAL;
3241 break;
3242
3243 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3244 if (buffer && buflen && (*buflen >= sizeof (int)))
3245 {
3246 /* VPP-TBD */
3247 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3248 VCL_SESS_ATTR_TCP_KEEPINTVL);
3249 *buflen = sizeof (int);
3250
Florin Coras0d427d82018-06-27 03:24:07 -07003251 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
3252 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003253 }
3254 else
3255 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003256 break;
3257
3258 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003259 if (buffer && buflen && (*buflen == sizeof (int)))
3260 {
3261 /* VPP-TBD */
3262 if (*(int *) buffer)
3263 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3264 else
3265 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3266
Florin Coras0d427d82018-06-27 03:24:07 -07003267 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
3268 "#VPP-TBD#", getpid (),
3269 VCL_SESS_ATTR_TEST (session->attr,
3270 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003271 }
3272 else
3273 rv = VPPCOM_EINVAL;
3274 break;
3275
3276 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3277 if (buffer && buflen && (*buflen >= sizeof (u32)))
3278 {
3279 /* VPP-TBD */
3280 *(u32 *) buffer = session->user_mss;
3281 *buflen = sizeof (int);
3282
Florin Coras0d427d82018-06-27 03:24:07 -07003283 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
3284 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003285 }
3286 else
3287 rv = VPPCOM_EINVAL;
3288 break;
3289
3290 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3291 if (buffer && buflen && (*buflen == sizeof (u32)))
3292 {
3293 /* VPP-TBD */
3294 session->user_mss = *(u32 *) buffer;
3295
Florin Coras0d427d82018-06-27 03:24:07 -07003296 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
3297 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003298 }
3299 else
3300 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003301 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003302
Florin Coras7baeb712019-01-04 17:05:43 -08003303 case VPPCOM_ATTR_SET_SHUT:
3304 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3305 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3306 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3307 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3308 break;
3309
3310 case VPPCOM_ATTR_GET_SHUT:
3311 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3312 tmp_flags = 1;
3313 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3314 tmp_flags |= 2;
3315 if (tmp_flags == 1)
3316 *(int *) buffer = SHUT_RD;
3317 else if (tmp_flags == 2)
3318 *(int *) buffer = SHUT_WR;
3319 else if (tmp_flags == 3)
3320 *(int *) buffer = SHUT_RDWR;
3321 *buflen = sizeof (int);
3322 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003323 default:
3324 rv = VPPCOM_EINVAL;
3325 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003326 }
3327
Dave Wallace35830af2017-10-09 01:43:42 -04003328 return rv;
3329}
3330
Stevenac1f96d2017-10-24 16:03:58 -07003331int
Florin Coras134a9962018-08-28 11:32:04 -07003332vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003333 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3334{
Florin Coras134a9962018-08-28 11:32:04 -07003335 vcl_worker_t *wrk = vcl_worker_get_current ();
Stevenac1f96d2017-10-24 16:03:58 -07003336 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07003337 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07003338
3339 if (ep)
3340 {
Florin Coras134a9962018-08-28 11:32:04 -07003341 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003342 if (PREDICT_FALSE (!session))
Stevenac1f96d2017-10-24 16:03:58 -07003343 {
Florin Coras0d427d82018-06-27 03:24:07 -07003344 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
Florin Coras134a9962018-08-28 11:32:04 -07003345 getpid (), session_handle);
Florin Coras460dce62018-07-27 05:45:06 -07003346 return VPPCOM_EBADFD;
Stevenac1f96d2017-10-24 16:03:58 -07003347 }
Florin Coras7e12d942018-06-27 14:32:43 -07003348 ep->is_ip4 = session->transport.is_ip4;
3349 ep->port = session->transport.rmt_port;
Stevenac1f96d2017-10-24 16:03:58 -07003350 }
Steven58f464e2017-10-25 12:33:12 -07003351
3352 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003353 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003354 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003355 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003356 else
3357 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003358 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003359 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003360 }
3361
Florin Coras99368312018-08-02 10:45:44 -07003362 if (ep)
3363 {
3364 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003365 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3366 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003367 else
Dave Barach178cf492018-11-13 16:34:13 -05003368 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3369 sizeof (ip6_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003370 }
Florin Coras460dce62018-07-27 05:45:06 -07003371
Stevenac1f96d2017-10-24 16:03:58 -07003372 return rv;
3373}
3374
3375int
Florin Coras134a9962018-08-28 11:32:04 -07003376vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003377 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3378{
Dave Wallace617dffa2017-10-26 14:47:06 -04003379 if (!buffer)
3380 return VPPCOM_EINVAL;
3381
3382 if (ep)
3383 {
3384 // TBD
3385 return VPPCOM_EINVAL;
3386 }
3387
3388 if (flags)
3389 {
3390 // TBD check the flags and do the right thing
Florin Coras0d427d82018-06-27 03:24:07 -07003391 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3392 getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003393 }
3394
Florin Coras42ceddb2018-12-12 10:56:01 -08003395 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
Stevenac1f96d2017-10-24 16:03:58 -07003396}
3397
Dave Wallace048b1d62018-01-03 22:24:41 -05003398int
3399vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3400{
Florin Coras134a9962018-08-28 11:32:04 -07003401 vcl_worker_t *wrk = vcl_worker_get_current ();
3402 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003403 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003404 svm_msg_q_msg_t msg;
3405 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003406 int rv, num_ev = 0;
3407
Florin Coras0d427d82018-06-27 03:24:07 -07003408 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3409 getpid (), vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003410
3411 if (!vp)
3412 return VPPCOM_EFAULT;
3413
3414 do
3415 {
Florin Coras7e12d942018-06-27 14:32:43 -07003416 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003417
Florin Coras6917b942018-11-13 22:44:54 -08003418 /* Dequeue all events and drop all unhandled io events */
3419 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3420 {
3421 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3422 vcl_handle_mq_event (wrk, e);
3423 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3424 }
3425 vec_reset_length (wrk->unhandled_evts_vector);
3426
Dave Wallace048b1d62018-01-03 22:24:41 -05003427 for (i = 0; i < n_sids; i++)
3428 {
Florin Coras7baeb712019-01-04 17:05:43 -08003429 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003430 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003431 {
3432 vp[i].revents = POLLHUP;
3433 num_ev++;
3434 continue;
3435 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003436
Florin Coras6917b942018-11-13 22:44:54 -08003437 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003438
3439 if (POLLIN & vp[i].events)
3440 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003441 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003442 if (rv > 0)
3443 {
Florin Coras6917b942018-11-13 22:44:54 -08003444 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003445 num_ev++;
3446 }
3447 else if (rv < 0)
3448 {
3449 switch (rv)
3450 {
3451 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003452 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003453 break;
3454
3455 default:
Florin Coras6917b942018-11-13 22:44:54 -08003456 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003457 break;
3458 }
3459 num_ev++;
3460 }
3461 }
3462
3463 if (POLLOUT & vp[i].events)
3464 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003465 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003466 if (rv > 0)
3467 {
Florin Coras6917b942018-11-13 22:44:54 -08003468 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003469 num_ev++;
3470 }
3471 else if (rv < 0)
3472 {
3473 switch (rv)
3474 {
3475 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003476 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003477 break;
3478
3479 default:
Florin Coras6917b942018-11-13 22:44:54 -08003480 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003481 break;
3482 }
3483 num_ev++;
3484 }
3485 }
3486
Dave Wallace7e607a72018-06-18 18:41:32 -04003487 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003488 {
Florin Coras6917b942018-11-13 22:44:54 -08003489 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003490 num_ev++;
3491 }
3492 }
3493 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003494 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003495 }
3496 while ((num_ev == 0) && keep_trying);
3497
3498 if (VPPCOM_DEBUG > 3)
3499 {
3500 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3501 for (i = 0; i < n_sids; i++)
3502 {
3503 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
Florin Coras7baeb712019-01-04 17:05:43 -08003504 ".revents 0x%x", getpid (), i, vp[i].sh, vp[i].sh,
Florin Coras6917b942018-11-13 22:44:54 -08003505 vp[i].events, vp[i].revents);
Dave Wallace048b1d62018-01-03 22:24:41 -05003506 }
3507 }
3508 return num_ev;
3509}
3510
Florin Coras99368312018-08-02 10:45:44 -07003511int
3512vppcom_mq_epoll_fd (void)
3513{
Florin Coras134a9962018-08-28 11:32:04 -07003514 vcl_worker_t *wrk = vcl_worker_get_current ();
3515 return wrk->mqs_epfd;
3516}
3517
3518int
Florin Coras30e79c22019-01-02 19:31:22 -08003519vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003520{
3521 return session_handle & 0xFFFFFF;
3522}
3523
3524int
Florin Coras30e79c22019-01-02 19:31:22 -08003525vppcom_session_worker (vcl_session_handle_t session_handle)
3526{
3527 return session_handle >> 24;
3528}
3529
3530int
Florin Coras134a9962018-08-28 11:32:04 -07003531vppcom_worker_register (void)
3532{
Florin Coras47c40e22018-11-26 17:01:36 -08003533 if (!vcl_worker_alloc_and_init ())
3534 return VPPCOM_EEXIST;
3535
3536 if (vcl_worker_set_bapi ())
3537 return VPPCOM_EEXIST;
3538
3539 if (vcl_worker_register_with_vpp ())
3540 return VPPCOM_EEXIST;
3541
3542 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003543}
3544
Florin Corasdfe4cf42018-11-28 22:13:45 -08003545int
3546vppcom_worker_index (void)
3547{
3548 return vcl_get_worker_index ();
3549}
3550
Florin Corase0982e52019-01-25 13:19:56 -08003551int
3552vppcom_worker_mqs_epfd (void)
3553{
3554 vcl_worker_t *wrk = vcl_worker_get_current ();
3555 if (!vcm->cfg.use_mq_eventfd)
3556 return -1;
3557 return wrk->mqs_epfd;
3558}
3559
Dave Wallacee22aa742017-10-20 12:30:38 -04003560/*
3561 * fd.io coding-style-patch-verification: ON
3562 *
3563 * Local Variables:
3564 * eval: (c-set-style "gnu")
3565 * End:
3566 */