aboutsummaryrefslogtreecommitdiffstats
path: root/src/svm/memfd.c
diff options
context:
space:
mode:
authorDave Barach <dave@barachs.net>2017-09-10 15:04:27 -0400
committerDamjan Marion <dmarion.lists@gmail.com>2017-10-03 11:03:47 +0000
commit59b2565cd91a67ced650739f36129650830211ac (patch)
tree1ae3b8d69d7952500b07186169fb31e0f72ae04e /src/svm/memfd.c
parent35ffa3e8f6b032f6e324234d495f769049d8feea (diff)
Repair vlib API socket server
- Teach vpp_api_test to send/receive API messages over sockets - Add memfd-based shared memory - Add api messages to create memfd-based shared memory segments - vpp_api_test supports both socket and shared memory segment connections - vpp_api_test pivot from socket to shared memory API messaging - add socket client support to libvlibclient.so - dead client reaper sends ping messages, container-friendly - dead client reaper falls back to kill (<pid>, 0) live checking if e.g. a python app goes silent for tens of seconds - handle ping messages in python client support code - teach show api ring about pairwise shared-memory segments - fix ip probing of already resolved destinations (VPP-998) We'll need this work to implement proper host-stack client isolation Change-Id: Ic23b65f75c854d0393d9a2e9d6b122a9551be769 Signed-off-by: Dave Barach <dave@barachs.net> Signed-off-by: Dave Wallace <dwallacelf@gmail.com> Signed-off-by: Florin Coras <fcoras@cisco.com>
Diffstat (limited to 'src/svm/memfd.c')
-rw-r--r--src/svm/memfd.c137
1 files changed, 137 insertions, 0 deletions
diff --git a/src/svm/memfd.c b/src/svm/memfd.c
new file mode 100644
index 00000000000..9fe487db788
--- /dev/null
+++ b/src/svm/memfd.c
@@ -0,0 +1,137 @@
+/*
+ * Copyright (c) 2017 Cisco and/or its affiliates.
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at:
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include "memfd.h"
+
+int
+memfd_master_init (memfd_private_t * memfd, u32 master_index)
+{
+ int flags;
+ memfd_shared_header_t *sh;
+ u64 ticks = clib_cpu_time_now ();
+ u64 randomize_baseva;
+ void *oldheap;
+
+ if (memfd->memfd_size == 0)
+ return MEMFD_API_ERROR_NO_SIZE;
+
+ ASSERT (vec_c_string_is_terminated (memfd->name));
+ memfd->name = format (0, "memfd svm region %d", master_index);
+
+ memfd->fd = memfd_create ((char *) memfd->name, MFD_ALLOW_SEALING);
+ if (memfd->fd < 0)
+ {
+ clib_unix_warning ("create segment '%s'", memfd->name);
+ return MEMFD_API_ERROR_CREATE_FAILURE;
+ }
+
+ if ((ftruncate (memfd->fd, memfd->memfd_size)) == -1)
+ {
+ clib_unix_warning ("set memfd size");
+ return MEMFD_API_ERROR_SET_SIZE;
+ }
+
+ if ((fcntl (memfd->fd, F_ADD_SEALS, F_SEAL_SHRINK)) == -1)
+ clib_unix_warning ("fcntl (F_ADD_SEALS, F_SEAL_SHRINK)");
+
+ flags = MAP_SHARED;
+ if (memfd->requested_va)
+ flags |= MAP_FIXED;
+
+ randomize_baseva = (ticks & 15) * MMAP_PAGESIZE;
+
+ if (memfd->requested_va)
+ memfd->requested_va += randomize_baseva;
+
+ sh = memfd->sh =
+ (memfd_shared_header_t *) mmap ((void *) memfd->requested_va,
+ memfd->memfd_size, PROT_READ | PROT_WRITE,
+ flags, memfd->fd, 0);
+
+ if (memfd->sh == MAP_FAILED)
+ {
+ clib_unix_warning ("mmap");
+ close (memfd->fd);
+ return MEMFD_API_ERROR_MMAP;
+ }
+
+ memfd->my_pid = getpid ();
+ sh->master_pid = memfd->my_pid;
+ sh->memfd_size = memfd->memfd_size;
+ sh->heap = mheap_alloc_with_flags
+ (((u8 *) sh) + MMAP_PAGESIZE, memfd->memfd_size - MMAP_PAGESIZE,
+ MHEAP_FLAG_DISABLE_VM | MHEAP_FLAG_THREAD_SAFE);
+
+ sh->memfd_va = pointer_to_uword (sh);
+ sh->master_index = master_index;
+
+ oldheap = memfd_push_heap (sh);
+ sh->name = format (0, "%s%c", memfd->name, 0);
+ memfd_pop_heap (oldheap);
+
+ memfd->i_am_master = 1;
+
+ /* The application has to set set sh->ready... */
+ return 0;
+}
+
+/*
+ * Subtly different than svm_slave_init. The caller
+ * needs to acquire a usable file descriptor for the memfd segment
+ * e.g. via vppinfra/socket.c:default_socket_recvmsg
+ */
+
+int
+memfd_slave_init (memfd_private_t * memfd)
+{
+ memfd_shared_header_t *sh;
+
+ memfd->i_am_master = 0;
+
+ /* Map the segment once, to look at the shared header */
+ sh = (void *) mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
+ memfd->fd, 0);
+ if (sh == MAP_FAILED)
+ {
+ clib_unix_warning ("slave research mmap");
+ close (memfd->fd);
+ return MEMFD_API_ERROR_MMAP;
+ }
+
+ memfd->requested_va = (u64) sh->memfd_va;
+ memfd->memfd_size = sh->memfd_size;
+ munmap (sh, MMAP_PAGESIZE);
+
+ sh = memfd->sh =
+ (void *) mmap ((void *) memfd->requested_va, memfd->memfd_size,
+ PROT_READ | PROT_WRITE,
+ MAP_SHARED | MAP_FIXED, memfd->fd, 0);
+
+ if (sh == MAP_FAILED)
+ {
+ clib_unix_warning ("slave final mmap");
+ close (memfd->fd);
+ return MEMFD_API_ERROR_MMAP;
+ }
+ sh->slave_pid = getpid ();
+ return 0;
+}
+
+/*
+ * fd.io coding-style-patch-verification: ON
+ *
+ * Local Variables:
+ * eval: (c-set-style "gnu")
+ * End:
+ */