blob: 7274749a93d504f7765012111a2db63d060b26a0 [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 Coras54693d22018-07-17 10:46:29 -070025static u8 not_ready;
26
27void
28sigsegv_signal (int signum)
29{
30 not_ready = 1;
31}
32
33static void
34vcl_wait_for_memory (void *mem)
35{
36 u8 __clib_unused test;
Florin Coras460dce62018-07-27 05:45:06 -070037 if (vcm->mounting_segment)
38 {
39 while (vcm->mounting_segment)
40 ;
41 return;
42 }
Florin Coras54693d22018-07-17 10:46:29 -070043 if (1 || vcm->debug)
44 {
Florin Coras460dce62018-07-27 05:45:06 -070045 usleep (1e5);
Florin Coras54693d22018-07-17 10:46:29 -070046 return;
47 }
48 if (signal (SIGSEGV, sigsegv_signal))
49 {
50 perror ("signal()");
51 return;
52 }
53 not_ready = 0;
54
55again:
56 test = *(u8 *) mem;
57 if (not_ready)
58 {
59 not_ready = 0;
60 usleep (1);
61 goto again;
62 }
63
64 signal (SIGSEGV, SIG_DFL);
65}
66
Florin Coras697faea2018-06-27 17:10:49 -070067const char *
Dave Wallace543852a2017-08-03 02:11:34 -040068vppcom_session_state_str (session_state_t state)
69{
70 char *st;
71
72 switch (state)
73 {
74 case STATE_START:
75 st = "STATE_START";
76 break;
77
78 case STATE_CONNECT:
79 st = "STATE_CONNECT";
80 break;
81
82 case STATE_LISTEN:
83 st = "STATE_LISTEN";
84 break;
85
86 case STATE_ACCEPT:
87 st = "STATE_ACCEPT";
88 break;
89
Dave Wallace4878cbe2017-11-21 03:45:09 -050090 case STATE_CLOSE_ON_EMPTY:
91 st = "STATE_CLOSE_ON_EMPTY";
92 break;
93
Dave Wallace543852a2017-08-03 02:11:34 -040094 case STATE_DISCONNECT:
95 st = "STATE_DISCONNECT";
96 break;
97
98 case STATE_FAILED:
99 st = "STATE_FAILED";
100 break;
101
102 default:
103 st = "UNKNOWN_STATE";
104 break;
105 }
106
107 return st;
108}
109
Dave Wallace543852a2017-08-03 02:11:34 -0400110u8 *
111format_ip4_address (u8 * s, va_list * args)
112{
113 u8 *a = va_arg (*args, u8 *);
114 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
115}
116
117u8 *
118format_ip6_address (u8 * s, va_list * args)
119{
120 ip6_address_t *a = va_arg (*args, ip6_address_t *);
121 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
122
123 i_max_n_zero = ARRAY_LEN (a->as_u16);
124 max_n_zeros = 0;
125 i_first_zero = i_max_n_zero;
126 n_zeros = 0;
127 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
128 {
129 u32 is_zero = a->as_u16[i] == 0;
130 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
131 {
132 i_first_zero = i;
133 n_zeros = 0;
134 }
135 n_zeros += is_zero;
136 if ((!is_zero && n_zeros > max_n_zeros)
137 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
138 {
139 i_max_n_zero = i_first_zero;
140 max_n_zeros = n_zeros;
141 i_first_zero = ARRAY_LEN (a->as_u16);
142 n_zeros = 0;
143 }
144 }
145
146 last_double_colon = 0;
147 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
148 {
149 if (i == i_max_n_zero && max_n_zeros > 1)
150 {
151 s = format (s, "::");
152 i += max_n_zeros - 1;
153 last_double_colon = 1;
154 }
155 else
156 {
157 s = format (s, "%s%x",
158 (last_double_colon || i == 0) ? "" : ":",
159 clib_net_to_host_u16 (a->as_u16[i]));
160 last_double_colon = 0;
161 }
162 }
163
164 return s;
165}
166
167/* Format an IP46 address. */
168u8 *
169format_ip46_address (u8 * s, va_list * args)
170{
171 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
172 ip46_type_t type = va_arg (*args, ip46_type_t);
173 int is_ip4 = 1;
174
175 switch (type)
176 {
177 case IP46_TYPE_ANY:
178 is_ip4 = ip46_address_is_ip4 (ip46);
179 break;
180 case IP46_TYPE_IP4:
181 is_ip4 = 1;
182 break;
183 case IP46_TYPE_IP6:
184 is_ip4 = 0;
185 break;
186 }
187
188 return is_ip4 ?
189 format (s, "%U", format_ip4_address, &ip46->ip4) :
190 format (s, "%U", format_ip6_address, &ip46->ip6);
191}
192
Florin Coras697faea2018-06-27 17:10:49 -0700193/*
194 * VPPCOM Utility Functions
195 */
196
Florin Coras697faea2018-06-27 17:10:49 -0700197
Florin Corasc9fbd662018-08-24 12:59:56 -0700198static svm_msg_q_t *
Florin Coras134a9962018-08-28 11:32:04 -0700199vcl_session_vpp_evt_q (vcl_worker_t * wrk, vcl_session_t * s)
Florin Corasc9fbd662018-08-24 12:59:56 -0700200{
201 if (vcl_session_is_ct (s))
Florin Coras134a9962018-08-28 11:32:04 -0700202 return wrk->vpp_event_queues[0];
Florin Corasc9fbd662018-08-24 12:59:56 -0700203 else
Florin Coras134a9962018-08-28 11:32:04 -0700204 return wrk->vpp_event_queues[s->tx_fifo->master_thread_index];
Florin Corasc9fbd662018-08-24 12:59:56 -0700205}
206
Florin Coras54693d22018-07-17 10:46:29 -0700207static void
208vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
209 session_handle_t handle, int retval)
210{
211 app_session_evt_t _app_evt, *app_evt = &_app_evt;
212 session_accepted_reply_msg_t *rmp;
213 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
214 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
215 rmp->handle = handle;
216 rmp->context = context;
217 rmp->retval = retval;
218 app_send_ctrl_evt_to_vpp (mq, app_evt);
219}
220
Florin Coras99368312018-08-02 10:45:44 -0700221static void
222vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
223 session_handle_t handle, int retval)
224{
225 app_session_evt_t _app_evt, *app_evt = &_app_evt;
226 session_disconnected_reply_msg_t *rmp;
227 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
228 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
229 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
230 rmp->handle = handle;
231 rmp->context = context;
232 rmp->retval = retval;
233 app_send_ctrl_evt_to_vpp (mq, app_evt);
234}
235
Florin Corasc9fbd662018-08-24 12:59:56 -0700236static void
237vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
238 session_handle_t handle, int retval)
239{
240 app_session_evt_t _app_evt, *app_evt = &_app_evt;
241 session_reset_reply_msg_t *rmp;
242 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
243 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
244 rmp->handle = handle;
245 rmp->context = context;
246 rmp->retval = retval;
247 app_send_ctrl_evt_to_vpp (mq, app_evt);
248}
249
Florin Coras54693d22018-07-17 10:46:29 -0700250static u32
Florin Coras134a9962018-08-28 11:32:04 -0700251vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700252{
253 vcl_session_t *session, *listen_session;
254 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras134a9962018-08-28 11:32:04 -0700255 u32 vpp_wrk_index;
Florin Coras99368312018-08-02 10:45:44 -0700256 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700257
Florin Coras134a9962018-08-28 11:32:04 -0700258 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700259
Florin Coras134a9962018-08-28 11:32:04 -0700260 listen_session = vcl_session_table_lookup_listener (wrk,
261 mp->listener_handle);
Florin Coras54693d22018-07-17 10:46:29 -0700262 if (!listen_session)
263 {
264 svm_msg_q_t *evt_q;
265 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
266 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
267 "unknown vpp listener handle %llx",
268 getpid (), mp->listener_handle);
269 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
270 VNET_API_ERROR_INVALID_ARGUMENT);
Florin Coras134a9962018-08-28 11:32:04 -0700271 vcl_session_free (wrk, session);
Florin Coras54693d22018-07-17 10:46:29 -0700272 return VCL_INVALID_SESSION_INDEX;
273 }
274
Florin Coras54693d22018-07-17 10:46:29 -0700275 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
276 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
277
278 if (mp->server_event_queue_address)
279 {
280 session->vpp_evt_q = uword_to_pointer (mp->client_event_queue_address,
281 svm_msg_q_t *);
282 session->our_evt_q = uword_to_pointer (mp->server_event_queue_address,
283 svm_msg_q_t *);
284 vcl_wait_for_memory (session->vpp_evt_q);
Florin Coras134a9962018-08-28 11:32:04 -0700285 rx_fifo->master_session_index = session->session_index;
286 tx_fifo->master_session_index = session->session_index;
Florin Coras21795132018-09-09 09:40:51 -0700287 rx_fifo->master_thread_index = vcl_get_worker_index ();
288 tx_fifo->master_thread_index = vcl_get_worker_index ();
Florin Coras134a9962018-08-28 11:32:04 -0700289 vec_validate (wrk->vpp_event_queues, 0);
Florin Coras99368312018-08-02 10:45:44 -0700290 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
Florin Coras134a9962018-08-28 11:32:04 -0700291 wrk->vpp_event_queues[0] = evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700292 }
293 else
294 {
295 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
296 svm_msg_q_t *);
Florin Coras134a9962018-08-28 11:32:04 -0700297 rx_fifo->client_session_index = session->session_index;
298 tx_fifo->client_session_index = session->session_index;
Florin Coras21795132018-09-09 09:40:51 -0700299 rx_fifo->client_thread_index = vcl_get_worker_index ();
300 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras99368312018-08-02 10:45:44 -0700301 vpp_wrk_index = tx_fifo->master_thread_index;
Florin Coras134a9962018-08-28 11:32:04 -0700302 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
303 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700304 }
305
306 session->vpp_handle = mp->handle;
307 session->client_context = mp->context;
308 session->rx_fifo = rx_fifo;
309 session->tx_fifo = tx_fifo;
310
311 session->session_state = STATE_ACCEPT;
312 session->transport.rmt_port = mp->port;
313 session->transport.is_ip4 = mp->is_ip4;
314 clib_memcpy (&session->transport.rmt_ip, mp->ip, sizeof (ip46_address_t));
315
Florin Coras134a9962018-08-28 11:32:04 -0700316 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700317 session->transport.lcl_port = listen_session->transport.lcl_port;
318 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700319 session->session_type = listen_session->session_type;
320 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
Florin Coras54693d22018-07-17 10:46:29 -0700321
322 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
Florin Coras134a9962018-08-28 11:32:04 -0700323 " address %U port %d queue %p!", getpid (), mp->handle,
324 session->session_index,
Florin Coras54693d22018-07-17 10:46:29 -0700325 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
326 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
327 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
328 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
329
Florin Coras134a9962018-08-28 11:32:04 -0700330 return session->session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700331}
332
333static u32
Florin Coras134a9962018-08-28 11:32:04 -0700334vcl_session_connected_handler (vcl_worker_t * wrk,
335 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700336{
Florin Coras99368312018-08-02 10:45:44 -0700337 u32 session_index, vpp_wrk_index;
Florin Coras54693d22018-07-17 10:46:29 -0700338 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700339 vcl_session_t *session = 0;
340 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700341
342 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700343 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700344 if (!session)
345 {
346 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
347 "Invalid session index (%u)!",
348 getpid (), mp->handle, session_index);
349 return VCL_INVALID_SESSION_INDEX;
350 }
Florin Coras54693d22018-07-17 10:46:29 -0700351 if (mp->retval)
352 {
Florin Coras070453d2018-08-24 17:04:27 -0700353 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed! %U", getpid (),
Florin Coras21795132018-09-09 09:40:51 -0700354 session_index, format_api_error, ntohl (mp->retval));
Florin Coras070453d2018-08-24 17:04:27 -0700355 session->session_state = STATE_FAILED;
356 session->vpp_handle = mp->handle;
357 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700358 }
359
Florin Coras460dce62018-07-27 05:45:06 -0700360 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
361 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
362 vcl_wait_for_memory (rx_fifo);
363 rx_fifo->client_session_index = session_index;
364 tx_fifo->client_session_index = session_index;
Florin Coras21795132018-09-09 09:40:51 -0700365 rx_fifo->client_thread_index = vcl_get_worker_index ();
366 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras460dce62018-07-27 05:45:06 -0700367
Florin Coras54693d22018-07-17 10:46:29 -0700368 if (mp->client_event_queue_address)
369 {
370 session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address,
371 svm_msg_q_t *);
372 session->our_evt_q = uword_to_pointer (mp->client_event_queue_address,
373 svm_msg_q_t *);
Florin Coras99368312018-08-02 10:45:44 -0700374
Florin Coras134a9962018-08-28 11:32:04 -0700375 vec_validate (wrk->vpp_event_queues, 0);
Florin Coras99368312018-08-02 10:45:44 -0700376 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
Florin Coras134a9962018-08-28 11:32:04 -0700377 wrk->vpp_event_queues[0] = evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700378 }
379 else
Florin Coras99368312018-08-02 10:45:44 -0700380 {
381 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
382 svm_msg_q_t *);
383 vpp_wrk_index = tx_fifo->master_thread_index;
Florin Coras134a9962018-08-28 11:32:04 -0700384 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
385 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -0700386 }
Florin Coras54693d22018-07-17 10:46:29 -0700387
Florin Coras54693d22018-07-17 10:46:29 -0700388 session->rx_fifo = rx_fifo;
389 session->tx_fifo = tx_fifo;
390 session->vpp_handle = mp->handle;
391 session->transport.is_ip4 = mp->is_ip4;
392 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
393 sizeof (session->transport.lcl_ip));
394 session->transport.lcl_port = mp->lcl_port;
395 session->session_state = STATE_CONNECT;
396
397 /* Add it to lookup table */
Florin Coras134a9962018-08-28 11:32:04 -0700398 hash_set (wrk->session_index_by_vpp_handles, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700399
400 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
401 "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
402 getpid (), mp->handle, session_index, session->rx_fifo,
403 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700404
Florin Coras54693d22018-07-17 10:46:29 -0700405 return session_index;
406}
407
Florin Corasc9fbd662018-08-24 12:59:56 -0700408static u32
Florin Coras134a9962018-08-28 11:32:04 -0700409vcl_session_reset_handler (vcl_worker_t * wrk,
410 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700411{
412 vcl_session_t *session;
413 u32 sid;
414
Florin Coras134a9962018-08-28 11:32:04 -0700415 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
416 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700417 if (!session)
418 {
419 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
420 return VCL_INVALID_SESSION_INDEX;
421 }
422 session->session_state = STATE_CLOSE_ON_EMPTY;
423 VDBG (0, "reset handle 0x%llx, sid %u ", reset_msg->handle, sid);
Florin Coras134a9962018-08-28 11:32:04 -0700424 vcl_send_session_reset_reply (vcl_session_vpp_evt_q (wrk, session),
Florin Corasc9fbd662018-08-24 12:59:56 -0700425 vcm->my_client_index, reset_msg->handle, 0);
426 return sid;
427}
428
Florin Coras60116992018-08-27 09:52:18 -0700429static u32
Florin Coras134a9962018-08-28 11:32:04 -0700430vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700431{
432 vcl_session_t *session;
433 u32 sid = mp->context;
434
Florin Coras134a9962018-08-28 11:32:04 -0700435 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700436 if (mp->retval)
437 {
438 VDBG (0, "VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: bind failed: %U",
439 getpid (), mp->handle, sid, format_api_error, ntohl (mp->retval));
440 if (session)
441 {
442 session->session_state = STATE_FAILED;
443 session->vpp_handle = mp->handle;
444 return sid;
445 }
446 else
447 {
448 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
449 "Invalid session index (%u)!",
450 getpid (), mp->handle, sid);
451 return VCL_INVALID_SESSION_INDEX;
452 }
453 }
454
455 session->vpp_handle = mp->handle;
456 session->transport.is_ip4 = mp->lcl_is_ip4;
457 clib_memcpy (&session->transport.lcl_ip, mp->lcl_ip,
458 sizeof (ip46_address_t));
459 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700460 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Coras60116992018-08-27 09:52:18 -0700461 session->session_state = STATE_LISTEN;
462
463 if (session->is_dgram)
464 {
465 svm_fifo_t *rx_fifo, *tx_fifo;
466 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
467 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
468 rx_fifo->client_session_index = sid;
469 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
470 tx_fifo->client_session_index = sid;
471 session->rx_fifo = rx_fifo;
472 session->tx_fifo = tx_fifo;
473 }
474
475 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
476 getpid (), mp->handle, sid);
477 return sid;
478}
479
Florin Coras86f04502018-09-12 16:08:01 -0700480static int
481vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700482{
483 session_accepted_msg_t *accepted_msg;
484 session_disconnected_msg_t *disconnected_msg;
485 vcl_session_msg_t *vcl_msg;
486 vcl_session_t *session;
487 u64 handle;
488 u32 sid;
489
490 switch (e->event_type)
491 {
492 case FIFO_EVENT_APP_RX:
Florin Coras86f04502018-09-12 16:08:01 -0700493 case FIFO_EVENT_APP_TX:
494 case SESSION_IO_EVT_CT_RX:
495 case SESSION_IO_EVT_CT_TX:
496 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -0700497 break;
498 case SESSION_CTRL_EVT_ACCEPTED:
499 accepted_msg = (session_accepted_msg_t *) e->data;
500 handle = accepted_msg->listener_handle;
Florin Coras134a9962018-08-28 11:32:04 -0700501 session = vcl_session_table_lookup_listener (wrk, handle);
Florin Coras54693d22018-07-17 10:46:29 -0700502 if (!session)
503 {
504 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
505 "listener handle %llx", getpid (), handle);
506 break;
507 }
508
509 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
510 vcl_msg->accepted_msg = *accepted_msg;
511 break;
512 case SESSION_CTRL_EVT_CONNECTED:
Florin Coras134a9962018-08-28 11:32:04 -0700513 vcl_session_connected_handler (wrk,
514 (session_connected_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700515 break;
516 case SESSION_CTRL_EVT_DISCONNECTED:
517 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras134a9962018-08-28 11:32:04 -0700518 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
519 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700520 if (!session)
521 {
522 VDBG (0, "request to disconnect unknown handle 0x%llx",
523 disconnected_msg->handle);
524 break;
525 }
Florin Coras54693d22018-07-17 10:46:29 -0700526 session->session_state = STATE_DISCONNECT;
Florin Coras86f04502018-09-12 16:08:01 -0700527 VDBG (0, "disconnected handle 0x%llx, sid %u", disconnected_msg->handle,
Florin Corasc9fbd662018-08-24 12:59:56 -0700528 sid);
529 break;
530 case SESSION_CTRL_EVT_RESET:
Florin Coras134a9962018-08-28 11:32:04 -0700531 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -0700532 break;
533 case SESSION_CTRL_EVT_BOUND:
Florin Coras134a9962018-08-28 11:32:04 -0700534 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700535 break;
536 default:
537 clib_warning ("unhandled %u", e->event_type);
538 }
539 return VPPCOM_OK;
540}
541
Florin Coras697faea2018-06-27 17:10:49 -0700542static inline int
543vppcom_wait_for_session_state_change (u32 session_index,
544 session_state_t state,
545 f64 wait_for_time)
546{
Florin Coras134a9962018-08-28 11:32:04 -0700547 vcl_worker_t *wrk = vcl_worker_get_current ();
548 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -0700549 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -0700550 svm_msg_q_msg_t msg;
551 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -0400552
Florin Coras697faea2018-06-27 17:10:49 -0700553 do
Dave Wallace543852a2017-08-03 02:11:34 -0400554 {
Florin Coras134a9962018-08-28 11:32:04 -0700555 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700556 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -0700557 {
Florin Coras070453d2018-08-24 17:04:27 -0700558 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -0700559 }
560 if (session->session_state & state)
561 {
Florin Coras697faea2018-06-27 17:10:49 -0700562 return VPPCOM_OK;
563 }
564 if (session->session_state & STATE_FAILED)
565 {
Florin Coras697faea2018-06-27 17:10:49 -0700566 return VPPCOM_ECONNREFUSED;
567 }
Florin Coras54693d22018-07-17 10:46:29 -0700568
Florin Coras134a9962018-08-28 11:32:04 -0700569 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -0700570 continue;
Florin Coras134a9962018-08-28 11:32:04 -0700571 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -0700572 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -0700573 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800574 }
Florin Coras134a9962018-08-28 11:32:04 -0700575 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800576
Florin Coras697faea2018-06-27 17:10:49 -0700577 VDBG (0, "VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (), state,
578 vppcom_session_state_str (state));
579 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -0400580
Florin Coras697faea2018-06-27 17:10:49 -0700581 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -0500582}
583
Florin Coras697faea2018-06-27 17:10:49 -0700584static int
585vppcom_app_session_enable (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400586{
Florin Coras697faea2018-06-27 17:10:49 -0700587 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400588
Florin Coras697faea2018-06-27 17:10:49 -0700589 if (vcm->app_state != STATE_APP_ENABLED)
590 {
591 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
Florin Coras134a9962018-08-28 11:32:04 -0700592 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
Florin Coras697faea2018-06-27 17:10:49 -0700593 if (PREDICT_FALSE (rv))
594 {
595 VDBG (0, "VCL<%d>: application session enable timed out! "
596 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
597 return rv;
598 }
599 }
600 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400601}
602
Florin Coras697faea2018-06-27 17:10:49 -0700603static int
604vppcom_app_attach (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400605{
Florin Coras697faea2018-06-27 17:10:49 -0700606 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400607
Florin Coras697faea2018-06-27 17:10:49 -0700608 vppcom_app_send_attach ();
Florin Coras134a9962018-08-28 11:32:04 -0700609 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
Florin Coras697faea2018-06-27 17:10:49 -0700610 if (PREDICT_FALSE (rv))
611 {
612 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
613 getpid (), rv, vppcom_retval_str (rv));
614 return rv;
615 }
Dave Wallace543852a2017-08-03 02:11:34 -0400616
Florin Coras697faea2018-06-27 17:10:49 -0700617 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400618}
619
620static int
Florin Corasab2f6db2018-08-31 14:31:41 -0700621vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400622{
Florin Coras134a9962018-08-28 11:32:04 -0700623 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -0700624 vcl_session_t *session = 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500625 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400626
Florin Corasab2f6db2018-08-31 14:31:41 -0700627 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700628 if (!session)
629 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500630
631 vpp_handle = session->vpp_handle;
Florin Coras134a9962018-08-28 11:32:04 -0700632 vcl_session_table_del_listener (wrk, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500633 session->vpp_handle = ~0;
Florin Coras7e12d942018-06-27 14:32:43 -0700634 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500635
Florin Coras0d427d82018-06-27 03:24:07 -0700636 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
Florin Corasab2f6db2018-08-31 14:31:41 -0700637 " 0x%x (%s)", getpid (), vpp_handle, session_handle, STATE_DISCONNECT,
Florin Coras0d427d82018-06-27 03:24:07 -0700638 vppcom_session_state_str (STATE_DISCONNECT));
639 vcl_evt (VCL_EVT_UNBIND, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500640 vppcom_send_unbind_sock (vpp_handle);
641
Florin Coras070453d2018-08-24 17:04:27 -0700642 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400643}
644
Florin Coras697faea2018-06-27 17:10:49 -0700645static int
Florin Corasab2f6db2018-08-31 14:31:41 -0700646vppcom_session_disconnect (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400647{
Florin Coras134a9962018-08-28 11:32:04 -0700648 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -0700649 svm_msg_q_t *vpp_evt_q;
Florin Coras7e12d942018-06-27 14:32:43 -0700650 vcl_session_t *session;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500651 session_state_t state;
Florin Coras99368312018-08-02 10:45:44 -0700652 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400653
Florin Corasab2f6db2018-08-31 14:31:41 -0700654 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras21795132018-09-09 09:40:51 -0700655 if (!session)
656 return VPPCOM_EBADFD;
657
Dave Wallace4878cbe2017-11-21 03:45:09 -0500658 vpp_handle = session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -0700659 state = session->session_state;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500660
Florin Coras0d427d82018-06-27 03:24:07 -0700661 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
Florin Corasab2f6db2018-08-31 14:31:41 -0700662 vpp_handle, session_handle, state, vppcom_session_state_str (state));
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400663
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800664 if (PREDICT_FALSE (state & STATE_LISTEN))
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400665 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500666 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500667 "Cannot disconnect a listen socket!",
Florin Corasab2f6db2018-08-31 14:31:41 -0700668 getpid (), vpp_handle, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700669 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500670 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400671
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800672 if (state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500673 {
Florin Coras134a9962018-08-28 11:32:04 -0700674 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
Florin Coras99368312018-08-02 10:45:44 -0700675 vcl_send_session_disconnected_reply (vpp_evt_q, vcm->my_client_index,
676 vpp_handle, 0);
Florin Coras0d427d82018-06-27 03:24:07 -0700677 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
Florin Corasab2f6db2018-08-31 14:31:41 -0700678 "REPLY...", getpid (), vpp_handle, session_handle);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400679 }
680 else
Dave Wallace227867f2017-11-13 21:21:53 -0500681 {
Florin Coras0d427d82018-06-27 03:24:07 -0700682 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
Florin Corasab2f6db2018-08-31 14:31:41 -0700683 getpid (), vpp_handle, session_handle);
684 vppcom_send_disconnect_session (vpp_handle);
Dave Wallace227867f2017-11-13 21:21:53 -0500685 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400686
Florin Coras070453d2018-08-24 17:04:27 -0700687 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400688}
689
Dave Wallace543852a2017-08-03 02:11:34 -0400690/*
691 * VPPCOM Public API functions
692 */
693int
694vppcom_app_create (char *app_name)
695{
Dave Wallace543852a2017-08-03 02:11:34 -0400696 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -0400697 int rv;
698
Florin Coras134a9962018-08-28 11:32:04 -0700699 if (!vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -0400700 {
Florin Coras134a9962018-08-28 11:32:04 -0700701 vcm->is_init = 1;
Dave Barach6a5adc32018-07-04 10:56:23 -0400702 vppcom_cfg (&vcm->cfg);
Florin Coras99368312018-08-02 10:45:44 -0700703 vcl_cfg = &vcm->cfg;
704
Florin Coras134a9962018-08-28 11:32:04 -0700705 vcm->main_cpu = pthread_self ();
Dave Wallace543852a2017-08-03 02:11:34 -0400706 vppcom_init_error_string_table ();
Florin Corasa332c462018-01-31 06:52:17 -0800707 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
708 20 /* timeout in secs */ );
Florin Coras134a9962018-08-28 11:32:04 -0700709 pool_init_fixed (vcm->workers, vcl_cfg->max_workers);
Florin Corasde9f08b2018-09-07 14:32:58 -0700710 clib_spinlock_init (&vcm->workers_lock);
Florin Coras134a9962018-08-28 11:32:04 -0700711 vcl_worker_alloc_and_init ();
Dave Wallace543852a2017-08-03 02:11:34 -0400712 }
713
714 if (vcm->my_client_index == ~0)
715 {
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -0800716 /* API hookup and connect to VPP */
Dave Wallace048b1d62018-01-03 22:24:41 -0500717 vppcom_api_hookup ();
Florin Coras0d427d82018-06-27 03:24:07 -0700718 vcl_elog_init (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -0400719 vcm->app_state = STATE_APP_START;
720 rv = vppcom_connect_to_vpp (app_name);
721 if (rv)
722 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500723 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
Dave Wallace2e005bb2017-11-07 01:21:39 -0500724 getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400725 return rv;
726 }
727
Florin Coras0d427d82018-06-27 03:24:07 -0700728 VDBG (0, "VCL<%d>: sending session enable", getpid ());
Dave Wallace048b1d62018-01-03 22:24:41 -0500729 rv = vppcom_app_session_enable ();
Dave Wallace543852a2017-08-03 02:11:34 -0400730 if (rv)
731 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500732 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
733 "failed!", getpid ());
Dave Wallace543852a2017-08-03 02:11:34 -0400734 return rv;
735 }
Dave Wallace543852a2017-08-03 02:11:34 -0400736
Florin Coras0d427d82018-06-27 03:24:07 -0700737 VDBG (0, "VCL<%d>: sending app attach", getpid ());
Dave Wallace048b1d62018-01-03 22:24:41 -0500738 rv = vppcom_app_attach ();
739 if (rv)
740 {
741 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
742 getpid ());
743 return rv;
744 }
745
Florin Coras0d427d82018-06-27 03:24:07 -0700746 VDBG (0, "VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
747 getpid (), app_name, vcm->my_client_index, vcm->my_client_index);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400748 }
Dave Wallace543852a2017-08-03 02:11:34 -0400749
750 return VPPCOM_OK;
751}
752
753void
754vppcom_app_destroy (void)
755{
Dave Wallace543852a2017-08-03 02:11:34 -0400756 int rv;
Dave Wallacede910062018-03-20 09:22:13 -0400757 f64 orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -0400758
759 if (vcm->my_client_index == ~0)
760 return;
761
Florin Coras0d427d82018-06-27 03:24:07 -0700762 VDBG (0, "VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
763 getpid (), vcm->my_client_index, vcm->my_client_index);
764 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800765
Florin Coras697faea2018-06-27 17:10:49 -0700766 vppcom_app_send_detach ();
Dave Wallacede910062018-03-20 09:22:13 -0400767 orig_app_timeout = vcm->cfg.app_timeout;
768 vcm->cfg.app_timeout = 2.0;
Florin Coras134a9962018-08-28 11:32:04 -0700769 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
Dave Wallacede910062018-03-20 09:22:13 -0400770 vcm->cfg.app_timeout = orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -0400771 if (PREDICT_FALSE (rv))
Florin Coras0d427d82018-06-27 03:24:07 -0700772 VDBG (0, "VCL<%d>: application detach timed out! returning %d (%s)",
773 getpid (), rv, vppcom_retval_str (rv));
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800774
Florin Coras0d427d82018-06-27 03:24:07 -0700775 vcl_elog_stop (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -0400776 vl_client_disconnect_from_vlib ();
777 vcm->my_client_index = ~0;
778 vcm->app_state = STATE_APP_START;
779}
780
781int
Dave Wallacec04cbf12018-02-07 18:14:02 -0500782vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -0400783{
Florin Coras134a9962018-08-28 11:32:04 -0700784 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -0700785 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -0400786
Florin Coras134a9962018-08-28 11:32:04 -0700787 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -0400788
Florin Coras7e12d942018-06-27 14:32:43 -0700789 session->session_type = proto;
790 session->session_state = STATE_START;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500791 session->vpp_handle = ~0;
Florin Coras460dce62018-07-27 05:45:06 -0700792 session->is_dgram = proto == VPPCOM_PROTO_UDP;
Dave Wallace543852a2017-08-03 02:11:34 -0400793
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800794 if (is_nonblocking)
795 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -0400796
Florin Coras7e12d942018-06-27 14:32:43 -0700797 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
798 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800799
Florin Coras134a9962018-08-28 11:32:04 -0700800 VDBG (0, "VCL<%d>: sid %u", getpid (), session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800801
Florin Coras134a9962018-08-28 11:32:04 -0700802 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -0400803}
804
805int
Florin Coras134a9962018-08-28 11:32:04 -0700806vppcom_session_close (uint32_t session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400807{
Florin Coras134a9962018-08-28 11:32:04 -0700808 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -0700809 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -0700810 u8 is_vep, is_vep_session;
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400811 session_state_t state;
Florin Coras134a9962018-08-28 11:32:04 -0700812 u32 next_sh, vep_sh;
813 int rv = VPPCOM_OK;
814 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400815
Florin Coras134a9962018-08-28 11:32:04 -0700816 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700817 if (!session)
818 return VPPCOM_EBADFD;
819
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400820 is_vep = session->is_vep;
821 is_vep_session = session->is_vep_session;
Florin Coras134a9962018-08-28 11:32:04 -0700822 next_sh = session->vep.next_sh;
823 vep_sh = session->vep.vep_sh;
Florin Coras7e12d942018-06-27 14:32:43 -0700824 state = session->session_state;
Dave Wallaceee45d412017-11-24 21:44:06 -0500825 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400826
827 if (VPPCOM_DEBUG > 0)
Dave Wallaceee45d412017-11-24 21:44:06 -0500828 {
829 if (is_vep)
Dave Wallace048b1d62018-01-03 22:24:41 -0500830 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
831 "closing epoll session...",
Florin Coras134a9962018-08-28 11:32:04 -0700832 getpid (), session_handle, session_handle);
Dave Wallaceee45d412017-11-24 21:44:06 -0500833 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500834 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
835 "closing session...",
Florin Coras134a9962018-08-28 11:32:04 -0700836 getpid (), vpp_handle, session_handle);
Dave Wallaceee45d412017-11-24 21:44:06 -0500837 }
Dave Wallace543852a2017-08-03 02:11:34 -0400838
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400839 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -0400840 {
Florin Coras134a9962018-08-28 11:32:04 -0700841 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -0400842 {
Florin Coras134a9962018-08-28 11:32:04 -0700843 rv = vppcom_epoll_ctl (session_handle, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -0700844 if (PREDICT_FALSE (rv < 0))
845 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
846 "vep_idx %u failed! rv %d (%s)",
Florin Coras134a9962018-08-28 11:32:04 -0700847 getpid (), vpp_handle, next_sh, vep_sh,
Florin Coras0d427d82018-06-27 03:24:07 -0700848 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400849
Florin Coras134a9962018-08-28 11:32:04 -0700850 next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400851 }
852 }
853 else
854 {
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400855 if (is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400856 {
Florin Coras134a9962018-08-28 11:32:04 -0700857 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, session_handle, 0);
Florin Coras0d427d82018-06-27 03:24:07 -0700858 if (rv < 0)
859 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL "
860 "vep_idx %u failed! rv %d (%s)",
Florin Coras134a9962018-08-28 11:32:04 -0700861 getpid (), vpp_handle, session_handle,
862 vep_sh, rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400863 }
864
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800865 if (state & STATE_LISTEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400866 {
Florin Coras134a9962018-08-28 11:32:04 -0700867 rv = vppcom_session_unbind (session_handle);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800868 if (PREDICT_FALSE (rv < 0))
Florin Coras0d427d82018-06-27 03:24:07 -0700869 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: listener unbind "
870 "failed! rv %d (%s)",
Florin Coras134a9962018-08-28 11:32:04 -0700871 getpid (), vpp_handle, session_handle,
Florin Coras0d427d82018-06-27 03:24:07 -0700872 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400873 }
Florin Coras070453d2018-08-24 17:04:27 -0700874 else if (state & STATE_OPEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400875 {
Florin Coras134a9962018-08-28 11:32:04 -0700876 rv = vppcom_session_disconnect (session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500877 if (PREDICT_FALSE (rv < 0))
Dave Wallace048b1d62018-01-03 22:24:41 -0500878 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500879 "session disconnect failed! rv %d (%s)",
Florin Coras134a9962018-08-28 11:32:04 -0700880 getpid (), vpp_handle, session_handle,
Dave Wallaceee45d412017-11-24 21:44:06 -0500881 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400882 }
Dave Wallace19481612017-09-15 18:47:44 -0400883 }
Dave Wallace4878cbe2017-11-21 03:45:09 -0500884
Florin Coras99368312018-08-02 10:45:44 -0700885 if (vcl_session_is_ct (session))
886 {
887 vcl_cut_through_registration_t *ctr;
888 uword mq_addr;
889
890 mq_addr = pointer_to_uword (session->our_evt_q);
Florin Coras134a9962018-08-28 11:32:04 -0700891 ctr = vcl_ct_registration_lock_and_lookup (wrk, mq_addr);
Florin Coras99368312018-08-02 10:45:44 -0700892 ASSERT (ctr);
893 if (ctr->epoll_evt_conn_index != ~0)
Florin Coras134a9962018-08-28 11:32:04 -0700894 vcl_mq_epoll_del_evfd (wrk, ctr->epoll_evt_conn_index);
Florin Coras99368312018-08-02 10:45:44 -0700895 VDBG (0, "Removing ct registration %u",
Florin Coras134a9962018-08-28 11:32:04 -0700896 vcl_ct_registration_index (wrk, ctr));
897 vcl_ct_registration_del (wrk, ctr);
898 vcl_ct_registration_lookup_del (wrk, mq_addr);
899 vcl_ct_registration_unlock (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700900 }
Florin Coras54693d22018-07-17 10:46:29 -0700901
Dave Wallaceee45d412017-11-24 21:44:06 -0500902 if (vpp_handle != ~0)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500903 {
Florin Coras134a9962018-08-28 11:32:04 -0700904 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500905 }
Florin Coras134a9962018-08-28 11:32:04 -0700906 vcl_session_free (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500907
908 if (VPPCOM_DEBUG > 0)
Dave Wallaceee45d412017-11-24 21:44:06 -0500909 {
910 if (is_vep)
Dave Wallace048b1d62018-01-03 22:24:41 -0500911 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
Florin Coras134a9962018-08-28 11:32:04 -0700912 getpid (), session_handle, session_handle);
Dave Wallaceee45d412017-11-24 21:44:06 -0500913 else
Dave Wallace048b1d62018-01-03 22:24:41 -0500914 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
Florin Coras134a9962018-08-28 11:32:04 -0700915 getpid (), vpp_handle, session_handle);
Dave Wallaceee45d412017-11-24 21:44:06 -0500916 }
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800917
Florin Coras0d427d82018-06-27 03:24:07 -0700918 vcl_evt (VCL_EVT_CLOSE, session, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800919
Dave Wallace543852a2017-08-03 02:11:34 -0400920 return rv;
921}
922
923int
Florin Coras134a9962018-08-28 11:32:04 -0700924vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -0400925{
Florin Coras134a9962018-08-28 11:32:04 -0700926 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -0700927 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -0400928
929 if (!ep || !ep->ip)
930 return VPPCOM_EINVAL;
931
Florin Coras134a9962018-08-28 11:32:04 -0700932 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700933 if (!session)
934 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -0400935
Dave Wallace9c4b5b22017-10-18 21:15:48 -0400936 if (session->is_vep)
937 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500938 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
Florin Coras134a9962018-08-28 11:32:04 -0700939 "bind to an epoll session!", getpid (), session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700940 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -0400941 }
942
Florin Coras7e12d942018-06-27 14:32:43 -0700943 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -0700944 if (ep->is_ip4)
945 clib_memcpy (&session->transport.lcl_ip.ip4, ep->ip,
946 sizeof (ip4_address_t));
947 else
948 clib_memcpy (&session->transport.lcl_ip.ip6, ep->ip,
949 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -0700950 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -0700951
Florin Coras0d427d82018-06-27 03:24:07 -0700952 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
Florin Coras134a9962018-08-28 11:32:04 -0700953 "proto %s", getpid (), session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -0700954 session->transport.is_ip4 ? "IPv4" : "IPv6",
955 format_ip46_address, &session->transport.lcl_ip,
956 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
957 clib_net_to_host_u16 (session->transport.lcl_port),
958 session->session_type ? "UDP" : "TCP");
Florin Coras0d427d82018-06-27 03:24:07 -0700959 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -0700960
961 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -0700962 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -0700963
Florin Coras070453d2018-08-24 17:04:27 -0700964 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400965}
966
967int
Florin Coras134a9962018-08-28 11:32:04 -0700968vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -0400969{
Florin Coras134a9962018-08-28 11:32:04 -0700970 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -0700971 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -0500972 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -0700973 int rv;
974
Florin Coras134a9962018-08-28 11:32:04 -0700975 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras070453d2018-08-24 17:04:27 -0700976 if (!listen_session)
977 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -0400978
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -0800979 if (q_len == 0 || q_len == ~0)
980 q_len = vcm->cfg.listen_queue_size;
981
Dave Wallace9c4b5b22017-10-18 21:15:48 -0400982 if (listen_session->is_vep)
983 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500984 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
Florin Coras134a9962018-08-28 11:32:04 -0700985 "epoll session!", getpid (), listen_sh);
Florin Coras070453d2018-08-24 17:04:27 -0700986 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -0400987 }
988
Dave Wallaceee45d412017-11-24 21:44:06 -0500989 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -0700990 if (listen_session->session_state & STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -0400991 {
Florin Coras0d427d82018-06-27 03:24:07 -0700992 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
Florin Coras134a9962018-08-28 11:32:04 -0700993 getpid (), listen_vpp_handle, listen_sh);
Florin Coras070453d2018-08-24 17:04:27 -0700994 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -0400995 }
996
Florin Coras0d427d82018-06-27 03:24:07 -0700997 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
Florin Coras134a9962018-08-28 11:32:04 -0700998 "request...", getpid (), listen_vpp_handle, listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -0400999
Florin Coras070453d2018-08-24 17:04:27 -07001000 /*
1001 * Send listen request to vpp and wait for reply
1002 */
Florin Coras134a9962018-08-28 11:32:04 -07001003 vppcom_send_bind_sock (listen_session);
1004 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1005 STATE_LISTEN,
1006 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001007
Florin Coras070453d2018-08-24 17:04:27 -07001008 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001009 {
Florin Coras134a9962018-08-28 11:32:04 -07001010 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras0d427d82018-06-27 03:24:07 -07001011 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
1012 "returning %d (%s)", getpid (), listen_session->vpp_handle,
Florin Coras134a9962018-08-28 11:32:04 -07001013 listen_sh, rv, vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001014 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001015 }
1016
Florin Coras070453d2018-08-24 17:04:27 -07001017 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001018}
1019
Florin Coras134a9962018-08-28 11:32:04 -07001020static int
1021validate_args_session_accept_ (vcl_worker_t * wrk,
1022 vcl_session_t * listen_session)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001023{
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001024 /* Input validation - expects spinlock on sessions_lockp */
1025 if (listen_session->is_vep)
1026 {
1027 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
Florin Coras134a9962018-08-28 11:32:04 -07001028 "epoll session!", getpid (),
1029 listen_session->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001030 return VPPCOM_EBADFD;
1031 }
1032
Florin Coras7e12d942018-06-27 14:32:43 -07001033 if (listen_session->session_state != STATE_LISTEN)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001034 {
1035 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1036 "not in listen state! state 0x%x (%s)", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07001037 listen_session->vpp_handle, listen_session->session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07001038 listen_session->session_state,
1039 vppcom_session_state_str (listen_session->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001040 return VPPCOM_EBADFD;
1041 }
1042 return VPPCOM_OK;
1043}
1044
1045int
Florin Coras134a9962018-08-28 11:32:04 -07001046vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001047 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001048{
Florin Coras134a9962018-08-28 11:32:04 -07001049 u32 client_session_index = ~0, listen_session_index;
1050 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001051 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001052 vcl_session_t *listen_session = 0;
1053 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001054 svm_msg_q_t *vpp_evt_q;
1055 vcl_session_msg_t *evt;
Dave Wallaceee45d412017-11-24 21:44:06 -05001056 u64 listen_vpp_handle;
Florin Coras54693d22018-07-17 10:46:29 -07001057 svm_msg_q_msg_t msg;
1058 session_event_t *e;
1059 u8 is_nonblocking;
1060 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001061
Florin Coras134a9962018-08-28 11:32:04 -07001062 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001063 if (!listen_session)
1064 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001065
Florin Coras134a9962018-08-28 11:32:04 -07001066 listen_session_index = listen_session->session_index;
1067 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001068 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001069
Florin Coras54693d22018-07-17 10:46:29 -07001070 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001071 {
Florin Coras54693d22018-07-17 10:46:29 -07001072 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1073 accepted_msg = evt->accepted_msg;
1074 goto handle;
1075 }
1076
1077 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1078 VCL_SESS_ATTR_NONBLOCK);
Florin Coras134a9962018-08-28 11:32:04 -07001079 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
Florin Coras54693d22018-07-17 10:46:29 -07001080 return VPPCOM_EAGAIN;
1081
1082 while (1)
1083 {
Florin Coras134a9962018-08-28 11:32:04 -07001084 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -07001085 return VPPCOM_EAGAIN;
1086
Florin Coras134a9962018-08-28 11:32:04 -07001087 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001088 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001089 {
Florin Coras54693d22018-07-17 10:46:29 -07001090 clib_warning ("discarded event: %u", e->event_type);
Florin Coras134a9962018-08-28 11:32:04 -07001091 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001092 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001093 }
Florin Coras54693d22018-07-17 10:46:29 -07001094 clib_memcpy (&accepted_msg, e->data, sizeof (accepted_msg));
Florin Coras134a9962018-08-28 11:32:04 -07001095 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001096 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001097 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001098
Florin Coras54693d22018-07-17 10:46:29 -07001099handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001100
Florin Coras134a9962018-08-28 11:32:04 -07001101 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg);
1102 listen_session = vcl_session_get (wrk, listen_session_index);
1103 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001104
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001105 if (flags & O_NONBLOCK)
1106 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001107
Florin Coras54693d22018-07-17 10:46:29 -07001108 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras0d427d82018-06-27 03:24:07 -07001109 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
1110 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
Florin Coras134a9962018-08-28 11:32:04 -07001111 getpid (), listen_vpp_handle, listen_session_handle,
Florin Coras0d427d82018-06-27 03:24:07 -07001112 client_session->vpp_handle, client_session_index,
1113 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1114 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001115
Dave Wallace048b1d62018-01-03 22:24:41 -05001116 if (ep)
1117 {
Florin Coras7e12d942018-06-27 14:32:43 -07001118 ep->is_ip4 = client_session->transport.is_ip4;
1119 ep->port = client_session->transport.rmt_port;
1120 if (client_session->transport.is_ip4)
1121 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip4,
Dave Wallace048b1d62018-01-03 22:24:41 -05001122 sizeof (ip4_address_t));
1123 else
Florin Coras7e12d942018-06-27 14:32:43 -07001124 clib_memcpy (ep->ip, &client_session->transport.rmt_ip.ip6,
Dave Wallace048b1d62018-01-03 22:24:41 -05001125 sizeof (ip6_address_t));
1126 }
Dave Wallace60caa062017-11-10 17:07:13 -05001127
Florin Coras54693d22018-07-17 10:46:29 -07001128 if (accepted_msg.server_event_queue_address)
1129 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1130 svm_msg_q_t *);
1131 else
1132 vpp_evt_q = client_session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -07001133
Florin Coras54693d22018-07-17 10:46:29 -07001134 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1135 client_session->vpp_handle, 0);
Dave Wallace60caa062017-11-10 17:07:13 -05001136
Florin Coras54693d22018-07-17 10:46:29 -07001137 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx, "
1138 "sid %u connection from peer %s address %U port %u to local %s "
1139 "address %U port %u", getpid (), listen_vpp_handle,
Florin Coras134a9962018-08-28 11:32:04 -07001140 listen_session_handle, client_session->vpp_handle,
Florin Coras0d427d82018-06-27 03:24:07 -07001141 client_session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07001142 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1143 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001144 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001145 clib_net_to_host_u16 (client_session->transport.rmt_port),
1146 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1147 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001148 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001149 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001150 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1151 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001152
Florin Corasab2f6db2018-08-31 14:31:41 -07001153 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001154}
1155
1156int
Florin Coras134a9962018-08-28 11:32:04 -07001157vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001158{
Florin Coras134a9962018-08-28 11:32:04 -07001159 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001160 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001161 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001162 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001163
Florin Coras134a9962018-08-28 11:32:04 -07001164 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001165 if (!session)
1166 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001167 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001168
1169 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001170 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001171 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
Florin Coras134a9962018-08-28 11:32:04 -07001172 "connect on an epoll session!", getpid (),
1173 session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001174 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001175 }
1176
Florin Coras7e12d942018-06-27 14:32:43 -07001177 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
Dave Wallace543852a2017-08-03 02:11:34 -04001178 {
Florin Coras0d427d82018-06-27 03:24:07 -07001179 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
1180 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras134a9962018-08-28 11:32:04 -07001181 getpid (), session->vpp_handle, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001182 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001183 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001184 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001185 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001186 clib_net_to_host_u16 (session->transport.rmt_port),
1187 session->session_type ? "UDP" : "TCP", session->session_state,
1188 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001189 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001190 }
1191
Florin Coras7e12d942018-06-27 14:32:43 -07001192 session->transport.is_ip4 = server_ep->is_ip4;
1193 if (session->transport.is_ip4)
1194 clib_memcpy (&session->transport.rmt_ip.ip4, server_ep->ip,
Dave Wallaced239f8d2018-06-19 13:37:30 -04001195 sizeof (ip4_address_t));
1196 else
Florin Coras7e12d942018-06-27 14:32:43 -07001197 clib_memcpy (&session->transport.rmt_ip.ip6, server_ep->ip,
Dave Wallaced239f8d2018-06-19 13:37:30 -04001198 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001199 session->transport.rmt_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04001200
Florin Coras0d427d82018-06-27 03:24:07 -07001201 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
1202 "port %d proto %s",
Florin Coras134a9962018-08-28 11:32:04 -07001203 getpid (), session->vpp_handle, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001204 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001205 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001206 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001207 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001208 clib_net_to_host_u16 (session->transport.rmt_port),
1209 session->session_type ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -04001210
Florin Coras070453d2018-08-24 17:04:27 -07001211 /*
1212 * Send connect request and wait for reply from vpp
1213 */
Florin Coras134a9962018-08-28 11:32:04 -07001214 vppcom_send_connect_sock (session);
Florin Coras070453d2018-08-24 17:04:27 -07001215 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1216 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001217
Florin Coras134a9962018-08-28 11:32:04 -07001218 session = vcl_session_get (wrk, session_index);
Dave Wallace7876d392017-10-19 03:53:57 -04001219
Florin Coras070453d2018-08-24 17:04:27 -07001220 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001221 {
Dave Wallaceee45d412017-11-24 21:44:06 -05001222 if (VPPCOM_DEBUG > 0)
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001223 {
1224 if (session)
Florin Coras0d427d82018-06-27 03:24:07 -07001225 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
Florin Coras134a9962018-08-28 11:32:04 -07001226 "failed! returning %d (%s)", getpid (),
1227 session->vpp_handle, session_handle, rv,
1228 vppcom_retval_str (rv));
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001229 else
1230 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1231 "returning %d (%s)", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07001232 session_handle, rv, vppcom_retval_str (rv));
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001233 }
Dave Wallaceee45d412017-11-24 21:44:06 -05001234 }
Florin Coras0d427d82018-06-27 03:24:07 -07001235 else
1236 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
Florin Coras134a9962018-08-28 11:32:04 -07001237 getpid (), session->vpp_handle, session_handle);
Dave Wallaceee45d412017-11-24 21:44:06 -05001238
Dave Wallace4878cbe2017-11-21 03:45:09 -05001239 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001240}
1241
Florin Coras54693d22018-07-17 10:46:29 -07001242static u8
1243vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1244{
1245 if (!is_ct)
1246 return (e->event_type == FIFO_EVENT_APP_RX
1247 && e->fifo->client_session_index == sid);
1248 else
1249 return (e->event_type == SESSION_IO_EVT_CT_TX);
1250}
1251
Florin Coras460dce62018-07-27 05:45:06 -07001252static inline u8
1253vcl_session_is_readable (vcl_session_t * s)
1254{
1255 return ((s->session_state & STATE_OPEN)
1256 || (s->session_state == STATE_LISTEN
1257 && s->session_type == VPPCOM_PROTO_UDP));
1258}
1259
Steven58f464e2017-10-25 12:33:12 -07001260static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001261vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001262 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001263{
Florin Coras134a9962018-08-28 11:32:04 -07001264 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001265 int n_read = 0, rv, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001266 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001267 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001268 svm_msg_q_msg_t msg;
1269 session_event_t *e;
1270 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001271 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001272
Florin Coras070453d2018-08-24 17:04:27 -07001273 if (PREDICT_FALSE (!buf))
1274 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001275
Florin Coras134a9962018-08-28 11:32:04 -07001276 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras2cba8532018-09-11 16:33:36 -07001277 if (PREDICT_FALSE (!s || s->is_vep))
Florin Coras070453d2018-08-24 17:04:27 -07001278 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001279
Florin Coras460dce62018-07-27 05:45:06 -07001280 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001281 {
Florin Coras460dce62018-07-27 05:45:06 -07001282 session_state_t state = s->session_state;
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001283 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001284
Florin Coras0d427d82018-06-27 03:24:07 -07001285 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
1286 "state 0x%x (%s), returning %d (%s)",
Florin Coras134a9962018-08-28 11:32:04 -07001287 getpid (), s->vpp_handle, session_handle, state,
Florin Coras0d427d82018-06-27 03:24:07 -07001288 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001289 return rv;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001290 }
1291
Florin Coras2cba8532018-09-11 16:33:36 -07001292 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001293 is_ct = vcl_session_is_ct (s);
1294 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
Florin Coras2cba8532018-09-11 16:33:36 -07001295 rx_fifo = s->rx_fifo;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001296
Florin Coras54693d22018-07-17 10:46:29 -07001297 if (svm_fifo_is_empty (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001298 {
Florin Coras54693d22018-07-17 10:46:29 -07001299 if (is_nonblocking)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001300 {
Florin Coras41c9e042018-09-11 00:10:41 -07001301 svm_fifo_unset_event (rx_fifo);
Florin Coras070453d2018-08-24 17:04:27 -07001302 return VPPCOM_OK;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001303 }
Florin Coras41c9e042018-09-11 00:10:41 -07001304 while (svm_fifo_is_empty (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001305 {
Florin Coras41c9e042018-09-11 00:10:41 -07001306 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001307 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001308 if (svm_msg_q_is_empty (mq))
1309 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001310
Florin Coras54693d22018-07-17 10:46:29 -07001311 svm_msg_q_sub_w_lock (mq, &msg);
1312 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001313 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001314 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras54693d22018-07-17 10:46:29 -07001315 {
Florin Coras86f04502018-09-12 16:08:01 -07001316 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001317 svm_msg_q_free_msg (mq, &msg);
1318 continue;
1319 }
Florin Coras54693d22018-07-17 10:46:29 -07001320 svm_msg_q_free_msg (mq, &msg);
Florin Coras41c9e042018-09-11 00:10:41 -07001321
Florin Coras60f1fc12018-08-16 14:57:31 -07001322 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1323 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07001324 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001325 }
Florin Coras54693d22018-07-17 10:46:29 -07001326
Florin Coras460dce62018-07-27 05:45:06 -07001327 if (s->is_dgram)
Florin Coras99368312018-08-02 10:45:44 -07001328 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001329 else
Florin Coras99368312018-08-02 10:45:44 -07001330 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
Florin Coras54693d22018-07-17 10:46:29 -07001331
Florin Coras41c9e042018-09-11 00:10:41 -07001332 if (svm_fifo_is_empty (rx_fifo))
1333 svm_fifo_unset_event (rx_fifo);
1334
1335 if (is_ct && n_read + svm_fifo_max_dequeue (rx_fifo) == rx_fifo->nitems)
Florin Coras99368312018-08-02 10:45:44 -07001336 {
1337 /* If the peer is not polling send notification */
1338 if (!svm_fifo_has_event (s->rx_fifo))
1339 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1340 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1341 }
Florin Coras54693d22018-07-17 10:46:29 -07001342
Florin Coras2cba8532018-09-11 16:33:36 -07001343 VDBG (2, "VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes from (%p)",
1344 getpid (), s->vpp_handle, session_handle, n_read, rx_fifo);
1345
Florin Coras54693d22018-07-17 10:46:29 -07001346 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001347}
1348
Steven58f464e2017-10-25 12:33:12 -07001349int
Florin Coras134a9962018-08-28 11:32:04 -07001350vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001351{
Florin Coras134a9962018-08-28 11:32:04 -07001352 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001353}
1354
1355static int
Florin Coras134a9962018-08-28 11:32:04 -07001356vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001357{
Florin Coras134a9962018-08-28 11:32:04 -07001358 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001359}
1360
Florin Coras2cba8532018-09-11 16:33:36 -07001361int
1362vppcom_session_read_segments (uint32_t session_handle,
1363 vppcom_data_segments_t ds)
1364{
1365 vcl_worker_t *wrk = vcl_worker_get_current ();
1366 int n_read = 0, rv, is_nonblocking;
1367 vcl_session_t *s = 0;
1368 svm_fifo_t *rx_fifo;
1369 svm_msg_q_msg_t msg;
1370 session_event_t *e;
1371 svm_msg_q_t *mq;
1372 u8 is_ct;
1373
1374 s = vcl_session_get_w_handle (wrk, session_handle);
1375 if (PREDICT_FALSE (!s || s->is_vep))
1376 return VPPCOM_EBADFD;
1377
1378 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1379 {
1380 session_state_t state = s->session_state;
1381 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1382 return rv;
1383 }
1384
1385 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1386 is_ct = vcl_session_is_ct (s);
1387 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1388 rx_fifo = s->rx_fifo;
1389
1390 if (svm_fifo_is_empty (rx_fifo))
1391 {
1392 if (is_nonblocking)
1393 {
1394 svm_fifo_unset_event (rx_fifo);
1395 return VPPCOM_OK;
1396 }
1397 while (svm_fifo_is_empty (rx_fifo))
1398 {
1399 svm_fifo_unset_event (rx_fifo);
1400 svm_msg_q_lock (mq);
1401 if (svm_msg_q_is_empty (mq))
1402 svm_msg_q_wait (mq);
1403
1404 svm_msg_q_sub_w_lock (mq, &msg);
1405 e = svm_msg_q_msg_data (mq, &msg);
1406 svm_msg_q_unlock (mq);
1407 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1408 {
Florin Coras86f04502018-09-12 16:08:01 -07001409 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001410 svm_msg_q_free_msg (mq, &msg);
1411 continue;
1412 }
1413 svm_msg_q_free_msg (mq, &msg);
1414
1415 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1416 return 0;
1417 }
1418 }
1419
1420 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_segment_t *) ds);
1421 svm_fifo_unset_event (rx_fifo);
1422
1423 if (is_ct && n_read + svm_fifo_max_dequeue (rx_fifo) == rx_fifo->nitems)
1424 {
1425 /* If the peer is not polling send notification */
1426 if (!svm_fifo_has_event (s->rx_fifo))
1427 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1428 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1429 }
1430
1431 return n_read;
1432}
1433
1434void
1435vppcom_session_free_segments (uint32_t session_handle,
1436 vppcom_data_segments_t ds)
1437{
1438 vcl_worker_t *wrk = vcl_worker_get_current ();
1439 vcl_session_t *s;
1440
1441 s = vcl_session_get_w_handle (wrk, session_handle);
1442 if (PREDICT_FALSE (!s || s->is_vep))
1443 return;
1444
1445 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_segment_t *) ds);
1446}
1447
Dave Wallace543852a2017-08-03 02:11:34 -04001448static inline int
Florin Coras54693d22018-07-17 10:46:29 -07001449vppcom_session_read_ready (vcl_session_t * session)
Dave Wallace543852a2017-08-03 02:11:34 -04001450{
Dave Wallace543852a2017-08-03 02:11:34 -04001451 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05001452 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001453 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001454 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
Florin Coras134a9962018-08-28 11:32:04 -07001455 "epoll session!", getpid (), session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -07001456 return VPPCOM_EBADFD;
1457 }
1458
1459 if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
1460 {
1461 session_state_t state = session->session_state;
1462 int rv;
1463
1464 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1465
1466 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
1467 " state 0x%x (%s), returning %d (%s)", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07001468 session->vpp_handle, session->session_index, state,
Florin Coras54693d22018-07-17 10:46:29 -07001469 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1470 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001471 }
Dave Wallace33e002b2017-09-06 01:20:02 -04001472
Florin Coras7e12d942018-06-27 14:32:43 -07001473 if (session->session_state & STATE_LISTEN)
Florin Coras54693d22018-07-17 10:46:29 -07001474 return clib_fifo_elts (session->accept_evts_fifo);
1475
1476 return svm_fifo_max_dequeue (session->rx_fifo);
1477}
1478
Florin Coras2cba8532018-09-11 16:33:36 -07001479int
1480vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1481{
1482 u32 first_copy = clib_min (ds[0].len, max_bytes);
1483 clib_memcpy (buf, ds[0].data, first_copy);
1484 if (first_copy < max_bytes)
1485 {
1486 clib_memcpy (buf + first_copy, ds[1].data,
1487 clib_min (ds[1].len, max_bytes - first_copy));
1488 }
1489 return 0;
1490}
1491
Florin Coras54693d22018-07-17 10:46:29 -07001492static u8
1493vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1494{
1495 if (!is_ct)
1496 return (e->event_type == FIFO_EVENT_APP_TX
1497 && e->fifo->client_session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04001498 else
Florin Coras54693d22018-07-17 10:46:29 -07001499 return (e->event_type == SESSION_IO_EVT_CT_RX);
Dave Wallace543852a2017-08-03 02:11:34 -04001500}
1501
1502int
Florin Coras134a9962018-08-28 11:32:04 -07001503vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
Dave Wallace543852a2017-08-03 02:11:34 -04001504{
Florin Coras134a9962018-08-28 11:32:04 -07001505 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001506 int rv, n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001507 vcl_session_t *s = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001508 svm_fifo_t *tx_fifo = 0;
Florin Coras460dce62018-07-27 05:45:06 -07001509 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07001510 svm_msg_q_msg_t msg;
1511 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001512 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07001513 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001514
Florin Coras070453d2018-08-24 17:04:27 -07001515 if (PREDICT_FALSE (!buf))
1516 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001517
Florin Coras134a9962018-08-28 11:32:04 -07001518 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001519 if (PREDICT_FALSE (!s))
1520 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001521
Florin Coras460dce62018-07-27 05:45:06 -07001522 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001523 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001524 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001525 "cannot write to an epoll session!",
Florin Coras134a9962018-08-28 11:32:04 -07001526 getpid (), s->vpp_handle, session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001527
Florin Coras070453d2018-08-24 17:04:27 -07001528 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001529 }
1530
Florin Coras0e88e852018-09-17 22:09:02 -07001531 if (PREDICT_FALSE (!(s->session_state & STATE_OPEN)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001532 {
Florin Coras460dce62018-07-27 05:45:06 -07001533 session_state_t state = s->session_state;
Florin Coras54693d22018-07-17 10:46:29 -07001534 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Florin Coras0d427d82018-06-27 03:24:07 -07001535 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
Florin Coras0e88e852018-09-17 22:09:02 -07001536 "state 0x%x (%s)", getpid (), s->vpp_handle, session_handle,
Florin Coras0d427d82018-06-27 03:24:07 -07001537 state, vppcom_session_state_str (state));
Florin Coras070453d2018-08-24 17:04:27 -07001538 return rv;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001539 }
1540
Florin Coras0e88e852018-09-17 22:09:02 -07001541 tx_fifo = s->tx_fifo;
1542 is_ct = vcl_session_is_ct (s);
1543 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1544 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
Florin Coras54693d22018-07-17 10:46:29 -07001545 if (svm_fifo_is_full (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04001546 {
Florin Coras54693d22018-07-17 10:46:29 -07001547 if (is_nonblocking)
1548 {
Florin Coras070453d2018-08-24 17:04:27 -07001549 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07001550 }
Florin Coras60f1fc12018-08-16 14:57:31 -07001551 while (svm_fifo_is_full (tx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001552 {
Florin Coras0e88e852018-09-17 22:09:02 -07001553 svm_fifo_set_want_tx_evt (tx_fifo, 1);
Florin Coras99368312018-08-02 10:45:44 -07001554 svm_msg_q_lock (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07001555 svm_msg_q_wait (mq);
1556
Florin Coras54693d22018-07-17 10:46:29 -07001557 svm_msg_q_sub_w_lock (mq, &msg);
1558 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001559 svm_msg_q_unlock (mq);
1560
Florin Coras0e88e852018-09-17 22:09:02 -07001561 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07001562 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001563 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001564 }
Dave Wallace543852a2017-08-03 02:11:34 -04001565 }
Dave Wallace543852a2017-08-03 02:11:34 -04001566
Florin Coras460dce62018-07-27 05:45:06 -07001567 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1568 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
1569 if (s->is_dgram)
1570 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1571 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1572 else
1573 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1574 SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001575
Florin Coras460dce62018-07-27 05:45:06 -07001576 ASSERT (n_write > 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001577
Florin Coras0e88e852018-09-17 22:09:02 -07001578 VDBG (2, "VCL<%d>: vpp handle 0x%llx, sid %u: wrote %d bytes", getpid (),
1579 s->vpp_handle, session_handle, n_write);
1580
Florin Coras54693d22018-07-17 10:46:29 -07001581 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04001582}
1583
Florin Coras99368312018-08-02 10:45:44 -07001584static vcl_session_t *
Florin Coras134a9962018-08-28 11:32:04 -07001585vcl_ct_session_get_from_fifo (vcl_worker_t * wrk, svm_fifo_t * f, u8 type)
Florin Coras99368312018-08-02 10:45:44 -07001586{
1587 vcl_session_t *s;
Florin Coras134a9962018-08-28 11:32:04 -07001588 s = vcl_session_get (wrk, f->client_session_index);
Florin Coras99368312018-08-02 10:45:44 -07001589 if (s)
1590 {
1591 /* rx fifo */
1592 if (type == 0 && s->rx_fifo == f)
1593 return s;
1594 /* tx fifo */
1595 if (type == 1 && s->tx_fifo == f)
1596 return s;
1597 }
Florin Coras134a9962018-08-28 11:32:04 -07001598 s = vcl_session_get (wrk, f->master_session_index);
Florin Coras99368312018-08-02 10:45:44 -07001599 if (s)
1600 {
1601 if (type == 0 && s->rx_fifo == f)
1602 return s;
1603 if (type == 1 && s->tx_fifo == f)
1604 return s;
1605 }
1606 return 0;
1607}
1608
Dave Wallace543852a2017-08-03 02:11:34 -04001609static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001610vppcom_session_write_ready (vcl_session_t * session)
Dave Wallace543852a2017-08-03 02:11:34 -04001611{
Dave Wallace543852a2017-08-03 02:11:34 -04001612 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05001613 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001614 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001615 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001616 "cannot write to an epoll session!",
Florin Coras134a9962018-08-28 11:32:04 -07001617 getpid (), session->vpp_handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -07001618 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001619 }
1620
Florin Coras7e12d942018-06-27 14:32:43 -07001621 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
Dave Wallace33e002b2017-09-06 01:20:02 -04001622 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001623 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001624 "cannot write to a listen session!",
Florin Coras134a9962018-08-28 11:32:04 -07001625 getpid (), session->vpp_handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -07001626 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001627 }
1628
Florin Coras54693d22018-07-17 10:46:29 -07001629 if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001630 {
Florin Coras7e12d942018-06-27 14:32:43 -07001631 session_state_t state = session->session_state;
Florin Coras54693d22018-07-17 10:46:29 -07001632 int rv;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001633
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001634 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace048b1d62018-01-03 22:24:41 -05001635 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001636 "session is not open! state 0x%x (%s), "
Dave Wallaceee45d412017-11-24 21:44:06 -05001637 "returning %d (%s)", getpid (), session->vpp_handle,
Florin Coras134a9962018-08-28 11:32:04 -07001638 session->session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05001639 state, vppcom_session_state_str (state),
1640 rv, vppcom_retval_str (rv));
Florin Coras54693d22018-07-17 10:46:29 -07001641 return rv;
Dave Wallace33e002b2017-09-06 01:20:02 -04001642 }
1643
Florin Coras0d427d82018-06-27 03:24:07 -07001644 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
Florin Coras134a9962018-08-28 11:32:04 -07001645 getpid (), session->vpp_handle, session->session_index,
1646 session->tx_fifo, svm_fifo_max_enqueue (session->tx_fifo));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001647
Florin Coras54693d22018-07-17 10:46:29 -07001648 return svm_fifo_max_enqueue (session->tx_fifo);
1649}
1650
Florin Coras99368312018-08-02 10:45:44 -07001651static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001652vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq)
Florin Coras99368312018-08-02 10:45:44 -07001653{
1654 svm_msg_q_msg_t *msg;
1655 u32 n_msgs;
1656 int i;
1657
1658 n_msgs = svm_msg_q_size (mq);
1659 for (i = 0; i < n_msgs; i++)
1660 {
Florin Coras134a9962018-08-28 11:32:04 -07001661 vec_add2 (wrk->mq_msg_vector, msg, 1);
Florin Coras99368312018-08-02 10:45:44 -07001662 svm_msg_q_sub_w_lock (mq, msg);
1663 }
1664 return n_msgs;
1665}
1666
Florin Coras6d4bb422018-09-04 22:07:27 -07001667#define vcl_fifo_rx_evt_valid_or_break(_fifo) \
1668if (PREDICT_FALSE (svm_fifo_is_empty (_fifo))) \
1669 { \
1670 svm_fifo_unset_event (_fifo); \
1671 if (svm_fifo_is_empty (_fifo)) \
Florin Coras41c9e042018-09-11 00:10:41 -07001672 break; \
Florin Coras6d4bb422018-09-04 22:07:27 -07001673 } \
1674
Florin Coras86f04502018-09-12 16:08:01 -07001675static void
1676vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1677 unsigned long n_bits, unsigned long *read_map,
1678 unsigned long *write_map,
1679 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07001680{
1681 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07001682 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07001683 session_accepted_msg_t *accepted_msg;
1684 vcl_session_msg_t *vcl_msg;
1685 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07001686 u64 handle;
1687 u32 sid;
1688
1689 switch (e->event_type)
1690 {
1691 case FIFO_EVENT_APP_RX:
1692 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1693 sid = e->fifo->client_session_index;
1694 session = vcl_session_get (wrk, sid);
1695 if (!session)
1696 break;
1697 if (sid < n_bits && read_map)
1698 {
1699 clib_bitmap_set_no_check (read_map, sid, 1);
1700 *bits_set += 1;
1701 }
1702 break;
1703 case FIFO_EVENT_APP_TX:
1704 sid = e->fifo->client_session_index;
1705 session = vcl_session_get (wrk, sid);
1706 if (!session)
1707 break;
1708 if (sid < n_bits && write_map)
1709 {
1710 clib_bitmap_set_no_check (write_map, sid, 1);
1711 *bits_set += 1;
1712 }
1713 break;
1714 case SESSION_IO_EVT_CT_TX:
1715 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1716 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
1717 if (!session)
1718 break;
1719 sid = session->session_index;
1720 if (sid < n_bits && read_map)
1721 {
1722 clib_bitmap_set_no_check (read_map, sid, 1);
1723 *bits_set += 1;
1724 }
1725 break;
1726 case SESSION_IO_EVT_CT_RX:
1727 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
1728 if (!session)
1729 break;
1730 sid = session->session_index;
1731 if (sid < n_bits && write_map)
1732 {
1733 clib_bitmap_set_no_check (write_map, sid, 1);
1734 *bits_set += 1;
1735 }
1736 break;
1737 case SESSION_CTRL_EVT_ACCEPTED:
1738 accepted_msg = (session_accepted_msg_t *) e->data;
1739 handle = accepted_msg->listener_handle;
1740 session = vcl_session_table_lookup_listener (wrk, handle);
1741 if (!session)
1742 {
1743 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
1744 "listener handle %llx", getpid (), handle);
1745 break;
1746 }
1747
1748 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
1749 vcl_msg->accepted_msg = *accepted_msg;
1750 sid = session->session_index;
1751 if (sid < n_bits && read_map)
1752 {
1753 clib_bitmap_set_no_check (read_map, sid, 1);
1754 *bits_set += 1;
1755 }
1756 break;
1757 case SESSION_CTRL_EVT_CONNECTED:
1758 connected_msg = (session_connected_msg_t *) e->data;
1759 vcl_session_connected_handler (wrk, connected_msg);
1760 break;
1761 case SESSION_CTRL_EVT_DISCONNECTED:
1762 disconnected_msg = (session_disconnected_msg_t *) e->data;
1763 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
1764 if (sid < n_bits && except_map)
1765 {
1766 clib_bitmap_set_no_check (except_map, sid, 1);
1767 *bits_set += 1;
1768 }
1769 break;
1770 case SESSION_CTRL_EVT_RESET:
1771 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1772 if (sid < n_bits && except_map)
1773 {
1774 clib_bitmap_set_no_check (except_map, sid, 1);
1775 *bits_set += 1;
1776 }
1777 break;
1778 default:
1779 clib_warning ("unhandled: %u", e->event_type);
1780 break;
1781 }
1782}
1783
1784static int
1785vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1786 unsigned long n_bits, unsigned long *read_map,
1787 unsigned long *write_map, unsigned long *except_map,
1788 double time_to_wait, u32 * bits_set)
1789{
Florin Coras99368312018-08-02 10:45:44 -07001790 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07001791 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07001792 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07001793
1794 svm_msg_q_lock (mq);
1795 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001796 {
Florin Coras54693d22018-07-17 10:46:29 -07001797 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001798 {
Florin Coras54693d22018-07-17 10:46:29 -07001799 svm_msg_q_unlock (mq);
1800 return 0;
1801 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001802
Florin Coras54693d22018-07-17 10:46:29 -07001803 if (!time_to_wait)
1804 {
1805 svm_msg_q_unlock (mq);
1806 return 0;
1807 }
1808 else if (time_to_wait < 0)
1809 {
1810 svm_msg_q_wait (mq);
1811 }
1812 else
1813 {
1814 if (svm_msg_q_timedwait (mq, time_to_wait))
1815 {
1816 svm_msg_q_unlock (mq);
1817 return 0;
1818 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001819 }
1820 }
Florin Coras134a9962018-08-28 11:32:04 -07001821 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07001822 svm_msg_q_unlock (mq);
1823
Florin Coras134a9962018-08-28 11:32:04 -07001824 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07001825 {
Florin Coras134a9962018-08-28 11:32:04 -07001826 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07001827 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07001828 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
1829 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07001830 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07001831 }
Florin Coras134a9962018-08-28 11:32:04 -07001832 vec_reset_length (wrk->mq_msg_vector);
Florin Coras54693d22018-07-17 10:46:29 -07001833 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04001834}
1835
Florin Coras99368312018-08-02 10:45:44 -07001836static int
Florin Coras134a9962018-08-28 11:32:04 -07001837vppcom_select_condvar (vcl_worker_t * wrk, unsigned long n_bits,
1838 unsigned long *read_map, unsigned long *write_map,
1839 unsigned long *except_map, double time_to_wait,
1840 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07001841{
1842 double total_wait = 0, wait_slice;
1843 vcl_cut_through_registration_t *cr;
1844
1845 time_to_wait = (time_to_wait == -1) ? 10e9 : time_to_wait;
Florin Coras134a9962018-08-28 11:32:04 -07001846 wait_slice = wrk->cut_through_registrations ? 10e-6 : time_to_wait;
Florin Coras99368312018-08-02 10:45:44 -07001847 do
1848 {
Florin Coras134a9962018-08-28 11:32:04 -07001849 vcl_ct_registration_lock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07001850 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07001851 pool_foreach (cr, wrk->cut_through_registrations, ({
1852 vcl_select_handle_mq (wrk, cr->mq, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07001853 0, bits_set);
1854 }));
1855 /* *INDENT-ON* */
Florin Coras134a9962018-08-28 11:32:04 -07001856 vcl_ct_registration_unlock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07001857
Florin Coras134a9962018-08-28 11:32:04 -07001858 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
1859 write_map, except_map, time_to_wait, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07001860 total_wait += wait_slice;
1861 if (*bits_set)
1862 return *bits_set;
1863 }
1864 while (total_wait < time_to_wait);
1865
1866 return 0;
1867}
1868
1869static int
Florin Coras134a9962018-08-28 11:32:04 -07001870vppcom_select_eventfd (vcl_worker_t * wrk, unsigned long n_bits,
1871 unsigned long *read_map, unsigned long *write_map,
1872 unsigned long *except_map, double time_to_wait,
1873 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07001874{
1875 vcl_mq_evt_conn_t *mqc;
1876 int __clib_unused n_read;
1877 int n_mq_evts, i;
1878 u64 buf;
1879
Florin Coras134a9962018-08-28 11:32:04 -07001880 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
1881 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
1882 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07001883 for (i = 0; i < n_mq_evts; i++)
1884 {
Florin Coras134a9962018-08-28 11:32:04 -07001885 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07001886 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07001887 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07001888 except_map, 0, bits_set);
1889 }
1890
1891 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1892}
1893
Dave Wallace543852a2017-08-03 02:11:34 -04001894int
1895vppcom_select (unsigned long n_bits, unsigned long *read_map,
1896 unsigned long *write_map, unsigned long *except_map,
1897 double time_to_wait)
1898{
Florin Coras54693d22018-07-17 10:46:29 -07001899 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001900 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001901 vcl_session_t *session = 0;
Florin Coras86f04502018-09-12 16:08:01 -07001902 int rv, i;
Dave Wallace543852a2017-08-03 02:11:34 -04001903
1904 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
1905
Dave Wallace7876d392017-10-19 03:53:57 -04001906 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001907 {
Florin Coras134a9962018-08-28 11:32:04 -07001908 clib_bitmap_validate (wrk->rd_bitmap, minbits);
1909 clib_memcpy (wrk->rd_bitmap, read_map,
1910 vec_len (wrk->rd_bitmap) * sizeof (clib_bitmap_t));
1911 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04001912 }
Dave Wallace7876d392017-10-19 03:53:57 -04001913 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001914 {
Florin Coras134a9962018-08-28 11:32:04 -07001915 clib_bitmap_validate (wrk->wr_bitmap, minbits);
1916 clib_memcpy (wrk->wr_bitmap, write_map,
1917 vec_len (wrk->wr_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001918 memset (write_map, 0,
Florin Coras134a9962018-08-28 11:32:04 -07001919 vec_len (wrk->wr_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04001920 }
Dave Wallace7876d392017-10-19 03:53:57 -04001921 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001922 {
Florin Coras134a9962018-08-28 11:32:04 -07001923 clib_bitmap_validate (wrk->ex_bitmap, minbits);
1924 clib_memcpy (wrk->ex_bitmap, except_map,
1925 vec_len (wrk->ex_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001926 memset (except_map, 0,
Florin Coras134a9962018-08-28 11:32:04 -07001927 vec_len (wrk->ex_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04001928 }
1929
Florin Coras54693d22018-07-17 10:46:29 -07001930 if (!n_bits)
1931 return 0;
1932
1933 if (!write_map)
1934 goto check_rd;
1935
1936 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07001937 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
1938 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07001939 {
Florin Coras54693d22018-07-17 10:46:29 -07001940 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1941 getpid (), sid);
1942 return VPPCOM_EBADFD;
1943 }
1944
1945 rv = svm_fifo_is_full (session->tx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07001946 if (!rv)
1947 {
1948 clib_bitmap_set_no_check (write_map, sid, 1);
1949 bits_set++;
1950 }
1951 }));
1952
1953check_rd:
1954 if (!read_map)
1955 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07001956
Florin Coras134a9962018-08-28 11:32:04 -07001957 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
1958 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07001959 {
Florin Coras54693d22018-07-17 10:46:29 -07001960 VDBG (0, "VCL<%d>: session %d specified in write_map is closed.",
1961 getpid (), sid);
1962 return VPPCOM_EBADFD;
1963 }
1964
1965 rv = vppcom_session_read_ready (session);
Florin Coras54693d22018-07-17 10:46:29 -07001966 if (rv)
1967 {
1968 clib_bitmap_set_no_check (read_map, sid, 1);
1969 bits_set++;
1970 }
1971 }));
1972 /* *INDENT-ON* */
1973
1974check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04001975
Florin Coras86f04502018-09-12 16:08:01 -07001976 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
1977 {
1978 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
1979 read_map, write_map, except_map, &bits_set);
1980 }
1981 vec_reset_length (wrk->unhandled_evts_vector);
1982
Florin Coras99368312018-08-02 10:45:44 -07001983 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07001984 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07001985 time_to_wait, &bits_set);
1986 else
Florin Coras134a9962018-08-28 11:32:04 -07001987 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07001988 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07001989
Dave Wallace543852a2017-08-03 02:11:34 -04001990 return (bits_set);
1991}
1992
Dave Wallacef7f809c2017-10-03 01:48:42 -04001993static inline void
Florin Coras134a9962018-08-28 11:32:04 -07001994vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001995{
Florin Coras7e12d942018-06-27 14:32:43 -07001996 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001997 vppcom_epoll_t *vep;
Dave Wallace498b3a52017-11-09 13:00:34 -05001998 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001999
Dave Wallace498b3a52017-11-09 13:00:34 -05002000 if (VPPCOM_DEBUG <= 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002001 return;
2002
2003 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Florin Coras134a9962018-08-28 11:32:04 -07002004 session = vcl_session_get (wrk, vep_idx);
Florin Coras070453d2018-08-24 17:04:27 -07002005 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002006 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002007 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
2008 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002009 goto done;
2010 }
2011 if (PREDICT_FALSE (!session->is_vep))
2012 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002013 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2014 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002015 goto done;
2016 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002017 vep = &session->vep;
Dave Wallace048b1d62018-01-03 22:24:41 -05002018 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05002019 "{\n"
2020 " is_vep = %u\n"
2021 " is_vep_session = %u\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05002022 " next_sid = 0x%x (%u)\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05002023 " wait_cont_idx = 0x%x (%u)\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05002024 "}\n", getpid (), vep_idx,
2025 session->is_vep, session->is_vep_session,
Florin Coras134a9962018-08-28 11:32:04 -07002026 vep->next_sh, vep->next_sh,
Dave Wallace498b3a52017-11-09 13:00:34 -05002027 session->wait_cont_idx, session->wait_cont_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002028
Florin Coras134a9962018-08-28 11:32:04 -07002029 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002030 {
Florin Coras134a9962018-08-28 11:32:04 -07002031 session = vcl_session_get (wrk, sid);
Florin Coras070453d2018-08-24 17:04:27 -07002032 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002033 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002034 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002035 goto done;
2036 }
2037 if (PREDICT_FALSE (session->is_vep))
Dave Wallace048b1d62018-01-03 22:24:41 -05002038 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
2039 getpid (), vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002040 else if (PREDICT_FALSE (!session->is_vep_session))
2041 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002042 clib_warning ("VCL<%d>: ERROR: session (%u) "
2043 "is not a vep session!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002044 goto done;
2045 }
2046 vep = &session->vep;
Florin Coras134a9962018-08-28 11:32:04 -07002047 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
Dave Wallace048b1d62018-01-03 22:24:41 -05002048 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002049 "vep_idx (%u)!", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002050 sid, session->vep.vep_sh, vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002051 if (session->is_vep_session)
2052 {
2053 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2054 "{\n"
2055 " next_sid = 0x%x (%u)\n"
2056 " prev_sid = 0x%x (%u)\n"
2057 " vep_idx = 0x%x (%u)\n"
2058 " ev.events = 0x%x\n"
2059 " ev.data.u64 = 0x%llx\n"
2060 " et_mask = 0x%x\n"
2061 "}\n",
2062 vep_idx, sid, sid,
Florin Coras134a9962018-08-28 11:32:04 -07002063 vep->next_sh, vep->next_sh,
2064 vep->prev_sh, vep->prev_sh,
2065 vep->vep_sh, vep->vep_sh,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002066 vep->ev.events, vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002067 }
2068 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002069
2070done:
Dave Wallace048b1d62018-01-03 22:24:41 -05002071 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
2072 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002073}
2074
2075int
2076vppcom_epoll_create (void)
2077{
Florin Coras134a9962018-08-28 11:32:04 -07002078 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002079 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002080
Florin Coras134a9962018-08-28 11:32:04 -07002081 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002082
2083 vep_session->is_vep = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002084 vep_session->vep.vep_sh = ~0;
2085 vep_session->vep.next_sh = ~0;
2086 vep_session->vep.prev_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002087 vep_session->wait_cont_idx = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002088 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002089
Florin Coras134a9962018-08-28 11:32:04 -07002090 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_sh);
Florin Coras0d427d82018-06-27 03:24:07 -07002091 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
Florin Coras134a9962018-08-28 11:32:04 -07002092 getpid (), vep_session->session_index, vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002093
Florin Corasab2f6db2018-08-31 14:31:41 -07002094 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002095}
2096
2097int
Florin Coras134a9962018-08-28 11:32:04 -07002098vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002099 struct epoll_event *event)
2100{
Florin Coras134a9962018-08-28 11:32:04 -07002101 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002102 vcl_session_t *vep_session;
2103 vcl_session_t *session;
Florin Coras070453d2018-08-24 17:04:27 -07002104 int rv = VPPCOM_OK;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002105
Florin Coras134a9962018-08-28 11:32:04 -07002106 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002107 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002108 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002109 getpid (), vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002110 return VPPCOM_EINVAL;
2111 }
2112
Florin Coras134a9962018-08-28 11:32:04 -07002113 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002114 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002115 {
Florin Coras134a9962018-08-28 11:32:04 -07002116 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002117 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002118 }
2119 if (PREDICT_FALSE (!vep_session->is_vep))
2120 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002121 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Florin Coras134a9962018-08-28 11:32:04 -07002122 getpid (), vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002123 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002124 }
2125
Florin Coras134a9962018-08-28 11:32:04 -07002126 ASSERT (vep_session->vep.vep_sh == ~0);
2127 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002128
Florin Coras134a9962018-08-28 11:32:04 -07002129 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002130 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002131 {
Florin Coras134a9962018-08-28 11:32:04 -07002132 VDBG (0, "VCL<%d>: ERROR: Invalid session_handle (%u)!",
2133 getpid (), session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002134 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002135 }
2136 if (PREDICT_FALSE (session->is_vep))
2137 {
Florin Coras134a9962018-08-28 11:32:04 -07002138 clib_warning ("ERROR: session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002139 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002140 }
2141
2142 switch (op)
2143 {
2144 case EPOLL_CTL_ADD:
2145 if (PREDICT_FALSE (!event))
2146 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002147 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002148 "epoll_event structure!", getpid ());
Florin Coras070453d2018-08-24 17:04:27 -07002149 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002150 }
Florin Coras134a9962018-08-28 11:32:04 -07002151 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002152 {
Florin Coras7e12d942018-06-27 14:32:43 -07002153 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002154 next_session = vcl_session_get_w_handle (wrk,
2155 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002156 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002157 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002158 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002159 "vep.next_sid (%u) on vep_idx (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002160 getpid (), vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002161 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002162 }
Florin Coras134a9962018-08-28 11:32:04 -07002163 ASSERT (next_session->vep.prev_sh == vep_handle);
2164 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002165 }
Florin Coras134a9962018-08-28 11:32:04 -07002166 session->vep.next_sh = vep_session->vep.next_sh;
2167 session->vep.prev_sh = vep_handle;
2168 session->vep.vep_sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002169 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2170 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002171 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002172 session->is_vep_session = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002173 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002174
Florin Coras070453d2018-08-24 17:04:27 -07002175 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x, "
Florin Coras134a9962018-08-28 11:32:04 -07002176 "data 0x%llx!", getpid (), vep_handle, session_handle,
2177 event->events, event->data.u64);
Florin Coras0d427d82018-06-27 03:24:07 -07002178 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002179 break;
2180
2181 case EPOLL_CTL_MOD:
2182 if (PREDICT_FALSE (!event))
2183 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002184 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002185 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04002186 rv = VPPCOM_EINVAL;
2187 goto done;
2188 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002189 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002190 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002191 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Florin Coras134a9962018-08-28 11:32:04 -07002192 "not a vep session!", getpid (), session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002193 rv = VPPCOM_EINVAL;
2194 goto done;
2195 }
Florin Coras134a9962018-08-28 11:32:04 -07002196 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002197 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002198 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002199 "vep_idx (%u) != vep_idx (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002200 getpid (), session_handle,
2201 session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002202 rv = VPPCOM_EINVAL;
2203 goto done;
2204 }
2205 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2206 session->vep.ev = *event;
Florin Coras0d427d82018-06-27 03:24:07 -07002207 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
Florin Coras134a9962018-08-28 11:32:04 -07002208 " data 0x%llx!", getpid (), vep_handle, session_handle,
2209 event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002210 break;
2211
2212 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05002213 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002214 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002215 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Florin Coras134a9962018-08-28 11:32:04 -07002216 "not a vep session!", getpid (), session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002217 rv = VPPCOM_EINVAL;
2218 goto done;
2219 }
Florin Coras134a9962018-08-28 11:32:04 -07002220 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002221 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002222 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002223 "vep_idx (%u) != vep_idx (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002224 getpid (), session_handle,
2225 session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002226 rv = VPPCOM_EINVAL;
2227 goto done;
2228 }
2229
2230 vep_session->wait_cont_idx =
Florin Coras134a9962018-08-28 11:32:04 -07002231 (vep_session->wait_cont_idx == session_handle) ?
2232 session->vep.next_sh : vep_session->wait_cont_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002233
Florin Coras134a9962018-08-28 11:32:04 -07002234 if (session->vep.prev_sh == vep_handle)
2235 vep_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002236 else
2237 {
Florin Coras7e12d942018-06-27 14:32:43 -07002238 vcl_session_t *prev_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002239 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002240 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002241 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002242 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002243 "vep.prev_sid (%u) on sid (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002244 getpid (), session->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002245 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002246 }
Florin Coras134a9962018-08-28 11:32:04 -07002247 ASSERT (prev_session->vep.next_sh == session_handle);
2248 prev_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002249 }
Florin Coras134a9962018-08-28 11:32:04 -07002250 if (session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002251 {
Florin Coras7e12d942018-06-27 14:32:43 -07002252 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002253 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002254 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002255 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002256 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002257 "vep.next_sid (%u) on sid (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002258 getpid (), session->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002259 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002260 }
Florin Coras134a9962018-08-28 11:32:04 -07002261 ASSERT (next_session->vep.prev_sh == session_handle);
2262 next_session->vep.prev_sh = session->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002263 }
2264
2265 memset (&session->vep, 0, sizeof (session->vep));
Florin Coras134a9962018-08-28 11:32:04 -07002266 session->vep.next_sh = ~0;
2267 session->vep.prev_sh = ~0;
2268 session->vep.vep_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002269 session->is_vep_session = 0;
Florin Coras0d427d82018-06-27 03:24:07 -07002270 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
Florin Coras134a9962018-08-28 11:32:04 -07002271 getpid (), vep_handle, session_handle);
2272 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002273 break;
2274
2275 default:
Dave Wallace048b1d62018-01-03 22:24:41 -05002276 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002277 rv = VPPCOM_EINVAL;
2278 }
2279
Florin Coras134a9962018-08-28 11:32:04 -07002280 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002281
2282done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002283 return rv;
2284}
2285
Florin Coras86f04502018-09-12 16:08:01 -07002286static inline void
2287vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2288 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002289{
2290 session_disconnected_msg_t *disconnected_msg;
2291 session_connected_msg_t *connected_msg;
2292 session_accepted_msg_t *accepted_msg;
Florin Coras54693d22018-07-17 10:46:29 -07002293 u64 session_evt_data = ~0, handle;
Florin Coras99368312018-08-02 10:45:44 -07002294 u32 sid = ~0, session_events;
Florin Coras54693d22018-07-17 10:46:29 -07002295 vcl_session_msg_t *vcl_msg;
2296 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002297 u8 add_event = 0;
2298
2299 switch (e->event_type)
2300 {
2301 case FIFO_EVENT_APP_RX:
2302 ASSERT (e->fifo->client_thread_index == vcl_get_worker_index ());
2303 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2304 sid = e->fifo->client_session_index;
2305 session = vcl_session_get (wrk, sid);
2306 session_events = session->vep.ev.events;
2307 if (!(EPOLLIN & session->vep.ev.events))
2308 break;
2309 add_event = 1;
2310 events[*num_ev].events |= EPOLLIN;
2311 session_evt_data = session->vep.ev.data.u64;
2312 break;
2313 case FIFO_EVENT_APP_TX:
2314 sid = e->fifo->client_session_index;
2315 session = vcl_session_get (wrk, sid);
2316 session_events = session->vep.ev.events;
2317 if (!(EPOLLOUT & session_events))
2318 break;
2319 add_event = 1;
2320 events[*num_ev].events |= EPOLLOUT;
2321 session_evt_data = session->vep.ev.data.u64;
2322 break;
2323 case SESSION_IO_EVT_CT_TX:
2324 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2325 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
2326 sid = session->session_index;
2327 session_events = session->vep.ev.events;
2328 if (!(EPOLLIN & session->vep.ev.events))
2329 break;
2330 add_event = 1;
2331 events[*num_ev].events |= EPOLLIN;
2332 session_evt_data = session->vep.ev.data.u64;
2333 break;
2334 case SESSION_IO_EVT_CT_RX:
2335 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
2336 sid = session->session_index;
2337 session_events = session->vep.ev.events;
2338 if (!(EPOLLOUT & session_events))
2339 break;
2340 add_event = 1;
2341 events[*num_ev].events |= EPOLLOUT;
2342 session_evt_data = session->vep.ev.data.u64;
2343 break;
2344 case SESSION_CTRL_EVT_ACCEPTED:
2345 accepted_msg = (session_accepted_msg_t *) e->data;
2346 handle = accepted_msg->listener_handle;
2347 session = vcl_session_table_lookup_listener (wrk, handle);
2348 if (!session)
2349 {
2350 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
2351 "listener handle %llx", getpid (), handle);
2352 break;
2353 }
2354
2355 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
2356 vcl_msg->accepted_msg = *accepted_msg;
2357 session_events = session->vep.ev.events;
2358 if (!(EPOLLIN & session_events))
2359 break;
2360
2361 add_event = 1;
2362 events[*num_ev].events |= EPOLLIN;
2363 session_evt_data = session->vep.ev.data.u64;
2364 break;
2365 case SESSION_CTRL_EVT_CONNECTED:
2366 connected_msg = (session_connected_msg_t *) e->data;
2367 vcl_session_connected_handler (wrk, connected_msg);
2368 /* Generate EPOLLOUT because there's no connected event */
2369 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
2370 session = vcl_session_get (wrk, sid);
2371 session_events = session->vep.ev.events;
2372 if (EPOLLOUT & session_events)
2373 {
2374 add_event = 1;
2375 events[*num_ev].events |= EPOLLOUT;
2376 session_evt_data = session->vep.ev.data.u64;
2377 }
2378 break;
2379 case SESSION_CTRL_EVT_DISCONNECTED:
2380 disconnected_msg = (session_disconnected_msg_t *) e->data;
2381 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
2382 if (!(session = vcl_session_get (wrk, sid)))
2383 break;
2384 add_event = 1;
2385 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2386 session_evt_data = session->vep.ev.data.u64;
2387 session_events = session->vep.ev.events;
2388 break;
2389 case SESSION_CTRL_EVT_RESET:
2390 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2391 if (!(session = vcl_session_get (wrk, sid)))
2392 break;
2393 add_event = 1;
2394 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2395 session_evt_data = session->vep.ev.data.u64;
2396 session_events = session->vep.ev.events;
2397 break;
2398 default:
2399 VDBG (0, "unhandled: %u", e->event_type);
2400 break;
2401 }
2402
2403 if (add_event)
2404 {
2405 events[*num_ev].data.u64 = session_evt_data;
2406 if (EPOLLONESHOT & session_events)
2407 {
2408 session = vcl_session_get (wrk, sid);
2409 session->vep.ev.events = 0;
2410 }
2411 *num_ev += 1;
2412 }
2413}
2414
2415static int
2416vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2417 struct epoll_event *events, u32 maxevents,
2418 double wait_for_time, u32 * num_ev)
2419{
Florin Coras99368312018-08-02 10:45:44 -07002420 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002421 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002422 int i;
2423
2424 svm_msg_q_lock (mq);
2425 if (svm_msg_q_is_empty (mq))
2426 {
2427 if (!wait_for_time)
2428 {
2429 svm_msg_q_unlock (mq);
2430 return 0;
2431 }
2432 else if (wait_for_time < 0)
2433 {
2434 svm_msg_q_wait (mq);
2435 }
2436 else
2437 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002438 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002439 {
2440 svm_msg_q_unlock (mq);
2441 return 0;
2442 }
2443 }
2444 }
Florin Coras134a9962018-08-28 11:32:04 -07002445 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07002446 svm_msg_q_unlock (mq);
2447
Florin Coras134a9962018-08-28 11:32:04 -07002448 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002449 {
Florin Coras134a9962018-08-28 11:32:04 -07002450 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002451 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002452 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
Florin Coras99368312018-08-02 10:45:44 -07002453 svm_msg_q_free_msg (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002454 if (*num_ev == maxevents)
Florin Coras54693d22018-07-17 10:46:29 -07002455 {
Florin Coras86f04502018-09-12 16:08:01 -07002456 i += 1;
2457 break;
Florin Coras54693d22018-07-17 10:46:29 -07002458 }
2459 }
Florin Coras99368312018-08-02 10:45:44 -07002460
Florin Coras86f04502018-09-12 16:08:01 -07002461 vec_delete (wrk->mq_msg_vector, i, 0);
2462
Florin Coras54693d22018-07-17 10:46:29 -07002463 return *num_ev;
2464}
2465
Florin Coras99368312018-08-02 10:45:44 -07002466static int
Florin Coras134a9962018-08-28 11:32:04 -07002467vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002468 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002469{
2470 vcl_cut_through_registration_t *cr;
2471 double total_wait = 0, wait_slice;
Florin Coras99368312018-08-02 10:45:44 -07002472 int rv;
2473
2474 wait_for_time = (wait_for_time == -1) ? (double) 10e9 : wait_for_time;
Florin Coras134a9962018-08-28 11:32:04 -07002475 wait_slice = wrk->cut_through_registrations ? 10e-6 : wait_for_time;
Florin Coras99368312018-08-02 10:45:44 -07002476
2477 do
2478 {
Florin Coras134a9962018-08-28 11:32:04 -07002479 vcl_ct_registration_lock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07002480 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002481 pool_foreach (cr, wrk->cut_through_registrations, ({
Florin Coras86f04502018-09-12 16:08:01 -07002482 vcl_epoll_wait_handle_mq (wrk, cr->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002483 }));
2484 /* *INDENT-ON* */
Florin Coras134a9962018-08-28 11:32:04 -07002485 vcl_ct_registration_unlock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07002486
Florin Coras134a9962018-08-28 11:32:04 -07002487 rv = vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events,
Florin Coras86f04502018-09-12 16:08:01 -07002488 maxevents, n_evts ? 0 : wait_slice,
2489 &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002490 if (rv)
2491 total_wait += wait_slice;
Florin Coras86f04502018-09-12 16:08:01 -07002492 if (n_evts)
2493 return n_evts;
Florin Coras99368312018-08-02 10:45:44 -07002494 }
2495 while (total_wait < wait_for_time);
Florin Coras86f04502018-09-12 16:08:01 -07002496 return n_evts;
Florin Coras99368312018-08-02 10:45:44 -07002497}
2498
2499static int
Florin Coras134a9962018-08-28 11:32:04 -07002500vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002501 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002502{
2503 vcl_mq_evt_conn_t *mqc;
2504 int __clib_unused n_read;
2505 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07002506 u64 buf;
2507
Florin Coras134a9962018-08-28 11:32:04 -07002508 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2509 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2510 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07002511 for (i = 0; i < n_mq_evts; i++)
2512 {
Florin Coras134a9962018-08-28 11:32:04 -07002513 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002514 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002515 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002516 }
2517
2518 return (int) n_evts;
2519}
2520
Dave Wallacef7f809c2017-10-03 01:48:42 -04002521int
Florin Coras134a9962018-08-28 11:32:04 -07002522vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002523 int maxevents, double wait_for_time)
2524{
Florin Coras134a9962018-08-28 11:32:04 -07002525 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002526 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07002527 u32 n_evts = 0;
2528 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002529
2530 if (PREDICT_FALSE (maxevents <= 0))
2531 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002532 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05002533 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002534 return VPPCOM_EINVAL;
2535 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002536
Florin Coras134a9962018-08-28 11:32:04 -07002537 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07002538 if (!vep_session)
2539 return VPPCOM_EBADFD;
2540
Florin Coras54693d22018-07-17 10:46:29 -07002541 if (PREDICT_FALSE (!vep_session->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002542 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002543 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Florin Coras134a9962018-08-28 11:32:04 -07002544 getpid (), vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07002545 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002546 }
Florin Coras54693d22018-07-17 10:46:29 -07002547
2548 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002549
Florin Coras86f04502018-09-12 16:08:01 -07002550 if (vec_len (wrk->unhandled_evts_vector))
2551 {
2552 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2553 {
2554 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2555 events, &n_evts);
2556 if (n_evts == maxevents)
2557 {
2558 i += 1;
2559 break;
2560 }
2561 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002562
Florin Coras86f04502018-09-12 16:08:01 -07002563 vec_delete (wrk->unhandled_evts_vector, i, 0);
2564 }
2565
2566 if (vcm->cfg.use_mq_eventfd)
2567 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2568 wait_for_time);
2569
2570 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2571 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002572}
2573
Dave Wallace35830af2017-10-09 01:43:42 -04002574int
Florin Coras134a9962018-08-28 11:32:04 -07002575vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04002576 void *buffer, uint32_t * buflen)
2577{
Florin Coras134a9962018-08-28 11:32:04 -07002578 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002579 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04002580 int rv = VPPCOM_OK;
2581 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07002582 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04002583
Florin Coras134a9962018-08-28 11:32:04 -07002584 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002585 if (!session)
2586 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002587
Dave Wallace35830af2017-10-09 01:43:42 -04002588 switch (op)
2589 {
2590 case VPPCOM_ATTR_GET_NREAD:
Florin Coras54693d22018-07-17 10:46:29 -07002591 rv = vppcom_session_read_ready (session);
Florin Coras0d427d82018-06-27 03:24:07 -07002592 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
2593 getpid (), rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002594 break;
2595
Dave Wallace227867f2017-11-13 21:21:53 -05002596 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras134a9962018-08-28 11:32:04 -07002597 rv = vppcom_session_write_ready (session);
Florin Coras0d427d82018-06-27 03:24:07 -07002598 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
Florin Coras134a9962018-08-28 11:32:04 -07002599 getpid (), session_handle, rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002600 break;
2601
2602 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002603 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002604 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002605 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2606 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002607 *buflen = sizeof (*flags);
Florin Coras0d427d82018-06-27 03:24:07 -07002608 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2609 "is_nonblocking = %u", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002610 session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002611 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002612 }
2613 else
2614 rv = VPPCOM_EINVAL;
2615 break;
2616
2617 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002618 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002619 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002620 if (*flags & O_NONBLOCK)
2621 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2622 else
2623 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2624
Florin Coras0d427d82018-06-27 03:24:07 -07002625 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2626 " is_nonblocking = %u",
Florin Coras134a9962018-08-28 11:32:04 -07002627 getpid (), session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002628 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002629 }
2630 else
2631 rv = VPPCOM_EINVAL;
2632 break;
2633
2634 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002635 if (PREDICT_TRUE (buffer && buflen &&
2636 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002637 {
Florin Coras7e12d942018-06-27 14:32:43 -07002638 ep->is_ip4 = session->transport.is_ip4;
2639 ep->port = session->transport.rmt_port;
2640 if (session->transport.is_ip4)
2641 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
Steven2199aab2017-10-15 20:18:47 -07002642 sizeof (ip4_address_t));
2643 else
Florin Coras7e12d942018-06-27 14:32:43 -07002644 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
Steven2199aab2017-10-15 20:18:47 -07002645 sizeof (ip6_address_t));
2646 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002647 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2648 "addr = %U, port %u", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002649 session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002650 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002651 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2652 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002653 }
2654 else
2655 rv = VPPCOM_EINVAL;
2656 break;
2657
2658 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002659 if (PREDICT_TRUE (buffer && buflen &&
2660 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002661 {
Florin Coras7e12d942018-06-27 14:32:43 -07002662 ep->is_ip4 = session->transport.is_ip4;
2663 ep->port = session->transport.lcl_port;
2664 if (session->transport.is_ip4)
2665 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip4,
Steven2199aab2017-10-15 20:18:47 -07002666 sizeof (ip4_address_t));
2667 else
Florin Coras7e12d942018-06-27 14:32:43 -07002668 clib_memcpy (ep->ip, &session->transport.lcl_ip.ip6,
Steven2199aab2017-10-15 20:18:47 -07002669 sizeof (ip6_address_t));
2670 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002671 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2672 " addr = %U port %d", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002673 session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002674 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002675 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2676 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002677 }
2678 else
2679 rv = VPPCOM_EINVAL;
2680 break;
Stevenb5a11602017-10-11 09:59:30 -07002681
Dave Wallace048b1d62018-01-03 22:24:41 -05002682 case VPPCOM_ATTR_GET_LIBC_EPFD:
2683 rv = session->libc_epfd;
Florin Coras0d427d82018-06-27 03:24:07 -07002684 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2685 getpid (), rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002686 break;
2687
2688 case VPPCOM_ATTR_SET_LIBC_EPFD:
2689 if (PREDICT_TRUE (buffer && buflen &&
2690 (*buflen == sizeof (session->libc_epfd))))
2691 {
2692 session->libc_epfd = *(int *) buffer;
2693 *buflen = sizeof (session->libc_epfd);
2694
Florin Coras0d427d82018-06-27 03:24:07 -07002695 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2696 "buflen %d", getpid (), session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002697 }
2698 else
2699 rv = VPPCOM_EINVAL;
2700 break;
2701
2702 case VPPCOM_ATTR_GET_PROTOCOL:
2703 if (buffer && buflen && (*buflen >= sizeof (int)))
2704 {
Florin Coras7e12d942018-06-27 14:32:43 -07002705 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05002706 *buflen = sizeof (int);
2707
Florin Coras0d427d82018-06-27 03:24:07 -07002708 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2709 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2710 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002711 }
2712 else
2713 rv = VPPCOM_EINVAL;
2714 break;
2715
2716 case VPPCOM_ATTR_GET_LISTEN:
2717 if (buffer && buflen && (*buflen >= sizeof (int)))
2718 {
2719 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2720 VCL_SESS_ATTR_LISTEN);
2721 *buflen = sizeof (int);
2722
Florin Coras0d427d82018-06-27 03:24:07 -07002723 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2724 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002725 }
2726 else
2727 rv = VPPCOM_EINVAL;
2728 break;
2729
2730 case VPPCOM_ATTR_GET_ERROR:
2731 if (buffer && buflen && (*buflen >= sizeof (int)))
2732 {
2733 *(int *) buffer = 0;
2734 *buflen = sizeof (int);
2735
Florin Coras0d427d82018-06-27 03:24:07 -07002736 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2737 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002738 }
2739 else
2740 rv = VPPCOM_EINVAL;
2741 break;
2742
2743 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2744 if (buffer && buflen && (*buflen >= sizeof (u32)))
2745 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002746
2747 /* VPP-TBD */
2748 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002749 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002750 vcm->cfg.tx_fifo_size);
2751 *buflen = sizeof (u32);
2752
Florin Coras0d427d82018-06-27 03:24:07 -07002753 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2754 "buflen %d, #VPP-TBD#", getpid (),
2755 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002756 }
2757 else
2758 rv = VPPCOM_EINVAL;
2759 break;
2760
2761 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2762 if (buffer && buflen && (*buflen == sizeof (u32)))
2763 {
2764 /* VPP-TBD */
2765 session->sndbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07002766 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2767 "buflen %d, #VPP-TBD#", getpid (),
2768 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002769 }
2770 else
2771 rv = VPPCOM_EINVAL;
2772 break;
2773
2774 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2775 if (buffer && buflen && (*buflen >= sizeof (u32)))
2776 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002777
2778 /* VPP-TBD */
2779 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002780 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002781 vcm->cfg.rx_fifo_size);
2782 *buflen = sizeof (u32);
2783
Florin Coras0d427d82018-06-27 03:24:07 -07002784 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2785 "buflen %d, #VPP-TBD#", getpid (),
2786 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002787 }
2788 else
2789 rv = VPPCOM_EINVAL;
2790 break;
2791
2792 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2793 if (buffer && buflen && (*buflen == sizeof (u32)))
2794 {
2795 /* VPP-TBD */
2796 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07002797 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2798 "buflen %d, #VPP-TBD#", getpid (),
2799 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002800 }
2801 else
2802 rv = VPPCOM_EINVAL;
2803 break;
2804
2805 case VPPCOM_ATTR_GET_REUSEADDR:
2806 if (buffer && buflen && (*buflen >= sizeof (int)))
2807 {
2808 /* VPP-TBD */
2809 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2810 VCL_SESS_ATTR_REUSEADDR);
2811 *buflen = sizeof (int);
2812
Florin Coras0d427d82018-06-27 03:24:07 -07002813 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2814 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002815 }
2816 else
2817 rv = VPPCOM_EINVAL;
2818 break;
2819
Stevenb5a11602017-10-11 09:59:30 -07002820 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002821 if (buffer && buflen && (*buflen == sizeof (int)) &&
2822 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2823 {
2824 /* VPP-TBD */
2825 if (*(int *) buffer)
2826 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2827 else
2828 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2829
Florin Coras0d427d82018-06-27 03:24:07 -07002830 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
2831 " #VPP-TBD#", getpid (),
2832 VCL_SESS_ATTR_TEST (session->attr,
2833 VCL_SESS_ATTR_REUSEADDR), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002834 }
2835 else
2836 rv = VPPCOM_EINVAL;
2837 break;
2838
2839 case VPPCOM_ATTR_GET_REUSEPORT:
2840 if (buffer && buflen && (*buflen >= sizeof (int)))
2841 {
2842 /* VPP-TBD */
2843 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2844 VCL_SESS_ATTR_REUSEPORT);
2845 *buflen = sizeof (int);
2846
Florin Coras0d427d82018-06-27 03:24:07 -07002847 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
2848 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002849 }
2850 else
2851 rv = VPPCOM_EINVAL;
2852 break;
2853
2854 case VPPCOM_ATTR_SET_REUSEPORT:
2855 if (buffer && buflen && (*buflen == sizeof (int)) &&
2856 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2857 {
2858 /* VPP-TBD */
2859 if (*(int *) buffer)
2860 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2861 else
2862 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2863
Florin Coras0d427d82018-06-27 03:24:07 -07002864 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
2865 " #VPP-TBD#", getpid (),
2866 VCL_SESS_ATTR_TEST (session->attr,
2867 VCL_SESS_ATTR_REUSEPORT), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002868 }
2869 else
2870 rv = VPPCOM_EINVAL;
2871 break;
2872
2873 case VPPCOM_ATTR_GET_BROADCAST:
2874 if (buffer && buflen && (*buflen >= sizeof (int)))
2875 {
2876 /* VPP-TBD */
2877 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2878 VCL_SESS_ATTR_BROADCAST);
2879 *buflen = sizeof (int);
2880
Florin Coras0d427d82018-06-27 03:24:07 -07002881 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
2882 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002883 }
2884 else
2885 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002886 break;
2887
2888 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05002889 if (buffer && buflen && (*buflen == sizeof (int)))
2890 {
2891 /* VPP-TBD */
2892 if (*(int *) buffer)
2893 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2894 else
2895 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2896
Florin Coras0d427d82018-06-27 03:24:07 -07002897 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
2898 "#VPP-TBD#", getpid (),
2899 VCL_SESS_ATTR_TEST (session->attr,
2900 VCL_SESS_ATTR_BROADCAST), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002901 }
2902 else
2903 rv = VPPCOM_EINVAL;
2904 break;
2905
2906 case VPPCOM_ATTR_GET_V6ONLY:
2907 if (buffer && buflen && (*buflen >= sizeof (int)))
2908 {
2909 /* VPP-TBD */
2910 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2911 VCL_SESS_ATTR_V6ONLY);
2912 *buflen = sizeof (int);
2913
Florin Coras0d427d82018-06-27 03:24:07 -07002914 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
2915 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002916 }
2917 else
2918 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002919 break;
2920
2921 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05002922 if (buffer && buflen && (*buflen == sizeof (int)))
2923 {
2924 /* VPP-TBD */
2925 if (*(int *) buffer)
2926 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2927 else
2928 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2929
Florin Coras0d427d82018-06-27 03:24:07 -07002930 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
2931 "#VPP-TBD#", getpid (),
2932 VCL_SESS_ATTR_TEST (session->attr,
2933 VCL_SESS_ATTR_V6ONLY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002934 }
2935 else
2936 rv = VPPCOM_EINVAL;
2937 break;
2938
2939 case VPPCOM_ATTR_GET_KEEPALIVE:
2940 if (buffer && buflen && (*buflen >= sizeof (int)))
2941 {
2942 /* VPP-TBD */
2943 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2944 VCL_SESS_ATTR_KEEPALIVE);
2945 *buflen = sizeof (int);
2946
Florin Coras0d427d82018-06-27 03:24:07 -07002947 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
2948 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002949 }
2950 else
2951 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002952 break;
Stevenbccd3392017-10-12 20:42:21 -07002953
2954 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05002955 if (buffer && buflen && (*buflen == sizeof (int)))
2956 {
2957 /* VPP-TBD */
2958 if (*(int *) buffer)
2959 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2960 else
2961 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2962
Florin Coras0d427d82018-06-27 03:24:07 -07002963 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
2964 "#VPP-TBD#", getpid (),
2965 VCL_SESS_ATTR_TEST (session->attr,
2966 VCL_SESS_ATTR_KEEPALIVE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002967 }
2968 else
2969 rv = VPPCOM_EINVAL;
2970 break;
2971
2972 case VPPCOM_ATTR_GET_TCP_NODELAY:
2973 if (buffer && buflen && (*buflen >= sizeof (int)))
2974 {
2975 /* VPP-TBD */
2976 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2977 VCL_SESS_ATTR_TCP_NODELAY);
2978 *buflen = sizeof (int);
2979
Florin Coras0d427d82018-06-27 03:24:07 -07002980 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
2981 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002982 }
2983 else
2984 rv = VPPCOM_EINVAL;
2985 break;
2986
2987 case VPPCOM_ATTR_SET_TCP_NODELAY:
2988 if (buffer && buflen && (*buflen == sizeof (int)))
2989 {
2990 /* VPP-TBD */
2991 if (*(int *) buffer)
2992 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2993 else
2994 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
2995
Florin Coras0d427d82018-06-27 03:24:07 -07002996 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
2997 "#VPP-TBD#", getpid (),
2998 VCL_SESS_ATTR_TEST (session->attr,
2999 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003000 }
3001 else
3002 rv = VPPCOM_EINVAL;
3003 break;
3004
3005 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3006 if (buffer && buflen && (*buflen >= sizeof (int)))
3007 {
3008 /* VPP-TBD */
3009 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3010 VCL_SESS_ATTR_TCP_KEEPIDLE);
3011 *buflen = sizeof (int);
3012
Florin Coras0d427d82018-06-27 03:24:07 -07003013 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
3014 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003015 }
3016 else
3017 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003018 break;
3019
3020 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003021 if (buffer && buflen && (*buflen == sizeof (int)))
3022 {
3023 /* VPP-TBD */
3024 if (*(int *) buffer)
3025 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3026 else
3027 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3028
Florin Coras0d427d82018-06-27 03:24:07 -07003029 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
3030 "#VPP-TBD#", getpid (),
3031 VCL_SESS_ATTR_TEST (session->attr,
3032 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003033 }
3034 else
3035 rv = VPPCOM_EINVAL;
3036 break;
3037
3038 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3039 if (buffer && buflen && (*buflen >= sizeof (int)))
3040 {
3041 /* VPP-TBD */
3042 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3043 VCL_SESS_ATTR_TCP_KEEPINTVL);
3044 *buflen = sizeof (int);
3045
Florin Coras0d427d82018-06-27 03:24:07 -07003046 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
3047 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003048 }
3049 else
3050 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003051 break;
3052
3053 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003054 if (buffer && buflen && (*buflen == sizeof (int)))
3055 {
3056 /* VPP-TBD */
3057 if (*(int *) buffer)
3058 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3059 else
3060 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3061
Florin Coras0d427d82018-06-27 03:24:07 -07003062 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
3063 "#VPP-TBD#", getpid (),
3064 VCL_SESS_ATTR_TEST (session->attr,
3065 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003066 }
3067 else
3068 rv = VPPCOM_EINVAL;
3069 break;
3070
3071 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3072 if (buffer && buflen && (*buflen >= sizeof (u32)))
3073 {
3074 /* VPP-TBD */
3075 *(u32 *) buffer = session->user_mss;
3076 *buflen = sizeof (int);
3077
Florin Coras0d427d82018-06-27 03:24:07 -07003078 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
3079 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003080 }
3081 else
3082 rv = VPPCOM_EINVAL;
3083 break;
3084
3085 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3086 if (buffer && buflen && (*buflen == sizeof (u32)))
3087 {
3088 /* VPP-TBD */
3089 session->user_mss = *(u32 *) buffer;
3090
Florin Coras0d427d82018-06-27 03:24:07 -07003091 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
3092 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003093 }
3094 else
3095 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003096 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003097
3098 default:
3099 rv = VPPCOM_EINVAL;
3100 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003101 }
3102
Dave Wallace35830af2017-10-09 01:43:42 -04003103 return rv;
3104}
3105
Stevenac1f96d2017-10-24 16:03:58 -07003106int
Florin Coras134a9962018-08-28 11:32:04 -07003107vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003108 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3109{
Florin Coras134a9962018-08-28 11:32:04 -07003110 vcl_worker_t *wrk = vcl_worker_get_current ();
Stevenac1f96d2017-10-24 16:03:58 -07003111 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07003112 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07003113
3114 if (ep)
3115 {
Florin Coras134a9962018-08-28 11:32:04 -07003116 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003117 if (PREDICT_FALSE (!session))
Stevenac1f96d2017-10-24 16:03:58 -07003118 {
Florin Coras0d427d82018-06-27 03:24:07 -07003119 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
Florin Coras134a9962018-08-28 11:32:04 -07003120 getpid (), session_handle);
Florin Coras460dce62018-07-27 05:45:06 -07003121 return VPPCOM_EBADFD;
Stevenac1f96d2017-10-24 16:03:58 -07003122 }
Florin Coras7e12d942018-06-27 14:32:43 -07003123 ep->is_ip4 = session->transport.is_ip4;
3124 ep->port = session->transport.rmt_port;
Stevenac1f96d2017-10-24 16:03:58 -07003125 }
Steven58f464e2017-10-25 12:33:12 -07003126
3127 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003128 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003129 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003130 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003131 else
3132 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003133 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
3134 getpid (), flags);
Florin Coras460dce62018-07-27 05:45:06 -07003135 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003136 }
3137
Florin Coras99368312018-08-02 10:45:44 -07003138 if (ep)
3139 {
3140 if (session->transport.is_ip4)
3141 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip4,
3142 sizeof (ip4_address_t));
3143 else
3144 clib_memcpy (ep->ip, &session->transport.rmt_ip.ip6,
3145 sizeof (ip6_address_t));
3146 }
Florin Coras460dce62018-07-27 05:45:06 -07003147
Stevenac1f96d2017-10-24 16:03:58 -07003148 return rv;
3149}
3150
3151int
Florin Coras134a9962018-08-28 11:32:04 -07003152vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003153 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3154{
Dave Wallace617dffa2017-10-26 14:47:06 -04003155 if (!buffer)
3156 return VPPCOM_EINVAL;
3157
3158 if (ep)
3159 {
3160 // TBD
3161 return VPPCOM_EINVAL;
3162 }
3163
3164 if (flags)
3165 {
3166 // TBD check the flags and do the right thing
Florin Coras0d427d82018-06-27 03:24:07 -07003167 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3168 getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003169 }
3170
Florin Coras134a9962018-08-28 11:32:04 -07003171 return (vppcom_session_write (session_handle, buffer, buflen));
Stevenac1f96d2017-10-24 16:03:58 -07003172}
3173
Dave Wallace048b1d62018-01-03 22:24:41 -05003174int
3175vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3176{
Florin Coras134a9962018-08-28 11:32:04 -07003177 vcl_worker_t *wrk = vcl_worker_get_current ();
3178 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003179 u32 i, keep_trying = 1;
3180 int rv, num_ev = 0;
3181
Florin Coras0d427d82018-06-27 03:24:07 -07003182 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3183 getpid (), vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003184
3185 if (!vp)
3186 return VPPCOM_EFAULT;
3187
3188 do
3189 {
Florin Coras7e12d942018-06-27 14:32:43 -07003190 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003191
3192 for (i = 0; i < n_sids; i++)
3193 {
3194 ASSERT (vp[i].revents);
3195
Florin Coras134a9962018-08-28 11:32:04 -07003196 session = vcl_session_get (wrk, vp[i].sid);
Florin Coras070453d2018-08-24 17:04:27 -07003197 if (!session)
3198 continue;
Dave Wallace048b1d62018-01-03 22:24:41 -05003199
3200 if (*vp[i].revents)
3201 *vp[i].revents = 0;
3202
3203 if (POLLIN & vp[i].events)
3204 {
Florin Coras54693d22018-07-17 10:46:29 -07003205 rv = vppcom_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003206 if (rv > 0)
3207 {
3208 *vp[i].revents |= POLLIN;
3209 num_ev++;
3210 }
3211 else if (rv < 0)
3212 {
3213 switch (rv)
3214 {
3215 case VPPCOM_ECONNRESET:
3216 *vp[i].revents = POLLHUP;
3217 break;
3218
3219 default:
3220 *vp[i].revents = POLLERR;
3221 break;
3222 }
3223 num_ev++;
3224 }
3225 }
3226
3227 if (POLLOUT & vp[i].events)
3228 {
Florin Coras134a9962018-08-28 11:32:04 -07003229 rv = vppcom_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003230 if (rv > 0)
3231 {
3232 *vp[i].revents |= POLLOUT;
3233 num_ev++;
3234 }
3235 else if (rv < 0)
3236 {
3237 switch (rv)
3238 {
3239 case VPPCOM_ECONNRESET:
3240 *vp[i].revents = POLLHUP;
3241 break;
3242
3243 default:
3244 *vp[i].revents = POLLERR;
3245 break;
3246 }
3247 num_ev++;
3248 }
3249 }
3250
Dave Wallace7e607a72018-06-18 18:41:32 -04003251 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003252 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003253 *vp[i].revents = POLLNVAL;
3254 num_ev++;
3255 }
3256 }
3257 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003258 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003259 }
3260 while ((num_ev == 0) && keep_trying);
3261
3262 if (VPPCOM_DEBUG > 3)
3263 {
3264 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3265 for (i = 0; i < n_sids; i++)
3266 {
3267 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
3268 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
3269 vp[i].events, *vp[i].revents);
3270 }
3271 }
3272 return num_ev;
3273}
3274
Florin Coras99368312018-08-02 10:45:44 -07003275int
3276vppcom_mq_epoll_fd (void)
3277{
Florin Coras134a9962018-08-28 11:32:04 -07003278 vcl_worker_t *wrk = vcl_worker_get_current ();
3279 return wrk->mqs_epfd;
3280}
3281
3282int
3283vppcom_session_index (uint32_t session_handle)
3284{
3285 return session_handle & 0xFFFFFF;
3286}
3287
3288int
3289vppcom_worker_register (void)
3290{
Florin Coras6d4bb422018-09-04 22:07:27 -07003291 if (vcl_worker_alloc_and_init ())
Florin Coras134a9962018-08-28 11:32:04 -07003292 return VPPCOM_OK;
3293 return VPPCOM_EEXIST;
Florin Coras99368312018-08-02 10:45:44 -07003294}
3295
Dave Wallacee22aa742017-10-20 12:30:38 -04003296/*
3297 * fd.io coding-style-patch-verification: ON
3298 *
3299 * Local Variables:
3300 * eval: (c-set-style "gnu")
3301 * End:
3302 */