blob: 662805373ba8292062b76abc9bfff8be84aab1ea [file] [log] [blame]
Damjan Marion7cd468a2016-12-19 23:05:39 +01001/*
2 *------------------------------------------------------------------
Damjan Marion7cd468a2016-12-19 23:05:39 +01003 * Copyright (c) 2009 Cisco and/or its affiliates.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at:
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *------------------------------------------------------------------
16 */
17
Florin Corase86a8ed2018-01-05 03:20:25 -080018#ifndef included_vlibmemory_api_common_h
19#define included_vlibmemory_api_common_h
Damjan Marion7cd468a2016-12-19 23:05:39 +010020
Florin Corase86a8ed2018-01-05 03:20:25 -080021#include <svm/svm_common.h>
Damjan Marion7cd468a2016-12-19 23:05:39 +010022#include <vlibapi/api.h>
Florin Corase86a8ed2018-01-05 03:20:25 -080023#include <vlibmemory/memory_api.h>
24#include <vlibmemory/memory_client.h>
25#include <vlibmemory/socket_api.h>
26#include <vlibmemory/socket_client.h>
Damjan Marion7cd468a2016-12-19 23:05:39 +010027
Florin Corase86a8ed2018-01-05 03:20:25 -080028void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
Neale Ranns6bcc6a42019-10-15 15:47:55 +000029void vl_api_force_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
Florin Corase86a8ed2018-01-05 03:20:25 -080030u16 vl_client_get_first_plugin_msg_id (const char *plugin_name);
31void vl_api_send_pending_rpc_requests (vlib_main_t * vm);
32u8 *vl_api_serialize_message_table (api_main_t * am, u8 * vector);
33
34always_inline void
35vl_api_send_msg (vl_api_registration_t * rp, u8 * elem)
Damjan Marion7cd468a2016-12-19 23:05:39 +010036{
Florin Corase86a8ed2018-01-05 03:20:25 -080037 if (PREDICT_FALSE (rp->registration_type > REGISTRATION_TYPE_SHMEM))
38 {
39 vl_socket_api_send (rp, elem);
40 }
41 else
42 {
43 vl_msg_api_send_shmem (rp->vl_input_queue, (u8 *) & elem);
44 }
Damjan Marion7cd468a2016-12-19 23:05:39 +010045}
46
Florin Corasaf0ff5a2018-01-10 08:17:03 -080047always_inline int
48vl_api_can_send_msg (vl_api_registration_t * rp)
49{
50 if (PREDICT_FALSE (rp->registration_type > REGISTRATION_TYPE_SHMEM))
51 return 1;
52 else
53 return vl_mem_api_can_send (rp->vl_input_queue);
54}
55
Ole Troanf5db3712020-05-20 15:47:06 +020056/*
57 * Suggests to an API handler to relinguish control. Currently limits
58 * an handler to a maximum of 1ms or it earlier if the client queue is
59 * full.
60 *
61 * May be enhanced in the future based on other performance
62 * characteristics of the main thread.
63 */
64#define VL_API_MAX_TIME_IN_HANDLER 0.001 /* 1 ms */
65always_inline int
66vl_api_process_may_suspend (vlib_main_t * vm, vl_api_registration_t * rp,
67 f64 start)
68{
69 /* Is client queue full (leave space for reply message) */
70 if (rp->registration_type <= REGISTRATION_TYPE_SHMEM &&
71 rp->vl_input_queue->cursize + 1 >= rp->vl_input_queue->maxsize)
72 return true;
73 if (vlib_time_now (vm) > start + VL_API_MAX_TIME_IN_HANDLER)
74 return true;
75 return false;
76}
77
Florin Corase86a8ed2018-01-05 03:20:25 -080078always_inline vl_api_registration_t *
79vl_api_client_index_to_registration (u32 index)
Damjan Marion7cd468a2016-12-19 23:05:39 +010080{
Florin Coras2881dec2018-10-02 18:29:25 -070081 if (vl_socket_api_registration_handle_is_valid (ntohl (index)))
82 return vl_socket_api_client_handle_to_registration (ntohl (index));
Ole Troan94495f22018-08-02 11:58:12 +020083 return vl_mem_api_client_index_to_registration (index);
Damjan Marion7cd468a2016-12-19 23:05:39 +010084}
85
Florin Corasb384b542018-01-15 01:08:33 -080086always_inline u32
87vl_api_registration_file_index (vl_api_registration_t * reg)
88{
89 return reg->clib_file_index;
90}
91
92always_inline clib_file_t *
93vl_api_registration_file (vl_api_registration_t * reg)
94{
95 return clib_file_get (&file_main, vl_api_registration_file_index (reg));
96}
97
98always_inline void
99vl_api_registration_del_file (vl_api_registration_t * reg)
100{
101 clib_file_t *cf = vl_api_registration_file (reg);
102 if (cf)
103 clib_file_del (&file_main, cf);
104}
105
106always_inline clib_error_t *
Florin Coras466f2892018-08-03 02:50:43 -0700107vl_api_send_fd_msg (vl_api_registration_t * reg, int fds[], int n_fds)
Florin Corasb384b542018-01-15 01:08:33 -0800108{
109 clib_file_t *cf = vl_api_registration_file (reg);
110 if (cf)
Florin Coras466f2892018-08-03 02:50:43 -0700111 return vl_sock_api_send_fd_msg (cf->file_descriptor, fds, n_fds);
Florin Corasb384b542018-01-15 01:08:33 -0800112 return 0;
113}
114
115always_inline clib_error_t *
Florin Coras466f2892018-08-03 02:50:43 -0700116vl_api_recv_fd_msg (vl_api_registration_t * reg, int fds[], int n_fds,
117 u32 wait)
Florin Corasb384b542018-01-15 01:08:33 -0800118{
119 clib_file_t *cf = vl_api_registration_file (reg);
120 if (cf)
Florin Coras466f2892018-08-03 02:50:43 -0700121 return vl_sock_api_recv_fd_msg (cf->file_descriptor, fds, n_fds, wait);
Florin Corasb384b542018-01-15 01:08:33 -0800122 return 0;
123}
124
Florin Corase86a8ed2018-01-05 03:20:25 -0800125/*
126 * vl_api_clnt process data used by transports (socket api in particular)
127 */
128extern vlib_node_registration_t vl_api_clnt_node;
129extern volatile int **vl_api_queue_cursizes;
130
131typedef enum vl_api_clnt_process_events
Damjan Marion7cd468a2016-12-19 23:05:39 +0100132{
Florin Corase86a8ed2018-01-05 03:20:25 -0800133 QUEUE_SIGNAL_EVENT = 1,
134 SOCKET_READ_EVENT
135} vl_api_clnt_process_events_t;
Damjan Marion7cd468a2016-12-19 23:05:39 +0100136
Florin Corase86a8ed2018-01-05 03:20:25 -0800137#define foreach_histogram_bucket \
138_(400) \
139_(200) \
140_(100) \
141_(10)
Damjan Marion7cd468a2016-12-19 23:05:39 +0100142
Florin Corase86a8ed2018-01-05 03:20:25 -0800143typedef enum
144{
145#define _(n) SLEEP_##n##_US,
146 foreach_histogram_bucket
147#undef _
148 SLEEP_N_BUCKETS,
149} histogram_index_t;
Dave Barach59b25652017-09-10 15:04:27 -0400150
Florin Corase86a8ed2018-01-05 03:20:25 -0800151extern u64 vector_rate_histogram[];
152
153/*
154 * sockclnt APIs XXX are these actually used anywhere?
155 */
156vl_api_registration_t *sockclnt_get_registration (u32 index);
157void socksvr_add_pending_output (struct clib_file *uf,
158 struct vl_api_registration_ *cf,
159 u8 * buffer, uword buffer_bytes);
160void vl_socket_process_msg (struct clib_file *uf,
161 struct vl_api_registration_ *rp, i8 * input_v);
162u32 sockclnt_open_index (char *client_name, char *hostname, int port);
163void sockclnt_close_index (u32 index);
164void vl_client_msg_api_send (vl_api_registration_t * cm, u8 * elem);
165
166#endif /* included_vlibmemory_api_common_h */
Damjan Marion7cd468a2016-12-19 23:05:39 +0100167
168/*
169 * fd.io coding-style-patch-verification: ON
170 *
171 * Local Variables:
172 * eval: (c-set-style "gnu")
173 * End:
174 */