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