aboutsummaryrefslogtreecommitdiffstats
path: root/src/svm/memfd.c
blob: 9fe487db788a97b2cc0310c536ede94fc64291b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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:
 */