2 * Copyright (c) 2017 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <svm/svm_fifo_segment.h>
20 #include <vlibmemory/api.h>
21 #include <vpp/api/vpe_msg_enum.h>
22 #include <vnet/session/application_interface.h>
23 #include <vcl/vppcom.h>
24 #include <vlib/unix/unix.h>
25 #include <vppinfra/vec_bootstrap.h>
27 #define vl_typedefs /* define message structures */
28 #include <vpp/api/vpe_all_api_h.h>
31 /* declare message handlers for each api */
33 #define vl_endianfun /* define message structures */
34 #include <vpp/api/vpe_all_api_h.h>
37 /* instantiate all the print functions we know about */
38 #define vl_print(handle, ...)
40 #include <vpp/api/vpe_all_api_h.h>
44 /* Set VPPCOM_DEBUG 2 for connection debug, 3 for read/write debug output */
45 #define VPPCOM_DEBUG 1
47 #define VPPCOM_DEBUG 0
51 * VPPCOM Private definitions and functions.
71 typedef struct epoll_event vppcom_epoll_event_t;
78 vppcom_epoll_event_t ev;
79 #define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT)
91 volatile session_state_t state;
93 svm_fifo_t *server_rx_fifo;
94 svm_fifo_t *server_tx_fifo;
96 u64 vpp_session_handle;
97 unix_shared_memory_queue_t *vpp_event_queue;
99 /* Socket configuration state */
100 /* TBD: covert 'is_*' vars to bit in u8 flags; */
110 vppcom_ip46_t lcl_addr;
111 vppcom_ip46_t peer_addr;
112 u16 lcl_port; // network order
113 u16 peer_port; // network order
115 u64 client_queue_address;
119 typedef struct vppcom_cfg_t_
124 u32 add_segment_size;
125 u32 preallocated_fifo_pairs;
128 u32 event_queue_size;
129 u32 listen_queue_size;
130 u8 app_proxy_transport_tcp;
131 u8 app_proxy_transport_udp;
135 u64 namespace_secret;
141 typedef struct vppcom_main_t_
144 u32 *client_session_index_fifo;
145 volatile u32 bind_session_index;
148 /* vpe input queue */
149 unix_shared_memory_queue_t *vl_input_queue;
151 /* API client handle */
155 clib_spinlock_t sessions_lockp;
158 /* Hash table for disconnect processing */
159 uword *session_index_by_vpp_handles;
162 clib_bitmap_t *rd_bitmap;
163 clib_bitmap_t *wr_bitmap;
164 clib_bitmap_t *ex_bitmap;
166 /* Our event queue */
167 unix_shared_memory_queue_t *app_event_queue;
169 /* unique segment name counter */
170 u32 unique_segment_index;
174 /* For deadman timers */
175 clib_time_t clib_time;
177 /* State of the connection, shared between msg RX thread and main thread */
178 volatile app_state_t app_state;
182 /* VNET_API_ERROR_FOO -> "Foo" hash table */
183 uword *error_string_by_error_number;
186 vppcom_main_t vppcom_main = {.my_client_index = ~0 };
189 vppcom_app_state_str (app_state_t state)
195 case STATE_APP_START:
196 st = "STATE_APP_START";
199 case STATE_APP_CONN_VPP:
200 st = "STATE_APP_CONN_VPP";
203 case STATE_APP_ENABLED:
204 st = "STATE_APP_ENABLED";
207 case STATE_APP_ATTACHED:
208 st = "STATE_APP_ATTACHED";
212 st = "UNKNOWN_APP_STATE";
220 vppcom_session_state_str (session_state_t state)
231 st = "STATE_CONNECT";
242 case STATE_DISCONNECT:
243 st = "STATE_DISCONNECT";
251 st = "UNKNOWN_STATE";
259 * VPPCOM Utility Functions
262 vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
264 vppcom_main_t *vcm = &vppcom_main;
266 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
267 if (PREDICT_FALSE ((session_index == ~0) ||
268 pool_is_free_index (vcm->sessions, session_index)))
270 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
271 vcm->my_pid, session_index);
272 return VPPCOM_EBADFD;
274 *sess = pool_elt_at_index (vcm->sessions, session_index);
279 vppcom_connect_to_vpp (char *app_name)
281 api_main_t *am = &api_main;
282 vppcom_main_t *vcm = &vppcom_main;
284 if (VPPCOM_DEBUG > 0)
285 printf ("\nConnecting to VPP api...");
286 if (vl_client_connect_to_vlib ("/vpe-api", app_name, 32) < 0)
288 clib_warning ("[%d] connect to vpp (%s) failed!",
289 vcm->my_pid, app_name);
290 return VPPCOM_ECONNREFUSED;
293 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
294 vcm->my_client_index = am->my_client_index;
295 if (VPPCOM_DEBUG > 0)
296 printf (" connected!\n");
298 vcm->app_state = STATE_APP_CONN_VPP;
303 format_api_error (u8 * s, va_list * args)
305 vppcom_main_t *vcm = &vppcom_main;
306 i32 error = va_arg (*args, u32);
309 p = hash_get (vcm->error_string_by_error_number, -error);
312 s = format (s, "%s (%d)", p[0], error);
314 s = format (s, "%d", error);
319 vppcom_init_error_string_table (void)
321 vppcom_main_t *vcm = &vppcom_main;
323 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
325 #define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s);
326 foreach_vnet_api_error;
329 hash_set (vcm->error_string_by_error_number, 99, "Misc");
333 vppcom_wait_for_app_state_change (app_state_t app_state)
335 vppcom_main_t *vcm = &vppcom_main;
336 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
338 while (clib_time_now (&vcm->clib_time) < timeout)
340 if (vcm->app_state == app_state)
343 if (VPPCOM_DEBUG > 0)
344 clib_warning ("[%d] timeout waiting for state %s (%d)", vcm->my_pid,
345 vppcom_app_state_str (app_state), app_state);
346 return VPPCOM_ETIMEDOUT;
350 vppcom_wait_for_session_state_change (u32 session_index,
351 session_state_t state,
354 vppcom_main_t *vcm = &vppcom_main;
355 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
356 session_t *volatile session;
361 clib_spinlock_lock (&vcm->sessions_lockp);
362 rv = vppcom_session_at_index (session_index, &session);
363 if (PREDICT_FALSE (rv))
365 clib_spinlock_unlock (&vcm->sessions_lockp);
368 if (session->state == state)
370 clib_spinlock_unlock (&vcm->sessions_lockp);
373 clib_spinlock_unlock (&vcm->sessions_lockp);
375 while (clib_time_now (&vcm->clib_time) < timeout);
377 if (VPPCOM_DEBUG > 0)
378 clib_warning ("[%d] timeout waiting for state %s (%d)", vcm->my_pid,
379 vppcom_session_state_str (state), state);
380 return VPPCOM_ETIMEDOUT;
384 vppcom_wait_for_client_session_index (f64 wait_for_time)
386 vppcom_main_t *vcm = &vppcom_main;
387 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
391 if (clib_fifo_elts (vcm->client_session_index_fifo))
394 while (clib_time_now (&vcm->clib_time) < timeout);
396 if (wait_for_time == 0)
397 return VPPCOM_EAGAIN;
399 if (VPPCOM_DEBUG > 0)
400 clib_warning ("[%d] timeout waiting for client_session_index",
402 return VPPCOM_ETIMEDOUT;
406 * VPP-API message functions
409 vppcom_send_session_enable_disable (u8 is_enable)
411 vppcom_main_t *vcm = &vppcom_main;
412 vl_api_session_enable_disable_t *bmp;
413 bmp = vl_msg_api_alloc (sizeof (*bmp));
414 memset (bmp, 0, sizeof (*bmp));
416 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
417 bmp->client_index = vcm->my_client_index;
418 bmp->context = htonl (0xfeedface);
419 bmp->is_enable = is_enable;
420 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
424 vppcom_app_session_enable (void)
426 vppcom_main_t *vcm = &vppcom_main;
429 if (vcm->app_state != STATE_APP_ENABLED)
431 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
432 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
433 if (PREDICT_FALSE (rv))
435 if (VPPCOM_DEBUG > 0)
436 clib_warning ("[%d] Session enable timed out, rv = %s (%d)",
437 vcm->my_pid, vppcom_retval_str (rv), rv);
445 vl_api_session_enable_disable_reply_t_handler
446 (vl_api_session_enable_disable_reply_t * mp)
448 vppcom_main_t *vcm = &vppcom_main;
452 clib_warning ("[%d] session_enable_disable failed: %U", vcm->my_pid,
453 format_api_error, ntohl (mp->retval));
456 vcm->app_state = STATE_APP_ENABLED;
460 vppcom_app_send_attach (void)
462 vppcom_main_t *vcm = &vppcom_main;
463 vl_api_application_attach_t *bmp;
464 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
465 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
466 vcm->cfg.app_proxy_transport_udp);
468 bmp = vl_msg_api_alloc (sizeof (*bmp));
469 memset (bmp, 0, sizeof (*bmp));
471 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
472 bmp->client_index = vcm->my_client_index;
473 bmp->context = htonl (0xfeedface);
474 bmp->options[APP_OPTIONS_FLAGS] =
475 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
476 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
477 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
478 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
479 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
480 (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
481 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
482 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
483 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
484 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
485 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
488 bmp->namespace_id_len = nsid_len;
489 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
490 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
492 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
496 vppcom_app_attach (void)
498 vppcom_main_t *vcm = &vppcom_main;
501 vppcom_app_send_attach ();
502 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
503 if (PREDICT_FALSE (rv))
505 if (VPPCOM_DEBUG > 0)
506 clib_warning ("[%d] application attach timed out, rv = %s (%d)",
507 vcm->my_pid, vppcom_retval_str (rv), rv);
514 vppcom_app_detach (void)
516 vppcom_main_t *vcm = &vppcom_main;
517 vl_api_application_detach_t *bmp;
518 bmp = vl_msg_api_alloc (sizeof (*bmp));
519 memset (bmp, 0, sizeof (*bmp));
521 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
522 bmp->client_index = vcm->my_client_index;
523 bmp->context = htonl (0xfeedface);
524 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
528 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
531 vppcom_main_t *vcm = &vppcom_main;
532 static svm_fifo_segment_create_args_t _a;
533 svm_fifo_segment_create_args_t *a = &_a;
536 memset (a, 0, sizeof (*a));
539 clib_warning ("[%d] attach failed: %U", vcm->my_pid,
540 format_api_error, ntohl (mp->retval));
544 if (mp->segment_name_length == 0)
546 clib_warning ("[%d] segment_name_length zero", vcm->my_pid);
550 a->segment_name = (char *) mp->segment_name;
551 a->segment_size = mp->segment_size;
553 ASSERT (mp->app_event_queue_address);
555 /* Attach to the segment vpp created */
556 rv = svm_fifo_segment_attach (a);
557 vec_reset_length (a->new_segment_indices);
558 if (PREDICT_FALSE (rv))
560 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", vcm->my_pid,
565 vcm->app_event_queue =
566 uword_to_pointer (mp->app_event_queue_address,
567 unix_shared_memory_queue_t *);
569 vcm->app_state = STATE_APP_ATTACHED;
573 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
576 vppcom_main_t *vcm = &vppcom_main;
579 clib_warning ("[%d] detach failed: %U", vcm->my_pid, format_api_error,
582 vcm->app_state = STATE_APP_ENABLED;
586 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
589 vppcom_main_t *vcm = &vppcom_main;
592 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
595 session_t *session = 0;
597 clib_spinlock_lock (&vcm->sessions_lockp);
598 rv = vppcom_session_at_index (p[0], &session);
599 if (PREDICT_FALSE (rv))
601 if (VPPCOM_DEBUG > 1)
602 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
605 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
606 session->state = STATE_DISCONNECT;
607 clib_spinlock_unlock (&vcm->sessions_lockp);
611 if (VPPCOM_DEBUG > 1)
612 clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
617 clib_warning ("[%d] disconnect_session failed: %U", vcm->my_pid,
618 format_api_error, ntohl (mp->retval));
622 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
624 vppcom_main_t *vcm = &vppcom_main;
625 static svm_fifo_segment_create_args_t _a;
626 svm_fifo_segment_create_args_t *a = &_a;
629 memset (a, 0, sizeof (*a));
630 a->segment_name = (char *) mp->segment_name;
631 a->segment_size = mp->segment_size;
632 /* Attach to the segment vpp created */
633 rv = svm_fifo_segment_attach (a);
634 vec_reset_length (a->new_segment_indices);
635 if (PREDICT_FALSE (rv))
637 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed",
638 vcm->my_pid, mp->segment_name);
641 if (VPPCOM_DEBUG > 1)
642 clib_warning ("[%d] mapped new segment '%s' size %d", vcm->my_pid,
643 mp->segment_name, mp->segment_size);
647 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
649 vppcom_main_t *vcm = &vppcom_main;
650 session_t *session = 0;
651 vl_api_disconnect_session_reply_t *rmp;
655 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
659 clib_spinlock_lock (&vcm->sessions_lockp);
660 rval = vppcom_session_at_index (p[0], &session);
661 if (PREDICT_FALSE (rval))
663 if (VPPCOM_DEBUG > 1)
664 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
668 pool_put (vcm->sessions, session);
669 clib_spinlock_unlock (&vcm->sessions_lockp);
670 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
674 clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
679 rmp = vl_msg_api_alloc (sizeof (*rmp));
680 memset (rmp, 0, sizeof (*rmp));
682 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
683 rmp->retval = htonl (rv);
684 rmp->handle = mp->handle;
685 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
689 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
691 vppcom_main_t *vcm = &vppcom_main;
692 session_t *session = 0;
693 vl_api_reset_session_reply_t *rmp;
697 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
701 clib_spinlock_lock (&vcm->sessions_lockp);
702 rval = vppcom_session_at_index (p[0], &session);
703 if (PREDICT_FALSE (rval))
705 if (VPPCOM_DEBUG > 1)
706 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
710 pool_put (vcm->sessions, session);
711 clib_spinlock_unlock (&vcm->sessions_lockp);
712 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
716 clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
721 rmp = vl_msg_api_alloc (sizeof (*rmp));
722 memset (rmp, 0, sizeof (*rmp));
723 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
724 rmp->retval = htonl (rv);
725 rmp->handle = mp->handle;
726 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
730 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
732 vppcom_main_t *vcm = &vppcom_main;
735 svm_fifo_t *rx_fifo, *tx_fifo;
741 clib_warning ("[%d] connect failed: %U", vcm->my_pid, format_api_error,
746 session_index = mp->context;
747 if (VPPCOM_DEBUG > 1)
748 clib_warning ("[%d] session_index = %d 0x%08x", vcm->my_pid,
749 session_index, session_index);
751 clib_spinlock_lock (&vcm->sessions_lockp);
752 if (pool_is_free_index (vcm->sessions, session_index))
754 clib_spinlock_unlock (&vcm->sessions_lockp);
755 if (VPPCOM_DEBUG > 1)
756 clib_warning ("[%d] invalid session, sid %d is closed!",
757 vcm->my_pid, session_index);
761 /* We've been redirected */
762 if (mp->segment_name_length > 0)
764 static svm_fifo_segment_create_args_t _a;
765 svm_fifo_segment_create_args_t *a = &_a;
768 memset (a, 0, sizeof (*a));
769 a->segment_name = (char *) mp->segment_name;
770 if (VPPCOM_DEBUG > 1)
771 clib_warning ("[%d] cut-thru segment: %s", vcm->my_pid,
773 rv = svm_fifo_segment_attach (a);
774 vec_reset_length (a->new_segment_indices);
775 if (PREDICT_FALSE (rv))
777 clib_spinlock_unlock (&vcm->sessions_lockp);
778 clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed",
779 vcm->my_pid, a->segment_name);
787 if (VPPCOM_DEBUG > 1)
788 clib_warning ("[%d] client sid %d", vcm->my_pid, session_index);
790 session = pool_elt_at_index (vcm->sessions, session_index);
791 session->is_cut_thru = is_cut_thru;
792 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
793 unix_shared_memory_queue_t *);
795 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
796 rx_fifo->client_session_index = session_index;
797 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
798 tx_fifo->client_session_index = session_index;
800 session->server_rx_fifo = rx_fifo;
801 session->server_tx_fifo = tx_fifo;
802 session->vpp_session_handle = mp->handle;
803 session->state = STATE_CONNECT;
805 /* Add it to lookup table */
806 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
807 clib_spinlock_unlock (&vcm->sessions_lockp);
811 vppcom_send_connect_sock (session_t * session, u32 session_index)
813 vppcom_main_t *vcm = &vppcom_main;
814 vl_api_connect_sock_t *cmp;
816 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
817 session->is_server = 0;
818 cmp = vl_msg_api_alloc (sizeof (*cmp));
819 memset (cmp, 0, sizeof (*cmp));
820 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
821 cmp->client_index = vcm->my_client_index;
822 cmp->context = session_index;
824 if (VPPCOM_DEBUG > 1)
825 clib_warning ("[%d] session_index = %d 0x%08x",
826 vcm->my_pid, session_index, session_index);
828 cmp->vrf = session->vrf;
829 cmp->is_ip4 = session->peer_addr.is_ip4;
830 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
831 cmp->port = session->peer_port;
832 cmp->proto = session->proto;
833 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
834 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
838 vppcom_send_disconnect (session_t * session)
840 vppcom_main_t *vcm = &vppcom_main;
841 vl_api_disconnect_session_t *dmp;
843 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
844 dmp = vl_msg_api_alloc (sizeof (*dmp));
845 memset (dmp, 0, sizeof (*dmp));
846 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
847 dmp->client_index = vcm->my_client_index;
848 dmp->handle = session->vpp_session_handle;
849 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
853 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
855 vppcom_main_t *vcm = &vppcom_main;
856 session_t *session = 0;
860 clib_warning ("[%d] bind failed: %U", vcm->my_pid, format_api_error,
863 ASSERT (vcm->bind_session_index != ~0);
865 clib_spinlock_lock (&vcm->sessions_lockp);
866 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
869 session->vpp_session_handle = mp->handle;
870 hash_set (vcm->session_index_by_vpp_handles, mp->handle,
871 vcm->bind_session_index);
872 session->state = mp->retval ? STATE_FAILED : STATE_LISTEN;
873 vcm->bind_session_index = ~0;
875 clib_spinlock_unlock (&vcm->sessions_lockp);
879 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
881 vppcom_main_t *vcm = &vppcom_main;
882 session_t *session = 0;
885 clib_spinlock_lock (&vcm->sessions_lockp);
886 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
889 if ((VPPCOM_DEBUG > 1) && (mp->retval))
890 clib_warning ("[%d] unbind failed: %U", vcm->my_pid, format_api_error,
893 vcm->bind_session_index = ~0;
894 session->state = STATE_START;
896 clib_spinlock_unlock (&vcm->sessions_lockp);
900 format_ip4_address (u8 * s, va_list * args)
902 u8 *a = va_arg (*args, u8 *);
903 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
907 format_ip6_address (u8 * s, va_list * args)
909 ip6_address_t *a = va_arg (*args, ip6_address_t *);
910 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
912 i_max_n_zero = ARRAY_LEN (a->as_u16);
914 i_first_zero = i_max_n_zero;
916 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
918 u32 is_zero = a->as_u16[i] == 0;
919 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
925 if ((!is_zero && n_zeros > max_n_zeros)
926 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
928 i_max_n_zero = i_first_zero;
929 max_n_zeros = n_zeros;
930 i_first_zero = ARRAY_LEN (a->as_u16);
935 last_double_colon = 0;
936 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
938 if (i == i_max_n_zero && max_n_zeros > 1)
940 s = format (s, "::");
941 i += max_n_zeros - 1;
942 last_double_colon = 1;
946 s = format (s, "%s%x",
947 (last_double_colon || i == 0) ? "" : ":",
948 clib_net_to_host_u16 (a->as_u16[i]));
949 last_double_colon = 0;
956 /* Format an IP46 address. */
958 format_ip46_address (u8 * s, va_list * args)
960 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
961 ip46_type_t type = va_arg (*args, ip46_type_t);
967 is_ip4 = ip46_address_is_ip4 (ip46);
978 format (s, "%U", format_ip4_address, &ip46->ip4) :
979 format (s, "%U", format_ip6_address, &ip46->ip6);
983 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
985 vppcom_main_t *vcm = &vppcom_main;
986 vl_api_accept_session_reply_t *rmp;
987 svm_fifo_t *rx_fifo, *tx_fifo;
992 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
994 clib_warning ("[%d] client session queue is full!", vcm->my_pid);
995 rv = VNET_API_ERROR_QUEUE_FULL;
999 if (VPPCOM_DEBUG > 1)
1001 u8 *ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
1002 clib_warning ("[%d] accepted session from: %s:%d", vcm->my_pid, ip_str,
1003 clib_net_to_host_u16 (mp->port));
1007 clib_spinlock_lock (&vcm->sessions_lockp);
1008 /* Allocate local session and set it up */
1009 pool_get (vcm->sessions, session);
1010 memset (session, 0, sizeof (*session));
1011 session_index = session - vcm->sessions;
1013 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1014 rx_fifo->client_session_index = session_index;
1015 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1016 tx_fifo->client_session_index = session_index;
1018 session->server_rx_fifo = rx_fifo;
1019 session->server_tx_fifo = tx_fifo;
1020 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1021 unix_shared_memory_queue_t *);
1022 session->state = STATE_ACCEPT;
1023 session->is_cut_thru = 0;
1024 session->is_server = 1;
1025 session->peer_port = mp->port;
1026 session->peer_addr.is_ip4 = mp->is_ip4;
1027 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1028 sizeof (session->peer_addr.ip46));
1030 /* Add it to lookup table */
1031 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1033 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1034 clib_spinlock_unlock (&vcm->sessions_lockp);
1037 * Send accept reply to vpp
1040 rmp = vl_msg_api_alloc (sizeof (*rmp));
1041 memset (rmp, 0, sizeof (*rmp));
1042 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1043 rmp->retval = htonl (rv);
1044 rmp->handle = mp->handle;
1045 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1049 * Acting as server for redirected connect requests
1052 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1054 static svm_fifo_segment_create_args_t _a;
1055 svm_fifo_segment_create_args_t *a = &_a;
1056 vppcom_main_t *vcm = &vppcom_main;
1058 svm_fifo_segment_private_t *seg;
1059 unix_shared_memory_queue_t *client_q;
1060 vl_api_connect_session_reply_t *rmp;
1061 session_t *session = 0;
1063 svm_fifo_t *rx_fifo;
1064 svm_fifo_t *tx_fifo;
1065 unix_shared_memory_queue_t *event_q = 0;
1067 clib_spinlock_lock (&vcm->sessions_lockp);
1068 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1070 if (VPPCOM_DEBUG > 1)
1071 clib_warning ("[%d] client session queue is full!", vcm->my_pid);
1072 rv = VNET_API_ERROR_QUEUE_FULL;
1073 clib_spinlock_unlock (&vcm->sessions_lockp);
1077 /* Create the segment */
1078 memset (a, 0, sizeof (*a));
1079 a->segment_name = (char *) format ((u8 *) a->segment_name, "%d:segment%d%c",
1080 vcm->my_pid, vcm->unique_segment_index++,
1082 a->segment_size = vcm->cfg.segment_size;
1083 a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
1084 a->rx_fifo_size = vcm->cfg.rx_fifo_size;
1085 a->tx_fifo_size = vcm->cfg.tx_fifo_size;
1087 rv = svm_fifo_segment_create (a);
1088 if (PREDICT_FALSE (rv))
1090 if (VPPCOM_DEBUG > 1)
1091 clib_warning ("[%d] svm_fifo_segment_create ('%s') failed",
1092 vcm->my_pid, a->segment_name);
1093 vec_reset_length (a->new_segment_indices);
1094 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
1098 if (VPPCOM_DEBUG > 1)
1099 clib_warning ("[%d] created segment '%s'", vcm->my_pid, a->segment_name);
1101 pool_get (vcm->sessions, session);
1102 memset (session, 0, sizeof (*session));
1103 session_index = session - vcm->sessions;
1105 session->sm_seg_index = a->new_segment_indices[0];
1106 vec_reset_length (a->new_segment_indices);
1108 seg = svm_fifo_segment_get_segment (session->sm_seg_index);
1109 rx_fifo = session->server_rx_fifo =
1110 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
1111 FIFO_SEGMENT_RX_FREELIST);
1112 if (PREDICT_FALSE (!session->server_rx_fifo))
1114 svm_fifo_segment_delete (seg);
1115 clib_warning ("[%d] rx fifo alloc failed, size %ld (0x%lx)",
1116 vcm->my_pid, vcm->cfg.rx_fifo_size,
1117 vcm->cfg.rx_fifo_size);
1118 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
1119 clib_spinlock_unlock (&vcm->sessions_lockp);
1123 tx_fifo = session->server_tx_fifo =
1124 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
1125 FIFO_SEGMENT_TX_FREELIST);
1126 if (PREDICT_FALSE (!session->server_tx_fifo))
1128 svm_fifo_segment_delete (seg);
1129 if (VPPCOM_DEBUG > 1)
1130 clib_warning ("[%d] tx fifo alloc failed, size %ld (0x%lx)",
1131 vcm->my_pid, vcm->cfg.tx_fifo_size,
1132 vcm->cfg.tx_fifo_size);
1133 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
1134 clib_spinlock_unlock (&vcm->sessions_lockp);
1138 session->server_rx_fifo->master_session_index = session_index;
1139 session->server_tx_fifo->master_session_index = session_index;
1140 session->client_queue_address = mp->client_queue_address;
1141 session->is_cut_thru = 1;
1142 session->is_server = 1;
1143 session->peer_port = mp->port;
1144 session->peer_addr.is_ip4 = mp->is_ip4;
1145 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1146 sizeof (session->peer_addr.ip46));
1149 ssvm_shared_header_t *sh = seg->ssvm.sh;
1151 ssvm_lock_non_recursive (sh, 1);
1152 oldheap = ssvm_push_heap (sh);
1153 event_q = session->vpp_event_queue =
1154 unix_shared_memory_queue_init (vcm->cfg.event_queue_size,
1155 sizeof (session_fifo_event_t),
1156 vcm->my_pid, 0 /* signal not sent */ );
1157 ssvm_pop_heap (oldheap);
1158 ssvm_unlock_non_recursive (sh);
1161 session->state = STATE_ACCEPT;
1162 if (VPPCOM_DEBUG > 1)
1163 clib_warning ("[%d] Connected cut-thru to client: sid %d",
1164 vcm->my_pid, session_index);
1165 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1166 clib_spinlock_unlock (&vcm->sessions_lockp);
1169 rmp = vl_msg_api_alloc (sizeof (*rmp));
1170 memset (rmp, 0, sizeof (*rmp));
1172 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
1173 rmp->context = mp->context;
1174 rmp->retval = htonl (rv);
1175 rmp->segment_name_length = vec_len (a->segment_name);
1176 clib_memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name));
1177 vec_reset_length (a->segment_name);
1181 rmp->vpp_event_queue_address = pointer_to_uword (event_q);
1182 rmp->server_rx_fifo = pointer_to_uword (rx_fifo);
1183 rmp->server_tx_fifo = pointer_to_uword (tx_fifo);
1186 uword_to_pointer (mp->client_queue_address, unix_shared_memory_queue_t *);
1189 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
1193 vppcom_send_bind_sock (session_t * session)
1195 vppcom_main_t *vcm = &vppcom_main;
1196 vl_api_bind_sock_t *bmp;
1198 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1199 session->is_server = 1;
1200 bmp = vl_msg_api_alloc (sizeof (*bmp));
1201 memset (bmp, 0, sizeof (*bmp));
1203 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1204 bmp->client_index = vcm->my_client_index;
1205 bmp->context = htonl (0xfeedface);
1206 bmp->vrf = session->vrf;
1207 bmp->is_ip4 = session->lcl_addr.is_ip4;
1208 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1209 bmp->port = session->lcl_port;
1210 bmp->proto = session->proto;
1211 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1212 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1216 vppcom_send_unbind_sock (u32 session_index)
1218 vppcom_main_t *vcm = &vppcom_main;
1219 vl_api_unbind_sock_t *ump;
1220 session_t *session = 0;
1223 clib_spinlock_lock (&vcm->sessions_lockp);
1224 rv = vppcom_session_at_index (session_index, &session);
1225 if (PREDICT_FALSE (rv))
1227 clib_spinlock_unlock (&vcm->sessions_lockp);
1228 if (VPPCOM_DEBUG > 0)
1229 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1230 vcm->my_pid, session_index);
1234 ump = vl_msg_api_alloc (sizeof (*ump));
1235 memset (ump, 0, sizeof (*ump));
1237 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1238 ump->client_index = vcm->my_client_index;
1239 ump->handle = session->vpp_session_handle;
1240 clib_spinlock_unlock (&vcm->sessions_lockp);
1241 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1245 vppcom_session_unbind_cut_thru (session_t * session)
1247 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1248 svm_fifo_segment_private_t *seg;
1251 seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
1252 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1253 FIFO_SEGMENT_RX_FREELIST);
1254 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1255 FIFO_SEGMENT_TX_FREELIST);
1256 svm_fifo_segment_delete (seg);
1262 vppcom_session_unbind (u32 session_index)
1264 vppcom_main_t *vcm = &vppcom_main;
1267 clib_spinlock_lock (&vcm->sessions_lockp);
1268 if (PREDICT_FALSE (pool_is_free_index (vcm->sessions, session_index)))
1270 clib_spinlock_unlock (&vcm->sessions_lockp);
1271 if (VPPCOM_DEBUG > 1)
1272 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1273 vcm->my_pid, session_index);
1274 return VPPCOM_EBADFD;
1276 clib_spinlock_unlock (&vcm->sessions_lockp);
1278 vcm->bind_session_index = session_index;
1279 vppcom_send_unbind_sock (session_index);
1280 rv = vppcom_wait_for_session_state_change (session_index, STATE_START,
1281 vcm->cfg.session_timeout);
1282 if (PREDICT_FALSE (rv))
1284 vcm->bind_session_index = ~0;
1285 if (VPPCOM_DEBUG > 0)
1286 clib_warning ("[%d] server unbind timed out, rv = %s (%d)",
1287 vcm->my_pid, vppcom_retval_str (rv), rv);
1294 vppcom_session_disconnect (u32 session_index)
1296 vppcom_main_t *vcm = &vppcom_main;
1300 clib_spinlock_lock (&vcm->sessions_lockp);
1301 rv = vppcom_session_at_index (session_index, &session);
1302 if (PREDICT_FALSE (rv))
1304 clib_spinlock_unlock (&vcm->sessions_lockp);
1305 if (VPPCOM_DEBUG > 1)
1306 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1307 vcm->my_pid, session_index);
1311 if (!session->is_cut_thru)
1313 vppcom_send_disconnect (session);
1314 clib_spinlock_unlock (&vcm->sessions_lockp);
1316 rv = vppcom_wait_for_session_state_change (session_index,
1317 STATE_DISCONNECT, 1.0);
1318 if ((VPPCOM_DEBUG > 0) && (rv < 0))
1319 clib_warning ("[%d] disconnect (session %d) failed, rv = %s (%d)",
1320 vcm->my_pid, session_index, vppcom_retval_str (rv), rv);
1323 clib_spinlock_unlock (&vcm->sessions_lockp);
1328 #define foreach_sock_msg \
1329 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1330 _(BIND_SOCK_REPLY, bind_sock_reply) \
1331 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1332 _(ACCEPT_SESSION, accept_session) \
1333 _(CONNECT_SOCK, connect_sock) \
1334 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1335 _(DISCONNECT_SESSION, disconnect_session) \
1336 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1337 _(RESET_SESSION, reset_session) \
1338 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1339 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1340 _(MAP_ANOTHER_SEGMENT, map_another_segment)
1343 vppcom_api_hookup (void)
1346 vl_msg_api_set_handlers(VL_API_##N, #n, \
1347 vl_api_##n##_t_handler, \
1349 vl_api_##n##_t_endian, \
1350 vl_api_##n##_t_print, \
1351 sizeof(vl_api_##n##_t), 1);
1357 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1361 vcl_cfg->heapsize = (256ULL << 20);
1362 vcl_cfg->segment_baseva = 0x200000000ULL;
1363 vcl_cfg->segment_size = (256 << 20);
1364 vcl_cfg->add_segment_size = (128 << 20);
1365 vcl_cfg->preallocated_fifo_pairs = 8;
1366 vcl_cfg->rx_fifo_size = (1 << 20);
1367 vcl_cfg->tx_fifo_size = (1 << 20);
1368 vcl_cfg->event_queue_size = 2048;
1369 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1370 vcl_cfg->app_timeout = 10 * 60.0;
1371 vcl_cfg->session_timeout = 10 * 60.0;
1372 vcl_cfg->accept_timeout = 60.0;
1376 vppcom_cfg_heapsize (char *conf_fname)
1378 vppcom_main_t *vcm = &vppcom_main;
1379 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1390 fp = fopen (conf_fname, "r");
1393 if (VPPCOM_DEBUG > 0)
1394 fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1397 argv = calloc (1, sizeof (char *));
1403 if (fgets (inbuf, 4096, fp) == 0)
1405 p = strtok (inbuf, " \t\n");
1411 char **tmp = realloc (argv, argc * sizeof (char *));
1415 arg = strndup (p, 1024);
1418 argv[argc - 1] = arg;
1419 p = strtok (NULL, " \t\n");
1426 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1433 * Look for and parse the "heapsize" config parameter.
1434 * Manual since none of the clib infra has been bootstrapped yet.
1436 * Format: heapsize <nn>[mM][gG]
1439 for (i = 1; i < (argc - 1); i++)
1441 if (!strncmp (argv[i], "heapsize", 8))
1443 sizep = (u8 *) argv[i + 1];
1445 while (*sizep >= '0' && *sizep <= '9')
1448 size += *sizep++ - '0';
1452 if (VPPCOM_DEBUG > 0)
1453 clib_warning ("[%d] parse error '%s %s', "
1454 "using default heapsize %lld (0x%llx)",
1455 vcm->my_pid, argv[i], argv[i + 1],
1456 vcl_cfg->heapsize, vcl_cfg->heapsize);
1460 if (*sizep == 'g' || *sizep == 'G')
1461 vcl_cfg->heapsize = size << 30;
1462 else if (*sizep == 'm' || *sizep == 'M')
1463 vcl_cfg->heapsize = size << 20;
1466 if (VPPCOM_DEBUG > 0)
1467 clib_warning ("[%d] parse error '%s %s', "
1468 "using default heapsize %lld (0x%llx)",
1469 vcm->my_pid, argv[i], argv[i + 1],
1470 vcl_cfg->heapsize, vcl_cfg->heapsize);
1481 if (!clib_mem_init (0, vcl_cfg->heapsize))
1482 clib_warning ("[%d] vppcom heap allocation failure!", vcm->my_pid);
1483 else if (VPPCOM_DEBUG > 0)
1484 clib_warning ("[%d] allocated vppcom heapsize %lld (0x%llx)",
1485 vcm->my_pid, vcl_cfg->heapsize, vcl_cfg->heapsize);
1489 vppcom_cfg_read (char *conf_fname)
1491 vppcom_main_t *vcm = &vppcom_main;
1492 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1494 unformat_input_t _input, *input = &_input;
1495 unformat_input_t _line_input, *line_input = &_line_input;
1496 u8 vc_cfg_input = 0;
1501 fd = open (conf_fname, O_RDONLY);
1504 if (VPPCOM_DEBUG > 0)
1505 clib_warning ("[%d] open configuration file '%s' failed!",
1506 vcm->my_pid, conf_fname);
1510 if (fstat (fd, &s) < 0)
1512 if (VPPCOM_DEBUG > 0)
1513 clib_warning ("[%d] failed to stat `%s'", vcm->my_pid, conf_fname);
1517 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1519 if (VPPCOM_DEBUG > 0)
1520 clib_warning ("[%d] not a regular file `%s'", vcm->my_pid,
1525 unformat_init_clib_file (input, fd);
1527 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1529 (void) unformat_user (input, unformat_line_input, line_input);
1530 unformat_skip_white_space (line_input);
1532 if (unformat (line_input, "vcl {"))
1540 if (unformat (line_input, "heapsize %s", &chroot_path))
1542 vec_terminate_c_string (chroot_path);
1543 if (VPPCOM_DEBUG > 0)
1544 clib_warning ("[%d] configured heapsize %s, "
1545 "actual heapsize %lld (0x%llx)",
1546 vcm->my_pid, chroot_path, vcl_cfg->heapsize,
1548 vec_free (chroot_path);
1550 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1552 vec_terminate_c_string (chroot_path);
1553 vl_set_memory_root_path ((char *) chroot_path);
1554 if (VPPCOM_DEBUG > 0)
1555 clib_warning ("[%d] configured api-prefix %s",
1556 vcm->my_pid, chroot_path);
1557 chroot_path = 0; /* Don't vec_free() it! */
1559 else if (unformat (line_input, "uid %d", &uid))
1561 vl_set_memory_uid (uid);
1562 if (VPPCOM_DEBUG > 0)
1563 clib_warning ("[%d] configured uid %d", vcm->my_pid, uid);
1565 else if (unformat (line_input, "gid %d", &gid))
1567 vl_set_memory_gid (gid);
1568 if (VPPCOM_DEBUG > 0)
1569 clib_warning ("[%d] configured gid %d", vcm->my_pid, gid);
1571 else if (unformat (line_input, "segment-baseva 0x%lx",
1572 &vcl_cfg->segment_baseva))
1574 if (VPPCOM_DEBUG > 0)
1575 clib_warning ("[%d] configured segment_baseva 0x%lx",
1576 vcm->my_pid, vcl_cfg->segment_baseva);
1578 else if (unformat (line_input, "segment-size 0x%lx",
1579 &vcl_cfg->segment_size))
1581 if (VPPCOM_DEBUG > 0)
1582 clib_warning ("[%d] configured segment_size 0x%lx (%ld)",
1583 vcm->my_pid, vcl_cfg->segment_size,
1584 vcl_cfg->segment_size);
1586 else if (unformat (line_input, "segment-size %ld",
1587 &vcl_cfg->segment_size))
1589 if (VPPCOM_DEBUG > 0)
1590 clib_warning ("[%d] configured segment_size %ld (0x%lx)",
1591 vcm->my_pid, vcl_cfg->segment_size,
1592 vcl_cfg->segment_size);
1594 else if (unformat (line_input, "add-segment-size 0x%lx",
1595 &vcl_cfg->add_segment_size))
1597 if (VPPCOM_DEBUG > 0)
1599 ("[%d] configured add_segment_size 0x%lx (%ld)",
1600 vcm->my_pid, vcl_cfg->add_segment_size,
1601 vcl_cfg->add_segment_size);
1603 else if (unformat (line_input, "add-segment-size %ld",
1604 &vcl_cfg->add_segment_size))
1606 if (VPPCOM_DEBUG > 0)
1608 ("[%d] configured add_segment_size %ld (0x%lx)",
1609 vcm->my_pid, vcl_cfg->add_segment_size,
1610 vcl_cfg->add_segment_size);
1612 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1613 &vcl_cfg->preallocated_fifo_pairs))
1615 if (VPPCOM_DEBUG > 0)
1616 clib_warning ("[%d] configured preallocated_fifo_pairs "
1617 "%d (0x%x)", vcm->my_pid,
1618 vcl_cfg->preallocated_fifo_pairs,
1619 vcl_cfg->preallocated_fifo_pairs);
1621 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1622 &vcl_cfg->rx_fifo_size))
1624 if (VPPCOM_DEBUG > 0)
1625 clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)",
1626 vcm->my_pid, vcl_cfg->rx_fifo_size,
1627 vcl_cfg->rx_fifo_size);
1629 else if (unformat (line_input, "rx-fifo-size %ld",
1630 &vcl_cfg->rx_fifo_size))
1632 if (VPPCOM_DEBUG > 0)
1633 clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)",
1634 vcm->my_pid, vcl_cfg->rx_fifo_size,
1635 vcl_cfg->rx_fifo_size);
1637 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1638 &vcl_cfg->tx_fifo_size))
1640 if (VPPCOM_DEBUG > 0)
1641 clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)",
1642 vcm->my_pid, vcl_cfg->tx_fifo_size,
1643 vcl_cfg->tx_fifo_size);
1645 else if (unformat (line_input, "tx-fifo-size %ld",
1646 &vcl_cfg->tx_fifo_size))
1648 if (VPPCOM_DEBUG > 0)
1649 clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)",
1650 vcm->my_pid, vcl_cfg->tx_fifo_size,
1651 vcl_cfg->tx_fifo_size);
1653 else if (unformat (line_input, "event-queue-size 0x%lx",
1654 &vcl_cfg->event_queue_size))
1656 if (VPPCOM_DEBUG > 0)
1657 clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)",
1658 vcm->my_pid, vcl_cfg->event_queue_size,
1659 vcl_cfg->event_queue_size);
1661 else if (unformat (line_input, "event-queue-size %ld",
1662 &vcl_cfg->event_queue_size))
1664 if (VPPCOM_DEBUG > 0)
1665 clib_warning ("[%d] configured event_queue_size %ld (0x%lx)",
1666 vcm->my_pid, vcl_cfg->event_queue_size,
1667 vcl_cfg->event_queue_size);
1669 else if (unformat (line_input, "listen-queue-size 0x%lx",
1670 &vcl_cfg->listen_queue_size))
1672 if (VPPCOM_DEBUG > 0)
1673 clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)",
1674 vcm->my_pid, vcl_cfg->listen_queue_size,
1675 vcl_cfg->listen_queue_size);
1677 else if (unformat (line_input, "listen-queue-size %ld",
1678 &vcl_cfg->listen_queue_size))
1680 if (VPPCOM_DEBUG > 0)
1681 clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)",
1682 vcm->my_pid, vcl_cfg->listen_queue_size,
1683 vcl_cfg->listen_queue_size);
1685 else if (unformat (line_input, "app-timeout %f",
1686 &vcl_cfg->app_timeout))
1688 if (VPPCOM_DEBUG > 0)
1689 clib_warning ("[%d] configured app_timeout %f",
1690 vcm->my_pid, vcl_cfg->app_timeout);
1692 else if (unformat (line_input, "session-timeout %f",
1693 &vcl_cfg->session_timeout))
1695 if (VPPCOM_DEBUG > 0)
1696 clib_warning ("[%d] configured session_timeout %f",
1697 vcm->my_pid, vcl_cfg->session_timeout);
1699 else if (unformat (line_input, "accept-timeout %f",
1700 &vcl_cfg->accept_timeout))
1702 if (VPPCOM_DEBUG > 0)
1703 clib_warning ("[%d] configured accept_timeout %f",
1704 vcm->my_pid, vcl_cfg->accept_timeout);
1706 else if (unformat (line_input, "app-proxy-transport-tcp"))
1708 vcl_cfg->app_proxy_transport_tcp = 1;
1709 if (VPPCOM_DEBUG > 0)
1710 clib_warning ("[%d] configured app_proxy_transport_tcp (%d)",
1711 vcm->my_pid, vcl_cfg->app_proxy_transport_tcp);
1713 else if (unformat (line_input, "app-proxy-transport-udp"))
1715 vcl_cfg->app_proxy_transport_udp = 1;
1716 if (VPPCOM_DEBUG > 0)
1717 clib_warning ("[%d] configured app_proxy_transport_udp (%d)",
1718 vcm->my_pid, vcl_cfg->app_proxy_transport_udp);
1720 else if (unformat (line_input, "app-scope-local"))
1722 vcl_cfg->app_scope_local = 1;
1723 if (VPPCOM_DEBUG > 0)
1724 clib_warning ("[%d] configured app_scope_local (%d)",
1725 vcm->my_pid, vcl_cfg->app_scope_local);
1727 else if (unformat (line_input, "app-scope-global"))
1729 vcl_cfg->app_scope_global = 1;
1730 if (VPPCOM_DEBUG > 0)
1731 clib_warning ("[%d] configured app_scope_global (%d)",
1732 vcm->my_pid, vcl_cfg->app_scope_global);
1734 else if (unformat (line_input, "namespace-secret %lu",
1735 &vcl_cfg->namespace_secret))
1737 if (VPPCOM_DEBUG > 0)
1739 ("[%d] configured namespace_secret %lu (0x%lx)",
1740 vcm->my_pid, vcl_cfg->namespace_secret,
1741 vcl_cfg->namespace_secret);
1743 else if (unformat (line_input, "namespace-id %v",
1744 &vcl_cfg->namespace_id))
1746 vl_api_application_attach_t *mp;
1747 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
1748 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
1749 if (nsid_vec_len > max_nsid_vec_len)
1751 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
1752 if (VPPCOM_DEBUG > 0)
1753 clib_warning ("[%d] configured namespace_id is too long,"
1754 " truncated to %d characters!", vcm->my_pid,
1758 if (VPPCOM_DEBUG > 0)
1759 clib_warning ("[%d] configured namespace_id %v",
1760 vcm->my_pid, vcl_cfg->namespace_id);
1762 else if (unformat (line_input, "}"))
1765 if (VPPCOM_DEBUG > 0)
1766 clib_warning ("[%d] completed parsing vppcom config!",
1772 if (line_input->buffer[line_input->index] != '#')
1774 clib_warning ("[%d] Unknown vppcom config option: '%s'",
1775 vcm->my_pid, (char *)
1776 &line_input->buffer[line_input->index]);
1783 unformat_free (input);
1791 * VPPCOM Public API functions
1794 vppcom_app_create (char *app_name)
1796 vppcom_main_t *vcm = &vppcom_main;
1797 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1808 vcm->my_pid = getpid ();
1809 clib_fifo_validate (vcm->client_session_index_fifo,
1810 vcm->cfg.listen_queue_size);
1811 vppcom_cfg_init (vcl_cfg);
1812 conf_fname = getenv (VPPCOM_ENV_CONF);
1815 conf_fname = VPPCOM_CONF_DEFAULT;
1816 if (VPPCOM_DEBUG > 0)
1817 clib_warning ("[%d] getenv '%s' failed!", vcm->my_pid,
1820 vppcom_cfg_heapsize (conf_fname);
1821 vppcom_cfg_read (conf_fname);
1822 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
1825 u32 ns_id_vec_len = strlen (env_var_str);
1827 vec_reset_length (vcm->cfg.namespace_id);
1828 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
1829 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
1831 if (VPPCOM_DEBUG > 0)
1832 clib_warning ("[%d] configured namespace_id (%v) from "
1833 VPPCOM_ENV_APP_NAMESPACE_ID "!", vcm->my_pid,
1834 vcm->cfg.namespace_id);
1836 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
1840 if (sscanf (env_var_str, "%lu", &tmp) != 1)
1841 clib_warning ("[%d] Invalid namespace secret specified in "
1842 "the environment variable "
1843 VPPCOM_ENV_APP_NAMESPACE_SECRET
1844 " (%s)!\n", vcm->my_pid, env_var_str);
1847 vcm->cfg.namespace_secret = tmp;
1848 if (VPPCOM_DEBUG > 0)
1849 clib_warning ("[%d] configured namespace secret (%lu) from "
1850 VPPCOM_ENV_APP_NAMESPACE_ID "!", vcm->my_pid,
1851 vcm->cfg.namespace_secret);
1854 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
1856 vcm->cfg.app_proxy_transport_tcp = 1;
1857 if (VPPCOM_DEBUG > 0)
1858 clib_warning ("[%d] configured app_proxy_transport_tcp (%u) from "
1859 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP "!", vcm->my_pid,
1860 vcm->cfg.app_proxy_transport_tcp);
1862 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
1864 vcm->cfg.app_proxy_transport_udp = 1;
1865 if (VPPCOM_DEBUG > 0)
1866 clib_warning ("[%d] configured app_proxy_transport_udp (%u) from "
1867 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP "!", vcm->my_pid,
1868 vcm->cfg.app_proxy_transport_udp);
1870 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
1872 vcm->cfg.app_scope_local = 1;
1873 if (VPPCOM_DEBUG > 0)
1874 clib_warning ("[%d] configured app_scope_local (%u) from "
1875 VPPCOM_ENV_APP_SCOPE_LOCAL "!", vcm->my_pid,
1876 vcm->cfg.app_scope_local);
1878 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
1880 vcm->cfg.app_scope_global = 1;
1881 if (VPPCOM_DEBUG > 0)
1882 clib_warning ("[%d] configured app_scope_global (%u) from "
1883 VPPCOM_ENV_APP_SCOPE_GLOBAL "!", vcm->my_pid,
1884 vcm->cfg.app_scope_global);
1887 vcm->bind_session_index = ~0;
1888 vcm->main_cpu = os_get_thread_index ();
1889 heap = clib_mem_get_per_cpu_heap ();
1890 h = mheap_header (heap);
1892 /* make the main heap thread-safe */
1893 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1895 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1897 clib_time_init (&vcm->clib_time);
1898 vppcom_init_error_string_table ();
1899 svm_fifo_segment_init (vcl_cfg->segment_baseva,
1900 20 /* timeout in secs */ );
1901 clib_spinlock_init (&vcm->sessions_lockp);
1902 vppcom_api_hookup ();
1905 if (vcm->my_client_index == ~0)
1907 vcm->app_state = STATE_APP_START;
1908 rv = vppcom_connect_to_vpp (app_name);
1911 clib_warning ("[%d] couldn't connect to VPP.", vcm->my_pid);
1915 if (VPPCOM_DEBUG > 0)
1916 clib_warning ("[%d] sending session enable", vcm->my_pid);
1918 rv = vppcom_app_session_enable ();
1921 clib_warning ("[%d] vppcom_app_session_enable() failed!",
1926 if (VPPCOM_DEBUG > 0)
1927 clib_warning ("[%d] sending app attach", vcm->my_pid);
1929 rv = vppcom_app_attach ();
1932 clib_warning ("[%d] vppcom_app_attach() failed!", vcm->my_pid);
1936 if (VPPCOM_DEBUG > 0)
1937 clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)",
1938 vcm->my_pid, app_name, vcm->my_client_index,
1939 vcm->my_client_index);
1946 vppcom_app_destroy (void)
1948 vppcom_main_t *vcm = &vppcom_main;
1951 if (vcm->my_client_index == ~0)
1954 if (VPPCOM_DEBUG > 0)
1955 clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)",
1956 vcm->my_pid, vcm->my_client_index, vcm->my_client_index);
1958 vppcom_app_detach ();
1959 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
1960 if (PREDICT_FALSE (rv))
1962 if (VPPCOM_DEBUG > 0)
1963 clib_warning ("[%d] application detach timed out, rv = %s (%d)",
1964 vcm->my_pid, vppcom_retval_str (rv), rv);
1966 vl_client_disconnect_from_vlib ();
1967 vcm->my_client_index = ~0;
1968 vcm->app_state = STATE_APP_START;
1972 vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
1974 vppcom_main_t *vcm = &vppcom_main;
1978 clib_spinlock_lock (&vcm->sessions_lockp);
1979 pool_get (vcm->sessions, session);
1980 memset (session, 0, sizeof (*session));
1981 session_index = session - vcm->sessions;
1984 session->proto = proto;
1985 session->state = STATE_START;
1986 session->is_nonblocking = is_nonblocking ? 1 : 0;
1987 clib_spinlock_unlock (&vcm->sessions_lockp);
1989 if (VPPCOM_DEBUG > 0)
1990 clib_warning ("[%d] sid %d", vcm->my_pid, session_index);
1992 return (int) session_index;
1996 vppcom_session_close (uint32_t session_index)
1998 vppcom_main_t *vcm = &vppcom_main;
1999 session_t *session = 0;
2008 session_state_t state;
2010 clib_spinlock_lock (&vcm->sessions_lockp);
2011 rv = vppcom_session_at_index (session_index, &session);
2012 if (PREDICT_FALSE (rv))
2014 if (VPPCOM_DEBUG > 0)
2015 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2016 vcm->my_pid, session_index);
2017 clib_spinlock_unlock (&vcm->sessions_lockp);
2020 is_server = session->is_server;
2021 is_listen = session->is_listen;
2022 is_cut_thru = session->is_cut_thru;
2023 is_vep = session->is_vep;
2024 is_vep_session = session->is_vep_session;
2025 next_sid = session->vep.next_sid;
2026 vep_idx = session->vep.vep_idx;
2027 state = session->state;
2028 clib_spinlock_unlock (&vcm->sessions_lockp);
2030 if (VPPCOM_DEBUG > 0)
2031 clib_warning ("[%d] sid %d", vcm->my_pid, session_index);
2035 while (next_sid != ~0)
2037 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
2038 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2039 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
2040 "rv = %s (%d)", vcm->my_pid, vep_idx, next_sid,
2041 vppcom_retval_str (rv), rv);
2043 clib_spinlock_lock (&vcm->sessions_lockp);
2044 rv = vppcom_session_at_index (session_index, &session);
2045 if (PREDICT_FALSE (rv))
2047 if (VPPCOM_DEBUG > 0)
2049 ("[%d] invalid session, sid (%u) has been closed!",
2050 vcm->my_pid, session_index);
2051 clib_spinlock_unlock (&vcm->sessions_lockp);
2054 next_sid = session->vep.next_sid;
2055 clib_spinlock_unlock (&vcm->sessions_lockp);
2062 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2063 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2064 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
2065 "rv = %s (%d)", vcm->my_pid, vep_idx, session_index,
2066 vppcom_retval_str (rv), rv);
2069 if (is_cut_thru && is_server && (state == STATE_ACCEPT))
2071 rv = vppcom_session_unbind_cut_thru (session);
2072 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2073 clib_warning ("[%d] unbind cut-thru (session %d) failed, "
2075 vcm->my_pid, session_index,
2076 vppcom_retval_str (rv), rv);
2078 else if (is_server && is_listen)
2080 rv = vppcom_session_unbind (session_index);
2081 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2082 clib_warning ("[%d] unbind (session %d) failed, rv = %s (%d)",
2083 vcm->my_pid, session_index,
2084 vppcom_retval_str (rv), rv);
2086 else if (state == STATE_CONNECT)
2087 if (vppcom_session_disconnect (session_index))
2090 clib_spinlock_lock (&vcm->sessions_lockp);
2091 pool_put_index (vcm->sessions, session_index);
2092 clib_spinlock_unlock (&vcm->sessions_lockp);
2098 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2100 vppcom_main_t *vcm = &vppcom_main;
2101 session_t *session = 0;
2105 return VPPCOM_EINVAL;
2107 clib_spinlock_lock (&vcm->sessions_lockp);
2108 rv = vppcom_session_at_index (session_index, &session);
2109 if (PREDICT_FALSE (rv))
2111 clib_spinlock_unlock (&vcm->sessions_lockp);
2112 if (VPPCOM_DEBUG > 0)
2113 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2114 vcm->my_pid, session_index);
2118 if (session->is_vep)
2120 clib_spinlock_unlock (&vcm->sessions_lockp);
2121 if (VPPCOM_DEBUG > 0)
2122 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2123 vcm->my_pid, session_index);
2124 return VPPCOM_EBADFD;
2127 session->vrf = ep->vrf;
2128 session->lcl_addr.is_ip4 = ep->is_ip4;
2129 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
2130 session->lcl_port = ep->port;
2132 if (VPPCOM_DEBUG > 0)
2133 clib_warning ("[%d] sid %d, bound to lcl address %U lcl port %u",
2134 vcm->my_pid, session_index, format_ip46_address,
2135 &session->lcl_addr.ip46, session->lcl_addr.is_ip4,
2136 clib_net_to_host_u16 (session->lcl_port));
2138 clib_spinlock_unlock (&vcm->sessions_lockp);
2143 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2145 vppcom_main_t *vcm = &vppcom_main;
2146 session_t *listen_session = 0;
2149 clib_spinlock_lock (&vcm->sessions_lockp);
2150 rv = vppcom_session_at_index (listen_session_index, &listen_session);
2151 if (PREDICT_FALSE (rv))
2153 clib_spinlock_unlock (&vcm->sessions_lockp);
2154 if (VPPCOM_DEBUG > 0)
2155 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2156 vcm->my_pid, listen_session_index);
2160 if (listen_session->is_vep)
2162 clib_spinlock_unlock (&vcm->sessions_lockp);
2163 if (VPPCOM_DEBUG > 0)
2164 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2165 vcm->my_pid, listen_session_index);
2166 return VPPCOM_EBADFD;
2169 if (VPPCOM_DEBUG > 0)
2170 clib_warning ("[%d] sid %d", vcm->my_pid, listen_session_index);
2172 ASSERT (vcm->bind_session_index == ~0);
2173 vcm->bind_session_index = listen_session_index;
2174 vppcom_send_bind_sock (listen_session);
2175 clib_spinlock_unlock (&vcm->sessions_lockp);
2177 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2178 vcm->cfg.session_timeout);
2179 if (PREDICT_FALSE (rv))
2181 vcm->bind_session_index = ~0;
2182 if (VPPCOM_DEBUG > 0)
2183 clib_warning ("[%d] server listen timed out, rv = %d (%d)",
2184 vcm->my_pid, vppcom_retval_str (rv), rv);
2188 clib_spinlock_lock (&vcm->sessions_lockp);
2189 rv = vppcom_session_at_index (listen_session_index, &listen_session);
2190 if (PREDICT_FALSE (rv))
2192 clib_spinlock_unlock (&vcm->sessions_lockp);
2193 if (VPPCOM_DEBUG > 0)
2194 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2195 vcm->my_pid, listen_session_index);
2198 listen_session->is_listen = 1;
2199 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2200 clib_spinlock_unlock (&vcm->sessions_lockp);
2206 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2207 double wait_for_time)
2209 vppcom_main_t *vcm = &vppcom_main;
2210 session_t *listen_session = 0;
2211 session_t *client_session = 0;
2212 u32 client_session_index;
2216 clib_spinlock_lock (&vcm->sessions_lockp);
2217 rv = vppcom_session_at_index (listen_session_index, &listen_session);
2218 if (PREDICT_FALSE (rv))
2220 clib_spinlock_unlock (&vcm->sessions_lockp);
2221 if (VPPCOM_DEBUG > 0)
2222 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2223 vcm->my_pid, listen_session_index);
2227 if (listen_session->is_vep)
2229 clib_spinlock_unlock (&vcm->sessions_lockp);
2230 if (VPPCOM_DEBUG > 0)
2231 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2232 vcm->my_pid, listen_session_index);
2233 return VPPCOM_EBADFD;
2236 if (listen_session->state != STATE_LISTEN)
2238 clib_spinlock_unlock (&vcm->sessions_lockp);
2239 if (VPPCOM_DEBUG > 0)
2240 clib_warning ("[%d] session not in listen state, state = %s",
2242 vppcom_session_state_str (listen_session->state));
2243 return VPPCOM_EBADFD;
2245 wait_for = listen_session->is_nonblocking ? 0 :
2246 (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time;
2248 if (VPPCOM_DEBUG > 0)
2249 clib_warning ("[%d] sid %d: %s (%d)", vcm->my_pid,
2250 listen_session_index,
2251 vppcom_session_state_str (listen_session->state),
2252 listen_session->state);
2253 clib_spinlock_unlock (&vcm->sessions_lockp);
2257 rv = vppcom_wait_for_client_session_index (wait_for);
2260 if ((VPPCOM_DEBUG > 0))
2261 clib_warning ("[%d] sid %d, accept timed out, rv = %s (%d)",
2262 vcm->my_pid, listen_session_index,
2263 vppcom_retval_str (rv), rv);
2264 if ((wait_for == 0) || (wait_for_time > 0))
2271 clib_spinlock_lock (&vcm->sessions_lockp);
2272 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
2273 rv = vppcom_session_at_index (client_session_index, &client_session);
2274 ASSERT (rv == VPPCOM_OK);
2275 ASSERT (client_session->peer_addr.is_ip4 ==
2276 listen_session->lcl_addr.is_ip4);
2278 if (VPPCOM_DEBUG > 0)
2279 clib_warning ("[%d] Got a request: client sid %d", vcm->my_pid,
2280 client_session_index);
2282 // Copy the lcl information from the listening session to the client session
2283 // client_session->lcl_port = listen_session->lcl_port;
2284 // client_session->lcl_addr = listen_session->lcl_addr;
2286 ep->vrf = client_session->vrf;
2287 ep->is_cut_thru = client_session->is_cut_thru;
2288 ep->is_ip4 = client_session->peer_addr.is_ip4;
2289 ep->port = client_session->peer_port;
2290 if (client_session->peer_addr.is_ip4)
2291 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2292 sizeof (ip4_address_t));
2294 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2295 sizeof (ip6_address_t));
2296 if (VPPCOM_DEBUG > 0)
2297 clib_warning ("[%d] sid %d, accepted peer address %U peer port %u",
2298 vcm->my_pid, client_session_index, format_ip46_address,
2299 &client_session->peer_addr.ip46,
2300 client_session->peer_addr.is_ip4,
2301 clib_net_to_host_u16 (client_session->peer_port));
2302 clib_spinlock_unlock (&vcm->sessions_lockp);
2303 return (int) client_session_index;
2307 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2309 vppcom_main_t *vcm = &vppcom_main;
2310 session_t *session = 0;
2313 clib_spinlock_lock (&vcm->sessions_lockp);
2314 rv = vppcom_session_at_index (session_index, &session);
2315 if (PREDICT_FALSE (rv))
2317 clib_spinlock_unlock (&vcm->sessions_lockp);
2318 if (VPPCOM_DEBUG > 0)
2319 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2320 vcm->my_pid, session_index);
2324 if (session->is_vep)
2326 clib_spinlock_unlock (&vcm->sessions_lockp);
2327 if (VPPCOM_DEBUG > 0)
2328 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2329 vcm->my_pid, session_index);
2330 return VPPCOM_EBADFD;
2333 if (session->state == STATE_CONNECT)
2335 clib_spinlock_unlock (&vcm->sessions_lockp);
2336 if (VPPCOM_DEBUG > 0)
2337 clib_warning ("[%d] session, sid (%u) already connected!",
2338 vcm->my_pid, session_index);
2342 session->vrf = server_ep->vrf;
2343 session->peer_addr.is_ip4 = server_ep->is_ip4;
2344 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
2345 session->peer_port = server_ep->port;
2347 if (VPPCOM_DEBUG > 0)
2349 u8 *ip_str = format (0, "%U", format_ip46_address,
2350 &session->peer_addr.ip46,
2351 session->peer_addr.is_ip4);
2352 clib_warning ("[%d] connect sid %d to %s server port %d proto %s",
2353 vcm->my_pid, session_index, ip_str,
2354 clib_net_to_host_u16 (session->peer_port),
2355 session->proto ? "UDP" : "TCP");
2359 vppcom_send_connect_sock (session, session_index);
2360 clib_spinlock_unlock (&vcm->sessions_lockp);
2361 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2362 vcm->cfg.session_timeout);
2363 if (PREDICT_FALSE (rv))
2365 if (VPPCOM_DEBUG > 0)
2366 clib_warning ("[%d] connect timed out, rv = %s (%d)",
2367 vcm->my_pid, vppcom_retval_str (rv), rv);
2370 if (VPPCOM_DEBUG > 0)
2371 clib_warning ("[%d] sid %d connected!", vcm->my_pid, session_index);
2377 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2380 vppcom_main_t *vcm = &vppcom_main;
2381 session_t *session = 0;
2382 svm_fifo_t *rx_fifo;
2390 clib_spinlock_lock (&vcm->sessions_lockp);
2391 rv = vppcom_session_at_index (session_index, &session);
2392 if (PREDICT_FALSE (rv))
2394 clib_spinlock_unlock (&vcm->sessions_lockp);
2395 if (VPPCOM_DEBUG > 0)
2396 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2397 vcm->my_pid, session_index);
2401 if (session->is_vep)
2403 clib_spinlock_unlock (&vcm->sessions_lockp);
2404 if (VPPCOM_DEBUG > 0)
2405 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2406 vcm->my_pid, session_index);
2407 return VPPCOM_EBADFD;
2410 if (session->state == STATE_DISCONNECT)
2412 clib_spinlock_unlock (&vcm->sessions_lockp);
2413 if (VPPCOM_DEBUG > 0)
2414 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2415 vcm->my_pid, session_index);
2416 return VPPCOM_ECONNRESET;
2419 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2420 session->server_rx_fifo : session->server_tx_fifo);
2421 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2422 "server_rx_fifo" : "server_tx_fifo");
2423 poll_et = EPOLLET & session->vep.ev.events;
2424 clib_spinlock_unlock (&vcm->sessions_lockp);
2429 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
2431 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
2433 while (!session->is_nonblocking && (n_read <= 0));
2435 if (poll_et && (n_read <= 0))
2437 clib_spinlock_lock (&vcm->sessions_lockp);
2438 session->vep.et_mask |= EPOLLIN;
2439 clib_spinlock_unlock (&vcm->sessions_lockp);
2442 if ((VPPCOM_DEBUG > 2) && (n_read > 0))
2443 clib_warning ("[%d] sid %d, read %d bytes from %s (%p)", vcm->my_pid,
2444 session_index, n_read, fifo_str, rx_fifo);
2446 return (n_read <= 0) ? VPPCOM_EAGAIN : n_read;
2450 vppcom_session_read (uint32_t session_index, void *buf, int n)
2452 return (vppcom_session_read_internal (session_index, buf, n, 0));
2456 vppcom_session_peek (uint32_t session_index, void *buf, int n)
2458 return (vppcom_session_read_internal (session_index, buf, n, 1));
2462 vppcom_session_read_ready (session_t * session, u32 session_index)
2464 vppcom_main_t *vcm = &vppcom_main;
2465 svm_fifo_t *rx_fifo;
2468 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2469 if (session->is_vep)
2471 clib_spinlock_unlock (&vcm->sessions_lockp);
2472 if (VPPCOM_DEBUG > 0)
2473 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2474 vcm->my_pid, session_index);
2475 return VPPCOM_EBADFD;
2478 if (session->state == STATE_DISCONNECT)
2480 if (VPPCOM_DEBUG > 0)
2481 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2482 vcm->my_pid, session_index);
2483 return VPPCOM_ECONNRESET;
2486 if (session->is_listen)
2487 ready = clib_fifo_elts (vcm->client_session_index_fifo);
2490 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2491 session->server_rx_fifo : session->server_tx_fifo);
2493 ready = svm_fifo_max_dequeue (rx_fifo);
2496 if (VPPCOM_DEBUG > 3)
2497 clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", vcm->my_pid,
2499 session->is_server ? "server_rx_fifo" : "server_tx_fifo",
2501 if ((session->vep.ev.events & EPOLLET) && (ready == 0))
2502 session->vep.et_mask |= EPOLLIN;
2508 vppcom_session_write (uint32_t session_index, void *buf, int n)
2510 vppcom_main_t *vcm = &vppcom_main;
2511 session_t *session = 0;
2512 svm_fifo_t *tx_fifo;
2513 unix_shared_memory_queue_t *q;
2514 session_fifo_event_t evt;
2521 clib_spinlock_lock (&vcm->sessions_lockp);
2522 rv = vppcom_session_at_index (session_index, &session);
2523 if (PREDICT_FALSE (rv))
2525 clib_spinlock_unlock (&vcm->sessions_lockp);
2526 if (VPPCOM_DEBUG > 0)
2527 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2528 vcm->my_pid, session_index);
2532 if (session->is_vep)
2534 clib_spinlock_unlock (&vcm->sessions_lockp);
2535 if (VPPCOM_DEBUG > 0)
2536 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2537 vcm->my_pid, session_index);
2538 return VPPCOM_EBADFD;
2541 if (session->state == STATE_DISCONNECT)
2543 clib_spinlock_unlock (&vcm->sessions_lockp);
2544 if (VPPCOM_DEBUG > 0)
2545 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2546 vcm->my_pid, session_index);
2547 return VPPCOM_ECONNRESET;
2550 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2551 session->server_tx_fifo : session->server_rx_fifo);
2552 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2553 "server_tx_fifo" : "server_rx_fifo");
2554 q = session->vpp_event_queue;
2555 poll_et = EPOLLET & session->vep.ev.events;
2556 clib_spinlock_unlock (&vcm->sessions_lockp);
2560 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
2562 while (!session->is_nonblocking && (n_write <= 0));
2564 /* If event wasn't set, add one */
2565 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
2569 /* Fabricate TX event, send to vpp */
2571 evt.event_type = FIFO_EVENT_APP_TX;
2573 rval = vppcom_session_at_index (session_index, &session);
2574 if (PREDICT_FALSE (rval))
2576 if (VPPCOM_DEBUG > 1)
2577 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2578 vcm->my_pid, session_index);
2582 unix_shared_memory_queue_add (q, (u8 *) & evt,
2583 0 /* do wait for mutex */ );
2586 if (poll_et && (n_write <= 0))
2588 clib_spinlock_lock (&vcm->sessions_lockp);
2589 session->vep.et_mask |= EPOLLOUT;
2590 clib_spinlock_unlock (&vcm->sessions_lockp);
2593 if (VPPCOM_DEBUG > 2)
2596 clib_warning ("[%d] sid %d, FIFO-FULL %s (%p)", vcm->my_pid,
2597 session_index, fifo_str, tx_fifo);
2599 clib_warning ("[%d] sid %d, wrote %d bytes to %s (%p)", vcm->my_pid,
2600 session_index, n_write, fifo_str, tx_fifo);
2602 return (n_write < 0) ? VPPCOM_EAGAIN : n_write;
2606 vppcom_session_write_ready (session_t * session, u32 session_index)
2608 vppcom_main_t *vcm = &vppcom_main;
2609 svm_fifo_t *tx_fifo;
2613 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2614 if (session->is_vep)
2616 clib_spinlock_unlock (&vcm->sessions_lockp);
2617 if (VPPCOM_DEBUG > 0)
2618 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2619 vcm->my_pid, session_index);
2620 return VPPCOM_EBADFD;
2623 if (session->state == STATE_DISCONNECT)
2625 if (VPPCOM_DEBUG > 0)
2626 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2627 vcm->my_pid, session_index);
2628 return VPPCOM_ECONNRESET;
2631 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2632 session->server_tx_fifo : session->server_rx_fifo);
2633 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2634 "server_tx_fifo" : "server_rx_fifo");
2636 ready = svm_fifo_max_enqueue (tx_fifo);
2638 if (VPPCOM_DEBUG > 3)
2639 clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", vcm->my_pid,
2640 session_index, fifo_str, tx_fifo, ready);
2641 if ((session->vep.ev.events & EPOLLET) && (ready == 0))
2642 session->vep.et_mask |= EPOLLOUT;
2648 vppcom_select (unsigned long n_bits, unsigned long *read_map,
2649 unsigned long *write_map, unsigned long *except_map,
2650 double time_to_wait)
2652 vppcom_main_t *vcm = &vppcom_main;
2654 session_t *session = 0;
2655 int rv, bits_set = 0;
2656 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2657 u32 minbits = clib_max (n_bits, BITS (uword));
2659 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2661 if (n_bits && read_map)
2663 clib_bitmap_validate (vcm->rd_bitmap, minbits);
2664 clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
2665 memset (read_map, 0, vec_len (vcm->rd_bitmap));
2667 if (n_bits && write_map)
2669 clib_bitmap_validate (vcm->wr_bitmap, minbits);
2670 clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
2671 memset (write_map, 0, vec_len (vcm->wr_bitmap));
2673 if (n_bits && except_map)
2675 clib_bitmap_validate (vcm->ex_bitmap, minbits);
2676 clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
2677 memset (except_map, 0, vec_len (vcm->ex_bitmap));
2687 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
2689 clib_spinlock_lock (&vcm->sessions_lockp);
2690 rv = vppcom_session_at_index (session_index, &session);
2693 clib_spinlock_unlock (&vcm->sessions_lockp);
2694 if (VPPCOM_DEBUG > 1)
2695 clib_warning ("[%d] session %d specified in "
2696 "read_map is closed.", vcm->my_pid,
2698 bits_set = VPPCOM_EBADFD;
2702 rv = vppcom_session_read_ready (session, session_index);
2703 clib_spinlock_unlock (&vcm->sessions_lockp);
2704 if (except_map && vcm->ex_bitmap &&
2705 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
2708 // TBD: clib_warning
2709 clib_bitmap_set_no_check (except_map, session_index, 1);
2714 // TBD: clib_warning
2715 clib_bitmap_set_no_check (read_map, session_index, 1);
2723 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
2725 clib_spinlock_lock (&vcm->sessions_lockp);
2726 rv = vppcom_session_at_index (session_index, &session);
2729 clib_spinlock_unlock (&vcm->sessions_lockp);
2730 if (VPPCOM_DEBUG > 0)
2731 clib_warning ("[%d] session %d specified in "
2732 "write_map is closed.", vcm->my_pid,
2734 bits_set = VPPCOM_EBADFD;
2738 rv = vppcom_session_write_ready (session, session_index);
2739 clib_spinlock_unlock (&vcm->sessions_lockp);
2740 if (write_map && (rv > 0))
2742 // TBD: clib_warning
2743 clib_bitmap_set_no_check (write_map, session_index, 1);
2751 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
2753 clib_spinlock_lock (&vcm->sessions_lockp);
2754 rv = vppcom_session_at_index (session_index, &session);
2757 clib_spinlock_unlock (&vcm->sessions_lockp);
2758 if (VPPCOM_DEBUG > 1)
2759 clib_warning ("[%d] session %d specified in "
2760 "except_map is closed.", vcm->my_pid,
2762 bits_set = VPPCOM_EBADFD;
2766 rv = vppcom_session_read_ready (session, session_index);
2767 clib_spinlock_unlock (&vcm->sessions_lockp);
2770 // TBD: clib_warning
2771 clib_bitmap_set_no_check (except_map, session_index, 1);
2779 while (clib_time_now (&vcm->clib_time) < timeout);
2786 vep_verify_epoll_chain (u32 vep_idx)
2788 vppcom_main_t *vcm = &vppcom_main;
2790 vppcom_epoll_t *vep;
2794 if (VPPCOM_DEBUG < 1)
2797 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2798 rv = vppcom_session_at_index (vep_idx, &session);
2799 if (PREDICT_FALSE (rv))
2801 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", vcm->my_pid,
2805 if (PREDICT_FALSE (!session->is_vep))
2807 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!", vcm->my_pid,
2811 if (VPPCOM_DEBUG > 1)
2812 clib_warning ("[%d] vep_idx (%u): Dumping epoll chain\n"
2815 " is_vep_session = %u\n"
2816 " wait_cont_idx = 0x%x (%u)\n"
2818 vep_idx, session->is_vep, session->is_vep_session,
2819 session->wait_cont_idx, session->wait_cont_idx);
2822 vep = &session->vep;
2823 sid = vep->next_sid;
2824 if (session->is_vep_session)
2826 if (VPPCOM_DEBUG > 1)
2827 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2829 " next_sid = 0x%x (%u)\n"
2830 " prev_sid = 0x%x (%u)\n"
2831 " vep_idx = 0x%x (%u)\n"
2832 " ev.events = 0x%x\n"
2833 " ev.data.u64 = 0x%llx\n"
2837 vep->next_sid, vep->next_sid,
2838 vep->prev_sid, vep->prev_sid,
2839 vep->vep_idx, vep->vep_idx,
2840 vep->ev.events, vep->ev.data.u64, vep->et_mask);
2844 rv = vppcom_session_at_index (sid, &session);
2845 if (PREDICT_FALSE (rv))
2847 clib_warning ("[%d] ERROR: Invalid sid (%u)!",
2851 if (PREDICT_FALSE (session->is_vep))
2852 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
2853 vcm->my_pid, vep_idx);
2854 else if (PREDICT_FALSE (!session->is_vep_session))
2856 clib_warning ("[%d] ERROR: session (%u) is not a vep session!",
2860 if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2861 clib_warning ("[%d] ERROR: session (%u) vep_idx (%u) != "
2862 "vep_idx (%u)!", vcm->my_pid,
2863 sid, session->vep.vep_idx, vep_idx);
2869 if (VPPCOM_DEBUG > 1)
2870 clib_warning ("[%d] vep_idx (%u): Dump complete!", vcm->my_pid, vep_idx);
2874 vppcom_epoll_create (void)
2876 vppcom_main_t *vcm = &vppcom_main;
2877 session_t *vep_session;
2880 clib_spinlock_lock (&vcm->sessions_lockp);
2881 pool_get (vcm->sessions, vep_session);
2882 memset (vep_session, 0, sizeof (*vep_session));
2883 vep_idx = vep_session - vcm->sessions;
2885 vep_session->is_vep = 1;
2886 vep_session->vep.vep_idx = ~0;
2887 vep_session->vep.next_sid = ~0;
2888 vep_session->vep.prev_sid = ~0;
2889 vep_session->wait_cont_idx = ~0;
2890 clib_spinlock_unlock (&vcm->sessions_lockp);
2892 if (VPPCOM_DEBUG > 0)
2893 clib_warning ("[%d] Created vep_idx %u!", vcm->my_pid, vep_idx);
2899 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
2900 struct epoll_event *event)
2902 vppcom_main_t *vcm = &vppcom_main;
2903 session_t *vep_session;
2907 if (vep_idx == session_index)
2909 if (VPPCOM_DEBUG > 0)
2910 clib_warning ("[%d] ERROR: vep_idx == session_index (%u)!",
2911 vcm->my_pid, vep_idx);
2912 return VPPCOM_EINVAL;
2915 clib_spinlock_lock (&vcm->sessions_lockp);
2916 rv = vppcom_session_at_index (vep_idx, &vep_session);
2917 if (PREDICT_FALSE (rv))
2919 if (VPPCOM_DEBUG > 0)
2920 clib_warning ("[%d] ERROR: Invalid vep_idx (%u)!", vep_idx);
2923 if (PREDICT_FALSE (!vep_session->is_vep))
2925 if (VPPCOM_DEBUG > 0)
2926 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
2927 vcm->my_pid, vep_idx);
2932 ASSERT (vep_session->vep.vep_idx == ~0);
2933 ASSERT (vep_session->vep.prev_sid == ~0);
2935 rv = vppcom_session_at_index (session_index, &session);
2936 if (PREDICT_FALSE (rv))
2938 if (VPPCOM_DEBUG > 0)
2939 clib_warning ("[%d] ERROR: Invalid session_index (%u)!",
2940 vcm->my_pid, session_index);
2943 if (PREDICT_FALSE (session->is_vep))
2945 if (VPPCOM_DEBUG > 0)
2946 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
2954 if (PREDICT_FALSE (!event))
2956 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: NULL pointer to "
2957 "epoll_event structure!", vcm->my_pid);
2961 if (vep_session->vep.next_sid != ~0)
2963 session_t *next_session;
2964 rv = vppcom_session_at_index (vep_session->vep.next_sid,
2966 if (PREDICT_FALSE (rv))
2968 if (VPPCOM_DEBUG > 0)
2969 clib_warning ("[%d] ERROR: EPOLL_CTL_ADD: Invalid "
2970 "vep.next_sid (%u) on vep_idx (%u)!",
2971 vcm->my_pid, vep_session->vep.next_sid,
2975 ASSERT (next_session->vep.prev_sid == vep_idx);
2976 next_session->vep.prev_sid = session_index;
2978 session->vep.next_sid = vep_session->vep.next_sid;
2979 session->vep.prev_sid = vep_idx;
2980 session->vep.vep_idx = vep_idx;
2981 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2982 session->vep.ev = *event;
2983 session->is_vep_session = 1;
2984 vep_session->vep.next_sid = session_index;
2985 if (VPPCOM_DEBUG > 1)
2986 clib_warning ("[%d] EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x,"
2987 " data 0x%llx!", vcm->my_pid, vep_idx, session_index,
2988 event->events, event->data.u64);
2992 if (PREDICT_FALSE (!event))
2994 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: NULL pointer to "
2995 "epoll_event structure!", vcm->my_pid);
2999 if (PREDICT_FALSE (!session->is_vep_session &&
3000 (session->vep.vep_idx != vep_idx)))
3002 if (VPPCOM_DEBUG > 0)
3004 if (!session->is_vep_session)
3005 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
3006 "is not a vep session!",
3007 vcm->my_pid, session_index);
3009 clib_warning ("[%d] ERROR: EPOLL_CTL_MOD: session (%u) "
3010 "vep_idx (%u) != vep_idx (%u)!",
3011 vcm->my_pid, session_index,
3012 session->vep.vep_idx, vep_idx);
3017 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3018 session->vep.ev = *event;
3019 if (VPPCOM_DEBUG > 1)
3020 clib_warning ("[%d] EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3021 " data 0x%llx!", vcm->my_pid, vep_idx, session_index,
3022 event->events, event->data.u64);
3026 if (PREDICT_FALSE (!session->is_vep_session &&
3027 (session->vep.vep_idx != vep_idx)))
3029 if (VPPCOM_DEBUG > 0)
3031 if (!session->is_vep_session)
3032 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3033 "is not a vep session!",
3034 vcm->my_pid, session_index);
3036 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: session (%u) "
3037 "vep_idx (%u) != vep_idx (%u)!",
3038 vcm->my_pid, session_index,
3039 session->vep.vep_idx, vep_idx);
3045 vep_session->wait_cont_idx =
3046 (vep_session->wait_cont_idx == session_index) ?
3047 session->vep.next_sid : vep_session->wait_cont_idx;
3049 if (session->vep.prev_sid == vep_idx)
3050 vep_session->vep.next_sid = session->vep.next_sid;
3053 session_t *prev_session;
3054 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3055 if (PREDICT_FALSE (rv))
3057 if (VPPCOM_DEBUG > 0)
3058 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3059 "vep.prev_sid (%u) on sid (%u)!",
3060 vcm->my_pid, session->vep.prev_sid,
3064 ASSERT (prev_session->vep.next_sid == session_index);
3065 prev_session->vep.next_sid = session->vep.next_sid;
3067 if (session->vep.next_sid != ~0)
3069 session_t *next_session;
3070 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3071 if (PREDICT_FALSE (rv))
3073 if (VPPCOM_DEBUG > 0)
3074 clib_warning ("[%d] ERROR: EPOLL_CTL_DEL: Invalid "
3075 "vep.next_sid (%u) on sid (%u)!",
3076 vcm->my_pid, session->vep.next_sid,
3080 ASSERT (next_session->vep.prev_sid == session_index);
3081 next_session->vep.prev_sid = session->vep.prev_sid;
3084 memset (&session->vep, 0, sizeof (session->vep));
3085 session->vep.next_sid = ~0;
3086 session->vep.prev_sid = ~0;
3087 session->vep.vep_idx = ~0;
3088 session->is_vep_session = 0;
3089 if (VPPCOM_DEBUG > 1)
3090 clib_warning ("[%d] EPOLL_CTL_DEL: vep_idx %u, sid %u!",
3091 vcm->my_pid, vep_idx, session_index);
3095 clib_warning ("[%d] ERROR: Invalid operation (%d)!", vcm->my_pid, op);
3099 vep_verify_epoll_chain (vep_idx);
3102 clib_spinlock_unlock (&vcm->sessions_lockp);
3106 #define VCL_LOCK_AND_GET_SESSION(I, S) \
3108 vppcom_main_t *vcm = &vppcom_main; \
3110 clib_spinlock_lock (&vcm->sessions_lockp); \
3111 rv = vppcom_session_at_index (I, S); \
3112 if (PREDICT_FALSE (rv)) \
3114 clib_spinlock_unlock (&vcm->sessions_lockp); \
3116 if (VPPCOM_DEBUG > 0) \
3117 clib_warning ("[%s] ERROR: Invalid ##I (%u)!", \
3125 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3126 int maxevents, double wait_for_time)
3128 vppcom_main_t *vcm = &vppcom_main;
3129 session_t *vep_session;
3131 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3133 u32 vep_next_sid, wait_cont_idx;
3136 if (PREDICT_FALSE (maxevents <= 0))
3138 if (VPPCOM_DEBUG > 0)
3139 clib_warning ("[%d] ERROR: Invalid maxevents (%d)!",
3140 vcm->my_pid, maxevents);
3141 return VPPCOM_EINVAL;
3143 if (PREDICT_FALSE (wait_for_time < 0))
3145 if (VPPCOM_DEBUG > 0)
3146 clib_warning ("[%d] ERROR: Invalid wait_for_time (%f)!",
3147 vcm->my_pid, wait_for_time);
3148 return VPPCOM_EINVAL;
3150 memset (events, 0, sizeof (*events) * maxevents);
3152 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3153 vep_next_sid = vep_session->vep.next_sid;
3154 is_vep = vep_session->is_vep;
3155 wait_cont_idx = vep_session->wait_cont_idx;
3156 clib_spinlock_unlock (&vcm->sessions_lockp);
3158 if (PREDICT_FALSE (!is_vep))
3160 if (VPPCOM_DEBUG > 0)
3161 clib_warning ("[%d] ERROR: vep_idx (%u) is not a vep!",
3162 vcm->my_pid, vep_idx);
3166 if ((VPPCOM_DEBUG > 0) && (PREDICT_FALSE (vep_next_sid == ~0)))
3168 clib_warning ("[%d] WARNING: vep_idx (%u) is empty!",
3169 vcm->my_pid, vep_idx);
3179 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3180 sid != ~0; sid = next_sid)
3182 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3183 u8 add_event, is_vep_session;
3185 u64 session_ev_data;
3187 VCL_LOCK_AND_GET_SESSION (sid, &session);
3188 next_sid = session->vep.next_sid;
3189 session_events = session->vep.ev.events;
3190 et_mask = session->vep.et_mask;
3191 is_vep = session->is_vep;
3192 is_vep_session = session->is_vep_session;
3193 session_vep_idx = session->vep.vep_idx;
3194 session_ev_data = session->vep.ev.data.u64;
3195 clib_spinlock_unlock (&vcm->sessions_lockp);
3197 if (PREDICT_FALSE (is_vep))
3199 if (VPPCOM_DEBUG > 0)
3200 clib_warning ("[%d] ERROR: sid (%u) is a vep!",
3201 vcm->my_pid, vep_idx);
3205 if (PREDICT_FALSE (!is_vep_session))
3207 if (VPPCOM_DEBUG > 0)
3208 clib_warning ("[%d] ERROR: session (%u) is not "
3209 "a vep session!", vcm->my_pid, sid);
3213 if (PREDICT_FALSE (session_vep_idx != vep_idx))
3215 clib_warning ("[%d] ERROR: session (%u) "
3216 "vep_idx (%u) != vep_idx (%u)!",
3217 vcm->my_pid, sid, session->vep.vep_idx, vep_idx);
3222 add_event = clear_et_mask = 0;
3224 if ((EPOLLIN & session_events) && (EPOLLIN & et_mask))
3226 VCL_LOCK_AND_GET_SESSION (sid, &session);
3227 ready = vppcom_session_read_ready (session, sid);
3228 clib_spinlock_unlock (&vcm->sessions_lockp);
3232 events[num_ev].events |= EPOLLIN;
3233 if (EPOLLET & session_events)
3234 clear_et_mask |= EPOLLIN;
3241 case VPPCOM_ECONNRESET:
3242 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3246 events[num_ev].events |= EPOLLERR;
3252 if ((EPOLLOUT & session_events) && (EPOLLOUT & et_mask))
3254 VCL_LOCK_AND_GET_SESSION (sid, &session);
3255 ready = vppcom_session_write_ready (session, sid);
3256 clib_spinlock_unlock (&vcm->sessions_lockp);
3260 events[num_ev].events |= EPOLLOUT;
3261 if (EPOLLET & session_events)
3262 clear_et_mask |= EPOLLOUT;
3269 case VPPCOM_ECONNRESET:
3270 events[num_ev].events |= EPOLLHUP;
3274 events[num_ev].events |= EPOLLERR;
3282 events[num_ev].data.u64 = session_ev_data;
3283 if (EPOLLONESHOT & session_events)
3285 VCL_LOCK_AND_GET_SESSION (sid, &session);
3286 session->vep.ev.events = 0;
3287 clib_spinlock_unlock (&vcm->sessions_lockp);
3290 if (num_ev == maxevents)
3292 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3293 vep_session->wait_cont_idx = next_sid;
3294 clib_spinlock_unlock (&vcm->sessions_lockp);
3298 if (wait_cont_idx != ~0)
3301 next_sid = vep_next_sid;
3302 else if (next_sid == wait_cont_idx)
3307 while ((num_ev == 0) && (clib_time_now (&vcm->clib_time) <= timeout));
3309 if (wait_cont_idx != ~0)
3311 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3312 vep_session->wait_cont_idx = ~0;
3313 clib_spinlock_unlock (&vcm->sessions_lockp);
3316 return (rv != VPPCOM_OK) ? rv : num_ev;
3320 vppcom_session_attr (uint32_t session_index, uint32_t op,
3321 void *buffer, uint32_t * buflen)
3323 vppcom_main_t *vcm = &vppcom_main;
3326 u32 *flags = buffer;
3327 vppcom_endpt_t *ep = buffer;
3329 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3332 case VPPCOM_ATTR_GET_NREAD:
3333 rv = vppcom_session_read_ready (session, session_index);
3334 if (VPPCOM_DEBUG > 1)
3335 clib_warning ("[%d] VPPCOM_ATTR_GET_NREAD: nread = %d",
3340 case VPPCOM_ATTR_PEEK_NREAD:
3344 case VPPCOM_ATTR_GET_FLAGS:
3345 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3347 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
3348 *buflen = sizeof (*flags);
3349 if (VPPCOM_DEBUG > 1)
3350 clib_warning ("[%d] VPPCOM_ATTR_GET_FLAGS: flags = 0x%08x, "
3351 "is_nonblocking = %u", vcm->my_pid, *flags,
3352 session->is_nonblocking);
3358 case VPPCOM_ATTR_SET_FLAGS:
3359 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3361 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
3362 if (VPPCOM_DEBUG > 1)
3363 clib_warning ("[%d] VPPCOM_ATTR_SET_FLAGS: flags = 0x%08x, "
3364 "is_nonblocking = %u", vcm->my_pid, *flags,
3365 session->is_nonblocking);
3371 case VPPCOM_ATTR_GET_PEER_ADDR:
3372 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3374 ep->vrf = session->vrf;
3375 ep->is_ip4 = session->peer_addr.is_ip4;
3376 ep->port = session->peer_port;
3377 if (session->peer_addr.is_ip4)
3378 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3379 sizeof (ip4_address_t));
3381 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3382 sizeof (ip6_address_t));
3383 *buflen = sizeof (*ep);
3384 if (VPPCOM_DEBUG > 1)
3385 clib_warning ("[%d] VPPCOM_ATTR_GET_PEER_ADDR: sid %u is_ip4 = "
3386 "%u, addr = %U, port %u", vcm->my_pid,
3387 session_index, ep->is_ip4, format_ip46_address,
3388 &session->peer_addr.ip46, ep->is_ip4,
3389 clib_net_to_host_u16 (ep->port));
3395 case VPPCOM_ATTR_GET_LCL_ADDR:
3396 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3398 ep->vrf = session->vrf;
3399 ep->is_ip4 = session->lcl_addr.is_ip4;
3400 ep->port = session->lcl_port;
3401 if (session->lcl_addr.is_ip4)
3402 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3403 sizeof (ip4_address_t));
3405 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3406 sizeof (ip6_address_t));
3407 *buflen = sizeof (*ep);
3408 if (VPPCOM_DEBUG > 1)
3409 clib_warning ("[%d] VPPCOM_ATTR_GET_LCL_ADDR: sid %u is_ip4 = "
3410 "%u, addr = %U port %d", vcm->my_pid,
3411 session_index, ep->is_ip4, format_ip46_address,
3412 &session->lcl_addr.ip46, ep->is_ip4,
3413 clib_net_to_host_u16 (ep->port));
3419 case VPPCOM_ATTR_SET_REUSEADDR:
3422 case VPPCOM_ATTR_SET_BROADCAST:
3425 case VPPCOM_ATTR_SET_V6ONLY:
3428 case VPPCOM_ATTR_SET_KEEPALIVE:
3431 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3434 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3443 clib_spinlock_unlock (&vcm->sessions_lockp);
3448 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3449 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3451 vppcom_main_t *vcm = &vppcom_main;
3453 session_t *session = 0;
3457 clib_spinlock_lock (&vcm->sessions_lockp);
3458 rv = vppcom_session_at_index (session_index, &session);
3459 if (PREDICT_FALSE (rv))
3461 clib_spinlock_unlock (&vcm->sessions_lockp);
3462 if (VPPCOM_DEBUG > 0)
3463 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
3464 vcm->my_pid, session_index);
3466 clib_spinlock_unlock (&vcm->sessions_lockp);
3469 ep->vrf = session->vrf;
3470 ep->is_ip4 = session->peer_addr.is_ip4;
3471 ep->port = session->peer_port;
3472 if (session->peer_addr.is_ip4)
3473 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3474 sizeof (ip4_address_t));
3476 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3477 sizeof (ip6_address_t));
3478 clib_spinlock_unlock (&vcm->sessions_lockp);
3482 rv = vppcom_session_read (session_index, buffer, buflen);
3483 else if (flags & MSG_PEEK)
3484 rv = vppcom_session_peek (session_index, buffer, buflen);
3487 clib_warning ("[%d] Unsupport flags for recvfrom %d",
3488 vcm->my_pid, flags);
3489 rv = VPPCOM_EAFNOSUPPORT;
3497 vppcom_session_sendto (uint32_t session_index, void *buffer,
3498 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3500 vppcom_main_t *vcm = &vppcom_main;
3503 return VPPCOM_EINVAL;
3508 return VPPCOM_EINVAL;
3513 // TBD check the flags and do the right thing
3514 if (VPPCOM_DEBUG > 2)
3515 clib_warning ("[%d] handling flags 0x%u (%d) not implemented yet.",
3516 vcm->my_pid, flags, flags);
3519 return (vppcom_session_write (session_index, buffer, buflen));
3523 * fd.io coding-style-patch-verification: ON
3526 * eval: (c-set-style "gnu")