ethernet: check destination mac for L3 in ethernet-input node
[vpp.git] / src / vlibmemory / socket_client.c
1 /*
2  *------------------------------------------------------------------
3  * socket_client.c - API message handling over sockets, client code.
4  *
5  * Copyright (c) 2017 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 <stdio.h>
21 #define __USE_GNU
22 #define _GNU_SOURCE
23 #include <sys/socket.h>
24
25 #ifdef __FreeBSD__
26 #define _WANT_UCRED
27 #include <sys/types.h>
28 #include <sys/param.h>
29 #include <sys/ucred.h>
30 #include <sys/un.h>
31 #endif /* __FreeBSD__ */
32
33 #include <svm/ssvm.h>
34 #include <vlibmemory/socket_client.h>
35 #include <vlibmemory/memory_client.h>
36
37 #include <vlibmemory/vl_memory_msg_enum.h>
38
39 #define vl_typedefs             /* define message structures */
40 #include <vlibmemory/vl_memory_api_h.h>
41 #undef vl_typedefs
42
43 #define vl_endianfun            /* define message structures */
44 #include <vlibmemory/vl_memory_api_h.h>
45 #undef vl_endianfun
46
47 #define vl_calcsizefun
48 #include <vlibmemory/vl_memory_api_h.h>
49 #undef vl_calcsizefun
50
51 /* instantiate all the print functions we know about */
52 #define vl_printfun
53 #include <vlibmemory/vl_memory_api_h.h>
54 #undef vl_printfun
55
56 socket_client_main_t socket_client_main;
57 __thread socket_client_main_t *socket_client_ctx = &socket_client_main;
58
59 /* Debug aid */
60 u32 vl (void *p) __attribute__ ((weak));
61
62 u32
63 vl (void *p)
64 {
65   return vec_len (p);
66 }
67
68 static socket_client_main_t *
69 vl_socket_client_ctx_push (socket_client_main_t * ctx)
70 {
71   socket_client_main_t *old = socket_client_ctx;
72   socket_client_ctx = ctx;
73   return old;
74 }
75
76 static void
77 vl_socket_client_ctx_pop (socket_client_main_t * old_ctx)
78 {
79   socket_client_ctx = old_ctx;
80 }
81
82 static int
83 vl_socket_client_read_internal (socket_client_main_t * scm, int wait)
84 {
85   u32 data_len = 0, msg_size;
86   int n, current_rx_index;
87   msgbuf_t *mbp = 0;
88   f64 timeout;
89
90   if (scm->socket_fd == 0)
91     return -1;
92
93   if (wait)
94     timeout = clib_time_now (&scm->clib_time) + wait;
95
96   while (1)
97     {
98       current_rx_index = vec_len (scm->socket_rx_buffer);
99       while (current_rx_index < sizeof (*mbp))
100         {
101           vec_validate (scm->socket_rx_buffer, current_rx_index
102                         + scm->socket_buffer_size - 1);
103           n = read (scm->socket_fd, scm->socket_rx_buffer + current_rx_index,
104                     scm->socket_buffer_size);
105           if (n < 0)
106             {
107               if (errno == EAGAIN)
108                 continue;
109
110               clib_unix_warning ("socket_read");
111               vec_set_len (scm->socket_rx_buffer, current_rx_index);
112               return -1;
113             }
114           current_rx_index += n;
115         }
116       vec_set_len (scm->socket_rx_buffer, current_rx_index);
117
118 #if CLIB_DEBUG > 1
119       if (n > 0)
120         clib_warning ("read %d bytes", n);
121 #endif
122
123       mbp = (msgbuf_t *) (scm->socket_rx_buffer);
124       data_len = ntohl (mbp->data_len);
125       current_rx_index = vec_len (scm->socket_rx_buffer);
126       vec_validate (scm->socket_rx_buffer, current_rx_index + data_len);
127       mbp = (msgbuf_t *) (scm->socket_rx_buffer);
128       msg_size = data_len + sizeof (*mbp);
129
130       while (current_rx_index < msg_size)
131         {
132           n = read (scm->socket_fd, scm->socket_rx_buffer + current_rx_index,
133                     msg_size - current_rx_index);
134           if (n < 0)
135             {
136               if (errno == EAGAIN)
137                 continue;
138
139               clib_unix_warning ("socket_read");
140               vec_set_len (scm->socket_rx_buffer, current_rx_index);
141               return -1;
142             }
143           current_rx_index += n;
144         }
145       vec_set_len (scm->socket_rx_buffer, current_rx_index);
146
147       if (vec_len (scm->socket_rx_buffer) >= data_len + sizeof (*mbp))
148         {
149           vl_msg_api_socket_handler ((void *) (mbp->data), data_len);
150
151           if (vec_len (scm->socket_rx_buffer) == data_len + sizeof (*mbp))
152             vec_set_len (scm->socket_rx_buffer, 0);
153           else
154             vec_delete (scm->socket_rx_buffer, data_len + sizeof (*mbp), 0);
155           mbp = 0;
156
157           /* Quit if we're out of data, and not expecting a ping reply */
158           if (vec_len (scm->socket_rx_buffer) == 0
159               && scm->control_pings_outstanding == 0)
160             break;
161         }
162       if (wait && clib_time_now (&scm->clib_time) >= timeout)
163         return -1;
164     }
165   return 0;
166 }
167
168 int
169 vl_socket_client_read (int wait)
170 {
171   return vl_socket_client_read_internal (socket_client_ctx, wait);
172 }
173
174 int
175 vl_socket_client_read2 (socket_client_main_t * scm, int wait)
176 {
177   socket_client_main_t *old_ctx;
178   int rv;
179
180   old_ctx = vl_socket_client_ctx_push (scm);
181   rv = vl_socket_client_read_internal (scm, wait);
182   vl_socket_client_ctx_pop (old_ctx);
183   return rv;
184 }
185
186 static int
187 vl_socket_client_write_internal (socket_client_main_t * scm)
188 {
189   int n;
190   int len = vec_len (scm->socket_tx_buffer);
191   msgbuf_t msgbuf = {
192     .q = 0,
193     .gc_mark_timestamp = 0,
194     .data_len = htonl (len),
195   };
196
197   n = write (scm->socket_fd, &msgbuf, sizeof (msgbuf));
198   if (n < sizeof (msgbuf))
199     {
200       clib_unix_warning ("socket write (msgbuf)");
201       return -1;
202     }
203
204   n = write (scm->socket_fd, scm->socket_tx_buffer, len);
205
206   vec_set_len (scm->socket_tx_buffer, 0);
207
208   if (n < len)
209     {
210       clib_unix_warning ("socket write (msg)");
211       return -1;
212     }
213
214   return n;
215 }
216
217 int
218 vl_socket_client_write (void)
219 {
220   return vl_socket_client_write_internal (socket_client_ctx);
221 }
222
223 int
224 vl_socket_client_write2 (socket_client_main_t * scm)
225 {
226   socket_client_main_t *old_ctx;
227   int rv;
228
229   old_ctx = vl_socket_client_ctx_push (scm);
230   rv = vl_socket_client_write_internal (scm);
231   vl_socket_client_ctx_pop (old_ctx);
232   return rv;
233 }
234
235 void *
236 vl_socket_client_msg_alloc2 (socket_client_main_t * scm, int nbytes)
237 {
238   vec_set_len (scm->socket_tx_buffer, nbytes);
239   return ((void *) scm->socket_tx_buffer);
240 }
241
242 void *
243 vl_socket_client_msg_alloc (int nbytes)
244 {
245   return vl_socket_client_msg_alloc2 (socket_client_ctx, nbytes);
246 }
247
248 void
249 vl_socket_client_disconnect2 (socket_client_main_t * scm)
250 {
251   if (vl_mem_client_is_connected ())
252     {
253       vl_client_disconnect_from_vlib_no_unmap ();
254       ssvm_delete_memfd (&scm->memfd_segment);
255     }
256   if (scm->socket_fd && (close (scm->socket_fd) < 0))
257     clib_unix_warning ("close");
258   scm->socket_fd = 0;
259 }
260
261 void
262 vl_socket_client_disconnect (void)
263 {
264   vl_socket_client_disconnect2 (socket_client_ctx);
265 }
266
267 void
268 vl_socket_client_enable_disable2 (socket_client_main_t * scm, int enable)
269 {
270   scm->socket_enable = enable;
271 }
272
273 void
274 vl_socket_client_enable_disable (int enable)
275 {
276   vl_socket_client_enable_disable2 (socket_client_ctx, enable);
277 }
278
279 static clib_error_t *
280 vl_sock_api_recv_fd_msg_internal (socket_client_main_t * scm, int fds[],
281                                   int n_fds, u32 wait)
282 {
283   char msgbuf[16];
284   char ctl[CMSG_SPACE (sizeof (int) * n_fds)
285            + CMSG_SPACE (sizeof (struct ucred))];
286   struct msghdr mh = { 0 };
287   struct iovec iov[1];
288   ssize_t size = 0;
289 #ifdef __linux__
290   struct ucred *cr = 0;
291 #elif __FreeBSD__
292   struct cmsgcred *cr = 0;
293 #endif /* __linux__ */
294   struct cmsghdr *cmsg;
295   pid_t pid __attribute__ ((unused));
296   uid_t uid __attribute__ ((unused));
297   gid_t gid __attribute__ ((unused));
298   int socket_fd;
299   f64 timeout;
300
301   socket_fd = scm->client_socket.fd;
302
303   iov[0].iov_base = msgbuf;
304   iov[0].iov_len = 5;
305   mh.msg_iov = iov;
306   mh.msg_iovlen = 1;
307   mh.msg_control = ctl;
308   mh.msg_controllen = sizeof (ctl);
309
310   clib_memset (ctl, 0, sizeof (ctl));
311
312   if (wait != ~0)
313     {
314       timeout = clib_time_now (&scm->clib_time) + wait;
315       while (size != 5 && clib_time_now (&scm->clib_time) < timeout)
316         size = recvmsg (socket_fd, &mh, MSG_DONTWAIT);
317     }
318   else
319     size = recvmsg (socket_fd, &mh, 0);
320
321   if (size != 5)
322     {
323       return (size == 0) ? clib_error_return (0, "disconnected") :
324         clib_error_return_unix (0, "recvmsg: malformed message (fd %d)",
325                                 socket_fd);
326     }
327
328   cmsg = CMSG_FIRSTHDR (&mh);
329   while (cmsg)
330     {
331       if (cmsg->cmsg_level == SOL_SOCKET)
332         {
333 #ifdef __linux__
334           if (cmsg->cmsg_type == SCM_CREDENTIALS)
335             {
336               cr = (struct ucred *) CMSG_DATA (cmsg);
337               uid = cr->uid;
338               gid = cr->gid;
339               pid = cr->pid;
340             }
341 #elif __FreeBSD__
342           if (cmsg->cmsg_type == SCM_CREDS)
343             {
344               cr = (struct cmsgcred *) CMSG_DATA (cmsg);
345               uid = cr->cmcred_uid;
346               gid = cr->cmcred_gid;
347               pid = cr->cmcred_pid;
348             }
349 #endif /* __linux__ */
350           else if (cmsg->cmsg_type == SCM_RIGHTS)
351             {
352               clib_memcpy_fast (fds, CMSG_DATA (cmsg), sizeof (int) * n_fds);
353             }
354         }
355       cmsg = CMSG_NXTHDR (&mh, cmsg);
356     }
357   return 0;
358 }
359
360 clib_error_t *
361 vl_sock_api_recv_fd_msg (int socket_fd, int fds[], int n_fds, u32 wait)
362 {
363   return vl_sock_api_recv_fd_msg_internal (socket_client_ctx, fds, n_fds,
364                                            wait);
365 }
366
367 clib_error_t *
368 vl_sock_api_recv_fd_msg2 (socket_client_main_t * scm, int socket_fd,
369                           int fds[], int n_fds, u32 wait)
370 {
371   socket_client_main_t *old_ctx;
372   clib_error_t *error;
373
374   old_ctx = vl_socket_client_ctx_push (scm);
375   error = vl_sock_api_recv_fd_msg_internal (scm, fds, n_fds, wait);
376   vl_socket_client_ctx_pop (old_ctx);
377   return error;
378 }
379
380 static void vl_api_sock_init_shm_reply_t_handler
381   (vl_api_sock_init_shm_reply_t * mp)
382 {
383   socket_client_main_t *scm = socket_client_ctx;
384   ssvm_private_t *memfd = &scm->memfd_segment;
385   i32 retval = ntohl (mp->retval);
386   api_main_t *am = vlibapi_get_main ();
387   clib_error_t *error;
388   int my_fd = -1;
389   u8 *new_name;
390
391   if (retval)
392     {
393       clib_warning ("failed to init shmem");
394       return;
395     }
396
397   /*
398    * Check the socket for the magic fd
399    */
400   error = vl_sock_api_recv_fd_msg (scm->socket_fd, &my_fd, 1, 5);
401   if (error)
402     {
403       clib_error_report (error);
404       retval = -99;
405       return;
406     }
407
408   clib_memset (memfd, 0, sizeof (*memfd));
409   memfd->fd = my_fd;
410
411   /* Note: this closes memfd.fd */
412   retval = ssvm_client_init_memfd (memfd);
413   if (retval)
414     clib_warning ("WARNING: segment map returned %d", retval);
415
416   /*
417    * Pivot to the memory client segment that vpp just created
418    */
419   am->vlib_rp = (void *) (memfd->requested_va + MMAP_PAGESIZE);
420   am->shmem_hdr = (void *) am->vlib_rp->user_ctx;
421
422   new_name = format (0, "%v[shm]%c", scm->name, 0);
423   vl_client_install_client_message_handlers ();
424   if (scm->want_shm_pthread)
425     {
426       vl_client_connect_to_vlib_no_map ("pvt", (char *) new_name,
427                                         32 /* input_queue_length */ );
428     }
429   else
430     {
431       vl_client_connect_to_vlib_no_rx_pthread_no_map ("pvt",
432                                                       (char *) new_name, 32
433                                                       /* input_queue_length */
434         );
435     }
436   vl_socket_client_enable_disable (0);
437   vec_free (new_name);
438 }
439
440 static void
441 vl_api_sockclnt_create_reply_t_handler (vl_api_sockclnt_create_reply_t * mp)
442 {
443   socket_client_main_t *scm = socket_client_ctx;
444   if (!mp->response)
445     {
446       scm->socket_enable = 1;
447       scm->client_index = clib_net_to_host_u32 (mp->index);
448     }
449 }
450
451 #define foreach_sock_client_api_msg                             \
452 _(SOCKCLNT_CREATE_REPLY, sockclnt_create_reply)                 \
453 _(SOCK_INIT_SHM_REPLY, sock_init_shm_reply)                     \
454
455 void
456 vl_sock_client_install_message_handlers (void)
457 {
458
459 #define _(N, n)                                                               \
460   vl_msg_api_config (&(vl_msg_api_msg_config_t){                              \
461     .id = VL_API_##N,                                                         \
462     .name = #n,                                                               \
463     .handler = vl_api_##n##_t_handler,                                        \
464     .endian = vl_api_##n##_t_endian,                                          \
465     .format_fn = vl_api_##n##_t_format,                                       \
466     .size = sizeof (vl_api_##n##_t),                                          \
467     .traced = 0,                                                              \
468     .tojson = vl_api_##n##_t_tojson,                                          \
469     .fromjson = vl_api_##n##_t_fromjson,                                      \
470     .calc_size = vl_api_##n##_t_calc_size,                                    \
471   });
472   foreach_sock_client_api_msg;
473 #undef _
474 }
475
476 int
477 vl_socket_client_connect_internal (socket_client_main_t * scm,
478                                    char *socket_path, char *client_name,
479                                    u32 socket_buffer_size)
480 {
481   vl_api_sockclnt_create_t *mp;
482   clib_socket_t *sock;
483   clib_error_t *error;
484
485   /* Already connected? */
486   if (scm->socket_fd)
487     return (-2);
488
489   /* bogus call? */
490   if (socket_path == 0 || client_name == 0)
491     return (-3);
492
493   sock = &scm->client_socket;
494   sock->config = socket_path;
495   sock->flags = CLIB_SOCKET_F_IS_CLIENT;
496
497   if ((error = clib_socket_init (sock)))
498     {
499       clib_error_report (error);
500       return (-1);
501     }
502
503   vl_sock_client_install_message_handlers ();
504
505   scm->socket_fd = sock->fd;
506   scm->socket_buffer_size = socket_buffer_size ? socket_buffer_size :
507     SOCKET_CLIENT_DEFAULT_BUFFER_SIZE;
508   vec_validate (scm->socket_tx_buffer, scm->socket_buffer_size - 1);
509   vec_validate (scm->socket_rx_buffer, scm->socket_buffer_size - 1);
510   vec_set_len (scm->socket_rx_buffer, 0);
511   vec_set_len (scm->socket_tx_buffer, 0);
512   scm->name = format (0, "%s", client_name);
513
514   mp = vl_socket_client_msg_alloc2 (scm, sizeof (*mp));
515   mp->_vl_msg_id = htons (VL_API_SOCKCLNT_CREATE);
516   strncpy ((char *) mp->name, client_name, sizeof (mp->name) - 1);
517   mp->name[sizeof (mp->name) - 1] = 0;
518   mp->context = 0xfeedface;
519
520   clib_time_init (&scm->clib_time);
521
522   if (vl_socket_client_write_internal (scm) <= 0)
523     return (-1);
524
525   if (vl_socket_client_read_internal (scm, 5))
526     return (-1);
527
528   return (0);
529 }
530
531 int
532 vl_socket_client_connect (char *socket_path, char *client_name,
533                           u32 socket_buffer_size)
534 {
535   return vl_socket_client_connect_internal (socket_client_ctx, socket_path,
536                                             client_name, socket_buffer_size);
537 }
538
539 int
540 vl_socket_client_connect2 (socket_client_main_t * scm, char *socket_path,
541                            char *client_name, u32 socket_buffer_size)
542 {
543   socket_client_main_t *old_ctx;
544   int rv;
545
546   old_ctx = vl_socket_client_ctx_push (scm);
547   rv = vl_socket_client_connect_internal (socket_client_ctx, socket_path,
548                                           client_name, socket_buffer_size);
549   vl_socket_client_ctx_pop (old_ctx);
550   return rv;
551 }
552
553 int
554 vl_socket_client_init_shm_internal (socket_client_main_t * scm,
555                                     vl_api_shm_elem_config_t * config,
556                                     int want_pthread)
557 {
558   vl_api_sock_init_shm_t *mp;
559   int rv, i;
560   u64 *cfg;
561
562   scm->want_shm_pthread = want_pthread;
563
564   mp = vl_socket_client_msg_alloc2 (scm, sizeof (*mp) +
565                                     vec_len (config) * sizeof (u64));
566   clib_memset (mp, 0, sizeof (*mp));
567   mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_SOCK_INIT_SHM);
568   mp->client_index = clib_host_to_net_u32 (scm->client_index);
569   mp->requested_size = 64 << 20;
570
571   if (config)
572     {
573       for (i = 0; i < vec_len (config); i++)
574         {
575           cfg = (u64 *) & config[i];
576           mp->configs[i] = *cfg;
577         }
578       mp->nitems = vec_len (config);
579     }
580   rv = vl_socket_client_write_internal (scm);
581   if (rv <= 0)
582     return rv;
583
584   if (vl_socket_client_read_internal (scm, 1))
585     return -1;
586
587   return 0;
588 }
589
590 int
591 vl_socket_client_init_shm (vl_api_shm_elem_config_t * config,
592                            int want_pthread)
593 {
594   return vl_socket_client_init_shm_internal (socket_client_ctx, config,
595                                              want_pthread);
596 }
597
598 int
599 vl_socket_client_init_shm2 (socket_client_main_t * scm,
600                             vl_api_shm_elem_config_t * config,
601                             int want_pthread)
602 {
603   socket_client_main_t *old_ctx;
604   int rv;
605
606   old_ctx = vl_socket_client_ctx_push (scm);
607   rv = vl_socket_client_init_shm_internal (socket_client_ctx, config,
608                                            want_pthread);
609   vl_socket_client_ctx_pop (old_ctx);
610   return rv;
611 }
612
613 clib_error_t *
614 vl_socket_client_recv_fd_msg2 (socket_client_main_t * scm, int fds[],
615                                int n_fds, u32 wait)
616 {
617   if (!scm->socket_fd)
618     return clib_error_return (0, "no socket");
619   return vl_sock_api_recv_fd_msg_internal (scm, fds, n_fds, wait);
620 }
621
622 clib_error_t *
623 vl_socket_client_recv_fd_msg (int fds[], int n_fds, u32 wait)
624 {
625   return vl_socket_client_recv_fd_msg2 (socket_client_ctx, fds, n_fds, wait);
626 }
627
628 /*
629  * fd.io coding-style-patch-verification: ON
630  *
631  * Local Variables:
632  * eval: (c-set-style "gnu")
633  * End:
634  */