blob: cc494ed3b6d6646189456a274ea3cf1dfd0dd30d [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
65ssvm_segment_attach (char *name, ssvm_segment_type_t type, int fd)
66{
67 svm_fifo_segment_create_args_t _a, *a = &_a;
68 int rv;
69
70 memset (a, 0, sizeof (*a));
71 a->segment_name = (char *) name;
72 a->segment_type = type;
73
74 if (type == SSVM_SEGMENT_MEMFD)
75 a->memfd_fd = fd;
76
77 if ((rv = svm_fifo_segment_attach (a)))
78 {
79 clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
80 return rv;
81 }
82 vec_reset_length (a->new_segment_indices);
83 return 0;
84}
85
Florin Coras697faea2018-06-27 17:10:49 -070086static void
87vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
88 mp)
89{
Florin Coras134a9962018-08-28 11:32:04 -070090 vcl_worker_t *wrk = vcl_worker_get (0);
Florin Coras99368312018-08-02 10:45:44 -070091 u32 n_fds = 0;
92 int *fds = 0;
Florin Coras697faea2018-06-27 17:10:49 -070093
Florin Coras697faea2018-06-27 17:10:49 -070094 if (mp->retval)
95 {
96 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
97 format_api_error, ntohl (mp->retval));
98 return;
99 }
100
Florin Coras134a9962018-08-28 11:32:04 -0700101 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
Florin Coras99368312018-08-02 10:45:44 -0700102 svm_msg_q_t *);
103 if (mp->n_fds)
Florin Coras697faea2018-06-27 17:10:49 -0700104 {
Florin Coras99368312018-08-02 10:45:44 -0700105 vec_validate (fds, mp->n_fds);
106 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
107
108 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
109 if (ssvm_segment_attach ("vpp-mq-seg", SSVM_SEGMENT_MEMFD,
110 fds[n_fds++]))
111 return;
112
113 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
114 if (ssvm_segment_attach ((char *) mp->segment_name,
115 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
116 return;
117
118 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
119 {
Florin Coras134a9962018-08-28 11:32:04 -0700120 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
121 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
Florin Coras99368312018-08-02 10:45:44 -0700122 n_fds++;
123 }
124
125 vec_free (fds);
Florin Coras697faea2018-06-27 17:10:49 -0700126 }
Florin Coras99368312018-08-02 10:45:44 -0700127 else
Florin Coras697faea2018-06-27 17:10:49 -0700128 {
Florin Coras99368312018-08-02 10:45:44 -0700129 if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM,
130 -1))
131 return;
Florin Coras697faea2018-06-27 17:10:49 -0700132 }
133
Florin Corasc1f5a432018-11-20 11:31:26 -0800134 vcm->app_index = clib_net_to_host_u32 (mp->app_index);
Florin Coras697faea2018-06-27 17:10:49 -0700135 vcm->app_state = STATE_APP_ATTACHED;
136}
137
138static void
Florin Coras134a9962018-08-28 11:32:04 -0700139vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
140 mp)
141{
Florin Corasab2f6db2018-08-31 14:31:41 -0700142 int n_fds = 0, *fds = 0;
Florin Coras134a9962018-08-28 11:32:04 -0700143 vcl_worker_t *wrk;
Florin Corasab2f6db2018-08-31 14:31:41 -0700144 u32 wrk_index;
Florin Coras134a9962018-08-28 11:32:04 -0700145
146 if (mp->retval)
147 {
148 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
149 format_api_error, ntohl (mp->retval));
150 goto failed;
151 }
Florin Coras053a0e42018-11-13 15:52:38 -0800152 wrk_index = mp->context;
153 wrk = vcl_worker_get (wrk_index);
154 wrk->vpp_wrk_index = clib_net_to_host_u32 (mp->wrk_index);
155
Florin Coras3348a4c2018-09-07 17:09:35 -0700156 if (!mp->is_add)
157 return;
158
Florin Coras134a9962018-08-28 11:32:04 -0700159 wrk->app_event_queue = uword_to_pointer (mp->app_event_queue_address,
160 svm_msg_q_t *);
161
162 if (mp->n_fds)
163 {
164 vec_validate (fds, mp->n_fds);
165 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
166
167 if (mp->fd_flags & SESSION_FD_F_VPP_MQ_SEGMENT)
168 if (ssvm_segment_attach ("vpp-worker-seg", SSVM_SEGMENT_MEMFD,
169 fds[n_fds++]))
170 goto failed;
171
172 if (mp->fd_flags & SESSION_FD_F_MEMFD_SEGMENT)
173 if (ssvm_segment_attach ((char *) mp->segment_name,
174 SSVM_SEGMENT_MEMFD, fds[n_fds++]))
175 goto failed;
176
177 if (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD)
178 {
179 svm_msg_q_set_consumer_eventfd (wrk->app_event_queue, fds[n_fds]);
180 vcl_mq_epoll_add_evfd (wrk, wrk->app_event_queue);
181 n_fds++;
182 }
183
184 vec_free (fds);
185 }
186 else
187 {
188 if (ssvm_segment_attach ((char *) mp->segment_name, SSVM_SEGMENT_SHM,
189 -1))
190 goto failed;
191 }
192 vcm->app_state = STATE_APP_READY;
Florin Coras053a0e42018-11-13 15:52:38 -0800193 VDBG (0, "worker %u vpp-worker %u added", wrk_index, wrk->vpp_wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700194 return;
195
196failed:
197 vcm->app_state = STATE_APP_FAILED;
198}
199
200static void
Florin Coras697faea2018-06-27 17:10:49 -0700201vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
202 mp)
203{
204 if (mp->retval)
205 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
206 ntohl (mp->retval));
207
208 vcm->app_state = STATE_APP_ENABLED;
209}
210
211static void
Florin Coras697faea2018-06-27 17:10:49 -0700212vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
213{
Florin Coras99368312018-08-02 10:45:44 -0700214 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
215 int fd = -1;
Florin Coras697faea2018-06-27 17:10:49 -0700216
Florin Coras460dce62018-07-27 05:45:06 -0700217 vcm->mounting_segment = 1;
Florin Coras99368312018-08-02 10:45:44 -0700218
219 if (mp->fd_flags)
220 {
221 vl_socket_client_recv_fd_msg (&fd, 1, 5);
222 seg_type = SSVM_SEGMENT_MEMFD;
223 }
224
225 if (PREDICT_FALSE (ssvm_segment_attach ((char *) mp->segment_name,
226 seg_type, fd)))
Florin Coras697faea2018-06-27 17:10:49 -0700227 {
228 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
229 getpid (), mp->segment_name);
230 return;
231 }
232
233 VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
234 mp->segment_name, mp->segment_size);
Florin Coras460dce62018-07-27 05:45:06 -0700235 vcm->mounting_segment = 0;
Florin Coras697faea2018-06-27 17:10:49 -0700236}
237
238static void
239vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
240{
241
242/*
243 * XXX Need segment_name to session_id hash,
244 * XXX - have sessionID by handle hash currently
245 */
246
247 VDBG (1, "Unmapped segment '%s'", mp->segment_name);
248}
249
250static void
Florin Coras99368312018-08-02 10:45:44 -0700251 vl_api_app_cut_through_registration_add_t_handler
252 (vl_api_app_cut_through_registration_add_t * mp)
253{
254 vcl_cut_through_registration_t *ctr;
255 u32 mqc_index = ~0;
Florin Coras134a9962018-08-28 11:32:04 -0700256 vcl_worker_t *wrk;
Florin Coras99368312018-08-02 10:45:44 -0700257 int *fds = 0;
258
259 if (mp->n_fds)
260 {
261 ASSERT (mp->n_fds == 2);
262 vec_validate (fds, mp->n_fds);
263 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
264 }
265
Florin Coras134a9962018-08-28 11:32:04 -0700266 wrk = vcl_worker_get (mp->wrk_index);
267 ctr = vcl_ct_registration_lock_and_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700268 ctr->mq = uword_to_pointer (mp->evt_q_address, svm_msg_q_t *);
269 ctr->peer_mq = uword_to_pointer (mp->peer_evt_q_address, svm_msg_q_t *);
Florin Coras134a9962018-08-28 11:32:04 -0700270 VDBG (0, "Adding ct registration %u", vcl_ct_registration_index (wrk, ctr));
Florin Coras99368312018-08-02 10:45:44 -0700271
Florin Coras15531972018-08-12 23:50:53 -0700272 if (mp->n_fds && (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD))
Florin Coras99368312018-08-02 10:45:44 -0700273 {
274 svm_msg_q_set_consumer_eventfd (ctr->mq, fds[0]);
275 svm_msg_q_set_producer_eventfd (ctr->peer_mq, fds[1]);
Florin Coras134a9962018-08-28 11:32:04 -0700276 mqc_index = vcl_mq_epoll_add_evfd (wrk, ctr->mq);
Florin Coras99368312018-08-02 10:45:44 -0700277 ctr->epoll_evt_conn_index = mqc_index;
278 vec_free (fds);
279 }
Florin Coras134a9962018-08-28 11:32:04 -0700280 vcl_ct_registration_lookup_add (wrk, mp->evt_q_address,
281 vcl_ct_registration_index (wrk, ctr));
282 vcl_ct_registration_unlock (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700283}
284
285static void
Florin Coras697faea2018-06-27 17:10:49 -0700286vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
287{
Florin Coras60116992018-08-27 09:52:18 -0700288 /* Expecting a similar message on mq. So ignore this */
289 VDBG (1, "VCL<%d>: bapi msg vpp handle 0x%llx, sid %u: bind retval: %u!",
290 getpid (), mp->handle, mp->context, mp->retval);
Florin Coras697faea2018-06-27 17:10:49 -0700291}
292
293static void
294vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
295{
296 if (mp->retval)
297 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
298 getpid (), mp->context, format_api_error,
299 ntohl (mp->retval));
300
301 else
302 VDBG (1, "VCL<%d>: sid %u: unbind succeeded!", getpid (), mp->context);
303}
304
Florin Corasab2f6db2018-08-31 14:31:41 -0700305static void
306vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
307 mp)
308{
309 if (mp->retval)
310 clib_warning ("VCL<%d>: ERROR: sid %u: disconnect failed: %U",
311 getpid (), mp->context, format_api_error,
312 ntohl (mp->retval));
313}
314
315static void
Florin Coras6d4bb422018-09-04 22:07:27 -0700316vl_api_connect_session_reply_t_handler (vl_api_connect_sock_reply_t * mp)
Florin Corasab2f6db2018-08-31 14:31:41 -0700317{
318 if (mp->retval)
319 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed: %U",
320 getpid (), mp->context, format_api_error,
321 ntohl (mp->retval));
322}
323
Florin Coras99368312018-08-02 10:45:44 -0700324#define foreach_sock_msg \
325_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
326_(BIND_SOCK_REPLY, bind_sock_reply) \
327_(UNBIND_SOCK_REPLY, unbind_sock_reply) \
Florin Coras6d4bb422018-09-04 22:07:27 -0700328_(CONNECT_SESSION_REPLY, connect_session_reply) \
Florin Corasab2f6db2018-08-31 14:31:41 -0700329_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
Florin Coras99368312018-08-02 10:45:44 -0700330_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
331_(APPLICATION_DETACH_REPLY, application_detach_reply) \
332_(MAP_ANOTHER_SEGMENT, map_another_segment) \
333_(UNMAP_SEGMENT, unmap_segment) \
334_(APP_CUT_THROUGH_REGISTRATION_ADD, app_cut_through_registration_add) \
Florin Coras134a9962018-08-28 11:32:04 -0700335_(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
Florin Coras697faea2018-06-27 17:10:49 -0700336
337void
338vppcom_api_hookup (void)
339{
Florin Coras60116992018-08-27 09:52:18 -0700340#define _(N, n) \
341 vl_msg_api_set_handlers(VL_API_##N, #n, \
Florin Coras697faea2018-06-27 17:10:49 -0700342 vl_api_##n##_t_handler, \
343 vl_noop_handler, \
344 vl_api_##n##_t_endian, \
345 vl_api_##n##_t_print, \
346 sizeof(vl_api_##n##_t), 1);
347 foreach_sock_msg;
348#undef _
349}
350
351/*
352 * VPP-API message functions
353 */
354void
355vppcom_send_session_enable_disable (u8 is_enable)
356{
357 vl_api_session_enable_disable_t *bmp;
358 bmp = vl_msg_api_alloc (sizeof (*bmp));
359 memset (bmp, 0, sizeof (*bmp));
360
361 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
362 bmp->client_index = vcm->my_client_index;
363 bmp->context = htonl (0xfeedface);
364 bmp->is_enable = is_enable;
365 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
366}
367
368void
369vppcom_app_send_attach (void)
370{
371 vl_api_application_attach_t *bmp;
372 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
373 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
374 vcm->cfg.app_proxy_transport_udp);
375
376 bmp = vl_msg_api_alloc (sizeof (*bmp));
377 memset (bmp, 0, sizeof (*bmp));
378
379 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
380 bmp->client_index = vcm->my_client_index;
381 bmp->context = htonl (0xfeedface);
382 bmp->options[APP_OPTIONS_FLAGS] =
383 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
384 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
385 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
Florin Coras54693d22018-07-17 10:46:29 -0700386 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
Florin Coras99368312018-08-02 10:45:44 -0700387 APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS |
388 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
Florin Coras697faea2018-06-27 17:10:49 -0700389 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
390 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
391 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
392 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
393 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
394 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
395 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
396 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
397 vcm->cfg.preallocated_fifo_pairs;
398 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
399 if (nsid_len)
400 {
401 bmp->namespace_id_len = nsid_len;
Dave Barach178cf492018-11-13 16:34:13 -0500402 clib_memcpy_fast (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
Florin Coras697faea2018-06-27 17:10:49 -0700403 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
404 }
405 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
406}
407
408void
409vppcom_app_send_detach (void)
410{
411 vl_api_application_detach_t *bmp;
412 bmp = vl_msg_api_alloc (sizeof (*bmp));
413 memset (bmp, 0, sizeof (*bmp));
414
415 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
416 bmp->client_index = vcm->my_client_index;
417 bmp->context = htonl (0xfeedface);
418 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
419}
420
421void
Florin Coras134a9962018-08-28 11:32:04 -0700422vcl_send_app_worker_add_del (u8 is_add)
423{
424 vcl_worker_t *wrk = vcl_worker_get_current ();
425 vl_api_app_worker_add_del_t *mp;
426 u32 wrk_index = wrk->wrk_index;
427
428 mp = vl_msg_api_alloc (sizeof (*mp));
429 memset (mp, 0, sizeof (*mp));
430
431 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
432 mp->client_index = vcm->my_client_index;
Florin Corasc1f5a432018-11-20 11:31:26 -0800433 mp->app_index = clib_host_to_net_u32 (vcm->app_index);
Florin Coras134a9962018-08-28 11:32:04 -0700434 mp->context = wrk_index;
435 mp->is_add = is_add;
436 if (!is_add)
437 mp->wrk_index = clib_host_to_net_u32 (wrk_index);
438
439 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & mp);
440}
441
442void
443vppcom_send_connect_sock (vcl_session_t * session)
Florin Coras697faea2018-06-27 17:10:49 -0700444{
Florin Coras053a0e42018-11-13 15:52:38 -0800445 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700446 vl_api_connect_sock_t *cmp;
447
Florin Coras697faea2018-06-27 17:10:49 -0700448 cmp = vl_msg_api_alloc (sizeof (*cmp));
449 memset (cmp, 0, sizeof (*cmp));
450 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
451 cmp->client_index = vcm->my_client_index;
Florin Coras134a9962018-08-28 11:32:04 -0700452 cmp->context = session->session_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800453 cmp->wrk_index = wrk->vpp_wrk_index;
Florin Coras697faea2018-06-27 17:10:49 -0700454 cmp->is_ip4 = session->transport.is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500455 clib_memcpy_fast (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip));
Florin Coras697faea2018-06-27 17:10:49 -0700456 cmp->port = session->transport.rmt_port;
457 cmp->proto = session->session_type;
Dave Barach178cf492018-11-13 16:34:13 -0500458 clib_memcpy_fast (cmp->options, session->options, sizeof (cmp->options));
Florin Coras697faea2018-06-27 17:10:49 -0700459 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
460}
461
462void
Florin Corasab2f6db2018-08-31 14:31:41 -0700463vppcom_send_disconnect_session (u64 vpp_handle)
Florin Coras697faea2018-06-27 17:10:49 -0700464{
465 vl_api_disconnect_session_t *dmp;
466
Florin Coras697faea2018-06-27 17:10:49 -0700467 dmp = vl_msg_api_alloc (sizeof (*dmp));
468 memset (dmp, 0, sizeof (*dmp));
469 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
470 dmp->client_index = vcm->my_client_index;
471 dmp->handle = vpp_handle;
472 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
473}
474
475/* VPP combines bind and listen as one operation. VCL manages the separation
476 * of bind and listen locally via vppcom_session_bind() and
477 * vppcom_session_listen() */
478void
Florin Coras134a9962018-08-28 11:32:04 -0700479vppcom_send_bind_sock (vcl_session_t * session)
Florin Coras697faea2018-06-27 17:10:49 -0700480{
Florin Coras053a0e42018-11-13 15:52:38 -0800481 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700482 vl_api_bind_sock_t *bmp;
483
484 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
485 bmp = vl_msg_api_alloc (sizeof (*bmp));
486 memset (bmp, 0, sizeof (*bmp));
487
488 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
489 bmp->client_index = vcm->my_client_index;
Florin Coras134a9962018-08-28 11:32:04 -0700490 bmp->context = session->session_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800491 bmp->wrk_index = wrk->vpp_wrk_index;
Florin Coras697faea2018-06-27 17:10:49 -0700492 bmp->is_ip4 = session->transport.is_ip4;
Dave Barach178cf492018-11-13 16:34:13 -0500493 clib_memcpy_fast (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip));
Florin Coras697faea2018-06-27 17:10:49 -0700494 bmp->port = session->transport.lcl_port;
495 bmp->proto = session->session_type;
Dave Barach178cf492018-11-13 16:34:13 -0500496 clib_memcpy_fast (bmp->options, session->options, sizeof (bmp->options));
Florin Coras697faea2018-06-27 17:10:49 -0700497 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
498}
499
500void
501vppcom_send_unbind_sock (u64 vpp_handle)
502{
Florin Coras053a0e42018-11-13 15:52:38 -0800503 vcl_worker_t *wrk = vcl_worker_get_current ();
Florin Coras697faea2018-06-27 17:10:49 -0700504 vl_api_unbind_sock_t *ump;
505
506 ump = vl_msg_api_alloc (sizeof (*ump));
507 memset (ump, 0, sizeof (*ump));
508
509 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
510 ump->client_index = vcm->my_client_index;
Florin Coras053a0e42018-11-13 15:52:38 -0800511 ump->wrk_index = wrk->vpp_wrk_index;
Florin Coras697faea2018-06-27 17:10:49 -0700512 ump->handle = vpp_handle;
513 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
514}
515
516void
517vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
518{
519 vl_api_accept_session_reply_t *rmp;
520
521 rmp = vl_msg_api_alloc (sizeof (*rmp));
522 memset (rmp, 0, sizeof (*rmp));
523 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
524 rmp->retval = htonl (retval);
525 rmp->context = context;
526 rmp->handle = handle;
527 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
528}
529
530u32
531vcl_max_nsid_len (void)
532{
533 vl_api_application_attach_t *mp;
534 return (sizeof (mp->namespace_id) - 1);
535}
536
537void
538vppcom_init_error_string_table (void)
539{
540 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
541
542#define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
543 foreach_vnet_api_error;
544#undef _
545
546 hash_set (vcm->error_string_by_error_number, 99, "Misc");
547}
548
549int
550vppcom_connect_to_vpp (char *app_name)
551{
552 api_main_t *am = &api_main;
553 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Florin Coras697faea2018-06-27 17:10:49 -0700554
Florin Coras99368312018-08-02 10:45:44 -0700555 if (vcl_cfg->vpp_api_socket_name)
Florin Coras697faea2018-06-27 17:10:49 -0700556 {
Florin Coras99368312018-08-02 10:45:44 -0700557 if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name,
558 app_name, 0 /* default rx/tx buffer */ ))
559 {
Florin Coras053a0e42018-11-13 15:52:38 -0800560 VERR ("app (%s) socket connect failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700561 return VPPCOM_ECONNREFUSED;
562 }
563
564 if (vl_socket_client_init_shm (0))
565 {
Florin Coras053a0e42018-11-13 15:52:38 -0800566 VERR ("app (%s) init shm failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700567 return VPPCOM_ECONNREFUSED;
568 }
Florin Coras697faea2018-06-27 17:10:49 -0700569 }
570 else
571 {
Florin Coras99368312018-08-02 10:45:44 -0700572 if (!vcl_cfg->vpp_api_filename)
573 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
Florin Coras697faea2018-06-27 17:10:49 -0700574
Florin Coras053a0e42018-11-13 15:52:38 -0800575 VDBG (0, "app (%s) connecting to VPP api (%s)...",
Florin Coras99368312018-08-02 10:45:44 -0700576 app_name, vcl_cfg->vpp_api_filename);
577
578 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
579 app_name, vcm->cfg.vpp_api_q_length) < 0)
580 {
Florin Coras053a0e42018-11-13 15:52:38 -0800581 VERR ("app (%s) connect failed!", app_name);
Florin Coras99368312018-08-02 10:45:44 -0700582 return VPPCOM_ECONNREFUSED;
583 }
584
Florin Coras697faea2018-06-27 17:10:49 -0700585 }
586
Florin Coras99368312018-08-02 10:45:44 -0700587 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
588 vcm->my_client_index = (u32) am->my_client_index;
589 vcm->app_state = STATE_APP_CONN_VPP;
590
Florin Coras053a0e42018-11-13 15:52:38 -0800591 VDBG (0, "app (%s) is connected to VPP!", app_name);
Florin Coras697faea2018-06-27 17:10:49 -0700592 vcl_evt (VCL_EVT_INIT, vcm);
Florin Coras99368312018-08-02 10:45:44 -0700593 return VPPCOM_OK;
Florin Coras697faea2018-06-27 17:10:49 -0700594}
595
596/*
597 * fd.io coding-style-patch-verification: ON
598 *
599 * Local Variables:
600 * eval: (c-set-style "gnu")
601 * End:
602 */