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 session->lcl_addr.ip46 = to_ip46 (!mp->lcl_is_ip4, mp->lcl_ip);
1211 session->lcl_port = mp->lcl_port;
1212 vppcom_session_table_add_listener (mp->handle, session_index);
1213 session->state = STATE_LISTEN;
1215 if (VPPCOM_DEBUG > 1)
1216 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind succeeded!",
1217 getpid (), mp->handle, mp->context);
1219 clib_spinlock_unlock (&vcm->sessions_lockp);
1223 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
1226 clib_warning ("VCL<%d>: ERROR: sid %u: unbind failed: %U",
1227 getpid (), mp->context, format_api_error,
1228 ntohl (mp->retval));
1230 else if (VPPCOM_DEBUG > 1)
1231 clib_warning ("VCL<%d>: sid %u: unbind succeeded!",
1232 getpid (), mp->context);
1236 format_ip4_address (u8 * s, va_list * args)
1238 u8 *a = va_arg (*args, u8 *);
1239 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
1243 format_ip6_address (u8 * s, va_list * args)
1245 ip6_address_t *a = va_arg (*args, ip6_address_t *);
1246 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
1248 i_max_n_zero = ARRAY_LEN (a->as_u16);
1250 i_first_zero = i_max_n_zero;
1252 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1254 u32 is_zero = a->as_u16[i] == 0;
1255 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
1261 if ((!is_zero && n_zeros > max_n_zeros)
1262 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
1264 i_max_n_zero = i_first_zero;
1265 max_n_zeros = n_zeros;
1266 i_first_zero = ARRAY_LEN (a->as_u16);
1271 last_double_colon = 0;
1272 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
1274 if (i == i_max_n_zero && max_n_zeros > 1)
1276 s = format (s, "::");
1277 i += max_n_zeros - 1;
1278 last_double_colon = 1;
1282 s = format (s, "%s%x",
1283 (last_double_colon || i == 0) ? "" : ":",
1284 clib_net_to_host_u16 (a->as_u16[i]));
1285 last_double_colon = 0;
1292 /* Format an IP46 address. */
1294 format_ip46_address (u8 * s, va_list * args)
1296 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
1297 ip46_type_t type = va_arg (*args, ip46_type_t);
1303 is_ip4 = ip46_address_is_ip4 (ip46);
1314 format (s, "%U", format_ip4_address, &ip46->ip4) :
1315 format (s, "%U", format_ip6_address, &ip46->ip6);
1319 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
1321 svm_fifo_t *rx_fifo, *tx_fifo;
1322 session_t *session, *listen_session;
1324 vce_event_connect_request_t *ecr;
1330 clib_spinlock_lock (&vcm->sessions_lockp);
1332 clib_spinlock_lock (&vcm->session_fifo_lockp);
1333 elts = clib_fifo_free_elts (vcm->client_session_index_fifo);
1334 clib_spinlock_unlock (&vcm->session_fifo_lockp);
1338 clib_warning ("VCL<%d>: client session queue is full!", getpid ());
1339 vppcom_send_accept_session_reply (mp->handle, mp->context,
1340 VNET_API_ERROR_QUEUE_FULL);
1341 clib_spinlock_unlock (&vcm->sessions_lockp);
1345 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1346 if (!listen_session)
1348 clib_warning ("VCL<%d>: ERROR: couldn't find listen session: "
1349 "unknown vpp listener handle %llx",
1350 getpid (), mp->listener_handle);
1351 vppcom_send_accept_session_reply (mp->handle, mp->context,
1352 VNET_API_ERROR_INVALID_ARGUMENT);
1353 clib_spinlock_unlock (&vcm->sessions_lockp);
1357 /* TODO check listener depth and update */
1358 /* TODO on "child" fd close, update listener depth */
1360 /* Allocate local session and set it up */
1361 pool_get (vcm->sessions, session);
1362 memset (session, 0, sizeof (*session));
1363 session_index = session - vcm->sessions;
1365 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1366 rx_fifo->client_session_index = session_index;
1367 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1368 tx_fifo->client_session_index = session_index;
1370 session->vpp_handle = mp->handle;
1371 session->client_context = mp->context;
1372 session->rx_fifo = rx_fifo;
1373 session->tx_fifo = tx_fifo;
1374 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1376 session->state = STATE_ACCEPT;
1377 session->peer_port = mp->port;
1378 session->peer_addr.is_ip4 = mp->is_ip4;
1379 session->peer_addr.ip46 = to_ip46 (!mp->is_ip4, mp->ip);
1381 /* Add it to lookup table */
1382 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1383 session->lcl_port = listen_session->lcl_port;
1384 session->lcl_addr = listen_session->lcl_addr;
1386 /* Create an event for handlers */
1388 clib_spinlock_lock (&vcm->event_thread.events_lockp);
1390 pool_get (vcm->event_thread.vce_events, ev);
1391 ev->data = clib_mem_alloc (sizeof (vce_event_connect_request_t));
1393 ev_idx = (u32) (ev - vcm->event_thread.vce_events);
1395 ev->evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
1396 listen_session = vppcom_session_table_lookup_listener (mp->listener_handle);
1397 ev->evk.session_index = (u32) (listen_session - vcm->sessions);
1398 ecr->accepted_session_index = session_index;
1400 clib_spinlock_unlock (&vcm->event_thread.events_lockp);
1402 rv = vce_generate_event (&vcm->event_thread, ev_idx);
1406 if (VPPCOM_DEBUG > 1)
1407 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client accept "
1408 "request from %s address %U port %d queue %p!", getpid (),
1409 mp->handle, session_index, mp->is_ip4 ? "IPv4" : "IPv6",
1410 format_ip46_address, &mp->ip,
1411 mp->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
1412 clib_net_to_host_u16 (mp->port), session->vpp_event_queue);
1414 if (VPPCOM_DEBUG > 0)
1416 session->elog_track.name =
1417 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
1419 elog_track_register (&vcm->elog_main, &session->elog_track);
1421 if (session->peer_addr.is_ip4)
1424 ELOG_TYPE_DECLARE (e) =
1427 "client_accept:handle:%x addr:%d.%d.%d.%d:%d",
1428 .format_args = "i8i1i1i1i1i2",
1431 CLIB_PACKED (struct {
1437 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
1439 ed->handle = mp->handle;
1440 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
1441 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
1442 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
1443 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
1444 ed->port = clib_net_to_host_u16 (session->peer_port);
1449 clib_warning ("ip6");
1453 clib_spinlock_unlock (&vcm->sessions_lockp);
1458 vppcom_send_bind_sock (session_t * session, u32 session_index)
1460 vl_api_bind_sock_t *bmp;
1462 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1463 bmp = vl_msg_api_alloc (sizeof (*bmp));
1464 memset (bmp, 0, sizeof (*bmp));
1466 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1467 bmp->client_index = vcm->my_client_index;
1468 bmp->context = session_index;
1469 bmp->is_ip4 = session->lcl_addr.is_ip4;
1470 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1471 bmp->port = session->lcl_port;
1472 bmp->proto = session->proto;
1473 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1474 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1478 vppcom_send_unbind_sock (u64 vpp_handle)
1480 vl_api_unbind_sock_t *ump;
1482 ump = vl_msg_api_alloc (sizeof (*ump));
1483 memset (ump, 0, sizeof (*ump));
1485 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1486 ump->client_index = vcm->my_client_index;
1487 ump->handle = vpp_handle;
1488 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1492 vppcom_session_unbind (u32 session_index)
1494 session_t *session = 0;
1497 elog_track_t session_elog_track;
1499 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1501 vpp_handle = session->vpp_handle;
1502 vppcom_session_table_del_listener (vpp_handle);
1503 session->vpp_handle = ~0;
1504 session->state = STATE_DISCONNECT;
1505 session_elog_track = session->elog_track;
1507 clib_spinlock_unlock (&vcm->sessions_lockp);
1509 if (VPPCOM_DEBUG > 1)
1510 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1511 "sending unbind msg! new state 0x%x (%s)",
1512 getpid (), vpp_handle, session_index,
1514 vppcom_session_state_str (STATE_DISCONNECT));
1516 if (VPPCOM_DEBUG > 0)
1519 ELOG_TYPE_DECLARE (e) =
1521 .format = "unbind: handle:%x",
1522 .format_args = "i8",
1530 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
1531 ed->handle = vpp_handle;
1535 vppcom_send_unbind_sock (vpp_handle);
1542 vppcom_session_disconnect (u32 session_index)
1547 session_state_t state;
1549 VCL_LOCK_AND_GET_SESSION (session_index, &session);
1551 vpp_handle = session->vpp_handle;
1552 state = session->state;
1553 clib_spinlock_unlock (&vcm->sessions_lockp);
1555 if (VPPCOM_DEBUG > 1)
1557 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u "
1559 getpid (), vpp_handle, session_index,
1560 state, vppcom_session_state_str (state));
1563 if (PREDICT_FALSE (state & STATE_LISTEN))
1565 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
1566 "Cannot disconnect a listen socket!",
1567 getpid (), vpp_handle, session_index);
1572 /* The peer has already initiated the close,
1573 * so send the disconnect session reply.
1575 if (state & STATE_CLOSE_ON_EMPTY)
1577 //XXX alagalah - Check and drain here?
1578 vppcom_send_disconnect_session_reply (vpp_handle,
1579 session_index, 0 /* rv */ );
1580 if (VPPCOM_DEBUG > 1)
1581 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1582 "sending disconnect REPLY...",
1583 getpid (), vpp_handle, session_index);
1586 /* Otherwise, send a disconnect session msg...
1590 if (VPPCOM_DEBUG > 1)
1591 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
1592 "sending disconnect...",
1593 getpid (), vpp_handle, session_index);
1595 vppcom_send_disconnect_session (vpp_handle, session_index);
1602 #define foreach_sock_msg \
1603 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1604 _(BIND_SOCK_REPLY, bind_sock_reply) \
1605 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1606 _(ACCEPT_SESSION, accept_session) \
1607 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1608 _(DISCONNECT_SESSION, disconnect_session) \
1609 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1610 _(RESET_SESSION, reset_session) \
1611 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1612 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1613 _(MAP_ANOTHER_SEGMENT, map_another_segment) \
1614 _(UNMAP_SEGMENT, unmap_segment)
1617 vppcom_api_hookup (void)
1620 vl_msg_api_set_handlers(VL_API_##N, #n, \
1621 vl_api_##n##_t_handler, \
1623 vl_api_##n##_t_endian, \
1624 vl_api_##n##_t_print, \
1625 sizeof(vl_api_##n##_t), 1);
1631 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1635 vcl_cfg->heapsize = (256ULL << 20);
1636 vcl_cfg->vpp_api_q_length = 1024;
1637 vcl_cfg->segment_baseva = 0x200000000ULL;
1638 vcl_cfg->segment_size = (256 << 20);
1639 vcl_cfg->add_segment_size = (128 << 20);
1640 vcl_cfg->preallocated_fifo_pairs = 8;
1641 vcl_cfg->rx_fifo_size = (1 << 20);
1642 vcl_cfg->tx_fifo_size = (1 << 20);
1643 vcl_cfg->event_queue_size = 2048;
1644 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1645 vcl_cfg->app_timeout = 10 * 60.0;
1646 vcl_cfg->session_timeout = 10 * 60.0;
1647 vcl_cfg->accept_timeout = 60.0;
1648 vcl_cfg->event_ring_size = (128 << 10);
1649 vcl_cfg->event_log_path = "/dev/shm";
1653 vppcom_cfg_heapsize (char *conf_fname)
1655 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1668 fp = fopen (conf_fname, "r");
1671 if (VPPCOM_DEBUG > 0)
1672 clib_warning ("VCL<%d>: using default heapsize %lld (0x%llx)",
1673 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1677 argv = calloc (1, sizeof (char *));
1680 if (VPPCOM_DEBUG > 0)
1681 clib_warning ("VCL<%d>: calloc failed, using default "
1682 "heapsize %lld (0x%llx)",
1683 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1689 if (fgets (inbuf, 4096, fp) == 0)
1691 p = strtok (inbuf, " \t\n");
1697 char **tmp = realloc (argv, argc * sizeof (char *));
1700 if (VPPCOM_DEBUG > 0)
1701 clib_warning ("VCL<%d>: realloc failed, "
1702 "using default heapsize %lld (0x%llx)",
1703 getpid (), vcl_cfg->heapsize,
1708 arg = strndup (p, 1024);
1711 if (VPPCOM_DEBUG > 0)
1712 clib_warning ("VCL<%d>: strndup failed, "
1713 "using default heapsize %lld (0x%llx)",
1714 getpid (), vcl_cfg->heapsize,
1718 argv[argc - 1] = arg;
1719 p = strtok (NULL, " \t\n");
1726 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1729 if (VPPCOM_DEBUG > 0)
1730 clib_warning ("VCL<%d>: realloc failed, "
1731 "using default heapsize %lld (0x%llx)",
1732 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1739 * Look for and parse the "heapsize" config parameter.
1740 * Manual since none of the clib infra has been bootstrapped yet.
1742 * Format: heapsize <nn>[mM][gG]
1745 for (i = 1; i < (argc - 1); i++)
1747 if (!strncmp (argv[i], "heapsize", 8))
1749 sizep = (u8 *) argv[i + 1];
1751 while (*sizep >= '0' && *sizep <= '9')
1754 size += *sizep++ - '0';
1758 if (VPPCOM_DEBUG > 0)
1759 clib_warning ("VCL<%d>: parse error '%s %s', "
1760 "using default heapsize %lld (0x%llx)",
1761 getpid (), argv[i], argv[i + 1],
1762 vcl_cfg->heapsize, vcl_cfg->heapsize);
1766 if (*sizep == 'g' || *sizep == 'G')
1767 vcl_cfg->heapsize = size << 30;
1768 else if (*sizep == 'm' || *sizep == 'M')
1769 vcl_cfg->heapsize = size << 20;
1772 if (VPPCOM_DEBUG > 0)
1773 clib_warning ("VCL<%d>: parse error '%s %s', "
1774 "using default heapsize %lld (0x%llx)",
1775 getpid (), argv[i], argv[i + 1],
1776 vcl_cfg->heapsize, vcl_cfg->heapsize);
1788 vcl_mem = mmap (0, vcl_cfg->heapsize, PROT_READ | PROT_WRITE,
1789 MAP_SHARED | MAP_ANONYMOUS, -1, 0);
1790 if (vcl_mem == MAP_FAILED)
1792 clib_unix_error ("VCL<%d>: ERROR: mmap(0, %lld == 0x%llx, "
1793 "PROT_READ | PROT_WRITE,MAP_SHARED | MAP_ANONYMOUS, "
1795 getpid (), vcl_cfg->heapsize, vcl_cfg->heapsize);
1796 ASSERT (vcl_mem != MAP_FAILED);
1799 heap = clib_mem_init (vcl_mem, vcl_cfg->heapsize);
1802 clib_warning ("VCL<%d>: ERROR: clib_mem_init() failed!", getpid ());
1806 vcl_mem = clib_mem_alloc (sizeof (_vppcom_main));
1809 clib_warning ("VCL<%d>: ERROR: clib_mem_alloc() failed!", getpid ());
1814 clib_memcpy (vcl_mem, &_vppcom_main, sizeof (_vppcom_main));
1817 if (VPPCOM_DEBUG > 0)
1818 clib_warning ("VCL<%d>: allocated VCL heap = %p, size %lld (0x%llx)",
1819 getpid (), heap, vcl_cfg->heapsize, vcl_cfg->heapsize);
1823 vppcom_cfg_read (char *conf_fname)
1825 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1827 unformat_input_t _input, *input = &_input;
1828 unformat_input_t _line_input, *line_input = &_line_input;
1829 u8 vc_cfg_input = 0;
1832 u32 uid, gid, q_len;
1834 fd = open (conf_fname, O_RDONLY);
1837 if (VPPCOM_DEBUG > 0)
1838 clib_warning ("VCL<%d>: using default configuration.",
1839 getpid (), conf_fname);
1843 if (fstat (fd, &s) < 0)
1845 if (VPPCOM_DEBUG > 0)
1846 clib_warning ("VCL<%d>: failed to stat `%s', "
1847 "using default configuration", getpid (), conf_fname);
1851 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1853 if (VPPCOM_DEBUG > 0)
1854 clib_warning ("VCL<%d>: not a regular file `%s', "
1855 "using default configuration", getpid (), conf_fname);
1859 unformat_init_clib_file (input, fd);
1861 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1863 (void) unformat_user (input, unformat_line_input, line_input);
1864 unformat_skip_white_space (line_input);
1866 if (unformat (line_input, "vcl {"))
1874 if (unformat (line_input, "heapsize %s", &chroot_path))
1876 vec_terminate_c_string (chroot_path);
1877 if (VPPCOM_DEBUG > 0)
1878 clib_warning ("VCL<%d>: configured heapsize %s, "
1879 "actual heapsize %lld (0x%llx)",
1880 getpid (), chroot_path, vcl_cfg->heapsize,
1882 vec_free (chroot_path);
1884 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1886 vec_terminate_c_string (chroot_path);
1887 if (vcl_cfg->vpp_api_filename)
1888 vec_free (vcl_cfg->vpp_api_filename);
1889 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
1891 vl_set_memory_root_path ((char *) chroot_path);
1893 if (VPPCOM_DEBUG > 0)
1894 clib_warning ("VCL<%d>: configured api-prefix (%s) and "
1895 "api filename (%s)", getpid (), chroot_path,
1896 vcl_cfg->vpp_api_filename);
1897 chroot_path = 0; /* Don't vec_free() it! */
1899 else if (unformat (line_input, "vpp-api-q-length %d", &q_len))
1901 if (q_len < vcl_cfg->vpp_api_q_length)
1903 clib_warning ("VCL<%d>: ERROR: configured vpp-api-q-length "
1904 "(%u) is too small! Using default: %u ",
1905 getpid (), q_len, vcl_cfg->vpp_api_q_length);
1909 vcl_cfg->vpp_api_q_length = q_len;
1911 if (VPPCOM_DEBUG > 0)
1912 clib_warning ("VCL<%d>: configured vpp-api-q-length %u",
1913 getpid (), vcl_cfg->vpp_api_q_length);
1916 else if (unformat (line_input, "uid %d", &uid))
1918 vl_set_memory_uid (uid);
1919 if (VPPCOM_DEBUG > 0)
1920 clib_warning ("VCL<%d>: configured uid %d", getpid (), uid);
1922 else if (unformat (line_input, "gid %d", &gid))
1924 vl_set_memory_gid (gid);
1925 if (VPPCOM_DEBUG > 0)
1926 clib_warning ("VCL<%d>: configured gid %d", getpid (), gid);
1928 else if (unformat (line_input, "segment-baseva 0x%lx",
1929 &vcl_cfg->segment_baseva))
1931 if (VPPCOM_DEBUG > 0)
1932 clib_warning ("VCL<%d>: configured segment_baseva 0x%lx",
1933 getpid (), vcl_cfg->segment_baseva);
1935 else if (unformat (line_input, "segment-size 0x%lx",
1936 &vcl_cfg->segment_size))
1938 if (VPPCOM_DEBUG > 0)
1939 clib_warning ("VCL<%d>: configured segment_size 0x%lx (%ld)",
1940 getpid (), vcl_cfg->segment_size,
1941 vcl_cfg->segment_size);
1943 else if (unformat (line_input, "segment-size %ld",
1944 &vcl_cfg->segment_size))
1946 if (VPPCOM_DEBUG > 0)
1947 clib_warning ("VCL<%d>: configured segment_size %ld (0x%lx)",
1948 getpid (), vcl_cfg->segment_size,
1949 vcl_cfg->segment_size);
1951 else if (unformat (line_input, "add-segment-size 0x%lx",
1952 &vcl_cfg->add_segment_size))
1954 if (VPPCOM_DEBUG > 0)
1956 ("VCL<%d>: configured add_segment_size 0x%lx (%ld)",
1957 getpid (), vcl_cfg->add_segment_size,
1958 vcl_cfg->add_segment_size);
1960 else if (unformat (line_input, "add-segment-size %ld",
1961 &vcl_cfg->add_segment_size))
1963 if (VPPCOM_DEBUG > 0)
1965 ("VCL<%d>: configured add_segment_size %ld (0x%lx)",
1966 getpid (), vcl_cfg->add_segment_size,
1967 vcl_cfg->add_segment_size);
1969 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1970 &vcl_cfg->preallocated_fifo_pairs))
1972 if (VPPCOM_DEBUG > 0)
1973 clib_warning ("VCL<%d>: configured preallocated_fifo_pairs "
1974 "%d (0x%x)", getpid (),
1975 vcl_cfg->preallocated_fifo_pairs,
1976 vcl_cfg->preallocated_fifo_pairs);
1978 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1979 &vcl_cfg->rx_fifo_size))
1981 if (VPPCOM_DEBUG > 0)
1982 clib_warning ("VCL<%d>: configured rx_fifo_size 0x%lx (%ld)",
1983 getpid (), vcl_cfg->rx_fifo_size,
1984 vcl_cfg->rx_fifo_size);
1986 else if (unformat (line_input, "rx-fifo-size %ld",
1987 &vcl_cfg->rx_fifo_size))
1989 if (VPPCOM_DEBUG > 0)
1990 clib_warning ("VCL<%d>: configured rx_fifo_size %ld (0x%lx)",
1991 getpid (), vcl_cfg->rx_fifo_size,
1992 vcl_cfg->rx_fifo_size);
1994 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1995 &vcl_cfg->tx_fifo_size))
1997 if (VPPCOM_DEBUG > 0)
1998 clib_warning ("VCL<%d>: configured tx_fifo_size 0x%lx (%ld)",
1999 getpid (), vcl_cfg->tx_fifo_size,
2000 vcl_cfg->tx_fifo_size);
2002 else if (unformat (line_input, "tx-fifo-size %ld",
2003 &vcl_cfg->tx_fifo_size))
2005 if (VPPCOM_DEBUG > 0)
2006 clib_warning ("VCL<%d>: configured tx_fifo_size %ld (0x%lx)",
2007 getpid (), vcl_cfg->tx_fifo_size,
2008 vcl_cfg->tx_fifo_size);
2010 else if (unformat (line_input, "event-queue-size 0x%lx",
2011 &vcl_cfg->event_queue_size))
2013 if (VPPCOM_DEBUG > 0)
2014 clib_warning ("VCL<%d>: configured event_queue_size "
2016 getpid (), vcl_cfg->event_queue_size,
2017 vcl_cfg->event_queue_size);
2019 else if (unformat (line_input, "event-queue-size %ld",
2020 &vcl_cfg->event_queue_size))
2022 if (VPPCOM_DEBUG > 0)
2023 clib_warning ("VCL<%d>: configured event_queue_size "
2025 getpid (), vcl_cfg->event_queue_size,
2026 vcl_cfg->event_queue_size);
2028 else if (unformat (line_input, "listen-queue-size 0x%lx",
2029 &vcl_cfg->listen_queue_size))
2031 if (VPPCOM_DEBUG > 0)
2032 clib_warning ("VCL<%d>: configured listen_queue_size "
2034 getpid (), vcl_cfg->listen_queue_size,
2035 vcl_cfg->listen_queue_size);
2037 else if (unformat (line_input, "listen-queue-size %ld",
2038 &vcl_cfg->listen_queue_size))
2040 if (VPPCOM_DEBUG > 0)
2041 clib_warning ("VCL<%d>: configured listen_queue_size "
2043 getpid (), vcl_cfg->listen_queue_size,
2044 vcl_cfg->listen_queue_size);
2046 else if (unformat (line_input, "app-timeout %f",
2047 &vcl_cfg->app_timeout))
2049 if (VPPCOM_DEBUG > 0)
2050 clib_warning ("VCL<%d>: configured app_timeout %f",
2051 getpid (), vcl_cfg->app_timeout);
2053 else if (unformat (line_input, "session-timeout %f",
2054 &vcl_cfg->session_timeout))
2056 if (VPPCOM_DEBUG > 0)
2057 clib_warning ("VCL<%d>: configured session_timeout %f",
2058 getpid (), vcl_cfg->session_timeout);
2060 else if (unformat (line_input, "accept-timeout %f",
2061 &vcl_cfg->accept_timeout))
2063 if (VPPCOM_DEBUG > 0)
2064 clib_warning ("VCL<%d>: configured accept_timeout %f",
2065 getpid (), vcl_cfg->accept_timeout);
2067 else if (unformat (line_input, "app-proxy-transport-tcp"))
2069 vcl_cfg->app_proxy_transport_tcp = 1;
2070 if (VPPCOM_DEBUG > 0)
2071 clib_warning ("VCL<%d>: configured "
2072 "app_proxy_transport_tcp (%d)",
2073 getpid (), vcl_cfg->app_proxy_transport_tcp);
2075 else if (unformat (line_input, "app-proxy-transport-udp"))
2077 vcl_cfg->app_proxy_transport_udp = 1;
2078 if (VPPCOM_DEBUG > 0)
2079 clib_warning ("VCL<%d>: configured "
2080 "app_proxy_transport_udp (%d)",
2081 getpid (), vcl_cfg->app_proxy_transport_udp);
2083 else if (unformat (line_input, "app-scope-local"))
2085 vcl_cfg->app_scope_local = 1;
2086 if (VPPCOM_DEBUG > 0)
2087 clib_warning ("VCL<%d>: configured app_scope_local (%d)",
2088 getpid (), vcl_cfg->app_scope_local);
2090 else if (unformat (line_input, "app-scope-global"))
2092 vcl_cfg->app_scope_global = 1;
2093 if (VPPCOM_DEBUG > 0)
2094 clib_warning ("VCL<%d>: configured app_scope_global (%d)",
2095 getpid (), vcl_cfg->app_scope_global);
2097 else if (unformat (line_input, "namespace-secret %lu",
2098 &vcl_cfg->namespace_secret))
2100 if (VPPCOM_DEBUG > 0)
2102 ("VCL<%d>: configured namespace_secret %lu (0x%lx)",
2103 getpid (), vcl_cfg->namespace_secret,
2104 vcl_cfg->namespace_secret);
2106 else if (unformat (line_input, "namespace-id %v",
2107 &vcl_cfg->namespace_id))
2109 vl_api_application_attach_t *mp;
2110 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
2111 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
2112 if (nsid_vec_len > max_nsid_vec_len)
2114 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
2115 if (VPPCOM_DEBUG > 0)
2116 clib_warning ("VCL<%d>: configured namespace_id is "
2117 "too long, truncated to %d characters!",
2118 getpid (), max_nsid_vec_len);
2121 if (VPPCOM_DEBUG > 0)
2122 clib_warning ("VCL<%d>: configured namespace_id %v",
2123 getpid (), vcl_cfg->namespace_id);
2125 else if (unformat (line_input, "}"))
2128 if (VPPCOM_DEBUG > 0)
2129 clib_warning ("VCL<%d>: completed parsing vppcom config!",
2135 if (line_input->buffer[line_input->index] != '#')
2137 clib_warning ("VCL<%d>: Unknown vppcom config option: '%s'",
2139 &line_input->buffer[line_input->index]);
2146 unformat_free (input);
2154 * VPPCOM Public API functions
2157 vppcom_app_create (char *app_name)
2159 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
2170 vppcom_cfg_init (vcl_cfg);
2171 env_var_str = getenv (VPPCOM_ENV_DEBUG);
2175 if (sscanf (env_var_str, "%u", &tmp) != 1)
2176 clib_warning ("VCL<%d>: WARNING: Invalid debug level specified "
2177 "in the environment variable " VPPCOM_ENV_DEBUG
2178 " (%s)!\n", getpid (), env_var_str);
2182 if (VPPCOM_DEBUG > 0)
2183 clib_warning ("VCL<%d>: configured VCL debug level (%u) from "
2184 VPPCOM_ENV_DEBUG "!", getpid (), vcm->debug);
2187 conf_fname = getenv (VPPCOM_ENV_CONF);
2189 conf_fname = VPPCOM_CONF_DEFAULT;
2190 vppcom_cfg_heapsize (conf_fname);
2191 vcl_cfg = &vcm->cfg;
2192 clib_spinlock_init (&vcm->session_fifo_lockp);
2193 clib_fifo_validate (vcm->client_session_index_fifo,
2194 vcm->cfg.listen_queue_size);
2195 vppcom_cfg_read (conf_fname);
2197 env_var_str = getenv (VPPCOM_ENV_API_PREFIX);
2200 if (vcl_cfg->vpp_api_filename)
2201 vec_free (vcl_cfg->vpp_api_filename);
2202 vcl_cfg->vpp_api_filename = format (0, "/%s-vpe-api%c",
2204 vl_set_memory_root_path ((char *) env_var_str);
2206 if (VPPCOM_DEBUG > 0)
2207 clib_warning ("VCL<%d>: configured api prefix (%s) and "
2208 "filename (%s) from " VPPCOM_ENV_API_PREFIX "!",
2209 getpid (), env_var_str, vcl_cfg->vpp_api_filename);
2211 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
2214 u32 ns_id_vec_len = strlen (env_var_str);
2216 vec_reset_length (vcm->cfg.namespace_id);
2217 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
2218 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
2220 if (VPPCOM_DEBUG > 0)
2221 clib_warning ("VCL<%d>: configured namespace_id (%v) from "
2222 VPPCOM_ENV_APP_NAMESPACE_ID
2223 "!", getpid (), vcm->cfg.namespace_id);
2225 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
2229 if (sscanf (env_var_str, "%lu", &tmp) != 1)
2230 clib_warning ("VCL<%d>: WARNING: Invalid namespace secret "
2231 "specified in the environment variable "
2232 VPPCOM_ENV_APP_NAMESPACE_SECRET
2233 " (%s)!\n", getpid (), env_var_str);
2236 vcm->cfg.namespace_secret = tmp;
2237 if (VPPCOM_DEBUG > 0)
2238 clib_warning ("VCL<%d>: configured namespace secret "
2240 VPPCOM_ENV_APP_NAMESPACE_SECRET
2241 "!", getpid (), vcm->cfg.namespace_secret);
2244 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP))
2246 vcm->cfg.app_proxy_transport_tcp = 1;
2247 if (VPPCOM_DEBUG > 0)
2248 clib_warning ("VCL<%d>: configured app_proxy_transport_tcp "
2250 VPPCOM_ENV_APP_PROXY_TRANSPORT_TCP
2251 "!", getpid (), vcm->cfg.app_proxy_transport_tcp);
2253 if (getenv (VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP))
2255 vcm->cfg.app_proxy_transport_udp = 1;
2256 if (VPPCOM_DEBUG > 0)
2257 clib_warning ("VCL<%d>: configured app_proxy_transport_udp "
2259 VPPCOM_ENV_APP_PROXY_TRANSPORT_UDP
2260 "!", getpid (), vcm->cfg.app_proxy_transport_udp);
2262 if (getenv (VPPCOM_ENV_APP_SCOPE_LOCAL))
2264 vcm->cfg.app_scope_local = 1;
2265 if (VPPCOM_DEBUG > 0)
2266 clib_warning ("VCL<%d>: configured app_scope_local (%u) from "
2267 VPPCOM_ENV_APP_SCOPE_LOCAL
2268 "!", getpid (), vcm->cfg.app_scope_local);
2270 if (getenv (VPPCOM_ENV_APP_SCOPE_GLOBAL))
2272 vcm->cfg.app_scope_global = 1;
2273 if (VPPCOM_DEBUG > 0)
2274 clib_warning ("VCL<%d>: configured app_scope_global (%u) from "
2275 VPPCOM_ENV_APP_SCOPE_GLOBAL
2276 "!", getpid (), vcm->cfg.app_scope_global);
2279 vcm->main_cpu = os_get_thread_index ();
2280 heap = clib_mem_get_per_cpu_heap ();
2281 h = mheap_header (heap);
2283 /* make the main heap thread-safe */
2284 h->flags |= MHEAP_FLAG_THREAD_SAFE;
2286 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
2288 clib_time_init (&vcm->clib_time);
2289 vppcom_init_error_string_table ();
2290 svm_fifo_segment_main_init (vcl_cfg->segment_baseva,
2291 20 /* timeout in secs */ );
2292 clib_spinlock_init (&vcm->sessions_lockp);
2295 if (vcm->my_client_index == ~0)
2298 /* API hookup and connect to VPP */
2299 vppcom_api_hookup ();
2300 vcm->app_state = STATE_APP_START;
2301 rv = vppcom_connect_to_vpp (app_name);
2304 clib_warning ("VCL<%d>: ERROR: couldn't connect to VPP!",
2309 /* State event handling thread */
2311 rv = vce_start_event_thread (&(vcm->event_thread), 20);
2314 if (VPPCOM_DEBUG > 0)
2315 clib_warning ("VCL<%d>: sending session enable", getpid ());
2317 rv = vppcom_app_session_enable ();
2320 clib_warning ("VCL<%d>: ERROR: vppcom_app_session_enable() "
2321 "failed!", getpid ());
2325 if (VPPCOM_DEBUG > 0)
2326 clib_warning ("VCL<%d>: sending app attach", getpid ());
2328 rv = vppcom_app_attach ();
2331 clib_warning ("VCL<%d>: ERROR: vppcom_app_attach() failed!",
2336 if (VPPCOM_DEBUG > 0)
2337 clib_warning ("VCL<%d>: app_name '%s', my_client_index %d (0x%x)",
2338 getpid (), app_name, vcm->my_client_index,
2339 vcm->my_client_index);
2346 vppcom_app_destroy (void)
2349 f64 orig_app_timeout;
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 orig_app_timeout = vcm->cfg.app_timeout;
2378 vcm->cfg.app_timeout = 2.0;
2379 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
2380 vcm->cfg.app_timeout = orig_app_timeout;
2381 if (PREDICT_FALSE (rv))
2383 if (VPPCOM_DEBUG > 0)
2384 clib_warning ("VCL<%d>: application detach timed out! "
2385 "returning %d (%s)",
2386 getpid (), rv, vppcom_retval_str (rv));
2389 /* Finished with logging before client gets reset to ~0 */
2390 if (VPPCOM_DEBUG > 0)
2393 vl_client_disconnect_from_vlib ();
2394 vcm->my_client_index = ~0;
2395 vcm->app_state = STATE_APP_START;
2399 vppcom_session_create (u8 proto, u8 is_nonblocking)
2403 session_state_t state;
2404 elog_track_t session_elog_track;
2406 clib_spinlock_lock (&vcm->sessions_lockp);
2407 pool_get (vcm->sessions, session);
2408 memset (session, 0, sizeof (*session));
2409 session_index = session - vcm->sessions;
2411 session->proto = proto;
2412 session->state = STATE_START;
2413 state = session->state;
2414 session->vpp_handle = ~0;
2417 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
2419 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
2421 if (VPPCOM_DEBUG > 0)
2423 session->elog_track.name =
2424 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2426 elog_track_register (&vcm->elog_main, &session->elog_track);
2427 session_elog_track = session->elog_track;
2430 clib_spinlock_unlock (&vcm->sessions_lockp);
2432 if (VPPCOM_DEBUG > 0)
2433 clib_warning ("VCL<%d>: sid %u", getpid (), session_index);
2435 if (VPPCOM_DEBUG > 0)
2438 ELOG_TYPE_DECLARE (e) =
2440 .format = "session_create:proto:%d state:%d is_nonblocking:%d",
2441 .format_args = "i4i4i4",
2449 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
2450 ed->data[0] = proto;
2451 ed->data[1] = state;
2452 ed->data[2] = is_nonblocking;
2456 return (int) session_index;
2460 vppcom_session_close (uint32_t session_index)
2462 session_t *session = 0;
2470 session_state_t state;
2471 elog_track_t session_elog_track;
2473 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2474 is_vep = session->is_vep;
2475 is_vep_session = session->is_vep_session;
2476 next_sid = session->vep.next_sid;
2477 vep_idx = session->vep.vep_idx;
2478 state = session->state;
2479 vpp_handle = session->vpp_handle;
2480 clib_spinlock_unlock (&vcm->sessions_lockp);
2483 * Why two if(VPPCOM_DEBUG) checks?
2485 * Eventually all clib_warnings need their own way of being
2486 * logged and signalled (like severity) where event logging
2487 * is a separate debugging tool. It will make the separation
2488 * easier. ... parting is such sweet sorrow ...
2490 if (VPPCOM_DEBUG > 0)
2492 session_elog_track = session->elog_track;
2495 if (VPPCOM_DEBUG > 0)
2498 clib_warning ("VCL<%d>: vep_idx %u / sid %u: "
2499 "closing epoll session...",
2500 getpid (), session_index, session_index);
2502 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %d: "
2503 "closing session...",
2504 getpid (), vpp_handle, session_index);
2509 while (next_sid != ~0)
2511 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
2512 if ((VPPCOM_DEBUG > 0) && PREDICT_FALSE (rv < 0))
2513 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2514 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2515 getpid (), vpp_handle, next_sid, vep_idx,
2516 rv, vppcom_retval_str (rv));
2518 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2519 next_sid = session->vep.next_sid;
2520 clib_spinlock_unlock (&vcm->sessions_lockp);
2527 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2528 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2529 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2530 "EPOLL_CTL_DEL vep_idx %u failed! rv %d (%s)",
2531 getpid (), vpp_handle, session_index,
2532 vep_idx, rv, vppcom_retval_str (rv));
2535 if (state & STATE_LISTEN)
2537 rv = vppcom_session_unbind (session_index);
2538 if (PREDICT_FALSE (rv < 0))
2540 if (VPPCOM_DEBUG > 0)
2541 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2542 "listener unbind failed! rv %d (%s)",
2543 getpid (), vpp_handle, session_index,
2544 rv, vppcom_retval_str (rv));
2548 else if (state & (CLIENT_STATE_OPEN | SERVER_STATE_OPEN))
2550 rv = vppcom_session_disconnect (session_index);
2551 if (PREDICT_FALSE (rv < 0))
2552 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2553 "session disconnect failed! rv %d (%s)",
2554 getpid (), vpp_handle, session_index,
2555 rv, vppcom_retval_str (rv));
2559 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2560 vpp_handle = session->vpp_handle;
2561 if (vpp_handle != ~0)
2563 p = hash_get (vcm->session_index_by_vpp_handles, vpp_handle);
2565 hash_unset (vcm->session_index_by_vpp_handles, vpp_handle);
2567 pool_put_index (vcm->sessions, session_index);
2569 clib_spinlock_unlock (&vcm->sessions_lockp);
2571 if (VPPCOM_DEBUG > 0)
2574 clib_warning ("VCL<%d>: vep_idx %u / sid %u: epoll session removed.",
2575 getpid (), session_index, session_index);
2577 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session removed.",
2578 getpid (), vpp_handle, session_index);
2582 if (VPPCOM_DEBUG > 0)
2585 ELOG_TYPE_DECLARE (e) =
2587 .format = "session_close:rv:%d",
2588 .format_args = "i4",
2596 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
2605 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2607 session_t *session = 0;
2611 return VPPCOM_EINVAL;
2613 VCL_LOCK_AND_GET_SESSION (session_index, &session);
2615 if (session->is_vep)
2617 clib_spinlock_unlock (&vcm->sessions_lockp);
2618 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
2619 "bind to an epoll session!", getpid (), session_index);
2624 session->lcl_addr.is_ip4 = ep->is_ip4;
2625 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
2626 session->lcl_port = ep->port;
2628 if (VPPCOM_DEBUG > 0)
2629 clib_warning ("VCL<%d>: sid %u: binding to local %s address %U "
2630 "port %u, proto %s", getpid (), session_index,
2631 session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2632 format_ip46_address, &session->lcl_addr.ip46,
2633 session->lcl_addr.is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
2634 clib_net_to_host_u16 (session->lcl_port),
2635 session->proto ? "UDP" : "TCP");
2637 if (VPPCOM_DEBUG > 0)
2639 if (session->lcl_addr.is_ip4)
2642 ELOG_TYPE_DECLARE (e) =
2644 .format = "bind local:%s:%d.%d.%d.%d:%d ",
2645 .format_args = "t1i1i1i1i1i2",
2646 .n_enum_strings = 2,
2647 .enum_strings = {"TCP", "UDP",},
2650 CLIB_PACKED (struct {
2656 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
2657 ed->proto = session->proto;
2658 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
2659 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
2660 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
2661 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
2662 ed->port = clib_net_to_host_u16 (session->lcl_port);
2667 clib_spinlock_unlock (&vcm->sessions_lockp);
2673 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2675 session_t *listen_session = 0;
2676 u64 listen_vpp_handle;
2679 if (q_len == 0 || q_len == ~0)
2680 q_len = vcm->cfg.listen_queue_size;
2682 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2684 if (listen_session->is_vep)
2686 clib_spinlock_unlock (&vcm->sessions_lockp);
2687 clib_warning ("VCL<%d>: ERROR: sid %u: cannot listen on an "
2688 "epoll session!", getpid (), listen_session_index);
2693 listen_vpp_handle = listen_session->vpp_handle;
2694 if (listen_session->state & STATE_LISTEN)
2696 clib_spinlock_unlock (&vcm->sessions_lockp);
2697 if (VPPCOM_DEBUG > 0)
2698 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
2699 "already in listen state!",
2700 getpid (), listen_vpp_handle, listen_session_index);
2705 if (VPPCOM_DEBUG > 0)
2706 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
2707 "sid %u: sending bind request...",
2708 getpid (), listen_vpp_handle, listen_session_index);
2710 vppcom_send_bind_sock (listen_session, listen_session_index);
2711 clib_spinlock_unlock (&vcm->sessions_lockp);
2713 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2714 vcm->cfg.session_timeout);
2716 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2717 if (PREDICT_FALSE (retval))
2719 if (VPPCOM_DEBUG > 0)
2720 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: bind failed! "
2721 "returning %d (%s)", getpid (),
2722 listen_session->vpp_handle, listen_session_index,
2723 retval, vppcom_retval_str (retval));
2724 clib_spinlock_unlock (&vcm->sessions_lockp);
2729 clib_spinlock_lock (&vcm->session_fifo_lockp);
2730 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2731 clib_spinlock_unlock (&vcm->session_fifo_lockp);
2733 clib_spinlock_unlock (&vcm->sessions_lockp);
2740 vppcom_session_register_listener (uint32_t session_index,
2741 vppcom_session_listener_cb cb,
2742 vppcom_session_listener_errcb
2743 errcb, uint8_t flags, int q_len, void *ptr)
2746 vce_event_key_t evk;
2747 vppcom_session_listener_t *listener_args;
2749 rv = vppcom_session_listen (session_index, q_len);
2756 /* Register handler for connect_request event on listen_session_index */
2757 listener_args = clib_mem_alloc (sizeof (vppcom_session_listener_t));
2758 listener_args->user_cb = cb;
2759 listener_args->user_cb_data = ptr;
2760 listener_args->user_errcb = errcb;
2762 evk.session_index = session_index;
2763 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
2764 (void) vce_register_handler (&vcm->event_thread, &evk,
2765 vce_registered_listener_connect_handler_fn,
2773 validate_args_session_accept_ (session_t * listen_session)
2775 u32 listen_session_index = listen_session - vcm->sessions;
2777 /* Input validation - expects spinlock on sessions_lockp */
2778 if (listen_session->is_vep)
2780 clib_warning ("VCL<%d>: ERROR: sid %u: cannot accept on an "
2781 "epoll session!", getpid (), listen_session_index);
2782 return VPPCOM_EBADFD;
2785 if (listen_session->state != STATE_LISTEN)
2787 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
2788 "not in listen state! state 0x%x (%s)", getpid (),
2789 listen_session->vpp_handle, listen_session_index,
2790 listen_session->state,
2791 vppcom_session_state_str (listen_session->state));
2792 return VPPCOM_EBADFD;
2798 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2801 session_t *listen_session = 0;
2802 session_t *client_session = 0;
2803 u32 client_session_index = ~0;
2805 u64 listen_vpp_handle;
2806 vce_event_handler_reg_t *reg;
2808 vce_event_connect_request_t *result;
2811 int millisecond_timeout = 1;
2812 int hours_timeout = 20 * 60 * 60;
2814 VCL_LOCK_AND_GET_SESSION (listen_session_index, &listen_session);
2815 listen_vpp_handle = listen_session->vpp_handle; // For debugging
2817 rv = validate_args_session_accept_ (listen_session);
2820 clib_spinlock_unlock (&vcm->sessions_lockp);
2824 /* Using an aggressive timer of 1ms and a generous timer of
2825 * 20 hours, we can implement a blocking and non-blocking listener
2826 * as both event and time driven */
2827 gettimeofday (&tv, NULL);
2828 ts.tv_nsec = (tv.tv_usec * 1000) + (1000 * millisecond_timeout);
2829 ts.tv_sec = tv.tv_sec;
2831 /* Predict that the Listener is blocking more often than not */
2832 if (PREDICT_TRUE (!VCL_SESS_ATTR_TEST (listen_session->attr,
2833 VCL_SESS_ATTR_NONBLOCK)))
2834 ts.tv_sec += hours_timeout;
2836 clib_spinlock_unlock (&vcm->sessions_lockp);
2838 /* Register handler for connect_request event on listen_session_index */
2839 vce_event_key_t evk;
2840 evk.session_index = listen_session_index;
2841 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
2842 reg = vce_register_handler (&vcm->event_thread, &evk,
2843 vce_connect_request_handler_fn, 0);
2844 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
2845 pthread_mutex_lock (®->handler_lock);
2849 pthread_cond_timedwait (®->handler_cond, ®->handler_lock, &ts);
2850 if (rv == ETIMEDOUT)
2855 ev = vce_get_event_from_index (&vcm->event_thread, reg->ev_idx);
2857 result = (vce_event_connect_request_t *) ev->data;
2858 client_session_index = result->accepted_session_index;
2862 /* Remove from the FIFO used to service epoll */
2863 clib_spinlock_lock (&vcm->session_fifo_lockp);
2864 if (clib_fifo_elts (vcm->client_session_index_fifo))
2866 u32 tmp_client_session_index;
2867 clib_fifo_sub1 (vcm->client_session_index_fifo,
2868 tmp_client_session_index);
2869 /* It wasn't ours... put it back ... */
2870 if (tmp_client_session_index != client_session_index)
2871 clib_fifo_add1 (vcm->client_session_index_fifo,
2872 tmp_client_session_index);
2874 clib_spinlock_unlock (&vcm->session_fifo_lockp);
2876 clib_spinlock_lock (&vcm->sessions_lockp);
2878 rv = vppcom_session_at_index (client_session_index, &client_session);
2879 if (PREDICT_FALSE (rv))
2881 rv = VPPCOM_ECONNABORTED;
2882 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: client sid %u "
2883 "lookup failed! returning %d (%s)", getpid (),
2884 listen_vpp_handle, listen_session_index,
2885 client_session_index, rv, vppcom_retval_str (rv));
2889 if (flags & O_NONBLOCK)
2890 VCL_SESS_ATTR_SET (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2892 VCL_SESS_ATTR_CLR (client_session->attr, VCL_SESS_ATTR_NONBLOCK);
2894 if (VPPCOM_DEBUG > 0)
2895 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Got a client request! "
2896 "vpp handle 0x%llx, sid %u, flags %d, is_nonblocking %u",
2897 getpid (), listen_vpp_handle, listen_session_index,
2898 client_session->vpp_handle, client_session_index,
2899 flags, VCL_SESS_ATTR_TEST (client_session->attr,
2900 VCL_SESS_ATTR_NONBLOCK));
2904 ep->is_ip4 = client_session->peer_addr.is_ip4;
2905 ep->port = client_session->peer_port;
2906 if (client_session->peer_addr.is_ip4)
2907 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2908 sizeof (ip4_address_t));
2910 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2911 sizeof (ip6_address_t));
2914 vppcom_send_accept_session_reply (client_session->vpp_handle,
2915 client_session->client_context,
2916 0 /* retval OK */ );
2918 if (VPPCOM_DEBUG > 0)
2919 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: accepted vpp handle "
2920 "0x%llx, sid %u connection to local %s address "
2921 "%U port %u", getpid (), listen_vpp_handle,
2922 listen_session_index, client_session->vpp_handle,
2923 client_session_index,
2924 client_session->lcl_addr.is_ip4 ? "IPv4" : "IPv6",
2925 format_ip46_address, &client_session->lcl_addr.ip46,
2926 client_session->lcl_addr.is_ip4 ?
2927 IP46_TYPE_IP4 : IP46_TYPE_IP6,
2928 clib_net_to_host_u16 (client_session->lcl_port));
2930 if (VPPCOM_DEBUG > 0)
2932 client_session->elog_track.name =
2933 (char *) format (0, "C:%d:S:%d%c", vcm->my_client_index,
2934 client_session_index, 0);
2935 elog_track_register (&vcm->elog_main, &client_session->elog_track);
2937 // Two elog entries due to 20-byte per entry constraint.
2939 ELOG_TYPE_DECLARE (e) =
2941 .format = "accept: listen_handle:%x from_handle:%x",
2942 .format_args = "i8i8",
2950 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, client_session->elog_track);
2951 ed->handle[0] = listen_vpp_handle;
2952 ed->handle[1] = client_session->vpp_handle;
2955 if (client_session->lcl_addr.is_ip4)
2958 ELOG_TYPE_DECLARE (e2) =
2960 .format = "accept: S:%d %d.%d.%d.%d:%d ",
2961 .format_args = "i4i1i1i1i1i2",
2964 CLIB_PACKED (struct {
2971 ELOG_TRACK_DATA (&vcm->elog_main, e2, client_session->elog_track);
2972 ed2->session = client_session_index;
2973 ed2->addr[0] = client_session->lcl_addr.ip46.ip4.as_u8[0];
2974 ed2->addr[1] = client_session->lcl_addr.ip46.ip4.as_u8[1];
2975 ed2->addr[2] = client_session->lcl_addr.ip46.ip4.as_u8[2];
2976 ed2->addr[3] = client_session->lcl_addr.ip46.ip4.as_u8[3];
2977 ed2->port = clib_net_to_host_u16 (client_session->lcl_port);
2982 clib_spinlock_unlock (&vcm->sessions_lockp);
2984 rv = (int) client_session_index;
2985 vce_clear_event (&vcm->event_thread, ev);
2988 vce_unregister_handler (&vcm->event_thread, reg);
2989 pthread_mutex_unlock (®->handler_lock);
2996 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2998 session_t *session = 0;
3000 int rv, retval = VPPCOM_OK;
3002 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3004 if (PREDICT_FALSE (session->is_vep))
3006 clib_spinlock_unlock (&vcm->sessions_lockp);
3007 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
3008 "connect on an epoll session!", getpid (), session_index);
3013 if (PREDICT_FALSE (session->state & CLIENT_STATE_OPEN))
3015 if (VPPCOM_DEBUG > 0)
3016 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session already "
3017 "connected to %s %U port %d proto %s, state 0x%x (%s)",
3018 getpid (), session->vpp_handle, session_index,
3019 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
3020 format_ip46_address,
3021 &session->peer_addr.ip46, session->peer_addr.is_ip4 ?
3022 IP46_TYPE_IP4 : IP46_TYPE_IP6,
3023 clib_net_to_host_u16 (session->peer_port),
3024 session->proto ? "UDP" : "TCP", session->state,
3025 vppcom_session_state_str (session->state));
3027 clib_spinlock_unlock (&vcm->sessions_lockp);
3031 session->peer_addr.is_ip4 = server_ep->is_ip4;
3032 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
3033 session->peer_port = server_ep->port;
3035 if (VPPCOM_DEBUG > 0)
3036 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connecting to server "
3037 "%s %U port %d proto %s",
3038 getpid (), session->vpp_handle, session_index,
3039 session->peer_addr.is_ip4 ? "IPv4" : "IPv6",
3040 format_ip46_address,
3041 &session->peer_addr.ip46, session->peer_addr.is_ip4 ?
3042 IP46_TYPE_IP4 : IP46_TYPE_IP6,
3043 clib_net_to_host_u16 (session->peer_port),
3044 session->proto ? "UDP" : "TCP");
3046 vppcom_send_connect_sock (session, session_index);
3047 clib_spinlock_unlock (&vcm->sessions_lockp);
3050 vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
3051 vcm->cfg.session_timeout);
3053 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3054 vpp_handle = session->vpp_handle;
3055 clib_spinlock_unlock (&vcm->sessions_lockp);
3058 if (PREDICT_FALSE (retval))
3061 if (VPPCOM_DEBUG > 0)
3065 ("VCL<%d>: vpp handle 0x%llx, sid %u: connect failed! "
3066 "returning %d (%s)", getpid (), vpp_handle,
3067 session_index, rv, vppcom_retval_str (rv));
3069 clib_warning ("VCL<%d>: no session for sid %u: connect failed! "
3070 "returning %d (%s)", getpid (),
3071 session_index, rv, vppcom_retval_str (rv));
3074 else if (VPPCOM_DEBUG > 0)
3075 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: connected!",
3076 getpid (), vpp_handle, session_index);
3082 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
3085 session_t *session = 0;
3086 svm_fifo_t *rx_fifo;
3093 session_state_t state;
3097 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3099 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
3100 rx_fifo = session->rx_fifo;
3101 state = session->state;
3102 vpp_handle = session->vpp_handle;
3104 if (PREDICT_FALSE (session->is_vep))
3106 clib_spinlock_unlock (&vcm->sessions_lockp);
3107 clib_warning ("VCL<%d>: ERROR: sid %u: cannot "
3108 "read from an epoll session!", getpid (), session_index);
3113 if (PREDICT_FALSE (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN))))
3115 clib_spinlock_unlock (&vcm->sessions_lockp);
3116 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3118 if (VPPCOM_DEBUG > 0)
3119 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: %s session is "
3120 "not open! state 0x%x (%s), returning %d (%s)",
3121 getpid (), vpp_handle, session_index, state,
3122 vppcom_session_state_str (state),
3123 rv, vppcom_retval_str (rv));
3127 clib_spinlock_unlock (&vcm->sessions_lockp);
3132 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
3134 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
3136 while (!is_nonblocking && (n_read <= 0));
3140 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3142 poll_et = (((EPOLLET | EPOLLIN) & session->vep.ev.events) ==
3143 (EPOLLET | EPOLLIN));
3145 session->vep.et_mask |= EPOLLIN;
3147 if (state & STATE_CLOSE_ON_EMPTY)
3149 rv = VPPCOM_ECONNRESET;
3151 if (VPPCOM_DEBUG > 1)
3153 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: Empty fifo "
3154 "with session state 0x%x (%s)!"
3155 " Setting state to 0x%x (%s), returning %d (%s)",
3156 getpid (), session->vpp_handle, session_index,
3157 state, vppcom_session_state_str (state),
3159 vppcom_session_state_str (STATE_DISCONNECT), rv,
3160 vppcom_retval_str (rv));
3163 session->state = STATE_DISCONNECT;
3168 clib_spinlock_unlock (&vcm->sessions_lockp);
3173 if (VPPCOM_DEBUG > 2)
3176 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: read %d bytes "
3177 "from (%p)", getpid (), vpp_handle,
3178 session_index, n_read, rx_fifo);
3180 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: nothing read! "
3181 "returning %d (%s)", getpid (), vpp_handle,
3182 session_index, rv, vppcom_retval_str (rv));
3189 vppcom_session_read (uint32_t session_index, void *buf, size_t n)
3191 return (vppcom_session_read_internal (session_index, buf, n, 0));
3195 vppcom_session_peek (uint32_t session_index, void *buf, int n)
3197 return (vppcom_session_read_internal (session_index, buf, n, 1));
3201 vppcom_session_read_ready (session_t * session, u32 session_index)
3206 session_state_t state = session->state;
3207 u64 vpp_handle = session->vpp_handle;
3209 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3210 if (PREDICT_FALSE (session->is_vep))
3212 clib_warning ("VCL<%d>: ERROR: sid %u: cannot read from an "
3213 "epoll session!", getpid (), session_index);
3218 if (session->state & STATE_LISTEN)
3220 clib_spinlock_lock (&vcm->session_fifo_lockp);
3221 ready = clib_fifo_elts (vcm->client_session_index_fifo);
3222 clib_spinlock_unlock (&vcm->session_fifo_lockp);
3226 if (!(state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN | STATE_LISTEN)))
3228 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3231 if (VPPCOM_DEBUG > 1)
3232 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: session is "
3233 "not open! state 0x%x (%s), returning %d (%s)",
3234 getpid (), vpp_handle, session_index,
3235 state, vppcom_session_state_str (state),
3236 rv, vppcom_retval_str (rv));
3240 ready = svm_fifo_max_dequeue (session->rx_fifo);
3246 ((EPOLLET | EPOLLIN) & session->vep.ev.events) == (EPOLLET | EPOLLIN);
3248 session->vep.et_mask |= EPOLLIN;
3250 if (state & STATE_CLOSE_ON_EMPTY)
3252 rv = VPPCOM_ECONNRESET;
3254 if (VPPCOM_DEBUG > 1)
3256 clib_warning ("VCL<%d>: vpp handle 0x%llx, "
3257 "sid %u: Empty fifo with"
3258 " session state 0x%x (%s)! Setting state to "
3259 "0x%x (%s), returning %d (%s)",
3260 getpid (), session_index, vpp_handle,
3261 state, vppcom_session_state_str (state),
3263 vppcom_session_state_str (STATE_DISCONNECT), rv,
3264 vppcom_retval_str (rv));
3266 session->state = STATE_DISCONNECT;
3272 if (vcm->app_event_queue->cursize &&
3273 !pthread_mutex_trylock (&vcm->app_event_queue->mutex))
3275 u32 i, n_to_dequeue = vcm->app_event_queue->cursize;
3276 session_fifo_event_t e;
3278 for (i = 0; i < n_to_dequeue; i++)
3279 svm_queue_sub_raw (vcm->app_event_queue, (u8 *) & e);
3281 pthread_mutex_unlock (&vcm->app_event_queue->mutex);
3288 vppcom_session_write (uint32_t session_index, void *buf, size_t n)
3290 session_t *session = 0;
3291 svm_fifo_t *tx_fifo = 0;
3293 session_fifo_event_t evt;
3294 session_state_t state;
3295 int rv, n_write, is_nonblocking;
3301 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3303 tx_fifo = session->tx_fifo;
3304 is_nonblocking = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
3305 vpp_handle = session->vpp_handle;
3306 state = session->state;
3308 if (PREDICT_FALSE (session->is_vep))
3310 clib_spinlock_unlock (&vcm->sessions_lockp);
3311 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3312 "cannot write to an epoll session!",
3313 getpid (), vpp_handle, session_index);
3319 if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3322 ((session->state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET :
3325 clib_spinlock_unlock (&vcm->sessions_lockp);
3326 if (VPPCOM_DEBUG > 1)
3327 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3328 "session is not open! state 0x%x (%s)",
3329 getpid (), vpp_handle, session_index,
3330 state, vppcom_session_state_str (state));
3334 clib_spinlock_unlock (&vcm->sessions_lockp);
3338 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, (void *) buf);
3340 while (!is_nonblocking && (n_write <= 0));
3342 /* If event wasn't set, add one */
3343 if ((n_write > 0) && svm_fifo_set_event (tx_fifo))
3345 /* Fabricate TX event, send to vpp */
3347 evt.event_type = FIFO_EVENT_APP_TX;
3349 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3350 q = session->vpp_event_queue;
3352 svm_queue_add (q, (u8 *) & evt, 0 /* do wait for mutex */ );
3353 clib_spinlock_unlock (&vcm->sessions_lockp);
3354 if (VPPCOM_DEBUG > 1)
3355 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3356 "added FIFO_EVENT_APP_TX to "
3357 "vpp_event_q %p, n_write %d", getpid (),
3358 vpp_handle, session_index, q, n_write);
3363 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3365 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3366 (EPOLLET | EPOLLOUT));
3368 session->vep.et_mask |= EPOLLOUT;
3370 if (session->state & STATE_CLOSE_ON_EMPTY)
3372 rv = VPPCOM_ECONNRESET;
3374 if (VPPCOM_DEBUG > 1)
3376 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3377 "Empty fifo with session state 0x%x (%s)!"
3378 " Setting state to 0x%x (%s), returning %d (%s)",
3379 getpid (), session->vpp_handle, session_index,
3381 vppcom_session_state_str (session->state),
3383 vppcom_session_state_str (STATE_DISCONNECT), rv,
3384 vppcom_retval_str (rv));
3387 session->state = STATE_DISCONNECT;
3392 clib_spinlock_unlock (&vcm->sessions_lockp);
3397 if (VPPCOM_DEBUG > 2)
3400 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3401 "FIFO-FULL (%p)", getpid (), vpp_handle,
3402 session_index, tx_fifo);
3404 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3405 "wrote %d bytes tx-fifo: (%p)", getpid (),
3406 vpp_handle, session_index, n_write, tx_fifo);
3413 vppcom_session_write_ready (session_t * session, u32 session_index)
3421 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3422 if (PREDICT_FALSE (session->is_vep))
3424 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3425 "cannot write to an epoll session!",
3426 getpid (), session->vpp_handle, session_index);
3431 if (PREDICT_FALSE (session->state & STATE_LISTEN))
3433 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3434 "cannot write to a listen session!",
3435 getpid (), session->vpp_handle, session_index);
3440 if (!(session->state & (SERVER_STATE_OPEN | CLIENT_STATE_OPEN)))
3442 session_state_t state = session->state;
3444 rv = ((state & STATE_DISCONNECT) ? VPPCOM_ECONNRESET : VPPCOM_ENOTCONN);
3446 clib_warning ("VCL<%d>: ERROR: vpp handle 0x%llx, sid %u: "
3447 "session is not open! state 0x%x (%s), "
3448 "returning %d (%s)", getpid (), session->vpp_handle,
3450 state, vppcom_session_state_str (state),
3451 rv, vppcom_retval_str (rv));
3455 ready = svm_fifo_max_enqueue (session->tx_fifo);
3457 if (VPPCOM_DEBUG > 3)
3458 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3459 "peek %s (%p), ready = %d", getpid (),
3460 session->vpp_handle, session_index,
3461 session->tx_fifo, ready);
3465 poll_et = (((EPOLLET | EPOLLOUT) & session->vep.ev.events) ==
3466 (EPOLLET | EPOLLOUT));
3468 session->vep.et_mask |= EPOLLOUT;
3470 if (session->state & STATE_CLOSE_ON_EMPTY)
3472 rv = VPPCOM_ECONNRESET;
3474 if (VPPCOM_DEBUG > 1)
3476 clib_warning ("VCL<%d>: vpp handle 0x%llx, sid %u: "
3477 "Empty fifo with session "
3478 "state 0x%x (%s)! Setting state to 0x%x (%s), "
3479 "returning %d (%s)", getpid (),
3480 session->vpp_handle, session_index,
3482 vppcom_session_state_str (session->state),
3484 vppcom_session_state_str (STATE_DISCONNECT), rv,
3485 vppcom_retval_str (rv));
3487 session->state = STATE_DISCONNECT;
3497 vppcom_select (unsigned long n_bits, unsigned long *read_map,
3498 unsigned long *write_map, unsigned long *except_map,
3499 double time_to_wait)
3502 session_t *session = 0;
3503 int rv, bits_set = 0;
3504 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
3505 u32 minbits = clib_max (n_bits, BITS (uword));
3507 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
3509 if (n_bits && read_map)
3511 clib_bitmap_validate (vcm->rd_bitmap, minbits);
3512 clib_memcpy (vcm->rd_bitmap, read_map,
3513 vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3514 memset (read_map, 0, vec_len (vcm->rd_bitmap) * sizeof (clib_bitmap_t));
3516 if (n_bits && write_map)
3518 clib_bitmap_validate (vcm->wr_bitmap, minbits);
3519 clib_memcpy (vcm->wr_bitmap, write_map,
3520 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3521 memset (write_map, 0,
3522 vec_len (vcm->wr_bitmap) * sizeof (clib_bitmap_t));
3524 if (n_bits && except_map)
3526 clib_bitmap_validate (vcm->ex_bitmap, minbits);
3527 clib_memcpy (vcm->ex_bitmap, except_map,
3528 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3529 memset (except_map, 0,
3530 vec_len (vcm->ex_bitmap) * sizeof (clib_bitmap_t));
3540 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
3542 clib_spinlock_lock (&vcm->sessions_lockp);
3543 rv = vppcom_session_at_index (session_index, &session);
3546 clib_spinlock_unlock (&vcm->sessions_lockp);
3547 if (VPPCOM_DEBUG > 1)
3548 clib_warning ("VCL<%d>: session %d specified in "
3549 "read_map is closed.", getpid (),
3551 bits_set = VPPCOM_EBADFD;
3554 if (session->state & STATE_LISTEN)
3556 vce_event_handler_reg_t *reg = 0;
3557 vce_event_key_t evk;
3559 /* Check if handler already registered for this
3561 * If not, register handler for connect_request event
3562 * on listen_session_index
3564 evk.session_index = session_index;
3565 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
3566 reg = vce_get_event_handler (&vcm->event_thread, &evk);
3568 reg = vce_register_handler (&vcm->event_thread, &evk,
3569 vce_poll_wait_connect_request_handler_fn,
3570 0 /* No callback args */);
3571 rv = vppcom_session_read_ready (session, session_index);
3574 vce_unregister_handler (&vcm->event_thread, reg);
3578 rv = vppcom_session_read_ready (session, session_index);
3579 clib_spinlock_unlock (&vcm->sessions_lockp);
3580 if (except_map && vcm->ex_bitmap &&
3581 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
3584 clib_bitmap_set_no_check (except_map, session_index, 1);
3589 clib_bitmap_set_no_check (read_map, session_index, 1);
3597 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
3599 clib_spinlock_lock (&vcm->sessions_lockp);
3600 rv = vppcom_session_at_index (session_index, &session);
3603 clib_spinlock_unlock (&vcm->sessions_lockp);
3604 if (VPPCOM_DEBUG > 0)
3605 clib_warning ("VCL<%d>: session %d specified in "
3606 "write_map is closed.", getpid (),
3608 bits_set = VPPCOM_EBADFD;
3612 rv = vppcom_session_write_ready (session, session_index);
3613 clib_spinlock_unlock (&vcm->sessions_lockp);
3614 if (write_map && (rv > 0))
3616 clib_bitmap_set_no_check (write_map, session_index, 1);
3624 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
3626 clib_spinlock_lock (&vcm->sessions_lockp);
3627 rv = vppcom_session_at_index (session_index, &session);
3630 clib_spinlock_unlock (&vcm->sessions_lockp);
3631 if (VPPCOM_DEBUG > 1)
3632 clib_warning ("VCL<%d>: session %d specified in "
3633 "except_map is closed.", getpid (),
3635 bits_set = VPPCOM_EBADFD;
3639 rv = vppcom_session_read_ready (session, session_index);
3640 clib_spinlock_unlock (&vcm->sessions_lockp);
3643 clib_bitmap_set_no_check (except_map, session_index, 1);
3651 while ((time_to_wait == -1) || (clib_time_now (&vcm->clib_time) < timeout));
3658 vep_verify_epoll_chain (u32 vep_idx)
3661 vppcom_epoll_t *vep;
3665 if (VPPCOM_DEBUG <= 1)
3668 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
3669 rv = vppcom_session_at_index (vep_idx, &session);
3670 if (PREDICT_FALSE (rv))
3672 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!",
3673 getpid (), vep_idx);
3676 if (PREDICT_FALSE (!session->is_vep))
3678 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3679 getpid (), vep_idx);
3682 vep = &session->vep;
3683 clib_warning ("VCL<%d>: vep_idx (%u): Dumping epoll chain\n"
3686 " is_vep_session = %u\n"
3687 " next_sid = 0x%x (%u)\n"
3688 " wait_cont_idx = 0x%x (%u)\n"
3689 "}\n", getpid (), vep_idx,
3690 session->is_vep, session->is_vep_session,
3691 vep->next_sid, vep->next_sid,
3692 session->wait_cont_idx, session->wait_cont_idx);
3694 for (sid = vep->next_sid; sid != ~0; sid = vep->next_sid)
3696 rv = vppcom_session_at_index (sid, &session);
3697 if (PREDICT_FALSE (rv))
3699 clib_warning ("VCL<%d>: ERROR: Invalid sid (%u)!", getpid (), sid);
3702 if (PREDICT_FALSE (session->is_vep))
3703 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
3704 getpid (), vep_idx);
3705 else if (PREDICT_FALSE (!session->is_vep_session))
3707 clib_warning ("VCL<%d>: ERROR: session (%u) "
3708 "is not a vep session!", getpid (), sid);
3711 vep = &session->vep;
3712 if (PREDICT_FALSE (vep->vep_idx != vep_idx))
3713 clib_warning ("VCL<%d>: ERROR: session (%u) vep_idx (%u) != "
3714 "vep_idx (%u)!", getpid (),
3715 sid, session->vep.vep_idx, vep_idx);
3716 if (session->is_vep_session)
3718 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
3720 " next_sid = 0x%x (%u)\n"
3721 " prev_sid = 0x%x (%u)\n"
3722 " vep_idx = 0x%x (%u)\n"
3723 " ev.events = 0x%x\n"
3724 " ev.data.u64 = 0x%llx\n"
3728 vep->next_sid, vep->next_sid,
3729 vep->prev_sid, vep->prev_sid,
3730 vep->vep_idx, vep->vep_idx,
3731 vep->ev.events, vep->ev.data.u64, vep->et_mask);
3736 clib_warning ("VCL<%d>: vep_idx (%u): Dump complete!\n",
3737 getpid (), vep_idx);
3741 vppcom_epoll_create (void)
3743 session_t *vep_session;
3745 elog_track_t vep_elog_track;
3747 clib_spinlock_lock (&vcm->sessions_lockp);
3748 pool_get (vcm->sessions, vep_session);
3749 memset (vep_session, 0, sizeof (*vep_session));
3750 vep_idx = vep_session - vcm->sessions;
3752 vep_session->is_vep = 1;
3753 vep_session->vep.vep_idx = ~0;
3754 vep_session->vep.next_sid = ~0;
3755 vep_session->vep.prev_sid = ~0;
3756 vep_session->wait_cont_idx = ~0;
3757 vep_session->vpp_handle = ~0;
3758 vep_session->poll_reg = 0;
3760 if (VPPCOM_DEBUG > 0)
3762 vep_session->elog_track.name =
3763 (char *) format (0, "C:%d:VEP:%d%c", vcm->my_client_index,
3765 elog_track_register (&vcm->elog_main, &vep_session->elog_track);
3766 vep_elog_track = vep_session->elog_track;
3769 clib_spinlock_unlock (&vcm->sessions_lockp);
3771 if (VPPCOM_DEBUG > 0)
3772 clib_warning ("VCL<%d>: Created vep_idx %u / sid %u!",
3773 getpid (), vep_idx, vep_idx);
3775 if (VPPCOM_DEBUG > 0)
3779 ELOG_TYPE_DECLARE (e) =
3781 .format = "created epoll session:%d",
3782 .format_args = "i4",
3790 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_elog_track);
3799 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
3800 struct epoll_event *event)
3802 session_t *vep_session;
3806 if (vep_idx == session_index)
3808 clib_warning ("VCL<%d>: ERROR: vep_idx == session_index (%u)!",
3809 getpid (), vep_idx);
3810 return VPPCOM_EINVAL;
3813 clib_spinlock_lock (&vcm->sessions_lockp);
3814 rv = vppcom_session_at_index (vep_idx, &vep_session);
3815 if (PREDICT_FALSE (rv))
3817 clib_warning ("VCL<%d>: ERROR: Invalid vep_idx (%u)!", vep_idx);
3820 if (PREDICT_FALSE (!vep_session->is_vep))
3822 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
3823 getpid (), vep_idx);
3828 ASSERT (vep_session->vep.vep_idx == ~0);
3829 ASSERT (vep_session->vep.prev_sid == ~0);
3831 rv = vppcom_session_at_index (session_index, &session);
3832 if (PREDICT_FALSE (rv))
3834 if (VPPCOM_DEBUG > 0)
3835 clib_warning ("VCL<%d>: ERROR: Invalid session_index (%u)!",
3836 getpid (), session_index);
3839 if (PREDICT_FALSE (session->is_vep))
3841 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
3849 if (PREDICT_FALSE (!event))
3851 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: NULL pointer to "
3852 "epoll_event structure!", getpid ());
3856 if (vep_session->vep.next_sid != ~0)
3858 session_t *next_session;
3859 rv = vppcom_session_at_index (vep_session->vep.next_sid,
3861 if (PREDICT_FALSE (rv))
3863 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_ADD: Invalid "
3864 "vep.next_sid (%u) on vep_idx (%u)!",
3865 getpid (), vep_session->vep.next_sid, vep_idx);
3868 ASSERT (next_session->vep.prev_sid == vep_idx);
3869 next_session->vep.prev_sid = session_index;
3871 session->vep.next_sid = vep_session->vep.next_sid;
3872 session->vep.prev_sid = vep_idx;
3873 session->vep.vep_idx = vep_idx;
3874 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3875 session->vep.ev = *event;
3876 session->is_vep = 0;
3877 session->is_vep_session = 1;
3878 vep_session->vep.next_sid = session_index;
3880 /* VCL Event Register handler */
3881 if (session->state & STATE_LISTEN)
3883 /* Register handler for connect_request event on listen_session_index */
3884 vce_event_key_t evk;
3885 evk.session_index = session_index;
3886 evk.eid = VCL_EVENT_CONNECT_REQ_ACCEPTED;
3887 vep_session->poll_reg =
3888 vce_register_handler (&vcm->event_thread, &evk,
3889 vce_poll_wait_connect_request_handler_fn,
3890 0 /* No callback args */ );
3892 if (VPPCOM_DEBUG > 1)
3893 clib_warning ("VCL<%d>: EPOLL_CTL_ADD: vep_idx %u, "
3894 "sid %u, events 0x%x, data 0x%llx!",
3895 getpid (), vep_idx, session_index,
3896 event->events, event->data.u64);
3897 if (VPPCOM_DEBUG > 0)
3900 ELOG_TYPE_DECLARE (e) =
3902 .format = "epoll_ctladd: events:%x data:%x",
3903 .format_args = "i4i4i8",
3911 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
3913 ed->events = event->events;
3914 ed->event_data = event->data.u64;
3920 if (PREDICT_FALSE (!event))
3922 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_MOD: NULL pointer to "
3923 "epoll_event structure!", getpid ());
3927 else if (PREDICT_FALSE (!session->is_vep_session))
3929 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3930 "not a vep session!", getpid (), session_index);
3934 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3936 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_MOD: "
3937 "vep_idx (%u) != vep_idx (%u)!",
3938 getpid (), session_index,
3939 session->vep.vep_idx, vep_idx);
3943 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
3944 session->vep.ev = *event;
3945 if (VPPCOM_DEBUG > 1)
3947 ("VCL<%d>: EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
3948 " data 0x%llx!", getpid (), vep_idx, session_index, event->events,
3953 if (PREDICT_FALSE (!session->is_vep_session))
3955 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3956 "not a vep session!", getpid (), session_index);
3960 else if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
3962 clib_warning ("VCL<%d>: ERROR: sid %u EPOLL_CTL_DEL: "
3963 "vep_idx (%u) != vep_idx (%u)!",
3964 getpid (), session_index,
3965 session->vep.vep_idx, vep_idx);
3970 /* VCL Event Un-register handler */
3971 if ((session->state & STATE_LISTEN) && vep_session->poll_reg)
3973 (void) vce_unregister_handler (&vcm->event_thread,
3974 vep_session->poll_reg);
3977 vep_session->wait_cont_idx =
3978 (vep_session->wait_cont_idx == session_index) ?
3979 session->vep.next_sid : vep_session->wait_cont_idx;
3981 if (session->vep.prev_sid == vep_idx)
3982 vep_session->vep.next_sid = session->vep.next_sid;
3985 session_t *prev_session;
3986 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3987 if (PREDICT_FALSE (rv))
3989 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
3990 "vep.prev_sid (%u) on sid (%u)!",
3991 getpid (), session->vep.prev_sid, session_index);
3994 ASSERT (prev_session->vep.next_sid == session_index);
3995 prev_session->vep.next_sid = session->vep.next_sid;
3997 if (session->vep.next_sid != ~0)
3999 session_t *next_session;
4000 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
4001 if (PREDICT_FALSE (rv))
4003 clib_warning ("VCL<%d>: ERROR: EPOLL_CTL_DEL: Invalid "
4004 "vep.next_sid (%u) on sid (%u)!",
4005 getpid (), session->vep.next_sid, session_index);
4008 ASSERT (next_session->vep.prev_sid == session_index);
4009 next_session->vep.prev_sid = session->vep.prev_sid;
4012 memset (&session->vep, 0, sizeof (session->vep));
4013 session->vep.next_sid = ~0;
4014 session->vep.prev_sid = ~0;
4015 session->vep.vep_idx = ~0;
4016 session->is_vep_session = 0;
4017 if (VPPCOM_DEBUG > 1)
4018 clib_warning ("VCL<%d>: EPOLL_CTL_DEL: vep_idx %u, sid %u!",
4019 getpid (), vep_idx, session_index);
4020 if (VPPCOM_DEBUG > 0)
4023 ELOG_TYPE_DECLARE (e) =
4025 .format = "epoll_ctldel: vep:%d",
4026 .format_args = "i4",
4033 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4041 clib_warning ("VCL<%d>: ERROR: Invalid operation (%d)!", getpid (), op);
4045 vep_verify_epoll_chain (vep_idx);
4048 clib_spinlock_unlock (&vcm->sessions_lockp);
4053 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
4054 int maxevents, double wait_for_time)
4056 session_t *vep_session;
4057 elog_track_t vep_elog_track;
4059 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
4060 u32 keep_trying = 1;
4062 u32 vep_next_sid, wait_cont_idx;
4065 if (PREDICT_FALSE (maxevents <= 0))
4067 clib_warning ("VCL<%d>: ERROR: Invalid maxevents (%d)!",
4068 getpid (), maxevents);
4069 return VPPCOM_EINVAL;
4071 memset (events, 0, sizeof (*events) * maxevents);
4073 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4074 vep_next_sid = vep_session->vep.next_sid;
4075 is_vep = vep_session->is_vep;
4076 wait_cont_idx = vep_session->wait_cont_idx;
4077 vep_elog_track = vep_session->elog_track;
4078 clib_spinlock_unlock (&vcm->sessions_lockp);
4080 if (PREDICT_FALSE (!is_vep))
4082 clib_warning ("VCL<%d>: ERROR: vep_idx (%u) is not a vep!",
4083 getpid (), vep_idx);
4087 if (PREDICT_FALSE (vep_next_sid == ~0))
4089 if (VPPCOM_DEBUG > 1)
4090 clib_warning ("VCL<%d>: WARNING: vep_idx (%u) is empty!",
4091 getpid (), vep_idx);
4092 if (VPPCOM_DEBUG > 1)
4095 ELOG_TYPE_DECLARE (e) =
4097 .format = "WRN: vep_idx:%d empty",
4098 .format_args = "i4",
4105 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vep_elog_track);
4118 elog_track_t session_elog_track;
4120 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
4121 sid != ~0; sid = next_sid)
4123 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
4124 u8 add_event, is_vep_session;
4126 u64 session_ev_data;
4128 VCL_LOCK_AND_GET_SESSION (sid, &session);
4129 next_sid = session->vep.next_sid;
4130 session_events = session->vep.ev.events;
4131 et_mask = session->vep.et_mask;
4132 is_vep = session->is_vep;
4133 is_vep_session = session->is_vep_session;
4134 session_vep_idx = session->vep.vep_idx;
4135 session_ev_data = session->vep.ev.data.u64;
4137 if (VPPCOM_DEBUG > 0)
4139 session_elog_track = session->elog_track;
4142 clib_spinlock_unlock (&vcm->sessions_lockp);
4144 if (PREDICT_FALSE (is_vep))
4146 if (VPPCOM_DEBUG > 0)
4147 clib_warning ("VCL<%d>: ERROR: sid (%u) is a vep!",
4148 getpid (), vep_idx);
4149 if (VPPCOM_DEBUG > 0)
4152 ELOG_TYPE_DECLARE (e) =
4154 .format = "ERR:vep_idx:%d is vep",
4155 .format_args = "i4",
4162 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
4171 if (PREDICT_FALSE (!is_vep_session))
4173 if (VPPCOM_DEBUG > 0)
4174 clib_warning ("VCL<%d>: ERROR: session (%u) is not "
4175 "a vep session!", getpid (), sid);
4176 if (VPPCOM_DEBUG > 0)
4179 ELOG_TYPE_DECLARE (e) =
4181 .format = "ERR:SID:%d not vep",
4182 .format_args = "i4",
4189 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session_elog_track);
4198 if (PREDICT_FALSE (session_vep_idx != vep_idx))
4200 clib_warning ("VCL<%d>: ERROR: session (%u) "
4201 "vep_idx (%u) != vep_idx (%u)!",
4202 getpid (), sid, session_vep_idx, vep_idx);
4207 add_event = clear_et_mask = 0;
4209 if (EPOLLIN & session_events)
4211 VCL_LOCK_AND_GET_SESSION (sid, &session);
4212 ready = vppcom_session_read_ready (session, sid);
4213 clib_spinlock_unlock (&vcm->sessions_lockp);
4214 if ((ready > 0) && (EPOLLIN & et_mask))
4217 events[num_ev].events |= EPOLLIN;
4218 if (((EPOLLET | EPOLLIN) & session_events) ==
4219 (EPOLLET | EPOLLIN))
4220 clear_et_mask |= EPOLLIN;
4227 case VPPCOM_ECONNRESET:
4228 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
4232 events[num_ev].events |= EPOLLERR;
4238 if (EPOLLOUT & session_events)
4240 VCL_LOCK_AND_GET_SESSION (sid, &session);
4241 ready = vppcom_session_write_ready (session, sid);
4242 clib_spinlock_unlock (&vcm->sessions_lockp);
4243 if ((ready > 0) && (EPOLLOUT & et_mask))
4246 events[num_ev].events |= EPOLLOUT;
4247 if (((EPOLLET | EPOLLOUT) & session_events) ==
4248 (EPOLLET | EPOLLOUT))
4249 clear_et_mask |= EPOLLOUT;
4256 case VPPCOM_ECONNRESET:
4257 events[num_ev].events |= EPOLLHUP;
4261 events[num_ev].events |= EPOLLERR;
4269 events[num_ev].data.u64 = session_ev_data;
4270 if (EPOLLONESHOT & session_events)
4272 VCL_LOCK_AND_GET_SESSION (sid, &session);
4273 session->vep.ev.events = 0;
4274 clib_spinlock_unlock (&vcm->sessions_lockp);
4277 if (num_ev == maxevents)
4279 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4280 vep_session->wait_cont_idx = next_sid;
4281 clib_spinlock_unlock (&vcm->sessions_lockp);
4285 if (wait_cont_idx != ~0)
4288 next_sid = vep_next_sid;
4289 else if (next_sid == wait_cont_idx)
4293 if (wait_for_time != -1)
4294 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
4296 while ((num_ev == 0) && keep_trying);
4298 if (wait_cont_idx != ~0)
4300 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
4301 vep_session->wait_cont_idx = ~0;
4302 clib_spinlock_unlock (&vcm->sessions_lockp);
4305 return (rv != VPPCOM_OK) ? rv : num_ev;
4309 vppcom_session_attr (uint32_t session_index, uint32_t op,
4310 void *buffer, uint32_t * buflen)
4314 u32 *flags = buffer;
4315 vppcom_endpt_t *ep = buffer;
4317 VCL_LOCK_AND_GET_SESSION (session_index, &session);
4323 case VPPCOM_ATTR_GET_NREAD:
4324 rv = vppcom_session_read_ready (session, session_index);
4325 if (VPPCOM_DEBUG > 2)
4326 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NREAD: sid %u, nread = %d",
4328 if (VPPCOM_DEBUG > 0)
4331 ELOG_TYPE_DECLARE (e) =
4333 .format = "VPPCOM_ATTR_GET_NREAD: nread=%d",
4334 .format_args = "i4",
4341 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4349 case VPPCOM_ATTR_GET_NWRITE:
4350 rv = vppcom_session_write_ready (session, session_index);
4351 if (VPPCOM_DEBUG > 2)
4352 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_NWRITE: sid %u, nwrite = %d",
4353 getpid (), session_index, rv);
4354 if (VPPCOM_DEBUG > 0)
4357 ELOG_TYPE_DECLARE (e) =
4359 .format = "VPPCOM_ATTR_GET_NWRITE: nwrite=%d",
4360 .format_args = "i4",
4367 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4374 case VPPCOM_ATTR_GET_FLAGS:
4375 if (PREDICT_TRUE (buffer && buflen && (*buflen >= sizeof (*flags))))
4377 *flags = O_RDWR | (VCL_SESS_ATTR_TEST (session->attr,
4378 VCL_SESS_ATTR_NONBLOCK));
4379 *buflen = sizeof (*flags);
4380 if (VPPCOM_DEBUG > 2)
4381 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_FLAGS: sid %u, "
4382 "flags = 0x%08x, is_nonblocking = %u", getpid (),
4383 session_index, *flags,
4384 VCL_SESS_ATTR_TEST (session->attr,
4385 VCL_SESS_ATTR_NONBLOCK));
4386 if (VPPCOM_DEBUG > 0)
4389 ELOG_TYPE_DECLARE (e) =
4391 .format = "VPPCOM_ATTR_GET_FLAGS: flags=%x is_nonblk=%d",
4392 .format_args = "i4i4",
4400 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4403 ed->is_nonblk = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
4412 case VPPCOM_ATTR_SET_FLAGS:
4413 if (PREDICT_TRUE (buffer && buflen && (*buflen == sizeof (*flags))))
4415 if (*flags & O_NONBLOCK)
4416 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_NONBLOCK);
4418 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_NONBLOCK);
4420 if (VPPCOM_DEBUG > 2)
4421 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_FLAGS: sid %u, "
4422 "flags = 0x%08x, is_nonblocking = %u",
4423 getpid (), session_index, *flags,
4424 VCL_SESS_ATTR_TEST (session->attr,
4425 VCL_SESS_ATTR_NONBLOCK));
4426 if (VPPCOM_DEBUG > 0)
4429 ELOG_TYPE_DECLARE (e) =
4431 .format = "VPPCOM_ATTR_SET_FLAGS: flags=%x is_nonblk=%d",
4432 .format_args = "i4i4",
4440 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4443 ed->is_nonblk = VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_NONBLOCK);
4451 case VPPCOM_ATTR_GET_PEER_ADDR:
4452 if (PREDICT_TRUE (buffer && buflen &&
4453 (*buflen >= sizeof (*ep)) && ep->ip))
4455 ep->is_ip4 = session->peer_addr.is_ip4;
4456 ep->port = session->peer_port;
4457 if (session->peer_addr.is_ip4)
4458 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
4459 sizeof (ip4_address_t));
4461 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
4462 sizeof (ip6_address_t));
4463 *buflen = sizeof (*ep);
4464 if (VPPCOM_DEBUG > 1)
4465 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PEER_ADDR: sid %u, "
4466 "is_ip4 = %u, addr = %U, port %u", getpid (),
4467 session_index, ep->is_ip4, format_ip46_address,
4468 &session->peer_addr.ip46,
4469 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
4470 clib_net_to_host_u16 (ep->port));
4471 if (VPPCOM_DEBUG > 0)
4476 ELOG_TYPE_DECLARE (e) =
4478 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:%d.%d.%d.%d:%d",
4479 .format_args = "i1i1i1i1i2",
4481 CLIB_PACKED (struct {
4486 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4488 ed->addr[0] = session->peer_addr.ip46.ip4.as_u8[0];
4489 ed->addr[1] = session->peer_addr.ip46.ip4.as_u8[1];
4490 ed->addr[2] = session->peer_addr.ip46.ip4.as_u8[2];
4491 ed->addr[3] = session->peer_addr.ip46.ip4.as_u8[3];
4492 ed->port = clib_net_to_host_u16 (session->peer_port);
4498 ELOG_TYPE_DECLARE (e) =
4500 .format = "VPPCOM_ATTR_GET_PEER_ADDR: addr:IP6:%d",
4501 .format_args = "i2",
4503 CLIB_PACKED (struct {
4507 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4509 ed->port = clib_net_to_host_u16 (session->peer_port);
4518 case VPPCOM_ATTR_GET_LCL_ADDR:
4519 if (PREDICT_TRUE (buffer && buflen &&
4520 (*buflen >= sizeof (*ep)) && ep->ip))
4522 ep->is_ip4 = session->lcl_addr.is_ip4;
4523 ep->port = session->lcl_port;
4524 if (session->lcl_addr.is_ip4)
4525 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
4526 sizeof (ip4_address_t));
4528 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
4529 sizeof (ip6_address_t));
4530 *buflen = sizeof (*ep);
4531 if (VPPCOM_DEBUG > 1)
4532 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LCL_ADDR: sid %u, "
4533 "is_ip4 = %u, addr = %U port %d", getpid (),
4534 session_index, ep->is_ip4, format_ip46_address,
4535 &session->lcl_addr.ip46,
4536 ep->is_ip4 ? IP46_TYPE_IP4 : IP46_TYPE_IP6,
4537 clib_net_to_host_u16 (ep->port));
4538 if (VPPCOM_DEBUG > 0)
4543 ELOG_TYPE_DECLARE (e) =
4545 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:%d.%d.%d.%d:%d",
4546 .format_args = "i1i1i1i1i2",
4548 CLIB_PACKED (struct {
4553 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4555 ed->addr[0] = session->lcl_addr.ip46.ip4.as_u8[0];
4556 ed->addr[1] = session->lcl_addr.ip46.ip4.as_u8[1];
4557 ed->addr[2] = session->lcl_addr.ip46.ip4.as_u8[2];
4558 ed->addr[3] = session->lcl_addr.ip46.ip4.as_u8[3];
4559 ed->port = clib_net_to_host_u16 (session->peer_port);
4565 ELOG_TYPE_DECLARE (e) =
4567 .format = "VPPCOM_ATTR_GET_LCL_ADDR: addr:IP6:%d",
4568 .format_args = "i2",
4570 CLIB_PACKED (struct {
4574 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4576 ed->port = clib_net_to_host_u16 (session->peer_port);
4585 case VPPCOM_ATTR_GET_LIBC_EPFD:
4586 rv = session->libc_epfd;
4587 if (VPPCOM_DEBUG > 2)
4588 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd %d",
4590 if (VPPCOM_DEBUG > 0)
4593 ELOG_TYPE_DECLARE (e) =
4595 .format = "VPPCOM_ATTR_GET_LIBC_EPFD: libc_epfd=%d",
4596 .format_args = "i4",
4598 CLIB_PACKED (struct {
4602 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4603 ed->data = session->libc_epfd;
4609 case VPPCOM_ATTR_SET_LIBC_EPFD:
4610 if (PREDICT_TRUE (buffer && buflen &&
4611 (*buflen == sizeof (session->libc_epfd))))
4613 session->libc_epfd = *(int *) buffer;
4614 *buflen = sizeof (session->libc_epfd);
4616 if (VPPCOM_DEBUG > 2)
4617 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd %d, "
4618 "buflen %d", getpid (), session->libc_epfd,
4620 if (VPPCOM_DEBUG > 0)
4623 ELOG_TYPE_DECLARE (e) =
4625 .format = "VPPCOM_ATTR_SET_LIBC_EPFD: libc_epfd=%s%d buflen=%d",
4626 .format_args = "t1i4i4",
4627 .n_enum_strings = 2,
4628 .enum_strings = {"", "-",},
4630 CLIB_PACKED (struct {
4635 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4637 ed->sign = (session->libc_epfd < 0);
4638 ed->data[0] = abs(session->libc_epfd);
4639 ed->data[1] = *buflen;
4647 case VPPCOM_ATTR_GET_PROTOCOL:
4648 if (buffer && buflen && (*buflen >= sizeof (int)))
4650 *(int *) buffer = session->proto;
4651 *buflen = sizeof (int);
4653 if (VPPCOM_DEBUG > 2)
4654 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_PROTOCOL: %d (%s), "
4655 "buflen %d", getpid (), *(int *) buffer,
4656 *(int *) buffer ? "UDP" : "TCP", *buflen);
4657 if (VPPCOM_DEBUG > 0)
4660 ELOG_TYPE_DECLARE (e) =
4662 .format = "VPPCOM_ATTR_GET_PROTOCOL: %s buflen=%d",
4663 .format_args = "t1i4",
4664 .n_enum_strings = 2,
4665 .enum_strings = {"TCP", "UDP",},
4668 CLIB_PACKED (struct {
4673 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4674 ed->proto = session->proto;
4675 ed->buflen = *(int *) buffer;
4683 case VPPCOM_ATTR_GET_LISTEN:
4684 if (buffer && buflen && (*buflen >= sizeof (int)))
4686 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4687 VCL_SESS_ATTR_LISTEN);
4688 *buflen = sizeof (int);
4690 if (VPPCOM_DEBUG > 2)
4691 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_LISTEN: %d, "
4692 "buflen %d", getpid (), *(int *) buffer, *buflen);
4693 if (VPPCOM_DEBUG > 0)
4696 ELOG_TYPE_DECLARE (e) =
4698 .format = "VPPCOM_ATTR_GET_LISTEN: %d buflen=%d",
4699 .format_args = "i4i4",
4706 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4707 ed->data[0] = *(int *) buffer;
4708 ed->data[1] = *buflen;
4716 case VPPCOM_ATTR_GET_ERROR:
4717 if (buffer && buflen && (*buflen >= sizeof (int)))
4719 *(int *) buffer = 0;
4720 *buflen = sizeof (int);
4722 if (VPPCOM_DEBUG > 2)
4723 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_ERROR: %d, "
4724 "buflen %d, #VPP-TBD#", getpid (),
4725 *(int *) buffer, *buflen);
4726 if (VPPCOM_DEBUG > 0)
4729 ELOG_TYPE_DECLARE (e) =
4731 .format = "VPPCOM_ATTR_GET_ERROR: %d buflen=%d",
4732 .format_args = "i4i4",
4739 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4740 ed->data[0] = *(int *) buffer;
4741 ed->data[1] = *buflen;
4749 case VPPCOM_ATTR_GET_TX_FIFO_LEN:
4750 if (buffer && buflen && (*buflen >= sizeof (u32)))
4754 *(size_t *) buffer = (session->sndbuf_size ? session->sndbuf_size :
4755 session->tx_fifo ? session->tx_fifo->nitems :
4756 vcm->cfg.tx_fifo_size);
4757 *buflen = sizeof (u32);
4759 if (VPPCOM_DEBUG > 2)
4760 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TX_FIFO_LEN: %u (0x%x), "
4761 "buflen %d, #VPP-TBD#", getpid (),
4762 *(size_t *) buffer, *(size_t *) buffer, *buflen);
4763 if (VPPCOM_DEBUG > 0)
4766 ELOG_TYPE_DECLARE (e) =
4768 .format = "VPPCOM_ATTR_GET_TX_FIFO_LEN: 0x%x buflen=%d",
4769 .format_args = "i4i4",
4776 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4777 ed->data[0] = *(size_t *) buffer;
4778 ed->data[1] = *buflen;
4786 case VPPCOM_ATTR_SET_TX_FIFO_LEN:
4787 if (buffer && buflen && (*buflen == sizeof (u32)))
4790 session->sndbuf_size = *(u32 *) buffer;
4791 if (VPPCOM_DEBUG > 2)
4792 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TX_FIFO_LEN: %u (0x%x), "
4793 "buflen %d, #VPP-TBD#", getpid (),
4794 session->sndbuf_size, session->sndbuf_size,
4796 if (VPPCOM_DEBUG > 0)
4799 ELOG_TYPE_DECLARE (e) =
4801 .format = "VPPCOM_ATTR_SET_TX_FIFO_LEN: 0x%x buflen=%d",
4802 .format_args = "i4i4",
4809 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4810 ed->data[0] = session->sndbuf_size;
4811 ed->data[1] = *buflen;
4819 case VPPCOM_ATTR_GET_RX_FIFO_LEN:
4820 if (buffer && buflen && (*buflen >= sizeof (u32)))
4824 *(size_t *) buffer = (session->rcvbuf_size ? session->rcvbuf_size :
4825 session->rx_fifo ? session->rx_fifo->nitems :
4826 vcm->cfg.rx_fifo_size);
4827 *buflen = sizeof (u32);
4829 if (VPPCOM_DEBUG > 2)
4830 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_RX_FIFO_LEN: %u (0x%x), "
4831 "buflen %d, #VPP-TBD#", getpid (),
4832 *(size_t *) buffer, *(size_t *) buffer, *buflen);
4833 if (VPPCOM_DEBUG > 0)
4836 ELOG_TYPE_DECLARE (e) =
4838 .format = "VPPCOM_ATTR_GET_RX_FIFO_LEN: 0x%x buflen=%d",
4839 .format_args = "i4i4",
4846 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4847 ed->data[0] = *(size_t *) buffer;
4848 ed->data[1] = *buflen;
4856 case VPPCOM_ATTR_SET_RX_FIFO_LEN:
4857 if (buffer && buflen && (*buflen == sizeof (u32)))
4860 session->rcvbuf_size = *(u32 *) buffer;
4861 if (VPPCOM_DEBUG > 2)
4862 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_RX_FIFO_LEN: %u (0x%x), "
4863 "buflen %d, #VPP-TBD#", getpid (),
4864 session->sndbuf_size, session->sndbuf_size,
4866 if (VPPCOM_DEBUG > 0)
4869 ELOG_TYPE_DECLARE (e) =
4871 .format = "VPPCOM_ATTR_SET_RX_FIFO_LEN: 0x%x buflen=%d",
4872 .format_args = "i4i4",
4879 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4880 ed->data[0] = session->sndbuf_size;
4881 ed->data[1] = *buflen;
4889 case VPPCOM_ATTR_GET_REUSEADDR:
4890 if (buffer && buflen && (*buflen >= sizeof (int)))
4893 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4894 VCL_SESS_ATTR_REUSEADDR);
4895 *buflen = sizeof (int);
4897 if (VPPCOM_DEBUG > 2)
4898 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEADDR: %d, "
4899 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4901 if (VPPCOM_DEBUG > 0)
4904 ELOG_TYPE_DECLARE (e) =
4906 .format = "VPPCOM_ATTR_GET_REUSEADDR: %d buflen=%d",
4907 .format_args = "i4i4",
4914 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4915 ed->data[0] = *(int *) buffer;
4916 ed->data[1] = *buflen;
4924 case VPPCOM_ATTR_SET_REUSEADDR:
4925 if (buffer && buflen && (*buflen == sizeof (int)) &&
4926 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
4929 if (*(int *) buffer)
4930 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEADDR);
4932 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEADDR);
4934 if (VPPCOM_DEBUG > 2)
4935 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEADDR: %d, "
4936 "buflen %d, #VPP-TBD#", getpid (),
4937 VCL_SESS_ATTR_TEST (session->attr,
4938 VCL_SESS_ATTR_REUSEADDR),
4940 if (VPPCOM_DEBUG > 0)
4943 ELOG_TYPE_DECLARE (e) =
4945 .format = "VPPCOM_ATTR_SET_REUSEADDR: %d buflen=%d",
4946 .format_args = "i4i4",
4953 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4954 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
4955 VCL_SESS_ATTR_REUSEADDR);
4956 ed->data[1] = *buflen;
4964 case VPPCOM_ATTR_GET_REUSEPORT:
4965 if (buffer && buflen && (*buflen >= sizeof (int)))
4968 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
4969 VCL_SESS_ATTR_REUSEPORT);
4970 *buflen = sizeof (int);
4972 if (VPPCOM_DEBUG > 2)
4973 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_REUSEPORT: %d, "
4974 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
4976 if (VPPCOM_DEBUG > 0)
4979 ELOG_TYPE_DECLARE (e) =
4981 .format = "VPPCOM_ATTR_GET_REUSEPORT: %d buflen=%d",
4982 .format_args = "i4i4",
4989 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
4990 ed->data[0] = *(int *) buffer;
4991 ed->data[1] = *buflen;
4999 case VPPCOM_ATTR_SET_REUSEPORT:
5000 if (buffer && buflen && (*buflen == sizeof (int)) &&
5001 !VCL_SESS_ATTR_TEST (session->attr, VCL_SESS_ATTR_LISTEN))
5004 if (*(int *) buffer)
5005 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_REUSEPORT);
5007 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_REUSEPORT);
5009 if (VPPCOM_DEBUG > 2)
5010 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_REUSEPORT: %d, "
5011 "buflen %d, #VPP-TBD#", getpid (),
5012 VCL_SESS_ATTR_TEST (session->attr,
5013 VCL_SESS_ATTR_REUSEPORT),
5015 if (VPPCOM_DEBUG > 0)
5018 ELOG_TYPE_DECLARE (e) =
5020 .format = "VPPCOM_ATTR_SET_REUSEPORT: %d buflen=%d",
5021 .format_args = "i4i4",
5028 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5029 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5030 VCL_SESS_ATTR_REUSEPORT);
5031 ed->data[1] = *buflen;
5039 case VPPCOM_ATTR_GET_BROADCAST:
5040 if (buffer && buflen && (*buflen >= sizeof (int)))
5043 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5044 VCL_SESS_ATTR_BROADCAST);
5045 *buflen = sizeof (int);
5047 if (VPPCOM_DEBUG > 2)
5048 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_BROADCAST: %d, "
5049 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5051 if (VPPCOM_DEBUG > 0)
5054 ELOG_TYPE_DECLARE (e) =
5056 .format = "VPPCOM_ATTR_GET_BROADCAST: %d buflen=%d",
5057 .format_args = "i4i4",
5064 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5065 ed->data[0] = *(int *) buffer;
5066 ed->data[1] = *buflen;
5074 case VPPCOM_ATTR_SET_BROADCAST:
5075 if (buffer && buflen && (*buflen == sizeof (int)))
5078 if (*(int *) buffer)
5079 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_BROADCAST);
5081 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_BROADCAST);
5083 if (VPPCOM_DEBUG > 2)
5084 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_BROADCAST: %d, "
5085 "buflen %d, #VPP-TBD#", getpid (),
5086 VCL_SESS_ATTR_TEST (session->attr,
5087 VCL_SESS_ATTR_BROADCAST),
5089 if (VPPCOM_DEBUG > 0)
5092 ELOG_TYPE_DECLARE (e) =
5094 .format = "VPPCOM_ATTR_SET_BROADCAST: %d buflen=%d",
5095 .format_args = "i4i4",
5102 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5103 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5104 VCL_SESS_ATTR_BROADCAST);
5105 ed->data[1] = *buflen;
5113 case VPPCOM_ATTR_GET_V6ONLY:
5114 if (buffer && buflen && (*buflen >= sizeof (int)))
5117 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5118 VCL_SESS_ATTR_V6ONLY);
5119 *buflen = sizeof (int);
5121 if (VPPCOM_DEBUG > 2)
5122 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_V6ONLY: %d, "
5123 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5125 if (VPPCOM_DEBUG > 0)
5128 ELOG_TYPE_DECLARE (e) =
5130 .format = "VPPCOM_ATTR_GET_V6ONLY: %d buflen=%d",
5131 .format_args = "i4i4",
5138 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5139 ed->data[0] = *(int *) buffer;
5140 ed->data[1] = *buflen;
5148 case VPPCOM_ATTR_SET_V6ONLY:
5149 if (buffer && buflen && (*buflen == sizeof (int)))
5152 if (*(int *) buffer)
5153 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_V6ONLY);
5155 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_V6ONLY);
5157 if (VPPCOM_DEBUG > 2)
5158 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_V6ONLY: %d, "
5159 "buflen %d, #VPP-TBD#", getpid (),
5160 VCL_SESS_ATTR_TEST (session->attr,
5161 VCL_SESS_ATTR_V6ONLY), *buflen);
5162 if (VPPCOM_DEBUG > 0)
5165 ELOG_TYPE_DECLARE (e) =
5167 .format = "VPPCOM_ATTR_SET_V6ONLY: %d buflen=%d",
5168 .format_args = "i4i4",
5175 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5176 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5177 VCL_SESS_ATTR_V6ONLY);
5178 ed->data[1] = *buflen;
5186 case VPPCOM_ATTR_GET_KEEPALIVE:
5187 if (buffer && buflen && (*buflen >= sizeof (int)))
5190 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5191 VCL_SESS_ATTR_KEEPALIVE);
5192 *buflen = sizeof (int);
5194 if (VPPCOM_DEBUG > 2)
5195 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_KEEPALIVE: %d, "
5196 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5198 if (VPPCOM_DEBUG > 0)
5201 ELOG_TYPE_DECLARE (e) =
5203 .format = "VPPCOM_ATTR_GET_KEEPALIVE: %d buflen=%d",
5204 .format_args = "i4i4",
5211 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5212 ed->data[0] = *(int *) buffer;
5213 ed->data[1] = *buflen;
5221 case VPPCOM_ATTR_SET_KEEPALIVE:
5222 if (buffer && buflen && (*buflen == sizeof (int)))
5225 if (*(int *) buffer)
5226 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5228 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_KEEPALIVE);
5230 if (VPPCOM_DEBUG > 2)
5231 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_KEEPALIVE: %d, "
5232 "buflen %d, #VPP-TBD#", getpid (),
5233 VCL_SESS_ATTR_TEST (session->attr,
5234 VCL_SESS_ATTR_KEEPALIVE),
5236 if (VPPCOM_DEBUG > 0)
5239 ELOG_TYPE_DECLARE (e) =
5241 .format = "VPPCOM_ATTR_SET_KEEPALIVE: %d buflen=%d",
5242 .format_args = "i4i4",
5249 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5250 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5251 VCL_SESS_ATTR_KEEPALIVE);
5252 ed->data[1] = *buflen;
5260 case VPPCOM_ATTR_GET_TCP_NODELAY:
5261 if (buffer && buflen && (*buflen >= sizeof (int)))
5264 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5265 VCL_SESS_ATTR_TCP_NODELAY);
5266 *buflen = sizeof (int);
5268 if (VPPCOM_DEBUG > 2)
5269 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_NODELAY: %d, "
5270 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5272 if (VPPCOM_DEBUG > 0)
5275 ELOG_TYPE_DECLARE (e) =
5277 .format = "VPPCOM_ATTR_GET_TCP_NODELAY: %d buflen=%d",
5278 .format_args = "i4i4",
5285 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5286 ed->data[0] = *(int *) buffer;
5287 ed->data[1] = *buflen;
5295 case VPPCOM_ATTR_SET_TCP_NODELAY:
5296 if (buffer && buflen && (*buflen == sizeof (int)))
5299 if (*(int *) buffer)
5300 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5302 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_NODELAY);
5304 if (VPPCOM_DEBUG > 2)
5305 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_NODELAY: %d, "
5306 "buflen %d, #VPP-TBD#", getpid (),
5307 VCL_SESS_ATTR_TEST (session->attr,
5308 VCL_SESS_ATTR_TCP_NODELAY),
5310 if (VPPCOM_DEBUG > 0)
5313 ELOG_TYPE_DECLARE (e) =
5315 .format = "VPPCOM_ATTR_SET_TCP_NODELAY: %d buflen=%d",
5316 .format_args = "i4i4",
5323 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5324 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5325 VCL_SESS_ATTR_TCP_NODELAY);
5326 ed->data[1] = *buflen;
5334 case VPPCOM_ATTR_GET_TCP_KEEPIDLE:
5335 if (buffer && buflen && (*buflen >= sizeof (int)))
5338 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5339 VCL_SESS_ATTR_TCP_KEEPIDLE);
5340 *buflen = sizeof (int);
5342 if (VPPCOM_DEBUG > 2)
5343 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d, "
5344 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5346 if (VPPCOM_DEBUG > 0)
5349 ELOG_TYPE_DECLARE (e) =
5351 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5352 .format_args = "i4i4",
5359 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5360 ed->data[0] = *(int *) buffer;
5361 ed->data[1] = *buflen;
5369 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
5370 if (buffer && buflen && (*buflen == sizeof (int)))
5373 if (*(int *) buffer)
5374 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5376 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPIDLE);
5378 if (VPPCOM_DEBUG > 2)
5379 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d, "
5380 "buflen %d, #VPP-TBD#", getpid (),
5381 VCL_SESS_ATTR_TEST (session->attr,
5382 VCL_SESS_ATTR_TCP_KEEPIDLE),
5384 if (VPPCOM_DEBUG > 0)
5387 ELOG_TYPE_DECLARE (e) =
5389 .format = "VPPCOM_ATTR_SET_TCP_KEEPIDLE: %d buflen=%d",
5390 .format_args = "i4i4",
5397 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5398 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5399 VCL_SESS_ATTR_TCP_KEEPIDLE);
5400 ed->data[1] = *buflen;
5408 case VPPCOM_ATTR_GET_TCP_KEEPINTVL:
5409 if (buffer && buflen && (*buflen >= sizeof (int)))
5412 *(int *) buffer = VCL_SESS_ATTR_TEST (session->attr,
5413 VCL_SESS_ATTR_TCP_KEEPINTVL);
5414 *buflen = sizeof (int);
5416 if (VPPCOM_DEBUG > 2)
5417 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_KEEPINTVL: %d, "
5418 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5420 if (VPPCOM_DEBUG > 0)
5423 ELOG_TYPE_DECLARE (e) =
5425 .format = "VPPCOM_ATTR_GET_TCP_KEEPIDLE: %d buflen=%d",
5426 .format_args = "i4i4",
5433 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5434 ed->data[0] = *(int *) buffer;
5435 ed->data[1] = *buflen;
5443 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
5444 if (buffer && buflen && (*buflen == sizeof (int)))
5447 if (*(int *) buffer)
5448 VCL_SESS_ATTR_SET (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5450 VCL_SESS_ATTR_CLR (session->attr, VCL_SESS_ATTR_TCP_KEEPINTVL);
5452 if (VPPCOM_DEBUG > 2)
5453 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d, "
5454 "buflen %d, #VPP-TBD#", getpid (),
5455 VCL_SESS_ATTR_TEST (session->attr,
5456 VCL_SESS_ATTR_TCP_KEEPINTVL),
5458 if (VPPCOM_DEBUG > 0)
5461 ELOG_TYPE_DECLARE (e) =
5463 .format = "VPPCOM_ATTR_SET_TCP_KEEPINTVL: %d buflen=%d",
5464 .format_args = "i4i4",
5471 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5472 ed->data[0] = VCL_SESS_ATTR_TEST (session->attr,
5473 VCL_SESS_ATTR_TCP_KEEPINTVL);
5474 ed->data[1] = *buflen;
5482 case VPPCOM_ATTR_GET_TCP_USER_MSS:
5483 if (buffer && buflen && (*buflen >= sizeof (u32)))
5486 *(u32 *) buffer = session->user_mss;
5487 *buflen = sizeof (int);
5489 if (VPPCOM_DEBUG > 2)
5490 clib_warning ("VCL<%d>: VPPCOM_ATTR_GET_TCP_USER_MSS: %d, "
5491 "buflen %d, #VPP-TBD#", getpid (), *(int *) buffer,
5493 if (VPPCOM_DEBUG > 0)
5496 ELOG_TYPE_DECLARE (e) =
5498 .format = "VPPCOM_ATTR_GET_TCP_USER_MSS: %d buflen=%d",
5499 .format_args = "i4i4",
5506 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5507 ed->data[0] = *(int *) buffer;
5508 ed->data[1] = *buflen;
5516 case VPPCOM_ATTR_SET_TCP_USER_MSS:
5517 if (buffer && buflen && (*buflen == sizeof (u32)))
5520 session->user_mss = *(u32 *) buffer;
5522 if (VPPCOM_DEBUG > 2)
5523 clib_warning ("VCL<%d>: VPPCOM_ATTR_SET_TCP_USER_MSS: %u, "
5524 "buflen %d, #VPP-TBD#", getpid (),
5525 session->user_mss, *buflen);
5526 if (VPPCOM_DEBUG > 0)
5529 ELOG_TYPE_DECLARE (e) =
5531 .format = "VPPCOM_ATTR_SET_TCP_USER_MSS: %d buflen=%d",
5532 .format_args = "i4i4",
5539 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, session->elog_track);
5540 ed->data[0] = session->user_mss;
5541 ed->data[1] = *buflen;
5555 clib_spinlock_unlock (&vcm->sessions_lockp);
5560 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
5561 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5564 session_t *session = 0;
5568 clib_spinlock_lock (&vcm->sessions_lockp);
5569 rv = vppcom_session_at_index (session_index, &session);
5570 if (PREDICT_FALSE (rv))
5572 clib_spinlock_unlock (&vcm->sessions_lockp);
5573 if (VPPCOM_DEBUG > 0)
5574 clib_warning ("VCL<%d>: invalid session, "
5575 "sid (%u) has been closed!",
5576 getpid (), session_index);
5577 if (VPPCOM_DEBUG > 0)
5580 ELOG_TYPE_DECLARE (e) =
5582 .format = "invalid session: %d closed",
5583 .format_args = "i4",
5590 ed = ELOG_TRACK_DATA (&vcm->elog_main, e, vcm->elog_track);
5591 ed->data = session_index;
5595 clib_spinlock_unlock (&vcm->sessions_lockp);
5598 ep->is_ip4 = session->peer_addr.is_ip4;
5599 ep->port = session->peer_port;
5600 if (session->peer_addr.is_ip4)
5601 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
5602 sizeof (ip4_address_t));
5604 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
5605 sizeof (ip6_address_t));
5606 clib_spinlock_unlock (&vcm->sessions_lockp);
5610 rv = vppcom_session_read (session_index, buffer, buflen);
5611 else if (flags & MSG_PEEK)
5612 rv = vppcom_session_peek (session_index, buffer, buflen);
5615 clib_warning ("VCL<%d>: Unsupport flags for recvfrom %d",
5617 rv = VPPCOM_EAFNOSUPPORT;
5625 vppcom_session_sendto (uint32_t session_index, void *buffer,
5626 uint32_t buflen, int flags, vppcom_endpt_t * ep)
5629 return VPPCOM_EINVAL;
5634 return VPPCOM_EINVAL;
5639 // TBD check the flags and do the right thing
5640 if (VPPCOM_DEBUG > 2)
5641 clib_warning ("VCL<%d>: handling flags 0x%u (%d) "
5642 "not implemented yet.", getpid (), flags, flags);
5645 return (vppcom_session_write (session_index, buffer, buflen));
5649 vppcom_poll (vcl_poll_t * vp, uint32_t n_sids, double wait_for_time)
5651 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
5652 u32 i, keep_trying = 1;
5655 if (VPPCOM_DEBUG > 3)
5656 clib_warning ("VCL<%d>: vp %p, nsids %u, wait_for_time %f",
5657 getpid (), vp, n_sids, wait_for_time);
5660 return VPPCOM_EFAULT;
5666 for (i = 0; i < n_sids; i++)
5668 ASSERT (vp[i].revents);
5670 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5671 clib_spinlock_unlock (&vcm->sessions_lockp);
5676 if (POLLIN & vp[i].events)
5678 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5679 rv = vppcom_session_read_ready (session, vp[i].sid);
5680 clib_spinlock_unlock (&vcm->sessions_lockp);
5683 *vp[i].revents |= POLLIN;
5690 case VPPCOM_ECONNRESET:
5691 *vp[i].revents = POLLHUP;
5695 *vp[i].revents = POLLERR;
5702 if (POLLOUT & vp[i].events)
5704 VCL_LOCK_AND_GET_SESSION (vp[i].sid, &session);
5705 rv = vppcom_session_write_ready (session, vp[i].sid);
5706 clib_spinlock_unlock (&vcm->sessions_lockp);
5709 *vp[i].revents |= POLLOUT;
5716 case VPPCOM_ECONNRESET:
5717 *vp[i].revents = POLLHUP;
5721 *vp[i].revents = POLLERR;
5728 if (0) // Note "done:" label used by VCL_LOCK_AND_GET_SESSION()
5731 *vp[i].revents = POLLNVAL;
5735 if (wait_for_time != -1)
5736 keep_trying = (clib_time_now (&vcm->clib_time) <= timeout) ? 1 : 0;
5738 while ((num_ev == 0) && keep_trying);
5740 if (VPPCOM_DEBUG > 3)
5742 clib_warning ("VCL<%d>: returning %d", getpid (), num_ev);
5743 for (i = 0; i < n_sids; i++)
5745 clib_warning ("VCL<%d>: vp[%d].sid %d (0x%x), .events 0x%x, "
5746 ".revents 0x%x", getpid (), i, vp[i].sid, vp[i].sid,
5747 vp[i].events, *vp[i].revents);
5754 * fd.io coding-style-patch-verification: ON
5757 * eval: (c-set-style "gnu")