blob: 20da381ac23aa732791e28b7c8865f0e58a4e86f [file] [log] [blame]
Klement Sekera0e3c0de2016-09-29 14:43:44 +02001/*
2 * Copyright (c) 2011-2016 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/**
16 * @file
17 * @brief BFD global declarations
18 */
19#ifndef __included_bfd_main_h__
20#define __included_bfd_main_h__
21
22#include <vppinfra/timing_wheel.h>
23#include <vnet/vnet.h>
24#include <vnet/bfd/bfd_protocol.h>
25#include <vnet/bfd/bfd_udp.h>
26
27#define foreach_bfd_transport(F) \
Klement Sekera46a87ad2017-01-02 08:22:23 +010028 F (UDP4, "ip4-rewrite") \
Klement Sekera0e3c0de2016-09-29 14:43:44 +020029 F (UDP6, "ip6-rewrite")
30
31typedef enum
32{
33#define F(t, n) BFD_TRANSPORT_##t,
34 foreach_bfd_transport (F)
35#undef F
36} bfd_transport_t;
37
38#define foreach_bfd_mode(F) \
39 F (asynchronous) \
40 F (demand)
41
42typedef enum
43{
44#define F(x) BFD_MODE_##x,
45 foreach_bfd_mode (F)
46#undef F
47} bfd_mode_e;
48
49typedef struct
50{
51 /* index in bfd_main.sessions pool */
Klement Sekera637b9c42016-12-08 05:19:14 +010052 u32 bs_idx;
Klement Sekera0e3c0de2016-09-29 14:43:44 +020053
54 /* session state */
55 bfd_state_e local_state;
56
57 /* local diagnostics */
58 bfd_diag_code_e local_diag;
59
60 /* remote session state */
61 bfd_state_e remote_state;
62
63 /* local discriminator */
64 u32 local_discr;
65
66 /* remote discriminator */
67 u32 remote_discr;
68
69 /* configured desired min tx interval (microseconds) */
70 u32 config_desired_min_tx_us;
71
72 /* desired min tx interval (microseconds) */
73 u32 desired_min_tx_us;
74
75 /* desired min tx interval (clocks) */
76 u64 desired_min_tx_clocks;
77
Klement Sekera3e0a3562016-12-19 09:05:21 +010078 /* required min rx interval (microseconds) */
Klement Sekera0e3c0de2016-09-29 14:43:44 +020079 u32 required_min_rx_us;
80
Klement Sekera3e0a3562016-12-19 09:05:21 +010081 /* required min echo rx interval (microseconds) */
82 u32 required_min_echo_rx_us;
83
Klement Sekera0e3c0de2016-09-29 14:43:44 +020084 /* remote min rx interval (microseconds) */
85 u32 remote_min_rx_us;
86
87 /* remote min rx interval (clocks) */
88 u64 remote_min_rx_clocks;
89
Klement Sekera3e0a3562016-12-19 09:05:21 +010090 /* remote desired min tx interval (microseconds) */
Klement Sekera0e3c0de2016-09-29 14:43:44 +020091 u32 remote_desired_min_tx_us;
92
93 /* 1 if in demand mode, 0 otherwise */
94 u8 local_demand;
95
96 /* 1 if remote system sets demand mode, 0 otherwise */
97 u8 remote_demand;
98
Klement Sekera637b9c42016-12-08 05:19:14 +010099 /* local detect multiplier */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200100 u8 local_detect_mult;
Klement Sekera637b9c42016-12-08 05:19:14 +0100101
102 /* remote detect multiplier */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200103 u8 remote_detect_mult;
104
Klement Sekera637b9c42016-12-08 05:19:14 +0100105 /* set to value of timer in timing wheel, 0 if never set */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200106 u64 wheel_time_clocks;
107
108 /* transmit interval */
109 u64 transmit_interval_clocks;
110
111 /* next time at which to transmit a packet */
112 u64 tx_timeout_clocks;
113
Klement Sekera3e0a3562016-12-19 09:05:21 +0100114 /* timestamp of last packet transmitted */
115 u64 last_tx_clocks;
116
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200117 /* timestamp of last packet received */
118 u64 last_rx_clocks;
119
120 /* detection time */
121 u64 detection_time_clocks;
122
123 /* transport type for this session */
124 bfd_transport_t transport;
125
126 union
127 {
128 bfd_udp_session_t udp;
129 };
130} bfd_session_t;
131
132typedef struct
133{
134 u32 client_index;
135 u32 client_pid;
136} event_subscriber_t;
137
138typedef struct
139{
140 /* pool of bfd sessions context data */
141 bfd_session_t *sessions;
142
143 /* timing wheel for scheduling timeouts */
144 timing_wheel_t wheel;
145
Klement Sekera637b9c42016-12-08 05:19:14 +0100146 /* timing wheel inaccuracy, in clocks */
147 u64 wheel_inaccuracy;
148
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200149 /* hashmap - bfd session by discriminator */
150 u32 *session_by_disc;
151
152 /* background process node index */
153 u32 bfd_process_node_index;
154
155 /* convenience variables */
156 vlib_main_t *vlib_main;
157 vnet_main_t *vnet_main;
158
159 /* cpu clocks per second */
160 f64 cpu_cps;
161
162 /* for generating random numbers */
163 u32 random_seed;
164
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200165} bfd_main_t;
166
167extern bfd_main_t bfd_main;
168
169/* Packet counters */
170#define foreach_bfd_error(F) \
171 F (NONE, "good bfd packets (processed)") \
172 F (BAD, "invalid bfd packets") \
173 F (DISABLED, "bfd packets received on disabled interfaces")
174
175typedef enum
176{
177#define F(sym, str) BFD_ERROR_##sym,
178 foreach_bfd_error (F)
179#undef F
180 BFD_N_ERROR,
181} bfd_error_t;
182
183/* bfd packet trace capture */
184typedef struct
185{
186 u32 len;
187 u8 data[400];
188} bfd_input_trace_t;
189
190enum
191{
192 BFD_EVENT_RESCHEDULE = 1,
193 BFD_EVENT_NEW_SESSION,
194} bfd_process_event_e;
195
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200196u8 *bfd_input_format_trace (u8 * s, va_list * args);
197
198bfd_session_t *bfd_get_session (bfd_main_t * bm, bfd_transport_t t);
199void bfd_put_session (bfd_main_t * bm, bfd_session_t * bs);
200bfd_session_t *bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx);
201bfd_session_t *bfd_find_session_by_disc (bfd_main_t * bm, u32 disc);
202void bfd_session_start (bfd_main_t * bm, bfd_session_t * bs);
203void bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * bfd, u32 bs_idx);
204int bfd_verify_pkt_common (const bfd_pkt_t * pkt);
205int bfd_verify_pkt_session (const bfd_pkt_t * pkt, u16 pkt_size,
206 const bfd_session_t * bs);
207void bfd_event (bfd_main_t * bm, bfd_session_t * bs);
208void bfd_send_final (vlib_main_t * vm, vlib_buffer_t * b, bfd_session_t * bs);
209u8 *format_bfd_session (u8 * s, va_list * args);
210
211
212#define USEC_PER_MS 1000LL
213#define USEC_PER_SECOND (1000 * USEC_PER_MS)
214
215/* default, slow transmission interval for BFD packets, per spec at least 1s */
216#define BFD_DEFAULT_DESIRED_MIN_TX_US USEC_PER_SECOND
217
218#endif /* __included_bfd_main_h__ */
219
220/*
221 * fd.io coding-style-patch-verification: ON
222 *
223 * Local Variables:
224 * eval: (c-set-style "gnu")
225 * End:
226 */