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 ASSERT (vec_c_string_is_terminated (ssvm->name));
36 ssvm_filename = format (0, "/dev/shm/%s%c", ssvm->name, 0);
38 unlink ((char *) ssvm_filename);
40 vec_free (ssvm_filename);
42 ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR | O_CREAT | O_EXCL, 0777);
46 clib_unix_warning ("create segment '%s'", ssvm->name);
47 return SSVM_API_ERROR_CREATE_FAILURE;
50 if (lseek (ssvm_fd, ssvm->ssvm_size, SEEK_SET) < 0)
52 clib_unix_warning ("lseek");
54 return SSVM_API_ERROR_SET_SIZE;
57 if (write (ssvm_fd, &junk, 1) != 1)
59 clib_unix_warning ("set ssvm size");
61 return SSVM_API_ERROR_SET_SIZE;
65 if (ssvm->requested_va)
68 randomize_baseva = (ticks & 15) * MMAP_PAGESIZE;
70 if (ssvm->requested_va)
71 ssvm->requested_va += randomize_baseva;
74 (ssvm_shared_header_t *) mmap ((void *) ssvm->requested_va,
75 ssvm->ssvm_size, PROT_READ | PROT_WRITE,
78 if (ssvm->sh == MAP_FAILED)
80 clib_unix_warning ("mmap");
82 return SSVM_API_ERROR_MMAP;
87 ssvm->my_pid = getpid ();
88 sh->master_pid = ssvm->my_pid;
89 sh->ssvm_size = ssvm->ssvm_size;
90 sh->heap = mheap_alloc_with_flags
91 (((u8 *) sh) + MMAP_PAGESIZE, ssvm->ssvm_size - MMAP_PAGESIZE,
92 MHEAP_FLAG_DISABLE_VM | MHEAP_FLAG_THREAD_SAFE);
94 sh->ssvm_va = pointer_to_uword (sh);
95 sh->master_index = master_index;
97 oldheap = ssvm_push_heap (sh);
98 sh->name = format (0, "%s%c", ssvm->name, 0);
99 ssvm_pop_heap (oldheap);
101 ssvm->i_am_master = 1;
103 /* The application has to set set sh->ready... */
108 ssvm_slave_init (ssvm_private_t * ssvm, int timeout_in_seconds)
112 ssvm_shared_header_t *sh;
114 ASSERT (vec_c_string_is_terminated (ssvm->name));
115 ssvm->i_am_master = 0;
117 while (timeout_in_seconds-- > 0)
120 ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR, 0777);
126 if (fstat (ssvm_fd, &stat) < 0)
132 if (stat.st_size > 0)
135 clib_warning ("slave timeout");
136 return SSVM_API_ERROR_SLAVE_TIMEOUT;
139 sh = (void *) mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
141 if (sh == MAP_FAILED)
143 clib_unix_warning ("slave research mmap");
145 return SSVM_API_ERROR_MMAP;
148 while (timeout_in_seconds-- > 0)
154 munmap (sh, MMAP_PAGESIZE);
155 clib_warning ("slave timeout 2");
156 return SSVM_API_ERROR_SLAVE_TIMEOUT;
159 ssvm->requested_va = (u64) sh->ssvm_va;
160 ssvm->ssvm_size = sh->ssvm_size;
161 munmap (sh, MMAP_PAGESIZE);
163 sh = ssvm->sh = (void *) mmap ((void *) ssvm->requested_va, ssvm->ssvm_size,
164 PROT_READ | PROT_WRITE,
165 MAP_SHARED | MAP_FIXED, ssvm_fd, 0);
167 if (sh == MAP_FAILED)
169 clib_unix_warning ("slave final mmap");
171 return SSVM_API_ERROR_MMAP;
173 sh->slave_pid = getpid ();
178 ssvm_delete (ssvm_private_t * ssvm)
182 fn = format (0, "/dev/shm/%s%c", ssvm->name, 0);
185 clib_warning ("[%d] unlinking ssvm (%s) backing file '%s'", getpid (),
188 /* Throw away the backing file */
189 if (unlink ((char *) fn) < 0)
190 clib_unix_warning ("unlink segment '%s'", ssvm->name);
193 vec_free (ssvm->name);
195 munmap ((void *) ssvm->requested_va, ssvm->ssvm_size);
200 * fd.io coding-style-patch-verification: ON
203 * eval: (c-set-style "gnu")