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 ? IP46_TYPE_IP4 :
1123 IP46_TYPE_IP6, mp->lcl_ip);
1124 session->lcl_port = mp->lcl_port;
1125 vppcom_session_table_add_listener (mp->handle, session_index);
1126 session->state = STATE_LISTEN;
1128 if (VPPCOM_DEBUG > 1)
1129 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
1130 getpid (), mp->handle, mp->context);
1132 VCL_SESSION_UNLOCK ();
1136 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
1139 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
1140 getpid (), mp->context, format_api_error,
1141 ntohl (mp->retval));
1143 else if (VPPCOM_DEBUG > 1)
1144 clib_warning ("VCL<%d>: sid %u: unbind succeeded!",
1145 getpid (), mp->context);
1149 format_ip4_address (u8 * s, va_list * args)
1151 u8 *a = va_arg (*args, u8 *);
1152 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1156 format_ip6_address (u8 * s, va_list * args)
1158 ip6_address_t *a = va_arg (*args, ip6_address_t *);
1159 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
1161 i_max_n_zero = ARRAY_LEN (a->as_u16);
1163 i_first_zero = i_max_n_zero;
1165 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1167 u32 is_zero = a->as_u16[i] == 0;
1168 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1174 if ((!is_zero && n_zeros > max_n_zeros)
1175 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1177 i_max_n_zero = i_first_zero;
1178 max_n_zeros = n_zeros;
1179 i_first_zero = ARRAY_LEN (a->as_u16);
1184 last_double_colon = 0;
1185 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1187 if (i == i_max_n_zero && max_n_zeros > 1)
1189 s = format (s, "::");
1190 i += max_n_zeros - 1;
1191 last_double_colon = 1;
1195 s = format (s, "%s%x",
1196 (last_double_colon || i == 0) ? "" : ":",
1197 clib_net_to_host_u16 (a->as_u16[i]));
1198 last_double_colon = 0;
1205 /* Format an IP46 address. */
1207 format_ip46_address (u8 * s, va_list * args)
1209 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1210 ip46_type_t type = va_arg (*args, ip46_type_t);
1216 is_ip4 = ip46_address_is_ip4 (ip46);
1227 format (s, "%U", format_ip4_address, &ip46->ip4) :
1228 format (s, "%U", format_ip6_address, &ip46->ip6);
1232 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1234 svm_fifo_t *rx_fifo, *tx_fifo;
1235 session_t *session, *listen_session;
1237 vce_event_connect_request_t *ecr;
1243 VCL_SESSION_LOCK ();
1245 VCL_ACCEPT_FIFO_LOCK ();
1246 elts = clib_fifo_free_elts (vcm->client_session_index_fifo);
1247 VCL_ACCEPT_FIFO_UNLOCK ();
1251 clib_warning ("VCL<%d>: client session queue is full!", getpid ());
1252 vppcom_send_accept_session_reply (mp->handle, mp->context,
1253 VNET_API_ERROR_QUEUE_FULL);
1254 VCL_SESSION_UNLOCK ();
1258 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1259 if (!listen_session)
1261 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
1262 "unknown vpp listener handle %llx",
1263 getpid (), mp->listener_handle);
1264 vppcom_send_accept_session_reply (mp->handle, mp->context,
1265 VNET_API_ERROR_INVALID_ARGUMENT);
1266 VCL_SESSION_UNLOCK ();
1270 /* TODO check listener depth and update */
1271 /* TODO on "child" fd close, update listener depth */
1273 /* Allocate local session and set it up */
1274 pool_get (vcm->sessions, session);
1275 memset (session, 0, sizeof (*session));
1276 session_index = (u32) (session - vcm->sessions);
1278 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1279 rx_fifo->client_session_index = session_index;
1280 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1281 tx_fifo->client_session_index = session_index;
1283 session->vpp_handle = mp->handle;
1284 session->client_context = mp->context;
1285 session->rx_fifo = rx_fifo;
1286 session->tx_fifo = tx_fifo;
1287 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1289 session->state = STATE_ACCEPT;
1290 session->peer_port = mp->port;
1291 session->peer_addr.is_ip4 = mp->is_ip4;
1292 session->peer_addr.ip46 = to_ip46 (mp->is_ip4 ? IP46_TYPE_IP4 :
1293 IP46_TYPE_IP6, mp->ip);
1295 /* Add it to lookup table */
1296 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1297 session->lcl_port = listen_session->lcl_port;
1298 session->lcl_addr = listen_session->lcl_addr;
1300 /* Create an event for handlers */
1304 pool_get (vcm->event_thread.vce_events, ev);
1305 ev_idx = (u32) (ev - vcm->event_thread.vce_events);
1306 ecr = vce_get_event_data (ev, sizeof (*ecr));
1307 ev->evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
1308 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1309 ev->evk.session_index = (u32) (listen_session - vcm->sessions);
1310 ecr->accepted_session_index = session_index;
1312 VCL_EVENTS_UNLOCK ();
1314 rv = vce_generate_event (&vcm->event_thread, ev_idx);
1317 if (VPPCOM_DEBUG > 1)
1318 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client accept "
1319 "request from %s address %U port %d queue %p!", getpid (),
1320 mp->handle, session_index, mp->is_ip4 ? "IPv4" : "IPv6",
1321 format_ip46_address, &mp->ip,
1322 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1323 clib_net_to_host_u16 (mp->port), session->vpp_event_queue);
1325 if (VPPCOM_DEBUG > 0)
1327 session->elog_track.name =
1328 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
1330 elog_track_register (&vcm->elog_main, &session->elog_track);
1332 if (session->peer_addr.is_ip4)
1335 ELOG_TYPE_DECLARE (e) =
1338 "client_accept:handle:%x addr:%d.%d.%d.%d:%d",
1339 .format_args = "i8i1i1i1i1i2",
1342 CLIB_PACKED (struct {
1348 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1350 ed->handle = mp->handle;
1351 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
1352 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
1353 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
1354 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
1355 ed->port = clib_net_to_host_u16 (session->peer_port);
1360 clib_warning ("ip6");
1364 VCL_SESSION_UNLOCK ();
1368 /* VPP combines bind and listen as one operation. VCL manages the separation
1369 * of bind and listen locally via vppcom_session_bind() and
1370 * vppcom_session_listen() */
1372 vppcom_send_bind_sock (session_t * session, u32 session_index)
1374 vl_api_bind_sock_t *bmp;
1376 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1377 bmp = vl_msg_api_alloc (sizeof (*bmp));
1378 memset (bmp, 0, sizeof (*bmp));
1380 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1381 bmp->client_index = vcm->my_client_index;
1382 bmp->context = session_index;
1383 bmp->is_ip4 = session->lcl_addr.is_ip4;
1384 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1385 bmp->port = session->lcl_port;
1386 bmp->proto = session->proto;
1387 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1388 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1392 vppcom_send_unbind_sock (u64 vpp_handle)
1394 vl_api_unbind_sock_t *ump;
1396 ump = vl_msg_api_alloc (sizeof (*ump));
1397 memset (ump, 0, sizeof (*ump));
1399 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1400 ump->client_index = vcm->my_client_index;
1401 ump->handle = vpp_handle;
1402 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1406 vppcom_session_unbind (u32 session_index)
1408 session_t *session = 0;
1411 elog_track_t session_elog_track;
1413 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1415 vpp_handle = session->vpp_handle;
1416 vppcom_session_table_del_listener (vpp_handle);
1417 session->vpp_handle = ~0;
1418 session->state = STATE_DISCONNECT;
1419 session_elog_track = session->elog_track;
1421 VCL_SESSION_UNLOCK ();
1423 if (VPPCOM_DEBUG > 1)
1424 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1425 "sending unbind msg! new state 0x%x (%s)",
1426 getpid (), vpp_handle, session_index,
1428 vppcom_session_state_str (STATE_DISCONNECT));
1430 if (VPPCOM_DEBUG > 0)
1433 ELOG_TYPE_DECLARE (e) =
1435 .format = "unbind: handle:%x",
1436 .format_args = "i8",
1444 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
1445 ed->handle = vpp_handle;
1449 vppcom_send_unbind_sock (vpp_handle);
1456 vppcom_session_disconnect (u32 session_index)
1461 session_state_t state;
1463 VCL_SESSION_LOCK_AND_GET (session_index, &session);
1465 vpp_handle = session->vpp_handle;
1466 state = session->state;
1467 VCL_SESSION_UNLOCK ();
1469 if (VPPCOM_DEBUG > 1)
1471 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u "
1473 getpid (), vpp_handle, session_index,
1474 state, vppcom_session_state_str (state));
1477 if (PREDICT_FALSE (state & STATE_LISTEN))
1479 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1480 "Cannot disconnect a listen socket!",
1481 getpid (), vpp_handle, session_index);
1486 /* The peer has already initiated the close,
1487 * so send the disconnect session reply.
1489 if (state & STATE_CLOSE_ON_EMPTY)
1491 //XXX alagalah - Check and drain here?
1492 vppcom_send_disconnect_session_reply (vpp_handle,
1493 session_index, 0 /* rv */ );
1494 if (VPPCOM_DEBUG > 1)
1495 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1496 "sending disconnect REPLY...",
1497 getpid (), vpp_handle, session_index);
1500 /* Otherwise, send a disconnect session msg...
1504 if (VPPCOM_DEBUG > 1)
1505 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1506 "sending disconnect...",
1507 getpid (), vpp_handle, session_index);
1509 vppcom_send_disconnect_session (vpp_handle, session_index);
1516 #define foreach_sock_msg \
1517 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1518 _(BIND_SOCK_REPLY, bind_sock_reply) \
1519 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1520 _(ACCEPT_SESSION, accept_session) \
1521 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1522 _(DISCONNECT_SESSION, disconnect_session) \
1523 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1524 _(RESET_SESSION, reset_session) \
1525 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1526 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1527 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
1528 _(UNMAP_SEGMENT, unmap_segment)
1531 vppcom_api_hookup (void)
1534 vl_msg_api_set_handlers(VL_API_##N, #n, \
1535 vl_api_##n##_t_handler, \
1537 vl_api_##n##_t_endian, \
1538 vl_api_##n##_t_print, \
1539 sizeof(vl_api_##n##_t), 1);
1545 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1549 vcl_cfg->heapsize = (256ULL << 20);
1550 vcl_cfg->vpp_api_q_length = 1024;
1551 vcl_cfg->segment_baseva = 0x200000000ULL;
1552 vcl_cfg->segment_size = (256 << 20);
1553 vcl_cfg->add_segment_size = (128 << 20);
1554 vcl_cfg->preallocated_fifo_pairs = 8;
1555 vcl_cfg->rx_fifo_size = (1 << 20);
1556 vcl_cfg->tx_fifo_size = (1 << 20);
1557 vcl_cfg->event_queue_size = 2048;
1558 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1559 vcl_cfg->app_timeout = 10 * 60.0;
1560 vcl_cfg->session_timeout = 10 * 60.0;
1561 vcl_cfg->accept_timeout = 60.0;
1562 vcl_cfg->event_ring_size = (128 << 10);
1563 vcl_cfg->event_log_path = "/dev/shm";
1567 vppcom_cfg_heapsize (char *conf_fname)
1569 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1582 fp = fopen (conf_fname, "r");
1585 if (VPPCOM_DEBUG > 0)
1586 clib_warning ("VCL<%d>: using default heapsize %lld (0x%llx)",
1587 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1591 argv = calloc (1, sizeof (char *));
1594 if (VPPCOM_DEBUG > 0)
1595 clib_warning ("VCL<%d>: calloc failed, using default "
1596 "heapsize %lld (0x%llx)",
1597 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1603 if (fgets (inbuf, 4096, fp) == 0)
1605 p = strtok (inbuf, " \t\n");
1611 char **tmp = realloc (argv, argc * sizeof (char *));
1614 if (VPPCOM_DEBUG > 0)
1615 clib_warning ("VCL<%d>: realloc failed, "
1616 "using default heapsize %lld (0x%llx)",
1617 getpid (), vcl_cfg->heapsize,
1622 arg = strndup (p, 1024);
1625 if (VPPCOM_DEBUG > 0)
1626 clib_warning ("VCL<%d>: strndup failed, "
1627 "using default heapsize %lld (0x%llx)",
1628 getpid (), vcl_cfg->heapsize,
1632 argv[argc - 1] = arg;
1633 p = strtok (NULL, " \t\n");
1640 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1643 if (VPPCOM_DEBUG > 0)
1644 clib_warning ("VCL<%d>: realloc failed, "
1645 "using default heapsize %lld (0x%llx)",
1646 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1653 * Look for and parse the "heapsize" config parameter.
1654 * Manual since none of the clib infra has been bootstrapped yet.
1656 * Format: heapsize <nn>[mM][gG]
1659 for (i = 1; i < (argc - 1); i++)
1661 if (!strncmp (argv[i], "heapsize", 8))
1663 sizep = (u8 *) argv[i + 1];
1665 while (*sizep >= '0' && *sizep <= '9')
1668 size += *sizep++ - '0';
1672 if (VPPCOM_DEBUG > 0)
1673 clib_warning ("VCL<%d>: parse error '%s %s', "
1674 "using default heapsize %lld (0x%llx)",
1675 getpid (), argv[i], argv[i + 1],
1676 vcl_cfg->heapsize, vcl_cfg->heapsize);
1680 if (*sizep == 'g' || *sizep == 'G')
1681 vcl_cfg->heapsize = size << 30;
1682 else if (*sizep == 'm' || *sizep == 'M')
1683 vcl_cfg->heapsize = size << 20;
1686 if (VPPCOM_DEBUG > 0)
1687 clib_warning ("VCL<%d>: parse error '%s %s', "
1688 "using default heapsize %lld (0x%llx)",
1689 getpid (), argv[i], argv[i + 1],
1690 vcl_cfg->heapsize, vcl_cfg->heapsize);
1702 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1703 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1704 if (vcl_mem == MAP_FAILED)
1706 clib_unix_error ("VCL<%d>: ERROR: mmap(0, %lld == 0x%llx, "
1707 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1709 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1710 ASSERT (vcl_mem != MAP_FAILED);
1713 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1716 clib_warning ("VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
1720 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1723 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
1728 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1731 if (VPPCOM_DEBUG > 0)
1732 clib_warning ("VCL<%d>: allocated VCL heap = %p, size %lld (0x%llx)",
1733 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
1737 vppcom_cfg_read (char *conf_fname)
1739 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1741 unformat_input_t _input, *input = &_input;
1742 unformat_input_t _line_input, *line_input = &_line_input;
1743 u8 vc_cfg_input = 0;
1746 u32 uid, gid, q_len;
1748 fd = open (conf_fname, O_RDONLY);
1751 if (VPPCOM_DEBUG > 0)
1752 clib_warning ("VCL<%d>: using default configuration.",
1753 getpid (), conf_fname);
1757 if (fstat (fd, &s) < 0)
1759 if (VPPCOM_DEBUG > 0)
1760 clib_warning ("VCL<%d>: failed to stat `%s', "
1761 "using default configuration", getpid (), conf_fname);
1765 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1767 if (VPPCOM_DEBUG > 0)
1768 clib_warning ("VCL<%d>: not a regular file `%s', "
1769 "using default configuration", getpid (), conf_fname);
1773 unformat_init_clib_file (input, fd);
1775 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1777 (void) unformat_user (input, unformat_line_input, line_input);
1778 unformat_skip_white_space (line_input);
1780 if (unformat (line_input, "vcl {"))
1788 if (unformat (line_input, "heapsize %s", &chroot_path))
1790 vec_terminate_c_string (chroot_path);
1791 if (VPPCOM_DEBUG > 0)
1792 clib_warning ("VCL<%d>: configured heapsize %s, "
1793 "actual heapsize %lld (0x%llx)",
1794 getpid (), chroot_path, vcl_cfg->heapsize,
1796 vec_free (chroot_path);
1798 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1800 vec_terminate_c_string (chroot_path);
1801 if (vcl_cfg->vpp_api_filename)
1802 vec_free (vcl_cfg->vpp_api_filename);
1803 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
1805 vl_set_memory_root_path ((char *) chroot_path);
1807 if (VPPCOM_DEBUG > 0)
1808 clib_warning ("VCL<%d>: configured api-prefix (%s) and "
1809 "api filename (%s)", getpid (), chroot_path,
1810 vcl_cfg->vpp_api_filename);
1811 chroot_path = 0; /* Don't vec_free() it! */
1813 else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
1815 if (q_len < vcl_cfg->vpp_api_q_length)
1817 clib_warning ("VCL<%d>: ERROR: configured vpp-api-q-length "
1818 "(%u) is too small! Using default: %u ",
1819 getpid (), q_len, vcl_cfg->vpp_api_q_length);
1823 vcl_cfg->vpp_api_q_length = q_len;
1825 if (VPPCOM_DEBUG > 0)
1826 clib_warning ("VCL<%d>: configured vpp-api-q-length %u",
1827 getpid (), vcl_cfg->vpp_api_q_length);
1830 else if (unformat (line_input, "uid %d", &uid))
1832 vl_set_memory_uid (uid);
1833 if (VPPCOM_DEBUG > 0)
1834 clib_warning ("VCL<%d>: configured uid %d", getpid (), uid);
1836 else if (unformat (line_input, "gid %d", &gid))
1838 vl_set_memory_gid (gid);
1839 if (VPPCOM_DEBUG > 0)
1840 clib_warning ("VCL<%d>: configured gid %d", getpid (), gid);
1842 else if (unformat (line_input, "segment-baseva 0x%lx",
1843 &vcl_cfg->segment_baseva))
1845 if (VPPCOM_DEBUG > 0)
1846 clib_warning ("VCL<%d>: configured segment_baseva 0x%lx",
1847 getpid (), vcl_cfg->segment_baseva);
1849 else if (unformat (line_input, "segment-size 0x%lx",
1850 &vcl_cfg->segment_size))
1852 if (VPPCOM_DEBUG > 0)
1853 clib_warning ("VCL<%d>: configured segment_size 0x%lx (%ld)",
1854 getpid (), vcl_cfg->segment_size,
1855 vcl_cfg->segment_size);
1857 else if (unformat (line_input, "segment-size %ld",
1858 &vcl_cfg->segment_size))
1860 if (VPPCOM_DEBUG > 0)
1861 clib_warning ("VCL<%d>: configured segment_size %ld (0x%lx)",
1862 getpid (), vcl_cfg->segment_size,
1863 vcl_cfg->segment_size);
1865 else if (unformat (line_input, "add-segment-size 0x%lx",
1866 &vcl_cfg->add_segment_size))
1868 if (VPPCOM_DEBUG > 0)
1870 ("VCL<%d>: configured add_segment_size 0x%lx (%ld)",
1871 getpid (), vcl_cfg->add_segment_size,
1872 vcl_cfg->add_segment_size);
1874 else if (unformat (line_input, "add-segment-size %ld",
1875 &vcl_cfg->add_segment_size))
1877 if (VPPCOM_DEBUG > 0)
1879 ("VCL<%d>: configured add_segment_size %ld (0x%lx)",
1880 getpid (), vcl_cfg->add_segment_size,
1881 vcl_cfg->add_segment_size);
1883 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1884 &vcl_cfg->preallocated_fifo_pairs))
1886 if (VPPCOM_DEBUG > 0)
1887 clib_warning ("VCL<%d>: configured preallocated_fifo_pairs "
1888 "%d (0x%x)", getpid (),
1889 vcl_cfg->preallocated_fifo_pairs,
1890 vcl_cfg->preallocated_fifo_pairs);
1892 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1893 &vcl_cfg->rx_fifo_size))
1895 if (VPPCOM_DEBUG > 0)
1896 clib_warning ("VCL<%d>: configured rx_fifo_size 0x%lx (%ld)",
1897 getpid (), vcl_cfg->rx_fifo_size,
1898 vcl_cfg->rx_fifo_size);
1900 else if (unformat (line_input, "rx-fifo-size %ld",
1901 &vcl_cfg->rx_fifo_size))
1903 if (VPPCOM_DEBUG > 0)
1904 clib_warning ("VCL<%d>: configured rx_fifo_size %ld (0x%lx)",
1905 getpid (), vcl_cfg->rx_fifo_size,
1906 vcl_cfg->rx_fifo_size);
1908 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1909 &vcl_cfg->tx_fifo_size))
1911 if (VPPCOM_DEBUG > 0)
1912 clib_warning ("VCL<%d>: configured tx_fifo_size 0x%lx (%ld)",
1913 getpid (), vcl_cfg->tx_fifo_size,
1914 vcl_cfg->tx_fifo_size);
1916 else if (unformat (line_input, "tx-fifo-size %ld",
1917 &vcl_cfg->tx_fifo_size))
1919 if (VPPCOM_DEBUG > 0)
1920 clib_warning ("VCL<%d>: configured tx_fifo_size %ld (0x%lx)",
1921 getpid (), vcl_cfg->tx_fifo_size,
1922 vcl_cfg->tx_fifo_size);
1924 else if (unformat (line_input, "event-queue-size 0x%lx",
1925 &vcl_cfg->event_queue_size))
1927 if (VPPCOM_DEBUG > 0)
1928 clib_warning ("VCL<%d>: configured event_queue_size "
1930 getpid (), vcl_cfg->event_queue_size,
1931 vcl_cfg->event_queue_size);
1933 else if (unformat (line_input, "event-queue-size %ld",
1934 &vcl_cfg->event_queue_size))
1936 if (VPPCOM_DEBUG > 0)
1937 clib_warning ("VCL<%d>: configured event_queue_size "
1939 getpid (), vcl_cfg->event_queue_size,
1940 vcl_cfg->event_queue_size);
1942 else if (unformat (line_input, "listen-queue-size 0x%lx",
1943 &vcl_cfg->listen_queue_size))
1945 if (VPPCOM_DEBUG > 0)
1946 clib_warning ("VCL<%d>: configured listen_queue_size "
1948 getpid (), vcl_cfg->listen_queue_size,
1949 vcl_cfg->listen_queue_size);
1951 else if (unformat (line_input, "listen-queue-size %ld",
1952 &vcl_cfg->listen_queue_size))
1954 if (VPPCOM_DEBUG > 0)
1955 clib_warning ("VCL<%d>: configured listen_queue_size "
1957 getpid (), vcl_cfg->listen_queue_size,
1958 vcl_cfg->listen_queue_size);
1960 else if (unformat (line_input, "app-timeout %f",
1961 &vcl_cfg->app_timeout))
1963 if (VPPCOM_DEBUG > 0)
1964 clib_warning ("VCL<%d>: configured app_timeout %f",
1965 getpid (), vcl_cfg->app_timeout);
1967 else if (unformat (line_input, "session-timeout %f",
1968 &vcl_cfg->session_timeout))
1970 if (VPPCOM_DEBUG > 0)
1971 clib_warning ("VCL<%d>: configured session_timeout %f",
1972 getpid (), vcl_cfg->session_timeout);
1974 else if (unformat (line_input, "accept-timeout %f",
1975 &vcl_cfg->accept_timeout))
1977 if (VPPCOM_DEBUG > 0)
1978 clib_warning ("VCL<%d>: configured accept_timeout %f",
1979 getpid (), vcl_cfg->accept_timeout);
1981 else if (unformat (line_input, "app-proxy-transport-tcp"))
1983 vcl_cfg->app_proxy_transport_tcp = 1;
1984 if (VPPCOM_DEBUG > 0)
1985 clib_warning ("VCL<%d>: configured "
1986 "app_proxy_transport_tcp (%d)",
1987 getpid (), vcl_cfg->app_proxy_transport_tcp);
1989 else if (unformat (line_input, "app-proxy-transport-udp"))
1991 vcl_cfg->app_proxy_transport_udp = 1;
1992 if (VPPCOM_DEBUG > 0)
1993 clib_warning ("VCL<%d>: configured "
1994 "app_proxy_transport_udp (%d)",
1995 getpid (), vcl_cfg->app_proxy_transport_udp);
1997 else if (unformat (line_input, "app-scope-local"))
1999 vcl_cfg->app_scope_local = 1;
2000 if (VPPCOM_DEBUG > 0)
2001 clib_warning ("VCL<%d>: configured app_scope_local (%d)",
2002 getpid (), vcl_cfg->app_scope_local);
2004 else if (unformat (line_input, "app-scope-global"))
2006 vcl_cfg->app_scope_global = 1;
2007 if (VPPCOM_DEBUG > 0)
2008 clib_warning ("VCL<%d>: configured app_scope_global (%d)",
2009 getpid (), vcl_cfg->app_scope_global);
2011 else if (unformat (line_input, "namespace-secret %lu",
2012 &vcl_cfg->namespace_secret))
2014 if (VPPCOM_DEBUG > 0)
2016 ("VCL<%d>: configured namespace_secret %lu (0x%lx)",
2017 getpid (), vcl_cfg->namespace_secret,
2018 vcl_cfg->namespace_secret);
2020 else if (unformat (line_input, "namespace-id %v",
2021 &vcl_cfg->namespace_id))
2023 vl_api_application_attach_t *mp;
2024 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
2025 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
2026 if (nsid_vec_len > max_nsid_vec_len)
2028 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
2029 if (VPPCOM_DEBUG > 0)
2030 clib_warning ("VCL<%d>: configured namespace_id is "
2031 "too long, truncated to %d characters!",
2032 getpid (), max_nsid_vec_len);
2035 if (VPPCOM_DEBUG > 0)
2036 clib_warning ("VCL<%d>: configured namespace_id %v",
2037 getpid (), vcl_cfg->namespace_id);
2039 else if (unformat (line_input, "}"))
2042 if (VPPCOM_DEBUG > 0)
2043 clib_warning ("VCL<%d>: completed parsing vppcom config!",
2049 if (line_input->buffer[line_input->index] != '#')
2051 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
2053 &line_input->buffer[line_input->index]);
2060 unformat_free (input);
2068 * VPPCOM Public API functions
2071 vppcom_app_create (char *app_name)
2073 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
2084 vppcom_cfg_init (vcl_cfg);
2085 env_var_str = getenv (VPPCOM_ENV_DEBUG);
2089 if (sscanf (env_var_str, "%u", &tmp) != 1)
2090 clib_warning ("VCL<%d>: WARNING: Invalid debug level specified "
2091 "in the environment variable " VPPCOM_ENV_DEBUG
2092 " (%s)!\n", getpid (), env_var_str);
2096 if (VPPCOM_DEBUG > 0)
2097 clib_warning ("VCL<%d>: configured VCL debug level (%u) from "
2098 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
2101 conf_fname = getenv (VPPCOM_ENV_CONF);
2103 conf_fname = VPPCOM_CONF_DEFAULT;
2104 vppcom_cfg_heapsize (conf_fname);
2105 vcl_cfg = &vcm->cfg;
2106 clib_spinlock_init (&vcm->session_fifo_lockp);
2107 clib_fifo_validate (vcm->client_session_index_fifo,
2108 vcm->cfg.listen_queue_size);
2109 vppcom_cfg_read (conf_fname);
2112 env_var_str = getenv (VPPCOM_ENV_API_PREFIX);
2115 if (vcl_cfg->vpp_api_filename)
2116 vec_free (vcl_cfg->vpp_api_filename);
2117 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
2119 vl_set_memory_root_path ((char *) env_var_str);
2121 if (VPPCOM_DEBUG > 0)
2122 clib_warning ("VCL<%d>: configured api prefix (%s) and "
2123 "filename (%s) from " VPPCOM_ENV_API_PREFIX "!",
2124 getpid (), env_var_str, vcl_cfg->vpp_api_filename);
2126 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
2129 u32 ns_id_vec_len = strlen (env_var_str);
2131 vec_reset_length (vcm->cfg.namespace_id);
2132 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
2133 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
2135 if (VPPCOM_DEBUG > 0)
2136 clib_warning ("VCL<%d>: configured namespace_id (%v) from "
2137 VPPCOM_ENV_APP_NAMESPACE_ID
2138 "!", getpid (), vcm->cfg.namespace_id);
2140 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
2144 if (sscanf (env_var_str, "%lu", &tmp) != 1)
2145 clib_warning ("VCL<%d>: WARNING: Invalid namespace secret "
2146 "specified in the environment variable "
2147 VPPCOM_ENV_APP_NAMESPACE_SECRET
2148 " (%s)!\n", getpid (), env_var_str);
2151 vcm->cfg.namespace_secret = tmp;
2152 if (VPPCOM_DEBUG > 0)
2153 clib_warning ("VCL<%d>: configured namespace secret "
2155 VPPCOM_ENV_APP_NAMESPACE_SECRET
2156 "!", getpid (), vcm->cfg.namespace_secret);
2159 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
2161 vcm->cfg.app_proxy_transport_tcp = 1;
2162 if (VPPCOM_DEBUG > 0)
2163 clib_warning ("VCL<%d>: configured app_proxy_transport_tcp "
2165 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP
2166 "!", getpid (), vcm->cfg.app_proxy_transport_tcp);
2168 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
2170 vcm->cfg.app_proxy_transport_udp = 1;
2171 if (VPPCOM_DEBUG > 0)
2172 clib_warning ("VCL<%d>: configured app_proxy_transport_udp "
2174 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP
2175 "!", getpid (), vcm->cfg.app_proxy_transport_udp);
2177 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
2179 vcm->cfg.app_scope_local = 1;
2180 if (VPPCOM_DEBUG > 0)
2181 clib_warning ("VCL<%d>: configured app_scope_local (%u) from "
2182 VPPCOM_ENV_APP_SCOPE_LOCAL
2183 "!", getpid (), vcm->cfg.app_scope_local);
2185 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
2187 vcm->cfg.app_scope_global = 1;
2188 if (VPPCOM_DEBUG > 0)
2189 clib_warning ("VCL<%d>: configured app_scope_global (%u) from "
2190 VPPCOM_ENV_APP_SCOPE_GLOBAL
2191 "!", getpid (), vcm->cfg.app_scope_global);
2194 vcm->main_cpu = os_get_thread_index ();
2195 heap = clib_mem_get_per_cpu_heap ();
2196 h = mheap_header (heap);
2198 /* make the main heap thread-safe */
2199 h->flags |= MHEAP_FLAG_THREAD_SAFE;
2201 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
2203 clib_time_init (&vcm->clib_time);
2204 vppcom_init_error_string_table ();
2205 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
2206 20 /* timeout in secs */ );
2207 clib_spinlock_init (&vcm->sessions_lockp);
2210 if (vcm->my_client_index == ~0)
2213 /* API hookup and connect to VPP */
2214 vppcom_api_hookup ();
2215 vcm->app_state = STATE_APP_START;
2216 rv = vppcom_connect_to_vpp (app_name);
2219 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
2224 /* State event handling thread */
2226 rv = vce_start_event_thread (&(vcm->event_thread), 20);
2228 if (VPPCOM_DEBUG > 0)
2229 clib_warning ("VCL<%d>: sending session enable", getpid ());
2231 rv = vppcom_app_session_enable ();
2234 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
2235 "failed!", getpid ());
2239 if (VPPCOM_DEBUG > 0)
2240 clib_warning ("VCL<%d>: sending app attach", getpid ());
2242 rv = vppcom_app_attach ();
2245 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
2250 if (VPPCOM_DEBUG > 0)
2251 clib_warning ("VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
2252 getpid (), app_name, vcm->my_client_index,
2253 vcm->my_client_index);
2260 vppcom_app_destroy (void)
2263 f64 orig_app_timeout;
2265 if (vcm->my_client_index == ~0)
2268 if (VPPCOM_DEBUG > 0)
2269 clib_warning ("VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
2270 getpid (), vcm->my_client_index, vcm->my_client_index);
2272 if (VPPCOM_DEBUG > 0)
2275 ELOG_TYPE_DECLARE (e) =
2277 .format = "app_detach:C:%d",
2278 .format_args = "i4",
2285 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
2286 ed->data = vcm->my_client_index;
2290 vppcom_app_detach ();
2291 orig_app_timeout = vcm->cfg.app_timeout;
2292 vcm->cfg.app_timeout = 2.0;
2293 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
2294 vcm->cfg.app_timeout = orig_app_timeout;
2295 if (PREDICT_FALSE (rv))
2297 if (VPPCOM_DEBUG > 0)
2298 clib_warning ("VCL<%d>: application detach timed out! "
2299 "returning %d (%s)",
2300 getpid (), rv, vppcom_retval_str (rv));
2303 /* Finished with logging before client gets reset to ~0 */
2304 if (VPPCOM_DEBUG > 0)
2307 vl_client_disconnect_from_vlib ();
2308 vcm->my_client_index = ~0;
2309 vcm->app_state = STATE_APP_START;
2313 vppcom_session_create (u8 proto, u8 is_nonblocking)
2317 session_state_t state;
2318 elog_track_t session_elog_track;
2320 VCL_SESSION_LOCK ();
2321 pool_get (vcm->sessions, session);
2322 memset (session, 0, sizeof (*session));
2323 session_index = session - vcm->sessions;
2325 session->proto = proto;
2326 session->state = STATE_START;
2327 state = session->state;
2328 session->vpp_handle = ~0;
2331 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2333 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2335 if (VPPCOM_DEBUG > 0)
2337 session->elog_track.name =
2338 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2340 elog_track_register (&vcm->elog_main, &session->elog_track);
2341 session_elog_track = session->elog_track;
2344 VCL_SESSION_UNLOCK ();
2346 if (VPPCOM_DEBUG > 0)
2347 clib_warning ("VCL<%d>: sid %u", getpid (), session_index);
2349 if (VPPCOM_DEBUG > 0)
2352 ELOG_TYPE_DECLARE (e) =
2354 .format = "session_create:proto:%d state:%d is_nonblocking:%d",
2355 .format_args = "i4i4i4",
2363 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
2364 ed->data[0] = proto;
2365 ed->data[1] = state;
2366 ed->data[2] = is_nonblocking;
2370 return (int) session_index;
2374 vppcom_session_close (uint32_t session_index)
2376 session_t *session = 0;
2384 session_state_t state;
2385 elog_track_t session_elog_track;
2387 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2388 is_vep = session->is_vep;
2389 is_vep_session = session->is_vep_session;
2390 next_sid = session->vep.next_sid;
2391 vep_idx = session->vep.vep_idx;
2392 state = session->state;
2393 vpp_handle = session->vpp_handle;
2394 VCL_SESSION_UNLOCK ();
2397 * Why two if(VPPCOM_DEBUG) checks?
2399 * Eventually all clib_warnings need their own way of being
2400 * logged and signalled (like severity) where event logging
2401 * is a separate debugging tool. It will make the separation
2402 * easier. ... parting is such sweet sorrow ...
2404 if (VPPCOM_DEBUG > 0)
2406 session_elog_track = session->elog_track;
2409 if (VPPCOM_DEBUG > 0)
2412 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
2413 "closing epoll session...",
2414 getpid (), session_index, session_index);
2416 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
2417 "closing session...",
2418 getpid (), vpp_handle, session_index);
2423 while (next_sid != ~0)
2425 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
2426 if ((VPPCOM_DEBUG > 0) && PREDICT_FALSE (rv < 0))
2427 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2428 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2429 getpid (), vpp_handle, next_sid, vep_idx,
2430 rv, vppcom_retval_str (rv));
2432 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2433 next_sid = session->vep.next_sid;
2434 VCL_SESSION_UNLOCK ();
2441 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2442 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2443 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2444 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2445 getpid (), vpp_handle, session_index,
2446 vep_idx, rv, vppcom_retval_str (rv));
2449 if (state & STATE_LISTEN)
2451 rv = vppcom_session_unbind (session_index);
2452 if (PREDICT_FALSE (rv < 0))
2454 if (VPPCOM_DEBUG > 0)
2455 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2456 "listener unbind failed! rv %d (%s)",
2457 getpid (), vpp_handle, session_index,
2458 rv, vppcom_retval_str (rv));
2462 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
2464 rv = vppcom_session_disconnect (session_index);
2465 if (PREDICT_FALSE (rv < 0))
2466 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2467 "session disconnect failed! rv %d (%s)",
2468 getpid (), vpp_handle, session_index,
2469 rv, vppcom_retval_str (rv));
2473 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2474 vpp_handle = session->vpp_handle;
2475 if (vpp_handle != ~0)
2477 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
2479 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
2481 pool_put_index (vcm->sessions, session_index);
2483 VCL_SESSION_UNLOCK ();
2485 if (VPPCOM_DEBUG > 0)
2488 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
2489 getpid (), session_index, session_index);
2491 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
2492 getpid (), vpp_handle, session_index);
2496 if (VPPCOM_DEBUG > 0)
2499 ELOG_TYPE_DECLARE (e) =
2501 .format = "session_close:rv:%d",
2502 .format_args = "i4",
2510 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
2519 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2521 session_t *session = 0;
2525 return VPPCOM_EINVAL;
2527 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2529 if (session->is_vep)
2531 VCL_SESSION_UNLOCK ();
2532 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2533 "bind to an epoll session!", getpid (), session_index);
2538 session->lcl_addr.is_ip4 = ep->is_ip4;
2539 session->lcl_addr.ip46 = to_ip46 (ep->is_ip4 ? IP46_TYPE_IP4 :
2540 IP46_TYPE_IP6, ep->ip);
2541 session->lcl_port = ep->port;
2543 if (VPPCOM_DEBUG > 0)
2544 clib_warning ("VCL<%d>: sid %u: binding to local %s address %U "
2545 "port %u, proto %s", getpid (), session_index,
2546 session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2547 format_ip46_address, &session->lcl_addr.ip46,
2548 session->lcl_addr.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2549 clib_net_to_host_u16 (session->lcl_port),
2550 session->proto ? "UDP" : "TCP");
2552 if (VPPCOM_DEBUG > 0)
2554 if (session->lcl_addr.is_ip4)
2557 ELOG_TYPE_DECLARE (e) =
2559 .format = "bind local:%s:%d.%d.%d.%d:%d ",
2560 .format_args = "t1i1i1i1i1i2",
2561 .n_enum_strings = 2,
2562 .enum_strings = {"TCP", "UDP",},
2565 CLIB_PACKED (struct {
2571 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2572 ed->proto = session->proto;
2573 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
2574 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
2575 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
2576 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
2577 ed->port = clib_net_to_host_u16 (session->lcl_port);
2582 VCL_SESSION_UNLOCK ();
2588 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2590 session_t *listen_session = 0;
2591 u64 listen_vpp_handle;
2594 if (q_len == 0 || q_len == ~0)
2595 q_len = vcm->cfg.listen_queue_size;
2597 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
2599 if (listen_session->is_vep)
2601 VCL_SESSION_UNLOCK ();
2602 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
2603 "epoll session!", getpid (), listen_session_index);
2608 listen_vpp_handle = listen_session->vpp_handle;
2609 if (listen_session->state & STATE_LISTEN)
2611 VCL_SESSION_UNLOCK ();
2612 if (VPPCOM_DEBUG > 0)
2613 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2614 "already in listen state!",
2615 getpid (), listen_vpp_handle, listen_session_index);
2620 if (VPPCOM_DEBUG > 0)
2621 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
2622 "sid %u: sending VPP bind+listen request...",
2623 getpid (), listen_vpp_handle, listen_session_index);
2625 vppcom_send_bind_sock (listen_session, listen_session_index);
2626 VCL_SESSION_UNLOCK ();
2628 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2629 vcm->cfg.session_timeout);
2631 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
2632 if (PREDICT_FALSE (retval))
2634 if (VPPCOM_DEBUG > 0)
2636 ("VCL<%d>: vpp handle 0x%llx, sid %u: bind+listen failed! "
2637 "returning %d (%s)", getpid (), listen_session->vpp_handle,
2638 listen_session_index, retval, vppcom_retval_str (retval));
2639 VCL_SESSION_UNLOCK ();
2644 VCL_ACCEPT_FIFO_LOCK ();
2645 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2646 VCL_ACCEPT_FIFO_UNLOCK ();
2648 VCL_SESSION_UNLOCK ();
2655 validate_args_session_accept_ (session_t * listen_session)
2657 u32 listen_session_index = listen_session - vcm->sessions;
2659 /* Input validation - expects spinlock on sessions_lockp */
2660 if (listen_session->is_vep)
2662 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
2663 "epoll session!", getpid (), listen_session_index);
2664 return VPPCOM_EBADFD;
2667 if (listen_session->state != STATE_LISTEN)
2669 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2670 "not in listen state! state 0x%x (%s)", getpid (),
2671 listen_session->vpp_handle, listen_session_index,
2672 listen_session->state,
2673 vppcom_session_state_str (listen_session->state));
2674 return VPPCOM_EBADFD;
2680 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2683 session_t *listen_session = 0;
2684 session_t *client_session = 0;
2685 u32 client_session_index = ~0;
2687 u64 listen_vpp_handle;
2688 vce_event_handler_reg_t *reg;
2690 vce_event_connect_request_t *result;
2693 int millisecond_timeout = 1;
2694 int hours_timeout = 20 * 60 * 60;
2696 VCL_SESSION_LOCK_AND_GET (listen_session_index, &listen_session);
2697 listen_vpp_handle = listen_session->vpp_handle; // For debugging
2699 rv = validate_args_session_accept_ (listen_session);
2702 VCL_SESSION_UNLOCK ();
2706 /* Using an aggressive timer of 1ms and a generous timer of
2707 * 20 hours, we can implement a blocking and non-blocking listener
2708 * as both event and time driven */
2709 gettimeofday (&tv, NULL);
2710 ts.tv_nsec = (tv.tv_usec * 1000) + (1000 * millisecond_timeout);
2711 ts.tv_sec = tv.tv_sec;
2713 /* Predict that the Listener is blocking more often than not */
2714 if (PREDICT_TRUE (!VCL_SESS_ATTR_TEST (listen_session->attr,
2715 VCL_SESS_ATTR_NONBLOCK)))
2716 ts.tv_sec += hours_timeout;
2718 VCL_SESSION_UNLOCK ();
2720 /* Register handler for connect_request event on listen_session_index */
2721 vce_event_key_t evk;
2722 evk.session_index = listen_session_index;
2723 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
2724 reg = vce_register_handler (&vcm->event_thread, &evk,
2725 vce_connect_request_handler_fn, 0);
2727 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
2728 pthread_mutex_lock (®->handler_lock);
2731 VCL_EVENTS_UNLOCK ();
2732 rv = pthread_cond_timedwait (®->handler_cond,
2733 ®->handler_lock, &ts);
2734 if (rv == ETIMEDOUT)
2740 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
2742 result = vce_get_event_data (ev, sizeof (*result));
2743 client_session_index = result->accepted_session_index;
2744 VCL_EVENTS_UNLOCK ();
2746 /* Remove from the FIFO used to service epoll */
2747 VCL_ACCEPT_FIFO_LOCK ();
2748 if (clib_fifo_elts (vcm->client_session_index_fifo))
2750 u32 tmp_client_session_index;
2751 clib_fifo_sub1 (vcm->client_session_index_fifo,
2752 tmp_client_session_index);
2753 /* It wasn't ours... put it back ... */
2754 if (tmp_client_session_index != client_session_index)
2755 clib_fifo_add1 (vcm->client_session_index_fifo,
2756 tmp_client_session_index);
2758 VCL_ACCEPT_FIFO_UNLOCK ();
2760 VCL_SESSION_LOCK ();
2762 rv = vppcom_session_at_index (client_session_index, &client_session);
2763 if (PREDICT_FALSE (rv))
2765 rv = VPPCOM_ECONNABORTED;
2766 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
2767 "lookup failed! returning %d (%s)", getpid (),
2768 listen_vpp_handle, listen_session_index,
2769 client_session_index, rv, vppcom_retval_str (rv));
2773 if (flags & O_NONBLOCK)
2774 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2776 VCL_SESS_ATTR_CLR (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2778 if (VPPCOM_DEBUG > 0)
2779 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
2780 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
2781 getpid (), listen_vpp_handle, listen_session_index,
2782 client_session->vpp_handle, client_session_index,
2783 flags, VCL_SESS_ATTR_TEST (client_session->attr,
2784 VCL_SESS_ATTR_NONBLOCK));
2788 ep->is_ip4 = client_session->peer_addr.is_ip4;
2789 ep->port = client_session->peer_port;
2790 if (client_session->peer_addr.is_ip4)
2791 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2792 sizeof (ip4_address_t));
2794 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2795 sizeof (ip6_address_t));
2798 vppcom_send_accept_session_reply (client_session->vpp_handle,
2799 client_session->client_context,
2800 0 /* retval OK */ );
2802 if (VPPCOM_DEBUG > 0)
2803 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle "
2804 "0x%llx, sid %u connection from peer %s address %U port %u "
2805 "to local %s address %U port %u",
2806 getpid (), listen_vpp_handle,
2807 listen_session_index, client_session->vpp_handle,
2808 client_session_index,
2809 client_session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2810 format_ip46_address, &client_session->peer_addr.ip46,
2811 client_session->peer_addr.is_ip4 ?
2812 IP46_TYPE_IP4 : IP46_TYPE_IP6,
2813 clib_net_to_host_u16 (client_session->peer_port),
2814 client_session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2815 format_ip46_address, &client_session->lcl_addr.ip46,
2816 client_session->lcl_addr.is_ip4 ?
2817 IP46_TYPE_IP4 : IP46_TYPE_IP6,
2818 clib_net_to_host_u16 (client_session->lcl_port));
2820 if (VPPCOM_DEBUG > 0)
2822 client_session->elog_track.name =
2823 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2824 client_session_index, 0);
2825 elog_track_register (&vcm->elog_main, &client_session->elog_track);
2827 // Two elog entries due to 20-byte per entry constraint.
2829 ELOG_TYPE_DECLARE (e) =
2831 .format = "accept: listen_handle:%x from_handle:%x",
2832 .format_args = "i8i8",
2840 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, client_session->elog_track);
2841 ed->handle[0] = listen_vpp_handle;
2842 ed->handle[1] = client_session->vpp_handle;
2845 if (client_session->lcl_addr.is_ip4)
2848 ELOG_TYPE_DECLARE (e2) =
2850 .format = "accept: S:%d %d.%d.%d.%d:%d ",
2851 .format_args = "i4i1i1i1i1i2",
2854 CLIB_PACKED (struct {
2861 ELOG_TRACK_DATA (&vcm->elog_main, e2, client_session->elog_track);
2862 ed2->session = client_session_index;
2863 ed2->addr[0] = client_session->lcl_addr.ip46.ip4.as_u8[0];
2864 ed2->addr[1] = client_session->lcl_addr.ip46.ip4.as_u8[1];
2865 ed2->addr[2] = client_session->lcl_addr.ip46.ip4.as_u8[2];
2866 ed2->addr[3] = client_session->lcl_addr.ip46.ip4.as_u8[3];
2867 ed2->port = clib_net_to_host_u16 (client_session->lcl_port);
2872 VCL_SESSION_UNLOCK ();
2874 rv = (int) client_session_index;
2875 vce_clear_event (&vcm->event_thread, reg->ev_idx);
2876 if (vcm->session_io_thread.io_sessions_lockp)
2878 /* Throw this new accepted session index into the rx poll thread pool */
2879 VCL_IO_SESSIONS_LOCK ();
2880 u32 *active_session_index;
2881 pool_get (vcm->session_io_thread.active_session_indexes,
2882 active_session_index);
2883 *active_session_index = client_session_index;
2884 VCL_IO_SESSIONS_UNLOCK ();
2887 vce_unregister_handler (&vcm->event_thread, reg);
2888 pthread_mutex_unlock (®->handler_lock);
2895 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2897 session_t *session = 0;
2899 int rv, retval = VPPCOM_OK;
2901 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2903 if (PREDICT_FALSE (session->is_vep))
2905 VCL_SESSION_UNLOCK ();
2906 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2907 "connect on an epoll session!", getpid (), session_index);
2912 if (PREDICT_FALSE (session->state & CLIENT_STATE_OPEN))
2914 if (VPPCOM_DEBUG > 0)
2915 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session already "
2916 "connected to %s %U port %d proto %s, state 0x%x (%s)",
2917 getpid (), session->vpp_handle, session_index,
2918 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2919 format_ip46_address,
2920 &session->peer_addr.ip46, session->peer_addr.is_ip4 ?
2921 IP46_TYPE_IP4 : IP46_TYPE_IP6,
2922 clib_net_to_host_u16 (session->peer_port),
2923 session->proto ? "UDP" : "TCP", session->state,
2924 vppcom_session_state_str (session->state));
2926 VCL_SESSION_UNLOCK ();
2930 session->peer_addr.is_ip4 = server_ep->is_ip4;
2931 if (session->peer_addr.is_ip4)
2932 clib_memcpy (&session->peer_addr.ip46.ip4, server_ep->ip,
2933 sizeof (ip4_address_t));
2935 clib_memcpy (&session->peer_addr.ip46.ip6, server_ep->ip,
2936 sizeof (ip6_address_t));
2937 session->peer_port = server_ep->port;
2939 if (VPPCOM_DEBUG > 0)
2940 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server "
2941 "%s %U port %d proto %s",
2942 getpid (), session->vpp_handle, session_index,
2943 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2944 format_ip46_address,
2945 &session->peer_addr.ip46, session->peer_addr.is_ip4 ?
2946 IP46_TYPE_IP4 : IP46_TYPE_IP6,
2947 clib_net_to_host_u16 (session->peer_port),
2948 session->proto ? "UDP" : "TCP");
2950 vppcom_send_connect_sock (session, session_index);
2951 VCL_SESSION_UNLOCK ();
2954 vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2955 vcm->cfg.session_timeout);
2957 VCL_SESSION_LOCK_AND_GET (session_index, &session);
2958 vpp_handle = session->vpp_handle;
2959 VCL_SESSION_UNLOCK ();
2962 if (PREDICT_FALSE (retval))
2965 if (VPPCOM_DEBUG > 0)
2969 ("VCL<%d>: vpp handle 0x%llx, sid %u: connect failed! "
2970 "returning %d (%s)", getpid (), vpp_handle,
2971 session_index, rv, vppcom_retval_str (rv));
2973 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
2974 "returning %d (%s)", getpid (),
2975 session_index, rv, vppcom_retval_str (rv));
2978 else if (VPPCOM_DEBUG > 0)
2979 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
2980 getpid (), vpp_handle, session_index);
2986 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2989 session_t *session = 0;
2990 svm_fifo_t *rx_fifo;
2997 session_state_t state;
3001 VCL_SESSION_LOCK_AND_GET (session_index, &session);
3003 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
3004 rx_fifo = session->rx_fifo;
3005 state = session->state;
3006 vpp_handle = session->vpp_handle;
3008 if (PREDICT_FALSE (session->is_vep))
3010 VCL_SESSION_UNLOCK ();
3011 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
3012 "read from an epoll session!", getpid (), session_index);
3017 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
3019 VCL_SESSION_UNLOCK ();
3020 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3022 if (VPPCOM_DEBUG > 0)
3023 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s session is "
3024 "not open! state 0x%x (%s), returning %d (%s)",
3025 getpid (), vpp_handle, session_index, state,
3026 vppcom_session_state_str (state),
3027 rv, vppcom_retval_str (rv));
3031 VCL_SESSION_UNLOCK ();
3036 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
3038 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
3040 while (!is_nonblocking && (n_read <= 0));
3044 VCL_SESSION_LOCK_AND_GET (session_index, &session);
3046 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
3047 (EPOLLET | EPOLLIN));
3049 session->vep.et_mask |= EPOLLIN;
3051 if (state & STATE_CLOSE_ON_EMPTY)
3053 rv = VPPCOM_ECONNRESET;
3055 if (VPPCOM_DEBUG > 1)
3057 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo "
3058 "with session state 0x%x (%s)!"
3059 " Setting state to 0x%x (%s), returning %d (%s)",
3060 getpid (), session->vpp_handle, session_index,
3061 state, vppcom_session_state_str (state),
3063 vppcom_session_state_str (STATE_DISCONNECT), rv,
3064 vppcom_retval_str (rv));
3067 session->state = STATE_DISCONNECT;
3072 VCL_SESSION_UNLOCK ();
3077 if (VPPCOM_DEBUG > 2)
3080 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
3081 "from (%p)", getpid (), vpp_handle,
3082 session_index, n_read, rx_fifo);
3084 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
3085 "returning %d (%s)", getpid (), vpp_handle,
3086 session_index, rv, vppcom_retval_str (rv));
3093 vppcom_session_read (uint32_t session_index, void *buf, size_t n)
3095 return (vppcom_session_read_internal (session_index, buf, n, 0));
3099 vppcom_session_peek (uint32_t session_index, void *buf, int n)
3101 return (vppcom_session_read_internal (session_index, buf, n, 1));
3105 vppcom_session_read_ready (session_t * session, u32 session_index)
3110 session_state_t state = session->state;
3111 u64 vpp_handle = session->vpp_handle;
3113 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3114 if (PREDICT_FALSE (session->is_vep))
3116 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
3117 "epoll session!", getpid (), session_index);
3122 if (session->state & STATE_LISTEN)
3124 VCL_ACCEPT_FIFO_LOCK ();
3125 ready = clib_fifo_elts (vcm->client_session_index_fifo);
3126 VCL_ACCEPT_FIFO_UNLOCK ();
3130 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
3132 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3135 if (VPPCOM_DEBUG > 1)
3136 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session is "
3137 "not open! state 0x%x (%s), returning %d (%s)",
3138 getpid (), vpp_handle, session_index,
3139 state, vppcom_session_state_str (state),
3140 rv, vppcom_retval_str (rv));
3144 ready = svm_fifo_max_dequeue (session->rx_fifo);
3150 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
3152 session->vep.et_mask |= EPOLLIN;
3154 if (state & STATE_CLOSE_ON_EMPTY)
3156 rv = VPPCOM_ECONNRESET;
3158 if (VPPCOM_DEBUG > 1)
3160 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
3161 "sid %u: Empty fifo with"
3162 " session state 0x%x (%s)! Setting state to "
3163 "0x%x (%s), returning %d (%s)",
3164 getpid (), session_index, vpp_handle,
3165 state, vppcom_session_state_str (state),
3167 vppcom_session_state_str (STATE_DISCONNECT), rv,
3168 vppcom_retval_str (rv));
3170 session->state = STATE_DISCONNECT;
3176 if (vcm->app_event_queue->cursize &&
3177 !pthread_mutex_trylock (&vcm->app_event_queue->mutex))
3179 u32 i, n_to_dequeue = vcm->app_event_queue->cursize;
3180 session_fifo_event_t e;
3182 for (i = 0; i < n_to_dequeue; i++)
3183 svm_queue_sub_raw (vcm->app_event_queue, (u8 *) & e);
3185 pthread_mutex_unlock (&vcm->app_event_queue->mutex);
3192 vppcom_session_write (uint32_t session_index, void *buf, size_t n)
3194 session_t *session = 0;
3195 svm_fifo_t *tx_fifo = 0;
3197 session_fifo_event_t evt;
3198 session_state_t state;
3199 int rv, n_write, is_nonblocking;
3205 VCL_SESSION_LOCK_AND_GET (session_index, &session);
3207 tx_fifo = session->tx_fifo;
3208 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
3209 vpp_handle = session->vpp_handle;
3210 state = session->state;
3212 if (PREDICT_FALSE (session->is_vep))
3214 VCL_SESSION_UNLOCK ();
3215 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3216 "cannot write to an epoll session!",
3217 getpid (), vpp_handle, session_index);
3223 if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3226 ((session->state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3229 VCL_SESSION_UNLOCK ();
3230 if (VPPCOM_DEBUG > 1)
3231 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3232 "session is not open! state 0x%x (%s)",
3233 getpid (), vpp_handle, session_index,
3234 state, vppcom_session_state_str (state));
3238 VCL_SESSION_UNLOCK ();
3242 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, (void *) buf);
3244 while (!is_nonblocking && (n_write <= 0));
3246 /* If event wasn't set, add one
3248 * To reduce context switching, can check if an
3249 * event is already there for this event_key, but for now
3250 * this will suffice. */
3252 if ((n_write > 0) && svm_fifo_set_event (tx_fifo))
3254 /* Fabricate TX event, send to vpp */
3256 evt.event_type = FIFO_EVENT_APP_TX;
3258 VCL_SESSION_LOCK_AND_GET (session_index, &session);
3259 q = session->vpp_event_queue;
3261 svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
3262 VCL_SESSION_UNLOCK ();
3263 if (VPPCOM_DEBUG > 1)
3264 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3265 "added FIFO_EVENT_APP_TX to "
3266 "vpp_event_q %p, n_write %d", getpid (),
3267 vpp_handle, session_index, q, n_write);
3272 VCL_SESSION_LOCK_AND_GET (session_index, &session);
3274 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3275 (EPOLLET | EPOLLOUT));
3277 session->vep.et_mask |= EPOLLOUT;
3279 if (session->state & STATE_CLOSE_ON_EMPTY)
3281 rv = VPPCOM_ECONNRESET;
3283 if (VPPCOM_DEBUG > 1)
3285 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3286 "Empty fifo with session state 0x%x (%s)!"
3287 " Setting state to 0x%x (%s), returning %d (%s)",
3288 getpid (), session->vpp_handle, session_index,
3290 vppcom_session_state_str (session->state),
3292 vppcom_session_state_str (STATE_DISCONNECT), rv,
3293 vppcom_retval_str (rv));
3296 session->state = STATE_DISCONNECT;
3301 VCL_SESSION_UNLOCK ();
3306 if (VPPCOM_DEBUG > 2)
3309 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3310 "FIFO-FULL (%p)", getpid (), vpp_handle,
3311 session_index, tx_fifo);
3313 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3314 "wrote %d bytes tx-fifo: (%p)", getpid (),
3315 vpp_handle, session_index, n_write, tx_fifo);
3322 vppcom_session_write_ready (session_t * session, u32 session_index)
3330 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3331 if (PREDICT_FALSE (session->is_vep))
3333 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3334 "cannot write to an epoll session!",
3335 getpid (), session->vpp_handle, session_index);
3340 if (PREDICT_FALSE (session->state & STATE_LISTEN))
3342 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3343 "cannot write to a listen session!",
3344 getpid (), session->vpp_handle, session_index);
3349 if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3351 session_state_t state = session->state;
3353 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3355 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3356 "session is not open! state 0x%x (%s), "
3357 "returning %d (%s)", getpid (), session->vpp_handle,
3359 state, vppcom_session_state_str (state),
3360 rv, vppcom_retval_str (rv));
3364 ready = svm_fifo_max_enqueue (session->tx_fifo);
3366 if (VPPCOM_DEBUG > 3)
3367 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3368 "peek %s (%p), ready = %d", getpid (),
3369 session->vpp_handle, session_index,
3370 session->tx_fifo, ready);
3374 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3375 (EPOLLET | EPOLLOUT));
3377 session->vep.et_mask |= EPOLLOUT;
3379 if (session->state & STATE_CLOSE_ON_EMPTY)
3381 rv = VPPCOM_ECONNRESET;
3383 if (VPPCOM_DEBUG > 1)
3385 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3386 "Empty fifo with session "
3387 "state 0x%x (%s)! Setting state to 0x%x (%s), "
3388 "returning %d (%s)", getpid (),
3389 session->vpp_handle, session_index,
3391 vppcom_session_state_str (session->state),
3393 vppcom_session_state_str (STATE_DISCONNECT), rv,
3394 vppcom_retval_str (rv));
3396 session->state = STATE_DISCONNECT;
3406 vppcom_select (unsigned long n_bits, unsigned long *read_map,
3407 unsigned long *write_map, unsigned long *except_map,
3408 double time_to_wait)
3411 session_t *session = 0;
3412 int rv, bits_set = 0;
3413 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
3414 u32 minbits = clib_max (n_bits, BITS (uword));
3416 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
3418 if (n_bits && read_map)
3420 clib_bitmap_validate (vcm->rd_bitmap, minbits);
3421 clib_memcpy (vcm->rd_bitmap, read_map,
3422 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3423 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3425 if (n_bits && write_map)
3427 clib_bitmap_validate (vcm->wr_bitmap, minbits);
3428 clib_memcpy (vcm->wr_bitmap, write_map,
3429 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3430 memset (write_map, 0,
3431 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3433 if (n_bits && except_map)
3435 clib_bitmap_validate (vcm->ex_bitmap, minbits);
3436 clib_memcpy (vcm->ex_bitmap, except_map,
3437 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3438 memset (except_map, 0,
3439 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3449 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
3452 rv = vppcom_session_at_index (session_index, &session);
3455 VCL_SESSION_UNLOCK();
3456 if (VPPCOM_DEBUG > 1)
3457 clib_warning ("VCL<%d>: session %d specified in "
3458 "read_map is closed.", getpid (),
3460 bits_set = VPPCOM_EBADFD;
3463 if (session->state & STATE_LISTEN)
3465 vce_event_handler_reg_t *reg = 0;
3466 vce_event_key_t evk;
3468 /* Check if handler already registered for this
3470 * If not, register handler for connect_request event
3471 * on listen_session_index
3473 evk.session_index = session_index;
3474 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
3475 reg = vce_get_event_handler (&vcm->event_thread, &evk);
3477 reg = vce_register_handler (&vcm->event_thread, &evk,
3478 vce_poll_wait_connect_request_handler_fn,
3479 0 /* No callback args */);
3480 rv = vppcom_session_read_ready (session, session_index);
3483 vce_unregister_handler (&vcm->event_thread, reg);
3487 rv = vppcom_session_read_ready (session, session_index);
3488 VCL_SESSION_UNLOCK();
3489 if (except_map && vcm->ex_bitmap &&
3490 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3493 clib_bitmap_set_no_check (except_map, session_index, 1);
3498 clib_bitmap_set_no_check (read_map, session_index, 1);
3506 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3509 rv = vppcom_session_at_index (session_index, &session);
3512 VCL_SESSION_UNLOCK();
3513 if (VPPCOM_DEBUG > 0)
3514 clib_warning ("VCL<%d>: session %d specified in "
3515 "write_map is closed.", getpid (),
3517 bits_set = VPPCOM_EBADFD;
3521 rv = vppcom_session_write_ready (session, session_index);
3522 VCL_SESSION_UNLOCK();
3523 if (write_map && (rv > 0))
3525 clib_bitmap_set_no_check (write_map, session_index, 1);
3533 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3536 rv = vppcom_session_at_index (session_index, &session);
3539 VCL_SESSION_UNLOCK();
3540 if (VPPCOM_DEBUG > 1)
3541 clib_warning ("VCL<%d>: session %d specified in "
3542 "except_map is closed.", getpid (),
3544 bits_set = VPPCOM_EBADFD;
3548 rv = vppcom_session_read_ready (session, session_index);
3549 VCL_SESSION_UNLOCK();
3552 clib_bitmap_set_no_check (except_map, session_index, 1);
3560 while ((time_to_wait == -1) || (clib_time_now (&vcm->clib_time) < timeout));
3567 vep_verify_epoll_chain (u32 vep_idx)
3570 vppcom_epoll_t *vep;
3574 if (VPPCOM_DEBUG <= 1)
3577 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3578 rv = vppcom_session_at_index (vep_idx, &session);
3579 if (PREDICT_FALSE (rv))
3581 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
3582 getpid (), vep_idx);
3585 if (PREDICT_FALSE (!session->is_vep))
3587 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3588 getpid (), vep_idx);
3591 vep = &session->vep;
3592 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
3595 " is_vep_session = %u\n"
3596 " next_sid = 0x%x (%u)\n"
3597 " wait_cont_idx = 0x%x (%u)\n"
3598 "}\n", getpid (), vep_idx,
3599 session->is_vep, session->is_vep_session,
3600 vep->next_sid, vep->next_sid,
3601 session->wait_cont_idx, session->wait_cont_idx);
3603 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
3605 rv = vppcom_session_at_index (sid, &session);
3606 if (PREDICT_FALSE (rv))
3608 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
3611 if (PREDICT_FALSE (session->is_vep))
3612 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
3613 getpid (), vep_idx);
3614 else if (PREDICT_FALSE (!session->is_vep_session))
3616 clib_warning ("VCL<%d>: ERROR: session (%u) "
3617 "is not a vep session!", getpid (), sid);
3620 vep = &session->vep;
3621 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
3622 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
3623 "vep_idx (%u)!", getpid (),
3624 sid, session->vep.vep_idx, vep_idx);
3625 if (session->is_vep_session)
3627 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3629 " next_sid = 0x%x (%u)\n"
3630 " prev_sid = 0x%x (%u)\n"
3631 " vep_idx = 0x%x (%u)\n"
3632 " ev.events = 0x%x\n"
3633 " ev.data.u64 = 0x%llx\n"
3637 vep->next_sid, vep->next_sid,
3638 vep->prev_sid, vep->prev_sid,
3639 vep->vep_idx, vep->vep_idx,
3640 vep->ev.events, vep->ev.data.u64, vep->et_mask);
3645 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
3646 getpid (), vep_idx);
3650 vppcom_epoll_create (void)
3652 session_t *vep_session;
3654 elog_track_t vep_elog_track;
3656 VCL_SESSION_LOCK ();
3657 pool_get (vcm->sessions, vep_session);
3658 memset (vep_session, 0, sizeof (*vep_session));
3659 vep_idx = vep_session - vcm->sessions;
3661 vep_session->is_vep = 1;
3662 vep_session->vep.vep_idx = ~0;
3663 vep_session->vep.next_sid = ~0;
3664 vep_session->vep.prev_sid = ~0;
3665 vep_session->wait_cont_idx = ~0;
3666 vep_session->vpp_handle = ~0;
3667 vep_session->poll_reg = 0;
3669 if (VPPCOM_DEBUG > 0)
3671 vep_session->elog_track.name =
3672 (char *) format (0, "C:%d:VEP:%d%c", vcm->my_client_index,
3674 elog_track_register (&vcm->elog_main, &vep_session->elog_track);
3675 vep_elog_track = vep_session->elog_track;
3678 VCL_SESSION_UNLOCK ();
3680 if (VPPCOM_DEBUG > 0)
3681 clib_warning ("VCL<%d>: Created vep_idx %u / sid %u!",
3682 getpid (), vep_idx, vep_idx);
3684 if (VPPCOM_DEBUG > 0)
3688 ELOG_TYPE_DECLARE (e) =
3690 .format = "created epoll session:%d",
3691 .format_args = "i4",
3699 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_elog_track);
3708 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3709 struct epoll_event *event)
3711 session_t *vep_session;
3715 if (vep_idx == session_index)
3717 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
3718 getpid (), vep_idx);
3719 return VPPCOM_EINVAL;
3722 VCL_SESSION_LOCK ();
3723 rv = vppcom_session_at_index (vep_idx, &vep_session);
3724 if (PREDICT_FALSE (rv))
3726 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
3729 if (PREDICT_FALSE (!vep_session->is_vep))
3731 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3732 getpid (), vep_idx);
3737 ASSERT (vep_session->vep.vep_idx == ~0);
3738 ASSERT (vep_session->vep.prev_sid == ~0);
3740 rv = vppcom_session_at_index (session_index, &session);
3741 if (PREDICT_FALSE (rv))
3743 if (VPPCOM_DEBUG > 0)
3744 clib_warning ("VCL<%d>: ERROR: Invalid session_index (%u)!",
3745 getpid (), session_index);
3748 if (PREDICT_FALSE (session->is_vep))
3750 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3758 if (PREDICT_FALSE (!event))
3760 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
3761 "epoll_event structure!", getpid ());
3765 if (vep_session->vep.next_sid != ~0)
3767 session_t *next_session;
3768 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3770 if (PREDICT_FALSE (rv))
3772 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
3773 "vep.next_sid (%u) on vep_idx (%u)!",
3774 getpid (), vep_session->vep.next_sid, vep_idx);
3777 ASSERT (next_session->vep.prev_sid == vep_idx);
3778 next_session->vep.prev_sid = session_index;
3780 session->vep.next_sid = vep_session->vep.next_sid;
3781 session->vep.prev_sid = vep_idx;
3782 session->vep.vep_idx = vep_idx;
3783 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3784 session->vep.ev = *event;
3785 session->is_vep = 0;
3786 session->is_vep_session = 1;
3787 vep_session->vep.next_sid = session_index;
3789 /* VCL Event Register handler */
3790 if (session->state & STATE_LISTEN)
3792 /* Register handler for connect_request event on listen_session_index */
3793 vce_event_key_t evk;
3794 evk.session_index = session_index;
3795 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
3796 vep_session->poll_reg =
3797 vce_register_handler (&vcm->event_thread, &evk,
3798 vce_poll_wait_connect_request_handler_fn,
3799 0 /* No callback args */ );
3801 if (VPPCOM_DEBUG > 1)
3802 clib_warning ("VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
3803 "sid %u, events 0x%x, data 0x%llx!",
3804 getpid (), vep_idx, session_index,
3805 event->events, event->data.u64);
3806 if (VPPCOM_DEBUG > 0)
3809 ELOG_TYPE_DECLARE (e) =
3811 .format = "epoll_ctladd: events:%x data:%x",
3812 .format_args = "i4i4i8",
3820 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3822 ed->events = event->events;
3823 ed->event_data = event->data.u64;
3829 if (PREDICT_FALSE (!event))
3831 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
3832 "epoll_event structure!", getpid ());
3836 else if (PREDICT_FALSE (!session->is_vep_session))
3838 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3839 "not a vep session!", getpid (), session_index);
3843 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3845 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3846 "vep_idx (%u) != vep_idx (%u)!",
3847 getpid (), session_index,
3848 session->vep.vep_idx, vep_idx);
3852 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3853 session->vep.ev = *event;
3854 if (VPPCOM_DEBUG > 1)
3856 ("VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3857 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
3862 if (PREDICT_FALSE (!session->is_vep_session))
3864 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3865 "not a vep session!", getpid (), session_index);
3869 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3871 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3872 "vep_idx (%u) != vep_idx (%u)!",
3873 getpid (), session_index,
3874 session->vep.vep_idx, vep_idx);
3879 /* VCL Event Un-register handler */
3880 if ((session->state & STATE_LISTEN) && vep_session->poll_reg)
3882 (void) vce_unregister_handler (&vcm->event_thread,
3883 vep_session->poll_reg);
3886 vep_session->wait_cont_idx =
3887 (vep_session->wait_cont_idx == session_index) ?
3888 session->vep.next_sid : vep_session->wait_cont_idx;
3890 if (session->vep.prev_sid == vep_idx)
3891 vep_session->vep.next_sid = session->vep.next_sid;
3894 session_t *prev_session;
3895 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3896 if (PREDICT_FALSE (rv))
3898 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
3899 "vep.prev_sid (%u) on sid (%u)!",
3900 getpid (), session->vep.prev_sid, session_index);
3903 ASSERT (prev_session->vep.next_sid == session_index);
3904 prev_session->vep.next_sid = session->vep.next_sid;
3906 if (session->vep.next_sid != ~0)
3908 session_t *next_session;
3909 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3910 if (PREDICT_FALSE (rv))
3912 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
3913 "vep.next_sid (%u) on sid (%u)!",
3914 getpid (), session->vep.next_sid, session_index);
3917 ASSERT (next_session->vep.prev_sid == session_index);
3918 next_session->vep.prev_sid = session->vep.prev_sid;
3921 memset (&session->vep, 0, sizeof (session->vep));
3922 session->vep.next_sid = ~0;
3923 session->vep.prev_sid = ~0;
3924 session->vep.vep_idx = ~0;
3925 session->is_vep_session = 0;
3926 if (VPPCOM_DEBUG > 1)
3927 clib_warning ("VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
3928 getpid (), vep_idx, session_index);
3929 if (VPPCOM_DEBUG > 0)
3932 ELOG_TYPE_DECLARE (e) =
3934 .format = "epoll_ctldel: vep:%d",
3935 .format_args = "i4",
3942 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3950 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
3954 vep_verify_epoll_chain (vep_idx);
3957 VCL_SESSION_UNLOCK ();
3962 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3963 int maxevents, double wait_for_time)
3965 session_t *vep_session;
3966 elog_track_t vep_elog_track;
3968 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3969 u32 keep_trying = 1;
3971 u32 vep_next_sid, wait_cont_idx;
3974 if (PREDICT_FALSE (maxevents <= 0))
3976 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
3977 getpid (), maxevents);
3978 return VPPCOM_EINVAL;
3980 memset (events, 0, sizeof (*events) * maxevents);
3982 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
3983 vep_next_sid = vep_session->vep.next_sid;
3984 is_vep = vep_session->is_vep;
3985 wait_cont_idx = vep_session->wait_cont_idx;
3986 vep_elog_track = vep_session->elog_track;
3987 VCL_SESSION_UNLOCK ();
3989 if (PREDICT_FALSE (!is_vep))
3991 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3992 getpid (), vep_idx);
3996 if (PREDICT_FALSE (vep_next_sid == ~0))
3998 if (VPPCOM_DEBUG > 1)
3999 clib_warning ("VCL<%d>: WARNING: vep_idx (%u) is empty!",
4000 getpid (), vep_idx);
4001 if (VPPCOM_DEBUG > 1)
4004 ELOG_TYPE_DECLARE (e) =
4006 .format = "WRN: vep_idx:%d empty",
4007 .format_args = "i4",
4014 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_elog_track);
4027 elog_track_t session_elog_track;
4029 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
4030 sid != ~0; sid = next_sid)
4032 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
4033 u8 add_event, is_vep_session;
4035 u64 session_ev_data;
4037 VCL_SESSION_LOCK_AND_GET (sid, &session);
4038 next_sid = session->vep.next_sid;
4039 session_events = session->vep.ev.events;
4040 et_mask = session->vep.et_mask;
4041 is_vep = session->is_vep;
4042 is_vep_session = session->is_vep_session;
4043 session_vep_idx = session->vep.vep_idx;
4044 session_ev_data = session->vep.ev.data.u64;
4046 if (VPPCOM_DEBUG > 0)
4048 session_elog_track = session->elog_track;
4051 VCL_SESSION_UNLOCK ();
4053 if (PREDICT_FALSE (is_vep))
4055 if (VPPCOM_DEBUG > 0)
4056 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
4057 getpid (), vep_idx);
4058 if (VPPCOM_DEBUG > 0)
4061 ELOG_TYPE_DECLARE (e) =
4063 .format = "ERR:vep_idx:%d is vep",
4064 .format_args = "i4",
4071 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
4080 if (PREDICT_FALSE (!is_vep_session))
4082 if (VPPCOM_DEBUG > 0)
4083 clib_warning ("VCL<%d>: ERROR: session (%u) is not "
4084 "a vep session!", getpid (), sid);
4085 if (VPPCOM_DEBUG > 0)
4088 ELOG_TYPE_DECLARE (e) =
4090 .format = "ERR:SID:%d not vep",
4091 .format_args = "i4",
4098 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
4107 if (PREDICT_FALSE (session_vep_idx != vep_idx))
4109 clib_warning ("VCL<%d>: ERROR: session (%u) "
4110 "vep_idx (%u) != vep_idx (%u)!",
4111 getpid (), sid, session_vep_idx, vep_idx);
4116 add_event = clear_et_mask = 0;
4118 if (EPOLLIN & session_events)
4120 VCL_SESSION_LOCK_AND_GET (sid, &session);
4121 ready = vppcom_session_read_ready (session, sid);
4122 VCL_SESSION_UNLOCK ();
4123 if ((ready > 0) && (EPOLLIN & et_mask))
4126 events[num_ev].events |= EPOLLIN;
4127 if (((EPOLLET | EPOLLIN) & session_events) ==
4128 (EPOLLET | EPOLLIN))
4129 clear_et_mask |= EPOLLIN;
4136 case VPPCOM_ECONNRESET:
4137 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
4141 events[num_ev].events |= EPOLLERR;
4147 if (EPOLLOUT & session_events)
4149 VCL_SESSION_LOCK_AND_GET (sid, &session);
4150 ready = vppcom_session_write_ready (session, sid);
4151 VCL_SESSION_UNLOCK ();
4152 if ((ready > 0) && (EPOLLOUT & et_mask))
4155 events[num_ev].events |= EPOLLOUT;
4156 if (((EPOLLET | EPOLLOUT) & session_events) ==
4157 (EPOLLET | EPOLLOUT))
4158 clear_et_mask |= EPOLLOUT;
4165 case VPPCOM_ECONNRESET:
4166 events[num_ev].events |= EPOLLHUP;
4170 events[num_ev].events |= EPOLLERR;
4178 events[num_ev].data.u64 = session_ev_data;
4179 if (EPOLLONESHOT & session_events)
4181 VCL_SESSION_LOCK_AND_GET (sid, &session);
4182 session->vep.ev.events = 0;
4183 VCL_SESSION_UNLOCK ();
4186 if (num_ev == maxevents)
4188 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
4189 vep_session->wait_cont_idx = next_sid;
4190 VCL_SESSION_UNLOCK ();
4194 if (wait_cont_idx != ~0)
4197 next_sid = vep_next_sid;
4198 else if (next_sid == wait_cont_idx)
4202 if (wait_for_time != -1)
4203 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
4205 while ((num_ev == 0) && keep_trying);
4207 if (wait_cont_idx != ~0)
4209 VCL_SESSION_LOCK_AND_GET (vep_idx, &vep_session);
4210 vep_session->wait_cont_idx = ~0;
4211 VCL_SESSION_UNLOCK ();
4214 return (rv != VPPCOM_OK) ? rv : num_ev;
4218 vppcom_session_attr (uint32_t session_index, uint32_t op,
4219 void *buffer, uint32_t * buflen)
4223 u32 *flags = buffer;
4224 vppcom_endpt_t *ep = buffer;
4226 VCL_SESSION_LOCK_AND_GET (session_index, &session);
4232 case VPPCOM_ATTR_GET_NREAD:
4233 rv = vppcom_session_read_ready (session, session_index);
4234 if (VPPCOM_DEBUG > 2)
4235 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
4237 if (VPPCOM_DEBUG > 0)
4240 ELOG_TYPE_DECLARE (e) =
4242 .format = "VPPCOM_ATTR_GET_NREAD: nread=%d",
4243 .format_args = "i4",
4250 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4258 case VPPCOM_ATTR_GET_NWRITE:
4259 rv = vppcom_session_write_ready (session, session_index);
4260 if (VPPCOM_DEBUG > 2)
4261 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
4262 getpid (), session_index, rv);
4263 if (VPPCOM_DEBUG > 0)
4266 ELOG_TYPE_DECLARE (e) =
4268 .format = "VPPCOM_ATTR_GET_NWRITE: nwrite=%d",
4269 .format_args = "i4",
4276 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4283 case VPPCOM_ATTR_GET_FLAGS:
4284 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
4286 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
4287 VCL_SESS_ATTR_NONBLOCK));
4288 *buflen = sizeof (*flags);
4289 if (VPPCOM_DEBUG > 2)
4290 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, "
4291 "flags = 0x%08x, is_nonblocking = %u", getpid (),
4292 session_index, *flags,
4293 VCL_SESS_ATTR_TEST (session->attr,
4294 VCL_SESS_ATTR_NONBLOCK));
4295 if (VPPCOM_DEBUG > 0)
4298 ELOG_TYPE_DECLARE (e) =
4300 .format = "VPPCOM_ATTR_GET_FLAGS: flags=%x is_nonblk=%d",
4301 .format_args = "i4i4",
4309 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4312 ed->is_nonblk = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
4321 case VPPCOM_ATTR_SET_FLAGS:
4322 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
4324 if (*flags & O_NONBLOCK)
4325 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
4327 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
4329 if (VPPCOM_DEBUG > 2)
4330 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, "
4331 "flags = 0x%08x, is_nonblocking = %u",
4332 getpid (), session_index, *flags,
4333 VCL_SESS_ATTR_TEST (session->attr,
4334 VCL_SESS_ATTR_NONBLOCK));
4335 if (VPPCOM_DEBUG > 0)
4338 ELOG_TYPE_DECLARE (e) =
4340 .format = "VPPCOM_ATTR_SET_FLAGS: flags=%x is_nonblk=%d",
4341 .format_args = "i4i4",
4349 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4352 ed->is_nonblk = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
4360 case VPPCOM_ATTR_GET_PEER_ADDR:
4361 if (PREDICT_TRUE (buffer && buflen &&
4362 (*buflen >= sizeof (*ep)) && ep->ip))
4364 ep->is_ip4 = session->peer_addr.is_ip4;
4365 ep->port = session->peer_port;
4366 if (session->peer_addr.is_ip4)
4367 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
4368 sizeof (ip4_address_t));
4370 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
4371 sizeof (ip6_address_t));
4372 *buflen = sizeof (*ep);
4373 if (VPPCOM_DEBUG > 1)
4374 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, "
4375 "is_ip4 = %u, addr = %U, port %u", getpid (),
4376 session_index, ep->is_ip4, format_ip46_address,
4377 &session->peer_addr.ip46,
4378 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
4379 clib_net_to_host_u16 (ep->port));
4380 if (VPPCOM_DEBUG > 0)
4385 ELOG_TYPE_DECLARE (e) =
4387 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:%d.%d.%d.%d:%d",
4388 .format_args = "i1i1i1i1i2",
4390 CLIB_PACKED (struct {
4395 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4397 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
4398 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
4399 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
4400 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
4401 ed->port = clib_net_to_host_u16 (session->peer_port);
4407 ELOG_TYPE_DECLARE (e) =
4409 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:IP6:%d",
4410 .format_args = "i2",
4412 CLIB_PACKED (struct {
4416 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4418 ed->port = clib_net_to_host_u16 (session->peer_port);
4427 case VPPCOM_ATTR_GET_LCL_ADDR:
4428 if (PREDICT_TRUE (buffer && buflen &&
4429 (*buflen >= sizeof (*ep)) && ep->ip))
4431 ep->is_ip4 = session->lcl_addr.is_ip4;
4432 ep->port = session->lcl_port;
4433 if (session->lcl_addr.is_ip4)
4434 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
4435 sizeof (ip4_address_t));
4437 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
4438 sizeof (ip6_address_t));
4439 *buflen = sizeof (*ep);
4440 if (VPPCOM_DEBUG > 1)
4441 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, "
4442 "is_ip4 = %u, addr = %U port %d", getpid (),
4443 session_index, ep->is_ip4, format_ip46_address,
4444 &session->lcl_addr.ip46,
4445 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
4446 clib_net_to_host_u16 (ep->port));
4447 if (VPPCOM_DEBUG > 0)
4452 ELOG_TYPE_DECLARE (e) =
4454 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:%d.%d.%d.%d:%d",
4455 .format_args = "i1i1i1i1i2",
4457 CLIB_PACKED (struct {
4462 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4464 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
4465 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
4466 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
4467 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
4468 ed->port = clib_net_to_host_u16 (session->peer_port);
4474 ELOG_TYPE_DECLARE (e) =
4476 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:IP6:%d",
4477 .format_args = "i2",
4479 CLIB_PACKED (struct {
4483 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4485 ed->port = clib_net_to_host_u16 (session->peer_port);
4494 case VPPCOM_ATTR_GET_LIBC_EPFD:
4495 rv = session->libc_epfd;
4496 if (VPPCOM_DEBUG > 2)
4497 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
4499 if (VPPCOM_DEBUG > 0)
4502 ELOG_TYPE_DECLARE (e) =
4504 .format = "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd=%d",
4505 .format_args = "i4",
4507 CLIB_PACKED (struct {
4511 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4512 ed->data = session->libc_epfd;
4518 case VPPCOM_ATTR_SET_LIBC_EPFD:
4519 if (PREDICT_TRUE (buffer && buflen &&
4520 (*buflen == sizeof (session->libc_epfd))))
4522 session->libc_epfd = *(int *) buffer;
4523 *buflen = sizeof (session->libc_epfd);
4525 if (VPPCOM_DEBUG > 2)
4526 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
4527 "buflen %d", getpid (), session->libc_epfd,
4529 if (VPPCOM_DEBUG > 0)
4532 ELOG_TYPE_DECLARE (e) =
4534 .format = "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd=%s%d buflen=%d",
4535 .format_args = "t1i4i4",
4536 .n_enum_strings = 2,
4537 .enum_strings = {"", "-",},
4539 CLIB_PACKED (struct {
4544 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4546 ed->sign = (session->libc_epfd < 0);
4547 ed->data[0] = abs(session->libc_epfd);
4548 ed->data[1] = *buflen;
4556 case VPPCOM_ATTR_GET_PROTOCOL:
4557 if (buffer && buflen && (*buflen >= sizeof (int)))
4559 *(int *) buffer = session->proto;
4560 *buflen = sizeof (int);
4562 if (VPPCOM_DEBUG > 2)
4563 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), "
4564 "buflen %d", getpid (), *(int *) buffer,
4565 *(int *) buffer ? "UDP" : "TCP", *buflen);
4566 if (VPPCOM_DEBUG > 0)
4569 ELOG_TYPE_DECLARE (e) =
4571 .format = "VPPCOM_ATTR_GET_PROTOCOL: %s buflen=%d",
4572 .format_args = "t1i4",
4573 .n_enum_strings = 2,
4574 .enum_strings = {"TCP", "UDP",},
4577 CLIB_PACKED (struct {
4582 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4583 ed->proto = session->proto;
4584 ed->buflen = *(int *) buffer;
4592 case VPPCOM_ATTR_GET_LISTEN:
4593 if (buffer && buflen && (*buflen >= sizeof (int)))
4595 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4596 VCL_SESS_ATTR_LISTEN);
4597 *buflen = sizeof (int);
4599 if (VPPCOM_DEBUG > 2)
4600 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, "
4601 "buflen %d", getpid (), *(int *) buffer, *buflen);
4602 if (VPPCOM_DEBUG > 0)
4605 ELOG_TYPE_DECLARE (e) =
4607 .format = "VPPCOM_ATTR_GET_LISTEN: %d buflen=%d",
4608 .format_args = "i4i4",
4615 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4616 ed->data[0] = *(int *) buffer;
4617 ed->data[1] = *buflen;
4625 case VPPCOM_ATTR_GET_ERROR:
4626 if (buffer && buflen && (*buflen >= sizeof (int)))
4628 *(int *) buffer = 0;
4629 *buflen = sizeof (int);
4631 if (VPPCOM_DEBUG > 2)
4632 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, "
4633 "buflen %d, #VPP-TBD#", getpid (),
4634 *(int *) buffer, *buflen);
4635 if (VPPCOM_DEBUG > 0)
4638 ELOG_TYPE_DECLARE (e) =
4640 .format = "VPPCOM_ATTR_GET_ERROR: %d buflen=%d",
4641 .format_args = "i4i4",
4648 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4649 ed->data[0] = *(int *) buffer;
4650 ed->data[1] = *buflen;
4658 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
4659 if (buffer && buflen && (*buflen >= sizeof (u32)))
4663 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
4664 session->tx_fifo ? session->tx_fifo->nitems :
4665 vcm->cfg.tx_fifo_size);
4666 *buflen = sizeof (u32);
4668 if (VPPCOM_DEBUG > 2)
4669 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
4670 "buflen %d, #VPP-TBD#", getpid (),
4671 *(size_t *) buffer, *(size_t *) buffer, *buflen);
4672 if (VPPCOM_DEBUG > 0)
4675 ELOG_TYPE_DECLARE (e) =
4677 .format = "VPPCOM_ATTR_GET_TX_FIFO_LEN: 0x%x buflen=%d",
4678 .format_args = "i4i4",
4685 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4686 ed->data[0] = *(size_t *) buffer;
4687 ed->data[1] = *buflen;
4695 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
4696 if (buffer && buflen && (*buflen == sizeof (u32)))
4699 session->sndbuf_size = *(u32 *) buffer;
4700 if (VPPCOM_DEBUG > 2)
4701 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
4702 "buflen %d, #VPP-TBD#", getpid (),
4703 session->sndbuf_size, session->sndbuf_size,
4705 if (VPPCOM_DEBUG > 0)
4708 ELOG_TYPE_DECLARE (e) =
4710 .format = "VPPCOM_ATTR_SET_TX_FIFO_LEN: 0x%x buflen=%d",
4711 .format_args = "i4i4",
4718 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4719 ed->data[0] = session->sndbuf_size;
4720 ed->data[1] = *buflen;
4728 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
4729 if (buffer && buflen && (*buflen >= sizeof (u32)))
4733 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
4734 session->rx_fifo ? session->rx_fifo->nitems :
4735 vcm->cfg.rx_fifo_size);
4736 *buflen = sizeof (u32);
4738 if (VPPCOM_DEBUG > 2)
4739 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
4740 "buflen %d, #VPP-TBD#", getpid (),
4741 *(size_t *) buffer, *(size_t *) buffer, *buflen);
4742 if (VPPCOM_DEBUG > 0)
4745 ELOG_TYPE_DECLARE (e) =
4747 .format = "VPPCOM_ATTR_GET_RX_FIFO_LEN: 0x%x buflen=%d",
4748 .format_args = "i4i4",
4755 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4756 ed->data[0] = *(size_t *) buffer;
4757 ed->data[1] = *buflen;
4765 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
4766 if (buffer && buflen && (*buflen == sizeof (u32)))
4769 session->rcvbuf_size = *(u32 *) buffer;
4770 if (VPPCOM_DEBUG > 2)
4771 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
4772 "buflen %d, #VPP-TBD#", getpid (),
4773 session->sndbuf_size, session->sndbuf_size,
4775 if (VPPCOM_DEBUG > 0)
4778 ELOG_TYPE_DECLARE (e) =
4780 .format = "VPPCOM_ATTR_SET_RX_FIFO_LEN: 0x%x buflen=%d",
4781 .format_args = "i4i4",
4788 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4789 ed->data[0] = session->sndbuf_size;
4790 ed->data[1] = *buflen;
4798 case VPPCOM_ATTR_GET_REUSEADDR:
4799 if (buffer && buflen && (*buflen >= sizeof (int)))
4802 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4803 VCL_SESS_ATTR_REUSEADDR);
4804 *buflen = sizeof (int);
4806 if (VPPCOM_DEBUG > 2)
4807 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
4808 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4810 if (VPPCOM_DEBUG > 0)
4813 ELOG_TYPE_DECLARE (e) =
4815 .format = "VPPCOM_ATTR_GET_REUSEADDR: %d buflen=%d",
4816 .format_args = "i4i4",
4823 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4824 ed->data[0] = *(int *) buffer;
4825 ed->data[1] = *buflen;
4833 case VPPCOM_ATTR_SET_REUSEADDR:
4834 if (buffer && buflen && (*buflen == sizeof (int)) &&
4835 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4838 if (*(int *) buffer)
4839 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
4841 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
4843 if (VPPCOM_DEBUG > 2)
4844 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, "
4845 "buflen %d, #VPP-TBD#", getpid (),
4846 VCL_SESS_ATTR_TEST (session->attr,
4847 VCL_SESS_ATTR_REUSEADDR),
4849 if (VPPCOM_DEBUG > 0)
4852 ELOG_TYPE_DECLARE (e) =
4854 .format = "VPPCOM_ATTR_SET_REUSEADDR: %d buflen=%d",
4855 .format_args = "i4i4",
4862 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4863 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4864 VCL_SESS_ATTR_REUSEADDR);
4865 ed->data[1] = *buflen;
4873 case VPPCOM_ATTR_GET_REUSEPORT:
4874 if (buffer && buflen && (*buflen >= sizeof (int)))
4877 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4878 VCL_SESS_ATTR_REUSEPORT);
4879 *buflen = sizeof (int);
4881 if (VPPCOM_DEBUG > 2)
4882 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, "
4883 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4885 if (VPPCOM_DEBUG > 0)
4888 ELOG_TYPE_DECLARE (e) =
4890 .format = "VPPCOM_ATTR_GET_REUSEPORT: %d buflen=%d",
4891 .format_args = "i4i4",
4898 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4899 ed->data[0] = *(int *) buffer;
4900 ed->data[1] = *buflen;
4908 case VPPCOM_ATTR_SET_REUSEPORT:
4909 if (buffer && buflen && (*buflen == sizeof (int)) &&
4910 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4913 if (*(int *) buffer)
4914 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
4916 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
4918 if (VPPCOM_DEBUG > 2)
4919 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, "
4920 "buflen %d, #VPP-TBD#", getpid (),
4921 VCL_SESS_ATTR_TEST (session->attr,
4922 VCL_SESS_ATTR_REUSEPORT),
4924 if (VPPCOM_DEBUG > 0)
4927 ELOG_TYPE_DECLARE (e) =
4929 .format = "VPPCOM_ATTR_SET_REUSEPORT: %d buflen=%d",
4930 .format_args = "i4i4",
4937 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4938 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4939 VCL_SESS_ATTR_REUSEPORT);
4940 ed->data[1] = *buflen;
4948 case VPPCOM_ATTR_GET_BROADCAST:
4949 if (buffer && buflen && (*buflen >= sizeof (int)))
4952 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4953 VCL_SESS_ATTR_BROADCAST);
4954 *buflen = sizeof (int);
4956 if (VPPCOM_DEBUG > 2)
4957 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, "
4958 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4960 if (VPPCOM_DEBUG > 0)
4963 ELOG_TYPE_DECLARE (e) =
4965 .format = "VPPCOM_ATTR_GET_BROADCAST: %d buflen=%d",
4966 .format_args = "i4i4",
4973 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4974 ed->data[0] = *(int *) buffer;
4975 ed->data[1] = *buflen;
4983 case VPPCOM_ATTR_SET_BROADCAST:
4984 if (buffer && buflen && (*buflen == sizeof (int)))
4987 if (*(int *) buffer)
4988 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
4990 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
4992 if (VPPCOM_DEBUG > 2)
4993 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, "
4994 "buflen %d, #VPP-TBD#", getpid (),
4995 VCL_SESS_ATTR_TEST (session->attr,
4996 VCL_SESS_ATTR_BROADCAST),
4998 if (VPPCOM_DEBUG > 0)
5001 ELOG_TYPE_DECLARE (e) =
5003 .format = "VPPCOM_ATTR_SET_BROADCAST: %d buflen=%d",
5004 .format_args = "i4i4",
5011 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5012 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5013 VCL_SESS_ATTR_BROADCAST);
5014 ed->data[1] = *buflen;
5022 case VPPCOM_ATTR_GET_V6ONLY:
5023 if (buffer && buflen && (*buflen >= sizeof (int)))
5026 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5027 VCL_SESS_ATTR_V6ONLY);
5028 *buflen = sizeof (int);
5030 if (VPPCOM_DEBUG > 2)
5031 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, "
5032 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5034 if (VPPCOM_DEBUG > 0)
5037 ELOG_TYPE_DECLARE (e) =
5039 .format = "VPPCOM_ATTR_GET_V6ONLY: %d buflen=%d",
5040 .format_args = "i4i4",
5047 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5048 ed->data[0] = *(int *) buffer;
5049 ed->data[1] = *buflen;
5057 case VPPCOM_ATTR_SET_V6ONLY:
5058 if (buffer && buflen && (*buflen == sizeof (int)))
5061 if (*(int *) buffer)
5062 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
5064 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
5066 if (VPPCOM_DEBUG > 2)
5067 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, "
5068 "buflen %d, #VPP-TBD#", getpid (),
5069 VCL_SESS_ATTR_TEST (session->attr,
5070 VCL_SESS_ATTR_V6ONLY), *buflen);
5071 if (VPPCOM_DEBUG > 0)
5074 ELOG_TYPE_DECLARE (e) =
5076 .format = "VPPCOM_ATTR_SET_V6ONLY: %d buflen=%d",
5077 .format_args = "i4i4",
5084 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5085 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5086 VCL_SESS_ATTR_V6ONLY);
5087 ed->data[1] = *buflen;
5095 case VPPCOM_ATTR_GET_KEEPALIVE:
5096 if (buffer && buflen && (*buflen >= sizeof (int)))
5099 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5100 VCL_SESS_ATTR_KEEPALIVE);
5101 *buflen = sizeof (int);
5103 if (VPPCOM_DEBUG > 2)
5104 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, "
5105 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5107 if (VPPCOM_DEBUG > 0)
5110 ELOG_TYPE_DECLARE (e) =
5112 .format = "VPPCOM_ATTR_GET_KEEPALIVE: %d buflen=%d",
5113 .format_args = "i4i4",
5120 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5121 ed->data[0] = *(int *) buffer;
5122 ed->data[1] = *buflen;
5130 case VPPCOM_ATTR_SET_KEEPALIVE:
5131 if (buffer && buflen && (*buflen == sizeof (int)))
5134 if (*(int *) buffer)
5135 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5137 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5139 if (VPPCOM_DEBUG > 2)
5140 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, "
5141 "buflen %d, #VPP-TBD#", getpid (),
5142 VCL_SESS_ATTR_TEST (session->attr,
5143 VCL_SESS_ATTR_KEEPALIVE),
5145 if (VPPCOM_DEBUG > 0)
5148 ELOG_TYPE_DECLARE (e) =
5150 .format = "VPPCOM_ATTR_SET_KEEPALIVE: %d buflen=%d",
5151 .format_args = "i4i4",
5158 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5159 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5160 VCL_SESS_ATTR_KEEPALIVE);
5161 ed->data[1] = *buflen;
5169 case VPPCOM_ATTR_GET_TCP_NODELAY:
5170 if (buffer && buflen && (*buflen >= sizeof (int)))
5173 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5174 VCL_SESS_ATTR_TCP_NODELAY);
5175 *buflen = sizeof (int);
5177 if (VPPCOM_DEBUG > 2)
5178 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, "
5179 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5181 if (VPPCOM_DEBUG > 0)
5184 ELOG_TYPE_DECLARE (e) =
5186 .format = "VPPCOM_ATTR_GET_TCP_NODELAY: %d buflen=%d",
5187 .format_args = "i4i4",
5194 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5195 ed->data[0] = *(int *) buffer;
5196 ed->data[1] = *buflen;
5204 case VPPCOM_ATTR_SET_TCP_NODELAY:
5205 if (buffer && buflen && (*buflen == sizeof (int)))
5208 if (*(int *) buffer)
5209 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5211 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5213 if (VPPCOM_DEBUG > 2)
5214 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, "
5215 "buflen %d, #VPP-TBD#", getpid (),
5216 VCL_SESS_ATTR_TEST (session->attr,
5217 VCL_SESS_ATTR_TCP_NODELAY),
5219 if (VPPCOM_DEBUG > 0)
5222 ELOG_TYPE_DECLARE (e) =
5224 .format = "VPPCOM_ATTR_SET_TCP_NODELAY: %d buflen=%d",
5225 .format_args = "i4i4",
5232 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5233 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5234 VCL_SESS_ATTR_TCP_NODELAY);
5235 ed->data[1] = *buflen;
5243 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
5244 if (buffer && buflen && (*buflen >= sizeof (int)))
5247 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5248 VCL_SESS_ATTR_TCP_KEEPIDLE);
5249 *buflen = sizeof (int);
5251 if (VPPCOM_DEBUG > 2)
5252 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, "
5253 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5255 if (VPPCOM_DEBUG > 0)
5258 ELOG_TYPE_DECLARE (e) =
5260 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5261 .format_args = "i4i4",
5268 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5269 ed->data[0] = *(int *) buffer;
5270 ed->data[1] = *buflen;
5278 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
5279 if (buffer && buflen && (*buflen == sizeof (int)))
5282 if (*(int *) buffer)
5283 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5285 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5287 if (VPPCOM_DEBUG > 2)
5288 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, "
5289 "buflen %d, #VPP-TBD#", getpid (),
5290 VCL_SESS_ATTR_TEST (session->attr,
5291 VCL_SESS_ATTR_TCP_KEEPIDLE),
5293 if (VPPCOM_DEBUG > 0)
5296 ELOG_TYPE_DECLARE (e) =
5298 .format = "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d buflen=%d",
5299 .format_args = "i4i4",
5306 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5307 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5308 VCL_SESS_ATTR_TCP_KEEPIDLE);
5309 ed->data[1] = *buflen;
5317 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
5318 if (buffer && buflen && (*buflen >= sizeof (int)))
5321 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5322 VCL_SESS_ATTR_TCP_KEEPINTVL);
5323 *buflen = sizeof (int);
5325 if (VPPCOM_DEBUG > 2)
5326 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, "
5327 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5329 if (VPPCOM_DEBUG > 0)
5332 ELOG_TYPE_DECLARE (e) =
5334 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5335 .format_args = "i4i4",
5342 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5343 ed->data[0] = *(int *) buffer;
5344 ed->data[1] = *buflen;
5352 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
5353 if (buffer && buflen && (*buflen == sizeof (int)))
5356 if (*(int *) buffer)
5357 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5359 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5361 if (VPPCOM_DEBUG > 2)
5362 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, "
5363 "buflen %d, #VPP-TBD#", getpid (),
5364 VCL_SESS_ATTR_TEST (session->attr,
5365 VCL_SESS_ATTR_TCP_KEEPINTVL),
5367 if (VPPCOM_DEBUG > 0)
5370 ELOG_TYPE_DECLARE (e) =
5372 .format = "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d buflen=%d",
5373 .format_args = "i4i4",
5380 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5381 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5382 VCL_SESS_ATTR_TCP_KEEPINTVL);
5383 ed->data[1] = *buflen;
5391 case VPPCOM_ATTR_GET_TCP_USER_MSS:
5392 if (buffer && buflen && (*buflen >= sizeof (u32)))
5395 *(u32 *) buffer = session->user_mss;
5396 *buflen = sizeof (int);
5398 if (VPPCOM_DEBUG > 2)
5399 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, "
5400 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5402 if (VPPCOM_DEBUG > 0)
5405 ELOG_TYPE_DECLARE (e) =
5407 .format = "VPPCOM_ATTR_GET_TCP_USER_MSS: %d buflen=%d",
5408 .format_args = "i4i4",
5415 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5416 ed->data[0] = *(int *) buffer;
5417 ed->data[1] = *buflen;
5425 case VPPCOM_ATTR_SET_TCP_USER_MSS:
5426 if (buffer && buflen && (*buflen == sizeof (u32)))
5429 session->user_mss = *(u32 *) buffer;
5431 if (VPPCOM_DEBUG > 2)
5432 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, "
5433 "buflen %d, #VPP-TBD#", getpid (),
5434 session->user_mss, *buflen);
5435 if (VPPCOM_DEBUG > 0)
5438 ELOG_TYPE_DECLARE (e) =
5440 .format = "VPPCOM_ATTR_SET_TCP_USER_MSS: %d buflen=%d",
5441 .format_args = "i4i4",
5448 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5449 ed->data[0] = session->user_mss;
5450 ed->data[1] = *buflen;
5464 VCL_SESSION_UNLOCK ();
5469 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
5470 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5473 session_t *session = 0;
5477 VCL_SESSION_LOCK ();
5478 rv = vppcom_session_at_index (session_index, &session);
5479 if (PREDICT_FALSE (rv))
5481 VCL_SESSION_UNLOCK ();
5482 if (VPPCOM_DEBUG > 0)
5483 clib_warning ("VCL<%d>: invalid session, "
5484 "sid (%u) has been closed!",
5485 getpid (), session_index);
5486 if (VPPCOM_DEBUG > 0)
5489 ELOG_TYPE_DECLARE (e) =
5491 .format = "invalid session: %d closed",
5492 .format_args = "i4",
5499 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
5500 ed->data = session_index;
5504 VCL_SESSION_UNLOCK ();
5507 ep->is_ip4 = session->peer_addr.is_ip4;
5508 ep->port = session->peer_port;
5509 if (session->peer_addr.is_ip4)
5510 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
5511 sizeof (ip4_address_t));
5513 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
5514 sizeof (ip6_address_t));
5515 VCL_SESSION_UNLOCK ();
5519 rv = vppcom_session_read (session_index, buffer, buflen);
5520 else if (flags & MSG_PEEK)
5521 rv = vppcom_session_peek (session_index, buffer, buflen);
5524 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
5526 rv = VPPCOM_EAFNOSUPPORT;
5534 vppcom_session_sendto (uint32_t session_index, void *buffer,
5535 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5538 return VPPCOM_EINVAL;
5543 return VPPCOM_EINVAL;
5548 // TBD check the flags and do the right thing
5549 if (VPPCOM_DEBUG > 2)
5550 clib_warning ("VCL<%d>: handling flags 0x%u (%d) "
5551 "not implemented yet.", getpid (), flags, flags);
5554 return (vppcom_session_write (session_index, buffer, buflen));
5558 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
5560 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
5561 u32 i, keep_trying = 1;
5564 if (VPPCOM_DEBUG > 3)
5565 clib_warning ("VCL<%d>: vp %p, nsids %u, wait_for_time %f",
5566 getpid (), vp, n_sids, wait_for_time);
5569 return VPPCOM_EFAULT;
5575 for (i = 0; i < n_sids; i++)
5577 ASSERT (vp[i].revents);
5579 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
5580 VCL_SESSION_UNLOCK ();
5585 if (POLLIN & vp[i].events)
5587 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
5588 rv = vppcom_session_read_ready (session, vp[i].sid);
5589 VCL_SESSION_UNLOCK ();
5592 *vp[i].revents |= POLLIN;
5599 case VPPCOM_ECONNRESET:
5600 *vp[i].revents = POLLHUP;
5604 *vp[i].revents = POLLERR;
5611 if (POLLOUT & vp[i].events)
5613 VCL_SESSION_LOCK_AND_GET (vp[i].sid, &session);
5614 rv = vppcom_session_write_ready (session, vp[i].sid);
5615 VCL_SESSION_UNLOCK ();
5618 *vp[i].revents |= POLLOUT;
5625 case VPPCOM_ECONNRESET:
5626 *vp[i].revents = POLLHUP;
5630 *vp[i].revents = POLLERR;
5637 if (0) // Note "done:" label used by VCL_SESSION_LOCK_AND_GET()
5640 *vp[i].revents = POLLNVAL;
5644 if (wait_for_time != -1)
5645 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
5647 while ((num_ev == 0) && keep_trying);
5649 if (VPPCOM_DEBUG > 3)
5651 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
5652 for (i = 0; i < n_sids; i++)
5654 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
5655 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
5656 vp[i].events, *vp[i].revents);
5663 * VPPCOM Event Functions
5667 vppcom_session_io_thread_fn (void *arg)
5669 vppcom_session_io_thread_t *evt = (vppcom_session_io_thread_t *) arg;
5670 u32 *session_indexes = 0, *session_index;
5677 vec_reset_length (session_indexes);
5678 VCE_IO_SESSIONS_LOCK ();
5679 pool_foreach (session_index, evt->active_session_indexes, (
5686 VCE_IO_SESSIONS_UNLOCK ();
5687 if (session_indexes)
5689 for (i = 0; i < vec_len (session_indexes); ++i)
5691 VCL_SESSION_LOCK_AND_GET (session_indexes[i], &session);
5692 bytes = svm_fifo_max_dequeue (session->rx_fifo);
5693 VCL_SESSION_UNLOCK ();
5697 vppcom_ioevent_t *eio;
5703 pool_get (vcm->event_thread.vce_events, ev);
5704 ev_idx = (u32) (ev - vcm->event_thread.vce_events);
5705 eio = vce_get_event_data (ev, sizeof (*eio));
5706 ev->evk.eid = VCL_EVENT_IOEVENT_RX_FIFO;
5707 ev->evk.session_index = session_indexes[i];
5709 eio->session_index = session_indexes[i];
5711 VCL_EVENTS_UNLOCK ();
5713 rv = vce_generate_event (&vcm->event_thread, ev_idx);
5719 ts.tv_nsec = 1000000; /* 1 millisecond */
5720 nanosleep (&ts, NULL);
5723 VCL_SESSION_UNLOCK ();
5728 vppcom_start_io_event_thread (vppcom_session_io_thread_t * evt,
5731 pthread_cond_init (&(evt->vce_io_cond), NULL);
5732 pthread_mutex_init (&(evt->vce_io_lock), NULL);
5734 clib_spinlock_init (&(evt->io_sessions_lockp));
5736 return pthread_create (&(evt->thread), NULL /* attr */ ,
5737 vppcom_session_io_thread_fn, evt);
5741 vce_registered_ioevent_handler_fn (void *arg)
5743 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
5744 vppcom_ioevent_t *eio;
5746 u32 ioevt_ndx = (u64) (reg->handler_fn_args);
5747 vppcom_session_ioevent_t *ioevent, ioevent_;
5750 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
5751 eio = vce_get_event_data (ev, sizeof (*eio));
5752 VCL_EVENTS_UNLOCK ();
5754 VCL_IO_SESSIONS_LOCK ();
5755 ioevent = pool_elt_at_index (vcm->session_io_thread.ioevents, ioevt_ndx);
5756 ioevent_ = *ioevent;
5757 VCL_IO_SESSIONS_UNLOCK ();
5758 (ioevent_.user_cb) (eio, ioevent_.user_cb_data);
5759 vce_clear_event (&vcm->event_thread, reg->ev_idx);
5762 /*TODO - Unregister check in close for this listener */
5767 vce_registered_listener_connect_handler_fn (void *arg)
5769 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
5770 vce_event_connect_request_t *ecr;
5774 session_t *new_session;
5777 vppcom_session_listener_t *session_listener =
5778 (vppcom_session_listener_t *) reg->handler_fn_args;
5781 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
5782 ecr = vce_get_event_data (ev, sizeof (*ecr));
5783 VCL_EVENTS_UNLOCK ();
5784 VCL_SESSION_LOCK_AND_GET (ecr->accepted_session_index, &new_session);
5786 ep.is_ip4 = new_session->peer_addr.is_ip4;
5787 ep.port = new_session->peer_port;
5788 if (new_session->peer_addr.is_ip4)
5789 clib_memcpy (&ep.ip, &new_session->peer_addr.ip46.ip4,
5790 sizeof (ip4_address_t));
5792 clib_memcpy (&ep.ip, &new_session->peer_addr.ip46.ip6,
5793 sizeof (ip6_address_t));
5795 vppcom_send_accept_session_reply (new_session->vpp_handle,
5796 new_session->client_context,
5797 0 /* retval OK */ );
5798 VCL_SESSION_UNLOCK ();
5800 (session_listener->user_cb) (ecr->accepted_session_index, &ep,
5801 session_listener->user_cb_data);
5803 if (vcm->session_io_thread.io_sessions_lockp)
5805 /* Throw this new accepted session index into the rx poll thread pool */
5806 VCL_IO_SESSIONS_LOCK ();
5807 u32 *active_session_index;
5808 pool_get (vcm->session_io_thread.active_session_indexes,
5809 active_session_index);
5810 *active_session_index = ecr->accepted_session_index;
5811 VCL_IO_SESSIONS_UNLOCK ();
5814 /*TODO - Unregister check in close for this listener */
5818 ASSERT (0); // If we can't get a lock or accepted session fails, lets blow up.
5822 * @brief vce_poll_wait_connect_request_handler_fn
5823 * - used by vppcom_epoll_xxxx() for listener sessions
5824 * - when a vl_api_accept_session_t_handler() generates an event
5825 * this callback is alerted and sets the fields that vppcom_epoll_wait()
5828 * @param arg - void* to be cast to vce_event_handler_reg_t*
5831 vce_poll_wait_connect_request_handler_fn (void *arg)
5833 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
5835 /* Retrieve the VCL_EVENT_CONNECT_REQ_ACCEPTED event */
5836 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
5837 vce_event_connect_request_t *ecr = vce_get_event_data (ev, sizeof (*ecr));
5839 /* Add the accepted_session_index to the FIFO */
5840 VCL_ACCEPT_FIFO_LOCK ();
5841 clib_fifo_add1 (vcm->client_session_index_fifo,
5842 ecr->accepted_session_index);
5843 VCL_ACCEPT_FIFO_UNLOCK ();
5845 /* Recycling the event. */
5848 clib_fifo_add1 (vcm->event_thread.event_index_fifo, reg->ev_idx);
5849 VCL_EVENTS_UNLOCK ();
5853 vppcom_session_register_ioevent_cb (uint32_t session_index,
5854 vppcom_session_ioevent_cb cb,
5855 uint8_t rx, void *ptr)
5858 vce_event_key_t evk;
5859 vppcom_session_ioevent_t *ioevent;
5861 if (!vcm->session_io_thread.io_sessions_lockp)
5862 rv = vppcom_start_io_event_thread (&vcm->session_io_thread, 100 /* DAW_TODO: ??? hard-coded value */
5865 if (rv == VPPCOM_OK)
5869 /* Register handler for ioevent on session_index */
5870 VCL_IO_SESSIONS_LOCK ();
5871 pool_get (vcm->session_io_thread.ioevents, ioevent);
5872 io_evt_ndx = (void *) (ioevent - vcm->session_io_thread.ioevents);
5873 ioevent->user_cb = cb;
5874 ioevent->user_cb_data = ptr;
5875 VCL_IO_SESSIONS_UNLOCK ();
5877 evk.session_index = session_index;
5878 evk.eid = rx ? VCL_EVENT_IOEVENT_RX_FIFO : VCL_EVENT_IOEVENT_TX_FIFO;
5880 (void) vce_register_handler (&vcm->event_thread, &evk,
5881 vce_registered_ioevent_handler_fn,
5888 vppcom_session_register_listener (uint32_t session_index,
5889 vppcom_session_listener_cb cb,
5890 vppcom_session_listener_errcb
5891 errcb, uint8_t flags, int q_len, void *ptr)
5894 vce_event_key_t evk;
5895 vppcom_session_listener_t *listener_args;
5897 if (!vcm->session_io_thread.io_sessions_lockp)
5898 rv = vppcom_start_io_event_thread (&vcm->session_io_thread, 100 /* DAW_TODO: ??? hard-coded value */
5904 rv = vppcom_session_listen (session_index, q_len);
5910 /* Register handler for connect_request event on listen_session_index */
5911 listener_args = clib_mem_alloc (sizeof (vppcom_session_listener_t)); // DAW_TODO: Use a pool instead of thrashing the memory allocator!
5912 listener_args->user_cb = cb;
5913 listener_args->user_cb_data = ptr;
5914 listener_args->user_errcb = errcb;
5916 evk.session_index = session_index;
5917 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
5918 (void) vce_register_handler (&vcm->event_thread, &evk,
5919 vce_registered_listener_connect_handler_fn,
5927 * fd.io coding-style-patch-verification: ON
5930 * eval: (c-set-style "gnu")