ssvm->name must be a vector containing a c-string.
[vpp.git] / src / svm / ssvm.c
1 /*
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:
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
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.
14  */
15 #include "ssvm.h"
16
17 int
18 ssvm_master_init (ssvm_private_t * ssvm, u32 master_index)
19 {
20   int ssvm_fd;
21   u8 *ssvm_filename;
22   u8 junk = 0;
23   int flags;
24   ssvm_shared_header_t *sh;
25   u64 ticks = clib_cpu_time_now ();
26   u64 randomize_baseva;
27   void *oldheap;
28
29   if (ssvm->ssvm_size == 0)
30     return SSVM_API_ERROR_NO_SIZE;
31
32   if (CLIB_DEBUG > 1)
33     clib_warning ("[%d] creating segment '%s'", getpid (), ssvm->name);
34
35   ASSERT (vec_c_string_is_terminated (ssvm->name));
36   ssvm_filename = format (0, "/dev/shm/%s%c", ssvm->name, 0);
37
38   unlink ((char *) ssvm_filename);
39
40   vec_free (ssvm_filename);
41
42   ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR | O_CREAT | O_EXCL, 0777);
43
44   if (ssvm_fd < 0)
45     {
46       clib_unix_warning ("create segment '%s'", ssvm->name);
47       return SSVM_API_ERROR_CREATE_FAILURE;
48     }
49
50   if (lseek (ssvm_fd, ssvm->ssvm_size, SEEK_SET) < 0)
51     {
52       clib_unix_warning ("lseek");
53       close (ssvm_fd);
54       return SSVM_API_ERROR_SET_SIZE;
55     }
56
57   if (write (ssvm_fd, &junk, 1) != 1)
58     {
59       clib_unix_warning ("set ssvm size");
60       close (ssvm_fd);
61       return SSVM_API_ERROR_SET_SIZE;
62     }
63
64   flags = MAP_SHARED;
65   if (ssvm->requested_va)
66     flags |= MAP_FIXED;
67
68   randomize_baseva = (ticks & 15) * MMAP_PAGESIZE;
69
70   if (ssvm->requested_va)
71     ssvm->requested_va += randomize_baseva;
72
73   sh = ssvm->sh =
74     (ssvm_shared_header_t *) mmap ((void *) ssvm->requested_va,
75                                    ssvm->ssvm_size, PROT_READ | PROT_WRITE,
76                                    flags, ssvm_fd, 0);
77
78   if (ssvm->sh == MAP_FAILED)
79     {
80       clib_unix_warning ("mmap");
81       close (ssvm_fd);
82       return SSVM_API_ERROR_MMAP;
83     }
84
85   close (ssvm_fd);
86
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);
93
94   sh->ssvm_va = pointer_to_uword (sh);
95   sh->master_index = master_index;
96
97   oldheap = ssvm_push_heap (sh);
98   sh->name = format (0, "%s%c", ssvm->name, 0);
99   ssvm_pop_heap (oldheap);
100
101   ssvm->i_am_master = 1;
102
103   /* The application has to set set sh->ready... */
104   return 0;
105 }
106
107 int
108 ssvm_slave_init (ssvm_private_t * ssvm, int timeout_in_seconds)
109 {
110   struct stat stat;
111   int ssvm_fd = -1;
112   ssvm_shared_header_t *sh;
113
114   ASSERT (vec_c_string_is_terminated (ssvm->name));
115   ssvm->i_am_master = 0;
116
117   while (timeout_in_seconds-- > 0)
118     {
119       if (ssvm_fd < 0)
120         ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR, 0777);
121       if (ssvm_fd < 0)
122         {
123           sleep (1);
124           continue;
125         }
126       if (fstat (ssvm_fd, &stat) < 0)
127         {
128           sleep (1);
129           continue;
130         }
131
132       if (stat.st_size > 0)
133         goto map_it;
134     }
135   clib_warning ("slave timeout");
136   return SSVM_API_ERROR_SLAVE_TIMEOUT;
137
138 map_it:
139   sh = (void *) mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED,
140                       ssvm_fd, 0);
141   if (sh == MAP_FAILED)
142     {
143       clib_unix_warning ("slave research mmap");
144       close (ssvm_fd);
145       return SSVM_API_ERROR_MMAP;
146     }
147
148   while (timeout_in_seconds-- > 0)
149     {
150       if (sh->ready)
151         goto re_map_it;
152     }
153   close (ssvm_fd);
154   munmap (sh, MMAP_PAGESIZE);
155   clib_warning ("slave timeout 2");
156   return SSVM_API_ERROR_SLAVE_TIMEOUT;
157
158 re_map_it:
159   ssvm->requested_va = (u64) sh->ssvm_va;
160   ssvm->ssvm_size = sh->ssvm_size;
161   munmap (sh, MMAP_PAGESIZE);
162
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);
166
167   if (sh == MAP_FAILED)
168     {
169       clib_unix_warning ("slave final mmap");
170       close (ssvm_fd);
171       return SSVM_API_ERROR_MMAP;
172     }
173   sh->slave_pid = getpid ();
174   return 0;
175 }
176
177 void
178 ssvm_delete (ssvm_private_t * ssvm)
179 {
180   u8 *fn;
181
182   fn = format (0, "/dev/shm/%s%c", ssvm->name, 0);
183
184   if (CLIB_DEBUG > 1)
185     clib_warning ("[%d] unlinking ssvm (%s) backing file '%s'", getpid (),
186                   ssvm->name, fn);
187
188   /* Throw away the backing file */
189   if (unlink ((char *) fn) < 0)
190     clib_unix_warning ("unlink segment '%s'", ssvm->name);
191
192   vec_free (fn);
193   vec_free (ssvm->name);
194
195   munmap ((void *) ssvm->requested_va, ssvm->ssvm_size);
196 }
197
198
199 /*
200  * fd.io coding-style-patch-verification: ON
201  *
202  * Local Variables:
203  * eval: (c-set-style "gnu")
204  * End:
205  */