udp: fix csum computation when offload disabled
[vpp.git] / src / vlibmemory / api.h
1 /*
2  *------------------------------------------------------------------
3  * 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
18 #ifndef included_vlibmemory_api_common_h
19 #define included_vlibmemory_api_common_h
20
21 #include <svm/svm_common.h>
22 #include <vlibapi/api.h>
23 #include <vlibmemory/memory_api.h>
24 #include <vlibmemory/memory_client.h>
25 #include <vlibmemory/socket_api.h>
26 #include <vlibmemory/socket_client.h>
27
28 void vl_api_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
29 void vl_api_force_rpc_call_main_thread (void *fp, u8 * data, u32 data_length);
30 u16 vl_client_get_first_plugin_msg_id (const char *plugin_name);
31 u8 *vl_api_serialize_message_table (api_main_t * am, u8 * vector);
32
33 always_inline void
34 vl_api_send_msg (vl_api_registration_t * rp, u8 * elem)
35 {
36   if (PREDICT_FALSE (rp->registration_type > REGISTRATION_TYPE_SHMEM))
37     {
38       vl_socket_api_send (rp, elem);
39     }
40   else
41     {
42       vl_msg_api_send_shmem (rp->vl_input_queue, (u8 *) & elem);
43     }
44 }
45
46 always_inline int
47 vl_api_can_send_msg (vl_api_registration_t * rp)
48 {
49   if (PREDICT_FALSE (rp->registration_type > REGISTRATION_TYPE_SHMEM))
50     return 1;
51   else
52     return vl_mem_api_can_send (rp->vl_input_queue);
53 }
54
55 /*
56  * Suggests to an API handler to relinguish control. Currently limits
57  * an handler to a maximum of 1ms or it earlier if the client queue is
58  * full.
59  *
60  * May be enhanced in the future based on other performance
61  * characteristics of the main thread.
62  */
63 #define VL_API_MAX_TIME_IN_HANDLER 0.001        /* 1 ms */
64 always_inline int
65 vl_api_process_may_suspend (vlib_main_t * vm, vl_api_registration_t * rp,
66                             f64 start)
67 {
68   /* Is client queue full (leave space for reply message) */
69   if (rp->registration_type <= REGISTRATION_TYPE_SHMEM &&
70       rp->vl_input_queue->cursize + 1 >= rp->vl_input_queue->maxsize)
71     return true;
72   if (vlib_time_now (vm) > start + VL_API_MAX_TIME_IN_HANDLER)
73     return true;
74   return false;
75 }
76
77 always_inline vl_api_registration_t *
78 vl_api_client_index_to_registration (u32 index)
79 {
80   if (vl_socket_api_registration_handle_is_valid (ntohl (index)))
81     return vl_socket_api_client_handle_to_registration (ntohl (index));
82   return vl_mem_api_client_index_to_registration (index);
83 }
84
85 always_inline u32
86 vl_api_registration_file_index (vl_api_registration_t * reg)
87 {
88   return reg->clib_file_index;
89 }
90
91 always_inline clib_file_t *
92 vl_api_registration_file (vl_api_registration_t * reg)
93 {
94   return clib_file_get (&file_main, vl_api_registration_file_index (reg));
95 }
96
97 always_inline void
98 vl_api_registration_del_file (vl_api_registration_t * reg)
99 {
100   clib_file_t *cf = vl_api_registration_file (reg);
101   if (cf)
102     clib_file_del (&file_main, cf);
103 }
104
105 always_inline clib_error_t *
106 vl_api_send_fd_msg (vl_api_registration_t * reg, int fds[], int n_fds)
107 {
108   clib_file_t *cf = vl_api_registration_file (reg);
109   if (cf)
110     return vl_sock_api_send_fd_msg (cf->file_descriptor, fds, n_fds);
111   return 0;
112 }
113
114 always_inline clib_error_t *
115 vl_api_recv_fd_msg (vl_api_registration_t * reg, int fds[], int n_fds,
116                     u32 wait)
117 {
118   clib_file_t *cf = vl_api_registration_file (reg);
119   if (cf)
120     return vl_sock_api_recv_fd_msg (cf->file_descriptor, fds, n_fds, wait);
121   return 0;
122 }
123
124 /*
125  * vl_api_clnt process data used by transports (socket api in particular)
126  */
127 extern vlib_node_registration_t vl_api_clnt_node;
128 extern volatile int **vl_api_queue_cursizes;
129
130 typedef enum vl_api_clnt_process_events
131 {
132   QUEUE_SIGNAL_EVENT = 1,
133   SOCKET_READ_EVENT
134 } vl_api_clnt_process_events_t;
135
136 #define foreach_histogram_bucket                \
137 _(400)                                          \
138 _(200)                                          \
139 _(100)                                          \
140 _(10)
141
142 typedef enum
143 {
144 #define _(n) SLEEP_##n##_US,
145   foreach_histogram_bucket
146 #undef _
147     SLEEP_N_BUCKETS,
148 } histogram_index_t;
149
150 extern u64 vector_rate_histogram[];
151
152 /*
153  * sockclnt APIs XXX are these actually used anywhere?
154  */
155 vl_api_registration_t *sockclnt_get_registration (u32 index);
156 void socksvr_add_pending_output (struct clib_file *uf,
157                                  struct vl_api_registration_ *cf,
158                                  u8 * buffer, uword buffer_bytes);
159 void vl_socket_process_msg (struct clib_file *uf,
160                             struct vl_api_registration_ *rp, i8 * input_v);
161 u32 sockclnt_open_index (char *client_name, char *hostname, int port);
162 void sockclnt_close_index (u32 index);
163 void vl_client_msg_api_send (vl_api_registration_t * cm, u8 * elem);
164
165 #endif /* included_vlibmemory_api_common_h */
166
167 /*
168  * fd.io coding-style-patch-verification: ON
169  *
170  * Local Variables:
171  * eval: (c-set-style "gnu")
172  * End:
173  */