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 <uri/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.
73 volatile session_state_t state;
75 svm_fifo_t *server_rx_fifo;
76 svm_fifo_t *server_tx_fifo;
78 u64 vpp_session_handle;
79 unix_shared_memory_queue_t *vpp_event_queue;
81 /* Socket configuration state */
91 u64 client_queue_address;
95 typedef struct vppcom_cfg_t_
100 u32 add_segment_size;
101 u32 preallocated_fifo_pairs;
104 u32 event_queue_size;
105 u32 listen_queue_size;
111 typedef struct vppcom_main_t_
114 u32 *client_session_index_fifo;
115 volatile u32 bind_session_index;
119 /* vpe input queue */
120 unix_shared_memory_queue_t *vl_input_queue;
122 /* API client handle */
126 clib_spinlock_t sessions_lockp;
129 /* Hash table for disconnect processing */
130 uword *session_index_by_vpp_handles;
133 clib_bitmap_t *rd_bitmap;
134 clib_bitmap_t *wr_bitmap;
135 clib_bitmap_t *ex_bitmap;
137 /* Our event queue */
138 unix_shared_memory_queue_t *app_event_queue;
140 /* unique segment name counter */
141 u32 unique_segment_index;
145 /* For deadman timers */
146 clib_time_t clib_time;
148 /* State of the connection, shared between msg RX thread and main thread */
149 volatile app_state_t app_state;
153 /* VNET_API_ERROR_FOO -> "Foo" hash table */
154 uword *error_string_by_error_number;
157 vppcom_main_t vppcom_main = {.my_client_index = ~0 };
160 vppcom_app_state_str (app_state_t state)
166 case STATE_APP_START:
167 st = "STATE_APP_START";
170 case STATE_APP_CONN_VPP:
171 st = "STATE_APP_CONN_VPP";
174 case STATE_APP_ENABLED:
175 st = "STATE_APP_ENABLED";
178 case STATE_APP_ATTACHED:
179 st = "STATE_APP_ATTACHED";
183 st = "UNKNOWN_APP_STATE";
191 vppcom_session_state_str (session_state_t state)
202 st = "STATE_CONNECT";
213 case STATE_DISCONNECT:
214 st = "STATE_DISCONNECT";
222 st = "UNKNOWN_STATE";
230 * VPPCOM Utility Functions
233 vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
235 vppcom_main_t *vcm = &vppcom_main;
237 /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
238 if (PREDICT_FALSE ((session_index == ~0) ||
239 pool_is_free_index (vcm->sessions, session_index)))
241 clib_warning ("[%d] invalid session, sid (%d) has been closed!",
242 vcm->my_pid, session_index);
243 return VPPCOM_EBADFD;
245 *sess = pool_elt_at_index (vcm->sessions, session_index);
250 vppcom_connect_to_vpp (char *app_name)
252 api_main_t *am = &api_main;
253 vppcom_main_t *vcm = &vppcom_main;
255 if (VPPCOM_DEBUG > 0)
256 printf ("\nConnecting to VPP api...");
257 if (vl_client_connect_to_vlib ("/vpe-api", app_name, 32) < 0)
259 clib_warning ("[%d] connect to vpp (%s) failed!",
260 vcm->my_pid, app_name);
261 return VPPCOM_ECONNREFUSED;
264 vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
265 vcm->my_client_index = am->my_client_index;
266 if (VPPCOM_DEBUG > 0)
267 printf (" connected!\n");
269 vcm->app_state = STATE_APP_CONN_VPP;
274 format_api_error (u8 * s, va_list * args)
276 vppcom_main_t *vcm = &vppcom_main;
277 i32 error = va_arg (*args, u32);
280 p = hash_get (vcm->error_string_by_error_number, -error);
283 s = format (s, "%s (%d)", p[0], error);
285 s = format (s, "%d", error);
290 vppcom_init_error_string_table (void)
292 vppcom_main_t *vcm = &vppcom_main;
294 vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
296 #define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s);
297 foreach_vnet_api_error;
300 hash_set (vcm->error_string_by_error_number, 99, "Misc");
304 vppcom_wait_for_app_state_change (app_state_t app_state)
306 vppcom_main_t *vcm = &vppcom_main;
307 f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
309 while (clib_time_now (&vcm->clib_time) < timeout)
311 if (vcm->app_state == app_state)
314 if (VPPCOM_DEBUG > 0)
315 clib_warning ("[%d] timeout waiting for state %s (%d)", vcm->my_pid,
316 vppcom_app_state_str (app_state), app_state);
317 return VPPCOM_ETIMEDOUT;
321 vppcom_wait_for_session_state_change (u32 session_index,
322 session_state_t state,
325 vppcom_main_t *vcm = &vppcom_main;
326 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
327 session_t *volatile session;
332 clib_spinlock_lock (&vcm->sessions_lockp);
333 rv = vppcom_session_at_index (session_index, &session);
334 if (PREDICT_FALSE (rv))
336 clib_spinlock_unlock (&vcm->sessions_lockp);
339 if (session->state == state)
341 clib_spinlock_unlock (&vcm->sessions_lockp);
344 clib_spinlock_unlock (&vcm->sessions_lockp);
346 while (clib_time_now (&vcm->clib_time) < timeout);
348 if (VPPCOM_DEBUG > 0)
349 clib_warning ("[%d] timeout waiting for state %s (%d)", vcm->my_pid,
350 vppcom_session_state_str (state), state);
351 return VPPCOM_ETIMEDOUT;
355 vppcom_wait_for_client_session_index (f64 wait_for_time)
357 vppcom_main_t *vcm = &vppcom_main;
358 f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
362 if (clib_fifo_elts (vcm->client_session_index_fifo))
365 while (clib_time_now (&vcm->clib_time) < timeout);
367 if (wait_for_time == 0)
368 return VPPCOM_EAGAIN;
370 if (VPPCOM_DEBUG > 0)
371 clib_warning ("[%d] timeout waiting for client_session_index",
373 return VPPCOM_ETIMEDOUT;
377 * VPP-API message functions
380 vppcom_send_session_enable_disable (u8 is_enable)
382 vppcom_main_t *vcm = &vppcom_main;
383 vl_api_session_enable_disable_t *bmp;
384 bmp = vl_msg_api_alloc (sizeof (*bmp));
385 memset (bmp, 0, sizeof (*bmp));
387 bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
388 bmp->client_index = vcm->my_client_index;
389 bmp->context = htonl (0xfeedface);
390 bmp->is_enable = is_enable;
391 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
395 vppcom_app_session_enable (void)
397 vppcom_main_t *vcm = &vppcom_main;
400 if (vcm->app_state != STATE_APP_ENABLED)
402 vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
403 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
404 if (PREDICT_FALSE (rv))
406 if (VPPCOM_DEBUG > 0)
407 clib_warning ("[%d] Session enable timed out, rv = %s (%d)",
408 vcm->my_pid, vppcom_retval_str (rv), rv);
416 vl_api_session_enable_disable_reply_t_handler
417 (vl_api_session_enable_disable_reply_t * mp)
419 vppcom_main_t *vcm = &vppcom_main;
423 clib_warning ("[%d] session_enable_disable failed: %U", vcm->my_pid,
424 format_api_error, ntohl (mp->retval));
427 vcm->app_state = STATE_APP_ENABLED;
431 vppcom_app_send_attach (void)
433 vppcom_main_t *vcm = &vppcom_main;
434 vl_api_application_attach_t *bmp;
435 bmp = vl_msg_api_alloc (sizeof (*bmp));
436 memset (bmp, 0, sizeof (*bmp));
438 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
439 bmp->client_index = vcm->my_client_index;
440 bmp->context = htonl (0xfeedface);
441 bmp->options[APP_OPTIONS_FLAGS] =
442 APP_OPTIONS_FLAGS_USE_FIFO | APP_OPTIONS_FLAGS_ADD_SEGMENT;
443 bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
444 bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
445 bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
446 bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
447 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
451 vppcom_app_attach (void)
453 vppcom_main_t *vcm = &vppcom_main;
456 vppcom_app_send_attach ();
457 rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
458 if (PREDICT_FALSE (rv))
460 if (VPPCOM_DEBUG > 0)
461 clib_warning ("[%d] application attach timed out, rv = %s (%d)",
462 vcm->my_pid, vppcom_retval_str (rv), rv);
469 vppcom_app_detach (void)
471 vppcom_main_t *vcm = &vppcom_main;
472 vl_api_application_detach_t *bmp;
473 bmp = vl_msg_api_alloc (sizeof (*bmp));
474 memset (bmp, 0, sizeof (*bmp));
476 bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
477 bmp->client_index = vcm->my_client_index;
478 bmp->context = htonl (0xfeedface);
479 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
483 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
486 vppcom_main_t *vcm = &vppcom_main;
487 static svm_fifo_segment_create_args_t _a;
488 svm_fifo_segment_create_args_t *a = &_a;
491 memset (a, 0, sizeof (*a));
494 clib_warning ("[%d] attach failed: %U", vcm->my_pid,
495 format_api_error, ntohl (mp->retval));
499 if (mp->segment_name_length == 0)
501 clib_warning ("[%d] segment_name_length zero", vcm->my_pid);
505 a->segment_name = (char *) mp->segment_name;
506 a->segment_size = mp->segment_size;
508 ASSERT (mp->app_event_queue_address);
510 /* Attach to the segment vpp created */
511 rv = svm_fifo_segment_attach (a);
512 vec_reset_length (a->new_segment_indices);
513 if (PREDICT_FALSE (rv))
515 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", vcm->my_pid,
520 vcm->app_event_queue =
521 uword_to_pointer (mp->app_event_queue_address,
522 unix_shared_memory_queue_t *);
524 vcm->app_state = STATE_APP_ATTACHED;
528 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
531 vppcom_main_t *vcm = &vppcom_main;
534 clib_warning ("[%d] detach failed: %U", vcm->my_pid, format_api_error,
537 vcm->app_state = STATE_APP_ENABLED;
541 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
544 vppcom_main_t *vcm = &vppcom_main;
547 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
550 session_t *session = 0;
552 clib_spinlock_lock (&vcm->sessions_lockp);
553 rv = vppcom_session_at_index (p[0], &session);
554 if (PREDICT_FALSE (rv))
556 if (VPPCOM_DEBUG > 1)
557 clib_warning ("[%d] invalid session, sid (%d) has been closed!",
560 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
561 session->state = STATE_DISCONNECT;
562 clib_spinlock_unlock (&vcm->sessions_lockp);
566 if (VPPCOM_DEBUG > 1)
567 clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
572 clib_warning ("[%d] disconnect_session failed: %U", vcm->my_pid,
573 format_api_error, ntohl (mp->retval));
577 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
579 vppcom_main_t *vcm = &vppcom_main;
580 static svm_fifo_segment_create_args_t _a;
581 svm_fifo_segment_create_args_t *a = &_a;
584 memset (a, 0, sizeof (*a));
585 a->segment_name = (char *) mp->segment_name;
586 a->segment_size = mp->segment_size;
587 /* Attach to the segment vpp created */
588 rv = svm_fifo_segment_attach (a);
589 vec_reset_length (a->new_segment_indices);
590 if (PREDICT_FALSE (rv))
592 clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed",
593 vcm->my_pid, mp->segment_name);
596 if (VPPCOM_DEBUG > 1)
597 clib_warning ("[%d] mapped new segment '%s' size %d", vcm->my_pid,
598 mp->segment_name, mp->segment_size);
602 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
604 vppcom_main_t *vcm = &vppcom_main;
605 session_t *session = 0;
606 vl_api_disconnect_session_reply_t *rmp;
610 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
614 clib_spinlock_lock (&vcm->sessions_lockp);
615 rval = vppcom_session_at_index (p[0], &session);
616 if (PREDICT_FALSE (rval))
618 if (VPPCOM_DEBUG > 1)
619 clib_warning ("[%d] invalid session, sid (%d) has been closed!",
623 pool_put (vcm->sessions, session);
624 clib_spinlock_unlock (&vcm->sessions_lockp);
625 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
629 clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
634 rmp = vl_msg_api_alloc (sizeof (*rmp));
635 memset (rmp, 0, sizeof (*rmp));
637 rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
638 rmp->retval = htonl (rv);
639 rmp->handle = mp->handle;
640 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
644 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
646 vppcom_main_t *vcm = &vppcom_main;
647 session_t *session = 0;
648 vl_api_reset_session_reply_t *rmp;
652 p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
656 clib_spinlock_lock (&vcm->sessions_lockp);
657 rval = vppcom_session_at_index (p[0], &session);
658 if (PREDICT_FALSE (rval))
660 if (VPPCOM_DEBUG > 1)
661 clib_warning ("[%d] invalid session, sid (%d) has been closed!",
665 pool_put (vcm->sessions, session);
666 clib_spinlock_unlock (&vcm->sessions_lockp);
667 hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
671 clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
676 rmp = vl_msg_api_alloc (sizeof (*rmp));
677 memset (rmp, 0, sizeof (*rmp));
678 rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
679 rmp->retval = htonl (rv);
680 rmp->handle = mp->handle;
681 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
685 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
687 vppcom_main_t *vcm = &vppcom_main;
690 svm_fifo_t *rx_fifo, *tx_fifo;
696 clib_warning ("[%d] connect failed: %U", vcm->my_pid, format_api_error,
701 session_index = mp->context;
702 if (VPPCOM_DEBUG > 1)
703 clib_warning ("[%d] session_index = %d 0x%08x", vcm->my_pid,
704 session_index, session_index);
706 clib_spinlock_lock (&vcm->sessions_lockp);
707 if (pool_is_free_index (vcm->sessions, session_index))
709 clib_spinlock_unlock (&vcm->sessions_lockp);
710 if (VPPCOM_DEBUG > 1)
711 clib_warning ("[%d] invalid session, sid %d is closed!",
712 vcm->my_pid, session_index);
716 /* We've been redirected */
717 if (mp->segment_name_length > 0)
719 static svm_fifo_segment_create_args_t _a;
720 svm_fifo_segment_create_args_t *a = &_a;
723 memset (a, 0, sizeof (*a));
724 a->segment_name = (char *) mp->segment_name;
725 if (VPPCOM_DEBUG > 1)
726 clib_warning ("[%d] cut-thru segment: %s", vcm->my_pid,
728 rv = svm_fifo_segment_attach (a);
729 vec_reset_length (a->new_segment_indices);
730 if (PREDICT_FALSE (rv))
732 clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed",
733 vcm->my_pid, a->segment_name);
741 if (VPPCOM_DEBUG > 1)
742 clib_warning ("[%d] client sid %d", vcm->my_pid, session_index);
744 session = pool_elt_at_index (vcm->sessions, session_index);
745 session->is_cut_thru = is_cut_thru;
746 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
747 unix_shared_memory_queue_t *);
749 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
750 rx_fifo->client_session_index = session_index;
751 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
752 tx_fifo->client_session_index = session_index;
754 session->server_rx_fifo = rx_fifo;
755 session->server_tx_fifo = tx_fifo;
756 session->vpp_session_handle = mp->handle;
757 session->state = STATE_CONNECT;
759 /* Add it to lookup table */
760 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
761 clib_spinlock_unlock (&vcm->sessions_lockp);
765 vppcom_send_connect_sock (session_t * session, u32 session_index)
767 vppcom_main_t *vcm = &vppcom_main;
768 vl_api_connect_sock_t *cmp;
770 /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
771 session->is_server = 0;
772 cmp = vl_msg_api_alloc (sizeof (*cmp));
773 memset (cmp, 0, sizeof (*cmp));
774 cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
775 cmp->client_index = vcm->my_client_index;
776 cmp->context = session_index;
778 if (VPPCOM_DEBUG > 1)
779 clib_warning ("[%d] session_index = %d 0x%08x",
780 vcm->my_pid, session_index, session_index);
782 cmp->vrf = session->vrf;
783 cmp->is_ip4 = session->is_ip4;
784 clib_memcpy (cmp->ip, session->ip, sizeof (cmp->ip));
785 cmp->port = session->port;
786 cmp->proto = session->proto;
787 clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
788 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
792 vppcom_send_disconnect (u32 session_index)
794 vppcom_main_t *vcm = &vppcom_main;
795 vl_api_disconnect_session_t *dmp;
796 session_t *session = 0;
799 clib_spinlock_lock (&vcm->sessions_lockp);
800 rv = vppcom_session_at_index (session_index, &session);
801 if (PREDICT_FALSE (rv))
803 clib_spinlock_unlock (&vcm->sessions_lockp);
804 if (VPPCOM_DEBUG > 1)
805 clib_warning ("[%d] invalid session, sid (%d) has been closed!",
806 vcm->my_pid, session_index);
810 dmp = vl_msg_api_alloc (sizeof (*dmp));
811 memset (dmp, 0, sizeof (*dmp));
812 dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
813 dmp->client_index = vcm->my_client_index;
814 dmp->handle = session->vpp_session_handle;
815 clib_spinlock_unlock (&vcm->sessions_lockp);
816 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
821 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
823 vppcom_main_t *vcm = &vppcom_main;
824 session_t *session = 0;
828 clib_warning ("[%d] bind failed: %U", vcm->my_pid, format_api_error,
831 ASSERT (vcm->bind_session_index != ~0);
833 clib_spinlock_lock (&vcm->sessions_lockp);
834 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
837 session->vpp_session_handle = mp->handle;
838 hash_set (vcm->session_index_by_vpp_handles, mp->handle,
839 vcm->bind_session_index);
840 session->state = mp->retval ? STATE_FAILED : STATE_LISTEN;
841 vcm->bind_session_index = ~0;
843 clib_spinlock_unlock (&vcm->sessions_lockp);
847 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
849 vppcom_main_t *vcm = &vppcom_main;
850 session_t *session = 0;
853 clib_spinlock_lock (&vcm->sessions_lockp);
854 rv = vppcom_session_at_index (vcm->bind_session_index, &session);
855 if (PREDICT_FALSE (rv))
857 if (VPPCOM_DEBUG > 1)
858 clib_warning ("[%d] invalid session, sid (%d) has been closed!",
859 vcm->my_pid, vcm->bind_session_index);
863 clib_warning ("[%d] unbind failed: %U", vcm->my_pid, format_api_error,
866 vcm->bind_session_index = ~0;
867 session->state = STATE_START;
868 clib_spinlock_unlock (&vcm->sessions_lockp);
872 format_ip4_address (u8 * s, va_list * args)
874 u8 *a = va_arg (*args, u8 *);
875 return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
879 format_ip6_address (u8 * s, va_list * args)
881 ip6_address_t *a = va_arg (*args, ip6_address_t *);
882 u32 i, i_max_n_zero, max_n_zeros, i_first_zero, n_zeros, last_double_colon;
884 i_max_n_zero = ARRAY_LEN (a->as_u16);
886 i_first_zero = i_max_n_zero;
888 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
890 u32 is_zero = a->as_u16[i] == 0;
891 if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
897 if ((!is_zero && n_zeros > max_n_zeros)
898 || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
900 i_max_n_zero = i_first_zero;
901 max_n_zeros = n_zeros;
902 i_first_zero = ARRAY_LEN (a->as_u16);
907 last_double_colon = 0;
908 for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
910 if (i == i_max_n_zero && max_n_zeros > 1)
912 s = format (s, "::");
913 i += max_n_zeros - 1;
914 last_double_colon = 1;
918 s = format (s, "%s%x",
919 (last_double_colon || i == 0) ? "" : ":",
920 clib_net_to_host_u16 (a->as_u16[i]));
921 last_double_colon = 0;
928 /* Format an IP46 address. */
930 format_ip46_address (u8 * s, va_list * args)
932 ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
933 ip46_type_t type = va_arg (*args, ip46_type_t);
939 is_ip4 = ip46_address_is_ip4 (ip46);
950 format (s, "%U", format_ip4_address, &ip46->ip4) :
951 format (s, "%U", format_ip6_address, &ip46->ip6);
955 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
957 vppcom_main_t *vcm = &vppcom_main;
958 vl_api_accept_session_reply_t *rmp;
959 svm_fifo_t *rx_fifo, *tx_fifo;
964 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
966 clib_warning ("[%d] client session queue is full!", vcm->my_pid);
967 rv = VNET_API_ERROR_QUEUE_FULL;
971 if (VPPCOM_DEBUG > 1)
973 u8 *ip_str = format (0, "%U", format_ip46_address, &mp->ip, mp->is_ip4);
974 clib_warning ("[%d] accepted session from: %s:%d", vcm->my_pid, ip_str,
975 clib_net_to_host_u16 (mp->port));
979 clib_spinlock_lock (&vcm->sessions_lockp);
980 /* Allocate local session and set it up */
981 pool_get (vcm->sessions, session);
982 memset (session, 0, sizeof (*session));
983 session_index = session - vcm->sessions;
985 rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
986 rx_fifo->client_session_index = session_index;
987 tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
988 tx_fifo->client_session_index = session_index;
990 session->server_rx_fifo = rx_fifo;
991 session->server_tx_fifo = tx_fifo;
992 session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
993 unix_shared_memory_queue_t *);
994 session->state = STATE_ACCEPT;
995 session->is_cut_thru = 0;
996 session->port = ntohs (mp->port);
997 session->is_ip4 = mp->is_ip4;
998 clib_memcpy (session->ip, mp->ip, sizeof (session->ip));
999 clib_spinlock_unlock (&vcm->sessions_lockp);
1001 /* Add it to lookup table */
1002 hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1004 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1007 * Send accept reply to vpp
1010 rmp = vl_msg_api_alloc (sizeof (*rmp));
1011 memset (rmp, 0, sizeof (*rmp));
1012 rmp->_vl_msg_id = ntohs (VL_API_ACCEPT_SESSION_REPLY);
1013 rmp->retval = htonl (rv);
1014 rmp->handle = mp->handle;
1015 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
1019 * Acting as server for redirected connect requests
1022 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1024 static svm_fifo_segment_create_args_t _a;
1025 svm_fifo_segment_create_args_t *a = &_a;
1026 vppcom_main_t *vcm = &vppcom_main;
1028 svm_fifo_segment_private_t *seg;
1029 unix_shared_memory_queue_t *client_q;
1030 vl_api_connect_session_reply_t *rmp;
1031 session_t *session = 0;
1033 svm_fifo_t *rx_fifo;
1034 svm_fifo_t *tx_fifo;
1035 unix_shared_memory_queue_t *event_q = 0;
1037 if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1039 if (VPPCOM_DEBUG > 1)
1040 clib_warning ("[%d] client session queue is full!", vcm->my_pid);
1041 rv = VNET_API_ERROR_QUEUE_FULL;
1045 /* Create the segment */
1046 memset (a, 0, sizeof (*a));
1047 a->segment_name = (char *) format ((u8 *) a->segment_name, "%d:segment%d%c",
1048 vcm->my_pid, vcm->unique_segment_index++,
1050 a->segment_size = vcm->cfg.segment_size;
1051 a->preallocated_fifo_pairs = vcm->cfg.preallocated_fifo_pairs;
1052 a->rx_fifo_size = vcm->cfg.rx_fifo_size;
1053 a->tx_fifo_size = vcm->cfg.tx_fifo_size;
1055 rv = svm_fifo_segment_create (a);
1056 if (PREDICT_FALSE (rv))
1058 if (VPPCOM_DEBUG > 1)
1059 clib_warning ("[%d] svm_fifo_segment_create ('%s') failed",
1060 vcm->my_pid, a->segment_name);
1061 vec_reset_length (a->new_segment_indices);
1062 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
1066 if (VPPCOM_DEBUG > 1)
1067 clib_warning ("[%d] created segment '%s'", vcm->my_pid, a->segment_name);
1069 clib_spinlock_lock (&vcm->sessions_lockp);
1070 pool_get (vcm->sessions, session);
1071 memset (session, 0, sizeof (*session));
1072 session_index = session - vcm->sessions;
1074 session->sm_seg_index = a->new_segment_indices[0];
1075 vec_reset_length (a->new_segment_indices);
1077 seg = svm_fifo_segment_get_segment (session->sm_seg_index);
1078 rx_fifo = session->server_rx_fifo =
1079 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.rx_fifo_size,
1080 FIFO_SEGMENT_RX_FREELIST);
1081 if (PREDICT_FALSE (!session->server_rx_fifo))
1083 svm_fifo_segment_delete (seg);
1084 clib_warning ("[%d] rx fifo alloc failed, size %ld (0x%lx)",
1085 vcm->my_pid, vcm->cfg.rx_fifo_size,
1086 vcm->cfg.rx_fifo_size);
1087 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
1088 clib_spinlock_unlock (&vcm->sessions_lockp);
1092 tx_fifo = session->server_tx_fifo =
1093 svm_fifo_segment_alloc_fifo (seg, vcm->cfg.tx_fifo_size,
1094 FIFO_SEGMENT_TX_FREELIST);
1095 if (PREDICT_FALSE (!session->server_tx_fifo))
1097 svm_fifo_segment_delete (seg);
1098 if (VPPCOM_DEBUG > 1)
1099 clib_warning ("[%d] tx fifo alloc failed, size %ld (0x%lx)",
1100 vcm->my_pid, vcm->cfg.tx_fifo_size,
1101 vcm->cfg.tx_fifo_size);
1102 rv = VNET_API_ERROR_URI_FIFO_CREATE_FAILED;
1103 clib_spinlock_unlock (&vcm->sessions_lockp);
1107 session->server_rx_fifo->master_session_index = session_index;
1108 session->server_tx_fifo->master_session_index = session_index;
1109 session->client_queue_address = mp->client_queue_address;
1110 session->is_cut_thru = 1;
1111 session->is_server = 1;
1112 session->is_ip4 = mp->is_ip4;
1113 session->port = mp->port;
1116 ssvm_shared_header_t *sh = seg->ssvm.sh;
1118 ssvm_lock_non_recursive (sh, 1);
1119 oldheap = ssvm_push_heap (sh);
1120 event_q = session->vpp_event_queue =
1121 unix_shared_memory_queue_init (vcm->cfg.event_queue_size,
1122 sizeof (session_fifo_event_t),
1123 vcm->my_pid, 0 /* signal not sent */ );
1124 ssvm_pop_heap (oldheap);
1125 ssvm_unlock_non_recursive (sh);
1127 clib_memcpy (session->ip, mp->ip, sizeof (session->ip));
1129 session->state = STATE_ACCEPT;
1130 if (VPPCOM_DEBUG > 1)
1131 clib_warning ("[%d] Connected cut-thru to client: sid %d",
1132 vcm->my_pid, session_index);
1133 clib_spinlock_unlock (&vcm->sessions_lockp);
1134 clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1137 rmp = vl_msg_api_alloc (sizeof (*rmp));
1138 memset (rmp, 0, sizeof (*rmp));
1140 rmp->_vl_msg_id = ntohs (VL_API_CONNECT_SESSION_REPLY);
1141 rmp->context = mp->context;
1142 rmp->retval = htonl (rv);
1143 rmp->segment_name_length = vec_len (a->segment_name);
1144 clib_memcpy (rmp->segment_name, a->segment_name, vec_len (a->segment_name));
1145 vec_reset_length (a->segment_name);
1149 rmp->vpp_event_queue_address = pointer_to_uword (event_q);
1150 rmp->server_rx_fifo = pointer_to_uword (rx_fifo);
1151 rmp->server_tx_fifo = pointer_to_uword (tx_fifo);
1154 uword_to_pointer (mp->client_queue_address, unix_shared_memory_queue_t *);
1157 vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
1161 vppcom_send_bind_sock (session_t * session)
1163 vppcom_main_t *vcm = &vppcom_main;
1164 vl_api_bind_sock_t *bmp;
1166 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
1167 session->is_server = 1;
1168 bmp = vl_msg_api_alloc (sizeof (*bmp));
1169 memset (bmp, 0, sizeof (*bmp));
1171 bmp->_vl_msg_id = ntohs (VL_API_BIND_SOCK);
1172 bmp->client_index = vcm->my_client_index;
1173 bmp->context = htonl (0xfeedface);
1174 bmp->vrf = session->vrf;
1175 bmp->is_ip4 = session->is_ip4;
1176 clib_memcpy (bmp->ip, session->ip, sizeof (bmp->ip));
1177 bmp->port = session->port;
1178 bmp->proto = session->proto;
1179 clib_memcpy (bmp->options, session->options, sizeof (bmp->options));
1180 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
1184 vppcom_send_unbind_sock (u32 session_index)
1186 vppcom_main_t *vcm = &vppcom_main;
1187 vl_api_unbind_sock_t *ump;
1188 session_t *session = 0;
1191 clib_spinlock_lock (&vcm->sessions_lockp);
1192 rv = vppcom_session_at_index (session_index, &session);
1193 if (PREDICT_FALSE (rv))
1195 clib_spinlock_unlock (&vcm->sessions_lockp);
1196 if (VPPCOM_DEBUG > 0)
1197 clib_warning ("[%d] invalid session, sid (%d) has been closed!",
1198 vcm->my_pid, session_index);
1202 ump = vl_msg_api_alloc (sizeof (*ump));
1203 memset (ump, 0, sizeof (*ump));
1205 ump->_vl_msg_id = ntohs (VL_API_UNBIND_SOCK);
1206 ump->client_index = vcm->my_client_index;
1207 ump->handle = session->vpp_session_handle;
1208 clib_spinlock_unlock (&vcm->sessions_lockp);
1209 vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & ump);
1213 vppcom_session_unbind_cut_thru (session_t * session)
1215 svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1216 svm_fifo_segment_private_t *seg;
1219 seg = vec_elt_at_index (sm->segments, session->sm_seg_index);
1220 svm_fifo_segment_free_fifo (seg, session->server_rx_fifo,
1221 FIFO_SEGMENT_RX_FREELIST);
1222 svm_fifo_segment_free_fifo (seg, session->server_tx_fifo,
1223 FIFO_SEGMENT_TX_FREELIST);
1224 svm_fifo_segment_delete (seg);
1230 vppcom_session_unbind (u32 session_index)
1232 vppcom_main_t *vcm = &vppcom_main;
1235 clib_spinlock_lock (&vcm->sessions_lockp);
1236 if (PREDICT_FALSE (pool_is_free_index (vcm->sessions, session_index)))
1238 clib_spinlock_unlock (&vcm->sessions_lockp);
1239 if (VPPCOM_DEBUG > 1)
1240 clib_warning ("[%d] invalid session, sid (%d) has been closed!",
1241 vcm->my_pid, session_index);
1242 return VPPCOM_EBADFD;
1244 clib_spinlock_unlock (&vcm->sessions_lockp);
1246 vcm->bind_session_index = session_index;
1247 vppcom_send_unbind_sock (session_index);
1248 rv = vppcom_wait_for_session_state_change (session_index, STATE_START,
1249 vcm->cfg.session_timeout);
1250 if (PREDICT_FALSE (rv))
1252 vcm->bind_session_index = ~0;
1253 if (VPPCOM_DEBUG > 0)
1254 clib_warning ("[%d] server unbind timed out, rv = %s (%d)",
1255 vcm->my_pid, vppcom_retval_str (rv), rv);
1262 vppcom_session_disconnect (u32 session_index)
1264 vppcom_main_t *vcm = &vppcom_main;
1267 rv = vppcom_send_disconnect (session_index);
1268 if (PREDICT_FALSE (rv))
1271 rv = vppcom_wait_for_session_state_change (session_index, STATE_DISCONNECT,
1272 vcm->cfg.session_timeout);
1273 if (PREDICT_FALSE (rv))
1275 if (VPPCOM_DEBUG > 0)
1276 clib_warning ("[%d] client disconnect timed out, rv = %s (%d)",
1277 vcm->my_pid, vppcom_retval_str (rv), rv);
1281 clib_spinlock_lock (&vcm->sessions_lockp);
1282 pool_put_index (vcm->sessions, session_index);
1283 clib_spinlock_unlock (&vcm->sessions_lockp);
1287 #define foreach_sock_msg \
1288 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply) \
1289 _(BIND_SOCK_REPLY, bind_sock_reply) \
1290 _(UNBIND_SOCK_REPLY, unbind_sock_reply) \
1291 _(ACCEPT_SESSION, accept_session) \
1292 _(CONNECT_SOCK, connect_sock) \
1293 _(CONNECT_SESSION_REPLY, connect_session_reply) \
1294 _(DISCONNECT_SESSION, disconnect_session) \
1295 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply) \
1296 _(RESET_SESSION, reset_session) \
1297 _(APPLICATION_ATTACH_REPLY, application_attach_reply) \
1298 _(APPLICATION_DETACH_REPLY, application_detach_reply) \
1299 _(MAP_ANOTHER_SEGMENT, map_another_segment)
1302 vppcom_api_hookup (void)
1305 vl_msg_api_set_handlers(VL_API_##N, #n, \
1306 vl_api_##n##_t_handler, \
1308 vl_api_##n##_t_endian, \
1309 vl_api_##n##_t_print, \
1310 sizeof(vl_api_##n##_t), 1);
1316 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1320 vcl_cfg->heapsize = (256ULL << 20);
1321 vcl_cfg->segment_baseva = 0x200000000ULL;
1322 vcl_cfg->segment_size = (256 << 20);
1323 vcl_cfg->add_segment_size = (128 << 20);
1324 vcl_cfg->preallocated_fifo_pairs = 8;
1325 vcl_cfg->rx_fifo_size = (1 << 20);
1326 vcl_cfg->tx_fifo_size = (1 << 20);
1327 vcl_cfg->event_queue_size = 2048;
1328 vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1329 vcl_cfg->app_timeout = 10 * 60.0;
1330 vcl_cfg->session_timeout = 10 * 60.0;
1331 vcl_cfg->accept_timeout = 60.0;
1335 vppcom_cfg_heapsize (char *conf_fname)
1337 vppcom_main_t *vcm = &vppcom_main;
1338 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1349 fp = fopen (conf_fname, "r");
1352 if (VPPCOM_DEBUG > 0)
1353 fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1356 argv = calloc (1, sizeof (char *));
1362 if (fgets (inbuf, 4096, fp) == 0)
1364 p = strtok (inbuf, " \t\n");
1370 char **tmp = realloc (argv, argc * sizeof (char *));
1377 arg = strndup (p, 1024);
1383 argv[argc - 1] = arg;
1384 p = strtok (NULL, " \t\n");
1390 char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1397 * Look for and parse the "heapsize" config parameter.
1398 * Manual since none of the clib infra has been bootstrapped yet.
1400 * Format: heapsize <nn>[mM][gG]
1403 for (i = 1; i < (argc - 1); i++)
1405 if (!strncmp (argv[i], "heapsize", 8))
1407 sizep = (u8 *) argv[i + 1];
1409 while (*sizep >= '0' && *sizep <= '9')
1412 size += *sizep++ - '0';
1416 if (VPPCOM_DEBUG > 0)
1417 clib_warning ("[%d] parse error '%s %s', "
1418 "using default heapsize %lld (0x%llx)",
1419 vcm->my_pid, argv[i], argv[i + 1],
1420 vcl_cfg->heapsize, vcl_cfg->heapsize);
1424 if (*sizep == 'g' || *sizep == 'G')
1425 vcl_cfg->heapsize = size << 30;
1426 else if (*sizep == 'm' || *sizep == 'M')
1427 vcl_cfg->heapsize = size << 20;
1430 if (VPPCOM_DEBUG > 0)
1431 clib_warning ("[%d] parse error '%s %s', "
1432 "using default heapsize %lld (0x%llx)",
1433 vcm->my_pid, argv[i], argv[i + 1],
1434 vcl_cfg->heapsize, vcl_cfg->heapsize);
1441 if (!clib_mem_init (0, vcl_cfg->heapsize))
1442 clib_warning ("[%d] vppcom heap allocation failure!", vcm->my_pid);
1443 else if (VPPCOM_DEBUG > 0)
1444 clib_warning ("[%d] allocated vppcom heapsize %lld (0x%llx)",
1445 vcm->my_pid, vcl_cfg->heapsize, vcl_cfg->heapsize);
1449 vppcom_cfg_read (char *conf_fname)
1451 vppcom_main_t *vcm = &vppcom_main;
1452 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1454 unformat_input_t _input, *input = &_input;
1455 unformat_input_t _line_input, *line_input = &_line_input;
1456 u8 vc_cfg_input = 0;
1461 fd = open (conf_fname, O_RDONLY);
1464 if (VPPCOM_DEBUG > 0)
1465 clib_warning ("[%d] open configuration file '%s' failed!",
1466 vcm->my_pid, conf_fname);
1470 if (fstat (fd, &s) < 0)
1472 if (VPPCOM_DEBUG > 0)
1473 clib_warning ("[%d] failed to stat `%s'", vcm->my_pid, conf_fname);
1477 if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1479 if (VPPCOM_DEBUG > 0)
1480 clib_warning ("[%d] not a regular file `%s'", vcm->my_pid,
1485 unformat_init_unix_file (input, fd);
1487 while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1489 unformat_user (input, unformat_line_input, line_input);
1490 unformat_skip_white_space (line_input);
1492 if (unformat (line_input, "vppcom {"))
1500 if (unformat (line_input, "heapsize %s", &chroot_path))
1502 vec_terminate_c_string (chroot_path);
1503 if (VPPCOM_DEBUG > 0)
1504 clib_warning ("[%d] configured heapsize %s, "
1505 "actual heapsize %lld (0x%llx)",
1506 vcm->my_pid, chroot_path, vcl_cfg->heapsize,
1508 vec_free (chroot_path);
1510 else if (unformat (line_input, "api-prefix %s", &chroot_path))
1512 vec_terminate_c_string (chroot_path);
1513 vl_set_memory_root_path ((char *) chroot_path);
1514 if (VPPCOM_DEBUG > 0)
1515 clib_warning ("[%d] configured api-prefix %s",
1516 vcm->my_pid, chroot_path);
1517 chroot_path = 0; /* Don't vec_free() it! */
1519 else if (unformat (line_input, "uid %d", &uid))
1521 vl_set_memory_uid (uid);
1522 if (VPPCOM_DEBUG > 0)
1523 clib_warning ("[%d] configured uid %d", vcm->my_pid, uid);
1525 else if (unformat (line_input, "gid %d", &gid))
1527 vl_set_memory_gid (gid);
1528 if (VPPCOM_DEBUG > 0)
1529 clib_warning ("[%d] configured gid %d", vcm->my_pid, gid);
1531 else if (unformat (line_input, "segment-baseva 0x%llx",
1532 &vcl_cfg->segment_baseva))
1534 if (VPPCOM_DEBUG > 0)
1535 clib_warning ("[%d] configured segment_baseva 0x%llx",
1536 vcm->my_pid, vcl_cfg->segment_baseva);
1538 else if (unformat (line_input, "segment-size 0x%lx",
1539 &vcl_cfg->segment_size))
1541 if (VPPCOM_DEBUG > 0)
1542 clib_warning ("[%d] configured segment_size 0x%lx (%ld)",
1543 vcm->my_pid, vcl_cfg->segment_size,
1544 vcl_cfg->segment_size);
1546 else if (unformat (line_input, "segment-size %ld",
1547 &vcl_cfg->segment_size))
1549 if (VPPCOM_DEBUG > 0)
1550 clib_warning ("[%d] configured segment_size %ld (0x%lx)",
1551 vcm->my_pid, vcl_cfg->segment_size,
1552 vcl_cfg->segment_size);
1554 else if (unformat (line_input, "add-segment-size 0x%lx",
1555 &vcl_cfg->add_segment_size))
1557 if (VPPCOM_DEBUG > 0)
1559 ("[%d] configured add_segment_size 0x%lx (%ld)",
1560 vcm->my_pid, vcl_cfg->add_segment_size,
1561 vcl_cfg->add_segment_size);
1563 else if (unformat (line_input, "add-segment-size %ld",
1564 &vcl_cfg->add_segment_size))
1566 if (VPPCOM_DEBUG > 0)
1568 ("[%d] configured add_segment_size %ld (0x%lx)",
1569 vcm->my_pid, vcl_cfg->add_segment_size,
1570 vcl_cfg->add_segment_size);
1572 else if (unformat (line_input, "preallocated-fifo-pairs %d",
1573 &vcl_cfg->preallocated_fifo_pairs))
1575 if (VPPCOM_DEBUG > 0)
1576 clib_warning ("[%d] configured preallocated_fifo_pairs "
1577 "%d (0x%x)", vcm->my_pid,
1578 vcl_cfg->preallocated_fifo_pairs,
1579 vcl_cfg->preallocated_fifo_pairs);
1581 else if (unformat (line_input, "rx-fifo-size 0x%lx",
1582 &vcl_cfg->rx_fifo_size))
1584 if (VPPCOM_DEBUG > 0)
1585 clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)",
1586 vcm->my_pid, vcl_cfg->rx_fifo_size,
1587 vcl_cfg->rx_fifo_size);
1589 else if (unformat (line_input, "rx-fifo-size %ld",
1590 &vcl_cfg->rx_fifo_size))
1592 if (VPPCOM_DEBUG > 0)
1593 clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)",
1594 vcm->my_pid, vcl_cfg->rx_fifo_size,
1595 vcl_cfg->rx_fifo_size);
1597 else if (unformat (line_input, "tx-fifo-size 0x%lx",
1598 &vcl_cfg->tx_fifo_size))
1600 if (VPPCOM_DEBUG > 0)
1601 clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)",
1602 vcm->my_pid, vcl_cfg->tx_fifo_size,
1603 vcl_cfg->tx_fifo_size);
1605 else if (unformat (line_input, "tx-fifo-size %ld",
1606 &vcl_cfg->tx_fifo_size))
1608 if (VPPCOM_DEBUG > 0)
1609 clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)",
1610 vcm->my_pid, vcl_cfg->tx_fifo_size,
1611 vcl_cfg->tx_fifo_size);
1613 else if (unformat (line_input, "event-queue-size 0x%lx",
1614 &vcl_cfg->event_queue_size))
1616 if (VPPCOM_DEBUG > 0)
1617 clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)",
1618 vcm->my_pid, vcl_cfg->event_queue_size,
1619 vcl_cfg->event_queue_size);
1621 else if (unformat (line_input, "event-queue-size %ld",
1622 &vcl_cfg->event_queue_size))
1624 if (VPPCOM_DEBUG > 0)
1625 clib_warning ("[%d] configured event_queue_size %ld (0x%lx)",
1626 vcm->my_pid, vcl_cfg->event_queue_size,
1627 vcl_cfg->event_queue_size);
1629 else if (unformat (line_input, "listen-queue-size 0x%lx",
1630 &vcl_cfg->listen_queue_size))
1632 if (VPPCOM_DEBUG > 0)
1633 clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)",
1634 vcm->my_pid, vcl_cfg->listen_queue_size,
1635 vcl_cfg->listen_queue_size);
1637 else if (unformat (line_input, "listen-queue-size %ld",
1638 &vcl_cfg->listen_queue_size))
1640 if (VPPCOM_DEBUG > 0)
1641 clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)",
1642 vcm->my_pid, vcl_cfg->listen_queue_size,
1643 vcl_cfg->listen_queue_size);
1645 else if (unformat (line_input, "app-timeout %f",
1646 &vcl_cfg->app_timeout))
1648 if (VPPCOM_DEBUG > 0)
1649 clib_warning ("[%d] configured app_timeout %f",
1650 vcm->my_pid, vcl_cfg->app_timeout);
1652 else if (unformat (line_input, "session-timeout %f",
1653 &vcl_cfg->session_timeout))
1655 if (VPPCOM_DEBUG > 0)
1656 clib_warning ("[%d] configured session_timeout %f",
1657 vcm->my_pid, vcl_cfg->session_timeout);
1659 else if (unformat (line_input, "accept-timeout %f",
1660 &vcl_cfg->accept_timeout))
1662 if (VPPCOM_DEBUG > 0)
1663 clib_warning ("[%d] configured accept_timeout %f",
1664 vcm->my_pid, vcl_cfg->accept_timeout);
1666 else if (unformat (line_input, "}"))
1669 if (VPPCOM_DEBUG > 0)
1670 clib_warning ("[%d] completed parsing vppcom config!",
1676 if (line_input->buffer[line_input->index] != '#')
1678 clib_warning ("[%d] Unknown vppcom config option: '%s'",
1679 vcm->my_pid, (char *)
1680 &line_input->buffer[line_input->index]);
1687 unformat_free (input);
1695 * VPPCOM Public API functions
1698 vppcom_app_create (char *app_name)
1700 vppcom_main_t *vcm = &vppcom_main;
1701 vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1711 vcm->my_pid = getpid ();
1712 clib_fifo_validate (vcm->client_session_index_fifo,
1713 vcm->cfg.listen_queue_size);
1714 vppcom_cfg_init (vcl_cfg);
1715 conf_fname = getenv (VPPCOM_CONF_ENV);
1718 conf_fname = VPPCOM_CONF_DEFAULT;
1719 if (VPPCOM_DEBUG > 0)
1720 clib_warning ("[%d] getenv '%s' failed!", vcm->my_pid,
1723 vppcom_cfg_heapsize (conf_fname);
1724 vppcom_cfg_read (conf_fname);
1725 vcm->bind_session_index = ~0;
1726 vcm->main_cpu = os_get_thread_index ();
1727 heap = clib_mem_get_per_cpu_heap ();
1728 h = mheap_header (heap);
1730 /* make the main heap thread-safe */
1731 h->flags |= MHEAP_FLAG_THREAD_SAFE;
1733 vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1735 clib_time_init (&vcm->clib_time);
1736 vppcom_init_error_string_table ();
1737 svm_fifo_segment_init (vcl_cfg->segment_baseva,
1738 20 /* timeout in secs */ );
1739 clib_spinlock_init (&vcm->sessions_lockp);
1740 vppcom_api_hookup ();
1743 if (vcm->my_client_index == ~0)
1745 vcm->app_state = STATE_APP_START;
1746 rv = vppcom_connect_to_vpp (app_name);
1749 clib_warning ("[%s] couldn't connect to VPP.", vcm->my_pid);
1753 if (VPPCOM_DEBUG > 0)
1754 clib_warning ("[%d] sending session enable", vcm->my_pid);
1756 rv = vppcom_app_session_enable ();
1759 clib_warning ("[%d] vppcom_app_session_enable() failed!",
1764 if (VPPCOM_DEBUG > 0)
1765 clib_warning ("[%d] sending app attach", vcm->my_pid);
1767 rv = vppcom_app_attach ();
1770 clib_warning ("[%d] vppcom_app_attach() failed!", vcm->my_pid);
1775 if (VPPCOM_DEBUG > 0)
1776 clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)",
1777 vcm->my_pid, app_name, vcm->my_client_index,
1778 vcm->my_client_index);
1784 vppcom_app_destroy (void)
1786 vppcom_main_t *vcm = &vppcom_main;
1789 if (vcm->my_client_index == ~0)
1792 if (VPPCOM_DEBUG > 0)
1793 clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)",
1794 vcm->my_pid, vcm->my_client_index, vcm->my_client_index);
1796 vppcom_app_detach ();
1797 rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
1798 if (PREDICT_FALSE (rv))
1800 if (VPPCOM_DEBUG > 0)
1801 clib_warning ("[%d] application detach timed out, rv = %s (%d)",
1802 vcm->my_pid, vppcom_retval_str (rv), rv);
1804 vl_client_disconnect_from_vlib ();
1805 vcm->my_client_index = ~0;
1806 vcm->app_state = STATE_APP_START;
1810 vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
1812 vppcom_main_t *vcm = &vppcom_main;
1816 clib_spinlock_lock (&vcm->sessions_lockp);
1817 pool_get (vcm->sessions, session);
1818 session_index = session - vcm->sessions;
1821 session->proto = proto;
1822 session->state = STATE_START;
1823 session->is_nonblocking = is_nonblocking;
1824 clib_spinlock_unlock (&vcm->sessions_lockp);
1826 if (VPPCOM_DEBUG > 0)
1827 clib_warning ("[%d] sid %d", vcm->my_pid, session_index);
1829 return (int) session_index;
1833 vppcom_session_close (uint32_t session_index)
1835 vppcom_main_t *vcm = &vppcom_main;
1836 session_t *session = 0;
1839 clib_spinlock_lock (&vcm->sessions_lockp);
1840 rv = vppcom_session_at_index (session_index, &session);
1841 if (PREDICT_FALSE (rv))
1843 clib_spinlock_unlock (&vcm->sessions_lockp);
1844 if (VPPCOM_DEBUG > 0)
1845 clib_warning ("[%d] invalid session, sid (%d) has been closed!",
1846 vcm->my_pid, session_index);
1849 clib_spinlock_unlock (&vcm->sessions_lockp);
1851 if (VPPCOM_DEBUG > 0)
1852 clib_warning ("[%d] sid %d", vcm->my_pid, session_index);
1854 if (session->is_cut_thru)
1856 if (session->is_server)
1857 rv = vppcom_session_unbind_cut_thru (session);
1861 rv = (session->is_server) ?
1862 vppcom_session_unbind (session_index) :
1863 vppcom_session_disconnect (session_index);
1866 clib_spinlock_lock (&vcm->sessions_lockp);
1867 pool_put_index (vcm->sessions, session_index);
1868 clib_spinlock_unlock (&vcm->sessions_lockp);
1873 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
1875 vppcom_main_t *vcm = &vppcom_main;
1876 session_t *session = 0;
1878 ip46_address_t *ip46;
1881 return VPPCOM_EINVAL;
1883 clib_spinlock_lock (&vcm->sessions_lockp);
1884 rv = vppcom_session_at_index (session_index, &session);
1885 if (PREDICT_FALSE (rv))
1887 clib_spinlock_unlock (&vcm->sessions_lockp);
1888 if (VPPCOM_DEBUG > 0)
1889 clib_warning ("[%d] invalid session, sid (%d) has been closed!",
1890 vcm->my_pid, session_index);
1894 if (VPPCOM_DEBUG > 0)
1895 clib_warning ("[%d] sid %d", vcm->my_pid, session_index);
1897 session->vrf = ep->vrf;
1898 session->is_ip4 = ep->is_ip4;
1899 memset (session->ip, 0, sizeof (session->ip));
1900 ip46 = (ip46_address_t *) session->ip;
1901 *ip46 = to_ip46 (!ep->is_ip4, ep->ip);
1902 session->port = ep->port;
1904 clib_spinlock_unlock (&vcm->sessions_lockp);
1909 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
1911 vppcom_main_t *vcm = &vppcom_main;
1912 session_t *listen_session = 0;
1915 clib_spinlock_lock (&vcm->sessions_lockp);
1916 rv = vppcom_session_at_index (listen_session_index, &listen_session);
1917 if (PREDICT_FALSE (rv))
1919 clib_spinlock_unlock (&vcm->sessions_lockp);
1920 if (VPPCOM_DEBUG > 0)
1921 clib_warning ("[%d] invalid session, sid (%d) has been closed!",
1922 vcm->my_pid, listen_session_index);
1926 if (VPPCOM_DEBUG > 0)
1927 clib_warning ("[%d] sid %d", vcm->my_pid, listen_session_index);
1929 ASSERT (vcm->bind_session_index == ~0);
1930 vcm->bind_session_index = listen_session_index;
1931 vppcom_send_bind_sock (listen_session);
1932 clib_spinlock_unlock (&vcm->sessions_lockp);
1934 vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
1935 vcm->cfg.session_timeout);
1936 if (PREDICT_FALSE (rv))
1938 vcm->bind_session_index = ~0;
1939 if (VPPCOM_DEBUG > 0)
1940 clib_warning ("[%d] server listen timed out, rv = %d (%d)",
1941 vcm->my_pid, vppcom_retval_str (rv), rv);
1945 clib_spinlock_lock (&vcm->sessions_lockp);
1946 rv = vppcom_session_at_index (listen_session_index, &listen_session);
1947 if (PREDICT_FALSE (rv))
1949 clib_spinlock_unlock (&vcm->sessions_lockp);
1950 if (VPPCOM_DEBUG > 0)
1951 clib_warning ("[%d] invalid session, sid (%d) has been closed!",
1952 vcm->my_pid, listen_session_index);
1955 listen_session->is_listen = 1;
1956 clib_spinlock_unlock (&vcm->sessions_lockp);
1957 clib_fifo_validate (vcm->client_session_index_fifo, q_len);
1963 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
1964 double wait_for_time)
1966 vppcom_main_t *vcm = &vppcom_main;
1967 session_t *listen_session = 0;
1968 session_t *client_session = 0;
1969 u32 client_session_index;
1973 clib_spinlock_lock (&vcm->sessions_lockp);
1974 rv = vppcom_session_at_index (listen_session_index, &listen_session);
1975 if (PREDICT_FALSE (rv))
1977 clib_spinlock_unlock (&vcm->sessions_lockp);
1978 if (VPPCOM_DEBUG > 0)
1979 clib_warning ("[%d] invalid session, sid (%d) has been closed!",
1980 vcm->my_pid, listen_session_index);
1984 if (listen_session->state != STATE_LISTEN)
1986 clib_spinlock_unlock (&vcm->sessions_lockp);
1987 if (VPPCOM_DEBUG > 0)
1988 clib_warning ("[%d] session not in listen state, state = %s",
1990 vppcom_session_state_str (listen_session->state));
1991 return VPPCOM_EBADFD;
1993 wait_for = listen_session->is_nonblocking ? 0 :
1994 (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time;
1996 if (VPPCOM_DEBUG > 0)
1997 clib_warning ("[%d] sid %d: %s (%d)", vcm->my_pid,
1998 listen_session_index,
1999 vppcom_session_state_str (listen_session->state),
2000 listen_session->state);
2001 clib_spinlock_unlock (&vcm->sessions_lockp);
2005 rv = vppcom_wait_for_client_session_index (wait_for);
2008 if ((VPPCOM_DEBUG > 0))
2009 clib_warning ("[%d] sid %d, accept timed out, rv = %s (%d)",
2010 vcm->my_pid, listen_session_index,
2011 vppcom_retval_str (rv), rv);
2012 if ((wait_for == 0) || (wait_for_time > 0))
2019 clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
2021 clib_spinlock_lock (&vcm->sessions_lockp);
2022 rv = vppcom_session_at_index (client_session_index, &client_session);
2023 ASSERT (rv == VPPCOM_OK);
2024 ASSERT (client_session->is_ip4 == listen_session->is_ip4);
2026 if (VPPCOM_DEBUG > 0)
2027 clib_warning ("[%d] Got a request: client sid %d", vcm->my_pid,
2028 client_session_index);
2030 ep->vrf = client_session->vrf;
2031 ep->is_cut_thru = client_session->is_cut_thru;
2032 ep->is_ip4 = client_session->is_ip4;
2033 ep->port = client_session->port;
2034 if (client_session->is_ip4)
2035 clib_memcpy (ep->ip, client_session->ip, sizeof (ip4_address_t));
2037 clib_memcpy (ep->ip, client_session->ip, sizeof (ip6_address_t));
2038 clib_spinlock_unlock (&vcm->sessions_lockp);
2039 return (int) client_session_index;
2043 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2045 vppcom_main_t *vcm = &vppcom_main;
2046 session_t *session = 0;
2048 ip46_address_t *ip46;
2050 clib_spinlock_lock (&vcm->sessions_lockp);
2051 rv = vppcom_session_at_index (session_index, &session);
2052 if (PREDICT_FALSE (rv))
2054 clib_spinlock_unlock (&vcm->sessions_lockp);
2055 if (VPPCOM_DEBUG > 0)
2056 clib_warning ("[%d] invalid session, sid (%d) has been closed!",
2057 vcm->my_pid, session_index);
2061 if (session->state == STATE_CONNECT)
2063 clib_spinlock_unlock (&vcm->sessions_lockp);
2064 if (VPPCOM_DEBUG > 0)
2065 clib_warning ("[%d] session, sid (%d) already connected!",
2066 vcm->my_pid, session_index);
2070 session->vrf = server_ep->vrf;
2071 session->is_ip4 = server_ep->is_ip4;
2072 memset (session->ip, 0, sizeof (session->ip));
2073 ip46 = (ip46_address_t *) session->ip;
2074 *ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
2075 session->port = server_ep->port;
2077 if (VPPCOM_DEBUG > 0)
2079 u8 *ip_str = format (0, "%U", format_ip46_address,
2080 &session->ip, session->is_ip4);
2081 clib_warning ("[%d] connect sid %d to %s server port %d",
2082 vcm->my_pid, session_index, ip_str,
2083 clib_net_to_host_u16 (session->port));
2087 vppcom_send_connect_sock (session, session_index);
2088 clib_spinlock_unlock (&vcm->sessions_lockp);
2089 rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2090 vcm->cfg.session_timeout);
2091 if (PREDICT_FALSE (rv))
2093 if (VPPCOM_DEBUG > 0)
2094 clib_warning ("[%d] connect timed out, rv = %s (%d)",
2095 vcm->my_pid, vppcom_retval_str (rv), rv);
2102 vppcom_session_read (uint32_t session_index, void *buf, int n)
2104 vppcom_main_t *vcm = &vppcom_main;
2105 session_t *session = 0;
2106 svm_fifo_t *rx_fifo;
2114 clib_spinlock_lock (&vcm->sessions_lockp);
2115 rv = vppcom_session_at_index (session_index, &session);
2116 if (PREDICT_FALSE (rv))
2118 clib_spinlock_unlock (&vcm->sessions_lockp);
2119 if (VPPCOM_DEBUG > 0)
2120 clib_warning ("[%d] invalid session, sid (%d) has been closed!",
2121 vcm->my_pid, session_index);
2125 if (session->state == STATE_DISCONNECT)
2127 clib_spinlock_unlock (&vcm->sessions_lockp);
2128 if (VPPCOM_DEBUG > 0)
2129 clib_warning ("[%d] sid (%d) has been closed by remote peer!",
2130 vcm->my_pid, session_index);
2131 return VPPCOM_ECONNRESET;
2134 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2135 session->server_rx_fifo : session->server_tx_fifo);
2136 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2137 "server_rx_fifo" : "server_tx_fifo");
2138 clib_spinlock_unlock (&vcm->sessions_lockp);
2140 max_dequeue = (int) svm_fifo_max_dequeue (rx_fifo);
2141 n_read = svm_fifo_dequeue_nowait (rx_fifo, clib_min (n, max_dequeue), buf);
2143 if (VPPCOM_DEBUG > 2)
2144 clib_warning ("[%d] sid %d, read %d bytes from %s (%p)", vcm->my_pid,
2145 session_index, n_read, fifo_str, rx_fifo);
2147 return (n_read <= 0) ? VPPCOM_EAGAIN : n_read;
2151 vppcom_session_read_ready (session_t * session, u32 session_index)
2153 vppcom_main_t *vcm = &vppcom_main;
2154 svm_fifo_t *rx_fifo;
2157 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2158 if (session->state == STATE_DISCONNECT)
2160 if (VPPCOM_DEBUG > 0)
2161 clib_warning ("[%d] sid (%d) has been closed by remote peer!",
2162 vcm->my_pid, session_index);
2163 return VPPCOM_ECONNRESET;
2166 if (session->is_listen)
2167 ready = clib_fifo_elts (vcm->client_session_index_fifo);
2170 rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2171 session->server_rx_fifo : session->server_tx_fifo);
2173 ready = svm_fifo_max_dequeue (rx_fifo);
2176 if (VPPCOM_DEBUG > 3)
2177 clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", vcm->my_pid,
2179 session->is_server ? "server_rx_fifo" : "server_tx_fifo",
2185 vppcom_session_write (uint32_t session_index, void *buf, int n)
2187 vppcom_main_t *vcm = &vppcom_main;
2188 session_t *session = 0;
2189 svm_fifo_t *tx_fifo;
2190 unix_shared_memory_queue_t *q;
2191 session_fifo_event_t evt;
2198 clib_spinlock_lock (&vcm->sessions_lockp);
2199 rv = vppcom_session_at_index (session_index, &session);
2200 if (PREDICT_FALSE (rv))
2202 clib_spinlock_unlock (&vcm->sessions_lockp);
2203 if (VPPCOM_DEBUG > 0)
2204 clib_warning ("[%d] invalid session, sid (%d) has been closed!",
2205 vcm->my_pid, session_index);
2209 if (session->state == STATE_DISCONNECT)
2211 clib_spinlock_unlock (&vcm->sessions_lockp);
2212 if (VPPCOM_DEBUG > 0)
2213 clib_warning ("[%d] sid (%d) has been closed by remote peer!",
2214 vcm->my_pid, session_index);
2215 return VPPCOM_ECONNRESET;
2218 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2219 session->server_tx_fifo : session->server_rx_fifo);
2220 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2221 "server_tx_fifo" : "server_rx_fifo");
2223 is_nonblocking = session->is_nonblocking;
2224 clib_spinlock_unlock (&vcm->sessions_lockp);
2228 rv = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
2230 while (!is_nonblocking && (rv <= 0));
2232 /* If event wasn't set, add one */
2233 if (!session->is_cut_thru && (rv > 0) && svm_fifo_set_event (tx_fifo))
2237 /* Fabricate TX event, send to vpp */
2239 evt.event_type = FIFO_EVENT_APP_TX;
2240 evt.event_id = vcm->tx_event_id++;
2242 clib_spinlock_lock (&vcm->sessions_lockp);
2243 rval = vppcom_session_at_index (session_index, &session);
2244 if (PREDICT_FALSE (rval))
2246 clib_spinlock_unlock (&vcm->sessions_lockp);
2247 if (VPPCOM_DEBUG > 1)
2248 clib_warning ("[%d] invalid session, sid (%d) has been closed!",
2249 vcm->my_pid, session_index);
2252 q = session->vpp_event_queue;
2253 clib_spinlock_unlock (&vcm->sessions_lockp);
2255 unix_shared_memory_queue_add (q, (u8 *) & evt,
2256 0 /* do wait for mutex */ );
2259 if (VPPCOM_DEBUG > 2)
2260 clib_warning ("[%d] sid %d, wrote %d bytes to %s (%p)", vcm->my_pid,
2261 session_index, rv, fifo_str, tx_fifo);
2267 vppcom_session_write_ready (session_t * session, u32 session_index)
2269 vppcom_main_t *vcm = &vppcom_main;
2270 svm_fifo_t *tx_fifo;
2274 /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2275 if (session->state == STATE_DISCONNECT)
2277 if (VPPCOM_DEBUG > 0)
2278 clib_warning ("[%d] sid (%d) has been closed by remote peer!",
2279 vcm->my_pid, session_index);
2280 return VPPCOM_ECONNRESET;
2283 tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2284 session->server_tx_fifo : session->server_rx_fifo);
2285 fifo_str = ((!session->is_cut_thru || session->is_server) ?
2286 "server_tx_fifo" : "server_rx_fifo");
2288 rv = svm_fifo_max_enqueue (tx_fifo);
2290 if (VPPCOM_DEBUG > 3)
2291 clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", vcm->my_pid,
2292 session_index, fifo_str, tx_fifo, rv);
2297 vppcom_select (unsigned long n_bits, unsigned long *read_map,
2298 unsigned long *write_map, unsigned long *except_map,
2299 double time_to_wait)
2301 vppcom_main_t *vcm = &vppcom_main;
2303 session_t *session = 0;
2304 int rv, bits_set = 0;
2305 f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2306 u32 minbits = clib_max (n_bits, BITS (uword));
2308 ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2312 clib_bitmap_validate (vcm->rd_bitmap, minbits);
2313 clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
2314 memset (read_map, 0, vec_len (vcm->rd_bitmap));
2318 clib_bitmap_validate (vcm->wr_bitmap, minbits);
2319 clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
2320 memset (write_map, 0, vec_len (vcm->wr_bitmap));
2324 clib_bitmap_validate (vcm->ex_bitmap, minbits);
2325 clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
2326 memset (except_map, 0, vec_len (vcm->ex_bitmap));
2332 clib_bitmap_foreach (session_index, vcm->rd_bitmap,
2334 clib_spinlock_lock (&vcm->sessions_lockp);
2335 rv = vppcom_session_at_index (session_index, &session);
2338 clib_spinlock_unlock (&vcm->sessions_lockp);
2339 if (VPPCOM_DEBUG > 1)
2340 clib_warning ("[%d] session %d specified in "
2341 "read_map is closed.", vcm->my_pid,
2343 bits_set = VPPCOM_EBADFD;
2347 rv = vppcom_session_read_ready (session, session_index);
2348 clib_spinlock_unlock (&vcm->sessions_lockp);
2349 if (vcm->ex_bitmap &&
2350 clib_bitmap_get (vcm->ex_bitmap, session_index) && (rv < 0))
2352 // TBD: clib_warning
2353 clib_bitmap_set_no_check (except_map, session_index, 1);
2358 // TBD: clib_warning
2359 clib_bitmap_set_no_check (read_map, session_index, 1);
2364 clib_bitmap_foreach (session_index, vcm->wr_bitmap,
2366 clib_spinlock_lock (&vcm->sessions_lockp);
2367 rv = vppcom_session_at_index (session_index, &session);
2370 clib_spinlock_unlock (&vcm->sessions_lockp);
2371 if (VPPCOM_DEBUG > 0)
2372 clib_warning ("[%d] session %d specified in "
2373 "write_map is closed.", vcm->my_pid,
2375 bits_set = VPPCOM_EBADFD;
2379 rv = vppcom_session_write_ready (session, session_index);
2380 clib_spinlock_unlock (&vcm->sessions_lockp);
2383 // TBD: clib_warning
2384 clib_bitmap_set_no_check (write_map, session_index, 1);
2389 clib_bitmap_foreach (session_index, vcm->ex_bitmap,
2391 clib_spinlock_lock (&vcm->sessions_lockp);
2392 rv = vppcom_session_at_index (session_index, &session);
2395 clib_spinlock_unlock (&vcm->sessions_lockp);
2396 if (VPPCOM_DEBUG > 1)
2397 clib_warning ("[%d] session %d specified in "
2398 "except_map is closed.", vcm->my_pid,
2400 bits_set = VPPCOM_EBADFD;
2404 rv = vppcom_session_read_ready (session, session_index);
2405 clib_spinlock_unlock (&vcm->sessions_lockp);
2408 // TBD: clib_warning
2409 clib_bitmap_set_no_check (except_map, session_index, 1);
2415 while (clib_time_now (&vcm->clib_time) < timeout);
2422 * fd.io coding-style-patch-verification: ON
2425 * eval: (c-set-style "gnu")