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 | #ifndef __included_ssvm_h__ |
| 16 | #define __included_ssvm_h__ |
| 17 | |
| 18 | #include <stdio.h> |
| 19 | #include <stdlib.h> |
| 20 | #include <sys/types.h> |
| 21 | #include <sys/mman.h> |
| 22 | #include <sys/stat.h> |
| 23 | #include <netinet/in.h> |
| 24 | #include <signal.h> |
| 25 | #include <pthread.h> |
| 26 | #include <unistd.h> |
| 27 | #include <time.h> |
| 28 | #include <fcntl.h> |
| 29 | #include <string.h> |
| 30 | #include <vppinfra/clib.h> |
| 31 | #include <vppinfra/vec.h> |
| 32 | #include <vppinfra/hash.h> |
| 33 | #include <vppinfra/bitmap.h> |
| 34 | #include <vppinfra/fifo.h> |
| 35 | #include <vppinfra/time.h> |
| 36 | #include <vppinfra/mheap.h> |
| 37 | #include <vppinfra/heap.h> |
| 38 | #include <vppinfra/pool.h> |
| 39 | #include <vppinfra/format.h> |
| 40 | |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 41 | #ifndef MMAP_PAGESIZE |
| 42 | #define MMAP_PAGESIZE (clib_mem_get_page_size()) |
| 43 | #endif |
| 44 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | #define SSVM_N_OPAQUE 7 |
| 46 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 47 | typedef struct |
| 48 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 49 | /* Spin-lock */ |
| 50 | volatile u32 lock; |
| 51 | volatile u32 owner_pid; |
| 52 | int recursion_count; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 53 | u32 tag; /* for debugging */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 54 | |
| 55 | /* The allocation arena */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 56 | void *heap; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | |
| 58 | /* Segment must be mapped at this address, or no supper */ |
| 59 | u64 ssvm_va; |
| 60 | /* The actual mmap size */ |
| 61 | u64 ssvm_size; |
| 62 | u32 master_pid; |
| 63 | u32 slave_pid; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 64 | u8 *name; |
| 65 | void *opaque[SSVM_N_OPAQUE]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 66 | |
| 67 | /* Set when the master application thinks it's time to make the donuts */ |
| 68 | volatile u32 ready; |
| 69 | |
| 70 | /* Needed to make unique MAC addresses, etc. */ |
| 71 | u32 master_index; |
| 72 | } ssvm_shared_header_t; |
| 73 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 74 | typedef struct |
| 75 | { |
| 76 | ssvm_shared_header_t *sh; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 77 | u64 ssvm_size; |
| 78 | u32 my_pid; |
| 79 | u32 vlib_hw_if_index; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 80 | u8 *name; |
Christophe Fontaine | fef15b4 | 2016-04-09 12:38:49 +0900 | [diff] [blame] | 81 | uword requested_va; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 82 | int i_am_master; |
| 83 | u32 per_interface_next_index; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 84 | u32 *rx_queue; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | } ssvm_private_t; |
| 86 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 87 | always_inline void |
| 88 | ssvm_lock (ssvm_shared_header_t * h, u32 my_pid, u32 tag) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 89 | { |
| 90 | if (h->owner_pid == my_pid) |
| 91 | { |
| 92 | h->recursion_count++; |
| 93 | return; |
| 94 | } |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 95 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 96 | while (__sync_lock_test_and_set (&h->lock, 1)) |
| 97 | ; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 98 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | h->owner_pid = my_pid; |
| 100 | h->recursion_count = 1; |
| 101 | h->tag = tag; |
| 102 | } |
| 103 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 104 | always_inline void |
| 105 | ssvm_unlock (ssvm_shared_header_t * h) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | { |
| 107 | if (--h->recursion_count == 0) |
| 108 | { |
| 109 | h->owner_pid = 0; |
| 110 | h->tag = 0; |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 111 | CLIB_MEMORY_BARRIER (); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 112 | h->lock = 0; |
| 113 | } |
| 114 | } |
| 115 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 116 | static inline void * |
| 117 | ssvm_push_heap (ssvm_shared_header_t * sh) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 118 | { |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 119 | u8 *oldheap; |
| 120 | oldheap = clib_mem_set_heap (sh->heap); |
| 121 | return ((void *) oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 124 | static inline void |
| 125 | ssvm_pop_heap (void *oldheap) |
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 | clib_mem_set_heap (oldheap); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | #define foreach_ssvm_api_error \ |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 131 | _(NO_NAME, "No shared segment name", -100) \ |
| 132 | _(NO_SIZE, "Size not set (master)", -101) \ |
| 133 | _(CREATE_FAILURE, "Create failed", -102) \ |
| 134 | _(SET_SIZE, "Set size failed", -103) \ |
| 135 | _(MMAP, "mmap failed", -104) \ |
| 136 | _(SLAVE_TIMEOUT, "Slave map timeout", -105) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 137 | |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 138 | typedef enum |
| 139 | { |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 140 | #define _(n,s,c) SSVM_API_ERROR_##n = c, |
| 141 | foreach_ssvm_api_error |
| 142 | #undef _ |
| 143 | } ssvm_api_error_enum_t; |
| 144 | |
| 145 | #define SSVM_API_ERROR_NO_NAME (-10) |
| 146 | |
| 147 | int ssvm_master_init (ssvm_private_t * ssvm, u32 master_index); |
| 148 | int ssvm_slave_init (ssvm_private_t * ssvm, int timeout_in_seconds); |
Dave Barach | 68b0fb0 | 2017-02-28 15:15:56 -0500 | [diff] [blame] | 149 | void ssvm_delete (ssvm_private_t * ssvm); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 150 | |
| 151 | #endif /* __included_ssvm_h__ */ |
Dave Barach | 8a7fb0c | 2016-07-08 14:44:23 -0400 | [diff] [blame] | 152 | |
| 153 | /* |
| 154 | * fd.io coding-style-patch-verification: ON |
| 155 | * |
| 156 | * Local Variables: |
| 157 | * eval: (c-set-style "gnu") |
| 158 | * End: |
| 159 | */ |