PAPI: Use UNIX domain sockets instead of shared memory
[vpp.git] / src / vlibmemory / socket_api.c
1 /*
2  *------------------------------------------------------------------
3  * socket_api.c
4  *
5  * Copyright (c) 2009 Cisco and/or its affiliates.
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at:
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *------------------------------------------------------------------
18  */
19
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <sys/ioctl.h>
24 #include <fcntl.h>
25 #include <sys/stat.h>
26
27 #include <vppinfra/byte_order.h>
28 #include <svm/ssvm.h>
29 #include <vlibmemory/api.h>
30
31 #include <vlibmemory/vl_memory_msg_enum.h>
32
33 #define vl_typedefs             /* define message structures */
34 #include <vlibmemory/vl_memory_api_h.h>
35 #undef vl_typedefs
36
37 /* instantiate all the print functions we know about */
38 #define vl_print(handle, ...) vlib_cli_output (handle, __VA_ARGS__)
39 #define vl_printfun
40 #include <vlibmemory/vl_memory_api_h.h>
41 #undef vl_printfun
42
43 /* instantiate all the endian swap functions we know about */
44 #define vl_endianfun
45 #include <vlibmemory/vl_memory_api_h.h>
46 #undef vl_endianfun
47
48 socket_main_t socket_main;
49
50 void
51 vl_sock_api_dump_clients (vlib_main_t * vm, api_main_t * am)
52 {
53   vl_api_registration_t *reg;
54   socket_main_t *sm = &socket_main;
55   clib_file_t *f;
56
57   /*
58    * Must have at least one active client, not counting the
59    * REGISTRATION_TYPE_SOCKET_LISTEN bind/accept socket
60    */
61   if (pool_elts (sm->registration_pool) < 2)
62     return;
63
64   vlib_cli_output (vm, "Socket clients");
65   vlib_cli_output (vm, "%20s %8s", "Name", "Fildesc");
66     /* *INDENT-OFF* */
67     pool_foreach (reg, sm->registration_pool,
68     ({
69         if (reg->registration_type == REGISTRATION_TYPE_SOCKET_SERVER) {
70             f = vl_api_registration_file (reg);
71             vlib_cli_output (vm, "%20s %8d", reg->name, f->file_descriptor);
72         }
73     }));
74 /* *INDENT-ON* */
75 }
76
77 vl_api_registration_t *
78 vl_socket_api_client_index_to_registration (u32 index)
79 {
80   socket_main_t *sm = &socket_main;
81   if (pool_is_free_index (sm->registration_pool, ntohl (index)))
82     {
83 #if DEBUG > 2
84       clib_warning ("Invalid index %d\n", ntohl (index));
85 #endif
86       return 0;
87     }
88   return pool_elt_at_index (sm->registration_pool, ntohl (index));
89 }
90
91 void
92 vl_socket_api_send (vl_api_registration_t * rp, u8 * elem)
93 {
94 #if CLIB_DEBUG > 1
95   u32 output_length;
96 #endif
97   socket_main_t *sm = &socket_main;
98   u16 msg_id = ntohs (*(u16 *) elem);
99   api_main_t *am = &api_main;
100   msgbuf_t *mb = (msgbuf_t *) (elem - offsetof (msgbuf_t, data));
101   vl_api_registration_t *sock_rp;
102   clib_file_main_t *fm = &file_main;
103   clib_error_t *error;
104   clib_file_t *cf;
105
106   cf = vl_api_registration_file (rp);
107   ASSERT (rp->registration_type > REGISTRATION_TYPE_SHMEM);
108
109   if (msg_id >= vec_len (am->api_trace_cfg))
110     {
111       clib_warning ("id out of range: %d", msg_id);
112       vl_msg_api_free ((void *) elem);
113       return;
114     }
115
116   sock_rp = pool_elt_at_index (sm->registration_pool,
117                                rp->vl_api_registration_pool_index);
118   ASSERT (sock_rp);
119
120   /* Add the msgbuf_t to the output vector */
121   vec_add (sock_rp->output_vector, (u8 *) mb, sizeof (*mb));
122
123   /* Try to send the message and save any error like
124    * we do in the input epoll loop */
125   vec_add (sock_rp->output_vector, elem, ntohl (mb->data_len));
126   error = clib_file_write (cf);
127   unix_save_error (&unix_main, error);
128
129   /* If we didn't finish sending everything, wait for tx space */
130   if (vec_len (sock_rp->output_vector) > 0
131       && !(cf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE))
132     {
133       cf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
134       fm->file_update (cf, UNIX_FILE_UPDATE_MODIFY);
135     }
136
137 #if CLIB_DEBUG > 1
138   output_length = sizeof (*mb) + ntohl (mb->data_len);
139   clib_warning ("wrote %u bytes to fd %d", output_length,
140                 cf->file_descriptor);
141 #endif
142
143   vl_msg_api_free ((void *) elem);
144 }
145
146 void
147 vl_socket_free_registration_index (u32 pool_index)
148 {
149   int i;
150   vl_api_registration_t *rp;
151   if (pool_is_free_index (socket_main.registration_pool, pool_index))
152     {
153       clib_warning ("main pool index %d already free", pool_index);
154       return;
155     }
156   rp = pool_elt_at_index (socket_main.registration_pool, pool_index);
157
158   ASSERT (rp->registration_type != REGISTRATION_TYPE_FREE);
159   for (i = 0; i < vec_len (rp->additional_fds_to_close); i++)
160     if (close (rp->additional_fds_to_close[i]) < 0)
161       clib_unix_warning ("close");
162   vec_free (rp->additional_fds_to_close);
163   vec_free (rp->name);
164   vec_free (rp->unprocessed_input);
165   vec_free (rp->output_vector);
166   rp->registration_type = REGISTRATION_TYPE_FREE;
167   pool_put (socket_main.registration_pool, rp);
168 }
169
170 void
171 vl_socket_process_api_msg (clib_file_t * uf, vl_api_registration_t * rp,
172                            i8 * input_v)
173 {
174   msgbuf_t *mbp = (msgbuf_t *) input_v;
175
176   u8 *the_msg = (u8 *) (mbp->data);
177   socket_main.current_uf = uf;
178   socket_main.current_rp = rp;
179   vl_msg_api_socket_handler (the_msg);
180   socket_main.current_uf = 0;
181   socket_main.current_rp = 0;
182 }
183
184 clib_error_t *
185 vl_socket_read_ready (clib_file_t * uf)
186 {
187   clib_file_main_t *fm = &file_main;
188   vlib_main_t *vm = vlib_get_main ();
189   vl_api_registration_t *rp;
190   int n;
191   i8 *msg_buffer = 0;
192   u8 *data_for_process;
193   u32 msg_len;
194   u32 save_input_buffer_length = vec_len (socket_main.input_buffer);
195   vl_socket_args_for_process_t *a;
196   msgbuf_t *mbp;
197   int mbp_set = 0;
198
199   rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
200
201   n = read (uf->file_descriptor, socket_main.input_buffer,
202             vec_len (socket_main.input_buffer));
203
204   if (n <= 0 && errno != EAGAIN)
205     {
206       clib_file_del (fm, uf);
207
208       if (!pool_is_free (socket_main.registration_pool, rp))
209         {
210           u32 index = rp - socket_main.registration_pool;
211           vl_socket_free_registration_index (index);
212         }
213       else
214         {
215           clib_warning ("client index %d already free?",
216                         rp->vl_api_registration_pool_index);
217         }
218       return 0;
219     }
220
221   _vec_len (socket_main.input_buffer) = n;
222
223   /*
224    * Look for bugs here. This code is tricky because
225    * data read from a stream socket does not honor message
226    * boundaries. In the case of a long message (>4K bytes)
227    * we have to do (at least) 2 reads, etc.
228    */
229   do
230     {
231       if (vec_len (rp->unprocessed_input))
232         {
233           vec_append (rp->unprocessed_input, socket_main.input_buffer);
234           msg_buffer = rp->unprocessed_input;
235         }
236       else
237         {
238           msg_buffer = socket_main.input_buffer;
239           mbp_set = 0;
240         }
241
242       if (mbp_set == 0)
243         {
244           /* Any chance that we have a complete message? */
245           if (vec_len (msg_buffer) <= sizeof (msgbuf_t))
246             goto save_and_split;
247
248           mbp = (msgbuf_t *) msg_buffer;
249           msg_len = ntohl (mbp->data_len);
250           mbp_set = 1;
251         }
252
253       /* We don't have the entire message yet. */
254       if (mbp_set == 0
255           || (msg_len + sizeof (msgbuf_t)) > vec_len (msg_buffer))
256         {
257         save_and_split:
258           /* if we were using the input buffer save the fragment */
259           if (msg_buffer == socket_main.input_buffer)
260             {
261               ASSERT (vec_len (rp->unprocessed_input) == 0);
262               vec_validate (rp->unprocessed_input, vec_len (msg_buffer) - 1);
263               clib_memcpy (rp->unprocessed_input, msg_buffer,
264                            vec_len (msg_buffer));
265               _vec_len (rp->unprocessed_input) = vec_len (msg_buffer);
266             }
267           _vec_len (socket_main.input_buffer) = save_input_buffer_length;
268           return 0;
269         }
270
271       data_for_process = (u8 *) vec_dup (msg_buffer);
272       _vec_len (data_for_process) = (msg_len + sizeof (msgbuf_t));
273       pool_get (socket_main.process_args, a);
274       a->clib_file = uf;
275       a->regp = rp;
276       a->data = data_for_process;
277
278       vlib_process_signal_event (vm, vl_api_clnt_node.index,
279                                  SOCKET_READ_EVENT,
280                                  a - socket_main.process_args);
281       if (n > (msg_len + sizeof (*mbp)))
282         vec_delete (msg_buffer, msg_len + sizeof (*mbp), 0);
283       else
284         _vec_len (msg_buffer) = 0;
285       n -= msg_len + sizeof (msgbuf_t);
286       msg_len = 0;
287       mbp_set = 0;
288     }
289   while (n > 0);
290
291   _vec_len (socket_main.input_buffer) = save_input_buffer_length;
292
293   return 0;
294 }
295
296 clib_error_t *
297 vl_socket_write_ready (clib_file_t * uf)
298 {
299   clib_file_main_t *fm = &file_main;
300   vl_api_registration_t *rp;
301   int n;
302
303   rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
304
305   /* Flush output vector. */
306   size_t total_bytes = vec_len (rp->output_vector);
307   size_t bytes_to_send, remaining_bytes = total_bytes;
308   void *p = rp->output_vector;
309   while (remaining_bytes > 0)
310     {
311       bytes_to_send = remaining_bytes > 4096 ? 4096 : remaining_bytes;
312       n = write (uf->file_descriptor, p, bytes_to_send);
313       if (n < 0)
314         {
315           if (errno == EAGAIN)
316             {
317               break;
318             }
319 #if DEBUG > 2
320           clib_warning ("write error, close the file...\n");
321 #endif
322           clib_file_del (fm, uf);
323           vl_socket_free_registration_index (rp -
324                                              socket_main.registration_pool);
325           return 0;
326         }
327       remaining_bytes -= bytes_to_send;
328       p += bytes_to_send;
329     }
330
331   vec_delete (rp->output_vector, total_bytes - remaining_bytes, 0);
332   if (vec_len (rp->output_vector) <= 0
333       && (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE))
334     {
335       uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE;
336       fm->file_update (uf, UNIX_FILE_UPDATE_MODIFY);
337     }
338
339   return 0;
340 }
341
342 clib_error_t *
343 vl_socket_error_ready (clib_file_t * uf)
344 {
345   vl_api_registration_t *rp;
346   clib_file_main_t *fm = &file_main;
347
348   rp = pool_elt_at_index (socket_main.registration_pool, uf->private_data);
349   clib_file_del (fm, uf);
350   vl_socket_free_registration_index (rp - socket_main.registration_pool);
351
352   return 0;
353 }
354
355 void
356 socksvr_file_add (clib_file_main_t * fm, int fd)
357 {
358   vl_api_registration_t *rp;
359   clib_file_t template = { 0 };
360
361   pool_get (socket_main.registration_pool, rp);
362   memset (rp, 0, sizeof (*rp));
363
364   template.read_function = vl_socket_read_ready;
365   template.write_function = vl_socket_write_ready;
366   template.error_function = vl_socket_error_ready;
367   template.file_descriptor = fd;
368   template.private_data = rp - socket_main.registration_pool;
369
370   rp->registration_type = REGISTRATION_TYPE_SOCKET_SERVER;
371   rp->vl_api_registration_pool_index = rp - socket_main.registration_pool;
372   rp->clib_file_index = clib_file_add (fm, &template);
373 }
374
375 static clib_error_t *
376 socksvr_accept_ready (clib_file_t * uf)
377 {
378   clib_file_main_t *fm = &file_main;
379   socket_main_t *sm = &socket_main;
380   clib_socket_t *sock = &sm->socksvr_listen_socket;
381   clib_socket_t client;
382   clib_error_t *error;
383
384   error = clib_socket_accept (sock, &client);
385   if (error)
386     return error;
387
388   socksvr_file_add (fm, client.fd);
389   return 0;
390 }
391
392 static clib_error_t *
393 socksvr_bogus_write (clib_file_t * uf)
394 {
395   clib_warning ("why am I here?");
396   return 0;
397 }
398
399 /*
400  * vl_api_sockclnt_create_t_handler
401  */
402 void
403 vl_api_sockclnt_create_t_handler (vl_api_sockclnt_create_t * mp)
404 {
405   vl_api_registration_t *regp;
406   vl_api_sockclnt_create_reply_t *rp;
407   api_main_t *am = &api_main;
408   hash_pair_t *hp;
409   int rv = 0;
410   u32 nmsg = hash_elts (am->msg_index_by_name_and_crc);
411   u32 i = 0;
412
413   regp = socket_main.current_rp;
414
415   ASSERT (regp->registration_type == REGISTRATION_TYPE_SOCKET_SERVER);
416
417   regp->name = format (0, "%s%c", mp->name, 0);
418
419   u32 size = sizeof (*rp) + (nmsg * sizeof (vl_api_message_table_entry_t));
420   rp = vl_msg_api_alloc (size);
421   rp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE_REPLY);
422   rp->index = htonl (regp->vl_api_registration_pool_index);
423   rp->context = mp->context;
424   rp->response = htonl (rv);
425   rp->count = htons (nmsg);
426
427   /* *INDENT-OFF* */
428   hash_foreach_pair (hp, am->msg_index_by_name_and_crc,
429   ({
430     rp->message_table[i].index = htons(hp->value[0]);
431     strncpy((char *)rp->message_table[i].name, (char *)hp->key, 64-1);
432     i++;
433   }));
434   /* *INDENT-ON* */
435   vl_api_send_msg (regp, (u8 *) rp);
436 }
437
438 /*
439  * vl_api_sockclnt_delete_t_handler
440  */
441 void
442 vl_api_sockclnt_delete_t_handler (vl_api_sockclnt_delete_t * mp)
443 {
444   vl_api_registration_t *regp;
445   vl_api_sockclnt_delete_reply_t *rp;
446
447   regp = vl_api_client_index_to_registration (mp->client_index);
448   if (!regp)
449     return;
450
451   u32 reg_index = ntohl (mp->index);
452   rp = vl_msg_api_alloc (sizeof (*rp));
453   rp->_vl_msg_id = htons (VL_API_SOCKCLNT_DELETE_REPLY);
454   rp->context = mp->context;
455
456   if (!pool_is_free_index (socket_main.registration_pool, reg_index))
457     {
458       rp->response = htonl (1);
459       vl_api_send_msg (regp, (u8 *) rp);
460
461       vl_api_registration_del_file (regp);
462       vl_socket_free_registration_index (reg_index);
463     }
464   else
465     {
466       clib_warning ("unknown client ID %d", reg_index);
467       rp->response = htonl (-1);
468       vl_api_send_msg (regp, (u8 *) rp);
469     }
470 }
471
472 clib_error_t *
473 vl_sock_api_send_fd_msg (int socket_fd, int fds[], int n_fds)
474 {
475   struct msghdr mh = { 0 };
476   struct iovec iov[1];
477   char ctl[CMSG_SPACE (sizeof (int) * n_fds)];
478   struct cmsghdr *cmsg;
479   char *msg = "fdmsg";
480   int rv;
481
482   iov[0].iov_base = msg;
483   iov[0].iov_len = strlen (msg);
484   mh.msg_iov = iov;
485   mh.msg_iovlen = 1;
486
487   memset (&ctl, 0, sizeof (ctl));
488   mh.msg_control = ctl;
489   mh.msg_controllen = sizeof (ctl);
490   cmsg = CMSG_FIRSTHDR (&mh);
491   cmsg->cmsg_len = CMSG_LEN (sizeof (int) * n_fds);
492   cmsg->cmsg_level = SOL_SOCKET;
493   cmsg->cmsg_type = SCM_RIGHTS;
494   clib_memcpy (CMSG_DATA (cmsg), fds, sizeof (int) * n_fds);
495
496   rv = sendmsg (socket_fd, &mh, 0);
497   if (rv < 0)
498     return clib_error_return_unix (0, "sendmsg");
499   return 0;
500 }
501
502 vl_api_shm_elem_config_t *
503 vl_api_make_shm_config (vl_api_sock_init_shm_t * mp)
504 {
505   vl_api_shm_elem_config_t *config = 0, *c;
506   u64 cfg;
507   int i;
508
509   if (!mp->nitems)
510     {
511       vec_validate (config, 6);
512       config[0].type = VL_API_VLIB_RING;
513       config[0].size = 256;
514       config[0].count = 32;
515
516       config[1].type = VL_API_VLIB_RING;
517       config[1].size = 1024;
518       config[1].count = 16;
519
520       config[2].type = VL_API_VLIB_RING;
521       config[2].size = 4096;
522       config[2].count = 2;
523
524       config[3].type = VL_API_CLIENT_RING;
525       config[3].size = 256;
526       config[3].count = 32;
527
528       config[4].type = VL_API_CLIENT_RING;
529       config[4].size = 1024;
530       config[4].count = 16;
531
532       config[5].type = VL_API_CLIENT_RING;
533       config[5].size = 4096;
534       config[5].count = 2;
535
536       config[6].type = VL_API_QUEUE;
537       config[6].count = 128;
538       config[6].size = sizeof (uword);
539     }
540   else
541     {
542       vec_validate (config, mp->nitems - 1);
543       for (i = 0; i < mp->nitems; i++)
544         {
545           cfg = mp->configs[i];
546           /* Pretty much a hack but it avoids defining our own api type
547            * in memclnt.api */
548           c = (vl_api_shm_elem_config_t *) & cfg;
549           config[i].type = c->type;
550           config[i].count = c->count;
551           config[i].size = c->size;
552         }
553     }
554   return config;
555 }
556
557 /*
558  * Bootstrap shm api using the socket api
559  */
560 void
561 vl_api_sock_init_shm_t_handler (vl_api_sock_init_shm_t * mp)
562 {
563   vl_api_sock_init_shm_reply_t *rmp;
564   ssvm_private_t _memfd_private, *memfd = &_memfd_private;
565   svm_map_region_args_t _args, *a = &_args;
566   vl_api_registration_t *regp;
567   api_main_t *am = &api_main;
568   svm_region_t *vlib_rp;
569   clib_file_t *cf;
570   vl_api_shm_elem_config_t *config = 0;
571   vl_shmem_hdr_t *shmem_hdr;
572   int rv;
573
574   regp = vl_api_client_index_to_registration (mp->client_index);
575   if (regp == 0)
576     {
577       clib_warning ("API client disconnected");
578       return;
579     }
580   if (regp->registration_type != REGISTRATION_TYPE_SOCKET_SERVER)
581     {
582       rv = -31;                 /* VNET_API_ERROR_INVALID_REGISTRATION */
583       goto reply;
584     }
585
586   /*
587    * Set up a memfd segment of the requested size wherein the
588    * shmem data structures will be initialized
589    */
590   memset (memfd, 0, sizeof (*memfd));
591   memfd->ssvm_size = mp->requested_size;
592   memfd->requested_va = 0ULL;
593   memfd->i_am_master = 1;
594   memfd->name = format (0, "%s%c", regp->name, 0);
595
596   if ((rv = ssvm_master_init_memfd (memfd)))
597     goto reply;
598
599   /* Remember to close this fd when the socket connection goes away */
600   vec_add1 (regp->additional_fds_to_close, memfd->fd);
601
602   /*
603    * Create a plausible svm_region in the memfd backed segment
604    */
605   memset (a, 0, sizeof (*a));
606   a->baseva = memfd->sh->ssvm_va + MMAP_PAGESIZE;
607   a->size = memfd->ssvm_size - MMAP_PAGESIZE;
608   /* $$$$ might want a different config parameter */
609   a->pvt_heap_size = am->api_pvt_heap_size;
610   a->flags = SVM_FLAGS_MHEAP;
611   svm_region_init_mapped_region (a, (svm_region_t *) a->baseva);
612
613   /*
614    * Part deux, initialize the svm_region_t shared-memory header
615    * api allocation rings, and so on.
616    */
617   config = vl_api_make_shm_config (mp);
618   vlib_rp = (svm_region_t *) a->baseva;
619   vl_init_shmem (vlib_rp, config, 1 /* is_vlib (dont-care) */ ,
620                  1 /* is_private */ );
621
622   /* Remember who created this. Needs to be post vl_init_shmem */
623   shmem_hdr = (vl_shmem_hdr_t *) vlib_rp->user_ctx;
624   shmem_hdr->clib_file_index = vl_api_registration_file_index (regp);
625
626   vec_add1 (am->vlib_private_rps, vlib_rp);
627   memfd->sh->ready = 1;
628   vec_free (config);
629
630   /* Recompute the set of input queues to poll in memclnt_process */
631   vec_reset_length (vl_api_queue_cursizes);
632
633 reply:
634
635   rmp = vl_msg_api_alloc (sizeof (*rmp));
636   rmp->_vl_msg_id = htons (VL_API_SOCK_INIT_SHM_REPLY);
637   rmp->context = mp->context;
638   rmp->retval = htonl (rv);
639
640   /*
641    * Note: The reply message needs to make it out the back door
642    * before we send the magic fd message. That's taken care of by
643    * the send function.
644    */
645   vl_socket_api_send (regp, (u8 *) rmp);
646
647   if (rv != 0)
648     return;
649
650   /* Send the magic "here's your sign (aka fd)" socket message */
651   cf = vl_api_registration_file (regp);
652   vl_sock_api_send_fd_msg (cf->file_descriptor, &memfd->fd, 1);
653 }
654
655 #define foreach_vlib_api_msg                            \
656 _(SOCKCLNT_CREATE, sockclnt_create)                     \
657 _(SOCKCLNT_DELETE, sockclnt_delete)                     \
658 _(SOCK_INIT_SHM, sock_init_shm)
659
660 clib_error_t *
661 vl_sock_api_init (vlib_main_t * vm)
662 {
663   clib_file_main_t *fm = &file_main;
664   clib_file_t template = { 0 };
665   vl_api_registration_t *rp;
666   socket_main_t *sm = &socket_main;
667   clib_socket_t *sock = &sm->socksvr_listen_socket;
668   clib_error_t *error;
669
670   /* If not explicitly configured, do not bind/enable, etc. */
671   if (sm->socket_name == 0)
672     return 0;
673
674 #define _(N,n)                                                  \
675     vl_msg_api_set_handlers(VL_API_##N, #n,                     \
676                            vl_api_##n##_t_handler,              \
677                            vl_noop_handler,                     \
678                            vl_api_##n##_t_endian,               \
679                            vl_api_##n##_t_print,                \
680                            sizeof(vl_api_##n##_t), 1);
681   foreach_vlib_api_msg;
682 #undef _
683
684   vec_resize (sm->input_buffer, 4096);
685
686   sock->config = (char *) sm->socket_name;
687
688   /* mkdir of file socket, only under /run  */
689   if (strncmp (sock->config, "/run", 4) == 0)
690     {
691       u8 *tmp = format (0, "%s", sock->config);
692       int i = vec_len (tmp);
693       while (i && tmp[--i] != '/')
694         ;
695
696       tmp[i] = 0;
697
698       if (i)
699         vlib_unix_recursive_mkdir ((char *) tmp);
700       vec_free (tmp);
701     }
702
703   sock->flags = CLIB_SOCKET_F_IS_SERVER | CLIB_SOCKET_F_SEQPACKET |
704     CLIB_SOCKET_F_ALLOW_GROUP_WRITE;
705   error = clib_socket_init (sock);
706   if (error)
707     return error;
708
709   pool_get (sm->registration_pool, rp);
710   memset (rp, 0, sizeof (*rp));
711
712   rp->registration_type = REGISTRATION_TYPE_SOCKET_LISTEN;
713
714   template.read_function = socksvr_accept_ready;
715   template.write_function = socksvr_bogus_write;
716   template.file_descriptor = sock->fd;
717   template.private_data = rp - sm->registration_pool;
718
719   rp->clib_file_index = clib_file_add (fm, &template);
720   return 0;
721 }
722
723 static clib_error_t *
724 socket_exit (vlib_main_t * vm)
725 {
726   socket_main_t *sm = &socket_main;
727   vl_api_registration_t *rp;
728
729   /* Defensive driving in case something wipes out early */
730   if (sm->registration_pool)
731     {
732       u32 index;
733         /* *INDENT-OFF* */
734         pool_foreach (rp, sm->registration_pool, ({
735           vl_api_registration_del_file (rp);
736           index = rp->vl_api_registration_pool_index;
737           vl_socket_free_registration_index (index);
738         }));
739 /* *INDENT-ON* */
740     }
741
742   return 0;
743 }
744
745 VLIB_MAIN_LOOP_EXIT_FUNCTION (socket_exit);
746
747 static clib_error_t *
748 socksvr_config (vlib_main_t * vm, unformat_input_t * input)
749 {
750   socket_main_t *sm = &socket_main;
751
752   while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
753     {
754       if (unformat (input, "socket-name %s", &sm->socket_name))
755         ;
756       else if (unformat (input, "default"))
757         {
758           sm->socket_name = format (0, "%s%c", API_SOCKET_FILE, 0);
759         }
760       else
761         {
762           return clib_error_return (0, "unknown input '%U'",
763                                     format_unformat_error, input);
764         }
765     }
766   return 0;
767 }
768
769 VLIB_CONFIG_FUNCTION (socksvr_config, "socksvr");
770
771 clib_error_t *
772 vlibsocket_init (vlib_main_t * vm)
773 {
774   return 0;
775 }
776
777 VLIB_INIT_FUNCTION (vlibsocket_init);
778
779 /*
780  * fd.io coding-style-patch-verification: ON
781  *
782  * Local Variables:
783  * eval: (c-set-style "gnu")
784  * End:
785  */