blob: 1b619cf3f21021ddb3fedcacf2763aa316842989 [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 -070025
Florin Corasd85de682018-11-29 17:02:29 -080026static int
27vcl_wait_for_segment (u64 segment_handle)
Florin Coras54693d22018-07-17 10:46:29 -070028{
Florin Corasd85de682018-11-29 17:02:29 -080029 vcl_worker_t *wrk = vcl_worker_get_current ();
30 u32 wait_for_seconds = 10, segment_index;
31 f64 timeout;
Florin Coras54693d22018-07-17 10:46:29 -070032
Florin Corasd85de682018-11-29 17:02:29 -080033 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
34 return 1;
Florin Coras54693d22018-07-17 10:46:29 -070035
Florin Corasd85de682018-11-29 17:02:29 -080036 timeout = clib_time_now (&wrk->clib_time) + wait_for_seconds;
37 while (clib_time_now (&wrk->clib_time) < timeout)
Florin Coras54693d22018-07-17 10:46:29 -070038 {
Florin Corasd85de682018-11-29 17:02:29 -080039 segment_index = vcl_segment_table_lookup (segment_handle);
40 if (segment_index != VCL_INVALID_SEGMENT_INDEX)
41 return 0;
42 usleep (10);
Florin Coras54693d22018-07-17 10:46:29 -070043 }
Florin Corasd85de682018-11-29 17:02:29 -080044 return 1;
Florin Coras54693d22018-07-17 10:46:29 -070045}
46
Florin Coras697faea2018-06-27 17:10:49 -070047const char *
Dave Wallace543852a2017-08-03 02:11:34 -040048vppcom_session_state_str (session_state_t state)
49{
50 char *st;
51
52 switch (state)
53 {
54 case STATE_START:
55 st = "STATE_START";
56 break;
57
58 case STATE_CONNECT:
59 st = "STATE_CONNECT";
60 break;
61
62 case STATE_LISTEN:
63 st = "STATE_LISTEN";
64 break;
65
66 case STATE_ACCEPT:
67 st = "STATE_ACCEPT";
68 break;
69
Dave Wallace4878cbe2017-11-21 03:45:09 -050070 case STATE_CLOSE_ON_EMPTY:
71 st = "STATE_CLOSE_ON_EMPTY";
72 break;
73
Dave Wallace543852a2017-08-03 02:11:34 -040074 case STATE_DISCONNECT:
75 st = "STATE_DISCONNECT";
76 break;
77
78 case STATE_FAILED:
79 st = "STATE_FAILED";
80 break;
81
82 default:
83 st = "UNKNOWN_STATE";
84 break;
85 }
86
87 return st;
88}
89
Dave Wallace543852a2017-08-03 02:11:34 -040090u8 *
91format_ip4_address (u8 * s, va_list * args)
92{
93 u8 *a = va_arg (*args, u8 *);
94 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
95}
96
97u8 *
98format_ip6_address (u8 * s, va_list * args)
99{
100 ip6_address_t *a = va_arg (*args, ip6_address_t *);
101 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
102
103 i_max_n_zero = ARRAY_LEN (a->as_u16);
104 max_n_zeros = 0;
105 i_first_zero = i_max_n_zero;
106 n_zeros = 0;
107 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
108 {
109 u32 is_zero = a->as_u16[i] == 0;
110 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
111 {
112 i_first_zero = i;
113 n_zeros = 0;
114 }
115 n_zeros += is_zero;
116 if ((!is_zero && n_zeros > max_n_zeros)
117 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
118 {
119 i_max_n_zero = i_first_zero;
120 max_n_zeros = n_zeros;
121 i_first_zero = ARRAY_LEN (a->as_u16);
122 n_zeros = 0;
123 }
124 }
125
126 last_double_colon = 0;
127 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
128 {
129 if (i == i_max_n_zero && max_n_zeros > 1)
130 {
131 s = format (s, "::");
132 i += max_n_zeros - 1;
133 last_double_colon = 1;
134 }
135 else
136 {
137 s = format (s, "%s%x",
138 (last_double_colon || i == 0) ? "" : ":",
139 clib_net_to_host_u16 (a->as_u16[i]));
140 last_double_colon = 0;
141 }
142 }
143
144 return s;
145}
146
147/* Format an IP46 address. */
148u8 *
149format_ip46_address (u8 * s, va_list * args)
150{
151 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
152 ip46_type_t type = va_arg (*args, ip46_type_t);
153 int is_ip4 = 1;
154
155 switch (type)
156 {
157 case IP46_TYPE_ANY:
158 is_ip4 = ip46_address_is_ip4 (ip46);
159 break;
160 case IP46_TYPE_IP4:
161 is_ip4 = 1;
162 break;
163 case IP46_TYPE_IP6:
164 is_ip4 = 0;
165 break;
166 }
167
168 return is_ip4 ?
169 format (s, "%U", format_ip4_address, &ip46->ip4) :
170 format (s, "%U", format_ip6_address, &ip46->ip6);
171}
172
Florin Coras697faea2018-06-27 17:10:49 -0700173/*
174 * VPPCOM Utility Functions
175 */
176
Florin Coras697faea2018-06-27 17:10:49 -0700177
Florin Corasc9fbd662018-08-24 12:59:56 -0700178static svm_msg_q_t *
Florin Coras134a9962018-08-28 11:32:04 -0700179vcl_session_vpp_evt_q (vcl_worker_t * wrk, vcl_session_t * s)
Florin Corasc9fbd662018-08-24 12:59:56 -0700180{
181 if (vcl_session_is_ct (s))
Florin Coras134a9962018-08-28 11:32:04 -0700182 return wrk->vpp_event_queues[0];
Florin Corasc9fbd662018-08-24 12:59:56 -0700183 else
Florin Coras134a9962018-08-28 11:32:04 -0700184 return wrk->vpp_event_queues[s->tx_fifo->master_thread_index];
Florin Corasc9fbd662018-08-24 12:59:56 -0700185}
186
Florin Coras54693d22018-07-17 10:46:29 -0700187static void
188vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
189 session_handle_t handle, int retval)
190{
191 app_session_evt_t _app_evt, *app_evt = &_app_evt;
192 session_accepted_reply_msg_t *rmp;
193 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
194 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
195 rmp->handle = handle;
196 rmp->context = context;
197 rmp->retval = retval;
198 app_send_ctrl_evt_to_vpp (mq, app_evt);
199}
200
Florin Coras99368312018-08-02 10:45:44 -0700201static void
202vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
203 session_handle_t handle, int retval)
204{
205 app_session_evt_t _app_evt, *app_evt = &_app_evt;
206 session_disconnected_reply_msg_t *rmp;
207 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
208 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
209 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
210 rmp->handle = handle;
211 rmp->context = context;
212 rmp->retval = retval;
213 app_send_ctrl_evt_to_vpp (mq, app_evt);
214}
215
Florin Corasc9fbd662018-08-24 12:59:56 -0700216static void
217vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
218 session_handle_t handle, int retval)
219{
220 app_session_evt_t _app_evt, *app_evt = &_app_evt;
221 session_reset_reply_msg_t *rmp;
222 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
223 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
224 rmp->handle = handle;
225 rmp->context = context;
226 rmp->retval = retval;
227 app_send_ctrl_evt_to_vpp (mq, app_evt);
228}
229
Florin Coras54693d22018-07-17 10:46:29 -0700230static u32
Florin Coras134a9962018-08-28 11:32:04 -0700231vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700232{
233 vcl_session_t *session, *listen_session;
234 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras134a9962018-08-28 11:32:04 -0700235 u32 vpp_wrk_index;
Florin Coras99368312018-08-02 10:45:44 -0700236 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700237
Florin Coras134a9962018-08-28 11:32:04 -0700238 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700239
Florin Coras134a9962018-08-28 11:32:04 -0700240 listen_session = vcl_session_table_lookup_listener (wrk,
241 mp->listener_handle);
Florin Coras54693d22018-07-17 10:46:29 -0700242 if (!listen_session)
243 {
244 svm_msg_q_t *evt_q;
245 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
246 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
247 "unknown vpp listener handle %llx",
248 getpid (), mp->listener_handle);
249 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
250 VNET_API_ERROR_INVALID_ARGUMENT);
Florin Coras134a9962018-08-28 11:32:04 -0700251 vcl_session_free (wrk, session);
Florin Coras54693d22018-07-17 10:46:29 -0700252 return VCL_INVALID_SESSION_INDEX;
253 }
254
Florin Coras54693d22018-07-17 10:46:29 -0700255 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
256 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
257
258 if (mp->server_event_queue_address)
259 {
260 session->vpp_evt_q = uword_to_pointer (mp->client_event_queue_address,
261 svm_msg_q_t *);
262 session->our_evt_q = uword_to_pointer (mp->server_event_queue_address,
263 svm_msg_q_t *);
Florin Corasd85de682018-11-29 17:02:29 -0800264 if (vcl_wait_for_segment (mp->segment_handle))
265 {
266 clib_warning ("segment for session %u couldn't be mounted!",
267 session->session_index);
268 return VCL_INVALID_SESSION_INDEX;
269 }
Florin Coras134a9962018-08-28 11:32:04 -0700270 rx_fifo->master_session_index = session->session_index;
271 tx_fifo->master_session_index = session->session_index;
Florin Coras21795132018-09-09 09:40:51 -0700272 rx_fifo->master_thread_index = vcl_get_worker_index ();
273 tx_fifo->master_thread_index = vcl_get_worker_index ();
Florin Coras134a9962018-08-28 11:32:04 -0700274 vec_validate (wrk->vpp_event_queues, 0);
Florin Coras99368312018-08-02 10:45:44 -0700275 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
Florin Coras134a9962018-08-28 11:32:04 -0700276 wrk->vpp_event_queues[0] = evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700277 }
278 else
279 {
280 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
281 svm_msg_q_t *);
Florin Coras134a9962018-08-28 11:32:04 -0700282 rx_fifo->client_session_index = session->session_index;
283 tx_fifo->client_session_index = session->session_index;
Florin Coras21795132018-09-09 09:40:51 -0700284 rx_fifo->client_thread_index = vcl_get_worker_index ();
285 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras99368312018-08-02 10:45:44 -0700286 vpp_wrk_index = tx_fifo->master_thread_index;
Florin Coras134a9962018-08-28 11:32:04 -0700287 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
288 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700289 }
290
291 session->vpp_handle = mp->handle;
292 session->client_context = mp->context;
293 session->rx_fifo = rx_fifo;
294 session->tx_fifo = tx_fifo;
295
296 session->session_state = STATE_ACCEPT;
297 session->transport.rmt_port = mp->port;
298 session->transport.is_ip4 = mp->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500299 clib_memcpy_fast (&session->transport.rmt_ip, mp->ip,
300 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700301
Florin Coras134a9962018-08-28 11:32:04 -0700302 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700303 session->transport.lcl_port = listen_session->transport.lcl_port;
304 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700305 session->session_type = listen_session->session_type;
306 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
Florin Coras54693d22018-07-17 10:46:29 -0700307
308 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
Florin Coras134a9962018-08-28 11:32:04 -0700309 " address %U port %d queue %p!", getpid (), mp->handle,
310 session->session_index,
Florin Coras54693d22018-07-17 10:46:29 -0700311 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
312 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
313 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
314 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
315
Florin Coras134a9962018-08-28 11:32:04 -0700316 return session->session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700317}
318
319static u32
Florin Coras134a9962018-08-28 11:32:04 -0700320vcl_session_connected_handler (vcl_worker_t * wrk,
321 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700322{
Florin Coras99368312018-08-02 10:45:44 -0700323 u32 session_index, vpp_wrk_index;
Florin Coras54693d22018-07-17 10:46:29 -0700324 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700325 vcl_session_t *session = 0;
326 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700327
328 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700329 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700330 if (!session)
331 {
332 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
333 "Invalid session index (%u)!",
334 getpid (), mp->handle, session_index);
335 return VCL_INVALID_SESSION_INDEX;
336 }
Florin Coras54693d22018-07-17 10:46:29 -0700337 if (mp->retval)
338 {
Florin Coras070453d2018-08-24 17:04:27 -0700339 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed! %U", getpid (),
Florin Coras21795132018-09-09 09:40:51 -0700340 session_index, format_api_error, ntohl (mp->retval));
Florin Coras070453d2018-08-24 17:04:27 -0700341 session->session_state = STATE_FAILED;
342 session->vpp_handle = mp->handle;
343 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700344 }
345
Florin Coras460dce62018-07-27 05:45:06 -0700346 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
347 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Corasd85de682018-11-29 17:02:29 -0800348 if (vcl_wait_for_segment (mp->segment_handle))
349 {
350 clib_warning ("segment for session %u couldn't be mounted!",
351 session->session_index);
352 return VCL_INVALID_SESSION_INDEX;
353 }
354
Florin Coras460dce62018-07-27 05:45:06 -0700355 rx_fifo->client_session_index = session_index;
356 tx_fifo->client_session_index = session_index;
Florin Coras21795132018-09-09 09:40:51 -0700357 rx_fifo->client_thread_index = vcl_get_worker_index ();
358 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras460dce62018-07-27 05:45:06 -0700359
Florin Coras54693d22018-07-17 10:46:29 -0700360 if (mp->client_event_queue_address)
361 {
362 session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address,
363 svm_msg_q_t *);
364 session->our_evt_q = uword_to_pointer (mp->client_event_queue_address,
365 svm_msg_q_t *);
Florin Coras99368312018-08-02 10:45:44 -0700366
Florin Coras134a9962018-08-28 11:32:04 -0700367 vec_validate (wrk->vpp_event_queues, 0);
Florin Coras99368312018-08-02 10:45:44 -0700368 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
Florin Coras134a9962018-08-28 11:32:04 -0700369 wrk->vpp_event_queues[0] = evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700370 }
371 else
Florin Coras99368312018-08-02 10:45:44 -0700372 {
373 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
374 svm_msg_q_t *);
375 vpp_wrk_index = tx_fifo->master_thread_index;
Florin Coras134a9962018-08-28 11:32:04 -0700376 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
377 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -0700378 }
Florin Coras54693d22018-07-17 10:46:29 -0700379
Florin Coras54693d22018-07-17 10:46:29 -0700380 session->rx_fifo = rx_fifo;
381 session->tx_fifo = tx_fifo;
382 session->vpp_handle = mp->handle;
383 session->transport.is_ip4 = mp->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500384 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
385 sizeof (session->transport.lcl_ip));
Florin Coras54693d22018-07-17 10:46:29 -0700386 session->transport.lcl_port = mp->lcl_port;
387 session->session_state = STATE_CONNECT;
388
389 /* Add it to lookup table */
Florin Coras134a9962018-08-28 11:32:04 -0700390 hash_set (wrk->session_index_by_vpp_handles, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700391
392 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
393 "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
394 getpid (), mp->handle, session_index, session->rx_fifo,
395 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700396
Florin Coras54693d22018-07-17 10:46:29 -0700397 return session_index;
398}
399
Florin Corasc9fbd662018-08-24 12:59:56 -0700400static u32
Florin Coras134a9962018-08-28 11:32:04 -0700401vcl_session_reset_handler (vcl_worker_t * wrk,
402 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700403{
404 vcl_session_t *session;
405 u32 sid;
406
Florin Coras134a9962018-08-28 11:32:04 -0700407 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
408 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700409 if (!session)
410 {
411 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
412 return VCL_INVALID_SESSION_INDEX;
413 }
414 session->session_state = STATE_CLOSE_ON_EMPTY;
415 VDBG (0, "reset handle 0x%llx, sid %u ", reset_msg->handle, sid);
Florin Coras134a9962018-08-28 11:32:04 -0700416 vcl_send_session_reset_reply (vcl_session_vpp_evt_q (wrk, session),
Florin Coras47c40e22018-11-26 17:01:36 -0800417 wrk->my_client_index, reset_msg->handle, 0);
Florin Corasc9fbd662018-08-24 12:59:56 -0700418 return sid;
419}
420
Florin Coras60116992018-08-27 09:52:18 -0700421static u32
Florin Coras134a9962018-08-28 11:32:04 -0700422vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700423{
424 vcl_session_t *session;
425 u32 sid = mp->context;
426
Florin Coras134a9962018-08-28 11:32:04 -0700427 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700428 if (mp->retval)
429 {
Florin Corasd85de682018-11-29 17:02:29 -0800430 VERR ("vpp handle 0x%llx, sid %u: bind failed: %U", mp->handle, sid,
431 format_api_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700432 if (session)
433 {
434 session->session_state = STATE_FAILED;
435 session->vpp_handle = mp->handle;
436 return sid;
437 }
438 else
439 {
440 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
441 "Invalid session index (%u)!",
442 getpid (), mp->handle, sid);
443 return VCL_INVALID_SESSION_INDEX;
444 }
445 }
446
447 session->vpp_handle = mp->handle;
448 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500449 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
450 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700451 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700452 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Coras60116992018-08-27 09:52:18 -0700453 session->session_state = STATE_LISTEN;
454
455 if (session->is_dgram)
456 {
457 svm_fifo_t *rx_fifo, *tx_fifo;
458 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
459 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
460 rx_fifo->client_session_index = sid;
461 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
462 tx_fifo->client_session_index = sid;
463 session->rx_fifo = rx_fifo;
464 session->tx_fifo = tx_fifo;
465 }
466
467 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
468 getpid (), mp->handle, sid);
469 return sid;
470}
471
Florin Coras86f04502018-09-12 16:08:01 -0700472static int
473vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700474{
475 session_accepted_msg_t *accepted_msg;
476 session_disconnected_msg_t *disconnected_msg;
477 vcl_session_msg_t *vcl_msg;
478 vcl_session_t *session;
479 u64 handle;
480 u32 sid;
481
482 switch (e->event_type)
483 {
484 case FIFO_EVENT_APP_RX:
Florin Coras86f04502018-09-12 16:08:01 -0700485 case FIFO_EVENT_APP_TX:
486 case SESSION_IO_EVT_CT_RX:
487 case SESSION_IO_EVT_CT_TX:
488 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -0700489 break;
490 case SESSION_CTRL_EVT_ACCEPTED:
491 accepted_msg = (session_accepted_msg_t *) e->data;
492 handle = accepted_msg->listener_handle;
Florin Coras134a9962018-08-28 11:32:04 -0700493 session = vcl_session_table_lookup_listener (wrk, handle);
Florin Coras54693d22018-07-17 10:46:29 -0700494 if (!session)
495 {
496 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
497 "listener handle %llx", getpid (), handle);
498 break;
499 }
500
501 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
502 vcl_msg->accepted_msg = *accepted_msg;
503 break;
504 case SESSION_CTRL_EVT_CONNECTED:
Florin Coras134a9962018-08-28 11:32:04 -0700505 vcl_session_connected_handler (wrk,
506 (session_connected_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700507 break;
508 case SESSION_CTRL_EVT_DISCONNECTED:
509 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras134a9962018-08-28 11:32:04 -0700510 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
511 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700512 if (!session)
513 {
514 VDBG (0, "request to disconnect unknown handle 0x%llx",
515 disconnected_msg->handle);
516 break;
517 }
Florin Coras54693d22018-07-17 10:46:29 -0700518 session->session_state = STATE_DISCONNECT;
Florin Coras86f04502018-09-12 16:08:01 -0700519 VDBG (0, "disconnected handle 0x%llx, sid %u", disconnected_msg->handle,
Florin Corasc9fbd662018-08-24 12:59:56 -0700520 sid);
521 break;
522 case SESSION_CTRL_EVT_RESET:
Florin Coras134a9962018-08-28 11:32:04 -0700523 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -0700524 break;
525 case SESSION_CTRL_EVT_BOUND:
Florin Coras134a9962018-08-28 11:32:04 -0700526 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700527 break;
528 default:
529 clib_warning ("unhandled %u", e->event_type);
530 }
531 return VPPCOM_OK;
532}
533
Florin Coras697faea2018-06-27 17:10:49 -0700534static inline int
535vppcom_wait_for_session_state_change (u32 session_index,
536 session_state_t state,
537 f64 wait_for_time)
538{
Florin Coras134a9962018-08-28 11:32:04 -0700539 vcl_worker_t *wrk = vcl_worker_get_current ();
540 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -0700541 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -0700542 svm_msg_q_msg_t msg;
543 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -0400544
Florin Coras697faea2018-06-27 17:10:49 -0700545 do
Dave Wallace543852a2017-08-03 02:11:34 -0400546 {
Florin Coras134a9962018-08-28 11:32:04 -0700547 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700548 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -0700549 {
Florin Coras070453d2018-08-24 17:04:27 -0700550 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -0700551 }
552 if (session->session_state & state)
553 {
Florin Coras697faea2018-06-27 17:10:49 -0700554 return VPPCOM_OK;
555 }
556 if (session->session_state & STATE_FAILED)
557 {
Florin Coras697faea2018-06-27 17:10:49 -0700558 return VPPCOM_ECONNREFUSED;
559 }
Florin Coras54693d22018-07-17 10:46:29 -0700560
Florin Coras134a9962018-08-28 11:32:04 -0700561 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -0700562 continue;
Florin Coras134a9962018-08-28 11:32:04 -0700563 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -0700564 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -0700565 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800566 }
Florin Coras134a9962018-08-28 11:32:04 -0700567 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800568
Florin Coras697faea2018-06-27 17:10:49 -0700569 VDBG (0, "VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (), state,
570 vppcom_session_state_str (state));
571 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -0400572
Florin Coras697faea2018-06-27 17:10:49 -0700573 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -0500574}
575
Florin Coras697faea2018-06-27 17:10:49 -0700576static int
577vppcom_app_session_enable (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400578{
Florin Coras697faea2018-06-27 17:10:49 -0700579 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400580
Florin Coras697faea2018-06-27 17:10:49 -0700581 if (vcm->app_state != STATE_APP_ENABLED)
582 {
583 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
Florin Coras134a9962018-08-28 11:32:04 -0700584 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
Florin Coras697faea2018-06-27 17:10:49 -0700585 if (PREDICT_FALSE (rv))
586 {
587 VDBG (0, "VCL<%d>: application session enable timed out! "
588 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
589 return rv;
590 }
591 }
592 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400593}
594
Florin Coras697faea2018-06-27 17:10:49 -0700595static int
596vppcom_app_attach (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400597{
Florin Coras697faea2018-06-27 17:10:49 -0700598 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400599
Florin Coras697faea2018-06-27 17:10:49 -0700600 vppcom_app_send_attach ();
Florin Coras134a9962018-08-28 11:32:04 -0700601 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
Florin Coras697faea2018-06-27 17:10:49 -0700602 if (PREDICT_FALSE (rv))
603 {
604 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
605 getpid (), rv, vppcom_retval_str (rv));
606 return rv;
607 }
Dave Wallace543852a2017-08-03 02:11:34 -0400608
Florin Coras697faea2018-06-27 17:10:49 -0700609 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400610}
611
612static int
Florin Corasab2f6db2018-08-31 14:31:41 -0700613vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400614{
Florin Coras134a9962018-08-28 11:32:04 -0700615 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -0700616 vcl_session_t *session = 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500617 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400618
Florin Corasab2f6db2018-08-31 14:31:41 -0700619 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700620 if (!session)
621 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500622
623 vpp_handle = session->vpp_handle;
Florin Coras134a9962018-08-28 11:32:04 -0700624 vcl_session_table_del_listener (wrk, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500625 session->vpp_handle = ~0;
Florin Coras7e12d942018-06-27 14:32:43 -0700626 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500627
Florin Coras0d427d82018-06-27 03:24:07 -0700628 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
Florin Corasab2f6db2018-08-31 14:31:41 -0700629 " 0x%x (%s)", getpid (), vpp_handle, session_handle, STATE_DISCONNECT,
Florin Coras0d427d82018-06-27 03:24:07 -0700630 vppcom_session_state_str (STATE_DISCONNECT));
631 vcl_evt (VCL_EVT_UNBIND, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500632 vppcom_send_unbind_sock (vpp_handle);
633
Florin Coras070453d2018-08-24 17:04:27 -0700634 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400635}
636
Florin Coras697faea2018-06-27 17:10:49 -0700637static int
Florin Corasab2f6db2018-08-31 14:31:41 -0700638vppcom_session_disconnect (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400639{
Florin Coras134a9962018-08-28 11:32:04 -0700640 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -0700641 svm_msg_q_t *vpp_evt_q;
Florin Coras7e12d942018-06-27 14:32:43 -0700642 vcl_session_t *session;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500643 session_state_t state;
Florin Coras99368312018-08-02 10:45:44 -0700644 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400645
Florin Corasab2f6db2018-08-31 14:31:41 -0700646 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras21795132018-09-09 09:40:51 -0700647 if (!session)
648 return VPPCOM_EBADFD;
649
Dave Wallace4878cbe2017-11-21 03:45:09 -0500650 vpp_handle = session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -0700651 state = session->session_state;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500652
Florin Coras0d427d82018-06-27 03:24:07 -0700653 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
Florin Corasab2f6db2018-08-31 14:31:41 -0700654 vpp_handle, session_handle, state, vppcom_session_state_str (state));
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400655
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800656 if (PREDICT_FALSE (state & STATE_LISTEN))
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400657 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500658 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500659 "Cannot disconnect a listen socket!",
Florin Corasab2f6db2018-08-31 14:31:41 -0700660 getpid (), vpp_handle, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700661 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500662 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400663
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800664 if (state & STATE_CLOSE_ON_EMPTY)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500665 {
Florin Coras134a9962018-08-28 11:32:04 -0700666 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
Florin Coras47c40e22018-11-26 17:01:36 -0800667 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index,
Florin Coras99368312018-08-02 10:45:44 -0700668 vpp_handle, 0);
Florin Coras0d427d82018-06-27 03:24:07 -0700669 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
Florin Corasab2f6db2018-08-31 14:31:41 -0700670 "REPLY...", getpid (), vpp_handle, session_handle);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400671 }
672 else
Dave Wallace227867f2017-11-13 21:21:53 -0500673 {
Florin Coras0d427d82018-06-27 03:24:07 -0700674 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
Florin Corasab2f6db2018-08-31 14:31:41 -0700675 getpid (), vpp_handle, session_handle);
676 vppcom_send_disconnect_session (vpp_handle);
Dave Wallace227867f2017-11-13 21:21:53 -0500677 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400678
Florin Coras070453d2018-08-24 17:04:27 -0700679 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400680}
681
Florin Coras053a0e42018-11-13 15:52:38 -0800682static void
683vcl_cleanup_bapi (void)
684{
Florin Coras47c40e22018-11-26 17:01:36 -0800685 socket_client_main_t *scm = &socket_client_main;
Florin Coras053a0e42018-11-13 15:52:38 -0800686 api_main_t *am = &api_main;
687
688 am->my_client_index = ~0;
689 am->my_registration = 0;
690 am->vl_input_queue = 0;
691 am->msg_index_by_name_and_crc = 0;
Florin Coras47c40e22018-11-26 17:01:36 -0800692 scm->socket_fd = 0;
Florin Coras053a0e42018-11-13 15:52:38 -0800693
694 vl_client_api_unmap ();
695}
696
697void
698vcl_app_fork_child_handler (void)
699{
700 u8 *child_name;
Florin Coras47c40e22018-11-26 17:01:36 -0800701 int rv, parent_wrk;
Florin Coras053a0e42018-11-13 15:52:38 -0800702
Florin Coras940f78f2018-11-30 12:11:20 -0800703 parent_wrk = vcl_get_worker_index ();
704 VDBG (0, "initializing forked child with parent wrk %u", parent_wrk);
Florin Coras053a0e42018-11-13 15:52:38 -0800705
Florin Coras47c40e22018-11-26 17:01:36 -0800706 /*
707 * Allocate worker
708 */
Florin Coras47c40e22018-11-26 17:01:36 -0800709 vcl_set_worker_index (~0);
710 if (!vcl_worker_alloc_and_init ())
711 VERR ("couldn't allocate new worker");
712
713 /*
714 * Attach to binary api
715 */
716 child_name = format (0, "%v-child-%u%c", vcm->app_name, getpid (), 0);
Florin Coras053a0e42018-11-13 15:52:38 -0800717 vcl_cleanup_bapi ();
718 vppcom_api_hookup ();
719 vcm->app_state = STATE_APP_START;
720 rv = vppcom_connect_to_vpp ((char *) child_name);
721 vec_free (child_name);
722 if (rv)
723 {
724 VERR ("couldn't connect to VPP!");
725 return;
726 }
727
Florin Coras47c40e22018-11-26 17:01:36 -0800728 /*
729 * Register worker with vpp and share sessions
730 */
731 vcl_worker_register_with_vpp ();
732 vcl_worker_share_sessions (parent_wrk);
733
Florin Coras053a0e42018-11-13 15:52:38 -0800734 VDBG (0, "forked child main worker initialized");
Florin Coras47c40e22018-11-26 17:01:36 -0800735 vcm->forking = 0;
736}
737
738void
739vcl_app_fork_parent_handler (void)
740{
741 vcm->forking = 1;
742
743 while (vcm->forking)
744 ;
Florin Coras053a0e42018-11-13 15:52:38 -0800745}
746
Florin Coras940f78f2018-11-30 12:11:20 -0800747/**
748 * Handle app exit
749 *
750 * Notify vpp of the disconnect and mark the worker as free. If we're the
751 * last worker, do a full cleanup otherwise, since we're probably a forked
752 * child, avoid syscalls as much as possible. We might've lost privileges.
753 */
754void
755vppcom_app_exit (void)
756{
757 if (!pool_elts (vcm->workers))
758 return;
759
760 vcl_worker_cleanup (1 /* notify vpp */ );
761 vcl_elog_stop (vcm);
762 if (vec_len (vcm->workers) == 1)
763 vl_client_disconnect_from_vlib ();
764 else
765 vl_client_send_disconnect ();
766}
767
Dave Wallace543852a2017-08-03 02:11:34 -0400768/*
769 * VPPCOM Public API functions
770 */
771int
772vppcom_app_create (char *app_name)
773{
Dave Wallace543852a2017-08-03 02:11:34 -0400774 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -0400775 int rv;
776
Florin Coras47c40e22018-11-26 17:01:36 -0800777 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -0400778 {
Florin Coras47c40e22018-11-26 17:01:36 -0800779 clib_warning ("already initialized");
780 return -1;
Dave Wallace543852a2017-08-03 02:11:34 -0400781 }
782
Florin Coras47c40e22018-11-26 17:01:36 -0800783 vcm->is_init = 1;
784 vppcom_cfg (&vcm->cfg);
785 vcl_cfg = &vcm->cfg;
786
787 vcm->main_cpu = pthread_self ();
788 vcm->main_pid = getpid ();
789 vcm->app_name = format (0, "%s", app_name);
790 vppcom_init_error_string_table ();
Florin Corasadc74d72018-12-02 13:36:00 -0800791 svm_fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
Florin Coras47c40e22018-11-26 17:01:36 -0800792 20 /* timeout in secs */ );
793 pool_alloc (vcm->workers, vcl_cfg->max_workers);
794 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -0800795 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras47c40e22018-11-26 17:01:36 -0800796 pthread_atfork (NULL, vcl_app_fork_parent_handler,
797 vcl_app_fork_child_handler);
Florin Coras940f78f2018-11-30 12:11:20 -0800798 atexit (vppcom_app_exit);
Florin Coras47c40e22018-11-26 17:01:36 -0800799
800 /* Allocate default worker */
801 vcl_worker_alloc_and_init ();
802
803 /* API hookup and connect to VPP */
804 vppcom_api_hookup ();
805 vcl_elog_init (vcm);
806 vcm->app_state = STATE_APP_START;
807 rv = vppcom_connect_to_vpp (app_name);
808 if (rv)
Dave Wallace543852a2017-08-03 02:11:34 -0400809 {
Florin Coras47c40e22018-11-26 17:01:36 -0800810 VERR ("couldn't connect to VPP!");
811 return rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400812 }
Florin Coras47c40e22018-11-26 17:01:36 -0800813 VDBG (0, "sending session enable");
814 rv = vppcom_app_session_enable ();
815 if (rv)
816 {
817 VERR ("vppcom_app_session_enable() failed!");
818 return rv;
819 }
820
821 VDBG (0, "sending app attach");
822 rv = vppcom_app_attach ();
823 if (rv)
824 {
825 VERR ("vppcom_app_attach() failed!");
826 return rv;
827 }
828
829 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
830 vcm->workers[0].my_client_index, vcm->workers[0].my_client_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400831
832 return VPPCOM_OK;
833}
834
835void
836vppcom_app_destroy (void)
837{
Dave Wallace543852a2017-08-03 02:11:34 -0400838 int rv;
Dave Wallacede910062018-03-20 09:22:13 -0400839 f64 orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -0400840
Florin Coras940f78f2018-11-30 12:11:20 -0800841 if (!pool_elts (vcm->workers))
842 return;
843
Florin Coras0d427d82018-06-27 03:24:07 -0700844 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800845
Florin Coras940f78f2018-11-30 12:11:20 -0800846 if (pool_elts (vcm->workers) == 1)
Florin Coras47c40e22018-11-26 17:01:36 -0800847 {
848 vppcom_app_send_detach ();
849 orig_app_timeout = vcm->cfg.app_timeout;
850 vcm->cfg.app_timeout = 2.0;
851 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
852 vcm->cfg.app_timeout = orig_app_timeout;
853 if (PREDICT_FALSE (rv))
854 VDBG (0, "application detach timed out! returning %d (%s)", rv,
855 vppcom_retval_str (rv));
Florin Coras940f78f2018-11-30 12:11:20 -0800856 vec_free (vcm->app_name);
857 vcl_worker_cleanup (0 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -0800858 }
859 else
860 {
Florin Coras940f78f2018-11-30 12:11:20 -0800861 vcl_worker_cleanup (1 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -0800862 }
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800863
Florin Coras0d427d82018-06-27 03:24:07 -0700864 vcl_elog_stop (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -0400865 vl_client_disconnect_from_vlib ();
Dave Wallace543852a2017-08-03 02:11:34 -0400866}
867
868int
Dave Wallacec04cbf12018-02-07 18:14:02 -0500869vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -0400870{
Florin Coras134a9962018-08-28 11:32:04 -0700871 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -0700872 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -0400873
Florin Coras134a9962018-08-28 11:32:04 -0700874 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -0400875
Florin Coras7e12d942018-06-27 14:32:43 -0700876 session->session_type = proto;
877 session->session_state = STATE_START;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500878 session->vpp_handle = ~0;
Florin Coras460dce62018-07-27 05:45:06 -0700879 session->is_dgram = proto == VPPCOM_PROTO_UDP;
Dave Wallace543852a2017-08-03 02:11:34 -0400880
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800881 if (is_nonblocking)
882 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -0400883
Florin Coras7e12d942018-06-27 14:32:43 -0700884 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
885 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800886
Florin Coras053a0e42018-11-13 15:52:38 -0800887 VDBG (0, "created sid %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800888
Florin Coras134a9962018-08-28 11:32:04 -0700889 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -0400890}
891
892int
Florin Coras134a9962018-08-28 11:32:04 -0700893vppcom_session_close (uint32_t session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400894{
Florin Coras134a9962018-08-28 11:32:04 -0700895 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras47c40e22018-11-26 17:01:36 -0800896 u8 is_vep, do_disconnect = 1;
Florin Coras7e12d942018-06-27 14:32:43 -0700897 vcl_session_t *session = 0;
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400898 session_state_t state;
Florin Coras134a9962018-08-28 11:32:04 -0700899 u32 next_sh, vep_sh;
900 int rv = VPPCOM_OK;
901 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400902
Florin Coras134a9962018-08-28 11:32:04 -0700903 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700904 if (!session)
905 return VPPCOM_EBADFD;
906
Florin Coras47c40e22018-11-26 17:01:36 -0800907 if (session->shared_index != ~0)
908 do_disconnect = vcl_worker_unshare_session (wrk, session);
909
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400910 is_vep = session->is_vep;
Florin Coras134a9962018-08-28 11:32:04 -0700911 next_sh = session->vep.next_sh;
912 vep_sh = session->vep.vep_sh;
Florin Coras7e12d942018-06-27 14:32:43 -0700913 state = session->session_state;
Dave Wallaceee45d412017-11-24 21:44:06 -0500914 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400915
Florin Coras47c40e22018-11-26 17:01:36 -0800916 VDBG (0, "Closing session handle %u vpp handle %u", session_handle,
917 vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -0400918
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400919 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -0400920 {
Florin Coras134a9962018-08-28 11:32:04 -0700921 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -0400922 {
Florin Coras134a9962018-08-28 11:32:04 -0700923 rv = vppcom_epoll_ctl (session_handle, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -0700924 if (PREDICT_FALSE (rv < 0))
Florin Coras47c40e22018-11-26 17:01:36 -0800925 VDBG (0, "vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL vep_idx %u"
926 " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv,
927 vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400928
Florin Coras134a9962018-08-28 11:32:04 -0700929 next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -0400930 }
931 }
932 else
933 {
Florin Coras47c40e22018-11-26 17:01:36 -0800934 if (session->is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400935 {
Florin Coras134a9962018-08-28 11:32:04 -0700936 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, session_handle, 0);
Florin Coras0d427d82018-06-27 03:24:07 -0700937 if (rv < 0)
Florin Coras47c40e22018-11-26 17:01:36 -0800938 VDBG (0, "vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL vep_idx %u "
939 "failed! rv %d (%s)", vpp_handle, session_handle, vep_sh,
940 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400941 }
942
Florin Coras47c40e22018-11-26 17:01:36 -0800943 if (!do_disconnect)
944 goto cleanup;
945
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800946 if (state & STATE_LISTEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400947 {
Florin Coras134a9962018-08-28 11:32:04 -0700948 rv = vppcom_session_unbind (session_handle);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800949 if (PREDICT_FALSE (rv < 0))
Florin Coras47c40e22018-11-26 17:01:36 -0800950 VDBG (0, "vpp handle 0x%llx, sid %u: listener unbind failed! "
951 "rv %d (%s)", vpp_handle, session_handle, rv,
952 vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400953 }
Florin Coras070453d2018-08-24 17:04:27 -0700954 else if (state & STATE_OPEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -0400955 {
Florin Coras134a9962018-08-28 11:32:04 -0700956 rv = vppcom_session_disconnect (session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500957 if (PREDICT_FALSE (rv < 0))
Dave Wallace048b1d62018-01-03 22:24:41 -0500958 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500959 "session disconnect failed! rv %d (%s)",
Florin Coras134a9962018-08-28 11:32:04 -0700960 getpid (), vpp_handle, session_handle,
Dave Wallaceee45d412017-11-24 21:44:06 -0500961 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -0400962 }
Dave Wallace19481612017-09-15 18:47:44 -0400963 }
Dave Wallace4878cbe2017-11-21 03:45:09 -0500964
Florin Coras47c40e22018-11-26 17:01:36 -0800965cleanup:
966
Florin Coras99368312018-08-02 10:45:44 -0700967 if (vcl_session_is_ct (session))
968 {
969 vcl_cut_through_registration_t *ctr;
970 uword mq_addr;
971
972 mq_addr = pointer_to_uword (session->our_evt_q);
Florin Coras134a9962018-08-28 11:32:04 -0700973 ctr = vcl_ct_registration_lock_and_lookup (wrk, mq_addr);
Florin Coras99368312018-08-02 10:45:44 -0700974 ASSERT (ctr);
975 if (ctr->epoll_evt_conn_index != ~0)
Florin Coras134a9962018-08-28 11:32:04 -0700976 vcl_mq_epoll_del_evfd (wrk, ctr->epoll_evt_conn_index);
Florin Coras99368312018-08-02 10:45:44 -0700977 VDBG (0, "Removing ct registration %u",
Florin Coras134a9962018-08-28 11:32:04 -0700978 vcl_ct_registration_index (wrk, ctr));
979 vcl_ct_registration_del (wrk, ctr);
980 vcl_ct_registration_lookup_del (wrk, mq_addr);
981 vcl_ct_registration_unlock (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700982 }
Florin Coras54693d22018-07-17 10:46:29 -0700983
Dave Wallaceee45d412017-11-24 21:44:06 -0500984 if (vpp_handle != ~0)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500985 {
Florin Coras134a9962018-08-28 11:32:04 -0700986 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500987 }
Florin Coras134a9962018-08-28 11:32:04 -0700988 vcl_session_free (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500989
Florin Coras47c40e22018-11-26 17:01:36 -0800990 VDBG (0, "session handle %u vpp handle %u removed", session_handle,
991 vpp_handle);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800992
Florin Coras0d427d82018-06-27 03:24:07 -0700993 vcl_evt (VCL_EVT_CLOSE, session, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -0800994
Dave Wallace543852a2017-08-03 02:11:34 -0400995 return rv;
996}
997
998int
Florin Coras134a9962018-08-28 11:32:04 -0700999vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001000{
Florin Coras134a9962018-08-28 11:32:04 -07001001 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001002 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001003
1004 if (!ep || !ep->ip)
1005 return VPPCOM_EINVAL;
1006
Florin Coras134a9962018-08-28 11:32:04 -07001007 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001008 if (!session)
1009 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001010
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001011 if (session->is_vep)
1012 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001013 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
Florin Coras134a9962018-08-28 11:32:04 -07001014 "bind to an epoll session!", getpid (), session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001015 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001016 }
1017
Florin Coras7e12d942018-06-27 14:32:43 -07001018 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001019 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001020 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1021 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001022 else
Dave Barach178cf492018-11-13 16:34:13 -05001023 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1024 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001025 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001026
Florin Coras0d427d82018-06-27 03:24:07 -07001027 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
Florin Coras134a9962018-08-28 11:32:04 -07001028 "proto %s", getpid (), session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001029 session->transport.is_ip4 ? "IPv4" : "IPv6",
1030 format_ip46_address, &session->transport.lcl_ip,
1031 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1032 clib_net_to_host_u16 (session->transport.lcl_port),
1033 session->session_type ? "UDP" : "TCP");
Florin Coras0d427d82018-06-27 03:24:07 -07001034 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001035
1036 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001037 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001038
Florin Coras070453d2018-08-24 17:04:27 -07001039 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001040}
1041
1042int
Florin Coras134a9962018-08-28 11:32:04 -07001043vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001044{
Florin Coras134a9962018-08-28 11:32:04 -07001045 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001046 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001047 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001048 int rv;
1049
Florin Coras134a9962018-08-28 11:32:04 -07001050 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras070453d2018-08-24 17:04:27 -07001051 if (!listen_session)
1052 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001053
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001054 if (q_len == 0 || q_len == ~0)
1055 q_len = vcm->cfg.listen_queue_size;
1056
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001057 if (listen_session->is_vep)
1058 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001059 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
Florin Coras134a9962018-08-28 11:32:04 -07001060 "epoll session!", getpid (), listen_sh);
Florin Coras070453d2018-08-24 17:04:27 -07001061 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001062 }
1063
Dave Wallaceee45d412017-11-24 21:44:06 -05001064 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -07001065 if (listen_session->session_state & STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001066 {
Florin Coras0d427d82018-06-27 03:24:07 -07001067 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: already in listen state!",
Florin Coras134a9962018-08-28 11:32:04 -07001068 getpid (), listen_vpp_handle, listen_sh);
Florin Coras070453d2018-08-24 17:04:27 -07001069 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001070 }
1071
Florin Coras0d427d82018-06-27 03:24:07 -07001072 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: sending VPP bind+listen "
Florin Coras134a9962018-08-28 11:32:04 -07001073 "request...", getpid (), listen_vpp_handle, listen_sh);
Dave Wallace543852a2017-08-03 02:11:34 -04001074
Florin Coras070453d2018-08-24 17:04:27 -07001075 /*
1076 * Send listen request to vpp and wait for reply
1077 */
Florin Coras134a9962018-08-28 11:32:04 -07001078 vppcom_send_bind_sock (listen_session);
1079 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1080 STATE_LISTEN,
1081 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001082
Florin Coras070453d2018-08-24 17:04:27 -07001083 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001084 {
Florin Coras134a9962018-08-28 11:32:04 -07001085 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras0d427d82018-06-27 03:24:07 -07001086 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
1087 "returning %d (%s)", getpid (), listen_session->vpp_handle,
Florin Coras134a9962018-08-28 11:32:04 -07001088 listen_sh, rv, vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001089 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001090 }
1091
Florin Coras070453d2018-08-24 17:04:27 -07001092 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001093}
1094
Florin Coras134a9962018-08-28 11:32:04 -07001095static int
1096validate_args_session_accept_ (vcl_worker_t * wrk,
1097 vcl_session_t * listen_session)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001098{
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001099 /* Input validation - expects spinlock on sessions_lockp */
1100 if (listen_session->is_vep)
1101 {
1102 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
Florin Coras134a9962018-08-28 11:32:04 -07001103 "epoll session!", getpid (),
1104 listen_session->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001105 return VPPCOM_EBADFD;
1106 }
1107
Florin Coras7e12d942018-06-27 14:32:43 -07001108 if (listen_session->session_state != STATE_LISTEN)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001109 {
1110 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1111 "not in listen state! state 0x%x (%s)", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07001112 listen_session->vpp_handle, listen_session->session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07001113 listen_session->session_state,
1114 vppcom_session_state_str (listen_session->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001115 return VPPCOM_EBADFD;
1116 }
1117 return VPPCOM_OK;
1118}
1119
1120int
Florin Coras134a9962018-08-28 11:32:04 -07001121vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001122 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001123{
Florin Coras134a9962018-08-28 11:32:04 -07001124 u32 client_session_index = ~0, listen_session_index;
1125 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001126 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001127 vcl_session_t *listen_session = 0;
1128 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001129 svm_msg_q_t *vpp_evt_q;
1130 vcl_session_msg_t *evt;
Dave Wallaceee45d412017-11-24 21:44:06 -05001131 u64 listen_vpp_handle;
Florin Coras54693d22018-07-17 10:46:29 -07001132 svm_msg_q_msg_t msg;
1133 session_event_t *e;
1134 u8 is_nonblocking;
1135 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001136
Florin Coras134a9962018-08-28 11:32:04 -07001137 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001138 if (!listen_session)
1139 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001140
Florin Coras134a9962018-08-28 11:32:04 -07001141 listen_session_index = listen_session->session_index;
1142 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001143 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001144
Florin Coras54693d22018-07-17 10:46:29 -07001145 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001146 {
Florin Coras54693d22018-07-17 10:46:29 -07001147 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
1148 accepted_msg = evt->accepted_msg;
1149 goto handle;
1150 }
1151
1152 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1153 VCL_SESS_ATTR_NONBLOCK);
Florin Coras134a9962018-08-28 11:32:04 -07001154 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
Florin Coras54693d22018-07-17 10:46:29 -07001155 return VPPCOM_EAGAIN;
1156
1157 while (1)
1158 {
Florin Coras134a9962018-08-28 11:32:04 -07001159 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -07001160 return VPPCOM_EAGAIN;
1161
Florin Coras134a9962018-08-28 11:32:04 -07001162 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001163 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001164 {
Florin Coras54693d22018-07-17 10:46:29 -07001165 clib_warning ("discarded event: %u", e->event_type);
Florin Coras134a9962018-08-28 11:32:04 -07001166 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001167 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001168 }
Dave Barach178cf492018-11-13 16:34:13 -05001169 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
Florin Coras134a9962018-08-28 11:32:04 -07001170 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001171 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001172 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001173
Florin Coras54693d22018-07-17 10:46:29 -07001174handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001175
Florin Coras134a9962018-08-28 11:32:04 -07001176 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg);
1177 listen_session = vcl_session_get (wrk, listen_session_index);
1178 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001179
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001180 if (flags & O_NONBLOCK)
1181 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001182
Florin Coras54693d22018-07-17 10:46:29 -07001183 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras0d427d82018-06-27 03:24:07 -07001184 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
1185 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
Florin Coras134a9962018-08-28 11:32:04 -07001186 getpid (), listen_vpp_handle, listen_session_handle,
Florin Coras0d427d82018-06-27 03:24:07 -07001187 client_session->vpp_handle, client_session_index,
1188 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1189 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001190
Dave Wallace048b1d62018-01-03 22:24:41 -05001191 if (ep)
1192 {
Florin Coras7e12d942018-06-27 14:32:43 -07001193 ep->is_ip4 = client_session->transport.is_ip4;
1194 ep->port = client_session->transport.rmt_port;
1195 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001196 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1197 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001198 else
Dave Barach178cf492018-11-13 16:34:13 -05001199 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1200 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001201 }
Dave Wallace60caa062017-11-10 17:07:13 -05001202
Florin Coras54693d22018-07-17 10:46:29 -07001203 if (accepted_msg.server_event_queue_address)
1204 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1205 svm_msg_q_t *);
1206 else
1207 vpp_evt_q = client_session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -07001208
Florin Coras54693d22018-07-17 10:46:29 -07001209 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1210 client_session->vpp_handle, 0);
Dave Wallace60caa062017-11-10 17:07:13 -05001211
Florin Coras54693d22018-07-17 10:46:29 -07001212 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle 0x%llx, "
1213 "sid %u connection from peer %s address %U port %u to local %s "
1214 "address %U port %u", getpid (), listen_vpp_handle,
Florin Coras134a9962018-08-28 11:32:04 -07001215 listen_session_handle, client_session->vpp_handle,
Florin Coras0d427d82018-06-27 03:24:07 -07001216 client_session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07001217 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1218 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001219 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001220 clib_net_to_host_u16 (client_session->transport.rmt_port),
1221 client_session->transport.is_ip4 ? "IPv4" : "IPv6",
1222 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001223 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001224 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001225 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1226 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001227
Florin Corasab2f6db2018-08-31 14:31:41 -07001228 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001229}
1230
1231int
Florin Coras134a9962018-08-28 11:32:04 -07001232vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001233{
Florin Coras134a9962018-08-28 11:32:04 -07001234 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001235 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001236 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001237 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001238
Florin Coras134a9962018-08-28 11:32:04 -07001239 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001240 if (!session)
1241 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001242 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001243
1244 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001245 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001246 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
Florin Coras134a9962018-08-28 11:32:04 -07001247 "connect on an epoll session!", getpid (),
1248 session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001249 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001250 }
1251
Florin Coras7e12d942018-06-27 14:32:43 -07001252 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
Dave Wallace543852a2017-08-03 02:11:34 -04001253 {
Florin Coras0d427d82018-06-27 03:24:07 -07001254 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
1255 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras134a9962018-08-28 11:32:04 -07001256 getpid (), session->vpp_handle, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001257 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001258 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001259 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001260 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001261 clib_net_to_host_u16 (session->transport.rmt_port),
1262 session->session_type ? "UDP" : "TCP", session->session_state,
1263 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001264 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001265 }
1266
Florin Coras7e12d942018-06-27 14:32:43 -07001267 session->transport.is_ip4 = server_ep->is_ip4;
1268 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001269 clib_memcpy_fast (&session->transport.rmt_ip.ip4, server_ep->ip,
1270 sizeof (ip4_address_t));
Dave Wallaced239f8d2018-06-19 13:37:30 -04001271 else
Dave Barach178cf492018-11-13 16:34:13 -05001272 clib_memcpy_fast (&session->transport.rmt_ip.ip6, server_ep->ip,
1273 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001274 session->transport.rmt_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04001275
Florin Coras0d427d82018-06-27 03:24:07 -07001276 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
1277 "port %d proto %s",
Florin Coras134a9962018-08-28 11:32:04 -07001278 getpid (), session->vpp_handle, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001279 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001280 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001281 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001282 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001283 clib_net_to_host_u16 (session->transport.rmt_port),
1284 session->session_type ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -04001285
Florin Coras070453d2018-08-24 17:04:27 -07001286 /*
1287 * Send connect request and wait for reply from vpp
1288 */
Florin Coras134a9962018-08-28 11:32:04 -07001289 vppcom_send_connect_sock (session);
Florin Coras070453d2018-08-24 17:04:27 -07001290 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1291 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001292
Florin Coras134a9962018-08-28 11:32:04 -07001293 session = vcl_session_get (wrk, session_index);
Dave Wallace7876d392017-10-19 03:53:57 -04001294
Florin Coras070453d2018-08-24 17:04:27 -07001295 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001296 {
Dave Wallaceee45d412017-11-24 21:44:06 -05001297 if (VPPCOM_DEBUG > 0)
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001298 {
1299 if (session)
Florin Coras0d427d82018-06-27 03:24:07 -07001300 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
Florin Coras134a9962018-08-28 11:32:04 -07001301 "failed! returning %d (%s)", getpid (),
1302 session->vpp_handle, session_handle, rv,
1303 vppcom_retval_str (rv));
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001304 else
1305 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1306 "returning %d (%s)", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07001307 session_handle, rv, vppcom_retval_str (rv));
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001308 }
Dave Wallaceee45d412017-11-24 21:44:06 -05001309 }
Florin Coras0d427d82018-06-27 03:24:07 -07001310 else
1311 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
Florin Coras134a9962018-08-28 11:32:04 -07001312 getpid (), session->vpp_handle, session_handle);
Dave Wallaceee45d412017-11-24 21:44:06 -05001313
Dave Wallace4878cbe2017-11-21 03:45:09 -05001314 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001315}
1316
Florin Coras54693d22018-07-17 10:46:29 -07001317static u8
1318vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1319{
1320 if (!is_ct)
1321 return (e->event_type == FIFO_EVENT_APP_RX
1322 && e->fifo->client_session_index == sid);
1323 else
1324 return (e->event_type == SESSION_IO_EVT_CT_TX);
1325}
1326
Florin Coras460dce62018-07-27 05:45:06 -07001327static inline u8
1328vcl_session_is_readable (vcl_session_t * s)
1329{
1330 return ((s->session_state & STATE_OPEN)
1331 || (s->session_state == STATE_LISTEN
1332 && s->session_type == VPPCOM_PROTO_UDP));
1333}
1334
Steven58f464e2017-10-25 12:33:12 -07001335static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001336vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001337 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001338{
Florin Coras134a9962018-08-28 11:32:04 -07001339 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001340 int n_read = 0, rv, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001341 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001342 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001343 svm_msg_q_msg_t msg;
1344 session_event_t *e;
1345 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001346 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001347
Florin Coras070453d2018-08-24 17:04:27 -07001348 if (PREDICT_FALSE (!buf))
1349 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001350
Florin Coras134a9962018-08-28 11:32:04 -07001351 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras2cba8532018-09-11 16:33:36 -07001352 if (PREDICT_FALSE (!s || s->is_vep))
Florin Coras070453d2018-08-24 17:04:27 -07001353 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001354
Florin Coras460dce62018-07-27 05:45:06 -07001355 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001356 {
Florin Coras460dce62018-07-27 05:45:06 -07001357 session_state_t state = s->session_state;
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001358 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001359
Florin Coras0d427d82018-06-27 03:24:07 -07001360 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
1361 "state 0x%x (%s), returning %d (%s)",
Florin Coras134a9962018-08-28 11:32:04 -07001362 getpid (), s->vpp_handle, session_handle, state,
Florin Coras0d427d82018-06-27 03:24:07 -07001363 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001364 return rv;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001365 }
1366
Florin Coras2cba8532018-09-11 16:33:36 -07001367 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001368 is_ct = vcl_session_is_ct (s);
1369 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
Florin Coras2cba8532018-09-11 16:33:36 -07001370 rx_fifo = s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001371 s->has_rx_evt = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001372
Florin Coras54693d22018-07-17 10:46:29 -07001373 if (svm_fifo_is_empty (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001374 {
Florin Coras54693d22018-07-17 10:46:29 -07001375 if (is_nonblocking)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001376 {
Florin Coras41c9e042018-09-11 00:10:41 -07001377 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001378 return VPPCOM_EWOULDBLOCK;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001379 }
Florin Coras41c9e042018-09-11 00:10:41 -07001380 while (svm_fifo_is_empty (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001381 {
Florin Coras41c9e042018-09-11 00:10:41 -07001382 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001383 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001384 if (svm_msg_q_is_empty (mq))
1385 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001386
Florin Coras54693d22018-07-17 10:46:29 -07001387 svm_msg_q_sub_w_lock (mq, &msg);
1388 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001389 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001390 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras54693d22018-07-17 10:46:29 -07001391 {
Florin Coras86f04502018-09-12 16:08:01 -07001392 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001393 svm_msg_q_free_msg (mq, &msg);
1394 continue;
1395 }
Florin Coras54693d22018-07-17 10:46:29 -07001396 svm_msg_q_free_msg (mq, &msg);
Florin Coras41c9e042018-09-11 00:10:41 -07001397
Florin Coras60f1fc12018-08-16 14:57:31 -07001398 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1399 return 0;
Florin Coras54693d22018-07-17 10:46:29 -07001400 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001401 }
Florin Coras54693d22018-07-17 10:46:29 -07001402
Florin Coras460dce62018-07-27 05:45:06 -07001403 if (s->is_dgram)
Florin Coras99368312018-08-02 10:45:44 -07001404 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001405 else
Florin Coras99368312018-08-02 10:45:44 -07001406 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
Florin Coras54693d22018-07-17 10:46:29 -07001407
Florin Coras41c9e042018-09-11 00:10:41 -07001408 if (svm_fifo_is_empty (rx_fifo))
1409 svm_fifo_unset_event (rx_fifo);
1410
Florin Coras58c101a2018-10-06 13:49:16 -07001411 if (is_ct && svm_fifo_want_tx_evt (rx_fifo))
Florin Coras99368312018-08-02 10:45:44 -07001412 {
Florin Coras58c101a2018-10-06 13:49:16 -07001413 svm_fifo_set_want_tx_evt (s->rx_fifo, 0);
1414 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo, SESSION_IO_EVT_CT_RX,
1415 SVM_Q_WAIT);
Florin Coras99368312018-08-02 10:45:44 -07001416 }
Florin Coras54693d22018-07-17 10:46:29 -07001417
Florin Coras2cba8532018-09-11 16:33:36 -07001418 VDBG (2, "VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes from (%p)",
1419 getpid (), s->vpp_handle, session_handle, n_read, rx_fifo);
1420
Florin Coras54693d22018-07-17 10:46:29 -07001421 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001422}
1423
Steven58f464e2017-10-25 12:33:12 -07001424int
Florin Coras134a9962018-08-28 11:32:04 -07001425vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001426{
Florin Coras134a9962018-08-28 11:32:04 -07001427 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001428}
1429
1430static int
Florin Coras134a9962018-08-28 11:32:04 -07001431vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001432{
Florin Coras134a9962018-08-28 11:32:04 -07001433 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001434}
1435
Florin Coras2cba8532018-09-11 16:33:36 -07001436int
1437vppcom_session_read_segments (uint32_t session_handle,
1438 vppcom_data_segments_t ds)
1439{
1440 vcl_worker_t *wrk = vcl_worker_get_current ();
1441 int n_read = 0, rv, is_nonblocking;
1442 vcl_session_t *s = 0;
1443 svm_fifo_t *rx_fifo;
1444 svm_msg_q_msg_t msg;
1445 session_event_t *e;
1446 svm_msg_q_t *mq;
1447 u8 is_ct;
1448
1449 s = vcl_session_get_w_handle (wrk, session_handle);
1450 if (PREDICT_FALSE (!s || s->is_vep))
1451 return VPPCOM_EBADFD;
1452
1453 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1454 {
1455 session_state_t state = s->session_state;
1456 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1457 return rv;
1458 }
1459
1460 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1461 is_ct = vcl_session_is_ct (s);
1462 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1463 rx_fifo = s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001464 s->has_rx_evt = 0;
Florin Coras2cba8532018-09-11 16:33:36 -07001465
1466 if (svm_fifo_is_empty (rx_fifo))
1467 {
1468 if (is_nonblocking)
1469 {
1470 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001471 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001472 }
1473 while (svm_fifo_is_empty (rx_fifo))
1474 {
1475 svm_fifo_unset_event (rx_fifo);
1476 svm_msg_q_lock (mq);
1477 if (svm_msg_q_is_empty (mq))
1478 svm_msg_q_wait (mq);
1479
1480 svm_msg_q_sub_w_lock (mq, &msg);
1481 e = svm_msg_q_msg_data (mq, &msg);
1482 svm_msg_q_unlock (mq);
1483 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
1484 {
Florin Coras86f04502018-09-12 16:08:01 -07001485 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001486 svm_msg_q_free_msg (mq, &msg);
1487 continue;
1488 }
1489 svm_msg_q_free_msg (mq, &msg);
1490
1491 if (PREDICT_FALSE (s->session_state == STATE_CLOSE_ON_EMPTY))
1492 return 0;
1493 }
1494 }
1495
1496 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_segment_t *) ds);
1497 svm_fifo_unset_event (rx_fifo);
1498
1499 if (is_ct && n_read + svm_fifo_max_dequeue (rx_fifo) == rx_fifo->nitems)
1500 {
1501 /* If the peer is not polling send notification */
1502 if (!svm_fifo_has_event (s->rx_fifo))
1503 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1504 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1505 }
1506
1507 return n_read;
1508}
1509
1510void
1511vppcom_session_free_segments (uint32_t session_handle,
1512 vppcom_data_segments_t ds)
1513{
1514 vcl_worker_t *wrk = vcl_worker_get_current ();
1515 vcl_session_t *s;
1516
1517 s = vcl_session_get_w_handle (wrk, session_handle);
1518 if (PREDICT_FALSE (!s || s->is_vep))
1519 return;
1520
1521 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_segment_t *) ds);
1522}
1523
Dave Wallace543852a2017-08-03 02:11:34 -04001524static inline int
Florin Coras54693d22018-07-17 10:46:29 -07001525vppcom_session_read_ready (vcl_session_t * session)
Dave Wallace543852a2017-08-03 02:11:34 -04001526{
Dave Wallace543852a2017-08-03 02:11:34 -04001527 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05001528 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001529 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001530 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
Florin Coras134a9962018-08-28 11:32:04 -07001531 "epoll session!", getpid (), session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -07001532 return VPPCOM_EBADFD;
1533 }
1534
1535 if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
1536 {
1537 session_state_t state = session->session_state;
1538 int rv;
1539
1540 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1541
1542 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
1543 " state 0x%x (%s), returning %d (%s)", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07001544 session->vpp_handle, session->session_index, state,
Florin Coras54693d22018-07-17 10:46:29 -07001545 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1546 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001547 }
Dave Wallace33e002b2017-09-06 01:20:02 -04001548
Florin Coras7e12d942018-06-27 14:32:43 -07001549 if (session->session_state & STATE_LISTEN)
Florin Coras54693d22018-07-17 10:46:29 -07001550 return clib_fifo_elts (session->accept_evts_fifo);
1551
1552 return svm_fifo_max_dequeue (session->rx_fifo);
1553}
1554
Florin Coras2cba8532018-09-11 16:33:36 -07001555int
1556vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1557{
1558 u32 first_copy = clib_min (ds[0].len, max_bytes);
Dave Barach178cf492018-11-13 16:34:13 -05001559 clib_memcpy_fast (buf, ds[0].data, first_copy);
Florin Coras2cba8532018-09-11 16:33:36 -07001560 if (first_copy < max_bytes)
1561 {
Dave Barach178cf492018-11-13 16:34:13 -05001562 clib_memcpy_fast (buf + first_copy, ds[1].data,
1563 clib_min (ds[1].len, max_bytes - first_copy));
Florin Coras2cba8532018-09-11 16:33:36 -07001564 }
1565 return 0;
1566}
1567
Florin Coras54693d22018-07-17 10:46:29 -07001568static u8
1569vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1570{
1571 if (!is_ct)
1572 return (e->event_type == FIFO_EVENT_APP_TX
1573 && e->fifo->client_session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04001574 else
Florin Coras54693d22018-07-17 10:46:29 -07001575 return (e->event_type == SESSION_IO_EVT_CT_RX);
Dave Wallace543852a2017-08-03 02:11:34 -04001576}
1577
1578int
Florin Coras134a9962018-08-28 11:32:04 -07001579vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
Dave Wallace543852a2017-08-03 02:11:34 -04001580{
Florin Coras134a9962018-08-28 11:32:04 -07001581 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001582 int rv, n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001583 vcl_session_t *s = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001584 svm_fifo_t *tx_fifo = 0;
Florin Coras460dce62018-07-27 05:45:06 -07001585 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07001586 svm_msg_q_msg_t msg;
1587 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001588 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07001589 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001590
Florin Coras070453d2018-08-24 17:04:27 -07001591 if (PREDICT_FALSE (!buf))
1592 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001593
Florin Coras134a9962018-08-28 11:32:04 -07001594 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001595 if (PREDICT_FALSE (!s))
1596 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001597
Florin Coras460dce62018-07-27 05:45:06 -07001598 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001599 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001600 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001601 "cannot write to an epoll session!",
Florin Coras134a9962018-08-28 11:32:04 -07001602 getpid (), s->vpp_handle, session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001603
Florin Coras070453d2018-08-24 17:04:27 -07001604 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001605 }
1606
Florin Coras0e88e852018-09-17 22:09:02 -07001607 if (PREDICT_FALSE (!(s->session_state & STATE_OPEN)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001608 {
Florin Coras460dce62018-07-27 05:45:06 -07001609 session_state_t state = s->session_state;
Florin Coras54693d22018-07-17 10:46:29 -07001610 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Florin Coras0d427d82018-06-27 03:24:07 -07001611 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
Florin Coras0e88e852018-09-17 22:09:02 -07001612 "state 0x%x (%s)", getpid (), s->vpp_handle, session_handle,
Florin Coras0d427d82018-06-27 03:24:07 -07001613 state, vppcom_session_state_str (state));
Florin Coras070453d2018-08-24 17:04:27 -07001614 return rv;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001615 }
1616
Florin Coras0e88e852018-09-17 22:09:02 -07001617 tx_fifo = s->tx_fifo;
1618 is_ct = vcl_session_is_ct (s);
1619 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1620 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
Florin Coras54693d22018-07-17 10:46:29 -07001621 if (svm_fifo_is_full (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04001622 {
Florin Coras54693d22018-07-17 10:46:29 -07001623 if (is_nonblocking)
1624 {
Florin Coras070453d2018-08-24 17:04:27 -07001625 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07001626 }
Florin Coras60f1fc12018-08-16 14:57:31 -07001627 while (svm_fifo_is_full (tx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001628 {
Florin Coras0e88e852018-09-17 22:09:02 -07001629 svm_fifo_set_want_tx_evt (tx_fifo, 1);
Florin Coras99368312018-08-02 10:45:44 -07001630 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07001631 if (svm_msg_q_is_empty (mq))
1632 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07001633
Florin Coras54693d22018-07-17 10:46:29 -07001634 svm_msg_q_sub_w_lock (mq, &msg);
1635 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001636 svm_msg_q_unlock (mq);
1637
Florin Coras0e88e852018-09-17 22:09:02 -07001638 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07001639 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001640 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001641 }
Dave Wallace543852a2017-08-03 02:11:34 -04001642 }
Dave Wallace543852a2017-08-03 02:11:34 -04001643
Florin Coras460dce62018-07-27 05:45:06 -07001644 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1645 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
1646 if (s->is_dgram)
1647 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1648 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1649 else
1650 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1651 SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001652
Florin Coras460dce62018-07-27 05:45:06 -07001653 ASSERT (n_write > 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001654
Florin Coras0e88e852018-09-17 22:09:02 -07001655 VDBG (2, "VCL<%d>: vpp handle 0x%llx, sid %u: wrote %d bytes", getpid (),
1656 s->vpp_handle, session_handle, n_write);
1657
Florin Coras54693d22018-07-17 10:46:29 -07001658 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04001659}
1660
Florin Coras99368312018-08-02 10:45:44 -07001661static vcl_session_t *
Florin Coras134a9962018-08-28 11:32:04 -07001662vcl_ct_session_get_from_fifo (vcl_worker_t * wrk, svm_fifo_t * f, u8 type)
Florin Coras99368312018-08-02 10:45:44 -07001663{
1664 vcl_session_t *s;
Florin Coras134a9962018-08-28 11:32:04 -07001665 s = vcl_session_get (wrk, f->client_session_index);
Florin Coras99368312018-08-02 10:45:44 -07001666 if (s)
1667 {
1668 /* rx fifo */
1669 if (type == 0 && s->rx_fifo == f)
1670 return s;
1671 /* tx fifo */
1672 if (type == 1 && s->tx_fifo == f)
1673 return s;
1674 }
Florin Coras134a9962018-08-28 11:32:04 -07001675 s = vcl_session_get (wrk, f->master_session_index);
Florin Coras99368312018-08-02 10:45:44 -07001676 if (s)
1677 {
1678 if (type == 0 && s->rx_fifo == f)
1679 return s;
1680 if (type == 1 && s->tx_fifo == f)
1681 return s;
1682 }
1683 return 0;
1684}
1685
Dave Wallace543852a2017-08-03 02:11:34 -04001686static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001687vppcom_session_write_ready (vcl_session_t * session)
Dave Wallace543852a2017-08-03 02:11:34 -04001688{
Dave Wallace543852a2017-08-03 02:11:34 -04001689 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05001690 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001691 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001692 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001693 "cannot write to an epoll session!",
Florin Coras134a9962018-08-28 11:32:04 -07001694 getpid (), session->vpp_handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -07001695 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001696 }
1697
Florin Coras7e12d942018-06-27 14:32:43 -07001698 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
Dave Wallace33e002b2017-09-06 01:20:02 -04001699 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001700 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001701 "cannot write to a listen session!",
Florin Coras134a9962018-08-28 11:32:04 -07001702 getpid (), session->vpp_handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -07001703 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001704 }
1705
Florin Coras54693d22018-07-17 10:46:29 -07001706 if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001707 {
Florin Coras7e12d942018-06-27 14:32:43 -07001708 session_state_t state = session->session_state;
Florin Coras54693d22018-07-17 10:46:29 -07001709 int rv;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001710
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001711 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace048b1d62018-01-03 22:24:41 -05001712 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001713 "session is not open! state 0x%x (%s), "
Dave Wallaceee45d412017-11-24 21:44:06 -05001714 "returning %d (%s)", getpid (), session->vpp_handle,
Florin Coras134a9962018-08-28 11:32:04 -07001715 session->session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05001716 state, vppcom_session_state_str (state),
1717 rv, vppcom_retval_str (rv));
Florin Coras54693d22018-07-17 10:46:29 -07001718 return rv;
Dave Wallace33e002b2017-09-06 01:20:02 -04001719 }
1720
Florin Coras0d427d82018-06-27 03:24:07 -07001721 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
Florin Coras134a9962018-08-28 11:32:04 -07001722 getpid (), session->vpp_handle, session->session_index,
1723 session->tx_fifo, svm_fifo_max_enqueue (session->tx_fifo));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001724
Florin Coras54693d22018-07-17 10:46:29 -07001725 return svm_fifo_max_enqueue (session->tx_fifo);
1726}
1727
Florin Coras99368312018-08-02 10:45:44 -07001728static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001729vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq)
Florin Coras99368312018-08-02 10:45:44 -07001730{
1731 svm_msg_q_msg_t *msg;
1732 u32 n_msgs;
1733 int i;
1734
1735 n_msgs = svm_msg_q_size (mq);
1736 for (i = 0; i < n_msgs; i++)
1737 {
Florin Coras134a9962018-08-28 11:32:04 -07001738 vec_add2 (wrk->mq_msg_vector, msg, 1);
Florin Coras99368312018-08-02 10:45:44 -07001739 svm_msg_q_sub_w_lock (mq, msg);
1740 }
1741 return n_msgs;
1742}
1743
Florin Coras6d4bb422018-09-04 22:07:27 -07001744#define vcl_fifo_rx_evt_valid_or_break(_fifo) \
1745if (PREDICT_FALSE (svm_fifo_is_empty (_fifo))) \
1746 { \
1747 svm_fifo_unset_event (_fifo); \
1748 if (svm_fifo_is_empty (_fifo)) \
Florin Coras41c9e042018-09-11 00:10:41 -07001749 break; \
Florin Coras6d4bb422018-09-04 22:07:27 -07001750 } \
1751
Florin Coras86f04502018-09-12 16:08:01 -07001752static void
1753vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1754 unsigned long n_bits, unsigned long *read_map,
1755 unsigned long *write_map,
1756 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07001757{
1758 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07001759 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07001760 session_accepted_msg_t *accepted_msg;
1761 vcl_session_msg_t *vcl_msg;
1762 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07001763 u64 handle;
1764 u32 sid;
1765
1766 switch (e->event_type)
1767 {
1768 case FIFO_EVENT_APP_RX:
1769 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1770 sid = e->fifo->client_session_index;
1771 session = vcl_session_get (wrk, sid);
1772 if (!session)
1773 break;
1774 if (sid < n_bits && read_map)
1775 {
1776 clib_bitmap_set_no_check (read_map, sid, 1);
1777 *bits_set += 1;
1778 }
1779 break;
1780 case FIFO_EVENT_APP_TX:
1781 sid = e->fifo->client_session_index;
1782 session = vcl_session_get (wrk, sid);
1783 if (!session)
1784 break;
1785 if (sid < n_bits && write_map)
1786 {
1787 clib_bitmap_set_no_check (write_map, sid, 1);
1788 *bits_set += 1;
1789 }
1790 break;
1791 case SESSION_IO_EVT_CT_TX:
1792 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1793 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
1794 if (!session)
1795 break;
1796 sid = session->session_index;
1797 if (sid < n_bits && read_map)
1798 {
1799 clib_bitmap_set_no_check (read_map, sid, 1);
1800 *bits_set += 1;
1801 }
1802 break;
1803 case SESSION_IO_EVT_CT_RX:
1804 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
1805 if (!session)
1806 break;
1807 sid = session->session_index;
1808 if (sid < n_bits && write_map)
1809 {
1810 clib_bitmap_set_no_check (write_map, sid, 1);
1811 *bits_set += 1;
1812 }
1813 break;
1814 case SESSION_CTRL_EVT_ACCEPTED:
1815 accepted_msg = (session_accepted_msg_t *) e->data;
1816 handle = accepted_msg->listener_handle;
1817 session = vcl_session_table_lookup_listener (wrk, handle);
1818 if (!session)
1819 {
1820 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
1821 "listener handle %llx", getpid (), handle);
1822 break;
1823 }
1824
1825 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
1826 vcl_msg->accepted_msg = *accepted_msg;
1827 sid = session->session_index;
1828 if (sid < n_bits && read_map)
1829 {
1830 clib_bitmap_set_no_check (read_map, sid, 1);
1831 *bits_set += 1;
1832 }
1833 break;
1834 case SESSION_CTRL_EVT_CONNECTED:
1835 connected_msg = (session_connected_msg_t *) e->data;
1836 vcl_session_connected_handler (wrk, connected_msg);
1837 break;
1838 case SESSION_CTRL_EVT_DISCONNECTED:
1839 disconnected_msg = (session_disconnected_msg_t *) e->data;
1840 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
1841 if (sid < n_bits && except_map)
1842 {
1843 clib_bitmap_set_no_check (except_map, sid, 1);
1844 *bits_set += 1;
1845 }
1846 break;
1847 case SESSION_CTRL_EVT_RESET:
1848 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1849 if (sid < n_bits && except_map)
1850 {
1851 clib_bitmap_set_no_check (except_map, sid, 1);
1852 *bits_set += 1;
1853 }
1854 break;
1855 default:
1856 clib_warning ("unhandled: %u", e->event_type);
1857 break;
1858 }
1859}
1860
1861static int
1862vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1863 unsigned long n_bits, unsigned long *read_map,
1864 unsigned long *write_map, unsigned long *except_map,
1865 double time_to_wait, u32 * bits_set)
1866{
Florin Coras99368312018-08-02 10:45:44 -07001867 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07001868 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07001869 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07001870
1871 svm_msg_q_lock (mq);
1872 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001873 {
Florin Coras54693d22018-07-17 10:46:29 -07001874 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001875 {
Florin Coras54693d22018-07-17 10:46:29 -07001876 svm_msg_q_unlock (mq);
1877 return 0;
1878 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001879
Florin Coras54693d22018-07-17 10:46:29 -07001880 if (!time_to_wait)
1881 {
1882 svm_msg_q_unlock (mq);
1883 return 0;
1884 }
1885 else if (time_to_wait < 0)
1886 {
1887 svm_msg_q_wait (mq);
1888 }
1889 else
1890 {
1891 if (svm_msg_q_timedwait (mq, time_to_wait))
1892 {
1893 svm_msg_q_unlock (mq);
1894 return 0;
1895 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001896 }
1897 }
Florin Coras134a9962018-08-28 11:32:04 -07001898 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07001899 svm_msg_q_unlock (mq);
1900
Florin Coras134a9962018-08-28 11:32:04 -07001901 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07001902 {
Florin Coras134a9962018-08-28 11:32:04 -07001903 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07001904 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07001905 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
1906 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07001907 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07001908 }
Florin Coras134a9962018-08-28 11:32:04 -07001909 vec_reset_length (wrk->mq_msg_vector);
Florin Coras54693d22018-07-17 10:46:29 -07001910 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04001911}
1912
Florin Coras99368312018-08-02 10:45:44 -07001913static int
Florin Coras134a9962018-08-28 11:32:04 -07001914vppcom_select_condvar (vcl_worker_t * wrk, unsigned long n_bits,
1915 unsigned long *read_map, unsigned long *write_map,
1916 unsigned long *except_map, double time_to_wait,
1917 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07001918{
1919 double total_wait = 0, wait_slice;
1920 vcl_cut_through_registration_t *cr;
1921
1922 time_to_wait = (time_to_wait == -1) ? 10e9 : time_to_wait;
Florin Coras134a9962018-08-28 11:32:04 -07001923 wait_slice = wrk->cut_through_registrations ? 10e-6 : time_to_wait;
Florin Coras99368312018-08-02 10:45:44 -07001924 do
1925 {
Florin Coras134a9962018-08-28 11:32:04 -07001926 vcl_ct_registration_lock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07001927 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07001928 pool_foreach (cr, wrk->cut_through_registrations, ({
1929 vcl_select_handle_mq (wrk, cr->mq, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07001930 0, bits_set);
1931 }));
1932 /* *INDENT-ON* */
Florin Coras134a9962018-08-28 11:32:04 -07001933 vcl_ct_registration_unlock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07001934
Florin Coras134a9962018-08-28 11:32:04 -07001935 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
1936 write_map, except_map, time_to_wait, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07001937 total_wait += wait_slice;
1938 if (*bits_set)
1939 return *bits_set;
1940 }
1941 while (total_wait < time_to_wait);
1942
1943 return 0;
1944}
1945
1946static int
Florin Coras134a9962018-08-28 11:32:04 -07001947vppcom_select_eventfd (vcl_worker_t * wrk, unsigned long n_bits,
1948 unsigned long *read_map, unsigned long *write_map,
1949 unsigned long *except_map, double time_to_wait,
1950 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07001951{
1952 vcl_mq_evt_conn_t *mqc;
1953 int __clib_unused n_read;
1954 int n_mq_evts, i;
1955 u64 buf;
1956
Florin Coras134a9962018-08-28 11:32:04 -07001957 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
1958 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
1959 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07001960 for (i = 0; i < n_mq_evts; i++)
1961 {
Florin Coras134a9962018-08-28 11:32:04 -07001962 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07001963 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07001964 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07001965 except_map, 0, bits_set);
1966 }
1967
1968 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1969}
1970
Dave Wallace543852a2017-08-03 02:11:34 -04001971int
1972vppcom_select (unsigned long n_bits, unsigned long *read_map,
1973 unsigned long *write_map, unsigned long *except_map,
1974 double time_to_wait)
1975{
Florin Coras54693d22018-07-17 10:46:29 -07001976 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001977 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001978 vcl_session_t *session = 0;
Florin Coras86f04502018-09-12 16:08:01 -07001979 int rv, i;
Dave Wallace543852a2017-08-03 02:11:34 -04001980
1981 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
1982
Dave Wallace7876d392017-10-19 03:53:57 -04001983 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001984 {
Florin Coras134a9962018-08-28 11:32:04 -07001985 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05001986 clib_memcpy_fast (wrk->rd_bitmap, read_map,
1987 vec_len (wrk->rd_bitmap) * sizeof (clib_bitmap_t));
Florin Coras134a9962018-08-28 11:32:04 -07001988 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04001989 }
Dave Wallace7876d392017-10-19 03:53:57 -04001990 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001991 {
Florin Coras134a9962018-08-28 11:32:04 -07001992 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05001993 clib_memcpy_fast (wrk->wr_bitmap, write_map,
1994 vec_len (wrk->wr_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001995 memset (write_map, 0,
Florin Coras134a9962018-08-28 11:32:04 -07001996 vec_len (wrk->wr_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04001997 }
Dave Wallace7876d392017-10-19 03:53:57 -04001998 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001999 {
Florin Coras134a9962018-08-28 11:32:04 -07002000 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002001 clib_memcpy_fast (wrk->ex_bitmap, except_map,
2002 vec_len (wrk->ex_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05002003 memset (except_map, 0,
Florin Coras134a9962018-08-28 11:32:04 -07002004 vec_len (wrk->ex_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04002005 }
2006
Florin Coras54693d22018-07-17 10:46:29 -07002007 if (!n_bits)
2008 return 0;
2009
2010 if (!write_map)
2011 goto check_rd;
2012
2013 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002014 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2015 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002016 {
Florin Coras47c40e22018-11-26 17:01:36 -08002017 if (except_map && sid < minbits)
2018 clib_bitmap_set_no_check (except_map, sid, 1);
2019 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002020 }
2021
2022 rv = svm_fifo_is_full (session->tx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07002023 if (!rv)
2024 {
2025 clib_bitmap_set_no_check (write_map, sid, 1);
2026 bits_set++;
2027 }
2028 }));
2029
2030check_rd:
2031 if (!read_map)
2032 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002033
Florin Coras134a9962018-08-28 11:32:04 -07002034 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2035 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002036 {
Florin Coras47c40e22018-11-26 17:01:36 -08002037 if (except_map && sid < minbits)
2038 clib_bitmap_set_no_check (except_map, sid, 1);
2039 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002040 }
2041
2042 rv = vppcom_session_read_ready (session);
Florin Coras54693d22018-07-17 10:46:29 -07002043 if (rv)
2044 {
2045 clib_bitmap_set_no_check (read_map, sid, 1);
2046 bits_set++;
2047 }
2048 }));
2049 /* *INDENT-ON* */
2050
2051check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002052
Florin Coras86f04502018-09-12 16:08:01 -07002053 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2054 {
2055 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2056 read_map, write_map, except_map, &bits_set);
2057 }
2058 vec_reset_length (wrk->unhandled_evts_vector);
2059
Florin Coras99368312018-08-02 10:45:44 -07002060 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002061 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002062 time_to_wait, &bits_set);
2063 else
Florin Coras134a9962018-08-28 11:32:04 -07002064 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002065 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002066
Dave Wallace543852a2017-08-03 02:11:34 -04002067 return (bits_set);
2068}
2069
Dave Wallacef7f809c2017-10-03 01:48:42 -04002070static inline void
Florin Coras134a9962018-08-28 11:32:04 -07002071vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002072{
Florin Coras7e12d942018-06-27 14:32:43 -07002073 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002074 vppcom_epoll_t *vep;
Dave Wallace498b3a52017-11-09 13:00:34 -05002075 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002076
Dave Wallace498b3a52017-11-09 13:00:34 -05002077 if (VPPCOM_DEBUG <= 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002078 return;
2079
2080 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Florin Coras134a9962018-08-28 11:32:04 -07002081 session = vcl_session_get (wrk, vep_idx);
Florin Coras070453d2018-08-24 17:04:27 -07002082 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002083 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002084 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
2085 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002086 goto done;
2087 }
2088 if (PREDICT_FALSE (!session->is_vep))
2089 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002090 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2091 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002092 goto done;
2093 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002094 vep = &session->vep;
Dave Wallace048b1d62018-01-03 22:24:41 -05002095 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05002096 "{\n"
2097 " is_vep = %u\n"
2098 " is_vep_session = %u\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05002099 " next_sid = 0x%x (%u)\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05002100 " wait_cont_idx = 0x%x (%u)\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05002101 "}\n", getpid (), vep_idx,
2102 session->is_vep, session->is_vep_session,
Florin Coras134a9962018-08-28 11:32:04 -07002103 vep->next_sh, vep->next_sh,
Dave Wallace498b3a52017-11-09 13:00:34 -05002104 session->wait_cont_idx, session->wait_cont_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002105
Florin Coras134a9962018-08-28 11:32:04 -07002106 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002107 {
Florin Coras134a9962018-08-28 11:32:04 -07002108 session = vcl_session_get (wrk, sid);
Florin Coras070453d2018-08-24 17:04:27 -07002109 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002110 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002111 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002112 goto done;
2113 }
2114 if (PREDICT_FALSE (session->is_vep))
Dave Wallace048b1d62018-01-03 22:24:41 -05002115 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
2116 getpid (), vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002117 else if (PREDICT_FALSE (!session->is_vep_session))
2118 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002119 clib_warning ("VCL<%d>: ERROR: session (%u) "
2120 "is not a vep session!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002121 goto done;
2122 }
2123 vep = &session->vep;
Florin Coras134a9962018-08-28 11:32:04 -07002124 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
Dave Wallace048b1d62018-01-03 22:24:41 -05002125 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002126 "vep_idx (%u)!", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002127 sid, session->vep.vep_sh, vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002128 if (session->is_vep_session)
2129 {
2130 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2131 "{\n"
2132 " next_sid = 0x%x (%u)\n"
2133 " prev_sid = 0x%x (%u)\n"
2134 " vep_idx = 0x%x (%u)\n"
2135 " ev.events = 0x%x\n"
2136 " ev.data.u64 = 0x%llx\n"
2137 " et_mask = 0x%x\n"
2138 "}\n",
2139 vep_idx, sid, sid,
Florin Coras134a9962018-08-28 11:32:04 -07002140 vep->next_sh, vep->next_sh,
2141 vep->prev_sh, vep->prev_sh,
2142 vep->vep_sh, vep->vep_sh,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002143 vep->ev.events, vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002144 }
2145 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002146
2147done:
Dave Wallace048b1d62018-01-03 22:24:41 -05002148 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
2149 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002150}
2151
2152int
2153vppcom_epoll_create (void)
2154{
Florin Coras134a9962018-08-28 11:32:04 -07002155 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002156 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002157
Florin Coras134a9962018-08-28 11:32:04 -07002158 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002159
2160 vep_session->is_vep = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002161 vep_session->vep.vep_sh = ~0;
2162 vep_session->vep.next_sh = ~0;
2163 vep_session->vep.prev_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002164 vep_session->wait_cont_idx = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002165 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002166
Florin Coras134a9962018-08-28 11:32:04 -07002167 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_sh);
Florin Coras0d427d82018-06-27 03:24:07 -07002168 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
Florin Coras134a9962018-08-28 11:32:04 -07002169 getpid (), vep_session->session_index, vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002170
Florin Corasab2f6db2018-08-31 14:31:41 -07002171 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002172}
2173
2174int
Florin Coras134a9962018-08-28 11:32:04 -07002175vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002176 struct epoll_event *event)
2177{
Florin Coras134a9962018-08-28 11:32:04 -07002178 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002179 vcl_session_t *vep_session;
2180 vcl_session_t *session;
Florin Coras070453d2018-08-24 17:04:27 -07002181 int rv = VPPCOM_OK;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002182
Florin Coras134a9962018-08-28 11:32:04 -07002183 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002184 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002185 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002186 getpid (), vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002187 return VPPCOM_EINVAL;
2188 }
2189
Florin Coras134a9962018-08-28 11:32:04 -07002190 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002191 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002192 {
Florin Coras134a9962018-08-28 11:32:04 -07002193 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002194 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002195 }
2196 if (PREDICT_FALSE (!vep_session->is_vep))
2197 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002198 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Florin Coras134a9962018-08-28 11:32:04 -07002199 getpid (), vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002200 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002201 }
2202
Florin Coras134a9962018-08-28 11:32:04 -07002203 ASSERT (vep_session->vep.vep_sh == ~0);
2204 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002205
Florin Coras134a9962018-08-28 11:32:04 -07002206 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002207 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002208 {
Florin Coras134a9962018-08-28 11:32:04 -07002209 VDBG (0, "VCL<%d>: ERROR: Invalid session_handle (%u)!",
2210 getpid (), session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002211 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002212 }
2213 if (PREDICT_FALSE (session->is_vep))
2214 {
Florin Coras134a9962018-08-28 11:32:04 -07002215 clib_warning ("ERROR: session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002216 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002217 }
2218
2219 switch (op)
2220 {
2221 case EPOLL_CTL_ADD:
2222 if (PREDICT_FALSE (!event))
2223 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002224 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002225 "epoll_event structure!", getpid ());
Florin Coras070453d2018-08-24 17:04:27 -07002226 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002227 }
Florin Coras134a9962018-08-28 11:32:04 -07002228 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002229 {
Florin Coras7e12d942018-06-27 14:32:43 -07002230 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002231 next_session = vcl_session_get_w_handle (wrk,
2232 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002233 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002234 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002235 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002236 "vep.next_sid (%u) on vep_idx (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002237 getpid (), vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002238 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002239 }
Florin Coras134a9962018-08-28 11:32:04 -07002240 ASSERT (next_session->vep.prev_sh == vep_handle);
2241 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002242 }
Florin Coras134a9962018-08-28 11:32:04 -07002243 session->vep.next_sh = vep_session->vep.next_sh;
2244 session->vep.prev_sh = vep_handle;
2245 session->vep.vep_sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002246 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2247 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002248 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002249 session->is_vep_session = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002250 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002251
Florin Coras070453d2018-08-24 17:04:27 -07002252 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x, "
Florin Coras134a9962018-08-28 11:32:04 -07002253 "data 0x%llx!", getpid (), vep_handle, session_handle,
2254 event->events, event->data.u64);
Florin Coras0d427d82018-06-27 03:24:07 -07002255 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002256 break;
2257
2258 case EPOLL_CTL_MOD:
2259 if (PREDICT_FALSE (!event))
2260 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002261 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002262 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04002263 rv = VPPCOM_EINVAL;
2264 goto done;
2265 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002266 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002267 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002268 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Florin Coras134a9962018-08-28 11:32:04 -07002269 "not a vep session!", getpid (), session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002270 rv = VPPCOM_EINVAL;
2271 goto done;
2272 }
Florin Coras134a9962018-08-28 11:32:04 -07002273 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002274 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002275 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002276 "vep_idx (%u) != vep_idx (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002277 getpid (), session_handle,
2278 session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002279 rv = VPPCOM_EINVAL;
2280 goto done;
2281 }
2282 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2283 session->vep.ev = *event;
Florin Coras0d427d82018-06-27 03:24:07 -07002284 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
Florin Coras134a9962018-08-28 11:32:04 -07002285 " data 0x%llx!", getpid (), vep_handle, session_handle,
2286 event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002287 break;
2288
2289 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05002290 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002291 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002292 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Florin Coras134a9962018-08-28 11:32:04 -07002293 "not a vep session!", getpid (), session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002294 rv = VPPCOM_EINVAL;
2295 goto done;
2296 }
Florin Coras134a9962018-08-28 11:32:04 -07002297 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002298 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002299 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002300 "vep_idx (%u) != vep_idx (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002301 getpid (), session_handle,
2302 session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002303 rv = VPPCOM_EINVAL;
2304 goto done;
2305 }
2306
2307 vep_session->wait_cont_idx =
Florin Coras134a9962018-08-28 11:32:04 -07002308 (vep_session->wait_cont_idx == session_handle) ?
2309 session->vep.next_sh : vep_session->wait_cont_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002310
Florin Coras134a9962018-08-28 11:32:04 -07002311 if (session->vep.prev_sh == vep_handle)
2312 vep_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002313 else
2314 {
Florin Coras7e12d942018-06-27 14:32:43 -07002315 vcl_session_t *prev_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002316 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002317 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002318 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002319 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002320 "vep.prev_sid (%u) on sid (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002321 getpid (), session->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002322 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002323 }
Florin Coras134a9962018-08-28 11:32:04 -07002324 ASSERT (prev_session->vep.next_sh == session_handle);
2325 prev_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002326 }
Florin Coras134a9962018-08-28 11:32:04 -07002327 if (session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002328 {
Florin Coras7e12d942018-06-27 14:32:43 -07002329 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002330 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002331 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002332 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002333 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002334 "vep.next_sid (%u) on sid (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002335 getpid (), session->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002336 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002337 }
Florin Coras134a9962018-08-28 11:32:04 -07002338 ASSERT (next_session->vep.prev_sh == session_handle);
2339 next_session->vep.prev_sh = session->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002340 }
2341
2342 memset (&session->vep, 0, sizeof (session->vep));
Florin Coras134a9962018-08-28 11:32:04 -07002343 session->vep.next_sh = ~0;
2344 session->vep.prev_sh = ~0;
2345 session->vep.vep_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002346 session->is_vep_session = 0;
Florin Coras0d427d82018-06-27 03:24:07 -07002347 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
Florin Coras134a9962018-08-28 11:32:04 -07002348 getpid (), vep_handle, session_handle);
2349 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002350 break;
2351
2352 default:
Dave Wallace048b1d62018-01-03 22:24:41 -05002353 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002354 rv = VPPCOM_EINVAL;
2355 }
2356
Florin Coras134a9962018-08-28 11:32:04 -07002357 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002358
2359done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002360 return rv;
2361}
2362
Florin Coras86f04502018-09-12 16:08:01 -07002363static inline void
2364vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2365 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002366{
2367 session_disconnected_msg_t *disconnected_msg;
2368 session_connected_msg_t *connected_msg;
2369 session_accepted_msg_t *accepted_msg;
Florin Coras54693d22018-07-17 10:46:29 -07002370 u64 session_evt_data = ~0, handle;
Florin Coras99368312018-08-02 10:45:44 -07002371 u32 sid = ~0, session_events;
Florin Coras54693d22018-07-17 10:46:29 -07002372 vcl_session_msg_t *vcl_msg;
2373 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002374 u8 add_event = 0;
2375
2376 switch (e->event_type)
2377 {
2378 case FIFO_EVENT_APP_RX:
2379 ASSERT (e->fifo->client_thread_index == vcl_get_worker_index ());
2380 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2381 sid = e->fifo->client_session_index;
2382 session = vcl_session_get (wrk, sid);
2383 session_events = session->vep.ev.events;
Florin Corasaa27eb92018-10-13 12:20:01 -07002384 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
Florin Coras86f04502018-09-12 16:08:01 -07002385 break;
2386 add_event = 1;
2387 events[*num_ev].events |= EPOLLIN;
2388 session_evt_data = session->vep.ev.data.u64;
Florin Corasaa27eb92018-10-13 12:20:01 -07002389 session->has_rx_evt = 1;
Florin Coras86f04502018-09-12 16:08:01 -07002390 break;
2391 case FIFO_EVENT_APP_TX:
2392 sid = e->fifo->client_session_index;
2393 session = vcl_session_get (wrk, sid);
2394 session_events = session->vep.ev.events;
2395 if (!(EPOLLOUT & session_events))
2396 break;
2397 add_event = 1;
2398 events[*num_ev].events |= EPOLLOUT;
2399 session_evt_data = session->vep.ev.data.u64;
2400 break;
2401 case SESSION_IO_EVT_CT_TX:
2402 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2403 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
2404 sid = session->session_index;
2405 session_events = session->vep.ev.events;
Florin Corasaa27eb92018-10-13 12:20:01 -07002406 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
Florin Coras86f04502018-09-12 16:08:01 -07002407 break;
2408 add_event = 1;
2409 events[*num_ev].events |= EPOLLIN;
2410 session_evt_data = session->vep.ev.data.u64;
Florin Corasaa27eb92018-10-13 12:20:01 -07002411 session->has_rx_evt = 1;
Florin Coras86f04502018-09-12 16:08:01 -07002412 break;
2413 case SESSION_IO_EVT_CT_RX:
2414 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
2415 sid = session->session_index;
2416 session_events = session->vep.ev.events;
2417 if (!(EPOLLOUT & session_events))
2418 break;
2419 add_event = 1;
2420 events[*num_ev].events |= EPOLLOUT;
2421 session_evt_data = session->vep.ev.data.u64;
2422 break;
2423 case SESSION_CTRL_EVT_ACCEPTED:
2424 accepted_msg = (session_accepted_msg_t *) e->data;
2425 handle = accepted_msg->listener_handle;
2426 session = vcl_session_table_lookup_listener (wrk, handle);
2427 if (!session)
2428 {
2429 clib_warning ("VCL<%d>: ERROR: couldn't find listen session:"
2430 "listener handle %llx", getpid (), handle);
2431 break;
2432 }
2433
2434 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
2435 vcl_msg->accepted_msg = *accepted_msg;
2436 session_events = session->vep.ev.events;
2437 if (!(EPOLLIN & session_events))
2438 break;
2439
2440 add_event = 1;
2441 events[*num_ev].events |= EPOLLIN;
2442 session_evt_data = session->vep.ev.data.u64;
2443 break;
2444 case SESSION_CTRL_EVT_CONNECTED:
2445 connected_msg = (session_connected_msg_t *) e->data;
2446 vcl_session_connected_handler (wrk, connected_msg);
2447 /* Generate EPOLLOUT because there's no connected event */
2448 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
2449 session = vcl_session_get (wrk, sid);
2450 session_events = session->vep.ev.events;
2451 if (EPOLLOUT & session_events)
2452 {
2453 add_event = 1;
2454 events[*num_ev].events |= EPOLLOUT;
2455 session_evt_data = session->vep.ev.data.u64;
2456 }
2457 break;
2458 case SESSION_CTRL_EVT_DISCONNECTED:
2459 disconnected_msg = (session_disconnected_msg_t *) e->data;
2460 sid = vcl_session_index_from_vpp_handle (wrk, disconnected_msg->handle);
2461 if (!(session = vcl_session_get (wrk, sid)))
2462 break;
2463 add_event = 1;
2464 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2465 session_evt_data = session->vep.ev.data.u64;
2466 session_events = session->vep.ev.events;
2467 break;
2468 case SESSION_CTRL_EVT_RESET:
2469 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2470 if (!(session = vcl_session_get (wrk, sid)))
2471 break;
2472 add_event = 1;
2473 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2474 session_evt_data = session->vep.ev.data.u64;
2475 session_events = session->vep.ev.events;
2476 break;
2477 default:
2478 VDBG (0, "unhandled: %u", e->event_type);
2479 break;
2480 }
2481
2482 if (add_event)
2483 {
2484 events[*num_ev].data.u64 = session_evt_data;
2485 if (EPOLLONESHOT & session_events)
2486 {
2487 session = vcl_session_get (wrk, sid);
2488 session->vep.ev.events = 0;
2489 }
2490 *num_ev += 1;
2491 }
2492}
2493
2494static int
2495vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2496 struct epoll_event *events, u32 maxevents,
2497 double wait_for_time, u32 * num_ev)
2498{
Florin Coras99368312018-08-02 10:45:44 -07002499 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002500 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002501 int i;
2502
Florin Coras539663c2018-09-28 14:59:37 -07002503 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2504 goto handle_dequeued;
2505
Florin Coras54693d22018-07-17 10:46:29 -07002506 svm_msg_q_lock (mq);
2507 if (svm_msg_q_is_empty (mq))
2508 {
2509 if (!wait_for_time)
2510 {
2511 svm_msg_q_unlock (mq);
2512 return 0;
2513 }
2514 else if (wait_for_time < 0)
2515 {
2516 svm_msg_q_wait (mq);
2517 }
2518 else
2519 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002520 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002521 {
2522 svm_msg_q_unlock (mq);
2523 return 0;
2524 }
2525 }
2526 }
Florin Coras134a9962018-08-28 11:32:04 -07002527 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07002528 svm_msg_q_unlock (mq);
2529
Florin Coras539663c2018-09-28 14:59:37 -07002530handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002531 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002532 {
Florin Coras134a9962018-08-28 11:32:04 -07002533 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002534 e = svm_msg_q_msg_data (mq, msg);
Florin Corasaa27eb92018-10-13 12:20:01 -07002535 if (*num_ev < maxevents)
2536 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2537 else
2538 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras99368312018-08-02 10:45:44 -07002539 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002540 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002541 vec_reset_length (wrk->mq_msg_vector);
Florin Coras86f04502018-09-12 16:08:01 -07002542
Florin Coras54693d22018-07-17 10:46:29 -07002543 return *num_ev;
2544}
2545
Florin Coras99368312018-08-02 10:45:44 -07002546static int
Florin Coras134a9962018-08-28 11:32:04 -07002547vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002548 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002549{
2550 vcl_cut_through_registration_t *cr;
2551 double total_wait = 0, wait_slice;
Florin Coras99368312018-08-02 10:45:44 -07002552 int rv;
2553
2554 wait_for_time = (wait_for_time == -1) ? (double) 10e9 : wait_for_time;
Florin Coras134a9962018-08-28 11:32:04 -07002555 wait_slice = wrk->cut_through_registrations ? 10e-6 : wait_for_time;
Florin Coras99368312018-08-02 10:45:44 -07002556
2557 do
2558 {
Florin Coras134a9962018-08-28 11:32:04 -07002559 vcl_ct_registration_lock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07002560 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002561 pool_foreach (cr, wrk->cut_through_registrations, ({
Florin Coras86f04502018-09-12 16:08:01 -07002562 vcl_epoll_wait_handle_mq (wrk, cr->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002563 }));
2564 /* *INDENT-ON* */
Florin Coras134a9962018-08-28 11:32:04 -07002565 vcl_ct_registration_unlock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07002566
Florin Coras134a9962018-08-28 11:32:04 -07002567 rv = vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events,
Florin Coras86f04502018-09-12 16:08:01 -07002568 maxevents, n_evts ? 0 : wait_slice,
2569 &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002570 if (rv)
2571 total_wait += wait_slice;
Florin Coras86f04502018-09-12 16:08:01 -07002572 if (n_evts)
2573 return n_evts;
Florin Coras99368312018-08-02 10:45:44 -07002574 }
2575 while (total_wait < wait_for_time);
Florin Coras86f04502018-09-12 16:08:01 -07002576 return n_evts;
Florin Coras99368312018-08-02 10:45:44 -07002577}
2578
2579static int
Florin Coras134a9962018-08-28 11:32:04 -07002580vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002581 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002582{
2583 vcl_mq_evt_conn_t *mqc;
2584 int __clib_unused n_read;
2585 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07002586 u64 buf;
2587
Florin Coras134a9962018-08-28 11:32:04 -07002588 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07002589again:
Florin Coras134a9962018-08-28 11:32:04 -07002590 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2591 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07002592 for (i = 0; i < n_mq_evts; i++)
2593 {
Florin Coras134a9962018-08-28 11:32:04 -07002594 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002595 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002596 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002597 }
Florin Corasa4878ed2018-10-12 13:09:36 -07002598 if (!n_evts && n_mq_evts > 0)
2599 goto again;
Florin Coras99368312018-08-02 10:45:44 -07002600
2601 return (int) n_evts;
2602}
2603
Dave Wallacef7f809c2017-10-03 01:48:42 -04002604int
Florin Coras134a9962018-08-28 11:32:04 -07002605vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002606 int maxevents, double wait_for_time)
2607{
Florin Coras134a9962018-08-28 11:32:04 -07002608 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002609 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07002610 u32 n_evts = 0;
2611 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002612
2613 if (PREDICT_FALSE (maxevents <= 0))
2614 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002615 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05002616 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002617 return VPPCOM_EINVAL;
2618 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002619
Florin Coras134a9962018-08-28 11:32:04 -07002620 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07002621 if (!vep_session)
2622 return VPPCOM_EBADFD;
2623
Florin Coras54693d22018-07-17 10:46:29 -07002624 if (PREDICT_FALSE (!vep_session->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002625 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002626 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Florin Coras134a9962018-08-28 11:32:04 -07002627 getpid (), vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07002628 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002629 }
Florin Coras54693d22018-07-17 10:46:29 -07002630
2631 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002632
Florin Coras86f04502018-09-12 16:08:01 -07002633 if (vec_len (wrk->unhandled_evts_vector))
2634 {
2635 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2636 {
2637 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2638 events, &n_evts);
2639 if (n_evts == maxevents)
2640 {
2641 i += 1;
2642 break;
2643 }
2644 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002645
Florin Coras86f04502018-09-12 16:08:01 -07002646 vec_delete (wrk->unhandled_evts_vector, i, 0);
2647 }
2648
2649 if (vcm->cfg.use_mq_eventfd)
2650 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2651 wait_for_time);
2652
2653 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2654 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002655}
2656
Dave Wallace35830af2017-10-09 01:43:42 -04002657int
Florin Coras134a9962018-08-28 11:32:04 -07002658vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04002659 void *buffer, uint32_t * buflen)
2660{
Florin Coras134a9962018-08-28 11:32:04 -07002661 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002662 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04002663 int rv = VPPCOM_OK;
2664 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07002665 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04002666
Florin Coras134a9962018-08-28 11:32:04 -07002667 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002668 if (!session)
2669 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002670
Dave Wallace35830af2017-10-09 01:43:42 -04002671 switch (op)
2672 {
2673 case VPPCOM_ATTR_GET_NREAD:
Florin Coras54693d22018-07-17 10:46:29 -07002674 rv = vppcom_session_read_ready (session);
Florin Coras0d427d82018-06-27 03:24:07 -07002675 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
2676 getpid (), rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002677 break;
2678
Dave Wallace227867f2017-11-13 21:21:53 -05002679 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras134a9962018-08-28 11:32:04 -07002680 rv = vppcom_session_write_ready (session);
Florin Coras0d427d82018-06-27 03:24:07 -07002681 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
Florin Coras134a9962018-08-28 11:32:04 -07002682 getpid (), session_handle, rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002683 break;
2684
2685 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002686 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002687 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002688 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2689 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002690 *buflen = sizeof (*flags);
Florin Coras0d427d82018-06-27 03:24:07 -07002691 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2692 "is_nonblocking = %u", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002693 session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002694 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002695 }
2696 else
2697 rv = VPPCOM_EINVAL;
2698 break;
2699
2700 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002701 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002702 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002703 if (*flags & O_NONBLOCK)
2704 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2705 else
2706 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2707
Florin Coras0d427d82018-06-27 03:24:07 -07002708 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2709 " is_nonblocking = %u",
Florin Coras134a9962018-08-28 11:32:04 -07002710 getpid (), session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002711 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002712 }
2713 else
2714 rv = VPPCOM_EINVAL;
2715 break;
2716
2717 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002718 if (PREDICT_TRUE (buffer && buflen &&
2719 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002720 {
Florin Coras7e12d942018-06-27 14:32:43 -07002721 ep->is_ip4 = session->transport.is_ip4;
2722 ep->port = session->transport.rmt_port;
2723 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002724 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2725 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002726 else
Dave Barach178cf492018-11-13 16:34:13 -05002727 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2728 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002729 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002730 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2731 "addr = %U, port %u", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002732 session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002733 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002734 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2735 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002736 }
2737 else
2738 rv = VPPCOM_EINVAL;
2739 break;
2740
2741 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002742 if (PREDICT_TRUE (buffer && buflen &&
2743 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002744 {
Florin Coras7e12d942018-06-27 14:32:43 -07002745 ep->is_ip4 = session->transport.is_ip4;
2746 ep->port = session->transport.lcl_port;
2747 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002748 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2749 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002750 else
Dave Barach178cf492018-11-13 16:34:13 -05002751 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2752 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002753 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002754 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2755 " addr = %U port %d", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002756 session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002757 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002758 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2759 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002760 }
2761 else
2762 rv = VPPCOM_EINVAL;
2763 break;
Stevenb5a11602017-10-11 09:59:30 -07002764
Dave Wallace048b1d62018-01-03 22:24:41 -05002765 case VPPCOM_ATTR_GET_LIBC_EPFD:
2766 rv = session->libc_epfd;
Florin Coras0d427d82018-06-27 03:24:07 -07002767 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2768 getpid (), rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002769 break;
2770
2771 case VPPCOM_ATTR_SET_LIBC_EPFD:
2772 if (PREDICT_TRUE (buffer && buflen &&
2773 (*buflen == sizeof (session->libc_epfd))))
2774 {
2775 session->libc_epfd = *(int *) buffer;
2776 *buflen = sizeof (session->libc_epfd);
2777
Florin Coras0d427d82018-06-27 03:24:07 -07002778 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2779 "buflen %d", getpid (), session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002780 }
2781 else
2782 rv = VPPCOM_EINVAL;
2783 break;
2784
2785 case VPPCOM_ATTR_GET_PROTOCOL:
2786 if (buffer && buflen && (*buflen >= sizeof (int)))
2787 {
Florin Coras7e12d942018-06-27 14:32:43 -07002788 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05002789 *buflen = sizeof (int);
2790
Florin Coras0d427d82018-06-27 03:24:07 -07002791 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2792 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2793 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002794 }
2795 else
2796 rv = VPPCOM_EINVAL;
2797 break;
2798
2799 case VPPCOM_ATTR_GET_LISTEN:
2800 if (buffer && buflen && (*buflen >= sizeof (int)))
2801 {
2802 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2803 VCL_SESS_ATTR_LISTEN);
2804 *buflen = sizeof (int);
2805
Florin Coras0d427d82018-06-27 03:24:07 -07002806 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2807 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002808 }
2809 else
2810 rv = VPPCOM_EINVAL;
2811 break;
2812
2813 case VPPCOM_ATTR_GET_ERROR:
2814 if (buffer && buflen && (*buflen >= sizeof (int)))
2815 {
2816 *(int *) buffer = 0;
2817 *buflen = sizeof (int);
2818
Florin Coras0d427d82018-06-27 03:24:07 -07002819 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2820 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002821 }
2822 else
2823 rv = VPPCOM_EINVAL;
2824 break;
2825
2826 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2827 if (buffer && buflen && (*buflen >= sizeof (u32)))
2828 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002829
2830 /* VPP-TBD */
2831 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002832 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002833 vcm->cfg.tx_fifo_size);
2834 *buflen = sizeof (u32);
2835
Florin Coras0d427d82018-06-27 03:24:07 -07002836 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2837 "buflen %d, #VPP-TBD#", getpid (),
2838 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002839 }
2840 else
2841 rv = VPPCOM_EINVAL;
2842 break;
2843
2844 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2845 if (buffer && buflen && (*buflen == sizeof (u32)))
2846 {
2847 /* VPP-TBD */
2848 session->sndbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07002849 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
2850 "buflen %d, #VPP-TBD#", getpid (),
2851 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002852 }
2853 else
2854 rv = VPPCOM_EINVAL;
2855 break;
2856
2857 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2858 if (buffer && buflen && (*buflen >= sizeof (u32)))
2859 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002860
2861 /* VPP-TBD */
2862 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002863 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002864 vcm->cfg.rx_fifo_size);
2865 *buflen = sizeof (u32);
2866
Florin Coras0d427d82018-06-27 03:24:07 -07002867 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
2868 "buflen %d, #VPP-TBD#", getpid (),
2869 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002870 }
2871 else
2872 rv = VPPCOM_EINVAL;
2873 break;
2874
2875 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2876 if (buffer && buflen && (*buflen == sizeof (u32)))
2877 {
2878 /* VPP-TBD */
2879 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07002880 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
2881 "buflen %d, #VPP-TBD#", getpid (),
2882 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002883 }
2884 else
2885 rv = VPPCOM_EINVAL;
2886 break;
2887
2888 case VPPCOM_ATTR_GET_REUSEADDR:
2889 if (buffer && buflen && (*buflen >= sizeof (int)))
2890 {
2891 /* VPP-TBD */
2892 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2893 VCL_SESS_ATTR_REUSEADDR);
2894 *buflen = sizeof (int);
2895
Florin Coras0d427d82018-06-27 03:24:07 -07002896 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
2897 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002898 }
2899 else
2900 rv = VPPCOM_EINVAL;
2901 break;
2902
Stevenb5a11602017-10-11 09:59:30 -07002903 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002904 if (buffer && buflen && (*buflen == sizeof (int)) &&
2905 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2906 {
2907 /* VPP-TBD */
2908 if (*(int *) buffer)
2909 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2910 else
2911 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2912
Florin Coras0d427d82018-06-27 03:24:07 -07002913 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
2914 " #VPP-TBD#", getpid (),
2915 VCL_SESS_ATTR_TEST (session->attr,
2916 VCL_SESS_ATTR_REUSEADDR), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002917 }
2918 else
2919 rv = VPPCOM_EINVAL;
2920 break;
2921
2922 case VPPCOM_ATTR_GET_REUSEPORT:
2923 if (buffer && buflen && (*buflen >= sizeof (int)))
2924 {
2925 /* VPP-TBD */
2926 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2927 VCL_SESS_ATTR_REUSEPORT);
2928 *buflen = sizeof (int);
2929
Florin Coras0d427d82018-06-27 03:24:07 -07002930 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
2931 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002932 }
2933 else
2934 rv = VPPCOM_EINVAL;
2935 break;
2936
2937 case VPPCOM_ATTR_SET_REUSEPORT:
2938 if (buffer && buflen && (*buflen == sizeof (int)) &&
2939 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2940 {
2941 /* VPP-TBD */
2942 if (*(int *) buffer)
2943 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2944 else
2945 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2946
Florin Coras0d427d82018-06-27 03:24:07 -07002947 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
2948 " #VPP-TBD#", getpid (),
2949 VCL_SESS_ATTR_TEST (session->attr,
2950 VCL_SESS_ATTR_REUSEPORT), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002951 }
2952 else
2953 rv = VPPCOM_EINVAL;
2954 break;
2955
2956 case VPPCOM_ATTR_GET_BROADCAST:
2957 if (buffer && buflen && (*buflen >= sizeof (int)))
2958 {
2959 /* VPP-TBD */
2960 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2961 VCL_SESS_ATTR_BROADCAST);
2962 *buflen = sizeof (int);
2963
Florin Coras0d427d82018-06-27 03:24:07 -07002964 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
2965 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002966 }
2967 else
2968 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002969 break;
2970
2971 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05002972 if (buffer && buflen && (*buflen == sizeof (int)))
2973 {
2974 /* VPP-TBD */
2975 if (*(int *) buffer)
2976 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2977 else
2978 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2979
Florin Coras0d427d82018-06-27 03:24:07 -07002980 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
2981 "#VPP-TBD#", getpid (),
2982 VCL_SESS_ATTR_TEST (session->attr,
2983 VCL_SESS_ATTR_BROADCAST), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002984 }
2985 else
2986 rv = VPPCOM_EINVAL;
2987 break;
2988
2989 case VPPCOM_ATTR_GET_V6ONLY:
2990 if (buffer && buflen && (*buflen >= sizeof (int)))
2991 {
2992 /* VPP-TBD */
2993 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2994 VCL_SESS_ATTR_V6ONLY);
2995 *buflen = sizeof (int);
2996
Florin Coras0d427d82018-06-27 03:24:07 -07002997 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
2998 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002999 }
3000 else
3001 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003002 break;
3003
3004 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003005 if (buffer && buflen && (*buflen == sizeof (int)))
3006 {
3007 /* VPP-TBD */
3008 if (*(int *) buffer)
3009 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
3010 else
3011 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
3012
Florin Coras0d427d82018-06-27 03:24:07 -07003013 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
3014 "#VPP-TBD#", getpid (),
3015 VCL_SESS_ATTR_TEST (session->attr,
3016 VCL_SESS_ATTR_V6ONLY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003017 }
3018 else
3019 rv = VPPCOM_EINVAL;
3020 break;
3021
3022 case VPPCOM_ATTR_GET_KEEPALIVE:
3023 if (buffer && buflen && (*buflen >= sizeof (int)))
3024 {
3025 /* VPP-TBD */
3026 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3027 VCL_SESS_ATTR_KEEPALIVE);
3028 *buflen = sizeof (int);
3029
Florin Coras0d427d82018-06-27 03:24:07 -07003030 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
3031 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003032 }
3033 else
3034 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003035 break;
Stevenbccd3392017-10-12 20:42:21 -07003036
3037 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003038 if (buffer && buflen && (*buflen == sizeof (int)))
3039 {
3040 /* VPP-TBD */
3041 if (*(int *) buffer)
3042 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3043 else
3044 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3045
Florin Coras0d427d82018-06-27 03:24:07 -07003046 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
3047 "#VPP-TBD#", getpid (),
3048 VCL_SESS_ATTR_TEST (session->attr,
3049 VCL_SESS_ATTR_KEEPALIVE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003050 }
3051 else
3052 rv = VPPCOM_EINVAL;
3053 break;
3054
3055 case VPPCOM_ATTR_GET_TCP_NODELAY:
3056 if (buffer && buflen && (*buflen >= sizeof (int)))
3057 {
3058 /* VPP-TBD */
3059 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3060 VCL_SESS_ATTR_TCP_NODELAY);
3061 *buflen = sizeof (int);
3062
Florin Coras0d427d82018-06-27 03:24:07 -07003063 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
3064 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003065 }
3066 else
3067 rv = VPPCOM_EINVAL;
3068 break;
3069
3070 case VPPCOM_ATTR_SET_TCP_NODELAY:
3071 if (buffer && buflen && (*buflen == sizeof (int)))
3072 {
3073 /* VPP-TBD */
3074 if (*(int *) buffer)
3075 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3076 else
3077 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3078
Florin Coras0d427d82018-06-27 03:24:07 -07003079 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
3080 "#VPP-TBD#", getpid (),
3081 VCL_SESS_ATTR_TEST (session->attr,
3082 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003083 }
3084 else
3085 rv = VPPCOM_EINVAL;
3086 break;
3087
3088 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3089 if (buffer && buflen && (*buflen >= sizeof (int)))
3090 {
3091 /* VPP-TBD */
3092 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3093 VCL_SESS_ATTR_TCP_KEEPIDLE);
3094 *buflen = sizeof (int);
3095
Florin Coras0d427d82018-06-27 03:24:07 -07003096 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
3097 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003098 }
3099 else
3100 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003101 break;
3102
3103 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003104 if (buffer && buflen && (*buflen == sizeof (int)))
3105 {
3106 /* VPP-TBD */
3107 if (*(int *) buffer)
3108 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3109 else
3110 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3111
Florin Coras0d427d82018-06-27 03:24:07 -07003112 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
3113 "#VPP-TBD#", getpid (),
3114 VCL_SESS_ATTR_TEST (session->attr,
3115 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003116 }
3117 else
3118 rv = VPPCOM_EINVAL;
3119 break;
3120
3121 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3122 if (buffer && buflen && (*buflen >= sizeof (int)))
3123 {
3124 /* VPP-TBD */
3125 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3126 VCL_SESS_ATTR_TCP_KEEPINTVL);
3127 *buflen = sizeof (int);
3128
Florin Coras0d427d82018-06-27 03:24:07 -07003129 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
3130 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003131 }
3132 else
3133 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003134 break;
3135
3136 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003137 if (buffer && buflen && (*buflen == sizeof (int)))
3138 {
3139 /* VPP-TBD */
3140 if (*(int *) buffer)
3141 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3142 else
3143 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3144
Florin Coras0d427d82018-06-27 03:24:07 -07003145 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
3146 "#VPP-TBD#", getpid (),
3147 VCL_SESS_ATTR_TEST (session->attr,
3148 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003149 }
3150 else
3151 rv = VPPCOM_EINVAL;
3152 break;
3153
3154 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3155 if (buffer && buflen && (*buflen >= sizeof (u32)))
3156 {
3157 /* VPP-TBD */
3158 *(u32 *) buffer = session->user_mss;
3159 *buflen = sizeof (int);
3160
Florin Coras0d427d82018-06-27 03:24:07 -07003161 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
3162 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003163 }
3164 else
3165 rv = VPPCOM_EINVAL;
3166 break;
3167
3168 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3169 if (buffer && buflen && (*buflen == sizeof (u32)))
3170 {
3171 /* VPP-TBD */
3172 session->user_mss = *(u32 *) buffer;
3173
Florin Coras0d427d82018-06-27 03:24:07 -07003174 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
3175 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003176 }
3177 else
3178 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003179 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003180
Florin Coras47c40e22018-11-26 17:01:36 -08003181 case VPPCOM_ATTR_GET_REFCNT:
3182 rv = vcl_session_get_refcnt (session);
3183 break;
3184
Dave Wallacee22aa742017-10-20 12:30:38 -04003185 default:
3186 rv = VPPCOM_EINVAL;
3187 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003188 }
3189
Dave Wallace35830af2017-10-09 01:43:42 -04003190 return rv;
3191}
3192
Stevenac1f96d2017-10-24 16:03:58 -07003193int
Florin Coras134a9962018-08-28 11:32:04 -07003194vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003195 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3196{
Florin Coras134a9962018-08-28 11:32:04 -07003197 vcl_worker_t *wrk = vcl_worker_get_current ();
Stevenac1f96d2017-10-24 16:03:58 -07003198 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07003199 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07003200
3201 if (ep)
3202 {
Florin Coras134a9962018-08-28 11:32:04 -07003203 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003204 if (PREDICT_FALSE (!session))
Stevenac1f96d2017-10-24 16:03:58 -07003205 {
Florin Coras0d427d82018-06-27 03:24:07 -07003206 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
Florin Coras134a9962018-08-28 11:32:04 -07003207 getpid (), session_handle);
Florin Coras460dce62018-07-27 05:45:06 -07003208 return VPPCOM_EBADFD;
Stevenac1f96d2017-10-24 16:03:58 -07003209 }
Florin Coras7e12d942018-06-27 14:32:43 -07003210 ep->is_ip4 = session->transport.is_ip4;
3211 ep->port = session->transport.rmt_port;
Stevenac1f96d2017-10-24 16:03:58 -07003212 }
Steven58f464e2017-10-25 12:33:12 -07003213
3214 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003215 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003216 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003217 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003218 else
3219 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003220 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
3221 getpid (), flags);
Florin Coras460dce62018-07-27 05:45:06 -07003222 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003223 }
3224
Florin Coras99368312018-08-02 10:45:44 -07003225 if (ep)
3226 {
3227 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003228 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3229 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003230 else
Dave Barach178cf492018-11-13 16:34:13 -05003231 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3232 sizeof (ip6_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003233 }
Florin Coras460dce62018-07-27 05:45:06 -07003234
Stevenac1f96d2017-10-24 16:03:58 -07003235 return rv;
3236}
3237
3238int
Florin Coras134a9962018-08-28 11:32:04 -07003239vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003240 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3241{
Dave Wallace617dffa2017-10-26 14:47:06 -04003242 if (!buffer)
3243 return VPPCOM_EINVAL;
3244
3245 if (ep)
3246 {
3247 // TBD
3248 return VPPCOM_EINVAL;
3249 }
3250
3251 if (flags)
3252 {
3253 // TBD check the flags and do the right thing
Florin Coras0d427d82018-06-27 03:24:07 -07003254 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3255 getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003256 }
3257
Florin Coras134a9962018-08-28 11:32:04 -07003258 return (vppcom_session_write (session_handle, buffer, buflen));
Stevenac1f96d2017-10-24 16:03:58 -07003259}
3260
Dave Wallace048b1d62018-01-03 22:24:41 -05003261int
3262vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3263{
Florin Coras134a9962018-08-28 11:32:04 -07003264 vcl_worker_t *wrk = vcl_worker_get_current ();
3265 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003266 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003267 svm_msg_q_msg_t msg;
3268 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003269 int rv, num_ev = 0;
3270
Florin Coras0d427d82018-06-27 03:24:07 -07003271 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3272 getpid (), vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003273
3274 if (!vp)
3275 return VPPCOM_EFAULT;
3276
3277 do
3278 {
Florin Coras7e12d942018-06-27 14:32:43 -07003279 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003280
Florin Coras6917b942018-11-13 22:44:54 -08003281 /* Dequeue all events and drop all unhandled io events */
3282 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3283 {
3284 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3285 vcl_handle_mq_event (wrk, e);
3286 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3287 }
3288 vec_reset_length (wrk->unhandled_evts_vector);
3289
Dave Wallace048b1d62018-01-03 22:24:41 -05003290 for (i = 0; i < n_sids; i++)
3291 {
Florin Coras134a9962018-08-28 11:32:04 -07003292 session = vcl_session_get (wrk, vp[i].sid);
Florin Coras070453d2018-08-24 17:04:27 -07003293 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003294 {
3295 vp[i].revents = POLLHUP;
3296 num_ev++;
3297 continue;
3298 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003299
Florin Coras6917b942018-11-13 22:44:54 -08003300 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003301
3302 if (POLLIN & vp[i].events)
3303 {
Florin Coras54693d22018-07-17 10:46:29 -07003304 rv = vppcom_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003305 if (rv > 0)
3306 {
Florin Coras6917b942018-11-13 22:44:54 -08003307 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003308 num_ev++;
3309 }
3310 else if (rv < 0)
3311 {
3312 switch (rv)
3313 {
3314 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003315 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003316 break;
3317
3318 default:
Florin Coras6917b942018-11-13 22:44:54 -08003319 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003320 break;
3321 }
3322 num_ev++;
3323 }
3324 }
3325
3326 if (POLLOUT & vp[i].events)
3327 {
Florin Coras134a9962018-08-28 11:32:04 -07003328 rv = vppcom_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003329 if (rv > 0)
3330 {
Florin Coras6917b942018-11-13 22:44:54 -08003331 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003332 num_ev++;
3333 }
3334 else if (rv < 0)
3335 {
3336 switch (rv)
3337 {
3338 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003339 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003340 break;
3341
3342 default:
Florin Coras6917b942018-11-13 22:44:54 -08003343 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003344 break;
3345 }
3346 num_ev++;
3347 }
3348 }
3349
Dave Wallace7e607a72018-06-18 18:41:32 -04003350 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003351 {
Florin Coras6917b942018-11-13 22:44:54 -08003352 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003353 num_ev++;
3354 }
3355 }
3356 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003357 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003358 }
3359 while ((num_ev == 0) && keep_trying);
3360
3361 if (VPPCOM_DEBUG > 3)
3362 {
3363 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3364 for (i = 0; i < n_sids; i++)
3365 {
3366 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
3367 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
Florin Coras6917b942018-11-13 22:44:54 -08003368 vp[i].events, vp[i].revents);
Dave Wallace048b1d62018-01-03 22:24:41 -05003369 }
3370 }
3371 return num_ev;
3372}
3373
Florin Coras99368312018-08-02 10:45:44 -07003374int
3375vppcom_mq_epoll_fd (void)
3376{
Florin Coras134a9962018-08-28 11:32:04 -07003377 vcl_worker_t *wrk = vcl_worker_get_current ();
3378 return wrk->mqs_epfd;
3379}
3380
3381int
3382vppcom_session_index (uint32_t session_handle)
3383{
3384 return session_handle & 0xFFFFFF;
3385}
3386
3387int
Florin Coras30e273b2018-11-27 00:04:59 -08003388vppcom_session_handle (uint32_t session_index)
3389{
Florin Coras47c40e22018-11-26 17:01:36 -08003390 return (vcl_get_worker_index () << 24) | session_index;
Florin Coras30e273b2018-11-27 00:04:59 -08003391}
3392
3393int
Florin Coras134a9962018-08-28 11:32:04 -07003394vppcom_worker_register (void)
3395{
Florin Coras47c40e22018-11-26 17:01:36 -08003396 if (!vcl_worker_alloc_and_init ())
3397 return VPPCOM_EEXIST;
3398
3399 if (vcl_worker_set_bapi ())
3400 return VPPCOM_EEXIST;
3401
3402 if (vcl_worker_register_with_vpp ())
3403 return VPPCOM_EEXIST;
3404
3405 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003406}
3407
Florin Corasdfe4cf42018-11-28 22:13:45 -08003408int
3409vppcom_worker_index (void)
3410{
3411 return vcl_get_worker_index ();
3412}
3413
Dave Wallacee22aa742017-10-20 12:30:38 -04003414/*
3415 * fd.io coding-style-patch-verification: ON
3416 *
3417 * Local Variables:
3418 * eval: (c-set-style "gnu")
3419 * End:
3420 */