STATS: Dynamically mapped shared memory segment
[vpp.git] / src / vpp / stats / stats.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_stats_h__
16 #define __included_stats_h__
17
18 #include <time.h>
19 #include <vlib/vlib.h>
20 #include <vnet/vnet.h>
21 #include <vnet/interface.h>
22 #include <pthread.h>
23 #include <vlib/threads.h>
24 #include <vnet/fib/fib_table.h>
25 #include <vnet/mfib/mfib_table.h>
26 #include <vlib/unix/unix.h>
27 #include <vlibmemory/api.h>
28 #include <vlibapi/api_helper_macros.h>
29 #include <vpp/stats/stat_segment.h>
30
31 typedef struct
32 {
33   volatile u32 lock;
34   volatile u32 release_hint;
35   u32 thread_index;
36   u32 count;
37   int tag;
38 } data_structure_lock_t;
39
40 /**
41  * @brief stats request registration indexes
42  *
43  */
44 /* from .../vnet/vnet/ip/lookup.c. Yuck */
45 /* *INDENT-OFF* */
46 typedef CLIB_PACKED (struct
47 {
48   ip4_address_t address;
49   u32 address_length: 6;
50   u32 index:         26;
51 }) ip4_route_t;
52 /* *INDENT-ON* */
53
54 typedef struct
55 {
56   ip6_address_t address;
57   u32 address_length;
58   u32 index;
59 } ip6_route_t;
60
61 typedef struct
62 {
63   ip4_route_t *ip4routes;
64   ip6_route_t *ip6routes;
65   mfib_prefix_t *mroutes;
66   fib_table_t **fibs;
67   mfib_table_t **mfibs;
68   hash_pair_t **pvec;
69   uword *results;
70 } do_ip46_fibs_t;
71
72 typedef struct
73 {
74   u16 msg_id;
75   u32 size;
76   u32 client_index;
77   u32 context;
78   i32 retval;
79 } client_registration_reply_t;
80
81 typedef enum
82 {
83 #define stats_reg(n) IDX_##n,
84 #include <vpp/stats/stats.reg>
85 #undef stats_reg
86   STATS_REG_N_IDX,
87 } stats_reg_index_t;
88
89 typedef struct
90 {
91   //Standard client information
92   uword *client_hash;
93   vpe_client_registration_t *clients;
94   u32 item;
95
96 } vpe_client_stats_registration_t;
97
98 typedef struct
99 {
100   void *mheap;
101   pthread_t thread_self;
102   pthread_t thread_handle;
103
104   u32 stats_poll_interval_in_seconds;
105   u32 enable_poller;
106
107   /*
108    * stats_registrations is a vector, indexed by
109    * IDX_xxxx_COUNTER generated for each streaming
110    * stat a client can register for. (see stats.reg)
111    *
112    * The values in the vector refer to pools.
113    *
114    * The pool is of type vpe_client_stats_registration_t
115    *
116    * This typedef consists of:
117    *
118    * u32 item: This is the instance of the IDX_xxxx_COUNTER a
119    *           client is interested in.
120    * vpe_client_registration_t *clients: The list of clients interested.
121    *
122    * e.g.
123    * stats_registrations[IDX_INTERFACE_SIMPLE_COUNTERS] refers to a pool
124    * containing elements:
125    *
126    * u32 item = sw_if_index1
127    * clients = ["clienta","clientb"]
128    *
129    * When clients == NULL the pool element is freed. When the pool is empty
130    *
131    * ie
132    * 0 == pool_elts(stats_registrations[IDX_INTERFACE_SIMPLE_COUNTERS]
133    *
134    * then there is no need to process INTERFACE_SIMPLE_COUNTERS
135    *
136    * Note that u32 item = ~0 is the simple case for ALL interfaces or fibs.
137    *
138    */
139
140   uword **stats_registration_hash;
141   vpe_client_stats_registration_t **stats_registrations;
142
143   /* control-plane data structure lock */
144   data_structure_lock_t *data_structure_lock;
145
146   /* bail out of FIB walk if set */
147   clib_longjmp_t jmp_buf;
148
149   /* Vectors for Distribution funcs: do_ip4_fibs and do_ip6_fibs. */
150   do_ip46_fibs_t do_ip46_fibs;
151
152   /*
153      Working vector vars so as to not thrash memory allocator.
154      Has effect of making "static"
155    */
156   vpe_client_stats_registration_t **regs_tmp;
157   vpe_client_registration_t **clients_tmp;
158
159   /* statistics segment */
160   uword *directory_vector_by_name;
161   stat_segment_directory_entry_t *directory_vector;
162   clib_spinlock_t *stat_segment_lockp;
163   clib_socket_t *socket;
164   u8 *socket_name;
165   ssize_t memory_size;
166   u8 node_counters_enabled;
167   void *heap;
168   stat_segment_shared_header_t *shared_header;  /* pointer to shared memory segment */
169   int memfd;
170
171   u64 last_input_packets;
172
173   /* convenience */
174   vlib_main_t *vlib_main;
175   vnet_main_t *vnet_main;
176   vnet_interface_main_t *interface_main;
177   api_main_t *api_main;
178 } stats_main_t;
179
180 extern stats_main_t stats_main;
181
182 void do_stat_segment_updates (stats_main_t * sm);
183
184 #endif /* __included_stats_h__ */
185
186 /*
187  * fd.io coding-style-patch-verification: ON
188  *
189  * Local Variables:
190  * eval: (c-set-style "gnu")
191  * End:
192  */