VCL-LDPRELOAD: statically link vppcom into libvcl-ldpreload.so
[vpp.git] / src / vcl / vppcom.c
1 /*
2  * Copyright (c) 2017 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 <stdio.h>
17 #include <stdlib.h>
18 #include <signal.h>
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>
26
27 #define vl_typedefs             /* define message structures */
28 #include <vpp/api/vpe_all_api_h.h>
29 #undef vl_typedefs
30
31 /* declare message handlers for each api */
32
33 #define vl_endianfun            /* define message structures */
34 #include <vpp/api/vpe_all_api_h.h>
35 #undef vl_endianfun
36
37 /* instantiate all the print functions we know about */
38 #define vl_print(handle, ...)
39 #define vl_printfun
40 #include <vpp/api/vpe_all_api_h.h>
41 #undef vl_printfun
42
43 #if (CLIB_DEBUG > 0)
44 /* Set VPPCOM_DEBUG 2 for connection debug, 3 for read/write debug output */
45 #define VPPCOM_DEBUG 1
46 #else
47 #define VPPCOM_DEBUG 0
48 #endif
49
50 /*
51  * VPPCOM Private definitions and functions.
52  */
53 typedef enum
54 {
55   STATE_APP_START,
56   STATE_APP_CONN_VPP,
57   STATE_APP_ENABLED,
58   STATE_APP_ATTACHED,
59 } app_state_t;
60
61 typedef enum
62 {
63   STATE_START,
64   STATE_CONNECT,
65   STATE_LISTEN,
66   STATE_ACCEPT,
67   STATE_DISCONNECT,
68   STATE_FAILED
69 } session_state_t;
70
71 typedef struct epoll_event vppcom_epoll_event_t;
72
73 typedef struct
74 {
75   u32 next_sid;
76   u32 prev_sid;
77   u32 vep_idx;
78   vppcom_epoll_event_t ev;
79 #define VEP_DEFAULT_ET_MASK  (EPOLLIN|EPOLLOUT)
80   u32 et_mask;
81 } vppcom_epoll_t;
82
83 typedef struct
84 {
85   u8 is_ip4;
86   ip46_address_t ip46;
87 } vppcom_ip46_t;
88
89 typedef struct
90 {
91   volatile session_state_t state;
92
93   svm_fifo_t *server_rx_fifo;
94   svm_fifo_t *server_tx_fifo;
95   u32 sm_seg_index;
96   u64 vpp_session_handle;
97   unix_shared_memory_queue_t *vpp_event_queue;
98
99   /* Socket configuration state */
100   /* TBD: covert 'is_*' vars to bit in u8 flags; */
101   u8 is_server;
102   u8 is_listen;
103   u8 is_cut_thru;
104   u8 is_nonblocking;
105   u8 is_vep;
106   u8 is_vep_session;
107   u32 wait_cont_idx;
108   vppcom_epoll_t vep;
109   u32 vrf;
110   vppcom_ip46_t lcl_addr;
111   vppcom_ip46_t peer_addr;
112   u16 port;
113   u8 proto;
114   u64 client_queue_address;
115   u64 options[16];
116 } session_t;
117
118 typedef struct vppcom_cfg_t_
119 {
120   u64 heapsize;
121   u64 segment_baseva;
122   u32 segment_size;
123   u32 add_segment_size;
124   u32 preallocated_fifo_pairs;
125   u32 rx_fifo_size;
126   u32 tx_fifo_size;
127   u32 event_queue_size;
128   u32 listen_queue_size;
129   f64 app_timeout;
130   f64 session_timeout;
131   f64 accept_timeout;
132 } vppcom_cfg_t;
133
134 typedef struct vppcom_main_t_
135 {
136   u8 init;
137   u32 *client_session_index_fifo;
138   volatile u32 bind_session_index;
139   int main_cpu;
140
141   /* vpe input queue */
142   unix_shared_memory_queue_t *vl_input_queue;
143
144   /* API client handle */
145   u32 my_client_index;
146
147   /* Session pool */
148   clib_spinlock_t sessions_lockp;
149   session_t *sessions;
150
151   /* Hash table for disconnect processing */
152   uword *session_index_by_vpp_handles;
153
154   /* Select bitmaps */
155   clib_bitmap_t *rd_bitmap;
156   clib_bitmap_t *wr_bitmap;
157   clib_bitmap_t *ex_bitmap;
158
159   /* Our event queue */
160   unix_shared_memory_queue_t *app_event_queue;
161
162   /* unique segment name counter */
163   u32 unique_segment_index;
164
165   pid_t my_pid;
166
167   /* For deadman timers */
168   clib_time_t clib_time;
169
170   /* State of the connection, shared between msg RX thread and main thread */
171   volatile app_state_t app_state;
172
173   vppcom_cfg_t cfg;
174
175   /* VNET_API_ERROR_FOO -> "Foo" hash table */
176   uword *error_string_by_error_number;
177 } vppcom_main_t;
178
179 vppcom_main_t vppcom_main = {.my_client_index = ~0 };
180
181 static const char *
182 vppcom_app_state_str (app_state_t state)
183 {
184   char *st;
185
186   switch (state)
187     {
188     case STATE_APP_START:
189       st = "STATE_APP_START";
190       break;
191
192     case STATE_APP_CONN_VPP:
193       st = "STATE_APP_CONN_VPP";
194       break;
195
196     case STATE_APP_ENABLED:
197       st = "STATE_APP_ENABLED";
198       break;
199
200     case STATE_APP_ATTACHED:
201       st = "STATE_APP_ATTACHED";
202       break;
203
204     default:
205       st = "UNKNOWN_APP_STATE";
206       break;
207     }
208
209   return st;
210 }
211
212 static const char *
213 vppcom_session_state_str (session_state_t state)
214 {
215   char *st;
216
217   switch (state)
218     {
219     case STATE_START:
220       st = "STATE_START";
221       break;
222
223     case STATE_CONNECT:
224       st = "STATE_CONNECT";
225       break;
226
227     case STATE_LISTEN:
228       st = "STATE_LISTEN";
229       break;
230
231     case STATE_ACCEPT:
232       st = "STATE_ACCEPT";
233       break;
234
235     case STATE_DISCONNECT:
236       st = "STATE_DISCONNECT";
237       break;
238
239     case STATE_FAILED:
240       st = "STATE_FAILED";
241       break;
242
243     default:
244       st = "UNKNOWN_STATE";
245       break;
246     }
247
248   return st;
249 }
250
251 /*
252  * VPPCOM Utility Functions
253  */
254 static inline int
255 vppcom_session_at_index (u32 session_index, session_t * volatile *sess)
256 {
257   vppcom_main_t *vcm = &vppcom_main;
258
259   /* Assumes that caller has acquired spinlock: vcm->sessions_lockp */
260   if (PREDICT_FALSE ((session_index == ~0) ||
261                      pool_is_free_index (vcm->sessions, session_index)))
262     {
263       clib_warning ("[%d] invalid session, sid (%u) has been closed!",
264                     vcm->my_pid, session_index);
265       return VPPCOM_EBADFD;
266     }
267   *sess = pool_elt_at_index (vcm->sessions, session_index);
268   return VPPCOM_OK;
269 }
270
271 static int
272 vppcom_connect_to_vpp (char *app_name)
273 {
274   api_main_t *am = &api_main;
275   vppcom_main_t *vcm = &vppcom_main;
276
277   if (VPPCOM_DEBUG > 0)
278     printf ("\nConnecting to VPP api...");
279   if (vl_client_connect_to_vlib ("/vpe-api", app_name, 32) < 0)
280     {
281       clib_warning ("[%d] connect to vpp (%s) failed!",
282                     vcm->my_pid, app_name);
283       return VPPCOM_ECONNREFUSED;
284     }
285
286   vcm->vl_input_queue = am->shmem_hdr->vl_input_queue;
287   vcm->my_client_index = am->my_client_index;
288   if (VPPCOM_DEBUG > 0)
289     printf (" connected!\n");
290
291   vcm->app_state = STATE_APP_CONN_VPP;
292   return VPPCOM_OK;
293 }
294
295 static u8 *
296 format_api_error (u8 * s, va_list * args)
297 {
298   vppcom_main_t *vcm = &vppcom_main;
299   i32 error = va_arg (*args, u32);
300   uword *p;
301
302   p = hash_get (vcm->error_string_by_error_number, -error);
303
304   if (p)
305     s = format (s, "%s (%d)", p[0], error);
306   else
307     s = format (s, "%d", error);
308   return s;
309 }
310
311 static void
312 vppcom_init_error_string_table (void)
313 {
314   vppcom_main_t *vcm = &vppcom_main;
315
316   vcm->error_string_by_error_number = hash_create (0, sizeof (uword));
317
318 #define _(n,v,s) hash_set (vcm->error_string_by_error_number, -v, s);
319   foreach_vnet_api_error;
320 #undef _
321
322   hash_set (vcm->error_string_by_error_number, 99, "Misc");
323 }
324
325 static inline int
326 vppcom_wait_for_app_state_change (app_state_t app_state)
327 {
328   vppcom_main_t *vcm = &vppcom_main;
329   f64 timeout = clib_time_now (&vcm->clib_time) + vcm->cfg.app_timeout;
330
331   while (clib_time_now (&vcm->clib_time) < timeout)
332     {
333       if (vcm->app_state == app_state)
334         return VPPCOM_OK;
335     }
336   if (VPPCOM_DEBUG > 0)
337     clib_warning ("[%d] timeout waiting for state %s (%d)", vcm->my_pid,
338                   vppcom_app_state_str (app_state), app_state);
339   return VPPCOM_ETIMEDOUT;
340 }
341
342 static inline int
343 vppcom_wait_for_session_state_change (u32 session_index,
344                                       session_state_t state,
345                                       f64 wait_for_time)
346 {
347   vppcom_main_t *vcm = &vppcom_main;
348   f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
349   session_t *volatile session;
350   int rv;
351
352   do
353     {
354       clib_spinlock_lock (&vcm->sessions_lockp);
355       rv = vppcom_session_at_index (session_index, &session);
356       if (PREDICT_FALSE (rv))
357         {
358           clib_spinlock_unlock (&vcm->sessions_lockp);
359           return rv;
360         }
361       if (session->state == state)
362         {
363           clib_spinlock_unlock (&vcm->sessions_lockp);
364           return VPPCOM_OK;
365         }
366       clib_spinlock_unlock (&vcm->sessions_lockp);
367     }
368   while (clib_time_now (&vcm->clib_time) < timeout);
369
370   if (VPPCOM_DEBUG > 0)
371     clib_warning ("[%d] timeout waiting for state %s (%d)", vcm->my_pid,
372                   vppcom_session_state_str (state), state);
373   return VPPCOM_ETIMEDOUT;
374 }
375
376 static inline int
377 vppcom_wait_for_client_session_index (f64 wait_for_time)
378 {
379   vppcom_main_t *vcm = &vppcom_main;
380   f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
381
382   do
383     {
384       if (clib_fifo_elts (vcm->client_session_index_fifo))
385         return VPPCOM_OK;
386     }
387   while (clib_time_now (&vcm->clib_time) < timeout);
388
389   if (wait_for_time == 0)
390     return VPPCOM_EAGAIN;
391
392   if (VPPCOM_DEBUG > 0)
393     clib_warning ("[%d] timeout waiting for client_session_index",
394                   vcm->my_pid);
395   return VPPCOM_ETIMEDOUT;
396 }
397
398 /*
399  * VPP-API message functions
400  */
401 static void
402 vppcom_send_session_enable_disable (u8 is_enable)
403 {
404   vppcom_main_t *vcm = &vppcom_main;
405   vl_api_session_enable_disable_t *bmp;
406   bmp = vl_msg_api_alloc (sizeof (*bmp));
407   memset (bmp, 0, sizeof (*bmp));
408
409   bmp->_vl_msg_id = ntohs (VL_API_SESSION_ENABLE_DISABLE);
410   bmp->client_index = vcm->my_client_index;
411   bmp->context = htonl (0xfeedface);
412   bmp->is_enable = is_enable;
413   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
414 }
415
416 static int
417 vppcom_app_session_enable (void)
418 {
419   vppcom_main_t *vcm = &vppcom_main;
420   int rv;
421
422   if (vcm->app_state != STATE_APP_ENABLED)
423     {
424       vppcom_send_session_enable_disable (1 /* is_enabled == TRUE */ );
425       rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
426       if (PREDICT_FALSE (rv))
427         {
428           if (VPPCOM_DEBUG > 0)
429             clib_warning ("[%d] Session enable timed out, rv = %s (%d)",
430                           vcm->my_pid, vppcom_retval_str (rv), rv);
431           return rv;
432         }
433     }
434   return VPPCOM_OK;
435 }
436
437 static void
438   vl_api_session_enable_disable_reply_t_handler
439   (vl_api_session_enable_disable_reply_t * mp)
440 {
441   vppcom_main_t *vcm = &vppcom_main;
442
443   if (mp->retval)
444     {
445       clib_warning ("[%d] session_enable_disable failed: %U", vcm->my_pid,
446                     format_api_error, ntohl (mp->retval));
447     }
448   else
449     vcm->app_state = STATE_APP_ENABLED;
450 }
451
452 static void
453 vppcom_app_send_attach (void)
454 {
455   vppcom_main_t *vcm = &vppcom_main;
456   vl_api_application_attach_t *bmp;
457   bmp = vl_msg_api_alloc (sizeof (*bmp));
458   memset (bmp, 0, sizeof (*bmp));
459
460   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_ATTACH);
461   bmp->client_index = vcm->my_client_index;
462   bmp->context = htonl (0xfeedface);
463   bmp->options[APP_OPTIONS_FLAGS] =
464     APP_OPTIONS_FLAGS_ACCEPT_REDIRECT | APP_OPTIONS_FLAGS_ADD_SEGMENT |
465     APP_OPTIONS_FLAGS_USE_LOCAL_SCOPE | APP_OPTIONS_FLAGS_USE_GLOBAL_SCOPE;
466   bmp->options[SESSION_OPTIONS_SEGMENT_SIZE] = vcm->cfg.segment_size;
467   bmp->options[SESSION_OPTIONS_ADD_SEGMENT_SIZE] = vcm->cfg.add_segment_size;
468   bmp->options[SESSION_OPTIONS_RX_FIFO_SIZE] = vcm->cfg.rx_fifo_size;
469   bmp->options[SESSION_OPTIONS_TX_FIFO_SIZE] = vcm->cfg.tx_fifo_size;
470   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
471 }
472
473 static int
474 vppcom_app_attach (void)
475 {
476   vppcom_main_t *vcm = &vppcom_main;
477   int rv;
478
479   vppcom_app_send_attach ();
480   rv = vppcom_wait_for_app_state_change (STATE_APP_ATTACHED);
481   if (PREDICT_FALSE (rv))
482     {
483       if (VPPCOM_DEBUG > 0)
484         clib_warning ("[%d] application attach timed out, rv = %s (%d)",
485                       vcm->my_pid, vppcom_retval_str (rv), rv);
486       return rv;
487     }
488   return VPPCOM_OK;
489 }
490
491 static void
492 vppcom_app_detach (void)
493 {
494   vppcom_main_t *vcm = &vppcom_main;
495   vl_api_application_detach_t *bmp;
496   bmp = vl_msg_api_alloc (sizeof (*bmp));
497   memset (bmp, 0, sizeof (*bmp));
498
499   bmp->_vl_msg_id = ntohs (VL_API_APPLICATION_DETACH);
500   bmp->client_index = vcm->my_client_index;
501   bmp->context = htonl (0xfeedface);
502   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & bmp);
503 }
504
505 static void
506 vl_api_application_attach_reply_t_handler (vl_api_application_attach_reply_t *
507                                            mp)
508 {
509   vppcom_main_t *vcm = &vppcom_main;
510   static svm_fifo_segment_create_args_t _a;
511   svm_fifo_segment_create_args_t *a = &_a;
512   int rv;
513
514   memset (a, 0, sizeof (*a));
515   if (mp->retval)
516     {
517       clib_warning ("[%d] attach failed: %U", vcm->my_pid,
518                     format_api_error, ntohl (mp->retval));
519       return;
520     }
521
522   if (mp->segment_name_length == 0)
523     {
524       clib_warning ("[%d] segment_name_length zero", vcm->my_pid);
525       return;
526     }
527
528   a->segment_name = (char *) mp->segment_name;
529   a->segment_size = mp->segment_size;
530
531   ASSERT (mp->app_event_queue_address);
532
533   /* Attach to the segment vpp created */
534   rv = svm_fifo_segment_attach (a);
535   vec_reset_length (a->new_segment_indices);
536   if (PREDICT_FALSE (rv))
537     {
538       clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed", vcm->my_pid,
539                     mp->segment_name);
540       return;
541     }
542
543   vcm->app_event_queue =
544     uword_to_pointer (mp->app_event_queue_address,
545                       unix_shared_memory_queue_t *);
546
547   vcm->app_state = STATE_APP_ATTACHED;
548 }
549
550 static void
551 vl_api_application_detach_reply_t_handler (vl_api_application_detach_reply_t *
552                                            mp)
553 {
554   vppcom_main_t *vcm = &vppcom_main;
555
556   if (mp->retval)
557     clib_warning ("[%d] detach failed: %U", vcm->my_pid, format_api_error,
558                   ntohl (mp->retval));
559
560   vcm->app_state = STATE_APP_ENABLED;
561 }
562
563 static void
564 vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t *
565                                            mp)
566 {
567   vppcom_main_t *vcm = &vppcom_main;
568   uword *p;
569
570   p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
571   if (p)
572     {
573       session_t *session = 0;
574       int rv;
575       clib_spinlock_lock (&vcm->sessions_lockp);
576       rv = vppcom_session_at_index (p[0], &session);
577       if (PREDICT_FALSE (rv))
578         {
579           if (VPPCOM_DEBUG > 1)
580             clib_warning ("[%d] invalid session, sid (%u) has been closed!",
581                           vcm->my_pid, p[0]);
582         }
583       hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
584       session->state = STATE_DISCONNECT;
585       clib_spinlock_unlock (&vcm->sessions_lockp);
586     }
587   else
588     {
589       if (VPPCOM_DEBUG > 1)
590         clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
591                       mp->handle);
592     }
593
594   if (mp->retval)
595     clib_warning ("[%d] disconnect_session failed: %U", vcm->my_pid,
596                   format_api_error, ntohl (mp->retval));
597 }
598
599 static void
600 vl_api_map_another_segment_t_handler (vl_api_map_another_segment_t * mp)
601 {
602   vppcom_main_t *vcm = &vppcom_main;
603   static svm_fifo_segment_create_args_t _a;
604   svm_fifo_segment_create_args_t *a = &_a;
605   int rv;
606
607   memset (a, 0, sizeof (*a));
608   a->segment_name = (char *) mp->segment_name;
609   a->segment_size = mp->segment_size;
610   /* Attach to the segment vpp created */
611   rv = svm_fifo_segment_attach (a);
612   vec_reset_length (a->new_segment_indices);
613   if (PREDICT_FALSE (rv))
614     {
615       clib_warning ("[%d] svm_fifo_segment_attach ('%s') failed",
616                     vcm->my_pid, mp->segment_name);
617       return;
618     }
619   if (VPPCOM_DEBUG > 1)
620     clib_warning ("[%d] mapped new segment '%s' size %d", vcm->my_pid,
621                   mp->segment_name, mp->segment_size);
622 }
623
624 static void
625 vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp)
626 {
627   vppcom_main_t *vcm = &vppcom_main;
628   session_t *session = 0;
629   vl_api_disconnect_session_reply_t *rmp;
630   uword *p;
631   int rv = 0;
632
633   p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
634   if (p)
635     {
636       int rval;
637       clib_spinlock_lock (&vcm->sessions_lockp);
638       rval = vppcom_session_at_index (p[0], &session);
639       if (PREDICT_FALSE (rval))
640         {
641           if (VPPCOM_DEBUG > 1)
642             clib_warning ("[%d] invalid session, sid (%u) has been closed!",
643                           vcm->my_pid, p[0]);
644         }
645       else
646         pool_put (vcm->sessions, session);
647       clib_spinlock_unlock (&vcm->sessions_lockp);
648       hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
649     }
650   else
651     {
652       clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
653                     mp->handle);
654       rv = -11;
655     }
656
657   rmp = vl_msg_api_alloc (sizeof (*rmp));
658   memset (rmp, 0, sizeof (*rmp));
659
660   rmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION_REPLY);
661   rmp->retval = htonl (rv);
662   rmp->handle = mp->handle;
663   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
664 }
665
666 static void
667 vl_api_reset_session_t_handler (vl_api_reset_session_t * mp)
668 {
669   vppcom_main_t *vcm = &vppcom_main;
670   session_t *session = 0;
671   vl_api_reset_session_reply_t *rmp;
672   uword *p;
673   int rv = 0;
674
675   p = hash_get (vcm->session_index_by_vpp_handles, mp->handle);
676   if (p)
677     {
678       int rval;
679       clib_spinlock_lock (&vcm->sessions_lockp);
680       rval = vppcom_session_at_index (p[0], &session);
681       if (PREDICT_FALSE (rval))
682         {
683           if (VPPCOM_DEBUG > 1)
684             clib_warning ("[%d] invalid session, sid (%u) has been closed!",
685                           vcm->my_pid, p[0]);
686         }
687       else
688         pool_put (vcm->sessions, session);
689       clib_spinlock_unlock (&vcm->sessions_lockp);
690       hash_unset (vcm->session_index_by_vpp_handles, mp->handle);
691     }
692   else
693     {
694       clib_warning ("[%d] couldn't find session key %llx", vcm->my_pid,
695                     mp->handle);
696       rv = -11;
697     }
698
699   rmp = vl_msg_api_alloc (sizeof (*rmp));
700   memset (rmp, 0, sizeof (*rmp));
701   rmp->_vl_msg_id = ntohs (VL_API_RESET_SESSION_REPLY);
702   rmp->retval = htonl (rv);
703   rmp->handle = mp->handle;
704   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & rmp);
705 }
706
707 static void
708 vl_api_connect_session_reply_t_handler (vl_api_connect_session_reply_t * mp)
709 {
710   vppcom_main_t *vcm = &vppcom_main;
711   session_t *session;
712   u32 session_index;
713   svm_fifo_t *rx_fifo, *tx_fifo;
714   u8 is_cut_thru = 0;
715   int rv;
716
717   if (mp->retval)
718     {
719       clib_warning ("[%d] connect failed: %U", vcm->my_pid, format_api_error,
720                     ntohl (mp->retval));
721       return;
722     }
723
724   session_index = mp->context;
725   if (VPPCOM_DEBUG > 1)
726     clib_warning ("[%d] session_index = %d 0x%08x", vcm->my_pid,
727                   session_index, session_index);
728
729   clib_spinlock_lock (&vcm->sessions_lockp);
730   if (pool_is_free_index (vcm->sessions, session_index))
731     {
732       clib_spinlock_unlock (&vcm->sessions_lockp);
733       if (VPPCOM_DEBUG > 1)
734         clib_warning ("[%d] invalid session, sid %d is closed!",
735                       vcm->my_pid, session_index);
736       return;
737     }
738
739   /* We've been redirected */
740   if (mp->segment_name_length > 0)
741     {
742       static svm_fifo_segment_create_args_t _a;
743       svm_fifo_segment_create_args_t *a = &_a;
744
745       is_cut_thru = 1;
746       memset (a, 0, sizeof (*a));
747       a->segment_name = (char *) mp->segment_name;
748       if (VPPCOM_DEBUG > 1)
749         clib_warning ("[%d] cut-thru segment: %s", vcm->my_pid,
750                       a->segment_name);
751       rv = svm_fifo_segment_attach (a);
752       vec_reset_length (a->new_segment_indices);
753       if (PREDICT_FALSE (rv))
754         {
755           clib_spinlock_unlock (&vcm->sessions_lockp);
756           clib_warning ("[%d] sm_fifo_segment_attach ('%s') failed",
757                         vcm->my_pid, a->segment_name);
758           return;
759         }
760     }
761
762   /*
763    * Setup session
764    */
765   if (VPPCOM_DEBUG > 1)
766     clib_warning ("[%d] client sid %d", vcm->my_pid, session_index);
767
768   session = pool_elt_at_index (vcm->sessions, session_index);
769   session->is_cut_thru = is_cut_thru;
770   session->vpp_event_queue = uword_to_pointer (mp->vpp_event_queue_address,
771                                                unix_shared_memory_queue_t *);
772
773   rx_fifo = uword_to_pointer (mp->server_rx_fifo, svm_fifo_t *);
774   rx_fifo->client_session_index = session_index;
775   tx_fifo = uword_to_pointer (mp->server_tx_fifo, svm_fifo_t *);
776   tx_fifo->client_session_index = session_index;
777
778   session->server_rx_fifo = rx_fifo;
779   session->server_tx_fifo = tx_fifo;
780   session->vpp_session_handle = mp->handle;
781   session->state = STATE_CONNECT;
782
783   /* Add it to lookup table */
784   hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
785   clib_spinlock_unlock (&vcm->sessions_lockp);
786 }
787
788 static void
789 vppcom_send_connect_sock (session_t * session, u32 session_index)
790 {
791   vppcom_main_t *vcm = &vppcom_main;
792   vl_api_connect_sock_t *cmp;
793
794   /* Assumes caller as acquired the spinlock: vcm->sessions_lockp */
795   session->is_server = 0;
796   cmp = vl_msg_api_alloc (sizeof (*cmp));
797   memset (cmp, 0, sizeof (*cmp));
798   cmp->_vl_msg_id = ntohs (VL_API_CONNECT_SOCK);
799   cmp->client_index = vcm->my_client_index;
800   cmp->context = session_index;
801
802   if (VPPCOM_DEBUG > 1)
803     clib_warning ("[%d] session_index = %d 0x%08x",
804                   vcm->my_pid, session_index, session_index);
805
806   cmp->vrf = session->vrf;
807   cmp->is_ip4 = session->peer_addr.is_ip4;
808   clib_memcpy (cmp->ip, &session->peer_addr.ip46, sizeof (cmp->ip));
809   cmp->port = session->port;
810   cmp->proto = session->proto;
811   clib_memcpy (cmp->options, session->options, sizeof (cmp->options));
812   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & cmp);
813 }
814
815 static int
816 vppcom_send_disconnect (u32 session_index)
817 {
818   vppcom_main_t *vcm = &vppcom_main;
819   vl_api_disconnect_session_t *dmp;
820   session_t *session = 0;
821   int rv;
822
823   clib_spinlock_lock (&vcm->sessions_lockp);
824   rv = vppcom_session_at_index (session_index, &session);
825   if (PREDICT_FALSE (rv))
826     {
827       clib_spinlock_unlock (&vcm->sessions_lockp);
828       if (VPPCOM_DEBUG > 1)
829         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
830                       vcm->my_pid, session_index);
831       return rv;
832     }
833
834   dmp = vl_msg_api_alloc (sizeof (*dmp));
835   memset (dmp, 0, sizeof (*dmp));
836   dmp->_vl_msg_id = ntohs (VL_API_DISCONNECT_SESSION);
837   dmp->client_index = vcm->my_client_index;
838   dmp->handle = session->vpp_session_handle;
839   clib_spinlock_unlock (&vcm->sessions_lockp);
840   vl_msg_api_send_shmem (vcm->vl_input_queue, (u8 *) & dmp);
841   return VPPCOM_OK;
842 }
843
844 static void
845 vl_api_bind_sock_reply_t_handler (vl_api_bind_sock_reply_t * mp)
846 {
847   vppcom_main_t *vcm = &vppcom_main;
848   session_t *session = 0;
849   int rv;
850
851   if (mp->retval)
852     clib_warning ("[%d] bind failed: %U", vcm->my_pid, format_api_error,
853                   ntohl (mp->retval));
854
855   ASSERT (vcm->bind_session_index != ~0);
856
857   clib_spinlock_lock (&vcm->sessions_lockp);
858   rv = vppcom_session_at_index (vcm->bind_session_index, &session);
859   if (rv == VPPCOM_OK)
860     {
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;
866     }
867   clib_spinlock_unlock (&vcm->sessions_lockp);
868 }
869
870 static void
871 vl_api_unbind_sock_reply_t_handler (vl_api_unbind_sock_reply_t * mp)
872 {
873   vppcom_main_t *vcm = &vppcom_main;
874   session_t *session = 0;
875   int rv;
876
877   clib_spinlock_lock (&vcm->sessions_lockp);
878   rv = vppcom_session_at_index (vcm->bind_session_index, &session);
879   if (rv == VPPCOM_OK)
880     {
881       if ((VPPCOM_DEBUG > 1) && (mp->retval))
882         clib_warning ("[%d] unbind failed: %U", vcm->my_pid, format_api_error,
883                       ntohl (mp->retval));
884
885       vcm->bind_session_index = ~0;
886       session->state = STATE_START;
887     }
888   clib_spinlock_unlock (&vcm->sessions_lockp);
889 }
890
891 u8 *
892 format_ip4_address (u8 * s, va_list * args)
893 {
894   u8 *a = va_arg (*args, u8 *);
895   return format (s, "%d.%d.%d.%d", a[0], a[1], a[2], a[3]);
896 }
897
898 u8 *
899 format_ip6_address (u8 * s, va_list * args)
900 {
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;
903
904   i_max_n_zero = ARRAY_LEN (a->as_u16);
905   max_n_zeros = 0;
906   i_first_zero = i_max_n_zero;
907   n_zeros = 0;
908   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
909     {
910       u32 is_zero = a->as_u16[i] == 0;
911       if (is_zero && i_first_zero >= ARRAY_LEN (a->as_u16))
912         {
913           i_first_zero = i;
914           n_zeros = 0;
915         }
916       n_zeros += is_zero;
917       if ((!is_zero && n_zeros > max_n_zeros)
918           || (i + 1 >= ARRAY_LEN (a->as_u16) && n_zeros > max_n_zeros))
919         {
920           i_max_n_zero = i_first_zero;
921           max_n_zeros = n_zeros;
922           i_first_zero = ARRAY_LEN (a->as_u16);
923           n_zeros = 0;
924         }
925     }
926
927   last_double_colon = 0;
928   for (i = 0; i < ARRAY_LEN (a->as_u16); i++)
929     {
930       if (i == i_max_n_zero && max_n_zeros > 1)
931         {
932           s = format (s, "::");
933           i += max_n_zeros - 1;
934           last_double_colon = 1;
935         }
936       else
937         {
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;
942         }
943     }
944
945   return s;
946 }
947
948 /* Format an IP46 address. */
949 u8 *
950 format_ip46_address (u8 * s, va_list * args)
951 {
952   ip46_address_t *ip46 = va_arg (*args, ip46_address_t *);
953   ip46_type_t type = va_arg (*args, ip46_type_t);
954   int is_ip4 = 1;
955
956   switch (type)
957     {
958     case IP46_TYPE_ANY:
959       is_ip4 = ip46_address_is_ip4 (ip46);
960       break;
961     case IP46_TYPE_IP4:
962       is_ip4 = 1;
963       break;
964     case IP46_TYPE_IP6:
965       is_ip4 = 0;
966       break;
967     }
968
969   return is_ip4 ?
970     format (s, "%U", format_ip4_address, &ip46->ip4) :
971     format (s, "%U", format_ip6_address, &ip46->ip6);
972 }
973
974 static void
975 vl_api_accept_session_t_handler (vl_api_accept_session_t * mp)
976 {
977   vppcom_main_t *vcm = &vppcom_main;
978   vl_api_accept_session_reply_t *rmp;
979   svm_fifo_t *rx_fifo, *tx_fifo;
980   session_t *session;
981   u32 session_index;
982   int rv = 0;
983
984   if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
985     {
986       clib_warning ("[%d] client session queue is full!", vcm->my_pid);
987       rv = VNET_API_ERROR_QUEUE_FULL;
988       goto send_reply;
989     }
990
991   if (VPPCOM_DEBUG > 1)
992     {
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));
996       vec_free (ip_str);
997     }
998
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;
1004
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;
1009
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->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));
1021
1022   /* Add it to lookup table */
1023   hash_set (vcm->session_index_by_vpp_handles, mp->handle, session_index);
1024
1025   clib_fifo_add1 (vcm->client_session_index_fifo, session_index);
1026   clib_spinlock_unlock (&vcm->sessions_lockp);
1027
1028   /*
1029    * Send accept reply to vpp
1030    */
1031 send_reply:
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);
1038 }
1039
1040 /*
1041  * Acting as server for redirected connect requests
1042  */
1043 static void
1044 vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp)
1045 {
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;
1049   u32 session_index;
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;
1054   int rv = 0;
1055   svm_fifo_t *rx_fifo;
1056   svm_fifo_t *tx_fifo;
1057   unix_shared_memory_queue_t *event_q = 0;
1058
1059   clib_spinlock_lock (&vcm->sessions_lockp);
1060   if (!clib_fifo_free_elts (vcm->client_session_index_fifo))
1061     {
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);
1066       goto send_reply;
1067     }
1068
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++,
1073                                      0);
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;
1078
1079   rv = svm_fifo_segment_create (a);
1080   if (PREDICT_FALSE (rv))
1081     {
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;
1087       goto send_reply;
1088     }
1089
1090   if (VPPCOM_DEBUG > 1)
1091     clib_warning ("[%d] created segment '%s'", vcm->my_pid, a->segment_name);
1092
1093   pool_get (vcm->sessions, session);
1094   memset (session, 0, sizeof (*session));
1095   session_index = session - vcm->sessions;
1096
1097   session->sm_seg_index = a->new_segment_indices[0];
1098   vec_reset_length (a->new_segment_indices);
1099
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))
1105     {
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);
1112       goto send_reply;
1113     }
1114
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))
1119     {
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);
1127       goto send_reply;
1128     }
1129
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->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));
1139   {
1140     void *oldheap;
1141     ssvm_shared_header_t *sh = seg->ssvm.sh;
1142
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);
1151   }
1152
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);
1159
1160 send_reply:
1161   rmp = vl_msg_api_alloc (sizeof (*rmp));
1162   memset (rmp, 0, sizeof (*rmp));
1163
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);
1170
1171   if (event_q)
1172     {
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);
1176     }
1177   client_q =
1178     uword_to_pointer (mp->client_queue_address, unix_shared_memory_queue_t *);
1179
1180   ASSERT (client_q);
1181   vl_msg_api_send_shmem (client_q, (u8 *) & rmp);
1182 }
1183
1184 static void
1185 vppcom_send_bind_sock (session_t * session)
1186 {
1187   vppcom_main_t *vcm = &vppcom_main;
1188   vl_api_bind_sock_t *bmp;
1189
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));
1194
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->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);
1205 }
1206
1207 static void
1208 vppcom_send_unbind_sock (u32 session_index)
1209 {
1210   vppcom_main_t *vcm = &vppcom_main;
1211   vl_api_unbind_sock_t *ump;
1212   session_t *session = 0;
1213   int rv;
1214
1215   clib_spinlock_lock (&vcm->sessions_lockp);
1216   rv = vppcom_session_at_index (session_index, &session);
1217   if (PREDICT_FALSE (rv))
1218     {
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);
1223       return;
1224     }
1225
1226   ump = vl_msg_api_alloc (sizeof (*ump));
1227   memset (ump, 0, sizeof (*ump));
1228
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);
1234 }
1235
1236 static int
1237 vppcom_session_unbind_cut_thru (session_t * session)
1238 {
1239   svm_fifo_segment_main_t *sm = &svm_fifo_segment_main;
1240   svm_fifo_segment_private_t *seg;
1241   int rv = VPPCOM_OK;
1242
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);
1249
1250   return rv;
1251 }
1252
1253 static int
1254 vppcom_session_unbind (u32 session_index)
1255 {
1256   vppcom_main_t *vcm = &vppcom_main;
1257   int rv;
1258
1259   clib_spinlock_lock (&vcm->sessions_lockp);
1260   if (PREDICT_FALSE (pool_is_free_index (vcm->sessions, session_index)))
1261     {
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;
1267     }
1268   clib_spinlock_unlock (&vcm->sessions_lockp);
1269
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))
1275     {
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);
1280       return rv;
1281     }
1282   return VPPCOM_OK;
1283 }
1284
1285 static int
1286 vppcom_session_disconnect (u32 session_index)
1287 {
1288   vppcom_main_t *vcm = &vppcom_main;
1289   int rv;
1290
1291   rv = vppcom_send_disconnect (session_index);
1292   if (PREDICT_FALSE (rv))
1293     return rv;
1294
1295   rv = vppcom_wait_for_session_state_change (session_index, STATE_DISCONNECT,
1296                                              vcm->cfg.session_timeout);
1297   if (PREDICT_FALSE (rv))
1298     {
1299       if (VPPCOM_DEBUG > 0)
1300         clib_warning ("[%d] client disconnect timed out, rv = %s (%d)",
1301                       vcm->my_pid, vppcom_retval_str (rv), rv);
1302       return rv;
1303     }
1304   return VPPCOM_OK;
1305 }
1306
1307 #define foreach_sock_msg                                        \
1308 _(SESSION_ENABLE_DISABLE_REPLY, session_enable_disable_reply)   \
1309 _(BIND_SOCK_REPLY, bind_sock_reply)                             \
1310 _(UNBIND_SOCK_REPLY, unbind_sock_reply)                         \
1311 _(ACCEPT_SESSION, accept_session)                               \
1312 _(CONNECT_SOCK, connect_sock)                                   \
1313 _(CONNECT_SESSION_REPLY, connect_session_reply)                 \
1314 _(DISCONNECT_SESSION, disconnect_session)                       \
1315 _(DISCONNECT_SESSION_REPLY, disconnect_session_reply)           \
1316 _(RESET_SESSION, reset_session)                                 \
1317 _(APPLICATION_ATTACH_REPLY, application_attach_reply)           \
1318 _(APPLICATION_DETACH_REPLY, application_detach_reply)           \
1319 _(MAP_ANOTHER_SEGMENT, map_another_segment)
1320
1321 static void
1322 vppcom_api_hookup (void)
1323 {
1324 #define _(N,n)                                                  \
1325     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
1326                            vl_api_##n##_t_handler,              \
1327                            vl_noop_handler,                     \
1328                            vl_api_##n##_t_endian,               \
1329                            vl_api_##n##_t_print,                \
1330                            sizeof(vl_api_##n##_t), 1);
1331   foreach_sock_msg;
1332 #undef _
1333 }
1334
1335 static void
1336 vppcom_cfg_init (vppcom_cfg_t * vcl_cfg)
1337 {
1338   ASSERT (vcl_cfg);
1339
1340   vcl_cfg->heapsize = (256ULL << 20);
1341   vcl_cfg->segment_baseva = 0x200000000ULL;
1342   vcl_cfg->segment_size = (256 << 20);
1343   vcl_cfg->add_segment_size = (128 << 20);
1344   vcl_cfg->preallocated_fifo_pairs = 8;
1345   vcl_cfg->rx_fifo_size = (1 << 20);
1346   vcl_cfg->tx_fifo_size = (1 << 20);
1347   vcl_cfg->event_queue_size = 2048;
1348   vcl_cfg->listen_queue_size = CLIB_CACHE_LINE_BYTES / sizeof (u32);
1349   vcl_cfg->app_timeout = 10 * 60.0;
1350   vcl_cfg->session_timeout = 10 * 60.0;
1351   vcl_cfg->accept_timeout = 60.0;
1352 }
1353
1354 static void
1355 vppcom_cfg_heapsize (char *conf_fname)
1356 {
1357   vppcom_main_t *vcm = &vppcom_main;
1358   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1359   FILE *fp;
1360   char inbuf[4096];
1361   int argc = 1;
1362   char **argv = NULL;
1363   char *arg = NULL;
1364   char *p;
1365   int i;
1366   u8 *sizep;
1367   u32 size;
1368
1369   fp = fopen (conf_fname, "r");
1370   if (fp == NULL)
1371     {
1372       if (VPPCOM_DEBUG > 0)
1373         fprintf (stderr, "open configuration file '%s' failed\n", conf_fname);
1374       goto defaulted;
1375     }
1376   argv = calloc (1, sizeof (char *));
1377   if (argv == NULL)
1378     goto defaulted;
1379
1380   while (1)
1381     {
1382       if (fgets (inbuf, 4096, fp) == 0)
1383         break;
1384       p = strtok (inbuf, " \t\n");
1385       while (p != NULL)
1386         {
1387           if (*p == '#')
1388             break;
1389           argc++;
1390           char **tmp = realloc (argv, argc * sizeof (char *));
1391           if (tmp == NULL)
1392             goto defaulted;
1393           argv = tmp;
1394           arg = strndup (p, 1024);
1395           if (arg == NULL)
1396             goto defaulted;
1397           argv[argc - 1] = arg;
1398           p = strtok (NULL, " \t\n");
1399         }
1400     }
1401
1402   fclose (fp);
1403   fp = NULL;
1404
1405   char **tmp = realloc (argv, (argc + 1) * sizeof (char *));
1406   if (tmp == NULL)
1407     goto defaulted;
1408   argv = tmp;
1409   argv[argc] = NULL;
1410
1411   /*
1412    * Look for and parse the "heapsize" config parameter.
1413    * Manual since none of the clib infra has been bootstrapped yet.
1414    *
1415    * Format: heapsize <nn>[mM][gG]
1416    */
1417
1418   for (i = 1; i < (argc - 1); i++)
1419     {
1420       if (!strncmp (argv[i], "heapsize", 8))
1421         {
1422           sizep = (u8 *) argv[i + 1];
1423           size = 0;
1424           while (*sizep >= '0' && *sizep <= '9')
1425             {
1426               size *= 10;
1427               size += *sizep++ - '0';
1428             }
1429           if (size == 0)
1430             {
1431               if (VPPCOM_DEBUG > 0)
1432                 clib_warning ("[%d] parse error '%s %s', "
1433                               "using default heapsize %lld (0x%llx)",
1434                               vcm->my_pid, argv[i], argv[i + 1],
1435                               vcl_cfg->heapsize, vcl_cfg->heapsize);
1436               goto defaulted;
1437             }
1438
1439           if (*sizep == 'g' || *sizep == 'G')
1440             vcl_cfg->heapsize = size << 30;
1441           else if (*sizep == 'm' || *sizep == 'M')
1442             vcl_cfg->heapsize = size << 20;
1443           else
1444             {
1445               if (VPPCOM_DEBUG > 0)
1446                 clib_warning ("[%d] parse error '%s %s', "
1447                               "using default heapsize %lld (0x%llx)",
1448                               vcm->my_pid, argv[i], argv[i + 1],
1449                               vcl_cfg->heapsize, vcl_cfg->heapsize);
1450               goto defaulted;
1451             }
1452         }
1453     }
1454
1455 defaulted:
1456   if (fp != NULL)
1457     fclose (fp);
1458   if (argv != NULL)
1459     free (argv);
1460   if (!clib_mem_init (0, vcl_cfg->heapsize))
1461     clib_warning ("[%d] vppcom heap allocation failure!", vcm->my_pid);
1462   else if (VPPCOM_DEBUG > 0)
1463     clib_warning ("[%d] allocated vppcom heapsize %lld (0x%llx)",
1464                   vcm->my_pid, vcl_cfg->heapsize, vcl_cfg->heapsize);
1465 }
1466
1467 static void
1468 vppcom_cfg_read (char *conf_fname)
1469 {
1470   vppcom_main_t *vcm = &vppcom_main;
1471   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1472   int fd;
1473   unformat_input_t _input, *input = &_input;
1474   unformat_input_t _line_input, *line_input = &_line_input;
1475   u8 vc_cfg_input = 0;
1476   u8 *chroot_path;
1477   struct stat s;
1478   u32 uid, gid;
1479
1480   fd = open (conf_fname, O_RDONLY);
1481   if (fd < 0)
1482     {
1483       if (VPPCOM_DEBUG > 0)
1484         clib_warning ("[%d] open configuration file '%s' failed!",
1485                       vcm->my_pid, conf_fname);
1486       goto file_done;
1487     }
1488
1489   if (fstat (fd, &s) < 0)
1490     {
1491       if (VPPCOM_DEBUG > 0)
1492         clib_warning ("[%d] failed to stat `%s'", vcm->my_pid, conf_fname);
1493       goto file_done;
1494     }
1495
1496   if (!(S_ISREG (s.st_mode) || S_ISLNK (s.st_mode)))
1497     {
1498       if (VPPCOM_DEBUG > 0)
1499         clib_warning ("[%d] not a regular file `%s'", vcm->my_pid,
1500                       conf_fname);
1501       goto file_done;
1502     }
1503
1504   unformat_init_clib_file (input, fd);
1505
1506   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
1507     {
1508       (void) unformat_user (input, unformat_line_input, line_input);
1509       unformat_skip_white_space (line_input);
1510
1511       if (unformat (line_input, "vppcom {"))
1512         {
1513           vc_cfg_input = 1;
1514           continue;
1515         }
1516
1517       if (vc_cfg_input)
1518         {
1519           if (unformat (line_input, "heapsize %s", &chroot_path))
1520             {
1521               vec_terminate_c_string (chroot_path);
1522               if (VPPCOM_DEBUG > 0)
1523                 clib_warning ("[%d] configured heapsize %s, "
1524                               "actual heapsize %lld (0x%llx)",
1525                               vcm->my_pid, chroot_path, vcl_cfg->heapsize,
1526                               vcl_cfg->heapsize);
1527               vec_free (chroot_path);
1528             }
1529           else if (unformat (line_input, "api-prefix %s", &chroot_path))
1530             {
1531               vec_terminate_c_string (chroot_path);
1532               vl_set_memory_root_path ((char *) chroot_path);
1533               if (VPPCOM_DEBUG > 0)
1534                 clib_warning ("[%d] configured api-prefix %s",
1535                               vcm->my_pid, chroot_path);
1536               chroot_path = 0;  /* Don't vec_free() it! */
1537             }
1538           else if (unformat (line_input, "uid %d", &uid))
1539             {
1540               vl_set_memory_uid (uid);
1541               if (VPPCOM_DEBUG > 0)
1542                 clib_warning ("[%d] configured uid %d", vcm->my_pid, uid);
1543             }
1544           else if (unformat (line_input, "gid %d", &gid))
1545             {
1546               vl_set_memory_gid (gid);
1547               if (VPPCOM_DEBUG > 0)
1548                 clib_warning ("[%d] configured gid %d", vcm->my_pid, gid);
1549             }
1550           else if (unformat (line_input, "segment-baseva 0x%llx",
1551                              &vcl_cfg->segment_baseva))
1552             {
1553               if (VPPCOM_DEBUG > 0)
1554                 clib_warning ("[%d] configured segment_baseva 0x%llx",
1555                               vcm->my_pid, vcl_cfg->segment_baseva);
1556             }
1557           else if (unformat (line_input, "segment-size 0x%lx",
1558                              &vcl_cfg->segment_size))
1559             {
1560               if (VPPCOM_DEBUG > 0)
1561                 clib_warning ("[%d] configured segment_size 0x%lx (%ld)",
1562                               vcm->my_pid, vcl_cfg->segment_size,
1563                               vcl_cfg->segment_size);
1564             }
1565           else if (unformat (line_input, "segment-size %ld",
1566                              &vcl_cfg->segment_size))
1567             {
1568               if (VPPCOM_DEBUG > 0)
1569                 clib_warning ("[%d] configured segment_size %ld (0x%lx)",
1570                               vcm->my_pid, vcl_cfg->segment_size,
1571                               vcl_cfg->segment_size);
1572             }
1573           else if (unformat (line_input, "add-segment-size 0x%lx",
1574                              &vcl_cfg->add_segment_size))
1575             {
1576               if (VPPCOM_DEBUG > 0)
1577                 clib_warning
1578                   ("[%d] configured add_segment_size 0x%lx (%ld)",
1579                    vcm->my_pid, vcl_cfg->add_segment_size,
1580                    vcl_cfg->add_segment_size);
1581             }
1582           else if (unformat (line_input, "add-segment-size %ld",
1583                              &vcl_cfg->add_segment_size))
1584             {
1585               if (VPPCOM_DEBUG > 0)
1586                 clib_warning
1587                   ("[%d] configured add_segment_size %ld (0x%lx)",
1588                    vcm->my_pid, vcl_cfg->add_segment_size,
1589                    vcl_cfg->add_segment_size);
1590             }
1591           else if (unformat (line_input, "preallocated-fifo-pairs %d",
1592                              &vcl_cfg->preallocated_fifo_pairs))
1593             {
1594               if (VPPCOM_DEBUG > 0)
1595                 clib_warning ("[%d] configured preallocated_fifo_pairs "
1596                               "%d (0x%x)", vcm->my_pid,
1597                               vcl_cfg->preallocated_fifo_pairs,
1598                               vcl_cfg->preallocated_fifo_pairs);
1599             }
1600           else if (unformat (line_input, "rx-fifo-size 0x%lx",
1601                              &vcl_cfg->rx_fifo_size))
1602             {
1603               if (VPPCOM_DEBUG > 0)
1604                 clib_warning ("[%d] configured rx_fifo_size 0x%lx (%ld)",
1605                               vcm->my_pid, vcl_cfg->rx_fifo_size,
1606                               vcl_cfg->rx_fifo_size);
1607             }
1608           else if (unformat (line_input, "rx-fifo-size %ld",
1609                              &vcl_cfg->rx_fifo_size))
1610             {
1611               if (VPPCOM_DEBUG > 0)
1612                 clib_warning ("[%d] configured rx_fifo_size %ld (0x%lx)",
1613                               vcm->my_pid, vcl_cfg->rx_fifo_size,
1614                               vcl_cfg->rx_fifo_size);
1615             }
1616           else if (unformat (line_input, "tx-fifo-size 0x%lx",
1617                              &vcl_cfg->tx_fifo_size))
1618             {
1619               if (VPPCOM_DEBUG > 0)
1620                 clib_warning ("[%d] configured tx_fifo_size 0x%lx (%ld)",
1621                               vcm->my_pid, vcl_cfg->tx_fifo_size,
1622                               vcl_cfg->tx_fifo_size);
1623             }
1624           else if (unformat (line_input, "tx-fifo-size %ld",
1625                              &vcl_cfg->tx_fifo_size))
1626             {
1627               if (VPPCOM_DEBUG > 0)
1628                 clib_warning ("[%d] configured tx_fifo_size %ld (0x%lx)",
1629                               vcm->my_pid, vcl_cfg->tx_fifo_size,
1630                               vcl_cfg->tx_fifo_size);
1631             }
1632           else if (unformat (line_input, "event-queue-size 0x%lx",
1633                              &vcl_cfg->event_queue_size))
1634             {
1635               if (VPPCOM_DEBUG > 0)
1636                 clib_warning ("[%d] configured event_queue_size 0x%lx (%ld)",
1637                               vcm->my_pid, vcl_cfg->event_queue_size,
1638                               vcl_cfg->event_queue_size);
1639             }
1640           else if (unformat (line_input, "event-queue-size %ld",
1641                              &vcl_cfg->event_queue_size))
1642             {
1643               if (VPPCOM_DEBUG > 0)
1644                 clib_warning ("[%d] configured event_queue_size %ld (0x%lx)",
1645                               vcm->my_pid, vcl_cfg->event_queue_size,
1646                               vcl_cfg->event_queue_size);
1647             }
1648           else if (unformat (line_input, "listen-queue-size 0x%lx",
1649                              &vcl_cfg->listen_queue_size))
1650             {
1651               if (VPPCOM_DEBUG > 0)
1652                 clib_warning ("[%d] configured listen_queue_size 0x%lx (%ld)",
1653                               vcm->my_pid, vcl_cfg->listen_queue_size,
1654                               vcl_cfg->listen_queue_size);
1655             }
1656           else if (unformat (line_input, "listen-queue-size %ld",
1657                              &vcl_cfg->listen_queue_size))
1658             {
1659               if (VPPCOM_DEBUG > 0)
1660                 clib_warning ("[%d] configured listen_queue_size %ld (0x%lx)",
1661                               vcm->my_pid, vcl_cfg->listen_queue_size,
1662                               vcl_cfg->listen_queue_size);
1663             }
1664           else if (unformat (line_input, "app-timeout %f",
1665                              &vcl_cfg->app_timeout))
1666             {
1667               if (VPPCOM_DEBUG > 0)
1668                 clib_warning ("[%d] configured app_timeout %f",
1669                               vcm->my_pid, vcl_cfg->app_timeout);
1670             }
1671           else if (unformat (line_input, "session-timeout %f",
1672                              &vcl_cfg->session_timeout))
1673             {
1674               if (VPPCOM_DEBUG > 0)
1675                 clib_warning ("[%d] configured session_timeout %f",
1676                               vcm->my_pid, vcl_cfg->session_timeout);
1677             }
1678           else if (unformat (line_input, "accept-timeout %f",
1679                              &vcl_cfg->accept_timeout))
1680             {
1681               if (VPPCOM_DEBUG > 0)
1682                 clib_warning ("[%d] configured accept_timeout %f",
1683                               vcm->my_pid, vcl_cfg->accept_timeout);
1684             }
1685           else if (unformat (line_input, "}"))
1686             {
1687               vc_cfg_input = 0;
1688               if (VPPCOM_DEBUG > 0)
1689                 clib_warning ("[%d] completed parsing vppcom config!",
1690                               vcm->my_pid);
1691               goto input_done;
1692             }
1693           else
1694             {
1695               if (line_input->buffer[line_input->index] != '#')
1696                 {
1697                   clib_warning ("[%d] Unknown vppcom config option: '%s'",
1698                                 vcm->my_pid, (char *)
1699                                 &line_input->buffer[line_input->index]);
1700                 }
1701             }
1702         }
1703     }
1704
1705 input_done:
1706   unformat_free (input);
1707
1708 file_done:
1709   if (fd >= 0)
1710     close (fd);
1711 }
1712
1713 /*
1714  * VPPCOM Public API functions
1715  */
1716 int
1717 vppcom_app_create (char *app_name)
1718 {
1719   vppcom_main_t *vcm = &vppcom_main;
1720   vppcom_cfg_t *vcl_cfg = &vcm->cfg;
1721   u8 *heap;
1722   mheap_t *h;
1723   int rv;
1724
1725   if (!vcm->init)
1726     {
1727       char *conf_fname;
1728
1729       vcm->init = 1;
1730       vcm->my_pid = getpid ();
1731       clib_fifo_validate (vcm->client_session_index_fifo,
1732                           vcm->cfg.listen_queue_size);
1733       vppcom_cfg_init (vcl_cfg);
1734       conf_fname = getenv (VPPCOM_CONF_ENV);
1735       if (!conf_fname)
1736         {
1737           conf_fname = VPPCOM_CONF_DEFAULT;
1738           if (VPPCOM_DEBUG > 0)
1739             clib_warning ("[%d] getenv '%s' failed!", vcm->my_pid,
1740                           VPPCOM_CONF_ENV);
1741         }
1742       vppcom_cfg_heapsize (conf_fname);
1743       vppcom_cfg_read (conf_fname);
1744       vcm->bind_session_index = ~0;
1745       vcm->main_cpu = os_get_thread_index ();
1746       heap = clib_mem_get_per_cpu_heap ();
1747       h = mheap_header (heap);
1748
1749       /* make the main heap thread-safe */
1750       h->flags |= MHEAP_FLAG_THREAD_SAFE;
1751
1752       vcm->session_index_by_vpp_handles = hash_create (0, sizeof (uword));
1753
1754       clib_time_init (&vcm->clib_time);
1755       vppcom_init_error_string_table ();
1756       svm_fifo_segment_init (vcl_cfg->segment_baseva,
1757                              20 /* timeout in secs */ );
1758       clib_spinlock_init (&vcm->sessions_lockp);
1759       vppcom_api_hookup ();
1760     }
1761
1762   if (vcm->my_client_index == ~0)
1763     {
1764       vcm->app_state = STATE_APP_START;
1765       rv = vppcom_connect_to_vpp (app_name);
1766       if (rv)
1767         {
1768           clib_warning ("[%d] couldn't connect to VPP.", vcm->my_pid);
1769           return rv;
1770         }
1771
1772       if (VPPCOM_DEBUG > 0)
1773         clib_warning ("[%d] sending session enable", vcm->my_pid);
1774
1775       rv = vppcom_app_session_enable ();
1776       if (rv)
1777         {
1778           clib_warning ("[%d] vppcom_app_session_enable() failed!",
1779                         vcm->my_pid);
1780           return rv;
1781         }
1782
1783       if (VPPCOM_DEBUG > 0)
1784         clib_warning ("[%d] sending app attach", vcm->my_pid);
1785
1786       rv = vppcom_app_attach ();
1787       if (rv)
1788         {
1789           clib_warning ("[%d] vppcom_app_attach() failed!", vcm->my_pid);
1790           return rv;
1791         }
1792     }
1793
1794   if (VPPCOM_DEBUG > 0)
1795     clib_warning ("[%d] app_name '%s', my_client_index %d (0x%x)",
1796                   vcm->my_pid, app_name, vcm->my_client_index,
1797                   vcm->my_client_index);
1798
1799   return VPPCOM_OK;
1800 }
1801
1802 void
1803 vppcom_app_destroy (void)
1804 {
1805   vppcom_main_t *vcm = &vppcom_main;
1806   int rv;
1807
1808   if (vcm->my_client_index == ~0)
1809     return;
1810
1811   if (VPPCOM_DEBUG > 0)
1812     clib_warning ("[%d] detaching from VPP, my_client_index %d (0x%x)",
1813                   vcm->my_pid, vcm->my_client_index, vcm->my_client_index);
1814
1815   vppcom_app_detach ();
1816   rv = vppcom_wait_for_app_state_change (STATE_APP_ENABLED);
1817   if (PREDICT_FALSE (rv))
1818     {
1819       if (VPPCOM_DEBUG > 0)
1820         clib_warning ("[%d] application detach timed out, rv = %s (%d)",
1821                       vcm->my_pid, vppcom_retval_str (rv), rv);
1822     }
1823   vl_client_disconnect_from_vlib ();
1824   vcm->my_client_index = ~0;
1825   vcm->app_state = STATE_APP_START;
1826 }
1827
1828 int
1829 vppcom_session_create (u32 vrf, u8 proto, u8 is_nonblocking)
1830 {
1831   vppcom_main_t *vcm = &vppcom_main;
1832   session_t *session;
1833   u32 session_index;
1834
1835   clib_spinlock_lock (&vcm->sessions_lockp);
1836   pool_get (vcm->sessions, session);
1837   memset (session, 0, sizeof (*session));
1838   session_index = session - vcm->sessions;
1839
1840   session->vrf = vrf;
1841   session->proto = proto;
1842   session->state = STATE_START;
1843   session->is_nonblocking = is_nonblocking ? 1 : 0;
1844   clib_spinlock_unlock (&vcm->sessions_lockp);
1845
1846   if (VPPCOM_DEBUG > 0)
1847     clib_warning ("[%d] sid %d", vcm->my_pid, session_index);
1848
1849   return (int) session_index;
1850 }
1851
1852 int
1853 vppcom_session_close (uint32_t session_index)
1854 {
1855   vppcom_main_t *vcm = &vppcom_main;
1856   session_t *session = 0;
1857   int rv;
1858
1859   clib_spinlock_lock (&vcm->sessions_lockp);
1860   rv = vppcom_session_at_index (session_index, &session);
1861   if (PREDICT_FALSE (rv))
1862     {
1863       if (VPPCOM_DEBUG > 0)
1864         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1865                       vcm->my_pid, session_index);
1866       clib_spinlock_unlock (&vcm->sessions_lockp);
1867       goto done;
1868     }
1869   clib_spinlock_unlock (&vcm->sessions_lockp);
1870
1871   if (VPPCOM_DEBUG > 0)
1872     clib_warning ("[%d] sid %d", vcm->my_pid, session_index);
1873
1874   if (session->is_vep)
1875     {
1876       u32 next_sid;
1877       for (next_sid = session->vep.next_sid; next_sid != ~0;
1878            next_sid = session->vep.next_sid)
1879         {
1880           rv = vppcom_epoll_ctl (session_index, EPOLL_CTL_DEL, next_sid, 0);
1881           if ((VPPCOM_DEBUG > 0) && (rv < 0))
1882             clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
1883                           "rv = %s (%d)", session_index, next_sid,
1884                           vcm->my_pid, session_index,
1885                           vppcom_retval_str (rv), rv);
1886
1887           clib_spinlock_lock (&vcm->sessions_lockp);
1888           rv = vppcom_session_at_index (session_index, &session);
1889           if (PREDICT_FALSE (rv))
1890             {
1891               if (VPPCOM_DEBUG > 0)
1892                 clib_warning
1893                   ("[%d] invalid session, sid (%u) has been closed!",
1894                    vcm->my_pid, session_index);
1895               clib_spinlock_unlock (&vcm->sessions_lockp);
1896               goto done;
1897             }
1898           clib_spinlock_unlock (&vcm->sessions_lockp);
1899         }
1900     }
1901   else
1902     {
1903       if (session->is_vep_session)
1904         {
1905           u32 vep_idx = session->vep.vep_idx;
1906           rv = vppcom_epoll_ctl (vep_idx, EPOLL_CTL_DEL, session_index, 0);
1907           if ((VPPCOM_DEBUG > 0) && (rv < 0))
1908             clib_warning ("[%d] EPOLL_CTL_DEL vep_idx %u, sid %u failed, "
1909                           "rv = %s (%d)", vep_idx, session_index,
1910                           vcm->my_pid, session_index,
1911                           vppcom_retval_str (rv), rv);
1912
1913           clib_spinlock_lock (&vcm->sessions_lockp);
1914           rv = vppcom_session_at_index (session_index, &session);
1915           if (PREDICT_FALSE (rv))
1916             {
1917               if (VPPCOM_DEBUG > 0)
1918                 clib_warning
1919                   ("[%d] invalid session, sid (%u) has been closed!",
1920                    vcm->my_pid, session_index);
1921               clib_spinlock_unlock (&vcm->sessions_lockp);
1922               goto done;
1923             }
1924           clib_spinlock_unlock (&vcm->sessions_lockp);
1925         }
1926
1927       if (session->is_cut_thru && session->is_server &&
1928           (session->state == STATE_ACCEPT))
1929         {
1930           rv = vppcom_session_unbind_cut_thru (session);
1931           if ((VPPCOM_DEBUG > 0) && (rv < 0))
1932             clib_warning ("[%d] unbind cut-thru (session %d) failed, "
1933                           "rv = %s (%d)",
1934                           vcm->my_pid, session_index,
1935                           vppcom_retval_str (rv), rv);
1936         }
1937       else if (session->is_server && session->is_listen)
1938         {
1939           rv = vppcom_session_unbind (session_index);
1940           if ((VPPCOM_DEBUG > 0) && (rv < 0))
1941             clib_warning ("[%d] unbind (session %d) failed, rv = %s (%d)",
1942                           vcm->my_pid, session_index,
1943                           vppcom_retval_str (rv), rv);
1944         }
1945       else if (session->state == STATE_CONNECT)
1946         {
1947           rv = vppcom_session_disconnect (session_index);
1948           if ((VPPCOM_DEBUG > 0) && (rv < 0))
1949             clib_warning ("[%d] disconnect (session %d) failed, rv = %s (%d)",
1950                           vcm->my_pid, session_index,
1951                           vppcom_retval_str (rv), rv);
1952         }
1953     }
1954   pool_put_index (vcm->sessions, session_index);
1955 done:
1956   return rv;
1957 }
1958
1959 int
1960 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
1961 {
1962   vppcom_main_t *vcm = &vppcom_main;
1963   session_t *session = 0;
1964   int rv;
1965
1966   if (!ep || !ep->ip)
1967     return VPPCOM_EINVAL;
1968
1969   clib_spinlock_lock (&vcm->sessions_lockp);
1970   rv = vppcom_session_at_index (session_index, &session);
1971   if (PREDICT_FALSE (rv))
1972     {
1973       clib_spinlock_unlock (&vcm->sessions_lockp);
1974       if (VPPCOM_DEBUG > 0)
1975         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1976                       vcm->my_pid, session_index);
1977       return rv;
1978     }
1979
1980   if (session->is_vep)
1981     {
1982       clib_spinlock_unlock (&vcm->sessions_lockp);
1983       if (VPPCOM_DEBUG > 0)
1984         clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
1985                       vcm->my_pid, session_index);
1986       return VPPCOM_EBADFD;
1987     }
1988
1989   if (VPPCOM_DEBUG > 0)
1990     clib_warning ("[%d] sid %d", vcm->my_pid, session_index);
1991
1992   session->vrf = ep->vrf;
1993   session->lcl_addr.is_ip4 = ep->is_ip4;
1994   session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
1995   session->port = ep->port;
1996
1997   clib_spinlock_unlock (&vcm->sessions_lockp);
1998   return VPPCOM_OK;
1999 }
2000
2001 int
2002 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
2003 {
2004   vppcom_main_t *vcm = &vppcom_main;
2005   session_t *listen_session = 0;
2006   int rv;
2007
2008   clib_spinlock_lock (&vcm->sessions_lockp);
2009   rv = vppcom_session_at_index (listen_session_index, &listen_session);
2010   if (PREDICT_FALSE (rv))
2011     {
2012       clib_spinlock_unlock (&vcm->sessions_lockp);
2013       if (VPPCOM_DEBUG > 0)
2014         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2015                       vcm->my_pid, listen_session_index);
2016       return rv;
2017     }
2018
2019   if (listen_session->is_vep)
2020     {
2021       clib_spinlock_unlock (&vcm->sessions_lockp);
2022       if (VPPCOM_DEBUG > 0)
2023         clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2024                       vcm->my_pid, listen_session_index);
2025       return VPPCOM_EBADFD;
2026     }
2027
2028   if (VPPCOM_DEBUG > 0)
2029     clib_warning ("[%d] sid %d", vcm->my_pid, listen_session_index);
2030
2031   ASSERT (vcm->bind_session_index == ~0);
2032   vcm->bind_session_index = listen_session_index;
2033   vppcom_send_bind_sock (listen_session);
2034   clib_spinlock_unlock (&vcm->sessions_lockp);
2035   rv =
2036     vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2037                                           vcm->cfg.session_timeout);
2038   if (PREDICT_FALSE (rv))
2039     {
2040       vcm->bind_session_index = ~0;
2041       if (VPPCOM_DEBUG > 0)
2042         clib_warning ("[%d] server listen timed out, rv = %d (%d)",
2043                       vcm->my_pid, vppcom_retval_str (rv), rv);
2044       return rv;
2045     }
2046
2047   clib_spinlock_lock (&vcm->sessions_lockp);
2048   rv = vppcom_session_at_index (listen_session_index, &listen_session);
2049   if (PREDICT_FALSE (rv))
2050     {
2051       clib_spinlock_unlock (&vcm->sessions_lockp);
2052       if (VPPCOM_DEBUG > 0)
2053         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2054                       vcm->my_pid, listen_session_index);
2055       return rv;
2056     }
2057   listen_session->is_listen = 1;
2058   clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2059   clib_spinlock_unlock (&vcm->sessions_lockp);
2060
2061   return VPPCOM_OK;
2062 }
2063
2064 int
2065 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2066                        double wait_for_time)
2067 {
2068   vppcom_main_t *vcm = &vppcom_main;
2069   session_t *listen_session = 0;
2070   session_t *client_session = 0;
2071   u32 client_session_index;
2072   int rv;
2073   f64 wait_for;
2074
2075   clib_spinlock_lock (&vcm->sessions_lockp);
2076   rv = vppcom_session_at_index (listen_session_index, &listen_session);
2077   if (PREDICT_FALSE (rv))
2078     {
2079       clib_spinlock_unlock (&vcm->sessions_lockp);
2080       if (VPPCOM_DEBUG > 0)
2081         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2082                       vcm->my_pid, listen_session_index);
2083       return rv;
2084     }
2085
2086   if (listen_session->is_vep)
2087     {
2088       clib_spinlock_unlock (&vcm->sessions_lockp);
2089       if (VPPCOM_DEBUG > 0)
2090         clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2091                       vcm->my_pid, listen_session_index);
2092       return VPPCOM_EBADFD;
2093     }
2094
2095   if (listen_session->state != STATE_LISTEN)
2096     {
2097       clib_spinlock_unlock (&vcm->sessions_lockp);
2098       if (VPPCOM_DEBUG > 0)
2099         clib_warning ("[%d] session not in listen state, state = %s",
2100                       vcm->my_pid,
2101                       vppcom_session_state_str (listen_session->state));
2102       return VPPCOM_EBADFD;
2103     }
2104   wait_for = listen_session->is_nonblocking ? 0 :
2105     (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time;
2106
2107   if (VPPCOM_DEBUG > 0)
2108     clib_warning ("[%d] sid %d: %s (%d)", vcm->my_pid,
2109                   listen_session_index,
2110                   vppcom_session_state_str (listen_session->state),
2111                   listen_session->state);
2112   clib_spinlock_unlock (&vcm->sessions_lockp);
2113
2114   while (1)
2115     {
2116       rv = vppcom_wait_for_client_session_index (wait_for);
2117       if (rv)
2118         {
2119           if ((VPPCOM_DEBUG > 0))
2120             clib_warning ("[%d] sid %d, accept timed out, rv = %s (%d)",
2121                           vcm->my_pid, listen_session_index,
2122                           vppcom_retval_str (rv), rv);
2123           if ((wait_for == 0) || (wait_for_time > 0))
2124             return rv;
2125         }
2126       else
2127         break;
2128     }
2129
2130   clib_spinlock_lock (&vcm->sessions_lockp);
2131   clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
2132   rv = vppcom_session_at_index (client_session_index, &client_session);
2133   ASSERT (rv == VPPCOM_OK);
2134   ASSERT (client_session->peer_addr.is_ip4 ==
2135           listen_session->lcl_addr.is_ip4);
2136
2137   if (VPPCOM_DEBUG > 0)
2138     clib_warning ("[%d] Got a request: client sid %d", vcm->my_pid,
2139                   client_session_index);
2140
2141   ep->vrf = client_session->vrf;
2142   ep->is_cut_thru = client_session->is_cut_thru;
2143   ep->is_ip4 = client_session->peer_addr.is_ip4;
2144   ep->port = client_session->port;
2145   if (client_session->peer_addr.is_ip4)
2146     clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2147                  sizeof (ip4_address_t));
2148   else
2149     clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2150                  sizeof (ip6_address_t));
2151   clib_spinlock_unlock (&vcm->sessions_lockp);
2152   return (int) client_session_index;
2153 }
2154
2155 int
2156 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2157 {
2158   vppcom_main_t *vcm = &vppcom_main;
2159   session_t *session = 0;
2160   int rv;
2161
2162   clib_spinlock_lock (&vcm->sessions_lockp);
2163   rv = vppcom_session_at_index (session_index, &session);
2164   if (PREDICT_FALSE (rv))
2165     {
2166       clib_spinlock_unlock (&vcm->sessions_lockp);
2167       if (VPPCOM_DEBUG > 0)
2168         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2169                       vcm->my_pid, session_index);
2170       return rv;
2171     }
2172
2173   if (session->is_vep)
2174     {
2175       clib_spinlock_unlock (&vcm->sessions_lockp);
2176       if (VPPCOM_DEBUG > 0)
2177         clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2178                       vcm->my_pid, session_index);
2179       return VPPCOM_EBADFD;
2180     }
2181
2182   if (session->state == STATE_CONNECT)
2183     {
2184       clib_spinlock_unlock (&vcm->sessions_lockp);
2185       if (VPPCOM_DEBUG > 0)
2186         clib_warning ("[%d] session, sid (%u) already connected!",
2187                       vcm->my_pid, session_index);
2188       return VPPCOM_OK;
2189     }
2190
2191   session->vrf = server_ep->vrf;
2192   session->peer_addr.is_ip4 = server_ep->is_ip4;
2193   session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
2194   session->port = server_ep->port;
2195
2196   if (VPPCOM_DEBUG > 0)
2197     {
2198       u8 *ip_str = format (0, "%U", format_ip46_address,
2199                            &session->peer_addr.ip46,
2200                            session->peer_addr.is_ip4);
2201       clib_warning ("[%d] connect sid %d to %s server port %d proto %s",
2202                     vcm->my_pid, session_index, ip_str,
2203                     clib_net_to_host_u16 (session->port),
2204                     session->proto ? "UDP" : "TCP");
2205       vec_free (ip_str);
2206     }
2207
2208   vppcom_send_connect_sock (session, session_index);
2209   clib_spinlock_unlock (&vcm->sessions_lockp);
2210   rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2211                                              vcm->cfg.session_timeout);
2212   if (PREDICT_FALSE (rv))
2213     {
2214       if (VPPCOM_DEBUG > 0)
2215         clib_warning ("[%d] connect timed out, rv = %s (%d)",
2216                       vcm->my_pid, vppcom_retval_str (rv), rv);
2217       return rv;
2218     }
2219   if (VPPCOM_DEBUG > 0)
2220     clib_warning ("[%d] sid %d connected!", vcm->my_pid, session_index);
2221
2222   return VPPCOM_OK;
2223 }
2224
2225 int
2226 vppcom_session_read (uint32_t session_index, void *buf, int n)
2227 {
2228   vppcom_main_t *vcm = &vppcom_main;
2229   session_t *session = 0;
2230   svm_fifo_t *rx_fifo;
2231   int n_read = 0;
2232   int rv;
2233   char *fifo_str;
2234   u32 poll_et;
2235
2236   ASSERT (buf);
2237
2238   clib_spinlock_lock (&vcm->sessions_lockp);
2239   rv = vppcom_session_at_index (session_index, &session);
2240   if (PREDICT_FALSE (rv))
2241     {
2242       clib_spinlock_unlock (&vcm->sessions_lockp);
2243       if (VPPCOM_DEBUG > 0)
2244         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2245                       vcm->my_pid, session_index);
2246       return rv;
2247     }
2248
2249   if (session->is_vep)
2250     {
2251       clib_spinlock_unlock (&vcm->sessions_lockp);
2252       if (VPPCOM_DEBUG > 0)
2253         clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2254                       vcm->my_pid, session_index);
2255       return VPPCOM_EBADFD;
2256     }
2257
2258   if (session->state == STATE_DISCONNECT)
2259     {
2260       clib_spinlock_unlock (&vcm->sessions_lockp);
2261       if (VPPCOM_DEBUG > 0)
2262         clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2263                       vcm->my_pid, session_index);
2264       return VPPCOM_ECONNRESET;
2265     }
2266
2267   rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2268              session->server_rx_fifo : session->server_tx_fifo);
2269   fifo_str = ((!session->is_cut_thru || session->is_server) ?
2270               "server_rx_fifo" : "server_tx_fifo");
2271   poll_et = EPOLLET & session->vep.ev.events;
2272   clib_spinlock_unlock (&vcm->sessions_lockp);
2273
2274   do
2275     {
2276       n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
2277     }
2278   while (!session->is_nonblocking && (n_read <= 0));
2279
2280   if (poll_et && (n_read <= 0))
2281     {
2282       clib_spinlock_lock (&vcm->sessions_lockp);
2283       session->vep.et_mask |= EPOLLIN;
2284       clib_spinlock_unlock (&vcm->sessions_lockp);
2285     }
2286
2287   if ((VPPCOM_DEBUG > 2) && (n_read > 0))
2288     clib_warning ("[%d] sid %d, read %d bytes from %s (%p)", vcm->my_pid,
2289                   session_index, n_read, fifo_str, rx_fifo);
2290
2291   return (n_read <= 0) ? VPPCOM_EAGAIN : n_read;
2292 }
2293
2294 static inline int
2295 vppcom_session_read_ready (session_t * session, u32 session_index)
2296 {
2297   vppcom_main_t *vcm = &vppcom_main;
2298   svm_fifo_t *rx_fifo;
2299   int ready = 0;
2300
2301   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2302   if (session->is_vep)
2303     {
2304       clib_spinlock_unlock (&vcm->sessions_lockp);
2305       if (VPPCOM_DEBUG > 0)
2306         clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2307                       vcm->my_pid, session_index);
2308       return VPPCOM_EBADFD;
2309     }
2310
2311   if (session->state == STATE_DISCONNECT)
2312     {
2313       if (VPPCOM_DEBUG > 0)
2314         clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2315                       vcm->my_pid, session_index);
2316       return VPPCOM_ECONNRESET;
2317     }
2318
2319   if (session->is_listen)
2320     ready = clib_fifo_elts (vcm->client_session_index_fifo);
2321   else
2322     {
2323       rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2324                  session->server_rx_fifo : session->server_tx_fifo);
2325
2326       ready = svm_fifo_max_dequeue (rx_fifo);
2327     }
2328
2329   if (VPPCOM_DEBUG > 3)
2330     clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", vcm->my_pid,
2331                   session_index,
2332                   session->is_server ? "server_rx_fifo" : "server_tx_fifo",
2333                   rx_fifo, ready);
2334   if ((session->vep.ev.events & EPOLLET) && (ready == 0))
2335     session->vep.et_mask |= EPOLLIN;
2336
2337   return ready;
2338 }
2339
2340 int
2341 vppcom_session_write (uint32_t session_index, void *buf, int n)
2342 {
2343   vppcom_main_t *vcm = &vppcom_main;
2344   session_t *session = 0;
2345   svm_fifo_t *tx_fifo;
2346   unix_shared_memory_queue_t *q;
2347   session_fifo_event_t evt;
2348   int rv, n_write;
2349   char *fifo_str;
2350   u32 poll_et;
2351
2352   ASSERT (buf);
2353
2354   clib_spinlock_lock (&vcm->sessions_lockp);
2355   rv = vppcom_session_at_index (session_index, &session);
2356   if (PREDICT_FALSE (rv))
2357     {
2358       clib_spinlock_unlock (&vcm->sessions_lockp);
2359       if (VPPCOM_DEBUG > 0)
2360         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2361                       vcm->my_pid, session_index);
2362       return rv;
2363     }
2364
2365   if (session->is_vep)
2366     {
2367       clib_spinlock_unlock (&vcm->sessions_lockp);
2368       if (VPPCOM_DEBUG > 0)
2369         clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2370                       vcm->my_pid, session_index);
2371       return VPPCOM_EBADFD;
2372     }
2373
2374   if (session->state == STATE_DISCONNECT)
2375     {
2376       clib_spinlock_unlock (&vcm->sessions_lockp);
2377       if (VPPCOM_DEBUG > 0)
2378         clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2379                       vcm->my_pid, session_index);
2380       return VPPCOM_ECONNRESET;
2381     }
2382
2383   tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2384              session->server_tx_fifo : session->server_rx_fifo);
2385   fifo_str = ((!session->is_cut_thru || session->is_server) ?
2386               "server_tx_fifo" : "server_rx_fifo");
2387   q = session->vpp_event_queue;
2388   poll_et = EPOLLET & session->vep.ev.events;
2389   clib_spinlock_unlock (&vcm->sessions_lockp);
2390
2391   do
2392     {
2393       n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
2394     }
2395   while (!session->is_nonblocking && (n_write <= 0));
2396
2397   /* If event wasn't set, add one */
2398   if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
2399     {
2400       int rval;
2401
2402       /* Fabricate TX event, send to vpp */
2403       evt.fifo = tx_fifo;
2404       evt.event_type = FIFO_EVENT_APP_TX;
2405
2406       rval = vppcom_session_at_index (session_index, &session);
2407       if (PREDICT_FALSE (rval))
2408         {
2409           if (VPPCOM_DEBUG > 1)
2410             clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2411                           vcm->my_pid, session_index);
2412           return rval;
2413         }
2414       ASSERT (q);
2415       unix_shared_memory_queue_add (q, (u8 *) & evt,
2416                                     0 /* do wait for mutex */ );
2417     }
2418
2419   if (poll_et && (n_write <= 0))
2420     {
2421       clib_spinlock_lock (&vcm->sessions_lockp);
2422       session->vep.et_mask |= EPOLLOUT;
2423       clib_spinlock_unlock (&vcm->sessions_lockp);
2424     }
2425
2426   if (VPPCOM_DEBUG > 2)
2427     {
2428       if (n_write == -2)
2429         clib_warning ("[%d] sid %d, FIFO-FULL %s (%p)", vcm->my_pid,
2430                       session_index, fifo_str, tx_fifo);
2431       else
2432         clib_warning ("[%d] sid %d, wrote %d bytes to %s (%p)", vcm->my_pid,
2433                       session_index, n_write, fifo_str, tx_fifo);
2434     }
2435   return (n_write < 0) ? VPPCOM_EAGAIN : n_write;
2436 }
2437
2438 static inline int
2439 vppcom_session_write_ready (session_t * session, u32 session_index)
2440 {
2441   vppcom_main_t *vcm = &vppcom_main;
2442   svm_fifo_t *tx_fifo;
2443   char *fifo_str;
2444   int ready;
2445
2446   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2447   if (session->is_vep)
2448     {
2449       clib_spinlock_unlock (&vcm->sessions_lockp);
2450       if (VPPCOM_DEBUG > 0)
2451         clib_warning ("[%d] invalid session, sid (%u) is an epoll session!",
2452                       vcm->my_pid, session_index);
2453       return VPPCOM_EBADFD;
2454     }
2455
2456   if (session->state == STATE_DISCONNECT)
2457     {
2458       if (VPPCOM_DEBUG > 0)
2459         clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2460                       vcm->my_pid, session_index);
2461       return VPPCOM_ECONNRESET;
2462     }
2463
2464   tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2465              session->server_tx_fifo : session->server_rx_fifo);
2466   fifo_str = ((!session->is_cut_thru || session->is_server) ?
2467               "server_tx_fifo" : "server_rx_fifo");
2468
2469   ready = svm_fifo_max_enqueue (tx_fifo);
2470
2471   if (VPPCOM_DEBUG > 3)
2472     clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", vcm->my_pid,
2473                   session_index, fifo_str, tx_fifo, ready);
2474   if ((session->vep.ev.events & EPOLLET) && (ready == 0))
2475     session->vep.et_mask |= EPOLLOUT;
2476
2477   return ready;
2478 }
2479
2480 int
2481 vppcom_select (unsigned long n_bits, unsigned long *read_map,
2482                unsigned long *write_map, unsigned long *except_map,
2483                double time_to_wait)
2484 {
2485   vppcom_main_t *vcm = &vppcom_main;
2486   u32 session_index;
2487   session_t *session = 0;
2488   int rv, bits_set = 0;
2489   f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2490   u32 minbits = clib_max (n_bits, BITS (uword));
2491
2492   ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2493
2494   if (n_bits && read_map)
2495     {
2496       clib_bitmap_validate (vcm->rd_bitmap, minbits);
2497       clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
2498       memset (read_map, 0, vec_len (vcm->rd_bitmap));
2499     }
2500   if (n_bits && write_map)
2501     {
2502       clib_bitmap_validate (vcm->wr_bitmap, minbits);
2503       clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
2504       memset (write_map, 0, vec_len (vcm->wr_bitmap));
2505     }
2506   if (n_bits && except_map)
2507     {
2508       clib_bitmap_validate (vcm->ex_bitmap, minbits);
2509       clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
2510       memset (except_map, 0, vec_len (vcm->ex_bitmap));
2511     }
2512
2513   do
2514     {
2515       /* *INDENT-OFF* */
2516       if (n_bits)
2517         {
2518           if (read_map)
2519             {
2520               clib_bitmap_foreach (session_index, vcm->rd_bitmap,
2521                 ({
2522                   clib_spinlock_lock (&vcm->sessions_lockp);
2523                   rv = vppcom_session_at_index (session_index, &session);
2524                   if (rv < 0)
2525                     {
2526                       clib_spinlock_unlock (&vcm->sessions_lockp);
2527                       if (VPPCOM_DEBUG > 1)
2528                         clib_warning ("[%d] session %d specified in "
2529                                       "read_map is closed.", vcm->my_pid,
2530                                       session_index);
2531                       bits_set = VPPCOM_EBADFD;
2532                       goto select_done;
2533                     }
2534
2535                   rv = vppcom_session_read_ready (session, session_index);
2536                   clib_spinlock_unlock (&vcm->sessions_lockp);
2537                   if (except_map && vcm->ex_bitmap &&
2538                       clib_bitmap_get (vcm->ex_bitmap, session_index) &&
2539                       (rv < 0))
2540                     {
2541                       // TBD: clib_warning
2542                       clib_bitmap_set_no_check (except_map, session_index, 1);
2543                       bits_set++;
2544                     }
2545                   else if (rv > 0)
2546                     {
2547                       // TBD: clib_warning
2548                       clib_bitmap_set_no_check (read_map, session_index, 1);
2549                       bits_set++;
2550                     }
2551                 }));
2552             }
2553
2554           if (write_map)
2555             {
2556               clib_bitmap_foreach (session_index, vcm->wr_bitmap,
2557                 ({
2558                   clib_spinlock_lock (&vcm->sessions_lockp);
2559                   rv = vppcom_session_at_index (session_index, &session);
2560                   if (rv < 0)
2561                     {
2562                       clib_spinlock_unlock (&vcm->sessions_lockp);
2563                       if (VPPCOM_DEBUG > 0)
2564                         clib_warning ("[%d] session %d specified in "
2565                                       "write_map is closed.", vcm->my_pid,
2566                                       session_index);
2567                       bits_set = VPPCOM_EBADFD;
2568                       goto select_done;
2569                     }
2570
2571                   rv = vppcom_session_write_ready (session, session_index);
2572                   clib_spinlock_unlock (&vcm->sessions_lockp);
2573                   if (write_map && (rv > 0))
2574                     {
2575                       // TBD: clib_warning
2576                       clib_bitmap_set_no_check (write_map, session_index, 1);
2577                       bits_set++;
2578                     }
2579                 }));
2580             }
2581
2582           if (except_map)
2583             {
2584               clib_bitmap_foreach (session_index, vcm->ex_bitmap,
2585                 ({
2586                   clib_spinlock_lock (&vcm->sessions_lockp);
2587                   rv = vppcom_session_at_index (session_index, &session);
2588                   if (rv < 0)
2589                     {
2590                       clib_spinlock_unlock (&vcm->sessions_lockp);
2591                       if (VPPCOM_DEBUG > 1)
2592                         clib_warning ("[%d] session %d specified in "
2593                                       "except_map is closed.", vcm->my_pid,
2594                                       session_index);
2595                       bits_set = VPPCOM_EBADFD;
2596                       goto select_done;
2597                     }
2598
2599                   rv = vppcom_session_read_ready (session, session_index);
2600                   clib_spinlock_unlock (&vcm->sessions_lockp);
2601                   if (rv < 0)
2602                     {
2603                       // TBD: clib_warning
2604                       clib_bitmap_set_no_check (except_map, session_index, 1);
2605                       bits_set++;
2606                     }
2607                 }));
2608             }
2609         }
2610       /* *INDENT-ON* */
2611     }
2612   while (clib_time_now (&vcm->clib_time) < timeout);
2613
2614 select_done:
2615   return (bits_set);
2616 }
2617
2618 static inline void
2619 vep_verify_epoll_chain (u32 vep_idx)
2620 {
2621   session_t *session;
2622   vppcom_epoll_t *vep;
2623   int rv;
2624   u32 sid;
2625
2626   if (VPPCOM_DEBUG < 1)
2627     return;
2628
2629   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2630   rv = vppcom_session_at_index (vep_idx, &session);
2631   if (PREDICT_FALSE (rv))
2632     {
2633       clib_warning ("ERROR: Invalid vep_idx (%u)!", vep_idx);
2634       goto done;
2635     }
2636   if (PREDICT_FALSE (!session->is_vep))
2637     {
2638       clib_warning ("ERROR: vep_idx (%u) is not a vep!", vep_idx);
2639       goto done;
2640     }
2641   if (VPPCOM_DEBUG > 1)
2642     clib_warning ("vep_idx (%u): Dumping epoll chain\n"
2643                   "{\n"
2644                   "   is_vep         = %u\n"
2645                   "   is_vep_session = %u\n"
2646                   "   wait_cont_idx  = 0x%x (%u)\n"
2647                   "}\n",
2648                   vep_idx, session->is_vep, session->is_vep_session,
2649                   session->wait_cont_idx, session->wait_cont_idx);
2650   do
2651     {
2652       vep = &session->vep;
2653       sid = vep->next_sid;
2654       if (session->is_vep_session)
2655         {
2656           if (VPPCOM_DEBUG > 1)
2657             clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2658                           "{\n"
2659                           "   next_sid       = 0x%x (%u)\n"
2660                           "   prev_sid       = 0x%x (%u)\n"
2661                           "   vep_idx        = 0x%x (%u)\n"
2662                           "   ev.events      = 0x%x\n"
2663                           "   ev.data.u64    = 0x%llx\n"
2664                           "   et_mask        = 0x%x\n"
2665                           "}\n",
2666                           vep_idx, sid, sid,
2667                           vep->next_sid, vep->next_sid,
2668                           vep->prev_sid, vep->prev_sid,
2669                           vep->vep_idx, vep->vep_idx,
2670                           vep->ev.events, vep->ev.data.u64, vep->et_mask);
2671         }
2672       if (sid != ~0)
2673         {
2674           rv = vppcom_session_at_index (sid, &session);
2675           if (PREDICT_FALSE (rv))
2676             {
2677               clib_warning ("ERROR: Invalid sid (%u)!", sid);
2678               goto done;
2679             }
2680           if (PREDICT_FALSE (session->is_vep))
2681             clib_warning ("ERROR: sid (%u) is a vep!", vep_idx);
2682           else if (PREDICT_FALSE (!session->is_vep_session))
2683             {
2684               clib_warning ("ERROR: session (%u) is not a vep session!", sid);
2685               goto done;
2686             }
2687           if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2688             clib_warning ("ERROR: session (%u) vep_idx (%u) != "
2689                           "vep_idx (%u)!",
2690                           sid, session->vep.vep_idx, vep_idx);
2691         }
2692     }
2693   while (sid != ~0);
2694
2695 done:
2696   if (VPPCOM_DEBUG > 1)
2697     clib_warning ("vep_idx (%u): Dump complete!", vep_idx);
2698 }
2699
2700 int
2701 vppcom_epoll_create (void)
2702 {
2703   vppcom_main_t *vcm = &vppcom_main;
2704   session_t *vep_session;
2705   u32 vep_idx;
2706
2707   clib_spinlock_lock (&vcm->sessions_lockp);
2708   pool_get (vcm->sessions, vep_session);
2709   memset (vep_session, 0, sizeof (*vep_session));
2710   vep_idx = vep_session - vcm->sessions;
2711
2712   vep_session->is_vep = 1;
2713   vep_session->vep.vep_idx = ~0;
2714   vep_session->vep.next_sid = ~0;
2715   vep_session->vep.prev_sid = ~0;
2716   vep_session->wait_cont_idx = ~0;
2717   clib_spinlock_unlock (&vcm->sessions_lockp);
2718
2719   if (VPPCOM_DEBUG > 0)
2720     clib_warning ("Created vep_idx %u!", vep_idx);
2721
2722   return (vep_idx);
2723 }
2724
2725 int
2726 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
2727                   struct epoll_event *event)
2728 {
2729   vppcom_main_t *vcm = &vppcom_main;
2730   session_t *vep_session;
2731   session_t *session;
2732   int rv;
2733
2734   if (vep_idx == session_index)
2735     {
2736       if (VPPCOM_DEBUG > 0)
2737         clib_warning ("ERROR: vep_idx == session_index (%u)!", vep_idx);
2738       return VPPCOM_EINVAL;
2739     }
2740
2741   clib_spinlock_lock (&vcm->sessions_lockp);
2742   rv = vppcom_session_at_index (vep_idx, &vep_session);
2743   if (PREDICT_FALSE (rv))
2744     {
2745       if (VPPCOM_DEBUG > 0)
2746         clib_warning ("ERROR: Invalid vep_idx (%u)!", vep_idx);
2747       goto done;
2748     }
2749   if (PREDICT_FALSE (!vep_session->is_vep))
2750     {
2751       if (VPPCOM_DEBUG > 0)
2752         clib_warning ("ERROR: vep_idx (%u) is not a vep!", vep_idx);
2753       rv = VPPCOM_EINVAL;
2754       goto done;
2755     }
2756
2757   ASSERT (vep_session->vep.vep_idx == ~0);
2758   ASSERT (vep_session->vep.prev_sid == ~0);
2759
2760   rv = vppcom_session_at_index (session_index, &session);
2761   if (PREDICT_FALSE (rv))
2762     {
2763       if (VPPCOM_DEBUG > 0)
2764         clib_warning ("ERROR: Invalid session_index (%u)!", session_index);
2765       goto done;
2766     }
2767   if (PREDICT_FALSE (session->is_vep))
2768     {
2769       if (VPPCOM_DEBUG > 0)
2770         clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
2771       rv = VPPCOM_EINVAL;
2772       goto done;
2773     }
2774
2775   switch (op)
2776     {
2777     case EPOLL_CTL_ADD:
2778       if (PREDICT_FALSE (!event))
2779         {
2780           clib_warning ("NULL pointer to epoll_event structure!");
2781           rv = VPPCOM_EINVAL;
2782           goto done;
2783         }
2784       if (vep_session->vep.next_sid != ~0)
2785         {
2786           session_t *next_session;
2787           rv = vppcom_session_at_index (vep_session->vep.next_sid,
2788                                         &next_session);
2789           if (PREDICT_FALSE (rv))
2790             {
2791               if (VPPCOM_DEBUG > 0)
2792                 clib_warning ("EPOLL_CTL_ADD: Invalid vep.next_sid (%u) on"
2793                               " vep_idx (%u)!", vep_session->vep.next_sid,
2794                               vep_idx);
2795               goto done;
2796             }
2797           ASSERT (next_session->vep.prev_sid == vep_idx);
2798           next_session->vep.prev_sid = session_index;
2799         }
2800       session->vep.next_sid = vep_session->vep.next_sid;
2801       session->vep.prev_sid = vep_idx;
2802       session->vep.vep_idx = vep_idx;
2803       session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2804       session->vep.ev = *event;
2805       session->is_vep_session = 1;
2806       vep_session->vep.next_sid = session_index;
2807       if (VPPCOM_DEBUG > 1)
2808         clib_warning ("EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x,"
2809                       " data 0x%llx!", vep_idx, session_index,
2810                       event->events, event->data.u64);
2811       break;
2812
2813     case EPOLL_CTL_MOD:
2814       if (PREDICT_FALSE (!event))
2815         {
2816           clib_warning ("NULL pointer to epoll_event structure!");
2817           rv = VPPCOM_EINVAL;
2818           goto done;
2819         }
2820       if (PREDICT_FALSE (!session->is_vep_session &&
2821                          (session->vep.vep_idx != vep_idx)))
2822         {
2823           if (VPPCOM_DEBUG > 0)
2824             {
2825               if (!session->is_vep_session)
2826                 clib_warning ("EPOLL_CTL_MOD: session (%u) is not "
2827                               "a vep session!", session_index);
2828               else
2829                 clib_warning ("EPOLL_CTL_MOD: session (%u) vep_idx (%u) != "
2830                               "vep_idx (%u)!", session_index,
2831                               session->vep.vep_idx, vep_idx);
2832             }
2833           rv = VPPCOM_EINVAL;
2834           goto done;
2835         }
2836       session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2837       session->vep.ev = *event;
2838       if (VPPCOM_DEBUG > 1)
2839         clib_warning ("EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
2840                       " data 0x%llx!", vep_idx, session_index,
2841                       event->events, event->data.u64);
2842       break;
2843
2844     case EPOLL_CTL_DEL:
2845       if (PREDICT_FALSE (!session->is_vep_session &&
2846                          (session->vep.vep_idx != vep_idx)))
2847         {
2848           if (VPPCOM_DEBUG > 0)
2849             {
2850               if (!session->is_vep_session)
2851                 clib_warning ("EPOLL_CTL_DEL: session (%u) is not "
2852                               "a vep session!", session_index);
2853               else
2854                 clib_warning ("EPOLL_CTL_DEL: session (%u) vep_idx (%u) != "
2855                               "vep_idx (%u)!", session_index,
2856                               session->vep.vep_idx, vep_idx);
2857             }
2858           rv = VPPCOM_EINVAL;
2859           goto done;
2860         }
2861
2862       vep_session->wait_cont_idx =
2863         (vep_session->wait_cont_idx == session_index) ?
2864         session->vep.next_sid : vep_session->wait_cont_idx;
2865
2866       if (session->vep.prev_sid == vep_idx)
2867         vep_session->vep.next_sid = session->vep.next_sid;
2868       else
2869         {
2870           session_t *prev_session;
2871           rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
2872           if (PREDICT_FALSE (rv))
2873             {
2874               if (VPPCOM_DEBUG > 0)
2875                 clib_warning ("EPOLL_CTL_DEL: Invalid vep.prev_sid (%u) on"
2876                               " sid (%u)!", session->vep.prev_sid,
2877                               session_index);
2878               goto done;
2879             }
2880           ASSERT (prev_session->vep.next_sid == session_index);
2881           prev_session->vep.next_sid = session->vep.next_sid;
2882         }
2883       if (session->vep.next_sid != ~0)
2884         {
2885           session_t *next_session;
2886           rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
2887           if (PREDICT_FALSE (rv))
2888             {
2889               if (VPPCOM_DEBUG > 0)
2890                 clib_warning ("EPOLL_CTL_DEL: Invalid vep.next_sid (%u) on"
2891                               " sid (%u)!", session->vep.next_sid,
2892                               session_index);
2893               goto done;
2894             }
2895           ASSERT (next_session->vep.prev_sid == session_index);
2896           next_session->vep.prev_sid = session->vep.prev_sid;
2897         }
2898
2899       memset (&session->vep, 0, sizeof (session->vep));
2900       session->vep.next_sid = ~0;
2901       session->vep.prev_sid = ~0;
2902       session->vep.vep_idx = ~0;
2903       session->is_vep_session = 0;
2904       if (VPPCOM_DEBUG > 1)
2905         clib_warning ("EPOLL_CTL_DEL: vep_idx %u, sid %u!", vep_idx,
2906                       session_index);
2907       break;
2908
2909     default:
2910       clib_warning ("Invalid operation (%d)!", op);
2911       rv = VPPCOM_EINVAL;
2912     }
2913
2914   vep_verify_epoll_chain (vep_idx);
2915
2916 done:
2917   clib_spinlock_unlock (&vcm->sessions_lockp);
2918   return rv;
2919 }
2920
2921 #define VCL_LOCK_AND_GET_SESSION(I, S)                  \
2922 do {                                                    \
2923   vppcom_main_t *vcm = &vppcom_main;                    \
2924                                                         \
2925   clib_spinlock_lock (&vcm->sessions_lockp);            \
2926   rv = vppcom_session_at_index (I, S);                  \
2927   if (PREDICT_FALSE (rv))                               \
2928     {                                                   \
2929       clib_spinlock_unlock (&vcm->sessions_lockp);      \
2930                                                         \
2931       if (VPPCOM_DEBUG > 0)                             \
2932         clib_warning ("ERROR: Invalid ##I (%u)!", I);   \
2933                                                         \
2934       goto done;                                        \
2935     }                                                   \
2936 } while (0)
2937
2938 int
2939 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
2940                    int maxevents, double wait_for_time)
2941 {
2942   vppcom_main_t *vcm = &vppcom_main;
2943   session_t *vep_session;
2944   int rv;
2945   f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
2946   int num_ev = 0;
2947   u32 vep_next_sid, wait_cont_idx;
2948   u8 is_vep;
2949
2950   if (PREDICT_FALSE (maxevents <= 0))
2951     {
2952       if (VPPCOM_DEBUG > 0)
2953         clib_warning ("ERROR: Invalid maxevents (%d)!", maxevents);
2954       return VPPCOM_EINVAL;
2955     }
2956   if (PREDICT_FALSE (wait_for_time < 0))
2957     {
2958       if (VPPCOM_DEBUG > 0)
2959         clib_warning ("ERROR: Invalid wait_for_time (%f)!", wait_for_time);
2960       return VPPCOM_EINVAL;
2961     }
2962   memset (events, 0, sizeof (*events) * maxevents);
2963
2964   VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
2965   vep_next_sid = vep_session->vep.next_sid;
2966   is_vep = vep_session->is_vep;
2967   wait_cont_idx = vep_session->wait_cont_idx;
2968   clib_spinlock_unlock (&vcm->sessions_lockp);
2969
2970   if (PREDICT_FALSE (!is_vep))
2971     {
2972       if (VPPCOM_DEBUG > 0)
2973         clib_warning ("ERROR: vep_idx (%u) is not a vep!", vep_idx);
2974       rv = VPPCOM_EINVAL;
2975       goto done;
2976     }
2977   if ((VPPCOM_DEBUG > 0) && (PREDICT_FALSE (vep_next_sid == ~0)))
2978     {
2979       clib_warning ("WARNING: vep_idx (%u) is empty!", vep_idx);
2980       goto done;
2981     }
2982
2983   do
2984     {
2985       u32 sid;
2986       u32 next_sid = ~0;
2987       session_t *session;
2988
2989       for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
2990            sid != ~0; sid = next_sid)
2991         {
2992           u32 session_events, et_mask, clear_et_mask, session_vep_idx;
2993           u8 add_event, is_vep_session;
2994           int ready;
2995           u64 session_ev_data;
2996
2997           VCL_LOCK_AND_GET_SESSION (sid, &session);
2998           next_sid = session->vep.next_sid;
2999           session_events = session->vep.ev.events;
3000           et_mask = session->vep.et_mask;
3001           is_vep = session->is_vep;
3002           is_vep_session = session->is_vep_session;
3003           session_vep_idx = session->vep.vep_idx;
3004           session_ev_data = session->vep.ev.data.u64;
3005           clib_spinlock_unlock (&vcm->sessions_lockp);
3006
3007           if (PREDICT_FALSE (is_vep))
3008             {
3009               if (VPPCOM_DEBUG > 0)
3010                 clib_warning ("ERROR: sid (%u) is a vep!", vep_idx);
3011               rv = VPPCOM_EINVAL;
3012               goto done;
3013             }
3014           if (PREDICT_FALSE (!is_vep_session))
3015             {
3016               if (VPPCOM_DEBUG > 0)
3017                 clib_warning ("EPOLL_CTL_MOD: session (%u) is not "
3018                               "a vep session!", sid);
3019               rv = VPPCOM_EINVAL;
3020               goto done;
3021             }
3022           if (PREDICT_FALSE (session_vep_idx != vep_idx))
3023             {
3024               clib_warning ("EPOLL_CTL_MOD: session (%u) "
3025                             "vep_idx (%u) != vep_idx (%u)!",
3026                             sid, session->vep.vep_idx, vep_idx);
3027               rv = VPPCOM_EINVAL;
3028               goto done;
3029             }
3030
3031           add_event = clear_et_mask = 0;
3032
3033           if ((EPOLLIN & session_events) && (EPOLLIN & et_mask))
3034             {
3035               VCL_LOCK_AND_GET_SESSION (sid, &session);
3036               ready = vppcom_session_read_ready (session, sid);
3037               clib_spinlock_unlock (&vcm->sessions_lockp);
3038               if (ready > 0)
3039                 {
3040                   add_event = 1;
3041                   events[num_ev].events |= EPOLLIN;
3042                   if (EPOLLET & session_events)
3043                     clear_et_mask |= EPOLLIN;
3044                 }
3045               else if (ready < 0)
3046                 {
3047                   add_event = 1;
3048                   switch (ready)
3049                     {
3050                     case VPPCOM_ECONNRESET:
3051                       events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
3052                       break;
3053
3054                     default:
3055                       events[num_ev].events |= EPOLLERR;
3056                       break;
3057                     }
3058                 }
3059             }
3060
3061           if ((EPOLLOUT & session_events) && (EPOLLOUT & et_mask))
3062             {
3063               VCL_LOCK_AND_GET_SESSION (sid, &session);
3064               ready = vppcom_session_write_ready (session, sid);
3065               clib_spinlock_unlock (&vcm->sessions_lockp);
3066               if (ready > 0)
3067                 {
3068                   add_event = 1;
3069                   events[num_ev].events |= EPOLLOUT;
3070                   if (EPOLLET & session_events)
3071                     clear_et_mask |= EPOLLOUT;
3072                 }
3073               else if (ready < 0)
3074                 {
3075                   add_event = 1;
3076                   switch (ready)
3077                     {
3078                     case VPPCOM_ECONNRESET:
3079                       events[num_ev].events |= EPOLLHUP;
3080                       break;
3081
3082                     default:
3083                       events[num_ev].events |= EPOLLERR;
3084                       break;
3085                     }
3086                 }
3087             }
3088
3089           if (add_event)
3090             {
3091               events[num_ev].data.u64 = session_ev_data;
3092               if (EPOLLONESHOT & session_events)
3093                 {
3094                   VCL_LOCK_AND_GET_SESSION (sid, &session);
3095                   session->vep.ev.events = 0;
3096                   clib_spinlock_unlock (&vcm->sessions_lockp);
3097                 }
3098               num_ev++;
3099               if (num_ev == maxevents)
3100                 {
3101                   VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3102                   vep_session->wait_cont_idx = next_sid;
3103                   clib_spinlock_unlock (&vcm->sessions_lockp);
3104                   goto done;
3105                 }
3106             }
3107           if (wait_cont_idx != ~0)
3108             {
3109               if (next_sid == ~0)
3110                 next_sid = vep_next_sid;
3111               else if (next_sid == wait_cont_idx)
3112                 next_sid = ~0;
3113             }
3114         }
3115     }
3116   while ((num_ev == 0) && (clib_time_now (&vcm->clib_time) <= timeout));
3117
3118   if (wait_cont_idx != ~0)
3119     {
3120       VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3121       vep_session->wait_cont_idx = ~0;
3122       clib_spinlock_unlock (&vcm->sessions_lockp);
3123     }
3124 done:
3125   return (rv != VPPCOM_OK) ? rv : num_ev;
3126 }
3127
3128 int
3129 vppcom_session_attr (uint32_t session_index, uint32_t op,
3130                      void *buffer, uint32_t * buflen)
3131 {
3132   vppcom_main_t *vcm = &vppcom_main;
3133   session_t *session;
3134   int rv = VPPCOM_OK;
3135   u32 *flags = buffer;
3136   vppcom_endpt_t *ep = buffer;
3137
3138   VCL_LOCK_AND_GET_SESSION (session_index, &session);
3139   switch (op)
3140     {
3141     case VPPCOM_ATTR_GET_NREAD:
3142       rv = vppcom_session_read_ready (session, session_index);
3143       if (VPPCOM_DEBUG > 0)
3144         clib_warning ("VPPCOM_ATTR_GET_NREAD: nread = %d", rv);
3145
3146       break;
3147
3148     case VPPCOM_ATTR_PEEK_NREAD:
3149       /* TBD */
3150       break;
3151
3152     case VPPCOM_ATTR_GET_FLAGS:
3153       if (buffer && buflen && (*buflen >= sizeof (*flags)))
3154         {
3155           *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
3156           *buflen = sizeof (*flags);
3157           if (VPPCOM_DEBUG > 0)
3158             clib_warning ("VPPCOM_ATTR_GET_FLAGS: flags = 0x%08x, "
3159                           "is_nonblocking = %u", *flags,
3160                           session->is_nonblocking);
3161         }
3162       else
3163         rv = VPPCOM_EINVAL;
3164       break;
3165
3166     case VPPCOM_ATTR_SET_FLAGS:
3167       if (buffer && buflen && (*buflen >= sizeof (*flags)))
3168         {
3169           session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
3170           if (VPPCOM_DEBUG > 0)
3171             clib_warning ("VPPCOM_ATTR_SET_FLAGS: flags = 0x%08x, "
3172                           "is_nonblocking = %u", *flags,
3173                           session->is_nonblocking);
3174         }
3175       else
3176         rv = VPPCOM_EINVAL;
3177       break;
3178
3179     case VPPCOM_ATTR_GET_PEER_ADDR:
3180       if (buffer && buflen && (*buflen >= sizeof (*ep)))
3181         {
3182           ep->vrf = session->vrf;
3183           ep->is_ip4 = session->peer_addr.is_ip4;
3184           ep->port = session->port;
3185           if (session->peer_addr.is_ip4)
3186             clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3187                          sizeof (ip4_address_t));
3188           else
3189             clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3190                          sizeof (ip6_address_t));
3191           *buflen = sizeof (*ep);
3192           if (VPPCOM_DEBUG > 0)
3193             clib_warning ("VPPCOM_ATTR_GET_PEER_ADDR: is_ip4 = %u, "
3194                           "addr = %U", ep->is_ip4, format_ip46_address,
3195                           &session->peer_addr.ip46, ep->is_ip4);
3196         }
3197       else
3198         rv = VPPCOM_EINVAL;
3199       break;
3200
3201     case VPPCOM_ATTR_GET_LCL_ADDR:
3202       if (buffer && buflen && (*buflen >= sizeof (*ep)))
3203         {
3204           ep->vrf = session->vrf;
3205           ep->is_ip4 = session->lcl_addr.is_ip4;
3206           ep->port = session->port;
3207           if (session->lcl_addr.is_ip4)
3208             clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3209                          sizeof (ip4_address_t));
3210           else
3211             clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3212                          sizeof (ip6_address_t));
3213           *buflen = sizeof (*ep);
3214           if (VPPCOM_DEBUG > 0)
3215             if (VPPCOM_DEBUG > 0)
3216               clib_warning ("VPPCOM_ATTR_GET_LCL_ADDR: is_ip4 = %u, "
3217                             "addr = %U", ep->is_ip4, format_ip46_address,
3218                             &session->lcl_addr.ip46, ep->is_ip4);
3219         }
3220       else
3221         rv = VPPCOM_EINVAL;
3222       break;
3223
3224     case VPPCOM_ATTR_SET_REUSEADDR:
3225       break;
3226
3227     case VPPCOM_ATTR_SET_BROADCAST:
3228       break;
3229
3230     case VPPCOM_ATTR_SET_V6ONLY:
3231       break;
3232
3233     case VPPCOM_ATTR_SET_KEEPALIVE:
3234       break;
3235
3236     case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3237       break;
3238
3239     case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3240       break;
3241
3242     default:
3243       rv = VPPCOM_EINVAL;
3244       break;
3245     }
3246
3247 done:
3248   clib_spinlock_unlock (&vcm->sessions_lockp);
3249   return rv;
3250 }
3251
3252 /*
3253  * fd.io coding-style-patch-verification: ON
3254  *
3255  * Local Variables:
3256  * eval: (c-set-style "gnu")
3257  * End:
3258  */