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 /* VPPCOM Event typedefs */
194 typedef enum vcl_event_id_
196 VCL_EVENT_INVALID_EVENT,
197 VCL_EVENT_CONNECT_REQ_ACCEPTED,
201 typedef struct vce_event_connect_request_
203 u32 accepted_session_index;
204 } vce_event_connect_request_t;
206 typedef struct vppcom_main_t_
212 /* FIFO for accepted connections - used in epoll/select */
213 clib_spinlock_t session_fifo_lockp;
214 u32 *client_session_index_fifo;
216 /* vpp input queue */
217 svm_queue_t *vl_input_queue;
219 /* API client handle */
222 clib_spinlock_t sessions_lockp;
225 /* Hash table for disconnect processing */
226 uword *session_index_by_vpp_handles;
229 clib_bitmap_t *rd_bitmap;
230 clib_bitmap_t *wr_bitmap;
231 clib_bitmap_t *ex_bitmap;
233 /* Our event queue */
234 svm_queue_t *app_event_queue;
236 /* unique segment name counter */
237 u32 unique_segment_index;
239 /* For deadman timers */
240 clib_time_t clib_time;
242 /* State of the connection, shared between msg RX thread and main thread */
243 volatile app_state_t app_state;
248 vce_event_thread_t event_thread;
250 /* VPP Event-logger */
251 elog_main_t elog_main;
252 elog_track_t elog_track;
254 /* VNET_API_ERROR_FOO -> "Foo" hash table */
255 uword *error_string_by_error_number;
258 /* NOTE: _vppcom_main is only used until the heap is allocated.
259 * Do not access it directly -- use vcm which will point to
260 * the heap allocated copy after init.
262 static vppcom_main_t _vppcom_main = {
263 .debug = VPPCOM_DEBUG_INIT,
264 .my_client_index = ~0
267 static vppcom_main_t *vcm = &_vppcom_main;
269 #define VCL_LOCK_AND_GET_SESSION(I, S) \
271 clib_spinlock_lock (&vcm->sessions_lockp); \
272 rv = vppcom_session_at_index (I, S); \
273 if (PREDICT_FALSE (rv)) \
275 clib_spinlock_unlock (&vcm->sessions_lockp); \
276 clib_warning ("VCL<%d>: ERROR: Invalid ##I (%u)!", \
283 vppcom_app_state_str (app_state_t state)
289 case STATE_APP_START:
290 st = "STATE_APP_START";
293 case STATE_APP_CONN_VPP:
294 st = "STATE_APP_CONN_VPP";
297 case STATE_APP_ENABLED:
298 st = "STATE_APP_ENABLED";
301 case STATE_APP_ATTACHED:
302 st = "STATE_APP_ATTACHED";
306 st = "UNKNOWN_APP_STATE";
314 vppcom_session_state_str (session_state_t state)
325 st = "STATE_CONNECT";
336 case STATE_CLOSE_ON_EMPTY:
337 st = "STATE_CLOSE_ON_EMPTY";
340 case STATE_DISCONNECT:
341 st = "STATE_DISCONNECT";
349 st = "UNKNOWN_STATE";
358 * VPPCOM Utility Functions
361 vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
363 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
364 if (PREDICT_FALSE ((session_index == ~0) ||
365 pool_is_free_index (vcm->sessions, session_index)))
367 clib_warning ("VCL<%d>: invalid session, sid (%u) has been closed!",
368 getpid (), session_index);
369 return VPPCOM_EBADFD;
371 *sess = pool_elt_at_index (vcm->sessions, session_index);
376 vppcom_session_table_add_listener (u64 listener_handle, u32 value)
378 /* Session and listener handles have different formats. The latter has
379 * the thread index in the upper 32 bits while the former has the session
380 * type. Knowing that, for listeners we just flip the MSB to 1 */
381 listener_handle |= 1ULL << 63;
382 hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
385 static inline session_t *
386 vppcom_session_table_lookup_listener (u64 listener_handle)
389 u64 handle = listener_handle | (1ULL << 63);
392 p = hash_get (vcm->session_index_by_vpp_handles, handle);
395 clib_warning ("VCL<%d>: couldn't find listen session: unknown vpp "
396 "listener handle %llx", getpid (), listener_handle);
399 if (pool_is_free_index (vcm->sessions, p[0]))
401 if (VPPCOM_DEBUG > 1)
402 clib_warning ("VCL<%d>: invalid listen session, sid (%u)",
407 session = pool_elt_at_index (vcm->sessions, p[0]);
408 ASSERT (session->state & STATE_LISTEN);
413 vppcom_session_table_del_listener (u64 listener_handle)
415 listener_handle |= 1ULL << 63;
416 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
422 elog_main_t *em = &vcm->elog_main;
424 clib_error_t *error = 0;
427 (char *) format (0, "%s/%d-%d-vcl-elog%c", vcm->cfg.event_log_path,
428 vcm->my_client_index, getpid (), 0);
429 error = elog_write_file (em, chroot_file, 1 /* flush ring */ );
432 clib_error_report (error);
434 if (VPPCOM_DEBUG > 0)
435 clib_warning ("[%d] Event Log:'%s' ", getpid (), chroot_file);
440 * VPPCOM Event Functions
444 * * @brief vce_connect_request_handler_fn
445 * - used for listener sessions
446 * - when a vl_api_accept_session_t_handler() generates an event
447 * this callback is alerted and sets fields that consumers such as
448 * vppcom_session_accept() expect to see, ie. accepted_client_index
450 * @param arg - void* to be cast to vce_event_handler_reg_t*
453 vce_connect_request_handler_fn (void *arg)
455 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
457 pthread_mutex_lock (®->handler_lock);
458 pthread_cond_signal (®->handler_cond);
459 pthread_mutex_unlock (®->handler_lock);
463 * @brief vce_epoll_wait_connect_request_handler_fn
464 * - used by vppcom_epoll_xxxx() for listener sessions
465 * - when a vl_api_accept_session_t_handler() generates an event
466 * this callback is alerted and sets the fields that vppcom_epoll_wait()
469 * @param arg - void* to be cast to vce_event_handler_reg_t*
472 vce_epoll_wait_connect_request_handler_fn (void *arg)
474 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
476 /* Retrieve the VCL_EVENT_CONNECT_REQ_ACCEPTED event */
477 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
478 vce_event_connect_request_t *ecr = (vce_event_connect_request_t *) ev->data;
480 /* Add the accepted_session_index to the FIFO */
481 clib_spinlock_lock (&vcm->session_fifo_lockp);
482 clib_fifo_add1 (vcm->client_session_index_fifo,
483 ecr->accepted_session_index);
484 clib_spinlock_unlock (&vcm->session_fifo_lockp);
486 /* Recycling the event. */
487 clib_spinlock_lock (&(vcm->event_thread.events_lockp));
488 vcm->event_thread.recycle_event = 1;
489 clib_fifo_add1 (vcm->event_thread.event_index_fifo, reg->ev_idx);
490 clib_spinlock_unlock (&(vcm->event_thread.events_lockp));
494 vppcom_connect_to_vpp (char *app_name)
496 api_main_t *am = &api_main;
497 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
500 if (!vcl_cfg->vpp_api_filename)
501 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
503 if (VPPCOM_DEBUG > 0)
504 clib_warning ("VCL<%d>: app (%s) connecting to VPP api (%s)...",
505 getpid (), app_name, vcl_cfg->vpp_api_filename);
507 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename, app_name,
508 vcm->cfg.vpp_api_q_length) < 0)
510 clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (), app_name);
511 rv = VPPCOM_ECONNREFUSED;
515 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
516 vcm->my_client_index = (u32) am->my_client_index;
517 vcm->app_state = STATE_APP_CONN_VPP;
519 if (VPPCOM_DEBUG > 0)
520 clib_warning ("VCL<%d>: app (%s) is connected to VPP!",
521 getpid (), app_name);
524 if (VPPCOM_DEBUG > 0)
526 vcm->elog_main.lock =
527 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
528 vcm->elog_main.lock[0] = 0;
529 vcm->elog_main.event_ring_size = vcm->cfg.event_ring_size;
530 elog_init (&vcm->elog_main, vcm->elog_main.event_ring_size);
531 elog_enable_disable (&vcm->elog_main, 1);
533 vcm->elog_track.name =
534 (char *) format (0, "P:%d:C:%d%c", getpid (),
535 vcm->my_client_index, 0);
536 elog_track_register (&vcm->elog_main, &vcm->elog_track);
539 ELOG_TYPE_DECLARE (e) =
541 .format = "connect_vpp:rv:%d",
548 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
556 format_api_error (u8 * s, va_list * args)
558 i32 error = va_arg (*args, u32);
561 p = hash_get (vcm->error_string_by_error_number, -error);
564 s = format (s, "%s (%d)", p[0], error);
566 s = format (s, "%d", error);
571 vppcom_init_error_string_table (void)
573 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
575 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
576 foreach_vnet_api_error;
579 hash_set (vcm->error_string_by_error_number, 99, "Misc");
583 vppcom_wait_for_app_state_change (app_state_t app_state)
585 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
587 while (clib_time_now (&vcm->clib_time) < timeout)
589 if (vcm->app_state == app_state)
592 if (VPPCOM_DEBUG > 0)
593 clib_warning ("VCL<%d>: timeout waiting for state %s (%d)", getpid (),
594 vppcom_app_state_str (app_state), app_state);
596 if (VPPCOM_DEBUG > 0)
599 ELOG_TYPE_DECLARE (e) =
601 .format = "ERR: timeout state:%d",
609 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
611 ed->data = app_state;
615 return VPPCOM_ETIMEDOUT;
619 vppcom_wait_for_session_state_change (u32 session_index,
620 session_state_t state,
623 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
624 session_t *volatile session;
629 clib_spinlock_lock (&vcm->sessions_lockp);
630 rv = vppcom_session_at_index (session_index, &session);
631 if (PREDICT_FALSE (rv))
633 clib_spinlock_unlock (&vcm->sessions_lockp);
636 if (session->state & state)
638 clib_spinlock_unlock (&vcm->sessions_lockp);
641 if (session->state & STATE_FAILED)
643 clib_spinlock_unlock (&vcm->sessions_lockp);
644 return VPPCOM_ECONNREFUSED;
647 clib_spinlock_unlock (&vcm->sessions_lockp);
649 while (clib_time_now (&vcm->clib_time) < timeout);
651 if (VPPCOM_DEBUG > 0)
652 clib_warning ("VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (),
653 state, vppcom_session_state_str (state));
655 if (VPPCOM_DEBUG > 0)
658 ELOG_TYPE_DECLARE (e) =
660 .format = "ERR: timeout state:%d",
668 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
674 return VPPCOM_ETIMEDOUT;
678 * VPP-API message functions
681 vppcom_send_session_enable_disable (u8 is_enable)
683 vl_api_session_enable_disable_t *bmp;
684 bmp = vl_msg_api_alloc (sizeof (*bmp));
685 memset (bmp, 0, sizeof (*bmp));
687 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
688 bmp->client_index = vcm->my_client_index;
689 bmp->context = htonl (0xfeedface);
690 bmp->is_enable = is_enable;
691 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
695 vppcom_app_session_enable (void)
699 if (vcm->app_state != STATE_APP_ENABLED)
701 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
702 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
703 if (PREDICT_FALSE (rv))
705 if (VPPCOM_DEBUG > 0)
706 clib_warning ("VCL<%d>: application session enable timed out! "
708 getpid (), rv, vppcom_retval_str (rv));
716 vl_api_session_enable_disable_reply_t_handler
717 (vl_api_session_enable_disable_reply_t * mp)
721 clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
722 format_api_error, ntohl (mp->retval));
725 vcm->app_state = STATE_APP_ENABLED;
729 vppcom_app_send_attach (void)
731 vl_api_application_attach_t *bmp;
732 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
733 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
734 vcm->cfg.app_proxy_transport_udp);
736 bmp = vl_msg_api_alloc (sizeof (*bmp));
737 memset (bmp, 0, sizeof (*bmp));
739 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
740 bmp->client_index = vcm->my_client_index;
741 bmp->context = htonl (0xfeedface);
742 bmp->options[APP_OPTIONS_FLAGS] =
743 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
744 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
745 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
746 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
747 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
748 (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
749 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
750 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
751 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
752 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
753 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
754 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
755 vcm->cfg.preallocated_fifo_pairs;
756 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
759 bmp->namespace_id_len = nsid_len;
760 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
761 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
763 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
767 vppcom_app_attach (void)
771 vppcom_app_send_attach ();
772 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
773 if (PREDICT_FALSE (rv))
775 if (VPPCOM_DEBUG > 0)
776 clib_warning ("VCL<%d>: application attach timed out! "
778 getpid (), rv, vppcom_retval_str (rv));
786 vppcom_app_detach (void)
788 vl_api_application_detach_t *bmp;
789 bmp = vl_msg_api_alloc (sizeof (*bmp));
790 memset (bmp, 0, sizeof (*bmp));
792 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
793 bmp->client_index = vcm->my_client_index;
794 bmp->context = htonl (0xfeedface);
795 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
799 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
802 static svm_fifo_segment_create_args_t _a;
803 svm_fifo_segment_create_args_t *a = &_a;
806 memset (a, 0, sizeof (*a));
809 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
810 format_api_error, ntohl (mp->retval));
814 if (mp->segment_name_length == 0)
816 clib_warning ("VCL<%d>: segment_name_length zero", getpid ());
820 a->segment_name = (char *) mp->segment_name;
821 a->segment_size = mp->segment_size;
823 ASSERT (mp->app_event_queue_address);
825 /* Attach to the segment vpp created */
826 rv = svm_fifo_segment_attach (a);
827 vec_reset_length (a->new_segment_indices);
828 if (PREDICT_FALSE (rv))
830 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
831 getpid (), mp->segment_name);
835 vcm->app_event_queue =
836 uword_to_pointer (mp->app_event_queue_address, svm_queue_t *);
838 vcm->app_state = STATE_APP_ATTACHED;
842 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
846 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
849 vcm->app_state = STATE_APP_ENABLED;
853 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
857 clib_warning ("VCL<%d>: vpp handle 0x%llx: disconnect session failed: %U",
858 getpid (), mp->handle, format_api_error,
863 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
865 static svm_fifo_segment_create_args_t _a;
866 svm_fifo_segment_create_args_t *a = &_a;
869 memset (a, 0, sizeof (*a));
870 a->segment_name = (char *) mp->segment_name;
871 a->segment_size = mp->segment_size;
872 /* Attach to the segment vpp created */
873 rv = svm_fifo_segment_attach (a);
874 vec_reset_length (a->new_segment_indices);
875 if (PREDICT_FALSE (rv))
877 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
878 getpid (), mp->segment_name);
881 if (VPPCOM_DEBUG > 1)
882 clib_warning ("VCL<%d>: mapped new segment '%s' size %d", getpid (),
883 mp->segment_name, mp->segment_size);
887 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
891 * XXX Need segment_name to session_id hash,
892 * XXX - have sessionID by handle hash currently
894 if (VPPCOM_DEBUG > 1)
895 clib_warning ("Unmapped segment '%s'", mp->segment_name);
899 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
903 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
907 session_t *session = 0;
908 u32 session_index = p[0];
910 VCL_LOCK_AND_GET_SESSION (session_index, &session);
911 session->state = STATE_CLOSE_ON_EMPTY;
913 if (VPPCOM_DEBUG > 1)
914 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
915 "setting state to 0x%x (%s)",
916 getpid (), mp->handle, session_index, session->state,
917 vppcom_session_state_str (session->state));
918 clib_spinlock_unlock (&vcm->sessions_lockp);
922 if (VPPCOM_DEBUG > 1)
923 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
924 "session lookup failed!",
925 getpid (), mp->handle, session_index);
928 clib_warning ("VCL<%d>: vpp handle 0x%llx: session lookup by "
929 "handle failed!", getpid (), mp->handle);
933 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
935 session_t *session = 0;
936 vl_api_reset_session_reply_t *rmp;
940 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
944 clib_spinlock_lock (&vcm->sessions_lockp);
945 rval = vppcom_session_at_index (p[0], &session);
946 if (PREDICT_FALSE (rval))
948 rv = VNET_API_ERROR_INVALID_VALUE_2;
949 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
950 "session lookup failed! returning %d %U",
951 getpid (), mp->handle, p[0],
952 rv, format_api_error, rv);
956 /* TBD: should this disconnect immediately and
959 session->state = STATE_CLOSE_ON_EMPTY;
961 if (VPPCOM_DEBUG > 1)
962 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
963 "state set to %d (%s)!", getpid (),
964 mp->handle, p[0], session->state,
965 vppcom_session_state_str (session->state));
967 clib_spinlock_unlock (&vcm->sessions_lockp);
971 rv = VNET_API_ERROR_INVALID_VALUE;
972 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx: session lookup "
973 "failed! returning %d %U",
974 getpid (), mp->handle, rv, format_api_error, rv);
977 rmp = vl_msg_api_alloc (sizeof (*rmp));
978 memset (rmp, 0, sizeof (*rmp));
979 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
980 rmp->retval = htonl (rv);
981 rmp->handle = mp->handle;
982 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
986 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
988 session_t *session = 0;
990 svm_fifo_t *rx_fifo, *tx_fifo;
993 session_index = mp->context;
994 VCL_LOCK_AND_GET_SESSION (session_index, &session);
998 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
999 "connect failed! %U",
1000 getpid (), mp->handle, session_index,
1001 format_api_error, ntohl (mp->retval));
1004 session->state = STATE_FAILED;
1005 session->vpp_handle = mp->handle;
1009 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
1010 "Invalid session index (%u)!",
1011 getpid (), mp->handle, session_index);
1022 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1025 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1026 rx_fifo->client_session_index = session_index;
1027 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1028 tx_fifo->client_session_index = session_index;
1030 session->rx_fifo = rx_fifo;
1031 session->tx_fifo = tx_fifo;
1032 session->vpp_handle = mp->handle;
1033 session->lcl_addr.is_ip4 = mp->is_ip4;
1034 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
1035 sizeof (session->peer_addr.ip46));
1036 session->lcl_port = mp->lcl_port;
1037 session->state = STATE_CONNECT;
1039 /* Add it to lookup table */
1040 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1042 if (VPPCOM_DEBUG > 1)
1043 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded!"
1044 " session_rx_fifo %p, refcnt %d,"
1045 " session_tx_fifo %p, refcnt %d",
1046 getpid (), mp->handle, session_index,
1048 session->rx_fifo->refcnt,
1049 session->tx_fifo, session->tx_fifo->refcnt);
1051 clib_spinlock_unlock (&vcm->sessions_lockp);
1055 vppcom_send_connect_sock (session_t * session, u32 session_index)
1057 vl_api_connect_sock_t *cmp;
1059 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
1060 cmp = vl_msg_api_alloc (sizeof (*cmp));
1061 memset (cmp, 0, sizeof (*cmp));
1062 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
1063 cmp->client_index = vcm->my_client_index;
1064 cmp->context = session_index;
1066 cmp->is_ip4 = session->peer_addr.is_ip4;
1067 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
1068 cmp->port = session->peer_port;
1069 cmp->proto = session->proto;
1070 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
1071 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
1075 vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
1078 vl_api_disconnect_session_reply_t *rmp;
1080 if (VPPCOM_DEBUG > 1)
1081 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1082 "sending disconnect msg",
1083 getpid (), vpp_handle, session_index);
1085 rmp = vl_msg_api_alloc (sizeof (*rmp));
1086 memset (rmp, 0, sizeof (*rmp));
1088 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
1089 rmp->retval = htonl (rv);
1090 rmp->handle = vpp_handle;
1091 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1095 vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
1097 vl_api_disconnect_session_t *dmp;
1099 if (VPPCOM_DEBUG > 1)
1100 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1101 "sending disconnect msg",
1102 getpid (), vpp_handle, session_index);
1104 dmp = vl_msg_api_alloc (sizeof (*dmp));
1105 memset (dmp, 0, sizeof (*dmp));
1106 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
1107 dmp->client_index = vcm->my_client_index;
1108 dmp->handle = vpp_handle;
1109 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
1113 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
1115 session_t *session = 0;
1116 u32 session_index = mp->context;
1119 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1123 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, "
1124 "sid %u: bind failed: %U",
1125 getpid (), mp->handle, session_index,
1126 format_api_error, ntohl (mp->retval));
1127 rv = vppcom_session_at_index (session_index, &session);
1128 if (rv == VPPCOM_OK)
1130 session->state = STATE_FAILED;
1131 session->vpp_handle = mp->handle;
1135 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
1136 "Invalid session index (%u)!",
1137 getpid (), mp->handle, session_index);
1142 session->vpp_handle = mp->handle;
1143 session->lcl_addr.is_ip4 = mp->lcl_is_ip4;
1144 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
1145 sizeof (session->peer_addr.ip46));
1146 session->lcl_port = mp->lcl_port;
1147 vppcom_session_table_add_listener (mp->handle, session_index);
1148 session->state = STATE_LISTEN;
1150 if (VPPCOM_DEBUG > 1)
1151 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
1152 getpid (), mp->handle, mp->context);
1154 clib_spinlock_unlock (&vcm->sessions_lockp);
1158 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
1161 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
1162 getpid (), mp->context, format_api_error,
1163 ntohl (mp->retval));
1165 else if (VPPCOM_DEBUG > 1)
1166 clib_warning ("VCL<%d>: sid %u: unbind succeeded!",
1167 getpid (), mp->context);
1171 format_ip4_address (u8 * s, va_list * args)
1173 u8 *a = va_arg (*args, u8 *);
1174 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1178 format_ip6_address (u8 * s, va_list * args)
1180 ip6_address_t *a = va_arg (*args, ip6_address_t *);
1181 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
1183 i_max_n_zero = ARRAY_LEN (a->as_u16);
1185 i_first_zero = i_max_n_zero;
1187 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1189 u32 is_zero = a->as_u16[i] == 0;
1190 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1196 if ((!is_zero && n_zeros > max_n_zeros)
1197 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1199 i_max_n_zero = i_first_zero;
1200 max_n_zeros = n_zeros;
1201 i_first_zero = ARRAY_LEN (a->as_u16);
1206 last_double_colon = 0;
1207 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1209 if (i == i_max_n_zero && max_n_zeros > 1)
1211 s = format (s, "::");
1212 i += max_n_zeros - 1;
1213 last_double_colon = 1;
1217 s = format (s, "%s%x",
1218 (last_double_colon || i == 0) ? "" : ":",
1219 clib_net_to_host_u16 (a->as_u16[i]));
1220 last_double_colon = 0;
1227 /* Format an IP46 address. */
1229 format_ip46_address (u8 * s, va_list * args)
1231 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1232 ip46_type_t type = va_arg (*args, ip46_type_t);
1238 is_ip4 = ip46_address_is_ip4 (ip46);
1249 format (s, "%U", format_ip4_address, &ip46->ip4) :
1250 format (s, "%U", format_ip6_address, &ip46->ip6);
1254 vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
1256 vl_api_accept_session_reply_t *rmp;
1258 rmp = vl_msg_api_alloc (sizeof (*rmp));
1259 memset (rmp, 0, sizeof (*rmp));
1260 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1261 rmp->retval = htonl (retval);
1262 rmp->context = context;
1263 rmp->handle = handle;
1264 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1268 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1270 svm_fifo_t *rx_fifo, *tx_fifo;
1271 session_t *session, *listen_session;
1273 vce_event_connect_request_t *ecr;
1279 clib_spinlock_lock (&vcm->sessions_lockp);
1281 clib_spinlock_lock (&vcm->session_fifo_lockp);
1282 elts = clib_fifo_free_elts (vcm->client_session_index_fifo);
1283 clib_spinlock_unlock (&vcm->session_fifo_lockp);
1287 clib_warning ("VCL<%d>: client session queue is full!", getpid ());
1288 vppcom_send_accept_session_reply (mp->handle, mp->context,
1289 VNET_API_ERROR_QUEUE_FULL);
1290 clib_spinlock_unlock (&vcm->sessions_lockp);
1294 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1295 if (!listen_session)
1297 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
1298 "unknown vpp listener handle %llx",
1299 getpid (), mp->listener_handle);
1300 vppcom_send_accept_session_reply (mp->handle, mp->context,
1301 VNET_API_ERROR_INVALID_ARGUMENT);
1302 clib_spinlock_unlock (&vcm->sessions_lockp);
1306 /* TODO check listener depth and update */
1307 /* TODO on "child" fd close, update listener depth */
1309 /* Allocate local session and set it up */
1310 pool_get (vcm->sessions, session);
1311 memset (session, 0, sizeof (*session));
1312 session_index = session - vcm->sessions;
1314 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1315 rx_fifo->client_session_index = session_index;
1316 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1317 tx_fifo->client_session_index = session_index;
1319 session->vpp_handle = mp->handle;
1320 session->client_context = mp->context;
1321 session->rx_fifo = rx_fifo;
1322 session->tx_fifo = tx_fifo;
1323 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1325 session->state = STATE_ACCEPT;
1326 session->peer_port = mp->port;
1327 session->peer_addr.is_ip4 = mp->is_ip4;
1328 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1329 sizeof (session->peer_addr.ip46));
1331 /* Add it to lookup table */
1332 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1333 session->lcl_port = listen_session->lcl_port;
1334 session->lcl_addr = listen_session->lcl_addr;
1336 /* Create an event for handlers */
1338 clib_spinlock_lock (&vcm->event_thread.events_lockp);
1340 pool_get (vcm->event_thread.vce_events, ev);
1341 ev->data = clib_mem_alloc (sizeof (vce_event_connect_request_t));
1343 ev_idx = (u32) (ev - vcm->event_thread.vce_events);
1345 ev->evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
1346 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1347 ev->evk.session_index = (u32) (listen_session - vcm->sessions);
1348 ecr->accepted_session_index = session_index;
1350 clib_spinlock_unlock (&vcm->event_thread.events_lockp);
1352 rv = vce_generate_event (&vcm->event_thread, ev_idx);
1356 if (VPPCOM_DEBUG > 1)
1357 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client accept "
1358 "request from %s address %U port %d queue %p!", getpid (),
1359 mp->handle, session_index, mp->is_ip4 ? "IPv4" : "IPv6",
1360 format_ip46_address, &mp->ip, mp->is_ip4,
1361 clib_net_to_host_u16 (mp->port), session->vpp_event_queue);
1363 if (VPPCOM_DEBUG > 0)
1365 session->elog_track.name =
1366 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
1368 elog_track_register (&vcm->elog_main, &session->elog_track);
1370 if (session->peer_addr.is_ip4)
1373 ELOG_TYPE_DECLARE (e) =
1376 "client_accept:handle:%x addr:%d.%d.%d.%d:%d",
1377 .format_args = "i8i1i1i1i1i2",
1380 CLIB_PACKED (struct {
1386 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1388 ed->handle = mp->handle;
1389 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
1390 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
1391 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
1392 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
1393 ed->port = clib_net_to_host_u16 (session->peer_port);
1398 clib_warning ("ip6");
1402 clib_spinlock_unlock (&vcm->sessions_lockp);
1407 vppcom_send_bind_sock (session_t * session, u32 session_index)
1409 vl_api_bind_sock_t *bmp;
1411 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1412 bmp = vl_msg_api_alloc (sizeof (*bmp));
1413 memset (bmp, 0, sizeof (*bmp));
1415 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1416 bmp->client_index = vcm->my_client_index;
1417 bmp->context = session_index;
1418 bmp->is_ip4 = session->lcl_addr.is_ip4;
1419 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1420 bmp->port = session->lcl_port;
1421 bmp->proto = session->proto;
1422 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1423 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1427 vppcom_send_unbind_sock (u64 vpp_handle)
1429 vl_api_unbind_sock_t *ump;
1431 ump = vl_msg_api_alloc (sizeof (*ump));
1432 memset (ump, 0, sizeof (*ump));
1434 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1435 ump->client_index = vcm->my_client_index;
1436 ump->handle = vpp_handle;
1437 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1441 vppcom_session_unbind (u32 session_index)
1443 session_t *session = 0;
1446 elog_track_t session_elog_track;
1448 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1450 vpp_handle = session->vpp_handle;
1451 vppcom_session_table_del_listener (vpp_handle);
1452 session->vpp_handle = ~0;
1453 session->state = STATE_DISCONNECT;
1454 session_elog_track = session->elog_track;
1456 clib_spinlock_unlock (&vcm->sessions_lockp);
1458 if (VPPCOM_DEBUG > 1)
1459 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1460 "sending unbind msg! new state 0x%x (%s)",
1461 getpid (), vpp_handle, session_index,
1463 vppcom_session_state_str (STATE_DISCONNECT));
1465 if (VPPCOM_DEBUG > 0)
1468 ELOG_TYPE_DECLARE (e) =
1470 .format = "unbind: handle:%x",
1471 .format_args = "i8",
1479 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
1480 ed->handle = vpp_handle;
1484 vppcom_send_unbind_sock (vpp_handle);
1491 vppcom_session_disconnect (u32 session_index)
1496 session_state_t state;
1498 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1500 vpp_handle = session->vpp_handle;
1501 state = session->state;
1502 clib_spinlock_unlock (&vcm->sessions_lockp);
1504 if (VPPCOM_DEBUG > 1)
1506 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u "
1508 getpid (), vpp_handle, session_index,
1509 state, vppcom_session_state_str (state));
1512 if (PREDICT_FALSE (state & STATE_LISTEN))
1514 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1515 "Cannot disconnect a listen socket!",
1516 getpid (), vpp_handle, session_index);
1521 /* The peer has already initiated the close,
1522 * so send the disconnect session reply.
1524 if (state & STATE_CLOSE_ON_EMPTY)
1526 //XXX alagalah - Check and drain here?
1527 vppcom_send_disconnect_session_reply (vpp_handle,
1528 session_index, 0 /* rv */ );
1529 if (VPPCOM_DEBUG > 1)
1530 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1531 "sending disconnect REPLY...",
1532 getpid (), vpp_handle, session_index);
1535 /* Otherwise, send a disconnect session msg...
1539 if (VPPCOM_DEBUG > 1)
1540 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1541 "sending disconnect...",
1542 getpid (), vpp_handle, session_index);
1544 vppcom_send_disconnect_session (vpp_handle, session_index);
1551 #define foreach_sock_msg \
1552 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1553 _(BIND_SOCK_REPLY, bind_sock_reply) \
1554 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1555 _(ACCEPT_SESSION, accept_session) \
1556 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1557 _(DISCONNECT_SESSION, disconnect_session) \
1558 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1559 _(RESET_SESSION, reset_session) \
1560 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1561 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1562 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
1563 _(UNMAP_SEGMENT, unmap_segment)
1566 vppcom_api_hookup (void)
1569 vl_msg_api_set_handlers(VL_API_##N, #n, \
1570 vl_api_##n##_t_handler, \
1572 vl_api_##n##_t_endian, \
1573 vl_api_##n##_t_print, \
1574 sizeof(vl_api_##n##_t), 1);
1580 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1584 vcl_cfg->heapsize = (256ULL << 20);
1585 vcl_cfg->vpp_api_q_length = 1024;
1586 vcl_cfg->segment_baseva = 0x200000000ULL;
1587 vcl_cfg->segment_size = (256 << 20);
1588 vcl_cfg->add_segment_size = (128 << 20);
1589 vcl_cfg->preallocated_fifo_pairs = 8;
1590 vcl_cfg->rx_fifo_size = (1 << 20);
1591 vcl_cfg->tx_fifo_size = (1 << 20);
1592 vcl_cfg->event_queue_size = 2048;
1593 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1594 vcl_cfg->app_timeout = 10 * 60.0;
1595 vcl_cfg->session_timeout = 10 * 60.0;
1596 vcl_cfg->accept_timeout = 60.0;
1597 vcl_cfg->event_ring_size = (128 << 10);
1598 vcl_cfg->event_log_path = "/dev/shm";
1602 vppcom_cfg_heapsize (char *conf_fname)
1604 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1617 fp = fopen (conf_fname, "r");
1620 if (VPPCOM_DEBUG > 0)
1621 clib_warning ("VCL<%d>: using default heapsize %lld (0x%llx)",
1622 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1626 argv = calloc (1, sizeof (char *));
1629 if (VPPCOM_DEBUG > 0)
1630 clib_warning ("VCL<%d>: calloc failed, using default "
1631 "heapsize %lld (0x%llx)",
1632 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1638 if (fgets (inbuf, 4096, fp) == 0)
1640 p = strtok (inbuf, " \t\n");
1646 char **tmp = realloc (argv, argc * sizeof (char *));
1649 if (VPPCOM_DEBUG > 0)
1650 clib_warning ("VCL<%d>: realloc failed, "
1651 "using default heapsize %lld (0x%llx)",
1652 getpid (), vcl_cfg->heapsize,
1657 arg = strndup (p, 1024);
1660 if (VPPCOM_DEBUG > 0)
1661 clib_warning ("VCL<%d>: strndup failed, "
1662 "using default heapsize %lld (0x%llx)",
1663 getpid (), vcl_cfg->heapsize,
1667 argv[argc - 1] = arg;
1668 p = strtok (NULL, " \t\n");
1675 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1678 if (VPPCOM_DEBUG > 0)
1679 clib_warning ("VCL<%d>: realloc failed, "
1680 "using default heapsize %lld (0x%llx)",
1681 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1688 * Look for and parse the "heapsize" config parameter.
1689 * Manual since none of the clib infra has been bootstrapped yet.
1691 * Format: heapsize <nn>[mM][gG]
1694 for (i = 1; i < (argc - 1); i++)
1696 if (!strncmp (argv[i], "heapsize", 8))
1698 sizep = (u8 *) argv[i + 1];
1700 while (*sizep >= '0' && *sizep <= '9')
1703 size += *sizep++ - '0';
1707 if (VPPCOM_DEBUG > 0)
1708 clib_warning ("VCL<%d>: parse error '%s %s', "
1709 "using default heapsize %lld (0x%llx)",
1710 getpid (), argv[i], argv[i + 1],
1711 vcl_cfg->heapsize, vcl_cfg->heapsize);
1715 if (*sizep == 'g' || *sizep == 'G')
1716 vcl_cfg->heapsize = size << 30;
1717 else if (*sizep == 'm' || *sizep == 'M')
1718 vcl_cfg->heapsize = size << 20;
1721 if (VPPCOM_DEBUG > 0)
1722 clib_warning ("VCL<%d>: parse error '%s %s', "
1723 "using default heapsize %lld (0x%llx)",
1724 getpid (), argv[i], argv[i + 1],
1725 vcl_cfg->heapsize, vcl_cfg->heapsize);
1737 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1738 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1739 if (vcl_mem == MAP_FAILED)
1741 clib_unix_error ("VCL<%d>: ERROR: mmap(0, %lld == 0x%llx, "
1742 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1744 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1745 ASSERT (vcl_mem != MAP_FAILED);
1748 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1751 clib_warning ("VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
1755 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1758 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
1763 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1766 if (VPPCOM_DEBUG > 0)
1767 clib_warning ("VCL<%d>: allocated VCL heap = %p, size %lld (0x%llx)",
1768 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
1772 vppcom_cfg_read (char *conf_fname)
1774 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1776 unformat_input_t _input, *input = &_input;
1777 unformat_input_t _line_input, *line_input = &_line_input;
1778 u8 vc_cfg_input = 0;
1781 u32 uid, gid, q_len;
1783 fd = open (conf_fname, O_RDONLY);
1786 if (VPPCOM_DEBUG > 0)
1787 clib_warning ("VCL<%d>: using default configuration.",
1788 getpid (), conf_fname);
1792 if (fstat (fd, &s) < 0)
1794 if (VPPCOM_DEBUG > 0)
1795 clib_warning ("VCL<%d>: failed to stat `%s', "
1796 "using default configuration", getpid (), conf_fname);
1800 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1802 if (VPPCOM_DEBUG > 0)
1803 clib_warning ("VCL<%d>: not a regular file `%s', "
1804 "using default configuration", getpid (), conf_fname);
1808 unformat_init_clib_file (input, fd);
1810 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1812 (void) unformat_user (input, unformat_line_input, line_input);
1813 unformat_skip_white_space (line_input);
1815 if (unformat (line_input, "vcl {"))
1823 if (unformat (line_input, "heapsize %s", &chroot_path))
1825 vec_terminate_c_string (chroot_path);
1826 if (VPPCOM_DEBUG > 0)
1827 clib_warning ("VCL<%d>: configured heapsize %s, "
1828 "actual heapsize %lld (0x%llx)",
1829 getpid (), chroot_path, vcl_cfg->heapsize,
1831 vec_free (chroot_path);
1833 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1835 vec_terminate_c_string (chroot_path);
1836 if (vcl_cfg->vpp_api_filename)
1837 vec_free (vcl_cfg->vpp_api_filename);
1838 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
1840 vl_set_memory_root_path ((char *) chroot_path);
1842 if (VPPCOM_DEBUG > 0)
1843 clib_warning ("VCL<%d>: configured api-prefix (%s) and "
1844 "api filename (%s)", getpid (), chroot_path,
1845 vcl_cfg->vpp_api_filename);
1846 chroot_path = 0; /* Don't vec_free() it! */
1848 else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
1850 if (q_len < vcl_cfg->vpp_api_q_length)
1852 clib_warning ("VCL<%d>: ERROR: configured vpp-api-q-length "
1853 "(%u) is too small! Using default: %u ",
1854 getpid (), q_len, vcl_cfg->vpp_api_q_length);
1858 vcl_cfg->vpp_api_q_length = q_len;
1860 if (VPPCOM_DEBUG > 0)
1861 clib_warning ("VCL<%d>: configured vpp-api-q-length %u",
1862 getpid (), vcl_cfg->vpp_api_q_length);
1865 else if (unformat (line_input, "uid %d", &uid))
1867 vl_set_memory_uid (uid);
1868 if (VPPCOM_DEBUG > 0)
1869 clib_warning ("VCL<%d>: configured uid %d", getpid (), uid);
1871 else if (unformat (line_input, "gid %d", &gid))
1873 vl_set_memory_gid (gid);
1874 if (VPPCOM_DEBUG > 0)
1875 clib_warning ("VCL<%d>: configured gid %d", getpid (), gid);
1877 else if (unformat (line_input, "segment-baseva 0x%lx",
1878 &vcl_cfg->segment_baseva))
1880 if (VPPCOM_DEBUG > 0)
1881 clib_warning ("VCL<%d>: configured segment_baseva 0x%lx",
1882 getpid (), vcl_cfg->segment_baseva);
1884 else if (unformat (line_input, "segment-size 0x%lx",
1885 &vcl_cfg->segment_size))
1887 if (VPPCOM_DEBUG > 0)
1888 clib_warning ("VCL<%d>: configured segment_size 0x%lx (%ld)",
1889 getpid (), vcl_cfg->segment_size,
1890 vcl_cfg->segment_size);
1892 else if (unformat (line_input, "segment-size %ld",
1893 &vcl_cfg->segment_size))
1895 if (VPPCOM_DEBUG > 0)
1896 clib_warning ("VCL<%d>: configured segment_size %ld (0x%lx)",
1897 getpid (), vcl_cfg->segment_size,
1898 vcl_cfg->segment_size);
1900 else if (unformat (line_input, "add-segment-size 0x%lx",
1901 &vcl_cfg->add_segment_size))
1903 if (VPPCOM_DEBUG > 0)
1905 ("VCL<%d>: configured add_segment_size 0x%lx (%ld)",
1906 getpid (), vcl_cfg->add_segment_size,
1907 vcl_cfg->add_segment_size);
1909 else if (unformat (line_input, "add-segment-size %ld",
1910 &vcl_cfg->add_segment_size))
1912 if (VPPCOM_DEBUG > 0)
1914 ("VCL<%d>: configured add_segment_size %ld (0x%lx)",
1915 getpid (), vcl_cfg->add_segment_size,
1916 vcl_cfg->add_segment_size);
1918 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1919 &vcl_cfg->preallocated_fifo_pairs))
1921 if (VPPCOM_DEBUG > 0)
1922 clib_warning ("VCL<%d>: configured preallocated_fifo_pairs "
1923 "%d (0x%x)", getpid (),
1924 vcl_cfg->preallocated_fifo_pairs,
1925 vcl_cfg->preallocated_fifo_pairs);
1927 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1928 &vcl_cfg->rx_fifo_size))
1930 if (VPPCOM_DEBUG > 0)
1931 clib_warning ("VCL<%d>: configured rx_fifo_size 0x%lx (%ld)",
1932 getpid (), vcl_cfg->rx_fifo_size,
1933 vcl_cfg->rx_fifo_size);
1935 else if (unformat (line_input, "rx-fifo-size %ld",
1936 &vcl_cfg->rx_fifo_size))
1938 if (VPPCOM_DEBUG > 0)
1939 clib_warning ("VCL<%d>: configured rx_fifo_size %ld (0x%lx)",
1940 getpid (), vcl_cfg->rx_fifo_size,
1941 vcl_cfg->rx_fifo_size);
1943 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1944 &vcl_cfg->tx_fifo_size))
1946 if (VPPCOM_DEBUG > 0)
1947 clib_warning ("VCL<%d>: configured tx_fifo_size 0x%lx (%ld)",
1948 getpid (), vcl_cfg->tx_fifo_size,
1949 vcl_cfg->tx_fifo_size);
1951 else if (unformat (line_input, "tx-fifo-size %ld",
1952 &vcl_cfg->tx_fifo_size))
1954 if (VPPCOM_DEBUG > 0)
1955 clib_warning ("VCL<%d>: configured tx_fifo_size %ld (0x%lx)",
1956 getpid (), vcl_cfg->tx_fifo_size,
1957 vcl_cfg->tx_fifo_size);
1959 else if (unformat (line_input, "event-queue-size 0x%lx",
1960 &vcl_cfg->event_queue_size))
1962 if (VPPCOM_DEBUG > 0)
1963 clib_warning ("VCL<%d>: configured event_queue_size "
1965 getpid (), vcl_cfg->event_queue_size,
1966 vcl_cfg->event_queue_size);
1968 else if (unformat (line_input, "event-queue-size %ld",
1969 &vcl_cfg->event_queue_size))
1971 if (VPPCOM_DEBUG > 0)
1972 clib_warning ("VCL<%d>: configured event_queue_size "
1974 getpid (), vcl_cfg->event_queue_size,
1975 vcl_cfg->event_queue_size);
1977 else if (unformat (line_input, "listen-queue-size 0x%lx",
1978 &vcl_cfg->listen_queue_size))
1980 if (VPPCOM_DEBUG > 0)
1981 clib_warning ("VCL<%d>: configured listen_queue_size "
1983 getpid (), vcl_cfg->listen_queue_size,
1984 vcl_cfg->listen_queue_size);
1986 else if (unformat (line_input, "listen-queue-size %ld",
1987 &vcl_cfg->listen_queue_size))
1989 if (VPPCOM_DEBUG > 0)
1990 clib_warning ("VCL<%d>: configured listen_queue_size "
1992 getpid (), vcl_cfg->listen_queue_size,
1993 vcl_cfg->listen_queue_size);
1995 else if (unformat (line_input, "app-timeout %f",
1996 &vcl_cfg->app_timeout))
1998 if (VPPCOM_DEBUG > 0)
1999 clib_warning ("VCL<%d>: configured app_timeout %f",
2000 getpid (), vcl_cfg->app_timeout);
2002 else if (unformat (line_input, "session-timeout %f",
2003 &vcl_cfg->session_timeout))
2005 if (VPPCOM_DEBUG > 0)
2006 clib_warning ("VCL<%d>: configured session_timeout %f",
2007 getpid (), vcl_cfg->session_timeout);
2009 else if (unformat (line_input, "accept-timeout %f",
2010 &vcl_cfg->accept_timeout))
2012 if (VPPCOM_DEBUG > 0)
2013 clib_warning ("VCL<%d>: configured accept_timeout %f",
2014 getpid (), vcl_cfg->accept_timeout);
2016 else if (unformat (line_input, "app-proxy-transport-tcp"))
2018 vcl_cfg->app_proxy_transport_tcp = 1;
2019 if (VPPCOM_DEBUG > 0)
2020 clib_warning ("VCL<%d>: configured "
2021 "app_proxy_transport_tcp (%d)",
2022 getpid (), vcl_cfg->app_proxy_transport_tcp);
2024 else if (unformat (line_input, "app-proxy-transport-udp"))
2026 vcl_cfg->app_proxy_transport_udp = 1;
2027 if (VPPCOM_DEBUG > 0)
2028 clib_warning ("VCL<%d>: configured "
2029 "app_proxy_transport_udp (%d)",
2030 getpid (), vcl_cfg->app_proxy_transport_udp);
2032 else if (unformat (line_input, "app-scope-local"))
2034 vcl_cfg->app_scope_local = 1;
2035 if (VPPCOM_DEBUG > 0)
2036 clib_warning ("VCL<%d>: configured app_scope_local (%d)",
2037 getpid (), vcl_cfg->app_scope_local);
2039 else if (unformat (line_input, "app-scope-global"))
2041 vcl_cfg->app_scope_global = 1;
2042 if (VPPCOM_DEBUG > 0)
2043 clib_warning ("VCL<%d>: configured app_scope_global (%d)",
2044 getpid (), vcl_cfg->app_scope_global);
2046 else if (unformat (line_input, "namespace-secret %lu",
2047 &vcl_cfg->namespace_secret))
2049 if (VPPCOM_DEBUG > 0)
2051 ("VCL<%d>: configured namespace_secret %lu (0x%lx)",
2052 getpid (), vcl_cfg->namespace_secret,
2053 vcl_cfg->namespace_secret);
2055 else if (unformat (line_input, "namespace-id %v",
2056 &vcl_cfg->namespace_id))
2058 vl_api_application_attach_t *mp;
2059 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
2060 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
2061 if (nsid_vec_len > max_nsid_vec_len)
2063 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
2064 if (VPPCOM_DEBUG > 0)
2065 clib_warning ("VCL<%d>: configured namespace_id is "
2066 "too long, truncated to %d characters!",
2067 getpid (), max_nsid_vec_len);
2070 if (VPPCOM_DEBUG > 0)
2071 clib_warning ("VCL<%d>: configured namespace_id %v",
2072 getpid (), vcl_cfg->namespace_id);
2074 else if (unformat (line_input, "}"))
2077 if (VPPCOM_DEBUG > 0)
2078 clib_warning ("VCL<%d>: completed parsing vppcom config!",
2084 if (line_input->buffer[line_input->index] != '#')
2086 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
2088 &line_input->buffer[line_input->index]);
2095 unformat_free (input);
2103 * VPPCOM Public API functions
2106 vppcom_app_create (char *app_name)
2108 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
2119 vppcom_cfg_init (vcl_cfg);
2120 env_var_str = getenv (VPPCOM_ENV_DEBUG);
2124 if (sscanf (env_var_str, "%u", &tmp) != 1)
2125 clib_warning ("VCL<%d>: WARNING: Invalid debug level specified "
2126 "in the environment variable " VPPCOM_ENV_DEBUG
2127 " (%s)!\n", getpid (), env_var_str);
2131 if (VPPCOM_DEBUG > 0)
2132 clib_warning ("VCL<%d>: configured VCL debug level (%u) from "
2133 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
2136 conf_fname = getenv (VPPCOM_ENV_CONF);
2138 conf_fname = VPPCOM_CONF_DEFAULT;
2139 vppcom_cfg_heapsize (conf_fname);
2140 vcl_cfg = &vcm->cfg;
2141 clib_spinlock_init (&vcm->session_fifo_lockp);
2142 clib_fifo_validate (vcm->client_session_index_fifo,
2143 vcm->cfg.listen_queue_size);
2144 vppcom_cfg_read (conf_fname);
2146 env_var_str = getenv (VPPCOM_ENV_API_PREFIX);
2149 if (vcl_cfg->vpp_api_filename)
2150 vec_free (vcl_cfg->vpp_api_filename);
2151 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
2153 vl_set_memory_root_path ((char *) env_var_str);
2155 if (VPPCOM_DEBUG > 0)
2156 clib_warning ("VCL<%d>: configured api prefix (%s) and "
2157 "filename (%s) from " VPPCOM_ENV_API_PREFIX "!",
2158 getpid (), env_var_str, vcl_cfg->vpp_api_filename);
2160 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
2163 u32 ns_id_vec_len = strlen (env_var_str);
2165 vec_reset_length (vcm->cfg.namespace_id);
2166 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
2167 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
2169 if (VPPCOM_DEBUG > 0)
2170 clib_warning ("VCL<%d>: configured namespace_id (%v) from "
2171 VPPCOM_ENV_APP_NAMESPACE_ID
2172 "!", getpid (), vcm->cfg.namespace_id);
2174 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
2178 if (sscanf (env_var_str, "%lu", &tmp) != 1)
2179 clib_warning ("VCL<%d>: WARNING: Invalid namespace secret "
2180 "specified in the environment variable "
2181 VPPCOM_ENV_APP_NAMESPACE_SECRET
2182 " (%s)!\n", getpid (), env_var_str);
2185 vcm->cfg.namespace_secret = tmp;
2186 if (VPPCOM_DEBUG > 0)
2187 clib_warning ("VCL<%d>: configured namespace secret "
2189 VPPCOM_ENV_APP_NAMESPACE_SECRET
2190 "!", getpid (), vcm->cfg.namespace_secret);
2193 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
2195 vcm->cfg.app_proxy_transport_tcp = 1;
2196 if (VPPCOM_DEBUG > 0)
2197 clib_warning ("VCL<%d>: configured app_proxy_transport_tcp "
2199 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP
2200 "!", getpid (), vcm->cfg.app_proxy_transport_tcp);
2202 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
2204 vcm->cfg.app_proxy_transport_udp = 1;
2205 if (VPPCOM_DEBUG > 0)
2206 clib_warning ("VCL<%d>: configured app_proxy_transport_udp "
2208 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP
2209 "!", getpid (), vcm->cfg.app_proxy_transport_udp);
2211 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
2213 vcm->cfg.app_scope_local = 1;
2214 if (VPPCOM_DEBUG > 0)
2215 clib_warning ("VCL<%d>: configured app_scope_local (%u) from "
2216 VPPCOM_ENV_APP_SCOPE_LOCAL
2217 "!", getpid (), vcm->cfg.app_scope_local);
2219 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
2221 vcm->cfg.app_scope_global = 1;
2222 if (VPPCOM_DEBUG > 0)
2223 clib_warning ("VCL<%d>: configured app_scope_global (%u) from "
2224 VPPCOM_ENV_APP_SCOPE_GLOBAL
2225 "!", getpid (), vcm->cfg.app_scope_global);
2228 vcm->main_cpu = os_get_thread_index ();
2229 heap = clib_mem_get_per_cpu_heap ();
2230 h = mheap_header (heap);
2232 /* make the main heap thread-safe */
2233 h->flags |= MHEAP_FLAG_THREAD_SAFE;
2235 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
2237 clib_time_init (&vcm->clib_time);
2238 vppcom_init_error_string_table ();
2239 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
2240 20 /* timeout in secs */ );
2241 clib_spinlock_init (&vcm->sessions_lockp);
2244 if (vcm->my_client_index == ~0)
2247 /* API hookup and connect to VPP */
2248 vppcom_api_hookup ();
2249 vcm->app_state = STATE_APP_START;
2250 rv = vppcom_connect_to_vpp (app_name);
2253 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
2258 /* State event handling thread */
2260 rv = vce_start_event_thread (&(vcm->event_thread), 20);
2263 if (VPPCOM_DEBUG > 0)
2264 clib_warning ("VCL<%d>: sending session enable", getpid ());
2266 rv = vppcom_app_session_enable ();
2269 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
2270 "failed!", getpid ());
2274 if (VPPCOM_DEBUG > 0)
2275 clib_warning ("VCL<%d>: sending app attach", getpid ());
2277 rv = vppcom_app_attach ();
2280 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
2285 if (VPPCOM_DEBUG > 0)
2286 clib_warning ("VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
2287 getpid (), app_name, vcm->my_client_index,
2288 vcm->my_client_index);
2295 vppcom_app_destroy (void)
2299 if (vcm->my_client_index == ~0)
2302 if (VPPCOM_DEBUG > 0)
2303 clib_warning ("VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
2304 getpid (), vcm->my_client_index, vcm->my_client_index);
2306 if (VPPCOM_DEBUG > 0)
2309 ELOG_TYPE_DECLARE (e) =
2311 .format = "app_detach:C:%d",
2312 .format_args = "i4",
2319 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
2320 ed->data = vcm->my_client_index;
2324 vppcom_app_detach ();
2325 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
2326 if (PREDICT_FALSE (rv))
2328 if (VPPCOM_DEBUG > 0)
2329 clib_warning ("VCL<%d>: application detach timed out! "
2330 "returning %d (%s)",
2331 getpid (), rv, vppcom_retval_str (rv));
2334 /* Finished with logging before client gets reset to ~0 */
2335 if (VPPCOM_DEBUG > 0)
2338 vl_client_disconnect_from_vlib ();
2339 vcm->my_client_index = ~0;
2340 vcm->app_state = STATE_APP_START;
2344 vppcom_session_create (u8 proto, u8 is_nonblocking)
2348 session_state_t state;
2349 elog_track_t session_elog_track;
2351 clib_spinlock_lock (&vcm->sessions_lockp);
2352 pool_get (vcm->sessions, session);
2353 memset (session, 0, sizeof (*session));
2354 session_index = session - vcm->sessions;
2356 session->proto = proto;
2357 session->state = STATE_START;
2358 state = session->state;
2359 session->vpp_handle = ~0;
2362 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2364 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2366 if (VPPCOM_DEBUG > 0)
2368 session->elog_track.name =
2369 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2371 elog_track_register (&vcm->elog_main, &session->elog_track);
2372 session_elog_track = session->elog_track;
2375 clib_spinlock_unlock (&vcm->sessions_lockp);
2377 if (VPPCOM_DEBUG > 0)
2378 clib_warning ("VCL<%d>: sid %u", getpid (), session_index);
2380 if (VPPCOM_DEBUG > 0)
2383 ELOG_TYPE_DECLARE (e) =
2385 .format = "session_create:proto:%d state:%d is_nonblocking:%d",
2386 .format_args = "i4i4i4",
2394 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
2395 ed->data[0] = proto;
2396 ed->data[1] = state;
2397 ed->data[2] = is_nonblocking;
2401 return (int) session_index;
2405 vppcom_session_close (uint32_t session_index)
2407 session_t *session = 0;
2415 session_state_t state;
2416 elog_track_t session_elog_track;
2418 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2419 is_vep = session->is_vep;
2420 is_vep_session = session->is_vep_session;
2421 next_sid = session->vep.next_sid;
2422 vep_idx = session->vep.vep_idx;
2423 state = session->state;
2424 vpp_handle = session->vpp_handle;
2425 clib_spinlock_unlock (&vcm->sessions_lockp);
2428 * Why two if(VPPCOM_DEBUG) checks?
2430 * Eventually all clib_warnings need their own way of being
2431 * logged and signalled (like severity) where event logging
2432 * is a separate debugging tool. It will make the separation
2433 * easier. ... parting is such sweet sorrow ...
2435 if (VPPCOM_DEBUG > 0)
2437 session_elog_track = session->elog_track;
2440 if (VPPCOM_DEBUG > 0)
2443 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
2444 "closing epoll session...",
2445 getpid (), session_index, session_index);
2447 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
2448 "closing session...",
2449 getpid (), vpp_handle, session_index);
2454 while (next_sid != ~0)
2456 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
2457 if ((VPPCOM_DEBUG > 0) && PREDICT_FALSE (rv < 0))
2458 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2459 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2460 getpid (), vpp_handle, next_sid, vep_idx,
2461 rv, vppcom_retval_str (rv));
2463 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2464 next_sid = session->vep.next_sid;
2465 clib_spinlock_unlock (&vcm->sessions_lockp);
2472 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2473 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2474 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2475 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2476 getpid (), vpp_handle, session_index,
2477 vep_idx, rv, vppcom_retval_str (rv));
2480 if (state & STATE_LISTEN)
2482 rv = vppcom_session_unbind (session_index);
2483 if (PREDICT_FALSE (rv < 0))
2485 if (VPPCOM_DEBUG > 0)
2486 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2487 "listener unbind failed! rv %d (%s)",
2488 getpid (), vpp_handle, session_index,
2489 rv, vppcom_retval_str (rv));
2493 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
2495 rv = vppcom_session_disconnect (session_index);
2496 if (PREDICT_FALSE (rv < 0))
2497 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2498 "session disconnect failed! rv %d (%s)",
2499 getpid (), vpp_handle, session_index,
2500 rv, vppcom_retval_str (rv));
2504 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2505 vpp_handle = session->vpp_handle;
2506 if (vpp_handle != ~0)
2508 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
2510 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
2512 pool_put_index (vcm->sessions, session_index);
2514 clib_spinlock_unlock (&vcm->sessions_lockp);
2516 if (VPPCOM_DEBUG > 0)
2519 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
2520 getpid (), session_index, session_index);
2522 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
2523 getpid (), vpp_handle, session_index);
2527 if (VPPCOM_DEBUG > 0)
2530 ELOG_TYPE_DECLARE (e) =
2532 .format = "session_close:rv:%d",
2533 .format_args = "i4",
2541 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
2550 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2552 session_t *session = 0;
2556 return VPPCOM_EINVAL;
2558 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2560 if (session->is_vep)
2562 clib_spinlock_unlock (&vcm->sessions_lockp);
2563 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2564 "bind to an epoll session!", getpid (), session_index);
2569 session->lcl_addr.is_ip4 = ep->is_ip4;
2570 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
2571 session->lcl_port = ep->port;
2573 if (VPPCOM_DEBUG > 0)
2574 clib_warning ("VCL<%d>: sid %u: binding to local %s address %U "
2575 "port %u, proto %s", getpid (), session_index,
2576 session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2577 format_ip46_address, &session->lcl_addr.ip46,
2578 session->lcl_addr.is_ip4,
2579 clib_net_to_host_u16 (session->lcl_port),
2580 session->proto ? "UDP" : "TCP");
2582 if (VPPCOM_DEBUG > 0)
2584 if (session->lcl_addr.is_ip4)
2587 ELOG_TYPE_DECLARE (e) =
2589 .format = "bind local:%s:%d.%d.%d.%d:%d ",
2590 .format_args = "t1i1i1i1i1i2",
2591 .n_enum_strings = 2,
2592 .enum_strings = {"TCP", "UDP",},
2595 CLIB_PACKED (struct {
2601 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2602 ed->proto = session->proto;
2603 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
2604 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
2605 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
2606 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
2607 ed->port = clib_net_to_host_u16 (session->lcl_port);
2612 clib_spinlock_unlock (&vcm->sessions_lockp);
2618 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2620 session_t *listen_session = 0;
2621 u64 listen_vpp_handle;
2624 if (q_len == 0 || q_len == ~0)
2625 q_len = vcm->cfg.listen_queue_size;
2627 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2629 if (listen_session->is_vep)
2631 clib_spinlock_unlock (&vcm->sessions_lockp);
2632 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
2633 "epoll session!", getpid (), listen_session_index);
2638 listen_vpp_handle = listen_session->vpp_handle;
2639 if (listen_session->state & STATE_LISTEN)
2641 clib_spinlock_unlock (&vcm->sessions_lockp);
2642 if (VPPCOM_DEBUG > 0)
2643 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2644 "already in listen state!",
2645 getpid (), listen_vpp_handle, listen_session_index);
2650 if (VPPCOM_DEBUG > 0)
2651 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
2652 "sid %u: sending bind request...",
2653 getpid (), listen_vpp_handle, listen_session_index);
2655 vppcom_send_bind_sock (listen_session, listen_session_index);
2656 clib_spinlock_unlock (&vcm->sessions_lockp);
2658 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2659 vcm->cfg.session_timeout);
2661 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2662 if (PREDICT_FALSE (retval))
2664 if (VPPCOM_DEBUG > 0)
2665 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind failed! "
2666 "returning %d (%s)", getpid (),
2667 listen_session->vpp_handle, listen_session_index,
2668 retval, vppcom_retval_str (retval));
2669 clib_spinlock_unlock (&vcm->sessions_lockp);
2674 clib_spinlock_lock (&vcm->session_fifo_lockp);
2675 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2676 clib_spinlock_unlock (&vcm->session_fifo_lockp);
2678 clib_spinlock_unlock (&vcm->sessions_lockp);
2685 validate_args_session_accept_ (session_t * listen_session)
2687 u32 listen_session_index = listen_session - vcm->sessions;
2689 /* Input validation - expects spinlock on sessions_lockp */
2690 if (listen_session->is_vep)
2692 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
2693 "epoll session!", getpid (), listen_session_index);
2694 return VPPCOM_EBADFD;
2697 if (listen_session->state != STATE_LISTEN)
2699 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2700 "not in listen state! state 0x%x (%s)", getpid (),
2701 listen_session->vpp_handle, listen_session_index,
2702 listen_session->state,
2703 vppcom_session_state_str (listen_session->state));
2704 return VPPCOM_EBADFD;
2710 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2713 session_t *listen_session = 0;
2714 session_t *client_session = 0;
2715 u32 client_session_index = ~0;
2717 u64 listen_vpp_handle;
2718 vce_event_handler_reg_t *reg;
2720 vce_event_connect_request_t *result;
2723 int millisecond_timeout = 1;
2724 int hours_timeout = 20 * 60 * 60;
2726 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2727 listen_vpp_handle = listen_session->vpp_handle; // For debugging
2729 rv = validate_args_session_accept_ (listen_session);
2732 clib_spinlock_unlock (&vcm->sessions_lockp);
2736 /* Using an aggressive timer of 1ms and a generous timer of
2737 * 20 hours, we can implement a blocking and non-blocking listener
2738 * as both event and time driven */
2739 gettimeofday (&tv, NULL);
2740 ts.tv_nsec = (tv.tv_usec * 1000) + (1000 * millisecond_timeout);
2741 ts.tv_sec = tv.tv_sec;
2743 /* Predict that the Listener is blocking more often than not */
2744 if (PREDICT_TRUE (!VCL_SESS_ATTR_TEST (listen_session->attr,
2745 VCL_SESS_ATTR_NONBLOCK)))
2746 ts.tv_sec += hours_timeout;
2748 clib_spinlock_unlock (&vcm->sessions_lockp);
2750 /* Register handler for connect_request event on listen_session_index */
2751 vce_event_key_t evk;
2752 evk.session_index = listen_session_index;
2753 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
2754 reg = vce_register_handler (&vcm->event_thread, &evk,
2755 vce_connect_request_handler_fn);
2758 pthread_mutex_lock (®->handler_lock);
2762 pthread_cond_timedwait (®->handler_cond, ®->handler_lock, &ts);
2763 if (rv == ETIMEDOUT)
2768 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
2770 result = (vce_event_connect_request_t *) ev->data;
2771 client_session_index = result->accepted_session_index;
2775 /* Remove from the FIFO used to service epoll */
2776 clib_spinlock_lock (&vcm->session_fifo_lockp);
2777 if (clib_fifo_elts (vcm->client_session_index_fifo))
2779 u32 tmp_client_session_index;
2780 clib_fifo_sub1 (vcm->client_session_index_fifo,
2781 tmp_client_session_index);
2782 /* It wasn't ours... put it back ... */
2783 if (tmp_client_session_index != client_session_index)
2784 clib_fifo_add1 (vcm->client_session_index_fifo,
2785 tmp_client_session_index);
2787 clib_spinlock_unlock (&vcm->session_fifo_lockp);
2789 clib_spinlock_lock (&vcm->sessions_lockp);
2791 rv = vppcom_session_at_index (client_session_index, &client_session);
2792 if (PREDICT_FALSE (rv))
2794 rv = VPPCOM_ECONNABORTED;
2795 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
2796 "lookup failed! returning %d (%s)", getpid (),
2797 listen_vpp_handle, listen_session_index,
2798 client_session_index, rv, vppcom_retval_str (rv));
2802 if (flags & O_NONBLOCK)
2803 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2805 VCL_SESS_ATTR_CLR (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2807 if (VPPCOM_DEBUG > 0)
2808 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
2809 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
2810 getpid (), listen_vpp_handle, listen_session_index,
2811 client_session->vpp_handle, client_session_index,
2812 flags, VCL_SESS_ATTR_TEST (client_session->attr,
2813 VCL_SESS_ATTR_NONBLOCK));
2817 ep->is_ip4 = client_session->peer_addr.is_ip4;
2818 ep->port = client_session->peer_port;
2819 if (client_session->peer_addr.is_ip4)
2820 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2821 sizeof (ip4_address_t));
2823 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2824 sizeof (ip6_address_t));
2827 vppcom_send_accept_session_reply (client_session->vpp_handle,
2828 client_session->client_context,
2829 0 /* retval OK */ );
2831 if (VPPCOM_DEBUG > 0)
2832 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle "
2833 "0x%llx, sid %u connection to local %s address "
2834 "%U port %u", getpid (), listen_vpp_handle,
2835 listen_session_index, client_session->vpp_handle,
2836 client_session_index,
2837 client_session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2838 format_ip46_address, &client_session->lcl_addr.ip46,
2839 client_session->lcl_addr.is_ip4,
2840 clib_net_to_host_u16 (client_session->lcl_port));
2842 if (VPPCOM_DEBUG > 0)
2844 client_session->elog_track.name =
2845 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2846 client_session_index, 0);
2847 elog_track_register (&vcm->elog_main, &client_session->elog_track);
2849 // Two elog entries due to 20-byte per entry constraint.
2851 ELOG_TYPE_DECLARE (e) =
2853 .format = "accept: listen_handle:%x from_handle:%x",
2854 .format_args = "i8i8",
2862 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, client_session->elog_track);
2863 ed->handle[0] = listen_vpp_handle;
2864 ed->handle[1] = client_session->vpp_handle;
2867 if (client_session->lcl_addr.is_ip4)
2870 ELOG_TYPE_DECLARE (e2) =
2872 .format = "accept: S:%d %d.%d.%d.%d:%d ",
2873 .format_args = "i4i1i1i1i1i2",
2876 CLIB_PACKED (struct {
2883 ELOG_TRACK_DATA (&vcm->elog_main, e2, client_session->elog_track);
2884 ed2->session = client_session_index;
2885 ed2->addr[0] = client_session->lcl_addr.ip46.ip4.as_u8[0];
2886 ed2->addr[1] = client_session->lcl_addr.ip46.ip4.as_u8[1];
2887 ed2->addr[2] = client_session->lcl_addr.ip46.ip4.as_u8[2];
2888 ed2->addr[3] = client_session->lcl_addr.ip46.ip4.as_u8[3];
2889 ed2->port = clib_net_to_host_u16 (client_session->lcl_port);
2894 clib_spinlock_unlock (&vcm->sessions_lockp);
2896 rv = (int) client_session_index;
2897 vce_clear_event (&vcm->event_thread, ev);
2900 vce_unregister_handler (&vcm->event_thread, reg);
2901 pthread_mutex_unlock (®->handler_lock);
2908 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2910 session_t *session = 0;
2912 int rv, retval = VPPCOM_OK;
2914 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2916 if (PREDICT_FALSE (session->is_vep))
2918 clib_spinlock_unlock (&vcm->sessions_lockp);
2919 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2920 "connect on an epoll session!", getpid (), session_index);
2925 if (PREDICT_FALSE (session->state & CLIENT_STATE_OPEN))
2927 if (VPPCOM_DEBUG > 0)
2928 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session already "
2929 "connected to %s %U port %d proto %s, state 0x%x (%s)",
2930 getpid (), session->vpp_handle, session_index,
2931 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2932 format_ip46_address,
2933 &session->peer_addr.ip46, session->peer_addr.is_ip4,
2934 clib_net_to_host_u16 (session->peer_port),
2935 session->proto ? "UDP" : "TCP", session->state,
2936 vppcom_session_state_str (session->state));
2938 clib_spinlock_unlock (&vcm->sessions_lockp);
2942 session->peer_addr.is_ip4 = server_ep->is_ip4;
2943 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
2944 session->peer_port = server_ep->port;
2946 if (VPPCOM_DEBUG > 0)
2947 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server "
2948 "%s %U port %d proto %s",
2949 getpid (), session->vpp_handle, session_index,
2950 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
2951 format_ip46_address,
2952 &session->peer_addr.ip46, session->peer_addr.is_ip4,
2953 clib_net_to_host_u16 (session->peer_port),
2954 session->proto ? "UDP" : "TCP");
2956 vppcom_send_connect_sock (session, session_index);
2957 clib_spinlock_unlock (&vcm->sessions_lockp);
2960 vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2961 vcm->cfg.session_timeout);
2963 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2964 vpp_handle = session->vpp_handle;
2965 clib_spinlock_unlock (&vcm->sessions_lockp);
2968 if (PREDICT_FALSE (retval))
2971 if (VPPCOM_DEBUG > 0)
2975 ("VCL<%d>: vpp handle 0x%llx, sid %u: connect failed! "
2976 "returning %d (%s)", getpid (), vpp_handle,
2977 session_index, rv, vppcom_retval_str (rv));
2979 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
2980 "returning %d (%s)", getpid (),
2981 session_index, rv, vppcom_retval_str (rv));
2984 else if (VPPCOM_DEBUG > 0)
2985 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
2986 getpid (), vpp_handle, session_index);
2992 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2995 session_t *session = 0;
2996 svm_fifo_t *rx_fifo;
3003 session_state_t state;
3007 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3009 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
3010 rx_fifo = session->rx_fifo;
3011 state = session->state;
3012 vpp_handle = session->vpp_handle;
3014 if (PREDICT_FALSE (session->is_vep))
3016 clib_spinlock_unlock (&vcm->sessions_lockp);
3017 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
3018 "read from an epoll session!", getpid (), session_index);
3023 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
3025 clib_spinlock_unlock (&vcm->sessions_lockp);
3026 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3028 if (VPPCOM_DEBUG > 0)
3029 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s session is "
3030 "not open! state 0x%x (%s), returning %d (%s)",
3031 getpid (), vpp_handle, session_index, state,
3032 vppcom_session_state_str (state),
3033 rv, vppcom_retval_str (rv));
3037 clib_spinlock_unlock (&vcm->sessions_lockp);
3042 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
3044 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
3046 while (!is_nonblocking && (n_read <= 0));
3050 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3052 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
3053 (EPOLLET | EPOLLIN));
3055 session->vep.et_mask |= EPOLLIN;
3057 if (state & STATE_CLOSE_ON_EMPTY)
3059 rv = VPPCOM_ECONNRESET;
3061 if (VPPCOM_DEBUG > 1)
3063 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo "
3064 "with session state 0x%x (%s)!"
3065 " Setting state to 0x%x (%s), returning %d (%s)",
3066 getpid (), session->vpp_handle, session_index,
3067 state, vppcom_session_state_str (state),
3069 vppcom_session_state_str (STATE_DISCONNECT), rv,
3070 vppcom_retval_str (rv));
3073 session->state = STATE_DISCONNECT;
3078 clib_spinlock_unlock (&vcm->sessions_lockp);
3083 if (VPPCOM_DEBUG > 2)
3086 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
3087 "from (%p)", getpid (), vpp_handle,
3088 session_index, n_read, rx_fifo);
3090 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
3091 "returning %d (%s)", getpid (), vpp_handle,
3092 session_index, rv, vppcom_retval_str (rv));
3099 vppcom_session_read (uint32_t session_index, void *buf, size_t n)
3101 return (vppcom_session_read_internal (session_index, buf, n, 0));
3105 vppcom_session_peek (uint32_t session_index, void *buf, int n)
3107 return (vppcom_session_read_internal (session_index, buf, n, 1));
3111 vppcom_session_read_ready (session_t * session, u32 session_index)
3116 session_state_t state = session->state;
3117 u64 vpp_handle = session->vpp_handle;
3119 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3120 if (PREDICT_FALSE (session->is_vep))
3122 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
3123 "epoll session!", getpid (), session_index);
3128 if (session->state & STATE_LISTEN)
3130 clib_spinlock_lock (&vcm->session_fifo_lockp);
3131 ready = clib_fifo_elts (vcm->client_session_index_fifo);
3132 clib_spinlock_unlock (&vcm->session_fifo_lockp);
3136 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
3138 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3141 if (VPPCOM_DEBUG > 1)
3142 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session is "
3143 "not open! state 0x%x (%s), returning %d (%s)",
3144 getpid (), vpp_handle, session_index,
3145 state, vppcom_session_state_str (state),
3146 rv, vppcom_retval_str (rv));
3150 ready = svm_fifo_max_dequeue (session->rx_fifo);
3156 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
3158 session->vep.et_mask |= EPOLLIN;
3160 if (state & STATE_CLOSE_ON_EMPTY)
3162 rv = VPPCOM_ECONNRESET;
3164 if (VPPCOM_DEBUG > 1)
3166 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
3167 "sid %u: Empty fifo with"
3168 " session state 0x%x (%s)! Setting state to "
3169 "0x%x (%s), returning %d (%s)",
3170 getpid (), session_index, vpp_handle,
3171 state, vppcom_session_state_str (state),
3173 vppcom_session_state_str (STATE_DISCONNECT), rv,
3174 vppcom_retval_str (rv));
3176 session->state = STATE_DISCONNECT;
3182 if (vcm->app_event_queue->cursize &&
3183 !pthread_mutex_trylock (&vcm->app_event_queue->mutex))
3185 u32 i, n_to_dequeue = vcm->app_event_queue->cursize;
3186 session_fifo_event_t e;
3188 for (i = 0; i < n_to_dequeue; i++)
3189 svm_queue_sub_raw (vcm->app_event_queue, (u8 *) & e);
3191 pthread_mutex_unlock (&vcm->app_event_queue->mutex);
3198 vppcom_session_write (uint32_t session_index, void *buf, size_t n)
3200 session_t *session = 0;
3201 svm_fifo_t *tx_fifo = 0;
3203 session_fifo_event_t evt;
3204 session_state_t state;
3205 int rv, n_write, is_nonblocking;
3211 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3213 tx_fifo = session->tx_fifo;
3214 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
3215 vpp_handle = session->vpp_handle;
3216 state = session->state;
3218 if (PREDICT_FALSE (session->is_vep))
3220 clib_spinlock_unlock (&vcm->sessions_lockp);
3221 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3222 "cannot write to an epoll session!",
3223 getpid (), vpp_handle, session_index);
3229 if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3232 ((session->state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3235 clib_spinlock_unlock (&vcm->sessions_lockp);
3236 if (VPPCOM_DEBUG > 1)
3237 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3238 "session is not open! state 0x%x (%s)",
3239 getpid (), vpp_handle, session_index,
3240 state, vppcom_session_state_str (state));
3244 clib_spinlock_unlock (&vcm->sessions_lockp);
3248 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, (void *) buf);
3250 while (!is_nonblocking && (n_write <= 0));
3252 /* If event wasn't set, add one */
3253 if ((n_write > 0) && svm_fifo_set_event (tx_fifo))
3255 /* Fabricate TX event, send to vpp */
3257 evt.event_type = FIFO_EVENT_APP_TX;
3259 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3260 q = session->vpp_event_queue;
3262 svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
3263 clib_spinlock_unlock (&vcm->sessions_lockp);
3264 if (VPPCOM_DEBUG > 1)
3265 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3266 "added FIFO_EVENT_APP_TX to "
3267 "vpp_event_q %p, n_write %d", getpid (),
3268 vpp_handle, session_index, q, n_write);
3273 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3275 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3276 (EPOLLET | EPOLLOUT));
3278 session->vep.et_mask |= EPOLLOUT;
3280 if (session->state & STATE_CLOSE_ON_EMPTY)
3282 rv = VPPCOM_ECONNRESET;
3284 if (VPPCOM_DEBUG > 1)
3286 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3287 "Empty fifo with session state 0x%x (%s)!"
3288 " Setting state to 0x%x (%s), returning %d (%s)",
3289 getpid (), session->vpp_handle, session_index,
3291 vppcom_session_state_str (session->state),
3293 vppcom_session_state_str (STATE_DISCONNECT), rv,
3294 vppcom_retval_str (rv));
3297 session->state = STATE_DISCONNECT;
3302 clib_spinlock_unlock (&vcm->sessions_lockp);
3307 if (VPPCOM_DEBUG > 2)
3310 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3311 "FIFO-FULL (%p)", getpid (), vpp_handle,
3312 session_index, tx_fifo);
3314 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3315 "wrote %d bytes tx-fifo: (%p)", getpid (),
3316 vpp_handle, session_index, n_write, tx_fifo);
3323 vppcom_session_write_ready (session_t * session, u32 session_index)
3331 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3332 if (PREDICT_FALSE (session->is_vep))
3334 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3335 "cannot write to an epoll session!",
3336 getpid (), session->vpp_handle, session_index);
3341 if (PREDICT_FALSE (session->state & STATE_LISTEN))
3343 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3344 "cannot write to a listen session!",
3345 getpid (), session->vpp_handle, session_index);
3350 if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3352 session_state_t state = session->state;
3354 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3356 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3357 "session is not open! state 0x%x (%s), "
3358 "returning %d (%s)", getpid (), session->vpp_handle,
3360 state, vppcom_session_state_str (state),
3361 rv, vppcom_retval_str (rv));
3365 ready = svm_fifo_max_enqueue (session->tx_fifo);
3367 if (VPPCOM_DEBUG > 3)
3368 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3369 "peek %s (%p), ready = %d", getpid (),
3370 session->vpp_handle, session_index,
3371 session->tx_fifo, ready);
3375 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3376 (EPOLLET | EPOLLOUT));
3378 session->vep.et_mask |= EPOLLOUT;
3380 if (session->state & STATE_CLOSE_ON_EMPTY)
3382 rv = VPPCOM_ECONNRESET;
3384 if (VPPCOM_DEBUG > 1)
3386 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3387 "Empty fifo with session "
3388 "state 0x%x (%s)! Setting state to 0x%x (%s), "
3389 "returning %d (%s)", getpid (),
3390 session->vpp_handle, session_index,
3392 vppcom_session_state_str (session->state),
3394 vppcom_session_state_str (STATE_DISCONNECT), rv,
3395 vppcom_retval_str (rv));
3397 session->state = STATE_DISCONNECT;
3407 vppcom_select (unsigned long n_bits, unsigned long *read_map,
3408 unsigned long *write_map, unsigned long *except_map,
3409 double time_to_wait)
3412 session_t *session = 0;
3413 int rv, bits_set = 0;
3414 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
3415 u32 minbits = clib_max (n_bits, BITS (uword));
3417 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
3419 if (n_bits && read_map)
3421 clib_bitmap_validate (vcm->rd_bitmap, minbits);
3422 clib_memcpy (vcm->rd_bitmap, read_map,
3423 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3424 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3426 if (n_bits && write_map)
3428 clib_bitmap_validate (vcm->wr_bitmap, minbits);
3429 clib_memcpy (vcm->wr_bitmap, write_map,
3430 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3431 memset (write_map, 0,
3432 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3434 if (n_bits && except_map)
3436 clib_bitmap_validate (vcm->ex_bitmap, minbits);
3437 clib_memcpy (vcm->ex_bitmap, except_map,
3438 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3439 memset (except_map, 0,
3440 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3450 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
3452 clib_spinlock_lock (&vcm->sessions_lockp);
3453 rv = vppcom_session_at_index (session_index, &session);
3456 clib_spinlock_unlock (&vcm->sessions_lockp);
3457 if (VPPCOM_DEBUG > 1)
3458 clib_warning ("VCL<%d>: session %d specified in "
3459 "read_map is closed.", getpid (),
3461 bits_set = VPPCOM_EBADFD;
3465 rv = vppcom_session_read_ready (session, session_index);
3466 clib_spinlock_unlock (&vcm->sessions_lockp);
3467 if (except_map && vcm->ex_bitmap &&
3468 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3471 clib_bitmap_set_no_check (except_map, session_index, 1);
3476 clib_bitmap_set_no_check (read_map, session_index, 1);
3484 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3486 clib_spinlock_lock (&vcm->sessions_lockp);
3487 rv = vppcom_session_at_index (session_index, &session);
3490 clib_spinlock_unlock (&vcm->sessions_lockp);
3491 if (VPPCOM_DEBUG > 0)
3492 clib_warning ("VCL<%d>: session %d specified in "
3493 "write_map is closed.", getpid (),
3495 bits_set = VPPCOM_EBADFD;
3499 rv = vppcom_session_write_ready (session, session_index);
3500 clib_spinlock_unlock (&vcm->sessions_lockp);
3501 if (write_map && (rv > 0))
3503 clib_bitmap_set_no_check (write_map, session_index, 1);
3511 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3513 clib_spinlock_lock (&vcm->sessions_lockp);
3514 rv = vppcom_session_at_index (session_index, &session);
3517 clib_spinlock_unlock (&vcm->sessions_lockp);
3518 if (VPPCOM_DEBUG > 1)
3519 clib_warning ("VCL<%d>: session %d specified in "
3520 "except_map is closed.", getpid (),
3522 bits_set = VPPCOM_EBADFD;
3526 rv = vppcom_session_read_ready (session, session_index);
3527 clib_spinlock_unlock (&vcm->sessions_lockp);
3530 clib_bitmap_set_no_check (except_map, session_index, 1);
3538 while ((time_to_wait == -1) || (clib_time_now (&vcm->clib_time) < timeout));
3545 vep_verify_epoll_chain (u32 vep_idx)
3548 vppcom_epoll_t *vep;
3552 if (VPPCOM_DEBUG <= 1)
3555 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3556 rv = vppcom_session_at_index (vep_idx, &session);
3557 if (PREDICT_FALSE (rv))
3559 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
3560 getpid (), vep_idx);
3563 if (PREDICT_FALSE (!session->is_vep))
3565 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3566 getpid (), vep_idx);
3569 vep = &session->vep;
3570 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
3573 " is_vep_session = %u\n"
3574 " next_sid = 0x%x (%u)\n"
3575 " wait_cont_idx = 0x%x (%u)\n"
3576 "}\n", getpid (), vep_idx,
3577 session->is_vep, session->is_vep_session,
3578 vep->next_sid, vep->next_sid,
3579 session->wait_cont_idx, session->wait_cont_idx);
3581 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
3583 rv = vppcom_session_at_index (sid, &session);
3584 if (PREDICT_FALSE (rv))
3586 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
3589 if (PREDICT_FALSE (session->is_vep))
3590 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
3591 getpid (), vep_idx);
3592 else if (PREDICT_FALSE (!session->is_vep_session))
3594 clib_warning ("VCL<%d>: ERROR: session (%u) "
3595 "is not a vep session!", getpid (), sid);
3598 vep = &session->vep;
3599 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
3600 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
3601 "vep_idx (%u)!", getpid (),
3602 sid, session->vep.vep_idx, vep_idx);
3603 if (session->is_vep_session)
3605 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3607 " next_sid = 0x%x (%u)\n"
3608 " prev_sid = 0x%x (%u)\n"
3609 " vep_idx = 0x%x (%u)\n"
3610 " ev.events = 0x%x\n"
3611 " ev.data.u64 = 0x%llx\n"
3615 vep->next_sid, vep->next_sid,
3616 vep->prev_sid, vep->prev_sid,
3617 vep->vep_idx, vep->vep_idx,
3618 vep->ev.events, vep->ev.data.u64, vep->et_mask);
3623 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
3624 getpid (), vep_idx);
3628 vppcom_epoll_create (void)
3630 session_t *vep_session;
3632 elog_track_t vep_elog_track;
3634 clib_spinlock_lock (&vcm->sessions_lockp);
3635 pool_get (vcm->sessions, vep_session);
3636 memset (vep_session, 0, sizeof (*vep_session));
3637 vep_idx = vep_session - vcm->sessions;
3639 vep_session->is_vep = 1;
3640 vep_session->vep.vep_idx = ~0;
3641 vep_session->vep.next_sid = ~0;
3642 vep_session->vep.prev_sid = ~0;
3643 vep_session->wait_cont_idx = ~0;
3644 vep_session->vpp_handle = ~0;
3645 vep_session->poll_reg = 0;
3647 if (VPPCOM_DEBUG > 0)
3649 vep_session->elog_track.name =
3650 (char *) format (0, "C:%d:VEP:%d%c", vcm->my_client_index,
3652 elog_track_register (&vcm->elog_main, &vep_session->elog_track);
3653 vep_elog_track = vep_session->elog_track;
3656 clib_spinlock_unlock (&vcm->sessions_lockp);
3658 if (VPPCOM_DEBUG > 0)
3659 clib_warning ("VCL<%d>: Created vep_idx %u / sid %u!",
3660 getpid (), vep_idx, vep_idx);
3662 if (VPPCOM_DEBUG > 0)
3666 ELOG_TYPE_DECLARE (e) =
3668 .format = "created epoll session:%d",
3669 .format_args = "i4",
3677 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_elog_track);
3686 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3687 struct epoll_event *event)
3689 session_t *vep_session;
3693 if (vep_idx == session_index)
3695 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
3696 getpid (), vep_idx);
3697 return VPPCOM_EINVAL;
3700 clib_spinlock_lock (&vcm->sessions_lockp);
3701 rv = vppcom_session_at_index (vep_idx, &vep_session);
3702 if (PREDICT_FALSE (rv))
3704 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
3707 if (PREDICT_FALSE (!vep_session->is_vep))
3709 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3710 getpid (), vep_idx);
3715 ASSERT (vep_session->vep.vep_idx == ~0);
3716 ASSERT (vep_session->vep.prev_sid == ~0);
3718 rv = vppcom_session_at_index (session_index, &session);
3719 if (PREDICT_FALSE (rv))
3721 if (VPPCOM_DEBUG > 0)
3722 clib_warning ("VCL<%d>: ERROR: Invalid session_index (%u)!",
3723 getpid (), session_index);
3726 if (PREDICT_FALSE (session->is_vep))
3728 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3736 if (PREDICT_FALSE (!event))
3738 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
3739 "epoll_event structure!", getpid ());
3743 if (vep_session->vep.next_sid != ~0)
3745 session_t *next_session;
3746 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3748 if (PREDICT_FALSE (rv))
3750 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
3751 "vep.next_sid (%u) on vep_idx (%u)!",
3752 getpid (), vep_session->vep.next_sid, vep_idx);
3755 ASSERT (next_session->vep.prev_sid == vep_idx);
3756 next_session->vep.prev_sid = session_index;
3758 session->vep.next_sid = vep_session->vep.next_sid;
3759 session->vep.prev_sid = vep_idx;
3760 session->vep.vep_idx = vep_idx;
3761 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3762 session->vep.ev = *event;
3763 session->is_vep = 0;
3764 session->is_vep_session = 1;
3765 vep_session->vep.next_sid = session_index;
3767 /* VCL Event Register handler */
3768 if (session->state & STATE_LISTEN)
3770 /* Register handler for connect_request event on listen_session_index */
3771 vce_event_key_t evk;
3772 evk.session_index = session_index;
3773 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
3774 vep_session->poll_reg =
3775 vce_register_handler (&vcm->event_thread, &evk,
3776 vce_epoll_wait_connect_request_handler_fn);
3778 if (VPPCOM_DEBUG > 1)
3779 clib_warning ("VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
3780 "sid %u, events 0x%x, data 0x%llx!",
3781 getpid (), vep_idx, session_index,
3782 event->events, event->data.u64);
3783 if (VPPCOM_DEBUG > 0)
3786 ELOG_TYPE_DECLARE (e) =
3788 .format = "epoll_ctladd: events:%x data:%x",
3789 .format_args = "i4i4i8",
3797 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3799 ed->events = event->events;
3800 ed->event_data = event->data.u64;
3806 if (PREDICT_FALSE (!event))
3808 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
3809 "epoll_event structure!", getpid ());
3813 else if (PREDICT_FALSE (!session->is_vep_session))
3815 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3816 "not a vep session!", getpid (), session_index);
3820 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3822 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3823 "vep_idx (%u) != vep_idx (%u)!",
3824 getpid (), session_index,
3825 session->vep.vep_idx, vep_idx);
3829 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3830 session->vep.ev = *event;
3831 if (VPPCOM_DEBUG > 1)
3833 ("VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3834 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
3839 if (PREDICT_FALSE (!session->is_vep_session))
3841 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3842 "not a vep session!", getpid (), session_index);
3846 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3848 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3849 "vep_idx (%u) != vep_idx (%u)!",
3850 getpid (), session_index,
3851 session->vep.vep_idx, vep_idx);
3856 /* VCL Event Un-register handler */
3857 if ((session->state & STATE_LISTEN) && vep_session->poll_reg)
3859 (void) vce_unregister_handler (&vcm->event_thread,
3860 vep_session->poll_reg);
3863 vep_session->wait_cont_idx =
3864 (vep_session->wait_cont_idx == session_index) ?
3865 session->vep.next_sid : vep_session->wait_cont_idx;
3867 if (session->vep.prev_sid == vep_idx)
3868 vep_session->vep.next_sid = session->vep.next_sid;
3871 session_t *prev_session;
3872 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3873 if (PREDICT_FALSE (rv))
3875 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
3876 "vep.prev_sid (%u) on sid (%u)!",
3877 getpid (), session->vep.prev_sid, session_index);
3880 ASSERT (prev_session->vep.next_sid == session_index);
3881 prev_session->vep.next_sid = session->vep.next_sid;
3883 if (session->vep.next_sid != ~0)
3885 session_t *next_session;
3886 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3887 if (PREDICT_FALSE (rv))
3889 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
3890 "vep.next_sid (%u) on sid (%u)!",
3891 getpid (), session->vep.next_sid, session_index);
3894 ASSERT (next_session->vep.prev_sid == session_index);
3895 next_session->vep.prev_sid = session->vep.prev_sid;
3898 memset (&session->vep, 0, sizeof (session->vep));
3899 session->vep.next_sid = ~0;
3900 session->vep.prev_sid = ~0;
3901 session->vep.vep_idx = ~0;
3902 session->is_vep_session = 0;
3903 if (VPPCOM_DEBUG > 1)
3904 clib_warning ("VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
3905 getpid (), vep_idx, session_index);
3906 if (VPPCOM_DEBUG > 0)
3909 ELOG_TYPE_DECLARE (e) =
3911 .format = "epoll_ctldel: vep:%d",
3912 .format_args = "i4",
3919 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3927 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
3931 vep_verify_epoll_chain (vep_idx);
3934 clib_spinlock_unlock (&vcm->sessions_lockp);
3939 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3940 int maxevents, double wait_for_time)
3942 session_t *vep_session;
3943 elog_track_t vep_elog_track;
3945 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3946 u32 keep_trying = 1;
3948 u32 vep_next_sid, wait_cont_idx;
3951 if (PREDICT_FALSE (maxevents <= 0))
3953 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
3954 getpid (), maxevents);
3955 return VPPCOM_EINVAL;
3957 memset (events, 0, sizeof (*events) * maxevents);
3959 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3960 vep_next_sid = vep_session->vep.next_sid;
3961 is_vep = vep_session->is_vep;
3962 wait_cont_idx = vep_session->wait_cont_idx;
3963 vep_elog_track = vep_session->elog_track;
3964 clib_spinlock_unlock (&vcm->sessions_lockp);
3966 if (PREDICT_FALSE (!is_vep))
3968 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3969 getpid (), vep_idx);
3973 if (PREDICT_FALSE (vep_next_sid == ~0))
3975 if (VPPCOM_DEBUG > 1)
3976 clib_warning ("VCL<%d>: WARNING: vep_idx (%u) is empty!",
3977 getpid (), vep_idx);
3978 if (VPPCOM_DEBUG > 1)
3981 ELOG_TYPE_DECLARE (e) =
3983 .format = "WRN: vep_idx:%d empty",
3984 .format_args = "i4",
3991 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_elog_track);
4004 elog_track_t session_elog_track;
4006 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
4007 sid != ~0; sid = next_sid)
4009 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
4010 u8 add_event, is_vep_session;
4012 u64 session_ev_data;
4014 VCL_LOCK_AND_GET_SESSION (sid, &session);
4015 next_sid = session->vep.next_sid;
4016 session_events = session->vep.ev.events;
4017 et_mask = session->vep.et_mask;
4018 is_vep = session->is_vep;
4019 is_vep_session = session->is_vep_session;
4020 session_vep_idx = session->vep.vep_idx;
4021 session_ev_data = session->vep.ev.data.u64;
4023 if (VPPCOM_DEBUG > 0)
4025 session_elog_track = session->elog_track;
4028 clib_spinlock_unlock (&vcm->sessions_lockp);
4030 if (PREDICT_FALSE (is_vep))
4032 if (VPPCOM_DEBUG > 0)
4033 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
4034 getpid (), vep_idx);
4035 if (VPPCOM_DEBUG > 0)
4038 ELOG_TYPE_DECLARE (e) =
4040 .format = "ERR:vep_idx:%d is vep",
4041 .format_args = "i4",
4048 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
4057 if (PREDICT_FALSE (!is_vep_session))
4059 if (VPPCOM_DEBUG > 0)
4060 clib_warning ("VCL<%d>: ERROR: session (%u) is not "
4061 "a vep session!", getpid (), sid);
4062 if (VPPCOM_DEBUG > 0)
4065 ELOG_TYPE_DECLARE (e) =
4067 .format = "ERR:SID:%d not vep",
4068 .format_args = "i4",
4075 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
4084 if (PREDICT_FALSE (session_vep_idx != vep_idx))
4086 clib_warning ("VCL<%d>: ERROR: session (%u) "
4087 "vep_idx (%u) != vep_idx (%u)!",
4088 getpid (), sid, session_vep_idx, vep_idx);
4093 add_event = clear_et_mask = 0;
4095 if (EPOLLIN & session_events)
4097 VCL_LOCK_AND_GET_SESSION (sid, &session);
4098 ready = vppcom_session_read_ready (session, sid);
4099 clib_spinlock_unlock (&vcm->sessions_lockp);
4100 if ((ready > 0) && (EPOLLIN & et_mask))
4103 events[num_ev].events |= EPOLLIN;
4104 if (((EPOLLET | EPOLLIN) & session_events) ==
4105 (EPOLLET | EPOLLIN))
4106 clear_et_mask |= EPOLLIN;
4113 case VPPCOM_ECONNRESET:
4114 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
4118 events[num_ev].events |= EPOLLERR;
4124 if (EPOLLOUT & session_events)
4126 VCL_LOCK_AND_GET_SESSION (sid, &session);
4127 ready = vppcom_session_write_ready (session, sid);
4128 clib_spinlock_unlock (&vcm->sessions_lockp);
4129 if ((ready > 0) && (EPOLLOUT & et_mask))
4132 events[num_ev].events |= EPOLLOUT;
4133 if (((EPOLLET | EPOLLOUT) & session_events) ==
4134 (EPOLLET | EPOLLOUT))
4135 clear_et_mask |= EPOLLOUT;
4142 case VPPCOM_ECONNRESET:
4143 events[num_ev].events |= EPOLLHUP;
4147 events[num_ev].events |= EPOLLERR;
4155 events[num_ev].data.u64 = session_ev_data;
4156 if (EPOLLONESHOT & session_events)
4158 VCL_LOCK_AND_GET_SESSION (sid, &session);
4159 session->vep.ev.events = 0;
4160 clib_spinlock_unlock (&vcm->sessions_lockp);
4163 if (num_ev == maxevents)
4165 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4166 vep_session->wait_cont_idx = next_sid;
4167 clib_spinlock_unlock (&vcm->sessions_lockp);
4171 if (wait_cont_idx != ~0)
4174 next_sid = vep_next_sid;
4175 else if (next_sid == wait_cont_idx)
4179 if (wait_for_time != -1)
4180 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
4182 while ((num_ev == 0) && keep_trying);
4184 if (wait_cont_idx != ~0)
4186 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4187 vep_session->wait_cont_idx = ~0;
4188 clib_spinlock_unlock (&vcm->sessions_lockp);
4191 return (rv != VPPCOM_OK) ? rv : num_ev;
4195 vppcom_session_attr (uint32_t session_index, uint32_t op,
4196 void *buffer, uint32_t * buflen)
4200 u32 *flags = buffer;
4201 vppcom_endpt_t *ep = buffer;
4203 VCL_LOCK_AND_GET_SESSION (session_index, &session);
4209 case VPPCOM_ATTR_GET_NREAD:
4210 rv = vppcom_session_read_ready (session, session_index);
4211 if (VPPCOM_DEBUG > 2)
4212 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
4214 if (VPPCOM_DEBUG > 0)
4217 ELOG_TYPE_DECLARE (e) =
4219 .format = "VPPCOM_ATTR_GET_NREAD: nread=%d",
4220 .format_args = "i4",
4227 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4235 case VPPCOM_ATTR_GET_NWRITE:
4236 rv = vppcom_session_write_ready (session, session_index);
4237 if (VPPCOM_DEBUG > 2)
4238 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
4239 getpid (), session_index, rv);
4240 if (VPPCOM_DEBUG > 0)
4243 ELOG_TYPE_DECLARE (e) =
4245 .format = "VPPCOM_ATTR_GET_NWRITE: nwrite=%d",
4246 .format_args = "i4",
4253 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4260 case VPPCOM_ATTR_GET_FLAGS:
4261 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
4263 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
4264 VCL_SESS_ATTR_NONBLOCK));
4265 *buflen = sizeof (*flags);
4266 if (VPPCOM_DEBUG > 2)
4267 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, "
4268 "flags = 0x%08x, is_nonblocking = %u", getpid (),
4269 session_index, *flags,
4270 VCL_SESS_ATTR_TEST (session->attr,
4271 VCL_SESS_ATTR_NONBLOCK));
4272 if (VPPCOM_DEBUG > 0)
4275 ELOG_TYPE_DECLARE (e) =
4277 .format = "VPPCOM_ATTR_GET_FLAGS: flags=%x is_nonblk=%d",
4278 .format_args = "i4i4",
4286 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4289 ed->is_nonblk = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
4298 case VPPCOM_ATTR_SET_FLAGS:
4299 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
4301 if (*flags & O_NONBLOCK)
4302 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
4304 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
4306 if (VPPCOM_DEBUG > 2)
4307 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, "
4308 "flags = 0x%08x, is_nonblocking = %u",
4309 getpid (), session_index, *flags,
4310 VCL_SESS_ATTR_TEST (session->attr,
4311 VCL_SESS_ATTR_NONBLOCK));
4312 if (VPPCOM_DEBUG > 0)
4315 ELOG_TYPE_DECLARE (e) =
4317 .format = "VPPCOM_ATTR_SET_FLAGS: flags=%x is_nonblk=%d",
4318 .format_args = "i4i4",
4326 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4329 ed->is_nonblk = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
4337 case VPPCOM_ATTR_GET_PEER_ADDR:
4338 if (PREDICT_TRUE (buffer && buflen &&
4339 (*buflen >= sizeof (*ep)) && ep->ip))
4341 ep->is_ip4 = session->peer_addr.is_ip4;
4342 ep->port = session->peer_port;
4343 if (session->peer_addr.is_ip4)
4344 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
4345 sizeof (ip4_address_t));
4347 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
4348 sizeof (ip6_address_t));
4349 *buflen = sizeof (*ep);
4350 if (VPPCOM_DEBUG > 1)
4351 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, "
4352 "is_ip4 = %u, addr = %U, port %u", getpid (),
4353 session_index, ep->is_ip4, format_ip46_address,
4354 &session->peer_addr.ip46, ep->is_ip4,
4355 clib_net_to_host_u16 (ep->port));
4356 if (VPPCOM_DEBUG > 0)
4361 ELOG_TYPE_DECLARE (e) =
4363 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:%d.%d.%d.%d:%d",
4364 .format_args = "i1i1i1i1i2",
4366 CLIB_PACKED (struct {
4371 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4373 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
4374 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
4375 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
4376 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
4377 ed->port = clib_net_to_host_u16 (session->peer_port);
4383 ELOG_TYPE_DECLARE (e) =
4385 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:IP6:%d",
4386 .format_args = "i2",
4388 CLIB_PACKED (struct {
4392 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4394 ed->port = clib_net_to_host_u16 (session->peer_port);
4403 case VPPCOM_ATTR_GET_LCL_ADDR:
4404 if (PREDICT_TRUE (buffer && buflen &&
4405 (*buflen >= sizeof (*ep)) && ep->ip))
4407 ep->is_ip4 = session->lcl_addr.is_ip4;
4408 ep->port = session->lcl_port;
4409 if (session->lcl_addr.is_ip4)
4410 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
4411 sizeof (ip4_address_t));
4413 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
4414 sizeof (ip6_address_t));
4415 *buflen = sizeof (*ep);
4416 if (VPPCOM_DEBUG > 1)
4417 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, "
4418 "is_ip4 = %u, addr = %U port %d", getpid (),
4419 session_index, ep->is_ip4, format_ip46_address,
4420 &session->lcl_addr.ip46, ep->is_ip4,
4421 clib_net_to_host_u16 (ep->port));
4422 if (VPPCOM_DEBUG > 0)
4427 ELOG_TYPE_DECLARE (e) =
4429 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:%d.%d.%d.%d:%d",
4430 .format_args = "i1i1i1i1i2",
4432 CLIB_PACKED (struct {
4437 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4439 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
4440 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
4441 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
4442 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
4443 ed->port = clib_net_to_host_u16 (session->peer_port);
4449 ELOG_TYPE_DECLARE (e) =
4451 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:IP6:%d",
4452 .format_args = "i2",
4454 CLIB_PACKED (struct {
4458 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4460 ed->port = clib_net_to_host_u16 (session->peer_port);
4469 case VPPCOM_ATTR_GET_LIBC_EPFD:
4470 rv = session->libc_epfd;
4471 if (VPPCOM_DEBUG > 2)
4472 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
4474 if (VPPCOM_DEBUG > 0)
4477 ELOG_TYPE_DECLARE (e) =
4479 .format = "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd=%d",
4480 .format_args = "i4",
4482 CLIB_PACKED (struct {
4486 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4487 ed->data = session->libc_epfd;
4493 case VPPCOM_ATTR_SET_LIBC_EPFD:
4494 if (PREDICT_TRUE (buffer && buflen &&
4495 (*buflen == sizeof (session->libc_epfd))))
4497 session->libc_epfd = *(int *) buffer;
4498 *buflen = sizeof (session->libc_epfd);
4500 if (VPPCOM_DEBUG > 2)
4501 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
4502 "buflen %d", getpid (), session->libc_epfd,
4504 if (VPPCOM_DEBUG > 0)
4507 ELOG_TYPE_DECLARE (e) =
4509 .format = "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd=%s%d buflen=%d",
4510 .format_args = "t1i4i4",
4511 .n_enum_strings = 2,
4512 .enum_strings = {"", "-",},
4514 CLIB_PACKED (struct {
4519 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4521 ed->sign = (session->libc_epfd < 0);
4522 ed->data[0] = abs(session->libc_epfd);
4523 ed->data[1] = *buflen;
4531 case VPPCOM_ATTR_GET_PROTOCOL:
4532 if (buffer && buflen && (*buflen >= sizeof (int)))
4534 *(int *) buffer = session->proto;
4535 *buflen = sizeof (int);
4537 if (VPPCOM_DEBUG > 2)
4538 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), "
4539 "buflen %d", getpid (), *(int *) buffer,
4540 *(int *) buffer ? "UDP" : "TCP", *buflen);
4541 if (VPPCOM_DEBUG > 0)
4544 ELOG_TYPE_DECLARE (e) =
4546 .format = "VPPCOM_ATTR_GET_PROTOCOL: %s buflen=%d",
4547 .format_args = "t1i4",
4548 .n_enum_strings = 2,
4549 .enum_strings = {"TCP", "UDP",},
4552 CLIB_PACKED (struct {
4557 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4558 ed->proto = session->proto;
4559 ed->buflen = *(int *) buffer;
4567 case VPPCOM_ATTR_GET_LISTEN:
4568 if (buffer && buflen && (*buflen >= sizeof (int)))
4570 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4571 VCL_SESS_ATTR_LISTEN);
4572 *buflen = sizeof (int);
4574 if (VPPCOM_DEBUG > 2)
4575 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, "
4576 "buflen %d", getpid (), *(int *) buffer, *buflen);
4577 if (VPPCOM_DEBUG > 0)
4580 ELOG_TYPE_DECLARE (e) =
4582 .format = "VPPCOM_ATTR_GET_LISTEN: %d buflen=%d",
4583 .format_args = "i4i4",
4590 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4591 ed->data[0] = *(int *) buffer;
4592 ed->data[1] = *buflen;
4600 case VPPCOM_ATTR_GET_ERROR:
4601 if (buffer && buflen && (*buflen >= sizeof (int)))
4603 *(int *) buffer = 0;
4604 *buflen = sizeof (int);
4606 if (VPPCOM_DEBUG > 2)
4607 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, "
4608 "buflen %d, #VPP-TBD#", getpid (),
4609 *(int *) buffer, *buflen);
4610 if (VPPCOM_DEBUG > 0)
4613 ELOG_TYPE_DECLARE (e) =
4615 .format = "VPPCOM_ATTR_GET_ERROR: %d buflen=%d",
4616 .format_args = "i4i4",
4623 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4624 ed->data[0] = *(int *) buffer;
4625 ed->data[1] = *buflen;
4633 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
4634 if (buffer && buflen && (*buflen >= sizeof (u32)))
4638 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
4639 session->tx_fifo ? session->tx_fifo->nitems :
4640 vcm->cfg.tx_fifo_size);
4641 *buflen = sizeof (u32);
4643 if (VPPCOM_DEBUG > 2)
4644 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
4645 "buflen %d, #VPP-TBD#", getpid (),
4646 *(size_t *) buffer, *(size_t *) buffer, *buflen);
4647 if (VPPCOM_DEBUG > 0)
4650 ELOG_TYPE_DECLARE (e) =
4652 .format = "VPPCOM_ATTR_GET_TX_FIFO_LEN: 0x%x buflen=%d",
4653 .format_args = "i4i4",
4660 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4661 ed->data[0] = *(size_t *) buffer;
4662 ed->data[1] = *buflen;
4670 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
4671 if (buffer && buflen && (*buflen == sizeof (u32)))
4674 session->sndbuf_size = *(u32 *) buffer;
4675 if (VPPCOM_DEBUG > 2)
4676 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
4677 "buflen %d, #VPP-TBD#", getpid (),
4678 session->sndbuf_size, session->sndbuf_size,
4680 if (VPPCOM_DEBUG > 0)
4683 ELOG_TYPE_DECLARE (e) =
4685 .format = "VPPCOM_ATTR_SET_TX_FIFO_LEN: 0x%x buflen=%d",
4686 .format_args = "i4i4",
4693 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4694 ed->data[0] = session->sndbuf_size;
4695 ed->data[1] = *buflen;
4703 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
4704 if (buffer && buflen && (*buflen >= sizeof (u32)))
4708 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
4709 session->rx_fifo ? session->rx_fifo->nitems :
4710 vcm->cfg.rx_fifo_size);
4711 *buflen = sizeof (u32);
4713 if (VPPCOM_DEBUG > 2)
4714 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
4715 "buflen %d, #VPP-TBD#", getpid (),
4716 *(size_t *) buffer, *(size_t *) buffer, *buflen);
4717 if (VPPCOM_DEBUG > 0)
4720 ELOG_TYPE_DECLARE (e) =
4722 .format = "VPPCOM_ATTR_GET_RX_FIFO_LEN: 0x%x buflen=%d",
4723 .format_args = "i4i4",
4730 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4731 ed->data[0] = *(size_t *) buffer;
4732 ed->data[1] = *buflen;
4740 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
4741 if (buffer && buflen && (*buflen == sizeof (u32)))
4744 session->rcvbuf_size = *(u32 *) buffer;
4745 if (VPPCOM_DEBUG > 2)
4746 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
4747 "buflen %d, #VPP-TBD#", getpid (),
4748 session->sndbuf_size, session->sndbuf_size,
4750 if (VPPCOM_DEBUG > 0)
4753 ELOG_TYPE_DECLARE (e) =
4755 .format = "VPPCOM_ATTR_SET_RX_FIFO_LEN: 0x%x buflen=%d",
4756 .format_args = "i4i4",
4763 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4764 ed->data[0] = session->sndbuf_size;
4765 ed->data[1] = *buflen;
4773 case VPPCOM_ATTR_GET_REUSEADDR:
4774 if (buffer && buflen && (*buflen >= sizeof (int)))
4777 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4778 VCL_SESS_ATTR_REUSEADDR);
4779 *buflen = sizeof (int);
4781 if (VPPCOM_DEBUG > 2)
4782 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
4783 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4785 if (VPPCOM_DEBUG > 0)
4788 ELOG_TYPE_DECLARE (e) =
4790 .format = "VPPCOM_ATTR_GET_REUSEADDR: %d buflen=%d",
4791 .format_args = "i4i4",
4798 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4799 ed->data[0] = *(int *) buffer;
4800 ed->data[1] = *buflen;
4808 case VPPCOM_ATTR_SET_REUSEADDR:
4809 if (buffer && buflen && (*buflen == sizeof (int)) &&
4810 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4813 if (*(int *) buffer)
4814 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
4816 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
4818 if (VPPCOM_DEBUG > 2)
4819 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, "
4820 "buflen %d, #VPP-TBD#", getpid (),
4821 VCL_SESS_ATTR_TEST (session->attr,
4822 VCL_SESS_ATTR_REUSEADDR),
4824 if (VPPCOM_DEBUG > 0)
4827 ELOG_TYPE_DECLARE (e) =
4829 .format = "VPPCOM_ATTR_SET_REUSEADDR: %d buflen=%d",
4830 .format_args = "i4i4",
4837 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4838 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4839 VCL_SESS_ATTR_REUSEADDR);
4840 ed->data[1] = *buflen;
4848 case VPPCOM_ATTR_GET_REUSEPORT:
4849 if (buffer && buflen && (*buflen >= sizeof (int)))
4852 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4853 VCL_SESS_ATTR_REUSEPORT);
4854 *buflen = sizeof (int);
4856 if (VPPCOM_DEBUG > 2)
4857 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, "
4858 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4860 if (VPPCOM_DEBUG > 0)
4863 ELOG_TYPE_DECLARE (e) =
4865 .format = "VPPCOM_ATTR_GET_REUSEPORT: %d buflen=%d",
4866 .format_args = "i4i4",
4873 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4874 ed->data[0] = *(int *) buffer;
4875 ed->data[1] = *buflen;
4883 case VPPCOM_ATTR_SET_REUSEPORT:
4884 if (buffer && buflen && (*buflen == sizeof (int)) &&
4885 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4888 if (*(int *) buffer)
4889 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
4891 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
4893 if (VPPCOM_DEBUG > 2)
4894 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, "
4895 "buflen %d, #VPP-TBD#", getpid (),
4896 VCL_SESS_ATTR_TEST (session->attr,
4897 VCL_SESS_ATTR_REUSEPORT),
4899 if (VPPCOM_DEBUG > 0)
4902 ELOG_TYPE_DECLARE (e) =
4904 .format = "VPPCOM_ATTR_SET_REUSEPORT: %d buflen=%d",
4905 .format_args = "i4i4",
4912 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4913 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4914 VCL_SESS_ATTR_REUSEPORT);
4915 ed->data[1] = *buflen;
4923 case VPPCOM_ATTR_GET_BROADCAST:
4924 if (buffer && buflen && (*buflen >= sizeof (int)))
4927 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4928 VCL_SESS_ATTR_BROADCAST);
4929 *buflen = sizeof (int);
4931 if (VPPCOM_DEBUG > 2)
4932 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, "
4933 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4935 if (VPPCOM_DEBUG > 0)
4938 ELOG_TYPE_DECLARE (e) =
4940 .format = "VPPCOM_ATTR_GET_BROADCAST: %d buflen=%d",
4941 .format_args = "i4i4",
4948 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4949 ed->data[0] = *(int *) buffer;
4950 ed->data[1] = *buflen;
4958 case VPPCOM_ATTR_SET_BROADCAST:
4959 if (buffer && buflen && (*buflen == sizeof (int)))
4962 if (*(int *) buffer)
4963 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
4965 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
4967 if (VPPCOM_DEBUG > 2)
4968 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, "
4969 "buflen %d, #VPP-TBD#", getpid (),
4970 VCL_SESS_ATTR_TEST (session->attr,
4971 VCL_SESS_ATTR_BROADCAST),
4973 if (VPPCOM_DEBUG > 0)
4976 ELOG_TYPE_DECLARE (e) =
4978 .format = "VPPCOM_ATTR_SET_BROADCAST: %d buflen=%d",
4979 .format_args = "i4i4",
4986 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4987 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4988 VCL_SESS_ATTR_BROADCAST);
4989 ed->data[1] = *buflen;
4997 case VPPCOM_ATTR_GET_V6ONLY:
4998 if (buffer && buflen && (*buflen >= sizeof (int)))
5001 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5002 VCL_SESS_ATTR_V6ONLY);
5003 *buflen = sizeof (int);
5005 if (VPPCOM_DEBUG > 2)
5006 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, "
5007 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5009 if (VPPCOM_DEBUG > 0)
5012 ELOG_TYPE_DECLARE (e) =
5014 .format = "VPPCOM_ATTR_GET_V6ONLY: %d buflen=%d",
5015 .format_args = "i4i4",
5022 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5023 ed->data[0] = *(int *) buffer;
5024 ed->data[1] = *buflen;
5032 case VPPCOM_ATTR_SET_V6ONLY:
5033 if (buffer && buflen && (*buflen == sizeof (int)))
5036 if (*(int *) buffer)
5037 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
5039 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
5041 if (VPPCOM_DEBUG > 2)
5042 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, "
5043 "buflen %d, #VPP-TBD#", getpid (),
5044 VCL_SESS_ATTR_TEST (session->attr,
5045 VCL_SESS_ATTR_V6ONLY), *buflen);
5046 if (VPPCOM_DEBUG > 0)
5049 ELOG_TYPE_DECLARE (e) =
5051 .format = "VPPCOM_ATTR_SET_V6ONLY: %d buflen=%d",
5052 .format_args = "i4i4",
5059 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5060 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5061 VCL_SESS_ATTR_V6ONLY);
5062 ed->data[1] = *buflen;
5070 case VPPCOM_ATTR_GET_KEEPALIVE:
5071 if (buffer && buflen && (*buflen >= sizeof (int)))
5074 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5075 VCL_SESS_ATTR_KEEPALIVE);
5076 *buflen = sizeof (int);
5078 if (VPPCOM_DEBUG > 2)
5079 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, "
5080 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5082 if (VPPCOM_DEBUG > 0)
5085 ELOG_TYPE_DECLARE (e) =
5087 .format = "VPPCOM_ATTR_GET_KEEPALIVE: %d buflen=%d",
5088 .format_args = "i4i4",
5095 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5096 ed->data[0] = *(int *) buffer;
5097 ed->data[1] = *buflen;
5105 case VPPCOM_ATTR_SET_KEEPALIVE:
5106 if (buffer && buflen && (*buflen == sizeof (int)))
5109 if (*(int *) buffer)
5110 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5112 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5114 if (VPPCOM_DEBUG > 2)
5115 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, "
5116 "buflen %d, #VPP-TBD#", getpid (),
5117 VCL_SESS_ATTR_TEST (session->attr,
5118 VCL_SESS_ATTR_KEEPALIVE),
5120 if (VPPCOM_DEBUG > 0)
5123 ELOG_TYPE_DECLARE (e) =
5125 .format = "VPPCOM_ATTR_SET_KEEPALIVE: %d buflen=%d",
5126 .format_args = "i4i4",
5133 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5134 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5135 VCL_SESS_ATTR_KEEPALIVE);
5136 ed->data[1] = *buflen;
5144 case VPPCOM_ATTR_GET_TCP_NODELAY:
5145 if (buffer && buflen && (*buflen >= sizeof (int)))
5148 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5149 VCL_SESS_ATTR_TCP_NODELAY);
5150 *buflen = sizeof (int);
5152 if (VPPCOM_DEBUG > 2)
5153 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, "
5154 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5156 if (VPPCOM_DEBUG > 0)
5159 ELOG_TYPE_DECLARE (e) =
5161 .format = "VPPCOM_ATTR_GET_TCP_NODELAY: %d buflen=%d",
5162 .format_args = "i4i4",
5169 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5170 ed->data[0] = *(int *) buffer;
5171 ed->data[1] = *buflen;
5179 case VPPCOM_ATTR_SET_TCP_NODELAY:
5180 if (buffer && buflen && (*buflen == sizeof (int)))
5183 if (*(int *) buffer)
5184 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5186 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5188 if (VPPCOM_DEBUG > 2)
5189 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, "
5190 "buflen %d, #VPP-TBD#", getpid (),
5191 VCL_SESS_ATTR_TEST (session->attr,
5192 VCL_SESS_ATTR_TCP_NODELAY),
5194 if (VPPCOM_DEBUG > 0)
5197 ELOG_TYPE_DECLARE (e) =
5199 .format = "VPPCOM_ATTR_SET_TCP_NODELAY: %d buflen=%d",
5200 .format_args = "i4i4",
5207 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5208 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5209 VCL_SESS_ATTR_TCP_NODELAY);
5210 ed->data[1] = *buflen;
5218 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
5219 if (buffer && buflen && (*buflen >= sizeof (int)))
5222 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5223 VCL_SESS_ATTR_TCP_KEEPIDLE);
5224 *buflen = sizeof (int);
5226 if (VPPCOM_DEBUG > 2)
5227 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, "
5228 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5230 if (VPPCOM_DEBUG > 0)
5233 ELOG_TYPE_DECLARE (e) =
5235 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5236 .format_args = "i4i4",
5243 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5244 ed->data[0] = *(int *) buffer;
5245 ed->data[1] = *buflen;
5253 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
5254 if (buffer && buflen && (*buflen == sizeof (int)))
5257 if (*(int *) buffer)
5258 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5260 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5262 if (VPPCOM_DEBUG > 2)
5263 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, "
5264 "buflen %d, #VPP-TBD#", getpid (),
5265 VCL_SESS_ATTR_TEST (session->attr,
5266 VCL_SESS_ATTR_TCP_KEEPIDLE),
5268 if (VPPCOM_DEBUG > 0)
5271 ELOG_TYPE_DECLARE (e) =
5273 .format = "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d buflen=%d",
5274 .format_args = "i4i4",
5281 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5282 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5283 VCL_SESS_ATTR_TCP_KEEPIDLE);
5284 ed->data[1] = *buflen;
5292 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
5293 if (buffer && buflen && (*buflen >= sizeof (int)))
5296 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5297 VCL_SESS_ATTR_TCP_KEEPINTVL);
5298 *buflen = sizeof (int);
5300 if (VPPCOM_DEBUG > 2)
5301 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, "
5302 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5304 if (VPPCOM_DEBUG > 0)
5307 ELOG_TYPE_DECLARE (e) =
5309 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5310 .format_args = "i4i4",
5317 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5318 ed->data[0] = *(int *) buffer;
5319 ed->data[1] = *buflen;
5327 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
5328 if (buffer && buflen && (*buflen == sizeof (int)))
5331 if (*(int *) buffer)
5332 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5334 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5336 if (VPPCOM_DEBUG > 2)
5337 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, "
5338 "buflen %d, #VPP-TBD#", getpid (),
5339 VCL_SESS_ATTR_TEST (session->attr,
5340 VCL_SESS_ATTR_TCP_KEEPINTVL),
5342 if (VPPCOM_DEBUG > 0)
5345 ELOG_TYPE_DECLARE (e) =
5347 .format = "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d buflen=%d",
5348 .format_args = "i4i4",
5355 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5356 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5357 VCL_SESS_ATTR_TCP_KEEPINTVL);
5358 ed->data[1] = *buflen;
5366 case VPPCOM_ATTR_GET_TCP_USER_MSS:
5367 if (buffer && buflen && (*buflen >= sizeof (u32)))
5370 *(u32 *) buffer = session->user_mss;
5371 *buflen = sizeof (int);
5373 if (VPPCOM_DEBUG > 2)
5374 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, "
5375 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5377 if (VPPCOM_DEBUG > 0)
5380 ELOG_TYPE_DECLARE (e) =
5382 .format = "VPPCOM_ATTR_GET_TCP_USER_MSS: %d buflen=%d",
5383 .format_args = "i4i4",
5390 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5391 ed->data[0] = *(int *) buffer;
5392 ed->data[1] = *buflen;
5400 case VPPCOM_ATTR_SET_TCP_USER_MSS:
5401 if (buffer && buflen && (*buflen == sizeof (u32)))
5404 session->user_mss = *(u32 *) buffer;
5406 if (VPPCOM_DEBUG > 2)
5407 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, "
5408 "buflen %d, #VPP-TBD#", getpid (),
5409 session->user_mss, *buflen);
5410 if (VPPCOM_DEBUG > 0)
5413 ELOG_TYPE_DECLARE (e) =
5415 .format = "VPPCOM_ATTR_SET_TCP_USER_MSS: %d buflen=%d",
5416 .format_args = "i4i4",
5423 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5424 ed->data[0] = session->user_mss;
5425 ed->data[1] = *buflen;
5439 clib_spinlock_unlock (&vcm->sessions_lockp);
5444 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
5445 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5448 session_t *session = 0;
5452 clib_spinlock_lock (&vcm->sessions_lockp);
5453 rv = vppcom_session_at_index (session_index, &session);
5454 if (PREDICT_FALSE (rv))
5456 clib_spinlock_unlock (&vcm->sessions_lockp);
5457 if (VPPCOM_DEBUG > 0)
5458 clib_warning ("VCL<%d>: invalid session, "
5459 "sid (%u) has been closed!",
5460 getpid (), session_index);
5461 if (VPPCOM_DEBUG > 0)
5464 ELOG_TYPE_DECLARE (e) =
5466 .format = "invalid session: %d closed",
5467 .format_args = "i4",
5474 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
5475 ed->data = session_index;
5479 clib_spinlock_unlock (&vcm->sessions_lockp);
5482 ep->is_ip4 = session->peer_addr.is_ip4;
5483 ep->port = session->peer_port;
5484 if (session->peer_addr.is_ip4)
5485 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
5486 sizeof (ip4_address_t));
5488 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
5489 sizeof (ip6_address_t));
5490 clib_spinlock_unlock (&vcm->sessions_lockp);
5494 rv = vppcom_session_read (session_index, buffer, buflen);
5495 else if (flags & MSG_PEEK)
5496 rv = vppcom_session_peek (session_index, buffer, buflen);
5499 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
5501 rv = VPPCOM_EAFNOSUPPORT;
5509 vppcom_session_sendto (uint32_t session_index, void *buffer,
5510 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5513 return VPPCOM_EINVAL;
5518 return VPPCOM_EINVAL;
5523 // TBD check the flags and do the right thing
5524 if (VPPCOM_DEBUG > 2)
5525 clib_warning ("VCL<%d>: handling flags 0x%u (%d) "
5526 "not implemented yet.", getpid (), flags, flags);
5529 return (vppcom_session_write (session_index, buffer, buflen));
5533 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
5535 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
5536 u32 i, keep_trying = 1;
5539 if (VPPCOM_DEBUG > 3)
5540 clib_warning ("VCL<%d>: vp %p, nsids %u, wait_for_time %f",
5541 getpid (), vp, n_sids, wait_for_time);
5544 return VPPCOM_EFAULT;
5550 for (i = 0; i < n_sids; i++)
5552 ASSERT (vp[i].revents);
5554 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5555 clib_spinlock_unlock (&vcm->sessions_lockp);
5560 if (POLLIN & vp[i].events)
5562 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5563 rv = vppcom_session_read_ready (session, vp[i].sid);
5564 clib_spinlock_unlock (&vcm->sessions_lockp);
5567 *vp[i].revents |= POLLIN;
5574 case VPPCOM_ECONNRESET:
5575 *vp[i].revents = POLLHUP;
5579 *vp[i].revents = POLLERR;
5586 if (POLLOUT & vp[i].events)
5588 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5589 rv = vppcom_session_write_ready (session, vp[i].sid);
5590 clib_spinlock_unlock (&vcm->sessions_lockp);
5593 *vp[i].revents |= POLLOUT;
5600 case VPPCOM_ECONNRESET:
5601 *vp[i].revents = POLLHUP;
5605 *vp[i].revents = POLLERR;
5612 if (0) // Note "done:" label used by VCL_LOCK_AND_GET_SESSION()
5615 *vp[i].revents = POLLNVAL;
5619 if (wait_for_time != -1)
5620 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
5622 while ((num_ev == 0) && keep_trying);
5624 if (VPPCOM_DEBUG > 3)
5626 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
5627 for (i = 0; i < n_sids; i++)
5629 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
5630 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
5631 vp[i].events, *vp[i].revents);
5638 * fd.io coding-style-patch-verification: ON
5641 * eval: (c-set-style "gnu")