VPP-108 : API calls to read classify table and sessions
[vpp.git] / svm / ssvm.h
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
41 #define MMAP_PAGESIZE (4<<10)
42 #define SSVM_N_OPAQUE 7
43
44 typedef struct {
45   /* Spin-lock */
46   volatile u32 lock;
47   volatile u32 owner_pid;
48   int recursion_count;
49   u32 tag;                      /* for debugging */
50
51   /* The allocation arena */
52   void * heap;
53
54   /* Segment must be mapped at this address, or no supper */
55   u64 ssvm_va;
56   /* The actual mmap size */
57   u64 ssvm_size;
58   u32 master_pid;
59   u32 slave_pid;
60   u8 * name;
61   void * opaque [SSVM_N_OPAQUE];
62
63   /* Set when the master application thinks it's time to make the donuts */
64   volatile u32 ready;
65
66   /* Needed to make unique MAC addresses, etc. */
67   u32 master_index;
68 } ssvm_shared_header_t;
69
70 typedef struct {
71   ssvm_shared_header_t * sh;
72   u64 ssvm_size;
73   u32 my_pid;
74   u32 vlib_hw_if_index;
75   u8 * name;
76   uword requested_va;
77   int i_am_master;
78   u32 per_interface_next_index;
79   u32 * rx_queue;
80 } ssvm_private_t;
81
82 always_inline void ssvm_lock (ssvm_shared_header_t * h, u32 my_pid, u32 tag)
83 {
84   if (h->owner_pid == my_pid)
85     {
86       h->recursion_count++;
87       return;
88     }
89   
90   while (__sync_lock_test_and_set (&h->lock, 1))
91     ;
92   
93   h->owner_pid = my_pid;
94   h->recursion_count = 1;
95   h->tag = tag;
96 }
97
98 always_inline void ssvm_unlock (ssvm_shared_header_t * h)
99 {
100   if (--h->recursion_count == 0)
101     {
102       h->owner_pid = 0;
103       h->tag = 0;
104       CLIB_MEMORY_BARRIER();
105       h->lock = 0;
106     }
107 }
108
109 static inline void *ssvm_push_heap (ssvm_shared_header_t *sh)
110 {
111     u8 *oldheap;
112     oldheap = clib_mem_set_heap(sh->heap);
113     return ((void *) oldheap);
114 }
115
116 static inline void ssvm_pop_heap (void *oldheap)
117 {
118     clib_mem_set_heap(oldheap);
119 }
120
121 #define foreach_ssvm_api_error                  \
122 _(NO_NAME, "No shared segment name", -10)       \
123 _(NO_SIZE, "Size not set (master)", -11)        \
124 _(CREATE_FAILURE, "Create failed", -12)         \
125 _(SET_SIZE, "Set size failed", -13)             \
126 _(MMAP, "mmap failed", -14)                     \
127 _(SLAVE_TIMEOUT, "Slave map timeout", -15)
128
129 typedef enum {
130 #define _(n,s,c) SSVM_API_ERROR_##n = c,
131   foreach_ssvm_api_error
132 #undef _
133 } ssvm_api_error_enum_t;
134
135 #define SSVM_API_ERROR_NO_NAME  (-10)
136
137 int ssvm_master_init (ssvm_private_t * ssvm, u32 master_index);
138 int ssvm_slave_init (ssvm_private_t * ssvm, int timeout_in_seconds);
139
140 #endif /* __included_ssvm_h__ */