blob: 8466e15579b590dedfc01cb271e25fd011949278 [file] [log] [blame]
Ed Warnickecb9cada2015-12-08 15:45:58 -07001/*
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 Barach68b0fb02017-02-28 15:15:56 -050041#ifndef MMAP_PAGESIZE
42#define MMAP_PAGESIZE (clib_mem_get_page_size())
43#endif
44
Ed Warnickecb9cada2015-12-08 15:45:58 -070045#define SSVM_N_OPAQUE 7
46
Dave Barach8a7fb0c2016-07-08 14:44:23 -040047typedef struct
48{
Ed Warnickecb9cada2015-12-08 15:45:58 -070049 /* Spin-lock */
50 volatile u32 lock;
51 volatile u32 owner_pid;
52 int recursion_count;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040053 u32 tag; /* for debugging */
Ed Warnickecb9cada2015-12-08 15:45:58 -070054
55 /* The allocation arena */
Dave Barach8a7fb0c2016-07-08 14:44:23 -040056 void *heap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070057
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 Barach8a7fb0c2016-07-08 14:44:23 -040064 u8 *name;
65 void *opaque[SSVM_N_OPAQUE];
Ed Warnickecb9cada2015-12-08 15:45:58 -070066
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 Barach8a7fb0c2016-07-08 14:44:23 -040074typedef struct
75{
76 ssvm_shared_header_t *sh;
Ed Warnickecb9cada2015-12-08 15:45:58 -070077 u64 ssvm_size;
78 u32 my_pid;
79 u32 vlib_hw_if_index;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040080 u8 *name;
Christophe Fontainefef15b42016-04-09 12:38:49 +090081 uword requested_va;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082 int i_am_master;
83 u32 per_interface_next_index;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040084 u32 *rx_queue;
Ed Warnickecb9cada2015-12-08 15:45:58 -070085} ssvm_private_t;
86
Dave Barach8a7fb0c2016-07-08 14:44:23 -040087always_inline void
88ssvm_lock (ssvm_shared_header_t * h, u32 my_pid, u32 tag)
Ed Warnickecb9cada2015-12-08 15:45:58 -070089{
90 if (h->owner_pid == my_pid)
91 {
92 h->recursion_count++;
93 return;
94 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -040095
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 while (__sync_lock_test_and_set (&h->lock, 1))
97 ;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040098
Ed Warnickecb9cada2015-12-08 15:45:58 -070099 h->owner_pid = my_pid;
100 h->recursion_count = 1;
101 h->tag = tag;
102}
103
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400104always_inline void
Dave Barach2c25a622017-06-26 11:35:07 -0400105ssvm_lock_non_recursive (ssvm_shared_header_t * h, u32 tag)
106{
107 while (__sync_lock_test_and_set (&h->lock, 1))
108 ;
109
110 h->tag = tag;
111}
112
113always_inline void
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400114ssvm_unlock (ssvm_shared_header_t * h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115{
116 if (--h->recursion_count == 0)
117 {
118 h->owner_pid = 0;
119 h->tag = 0;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400120 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700121 h->lock = 0;
122 }
123}
124
Dave Barach2c25a622017-06-26 11:35:07 -0400125always_inline void
126ssvm_unlock_non_recursive (ssvm_shared_header_t * h)
127{
128 h->tag = 0;
129 CLIB_MEMORY_BARRIER ();
130 h->lock = 0;
131}
132
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400133static inline void *
134ssvm_push_heap (ssvm_shared_header_t * sh)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700135{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400136 u8 *oldheap;
137 oldheap = clib_mem_set_heap (sh->heap);
138 return ((void *) oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700139}
140
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400141static inline void
142ssvm_pop_heap (void *oldheap)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700143{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400144 clib_mem_set_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700145}
146
147#define foreach_ssvm_api_error \
Dave Barach68b0fb02017-02-28 15:15:56 -0500148_(NO_NAME, "No shared segment name", -100) \
149_(NO_SIZE, "Size not set (master)", -101) \
150_(CREATE_FAILURE, "Create failed", -102) \
151_(SET_SIZE, "Set size failed", -103) \
152_(MMAP, "mmap failed", -104) \
153_(SLAVE_TIMEOUT, "Slave map timeout", -105)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700154
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400155typedef enum
156{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700157#define _(n,s,c) SSVM_API_ERROR_##n = c,
158 foreach_ssvm_api_error
159#undef _
160} ssvm_api_error_enum_t;
161
162#define SSVM_API_ERROR_NO_NAME (-10)
163
164int ssvm_master_init (ssvm_private_t * ssvm, u32 master_index);
165int ssvm_slave_init (ssvm_private_t * ssvm, int timeout_in_seconds);
Dave Barach68b0fb02017-02-28 15:15:56 -0500166void ssvm_delete (ssvm_private_t * ssvm);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700167
168#endif /* __included_ssvm_h__ */
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400169
170/*
171 * fd.io coding-style-patch-verification: ON
172 *
173 * Local Variables:
174 * eval: (c-set-style "gnu")
175 * End:
176 */