2 * Copyright (c) 2015 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_common.h"
19 ssvm_master_init (ssvm_private_t * ssvm, u32 master_index)
21 svm_main_region_t *smr = svm_get_root_rp ()->data_base;
26 ssvm_shared_header_t *sh;
27 u64 ticks = clib_cpu_time_now ();
31 if (ssvm->ssvm_size == 0)
32 return SSVM_API_ERROR_NO_SIZE;
35 clib_warning ("[%d] creating segment '%s'", getpid (), ssvm->name);
37 ASSERT (vec_c_string_is_terminated (ssvm->name));
38 ssvm_filename = format (0, "/dev/shm/%s%c", ssvm->name, 0);
40 unlink ((char *) ssvm_filename);
42 vec_free (ssvm_filename);
44 ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR | O_CREAT | O_EXCL, 0777);
48 clib_unix_warning ("create segment '%s'", ssvm->name);
49 return SSVM_API_ERROR_CREATE_FAILURE;
52 if (fchmod (ssvm_fd, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP) < 0)
53 clib_unix_warning ("ssvm segment chmod");
54 if (fchown (ssvm_fd, smr->uid, smr->gid) < 0)
55 clib_unix_warning ("ssvm segment chown");
57 if (lseek (ssvm_fd, ssvm->ssvm_size, SEEK_SET) < 0)
59 clib_unix_warning ("lseek");
61 return SSVM_API_ERROR_SET_SIZE;
64 if (write (ssvm_fd, &junk, 1) != 1)
66 clib_unix_warning ("set ssvm size");
68 return SSVM_API_ERROR_SET_SIZE;
72 if (ssvm->requested_va)
75 randomize_baseva = (ticks & 15) * MMAP_PAGESIZE;
77 if (ssvm->requested_va)
78 ssvm->requested_va += randomize_baseva;
81 (ssvm_shared_header_t *) mmap ((void *) ssvm->requested_va,
82 ssvm->ssvm_size, PROT_READ | PROT_WRITE,
85 if (ssvm->sh == MAP_FAILED)
87 clib_unix_warning ("mmap");
89 return SSVM_API_ERROR_MMAP;
94 ssvm->my_pid = getpid ();
95 sh->master_pid = ssvm->my_pid;
96 sh->ssvm_size = ssvm->ssvm_size;
97 sh->heap = mheap_alloc_with_flags
98 (((u8 *) sh) + MMAP_PAGESIZE, ssvm->ssvm_size - MMAP_PAGESIZE,
99 MHEAP_FLAG_DISABLE_VM | MHEAP_FLAG_THREAD_SAFE);
101 sh->ssvm_va = pointer_to_uword (sh);
102 sh->master_index = master_index;
104 oldheap = ssvm_push_heap (sh);
105 sh->name = format (0, "%s%c", ssvm->name, 0);
106 ssvm_pop_heap (oldheap);
108 ssvm->i_am_master = 1;
110 /* The application has to set set sh->ready... */
115 ssvm_slave_init (ssvm_private_t * ssvm, int timeout_in_seconds)
119 ssvm_shared_header_t *sh;
121 ASSERT (vec_c_string_is_terminated (ssvm->name));
122 ssvm->i_am_master = 0;
124 while (timeout_in_seconds-- > 0)
127 ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR, 0777);
133 if (fstat (ssvm_fd, &stat) < 0)
139 if (stat.st_size > 0)
142 clib_warning ("slave timeout");
143 return SSVM_API_ERROR_SLAVE_TIMEOUT;
146 sh = (void *) mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
148 if (sh == MAP_FAILED)
150 clib_unix_warning ("slave research mmap");
152 return SSVM_API_ERROR_MMAP;
155 while (timeout_in_seconds-- > 0)
161 munmap (sh, MMAP_PAGESIZE);
162 clib_warning ("slave timeout 2");
163 return SSVM_API_ERROR_SLAVE_TIMEOUT;
166 ssvm->requested_va = (u64) sh->ssvm_va;
167 ssvm->ssvm_size = sh->ssvm_size;
168 munmap (sh, MMAP_PAGESIZE);
170 sh = ssvm->sh = (void *) mmap ((void *) ssvm->requested_va, ssvm->ssvm_size,
171 PROT_READ | PROT_WRITE,
172 MAP_SHARED | MAP_FIXED, ssvm_fd, 0);
174 if (sh == MAP_FAILED)
176 clib_unix_warning ("slave final mmap");
178 return SSVM_API_ERROR_MMAP;
180 sh->slave_pid = getpid ();
185 ssvm_delete (ssvm_private_t * ssvm)
189 fn = format (0, "/dev/shm/%s%c", ssvm->name, 0);
192 clib_warning ("[%d] unlinking ssvm (%s) backing file '%s'", getpid (),
195 /* Throw away the backing file */
196 if (unlink ((char *) fn) < 0)
197 clib_unix_warning ("unlink segment '%s'", ssvm->name);
200 vec_free (ssvm->name);
202 munmap ((void *) ssvm->requested_va, ssvm->ssvm_size);
207 * fd.io coding-style-patch-verification: ON
210 * eval: (c-set-style "gnu")