blob: 0c46d8220fd40d59e8ba4748504f9cb84fca30a0 [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 Coras697faea2018-06-27 17:10:49 -0700134 vcm->app_state = STATE_APP_ATTACHED;
135}
136
137static void
Florin Coras134a9962018-08-28 11:32:04 -0700138vl_api_app_worker_add_del_reply_t_handler (vl_api_app_worker_add_del_reply_t *
139 mp)
140{
Florin Corasab2f6db2018-08-31 14:31:41 -0700141 int n_fds = 0, *fds = 0;
Florin Coras134a9962018-08-28 11:32:04 -0700142 vcl_worker_t *wrk;
Florin Corasab2f6db2018-08-31 14:31:41 -0700143 u32 wrk_index;
Florin Coras134a9962018-08-28 11:32:04 -0700144
145 if (mp->retval)
146 {
147 clib_warning ("VCL<%d>: add/del worker failed: %U", getpid (),
148 format_api_error, ntohl (mp->retval));
149 goto failed;
150 }
Florin Corasab2f6db2018-08-31 14:31:41 -0700151 wrk_index = clib_net_to_host_u32 (mp->wrk_index);
152 if (mp->context != wrk_index)
Florin Coras134a9962018-08-28 11:32:04 -0700153 {
154 clib_warning ("VCL<%d>: wrk numbering doesn't match ours: %u, vpp: %u",
Florin Corasab2f6db2018-08-31 14:31:41 -0700155 getpid (), mp->context, wrk_index);
Florin Coras134a9962018-08-28 11:32:04 -0700156 goto failed;
157 }
Florin Corasab2f6db2018-08-31 14:31:41 -0700158 wrk = vcl_worker_get (wrk_index);
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;
193 return;
194
195failed:
196 vcm->app_state = STATE_APP_FAILED;
197}
198
199static void
Florin Coras697faea2018-06-27 17:10:49 -0700200vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
201 mp)
202{
203 if (mp->retval)
204 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
205 ntohl (mp->retval));
206
207 vcm->app_state = STATE_APP_ENABLED;
208}
209
210static void
Florin Coras697faea2018-06-27 17:10:49 -0700211vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
212{
Florin Coras99368312018-08-02 10:45:44 -0700213 ssvm_segment_type_t seg_type = SSVM_SEGMENT_SHM;
214 int fd = -1;
Florin Coras697faea2018-06-27 17:10:49 -0700215
Florin Coras460dce62018-07-27 05:45:06 -0700216 vcm->mounting_segment = 1;
Florin Coras99368312018-08-02 10:45:44 -0700217
218 if (mp->fd_flags)
219 {
220 vl_socket_client_recv_fd_msg (&fd, 1, 5);
221 seg_type = SSVM_SEGMENT_MEMFD;
222 }
223
224 if (PREDICT_FALSE (ssvm_segment_attach ((char *) mp->segment_name,
225 seg_type, fd)))
Florin Coras697faea2018-06-27 17:10:49 -0700226 {
227 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
228 getpid (), mp->segment_name);
229 return;
230 }
231
232 VDBG (1, "VCL<%d>: mapped new segment '%s' size %d", getpid (),
233 mp->segment_name, mp->segment_size);
Florin Coras460dce62018-07-27 05:45:06 -0700234 vcm->mounting_segment = 0;
Florin Coras697faea2018-06-27 17:10:49 -0700235}
236
237static void
238vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
239{
240
241/*
242 * XXX Need segment_name to session_id hash,
243 * XXX - have sessionID by handle hash currently
244 */
245
246 VDBG (1, "Unmapped segment '%s'", mp->segment_name);
247}
248
249static void
Florin Coras99368312018-08-02 10:45:44 -0700250 vl_api_app_cut_through_registration_add_t_handler
251 (vl_api_app_cut_through_registration_add_t * mp)
252{
253 vcl_cut_through_registration_t *ctr;
254 u32 mqc_index = ~0;
Florin Coras134a9962018-08-28 11:32:04 -0700255 vcl_worker_t *wrk;
Florin Coras99368312018-08-02 10:45:44 -0700256 int *fds = 0;
257
258 if (mp->n_fds)
259 {
260 ASSERT (mp->n_fds == 2);
261 vec_validate (fds, mp->n_fds);
262 vl_socket_client_recv_fd_msg (fds, mp->n_fds, 5);
263 }
264
Florin Coras134a9962018-08-28 11:32:04 -0700265 wrk = vcl_worker_get (mp->wrk_index);
266 ctr = vcl_ct_registration_lock_and_alloc (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700267 ctr->mq = uword_to_pointer (mp->evt_q_address, svm_msg_q_t *);
268 ctr->peer_mq = uword_to_pointer (mp->peer_evt_q_address, svm_msg_q_t *);
Florin Coras134a9962018-08-28 11:32:04 -0700269 VDBG (0, "Adding ct registration %u", vcl_ct_registration_index (wrk, ctr));
Florin Coras99368312018-08-02 10:45:44 -0700270
Florin Coras15531972018-08-12 23:50:53 -0700271 if (mp->n_fds && (mp->fd_flags & SESSION_FD_F_MQ_EVENTFD))
Florin Coras99368312018-08-02 10:45:44 -0700272 {
273 svm_msg_q_set_consumer_eventfd (ctr->mq, fds[0]);
274 svm_msg_q_set_producer_eventfd (ctr->peer_mq, fds[1]);
Florin Coras134a9962018-08-28 11:32:04 -0700275 mqc_index = vcl_mq_epoll_add_evfd (wrk, ctr->mq);
Florin Coras99368312018-08-02 10:45:44 -0700276 ctr->epoll_evt_conn_index = mqc_index;
277 vec_free (fds);
278 }
Florin Coras134a9962018-08-28 11:32:04 -0700279 vcl_ct_registration_lookup_add (wrk, mp->evt_q_address,
280 vcl_ct_registration_index (wrk, ctr));
281 vcl_ct_registration_unlock (wrk);
Florin Coras99368312018-08-02 10:45:44 -0700282}
283
284static void
Florin Coras697faea2018-06-27 17:10:49 -0700285vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
286{
Florin Coras60116992018-08-27 09:52:18 -0700287 /* Expecting a similar message on mq. So ignore this */
288 VDBG (1, "VCL<%d>: bapi msg vpp handle 0x%llx, sid %u: bind retval: %u!",
289 getpid (), mp->handle, mp->context, mp->retval);
Florin Coras697faea2018-06-27 17:10:49 -0700290}
291
292static void
293vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
294{
295 if (mp->retval)
296 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
297 getpid (), mp->context, format_api_error,
298 ntohl (mp->retval));
299
300 else
301 VDBG (1, "VCL<%d>: sid %u: unbind succeeded!", getpid (), mp->context);
302}
303
Florin Corasab2f6db2018-08-31 14:31:41 -0700304static void
305vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
306 mp)
307{
308 if (mp->retval)
309 clib_warning ("VCL<%d>: ERROR: sid %u: disconnect failed: %U",
310 getpid (), mp->context, format_api_error,
311 ntohl (mp->retval));
312}
313
314static void
315vl_api_connect_sock_reply_t_handler (vl_api_connect_sock_reply_t * mp)
316{
317 if (mp->retval)
318 clib_warning ("VCL<%d>: ERROR: sid %u: connect failed: %U",
319 getpid (), mp->context, format_api_error,
320 ntohl (mp->retval));
321}
322
Florin Coras99368312018-08-02 10:45:44 -0700323#define foreach_sock_msg \
324_(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
325_(BIND_SOCK_REPLY, bind_sock_reply) \
326_(UNBIND_SOCK_REPLY, unbind_sock_reply) \
Florin Corasab2f6db2018-08-31 14:31:41 -0700327_(CONNECT_SOCK_REPLY, connect_sock_reply) \
328_(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
Florin Coras99368312018-08-02 10:45:44 -0700329_(APPLICATION_ATTACH_REPLY, application_attach_reply) \
330_(APPLICATION_DETACH_REPLY, application_detach_reply) \
331_(MAP_ANOTHER_SEGMENT, map_another_segment) \
332_(UNMAP_SEGMENT, unmap_segment) \
333_(APP_CUT_THROUGH_REGISTRATION_ADD, app_cut_through_registration_add) \
Florin Coras134a9962018-08-28 11:32:04 -0700334_(APP_WORKER_ADD_DEL_REPLY, app_worker_add_del_reply) \
Florin Coras697faea2018-06-27 17:10:49 -0700335
336void
337vppcom_api_hookup (void)
338{
Florin Coras60116992018-08-27 09:52:18 -0700339#define _(N, n) \
340 vl_msg_api_set_handlers(VL_API_##N, #n, \
Florin Coras697faea2018-06-27 17:10:49 -0700341 vl_api_##n##_t_handler, \
342 vl_noop_handler, \
343 vl_api_##n##_t_endian, \
344 vl_api_##n##_t_print, \
345 sizeof(vl_api_##n##_t), 1);
346 foreach_sock_msg;
347#undef _
348}
349
350/*
351 * VPP-API message functions
352 */
353void
354vppcom_send_session_enable_disable (u8 is_enable)
355{
356 vl_api_session_enable_disable_t *bmp;
357 bmp = vl_msg_api_alloc (sizeof (*bmp));
358 memset (bmp, 0, sizeof (*bmp));
359
360 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
361 bmp->client_index = vcm->my_client_index;
362 bmp->context = htonl (0xfeedface);
363 bmp->is_enable = is_enable;
364 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
365}
366
367void
368vppcom_app_send_attach (void)
369{
370 vl_api_application_attach_t *bmp;
371 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
372 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
373 vcm->cfg.app_proxy_transport_udp);
374
375 bmp = vl_msg_api_alloc (sizeof (*bmp));
376 memset (bmp, 0, sizeof (*bmp));
377
378 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
379 bmp->client_index = vcm->my_client_index;
380 bmp->context = htonl (0xfeedface);
381 bmp->options[APP_OPTIONS_FLAGS] =
382 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
383 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
384 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
Florin Coras54693d22018-07-17 10:46:29 -0700385 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0) |
Florin Coras99368312018-08-02 10:45:44 -0700386 APP_OPTIONS_FLAGS_USE_MQ_FOR_CTRL_MSGS |
387 (vcm->cfg.use_mq_eventfd ? APP_OPTIONS_FLAGS_EVT_MQ_USE_EVENTFD : 0);
Florin Coras697faea2018-06-27 17:10:49 -0700388 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
389 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
390 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
391 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
392 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
393 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
394 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
395 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
396 vcm->cfg.preallocated_fifo_pairs;
397 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
398 if (nsid_len)
399 {
400 bmp->namespace_id_len = nsid_len;
401 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
402 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
403 }
404 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
405}
406
407void
408vppcom_app_send_detach (void)
409{
410 vl_api_application_detach_t *bmp;
411 bmp = vl_msg_api_alloc (sizeof (*bmp));
412 memset (bmp, 0, sizeof (*bmp));
413
414 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
415 bmp->client_index = vcm->my_client_index;
416 bmp->context = htonl (0xfeedface);
417 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
418}
419
420void
Florin Coras134a9962018-08-28 11:32:04 -0700421vcl_send_app_worker_add_del (u8 is_add)
422{
423 vcl_worker_t *wrk = vcl_worker_get_current ();
424 vl_api_app_worker_add_del_t *mp;
425 u32 wrk_index = wrk->wrk_index;
426
427 mp = vl_msg_api_alloc (sizeof (*mp));
428 memset (mp, 0, sizeof (*mp));
429
430 mp->_vl_msg_id = ntohs (VL_API_APP_WORKER_ADD_DEL);
431 mp->client_index = vcm->my_client_index;
432 mp->context = wrk_index;
433 mp->is_add = is_add;
434 if (!is_add)
435 mp->wrk_index = clib_host_to_net_u32 (wrk_index);
436
437 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & mp);
438}
439
440void
441vppcom_send_connect_sock (vcl_session_t * session)
Florin Coras697faea2018-06-27 17:10:49 -0700442{
443 vl_api_connect_sock_t *cmp;
444
Florin Coras697faea2018-06-27 17:10:49 -0700445 cmp = vl_msg_api_alloc (sizeof (*cmp));
446 memset (cmp, 0, sizeof (*cmp));
447 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
448 cmp->client_index = vcm->my_client_index;
Florin Coras134a9962018-08-28 11:32:04 -0700449 cmp->context = session->session_index;
Florin Corasab2f6db2018-08-31 14:31:41 -0700450 cmp->wrk_index = vcl_get_worker_index ();
Florin Coras697faea2018-06-27 17:10:49 -0700451 cmp->is_ip4 = session->transport.is_ip4;
452 clib_memcpy (cmp->ip, &session->transport.rmt_ip, sizeof (cmp->ip));
453 cmp->port = session->transport.rmt_port;
454 cmp->proto = session->session_type;
455 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
456 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
457}
458
459void
Florin Corasab2f6db2018-08-31 14:31:41 -0700460vppcom_send_disconnect_session (u64 vpp_handle)
Florin Coras697faea2018-06-27 17:10:49 -0700461{
462 vl_api_disconnect_session_t *dmp;
463
Florin Coras697faea2018-06-27 17:10:49 -0700464 dmp = vl_msg_api_alloc (sizeof (*dmp));
465 memset (dmp, 0, sizeof (*dmp));
466 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
467 dmp->client_index = vcm->my_client_index;
468 dmp->handle = vpp_handle;
469 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
470}
471
472/* VPP combines bind and listen as one operation. VCL manages the separation
473 * of bind and listen locally via vppcom_session_bind() and
474 * vppcom_session_listen() */
475void
Florin Coras134a9962018-08-28 11:32:04 -0700476vppcom_send_bind_sock (vcl_session_t * session)
Florin Coras697faea2018-06-27 17:10:49 -0700477{
478 vl_api_bind_sock_t *bmp;
479
480 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
481 bmp = vl_msg_api_alloc (sizeof (*bmp));
482 memset (bmp, 0, sizeof (*bmp));
483
484 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
485 bmp->client_index = vcm->my_client_index;
Florin Coras134a9962018-08-28 11:32:04 -0700486 bmp->context = session->session_index;
Florin Corasab2f6db2018-08-31 14:31:41 -0700487 bmp->wrk_index = vcl_get_worker_index ();
Florin Coras697faea2018-06-27 17:10:49 -0700488 bmp->is_ip4 = session->transport.is_ip4;
489 clib_memcpy (bmp->ip, &session->transport.lcl_ip, sizeof (bmp->ip));
490 bmp->port = session->transport.lcl_port;
491 bmp->proto = session->session_type;
492 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
493 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
494}
495
496void
497vppcom_send_unbind_sock (u64 vpp_handle)
498{
499 vl_api_unbind_sock_t *ump;
500
501 ump = vl_msg_api_alloc (sizeof (*ump));
502 memset (ump, 0, sizeof (*ump));
503
504 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
505 ump->client_index = vcm->my_client_index;
Florin Corasab2f6db2018-08-31 14:31:41 -0700506 ump->wrk_index = vcl_get_worker_index ();
Florin Coras697faea2018-06-27 17:10:49 -0700507 ump->handle = vpp_handle;
508 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
509}
510
511void
512vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
513{
514 vl_api_accept_session_reply_t *rmp;
515
516 rmp = vl_msg_api_alloc (sizeof (*rmp));
517 memset (rmp, 0, sizeof (*rmp));
518 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
519 rmp->retval = htonl (retval);
520 rmp->context = context;
521 rmp->handle = handle;
522 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
523}
524
525u32
526vcl_max_nsid_len (void)
527{
528 vl_api_application_attach_t *mp;
529 return (sizeof (mp->namespace_id) - 1);
530}
531
532void
533vppcom_init_error_string_table (void)
534{
535 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
536
537#define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
538 foreach_vnet_api_error;
539#undef _
540
541 hash_set (vcm->error_string_by_error_number, 99, "Misc");
542}
543
544int
545vppcom_connect_to_vpp (char *app_name)
546{
547 api_main_t *am = &api_main;
548 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
Florin Coras697faea2018-06-27 17:10:49 -0700549
Florin Coras99368312018-08-02 10:45:44 -0700550 if (vcl_cfg->vpp_api_socket_name)
Florin Coras697faea2018-06-27 17:10:49 -0700551 {
Florin Coras99368312018-08-02 10:45:44 -0700552 if (vl_socket_client_connect ((char *) vcl_cfg->vpp_api_socket_name,
553 app_name, 0 /* default rx/tx buffer */ ))
554 {
555 clib_warning ("VCL<%d>: app (%s) socket connect failed!",
556 getpid (), app_name);
557 return VPPCOM_ECONNREFUSED;
558 }
559
560 if (vl_socket_client_init_shm (0))
561 {
562 clib_warning ("VCL<%d>: app (%s) init shm failed!",
563 getpid (), app_name);
564 return VPPCOM_ECONNREFUSED;
565 }
Florin Coras697faea2018-06-27 17:10:49 -0700566 }
567 else
568 {
Florin Coras99368312018-08-02 10:45:44 -0700569 if (!vcl_cfg->vpp_api_filename)
570 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
Florin Coras697faea2018-06-27 17:10:49 -0700571
Florin Coras99368312018-08-02 10:45:44 -0700572 VDBG (0, "VCL<%d>: app (%s) connecting to VPP api (%s)...", getpid (),
573 app_name, vcl_cfg->vpp_api_filename);
574
575 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename,
576 app_name, vcm->cfg.vpp_api_q_length) < 0)
577 {
578 clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (),
579 app_name);
580 return VPPCOM_ECONNREFUSED;
581 }
582
Florin Coras697faea2018-06-27 17:10:49 -0700583 }
584
Florin Coras99368312018-08-02 10:45:44 -0700585 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
586 vcm->my_client_index = (u32) am->my_client_index;
587 vcm->app_state = STATE_APP_CONN_VPP;
588
589 VDBG (0, "VCL<%d>: app (%s) is connected to VPP!", getpid (), app_name);
590
Florin Coras697faea2018-06-27 17:10:49 -0700591 vcl_evt (VCL_EVT_INIT, vcm);
Florin Coras99368312018-08-02 10:45:44 -0700592 return VPPCOM_OK;
Florin Coras697faea2018-06-27 17:10:49 -0700593}
594
595/*
596 * fd.io coding-style-patch-verification: ON
597 *
598 * Local Variables:
599 * eval: (c-set-style "gnu")
600 * End:
601 */