blob: 9e61b9a0827afda576eec55401a259d91f9a4c4e [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
41#define MMAP_PAGESIZE (4<<10)
42#define SSVM_N_OPAQUE 7
43
Dave Barach8a7fb0c2016-07-08 14:44:23 -040044typedef struct
45{
Ed Warnickecb9cada2015-12-08 15:45:58 -070046 /* Spin-lock */
47 volatile u32 lock;
48 volatile u32 owner_pid;
49 int recursion_count;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040050 u32 tag; /* for debugging */
Ed Warnickecb9cada2015-12-08 15:45:58 -070051
52 /* The allocation arena */
Dave Barach8a7fb0c2016-07-08 14:44:23 -040053 void *heap;
Ed Warnickecb9cada2015-12-08 15:45:58 -070054
55 /* Segment must be mapped at this address, or no supper */
56 u64 ssvm_va;
57 /* The actual mmap size */
58 u64 ssvm_size;
59 u32 master_pid;
60 u32 slave_pid;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040061 u8 *name;
62 void *opaque[SSVM_N_OPAQUE];
Ed Warnickecb9cada2015-12-08 15:45:58 -070063
64 /* Set when the master application thinks it's time to make the donuts */
65 volatile u32 ready;
66
67 /* Needed to make unique MAC addresses, etc. */
68 u32 master_index;
69} ssvm_shared_header_t;
70
Dave Barach8a7fb0c2016-07-08 14:44:23 -040071typedef struct
72{
73 ssvm_shared_header_t *sh;
Ed Warnickecb9cada2015-12-08 15:45:58 -070074 u64 ssvm_size;
75 u32 my_pid;
76 u32 vlib_hw_if_index;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040077 u8 *name;
Christophe Fontainefef15b42016-04-09 12:38:49 +090078 uword requested_va;
Ed Warnickecb9cada2015-12-08 15:45:58 -070079 int i_am_master;
80 u32 per_interface_next_index;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040081 u32 *rx_queue;
Ed Warnickecb9cada2015-12-08 15:45:58 -070082} ssvm_private_t;
83
Dave Barach8a7fb0c2016-07-08 14:44:23 -040084always_inline void
85ssvm_lock (ssvm_shared_header_t * h, u32 my_pid, u32 tag)
Ed Warnickecb9cada2015-12-08 15:45:58 -070086{
87 if (h->owner_pid == my_pid)
88 {
89 h->recursion_count++;
90 return;
91 }
Dave Barach8a7fb0c2016-07-08 14:44:23 -040092
Ed Warnickecb9cada2015-12-08 15:45:58 -070093 while (__sync_lock_test_and_set (&h->lock, 1))
94 ;
Dave Barach8a7fb0c2016-07-08 14:44:23 -040095
Ed Warnickecb9cada2015-12-08 15:45:58 -070096 h->owner_pid = my_pid;
97 h->recursion_count = 1;
98 h->tag = tag;
99}
100
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400101always_inline void
102ssvm_unlock (ssvm_shared_header_t * h)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700103{
104 if (--h->recursion_count == 0)
105 {
106 h->owner_pid = 0;
107 h->tag = 0;
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400108 CLIB_MEMORY_BARRIER ();
Ed Warnickecb9cada2015-12-08 15:45:58 -0700109 h->lock = 0;
110 }
111}
112
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400113static inline void *
114ssvm_push_heap (ssvm_shared_header_t * sh)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700115{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400116 u8 *oldheap;
117 oldheap = clib_mem_set_heap (sh->heap);
118 return ((void *) oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700119}
120
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400121static inline void
122ssvm_pop_heap (void *oldheap)
Ed Warnickecb9cada2015-12-08 15:45:58 -0700123{
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400124 clib_mem_set_heap (oldheap);
Ed Warnickecb9cada2015-12-08 15:45:58 -0700125}
126
127#define foreach_ssvm_api_error \
128_(NO_NAME, "No shared segment name", -10) \
129_(NO_SIZE, "Size not set (master)", -11) \
130_(CREATE_FAILURE, "Create failed", -12) \
131_(SET_SIZE, "Set size failed", -13) \
132_(MMAP, "mmap failed", -14) \
133_(SLAVE_TIMEOUT, "Slave map timeout", -15)
134
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400135typedef enum
136{
Ed Warnickecb9cada2015-12-08 15:45:58 -0700137#define _(n,s,c) SSVM_API_ERROR_##n = c,
138 foreach_ssvm_api_error
139#undef _
140} ssvm_api_error_enum_t;
141
142#define SSVM_API_ERROR_NO_NAME (-10)
143
144int ssvm_master_init (ssvm_private_t * ssvm, u32 master_index);
145int ssvm_slave_init (ssvm_private_t * ssvm, int timeout_in_seconds);
146
147#endif /* __included_ssvm_h__ */
Dave Barach8a7fb0c2016-07-08 14:44:23 -0400148
149/*
150 * fd.io coding-style-patch-verification: ON
151 *
152 * Local Variables:
153 * eval: (c-set-style "gnu")
154 * End:
155 */