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 <vlib/unix/unix.h>
25 #include <vppinfra/vec_bootstrap.h>
27 #define vl_typedefs /* define message structures */
28 #include <vpp/api/vpe_all_api_h.h>
31 /* declare message handlers for each api */
33 #define vl_endianfun /* define message structures */
34 #include <vpp/api/vpe_all_api_h.h>
37 /* instantiate all the print functions we know about */
38 #define vl_print(handle, ...)
40 #include <vpp/api/vpe_all_api_h.h>
44 /* Set VPPCOM_DEBUG 2 for connection debug, 3 for read/write debug output */
45 #define VPPCOM_DEBUG 1
47 #define VPPCOM_DEBUG 0
51 * VPPCOM Private definitions and functions.
71 typedef struct epoll_event vppcom_epoll_event_t;
78 vppcom_epoll_event_t ev;
79 #define VEP_DEFAULT_ET_MASK (EPOLLIN|EPOLLOUT)
91 volatile session_state_t state;
93 svm_fifo_t *server_rx_fifo;
94 svm_fifo_t *server_tx_fifo;
96 u64 vpp_session_handle;
97 unix_shared_memory_queue_t *vpp_event_queue;
99 /* Socket configuration state */
100 /* TBD: covert 'is_*' vars to bit in u8 flags; */
110 vppcom_ip46_t lcl_addr;
111 vppcom_ip46_t peer_addr;
112 u16 lcl_port; // network order
113 u16 peer_port; // network order
115 u64 client_queue_address;
119 typedef struct vppcom_cfg_t_
124 u32 add_segment_size;
125 u32 preallocated_fifo_pairs;
128 u32 event_queue_size;
129 u32 listen_queue_size;
130 u8 session_scope_local;
131 u8 session_scope_global;
133 u64 namespace_secret;
139 typedef struct vppcom_main_t_
142 u32 *client_session_index_fifo;
143 volatile u32 bind_session_index;
146 /* vpe input queue */
147 unix_shared_memory_queue_t *vl_input_queue;
149 /* API client handle */
153 clib_spinlock_t sessions_lockp;
156 /* Hash table for disconnect processing */
157 uword *session_index_by_vpp_handles;
160 clib_bitmap_t *rd_bitmap;
161 clib_bitmap_t *wr_bitmap;
162 clib_bitmap_t *ex_bitmap;
164 /* Our event queue */
165 unix_shared_memory_queue_t *app_event_queue;
167 /* unique segment name counter */
168 u32 unique_segment_index;
172 /* For deadman timers */
173 clib_time_t clib_time;
175 /* State of the connection, shared between msg RX thread and main thread */
176 volatile app_state_t app_state;
180 /* VNET_API_ERROR_FOO -> "Foo" hash table */
181 uword *error_string_by_error_number;
184 vppcom_main_t vppcom_main = {.my_client_index = ~0 };
187 vppcom_app_state_str (app_state_t state)
193 case STATE_APP_START:
194 st = "STATE_APP_START";
197 case STATE_APP_CONN_VPP:
198 st = "STATE_APP_CONN_VPP";
201 case STATE_APP_ENABLED:
202 st = "STATE_APP_ENABLED";
205 case STATE_APP_ATTACHED:
206 st = "STATE_APP_ATTACHED";
210 st = "UNKNOWN_APP_STATE";
218 vppcom_session_state_str (session_state_t state)
229 st = "STATE_CONNECT";
240 case STATE_DISCONNECT:
241 st = "STATE_DISCONNECT";
249 st = "UNKNOWN_STATE";
257 * VPPCOM Utility Functions
260 vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
262 vppcom_main_t *vcm = &vppcom_main;
264 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
265 if (PREDICT_FALSE ((session_index == ~0) ||
266 pool_is_free_index (vcm->sessions, session_index)))
268 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
269 vcm->my_pid, session_index);
270 return VPPCOM_EBADFD;
272 *sess = pool_elt_at_index (vcm->sessions, session_index);
277 vppcom_connect_to_vpp (char *app_name)
279 api_main_t *am = &api_main;
280 vppcom_main_t *vcm = &vppcom_main;
282 if (VPPCOM_DEBUG > 0)
283 printf ("\nConnecting to VPP api...");
284 if (vl_client_connect_to_vlib ("/vpe-api", app_name, 32) < 0)
286 clib_warning ("[%d] connect to vpp (%s) failed!",
287 vcm->my_pid, app_name);
288 return VPPCOM_ECONNREFUSED;
291 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
292 vcm->my_client_index = am->my_client_index;
293 if (VPPCOM_DEBUG > 0)
294 printf (" connected!\n");
296 vcm->app_state = STATE_APP_CONN_VPP;
301 format_api_error (u8 * s, va_list * args)
303 vppcom_main_t *vcm = &vppcom_main;
304 i32 error = va_arg (*args, u32);
307 p = hash_get (vcm->error_string_by_error_number, -error);
310 s = format (s, "%s (%d)", p[0], error);
312 s = format (s, "%d", error);
317 vppcom_init_error_string_table (void)
319 vppcom_main_t *vcm = &vppcom_main;
321 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
323 #define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s);
324 foreach_vnet_api_error;
327 hash_set (vcm->error_string_by_error_number, 99, "Misc");
331 vppcom_wait_for_app_state_change (app_state_t app_state)
333 vppcom_main_t *vcm = &vppcom_main;
334 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
336 while (clib_time_now (&vcm->clib_time) < timeout)
338 if (vcm->app_state == app_state)
341 if (VPPCOM_DEBUG > 0)
342 clib_warning ("[%d] timeout waiting for state %s (%d)", vcm->my_pid,
343 vppcom_app_state_str (app_state), app_state);
344 return VPPCOM_ETIMEDOUT;
348 vppcom_wait_for_session_state_change (u32 session_index,
349 session_state_t state,
352 vppcom_main_t *vcm = &vppcom_main;
353 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
354 session_t *volatile session;
359 clib_spinlock_lock (&vcm->sessions_lockp);
360 rv = vppcom_session_at_index (session_index, &session);
361 if (PREDICT_FALSE (rv))
363 clib_spinlock_unlock (&vcm->sessions_lockp);
366 if (session->state == state)
368 clib_spinlock_unlock (&vcm->sessions_lockp);
371 clib_spinlock_unlock (&vcm->sessions_lockp);
373 while (clib_time_now (&vcm->clib_time) < timeout);
375 if (VPPCOM_DEBUG > 0)
376 clib_warning ("[%d] timeout waiting for state %s (%d)", vcm->my_pid,
377 vppcom_session_state_str (state), state);
378 return VPPCOM_ETIMEDOUT;
382 vppcom_wait_for_client_session_index (f64 wait_for_time)
384 vppcom_main_t *vcm = &vppcom_main;
385 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
389 if (clib_fifo_elts (vcm->client_session_index_fifo))
392 while (clib_time_now (&vcm->clib_time) < timeout);
394 if (wait_for_time == 0)
395 return VPPCOM_EAGAIN;
397 if (VPPCOM_DEBUG > 0)
398 clib_warning ("[%d] timeout waiting for client_session_index",
400 return VPPCOM_ETIMEDOUT;
404 * VPP-API message functions
407 vppcom_send_session_enable_disable (u8 is_enable)
409 vppcom_main_t *vcm = &vppcom_main;
410 vl_api_session_enable_disable_t *bmp;
411 bmp = vl_msg_api_alloc (sizeof (*bmp));
412 memset (bmp, 0, sizeof (*bmp));
414 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
415 bmp->client_index = vcm->my_client_index;
416 bmp->context = htonl (0xfeedface);
417 bmp->is_enable = is_enable;
418 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
422 vppcom_app_session_enable (void)
424 vppcom_main_t *vcm = &vppcom_main;
427 if (vcm->app_state != STATE_APP_ENABLED)
429 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
430 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
431 if (PREDICT_FALSE (rv))
433 if (VPPCOM_DEBUG > 0)
434 clib_warning ("[%d] Session enable timed out, rv = %s (%d)",
435 vcm->my_pid, vppcom_retval_str (rv), rv);
443 vl_api_session_enable_disable_reply_t_handler
444 (vl_api_session_enable_disable_reply_t * mp)
446 vppcom_main_t *vcm = &vppcom_main;
450 clib_warning ("[%d] session_enable_disable failed: %U", vcm->my_pid,
451 format_api_error, ntohl (mp->retval));
454 vcm->app_state = STATE_APP_ENABLED;
458 vppcom_app_send_attach (void)
460 vppcom_main_t *vcm = &vppcom_main;
461 vl_api_application_attach_t *bmp;
462 u8 nsid_len = vec_len (vcm->cfg.namespace_id);
464 bmp = vl_msg_api_alloc (sizeof (*bmp));
465 memset (bmp, 0, sizeof (*bmp));
467 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
468 bmp->client_index = vcm->my_client_index;
469 bmp->context = htonl (0xfeedface);
470 bmp->options[APP_OPTIONS_FLAGS] =
471 APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
472 (vcm->cfg.session_scope_local ? APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE : 0) |
473 (vcm->cfg.session_scope_global ? APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE : 0);
474 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
475 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
476 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
477 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
480 bmp->namespace_id_len = nsid_len;
481 clib_memcpy (bmp->namespace_id, vcm->cfg.namespace_id, nsid_len);
482 bmp->options[APP_OPTIONS_NAMESPACE_SECRET] = vcm->cfg.namespace_secret;
484 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
488 vppcom_app_attach (void)
490 vppcom_main_t *vcm = &vppcom_main;
493 vppcom_app_send_attach ();
494 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
495 if (PREDICT_FALSE (rv))
497 if (VPPCOM_DEBUG > 0)
498 clib_warning ("[%d] application attach timed out, rv = %s (%d)",
499 vcm->my_pid, vppcom_retval_str (rv), rv);
506 vppcom_app_detach (void)
508 vppcom_main_t *vcm = &vppcom_main;
509 vl_api_application_detach_t *bmp;
510 bmp = vl_msg_api_alloc (sizeof (*bmp));
511 memset (bmp, 0, sizeof (*bmp));
513 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
514 bmp->client_index = vcm->my_client_index;
515 bmp->context = htonl (0xfeedface);
516 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
520 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
523 vppcom_main_t *vcm = &vppcom_main;
524 static svm_fifo_segment_create_args_t _a;
525 svm_fifo_segment_create_args_t *a = &_a;
528 memset (a, 0, sizeof (*a));
531 clib_warning ("[%d] attach failed: %U", vcm->my_pid,
532 format_api_error, ntohl (mp->retval));
536 if (mp->segment_name_length == 0)
538 clib_warning ("[%d] segment_name_length zero", vcm->my_pid);
542 a->segment_name = (char *) mp->segment_name;
543 a->segment_size = mp->segment_size;
545 ASSERT (mp->app_event_queue_address);
547 /* Attach to the segment vpp created */
548 rv = svm_fifo_segment_attach (a);
549 vec_reset_length (a->new_segment_indices);
550 if (PREDICT_FALSE (rv))
552 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", vcm->my_pid,
557 vcm->app_event_queue =
558 uword_to_pointer (mp->app_event_queue_address,
559 unix_shared_memory_queue_t *);
561 vcm->app_state = STATE_APP_ATTACHED;
565 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
568 vppcom_main_t *vcm = &vppcom_main;
571 clib_warning ("[%d] detach failed: %U", vcm->my_pid, format_api_error,
574 vcm->app_state = STATE_APP_ENABLED;
578 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
581 vppcom_main_t *vcm = &vppcom_main;
584 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
587 session_t *session = 0;
589 clib_spinlock_lock (&vcm->sessions_lockp);
590 rv = vppcom_session_at_index (p[0], &session);
591 if (PREDICT_FALSE (rv))
593 if (VPPCOM_DEBUG > 1)
594 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
597 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
598 session->state = STATE_DISCONNECT;
599 clib_spinlock_unlock (&vcm->sessions_lockp);
603 if (VPPCOM_DEBUG > 1)
604 clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
609 clib_warning ("[%d] disconnect_session failed: %U", vcm->my_pid,
610 format_api_error, ntohl (mp->retval));
614 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
616 vppcom_main_t *vcm = &vppcom_main;
617 static svm_fifo_segment_create_args_t _a;
618 svm_fifo_segment_create_args_t *a = &_a;
621 memset (a, 0, sizeof (*a));
622 a->segment_name = (char *) mp->segment_name;
623 a->segment_size = mp->segment_size;
624 /* Attach to the segment vpp created */
625 rv = svm_fifo_segment_attach (a);
626 vec_reset_length (a->new_segment_indices);
627 if (PREDICT_FALSE (rv))
629 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed",
630 vcm->my_pid, mp->segment_name);
633 if (VPPCOM_DEBUG > 1)
634 clib_warning ("[%d] mapped new segment '%s' size %d", vcm->my_pid,
635 mp->segment_name, mp->segment_size);
639 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
641 vppcom_main_t *vcm = &vppcom_main;
642 session_t *session = 0;
643 vl_api_disconnect_session_reply_t *rmp;
647 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
651 clib_spinlock_lock (&vcm->sessions_lockp);
652 rval = vppcom_session_at_index (p[0], &session);
653 if (PREDICT_FALSE (rval))
655 if (VPPCOM_DEBUG > 1)
656 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
660 pool_put (vcm->sessions, session);
661 clib_spinlock_unlock (&vcm->sessions_lockp);
662 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
666 clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
671 rmp = vl_msg_api_alloc (sizeof (*rmp));
672 memset (rmp, 0, sizeof (*rmp));
674 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
675 rmp->retval = htonl (rv);
676 rmp->handle = mp->handle;
677 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
681 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
683 vppcom_main_t *vcm = &vppcom_main;
684 session_t *session = 0;
685 vl_api_reset_session_reply_t *rmp;
689 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
693 clib_spinlock_lock (&vcm->sessions_lockp);
694 rval = vppcom_session_at_index (p[0], &session);
695 if (PREDICT_FALSE (rval))
697 if (VPPCOM_DEBUG > 1)
698 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
702 pool_put (vcm->sessions, session);
703 clib_spinlock_unlock (&vcm->sessions_lockp);
704 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
708 clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
713 rmp = vl_msg_api_alloc (sizeof (*rmp));
714 memset (rmp, 0, sizeof (*rmp));
715 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
716 rmp->retval = htonl (rv);
717 rmp->handle = mp->handle;
718 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
722 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
724 vppcom_main_t *vcm = &vppcom_main;
727 svm_fifo_t *rx_fifo, *tx_fifo;
733 clib_warning ("[%d] connect failed: %U", vcm->my_pid, format_api_error,
738 session_index = mp->context;
739 if (VPPCOM_DEBUG > 1)
740 clib_warning ("[%d] session_index = %d 0x%08x", vcm->my_pid,
741 session_index, session_index);
743 clib_spinlock_lock (&vcm->sessions_lockp);
744 if (pool_is_free_index (vcm->sessions, session_index))
746 clib_spinlock_unlock (&vcm->sessions_lockp);
747 if (VPPCOM_DEBUG > 1)
748 clib_warning ("[%d] invalid session, sid %d is closed!",
749 vcm->my_pid, session_index);
753 /* We've been redirected */
754 if (mp->segment_name_length > 0)
756 static svm_fifo_segment_create_args_t _a;
757 svm_fifo_segment_create_args_t *a = &_a;
760 memset (a, 0, sizeof (*a));
761 a->segment_name = (char *) mp->segment_name;
762 if (VPPCOM_DEBUG > 1)
763 clib_warning ("[%d] cut-thru segment: %s", vcm->my_pid,
765 rv = svm_fifo_segment_attach (a);
766 vec_reset_length (a->new_segment_indices);
767 if (PREDICT_FALSE (rv))
769 clib_spinlock_unlock (&vcm->sessions_lockp);
770 clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed",
771 vcm->my_pid, a->segment_name);
779 if (VPPCOM_DEBUG > 1)
780 clib_warning ("[%d] client sid %d", vcm->my_pid, session_index);
782 session = pool_elt_at_index (vcm->sessions, session_index);
783 session->is_cut_thru = is_cut_thru;
784 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
785 unix_shared_memory_queue_t *);
787 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
788 rx_fifo->client_session_index = session_index;
789 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
790 tx_fifo->client_session_index = session_index;
792 session->server_rx_fifo = rx_fifo;
793 session->server_tx_fifo = tx_fifo;
794 session->vpp_session_handle = mp->handle;
795 session->state = STATE_CONNECT;
797 /* Add it to lookup table */
798 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
799 clib_spinlock_unlock (&vcm->sessions_lockp);
803 vppcom_send_connect_sock (session_t * session, u32 session_index)
805 vppcom_main_t *vcm = &vppcom_main;
806 vl_api_connect_sock_t *cmp;
808 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
809 session->is_server = 0;
810 cmp = vl_msg_api_alloc (sizeof (*cmp));
811 memset (cmp, 0, sizeof (*cmp));
812 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
813 cmp->client_index = vcm->my_client_index;
814 cmp->context = session_index;
816 if (VPPCOM_DEBUG > 1)
817 clib_warning ("[%d] session_index = %d 0x%08x",
818 vcm->my_pid, session_index, session_index);
820 cmp->vrf = session->vrf;
821 cmp->is_ip4 = session->peer_addr.is_ip4;
822 clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
823 cmp->port = session->peer_port;
824 cmp->proto = session->proto;
825 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
826 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
830 vppcom_send_disconnect (session_t * session)
832 vppcom_main_t *vcm = &vppcom_main;
833 vl_api_disconnect_session_t *dmp;
835 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
836 dmp = vl_msg_api_alloc (sizeof (*dmp));
837 memset (dmp, 0, sizeof (*dmp));
838 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
839 dmp->client_index = vcm->my_client_index;
840 dmp->handle = session->vpp_session_handle;
841 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
845 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
847 vppcom_main_t *vcm = &vppcom_main;
848 session_t *session = 0;
852 clib_warning ("[%d] bind failed: %U", vcm->my_pid, format_api_error,
855 ASSERT (vcm->bind_session_index != ~0);
857 clib_spinlock_lock (&vcm->sessions_lockp);
858 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
861 session->vpp_session_handle = mp->handle;
862 hash_set (vcm->session_index_by_vpp_handles, mp->handle,
863 vcm->bind_session_index);
864 session->state = mp->retval ? STATE_FAILED : STATE_LISTEN;
865 vcm->bind_session_index = ~0;
867 clib_spinlock_unlock (&vcm->sessions_lockp);
871 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
873 vppcom_main_t *vcm = &vppcom_main;
874 session_t *session = 0;
877 clib_spinlock_lock (&vcm->sessions_lockp);
878 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
881 if ((VPPCOM_DEBUG > 1) && (mp->retval))
882 clib_warning ("[%d] unbind failed: %U", vcm->my_pid, format_api_error,
885 vcm->bind_session_index = ~0;
886 session->state = STATE_START;
888 clib_spinlock_unlock (&vcm->sessions_lockp);
892 format_ip4_address (u8 * s, va_list * args)
894 u8 *a = va_arg (*args, u8 *);
895 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
899 format_ip6_address (u8 * s, va_list * args)
901 ip6_address_t *a = va_arg (*args, ip6_address_t *);
902 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
904 i_max_n_zero = ARRAY_LEN (a->as_u16);
906 i_first_zero = i_max_n_zero;
908 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
910 u32 is_zero = a->as_u16[i] == 0;
911 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
917 if ((!is_zero && n_zeros > max_n_zeros)
918 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
920 i_max_n_zero = i_first_zero;
921 max_n_zeros = n_zeros;
922 i_first_zero = ARRAY_LEN (a->as_u16);
927 last_double_colon = 0;
928 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
930 if (i == i_max_n_zero && max_n_zeros > 1)
932 s = format (s, "::");
933 i += max_n_zeros - 1;
934 last_double_colon = 1;
938 s = format (s, "%s%x",
939 (last_double_colon || i == 0) ? "" : ":",
940 clib_net_to_host_u16 (a->as_u16[i]));
941 last_double_colon = 0;
948 /* Format an IP46 address. */
950 format_ip46_address (u8 * s, va_list * args)
952 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
953 ip46_type_t type = va_arg (*args, ip46_type_t);
959 is_ip4 = ip46_address_is_ip4 (ip46);
970 format (s, "%U", format_ip4_address, &ip46->ip4) :
971 format (s, "%U", format_ip6_address, &ip46->ip6);
975 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
977 vppcom_main_t *vcm = &vppcom_main;
978 vl_api_accept_session_reply_t *rmp;
979 svm_fifo_t *rx_fifo, *tx_fifo;
984 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
986 clib_warning ("[%d] client session queue is full!", vcm->my_pid);
987 rv = VNET_API_ERROR_QUEUE_FULL;
991 if (VPPCOM_DEBUG > 1)
993 u8 *ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
994 clib_warning ("[%d] accepted session from: %s:%d", vcm->my_pid, ip_str,
995 clib_net_to_host_u16 (mp->port));
999 clib_spinlock_lock (&vcm->sessions_lockp);
1000 /* Allocate local session and set it up */
1001 pool_get (vcm->sessions, session);
1002 memset (session, 0, sizeof (*session));
1003 session_index = session - vcm->sessions;
1005 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
1006 rx_fifo->client_session_index = session_index;
1007 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
1008 tx_fifo->client_session_index = session_index;
1010 session->server_rx_fifo = rx_fifo;
1011 session->server_tx_fifo = tx_fifo;
1012 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
1013 unix_shared_memory_queue_t *);
1014 session->state = STATE_ACCEPT;
1015 session->is_cut_thru = 0;
1016 session->is_server = 1;
1017 session->peer_port = mp->port;
1018 session->peer_addr.is_ip4 = mp->is_ip4;
1019 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1020 sizeof (session->peer_addr.ip46));
1022 /* Add it to lookup table */
1023 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1025 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1026 clib_spinlock_unlock (&vcm->sessions_lockp);
1029 * Send accept reply to vpp
1032 rmp = vl_msg_api_alloc (sizeof (*rmp));
1033 memset (rmp, 0, sizeof (*rmp));
1034 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1035 rmp->retval = htonl (rv);
1036 rmp->handle = mp->handle;
1037 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1041 * Acting as server for redirected connect requests
1044 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1046 static svm_fifo_segment_create_args_t _a;
1047 svm_fifo_segment_create_args_t *a = &_a;
1048 vppcom_main_t *vcm = &vppcom_main;
1050 svm_fifo_segment_private_t *seg;
1051 unix_shared_memory_queue_t *client_q;
1052 vl_api_connect_session_reply_t *rmp;
1053 session_t *session = 0;
1055 svm_fifo_t *rx_fifo;
1056 svm_fifo_t *tx_fifo;
1057 unix_shared_memory_queue_t *event_q = 0;
1059 clib_spinlock_lock (&vcm->sessions_lockp);
1060 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1062 if (VPPCOM_DEBUG > 1)
1063 clib_warning ("[%d] client session queue is full!", vcm->my_pid);
1064 rv = VNET_API_ERROR_QUEUE_FULL;
1065 clib_spinlock_unlock (&vcm->sessions_lockp);
1069 /* Create the segment */
1070 memset (a, 0, sizeof (*a));
1071 a->segment_name = (char *) format ((u8 *) a->segment_name, "%d:segment%d%c",
1072 vcm->my_pid, vcm->unique_segment_index++,
1074 a->segment_size = vcm->cfg.segment_size;
1075 a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
1076 a->rx_fifo_size = vcm->cfg.rx_fifo_size;
1077 a->tx_fifo_size = vcm->cfg.tx_fifo_size;
1079 rv = svm_fifo_segment_create (a);
1080 if (PREDICT_FALSE (rv))
1082 if (VPPCOM_DEBUG > 1)
1083 clib_warning ("[%d] svm_fifo_segment_create ('%s') failed",
1084 vcm->my_pid, a->segment_name);
1085 vec_reset_length (a->new_segment_indices);
1086 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
1090 if (VPPCOM_DEBUG > 1)
1091 clib_warning ("[%d] created segment '%s'", vcm->my_pid, a->segment_name);
1093 pool_get (vcm->sessions, session);
1094 memset (session, 0, sizeof (*session));
1095 session_index = session - vcm->sessions;
1097 session->sm_seg_index = a->new_segment_indices[0];
1098 vec_reset_length (a->new_segment_indices);
1100 seg = svm_fifo_segment_get_segment (session->sm_seg_index);
1101 rx_fifo = session->server_rx_fifo =
1102 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
1103 FIFO_SEGMENT_RX_FREELIST);
1104 if (PREDICT_FALSE (!session->server_rx_fifo))
1106 svm_fifo_segment_delete (seg);
1107 clib_warning ("[%d] rx fifo alloc failed, size %ld (0x%lx)",
1108 vcm->my_pid, vcm->cfg.rx_fifo_size,
1109 vcm->cfg.rx_fifo_size);
1110 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
1111 clib_spinlock_unlock (&vcm->sessions_lockp);
1115 tx_fifo = session->server_tx_fifo =
1116 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
1117 FIFO_SEGMENT_TX_FREELIST);
1118 if (PREDICT_FALSE (!session->server_tx_fifo))
1120 svm_fifo_segment_delete (seg);
1121 if (VPPCOM_DEBUG > 1)
1122 clib_warning ("[%d] tx fifo alloc failed, size %ld (0x%lx)",
1123 vcm->my_pid, vcm->cfg.tx_fifo_size,
1124 vcm->cfg.tx_fifo_size);
1125 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
1126 clib_spinlock_unlock (&vcm->sessions_lockp);
1130 session->server_rx_fifo->master_session_index = session_index;
1131 session->server_tx_fifo->master_session_index = session_index;
1132 session->client_queue_address = mp->client_queue_address;
1133 session->is_cut_thru = 1;
1134 session->is_server = 1;
1135 session->peer_port = mp->port;
1136 session->peer_addr.is_ip4 = mp->is_ip4;
1137 clib_memcpy (&session->peer_addr.ip46, mp->ip,
1138 sizeof (session->peer_addr.ip46));
1141 ssvm_shared_header_t *sh = seg->ssvm.sh;
1143 ssvm_lock_non_recursive (sh, 1);
1144 oldheap = ssvm_push_heap (sh);
1145 event_q = session->vpp_event_queue =
1146 unix_shared_memory_queue_init (vcm->cfg.event_queue_size,
1147 sizeof (session_fifo_event_t),
1148 vcm->my_pid, 0 /* signal not sent */ );
1149 ssvm_pop_heap (oldheap);
1150 ssvm_unlock_non_recursive (sh);
1153 session->state = STATE_ACCEPT;
1154 if (VPPCOM_DEBUG > 1)
1155 clib_warning ("[%d] Connected cut-thru to client: sid %d",
1156 vcm->my_pid, session_index);
1157 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1158 clib_spinlock_unlock (&vcm->sessions_lockp);
1161 rmp = vl_msg_api_alloc (sizeof (*rmp));
1162 memset (rmp, 0, sizeof (*rmp));
1164 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
1165 rmp->context = mp->context;
1166 rmp->retval = htonl (rv);
1167 rmp->segment_name_length = vec_len (a->segment_name);
1168 clib_memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name));
1169 vec_reset_length (a->segment_name);
1173 rmp->vpp_event_queue_address = pointer_to_uword (event_q);
1174 rmp->server_rx_fifo = pointer_to_uword (rx_fifo);
1175 rmp->server_tx_fifo = pointer_to_uword (tx_fifo);
1178 uword_to_pointer (mp->client_queue_address, unix_shared_memory_queue_t *);
1181 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
1185 vppcom_send_bind_sock (session_t * session)
1187 vppcom_main_t *vcm = &vppcom_main;
1188 vl_api_bind_sock_t *bmp;
1190 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1191 session->is_server = 1;
1192 bmp = vl_msg_api_alloc (sizeof (*bmp));
1193 memset (bmp, 0, sizeof (*bmp));
1195 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1196 bmp->client_index = vcm->my_client_index;
1197 bmp->context = htonl (0xfeedface);
1198 bmp->vrf = session->vrf;
1199 bmp->is_ip4 = session->lcl_addr.is_ip4;
1200 clib_memcpy (bmp->ip, &session->lcl_addr.ip46, sizeof (bmp->ip));
1201 bmp->port = session->lcl_port;
1202 bmp->proto = session->proto;
1203 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1204 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1208 vppcom_send_unbind_sock (u32 session_index)
1210 vppcom_main_t *vcm = &vppcom_main;
1211 vl_api_unbind_sock_t *ump;
1212 session_t *session = 0;
1215 clib_spinlock_lock (&vcm->sessions_lockp);
1216 rv = vppcom_session_at_index (session_index, &session);
1217 if (PREDICT_FALSE (rv))
1219 clib_spinlock_unlock (&vcm->sessions_lockp);
1220 if (VPPCOM_DEBUG > 0)
1221 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1222 vcm->my_pid, session_index);
1226 ump = vl_msg_api_alloc (sizeof (*ump));
1227 memset (ump, 0, sizeof (*ump));
1229 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1230 ump->client_index = vcm->my_client_index;
1231 ump->handle = session->vpp_session_handle;
1232 clib_spinlock_unlock (&vcm->sessions_lockp);
1233 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1237 vppcom_session_unbind_cut_thru (session_t * session)
1239 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1240 svm_fifo_segment_private_t *seg;
1243 seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
1244 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1245 FIFO_SEGMENT_RX_FREELIST);
1246 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1247 FIFO_SEGMENT_TX_FREELIST);
1248 svm_fifo_segment_delete (seg);
1254 vppcom_session_unbind (u32 session_index)
1256 vppcom_main_t *vcm = &vppcom_main;
1259 clib_spinlock_lock (&vcm->sessions_lockp);
1260 if (PREDICT_FALSE (pool_is_free_index (vcm->sessions, session_index)))
1262 clib_spinlock_unlock (&vcm->sessions_lockp);
1263 if (VPPCOM_DEBUG > 1)
1264 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1265 vcm->my_pid, session_index);
1266 return VPPCOM_EBADFD;
1268 clib_spinlock_unlock (&vcm->sessions_lockp);
1270 vcm->bind_session_index = session_index;
1271 vppcom_send_unbind_sock (session_index);
1272 rv = vppcom_wait_for_session_state_change (session_index, STATE_START,
1273 vcm->cfg.session_timeout);
1274 if (PREDICT_FALSE (rv))
1276 vcm->bind_session_index = ~0;
1277 if (VPPCOM_DEBUG > 0)
1278 clib_warning ("[%d] server unbind timed out, rv = %s (%d)",
1279 vcm->my_pid, vppcom_retval_str (rv), rv);
1286 vppcom_session_disconnect (u32 session_index)
1288 vppcom_main_t *vcm = &vppcom_main;
1292 clib_spinlock_lock (&vcm->sessions_lockp);
1293 rv = vppcom_session_at_index (session_index, &session);
1294 if (PREDICT_FALSE (rv))
1296 clib_spinlock_unlock (&vcm->sessions_lockp);
1297 if (VPPCOM_DEBUG > 1)
1298 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1299 vcm->my_pid, session_index);
1303 if (!session->is_cut_thru)
1305 vppcom_send_disconnect (session);
1306 clib_spinlock_unlock (&vcm->sessions_lockp);
1308 rv = vppcom_wait_for_session_state_change (session_index,
1309 STATE_DISCONNECT, 1.0);
1310 if ((VPPCOM_DEBUG > 0) && (rv < 0))
1311 clib_warning ("[%d] disconnect (session %d) failed, rv = %s (%d)",
1312 vcm->my_pid, session_index, vppcom_retval_str (rv), rv);
1315 clib_spinlock_unlock (&vcm->sessions_lockp);
1320 #define foreach_sock_msg \
1321 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1322 _(BIND_SOCK_REPLY, bind_sock_reply) \
1323 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1324 _(ACCEPT_SESSION, accept_session) \
1325 _(CONNECT_SOCK, connect_sock) \
1326 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1327 _(DISCONNECT_SESSION, disconnect_session) \
1328 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1329 _(RESET_SESSION, reset_session) \
1330 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1331 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1332 _(MAP_ANOTHER_SEGMENT, map_another_segment)
1335 vppcom_api_hookup (void)
1338 vl_msg_api_set_handlers(VL_API_##N, #n, \
1339 vl_api_##n##_t_handler, \
1341 vl_api_##n##_t_endian, \
1342 vl_api_##n##_t_print, \
1343 sizeof(vl_api_##n##_t), 1);
1349 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1353 vcl_cfg->heapsize = (256ULL << 20);
1354 vcl_cfg->segment_baseva = 0x200000000ULL;
1355 vcl_cfg->segment_size = (256 << 20);
1356 vcl_cfg->add_segment_size = (128 << 20);
1357 vcl_cfg->preallocated_fifo_pairs = 8;
1358 vcl_cfg->rx_fifo_size = (1 << 20);
1359 vcl_cfg->tx_fifo_size = (1 << 20);
1360 vcl_cfg->event_queue_size = 2048;
1361 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1362 vcl_cfg->app_timeout = 10 * 60.0;
1363 vcl_cfg->session_timeout = 10 * 60.0;
1364 vcl_cfg->accept_timeout = 60.0;
1368 vppcom_cfg_heapsize (char *conf_fname)
1370 vppcom_main_t *vcm = &vppcom_main;
1371 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1382 fp = fopen (conf_fname, "r");
1385 if (VPPCOM_DEBUG > 0)
1386 fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1389 argv = calloc (1, sizeof (char *));
1395 if (fgets (inbuf, 4096, fp) == 0)
1397 p = strtok (inbuf, " \t\n");
1403 char **tmp = realloc (argv, argc * sizeof (char *));
1407 arg = strndup (p, 1024);
1410 argv[argc - 1] = arg;
1411 p = strtok (NULL, " \t\n");
1418 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1425 * Look for and parse the "heapsize" config parameter.
1426 * Manual since none of the clib infra has been bootstrapped yet.
1428 * Format: heapsize <nn>[mM][gG]
1431 for (i = 1; i < (argc - 1); i++)
1433 if (!strncmp (argv[i], "heapsize", 8))
1435 sizep = (u8 *) argv[i + 1];
1437 while (*sizep >= '0' && *sizep <= '9')
1440 size += *sizep++ - '0';
1444 if (VPPCOM_DEBUG > 0)
1445 clib_warning ("[%d] parse error '%s %s', "
1446 "using default heapsize %lld (0x%llx)",
1447 vcm->my_pid, argv[i], argv[i + 1],
1448 vcl_cfg->heapsize, vcl_cfg->heapsize);
1452 if (*sizep == 'g' || *sizep == 'G')
1453 vcl_cfg->heapsize = size << 30;
1454 else if (*sizep == 'm' || *sizep == 'M')
1455 vcl_cfg->heapsize = size << 20;
1458 if (VPPCOM_DEBUG > 0)
1459 clib_warning ("[%d] parse error '%s %s', "
1460 "using default heapsize %lld (0x%llx)",
1461 vcm->my_pid, argv[i], argv[i + 1],
1462 vcl_cfg->heapsize, vcl_cfg->heapsize);
1473 if (!clib_mem_init (0, vcl_cfg->heapsize))
1474 clib_warning ("[%d] vppcom heap allocation failure!", vcm->my_pid);
1475 else if (VPPCOM_DEBUG > 0)
1476 clib_warning ("[%d] allocated vppcom heapsize %lld (0x%llx)",
1477 vcm->my_pid, vcl_cfg->heapsize, vcl_cfg->heapsize);
1481 vppcom_cfg_read (char *conf_fname)
1483 vppcom_main_t *vcm = &vppcom_main;
1484 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1486 unformat_input_t _input, *input = &_input;
1487 unformat_input_t _line_input, *line_input = &_line_input;
1488 u8 vc_cfg_input = 0;
1493 fd = open (conf_fname, O_RDONLY);
1496 if (VPPCOM_DEBUG > 0)
1497 clib_warning ("[%d] open configuration file '%s' failed!",
1498 vcm->my_pid, conf_fname);
1502 if (fstat (fd, &s) < 0)
1504 if (VPPCOM_DEBUG > 0)
1505 clib_warning ("[%d] failed to stat `%s'", vcm->my_pid, conf_fname);
1509 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1511 if (VPPCOM_DEBUG > 0)
1512 clib_warning ("[%d] not a regular file `%s'", vcm->my_pid,
1517 unformat_init_clib_file (input, fd);
1519 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1521 (void) unformat_user (input, unformat_line_input, line_input);
1522 unformat_skip_white_space (line_input);
1524 if (unformat (line_input, "vcl {"))
1532 if (unformat (line_input, "heapsize %s", &chroot_path))
1534 vec_terminate_c_string (chroot_path);
1535 if (VPPCOM_DEBUG > 0)
1536 clib_warning ("[%d] configured heapsize %s, "
1537 "actual heapsize %lld (0x%llx)",
1538 vcm->my_pid, chroot_path, vcl_cfg->heapsize,
1540 vec_free (chroot_path);
1542 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1544 vec_terminate_c_string (chroot_path);
1545 vl_set_memory_root_path ((char *) chroot_path);
1546 if (VPPCOM_DEBUG > 0)
1547 clib_warning ("[%d] configured api-prefix %s",
1548 vcm->my_pid, chroot_path);
1549 chroot_path = 0; /* Don't vec_free() it! */
1551 else if (unformat (line_input, "uid %d", &uid))
1553 vl_set_memory_uid (uid);
1554 if (VPPCOM_DEBUG > 0)
1555 clib_warning ("[%d] configured uid %d", vcm->my_pid, uid);
1557 else if (unformat (line_input, "gid %d", &gid))
1559 vl_set_memory_gid (gid);
1560 if (VPPCOM_DEBUG > 0)
1561 clib_warning ("[%d] configured gid %d", vcm->my_pid, gid);
1563 else if (unformat (line_input, "segment-baseva 0x%lx",
1564 &vcl_cfg->segment_baseva))
1566 if (VPPCOM_DEBUG > 0)
1567 clib_warning ("[%d] configured segment_baseva 0x%lx",
1568 vcm->my_pid, vcl_cfg->segment_baseva);
1570 else if (unformat (line_input, "segment-size 0x%lx",
1571 &vcl_cfg->segment_size))
1573 if (VPPCOM_DEBUG > 0)
1574 clib_warning ("[%d] configured segment_size 0x%lx (%ld)",
1575 vcm->my_pid, vcl_cfg->segment_size,
1576 vcl_cfg->segment_size);
1578 else if (unformat (line_input, "segment-size %ld",
1579 &vcl_cfg->segment_size))
1581 if (VPPCOM_DEBUG > 0)
1582 clib_warning ("[%d] configured segment_size %ld (0x%lx)",
1583 vcm->my_pid, vcl_cfg->segment_size,
1584 vcl_cfg->segment_size);
1586 else if (unformat (line_input, "add-segment-size 0x%lx",
1587 &vcl_cfg->add_segment_size))
1589 if (VPPCOM_DEBUG > 0)
1591 ("[%d] configured add_segment_size 0x%lx (%ld)",
1592 vcm->my_pid, vcl_cfg->add_segment_size,
1593 vcl_cfg->add_segment_size);
1595 else if (unformat (line_input, "add-segment-size %ld",
1596 &vcl_cfg->add_segment_size))
1598 if (VPPCOM_DEBUG > 0)
1600 ("[%d] configured add_segment_size %ld (0x%lx)",
1601 vcm->my_pid, vcl_cfg->add_segment_size,
1602 vcl_cfg->add_segment_size);
1604 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1605 &vcl_cfg->preallocated_fifo_pairs))
1607 if (VPPCOM_DEBUG > 0)
1608 clib_warning ("[%d] configured preallocated_fifo_pairs "
1609 "%d (0x%x)", vcm->my_pid,
1610 vcl_cfg->preallocated_fifo_pairs,
1611 vcl_cfg->preallocated_fifo_pairs);
1613 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1614 &vcl_cfg->rx_fifo_size))
1616 if (VPPCOM_DEBUG > 0)
1617 clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)",
1618 vcm->my_pid, vcl_cfg->rx_fifo_size,
1619 vcl_cfg->rx_fifo_size);
1621 else if (unformat (line_input, "rx-fifo-size %ld",
1622 &vcl_cfg->rx_fifo_size))
1624 if (VPPCOM_DEBUG > 0)
1625 clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)",
1626 vcm->my_pid, vcl_cfg->rx_fifo_size,
1627 vcl_cfg->rx_fifo_size);
1629 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1630 &vcl_cfg->tx_fifo_size))
1632 if (VPPCOM_DEBUG > 0)
1633 clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)",
1634 vcm->my_pid, vcl_cfg->tx_fifo_size,
1635 vcl_cfg->tx_fifo_size);
1637 else if (unformat (line_input, "tx-fifo-size %ld",
1638 &vcl_cfg->tx_fifo_size))
1640 if (VPPCOM_DEBUG > 0)
1641 clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)",
1642 vcm->my_pid, vcl_cfg->tx_fifo_size,
1643 vcl_cfg->tx_fifo_size);
1645 else if (unformat (line_input, "event-queue-size 0x%lx",
1646 &vcl_cfg->event_queue_size))
1648 if (VPPCOM_DEBUG > 0)
1649 clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)",
1650 vcm->my_pid, vcl_cfg->event_queue_size,
1651 vcl_cfg->event_queue_size);
1653 else if (unformat (line_input, "event-queue-size %ld",
1654 &vcl_cfg->event_queue_size))
1656 if (VPPCOM_DEBUG > 0)
1657 clib_warning ("[%d] configured event_queue_size %ld (0x%lx)",
1658 vcm->my_pid, vcl_cfg->event_queue_size,
1659 vcl_cfg->event_queue_size);
1661 else if (unformat (line_input, "listen-queue-size 0x%lx",
1662 &vcl_cfg->listen_queue_size))
1664 if (VPPCOM_DEBUG > 0)
1665 clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)",
1666 vcm->my_pid, vcl_cfg->listen_queue_size,
1667 vcl_cfg->listen_queue_size);
1669 else if (unformat (line_input, "listen-queue-size %ld",
1670 &vcl_cfg->listen_queue_size))
1672 if (VPPCOM_DEBUG > 0)
1673 clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)",
1674 vcm->my_pid, vcl_cfg->listen_queue_size,
1675 vcl_cfg->listen_queue_size);
1677 else if (unformat (line_input, "app-timeout %f",
1678 &vcl_cfg->app_timeout))
1680 if (VPPCOM_DEBUG > 0)
1681 clib_warning ("[%d] configured app_timeout %f",
1682 vcm->my_pid, vcl_cfg->app_timeout);
1684 else if (unformat (line_input, "session-timeout %f",
1685 &vcl_cfg->session_timeout))
1687 if (VPPCOM_DEBUG > 0)
1688 clib_warning ("[%d] configured session_timeout %f",
1689 vcm->my_pid, vcl_cfg->session_timeout);
1691 else if (unformat (line_input, "accept-timeout %f",
1692 &vcl_cfg->accept_timeout))
1694 if (VPPCOM_DEBUG > 0)
1695 clib_warning ("[%d] configured accept_timeout %f",
1696 vcm->my_pid, vcl_cfg->accept_timeout);
1698 else if (unformat (line_input, "session-scope-local"))
1700 vcl_cfg->session_scope_local = 1;
1701 if (VPPCOM_DEBUG > 0)
1702 clib_warning ("[%d] configured session_scope_local (%d)",
1703 vcm->my_pid, vcl_cfg->session_scope_local);
1705 else if (unformat (line_input, "session-scope-global"))
1707 vcl_cfg->session_scope_global = 1;
1708 if (VPPCOM_DEBUG > 0)
1709 clib_warning ("[%d] configured session_scope_global (%d)",
1710 vcm->my_pid, vcl_cfg->session_scope_global);
1712 else if (unformat (line_input, "namespace-secret 0x%lx",
1713 &vcl_cfg->namespace_secret))
1715 if (VPPCOM_DEBUG > 0)
1717 ("[%d] configured namespace_secret 0x%lx (%lu)",
1718 vcm->my_pid, vcl_cfg->namespace_secret,
1719 vcl_cfg->namespace_secret);
1721 else if (unformat (line_input, "namespace-secret %lu",
1722 &vcl_cfg->namespace_secret))
1724 if (VPPCOM_DEBUG > 0)
1726 ("[%d] configured namespace_secret %lu (0x%lx)",
1727 vcm->my_pid, vcl_cfg->namespace_secret,
1728 vcl_cfg->namespace_secret);
1730 else if (unformat (line_input, "namespace-id %v",
1731 &vcl_cfg->namespace_id))
1733 vl_api_application_attach_t *mp;
1734 u32 max_nsid_vec_len = sizeof (mp->namespace_id) - 1;
1735 u32 nsid_vec_len = vec_len (vcl_cfg->namespace_id);
1736 if (nsid_vec_len > max_nsid_vec_len)
1738 _vec_len (vcl_cfg->namespace_id) = max_nsid_vec_len;
1739 if (VPPCOM_DEBUG > 0)
1740 clib_warning ("[%d] configured namespace_id is too long,"
1741 " truncated to %d characters!", vcm->my_pid,
1745 if (VPPCOM_DEBUG > 0)
1746 clib_warning ("[%d] configured namespace_id %v",
1747 vcm->my_pid, vcl_cfg->namespace_id);
1749 else if (unformat (line_input, "}"))
1752 if (VPPCOM_DEBUG > 0)
1753 clib_warning ("[%d] completed parsing vppcom config!",
1759 if (line_input->buffer[line_input->index] != '#')
1761 clib_warning ("[%d] Unknown vppcom config option: '%s'",
1762 vcm->my_pid, (char *)
1763 &line_input->buffer[line_input->index]);
1770 unformat_free (input);
1778 * VPPCOM Public API functions
1781 vppcom_app_create (char *app_name)
1783 vppcom_main_t *vcm = &vppcom_main;
1784 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1795 vcm->my_pid = getpid ();
1796 clib_fifo_validate (vcm->client_session_index_fifo,
1797 vcm->cfg.listen_queue_size);
1798 vppcom_cfg_init (vcl_cfg);
1799 conf_fname = getenv (VPPCOM_ENV_CONF);
1802 conf_fname = VPPCOM_CONF_DEFAULT;
1803 if (VPPCOM_DEBUG > 0)
1804 clib_warning ("[%d] getenv '%s' failed!", vcm->my_pid,
1807 vppcom_cfg_heapsize (conf_fname);
1808 vppcom_cfg_read (conf_fname);
1809 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_ID);
1812 u32 ns_id_vec_len = strlen (env_var_str);
1814 vec_reset_length (vcm->cfg.namespace_id);
1815 vec_validate (vcm->cfg.namespace_id, ns_id_vec_len - 1);
1816 clib_memcpy (vcm->cfg.namespace_id, env_var_str, ns_id_vec_len);
1818 if (VPPCOM_DEBUG > 0)
1819 clib_warning ("[%d] configured namespace_id (%v) from "
1820 VPPCOM_ENV_APP_NAMESPACE_ID "!", vcm->my_pid,
1821 vcm->cfg.namespace_id);
1823 env_var_str = getenv (VPPCOM_ENV_APP_NAMESPACE_SECRET);
1827 if (sscanf (env_var_str, "%lu", &tmp) != 1)
1828 clib_warning ("[%d] Invalid namespace secret specified in "
1829 "the environment variable "
1830 VPPCOM_ENV_APP_NAMESPACE_SECRET
1831 " (%s)!\n", vcm->my_pid, env_var_str);
1834 vcm->cfg.namespace_secret = tmp;
1835 if (VPPCOM_DEBUG > 0)
1836 clib_warning ("[%d] configured namespace secret (%lu) from "
1837 VPPCOM_ENV_APP_NAMESPACE_ID "!", vcm->my_pid,
1838 vcm->cfg.namespace_secret);
1841 if (getenv (VPPCOM_ENV_SESSION_SCOPE_LOCAL))
1843 vcm->cfg.session_scope_local = 1;
1844 if (VPPCOM_DEBUG > 0)
1845 clib_warning ("[%d] configured session_scope_local (%u) from "
1846 VPPCOM_ENV_SESSION_SCOPE_LOCAL "!", vcm->my_pid,
1847 vcm->cfg.session_scope_local);
1849 if (getenv (VPPCOM_ENV_SESSION_SCOPE_GLOBAL))
1851 vcm->cfg.session_scope_global = 1;
1852 if (VPPCOM_DEBUG > 0)
1853 clib_warning ("[%d] configured session_scope_global (%u) from "
1854 VPPCOM_ENV_SESSION_SCOPE_GLOBAL "!", vcm->my_pid,
1855 vcm->cfg.session_scope_global);
1858 vcm->bind_session_index = ~0;
1859 vcm->main_cpu = os_get_thread_index ();
1860 heap = clib_mem_get_per_cpu_heap ();
1861 h = mheap_header (heap);
1863 /* make the main heap thread-safe */
1864 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1866 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1868 clib_time_init (&vcm->clib_time);
1869 vppcom_init_error_string_table ();
1870 svm_fifo_segment_init (vcl_cfg->segment_baseva,
1871 20 /* timeout in secs */ );
1872 clib_spinlock_init (&vcm->sessions_lockp);
1873 vppcom_api_hookup ();
1876 if (vcm->my_client_index == ~0)
1878 vcm->app_state = STATE_APP_START;
1879 rv = vppcom_connect_to_vpp (app_name);
1882 clib_warning ("[%d] couldn't connect to VPP.", vcm->my_pid);
1886 if (VPPCOM_DEBUG > 0)
1887 clib_warning ("[%d] sending session enable", vcm->my_pid);
1889 rv = vppcom_app_session_enable ();
1892 clib_warning ("[%d] vppcom_app_session_enable() failed!",
1897 if (VPPCOM_DEBUG > 0)
1898 clib_warning ("[%d] sending app attach", vcm->my_pid);
1900 rv = vppcom_app_attach ();
1903 clib_warning ("[%d] vppcom_app_attach() failed!", vcm->my_pid);
1907 if (VPPCOM_DEBUG > 0)
1908 clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)",
1909 vcm->my_pid, app_name, vcm->my_client_index,
1910 vcm->my_client_index);
1917 vppcom_app_destroy (void)
1919 vppcom_main_t *vcm = &vppcom_main;
1922 if (vcm->my_client_index == ~0)
1925 if (VPPCOM_DEBUG > 0)
1926 clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)",
1927 vcm->my_pid, vcm->my_client_index, vcm->my_client_index);
1929 vppcom_app_detach ();
1930 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
1931 if (PREDICT_FALSE (rv))
1933 if (VPPCOM_DEBUG > 0)
1934 clib_warning ("[%d] application detach timed out, rv = %s (%d)",
1935 vcm->my_pid, vppcom_retval_str (rv), rv);
1937 vl_client_disconnect_from_vlib ();
1938 vcm->my_client_index = ~0;
1939 vcm->app_state = STATE_APP_START;
1943 vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
1945 vppcom_main_t *vcm = &vppcom_main;
1949 clib_spinlock_lock (&vcm->sessions_lockp);
1950 pool_get (vcm->sessions, session);
1951 memset (session, 0, sizeof (*session));
1952 session_index = session - vcm->sessions;
1955 session->proto = proto;
1956 session->state = STATE_START;
1957 session->is_nonblocking = is_nonblocking ? 1 : 0;
1958 clib_spinlock_unlock (&vcm->sessions_lockp);
1960 if (VPPCOM_DEBUG > 0)
1961 clib_warning ("[%d] sid %d", vcm->my_pid, session_index);
1963 return (int) session_index;
1967 vppcom_session_close (uint32_t session_index)
1969 vppcom_main_t *vcm = &vppcom_main;
1970 session_t *session = 0;
1979 session_state_t state;
1981 clib_spinlock_lock (&vcm->sessions_lockp);
1982 rv = vppcom_session_at_index (session_index, &session);
1983 if (PREDICT_FALSE (rv))
1985 if (VPPCOM_DEBUG > 0)
1986 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1987 vcm->my_pid, session_index);
1988 clib_spinlock_unlock (&vcm->sessions_lockp);
1991 is_server = session->is_server;
1992 is_listen = session->is_listen;
1993 is_cut_thru = session->is_cut_thru;
1994 is_vep = session->is_vep;
1995 is_vep_session = session->is_vep_session;
1996 next_sid = session->vep.next_sid;
1997 vep_idx = session->vep.vep_idx;
1998 state = session->state;
1999 clib_spinlock_unlock (&vcm->sessions_lockp);
2001 if (VPPCOM_DEBUG > 0)
2002 clib_warning ("[%d] sid %d", vcm->my_pid, session_index);
2006 while (next_sid != ~0)
2008 rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
2009 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2010 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
2011 "rv = %s (%d)", vcm->my_pid, vep_idx, next_sid,
2012 vppcom_retval_str (rv), rv);
2014 clib_spinlock_lock (&vcm->sessions_lockp);
2015 rv = vppcom_session_at_index (session_index, &session);
2016 if (PREDICT_FALSE (rv))
2018 if (VPPCOM_DEBUG > 0)
2020 ("[%d] invalid session, sid (%u) has been closed!",
2021 vcm->my_pid, session_index);
2022 clib_spinlock_unlock (&vcm->sessions_lockp);
2025 next_sid = session->vep.next_sid;
2026 clib_spinlock_unlock (&vcm->sessions_lockp);
2033 rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
2034 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2035 clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
2036 "rv = %s (%d)", vcm->my_pid, vep_idx, session_index,
2037 vppcom_retval_str (rv), rv);
2040 if (is_cut_thru && is_server && (state == STATE_ACCEPT))
2042 rv = vppcom_session_unbind_cut_thru (session);
2043 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2044 clib_warning ("[%d] unbind cut-thru (session %d) failed, "
2046 vcm->my_pid, session_index,
2047 vppcom_retval_str (rv), rv);
2049 else if (is_server && is_listen)
2051 rv = vppcom_session_unbind (session_index);
2052 if ((VPPCOM_DEBUG > 0) && (rv < 0))
2053 clib_warning ("[%d] unbind (session %d) failed, rv = %s (%d)",
2054 vcm->my_pid, session_index,
2055 vppcom_retval_str (rv), rv);
2057 else if (state == STATE_CONNECT)
2058 if (vppcom_session_disconnect (session_index))
2061 clib_spinlock_lock (&vcm->sessions_lockp);
2062 pool_put_index (vcm->sessions, session_index);
2063 clib_spinlock_unlock (&vcm->sessions_lockp);
2069 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
2071 vppcom_main_t *vcm = &vppcom_main;
2072 session_t *session = 0;
2076 return VPPCOM_EINVAL;
2078 clib_spinlock_lock (&vcm->sessions_lockp);
2079 rv = vppcom_session_at_index (session_index, &session);
2080 if (PREDICT_FALSE (rv))
2082 clib_spinlock_unlock (&vcm->sessions_lockp);
2083 if (VPPCOM_DEBUG > 0)
2084 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2085 vcm->my_pid, session_index);
2089 if (session->is_vep)
2091 clib_spinlock_unlock (&vcm->sessions_lockp);
2092 if (VPPCOM_DEBUG > 0)
2093 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2094 vcm->my_pid, session_index);
2095 return VPPCOM_EBADFD;
2098 session->vrf = ep->vrf;
2099 session->lcl_addr.is_ip4 = ep->is_ip4;
2100 session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
2101 session->lcl_port = ep->port;
2103 if (VPPCOM_DEBUG > 0)
2104 clib_warning ("[%d] sid %d, bound to lcl address %U lcl port %u",
2105 vcm->my_pid, session_index, format_ip46_address,
2106 &session->lcl_addr.ip46, session->lcl_addr.is_ip4,
2107 clib_net_to_host_u16 (session->lcl_port));
2109 clib_spinlock_unlock (&vcm->sessions_lockp);
2114 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2116 vppcom_main_t *vcm = &vppcom_main;
2117 session_t *listen_session = 0;
2120 clib_spinlock_lock (&vcm->sessions_lockp);
2121 rv = vppcom_session_at_index (listen_session_index, &listen_session);
2122 if (PREDICT_FALSE (rv))
2124 clib_spinlock_unlock (&vcm->sessions_lockp);
2125 if (VPPCOM_DEBUG > 0)
2126 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2127 vcm->my_pid, listen_session_index);
2131 if (listen_session->is_vep)
2133 clib_spinlock_unlock (&vcm->sessions_lockp);
2134 if (VPPCOM_DEBUG > 0)
2135 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2136 vcm->my_pid, listen_session_index);
2137 return VPPCOM_EBADFD;
2140 if (VPPCOM_DEBUG > 0)
2141 clib_warning ("[%d] sid %d", vcm->my_pid, listen_session_index);
2143 ASSERT (vcm->bind_session_index == ~0);
2144 vcm->bind_session_index = listen_session_index;
2145 vppcom_send_bind_sock (listen_session);
2146 clib_spinlock_unlock (&vcm->sessions_lockp);
2148 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2149 vcm->cfg.session_timeout);
2150 if (PREDICT_FALSE (rv))
2152 vcm->bind_session_index = ~0;
2153 if (VPPCOM_DEBUG > 0)
2154 clib_warning ("[%d] server listen timed out, rv = %d (%d)",
2155 vcm->my_pid, vppcom_retval_str (rv), rv);
2159 clib_spinlock_lock (&vcm->sessions_lockp);
2160 rv = vppcom_session_at_index (listen_session_index, &listen_session);
2161 if (PREDICT_FALSE (rv))
2163 clib_spinlock_unlock (&vcm->sessions_lockp);
2164 if (VPPCOM_DEBUG > 0)
2165 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2166 vcm->my_pid, listen_session_index);
2169 listen_session->is_listen = 1;
2170 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2171 clib_spinlock_unlock (&vcm->sessions_lockp);
2177 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2178 double wait_for_time)
2180 vppcom_main_t *vcm = &vppcom_main;
2181 session_t *listen_session = 0;
2182 session_t *client_session = 0;
2183 u32 client_session_index;
2187 clib_spinlock_lock (&vcm->sessions_lockp);
2188 rv = vppcom_session_at_index (listen_session_index, &listen_session);
2189 if (PREDICT_FALSE (rv))
2191 clib_spinlock_unlock (&vcm->sessions_lockp);
2192 if (VPPCOM_DEBUG > 0)
2193 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2194 vcm->my_pid, listen_session_index);
2198 if (listen_session->is_vep)
2200 clib_spinlock_unlock (&vcm->sessions_lockp);
2201 if (VPPCOM_DEBUG > 0)
2202 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2203 vcm->my_pid, listen_session_index);
2204 return VPPCOM_EBADFD;
2207 if (listen_session->state != STATE_LISTEN)
2209 clib_spinlock_unlock (&vcm->sessions_lockp);
2210 if (VPPCOM_DEBUG > 0)
2211 clib_warning ("[%d] session not in listen state, state = %s",
2213 vppcom_session_state_str (listen_session->state));
2214 return VPPCOM_EBADFD;
2216 wait_for = listen_session->is_nonblocking ? 0 :
2217 (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time;
2219 if (VPPCOM_DEBUG > 0)
2220 clib_warning ("[%d] sid %d: %s (%d)", vcm->my_pid,
2221 listen_session_index,
2222 vppcom_session_state_str (listen_session->state),
2223 listen_session->state);
2224 clib_spinlock_unlock (&vcm->sessions_lockp);
2228 rv = vppcom_wait_for_client_session_index (wait_for);
2231 if ((VPPCOM_DEBUG > 0))
2232 clib_warning ("[%d] sid %d, accept timed out, rv = %s (%d)",
2233 vcm->my_pid, listen_session_index,
2234 vppcom_retval_str (rv), rv);
2235 if ((wait_for == 0) || (wait_for_time > 0))
2242 clib_spinlock_lock (&vcm->sessions_lockp);
2243 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
2244 rv = vppcom_session_at_index (client_session_index, &client_session);
2245 ASSERT (rv == VPPCOM_OK);
2246 ASSERT (client_session->peer_addr.is_ip4 ==
2247 listen_session->lcl_addr.is_ip4);
2249 if (VPPCOM_DEBUG > 0)
2250 clib_warning ("[%d] Got a request: client sid %d", vcm->my_pid,
2251 client_session_index);
2253 // Copy the lcl information from the listening session to the client session
2254 // client_session->lcl_port = listen_session->lcl_port;
2255 // client_session->lcl_addr = listen_session->lcl_addr;
2257 ep->vrf = client_session->vrf;
2258 ep->is_cut_thru = client_session->is_cut_thru;
2259 ep->is_ip4 = client_session->peer_addr.is_ip4;
2260 ep->port = client_session->peer_port;
2261 if (client_session->peer_addr.is_ip4)
2262 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2263 sizeof (ip4_address_t));
2265 clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2266 sizeof (ip6_address_t));
2267 if (VPPCOM_DEBUG > 0)
2268 clib_warning ("[%d] sid %d, accepted peer address %U peer port %u",
2269 vcm->my_pid, client_session_index, format_ip46_address,
2270 &client_session->peer_addr.ip46,
2271 client_session->peer_addr.is_ip4,
2272 clib_net_to_host_u16 (client_session->peer_port));
2273 clib_spinlock_unlock (&vcm->sessions_lockp);
2274 return (int) client_session_index;
2278 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2280 vppcom_main_t *vcm = &vppcom_main;
2281 session_t *session = 0;
2284 clib_spinlock_lock (&vcm->sessions_lockp);
2285 rv = vppcom_session_at_index (session_index, &session);
2286 if (PREDICT_FALSE (rv))
2288 clib_spinlock_unlock (&vcm->sessions_lockp);
2289 if (VPPCOM_DEBUG > 0)
2290 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2291 vcm->my_pid, session_index);
2295 if (session->is_vep)
2297 clib_spinlock_unlock (&vcm->sessions_lockp);
2298 if (VPPCOM_DEBUG > 0)
2299 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2300 vcm->my_pid, session_index);
2301 return VPPCOM_EBADFD;
2304 if (session->state == STATE_CONNECT)
2306 clib_spinlock_unlock (&vcm->sessions_lockp);
2307 if (VPPCOM_DEBUG > 0)
2308 clib_warning ("[%d] session, sid (%u) already connected!",
2309 vcm->my_pid, session_index);
2313 session->vrf = server_ep->vrf;
2314 session->peer_addr.is_ip4 = server_ep->is_ip4;
2315 session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
2316 session->peer_port = server_ep->port;
2318 if (VPPCOM_DEBUG > 0)
2320 u8 *ip_str = format (0, "%U", format_ip46_address,
2321 &session->peer_addr.ip46,
2322 session->peer_addr.is_ip4);
2323 clib_warning ("[%d] connect sid %d to %s server port %d proto %s",
2324 vcm->my_pid, session_index, ip_str,
2325 clib_net_to_host_u16 (session->peer_port),
2326 session->proto ? "UDP" : "TCP");
2330 vppcom_send_connect_sock (session, session_index);
2331 clib_spinlock_unlock (&vcm->sessions_lockp);
2332 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2333 vcm->cfg.session_timeout);
2334 if (PREDICT_FALSE (rv))
2336 if (VPPCOM_DEBUG > 0)
2337 clib_warning ("[%d] connect timed out, rv = %s (%d)",
2338 vcm->my_pid, vppcom_retval_str (rv), rv);
2341 if (VPPCOM_DEBUG > 0)
2342 clib_warning ("[%d] sid %d connected!", vcm->my_pid, session_index);
2348 vppcom_session_read_internal (uint32_t session_index, void *buf, int n,
2351 vppcom_main_t *vcm = &vppcom_main;
2352 session_t *session = 0;
2353 svm_fifo_t *rx_fifo;
2361 clib_spinlock_lock (&vcm->sessions_lockp);
2362 rv = vppcom_session_at_index (session_index, &session);
2363 if (PREDICT_FALSE (rv))
2365 clib_spinlock_unlock (&vcm->sessions_lockp);
2366 if (VPPCOM_DEBUG > 0)
2367 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2368 vcm->my_pid, session_index);
2372 if (session->is_vep)
2374 clib_spinlock_unlock (&vcm->sessions_lockp);
2375 if (VPPCOM_DEBUG > 0)
2376 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2377 vcm->my_pid, session_index);
2378 return VPPCOM_EBADFD;
2381 if (session->state == STATE_DISCONNECT)
2383 clib_spinlock_unlock (&vcm->sessions_lockp);
2384 if (VPPCOM_DEBUG > 0)
2385 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2386 vcm->my_pid, session_index);
2387 return VPPCOM_ECONNRESET;
2390 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2391 session->server_rx_fifo : session->server_tx_fifo);
2392 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2393 "server_rx_fifo" : "server_tx_fifo");
2394 poll_et = EPOLLET & session->vep.ev.events;
2395 clib_spinlock_unlock (&vcm->sessions_lockp);
2400 n_read = svm_fifo_peek (rx_fifo, 0, n, buf);
2402 n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
2404 while (!session->is_nonblocking && (n_read <= 0));
2406 if (poll_et && (n_read <= 0))
2408 clib_spinlock_lock (&vcm->sessions_lockp);
2409 session->vep.et_mask |= EPOLLIN;
2410 clib_spinlock_unlock (&vcm->sessions_lockp);
2413 if ((VPPCOM_DEBUG > 2) && (n_read > 0))
2414 clib_warning ("[%d] sid %d, read %d bytes from %s (%p)", vcm->my_pid,
2415 session_index, n_read, fifo_str, rx_fifo);
2417 return (n_read <= 0) ? VPPCOM_EAGAIN : n_read;
2421 vppcom_session_read (uint32_t session_index, void *buf, int n)
2423 return (vppcom_session_read_internal (session_index, buf, n, 0));
2427 vppcom_session_peek (uint32_t session_index, void *buf, int n)
2429 return (vppcom_session_read_internal (session_index, buf, n, 1));
2433 vppcom_session_read_ready (session_t * session, u32 session_index)
2435 vppcom_main_t *vcm = &vppcom_main;
2436 svm_fifo_t *rx_fifo;
2439 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2440 if (session->is_vep)
2442 clib_spinlock_unlock (&vcm->sessions_lockp);
2443 if (VPPCOM_DEBUG > 0)
2444 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2445 vcm->my_pid, session_index);
2446 return VPPCOM_EBADFD;
2449 if (session->state == STATE_DISCONNECT)
2451 if (VPPCOM_DEBUG > 0)
2452 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2453 vcm->my_pid, session_index);
2454 return VPPCOM_ECONNRESET;
2457 if (session->is_listen)
2458 ready = clib_fifo_elts (vcm->client_session_index_fifo);
2461 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2462 session->server_rx_fifo : session->server_tx_fifo);
2464 ready = svm_fifo_max_dequeue (rx_fifo);
2467 if (VPPCOM_DEBUG > 3)
2468 clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", vcm->my_pid,
2470 session->is_server ? "server_rx_fifo" : "server_tx_fifo",
2472 if ((session->vep.ev.events & EPOLLET) && (ready == 0))
2473 session->vep.et_mask |= EPOLLIN;
2479 vppcom_session_write (uint32_t session_index, void *buf, int n)
2481 vppcom_main_t *vcm = &vppcom_main;
2482 session_t *session = 0;
2483 svm_fifo_t *tx_fifo;
2484 unix_shared_memory_queue_t *q;
2485 session_fifo_event_t evt;
2492 clib_spinlock_lock (&vcm->sessions_lockp);
2493 rv = vppcom_session_at_index (session_index, &session);
2494 if (PREDICT_FALSE (rv))
2496 clib_spinlock_unlock (&vcm->sessions_lockp);
2497 if (VPPCOM_DEBUG > 0)
2498 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2499 vcm->my_pid, session_index);
2503 if (session->is_vep)
2505 clib_spinlock_unlock (&vcm->sessions_lockp);
2506 if (VPPCOM_DEBUG > 0)
2507 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2508 vcm->my_pid, session_index);
2509 return VPPCOM_EBADFD;
2512 if (session->state == STATE_DISCONNECT)
2514 clib_spinlock_unlock (&vcm->sessions_lockp);
2515 if (VPPCOM_DEBUG > 0)
2516 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2517 vcm->my_pid, session_index);
2518 return VPPCOM_ECONNRESET;
2521 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2522 session->server_tx_fifo : session->server_rx_fifo);
2523 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2524 "server_tx_fifo" : "server_rx_fifo");
2525 q = session->vpp_event_queue;
2526 poll_et = EPOLLET & session->vep.ev.events;
2527 clib_spinlock_unlock (&vcm->sessions_lockp);
2531 n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
2533 while (!session->is_nonblocking && (n_write <= 0));
2535 /* If event wasn't set, add one */
2536 if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
2540 /* Fabricate TX event, send to vpp */
2542 evt.event_type = FIFO_EVENT_APP_TX;
2544 rval = vppcom_session_at_index (session_index, &session);
2545 if (PREDICT_FALSE (rval))
2547 if (VPPCOM_DEBUG > 1)
2548 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2549 vcm->my_pid, session_index);
2553 unix_shared_memory_queue_add (q, (u8 *) & evt,
2554 0 /* do wait for mutex */ );
2557 if (poll_et && (n_write <= 0))
2559 clib_spinlock_lock (&vcm->sessions_lockp);
2560 session->vep.et_mask |= EPOLLOUT;
2561 clib_spinlock_unlock (&vcm->sessions_lockp);
2564 if (VPPCOM_DEBUG > 2)
2567 clib_warning ("[%d] sid %d, FIFO-FULL %s (%p)", vcm->my_pid,
2568 session_index, fifo_str, tx_fifo);
2570 clib_warning ("[%d] sid %d, wrote %d bytes to %s (%p)", vcm->my_pid,
2571 session_index, n_write, fifo_str, tx_fifo);
2573 return (n_write < 0) ? VPPCOM_EAGAIN : n_write;
2577 vppcom_session_write_ready (session_t * session, u32 session_index)
2579 vppcom_main_t *vcm = &vppcom_main;
2580 svm_fifo_t *tx_fifo;
2584 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2585 if (session->is_vep)
2587 clib_spinlock_unlock (&vcm->sessions_lockp);
2588 if (VPPCOM_DEBUG > 0)
2589 clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2590 vcm->my_pid, session_index);
2591 return VPPCOM_EBADFD;
2594 if (session->state == STATE_DISCONNECT)
2596 if (VPPCOM_DEBUG > 0)
2597 clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2598 vcm->my_pid, session_index);
2599 return VPPCOM_ECONNRESET;
2602 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2603 session->server_tx_fifo : session->server_rx_fifo);
2604 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2605 "server_tx_fifo" : "server_rx_fifo");
2607 ready = svm_fifo_max_enqueue (tx_fifo);
2609 if (VPPCOM_DEBUG > 3)
2610 clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", vcm->my_pid,
2611 session_index, fifo_str, tx_fifo, ready);
2612 if ((session->vep.ev.events & EPOLLET) && (ready == 0))
2613 session->vep.et_mask |= EPOLLOUT;
2619 vppcom_select (unsigned long n_bits, unsigned long *read_map,
2620 unsigned long *write_map, unsigned long *except_map,
2621 double time_to_wait)
2623 vppcom_main_t *vcm = &vppcom_main;
2625 session_t *session = 0;
2626 int rv, bits_set = 0;
2627 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2628 u32 minbits = clib_max (n_bits, BITS (uword));
2630 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2632 if (n_bits && read_map)
2634 clib_bitmap_validate (vcm->rd_bitmap, minbits);
2635 clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
2636 memset (read_map, 0, vec_len (vcm->rd_bitmap));
2638 if (n_bits && write_map)
2640 clib_bitmap_validate (vcm->wr_bitmap, minbits);
2641 clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
2642 memset (write_map, 0, vec_len (vcm->wr_bitmap));
2644 if (n_bits && except_map)
2646 clib_bitmap_validate (vcm->ex_bitmap, minbits);
2647 clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
2648 memset (except_map, 0, vec_len (vcm->ex_bitmap));
2658 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
2660 clib_spinlock_lock (&vcm->sessions_lockp);
2661 rv = vppcom_session_at_index (session_index, &session);
2664 clib_spinlock_unlock (&vcm->sessions_lockp);
2665 if (VPPCOM_DEBUG > 1)
2666 clib_warning ("[%d] session %d specified in "
2667 "read_map is closed.", vcm->my_pid,
2669 bits_set = VPPCOM_EBADFD;
2673 rv = vppcom_session_read_ready (session, session_index);
2674 clib_spinlock_unlock (&vcm->sessions_lockp);
2675 if (except_map && vcm->ex_bitmap &&
2676 clib_bitmap_get (vcm->ex_bitmap, session_index) &&
2679 // TBD: clib_warning
2680 clib_bitmap_set_no_check (except_map, session_index, 1);
2685 // TBD: clib_warning
2686 clib_bitmap_set_no_check (read_map, session_index, 1);
2694 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
2696 clib_spinlock_lock (&vcm->sessions_lockp);
2697 rv = vppcom_session_at_index (session_index, &session);
2700 clib_spinlock_unlock (&vcm->sessions_lockp);
2701 if (VPPCOM_DEBUG > 0)
2702 clib_warning ("[%d] session %d specified in "
2703 "write_map is closed.", vcm->my_pid,
2705 bits_set = VPPCOM_EBADFD;
2709 rv = vppcom_session_write_ready (session, session_index);
2710 clib_spinlock_unlock (&vcm->sessions_lockp);
2711 if (write_map && (rv > 0))
2713 // TBD: clib_warning
2714 clib_bitmap_set_no_check (write_map, session_index, 1);
2722 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
2724 clib_spinlock_lock (&vcm->sessions_lockp);
2725 rv = vppcom_session_at_index (session_index, &session);
2728 clib_spinlock_unlock (&vcm->sessions_lockp);
2729 if (VPPCOM_DEBUG > 1)
2730 clib_warning ("[%d] session %d specified in "
2731 "except_map is closed.", vcm->my_pid,
2733 bits_set = VPPCOM_EBADFD;
2737 rv = vppcom_session_read_ready (session, session_index);
2738 clib_spinlock_unlock (&vcm->sessions_lockp);
2741 // TBD: clib_warning
2742 clib_bitmap_set_no_check (except_map, session_index, 1);
2750 while (clib_time_now (&vcm->clib_time) < timeout);
2757 vep_verify_epoll_chain (u32 vep_idx)
2760 vppcom_epoll_t *vep;
2764 if (VPPCOM_DEBUG < 1)
2767 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2768 rv = vppcom_session_at_index (vep_idx, &session);
2769 if (PREDICT_FALSE (rv))
2771 clib_warning ("ERROR: Invalid vep_idx (%u)!", vep_idx);
2774 if (PREDICT_FALSE (!session->is_vep))
2776 clib_warning ("ERROR: vep_idx (%u) is not a vep!", vep_idx);
2779 if (VPPCOM_DEBUG > 1)
2780 clib_warning ("vep_idx (%u): Dumping epoll chain\n"
2783 " is_vep_session = %u\n"
2784 " wait_cont_idx = 0x%x (%u)\n"
2786 vep_idx, session->is_vep, session->is_vep_session,
2787 session->wait_cont_idx, session->wait_cont_idx);
2790 vep = &session->vep;
2791 sid = vep->next_sid;
2792 if (session->is_vep_session)
2794 if (VPPCOM_DEBUG > 1)
2795 clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2797 " next_sid = 0x%x (%u)\n"
2798 " prev_sid = 0x%x (%u)\n"
2799 " vep_idx = 0x%x (%u)\n"
2800 " ev.events = 0x%x\n"
2801 " ev.data.u64 = 0x%llx\n"
2805 vep->next_sid, vep->next_sid,
2806 vep->prev_sid, vep->prev_sid,
2807 vep->vep_idx, vep->vep_idx,
2808 vep->ev.events, vep->ev.data.u64, vep->et_mask);
2812 rv = vppcom_session_at_index (sid, &session);
2813 if (PREDICT_FALSE (rv))
2815 clib_warning ("ERROR: Invalid sid (%u)!", sid);
2818 if (PREDICT_FALSE (session->is_vep))
2819 clib_warning ("ERROR: sid (%u) is a vep!", vep_idx);
2820 else if (PREDICT_FALSE (!session->is_vep_session))
2822 clib_warning ("ERROR: session (%u) is not a vep session!", sid);
2825 if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2826 clib_warning ("ERROR: session (%u) vep_idx (%u) != "
2828 sid, session->vep.vep_idx, vep_idx);
2834 if (VPPCOM_DEBUG > 1)
2835 clib_warning ("vep_idx (%u): Dump complete!", vep_idx);
2839 vppcom_epoll_create (void)
2841 vppcom_main_t *vcm = &vppcom_main;
2842 session_t *vep_session;
2845 clib_spinlock_lock (&vcm->sessions_lockp);
2846 pool_get (vcm->sessions, vep_session);
2847 memset (vep_session, 0, sizeof (*vep_session));
2848 vep_idx = vep_session - vcm->sessions;
2850 vep_session->is_vep = 1;
2851 vep_session->vep.vep_idx = ~0;
2852 vep_session->vep.next_sid = ~0;
2853 vep_session->vep.prev_sid = ~0;
2854 vep_session->wait_cont_idx = ~0;
2855 clib_spinlock_unlock (&vcm->sessions_lockp);
2857 if (VPPCOM_DEBUG > 0)
2858 clib_warning ("Created vep_idx %u!", vep_idx);
2864 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
2865 struct epoll_event *event)
2867 vppcom_main_t *vcm = &vppcom_main;
2868 session_t *vep_session;
2872 if (vep_idx == session_index)
2874 if (VPPCOM_DEBUG > 0)
2875 clib_warning ("ERROR: vep_idx == session_index (%u)!", vep_idx);
2876 return VPPCOM_EINVAL;
2879 clib_spinlock_lock (&vcm->sessions_lockp);
2880 rv = vppcom_session_at_index (vep_idx, &vep_session);
2881 if (PREDICT_FALSE (rv))
2883 if (VPPCOM_DEBUG > 0)
2884 clib_warning ("ERROR: Invalid vep_idx (%u)!", vep_idx);
2887 if (PREDICT_FALSE (!vep_session->is_vep))
2889 if (VPPCOM_DEBUG > 0)
2890 clib_warning ("ERROR: vep_idx (%u) is not a vep!", vep_idx);
2895 ASSERT (vep_session->vep.vep_idx == ~0);
2896 ASSERT (vep_session->vep.prev_sid == ~0);
2898 rv = vppcom_session_at_index (session_index, &session);
2899 if (PREDICT_FALSE (rv))
2901 if (VPPCOM_DEBUG > 0)
2902 clib_warning ("ERROR: Invalid session_index (%u)!", session_index);
2905 if (PREDICT_FALSE (session->is_vep))
2907 if (VPPCOM_DEBUG > 0)
2908 clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
2916 if (PREDICT_FALSE (!event))
2918 clib_warning ("NULL pointer to epoll_event structure!");
2922 if (vep_session->vep.next_sid != ~0)
2924 session_t *next_session;
2925 rv = vppcom_session_at_index (vep_session->vep.next_sid,
2927 if (PREDICT_FALSE (rv))
2929 if (VPPCOM_DEBUG > 0)
2930 clib_warning ("EPOLL_CTL_ADD: Invalid vep.next_sid (%u) on"
2931 " vep_idx (%u)!", vep_session->vep.next_sid,
2935 ASSERT (next_session->vep.prev_sid == vep_idx);
2936 next_session->vep.prev_sid = session_index;
2938 session->vep.next_sid = vep_session->vep.next_sid;
2939 session->vep.prev_sid = vep_idx;
2940 session->vep.vep_idx = vep_idx;
2941 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2942 session->vep.ev = *event;
2943 session->is_vep_session = 1;
2944 vep_session->vep.next_sid = session_index;
2945 if (VPPCOM_DEBUG > 1)
2946 clib_warning ("EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x,"
2947 " data 0x%llx!", vep_idx, session_index,
2948 event->events, event->data.u64);
2952 if (PREDICT_FALSE (!event))
2954 clib_warning ("NULL pointer to epoll_event structure!");
2958 if (PREDICT_FALSE (!session->is_vep_session &&
2959 (session->vep.vep_idx != vep_idx)))
2961 if (VPPCOM_DEBUG > 0)
2963 if (!session->is_vep_session)
2964 clib_warning ("EPOLL_CTL_MOD: session (%u) is not "
2965 "a vep session!", session_index);
2967 clib_warning ("EPOLL_CTL_MOD: session (%u) vep_idx (%u) != "
2968 "vep_idx (%u)!", session_index,
2969 session->vep.vep_idx, vep_idx);
2974 session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2975 session->vep.ev = *event;
2976 if (VPPCOM_DEBUG > 1)
2977 clib_warning ("EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
2978 " data 0x%llx!", vep_idx, session_index,
2979 event->events, event->data.u64);
2983 if (PREDICT_FALSE (!session->is_vep_session &&
2984 (session->vep.vep_idx != vep_idx)))
2986 if (VPPCOM_DEBUG > 0)
2988 if (!session->is_vep_session)
2989 clib_warning ("EPOLL_CTL_DEL: session (%u) is not "
2990 "a vep session!", session_index);
2992 clib_warning ("EPOLL_CTL_DEL: session (%u) vep_idx (%u) != "
2993 "vep_idx (%u)!", session_index,
2994 session->vep.vep_idx, vep_idx);
3000 vep_session->wait_cont_idx =
3001 (vep_session->wait_cont_idx == session_index) ?
3002 session->vep.next_sid : vep_session->wait_cont_idx;
3004 if (session->vep.prev_sid == vep_idx)
3005 vep_session->vep.next_sid = session->vep.next_sid;
3008 session_t *prev_session;
3009 rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
3010 if (PREDICT_FALSE (rv))
3012 if (VPPCOM_DEBUG > 0)
3013 clib_warning ("EPOLL_CTL_DEL: Invalid vep.prev_sid (%u) on"
3014 " sid (%u)!", session->vep.prev_sid,
3018 ASSERT (prev_session->vep.next_sid == session_index);
3019 prev_session->vep.next_sid = session->vep.next_sid;
3021 if (session->vep.next_sid != ~0)
3023 session_t *next_session;
3024 rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
3025 if (PREDICT_FALSE (rv))
3027 if (VPPCOM_DEBUG > 0)
3028 clib_warning ("EPOLL_CTL_DEL: Invalid vep.next_sid (%u) on"
3029 " sid (%u)!", session->vep.next_sid,
3033 ASSERT (next_session->vep.prev_sid == session_index);
3034 next_session->vep.prev_sid = session->vep.prev_sid;
3037 memset (&session->vep, 0, sizeof (session->vep));
3038 session->vep.next_sid = ~0;
3039 session->vep.prev_sid = ~0;
3040 session->vep.vep_idx = ~0;
3041 session->is_vep_session = 0;
3042 if (VPPCOM_DEBUG > 1)
3043 clib_warning ("EPOLL_CTL_DEL: vep_idx %u, sid %u!", vep_idx,
3048 clib_warning ("Invalid operation (%d)!", op);
3052 vep_verify_epoll_chain (vep_idx);
3055 clib_spinlock_unlock (&vcm->sessions_lockp);
3059 #define VCL_LOCK_AND_GET_SESSION(I, S) \
3061 vppcom_main_t *vcm = &vppcom_main; \
3063 clib_spinlock_lock (&vcm->sessions_lockp); \
3064 rv = vppcom_session_at_index (I, S); \
3065 if (PREDICT_FALSE (rv)) \
3067 clib_spinlock_unlock (&vcm->sessions_lockp); \
3069 if (VPPCOM_DEBUG > 0) \
3070 clib_warning ("ERROR: Invalid ##I (%u)!", I); \
3077 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
3078 int maxevents, double wait_for_time)
3080 vppcom_main_t *vcm = &vppcom_main;
3081 session_t *vep_session;
3083 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
3085 u32 vep_next_sid, wait_cont_idx;
3088 if (PREDICT_FALSE (maxevents <= 0))
3090 if (VPPCOM_DEBUG > 0)
3091 clib_warning ("ERROR: Invalid maxevents (%d)!", maxevents);
3092 return VPPCOM_EINVAL;
3094 if (PREDICT_FALSE (wait_for_time < 0))
3096 if (VPPCOM_DEBUG > 0)
3097 clib_warning ("ERROR: Invalid wait_for_time (%f)!", wait_for_time);
3098 return VPPCOM_EINVAL;
3100 memset (events, 0, sizeof (*events) * maxevents);
3102 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3103 vep_next_sid = vep_session->vep.next_sid;
3104 is_vep = vep_session->is_vep;
3105 wait_cont_idx = vep_session->wait_cont_idx;
3106 clib_spinlock_unlock (&vcm->sessions_lockp);
3108 if (PREDICT_FALSE (!is_vep))
3110 if (VPPCOM_DEBUG > 0)
3111 clib_warning ("ERROR: vep_idx (%u) is not a vep!", vep_idx);
3115 if ((VPPCOM_DEBUG > 0) && (PREDICT_FALSE (vep_next_sid == ~0)))
3117 clib_warning ("WARNING: vep_idx (%u) is empty!", vep_idx);
3127 for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
3128 sid != ~0; sid = next_sid)
3130 u32 session_events, et_mask, clear_et_mask, session_vep_idx;
3131 u8 add_event, is_vep_session;
3133 u64 session_ev_data;
3135 VCL_LOCK_AND_GET_SESSION (sid, &session);
3136 next_sid = session->vep.next_sid;
3137 session_events = session->vep.ev.events;
3138 et_mask = session->vep.et_mask;
3139 is_vep = session->is_vep;
3140 is_vep_session = session->is_vep_session;
3141 session_vep_idx = session->vep.vep_idx;
3142 session_ev_data = session->vep.ev.data.u64;
3143 clib_spinlock_unlock (&vcm->sessions_lockp);
3145 if (PREDICT_FALSE (is_vep))
3147 if (VPPCOM_DEBUG > 0)
3148 clib_warning ("ERROR: sid (%u) is a vep!", vep_idx);
3152 if (PREDICT_FALSE (!is_vep_session))
3154 if (VPPCOM_DEBUG > 0)
3155 clib_warning ("EPOLL_CTL_MOD: session (%u) is not "
3156 "a vep session!", sid);
3160 if (PREDICT_FALSE (session_vep_idx != vep_idx))
3162 clib_warning ("EPOLL_CTL_MOD: session (%u) "
3163 "vep_idx (%u) != vep_idx (%u)!",
3164 sid, session->vep.vep_idx, vep_idx);
3169 add_event = clear_et_mask = 0;
3171 if ((EPOLLIN & session_events) && (EPOLLIN & et_mask))
3173 VCL_LOCK_AND_GET_SESSION (sid, &session);
3174 ready = vppcom_session_read_ready (session, sid);
3175 clib_spinlock_unlock (&vcm->sessions_lockp);
3179 events[num_ev].events |= EPOLLIN;
3180 if (EPOLLET & session_events)
3181 clear_et_mask |= EPOLLIN;
3188 case VPPCOM_ECONNRESET:
3189 events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3193 events[num_ev].events |= EPOLLERR;
3199 if ((EPOLLOUT & session_events) && (EPOLLOUT & et_mask))
3201 VCL_LOCK_AND_GET_SESSION (sid, &session);
3202 ready = vppcom_session_write_ready (session, sid);
3203 clib_spinlock_unlock (&vcm->sessions_lockp);
3207 events[num_ev].events |= EPOLLOUT;
3208 if (EPOLLET & session_events)
3209 clear_et_mask |= EPOLLOUT;
3216 case VPPCOM_ECONNRESET:
3217 events[num_ev].events |= EPOLLHUP;
3221 events[num_ev].events |= EPOLLERR;
3229 events[num_ev].data.u64 = session_ev_data;
3230 if (EPOLLONESHOT & session_events)
3232 VCL_LOCK_AND_GET_SESSION (sid, &session);
3233 session->vep.ev.events = 0;
3234 clib_spinlock_unlock (&vcm->sessions_lockp);
3237 if (num_ev == maxevents)
3239 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3240 vep_session->wait_cont_idx = next_sid;
3241 clib_spinlock_unlock (&vcm->sessions_lockp);
3245 if (wait_cont_idx != ~0)
3248 next_sid = vep_next_sid;
3249 else if (next_sid == wait_cont_idx)
3254 while ((num_ev == 0) && (clib_time_now (&vcm->clib_time) <= timeout));
3256 if (wait_cont_idx != ~0)
3258 VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3259 vep_session->wait_cont_idx = ~0;
3260 clib_spinlock_unlock (&vcm->sessions_lockp);
3263 return (rv != VPPCOM_OK) ? rv : num_ev;
3267 vppcom_session_attr (uint32_t session_index, uint32_t op,
3268 void *buffer, uint32_t * buflen)
3270 vppcom_main_t *vcm = &vppcom_main;
3273 u32 *flags = buffer;
3274 vppcom_endpt_t *ep = buffer;
3276 VCL_LOCK_AND_GET_SESSION (session_index, &session);
3279 case VPPCOM_ATTR_GET_NREAD:
3280 rv = vppcom_session_read_ready (session, session_index);
3281 if (VPPCOM_DEBUG > 1)
3282 clib_warning ("VPPCOM_ATTR_GET_NREAD: nread = %d", rv);
3286 case VPPCOM_ATTR_PEEK_NREAD:
3290 case VPPCOM_ATTR_GET_FLAGS:
3291 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3293 *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
3294 *buflen = sizeof (*flags);
3295 if (VPPCOM_DEBUG > 1)
3296 clib_warning ("VPPCOM_ATTR_GET_FLAGS: flags = 0x%08x, "
3297 "is_nonblocking = %u", *flags,
3298 session->is_nonblocking);
3304 case VPPCOM_ATTR_SET_FLAGS:
3305 if (buffer && buflen && (*buflen >= sizeof (*flags)))
3307 session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
3308 if (VPPCOM_DEBUG > 1)
3309 clib_warning ("VPPCOM_ATTR_SET_FLAGS: flags = 0x%08x, "
3310 "is_nonblocking = %u", *flags,
3311 session->is_nonblocking);
3317 case VPPCOM_ATTR_GET_PEER_ADDR:
3318 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3320 ep->vrf = session->vrf;
3321 ep->is_ip4 = session->peer_addr.is_ip4;
3322 ep->port = session->peer_port;
3323 if (session->peer_addr.is_ip4)
3324 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3325 sizeof (ip4_address_t));
3327 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3328 sizeof (ip6_address_t));
3329 *buflen = sizeof (*ep);
3330 if (VPPCOM_DEBUG > 1)
3331 clib_warning ("VPPCOM_ATTR_GET_PEER_ADDR: sid %u is_ip4 = %u, "
3332 "addr = %U, port %u", session_index,
3333 ep->is_ip4, format_ip46_address,
3334 &session->peer_addr.ip46, ep->is_ip4,
3335 clib_net_to_host_u16 (ep->port));
3341 case VPPCOM_ATTR_GET_LCL_ADDR:
3342 if (buffer && buflen && (*buflen >= sizeof (*ep)))
3344 ep->vrf = session->vrf;
3345 ep->is_ip4 = session->lcl_addr.is_ip4;
3346 ep->port = session->lcl_port;
3347 if (session->lcl_addr.is_ip4)
3348 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3349 sizeof (ip4_address_t));
3351 clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3352 sizeof (ip6_address_t));
3353 *buflen = sizeof (*ep);
3354 if (VPPCOM_DEBUG > 1)
3355 clib_warning ("VPPCOM_ATTR_GET_LCL_ADDR: sid %u is_ip4 = %u, "
3356 "addr = %U port %d", session_index,
3357 ep->is_ip4, format_ip46_address,
3358 &session->lcl_addr.ip46, ep->is_ip4,
3359 clib_net_to_host_u16 (ep->port));
3365 case VPPCOM_ATTR_SET_REUSEADDR:
3368 case VPPCOM_ATTR_SET_BROADCAST:
3371 case VPPCOM_ATTR_SET_V6ONLY:
3374 case VPPCOM_ATTR_SET_KEEPALIVE:
3377 case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3380 case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3389 clib_spinlock_unlock (&vcm->sessions_lockp);
3394 vppcom_session_recvfrom (uint32_t session_index, void *buffer,
3395 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3397 vppcom_main_t *vcm = &vppcom_main;
3399 session_t *session = 0;
3403 clib_spinlock_lock (&vcm->sessions_lockp);
3404 rv = vppcom_session_at_index (session_index, &session);
3405 if (PREDICT_FALSE (rv))
3407 clib_spinlock_unlock (&vcm->sessions_lockp);
3408 if (VPPCOM_DEBUG > 0)
3409 clib_warning ("[%d] invalid session, sid (%u) has been closed!",
3410 vcm->my_pid, session_index);
3412 clib_spinlock_unlock (&vcm->sessions_lockp);
3415 ep->vrf = session->vrf;
3416 ep->is_ip4 = session->peer_addr.is_ip4;
3417 ep->port = session->peer_port;
3418 if (session->peer_addr.is_ip4)
3419 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3420 sizeof (ip4_address_t));
3422 clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3423 sizeof (ip6_address_t));
3424 clib_spinlock_unlock (&vcm->sessions_lockp);
3428 rv = vppcom_session_read (session_index, buffer, buflen);
3429 else if (flags & MSG_PEEK)
3430 rv = vppcom_session_peek (session_index, buffer, buflen);
3433 clib_warning ("Unsupport flags for recvfrom %d", flags);
3434 rv = VPPCOM_EAFNOSUPPORT;
3442 vppcom_session_sendto (uint32_t session_index, void *buffer,
3443 uint32_t buflen, int flags, vppcom_endpt_t * ep)
3445 vppcom_main_t *vcm = &vppcom_main;
3448 return VPPCOM_EINVAL;
3453 return VPPCOM_EINVAL;
3458 // TBD check the flags and do the right thing
3459 if (VPPCOM_DEBUG > 2)
3460 clib_warning ("[%d] handling flags 0x%u (%d) not implemented yet.",
3461 vcm->my_pid, flags, flags);
3464 return (vppcom_session_write (session_index, buffer, buflen));
3468 * fd.io coding-style-patch-verification: ON
3471 * eval: (c-set-style "gnu")