vcl: refactor session state enum
[vpp.git] / src / vcl / vcl_private.c
1 /*
2  * Copyright (c) 2018-2019 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this
5  * You may obtain a copy of the License at:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15
16 #include <vcl/vcl_private.h>
17
18 static pthread_key_t vcl_worker_stop_key;
19
20 vcl_mq_evt_conn_t *
21 vcl_mq_evt_conn_alloc (vcl_worker_t * wrk)
22 {
23   vcl_mq_evt_conn_t *mqc;
24   pool_get (wrk->mq_evt_conns, mqc);
25   memset (mqc, 0, sizeof (*mqc));
26   return mqc;
27 }
28
29 u32
30 vcl_mq_evt_conn_index (vcl_worker_t * wrk, vcl_mq_evt_conn_t * mqc)
31 {
32   return (mqc - wrk->mq_evt_conns);
33 }
34
35 vcl_mq_evt_conn_t *
36 vcl_mq_evt_conn_get (vcl_worker_t * wrk, u32 mq_conn_idx)
37 {
38   return pool_elt_at_index (wrk->mq_evt_conns, mq_conn_idx);
39 }
40
41 int
42 vcl_mq_epoll_add_evfd (vcl_worker_t * wrk, svm_msg_q_t * mq)
43 {
44   struct epoll_event e = { 0 };
45   vcl_mq_evt_conn_t *mqc;
46   u32 mqc_index;
47   int mq_fd;
48
49   mq_fd = svm_msg_q_get_consumer_eventfd (mq);
50
51   if (wrk->mqs_epfd < 0 || mq_fd == -1)
52     return -1;
53
54   mqc = vcl_mq_evt_conn_alloc (wrk);
55   mqc_index = vcl_mq_evt_conn_index (wrk, mqc);
56   mqc->mq_fd = mq_fd;
57   mqc->mq = mq;
58
59   e.events = EPOLLIN;
60   e.data.u32 = mqc_index;
61   if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_ADD, mq_fd, &e) < 0)
62     {
63       VDBG (0, "failed to add mq eventfd to mq epoll fd");
64       return -1;
65     }
66
67   return mqc_index;
68 }
69
70 int
71 vcl_mq_epoll_del_evfd (vcl_worker_t * wrk, u32 mqc_index)
72 {
73   vcl_mq_evt_conn_t *mqc;
74
75   if (wrk->mqs_epfd || mqc_index == ~0)
76     return -1;
77
78   mqc = vcl_mq_evt_conn_get (wrk, mqc_index);
79   if (epoll_ctl (wrk->mqs_epfd, EPOLL_CTL_DEL, mqc->mq_fd, 0) < 0)
80     {
81       VDBG (0, "failed to del mq eventfd to mq epoll fd");
82       return -1;
83     }
84   return 0;
85 }
86
87 static vcl_worker_t *
88 vcl_worker_alloc (void)
89 {
90   vcl_worker_t *wrk;
91   pool_get (vcm->workers, wrk);
92   memset (wrk, 0, sizeof (*wrk));
93   wrk->wrk_index = wrk - vcm->workers;
94   wrk->forked_child = ~0;
95   return wrk;
96 }
97
98 static void
99 vcl_worker_free (vcl_worker_t * wrk)
100 {
101   pool_put (vcm->workers, wrk);
102 }
103
104 int
105 vcl_api_app_worker_add (void)
106 {
107   if (vcm->cfg.vpp_app_socket_api)
108     return vcl_sapi_app_worker_add ();
109
110   return vcl_bapi_app_worker_add ();
111 }
112
113 void
114 vcl_api_app_worker_del (vcl_worker_t * wrk)
115 {
116   if (vcm->cfg.vpp_app_socket_api)
117     return vcl_sapi_app_worker_del (wrk);
118
119   vcl_bapi_app_worker_del (wrk);
120 }
121
122 void
123 vcl_worker_cleanup (vcl_worker_t * wrk, u8 notify_vpp)
124 {
125   clib_spinlock_lock (&vcm->workers_lock);
126   if (notify_vpp)
127     vcl_api_app_worker_del (wrk);
128
129   if (wrk->mqs_epfd > 0)
130     close (wrk->mqs_epfd);
131   hash_free (wrk->session_index_by_vpp_handles);
132   vec_free (wrk->mq_events);
133   vec_free (wrk->mq_msg_vector);
134   vcl_worker_free (wrk);
135   clib_spinlock_unlock (&vcm->workers_lock);
136 }
137
138 static void
139 vcl_worker_cleanup_cb (void *arg)
140 {
141   vcl_worker_t *wrk = vcl_worker_get_current ();
142   u32 wrk_index = wrk->wrk_index;
143   vcl_worker_cleanup (wrk, 1 /* notify vpp */ );
144   vcl_set_worker_index (~0);
145   VDBG (0, "cleaned up worker %u", wrk_index);
146 }
147
148 vcl_worker_t *
149 vcl_worker_alloc_and_init ()
150 {
151   vcl_worker_t *wrk;
152
153   /* This was initialized already */
154   if (vcl_get_worker_index () != ~0)
155     return 0;
156
157   /* Use separate heap map entry for worker */
158   clib_mem_set_thread_index ();
159
160   if (pool_elts (vcm->workers) == vcm->cfg.max_workers)
161     {
162       VDBG (0, "max-workers %u limit reached", vcm->cfg.max_workers);
163       return 0;
164     }
165
166   clib_spinlock_lock (&vcm->workers_lock);
167   wrk = vcl_worker_alloc ();
168   vcl_set_worker_index (wrk->wrk_index);
169   wrk->thread_id = pthread_self ();
170   wrk->current_pid = getpid ();
171
172   wrk->mqs_epfd = -1;
173   if (vcm->cfg.use_mq_eventfd)
174     {
175       wrk->vcl_needs_real_epoll = 1;
176       wrk->mqs_epfd = epoll_create (1);
177       wrk->vcl_needs_real_epoll = 0;
178       if (wrk->mqs_epfd < 0)
179         {
180           clib_unix_warning ("epoll_create() returned");
181           goto done;
182         }
183     }
184
185   wrk->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
186   clib_time_init (&wrk->clib_time);
187   vec_validate (wrk->mq_events, 64);
188   vec_validate (wrk->mq_msg_vector, 128);
189   vec_reset_length (wrk->mq_msg_vector);
190   vec_validate (wrk->unhandled_evts_vector, 128);
191   vec_reset_length (wrk->unhandled_evts_vector);
192   clib_spinlock_unlock (&vcm->workers_lock);
193
194 done:
195   return wrk;
196 }
197
198 int
199 vcl_worker_register_with_vpp (void)
200 {
201   vcl_worker_t *wrk = vcl_worker_get_current ();
202
203   clib_spinlock_lock (&vcm->workers_lock);
204
205   if (vcl_api_app_worker_add ())
206     {
207       VDBG (0, "failed to add worker to vpp");
208       clib_spinlock_unlock (&vcm->workers_lock);
209       return -1;
210     }
211   if (pthread_key_create (&vcl_worker_stop_key, vcl_worker_cleanup_cb))
212     VDBG (0, "failed to add pthread cleanup function");
213   if (pthread_setspecific (vcl_worker_stop_key, &wrk->thread_id))
214     VDBG (0, "failed to setup key value");
215
216   clib_spinlock_unlock (&vcm->workers_lock);
217
218   VDBG (0, "added worker %u", wrk->wrk_index);
219   return 0;
220 }
221
222 svm_msg_q_t *
223 vcl_worker_ctrl_mq (vcl_worker_t * wrk)
224 {
225   return wrk->ctrl_mq;
226 }
227
228 int
229 vcl_session_read_ready (vcl_session_t * s)
230 {
231   u32 max_deq;
232
233   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
234   if (PREDICT_FALSE (s->is_vep))
235     {
236       VDBG (0, "ERROR: session %u: cannot read from an epoll session!",
237             s->session_index);
238       return VPPCOM_EBADFD;
239     }
240
241   if (PREDICT_FALSE (!(vcl_session_is_ready (s)
242                        || s->session_state == VCL_STATE_LISTEN)))
243     {
244       vcl_session_state_t state = s->session_state;
245       int rv;
246
247       rv = (state == VCL_STATE_DISCONNECT) ?
248         VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
249
250       VDBG (1, "session %u [0x%llx]: not open! state 0x%x (%s), ret %d (%s)",
251             s->session_index, s->vpp_handle, state,
252             vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
253       return rv;
254     }
255
256   if (s->session_state == VCL_STATE_LISTEN)
257     return clib_fifo_elts (s->accept_evts_fifo);
258
259   if (vcl_session_is_ct (s))
260     return svm_fifo_max_dequeue_cons (s->ct_rx_fifo);
261
262   max_deq = svm_fifo_max_dequeue_cons (s->rx_fifo);
263
264   if (s->is_dgram)
265     {
266       session_dgram_pre_hdr_t ph;
267
268       if (max_deq <= SESSION_CONN_HDR_LEN)
269         return 0;
270       if (svm_fifo_peek (s->rx_fifo, 0, sizeof (ph), (u8 *) & ph) < 0)
271         return 0;
272       if (ph.data_length + SESSION_CONN_HDR_LEN > max_deq)
273         return 0;
274
275       return ph.data_length;
276     }
277
278   return max_deq;
279 }
280
281 int
282 vcl_session_write_ready (vcl_session_t * s)
283 {
284   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
285   if (PREDICT_FALSE (s->is_vep))
286     {
287       VDBG (0, "session %u [0x%llx]: cannot write to an epoll session!",
288             s->session_index, s->vpp_handle);
289       return VPPCOM_EBADFD;
290     }
291
292   if (PREDICT_FALSE (s->session_state == VCL_STATE_LISTEN))
293     {
294       if (s->tx_fifo)
295         return svm_fifo_max_enqueue_prod (s->tx_fifo);
296       else
297         return VPPCOM_EBADFD;
298     }
299
300   if (PREDICT_FALSE (!vcl_session_is_ready (s)))
301     {
302       vcl_session_state_t state = s->session_state;
303       int rv;
304
305       rv = (state == VCL_STATE_DISCONNECT) ?
306         VPPCOM_ECONNRESET : VPPCOM_ENOTCONN;
307       VDBG (0, "session %u [0x%llx]: not open! state 0x%x (%s), ret %d (%s)",
308             s->session_index, s->vpp_handle, state,
309             vppcom_session_state_str (state), rv, vppcom_retval_str (rv));
310       return rv;
311     }
312
313   if (vcl_session_is_ct (s))
314     return svm_fifo_max_enqueue_prod (s->ct_tx_fifo);
315
316   if (s->is_dgram)
317     {
318       u32 max_enq = svm_fifo_max_enqueue_prod (s->tx_fifo);
319
320       if (max_enq <= sizeof (session_dgram_hdr_t))
321         return 0;
322       return max_enq - sizeof (session_dgram_hdr_t);
323     }
324
325   return svm_fifo_max_enqueue_prod (s->tx_fifo);
326 }
327
328 int
329 vcl_segment_attach (u64 segment_handle, char *name, ssvm_segment_type_t type,
330                     int fd)
331 {
332   fifo_segment_create_args_t _a, *a = &_a;
333   int rv;
334
335   memset (a, 0, sizeof (*a));
336   a->segment_name = name;
337   a->segment_type = type;
338
339   if (type == SSVM_SEGMENT_MEMFD)
340     a->memfd_fd = fd;
341
342   clib_rwlock_writer_lock (&vcm->segment_table_lock);
343
344   if ((rv = fifo_segment_attach (&vcm->segment_main, a)))
345     {
346       clib_warning ("svm_fifo_segment_attach ('%s') failed", name);
347       return rv;
348     }
349   hash_set (vcm->segment_table, segment_handle, a->new_segment_indices[0]);
350
351   clib_rwlock_writer_unlock (&vcm->segment_table_lock);
352
353   vec_reset_length (a->new_segment_indices);
354   return 0;
355 }
356
357 u32
358 vcl_segment_table_lookup (u64 segment_handle)
359 {
360   uword *seg_indexp;
361
362   clib_rwlock_reader_lock (&vcm->segment_table_lock);
363   seg_indexp = hash_get (vcm->segment_table, segment_handle);
364   clib_rwlock_reader_unlock (&vcm->segment_table_lock);
365
366   if (!seg_indexp)
367     return VCL_INVALID_SEGMENT_INDEX;
368   return ((u32) * seg_indexp);
369 }
370
371 void
372 vcl_segment_detach (u64 segment_handle)
373 {
374   fifo_segment_main_t *sm = &vcm->segment_main;
375   fifo_segment_t *segment;
376   u32 segment_index;
377
378   segment_index = vcl_segment_table_lookup (segment_handle);
379   if (segment_index == (u32) ~ 0)
380     return;
381
382   clib_rwlock_writer_lock (&vcm->segment_table_lock);
383
384   segment = fifo_segment_get_segment (sm, segment_index);
385   fifo_segment_delete (sm, segment);
386   hash_unset (vcm->segment_table, segment_handle);
387
388   clib_rwlock_writer_unlock (&vcm->segment_table_lock);
389
390   VDBG (0, "detached segment %u handle %u", segment_index, segment_handle);
391 }
392
393
394 /*
395  * fd.io coding-style-patch-verification: ON
396  *
397  * Local Variables:
398  * eval: (c-set-style "gnu")
399  * End:
400  */