blob: c72ea92a70fd432b2253d04f451f359ed5fb99aa [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) \
28 F (UDP4, "ip4-rewrite") \
29 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
78 /* required min rx interval */
79 u32 required_min_rx_us;
80
81 /* remote min rx interval (microseconds) */
82 u32 remote_min_rx_us;
83
84 /* remote min rx interval (clocks) */
85 u64 remote_min_rx_clocks;
86
87 /* remote desired min tx interval */
88 u32 remote_desired_min_tx_us;
89
90 /* 1 if in demand mode, 0 otherwise */
91 u8 local_demand;
92
93 /* 1 if remote system sets demand mode, 0 otherwise */
94 u8 remote_demand;
95
Klement Sekera637b9c42016-12-08 05:19:14 +010096 /* local detect multiplier */
Klement Sekera0e3c0de2016-09-29 14:43:44 +020097 u8 local_detect_mult;
Klement Sekera637b9c42016-12-08 05:19:14 +010098
99 /* remote detect multiplier */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200100 u8 remote_detect_mult;
101
Klement Sekera637b9c42016-12-08 05:19:14 +0100102 /* set to value of timer in timing wheel, 0 if never set */
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200103 u64 wheel_time_clocks;
104
105 /* transmit interval */
106 u64 transmit_interval_clocks;
107
108 /* next time at which to transmit a packet */
109 u64 tx_timeout_clocks;
110
111 /* timestamp of last packet received */
112 u64 last_rx_clocks;
113
114 /* detection time */
115 u64 detection_time_clocks;
116
117 /* transport type for this session */
118 bfd_transport_t transport;
119
120 union
121 {
122 bfd_udp_session_t udp;
123 };
124} bfd_session_t;
125
126typedef struct
127{
128 u32 client_index;
129 u32 client_pid;
130} event_subscriber_t;
131
132typedef struct
133{
134 /* pool of bfd sessions context data */
135 bfd_session_t *sessions;
136
137 /* timing wheel for scheduling timeouts */
138 timing_wheel_t wheel;
139
Klement Sekera637b9c42016-12-08 05:19:14 +0100140 /* timing wheel inaccuracy, in clocks */
141 u64 wheel_inaccuracy;
142
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200143 /* hashmap - bfd session by discriminator */
144 u32 *session_by_disc;
145
146 /* background process node index */
147 u32 bfd_process_node_index;
148
149 /* convenience variables */
150 vlib_main_t *vlib_main;
151 vnet_main_t *vnet_main;
152
153 /* cpu clocks per second */
154 f64 cpu_cps;
155
156 /* for generating random numbers */
157 u32 random_seed;
158
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200159} bfd_main_t;
160
161extern bfd_main_t bfd_main;
162
163/* Packet counters */
164#define foreach_bfd_error(F) \
165 F (NONE, "good bfd packets (processed)") \
166 F (BAD, "invalid bfd packets") \
167 F (DISABLED, "bfd packets received on disabled interfaces")
168
169typedef enum
170{
171#define F(sym, str) BFD_ERROR_##sym,
172 foreach_bfd_error (F)
173#undef F
174 BFD_N_ERROR,
175} bfd_error_t;
176
177/* bfd packet trace capture */
178typedef struct
179{
180 u32 len;
181 u8 data[400];
182} bfd_input_trace_t;
183
184enum
185{
186 BFD_EVENT_RESCHEDULE = 1,
187 BFD_EVENT_NEW_SESSION,
188} bfd_process_event_e;
189
Klement Sekera0e3c0de2016-09-29 14:43:44 +0200190u8 *bfd_input_format_trace (u8 * s, va_list * args);
191
192bfd_session_t *bfd_get_session (bfd_main_t * bm, bfd_transport_t t);
193void bfd_put_session (bfd_main_t * bm, bfd_session_t * bs);
194bfd_session_t *bfd_find_session_by_idx (bfd_main_t * bm, uword bs_idx);
195bfd_session_t *bfd_find_session_by_disc (bfd_main_t * bm, u32 disc);
196void bfd_session_start (bfd_main_t * bm, bfd_session_t * bs);
197void bfd_consume_pkt (bfd_main_t * bm, const bfd_pkt_t * bfd, u32 bs_idx);
198int bfd_verify_pkt_common (const bfd_pkt_t * pkt);
199int bfd_verify_pkt_session (const bfd_pkt_t * pkt, u16 pkt_size,
200 const bfd_session_t * bs);
201void bfd_event (bfd_main_t * bm, bfd_session_t * bs);
202void bfd_send_final (vlib_main_t * vm, vlib_buffer_t * b, bfd_session_t * bs);
203u8 *format_bfd_session (u8 * s, va_list * args);
204
205
206#define USEC_PER_MS 1000LL
207#define USEC_PER_SECOND (1000 * USEC_PER_MS)
208
209/* default, slow transmission interval for BFD packets, per spec at least 1s */
210#define BFD_DEFAULT_DESIRED_MIN_TX_US USEC_PER_SECOND
211
212#endif /* __included_bfd_main_h__ */
213
214/*
215 * fd.io coding-style-patch-verification: ON
216 *
217 * Local Variables:
218 * eval: (c-set-style "gnu")
219 * End:
220 */