2 * Copyright (c) 2015-2019 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 #include <svm/svm_common.h>
18 typedef int (*init_fn) (ssvm_private_t *);
19 typedef void (*delete_fn) (ssvm_private_t *);
21 static init_fn master_init_fns[SSVM_N_SEGMENT_TYPES] =
22 { ssvm_master_init_shm, ssvm_master_init_memfd, ssvm_master_init_private };
23 static init_fn slave_init_fns[SSVM_N_SEGMENT_TYPES] =
24 { ssvm_slave_init_shm, ssvm_slave_init_memfd, ssvm_slave_init_private };
25 static delete_fn delete_fns[SSVM_N_SEGMENT_TYPES] =
26 { ssvm_delete_shm, ssvm_delete_memfd, ssvm_delete_private };
29 ssvm_master_init_shm (ssvm_private_t * ssvm)
32 clib_mem_vm_map_t mapa = { 0 };
33 u8 junk = 0, *ssvm_filename;
34 ssvm_shared_header_t *sh;
35 uword page_size, requested_va = 0;
38 if (ssvm->ssvm_size == 0)
39 return SSVM_API_ERROR_NO_SIZE;
42 clib_warning ("[%d] creating segment '%s'", getpid (), ssvm->name);
44 ASSERT (vec_c_string_is_terminated (ssvm->name));
45 ssvm_filename = format (0, "/dev/shm/%s%c", ssvm->name, 0);
46 unlink ((char *) ssvm_filename);
47 vec_free (ssvm_filename);
49 ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR | O_CREAT | O_EXCL, 0777);
52 clib_unix_warning ("create segment '%s'", ssvm->name);
53 return SSVM_API_ERROR_CREATE_FAILURE;
56 if (fchmod (ssvm_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0)
57 clib_unix_warning ("ssvm segment chmod");
58 if (svm_get_root_rp ())
60 /* TODO: is this really needed? */
61 svm_main_region_t *smr = svm_get_root_rp ()->data_base;
62 if (fchown (ssvm_fd, smr->uid, smr->gid) < 0)
63 clib_unix_warning ("ssvm segment chown");
66 if (lseek (ssvm_fd, ssvm->ssvm_size, SEEK_SET) < 0)
68 clib_unix_warning ("lseek");
70 return SSVM_API_ERROR_SET_SIZE;
73 if (write (ssvm_fd, &junk, 1) != 1)
75 clib_unix_warning ("set ssvm size");
77 return SSVM_API_ERROR_SET_SIZE;
80 page_size = clib_mem_get_fd_page_size (ssvm_fd);
81 if (ssvm->requested_va)
83 requested_va = ssvm->requested_va;
84 clib_mem_vm_randomize_va (&requested_va, min_log2 (page_size));
87 mapa.requested_va = requested_va;
88 mapa.size = ssvm->ssvm_size;
90 mapa.numa_node = ssvm->numa;
91 if (clib_mem_vm_ext_map (&mapa))
93 clib_unix_warning ("mmap");
95 return SSVM_API_ERROR_MMAP;
100 sh->master_pid = ssvm->my_pid;
101 sh->ssvm_size = ssvm->ssvm_size;
102 sh->ssvm_va = pointer_to_uword (sh);
103 sh->type = SSVM_SEGMENT_SHM;
104 sh->heap = create_mspace_with_base (((u8 *) sh) + page_size,
105 ssvm->ssvm_size - page_size,
107 mspace_disable_expand (sh->heap);
109 oldheap = ssvm_push_heap (sh);
110 sh->name = format (0, "%s", ssvm->name, 0);
111 ssvm_pop_heap (oldheap);
114 ssvm->my_pid = getpid ();
115 ssvm->i_am_master = 1;
117 /* The application has to set set sh->ready... */
122 ssvm_slave_init_shm (ssvm_private_t * ssvm)
126 ssvm_shared_header_t *sh;
128 ASSERT (vec_c_string_is_terminated (ssvm->name));
129 ssvm->i_am_master = 0;
131 while (ssvm->attach_timeout-- > 0)
134 ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR, 0777);
140 if (fstat (ssvm_fd, &stat) < 0)
146 if (stat.st_size > 0)
149 clib_warning ("slave timeout");
150 return SSVM_API_ERROR_SLAVE_TIMEOUT;
153 sh = (void *) mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
155 if (sh == MAP_FAILED)
157 clib_unix_warning ("slave research mmap");
159 return SSVM_API_ERROR_MMAP;
162 while (ssvm->attach_timeout-- > 0)
168 munmap (sh, MMAP_PAGESIZE);
169 clib_warning ("slave timeout 2");
170 return SSVM_API_ERROR_SLAVE_TIMEOUT;
173 ssvm->requested_va = sh->ssvm_va;
174 ssvm->ssvm_size = sh->ssvm_size;
175 munmap (sh, MMAP_PAGESIZE);
177 sh = ssvm->sh = (void *) mmap ((void *) ssvm->requested_va, ssvm->ssvm_size,
178 PROT_READ | PROT_WRITE,
179 MAP_SHARED | MAP_FIXED, ssvm_fd, 0);
181 if (sh == MAP_FAILED)
183 clib_unix_warning ("slave final mmap");
185 return SSVM_API_ERROR_MMAP;
187 sh->slave_pid = getpid ();
192 ssvm_delete_shm (ssvm_private_t * ssvm)
196 fn = format (0, "/dev/shm/%s%c", ssvm->name, 0);
199 clib_warning ("[%d] unlinking ssvm (%s) backing file '%s'", getpid (),
202 /* Throw away the backing file */
203 if (unlink ((char *) fn) < 0)
204 clib_unix_warning ("unlink segment '%s'", ssvm->name);
207 vec_free (ssvm->name);
209 munmap ((void *) ssvm->sh, ssvm->ssvm_size);
213 * Initialize memfd segment master
216 ssvm_master_init_memfd (ssvm_private_t * memfd)
219 ssvm_shared_header_t *sh;
221 clib_mem_vm_alloc_t alloc = { 0 };
224 if (memfd->ssvm_size == 0)
225 return SSVM_API_ERROR_NO_SIZE;
227 ASSERT (vec_c_string_is_terminated (memfd->name));
229 alloc.name = (char *) memfd->name;
230 alloc.size = memfd->ssvm_size;
231 alloc.flags = CLIB_MEM_VM_F_SHARED;
232 alloc.requested_va = memfd->requested_va;
235 alloc.numa_node = memfd->numa;
236 alloc.flags |= CLIB_MEM_VM_F_NUMA_PREFER;
238 if ((err = clib_mem_vm_ext_alloc (&alloc)))
240 clib_error_report (err);
241 return SSVM_API_ERROR_CREATE_FAILURE;
244 memfd->fd = alloc.fd;
245 memfd->sh = (ssvm_shared_header_t *) alloc.addr;
246 memfd->my_pid = getpid ();
247 memfd->i_am_master = 1;
249 page_size = 1ull << alloc.log2_page_size;
251 sh->master_pid = memfd->my_pid;
252 sh->ssvm_size = memfd->ssvm_size;
253 sh->ssvm_va = pointer_to_uword (sh);
254 sh->type = SSVM_SEGMENT_MEMFD;
256 sh->heap = create_mspace_with_base (((u8 *) sh) + page_size,
257 memfd->ssvm_size - page_size,
259 mspace_disable_expand (sh->heap);
260 oldheap = ssvm_push_heap (sh);
261 sh->name = format (0, "%s", memfd->name, 0);
262 ssvm_pop_heap (oldheap);
264 /* The application has to set set sh->ready... */
269 * Initialize memfd segment slave
271 * Subtly different than svm_slave_init. The caller needs to acquire
272 * a usable file descriptor for the memfd segment e.g. via
273 * vppinfra/socket.c:default_socket_recvmsg
276 ssvm_slave_init_memfd (ssvm_private_t * memfd)
278 clib_mem_vm_map_t mapa = { 0 };
279 ssvm_shared_header_t *sh;
282 memfd->i_am_master = 0;
284 page_size = clib_mem_get_fd_page_size (memfd->fd);
287 clib_unix_warning ("page size unknown");
288 return SSVM_API_ERROR_MMAP;
292 * Map the segment once, to look at the shared header
295 mapa.size = page_size;
297 if (clib_mem_vm_ext_map (&mapa))
299 clib_unix_warning ("slave research mmap (fd %d)", mapa.fd);
301 return SSVM_API_ERROR_MMAP;
305 memfd->requested_va = sh->ssvm_va;
306 memfd->ssvm_size = sh->ssvm_size;
307 clib_mem_vm_free (sh, page_size);
310 * Remap the segment at the 'right' address
312 mapa.requested_va = memfd->requested_va;
313 mapa.size = memfd->ssvm_size;
314 if (clib_mem_vm_ext_map (&mapa))
316 clib_unix_warning ("slave final mmap");
318 return SSVM_API_ERROR_MMAP;
322 sh->slave_pid = getpid ();
328 ssvm_delete_memfd (ssvm_private_t * memfd)
330 vec_free (memfd->name);
331 clib_mem_vm_free (memfd->sh, memfd->ssvm_size);
336 * Initialize segment in a private heap
339 ssvm_master_init_private (ssvm_private_t * ssvm)
341 uword pagesize = clib_mem_get_page_size (), rnd_size = 0;
342 clib_mem_vm_alloc_t alloc = { 0 };
343 struct dlmallinfo dlminfo;
344 ssvm_shared_header_t *sh;
348 rnd_size = clib_max (ssvm->ssvm_size + (pagesize - 1), ssvm->ssvm_size);
349 rnd_size &= ~(pagesize - 1);
351 alloc.name = (char *) ssvm->name;
352 alloc.size = rnd_size + pagesize;
355 alloc.numa_node = ssvm->numa;
356 alloc.flags |= CLIB_MEM_VM_F_NUMA_PREFER;
359 if ((err = clib_mem_vm_ext_alloc (&alloc)))
361 clib_error_report (err);
362 return SSVM_API_ERROR_CREATE_FAILURE;
365 heap = create_mspace_with_base ((u8 *) alloc.addr + pagesize, rnd_size,
369 clib_unix_warning ("mheap alloc");
373 mspace_disable_expand (heap);
375 /* Find actual size because mspace size is rounded up by dlmalloc */
376 dlminfo = mspace_mallinfo (heap);
377 rnd_size = dlminfo.fordblks;
379 ssvm->ssvm_size = rnd_size;
380 ssvm->i_am_master = 1;
381 ssvm->my_pid = getpid ();
382 ssvm->requested_va = ~0;
384 /* First page in allocated memory is set aside for the shared header */
388 clib_memset (sh, 0, sizeof (*sh));
390 sh->ssvm_size = rnd_size;
391 sh->ssvm_va = pointer_to_uword (heap);
392 sh->type = SSVM_SEGMENT_PRIVATE;
393 sh->name = ssvm->name;
399 ssvm_slave_init_private (ssvm_private_t * ssvm)
401 clib_warning ("BUG: this should not be called!");
406 ssvm_delete_private (ssvm_private_t * ssvm)
408 vec_free (ssvm->name);
409 destroy_mspace (ssvm->sh->heap);
410 clib_mem_vm_free (ssvm->sh, ssvm->ssvm_size + clib_mem_get_page_size ());
414 ssvm_master_init (ssvm_private_t * ssvm, ssvm_segment_type_t type)
416 return (master_init_fns[type]) (ssvm);
420 ssvm_slave_init (ssvm_private_t * ssvm, ssvm_segment_type_t type)
422 return (slave_init_fns[type]) (ssvm);
426 ssvm_delete (ssvm_private_t * ssvm)
428 delete_fns[ssvm->sh->type] (ssvm);
432 ssvm_type (const ssvm_private_t * ssvm)
434 return ssvm->sh->type;
438 ssvm_name (const ssvm_private_t * ssvm)
440 return ssvm->sh->name;
444 * fd.io coding-style-patch-verification: ON
447 * eval: (c-set-style "gnu")