blob: 1641beee9f67b5944ba8a1ef3771b79898d93ecd [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 Wallace5c7cf1c2017-10-24 04:12:18 -040018#include <vcl/vppcom.h>
Florin Coras0d427d82018-06-27 03:24:07 -070019#include <vcl/vcl_debug.h>
Florin Coras697faea2018-06-27 17:10:49 -070020#include <vcl/vcl_private.h>
Florin Coras88001c62019-04-24 14:44:46 -070021#include <svm/fifo_segment.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;
Florin Coras09d18c22019-04-24 11:10:02 -0700312 session->transport.rmt_port = mp->rmt.port;
313 session->transport.is_ip4 = mp->rmt.is_ip4;
314 clib_memcpy_fast (&session->transport.rmt_ip, &mp->rmt.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500315 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 Coras09d18c22019-04-24 11:10:02 -0700325 mp->rmt.is_ip4 ? "IPv4" : "IPv6", format_ip46_address, &mp->rmt.ip,
326 mp->rmt.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
327 clib_net_to_host_u16 (mp->rmt.port), session->vpp_evt_q);
Florin Coras54693d22018-07-17 10:46:29 -0700328 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 Coras09d18c22019-04-24 11:10:02 -0700396 session->transport.is_ip4 = mp->lcl.is_ip4;
397 clib_memcpy_fast (&session->transport.lcl_ip, &mp->lcl.ip,
Dave Barach178cf492018-11-13 16:34:13 -0500398 sizeof (session->transport.lcl_ip));
Florin Coras09d18c22019-04-24 11:10:02 -0700399 session->transport.lcl_port = mp->lcl.port;
Florin Coras54693d22018-07-17 10:46:29 -0700400 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 Coras88001c62019-04-24 14:44:46 -0700927 fifo_segment_main_init (&vcm->segment_main, vcl_cfg->segment_baseva,
928 20 /* timeout in secs */ );
Florin Coras47c40e22018-11-26 17:01:36 -0800929 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 Coras317b8e02019-04-17 09:57:46 -07001533 /* Cut-through sessions might request tx notifications on rx fifos */
1534 if (PREDICT_FALSE (rx_fifo->want_tx_ntf))
1535 {
1536 app_send_io_evt_to_vpp (s->vpp_evt_q, s->rx_fifo->master_session_index,
1537 SESSION_IO_EVT_RX, SVM_Q_WAIT);
1538 svm_fifo_reset_tx_ntf (s->rx_fifo);
1539 }
1540
Florin Coras5e062572019-03-14 19:07:51 -07001541 VDBG (2, "session %u[0x%llx]: read %d bytes from (%p)", s->session_index,
1542 s->vpp_handle, n_read, rx_fifo);
Florin Coras2cba8532018-09-11 16:33:36 -07001543
Florin Coras54693d22018-07-17 10:46:29 -07001544 return n_read;
Dave Wallace543852a2017-08-03 02:11:34 -04001545}
1546
Steven58f464e2017-10-25 12:33:12 -07001547int
Florin Coras134a9962018-08-28 11:32:04 -07001548vppcom_session_read (uint32_t session_handle, void *buf, size_t n)
Steven58f464e2017-10-25 12:33:12 -07001549{
Florin Coras134a9962018-08-28 11:32:04 -07001550 return (vppcom_session_read_internal (session_handle, buf, n, 0));
Steven58f464e2017-10-25 12:33:12 -07001551}
1552
1553static int
Florin Coras134a9962018-08-28 11:32:04 -07001554vppcom_session_peek (uint32_t session_handle, void *buf, int n)
Steven58f464e2017-10-25 12:33:12 -07001555{
Florin Coras134a9962018-08-28 11:32:04 -07001556 return (vppcom_session_read_internal (session_handle, buf, n, 1));
Steven58f464e2017-10-25 12:33:12 -07001557}
1558
Florin Coras2cba8532018-09-11 16:33:36 -07001559int
1560vppcom_session_read_segments (uint32_t session_handle,
1561 vppcom_data_segments_t ds)
1562{
1563 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001564 int n_read = 0, is_nonblocking;
Florin Coras2cba8532018-09-11 16:33:36 -07001565 vcl_session_t *s = 0;
1566 svm_fifo_t *rx_fifo;
1567 svm_msg_q_msg_t msg;
1568 session_event_t *e;
1569 svm_msg_q_t *mq;
1570 u8 is_ct;
1571
1572 s = vcl_session_get_w_handle (wrk, session_handle);
1573 if (PREDICT_FALSE (!s || s->is_vep))
1574 return VPPCOM_EBADFD;
1575
Florin Coras6d0106e2019-01-29 20:11:58 -08001576 if (PREDICT_FALSE (!vcl_session_is_open (s)))
1577 return vcl_session_closed_error (s);
Florin Coras2cba8532018-09-11 16:33:36 -07001578
1579 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
1580 is_ct = vcl_session_is_ct (s);
1581 mq = is_ct ? s->our_evt_q : wrk->app_event_queue;
1582 rx_fifo = s->rx_fifo;
Florin Corasaa27eb92018-10-13 12:20:01 -07001583 s->has_rx_evt = 0;
Florin Coras2cba8532018-09-11 16:33:36 -07001584
Florin Coras653e43f2019-03-04 10:56:23 -08001585 if (is_ct)
1586 svm_fifo_unset_event (s->rx_fifo);
1587
Sirshak Das28aa5392019-02-05 01:33:33 -06001588 if (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001589 {
1590 if (is_nonblocking)
1591 {
1592 svm_fifo_unset_event (rx_fifo);
Florin Corasaa27eb92018-10-13 12:20:01 -07001593 return VPPCOM_EWOULDBLOCK;
Florin Coras2cba8532018-09-11 16:33:36 -07001594 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001595 while (svm_fifo_is_empty_cons (rx_fifo))
Florin Coras2cba8532018-09-11 16:33:36 -07001596 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001597 if (vcl_session_is_closing (s))
1598 return vcl_session_closing_error (s);
1599
Florin Coras2cba8532018-09-11 16:33:36 -07001600 svm_fifo_unset_event (rx_fifo);
1601 svm_msg_q_lock (mq);
1602 if (svm_msg_q_is_empty (mq))
1603 svm_msg_q_wait (mq);
1604
1605 svm_msg_q_sub_w_lock (mq, &msg);
1606 e = svm_msg_q_msg_data (mq, &msg);
1607 svm_msg_q_unlock (mq);
1608 if (!vcl_is_rx_evt_for_session (e, s->session_index, is_ct))
Florin Coras05ce4b82018-12-15 18:30:43 -08001609 vcl_handle_mq_event (wrk, e);
Florin Coras2cba8532018-09-11 16:33:36 -07001610 svm_msg_q_free_msg (mq, &msg);
Florin Coras2cba8532018-09-11 16:33:36 -07001611 }
1612 }
1613
Florin Coras88001c62019-04-24 14:44:46 -07001614 n_read = svm_fifo_segments (rx_fifo, (svm_fifo_seg_t *) ds);
Florin Coras2cba8532018-09-11 16:33:36 -07001615 svm_fifo_unset_event (rx_fifo);
1616
Florin Coras2cba8532018-09-11 16:33:36 -07001617 return n_read;
1618}
1619
1620void
1621vppcom_session_free_segments (uint32_t session_handle,
1622 vppcom_data_segments_t ds)
1623{
1624 vcl_worker_t *wrk = vcl_worker_get_current ();
1625 vcl_session_t *s;
1626
1627 s = vcl_session_get_w_handle (wrk, session_handle);
1628 if (PREDICT_FALSE (!s || s->is_vep))
1629 return;
1630
Florin Coras88001c62019-04-24 14:44:46 -07001631 svm_fifo_segments_free (s->rx_fifo, (svm_fifo_seg_t *) ds);
Florin Coras2cba8532018-09-11 16:33:36 -07001632}
1633
Florin Coras2cba8532018-09-11 16:33:36 -07001634int
1635vppcom_data_segment_copy (void *buf, vppcom_data_segments_t ds, u32 max_bytes)
1636{
1637 u32 first_copy = clib_min (ds[0].len, max_bytes);
Dave Barach178cf492018-11-13 16:34:13 -05001638 clib_memcpy_fast (buf, ds[0].data, first_copy);
Florin Coras2cba8532018-09-11 16:33:36 -07001639 if (first_copy < max_bytes)
1640 {
Dave Barach178cf492018-11-13 16:34:13 -05001641 clib_memcpy_fast (buf + first_copy, ds[1].data,
1642 clib_min (ds[1].len, max_bytes - first_copy));
Florin Coras2cba8532018-09-11 16:33:36 -07001643 }
1644 return 0;
1645}
1646
Florin Coras54693d22018-07-17 10:46:29 -07001647static u8
1648vcl_is_tx_evt_for_session (session_event_t * e, u32 sid, u8 is_ct)
1649{
Florin Corasc0737e92019-03-04 14:19:39 -08001650 return (e->event_type == SESSION_IO_EVT_TX && e->session_index == sid);
Dave Wallace543852a2017-08-03 02:11:34 -04001651}
1652
Florin Coras42ceddb2018-12-12 10:56:01 -08001653static inline int
1654vppcom_session_write_inline (uint32_t session_handle, void *buf, size_t n,
1655 u8 is_flush)
Dave Wallace543852a2017-08-03 02:11:34 -04001656{
Florin Coras134a9962018-08-28 11:32:04 -07001657 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras6d0106e2019-01-29 20:11:58 -08001658 int n_write, is_nonblocking;
Florin Coras460dce62018-07-27 05:45:06 -07001659 vcl_session_t *s = 0;
Florin Coras460dce62018-07-27 05:45:06 -07001660 session_evt_type_t et;
Florin Coras54693d22018-07-17 10:46:29 -07001661 svm_msg_q_msg_t msg;
Florin Coras14ed6df2019-03-06 21:13:42 -08001662 svm_fifo_t *tx_fifo;
Florin Coras54693d22018-07-17 10:46:29 -07001663 session_event_t *e;
Florin Coras3c2fed52018-07-04 04:15:05 -07001664 svm_msg_q_t *mq;
Florin Coras0e88e852018-09-17 22:09:02 -07001665 u8 is_ct;
Dave Wallace543852a2017-08-03 02:11:34 -04001666
Florin Coras070453d2018-08-24 17:04:27 -07001667 if (PREDICT_FALSE (!buf))
1668 return VPPCOM_EINVAL;
Dave Wallace543852a2017-08-03 02:11:34 -04001669
Florin Coras134a9962018-08-28 11:32:04 -07001670 s = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001671 if (PREDICT_FALSE (!s))
1672 return VPPCOM_EBADFD;
Dave Wallace4878cbe2017-11-21 03:45:09 -05001673
Florin Coras460dce62018-07-27 05:45:06 -07001674 if (PREDICT_FALSE (s->is_vep))
Dave Wallace543852a2017-08-03 02:11:34 -04001675 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001676 VDBG (0, "ERROR: session %u [0x%llx]: cannot write to an epoll"
1677 " session!", s->session_index, s->vpp_handle);
Florin Coras070453d2018-08-24 17:04:27 -07001678 return VPPCOM_EBADFD;
Dave Wallace543852a2017-08-03 02:11:34 -04001679 }
1680
Florin Coras6d0106e2019-01-29 20:11:58 -08001681 if (PREDICT_FALSE (!vcl_session_is_open (s)))
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001682 {
Florin Coras6d0106e2019-01-29 20:11:58 -08001683 VDBG (1, "session %u [0x%llx]: is not open! state 0x%x (%s)",
1684 s->session_index, s->vpp_handle, s->session_state,
1685 vppcom_session_state_str (s->session_state));
1686 return vcl_session_closed_error (s);;
Dave Wallace9c4b5b22017-10-18 21:15:48 -04001687 }
1688
Florin Coras0e88e852018-09-17 22:09:02 -07001689 is_ct = vcl_session_is_ct (s);
Florin Coras653e43f2019-03-04 10:56:23 -08001690 tx_fifo = is_ct ? s->ct_tx_fifo : s->tx_fifo;
Florin Coras0e88e852018-09-17 22:09:02 -07001691 is_nonblocking = VCL_SESS_ATTR_TEST (s->attr, VCL_SESS_ATTR_NONBLOCK);
Sirshak Das28aa5392019-02-05 01:33:33 -06001692
Florin Coras653e43f2019-03-04 10:56:23 -08001693 mq = wrk->app_event_queue;
Sirshak Das28aa5392019-02-05 01:33:33 -06001694 if (svm_fifo_is_full_prod (tx_fifo))
Dave Wallace543852a2017-08-03 02:11:34 -04001695 {
Florin Coras54693d22018-07-17 10:46:29 -07001696 if (is_nonblocking)
1697 {
Florin Coras070453d2018-08-24 17:04:27 -07001698 return VPPCOM_EWOULDBLOCK;
Florin Coras54693d22018-07-17 10:46:29 -07001699 }
Sirshak Das28aa5392019-02-05 01:33:33 -06001700 while (svm_fifo_is_full_prod (tx_fifo))
Florin Coras54693d22018-07-17 10:46:29 -07001701 {
Florin Coras1bcad5c2019-01-09 20:04:38 -08001702 svm_fifo_add_want_tx_ntf (tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
Florin Coras6d0106e2019-01-29 20:11:58 -08001703 if (vcl_session_is_closing (s))
1704 return vcl_session_closing_error (s);
Florin Coras99368312018-08-02 10:45:44 -07001705 svm_msg_q_lock (mq);
Florin Corasaa27eb92018-10-13 12:20:01 -07001706 if (svm_msg_q_is_empty (mq))
1707 svm_msg_q_wait (mq);
Florin Coras0e88e852018-09-17 22:09:02 -07001708
Florin Coras54693d22018-07-17 10:46:29 -07001709 svm_msg_q_sub_w_lock (mq, &msg);
1710 e = svm_msg_q_msg_data (mq, &msg);
Florin Coras99368312018-08-02 10:45:44 -07001711 svm_msg_q_unlock (mq);
1712
Florin Coras0e88e852018-09-17 22:09:02 -07001713 if (!vcl_is_tx_evt_for_session (e, s->session_index, is_ct))
Florin Coras86f04502018-09-12 16:08:01 -07001714 vcl_handle_mq_event (wrk, e);
Florin Coras54693d22018-07-17 10:46:29 -07001715 svm_msg_q_free_msg (mq, &msg);
Florin Coras54693d22018-07-17 10:46:29 -07001716 }
Dave Wallace543852a2017-08-03 02:11:34 -04001717 }
Dave Wallace543852a2017-08-03 02:11:34 -04001718
Florin Coras653e43f2019-03-04 10:56:23 -08001719 et = SESSION_IO_EVT_TX;
1720 if (is_flush && !is_ct)
Florin Coras42ceddb2018-12-12 10:56:01 -08001721 et = SESSION_IO_EVT_TX_FLUSH;
1722
Florin Coras460dce62018-07-27 05:45:06 -07001723 if (s->is_dgram)
1724 n_write = app_send_dgram_raw (tx_fifo, &s->transport,
Florin Coras653e43f2019-03-04 10:56:23 -08001725 s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08001726 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras460dce62018-07-27 05:45:06 -07001727 else
1728 n_write = app_send_stream_raw (tx_fifo, s->vpp_evt_q, buf, n, et,
Florin Coras14ed6df2019-03-06 21:13:42 -08001729 0 /* do_evt */ , SVM_Q_WAIT);
Florin Coras653e43f2019-03-04 10:56:23 -08001730
Florin Coras14ed6df2019-03-06 21:13:42 -08001731 if (svm_fifo_set_event (s->tx_fifo))
Florin Corasc0737e92019-03-04 14:19:39 -08001732 app_send_io_evt_to_vpp (s->vpp_evt_q, s->tx_fifo->master_session_index,
1733 et, SVM_Q_WAIT);
Keith Burns (alagalah)410bcca2018-03-23 13:42:49 -07001734
Florin Coras460dce62018-07-27 05:45:06 -07001735 ASSERT (n_write > 0);
Dave Wallace543852a2017-08-03 02:11:34 -04001736
Florin Coras6d0106e2019-01-29 20:11:58 -08001737 VDBG (2, "session %u [0x%llx]: wrote %d bytes", s->session_index,
1738 s->vpp_handle, n_write);
Florin Coras0e88e852018-09-17 22:09:02 -07001739
Florin Coras54693d22018-07-17 10:46:29 -07001740 return n_write;
Dave Wallace543852a2017-08-03 02:11:34 -04001741}
1742
Florin Coras42ceddb2018-12-12 10:56:01 -08001743int
1744vppcom_session_write (uint32_t session_handle, void *buf, size_t n)
1745{
1746 return vppcom_session_write_inline (session_handle, buf, n,
1747 0 /* is_flush */ );
1748}
1749
Florin Corasb0f662f2018-12-27 14:51:46 -08001750int
1751vppcom_session_write_msg (uint32_t session_handle, void *buf, size_t n)
1752{
1753 return vppcom_session_write_inline (session_handle, buf, n,
1754 1 /* is_flush */ );
1755}
1756
Florin Corasc0737e92019-03-04 14:19:39 -08001757#define vcl_fifo_rx_evt_valid_or_break(_s) \
1758if (PREDICT_FALSE (svm_fifo_is_empty (_s->rx_fifo))) \
1759 { \
1760 if (!vcl_session_is_ct (_s)) \
1761 { \
1762 svm_fifo_unset_event (_s->rx_fifo); \
1763 if (svm_fifo_is_empty (_s->rx_fifo)) \
1764 break; \
1765 } \
1766 else if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1767 { \
1768 svm_fifo_unset_event (_s->ct_rx_fifo); \
1769 if (svm_fifo_is_empty (_s->ct_rx_fifo)) \
1770 break; \
1771 } \
1772 } \
Florin Coras6d4bb422018-09-04 22:07:27 -07001773
Florin Coras86f04502018-09-12 16:08:01 -07001774static void
1775vcl_select_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
1776 unsigned long n_bits, unsigned long *read_map,
1777 unsigned long *write_map,
1778 unsigned long *except_map, u32 * bits_set)
Florin Coras54693d22018-07-17 10:46:29 -07001779{
1780 session_disconnected_msg_t *disconnected_msg;
Florin Coras99368312018-08-02 10:45:44 -07001781 session_connected_msg_t *connected_msg;
Florin Coras54693d22018-07-17 10:46:29 -07001782 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07001783 u32 sid;
1784
1785 switch (e->event_type)
1786 {
Florin Corasc0737e92019-03-04 14:19:39 -08001787 case SESSION_IO_EVT_RX:
1788 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001789 session = vcl_session_get (wrk, sid);
1790 if (!session)
1791 break;
Florin Corasfff68f72019-03-13 16:01:38 -07001792 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07001793 if (sid < n_bits && read_map)
1794 {
David Johnsond9818dd2018-12-14 14:53:41 -05001795 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001796 *bits_set += 1;
1797 }
1798 break;
Florin Corasfe97da32019-03-06 10:09:04 -08001799 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08001800 sid = e->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001801 session = vcl_session_get (wrk, sid);
1802 if (!session)
1803 break;
1804 if (sid < n_bits && write_map)
1805 {
David Johnsond9818dd2018-12-14 14:53:41 -05001806 clib_bitmap_set_no_check ((uword *) write_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001807 *bits_set += 1;
1808 }
1809 break;
Florin Coras86f04502018-09-12 16:08:01 -07001810 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08001811 session = vcl_session_accepted (wrk,
1812 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07001813 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08001814 break;
Florin Coras86f04502018-09-12 16:08:01 -07001815 sid = session->session_index;
1816 if (sid < n_bits && read_map)
1817 {
David Johnsond9818dd2018-12-14 14:53:41 -05001818 clib_bitmap_set_no_check ((uword *) read_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001819 *bits_set += 1;
1820 }
1821 break;
1822 case SESSION_CTRL_EVT_CONNECTED:
1823 connected_msg = (session_connected_msg_t *) e->data;
1824 vcl_session_connected_handler (wrk, connected_msg);
1825 break;
1826 case SESSION_CTRL_EVT_DISCONNECTED:
1827 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08001828 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
1829 if (!session)
1830 break;
1831 sid = session->session_index;
Florin Coras86f04502018-09-12 16:08:01 -07001832 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;
1838 case SESSION_CTRL_EVT_RESET:
1839 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
1840 if (sid < n_bits && except_map)
1841 {
David Johnsond9818dd2018-12-14 14:53:41 -05001842 clib_bitmap_set_no_check ((uword *) except_map, sid, 1);
Florin Coras86f04502018-09-12 16:08:01 -07001843 *bits_set += 1;
1844 }
1845 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08001846 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
1847 vcl_session_unlisten_reply_handler (wrk, e->data);
1848 break;
Florin Coras30e79c22019-01-02 19:31:22 -08001849 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
1850 vcl_session_worker_update_reply_handler (wrk, e->data);
1851 break;
1852 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
1853 vcl_session_req_worker_update_handler (wrk, e->data);
1854 break;
Florin Coras86f04502018-09-12 16:08:01 -07001855 default:
1856 clib_warning ("unhandled: %u", e->event_type);
1857 break;
1858 }
1859}
1860
1861static int
1862vcl_select_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
1863 unsigned long n_bits, unsigned long *read_map,
1864 unsigned long *write_map, unsigned long *except_map,
1865 double time_to_wait, u32 * bits_set)
1866{
Florin Coras99368312018-08-02 10:45:44 -07001867 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07001868 session_event_t *e;
Florin Coras86f04502018-09-12 16:08:01 -07001869 u32 i;
Florin Coras54693d22018-07-17 10:46:29 -07001870
1871 svm_msg_q_lock (mq);
1872 if (svm_msg_q_is_empty (mq))
Dave Wallace4878cbe2017-11-21 03:45:09 -05001873 {
Florin Coras54693d22018-07-17 10:46:29 -07001874 if (*bits_set)
Dave Wallace4878cbe2017-11-21 03:45:09 -05001875 {
Florin Coras54693d22018-07-17 10:46:29 -07001876 svm_msg_q_unlock (mq);
1877 return 0;
1878 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001879
Florin Coras54693d22018-07-17 10:46:29 -07001880 if (!time_to_wait)
1881 {
1882 svm_msg_q_unlock (mq);
1883 return 0;
1884 }
1885 else if (time_to_wait < 0)
1886 {
1887 svm_msg_q_wait (mq);
1888 }
1889 else
1890 {
1891 if (svm_msg_q_timedwait (mq, time_to_wait))
1892 {
1893 svm_msg_q_unlock (mq);
1894 return 0;
1895 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05001896 }
1897 }
Florin Coras134a9962018-08-28 11:32:04 -07001898 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07001899 svm_msg_q_unlock (mq);
1900
Florin Coras134a9962018-08-28 11:32:04 -07001901 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07001902 {
Florin Coras134a9962018-08-28 11:32:04 -07001903 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07001904 e = svm_msg_q_msg_data (mq, msg);
Florin Coras86f04502018-09-12 16:08:01 -07001905 vcl_select_handle_mq_event (wrk, e, n_bits, read_map, write_map,
1906 except_map, bits_set);
Florin Coras99368312018-08-02 10:45:44 -07001907 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07001908 }
Florin Coras134a9962018-08-28 11:32:04 -07001909 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08001910 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07001911 return *bits_set;
Dave Wallace543852a2017-08-03 02:11:34 -04001912}
1913
Florin Coras99368312018-08-02 10:45:44 -07001914static int
Florin Coras294afe22019-01-07 17:49:17 -08001915vppcom_select_condvar (vcl_worker_t * wrk, int n_bits,
1916 vcl_si_set * read_map, vcl_si_set * write_map,
1917 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07001918 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07001919{
Florin Coras14ed6df2019-03-06 21:13:42 -08001920 double wait = 0, start = 0;
1921
1922 if (!*bits_set)
1923 {
1924 wait = time_to_wait;
1925 start = clib_time_now (&wrk->clib_time);
1926 }
1927
1928 do
1929 {
1930 vcl_select_handle_mq (wrk, wrk->app_event_queue, n_bits, read_map,
1931 write_map, except_map, wait, bits_set);
1932 if (*bits_set)
1933 return *bits_set;
1934 if (wait == -1)
1935 continue;
1936
1937 wait = wait - (clib_time_now (&wrk->clib_time) - start);
1938 }
1939 while (wait > 0);
1940
1941 return 0;
Florin Coras99368312018-08-02 10:45:44 -07001942}
1943
1944static int
Florin Coras294afe22019-01-07 17:49:17 -08001945vppcom_select_eventfd (vcl_worker_t * wrk, int n_bits,
1946 vcl_si_set * read_map, vcl_si_set * write_map,
1947 vcl_si_set * except_map, double time_to_wait,
Florin Coras134a9962018-08-28 11:32:04 -07001948 u32 * bits_set)
Florin Coras99368312018-08-02 10:45:44 -07001949{
1950 vcl_mq_evt_conn_t *mqc;
1951 int __clib_unused n_read;
1952 int n_mq_evts, i;
1953 u64 buf;
1954
Florin Coras134a9962018-08-28 11:32:04 -07001955 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
1956 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
1957 vec_len (wrk->mq_events), time_to_wait);
Florin Coras99368312018-08-02 10:45:44 -07001958 for (i = 0; i < n_mq_evts; i++)
1959 {
Florin Coras134a9962018-08-28 11:32:04 -07001960 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07001961 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07001962 vcl_select_handle_mq (wrk, mqc->mq, n_bits, read_map, write_map,
Florin Coras99368312018-08-02 10:45:44 -07001963 except_map, 0, bits_set);
1964 }
1965
1966 return (n_mq_evts > 0 ? (int) *bits_set : 0);
1967}
1968
Dave Wallace543852a2017-08-03 02:11:34 -04001969int
Florin Coras294afe22019-01-07 17:49:17 -08001970vppcom_select (int n_bits, vcl_si_set * read_map, vcl_si_set * write_map,
1971 vcl_si_set * except_map, double time_to_wait)
Dave Wallace543852a2017-08-03 02:11:34 -04001972{
Florin Coras54693d22018-07-17 10:46:29 -07001973 u32 sid, minbits = clib_max (n_bits, BITS (uword)), bits_set = 0;
Florin Coras134a9962018-08-28 11:32:04 -07001974 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07001975 vcl_session_t *session = 0;
Florin Coras86f04502018-09-12 16:08:01 -07001976 int rv, i;
Dave Wallace543852a2017-08-03 02:11:34 -04001977
Dave Wallace7876d392017-10-19 03:53:57 -04001978 if (n_bits && read_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001979 {
Florin Coras134a9962018-08-28 11:32:04 -07001980 clib_bitmap_validate (wrk->rd_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05001981 clib_memcpy_fast (wrk->rd_bitmap, read_map,
Florin Coras294afe22019-01-07 17:49:17 -08001982 vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
1983 memset (read_map, 0, vec_len (wrk->rd_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04001984 }
Dave Wallace7876d392017-10-19 03:53:57 -04001985 if (n_bits && write_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001986 {
Florin Coras134a9962018-08-28 11:32:04 -07001987 clib_bitmap_validate (wrk->wr_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05001988 clib_memcpy_fast (wrk->wr_bitmap, write_map,
Florin Coras294afe22019-01-07 17:49:17 -08001989 vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
1990 memset (write_map, 0, vec_len (wrk->wr_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04001991 }
Dave Wallace7876d392017-10-19 03:53:57 -04001992 if (n_bits && except_map)
Dave Wallace543852a2017-08-03 02:11:34 -04001993 {
Florin Coras134a9962018-08-28 11:32:04 -07001994 clib_bitmap_validate (wrk->ex_bitmap, minbits);
Dave Barach178cf492018-11-13 16:34:13 -05001995 clib_memcpy_fast (wrk->ex_bitmap, except_map,
Florin Coras294afe22019-01-07 17:49:17 -08001996 vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
1997 memset (except_map, 0, vec_len (wrk->ex_bitmap) * sizeof (vcl_si_set));
Dave Wallace543852a2017-08-03 02:11:34 -04001998 }
1999
Florin Coras54693d22018-07-17 10:46:29 -07002000 if (!n_bits)
2001 return 0;
2002
2003 if (!write_map)
2004 goto check_rd;
2005
2006 /* *INDENT-OFF* */
Florin Coras134a9962018-08-28 11:32:04 -07002007 clib_bitmap_foreach (sid, wrk->wr_bitmap, ({
2008 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002009 {
Florin Coras47c40e22018-11-26 17:01:36 -08002010 if (except_map && sid < minbits)
2011 clib_bitmap_set_no_check (except_map, sid, 1);
2012 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002013 }
2014
Sirshak Das28aa5392019-02-05 01:33:33 -06002015 rv = svm_fifo_is_full_prod (session->tx_fifo);
Florin Coras54693d22018-07-17 10:46:29 -07002016 if (!rv)
2017 {
David Johnsond9818dd2018-12-14 14:53:41 -05002018 clib_bitmap_set_no_check ((uword*)write_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002019 bits_set++;
2020 }
Florin Coras294afe22019-01-07 17:49:17 -08002021 else
Florin Coras1bcad5c2019-01-09 20:04:38 -08002022 svm_fifo_add_want_tx_ntf (session->tx_fifo, SVM_FIFO_WANT_TX_NOTIF);
Florin Coras54693d22018-07-17 10:46:29 -07002023 }));
2024
2025check_rd:
2026 if (!read_map)
2027 goto check_mq;
Florin Coras99368312018-08-02 10:45:44 -07002028
Florin Coras134a9962018-08-28 11:32:04 -07002029 clib_bitmap_foreach (sid, wrk->rd_bitmap, ({
2030 if (!(session = vcl_session_get (wrk, sid)))
Florin Coras54693d22018-07-17 10:46:29 -07002031 {
Florin Coras47c40e22018-11-26 17:01:36 -08002032 if (except_map && sid < minbits)
2033 clib_bitmap_set_no_check (except_map, sid, 1);
2034 continue;
Florin Coras54693d22018-07-17 10:46:29 -07002035 }
2036
Florin Coras0ef8ef22019-01-18 08:37:13 -08002037 rv = vcl_session_read_ready (session);
Florin Coras54693d22018-07-17 10:46:29 -07002038 if (rv)
2039 {
David Johnsond9818dd2018-12-14 14:53:41 -05002040 clib_bitmap_set_no_check ((uword*)read_map, sid, 1);
Florin Coras54693d22018-07-17 10:46:29 -07002041 bits_set++;
2042 }
2043 }));
2044 /* *INDENT-ON* */
2045
2046check_mq:
Dave Wallace543852a2017-08-03 02:11:34 -04002047
Florin Coras86f04502018-09-12 16:08:01 -07002048 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2049 {
2050 vcl_select_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i], n_bits,
2051 read_map, write_map, except_map, &bits_set);
2052 }
2053 vec_reset_length (wrk->unhandled_evts_vector);
2054
Florin Coras99368312018-08-02 10:45:44 -07002055 if (vcm->cfg.use_mq_eventfd)
Florin Coras134a9962018-08-28 11:32:04 -07002056 vppcom_select_eventfd (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002057 time_to_wait, &bits_set);
2058 else
Florin Coras134a9962018-08-28 11:32:04 -07002059 vppcom_select_condvar (wrk, n_bits, read_map, write_map, except_map,
Florin Coras99368312018-08-02 10:45:44 -07002060 time_to_wait, &bits_set);
Florin Coras54693d22018-07-17 10:46:29 -07002061
Dave Wallace543852a2017-08-03 02:11:34 -04002062 return (bits_set);
2063}
2064
Dave Wallacef7f809c2017-10-03 01:48:42 -04002065static inline void
Florin Coras134a9962018-08-28 11:32:04 -07002066vep_verify_epoll_chain (vcl_worker_t * wrk, u32 vep_idx)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002067{
Florin Coras7e12d942018-06-27 14:32:43 -07002068 vcl_session_t *session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002069 vppcom_epoll_t *vep;
Dave Wallace498b3a52017-11-09 13:00:34 -05002070 u32 sid = vep_idx;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002071
Florin Corasc0737e92019-03-04 14:19:39 -08002072 if (VPPCOM_DEBUG <= 2)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002073 return;
2074
Florin Coras134a9962018-08-28 11:32:04 -07002075 session = vcl_session_get (wrk, vep_idx);
Florin Coras070453d2018-08-24 17:04:27 -07002076 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002077 {
Florin Coras5e062572019-03-14 19:07:51 -07002078 VDBG (0, "ERROR: Invalid vep_idx (%u)!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002079 goto done;
2080 }
2081 if (PREDICT_FALSE (!session->is_vep))
2082 {
Florin Coras5e062572019-03-14 19:07:51 -07002083 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002084 goto done;
2085 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002086 vep = &session->vep;
Florin Coras5e062572019-03-14 19:07:51 -07002087 VDBG (0, "vep_idx (%u): Dumping epoll chain\n"
2088 "{\n"
2089 " is_vep = %u\n"
2090 " is_vep_session = %u\n"
2091 " next_sid = 0x%x (%u)\n"
2092 "}\n", vep_idx, session->is_vep, session->is_vep_session,
2093 vep->next_sh, vep->next_sh);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002094
Florin Coras134a9962018-08-28 11:32:04 -07002095 for (sid = vep->next_sh; sid != ~0; sid = vep->next_sh)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002096 {
Florin Coras134a9962018-08-28 11:32:04 -07002097 session = vcl_session_get (wrk, sid);
Florin Coras070453d2018-08-24 17:04:27 -07002098 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002099 {
Florin Coras5e062572019-03-14 19:07:51 -07002100 VDBG (0, "ERROR: Invalid sid (%u)!", sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002101 goto done;
2102 }
2103 if (PREDICT_FALSE (session->is_vep))
Florin Coras5e062572019-03-14 19:07:51 -07002104 {
2105 VDBG (0, "ERROR: sid (%u) is a vep!", vep_idx);
2106 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002107 else if (PREDICT_FALSE (!session->is_vep_session))
2108 {
Florin Coras5e062572019-03-14 19:07:51 -07002109 VDBG (0, "ERROR: session (%u) is not a vep session!", sid);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002110 goto done;
2111 }
2112 vep = &session->vep;
Florin Coras134a9962018-08-28 11:32:04 -07002113 if (PREDICT_FALSE (vep->vep_sh != vep_idx))
Florin Coras5e062572019-03-14 19:07:51 -07002114 VDBG (0, "ERROR: session (%u) vep_idx (%u) != vep_idx (%u)!",
2115 sid, session->vep.vep_sh, vep_idx);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002116 if (session->is_vep_session)
2117 {
Florin Coras5e062572019-03-14 19:07:51 -07002118 VDBG (0, "vep_idx[%u]: sid 0x%x (%u)\n"
2119 "{\n"
2120 " next_sid = 0x%x (%u)\n"
2121 " prev_sid = 0x%x (%u)\n"
2122 " vep_idx = 0x%x (%u)\n"
2123 " ev.events = 0x%x\n"
2124 " ev.data.u64 = 0x%llx\n"
2125 " et_mask = 0x%x\n"
2126 "}\n",
2127 vep_idx, sid, sid, vep->next_sh, vep->next_sh, vep->prev_sh,
2128 vep->prev_sh, vep->vep_sh, vep->vep_sh, vep->ev.events,
2129 vep->ev.data.u64, vep->et_mask);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002130 }
2131 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002132
2133done:
Florin Coras5e062572019-03-14 19:07:51 -07002134 VDBG (0, "vep_idx (%u): Dump complete!\n", vep_idx);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002135}
2136
2137int
2138vppcom_epoll_create (void)
2139{
Florin Coras134a9962018-08-28 11:32:04 -07002140 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002141 vcl_session_t *vep_session;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002142
Florin Coras134a9962018-08-28 11:32:04 -07002143 vep_session = vcl_session_alloc (wrk);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002144
2145 vep_session->is_vep = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002146 vep_session->vep.vep_sh = ~0;
2147 vep_session->vep.next_sh = ~0;
2148 vep_session->vep.prev_sh = ~0;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002149 vep_session->vpp_handle = ~0;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002150
Florin Corasa7a1a222018-12-30 17:11:31 -08002151 vcl_evt (VCL_EVT_EPOLL_CREATE, vep_session, vep_session->session_index);
2152 VDBG (0, "Created vep_idx %u", vep_session->session_index);
Keith Burns (alagalah)09b27842018-01-05 14:39:59 -08002153
Florin Corasab2f6db2018-08-31 14:31:41 -07002154 return vcl_session_handle (vep_session);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002155}
2156
2157int
Florin Coras134a9962018-08-28 11:32:04 -07002158vppcom_epoll_ctl (uint32_t vep_handle, int op, uint32_t session_handle,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002159 struct epoll_event *event)
2160{
Florin Coras134a9962018-08-28 11:32:04 -07002161 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002162 vcl_session_t *vep_session;
2163 vcl_session_t *session;
Florin Coras070453d2018-08-24 17:04:27 -07002164 int rv = VPPCOM_OK;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002165
Florin Coras134a9962018-08-28 11:32:04 -07002166 if (vep_handle == session_handle)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002167 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002168 VDBG (0, "vep_sh == session handle (%u)!", vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002169 return VPPCOM_EINVAL;
2170 }
2171
Florin Coras134a9962018-08-28 11:32:04 -07002172 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002173 if (PREDICT_FALSE (!vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002174 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002175 VDBG (0, "Invalid vep_sh (%u)!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002176 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002177 }
2178 if (PREDICT_FALSE (!vep_session->is_vep))
2179 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002180 VDBG (0, "vep_sh (%u) is not a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002181 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002182 }
2183
Florin Coras134a9962018-08-28 11:32:04 -07002184 ASSERT (vep_session->vep.vep_sh == ~0);
2185 ASSERT (vep_session->vep.prev_sh == ~0);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002186
Florin Coras134a9962018-08-28 11:32:04 -07002187 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002188 if (PREDICT_FALSE (!session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002189 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002190 VDBG (0, "Invalid session_handle (%u)!", session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002191 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002192 }
2193 if (PREDICT_FALSE (session->is_vep))
2194 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002195 VDBG (0, "session_handle (%u) is a vep!", vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002196 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002197 }
2198
2199 switch (op)
2200 {
2201 case EPOLL_CTL_ADD:
2202 if (PREDICT_FALSE (!event))
2203 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002204 VDBG (0, "EPOLL_CTL_ADD: NULL pointer to epoll_event structure!");
Florin Coras070453d2018-08-24 17:04:27 -07002205 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002206 }
Florin Coras134a9962018-08-28 11:32:04 -07002207 if (vep_session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002208 {
Florin Coras7e12d942018-06-27 14:32:43 -07002209 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002210 next_session = vcl_session_get_w_handle (wrk,
2211 vep_session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002212 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002213 {
Florin Coras5e062572019-03-14 19:07:51 -07002214 VDBG (0, "EPOLL_CTL_ADD: Invalid vep.next_sh (%u) on "
Florin Corasa7a1a222018-12-30 17:11:31 -08002215 "vep_idx (%u)!", vep_session->vep.next_sh, vep_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002216 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002217 }
Florin Coras134a9962018-08-28 11:32:04 -07002218 ASSERT (next_session->vep.prev_sh == vep_handle);
2219 next_session->vep.prev_sh = session_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002220 }
Florin Coras134a9962018-08-28 11:32:04 -07002221 session->vep.next_sh = vep_session->vep.next_sh;
2222 session->vep.prev_sh = vep_handle;
2223 session->vep.vep_sh = vep_handle;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002224 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2225 session->vep.ev = *event;
Dave Wallace4878cbe2017-11-21 03:45:09 -05002226 session->is_vep = 0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002227 session->is_vep_session = 1;
Florin Coras134a9962018-08-28 11:32:04 -07002228 vep_session->vep.next_sh = session_handle;
Keith Burns (alagalah)3cf2d642018-02-23 10:17:01 -08002229
Florin Coras1bcad5c2019-01-09 20:04:38 -08002230 if (session->tx_fifo)
2231 svm_fifo_add_want_tx_ntf (session->tx_fifo,
2232 SVM_FIFO_WANT_TX_NOTIF_IF_FULL);
2233
Florin Corasa7a1a222018-12-30 17:11:31 -08002234 VDBG (1, "EPOLL_CTL_ADD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2235 vep_handle, session_handle, event->events, event->data.u64);
Florin Coras0d427d82018-06-27 03:24:07 -07002236 vcl_evt (VCL_EVT_EPOLL_CTLADD, session, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002237 break;
2238
2239 case EPOLL_CTL_MOD:
2240 if (PREDICT_FALSE (!event))
2241 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002242 VDBG (0, "EPOLL_CTL_MOD: NULL pointer to epoll_event structure!");
Dave Wallacef7f809c2017-10-03 01:48:42 -04002243 rv = VPPCOM_EINVAL;
2244 goto done;
2245 }
Dave Wallace4878cbe2017-11-21 03:45:09 -05002246 else if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002247 {
Florin Coras5e062572019-03-14 19:07:51 -07002248 VDBG (0, "sh %u EPOLL_CTL_MOD: not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002249 rv = VPPCOM_EINVAL;
2250 goto done;
2251 }
Florin Coras134a9962018-08-28 11:32:04 -07002252 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002253 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002254 VDBG (0, "EPOLL_CTL_MOD: sh %u vep_sh (%u) != vep_sh (%u)!",
2255 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002256 rv = VPPCOM_EINVAL;
2257 goto done;
2258 }
2259 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2260 session->vep.ev = *event;
Florin Corasa7a1a222018-12-30 17:11:31 -08002261 VDBG (1, "EPOLL_CTL_MOD: vep_sh %u, sh %u, events 0x%x, data 0x%llx!",
2262 vep_handle, session_handle, event->events, event->data.u64);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002263 break;
2264
2265 case EPOLL_CTL_DEL:
Dave Wallace4878cbe2017-11-21 03:45:09 -05002266 if (PREDICT_FALSE (!session->is_vep_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002267 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002268 VDBG (0, "EPOLL_CTL_DEL: %u not a vep session!", session_handle);
Dave Wallace4878cbe2017-11-21 03:45:09 -05002269 rv = VPPCOM_EINVAL;
2270 goto done;
2271 }
Florin Coras134a9962018-08-28 11:32:04 -07002272 else if (PREDICT_FALSE (session->vep.vep_sh != vep_handle))
Dave Wallace4878cbe2017-11-21 03:45:09 -05002273 {
Florin Corasa7a1a222018-12-30 17:11:31 -08002274 VDBG (0, "EPOLL_CTL_DEL: sh %u vep_sh (%u) != vep_sh (%u)!",
2275 session_handle, session->vep.vep_sh, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002276 rv = VPPCOM_EINVAL;
2277 goto done;
2278 }
2279
Florin Coras134a9962018-08-28 11:32:04 -07002280 if (session->vep.prev_sh == vep_handle)
2281 vep_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002282 else
2283 {
Florin Coras7e12d942018-06-27 14:32:43 -07002284 vcl_session_t *prev_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002285 prev_session = vcl_session_get_w_handle (wrk, session->vep.prev_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002286 if (PREDICT_FALSE (!prev_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002287 {
Florin Coras5e062572019-03-14 19:07:51 -07002288 VDBG (0, "EPOLL_CTL_DEL: Invalid prev_sh (%u) on sh (%u)!",
Florin Corasa7a1a222018-12-30 17:11:31 -08002289 session->vep.prev_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002290 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002291 }
Florin Coras134a9962018-08-28 11:32:04 -07002292 ASSERT (prev_session->vep.next_sh == session_handle);
2293 prev_session->vep.next_sh = session->vep.next_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002294 }
Florin Coras134a9962018-08-28 11:32:04 -07002295 if (session->vep.next_sh != ~0)
Dave Wallacef7f809c2017-10-03 01:48:42 -04002296 {
Florin Coras7e12d942018-06-27 14:32:43 -07002297 vcl_session_t *next_session;
Florin Corasab2f6db2018-08-31 14:31:41 -07002298 next_session = vcl_session_get_w_handle (wrk, session->vep.next_sh);
Florin Coras070453d2018-08-24 17:04:27 -07002299 if (PREDICT_FALSE (!next_session))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002300 {
Florin Coras5e062572019-03-14 19:07:51 -07002301 VDBG (0, "EPOLL_CTL_DEL: Invalid next_sh (%u) on sh (%u)!",
Florin Corasa7a1a222018-12-30 17:11:31 -08002302 session->vep.next_sh, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002303 return VPPCOM_EBADFD;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002304 }
Florin Coras134a9962018-08-28 11:32:04 -07002305 ASSERT (next_session->vep.prev_sh == session_handle);
2306 next_session->vep.prev_sh = session->vep.prev_sh;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002307 }
2308
2309 memset (&session->vep, 0, sizeof (session->vep));
Florin Coras134a9962018-08-28 11:32:04 -07002310 session->vep.next_sh = ~0;
2311 session->vep.prev_sh = ~0;
2312 session->vep.vep_sh = ~0;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002313 session->is_vep_session = 0;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002314
2315 if (session->tx_fifo)
2316 svm_fifo_del_want_tx_ntf (session->tx_fifo, SVM_FIFO_NO_TX_NOTIF);
2317
Florin Coras5e062572019-03-14 19:07:51 -07002318 VDBG (1, "EPOLL_CTL_DEL: vep_idx %u, sh %u!", vep_handle,
Florin Corasa7a1a222018-12-30 17:11:31 -08002319 session_handle);
Florin Coras134a9962018-08-28 11:32:04 -07002320 vcl_evt (VCL_EVT_EPOLL_CTLDEL, session, vep_sh);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002321 break;
2322
2323 default:
Florin Corasa7a1a222018-12-30 17:11:31 -08002324 VDBG (0, "Invalid operation (%d)!", op);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002325 rv = VPPCOM_EINVAL;
2326 }
2327
Florin Coras134a9962018-08-28 11:32:04 -07002328 vep_verify_epoll_chain (wrk, vep_handle);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002329
2330done:
Dave Wallacef7f809c2017-10-03 01:48:42 -04002331 return rv;
2332}
2333
Florin Coras86f04502018-09-12 16:08:01 -07002334static inline void
2335vcl_epoll_wait_handle_mq_event (vcl_worker_t * wrk, session_event_t * e,
2336 struct epoll_event *events, u32 * num_ev)
Florin Coras54693d22018-07-17 10:46:29 -07002337{
2338 session_disconnected_msg_t *disconnected_msg;
2339 session_connected_msg_t *connected_msg;
Florin Coras99368312018-08-02 10:45:44 -07002340 u32 sid = ~0, session_events;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002341 u64 session_evt_data = ~0;
Florin Coras54693d22018-07-17 10:46:29 -07002342 vcl_session_t *session;
Florin Coras86f04502018-09-12 16:08:01 -07002343 u8 add_event = 0;
2344
2345 switch (e->event_type)
2346 {
Florin Coras653e43f2019-03-04 10:56:23 -08002347 case SESSION_IO_EVT_RX:
Florin Corasc0737e92019-03-04 14:19:39 -08002348 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002349 if (!(session = vcl_session_get (wrk, sid)))
2350 break;
Florin Corasc0737e92019-03-04 14:19:39 -08002351 vcl_fifo_rx_evt_valid_or_break (session);
Florin Coras86f04502018-09-12 16:08:01 -07002352 session_events = session->vep.ev.events;
Florin Corasaa27eb92018-10-13 12:20:01 -07002353 if (!(EPOLLIN & session->vep.ev.events) || session->has_rx_evt)
Florin Coras86f04502018-09-12 16:08:01 -07002354 break;
2355 add_event = 1;
2356 events[*num_ev].events |= EPOLLIN;
2357 session_evt_data = session->vep.ev.data.u64;
Florin Corasaa27eb92018-10-13 12:20:01 -07002358 session->has_rx_evt = 1;
Florin Coras86f04502018-09-12 16:08:01 -07002359 break;
Florin Coras653e43f2019-03-04 10:56:23 -08002360 case SESSION_IO_EVT_TX:
Florin Corasc0737e92019-03-04 14:19:39 -08002361 sid = e->session_index;
Florin Corasfa915f82018-12-26 16:29:06 -08002362 if (!(session = vcl_session_get (wrk, sid)))
2363 break;
Florin Coras86f04502018-09-12 16:08:01 -07002364 session_events = session->vep.ev.events;
2365 if (!(EPOLLOUT & session_events))
2366 break;
2367 add_event = 1;
2368 events[*num_ev].events |= EPOLLOUT;
2369 session_evt_data = session->vep.ev.data.u64;
Florin Coras1bcad5c2019-01-09 20:04:38 -08002370 svm_fifo_reset_tx_ntf (session->tx_fifo);
Florin Coras86f04502018-09-12 16:08:01 -07002371 break;
Florin Coras86f04502018-09-12 16:08:01 -07002372 case SESSION_CTRL_EVT_ACCEPTED:
Florin Coras3c7d4f92018-12-14 11:28:43 -08002373 session = vcl_session_accepted (wrk,
2374 (session_accepted_msg_t *) e->data);
Florin Coras86f04502018-09-12 16:08:01 -07002375 if (!session)
Florin Coras3c7d4f92018-12-14 11:28:43 -08002376 break;
Florin Coras86f04502018-09-12 16:08:01 -07002377
Florin Coras86f04502018-09-12 16:08:01 -07002378 session_events = session->vep.ev.events;
2379 if (!(EPOLLIN & session_events))
2380 break;
2381
2382 add_event = 1;
2383 events[*num_ev].events |= EPOLLIN;
2384 session_evt_data = session->vep.ev.data.u64;
2385 break;
2386 case SESSION_CTRL_EVT_CONNECTED:
2387 connected_msg = (session_connected_msg_t *) e->data;
2388 vcl_session_connected_handler (wrk, connected_msg);
2389 /* Generate EPOLLOUT because there's no connected event */
2390 sid = vcl_session_index_from_vpp_handle (wrk, connected_msg->handle);
Florin Corasfa915f82018-12-26 16:29:06 -08002391 if (!(session = vcl_session_get (wrk, sid)))
2392 break;
Florin Coras86f04502018-09-12 16:08:01 -07002393 session_events = session->vep.ev.events;
Florin Coras72f77822019-01-22 19:05:52 -08002394 if (!(EPOLLOUT & session_events))
2395 break;
2396 add_event = 1;
2397 events[*num_ev].events |= EPOLLOUT;
2398 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002399 break;
2400 case SESSION_CTRL_EVT_DISCONNECTED:
2401 disconnected_msg = (session_disconnected_msg_t *) e->data;
Florin Coras3c7d4f92018-12-14 11:28:43 -08002402 session = vcl_session_disconnected_handler (wrk, disconnected_msg);
2403 if (!session)
Florin Coras86f04502018-09-12 16:08:01 -07002404 break;
Florin Coras72f77822019-01-22 19:05:52 -08002405 session_events = session->vep.ev.events;
2406 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2407 break;
Florin Coras86f04502018-09-12 16:08:01 -07002408 add_event = 1;
2409 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2410 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002411 break;
2412 case SESSION_CTRL_EVT_RESET:
2413 sid = vcl_session_reset_handler (wrk, (session_reset_msg_t *) e->data);
2414 if (!(session = vcl_session_get (wrk, sid)))
2415 break;
Florin Coras72f77822019-01-22 19:05:52 -08002416 session_events = session->vep.ev.events;
2417 if (!((EPOLLHUP | EPOLLRDHUP) & session_events))
2418 break;
Florin Coras86f04502018-09-12 16:08:01 -07002419 add_event = 1;
2420 events[*num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2421 session_evt_data = session->vep.ev.data.u64;
Florin Coras86f04502018-09-12 16:08:01 -07002422 break;
Florin Corasdfae9f92019-02-20 19:48:31 -08002423 case SESSION_CTRL_EVT_UNLISTEN_REPLY:
2424 vcl_session_unlisten_reply_handler (wrk, e->data);
2425 break;
Florin Coras30e79c22019-01-02 19:31:22 -08002426 case SESSION_CTRL_EVT_REQ_WORKER_UPDATE:
2427 vcl_session_req_worker_update_handler (wrk, e->data);
2428 break;
2429 case SESSION_CTRL_EVT_WORKER_UPDATE_REPLY:
2430 vcl_session_worker_update_reply_handler (wrk, e->data);
2431 break;
Florin Coras86f04502018-09-12 16:08:01 -07002432 default:
2433 VDBG (0, "unhandled: %u", e->event_type);
2434 break;
2435 }
2436
2437 if (add_event)
2438 {
2439 events[*num_ev].data.u64 = session_evt_data;
2440 if (EPOLLONESHOT & session_events)
2441 {
2442 session = vcl_session_get (wrk, sid);
2443 session->vep.ev.events = 0;
2444 }
2445 *num_ev += 1;
2446 }
2447}
2448
2449static int
2450vcl_epoll_wait_handle_mq (vcl_worker_t * wrk, svm_msg_q_t * mq,
2451 struct epoll_event *events, u32 maxevents,
2452 double wait_for_time, u32 * num_ev)
2453{
Florin Coras99368312018-08-02 10:45:44 -07002454 svm_msg_q_msg_t *msg;
Florin Coras54693d22018-07-17 10:46:29 -07002455 session_event_t *e;
Florin Coras54693d22018-07-17 10:46:29 -07002456 int i;
2457
Florin Coras539663c2018-09-28 14:59:37 -07002458 if (vec_len (wrk->mq_msg_vector) && svm_msg_q_is_empty (mq))
2459 goto handle_dequeued;
2460
Florin Coras54693d22018-07-17 10:46:29 -07002461 svm_msg_q_lock (mq);
2462 if (svm_msg_q_is_empty (mq))
2463 {
2464 if (!wait_for_time)
2465 {
2466 svm_msg_q_unlock (mq);
2467 return 0;
2468 }
2469 else if (wait_for_time < 0)
2470 {
2471 svm_msg_q_wait (mq);
2472 }
2473 else
2474 {
Florin Coras60f1fc12018-08-16 14:57:31 -07002475 if (svm_msg_q_timedwait (mq, wait_for_time / 1e3))
Florin Coras54693d22018-07-17 10:46:29 -07002476 {
2477 svm_msg_q_unlock (mq);
2478 return 0;
2479 }
2480 }
2481 }
Florin Coras134a9962018-08-28 11:32:04 -07002482 vcl_mq_dequeue_batch (wrk, mq);
Florin Coras54693d22018-07-17 10:46:29 -07002483 svm_msg_q_unlock (mq);
2484
Florin Coras539663c2018-09-28 14:59:37 -07002485handle_dequeued:
Florin Coras134a9962018-08-28 11:32:04 -07002486 for (i = 0; i < vec_len (wrk->mq_msg_vector); i++)
Florin Coras54693d22018-07-17 10:46:29 -07002487 {
Florin Coras134a9962018-08-28 11:32:04 -07002488 msg = vec_elt_at_index (wrk->mq_msg_vector, i);
Florin Coras99368312018-08-02 10:45:44 -07002489 e = svm_msg_q_msg_data (mq, msg);
Florin Corasaa27eb92018-10-13 12:20:01 -07002490 if (*num_ev < maxevents)
2491 vcl_epoll_wait_handle_mq_event (wrk, e, events, num_ev);
2492 else
2493 vec_add1 (wrk->unhandled_evts_vector, *e);
Florin Coras99368312018-08-02 10:45:44 -07002494 svm_msg_q_free_msg (mq, msg);
Florin Coras54693d22018-07-17 10:46:29 -07002495 }
Florin Corasaa27eb92018-10-13 12:20:01 -07002496 vec_reset_length (wrk->mq_msg_vector);
Florin Coras30e79c22019-01-02 19:31:22 -08002497 vcl_handle_pending_wrk_updates (wrk);
Florin Coras54693d22018-07-17 10:46:29 -07002498 return *num_ev;
2499}
2500
Florin Coras99368312018-08-02 10:45:44 -07002501static int
Florin Coras134a9962018-08-28 11:32:04 -07002502vppcom_epoll_wait_condvar (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002503 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002504{
Florin Coras14ed6df2019-03-06 21:13:42 -08002505 double wait = 0, start = 0;
2506
2507 if (!n_evts)
2508 {
2509 wait = wait_for_time;
2510 start = clib_time_now (&wrk->clib_time);
2511 }
2512
2513 do
2514 {
2515 vcl_epoll_wait_handle_mq (wrk, wrk->app_event_queue, events, maxevents,
2516 wait, &n_evts);
2517 if (n_evts)
2518 return n_evts;
2519 if (wait == -1)
2520 continue;
2521
2522 wait = wait - (clib_time_now (&wrk->clib_time) - start);
2523 }
2524 while (wait > 0);
2525
2526 return 0;
Florin Coras99368312018-08-02 10:45:44 -07002527}
2528
2529static int
Florin Coras134a9962018-08-28 11:32:04 -07002530vppcom_epoll_wait_eventfd (vcl_worker_t * wrk, struct epoll_event *events,
Florin Coras86f04502018-09-12 16:08:01 -07002531 int maxevents, u32 n_evts, double wait_for_time)
Florin Coras99368312018-08-02 10:45:44 -07002532{
2533 vcl_mq_evt_conn_t *mqc;
2534 int __clib_unused n_read;
2535 int n_mq_evts, i;
Florin Coras99368312018-08-02 10:45:44 -07002536 u64 buf;
2537
Florin Coras134a9962018-08-28 11:32:04 -07002538 vec_validate (wrk->mq_events, pool_elts (wrk->mq_evt_conns));
Florin Corasa4878ed2018-10-12 13:09:36 -07002539again:
Florin Coras134a9962018-08-28 11:32:04 -07002540 n_mq_evts = epoll_wait (wrk->mqs_epfd, wrk->mq_events,
2541 vec_len (wrk->mq_events), wait_for_time);
Florin Coras99368312018-08-02 10:45:44 -07002542 for (i = 0; i < n_mq_evts; i++)
2543 {
Florin Coras134a9962018-08-28 11:32:04 -07002544 mqc = vcl_mq_evt_conn_get (wrk, wrk->mq_events[i].data.u32);
Florin Coras99368312018-08-02 10:45:44 -07002545 n_read = read (mqc->mq_fd, &buf, sizeof (buf));
Florin Coras134a9962018-08-28 11:32:04 -07002546 vcl_epoll_wait_handle_mq (wrk, mqc->mq, events, maxevents, 0, &n_evts);
Florin Coras99368312018-08-02 10:45:44 -07002547 }
Florin Corasa4878ed2018-10-12 13:09:36 -07002548 if (!n_evts && n_mq_evts > 0)
2549 goto again;
Florin Coras99368312018-08-02 10:45:44 -07002550
2551 return (int) n_evts;
2552}
2553
Dave Wallacef7f809c2017-10-03 01:48:42 -04002554int
Florin Coras134a9962018-08-28 11:32:04 -07002555vppcom_epoll_wait (uint32_t vep_handle, struct epoll_event *events,
Dave Wallacef7f809c2017-10-03 01:48:42 -04002556 int maxevents, double wait_for_time)
2557{
Florin Coras134a9962018-08-28 11:32:04 -07002558 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002559 vcl_session_t *vep_session;
Florin Coras86f04502018-09-12 16:08:01 -07002560 u32 n_evts = 0;
2561 int i;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002562
2563 if (PREDICT_FALSE (maxevents <= 0))
2564 {
Florin Coras5e062572019-03-14 19:07:51 -07002565 VDBG (0, "ERROR: Invalid maxevents (%d)!", maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002566 return VPPCOM_EINVAL;
2567 }
Dave Wallacef7f809c2017-10-03 01:48:42 -04002568
Florin Coras134a9962018-08-28 11:32:04 -07002569 vep_session = vcl_session_get_w_handle (wrk, vep_handle);
Florin Coras14598772018-09-04 19:47:52 -07002570 if (!vep_session)
2571 return VPPCOM_EBADFD;
2572
Florin Coras54693d22018-07-17 10:46:29 -07002573 if (PREDICT_FALSE (!vep_session->is_vep))
Dave Wallacef7f809c2017-10-03 01:48:42 -04002574 {
Florin Coras5e062572019-03-14 19:07:51 -07002575 VDBG (0, "ERROR: vep_idx (%u) is not a vep!", vep_handle);
Florin Coras54693d22018-07-17 10:46:29 -07002576 return VPPCOM_EINVAL;
Dave Wallacef7f809c2017-10-03 01:48:42 -04002577 }
Florin Coras54693d22018-07-17 10:46:29 -07002578
2579 memset (events, 0, sizeof (*events) * maxevents);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002580
Florin Coras86f04502018-09-12 16:08:01 -07002581 if (vec_len (wrk->unhandled_evts_vector))
2582 {
2583 for (i = 0; i < vec_len (wrk->unhandled_evts_vector); i++)
2584 {
2585 vcl_epoll_wait_handle_mq_event (wrk, &wrk->unhandled_evts_vector[i],
2586 events, &n_evts);
2587 if (n_evts == maxevents)
2588 {
2589 i += 1;
2590 break;
2591 }
2592 }
Florin Coras86f04502018-09-12 16:08:01 -07002593 vec_delete (wrk->unhandled_evts_vector, i, 0);
2594 }
2595
2596 if (vcm->cfg.use_mq_eventfd)
2597 return vppcom_epoll_wait_eventfd (wrk, events, maxevents, n_evts,
2598 wait_for_time);
2599
2600 return vppcom_epoll_wait_condvar (wrk, events, maxevents, n_evts,
2601 wait_for_time);
Dave Wallacef7f809c2017-10-03 01:48:42 -04002602}
2603
Dave Wallace35830af2017-10-09 01:43:42 -04002604int
Florin Coras134a9962018-08-28 11:32:04 -07002605vppcom_session_attr (uint32_t session_handle, uint32_t op,
Dave Wallace35830af2017-10-09 01:43:42 -04002606 void *buffer, uint32_t * buflen)
2607{
Florin Coras134a9962018-08-28 11:32:04 -07002608 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras7e12d942018-06-27 14:32:43 -07002609 vcl_session_t *session;
Dave Wallace35830af2017-10-09 01:43:42 -04002610 int rv = VPPCOM_OK;
Florin Coras7baeb712019-01-04 17:05:43 -08002611 u32 *flags = buffer, tmp_flags = 0;
Steven2199aab2017-10-15 20:18:47 -07002612 vppcom_endpt_t *ep = buffer;
Dave Wallace35830af2017-10-09 01:43:42 -04002613
Florin Coras134a9962018-08-28 11:32:04 -07002614 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07002615 if (!session)
2616 return VPPCOM_EBADFD;
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002617
Dave Wallace35830af2017-10-09 01:43:42 -04002618 switch (op)
2619 {
2620 case VPPCOM_ATTR_GET_NREAD:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002621 rv = vcl_session_read_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07002622 VDBG (2, "VPPCOM_ATTR_GET_NREAD: sh %u, nread = %d", session_handle,
2623 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002624 break;
2625
Dave Wallace227867f2017-11-13 21:21:53 -05002626 case VPPCOM_ATTR_GET_NWRITE:
Florin Coras0ef8ef22019-01-18 08:37:13 -08002627 rv = vcl_session_write_ready (session);
Florin Coras5e062572019-03-14 19:07:51 -07002628 VDBG (2, "VPPCOM_ATTR_GET_NWRITE: sh %u, nwrite = %d", session_handle,
2629 rv);
Dave Wallace35830af2017-10-09 01:43:42 -04002630 break;
2631
2632 case VPPCOM_ATTR_GET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002633 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002634 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002635 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
2636 VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002637 *buflen = sizeof (*flags);
Florin Coras5e062572019-03-14 19:07:51 -07002638 VDBG (2, "VPPCOM_ATTR_GET_FLAGS: sh %u, flags = 0x%08x, "
2639 "is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002640 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002641 }
2642 else
2643 rv = VPPCOM_EINVAL;
2644 break;
2645
2646 case VPPCOM_ATTR_SET_FLAGS:
Dave Wallace048b1d62018-01-03 22:24:41 -05002647 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
Dave Wallace35830af2017-10-09 01:43:42 -04002648 {
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002649 if (*flags & O_NONBLOCK)
2650 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2651 else
2652 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2653
Florin Coras5e062572019-03-14 19:07:51 -07002654 VDBG (2, "VPPCOM_ATTR_SET_FLAGS: sh %u, flags = 0x%08x,"
2655 " is_nonblocking = %u", session_handle, *flags,
Florin Coras0d427d82018-06-27 03:24:07 -07002656 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK));
Dave Wallace35830af2017-10-09 01:43:42 -04002657 }
2658 else
2659 rv = VPPCOM_EINVAL;
2660 break;
2661
2662 case VPPCOM_ATTR_GET_PEER_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002663 if (PREDICT_TRUE (buffer && buflen &&
2664 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002665 {
Florin Coras7e12d942018-06-27 14:32:43 -07002666 ep->is_ip4 = session->transport.is_ip4;
2667 ep->port = session->transport.rmt_port;
2668 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002669 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
2670 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002671 else
Dave Barach178cf492018-11-13 16:34:13 -05002672 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
2673 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002674 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07002675 VDBG (1, "VPPCOM_ATTR_GET_PEER_ADDR: sh %u, is_ip4 = %u, "
2676 "addr = %U, port %u", session_handle, ep->is_ip4,
2677 format_ip46_address, &session->transport.rmt_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002678 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2679 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002680 }
2681 else
2682 rv = VPPCOM_EINVAL;
2683 break;
2684
2685 case VPPCOM_ATTR_GET_LCL_ADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002686 if (PREDICT_TRUE (buffer && buflen &&
2687 (*buflen >= sizeof (*ep)) && ep->ip))
Dave Wallace35830af2017-10-09 01:43:42 -04002688 {
Florin Coras7e12d942018-06-27 14:32:43 -07002689 ep->is_ip4 = session->transport.is_ip4;
2690 ep->port = session->transport.lcl_port;
2691 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05002692 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip4,
2693 sizeof (ip4_address_t));
Steven2199aab2017-10-15 20:18:47 -07002694 else
Dave Barach178cf492018-11-13 16:34:13 -05002695 clib_memcpy_fast (ep->ip, &session->transport.lcl_ip.ip6,
2696 sizeof (ip6_address_t));
Steven2199aab2017-10-15 20:18:47 -07002697 *buflen = sizeof (*ep);
Florin Coras5e062572019-03-14 19:07:51 -07002698 VDBG (1, "VPPCOM_ATTR_GET_LCL_ADDR: sh %u, is_ip4 = %u, addr = %U"
2699 " port %d", session_handle, ep->is_ip4, format_ip46_address,
Florin Coras7e12d942018-06-27 14:32:43 -07002700 &session->transport.lcl_ip,
Florin Coras0d427d82018-06-27 03:24:07 -07002701 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2702 clib_net_to_host_u16 (ep->port));
Dave Wallace35830af2017-10-09 01:43:42 -04002703 }
2704 else
2705 rv = VPPCOM_EINVAL;
2706 break;
Stevenb5a11602017-10-11 09:59:30 -07002707
Dave Wallace048b1d62018-01-03 22:24:41 -05002708 case VPPCOM_ATTR_GET_LIBC_EPFD:
2709 rv = session->libc_epfd;
Florin Coras5e062572019-03-14 19:07:51 -07002710 VDBG (2, "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d", rv);
Dave Wallace048b1d62018-01-03 22:24:41 -05002711 break;
2712
2713 case VPPCOM_ATTR_SET_LIBC_EPFD:
2714 if (PREDICT_TRUE (buffer && buflen &&
2715 (*buflen == sizeof (session->libc_epfd))))
2716 {
2717 session->libc_epfd = *(int *) buffer;
2718 *buflen = sizeof (session->libc_epfd);
2719
Florin Coras5e062572019-03-14 19:07:51 -07002720 VDBG (2, "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, buflen %d",
2721 session->libc_epfd, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002722 }
2723 else
2724 rv = VPPCOM_EINVAL;
2725 break;
2726
2727 case VPPCOM_ATTR_GET_PROTOCOL:
2728 if (buffer && buflen && (*buflen >= sizeof (int)))
2729 {
Florin Coras7e12d942018-06-27 14:32:43 -07002730 *(int *) buffer = session->session_type;
Dave Wallace048b1d62018-01-03 22:24:41 -05002731 *buflen = sizeof (int);
2732
Florin Coras5e062572019-03-14 19:07:51 -07002733 VDBG (2, "VPPCOM_ATTR_GET_PROTOCOL: %d (%s), buflen %d",
2734 *(int *) buffer, *(int *) buffer ? "UDP" : "TCP", *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002735 }
2736 else
2737 rv = VPPCOM_EINVAL;
2738 break;
2739
2740 case VPPCOM_ATTR_GET_LISTEN:
2741 if (buffer && buflen && (*buflen >= sizeof (int)))
2742 {
2743 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2744 VCL_SESS_ATTR_LISTEN);
2745 *buflen = sizeof (int);
2746
Florin Coras5e062572019-03-14 19:07:51 -07002747 VDBG (2, "VPPCOM_ATTR_GET_LISTEN: %d, buflen %d", *(int *) buffer,
2748 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002749 }
2750 else
2751 rv = VPPCOM_EINVAL;
2752 break;
2753
2754 case VPPCOM_ATTR_GET_ERROR:
2755 if (buffer && buflen && (*buflen >= sizeof (int)))
2756 {
2757 *(int *) buffer = 0;
2758 *buflen = sizeof (int);
2759
Florin Coras5e062572019-03-14 19:07:51 -07002760 VDBG (2, "VPPCOM_ATTR_GET_ERROR: %d, buflen %d, #VPP-TBD#",
2761 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002762 }
2763 else
2764 rv = VPPCOM_EINVAL;
2765 break;
2766
2767 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
2768 if (buffer && buflen && (*buflen >= sizeof (u32)))
2769 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002770
2771 /* VPP-TBD */
2772 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002773 session->tx_fifo ? session->tx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002774 vcm->cfg.tx_fifo_size);
2775 *buflen = sizeof (u32);
2776
Florin Coras5e062572019-03-14 19:07:51 -07002777 VDBG (2, "VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
2778 " #VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer,
2779 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002780 }
2781 else
2782 rv = VPPCOM_EINVAL;
2783 break;
2784
2785 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
2786 if (buffer && buflen && (*buflen == sizeof (u32)))
2787 {
2788 /* VPP-TBD */
2789 session->sndbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07002790 VDBG (2, "VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), buflen %d,"
2791 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
2792 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002793 }
2794 else
2795 rv = VPPCOM_EINVAL;
2796 break;
2797
2798 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
2799 if (buffer && buflen && (*buflen >= sizeof (u32)))
2800 {
Dave Wallace048b1d62018-01-03 22:24:41 -05002801
2802 /* VPP-TBD */
2803 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
Keith Burns (alagalah)9b793772018-02-16 08:20:56 -08002804 session->rx_fifo ? session->rx_fifo->nitems :
Dave Wallace048b1d62018-01-03 22:24:41 -05002805 vcm->cfg.rx_fifo_size);
2806 *buflen = sizeof (u32);
2807
Florin Coras5e062572019-03-14 19:07:51 -07002808 VDBG (2, "VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), buflen %d, "
2809 "#VPP-TBD#", *(size_t *) buffer, *(size_t *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002810 }
2811 else
2812 rv = VPPCOM_EINVAL;
2813 break;
2814
2815 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
2816 if (buffer && buflen && (*buflen == sizeof (u32)))
2817 {
2818 /* VPP-TBD */
2819 session->rcvbuf_size = *(u32 *) buffer;
Florin Coras5e062572019-03-14 19:07:51 -07002820 VDBG (2, "VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), buflen %d,"
2821 " #VPP-TBD#", session->sndbuf_size, session->sndbuf_size,
2822 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002823 }
2824 else
2825 rv = VPPCOM_EINVAL;
2826 break;
2827
2828 case VPPCOM_ATTR_GET_REUSEADDR:
2829 if (buffer && buflen && (*buflen >= sizeof (int)))
2830 {
2831 /* VPP-TBD */
2832 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2833 VCL_SESS_ATTR_REUSEADDR);
2834 *buflen = sizeof (int);
2835
Florin Coras5e062572019-03-14 19:07:51 -07002836 VDBG (2, "VPPCOM_ATTR_GET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
2837 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002838 }
2839 else
2840 rv = VPPCOM_EINVAL;
2841 break;
2842
Stevenb5a11602017-10-11 09:59:30 -07002843 case VPPCOM_ATTR_SET_REUSEADDR:
Dave Wallace048b1d62018-01-03 22:24:41 -05002844 if (buffer && buflen && (*buflen == sizeof (int)) &&
2845 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2846 {
2847 /* VPP-TBD */
2848 if (*(int *) buffer)
2849 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
2850 else
2851 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
2852
Florin Coras5e062572019-03-14 19:07:51 -07002853 VDBG (2, "VPPCOM_ATTR_SET_REUSEADDR: %d, buflen %d, #VPP-TBD#",
2854 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEADDR),
2855 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002856 }
2857 else
2858 rv = VPPCOM_EINVAL;
2859 break;
2860
2861 case VPPCOM_ATTR_GET_REUSEPORT:
2862 if (buffer && buflen && (*buflen >= sizeof (int)))
2863 {
2864 /* VPP-TBD */
2865 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2866 VCL_SESS_ATTR_REUSEPORT);
2867 *buflen = sizeof (int);
2868
Florin Coras5e062572019-03-14 19:07:51 -07002869 VDBG (2, "VPPCOM_ATTR_GET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
2870 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002871 }
2872 else
2873 rv = VPPCOM_EINVAL;
2874 break;
2875
2876 case VPPCOM_ATTR_SET_REUSEPORT:
2877 if (buffer && buflen && (*buflen == sizeof (int)) &&
2878 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
2879 {
2880 /* VPP-TBD */
2881 if (*(int *) buffer)
2882 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
2883 else
2884 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
2885
Florin Coras5e062572019-03-14 19:07:51 -07002886 VDBG (2, "VPPCOM_ATTR_SET_REUSEPORT: %d, buflen %d, #VPP-TBD#",
2887 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_REUSEPORT),
2888 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002889 }
2890 else
2891 rv = VPPCOM_EINVAL;
2892 break;
2893
2894 case VPPCOM_ATTR_GET_BROADCAST:
2895 if (buffer && buflen && (*buflen >= sizeof (int)))
2896 {
2897 /* VPP-TBD */
2898 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2899 VCL_SESS_ATTR_BROADCAST);
2900 *buflen = sizeof (int);
2901
Florin Coras5e062572019-03-14 19:07:51 -07002902 VDBG (2, "VPPCOM_ATTR_GET_BROADCAST: %d, buflen %d, #VPP-TBD#",
2903 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002904 }
2905 else
2906 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002907 break;
2908
2909 case VPPCOM_ATTR_SET_BROADCAST:
Dave Wallace048b1d62018-01-03 22:24:41 -05002910 if (buffer && buflen && (*buflen == sizeof (int)))
2911 {
2912 /* VPP-TBD */
2913 if (*(int *) buffer)
2914 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
2915 else
2916 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
2917
Florin Coras5e062572019-03-14 19:07:51 -07002918 VDBG (2, "VPPCOM_ATTR_SET_BROADCAST: %d, buflen %d, #VPP-TBD#",
2919 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_BROADCAST),
2920 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002921 }
2922 else
2923 rv = VPPCOM_EINVAL;
2924 break;
2925
2926 case VPPCOM_ATTR_GET_V6ONLY:
2927 if (buffer && buflen && (*buflen >= sizeof (int)))
2928 {
2929 /* VPP-TBD */
2930 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2931 VCL_SESS_ATTR_V6ONLY);
2932 *buflen = sizeof (int);
2933
Florin Coras5e062572019-03-14 19:07:51 -07002934 VDBG (2, "VPPCOM_ATTR_GET_V6ONLY: %d, buflen %d, #VPP-TBD#",
2935 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002936 }
2937 else
2938 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002939 break;
2940
2941 case VPPCOM_ATTR_SET_V6ONLY:
Dave Wallace048b1d62018-01-03 22:24:41 -05002942 if (buffer && buflen && (*buflen == sizeof (int)))
2943 {
2944 /* VPP-TBD */
2945 if (*(int *) buffer)
2946 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
2947 else
2948 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
2949
Florin Coras5e062572019-03-14 19:07:51 -07002950 VDBG (2, "VPPCOM_ATTR_SET_V6ONLY: %d, buflen %d, #VPP-TBD#",
2951 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_V6ONLY),
2952 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002953 }
2954 else
2955 rv = VPPCOM_EINVAL;
2956 break;
2957
2958 case VPPCOM_ATTR_GET_KEEPALIVE:
2959 if (buffer && buflen && (*buflen >= sizeof (int)))
2960 {
2961 /* VPP-TBD */
2962 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2963 VCL_SESS_ATTR_KEEPALIVE);
2964 *buflen = sizeof (int);
2965
Florin Coras5e062572019-03-14 19:07:51 -07002966 VDBG (2, "VPPCOM_ATTR_GET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
2967 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002968 }
2969 else
2970 rv = VPPCOM_EINVAL;
Stevenb5a11602017-10-11 09:59:30 -07002971 break;
Stevenbccd3392017-10-12 20:42:21 -07002972
2973 case VPPCOM_ATTR_SET_KEEPALIVE:
Dave Wallace048b1d62018-01-03 22:24:41 -05002974 if (buffer && buflen && (*buflen == sizeof (int)))
2975 {
2976 /* VPP-TBD */
2977 if (*(int *) buffer)
2978 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2979 else
2980 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
2981
Florin Coras5e062572019-03-14 19:07:51 -07002982 VDBG (2, "VPPCOM_ATTR_SET_KEEPALIVE: %d, buflen %d, #VPP-TBD#",
2983 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_KEEPALIVE),
2984 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05002985 }
2986 else
2987 rv = VPPCOM_EINVAL;
2988 break;
2989
2990 case VPPCOM_ATTR_GET_TCP_NODELAY:
2991 if (buffer && buflen && (*buflen >= sizeof (int)))
2992 {
2993 /* VPP-TBD */
2994 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
2995 VCL_SESS_ATTR_TCP_NODELAY);
2996 *buflen = sizeof (int);
2997
Florin Coras5e062572019-03-14 19:07:51 -07002998 VDBG (2, "VPPCOM_ATTR_GET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
2999 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003000 }
3001 else
3002 rv = VPPCOM_EINVAL;
3003 break;
3004
3005 case VPPCOM_ATTR_SET_TCP_NODELAY:
3006 if (buffer && buflen && (*buflen == sizeof (int)))
3007 {
3008 /* VPP-TBD */
3009 if (*(int *) buffer)
3010 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3011 else
3012 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
3013
Florin Coras5e062572019-03-14 19:07:51 -07003014 VDBG (2, "VPPCOM_ATTR_SET_TCP_NODELAY: %d, buflen %d, #VPP-TBD#",
3015 VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_TCP_NODELAY),
3016 *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003017 }
3018 else
3019 rv = VPPCOM_EINVAL;
3020 break;
3021
3022 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
3023 if (buffer && buflen && (*buflen >= sizeof (int)))
3024 {
3025 /* VPP-TBD */
3026 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3027 VCL_SESS_ATTR_TCP_KEEPIDLE);
3028 *buflen = sizeof (int);
3029
Florin Coras5e062572019-03-14 19:07:51 -07003030 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
3031 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003032 }
3033 else
3034 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003035 break;
3036
3037 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
Dave Wallace048b1d62018-01-03 22:24:41 -05003038 if (buffer && buflen && (*buflen == sizeof (int)))
3039 {
3040 /* VPP-TBD */
3041 if (*(int *) buffer)
3042 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3043 else
3044 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
3045
Florin Coras5e062572019-03-14 19:07:51 -07003046 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003047 VCL_SESS_ATTR_TEST (session->attr,
3048 VCL_SESS_ATTR_TCP_KEEPIDLE), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003049 }
3050 else
3051 rv = VPPCOM_EINVAL;
3052 break;
3053
3054 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
3055 if (buffer && buflen && (*buflen >= sizeof (int)))
3056 {
3057 /* VPP-TBD */
3058 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
3059 VCL_SESS_ATTR_TCP_KEEPINTVL);
3060 *buflen = sizeof (int);
3061
Florin Coras5e062572019-03-14 19:07:51 -07003062 VDBG (2, "VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
3063 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003064 }
3065 else
3066 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003067 break;
3068
3069 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
Dave Wallace048b1d62018-01-03 22:24:41 -05003070 if (buffer && buflen && (*buflen == sizeof (int)))
3071 {
3072 /* VPP-TBD */
3073 if (*(int *) buffer)
3074 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3075 else
3076 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
3077
Florin Coras5e062572019-03-14 19:07:51 -07003078 VDBG (2, "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, buflen %d, #VPP-TBD#",
Florin Coras0d427d82018-06-27 03:24:07 -07003079 VCL_SESS_ATTR_TEST (session->attr,
3080 VCL_SESS_ATTR_TCP_KEEPINTVL), *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003081 }
3082 else
3083 rv = VPPCOM_EINVAL;
3084 break;
3085
3086 case VPPCOM_ATTR_GET_TCP_USER_MSS:
3087 if (buffer && buflen && (*buflen >= sizeof (u32)))
3088 {
3089 /* VPP-TBD */
3090 *(u32 *) buffer = session->user_mss;
3091 *buflen = sizeof (int);
3092
Florin Coras5e062572019-03-14 19:07:51 -07003093 VDBG (2, "VPPCOM_ATTR_GET_TCP_USER_MSS: %d, buflen %d, #VPP-TBD#",
3094 *(int *) buffer, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003095 }
3096 else
3097 rv = VPPCOM_EINVAL;
3098 break;
3099
3100 case VPPCOM_ATTR_SET_TCP_USER_MSS:
3101 if (buffer && buflen && (*buflen == sizeof (u32)))
3102 {
3103 /* VPP-TBD */
3104 session->user_mss = *(u32 *) buffer;
3105
Florin Coras5e062572019-03-14 19:07:51 -07003106 VDBG (2, "VPPCOM_ATTR_SET_TCP_USER_MSS: %u, buflen %d, #VPP-TBD#",
3107 session->user_mss, *buflen);
Dave Wallace048b1d62018-01-03 22:24:41 -05003108 }
3109 else
3110 rv = VPPCOM_EINVAL;
Stevenbccd3392017-10-12 20:42:21 -07003111 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003112
Florin Coras7baeb712019-01-04 17:05:43 -08003113 case VPPCOM_ATTR_SET_SHUT:
3114 if (*flags == SHUT_RD || *flags == SHUT_RDWR)
3115 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_RD);
3116 if (*flags == SHUT_WR || *flags == SHUT_RDWR)
3117 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_SHUT_WR);
3118 break;
3119
3120 case VPPCOM_ATTR_GET_SHUT:
3121 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_RD))
3122 tmp_flags = 1;
3123 if (VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_SHUT_WR))
3124 tmp_flags |= 2;
3125 if (tmp_flags == 1)
3126 *(int *) buffer = SHUT_RD;
3127 else if (tmp_flags == 2)
3128 *(int *) buffer = SHUT_WR;
3129 else if (tmp_flags == 3)
3130 *(int *) buffer = SHUT_RDWR;
3131 *buflen = sizeof (int);
3132 break;
Dave Wallacee22aa742017-10-20 12:30:38 -04003133 default:
3134 rv = VPPCOM_EINVAL;
3135 break;
Dave Wallace35830af2017-10-09 01:43:42 -04003136 }
3137
Dave Wallace35830af2017-10-09 01:43:42 -04003138 return rv;
3139}
3140
Stevenac1f96d2017-10-24 16:03:58 -07003141int
Florin Coras134a9962018-08-28 11:32:04 -07003142vppcom_session_recvfrom (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003143 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3144{
Florin Coras134a9962018-08-28 11:32:04 -07003145 vcl_worker_t *wrk = vcl_worker_get_current ();
Stevenac1f96d2017-10-24 16:03:58 -07003146 int rv = VPPCOM_OK;
Florin Coras7e12d942018-06-27 14:32:43 -07003147 vcl_session_t *session = 0;
Stevenac1f96d2017-10-24 16:03:58 -07003148
3149 if (ep)
3150 {
Florin Coras134a9962018-08-28 11:32:04 -07003151 session = vcl_session_get_w_handle (wrk, session_handle);
Florin Coras070453d2018-08-24 17:04:27 -07003152 if (PREDICT_FALSE (!session))
Stevenac1f96d2017-10-24 16:03:58 -07003153 {
Florin Coras5e062572019-03-14 19:07:51 -07003154 VDBG (0, "sh 0x%llx is closed!", session_handle);
Florin Coras460dce62018-07-27 05:45:06 -07003155 return VPPCOM_EBADFD;
Stevenac1f96d2017-10-24 16:03:58 -07003156 }
Florin Coras7e12d942018-06-27 14:32:43 -07003157 ep->is_ip4 = session->transport.is_ip4;
3158 ep->port = session->transport.rmt_port;
Stevenac1f96d2017-10-24 16:03:58 -07003159 }
Steven58f464e2017-10-25 12:33:12 -07003160
3161 if (flags == 0)
Florin Coras134a9962018-08-28 11:32:04 -07003162 rv = vppcom_session_read (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003163 else if (flags & MSG_PEEK)
Florin Coras134a9962018-08-28 11:32:04 -07003164 rv = vppcom_session_peek (session_handle, buffer, buflen);
Stevenac1f96d2017-10-24 16:03:58 -07003165 else
3166 {
Florin Corasa7a1a222018-12-30 17:11:31 -08003167 VDBG (0, "Unsupport flags for recvfrom %d", flags);
Florin Coras460dce62018-07-27 05:45:06 -07003168 return VPPCOM_EAFNOSUPPORT;
Stevenac1f96d2017-10-24 16:03:58 -07003169 }
3170
Florin Coras99368312018-08-02 10:45:44 -07003171 if (ep)
3172 {
3173 if (session->transport.is_ip4)
Dave Barach178cf492018-11-13 16:34:13 -05003174 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip4,
3175 sizeof (ip4_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003176 else
Dave Barach178cf492018-11-13 16:34:13 -05003177 clib_memcpy_fast (ep->ip, &session->transport.rmt_ip.ip6,
3178 sizeof (ip6_address_t));
Florin Coras99368312018-08-02 10:45:44 -07003179 }
Florin Coras460dce62018-07-27 05:45:06 -07003180
Stevenac1f96d2017-10-24 16:03:58 -07003181 return rv;
3182}
3183
3184int
Florin Coras134a9962018-08-28 11:32:04 -07003185vppcom_session_sendto (uint32_t session_handle, void *buffer,
Stevenac1f96d2017-10-24 16:03:58 -07003186 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3187{
Dave Wallace617dffa2017-10-26 14:47:06 -04003188 if (!buffer)
3189 return VPPCOM_EINVAL;
3190
3191 if (ep)
3192 {
3193 // TBD
3194 return VPPCOM_EINVAL;
3195 }
3196
3197 if (flags)
3198 {
3199 // TBD check the flags and do the right thing
Florin Coras5e062572019-03-14 19:07:51 -07003200 VDBG (2, "handling flags 0x%u (%d) not implemented yet.", flags, flags);
Dave Wallace617dffa2017-10-26 14:47:06 -04003201 }
3202
Florin Coras42ceddb2018-12-12 10:56:01 -08003203 return (vppcom_session_write_inline (session_handle, buffer, buflen, 1));
Stevenac1f96d2017-10-24 16:03:58 -07003204}
3205
Dave Wallace048b1d62018-01-03 22:24:41 -05003206int
3207vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
3208{
Florin Coras134a9962018-08-28 11:32:04 -07003209 vcl_worker_t *wrk = vcl_worker_get_current ();
3210 f64 timeout = clib_time_now (&wrk->clib_time) + wait_for_time;
Dave Wallace048b1d62018-01-03 22:24:41 -05003211 u32 i, keep_trying = 1;
Florin Coras6917b942018-11-13 22:44:54 -08003212 svm_msg_q_msg_t msg;
3213 session_event_t *e;
Dave Wallace048b1d62018-01-03 22:24:41 -05003214 int rv, num_ev = 0;
3215
Florin Coras5e062572019-03-14 19:07:51 -07003216 VDBG (3, "vp %p, nsids %u, wait_for_time %f", vp, n_sids, wait_for_time);
Dave Wallace048b1d62018-01-03 22:24:41 -05003217
3218 if (!vp)
3219 return VPPCOM_EFAULT;
3220
3221 do
3222 {
Florin Coras7e12d942018-06-27 14:32:43 -07003223 vcl_session_t *session;
Dave Wallace048b1d62018-01-03 22:24:41 -05003224
Florin Coras6917b942018-11-13 22:44:54 -08003225 /* Dequeue all events and drop all unhandled io events */
3226 while (svm_msg_q_sub (wrk->app_event_queue, &msg, SVM_Q_NOWAIT, 0) == 0)
3227 {
3228 e = svm_msg_q_msg_data (wrk->app_event_queue, &msg);
3229 vcl_handle_mq_event (wrk, e);
3230 svm_msg_q_free_msg (wrk->app_event_queue, &msg);
3231 }
3232 vec_reset_length (wrk->unhandled_evts_vector);
3233
Dave Wallace048b1d62018-01-03 22:24:41 -05003234 for (i = 0; i < n_sids; i++)
3235 {
Florin Coras7baeb712019-01-04 17:05:43 -08003236 session = vcl_session_get (wrk, vp[i].sh);
Florin Coras070453d2018-08-24 17:04:27 -07003237 if (!session)
Florin Coras6917b942018-11-13 22:44:54 -08003238 {
3239 vp[i].revents = POLLHUP;
3240 num_ev++;
3241 continue;
3242 }
Dave Wallace048b1d62018-01-03 22:24:41 -05003243
Florin Coras6917b942018-11-13 22:44:54 -08003244 vp[i].revents = 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003245
3246 if (POLLIN & vp[i].events)
3247 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003248 rv = vcl_session_read_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003249 if (rv > 0)
3250 {
Florin Coras6917b942018-11-13 22:44:54 -08003251 vp[i].revents |= POLLIN;
Dave Wallace048b1d62018-01-03 22:24:41 -05003252 num_ev++;
3253 }
3254 else if (rv < 0)
3255 {
3256 switch (rv)
3257 {
3258 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003259 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003260 break;
3261
3262 default:
Florin Coras6917b942018-11-13 22:44:54 -08003263 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003264 break;
3265 }
3266 num_ev++;
3267 }
3268 }
3269
3270 if (POLLOUT & vp[i].events)
3271 {
Florin Coras0ef8ef22019-01-18 08:37:13 -08003272 rv = vcl_session_write_ready (session);
Dave Wallace048b1d62018-01-03 22:24:41 -05003273 if (rv > 0)
3274 {
Florin Coras6917b942018-11-13 22:44:54 -08003275 vp[i].revents |= POLLOUT;
Dave Wallace048b1d62018-01-03 22:24:41 -05003276 num_ev++;
3277 }
3278 else if (rv < 0)
3279 {
3280 switch (rv)
3281 {
3282 case VPPCOM_ECONNRESET:
Florin Coras6917b942018-11-13 22:44:54 -08003283 vp[i].revents = POLLHUP;
Dave Wallace048b1d62018-01-03 22:24:41 -05003284 break;
3285
3286 default:
Florin Coras6917b942018-11-13 22:44:54 -08003287 vp[i].revents = POLLERR;
Dave Wallace048b1d62018-01-03 22:24:41 -05003288 break;
3289 }
3290 num_ev++;
3291 }
3292 }
3293
Dave Wallace7e607a72018-06-18 18:41:32 -04003294 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
Dave Wallace048b1d62018-01-03 22:24:41 -05003295 {
Florin Coras6917b942018-11-13 22:44:54 -08003296 vp[i].revents = POLLNVAL;
Dave Wallace048b1d62018-01-03 22:24:41 -05003297 num_ev++;
3298 }
3299 }
3300 if (wait_for_time != -1)
Florin Coras134a9962018-08-28 11:32:04 -07003301 keep_trying = (clib_time_now (&wrk->clib_time) <= timeout) ? 1 : 0;
Dave Wallace048b1d62018-01-03 22:24:41 -05003302 }
3303 while ((num_ev == 0) && keep_trying);
3304
Dave Wallace048b1d62018-01-03 22:24:41 -05003305 return num_ev;
3306}
3307
Florin Coras99368312018-08-02 10:45:44 -07003308int
3309vppcom_mq_epoll_fd (void)
3310{
Florin Coras134a9962018-08-28 11:32:04 -07003311 vcl_worker_t *wrk = vcl_worker_get_current ();
3312 return wrk->mqs_epfd;
3313}
3314
3315int
Florin Coras30e79c22019-01-02 19:31:22 -08003316vppcom_session_index (vcl_session_handle_t session_handle)
Florin Coras134a9962018-08-28 11:32:04 -07003317{
3318 return session_handle & 0xFFFFFF;
3319}
3320
3321int
Florin Coras30e79c22019-01-02 19:31:22 -08003322vppcom_session_worker (vcl_session_handle_t session_handle)
3323{
3324 return session_handle >> 24;
3325}
3326
3327int
Florin Coras134a9962018-08-28 11:32:04 -07003328vppcom_worker_register (void)
3329{
Florin Coras47c40e22018-11-26 17:01:36 -08003330 if (!vcl_worker_alloc_and_init ())
3331 return VPPCOM_EEXIST;
3332
3333 if (vcl_worker_set_bapi ())
3334 return VPPCOM_EEXIST;
3335
3336 if (vcl_worker_register_with_vpp ())
3337 return VPPCOM_EEXIST;
3338
3339 return VPPCOM_OK;
Florin Coras99368312018-08-02 10:45:44 -07003340}
3341
Florin Corasdfe4cf42018-11-28 22:13:45 -08003342int
3343vppcom_worker_index (void)
3344{
3345 return vcl_get_worker_index ();
3346}
3347
Florin Corase0982e52019-01-25 13:19:56 -08003348int
3349vppcom_worker_mqs_epfd (void)
3350{
3351 vcl_worker_t *wrk = vcl_worker_get_current ();
3352 if (!vcm->cfg.use_mq_eventfd)
3353 return -1;
3354 return wrk->mqs_epfd;
3355}
3356
Dave Wallacee22aa742017-10-20 12:30:38 -04003357/*
3358 * fd.io coding-style-patch-verification: ON
3359 *
3360 * Local Variables:
3361 * eval: (c-set-style "gnu")
3362 * End:
3363 */