blob: cd3aaaa3d4ea34c70d43bd9c61ab044ff2a7549c [file] [log] [blame]
Florin Coras697faea2018-06-27 17:10:49 -07001/*
2 * Copyright (c) 2018 Cisco and/or its affiliates.
3 * 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 <vcl/vcl_private.h>
17#include <vlibmemory/api.h>
18#include <vpp/api/vpe_msg_enum.h>
19
20#define vl_typedefs /* define message structures */
21#include <vpp/api/vpe_all_api_h.h>
22#undef vl_typedefs
23
24/* declare message handlers for each api */
25
26#define vl_endianfun /* define message structures */
27#include <vpp/api/vpe_all_api_h.h>
28#undef vl_endianfun
29
30/* instantiate all the print functions we know about */
31#define vl_print(handle, ...)
32#define vl_printfun
33#include <vpp/api/vpe_all_api_h.h>
34#undef vl_printfun
35
Florin Coras54693d22018-07-17 10:46:29 -070036u8 *
Florin Coras697faea2018-06-27 17:10:49 -070037format_api_error (u8 * s, va_list * args)
38{
39 i32 error = va_arg (*args, u32);
40 uword *p;
41
42 p = hash_get (vcm->error_string_by_error_number, -error);
43
44 if (p)
45 s = format (s, "%s (%d)", p[0], error);
46 else
47 s = format (s, "%d", error);
48 return s;
49}
50
51static void
52 vl_api_session_enable_disable_reply_t_handler
53 (vl_api_session_enable_disable_reply_t * mp)
54{
55 if (mp->retval)
56 {
57 clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
58 format_api_error, ntohl (mp->retval));
59 }
60 else
61 vcm->app_state = STATE_APP_ENABLED;
62}
63
Florin Coras99368312018-08-02 10:45:44 -070064static int
Florin Corasd85de682018-11-29 17:02:29 -080065vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
66 int fd)
Florin Coras99368312018-08-02 10:45:44 -070067{
68 svm_fifo_segment_create_args_t _a, *a = &_a;
69 int rv;
70
71 memset (a, 0, sizeof (*a));
72 a->segment_name = (char *) name;
73 a->segment_type = type;
74
75 if (type == SSVM_SEGMENT_MEMFD)
76 a->memfd_fd = fd;
77
Florin Corasadc74d72018-12-02 13:36:00 -080078 if ((rv = svm_fifo_segment_attach (&vcm->segment_main, a)))
Florin Coras99368312018-08-02 10:45:44 -070079 {
80 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
81 return rv;
82 }
Florin Corasd85de682018-11-29 17:02:29 -080083 vcl_segment_table_add (segment_handle, a->new_segment_indices[0]);
Florin Coras99368312018-08-02 10:45:44 -070084 vec_reset_length (a->new_segment_indices);
85 return 0;
86}
87
Florin Coras697faea2018-06-27 17:10:49 -070088static void
Florin Corasd85de682018-11-29 17:02:29 -080089vcl_segment_detach (u64 segment_handle)
90{
Florin Corasadc74d72018-12-02 13:36:00 -080091 svm_fifo_segment_main_t *sm = &vcm->segment_main;
Florin Corasd85de682018-11-29 17:02:29 -080092 svm_fifo_segment_private_t *segment;
93 u32 segment_index;
94
95 segment_index = vcl_segment_table_lookup (segment_handle);
96 if (segment_index == (u32) ~ 0)
97 return;
Florin Corasadc74d72018-12-02 13:36:00 -080098 segment = svm_fifo_segment_get_segment (sm, segment_index);
99 svm_fifo_segment_delete (sm, segment);
Florin Corasd85de682018-11-29 17:02:29 -0800100 vcl_segment_table_del (segment_handle);
101}
102
103static u64
104vcl_vpp_worker_segment_handle (u32 wrk_index)
105{
106 return (VCL_INVALID_SEGMENT_HANDLE - wrk_index - 1);
107}
108
109static void
Florin Coras697faea2018-06-27 17:10:49 -0700110vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
111 mp)
112{
Florin Coras134a9962018-08-28 11:32:04 -0700113 vcl_worker_t *wrk = vcl_worker_get (0);
Florin Corasd85de682018-11-29 17:02:29 -0800114 u64 segment_handle;
Florin Coras99368312018-08-02 10:45:44 -0700115 u32 n_fds = 0;
116 int *fds = 0;
Florin Coras697faea2018-06-27 17:10:49 -0700117
Florin Coras697faea2018-06-27 17:10:49 -0700118 if (mp->retval)
119 {
120 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
121 format_api_error, ntohl (mp->retval));
122 return;
123 }
124
Florin Coras134a9962018-08-28 11:32:04 -0700125 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
Florin Coras99368312018-08-02 10:45:44 -0700126 svm_msg_q_t *);
Florin Corasd85de682018-11-29 17:02:29 -0800127 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
128 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
129 {
130 clib_warning ("invalid segment handle");
131 return;
132 }
133
Florin Coras99368312018-08-02 10:45:44 -0700134 if (mp->n_fds)
Florin Coras697faea2018-06-27 17:10:49 -0700135 {
Florin Coras99368312018-08-02 10:45:44 -0700136 vec_validate (fds, mp->n_fds);
137 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
138
139 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -0800140 if (vcl_segment_attach (vcl_vpp_worker_segment_handle (0),
141 "vpp-mq-seg", SSVM_SEGMENT_MEMFD,
142 fds[n_fds++]))
Florin Coras99368312018-08-02 10:45:44 -0700143 return;
144
145 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -0800146 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
147 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
Florin Coras99368312018-08-02 10:45:44 -0700148 return;
149
150 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
151 {
Florin Coras134a9962018-08-28 11:32:04 -0700152 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
153 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
Florin Coras99368312018-08-02 10:45:44 -0700154 n_fds++;
155 }
156
157 vec_free (fds);
Florin Coras697faea2018-06-27 17:10:49 -0700158 }
Florin Coras99368312018-08-02 10:45:44 -0700159 else
Florin Coras697faea2018-06-27 17:10:49 -0700160 {
Florin Corasd85de682018-11-29 17:02:29 -0800161 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
162 SSVM_SEGMENT_SHM, -1))
Florin Coras99368312018-08-02 10:45:44 -0700163 return;
Florin Coras697faea2018-06-27 17:10:49 -0700164 }
165
Florin Corasc1f5a432018-11-20 11:31:26 -0800166 vcm->app_index = clib_net_to_host_u32 (mp->app_index);
Florin Coras697faea2018-06-27 17:10:49 -0700167 vcm->app_state = STATE_APP_ATTACHED;
168}
169
170static void
Florin Coras134a9962018-08-28 11:32:04 -0700171vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
172 mp)
173{
Florin Corasab2f6db2018-08-31 14:31:41 -0700174 int n_fds = 0, *fds = 0;
Florin Corasd85de682018-11-29 17:02:29 -0800175 u64 segment_handle;
Florin Coras134a9962018-08-28 11:32:04 -0700176 vcl_worker_t *wrk;
Florin Corasab2f6db2018-08-31 14:31:41 -0700177 u32 wrk_index;
Florin Coras134a9962018-08-28 11:32:04 -0700178
179 if (mp->retval)
180 {
181 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
182 format_api_error, ntohl (mp->retval));
183 goto failed;
184 }
Florin Coras053a0e42018-11-13 15:52:38 -0800185 wrk_index = mp->context;
186 wrk = vcl_worker_get (wrk_index);
187 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
188
Florin Coras3348a4c2018-09-07 17:09:35 -0700189 if (!mp->is_add)
190 return;
191
Florin Coras134a9962018-08-28 11:32:04 -0700192 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
193 svm_msg_q_t *);
194
Florin Corasd85de682018-11-29 17:02:29 -0800195 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
196 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
197 {
198 clib_warning ("invalid segment handle");
199 return;
200 }
201
Florin Coras134a9962018-08-28 11:32:04 -0700202 if (mp->n_fds)
203 {
204 vec_validate (fds, mp->n_fds);
205 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
206
207 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -0800208 if (vcl_segment_attach
209 (vcl_vpp_worker_segment_handle (wrk->wrk_index), "vpp-worker-seg",
210 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
Florin Coras134a9962018-08-28 11:32:04 -0700211 goto failed;
212
213 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
Florin Corasd85de682018-11-29 17:02:29 -0800214 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
215 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
Florin Coras134a9962018-08-28 11:32:04 -0700216 goto failed;
217
218 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
219 {
220 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
221 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
222 n_fds++;
223 }
224
225 vec_free (fds);
226 }
227 else
228 {
Florin Corasd85de682018-11-29 17:02:29 -0800229 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
230 SSVM_SEGMENT_SHM, -1))
Florin Coras134a9962018-08-28 11:32:04 -0700231 goto failed;
232 }
233 vcm->app_state = STATE_APP_READY;
Florin Coras053a0e42018-11-13 15:52:38 -0800234 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700235 return;
236
237failed:
238 vcm->app_state = STATE_APP_FAILED;
239}
240
241static void
Florin Coras697faea2018-06-27 17:10:49 -0700242vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
243 mp)
244{
245 if (mp->retval)
246 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
247 ntohl (mp->retval));
248
249 vcm->app_state = STATE_APP_ENABLED;
250}
251
252static void
Florin Coras697faea2018-06-27 17:10:49 -0700253vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
254{
Florin Coras99368312018-08-02 10:45:44 -0700255 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
Florin Corasd85de682018-11-29 17:02:29 -0800256 u64 segment_handle;
Florin Coras99368312018-08-02 10:45:44 -0700257 int fd = -1;
Florin Coras697faea2018-06-27 17:10:49 -0700258
Florin Coras99368312018-08-02 10:45:44 -0700259 if (mp->fd_flags)
260 {
261 vl_socket_client_recv_fd_msg (&fd, 1, 5);
262 seg_type = SSVM_SEGMENT_MEMFD;
263 }
264
Florin Corasd85de682018-11-29 17:02:29 -0800265 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
266 if (segment_handle == VCL_INVALID_SEGMENT_HANDLE)
267 {
268 clib_warning ("invalid segment handle");
269 return;
270 }
271
272 if (vcl_segment_attach (segment_handle, (char *) mp->segment_name,
273 seg_type, fd))
Florin Coras697faea2018-06-27 17:10:49 -0700274 {
275 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
276 getpid (), mp->segment_name);
277 return;
278 }
279
280 VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
281 mp->segment_name, mp->segment_size);
282}
283
284static void
285vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
286{
Florin Corasd85de682018-11-29 17:02:29 -0800287 u64 segment_handle = clib_net_to_host_u64 (mp->segment_handle);
288 vcl_segment_detach (segment_handle);
289 VDBG (1, "Unmapped segment: %d", segment_handle);
Florin Coras697faea2018-06-27 17:10:49 -0700290}
291
292static void
Florin Coras99368312018-08-02 10:45:44 -0700293 vl_api_app_cut_through_registration_add_t_handler
294 (vl_api_app_cut_through_registration_add_t * mp)
295{
296 vcl_cut_through_registration_t *ctr;
297 u32 mqc_index = ~0;
Florin Coras134a9962018-08-28 11:32:04 -0700298 vcl_worker_t *wrk;
Florin Coras99368312018-08-02 10:45:44 -0700299 int *fds = 0;
300
301 if (mp->n_fds)
302 {
303 ASSERT (mp->n_fds == 2);
304 vec_validate (fds, mp->n_fds);
305 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
306 }
307
Florin Coras134a9962018-08-28 11:32:04 -0700308 wrk = vcl_worker_get (mp->wrk_index);
309 ctr = vcl_ct_registration_lock_and_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700310 ctr->mq = uword_to_pointer (mp->evt_q_address, svm_msg_q_t *);
311 ctr->peer_mq = uword_to_pointer (mp->peer_evt_q_address, svm_msg_q_t *);
Florin Coras134a9962018-08-28 11:32:04 -0700312 VDBG (0, "Adding ct registration %u", vcl_ct_registration_index (wrk, ctr));
Florin Coras99368312018-08-02 10:45:44 -0700313
Florin Coras15531972018-08-12 23:50:53 -0700314 if (mp->n_fds && (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD))
Florin Coras99368312018-08-02 10:45:44 -0700315 {
316 svm_msg_q_set_consumer_eventfd (ctr->mq, fds[0]);
317 svm_msg_q_set_producer_eventfd (ctr->peer_mq, fds[1]);
Florin Coras134a9962018-08-28 11:32:04 -0700318 mqc_index = vcl_mq_epoll_add_evfd (wrk, ctr->mq);
Florin Coras99368312018-08-02 10:45:44 -0700319 ctr->epoll_evt_conn_index = mqc_index;
320 vec_free (fds);
321 }
Florin Coras134a9962018-08-28 11:32:04 -0700322 vcl_ct_registration_lookup_add (wrk, mp->evt_q_address,
323 vcl_ct_registration_index (wrk, ctr));
324 vcl_ct_registration_unlock (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700325}
326
327static void
Florin Coras697faea2018-06-27 17:10:49 -0700328vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
329{
Florin Coras60116992018-08-27 09:52:18 -0700330 /* Expecting a similar message on mq. So ignore this */
331 VDBG (1, "VCL<%d>: bapi msg vpp handle 0x%llx, sid %u: bind retval: %u!",
332 getpid (), mp->handle, mp->context, mp->retval);
Florin Coras697faea2018-06-27 17:10:49 -0700333}
334
335static void
336vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
337{
338 if (mp->retval)
339 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
340 getpid (), mp->context, format_api_error,
341 ntohl (mp->retval));
342
343 else
344 VDBG (1, "VCL<%d>: sid %u: unbind succeeded!", getpid (), mp->context);
345}
346
Florin Corasab2f6db2018-08-31 14:31:41 -0700347static void
348vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
349 mp)
350{
351 if (mp->retval)
352 clib_warning ("VCL<%d>: ERROR: sid %u: disconnect failed: %U",
353 getpid (), mp->context, format_api_error,
354 ntohl (mp->retval));
355}
356
357static void
Florin Coras6d4bb422018-09-04 22:07:27 -0700358vl_api_connect_session_reply_t_handler (vl_api_connect_sock_reply_t * mp)
Florin Corasab2f6db2018-08-31 14:31:41 -0700359{
360 if (mp->retval)
361 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed: %U",
362 getpid (), mp->context, format_api_error,
363 ntohl (mp->retval));
364}
365
Florin Coras99368312018-08-02 10:45:44 -0700366#define foreach_sock_msg \
367_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
368_(BIND_SOCK_REPLY, bind_sock_reply) \
369_(UNBIND_SOCK_REPLY, unbind_sock_reply) \
Florin Coras6d4bb422018-09-04 22:07:27 -0700370_(CONNECT_SESSION_REPLY, connect_session_reply) \
Florin Corasab2f6db2018-08-31 14:31:41 -0700371_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
Florin Coras99368312018-08-02 10:45:44 -0700372_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
373_(APPLICATION_DETACH_REPLY, application_detach_reply) \
374_(MAP_ANOTHER_SEGMENT, map_another_segment) \
375_(UNMAP_SEGMENT, unmap_segment) \
376_(APP_CUT_THROUGH_REGISTRATION_ADD, app_cut_through_registration_add) \
Florin Coras134a9962018-08-28 11:32:04 -0700377_(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
Florin Coras697faea2018-06-27 17:10:49 -0700378
379void
380vppcom_api_hookup (void)
381{
Florin Coras60116992018-08-27 09:52:18 -0700382#define _(N, n) \
383 vl_msg_api_set_handlers(VL_API_##N, #n, \
Florin Coras697faea2018-06-27 17:10:49 -0700384 vl_api_##n##_t_handler, \
385 vl_noop_handler, \
386 vl_api_##n##_t_endian, \
387 vl_api_##n##_t_print, \
388 sizeof(vl_api_##n##_t), 1);
389 foreach_sock_msg;
390#undef _
391}
392
393/*
394 * VPP-API message functions
395 */
396void
397vppcom_send_session_enable_disable (u8 is_enable)
398{
Florin Coras47c40e22018-11-26 17:01:36 -0800399 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700400 vl_api_session_enable_disable_t *bmp;
401 bmp = vl_msg_api_alloc (sizeof (*bmp));
402 memset (bmp, 0, sizeof (*bmp));
403
404 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
Florin Coras47c40e22018-11-26 17:01:36 -0800405 bmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700406 bmp->context = htonl (0xfeedface);
407 bmp->is_enable = is_enable;
Florin Coras47c40e22018-11-26 17:01:36 -0800408 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700409}
410
411void
412vppcom_app_send_attach (void)
413{
Florin Coras47c40e22018-11-26 17:01:36 -0800414 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700415 vl_api_application_attach_t *bmp;
416 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
417 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
418 vcm->cfg.app_proxy_transport_udp);
419
420 bmp = vl_msg_api_alloc (sizeof (*bmp));
421 memset (bmp, 0, sizeof (*bmp));
422
423 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
Florin Coras47c40e22018-11-26 17:01:36 -0800424 bmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700425 bmp->context = htonl (0xfeedface);
426 bmp->options[APP_OPTIONS_FLAGS] =
427 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
428 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
429 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
Florin Coras54693d22018-07-17 10:46:29 -0700430 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
Florin Coras99368312018-08-02 10:45:44 -0700431 APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS |
432 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
Florin Coras697faea2018-06-27 17:10:49 -0700433 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
434 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
435 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
436 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
437 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
438 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
439 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
440 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
441 vcm->cfg.preallocated_fifo_pairs;
442 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
443 if (nsid_len)
444 {
445 bmp->namespace_id_len = nsid_len;
Dave Barach178cf492018-11-13 16:34:13 -0500446 clib_memcpy_fast (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
Florin Coras697faea2018-06-27 17:10:49 -0700447 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
448 }
Florin Coras47c40e22018-11-26 17:01:36 -0800449 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700450}
451
452void
453vppcom_app_send_detach (void)
454{
Florin Coras47c40e22018-11-26 17:01:36 -0800455 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700456 vl_api_application_detach_t *bmp;
457 bmp = vl_msg_api_alloc (sizeof (*bmp));
458 memset (bmp, 0, sizeof (*bmp));
459
460 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
Florin Coras47c40e22018-11-26 17:01:36 -0800461 bmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700462 bmp->context = htonl (0xfeedface);
Florin Coras47c40e22018-11-26 17:01:36 -0800463 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700464}
465
466void
Florin Coras134a9962018-08-28 11:32:04 -0700467vcl_send_app_worker_add_del (u8 is_add)
468{
469 vcl_worker_t *wrk = vcl_worker_get_current ();
470 vl_api_app_worker_add_del_t *mp;
471 u32 wrk_index = wrk->wrk_index;
472
473 mp = vl_msg_api_alloc (sizeof (*mp));
474 memset (mp, 0, sizeof (*mp));
475
476 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
Florin Coras47c40e22018-11-26 17:01:36 -0800477 mp->client_index = wrk->my_client_index;
Florin Corasc1f5a432018-11-20 11:31:26 -0800478 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
Florin Coras134a9962018-08-28 11:32:04 -0700479 mp->context = wrk_index;
480 mp->is_add = is_add;
481 if (!is_add)
482 mp->wrk_index = clib_host_to_net_u32 (wrk_index);
483
Florin Coras47c40e22018-11-26 17:01:36 -0800484 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & mp);
Florin Coras134a9962018-08-28 11:32:04 -0700485}
486
487void
488vppcom_send_connect_sock (vcl_session_t * session)
Florin Coras697faea2018-06-27 17:10:49 -0700489{
Florin Coras053a0e42018-11-13 15:52:38 -0800490 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700491 vl_api_connect_sock_t *cmp;
492
Florin Coras697faea2018-06-27 17:10:49 -0700493 cmp = vl_msg_api_alloc (sizeof (*cmp));
494 memset (cmp, 0, sizeof (*cmp));
495 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
Florin Coras47c40e22018-11-26 17:01:36 -0800496 cmp->client_index = wrk->my_client_index;
Florin Coras134a9962018-08-28 11:32:04 -0700497 cmp->context = session->session_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800498 cmp->wrk_index = wrk->vpp_wrk_index;
Florin Coras697faea2018-06-27 17:10:49 -0700499 cmp->is_ip4 = session->transport.is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500500 clib_memcpy_fast (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip));
Florin Coras697faea2018-06-27 17:10:49 -0700501 cmp->port = session->transport.rmt_port;
502 cmp->proto = session->session_type;
Dave Barach178cf492018-11-13 16:34:13 -0500503 clib_memcpy_fast (cmp->options, session->options, sizeof (cmp->options));
Florin Coras47c40e22018-11-26 17:01:36 -0800504 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & cmp);
Florin Coras697faea2018-06-27 17:10:49 -0700505}
506
507void
Florin Corasab2f6db2018-08-31 14:31:41 -0700508vppcom_send_disconnect_session (u64 vpp_handle)
Florin Coras697faea2018-06-27 17:10:49 -0700509{
Florin Coras47c40e22018-11-26 17:01:36 -0800510 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700511 vl_api_disconnect_session_t *dmp;
512
Florin Coras697faea2018-06-27 17:10:49 -0700513 dmp = vl_msg_api_alloc (sizeof (*dmp));
514 memset (dmp, 0, sizeof (*dmp));
515 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
Florin Coras47c40e22018-11-26 17:01:36 -0800516 dmp->client_index = wrk->my_client_index;
Florin Coras697faea2018-06-27 17:10:49 -0700517 dmp->handle = vpp_handle;
Florin Coras47c40e22018-11-26 17:01:36 -0800518 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & dmp);
Florin Coras697faea2018-06-27 17:10:49 -0700519}
520
521/* VPP combines bind and listen as one operation. VCL manages the separation
522 * of bind and listen locally via vppcom_session_bind() and
523 * vppcom_session_listen() */
524void
Florin Coras134a9962018-08-28 11:32:04 -0700525vppcom_send_bind_sock (vcl_session_t * session)
Florin Coras697faea2018-06-27 17:10:49 -0700526{
Florin Coras053a0e42018-11-13 15:52:38 -0800527 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700528 vl_api_bind_sock_t *bmp;
529
530 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
531 bmp = vl_msg_api_alloc (sizeof (*bmp));
532 memset (bmp, 0, sizeof (*bmp));
533
534 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
Florin Coras47c40e22018-11-26 17:01:36 -0800535 bmp->client_index = wrk->my_client_index;
Florin Coras134a9962018-08-28 11:32:04 -0700536 bmp->context = session->session_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800537 bmp->wrk_index = wrk->vpp_wrk_index;
Florin Coras697faea2018-06-27 17:10:49 -0700538 bmp->is_ip4 = session->transport.is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500539 clib_memcpy_fast (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip));
Florin Coras697faea2018-06-27 17:10:49 -0700540 bmp->port = session->transport.lcl_port;
541 bmp->proto = session->session_type;
Dave Barach178cf492018-11-13 16:34:13 -0500542 clib_memcpy_fast (bmp->options, session->options, sizeof (bmp->options));
Florin Coras47c40e22018-11-26 17:01:36 -0800543 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & bmp);
Florin Coras697faea2018-06-27 17:10:49 -0700544}
545
546void
547vppcom_send_unbind_sock (u64 vpp_handle)
548{
Florin Coras053a0e42018-11-13 15:52:38 -0800549 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700550 vl_api_unbind_sock_t *ump;
551
552 ump = vl_msg_api_alloc (sizeof (*ump));
553 memset (ump, 0, sizeof (*ump));
554
555 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
Florin Coras47c40e22018-11-26 17:01:36 -0800556 ump->client_index = wrk->my_client_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800557 ump->wrk_index = wrk->vpp_wrk_index;
Florin Coras697faea2018-06-27 17:10:49 -0700558 ump->handle = vpp_handle;
Florin Coras47c40e22018-11-26 17:01:36 -0800559 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & ump);
Florin Coras697faea2018-06-27 17:10:49 -0700560}
561
562void
563vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
564{
Florin Coras47c40e22018-11-26 17:01:36 -0800565 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700566 vl_api_accept_session_reply_t *rmp;
567
568 rmp = vl_msg_api_alloc (sizeof (*rmp));
569 memset (rmp, 0, sizeof (*rmp));
570 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
571 rmp->retval = htonl (retval);
572 rmp->context = context;
573 rmp->handle = handle;
Florin Coras47c40e22018-11-26 17:01:36 -0800574 vl_msg_api_send_shmem (wrk->vl_input_queue, (u8 *) & rmp);
Florin Coras697faea2018-06-27 17:10:49 -0700575}
576
577u32
578vcl_max_nsid_len (void)
579{
580 vl_api_application_attach_t *mp;
581 return (sizeof (mp->namespace_id) - 1);
582}
583
584void
585vppcom_init_error_string_table (void)
586{
587 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
588
589#define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
590 foreach_vnet_api_error;
591#undef _
592
593 hash_set (vcm->error_string_by_error_number, 99, "Misc");
594}
595
596int
597vppcom_connect_to_vpp (char *app_name)
598{
Florin Coras47c40e22018-11-26 17:01:36 -0800599 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700600 api_main_t *am = &api_main;
601 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Florin Coras697faea2018-06-27 17:10:49 -0700602
Florin Coras99368312018-08-02 10:45:44 -0700603 if (vcl_cfg->vpp_api_socket_name)
Florin Coras697faea2018-06-27 17:10:49 -0700604 {
Florin Coras99368312018-08-02 10:45:44 -0700605 if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name,
606 app_name, 0 /* default rx/tx buffer */ ))
607 {
Florin Coras053a0e42018-11-13 15:52:38 -0800608 VERR ("app (%s) socket connect failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700609 return VPPCOM_ECONNREFUSED;
610 }
611
612 if (vl_socket_client_init_shm (0))
613 {
Florin Coras053a0e42018-11-13 15:52:38 -0800614 VERR ("app (%s) init shm failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700615 return VPPCOM_ECONNREFUSED;
616 }
Florin Coras697faea2018-06-27 17:10:49 -0700617 }
618 else
619 {
Florin Coras99368312018-08-02 10:45:44 -0700620 if (!vcl_cfg->vpp_api_filename)
621 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
Florin Coras697faea2018-06-27 17:10:49 -0700622
Florin Coras053a0e42018-11-13 15:52:38 -0800623 VDBG (0, "app (%s) connecting to VPP api (%s)...",
Florin Coras99368312018-08-02 10:45:44 -0700624 app_name, vcl_cfg->vpp_api_filename);
625
626 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
627 app_name, vcm->cfg.vpp_api_q_length) < 0)
628 {
Florin Coras053a0e42018-11-13 15:52:38 -0800629 VERR ("app (%s) connect failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700630 return VPPCOM_ECONNREFUSED;
631 }
632
Florin Coras697faea2018-06-27 17:10:49 -0700633 }
634
Florin Coras47c40e22018-11-26 17:01:36 -0800635 wrk->vl_input_queue = am->shmem_hdr->vl_input_queue;
636 wrk->my_client_index = (u32) am->my_client_index;
637 wrk->wrk_state = STATE_APP_CONN_VPP;
Florin Coras99368312018-08-02 10:45:44 -0700638
Florin Coras053a0e42018-11-13 15:52:38 -0800639 VDBG (0, "app (%s) is connected to VPP!", app_name);
Florin Coras697faea2018-06-27 17:10:49 -0700640 vcl_evt (VCL_EVT_INIT, vcm);
Florin Coras99368312018-08-02 10:45:44 -0700641 return VPPCOM_OK;
Florin Coras697faea2018-06-27 17:10:49 -0700642}
643
644/*
645 * fd.io coding-style-patch-verification: ON
646 *
647 * Local Variables:
648 * eval: (c-set-style "gnu")
649 * End:
650 */