Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 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 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 17 | int |
| 18 | ssvm_master_init (ssvm_private_t * ssvm, u32 master_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 19 | { |
| 20 | int ssvm_fd; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 21 | u8 *ssvm_filename; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 22 | u8 junk = 0; |
| 23 | int flags; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 24 | ssvm_shared_header_t *sh; |
| 25 | u64 ticks = clib_cpu_time_now (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 26 | u64 randomize_baseva; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 27 | void *oldheap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 28 | |
| 29 | if (ssvm->ssvm_size == 0) |
| 30 | return SSVM_API_ERROR_NO_SIZE; |
| 31 | |
| 32 | ssvm_filename = format (0, "/dev/shm/%s%c", ssvm->name, 0); |
| 33 | |
| 34 | unlink ((char *) ssvm_filename); |
| 35 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 36 | vec_free (ssvm_filename); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 37 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 38 | ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR | O_CREAT | O_EXCL, 0777); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 39 | |
| 40 | if (ssvm_fd < 0) |
| 41 | { |
| 42 | clib_unix_warning ("create segment '%s'", ssvm->name); |
| 43 | return SSVM_API_ERROR_CREATE_FAILURE; |
| 44 | } |
| 45 | |
Dave Barach | 56faee8 | 2016-08-04 18:58:05 -0400 | [diff] [blame] | 46 | if (lseek (ssvm_fd, ssvm->ssvm_size, SEEK_SET) < 0) |
| 47 | { |
| 48 | clib_unix_warning ("lseek"); |
| 49 | close (ssvm_fd); |
| 50 | return SSVM_API_ERROR_SET_SIZE; |
| 51 | } |
Ed Warnicke | 853e720 | 2016-08-12 11:42:26 -0700 | [diff] [blame^] | 52 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 53 | if (write (ssvm_fd, &junk, 1) != 1) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | { |
| 55 | clib_unix_warning ("set ssvm size"); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 56 | close (ssvm_fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | return SSVM_API_ERROR_SET_SIZE; |
| 58 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 59 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 60 | flags = MAP_SHARED; |
| 61 | if (ssvm->requested_va) |
| 62 | flags |= MAP_FIXED; |
| 63 | |
| 64 | randomize_baseva = (ticks & 15) * MMAP_PAGESIZE; |
| 65 | |
| 66 | if (ssvm->requested_va) |
| 67 | ssvm->requested_va += randomize_baseva; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 68 | |
| 69 | sh = ssvm->sh = |
| 70 | (ssvm_shared_header_t *) mmap ((void *) ssvm->requested_va, |
| 71 | ssvm->ssvm_size, PROT_READ | PROT_WRITE, |
| 72 | flags, ssvm_fd, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 74 | if (ssvm->sh == MAP_FAILED) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 75 | { |
| 76 | clib_unix_warning ("mmap"); |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 77 | close (ssvm_fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 78 | return SSVM_API_ERROR_MMAP; |
| 79 | } |
| 80 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 81 | close (ssvm_fd); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 83 | ssvm->my_pid = getpid (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | sh->master_pid = ssvm->my_pid; |
| 85 | sh->ssvm_size = ssvm->ssvm_size; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 86 | sh->heap = mheap_alloc_with_flags |
| 87 | (((u8 *) sh) + MMAP_PAGESIZE, ssvm->ssvm_size - MMAP_PAGESIZE, |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 88 | MHEAP_FLAG_DISABLE_VM | MHEAP_FLAG_THREAD_SAFE); |
| 89 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 90 | sh->ssvm_va = pointer_to_uword (sh); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 91 | sh->master_index = master_index; |
| 92 | |
| 93 | oldheap = ssvm_push_heap (sh); |
| 94 | sh->name = format (0, "%s%c", ssvm->name, 0); |
| 95 | ssvm_pop_heap (oldheap); |
| 96 | |
| 97 | ssvm->i_am_master = 1; |
| 98 | |
| 99 | /* The application has to set set sh->ready... */ |
| 100 | return 0; |
| 101 | } |
| 102 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 103 | int |
| 104 | ssvm_slave_init (ssvm_private_t * ssvm, int timeout_in_seconds) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 105 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 106 | struct stat stat; |
| 107 | int ssvm_fd = -1; |
| 108 | ssvm_shared_header_t *sh; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 109 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 110 | ssvm->i_am_master = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 111 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 112 | while (timeout_in_seconds-- > 0) |
| 113 | { |
| 114 | if (ssvm_fd < 0) |
| 115 | ssvm_fd = shm_open ((char *) ssvm->name, O_RDWR, 0777); |
| 116 | if (ssvm_fd < 0) |
| 117 | { |
| 118 | sleep (1); |
| 119 | continue; |
| 120 | } |
| 121 | if (fstat (ssvm_fd, &stat) < 0) |
| 122 | { |
| 123 | sleep (1); |
| 124 | continue; |
| 125 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 126 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 127 | if (stat.st_size > 0) |
| 128 | goto map_it; |
| 129 | } |
| 130 | clib_warning ("slave timeout"); |
| 131 | return SSVM_API_ERROR_SLAVE_TIMEOUT; |
| 132 | |
| 133 | map_it: |
| 134 | sh = (void *) mmap (0, MMAP_PAGESIZE, PROT_READ | PROT_WRITE, MAP_SHARED, |
| 135 | ssvm_fd, 0); |
| 136 | if (sh == MAP_FAILED) |
| 137 | { |
| 138 | clib_unix_warning ("slave research mmap"); |
| 139 | close (ssvm_fd); |
| 140 | return SSVM_API_ERROR_MMAP; |
| 141 | } |
| 142 | |
| 143 | while (timeout_in_seconds-- > 0) |
| 144 | { |
| 145 | if (sh->ready) |
| 146 | goto re_map_it; |
| 147 | } |
| 148 | close (ssvm_fd); |
| 149 | munmap (sh, MMAP_PAGESIZE); |
| 150 | clib_warning ("slave timeout 2"); |
| 151 | return SSVM_API_ERROR_SLAVE_TIMEOUT; |
| 152 | |
| 153 | re_map_it: |
| 154 | ssvm->requested_va = (u64) sh->ssvm_va; |
| 155 | ssvm->ssvm_size = sh->ssvm_size; |
| 156 | munmap (sh, MMAP_PAGESIZE); |
| 157 | |
| 158 | sh = ssvm->sh = (void *) mmap ((void *) ssvm->requested_va, ssvm->ssvm_size, |
| 159 | PROT_READ | PROT_WRITE, |
| 160 | MAP_SHARED | MAP_FIXED, ssvm_fd, 0); |
| 161 | |
| 162 | if (sh == MAP_FAILED) |
| 163 | { |
| 164 | clib_unix_warning ("slave final mmap"); |
| 165 | close (ssvm_fd); |
| 166 | return SSVM_API_ERROR_MMAP; |
| 167 | } |
| 168 | sh->slave_pid = getpid (); |
| 169 | return 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 170 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | * fd.io coding-style-patch-verification: ON |
| 174 | * |
| 175 | * Local Variables: |
| 176 | * eval: (c-set-style "gnu") |
| 177 | * End: |
| 178 | */ |