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_session_listener
208 vppcom_session_listener_cb user_cb;
209 vppcom_session_listener_errcb user_errcb;
211 } vppcom_session_listener_t;
213 typedef struct vppcom_main_t_
219 /* FIFO for accepted connections - used in epoll/select */
220 clib_spinlock_t session_fifo_lockp;
221 u32 *client_session_index_fifo;
223 /* vpp input queue */
224 svm_queue_t *vl_input_queue;
226 /* API client handle */
229 clib_spinlock_t sessions_lockp;
232 /* Hash table for disconnect processing */
233 uword *session_index_by_vpp_handles;
236 clib_bitmap_t *rd_bitmap;
237 clib_bitmap_t *wr_bitmap;
238 clib_bitmap_t *ex_bitmap;
240 /* Our event queue */
241 svm_queue_t *app_event_queue;
243 /* unique segment name counter */
244 u32 unique_segment_index;
246 /* For deadman timers */
247 clib_time_t clib_time;
249 /* State of the connection, shared between msg RX thread and main thread */
250 volatile app_state_t app_state;
255 vce_event_thread_t event_thread;
257 /* VPP Event-logger */
258 elog_main_t elog_main;
259 elog_track_t elog_track;
261 /* VNET_API_ERROR_FOO -> "Foo" hash table */
262 uword *error_string_by_error_number;
265 /* NOTE: _vppcom_main is only used until the heap is allocated.
266 * Do not access it directly -- use vcm which will point to
267 * the heap allocated copy after init.
269 static vppcom_main_t _vppcom_main = {
270 .debug = VPPCOM_DEBUG_INIT,
271 .my_client_index = ~0
274 static vppcom_main_t *vcm = &_vppcom_main;
276 #define VCL_LOCK_AND_GET_SESSION(I, S) \
278 clib_spinlock_lock (&vcm->sessions_lockp); \
279 rv = vppcom_session_at_index (I, S); \
280 if (PREDICT_FALSE (rv)) \
282 clib_spinlock_unlock (&vcm->sessions_lockp); \
283 clib_warning ("VCL<%d>: ERROR: Invalid ##I (%u)!", \
290 vppcom_app_state_str (app_state_t state)
296 case STATE_APP_START:
297 st = "STATE_APP_START";
300 case STATE_APP_CONN_VPP:
301 st = "STATE_APP_CONN_VPP";
304 case STATE_APP_ENABLED:
305 st = "STATE_APP_ENABLED";
308 case STATE_APP_ATTACHED:
309 st = "STATE_APP_ATTACHED";
313 st = "UNKNOWN_APP_STATE";
321 vppcom_session_state_str (session_state_t state)
332 st = "STATE_CONNECT";
343 case STATE_CLOSE_ON_EMPTY:
344 st = "STATE_CLOSE_ON_EMPTY";
347 case STATE_DISCONNECT:
348 st = "STATE_DISCONNECT";
356 st = "UNKNOWN_STATE";
365 * VPPCOM Utility Functions
368 vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
370 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
371 if (PREDICT_FALSE ((session_index == ~0) ||
372 pool_is_free_index (vcm->sessions, session_index)))
374 clib_warning ("VCL<%d>: invalid session, sid (%u) has been closed!",
375 getpid (), session_index);
376 return VPPCOM_EBADFD;
378 *sess = pool_elt_at_index (vcm->sessions, session_index);
383 vppcom_session_table_add_listener (u64 listener_handle, u32 value)
385 /* Session and listener handles have different formats. The latter has
386 * the thread index in the upper 32 bits while the former has the session
387 * type. Knowing that, for listeners we just flip the MSB to 1 */
388 listener_handle |= 1ULL << 63;
389 hash_set (vcm->session_index_by_vpp_handles, listener_handle, value);
392 static inline session_t *
393 vppcom_session_table_lookup_listener (u64 listener_handle)
396 u64 handle = listener_handle | (1ULL << 63);
399 p = hash_get (vcm->session_index_by_vpp_handles, handle);
402 clib_warning ("VCL<%d>: couldn't find listen session: unknown vpp "
403 "listener handle %llx", getpid (), listener_handle);
406 if (pool_is_free_index (vcm->sessions, p[0]))
408 if (VPPCOM_DEBUG > 1)
409 clib_warning ("VCL<%d>: invalid listen session, sid (%u)",
414 session = pool_elt_at_index (vcm->sessions, p[0]);
415 ASSERT (session->state & STATE_LISTEN);
420 vppcom_session_table_del_listener (u64 listener_handle)
422 listener_handle |= 1ULL << 63;
423 hash_unset (vcm->session_index_by_vpp_handles, listener_handle);
429 elog_main_t *em = &vcm->elog_main;
431 clib_error_t *error = 0;
434 (char *) format (0, "%s/%d-%d-vcl-elog%c", vcm->cfg.event_log_path,
435 vcm->my_client_index, getpid (), 0);
436 error = elog_write_file (em, chroot_file, 1 /* flush ring */ );
439 clib_error_report (error);
441 if (VPPCOM_DEBUG > 0)
442 clib_warning ("[%d] Event Log:'%s' ", getpid (), chroot_file);
447 vppcom_send_accept_session_reply (u64 handle, u32 context, int retval)
449 vl_api_accept_session_reply_t *rmp;
451 rmp = vl_msg_api_alloc (sizeof (*rmp));
452 memset (rmp, 0, sizeof (*rmp));
453 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
454 rmp->retval = htonl (retval);
455 rmp->context = context;
456 rmp->handle = handle;
457 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
461 * VPPCOM Event Functions
465 vce_registered_listener_connect_handler_fn (void *arg)
467 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
468 vce_event_connect_request_t *ecr;
472 session_t *new_session;
475 vppcom_session_listener_t *session_listener =
476 (vppcom_session_listener_t *) reg->handler_fn_args;
478 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
480 ecr = (vce_event_connect_request_t *) ev->data;
481 VCL_LOCK_AND_GET_SESSION (ecr->accepted_session_index, &new_session);
484 ep.is_ip4 = new_session->peer_addr.is_ip4;
485 ep.port = new_session->peer_port;
486 if (new_session->peer_addr.is_ip4)
487 clib_memcpy (&ep.ip, &new_session->peer_addr.ip46.ip4,
488 sizeof (ip4_address_t));
490 clib_memcpy (&ep.ip, &new_session->peer_addr.ip46.ip6,
491 sizeof (ip6_address_t));
493 vppcom_send_accept_session_reply (new_session->vpp_handle,
494 new_session->client_context,
496 clib_spinlock_unlock (&vcm->sessions_lockp);
498 (session_listener->user_cb) (ecr->accepted_session_index, &ep,
499 session_listener->user_cb_data);
501 /*TODO - Unregister check in close for this listener */
506 ASSERT (0); // If we can't get a lock or accepted session fails, lets blow up.
510 * * @brief vce_connect_request_handler_fn
511 * - used for listener sessions
512 * - when a vl_api_accept_session_t_handler() generates an event
513 * this callback is alerted and sets fields that consumers such as
514 * vppcom_session_accept() expect to see, ie. accepted_client_index
516 * @param arg - void* to be cast to vce_event_handler_reg_t*
519 vce_connect_request_handler_fn (void *arg)
521 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
523 pthread_mutex_lock (®->handler_lock);
524 pthread_cond_signal (®->handler_cond);
525 pthread_mutex_unlock (®->handler_lock);
529 * @brief vce_poll_wait_connect_request_handler_fn
530 * - used by vppcom_epoll_xxxx() for listener sessions
531 * - when a vl_api_accept_session_t_handler() generates an event
532 * this callback is alerted and sets the fields that vppcom_epoll_wait()
535 * @param arg - void* to be cast to vce_event_handler_reg_t*
538 vce_poll_wait_connect_request_handler_fn (void *arg)
540 vce_event_handler_reg_t *reg = (vce_event_handler_reg_t *) arg;
542 /* Retrieve the VCL_EVENT_CONNECT_REQ_ACCEPTED event */
543 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
544 vce_event_connect_request_t *ecr = (vce_event_connect_request_t *) ev->data;
546 /* Add the accepted_session_index to the FIFO */
547 clib_spinlock_lock (&vcm->session_fifo_lockp);
548 clib_fifo_add1 (vcm->client_session_index_fifo,
549 ecr->accepted_session_index);
550 clib_spinlock_unlock (&vcm->session_fifo_lockp);
552 /* Recycling the event. */
553 clib_spinlock_lock (&(vcm->event_thread.events_lockp));
554 vcm->event_thread.recycle_event = 1;
555 clib_fifo_add1 (vcm->event_thread.event_index_fifo, reg->ev_idx);
556 clib_spinlock_unlock (&(vcm->event_thread.events_lockp));
560 vppcom_connect_to_vpp (char *app_name)
562 api_main_t *am = &api_main;
563 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
566 if (!vcl_cfg->vpp_api_filename)
567 vcl_cfg->vpp_api_filename = format (0, "/vpe-api%c", 0);
569 if (VPPCOM_DEBUG > 0)
570 clib_warning ("VCL<%d>: app (%s) connecting to VPP api (%s)...",
571 getpid (), app_name, vcl_cfg->vpp_api_filename);
573 if (vl_client_connect_to_vlib ((char *) vcl_cfg->vpp_api_filename, app_name,
574 vcm->cfg.vpp_api_q_length) < 0)
576 clib_warning ("VCL<%d>: app (%s) connect failed!", getpid (), app_name);
577 rv = VPPCOM_ECONNREFUSED;
581 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
582 vcm->my_client_index = (u32) am->my_client_index;
583 vcm->app_state = STATE_APP_CONN_VPP;
585 if (VPPCOM_DEBUG > 0)
586 clib_warning ("VCL<%d>: app (%s) is connected to VPP!",
587 getpid (), app_name);
590 if (VPPCOM_DEBUG > 0)
592 vcm->elog_main.lock =
593 clib_mem_alloc_aligned (CLIB_CACHE_LINE_BYTES, CLIB_CACHE_LINE_BYTES);
594 vcm->elog_main.lock[0] = 0;
595 vcm->elog_main.event_ring_size = vcm->cfg.event_ring_size;
596 elog_init (&vcm->elog_main, vcm->elog_main.event_ring_size);
597 elog_enable_disable (&vcm->elog_main, 1);
599 vcm->elog_track.name =
600 (char *) format (0, "P:%d:C:%d%c", getpid (),
601 vcm->my_client_index, 0);
602 elog_track_register (&vcm->elog_main, &vcm->elog_track);
605 ELOG_TYPE_DECLARE (e) =
607 .format = "connect_vpp:rv:%d",
614 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
622 format_api_error (u8 * s, va_list * args)
624 i32 error = va_arg (*args, u32);
627 p = hash_get (vcm->error_string_by_error_number, -error);
630 s = format (s, "%s (%d)", p[0], error);
632 s = format (s, "%d", error);
637 vppcom_init_error_string_table (void)
639 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
641 #define _(n, v, s) hash_set (vcm->error_string_by_error_number, -v, s);
642 foreach_vnet_api_error;
645 hash_set (vcm->error_string_by_error_number, 99, "Misc");
649 vppcom_wait_for_app_state_change (app_state_t app_state)
651 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
653 while (clib_time_now (&vcm->clib_time) < timeout)
655 if (vcm->app_state == app_state)
658 if (VPPCOM_DEBUG > 0)
659 clib_warning ("VCL<%d>: timeout waiting for state %s (%d)", getpid (),
660 vppcom_app_state_str (app_state), app_state);
662 if (VPPCOM_DEBUG > 0)
665 ELOG_TYPE_DECLARE (e) =
667 .format = "ERR: timeout state:%d",
675 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
677 ed->data = app_state;
681 return VPPCOM_ETIMEDOUT;
685 vppcom_wait_for_session_state_change (u32 session_index,
686 session_state_t state,
689 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
690 session_t *volatile session;
695 clib_spinlock_lock (&vcm->sessions_lockp);
696 rv = vppcom_session_at_index (session_index, &session);
697 if (PREDICT_FALSE (rv))
699 clib_spinlock_unlock (&vcm->sessions_lockp);
702 if (session->state & state)
704 clib_spinlock_unlock (&vcm->sessions_lockp);
707 if (session->state & STATE_FAILED)
709 clib_spinlock_unlock (&vcm->sessions_lockp);
710 return VPPCOM_ECONNREFUSED;
713 clib_spinlock_unlock (&vcm->sessions_lockp);
715 while (clib_time_now (&vcm->clib_time) < timeout);
717 if (VPPCOM_DEBUG > 0)
718 clib_warning ("VCL<%d>: timeout waiting for state 0x%x (%s)", getpid (),
719 state, vppcom_session_state_str (state));
721 if (VPPCOM_DEBUG > 0)
724 ELOG_TYPE_DECLARE (e) =
726 .format = "ERR: timeout state:%d",
734 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
740 return VPPCOM_ETIMEDOUT;
744 * VPP-API message functions
747 vppcom_send_session_enable_disable (u8 is_enable)
749 vl_api_session_enable_disable_t *bmp;
750 bmp = vl_msg_api_alloc (sizeof (*bmp));
751 memset (bmp, 0, sizeof (*bmp));
753 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
754 bmp->client_index = vcm->my_client_index;
755 bmp->context = htonl (0xfeedface);
756 bmp->is_enable = is_enable;
757 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
761 vppcom_app_session_enable (void)
765 if (vcm->app_state != STATE_APP_ENABLED)
767 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
768 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
769 if (PREDICT_FALSE (rv))
771 if (VPPCOM_DEBUG > 0)
772 clib_warning ("VCL<%d>: application session enable timed out! "
774 getpid (), rv, vppcom_retval_str (rv));
782 vl_api_session_enable_disable_reply_t_handler
783 (vl_api_session_enable_disable_reply_t * mp)
787 clib_warning ("VCL<%d>: session_enable_disable failed: %U", getpid (),
788 format_api_error, ntohl (mp->retval));
791 vcm->app_state = STATE_APP_ENABLED;
795 vppcom_app_send_attach (void)
797 vl_api_application_attach_t *bmp;
798 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
799 u8 app_is_proxy = (vcm->cfg.app_proxy_transport_tcp ||
800 vcm->cfg.app_proxy_transport_udp);
802 bmp = vl_msg_api_alloc (sizeof (*bmp));
803 memset (bmp, 0, sizeof (*bmp));
805 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
806 bmp->client_index = vcm->my_client_index;
807 bmp->context = htonl (0xfeedface);
808 bmp->options[APP_OPTIONS_FLAGS] =
809 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
810 (vcm->cfg.app_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
811 (vcm->cfg.app_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0) |
812 (app_is_proxy ? APP_OPTIONS_FLAGS_IS_PROXY : 0);
813 bmp->options[APP_OPTIONS_PROXY_TRANSPORT] =
814 (vcm->cfg.app_proxy_transport_tcp ? 1 << TRANSPORT_PROTO_TCP : 0) |
815 (vcm->cfg.app_proxy_transport_udp ? 1 << TRANSPORT_PROTO_UDP : 0);
816 bmp->options[APP_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
817 bmp->options[APP_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
818 bmp->options[APP_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
819 bmp->options[APP_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
820 bmp->options[APP_OPTIONS_PREALLOC_FIFO_PAIRS] =
821 vcm->cfg.preallocated_fifo_pairs;
822 bmp->options[APP_OPTIONS_EVT_QUEUE_SIZE] = vcm->cfg.event_queue_size;
825 bmp->namespace_id_len = nsid_len;
826 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
827 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
829 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
833 vppcom_app_attach (void)
837 vppcom_app_send_attach ();
838 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
839 if (PREDICT_FALSE (rv))
841 if (VPPCOM_DEBUG > 0)
842 clib_warning ("VCL<%d>: application attach timed out! "
844 getpid (), rv, vppcom_retval_str (rv));
852 vppcom_app_detach (void)
854 vl_api_application_detach_t *bmp;
855 bmp = vl_msg_api_alloc (sizeof (*bmp));
856 memset (bmp, 0, sizeof (*bmp));
858 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
859 bmp->client_index = vcm->my_client_index;
860 bmp->context = htonl (0xfeedface);
861 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
865 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
868 static svm_fifo_segment_create_args_t _a;
869 svm_fifo_segment_create_args_t *a = &_a;
872 memset (a, 0, sizeof (*a));
875 clib_warning ("VCL<%d>: attach failed: %U", getpid (),
876 format_api_error, ntohl (mp->retval));
880 if (mp->segment_name_length == 0)
882 clib_warning ("VCL<%d>: segment_name_length zero", getpid ());
886 a->segment_name = (char *) mp->segment_name;
887 a->segment_size = mp->segment_size;
889 ASSERT (mp->app_event_queue_address);
891 /* Attach to the segment vpp created */
892 rv = svm_fifo_segment_attach (a);
893 vec_reset_length (a->new_segment_indices);
894 if (PREDICT_FALSE (rv))
896 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
897 getpid (), mp->segment_name);
901 vcm->app_event_queue =
902 uword_to_pointer (mp->app_event_queue_address, svm_queue_t *);
904 vcm->app_state = STATE_APP_ATTACHED;
908 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
912 clib_warning ("VCL<%d>: detach failed: %U", getpid (), format_api_error,
915 vcm->app_state = STATE_APP_ENABLED;
919 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
923 clib_warning ("VCL<%d>: vpp handle 0x%llx: disconnect session failed: %U",
924 getpid (), mp->handle, format_api_error,
929 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
931 static svm_fifo_segment_create_args_t _a;
932 svm_fifo_segment_create_args_t *a = &_a;
935 memset (a, 0, sizeof (*a));
936 a->segment_name = (char *) mp->segment_name;
937 a->segment_size = mp->segment_size;
938 /* Attach to the segment vpp created */
939 rv = svm_fifo_segment_attach (a);
940 vec_reset_length (a->new_segment_indices);
941 if (PREDICT_FALSE (rv))
943 clib_warning ("VCL<%d>: svm_fifo_segment_attach ('%s') failed",
944 getpid (), mp->segment_name);
947 if (VPPCOM_DEBUG > 1)
948 clib_warning ("VCL<%d>: mapped new segment '%s' size %d", getpid (),
949 mp->segment_name, mp->segment_size);
953 vl_api_unmap_segment_t_handler (vl_api_unmap_segment_t * mp)
957 * XXX Need segment_name to session_id hash,
958 * XXX - have sessionID by handle hash currently
960 if (VPPCOM_DEBUG > 1)
961 clib_warning ("Unmapped segment '%s'", mp->segment_name);
965 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
969 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
973 session_t *session = 0;
974 u32 session_index = p[0];
976 VCL_LOCK_AND_GET_SESSION (session_index, &session);
977 session->state = STATE_CLOSE_ON_EMPTY;
979 if (VPPCOM_DEBUG > 1)
980 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
981 "setting state to 0x%x (%s)",
982 getpid (), mp->handle, session_index, session->state,
983 vppcom_session_state_str (session->state));
984 clib_spinlock_unlock (&vcm->sessions_lockp);
988 if (VPPCOM_DEBUG > 1)
989 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
990 "session lookup failed!",
991 getpid (), mp->handle, session_index);
994 clib_warning ("VCL<%d>: vpp handle 0x%llx: session lookup by "
995 "handle failed!", getpid (), mp->handle);
999 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
1001 session_t *session = 0;
1002 vl_api_reset_session_reply_t *rmp;
1006 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
1010 clib_spinlock_lock (&vcm->sessions_lockp);
1011 rval = vppcom_session_at_index (p[0], &session);
1012 if (PREDICT_FALSE (rval))
1014 rv = VNET_API_ERROR_INVALID_VALUE_2;
1015 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1016 "session lookup failed! returning %d %U",
1017 getpid (), mp->handle, p[0],
1018 rv, format_api_error, rv);
1022 /* TBD: should this disconnect immediately and
1025 session->state = STATE_CLOSE_ON_EMPTY;
1027 if (VPPCOM_DEBUG > 1)
1028 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1029 "state set to %d (%s)!", getpid (),
1030 mp->handle, p[0], session->state,
1031 vppcom_session_state_str (session->state));
1033 clib_spinlock_unlock (&vcm->sessions_lockp);
1037 rv = VNET_API_ERROR_INVALID_VALUE;
1038 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx: session lookup "
1039 "failed! returning %d %U",
1040 getpid (), mp->handle, rv, format_api_error, rv);
1043 rmp = vl_msg_api_alloc (sizeof (*rmp));
1044 memset (rmp, 0, sizeof (*rmp));
1045 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
1046 rmp->retval = htonl (rv);
1047 rmp->handle = mp->handle;
1048 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1052 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
1054 session_t *session = 0;
1056 svm_fifo_t *rx_fifo, *tx_fifo;
1059 session_index = mp->context;
1060 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1064 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1065 "connect failed! %U",
1066 getpid (), mp->handle, session_index,
1067 format_api_error, ntohl (mp->retval));
1070 session->state = STATE_FAILED;
1071 session->vpp_handle = mp->handle;
1075 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
1076 "Invalid session index (%u)!",
1077 getpid (), mp->handle, session_index);
1088 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1091 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1092 rx_fifo->client_session_index = session_index;
1093 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1094 tx_fifo->client_session_index = session_index;
1096 session->rx_fifo = rx_fifo;
1097 session->tx_fifo = tx_fifo;
1098 session->vpp_handle = mp->handle;
1099 session->lcl_addr.is_ip4 = mp->is_ip4;
1100 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
1101 sizeof (session->peer_addr.ip46));
1102 session->lcl_port = mp->lcl_port;
1103 session->state = STATE_CONNECT;
1105 /* Add it to lookup table */
1106 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1108 if (VPPCOM_DEBUG > 1)
1109 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connect succeeded!"
1110 " session_rx_fifo %p, refcnt %d,"
1111 " session_tx_fifo %p, refcnt %d",
1112 getpid (), mp->handle, session_index,
1114 session->rx_fifo->refcnt,
1115 session->tx_fifo, session->tx_fifo->refcnt);
1117 clib_spinlock_unlock (&vcm->sessions_lockp);
1121 vppcom_send_connect_sock (session_t * session, u32 session_index)
1123 vl_api_connect_sock_t *cmp;
1125 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
1126 cmp = vl_msg_api_alloc (sizeof (*cmp));
1127 memset (cmp, 0, sizeof (*cmp));
1128 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
1129 cmp->client_index = vcm->my_client_index;
1130 cmp->context = session_index;
1132 cmp->is_ip4 = session->peer_addr.is_ip4;
1133 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
1134 cmp->port = session->peer_port;
1135 cmp->proto = session->proto;
1136 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
1137 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
1141 vppcom_send_disconnect_session_reply (u64 vpp_handle, u32 session_index,
1144 vl_api_disconnect_session_reply_t *rmp;
1146 if (VPPCOM_DEBUG > 1)
1147 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1148 "sending disconnect msg",
1149 getpid (), vpp_handle, session_index);
1151 rmp = vl_msg_api_alloc (sizeof (*rmp));
1152 memset (rmp, 0, sizeof (*rmp));
1154 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
1155 rmp->retval = htonl (rv);
1156 rmp->handle = vpp_handle;
1157 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1161 vppcom_send_disconnect_session (u64 vpp_handle, u32 session_index)
1163 vl_api_disconnect_session_t *dmp;
1165 if (VPPCOM_DEBUG > 1)
1166 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1167 "sending disconnect msg",
1168 getpid (), vpp_handle, session_index);
1170 dmp = vl_msg_api_alloc (sizeof (*dmp));
1171 memset (dmp, 0, sizeof (*dmp));
1172 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
1173 dmp->client_index = vcm->my_client_index;
1174 dmp->handle = vpp_handle;
1175 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
1179 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
1181 session_t *session = 0;
1182 u32 session_index = mp->context;
1185 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1189 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, "
1190 "sid %u: bind failed: %U",
1191 getpid (), mp->handle, session_index,
1192 format_api_error, ntohl (mp->retval));
1193 rv = vppcom_session_at_index (session_index, &session);
1194 if (rv == VPPCOM_OK)
1196 session->state = STATE_FAILED;
1197 session->vpp_handle = mp->handle;
1201 clib_warning ("[%s] ERROR: vpp handle 0x%llx, sid %u: "
1202 "Invalid session index (%u)!",
1203 getpid (), mp->handle, session_index);
1208 session->vpp_handle = mp->handle;
1209 session->lcl_addr.is_ip4 = mp->lcl_is_ip4;
1210 clib_memcpy (&session->lcl_addr.ip46, mp->lcl_ip,
1211 sizeof (session->peer_addr.ip46));
1212 session->lcl_port = mp->lcl_port;
1213 vppcom_session_table_add_listener (mp->handle, session_index);
1214 session->state = STATE_LISTEN;
1216 if (VPPCOM_DEBUG > 1)
1217 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
1218 getpid (), mp->handle, mp->context);
1220 clib_spinlock_unlock (&vcm->sessions_lockp);
1224 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
1227 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
1228 getpid (), mp->context, format_api_error,
1229 ntohl (mp->retval));
1231 else if (VPPCOM_DEBUG > 1)
1232 clib_warning ("VCL<%d>: sid %u: unbind succeeded!",
1233 getpid (), mp->context);
1237 format_ip4_address (u8 * s, va_list * args)
1239 u8 *a = va_arg (*args, u8 *);
1240 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1244 format_ip6_address (u8 * s, va_list * args)
1246 ip6_address_t *a = va_arg (*args, ip6_address_t *);
1247 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
1249 i_max_n_zero = ARRAY_LEN (a->as_u16);
1251 i_first_zero = i_max_n_zero;
1253 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1255 u32 is_zero = a->as_u16[i] == 0;
1256 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1262 if ((!is_zero && n_zeros > max_n_zeros)
1263 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1265 i_max_n_zero = i_first_zero;
1266 max_n_zeros = n_zeros;
1267 i_first_zero = ARRAY_LEN (a->as_u16);
1272 last_double_colon = 0;
1273 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1275 if (i == i_max_n_zero && max_n_zeros > 1)
1277 s = format (s, "::");
1278 i += max_n_zeros - 1;
1279 last_double_colon = 1;
1283 s = format (s, "%s%x",
1284 (last_double_colon || i == 0) ? "" : ":",
1285 clib_net_to_host_u16 (a->as_u16[i]));
1286 last_double_colon = 0;
1293 /* Format an IP46 address. */
1295 format_ip46_address (u8 * s, va_list * args)
1297 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1298 ip46_type_t type = va_arg (*args, ip46_type_t);
1304 is_ip4 = ip46_address_is_ip4 (ip46);
1315 format (s, "%U", format_ip4_address, &ip46->ip4) :
1316 format (s, "%U", format_ip6_address, &ip46->ip6);
1320 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1322 svm_fifo_t *rx_fifo, *tx_fifo;
1323 session_t *session, *listen_session;
1325 vce_event_connect_request_t *ecr;
1331 clib_spinlock_lock (&vcm->sessions_lockp);
1333 clib_spinlock_lock (&vcm->session_fifo_lockp);
1334 elts = clib_fifo_free_elts (vcm->client_session_index_fifo);
1335 clib_spinlock_unlock (&vcm->session_fifo_lockp);
1339 clib_warning ("VCL<%d>: client session queue is full!", getpid ());
1340 vppcom_send_accept_session_reply (mp->handle, mp->context,
1341 VNET_API_ERROR_QUEUE_FULL);
1342 clib_spinlock_unlock (&vcm->sessions_lockp);
1346 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1347 if (!listen_session)
1349 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
1350 "unknown vpp listener handle %llx",
1351 getpid (), mp->listener_handle);
1352 vppcom_send_accept_session_reply (mp->handle, mp->context,
1353 VNET_API_ERROR_INVALID_ARGUMENT);
1354 clib_spinlock_unlock (&vcm->sessions_lockp);
1358 /* TODO check listener depth and update */
1359 /* TODO on "child" fd close, update listener depth */
1361 /* Allocate local session and set it up */
1362 pool_get (vcm->sessions, session);
1363 memset (session, 0, sizeof (*session));
1364 session_index = session - vcm->sessions;
1366 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1367 rx_fifo->client_session_index = session_index;
1368 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1369 tx_fifo->client_session_index = session_index;
1371 session->vpp_handle = mp->handle;
1372 session->client_context = mp->context;
1373 session->rx_fifo = rx_fifo;
1374 session->tx_fifo = tx_fifo;
1375 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1377 session->state = STATE_ACCEPT;
1378 session->peer_port = mp->port;
1379 session->peer_addr.is_ip4 = mp->is_ip4;
1380 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1381 sizeof (session->peer_addr.ip46));
1383 /* Add it to lookup table */
1384 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1385 session->lcl_port = listen_session->lcl_port;
1386 session->lcl_addr = listen_session->lcl_addr;
1388 /* Create an event for handlers */
1390 clib_spinlock_lock (&vcm->event_thread.events_lockp);
1392 pool_get (vcm->event_thread.vce_events, ev);
1393 ev->data = clib_mem_alloc (sizeof (vce_event_connect_request_t));
1395 ev_idx = (u32) (ev - vcm->event_thread.vce_events);
1397 ev->evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
1398 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1399 ev->evk.session_index = (u32) (listen_session - vcm->sessions);
1400 ecr->accepted_session_index = session_index;
1402 clib_spinlock_unlock (&vcm->event_thread.events_lockp);
1404 rv = vce_generate_event (&vcm->event_thread, ev_idx);
1408 if (VPPCOM_DEBUG > 1)
1409 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client accept "
1410 "request from %s address %U port %d queue %p!", getpid (),
1411 mp->handle, session_index, mp->is_ip4 ? "IPv4" : "IPv6",
1412 format_ip46_address, &mp->ip, mp->is_ip4,
1413 clib_net_to_host_u16 (mp->port), session->vpp_event_queue);
1415 if (VPPCOM_DEBUG > 0)
1417 session->elog_track.name =
1418 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
1420 elog_track_register (&vcm->elog_main, &session->elog_track);
1422 if (session->peer_addr.is_ip4)
1425 ELOG_TYPE_DECLARE (e) =
1428 "client_accept:handle:%x addr:%d.%d.%d.%d:%d",
1429 .format_args = "i8i1i1i1i1i2",
1432 CLIB_PACKED (struct {
1438 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1440 ed->handle = mp->handle;
1441 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
1442 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
1443 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
1444 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
1445 ed->port = clib_net_to_host_u16 (session->peer_port);
1450 clib_warning ("ip6");
1454 clib_spinlock_unlock (&vcm->sessions_lockp);
1459 vppcom_send_bind_sock (session_t * session, u32 session_index)
1461 vl_api_bind_sock_t *bmp;
1463 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1464 bmp = vl_msg_api_alloc (sizeof (*bmp));
1465 memset (bmp, 0, sizeof (*bmp));
1467 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1468 bmp->client_index = vcm->my_client_index;
1469 bmp->context = session_index;
1470 bmp->is_ip4 = session->lcl_addr.is_ip4;
1471 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1472 bmp->port = session->lcl_port;
1473 bmp->proto = session->proto;
1474 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1475 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1479 vppcom_send_unbind_sock (u64 vpp_handle)
1481 vl_api_unbind_sock_t *ump;
1483 ump = vl_msg_api_alloc (sizeof (*ump));
1484 memset (ump, 0, sizeof (*ump));
1486 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1487 ump->client_index = vcm->my_client_index;
1488 ump->handle = vpp_handle;
1489 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1493 vppcom_session_unbind (u32 session_index)
1495 session_t *session = 0;
1498 elog_track_t session_elog_track;
1500 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1502 vpp_handle = session->vpp_handle;
1503 vppcom_session_table_del_listener (vpp_handle);
1504 session->vpp_handle = ~0;
1505 session->state = STATE_DISCONNECT;
1506 session_elog_track = session->elog_track;
1508 clib_spinlock_unlock (&vcm->sessions_lockp);
1510 if (VPPCOM_DEBUG > 1)
1511 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1512 "sending unbind msg! new state 0x%x (%s)",
1513 getpid (), vpp_handle, session_index,
1515 vppcom_session_state_str (STATE_DISCONNECT));
1517 if (VPPCOM_DEBUG > 0)
1520 ELOG_TYPE_DECLARE (e) =
1522 .format = "unbind: handle:%x",
1523 .format_args = "i8",
1531 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
1532 ed->handle = vpp_handle;
1536 vppcom_send_unbind_sock (vpp_handle);
1543 vppcom_session_disconnect (u32 session_index)
1548 session_state_t state;
1550 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1552 vpp_handle = session->vpp_handle;
1553 state = session->state;
1554 clib_spinlock_unlock (&vcm->sessions_lockp);
1556 if (VPPCOM_DEBUG > 1)
1558 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u "
1560 getpid (), vpp_handle, session_index,
1561 state, vppcom_session_state_str (state));
1564 if (PREDICT_FALSE (state & STATE_LISTEN))
1566 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1567 "Cannot disconnect a listen socket!",
1568 getpid (), vpp_handle, session_index);
1573 /* The peer has already initiated the close,
1574 * so send the disconnect session reply.
1576 if (state & STATE_CLOSE_ON_EMPTY)
1578 //XXX alagalah - Check and drain here?
1579 vppcom_send_disconnect_session_reply (vpp_handle,
1580 session_index, 0 /* rv */ );
1581 if (VPPCOM_DEBUG > 1)
1582 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1583 "sending disconnect REPLY...",
1584 getpid (), vpp_handle, session_index);
1587 /* Otherwise, send a disconnect session msg...
1591 if (VPPCOM_DEBUG > 1)
1592 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1593 "sending disconnect...",
1594 getpid (), vpp_handle, session_index);
1596 vppcom_send_disconnect_session (vpp_handle, session_index);
1603 #define foreach_sock_msg \
1604 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1605 _(BIND_SOCK_REPLY, bind_sock_reply) \
1606 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1607 _(ACCEPT_SESSION, accept_session) \
1608 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1609 _(DISCONNECT_SESSION, disconnect_session) \
1610 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1611 _(RESET_SESSION, reset_session) \
1612 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1613 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1614 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
1615 _(UNMAP_SEGMENT, unmap_segment)
1618 vppcom_api_hookup (void)
1621 vl_msg_api_set_handlers(VL_API_##N, #n, \
1622 vl_api_##n##_t_handler, \
1624 vl_api_##n##_t_endian, \
1625 vl_api_##n##_t_print, \
1626 sizeof(vl_api_##n##_t), 1);
1632 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1636 vcl_cfg->heapsize = (256ULL << 20);
1637 vcl_cfg->vpp_api_q_length = 1024;
1638 vcl_cfg->segment_baseva = 0x200000000ULL;
1639 vcl_cfg->segment_size = (256 << 20);
1640 vcl_cfg->add_segment_size = (128 << 20);
1641 vcl_cfg->preallocated_fifo_pairs = 8;
1642 vcl_cfg->rx_fifo_size = (1 << 20);
1643 vcl_cfg->tx_fifo_size = (1 << 20);
1644 vcl_cfg->event_queue_size = 2048;
1645 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1646 vcl_cfg->app_timeout = 10 * 60.0;
1647 vcl_cfg->session_timeout = 10 * 60.0;
1648 vcl_cfg->accept_timeout = 60.0;
1649 vcl_cfg->event_ring_size = (128 << 10);
1650 vcl_cfg->event_log_path = "/dev/shm";
1654 vppcom_cfg_heapsize (char *conf_fname)
1656 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1669 fp = fopen (conf_fname, "r");
1672 if (VPPCOM_DEBUG > 0)
1673 clib_warning ("VCL<%d>: using default heapsize %lld (0x%llx)",
1674 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1678 argv = calloc (1, sizeof (char *));
1681 if (VPPCOM_DEBUG > 0)
1682 clib_warning ("VCL<%d>: calloc failed, using default "
1683 "heapsize %lld (0x%llx)",
1684 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1690 if (fgets (inbuf, 4096, fp) == 0)
1692 p = strtok (inbuf, " \t\n");
1698 char **tmp = realloc (argv, argc * sizeof (char *));
1701 if (VPPCOM_DEBUG > 0)
1702 clib_warning ("VCL<%d>: realloc failed, "
1703 "using default heapsize %lld (0x%llx)",
1704 getpid (), vcl_cfg->heapsize,
1709 arg = strndup (p, 1024);
1712 if (VPPCOM_DEBUG > 0)
1713 clib_warning ("VCL<%d>: strndup failed, "
1714 "using default heapsize %lld (0x%llx)",
1715 getpid (), vcl_cfg->heapsize,
1719 argv[argc - 1] = arg;
1720 p = strtok (NULL, " \t\n");
1727 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1730 if (VPPCOM_DEBUG > 0)
1731 clib_warning ("VCL<%d>: realloc failed, "
1732 "using default heapsize %lld (0x%llx)",
1733 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1740 * Look for and parse the "heapsize" config parameter.
1741 * Manual since none of the clib infra has been bootstrapped yet.
1743 * Format: heapsize <nn>[mM][gG]
1746 for (i = 1; i < (argc - 1); i++)
1748 if (!strncmp (argv[i], "heapsize", 8))
1750 sizep = (u8 *) argv[i + 1];
1752 while (*sizep >= '0' && *sizep <= '9')
1755 size += *sizep++ - '0';
1759 if (VPPCOM_DEBUG > 0)
1760 clib_warning ("VCL<%d>: parse error '%s %s', "
1761 "using default heapsize %lld (0x%llx)",
1762 getpid (), argv[i], argv[i + 1],
1763 vcl_cfg->heapsize, vcl_cfg->heapsize);
1767 if (*sizep == 'g' || *sizep == 'G')
1768 vcl_cfg->heapsize = size << 30;
1769 else if (*sizep == 'm' || *sizep == 'M')
1770 vcl_cfg->heapsize = size << 20;
1773 if (VPPCOM_DEBUG > 0)
1774 clib_warning ("VCL<%d>: parse error '%s %s', "
1775 "using default heapsize %lld (0x%llx)",
1776 getpid (), argv[i], argv[i + 1],
1777 vcl_cfg->heapsize, vcl_cfg->heapsize);
1789 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1790 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1791 if (vcl_mem == MAP_FAILED)
1793 clib_unix_error ("VCL<%d>: ERROR: mmap(0, %lld == 0x%llx, "
1794 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1796 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1797 ASSERT (vcl_mem != MAP_FAILED);
1800 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1803 clib_warning ("VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
1807 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1810 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
1815 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1818 if (VPPCOM_DEBUG > 0)
1819 clib_warning ("VCL<%d>: allocated VCL heap = %p, size %lld (0x%llx)",
1820 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
1824 vppcom_cfg_read (char *conf_fname)
1826 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1828 unformat_input_t _input, *input = &_input;
1829 unformat_input_t _line_input, *line_input = &_line_input;
1830 u8 vc_cfg_input = 0;
1833 u32 uid, gid, q_len;
1835 fd = open (conf_fname, O_RDONLY);
1838 if (VPPCOM_DEBUG > 0)
1839 clib_warning ("VCL<%d>: using default configuration.",
1840 getpid (), conf_fname);
1844 if (fstat (fd, &s) < 0)
1846 if (VPPCOM_DEBUG > 0)
1847 clib_warning ("VCL<%d>: failed to stat `%s', "
1848 "using default configuration", getpid (), conf_fname);
1852 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1854 if (VPPCOM_DEBUG > 0)
1855 clib_warning ("VCL<%d>: not a regular file `%s', "
1856 "using default configuration", getpid (), conf_fname);
1860 unformat_init_clib_file (input, fd);
1862 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1864 (void) unformat_user (input, unformat_line_input, line_input);
1865 unformat_skip_white_space (line_input);
1867 if (unformat (line_input, "vcl {"))
1875 if (unformat (line_input, "heapsize %s", &chroot_path))
1877 vec_terminate_c_string (chroot_path);
1878 if (VPPCOM_DEBUG > 0)
1879 clib_warning ("VCL<%d>: configured heapsize %s, "
1880 "actual heapsize %lld (0x%llx)",
1881 getpid (), chroot_path, vcl_cfg->heapsize,
1883 vec_free (chroot_path);
1885 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1887 vec_terminate_c_string (chroot_path);
1888 if (vcl_cfg->vpp_api_filename)
1889 vec_free (vcl_cfg->vpp_api_filename);
1890 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
1892 vl_set_memory_root_path ((char *) chroot_path);
1894 if (VPPCOM_DEBUG > 0)
1895 clib_warning ("VCL<%d>: configured api-prefix (%s) and "
1896 "api filename (%s)", getpid (), chroot_path,
1897 vcl_cfg->vpp_api_filename);
1898 chroot_path = 0; /* Don't vec_free() it! */
1900 else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
1902 if (q_len < vcl_cfg->vpp_api_q_length)
1904 clib_warning ("VCL<%d>: ERROR: configured vpp-api-q-length "
1905 "(%u) is too small! Using default: %u ",
1906 getpid (), q_len, vcl_cfg->vpp_api_q_length);
1910 vcl_cfg->vpp_api_q_length = q_len;
1912 if (VPPCOM_DEBUG > 0)
1913 clib_warning ("VCL<%d>: configured vpp-api-q-length %u",
1914 getpid (), vcl_cfg->vpp_api_q_length);
1917 else if (unformat (line_input, "uid %d", &uid))
1919 vl_set_memory_uid (uid);
1920 if (VPPCOM_DEBUG > 0)
1921 clib_warning ("VCL<%d>: configured uid %d", getpid (), uid);
1923 else if (unformat (line_input, "gid %d", &gid))
1925 vl_set_memory_gid (gid);
1926 if (VPPCOM_DEBUG > 0)
1927 clib_warning ("VCL<%d>: configured gid %d", getpid (), gid);
1929 else if (unformat (line_input, "segment-baseva 0x%lx",
1930 &vcl_cfg->segment_baseva))
1932 if (VPPCOM_DEBUG > 0)
1933 clib_warning ("VCL<%d>: configured segment_baseva 0x%lx",
1934 getpid (), vcl_cfg->segment_baseva);
1936 else if (unformat (line_input, "segment-size 0x%lx",
1937 &vcl_cfg->segment_size))
1939 if (VPPCOM_DEBUG > 0)
1940 clib_warning ("VCL<%d>: configured segment_size 0x%lx (%ld)",
1941 getpid (), vcl_cfg->segment_size,
1942 vcl_cfg->segment_size);
1944 else if (unformat (line_input, "segment-size %ld",
1945 &vcl_cfg->segment_size))
1947 if (VPPCOM_DEBUG > 0)
1948 clib_warning ("VCL<%d>: configured segment_size %ld (0x%lx)",
1949 getpid (), vcl_cfg->segment_size,
1950 vcl_cfg->segment_size);
1952 else if (unformat (line_input, "add-segment-size 0x%lx",
1953 &vcl_cfg->add_segment_size))
1955 if (VPPCOM_DEBUG > 0)
1957 ("VCL<%d>: configured add_segment_size 0x%lx (%ld)",
1958 getpid (), vcl_cfg->add_segment_size,
1959 vcl_cfg->add_segment_size);
1961 else if (unformat (line_input, "add-segment-size %ld",
1962 &vcl_cfg->add_segment_size))
1964 if (VPPCOM_DEBUG > 0)
1966 ("VCL<%d>: configured add_segment_size %ld (0x%lx)",
1967 getpid (), vcl_cfg->add_segment_size,
1968 vcl_cfg->add_segment_size);
1970 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1971 &vcl_cfg->preallocated_fifo_pairs))
1973 if (VPPCOM_DEBUG > 0)
1974 clib_warning ("VCL<%d>: configured preallocated_fifo_pairs "
1975 "%d (0x%x)", getpid (),
1976 vcl_cfg->preallocated_fifo_pairs,
1977 vcl_cfg->preallocated_fifo_pairs);
1979 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1980 &vcl_cfg->rx_fifo_size))
1982 if (VPPCOM_DEBUG > 0)
1983 clib_warning ("VCL<%d>: configured rx_fifo_size 0x%lx (%ld)",
1984 getpid (), vcl_cfg->rx_fifo_size,
1985 vcl_cfg->rx_fifo_size);
1987 else if (unformat (line_input, "rx-fifo-size %ld",
1988 &vcl_cfg->rx_fifo_size))
1990 if (VPPCOM_DEBUG > 0)
1991 clib_warning ("VCL<%d>: configured rx_fifo_size %ld (0x%lx)",
1992 getpid (), vcl_cfg->rx_fifo_size,
1993 vcl_cfg->rx_fifo_size);
1995 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1996 &vcl_cfg->tx_fifo_size))
1998 if (VPPCOM_DEBUG > 0)
1999 clib_warning ("VCL<%d>: configured tx_fifo_size 0x%lx (%ld)",
2000 getpid (), vcl_cfg->tx_fifo_size,
2001 vcl_cfg->tx_fifo_size);
2003 else if (unformat (line_input, "tx-fifo-size %ld",
2004 &vcl_cfg->tx_fifo_size))
2006 if (VPPCOM_DEBUG > 0)
2007 clib_warning ("VCL<%d>: configured tx_fifo_size %ld (0x%lx)",
2008 getpid (), vcl_cfg->tx_fifo_size,
2009 vcl_cfg->tx_fifo_size);
2011 else if (unformat (line_input, "event-queue-size 0x%lx",
2012 &vcl_cfg->event_queue_size))
2014 if (VPPCOM_DEBUG > 0)
2015 clib_warning ("VCL<%d>: configured event_queue_size "
2017 getpid (), vcl_cfg->event_queue_size,
2018 vcl_cfg->event_queue_size);
2020 else if (unformat (line_input, "event-queue-size %ld",
2021 &vcl_cfg->event_queue_size))
2023 if (VPPCOM_DEBUG > 0)
2024 clib_warning ("VCL<%d>: configured event_queue_size "
2026 getpid (), vcl_cfg->event_queue_size,
2027 vcl_cfg->event_queue_size);
2029 else if (unformat (line_input, "listen-queue-size 0x%lx",
2030 &vcl_cfg->listen_queue_size))
2032 if (VPPCOM_DEBUG > 0)
2033 clib_warning ("VCL<%d>: configured listen_queue_size "
2035 getpid (), vcl_cfg->listen_queue_size,
2036 vcl_cfg->listen_queue_size);
2038 else if (unformat (line_input, "listen-queue-size %ld",
2039 &vcl_cfg->listen_queue_size))
2041 if (VPPCOM_DEBUG > 0)
2042 clib_warning ("VCL<%d>: configured listen_queue_size "
2044 getpid (), vcl_cfg->listen_queue_size,
2045 vcl_cfg->listen_queue_size);
2047 else if (unformat (line_input, "app-timeout %f",
2048 &vcl_cfg->app_timeout))
2050 if (VPPCOM_DEBUG > 0)
2051 clib_warning ("VCL<%d>: configured app_timeout %f",
2052 getpid (), vcl_cfg->app_timeout);
2054 else if (unformat (line_input, "session-timeout %f",
2055 &vcl_cfg->session_timeout))
2057 if (VPPCOM_DEBUG > 0)
2058 clib_warning ("VCL<%d>: configured session_timeout %f",
2059 getpid (), vcl_cfg->session_timeout);
2061 else if (unformat (line_input, "accept-timeout %f",
2062 &vcl_cfg->accept_timeout))
2064 if (VPPCOM_DEBUG > 0)
2065 clib_warning ("VCL<%d>: configured accept_timeout %f",
2066 getpid (), vcl_cfg->accept_timeout);
2068 else if (unformat (line_input, "app-proxy-transport-tcp"))
2070 vcl_cfg->app_proxy_transport_tcp = 1;
2071 if (VPPCOM_DEBUG > 0)
2072 clib_warning ("VCL<%d>: configured "
2073 "app_proxy_transport_tcp (%d)",
2074 getpid (), vcl_cfg->app_proxy_transport_tcp);
2076 else if (unformat (line_input, "app-proxy-transport-udp"))
2078 vcl_cfg->app_proxy_transport_udp = 1;
2079 if (VPPCOM_DEBUG > 0)
2080 clib_warning ("VCL<%d>: configured "
2081 "app_proxy_transport_udp (%d)",
2082 getpid (), vcl_cfg->app_proxy_transport_udp);
2084 else if (unformat (line_input, "app-scope-local"))
2086 vcl_cfg->app_scope_local = 1;
2087 if (VPPCOM_DEBUG > 0)
2088 clib_warning ("VCL<%d>: configured app_scope_local (%d)",
2089 getpid (), vcl_cfg->app_scope_local);
2091 else if (unformat (line_input, "app-scope-global"))
2093 vcl_cfg->app_scope_global = 1;
2094 if (VPPCOM_DEBUG > 0)
2095 clib_warning ("VCL<%d>: configured app_scope_global (%d)",
2096 getpid (), vcl_cfg->app_scope_global);
2098 else if (unformat (line_input, "namespace-secret %lu",
2099 &vcl_cfg->namespace_secret))
2101 if (VPPCOM_DEBUG > 0)
2103 ("VCL<%d>: configured namespace_secret %lu (0x%lx)",
2104 getpid (), vcl_cfg->namespace_secret,
2105 vcl_cfg->namespace_secret);
2107 else if (unformat (line_input, "namespace-id %v",
2108 &vcl_cfg->namespace_id))
2110 vl_api_application_attach_t *mp;
2111 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
2112 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
2113 if (nsid_vec_len > max_nsid_vec_len)
2115 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
2116 if (VPPCOM_DEBUG > 0)
2117 clib_warning ("VCL<%d>: configured namespace_id is "
2118 "too long, truncated to %d characters!",
2119 getpid (), max_nsid_vec_len);
2122 if (VPPCOM_DEBUG > 0)
2123 clib_warning ("VCL<%d>: configured namespace_id %v",
2124 getpid (), vcl_cfg->namespace_id);
2126 else if (unformat (line_input, "}"))
2129 if (VPPCOM_DEBUG > 0)
2130 clib_warning ("VCL<%d>: completed parsing vppcom config!",
2136 if (line_input->buffer[line_input->index] != '#')
2138 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
2140 &line_input->buffer[line_input->index]);
2147 unformat_free (input);
2155 * VPPCOM Public API functions
2158 vppcom_app_create (char *app_name)
2160 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
2171 vppcom_cfg_init (vcl_cfg);
2172 env_var_str = getenv (VPPCOM_ENV_DEBUG);
2176 if (sscanf (env_var_str, "%u", &tmp) != 1)
2177 clib_warning ("VCL<%d>: WARNING: Invalid debug level specified "
2178 "in the environment variable " VPPCOM_ENV_DEBUG
2179 " (%s)!\n", getpid (), env_var_str);
2183 if (VPPCOM_DEBUG > 0)
2184 clib_warning ("VCL<%d>: configured VCL debug level (%u) from "
2185 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
2188 conf_fname = getenv (VPPCOM_ENV_CONF);
2190 conf_fname = VPPCOM_CONF_DEFAULT;
2191 vppcom_cfg_heapsize (conf_fname);
2192 vcl_cfg = &vcm->cfg;
2193 clib_spinlock_init (&vcm->session_fifo_lockp);
2194 clib_fifo_validate (vcm->client_session_index_fifo,
2195 vcm->cfg.listen_queue_size);
2196 vppcom_cfg_read (conf_fname);
2198 env_var_str = getenv (VPPCOM_ENV_API_PREFIX);
2201 if (vcl_cfg->vpp_api_filename)
2202 vec_free (vcl_cfg->vpp_api_filename);
2203 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
2205 vl_set_memory_root_path ((char *) env_var_str);
2207 if (VPPCOM_DEBUG > 0)
2208 clib_warning ("VCL<%d>: configured api prefix (%s) and "
2209 "filename (%s) from " VPPCOM_ENV_API_PREFIX "!",
2210 getpid (), env_var_str, vcl_cfg->vpp_api_filename);
2212 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
2215 u32 ns_id_vec_len = strlen (env_var_str);
2217 vec_reset_length (vcm->cfg.namespace_id);
2218 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
2219 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
2221 if (VPPCOM_DEBUG > 0)
2222 clib_warning ("VCL<%d>: configured namespace_id (%v) from "
2223 VPPCOM_ENV_APP_NAMESPACE_ID
2224 "!", getpid (), vcm->cfg.namespace_id);
2226 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
2230 if (sscanf (env_var_str, "%lu", &tmp) != 1)
2231 clib_warning ("VCL<%d>: WARNING: Invalid namespace secret "
2232 "specified in the environment variable "
2233 VPPCOM_ENV_APP_NAMESPACE_SECRET
2234 " (%s)!\n", getpid (), env_var_str);
2237 vcm->cfg.namespace_secret = tmp;
2238 if (VPPCOM_DEBUG > 0)
2239 clib_warning ("VCL<%d>: configured namespace secret "
2241 VPPCOM_ENV_APP_NAMESPACE_SECRET
2242 "!", getpid (), vcm->cfg.namespace_secret);
2245 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
2247 vcm->cfg.app_proxy_transport_tcp = 1;
2248 if (VPPCOM_DEBUG > 0)
2249 clib_warning ("VCL<%d>: configured app_proxy_transport_tcp "
2251 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP
2252 "!", getpid (), vcm->cfg.app_proxy_transport_tcp);
2254 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
2256 vcm->cfg.app_proxy_transport_udp = 1;
2257 if (VPPCOM_DEBUG > 0)
2258 clib_warning ("VCL<%d>: configured app_proxy_transport_udp "
2260 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP
2261 "!", getpid (), vcm->cfg.app_proxy_transport_udp);
2263 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
2265 vcm->cfg.app_scope_local = 1;
2266 if (VPPCOM_DEBUG > 0)
2267 clib_warning ("VCL<%d>: configured app_scope_local (%u) from "
2268 VPPCOM_ENV_APP_SCOPE_LOCAL
2269 "!", getpid (), vcm->cfg.app_scope_local);
2271 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
2273 vcm->cfg.app_scope_global = 1;
2274 if (VPPCOM_DEBUG > 0)
2275 clib_warning ("VCL<%d>: configured app_scope_global (%u) from "
2276 VPPCOM_ENV_APP_SCOPE_GLOBAL
2277 "!", getpid (), vcm->cfg.app_scope_global);
2280 vcm->main_cpu = os_get_thread_index ();
2281 heap = clib_mem_get_per_cpu_heap ();
2282 h = mheap_header (heap);
2284 /* make the main heap thread-safe */
2285 h->flags |= MHEAP_FLAG_THREAD_SAFE;
2287 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
2289 clib_time_init (&vcm->clib_time);
2290 vppcom_init_error_string_table ();
2291 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
2292 20 /* timeout in secs */ );
2293 clib_spinlock_init (&vcm->sessions_lockp);
2296 if (vcm->my_client_index == ~0)
2299 /* API hookup and connect to VPP */
2300 vppcom_api_hookup ();
2301 vcm->app_state = STATE_APP_START;
2302 rv = vppcom_connect_to_vpp (app_name);
2305 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
2310 /* State event handling thread */
2312 rv = vce_start_event_thread (&(vcm->event_thread), 20);
2315 if (VPPCOM_DEBUG > 0)
2316 clib_warning ("VCL<%d>: sending session enable", getpid ());
2318 rv = vppcom_app_session_enable ();
2321 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
2322 "failed!", getpid ());
2326 if (VPPCOM_DEBUG > 0)
2327 clib_warning ("VCL<%d>: sending app attach", getpid ());
2329 rv = vppcom_app_attach ();
2332 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
2337 if (VPPCOM_DEBUG > 0)
2338 clib_warning ("VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
2339 getpid (), app_name, vcm->my_client_index,
2340 vcm->my_client_index);
2347 vppcom_app_destroy (void)
2351 if (vcm->my_client_index == ~0)
2354 if (VPPCOM_DEBUG > 0)
2355 clib_warning ("VCL<%d>: detaching from VPP, my_client_index %d (0x%x)",
2356 getpid (), vcm->my_client_index, vcm->my_client_index);
2358 if (VPPCOM_DEBUG > 0)
2361 ELOG_TYPE_DECLARE (e) =
2363 .format = "app_detach:C:%d",
2364 .format_args = "i4",
2371 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
2372 ed->data = vcm->my_client_index;
2376 vppcom_app_detach ();
2377 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
2378 if (PREDICT_FALSE (rv))
2380 if (VPPCOM_DEBUG > 0)
2381 clib_warning ("VCL<%d>: application detach timed out! "
2382 "returning %d (%s)",
2383 getpid (), rv, vppcom_retval_str (rv));
2386 /* Finished with logging before client gets reset to ~0 */
2387 if (VPPCOM_DEBUG > 0)
2390 vl_client_disconnect_from_vlib ();
2391 vcm->my_client_index = ~0;
2392 vcm->app_state = STATE_APP_START;
2396 vppcom_session_create (u8 proto, u8 is_nonblocking)
2400 session_state_t state;
2401 elog_track_t session_elog_track;
2403 clib_spinlock_lock (&vcm->sessions_lockp);
2404 pool_get (vcm->sessions, session);
2405 memset (session, 0, sizeof (*session));
2406 session_index = session - vcm->sessions;
2408 session->proto = proto;
2409 session->state = STATE_START;
2410 state = session->state;
2411 session->vpp_handle = ~0;
2414 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2416 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2418 if (VPPCOM_DEBUG > 0)
2420 session->elog_track.name =
2421 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2423 elog_track_register (&vcm->elog_main, &session->elog_track);
2424 session_elog_track = session->elog_track;
2427 clib_spinlock_unlock (&vcm->sessions_lockp);
2429 if (VPPCOM_DEBUG > 0)
2430 clib_warning ("VCL<%d>: sid %u", getpid (), session_index);
2432 if (VPPCOM_DEBUG > 0)
2435 ELOG_TYPE_DECLARE (e) =
2437 .format = "session_create:proto:%d state:%d is_nonblocking:%d",
2438 .format_args = "i4i4i4",
2446 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
2447 ed->data[0] = proto;
2448 ed->data[1] = state;
2449 ed->data[2] = is_nonblocking;
2453 return (int) session_index;
2457 vppcom_session_close (uint32_t session_index)
2459 session_t *session = 0;
2467 session_state_t state;
2468 elog_track_t session_elog_track;
2470 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2471 is_vep = session->is_vep;
2472 is_vep_session = session->is_vep_session;
2473 next_sid = session->vep.next_sid;
2474 vep_idx = session->vep.vep_idx;
2475 state = session->state;
2476 vpp_handle = session->vpp_handle;
2477 clib_spinlock_unlock (&vcm->sessions_lockp);
2480 * Why two if(VPPCOM_DEBUG) checks?
2482 * Eventually all clib_warnings need their own way of being
2483 * logged and signalled (like severity) where event logging
2484 * is a separate debugging tool. It will make the separation
2485 * easier. ... parting is such sweet sorrow ...
2487 if (VPPCOM_DEBUG > 0)
2489 session_elog_track = session->elog_track;
2492 if (VPPCOM_DEBUG > 0)
2495 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
2496 "closing epoll session...",
2497 getpid (), session_index, session_index);
2499 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
2500 "closing session...",
2501 getpid (), vpp_handle, session_index);
2506 while (next_sid != ~0)
2508 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
2509 if ((VPPCOM_DEBUG > 0) && PREDICT_FALSE (rv < 0))
2510 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2511 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2512 getpid (), vpp_handle, next_sid, vep_idx,
2513 rv, vppcom_retval_str (rv));
2515 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2516 next_sid = session->vep.next_sid;
2517 clib_spinlock_unlock (&vcm->sessions_lockp);
2524 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2525 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2526 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2527 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2528 getpid (), vpp_handle, session_index,
2529 vep_idx, rv, vppcom_retval_str (rv));
2532 if (state & STATE_LISTEN)
2534 rv = vppcom_session_unbind (session_index);
2535 if (PREDICT_FALSE (rv < 0))
2537 if (VPPCOM_DEBUG > 0)
2538 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2539 "listener unbind failed! rv %d (%s)",
2540 getpid (), vpp_handle, session_index,
2541 rv, vppcom_retval_str (rv));
2545 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
2547 rv = vppcom_session_disconnect (session_index);
2548 if (PREDICT_FALSE (rv < 0))
2549 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2550 "session disconnect failed! rv %d (%s)",
2551 getpid (), vpp_handle, session_index,
2552 rv, vppcom_retval_str (rv));
2556 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2557 vpp_handle = session->vpp_handle;
2558 if (vpp_handle != ~0)
2560 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
2562 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
2564 pool_put_index (vcm->sessions, session_index);
2566 clib_spinlock_unlock (&vcm->sessions_lockp);
2568 if (VPPCOM_DEBUG > 0)
2571 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
2572 getpid (), session_index, session_index);
2574 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
2575 getpid (), vpp_handle, session_index);
2579 if (VPPCOM_DEBUG > 0)
2582 ELOG_TYPE_DECLARE (e) =
2584 .format = "session_close:rv:%d",
2585 .format_args = "i4",
2593 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
2602 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2604 session_t *session = 0;
2608 return VPPCOM_EINVAL;
2610 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2612 if (session->is_vep)
2614 clib_spinlock_unlock (&vcm->sessions_lockp);
2615 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2616 "bind to an epoll session!", getpid (), session_index);
2621 session->lcl_addr.is_ip4 = ep->is_ip4;
2622 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
2623 session->lcl_port = ep->port;
2625 if (VPPCOM_DEBUG > 0)
2626 clib_warning ("VCL<%d>: sid %u: binding to local %s address %U "
2627 "port %u, proto %s", getpid (), session_index,
2628 session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2629 format_ip46_address, &session->lcl_addr.ip46,
2630 session->lcl_addr.is_ip4,
2631 clib_net_to_host_u16 (session->lcl_port),
2632 session->proto ? "UDP" : "TCP");
2634 if (VPPCOM_DEBUG > 0)
2636 if (session->lcl_addr.is_ip4)
2639 ELOG_TYPE_DECLARE (e) =
2641 .format = "bind local:%s:%d.%d.%d.%d:%d ",
2642 .format_args = "t1i1i1i1i1i2",
2643 .n_enum_strings = 2,
2644 .enum_strings = {"TCP", "UDP",},
2647 CLIB_PACKED (struct {
2653 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2654 ed->proto = session->proto;
2655 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
2656 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
2657 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
2658 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
2659 ed->port = clib_net_to_host_u16 (session->lcl_port);
2664 clib_spinlock_unlock (&vcm->sessions_lockp);
2670 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2672 session_t *listen_session = 0;
2673 u64 listen_vpp_handle;
2676 if (q_len == 0 || q_len == ~0)
2677 q_len = vcm->cfg.listen_queue_size;
2679 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2681 if (listen_session->is_vep)
2683 clib_spinlock_unlock (&vcm->sessions_lockp);
2684 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
2685 "epoll session!", getpid (), listen_session_index);
2690 listen_vpp_handle = listen_session->vpp_handle;
2691 if (listen_session->state & STATE_LISTEN)
2693 clib_spinlock_unlock (&vcm->sessions_lockp);
2694 if (VPPCOM_DEBUG > 0)
2695 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2696 "already in listen state!",
2697 getpid (), listen_vpp_handle, listen_session_index);
2702 if (VPPCOM_DEBUG > 0)
2703 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
2704 "sid %u: sending bind request...",
2705 getpid (), listen_vpp_handle, listen_session_index);
2707 vppcom_send_bind_sock (listen_session, listen_session_index);
2708 clib_spinlock_unlock (&vcm->sessions_lockp);
2710 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2711 vcm->cfg.session_timeout);
2713 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2714 if (PREDICT_FALSE (retval))
2716 if (VPPCOM_DEBUG > 0)
2717 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind failed! "
2718 "returning %d (%s)", getpid (),
2719 listen_session->vpp_handle, listen_session_index,
2720 retval, vppcom_retval_str (retval));
2721 clib_spinlock_unlock (&vcm->sessions_lockp);
2726 clib_spinlock_lock (&vcm->session_fifo_lockp);
2727 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2728 clib_spinlock_unlock (&vcm->session_fifo_lockp);
2730 clib_spinlock_unlock (&vcm->sessions_lockp);
2737 vppcom_session_register_listener (uint32_t session_index,
2738 vppcom_session_listener_cb cb,
2739 vppcom_session_listener_errcb
2740 errcb, uint8_t flags, int q_len, void *ptr)
2743 vce_event_key_t evk;
2744 vppcom_session_listener_t *listener_args;
2746 rv = vppcom_session_listen (session_index, q_len);
2753 /* Register handler for connect_request event on listen_session_index */
2754 listener_args = clib_mem_alloc (sizeof (vppcom_session_listener_t));
2755 listener_args->user_cb = cb;
2756 listener_args->user_cb_data = ptr;
2757 listener_args->user_errcb = errcb;
2759 evk.session_index = session_index;
2760 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
2761 (void) vce_register_handler (&vcm->event_thread, &evk,
2762 vce_registered_listener_connect_handler_fn,
2770 validate_args_session_accept_ (session_t * listen_session)
2772 u32 listen_session_index = listen_session - vcm->sessions;
2774 /* Input validation - expects spinlock on sessions_lockp */
2775 if (listen_session->is_vep)
2777 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
2778 "epoll session!", getpid (), listen_session_index);
2779 return VPPCOM_EBADFD;
2782 if (listen_session->state != STATE_LISTEN)
2784 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2785 "not in listen state! state 0x%x (%s)", getpid (),
2786 listen_session->vpp_handle, listen_session_index,
2787 listen_session->state,
2788 vppcom_session_state_str (listen_session->state));
2789 return VPPCOM_EBADFD;
2795 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2798 session_t *listen_session = 0;
2799 session_t *client_session = 0;
2800 u32 client_session_index = ~0;
2802 u64 listen_vpp_handle;
2803 vce_event_handler_reg_t *reg;
2805 vce_event_connect_request_t *result;
2808 int millisecond_timeout = 1;
2809 int hours_timeout = 20 * 60 * 60;
2811 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2812 listen_vpp_handle = listen_session->vpp_handle; // For debugging
2814 rv = validate_args_session_accept_ (listen_session);
2817 clib_spinlock_unlock (&vcm->sessions_lockp);
2821 /* Using an aggressive timer of 1ms and a generous timer of
2822 * 20 hours, we can implement a blocking and non-blocking listener
2823 * as both event and time driven */
2824 gettimeofday (&tv, NULL);
2825 ts.tv_nsec = (tv.tv_usec * 1000) + (1000 * millisecond_timeout);
2826 ts.tv_sec = tv.tv_sec;
2828 /* Predict that the Listener is blocking more often than not */
2829 if (PREDICT_TRUE (!VCL_SESS_ATTR_TEST (listen_session->attr,
2830 VCL_SESS_ATTR_NONBLOCK)))
2831 ts.tv_sec += hours_timeout;
2833 clib_spinlock_unlock (&vcm->sessions_lockp);
2835 /* Register handler for connect_request event on listen_session_index */
2836 vce_event_key_t evk;
2837 evk.session_index = listen_session_index;
2838 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
2839 reg = vce_register_handler (&vcm->event_thread, &evk,
2840 vce_connect_request_handler_fn, 0);
2841 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
2842 pthread_mutex_lock (®->handler_lock);
2846 pthread_cond_timedwait (®->handler_cond, ®->handler_lock, &ts);
2847 if (rv == ETIMEDOUT)
2852 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
2854 result = (vce_event_connect_request_t *) ev->data;
2855 client_session_index = result->accepted_session_index;
2859 /* Remove from the FIFO used to service epoll */
2860 clib_spinlock_lock (&vcm->session_fifo_lockp);
2861 if (clib_fifo_elts (vcm->client_session_index_fifo))
2863 u32 tmp_client_session_index;
2864 clib_fifo_sub1 (vcm->client_session_index_fifo,
2865 tmp_client_session_index);
2866 /* It wasn't ours... put it back ... */
2867 if (tmp_client_session_index != client_session_index)
2868 clib_fifo_add1 (vcm->client_session_index_fifo,
2869 tmp_client_session_index);
2871 clib_spinlock_unlock (&vcm->session_fifo_lockp);
2873 clib_spinlock_lock (&vcm->sessions_lockp);
2875 rv = vppcom_session_at_index (client_session_index, &client_session);
2876 if (PREDICT_FALSE (rv))
2878 rv = VPPCOM_ECONNABORTED;
2879 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
2880 "lookup failed! returning %d (%s)", getpid (),
2881 listen_vpp_handle, listen_session_index,
2882 client_session_index, rv, vppcom_retval_str (rv));
2886 if (flags & O_NONBLOCK)
2887 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2889 VCL_SESS_ATTR_CLR (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2891 if (VPPCOM_DEBUG > 0)
2892 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
2893 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
2894 getpid (), listen_vpp_handle, listen_session_index,
2895 client_session->vpp_handle, client_session_index,
2896 flags, VCL_SESS_ATTR_TEST (client_session->attr,
2897 VCL_SESS_ATTR_NONBLOCK));
2901 ep->is_ip4 = client_session->peer_addr.is_ip4;
2902 ep->port = client_session->peer_port;
2903 if (client_session->peer_addr.is_ip4)
2904 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2905 sizeof (ip4_address_t));
2907 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2908 sizeof (ip6_address_t));
2911 vppcom_send_accept_session_reply (client_session->vpp_handle,
2912 client_session->client_context,
2913 0 /* retval OK */ );
2915 if (VPPCOM_DEBUG > 0)
2916 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle "
2917 "0x%llx, sid %u connection to local %s address "
2918 "%U port %u", getpid (), listen_vpp_handle,
2919 listen_session_index, client_session->vpp_handle,
2920 client_session_index,
2921 client_session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2922 format_ip46_address, &client_session->lcl_addr.ip46,
2923 client_session->lcl_addr.is_ip4,
2924 clib_net_to_host_u16 (client_session->lcl_port));
2926 if (VPPCOM_DEBUG > 0)
2928 client_session->elog_track.name =
2929 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2930 client_session_index, 0);
2931 elog_track_register (&vcm->elog_main, &client_session->elog_track);
2933 // Two elog entries due to 20-byte per entry constraint.
2935 ELOG_TYPE_DECLARE (e) =
2937 .format = "accept: listen_handle:%x from_handle:%x",
2938 .format_args = "i8i8",
2946 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, client_session->elog_track);
2947 ed->handle[0] = listen_vpp_handle;
2948 ed->handle[1] = client_session->vpp_handle;
2951 if (client_session->lcl_addr.is_ip4)
2954 ELOG_TYPE_DECLARE (e2) =
2956 .format = "accept: S:%d %d.%d.%d.%d:%d ",
2957 .format_args = "i4i1i1i1i1i2",
2960 CLIB_PACKED (struct {
2967 ELOG_TRACK_DATA (&vcm->elog_main, e2, client_session->elog_track);
2968 ed2->session = client_session_index;
2969 ed2->addr[0] = client_session->lcl_addr.ip46.ip4.as_u8[0];
2970 ed2->addr[1] = client_session->lcl_addr.ip46.ip4.as_u8[1];
2971 ed2->addr[2] = client_session->lcl_addr.ip46.ip4.as_u8[2];
2972 ed2->addr[3] = client_session->lcl_addr.ip46.ip4.as_u8[3];
2973 ed2->port = clib_net_to_host_u16 (client_session->lcl_port);
2978 clib_spinlock_unlock (&vcm->sessions_lockp);
2980 rv = (int) client_session_index;
2981 vce_clear_event (&vcm->event_thread, ev);
2984 vce_unregister_handler (&vcm->event_thread, reg);
2985 pthread_mutex_unlock (®->handler_lock);
2992 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2994 session_t *session = 0;
2996 int rv, retval = VPPCOM_OK;
2998 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3000 if (PREDICT_FALSE (session->is_vep))
3002 clib_spinlock_unlock (&vcm->sessions_lockp);
3003 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
3004 "connect on an epoll session!", getpid (), session_index);
3009 if (PREDICT_FALSE (session->state & CLIENT_STATE_OPEN))
3011 if (VPPCOM_DEBUG > 0)
3012 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session already "
3013 "connected to %s %U port %d proto %s, state 0x%x (%s)",
3014 getpid (), session->vpp_handle, session_index,
3015 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
3016 format_ip46_address,
3017 &session->peer_addr.ip46, session->peer_addr.is_ip4,
3018 clib_net_to_host_u16 (session->peer_port),
3019 session->proto ? "UDP" : "TCP", session->state,
3020 vppcom_session_state_str (session->state));
3022 clib_spinlock_unlock (&vcm->sessions_lockp);
3026 session->peer_addr.is_ip4 = server_ep->is_ip4;
3027 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
3028 session->peer_port = server_ep->port;
3030 if (VPPCOM_DEBUG > 0)
3031 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server "
3032 "%s %U port %d proto %s",
3033 getpid (), session->vpp_handle, session_index,
3034 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
3035 format_ip46_address,
3036 &session->peer_addr.ip46, session->peer_addr.is_ip4,
3037 clib_net_to_host_u16 (session->peer_port),
3038 session->proto ? "UDP" : "TCP");
3040 vppcom_send_connect_sock (session, session_index);
3041 clib_spinlock_unlock (&vcm->sessions_lockp);
3044 vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
3045 vcm->cfg.session_timeout);
3047 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3048 vpp_handle = session->vpp_handle;
3049 clib_spinlock_unlock (&vcm->sessions_lockp);
3052 if (PREDICT_FALSE (retval))
3055 if (VPPCOM_DEBUG > 0)
3059 ("VCL<%d>: vpp handle 0x%llx, sid %u: connect failed! "
3060 "returning %d (%s)", getpid (), vpp_handle,
3061 session_index, rv, vppcom_retval_str (rv));
3063 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
3064 "returning %d (%s)", getpid (),
3065 session_index, rv, vppcom_retval_str (rv));
3068 else if (VPPCOM_DEBUG > 0)
3069 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
3070 getpid (), vpp_handle, session_index);
3076 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
3079 session_t *session = 0;
3080 svm_fifo_t *rx_fifo;
3087 session_state_t state;
3091 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3093 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
3094 rx_fifo = session->rx_fifo;
3095 state = session->state;
3096 vpp_handle = session->vpp_handle;
3098 if (PREDICT_FALSE (session->is_vep))
3100 clib_spinlock_unlock (&vcm->sessions_lockp);
3101 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
3102 "read from an epoll session!", getpid (), session_index);
3107 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
3109 clib_spinlock_unlock (&vcm->sessions_lockp);
3110 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3112 if (VPPCOM_DEBUG > 0)
3113 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s session is "
3114 "not open! state 0x%x (%s), returning %d (%s)",
3115 getpid (), vpp_handle, session_index, state,
3116 vppcom_session_state_str (state),
3117 rv, vppcom_retval_str (rv));
3121 clib_spinlock_unlock (&vcm->sessions_lockp);
3126 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
3128 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
3130 while (!is_nonblocking && (n_read <= 0));
3134 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3136 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
3137 (EPOLLET | EPOLLIN));
3139 session->vep.et_mask |= EPOLLIN;
3141 if (state & STATE_CLOSE_ON_EMPTY)
3143 rv = VPPCOM_ECONNRESET;
3145 if (VPPCOM_DEBUG > 1)
3147 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo "
3148 "with session state 0x%x (%s)!"
3149 " Setting state to 0x%x (%s), returning %d (%s)",
3150 getpid (), session->vpp_handle, session_index,
3151 state, vppcom_session_state_str (state),
3153 vppcom_session_state_str (STATE_DISCONNECT), rv,
3154 vppcom_retval_str (rv));
3157 session->state = STATE_DISCONNECT;
3162 clib_spinlock_unlock (&vcm->sessions_lockp);
3167 if (VPPCOM_DEBUG > 2)
3170 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
3171 "from (%p)", getpid (), vpp_handle,
3172 session_index, n_read, rx_fifo);
3174 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
3175 "returning %d (%s)", getpid (), vpp_handle,
3176 session_index, rv, vppcom_retval_str (rv));
3183 vppcom_session_read (uint32_t session_index, void *buf, size_t n)
3185 return (vppcom_session_read_internal (session_index, buf, n, 0));
3189 vppcom_session_peek (uint32_t session_index, void *buf, int n)
3191 return (vppcom_session_read_internal (session_index, buf, n, 1));
3195 vppcom_session_read_ready (session_t * session, u32 session_index)
3200 session_state_t state = session->state;
3201 u64 vpp_handle = session->vpp_handle;
3203 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3204 if (PREDICT_FALSE (session->is_vep))
3206 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
3207 "epoll session!", getpid (), session_index);
3212 if (session->state & STATE_LISTEN)
3214 clib_spinlock_lock (&vcm->session_fifo_lockp);
3215 ready = clib_fifo_elts (vcm->client_session_index_fifo);
3216 clib_spinlock_unlock (&vcm->session_fifo_lockp);
3220 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
3222 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3225 if (VPPCOM_DEBUG > 1)
3226 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session is "
3227 "not open! state 0x%x (%s), returning %d (%s)",
3228 getpid (), vpp_handle, session_index,
3229 state, vppcom_session_state_str (state),
3230 rv, vppcom_retval_str (rv));
3234 ready = svm_fifo_max_dequeue (session->rx_fifo);
3240 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
3242 session->vep.et_mask |= EPOLLIN;
3244 if (state & STATE_CLOSE_ON_EMPTY)
3246 rv = VPPCOM_ECONNRESET;
3248 if (VPPCOM_DEBUG > 1)
3250 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
3251 "sid %u: Empty fifo with"
3252 " session state 0x%x (%s)! Setting state to "
3253 "0x%x (%s), returning %d (%s)",
3254 getpid (), session_index, vpp_handle,
3255 state, vppcom_session_state_str (state),
3257 vppcom_session_state_str (STATE_DISCONNECT), rv,
3258 vppcom_retval_str (rv));
3260 session->state = STATE_DISCONNECT;
3266 if (vcm->app_event_queue->cursize &&
3267 !pthread_mutex_trylock (&vcm->app_event_queue->mutex))
3269 u32 i, n_to_dequeue = vcm->app_event_queue->cursize;
3270 session_fifo_event_t e;
3272 for (i = 0; i < n_to_dequeue; i++)
3273 svm_queue_sub_raw (vcm->app_event_queue, (u8 *) & e);
3275 pthread_mutex_unlock (&vcm->app_event_queue->mutex);
3282 vppcom_session_write (uint32_t session_index, void *buf, size_t n)
3284 session_t *session = 0;
3285 svm_fifo_t *tx_fifo = 0;
3287 session_fifo_event_t evt;
3288 session_state_t state;
3289 int rv, n_write, is_nonblocking;
3295 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3297 tx_fifo = session->tx_fifo;
3298 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
3299 vpp_handle = session->vpp_handle;
3300 state = session->state;
3302 if (PREDICT_FALSE (session->is_vep))
3304 clib_spinlock_unlock (&vcm->sessions_lockp);
3305 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3306 "cannot write to an epoll session!",
3307 getpid (), vpp_handle, session_index);
3313 if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3316 ((session->state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3319 clib_spinlock_unlock (&vcm->sessions_lockp);
3320 if (VPPCOM_DEBUG > 1)
3321 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3322 "session is not open! state 0x%x (%s)",
3323 getpid (), vpp_handle, session_index,
3324 state, vppcom_session_state_str (state));
3328 clib_spinlock_unlock (&vcm->sessions_lockp);
3332 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, (void *) buf);
3334 while (!is_nonblocking && (n_write <= 0));
3336 /* If event wasn't set, add one */
3337 if ((n_write > 0) && svm_fifo_set_event (tx_fifo))
3339 /* Fabricate TX event, send to vpp */
3341 evt.event_type = FIFO_EVENT_APP_TX;
3343 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3344 q = session->vpp_event_queue;
3346 svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
3347 clib_spinlock_unlock (&vcm->sessions_lockp);
3348 if (VPPCOM_DEBUG > 1)
3349 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3350 "added FIFO_EVENT_APP_TX to "
3351 "vpp_event_q %p, n_write %d", getpid (),
3352 vpp_handle, session_index, q, n_write);
3357 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3359 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3360 (EPOLLET | EPOLLOUT));
3362 session->vep.et_mask |= EPOLLOUT;
3364 if (session->state & STATE_CLOSE_ON_EMPTY)
3366 rv = VPPCOM_ECONNRESET;
3368 if (VPPCOM_DEBUG > 1)
3370 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3371 "Empty fifo with session state 0x%x (%s)!"
3372 " Setting state to 0x%x (%s), returning %d (%s)",
3373 getpid (), session->vpp_handle, session_index,
3375 vppcom_session_state_str (session->state),
3377 vppcom_session_state_str (STATE_DISCONNECT), rv,
3378 vppcom_retval_str (rv));
3381 session->state = STATE_DISCONNECT;
3386 clib_spinlock_unlock (&vcm->sessions_lockp);
3391 if (VPPCOM_DEBUG > 2)
3394 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3395 "FIFO-FULL (%p)", getpid (), vpp_handle,
3396 session_index, tx_fifo);
3398 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3399 "wrote %d bytes tx-fifo: (%p)", getpid (),
3400 vpp_handle, session_index, n_write, tx_fifo);
3407 vppcom_session_write_ready (session_t * session, u32 session_index)
3415 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3416 if (PREDICT_FALSE (session->is_vep))
3418 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3419 "cannot write to an epoll session!",
3420 getpid (), session->vpp_handle, session_index);
3425 if (PREDICT_FALSE (session->state & STATE_LISTEN))
3427 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3428 "cannot write to a listen session!",
3429 getpid (), session->vpp_handle, session_index);
3434 if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3436 session_state_t state = session->state;
3438 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3440 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3441 "session is not open! state 0x%x (%s), "
3442 "returning %d (%s)", getpid (), session->vpp_handle,
3444 state, vppcom_session_state_str (state),
3445 rv, vppcom_retval_str (rv));
3449 ready = svm_fifo_max_enqueue (session->tx_fifo);
3451 if (VPPCOM_DEBUG > 3)
3452 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3453 "peek %s (%p), ready = %d", getpid (),
3454 session->vpp_handle, session_index,
3455 session->tx_fifo, ready);
3459 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3460 (EPOLLET | EPOLLOUT));
3462 session->vep.et_mask |= EPOLLOUT;
3464 if (session->state & STATE_CLOSE_ON_EMPTY)
3466 rv = VPPCOM_ECONNRESET;
3468 if (VPPCOM_DEBUG > 1)
3470 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3471 "Empty fifo with session "
3472 "state 0x%x (%s)! Setting state to 0x%x (%s), "
3473 "returning %d (%s)", getpid (),
3474 session->vpp_handle, session_index,
3476 vppcom_session_state_str (session->state),
3478 vppcom_session_state_str (STATE_DISCONNECT), rv,
3479 vppcom_retval_str (rv));
3481 session->state = STATE_DISCONNECT;
3491 vppcom_select (unsigned long n_bits, unsigned long *read_map,
3492 unsigned long *write_map, unsigned long *except_map,
3493 double time_to_wait)
3496 session_t *session = 0;
3497 int rv, bits_set = 0;
3498 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
3499 u32 minbits = clib_max (n_bits, BITS (uword));
3501 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
3503 if (n_bits && read_map)
3505 clib_bitmap_validate (vcm->rd_bitmap, minbits);
3506 clib_memcpy (vcm->rd_bitmap, read_map,
3507 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3508 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3510 if (n_bits && write_map)
3512 clib_bitmap_validate (vcm->wr_bitmap, minbits);
3513 clib_memcpy (vcm->wr_bitmap, write_map,
3514 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3515 memset (write_map, 0,
3516 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3518 if (n_bits && except_map)
3520 clib_bitmap_validate (vcm->ex_bitmap, minbits);
3521 clib_memcpy (vcm->ex_bitmap, except_map,
3522 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3523 memset (except_map, 0,
3524 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3534 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
3536 clib_spinlock_lock (&vcm->sessions_lockp);
3537 rv = vppcom_session_at_index (session_index, &session);
3540 clib_spinlock_unlock (&vcm->sessions_lockp);
3541 if (VPPCOM_DEBUG > 1)
3542 clib_warning ("VCL<%d>: session %d specified in "
3543 "read_map is closed.", getpid (),
3545 bits_set = VPPCOM_EBADFD;
3548 if (session->state & STATE_LISTEN)
3550 vce_event_handler_reg_t *reg = 0;
3551 vce_event_key_t evk;
3553 /* Check if handler already registered for this
3555 * If not, register handler for connect_request event
3556 * on listen_session_index
3558 evk.session_index = session_index;
3559 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
3560 reg = vce_get_event_handler (&vcm->event_thread, &evk);
3562 reg = vce_register_handler (&vcm->event_thread, &evk,
3563 vce_poll_wait_connect_request_handler_fn,
3564 0 /* No callback args */);
3565 rv = vppcom_session_read_ready (session, session_index);
3568 vce_unregister_handler (&vcm->event_thread, reg);
3572 rv = vppcom_session_read_ready (session, session_index);
3573 clib_spinlock_unlock (&vcm->sessions_lockp);
3574 if (except_map && vcm->ex_bitmap &&
3575 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3578 clib_bitmap_set_no_check (except_map, session_index, 1);
3583 clib_bitmap_set_no_check (read_map, session_index, 1);
3591 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3593 clib_spinlock_lock (&vcm->sessions_lockp);
3594 rv = vppcom_session_at_index (session_index, &session);
3597 clib_spinlock_unlock (&vcm->sessions_lockp);
3598 if (VPPCOM_DEBUG > 0)
3599 clib_warning ("VCL<%d>: session %d specified in "
3600 "write_map is closed.", getpid (),
3602 bits_set = VPPCOM_EBADFD;
3606 rv = vppcom_session_write_ready (session, session_index);
3607 clib_spinlock_unlock (&vcm->sessions_lockp);
3608 if (write_map && (rv > 0))
3610 clib_bitmap_set_no_check (write_map, session_index, 1);
3618 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3620 clib_spinlock_lock (&vcm->sessions_lockp);
3621 rv = vppcom_session_at_index (session_index, &session);
3624 clib_spinlock_unlock (&vcm->sessions_lockp);
3625 if (VPPCOM_DEBUG > 1)
3626 clib_warning ("VCL<%d>: session %d specified in "
3627 "except_map is closed.", getpid (),
3629 bits_set = VPPCOM_EBADFD;
3633 rv = vppcom_session_read_ready (session, session_index);
3634 clib_spinlock_unlock (&vcm->sessions_lockp);
3637 clib_bitmap_set_no_check (except_map, session_index, 1);
3645 while ((time_to_wait == -1) || (clib_time_now (&vcm->clib_time) < timeout));
3652 vep_verify_epoll_chain (u32 vep_idx)
3655 vppcom_epoll_t *vep;
3659 if (VPPCOM_DEBUG <= 1)
3662 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3663 rv = vppcom_session_at_index (vep_idx, &session);
3664 if (PREDICT_FALSE (rv))
3666 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
3667 getpid (), vep_idx);
3670 if (PREDICT_FALSE (!session->is_vep))
3672 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3673 getpid (), vep_idx);
3676 vep = &session->vep;
3677 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
3680 " is_vep_session = %u\n"
3681 " next_sid = 0x%x (%u)\n"
3682 " wait_cont_idx = 0x%x (%u)\n"
3683 "}\n", getpid (), vep_idx,
3684 session->is_vep, session->is_vep_session,
3685 vep->next_sid, vep->next_sid,
3686 session->wait_cont_idx, session->wait_cont_idx);
3688 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
3690 rv = vppcom_session_at_index (sid, &session);
3691 if (PREDICT_FALSE (rv))
3693 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
3696 if (PREDICT_FALSE (session->is_vep))
3697 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
3698 getpid (), vep_idx);
3699 else if (PREDICT_FALSE (!session->is_vep_session))
3701 clib_warning ("VCL<%d>: ERROR: session (%u) "
3702 "is not a vep session!", getpid (), sid);
3705 vep = &session->vep;
3706 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
3707 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
3708 "vep_idx (%u)!", getpid (),
3709 sid, session->vep.vep_idx, vep_idx);
3710 if (session->is_vep_session)
3712 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3714 " next_sid = 0x%x (%u)\n"
3715 " prev_sid = 0x%x (%u)\n"
3716 " vep_idx = 0x%x (%u)\n"
3717 " ev.events = 0x%x\n"
3718 " ev.data.u64 = 0x%llx\n"
3722 vep->next_sid, vep->next_sid,
3723 vep->prev_sid, vep->prev_sid,
3724 vep->vep_idx, vep->vep_idx,
3725 vep->ev.events, vep->ev.data.u64, vep->et_mask);
3730 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
3731 getpid (), vep_idx);
3735 vppcom_epoll_create (void)
3737 session_t *vep_session;
3739 elog_track_t vep_elog_track;
3741 clib_spinlock_lock (&vcm->sessions_lockp);
3742 pool_get (vcm->sessions, vep_session);
3743 memset (vep_session, 0, sizeof (*vep_session));
3744 vep_idx = vep_session - vcm->sessions;
3746 vep_session->is_vep = 1;
3747 vep_session->vep.vep_idx = ~0;
3748 vep_session->vep.next_sid = ~0;
3749 vep_session->vep.prev_sid = ~0;
3750 vep_session->wait_cont_idx = ~0;
3751 vep_session->vpp_handle = ~0;
3752 vep_session->poll_reg = 0;
3754 if (VPPCOM_DEBUG > 0)
3756 vep_session->elog_track.name =
3757 (char *) format (0, "C:%d:VEP:%d%c", vcm->my_client_index,
3759 elog_track_register (&vcm->elog_main, &vep_session->elog_track);
3760 vep_elog_track = vep_session->elog_track;
3763 clib_spinlock_unlock (&vcm->sessions_lockp);
3765 if (VPPCOM_DEBUG > 0)
3766 clib_warning ("VCL<%d>: Created vep_idx %u / sid %u!",
3767 getpid (), vep_idx, vep_idx);
3769 if (VPPCOM_DEBUG > 0)
3773 ELOG_TYPE_DECLARE (e) =
3775 .format = "created epoll session:%d",
3776 .format_args = "i4",
3784 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_elog_track);
3793 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3794 struct epoll_event *event)
3796 session_t *vep_session;
3800 if (vep_idx == session_index)
3802 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
3803 getpid (), vep_idx);
3804 return VPPCOM_EINVAL;
3807 clib_spinlock_lock (&vcm->sessions_lockp);
3808 rv = vppcom_session_at_index (vep_idx, &vep_session);
3809 if (PREDICT_FALSE (rv))
3811 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
3814 if (PREDICT_FALSE (!vep_session->is_vep))
3816 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3817 getpid (), vep_idx);
3822 ASSERT (vep_session->vep.vep_idx == ~0);
3823 ASSERT (vep_session->vep.prev_sid == ~0);
3825 rv = vppcom_session_at_index (session_index, &session);
3826 if (PREDICT_FALSE (rv))
3828 if (VPPCOM_DEBUG > 0)
3829 clib_warning ("VCL<%d>: ERROR: Invalid session_index (%u)!",
3830 getpid (), session_index);
3833 if (PREDICT_FALSE (session->is_vep))
3835 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3843 if (PREDICT_FALSE (!event))
3845 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
3846 "epoll_event structure!", getpid ());
3850 if (vep_session->vep.next_sid != ~0)
3852 session_t *next_session;
3853 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3855 if (PREDICT_FALSE (rv))
3857 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
3858 "vep.next_sid (%u) on vep_idx (%u)!",
3859 getpid (), vep_session->vep.next_sid, vep_idx);
3862 ASSERT (next_session->vep.prev_sid == vep_idx);
3863 next_session->vep.prev_sid = session_index;
3865 session->vep.next_sid = vep_session->vep.next_sid;
3866 session->vep.prev_sid = vep_idx;
3867 session->vep.vep_idx = vep_idx;
3868 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3869 session->vep.ev = *event;
3870 session->is_vep = 0;
3871 session->is_vep_session = 1;
3872 vep_session->vep.next_sid = session_index;
3874 /* VCL Event Register handler */
3875 if (session->state & STATE_LISTEN)
3877 /* Register handler for connect_request event on listen_session_index */
3878 vce_event_key_t evk;
3879 evk.session_index = session_index;
3880 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
3881 vep_session->poll_reg =
3882 vce_register_handler (&vcm->event_thread, &evk,
3883 vce_poll_wait_connect_request_handler_fn,
3884 0 /* No callback args */ );
3886 if (VPPCOM_DEBUG > 1)
3887 clib_warning ("VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
3888 "sid %u, events 0x%x, data 0x%llx!",
3889 getpid (), vep_idx, session_index,
3890 event->events, event->data.u64);
3891 if (VPPCOM_DEBUG > 0)
3894 ELOG_TYPE_DECLARE (e) =
3896 .format = "epoll_ctladd: events:%x data:%x",
3897 .format_args = "i4i4i8",
3905 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3907 ed->events = event->events;
3908 ed->event_data = event->data.u64;
3914 if (PREDICT_FALSE (!event))
3916 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
3917 "epoll_event structure!", getpid ());
3921 else if (PREDICT_FALSE (!session->is_vep_session))
3923 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3924 "not a vep session!", getpid (), session_index);
3928 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3930 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3931 "vep_idx (%u) != vep_idx (%u)!",
3932 getpid (), session_index,
3933 session->vep.vep_idx, vep_idx);
3937 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3938 session->vep.ev = *event;
3939 if (VPPCOM_DEBUG > 1)
3941 ("VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3942 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
3947 if (PREDICT_FALSE (!session->is_vep_session))
3949 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3950 "not a vep session!", getpid (), session_index);
3954 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3956 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3957 "vep_idx (%u) != vep_idx (%u)!",
3958 getpid (), session_index,
3959 session->vep.vep_idx, vep_idx);
3964 /* VCL Event Un-register handler */
3965 if ((session->state & STATE_LISTEN) && vep_session->poll_reg)
3967 (void) vce_unregister_handler (&vcm->event_thread,
3968 vep_session->poll_reg);
3971 vep_session->wait_cont_idx =
3972 (vep_session->wait_cont_idx == session_index) ?
3973 session->vep.next_sid : vep_session->wait_cont_idx;
3975 if (session->vep.prev_sid == vep_idx)
3976 vep_session->vep.next_sid = session->vep.next_sid;
3979 session_t *prev_session;
3980 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3981 if (PREDICT_FALSE (rv))
3983 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
3984 "vep.prev_sid (%u) on sid (%u)!",
3985 getpid (), session->vep.prev_sid, session_index);
3988 ASSERT (prev_session->vep.next_sid == session_index);
3989 prev_session->vep.next_sid = session->vep.next_sid;
3991 if (session->vep.next_sid != ~0)
3993 session_t *next_session;
3994 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3995 if (PREDICT_FALSE (rv))
3997 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
3998 "vep.next_sid (%u) on sid (%u)!",
3999 getpid (), session->vep.next_sid, session_index);
4002 ASSERT (next_session->vep.prev_sid == session_index);
4003 next_session->vep.prev_sid = session->vep.prev_sid;
4006 memset (&session->vep, 0, sizeof (session->vep));
4007 session->vep.next_sid = ~0;
4008 session->vep.prev_sid = ~0;
4009 session->vep.vep_idx = ~0;
4010 session->is_vep_session = 0;
4011 if (VPPCOM_DEBUG > 1)
4012 clib_warning ("VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
4013 getpid (), vep_idx, session_index);
4014 if (VPPCOM_DEBUG > 0)
4017 ELOG_TYPE_DECLARE (e) =
4019 .format = "epoll_ctldel: vep:%d",
4020 .format_args = "i4",
4027 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4035 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
4039 vep_verify_epoll_chain (vep_idx);
4042 clib_spinlock_unlock (&vcm->sessions_lockp);
4047 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
4048 int maxevents, double wait_for_time)
4050 session_t *vep_session;
4051 elog_track_t vep_elog_track;
4053 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
4054 u32 keep_trying = 1;
4056 u32 vep_next_sid, wait_cont_idx;
4059 if (PREDICT_FALSE (maxevents <= 0))
4061 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
4062 getpid (), maxevents);
4063 return VPPCOM_EINVAL;
4065 memset (events, 0, sizeof (*events) * maxevents);
4067 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4068 vep_next_sid = vep_session->vep.next_sid;
4069 is_vep = vep_session->is_vep;
4070 wait_cont_idx = vep_session->wait_cont_idx;
4071 vep_elog_track = vep_session->elog_track;
4072 clib_spinlock_unlock (&vcm->sessions_lockp);
4074 if (PREDICT_FALSE (!is_vep))
4076 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
4077 getpid (), vep_idx);
4081 if (PREDICT_FALSE (vep_next_sid == ~0))
4083 if (VPPCOM_DEBUG > 1)
4084 clib_warning ("VCL<%d>: WARNING: vep_idx (%u) is empty!",
4085 getpid (), vep_idx);
4086 if (VPPCOM_DEBUG > 1)
4089 ELOG_TYPE_DECLARE (e) =
4091 .format = "WRN: vep_idx:%d empty",
4092 .format_args = "i4",
4099 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_elog_track);
4112 elog_track_t session_elog_track;
4114 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
4115 sid != ~0; sid = next_sid)
4117 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
4118 u8 add_event, is_vep_session;
4120 u64 session_ev_data;
4122 VCL_LOCK_AND_GET_SESSION (sid, &session);
4123 next_sid = session->vep.next_sid;
4124 session_events = session->vep.ev.events;
4125 et_mask = session->vep.et_mask;
4126 is_vep = session->is_vep;
4127 is_vep_session = session->is_vep_session;
4128 session_vep_idx = session->vep.vep_idx;
4129 session_ev_data = session->vep.ev.data.u64;
4131 if (VPPCOM_DEBUG > 0)
4133 session_elog_track = session->elog_track;
4136 clib_spinlock_unlock (&vcm->sessions_lockp);
4138 if (PREDICT_FALSE (is_vep))
4140 if (VPPCOM_DEBUG > 0)
4141 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
4142 getpid (), vep_idx);
4143 if (VPPCOM_DEBUG > 0)
4146 ELOG_TYPE_DECLARE (e) =
4148 .format = "ERR:vep_idx:%d is vep",
4149 .format_args = "i4",
4156 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
4165 if (PREDICT_FALSE (!is_vep_session))
4167 if (VPPCOM_DEBUG > 0)
4168 clib_warning ("VCL<%d>: ERROR: session (%u) is not "
4169 "a vep session!", getpid (), sid);
4170 if (VPPCOM_DEBUG > 0)
4173 ELOG_TYPE_DECLARE (e) =
4175 .format = "ERR:SID:%d not vep",
4176 .format_args = "i4",
4183 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
4192 if (PREDICT_FALSE (session_vep_idx != vep_idx))
4194 clib_warning ("VCL<%d>: ERROR: session (%u) "
4195 "vep_idx (%u) != vep_idx (%u)!",
4196 getpid (), sid, session_vep_idx, vep_idx);
4201 add_event = clear_et_mask = 0;
4203 if (EPOLLIN & session_events)
4205 VCL_LOCK_AND_GET_SESSION (sid, &session);
4206 ready = vppcom_session_read_ready (session, sid);
4207 clib_spinlock_unlock (&vcm->sessions_lockp);
4208 if ((ready > 0) && (EPOLLIN & et_mask))
4211 events[num_ev].events |= EPOLLIN;
4212 if (((EPOLLET | EPOLLIN) & session_events) ==
4213 (EPOLLET | EPOLLIN))
4214 clear_et_mask |= EPOLLIN;
4221 case VPPCOM_ECONNRESET:
4222 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
4226 events[num_ev].events |= EPOLLERR;
4232 if (EPOLLOUT & session_events)
4234 VCL_LOCK_AND_GET_SESSION (sid, &session);
4235 ready = vppcom_session_write_ready (session, sid);
4236 clib_spinlock_unlock (&vcm->sessions_lockp);
4237 if ((ready > 0) && (EPOLLOUT & et_mask))
4240 events[num_ev].events |= EPOLLOUT;
4241 if (((EPOLLET | EPOLLOUT) & session_events) ==
4242 (EPOLLET | EPOLLOUT))
4243 clear_et_mask |= EPOLLOUT;
4250 case VPPCOM_ECONNRESET:
4251 events[num_ev].events |= EPOLLHUP;
4255 events[num_ev].events |= EPOLLERR;
4263 events[num_ev].data.u64 = session_ev_data;
4264 if (EPOLLONESHOT & session_events)
4266 VCL_LOCK_AND_GET_SESSION (sid, &session);
4267 session->vep.ev.events = 0;
4268 clib_spinlock_unlock (&vcm->sessions_lockp);
4271 if (num_ev == maxevents)
4273 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4274 vep_session->wait_cont_idx = next_sid;
4275 clib_spinlock_unlock (&vcm->sessions_lockp);
4279 if (wait_cont_idx != ~0)
4282 next_sid = vep_next_sid;
4283 else if (next_sid == wait_cont_idx)
4287 if (wait_for_time != -1)
4288 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
4290 while ((num_ev == 0) && keep_trying);
4292 if (wait_cont_idx != ~0)
4294 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4295 vep_session->wait_cont_idx = ~0;
4296 clib_spinlock_unlock (&vcm->sessions_lockp);
4299 return (rv != VPPCOM_OK) ? rv : num_ev;
4303 vppcom_session_attr (uint32_t session_index, uint32_t op,
4304 void *buffer, uint32_t * buflen)
4308 u32 *flags = buffer;
4309 vppcom_endpt_t *ep = buffer;
4311 VCL_LOCK_AND_GET_SESSION (session_index, &session);
4317 case VPPCOM_ATTR_GET_NREAD:
4318 rv = vppcom_session_read_ready (session, session_index);
4319 if (VPPCOM_DEBUG > 2)
4320 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
4322 if (VPPCOM_DEBUG > 0)
4325 ELOG_TYPE_DECLARE (e) =
4327 .format = "VPPCOM_ATTR_GET_NREAD: nread=%d",
4328 .format_args = "i4",
4335 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4343 case VPPCOM_ATTR_GET_NWRITE:
4344 rv = vppcom_session_write_ready (session, session_index);
4345 if (VPPCOM_DEBUG > 2)
4346 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
4347 getpid (), session_index, rv);
4348 if (VPPCOM_DEBUG > 0)
4351 ELOG_TYPE_DECLARE (e) =
4353 .format = "VPPCOM_ATTR_GET_NWRITE: nwrite=%d",
4354 .format_args = "i4",
4361 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4368 case VPPCOM_ATTR_GET_FLAGS:
4369 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
4371 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
4372 VCL_SESS_ATTR_NONBLOCK));
4373 *buflen = sizeof (*flags);
4374 if (VPPCOM_DEBUG > 2)
4375 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, "
4376 "flags = 0x%08x, is_nonblocking = %u", getpid (),
4377 session_index, *flags,
4378 VCL_SESS_ATTR_TEST (session->attr,
4379 VCL_SESS_ATTR_NONBLOCK));
4380 if (VPPCOM_DEBUG > 0)
4383 ELOG_TYPE_DECLARE (e) =
4385 .format = "VPPCOM_ATTR_GET_FLAGS: flags=%x is_nonblk=%d",
4386 .format_args = "i4i4",
4394 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4397 ed->is_nonblk = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
4406 case VPPCOM_ATTR_SET_FLAGS:
4407 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
4409 if (*flags & O_NONBLOCK)
4410 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
4412 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
4414 if (VPPCOM_DEBUG > 2)
4415 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, "
4416 "flags = 0x%08x, is_nonblocking = %u",
4417 getpid (), session_index, *flags,
4418 VCL_SESS_ATTR_TEST (session->attr,
4419 VCL_SESS_ATTR_NONBLOCK));
4420 if (VPPCOM_DEBUG > 0)
4423 ELOG_TYPE_DECLARE (e) =
4425 .format = "VPPCOM_ATTR_SET_FLAGS: flags=%x is_nonblk=%d",
4426 .format_args = "i4i4",
4434 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4437 ed->is_nonblk = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
4445 case VPPCOM_ATTR_GET_PEER_ADDR:
4446 if (PREDICT_TRUE (buffer && buflen &&
4447 (*buflen >= sizeof (*ep)) && ep->ip))
4449 ep->is_ip4 = session->peer_addr.is_ip4;
4450 ep->port = session->peer_port;
4451 if (session->peer_addr.is_ip4)
4452 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
4453 sizeof (ip4_address_t));
4455 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
4456 sizeof (ip6_address_t));
4457 *buflen = sizeof (*ep);
4458 if (VPPCOM_DEBUG > 1)
4459 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, "
4460 "is_ip4 = %u, addr = %U, port %u", getpid (),
4461 session_index, ep->is_ip4, format_ip46_address,
4462 &session->peer_addr.ip46, ep->is_ip4,
4463 clib_net_to_host_u16 (ep->port));
4464 if (VPPCOM_DEBUG > 0)
4469 ELOG_TYPE_DECLARE (e) =
4471 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:%d.%d.%d.%d:%d",
4472 .format_args = "i1i1i1i1i2",
4474 CLIB_PACKED (struct {
4479 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4481 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
4482 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
4483 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
4484 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
4485 ed->port = clib_net_to_host_u16 (session->peer_port);
4491 ELOG_TYPE_DECLARE (e) =
4493 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:IP6:%d",
4494 .format_args = "i2",
4496 CLIB_PACKED (struct {
4500 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4502 ed->port = clib_net_to_host_u16 (session->peer_port);
4511 case VPPCOM_ATTR_GET_LCL_ADDR:
4512 if (PREDICT_TRUE (buffer && buflen &&
4513 (*buflen >= sizeof (*ep)) && ep->ip))
4515 ep->is_ip4 = session->lcl_addr.is_ip4;
4516 ep->port = session->lcl_port;
4517 if (session->lcl_addr.is_ip4)
4518 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
4519 sizeof (ip4_address_t));
4521 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
4522 sizeof (ip6_address_t));
4523 *buflen = sizeof (*ep);
4524 if (VPPCOM_DEBUG > 1)
4525 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, "
4526 "is_ip4 = %u, addr = %U port %d", getpid (),
4527 session_index, ep->is_ip4, format_ip46_address,
4528 &session->lcl_addr.ip46, ep->is_ip4,
4529 clib_net_to_host_u16 (ep->port));
4530 if (VPPCOM_DEBUG > 0)
4535 ELOG_TYPE_DECLARE (e) =
4537 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:%d.%d.%d.%d:%d",
4538 .format_args = "i1i1i1i1i2",
4540 CLIB_PACKED (struct {
4545 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4547 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
4548 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
4549 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
4550 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
4551 ed->port = clib_net_to_host_u16 (session->peer_port);
4557 ELOG_TYPE_DECLARE (e) =
4559 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:IP6:%d",
4560 .format_args = "i2",
4562 CLIB_PACKED (struct {
4566 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4568 ed->port = clib_net_to_host_u16 (session->peer_port);
4577 case VPPCOM_ATTR_GET_LIBC_EPFD:
4578 rv = session->libc_epfd;
4579 if (VPPCOM_DEBUG > 2)
4580 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
4582 if (VPPCOM_DEBUG > 0)
4585 ELOG_TYPE_DECLARE (e) =
4587 .format = "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd=%d",
4588 .format_args = "i4",
4590 CLIB_PACKED (struct {
4594 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4595 ed->data = session->libc_epfd;
4601 case VPPCOM_ATTR_SET_LIBC_EPFD:
4602 if (PREDICT_TRUE (buffer && buflen &&
4603 (*buflen == sizeof (session->libc_epfd))))
4605 session->libc_epfd = *(int *) buffer;
4606 *buflen = sizeof (session->libc_epfd);
4608 if (VPPCOM_DEBUG > 2)
4609 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
4610 "buflen %d", getpid (), session->libc_epfd,
4612 if (VPPCOM_DEBUG > 0)
4615 ELOG_TYPE_DECLARE (e) =
4617 .format = "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd=%s%d buflen=%d",
4618 .format_args = "t1i4i4",
4619 .n_enum_strings = 2,
4620 .enum_strings = {"", "-",},
4622 CLIB_PACKED (struct {
4627 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4629 ed->sign = (session->libc_epfd < 0);
4630 ed->data[0] = abs(session->libc_epfd);
4631 ed->data[1] = *buflen;
4639 case VPPCOM_ATTR_GET_PROTOCOL:
4640 if (buffer && buflen && (*buflen >= sizeof (int)))
4642 *(int *) buffer = session->proto;
4643 *buflen = sizeof (int);
4645 if (VPPCOM_DEBUG > 2)
4646 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), "
4647 "buflen %d", getpid (), *(int *) buffer,
4648 *(int *) buffer ? "UDP" : "TCP", *buflen);
4649 if (VPPCOM_DEBUG > 0)
4652 ELOG_TYPE_DECLARE (e) =
4654 .format = "VPPCOM_ATTR_GET_PROTOCOL: %s buflen=%d",
4655 .format_args = "t1i4",
4656 .n_enum_strings = 2,
4657 .enum_strings = {"TCP", "UDP",},
4660 CLIB_PACKED (struct {
4665 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4666 ed->proto = session->proto;
4667 ed->buflen = *(int *) buffer;
4675 case VPPCOM_ATTR_GET_LISTEN:
4676 if (buffer && buflen && (*buflen >= sizeof (int)))
4678 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4679 VCL_SESS_ATTR_LISTEN);
4680 *buflen = sizeof (int);
4682 if (VPPCOM_DEBUG > 2)
4683 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, "
4684 "buflen %d", getpid (), *(int *) buffer, *buflen);
4685 if (VPPCOM_DEBUG > 0)
4688 ELOG_TYPE_DECLARE (e) =
4690 .format = "VPPCOM_ATTR_GET_LISTEN: %d buflen=%d",
4691 .format_args = "i4i4",
4698 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4699 ed->data[0] = *(int *) buffer;
4700 ed->data[1] = *buflen;
4708 case VPPCOM_ATTR_GET_ERROR:
4709 if (buffer && buflen && (*buflen >= sizeof (int)))
4711 *(int *) buffer = 0;
4712 *buflen = sizeof (int);
4714 if (VPPCOM_DEBUG > 2)
4715 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, "
4716 "buflen %d, #VPP-TBD#", getpid (),
4717 *(int *) buffer, *buflen);
4718 if (VPPCOM_DEBUG > 0)
4721 ELOG_TYPE_DECLARE (e) =
4723 .format = "VPPCOM_ATTR_GET_ERROR: %d buflen=%d",
4724 .format_args = "i4i4",
4731 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4732 ed->data[0] = *(int *) buffer;
4733 ed->data[1] = *buflen;
4741 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
4742 if (buffer && buflen && (*buflen >= sizeof (u32)))
4746 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
4747 session->tx_fifo ? session->tx_fifo->nitems :
4748 vcm->cfg.tx_fifo_size);
4749 *buflen = sizeof (u32);
4751 if (VPPCOM_DEBUG > 2)
4752 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
4753 "buflen %d, #VPP-TBD#", getpid (),
4754 *(size_t *) buffer, *(size_t *) buffer, *buflen);
4755 if (VPPCOM_DEBUG > 0)
4758 ELOG_TYPE_DECLARE (e) =
4760 .format = "VPPCOM_ATTR_GET_TX_FIFO_LEN: 0x%x buflen=%d",
4761 .format_args = "i4i4",
4768 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4769 ed->data[0] = *(size_t *) buffer;
4770 ed->data[1] = *buflen;
4778 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
4779 if (buffer && buflen && (*buflen == sizeof (u32)))
4782 session->sndbuf_size = *(u32 *) buffer;
4783 if (VPPCOM_DEBUG > 2)
4784 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
4785 "buflen %d, #VPP-TBD#", getpid (),
4786 session->sndbuf_size, session->sndbuf_size,
4788 if (VPPCOM_DEBUG > 0)
4791 ELOG_TYPE_DECLARE (e) =
4793 .format = "VPPCOM_ATTR_SET_TX_FIFO_LEN: 0x%x buflen=%d",
4794 .format_args = "i4i4",
4801 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4802 ed->data[0] = session->sndbuf_size;
4803 ed->data[1] = *buflen;
4811 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
4812 if (buffer && buflen && (*buflen >= sizeof (u32)))
4816 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
4817 session->rx_fifo ? session->rx_fifo->nitems :
4818 vcm->cfg.rx_fifo_size);
4819 *buflen = sizeof (u32);
4821 if (VPPCOM_DEBUG > 2)
4822 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
4823 "buflen %d, #VPP-TBD#", getpid (),
4824 *(size_t *) buffer, *(size_t *) buffer, *buflen);
4825 if (VPPCOM_DEBUG > 0)
4828 ELOG_TYPE_DECLARE (e) =
4830 .format = "VPPCOM_ATTR_GET_RX_FIFO_LEN: 0x%x buflen=%d",
4831 .format_args = "i4i4",
4838 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4839 ed->data[0] = *(size_t *) buffer;
4840 ed->data[1] = *buflen;
4848 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
4849 if (buffer && buflen && (*buflen == sizeof (u32)))
4852 session->rcvbuf_size = *(u32 *) buffer;
4853 if (VPPCOM_DEBUG > 2)
4854 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
4855 "buflen %d, #VPP-TBD#", getpid (),
4856 session->sndbuf_size, session->sndbuf_size,
4858 if (VPPCOM_DEBUG > 0)
4861 ELOG_TYPE_DECLARE (e) =
4863 .format = "VPPCOM_ATTR_SET_RX_FIFO_LEN: 0x%x buflen=%d",
4864 .format_args = "i4i4",
4871 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4872 ed->data[0] = session->sndbuf_size;
4873 ed->data[1] = *buflen;
4881 case VPPCOM_ATTR_GET_REUSEADDR:
4882 if (buffer && buflen && (*buflen >= sizeof (int)))
4885 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4886 VCL_SESS_ATTR_REUSEADDR);
4887 *buflen = sizeof (int);
4889 if (VPPCOM_DEBUG > 2)
4890 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
4891 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4893 if (VPPCOM_DEBUG > 0)
4896 ELOG_TYPE_DECLARE (e) =
4898 .format = "VPPCOM_ATTR_GET_REUSEADDR: %d buflen=%d",
4899 .format_args = "i4i4",
4906 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4907 ed->data[0] = *(int *) buffer;
4908 ed->data[1] = *buflen;
4916 case VPPCOM_ATTR_SET_REUSEADDR:
4917 if (buffer && buflen && (*buflen == sizeof (int)) &&
4918 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4921 if (*(int *) buffer)
4922 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
4924 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
4926 if (VPPCOM_DEBUG > 2)
4927 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, "
4928 "buflen %d, #VPP-TBD#", getpid (),
4929 VCL_SESS_ATTR_TEST (session->attr,
4930 VCL_SESS_ATTR_REUSEADDR),
4932 if (VPPCOM_DEBUG > 0)
4935 ELOG_TYPE_DECLARE (e) =
4937 .format = "VPPCOM_ATTR_SET_REUSEADDR: %d buflen=%d",
4938 .format_args = "i4i4",
4945 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4946 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4947 VCL_SESS_ATTR_REUSEADDR);
4948 ed->data[1] = *buflen;
4956 case VPPCOM_ATTR_GET_REUSEPORT:
4957 if (buffer && buflen && (*buflen >= sizeof (int)))
4960 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4961 VCL_SESS_ATTR_REUSEPORT);
4962 *buflen = sizeof (int);
4964 if (VPPCOM_DEBUG > 2)
4965 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, "
4966 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4968 if (VPPCOM_DEBUG > 0)
4971 ELOG_TYPE_DECLARE (e) =
4973 .format = "VPPCOM_ATTR_GET_REUSEPORT: %d buflen=%d",
4974 .format_args = "i4i4",
4981 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4982 ed->data[0] = *(int *) buffer;
4983 ed->data[1] = *buflen;
4991 case VPPCOM_ATTR_SET_REUSEPORT:
4992 if (buffer && buflen && (*buflen == sizeof (int)) &&
4993 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4996 if (*(int *) buffer)
4997 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
4999 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
5001 if (VPPCOM_DEBUG > 2)
5002 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, "
5003 "buflen %d, #VPP-TBD#", getpid (),
5004 VCL_SESS_ATTR_TEST (session->attr,
5005 VCL_SESS_ATTR_REUSEPORT),
5007 if (VPPCOM_DEBUG > 0)
5010 ELOG_TYPE_DECLARE (e) =
5012 .format = "VPPCOM_ATTR_SET_REUSEPORT: %d buflen=%d",
5013 .format_args = "i4i4",
5020 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5021 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5022 VCL_SESS_ATTR_REUSEPORT);
5023 ed->data[1] = *buflen;
5031 case VPPCOM_ATTR_GET_BROADCAST:
5032 if (buffer && buflen && (*buflen >= sizeof (int)))
5035 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5036 VCL_SESS_ATTR_BROADCAST);
5037 *buflen = sizeof (int);
5039 if (VPPCOM_DEBUG > 2)
5040 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, "
5041 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5043 if (VPPCOM_DEBUG > 0)
5046 ELOG_TYPE_DECLARE (e) =
5048 .format = "VPPCOM_ATTR_GET_BROADCAST: %d buflen=%d",
5049 .format_args = "i4i4",
5056 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5057 ed->data[0] = *(int *) buffer;
5058 ed->data[1] = *buflen;
5066 case VPPCOM_ATTR_SET_BROADCAST:
5067 if (buffer && buflen && (*buflen == sizeof (int)))
5070 if (*(int *) buffer)
5071 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
5073 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
5075 if (VPPCOM_DEBUG > 2)
5076 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, "
5077 "buflen %d, #VPP-TBD#", getpid (),
5078 VCL_SESS_ATTR_TEST (session->attr,
5079 VCL_SESS_ATTR_BROADCAST),
5081 if (VPPCOM_DEBUG > 0)
5084 ELOG_TYPE_DECLARE (e) =
5086 .format = "VPPCOM_ATTR_SET_BROADCAST: %d buflen=%d",
5087 .format_args = "i4i4",
5094 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5095 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5096 VCL_SESS_ATTR_BROADCAST);
5097 ed->data[1] = *buflen;
5105 case VPPCOM_ATTR_GET_V6ONLY:
5106 if (buffer && buflen && (*buflen >= sizeof (int)))
5109 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5110 VCL_SESS_ATTR_V6ONLY);
5111 *buflen = sizeof (int);
5113 if (VPPCOM_DEBUG > 2)
5114 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, "
5115 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5117 if (VPPCOM_DEBUG > 0)
5120 ELOG_TYPE_DECLARE (e) =
5122 .format = "VPPCOM_ATTR_GET_V6ONLY: %d buflen=%d",
5123 .format_args = "i4i4",
5130 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5131 ed->data[0] = *(int *) buffer;
5132 ed->data[1] = *buflen;
5140 case VPPCOM_ATTR_SET_V6ONLY:
5141 if (buffer && buflen && (*buflen == sizeof (int)))
5144 if (*(int *) buffer)
5145 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
5147 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
5149 if (VPPCOM_DEBUG > 2)
5150 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, "
5151 "buflen %d, #VPP-TBD#", getpid (),
5152 VCL_SESS_ATTR_TEST (session->attr,
5153 VCL_SESS_ATTR_V6ONLY), *buflen);
5154 if (VPPCOM_DEBUG > 0)
5157 ELOG_TYPE_DECLARE (e) =
5159 .format = "VPPCOM_ATTR_SET_V6ONLY: %d buflen=%d",
5160 .format_args = "i4i4",
5167 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5168 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5169 VCL_SESS_ATTR_V6ONLY);
5170 ed->data[1] = *buflen;
5178 case VPPCOM_ATTR_GET_KEEPALIVE:
5179 if (buffer && buflen && (*buflen >= sizeof (int)))
5182 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5183 VCL_SESS_ATTR_KEEPALIVE);
5184 *buflen = sizeof (int);
5186 if (VPPCOM_DEBUG > 2)
5187 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, "
5188 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5190 if (VPPCOM_DEBUG > 0)
5193 ELOG_TYPE_DECLARE (e) =
5195 .format = "VPPCOM_ATTR_GET_KEEPALIVE: %d buflen=%d",
5196 .format_args = "i4i4",
5203 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5204 ed->data[0] = *(int *) buffer;
5205 ed->data[1] = *buflen;
5213 case VPPCOM_ATTR_SET_KEEPALIVE:
5214 if (buffer && buflen && (*buflen == sizeof (int)))
5217 if (*(int *) buffer)
5218 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5220 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5222 if (VPPCOM_DEBUG > 2)
5223 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, "
5224 "buflen %d, #VPP-TBD#", getpid (),
5225 VCL_SESS_ATTR_TEST (session->attr,
5226 VCL_SESS_ATTR_KEEPALIVE),
5228 if (VPPCOM_DEBUG > 0)
5231 ELOG_TYPE_DECLARE (e) =
5233 .format = "VPPCOM_ATTR_SET_KEEPALIVE: %d buflen=%d",
5234 .format_args = "i4i4",
5241 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5242 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5243 VCL_SESS_ATTR_KEEPALIVE);
5244 ed->data[1] = *buflen;
5252 case VPPCOM_ATTR_GET_TCP_NODELAY:
5253 if (buffer && buflen && (*buflen >= sizeof (int)))
5256 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5257 VCL_SESS_ATTR_TCP_NODELAY);
5258 *buflen = sizeof (int);
5260 if (VPPCOM_DEBUG > 2)
5261 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, "
5262 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5264 if (VPPCOM_DEBUG > 0)
5267 ELOG_TYPE_DECLARE (e) =
5269 .format = "VPPCOM_ATTR_GET_TCP_NODELAY: %d buflen=%d",
5270 .format_args = "i4i4",
5277 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5278 ed->data[0] = *(int *) buffer;
5279 ed->data[1] = *buflen;
5287 case VPPCOM_ATTR_SET_TCP_NODELAY:
5288 if (buffer && buflen && (*buflen == sizeof (int)))
5291 if (*(int *) buffer)
5292 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5294 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5296 if (VPPCOM_DEBUG > 2)
5297 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, "
5298 "buflen %d, #VPP-TBD#", getpid (),
5299 VCL_SESS_ATTR_TEST (session->attr,
5300 VCL_SESS_ATTR_TCP_NODELAY),
5302 if (VPPCOM_DEBUG > 0)
5305 ELOG_TYPE_DECLARE (e) =
5307 .format = "VPPCOM_ATTR_SET_TCP_NODELAY: %d buflen=%d",
5308 .format_args = "i4i4",
5315 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5316 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5317 VCL_SESS_ATTR_TCP_NODELAY);
5318 ed->data[1] = *buflen;
5326 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
5327 if (buffer && buflen && (*buflen >= sizeof (int)))
5330 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5331 VCL_SESS_ATTR_TCP_KEEPIDLE);
5332 *buflen = sizeof (int);
5334 if (VPPCOM_DEBUG > 2)
5335 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, "
5336 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5338 if (VPPCOM_DEBUG > 0)
5341 ELOG_TYPE_DECLARE (e) =
5343 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5344 .format_args = "i4i4",
5351 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5352 ed->data[0] = *(int *) buffer;
5353 ed->data[1] = *buflen;
5361 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
5362 if (buffer && buflen && (*buflen == sizeof (int)))
5365 if (*(int *) buffer)
5366 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5368 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5370 if (VPPCOM_DEBUG > 2)
5371 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, "
5372 "buflen %d, #VPP-TBD#", getpid (),
5373 VCL_SESS_ATTR_TEST (session->attr,
5374 VCL_SESS_ATTR_TCP_KEEPIDLE),
5376 if (VPPCOM_DEBUG > 0)
5379 ELOG_TYPE_DECLARE (e) =
5381 .format = "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d buflen=%d",
5382 .format_args = "i4i4",
5389 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5390 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5391 VCL_SESS_ATTR_TCP_KEEPIDLE);
5392 ed->data[1] = *buflen;
5400 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
5401 if (buffer && buflen && (*buflen >= sizeof (int)))
5404 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5405 VCL_SESS_ATTR_TCP_KEEPINTVL);
5406 *buflen = sizeof (int);
5408 if (VPPCOM_DEBUG > 2)
5409 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, "
5410 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5412 if (VPPCOM_DEBUG > 0)
5415 ELOG_TYPE_DECLARE (e) =
5417 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5418 .format_args = "i4i4",
5425 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5426 ed->data[0] = *(int *) buffer;
5427 ed->data[1] = *buflen;
5435 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
5436 if (buffer && buflen && (*buflen == sizeof (int)))
5439 if (*(int *) buffer)
5440 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5442 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5444 if (VPPCOM_DEBUG > 2)
5445 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, "
5446 "buflen %d, #VPP-TBD#", getpid (),
5447 VCL_SESS_ATTR_TEST (session->attr,
5448 VCL_SESS_ATTR_TCP_KEEPINTVL),
5450 if (VPPCOM_DEBUG > 0)
5453 ELOG_TYPE_DECLARE (e) =
5455 .format = "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d buflen=%d",
5456 .format_args = "i4i4",
5463 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5464 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5465 VCL_SESS_ATTR_TCP_KEEPINTVL);
5466 ed->data[1] = *buflen;
5474 case VPPCOM_ATTR_GET_TCP_USER_MSS:
5475 if (buffer && buflen && (*buflen >= sizeof (u32)))
5478 *(u32 *) buffer = session->user_mss;
5479 *buflen = sizeof (int);
5481 if (VPPCOM_DEBUG > 2)
5482 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, "
5483 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5485 if (VPPCOM_DEBUG > 0)
5488 ELOG_TYPE_DECLARE (e) =
5490 .format = "VPPCOM_ATTR_GET_TCP_USER_MSS: %d buflen=%d",
5491 .format_args = "i4i4",
5498 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5499 ed->data[0] = *(int *) buffer;
5500 ed->data[1] = *buflen;
5508 case VPPCOM_ATTR_SET_TCP_USER_MSS:
5509 if (buffer && buflen && (*buflen == sizeof (u32)))
5512 session->user_mss = *(u32 *) buffer;
5514 if (VPPCOM_DEBUG > 2)
5515 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, "
5516 "buflen %d, #VPP-TBD#", getpid (),
5517 session->user_mss, *buflen);
5518 if (VPPCOM_DEBUG > 0)
5521 ELOG_TYPE_DECLARE (e) =
5523 .format = "VPPCOM_ATTR_SET_TCP_USER_MSS: %d buflen=%d",
5524 .format_args = "i4i4",
5531 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5532 ed->data[0] = session->user_mss;
5533 ed->data[1] = *buflen;
5547 clib_spinlock_unlock (&vcm->sessions_lockp);
5552 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
5553 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5556 session_t *session = 0;
5560 clib_spinlock_lock (&vcm->sessions_lockp);
5561 rv = vppcom_session_at_index (session_index, &session);
5562 if (PREDICT_FALSE (rv))
5564 clib_spinlock_unlock (&vcm->sessions_lockp);
5565 if (VPPCOM_DEBUG > 0)
5566 clib_warning ("VCL<%d>: invalid session, "
5567 "sid (%u) has been closed!",
5568 getpid (), session_index);
5569 if (VPPCOM_DEBUG > 0)
5572 ELOG_TYPE_DECLARE (e) =
5574 .format = "invalid session: %d closed",
5575 .format_args = "i4",
5582 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
5583 ed->data = session_index;
5587 clib_spinlock_unlock (&vcm->sessions_lockp);
5590 ep->is_ip4 = session->peer_addr.is_ip4;
5591 ep->port = session->peer_port;
5592 if (session->peer_addr.is_ip4)
5593 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
5594 sizeof (ip4_address_t));
5596 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
5597 sizeof (ip6_address_t));
5598 clib_spinlock_unlock (&vcm->sessions_lockp);
5602 rv = vppcom_session_read (session_index, buffer, buflen);
5603 else if (flags & MSG_PEEK)
5604 rv = vppcom_session_peek (session_index, buffer, buflen);
5607 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
5609 rv = VPPCOM_EAFNOSUPPORT;
5617 vppcom_session_sendto (uint32_t session_index, void *buffer,
5618 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5621 return VPPCOM_EINVAL;
5626 return VPPCOM_EINVAL;
5631 // TBD check the flags and do the right thing
5632 if (VPPCOM_DEBUG > 2)
5633 clib_warning ("VCL<%d>: handling flags 0x%u (%d) "
5634 "not implemented yet.", getpid (), flags, flags);
5637 return (vppcom_session_write (session_index, buffer, buflen));
5641 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
5643 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
5644 u32 i, keep_trying = 1;
5647 if (VPPCOM_DEBUG > 3)
5648 clib_warning ("VCL<%d>: vp %p, nsids %u, wait_for_time %f",
5649 getpid (), vp, n_sids, wait_for_time);
5652 return VPPCOM_EFAULT;
5658 for (i = 0; i < n_sids; i++)
5660 ASSERT (vp[i].revents);
5662 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5663 clib_spinlock_unlock (&vcm->sessions_lockp);
5668 if (POLLIN & vp[i].events)
5670 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5671 rv = vppcom_session_read_ready (session, vp[i].sid);
5672 clib_spinlock_unlock (&vcm->sessions_lockp);
5675 *vp[i].revents |= POLLIN;
5682 case VPPCOM_ECONNRESET:
5683 *vp[i].revents = POLLHUP;
5687 *vp[i].revents = POLLERR;
5694 if (POLLOUT & vp[i].events)
5696 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5697 rv = vppcom_session_write_ready (session, vp[i].sid);
5698 clib_spinlock_unlock (&vcm->sessions_lockp);
5701 *vp[i].revents |= POLLOUT;
5708 case VPPCOM_ECONNRESET:
5709 *vp[i].revents = POLLHUP;
5713 *vp[i].revents = POLLERR;
5720 if (0) // Note "done:" label used by VCL_LOCK_AND_GET_SESSION()
5723 *vp[i].revents = POLLNVAL;
5727 if (wait_for_time != -1)
5728 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
5730 while ((num_ev == 0) && keep_trying);
5732 if (VPPCOM_DEBUG > 3)
5734 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
5735 for (i = 0; i < n_sids; i++)
5737 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
5738 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
5739 vp[i].events, *vp[i].revents);
5746 * fd.io coding-style-patch-verification: ON
5749 * eval: (c-set-style "gnu")