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 <vcl/vcl_event.h>
25 #include <vlib/unix/unix.h>
26 #include <vppinfra/vec_bootstrap.h>
27 #include <vppinfra/elog.h>
29 #define vl_typedefs /* define message structures */
30 #include <vpp/api/vpe_all_api_h.h>
33 /* declare message handlers for each api */
35 #define vl_endianfun /* define message structures */
36 #include <vpp/api/vpe_all_api_h.h>
39 /* instantiate all the print functions we know about */
40 #define vl_print(handle, ...)
42 #include <vpp/api/vpe_all_api_h.h>
46 /* Set VPPCOM_DEBUG_INIT 2 for connection debug,
47 * 3 for read/write debug output
49 * export VCL_DEBUG=<#> to set dynamically.
51 #define VPPCOM_DEBUG_INIT 1
53 #define VPPCOM_DEBUG_INIT 0
56 #define VPPCOM_DEBUG vcm->debug
59 * VPPCOM Private definitions and functions.
75 STATE_CLOSE_ON_EMPTY = 0x10,
76 STATE_DISCONNECT = 0x20,
80 #define SERVER_STATE_OPEN (STATE_ACCEPT|STATE_CLOSE_ON_EMPTY)
81 #define CLIENT_STATE_OPEN (STATE_CONNECT|STATE_CLOSE_ON_EMPTY)
83 typedef struct epoll_event vppcom_epoll_event_t;
90 vppcom_epoll_event_t ev;
91 #define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT)
92 #define VEP_UNSUPPORTED_EVENTS (EPOLLONESHOT|EPOLLEXCLUSIVE)
104 VCL_SESS_ATTR_SERVER,
105 VCL_SESS_ATTR_CUT_THRU,
107 VCL_SESS_ATTR_VEP_SESSION,
108 VCL_SESS_ATTR_LISTEN, // SOL_SOCKET,SO_ACCEPTCONN
109 VCL_SESS_ATTR_NONBLOCK, // fcntl,O_NONBLOCK
110 VCL_SESS_ATTR_REUSEADDR, // SOL_SOCKET,SO_REUSEADDR
111 VCL_SESS_ATTR_REUSEPORT, // SOL_SOCKET,SO_REUSEPORT
112 VCL_SESS_ATTR_BROADCAST, // SOL_SOCKET,SO_BROADCAST
113 VCL_SESS_ATTR_V6ONLY, // SOL_TCP,IPV6_V6ONLY
114 VCL_SESS_ATTR_KEEPALIVE, // SOL_SOCKET,SO_KEEPALIVE
115 VCL_SESS_ATTR_TCP_NODELAY, // SOL_TCP,TCP_NODELAY
116 VCL_SESS_ATTR_TCP_KEEPIDLE, // SOL_TCP,TCP_KEEPIDLE
117 VCL_SESS_ATTR_TCP_KEEPINTVL, // SOL_TCP,TCP_KEEPINTVL
119 } vppcom_session_attr_t;
121 #define VCL_SESS_ATTR_SET(ATTR, VAL) \
123 (ATTR) |= 1 << (VAL); \
126 #define VCL_SESS_ATTR_CLR(ATTR, VAL) \
128 (ATTR) &= ~(1 << (VAL)); \
131 #define VCL_SESS_ATTR_TEST(ATTR, VAL) \
132 ((ATTR) & (1 << (VAL)) ? 1 : 0)
136 volatile session_state_t state;
140 u32 sndbuf_size; // VPP-TBD: Hack until support setsockopt(SO_SNDBUF)
141 u32 rcvbuf_size; // VPP-TBD: Hack until support setsockopt(SO_RCVBUF)
142 u32 user_mss; // VPP-TBD: Hack until support setsockopt(TCP_MAXSEG)
147 svm_queue_t *vpp_event_queue;
149 /* Socket configuration state */
156 vppcom_ip46_t lcl_addr;
157 vppcom_ip46_t peer_addr;
158 u16 lcl_port; // network order
159 u16 peer_port; // network order
161 u64 client_queue_address;
163 elog_track_t elog_track;
164 vce_event_handler_reg_t *poll_reg;
167 typedef struct vppcom_cfg_t_
170 u32 vpp_api_q_length;
173 u32 add_segment_size;
174 u32 preallocated_fifo_pairs;
177 u32 event_queue_size;
178 u32 listen_queue_size;
179 u8 app_proxy_transport_tcp;
180 u8 app_proxy_transport_udp;
184 u64 namespace_secret;
189 char *event_log_path;
190 u8 *vpp_api_filename;
193 typedef struct vppcom_main_t_
199 /* FIFO for accepted connections - used in epoll/select */
200 clib_spinlock_t session_fifo_lockp;
201 u32 *client_session_index_fifo;
203 /* vpp input queue */
204 svm_queue_t *vl_input_queue;
206 /* API client handle */
209 clib_spinlock_t sessions_lockp;
212 /* Hash table for disconnect processing */
213 uword *session_index_by_vpp_handles;
216 clib_bitmap_t *rd_bitmap;
217 clib_bitmap_t *wr_bitmap;
218 clib_bitmap_t *ex_bitmap;
220 /* Our event queue */
221 svm_queue_t *app_event_queue;
223 /* unique segment name counter */
224 u32 unique_segment_index;
226 /* For deadman timers */
227 clib_time_t clib_time;
229 /* State of the connection, shared between msg RX thread and main thread */
230 volatile app_state_t app_state;
235 vce_event_thread_t event_thread;
238 vppcom_session_io_thread_t session_io_thread;
240 /* VPP Event-logger */
241 elog_main_t elog_main;
242 elog_track_t elog_track;
244 /* VNET_API_ERROR_FOO -> "Foo" hash table */
245 uword *error_string_by_error_number;
248 /* NOTE: _vppcom_main is only used until the heap is allocated.
249 * Do not access it directly -- use vcm which will point to
250 * the heap allocated copy after init.
252 static vppcom_main_t _vppcom_main = {
253 .debug = VPPCOM_DEBUG_INIT,
254 .my_client_index = ~0
257 static vppcom_main_t *vcm = &_vppcom_main;
259 #define VCL_SESSION_LOCK_AND_GET(I, S) \
261 clib_spinlock_lock (&vcm->sessions_lockp); \
262 rv = vppcom_session_at_index (I, S); \
263 if (PREDICT_FALSE (rv)) \
265 clib_spinlock_unlock (&vcm->sessions_lockp); \
266 clib_warning ("VCL<%d>: ERROR: Invalid ##I (%u)!", \
272 #define VCL_SESSION_LOCK() clib_spinlock_lock (&(vcm->sessions_lockp))
273 #define VCL_SESSION_UNLOCK() clib_spinlock_unlock (&(vcm->sessions_lockp))
275 #define VCL_IO_SESSIONS_LOCK() \
276 clib_spinlock_lock (&(vcm->session_io_thread.io_sessions_lockp))
277 #define VCL_IO_SESSIONS_UNLOCK() \
278 clib_spinlock_unlock (&(vcm->session_io_thread.io_sessions_lockp))
280 #define VCL_ACCEPT_FIFO_LOCK() clib_spinlock_lock (&(vcm->session_fifo_lockp))
281 #define VCL_ACCEPT_FIFO_UNLOCK() \
282 clib_spinlock_unlock (&(vcm->session_fifo_lockp))
284 #define VCL_EVENTS_LOCK() \
285 clib_spinlock_lock (&(vcm->event_thread.events_lockp))
286 #define VCL_EVENTS_UNLOCK() \
287 clib_spinlock_unlock (&(vcm->event_thread.events_lockp))
290 vppcom_app_state_str (app_state_t state)
296 case STATE_APP_START:
297 st = "STATE_APP_START";
300 case STATE_APP_CONN_VPP:
301 st = "STATE_APP_CONN_VPP";
304 case STATE_APP_ENABLED:
305 st = "STATE_APP_ENABLED";
308 case STATE_APP_ATTACHED:
309 st = "STATE_APP_ATTACHED";
313 st = "UNKNOWN_APP_STATE";
321 vppcom_session_state_str (session_state_t state)
332 st = "STATE_CONNECT";
343 case STATE_CLOSE_ON_EMPTY:
344 st = "STATE_CLOSE_ON_EMPTY";
347 case STATE_DISCONNECT:
348 st = "STATE_DISCONNECT";
356 st = "UNKNOWN_STATE";
365 * VPPCOM Utility Functions
369 vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
371 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
372 if (PREDICT_FALSE ((session_index == ~0) ||
373 pool_is_free_index (vcm->sessions, session_index)))
375 clib_warning ("VCL<%d>: invalid session, sid (%u) has been closed!",
376 getpid (), session_index);
377 return VPPCOM_EBADFD;
379 *sess = pool_elt_at_index (vcm->sessions, session_index);
384 vppcom_session_table_add_listener (u64 listener_handle, u32 value)
386 /* Session and listener handles have different formats. The latter has
387 * the thread index in the upper 32 bits while the former has the session
388 * type. Knowing that, for listeners we just flip the MSB to 1 */
389 listener_handle |= 1ULL << 63;
390 hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
393 static inline session_t *
394 vppcom_session_table_lookup_listener (u64 listener_handle)
397 u64 handle = listener_handle | (1ULL << 63);
400 p = hash_get (vcm->session_index_by_vpp_handles, handle);
403 clib_warning ("VCL<%d>: couldn't find listen session: unknown vpp "
404 "listener handle %llx", getpid (), listener_handle);
407 if (pool_is_free_index (vcm->sessions, p[0]))
409 if (VPPCOM_DEBUG > 1)
410 clib_warning ("VCL<%d>: invalid listen session, sid (%u)",
415 session = pool_elt_at_index (vcm->sessions, p[0]);
416 ASSERT (session->state & STATE_LISTEN);
421 vppcom_session_table_del_listener (u64 listener_handle)
423 listener_handle |= 1ULL << 63;
424 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
430 elog_main_t *em = &vcm->elog_main;
432 clib_error_t *error = 0;
435 (char *) format (0, "%s/%d-%d-vcl-elog%c", vcm->cfg.event_log_path,
436 vcm->my_client_index, getpid (), 0);
437 error = elog_write_file (em, chroot_file, 1 /* flush ring */ );
440 clib_error_report (error);
442 if (VPPCOM_DEBUG > 0)
443 clib_warning ("[%d] Event Log:'%s' ", getpid (), chroot_file);
448 vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
450 vl_api_accept_session_reply_t *rmp;
452 rmp = vl_msg_api_alloc (sizeof (*rmp));
453 memset (rmp, 0, sizeof (*rmp));
454 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
455 rmp->retval = htonl (retval);
456 rmp->context = context;
457 rmp->handle = handle;
458 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
462 vppcom_connect_to_vpp (char *app_name)
464 api_main_t *am = &api_main;
465 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
468 if (!vcl_cfg->vpp_api_filename)
469 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
471 if (VPPCOM_DEBUG > 0)
472 clib_warning ("VCL<%d>: app (%s) connecting to VPP api (%s)...",
473 getpid (), app_name, vcl_cfg->vpp_api_filename);
475 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename, app_name,
476 vcm->cfg.vpp_api_q_length) < 0)
478 clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (), app_name);
479 rv = VPPCOM_ECONNREFUSED;
483 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
484 vcm->my_client_index = (u32) am->my_client_index;
485 vcm->app_state = STATE_APP_CONN_VPP;
487 if (VPPCOM_DEBUG > 0)
488 clib_warning ("VCL<%d>: app (%s) is connected to VPP!",
489 getpid (), app_name);
492 if (VPPCOM_DEBUG > 0)
494 vcm->elog_main.lock =
495 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
496 vcm->elog_main.lock[0] = 0;
497 vcm->elog_main.event_ring_size = vcm->cfg.event_ring_size;
498 elog_init (&vcm->elog_main, vcm->elog_main.event_ring_size);
499 elog_enable_disable (&vcm->elog_main, 1);
501 vcm->elog_track.name =
502 (char *) format (0, "P:%d:C:%d%c", getpid (),
503 vcm->my_client_index, 0);
504 elog_track_register (&vcm->elog_main, &vcm->elog_track);
507 ELOG_TYPE_DECLARE (e) =
509 .format = "connect_vpp:rv:%d",
516 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
524 format_api_error (u8 * s, va_list * args)
526 i32 error = va_arg (*args, u32);
529 p = hash_get (vcm->error_string_by_error_number, -error);
532 s = format (s, "%s (%d)", p[0], error);
534 s = format (s, "%d", error);
539 vppcom_init_error_string_table (void)
541 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
543 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
544 foreach_vnet_api_error;
547 hash_set (vcm->error_string_by_error_number, 99, "Misc");
551 vppcom_wait_for_app_state_change (app_state_t app_state)
553 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
555 while (clib_time_now (&vcm->clib_time) < timeout)
557 if (vcm->app_state == app_state)
560 if (VPPCOM_DEBUG > 0)
561 clib_warning ("VCL<%d>: timeout waiting for state %s (%d)", getpid (),
562 vppcom_app_state_str (app_state), app_state);
564 if (VPPCOM_DEBUG > 0)
567 ELOG_TYPE_DECLARE (e) =
569 .format = "ERR: timeout state:%d",
577 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
579 ed->data = app_state;
583 return VPPCOM_ETIMEDOUT;
587 vppcom_wait_for_session_state_change (u32 session_index,
588 session_state_t state,
591 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
592 session_t *volatile session;
598 rv = vppcom_session_at_index (session_index, &session);
599 if (PREDICT_FALSE (rv))
601 VCL_SESSION_UNLOCK ();
604 if (session->state & state)
606 VCL_SESSION_UNLOCK ();
609 if (session->state & STATE_FAILED)
611 VCL_SESSION_UNLOCK ();
612 return VPPCOM_ECONNREFUSED;
615 VCL_SESSION_UNLOCK ();
617 while (clib_time_now (&vcm->clib_time) < timeout);
619 if (VPPCOM_DEBUG > 0)
620 clib_warning ("VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (),
621 state, vppcom_session_state_str (state));
623 if (VPPCOM_DEBUG > 0)
626 ELOG_TYPE_DECLARE (e) =
628 .format = "ERR: timeout state:%d",
636 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
642 return VPPCOM_ETIMEDOUT;
646 * VPP-API message functions
649 vppcom_send_session_enable_disable (u8 is_enable)
651 vl_api_session_enable_disable_t *bmp;
652 bmp = vl_msg_api_alloc (sizeof (*bmp));
653 memset (bmp, 0, sizeof (*bmp));
655 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
656 bmp->client_index = vcm->my_client_index;
657 bmp->context = htonl (0xfeedface);
658 bmp->is_enable = is_enable;
659 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
663 vppcom_app_session_enable (void)
667 if (vcm->app_state != STATE_APP_ENABLED)
669 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
670 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
671 if (PREDICT_FALSE (rv))
673 if (VPPCOM_DEBUG > 0)
674 clib_warning ("VCL<%d>: application session enable timed out! "
676 getpid (), rv, vppcom_retval_str (rv));
684 vl_api_session_enable_disable_reply_t_handler
685 (vl_api_session_enable_disable_reply_t * mp)
689 clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
690 format_api_error, ntohl (mp->retval));
693 vcm->app_state = STATE_APP_ENABLED;
697 vppcom_app_send_attach (void)
699 vl_api_application_attach_t *bmp;
700 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
701 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
702 vcm->cfg.app_proxy_transport_udp);
704 bmp = vl_msg_api_alloc (sizeof (*bmp));
705 memset (bmp, 0, sizeof (*bmp));
707 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
708 bmp->client_index = vcm->my_client_index;
709 bmp->context = htonl (0xfeedface);
710 bmp->options[APP_OPTIONS_FLAGS] =
711 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
712 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
713 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
714 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
715 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
716 (u64) ((vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
717 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0));
718 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
719 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
720 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
721 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
722 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
723 vcm->cfg.preallocated_fifo_pairs;
724 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
727 bmp->namespace_id_len = nsid_len;
728 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
729 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
731 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
735 vppcom_app_attach (void)
739 vppcom_app_send_attach ();
740 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
741 if (PREDICT_FALSE (rv))
743 if (VPPCOM_DEBUG > 0)
744 clib_warning ("VCL<%d>: application attach timed out! "
746 getpid (), rv, vppcom_retval_str (rv));
754 vppcom_app_detach (void)
756 vl_api_application_detach_t *bmp;
757 bmp = vl_msg_api_alloc (sizeof (*bmp));
758 memset (bmp, 0, sizeof (*bmp));
760 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
761 bmp->client_index = vcm->my_client_index;
762 bmp->context = htonl (0xfeedface);
763 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
767 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
770 static svm_fifo_segment_create_args_t _a;
771 svm_fifo_segment_create_args_t *a = &_a;
774 memset (a, 0, sizeof (*a));
777 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
778 format_api_error, ntohl (mp->retval));
782 if (mp->segment_name_length == 0)
784 clib_warning ("VCL<%d>: segment_name_length zero", getpid ());
788 a->segment_name = (char *) mp->segment_name;
789 a->segment_size = mp->segment_size;
791 ASSERT (mp->app_event_queue_address);
793 /* Attach to the segment vpp created */
794 rv = svm_fifo_segment_attach (a);
795 vec_reset_length (a->new_segment_indices);
796 if (PREDICT_FALSE (rv))
798 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
799 getpid (), mp->segment_name);
803 vcm->app_event_queue =
804 uword_to_pointer (mp->app_event_queue_address, svm_queue_t *);
806 vcm->app_state = STATE_APP_ATTACHED;
810 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
814 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
817 vcm->app_state = STATE_APP_ENABLED;
821 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
825 clib_warning ("VCL<%d>: vpp handle 0x%llx: disconnect session failed: %U",
826 getpid (), mp->handle, format_api_error,
831 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
833 static svm_fifo_segment_create_args_t _a;
834 svm_fifo_segment_create_args_t *a = &_a;
837 memset (a, 0, sizeof (*a));
838 a->segment_name = (char *) mp->segment_name;
839 a->segment_size = mp->segment_size;
840 /* Attach to the segment vpp created */
841 rv = svm_fifo_segment_attach (a);
842 vec_reset_length (a->new_segment_indices);
843 if (PREDICT_FALSE (rv))
845 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
846 getpid (), mp->segment_name);
849 if (VPPCOM_DEBUG > 1)
850 clib_warning ("VCL<%d>: mapped new segment '%s' size %d", getpid (),
851 mp->segment_name, mp->segment_size);
855 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
859 * XXX Need segment_name to session_id hash,
860 * XXX - have sessionID by handle hash currently
862 if (VPPCOM_DEBUG > 1)
863 clib_warning ("Unmapped segment '%s'", mp->segment_name);
867 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
871 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
875 session_t *session = 0;
876 u32 session_index = p[0];
878 VCL_SESSION_LOCK_AND_GET (session_index, &session);
879 session->state = STATE_CLOSE_ON_EMPTY;
881 if (VPPCOM_DEBUG > 1)
882 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
883 "setting state to 0x%x (%s)",
884 getpid (), mp->handle, session_index, session->state,
885 vppcom_session_state_str (session->state));
886 VCL_SESSION_UNLOCK ();
890 if (VPPCOM_DEBUG > 1)
891 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
892 "session lookup failed!",
893 getpid (), mp->handle, session_index);
896 clib_warning ("VCL<%d>: vpp handle 0x%llx: session lookup by "
897 "handle failed!", getpid (), mp->handle);
901 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
903 session_t *session = 0;
904 vl_api_reset_session_reply_t *rmp;
908 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
913 rval = vppcom_session_at_index (p[0], &session);
914 if (PREDICT_FALSE (rval))
916 rv = VNET_API_ERROR_INVALID_VALUE_2;
917 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
918 "session lookup failed! returning %d %U",
919 getpid (), mp->handle, p[0],
920 rv, format_api_error, rv);
924 /* TBD: should this disconnect immediately and
927 session->state = STATE_CLOSE_ON_EMPTY;
929 if (VPPCOM_DEBUG > 1)
930 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
931 "state set to %d (%s)!", getpid (),
932 mp->handle, p[0], session->state,
933 vppcom_session_state_str (session->state));
935 VCL_SESSION_UNLOCK ();
939 rv = VNET_API_ERROR_INVALID_VALUE;
940 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx: session lookup "
941 "failed! returning %d %U",
942 getpid (), mp->handle, rv, format_api_error, rv);
945 rmp = vl_msg_api_alloc (sizeof (*rmp));
946 memset (rmp, 0, sizeof (*rmp));
947 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
948 rmp->retval = htonl (rv);
949 rmp->handle = mp->handle;
950 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
954 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
956 session_t *session = 0;
958 svm_fifo_t *rx_fifo, *tx_fifo;
961 session_index = mp->context;
962 VCL_SESSION_LOCK_AND_GET (session_index, &session);
966 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
967 "connect failed! %U",
968 getpid (), mp->handle, session_index,
969 format_api_error, ntohl (mp->retval));
972 session->state = STATE_FAILED;
973 session->vpp_handle = mp->handle;
977 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
978 "Invalid session index (%u)!",
979 getpid (), mp->handle, session_index);
990 if (vcm->session_io_thread.io_sessions_lockp)
992 // Add this connection to the active io sessions list
993 VCL_IO_SESSIONS_LOCK ();
994 u32 *active_session_index;
995 pool_get (vcm->session_io_thread.active_session_indexes,
996 active_session_index);
997 *active_session_index = session_index;
998 VCL_IO_SESSIONS_UNLOCK ();
1000 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1003 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1004 rx_fifo->client_session_index = session_index;
1005 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1006 tx_fifo->client_session_index = session_index;
1008 session->rx_fifo = rx_fifo;
1009 session->tx_fifo = tx_fifo;
1010 session->vpp_handle = mp->handle;
1011 session->lcl_addr.is_ip4 = mp->is_ip4;
1012 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
1013 sizeof (session->peer_addr.ip46));
1014 session->lcl_port = mp->lcl_port;
1015 session->state = STATE_CONNECT;
1017 /* Add it to lookup table */
1018 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1020 if (VPPCOM_DEBUG > 1)
1021 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded!"
1022 " session_rx_fifo %p, refcnt %d,"
1023 " session_tx_fifo %p, refcnt %d",
1024 getpid (), mp->handle, session_index,
1026 session->rx_fifo->refcnt,
1027 session->tx_fifo, session->tx_fifo->refcnt);
1029 VCL_SESSION_UNLOCK ();
1033 vppcom_send_connect_sock (session_t * session, u32 session_index)
1035 vl_api_connect_sock_t *cmp;
1037 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
1038 cmp = vl_msg_api_alloc (sizeof (*cmp));
1039 memset (cmp, 0, sizeof (*cmp));
1040 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
1041 cmp->client_index = vcm->my_client_index;
1042 cmp->context = session_index;
1044 cmp->is_ip4 = session->peer_addr.is_ip4;
1045 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
1046 cmp->port = session->peer_port;
1047 cmp->proto = session->proto;
1048 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
1049 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
1053 vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
1056 vl_api_disconnect_session_reply_t *rmp;
1058 if (VPPCOM_DEBUG > 1)
1059 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1060 "sending disconnect msg",
1061 getpid (), vpp_handle, session_index);
1063 rmp = vl_msg_api_alloc (sizeof (*rmp));
1064 memset (rmp, 0, sizeof (*rmp));
1066 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
1067 rmp->retval = htonl (rv);
1068 rmp->handle = vpp_handle;
1069 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1073 vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
1075 vl_api_disconnect_session_t *dmp;
1077 if (VPPCOM_DEBUG > 1)
1078 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1079 "sending disconnect msg",
1080 getpid (), vpp_handle, session_index);
1082 dmp = vl_msg_api_alloc (sizeof (*dmp));
1083 memset (dmp, 0, sizeof (*dmp));
1084 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
1085 dmp->client_index = vcm->my_client_index;
1086 dmp->handle = vpp_handle;
1087 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
1091 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
1093 session_t *session = 0;
1094 u32 session_index = mp->context;
1097 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1101 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, "
1102 "sid %u: bind failed: %U",
1103 getpid (), mp->handle, session_index,
1104 format_api_error, ntohl (mp->retval));
1105 rv = vppcom_session_at_index (session_index, &session);
1106 if (rv == VPPCOM_OK)
1108 session->state = STATE_FAILED;
1109 session->vpp_handle = mp->handle;
1113 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
1114 "Invalid session index (%u)!",
1115 getpid (), mp->handle, session_index);
1120 session->vpp_handle = mp->handle;
1121 session->lcl_addr.is_ip4 = mp->lcl_is_ip4;
1122 session->lcl_addr.ip46 = to_ip46 (!mp->lcl_is_ip4, mp->lcl_ip);
1123 session->lcl_port = mp->lcl_port;
1124 vppcom_session_table_add_listener (mp->handle, session_index);
1125 session->state = STATE_LISTEN;
1127 if (VPPCOM_DEBUG > 1)
1128 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
1129 getpid (), mp->handle, mp->context);
1131 VCL_SESSION_UNLOCK ();
1135 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
1138 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
1139 getpid (), mp->context, format_api_error,
1140 ntohl (mp->retval));
1142 else if (VPPCOM_DEBUG > 1)
1143 clib_warning ("VCL<%d>: sid %u: unbind succeeded!",
1144 getpid (), mp->context);
1148 format_ip4_address (u8 * s, va_list * args)
1150 u8 *a = va_arg (*args, u8 *);
1151 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1155 format_ip6_address (u8 * s, va_list * args)
1157 ip6_address_t *a = va_arg (*args, ip6_address_t *);
1158 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
1160 i_max_n_zero = ARRAY_LEN (a->as_u16);
1162 i_first_zero = i_max_n_zero;
1164 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1166 u32 is_zero = a->as_u16[i] == 0;
1167 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1173 if ((!is_zero && n_zeros > max_n_zeros)
1174 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1176 i_max_n_zero = i_first_zero;
1177 max_n_zeros = n_zeros;
1178 i_first_zero = ARRAY_LEN (a->as_u16);
1183 last_double_colon = 0;
1184 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1186 if (i == i_max_n_zero && max_n_zeros > 1)
1188 s = format (s, "::");
1189 i += max_n_zeros - 1;
1190 last_double_colon = 1;
1194 s = format (s, "%s%x",
1195 (last_double_colon || i == 0) ? "" : ":",
1196 clib_net_to_host_u16 (a->as_u16[i]));
1197 last_double_colon = 0;
1204 /* Format an IP46 address. */
1206 format_ip46_address (u8 * s, va_list * args)
1208 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1209 ip46_type_t type = va_arg (*args, ip46_type_t);
1215 is_ip4 = ip46_address_is_ip4 (ip46);
1226 format (s, "%U", format_ip4_address, &ip46->ip4) :
1227 format (s, "%U", format_ip6_address, &ip46->ip6);
1231 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1233 svm_fifo_t *rx_fifo, *tx_fifo;
1234 session_t *session, *listen_session;
1236 vce_event_connect_request_t *ecr;
1242 VCL_SESSION_LOCK ();
1244 VCL_ACCEPT_FIFO_LOCK ();
1245 elts = clib_fifo_free_elts (vcm->client_session_index_fifo);
1246 VCL_ACCEPT_FIFO_UNLOCK ();
1250 clib_warning ("VCL<%d>: client session queue is full!", getpid ());
1251 vppcom_send_accept_session_reply (mp->handle, mp->context,
1252 VNET_API_ERROR_QUEUE_FULL);
1253 VCL_SESSION_UNLOCK ();
1257 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1258 if (!listen_session)
1260 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
1261 "unknown vpp listener handle %llx",
1262 getpid (), mp->listener_handle);
1263 vppcom_send_accept_session_reply (mp->handle, mp->context,
1264 VNET_API_ERROR_INVALID_ARGUMENT);
1265 VCL_SESSION_UNLOCK ();
1269 /* TODO check listener depth and update */
1270 /* TODO on "child" fd close, update listener depth */
1272 /* Allocate local session and set it up */
1273 pool_get (vcm->sessions, session);
1274 memset (session, 0, sizeof (*session));
1275 session_index = (u32) (session - vcm->sessions);
1277 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1278 rx_fifo->client_session_index = session_index;
1279 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1280 tx_fifo->client_session_index = session_index;
1282 session->vpp_handle = mp->handle;
1283 session->client_context = mp->context;
1284 session->rx_fifo = rx_fifo;
1285 session->tx_fifo = tx_fifo;
1286 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1288 session->state = STATE_ACCEPT;
1289 session->peer_port = mp->port;
1290 session->peer_addr.is_ip4 = mp->is_ip4;
1291 session->peer_addr.ip46 = to_ip46 (!mp->is_ip4, mp->ip);
1293 /* Add it to lookup table */
1294 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1295 session->lcl_port = listen_session->lcl_port;
1296 session->lcl_addr = listen_session->lcl_addr;
1298 /* Create an event for handlers */
1302 pool_get (vcm->event_thread.vce_events, ev);
1303 ev_idx = (u32) (ev - vcm->event_thread.vce_events);
1304 ecr = vce_get_event_data (ev, sizeof (*ecr));
1305 ev->evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
1306 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1307 ev->evk.session_index = (u32) (listen_session - vcm->sessions);
1308 ecr->accepted_session_index = session_index;
1310 VCL_EVENTS_UNLOCK ();
1312 rv = vce_generate_event (&vcm->event_thread, ev_idx);
1315 if (VPPCOM_DEBUG > 1)
1316 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client accept "
1317 "request from %s address %U port %d queue %p!", getpid (),
1318 mp->handle, session_index, mp->is_ip4 ? "IPv4" : "IPv6",
1319 format_ip46_address, &mp->ip,
1320 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1321 clib_net_to_host_u16 (mp->port), session->vpp_event_queue);
1323 if (VPPCOM_DEBUG > 0)
1325 session->elog_track.name =
1326 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
1328 elog_track_register (&vcm->elog_main, &session->elog_track);
1330 if (session->peer_addr.is_ip4)
1333 ELOG_TYPE_DECLARE (e) =
1336 "client_accept:handle:%x addr:%d.%d.%d.%d:%d",
1337 .format_args = "i8i1i1i1i1i2",
1340 CLIB_PACKED (struct {
1346 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1348 ed->handle = mp->handle;
1349 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
1350 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
1351 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
1352 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
1353 ed->port = clib_net_to_host_u16 (session->peer_port);
1358 clib_warning ("ip6");
1362 VCL_SESSION_UNLOCK ();
1366 /* VPP combines bind and listen as one operation. VCL manages the separation
1367 * of bind and listen locally via vppcom_session_bind() and
1368 * vppcom_session_listen() */
1370 vppcom_send_bind_sock (session_t * session, u32 session_index)
1372 vl_api_bind_sock_t *bmp;
1374 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1375 bmp = vl_msg_api_alloc (sizeof (*bmp));
1376 memset (bmp, 0, sizeof (*bmp));
1378 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1379 bmp->client_index = vcm->my_client_index;
1380 bmp->context = session_index;
1381 bmp->is_ip4 = session->lcl_addr.is_ip4;
1382 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1383 bmp->port = session->lcl_port;
1384 bmp->proto = session->proto;
1385 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1386 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1390 vppcom_send_unbind_sock (u64 vpp_handle)
1392 vl_api_unbind_sock_t *ump;
1394 ump = vl_msg_api_alloc (sizeof (*ump));
1395 memset (ump, 0, sizeof (*ump));
1397 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1398 ump->client_index = vcm->my_client_index;
1399 ump->handle = vpp_handle;
1400 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1404 vppcom_session_unbind (u32 session_index)
1406 session_t *session = 0;
1409 elog_track_t session_elog_track;
1411 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1413 vpp_handle = session->vpp_handle;
1414 vppcom_session_table_del_listener (vpp_handle);
1415 session->vpp_handle = ~0;
1416 session->state = STATE_DISCONNECT;
1417 session_elog_track = session->elog_track;
1419 VCL_SESSION_UNLOCK ();
1421 if (VPPCOM_DEBUG > 1)
1422 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1423 "sending unbind msg! new state 0x%x (%s)",
1424 getpid (), vpp_handle, session_index,
1426 vppcom_session_state_str (STATE_DISCONNECT));
1428 if (VPPCOM_DEBUG > 0)
1431 ELOG_TYPE_DECLARE (e) =
1433 .format = "unbind: handle:%x",
1434 .format_args = "i8",
1442 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
1443 ed->handle = vpp_handle;
1447 vppcom_send_unbind_sock (vpp_handle);
1454 vppcom_session_disconnect (u32 session_index)
1459 session_state_t state;
1461 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1463 vpp_handle = session->vpp_handle;
1464 state = session->state;
1465 VCL_SESSION_UNLOCK ();
1467 if (VPPCOM_DEBUG > 1)
1469 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u "
1471 getpid (), vpp_handle, session_index,
1472 state, vppcom_session_state_str (state));
1475 if (PREDICT_FALSE (state & STATE_LISTEN))
1477 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1478 "Cannot disconnect a listen socket!",
1479 getpid (), vpp_handle, session_index);
1484 /* The peer has already initiated the close,
1485 * so send the disconnect session reply.
1487 if (state & STATE_CLOSE_ON_EMPTY)
1489 //XXX alagalah - Check and drain here?
1490 vppcom_send_disconnect_session_reply (vpp_handle,
1491 session_index, 0 /* rv */ );
1492 if (VPPCOM_DEBUG > 1)
1493 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1494 "sending disconnect REPLY...",
1495 getpid (), vpp_handle, session_index);
1498 /* Otherwise, send a disconnect session msg...
1502 if (VPPCOM_DEBUG > 1)
1503 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1504 "sending disconnect...",
1505 getpid (), vpp_handle, session_index);
1507 vppcom_send_disconnect_session (vpp_handle, session_index);
1514 #define foreach_sock_msg \
1515 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1516 _(BIND_SOCK_REPLY, bind_sock_reply) \
1517 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1518 _(ACCEPT_SESSION, accept_session) \
1519 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1520 _(DISCONNECT_SESSION, disconnect_session) \
1521 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1522 _(RESET_SESSION, reset_session) \
1523 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1524 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1525 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
1526 _(UNMAP_SEGMENT, unmap_segment)
1529 vppcom_api_hookup (void)
1532 vl_msg_api_set_handlers(VL_API_##N, #n, \
1533 vl_api_##n##_t_handler, \
1535 vl_api_##n##_t_endian, \
1536 vl_api_##n##_t_print, \
1537 sizeof(vl_api_##n##_t), 1);
1543 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1547 vcl_cfg->heapsize = (256ULL << 20);
1548 vcl_cfg->vpp_api_q_length = 1024;
1549 vcl_cfg->segment_baseva = 0x200000000ULL;
1550 vcl_cfg->segment_size = (256 << 20);
1551 vcl_cfg->add_segment_size = (128 << 20);
1552 vcl_cfg->preallocated_fifo_pairs = 8;
1553 vcl_cfg->rx_fifo_size = (1 << 20);
1554 vcl_cfg->tx_fifo_size = (1 << 20);
1555 vcl_cfg->event_queue_size = 2048;
1556 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1557 vcl_cfg->app_timeout = 10 * 60.0;
1558 vcl_cfg->session_timeout = 10 * 60.0;
1559 vcl_cfg->accept_timeout = 60.0;
1560 vcl_cfg->event_ring_size = (128 << 10);
1561 vcl_cfg->event_log_path = "/dev/shm";
1565 vppcom_cfg_heapsize (char *conf_fname)
1567 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1580 fp = fopen (conf_fname, "r");
1583 if (VPPCOM_DEBUG > 0)
1584 clib_warning ("VCL<%d>: using default heapsize %lld (0x%llx)",
1585 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1589 argv = calloc (1, sizeof (char *));
1592 if (VPPCOM_DEBUG > 0)
1593 clib_warning ("VCL<%d>: calloc failed, using default "
1594 "heapsize %lld (0x%llx)",
1595 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1601 if (fgets (inbuf, 4096, fp) == 0)
1603 p = strtok (inbuf, " \t\n");
1609 char **tmp = realloc (argv, argc * sizeof (char *));
1612 if (VPPCOM_DEBUG > 0)
1613 clib_warning ("VCL<%d>: realloc failed, "
1614 "using default heapsize %lld (0x%llx)",
1615 getpid (), vcl_cfg->heapsize,
1620 arg = strndup (p, 1024);
1623 if (VPPCOM_DEBUG > 0)
1624 clib_warning ("VCL<%d>: strndup failed, "
1625 "using default heapsize %lld (0x%llx)",
1626 getpid (), vcl_cfg->heapsize,
1630 argv[argc - 1] = arg;
1631 p = strtok (NULL, " \t\n");
1638 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1641 if (VPPCOM_DEBUG > 0)
1642 clib_warning ("VCL<%d>: realloc failed, "
1643 "using default heapsize %lld (0x%llx)",
1644 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1651 * Look for and parse the "heapsize" config parameter.
1652 * Manual since none of the clib infra has been bootstrapped yet.
1654 * Format: heapsize <nn>[mM][gG]
1657 for (i = 1; i < (argc - 1); i++)
1659 if (!strncmp (argv[i], "heapsize", 8))
1661 sizep = (u8 *) argv[i + 1];
1663 while (*sizep >= '0' && *sizep <= '9')
1666 size += *sizep++ - '0';
1670 if (VPPCOM_DEBUG > 0)
1671 clib_warning ("VCL<%d>: parse error '%s %s', "
1672 "using default heapsize %lld (0x%llx)",
1673 getpid (), argv[i], argv[i + 1],
1674 vcl_cfg->heapsize, vcl_cfg->heapsize);
1678 if (*sizep == 'g' || *sizep == 'G')
1679 vcl_cfg->heapsize = size << 30;
1680 else if (*sizep == 'm' || *sizep == 'M')
1681 vcl_cfg->heapsize = size << 20;
1684 if (VPPCOM_DEBUG > 0)
1685 clib_warning ("VCL<%d>: parse error '%s %s', "
1686 "using default heapsize %lld (0x%llx)",
1687 getpid (), argv[i], argv[i + 1],
1688 vcl_cfg->heapsize, vcl_cfg->heapsize);
1700 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1701 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1702 if (vcl_mem == MAP_FAILED)
1704 clib_unix_error ("VCL<%d>: ERROR: mmap(0, %lld == 0x%llx, "
1705 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1707 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1708 ASSERT (vcl_mem != MAP_FAILED);
1711 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1714 clib_warning ("VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
1718 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1721 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
1726 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1729 if (VPPCOM_DEBUG > 0)
1730 clib_warning ("VCL<%d>: allocated VCL heap = %p, size %lld (0x%llx)",
1731 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
1735 vppcom_cfg_read (char *conf_fname)
1737 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1739 unformat_input_t _input, *input = &_input;
1740 unformat_input_t _line_input, *line_input = &_line_input;
1741 u8 vc_cfg_input = 0;
1744 u32 uid, gid, q_len;
1746 fd = open (conf_fname, O_RDONLY);
1749 if (VPPCOM_DEBUG > 0)
1750 clib_warning ("VCL<%d>: using default configuration.",
1751 getpid (), conf_fname);
1755 if (fstat (fd, &s) < 0)
1757 if (VPPCOM_DEBUG > 0)
1758 clib_warning ("VCL<%d>: failed to stat `%s', "
1759 "using default configuration", getpid (), conf_fname);
1763 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1765 if (VPPCOM_DEBUG > 0)
1766 clib_warning ("VCL<%d>: not a regular file `%s', "
1767 "using default configuration", getpid (), conf_fname);
1771 unformat_init_clib_file (input, fd);
1773 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1775 (void) unformat_user (input, unformat_line_input, line_input);
1776 unformat_skip_white_space (line_input);
1778 if (unformat (line_input, "vcl {"))
1786 if (unformat (line_input, "heapsize %s", &chroot_path))
1788 vec_terminate_c_string (chroot_path);
1789 if (VPPCOM_DEBUG > 0)
1790 clib_warning ("VCL<%d>: configured heapsize %s, "
1791 "actual heapsize %lld (0x%llx)",
1792 getpid (), chroot_path, vcl_cfg->heapsize,
1794 vec_free (chroot_path);
1796 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1798 vec_terminate_c_string (chroot_path);
1799 if (vcl_cfg->vpp_api_filename)
1800 vec_free (vcl_cfg->vpp_api_filename);
1801 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
1803 vl_set_memory_root_path ((char *) chroot_path);
1805 if (VPPCOM_DEBUG > 0)
1806 clib_warning ("VCL<%d>: configured api-prefix (%s) and "
1807 "api filename (%s)", getpid (), chroot_path,
1808 vcl_cfg->vpp_api_filename);
1809 chroot_path = 0; /* Don't vec_free() it! */
1811 else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
1813 if (q_len < vcl_cfg->vpp_api_q_length)
1815 clib_warning ("VCL<%d>: ERROR: configured vpp-api-q-length "
1816 "(%u) is too small! Using default: %u ",
1817 getpid (), q_len, vcl_cfg->vpp_api_q_length);
1821 vcl_cfg->vpp_api_q_length = q_len;
1823 if (VPPCOM_DEBUG > 0)
1824 clib_warning ("VCL<%d>: configured vpp-api-q-length %u",
1825 getpid (), vcl_cfg->vpp_api_q_length);
1828 else if (unformat (line_input, "uid %d", &uid))
1830 vl_set_memory_uid (uid);
1831 if (VPPCOM_DEBUG > 0)
1832 clib_warning ("VCL<%d>: configured uid %d", getpid (), uid);
1834 else if (unformat (line_input, "gid %d", &gid))
1836 vl_set_memory_gid (gid);
1837 if (VPPCOM_DEBUG > 0)
1838 clib_warning ("VCL<%d>: configured gid %d", getpid (), gid);
1840 else if (unformat (line_input, "segment-baseva 0x%lx",
1841 &vcl_cfg->segment_baseva))
1843 if (VPPCOM_DEBUG > 0)
1844 clib_warning ("VCL<%d>: configured segment_baseva 0x%lx",
1845 getpid (), vcl_cfg->segment_baseva);
1847 else if (unformat (line_input, "segment-size 0x%lx",
1848 &vcl_cfg->segment_size))
1850 if (VPPCOM_DEBUG > 0)
1851 clib_warning ("VCL<%d>: configured segment_size 0x%lx (%ld)",
1852 getpid (), vcl_cfg->segment_size,
1853 vcl_cfg->segment_size);
1855 else if (unformat (line_input, "segment-size %ld",
1856 &vcl_cfg->segment_size))
1858 if (VPPCOM_DEBUG > 0)
1859 clib_warning ("VCL<%d>: configured segment_size %ld (0x%lx)",
1860 getpid (), vcl_cfg->segment_size,
1861 vcl_cfg->segment_size);
1863 else if (unformat (line_input, "add-segment-size 0x%lx",
1864 &vcl_cfg->add_segment_size))
1866 if (VPPCOM_DEBUG > 0)
1868 ("VCL<%d>: configured add_segment_size 0x%lx (%ld)",
1869 getpid (), vcl_cfg->add_segment_size,
1870 vcl_cfg->add_segment_size);
1872 else if (unformat (line_input, "add-segment-size %ld",
1873 &vcl_cfg->add_segment_size))
1875 if (VPPCOM_DEBUG > 0)
1877 ("VCL<%d>: configured add_segment_size %ld (0x%lx)",
1878 getpid (), vcl_cfg->add_segment_size,
1879 vcl_cfg->add_segment_size);
1881 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1882 &vcl_cfg->preallocated_fifo_pairs))
1884 if (VPPCOM_DEBUG > 0)
1885 clib_warning ("VCL<%d>: configured preallocated_fifo_pairs "
1886 "%d (0x%x)", getpid (),
1887 vcl_cfg->preallocated_fifo_pairs,
1888 vcl_cfg->preallocated_fifo_pairs);
1890 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1891 &vcl_cfg->rx_fifo_size))
1893 if (VPPCOM_DEBUG > 0)
1894 clib_warning ("VCL<%d>: configured rx_fifo_size 0x%lx (%ld)",
1895 getpid (), vcl_cfg->rx_fifo_size,
1896 vcl_cfg->rx_fifo_size);
1898 else if (unformat (line_input, "rx-fifo-size %ld",
1899 &vcl_cfg->rx_fifo_size))
1901 if (VPPCOM_DEBUG > 0)
1902 clib_warning ("VCL<%d>: configured rx_fifo_size %ld (0x%lx)",
1903 getpid (), vcl_cfg->rx_fifo_size,
1904 vcl_cfg->rx_fifo_size);
1906 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1907 &vcl_cfg->tx_fifo_size))
1909 if (VPPCOM_DEBUG > 0)
1910 clib_warning ("VCL<%d>: configured tx_fifo_size 0x%lx (%ld)",
1911 getpid (), vcl_cfg->tx_fifo_size,
1912 vcl_cfg->tx_fifo_size);
1914 else if (unformat (line_input, "tx-fifo-size %ld",
1915 &vcl_cfg->tx_fifo_size))
1917 if (VPPCOM_DEBUG > 0)
1918 clib_warning ("VCL<%d>: configured tx_fifo_size %ld (0x%lx)",
1919 getpid (), vcl_cfg->tx_fifo_size,
1920 vcl_cfg->tx_fifo_size);
1922 else if (unformat (line_input, "event-queue-size 0x%lx",
1923 &vcl_cfg->event_queue_size))
1925 if (VPPCOM_DEBUG > 0)
1926 clib_warning ("VCL<%d>: configured event_queue_size "
1928 getpid (), vcl_cfg->event_queue_size,
1929 vcl_cfg->event_queue_size);
1931 else if (unformat (line_input, "event-queue-size %ld",
1932 &vcl_cfg->event_queue_size))
1934 if (VPPCOM_DEBUG > 0)
1935 clib_warning ("VCL<%d>: configured event_queue_size "
1937 getpid (), vcl_cfg->event_queue_size,
1938 vcl_cfg->event_queue_size);
1940 else if (unformat (line_input, "listen-queue-size 0x%lx",
1941 &vcl_cfg->listen_queue_size))
1943 if (VPPCOM_DEBUG > 0)
1944 clib_warning ("VCL<%d>: configured listen_queue_size "
1946 getpid (), vcl_cfg->listen_queue_size,
1947 vcl_cfg->listen_queue_size);
1949 else if (unformat (line_input, "listen-queue-size %ld",
1950 &vcl_cfg->listen_queue_size))
1952 if (VPPCOM_DEBUG > 0)
1953 clib_warning ("VCL<%d>: configured listen_queue_size "
1955 getpid (), vcl_cfg->listen_queue_size,
1956 vcl_cfg->listen_queue_size);
1958 else if (unformat (line_input, "app-timeout %f",
1959 &vcl_cfg->app_timeout))
1961 if (VPPCOM_DEBUG > 0)
1962 clib_warning ("VCL<%d>: configured app_timeout %f",
1963 getpid (), vcl_cfg->app_timeout);
1965 else if (unformat (line_input, "session-timeout %f",
1966 &vcl_cfg->session_timeout))
1968 if (VPPCOM_DEBUG > 0)
1969 clib_warning ("VCL<%d>: configured session_timeout %f",
1970 getpid (), vcl_cfg->session_timeout);
1972 else if (unformat (line_input, "accept-timeout %f",
1973 &vcl_cfg->accept_timeout))
1975 if (VPPCOM_DEBUG > 0)
1976 clib_warning ("VCL<%d>: configured accept_timeout %f",
1977 getpid (), vcl_cfg->accept_timeout);
1979 else if (unformat (line_input, "app-proxy-transport-tcp"))
1981 vcl_cfg->app_proxy_transport_tcp = 1;
1982 if (VPPCOM_DEBUG > 0)
1983 clib_warning ("VCL<%d>: configured "
1984 "app_proxy_transport_tcp (%d)",
1985 getpid (), vcl_cfg->app_proxy_transport_tcp);
1987 else if (unformat (line_input, "app-proxy-transport-udp"))
1989 vcl_cfg->app_proxy_transport_udp = 1;
1990 if (VPPCOM_DEBUG > 0)
1991 clib_warning ("VCL<%d>: configured "
1992 "app_proxy_transport_udp (%d)",
1993 getpid (), vcl_cfg->app_proxy_transport_udp);
1995 else if (unformat (line_input, "app-scope-local"))
1997 vcl_cfg->app_scope_local = 1;
1998 if (VPPCOM_DEBUG > 0)
1999 clib_warning ("VCL<%d>: configured app_scope_local (%d)",
2000 getpid (), vcl_cfg->app_scope_local);
2002 else if (unformat (line_input, "app-scope-global"))
2004 vcl_cfg->app_scope_global = 1;
2005 if (VPPCOM_DEBUG > 0)
2006 clib_warning ("VCL<%d>: configured app_scope_global (%d)",
2007 getpid (), vcl_cfg->app_scope_global);
2009 else if (unformat (line_input, "namespace-secret %lu",
2010 &vcl_cfg->namespace_secret))
2012 if (VPPCOM_DEBUG > 0)
2014 ("VCL<%d>: configured namespace_secret %lu (0x%lx)",
2015 getpid (), vcl_cfg->namespace_secret,
2016 vcl_cfg->namespace_secret);
2018 else if (unformat (line_input, "namespace-id %v",
2019 &vcl_cfg->namespace_id))
2021 vl_api_application_attach_t *mp;
2022 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
2023 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
2024 if (nsid_vec_len > max_nsid_vec_len)
2026 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
2027 if (VPPCOM_DEBUG > 0)
2028 clib_warning ("VCL<%d>: configured namespace_id is "
2029 "too long, truncated to %d characters!",
2030 getpid (), max_nsid_vec_len);
2033 if (VPPCOM_DEBUG > 0)
2034 clib_warning ("VCL<%d>: configured namespace_id %v",
2035 getpid (), vcl_cfg->namespace_id);
2037 else if (unformat (line_input, "}"))
2040 if (VPPCOM_DEBUG > 0)
2041 clib_warning ("VCL<%d>: completed parsing vppcom config!",
2047 if (line_input->buffer[line_input->index] != '#')
2049 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
2051 &line_input->buffer[line_input->index]);
2058 unformat_free (input);
2066 * VPPCOM Public API functions
2069 vppcom_app_create (char *app_name)
2071 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
2082 vppcom_cfg_init (vcl_cfg);
2083 env_var_str = getenv (VPPCOM_ENV_DEBUG);
2087 if (sscanf (env_var_str, "%u", &tmp) != 1)
2088 clib_warning ("VCL<%d>: WARNING: Invalid debug level specified "
2089 "in the environment variable " VPPCOM_ENV_DEBUG
2090 " (%s)!\n", getpid (), env_var_str);
2094 if (VPPCOM_DEBUG > 0)
2095 clib_warning ("VCL<%d>: configured VCL debug level (%u) from "
2096 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
2099 conf_fname = getenv (VPPCOM_ENV_CONF);
2101 conf_fname = VPPCOM_CONF_DEFAULT;
2102 vppcom_cfg_heapsize (conf_fname);
2103 vcl_cfg = &vcm->cfg;
2104 clib_spinlock_init (&vcm->session_fifo_lockp);
2105 clib_fifo_validate (vcm->client_session_index_fifo,
2106 vcm->cfg.listen_queue_size);
2107 vppcom_cfg_read (conf_fname);
2110 env_var_str = getenv (VPPCOM_ENV_API_PREFIX);
2113 if (vcl_cfg->vpp_api_filename)
2114 vec_free (vcl_cfg->vpp_api_filename);
2115 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
2117 vl_set_memory_root_path ((char *) env_var_str);
2119 if (VPPCOM_DEBUG > 0)
2120 clib_warning ("VCL<%d>: configured api prefix (%s) and "
2121 "filename (%s) from " VPPCOM_ENV_API_PREFIX "!",
2122 getpid (), env_var_str, vcl_cfg->vpp_api_filename);
2124 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
2127 u32 ns_id_vec_len = strlen (env_var_str);
2129 vec_reset_length (vcm->cfg.namespace_id);
2130 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
2131 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
2133 if (VPPCOM_DEBUG > 0)
2134 clib_warning ("VCL<%d>: configured namespace_id (%v) from "
2135 VPPCOM_ENV_APP_NAMESPACE_ID
2136 "!", getpid (), vcm->cfg.namespace_id);
2138 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
2142 if (sscanf (env_var_str, "%lu", &tmp) != 1)
2143 clib_warning ("VCL<%d>: WARNING: Invalid namespace secret "
2144 "specified in the environment variable "
2145 VPPCOM_ENV_APP_NAMESPACE_SECRET
2146 " (%s)!\n", getpid (), env_var_str);
2149 vcm->cfg.namespace_secret = tmp;
2150 if (VPPCOM_DEBUG > 0)
2151 clib_warning ("VCL<%d>: configured namespace secret "
2153 VPPCOM_ENV_APP_NAMESPACE_SECRET
2154 "!", getpid (), vcm->cfg.namespace_secret);
2157 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
2159 vcm->cfg.app_proxy_transport_tcp = 1;
2160 if (VPPCOM_DEBUG > 0)
2161 clib_warning ("VCL<%d>: configured app_proxy_transport_tcp "
2163 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP
2164 "!", getpid (), vcm->cfg.app_proxy_transport_tcp);
2166 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
2168 vcm->cfg.app_proxy_transport_udp = 1;
2169 if (VPPCOM_DEBUG > 0)
2170 clib_warning ("VCL<%d>: configured app_proxy_transport_udp "
2172 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP
2173 "!", getpid (), vcm->cfg.app_proxy_transport_udp);
2175 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
2177 vcm->cfg.app_scope_local = 1;
2178 if (VPPCOM_DEBUG > 0)
2179 clib_warning ("VCL<%d>: configured app_scope_local (%u) from "
2180 VPPCOM_ENV_APP_SCOPE_LOCAL
2181 "!", getpid (), vcm->cfg.app_scope_local);
2183 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
2185 vcm->cfg.app_scope_global = 1;
2186 if (VPPCOM_DEBUG > 0)
2187 clib_warning ("VCL<%d>: configured app_scope_global (%u) from "
2188 VPPCOM_ENV_APP_SCOPE_GLOBAL
2189 "!", getpid (), vcm->cfg.app_scope_global);
2192 vcm->main_cpu = os_get_thread_index ();
2193 heap = clib_mem_get_per_cpu_heap ();
2194 h = mheap_header (heap);
2196 /* make the main heap thread-safe */
2197 h->flags |= MHEAP_FLAG_THREAD_SAFE;
2199 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
2201 clib_time_init (&vcm->clib_time);
2202 vppcom_init_error_string_table ();
2203 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
2204 20 /* timeout in secs */ );
2205 clib_spinlock_init (&vcm->sessions_lockp);
2208 if (vcm->my_client_index == ~0)
2211 /* API hookup and connect to VPP */
2212 vppcom_api_hookup ();
2213 vcm->app_state = STATE_APP_START;
2214 rv = vppcom_connect_to_vpp (app_name);
2217 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
2222 /* State event handling thread */
2224 rv = vce_start_event_thread (&(vcm->event_thread), 20);
2226 if (VPPCOM_DEBUG > 0)
2227 clib_warning ("VCL<%d>: sending session enable", getpid ());
2229 rv = vppcom_app_session_enable ();
2232 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
2233 "failed!", getpid ());
2237 if (VPPCOM_DEBUG > 0)
2238 clib_warning ("VCL<%d>: sending app attach", getpid ());
2240 rv = vppcom_app_attach ();
2243 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
2248 if (VPPCOM_DEBUG > 0)
2249 clib_warning ("VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
2250 getpid (), app_name, vcm->my_client_index,
2251 vcm->my_client_index);
2258 vppcom_app_destroy (void)
2261 f64 orig_app_timeout;
2263 if (vcm->my_client_index == ~0)
2266 if (VPPCOM_DEBUG > 0)
2267 clib_warning ("VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
2268 getpid (), vcm->my_client_index, vcm->my_client_index);
2270 if (VPPCOM_DEBUG > 0)
2273 ELOG_TYPE_DECLARE (e) =
2275 .format = "app_detach:C:%d",
2276 .format_args = "i4",
2283 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
2284 ed->data = vcm->my_client_index;
2288 vppcom_app_detach ();
2289 orig_app_timeout = vcm->cfg.app_timeout;
2290 vcm->cfg.app_timeout = 2.0;
2291 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
2292 vcm->cfg.app_timeout = orig_app_timeout;
2293 if (PREDICT_FALSE (rv))
2295 if (VPPCOM_DEBUG > 0)
2296 clib_warning ("VCL<%d>: application detach timed out! "
2297 "returning %d (%s)",
2298 getpid (), rv, vppcom_retval_str (rv));
2301 /* Finished with logging before client gets reset to ~0 */
2302 if (VPPCOM_DEBUG > 0)
2305 vl_client_disconnect_from_vlib ();
2306 vcm->my_client_index = ~0;
2307 vcm->app_state = STATE_APP_START;
2311 vppcom_session_create (u8 proto, u8 is_nonblocking)
2315 session_state_t state;
2316 elog_track_t session_elog_track;
2318 VCL_SESSION_LOCK ();
2319 pool_get (vcm->sessions, session);
2320 memset (session, 0, sizeof (*session));
2321 session_index = session - vcm->sessions;
2323 session->proto = proto;
2324 session->state = STATE_START;
2325 state = session->state;
2326 session->vpp_handle = ~0;
2329 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2331 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2333 if (VPPCOM_DEBUG > 0)
2335 session->elog_track.name =
2336 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2338 elog_track_register (&vcm->elog_main, &session->elog_track);
2339 session_elog_track = session->elog_track;
2342 VCL_SESSION_UNLOCK ();
2344 if (VPPCOM_DEBUG > 0)
2345 clib_warning ("VCL<%d>: sid %u", getpid (), session_index);
2347 if (VPPCOM_DEBUG > 0)
2350 ELOG_TYPE_DECLARE (e) =
2352 .format = "session_create:proto:%d state:%d is_nonblocking:%d",
2353 .format_args = "i4i4i4",
2361 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
2362 ed->data[0] = proto;
2363 ed->data[1] = state;
2364 ed->data[2] = is_nonblocking;
2368 return (int) session_index;
2372 vppcom_session_close (uint32_t session_index)
2374 session_t *session = 0;
2382 session_state_t state;
2383 elog_track_t session_elog_track;
2385 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2386 is_vep = session->is_vep;
2387 is_vep_session = session->is_vep_session;
2388 next_sid = session->vep.next_sid;
2389 vep_idx = session->vep.vep_idx;
2390 state = session->state;
2391 vpp_handle = session->vpp_handle;
2392 VCL_SESSION_UNLOCK ();
2395 * Why two if(VPPCOM_DEBUG) checks?
2397 * Eventually all clib_warnings need their own way of being
2398 * logged and signalled (like severity) where event logging
2399 * is a separate debugging tool. It will make the separation
2400 * easier. ... parting is such sweet sorrow ...
2402 if (VPPCOM_DEBUG > 0)
2404 session_elog_track = session->elog_track;
2407 if (VPPCOM_DEBUG > 0)
2410 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
2411 "closing epoll session...",
2412 getpid (), session_index, session_index);
2414 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
2415 "closing session...",
2416 getpid (), vpp_handle, session_index);
2421 while (next_sid != ~0)
2423 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
2424 if ((VPPCOM_DEBUG > 0) && PREDICT_FALSE (rv < 0))
2425 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2426 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2427 getpid (), vpp_handle, next_sid, vep_idx,
2428 rv, vppcom_retval_str (rv));
2430 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2431 next_sid = session->vep.next_sid;
2432 VCL_SESSION_UNLOCK ();
2439 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2440 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2441 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2442 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2443 getpid (), vpp_handle, session_index,
2444 vep_idx, rv, vppcom_retval_str (rv));
2447 if (state & STATE_LISTEN)
2449 rv = vppcom_session_unbind (session_index);
2450 if (PREDICT_FALSE (rv < 0))
2452 if (VPPCOM_DEBUG > 0)
2453 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2454 "listener unbind failed! rv %d (%s)",
2455 getpid (), vpp_handle, session_index,
2456 rv, vppcom_retval_str (rv));
2460 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
2462 rv = vppcom_session_disconnect (session_index);
2463 if (PREDICT_FALSE (rv < 0))
2464 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2465 "session disconnect failed! rv %d (%s)",
2466 getpid (), vpp_handle, session_index,
2467 rv, vppcom_retval_str (rv));
2471 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2472 vpp_handle = session->vpp_handle;
2473 if (vpp_handle != ~0)
2475 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
2477 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
2479 pool_put_index (vcm->sessions, session_index);
2481 VCL_SESSION_UNLOCK ();
2483 if (VPPCOM_DEBUG > 0)
2486 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
2487 getpid (), session_index, session_index);
2489 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
2490 getpid (), vpp_handle, session_index);
2494 if (VPPCOM_DEBUG > 0)
2497 ELOG_TYPE_DECLARE (e) =
2499 .format = "session_close:rv:%d",
2500 .format_args = "i4",
2508 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
2517 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2519 session_t *session = 0;
2523 return VPPCOM_EINVAL;
2525 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2527 if (session->is_vep)
2529 VCL_SESSION_UNLOCK ();
2530 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2531 "bind to an epoll session!", getpid (), session_index);
2536 session->lcl_addr.is_ip4 = ep->is_ip4;
2537 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
2538 session->lcl_port = ep->port;
2540 if (VPPCOM_DEBUG > 0)
2541 clib_warning ("VCL<%d>: sid %u: binding to local %s address %U "
2542 "port %u, proto %s", getpid (), session_index,
2543 session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2544 format_ip46_address, &session->lcl_addr.ip46,
2545 session->lcl_addr.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2546 clib_net_to_host_u16 (session->lcl_port),
2547 session->proto ? "UDP" : "TCP");
2549 if (VPPCOM_DEBUG > 0)
2551 if (session->lcl_addr.is_ip4)
2554 ELOG_TYPE_DECLARE (e) =
2556 .format = "bind local:%s:%d.%d.%d.%d:%d ",
2557 .format_args = "t1i1i1i1i1i2",
2558 .n_enum_strings = 2,
2559 .enum_strings = {"TCP", "UDP",},
2562 CLIB_PACKED (struct {
2568 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2569 ed->proto = session->proto;
2570 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
2571 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
2572 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
2573 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
2574 ed->port = clib_net_to_host_u16 (session->lcl_port);
2579 VCL_SESSION_UNLOCK ();
2585 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2587 session_t *listen_session = 0;
2588 u64 listen_vpp_handle;
2591 if (q_len == 0 || q_len == ~0)
2592 q_len = vcm->cfg.listen_queue_size;
2594 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
2596 if (listen_session->is_vep)
2598 VCL_SESSION_UNLOCK ();
2599 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
2600 "epoll session!", getpid (), listen_session_index);
2605 listen_vpp_handle = listen_session->vpp_handle;
2606 if (listen_session->state & STATE_LISTEN)
2608 VCL_SESSION_UNLOCK ();
2609 if (VPPCOM_DEBUG > 0)
2610 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2611 "already in listen state!",
2612 getpid (), listen_vpp_handle, listen_session_index);
2617 if (VPPCOM_DEBUG > 0)
2618 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
2619 "sid %u: sending VPP bind+listen request...",
2620 getpid (), listen_vpp_handle, listen_session_index);
2622 vppcom_send_bind_sock (listen_session, listen_session_index);
2623 VCL_SESSION_UNLOCK ();
2625 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2626 vcm->cfg.session_timeout);
2628 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
2629 if (PREDICT_FALSE (retval))
2631 if (VPPCOM_DEBUG > 0)
2633 ("VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
2634 "returning %d (%s)", getpid (), listen_session->vpp_handle,
2635 listen_session_index, retval, vppcom_retval_str (retval));
2636 VCL_SESSION_UNLOCK ();
2641 VCL_ACCEPT_FIFO_LOCK ();
2642 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2643 VCL_ACCEPT_FIFO_UNLOCK ();
2645 VCL_SESSION_UNLOCK ();
2652 validate_args_session_accept_ (session_t * listen_session)
2654 u32 listen_session_index = listen_session - vcm->sessions;
2656 /* Input validation - expects spinlock on sessions_lockp */
2657 if (listen_session->is_vep)
2659 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
2660 "epoll session!", getpid (), listen_session_index);
2661 return VPPCOM_EBADFD;
2664 if (listen_session->state != STATE_LISTEN)
2666 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2667 "not in listen state! state 0x%x (%s)", getpid (),
2668 listen_session->vpp_handle, listen_session_index,
2669 listen_session->state,
2670 vppcom_session_state_str (listen_session->state));
2671 return VPPCOM_EBADFD;
2677 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2680 session_t *listen_session = 0;
2681 session_t *client_session = 0;
2682 u32 client_session_index = ~0;
2684 u64 listen_vpp_handle;
2685 vce_event_handler_reg_t *reg;
2687 vce_event_connect_request_t *result;
2690 int millisecond_timeout = 1;
2691 int hours_timeout = 20 * 60 * 60;
2693 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
2694 listen_vpp_handle = listen_session->vpp_handle; // For debugging
2696 rv = validate_args_session_accept_ (listen_session);
2699 VCL_SESSION_UNLOCK ();
2703 /* Using an aggressive timer of 1ms and a generous timer of
2704 * 20 hours, we can implement a blocking and non-blocking listener
2705 * as both event and time driven */
2706 gettimeofday (&tv, NULL);
2707 ts.tv_nsec = (tv.tv_usec * 1000) + (1000 * millisecond_timeout);
2708 ts.tv_sec = tv.tv_sec;
2710 /* Predict that the Listener is blocking more often than not */
2711 if (PREDICT_TRUE (!VCL_SESS_ATTR_TEST (listen_session->attr,
2712 VCL_SESS_ATTR_NONBLOCK)))
2713 ts.tv_sec += hours_timeout;
2715 VCL_SESSION_UNLOCK ();
2717 /* Register handler for connect_request event on listen_session_index */
2718 vce_event_key_t evk;
2719 evk.session_index = listen_session_index;
2720 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
2721 reg = vce_register_handler (&vcm->event_thread, &evk,
2722 vce_connect_request_handler_fn, 0);
2724 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
2725 pthread_mutex_lock (®->handler_lock);
2728 VCL_EVENTS_UNLOCK ();
2729 rv = pthread_cond_timedwait (®->handler_cond,
2730 ®->handler_lock, &ts);
2731 if (rv == ETIMEDOUT)
2737 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
2739 result = vce_get_event_data (ev, sizeof (*result));
2740 client_session_index = result->accepted_session_index;
2741 VCL_EVENTS_UNLOCK ();
2743 /* Remove from the FIFO used to service epoll */
2744 VCL_ACCEPT_FIFO_LOCK ();
2745 if (clib_fifo_elts (vcm->client_session_index_fifo))
2747 u32 tmp_client_session_index;
2748 clib_fifo_sub1 (vcm->client_session_index_fifo,
2749 tmp_client_session_index);
2750 /* It wasn't ours... put it back ... */
2751 if (tmp_client_session_index != client_session_index)
2752 clib_fifo_add1 (vcm->client_session_index_fifo,
2753 tmp_client_session_index);
2755 VCL_ACCEPT_FIFO_UNLOCK ();
2757 VCL_SESSION_LOCK ();
2759 rv = vppcom_session_at_index (client_session_index, &client_session);
2760 if (PREDICT_FALSE (rv))
2762 rv = VPPCOM_ECONNABORTED;
2763 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
2764 "lookup failed! returning %d (%s)", getpid (),
2765 listen_vpp_handle, listen_session_index,
2766 client_session_index, rv, vppcom_retval_str (rv));
2770 if (flags & O_NONBLOCK)
2771 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2773 VCL_SESS_ATTR_CLR (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2775 if (VPPCOM_DEBUG > 0)
2776 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
2777 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
2778 getpid (), listen_vpp_handle, listen_session_index,
2779 client_session->vpp_handle, client_session_index,
2780 flags, VCL_SESS_ATTR_TEST (client_session->attr,
2781 VCL_SESS_ATTR_NONBLOCK));
2785 ep->is_ip4 = client_session->peer_addr.is_ip4;
2786 ep->port = client_session->peer_port;
2787 if (client_session->peer_addr.is_ip4)
2788 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2789 sizeof (ip4_address_t));
2791 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2792 sizeof (ip6_address_t));
2795 vppcom_send_accept_session_reply (client_session->vpp_handle,
2796 client_session->client_context,
2797 0 /* retval OK */ );
2799 if (VPPCOM_DEBUG > 0)
2800 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle "
2801 "0x%llx, sid %u connection to local %s address "
2802 "%U port %u", getpid (), listen_vpp_handle,
2803 listen_session_index, client_session->vpp_handle,
2804 client_session_index,
2805 client_session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2806 format_ip46_address, &client_session->lcl_addr.ip46,
2807 client_session->lcl_addr.is_ip4 ?
2808 IP46_TYPE_IP4 : IP46_TYPE_IP6,
2809 clib_net_to_host_u16 (client_session->lcl_port));
2811 if (VPPCOM_DEBUG > 0)
2813 client_session->elog_track.name =
2814 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2815 client_session_index, 0);
2816 elog_track_register (&vcm->elog_main, &client_session->elog_track);
2818 // Two elog entries due to 20-byte per entry constraint.
2820 ELOG_TYPE_DECLARE (e) =
2822 .format = "accept: listen_handle:%x from_handle:%x",
2823 .format_args = "i8i8",
2831 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, client_session->elog_track);
2832 ed->handle[0] = listen_vpp_handle;
2833 ed->handle[1] = client_session->vpp_handle;
2836 if (client_session->lcl_addr.is_ip4)
2839 ELOG_TYPE_DECLARE (e2) =
2841 .format = "accept: S:%d %d.%d.%d.%d:%d ",
2842 .format_args = "i4i1i1i1i1i2",
2845 CLIB_PACKED (struct {
2852 ELOG_TRACK_DATA (&vcm->elog_main, e2, client_session->elog_track);
2853 ed2->session = client_session_index;
2854 ed2->addr[0] = client_session->lcl_addr.ip46.ip4.as_u8[0];
2855 ed2->addr[1] = client_session->lcl_addr.ip46.ip4.as_u8[1];
2856 ed2->addr[2] = client_session->lcl_addr.ip46.ip4.as_u8[2];
2857 ed2->addr[3] = client_session->lcl_addr.ip46.ip4.as_u8[3];
2858 ed2->port = clib_net_to_host_u16 (client_session->lcl_port);
2863 VCL_SESSION_UNLOCK ();
2865 rv = (int) client_session_index;
2866 vce_clear_event (&vcm->event_thread, reg->ev_idx);
2867 if (vcm->session_io_thread.io_sessions_lockp)
2869 /* Throw this new accepted session index into the rx poll thread pool */
2870 VCL_IO_SESSIONS_LOCK ();
2871 u32 *active_session_index;
2872 pool_get (vcm->session_io_thread.active_session_indexes,
2873 active_session_index);
2874 *active_session_index = client_session_index;
2875 VCL_IO_SESSIONS_UNLOCK ();
2878 vce_unregister_handler (&vcm->event_thread, reg);
2879 pthread_mutex_unlock (®->handler_lock);
2886 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2888 session_t *session = 0;
2890 int rv, retval = VPPCOM_OK;
2892 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2894 if (PREDICT_FALSE (session->is_vep))
2896 VCL_SESSION_UNLOCK ();
2897 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2898 "connect on an epoll session!", getpid (), session_index);
2903 if (PREDICT_FALSE (session->state & CLIENT_STATE_OPEN))
2905 if (VPPCOM_DEBUG > 0)
2906 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session already "
2907 "connected to %s %U port %d proto %s, state 0x%x (%s)",
2908 getpid (), session->vpp_handle, session_index,
2909 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2910 format_ip46_address,
2911 &session->peer_addr.ip46, session->peer_addr.is_ip4 ?
2912 IP46_TYPE_IP4 : IP46_TYPE_IP6,
2913 clib_net_to_host_u16 (session->peer_port),
2914 session->proto ? "UDP" : "TCP", session->state,
2915 vppcom_session_state_str (session->state));
2917 VCL_SESSION_UNLOCK ();
2921 session->peer_addr.is_ip4 = server_ep->is_ip4;
2922 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
2923 session->peer_port = server_ep->port;
2925 if (VPPCOM_DEBUG > 0)
2926 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server "
2927 "%s %U port %d proto %s",
2928 getpid (), session->vpp_handle, session_index,
2929 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2930 format_ip46_address,
2931 &session->peer_addr.ip46, session->peer_addr.is_ip4 ?
2932 IP46_TYPE_IP4 : IP46_TYPE_IP6,
2933 clib_net_to_host_u16 (session->peer_port),
2934 session->proto ? "UDP" : "TCP");
2936 vppcom_send_connect_sock (session, session_index);
2937 VCL_SESSION_UNLOCK ();
2940 vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2941 vcm->cfg.session_timeout);
2943 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2944 vpp_handle = session->vpp_handle;
2945 VCL_SESSION_UNLOCK ();
2948 if (PREDICT_FALSE (retval))
2951 if (VPPCOM_DEBUG > 0)
2955 ("VCL<%d>: vpp handle 0x%llx, sid %u: connect failed! "
2956 "returning %d (%s)", getpid (), vpp_handle,
2957 session_index, rv, vppcom_retval_str (rv));
2959 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
2960 "returning %d (%s)", getpid (),
2961 session_index, rv, vppcom_retval_str (rv));
2964 else if (VPPCOM_DEBUG > 0)
2965 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
2966 getpid (), vpp_handle, session_index);
2972 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2975 session_t *session = 0;
2976 svm_fifo_t *rx_fifo;
2983 session_state_t state;
2987 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2989 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
2990 rx_fifo = session->rx_fifo;
2991 state = session->state;
2992 vpp_handle = session->vpp_handle;
2994 if (PREDICT_FALSE (session->is_vep))
2996 VCL_SESSION_UNLOCK ();
2997 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2998 "read from an epoll session!", getpid (), session_index);
3003 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
3005 VCL_SESSION_UNLOCK ();
3006 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3008 if (VPPCOM_DEBUG > 0)
3009 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s session is "
3010 "not open! state 0x%x (%s), returning %d (%s)",
3011 getpid (), vpp_handle, session_index, state,
3012 vppcom_session_state_str (state),
3013 rv, vppcom_retval_str (rv));
3017 VCL_SESSION_UNLOCK ();
3022 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
3024 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
3026 while (!is_nonblocking && (n_read <= 0));
3030 VCL_SESSION_LOCK_AND_GET (session_index, &session);
3032 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
3033 (EPOLLET | EPOLLIN));
3035 session->vep.et_mask |= EPOLLIN;
3037 if (state & STATE_CLOSE_ON_EMPTY)
3039 rv = VPPCOM_ECONNRESET;
3041 if (VPPCOM_DEBUG > 1)
3043 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo "
3044 "with session state 0x%x (%s)!"
3045 " Setting state to 0x%x (%s), returning %d (%s)",
3046 getpid (), session->vpp_handle, session_index,
3047 state, vppcom_session_state_str (state),
3049 vppcom_session_state_str (STATE_DISCONNECT), rv,
3050 vppcom_retval_str (rv));
3053 session->state = STATE_DISCONNECT;
3058 VCL_SESSION_UNLOCK ();
3063 if (VPPCOM_DEBUG > 2)
3066 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
3067 "from (%p)", getpid (), vpp_handle,
3068 session_index, n_read, rx_fifo);
3070 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
3071 "returning %d (%s)", getpid (), vpp_handle,
3072 session_index, rv, vppcom_retval_str (rv));
3079 vppcom_session_read (uint32_t session_index, void *buf, size_t n)
3081 return (vppcom_session_read_internal (session_index, buf, n, 0));
3085 vppcom_session_peek (uint32_t session_index, void *buf, int n)
3087 return (vppcom_session_read_internal (session_index, buf, n, 1));
3091 vppcom_session_read_ready (session_t * session, u32 session_index)
3096 session_state_t state = session->state;
3097 u64 vpp_handle = session->vpp_handle;
3099 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3100 if (PREDICT_FALSE (session->is_vep))
3102 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
3103 "epoll session!", getpid (), session_index);
3108 if (session->state & STATE_LISTEN)
3110 VCL_ACCEPT_FIFO_LOCK ();
3111 ready = clib_fifo_elts (vcm->client_session_index_fifo);
3112 VCL_ACCEPT_FIFO_UNLOCK ();
3116 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
3118 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3121 if (VPPCOM_DEBUG > 1)
3122 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session is "
3123 "not open! state 0x%x (%s), returning %d (%s)",
3124 getpid (), vpp_handle, session_index,
3125 state, vppcom_session_state_str (state),
3126 rv, vppcom_retval_str (rv));
3130 ready = svm_fifo_max_dequeue (session->rx_fifo);
3136 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
3138 session->vep.et_mask |= EPOLLIN;
3140 if (state & STATE_CLOSE_ON_EMPTY)
3142 rv = VPPCOM_ECONNRESET;
3144 if (VPPCOM_DEBUG > 1)
3146 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
3147 "sid %u: Empty fifo with"
3148 " session state 0x%x (%s)! Setting state to "
3149 "0x%x (%s), returning %d (%s)",
3150 getpid (), session_index, vpp_handle,
3151 state, vppcom_session_state_str (state),
3153 vppcom_session_state_str (STATE_DISCONNECT), rv,
3154 vppcom_retval_str (rv));
3156 session->state = STATE_DISCONNECT;
3162 if (vcm->app_event_queue->cursize &&
3163 !pthread_mutex_trylock (&vcm->app_event_queue->mutex))
3165 u32 i, n_to_dequeue = vcm->app_event_queue->cursize;
3166 session_fifo_event_t e;
3168 for (i = 0; i < n_to_dequeue; i++)
3169 svm_queue_sub_raw (vcm->app_event_queue, (u8 *) & e);
3171 pthread_mutex_unlock (&vcm->app_event_queue->mutex);
3178 vppcom_session_write (uint32_t session_index, void *buf, size_t n)
3180 session_t *session = 0;
3181 svm_fifo_t *tx_fifo = 0;
3183 session_fifo_event_t evt;
3184 session_state_t state;
3185 int rv, n_write, is_nonblocking;
3191 VCL_SESSION_LOCK_AND_GET (session_index, &session);
3193 tx_fifo = session->tx_fifo;
3194 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
3195 vpp_handle = session->vpp_handle;
3196 state = session->state;
3198 if (PREDICT_FALSE (session->is_vep))
3200 VCL_SESSION_UNLOCK ();
3201 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3202 "cannot write to an epoll session!",
3203 getpid (), vpp_handle, session_index);
3209 if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3212 ((session->state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3215 VCL_SESSION_UNLOCK ();
3216 if (VPPCOM_DEBUG > 1)
3217 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3218 "session is not open! state 0x%x (%s)",
3219 getpid (), vpp_handle, session_index,
3220 state, vppcom_session_state_str (state));
3224 VCL_SESSION_UNLOCK ();
3228 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, (void *) buf);
3230 while (!is_nonblocking && (n_write <= 0));
3232 /* If event wasn't set, add one
3234 * To reduce context switching, can check if an
3235 * event is already there for this event_key, but for now
3236 * this will suffice. */
3238 if ((n_write > 0) && svm_fifo_set_event (tx_fifo))
3240 /* Fabricate TX event, send to vpp */
3242 evt.event_type = FIFO_EVENT_APP_TX;
3244 VCL_SESSION_LOCK_AND_GET (session_index, &session);
3245 q = session->vpp_event_queue;
3247 svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
3248 VCL_SESSION_UNLOCK ();
3249 if (VPPCOM_DEBUG > 1)
3250 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3251 "added FIFO_EVENT_APP_TX to "
3252 "vpp_event_q %p, n_write %d", getpid (),
3253 vpp_handle, session_index, q, n_write);
3258 VCL_SESSION_LOCK_AND_GET (session_index, &session);
3260 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3261 (EPOLLET | EPOLLOUT));
3263 session->vep.et_mask |= EPOLLOUT;
3265 if (session->state & STATE_CLOSE_ON_EMPTY)
3267 rv = VPPCOM_ECONNRESET;
3269 if (VPPCOM_DEBUG > 1)
3271 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3272 "Empty fifo with session state 0x%x (%s)!"
3273 " Setting state to 0x%x (%s), returning %d (%s)",
3274 getpid (), session->vpp_handle, session_index,
3276 vppcom_session_state_str (session->state),
3278 vppcom_session_state_str (STATE_DISCONNECT), rv,
3279 vppcom_retval_str (rv));
3282 session->state = STATE_DISCONNECT;
3287 VCL_SESSION_UNLOCK ();
3292 if (VPPCOM_DEBUG > 2)
3295 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3296 "FIFO-FULL (%p)", getpid (), vpp_handle,
3297 session_index, tx_fifo);
3299 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3300 "wrote %d bytes tx-fifo: (%p)", getpid (),
3301 vpp_handle, session_index, n_write, tx_fifo);
3308 vppcom_session_write_ready (session_t * session, u32 session_index)
3316 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3317 if (PREDICT_FALSE (session->is_vep))
3319 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3320 "cannot write to an epoll session!",
3321 getpid (), session->vpp_handle, session_index);
3326 if (PREDICT_FALSE (session->state & STATE_LISTEN))
3328 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3329 "cannot write to a listen session!",
3330 getpid (), session->vpp_handle, session_index);
3335 if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3337 session_state_t state = session->state;
3339 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3341 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3342 "session is not open! state 0x%x (%s), "
3343 "returning %d (%s)", getpid (), session->vpp_handle,
3345 state, vppcom_session_state_str (state),
3346 rv, vppcom_retval_str (rv));
3350 ready = svm_fifo_max_enqueue (session->tx_fifo);
3352 if (VPPCOM_DEBUG > 3)
3353 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3354 "peek %s (%p), ready = %d", getpid (),
3355 session->vpp_handle, session_index,
3356 session->tx_fifo, ready);
3360 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3361 (EPOLLET | EPOLLOUT));
3363 session->vep.et_mask |= EPOLLOUT;
3365 if (session->state & STATE_CLOSE_ON_EMPTY)
3367 rv = VPPCOM_ECONNRESET;
3369 if (VPPCOM_DEBUG > 1)
3371 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3372 "Empty fifo with session "
3373 "state 0x%x (%s)! Setting state to 0x%x (%s), "
3374 "returning %d (%s)", getpid (),
3375 session->vpp_handle, session_index,
3377 vppcom_session_state_str (session->state),
3379 vppcom_session_state_str (STATE_DISCONNECT), rv,
3380 vppcom_retval_str (rv));
3382 session->state = STATE_DISCONNECT;
3392 vppcom_select (unsigned long n_bits, unsigned long *read_map,
3393 unsigned long *write_map, unsigned long *except_map,
3394 double time_to_wait)
3397 session_t *session = 0;
3398 int rv, bits_set = 0;
3399 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
3400 u32 minbits = clib_max (n_bits, BITS (uword));
3402 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
3404 if (n_bits && read_map)
3406 clib_bitmap_validate (vcm->rd_bitmap, minbits);
3407 clib_memcpy (vcm->rd_bitmap, read_map,
3408 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3409 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3411 if (n_bits && write_map)
3413 clib_bitmap_validate (vcm->wr_bitmap, minbits);
3414 clib_memcpy (vcm->wr_bitmap, write_map,
3415 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3416 memset (write_map, 0,
3417 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3419 if (n_bits && except_map)
3421 clib_bitmap_validate (vcm->ex_bitmap, minbits);
3422 clib_memcpy (vcm->ex_bitmap, except_map,
3423 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3424 memset (except_map, 0,
3425 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3435 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
3438 rv = vppcom_session_at_index (session_index, &session);
3441 VCL_SESSION_UNLOCK();
3442 if (VPPCOM_DEBUG > 1)
3443 clib_warning ("VCL<%d>: session %d specified in "
3444 "read_map is closed.", getpid (),
3446 bits_set = VPPCOM_EBADFD;
3449 if (session->state & STATE_LISTEN)
3451 vce_event_handler_reg_t *reg = 0;
3452 vce_event_key_t evk;
3454 /* Check if handler already registered for this
3456 * If not, register handler for connect_request event
3457 * on listen_session_index
3459 evk.session_index = session_index;
3460 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
3461 reg = vce_get_event_handler (&vcm->event_thread, &evk);
3463 reg = vce_register_handler (&vcm->event_thread, &evk,
3464 vce_poll_wait_connect_request_handler_fn,
3465 0 /* No callback args */);
3466 rv = vppcom_session_read_ready (session, session_index);
3469 vce_unregister_handler (&vcm->event_thread, reg);
3473 rv = vppcom_session_read_ready (session, session_index);
3474 VCL_SESSION_UNLOCK();
3475 if (except_map && vcm->ex_bitmap &&
3476 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3479 clib_bitmap_set_no_check (except_map, session_index, 1);
3484 clib_bitmap_set_no_check (read_map, session_index, 1);
3492 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3495 rv = vppcom_session_at_index (session_index, &session);
3498 VCL_SESSION_UNLOCK();
3499 if (VPPCOM_DEBUG > 0)
3500 clib_warning ("VCL<%d>: session %d specified in "
3501 "write_map is closed.", getpid (),
3503 bits_set = VPPCOM_EBADFD;
3507 rv = vppcom_session_write_ready (session, session_index);
3508 VCL_SESSION_UNLOCK();
3509 if (write_map && (rv > 0))
3511 clib_bitmap_set_no_check (write_map, session_index, 1);
3519 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3522 rv = vppcom_session_at_index (session_index, &session);
3525 VCL_SESSION_UNLOCK();
3526 if (VPPCOM_DEBUG > 1)
3527 clib_warning ("VCL<%d>: session %d specified in "
3528 "except_map is closed.", getpid (),
3530 bits_set = VPPCOM_EBADFD;
3534 rv = vppcom_session_read_ready (session, session_index);
3535 VCL_SESSION_UNLOCK();
3538 clib_bitmap_set_no_check (except_map, session_index, 1);
3546 while ((time_to_wait == -1) || (clib_time_now (&vcm->clib_time) < timeout));
3553 vep_verify_epoll_chain (u32 vep_idx)
3556 vppcom_epoll_t *vep;
3560 if (VPPCOM_DEBUG <= 1)
3563 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3564 rv = vppcom_session_at_index (vep_idx, &session);
3565 if (PREDICT_FALSE (rv))
3567 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
3568 getpid (), vep_idx);
3571 if (PREDICT_FALSE (!session->is_vep))
3573 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3574 getpid (), vep_idx);
3577 vep = &session->vep;
3578 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
3581 " is_vep_session = %u\n"
3582 " next_sid = 0x%x (%u)\n"
3583 " wait_cont_idx = 0x%x (%u)\n"
3584 "}\n", getpid (), vep_idx,
3585 session->is_vep, session->is_vep_session,
3586 vep->next_sid, vep->next_sid,
3587 session->wait_cont_idx, session->wait_cont_idx);
3589 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
3591 rv = vppcom_session_at_index (sid, &session);
3592 if (PREDICT_FALSE (rv))
3594 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
3597 if (PREDICT_FALSE (session->is_vep))
3598 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
3599 getpid (), vep_idx);
3600 else if (PREDICT_FALSE (!session->is_vep_session))
3602 clib_warning ("VCL<%d>: ERROR: session (%u) "
3603 "is not a vep session!", getpid (), sid);
3606 vep = &session->vep;
3607 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
3608 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
3609 "vep_idx (%u)!", getpid (),
3610 sid, session->vep.vep_idx, vep_idx);
3611 if (session->is_vep_session)
3613 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3615 " next_sid = 0x%x (%u)\n"
3616 " prev_sid = 0x%x (%u)\n"
3617 " vep_idx = 0x%x (%u)\n"
3618 " ev.events = 0x%x\n"
3619 " ev.data.u64 = 0x%llx\n"
3623 vep->next_sid, vep->next_sid,
3624 vep->prev_sid, vep->prev_sid,
3625 vep->vep_idx, vep->vep_idx,
3626 vep->ev.events, vep->ev.data.u64, vep->et_mask);
3631 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
3632 getpid (), vep_idx);
3636 vppcom_epoll_create (void)
3638 session_t *vep_session;
3640 elog_track_t vep_elog_track;
3642 VCL_SESSION_LOCK ();
3643 pool_get (vcm->sessions, vep_session);
3644 memset (vep_session, 0, sizeof (*vep_session));
3645 vep_idx = vep_session - vcm->sessions;
3647 vep_session->is_vep = 1;
3648 vep_session->vep.vep_idx = ~0;
3649 vep_session->vep.next_sid = ~0;
3650 vep_session->vep.prev_sid = ~0;
3651 vep_session->wait_cont_idx = ~0;
3652 vep_session->vpp_handle = ~0;
3653 vep_session->poll_reg = 0;
3655 if (VPPCOM_DEBUG > 0)
3657 vep_session->elog_track.name =
3658 (char *) format (0, "C:%d:VEP:%d%c", vcm->my_client_index,
3660 elog_track_register (&vcm->elog_main, &vep_session->elog_track);
3661 vep_elog_track = vep_session->elog_track;
3664 VCL_SESSION_UNLOCK ();
3666 if (VPPCOM_DEBUG > 0)
3667 clib_warning ("VCL<%d>: Created vep_idx %u / sid %u!",
3668 getpid (), vep_idx, vep_idx);
3670 if (VPPCOM_DEBUG > 0)
3674 ELOG_TYPE_DECLARE (e) =
3676 .format = "created epoll session:%d",
3677 .format_args = "i4",
3685 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_elog_track);
3694 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3695 struct epoll_event *event)
3697 session_t *vep_session;
3701 if (vep_idx == session_index)
3703 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
3704 getpid (), vep_idx);
3705 return VPPCOM_EINVAL;
3708 VCL_SESSION_LOCK ();
3709 rv = vppcom_session_at_index (vep_idx, &vep_session);
3710 if (PREDICT_FALSE (rv))
3712 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
3715 if (PREDICT_FALSE (!vep_session->is_vep))
3717 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3718 getpid (), vep_idx);
3723 ASSERT (vep_session->vep.vep_idx == ~0);
3724 ASSERT (vep_session->vep.prev_sid == ~0);
3726 rv = vppcom_session_at_index (session_index, &session);
3727 if (PREDICT_FALSE (rv))
3729 if (VPPCOM_DEBUG > 0)
3730 clib_warning ("VCL<%d>: ERROR: Invalid session_index (%u)!",
3731 getpid (), session_index);
3734 if (PREDICT_FALSE (session->is_vep))
3736 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3744 if (PREDICT_FALSE (!event))
3746 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
3747 "epoll_event structure!", getpid ());
3751 if (vep_session->vep.next_sid != ~0)
3753 session_t *next_session;
3754 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3756 if (PREDICT_FALSE (rv))
3758 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
3759 "vep.next_sid (%u) on vep_idx (%u)!",
3760 getpid (), vep_session->vep.next_sid, vep_idx);
3763 ASSERT (next_session->vep.prev_sid == vep_idx);
3764 next_session->vep.prev_sid = session_index;
3766 session->vep.next_sid = vep_session->vep.next_sid;
3767 session->vep.prev_sid = vep_idx;
3768 session->vep.vep_idx = vep_idx;
3769 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3770 session->vep.ev = *event;
3771 session->is_vep = 0;
3772 session->is_vep_session = 1;
3773 vep_session->vep.next_sid = session_index;
3775 /* VCL Event Register handler */
3776 if (session->state & STATE_LISTEN)
3778 /* Register handler for connect_request event on listen_session_index */
3779 vce_event_key_t evk;
3780 evk.session_index = session_index;
3781 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
3782 vep_session->poll_reg =
3783 vce_register_handler (&vcm->event_thread, &evk,
3784 vce_poll_wait_connect_request_handler_fn,
3785 0 /* No callback args */ );
3787 if (VPPCOM_DEBUG > 1)
3788 clib_warning ("VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
3789 "sid %u, events 0x%x, data 0x%llx!",
3790 getpid (), vep_idx, session_index,
3791 event->events, event->data.u64);
3792 if (VPPCOM_DEBUG > 0)
3795 ELOG_TYPE_DECLARE (e) =
3797 .format = "epoll_ctladd: events:%x data:%x",
3798 .format_args = "i4i4i8",
3806 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3808 ed->events = event->events;
3809 ed->event_data = event->data.u64;
3815 if (PREDICT_FALSE (!event))
3817 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
3818 "epoll_event structure!", getpid ());
3822 else if (PREDICT_FALSE (!session->is_vep_session))
3824 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3825 "not a vep session!", getpid (), session_index);
3829 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3831 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3832 "vep_idx (%u) != vep_idx (%u)!",
3833 getpid (), session_index,
3834 session->vep.vep_idx, vep_idx);
3838 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3839 session->vep.ev = *event;
3840 if (VPPCOM_DEBUG > 1)
3842 ("VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3843 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
3848 if (PREDICT_FALSE (!session->is_vep_session))
3850 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3851 "not a vep session!", getpid (), session_index);
3855 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3857 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3858 "vep_idx (%u) != vep_idx (%u)!",
3859 getpid (), session_index,
3860 session->vep.vep_idx, vep_idx);
3865 /* VCL Event Un-register handler */
3866 if ((session->state & STATE_LISTEN) && vep_session->poll_reg)
3868 (void) vce_unregister_handler (&vcm->event_thread,
3869 vep_session->poll_reg);
3872 vep_session->wait_cont_idx =
3873 (vep_session->wait_cont_idx == session_index) ?
3874 session->vep.next_sid : vep_session->wait_cont_idx;
3876 if (session->vep.prev_sid == vep_idx)
3877 vep_session->vep.next_sid = session->vep.next_sid;
3880 session_t *prev_session;
3881 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3882 if (PREDICT_FALSE (rv))
3884 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
3885 "vep.prev_sid (%u) on sid (%u)!",
3886 getpid (), session->vep.prev_sid, session_index);
3889 ASSERT (prev_session->vep.next_sid == session_index);
3890 prev_session->vep.next_sid = session->vep.next_sid;
3892 if (session->vep.next_sid != ~0)
3894 session_t *next_session;
3895 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3896 if (PREDICT_FALSE (rv))
3898 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
3899 "vep.next_sid (%u) on sid (%u)!",
3900 getpid (), session->vep.next_sid, session_index);
3903 ASSERT (next_session->vep.prev_sid == session_index);
3904 next_session->vep.prev_sid = session->vep.prev_sid;
3907 memset (&session->vep, 0, sizeof (session->vep));
3908 session->vep.next_sid = ~0;
3909 session->vep.prev_sid = ~0;
3910 session->vep.vep_idx = ~0;
3911 session->is_vep_session = 0;
3912 if (VPPCOM_DEBUG > 1)
3913 clib_warning ("VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
3914 getpid (), vep_idx, session_index);
3915 if (VPPCOM_DEBUG > 0)
3918 ELOG_TYPE_DECLARE (e) =
3920 .format = "epoll_ctldel: vep:%d",
3921 .format_args = "i4",
3928 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3936 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
3940 vep_verify_epoll_chain (vep_idx);
3943 VCL_SESSION_UNLOCK ();
3948 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3949 int maxevents, double wait_for_time)
3951 session_t *vep_session;
3952 elog_track_t vep_elog_track;
3954 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3955 u32 keep_trying = 1;
3957 u32 vep_next_sid, wait_cont_idx;
3960 if (PREDICT_FALSE (maxevents <= 0))
3962 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
3963 getpid (), maxevents);
3964 return VPPCOM_EINVAL;
3966 memset (events, 0, sizeof (*events) * maxevents);
3968 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
3969 vep_next_sid = vep_session->vep.next_sid;
3970 is_vep = vep_session->is_vep;
3971 wait_cont_idx = vep_session->wait_cont_idx;
3972 vep_elog_track = vep_session->elog_track;
3973 VCL_SESSION_UNLOCK ();
3975 if (PREDICT_FALSE (!is_vep))
3977 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3978 getpid (), vep_idx);
3982 if (PREDICT_FALSE (vep_next_sid == ~0))
3984 if (VPPCOM_DEBUG > 1)
3985 clib_warning ("VCL<%d>: WARNING: vep_idx (%u) is empty!",
3986 getpid (), vep_idx);
3987 if (VPPCOM_DEBUG > 1)
3990 ELOG_TYPE_DECLARE (e) =
3992 .format = "WRN: vep_idx:%d empty",
3993 .format_args = "i4",
4000 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_elog_track);
4013 elog_track_t session_elog_track;
4015 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
4016 sid != ~0; sid = next_sid)
4018 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
4019 u8 add_event, is_vep_session;
4021 u64 session_ev_data;
4023 VCL_SESSION_LOCK_AND_GET (sid, &session);
4024 next_sid = session->vep.next_sid;
4025 session_events = session->vep.ev.events;
4026 et_mask = session->vep.et_mask;
4027 is_vep = session->is_vep;
4028 is_vep_session = session->is_vep_session;
4029 session_vep_idx = session->vep.vep_idx;
4030 session_ev_data = session->vep.ev.data.u64;
4032 if (VPPCOM_DEBUG > 0)
4034 session_elog_track = session->elog_track;
4037 VCL_SESSION_UNLOCK ();
4039 if (PREDICT_FALSE (is_vep))
4041 if (VPPCOM_DEBUG > 0)
4042 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
4043 getpid (), vep_idx);
4044 if (VPPCOM_DEBUG > 0)
4047 ELOG_TYPE_DECLARE (e) =
4049 .format = "ERR:vep_idx:%d is vep",
4050 .format_args = "i4",
4057 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
4066 if (PREDICT_FALSE (!is_vep_session))
4068 if (VPPCOM_DEBUG > 0)
4069 clib_warning ("VCL<%d>: ERROR: session (%u) is not "
4070 "a vep session!", getpid (), sid);
4071 if (VPPCOM_DEBUG > 0)
4074 ELOG_TYPE_DECLARE (e) =
4076 .format = "ERR:SID:%d not vep",
4077 .format_args = "i4",
4084 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
4093 if (PREDICT_FALSE (session_vep_idx != vep_idx))
4095 clib_warning ("VCL<%d>: ERROR: session (%u) "
4096 "vep_idx (%u) != vep_idx (%u)!",
4097 getpid (), sid, session_vep_idx, vep_idx);
4102 add_event = clear_et_mask = 0;
4104 if (EPOLLIN & session_events)
4106 VCL_SESSION_LOCK_AND_GET (sid, &session);
4107 ready = vppcom_session_read_ready (session, sid);
4108 VCL_SESSION_UNLOCK ();
4109 if ((ready > 0) && (EPOLLIN & et_mask))
4112 events[num_ev].events |= EPOLLIN;
4113 if (((EPOLLET | EPOLLIN) & session_events) ==
4114 (EPOLLET | EPOLLIN))
4115 clear_et_mask |= EPOLLIN;
4122 case VPPCOM_ECONNRESET:
4123 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
4127 events[num_ev].events |= EPOLLERR;
4133 if (EPOLLOUT & session_events)
4135 VCL_SESSION_LOCK_AND_GET (sid, &session);
4136 ready = vppcom_session_write_ready (session, sid);
4137 VCL_SESSION_UNLOCK ();
4138 if ((ready > 0) && (EPOLLOUT & et_mask))
4141 events[num_ev].events |= EPOLLOUT;
4142 if (((EPOLLET | EPOLLOUT) & session_events) ==
4143 (EPOLLET | EPOLLOUT))
4144 clear_et_mask |= EPOLLOUT;
4151 case VPPCOM_ECONNRESET:
4152 events[num_ev].events |= EPOLLHUP;
4156 events[num_ev].events |= EPOLLERR;
4164 events[num_ev].data.u64 = session_ev_data;
4165 if (EPOLLONESHOT & session_events)
4167 VCL_SESSION_LOCK_AND_GET (sid, &session);
4168 session->vep.ev.events = 0;
4169 VCL_SESSION_UNLOCK ();
4172 if (num_ev == maxevents)
4174 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
4175 vep_session->wait_cont_idx = next_sid;
4176 VCL_SESSION_UNLOCK ();
4180 if (wait_cont_idx != ~0)
4183 next_sid = vep_next_sid;
4184 else if (next_sid == wait_cont_idx)
4188 if (wait_for_time != -1)
4189 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
4191 while ((num_ev == 0) && keep_trying);
4193 if (wait_cont_idx != ~0)
4195 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
4196 vep_session->wait_cont_idx = ~0;
4197 VCL_SESSION_UNLOCK ();
4200 return (rv != VPPCOM_OK) ? rv : num_ev;
4204 vppcom_session_attr (uint32_t session_index, uint32_t op,
4205 void *buffer, uint32_t * buflen)
4209 u32 *flags = buffer;
4210 vppcom_endpt_t *ep = buffer;
4212 VCL_SESSION_LOCK_AND_GET (session_index, &session);
4218 case VPPCOM_ATTR_GET_NREAD:
4219 rv = vppcom_session_read_ready (session, session_index);
4220 if (VPPCOM_DEBUG > 2)
4221 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
4223 if (VPPCOM_DEBUG > 0)
4226 ELOG_TYPE_DECLARE (e) =
4228 .format = "VPPCOM_ATTR_GET_NREAD: nread=%d",
4229 .format_args = "i4",
4236 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4244 case VPPCOM_ATTR_GET_NWRITE:
4245 rv = vppcom_session_write_ready (session, session_index);
4246 if (VPPCOM_DEBUG > 2)
4247 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
4248 getpid (), session_index, rv);
4249 if (VPPCOM_DEBUG > 0)
4252 ELOG_TYPE_DECLARE (e) =
4254 .format = "VPPCOM_ATTR_GET_NWRITE: nwrite=%d",
4255 .format_args = "i4",
4262 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4269 case VPPCOM_ATTR_GET_FLAGS:
4270 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
4272 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
4273 VCL_SESS_ATTR_NONBLOCK));
4274 *buflen = sizeof (*flags);
4275 if (VPPCOM_DEBUG > 2)
4276 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, "
4277 "flags = 0x%08x, is_nonblocking = %u", getpid (),
4278 session_index, *flags,
4279 VCL_SESS_ATTR_TEST (session->attr,
4280 VCL_SESS_ATTR_NONBLOCK));
4281 if (VPPCOM_DEBUG > 0)
4284 ELOG_TYPE_DECLARE (e) =
4286 .format = "VPPCOM_ATTR_GET_FLAGS: flags=%x is_nonblk=%d",
4287 .format_args = "i4i4",
4295 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4298 ed->is_nonblk = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
4307 case VPPCOM_ATTR_SET_FLAGS:
4308 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
4310 if (*flags & O_NONBLOCK)
4311 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
4313 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
4315 if (VPPCOM_DEBUG > 2)
4316 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, "
4317 "flags = 0x%08x, is_nonblocking = %u",
4318 getpid (), session_index, *flags,
4319 VCL_SESS_ATTR_TEST (session->attr,
4320 VCL_SESS_ATTR_NONBLOCK));
4321 if (VPPCOM_DEBUG > 0)
4324 ELOG_TYPE_DECLARE (e) =
4326 .format = "VPPCOM_ATTR_SET_FLAGS: flags=%x is_nonblk=%d",
4327 .format_args = "i4i4",
4335 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4338 ed->is_nonblk = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
4346 case VPPCOM_ATTR_GET_PEER_ADDR:
4347 if (PREDICT_TRUE (buffer && buflen &&
4348 (*buflen >= sizeof (*ep)) && ep->ip))
4350 ep->is_ip4 = session->peer_addr.is_ip4;
4351 ep->port = session->peer_port;
4352 if (session->peer_addr.is_ip4)
4353 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
4354 sizeof (ip4_address_t));
4356 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
4357 sizeof (ip6_address_t));
4358 *buflen = sizeof (*ep);
4359 if (VPPCOM_DEBUG > 1)
4360 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, "
4361 "is_ip4 = %u, addr = %U, port %u", getpid (),
4362 session_index, ep->is_ip4, format_ip46_address,
4363 &session->peer_addr.ip46,
4364 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
4365 clib_net_to_host_u16 (ep->port));
4366 if (VPPCOM_DEBUG > 0)
4371 ELOG_TYPE_DECLARE (e) =
4373 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:%d.%d.%d.%d:%d",
4374 .format_args = "i1i1i1i1i2",
4376 CLIB_PACKED (struct {
4381 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4383 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
4384 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
4385 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
4386 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
4387 ed->port = clib_net_to_host_u16 (session->peer_port);
4393 ELOG_TYPE_DECLARE (e) =
4395 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:IP6:%d",
4396 .format_args = "i2",
4398 CLIB_PACKED (struct {
4402 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4404 ed->port = clib_net_to_host_u16 (session->peer_port);
4413 case VPPCOM_ATTR_GET_LCL_ADDR:
4414 if (PREDICT_TRUE (buffer && buflen &&
4415 (*buflen >= sizeof (*ep)) && ep->ip))
4417 ep->is_ip4 = session->lcl_addr.is_ip4;
4418 ep->port = session->lcl_port;
4419 if (session->lcl_addr.is_ip4)
4420 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
4421 sizeof (ip4_address_t));
4423 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
4424 sizeof (ip6_address_t));
4425 *buflen = sizeof (*ep);
4426 if (VPPCOM_DEBUG > 1)
4427 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, "
4428 "is_ip4 = %u, addr = %U port %d", getpid (),
4429 session_index, ep->is_ip4, format_ip46_address,
4430 &session->lcl_addr.ip46,
4431 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
4432 clib_net_to_host_u16 (ep->port));
4433 if (VPPCOM_DEBUG > 0)
4438 ELOG_TYPE_DECLARE (e) =
4440 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:%d.%d.%d.%d:%d",
4441 .format_args = "i1i1i1i1i2",
4443 CLIB_PACKED (struct {
4448 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4450 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
4451 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
4452 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
4453 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
4454 ed->port = clib_net_to_host_u16 (session->peer_port);
4460 ELOG_TYPE_DECLARE (e) =
4462 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:IP6:%d",
4463 .format_args = "i2",
4465 CLIB_PACKED (struct {
4469 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4471 ed->port = clib_net_to_host_u16 (session->peer_port);
4480 case VPPCOM_ATTR_GET_LIBC_EPFD:
4481 rv = session->libc_epfd;
4482 if (VPPCOM_DEBUG > 2)
4483 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
4485 if (VPPCOM_DEBUG > 0)
4488 ELOG_TYPE_DECLARE (e) =
4490 .format = "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd=%d",
4491 .format_args = "i4",
4493 CLIB_PACKED (struct {
4497 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4498 ed->data = session->libc_epfd;
4504 case VPPCOM_ATTR_SET_LIBC_EPFD:
4505 if (PREDICT_TRUE (buffer && buflen &&
4506 (*buflen == sizeof (session->libc_epfd))))
4508 session->libc_epfd = *(int *) buffer;
4509 *buflen = sizeof (session->libc_epfd);
4511 if (VPPCOM_DEBUG > 2)
4512 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
4513 "buflen %d", getpid (), session->libc_epfd,
4515 if (VPPCOM_DEBUG > 0)
4518 ELOG_TYPE_DECLARE (e) =
4520 .format = "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd=%s%d buflen=%d",
4521 .format_args = "t1i4i4",
4522 .n_enum_strings = 2,
4523 .enum_strings = {"", "-",},
4525 CLIB_PACKED (struct {
4530 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4532 ed->sign = (session->libc_epfd < 0);
4533 ed->data[0] = abs(session->libc_epfd);
4534 ed->data[1] = *buflen;
4542 case VPPCOM_ATTR_GET_PROTOCOL:
4543 if (buffer && buflen && (*buflen >= sizeof (int)))
4545 *(int *) buffer = session->proto;
4546 *buflen = sizeof (int);
4548 if (VPPCOM_DEBUG > 2)
4549 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), "
4550 "buflen %d", getpid (), *(int *) buffer,
4551 *(int *) buffer ? "UDP" : "TCP", *buflen);
4552 if (VPPCOM_DEBUG > 0)
4555 ELOG_TYPE_DECLARE (e) =
4557 .format = "VPPCOM_ATTR_GET_PROTOCOL: %s buflen=%d",
4558 .format_args = "t1i4",
4559 .n_enum_strings = 2,
4560 .enum_strings = {"TCP", "UDP",},
4563 CLIB_PACKED (struct {
4568 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4569 ed->proto = session->proto;
4570 ed->buflen = *(int *) buffer;
4578 case VPPCOM_ATTR_GET_LISTEN:
4579 if (buffer && buflen && (*buflen >= sizeof (int)))
4581 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4582 VCL_SESS_ATTR_LISTEN);
4583 *buflen = sizeof (int);
4585 if (VPPCOM_DEBUG > 2)
4586 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, "
4587 "buflen %d", getpid (), *(int *) buffer, *buflen);
4588 if (VPPCOM_DEBUG > 0)
4591 ELOG_TYPE_DECLARE (e) =
4593 .format = "VPPCOM_ATTR_GET_LISTEN: %d buflen=%d",
4594 .format_args = "i4i4",
4601 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4602 ed->data[0] = *(int *) buffer;
4603 ed->data[1] = *buflen;
4611 case VPPCOM_ATTR_GET_ERROR:
4612 if (buffer && buflen && (*buflen >= sizeof (int)))
4614 *(int *) buffer = 0;
4615 *buflen = sizeof (int);
4617 if (VPPCOM_DEBUG > 2)
4618 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, "
4619 "buflen %d, #VPP-TBD#", getpid (),
4620 *(int *) buffer, *buflen);
4621 if (VPPCOM_DEBUG > 0)
4624 ELOG_TYPE_DECLARE (e) =
4626 .format = "VPPCOM_ATTR_GET_ERROR: %d buflen=%d",
4627 .format_args = "i4i4",
4634 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4635 ed->data[0] = *(int *) buffer;
4636 ed->data[1] = *buflen;
4644 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
4645 if (buffer && buflen && (*buflen >= sizeof (u32)))
4649 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
4650 session->tx_fifo ? session->tx_fifo->nitems :
4651 vcm->cfg.tx_fifo_size);
4652 *buflen = sizeof (u32);
4654 if (VPPCOM_DEBUG > 2)
4655 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
4656 "buflen %d, #VPP-TBD#", getpid (),
4657 *(size_t *) buffer, *(size_t *) buffer, *buflen);
4658 if (VPPCOM_DEBUG > 0)
4661 ELOG_TYPE_DECLARE (e) =
4663 .format = "VPPCOM_ATTR_GET_TX_FIFO_LEN: 0x%x buflen=%d",
4664 .format_args = "i4i4",
4671 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4672 ed->data[0] = *(size_t *) buffer;
4673 ed->data[1] = *buflen;
4681 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
4682 if (buffer && buflen && (*buflen == sizeof (u32)))
4685 session->sndbuf_size = *(u32 *) buffer;
4686 if (VPPCOM_DEBUG > 2)
4687 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
4688 "buflen %d, #VPP-TBD#", getpid (),
4689 session->sndbuf_size, session->sndbuf_size,
4691 if (VPPCOM_DEBUG > 0)
4694 ELOG_TYPE_DECLARE (e) =
4696 .format = "VPPCOM_ATTR_SET_TX_FIFO_LEN: 0x%x buflen=%d",
4697 .format_args = "i4i4",
4704 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4705 ed->data[0] = session->sndbuf_size;
4706 ed->data[1] = *buflen;
4714 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
4715 if (buffer && buflen && (*buflen >= sizeof (u32)))
4719 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
4720 session->rx_fifo ? session->rx_fifo->nitems :
4721 vcm->cfg.rx_fifo_size);
4722 *buflen = sizeof (u32);
4724 if (VPPCOM_DEBUG > 2)
4725 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
4726 "buflen %d, #VPP-TBD#", getpid (),
4727 *(size_t *) buffer, *(size_t *) buffer, *buflen);
4728 if (VPPCOM_DEBUG > 0)
4731 ELOG_TYPE_DECLARE (e) =
4733 .format = "VPPCOM_ATTR_GET_RX_FIFO_LEN: 0x%x buflen=%d",
4734 .format_args = "i4i4",
4741 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4742 ed->data[0] = *(size_t *) buffer;
4743 ed->data[1] = *buflen;
4751 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
4752 if (buffer && buflen && (*buflen == sizeof (u32)))
4755 session->rcvbuf_size = *(u32 *) buffer;
4756 if (VPPCOM_DEBUG > 2)
4757 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
4758 "buflen %d, #VPP-TBD#", getpid (),
4759 session->sndbuf_size, session->sndbuf_size,
4761 if (VPPCOM_DEBUG > 0)
4764 ELOG_TYPE_DECLARE (e) =
4766 .format = "VPPCOM_ATTR_SET_RX_FIFO_LEN: 0x%x buflen=%d",
4767 .format_args = "i4i4",
4774 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4775 ed->data[0] = session->sndbuf_size;
4776 ed->data[1] = *buflen;
4784 case VPPCOM_ATTR_GET_REUSEADDR:
4785 if (buffer && buflen && (*buflen >= sizeof (int)))
4788 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4789 VCL_SESS_ATTR_REUSEADDR);
4790 *buflen = sizeof (int);
4792 if (VPPCOM_DEBUG > 2)
4793 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
4794 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4796 if (VPPCOM_DEBUG > 0)
4799 ELOG_TYPE_DECLARE (e) =
4801 .format = "VPPCOM_ATTR_GET_REUSEADDR: %d buflen=%d",
4802 .format_args = "i4i4",
4809 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4810 ed->data[0] = *(int *) buffer;
4811 ed->data[1] = *buflen;
4819 case VPPCOM_ATTR_SET_REUSEADDR:
4820 if (buffer && buflen && (*buflen == sizeof (int)) &&
4821 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4824 if (*(int *) buffer)
4825 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
4827 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
4829 if (VPPCOM_DEBUG > 2)
4830 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, "
4831 "buflen %d, #VPP-TBD#", getpid (),
4832 VCL_SESS_ATTR_TEST (session->attr,
4833 VCL_SESS_ATTR_REUSEADDR),
4835 if (VPPCOM_DEBUG > 0)
4838 ELOG_TYPE_DECLARE (e) =
4840 .format = "VPPCOM_ATTR_SET_REUSEADDR: %d buflen=%d",
4841 .format_args = "i4i4",
4848 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4849 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4850 VCL_SESS_ATTR_REUSEADDR);
4851 ed->data[1] = *buflen;
4859 case VPPCOM_ATTR_GET_REUSEPORT:
4860 if (buffer && buflen && (*buflen >= sizeof (int)))
4863 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4864 VCL_SESS_ATTR_REUSEPORT);
4865 *buflen = sizeof (int);
4867 if (VPPCOM_DEBUG > 2)
4868 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, "
4869 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4871 if (VPPCOM_DEBUG > 0)
4874 ELOG_TYPE_DECLARE (e) =
4876 .format = "VPPCOM_ATTR_GET_REUSEPORT: %d buflen=%d",
4877 .format_args = "i4i4",
4884 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4885 ed->data[0] = *(int *) buffer;
4886 ed->data[1] = *buflen;
4894 case VPPCOM_ATTR_SET_REUSEPORT:
4895 if (buffer && buflen && (*buflen == sizeof (int)) &&
4896 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4899 if (*(int *) buffer)
4900 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
4902 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
4904 if (VPPCOM_DEBUG > 2)
4905 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, "
4906 "buflen %d, #VPP-TBD#", getpid (),
4907 VCL_SESS_ATTR_TEST (session->attr,
4908 VCL_SESS_ATTR_REUSEPORT),
4910 if (VPPCOM_DEBUG > 0)
4913 ELOG_TYPE_DECLARE (e) =
4915 .format = "VPPCOM_ATTR_SET_REUSEPORT: %d buflen=%d",
4916 .format_args = "i4i4",
4923 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4924 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4925 VCL_SESS_ATTR_REUSEPORT);
4926 ed->data[1] = *buflen;
4934 case VPPCOM_ATTR_GET_BROADCAST:
4935 if (buffer && buflen && (*buflen >= sizeof (int)))
4938 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4939 VCL_SESS_ATTR_BROADCAST);
4940 *buflen = sizeof (int);
4942 if (VPPCOM_DEBUG > 2)
4943 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, "
4944 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4946 if (VPPCOM_DEBUG > 0)
4949 ELOG_TYPE_DECLARE (e) =
4951 .format = "VPPCOM_ATTR_GET_BROADCAST: %d buflen=%d",
4952 .format_args = "i4i4",
4959 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4960 ed->data[0] = *(int *) buffer;
4961 ed->data[1] = *buflen;
4969 case VPPCOM_ATTR_SET_BROADCAST:
4970 if (buffer && buflen && (*buflen == sizeof (int)))
4973 if (*(int *) buffer)
4974 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
4976 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
4978 if (VPPCOM_DEBUG > 2)
4979 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, "
4980 "buflen %d, #VPP-TBD#", getpid (),
4981 VCL_SESS_ATTR_TEST (session->attr,
4982 VCL_SESS_ATTR_BROADCAST),
4984 if (VPPCOM_DEBUG > 0)
4987 ELOG_TYPE_DECLARE (e) =
4989 .format = "VPPCOM_ATTR_SET_BROADCAST: %d buflen=%d",
4990 .format_args = "i4i4",
4997 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4998 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4999 VCL_SESS_ATTR_BROADCAST);
5000 ed->data[1] = *buflen;
5008 case VPPCOM_ATTR_GET_V6ONLY:
5009 if (buffer && buflen && (*buflen >= sizeof (int)))
5012 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5013 VCL_SESS_ATTR_V6ONLY);
5014 *buflen = sizeof (int);
5016 if (VPPCOM_DEBUG > 2)
5017 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, "
5018 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5020 if (VPPCOM_DEBUG > 0)
5023 ELOG_TYPE_DECLARE (e) =
5025 .format = "VPPCOM_ATTR_GET_V6ONLY: %d buflen=%d",
5026 .format_args = "i4i4",
5033 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5034 ed->data[0] = *(int *) buffer;
5035 ed->data[1] = *buflen;
5043 case VPPCOM_ATTR_SET_V6ONLY:
5044 if (buffer && buflen && (*buflen == sizeof (int)))
5047 if (*(int *) buffer)
5048 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
5050 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
5052 if (VPPCOM_DEBUG > 2)
5053 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, "
5054 "buflen %d, #VPP-TBD#", getpid (),
5055 VCL_SESS_ATTR_TEST (session->attr,
5056 VCL_SESS_ATTR_V6ONLY), *buflen);
5057 if (VPPCOM_DEBUG > 0)
5060 ELOG_TYPE_DECLARE (e) =
5062 .format = "VPPCOM_ATTR_SET_V6ONLY: %d buflen=%d",
5063 .format_args = "i4i4",
5070 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5071 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5072 VCL_SESS_ATTR_V6ONLY);
5073 ed->data[1] = *buflen;
5081 case VPPCOM_ATTR_GET_KEEPALIVE:
5082 if (buffer && buflen && (*buflen >= sizeof (int)))
5085 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5086 VCL_SESS_ATTR_KEEPALIVE);
5087 *buflen = sizeof (int);
5089 if (VPPCOM_DEBUG > 2)
5090 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, "
5091 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5093 if (VPPCOM_DEBUG > 0)
5096 ELOG_TYPE_DECLARE (e) =
5098 .format = "VPPCOM_ATTR_GET_KEEPALIVE: %d buflen=%d",
5099 .format_args = "i4i4",
5106 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5107 ed->data[0] = *(int *) buffer;
5108 ed->data[1] = *buflen;
5116 case VPPCOM_ATTR_SET_KEEPALIVE:
5117 if (buffer && buflen && (*buflen == sizeof (int)))
5120 if (*(int *) buffer)
5121 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5123 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5125 if (VPPCOM_DEBUG > 2)
5126 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, "
5127 "buflen %d, #VPP-TBD#", getpid (),
5128 VCL_SESS_ATTR_TEST (session->attr,
5129 VCL_SESS_ATTR_KEEPALIVE),
5131 if (VPPCOM_DEBUG > 0)
5134 ELOG_TYPE_DECLARE (e) =
5136 .format = "VPPCOM_ATTR_SET_KEEPALIVE: %d buflen=%d",
5137 .format_args = "i4i4",
5144 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5145 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5146 VCL_SESS_ATTR_KEEPALIVE);
5147 ed->data[1] = *buflen;
5155 case VPPCOM_ATTR_GET_TCP_NODELAY:
5156 if (buffer && buflen && (*buflen >= sizeof (int)))
5159 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5160 VCL_SESS_ATTR_TCP_NODELAY);
5161 *buflen = sizeof (int);
5163 if (VPPCOM_DEBUG > 2)
5164 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, "
5165 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5167 if (VPPCOM_DEBUG > 0)
5170 ELOG_TYPE_DECLARE (e) =
5172 .format = "VPPCOM_ATTR_GET_TCP_NODELAY: %d buflen=%d",
5173 .format_args = "i4i4",
5180 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5181 ed->data[0] = *(int *) buffer;
5182 ed->data[1] = *buflen;
5190 case VPPCOM_ATTR_SET_TCP_NODELAY:
5191 if (buffer && buflen && (*buflen == sizeof (int)))
5194 if (*(int *) buffer)
5195 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5197 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5199 if (VPPCOM_DEBUG > 2)
5200 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, "
5201 "buflen %d, #VPP-TBD#", getpid (),
5202 VCL_SESS_ATTR_TEST (session->attr,
5203 VCL_SESS_ATTR_TCP_NODELAY),
5205 if (VPPCOM_DEBUG > 0)
5208 ELOG_TYPE_DECLARE (e) =
5210 .format = "VPPCOM_ATTR_SET_TCP_NODELAY: %d buflen=%d",
5211 .format_args = "i4i4",
5218 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5219 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5220 VCL_SESS_ATTR_TCP_NODELAY);
5221 ed->data[1] = *buflen;
5229 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
5230 if (buffer && buflen && (*buflen >= sizeof (int)))
5233 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5234 VCL_SESS_ATTR_TCP_KEEPIDLE);
5235 *buflen = sizeof (int);
5237 if (VPPCOM_DEBUG > 2)
5238 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, "
5239 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5241 if (VPPCOM_DEBUG > 0)
5244 ELOG_TYPE_DECLARE (e) =
5246 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5247 .format_args = "i4i4",
5254 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5255 ed->data[0] = *(int *) buffer;
5256 ed->data[1] = *buflen;
5264 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
5265 if (buffer && buflen && (*buflen == sizeof (int)))
5268 if (*(int *) buffer)
5269 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5271 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5273 if (VPPCOM_DEBUG > 2)
5274 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, "
5275 "buflen %d, #VPP-TBD#", getpid (),
5276 VCL_SESS_ATTR_TEST (session->attr,
5277 VCL_SESS_ATTR_TCP_KEEPIDLE),
5279 if (VPPCOM_DEBUG > 0)
5282 ELOG_TYPE_DECLARE (e) =
5284 .format = "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d buflen=%d",
5285 .format_args = "i4i4",
5292 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5293 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5294 VCL_SESS_ATTR_TCP_KEEPIDLE);
5295 ed->data[1] = *buflen;
5303 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
5304 if (buffer && buflen && (*buflen >= sizeof (int)))
5307 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5308 VCL_SESS_ATTR_TCP_KEEPINTVL);
5309 *buflen = sizeof (int);
5311 if (VPPCOM_DEBUG > 2)
5312 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, "
5313 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5315 if (VPPCOM_DEBUG > 0)
5318 ELOG_TYPE_DECLARE (e) =
5320 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5321 .format_args = "i4i4",
5328 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5329 ed->data[0] = *(int *) buffer;
5330 ed->data[1] = *buflen;
5338 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
5339 if (buffer && buflen && (*buflen == sizeof (int)))
5342 if (*(int *) buffer)
5343 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5345 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5347 if (VPPCOM_DEBUG > 2)
5348 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, "
5349 "buflen %d, #VPP-TBD#", getpid (),
5350 VCL_SESS_ATTR_TEST (session->attr,
5351 VCL_SESS_ATTR_TCP_KEEPINTVL),
5353 if (VPPCOM_DEBUG > 0)
5356 ELOG_TYPE_DECLARE (e) =
5358 .format = "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d buflen=%d",
5359 .format_args = "i4i4",
5366 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5367 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5368 VCL_SESS_ATTR_TCP_KEEPINTVL);
5369 ed->data[1] = *buflen;
5377 case VPPCOM_ATTR_GET_TCP_USER_MSS:
5378 if (buffer && buflen && (*buflen >= sizeof (u32)))
5381 *(u32 *) buffer = session->user_mss;
5382 *buflen = sizeof (int);
5384 if (VPPCOM_DEBUG > 2)
5385 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, "
5386 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5388 if (VPPCOM_DEBUG > 0)
5391 ELOG_TYPE_DECLARE (e) =
5393 .format = "VPPCOM_ATTR_GET_TCP_USER_MSS: %d buflen=%d",
5394 .format_args = "i4i4",
5401 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5402 ed->data[0] = *(int *) buffer;
5403 ed->data[1] = *buflen;
5411 case VPPCOM_ATTR_SET_TCP_USER_MSS:
5412 if (buffer && buflen && (*buflen == sizeof (u32)))
5415 session->user_mss = *(u32 *) buffer;
5417 if (VPPCOM_DEBUG > 2)
5418 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, "
5419 "buflen %d, #VPP-TBD#", getpid (),
5420 session->user_mss, *buflen);
5421 if (VPPCOM_DEBUG > 0)
5424 ELOG_TYPE_DECLARE (e) =
5426 .format = "VPPCOM_ATTR_SET_TCP_USER_MSS: %d buflen=%d",
5427 .format_args = "i4i4",
5434 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5435 ed->data[0] = session->user_mss;
5436 ed->data[1] = *buflen;
5450 VCL_SESSION_UNLOCK ();
5455 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
5456 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5459 session_t *session = 0;
5463 VCL_SESSION_LOCK ();
5464 rv = vppcom_session_at_index (session_index, &session);
5465 if (PREDICT_FALSE (rv))
5467 VCL_SESSION_UNLOCK ();
5468 if (VPPCOM_DEBUG > 0)
5469 clib_warning ("VCL<%d>: invalid session, "
5470 "sid (%u) has been closed!",
5471 getpid (), session_index);
5472 if (VPPCOM_DEBUG > 0)
5475 ELOG_TYPE_DECLARE (e) =
5477 .format = "invalid session: %d closed",
5478 .format_args = "i4",
5485 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
5486 ed->data = session_index;
5490 VCL_SESSION_UNLOCK ();
5493 ep->is_ip4 = session->peer_addr.is_ip4;
5494 ep->port = session->peer_port;
5495 if (session->peer_addr.is_ip4)
5496 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
5497 sizeof (ip4_address_t));
5499 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
5500 sizeof (ip6_address_t));
5501 VCL_SESSION_UNLOCK ();
5505 rv = vppcom_session_read (session_index, buffer, buflen);
5506 else if (flags & MSG_PEEK)
5507 rv = vppcom_session_peek (session_index, buffer, buflen);
5510 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
5512 rv = VPPCOM_EAFNOSUPPORT;
5520 vppcom_session_sendto (uint32_t session_index, void *buffer,
5521 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5524 return VPPCOM_EINVAL;
5529 return VPPCOM_EINVAL;
5534 // TBD check the flags and do the right thing
5535 if (VPPCOM_DEBUG > 2)
5536 clib_warning ("VCL<%d>: handling flags 0x%u (%d) "
5537 "not implemented yet.", getpid (), flags, flags);
5540 return (vppcom_session_write (session_index, buffer, buflen));
5544 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
5546 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
5547 u32 i, keep_trying = 1;
5550 if (VPPCOM_DEBUG > 3)
5551 clib_warning ("VCL<%d>: vp %p, nsids %u, wait_for_time %f",
5552 getpid (), vp, n_sids, wait_for_time);
5555 return VPPCOM_EFAULT;
5561 for (i = 0; i < n_sids; i++)
5563 ASSERT (vp[i].revents);
5565 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
5566 VCL_SESSION_UNLOCK ();
5571 if (POLLIN & vp[i].events)
5573 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
5574 rv = vppcom_session_read_ready (session, vp[i].sid);
5575 VCL_SESSION_UNLOCK ();
5578 *vp[i].revents |= POLLIN;
5585 case VPPCOM_ECONNRESET:
5586 *vp[i].revents = POLLHUP;
5590 *vp[i].revents = POLLERR;
5597 if (POLLOUT & vp[i].events)
5599 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
5600 rv = vppcom_session_write_ready (session, vp[i].sid);
5601 VCL_SESSION_UNLOCK ();
5604 *vp[i].revents |= POLLOUT;
5611 case VPPCOM_ECONNRESET:
5612 *vp[i].revents = POLLHUP;
5616 *vp[i].revents = POLLERR;
5623 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
5626 *vp[i].revents = POLLNVAL;
5630 if (wait_for_time != -1)
5631 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
5633 while ((num_ev == 0) && keep_trying);
5635 if (VPPCOM_DEBUG > 3)
5637 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
5638 for (i = 0; i < n_sids; i++)
5640 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
5641 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
5642 vp[i].events, *vp[i].revents);
5649 * VPPCOM Event Functions
5653 vppcom_session_io_thread_fn (void *arg)
5655 vppcom_session_io_thread_t *evt = (vppcom_session_io_thread_t *) arg;
5656 u32 *session_indexes = 0, *session_index;
5663 vec_reset_length (session_indexes);
5664 VCE_IO_SESSIONS_LOCK ();
5665 pool_foreach (session_index, evt->active_session_indexes, (
5672 VCE_IO_SESSIONS_UNLOCK ();
5673 if (session_indexes)
5675 for (i = 0; i < vec_len (session_indexes); ++i)
5677 VCL_SESSION_LOCK_AND_GET (session_indexes[i], &session);
5678 bytes = svm_fifo_max_dequeue (session->rx_fifo);
5679 VCL_SESSION_UNLOCK ();
5683 vppcom_ioevent_t *eio;
5689 pool_get (vcm->event_thread.vce_events, ev);
5690 ev_idx = (u32) (ev - vcm->event_thread.vce_events);
5691 eio = vce_get_event_data (ev, sizeof (*eio));
5692 ev->evk.eid = VCL_EVENT_IOEVENT_RX_FIFO;
5693 ev->evk.session_index = session_indexes[i];
5695 eio->session_index = session_indexes[i];
5697 VCL_EVENTS_UNLOCK ();
5699 rv = vce_generate_event (&vcm->event_thread, ev_idx);
5705 ts.tv_nsec = 1000000; /* 1 millisecond */
5706 nanosleep (&ts, NULL);
5709 VCL_SESSION_UNLOCK ();
5714 vppcom_start_io_event_thread (vppcom_session_io_thread_t * evt,
5717 pthread_cond_init (&(evt->vce_io_cond), NULL);
5718 pthread_mutex_init (&(evt->vce_io_lock), NULL);
5720 clib_spinlock_init (&(evt->io_sessions_lockp));
5722 return pthread_create (&(evt->thread), NULL /* attr */ ,
5723 vppcom_session_io_thread_fn, evt);
5727 vce_registered_ioevent_handler_fn (void *arg)
5729 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
5730 vppcom_ioevent_t *eio;
5732 u32 ioevt_ndx = (u64) (reg->handler_fn_args);
5733 vppcom_session_ioevent_t *ioevent, ioevent_;
5736 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
5737 eio = vce_get_event_data (ev, sizeof (*eio));
5738 VCL_EVENTS_UNLOCK ();
5740 VCL_IO_SESSIONS_LOCK ();
5741 ioevent = pool_elt_at_index (vcm->session_io_thread.ioevents, ioevt_ndx);
5742 ioevent_ = *ioevent;
5743 VCL_IO_SESSIONS_UNLOCK ();
5744 (ioevent_.user_cb) (eio, ioevent_.user_cb_data);
5745 vce_clear_event (&vcm->event_thread, reg->ev_idx);
5748 /*TODO - Unregister check in close for this listener */
5753 vce_registered_listener_connect_handler_fn (void *arg)
5755 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
5756 vce_event_connect_request_t *ecr;
5760 session_t *new_session;
5763 vppcom_session_listener_t *session_listener =
5764 (vppcom_session_listener_t *) reg->handler_fn_args;
5767 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
5768 ecr = vce_get_event_data (ev, sizeof (*ecr));
5769 VCL_EVENTS_UNLOCK ();
5770 VCL_SESSION_LOCK_AND_GET (ecr->accepted_session_index, &new_session);
5772 ep.is_ip4 = new_session->peer_addr.is_ip4;
5773 ep.port = new_session->peer_port;
5774 if (new_session->peer_addr.is_ip4)
5775 clib_memcpy (&ep.ip, &new_session->peer_addr.ip46.ip4,
5776 sizeof (ip4_address_t));
5778 clib_memcpy (&ep.ip, &new_session->peer_addr.ip46.ip6,
5779 sizeof (ip6_address_t));
5781 vppcom_send_accept_session_reply (new_session->vpp_handle,
5782 new_session->client_context,
5783 0 /* retval OK */ );
5784 VCL_SESSION_UNLOCK ();
5786 (session_listener->user_cb) (ecr->accepted_session_index, &ep,
5787 session_listener->user_cb_data);
5789 if (vcm->session_io_thread.io_sessions_lockp)
5791 /* Throw this new accepted session index into the rx poll thread pool */
5792 VCL_IO_SESSIONS_LOCK ();
5793 u32 *active_session_index;
5794 pool_get (vcm->session_io_thread.active_session_indexes,
5795 active_session_index);
5796 *active_session_index = ecr->accepted_session_index;
5797 VCL_IO_SESSIONS_UNLOCK ();
5800 /*TODO - Unregister check in close for this listener */
5804 ASSERT (0); // If we can't get a lock or accepted session fails, lets blow up.
5808 * @brief vce_poll_wait_connect_request_handler_fn
5809 * - used by vppcom_epoll_xxxx() for listener sessions
5810 * - when a vl_api_accept_session_t_handler() generates an event
5811 * this callback is alerted and sets the fields that vppcom_epoll_wait()
5814 * @param arg - void* to be cast to vce_event_handler_reg_t*
5817 vce_poll_wait_connect_request_handler_fn (void *arg)
5819 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
5821 /* Retrieve the VCL_EVENT_CONNECT_REQ_ACCEPTED event */
5822 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
5823 vce_event_connect_request_t *ecr = vce_get_event_data (ev, sizeof (*ecr));
5825 /* Add the accepted_session_index to the FIFO */
5826 VCL_ACCEPT_FIFO_LOCK ();
5827 clib_fifo_add1 (vcm->client_session_index_fifo,
5828 ecr->accepted_session_index);
5829 VCL_ACCEPT_FIFO_UNLOCK ();
5831 /* Recycling the event. */
5834 clib_fifo_add1 (vcm->event_thread.event_index_fifo, reg->ev_idx);
5835 VCL_EVENTS_UNLOCK ();
5839 vppcom_session_register_ioevent_cb (uint32_t session_index,
5840 vppcom_session_ioevent_cb cb,
5841 uint8_t rx, void *ptr)
5844 vce_event_key_t evk;
5845 vppcom_session_ioevent_t *ioevent;
5847 if (!vcm->session_io_thread.io_sessions_lockp)
5848 rv = vppcom_start_io_event_thread (&vcm->session_io_thread, 100 /* DAW_TODO: ??? hard-coded value */
5851 if (rv == VPPCOM_OK)
5855 /* Register handler for ioevent on session_index */
5856 VCL_IO_SESSIONS_LOCK ();
5857 pool_get (vcm->session_io_thread.ioevents, ioevent);
5858 io_evt_ndx = (void *) (ioevent - vcm->session_io_thread.ioevents);
5859 ioevent->user_cb = cb;
5860 ioevent->user_cb_data = ptr;
5861 VCL_IO_SESSIONS_UNLOCK ();
5863 evk.session_index = session_index;
5864 evk.eid = rx ? VCL_EVENT_IOEVENT_RX_FIFO : VCL_EVENT_IOEVENT_TX_FIFO;
5866 (void) vce_register_handler (&vcm->event_thread, &evk,
5867 vce_registered_ioevent_handler_fn,
5874 vppcom_session_register_listener (uint32_t session_index,
5875 vppcom_session_listener_cb cb,
5876 vppcom_session_listener_errcb
5877 errcb, uint8_t flags, int q_len, void *ptr)
5880 vce_event_key_t evk;
5881 vppcom_session_listener_t *listener_args;
5883 if (!vcm->session_io_thread.io_sessions_lockp)
5884 rv = vppcom_start_io_event_thread (&vcm->session_io_thread, 100 /* DAW_TODO: ??? hard-coded value */
5890 rv = vppcom_session_listen (session_index, q_len);
5896 /* Register handler for connect_request event on listen_session_index */
5897 listener_args = clib_mem_alloc (sizeof (vppcom_session_listener_t)); // DAW_TODO: Use a pool instead of thrashing the memory allocator!
5898 listener_args->user_cb = cb;
5899 listener_args->user_cb_data = ptr;
5900 listener_args->user_errcb = errcb;
5902 evk.session_index = session_index;
5903 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
5904 (void) vce_register_handler (&vcm->event_thread, &evk,
5905 vce_registered_listener_connect_handler_fn,
5913 * fd.io coding-style-patch-verification: ON
5916 * eval: (c-set-style "gnu")