blob: a456ed0e4cd4a080b10a8c19b79cb0f6747e9edc [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
Florin Coras3c7d4f92018-12-14 11:28:43 -080070 case STATE_VPP_CLOSING:
71 st = "STATE_VPP_CLOSING";
Dave Wallace4878cbe2017-11-21 03:45:09 -050072 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 Coras3c7d4f92018-12-14 11:28:43 -0800184 return wrk->vpp_event_queues[s->vpp_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;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800292 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras54693d22018-07-17 10:46:29 -0700293 session->client_context = mp->context;
294 session->rx_fifo = rx_fifo;
295 session->tx_fifo = tx_fifo;
296
297 session->session_state = STATE_ACCEPT;
298 session->transport.rmt_port = mp->port;
299 session->transport.is_ip4 = mp->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500300 clib_memcpy_fast (&session->transport.rmt_ip, mp->ip,
301 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700302
Florin Coras134a9962018-08-28 11:32:04 -0700303 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700304 session->transport.lcl_port = listen_session->transport.lcl_port;
305 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700306 session->session_type = listen_session->session_type;
307 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
Florin Coras54693d22018-07-17 10:46:29 -0700308
309 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: client accept request from %s"
Florin Coras134a9962018-08-28 11:32:04 -0700310 " address %U port %d queue %p!", getpid (), mp->handle,
311 session->session_index,
Florin Coras54693d22018-07-17 10:46:29 -0700312 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
313 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
314 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
315 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
316
Florin Coras134a9962018-08-28 11:32:04 -0700317 return session->session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700318}
319
320static u32
Florin Coras134a9962018-08-28 11:32:04 -0700321vcl_session_connected_handler (vcl_worker_t * wrk,
322 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700323{
Florin Coras99368312018-08-02 10:45:44 -0700324 u32 session_index, vpp_wrk_index;
Florin Coras54693d22018-07-17 10:46:29 -0700325 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700326 vcl_session_t *session = 0;
327 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700328
329 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700330 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700331 if (!session)
332 {
333 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
334 "Invalid session index (%u)!",
335 getpid (), mp->handle, session_index);
336 return VCL_INVALID_SESSION_INDEX;
337 }
Florin Coras54693d22018-07-17 10:46:29 -0700338 if (mp->retval)
339 {
Florin Coras070453d2018-08-24 17:04:27 -0700340 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed! %U", getpid (),
Florin Coras21795132018-09-09 09:40:51 -0700341 session_index, format_api_error, ntohl (mp->retval));
Florin Coras070453d2018-08-24 17:04:27 -0700342 session->session_state = STATE_FAILED;
343 session->vpp_handle = mp->handle;
344 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700345 }
346
Florin Coras460dce62018-07-27 05:45:06 -0700347 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
348 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Corasd85de682018-11-29 17:02:29 -0800349 if (vcl_wait_for_segment (mp->segment_handle))
350 {
351 clib_warning ("segment for session %u couldn't be mounted!",
352 session->session_index);
353 return VCL_INVALID_SESSION_INDEX;
354 }
355
Florin Coras460dce62018-07-27 05:45:06 -0700356 rx_fifo->client_session_index = session_index;
357 tx_fifo->client_session_index = session_index;
Florin Coras21795132018-09-09 09:40:51 -0700358 rx_fifo->client_thread_index = vcl_get_worker_index ();
359 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras460dce62018-07-27 05:45:06 -0700360
Florin Coras54693d22018-07-17 10:46:29 -0700361 if (mp->client_event_queue_address)
362 {
363 session->vpp_evt_q = uword_to_pointer (mp->server_event_queue_address,
364 svm_msg_q_t *);
365 session->our_evt_q = uword_to_pointer (mp->client_event_queue_address,
366 svm_msg_q_t *);
Florin Coras99368312018-08-02 10:45:44 -0700367
Florin Coras134a9962018-08-28 11:32:04 -0700368 vec_validate (wrk->vpp_event_queues, 0);
Florin Coras99368312018-08-02 10:45:44 -0700369 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
Florin Coras134a9962018-08-28 11:32:04 -0700370 wrk->vpp_event_queues[0] = evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700371 }
372 else
Florin Coras99368312018-08-02 10:45:44 -0700373 {
374 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
375 svm_msg_q_t *);
376 vpp_wrk_index = tx_fifo->master_thread_index;
Florin Coras134a9962018-08-28 11:32:04 -0700377 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
378 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -0700379 }
Florin Coras54693d22018-07-17 10:46:29 -0700380
Florin Coras54693d22018-07-17 10:46:29 -0700381 session->rx_fifo = rx_fifo;
382 session->tx_fifo = tx_fifo;
383 session->vpp_handle = mp->handle;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800384 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras54693d22018-07-17 10:46:29 -0700385 session->transport.is_ip4 = mp->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500386 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
387 sizeof (session->transport.lcl_ip));
Florin Coras54693d22018-07-17 10:46:29 -0700388 session->transport.lcl_port = mp->lcl_port;
389 session->session_state = STATE_CONNECT;
390
391 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800392 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700393
394 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded! "
395 "session_rx_fifo %p, refcnt %d, session_tx_fifo %p, refcnt %d",
396 getpid (), mp->handle, session_index, session->rx_fifo,
397 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700398
Florin Coras54693d22018-07-17 10:46:29 -0700399 return session_index;
400}
401
Florin Coras3c7d4f92018-12-14 11:28:43 -0800402static int
403vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
404{
405 vcl_session_msg_t *accepted_msg;
406 int i;
407
408 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
409 {
410 accepted_msg = &session->accept_evts_fifo[i];
411 if (accepted_msg->accepted_msg.handle == handle)
412 {
413 accepted_msg->flags = flags;
414 return 1;
415 }
416 }
417 return 0;
418}
419
Florin Corasc9fbd662018-08-24 12:59:56 -0700420static u32
Florin Coras134a9962018-08-28 11:32:04 -0700421vcl_session_reset_handler (vcl_worker_t * wrk,
422 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700423{
424 vcl_session_t *session;
425 u32 sid;
426
Florin Coras134a9962018-08-28 11:32:04 -0700427 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
428 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700429 if (!session)
430 {
431 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
432 return VCL_INVALID_SESSION_INDEX;
433 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800434 if (session->session_state >= STATE_VPP_CLOSING)
435 return sid;
436
437 /* Caught a reset before actually accepting the session */
438 if (session->session_state == STATE_LISTEN)
439 {
440
441 if (!vcl_flag_accepted_session (session, reset_msg->handle,
442 VCL_ACCEPTED_F_RESET))
443 VDBG (0, "session was not accepted!");
444 return VCL_INVALID_SESSION_INDEX;
445 }
446
447 session->session_state = STATE_DISCONNECT;
448 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Coras134a9962018-08-28 11:32:04 -0700449 vcl_send_session_reset_reply (vcl_session_vpp_evt_q (wrk, session),
Florin Coras47c40e22018-11-26 17:01:36 -0800450 wrk->my_client_index, reset_msg->handle, 0);
Florin Corasc9fbd662018-08-24 12:59:56 -0700451 return sid;
452}
453
Florin Coras60116992018-08-27 09:52:18 -0700454static u32
Florin Coras134a9962018-08-28 11:32:04 -0700455vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700456{
457 vcl_session_t *session;
458 u32 sid = mp->context;
459
Florin Coras134a9962018-08-28 11:32:04 -0700460 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700461 if (mp->retval)
462 {
Florin Corasd85de682018-11-29 17:02:29 -0800463 VERR ("vpp handle 0x%llx, sid %u: bind failed: %U", mp->handle, sid,
464 format_api_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700465 if (session)
466 {
467 session->session_state = STATE_FAILED;
468 session->vpp_handle = mp->handle;
469 return sid;
470 }
471 else
472 {
473 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
474 "Invalid session index (%u)!",
475 getpid (), mp->handle, sid);
476 return VCL_INVALID_SESSION_INDEX;
477 }
478 }
479
480 session->vpp_handle = mp->handle;
481 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500482 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
483 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700484 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700485 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Coras60116992018-08-27 09:52:18 -0700486 session->session_state = STATE_LISTEN;
487
488 if (session->is_dgram)
489 {
490 svm_fifo_t *rx_fifo, *tx_fifo;
491 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
492 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
493 rx_fifo->client_session_index = sid;
494 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
495 tx_fifo->client_session_index = sid;
496 session->rx_fifo = rx_fifo;
497 session->tx_fifo = tx_fifo;
498 }
499
Florin Coras05ecfcc2018-12-12 18:19:39 -0800500 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700501 return sid;
502}
503
Florin Coras3c7d4f92018-12-14 11:28:43 -0800504static vcl_session_t *
505vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
506{
507 vcl_session_msg_t *vcl_msg;
508 vcl_session_t *session;
509
510 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
511 if (PREDICT_FALSE (session != 0))
512 VWRN ("session handle overlap %lu!", msg->handle);
513
514 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
515 if (!session)
516 {
517 VERR ("couldn't find listen session: listener handle %llx",
518 msg->listener_handle);
519 return 0;
520 }
521
522 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
523 vcl_msg->accepted_msg = *msg;
524 /* Session handle points to listener until fully accepted by app */
525 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
526
527 return session;
528}
529
530static vcl_session_t *
531vcl_session_disconnected_handler (vcl_worker_t * wrk,
532 session_disconnected_msg_t * msg)
533{
534 vcl_session_t *session;
535
536 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
537 if (!session)
538 {
539 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
540 return 0;
541 }
542
543 /* Caught a disconnect before actually accepting the session */
544 if (session->session_state == STATE_LISTEN)
545 {
546
547 if (!vcl_flag_accepted_session (session, msg->handle,
548 VCL_ACCEPTED_F_CLOSED))
549 VDBG (0, "session was not accepted!");
550 return 0;
551 }
552
553 session->session_state = STATE_VPP_CLOSING;
554 return session;
555}
556
Florin Coras86f04502018-09-12 16:08:01 -0700557static int
558vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700559{
Florin Coras54693d22018-07-17 10:46:29 -0700560 session_disconnected_msg_t *disconnected_msg;
Florin Coras54693d22018-07-17 10:46:29 -0700561 vcl_session_t *session;
Florin Coras54693d22018-07-17 10:46:29 -0700562
563 switch (e->event_type)
564 {
565 case FIFO_EVENT_APP_RX:
Florin Coras86f04502018-09-12 16:08:01 -0700566 case FIFO_EVENT_APP_TX:
567 case SESSION_IO_EVT_CT_RX:
568 case SESSION_IO_EVT_CT_TX:
569 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -0700570 break;
571 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -0800572 vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700573 break;
574 case SESSION_CTRL_EVT_CONNECTED:
Florin Coras134a9962018-08-28 11:32:04 -0700575 vcl_session_connected_handler (wrk,
576 (session_connected_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700577 break;
578 case SESSION_CTRL_EVT_DISCONNECTED:
579 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800580 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
Florin Corasc9fbd662018-08-24 12:59:56 -0700581 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800582 break;
Florin Coras54693d22018-07-17 10:46:29 -0700583 session->session_state = STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800584 VDBG (0, "disconnected session %u [0x%llx]", session->session_index,
585 session->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700586 break;
587 case SESSION_CTRL_EVT_RESET:
Florin Coras134a9962018-08-28 11:32:04 -0700588 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -0700589 break;
590 case SESSION_CTRL_EVT_BOUND:
Florin Coras134a9962018-08-28 11:32:04 -0700591 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700592 break;
593 default:
594 clib_warning ("unhandled %u", e->event_type);
595 }
596 return VPPCOM_OK;
597}
598
Florin Coras697faea2018-06-27 17:10:49 -0700599static inline int
600vppcom_wait_for_session_state_change (u32 session_index,
601 session_state_t state,
602 f64 wait_for_time)
603{
Florin Coras134a9962018-08-28 11:32:04 -0700604 vcl_worker_t *wrk = vcl_worker_get_current ();
605 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -0700606 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -0700607 svm_msg_q_msg_t msg;
608 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -0400609
Florin Coras697faea2018-06-27 17:10:49 -0700610 do
Dave Wallace543852a2017-08-03 02:11:34 -0400611 {
Florin Coras134a9962018-08-28 11:32:04 -0700612 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700613 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -0700614 {
Florin Coras070453d2018-08-24 17:04:27 -0700615 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -0700616 }
617 if (session->session_state & state)
618 {
Florin Coras697faea2018-06-27 17:10:49 -0700619 return VPPCOM_OK;
620 }
621 if (session->session_state & STATE_FAILED)
622 {
Florin Coras697faea2018-06-27 17:10:49 -0700623 return VPPCOM_ECONNREFUSED;
624 }
Florin Coras54693d22018-07-17 10:46:29 -0700625
Florin Coras134a9962018-08-28 11:32:04 -0700626 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -0800627 {
628 usleep (100);
629 continue;
630 }
Florin Coras134a9962018-08-28 11:32:04 -0700631 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -0700632 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -0700633 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800634 }
Florin Coras134a9962018-08-28 11:32:04 -0700635 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800636
Florin Coras05ecfcc2018-12-12 18:19:39 -0800637 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -0700638 vppcom_session_state_str (state));
639 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -0400640
Florin Coras697faea2018-06-27 17:10:49 -0700641 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -0500642}
643
Florin Coras697faea2018-06-27 17:10:49 -0700644static int
645vppcom_app_session_enable (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400646{
Florin Coras697faea2018-06-27 17:10:49 -0700647 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400648
Florin Coras697faea2018-06-27 17:10:49 -0700649 if (vcm->app_state != STATE_APP_ENABLED)
650 {
651 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
Florin Coras134a9962018-08-28 11:32:04 -0700652 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
Florin Coras697faea2018-06-27 17:10:49 -0700653 if (PREDICT_FALSE (rv))
654 {
655 VDBG (0, "VCL<%d>: application session enable timed out! "
656 "returning %d (%s)", getpid (), rv, vppcom_retval_str (rv));
657 return rv;
658 }
659 }
660 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400661}
662
Florin Coras697faea2018-06-27 17:10:49 -0700663static int
664vppcom_app_attach (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400665{
Florin Coras697faea2018-06-27 17:10:49 -0700666 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400667
Florin Coras697faea2018-06-27 17:10:49 -0700668 vppcom_app_send_attach ();
Florin Coras134a9962018-08-28 11:32:04 -0700669 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
Florin Coras697faea2018-06-27 17:10:49 -0700670 if (PREDICT_FALSE (rv))
671 {
672 VDBG (0, "VCL<%d>: application attach timed out! returning %d (%s)",
673 getpid (), rv, vppcom_retval_str (rv));
674 return rv;
675 }
Dave Wallace543852a2017-08-03 02:11:34 -0400676
Florin Coras697faea2018-06-27 17:10:49 -0700677 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400678}
679
680static int
Florin Corasab2f6db2018-08-31 14:31:41 -0700681vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400682{
Florin Coras134a9962018-08-28 11:32:04 -0700683 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -0700684 vcl_session_t *session = 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500685 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400686
Florin Corasab2f6db2018-08-31 14:31:41 -0700687 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700688 if (!session)
689 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500690
691 vpp_handle = session->vpp_handle;
Florin Coras134a9962018-08-28 11:32:04 -0700692 vcl_session_table_del_listener (wrk, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500693 session->vpp_handle = ~0;
Florin Coras7e12d942018-06-27 14:32:43 -0700694 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500695
Florin Coras0d427d82018-06-27 03:24:07 -0700696 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending unbind msg! new state"
Florin Corasab2f6db2018-08-31 14:31:41 -0700697 " 0x%x (%s)", getpid (), vpp_handle, session_handle, STATE_DISCONNECT,
Florin Coras0d427d82018-06-27 03:24:07 -0700698 vppcom_session_state_str (STATE_DISCONNECT));
699 vcl_evt (VCL_EVT_UNBIND, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500700 vppcom_send_unbind_sock (vpp_handle);
701
Florin Coras070453d2018-08-24 17:04:27 -0700702 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400703}
704
Florin Coras697faea2018-06-27 17:10:49 -0700705static int
Florin Corasab2f6db2018-08-31 14:31:41 -0700706vppcom_session_disconnect (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400707{
Florin Coras134a9962018-08-28 11:32:04 -0700708 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -0700709 svm_msg_q_t *vpp_evt_q;
Florin Coras7e12d942018-06-27 14:32:43 -0700710 vcl_session_t *session;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500711 session_state_t state;
Florin Coras99368312018-08-02 10:45:44 -0700712 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400713
Florin Corasab2f6db2018-08-31 14:31:41 -0700714 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras21795132018-09-09 09:40:51 -0700715 if (!session)
716 return VPPCOM_EBADFD;
717
Dave Wallace4878cbe2017-11-21 03:45:09 -0500718 vpp_handle = session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -0700719 state = session->session_state;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500720
Florin Coras0d427d82018-06-27 03:24:07 -0700721 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u state 0x%x (%s)", getpid (),
Florin Corasab2f6db2018-08-31 14:31:41 -0700722 vpp_handle, session_handle, state, vppcom_session_state_str (state));
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400723
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800724 if (PREDICT_FALSE (state & STATE_LISTEN))
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400725 {
Dave Wallace048b1d62018-01-03 22:24:41 -0500726 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -0500727 "Cannot disconnect a listen socket!",
Florin Corasab2f6db2018-08-31 14:31:41 -0700728 getpid (), vpp_handle, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700729 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500730 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400731
Florin Coras3c7d4f92018-12-14 11:28:43 -0800732 if (state & STATE_VPP_CLOSING)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500733 {
Florin Coras134a9962018-08-28 11:32:04 -0700734 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
Florin Coras47c40e22018-11-26 17:01:36 -0800735 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index,
Florin Coras99368312018-08-02 10:45:44 -0700736 vpp_handle, 0);
Florin Coras0d427d82018-06-27 03:24:07 -0700737 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect "
Florin Corasab2f6db2018-08-31 14:31:41 -0700738 "REPLY...", getpid (), vpp_handle, session_handle);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400739 }
740 else
Dave Wallace227867f2017-11-13 21:21:53 -0500741 {
Florin Coras0d427d82018-06-27 03:24:07 -0700742 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: sending disconnect...",
Florin Corasab2f6db2018-08-31 14:31:41 -0700743 getpid (), vpp_handle, session_handle);
744 vppcom_send_disconnect_session (vpp_handle);
Dave Wallace227867f2017-11-13 21:21:53 -0500745 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400746
Florin Coras070453d2018-08-24 17:04:27 -0700747 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400748}
749
Florin Coras053a0e42018-11-13 15:52:38 -0800750static void
751vcl_cleanup_bapi (void)
752{
Florin Coras47c40e22018-11-26 17:01:36 -0800753 socket_client_main_t *scm = &socket_client_main;
Florin Coras053a0e42018-11-13 15:52:38 -0800754 api_main_t *am = &api_main;
755
756 am->my_client_index = ~0;
757 am->my_registration = 0;
758 am->vl_input_queue = 0;
759 am->msg_index_by_name_and_crc = 0;
Florin Coras47c40e22018-11-26 17:01:36 -0800760 scm->socket_fd = 0;
Florin Coras053a0e42018-11-13 15:52:38 -0800761
762 vl_client_api_unmap ();
763}
764
Florin Coras01f3f892018-12-02 12:45:53 -0800765static void
766vcl_cleanup_forked_child (vcl_worker_t * wrk, vcl_worker_t * child_wrk)
767{
768 vcl_worker_t *sub_child;
769 int tries = 0;
770
771 if (child_wrk->forked_child != ~0)
772 {
773 sub_child = vcl_worker_get_if_valid (child_wrk->forked_child);
774 if (sub_child)
775 {
776 /* Wait a bit, maybe the process is going away */
777 while (kill (sub_child->current_pid, 0) >= 0 && tries++ < 50)
778 usleep (1e3);
779 if (kill (sub_child->current_pid, 0) < 0)
780 vcl_cleanup_forked_child (child_wrk, sub_child);
781 }
782 }
783 vcl_worker_cleanup (child_wrk, 1 /* notify vpp */ );
784 VDBG (0, "Cleaned up wrk %u", child_wrk->wrk_index);
785 wrk->forked_child = ~0;
786}
787
788static struct sigaction old_sa;
789
790static void
791vcl_intercept_sigchld_handler (int signum, siginfo_t * si, void *uc)
792{
793 vcl_worker_t *wrk, *child_wrk;
794
795 if (vcl_get_worker_index () == ~0)
796 return;
797
798 sigaction (SIGCHLD, &old_sa, 0);
799
800 wrk = vcl_worker_get_current ();
801 if (wrk->forked_child == ~0)
802 return;
803
804 child_wrk = vcl_worker_get_if_valid (wrk->forked_child);
Florin Coraseaec2a62018-12-04 16:34:05 -0800805 if (!child_wrk)
806 goto done;
807
808 if (si && si->si_pid != child_wrk->current_pid)
Florin Coras01f3f892018-12-02 12:45:53 -0800809 {
810 VDBG (0, "unexpected child pid %u", si->si_pid);
Florin Coraseaec2a62018-12-04 16:34:05 -0800811 goto done;
Florin Coras01f3f892018-12-02 12:45:53 -0800812 }
Florin Coraseaec2a62018-12-04 16:34:05 -0800813 vcl_cleanup_forked_child (wrk, child_wrk);
Florin Coras01f3f892018-12-02 12:45:53 -0800814
Florin Coraseaec2a62018-12-04 16:34:05 -0800815done:
Florin Coras01f3f892018-12-02 12:45:53 -0800816 if (old_sa.sa_flags & SA_SIGINFO)
817 {
818 void (*fn) (int, siginfo_t *, void *) = old_sa.sa_sigaction;
819 fn (signum, si, uc);
820 }
821 else
822 {
823 void (*fn) (int) = old_sa.sa_handler;
824 if (fn)
825 fn (signum);
826 }
827}
828
829static void
830vcl_incercept_sigchld ()
831{
832 struct sigaction sa;
833 clib_memset (&sa, 0, sizeof (sa));
834 sa.sa_sigaction = vcl_intercept_sigchld_handler;
835 sa.sa_flags = SA_SIGINFO;
836 if (sigaction (SIGCHLD, &sa, &old_sa))
837 {
838 VERR ("couldn't intercept sigchld");
839 exit (-1);
840 }
841}
842
843static void
844vcl_app_pre_fork (void)
845{
846 vcl_incercept_sigchld ();
847}
848
849static void
Florin Coras053a0e42018-11-13 15:52:38 -0800850vcl_app_fork_child_handler (void)
851{
Florin Coras01f3f892018-12-02 12:45:53 -0800852 int rv, parent_wrk_index;
853 vcl_worker_t *parent_wrk;
Florin Coras053a0e42018-11-13 15:52:38 -0800854 u8 *child_name;
Florin Coras053a0e42018-11-13 15:52:38 -0800855
Florin Coras01f3f892018-12-02 12:45:53 -0800856 parent_wrk_index = vcl_get_worker_index ();
857 VDBG (0, "initializing forked child with parent wrk %u", parent_wrk_index);
Florin Coras053a0e42018-11-13 15:52:38 -0800858
Florin Coras47c40e22018-11-26 17:01:36 -0800859 /*
860 * Allocate worker
861 */
Florin Coras47c40e22018-11-26 17:01:36 -0800862 vcl_set_worker_index (~0);
863 if (!vcl_worker_alloc_and_init ())
864 VERR ("couldn't allocate new worker");
865
866 /*
867 * Attach to binary api
868 */
869 child_name = format (0, "%v-child-%u%c", vcm->app_name, getpid (), 0);
Florin Coras053a0e42018-11-13 15:52:38 -0800870 vcl_cleanup_bapi ();
871 vppcom_api_hookup ();
872 vcm->app_state = STATE_APP_START;
873 rv = vppcom_connect_to_vpp ((char *) child_name);
874 vec_free (child_name);
875 if (rv)
876 {
877 VERR ("couldn't connect to VPP!");
878 return;
879 }
880
Florin Coras47c40e22018-11-26 17:01:36 -0800881 /*
882 * Register worker with vpp and share sessions
883 */
884 vcl_worker_register_with_vpp ();
Florin Coras01f3f892018-12-02 12:45:53 -0800885 parent_wrk = vcl_worker_get (parent_wrk_index);
Florin Coras47c40e22018-11-26 17:01:36 -0800886 vcl_worker_share_sessions (parent_wrk);
Florin Coras01f3f892018-12-02 12:45:53 -0800887 parent_wrk->forked_child = vcl_get_worker_index ();
Florin Coras47c40e22018-11-26 17:01:36 -0800888
Florin Coras053a0e42018-11-13 15:52:38 -0800889 VDBG (0, "forked child main worker initialized");
Florin Coras47c40e22018-11-26 17:01:36 -0800890 vcm->forking = 0;
891}
892
Florin Coras01f3f892018-12-02 12:45:53 -0800893static void
Florin Coras47c40e22018-11-26 17:01:36 -0800894vcl_app_fork_parent_handler (void)
895{
896 vcm->forking = 1;
Florin Coras47c40e22018-11-26 17:01:36 -0800897 while (vcm->forking)
898 ;
Florin Coras053a0e42018-11-13 15:52:38 -0800899}
900
Florin Coras940f78f2018-11-30 12:11:20 -0800901/**
902 * Handle app exit
903 *
904 * Notify vpp of the disconnect and mark the worker as free. If we're the
905 * last worker, do a full cleanup otherwise, since we're probably a forked
906 * child, avoid syscalls as much as possible. We might've lost privileges.
907 */
908void
909vppcom_app_exit (void)
910{
911 if (!pool_elts (vcm->workers))
912 return;
Florin Coras01f3f892018-12-02 12:45:53 -0800913 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
914 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -0800915 vcl_elog_stop (vcm);
916 if (vec_len (vcm->workers) == 1)
917 vl_client_disconnect_from_vlib ();
918 else
Florin Coraseaec2a62018-12-04 16:34:05 -0800919 vl_client_send_disconnect (1 /* vpp should cleanup */ );
Florin Coras940f78f2018-11-30 12:11:20 -0800920}
921
Dave Wallace543852a2017-08-03 02:11:34 -0400922/*
923 * VPPCOM Public API functions
924 */
925int
926vppcom_app_create (char *app_name)
927{
Dave Wallace543852a2017-08-03 02:11:34 -0400928 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -0400929 int rv;
930
Florin Coras47c40e22018-11-26 17:01:36 -0800931 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -0400932 {
Florin Coras955bfbb2018-12-04 13:43:45 -0800933 VDBG (1, "already initialized");
934 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -0400935 }
936
Florin Coras47c40e22018-11-26 17:01:36 -0800937 vcm->is_init = 1;
938 vppcom_cfg (&vcm->cfg);
939 vcl_cfg = &vcm->cfg;
940
941 vcm->main_cpu = pthread_self ();
942 vcm->main_pid = getpid ();
943 vcm->app_name = format (0, "%s", app_name);
944 vppcom_init_error_string_table ();
Florin Corasadc74d72018-12-02 13:36:00 -0800945 svm_fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
Florin Coras47c40e22018-11-26 17:01:36 -0800946 20 /* timeout in secs */ );
947 pool_alloc (vcm->workers, vcl_cfg->max_workers);
948 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -0800949 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras01f3f892018-12-02 12:45:53 -0800950 pthread_atfork (vcl_app_pre_fork, vcl_app_fork_parent_handler,
Florin Coras47c40e22018-11-26 17:01:36 -0800951 vcl_app_fork_child_handler);
Florin Coras940f78f2018-11-30 12:11:20 -0800952 atexit (vppcom_app_exit);
Florin Coras47c40e22018-11-26 17:01:36 -0800953
954 /* Allocate default worker */
955 vcl_worker_alloc_and_init ();
956
957 /* API hookup and connect to VPP */
958 vppcom_api_hookup ();
959 vcl_elog_init (vcm);
960 vcm->app_state = STATE_APP_START;
961 rv = vppcom_connect_to_vpp (app_name);
962 if (rv)
Dave Wallace543852a2017-08-03 02:11:34 -0400963 {
Florin Coras47c40e22018-11-26 17:01:36 -0800964 VERR ("couldn't connect to VPP!");
965 return rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400966 }
Florin Coras47c40e22018-11-26 17:01:36 -0800967 VDBG (0, "sending session enable");
968 rv = vppcom_app_session_enable ();
969 if (rv)
970 {
971 VERR ("vppcom_app_session_enable() failed!");
972 return rv;
973 }
974
975 VDBG (0, "sending app attach");
976 rv = vppcom_app_attach ();
977 if (rv)
978 {
979 VERR ("vppcom_app_attach() failed!");
980 return rv;
981 }
982
983 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
984 vcm->workers[0].my_client_index, vcm->workers[0].my_client_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400985
986 return VPPCOM_OK;
987}
988
989void
990vppcom_app_destroy (void)
991{
Dave Wallace543852a2017-08-03 02:11:34 -0400992 int rv;
Dave Wallacede910062018-03-20 09:22:13 -0400993 f64 orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -0400994
Florin Coras940f78f2018-11-30 12:11:20 -0800995 if (!pool_elts (vcm->workers))
996 return;
997
Florin Coras0d427d82018-06-27 03:24:07 -0700998 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800999
Florin Coras940f78f2018-11-30 12:11:20 -08001000 if (pool_elts (vcm->workers) == 1)
Florin Coras47c40e22018-11-26 17:01:36 -08001001 {
1002 vppcom_app_send_detach ();
1003 orig_app_timeout = vcm->cfg.app_timeout;
1004 vcm->cfg.app_timeout = 2.0;
1005 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
1006 vcm->cfg.app_timeout = orig_app_timeout;
1007 if (PREDICT_FALSE (rv))
1008 VDBG (0, "application detach timed out! returning %d (%s)", rv,
1009 vppcom_retval_str (rv));
Florin Coras940f78f2018-11-30 12:11:20 -08001010 vec_free (vcm->app_name);
Florin Coras01f3f892018-12-02 12:45:53 -08001011 vcl_worker_cleanup (vcl_worker_get_current (), 0 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001012 }
1013 else
1014 {
Florin Coras01f3f892018-12-02 12:45:53 -08001015 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -08001016 }
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -08001017
Florin Coras01f3f892018-12-02 12:45:53 -08001018 vcl_set_worker_index (~0);
Florin Coras0d427d82018-06-27 03:24:07 -07001019 vcl_elog_stop (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -04001020 vl_client_disconnect_from_vlib ();
Dave Wallace543852a2017-08-03 02:11:34 -04001021}
1022
1023int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001024vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001025{
Florin Coras134a9962018-08-28 11:32:04 -07001026 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001027 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001028
Florin Coras134a9962018-08-28 11:32:04 -07001029 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001030
Florin Coras7e12d942018-06-27 14:32:43 -07001031 session->session_type = proto;
1032 session->session_state = STATE_START;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001033 session->vpp_handle = ~0;
Florin Coras460dce62018-07-27 05:45:06 -07001034 session->is_dgram = proto == VPPCOM_PROTO_UDP;
Dave Wallace543852a2017-08-03 02:11:34 -04001035
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001036 if (is_nonblocking)
1037 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001038
Florin Coras7e12d942018-06-27 14:32:43 -07001039 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1040 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001041
Florin Coras053a0e42018-11-13 15:52:38 -08001042 VDBG (0, "created sid %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001043
Florin Coras134a9962018-08-28 11:32:04 -07001044 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001045}
1046
1047int
Florin Coras134a9962018-08-28 11:32:04 -07001048vppcom_session_close (uint32_t session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -04001049{
Florin Coras134a9962018-08-28 11:32:04 -07001050 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras47c40e22018-11-26 17:01:36 -08001051 u8 is_vep, do_disconnect = 1;
Florin Coras7e12d942018-06-27 14:32:43 -07001052 vcl_session_t *session = 0;
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001053 session_state_t state;
Florin Coras134a9962018-08-28 11:32:04 -07001054 u32 next_sh, vep_sh;
1055 int rv = VPPCOM_OK;
1056 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001057
Florin Coras134a9962018-08-28 11:32:04 -07001058 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001059 if (!session)
1060 return VPPCOM_EBADFD;
1061
Florin Coras47c40e22018-11-26 17:01:36 -08001062 if (session->shared_index != ~0)
1063 do_disconnect = vcl_worker_unshare_session (wrk, session);
1064
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001065 is_vep = session->is_vep;
Florin Coras134a9962018-08-28 11:32:04 -07001066 next_sh = session->vep.next_sh;
1067 vep_sh = session->vep.vep_sh;
Florin Coras7e12d942018-06-27 14:32:43 -07001068 state = session->session_state;
Dave Wallaceee45d412017-11-24 21:44:06 -05001069 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001070
Florin Coras05ecfcc2018-12-12 18:19:39 -08001071 VDBG (1, "closing session handle %u vpp handle %u", session_handle,
Florin Coras47c40e22018-11-26 17:01:36 -08001072 vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001073
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001074 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04001075 {
Florin Coras134a9962018-08-28 11:32:04 -07001076 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001077 {
Florin Coras134a9962018-08-28 11:32:04 -07001078 rv = vppcom_epoll_ctl (session_handle, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001079 if (PREDICT_FALSE (rv < 0))
Florin Coras47c40e22018-11-26 17:01:36 -08001080 VDBG (0, "vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL vep_idx %u"
1081 " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv,
1082 vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001083
Florin Coras134a9962018-08-28 11:32:04 -07001084 next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001085 }
1086 }
1087 else
1088 {
Florin Coras47c40e22018-11-26 17:01:36 -08001089 if (session->is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001090 {
Florin Coras134a9962018-08-28 11:32:04 -07001091 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, session_handle, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001092 if (rv < 0)
Florin Coras47c40e22018-11-26 17:01:36 -08001093 VDBG (0, "vpp handle 0x%llx, sid %u: EPOLL_CTL_DEL vep_idx %u "
1094 "failed! rv %d (%s)", vpp_handle, session_handle, vep_sh,
1095 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001096 }
1097
Florin Coras47c40e22018-11-26 17:01:36 -08001098 if (!do_disconnect)
1099 goto cleanup;
1100
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001101 if (state & STATE_LISTEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001102 {
Florin Coras134a9962018-08-28 11:32:04 -07001103 rv = vppcom_session_unbind (session_handle);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001104 if (PREDICT_FALSE (rv < 0))
Florin Coras47c40e22018-11-26 17:01:36 -08001105 VDBG (0, "vpp handle 0x%llx, sid %u: listener unbind failed! "
1106 "rv %d (%s)", vpp_handle, session_handle, rv,
1107 vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001108 }
Florin Coras070453d2018-08-24 17:04:27 -07001109 else if (state & STATE_OPEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001110 {
Florin Coras134a9962018-08-28 11:32:04 -07001111 rv = vppcom_session_disconnect (session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001112 if (PREDICT_FALSE (rv < 0))
Dave Wallace048b1d62018-01-03 22:24:41 -05001113 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001114 "session disconnect failed! rv %d (%s)",
Florin Coras134a9962018-08-28 11:32:04 -07001115 getpid (), vpp_handle, session_handle,
Dave Wallaceee45d412017-11-24 21:44:06 -05001116 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001117 }
Dave Wallace19481612017-09-15 18:47:44 -04001118 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001119
Florin Coras47c40e22018-11-26 17:01:36 -08001120cleanup:
1121
Florin Coras99368312018-08-02 10:45:44 -07001122 if (vcl_session_is_ct (session))
1123 {
1124 vcl_cut_through_registration_t *ctr;
1125 uword mq_addr;
1126
1127 mq_addr = pointer_to_uword (session->our_evt_q);
Florin Coras134a9962018-08-28 11:32:04 -07001128 ctr = vcl_ct_registration_lock_and_lookup (wrk, mq_addr);
Florin Coras99368312018-08-02 10:45:44 -07001129 ASSERT (ctr);
1130 if (ctr->epoll_evt_conn_index != ~0)
Florin Coras134a9962018-08-28 11:32:04 -07001131 vcl_mq_epoll_del_evfd (wrk, ctr->epoll_evt_conn_index);
Florin Coras99368312018-08-02 10:45:44 -07001132 VDBG (0, "Removing ct registration %u",
Florin Coras134a9962018-08-28 11:32:04 -07001133 vcl_ct_registration_index (wrk, ctr));
1134 vcl_ct_registration_del (wrk, ctr);
1135 vcl_ct_registration_lookup_del (wrk, mq_addr);
1136 vcl_ct_registration_unlock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07001137 }
Florin Coras54693d22018-07-17 10:46:29 -07001138
Dave Wallaceee45d412017-11-24 21:44:06 -05001139 if (vpp_handle != ~0)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001140 {
Florin Coras134a9962018-08-28 11:32:04 -07001141 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001142 }
Florin Coras134a9962018-08-28 11:32:04 -07001143 vcl_session_free (wrk, session);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001144
Florin Coras05ecfcc2018-12-12 18:19:39 -08001145 VDBG (0, "session handle %u [0x%llx] removed", session_handle, vpp_handle);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001146
Florin Coras0d427d82018-06-27 03:24:07 -07001147 vcl_evt (VCL_EVT_CLOSE, session, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001148
Dave Wallace543852a2017-08-03 02:11:34 -04001149 return rv;
1150}
1151
1152int
Florin Coras134a9962018-08-28 11:32:04 -07001153vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001154{
Florin Coras134a9962018-08-28 11:32:04 -07001155 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001156 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001157
1158 if (!ep || !ep->ip)
1159 return VPPCOM_EINVAL;
1160
Florin Coras134a9962018-08-28 11:32:04 -07001161 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001162 if (!session)
1163 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001164
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001165 if (session->is_vep)
1166 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001167 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
Florin Coras134a9962018-08-28 11:32:04 -07001168 "bind to an epoll session!", getpid (), session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001169 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001170 }
1171
Florin Coras7e12d942018-06-27 14:32:43 -07001172 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001173 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001174 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1175 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001176 else
Dave Barach178cf492018-11-13 16:34:13 -05001177 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1178 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001179 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001180
Florin Coras0d427d82018-06-27 03:24:07 -07001181 VDBG (0, "VCL<%d>: sid %u: binding to local %s address %U port %u, "
Florin Coras134a9962018-08-28 11:32:04 -07001182 "proto %s", getpid (), session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001183 session->transport.is_ip4 ? "IPv4" : "IPv6",
1184 format_ip46_address, &session->transport.lcl_ip,
1185 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1186 clib_net_to_host_u16 (session->transport.lcl_port),
1187 session->session_type ? "UDP" : "TCP");
Florin Coras0d427d82018-06-27 03:24:07 -07001188 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001189
1190 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001191 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001192
Florin Coras070453d2018-08-24 17:04:27 -07001193 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001194}
1195
1196int
Florin Coras134a9962018-08-28 11:32:04 -07001197vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001198{
Florin Coras134a9962018-08-28 11:32:04 -07001199 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001200 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001201 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001202 int rv;
1203
Florin Coras134a9962018-08-28 11:32:04 -07001204 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001205 if (!listen_session || listen_session->is_vep)
Florin Coras070453d2018-08-24 17:04:27 -07001206 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001207
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001208 if (q_len == 0 || q_len == ~0)
1209 q_len = vcm->cfg.listen_queue_size;
1210
Dave Wallaceee45d412017-11-24 21:44:06 -05001211 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -07001212 if (listen_session->session_state & STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001213 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001214 VDBG (0, "session %u [0x%llx]: already in listen state!",
1215 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001216 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001217 }
1218
Florin Coras05ecfcc2018-12-12 18:19:39 -08001219 VDBG (0, "session %u [0x%llx]: sending vpp listen request...",
1220 listen_sh, listen_vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001221
Florin Coras070453d2018-08-24 17:04:27 -07001222 /*
1223 * Send listen request to vpp and wait for reply
1224 */
Florin Coras134a9962018-08-28 11:32:04 -07001225 vppcom_send_bind_sock (listen_session);
1226 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1227 STATE_LISTEN,
1228 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001229
Florin Coras070453d2018-08-24 17:04:27 -07001230 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001231 {
Florin Coras134a9962018-08-28 11:32:04 -07001232 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001233 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1234 listen_sh, listen_session->vpp_handle, rv,
1235 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001236 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001237 }
1238
Florin Coras070453d2018-08-24 17:04:27 -07001239 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001240}
1241
Florin Coras134a9962018-08-28 11:32:04 -07001242static int
1243validate_args_session_accept_ (vcl_worker_t * wrk,
1244 vcl_session_t * listen_session)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001245{
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001246 /* Input validation - expects spinlock on sessions_lockp */
1247 if (listen_session->is_vep)
1248 {
1249 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
Florin Coras134a9962018-08-28 11:32:04 -07001250 "epoll session!", getpid (),
1251 listen_session->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001252 return VPPCOM_EBADFD;
1253 }
1254
Florin Coras7e12d942018-06-27 14:32:43 -07001255 if (listen_session->session_state != STATE_LISTEN)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001256 {
1257 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1258 "not in listen state! state 0x%x (%s)", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07001259 listen_session->vpp_handle, listen_session->session_index,
Florin Coras7e12d942018-06-27 14:32:43 -07001260 listen_session->session_state,
1261 vppcom_session_state_str (listen_session->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001262 return VPPCOM_EBADFD;
1263 }
1264 return VPPCOM_OK;
1265}
1266
1267int
Florin Coras134a9962018-08-28 11:32:04 -07001268vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001269 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001270{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001271 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001272 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001273 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001274 vcl_session_t *listen_session = 0;
1275 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001276 svm_msg_q_t *vpp_evt_q;
1277 vcl_session_msg_t *evt;
Dave Wallaceee45d412017-11-24 21:44:06 -05001278 u64 listen_vpp_handle;
Florin Coras54693d22018-07-17 10:46:29 -07001279 svm_msg_q_msg_t msg;
1280 session_event_t *e;
1281 u8 is_nonblocking;
1282 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001283
Florin Coras134a9962018-08-28 11:32:04 -07001284 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001285 if (!listen_session)
1286 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001287
Florin Coras134a9962018-08-28 11:32:04 -07001288 listen_session_index = listen_session->session_index;
1289 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001290 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001291
Florin Coras54693d22018-07-17 10:46:29 -07001292 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001293 {
Florin Coras54693d22018-07-17 10:46:29 -07001294 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001295 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001296 accepted_msg = evt->accepted_msg;
1297 goto handle;
1298 }
1299
1300 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1301 VCL_SESS_ATTR_NONBLOCK);
Florin Coras134a9962018-08-28 11:32:04 -07001302 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
Florin Coras54693d22018-07-17 10:46:29 -07001303 return VPPCOM_EAGAIN;
1304
1305 while (1)
1306 {
Florin Coras134a9962018-08-28 11:32:04 -07001307 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -07001308 return VPPCOM_EAGAIN;
1309
Florin Coras134a9962018-08-28 11:32:04 -07001310 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001311 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001312 {
Florin Coras54693d22018-07-17 10:46:29 -07001313 clib_warning ("discarded event: %u", e->event_type);
Florin Coras134a9962018-08-28 11:32:04 -07001314 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001315 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001316 }
Dave Barach178cf492018-11-13 16:34:13 -05001317 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
Florin Coras134a9962018-08-28 11:32:04 -07001318 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001319 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001320 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001321
Florin Coras54693d22018-07-17 10:46:29 -07001322handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001323
Florin Coras134a9962018-08-28 11:32:04 -07001324 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg);
1325 listen_session = vcl_session_get (wrk, listen_session_index);
1326 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001327
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001328 if (flags & O_NONBLOCK)
1329 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001330
Florin Coras54693d22018-07-17 10:46:29 -07001331 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras05ecfcc2018-12-12 18:19:39 -08001332 VDBG (1, "vpp handle 0x%llx, sid %u: Got a client request! "
Florin Coras0d427d82018-06-27 03:24:07 -07001333 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
Florin Corasdc2e2512018-12-03 17:47:26 -08001334 listen_vpp_handle, listen_session_handle,
Florin Coras0d427d82018-06-27 03:24:07 -07001335 client_session->vpp_handle, client_session_index,
1336 flags, VCL_SESS_ATTR_TEST (client_session->attr,
1337 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001338
Dave Wallace048b1d62018-01-03 22:24:41 -05001339 if (ep)
1340 {
Florin Coras7e12d942018-06-27 14:32:43 -07001341 ep->is_ip4 = client_session->transport.is_ip4;
1342 ep->port = client_session->transport.rmt_port;
1343 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001344 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1345 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001346 else
Dave Barach178cf492018-11-13 16:34:13 -05001347 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1348 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001349 }
Dave Wallace60caa062017-11-10 17:07:13 -05001350
Florin Coras54693d22018-07-17 10:46:29 -07001351 if (accepted_msg.server_event_queue_address)
1352 vpp_evt_q = uword_to_pointer (accepted_msg.vpp_event_queue_address,
1353 svm_msg_q_t *);
1354 else
1355 vpp_evt_q = client_session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -07001356
Florin Coras54693d22018-07-17 10:46:29 -07001357 vcl_send_session_accepted_reply (vpp_evt_q, client_session->client_context,
1358 client_session->vpp_handle, 0);
Dave Wallace60caa062017-11-10 17:07:13 -05001359
Florin Coras05ecfcc2018-12-12 18:19:39 -08001360 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
1361 "local: %U:%u", listen_session_handle, listen_vpp_handle,
1362 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001363 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001364 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001365 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001366 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001367 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001368 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001369 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1370 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001371
Florin Coras3c7d4f92018-12-14 11:28:43 -08001372 /*
1373 * Session might have been closed already
1374 */
1375 if (accept_flags)
1376 {
1377 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, client_session);
1378 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
1379 {
1380 client_session->session_state = STATE_DISCONNECT;
1381 vcl_send_session_disconnected_reply (mq, wrk->my_client_index,
1382 client_session->vpp_handle, 0);
1383 }
1384 else if (accept_flags & VCL_ACCEPTED_F_RESET)
1385 {
1386 client_session->session_state = STATE_DISCONNECT;
1387 vcl_send_session_reset_reply (mq, wrk->my_client_index,
1388 client_session->vpp_handle, 0);
1389 }
1390 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001391 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001392}
1393
1394int
Florin Coras134a9962018-08-28 11:32:04 -07001395vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001396{
Florin Coras134a9962018-08-28 11:32:04 -07001397 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001398 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001399 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001400 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001401
Florin Coras134a9962018-08-28 11:32:04 -07001402 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001403 if (!session)
1404 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001405 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001406
1407 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001408 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001409 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
Florin Coras134a9962018-08-28 11:32:04 -07001410 "connect on an epoll session!", getpid (),
1411 session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001412 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001413 }
1414
Florin Coras7e12d942018-06-27 14:32:43 -07001415 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
Dave Wallace543852a2017-08-03 02:11:34 -04001416 {
Florin Coras0d427d82018-06-27 03:24:07 -07001417 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: session already "
1418 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras134a9962018-08-28 11:32:04 -07001419 getpid (), session->vpp_handle, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001420 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001421 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001422 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001423 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001424 clib_net_to_host_u16 (session->transport.rmt_port),
1425 session->session_type ? "UDP" : "TCP", session->session_state,
1426 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001427 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001428 }
1429
Florin Coras7e12d942018-06-27 14:32:43 -07001430 session->transport.is_ip4 = server_ep->is_ip4;
1431 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001432 clib_memcpy_fast (&session->transport.rmt_ip.ip4, server_ep->ip,
1433 sizeof (ip4_address_t));
Dave Wallaced239f8d2018-06-19 13:37:30 -04001434 else
Dave Barach178cf492018-11-13 16:34:13 -05001435 clib_memcpy_fast (&session->transport.rmt_ip.ip6, server_ep->ip,
1436 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001437 session->transport.rmt_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04001438
Florin Coras0d427d82018-06-27 03:24:07 -07001439 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server %s %U "
1440 "port %d proto %s",
Florin Coras134a9962018-08-28 11:32:04 -07001441 getpid (), session->vpp_handle, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001442 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001443 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001444 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001445 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001446 clib_net_to_host_u16 (session->transport.rmt_port),
1447 session->session_type ? "UDP" : "TCP");
Dave Wallace543852a2017-08-03 02:11:34 -04001448
Florin Coras070453d2018-08-24 17:04:27 -07001449 /*
1450 * Send connect request and wait for reply from vpp
1451 */
Florin Coras134a9962018-08-28 11:32:04 -07001452 vppcom_send_connect_sock (session);
Florin Coras070453d2018-08-24 17:04:27 -07001453 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1454 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001455
Florin Coras134a9962018-08-28 11:32:04 -07001456 session = vcl_session_get (wrk, session_index);
Dave Wallace7876d392017-10-19 03:53:57 -04001457
Florin Coras070453d2018-08-24 17:04:27 -07001458 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001459 {
Dave Wallaceee45d412017-11-24 21:44:06 -05001460 if (VPPCOM_DEBUG > 0)
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001461 {
1462 if (session)
Florin Coras0d427d82018-06-27 03:24:07 -07001463 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect "
Florin Coras134a9962018-08-28 11:32:04 -07001464 "failed! returning %d (%s)", getpid (),
1465 session->vpp_handle, session_handle, rv,
1466 vppcom_retval_str (rv));
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001467 else
1468 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
1469 "returning %d (%s)", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07001470 session_handle, rv, vppcom_retval_str (rv));
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001471 }
Dave Wallaceee45d412017-11-24 21:44:06 -05001472 }
Florin Coras0d427d82018-06-27 03:24:07 -07001473 else
1474 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
Florin Coras134a9962018-08-28 11:32:04 -07001475 getpid (), session->vpp_handle, session_handle);
Dave Wallaceee45d412017-11-24 21:44:06 -05001476
Dave Wallace4878cbe2017-11-21 03:45:09 -05001477 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001478}
1479
Florin Coras54693d22018-07-17 10:46:29 -07001480static u8
1481vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1482{
1483 if (!is_ct)
1484 return (e->event_type == FIFO_EVENT_APP_RX
1485 && e->fifo->client_session_index == sid);
1486 else
1487 return (e->event_type == SESSION_IO_EVT_CT_TX);
1488}
1489
Florin Coras460dce62018-07-27 05:45:06 -07001490static inline u8
1491vcl_session_is_readable (vcl_session_t * s)
1492{
1493 return ((s->session_state & STATE_OPEN)
1494 || (s->session_state == STATE_LISTEN
1495 && s->session_type == VPPCOM_PROTO_UDP));
1496}
1497
Steven58f464e2017-10-25 12:33:12 -07001498static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001499vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001500 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001501{
Florin Coras134a9962018-08-28 11:32:04 -07001502 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001503 int n_read = 0, rv, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001504 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001505 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001506 svm_msg_q_msg_t msg;
1507 session_event_t *e;
1508 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001509 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001510
Florin Coras070453d2018-08-24 17:04:27 -07001511 if (PREDICT_FALSE (!buf))
1512 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001513
Florin Coras134a9962018-08-28 11:32:04 -07001514 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras2cba8532018-09-11 16:33:36 -07001515 if (PREDICT_FALSE (!s || s->is_vep))
Florin Coras070453d2018-08-24 17:04:27 -07001516 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001517
Florin Coras460dce62018-07-27 05:45:06 -07001518 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001519 {
Florin Coras460dce62018-07-27 05:45:06 -07001520 session_state_t state = s->session_state;
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001521 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001522
Florin Coras0d427d82018-06-27 03:24:07 -07001523 VDBG (0, "VCL<%d>: vpp handle 0x%llx, sid %u: %s session is not open! "
1524 "state 0x%x (%s), returning %d (%s)",
Florin Coras134a9962018-08-28 11:32:04 -07001525 getpid (), s->vpp_handle, session_handle, state,
Florin Coras0d427d82018-06-27 03:24:07 -07001526 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001527 return rv;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001528 }
1529
Florin Coras2cba8532018-09-11 16:33:36 -07001530 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001531 is_ct = vcl_session_is_ct (s);
1532 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
Florin Coras2cba8532018-09-11 16:33:36 -07001533 rx_fifo = s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001534 s->has_rx_evt = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001535
Florin Coras54693d22018-07-17 10:46:29 -07001536 if (svm_fifo_is_empty (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001537 {
Florin Coras54693d22018-07-17 10:46:29 -07001538 if (is_nonblocking)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001539 {
Florin Coras41c9e042018-09-11 00:10:41 -07001540 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001541 return VPPCOM_EWOULDBLOCK;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001542 }
Florin Coras41c9e042018-09-11 00:10:41 -07001543 while (svm_fifo_is_empty (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001544 {
Florin Coras41c9e042018-09-11 00:10:41 -07001545 svm_fifo_unset_event (rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001546 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001547 if (svm_msg_q_is_empty (mq))
1548 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001549
Florin Coras54693d22018-07-17 10:46:29 -07001550 svm_msg_q_sub_w_lock (mq, &msg);
1551 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001552 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001553 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001554 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001555 svm_msg_q_free_msg (mq, &msg);
Florin Coras41c9e042018-09-11 00:10:41 -07001556
Florin Coras05ce4b82018-12-15 18:30:43 -08001557 if (PREDICT_FALSE (s->session_state == STATE_DISCONNECT))
1558 return VPPCOM_ECONNRESET;
Florin Coras54693d22018-07-17 10:46:29 -07001559 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001560 }
Florin Coras54693d22018-07-17 10:46:29 -07001561
Florin Coras460dce62018-07-27 05:45:06 -07001562 if (s->is_dgram)
Florin Coras99368312018-08-02 10:45:44 -07001563 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001564 else
Florin Coras99368312018-08-02 10:45:44 -07001565 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
Florin Coras54693d22018-07-17 10:46:29 -07001566
Florin Coras41c9e042018-09-11 00:10:41 -07001567 if (svm_fifo_is_empty (rx_fifo))
1568 svm_fifo_unset_event (rx_fifo);
1569
Florin Coras58c101a2018-10-06 13:49:16 -07001570 if (is_ct && svm_fifo_want_tx_evt (rx_fifo))
Florin Coras99368312018-08-02 10:45:44 -07001571 {
Florin Coras58c101a2018-10-06 13:49:16 -07001572 svm_fifo_set_want_tx_evt (s->rx_fifo, 0);
1573 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo, SESSION_IO_EVT_CT_RX,
1574 SVM_Q_WAIT);
Florin Coras99368312018-08-02 10:45:44 -07001575 }
Florin Coras54693d22018-07-17 10:46:29 -07001576
Florin Coras2cba8532018-09-11 16:33:36 -07001577 VDBG (2, "VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes from (%p)",
1578 getpid (), s->vpp_handle, session_handle, n_read, rx_fifo);
1579
Florin Coras54693d22018-07-17 10:46:29 -07001580 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001581}
1582
Steven58f464e2017-10-25 12:33:12 -07001583int
Florin Coras134a9962018-08-28 11:32:04 -07001584vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001585{
Florin Coras134a9962018-08-28 11:32:04 -07001586 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001587}
1588
1589static int
Florin Coras134a9962018-08-28 11:32:04 -07001590vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001591{
Florin Coras134a9962018-08-28 11:32:04 -07001592 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001593}
1594
Florin Coras2cba8532018-09-11 16:33:36 -07001595int
1596vppcom_session_read_segments (uint32_t session_handle,
1597 vppcom_data_segments_t ds)
1598{
1599 vcl_worker_t *wrk = vcl_worker_get_current ();
1600 int n_read = 0, rv, is_nonblocking;
1601 vcl_session_t *s = 0;
1602 svm_fifo_t *rx_fifo;
1603 svm_msg_q_msg_t msg;
1604 session_event_t *e;
1605 svm_msg_q_t *mq;
1606 u8 is_ct;
1607
1608 s = vcl_session_get_w_handle (wrk, session_handle);
1609 if (PREDICT_FALSE (!s || s->is_vep))
1610 return VPPCOM_EBADFD;
1611
1612 if (PREDICT_FALSE (!vcl_session_is_readable (s)))
1613 {
1614 session_state_t state = s->session_state;
1615 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1616 return rv;
1617 }
1618
1619 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1620 is_ct = vcl_session_is_ct (s);
1621 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1622 rx_fifo = s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001623 s->has_rx_evt = 0;
Florin Coras2cba8532018-09-11 16:33:36 -07001624
1625 if (svm_fifo_is_empty (rx_fifo))
1626 {
1627 if (is_nonblocking)
1628 {
1629 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001630 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001631 }
1632 while (svm_fifo_is_empty (rx_fifo))
1633 {
1634 svm_fifo_unset_event (rx_fifo);
1635 svm_msg_q_lock (mq);
1636 if (svm_msg_q_is_empty (mq))
1637 svm_msg_q_wait (mq);
1638
1639 svm_msg_q_sub_w_lock (mq, &msg);
1640 e = svm_msg_q_msg_data (mq, &msg);
1641 svm_msg_q_unlock (mq);
1642 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001643 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001644 svm_msg_q_free_msg (mq, &msg);
1645
Florin Coras05ce4b82018-12-15 18:30:43 -08001646 if (PREDICT_FALSE (s->session_state == STATE_DISCONNECT))
1647 return VPPCOM_ECONNRESET;
Florin Coras2cba8532018-09-11 16:33:36 -07001648 }
1649 }
1650
1651 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_segment_t *) ds);
1652 svm_fifo_unset_event (rx_fifo);
1653
1654 if (is_ct && n_read + svm_fifo_max_dequeue (rx_fifo) == rx_fifo->nitems)
1655 {
1656 /* If the peer is not polling send notification */
1657 if (!svm_fifo_has_event (s->rx_fifo))
1658 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo,
1659 SESSION_IO_EVT_CT_RX, SVM_Q_WAIT);
1660 }
1661
1662 return n_read;
1663}
1664
1665void
1666vppcom_session_free_segments (uint32_t session_handle,
1667 vppcom_data_segments_t ds)
1668{
1669 vcl_worker_t *wrk = vcl_worker_get_current ();
1670 vcl_session_t *s;
1671
1672 s = vcl_session_get_w_handle (wrk, session_handle);
1673 if (PREDICT_FALSE (!s || s->is_vep))
1674 return;
1675
1676 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_segment_t *) ds);
1677}
1678
Dave Wallace543852a2017-08-03 02:11:34 -04001679static inline int
Florin Coras54693d22018-07-17 10:46:29 -07001680vppcom_session_read_ready (vcl_session_t * session)
Dave Wallace543852a2017-08-03 02:11:34 -04001681{
Dave Wallace543852a2017-08-03 02:11:34 -04001682 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05001683 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001684 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001685 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
Florin Coras134a9962018-08-28 11:32:04 -07001686 "epoll session!", getpid (), session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -07001687 return VPPCOM_EBADFD;
1688 }
1689
1690 if (PREDICT_FALSE (!(session->session_state & (STATE_OPEN | STATE_LISTEN))))
1691 {
1692 session_state_t state = session->session_state;
1693 int rv;
1694
1695 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
1696
1697 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open!"
1698 " state 0x%x (%s), returning %d (%s)", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07001699 session->vpp_handle, session->session_index, state,
Florin Coras54693d22018-07-17 10:46:29 -07001700 vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
1701 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001702 }
Dave Wallace33e002b2017-09-06 01:20:02 -04001703
Florin Coras7e12d942018-06-27 14:32:43 -07001704 if (session->session_state & STATE_LISTEN)
Florin Coras54693d22018-07-17 10:46:29 -07001705 return clib_fifo_elts (session->accept_evts_fifo);
1706
1707 return svm_fifo_max_dequeue (session->rx_fifo);
1708}
1709
Florin Coras2cba8532018-09-11 16:33:36 -07001710int
1711vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1712{
1713 u32 first_copy = clib_min (ds[0].len, max_bytes);
Dave Barach178cf492018-11-13 16:34:13 -05001714 clib_memcpy_fast (buf, ds[0].data, first_copy);
Florin Coras2cba8532018-09-11 16:33:36 -07001715 if (first_copy < max_bytes)
1716 {
Dave Barach178cf492018-11-13 16:34:13 -05001717 clib_memcpy_fast (buf + first_copy, ds[1].data,
1718 clib_min (ds[1].len, max_bytes - first_copy));
Florin Coras2cba8532018-09-11 16:33:36 -07001719 }
1720 return 0;
1721}
1722
Florin Coras54693d22018-07-17 10:46:29 -07001723static u8
1724vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1725{
1726 if (!is_ct)
1727 return (e->event_type == FIFO_EVENT_APP_TX
1728 && e->fifo->client_session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04001729 else
Florin Coras54693d22018-07-17 10:46:29 -07001730 return (e->event_type == SESSION_IO_EVT_CT_RX);
Dave Wallace543852a2017-08-03 02:11:34 -04001731}
1732
Florin Coras42ceddb2018-12-12 10:56:01 -08001733static inline int
1734vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
1735 u8 is_flush)
Dave Wallace543852a2017-08-03 02:11:34 -04001736{
Florin Coras134a9962018-08-28 11:32:04 -07001737 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001738 int rv, n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001739 vcl_session_t *s = 0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001740 svm_fifo_t *tx_fifo = 0;
Florin Coras460dce62018-07-27 05:45:06 -07001741 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07001742 svm_msg_q_msg_t msg;
1743 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001744 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07001745 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001746
Florin Coras070453d2018-08-24 17:04:27 -07001747 if (PREDICT_FALSE (!buf))
1748 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001749
Florin Coras134a9962018-08-28 11:32:04 -07001750 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001751 if (PREDICT_FALSE (!s))
1752 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001753
Florin Coras460dce62018-07-27 05:45:06 -07001754 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001755 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001756 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001757 "cannot write to an epoll session!",
Florin Coras134a9962018-08-28 11:32:04 -07001758 getpid (), s->vpp_handle, session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001759
Florin Coras070453d2018-08-24 17:04:27 -07001760 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001761 }
1762
Florin Coras0e88e852018-09-17 22:09:02 -07001763 if (PREDICT_FALSE (!(s->session_state & STATE_OPEN)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001764 {
Florin Coras460dce62018-07-27 05:45:06 -07001765 session_state_t state = s->session_state;
Florin Coras54693d22018-07-17 10:46:29 -07001766 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Florin Coras0d427d82018-06-27 03:24:07 -07001767 VDBG (1, "VCL<%d>: vpp handle 0x%llx, sid %u: session is not open! "
Florin Coras0e88e852018-09-17 22:09:02 -07001768 "state 0x%x (%s)", getpid (), s->vpp_handle, session_handle,
Florin Coras0d427d82018-06-27 03:24:07 -07001769 state, vppcom_session_state_str (state));
Florin Coras070453d2018-08-24 17:04:27 -07001770 return rv;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001771 }
1772
Florin Coras0e88e852018-09-17 22:09:02 -07001773 tx_fifo = s->tx_fifo;
1774 is_ct = vcl_session_is_ct (s);
1775 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1776 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
Florin Coras54693d22018-07-17 10:46:29 -07001777 if (svm_fifo_is_full (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04001778 {
Florin Coras54693d22018-07-17 10:46:29 -07001779 if (is_nonblocking)
1780 {
Florin Coras070453d2018-08-24 17:04:27 -07001781 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07001782 }
Florin Coras60f1fc12018-08-16 14:57:31 -07001783 while (svm_fifo_is_full (tx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001784 {
Florin Coras0e88e852018-09-17 22:09:02 -07001785 svm_fifo_set_want_tx_evt (tx_fifo, 1);
Florin Coras99368312018-08-02 10:45:44 -07001786 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07001787 if (svm_msg_q_is_empty (mq))
1788 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07001789
Florin Coras54693d22018-07-17 10:46:29 -07001790 svm_msg_q_sub_w_lock (mq, &msg);
1791 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001792 svm_msg_q_unlock (mq);
1793
Florin Coras0e88e852018-09-17 22:09:02 -07001794 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07001795 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001796 svm_msg_q_free_msg (mq, &msg);
Florin Coras05ce4b82018-12-15 18:30:43 -08001797
1798 if (PREDICT_FALSE (!(s->session_state & STATE_OPEN)))
1799 return VPPCOM_ECONNRESET;
Florin Coras54693d22018-07-17 10:46:29 -07001800 }
Dave Wallace543852a2017-08-03 02:11:34 -04001801 }
Dave Wallace543852a2017-08-03 02:11:34 -04001802
Florin Coras460dce62018-07-27 05:45:06 -07001803 ASSERT (FIFO_EVENT_APP_TX + 1 == SESSION_IO_EVT_CT_TX);
1804 et = FIFO_EVENT_APP_TX + vcl_session_is_ct (s);
Florin Coras42ceddb2018-12-12 10:56:01 -08001805 if (is_flush && !vcl_session_is_ct (s))
1806 et = SESSION_IO_EVT_TX_FLUSH;
1807
Florin Coras460dce62018-07-27 05:45:06 -07001808 if (s->is_dgram)
1809 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
1810 s->vpp_evt_q, buf, n, et, SVM_Q_WAIT);
1811 else
1812 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
1813 SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001814
Florin Coras460dce62018-07-27 05:45:06 -07001815 ASSERT (n_write > 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001816
Florin Coras0e88e852018-09-17 22:09:02 -07001817 VDBG (2, "VCL<%d>: vpp handle 0x%llx, sid %u: wrote %d bytes", getpid (),
1818 s->vpp_handle, session_handle, n_write);
1819
Florin Coras54693d22018-07-17 10:46:29 -07001820 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04001821}
1822
Florin Coras42ceddb2018-12-12 10:56:01 -08001823int
1824vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1825{
1826 return vppcom_session_write_inline (session_handle, buf, n,
1827 0 /* is_flush */ );
1828}
1829
Florin Coras99368312018-08-02 10:45:44 -07001830static vcl_session_t *
Florin Coras134a9962018-08-28 11:32:04 -07001831vcl_ct_session_get_from_fifo (vcl_worker_t * wrk, svm_fifo_t * f, u8 type)
Florin Coras99368312018-08-02 10:45:44 -07001832{
1833 vcl_session_t *s;
Florin Coras134a9962018-08-28 11:32:04 -07001834 s = vcl_session_get (wrk, f->client_session_index);
Florin Coras99368312018-08-02 10:45:44 -07001835 if (s)
1836 {
1837 /* rx fifo */
1838 if (type == 0 && s->rx_fifo == f)
1839 return s;
1840 /* tx fifo */
1841 if (type == 1 && s->tx_fifo == f)
1842 return s;
1843 }
Florin Coras134a9962018-08-28 11:32:04 -07001844 s = vcl_session_get (wrk, f->master_session_index);
Florin Coras99368312018-08-02 10:45:44 -07001845 if (s)
1846 {
1847 if (type == 0 && s->rx_fifo == f)
1848 return s;
1849 if (type == 1 && s->tx_fifo == f)
1850 return s;
1851 }
1852 return 0;
1853}
1854
Dave Wallace543852a2017-08-03 02:11:34 -04001855static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001856vppcom_session_write_ready (vcl_session_t * session)
Dave Wallace543852a2017-08-03 02:11:34 -04001857{
Dave Wallace543852a2017-08-03 02:11:34 -04001858 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Dave Wallace4878cbe2017-11-21 03:45:09 -05001859 if (PREDICT_FALSE (session->is_vep))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001860 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001861 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001862 "cannot write to an epoll session!",
Florin Coras134a9962018-08-28 11:32:04 -07001863 getpid (), session->vpp_handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -07001864 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001865 }
1866
Florin Coras7e12d942018-06-27 14:32:43 -07001867 if (PREDICT_FALSE (session->session_state & STATE_LISTEN))
Dave Wallace33e002b2017-09-06 01:20:02 -04001868 {
Dave Wallace048b1d62018-01-03 22:24:41 -05001869 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Dave Wallaceee45d412017-11-24 21:44:06 -05001870 "cannot write to a listen session!",
Florin Coras134a9962018-08-28 11:32:04 -07001871 getpid (), session->vpp_handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -07001872 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001873 }
1874
Florin Coras54693d22018-07-17 10:46:29 -07001875 if (PREDICT_FALSE (!(session->session_state & STATE_OPEN)))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001876 {
Florin Coras7e12d942018-06-27 14:32:43 -07001877 session_state_t state = session->session_state;
Florin Coras54693d22018-07-17 10:46:29 -07001878 int rv;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001879
Keith Burns (alagalah)56a0d062018-02-15 07:52:50 -08001880 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
Dave Wallace048b1d62018-01-03 22:24:41 -05001881 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001882 "session is not open! state 0x%x (%s), "
Dave Wallaceee45d412017-11-24 21:44:06 -05001883 "returning %d (%s)", getpid (), session->vpp_handle,
Florin Coras134a9962018-08-28 11:32:04 -07001884 session->session_index,
Dave Wallace4878cbe2017-11-21 03:45:09 -05001885 state, vppcom_session_state_str (state),
1886 rv, vppcom_retval_str (rv));
Florin Coras54693d22018-07-17 10:46:29 -07001887 return rv;
Dave Wallace33e002b2017-09-06 01:20:02 -04001888 }
1889
Florin Coras0d427d82018-06-27 03:24:07 -07001890 VDBG (3, "VCL<%d>: vpp handle 0x%llx, sid %u: peek %s (%p), ready = %d",
Florin Coras134a9962018-08-28 11:32:04 -07001891 getpid (), session->vpp_handle, session->session_index,
1892 session->tx_fifo, svm_fifo_max_enqueue (session->tx_fifo));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001893
Florin Coras54693d22018-07-17 10:46:29 -07001894 return svm_fifo_max_enqueue (session->tx_fifo);
1895}
1896
Florin Coras99368312018-08-02 10:45:44 -07001897static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001898vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq)
Florin Coras99368312018-08-02 10:45:44 -07001899{
1900 svm_msg_q_msg_t *msg;
1901 u32 n_msgs;
1902 int i;
1903
1904 n_msgs = svm_msg_q_size (mq);
1905 for (i = 0; i < n_msgs; i++)
1906 {
Florin Coras134a9962018-08-28 11:32:04 -07001907 vec_add2 (wrk->mq_msg_vector, msg, 1);
Florin Coras99368312018-08-02 10:45:44 -07001908 svm_msg_q_sub_w_lock (mq, msg);
1909 }
1910 return n_msgs;
1911}
1912
Florin Coras6d4bb422018-09-04 22:07:27 -07001913#define vcl_fifo_rx_evt_valid_or_break(_fifo) \
1914if (PREDICT_FALSE (svm_fifo_is_empty (_fifo))) \
1915 { \
1916 svm_fifo_unset_event (_fifo); \
1917 if (svm_fifo_is_empty (_fifo)) \
Florin Coras41c9e042018-09-11 00:10:41 -07001918 break; \
Florin Coras6d4bb422018-09-04 22:07:27 -07001919 } \
1920
Florin Coras86f04502018-09-12 16:08:01 -07001921static void
1922vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1923 unsigned long n_bits, unsigned long *read_map,
1924 unsigned long *write_map,
1925 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07001926{
1927 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07001928 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07001929 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07001930 u32 sid;
1931
1932 switch (e->event_type)
1933 {
1934 case FIFO_EVENT_APP_RX:
1935 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1936 sid = e->fifo->client_session_index;
1937 session = vcl_session_get (wrk, sid);
1938 if (!session)
1939 break;
1940 if (sid < n_bits && read_map)
1941 {
1942 clib_bitmap_set_no_check (read_map, sid, 1);
1943 *bits_set += 1;
1944 }
1945 break;
1946 case FIFO_EVENT_APP_TX:
1947 sid = e->fifo->client_session_index;
1948 session = vcl_session_get (wrk, sid);
1949 if (!session)
1950 break;
1951 if (sid < n_bits && write_map)
1952 {
1953 clib_bitmap_set_no_check (write_map, sid, 1);
1954 *bits_set += 1;
1955 }
1956 break;
1957 case SESSION_IO_EVT_CT_TX:
1958 vcl_fifo_rx_evt_valid_or_break (e->fifo);
1959 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
1960 if (!session)
1961 break;
1962 sid = session->session_index;
1963 if (sid < n_bits && read_map)
1964 {
1965 clib_bitmap_set_no_check (read_map, sid, 1);
1966 *bits_set += 1;
1967 }
1968 break;
1969 case SESSION_IO_EVT_CT_RX:
1970 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
1971 if (!session)
1972 break;
1973 sid = session->session_index;
1974 if (sid < n_bits && write_map)
1975 {
1976 clib_bitmap_set_no_check (write_map, sid, 1);
1977 *bits_set += 1;
1978 }
1979 break;
1980 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08001981 session = vcl_session_accepted (wrk,
1982 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07001983 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08001984 break;
Florin Coras86f04502018-09-12 16:08:01 -07001985 sid = session->session_index;
1986 if (sid < n_bits && read_map)
1987 {
1988 clib_bitmap_set_no_check (read_map, sid, 1);
1989 *bits_set += 1;
1990 }
1991 break;
1992 case SESSION_CTRL_EVT_CONNECTED:
1993 connected_msg = (session_connected_msg_t *) e->data;
1994 vcl_session_connected_handler (wrk, connected_msg);
1995 break;
1996 case SESSION_CTRL_EVT_DISCONNECTED:
1997 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001998 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
1999 if (!session)
2000 break;
2001 sid = session->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07002002 if (sid < n_bits && except_map)
2003 {
2004 clib_bitmap_set_no_check (except_map, sid, 1);
2005 *bits_set += 1;
2006 }
2007 break;
2008 case SESSION_CTRL_EVT_RESET:
2009 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2010 if (sid < n_bits && except_map)
2011 {
2012 clib_bitmap_set_no_check (except_map, sid, 1);
2013 *bits_set += 1;
2014 }
2015 break;
2016 default:
2017 clib_warning ("unhandled: %u", e->event_type);
2018 break;
2019 }
2020}
2021
2022static int
2023vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2024 unsigned long n_bits, unsigned long *read_map,
2025 unsigned long *write_map, unsigned long *except_map,
2026 double time_to_wait, u32 * bits_set)
2027{
Florin Coras99368312018-08-02 10:45:44 -07002028 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002029 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07002030 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07002031
2032 svm_msg_q_lock (mq);
2033 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002034 {
Florin Coras54693d22018-07-17 10:46:29 -07002035 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05002036 {
Florin Coras54693d22018-07-17 10:46:29 -07002037 svm_msg_q_unlock (mq);
2038 return 0;
2039 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002040
Florin Coras54693d22018-07-17 10:46:29 -07002041 if (!time_to_wait)
2042 {
2043 svm_msg_q_unlock (mq);
2044 return 0;
2045 }
2046 else if (time_to_wait < 0)
2047 {
2048 svm_msg_q_wait (mq);
2049 }
2050 else
2051 {
2052 if (svm_msg_q_timedwait (mq, time_to_wait))
2053 {
2054 svm_msg_q_unlock (mq);
2055 return 0;
2056 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002057 }
2058 }
Florin Coras134a9962018-08-28 11:32:04 -07002059 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07002060 svm_msg_q_unlock (mq);
2061
Florin Coras134a9962018-08-28 11:32:04 -07002062 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002063 {
Florin Coras134a9962018-08-28 11:32:04 -07002064 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002065 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07002066 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
2067 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002068 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002069 }
Florin Coras134a9962018-08-28 11:32:04 -07002070 vec_reset_length (wrk->mq_msg_vector);
Florin Coras54693d22018-07-17 10:46:29 -07002071 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04002072}
2073
Florin Coras99368312018-08-02 10:45:44 -07002074static int
Florin Coras134a9962018-08-28 11:32:04 -07002075vppcom_select_condvar (vcl_worker_t * wrk, unsigned long n_bits,
2076 unsigned long *read_map, unsigned long *write_map,
2077 unsigned long *except_map, double time_to_wait,
2078 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002079{
2080 double total_wait = 0, wait_slice;
2081 vcl_cut_through_registration_t *cr;
2082
2083 time_to_wait = (time_to_wait == -1) ? 10e9 : time_to_wait;
Florin Coras134a9962018-08-28 11:32:04 -07002084 wait_slice = wrk->cut_through_registrations ? 10e-6 : time_to_wait;
Florin Coras99368312018-08-02 10:45:44 -07002085 do
2086 {
Florin Coras134a9962018-08-28 11:32:04 -07002087 vcl_ct_registration_lock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07002088 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002089 pool_foreach (cr, wrk->cut_through_registrations, ({
2090 vcl_select_handle_mq (wrk, cr->mq, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002091 0, bits_set);
2092 }));
2093 /* *INDENT-ON* */
Florin Coras134a9962018-08-28 11:32:04 -07002094 vcl_ct_registration_unlock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07002095
Florin Coras134a9962018-08-28 11:32:04 -07002096 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
2097 write_map, except_map, time_to_wait, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07002098 total_wait += wait_slice;
2099 if (*bits_set)
2100 return *bits_set;
2101 }
2102 while (total_wait < time_to_wait);
2103
2104 return 0;
2105}
2106
2107static int
Florin Coras134a9962018-08-28 11:32:04 -07002108vppcom_select_eventfd (vcl_worker_t * wrk, unsigned long n_bits,
2109 unsigned long *read_map, unsigned long *write_map,
2110 unsigned long *except_map, double time_to_wait,
2111 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07002112{
2113 vcl_mq_evt_conn_t *mqc;
2114 int __clib_unused n_read;
2115 int n_mq_evts, i;
2116 u64 buf;
2117
Florin Coras134a9962018-08-28 11:32:04 -07002118 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
2119 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2120 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07002121 for (i = 0; i < n_mq_evts; i++)
2122 {
Florin Coras134a9962018-08-28 11:32:04 -07002123 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002124 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002125 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07002126 except_map, 0, bits_set);
2127 }
2128
2129 return (n_mq_evts > 0 ? (int) *bits_set : 0);
2130}
2131
Dave Wallace543852a2017-08-03 02:11:34 -04002132int
2133vppcom_select (unsigned long n_bits, unsigned long *read_map,
2134 unsigned long *write_map, unsigned long *except_map,
2135 double time_to_wait)
2136{
Florin Coras54693d22018-07-17 10:46:29 -07002137 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07002138 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002139 vcl_session_t *session = 0;
Florin Coras86f04502018-09-12 16:08:01 -07002140 int rv, i;
Dave Wallace543852a2017-08-03 02:11:34 -04002141
2142 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2143
Dave Wallace7876d392017-10-19 03:53:57 -04002144 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002145 {
Florin Coras134a9962018-08-28 11:32:04 -07002146 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002147 clib_memcpy_fast (wrk->rd_bitmap, read_map,
2148 vec_len (wrk->rd_bitmap) * sizeof (clib_bitmap_t));
Florin Coras134a9962018-08-28 11:32:04 -07002149 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04002150 }
Dave Wallace7876d392017-10-19 03:53:57 -04002151 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002152 {
Florin Coras134a9962018-08-28 11:32:04 -07002153 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002154 clib_memcpy_fast (wrk->wr_bitmap, write_map,
2155 vec_len (wrk->wr_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05002156 memset (write_map, 0,
Florin Coras134a9962018-08-28 11:32:04 -07002157 vec_len (wrk->wr_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04002158 }
Dave Wallace7876d392017-10-19 03:53:57 -04002159 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04002160 {
Florin Coras134a9962018-08-28 11:32:04 -07002161 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05002162 clib_memcpy_fast (wrk->ex_bitmap, except_map,
2163 vec_len (wrk->ex_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05002164 memset (except_map, 0,
Florin Coras134a9962018-08-28 11:32:04 -07002165 vec_len (wrk->ex_bitmap) * sizeof (clib_bitmap_t));
Dave Wallace543852a2017-08-03 02:11:34 -04002166 }
2167
Florin Coras54693d22018-07-17 10:46:29 -07002168 if (!n_bits)
2169 return 0;
2170
2171 if (!write_map)
2172 goto check_rd;
2173
2174 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002175 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2176 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002177 {
Florin Coras47c40e22018-11-26 17:01:36 -08002178 if (except_map && sid < minbits)
2179 clib_bitmap_set_no_check (except_map, sid, 1);
2180 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002181 }
2182
2183 rv = svm_fifo_is_full (session->tx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07002184 if (!rv)
2185 {
2186 clib_bitmap_set_no_check (write_map, sid, 1);
2187 bits_set++;
2188 }
2189 }));
2190
2191check_rd:
2192 if (!read_map)
2193 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002194
Florin Coras134a9962018-08-28 11:32:04 -07002195 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2196 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002197 {
Florin Coras47c40e22018-11-26 17:01:36 -08002198 if (except_map && sid < minbits)
2199 clib_bitmap_set_no_check (except_map, sid, 1);
2200 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002201 }
2202
2203 rv = vppcom_session_read_ready (session);
Florin Coras54693d22018-07-17 10:46:29 -07002204 if (rv)
2205 {
2206 clib_bitmap_set_no_check (read_map, sid, 1);
2207 bits_set++;
2208 }
2209 }));
2210 /* *INDENT-ON* */
2211
2212check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002213
Florin Coras86f04502018-09-12 16:08:01 -07002214 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2215 {
2216 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2217 read_map, write_map, except_map, &bits_set);
2218 }
2219 vec_reset_length (wrk->unhandled_evts_vector);
2220
Florin Coras99368312018-08-02 10:45:44 -07002221 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002222 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002223 time_to_wait, &bits_set);
2224 else
Florin Coras134a9962018-08-28 11:32:04 -07002225 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002226 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002227
Dave Wallace543852a2017-08-03 02:11:34 -04002228 return (bits_set);
2229}
2230
Dave Wallacef7f809c2017-10-03 01:48:42 -04002231static inline void
Florin Coras134a9962018-08-28 11:32:04 -07002232vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002233{
Florin Coras7e12d942018-06-27 14:32:43 -07002234 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002235 vppcom_epoll_t *vep;
Dave Wallace498b3a52017-11-09 13:00:34 -05002236 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002237
Dave Wallace498b3a52017-11-09 13:00:34 -05002238 if (VPPCOM_DEBUG <= 1)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002239 return;
2240
2241 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
Florin Coras134a9962018-08-28 11:32:04 -07002242 session = vcl_session_get (wrk, vep_idx);
Florin Coras070453d2018-08-24 17:04:27 -07002243 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002244 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002245 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
2246 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002247 goto done;
2248 }
2249 if (PREDICT_FALSE (!session->is_vep))
2250 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002251 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
2252 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002253 goto done;
2254 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002255 vep = &session->vep;
Dave Wallace048b1d62018-01-03 22:24:41 -05002256 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05002257 "{\n"
2258 " is_vep = %u\n"
2259 " is_vep_session = %u\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05002260 " next_sid = 0x%x (%u)\n"
Dave Wallace498b3a52017-11-09 13:00:34 -05002261 " wait_cont_idx = 0x%x (%u)\n"
Dave Wallace4878cbe2017-11-21 03:45:09 -05002262 "}\n", getpid (), vep_idx,
2263 session->is_vep, session->is_vep_session,
Florin Coras134a9962018-08-28 11:32:04 -07002264 vep->next_sh, vep->next_sh,
Dave Wallace498b3a52017-11-09 13:00:34 -05002265 session->wait_cont_idx, session->wait_cont_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002266
Florin Coras134a9962018-08-28 11:32:04 -07002267 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002268 {
Florin Coras134a9962018-08-28 11:32:04 -07002269 session = vcl_session_get (wrk, sid);
Florin Coras070453d2018-08-24 17:04:27 -07002270 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002271 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002272 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002273 goto done;
2274 }
2275 if (PREDICT_FALSE (session->is_vep))
Dave Wallace048b1d62018-01-03 22:24:41 -05002276 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
2277 getpid (), vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002278 else if (PREDICT_FALSE (!session->is_vep_session))
2279 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002280 clib_warning ("VCL<%d>: ERROR: session (%u) "
2281 "is not a vep session!", getpid (), sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002282 goto done;
2283 }
2284 vep = &session->vep;
Florin Coras134a9962018-08-28 11:32:04 -07002285 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
Dave Wallace048b1d62018-01-03 22:24:41 -05002286 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002287 "vep_idx (%u)!", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002288 sid, session->vep.vep_sh, vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002289 if (session->is_vep_session)
2290 {
2291 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2292 "{\n"
2293 " next_sid = 0x%x (%u)\n"
2294 " prev_sid = 0x%x (%u)\n"
2295 " vep_idx = 0x%x (%u)\n"
2296 " ev.events = 0x%x\n"
2297 " ev.data.u64 = 0x%llx\n"
2298 " et_mask = 0x%x\n"
2299 "}\n",
2300 vep_idx, sid, sid,
Florin Coras134a9962018-08-28 11:32:04 -07002301 vep->next_sh, vep->next_sh,
2302 vep->prev_sh, vep->prev_sh,
2303 vep->vep_sh, vep->vep_sh,
Dave Wallace4878cbe2017-11-21 03:45:09 -05002304 vep->ev.events, vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002305 }
2306 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002307
2308done:
Dave Wallace048b1d62018-01-03 22:24:41 -05002309 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
2310 getpid (), vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002311}
2312
2313int
2314vppcom_epoll_create (void)
2315{
Florin Coras134a9962018-08-28 11:32:04 -07002316 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002317 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002318
Florin Coras134a9962018-08-28 11:32:04 -07002319 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002320
2321 vep_session->is_vep = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002322 vep_session->vep.vep_sh = ~0;
2323 vep_session->vep.next_sh = ~0;
2324 vep_session->vep.prev_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002325 vep_session->wait_cont_idx = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002326 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002327
Florin Coras134a9962018-08-28 11:32:04 -07002328 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_sh);
Florin Coras0d427d82018-06-27 03:24:07 -07002329 VDBG (0, "VCL<%d>: Created vep_idx %u / sid %u!",
Florin Coras134a9962018-08-28 11:32:04 -07002330 getpid (), vep_session->session_index, vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002331
Florin Corasab2f6db2018-08-31 14:31:41 -07002332 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002333}
2334
2335int
Florin Coras134a9962018-08-28 11:32:04 -07002336vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002337 struct epoll_event *event)
2338{
Florin Coras134a9962018-08-28 11:32:04 -07002339 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002340 vcl_session_t *vep_session;
2341 vcl_session_t *session;
Florin Coras070453d2018-08-24 17:04:27 -07002342 int rv = VPPCOM_OK;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002343
Florin Coras134a9962018-08-28 11:32:04 -07002344 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002345 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002346 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002347 getpid (), vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002348 return VPPCOM_EINVAL;
2349 }
2350
Florin Coras134a9962018-08-28 11:32:04 -07002351 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002352 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002353 {
Florin Coras134a9962018-08-28 11:32:04 -07002354 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002355 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002356 }
2357 if (PREDICT_FALSE (!vep_session->is_vep))
2358 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002359 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Florin Coras134a9962018-08-28 11:32:04 -07002360 getpid (), vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002361 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002362 }
2363
Florin Coras134a9962018-08-28 11:32:04 -07002364 ASSERT (vep_session->vep.vep_sh == ~0);
2365 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002366
Florin Coras134a9962018-08-28 11:32:04 -07002367 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002368 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002369 {
Florin Coras134a9962018-08-28 11:32:04 -07002370 VDBG (0, "VCL<%d>: ERROR: Invalid session_handle (%u)!",
2371 getpid (), session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002372 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002373 }
2374 if (PREDICT_FALSE (session->is_vep))
2375 {
Florin Coras134a9962018-08-28 11:32:04 -07002376 clib_warning ("ERROR: session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002377 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002378 }
2379
2380 switch (op)
2381 {
2382 case EPOLL_CTL_ADD:
2383 if (PREDICT_FALSE (!event))
2384 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002385 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002386 "epoll_event structure!", getpid ());
Florin Coras070453d2018-08-24 17:04:27 -07002387 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002388 }
Florin Coras134a9962018-08-28 11:32:04 -07002389 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002390 {
Florin Coras7e12d942018-06-27 14:32:43 -07002391 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002392 next_session = vcl_session_get_w_handle (wrk,
2393 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002394 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002395 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002396 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002397 "vep.next_sid (%u) on vep_idx (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002398 getpid (), vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002399 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002400 }
Florin Coras134a9962018-08-28 11:32:04 -07002401 ASSERT (next_session->vep.prev_sh == vep_handle);
2402 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002403 }
Florin Coras134a9962018-08-28 11:32:04 -07002404 session->vep.next_sh = vep_session->vep.next_sh;
2405 session->vep.prev_sh = vep_handle;
2406 session->vep.vep_sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002407 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2408 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002409 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002410 session->is_vep_session = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002411 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002412
Florin Coras070453d2018-08-24 17:04:27 -07002413 VDBG (1, "VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x, "
Florin Coras134a9962018-08-28 11:32:04 -07002414 "data 0x%llx!", getpid (), vep_handle, session_handle,
2415 event->events, event->data.u64);
Florin Coras0d427d82018-06-27 03:24:07 -07002416 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002417 break;
2418
2419 case EPOLL_CTL_MOD:
2420 if (PREDICT_FALSE (!event))
2421 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002422 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
Dave Wallace2e005bb2017-11-07 01:21:39 -05002423 "epoll_event structure!", getpid ());
Dave Wallacef7f809c2017-10-03 01:48:42 -04002424 rv = VPPCOM_EINVAL;
2425 goto done;
2426 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002427 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002428 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002429 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Florin Coras134a9962018-08-28 11:32:04 -07002430 "not a vep session!", getpid (), session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002431 rv = VPPCOM_EINVAL;
2432 goto done;
2433 }
Florin Coras134a9962018-08-28 11:32:04 -07002434 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002435 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002436 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002437 "vep_idx (%u) != vep_idx (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002438 getpid (), session_handle,
2439 session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002440 rv = VPPCOM_EINVAL;
2441 goto done;
2442 }
2443 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2444 session->vep.ev = *event;
Florin Coras0d427d82018-06-27 03:24:07 -07002445 VDBG (1, "VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
Florin Coras134a9962018-08-28 11:32:04 -07002446 " data 0x%llx!", getpid (), vep_handle, session_handle,
2447 event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002448 break;
2449
2450 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05002451 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002452 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002453 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Florin Coras134a9962018-08-28 11:32:04 -07002454 "not a vep session!", getpid (), session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002455 rv = VPPCOM_EINVAL;
2456 goto done;
2457 }
Florin Coras134a9962018-08-28 11:32:04 -07002458 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002459 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002460 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002461 "vep_idx (%u) != vep_idx (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002462 getpid (), session_handle,
2463 session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002464 rv = VPPCOM_EINVAL;
2465 goto done;
2466 }
2467
2468 vep_session->wait_cont_idx =
Florin Coras134a9962018-08-28 11:32:04 -07002469 (vep_session->wait_cont_idx == session_handle) ?
2470 session->vep.next_sh : vep_session->wait_cont_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002471
Florin Coras134a9962018-08-28 11:32:04 -07002472 if (session->vep.prev_sh == vep_handle)
2473 vep_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002474 else
2475 {
Florin Coras7e12d942018-06-27 14:32:43 -07002476 vcl_session_t *prev_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002477 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002478 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002479 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002480 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002481 "vep.prev_sid (%u) on sid (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002482 getpid (), session->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002483 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002484 }
Florin Coras134a9962018-08-28 11:32:04 -07002485 ASSERT (prev_session->vep.next_sh == session_handle);
2486 prev_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002487 }
Florin Coras134a9962018-08-28 11:32:04 -07002488 if (session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002489 {
Florin Coras7e12d942018-06-27 14:32:43 -07002490 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002491 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002492 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002493 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002494 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
Dave Wallace4878cbe2017-11-21 03:45:09 -05002495 "vep.next_sid (%u) on sid (%u)!",
Florin Coras134a9962018-08-28 11:32:04 -07002496 getpid (), session->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002497 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002498 }
Florin Coras134a9962018-08-28 11:32:04 -07002499 ASSERT (next_session->vep.prev_sh == session_handle);
2500 next_session->vep.prev_sh = session->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002501 }
2502
2503 memset (&session->vep, 0, sizeof (session->vep));
Florin Coras134a9962018-08-28 11:32:04 -07002504 session->vep.next_sh = ~0;
2505 session->vep.prev_sh = ~0;
2506 session->vep.vep_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002507 session->is_vep_session = 0;
Florin Coras0d427d82018-06-27 03:24:07 -07002508 VDBG (1, "VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
Florin Coras134a9962018-08-28 11:32:04 -07002509 getpid (), vep_handle, session_handle);
2510 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002511 break;
2512
2513 default:
Dave Wallace048b1d62018-01-03 22:24:41 -05002514 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002515 rv = VPPCOM_EINVAL;
2516 }
2517
Florin Coras134a9962018-08-28 11:32:04 -07002518 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002519
2520done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002521 return rv;
2522}
2523
Florin Coras86f04502018-09-12 16:08:01 -07002524static inline void
2525vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2526 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002527{
2528 session_disconnected_msg_t *disconnected_msg;
2529 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002530 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002531 u64 session_evt_data = ~0;
Florin Coras54693d22018-07-17 10:46:29 -07002532 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002533 u8 add_event = 0;
2534
2535 switch (e->event_type)
2536 {
2537 case FIFO_EVENT_APP_RX:
2538 ASSERT (e->fifo->client_thread_index == vcl_get_worker_index ());
2539 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2540 sid = e->fifo->client_session_index;
2541 session = vcl_session_get (wrk, sid);
2542 session_events = session->vep.ev.events;
Florin Corasaa27eb92018-10-13 12:20:01 -07002543 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
Florin Coras86f04502018-09-12 16:08:01 -07002544 break;
2545 add_event = 1;
2546 events[*num_ev].events |= EPOLLIN;
2547 session_evt_data = session->vep.ev.data.u64;
Florin Corasaa27eb92018-10-13 12:20:01 -07002548 session->has_rx_evt = 1;
Florin Coras86f04502018-09-12 16:08:01 -07002549 break;
2550 case FIFO_EVENT_APP_TX:
2551 sid = e->fifo->client_session_index;
2552 session = vcl_session_get (wrk, sid);
2553 session_events = session->vep.ev.events;
2554 if (!(EPOLLOUT & session_events))
2555 break;
2556 add_event = 1;
2557 events[*num_ev].events |= EPOLLOUT;
2558 session_evt_data = session->vep.ev.data.u64;
2559 break;
2560 case SESSION_IO_EVT_CT_TX:
2561 vcl_fifo_rx_evt_valid_or_break (e->fifo);
2562 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 0);
2563 sid = session->session_index;
2564 session_events = session->vep.ev.events;
Florin Corasaa27eb92018-10-13 12:20:01 -07002565 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
Florin Coras86f04502018-09-12 16:08:01 -07002566 break;
2567 add_event = 1;
2568 events[*num_ev].events |= EPOLLIN;
2569 session_evt_data = session->vep.ev.data.u64;
Florin Corasaa27eb92018-10-13 12:20:01 -07002570 session->has_rx_evt = 1;
Florin Coras86f04502018-09-12 16:08:01 -07002571 break;
2572 case SESSION_IO_EVT_CT_RX:
2573 session = vcl_ct_session_get_from_fifo (wrk, e->fifo, 1);
2574 sid = session->session_index;
2575 session_events = session->vep.ev.events;
2576 if (!(EPOLLOUT & session_events))
2577 break;
2578 add_event = 1;
2579 events[*num_ev].events |= EPOLLOUT;
2580 session_evt_data = session->vep.ev.data.u64;
2581 break;
2582 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002583 session = vcl_session_accepted (wrk,
2584 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002585 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002586 break;
Florin Coras86f04502018-09-12 16:08:01 -07002587
Florin Coras86f04502018-09-12 16:08:01 -07002588 session_events = session->vep.ev.events;
2589 if (!(EPOLLIN & session_events))
2590 break;
2591
2592 add_event = 1;
2593 events[*num_ev].events |= EPOLLIN;
2594 session_evt_data = session->vep.ev.data.u64;
2595 break;
2596 case SESSION_CTRL_EVT_CONNECTED:
2597 connected_msg = (session_connected_msg_t *) e->data;
2598 vcl_session_connected_handler (wrk, connected_msg);
2599 /* Generate EPOLLOUT because there's no connected event */
2600 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
2601 session = vcl_session_get (wrk, sid);
2602 session_events = session->vep.ev.events;
2603 if (EPOLLOUT & session_events)
2604 {
2605 add_event = 1;
2606 events[*num_ev].events |= EPOLLOUT;
2607 session_evt_data = session->vep.ev.data.u64;
2608 }
2609 break;
2610 case SESSION_CTRL_EVT_DISCONNECTED:
2611 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002612 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2613 if (!session)
Florin Coras86f04502018-09-12 16:08:01 -07002614 break;
2615 add_event = 1;
2616 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2617 session_evt_data = session->vep.ev.data.u64;
2618 session_events = session->vep.ev.events;
2619 break;
2620 case SESSION_CTRL_EVT_RESET:
2621 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2622 if (!(session = vcl_session_get (wrk, sid)))
2623 break;
2624 add_event = 1;
2625 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2626 session_evt_data = session->vep.ev.data.u64;
2627 session_events = session->vep.ev.events;
2628 break;
2629 default:
2630 VDBG (0, "unhandled: %u", e->event_type);
2631 break;
2632 }
2633
2634 if (add_event)
2635 {
2636 events[*num_ev].data.u64 = session_evt_data;
2637 if (EPOLLONESHOT & session_events)
2638 {
2639 session = vcl_session_get (wrk, sid);
2640 session->vep.ev.events = 0;
2641 }
2642 *num_ev += 1;
2643 }
2644}
2645
2646static int
2647vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2648 struct epoll_event *events, u32 maxevents,
2649 double wait_for_time, u32 * num_ev)
2650{
Florin Coras99368312018-08-02 10:45:44 -07002651 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002652 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002653 int i;
2654
Florin Coras539663c2018-09-28 14:59:37 -07002655 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2656 goto handle_dequeued;
2657
Florin Coras54693d22018-07-17 10:46:29 -07002658 svm_msg_q_lock (mq);
2659 if (svm_msg_q_is_empty (mq))
2660 {
2661 if (!wait_for_time)
2662 {
2663 svm_msg_q_unlock (mq);
2664 return 0;
2665 }
2666 else if (wait_for_time < 0)
2667 {
2668 svm_msg_q_wait (mq);
2669 }
2670 else
2671 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002672 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002673 {
2674 svm_msg_q_unlock (mq);
2675 return 0;
2676 }
2677 }
2678 }
Florin Coras134a9962018-08-28 11:32:04 -07002679 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07002680 svm_msg_q_unlock (mq);
2681
Florin Coras539663c2018-09-28 14:59:37 -07002682handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002683 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002684 {
Florin Coras134a9962018-08-28 11:32:04 -07002685 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002686 e = svm_msg_q_msg_data (mq, msg);
Florin Corasaa27eb92018-10-13 12:20:01 -07002687 if (*num_ev < maxevents)
2688 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2689 else
2690 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras99368312018-08-02 10:45:44 -07002691 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002692 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002693 vec_reset_length (wrk->mq_msg_vector);
Florin Coras86f04502018-09-12 16:08:01 -07002694
Florin Coras54693d22018-07-17 10:46:29 -07002695 return *num_ev;
2696}
2697
Florin Coras99368312018-08-02 10:45:44 -07002698static int
Florin Coras134a9962018-08-28 11:32:04 -07002699vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002700 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002701{
2702 vcl_cut_through_registration_t *cr;
2703 double total_wait = 0, wait_slice;
Florin Coras99368312018-08-02 10:45:44 -07002704 int rv;
2705
2706 wait_for_time = (wait_for_time == -1) ? (double) 10e9 : wait_for_time;
Florin Coras134a9962018-08-28 11:32:04 -07002707 wait_slice = wrk->cut_through_registrations ? 10e-6 : wait_for_time;
Florin Coras99368312018-08-02 10:45:44 -07002708
2709 do
2710 {
Florin Coras134a9962018-08-28 11:32:04 -07002711 vcl_ct_registration_lock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07002712 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002713 pool_foreach (cr, wrk->cut_through_registrations, ({
Florin Coras86f04502018-09-12 16:08:01 -07002714 vcl_epoll_wait_handle_mq (wrk, cr->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002715 }));
2716 /* *INDENT-ON* */
Florin Coras134a9962018-08-28 11:32:04 -07002717 vcl_ct_registration_unlock (wrk);
Florin Coras99368312018-08-02 10:45:44 -07002718
Florin Coras134a9962018-08-28 11:32:04 -07002719 rv = vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events,
Florin Coras86f04502018-09-12 16:08:01 -07002720 maxevents, n_evts ? 0 : wait_slice,
2721 &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002722 if (rv)
2723 total_wait += wait_slice;
Florin Coras86f04502018-09-12 16:08:01 -07002724 if (n_evts)
2725 return n_evts;
Florin Coras99368312018-08-02 10:45:44 -07002726 }
2727 while (total_wait < wait_for_time);
Florin Coras86f04502018-09-12 16:08:01 -07002728 return n_evts;
Florin Coras99368312018-08-02 10:45:44 -07002729}
2730
2731static int
Florin Coras134a9962018-08-28 11:32:04 -07002732vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002733 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002734{
2735 vcl_mq_evt_conn_t *mqc;
2736 int __clib_unused n_read;
2737 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07002738 u64 buf;
2739
Florin Coras134a9962018-08-28 11:32:04 -07002740 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07002741again:
Florin Coras134a9962018-08-28 11:32:04 -07002742 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2743 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07002744 for (i = 0; i < n_mq_evts; i++)
2745 {
Florin Coras134a9962018-08-28 11:32:04 -07002746 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002747 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002748 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002749 }
Florin Corasa4878ed2018-10-12 13:09:36 -07002750 if (!n_evts && n_mq_evts > 0)
2751 goto again;
Florin Coras99368312018-08-02 10:45:44 -07002752
2753 return (int) n_evts;
2754}
2755
Dave Wallacef7f809c2017-10-03 01:48:42 -04002756int
Florin Coras134a9962018-08-28 11:32:04 -07002757vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002758 int maxevents, double wait_for_time)
2759{
Florin Coras134a9962018-08-28 11:32:04 -07002760 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002761 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07002762 u32 n_evts = 0;
2763 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002764
2765 if (PREDICT_FALSE (maxevents <= 0))
2766 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002767 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
Dave Wallace4878cbe2017-11-21 03:45:09 -05002768 getpid (), maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002769 return VPPCOM_EINVAL;
2770 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002771
Florin Coras134a9962018-08-28 11:32:04 -07002772 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07002773 if (!vep_session)
2774 return VPPCOM_EBADFD;
2775
Florin Coras54693d22018-07-17 10:46:29 -07002776 if (PREDICT_FALSE (!vep_session->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002777 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002778 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
Florin Coras134a9962018-08-28 11:32:04 -07002779 getpid (), vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07002780 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002781 }
Florin Coras54693d22018-07-17 10:46:29 -07002782
2783 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002784
Florin Coras86f04502018-09-12 16:08:01 -07002785 if (vec_len (wrk->unhandled_evts_vector))
2786 {
2787 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2788 {
2789 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2790 events, &n_evts);
2791 if (n_evts == maxevents)
2792 {
2793 i += 1;
2794 break;
2795 }
2796 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002797
Florin Coras86f04502018-09-12 16:08:01 -07002798 vec_delete (wrk->unhandled_evts_vector, i, 0);
2799 }
2800
2801 if (vcm->cfg.use_mq_eventfd)
2802 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2803 wait_for_time);
2804
2805 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2806 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002807}
2808
Dave Wallace35830af2017-10-09 01:43:42 -04002809int
Florin Coras134a9962018-08-28 11:32:04 -07002810vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04002811 void *buffer, uint32_t * buflen)
2812{
Florin Coras134a9962018-08-28 11:32:04 -07002813 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002814 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04002815 int rv = VPPCOM_OK;
2816 u32 *flags = buffer;
Steven2199aab2017-10-15 20:18:47 -07002817 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04002818
Florin Coras134a9962018-08-28 11:32:04 -07002819 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002820 if (!session)
2821 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002822
Dave Wallace35830af2017-10-09 01:43:42 -04002823 switch (op)
2824 {
2825 case VPPCOM_ATTR_GET_NREAD:
Florin Coras54693d22018-07-17 10:46:29 -07002826 rv = vppcom_session_read_ready (session);
Florin Coras0d427d82018-06-27 03:24:07 -07002827 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
2828 getpid (), rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002829 break;
2830
Dave Wallace227867f2017-11-13 21:21:53 -05002831 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras134a9962018-08-28 11:32:04 -07002832 rv = vppcom_session_write_ready (session);
Florin Coras0d427d82018-06-27 03:24:07 -07002833 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
Florin Coras134a9962018-08-28 11:32:04 -07002834 getpid (), session_handle, rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002835 break;
2836
2837 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002838 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002839 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002840 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2841 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002842 *buflen = sizeof (*flags);
Florin Coras0d427d82018-06-27 03:24:07 -07002843 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, flags = 0x%08x, "
2844 "is_nonblocking = %u", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002845 session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002846 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002847 }
2848 else
2849 rv = VPPCOM_EINVAL;
2850 break;
2851
2852 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002853 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002854 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002855 if (*flags & O_NONBLOCK)
2856 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2857 else
2858 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2859
Florin Coras0d427d82018-06-27 03:24:07 -07002860 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, flags = 0x%08x,"
2861 " is_nonblocking = %u",
Florin Coras134a9962018-08-28 11:32:04 -07002862 getpid (), session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002863 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002864 }
2865 else
2866 rv = VPPCOM_EINVAL;
2867 break;
2868
2869 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002870 if (PREDICT_TRUE (buffer && buflen &&
2871 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002872 {
Florin Coras7e12d942018-06-27 14:32:43 -07002873 ep->is_ip4 = session->transport.is_ip4;
2874 ep->port = session->transport.rmt_port;
2875 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002876 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2877 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002878 else
Dave Barach178cf492018-11-13 16:34:13 -05002879 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2880 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002881 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002882 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, is_ip4 = %u, "
2883 "addr = %U, port %u", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002884 session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002885 &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002886 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2887 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002888 }
2889 else
2890 rv = VPPCOM_EINVAL;
2891 break;
2892
2893 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002894 if (PREDICT_TRUE (buffer && buflen &&
2895 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002896 {
Florin Coras7e12d942018-06-27 14:32:43 -07002897 ep->is_ip4 = session->transport.is_ip4;
2898 ep->port = session->transport.lcl_port;
2899 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002900 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2901 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002902 else
Dave Barach178cf492018-11-13 16:34:13 -05002903 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2904 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002905 *buflen = sizeof (*ep);
Florin Coras0d427d82018-06-27 03:24:07 -07002906 VDBG (1, "VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, is_ip4 = %u,"
2907 " addr = %U port %d", getpid (),
Florin Coras134a9962018-08-28 11:32:04 -07002908 session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002909 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002910 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2911 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002912 }
2913 else
2914 rv = VPPCOM_EINVAL;
2915 break;
Stevenb5a11602017-10-11 09:59:30 -07002916
Dave Wallace048b1d62018-01-03 22:24:41 -05002917 case VPPCOM_ATTR_GET_LIBC_EPFD:
2918 rv = session->libc_epfd;
Florin Coras0d427d82018-06-27 03:24:07 -07002919 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
2920 getpid (), rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002921 break;
2922
2923 case VPPCOM_ATTR_SET_LIBC_EPFD:
2924 if (PREDICT_TRUE (buffer && buflen &&
2925 (*buflen == sizeof (session->libc_epfd))))
2926 {
2927 session->libc_epfd = *(int *) buffer;
2928 *buflen = sizeof (session->libc_epfd);
2929
Florin Coras0d427d82018-06-27 03:24:07 -07002930 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
2931 "buflen %d", getpid (), session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002932 }
2933 else
2934 rv = VPPCOM_EINVAL;
2935 break;
2936
2937 case VPPCOM_ATTR_GET_PROTOCOL:
2938 if (buffer && buflen && (*buflen >= sizeof (int)))
2939 {
Florin Coras7e12d942018-06-27 14:32:43 -07002940 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05002941 *buflen = sizeof (int);
2942
Florin Coras0d427d82018-06-27 03:24:07 -07002943 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2944 getpid (), *(int *) buffer, *(int *) buffer ? "UDP" : "TCP",
2945 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002946 }
2947 else
2948 rv = VPPCOM_EINVAL;
2949 break;
2950
2951 case VPPCOM_ATTR_GET_LISTEN:
2952 if (buffer && buflen && (*buflen >= sizeof (int)))
2953 {
2954 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2955 VCL_SESS_ATTR_LISTEN);
2956 *buflen = sizeof (int);
2957
Florin Coras0d427d82018-06-27 03:24:07 -07002958 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, buflen %d",
2959 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002960 }
2961 else
2962 rv = VPPCOM_EINVAL;
2963 break;
2964
2965 case VPPCOM_ATTR_GET_ERROR:
2966 if (buffer && buflen && (*buflen >= sizeof (int)))
2967 {
2968 *(int *) buffer = 0;
2969 *buflen = sizeof (int);
2970
Florin Coras0d427d82018-06-27 03:24:07 -07002971 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2972 getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002973 }
2974 else
2975 rv = VPPCOM_EINVAL;
2976 break;
2977
2978 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2979 if (buffer && buflen && (*buflen >= sizeof (u32)))
2980 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002981
2982 /* VPP-TBD */
2983 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002984 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002985 vcm->cfg.tx_fifo_size);
2986 *buflen = sizeof (u32);
2987
Florin Coras0d427d82018-06-27 03:24:07 -07002988 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
2989 "buflen %d, #VPP-TBD#", getpid (),
2990 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002991 }
2992 else
2993 rv = VPPCOM_EINVAL;
2994 break;
2995
2996 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2997 if (buffer && buflen && (*buflen == sizeof (u32)))
2998 {
2999 /* VPP-TBD */
3000 session->sndbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07003001 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
3002 "buflen %d, #VPP-TBD#", getpid (),
3003 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003004 }
3005 else
3006 rv = VPPCOM_EINVAL;
3007 break;
3008
3009 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
3010 if (buffer && buflen && (*buflen >= sizeof (u32)))
3011 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003012
3013 /* VPP-TBD */
3014 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08003015 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05003016 vcm->cfg.rx_fifo_size);
3017 *buflen = sizeof (u32);
3018
Florin Coras0d427d82018-06-27 03:24:07 -07003019 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
3020 "buflen %d, #VPP-TBD#", getpid (),
3021 *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003022 }
3023 else
3024 rv = VPPCOM_EINVAL;
3025 break;
3026
3027 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
3028 if (buffer && buflen && (*buflen == sizeof (u32)))
3029 {
3030 /* VPP-TBD */
3031 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras0d427d82018-06-27 03:24:07 -07003032 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
3033 "buflen %d, #VPP-TBD#", getpid (),
3034 session->sndbuf_size, session->sndbuf_size, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003035 }
3036 else
3037 rv = VPPCOM_EINVAL;
3038 break;
3039
3040 case VPPCOM_ATTR_GET_REUSEADDR:
3041 if (buffer && buflen && (*buflen >= sizeof (int)))
3042 {
3043 /* VPP-TBD */
3044 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3045 VCL_SESS_ATTR_REUSEADDR);
3046 *buflen = sizeof (int);
3047
Florin Coras0d427d82018-06-27 03:24:07 -07003048 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
3049 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003050 }
3051 else
3052 rv = VPPCOM_EINVAL;
3053 break;
3054
Stevenb5a11602017-10-11 09:59:30 -07003055 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05003056 if (buffer && buflen && (*buflen == sizeof (int)) &&
3057 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3058 {
3059 /* VPP-TBD */
3060 if (*(int *) buffer)
3061 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
3062 else
3063 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
3064
Florin Coras0d427d82018-06-27 03:24:07 -07003065 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d,"
3066 " #VPP-TBD#", getpid (),
3067 VCL_SESS_ATTR_TEST (session->attr,
3068 VCL_SESS_ATTR_REUSEADDR), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003069 }
3070 else
3071 rv = VPPCOM_EINVAL;
3072 break;
3073
3074 case VPPCOM_ATTR_GET_REUSEPORT:
3075 if (buffer && buflen && (*buflen >= sizeof (int)))
3076 {
3077 /* VPP-TBD */
3078 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3079 VCL_SESS_ATTR_REUSEPORT);
3080 *buflen = sizeof (int);
3081
Florin Coras0d427d82018-06-27 03:24:07 -07003082 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d,"
3083 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003084 }
3085 else
3086 rv = VPPCOM_EINVAL;
3087 break;
3088
3089 case VPPCOM_ATTR_SET_REUSEPORT:
3090 if (buffer && buflen && (*buflen == sizeof (int)) &&
3091 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
3092 {
3093 /* VPP-TBD */
3094 if (*(int *) buffer)
3095 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
3096 else
3097 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
3098
Florin Coras0d427d82018-06-27 03:24:07 -07003099 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d,"
3100 " #VPP-TBD#", getpid (),
3101 VCL_SESS_ATTR_TEST (session->attr,
3102 VCL_SESS_ATTR_REUSEPORT), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003103 }
3104 else
3105 rv = VPPCOM_EINVAL;
3106 break;
3107
3108 case VPPCOM_ATTR_GET_BROADCAST:
3109 if (buffer && buflen && (*buflen >= sizeof (int)))
3110 {
3111 /* VPP-TBD */
3112 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3113 VCL_SESS_ATTR_BROADCAST);
3114 *buflen = sizeof (int);
3115
Florin Coras0d427d82018-06-27 03:24:07 -07003116 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d,"
3117 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003118 }
3119 else
3120 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003121 break;
3122
3123 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05003124 if (buffer && buflen && (*buflen == sizeof (int)))
3125 {
3126 /* VPP-TBD */
3127 if (*(int *) buffer)
3128 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
3129 else
3130 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
3131
Florin Coras0d427d82018-06-27 03:24:07 -07003132 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, "
3133 "#VPP-TBD#", getpid (),
3134 VCL_SESS_ATTR_TEST (session->attr,
3135 VCL_SESS_ATTR_BROADCAST), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003136 }
3137 else
3138 rv = VPPCOM_EINVAL;
3139 break;
3140
3141 case VPPCOM_ATTR_GET_V6ONLY:
3142 if (buffer && buflen && (*buflen >= sizeof (int)))
3143 {
3144 /* VPP-TBD */
3145 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3146 VCL_SESS_ATTR_V6ONLY);
3147 *buflen = sizeof (int);
3148
Florin Coras0d427d82018-06-27 03:24:07 -07003149 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, "
3150 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003151 }
3152 else
3153 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003154 break;
3155
3156 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05003157 if (buffer && buflen && (*buflen == sizeof (int)))
3158 {
3159 /* VPP-TBD */
3160 if (*(int *) buffer)
3161 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
3162 else
3163 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
3164
Florin Coras0d427d82018-06-27 03:24:07 -07003165 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, "
3166 "#VPP-TBD#", getpid (),
3167 VCL_SESS_ATTR_TEST (session->attr,
3168 VCL_SESS_ATTR_V6ONLY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003169 }
3170 else
3171 rv = VPPCOM_EINVAL;
3172 break;
3173
3174 case VPPCOM_ATTR_GET_KEEPALIVE:
3175 if (buffer && buflen && (*buflen >= sizeof (int)))
3176 {
3177 /* VPP-TBD */
3178 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3179 VCL_SESS_ATTR_KEEPALIVE);
3180 *buflen = sizeof (int);
3181
Florin Coras0d427d82018-06-27 03:24:07 -07003182 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, "
3183 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003184 }
3185 else
3186 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07003187 break;
Stevenbccd3392017-10-12 20:42:21 -07003188
3189 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003190 if (buffer && buflen && (*buflen == sizeof (int)))
3191 {
3192 /* VPP-TBD */
3193 if (*(int *) buffer)
3194 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3195 else
3196 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
3197
Florin Coras0d427d82018-06-27 03:24:07 -07003198 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, "
3199 "#VPP-TBD#", getpid (),
3200 VCL_SESS_ATTR_TEST (session->attr,
3201 VCL_SESS_ATTR_KEEPALIVE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003202 }
3203 else
3204 rv = VPPCOM_EINVAL;
3205 break;
3206
3207 case VPPCOM_ATTR_GET_TCP_NODELAY:
3208 if (buffer && buflen && (*buflen >= sizeof (int)))
3209 {
3210 /* VPP-TBD */
3211 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3212 VCL_SESS_ATTR_TCP_NODELAY);
3213 *buflen = sizeof (int);
3214
Florin Coras0d427d82018-06-27 03:24:07 -07003215 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, "
3216 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003217 }
3218 else
3219 rv = VPPCOM_EINVAL;
3220 break;
3221
3222 case VPPCOM_ATTR_SET_TCP_NODELAY:
3223 if (buffer && buflen && (*buflen == sizeof (int)))
3224 {
3225 /* VPP-TBD */
3226 if (*(int *) buffer)
3227 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3228 else
3229 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3230
Florin Coras0d427d82018-06-27 03:24:07 -07003231 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, "
3232 "#VPP-TBD#", getpid (),
3233 VCL_SESS_ATTR_TEST (session->attr,
3234 VCL_SESS_ATTR_TCP_NODELAY), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003235 }
3236 else
3237 rv = VPPCOM_EINVAL;
3238 break;
3239
3240 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3241 if (buffer && buflen && (*buflen >= sizeof (int)))
3242 {
3243 /* VPP-TBD */
3244 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3245 VCL_SESS_ATTR_TCP_KEEPIDLE);
3246 *buflen = sizeof (int);
3247
Florin Coras0d427d82018-06-27 03:24:07 -07003248 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, "
3249 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003250 }
3251 else
3252 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003253 break;
3254
3255 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003256 if (buffer && buflen && (*buflen == sizeof (int)))
3257 {
3258 /* VPP-TBD */
3259 if (*(int *) buffer)
3260 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3261 else
3262 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3263
Florin Coras0d427d82018-06-27 03:24:07 -07003264 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, "
3265 "#VPP-TBD#", getpid (),
3266 VCL_SESS_ATTR_TEST (session->attr,
3267 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003268 }
3269 else
3270 rv = VPPCOM_EINVAL;
3271 break;
3272
3273 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3274 if (buffer && buflen && (*buflen >= sizeof (int)))
3275 {
3276 /* VPP-TBD */
3277 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3278 VCL_SESS_ATTR_TCP_KEEPINTVL);
3279 *buflen = sizeof (int);
3280
Florin Coras0d427d82018-06-27 03:24:07 -07003281 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, "
3282 "#VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003283 }
3284 else
3285 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003286 break;
3287
3288 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003289 if (buffer && buflen && (*buflen == sizeof (int)))
3290 {
3291 /* VPP-TBD */
3292 if (*(int *) buffer)
3293 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3294 else
3295 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3296
Florin Coras0d427d82018-06-27 03:24:07 -07003297 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, "
3298 "#VPP-TBD#", getpid (),
3299 VCL_SESS_ATTR_TEST (session->attr,
3300 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003301 }
3302 else
3303 rv = VPPCOM_EINVAL;
3304 break;
3305
3306 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3307 if (buffer && buflen && (*buflen >= sizeof (u32)))
3308 {
3309 /* VPP-TBD */
3310 *(u32 *) buffer = session->user_mss;
3311 *buflen = sizeof (int);
3312
Florin Coras0d427d82018-06-27 03:24:07 -07003313 VDBG (2, "VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d,"
3314 " #VPP-TBD#", getpid (), *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003315 }
3316 else
3317 rv = VPPCOM_EINVAL;
3318 break;
3319
3320 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3321 if (buffer && buflen && (*buflen == sizeof (u32)))
3322 {
3323 /* VPP-TBD */
3324 session->user_mss = *(u32 *) buffer;
3325
Florin Coras0d427d82018-06-27 03:24:07 -07003326 VDBG (2, "VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, "
3327 "#VPP-TBD#", getpid (), session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003328 }
3329 else
3330 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003331 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003332
Florin Coras47c40e22018-11-26 17:01:36 -08003333 case VPPCOM_ATTR_GET_REFCNT:
3334 rv = vcl_session_get_refcnt (session);
3335 break;
3336
Dave Wallacee22aa742017-10-20 12:30:38 -04003337 default:
3338 rv = VPPCOM_EINVAL;
3339 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003340 }
3341
Dave Wallace35830af2017-10-09 01:43:42 -04003342 return rv;
3343}
3344
Stevenac1f96d2017-10-24 16:03:58 -07003345int
Florin Coras134a9962018-08-28 11:32:04 -07003346vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003347 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3348{
Florin Coras134a9962018-08-28 11:32:04 -07003349 vcl_worker_t *wrk = vcl_worker_get_current ();
Stevenac1f96d2017-10-24 16:03:58 -07003350 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07003351 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07003352
3353 if (ep)
3354 {
Florin Coras134a9962018-08-28 11:32:04 -07003355 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003356 if (PREDICT_FALSE (!session))
Stevenac1f96d2017-10-24 16:03:58 -07003357 {
Florin Coras0d427d82018-06-27 03:24:07 -07003358 VDBG (0, "VCL<%d>: invalid session, sid (%u) has been closed!",
Florin Coras134a9962018-08-28 11:32:04 -07003359 getpid (), session_handle);
Florin Coras460dce62018-07-27 05:45:06 -07003360 return VPPCOM_EBADFD;
Stevenac1f96d2017-10-24 16:03:58 -07003361 }
Florin Coras7e12d942018-06-27 14:32:43 -07003362 ep->is_ip4 = session->transport.is_ip4;
3363 ep->port = session->transport.rmt_port;
Stevenac1f96d2017-10-24 16:03:58 -07003364 }
Steven58f464e2017-10-25 12:33:12 -07003365
3366 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003367 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003368 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003369 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003370 else
3371 {
Dave Wallace048b1d62018-01-03 22:24:41 -05003372 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
3373 getpid (), flags);
Florin Coras460dce62018-07-27 05:45:06 -07003374 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003375 }
3376
Florin Coras99368312018-08-02 10:45:44 -07003377 if (ep)
3378 {
3379 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003380 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3381 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003382 else
Dave Barach178cf492018-11-13 16:34:13 -05003383 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3384 sizeof (ip6_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003385 }
Florin Coras460dce62018-07-27 05:45:06 -07003386
Stevenac1f96d2017-10-24 16:03:58 -07003387 return rv;
3388}
3389
3390int
Florin Coras134a9962018-08-28 11:32:04 -07003391vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003392 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3393{
Dave Wallace617dffa2017-10-26 14:47:06 -04003394 if (!buffer)
3395 return VPPCOM_EINVAL;
3396
3397 if (ep)
3398 {
3399 // TBD
3400 return VPPCOM_EINVAL;
3401 }
3402
3403 if (flags)
3404 {
3405 // TBD check the flags and do the right thing
Florin Coras0d427d82018-06-27 03:24:07 -07003406 VDBG (2, "VCL<%d>: handling flags 0x%u (%d) not implemented yet.",
3407 getpid (), flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003408 }
3409
Florin Coras42ceddb2018-12-12 10:56:01 -08003410 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
Stevenac1f96d2017-10-24 16:03:58 -07003411}
3412
Dave Wallace048b1d62018-01-03 22:24:41 -05003413int
3414vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3415{
Florin Coras134a9962018-08-28 11:32:04 -07003416 vcl_worker_t *wrk = vcl_worker_get_current ();
3417 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003418 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003419 svm_msg_q_msg_t msg;
3420 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003421 int rv, num_ev = 0;
3422
Florin Coras0d427d82018-06-27 03:24:07 -07003423 VDBG (3, "VCL<%d>: vp %p, nsids %u, wait_for_time %f",
3424 getpid (), vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003425
3426 if (!vp)
3427 return VPPCOM_EFAULT;
3428
3429 do
3430 {
Florin Coras7e12d942018-06-27 14:32:43 -07003431 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003432
Florin Coras6917b942018-11-13 22:44:54 -08003433 /* Dequeue all events and drop all unhandled io events */
3434 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3435 {
3436 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3437 vcl_handle_mq_event (wrk, e);
3438 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3439 }
3440 vec_reset_length (wrk->unhandled_evts_vector);
3441
Dave Wallace048b1d62018-01-03 22:24:41 -05003442 for (i = 0; i < n_sids; i++)
3443 {
Florin Coras134a9962018-08-28 11:32:04 -07003444 session = vcl_session_get (wrk, vp[i].sid);
Florin Coras070453d2018-08-24 17:04:27 -07003445 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003446 {
3447 vp[i].revents = POLLHUP;
3448 num_ev++;
3449 continue;
3450 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003451
Florin Coras6917b942018-11-13 22:44:54 -08003452 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003453
3454 if (POLLIN & vp[i].events)
3455 {
Florin Coras54693d22018-07-17 10:46:29 -07003456 rv = vppcom_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003457 if (rv > 0)
3458 {
Florin Coras6917b942018-11-13 22:44:54 -08003459 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003460 num_ev++;
3461 }
3462 else if (rv < 0)
3463 {
3464 switch (rv)
3465 {
3466 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003467 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003468 break;
3469
3470 default:
Florin Coras6917b942018-11-13 22:44:54 -08003471 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003472 break;
3473 }
3474 num_ev++;
3475 }
3476 }
3477
3478 if (POLLOUT & vp[i].events)
3479 {
Florin Coras134a9962018-08-28 11:32:04 -07003480 rv = vppcom_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003481 if (rv > 0)
3482 {
Florin Coras6917b942018-11-13 22:44:54 -08003483 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003484 num_ev++;
3485 }
3486 else if (rv < 0)
3487 {
3488 switch (rv)
3489 {
3490 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003491 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003492 break;
3493
3494 default:
Florin Coras6917b942018-11-13 22:44:54 -08003495 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003496 break;
3497 }
3498 num_ev++;
3499 }
3500 }
3501
Dave Wallace7e607a72018-06-18 18:41:32 -04003502 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003503 {
Florin Coras6917b942018-11-13 22:44:54 -08003504 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003505 num_ev++;
3506 }
3507 }
3508 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003509 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003510 }
3511 while ((num_ev == 0) && keep_trying);
3512
3513 if (VPPCOM_DEBUG > 3)
3514 {
3515 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
3516 for (i = 0; i < n_sids; i++)
3517 {
3518 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
3519 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
Florin Coras6917b942018-11-13 22:44:54 -08003520 vp[i].events, vp[i].revents);
Dave Wallace048b1d62018-01-03 22:24:41 -05003521 }
3522 }
3523 return num_ev;
3524}
3525
Florin Coras99368312018-08-02 10:45:44 -07003526int
3527vppcom_mq_epoll_fd (void)
3528{
Florin Coras134a9962018-08-28 11:32:04 -07003529 vcl_worker_t *wrk = vcl_worker_get_current ();
3530 return wrk->mqs_epfd;
3531}
3532
3533int
3534vppcom_session_index (uint32_t session_handle)
3535{
3536 return session_handle & 0xFFFFFF;
3537}
3538
3539int
Florin Coras30e273b2018-11-27 00:04:59 -08003540vppcom_session_handle (uint32_t session_index)
3541{
Florin Coras47c40e22018-11-26 17:01:36 -08003542 return (vcl_get_worker_index () << 24) | session_index;
Florin Coras30e273b2018-11-27 00:04:59 -08003543}
3544
3545int
Florin Coras134a9962018-08-28 11:32:04 -07003546vppcom_worker_register (void)
3547{
Florin Coras47c40e22018-11-26 17:01:36 -08003548 if (!vcl_worker_alloc_and_init ())
3549 return VPPCOM_EEXIST;
3550
3551 if (vcl_worker_set_bapi ())
3552 return VPPCOM_EEXIST;
3553
3554 if (vcl_worker_register_with_vpp ())
3555 return VPPCOM_EEXIST;
3556
3557 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003558}
3559
Florin Corasdfe4cf42018-11-28 22:13:45 -08003560int
3561vppcom_worker_index (void)
3562{
3563 return vcl_get_worker_index ();
3564}
3565
Dave Wallacee22aa742017-10-20 12:30:38 -04003566/*
3567 * fd.io coding-style-patch-verification: ON
3568 *
3569 * Local Variables:
3570 * eval: (c-set-style "gnu")
3571 * End:
3572 */