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.
18 ssvm_master_init (ssvm_private_t * ssvm, u32 master_index)
24 ssvm_shared_header_t *sh;
25 u64 ticks = clib_cpu_time_now ();
29 if (ssvm->ssvm_size == 0)
30 return SSVM_API_ERROR_NO_SIZE;
33 clib_warning ("[%d] creating segment '%s'", getpid (), ssvm->name);
35 ssvm_filename = format (0, "/dev/shm/%s%c", ssvm->name, 0);
37 unlink ((char *) ssvm_filename);
39 vec_free (ssvm_filename);
41 ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR | O_CREAT | O_EXCL, 0777);
45 clib_unix_warning ("create segment '%s'", ssvm->name);
46 return SSVM_API_ERROR_CREATE_FAILURE;
49 if (lseek (ssvm_fd, ssvm->ssvm_size, SEEK_SET) < 0)
51 clib_unix_warning ("lseek");
53 return SSVM_API_ERROR_SET_SIZE;
56 if (write (ssvm_fd, &junk, 1) != 1)
58 clib_unix_warning ("set ssvm size");
60 return SSVM_API_ERROR_SET_SIZE;
64 if (ssvm->requested_va)
67 randomize_baseva = (ticks & 15) * MMAP_PAGESIZE;
69 if (ssvm->requested_va)
70 ssvm->requested_va += randomize_baseva;
73 (ssvm_shared_header_t *) mmap ((void *) ssvm->requested_va,
74 ssvm->ssvm_size, PROT_READ | PROT_WRITE,
77 if (ssvm->sh == MAP_FAILED)
79 clib_unix_warning ("mmap");
81 return SSVM_API_ERROR_MMAP;
86 ssvm->my_pid = getpid ();
87 sh->master_pid = ssvm->my_pid;
88 sh->ssvm_size = ssvm->ssvm_size;
89 sh->heap = mheap_alloc_with_flags
90 (((u8 *) sh) + MMAP_PAGESIZE, ssvm->ssvm_size - MMAP_PAGESIZE,
91 MHEAP_FLAG_DISABLE_VM | MHEAP_FLAG_THREAD_SAFE);
93 sh->ssvm_va = pointer_to_uword (sh);
94 sh->master_index = master_index;
96 oldheap = ssvm_push_heap (sh);
97 sh->name = format (0, "%s%c", ssvm->name, 0);
98 ssvm_pop_heap (oldheap);
100 ssvm->i_am_master = 1;
102 /* The application has to set set sh->ready... */
107 ssvm_slave_init (ssvm_private_t * ssvm, int timeout_in_seconds)
111 ssvm_shared_header_t *sh;
113 ssvm->i_am_master = 0;
115 while (timeout_in_seconds-- > 0)
118 ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR, 0777);
124 if (fstat (ssvm_fd, &stat) < 0)
130 if (stat.st_size > 0)
133 clib_warning ("slave timeout");
134 return SSVM_API_ERROR_SLAVE_TIMEOUT;
137 sh = (void *) mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
139 if (sh == MAP_FAILED)
141 clib_unix_warning ("slave research mmap");
143 return SSVM_API_ERROR_MMAP;
146 while (timeout_in_seconds-- > 0)
152 munmap (sh, MMAP_PAGESIZE);
153 clib_warning ("slave timeout 2");
154 return SSVM_API_ERROR_SLAVE_TIMEOUT;
157 ssvm->requested_va = (u64) sh->ssvm_va;
158 ssvm->ssvm_size = sh->ssvm_size;
159 munmap (sh, MMAP_PAGESIZE);
161 sh = ssvm->sh = (void *) mmap ((void *) ssvm->requested_va, ssvm->ssvm_size,
162 PROT_READ | PROT_WRITE,
163 MAP_SHARED | MAP_FIXED, ssvm_fd, 0);
165 if (sh == MAP_FAILED)
167 clib_unix_warning ("slave final mmap");
169 return SSVM_API_ERROR_MMAP;
171 sh->slave_pid = getpid ();
176 ssvm_delete (ssvm_private_t * ssvm)
180 fn = format (0, "/dev/shm/%s%c", ssvm->name, 0);
183 clib_warning ("[%d] unlinking ssvm (%s) backing file '%s'", getpid (),
186 /* Throw away the backing file */
187 if (unlink ((char *) fn) < 0)
188 clib_unix_warning ("unlink segment '%s'", ssvm->name);
191 vec_free (ssvm->name);
193 munmap ((void *) ssvm->requested_va, ssvm->ssvm_size);
198 * fd.io coding-style-patch-verification: ON
201 * eval: (c-set-style "gnu")