blob: b5953872f816fbabd9b6db75617cf472526e0d27 [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
Sirshak Das28aa5392019-02-05 01:33:33 -06001499 if (svm_fifo_is_empty_cons (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 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001506 while (svm_fifo_is_empty_cons (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
Sirshak Das28aa5392019-02-05 01:33:33 -06001530 if (svm_fifo_is_empty_cons (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
Sirshak Das28aa5392019-02-05 01:33:33 -06001580 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001581 {
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 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001587 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001588 {
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);
Sirshak Das28aa5392019-02-05 01:33:33 -06001684
Florin Coras653e43f2019-03-04 10:56:23 -08001685 mq = wrk->app_event_queue;
Sirshak Das28aa5392019-02-05 01:33:33 -06001686 if (svm_fifo_is_full_prod (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04001687 {
Florin Coras54693d22018-07-17 10:46:29 -07001688 if (is_nonblocking)
1689 {
Florin Coras070453d2018-08-24 17:04:27 -07001690 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07001691 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001692 while (svm_fifo_is_full_prod (tx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001693 {
Florin Coras1bcad5c2019-01-09 20:04:38 -08001694 svm_fifo_add_want_tx_ntf (tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08001695 if (vcl_session_is_closing (s))
1696 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07001697 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07001698 if (svm_msg_q_is_empty (mq))
1699 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07001700
Florin Coras54693d22018-07-17 10:46:29 -07001701 svm_msg_q_sub_w_lock (mq, &msg);
1702 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001703 svm_msg_q_unlock (mq);
1704
Florin Coras0e88e852018-09-17 22:09:02 -07001705 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07001706 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001707 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001708 }
Dave Wallace543852a2017-08-03 02:11:34 -04001709 }
Dave Wallace543852a2017-08-03 02:11:34 -04001710
Florin Coras653e43f2019-03-04 10:56:23 -08001711 et = SESSION_IO_EVT_TX;
1712 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08001713 et = SESSION_IO_EVT_TX_FLUSH;
1714
Florin Coras460dce62018-07-27 05:45:06 -07001715 if (s->is_dgram)
1716 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08001717 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08001718 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07001719 else
1720 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08001721 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08001722
Florin Coras14ed6df2019-03-06 21:13:42 -08001723 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08001724 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
1725 et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001726
Florin Coras460dce62018-07-27 05:45:06 -07001727 ASSERT (n_write > 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001728
Florin Coras6d0106e2019-01-29 20:11:58 -08001729 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
1730 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07001731
Florin Coras54693d22018-07-17 10:46:29 -07001732 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04001733}
1734
Florin Coras42ceddb2018-12-12 10:56:01 -08001735int
1736vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1737{
1738 return vppcom_session_write_inline (session_handle, buf, n,
1739 0 /* is_flush */ );
1740}
1741
Florin Corasb0f662f2018-12-27 14:51:46 -08001742int
1743vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
1744{
1745 return vppcom_session_write_inline (session_handle, buf, n,
1746 1 /* is_flush */ );
1747}
1748
Florin Corasc0737e92019-03-04 14:19:39 -08001749#define vcl_fifo_rx_evt_valid_or_break(_s) \
1750if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
1751 { \
1752 if (!vcl_session_is_ct (_s)) \
1753 { \
1754 svm_fifo_unset_event (_s->rx_fifo); \
1755 if (svm_fifo_is_empty (_s->rx_fifo)) \
1756 break; \
1757 } \
1758 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1759 { \
1760 svm_fifo_unset_event (_s->ct_rx_fifo); \
1761 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1762 break; \
1763 } \
1764 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07001765
Florin Coras86f04502018-09-12 16:08:01 -07001766static void
1767vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1768 unsigned long n_bits, unsigned long *read_map,
1769 unsigned long *write_map,
1770 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07001771{
1772 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07001773 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07001774 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07001775 u32 sid;
1776
1777 switch (e->event_type)
1778 {
Florin Corasc0737e92019-03-04 14:19:39 -08001779 case SESSION_IO_EVT_RX:
1780 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001781 session = vcl_session_get (wrk, sid);
1782 if (!session)
1783 break;
Florin Corasfff68f72019-03-13 16:01:38 -07001784 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07001785 if (sid < n_bits && read_map)
1786 {
David Johnsond9818dd2018-12-14 14:53:41 -05001787 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001788 *bits_set += 1;
1789 }
1790 break;
Florin Corasfe97da32019-03-06 10:09:04 -08001791 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08001792 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001793 session = vcl_session_get (wrk, sid);
1794 if (!session)
1795 break;
1796 if (sid < n_bits && write_map)
1797 {
David Johnsond9818dd2018-12-14 14:53:41 -05001798 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001799 *bits_set += 1;
1800 }
1801 break;
Florin Coras86f04502018-09-12 16:08:01 -07001802 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08001803 session = vcl_session_accepted (wrk,
1804 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07001805 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08001806 break;
Florin Coras86f04502018-09-12 16:08:01 -07001807 sid = session->session_index;
1808 if (sid < n_bits && read_map)
1809 {
David Johnsond9818dd2018-12-14 14:53:41 -05001810 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001811 *bits_set += 1;
1812 }
1813 break;
1814 case SESSION_CTRL_EVT_CONNECTED:
1815 connected_msg = (session_connected_msg_t *) e->data;
1816 vcl_session_connected_handler (wrk, connected_msg);
1817 break;
1818 case SESSION_CTRL_EVT_DISCONNECTED:
1819 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001820 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
1821 if (!session)
1822 break;
1823 sid = session->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001824 if (sid < n_bits && except_map)
1825 {
David Johnsond9818dd2018-12-14 14:53:41 -05001826 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001827 *bits_set += 1;
1828 }
1829 break;
1830 case SESSION_CTRL_EVT_RESET:
1831 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1832 if (sid < n_bits && except_map)
1833 {
David Johnsond9818dd2018-12-14 14:53:41 -05001834 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001835 *bits_set += 1;
1836 }
1837 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001838 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1839 vcl_session_unlisten_reply_handler (wrk, e->data);
1840 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001841 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1842 vcl_session_worker_update_reply_handler (wrk, e->data);
1843 break;
1844 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1845 vcl_session_req_worker_update_handler (wrk, e->data);
1846 break;
Florin Coras86f04502018-09-12 16:08:01 -07001847 default:
1848 clib_warning ("unhandled: %u", e->event_type);
1849 break;
1850 }
1851}
1852
1853static int
1854vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1855 unsigned long n_bits, unsigned long *read_map,
1856 unsigned long *write_map, unsigned long *except_map,
1857 double time_to_wait, u32 * bits_set)
1858{
Florin Coras99368312018-08-02 10:45:44 -07001859 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07001860 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07001861 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07001862
1863 svm_msg_q_lock (mq);
1864 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001865 {
Florin Coras54693d22018-07-17 10:46:29 -07001866 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001867 {
Florin Coras54693d22018-07-17 10:46:29 -07001868 svm_msg_q_unlock (mq);
1869 return 0;
1870 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001871
Florin Coras54693d22018-07-17 10:46:29 -07001872 if (!time_to_wait)
1873 {
1874 svm_msg_q_unlock (mq);
1875 return 0;
1876 }
1877 else if (time_to_wait < 0)
1878 {
1879 svm_msg_q_wait (mq);
1880 }
1881 else
1882 {
1883 if (svm_msg_q_timedwait (mq, time_to_wait))
1884 {
1885 svm_msg_q_unlock (mq);
1886 return 0;
1887 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001888 }
1889 }
Florin Coras134a9962018-08-28 11:32:04 -07001890 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07001891 svm_msg_q_unlock (mq);
1892
Florin Coras134a9962018-08-28 11:32:04 -07001893 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07001894 {
Florin Coras134a9962018-08-28 11:32:04 -07001895 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07001896 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07001897 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
1898 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07001899 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07001900 }
Florin Coras134a9962018-08-28 11:32:04 -07001901 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08001902 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07001903 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04001904}
1905
Florin Coras99368312018-08-02 10:45:44 -07001906static int
Florin Coras294afe22019-01-07 17:49:17 -08001907vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
1908 vcl_si_set * read_map, vcl_si_set * write_map,
1909 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07001910 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07001911{
Florin Coras14ed6df2019-03-06 21:13:42 -08001912 double wait = 0, start = 0;
1913
1914 if (!*bits_set)
1915 {
1916 wait = time_to_wait;
1917 start = clib_time_now (&wrk->clib_time);
1918 }
1919
1920 do
1921 {
1922 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
1923 write_map, except_map, wait, bits_set);
1924 if (*bits_set)
1925 return *bits_set;
1926 if (wait == -1)
1927 continue;
1928
1929 wait = wait - (clib_time_now (&wrk->clib_time) - start);
1930 }
1931 while (wait > 0);
1932
1933 return 0;
Florin Coras99368312018-08-02 10:45:44 -07001934}
1935
1936static int
Florin Coras294afe22019-01-07 17:49:17 -08001937vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
1938 vcl_si_set * read_map, vcl_si_set * write_map,
1939 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07001940 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07001941{
1942 vcl_mq_evt_conn_t *mqc;
1943 int __clib_unused n_read;
1944 int n_mq_evts, i;
1945 u64 buf;
1946
Florin Coras134a9962018-08-28 11:32:04 -07001947 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
1948 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
1949 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07001950 for (i = 0; i < n_mq_evts; i++)
1951 {
Florin Coras134a9962018-08-28 11:32:04 -07001952 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07001953 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07001954 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07001955 except_map, 0, bits_set);
1956 }
1957
1958 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1959}
1960
Dave Wallace543852a2017-08-03 02:11:34 -04001961int
Florin Coras294afe22019-01-07 17:49:17 -08001962vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
1963 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04001964{
Florin Coras54693d22018-07-17 10:46:29 -07001965 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001966 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001967 vcl_session_t *session = 0;
Florin Coras86f04502018-09-12 16:08:01 -07001968 int rv, i;
Dave Wallace543852a2017-08-03 02:11:34 -04001969
Dave Wallace7876d392017-10-19 03:53:57 -04001970 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001971 {
Florin Coras134a9962018-08-28 11:32:04 -07001972 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05001973 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08001974 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
1975 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04001976 }
Dave Wallace7876d392017-10-19 03:53:57 -04001977 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001978 {
Florin Coras134a9962018-08-28 11:32:04 -07001979 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05001980 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08001981 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
1982 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04001983 }
Dave Wallace7876d392017-10-19 03:53:57 -04001984 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001985 {
Florin Coras134a9962018-08-28 11:32:04 -07001986 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05001987 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08001988 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
1989 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04001990 }
1991
Florin Coras54693d22018-07-17 10:46:29 -07001992 if (!n_bits)
1993 return 0;
1994
1995 if (!write_map)
1996 goto check_rd;
1997
1998 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07001999 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2000 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002001 {
Florin Coras47c40e22018-11-26 17:01:36 -08002002 if (except_map && sid < minbits)
2003 clib_bitmap_set_no_check (except_map, sid, 1);
2004 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002005 }
2006
Sirshak Das28aa5392019-02-05 01:33:33 -06002007 rv = svm_fifo_is_full_prod (session->tx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07002008 if (!rv)
2009 {
David Johnsond9818dd2018-12-14 14:53:41 -05002010 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002011 bits_set++;
2012 }
Florin Coras294afe22019-01-07 17:49:17 -08002013 else
Florin Coras1bcad5c2019-01-09 20:04:38 -08002014 svm_fifo_add_want_tx_ntf (session->tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
Florin Coras54693d22018-07-17 10:46:29 -07002015 }));
2016
2017check_rd:
2018 if (!read_map)
2019 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002020
Florin Coras134a9962018-08-28 11:32:04 -07002021 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2022 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002023 {
Florin Coras47c40e22018-11-26 17:01:36 -08002024 if (except_map && sid < minbits)
2025 clib_bitmap_set_no_check (except_map, sid, 1);
2026 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002027 }
2028
Florin Coras0ef8ef22019-01-18 08:37:13 -08002029 rv = vcl_session_read_ready (session);
Florin Coras54693d22018-07-17 10:46:29 -07002030 if (rv)
2031 {
David Johnsond9818dd2018-12-14 14:53:41 -05002032 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002033 bits_set++;
2034 }
2035 }));
2036 /* *INDENT-ON* */
2037
2038check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002039
Florin Coras86f04502018-09-12 16:08:01 -07002040 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2041 {
2042 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2043 read_map, write_map, except_map, &bits_set);
2044 }
2045 vec_reset_length (wrk->unhandled_evts_vector);
2046
Florin Coras99368312018-08-02 10:45:44 -07002047 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002048 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002049 time_to_wait, &bits_set);
2050 else
Florin Coras134a9962018-08-28 11:32:04 -07002051 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002052 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002053
Dave Wallace543852a2017-08-03 02:11:34 -04002054 return (bits_set);
2055}
2056
Dave Wallacef7f809c2017-10-03 01:48:42 -04002057static inline void
Florin Coras134a9962018-08-28 11:32:04 -07002058vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002059{
Florin Coras7e12d942018-06-27 14:32:43 -07002060 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002061 vppcom_epoll_t *vep;
Dave Wallace498b3a52017-11-09 13:00:34 -05002062 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002063
Florin Corasc0737e92019-03-04 14:19:39 -08002064 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002065 return;
2066
Florin Coras134a9962018-08-28 11:32:04 -07002067 session = vcl_session_get (wrk, vep_idx);
Florin Coras070453d2018-08-24 17:04:27 -07002068 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002069 {
Florin Coras5e062572019-03-14 19:07:51 -07002070 VDBG (0, "ERROR: Invalid vep_idx (%u)!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002071 goto done;
2072 }
2073 if (PREDICT_FALSE (!session->is_vep))
2074 {
Florin Coras5e062572019-03-14 19:07:51 -07002075 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002076 goto done;
2077 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002078 vep = &session->vep;
Florin Coras5e062572019-03-14 19:07:51 -07002079 VDBG (0, "vep_idx (%u): Dumping epoll chain\n"
2080 "{\n"
2081 " is_vep = %u\n"
2082 " is_vep_session = %u\n"
2083 " next_sid = 0x%x (%u)\n"
2084 "}\n", vep_idx, session->is_vep, session->is_vep_session,
2085 vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002086
Florin Coras134a9962018-08-28 11:32:04 -07002087 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002088 {
Florin Coras134a9962018-08-28 11:32:04 -07002089 session = vcl_session_get (wrk, sid);
Florin Coras070453d2018-08-24 17:04:27 -07002090 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002091 {
Florin Coras5e062572019-03-14 19:07:51 -07002092 VDBG (0, "ERROR: Invalid sid (%u)!", sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002093 goto done;
2094 }
2095 if (PREDICT_FALSE (session->is_vep))
Florin Coras5e062572019-03-14 19:07:51 -07002096 {
2097 VDBG (0, "ERROR: sid (%u) is a vep!", vep_idx);
2098 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002099 else if (PREDICT_FALSE (!session->is_vep_session))
2100 {
Florin Coras5e062572019-03-14 19:07:51 -07002101 VDBG (0, "ERROR: session (%u) is not a vep session!", sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002102 goto done;
2103 }
2104 vep = &session->vep;
Florin Coras134a9962018-08-28 11:32:04 -07002105 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
Florin Coras5e062572019-03-14 19:07:51 -07002106 VDBG (0, "ERROR: session (%u) vep_idx (%u) != vep_idx (%u)!",
2107 sid, session->vep.vep_sh, vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002108 if (session->is_vep_session)
2109 {
Florin Coras5e062572019-03-14 19:07:51 -07002110 VDBG (0, "vep_idx[%u]: sid 0x%x (%u)\n"
2111 "{\n"
2112 " next_sid = 0x%x (%u)\n"
2113 " prev_sid = 0x%x (%u)\n"
2114 " vep_idx = 0x%x (%u)\n"
2115 " ev.events = 0x%x\n"
2116 " ev.data.u64 = 0x%llx\n"
2117 " et_mask = 0x%x\n"
2118 "}\n",
2119 vep_idx, sid, sid, vep->next_sh, vep->next_sh, vep->prev_sh,
2120 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2121 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002122 }
2123 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002124
2125done:
Florin Coras5e062572019-03-14 19:07:51 -07002126 VDBG (0, "vep_idx (%u): Dump complete!\n", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002127}
2128
2129int
2130vppcom_epoll_create (void)
2131{
Florin Coras134a9962018-08-28 11:32:04 -07002132 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002133 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002134
Florin Coras134a9962018-08-28 11:32:04 -07002135 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002136
2137 vep_session->is_vep = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002138 vep_session->vep.vep_sh = ~0;
2139 vep_session->vep.next_sh = ~0;
2140 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002141 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002142
Florin Corasa7a1a222018-12-30 17:11:31 -08002143 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2144 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002145
Florin Corasab2f6db2018-08-31 14:31:41 -07002146 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002147}
2148
2149int
Florin Coras134a9962018-08-28 11:32:04 -07002150vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002151 struct epoll_event *event)
2152{
Florin Coras134a9962018-08-28 11:32:04 -07002153 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002154 vcl_session_t *vep_session;
2155 vcl_session_t *session;
Florin Coras070453d2018-08-24 17:04:27 -07002156 int rv = VPPCOM_OK;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002157
Florin Coras134a9962018-08-28 11:32:04 -07002158 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002159 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002160 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002161 return VPPCOM_EINVAL;
2162 }
2163
Florin Coras134a9962018-08-28 11:32:04 -07002164 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002165 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002166 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002167 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002168 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002169 }
2170 if (PREDICT_FALSE (!vep_session->is_vep))
2171 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002172 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002173 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002174 }
2175
Florin Coras134a9962018-08-28 11:32:04 -07002176 ASSERT (vep_session->vep.vep_sh == ~0);
2177 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002178
Florin Coras134a9962018-08-28 11:32:04 -07002179 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002180 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002181 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002182 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002183 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002184 }
2185 if (PREDICT_FALSE (session->is_vep))
2186 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002187 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002188 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002189 }
2190
2191 switch (op)
2192 {
2193 case EPOLL_CTL_ADD:
2194 if (PREDICT_FALSE (!event))
2195 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002196 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002197 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002198 }
Florin Coras134a9962018-08-28 11:32:04 -07002199 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002200 {
Florin Coras7e12d942018-06-27 14:32:43 -07002201 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002202 next_session = vcl_session_get_w_handle (wrk,
2203 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002204 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002205 {
Florin Coras5e062572019-03-14 19:07:51 -07002206 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002207 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002208 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002209 }
Florin Coras134a9962018-08-28 11:32:04 -07002210 ASSERT (next_session->vep.prev_sh == vep_handle);
2211 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002212 }
Florin Coras134a9962018-08-28 11:32:04 -07002213 session->vep.next_sh = vep_session->vep.next_sh;
2214 session->vep.prev_sh = vep_handle;
2215 session->vep.vep_sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002216 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2217 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002218 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002219 session->is_vep_session = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002220 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002221
Florin Coras1bcad5c2019-01-09 20:04:38 -08002222 if (session->tx_fifo)
2223 svm_fifo_add_want_tx_ntf (session->tx_fifo,
2224 SVM_FIFO_WANT_TX_NOTIF_IF_FULL);
2225
Florin Corasa7a1a222018-12-30 17:11:31 -08002226 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2227 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras0d427d82018-06-27 03:24:07 -07002228 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002229 break;
2230
2231 case EPOLL_CTL_MOD:
2232 if (PREDICT_FALSE (!event))
2233 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002234 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002235 rv = VPPCOM_EINVAL;
2236 goto done;
2237 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002238 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002239 {
Florin Coras5e062572019-03-14 19:07:51 -07002240 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002241 rv = VPPCOM_EINVAL;
2242 goto done;
2243 }
Florin Coras134a9962018-08-28 11:32:04 -07002244 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002245 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002246 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2247 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002248 rv = VPPCOM_EINVAL;
2249 goto done;
2250 }
2251 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2252 session->vep.ev = *event;
Florin Corasa7a1a222018-12-30 17:11:31 -08002253 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2254 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002255 break;
2256
2257 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05002258 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002259 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002260 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002261 rv = VPPCOM_EINVAL;
2262 goto done;
2263 }
Florin Coras134a9962018-08-28 11:32:04 -07002264 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002265 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002266 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2267 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002268 rv = VPPCOM_EINVAL;
2269 goto done;
2270 }
2271
Florin Coras134a9962018-08-28 11:32:04 -07002272 if (session->vep.prev_sh == vep_handle)
2273 vep_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002274 else
2275 {
Florin Coras7e12d942018-06-27 14:32:43 -07002276 vcl_session_t *prev_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002277 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002278 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002279 {
Florin Coras5e062572019-03-14 19:07:51 -07002280 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Corasa7a1a222018-12-30 17:11:31 -08002281 session->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002282 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002283 }
Florin Coras134a9962018-08-28 11:32:04 -07002284 ASSERT (prev_session->vep.next_sh == session_handle);
2285 prev_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002286 }
Florin Coras134a9962018-08-28 11:32:04 -07002287 if (session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002288 {
Florin Coras7e12d942018-06-27 14:32:43 -07002289 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002290 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002291 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002292 {
Florin Coras5e062572019-03-14 19:07:51 -07002293 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Corasa7a1a222018-12-30 17:11:31 -08002294 session->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002295 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002296 }
Florin Coras134a9962018-08-28 11:32:04 -07002297 ASSERT (next_session->vep.prev_sh == session_handle);
2298 next_session->vep.prev_sh = session->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002299 }
2300
2301 memset (&session->vep, 0, sizeof (session->vep));
Florin Coras134a9962018-08-28 11:32:04 -07002302 session->vep.next_sh = ~0;
2303 session->vep.prev_sh = ~0;
2304 session->vep.vep_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002305 session->is_vep_session = 0;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002306
2307 if (session->tx_fifo)
2308 svm_fifo_del_want_tx_ntf (session->tx_fifo, SVM_FIFO_NO_TX_NOTIF);
2309
Florin Coras5e062572019-03-14 19:07:51 -07002310 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002311 session_handle);
Florin Coras134a9962018-08-28 11:32:04 -07002312 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002313 break;
2314
2315 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002316 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002317 rv = VPPCOM_EINVAL;
2318 }
2319
Florin Coras134a9962018-08-28 11:32:04 -07002320 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002321
2322done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002323 return rv;
2324}
2325
Florin Coras86f04502018-09-12 16:08:01 -07002326static inline void
2327vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2328 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002329{
2330 session_disconnected_msg_t *disconnected_msg;
2331 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002332 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002333 u64 session_evt_data = ~0;
Florin Coras54693d22018-07-17 10:46:29 -07002334 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002335 u8 add_event = 0;
2336
2337 switch (e->event_type)
2338 {
Florin Coras653e43f2019-03-04 10:56:23 -08002339 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002340 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002341 if (!(session = vcl_session_get (wrk, sid)))
2342 break;
Florin Corasc0737e92019-03-04 14:19:39 -08002343 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07002344 session_events = session->vep.ev.events;
Florin Corasaa27eb92018-10-13 12:20:01 -07002345 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
Florin Coras86f04502018-09-12 16:08:01 -07002346 break;
2347 add_event = 1;
2348 events[*num_ev].events |= EPOLLIN;
2349 session_evt_data = session->vep.ev.data.u64;
Florin Corasaa27eb92018-10-13 12:20:01 -07002350 session->has_rx_evt = 1;
Florin Coras86f04502018-09-12 16:08:01 -07002351 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002352 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002353 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002354 if (!(session = vcl_session_get (wrk, sid)))
2355 break;
Florin Coras86f04502018-09-12 16:08:01 -07002356 session_events = session->vep.ev.events;
2357 if (!(EPOLLOUT & session_events))
2358 break;
2359 add_event = 1;
2360 events[*num_ev].events |= EPOLLOUT;
2361 session_evt_data = session->vep.ev.data.u64;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002362 svm_fifo_reset_tx_ntf (session->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002363 break;
Florin Coras86f04502018-09-12 16:08:01 -07002364 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002365 session = vcl_session_accepted (wrk,
2366 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002367 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002368 break;
Florin Coras86f04502018-09-12 16:08:01 -07002369
Florin Coras86f04502018-09-12 16:08:01 -07002370 session_events = session->vep.ev.events;
2371 if (!(EPOLLIN & session_events))
2372 break;
2373
2374 add_event = 1;
2375 events[*num_ev].events |= EPOLLIN;
2376 session_evt_data = session->vep.ev.data.u64;
2377 break;
2378 case SESSION_CTRL_EVT_CONNECTED:
2379 connected_msg = (session_connected_msg_t *) e->data;
2380 vcl_session_connected_handler (wrk, connected_msg);
2381 /* Generate EPOLLOUT because there's no connected event */
2382 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
Florin Corasfa915f82018-12-26 16:29:06 -08002383 if (!(session = vcl_session_get (wrk, sid)))
2384 break;
Florin Coras86f04502018-09-12 16:08:01 -07002385 session_events = session->vep.ev.events;
Florin Coras72f77822019-01-22 19:05:52 -08002386 if (!(EPOLLOUT & session_events))
2387 break;
2388 add_event = 1;
2389 events[*num_ev].events |= EPOLLOUT;
2390 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002391 break;
2392 case SESSION_CTRL_EVT_DISCONNECTED:
2393 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002394 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2395 if (!session)
Florin Coras86f04502018-09-12 16:08:01 -07002396 break;
Florin Coras72f77822019-01-22 19:05:52 -08002397 session_events = session->vep.ev.events;
2398 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2399 break;
Florin Coras86f04502018-09-12 16:08:01 -07002400 add_event = 1;
2401 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2402 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002403 break;
2404 case SESSION_CTRL_EVT_RESET:
2405 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2406 if (!(session = vcl_session_get (wrk, sid)))
2407 break;
Florin Coras72f77822019-01-22 19:05:52 -08002408 session_events = session->vep.ev.events;
2409 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2410 break;
Florin Coras86f04502018-09-12 16:08:01 -07002411 add_event = 1;
2412 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2413 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002414 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002415 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2416 vcl_session_unlisten_reply_handler (wrk, e->data);
2417 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002418 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2419 vcl_session_req_worker_update_handler (wrk, e->data);
2420 break;
2421 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2422 vcl_session_worker_update_reply_handler (wrk, e->data);
2423 break;
Florin Coras86f04502018-09-12 16:08:01 -07002424 default:
2425 VDBG (0, "unhandled: %u", e->event_type);
2426 break;
2427 }
2428
2429 if (add_event)
2430 {
2431 events[*num_ev].data.u64 = session_evt_data;
2432 if (EPOLLONESHOT & session_events)
2433 {
2434 session = vcl_session_get (wrk, sid);
2435 session->vep.ev.events = 0;
2436 }
2437 *num_ev += 1;
2438 }
2439}
2440
2441static int
2442vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2443 struct epoll_event *events, u32 maxevents,
2444 double wait_for_time, u32 * num_ev)
2445{
Florin Coras99368312018-08-02 10:45:44 -07002446 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002447 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002448 int i;
2449
Florin Coras539663c2018-09-28 14:59:37 -07002450 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2451 goto handle_dequeued;
2452
Florin Coras54693d22018-07-17 10:46:29 -07002453 svm_msg_q_lock (mq);
2454 if (svm_msg_q_is_empty (mq))
2455 {
2456 if (!wait_for_time)
2457 {
2458 svm_msg_q_unlock (mq);
2459 return 0;
2460 }
2461 else if (wait_for_time < 0)
2462 {
2463 svm_msg_q_wait (mq);
2464 }
2465 else
2466 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002467 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002468 {
2469 svm_msg_q_unlock (mq);
2470 return 0;
2471 }
2472 }
2473 }
Florin Coras134a9962018-08-28 11:32:04 -07002474 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07002475 svm_msg_q_unlock (mq);
2476
Florin Coras539663c2018-09-28 14:59:37 -07002477handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002478 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002479 {
Florin Coras134a9962018-08-28 11:32:04 -07002480 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002481 e = svm_msg_q_msg_data (mq, msg);
Florin Corasaa27eb92018-10-13 12:20:01 -07002482 if (*num_ev < maxevents)
2483 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2484 else
2485 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras99368312018-08-02 10:45:44 -07002486 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002487 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002488 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002489 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002490 return *num_ev;
2491}
2492
Florin Coras99368312018-08-02 10:45:44 -07002493static int
Florin Coras134a9962018-08-28 11:32:04 -07002494vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002495 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002496{
Florin Coras14ed6df2019-03-06 21:13:42 -08002497 double wait = 0, start = 0;
2498
2499 if (!n_evts)
2500 {
2501 wait = wait_for_time;
2502 start = clib_time_now (&wrk->clib_time);
2503 }
2504
2505 do
2506 {
2507 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2508 wait, &n_evts);
2509 if (n_evts)
2510 return n_evts;
2511 if (wait == -1)
2512 continue;
2513
2514 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2515 }
2516 while (wait > 0);
2517
2518 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002519}
2520
2521static int
Florin Coras134a9962018-08-28 11:32:04 -07002522vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002523 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002524{
2525 vcl_mq_evt_conn_t *mqc;
2526 int __clib_unused n_read;
2527 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07002528 u64 buf;
2529
Florin Coras134a9962018-08-28 11:32:04 -07002530 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07002531again:
Florin Coras134a9962018-08-28 11:32:04 -07002532 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2533 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07002534 for (i = 0; i < n_mq_evts; i++)
2535 {
Florin Coras134a9962018-08-28 11:32:04 -07002536 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002537 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002538 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002539 }
Florin Corasa4878ed2018-10-12 13:09:36 -07002540 if (!n_evts && n_mq_evts > 0)
2541 goto again;
Florin Coras99368312018-08-02 10:45:44 -07002542
2543 return (int) n_evts;
2544}
2545
Dave Wallacef7f809c2017-10-03 01:48:42 -04002546int
Florin Coras134a9962018-08-28 11:32:04 -07002547vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002548 int maxevents, double wait_for_time)
2549{
Florin Coras134a9962018-08-28 11:32:04 -07002550 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002551 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07002552 u32 n_evts = 0;
2553 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002554
2555 if (PREDICT_FALSE (maxevents <= 0))
2556 {
Florin Coras5e062572019-03-14 19:07:51 -07002557 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002558 return VPPCOM_EINVAL;
2559 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002560
Florin Coras134a9962018-08-28 11:32:04 -07002561 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07002562 if (!vep_session)
2563 return VPPCOM_EBADFD;
2564
Florin Coras54693d22018-07-17 10:46:29 -07002565 if (PREDICT_FALSE (!vep_session->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002566 {
Florin Coras5e062572019-03-14 19:07:51 -07002567 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07002568 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002569 }
Florin Coras54693d22018-07-17 10:46:29 -07002570
2571 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002572
Florin Coras86f04502018-09-12 16:08:01 -07002573 if (vec_len (wrk->unhandled_evts_vector))
2574 {
2575 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2576 {
2577 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2578 events, &n_evts);
2579 if (n_evts == maxevents)
2580 {
2581 i += 1;
2582 break;
2583 }
2584 }
Florin Coras86f04502018-09-12 16:08:01 -07002585 vec_delete (wrk->unhandled_evts_vector, i, 0);
2586 }
2587
2588 if (vcm->cfg.use_mq_eventfd)
2589 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2590 wait_for_time);
2591
2592 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2593 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002594}
2595
Dave Wallace35830af2017-10-09 01:43:42 -04002596int
Florin Coras134a9962018-08-28 11:32:04 -07002597vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04002598 void *buffer, uint32_t * buflen)
2599{
Florin Coras134a9962018-08-28 11:32:04 -07002600 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002601 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04002602 int rv = VPPCOM_OK;
Florin Coras7baeb712019-01-04 17:05:43 -08002603 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07002604 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04002605
Florin Coras134a9962018-08-28 11:32:04 -07002606 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002607 if (!session)
2608 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002609
Dave Wallace35830af2017-10-09 01:43:42 -04002610 switch (op)
2611 {
2612 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002613 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07002614 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
2615 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002616 break;
2617
Dave Wallace227867f2017-11-13 21:21:53 -05002618 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002619 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07002620 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
2621 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002622 break;
2623
2624 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002625 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002626 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002627 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2628 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002629 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07002630 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
2631 "is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002632 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002633 }
2634 else
2635 rv = VPPCOM_EINVAL;
2636 break;
2637
2638 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002639 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002640 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002641 if (*flags & O_NONBLOCK)
2642 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2643 else
2644 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2645
Florin Coras5e062572019-03-14 19:07:51 -07002646 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
2647 " is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002648 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002649 }
2650 else
2651 rv = VPPCOM_EINVAL;
2652 break;
2653
2654 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002655 if (PREDICT_TRUE (buffer && buflen &&
2656 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002657 {
Florin Coras7e12d942018-06-27 14:32:43 -07002658 ep->is_ip4 = session->transport.is_ip4;
2659 ep->port = session->transport.rmt_port;
2660 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002661 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2662 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002663 else
Dave Barach178cf492018-11-13 16:34:13 -05002664 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2665 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002666 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07002667 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
2668 "addr = %U, port %u", session_handle, ep->is_ip4,
2669 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002670 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2671 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002672 }
2673 else
2674 rv = VPPCOM_EINVAL;
2675 break;
2676
2677 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002678 if (PREDICT_TRUE (buffer && buflen &&
2679 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002680 {
Florin Coras7e12d942018-06-27 14:32:43 -07002681 ep->is_ip4 = session->transport.is_ip4;
2682 ep->port = session->transport.lcl_port;
2683 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002684 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2685 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002686 else
Dave Barach178cf492018-11-13 16:34:13 -05002687 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2688 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002689 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07002690 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
2691 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002692 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002693 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2694 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002695 }
2696 else
2697 rv = VPPCOM_EINVAL;
2698 break;
Stevenb5a11602017-10-11 09:59:30 -07002699
Dave Wallace048b1d62018-01-03 22:24:41 -05002700 case VPPCOM_ATTR_GET_LIBC_EPFD:
2701 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07002702 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002703 break;
2704
2705 case VPPCOM_ATTR_SET_LIBC_EPFD:
2706 if (PREDICT_TRUE (buffer && buflen &&
2707 (*buflen == sizeof (session->libc_epfd))))
2708 {
2709 session->libc_epfd = *(int *) buffer;
2710 *buflen = sizeof (session->libc_epfd);
2711
Florin Coras5e062572019-03-14 19:07:51 -07002712 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
2713 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002714 }
2715 else
2716 rv = VPPCOM_EINVAL;
2717 break;
2718
2719 case VPPCOM_ATTR_GET_PROTOCOL:
2720 if (buffer && buflen && (*buflen >= sizeof (int)))
2721 {
Florin Coras7e12d942018-06-27 14:32:43 -07002722 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05002723 *buflen = sizeof (int);
2724
Florin Coras5e062572019-03-14 19:07:51 -07002725 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2726 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002727 }
2728 else
2729 rv = VPPCOM_EINVAL;
2730 break;
2731
2732 case VPPCOM_ATTR_GET_LISTEN:
2733 if (buffer && buflen && (*buflen >= sizeof (int)))
2734 {
2735 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2736 VCL_SESS_ATTR_LISTEN);
2737 *buflen = sizeof (int);
2738
Florin Coras5e062572019-03-14 19:07:51 -07002739 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
2740 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002741 }
2742 else
2743 rv = VPPCOM_EINVAL;
2744 break;
2745
2746 case VPPCOM_ATTR_GET_ERROR:
2747 if (buffer && buflen && (*buflen >= sizeof (int)))
2748 {
2749 *(int *) buffer = 0;
2750 *buflen = sizeof (int);
2751
Florin Coras5e062572019-03-14 19:07:51 -07002752 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2753 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002754 }
2755 else
2756 rv = VPPCOM_EINVAL;
2757 break;
2758
2759 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2760 if (buffer && buflen && (*buflen >= sizeof (u32)))
2761 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002762
2763 /* VPP-TBD */
2764 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002765 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002766 vcm->cfg.tx_fifo_size);
2767 *buflen = sizeof (u32);
2768
Florin Coras5e062572019-03-14 19:07:51 -07002769 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
2770 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
2771 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002772 }
2773 else
2774 rv = VPPCOM_EINVAL;
2775 break;
2776
2777 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2778 if (buffer && buflen && (*buflen == sizeof (u32)))
2779 {
2780 /* VPP-TBD */
2781 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07002782 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
2783 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
2784 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002785 }
2786 else
2787 rv = VPPCOM_EINVAL;
2788 break;
2789
2790 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2791 if (buffer && buflen && (*buflen >= sizeof (u32)))
2792 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002793
2794 /* VPP-TBD */
2795 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002796 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002797 vcm->cfg.rx_fifo_size);
2798 *buflen = sizeof (u32);
2799
Florin Coras5e062572019-03-14 19:07:51 -07002800 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
2801 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002802 }
2803 else
2804 rv = VPPCOM_EINVAL;
2805 break;
2806
2807 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2808 if (buffer && buflen && (*buflen == sizeof (u32)))
2809 {
2810 /* VPP-TBD */
2811 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07002812 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
2813 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
2814 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002815 }
2816 else
2817 rv = VPPCOM_EINVAL;
2818 break;
2819
2820 case VPPCOM_ATTR_GET_REUSEADDR:
2821 if (buffer && buflen && (*buflen >= sizeof (int)))
2822 {
2823 /* VPP-TBD */
2824 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2825 VCL_SESS_ATTR_REUSEADDR);
2826 *buflen = sizeof (int);
2827
Florin Coras5e062572019-03-14 19:07:51 -07002828 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
2829 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002830 }
2831 else
2832 rv = VPPCOM_EINVAL;
2833 break;
2834
Stevenb5a11602017-10-11 09:59:30 -07002835 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002836 if (buffer && buflen && (*buflen == sizeof (int)) &&
2837 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2838 {
2839 /* VPP-TBD */
2840 if (*(int *) buffer)
2841 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2842 else
2843 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2844
Florin Coras5e062572019-03-14 19:07:51 -07002845 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
2846 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEADDR),
2847 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002848 }
2849 else
2850 rv = VPPCOM_EINVAL;
2851 break;
2852
2853 case VPPCOM_ATTR_GET_REUSEPORT:
2854 if (buffer && buflen && (*buflen >= sizeof (int)))
2855 {
2856 /* VPP-TBD */
2857 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2858 VCL_SESS_ATTR_REUSEPORT);
2859 *buflen = sizeof (int);
2860
Florin Coras5e062572019-03-14 19:07:51 -07002861 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
2862 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002863 }
2864 else
2865 rv = VPPCOM_EINVAL;
2866 break;
2867
2868 case VPPCOM_ATTR_SET_REUSEPORT:
2869 if (buffer && buflen && (*buflen == sizeof (int)) &&
2870 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2871 {
2872 /* VPP-TBD */
2873 if (*(int *) buffer)
2874 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2875 else
2876 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2877
Florin Coras5e062572019-03-14 19:07:51 -07002878 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
2879 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEPORT),
2880 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002881 }
2882 else
2883 rv = VPPCOM_EINVAL;
2884 break;
2885
2886 case VPPCOM_ATTR_GET_BROADCAST:
2887 if (buffer && buflen && (*buflen >= sizeof (int)))
2888 {
2889 /* VPP-TBD */
2890 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2891 VCL_SESS_ATTR_BROADCAST);
2892 *buflen = sizeof (int);
2893
Florin Coras5e062572019-03-14 19:07:51 -07002894 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
2895 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002896 }
2897 else
2898 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002899 break;
2900
2901 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05002902 if (buffer && buflen && (*buflen == sizeof (int)))
2903 {
2904 /* VPP-TBD */
2905 if (*(int *) buffer)
2906 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2907 else
2908 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2909
Florin Coras5e062572019-03-14 19:07:51 -07002910 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
2911 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_BROADCAST),
2912 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002913 }
2914 else
2915 rv = VPPCOM_EINVAL;
2916 break;
2917
2918 case VPPCOM_ATTR_GET_V6ONLY:
2919 if (buffer && buflen && (*buflen >= sizeof (int)))
2920 {
2921 /* VPP-TBD */
2922 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2923 VCL_SESS_ATTR_V6ONLY);
2924 *buflen = sizeof (int);
2925
Florin Coras5e062572019-03-14 19:07:51 -07002926 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
2927 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002928 }
2929 else
2930 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002931 break;
2932
2933 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05002934 if (buffer && buflen && (*buflen == sizeof (int)))
2935 {
2936 /* VPP-TBD */
2937 if (*(int *) buffer)
2938 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2939 else
2940 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2941
Florin Coras5e062572019-03-14 19:07:51 -07002942 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
2943 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_V6ONLY),
2944 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002945 }
2946 else
2947 rv = VPPCOM_EINVAL;
2948 break;
2949
2950 case VPPCOM_ATTR_GET_KEEPALIVE:
2951 if (buffer && buflen && (*buflen >= sizeof (int)))
2952 {
2953 /* VPP-TBD */
2954 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2955 VCL_SESS_ATTR_KEEPALIVE);
2956 *buflen = sizeof (int);
2957
Florin Coras5e062572019-03-14 19:07:51 -07002958 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
2959 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002960 }
2961 else
2962 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002963 break;
Stevenbccd3392017-10-12 20:42:21 -07002964
2965 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05002966 if (buffer && buflen && (*buflen == sizeof (int)))
2967 {
2968 /* VPP-TBD */
2969 if (*(int *) buffer)
2970 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2971 else
2972 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2973
Florin Coras5e062572019-03-14 19:07:51 -07002974 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
2975 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_KEEPALIVE),
2976 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002977 }
2978 else
2979 rv = VPPCOM_EINVAL;
2980 break;
2981
2982 case VPPCOM_ATTR_GET_TCP_NODELAY:
2983 if (buffer && buflen && (*buflen >= sizeof (int)))
2984 {
2985 /* VPP-TBD */
2986 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2987 VCL_SESS_ATTR_TCP_NODELAY);
2988 *buflen = sizeof (int);
2989
Florin Coras5e062572019-03-14 19:07:51 -07002990 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
2991 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002992 }
2993 else
2994 rv = VPPCOM_EINVAL;
2995 break;
2996
2997 case VPPCOM_ATTR_SET_TCP_NODELAY:
2998 if (buffer && buflen && (*buflen == sizeof (int)))
2999 {
3000 /* VPP-TBD */
3001 if (*(int *) buffer)
3002 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3003 else
3004 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3005
Florin Coras5e062572019-03-14 19:07:51 -07003006 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3007 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_TCP_NODELAY),
3008 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003009 }
3010 else
3011 rv = VPPCOM_EINVAL;
3012 break;
3013
3014 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3015 if (buffer && buflen && (*buflen >= sizeof (int)))
3016 {
3017 /* VPP-TBD */
3018 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3019 VCL_SESS_ATTR_TCP_KEEPIDLE);
3020 *buflen = sizeof (int);
3021
Florin Coras5e062572019-03-14 19:07:51 -07003022 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3023 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003024 }
3025 else
3026 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003027 break;
3028
3029 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003030 if (buffer && buflen && (*buflen == sizeof (int)))
3031 {
3032 /* VPP-TBD */
3033 if (*(int *) buffer)
3034 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3035 else
3036 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3037
Florin Coras5e062572019-03-14 19:07:51 -07003038 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003039 VCL_SESS_ATTR_TEST (session->attr,
3040 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003041 }
3042 else
3043 rv = VPPCOM_EINVAL;
3044 break;
3045
3046 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3047 if (buffer && buflen && (*buflen >= sizeof (int)))
3048 {
3049 /* VPP-TBD */
3050 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3051 VCL_SESS_ATTR_TCP_KEEPINTVL);
3052 *buflen = sizeof (int);
3053
Florin Coras5e062572019-03-14 19:07:51 -07003054 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3055 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003056 }
3057 else
3058 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003059 break;
3060
3061 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003062 if (buffer && buflen && (*buflen == sizeof (int)))
3063 {
3064 /* VPP-TBD */
3065 if (*(int *) buffer)
3066 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3067 else
3068 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3069
Florin Coras5e062572019-03-14 19:07:51 -07003070 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003071 VCL_SESS_ATTR_TEST (session->attr,
3072 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003073 }
3074 else
3075 rv = VPPCOM_EINVAL;
3076 break;
3077
3078 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3079 if (buffer && buflen && (*buflen >= sizeof (u32)))
3080 {
3081 /* VPP-TBD */
3082 *(u32 *) buffer = session->user_mss;
3083 *buflen = sizeof (int);
3084
Florin Coras5e062572019-03-14 19:07:51 -07003085 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3086 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003087 }
3088 else
3089 rv = VPPCOM_EINVAL;
3090 break;
3091
3092 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3093 if (buffer && buflen && (*buflen == sizeof (u32)))
3094 {
3095 /* VPP-TBD */
3096 session->user_mss = *(u32 *) buffer;
3097
Florin Coras5e062572019-03-14 19:07:51 -07003098 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3099 session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003100 }
3101 else
3102 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003103 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003104
Florin Coras7baeb712019-01-04 17:05:43 -08003105 case VPPCOM_ATTR_SET_SHUT:
3106 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3107 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3108 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3109 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3110 break;
3111
3112 case VPPCOM_ATTR_GET_SHUT:
3113 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3114 tmp_flags = 1;
3115 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3116 tmp_flags |= 2;
3117 if (tmp_flags == 1)
3118 *(int *) buffer = SHUT_RD;
3119 else if (tmp_flags == 2)
3120 *(int *) buffer = SHUT_WR;
3121 else if (tmp_flags == 3)
3122 *(int *) buffer = SHUT_RDWR;
3123 *buflen = sizeof (int);
3124 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003125 default:
3126 rv = VPPCOM_EINVAL;
3127 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003128 }
3129
Dave Wallace35830af2017-10-09 01:43:42 -04003130 return rv;
3131}
3132
Stevenac1f96d2017-10-24 16:03:58 -07003133int
Florin Coras134a9962018-08-28 11:32:04 -07003134vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003135 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3136{
Florin Coras134a9962018-08-28 11:32:04 -07003137 vcl_worker_t *wrk = vcl_worker_get_current ();
Stevenac1f96d2017-10-24 16:03:58 -07003138 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07003139 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07003140
3141 if (ep)
3142 {
Florin Coras134a9962018-08-28 11:32:04 -07003143 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003144 if (PREDICT_FALSE (!session))
Stevenac1f96d2017-10-24 16:03:58 -07003145 {
Florin Coras5e062572019-03-14 19:07:51 -07003146 VDBG (0, "sh 0x%llx is closed!", session_handle);
Florin Coras460dce62018-07-27 05:45:06 -07003147 return VPPCOM_EBADFD;
Stevenac1f96d2017-10-24 16:03:58 -07003148 }
Florin Coras7e12d942018-06-27 14:32:43 -07003149 ep->is_ip4 = session->transport.is_ip4;
3150 ep->port = session->transport.rmt_port;
Stevenac1f96d2017-10-24 16:03:58 -07003151 }
Steven58f464e2017-10-25 12:33:12 -07003152
3153 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003154 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003155 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003156 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003157 else
3158 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003159 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003160 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003161 }
3162
Florin Coras99368312018-08-02 10:45:44 -07003163 if (ep)
3164 {
3165 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003166 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3167 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003168 else
Dave Barach178cf492018-11-13 16:34:13 -05003169 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3170 sizeof (ip6_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003171 }
Florin Coras460dce62018-07-27 05:45:06 -07003172
Stevenac1f96d2017-10-24 16:03:58 -07003173 return rv;
3174}
3175
3176int
Florin Coras134a9962018-08-28 11:32:04 -07003177vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003178 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3179{
Dave Wallace617dffa2017-10-26 14:47:06 -04003180 if (!buffer)
3181 return VPPCOM_EINVAL;
3182
3183 if (ep)
3184 {
3185 // TBD
3186 return VPPCOM_EINVAL;
3187 }
3188
3189 if (flags)
3190 {
3191 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003192 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003193 }
3194
Florin Coras42ceddb2018-12-12 10:56:01 -08003195 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
Stevenac1f96d2017-10-24 16:03:58 -07003196}
3197
Dave Wallace048b1d62018-01-03 22:24:41 -05003198int
3199vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3200{
Florin Coras134a9962018-08-28 11:32:04 -07003201 vcl_worker_t *wrk = vcl_worker_get_current ();
3202 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003203 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003204 svm_msg_q_msg_t msg;
3205 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003206 int rv, num_ev = 0;
3207
Florin Coras5e062572019-03-14 19:07:51 -07003208 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003209
3210 if (!vp)
3211 return VPPCOM_EFAULT;
3212
3213 do
3214 {
Florin Coras7e12d942018-06-27 14:32:43 -07003215 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003216
Florin Coras6917b942018-11-13 22:44:54 -08003217 /* Dequeue all events and drop all unhandled io events */
3218 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3219 {
3220 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3221 vcl_handle_mq_event (wrk, e);
3222 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3223 }
3224 vec_reset_length (wrk->unhandled_evts_vector);
3225
Dave Wallace048b1d62018-01-03 22:24:41 -05003226 for (i = 0; i < n_sids; i++)
3227 {
Florin Coras7baeb712019-01-04 17:05:43 -08003228 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003229 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003230 {
3231 vp[i].revents = POLLHUP;
3232 num_ev++;
3233 continue;
3234 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003235
Florin Coras6917b942018-11-13 22:44:54 -08003236 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003237
3238 if (POLLIN & vp[i].events)
3239 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003240 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003241 if (rv > 0)
3242 {
Florin Coras6917b942018-11-13 22:44:54 -08003243 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003244 num_ev++;
3245 }
3246 else if (rv < 0)
3247 {
3248 switch (rv)
3249 {
3250 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003251 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003252 break;
3253
3254 default:
Florin Coras6917b942018-11-13 22:44:54 -08003255 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003256 break;
3257 }
3258 num_ev++;
3259 }
3260 }
3261
3262 if (POLLOUT & vp[i].events)
3263 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003264 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003265 if (rv > 0)
3266 {
Florin Coras6917b942018-11-13 22:44:54 -08003267 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003268 num_ev++;
3269 }
3270 else if (rv < 0)
3271 {
3272 switch (rv)
3273 {
3274 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003275 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003276 break;
3277
3278 default:
Florin Coras6917b942018-11-13 22:44:54 -08003279 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003280 break;
3281 }
3282 num_ev++;
3283 }
3284 }
3285
Dave Wallace7e607a72018-06-18 18:41:32 -04003286 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003287 {
Florin Coras6917b942018-11-13 22:44:54 -08003288 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003289 num_ev++;
3290 }
3291 }
3292 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003293 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003294 }
3295 while ((num_ev == 0) && keep_trying);
3296
Dave Wallace048b1d62018-01-03 22:24:41 -05003297 return num_ev;
3298}
3299
Florin Coras99368312018-08-02 10:45:44 -07003300int
3301vppcom_mq_epoll_fd (void)
3302{
Florin Coras134a9962018-08-28 11:32:04 -07003303 vcl_worker_t *wrk = vcl_worker_get_current ();
3304 return wrk->mqs_epfd;
3305}
3306
3307int
Florin Coras30e79c22019-01-02 19:31:22 -08003308vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003309{
3310 return session_handle & 0xFFFFFF;
3311}
3312
3313int
Florin Coras30e79c22019-01-02 19:31:22 -08003314vppcom_session_worker (vcl_session_handle_t session_handle)
3315{
3316 return session_handle >> 24;
3317}
3318
3319int
Florin Coras134a9962018-08-28 11:32:04 -07003320vppcom_worker_register (void)
3321{
Florin Coras47c40e22018-11-26 17:01:36 -08003322 if (!vcl_worker_alloc_and_init ())
3323 return VPPCOM_EEXIST;
3324
3325 if (vcl_worker_set_bapi ())
3326 return VPPCOM_EEXIST;
3327
3328 if (vcl_worker_register_with_vpp ())
3329 return VPPCOM_EEXIST;
3330
3331 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003332}
3333
Florin Corasdfe4cf42018-11-28 22:13:45 -08003334int
3335vppcom_worker_index (void)
3336{
3337 return vcl_get_worker_index ();
3338}
3339
Florin Corase0982e52019-01-25 13:19:56 -08003340int
3341vppcom_worker_mqs_epfd (void)
3342{
3343 vcl_worker_t *wrk = vcl_worker_get_current ();
3344 if (!vcm->cfg.use_mq_eventfd)
3345 return -1;
3346 return wrk->mqs_epfd;
3347}
3348
Dave Wallacee22aa742017-10-20 12:30:38 -04003349/*
3350 * fd.io coding-style-patch-verification: ON
3351 *
3352 * Local Variables:
3353 * eval: (c-set-style "gnu")
3354 * End:
3355 */