udp: refactor udp code
[vpp.git] / src / uri / 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 <uri/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 = ntohs (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;
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)
1928         {
1929           if (session->is_server)
1930             {
1931               rv = vppcom_session_unbind_cut_thru (session);
1932               if ((VPPCOM_DEBUG > 0) && (rv < 0))
1933                 clib_warning ("[%d] unbind cut-thru (session %d) failed, "
1934                               "rv = %s (%d)",
1935                               vcm->my_pid, session_index,
1936                               vppcom_retval_str (rv), rv);
1937             }
1938         }
1939       else if (session->is_server)
1940         {
1941           rv = vppcom_session_unbind (session_index);
1942           if ((VPPCOM_DEBUG > 0) && (rv < 0))
1943             clib_warning ("[%d] unbind (session %d) failed, rv = %s (%d)",
1944                           vcm->my_pid, session_index,
1945                           vppcom_retval_str (rv), rv);
1946         }
1947       else
1948         {
1949           rv = vppcom_session_disconnect (session_index);
1950           if ((VPPCOM_DEBUG > 0) && (rv < 0))
1951             clib_warning ("[%d] disconnect (session %d) failed, rv = %s (%d)",
1952                           vcm->my_pid, session_index,
1953                           vppcom_retval_str (rv), rv);
1954         }
1955     }
1956   pool_put_index (vcm->sessions, session_index);
1957 done:
1958   return rv;
1959 }
1960
1961 int
1962 vppcom_session_bind (uint32_t session_index, vppcom_endpt_t * ep)
1963 {
1964   vppcom_main_t *vcm = &vppcom_main;
1965   session_t *session = 0;
1966   int rv;
1967
1968   if (!ep || !ep->ip)
1969     return VPPCOM_EINVAL;
1970
1971   clib_spinlock_lock (&vcm->sessions_lockp);
1972   rv = vppcom_session_at_index (session_index, &session);
1973   if (PREDICT_FALSE (rv))
1974     {
1975       clib_spinlock_unlock (&vcm->sessions_lockp);
1976       if (VPPCOM_DEBUG > 0)
1977         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
1978                       vcm->my_pid, session_index);
1979       return rv;
1980     }
1981
1982   if (VPPCOM_DEBUG > 0)
1983     clib_warning ("[%d] sid %d", vcm->my_pid, session_index);
1984
1985   session->vrf = ep->vrf;
1986   session->lcl_addr.is_ip4 = ep->is_ip4;
1987   session->lcl_addr.ip46 = to_ip46 (!ep->is_ip4, ep->ip);
1988   session->port = ep->port;
1989
1990   clib_spinlock_unlock (&vcm->sessions_lockp);
1991   return VPPCOM_OK;
1992 }
1993
1994 int
1995 vppcom_session_listen (uint32_t listen_session_index, uint32_t q_len)
1996 {
1997   vppcom_main_t *vcm = &vppcom_main;
1998   session_t *listen_session = 0;
1999   int rv;
2000
2001   clib_spinlock_lock (&vcm->sessions_lockp);
2002   rv = vppcom_session_at_index (listen_session_index, &listen_session);
2003   if (PREDICT_FALSE (rv))
2004     {
2005       clib_spinlock_unlock (&vcm->sessions_lockp);
2006       if (VPPCOM_DEBUG > 0)
2007         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2008                       vcm->my_pid, listen_session_index);
2009       return rv;
2010     }
2011
2012   if (VPPCOM_DEBUG > 0)
2013     clib_warning ("[%d] sid %d", vcm->my_pid, listen_session_index);
2014
2015   ASSERT (vcm->bind_session_index == ~0);
2016   vcm->bind_session_index = listen_session_index;
2017   vppcom_send_bind_sock (listen_session);
2018   clib_spinlock_unlock (&vcm->sessions_lockp);
2019   rv =
2020     vppcom_wait_for_session_state_change (listen_session_index, STATE_LISTEN,
2021                                           vcm->cfg.session_timeout);
2022   if (PREDICT_FALSE (rv))
2023     {
2024       vcm->bind_session_index = ~0;
2025       if (VPPCOM_DEBUG > 0)
2026         clib_warning ("[%d] server listen timed out, rv = %d (%d)",
2027                       vcm->my_pid, vppcom_retval_str (rv), rv);
2028       return rv;
2029     }
2030
2031   clib_spinlock_lock (&vcm->sessions_lockp);
2032   rv = vppcom_session_at_index (listen_session_index, &listen_session);
2033   if (PREDICT_FALSE (rv))
2034     {
2035       clib_spinlock_unlock (&vcm->sessions_lockp);
2036       if (VPPCOM_DEBUG > 0)
2037         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2038                       vcm->my_pid, listen_session_index);
2039       return rv;
2040     }
2041   listen_session->is_listen = 1;
2042   clib_fifo_validate (vcm->client_session_index_fifo, q_len);
2043   clib_spinlock_unlock (&vcm->sessions_lockp);
2044
2045   return VPPCOM_OK;
2046 }
2047
2048 int
2049 vppcom_session_accept (uint32_t listen_session_index, vppcom_endpt_t * ep,
2050                        double wait_for_time)
2051 {
2052   vppcom_main_t *vcm = &vppcom_main;
2053   session_t *listen_session = 0;
2054   session_t *client_session = 0;
2055   u32 client_session_index;
2056   int rv;
2057   f64 wait_for;
2058
2059   clib_spinlock_lock (&vcm->sessions_lockp);
2060   rv = vppcom_session_at_index (listen_session_index, &listen_session);
2061   if (PREDICT_FALSE (rv))
2062     {
2063       clib_spinlock_unlock (&vcm->sessions_lockp);
2064       if (VPPCOM_DEBUG > 0)
2065         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2066                       vcm->my_pid, listen_session_index);
2067       return rv;
2068     }
2069
2070   if (listen_session->state != STATE_LISTEN)
2071     {
2072       clib_spinlock_unlock (&vcm->sessions_lockp);
2073       if (VPPCOM_DEBUG > 0)
2074         clib_warning ("[%d] session not in listen state, state = %s",
2075                       vcm->my_pid,
2076                       vppcom_session_state_str (listen_session->state));
2077       return VPPCOM_EBADFD;
2078     }
2079   wait_for = listen_session->is_nonblocking ? 0 :
2080     (wait_for_time < 0) ? vcm->cfg.accept_timeout : wait_for_time;
2081
2082   if (VPPCOM_DEBUG > 0)
2083     clib_warning ("[%d] sid %d: %s (%d)", vcm->my_pid,
2084                   listen_session_index,
2085                   vppcom_session_state_str (listen_session->state),
2086                   listen_session->state);
2087   clib_spinlock_unlock (&vcm->sessions_lockp);
2088
2089   while (1)
2090     {
2091       rv = vppcom_wait_for_client_session_index (wait_for);
2092       if (rv)
2093         {
2094           if ((VPPCOM_DEBUG > 0))
2095             clib_warning ("[%d] sid %d, accept timed out, rv = %s (%d)",
2096                           vcm->my_pid, listen_session_index,
2097                           vppcom_retval_str (rv), rv);
2098           if ((wait_for == 0) || (wait_for_time > 0))
2099             return rv;
2100         }
2101       else
2102         break;
2103     }
2104
2105   clib_spinlock_lock (&vcm->sessions_lockp);
2106   clib_fifo_sub1 (vcm->client_session_index_fifo, client_session_index);
2107   rv = vppcom_session_at_index (client_session_index, &client_session);
2108   ASSERT (rv == VPPCOM_OK);
2109   ASSERT (client_session->peer_addr.is_ip4 ==
2110           listen_session->lcl_addr.is_ip4);
2111
2112   if (VPPCOM_DEBUG > 0)
2113     clib_warning ("[%d] Got a request: client sid %d", vcm->my_pid,
2114                   client_session_index);
2115
2116   ep->vrf = client_session->vrf;
2117   ep->is_cut_thru = client_session->is_cut_thru;
2118   ep->is_ip4 = client_session->peer_addr.is_ip4;
2119   ep->port = client_session->port;
2120   if (client_session->peer_addr.is_ip4)
2121     clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip4,
2122                  sizeof (ip4_address_t));
2123   else
2124     clib_memcpy (ep->ip, &client_session->peer_addr.ip46.ip6,
2125                  sizeof (ip6_address_t));
2126   clib_spinlock_unlock (&vcm->sessions_lockp);
2127   return (int) client_session_index;
2128 }
2129
2130 int
2131 vppcom_session_connect (uint32_t session_index, vppcom_endpt_t * server_ep)
2132 {
2133   vppcom_main_t *vcm = &vppcom_main;
2134   session_t *session = 0;
2135   int rv;
2136
2137   clib_spinlock_lock (&vcm->sessions_lockp);
2138   rv = vppcom_session_at_index (session_index, &session);
2139   if (PREDICT_FALSE (rv))
2140     {
2141       clib_spinlock_unlock (&vcm->sessions_lockp);
2142       if (VPPCOM_DEBUG > 0)
2143         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2144                       vcm->my_pid, session_index);
2145       return rv;
2146     }
2147
2148   if (session->state == STATE_CONNECT)
2149     {
2150       clib_spinlock_unlock (&vcm->sessions_lockp);
2151       if (VPPCOM_DEBUG > 0)
2152         clib_warning ("[%d] session, sid (%u) already connected!",
2153                       vcm->my_pid, session_index);
2154       return VPPCOM_OK;
2155     }
2156
2157   session->vrf = server_ep->vrf;
2158   session->peer_addr.is_ip4 = server_ep->is_ip4;
2159   session->peer_addr.ip46 = to_ip46 (!server_ep->is_ip4, server_ep->ip);
2160   session->port = server_ep->port;
2161
2162   if (VPPCOM_DEBUG > 0)
2163     {
2164       u8 *ip_str = format (0, "%U", format_ip46_address,
2165                            &session->peer_addr.ip46,
2166                            session->peer_addr.is_ip4);
2167       clib_warning ("[%d] connect sid %d to %s server port %d",
2168                     vcm->my_pid, session_index, ip_str,
2169                     clib_net_to_host_u16 (session->port));
2170       vec_free (ip_str);
2171     }
2172
2173   vppcom_send_connect_sock (session, session_index);
2174   clib_spinlock_unlock (&vcm->sessions_lockp);
2175   rv = vppcom_wait_for_session_state_change (session_index, STATE_CONNECT,
2176                                              vcm->cfg.session_timeout);
2177   if (PREDICT_FALSE (rv))
2178     {
2179       if (VPPCOM_DEBUG > 0)
2180         clib_warning ("[%d] connect timed out, rv = %s (%d)",
2181                       vcm->my_pid, vppcom_retval_str (rv), rv);
2182       return rv;
2183     }
2184   return VPPCOM_OK;
2185 }
2186
2187 int
2188 vppcom_session_read (uint32_t session_index, void *buf, int n)
2189 {
2190   vppcom_main_t *vcm = &vppcom_main;
2191   session_t *session = 0;
2192   svm_fifo_t *rx_fifo;
2193   int n_read = 0;
2194   int rv;
2195   char *fifo_str;
2196
2197   ASSERT (buf);
2198
2199   clib_spinlock_lock (&vcm->sessions_lockp);
2200   rv = vppcom_session_at_index (session_index, &session);
2201   if (PREDICT_FALSE (rv))
2202     {
2203       clib_spinlock_unlock (&vcm->sessions_lockp);
2204       if (VPPCOM_DEBUG > 0)
2205         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2206                       vcm->my_pid, session_index);
2207       return rv;
2208     }
2209
2210   if (session->state == STATE_DISCONNECT)
2211     {
2212       clib_spinlock_unlock (&vcm->sessions_lockp);
2213       if (VPPCOM_DEBUG > 0)
2214         clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2215                       vcm->my_pid, session_index);
2216       return VPPCOM_ECONNRESET;
2217     }
2218
2219   rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2220              session->server_rx_fifo : session->server_tx_fifo);
2221   fifo_str = ((!session->is_cut_thru || session->is_server) ?
2222               "server_rx_fifo" : "server_tx_fifo");
2223
2224   do
2225     {
2226       n_read = svm_fifo_dequeue_nowait (rx_fifo, n, buf);
2227     }
2228   while (!session->is_nonblocking && (n_read <= 0));
2229
2230   if (n_read <= 0)
2231     session->vep.et_mask |= EPOLLIN;
2232
2233   clib_spinlock_unlock (&vcm->sessions_lockp);
2234
2235   if ((VPPCOM_DEBUG > 2) && (n_read > 0))
2236     clib_warning ("[%d] sid %d, read %d bytes from %s (%p)", vcm->my_pid,
2237                   session_index, n_read, fifo_str, rx_fifo);
2238
2239   return (n_read <= 0) ? VPPCOM_EAGAIN : n_read;
2240 }
2241
2242 static inline int
2243 vppcom_session_read_ready (session_t * session, u32 session_index)
2244 {
2245   vppcom_main_t *vcm = &vppcom_main;
2246   svm_fifo_t *rx_fifo;
2247   int ready = 0;
2248
2249   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2250   if (session->state == STATE_DISCONNECT)
2251     {
2252       if (VPPCOM_DEBUG > 0)
2253         clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2254                       vcm->my_pid, session_index);
2255       return VPPCOM_ECONNRESET;
2256     }
2257
2258   if (session->is_listen)
2259     ready = clib_fifo_elts (vcm->client_session_index_fifo);
2260   else
2261     {
2262       rx_fifo = ((!session->is_cut_thru || session->is_server) ?
2263                  session->server_rx_fifo : session->server_tx_fifo);
2264
2265       ready = svm_fifo_max_dequeue (rx_fifo);
2266     }
2267
2268   if (VPPCOM_DEBUG > 3)
2269     clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", vcm->my_pid,
2270                   session_index,
2271                   session->is_server ? "server_rx_fifo" : "server_tx_fifo",
2272                   rx_fifo, ready);
2273   if (ready == 0)
2274     session->vep.et_mask |= EPOLLIN;
2275
2276   return ready;
2277 }
2278
2279 int
2280 vppcom_session_write (uint32_t session_index, void *buf, int n)
2281 {
2282   vppcom_main_t *vcm = &vppcom_main;
2283   session_t *session = 0;
2284   svm_fifo_t *tx_fifo;
2285   unix_shared_memory_queue_t *q;
2286   session_fifo_event_t evt;
2287   int rv, n_write;
2288   char *fifo_str;
2289
2290   ASSERT (buf);
2291
2292   clib_spinlock_lock (&vcm->sessions_lockp);
2293   rv = vppcom_session_at_index (session_index, &session);
2294   if (PREDICT_FALSE (rv))
2295     {
2296       clib_spinlock_unlock (&vcm->sessions_lockp);
2297       if (VPPCOM_DEBUG > 0)
2298         clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2299                       vcm->my_pid, session_index);
2300       return rv;
2301     }
2302
2303   if (session->state == STATE_DISCONNECT)
2304     {
2305       clib_spinlock_unlock (&vcm->sessions_lockp);
2306       if (VPPCOM_DEBUG > 0)
2307         clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2308                       vcm->my_pid, session_index);
2309       return VPPCOM_ECONNRESET;
2310     }
2311
2312   tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2313              session->server_tx_fifo : session->server_rx_fifo);
2314   fifo_str = ((!session->is_cut_thru || session->is_server) ?
2315               "server_tx_fifo" : "server_rx_fifo");
2316   do
2317     {
2318       n_write = svm_fifo_enqueue_nowait (tx_fifo, n, buf);
2319     }
2320   while (!session->is_nonblocking && (n_write <= 0));
2321
2322   /* If event wasn't set, add one */
2323   if (!session->is_cut_thru && (n_write > 0) && svm_fifo_set_event (tx_fifo))
2324     {
2325       int rval;
2326
2327       /* Fabricate TX event, send to vpp */
2328       evt.fifo = tx_fifo;
2329       evt.event_type = FIFO_EVENT_APP_TX;
2330
2331       rval = vppcom_session_at_index (session_index, &session);
2332       if (PREDICT_FALSE (rval))
2333         {
2334           clib_spinlock_unlock (&vcm->sessions_lockp);
2335           if (VPPCOM_DEBUG > 1)
2336             clib_warning ("[%d] invalid session, sid (%u) has been closed!",
2337                           vcm->my_pid, session_index);
2338           return rval;
2339         }
2340       q = session->vpp_event_queue;
2341       ASSERT (q);
2342       unix_shared_memory_queue_add (q, (u8 *) & evt,
2343                                     0 /* do wait for mutex */ );
2344     }
2345
2346   if (n_write <= 0)
2347     session->vep.et_mask |= EPOLLOUT;
2348
2349   clib_spinlock_unlock (&vcm->sessions_lockp);
2350
2351   if (VPPCOM_DEBUG > 2)
2352     clib_warning ("[%d] sid %d, wrote %d bytes to %s (%p)", vcm->my_pid,
2353                   session_index, n_write, fifo_str, tx_fifo);
2354   return n_write;
2355 }
2356
2357 static inline int
2358 vppcom_session_write_ready (session_t * session, u32 session_index)
2359 {
2360   vppcom_main_t *vcm = &vppcom_main;
2361   svm_fifo_t *tx_fifo;
2362   char *fifo_str;
2363   int ready;
2364
2365   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2366   if (session->state == STATE_DISCONNECT)
2367     {
2368       if (VPPCOM_DEBUG > 0)
2369         clib_warning ("[%d] sid (%u) has been closed by remote peer!",
2370                       vcm->my_pid, session_index);
2371       return VPPCOM_ECONNRESET;
2372     }
2373
2374   tx_fifo = ((!session->is_cut_thru || session->is_server) ?
2375              session->server_tx_fifo : session->server_rx_fifo);
2376   fifo_str = ((!session->is_cut_thru || session->is_server) ?
2377               "server_tx_fifo" : "server_rx_fifo");
2378
2379   ready = svm_fifo_max_enqueue (tx_fifo);
2380
2381   if (VPPCOM_DEBUG > 3)
2382     clib_warning ("[%d] sid %d, peek %s (%p), ready = %d", vcm->my_pid,
2383                   session_index, fifo_str, tx_fifo, ready);
2384   if (ready == 0)
2385     session->vep.et_mask |= EPOLLOUT;
2386
2387   return ready;
2388 }
2389
2390 int
2391 vppcom_select (unsigned long n_bits, unsigned long *read_map,
2392                unsigned long *write_map, unsigned long *except_map,
2393                double time_to_wait)
2394 {
2395   vppcom_main_t *vcm = &vppcom_main;
2396   u32 session_index;
2397   session_t *session = 0;
2398   int rv, bits_set = 0;
2399   f64 timeout = clib_time_now (&vcm->clib_time) + time_to_wait;
2400   u32 minbits = clib_max (n_bits, BITS (uword));
2401
2402   ASSERT (sizeof (clib_bitmap_t) == sizeof (long int));
2403
2404   if (read_map)
2405     {
2406       clib_bitmap_validate (vcm->rd_bitmap, minbits);
2407       clib_memcpy (vcm->rd_bitmap, read_map, vec_len (vcm->rd_bitmap));
2408       memset (read_map, 0, vec_len (vcm->rd_bitmap));
2409     }
2410   if (write_map)
2411     {
2412       clib_bitmap_validate (vcm->wr_bitmap, minbits);
2413       clib_memcpy (vcm->wr_bitmap, write_map, vec_len (vcm->wr_bitmap));
2414       memset (write_map, 0, vec_len (vcm->wr_bitmap));
2415     }
2416   if (except_map)
2417     {
2418       clib_bitmap_validate (vcm->ex_bitmap, minbits);
2419       clib_memcpy (vcm->ex_bitmap, except_map, vec_len (vcm->ex_bitmap));
2420       memset (except_map, 0, vec_len (vcm->ex_bitmap));
2421     }
2422
2423   do
2424     {
2425       /* *INDENT-OFF* */
2426       clib_bitmap_foreach (session_index, vcm->rd_bitmap,
2427         ({
2428           clib_spinlock_lock (&vcm->sessions_lockp);
2429           rv = vppcom_session_at_index (session_index, &session);
2430           if (rv < 0)
2431             {
2432               clib_spinlock_unlock (&vcm->sessions_lockp);
2433               if (VPPCOM_DEBUG > 1)
2434                 clib_warning ("[%d] session %d specified in "
2435                               "read_map is closed.", vcm->my_pid,
2436                               session_index);
2437               bits_set = VPPCOM_EBADFD;
2438               goto select_done;
2439             }
2440
2441           rv = vppcom_session_read_ready (session, session_index);
2442           clib_spinlock_unlock (&vcm->sessions_lockp);
2443           if (vcm->ex_bitmap &&
2444               clib_bitmap_get (vcm->ex_bitmap, session_index) && (rv < 0))
2445             {
2446               // TBD: clib_warning
2447               /* coverity[FORWARD_NULL] */
2448               clib_bitmap_set_no_check (except_map, session_index, 1);
2449               bits_set++;
2450             }
2451           else if (rv > 0)
2452             {
2453               // TBD: clib_warning
2454               /* coverity[FORWARD_NULL] */
2455               clib_bitmap_set_no_check (read_map, session_index, 1);
2456               bits_set++;
2457             }
2458         }));
2459
2460       clib_bitmap_foreach (session_index, vcm->wr_bitmap,
2461         ({
2462           clib_spinlock_lock (&vcm->sessions_lockp);
2463           rv = vppcom_session_at_index (session_index, &session);
2464           if (rv < 0)
2465             {
2466               clib_spinlock_unlock (&vcm->sessions_lockp);
2467               if (VPPCOM_DEBUG > 0)
2468                 clib_warning ("[%d] session %d specified in "
2469                               "write_map is closed.", vcm->my_pid,
2470                               session_index);
2471               bits_set = VPPCOM_EBADFD;
2472               goto select_done;
2473             }
2474
2475           rv = vppcom_session_write_ready (session, session_index);
2476           clib_spinlock_unlock (&vcm->sessions_lockp);
2477           if (rv > 0 )
2478             {
2479               // TBD: clib_warning
2480               /* coverity[FORWARD_NULL] */
2481               clib_bitmap_set_no_check (write_map, session_index, 1);
2482               bits_set++;
2483             }
2484         }));
2485
2486       clib_bitmap_foreach (session_index, vcm->ex_bitmap,
2487         ({
2488           clib_spinlock_lock (&vcm->sessions_lockp);
2489           rv = vppcom_session_at_index (session_index, &session);
2490           if (rv < 0)
2491             {
2492               clib_spinlock_unlock (&vcm->sessions_lockp);
2493               if (VPPCOM_DEBUG > 1)
2494                 clib_warning ("[%d] session %d specified in "
2495                               "except_map is closed.", vcm->my_pid,
2496                               session_index);
2497               bits_set = VPPCOM_EBADFD;
2498               goto select_done;
2499             }
2500
2501           rv = vppcom_session_read_ready (session, session_index);
2502           clib_spinlock_unlock (&vcm->sessions_lockp);
2503           if (rv < 0)
2504             {
2505               // TBD: clib_warning
2506               /* coverity[FORWARD_NULL] */
2507               clib_bitmap_set_no_check (except_map, session_index, 1);
2508               bits_set++;
2509             }
2510         }));
2511       /* *INDENT-ON* */
2512     }
2513   while (clib_time_now (&vcm->clib_time) < timeout);
2514
2515 select_done:
2516   return (bits_set);
2517 }
2518
2519 static inline void
2520 vep_verify_epoll_chain (u32 vep_idx)
2521 {
2522   session_t *session;
2523   vppcom_epoll_t *vep;
2524   int rv;
2525   u32 sid;
2526
2527   if (VPPCOM_DEBUG < 1)
2528     return;
2529
2530   /* Assumes caller has acquired spinlock: vcm->sessions_lockp */
2531   rv = vppcom_session_at_index (vep_idx, &session);
2532   if (PREDICT_FALSE (rv))
2533     {
2534       clib_warning ("ERROR: Invalid vep_idx (%u)!", vep_idx);
2535       goto done;
2536     }
2537   if (PREDICT_FALSE (!session->is_vep))
2538     {
2539       clib_warning ("ERROR: vep_idx (%u) is not a vep!", vep_idx);
2540       goto done;
2541     }
2542   if (VPPCOM_DEBUG > 1)
2543     clib_warning ("vep_idx (%u): Dumping epoll chain\n"
2544                   "{\n"
2545                   "   is_vep         = %u\n"
2546                   "   is_vep_session = %u\n"
2547                   "   wait_cont_idx  = 0x%x (%u)\n"
2548                   "}\n",
2549                   vep_idx, session->is_vep, session->is_vep_session,
2550                   session->wait_cont_idx, session->wait_cont_idx);
2551   do
2552     {
2553       vep = &session->vep;
2554       if (session->is_vep_session)
2555         {
2556           if (VPPCOM_DEBUG > 1)
2557             clib_warning ("vep_idx[%u]: sid 0x%x (%u)\n"
2558                           "{\n"
2559                           "   next_sid       = 0x%x (%u)\n"
2560                           "   prev_sid       = 0x%x (%u)\n"
2561                           "   vep_idx        = 0x%x (%u)\n"
2562                           "   ev.events      = 0x%x\n"
2563                           "   ev.data.u64    = 0x%llx\n"
2564                           "   et_mask        = 0x%x\n"
2565                           "}\n",
2566                           vep_idx, sid, sid,
2567                           vep->next_sid, vep->next_sid,
2568                           vep->prev_sid, vep->prev_sid,
2569                           vep->vep_idx, vep->vep_idx,
2570                           vep->ev.events, vep->ev.data.u64, vep->et_mask);
2571         }
2572       sid = vep->next_sid;
2573       if (sid != ~0)
2574         {
2575           rv = vppcom_session_at_index (sid, &session);
2576           if (PREDICT_FALSE (rv))
2577             {
2578               clib_warning ("ERROR: Invalid sid (%u)!", sid);
2579               goto done;
2580             }
2581           if (PREDICT_FALSE (session->is_vep))
2582             clib_warning ("ERROR: sid (%u) is a vep!", vep_idx);
2583           else if (PREDICT_FALSE (!session->is_vep_session))
2584             {
2585               clib_warning ("ERROR: session (%u) is not a vep session!", sid);
2586               goto done;
2587             }
2588           if (PREDICT_FALSE (session->vep.vep_idx != vep_idx))
2589             clib_warning ("ERROR: session (%u) vep_idx (%u) != "
2590                           "vep_idx (%u)!",
2591                           sid, session->vep.vep_idx, vep_idx);
2592         }
2593     }
2594   while (sid != ~0);
2595
2596 done:
2597   if (VPPCOM_DEBUG > 1)
2598     clib_warning ("vep_idx (%u): Dump complete!", vep_idx);
2599 }
2600
2601 int
2602 vppcom_epoll_create (void)
2603 {
2604   vppcom_main_t *vcm = &vppcom_main;
2605   session_t *vep_session;
2606   u32 vep_idx;
2607
2608   clib_spinlock_lock (&vcm->sessions_lockp);
2609   pool_get (vcm->sessions, vep_session);
2610   memset (vep_session, 0, sizeof (*vep_session));
2611   vep_idx = vep_session - vcm->sessions;
2612
2613   vep_session->is_vep = 1;
2614   vep_session->vep.vep_idx = ~0;
2615   vep_session->vep.next_sid = ~0;
2616   vep_session->vep.prev_sid = ~0;
2617   vep_session->wait_cont_idx = ~0;
2618   clib_spinlock_unlock (&vcm->sessions_lockp);
2619
2620   if (VPPCOM_DEBUG > 0)
2621     clib_warning ("Created vep_idx %u!", vep_idx);
2622
2623   return (vep_idx);
2624 }
2625
2626 int
2627 vppcom_epoll_ctl (uint32_t vep_idx, int op, uint32_t session_index,
2628                   struct epoll_event *event)
2629 {
2630   vppcom_main_t *vcm = &vppcom_main;
2631   session_t *vep_session;
2632   session_t *session;
2633   int rv;
2634
2635   if (vep_idx == session_index)
2636     {
2637       if (VPPCOM_DEBUG > 0)
2638         clib_warning ("ERROR: vep_idx == session_index (%u)!", vep_idx);
2639       return VPPCOM_EINVAL;
2640     }
2641
2642   clib_spinlock_lock (&vcm->sessions_lockp);
2643   rv = vppcom_session_at_index (vep_idx, &vep_session);
2644   if (PREDICT_FALSE (rv))
2645     {
2646       if (VPPCOM_DEBUG > 0)
2647         clib_warning ("ERROR: Invalid vep_idx (%u)!", vep_idx);
2648       goto done;
2649     }
2650   if (PREDICT_FALSE (!vep_session->is_vep))
2651     {
2652       if (VPPCOM_DEBUG > 0)
2653         clib_warning ("ERROR: vep_idx (%u) is not a vep!", vep_idx);
2654       rv = VPPCOM_EINVAL;
2655       goto done;
2656     }
2657
2658   ASSERT (vep_session->vep.vep_idx == ~0);
2659   ASSERT (vep_session->vep.prev_sid == ~0);
2660
2661   rv = vppcom_session_at_index (session_index, &session);
2662   if (PREDICT_FALSE (rv))
2663     {
2664       if (VPPCOM_DEBUG > 0)
2665         clib_warning ("ERROR: Invalid session_index (%u)!", session_index);
2666       goto done;
2667     }
2668   if (PREDICT_FALSE (session->is_vep))
2669     {
2670       if (VPPCOM_DEBUG > 0)
2671         clib_warning ("ERROR: session_index (%u) is a vep!", vep_idx);
2672       rv = VPPCOM_EINVAL;
2673       goto done;
2674     }
2675
2676   switch (op)
2677     {
2678     case EPOLL_CTL_ADD:
2679       if (PREDICT_FALSE (!event))
2680         {
2681           clib_warning ("NULL pointer to epoll_event structure!");
2682           rv = VPPCOM_EINVAL;
2683           goto done;
2684         }
2685       if (vep_session->vep.next_sid != ~0)
2686         {
2687           session_t *next_session;
2688           rv = vppcom_session_at_index (vep_session->vep.next_sid,
2689                                         &next_session);
2690           if (PREDICT_FALSE (rv))
2691             {
2692               if (VPPCOM_DEBUG > 0)
2693                 clib_warning ("EPOLL_CTL_ADD: Invalid vep.next_sid (%u) on"
2694                               " vep_idx (%u)!", vep_session->vep.next_sid,
2695                               vep_idx);
2696               goto done;
2697             }
2698           ASSERT (next_session->vep.prev_sid == vep_idx);
2699           next_session->vep.prev_sid = session_index;
2700         }
2701       session->vep.next_sid = vep_session->vep.next_sid;
2702       session->vep.prev_sid = vep_idx;
2703       session->vep.vep_idx = vep_idx;
2704       session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2705       session->vep.ev = *event;
2706       session->is_vep_session = 1;
2707       vep_session->vep.next_sid = session_index;
2708       if (VPPCOM_DEBUG > 1)
2709         clib_warning ("EPOLL_CTL_ADD: vep_idx %u, sid %u, events 0x%x,"
2710                       " data 0x%llx!", vep_idx, session_index,
2711                       event->events, event->data.u64);
2712       break;
2713
2714     case EPOLL_CTL_MOD:
2715       if (PREDICT_FALSE (!event))
2716         {
2717           clib_warning ("NULL pointer to epoll_event structure!");
2718           rv = VPPCOM_EINVAL;
2719           goto done;
2720         }
2721       if (PREDICT_FALSE (!session->is_vep_session &&
2722                          (session->vep.vep_idx != vep_idx)))
2723         {
2724           if (VPPCOM_DEBUG > 0)
2725             {
2726               if (!session->is_vep_session)
2727                 clib_warning ("EPOLL_CTL_MOD: session (%u) is not "
2728                               "a vep session!", session_index);
2729               else
2730                 clib_warning ("EPOLL_CTL_MOD: session (%u) vep_idx (%u) != "
2731                               "vep_idx (%u)!", session_index,
2732                               session->vep.vep_idx, vep_idx);
2733             }
2734           rv = VPPCOM_EINVAL;
2735           goto done;
2736         }
2737       session->vep.et_mask = VEP_DEFAULT_ET_MASK;
2738       session->vep.ev = *event;
2739       if (VPPCOM_DEBUG > 1)
2740         clib_warning ("EPOLL_CTL_MOD: vep_idx %u, sid %u, events 0x%x,"
2741                       " data 0x%llx!", vep_idx, session_index,
2742                       event->events, event->data.u64);
2743       break;
2744
2745     case EPOLL_CTL_DEL:
2746       if (PREDICT_FALSE (!session->is_vep_session &&
2747                          (session->vep.vep_idx != vep_idx)))
2748         {
2749           if (VPPCOM_DEBUG > 0)
2750             {
2751               if (!session->is_vep_session)
2752                 clib_warning ("EPOLL_CTL_DEL: session (%u) is not "
2753                               "a vep session!", session_index);
2754               else
2755                 clib_warning ("EPOLL_CTL_DEL: session (%u) vep_idx (%u) != "
2756                               "vep_idx (%u)!", session_index,
2757                               session->vep.vep_idx, vep_idx);
2758             }
2759           rv = VPPCOM_EINVAL;
2760           goto done;
2761         }
2762
2763       vep_session->wait_cont_idx =
2764         (vep_session->wait_cont_idx == session_index) ?
2765         session->vep.next_sid : vep_session->wait_cont_idx;
2766
2767       if (session->vep.prev_sid == vep_idx)
2768         vep_session->vep.next_sid = session->vep.next_sid;
2769       else
2770         {
2771           session_t *prev_session;
2772           rv = vppcom_session_at_index (session->vep.prev_sid, &prev_session);
2773           if (PREDICT_FALSE (rv))
2774             {
2775               if (VPPCOM_DEBUG > 0)
2776                 clib_warning ("EPOLL_CTL_DEL: Invalid vep.prev_sid (%u) on"
2777                               " sid (%u)!", session->vep.prev_sid,
2778                               session_index);
2779               goto done;
2780             }
2781           ASSERT (prev_session->vep.next_sid == session_index);
2782           prev_session->vep.next_sid = session->vep.next_sid;
2783         }
2784       if (session->vep.next_sid != ~0)
2785         {
2786           session_t *next_session;
2787           rv = vppcom_session_at_index (session->vep.next_sid, &next_session);
2788           if (PREDICT_FALSE (rv))
2789             {
2790               if (VPPCOM_DEBUG > 0)
2791                 clib_warning ("EPOLL_CTL_DEL: Invalid vep.next_sid (%u) on"
2792                               " sid (%u)!", session->vep.next_sid,
2793                               session_index);
2794               goto done;
2795             }
2796           ASSERT (next_session->vep.prev_sid == session_index);
2797           next_session->vep.prev_sid = session->vep.prev_sid;
2798         }
2799
2800       memset (&session->vep, 0, sizeof (session->vep));
2801       session->vep.next_sid = ~0;
2802       session->vep.prev_sid = ~0;
2803       session->vep.vep_idx = ~0;
2804       session->is_vep_session = 0;
2805       if (VPPCOM_DEBUG > 1)
2806         clib_warning ("EPOLL_CTL_DEL: vep_idx %u, sid %u!", vep_idx,
2807                       session_index);
2808       break;
2809
2810     default:
2811       clib_warning ("Invalid operation (%d)!", op);
2812       rv = VPPCOM_EINVAL;
2813     }
2814
2815   vep_verify_epoll_chain (vep_idx);
2816
2817 done:
2818   clib_spinlock_unlock (&vcm->sessions_lockp);
2819   return rv;
2820 }
2821
2822 #define VCL_LOCK_AND_GET_SESSION(I, S)                  \
2823 do {                                                    \
2824   vppcom_main_t *vcm = &vppcom_main;                    \
2825                                                         \
2826   clib_spinlock_lock (&vcm->sessions_lockp);            \
2827   rv = vppcom_session_at_index (I, S);                  \
2828   if (PREDICT_FALSE (rv))                               \
2829     {                                                   \
2830       clib_spinlock_unlock (&vcm->sessions_lockp);      \
2831                                                         \
2832       if (VPPCOM_DEBUG > 0)                             \
2833         clib_warning ("ERROR: Invalid ##I (%u)!", I);   \
2834                                                         \
2835       goto done;                                        \
2836     }                                                   \
2837 } while (0)
2838
2839 int
2840 vppcom_epoll_wait (uint32_t vep_idx, struct epoll_event *events,
2841                    int maxevents, double wait_for_time)
2842 {
2843   vppcom_main_t *vcm = &vppcom_main;
2844   session_t *vep_session;
2845   int rv;
2846   f64 timeout = clib_time_now (&vcm->clib_time) + wait_for_time;
2847   int num_ev = 0;
2848   u32 vep_next_sid, wait_cont_idx;
2849   u8 is_vep;
2850
2851   if (PREDICT_FALSE (maxevents <= 0))
2852     {
2853       if (VPPCOM_DEBUG > 0)
2854         clib_warning ("ERROR: Invalid maxevents (%d)!", maxevents);
2855       return VPPCOM_EINVAL;
2856     }
2857   if (PREDICT_FALSE (wait_for_time < 0))
2858     {
2859       if (VPPCOM_DEBUG > 0)
2860         clib_warning ("ERROR: Invalid wait_for_time (%f)!", wait_for_time);
2861       return VPPCOM_EINVAL;
2862     }
2863   memset (events, 0, sizeof (*events) * maxevents);
2864
2865   VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
2866   vep_next_sid = vep_session->vep.next_sid;
2867   is_vep = vep_session->is_vep;
2868   wait_cont_idx = vep_session->wait_cont_idx;
2869   clib_spinlock_unlock (&vcm->sessions_lockp);
2870
2871   if (PREDICT_FALSE (!is_vep))
2872     {
2873       if (VPPCOM_DEBUG > 0)
2874         clib_warning ("ERROR: vep_idx (%u) is not a vep!", vep_idx);
2875       rv = VPPCOM_EINVAL;
2876       goto done;
2877     }
2878   if ((VPPCOM_DEBUG > 0) && (PREDICT_FALSE (vep_next_sid == ~0)))
2879     {
2880       clib_warning ("WARNING: vep_idx (%u) is empty!", vep_idx);
2881       goto done;
2882     }
2883
2884   do
2885     {
2886       u32 sid;
2887       u32 next_sid = ~0;
2888       session_t *session;
2889
2890       for (sid = (wait_cont_idx == ~0) ? vep_next_sid : wait_cont_idx;
2891            sid != ~0; sid = next_sid)
2892         {
2893           u32 session_events, et_mask, clear_et_mask, session_vep_idx;
2894           u8 add_event, is_vep_session;
2895           int ready;
2896           u64 session_ev_data;
2897
2898           VCL_LOCK_AND_GET_SESSION (sid, &session);
2899           next_sid = session->vep.next_sid;
2900           session_events = session->vep.ev.events;
2901           et_mask = session->vep.et_mask;
2902           is_vep = session->is_vep;
2903           is_vep_session = session->is_vep_session;
2904           session_vep_idx = session->vep.vep_idx;
2905           session_ev_data = session->vep.ev.data.u64;
2906           clib_spinlock_unlock (&vcm->sessions_lockp);
2907
2908           if (PREDICT_FALSE (is_vep))
2909             {
2910               if (VPPCOM_DEBUG > 0)
2911                 clib_warning ("ERROR: sid (%u) is a vep!", vep_idx);
2912               rv = VPPCOM_EINVAL;
2913               goto done;
2914             }
2915           if (PREDICT_FALSE (!is_vep_session))
2916             {
2917               if (VPPCOM_DEBUG > 0)
2918                 clib_warning ("EPOLL_CTL_MOD: session (%u) is not "
2919                               "a vep session!", sid);
2920               rv = VPPCOM_EINVAL;
2921               goto done;
2922             }
2923           if (PREDICT_FALSE (session_vep_idx != vep_idx))
2924             {
2925               clib_warning ("EPOLL_CTL_MOD: session (%u) "
2926                             "vep_idx (%u) != vep_idx (%u)!",
2927                             sid, session->vep.vep_idx, vep_idx);
2928               rv = VPPCOM_EINVAL;
2929               goto done;
2930             }
2931
2932           add_event = clear_et_mask = 0;
2933
2934           if ((EPOLLIN & session_events) && (EPOLLIN & et_mask))
2935             {
2936               VCL_LOCK_AND_GET_SESSION (sid, &session);
2937               ready = vppcom_session_read_ready (session, sid);
2938               clib_spinlock_unlock (&vcm->sessions_lockp);
2939               if (ready > 0)
2940                 {
2941                   add_event = 1;
2942                   events[num_ev].events |= EPOLLIN;
2943                   if (EPOLLET & session_events)
2944                     clear_et_mask |= EPOLLIN;
2945                 }
2946               else if (ready < 0)
2947                 {
2948                   add_event = 1;
2949                   switch (ready)
2950                     {
2951                     case VPPCOM_ECONNRESET:
2952                       events[num_ev].events |= EPOLLHUP | EPOLLRDHUP;
2953                       break;
2954
2955                     default:
2956                       events[num_ev].events |= EPOLLERR;
2957                       break;
2958                     }
2959                 }
2960             }
2961
2962           if ((EPOLLOUT & session_events) && (EPOLLOUT & et_mask))
2963             {
2964               VCL_LOCK_AND_GET_SESSION (sid, &session);
2965               ready = vppcom_session_write_ready (session, sid);
2966               clib_spinlock_unlock (&vcm->sessions_lockp);
2967               if (ready > 0)
2968                 {
2969                   add_event = 1;
2970                   events[num_ev].events |= EPOLLOUT;
2971                   if (EPOLLET & session_events)
2972                     clear_et_mask |= EPOLLOUT;
2973                 }
2974               else if (ready < 0)
2975                 {
2976                   add_event = 1;
2977                   switch (ready)
2978                     {
2979                     case VPPCOM_ECONNRESET:
2980                       events[num_ev].events |= EPOLLHUP;
2981                       break;
2982
2983                     default:
2984                       events[num_ev].events |= EPOLLERR;
2985                       break;
2986                     }
2987                 }
2988             }
2989
2990           if (add_event)
2991             {
2992               events[num_ev].data.u64 = session_ev_data;
2993               if (EPOLLONESHOT & session_events)
2994                 {
2995                   VCL_LOCK_AND_GET_SESSION (sid, &session);
2996                   session->vep.ev.events = 0;
2997                   clib_spinlock_unlock (&vcm->sessions_lockp);
2998                 }
2999               num_ev++;
3000               if (num_ev == maxevents)
3001                 {
3002                   VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3003                   vep_session->wait_cont_idx = next_sid;
3004                   clib_spinlock_unlock (&vcm->sessions_lockp);
3005                   goto done;
3006                 }
3007             }
3008           if (wait_cont_idx != ~0)
3009             {
3010               if (next_sid == ~0)
3011                 next_sid = vep_next_sid;
3012               else if (next_sid == wait_cont_idx)
3013                 next_sid = ~0;
3014             }
3015         }
3016     }
3017   while ((num_ev == 0) && (clib_time_now (&vcm->clib_time) <= timeout));
3018
3019   if (wait_cont_idx != ~0)
3020     {
3021       VCL_LOCK_AND_GET_SESSION (vep_idx, &vep_session);
3022       vep_session->wait_cont_idx = ~0;
3023       clib_spinlock_unlock (&vcm->sessions_lockp);
3024     }
3025 done:
3026   return (rv != VPPCOM_OK) ? rv : num_ev;
3027 }
3028
3029 int
3030 vppcom_session_attr (uint32_t session_index, uint32_t op,
3031                      void *buffer, uint32_t * buflen)
3032 {
3033   vppcom_main_t *vcm = &vppcom_main;
3034   session_t *session;
3035   int rv = VPPCOM_OK;
3036   u32 *flags = buffer;
3037   vppcom_endpt_t *ep = buffer;
3038
3039   VCL_LOCK_AND_GET_SESSION (session_index, &session);
3040   switch (op)
3041     {
3042     case VPPCOM_ATTR_GET_NREAD:
3043       rv = vppcom_session_read_ready (session, session_index);
3044       if (VPPCOM_DEBUG > 0)
3045         clib_warning ("VPPCOM_ATTR_GET_NREAD: nread = %d", rv);
3046
3047       break;
3048
3049     case VPPCOM_ATTR_PEEK_NREAD:
3050       /* TBD */
3051       break;
3052
3053     case VPPCOM_ATTR_GET_FLAGS:
3054       if (buffer && buflen && (*buflen >= sizeof (*flags)))
3055         {
3056           *flags = O_RDWR | ((session->is_nonblocking) ? O_NONBLOCK : 0);
3057           *buflen = sizeof (*flags);
3058           if (VPPCOM_DEBUG > 0)
3059             clib_warning ("VPPCOM_ATTR_GET_FLAGS: flags = 0x%08x, "
3060                           "is_nonblocking = %u", *flags,
3061                           session->is_nonblocking);
3062         }
3063       else
3064         rv = VPPCOM_EINVAL;
3065       break;
3066
3067     case VPPCOM_ATTR_SET_FLAGS:
3068       if (buffer && buflen && (*buflen >= sizeof (*flags)))
3069         {
3070           session->is_nonblocking = (*flags & O_NONBLOCK) ? 1 : 0;
3071           if (VPPCOM_DEBUG > 0)
3072             clib_warning ("VPPCOM_ATTR_SET_FLAGS: flags = 0x%08x, "
3073                           "is_nonblocking = %u", *flags,
3074                           session->is_nonblocking);
3075         }
3076       else
3077         rv = VPPCOM_EINVAL;
3078       break;
3079
3080     case VPPCOM_ATTR_GET_PEER_ADDR:
3081       if (buffer && buflen && (*buflen >= sizeof (*ep)))
3082         {
3083           ep->vrf = session->vrf;
3084           ep->is_ip4 = session->peer_addr.is_ip4;
3085           ep->port = session->port;
3086           if (session->peer_addr.is_ip4)
3087             clib_memcpy (ep->ip, &session->peer_addr.ip46.ip4,
3088                          sizeof (ip4_address_t));
3089           else
3090             clib_memcpy (ep->ip, &session->peer_addr.ip46.ip6,
3091                          sizeof (ip6_address_t));
3092           *buflen = sizeof (*ep);
3093           if (VPPCOM_DEBUG > 0)
3094             clib_warning ("VPPCOM_ATTR_GET_PEER_ADDR: is_ip4 = %u, "
3095                           "addr = %U", ep->is_ip4, format_ip46_address,
3096                           &session->peer_addr.ip46, ep->is_ip4);
3097         }
3098       else
3099         rv = VPPCOM_EINVAL;
3100       break;
3101
3102     case VPPCOM_ATTR_GET_LCL_ADDR:
3103       if (buffer && buflen && (*buflen >= sizeof (*ep)))
3104         {
3105           ep->vrf = session->vrf;
3106           ep->is_ip4 = session->lcl_addr.is_ip4;
3107           ep->port = session->port;
3108           if (session->lcl_addr.is_ip4)
3109             clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip4,
3110                          sizeof (ip4_address_t));
3111           else
3112             clib_memcpy (ep->ip, &session->lcl_addr.ip46.ip6,
3113                          sizeof (ip6_address_t));
3114           *buflen = sizeof (*ep);
3115           if (VPPCOM_DEBUG > 0)
3116             if (VPPCOM_DEBUG > 0)
3117               clib_warning ("VPPCOM_ATTR_GET_LCL_ADDR: is_ip4 = %u, "
3118                             "addr = %U", ep->is_ip4, format_ip46_address,
3119                             &session->lcl_addr.ip46, ep->is_ip4);
3120         }
3121       else
3122         rv = VPPCOM_EINVAL;
3123       break;
3124
3125     case VPPCOM_ATTR_SET_REUSEADDR:
3126       break;
3127
3128     case VPPCOM_ATTR_SET_BROADCAST:
3129       break;
3130
3131     case VPPCOM_ATTR_SET_V6ONLY:
3132       break;
3133
3134     case VPPCOM_ATTR_SET_KEEPALIVE:
3135       break;
3136
3137     case VPPCOM_ATTR_SET_TCP_KEEPIDLE:
3138       break;
3139
3140     case VPPCOM_ATTR_SET_TCP_KEEPINTVL:
3141       break;
3142     }
3143
3144 done:
3145   clib_spinlock_unlock (&vcm->sessions_lockp);
3146   return rv;
3147 }
3148
3149   /*
3150    * fd.io coding-style-patch-verification: ON
3151    *
3152    * Local Variables:
3153    * eval: (c-set-style "gnu")
3154    * End:
3155    */