blob: 037df995510676b9f1291d38257f309dc491b464 [file] [log] [blame]
Dave Wallace543852a2017-08-03 02:11:34 -04001/*
Florin Coras5e062572019-03-14 19:07:51 -07002 * Copyright (c) 2017-2019 Cisco and/or its affiliates.
Dave Wallace543852a2017-08-03 02:11:34 -04003 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this
5 * You may obtain a copy of the License at:
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <stdlib.h>
Dave Wallace543852a2017-08-03 02:11:34 -040018#include <svm/svm_fifo_segment.h>
Dave Wallace5c7cf1c2017-10-24 04:12:18 -040019#include <vcl/vppcom.h>
Florin Coras0d427d82018-06-27 03:24:07 -070020#include <vcl/vcl_debug.h>
Florin Coras697faea2018-06-27 17:10:49 -070021#include <vcl/vcl_private.h>
Dave Wallace7e607a72018-06-18 18:41:32 -040022
Florin Coras134a9962018-08-28 11:32:04 -070023__thread uword __vcl_worker_index = ~0;
24
Florin Corasd85de682018-11-29 17:02:29 -080025static int
26vcl_wait_for_segment (u64 segment_handle)
Florin Coras54693d22018-07-17 10:46:29 -070027{
Florin Corasd85de682018-11-29 17:02:29 -080028 vcl_worker_t *wrk = vcl_worker_get_current ();
29 u32 wait_for_seconds = 10, segment_index;
30 f64 timeout;
Florin Coras54693d22018-07-17 10:46:29 -070031
Florin Corasd85de682018-11-29 17:02:29 -080032 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
Florin Coras5f45e012019-01-23 09:21:30 -080033 return 0;
Florin Coras54693d22018-07-17 10:46:29 -070034
Florin Corasd85de682018-11-29 17:02:29 -080035 timeout = clib_time_now (&wrk->clib_time) + wait_for_seconds;
36 while (clib_time_now (&wrk->clib_time) < timeout)
Florin Coras54693d22018-07-17 10:46:29 -070037 {
Florin Corasd85de682018-11-29 17:02:29 -080038 segment_index = vcl_segment_table_lookup (segment_handle);
39 if (segment_index != VCL_INVALID_SEGMENT_INDEX)
40 return 0;
41 usleep (10);
Florin Coras54693d22018-07-17 10:46:29 -070042 }
Florin Corasd85de682018-11-29 17:02:29 -080043 return 1;
Florin Coras54693d22018-07-17 10:46:29 -070044}
45
Florin Coras30e79c22019-01-02 19:31:22 -080046static inline int
47vcl_mq_dequeue_batch (vcl_worker_t * wrk, svm_msg_q_t * mq)
48{
49 svm_msg_q_msg_t *msg;
50 u32 n_msgs;
51 int i;
52
53 n_msgs = svm_msg_q_size (mq);
54 for (i = 0; i < n_msgs; i++)
55 {
56 vec_add2 (wrk->mq_msg_vector, msg, 1);
57 svm_msg_q_sub_w_lock (mq, msg);
58 }
59 return n_msgs;
60}
61
Florin Coras697faea2018-06-27 17:10:49 -070062const char *
Florin Coras288eaab2019-02-03 15:26:14 -080063vppcom_session_state_str (vcl_session_state_t state)
Dave Wallace543852a2017-08-03 02:11:34 -040064{
65 char *st;
66
67 switch (state)
68 {
69 case STATE_START:
70 st = "STATE_START";
71 break;
72
73 case STATE_CONNECT:
74 st = "STATE_CONNECT";
75 break;
76
77 case STATE_LISTEN:
78 st = "STATE_LISTEN";
79 break;
80
81 case STATE_ACCEPT:
82 st = "STATE_ACCEPT";
83 break;
84
Florin Coras3c7d4f92018-12-14 11:28:43 -080085 case STATE_VPP_CLOSING:
86 st = "STATE_VPP_CLOSING";
Dave Wallace4878cbe2017-11-21 03:45:09 -050087 break;
88
Dave Wallace543852a2017-08-03 02:11:34 -040089 case STATE_DISCONNECT:
90 st = "STATE_DISCONNECT";
91 break;
92
93 case STATE_FAILED:
94 st = "STATE_FAILED";
95 break;
96
Florin Coras2d675d72019-01-28 15:54:27 -080097 case STATE_UPDATED:
98 st = "STATE_UPDATED";
99 break;
100
101 case STATE_LISTEN_NO_MQ:
102 st = "STATE_LISTEN_NO_MQ";
103 break;
104
Dave Wallace543852a2017-08-03 02:11:34 -0400105 default:
106 st = "UNKNOWN_STATE";
107 break;
108 }
109
110 return st;
111}
112
Dave Wallace543852a2017-08-03 02:11:34 -0400113u8 *
114format_ip4_address (u8 * s, va_list * args)
115{
116 u8 *a = va_arg (*args, u8 *);
117 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
118}
119
120u8 *
121format_ip6_address (u8 * s, va_list * args)
122{
123 ip6_address_t *a = va_arg (*args, ip6_address_t *);
124 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
125
126 i_max_n_zero = ARRAY_LEN (a->as_u16);
127 max_n_zeros = 0;
128 i_first_zero = i_max_n_zero;
129 n_zeros = 0;
130 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
131 {
132 u32 is_zero = a->as_u16[i] == 0;
133 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
134 {
135 i_first_zero = i;
136 n_zeros = 0;
137 }
138 n_zeros += is_zero;
139 if ((!is_zero && n_zeros > max_n_zeros)
140 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
141 {
142 i_max_n_zero = i_first_zero;
143 max_n_zeros = n_zeros;
144 i_first_zero = ARRAY_LEN (a->as_u16);
145 n_zeros = 0;
146 }
147 }
148
149 last_double_colon = 0;
150 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
151 {
152 if (i == i_max_n_zero && max_n_zeros > 1)
153 {
154 s = format (s, "::");
155 i += max_n_zeros - 1;
156 last_double_colon = 1;
157 }
158 else
159 {
160 s = format (s, "%s%x",
161 (last_double_colon || i == 0) ? "" : ":",
162 clib_net_to_host_u16 (a->as_u16[i]));
163 last_double_colon = 0;
164 }
165 }
166
167 return s;
168}
169
170/* Format an IP46 address. */
171u8 *
172format_ip46_address (u8 * s, va_list * args)
173{
174 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
175 ip46_type_t type = va_arg (*args, ip46_type_t);
176 int is_ip4 = 1;
177
178 switch (type)
179 {
180 case IP46_TYPE_ANY:
181 is_ip4 = ip46_address_is_ip4 (ip46);
182 break;
183 case IP46_TYPE_IP4:
184 is_ip4 = 1;
185 break;
186 case IP46_TYPE_IP6:
187 is_ip4 = 0;
188 break;
189 }
190
191 return is_ip4 ?
192 format (s, "%U", format_ip4_address, &ip46->ip4) :
193 format (s, "%U", format_ip6_address, &ip46->ip6);
194}
195
Florin Coras697faea2018-06-27 17:10:49 -0700196/*
197 * VPPCOM Utility Functions
198 */
199
Florin Coras697faea2018-06-27 17:10:49 -0700200
Florin Coras54693d22018-07-17 10:46:29 -0700201static void
202vcl_send_session_accepted_reply (svm_msg_q_t * mq, u32 context,
203 session_handle_t handle, int retval)
204{
205 app_session_evt_t _app_evt, *app_evt = &_app_evt;
206 session_accepted_reply_msg_t *rmp;
207 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_ACCEPTED_REPLY);
208 rmp = (session_accepted_reply_msg_t *) app_evt->evt->data;
209 rmp->handle = handle;
210 rmp->context = context;
211 rmp->retval = retval;
212 app_send_ctrl_evt_to_vpp (mq, app_evt);
213}
214
Florin Coras99368312018-08-02 10:45:44 -0700215static void
216vcl_send_session_disconnected_reply (svm_msg_q_t * mq, u32 context,
217 session_handle_t handle, int retval)
218{
219 app_session_evt_t _app_evt, *app_evt = &_app_evt;
220 session_disconnected_reply_msg_t *rmp;
221 app_alloc_ctrl_evt_to_vpp (mq, app_evt,
222 SESSION_CTRL_EVT_DISCONNECTED_REPLY);
223 rmp = (session_disconnected_reply_msg_t *) app_evt->evt->data;
224 rmp->handle = handle;
225 rmp->context = context;
226 rmp->retval = retval;
227 app_send_ctrl_evt_to_vpp (mq, app_evt);
228}
229
Florin Corasc9fbd662018-08-24 12:59:56 -0700230static void
231vcl_send_session_reset_reply (svm_msg_q_t * mq, u32 context,
232 session_handle_t handle, int retval)
233{
234 app_session_evt_t _app_evt, *app_evt = &_app_evt;
235 session_reset_reply_msg_t *rmp;
236 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_RESET_REPLY);
237 rmp = (session_reset_reply_msg_t *) app_evt->evt->data;
238 rmp->handle = handle;
239 rmp->context = context;
240 rmp->retval = retval;
241 app_send_ctrl_evt_to_vpp (mq, app_evt);
242}
243
Florin Coras30e79c22019-01-02 19:31:22 -0800244void
245vcl_send_session_worker_update (vcl_worker_t * wrk, vcl_session_t * s,
246 u32 wrk_index)
247{
248 app_session_evt_t _app_evt, *app_evt = &_app_evt;
249 session_worker_update_msg_t *mp;
250 svm_msg_q_t *mq;
251
252 mq = vcl_session_vpp_evt_q (wrk, s);
253 app_alloc_ctrl_evt_to_vpp (mq, app_evt, SESSION_CTRL_EVT_WORKER_UPDATE);
254 mp = (session_worker_update_msg_t *) app_evt->evt->data;
255 mp->client_index = wrk->my_client_index;
256 mp->handle = s->vpp_handle;
257 mp->req_wrk_index = wrk->vpp_wrk_index;
258 mp->wrk_index = wrk_index;
259 app_send_ctrl_evt_to_vpp (mq, app_evt);
260}
261
Florin Coras54693d22018-07-17 10:46:29 -0700262static u32
Florin Coras134a9962018-08-28 11:32:04 -0700263vcl_session_accepted_handler (vcl_worker_t * wrk, session_accepted_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700264{
265 vcl_session_t *session, *listen_session;
266 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras134a9962018-08-28 11:32:04 -0700267 u32 vpp_wrk_index;
Florin Coras99368312018-08-02 10:45:44 -0700268 svm_msg_q_t *evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700269
Florin Coras134a9962018-08-28 11:32:04 -0700270 session = vcl_session_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700271
Florin Coras134a9962018-08-28 11:32:04 -0700272 listen_session = vcl_session_table_lookup_listener (wrk,
273 mp->listener_handle);
Florin Coras54693d22018-07-17 10:46:29 -0700274 if (!listen_session)
275 {
Florin Coras54693d22018-07-17 10:46:29 -0700276 evt_q = uword_to_pointer (mp->vpp_event_queue_address, svm_msg_q_t *);
Florin Coras5e062572019-03-14 19:07:51 -0700277 VDBG (0, "ERROR: couldn't find listen session: unknown vpp listener "
278 "handle %llx", mp->listener_handle);
Florin Coras54693d22018-07-17 10:46:29 -0700279 vcl_send_session_accepted_reply (evt_q, mp->context, mp->handle,
280 VNET_API_ERROR_INVALID_ARGUMENT);
Florin Coras134a9962018-08-28 11:32:04 -0700281 vcl_session_free (wrk, session);
Florin Coras54693d22018-07-17 10:46:29 -0700282 return VCL_INVALID_SESSION_INDEX;
283 }
284
Florin Coras54693d22018-07-17 10:46:29 -0700285 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
286 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
287
Florin Coras653e43f2019-03-04 10:56:23 -0800288 if (vcl_wait_for_segment (mp->segment_handle))
Florin Coras54693d22018-07-17 10:46:29 -0700289 {
Florin Coras5e062572019-03-14 19:07:51 -0700290 VDBG (0, "segment for session %u couldn't be mounted!",
291 session->session_index);
Florin Coras653e43f2019-03-04 10:56:23 -0800292 return VCL_INVALID_SESSION_INDEX;
Florin Coras54693d22018-07-17 10:46:29 -0700293 }
Florin Coras653e43f2019-03-04 10:56:23 -0800294
295 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
296 svm_msg_q_t *);
297 rx_fifo->client_session_index = session->session_index;
298 tx_fifo->client_session_index = session->session_index;
299 rx_fifo->client_thread_index = vcl_get_worker_index ();
300 tx_fifo->client_thread_index = vcl_get_worker_index ();
301 vpp_wrk_index = tx_fifo->master_thread_index;
302 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
303 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras54693d22018-07-17 10:46:29 -0700304
305 session->vpp_handle = mp->handle;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800306 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras54693d22018-07-17 10:46:29 -0700307 session->client_context = mp->context;
308 session->rx_fifo = rx_fifo;
309 session->tx_fifo = tx_fifo;
310
311 session->session_state = STATE_ACCEPT;
312 session->transport.rmt_port = mp->port;
313 session->transport.is_ip4 = mp->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500314 clib_memcpy_fast (&session->transport.rmt_ip, mp->ip,
315 sizeof (ip46_address_t));
Florin Coras54693d22018-07-17 10:46:29 -0700316
Florin Coras134a9962018-08-28 11:32:04 -0700317 vcl_session_table_add_vpp_handle (wrk, mp->handle, session->session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700318 session->transport.lcl_port = listen_session->transport.lcl_port;
319 session->transport.lcl_ip = listen_session->transport.lcl_ip;
Florin Coras460dce62018-07-27 05:45:06 -0700320 session->session_type = listen_session->session_type;
321 session->is_dgram = session->session_type == VPPCOM_PROTO_UDP;
Florin Coras54693d22018-07-17 10:46:29 -0700322
Florin Coras5e062572019-03-14 19:07:51 -0700323 VDBG (1, "session %u [0x%llx]: client accept request from %s address %U"
324 " port %d queue %p!", session->session_index, mp->handle,
Florin Coras54693d22018-07-17 10:46:29 -0700325 mp->is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->ip,
326 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
327 clib_net_to_host_u16 (mp->port), session->vpp_evt_q);
328 vcl_evt (VCL_EVT_ACCEPT, session, listen_session, session_index);
329
Florin Coras134a9962018-08-28 11:32:04 -0700330 return session->session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700331}
332
333static u32
Florin Coras134a9962018-08-28 11:32:04 -0700334vcl_session_connected_handler (vcl_worker_t * wrk,
335 session_connected_msg_t * mp)
Florin Coras54693d22018-07-17 10:46:29 -0700336{
Florin Coras99368312018-08-02 10:45:44 -0700337 u32 session_index, vpp_wrk_index;
Florin Coras54693d22018-07-17 10:46:29 -0700338 svm_fifo_t *rx_fifo, *tx_fifo;
Florin Coras99368312018-08-02 10:45:44 -0700339 vcl_session_t *session = 0;
Florin Coras54693d22018-07-17 10:46:29 -0700340
341 session_index = mp->context;
Florin Coras134a9962018-08-28 11:32:04 -0700342 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700343 if (!session)
344 {
Florin Coras5e062572019-03-14 19:07:51 -0700345 VDBG (0, "ERROR: vpp handle 0x%llx has no session index (%u)!",
346 mp->handle, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700347 return VCL_INVALID_SESSION_INDEX;
348 }
Florin Coras54693d22018-07-17 10:46:29 -0700349 if (mp->retval)
350 {
Florin Coras5e062572019-03-14 19:07:51 -0700351 VDBG (0, "ERROR: session index %u: connect failed! %U",
352 session_index, format_api_error, ntohl (mp->retval));
Florin Coras070453d2018-08-24 17:04:27 -0700353 session->session_state = STATE_FAILED;
354 session->vpp_handle = mp->handle;
355 return session_index;
Florin Coras54693d22018-07-17 10:46:29 -0700356 }
357
Florin Coras460dce62018-07-27 05:45:06 -0700358 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
359 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
Florin Corasd85de682018-11-29 17:02:29 -0800360 if (vcl_wait_for_segment (mp->segment_handle))
361 {
Florin Coras653e43f2019-03-04 10:56:23 -0800362 VDBG (0, "segment for session %u couldn't be mounted!",
363 session->session_index);
Florin Coras5e062572019-03-14 19:07:51 -0700364 session->session_state = STATE_FAILED;
Florin Corasd85de682018-11-29 17:02:29 -0800365 return VCL_INVALID_SESSION_INDEX;
366 }
367
Florin Coras460dce62018-07-27 05:45:06 -0700368 rx_fifo->client_session_index = session_index;
369 tx_fifo->client_session_index = session_index;
Florin Coras21795132018-09-09 09:40:51 -0700370 rx_fifo->client_thread_index = vcl_get_worker_index ();
371 tx_fifo->client_thread_index = vcl_get_worker_index ();
Florin Coras460dce62018-07-27 05:45:06 -0700372
Florin Coras653e43f2019-03-04 10:56:23 -0800373 session->vpp_evt_q = uword_to_pointer (mp->vpp_event_queue_address,
374 svm_msg_q_t *);
375 vpp_wrk_index = tx_fifo->master_thread_index;
376 vec_validate (wrk->vpp_event_queues, vpp_wrk_index);
377 wrk->vpp_event_queues[vpp_wrk_index] = session->vpp_evt_q;
Florin Coras99368312018-08-02 10:45:44 -0700378
Florin Coras653e43f2019-03-04 10:56:23 -0800379 if (mp->ct_rx_fifo)
Florin Coras99368312018-08-02 10:45:44 -0700380 {
Florin Coras653e43f2019-03-04 10:56:23 -0800381 session->ct_rx_fifo = uword_to_pointer (mp->ct_rx_fifo, svm_fifo_t *);
382 session->ct_tx_fifo = uword_to_pointer (mp->ct_tx_fifo, svm_fifo_t *);
383 if (vcl_wait_for_segment (mp->ct_segment_handle))
384 {
385 VDBG (0, "ct segment for session %u couldn't be mounted!",
386 session->session_index);
Florin Coras5e062572019-03-14 19:07:51 -0700387 session->session_state = STATE_FAILED;
Florin Coras653e43f2019-03-04 10:56:23 -0800388 return VCL_INVALID_SESSION_INDEX;
389 }
Florin Coras99368312018-08-02 10:45:44 -0700390 }
Florin Coras54693d22018-07-17 10:46:29 -0700391
Florin Coras54693d22018-07-17 10:46:29 -0700392 session->rx_fifo = rx_fifo;
393 session->tx_fifo = tx_fifo;
394 session->vpp_handle = mp->handle;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800395 session->vpp_thread_index = rx_fifo->master_thread_index;
Florin Coras54693d22018-07-17 10:46:29 -0700396 session->transport.is_ip4 = mp->is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500397 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
398 sizeof (session->transport.lcl_ip));
Florin Coras54693d22018-07-17 10:46:29 -0700399 session->transport.lcl_port = mp->lcl_port;
400 session->session_state = STATE_CONNECT;
401
402 /* Add it to lookup table */
Florin Coras3c7d4f92018-12-14 11:28:43 -0800403 vcl_session_table_add_vpp_handle (wrk, mp->handle, session_index);
Florin Coras54693d22018-07-17 10:46:29 -0700404
Florin Coras5e062572019-03-14 19:07:51 -0700405 VDBG (1, "session %u [0x%llx] connected! rx_fifo %p, refcnt %d, tx_fifo %p,"
406 " refcnt %d", session_index, mp->handle, session->rx_fifo,
Florin Coras54693d22018-07-17 10:46:29 -0700407 session->rx_fifo->refcnt, session->tx_fifo, session->tx_fifo->refcnt);
Florin Coras070453d2018-08-24 17:04:27 -0700408
Florin Coras54693d22018-07-17 10:46:29 -0700409 return session_index;
410}
411
Florin Coras3c7d4f92018-12-14 11:28:43 -0800412static int
413vcl_flag_accepted_session (vcl_session_t * session, u64 handle, u32 flags)
414{
415 vcl_session_msg_t *accepted_msg;
416 int i;
417
418 for (i = 0; i < vec_len (session->accept_evts_fifo); i++)
419 {
420 accepted_msg = &session->accept_evts_fifo[i];
421 if (accepted_msg->accepted_msg.handle == handle)
422 {
Florin Corasb0f662f2018-12-27 14:51:46 -0800423 accepted_msg->flags |= flags;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800424 return 1;
425 }
426 }
427 return 0;
428}
429
Florin Corasc9fbd662018-08-24 12:59:56 -0700430static u32
Florin Coras134a9962018-08-28 11:32:04 -0700431vcl_session_reset_handler (vcl_worker_t * wrk,
432 session_reset_msg_t * reset_msg)
Florin Corasc9fbd662018-08-24 12:59:56 -0700433{
434 vcl_session_t *session;
435 u32 sid;
436
Florin Coras134a9962018-08-28 11:32:04 -0700437 sid = vcl_session_index_from_vpp_handle (wrk, reset_msg->handle);
438 session = vcl_session_get (wrk, sid);
Florin Corasc9fbd662018-08-24 12:59:56 -0700439 if (!session)
440 {
441 VDBG (0, "request to reset unknown handle 0x%llx", reset_msg->handle);
442 return VCL_INVALID_SESSION_INDEX;
443 }
Florin Coras3c7d4f92018-12-14 11:28:43 -0800444
445 /* Caught a reset before actually accepting the session */
446 if (session->session_state == STATE_LISTEN)
447 {
448
449 if (!vcl_flag_accepted_session (session, reset_msg->handle,
450 VCL_ACCEPTED_F_RESET))
451 VDBG (0, "session was not accepted!");
452 return VCL_INVALID_SESSION_INDEX;
453 }
454
455 session->session_state = STATE_DISCONNECT;
456 VDBG (0, "reset session %u [0x%llx]", sid, reset_msg->handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700457 return sid;
458}
459
Florin Coras60116992018-08-27 09:52:18 -0700460static u32
Florin Coras134a9962018-08-28 11:32:04 -0700461vcl_session_bound_handler (vcl_worker_t * wrk, session_bound_msg_t * mp)
Florin Coras60116992018-08-27 09:52:18 -0700462{
463 vcl_session_t *session;
464 u32 sid = mp->context;
465
Florin Coras134a9962018-08-28 11:32:04 -0700466 session = vcl_session_get (wrk, sid);
Florin Coras60116992018-08-27 09:52:18 -0700467 if (mp->retval)
468 {
Florin Coras5e062572019-03-14 19:07:51 -0700469 VERR ("session %u [0x%llx]: bind failed: %U", sid, mp->handle,
Florin Corasd85de682018-11-29 17:02:29 -0800470 format_api_error, mp->retval);
Florin Coras60116992018-08-27 09:52:18 -0700471 if (session)
472 {
473 session->session_state = STATE_FAILED;
474 session->vpp_handle = mp->handle;
475 return sid;
476 }
477 else
478 {
Florin Coras5e062572019-03-14 19:07:51 -0700479 VDBG (0, "ERROR: session %u [0x%llx]: Invalid session index!",
480 sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700481 return VCL_INVALID_SESSION_INDEX;
482 }
483 }
484
485 session->vpp_handle = mp->handle;
486 session->transport.is_ip4 = mp->lcl_is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500487 clib_memcpy_fast (&session->transport.lcl_ip, mp->lcl_ip,
488 sizeof (ip46_address_t));
Florin Coras60116992018-08-27 09:52:18 -0700489 session->transport.lcl_port = mp->lcl_port;
Florin Coras134a9962018-08-28 11:32:04 -0700490 vcl_session_table_add_listener (wrk, mp->handle, sid);
Florin Coras60116992018-08-27 09:52:18 -0700491 session->session_state = STATE_LISTEN;
492
Florin Coras5f45e012019-01-23 09:21:30 -0800493 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
494 vec_validate (wrk->vpp_event_queues, 0);
495 wrk->vpp_event_queues[0] = session->vpp_evt_q;
496
Florin Coras60116992018-08-27 09:52:18 -0700497 if (session->is_dgram)
498 {
499 svm_fifo_t *rx_fifo, *tx_fifo;
500 session->vpp_evt_q = uword_to_pointer (mp->vpp_evt_q, svm_msg_q_t *);
501 rx_fifo = uword_to_pointer (mp->rx_fifo, svm_fifo_t *);
502 rx_fifo->client_session_index = sid;
503 tx_fifo = uword_to_pointer (mp->tx_fifo, svm_fifo_t *);
504 tx_fifo->client_session_index = sid;
505 session->rx_fifo = rx_fifo;
506 session->tx_fifo = tx_fifo;
507 }
508
Florin Coras05ecfcc2018-12-12 18:19:39 -0800509 VDBG (0, "session %u [0x%llx]: listen succeeded!", sid, mp->handle);
Florin Coras60116992018-08-27 09:52:18 -0700510 return sid;
511}
512
Florin Corasdfae9f92019-02-20 19:48:31 -0800513static void
514vcl_session_unlisten_reply_handler (vcl_worker_t * wrk, void *data)
515{
516 session_unlisten_reply_msg_t *mp = (session_unlisten_reply_msg_t *) data;
517 vcl_session_t *s;
518
519 s = vcl_session_get_w_vpp_handle (wrk, mp->handle);
520 if (!s || s->session_state != STATE_DISCONNECT)
521 {
522 VDBG (0, "Unlisten reply with wrong handle %llx", mp->handle);
523 return;
524 }
525
526 if (mp->retval)
527 VDBG (0, "ERROR: session %u [0xllx]: unlisten failed: %U",
528 s->session_index, mp->handle, format_api_error, ntohl (mp->retval));
529
530 if (mp->context != wrk->wrk_index)
531 VDBG (0, "wrong context");
532
533 vcl_session_table_del_vpp_handle (wrk, mp->handle);
534 vcl_session_free (wrk, s);
535}
536
Florin Coras3c7d4f92018-12-14 11:28:43 -0800537static vcl_session_t *
538vcl_session_accepted (vcl_worker_t * wrk, session_accepted_msg_t * msg)
539{
540 vcl_session_msg_t *vcl_msg;
541 vcl_session_t *session;
542
543 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
544 if (PREDICT_FALSE (session != 0))
Florin Corasb0f662f2018-12-27 14:51:46 -0800545 VWRN ("session overlap handle %lu state %u!", msg->handle,
546 session->session_state);
Florin Coras3c7d4f92018-12-14 11:28:43 -0800547
548 session = vcl_session_table_lookup_listener (wrk, msg->listener_handle);
549 if (!session)
550 {
551 VERR ("couldn't find listen session: listener handle %llx",
552 msg->listener_handle);
553 return 0;
554 }
555
556 clib_fifo_add2 (session->accept_evts_fifo, vcl_msg);
557 vcl_msg->accepted_msg = *msg;
558 /* Session handle points to listener until fully accepted by app */
559 vcl_session_table_add_vpp_handle (wrk, msg->handle, session->session_index);
560
561 return session;
562}
563
564static vcl_session_t *
565vcl_session_disconnected_handler (vcl_worker_t * wrk,
566 session_disconnected_msg_t * msg)
567{
568 vcl_session_t *session;
569
570 session = vcl_session_get_w_vpp_handle (wrk, msg->handle);
571 if (!session)
572 {
573 VDBG (0, "request to disconnect unknown handle 0x%llx", msg->handle);
574 return 0;
575 }
576
577 /* Caught a disconnect before actually accepting the session */
578 if (session->session_state == STATE_LISTEN)
579 {
Florin Coras3c7d4f92018-12-14 11:28:43 -0800580 if (!vcl_flag_accepted_session (session, msg->handle,
581 VCL_ACCEPTED_F_CLOSED))
582 VDBG (0, "session was not accepted!");
583 return 0;
584 }
585
586 session->session_state = STATE_VPP_CLOSING;
587 return session;
588}
589
Florin Coras30e79c22019-01-02 19:31:22 -0800590static void
591vcl_session_req_worker_update_handler (vcl_worker_t * wrk, void *data)
592{
593 session_req_worker_update_msg_t *msg;
594 vcl_session_t *s;
595
596 msg = (session_req_worker_update_msg_t *) data;
597 s = vcl_session_get_w_vpp_handle (wrk, msg->session_handle);
598 if (!s)
599 return;
600
601 vec_add1 (wrk->pending_session_wrk_updates, s->session_index);
602}
603
604static void
605vcl_session_worker_update_reply_handler (vcl_worker_t * wrk, void *data)
606{
607 session_worker_update_reply_msg_t *msg;
608 vcl_session_t *s;
609
610 msg = (session_worker_update_reply_msg_t *) data;
611 s = vcl_session_get_w_vpp_handle (wrk, msg->handle);
612 if (!s)
613 {
614 VDBG (0, "unknown handle 0x%llx", msg->handle);
615 return;
616 }
617 if (vcl_wait_for_segment (msg->segment_handle))
618 {
619 clib_warning ("segment for session %u couldn't be mounted!",
620 s->session_index);
621 return;
622 }
Florin Coras30e79c22019-01-02 19:31:22 -0800623
Florin Coras5f45e012019-01-23 09:21:30 -0800624 if (s->rx_fifo)
625 {
626 s->rx_fifo = uword_to_pointer (msg->rx_fifo, svm_fifo_t *);
627 s->tx_fifo = uword_to_pointer (msg->tx_fifo, svm_fifo_t *);
628 s->rx_fifo->client_session_index = s->session_index;
629 s->tx_fifo->client_session_index = s->session_index;
630 s->rx_fifo->client_thread_index = wrk->wrk_index;
631 s->tx_fifo->client_thread_index = wrk->wrk_index;
632 }
Florin Coras30e79c22019-01-02 19:31:22 -0800633 s->session_state = STATE_UPDATED;
634
Florin Coras30e79c22019-01-02 19:31:22 -0800635 VDBG (0, "session %u[0x%llx] moved to worker %u", s->session_index,
636 s->vpp_handle, wrk->wrk_index);
637}
638
Florin Coras86f04502018-09-12 16:08:01 -0700639static int
640vcl_handle_mq_event (vcl_worker_t * wrk, session_event_t * e)
Florin Coras54693d22018-07-17 10:46:29 -0700641{
Florin Coras54693d22018-07-17 10:46:29 -0700642 session_disconnected_msg_t *disconnected_msg;
Florin Coras54693d22018-07-17 10:46:29 -0700643 vcl_session_t *session;
Florin Coras54693d22018-07-17 10:46:29 -0700644
645 switch (e->event_type)
646 {
Florin Coras653e43f2019-03-04 10:56:23 -0800647 case SESSION_IO_EVT_RX:
648 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -0800649 session = vcl_session_get (wrk, e->session_index);
Florin Coras653e43f2019-03-04 10:56:23 -0800650 if (!session || !(session->session_state & STATE_OPEN))
651 break;
Florin Coras86f04502018-09-12 16:08:01 -0700652 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras54693d22018-07-17 10:46:29 -0700653 break;
654 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -0800655 vcl_session_accepted (wrk, (session_accepted_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700656 break;
657 case SESSION_CTRL_EVT_CONNECTED:
Florin Coras134a9962018-08-28 11:32:04 -0700658 vcl_session_connected_handler (wrk,
659 (session_connected_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700660 break;
661 case SESSION_CTRL_EVT_DISCONNECTED:
662 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800663 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
Florin Corasc9fbd662018-08-24 12:59:56 -0700664 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -0800665 break;
Florin Coras3c7d4f92018-12-14 11:28:43 -0800666 VDBG (0, "disconnected session %u [0x%llx]", session->session_index,
667 session->vpp_handle);
Florin Corasc9fbd662018-08-24 12:59:56 -0700668 break;
669 case SESSION_CTRL_EVT_RESET:
Florin Coras134a9962018-08-28 11:32:04 -0700670 vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
Florin Coras60116992018-08-27 09:52:18 -0700671 break;
672 case SESSION_CTRL_EVT_BOUND:
Florin Coras134a9962018-08-28 11:32:04 -0700673 vcl_session_bound_handler (wrk, (session_bound_msg_t *) e->data);
Florin Coras54693d22018-07-17 10:46:29 -0700674 break;
Florin Corasdfae9f92019-02-20 19:48:31 -0800675 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
676 vcl_session_unlisten_reply_handler (wrk, e->data);
677 break;
Florin Coras30e79c22019-01-02 19:31:22 -0800678 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
679 vcl_session_req_worker_update_handler (wrk, e->data);
680 break;
681 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
682 vcl_session_worker_update_reply_handler (wrk, e->data);
683 break;
Florin Coras54693d22018-07-17 10:46:29 -0700684 default:
685 clib_warning ("unhandled %u", e->event_type);
686 }
687 return VPPCOM_OK;
688}
689
Florin Coras30e79c22019-01-02 19:31:22 -0800690static int
Florin Coras697faea2018-06-27 17:10:49 -0700691vppcom_wait_for_session_state_change (u32 session_index,
Florin Coras288eaab2019-02-03 15:26:14 -0800692 vcl_session_state_t state,
Florin Coras697faea2018-06-27 17:10:49 -0700693 f64 wait_for_time)
694{
Florin Coras134a9962018-08-28 11:32:04 -0700695 vcl_worker_t *wrk = vcl_worker_get_current ();
696 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Florin Coras697faea2018-06-27 17:10:49 -0700697 vcl_session_t *volatile session;
Florin Coras54693d22018-07-17 10:46:29 -0700698 svm_msg_q_msg_t msg;
699 session_event_t *e;
Dave Wallace543852a2017-08-03 02:11:34 -0400700
Florin Coras697faea2018-06-27 17:10:49 -0700701 do
Dave Wallace543852a2017-08-03 02:11:34 -0400702 {
Florin Coras134a9962018-08-28 11:32:04 -0700703 session = vcl_session_get (wrk, session_index);
Florin Coras070453d2018-08-24 17:04:27 -0700704 if (PREDICT_FALSE (!session))
Florin Coras697faea2018-06-27 17:10:49 -0700705 {
Florin Coras070453d2018-08-24 17:04:27 -0700706 return VPPCOM_EBADFD;
Florin Coras697faea2018-06-27 17:10:49 -0700707 }
708 if (session->session_state & state)
709 {
Florin Coras697faea2018-06-27 17:10:49 -0700710 return VPPCOM_OK;
711 }
712 if (session->session_state & STATE_FAILED)
713 {
Florin Coras697faea2018-06-27 17:10:49 -0700714 return VPPCOM_ECONNREFUSED;
715 }
Florin Coras54693d22018-07-17 10:46:29 -0700716
Florin Coras134a9962018-08-28 11:32:04 -0700717 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0))
Florin Corasdc2e2512018-12-03 17:47:26 -0800718 {
719 usleep (100);
720 continue;
721 }
Florin Coras134a9962018-08-28 11:32:04 -0700722 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras86f04502018-09-12 16:08:01 -0700723 vcl_handle_mq_event (wrk, e);
Florin Coras134a9962018-08-28 11:32:04 -0700724 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800725 }
Florin Coras134a9962018-08-28 11:32:04 -0700726 while (clib_time_now (&wrk->clib_time) < timeout);
Florin Corasdcf55ce2017-11-16 15:32:50 -0800727
Florin Coras05ecfcc2018-12-12 18:19:39 -0800728 VDBG (0, "timeout waiting for state 0x%x (%s)", state,
Florin Coras697faea2018-06-27 17:10:49 -0700729 vppcom_session_state_str (state));
730 vcl_evt (VCL_EVT_SESSION_TIMEOUT, session, session_state);
Dave Wallace543852a2017-08-03 02:11:34 -0400731
Florin Coras697faea2018-06-27 17:10:49 -0700732 return VPPCOM_ETIMEDOUT;
Dave Wallace60caa062017-11-10 17:07:13 -0500733}
734
Florin Coras30e79c22019-01-02 19:31:22 -0800735static void
736vcl_handle_pending_wrk_updates (vcl_worker_t * wrk)
737{
Florin Coras288eaab2019-02-03 15:26:14 -0800738 vcl_session_state_t state;
Florin Coras30e79c22019-01-02 19:31:22 -0800739 vcl_session_t *s;
740 u32 *sip;
741
742 if (PREDICT_TRUE (vec_len (wrk->pending_session_wrk_updates) == 0))
743 return;
744
745 vec_foreach (sip, wrk->pending_session_wrk_updates)
746 {
747 s = vcl_session_get (wrk, *sip);
748 vcl_send_session_worker_update (wrk, s, wrk->wrk_index);
749 state = s->session_state;
750 vppcom_wait_for_session_state_change (s->session_index, STATE_UPDATED, 5);
751 s->session_state = state;
752 }
753 vec_reset_length (wrk->pending_session_wrk_updates);
754}
755
Florin Corasf9240dc2019-01-15 08:03:17 -0800756void
Florin Coras30e79c22019-01-02 19:31:22 -0800757vcl_flush_mq_events (void)
758{
759 vcl_worker_t *wrk = vcl_worker_get_current ();
760 svm_msg_q_msg_t *msg;
761 session_event_t *e;
762 svm_msg_q_t *mq;
763 int i;
764
765 mq = wrk->app_event_queue;
766 svm_msg_q_lock (mq);
767 vcl_mq_dequeue_batch (wrk, mq);
768 svm_msg_q_unlock (mq);
769
770 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
771 {
772 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
773 e = svm_msg_q_msg_data (mq, msg);
774 vcl_handle_mq_event (wrk, e);
775 svm_msg_q_free_msg (mq, msg);
776 }
777 vec_reset_length (wrk->mq_msg_vector);
778 vcl_handle_pending_wrk_updates (wrk);
779}
780
Florin Coras697faea2018-06-27 17:10:49 -0700781static int
782vppcom_app_session_enable (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400783{
Florin Coras697faea2018-06-27 17:10:49 -0700784 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400785
Florin Coras697faea2018-06-27 17:10:49 -0700786 if (vcm->app_state != STATE_APP_ENABLED)
787 {
788 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
Florin Coras134a9962018-08-28 11:32:04 -0700789 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
Florin Coras697faea2018-06-27 17:10:49 -0700790 if (PREDICT_FALSE (rv))
791 {
Florin Coras5e062572019-03-14 19:07:51 -0700792 VDBG (0, "application session enable timed out! returning %d (%s)",
793 rv, vppcom_retval_str (rv));
Florin Coras697faea2018-06-27 17:10:49 -0700794 return rv;
795 }
796 }
797 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400798}
799
Florin Coras697faea2018-06-27 17:10:49 -0700800static int
801vppcom_app_attach (void)
Dave Wallace543852a2017-08-03 02:11:34 -0400802{
Florin Coras697faea2018-06-27 17:10:49 -0700803 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -0400804
Florin Coras697faea2018-06-27 17:10:49 -0700805 vppcom_app_send_attach ();
Florin Coras134a9962018-08-28 11:32:04 -0700806 rv = vcl_wait_for_app_state_change (STATE_APP_ATTACHED);
Florin Coras697faea2018-06-27 17:10:49 -0700807 if (PREDICT_FALSE (rv))
808 {
Florin Coras5e062572019-03-14 19:07:51 -0700809 VDBG (0, "application attach timed out! returning %d (%s)", rv,
810 vppcom_retval_str (rv));
Florin Coras697faea2018-06-27 17:10:49 -0700811 return rv;
812 }
Dave Wallace543852a2017-08-03 02:11:34 -0400813
Florin Coras697faea2018-06-27 17:10:49 -0700814 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400815}
816
817static int
Florin Corasab2f6db2018-08-31 14:31:41 -0700818vppcom_session_unbind (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400819{
Florin Coras134a9962018-08-28 11:32:04 -0700820 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -0700821 vcl_session_t *session = 0;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500822 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400823
Florin Corasab2f6db2018-08-31 14:31:41 -0700824 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -0700825 if (!session)
826 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500827
828 vpp_handle = session->vpp_handle;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500829 session->vpp_handle = ~0;
Florin Coras7e12d942018-06-27 14:32:43 -0700830 session->session_state = STATE_DISCONNECT;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500831
Florin Coras5e062572019-03-14 19:07:51 -0700832 VDBG (1, "session %u [0x%llx]: sending unbind!", session->session_index,
833 vpp_handle);
Florin Coras0d427d82018-06-27 03:24:07 -0700834 vcl_evt (VCL_EVT_UNBIND, session);
Florin Coras2d675d72019-01-28 15:54:27 -0800835 vppcom_send_unbind_sock (wrk, vpp_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -0500836
Florin Coras070453d2018-08-24 17:04:27 -0700837 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400838}
839
Florin Coras697faea2018-06-27 17:10:49 -0700840static int
Florin Corasab2f6db2018-08-31 14:31:41 -0700841vppcom_session_disconnect (u32 session_handle)
Dave Wallace543852a2017-08-03 02:11:34 -0400842{
Florin Coras134a9962018-08-28 11:32:04 -0700843 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras99368312018-08-02 10:45:44 -0700844 svm_msg_q_t *vpp_evt_q;
Florin Coras7e12d942018-06-27 14:32:43 -0700845 vcl_session_t *session;
Florin Coras288eaab2019-02-03 15:26:14 -0800846 vcl_session_state_t state;
Florin Coras99368312018-08-02 10:45:44 -0700847 u64 vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -0400848
Florin Corasab2f6db2018-08-31 14:31:41 -0700849 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras21795132018-09-09 09:40:51 -0700850 if (!session)
851 return VPPCOM_EBADFD;
852
Dave Wallace4878cbe2017-11-21 03:45:09 -0500853 vpp_handle = session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -0700854 state = session->session_state;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500855
Florin Coras5e062572019-03-14 19:07:51 -0700856 VDBG (1, "session %u [0x%llx] state 0x%x (%s)", session->session_index,
857 vpp_handle, state, vppcom_session_state_str (state));
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400858
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -0800859 if (PREDICT_FALSE (state & STATE_LISTEN))
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400860 {
Florin Coras5e062572019-03-14 19:07:51 -0700861 VDBG (0, "ERROR: Cannot disconnect a listen socket!");
Florin Coras070453d2018-08-24 17:04:27 -0700862 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -0500863 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400864
Florin Coras3c7d4f92018-12-14 11:28:43 -0800865 if (state & STATE_VPP_CLOSING)
Dave Wallace4878cbe2017-11-21 03:45:09 -0500866 {
Florin Coras134a9962018-08-28 11:32:04 -0700867 vpp_evt_q = vcl_session_vpp_evt_q (wrk, session);
Florin Coras47c40e22018-11-26 17:01:36 -0800868 vcl_send_session_disconnected_reply (vpp_evt_q, wrk->my_client_index,
Florin Coras99368312018-08-02 10:45:44 -0700869 vpp_handle, 0);
Florin Coras5e062572019-03-14 19:07:51 -0700870 VDBG (1, "session %u [0x%llx]: sending disconnect REPLY...",
871 session->session_index, vpp_handle);
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400872 }
873 else
Dave Wallace227867f2017-11-13 21:21:53 -0500874 {
Florin Coras5e062572019-03-14 19:07:51 -0700875 VDBG (1, "session %u [0x%llx]: sending disconnect...",
876 session->session_index, vpp_handle);
Florin Corasab2f6db2018-08-31 14:31:41 -0700877 vppcom_send_disconnect_session (vpp_handle);
Dave Wallace227867f2017-11-13 21:21:53 -0500878 }
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400879
Florin Coras070453d2018-08-24 17:04:27 -0700880 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -0400881}
882
Florin Coras940f78f2018-11-30 12:11:20 -0800883/**
884 * Handle app exit
885 *
886 * Notify vpp of the disconnect and mark the worker as free. If we're the
887 * last worker, do a full cleanup otherwise, since we're probably a forked
888 * child, avoid syscalls as much as possible. We might've lost privileges.
889 */
890void
891vppcom_app_exit (void)
892{
893 if (!pool_elts (vcm->workers))
894 return;
Florin Coras01f3f892018-12-02 12:45:53 -0800895 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
896 vcl_set_worker_index (~0);
Florin Coras940f78f2018-11-30 12:11:20 -0800897 vcl_elog_stop (vcm);
898 if (vec_len (vcm->workers) == 1)
899 vl_client_disconnect_from_vlib ();
900 else
Florin Coraseaec2a62018-12-04 16:34:05 -0800901 vl_client_send_disconnect (1 /* vpp should cleanup */ );
Florin Coras940f78f2018-11-30 12:11:20 -0800902}
903
Dave Wallace543852a2017-08-03 02:11:34 -0400904/*
905 * VPPCOM Public API functions
906 */
907int
908vppcom_app_create (char *app_name)
909{
Dave Wallace543852a2017-08-03 02:11:34 -0400910 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Dave Wallace543852a2017-08-03 02:11:34 -0400911 int rv;
912
Florin Coras47c40e22018-11-26 17:01:36 -0800913 if (vcm->is_init)
Dave Wallace543852a2017-08-03 02:11:34 -0400914 {
Florin Coras955bfbb2018-12-04 13:43:45 -0800915 VDBG (1, "already initialized");
916 return VPPCOM_EEXIST;
Dave Wallace543852a2017-08-03 02:11:34 -0400917 }
918
Florin Coras47c40e22018-11-26 17:01:36 -0800919 vcm->is_init = 1;
920 vppcom_cfg (&vcm->cfg);
921 vcl_cfg = &vcm->cfg;
922
923 vcm->main_cpu = pthread_self ();
924 vcm->main_pid = getpid ();
925 vcm->app_name = format (0, "%s", app_name);
926 vppcom_init_error_string_table ();
Florin Corasadc74d72018-12-02 13:36:00 -0800927 svm_fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
Florin Coras47c40e22018-11-26 17:01:36 -0800928 20 /* timeout in secs */ );
929 pool_alloc (vcm->workers, vcl_cfg->max_workers);
930 clib_spinlock_init (&vcm->workers_lock);
Florin Corasd85de682018-11-29 17:02:29 -0800931 clib_rwlock_init (&vcm->segment_table_lock);
Florin Coras940f78f2018-11-30 12:11:20 -0800932 atexit (vppcom_app_exit);
Florin Coras47c40e22018-11-26 17:01:36 -0800933
934 /* Allocate default worker */
935 vcl_worker_alloc_and_init ();
936
937 /* API hookup and connect to VPP */
938 vppcom_api_hookup ();
939 vcl_elog_init (vcm);
940 vcm->app_state = STATE_APP_START;
941 rv = vppcom_connect_to_vpp (app_name);
942 if (rv)
Dave Wallace543852a2017-08-03 02:11:34 -0400943 {
Florin Coras47c40e22018-11-26 17:01:36 -0800944 VERR ("couldn't connect to VPP!");
945 return rv;
Dave Wallace66cf6eb2017-10-26 02:51:07 -0400946 }
Florin Coras47c40e22018-11-26 17:01:36 -0800947 VDBG (0, "sending session enable");
948 rv = vppcom_app_session_enable ();
949 if (rv)
950 {
951 VERR ("vppcom_app_session_enable() failed!");
952 return rv;
953 }
954
955 VDBG (0, "sending app attach");
956 rv = vppcom_app_attach ();
957 if (rv)
958 {
959 VERR ("vppcom_app_attach() failed!");
960 return rv;
961 }
962
963 VDBG (0, "app_name '%s', my_client_index %d (0x%x)", app_name,
964 vcm->workers[0].my_client_index, vcm->workers[0].my_client_index);
Dave Wallace543852a2017-08-03 02:11:34 -0400965
966 return VPPCOM_OK;
967}
968
969void
970vppcom_app_destroy (void)
971{
Dave Wallace543852a2017-08-03 02:11:34 -0400972 int rv;
Dave Wallacede910062018-03-20 09:22:13 -0400973 f64 orig_app_timeout;
Dave Wallace543852a2017-08-03 02:11:34 -0400974
Florin Coras940f78f2018-11-30 12:11:20 -0800975 if (!pool_elts (vcm->workers))
976 return;
977
Florin Coras0d427d82018-06-27 03:24:07 -0700978 vcl_evt (VCL_EVT_DETACH, vcm);
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800979
Florin Coras940f78f2018-11-30 12:11:20 -0800980 if (pool_elts (vcm->workers) == 1)
Florin Coras47c40e22018-11-26 17:01:36 -0800981 {
982 vppcom_app_send_detach ();
983 orig_app_timeout = vcm->cfg.app_timeout;
984 vcm->cfg.app_timeout = 2.0;
985 rv = vcl_wait_for_app_state_change (STATE_APP_ENABLED);
986 vcm->cfg.app_timeout = orig_app_timeout;
987 if (PREDICT_FALSE (rv))
988 VDBG (0, "application detach timed out! returning %d (%s)", rv,
989 vppcom_retval_str (rv));
Florin Coras940f78f2018-11-30 12:11:20 -0800990 vec_free (vcm->app_name);
Florin Coras01f3f892018-12-02 12:45:53 -0800991 vcl_worker_cleanup (vcl_worker_get_current (), 0 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -0800992 }
993 else
994 {
Florin Coras01f3f892018-12-02 12:45:53 -0800995 vcl_worker_cleanup (vcl_worker_get_current (), 1 /* notify vpp */ );
Florin Coras47c40e22018-11-26 17:01:36 -0800996 }
Keith Burns (alagalah)8aa9aaf2018-01-05 12:16:22 -0800997
Florin Coras01f3f892018-12-02 12:45:53 -0800998 vcl_set_worker_index (~0);
Florin Coras0d427d82018-06-27 03:24:07 -0700999 vcl_elog_stop (vcm);
Dave Wallace543852a2017-08-03 02:11:34 -04001000 vl_client_disconnect_from_vlib ();
Dave Wallace543852a2017-08-03 02:11:34 -04001001}
1002
1003int
Dave Wallacec04cbf12018-02-07 18:14:02 -05001004vppcom_session_create (u8 proto, u8 is_nonblocking)
Dave Wallace543852a2017-08-03 02:11:34 -04001005{
Florin Coras134a9962018-08-28 11:32:04 -07001006 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001007 vcl_session_t *session;
Dave Wallace543852a2017-08-03 02:11:34 -04001008
Florin Coras134a9962018-08-28 11:32:04 -07001009 session = vcl_session_alloc (wrk);
Dave Wallace543852a2017-08-03 02:11:34 -04001010
Florin Coras7e12d942018-06-27 14:32:43 -07001011 session->session_type = proto;
1012 session->session_state = STATE_START;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001013 session->vpp_handle = ~0;
Florin Coras460dce62018-07-27 05:45:06 -07001014 session->is_dgram = proto == VPPCOM_PROTO_UDP;
Dave Wallace543852a2017-08-03 02:11:34 -04001015
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001016 if (is_nonblocking)
1017 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
Dave Wallace543852a2017-08-03 02:11:34 -04001018
Florin Coras7e12d942018-06-27 14:32:43 -07001019 vcl_evt (VCL_EVT_CREATE, session, session_type, session->session_state,
1020 is_nonblocking, session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001021
Florin Coras5e062572019-03-14 19:07:51 -07001022 VDBG (0, "created session %u", session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001023
Florin Coras134a9962018-08-28 11:32:04 -07001024 return vcl_session_handle (session);
Dave Wallace543852a2017-08-03 02:11:34 -04001025}
1026
1027int
Florin Corasf9240dc2019-01-15 08:03:17 -08001028vcl_session_cleanup (vcl_worker_t * wrk, vcl_session_t * session,
1029 vcl_session_handle_t sh, u8 do_disconnect)
Dave Wallace543852a2017-08-03 02:11:34 -04001030{
Florin Coras288eaab2019-02-03 15:26:14 -08001031 vcl_session_state_t state;
Florin Coras134a9962018-08-28 11:32:04 -07001032 u32 next_sh, vep_sh;
1033 int rv = VPPCOM_OK;
1034 u64 vpp_handle;
Florin Corasf9240dc2019-01-15 08:03:17 -08001035 u8 is_vep;
Florin Coras47c40e22018-11-26 17:01:36 -08001036
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001037 is_vep = session->is_vep;
Florin Coras134a9962018-08-28 11:32:04 -07001038 next_sh = session->vep.next_sh;
1039 vep_sh = session->vep.vep_sh;
Florin Coras7e12d942018-06-27 14:32:43 -07001040 state = session->session_state;
Dave Wallaceee45d412017-11-24 21:44:06 -05001041 vpp_handle = session->vpp_handle;
Dave Wallace543852a2017-08-03 02:11:34 -04001042
Florin Corasf9240dc2019-01-15 08:03:17 -08001043 VDBG (1, "session %u [0x%llx] closing", session->session_index, vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001044
Dave Wallace66cf6eb2017-10-26 02:51:07 -04001045 if (is_vep)
Dave Wallace543852a2017-08-03 02:11:34 -04001046 {
Florin Coras134a9962018-08-28 11:32:04 -07001047 while (next_sh != ~0)
Dave Wallace19481612017-09-15 18:47:44 -04001048 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001049 rv = vppcom_epoll_ctl (sh, EPOLL_CTL_DEL, next_sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001050 if (PREDICT_FALSE (rv < 0))
Florin Coras5e062572019-03-14 19:07:51 -07001051 VDBG (0, "vpp handle 0x%llx, sh %u: EPOLL_CTL_DEL vep_idx %u"
Florin Coras47c40e22018-11-26 17:01:36 -08001052 " failed! rv %d (%s)", vpp_handle, next_sh, vep_sh, rv,
1053 vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001054
Florin Coras134a9962018-08-28 11:32:04 -07001055 next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001056 }
1057 }
1058 else
1059 {
Florin Coras47c40e22018-11-26 17:01:36 -08001060 if (session->is_vep_session)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001061 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001062 rv = vppcom_epoll_ctl (vep_sh, EPOLL_CTL_DEL, sh, 0);
Florin Coras0d427d82018-06-27 03:24:07 -07001063 if (rv < 0)
Florin Corasf9240dc2019-01-15 08:03:17 -08001064 VDBG (0, "session %u [0x%llx]: EPOLL_CTL_DEL vep_idx %u "
1065 "failed! rv %d (%s)", session->session_index, vpp_handle,
1066 vep_sh, rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001067 }
1068
Florin Coras47c40e22018-11-26 17:01:36 -08001069 if (!do_disconnect)
Florin Coras30e79c22019-01-02 19:31:22 -08001070 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08001071 VDBG (1, "session %u [0x%llx] disconnect skipped",
Florin Corasf9240dc2019-01-15 08:03:17 -08001072 session->session_index, vpp_handle);
Florin Coras30e79c22019-01-02 19:31:22 -08001073 goto cleanup;
1074 }
Florin Coras47c40e22018-11-26 17:01:36 -08001075
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001076 if (state & STATE_LISTEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001077 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001078 rv = vppcom_session_unbind (sh);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001079 if (PREDICT_FALSE (rv < 0))
Florin Corasf9240dc2019-01-15 08:03:17 -08001080 VDBG (0, "session %u [0x%llx]: listener unbind failed! "
1081 "rv %d (%s)", session->session_index, vpp_handle, rv,
Florin Coras47c40e22018-11-26 17:01:36 -08001082 vppcom_retval_str (rv));
Florin Corasdfae9f92019-02-20 19:48:31 -08001083 return rv;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001084 }
Florin Coras070453d2018-08-24 17:04:27 -07001085 else if (state & STATE_OPEN)
Dave Wallacef7f809c2017-10-03 01:48:42 -04001086 {
Florin Corasf9240dc2019-01-15 08:03:17 -08001087 rv = vppcom_session_disconnect (sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001088 if (PREDICT_FALSE (rv < 0))
Florin Corasf9240dc2019-01-15 08:03:17 -08001089 VDBG (0, "ERROR: session %u [0x%llx]: disconnect failed!"
1090 " rv %d (%s)", session->session_index, vpp_handle,
1091 rv, vppcom_retval_str (rv));
Dave Wallacef7f809c2017-10-03 01:48:42 -04001092 }
Florin Corasb0f662f2018-12-27 14:51:46 -08001093 else if (state == STATE_DISCONNECT)
1094 {
1095 svm_msg_q_t *mq = vcl_session_vpp_evt_q (wrk, session);
1096 vcl_send_session_reset_reply (mq, wrk->my_client_index,
1097 session->vpp_handle, 0);
1098 }
Dave Wallace19481612017-09-15 18:47:44 -04001099 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001100
Florin Coras0ef8ef22019-01-18 08:37:13 -08001101 VDBG (0, "session %u [0x%llx] removed", session->session_index, vpp_handle);
1102
Florin Corasf9240dc2019-01-15 08:03:17 -08001103cleanup:
Florin Coras30e79c22019-01-02 19:31:22 -08001104 vcl_session_table_del_vpp_handle (wrk, vpp_handle);
Florin Coras134a9962018-08-28 11:32:04 -07001105 vcl_session_free (wrk, session);
Florin Coras0d427d82018-06-27 03:24:07 -07001106 vcl_evt (VCL_EVT_CLOSE, session, rv);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08001107
Dave Wallace543852a2017-08-03 02:11:34 -04001108 return rv;
1109}
1110
1111int
Florin Corasf9240dc2019-01-15 08:03:17 -08001112vppcom_session_close (uint32_t session_handle)
1113{
1114 vcl_worker_t *wrk = vcl_worker_get_current ();
1115 vcl_session_t *session;
1116
1117 session = vcl_session_get_w_handle (wrk, session_handle);
1118 if (!session)
1119 return VPPCOM_EBADFD;
1120 return vcl_session_cleanup (wrk, session, session_handle,
1121 1 /* do_disconnect */ );
1122}
1123
1124int
Florin Coras134a9962018-08-28 11:32:04 -07001125vppcom_session_bind (uint32_t session_handle, vppcom_endpt_t * ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001126{
Florin Coras134a9962018-08-28 11:32:04 -07001127 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001128 vcl_session_t *session = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001129
1130 if (!ep || !ep->ip)
1131 return VPPCOM_EINVAL;
1132
Florin Coras134a9962018-08-28 11:32:04 -07001133 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001134 if (!session)
1135 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001136
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001137 if (session->is_vep)
1138 {
Florin Coras5e062572019-03-14 19:07:51 -07001139 VDBG (0, "ERROR: cannot bind to epoll session %u!",
1140 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001141 return VPPCOM_EBADFD;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001142 }
1143
Florin Coras7e12d942018-06-27 14:32:43 -07001144 session->transport.is_ip4 = ep->is_ip4;
Florin Coras54693d22018-07-17 10:46:29 -07001145 if (ep->is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001146 clib_memcpy_fast (&session->transport.lcl_ip.ip4, ep->ip,
1147 sizeof (ip4_address_t));
Florin Coras54693d22018-07-17 10:46:29 -07001148 else
Dave Barach178cf492018-11-13 16:34:13 -05001149 clib_memcpy_fast (&session->transport.lcl_ip.ip6, ep->ip,
1150 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001151 session->transport.lcl_port = ep->port;
Stevenac1f96d2017-10-24 16:03:58 -07001152
Florin Coras5e062572019-03-14 19:07:51 -07001153 VDBG (0, "session %u handle %u: binding to local %s address %U port %u, "
1154 "proto %s", session->session_index, session_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001155 session->transport.is_ip4 ? "IPv4" : "IPv6",
1156 format_ip46_address, &session->transport.lcl_ip,
1157 session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1158 clib_net_to_host_u16 (session->transport.lcl_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001159 vppcom_proto_str (session->session_type));
Florin Coras0d427d82018-06-27 03:24:07 -07001160 vcl_evt (VCL_EVT_BIND, session);
Florin Coras460dce62018-07-27 05:45:06 -07001161
1162 if (session->session_type == VPPCOM_PROTO_UDP)
Florin Coras134a9962018-08-28 11:32:04 -07001163 vppcom_session_listen (session_handle, 10);
Florin Coras460dce62018-07-27 05:45:06 -07001164
Florin Coras070453d2018-08-24 17:04:27 -07001165 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001166}
1167
1168int
Florin Coras134a9962018-08-28 11:32:04 -07001169vppcom_session_listen (uint32_t listen_sh, uint32_t q_len)
Dave Wallace543852a2017-08-03 02:11:34 -04001170{
Florin Coras134a9962018-08-28 11:32:04 -07001171 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001172 vcl_session_t *listen_session = 0;
Dave Wallaceee45d412017-11-24 21:44:06 -05001173 u64 listen_vpp_handle;
Florin Coras070453d2018-08-24 17:04:27 -07001174 int rv;
1175
Florin Coras134a9962018-08-28 11:32:04 -07001176 listen_session = vcl_session_get_w_handle (wrk, listen_sh);
Florin Coras05ecfcc2018-12-12 18:19:39 -08001177 if (!listen_session || listen_session->is_vep)
Florin Coras070453d2018-08-24 17:04:27 -07001178 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001179
Keith Burns (alagalah)aba98de2018-02-22 03:23:40 -08001180 if (q_len == 0 || q_len == ~0)
1181 q_len = vcm->cfg.listen_queue_size;
1182
Dave Wallaceee45d412017-11-24 21:44:06 -05001183 listen_vpp_handle = listen_session->vpp_handle;
Florin Coras7e12d942018-06-27 14:32:43 -07001184 if (listen_session->session_state & STATE_LISTEN)
Dave Wallacee695cb42017-11-02 22:04:42 -04001185 {
Florin Coras05ecfcc2018-12-12 18:19:39 -08001186 VDBG (0, "session %u [0x%llx]: already in listen state!",
1187 listen_sh, listen_vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001188 return VPPCOM_OK;
Dave Wallacee695cb42017-11-02 22:04:42 -04001189 }
1190
Florin Coras05ecfcc2018-12-12 18:19:39 -08001191 VDBG (0, "session %u [0x%llx]: sending vpp listen request...",
1192 listen_sh, listen_vpp_handle);
Dave Wallace543852a2017-08-03 02:11:34 -04001193
Florin Coras070453d2018-08-24 17:04:27 -07001194 /*
1195 * Send listen request to vpp and wait for reply
1196 */
Florin Coras134a9962018-08-28 11:32:04 -07001197 vppcom_send_bind_sock (listen_session);
1198 rv = vppcom_wait_for_session_state_change (listen_session->session_index,
1199 STATE_LISTEN,
1200 vcm->cfg.session_timeout);
Dave Wallace543852a2017-08-03 02:11:34 -04001201
Florin Coras070453d2018-08-24 17:04:27 -07001202 if (PREDICT_FALSE (rv))
Dave Wallaceee45d412017-11-24 21:44:06 -05001203 {
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 VDBG (0, "session %u [0x%llx]: listen failed! returning %d (%s)",
1206 listen_sh, listen_session->vpp_handle, rv,
1207 vppcom_retval_str (rv));
Florin Coras070453d2018-08-24 17:04:27 -07001208 return rv;
Dave Wallaceee45d412017-11-24 21:44:06 -05001209 }
1210
Florin Coras070453d2018-08-24 17:04:27 -07001211 return VPPCOM_OK;
Keith Burns (alagalah)0d2b0d52018-03-06 15:55:22 -08001212}
1213
Ping Yu34a3a082018-11-30 19:16:17 -05001214int
1215vppcom_session_tls_add_cert (uint32_t session_handle, char *cert,
1216 uint32_t cert_len)
1217{
1218
1219 vcl_worker_t *wrk = vcl_worker_get_current ();
1220 vcl_session_t *session = 0;
1221
1222 session = vcl_session_get_w_handle (wrk, session_handle);
1223 if (!session)
1224 return VPPCOM_EBADFD;
1225
1226 if (cert_len == 0 || cert_len == ~0)
1227 return VPPCOM_EBADFD;
1228
1229 /*
1230 * Send listen request to vpp and wait for reply
1231 */
1232 vppcom_send_application_tls_cert_add (session, cert, cert_len);
1233
1234 return VPPCOM_OK;
1235
1236}
1237
1238int
1239vppcom_session_tls_add_key (uint32_t session_handle, char *key,
1240 uint32_t key_len)
1241{
1242
1243 vcl_worker_t *wrk = vcl_worker_get_current ();
1244 vcl_session_t *session = 0;
1245
1246 session = vcl_session_get_w_handle (wrk, session_handle);
1247 if (!session)
1248 return VPPCOM_EBADFD;
1249
1250 if (key_len == 0 || key_len == ~0)
1251 return VPPCOM_EBADFD;
1252
1253 /*
1254 * Send listen request to vpp and wait for reply
1255 */
1256 vppcom_send_application_tls_key_add (session, key, key_len);
1257
1258 return VPPCOM_OK;
1259
1260
1261}
1262
Florin Coras134a9962018-08-28 11:32:04 -07001263static int
Florin Coras5e062572019-03-14 19:07:51 -07001264validate_args_session_accept_ (vcl_worker_t * wrk, vcl_session_t * ls)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001265{
Florin Coras5e062572019-03-14 19:07:51 -07001266 if (ls->is_vep)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001267 {
Florin Coras5e062572019-03-14 19:07:51 -07001268 VDBG (0, "ERROR: cannot accept on epoll session %u!",
1269 ls->session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001270 return VPPCOM_EBADFD;
1271 }
1272
Florin Coras5e062572019-03-14 19:07:51 -07001273 if (ls->session_state != STATE_LISTEN)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001274 {
Florin Coras5e062572019-03-14 19:07:51 -07001275 VDBG (0, "ERROR: session [0x%llx]: not in listen state! state 0x%x"
1276 " (%s)", ls->vpp_handle, ls->session_index, ls->session_state,
1277 vppcom_session_state_str (ls->session_state));
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001278 return VPPCOM_EBADFD;
1279 }
1280 return VPPCOM_OK;
1281}
1282
1283int
Florin Coras134a9962018-08-28 11:32:04 -07001284vppcom_session_accept (uint32_t listen_session_handle, vppcom_endpt_t * ep,
Dave Wallace048b1d62018-01-03 22:24:41 -05001285 uint32_t flags)
Dave Wallace543852a2017-08-03 02:11:34 -04001286{
Florin Coras3c7d4f92018-12-14 11:28:43 -08001287 u32 client_session_index = ~0, listen_session_index, accept_flags = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001288 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras54693d22018-07-17 10:46:29 -07001289 session_accepted_msg_t accepted_msg;
Florin Coras7e12d942018-06-27 14:32:43 -07001290 vcl_session_t *listen_session = 0;
1291 vcl_session_t *client_session = 0;
Florin Coras54693d22018-07-17 10:46:29 -07001292 vcl_session_msg_t *evt;
Florin Coras54693d22018-07-17 10:46:29 -07001293 svm_msg_q_msg_t msg;
1294 session_event_t *e;
1295 u8 is_nonblocking;
1296 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001297
Florin Coras134a9962018-08-28 11:32:04 -07001298 listen_session = vcl_session_get_w_handle (wrk, listen_session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001299 if (!listen_session)
1300 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001301
Florin Coras134a9962018-08-28 11:32:04 -07001302 listen_session_index = listen_session->session_index;
1303 if ((rv = validate_args_session_accept_ (wrk, listen_session)))
Florin Coras070453d2018-08-24 17:04:27 -07001304 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001305
Florin Coras54693d22018-07-17 10:46:29 -07001306 if (clib_fifo_elts (listen_session->accept_evts_fifo))
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001307 {
Florin Coras54693d22018-07-17 10:46:29 -07001308 clib_fifo_sub2 (listen_session->accept_evts_fifo, evt);
Florin Coras3c7d4f92018-12-14 11:28:43 -08001309 accept_flags = evt->flags;
Florin Coras54693d22018-07-17 10:46:29 -07001310 accepted_msg = evt->accepted_msg;
1311 goto handle;
1312 }
1313
1314 is_nonblocking = VCL_SESS_ATTR_TEST (listen_session->attr,
1315 VCL_SESS_ATTR_NONBLOCK);
Florin Coras134a9962018-08-28 11:32:04 -07001316 if (svm_msg_q_is_empty (wrk->app_event_queue) && is_nonblocking)
Florin Coras54693d22018-07-17 10:46:29 -07001317 return VPPCOM_EAGAIN;
1318
1319 while (1)
1320 {
Florin Coras134a9962018-08-28 11:32:04 -07001321 if (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_WAIT, 0))
Florin Coras54693d22018-07-17 10:46:29 -07001322 return VPPCOM_EAGAIN;
1323
Florin Coras134a9962018-08-28 11:32:04 -07001324 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001325 if (e->event_type != SESSION_CTRL_EVT_ACCEPTED)
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001326 {
Florin Coras54693d22018-07-17 10:46:29 -07001327 clib_warning ("discarded event: %u", e->event_type);
Florin Coras134a9962018-08-28 11:32:04 -07001328 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001329 continue;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001330 }
Dave Barach178cf492018-11-13 16:34:13 -05001331 clib_memcpy_fast (&accepted_msg, e->data, sizeof (accepted_msg));
Florin Coras134a9962018-08-28 11:32:04 -07001332 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001333 break;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001334 }
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001335
Florin Coras54693d22018-07-17 10:46:29 -07001336handle:
Keith Burns (alagalah)00f44cc2018-03-07 09:26:38 -08001337
Florin Coras134a9962018-08-28 11:32:04 -07001338 client_session_index = vcl_session_accepted_handler (wrk, &accepted_msg);
1339 listen_session = vcl_session_get (wrk, listen_session_index);
1340 client_session = vcl_session_get (wrk, client_session_index);
Dave Wallace543852a2017-08-03 02:11:34 -04001341
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001342 if (flags & O_NONBLOCK)
1343 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08001344
Florin Coras5e062572019-03-14 19:07:51 -07001345 VDBG (1, "listener %u [0x%llx]: Got a connect request! session %u [0x%llx],"
1346 " flags %d, is_nonblocking %u", listen_session->session_index,
1347 listen_session->vpp_handle, client_session_index,
1348 client_session->vpp_handle, flags,
1349 VCL_SESS_ATTR_TEST (client_session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace543852a2017-08-03 02:11:34 -04001350
Dave Wallace048b1d62018-01-03 22:24:41 -05001351 if (ep)
1352 {
Florin Coras7e12d942018-06-27 14:32:43 -07001353 ep->is_ip4 = client_session->transport.is_ip4;
1354 ep->port = client_session->transport.rmt_port;
1355 if (client_session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001356 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip4,
1357 sizeof (ip4_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001358 else
Dave Barach178cf492018-11-13 16:34:13 -05001359 clib_memcpy_fast (ep->ip, &client_session->transport.rmt_ip.ip6,
1360 sizeof (ip6_address_t));
Dave Wallace048b1d62018-01-03 22:24:41 -05001361 }
Dave Wallace60caa062017-11-10 17:07:13 -05001362
Florin Coras653e43f2019-03-04 10:56:23 -08001363 vcl_send_session_accepted_reply (client_session->vpp_evt_q,
1364 client_session->client_context,
Florin Coras54693d22018-07-17 10:46:29 -07001365 client_session->vpp_handle, 0);
Dave Wallace60caa062017-11-10 17:07:13 -05001366
Florin Coras05ecfcc2018-12-12 18:19:39 -08001367 VDBG (0, "listener %u [0x%llx] accepted %u [0x%llx] peer: %U:%u "
Florin Coras5e062572019-03-14 19:07:51 -07001368 "local: %U:%u", listen_session_handle, listen_session->vpp_handle,
Florin Coras05ecfcc2018-12-12 18:19:39 -08001369 client_session_index, client_session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001370 format_ip46_address, &client_session->transport.rmt_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001371 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001372 clib_net_to_host_u16 (client_session->transport.rmt_port),
Florin Coras7e12d942018-06-27 14:32:43 -07001373 format_ip46_address, &client_session->transport.lcl_ip,
Florin Coras54693d22018-07-17 10:46:29 -07001374 client_session->transport.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001375 clib_net_to_host_u16 (client_session->transport.lcl_port));
Florin Coras0d427d82018-06-27 03:24:07 -07001376 vcl_evt (VCL_EVT_ACCEPT, client_session, listen_session,
1377 client_session_index);
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08001378
Florin Coras3c7d4f92018-12-14 11:28:43 -08001379 /*
1380 * Session might have been closed already
1381 */
1382 if (accept_flags)
1383 {
Florin Coras3c7d4f92018-12-14 11:28:43 -08001384 if (accept_flags & VCL_ACCEPTED_F_CLOSED)
Florin Corasb0f662f2018-12-27 14:51:46 -08001385 client_session->session_state = STATE_VPP_CLOSING;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001386 else if (accept_flags & VCL_ACCEPTED_F_RESET)
Florin Corasb0f662f2018-12-27 14:51:46 -08001387 client_session->session_state = STATE_DISCONNECT;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001388 }
Florin Corasab2f6db2018-08-31 14:31:41 -07001389 return vcl_session_handle (client_session);
Dave Wallace543852a2017-08-03 02:11:34 -04001390}
1391
1392int
Florin Coras134a9962018-08-28 11:32:04 -07001393vppcom_session_connect (uint32_t session_handle, vppcom_endpt_t * server_ep)
Dave Wallace543852a2017-08-03 02:11:34 -04001394{
Florin Coras134a9962018-08-28 11:32:04 -07001395 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001396 vcl_session_t *session = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001397 u32 session_index;
Florin Coras070453d2018-08-24 17:04:27 -07001398 int rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001399
Florin Coras134a9962018-08-28 11:32:04 -07001400 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001401 if (!session)
1402 return VPPCOM_EBADFD;
Florin Coras134a9962018-08-28 11:32:04 -07001403 session_index = session->session_index;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001404
1405 if (PREDICT_FALSE (session->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001406 {
Florin Coras5e062572019-03-14 19:07:51 -07001407 VDBG (0, "ERROR: cannot connect epoll session %u!",
1408 session->session_index);
Florin Coras070453d2018-08-24 17:04:27 -07001409 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001410 }
1411
Florin Coras7e12d942018-06-27 14:32:43 -07001412 if (PREDICT_FALSE (session->session_state & CLIENT_STATE_OPEN))
Dave Wallace543852a2017-08-03 02:11:34 -04001413 {
Florin Coras7baeb712019-01-04 17:05:43 -08001414 VDBG (0, "session handle %u [0x%llx]: session already "
Florin Coras0d427d82018-06-27 03:24:07 -07001415 "connected to %s %U port %d proto %s, state 0x%x (%s)",
Florin Coras7baeb712019-01-04 17:05:43 -08001416 session_handle, session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001417 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001418 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001419 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001420 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001421 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001422 vppcom_proto_str (session->session_type), session->session_state,
Florin Coras7e12d942018-06-27 14:32:43 -07001423 vppcom_session_state_str (session->session_state));
Florin Coras070453d2018-08-24 17:04:27 -07001424 return VPPCOM_OK;
Dave Wallace543852a2017-08-03 02:11:34 -04001425 }
1426
Florin Coras7e12d942018-06-27 14:32:43 -07001427 session->transport.is_ip4 = server_ep->is_ip4;
1428 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05001429 clib_memcpy_fast (&session->transport.rmt_ip.ip4, server_ep->ip,
1430 sizeof (ip4_address_t));
Dave Wallaced239f8d2018-06-19 13:37:30 -04001431 else
Dave Barach178cf492018-11-13 16:34:13 -05001432 clib_memcpy_fast (&session->transport.rmt_ip.ip6, server_ep->ip,
1433 sizeof (ip6_address_t));
Florin Coras7e12d942018-06-27 14:32:43 -07001434 session->transport.rmt_port = server_ep->port;
Dave Wallace543852a2017-08-03 02:11:34 -04001435
Florin Coras7baeb712019-01-04 17:05:43 -08001436 VDBG (0, "session handle %u [0x%llx]: connecting to server %s %U "
1437 "port %d proto %s", session_handle, session->vpp_handle,
Florin Coras7e12d942018-06-27 14:32:43 -07001438 session->transport.is_ip4 ? "IPv4" : "IPv6",
Florin Coras0d427d82018-06-27 03:24:07 -07001439 format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07001440 &session->transport.rmt_ip, session->transport.is_ip4 ?
Florin Coras0d427d82018-06-27 03:24:07 -07001441 IP46_TYPE_IP4 : IP46_TYPE_IP6,
Florin Coras7e12d942018-06-27 14:32:43 -07001442 clib_net_to_host_u16 (session->transport.rmt_port),
Ping Yu34a3a082018-11-30 19:16:17 -05001443 vppcom_proto_str (session->session_type));
Dave Wallace543852a2017-08-03 02:11:34 -04001444
Florin Coras070453d2018-08-24 17:04:27 -07001445 /*
1446 * Send connect request and wait for reply from vpp
1447 */
Florin Coras134a9962018-08-28 11:32:04 -07001448 vppcom_send_connect_sock (session);
Florin Coras070453d2018-08-24 17:04:27 -07001449 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
1450 vcm->cfg.session_timeout);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001451
Florin Coras134a9962018-08-28 11:32:04 -07001452 session = vcl_session_get (wrk, session_index);
Florin Coras5e062572019-03-14 19:07:51 -07001453 VDBG (0, "session %u [0x%llx]: connect %s!", session->session_index,
1454 session->vpp_handle, rv ? "failed" : "succeeded");
Dave Wallaceee45d412017-11-24 21:44:06 -05001455
Dave Wallace4878cbe2017-11-21 03:45:09 -05001456 return rv;
Dave Wallace543852a2017-08-03 02:11:34 -04001457}
1458
Florin Coras54693d22018-07-17 10:46:29 -07001459static u8
1460vcl_is_rx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1461{
Florin Corasc0737e92019-03-04 14:19:39 -08001462 return (e->event_type == SESSION_IO_EVT_RX && e->session_index == sid);
Florin Coras54693d22018-07-17 10:46:29 -07001463}
1464
Steven58f464e2017-10-25 12:33:12 -07001465static inline int
Florin Coras134a9962018-08-28 11:32:04 -07001466vppcom_session_read_internal (uint32_t session_handle, void *buf, int n,
Steven58f464e2017-10-25 12:33:12 -07001467 u8 peek)
Dave Wallace543852a2017-08-03 02:11:34 -04001468{
Florin Coras134a9962018-08-28 11:32:04 -07001469 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001470 int n_read = 0, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001471 vcl_session_t *s = 0;
Dave Wallace543852a2017-08-03 02:11:34 -04001472 svm_fifo_t *rx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001473 svm_msg_q_msg_t msg;
1474 session_event_t *e;
1475 svm_msg_q_t *mq;
Florin Coras41c9e042018-09-11 00:10:41 -07001476 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001477
Florin Coras070453d2018-08-24 17:04:27 -07001478 if (PREDICT_FALSE (!buf))
1479 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001480
Florin Coras134a9962018-08-28 11:32:04 -07001481 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras2cba8532018-09-11 16:33:36 -07001482 if (PREDICT_FALSE (!s || s->is_vep))
Florin Coras070453d2018-08-24 17:04:27 -07001483 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001484
Florin Coras6d0106e2019-01-29 20:11:58 -08001485 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001486 {
Florin Coras5e062572019-03-14 19:07:51 -07001487 VDBG (0, "session %u[0x%llx] is not open! state 0x%x (%s)",
Florin Coras6d0106e2019-01-29 20:11:58 -08001488 s->session_index, s->vpp_handle, s->session_state,
1489 vppcom_session_state_str (s->session_state));
1490 return vcl_session_closed_error (s);
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001491 }
1492
Florin Coras2cba8532018-09-11 16:33:36 -07001493 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Florin Coras41c9e042018-09-11 00:10:41 -07001494 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001495 mq = wrk->app_event_queue;
1496 rx_fifo = is_ct ? s->ct_rx_fifo : s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001497 s->has_rx_evt = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04001498
Florin Coras54693d22018-07-17 10:46:29 -07001499 if (svm_fifo_is_empty (rx_fifo))
Dave Wallacef7f809c2017-10-03 01:48:42 -04001500 {
Florin Coras54693d22018-07-17 10:46:29 -07001501 if (is_nonblocking)
Florin Corasc0737e92019-03-04 14:19:39 -08001502 {
1503 svm_fifo_unset_event (s->rx_fifo);
1504 return VPPCOM_EWOULDBLOCK;
1505 }
Florin Coras41c9e042018-09-11 00:10:41 -07001506 while (svm_fifo_is_empty (rx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001507 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001508 if (vcl_session_is_closing (s))
1509 return vcl_session_closing_error (s);
1510
Florin Corasc0737e92019-03-04 14:19:39 -08001511 svm_fifo_unset_event (s->rx_fifo);
Florin Coras99368312018-08-02 10:45:44 -07001512 svm_msg_q_lock (mq);
Florin Coras54693d22018-07-17 10:46:29 -07001513 if (svm_msg_q_is_empty (mq))
1514 svm_msg_q_wait (mq);
Florin Coras99368312018-08-02 10:45:44 -07001515
Florin Coras54693d22018-07-17 10:46:29 -07001516 svm_msg_q_sub_w_lock (mq, &msg);
1517 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001518 svm_msg_q_unlock (mq);
Florin Coras41c9e042018-09-11 00:10:41 -07001519 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Corasc0737e92019-03-04 14:19:39 -08001520 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001521 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001522 }
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001523 }
Florin Coras54693d22018-07-17 10:46:29 -07001524
Florin Coras460dce62018-07-27 05:45:06 -07001525 if (s->is_dgram)
Florin Coras99368312018-08-02 10:45:44 -07001526 n_read = app_recv_dgram_raw (rx_fifo, buf, n, &s->transport, 0, peek);
Dave Wallace4878cbe2017-11-21 03:45:09 -05001527 else
Florin Coras99368312018-08-02 10:45:44 -07001528 n_read = app_recv_stream_raw (rx_fifo, buf, n, 0, peek);
Florin Coras54693d22018-07-17 10:46:29 -07001529
Florin Coras41c9e042018-09-11 00:10:41 -07001530 if (svm_fifo_is_empty (rx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08001531 svm_fifo_unset_event (s->rx_fifo);
Florin Coras41c9e042018-09-11 00:10:41 -07001532
Florin Coras5e062572019-03-14 19:07:51 -07001533 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1534 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001535
Florin Coras54693d22018-07-17 10:46:29 -07001536 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001537}
1538
Steven58f464e2017-10-25 12:33:12 -07001539int
Florin Coras134a9962018-08-28 11:32:04 -07001540vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001541{
Florin Coras134a9962018-08-28 11:32:04 -07001542 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001543}
1544
1545static int
Florin Coras134a9962018-08-28 11:32:04 -07001546vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001547{
Florin Coras134a9962018-08-28 11:32:04 -07001548 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001549}
1550
Florin Coras2cba8532018-09-11 16:33:36 -07001551int
1552vppcom_session_read_segments (uint32_t session_handle,
1553 vppcom_data_segments_t ds)
1554{
1555 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001556 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001557 vcl_session_t *s = 0;
1558 svm_fifo_t *rx_fifo;
1559 svm_msg_q_msg_t msg;
1560 session_event_t *e;
1561 svm_msg_q_t *mq;
1562 u8 is_ct;
1563
1564 s = vcl_session_get_w_handle (wrk, session_handle);
1565 if (PREDICT_FALSE (!s || s->is_vep))
1566 return VPPCOM_EBADFD;
1567
Florin Coras6d0106e2019-01-29 20:11:58 -08001568 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1569 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001570
1571 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1572 is_ct = vcl_session_is_ct (s);
1573 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1574 rx_fifo = s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001575 s->has_rx_evt = 0;
Florin Coras2cba8532018-09-11 16:33:36 -07001576
Florin Coras653e43f2019-03-04 10:56:23 -08001577 if (is_ct)
1578 svm_fifo_unset_event (s->rx_fifo);
1579
Florin Coras2cba8532018-09-11 16:33:36 -07001580 if (svm_fifo_is_empty (rx_fifo))
1581 {
1582 if (is_nonblocking)
1583 {
1584 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001585 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001586 }
1587 while (svm_fifo_is_empty (rx_fifo))
1588 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001589 if (vcl_session_is_closing (s))
1590 return vcl_session_closing_error (s);
1591
Florin Coras2cba8532018-09-11 16:33:36 -07001592 svm_fifo_unset_event (rx_fifo);
1593 svm_msg_q_lock (mq);
1594 if (svm_msg_q_is_empty (mq))
1595 svm_msg_q_wait (mq);
1596
1597 svm_msg_q_sub_w_lock (mq, &msg);
1598 e = svm_msg_q_msg_data (mq, &msg);
1599 svm_msg_q_unlock (mq);
1600 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001601 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001602 svm_msg_q_free_msg (mq, &msg);
Florin Coras2cba8532018-09-11 16:33:36 -07001603 }
1604 }
1605
1606 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_segment_t *) ds);
1607 svm_fifo_unset_event (rx_fifo);
1608
Florin Coras2cba8532018-09-11 16:33:36 -07001609 return n_read;
1610}
1611
1612void
1613vppcom_session_free_segments (uint32_t session_handle,
1614 vppcom_data_segments_t ds)
1615{
1616 vcl_worker_t *wrk = vcl_worker_get_current ();
1617 vcl_session_t *s;
1618
1619 s = vcl_session_get_w_handle (wrk, session_handle);
1620 if (PREDICT_FALSE (!s || s->is_vep))
1621 return;
1622
1623 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_segment_t *) ds);
1624}
1625
Florin Coras2cba8532018-09-11 16:33:36 -07001626int
1627vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1628{
1629 u32 first_copy = clib_min (ds[0].len, max_bytes);
Dave Barach178cf492018-11-13 16:34:13 -05001630 clib_memcpy_fast (buf, ds[0].data, first_copy);
Florin Coras2cba8532018-09-11 16:33:36 -07001631 if (first_copy < max_bytes)
1632 {
Dave Barach178cf492018-11-13 16:34:13 -05001633 clib_memcpy_fast (buf + first_copy, ds[1].data,
1634 clib_min (ds[1].len, max_bytes - first_copy));
Florin Coras2cba8532018-09-11 16:33:36 -07001635 }
1636 return 0;
1637}
1638
Florin Coras54693d22018-07-17 10:46:29 -07001639static u8
1640vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1641{
Florin Corasc0737e92019-03-04 14:19:39 -08001642 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04001643}
1644
Florin Coras42ceddb2018-12-12 10:56:01 -08001645static inline int
1646vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
1647 u8 is_flush)
Dave Wallace543852a2017-08-03 02:11:34 -04001648{
Florin Coras134a9962018-08-28 11:32:04 -07001649 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001650 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001651 vcl_session_t *s = 0;
Florin Coras460dce62018-07-27 05:45:06 -07001652 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07001653 svm_msg_q_msg_t msg;
Florin Coras14ed6df2019-03-06 21:13:42 -08001654 svm_fifo_t *tx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001655 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001656 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07001657 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001658
Florin Coras070453d2018-08-24 17:04:27 -07001659 if (PREDICT_FALSE (!buf))
1660 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001661
Florin Coras134a9962018-08-28 11:32:04 -07001662 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001663 if (PREDICT_FALSE (!s))
1664 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001665
Florin Coras460dce62018-07-27 05:45:06 -07001666 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001667 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001668 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
1669 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001670 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001671 }
1672
Florin Coras6d0106e2019-01-29 20:11:58 -08001673 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001674 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001675 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
1676 s->session_index, s->vpp_handle, s->session_state,
1677 vppcom_session_state_str (s->session_state));
1678 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001679 }
1680
Florin Coras0e88e852018-09-17 22:09:02 -07001681 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001682 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras0e88e852018-09-17 22:09:02 -07001683 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Florin Coras653e43f2019-03-04 10:56:23 -08001684 mq = wrk->app_event_queue;
Florin Coras54693d22018-07-17 10:46:29 -07001685 if (svm_fifo_is_full (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04001686 {
Florin Coras54693d22018-07-17 10:46:29 -07001687 if (is_nonblocking)
1688 {
Florin Coras070453d2018-08-24 17:04:27 -07001689 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07001690 }
Florin Coras60f1fc12018-08-16 14:57:31 -07001691 while (svm_fifo_is_full (tx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001692 {
Florin Coras1bcad5c2019-01-09 20:04:38 -08001693 svm_fifo_add_want_tx_ntf (tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08001694 if (vcl_session_is_closing (s))
1695 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07001696 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07001697 if (svm_msg_q_is_empty (mq))
1698 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07001699
Florin Coras54693d22018-07-17 10:46:29 -07001700 svm_msg_q_sub_w_lock (mq, &msg);
1701 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001702 svm_msg_q_unlock (mq);
1703
Florin Coras0e88e852018-09-17 22:09:02 -07001704 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07001705 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001706 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001707 }
Dave Wallace543852a2017-08-03 02:11:34 -04001708 }
Dave Wallace543852a2017-08-03 02:11:34 -04001709
Florin Coras653e43f2019-03-04 10:56:23 -08001710 et = SESSION_IO_EVT_TX;
1711 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08001712 et = SESSION_IO_EVT_TX_FLUSH;
1713
Florin Coras460dce62018-07-27 05:45:06 -07001714 if (s->is_dgram)
1715 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08001716 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08001717 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07001718 else
1719 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08001720 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08001721
Florin Coras14ed6df2019-03-06 21:13:42 -08001722 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08001723 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
1724 et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001725
Florin Coras460dce62018-07-27 05:45:06 -07001726 ASSERT (n_write > 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001727
Florin Coras6d0106e2019-01-29 20:11:58 -08001728 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
1729 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07001730
Florin Coras54693d22018-07-17 10:46:29 -07001731 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04001732}
1733
Florin Coras42ceddb2018-12-12 10:56:01 -08001734int
1735vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1736{
1737 return vppcom_session_write_inline (session_handle, buf, n,
1738 0 /* is_flush */ );
1739}
1740
Florin Corasb0f662f2018-12-27 14:51:46 -08001741int
1742vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
1743{
1744 return vppcom_session_write_inline (session_handle, buf, n,
1745 1 /* is_flush */ );
1746}
1747
Florin Corasc0737e92019-03-04 14:19:39 -08001748#define vcl_fifo_rx_evt_valid_or_break(_s) \
1749if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
1750 { \
1751 if (!vcl_session_is_ct (_s)) \
1752 { \
1753 svm_fifo_unset_event (_s->rx_fifo); \
1754 if (svm_fifo_is_empty (_s->rx_fifo)) \
1755 break; \
1756 } \
1757 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1758 { \
1759 svm_fifo_unset_event (_s->ct_rx_fifo); \
1760 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1761 break; \
1762 } \
1763 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07001764
Florin Coras86f04502018-09-12 16:08:01 -07001765static void
1766vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1767 unsigned long n_bits, unsigned long *read_map,
1768 unsigned long *write_map,
1769 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07001770{
1771 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07001772 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07001773 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07001774 u32 sid;
1775
1776 switch (e->event_type)
1777 {
Florin Corasc0737e92019-03-04 14:19:39 -08001778 case SESSION_IO_EVT_RX:
1779 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001780 session = vcl_session_get (wrk, sid);
1781 if (!session)
1782 break;
Florin Corasfff68f72019-03-13 16:01:38 -07001783 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07001784 if (sid < n_bits && read_map)
1785 {
David Johnsond9818dd2018-12-14 14:53:41 -05001786 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001787 *bits_set += 1;
1788 }
1789 break;
Florin Corasfe97da32019-03-06 10:09:04 -08001790 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08001791 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001792 session = vcl_session_get (wrk, sid);
1793 if (!session)
1794 break;
1795 if (sid < n_bits && write_map)
1796 {
David Johnsond9818dd2018-12-14 14:53:41 -05001797 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001798 *bits_set += 1;
1799 }
1800 break;
Florin Coras86f04502018-09-12 16:08:01 -07001801 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08001802 session = vcl_session_accepted (wrk,
1803 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07001804 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08001805 break;
Florin Coras86f04502018-09-12 16:08:01 -07001806 sid = session->session_index;
1807 if (sid < n_bits && read_map)
1808 {
David Johnsond9818dd2018-12-14 14:53:41 -05001809 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001810 *bits_set += 1;
1811 }
1812 break;
1813 case SESSION_CTRL_EVT_CONNECTED:
1814 connected_msg = (session_connected_msg_t *) e->data;
1815 vcl_session_connected_handler (wrk, connected_msg);
1816 break;
1817 case SESSION_CTRL_EVT_DISCONNECTED:
1818 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001819 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
1820 if (!session)
1821 break;
1822 sid = session->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001823 if (sid < n_bits && except_map)
1824 {
David Johnsond9818dd2018-12-14 14:53:41 -05001825 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001826 *bits_set += 1;
1827 }
1828 break;
1829 case SESSION_CTRL_EVT_RESET:
1830 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1831 if (sid < n_bits && except_map)
1832 {
David Johnsond9818dd2018-12-14 14:53:41 -05001833 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001834 *bits_set += 1;
1835 }
1836 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001837 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1838 vcl_session_unlisten_reply_handler (wrk, e->data);
1839 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001840 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1841 vcl_session_worker_update_reply_handler (wrk, e->data);
1842 break;
1843 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1844 vcl_session_req_worker_update_handler (wrk, e->data);
1845 break;
Florin Coras86f04502018-09-12 16:08:01 -07001846 default:
1847 clib_warning ("unhandled: %u", e->event_type);
1848 break;
1849 }
1850}
1851
1852static int
1853vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1854 unsigned long n_bits, unsigned long *read_map,
1855 unsigned long *write_map, unsigned long *except_map,
1856 double time_to_wait, u32 * bits_set)
1857{
Florin Coras99368312018-08-02 10:45:44 -07001858 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07001859 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07001860 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07001861
1862 svm_msg_q_lock (mq);
1863 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001864 {
Florin Coras54693d22018-07-17 10:46:29 -07001865 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001866 {
Florin Coras54693d22018-07-17 10:46:29 -07001867 svm_msg_q_unlock (mq);
1868 return 0;
1869 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001870
Florin Coras54693d22018-07-17 10:46:29 -07001871 if (!time_to_wait)
1872 {
1873 svm_msg_q_unlock (mq);
1874 return 0;
1875 }
1876 else if (time_to_wait < 0)
1877 {
1878 svm_msg_q_wait (mq);
1879 }
1880 else
1881 {
1882 if (svm_msg_q_timedwait (mq, time_to_wait))
1883 {
1884 svm_msg_q_unlock (mq);
1885 return 0;
1886 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001887 }
1888 }
Florin Coras134a9962018-08-28 11:32:04 -07001889 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07001890 svm_msg_q_unlock (mq);
1891
Florin Coras134a9962018-08-28 11:32:04 -07001892 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07001893 {
Florin Coras134a9962018-08-28 11:32:04 -07001894 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07001895 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07001896 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
1897 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07001898 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07001899 }
Florin Coras134a9962018-08-28 11:32:04 -07001900 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08001901 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07001902 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04001903}
1904
Florin Coras99368312018-08-02 10:45:44 -07001905static int
Florin Coras294afe22019-01-07 17:49:17 -08001906vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
1907 vcl_si_set * read_map, vcl_si_set * write_map,
1908 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07001909 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07001910{
Florin Coras14ed6df2019-03-06 21:13:42 -08001911 double wait = 0, start = 0;
1912
1913 if (!*bits_set)
1914 {
1915 wait = time_to_wait;
1916 start = clib_time_now (&wrk->clib_time);
1917 }
1918
1919 do
1920 {
1921 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
1922 write_map, except_map, wait, bits_set);
1923 if (*bits_set)
1924 return *bits_set;
1925 if (wait == -1)
1926 continue;
1927
1928 wait = wait - (clib_time_now (&wrk->clib_time) - start);
1929 }
1930 while (wait > 0);
1931
1932 return 0;
Florin Coras99368312018-08-02 10:45:44 -07001933}
1934
1935static int
Florin Coras294afe22019-01-07 17:49:17 -08001936vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
1937 vcl_si_set * read_map, vcl_si_set * write_map,
1938 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07001939 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07001940{
1941 vcl_mq_evt_conn_t *mqc;
1942 int __clib_unused n_read;
1943 int n_mq_evts, i;
1944 u64 buf;
1945
Florin Coras134a9962018-08-28 11:32:04 -07001946 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
1947 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
1948 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07001949 for (i = 0; i < n_mq_evts; i++)
1950 {
Florin Coras134a9962018-08-28 11:32:04 -07001951 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07001952 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07001953 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07001954 except_map, 0, bits_set);
1955 }
1956
1957 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1958}
1959
Dave Wallace543852a2017-08-03 02:11:34 -04001960int
Florin Coras294afe22019-01-07 17:49:17 -08001961vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
1962 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04001963{
Florin Coras54693d22018-07-17 10:46:29 -07001964 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001965 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001966 vcl_session_t *session = 0;
Florin Coras86f04502018-09-12 16:08:01 -07001967 int rv, i;
Dave Wallace543852a2017-08-03 02:11:34 -04001968
Dave Wallace7876d392017-10-19 03:53:57 -04001969 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001970 {
Florin Coras134a9962018-08-28 11:32:04 -07001971 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05001972 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08001973 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
1974 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04001975 }
Dave Wallace7876d392017-10-19 03:53:57 -04001976 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001977 {
Florin Coras134a9962018-08-28 11:32:04 -07001978 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05001979 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08001980 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
1981 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04001982 }
Dave Wallace7876d392017-10-19 03:53:57 -04001983 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001984 {
Florin Coras134a9962018-08-28 11:32:04 -07001985 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05001986 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08001987 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
1988 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04001989 }
1990
Florin Coras54693d22018-07-17 10:46:29 -07001991 if (!n_bits)
1992 return 0;
1993
1994 if (!write_map)
1995 goto check_rd;
1996
1997 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07001998 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
1999 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002000 {
Florin Coras47c40e22018-11-26 17:01:36 -08002001 if (except_map && sid < minbits)
2002 clib_bitmap_set_no_check (except_map, sid, 1);
2003 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002004 }
2005
2006 rv = svm_fifo_is_full (session->tx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07002007 if (!rv)
2008 {
David Johnsond9818dd2018-12-14 14:53:41 -05002009 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002010 bits_set++;
2011 }
Florin Coras294afe22019-01-07 17:49:17 -08002012 else
Florin Coras1bcad5c2019-01-09 20:04:38 -08002013 svm_fifo_add_want_tx_ntf (session->tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
Florin Coras54693d22018-07-17 10:46:29 -07002014 }));
2015
2016check_rd:
2017 if (!read_map)
2018 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002019
Florin Coras134a9962018-08-28 11:32:04 -07002020 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2021 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002022 {
Florin Coras47c40e22018-11-26 17:01:36 -08002023 if (except_map && sid < minbits)
2024 clib_bitmap_set_no_check (except_map, sid, 1);
2025 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002026 }
2027
Florin Coras0ef8ef22019-01-18 08:37:13 -08002028 rv = vcl_session_read_ready (session);
Florin Coras54693d22018-07-17 10:46:29 -07002029 if (rv)
2030 {
David Johnsond9818dd2018-12-14 14:53:41 -05002031 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002032 bits_set++;
2033 }
2034 }));
2035 /* *INDENT-ON* */
2036
2037check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002038
Florin Coras86f04502018-09-12 16:08:01 -07002039 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2040 {
2041 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2042 read_map, write_map, except_map, &bits_set);
2043 }
2044 vec_reset_length (wrk->unhandled_evts_vector);
2045
Florin Coras99368312018-08-02 10:45:44 -07002046 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002047 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002048 time_to_wait, &bits_set);
2049 else
Florin Coras134a9962018-08-28 11:32:04 -07002050 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002051 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002052
Dave Wallace543852a2017-08-03 02:11:34 -04002053 return (bits_set);
2054}
2055
Dave Wallacef7f809c2017-10-03 01:48:42 -04002056static inline void
Florin Coras134a9962018-08-28 11:32:04 -07002057vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002058{
Florin Coras7e12d942018-06-27 14:32:43 -07002059 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002060 vppcom_epoll_t *vep;
Dave Wallace498b3a52017-11-09 13:00:34 -05002061 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002062
Florin Corasc0737e92019-03-04 14:19:39 -08002063 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002064 return;
2065
Florin Coras134a9962018-08-28 11:32:04 -07002066 session = vcl_session_get (wrk, vep_idx);
Florin Coras070453d2018-08-24 17:04:27 -07002067 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002068 {
Florin Coras5e062572019-03-14 19:07:51 -07002069 VDBG (0, "ERROR: Invalid vep_idx (%u)!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002070 goto done;
2071 }
2072 if (PREDICT_FALSE (!session->is_vep))
2073 {
Florin Coras5e062572019-03-14 19:07:51 -07002074 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002075 goto done;
2076 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002077 vep = &session->vep;
Florin Coras5e062572019-03-14 19:07:51 -07002078 VDBG (0, "vep_idx (%u): Dumping epoll chain\n"
2079 "{\n"
2080 " is_vep = %u\n"
2081 " is_vep_session = %u\n"
2082 " next_sid = 0x%x (%u)\n"
2083 "}\n", vep_idx, session->is_vep, session->is_vep_session,
2084 vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002085
Florin Coras134a9962018-08-28 11:32:04 -07002086 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002087 {
Florin Coras134a9962018-08-28 11:32:04 -07002088 session = vcl_session_get (wrk, sid);
Florin Coras070453d2018-08-24 17:04:27 -07002089 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002090 {
Florin Coras5e062572019-03-14 19:07:51 -07002091 VDBG (0, "ERROR: Invalid sid (%u)!", sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002092 goto done;
2093 }
2094 if (PREDICT_FALSE (session->is_vep))
Florin Coras5e062572019-03-14 19:07:51 -07002095 {
2096 VDBG (0, "ERROR: sid (%u) is a vep!", vep_idx);
2097 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002098 else if (PREDICT_FALSE (!session->is_vep_session))
2099 {
Florin Coras5e062572019-03-14 19:07:51 -07002100 VDBG (0, "ERROR: session (%u) is not a vep session!", sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002101 goto done;
2102 }
2103 vep = &session->vep;
Florin Coras134a9962018-08-28 11:32:04 -07002104 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
Florin Coras5e062572019-03-14 19:07:51 -07002105 VDBG (0, "ERROR: session (%u) vep_idx (%u) != vep_idx (%u)!",
2106 sid, session->vep.vep_sh, vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002107 if (session->is_vep_session)
2108 {
Florin Coras5e062572019-03-14 19:07:51 -07002109 VDBG (0, "vep_idx[%u]: sid 0x%x (%u)\n"
2110 "{\n"
2111 " next_sid = 0x%x (%u)\n"
2112 " prev_sid = 0x%x (%u)\n"
2113 " vep_idx = 0x%x (%u)\n"
2114 " ev.events = 0x%x\n"
2115 " ev.data.u64 = 0x%llx\n"
2116 " et_mask = 0x%x\n"
2117 "}\n",
2118 vep_idx, sid, sid, vep->next_sh, vep->next_sh, vep->prev_sh,
2119 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2120 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002121 }
2122 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002123
2124done:
Florin Coras5e062572019-03-14 19:07:51 -07002125 VDBG (0, "vep_idx (%u): Dump complete!\n", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002126}
2127
2128int
2129vppcom_epoll_create (void)
2130{
Florin Coras134a9962018-08-28 11:32:04 -07002131 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002132 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002133
Florin Coras134a9962018-08-28 11:32:04 -07002134 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002135
2136 vep_session->is_vep = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002137 vep_session->vep.vep_sh = ~0;
2138 vep_session->vep.next_sh = ~0;
2139 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002140 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002141
Florin Corasa7a1a222018-12-30 17:11:31 -08002142 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2143 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002144
Florin Corasab2f6db2018-08-31 14:31:41 -07002145 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002146}
2147
2148int
Florin Coras134a9962018-08-28 11:32:04 -07002149vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002150 struct epoll_event *event)
2151{
Florin Coras134a9962018-08-28 11:32:04 -07002152 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002153 vcl_session_t *vep_session;
2154 vcl_session_t *session;
Florin Coras070453d2018-08-24 17:04:27 -07002155 int rv = VPPCOM_OK;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002156
Florin Coras134a9962018-08-28 11:32:04 -07002157 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002158 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002159 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002160 return VPPCOM_EINVAL;
2161 }
2162
Florin Coras134a9962018-08-28 11:32:04 -07002163 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002164 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002165 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002166 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002167 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002168 }
2169 if (PREDICT_FALSE (!vep_session->is_vep))
2170 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002171 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002172 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002173 }
2174
Florin Coras134a9962018-08-28 11:32:04 -07002175 ASSERT (vep_session->vep.vep_sh == ~0);
2176 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002177
Florin Coras134a9962018-08-28 11:32:04 -07002178 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002179 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002180 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002181 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002182 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002183 }
2184 if (PREDICT_FALSE (session->is_vep))
2185 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002186 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002187 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002188 }
2189
2190 switch (op)
2191 {
2192 case EPOLL_CTL_ADD:
2193 if (PREDICT_FALSE (!event))
2194 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002195 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002196 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002197 }
Florin Coras134a9962018-08-28 11:32:04 -07002198 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002199 {
Florin Coras7e12d942018-06-27 14:32:43 -07002200 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002201 next_session = vcl_session_get_w_handle (wrk,
2202 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002203 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002204 {
Florin Coras5e062572019-03-14 19:07:51 -07002205 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002206 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002207 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002208 }
Florin Coras134a9962018-08-28 11:32:04 -07002209 ASSERT (next_session->vep.prev_sh == vep_handle);
2210 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002211 }
Florin Coras134a9962018-08-28 11:32:04 -07002212 session->vep.next_sh = vep_session->vep.next_sh;
2213 session->vep.prev_sh = vep_handle;
2214 session->vep.vep_sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002215 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2216 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002217 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002218 session->is_vep_session = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002219 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002220
Florin Coras1bcad5c2019-01-09 20:04:38 -08002221 if (session->tx_fifo)
2222 svm_fifo_add_want_tx_ntf (session->tx_fifo,
2223 SVM_FIFO_WANT_TX_NOTIF_IF_FULL);
2224
Florin Corasa7a1a222018-12-30 17:11:31 -08002225 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2226 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras0d427d82018-06-27 03:24:07 -07002227 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002228 break;
2229
2230 case EPOLL_CTL_MOD:
2231 if (PREDICT_FALSE (!event))
2232 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002233 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002234 rv = VPPCOM_EINVAL;
2235 goto done;
2236 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002237 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002238 {
Florin Coras5e062572019-03-14 19:07:51 -07002239 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002240 rv = VPPCOM_EINVAL;
2241 goto done;
2242 }
Florin Coras134a9962018-08-28 11:32:04 -07002243 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002244 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002245 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2246 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002247 rv = VPPCOM_EINVAL;
2248 goto done;
2249 }
2250 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2251 session->vep.ev = *event;
Florin Corasa7a1a222018-12-30 17:11:31 -08002252 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2253 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002254 break;
2255
2256 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05002257 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002258 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002259 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002260 rv = VPPCOM_EINVAL;
2261 goto done;
2262 }
Florin Coras134a9962018-08-28 11:32:04 -07002263 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002264 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002265 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2266 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002267 rv = VPPCOM_EINVAL;
2268 goto done;
2269 }
2270
Florin Coras134a9962018-08-28 11:32:04 -07002271 if (session->vep.prev_sh == vep_handle)
2272 vep_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002273 else
2274 {
Florin Coras7e12d942018-06-27 14:32:43 -07002275 vcl_session_t *prev_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002276 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002277 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002278 {
Florin Coras5e062572019-03-14 19:07:51 -07002279 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Corasa7a1a222018-12-30 17:11:31 -08002280 session->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002281 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002282 }
Florin Coras134a9962018-08-28 11:32:04 -07002283 ASSERT (prev_session->vep.next_sh == session_handle);
2284 prev_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002285 }
Florin Coras134a9962018-08-28 11:32:04 -07002286 if (session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002287 {
Florin Coras7e12d942018-06-27 14:32:43 -07002288 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002289 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002290 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002291 {
Florin Coras5e062572019-03-14 19:07:51 -07002292 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Corasa7a1a222018-12-30 17:11:31 -08002293 session->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002294 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002295 }
Florin Coras134a9962018-08-28 11:32:04 -07002296 ASSERT (next_session->vep.prev_sh == session_handle);
2297 next_session->vep.prev_sh = session->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002298 }
2299
2300 memset (&session->vep, 0, sizeof (session->vep));
Florin Coras134a9962018-08-28 11:32:04 -07002301 session->vep.next_sh = ~0;
2302 session->vep.prev_sh = ~0;
2303 session->vep.vep_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002304 session->is_vep_session = 0;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002305
2306 if (session->tx_fifo)
2307 svm_fifo_del_want_tx_ntf (session->tx_fifo, SVM_FIFO_NO_TX_NOTIF);
2308
Florin Coras5e062572019-03-14 19:07:51 -07002309 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002310 session_handle);
Florin Coras134a9962018-08-28 11:32:04 -07002311 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002312 break;
2313
2314 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002315 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002316 rv = VPPCOM_EINVAL;
2317 }
2318
Florin Coras134a9962018-08-28 11:32:04 -07002319 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002320
2321done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002322 return rv;
2323}
2324
Florin Coras86f04502018-09-12 16:08:01 -07002325static inline void
2326vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2327 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002328{
2329 session_disconnected_msg_t *disconnected_msg;
2330 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002331 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002332 u64 session_evt_data = ~0;
Florin Coras54693d22018-07-17 10:46:29 -07002333 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002334 u8 add_event = 0;
2335
2336 switch (e->event_type)
2337 {
Florin Coras653e43f2019-03-04 10:56:23 -08002338 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002339 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002340 if (!(session = vcl_session_get (wrk, sid)))
2341 break;
Florin Corasc0737e92019-03-04 14:19:39 -08002342 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07002343 session_events = session->vep.ev.events;
Florin Corasaa27eb92018-10-13 12:20:01 -07002344 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
Florin Coras86f04502018-09-12 16:08:01 -07002345 break;
2346 add_event = 1;
2347 events[*num_ev].events |= EPOLLIN;
2348 session_evt_data = session->vep.ev.data.u64;
Florin Corasaa27eb92018-10-13 12:20:01 -07002349 session->has_rx_evt = 1;
Florin Coras86f04502018-09-12 16:08:01 -07002350 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002351 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002352 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002353 if (!(session = vcl_session_get (wrk, sid)))
2354 break;
Florin Coras86f04502018-09-12 16:08:01 -07002355 session_events = session->vep.ev.events;
2356 if (!(EPOLLOUT & session_events))
2357 break;
2358 add_event = 1;
2359 events[*num_ev].events |= EPOLLOUT;
2360 session_evt_data = session->vep.ev.data.u64;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002361 svm_fifo_reset_tx_ntf (session->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002362 break;
Florin Coras86f04502018-09-12 16:08:01 -07002363 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002364 session = vcl_session_accepted (wrk,
2365 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002366 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002367 break;
Florin Coras86f04502018-09-12 16:08:01 -07002368
Florin Coras86f04502018-09-12 16:08:01 -07002369 session_events = session->vep.ev.events;
2370 if (!(EPOLLIN & session_events))
2371 break;
2372
2373 add_event = 1;
2374 events[*num_ev].events |= EPOLLIN;
2375 session_evt_data = session->vep.ev.data.u64;
2376 break;
2377 case SESSION_CTRL_EVT_CONNECTED:
2378 connected_msg = (session_connected_msg_t *) e->data;
2379 vcl_session_connected_handler (wrk, connected_msg);
2380 /* Generate EPOLLOUT because there's no connected event */
2381 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
Florin Corasfa915f82018-12-26 16:29:06 -08002382 if (!(session = vcl_session_get (wrk, sid)))
2383 break;
Florin Coras86f04502018-09-12 16:08:01 -07002384 session_events = session->vep.ev.events;
Florin Coras72f77822019-01-22 19:05:52 -08002385 if (!(EPOLLOUT & session_events))
2386 break;
2387 add_event = 1;
2388 events[*num_ev].events |= EPOLLOUT;
2389 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002390 break;
2391 case SESSION_CTRL_EVT_DISCONNECTED:
2392 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002393 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2394 if (!session)
Florin Coras86f04502018-09-12 16:08:01 -07002395 break;
Florin Coras72f77822019-01-22 19:05:52 -08002396 session_events = session->vep.ev.events;
2397 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2398 break;
Florin Coras86f04502018-09-12 16:08:01 -07002399 add_event = 1;
2400 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2401 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002402 break;
2403 case SESSION_CTRL_EVT_RESET:
2404 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2405 if (!(session = vcl_session_get (wrk, sid)))
2406 break;
Florin Coras72f77822019-01-22 19:05:52 -08002407 session_events = session->vep.ev.events;
2408 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2409 break;
Florin Coras86f04502018-09-12 16:08:01 -07002410 add_event = 1;
2411 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2412 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002413 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002414 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2415 vcl_session_unlisten_reply_handler (wrk, e->data);
2416 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002417 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2418 vcl_session_req_worker_update_handler (wrk, e->data);
2419 break;
2420 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2421 vcl_session_worker_update_reply_handler (wrk, e->data);
2422 break;
Florin Coras86f04502018-09-12 16:08:01 -07002423 default:
2424 VDBG (0, "unhandled: %u", e->event_type);
2425 break;
2426 }
2427
2428 if (add_event)
2429 {
2430 events[*num_ev].data.u64 = session_evt_data;
2431 if (EPOLLONESHOT & session_events)
2432 {
2433 session = vcl_session_get (wrk, sid);
2434 session->vep.ev.events = 0;
2435 }
2436 *num_ev += 1;
2437 }
2438}
2439
2440static int
2441vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2442 struct epoll_event *events, u32 maxevents,
2443 double wait_for_time, u32 * num_ev)
2444{
Florin Coras99368312018-08-02 10:45:44 -07002445 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002446 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002447 int i;
2448
Florin Coras539663c2018-09-28 14:59:37 -07002449 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2450 goto handle_dequeued;
2451
Florin Coras54693d22018-07-17 10:46:29 -07002452 svm_msg_q_lock (mq);
2453 if (svm_msg_q_is_empty (mq))
2454 {
2455 if (!wait_for_time)
2456 {
2457 svm_msg_q_unlock (mq);
2458 return 0;
2459 }
2460 else if (wait_for_time < 0)
2461 {
2462 svm_msg_q_wait (mq);
2463 }
2464 else
2465 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002466 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002467 {
2468 svm_msg_q_unlock (mq);
2469 return 0;
2470 }
2471 }
2472 }
Florin Coras134a9962018-08-28 11:32:04 -07002473 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07002474 svm_msg_q_unlock (mq);
2475
Florin Coras539663c2018-09-28 14:59:37 -07002476handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002477 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002478 {
Florin Coras134a9962018-08-28 11:32:04 -07002479 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002480 e = svm_msg_q_msg_data (mq, msg);
Florin Corasaa27eb92018-10-13 12:20:01 -07002481 if (*num_ev < maxevents)
2482 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2483 else
2484 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras99368312018-08-02 10:45:44 -07002485 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002486 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002487 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002488 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002489 return *num_ev;
2490}
2491
Florin Coras99368312018-08-02 10:45:44 -07002492static int
Florin Coras134a9962018-08-28 11:32:04 -07002493vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002494 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002495{
Florin Coras14ed6df2019-03-06 21:13:42 -08002496 double wait = 0, start = 0;
2497
2498 if (!n_evts)
2499 {
2500 wait = wait_for_time;
2501 start = clib_time_now (&wrk->clib_time);
2502 }
2503
2504 do
2505 {
2506 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2507 wait, &n_evts);
2508 if (n_evts)
2509 return n_evts;
2510 if (wait == -1)
2511 continue;
2512
2513 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2514 }
2515 while (wait > 0);
2516
2517 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002518}
2519
2520static int
Florin Coras134a9962018-08-28 11:32:04 -07002521vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002522 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002523{
2524 vcl_mq_evt_conn_t *mqc;
2525 int __clib_unused n_read;
2526 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07002527 u64 buf;
2528
Florin Coras134a9962018-08-28 11:32:04 -07002529 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07002530again:
Florin Coras134a9962018-08-28 11:32:04 -07002531 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2532 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07002533 for (i = 0; i < n_mq_evts; i++)
2534 {
Florin Coras134a9962018-08-28 11:32:04 -07002535 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002536 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002537 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002538 }
Florin Corasa4878ed2018-10-12 13:09:36 -07002539 if (!n_evts && n_mq_evts > 0)
2540 goto again;
Florin Coras99368312018-08-02 10:45:44 -07002541
2542 return (int) n_evts;
2543}
2544
Dave Wallacef7f809c2017-10-03 01:48:42 -04002545int
Florin Coras134a9962018-08-28 11:32:04 -07002546vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002547 int maxevents, double wait_for_time)
2548{
Florin Coras134a9962018-08-28 11:32:04 -07002549 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002550 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07002551 u32 n_evts = 0;
2552 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002553
2554 if (PREDICT_FALSE (maxevents <= 0))
2555 {
Florin Coras5e062572019-03-14 19:07:51 -07002556 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002557 return VPPCOM_EINVAL;
2558 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002559
Florin Coras134a9962018-08-28 11:32:04 -07002560 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07002561 if (!vep_session)
2562 return VPPCOM_EBADFD;
2563
Florin Coras54693d22018-07-17 10:46:29 -07002564 if (PREDICT_FALSE (!vep_session->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002565 {
Florin Coras5e062572019-03-14 19:07:51 -07002566 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07002567 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002568 }
Florin Coras54693d22018-07-17 10:46:29 -07002569
2570 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002571
Florin Coras86f04502018-09-12 16:08:01 -07002572 if (vec_len (wrk->unhandled_evts_vector))
2573 {
2574 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2575 {
2576 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2577 events, &n_evts);
2578 if (n_evts == maxevents)
2579 {
2580 i += 1;
2581 break;
2582 }
2583 }
Florin Coras86f04502018-09-12 16:08:01 -07002584 vec_delete (wrk->unhandled_evts_vector, i, 0);
2585 }
2586
2587 if (vcm->cfg.use_mq_eventfd)
2588 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2589 wait_for_time);
2590
2591 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2592 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002593}
2594
Dave Wallace35830af2017-10-09 01:43:42 -04002595int
Florin Coras134a9962018-08-28 11:32:04 -07002596vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04002597 void *buffer, uint32_t * buflen)
2598{
Florin Coras134a9962018-08-28 11:32:04 -07002599 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002600 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04002601 int rv = VPPCOM_OK;
Florin Coras7baeb712019-01-04 17:05:43 -08002602 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07002603 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04002604
Florin Coras134a9962018-08-28 11:32:04 -07002605 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002606 if (!session)
2607 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002608
Dave Wallace35830af2017-10-09 01:43:42 -04002609 switch (op)
2610 {
2611 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002612 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07002613 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
2614 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002615 break;
2616
Dave Wallace227867f2017-11-13 21:21:53 -05002617 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002618 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07002619 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
2620 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002621 break;
2622
2623 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002624 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002625 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002626 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2627 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002628 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07002629 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
2630 "is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002631 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002632 }
2633 else
2634 rv = VPPCOM_EINVAL;
2635 break;
2636
2637 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002638 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002639 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002640 if (*flags & O_NONBLOCK)
2641 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2642 else
2643 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2644
Florin Coras5e062572019-03-14 19:07:51 -07002645 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
2646 " is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002647 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002648 }
2649 else
2650 rv = VPPCOM_EINVAL;
2651 break;
2652
2653 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002654 if (PREDICT_TRUE (buffer && buflen &&
2655 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002656 {
Florin Coras7e12d942018-06-27 14:32:43 -07002657 ep->is_ip4 = session->transport.is_ip4;
2658 ep->port = session->transport.rmt_port;
2659 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002660 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2661 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002662 else
Dave Barach178cf492018-11-13 16:34:13 -05002663 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2664 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002665 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07002666 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
2667 "addr = %U, port %u", session_handle, ep->is_ip4,
2668 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002669 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2670 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002671 }
2672 else
2673 rv = VPPCOM_EINVAL;
2674 break;
2675
2676 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002677 if (PREDICT_TRUE (buffer && buflen &&
2678 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002679 {
Florin Coras7e12d942018-06-27 14:32:43 -07002680 ep->is_ip4 = session->transport.is_ip4;
2681 ep->port = session->transport.lcl_port;
2682 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002683 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2684 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002685 else
Dave Barach178cf492018-11-13 16:34:13 -05002686 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2687 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002688 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07002689 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
2690 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002691 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002692 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2693 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002694 }
2695 else
2696 rv = VPPCOM_EINVAL;
2697 break;
Stevenb5a11602017-10-11 09:59:30 -07002698
Dave Wallace048b1d62018-01-03 22:24:41 -05002699 case VPPCOM_ATTR_GET_LIBC_EPFD:
2700 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07002701 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002702 break;
2703
2704 case VPPCOM_ATTR_SET_LIBC_EPFD:
2705 if (PREDICT_TRUE (buffer && buflen &&
2706 (*buflen == sizeof (session->libc_epfd))))
2707 {
2708 session->libc_epfd = *(int *) buffer;
2709 *buflen = sizeof (session->libc_epfd);
2710
Florin Coras5e062572019-03-14 19:07:51 -07002711 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
2712 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002713 }
2714 else
2715 rv = VPPCOM_EINVAL;
2716 break;
2717
2718 case VPPCOM_ATTR_GET_PROTOCOL:
2719 if (buffer && buflen && (*buflen >= sizeof (int)))
2720 {
Florin Coras7e12d942018-06-27 14:32:43 -07002721 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05002722 *buflen = sizeof (int);
2723
Florin Coras5e062572019-03-14 19:07:51 -07002724 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2725 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002726 }
2727 else
2728 rv = VPPCOM_EINVAL;
2729 break;
2730
2731 case VPPCOM_ATTR_GET_LISTEN:
2732 if (buffer && buflen && (*buflen >= sizeof (int)))
2733 {
2734 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2735 VCL_SESS_ATTR_LISTEN);
2736 *buflen = sizeof (int);
2737
Florin Coras5e062572019-03-14 19:07:51 -07002738 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
2739 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002740 }
2741 else
2742 rv = VPPCOM_EINVAL;
2743 break;
2744
2745 case VPPCOM_ATTR_GET_ERROR:
2746 if (buffer && buflen && (*buflen >= sizeof (int)))
2747 {
2748 *(int *) buffer = 0;
2749 *buflen = sizeof (int);
2750
Florin Coras5e062572019-03-14 19:07:51 -07002751 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2752 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002753 }
2754 else
2755 rv = VPPCOM_EINVAL;
2756 break;
2757
2758 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2759 if (buffer && buflen && (*buflen >= sizeof (u32)))
2760 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002761
2762 /* VPP-TBD */
2763 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002764 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002765 vcm->cfg.tx_fifo_size);
2766 *buflen = sizeof (u32);
2767
Florin Coras5e062572019-03-14 19:07:51 -07002768 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
2769 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
2770 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002771 }
2772 else
2773 rv = VPPCOM_EINVAL;
2774 break;
2775
2776 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2777 if (buffer && buflen && (*buflen == sizeof (u32)))
2778 {
2779 /* VPP-TBD */
2780 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07002781 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
2782 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
2783 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002784 }
2785 else
2786 rv = VPPCOM_EINVAL;
2787 break;
2788
2789 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2790 if (buffer && buflen && (*buflen >= sizeof (u32)))
2791 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002792
2793 /* VPP-TBD */
2794 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002795 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002796 vcm->cfg.rx_fifo_size);
2797 *buflen = sizeof (u32);
2798
Florin Coras5e062572019-03-14 19:07:51 -07002799 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
2800 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002801 }
2802 else
2803 rv = VPPCOM_EINVAL;
2804 break;
2805
2806 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2807 if (buffer && buflen && (*buflen == sizeof (u32)))
2808 {
2809 /* VPP-TBD */
2810 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07002811 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
2812 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
2813 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002814 }
2815 else
2816 rv = VPPCOM_EINVAL;
2817 break;
2818
2819 case VPPCOM_ATTR_GET_REUSEADDR:
2820 if (buffer && buflen && (*buflen >= sizeof (int)))
2821 {
2822 /* VPP-TBD */
2823 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2824 VCL_SESS_ATTR_REUSEADDR);
2825 *buflen = sizeof (int);
2826
Florin Coras5e062572019-03-14 19:07:51 -07002827 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
2828 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002829 }
2830 else
2831 rv = VPPCOM_EINVAL;
2832 break;
2833
Stevenb5a11602017-10-11 09:59:30 -07002834 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002835 if (buffer && buflen && (*buflen == sizeof (int)) &&
2836 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2837 {
2838 /* VPP-TBD */
2839 if (*(int *) buffer)
2840 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2841 else
2842 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2843
Florin Coras5e062572019-03-14 19:07:51 -07002844 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
2845 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEADDR),
2846 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002847 }
2848 else
2849 rv = VPPCOM_EINVAL;
2850 break;
2851
2852 case VPPCOM_ATTR_GET_REUSEPORT:
2853 if (buffer && buflen && (*buflen >= sizeof (int)))
2854 {
2855 /* VPP-TBD */
2856 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2857 VCL_SESS_ATTR_REUSEPORT);
2858 *buflen = sizeof (int);
2859
Florin Coras5e062572019-03-14 19:07:51 -07002860 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
2861 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002862 }
2863 else
2864 rv = VPPCOM_EINVAL;
2865 break;
2866
2867 case VPPCOM_ATTR_SET_REUSEPORT:
2868 if (buffer && buflen && (*buflen == sizeof (int)) &&
2869 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2870 {
2871 /* VPP-TBD */
2872 if (*(int *) buffer)
2873 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2874 else
2875 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2876
Florin Coras5e062572019-03-14 19:07:51 -07002877 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
2878 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEPORT),
2879 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002880 }
2881 else
2882 rv = VPPCOM_EINVAL;
2883 break;
2884
2885 case VPPCOM_ATTR_GET_BROADCAST:
2886 if (buffer && buflen && (*buflen >= sizeof (int)))
2887 {
2888 /* VPP-TBD */
2889 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2890 VCL_SESS_ATTR_BROADCAST);
2891 *buflen = sizeof (int);
2892
Florin Coras5e062572019-03-14 19:07:51 -07002893 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
2894 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002895 }
2896 else
2897 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002898 break;
2899
2900 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05002901 if (buffer && buflen && (*buflen == sizeof (int)))
2902 {
2903 /* VPP-TBD */
2904 if (*(int *) buffer)
2905 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2906 else
2907 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2908
Florin Coras5e062572019-03-14 19:07:51 -07002909 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
2910 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_BROADCAST),
2911 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002912 }
2913 else
2914 rv = VPPCOM_EINVAL;
2915 break;
2916
2917 case VPPCOM_ATTR_GET_V6ONLY:
2918 if (buffer && buflen && (*buflen >= sizeof (int)))
2919 {
2920 /* VPP-TBD */
2921 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2922 VCL_SESS_ATTR_V6ONLY);
2923 *buflen = sizeof (int);
2924
Florin Coras5e062572019-03-14 19:07:51 -07002925 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
2926 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002927 }
2928 else
2929 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002930 break;
2931
2932 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05002933 if (buffer && buflen && (*buflen == sizeof (int)))
2934 {
2935 /* VPP-TBD */
2936 if (*(int *) buffer)
2937 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2938 else
2939 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2940
Florin Coras5e062572019-03-14 19:07:51 -07002941 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
2942 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_V6ONLY),
2943 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002944 }
2945 else
2946 rv = VPPCOM_EINVAL;
2947 break;
2948
2949 case VPPCOM_ATTR_GET_KEEPALIVE:
2950 if (buffer && buflen && (*buflen >= sizeof (int)))
2951 {
2952 /* VPP-TBD */
2953 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2954 VCL_SESS_ATTR_KEEPALIVE);
2955 *buflen = sizeof (int);
2956
Florin Coras5e062572019-03-14 19:07:51 -07002957 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
2958 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002959 }
2960 else
2961 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002962 break;
Stevenbccd3392017-10-12 20:42:21 -07002963
2964 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05002965 if (buffer && buflen && (*buflen == sizeof (int)))
2966 {
2967 /* VPP-TBD */
2968 if (*(int *) buffer)
2969 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2970 else
2971 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2972
Florin Coras5e062572019-03-14 19:07:51 -07002973 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
2974 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_KEEPALIVE),
2975 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002976 }
2977 else
2978 rv = VPPCOM_EINVAL;
2979 break;
2980
2981 case VPPCOM_ATTR_GET_TCP_NODELAY:
2982 if (buffer && buflen && (*buflen >= sizeof (int)))
2983 {
2984 /* VPP-TBD */
2985 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2986 VCL_SESS_ATTR_TCP_NODELAY);
2987 *buflen = sizeof (int);
2988
Florin Coras5e062572019-03-14 19:07:51 -07002989 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
2990 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002991 }
2992 else
2993 rv = VPPCOM_EINVAL;
2994 break;
2995
2996 case VPPCOM_ATTR_SET_TCP_NODELAY:
2997 if (buffer && buflen && (*buflen == sizeof (int)))
2998 {
2999 /* VPP-TBD */
3000 if (*(int *) buffer)
3001 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3002 else
3003 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3004
Florin Coras5e062572019-03-14 19:07:51 -07003005 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3006 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_TCP_NODELAY),
3007 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003008 }
3009 else
3010 rv = VPPCOM_EINVAL;
3011 break;
3012
3013 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3014 if (buffer && buflen && (*buflen >= sizeof (int)))
3015 {
3016 /* VPP-TBD */
3017 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3018 VCL_SESS_ATTR_TCP_KEEPIDLE);
3019 *buflen = sizeof (int);
3020
Florin Coras5e062572019-03-14 19:07:51 -07003021 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3022 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003023 }
3024 else
3025 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003026 break;
3027
3028 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003029 if (buffer && buflen && (*buflen == sizeof (int)))
3030 {
3031 /* VPP-TBD */
3032 if (*(int *) buffer)
3033 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3034 else
3035 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3036
Florin Coras5e062572019-03-14 19:07:51 -07003037 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003038 VCL_SESS_ATTR_TEST (session->attr,
3039 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003040 }
3041 else
3042 rv = VPPCOM_EINVAL;
3043 break;
3044
3045 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3046 if (buffer && buflen && (*buflen >= sizeof (int)))
3047 {
3048 /* VPP-TBD */
3049 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3050 VCL_SESS_ATTR_TCP_KEEPINTVL);
3051 *buflen = sizeof (int);
3052
Florin Coras5e062572019-03-14 19:07:51 -07003053 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3054 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003055 }
3056 else
3057 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003058 break;
3059
3060 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003061 if (buffer && buflen && (*buflen == sizeof (int)))
3062 {
3063 /* VPP-TBD */
3064 if (*(int *) buffer)
3065 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3066 else
3067 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3068
Florin Coras5e062572019-03-14 19:07:51 -07003069 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003070 VCL_SESS_ATTR_TEST (session->attr,
3071 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003072 }
3073 else
3074 rv = VPPCOM_EINVAL;
3075 break;
3076
3077 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3078 if (buffer && buflen && (*buflen >= sizeof (u32)))
3079 {
3080 /* VPP-TBD */
3081 *(u32 *) buffer = session->user_mss;
3082 *buflen = sizeof (int);
3083
Florin Coras5e062572019-03-14 19:07:51 -07003084 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3085 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003086 }
3087 else
3088 rv = VPPCOM_EINVAL;
3089 break;
3090
3091 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3092 if (buffer && buflen && (*buflen == sizeof (u32)))
3093 {
3094 /* VPP-TBD */
3095 session->user_mss = *(u32 *) buffer;
3096
Florin Coras5e062572019-03-14 19:07:51 -07003097 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3098 session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003099 }
3100 else
3101 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003102 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003103
Florin Coras7baeb712019-01-04 17:05:43 -08003104 case VPPCOM_ATTR_SET_SHUT:
3105 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3106 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3107 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3108 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3109 break;
3110
3111 case VPPCOM_ATTR_GET_SHUT:
3112 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3113 tmp_flags = 1;
3114 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3115 tmp_flags |= 2;
3116 if (tmp_flags == 1)
3117 *(int *) buffer = SHUT_RD;
3118 else if (tmp_flags == 2)
3119 *(int *) buffer = SHUT_WR;
3120 else if (tmp_flags == 3)
3121 *(int *) buffer = SHUT_RDWR;
3122 *buflen = sizeof (int);
3123 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003124 default:
3125 rv = VPPCOM_EINVAL;
3126 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003127 }
3128
Dave Wallace35830af2017-10-09 01:43:42 -04003129 return rv;
3130}
3131
Stevenac1f96d2017-10-24 16:03:58 -07003132int
Florin Coras134a9962018-08-28 11:32:04 -07003133vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003134 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3135{
Florin Coras134a9962018-08-28 11:32:04 -07003136 vcl_worker_t *wrk = vcl_worker_get_current ();
Stevenac1f96d2017-10-24 16:03:58 -07003137 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07003138 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07003139
3140 if (ep)
3141 {
Florin Coras134a9962018-08-28 11:32:04 -07003142 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003143 if (PREDICT_FALSE (!session))
Stevenac1f96d2017-10-24 16:03:58 -07003144 {
Florin Coras5e062572019-03-14 19:07:51 -07003145 VDBG (0, "sh 0x%llx is closed!", session_handle);
Florin Coras460dce62018-07-27 05:45:06 -07003146 return VPPCOM_EBADFD;
Stevenac1f96d2017-10-24 16:03:58 -07003147 }
Florin Coras7e12d942018-06-27 14:32:43 -07003148 ep->is_ip4 = session->transport.is_ip4;
3149 ep->port = session->transport.rmt_port;
Stevenac1f96d2017-10-24 16:03:58 -07003150 }
Steven58f464e2017-10-25 12:33:12 -07003151
3152 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003153 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003154 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003155 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003156 else
3157 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003158 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003159 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003160 }
3161
Florin Coras99368312018-08-02 10:45:44 -07003162 if (ep)
3163 {
3164 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003165 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3166 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003167 else
Dave Barach178cf492018-11-13 16:34:13 -05003168 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3169 sizeof (ip6_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003170 }
Florin Coras460dce62018-07-27 05:45:06 -07003171
Stevenac1f96d2017-10-24 16:03:58 -07003172 return rv;
3173}
3174
3175int
Florin Coras134a9962018-08-28 11:32:04 -07003176vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003177 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3178{
Dave Wallace617dffa2017-10-26 14:47:06 -04003179 if (!buffer)
3180 return VPPCOM_EINVAL;
3181
3182 if (ep)
3183 {
3184 // TBD
3185 return VPPCOM_EINVAL;
3186 }
3187
3188 if (flags)
3189 {
3190 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003191 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003192 }
3193
Florin Coras42ceddb2018-12-12 10:56:01 -08003194 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
Stevenac1f96d2017-10-24 16:03:58 -07003195}
3196
Dave Wallace048b1d62018-01-03 22:24:41 -05003197int
3198vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3199{
Florin Coras134a9962018-08-28 11:32:04 -07003200 vcl_worker_t *wrk = vcl_worker_get_current ();
3201 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003202 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003203 svm_msg_q_msg_t msg;
3204 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003205 int rv, num_ev = 0;
3206
Florin Coras5e062572019-03-14 19:07:51 -07003207 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003208
3209 if (!vp)
3210 return VPPCOM_EFAULT;
3211
3212 do
3213 {
Florin Coras7e12d942018-06-27 14:32:43 -07003214 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003215
Florin Coras6917b942018-11-13 22:44:54 -08003216 /* Dequeue all events and drop all unhandled io events */
3217 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3218 {
3219 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3220 vcl_handle_mq_event (wrk, e);
3221 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3222 }
3223 vec_reset_length (wrk->unhandled_evts_vector);
3224
Dave Wallace048b1d62018-01-03 22:24:41 -05003225 for (i = 0; i < n_sids; i++)
3226 {
Florin Coras7baeb712019-01-04 17:05:43 -08003227 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003228 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003229 {
3230 vp[i].revents = POLLHUP;
3231 num_ev++;
3232 continue;
3233 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003234
Florin Coras6917b942018-11-13 22:44:54 -08003235 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003236
3237 if (POLLIN & vp[i].events)
3238 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003239 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003240 if (rv > 0)
3241 {
Florin Coras6917b942018-11-13 22:44:54 -08003242 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003243 num_ev++;
3244 }
3245 else if (rv < 0)
3246 {
3247 switch (rv)
3248 {
3249 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003250 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003251 break;
3252
3253 default:
Florin Coras6917b942018-11-13 22:44:54 -08003254 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003255 break;
3256 }
3257 num_ev++;
3258 }
3259 }
3260
3261 if (POLLOUT & vp[i].events)
3262 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003263 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003264 if (rv > 0)
3265 {
Florin Coras6917b942018-11-13 22:44:54 -08003266 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003267 num_ev++;
3268 }
3269 else if (rv < 0)
3270 {
3271 switch (rv)
3272 {
3273 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003274 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003275 break;
3276
3277 default:
Florin Coras6917b942018-11-13 22:44:54 -08003278 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003279 break;
3280 }
3281 num_ev++;
3282 }
3283 }
3284
Dave Wallace7e607a72018-06-18 18:41:32 -04003285 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003286 {
Florin Coras6917b942018-11-13 22:44:54 -08003287 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003288 num_ev++;
3289 }
3290 }
3291 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003292 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003293 }
3294 while ((num_ev == 0) && keep_trying);
3295
Dave Wallace048b1d62018-01-03 22:24:41 -05003296 return num_ev;
3297}
3298
Florin Coras99368312018-08-02 10:45:44 -07003299int
3300vppcom_mq_epoll_fd (void)
3301{
Florin Coras134a9962018-08-28 11:32:04 -07003302 vcl_worker_t *wrk = vcl_worker_get_current ();
3303 return wrk->mqs_epfd;
3304}
3305
3306int
Florin Coras30e79c22019-01-02 19:31:22 -08003307vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003308{
3309 return session_handle & 0xFFFFFF;
3310}
3311
3312int
Florin Coras30e79c22019-01-02 19:31:22 -08003313vppcom_session_worker (vcl_session_handle_t session_handle)
3314{
3315 return session_handle >> 24;
3316}
3317
3318int
Florin Coras134a9962018-08-28 11:32:04 -07003319vppcom_worker_register (void)
3320{
Florin Coras47c40e22018-11-26 17:01:36 -08003321 if (!vcl_worker_alloc_and_init ())
3322 return VPPCOM_EEXIST;
3323
3324 if (vcl_worker_set_bapi ())
3325 return VPPCOM_EEXIST;
3326
3327 if (vcl_worker_register_with_vpp ())
3328 return VPPCOM_EEXIST;
3329
3330 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003331}
3332
Florin Corasdfe4cf42018-11-28 22:13:45 -08003333int
3334vppcom_worker_index (void)
3335{
3336 return vcl_get_worker_index ();
3337}
3338
Florin Corase0982e52019-01-25 13:19:56 -08003339int
3340vppcom_worker_mqs_epfd (void)
3341{
3342 vcl_worker_t *wrk = vcl_worker_get_current ();
3343 if (!vcm->cfg.use_mq_eventfd)
3344 return -1;
3345 return wrk->mqs_epfd;
3346}
3347
Dave Wallacee22aa742017-10-20 12:30:38 -04003348/*
3349 * fd.io coding-style-patch-verification: ON
3350 *
3351 * Local Variables:
3352 * eval: (c-set-style "gnu")
3353 * End:
3354 */