]>
Commit | Line | Data |
---|---|---|
e9e2c950 RZ |
1 | /********************************************************************* |
2 | * Copyright 2013 Cumulus Networks, LLC. All rights reserved. | |
3 | * Copyright 2014,2015,2016,2017 Cumulus Networks, Inc. All rights reserved. | |
4 | * | |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the Free | |
7 | * Software Foundation; either version 2 of the License, or (at your option) | |
8 | * any later version. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but WITHOUT | |
11 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
12 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
13 | * more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License along | |
16 | * with this program; see the file COPYING; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
18 | * | |
19 | * bfd.c: implements the BFD protocol. | |
20 | * | |
21 | * Authors | |
22 | * ------- | |
23 | * Shrijeet Mukherjee [shm@cumulusnetworks.com] | |
24 | * Kanna Rajagopal [kanna@cumulusnetworks.com] | |
25 | * Radhika Mahankali [Radhika@cumulusnetworks.com] | |
26 | */ | |
27 | ||
28 | #include <zebra.h> | |
29 | ||
e9e2c950 | 30 | #include "lib/jhash.h" |
5920b3eb | 31 | #include "lib/network.h" |
e9e2c950 RZ |
32 | |
33 | #include "bfd.h" | |
34 | ||
bf8d3d6a DL |
35 | DEFINE_MTYPE_STATIC(BFDD, BFDD_CONFIG, "long-lived configuration memory"); |
36 | DEFINE_MTYPE_STATIC(BFDD, BFDD_PROFILE, "long-lived profile memory"); | |
37 | DEFINE_MTYPE_STATIC(BFDD, BFDD_SESSION_OBSERVER, "Session observer"); | |
38 | DEFINE_MTYPE_STATIC(BFDD, BFDD_VRF, "BFD VRF"); | |
e9e2c950 RZ |
39 | |
40 | /* | |
41 | * Prototypes | |
42 | */ | |
43 | static uint32_t ptm_bfd_gen_ID(void); | |
44 | static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd); | |
e9e2c950 RZ |
45 | static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa, |
46 | uint32_t ldisc); | |
47 | static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc); | |
48 | static const char *get_diag_str(int diag); | |
49 | ||
9f37770f RZ |
50 | static void bs_admin_down_handler(struct bfd_session *bs, int nstate); |
51 | static void bs_down_handler(struct bfd_session *bs, int nstate); | |
52 | static void bs_init_handler(struct bfd_session *bs, int nstate); | |
53 | static void bs_up_handler(struct bfd_session *bs, int nstate); | |
54 | ||
f6dfa247 RZ |
55 | /** |
56 | * Remove BFD profile from all BFD sessions so we don't leave dangling | |
57 | * pointers. | |
58 | */ | |
59 | static void bfd_profile_detach(struct bfd_profile *bp); | |
60 | ||
79b4a6fc RZ |
61 | /* Zeroed array with the size of an IPv6 address. */ |
62 | struct in6_addr zero_addr; | |
e9e2c950 | 63 | |
ccc9ada8 RZ |
64 | /** BFD profiles list. */ |
65 | struct bfdproflist bplist; | |
66 | ||
e9e2c950 RZ |
67 | /* |
68 | * Functions | |
69 | */ | |
ccc9ada8 RZ |
70 | struct bfd_profile *bfd_profile_lookup(const char *name) |
71 | { | |
72 | struct bfd_profile *bp; | |
73 | ||
74 | TAILQ_FOREACH (bp, &bplist, entry) { | |
75 | if (strcmp(name, bp->name)) | |
76 | continue; | |
77 | ||
78 | return bp; | |
79 | } | |
80 | ||
81 | return NULL; | |
82 | } | |
83 | ||
84 | static void bfd_profile_set_default(struct bfd_profile *bp) | |
85 | { | |
59dad813 | 86 | bp->admin_shutdown = false; |
ccc9ada8 RZ |
87 | bp->detection_multiplier = BFD_DEFDETECTMULT; |
88 | bp->echo_mode = false; | |
1a2e2fff | 89 | bp->passive = false; |
262e1d25 | 90 | bp->minimum_ttl = BFD_DEF_MHOP_TTL; |
4df3e31c IR |
91 | bp->min_echo_rx = BFD_DEF_REQ_MIN_ECHO_RX; |
92 | bp->min_echo_tx = BFD_DEF_DES_MIN_ECHO_TX; | |
ccc9ada8 RZ |
93 | bp->min_rx = BFD_DEFREQUIREDMINRX; |
94 | bp->min_tx = BFD_DEFDESIREDMINTX; | |
95 | } | |
96 | ||
97 | struct bfd_profile *bfd_profile_new(const char *name) | |
98 | { | |
99 | struct bfd_profile *bp; | |
100 | ||
101 | /* Search for duplicates. */ | |
102 | if (bfd_profile_lookup(name) != NULL) | |
103 | return NULL; | |
104 | ||
105 | /* Allocate, name it and put into list. */ | |
106 | bp = XCALLOC(MTYPE_BFDD_PROFILE, sizeof(*bp)); | |
107 | strlcpy(bp->name, name, sizeof(bp->name)); | |
108 | TAILQ_INSERT_TAIL(&bplist, bp, entry); | |
109 | ||
110 | /* Set default values. */ | |
111 | bfd_profile_set_default(bp); | |
112 | ||
113 | return bp; | |
114 | } | |
115 | ||
116 | void bfd_profile_free(struct bfd_profile *bp) | |
117 | { | |
f6dfa247 | 118 | /* Detach from any session. */ |
f3e1d224 RZ |
119 | if (bglobal.bg_shutdown == false) |
120 | bfd_profile_detach(bp); | |
f6dfa247 RZ |
121 | |
122 | /* Remove from global list. */ | |
ccc9ada8 | 123 | TAILQ_REMOVE(&bplist, bp, entry); |
dd8bc21d MS |
124 | |
125 | XFREE(MTYPE_BFDD_PROFILE, bp); | |
ccc9ada8 RZ |
126 | } |
127 | ||
ccc9ada8 RZ |
128 | void bfd_profile_apply(const char *profname, struct bfd_session *bs) |
129 | { | |
130 | struct bfd_profile *bp; | |
131 | ||
132 | /* Remove previous profile if any. */ | |
133 | if (bs->profile_name) { | |
ccc9ada8 RZ |
134 | /* We are changing profiles. */ |
135 | if (strcmp(bs->profile_name, profname)) { | |
136 | XFREE(MTYPE_BFDD_PROFILE, bs->profile_name); | |
137 | bs->profile_name = | |
138 | XSTRDUP(MTYPE_BFDD_PROFILE, profname); | |
139 | } | |
140 | } else /* Save the current profile name (in case it doesn't exist). */ | |
141 | bs->profile_name = XSTRDUP(MTYPE_BFDD_PROFILE, profname); | |
142 | ||
143 | /* Look up new profile to apply. */ | |
144 | bp = bfd_profile_lookup(profname); | |
ccc9ada8 RZ |
145 | |
146 | /* Point to profile if it exists. */ | |
147 | bs->profile = bp; | |
148 | ||
4e38f82a RZ |
149 | /* Apply configuration. */ |
150 | bfd_session_apply(bs); | |
151 | } | |
152 | ||
153 | void bfd_session_apply(struct bfd_session *bs) | |
154 | { | |
155 | struct bfd_profile *bp; | |
156 | uint32_t min_tx = bs->timers.desired_min_tx; | |
157 | uint32_t min_rx = bs->timers.required_min_rx; | |
158 | ||
159 | /* Pick the source of configuration. */ | |
160 | bp = bs->profile ? bs->profile : &bs->peer_profile; | |
161 | ||
ccc9ada8 RZ |
162 | /* Set multiplier if not the default. */ |
163 | if (bs->peer_profile.detection_multiplier == BFD_DEFDETECTMULT) | |
164 | bs->detect_mult = bp->detection_multiplier; | |
165 | else | |
166 | bs->detect_mult = bs->peer_profile.detection_multiplier; | |
167 | ||
168 | /* Set timers if not the default. */ | |
169 | if (bs->peer_profile.min_tx == BFD_DEFDESIREDMINTX) | |
170 | bs->timers.desired_min_tx = bp->min_tx; | |
171 | else | |
172 | bs->timers.desired_min_tx = bs->peer_profile.min_tx; | |
173 | ||
174 | if (bs->peer_profile.min_rx == BFD_DEFREQUIREDMINRX) | |
175 | bs->timers.required_min_rx = bp->min_rx; | |
176 | else | |
177 | bs->timers.required_min_rx = bs->peer_profile.min_rx; | |
178 | ||
179 | /* We can only apply echo options on single hop sessions. */ | |
180 | if (!CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)) { | |
4df3e31c IR |
181 | /* Configure echo timers if they were default. */ |
182 | if (bs->peer_profile.min_echo_rx == BFD_DEF_REQ_MIN_ECHO_RX) | |
183 | bs->timers.required_min_echo_rx = bp->min_echo_rx; | |
ccc9ada8 | 184 | else |
4df3e31c | 185 | bs->timers.required_min_echo_rx = |
ccc9ada8 RZ |
186 | bs->peer_profile.min_echo_rx; |
187 | ||
4df3e31c IR |
188 | if (bs->peer_profile.min_echo_tx == BFD_DEF_DES_MIN_ECHO_TX) |
189 | bs->timers.desired_min_echo_tx = bp->min_echo_tx; | |
190 | else | |
191 | bs->timers.desired_min_echo_tx = | |
192 | bs->peer_profile.min_echo_tx; | |
193 | ||
ccc9ada8 RZ |
194 | /* Toggle echo if default value. */ |
195 | if (bs->peer_profile.echo_mode == false) | |
196 | bfd_set_echo(bs, bp->echo_mode); | |
197 | else | |
198 | bfd_set_echo(bs, bs->peer_profile.echo_mode); | |
262e1d25 RZ |
199 | } else { |
200 | /* Configure the TTL packet filter. */ | |
201 | if (bs->peer_profile.minimum_ttl == BFD_DEF_MHOP_TTL) | |
202 | bs->mh_ttl = bp->minimum_ttl; | |
203 | else | |
204 | bs->mh_ttl = bs->peer_profile.minimum_ttl; | |
ccc9ada8 RZ |
205 | } |
206 | ||
1a2e2fff RZ |
207 | /* Toggle 'passive-mode' if default value. */ |
208 | if (bs->peer_profile.passive == false) | |
209 | bfd_set_passive_mode(bs, bp->passive); | |
210 | else | |
211 | bfd_set_passive_mode(bs, bs->peer_profile.passive); | |
212 | ||
ccc9ada8 | 213 | /* Toggle 'no shutdown' if default value. */ |
59dad813 | 214 | if (bs->peer_profile.admin_shutdown == false) |
ccc9ada8 RZ |
215 | bfd_set_shutdown(bs, bp->admin_shutdown); |
216 | else | |
217 | bfd_set_shutdown(bs, bs->peer_profile.admin_shutdown); | |
4e38f82a RZ |
218 | |
219 | /* If session interval changed negotiate new timers. */ | |
220 | if (bs->ses_state == PTM_BFD_UP | |
221 | && (bs->timers.desired_min_tx != min_tx | |
222 | || bs->timers.required_min_rx != min_rx)) | |
223 | bfd_set_polling(bs); | |
efd04d60 RZ |
224 | |
225 | /* Send updated information to data plane. */ | |
226 | bfd_dplane_update_session(bs); | |
ccc9ada8 RZ |
227 | } |
228 | ||
229 | void bfd_profile_remove(struct bfd_session *bs) | |
230 | { | |
231 | /* Remove any previous set profile name. */ | |
232 | XFREE(MTYPE_BFDD_PROFILE, bs->profile_name); | |
4e38f82a | 233 | bs->profile = NULL; |
ccc9ada8 | 234 | |
4e38f82a | 235 | bfd_session_apply(bs); |
ccc9ada8 RZ |
236 | } |
237 | ||
79b4a6fc RZ |
238 | void gen_bfd_key(struct bfd_key *key, struct sockaddr_any *peer, |
239 | struct sockaddr_any *local, bool mhop, const char *ifname, | |
240 | const char *vrfname) | |
d245e522 | 241 | { |
79b4a6fc | 242 | memset(key, 0, sizeof(*key)); |
d245e522 | 243 | |
79b4a6fc RZ |
244 | switch (peer->sa_sin.sin_family) { |
245 | case AF_INET: | |
246 | key->family = AF_INET; | |
247 | memcpy(&key->peer, &peer->sa_sin.sin_addr, | |
248 | sizeof(peer->sa_sin.sin_addr)); | |
249 | memcpy(&key->local, &local->sa_sin.sin_addr, | |
250 | sizeof(local->sa_sin.sin_addr)); | |
251 | break; | |
252 | case AF_INET6: | |
253 | key->family = AF_INET6; | |
254 | memcpy(&key->peer, &peer->sa_sin6.sin6_addr, | |
255 | sizeof(peer->sa_sin6.sin6_addr)); | |
256 | memcpy(&key->local, &local->sa_sin6.sin6_addr, | |
257 | sizeof(local->sa_sin6.sin6_addr)); | |
d245e522 RZ |
258 | break; |
259 | } | |
d245e522 | 260 | |
79b4a6fc RZ |
261 | key->mhop = mhop; |
262 | if (ifname && ifname[0]) | |
263 | strlcpy(key->ifname, ifname, sizeof(key->ifname)); | |
264 | if (vrfname && vrfname[0]) | |
265 | strlcpy(key->vrfname, vrfname, sizeof(key->vrfname)); | |
adc26455 RZ |
266 | else |
267 | strlcpy(key->vrfname, VRF_DEFAULT_NAME, sizeof(key->vrfname)); | |
d245e522 RZ |
268 | } |
269 | ||
e9e2c950 RZ |
270 | struct bfd_session *bs_peer_find(struct bfd_peer_cfg *bpc) |
271 | { | |
272 | struct bfd_session *bs; | |
273 | struct peer_label *pl; | |
79b4a6fc | 274 | struct bfd_key key; |
e9e2c950 RZ |
275 | |
276 | /* Try to find label first. */ | |
277 | if (bpc->bpc_has_label) { | |
278 | pl = pl_find(bpc->bpc_label); | |
279 | if (pl != NULL) { | |
280 | bs = pl->pl_bs; | |
281 | return bs; | |
282 | } | |
283 | } | |
284 | ||
285 | /* Otherwise fallback to peer/local hash lookup. */ | |
79b4a6fc RZ |
286 | gen_bfd_key(&key, &bpc->bpc_peer, &bpc->bpc_local, bpc->bpc_mhop, |
287 | bpc->bpc_localif, bpc->bpc_vrfname); | |
e9e2c950 | 288 | |
79b4a6fc | 289 | return bfd_key_lookup(key); |
d245e522 RZ |
290 | } |
291 | ||
292 | /* | |
293 | * Starts a disabled BFD session. | |
294 | * | |
295 | * A session is disabled when the specified interface/VRF doesn't exist | |
296 | * yet. It might happen on FRR boot or with virtual interfaces. | |
297 | */ | |
298 | int bfd_session_enable(struct bfd_session *bs) | |
299 | { | |
d245e522 RZ |
300 | struct interface *ifp = NULL; |
301 | struct vrf *vrf = NULL; | |
302 | int psock; | |
303 | ||
efd04d60 RZ |
304 | /* We are using data plane, we don't need software. */ |
305 | if (bs->bdc) | |
306 | return 0; | |
307 | ||
d245e522 RZ |
308 | /* |
309 | * If the interface or VRF doesn't exist, then we must register | |
310 | * the session but delay its start. | |
311 | */ | |
218afa36 PG |
312 | if (bs->key.vrfname[0]) { |
313 | vrf = vrf_lookup_by_name(bs->key.vrfname); | |
d245e522 | 314 | if (vrf == NULL) { |
259b64eb | 315 | zlog_err( |
33fc8bc7 IR |
316 | "session-enable: specified VRF %s doesn't exists.", |
317 | bs->key.vrfname); | |
d245e522 RZ |
318 | return 0; |
319 | } | |
33fc8bc7 IR |
320 | } else { |
321 | vrf = vrf_lookup_by_id(VRF_DEFAULT); | |
d245e522 RZ |
322 | } |
323 | ||
33fc8bc7 | 324 | assert(vrf); |
039c8158 | 325 | |
218afa36 | 326 | if (bs->key.ifname[0]) { |
33fc8bc7 | 327 | ifp = if_lookup_by_name(bs->key.ifname, vrf->vrf_id); |
218afa36 | 328 | if (ifp == NULL) { |
259b64eb | 329 | zlog_err( |
99950bc5 | 330 | "session-enable: specified interface %s (VRF %s) doesn't exist.", |
33fc8bc7 | 331 | bs->key.ifname, vrf->name); |
d245e522 RZ |
332 | return 0; |
333 | } | |
334 | } | |
335 | ||
336 | /* Assign interface/VRF pointers. */ | |
337 | bs->vrf = vrf; | |
d245e522 | 338 | |
efd04d60 RZ |
339 | /* Assign interface pointer (if any). */ |
340 | bs->ifp = ifp; | |
341 | ||
342 | /* Attempt to use data plane. */ | |
343 | if (bglobal.bg_use_dplane && bfd_dplane_add_session(bs) == 0) { | |
344 | control_notify_config(BCM_NOTIFY_CONFIG_ADD, bs); | |
345 | return 0; | |
346 | } | |
d245e522 | 347 | |
261e0ba9 RZ |
348 | /* Sanity check: don't leak open sockets. */ |
349 | if (bs->sock != -1) { | |
48da2c31 RZ |
350 | if (bglobal.debug_peer_event) |
351 | zlog_debug("session-enable: previous socket open"); | |
352 | ||
261e0ba9 RZ |
353 | close(bs->sock); |
354 | bs->sock = -1; | |
355 | } | |
356 | ||
d245e522 RZ |
357 | /* |
358 | * Get socket for transmitting control packets. Note that if we | |
359 | * could use the destination port (3784) for the source | |
360 | * port we wouldn't need a socket per session. | |
361 | */ | |
b88113ef | 362 | if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_IPV6) == 0) { |
d245e522 RZ |
363 | psock = bp_peer_socket(bs); |
364 | if (psock == -1) | |
261e0ba9 | 365 | return 0; |
d245e522 RZ |
366 | } else { |
367 | psock = bp_peer_socketv6(bs); | |
368 | if (psock == -1) | |
261e0ba9 | 369 | return 0; |
d245e522 RZ |
370 | } |
371 | ||
372 | /* | |
373 | * We've got a valid socket, lets start the timers and the | |
374 | * protocol. | |
375 | */ | |
376 | bs->sock = psock; | |
1a2e2fff RZ |
377 | |
378 | /* Only start timers if we are using active mode. */ | |
379 | if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE) == 0) { | |
380 | bfd_recvtimer_update(bs); | |
381 | ptm_bfd_start_xmt_timer(bs, false); | |
382 | } | |
d245e522 | 383 | |
d245e522 RZ |
384 | return 0; |
385 | } | |
386 | ||
387 | /* | |
388 | * Disabled a running BFD session. | |
389 | * | |
390 | * A session is disabled when the specified interface/VRF gets removed | |
391 | * (e.g. virtual interfaces). | |
392 | */ | |
393 | void bfd_session_disable(struct bfd_session *bs) | |
394 | { | |
efd04d60 RZ |
395 | /* We are using data plane, we don't need software. */ |
396 | if (bs->bdc) | |
397 | return; | |
398 | ||
d245e522 RZ |
399 | /* Free up socket resources. */ |
400 | if (bs->sock != -1) { | |
401 | close(bs->sock); | |
402 | bs->sock = -1; | |
403 | } | |
404 | ||
405 | /* Disable all timers. */ | |
406 | bfd_recvtimer_delete(bs); | |
d245e522 | 407 | bfd_xmttimer_delete(bs); |
8ee0862e | 408 | ptm_bfd_echo_stop(bs); |
8ee0862e RZ |
409 | |
410 | /* Set session down so it doesn't report UP and disabled. */ | |
411 | ptm_bfd_sess_dn(bs, BD_PATH_DOWN); | |
e9e2c950 RZ |
412 | } |
413 | ||
414 | static uint32_t ptm_bfd_gen_ID(void) | |
415 | { | |
843b324d | 416 | uint32_t session_id; |
e9e2c950 | 417 | |
843b324d RZ |
418 | /* |
419 | * RFC 5880, Section 6.8.1. recommends that we should generate | |
420 | * random session identification numbers. | |
421 | */ | |
422 | do { | |
5920b3eb RZ |
423 | session_id = ((frr_weak_random() << 16) & 0xFFFF0000) |
424 | | (frr_weak_random() & 0x0000FFFF); | |
843b324d RZ |
425 | } while (session_id == 0 || bfd_id_lookup(session_id) != NULL); |
426 | ||
427 | return session_id; | |
e9e2c950 RZ |
428 | } |
429 | ||
430 | void ptm_bfd_start_xmt_timer(struct bfd_session *bfd, bool is_echo) | |
431 | { | |
432 | uint64_t jitter, xmt_TO; | |
433 | int maxpercent; | |
434 | ||
435 | xmt_TO = is_echo ? bfd->echo_xmt_TO : bfd->xmt_TO; | |
436 | ||
437 | /* | |
438 | * From section 6.5.2: trasmit interval should be randomly jittered | |
439 | * between | |
440 | * 75% and 100% of nominal value, unless detect_mult is 1, then should | |
441 | * be | |
442 | * between 75% and 90%. | |
443 | */ | |
444 | maxpercent = (bfd->detect_mult == 1) ? 16 : 26; | |
5920b3eb | 445 | jitter = (xmt_TO * (75 + (frr_weak_random() % maxpercent))) / 100; |
e9e2c950 RZ |
446 | /* XXX remove that division above */ |
447 | ||
448 | if (is_echo) | |
449 | bfd_echo_xmttimer_update(bfd, jitter); | |
450 | else | |
451 | bfd_xmttimer_update(bfd, jitter); | |
452 | } | |
453 | ||
454 | static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd) | |
455 | { | |
456 | /* Send the scheduled echo packet */ | |
57485b0b | 457 | /* if ipv4 use the new echo implementation that causes |
458 | * the packet to be looped in forwarding plane of peer | |
459 | */ | |
460 | if (CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_IPV6) == 0) | |
461 | #ifdef BFD_LINUX | |
462 | ptm_bfd_echo_fp_snd(bfd); | |
463 | #else | |
464 | ptm_bfd_echo_snd(bfd); | |
465 | #endif | |
466 | else | |
467 | ptm_bfd_echo_snd(bfd); | |
e9e2c950 RZ |
468 | |
469 | /* Restart the timer for next time */ | |
470 | ptm_bfd_start_xmt_timer(bfd, true); | |
471 | } | |
472 | ||
473 | void ptm_bfd_xmt_TO(struct bfd_session *bfd, int fbit) | |
474 | { | |
475 | /* Send the scheduled control packet */ | |
476 | ptm_bfd_snd(bfd, fbit); | |
477 | ||
478 | /* Restart the timer for next time */ | |
479 | ptm_bfd_start_xmt_timer(bfd, false); | |
480 | } | |
481 | ||
8bd859f6 | 482 | void ptm_bfd_echo_stop(struct bfd_session *bfd) |
e9e2c950 RZ |
483 | { |
484 | bfd->echo_xmt_TO = 0; | |
485 | bfd->echo_detect_TO = 0; | |
b88113ef | 486 | UNSET_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE); |
e9e2c950 RZ |
487 | |
488 | bfd_echo_xmttimer_delete(bfd); | |
489 | bfd_echo_recvtimer_delete(bfd); | |
e9e2c950 RZ |
490 | } |
491 | ||
492 | void ptm_bfd_echo_start(struct bfd_session *bfd) | |
493 | { | |
494 | bfd->echo_detect_TO = (bfd->remote_detect_mult * bfd->echo_xmt_TO); | |
5aeb216e IR |
495 | if (bfd->echo_detect_TO > 0) { |
496 | bfd_echo_recvtimer_update(bfd); | |
451eb5a2 | 497 | ptm_bfd_echo_xmt_TO(bfd); |
5aeb216e | 498 | } |
e9e2c950 RZ |
499 | } |
500 | ||
bc50bcc8 | 501 | void ptm_bfd_sess_up(struct bfd_session *bfd) |
e9e2c950 | 502 | { |
03e7f088 RZ |
503 | int old_state = bfd->ses_state; |
504 | ||
e9e2c950 RZ |
505 | bfd->local_diag = 0; |
506 | bfd->ses_state = PTM_BFD_UP; | |
e9e2c950 RZ |
507 | monotime(&bfd->uptime); |
508 | ||
c0ef9a8a RZ |
509 | /* Connection is up, lets negotiate timers. */ |
510 | bfd_set_polling(bfd); | |
511 | ||
512 | /* Start sending control packets with poll bit immediately. */ | |
513 | ptm_bfd_snd(bfd, 0); | |
e9e2c950 | 514 | |
7555dc61 | 515 | control_notify(bfd, bfd->ses_state); |
e9e2c950 | 516 | |
0684c9b1 RZ |
517 | if (old_state != bfd->ses_state) { |
518 | bfd->stats.session_up++; | |
48da2c31 RZ |
519 | if (bglobal.debug_peer_event) |
520 | zlog_debug("state-change: [%s] %s -> %s", | |
521 | bs_to_string(bfd), state_list[old_state].str, | |
522 | state_list[bfd->ses_state].str); | |
0684c9b1 | 523 | } |
e9e2c950 RZ |
524 | } |
525 | ||
bc50bcc8 | 526 | void ptm_bfd_sess_dn(struct bfd_session *bfd, uint8_t diag) |
e9e2c950 RZ |
527 | { |
528 | int old_state = bfd->ses_state; | |
529 | ||
530 | bfd->local_diag = diag; | |
531 | bfd->discrs.remote_discr = 0; | |
532 | bfd->ses_state = PTM_BFD_DOWN; | |
533 | bfd->polling = 0; | |
534 | bfd->demand_mode = 0; | |
535 | monotime(&bfd->downtime); | |
536 | ||
8ee0862e RZ |
537 | /* |
538 | * Only attempt to send if we have a valid socket: | |
539 | * this function might be called by session disablers and in | |
540 | * this case we won't have a valid socket (i.e. interface was | |
541 | * removed or VRF doesn't exist anymore). | |
542 | */ | |
543 | if (bfd->sock != -1) | |
544 | ptm_bfd_snd(bfd, 0); | |
e9e2c950 | 545 | |
b912b189 RZ |
546 | /* Slow down the control packets, the connection is down. */ |
547 | bs_set_slow_timers(bfd); | |
548 | ||
e9e2c950 RZ |
549 | /* only signal clients when going from up->down state */ |
550 | if (old_state == PTM_BFD_UP) | |
7555dc61 | 551 | control_notify(bfd, PTM_BFD_DOWN); |
e9e2c950 | 552 | |
e9e2c950 | 553 | /* Stop echo packet transmission if they are active */ |
b88113ef | 554 | if (CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE)) |
8bd859f6 | 555 | ptm_bfd_echo_stop(bfd); |
03e7f088 | 556 | |
1a2e2fff RZ |
557 | /* Stop attempting to transmit or expect control packets if passive. */ |
558 | if (CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_PASSIVE)) { | |
559 | bfd_recvtimer_delete(bfd); | |
560 | bfd_xmttimer_delete(bfd); | |
561 | } | |
562 | ||
0684c9b1 RZ |
563 | if (old_state != bfd->ses_state) { |
564 | bfd->stats.session_down++; | |
48da2c31 RZ |
565 | if (bglobal.debug_peer_event) |
566 | zlog_debug("state-change: [%s] %s -> %s reason:%s", | |
567 | bs_to_string(bfd), state_list[old_state].str, | |
568 | state_list[bfd->ses_state].str, | |
569 | get_diag_str(bfd->local_diag)); | |
0684c9b1 | 570 | } |
57485b0b | 571 | |
572 | /* clear peer's mac address */ | |
573 | UNSET_FLAG(bfd->flags, BFD_SESS_FLAG_MAC_SET); | |
574 | memset(bfd->peer_hw_addr, 0, sizeof(bfd->peer_hw_addr)); | |
575 | /* reset local address ,it might has been be changed after bfd is up*/ | |
576 | memset(&bfd->local_address, 0, sizeof(bfd->local_address)); | |
e9e2c950 RZ |
577 | } |
578 | ||
e9e2c950 RZ |
579 | static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa, |
580 | uint32_t ldisc) | |
581 | { | |
582 | struct bfd_session *bs; | |
583 | ||
584 | bs = bfd_id_lookup(ldisc); | |
585 | if (bs == NULL) | |
586 | return NULL; | |
587 | ||
79b4a6fc | 588 | switch (bs->key.family) { |
e9e2c950 | 589 | case AF_INET: |
79b4a6fc RZ |
590 | if (memcmp(&sa->sa_sin.sin_addr, &bs->key.peer, |
591 | sizeof(sa->sa_sin.sin_addr))) | |
592 | return NULL; | |
e9e2c950 RZ |
593 | break; |
594 | case AF_INET6: | |
79b4a6fc RZ |
595 | if (memcmp(&sa->sa_sin6.sin6_addr, &bs->key.peer, |
596 | sizeof(sa->sa_sin6.sin6_addr))) | |
597 | return NULL; | |
e9e2c950 RZ |
598 | break; |
599 | } | |
600 | ||
79b4a6fc | 601 | return bs; |
e9e2c950 RZ |
602 | } |
603 | ||
b333abc2 | 604 | struct bfd_session *ptm_bfd_sess_find(struct bfd_pkt *cp, |
e9e2c950 RZ |
605 | struct sockaddr_any *peer, |
606 | struct sockaddr_any *local, | |
eb4135ba IR |
607 | struct interface *ifp, |
608 | vrf_id_t vrfid, | |
b333abc2 | 609 | bool is_mhop) |
e9e2c950 | 610 | { |
79b4a6fc RZ |
611 | struct vrf *vrf; |
612 | struct bfd_key key; | |
e9e2c950 | 613 | |
9d63adda RZ |
614 | /* Find our session using the ID signaled by the remote end. */ |
615 | if (cp->discrs.remote_discr) | |
616 | return bfd_find_disc(peer, ntohl(cp->discrs.remote_discr)); | |
617 | ||
eb4135ba IR |
618 | /* Search for session without using discriminator. */ |
619 | vrf = vrf_lookup_by_id(vrfid); | |
9d63adda | 620 | |
79b4a6fc | 621 | gen_bfd_key(&key, peer, local, is_mhop, ifp ? ifp->name : NULL, |
adc26455 | 622 | vrf ? vrf->name : VRF_DEFAULT_NAME); |
e9e2c950 | 623 | |
9d63adda | 624 | /* XXX maybe remoteDiscr should be checked for remoteHeard cases. */ |
79b4a6fc | 625 | return bfd_key_lookup(key); |
e9e2c950 RZ |
626 | } |
627 | ||
cc9f21da | 628 | void bfd_xmt_cb(struct thread *t) |
e9e2c950 RZ |
629 | { |
630 | struct bfd_session *bs = THREAD_ARG(t); | |
631 | ||
632 | ptm_bfd_xmt_TO(bs, 0); | |
e9e2c950 RZ |
633 | } |
634 | ||
cc9f21da | 635 | void bfd_echo_xmt_cb(struct thread *t) |
e9e2c950 RZ |
636 | { |
637 | struct bfd_session *bs = THREAD_ARG(t); | |
638 | ||
451eb5a2 RZ |
639 | if (bs->echo_xmt_TO > 0) |
640 | ptm_bfd_echo_xmt_TO(bs); | |
e9e2c950 RZ |
641 | } |
642 | ||
643 | /* Was ptm_bfd_detect_TO() */ | |
cc9f21da | 644 | void bfd_recvtimer_cb(struct thread *t) |
e9e2c950 RZ |
645 | { |
646 | struct bfd_session *bs = THREAD_ARG(t); | |
e9e2c950 RZ |
647 | |
648 | switch (bs->ses_state) { | |
649 | case PTM_BFD_INIT: | |
650 | case PTM_BFD_UP: | |
bc50bcc8 | 651 | ptm_bfd_sess_dn(bs, BD_CONTROL_EXPIRED); |
e9e2c950 RZ |
652 | break; |
653 | } | |
e9e2c950 RZ |
654 | } |
655 | ||
656 | /* Was ptm_bfd_echo_detect_TO() */ | |
cc9f21da | 657 | void bfd_echo_recvtimer_cb(struct thread *t) |
e9e2c950 RZ |
658 | { |
659 | struct bfd_session *bs = THREAD_ARG(t); | |
e9e2c950 RZ |
660 | |
661 | switch (bs->ses_state) { | |
662 | case PTM_BFD_INIT: | |
663 | case PTM_BFD_UP: | |
bc50bcc8 | 664 | ptm_bfd_sess_dn(bs, BD_ECHO_FAILED); |
e9e2c950 RZ |
665 | break; |
666 | } | |
e9e2c950 RZ |
667 | } |
668 | ||
014cab13 | 669 | struct bfd_session *bfd_session_new(void) |
e9e2c950 RZ |
670 | { |
671 | struct bfd_session *bs; | |
672 | ||
673 | bs = XCALLOC(MTYPE_BFDD_CONFIG, sizeof(*bs)); | |
e9e2c950 | 674 | |
ccc9ada8 RZ |
675 | /* Set peer session defaults. */ |
676 | bfd_profile_set_default(&bs->peer_profile); | |
677 | ||
f43b9368 | 678 | bs->timers.desired_min_tx = BFD_DEFDESIREDMINTX; |
e9e2c950 | 679 | bs->timers.required_min_rx = BFD_DEFREQUIREDMINRX; |
4df3e31c IR |
680 | bs->timers.required_min_echo_rx = BFD_DEF_REQ_MIN_ECHO_RX; |
681 | bs->timers.desired_min_echo_tx = BFD_DEF_DES_MIN_ECHO_TX; | |
e9e2c950 RZ |
682 | bs->detect_mult = BFD_DEFDETECTMULT; |
683 | bs->mh_ttl = BFD_DEF_MHOP_TTL; | |
d245e522 | 684 | bs->ses_state = PTM_BFD_DOWN; |
e9e2c950 | 685 | |
b912b189 RZ |
686 | /* Initiate connection with slow timers. */ |
687 | bs_set_slow_timers(bs); | |
f43b9368 RZ |
688 | |
689 | /* Initiate remote settings as well. */ | |
690 | bs->remote_timers = bs->cur_timers; | |
691 | bs->remote_detect_mult = BFD_DEFDETECTMULT; | |
692 | ||
d245e522 | 693 | bs->sock = -1; |
e9e2c950 RZ |
694 | monotime(&bs->uptime); |
695 | bs->downtime = bs->uptime; | |
696 | ||
697 | return bs; | |
698 | } | |
699 | ||
700 | int bfd_session_update_label(struct bfd_session *bs, const char *nlabel) | |
701 | { | |
702 | /* New label treatment: | |
703 | * - Check if the label is taken; | |
704 | * - Try to allocate the memory for it and register; | |
705 | */ | |
706 | if (bs->pl == NULL) { | |
707 | if (pl_find(nlabel) != NULL) { | |
708 | /* Someone is already using it. */ | |
709 | return -1; | |
710 | } | |
711 | ||
08de92af | 712 | pl_new(nlabel, bs); |
e9e2c950 RZ |
713 | |
714 | return 0; | |
715 | } | |
716 | ||
717 | /* | |
718 | * Test label change consistency: | |
719 | * - Do nothing if it's the same label; | |
720 | * - Check if the future label is already taken; | |
721 | * - Change label; | |
722 | */ | |
723 | if (strcmp(nlabel, bs->pl->pl_label) == 0) | |
724 | return -1; | |
725 | if (pl_find(nlabel) != NULL) | |
726 | return -1; | |
727 | ||
728 | strlcpy(bs->pl->pl_label, nlabel, sizeof(bs->pl->pl_label)); | |
729 | return 0; | |
730 | } | |
731 | ||
732 | static void _bfd_session_update(struct bfd_session *bs, | |
733 | struct bfd_peer_cfg *bpc) | |
734 | { | |
ccc9ada8 | 735 | if (bpc->bpc_has_txinterval) { |
f43b9368 | 736 | bs->timers.desired_min_tx = bpc->bpc_txinterval * 1000; |
ccc9ada8 RZ |
737 | bs->peer_profile.min_tx = bs->timers.desired_min_tx; |
738 | } | |
e9e2c950 | 739 | |
ccc9ada8 | 740 | if (bpc->bpc_has_recvinterval) { |
e9e2c950 | 741 | bs->timers.required_min_rx = bpc->bpc_recvinterval * 1000; |
ccc9ada8 RZ |
742 | bs->peer_profile.min_rx = bs->timers.required_min_rx; |
743 | } | |
e9e2c950 | 744 | |
ccc9ada8 | 745 | if (bpc->bpc_has_detectmultiplier) { |
e9e2c950 | 746 | bs->detect_mult = bpc->bpc_detectmultiplier; |
ccc9ada8 RZ |
747 | bs->peer_profile.detection_multiplier = bs->detect_mult; |
748 | } | |
e9e2c950 | 749 | |
4df3e31c IR |
750 | if (bpc->bpc_has_echorecvinterval) { |
751 | bs->timers.required_min_echo_rx = bpc->bpc_echorecvinterval * 1000; | |
752 | bs->peer_profile.min_echo_rx = bs->timers.required_min_echo_rx; | |
753 | } | |
754 | ||
755 | if (bpc->bpc_has_echotxinterval) { | |
756 | bs->timers.desired_min_echo_tx = bpc->bpc_echotxinterval * 1000; | |
757 | bs->peer_profile.min_echo_tx = bs->timers.desired_min_echo_tx; | |
ccc9ada8 | 758 | } |
e9e2c950 RZ |
759 | |
760 | if (bpc->bpc_has_label) | |
761 | bfd_session_update_label(bs, bpc->bpc_label); | |
762 | ||
4d12e1f9 | 763 | if (bpc->bpc_cbit) |
b88113ef | 764 | SET_FLAG(bs->flags, BFD_SESS_FLAG_CBIT); |
4d12e1f9 | 765 | else |
b88113ef | 766 | UNSET_FLAG(bs->flags, BFD_SESS_FLAG_CBIT); |
4d12e1f9 | 767 | |
262e1d25 RZ |
768 | if (bpc->bpc_has_minimum_ttl) { |
769 | bs->mh_ttl = bpc->bpc_minimum_ttl; | |
770 | bs->peer_profile.minimum_ttl = bpc->bpc_minimum_ttl; | |
771 | } | |
772 | ||
ccc9ada8 | 773 | bs->peer_profile.echo_mode = bpc->bpc_echo; |
4d12e1f9 RZ |
774 | bfd_set_echo(bs, bpc->bpc_echo); |
775 | ||
776 | /* | |
777 | * Shutdown needs to be the last in order to avoid timers enable when | |
778 | * the session is disabled. | |
779 | */ | |
ccc9ada8 | 780 | bs->peer_profile.admin_shutdown = bpc->bpc_shutdown; |
1a2e2fff | 781 | bfd_set_passive_mode(bs, bpc->bpc_passive); |
4d12e1f9 | 782 | bfd_set_shutdown(bs, bpc->bpc_shutdown); |
18322efd RZ |
783 | |
784 | /* | |
785 | * Apply profile last: it also calls `bfd_set_shutdown`. | |
786 | * | |
787 | * There is no problem calling `shutdown` twice if the value doesn't | |
2b669d3a | 788 | * change or if it is overridden by peer specific configuration. |
18322efd RZ |
789 | */ |
790 | if (bpc->bpc_has_profile) | |
791 | bfd_profile_apply(bpc->bpc_profile, bs); | |
e9e2c950 RZ |
792 | } |
793 | ||
794 | static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc) | |
795 | { | |
796 | /* User didn't want to update, return failure. */ | |
797 | if (bpc->bpc_createonly) | |
798 | return -1; | |
799 | ||
800 | _bfd_session_update(bs, bpc); | |
801 | ||
e9e2c950 RZ |
802 | control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs); |
803 | ||
804 | return 0; | |
805 | } | |
806 | ||
014cab13 | 807 | void bfd_session_free(struct bfd_session *bs) |
e9e2c950 | 808 | { |
d245e522 | 809 | struct bfd_session_observer *bso; |
e9e2c950 | 810 | |
d245e522 | 811 | bfd_session_disable(bs); |
e9e2c950 | 812 | |
efd04d60 RZ |
813 | /* Remove session from data plane if any. */ |
814 | bfd_dplane_delete_session(bs); | |
815 | ||
79b4a6fc RZ |
816 | bfd_key_delete(bs->key); |
817 | bfd_id_delete(bs->discrs.my_discr); | |
818 | ||
d245e522 RZ |
819 | /* Remove observer if any. */ |
820 | TAILQ_FOREACH(bso, &bglobal.bg_obslist, bso_entry) { | |
821 | if (bso->bso_bs != bs) | |
822 | continue; | |
823 | ||
824 | break; | |
825 | } | |
826 | if (bso != NULL) | |
827 | bs_observer_del(bso); | |
e9e2c950 RZ |
828 | |
829 | pl_free(bs->pl); | |
830 | ||
ccc9ada8 | 831 | XFREE(MTYPE_BFDD_PROFILE, bs->profile_name); |
e9e2c950 RZ |
832 | XFREE(MTYPE_BFDD_CONFIG, bs); |
833 | } | |
834 | ||
835 | struct bfd_session *ptm_bfd_sess_new(struct bfd_peer_cfg *bpc) | |
836 | { | |
837 | struct bfd_session *bfd, *l_bfd; | |
e9e2c950 RZ |
838 | |
839 | /* check to see if this needs a new session */ | |
840 | l_bfd = bs_peer_find(bpc); | |
841 | if (l_bfd) { | |
842 | /* Requesting a duplicated peer means update configuration. */ | |
843 | if (bfd_session_update(l_bfd, bpc) == 0) | |
844 | return l_bfd; | |
845 | else | |
846 | return NULL; | |
847 | } | |
848 | ||
d245e522 RZ |
849 | /* Get BFD session storage with its defaults. */ |
850 | bfd = bfd_session_new(); | |
e9e2c950 | 851 | |
d245e522 RZ |
852 | /* |
853 | * Store interface/VRF name in case we need to delay session | |
854 | * start. See `bfd_session_enable` for more information. | |
855 | */ | |
856 | if (bpc->bpc_has_localif) | |
79b4a6fc RZ |
857 | strlcpy(bfd->key.ifname, bpc->bpc_localif, |
858 | sizeof(bfd->key.ifname)); | |
e9e2c950 | 859 | |
d245e522 | 860 | if (bpc->bpc_has_vrfname) |
79b4a6fc RZ |
861 | strlcpy(bfd->key.vrfname, bpc->bpc_vrfname, |
862 | sizeof(bfd->key.vrfname)); | |
adc26455 RZ |
863 | else |
864 | strlcpy(bfd->key.vrfname, VRF_DEFAULT_NAME, | |
865 | sizeof(bfd->key.vrfname)); | |
e9e2c950 | 866 | |
d245e522 RZ |
867 | /* Copy remaining data. */ |
868 | if (bpc->bpc_ipv4 == false) | |
b88113ef | 869 | SET_FLAG(bfd->flags, BFD_SESS_FLAG_IPV6); |
e9e2c950 | 870 | |
79b4a6fc RZ |
871 | bfd->key.family = (bpc->bpc_ipv4) ? AF_INET : AF_INET6; |
872 | switch (bfd->key.family) { | |
873 | case AF_INET: | |
874 | memcpy(&bfd->key.peer, &bpc->bpc_peer.sa_sin.sin_addr, | |
875 | sizeof(bpc->bpc_peer.sa_sin.sin_addr)); | |
876 | memcpy(&bfd->key.local, &bpc->bpc_local.sa_sin.sin_addr, | |
877 | sizeof(bpc->bpc_local.sa_sin.sin_addr)); | |
878 | break; | |
879 | ||
880 | case AF_INET6: | |
881 | memcpy(&bfd->key.peer, &bpc->bpc_peer.sa_sin6.sin6_addr, | |
882 | sizeof(bpc->bpc_peer.sa_sin6.sin6_addr)); | |
883 | memcpy(&bfd->key.local, &bpc->bpc_local.sa_sin6.sin6_addr, | |
884 | sizeof(bpc->bpc_local.sa_sin6.sin6_addr)); | |
885 | break; | |
886 | ||
887 | default: | |
888 | assert(1); | |
889 | break; | |
d245e522 | 890 | } |
e9e2c950 | 891 | |
79b4a6fc | 892 | if (bpc->bpc_mhop) |
b88113ef | 893 | SET_FLAG(bfd->flags, BFD_SESS_FLAG_MH); |
79b4a6fc RZ |
894 | |
895 | bfd->key.mhop = bpc->bpc_mhop; | |
896 | ||
014cab13 RZ |
897 | if (bs_registrate(bfd) == NULL) |
898 | return NULL; | |
899 | ||
900 | /* Apply other configurations. */ | |
901 | _bfd_session_update(bfd, bpc); | |
902 | ||
903 | return bfd; | |
904 | } | |
905 | ||
906 | struct bfd_session *bs_registrate(struct bfd_session *bfd) | |
907 | { | |
79b4a6fc RZ |
908 | /* Registrate session into data structures. */ |
909 | bfd_key_insert(bfd); | |
910 | bfd->discrs.my_discr = ptm_bfd_gen_ID(); | |
911 | bfd_id_insert(bfd); | |
d245e522 RZ |
912 | |
913 | /* Try to enable session and schedule for packet receive/send. */ | |
914 | if (bfd_session_enable(bfd) == -1) { | |
915 | /* Unrecoverable failure, remove the session/peer. */ | |
916 | bfd_session_free(bfd); | |
917 | return NULL; | |
e9e2c950 RZ |
918 | } |
919 | ||
261e0ba9 RZ |
920 | /* Add observer if we have moving parts. */ |
921 | if (bfd->key.ifname[0] || bfd->key.vrfname[0] || bfd->sock == -1) | |
922 | bs_observer_add(bfd); | |
923 | ||
48da2c31 RZ |
924 | if (bglobal.debug_peer_event) |
925 | zlog_debug("session-new: %s", bs_to_string(bfd)); | |
e9e2c950 RZ |
926 | |
927 | control_notify_config(BCM_NOTIFY_CONFIG_ADD, bfd); | |
928 | ||
929 | return bfd; | |
930 | } | |
931 | ||
bc50bcc8 | 932 | int ptm_bfd_sess_del(struct bfd_peer_cfg *bpc) |
e9e2c950 RZ |
933 | { |
934 | struct bfd_session *bs; | |
935 | ||
936 | /* Find session and call free(). */ | |
937 | bs = bs_peer_find(bpc); | |
938 | if (bs == NULL) | |
939 | return -1; | |
940 | ||
941 | /* This pointer is being referenced, don't let it be deleted. */ | |
942 | if (bs->refcount > 0) { | |
6cde4b45 | 943 | zlog_err("session-delete: refcount failure: %" PRIu64" references", |
259b64eb | 944 | bs->refcount); |
e9e2c950 RZ |
945 | return -1; |
946 | } | |
947 | ||
48da2c31 RZ |
948 | if (bglobal.debug_peer_event) |
949 | zlog_debug("session-delete: %s", bs_to_string(bs)); | |
e9e2c950 RZ |
950 | |
951 | control_notify_config(BCM_NOTIFY_CONFIG_DELETE, bs); | |
952 | ||
953 | bfd_session_free(bs); | |
954 | ||
955 | return 0; | |
956 | } | |
957 | ||
d3f3a2c4 RZ |
958 | void bfd_set_polling(struct bfd_session *bs) |
959 | { | |
c0ef9a8a RZ |
960 | /* |
961 | * Start polling procedure: the only timers that require polling | |
962 | * to change value without losing connection are: | |
963 | * | |
964 | * - Desired minimum transmission interval; | |
965 | * - Required minimum receive interval; | |
966 | * | |
967 | * RFC 5880, Section 6.8.3. | |
968 | */ | |
d3f3a2c4 RZ |
969 | bs->polling = 1; |
970 | } | |
971 | ||
aef131af RZ |
972 | /* |
973 | * bs_<state>_handler() functions implement the BFD state machine | |
974 | * transition mechanism. `<state>` is the current session state and | |
975 | * the parameter `nstate` is the peer new state. | |
976 | */ | |
9f37770f RZ |
977 | static void bs_admin_down_handler(struct bfd_session *bs |
978 | __attribute__((__unused__)), | |
979 | int nstate __attribute__((__unused__))) | |
aef131af RZ |
980 | { |
981 | /* | |
982 | * We are administratively down, there is no state machine | |
983 | * handling. | |
984 | */ | |
985 | } | |
986 | ||
9f37770f | 987 | static void bs_down_handler(struct bfd_session *bs, int nstate) |
aef131af RZ |
988 | { |
989 | switch (nstate) { | |
990 | case PTM_BFD_ADM_DOWN: | |
991 | /* | |
992 | * Remote peer doesn't want to talk, so lets keep the | |
993 | * connection down. | |
994 | */ | |
995 | case PTM_BFD_UP: | |
996 | /* Peer can't be up yet, wait it go to 'init' or 'down'. */ | |
997 | break; | |
998 | ||
999 | case PTM_BFD_DOWN: | |
1000 | /* | |
1001 | * Remote peer agreed that the path is down, lets try to | |
1002 | * bring it up. | |
1003 | */ | |
1004 | bs->ses_state = PTM_BFD_INIT; | |
1a2e2fff | 1005 | |
fd2109e5 | 1006 | /* |
1007 | * RFC 5880, Section 6.1. | |
1008 | * A system taking the Passive role MUST NOT begin | |
1009 | * sending BFD packets for a particular session until | |
1010 | * it has received a BFD packet for that session, and thus | |
1011 | * has learned the remote system's discriminator value. | |
1012 | * | |
1013 | * Now we can start transmission timer in passive mode. | |
1014 | */ | |
1a2e2fff | 1015 | if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE)) |
fd2109e5 | 1016 | ptm_bfd_xmt_TO(bs, 0); |
1017 | ||
aef131af RZ |
1018 | break; |
1019 | ||
1020 | case PTM_BFD_INIT: | |
1021 | /* | |
1022 | * Remote peer told us his path is up, lets turn | |
1023 | * activate the session. | |
1024 | */ | |
bc50bcc8 | 1025 | ptm_bfd_sess_up(bs); |
aef131af RZ |
1026 | break; |
1027 | ||
1028 | default: | |
48da2c31 RZ |
1029 | if (bglobal.debug_peer_event) |
1030 | zlog_debug("state-change: unhandled neighbor state: %d", | |
1031 | nstate); | |
aef131af RZ |
1032 | break; |
1033 | } | |
1034 | } | |
1035 | ||
9f37770f | 1036 | static void bs_init_handler(struct bfd_session *bs, int nstate) |
aef131af RZ |
1037 | { |
1038 | switch (nstate) { | |
1039 | case PTM_BFD_ADM_DOWN: | |
1040 | /* | |
1041 | * Remote peer doesn't want to talk, so lets make the | |
1042 | * connection down. | |
1043 | */ | |
fd2109e5 | 1044 | ptm_bfd_sess_dn(bs, BD_NEIGHBOR_DOWN); |
aef131af RZ |
1045 | break; |
1046 | ||
1047 | case PTM_BFD_DOWN: | |
1048 | /* Remote peer hasn't moved to first stage yet. */ | |
1049 | break; | |
1050 | ||
1051 | case PTM_BFD_INIT: | |
1052 | case PTM_BFD_UP: | |
1053 | /* We agreed on the settings and the path is up. */ | |
bc50bcc8 | 1054 | ptm_bfd_sess_up(bs); |
aef131af RZ |
1055 | break; |
1056 | ||
1057 | default: | |
48da2c31 RZ |
1058 | if (bglobal.debug_peer_event) |
1059 | zlog_debug("state-change: unhandled neighbor state: %d", | |
1060 | nstate); | |
aef131af RZ |
1061 | break; |
1062 | } | |
1063 | } | |
1064 | ||
9f37770f | 1065 | static void bs_up_handler(struct bfd_session *bs, int nstate) |
aef131af RZ |
1066 | { |
1067 | switch (nstate) { | |
1068 | case PTM_BFD_ADM_DOWN: | |
1069 | case PTM_BFD_DOWN: | |
1070 | /* Peer lost or asked to shutdown connection. */ | |
bc50bcc8 | 1071 | ptm_bfd_sess_dn(bs, BD_NEIGHBOR_DOWN); |
aef131af RZ |
1072 | break; |
1073 | ||
1074 | case PTM_BFD_INIT: | |
1075 | case PTM_BFD_UP: | |
1076 | /* Path is up and working. */ | |
1077 | break; | |
1078 | ||
1079 | default: | |
48da2c31 RZ |
1080 | if (bglobal.debug_peer_event) |
1081 | zlog_debug("state-change: unhandled neighbor state: %d", | |
1082 | nstate); | |
aef131af RZ |
1083 | break; |
1084 | } | |
1085 | } | |
1086 | ||
1087 | void bs_state_handler(struct bfd_session *bs, int nstate) | |
1088 | { | |
1089 | switch (bs->ses_state) { | |
1090 | case PTM_BFD_ADM_DOWN: | |
1091 | bs_admin_down_handler(bs, nstate); | |
1092 | break; | |
1093 | case PTM_BFD_DOWN: | |
1094 | bs_down_handler(bs, nstate); | |
1095 | break; | |
1096 | case PTM_BFD_INIT: | |
1097 | bs_init_handler(bs, nstate); | |
1098 | break; | |
1099 | case PTM_BFD_UP: | |
1100 | bs_up_handler(bs, nstate); | |
1101 | break; | |
1102 | ||
1103 | default: | |
48da2c31 RZ |
1104 | if (bglobal.debug_peer_event) |
1105 | zlog_debug("state-change: [%s] is in invalid state: %d", | |
1106 | bs_to_string(bs), nstate); | |
aef131af RZ |
1107 | break; |
1108 | } | |
1109 | } | |
1110 | ||
c0ef9a8a RZ |
1111 | /* |
1112 | * Handles echo timer manipulation after updating timer. | |
1113 | */ | |
1114 | void bs_echo_timer_handler(struct bfd_session *bs) | |
1115 | { | |
1116 | uint32_t old_timer; | |
1117 | ||
1118 | /* | |
1119 | * Before doing any echo handling, check if it is possible to | |
1120 | * use it. | |
1121 | * | |
1122 | * - Check for `echo-mode` configuration. | |
1123 | * - Check that we are not using multi hop (RFC 5883, | |
1124 | * Section 3). | |
1125 | * - Check that we are already at the up state. | |
1126 | */ | |
b88113ef RZ |
1127 | if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO) == 0 |
1128 | || CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH) | |
c0ef9a8a RZ |
1129 | || bs->ses_state != PTM_BFD_UP) |
1130 | return; | |
1131 | ||
1132 | /* Remote peer asked to stop echo. */ | |
1133 | if (bs->remote_timers.required_min_echo == 0) { | |
b88113ef | 1134 | if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO_ACTIVE)) |
8bd859f6 | 1135 | ptm_bfd_echo_stop(bs); |
c0ef9a8a RZ |
1136 | |
1137 | return; | |
1138 | } | |
1139 | ||
1140 | /* | |
1141 | * Calculate the echo transmission timer: we must not send | |
1142 | * echo packets faster than the minimum required time | |
1143 | * announced by the remote system. | |
1144 | * | |
1145 | * RFC 5880, Section 6.8.9. | |
1146 | */ | |
1147 | old_timer = bs->echo_xmt_TO; | |
4df3e31c | 1148 | if (bs->remote_timers.required_min_echo > bs->timers.desired_min_echo_tx) |
c0ef9a8a RZ |
1149 | bs->echo_xmt_TO = bs->remote_timers.required_min_echo; |
1150 | else | |
4df3e31c | 1151 | bs->echo_xmt_TO = bs->timers.desired_min_echo_tx; |
c0ef9a8a | 1152 | |
b88113ef | 1153 | if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO_ACTIVE) == 0 |
c0ef9a8a RZ |
1154 | || old_timer != bs->echo_xmt_TO) |
1155 | ptm_bfd_echo_start(bs); | |
1156 | } | |
1157 | ||
1158 | /* | |
1159 | * RFC 5880 Section 6.5. | |
1160 | * | |
1161 | * When a BFD control packet with the final bit is received, we must | |
1162 | * update the session parameters. | |
1163 | */ | |
1164 | void bs_final_handler(struct bfd_session *bs) | |
1165 | { | |
1166 | /* Start using our new timers. */ | |
f43b9368 RZ |
1167 | bs->cur_timers.desired_min_tx = bs->timers.desired_min_tx; |
1168 | bs->cur_timers.required_min_rx = bs->timers.required_min_rx; | |
c0ef9a8a RZ |
1169 | |
1170 | /* | |
1171 | * TODO: demand mode. See RFC 5880 Section 6.1. | |
1172 | * | |
1173 | * When using demand mode we must disable the detection timer | |
1174 | * for lost control packets. | |
1175 | */ | |
1176 | if (bs->demand_mode) { | |
1177 | /* Notify watchers about changed timers. */ | |
1178 | control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs); | |
1179 | return; | |
1180 | } | |
1181 | ||
1182 | /* | |
4caf6388 | 1183 | * Calculate transmission time based on new timers. |
c0ef9a8a RZ |
1184 | * |
1185 | * Transmission calculation: | |
4caf6388 RZ |
1186 | * Unless specified by exceptions at the end of Section 6.8.7, the |
1187 | * transmission time will be determined by the system with the | |
1188 | * slowest rate. | |
c0ef9a8a | 1189 | * |
4caf6388 | 1190 | * RFC 5880, Section 6.8.7. |
c0ef9a8a RZ |
1191 | */ |
1192 | if (bs->timers.desired_min_tx > bs->remote_timers.required_min_rx) | |
c0ef9a8a | 1193 | bs->xmt_TO = bs->timers.desired_min_tx; |
4caf6388 RZ |
1194 | else |
1195 | bs->xmt_TO = bs->remote_timers.required_min_rx; | |
c0ef9a8a RZ |
1196 | |
1197 | /* Apply new transmission timer immediately. */ | |
1198 | ptm_bfd_start_xmt_timer(bs, false); | |
1199 | ||
c0ef9a8a RZ |
1200 | /* Notify watchers about changed timers. */ |
1201 | control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs); | |
1202 | } | |
1203 | ||
b912b189 RZ |
1204 | void bs_set_slow_timers(struct bfd_session *bs) |
1205 | { | |
1206 | /* | |
1207 | * BFD connection must use slow timers before going up or after | |
1208 | * losing connectivity to avoid wasting bandwidth. | |
1209 | * | |
1210 | * RFC 5880, Section 6.8.3. | |
1211 | */ | |
1212 | bs->cur_timers.desired_min_tx = BFD_DEF_SLOWTX; | |
1213 | bs->cur_timers.required_min_rx = BFD_DEF_SLOWTX; | |
1214 | bs->cur_timers.required_min_echo = 0; | |
1215 | ||
1216 | /* Set the appropriated timeouts for slow connection. */ | |
1217 | bs->detect_TO = (BFD_DEFDETECTMULT * BFD_DEF_SLOWTX); | |
1218 | bs->xmt_TO = BFD_DEF_SLOWTX; | |
1219 | } | |
e9e2c950 | 1220 | |
4d12e1f9 RZ |
1221 | void bfd_set_echo(struct bfd_session *bs, bool echo) |
1222 | { | |
1223 | if (echo) { | |
1224 | /* Check if echo mode is already active. */ | |
1225 | if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO)) | |
1226 | return; | |
1227 | ||
1228 | SET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO); | |
1229 | ||
1230 | /* Activate/update echo receive timeout timer. */ | |
efd04d60 RZ |
1231 | if (bs->bdc == NULL) |
1232 | bs_echo_timer_handler(bs); | |
4d12e1f9 RZ |
1233 | } else { |
1234 | /* Check if echo mode is already disabled. */ | |
1235 | if (!CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO)) | |
1236 | return; | |
1237 | ||
1238 | UNSET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO); | |
efd04d60 RZ |
1239 | |
1240 | /* Deactivate timeout timer. */ | |
1241 | if (bs->bdc == NULL) | |
1242 | ptm_bfd_echo_stop(bs); | |
4d12e1f9 RZ |
1243 | } |
1244 | } | |
1245 | ||
1246 | void bfd_set_shutdown(struct bfd_session *bs, bool shutdown) | |
1247 | { | |
ccc9ada8 RZ |
1248 | bool is_shutdown; |
1249 | ||
1250 | /* | |
1251 | * Special case: we are batching changes and the previous state was | |
1252 | * not shutdown. Instead of potentially disconnect a running peer, | |
1253 | * we'll get the current status to validate we were really down. | |
1254 | */ | |
1255 | if (bs->ses_state == PTM_BFD_UP) | |
1256 | is_shutdown = false; | |
1257 | else | |
1258 | is_shutdown = CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN); | |
1259 | ||
4d12e1f9 RZ |
1260 | if (shutdown) { |
1261 | /* Already shutdown. */ | |
ccc9ada8 | 1262 | if (is_shutdown) |
4d12e1f9 RZ |
1263 | return; |
1264 | ||
1265 | SET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN); | |
1266 | ||
efd04d60 RZ |
1267 | /* Handle data plane shutdown case. */ |
1268 | if (bs->bdc) { | |
1269 | bs->ses_state = PTM_BFD_ADM_DOWN; | |
1270 | bfd_dplane_update_session(bs); | |
1271 | control_notify(bs, bs->ses_state); | |
1272 | return; | |
1273 | } | |
1274 | ||
4d12e1f9 RZ |
1275 | /* Disable all events. */ |
1276 | bfd_recvtimer_delete(bs); | |
1277 | bfd_echo_recvtimer_delete(bs); | |
1278 | bfd_xmttimer_delete(bs); | |
1279 | bfd_echo_xmttimer_delete(bs); | |
1280 | ||
1281 | /* Change and notify state change. */ | |
1282 | bs->ses_state = PTM_BFD_ADM_DOWN; | |
1283 | control_notify(bs, bs->ses_state); | |
1284 | ||
1285 | /* Don't try to send packets with a disabled session. */ | |
1286 | if (bs->sock != -1) | |
1287 | ptm_bfd_snd(bs, 0); | |
1288 | } else { | |
1289 | /* Already working. */ | |
ccc9ada8 | 1290 | if (!is_shutdown) |
4d12e1f9 RZ |
1291 | return; |
1292 | ||
1293 | UNSET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN); | |
1294 | ||
efd04d60 RZ |
1295 | /* Handle data plane shutdown case. */ |
1296 | if (bs->bdc) { | |
1297 | bs->ses_state = PTM_BFD_DOWN; | |
1298 | bfd_dplane_update_session(bs); | |
1299 | control_notify(bs, bs->ses_state); | |
1300 | return; | |
1301 | } | |
1302 | ||
4d12e1f9 RZ |
1303 | /* Change and notify state change. */ |
1304 | bs->ses_state = PTM_BFD_DOWN; | |
1305 | control_notify(bs, bs->ses_state); | |
1306 | ||
1a2e2fff RZ |
1307 | /* Enable timers if non passive, otherwise stop them. */ |
1308 | if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE)) { | |
1309 | bfd_recvtimer_delete(bs); | |
1310 | bfd_xmttimer_delete(bs); | |
1311 | } else { | |
1312 | bfd_recvtimer_update(bs); | |
1313 | bfd_xmttimer_update(bs, bs->xmt_TO); | |
1314 | } | |
1315 | } | |
1316 | } | |
1317 | ||
1318 | void bfd_set_passive_mode(struct bfd_session *bs, bool passive) | |
1319 | { | |
1320 | if (passive) { | |
1321 | SET_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE); | |
1322 | ||
1323 | /* Session is already up and running, nothing to do now. */ | |
1324 | if (bs->ses_state != PTM_BFD_DOWN) | |
1325 | return; | |
1326 | ||
1327 | /* Lets disable the timers since we are now passive. */ | |
1328 | bfd_recvtimer_delete(bs); | |
1329 | bfd_xmttimer_delete(bs); | |
1330 | } else { | |
1331 | UNSET_FLAG(bs->flags, BFD_SESS_FLAG_PASSIVE); | |
1332 | ||
1333 | /* Session is already up and running, nothing to do now. */ | |
1334 | if (bs->ses_state != PTM_BFD_DOWN) | |
1335 | return; | |
1336 | ||
1337 | /* Session is down, let it attempt to start the connection. */ | |
4d12e1f9 | 1338 | bfd_xmttimer_update(bs, bs->xmt_TO); |
1a2e2fff | 1339 | bfd_recvtimer_update(bs); |
4d12e1f9 RZ |
1340 | } |
1341 | } | |
1342 | ||
e9e2c950 RZ |
1343 | /* |
1344 | * Helper functions. | |
1345 | */ | |
1346 | static const char *get_diag_str(int diag) | |
1347 | { | |
1348 | for (int i = 0; diag_list[i].str; i++) { | |
1349 | if (diag_list[i].type == diag) | |
1350 | return diag_list[i].str; | |
1351 | } | |
1352 | return "N/A"; | |
1353 | } | |
1354 | ||
6e10bd97 | 1355 | const char *satostr(const struct sockaddr_any *sa) |
e9e2c950 RZ |
1356 | { |
1357 | #define INETSTR_BUFCOUNT 8 | |
1358 | static char buf[INETSTR_BUFCOUNT][INET6_ADDRSTRLEN]; | |
1359 | static int bufidx; | |
6e10bd97 RZ |
1360 | const struct sockaddr_in *sin = &sa->sa_sin; |
1361 | const struct sockaddr_in6 *sin6 = &sa->sa_sin6; | |
e9e2c950 RZ |
1362 | |
1363 | bufidx += (bufidx + 1) % INETSTR_BUFCOUNT; | |
1364 | buf[bufidx][0] = 0; | |
1365 | ||
1366 | switch (sin->sin_family) { | |
1367 | case AF_INET: | |
1368 | inet_ntop(AF_INET, &sin->sin_addr, buf[bufidx], | |
1369 | sizeof(buf[bufidx])); | |
1370 | break; | |
1371 | case AF_INET6: | |
1372 | inet_ntop(AF_INET6, &sin6->sin6_addr, buf[bufidx], | |
1373 | sizeof(buf[bufidx])); | |
1374 | break; | |
1375 | ||
1376 | default: | |
1377 | strlcpy(buf[bufidx], "unknown", sizeof(buf[bufidx])); | |
1378 | break; | |
1379 | } | |
1380 | ||
1381 | return buf[bufidx]; | |
1382 | } | |
1383 | ||
1384 | const char *diag2str(uint8_t diag) | |
1385 | { | |
1386 | switch (diag) { | |
1387 | case 0: | |
1388 | return "ok"; | |
1389 | case 1: | |
1390 | return "control detection time expired"; | |
1391 | case 2: | |
1392 | return "echo function failed"; | |
1393 | case 3: | |
1394 | return "neighbor signaled session down"; | |
1395 | case 4: | |
1396 | return "forwarding plane reset"; | |
1397 | case 5: | |
1398 | return "path down"; | |
1399 | case 6: | |
1400 | return "concatenated path down"; | |
1401 | case 7: | |
1402 | return "administratively down"; | |
1403 | case 8: | |
1404 | return "reverse concatenated path down"; | |
1405 | default: | |
1406 | return "unknown"; | |
1407 | } | |
1408 | } | |
1409 | ||
1410 | int strtosa(const char *addr, struct sockaddr_any *sa) | |
1411 | { | |
1412 | memset(sa, 0, sizeof(*sa)); | |
1413 | ||
1414 | if (inet_pton(AF_INET, addr, &sa->sa_sin.sin_addr) == 1) { | |
1415 | sa->sa_sin.sin_family = AF_INET; | |
1416 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN | |
1417 | sa->sa_sin.sin_len = sizeof(sa->sa_sin); | |
1418 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
1419 | return 0; | |
1420 | } | |
1421 | ||
1422 | if (inet_pton(AF_INET6, addr, &sa->sa_sin6.sin6_addr) == 1) { | |
1423 | sa->sa_sin6.sin6_family = AF_INET6; | |
1424 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN | |
1425 | sa->sa_sin6.sin6_len = sizeof(sa->sa_sin6); | |
1426 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
1427 | return 0; | |
1428 | } | |
1429 | ||
1430 | return -1; | |
1431 | } | |
1432 | ||
1433 | void integer2timestr(uint64_t time, char *buf, size_t buflen) | |
1434 | { | |
46da676a | 1435 | uint64_t year, month, day, hour, minute, second; |
e9e2c950 RZ |
1436 | int rv; |
1437 | ||
1438 | #define MINUTES (60) | |
23586b05 DS |
1439 | #define HOURS (60 * MINUTES) |
1440 | #define DAYS (24 * HOURS) | |
1441 | #define MONTHS (30 * DAYS) | |
1442 | #define YEARS (12 * MONTHS) | |
e9e2c950 RZ |
1443 | if (time >= YEARS) { |
1444 | year = time / YEARS; | |
1445 | time -= year * YEARS; | |
1446 | ||
46da676a | 1447 | rv = snprintfrr(buf, buflen, "%" PRIu64 " year(s), ", year); |
e9e2c950 RZ |
1448 | buf += rv; |
1449 | buflen -= rv; | |
1450 | } | |
1451 | if (time >= MONTHS) { | |
1452 | month = time / MONTHS; | |
1453 | time -= month * MONTHS; | |
1454 | ||
46da676a | 1455 | rv = snprintfrr(buf, buflen, "%" PRIu64 " month(s), ", month); |
e9e2c950 RZ |
1456 | buf += rv; |
1457 | buflen -= rv; | |
1458 | } | |
1459 | if (time >= DAYS) { | |
1460 | day = time / DAYS; | |
1461 | time -= day * DAYS; | |
1462 | ||
46da676a | 1463 | rv = snprintfrr(buf, buflen, "%" PRIu64 " day(s), ", day); |
e9e2c950 RZ |
1464 | buf += rv; |
1465 | buflen -= rv; | |
1466 | } | |
1467 | if (time >= HOURS) { | |
1468 | hour = time / HOURS; | |
1469 | time -= hour * HOURS; | |
1470 | ||
46da676a | 1471 | rv = snprintfrr(buf, buflen, "%" PRIu64 " hour(s), ", hour); |
e9e2c950 RZ |
1472 | buf += rv; |
1473 | buflen -= rv; | |
1474 | } | |
1475 | if (time >= MINUTES) { | |
1476 | minute = time / MINUTES; | |
1477 | time -= minute * MINUTES; | |
1478 | ||
46da676a | 1479 | rv = snprintfrr(buf, buflen, "%" PRIu64 " minute(s), ", minute); |
e9e2c950 RZ |
1480 | buf += rv; |
1481 | buflen -= rv; | |
1482 | } | |
1483 | second = time % MINUTES; | |
46da676a | 1484 | snprintfrr(buf, buflen, "%" PRIu64 " second(s)", second); |
e9e2c950 RZ |
1485 | } |
1486 | ||
79b4a6fc | 1487 | const char *bs_to_string(const struct bfd_session *bs) |
03e7f088 RZ |
1488 | { |
1489 | static char buf[256]; | |
79b4a6fc | 1490 | char addr_buf[INET6_ADDRSTRLEN]; |
03e7f088 | 1491 | int pos; |
b88113ef | 1492 | bool is_mhop = CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH); |
03e7f088 RZ |
1493 | |
1494 | pos = snprintf(buf, sizeof(buf), "mhop:%s", is_mhop ? "yes" : "no"); | |
79b4a6fc RZ |
1495 | pos += snprintf(buf + pos, sizeof(buf) - pos, " peer:%s", |
1496 | inet_ntop(bs->key.family, &bs->key.peer, addr_buf, | |
1497 | sizeof(addr_buf))); | |
1498 | pos += snprintf(buf + pos, sizeof(buf) - pos, " local:%s", | |
1499 | inet_ntop(bs->key.family, &bs->key.local, addr_buf, | |
1500 | sizeof(addr_buf))); | |
1501 | if (bs->key.vrfname[0]) | |
1502 | pos += snprintf(buf + pos, sizeof(buf) - pos, " vrf:%s", | |
1503 | bs->key.vrfname); | |
1504 | if (bs->key.ifname[0]) | |
1505 | pos += snprintf(buf + pos, sizeof(buf) - pos, " ifname:%s", | |
1506 | bs->key.ifname); | |
3cef9b7f DS |
1507 | |
1508 | (void)pos; | |
1509 | ||
03e7f088 RZ |
1510 | return buf; |
1511 | } | |
1512 | ||
d245e522 RZ |
1513 | int bs_observer_add(struct bfd_session *bs) |
1514 | { | |
1515 | struct bfd_session_observer *bso; | |
1516 | ||
ed74032b | 1517 | bso = XCALLOC(MTYPE_BFDD_SESSION_OBSERVER, sizeof(*bso)); |
d245e522 | 1518 | bso->bso_bs = bs; |
ced291de RZ |
1519 | bso->bso_addr.family = bs->key.family; |
1520 | memcpy(&bso->bso_addr.u.prefix, &bs->key.local, | |
1521 | sizeof(bs->key.local)); | |
261e0ba9 | 1522 | |
d245e522 RZ |
1523 | TAILQ_INSERT_TAIL(&bglobal.bg_obslist, bso, bso_entry); |
1524 | ||
1525 | return 0; | |
1526 | } | |
1527 | ||
1528 | void bs_observer_del(struct bfd_session_observer *bso) | |
1529 | { | |
1530 | TAILQ_REMOVE(&bglobal.bg_obslist, bso, bso_entry); | |
1531 | XFREE(MTYPE_BFDD_SESSION_OBSERVER, bso); | |
1532 | } | |
1533 | ||
79b4a6fc RZ |
1534 | void bs_to_bpc(struct bfd_session *bs, struct bfd_peer_cfg *bpc) |
1535 | { | |
1536 | memset(bpc, 0, sizeof(*bpc)); | |
1537 | ||
1538 | bpc->bpc_ipv4 = (bs->key.family == AF_INET); | |
1539 | bpc->bpc_mhop = bs->key.mhop; | |
1540 | ||
1541 | switch (bs->key.family) { | |
1542 | case AF_INET: | |
1543 | bpc->bpc_peer.sa_sin.sin_family = AF_INET; | |
1544 | memcpy(&bpc->bpc_peer.sa_sin.sin_addr, &bs->key.peer, | |
1545 | sizeof(bpc->bpc_peer.sa_sin.sin_addr)); | |
1546 | ||
1547 | if (memcmp(&bs->key.local, &zero_addr, sizeof(bs->key.local))) { | |
1548 | bpc->bpc_local.sa_sin.sin_family = AF_INET6; | |
5ff7d1be | 1549 | memcpy(&bpc->bpc_local.sa_sin.sin_addr, &bs->key.local, |
79b4a6fc RZ |
1550 | sizeof(bpc->bpc_local.sa_sin.sin_addr)); |
1551 | } | |
1552 | break; | |
1553 | ||
1554 | case AF_INET6: | |
1555 | bpc->bpc_peer.sa_sin.sin_family = AF_INET6; | |
1556 | memcpy(&bpc->bpc_peer.sa_sin6.sin6_addr, &bs->key.peer, | |
1557 | sizeof(bpc->bpc_peer.sa_sin6.sin6_addr)); | |
1558 | ||
1559 | bpc->bpc_local.sa_sin6.sin6_family = AF_INET6; | |
5ff7d1be | 1560 | memcpy(&bpc->bpc_local.sa_sin6.sin6_addr, &bs->key.local, |
79b4a6fc RZ |
1561 | sizeof(bpc->bpc_local.sa_sin6.sin6_addr)); |
1562 | break; | |
1563 | } | |
1564 | ||
1565 | if (bs->key.ifname[0]) { | |
1566 | bpc->bpc_has_localif = true; | |
1567 | strlcpy(bpc->bpc_localif, bs->key.ifname, | |
1568 | sizeof(bpc->bpc_localif)); | |
1569 | } | |
1570 | ||
1571 | if (bs->key.vrfname[0]) { | |
1572 | bpc->bpc_has_vrfname = true; | |
1573 | strlcpy(bpc->bpc_vrfname, bs->key.vrfname, | |
1574 | sizeof(bpc->bpc_vrfname)); | |
1575 | } | |
1576 | } | |
1577 | ||
e9e2c950 RZ |
1578 | |
1579 | /* | |
1580 | * BFD hash data structures to find sessions. | |
1581 | */ | |
1582 | static struct hash *bfd_id_hash; | |
79b4a6fc | 1583 | static struct hash *bfd_key_hash; |
e9e2c950 | 1584 | |
d8b87afe QY |
1585 | static unsigned int bfd_id_hash_do(const void *p); |
1586 | static unsigned int bfd_key_hash_do(const void *p); | |
e9e2c950 | 1587 | |
e3b78da8 | 1588 | static void _bfd_free(struct hash_bucket *hb, |
e9e2c950 | 1589 | void *arg __attribute__((__unused__))); |
e9e2c950 RZ |
1590 | |
1591 | /* BFD hash for our discriminator. */ | |
d8b87afe | 1592 | static unsigned int bfd_id_hash_do(const void *p) |
e9e2c950 | 1593 | { |
d8b87afe | 1594 | const struct bfd_session *bs = p; |
e9e2c950 RZ |
1595 | |
1596 | return jhash_1word(bs->discrs.my_discr, 0); | |
1597 | } | |
1598 | ||
74df8d6d | 1599 | static bool bfd_id_hash_cmp(const void *n1, const void *n2) |
e9e2c950 RZ |
1600 | { |
1601 | const struct bfd_session *bs1 = n1, *bs2 = n2; | |
1602 | ||
1603 | return bs1->discrs.my_discr == bs2->discrs.my_discr; | |
1604 | } | |
1605 | ||
1606 | /* BFD hash for single hop. */ | |
d8b87afe | 1607 | static unsigned int bfd_key_hash_do(const void *p) |
e9e2c950 | 1608 | { |
d8b87afe | 1609 | const struct bfd_session *bs = p; |
6cfcb775 | 1610 | struct bfd_key key = bs->key; |
e9e2c950 | 1611 | |
6cfcb775 IR |
1612 | /* |
1613 | * Local address and interface name are optional and | |
1614 | * can be filled any time after session creation. | |
1615 | * Hash key should not depend on these fields. | |
1616 | */ | |
1617 | memset(&key.local, 0, sizeof(key.local)); | |
1618 | memset(key.ifname, 0, sizeof(key.ifname)); | |
1619 | ||
1620 | return jhash(&key, sizeof(key), 0); | |
e9e2c950 RZ |
1621 | } |
1622 | ||
79b4a6fc | 1623 | static bool bfd_key_hash_cmp(const void *n1, const void *n2) |
e9e2c950 RZ |
1624 | { |
1625 | const struct bfd_session *bs1 = n1, *bs2 = n2; | |
1626 | ||
6cfcb775 IR |
1627 | if (bs1->key.family != bs2->key.family) |
1628 | return false; | |
1629 | if (bs1->key.mhop != bs2->key.mhop) | |
1630 | return false; | |
1631 | if (memcmp(&bs1->key.peer, &bs2->key.peer, sizeof(bs1->key.peer))) | |
1632 | return false; | |
1633 | if (memcmp(bs1->key.vrfname, bs2->key.vrfname, | |
1634 | sizeof(bs1->key.vrfname))) | |
1635 | return false; | |
1636 | ||
1637 | /* | |
1638 | * Local address is optional and can be empty. | |
1639 | * If both addresses are not empty and different, | |
1640 | * then the keys are different. | |
1641 | */ | |
1642 | if (memcmp(&bs1->key.local, &zero_addr, sizeof(bs1->key.local)) | |
1643 | && memcmp(&bs2->key.local, &zero_addr, sizeof(bs2->key.local)) | |
1644 | && memcmp(&bs1->key.local, &bs2->key.local, sizeof(bs1->key.local))) | |
1645 | return false; | |
1646 | ||
1647 | /* | |
1648 | * Interface name is optional and can be empty. | |
1649 | * If both names are not empty and different, | |
1650 | * then the keys are different. | |
1651 | */ | |
1652 | if (bs1->key.ifname[0] && bs2->key.ifname[0] | |
1653 | && memcmp(bs1->key.ifname, bs2->key.ifname, | |
1654 | sizeof(bs1->key.ifname))) | |
1655 | return false; | |
1656 | ||
1657 | return true; | |
e9e2c950 RZ |
1658 | } |
1659 | ||
e9e2c950 | 1660 | |
e9e2c950 RZ |
1661 | /* |
1662 | * Hash public interface / exported functions. | |
1663 | */ | |
1664 | ||
1665 | /* Lookup functions. */ | |
1666 | struct bfd_session *bfd_id_lookup(uint32_t id) | |
1667 | { | |
1668 | struct bfd_session bs; | |
1669 | ||
1670 | bs.discrs.my_discr = id; | |
1671 | ||
1672 | return hash_lookup(bfd_id_hash, &bs); | |
1673 | } | |
1674 | ||
79b4a6fc | 1675 | struct bfd_session *bfd_key_lookup(struct bfd_key key) |
e9e2c950 | 1676 | { |
6cfcb775 | 1677 | struct bfd_session bs; |
e9e2c950 | 1678 | |
79b4a6fc | 1679 | bs.key = key; |
e9e2c950 | 1680 | |
6cfcb775 | 1681 | return hash_lookup(bfd_key_hash, &bs); |
e9e2c950 RZ |
1682 | } |
1683 | ||
e9e2c950 RZ |
1684 | /* |
1685 | * Delete functions. | |
1686 | * | |
1687 | * Delete functions searches and remove the item from the hash and | |
1688 | * returns a pointer to the removed item data. If the item was not found | |
1689 | * then it returns NULL. | |
1690 | * | |
1691 | * The data stored inside the hash is not free()ed, so you must do it | |
1692 | * manually after getting the pointer back. | |
1693 | */ | |
1694 | struct bfd_session *bfd_id_delete(uint32_t id) | |
1695 | { | |
1696 | struct bfd_session bs; | |
1697 | ||
1698 | bs.discrs.my_discr = id; | |
1699 | ||
1700 | return hash_release(bfd_id_hash, &bs); | |
1701 | } | |
1702 | ||
79b4a6fc | 1703 | struct bfd_session *bfd_key_delete(struct bfd_key key) |
e9e2c950 | 1704 | { |
6cfcb775 | 1705 | struct bfd_session bs; |
e9e2c950 | 1706 | |
79b4a6fc | 1707 | bs.key = key; |
e9e2c950 | 1708 | |
6cfcb775 | 1709 | return hash_release(bfd_key_hash, &bs); |
e9e2c950 RZ |
1710 | } |
1711 | ||
e9e2c950 RZ |
1712 | /* Iteration functions. */ |
1713 | void bfd_id_iterate(hash_iter_func hif, void *arg) | |
1714 | { | |
1715 | hash_iterate(bfd_id_hash, hif, arg); | |
1716 | } | |
1717 | ||
79b4a6fc | 1718 | void bfd_key_iterate(hash_iter_func hif, void *arg) |
e9e2c950 | 1719 | { |
79b4a6fc | 1720 | hash_iterate(bfd_key_hash, hif, arg); |
e9e2c950 RZ |
1721 | } |
1722 | ||
e9e2c950 RZ |
1723 | /* |
1724 | * Insert functions. | |
1725 | * | |
1726 | * Inserts session into hash and returns `true` on success, otherwise | |
1727 | * `false`. | |
1728 | */ | |
1729 | bool bfd_id_insert(struct bfd_session *bs) | |
1730 | { | |
1731 | return (hash_get(bfd_id_hash, bs, hash_alloc_intern) == bs); | |
1732 | } | |
1733 | ||
79b4a6fc | 1734 | bool bfd_key_insert(struct bfd_session *bs) |
e9e2c950 | 1735 | { |
79b4a6fc | 1736 | return (hash_get(bfd_key_hash, bs, hash_alloc_intern) == bs); |
e9e2c950 RZ |
1737 | } |
1738 | ||
e9e2c950 RZ |
1739 | void bfd_initialize(void) |
1740 | { | |
1741 | bfd_id_hash = hash_create(bfd_id_hash_do, bfd_id_hash_cmp, | |
79b4a6fc RZ |
1742 | "BFD session discriminator hash"); |
1743 | bfd_key_hash = hash_create(bfd_key_hash_do, bfd_key_hash_cmp, | |
1744 | "BFD session hash"); | |
ccc9ada8 | 1745 | TAILQ_INIT(&bplist); |
e9e2c950 RZ |
1746 | } |
1747 | ||
e3b78da8 | 1748 | static void _bfd_free(struct hash_bucket *hb, |
e9e2c950 RZ |
1749 | void *arg __attribute__((__unused__))) |
1750 | { | |
1751 | struct bfd_session *bs = hb->data; | |
1752 | ||
1753 | bfd_session_free(bs); | |
1754 | } | |
1755 | ||
e9e2c950 RZ |
1756 | void bfd_shutdown(void) |
1757 | { | |
f6dfa247 RZ |
1758 | struct bfd_profile *bp; |
1759 | ||
e9e2c950 RZ |
1760 | /* |
1761 | * Close and free all BFD sessions. | |
1762 | * | |
1763 | * _bfd_free() will call bfd_session_free() which will take care | |
1764 | * of removing the session from all hashes, so we just run an | |
1765 | * assert() here to make sure it really happened. | |
1766 | */ | |
1767 | bfd_id_iterate(_bfd_free, NULL); | |
79b4a6fc | 1768 | assert(bfd_key_hash->count == 0); |
e9e2c950 | 1769 | |
e9e2c950 RZ |
1770 | /* Now free the hashes themselves. */ |
1771 | hash_free(bfd_id_hash); | |
79b4a6fc | 1772 | hash_free(bfd_key_hash); |
f6dfa247 RZ |
1773 | |
1774 | /* Free all profile allocations. */ | |
1775 | while ((bp = TAILQ_FIRST(&bplist)) != NULL) | |
1776 | bfd_profile_free(bp); | |
e9e2c950 | 1777 | } |
9fc0bc5c | 1778 | |
adc26455 RZ |
1779 | struct bfd_session_iterator { |
1780 | int bsi_stop; | |
1781 | bool bsi_mhop; | |
1782 | const struct bfd_session *bsi_bs; | |
1783 | }; | |
1784 | ||
307fc425 | 1785 | static int _bfd_session_next(struct hash_bucket *hb, void *arg) |
adc26455 RZ |
1786 | { |
1787 | struct bfd_session_iterator *bsi = arg; | |
1788 | struct bfd_session *bs = hb->data; | |
1789 | ||
1790 | /* Previous entry signaled stop. */ | |
1791 | if (bsi->bsi_stop == 1) { | |
1792 | /* Match the single/multi hop sessions. */ | |
1793 | if (bs->key.mhop != bsi->bsi_mhop) | |
1794 | return HASHWALK_CONTINUE; | |
1795 | ||
1796 | bsi->bsi_bs = bs; | |
1797 | return HASHWALK_ABORT; | |
1798 | } | |
1799 | ||
1800 | /* We found the current item, stop in the next one. */ | |
1801 | if (bsi->bsi_bs == hb->data) { | |
1802 | bsi->bsi_stop = 1; | |
1803 | /* Set entry to NULL to signal end of list. */ | |
1804 | bsi->bsi_bs = NULL; | |
1805 | } else if (bsi->bsi_bs == NULL && bsi->bsi_mhop == bs->key.mhop) { | |
1806 | /* We want the first list item. */ | |
1807 | bsi->bsi_stop = 1; | |
1808 | bsi->bsi_bs = hb->data; | |
1809 | return HASHWALK_ABORT; | |
1810 | } | |
1811 | ||
1812 | return HASHWALK_CONTINUE; | |
1813 | } | |
1814 | ||
1815 | /* | |
1816 | * bfd_session_next: uses the current session to find the next. | |
1817 | * | |
1818 | * `bs` might point to NULL to get the first item of the data structure. | |
1819 | */ | |
1820 | const struct bfd_session *bfd_session_next(const struct bfd_session *bs, | |
1821 | bool mhop) | |
1822 | { | |
1823 | struct bfd_session_iterator bsi; | |
1824 | ||
1825 | bsi.bsi_stop = 0; | |
1826 | bsi.bsi_bs = bs; | |
1827 | bsi.bsi_mhop = mhop; | |
1828 | hash_walk(bfd_key_hash, _bfd_session_next, &bsi); | |
1829 | if (bsi.bsi_stop == 0) | |
1830 | return NULL; | |
1831 | ||
1832 | return bsi.bsi_bs; | |
1833 | } | |
1834 | ||
307fc425 RZ |
1835 | static void _bfd_session_remove_manual(struct hash_bucket *hb, |
1836 | void *arg __attribute__((__unused__))) | |
2a573ff6 RZ |
1837 | { |
1838 | struct bfd_session *bs = hb->data; | |
1839 | ||
1840 | /* Delete only manually configured sessions. */ | |
b88113ef | 1841 | if (CHECK_FLAG(bs->flags, BFD_SESS_FLAG_CONFIG) == 0) |
2a573ff6 RZ |
1842 | return; |
1843 | ||
1844 | bs->refcount--; | |
b88113ef | 1845 | UNSET_FLAG(bs->flags, BFD_SESS_FLAG_CONFIG); |
2a573ff6 RZ |
1846 | |
1847 | /* Don't delete sessions still in use. */ | |
1848 | if (bs->refcount != 0) | |
1849 | return; | |
1850 | ||
1851 | bfd_session_free(bs); | |
1852 | } | |
1853 | ||
1854 | /* | |
1855 | * bfd_sessions_remove_manual: remove all manually configured sessions. | |
1856 | * | |
1857 | * NOTE: this function doesn't remove automatically created sessions. | |
1858 | */ | |
1859 | void bfd_sessions_remove_manual(void) | |
1860 | { | |
1861 | hash_iterate(bfd_key_hash, _bfd_session_remove_manual, NULL); | |
1862 | } | |
1863 | ||
e93c3c00 IR |
1864 | void bfd_profiles_remove(void) |
1865 | { | |
1866 | struct bfd_profile *bp; | |
1867 | ||
1868 | while ((bp = TAILQ_FIRST(&bplist)) != NULL) | |
1869 | bfd_profile_free(bp); | |
1870 | } | |
1871 | ||
ccc9ada8 RZ |
1872 | /* |
1873 | * Profile related hash functions. | |
1874 | */ | |
1875 | static void _bfd_profile_update(struct hash_bucket *hb, void *arg) | |
1876 | { | |
1877 | struct bfd_profile *bp = arg; | |
1878 | struct bfd_session *bs = hb->data; | |
1879 | ||
1880 | /* This session is not using the profile. */ | |
1881 | if (bs->profile_name == NULL || strcmp(bs->profile_name, bp->name) != 0) | |
1882 | return; | |
1883 | ||
1884 | bfd_profile_apply(bp->name, bs); | |
1885 | } | |
1886 | ||
1887 | void bfd_profile_update(struct bfd_profile *bp) | |
1888 | { | |
1889 | hash_iterate(bfd_key_hash, _bfd_profile_update, bp); | |
1890 | } | |
1891 | ||
f6dfa247 RZ |
1892 | static void _bfd_profile_detach(struct hash_bucket *hb, void *arg) |
1893 | { | |
1894 | struct bfd_profile *bp = arg; | |
1895 | struct bfd_session *bs = hb->data; | |
1896 | ||
1897 | /* This session is not using the profile. */ | |
1898 | if (bs->profile_name == NULL || strcmp(bs->profile_name, bp->name) != 0) | |
1899 | return; | |
1900 | ||
1901 | bfd_profile_remove(bs); | |
1902 | } | |
1903 | ||
1904 | static void bfd_profile_detach(struct bfd_profile *bp) | |
1905 | { | |
1906 | hash_iterate(bfd_key_hash, _bfd_profile_detach, bp); | |
1907 | } | |
1908 | ||
014cab13 RZ |
1909 | /* |
1910 | * VRF related functions. | |
1911 | */ | |
9fc0bc5c PG |
1912 | static int bfd_vrf_new(struct vrf *vrf) |
1913 | { | |
48da2c31 RZ |
1914 | if (bglobal.debug_zebra) |
1915 | zlog_debug("VRF Created: %s(%u)", vrf->name, vrf->vrf_id); | |
1916 | ||
9fc0bc5c PG |
1917 | return 0; |
1918 | } | |
1919 | ||
1920 | static int bfd_vrf_delete(struct vrf *vrf) | |
1921 | { | |
48da2c31 RZ |
1922 | if (bglobal.debug_zebra) |
1923 | zlog_debug("VRF Deletion: %s(%u)", vrf->name, vrf->vrf_id); | |
1924 | ||
9fc0bc5c PG |
1925 | return 0; |
1926 | } | |
1927 | ||
1928 | static int bfd_vrf_enable(struct vrf *vrf) | |
1929 | { | |
7bcadbae PG |
1930 | struct bfd_vrf_global *bvrf; |
1931 | ||
1932 | /* a different name */ | |
1933 | if (!vrf->info) { | |
1934 | bvrf = XCALLOC(MTYPE_BFDD_VRF, sizeof(struct bfd_vrf_global)); | |
1935 | bvrf->vrf = vrf; | |
1936 | vrf->info = (void *)bvrf; | |
29397ec1 RZ |
1937 | |
1938 | /* Disable sockets if using data plane. */ | |
1939 | if (bglobal.bg_use_dplane) { | |
1940 | bvrf->bg_shop = -1; | |
1941 | bvrf->bg_mhop = -1; | |
1942 | bvrf->bg_shop6 = -1; | |
1943 | bvrf->bg_mhop6 = -1; | |
1944 | bvrf->bg_echo = -1; | |
1945 | bvrf->bg_echov6 = -1; | |
1946 | } | |
7bcadbae PG |
1947 | } else |
1948 | bvrf = vrf->info; | |
48da2c31 RZ |
1949 | |
1950 | if (bglobal.debug_zebra) | |
1951 | zlog_debug("VRF enable add %s id %u", vrf->name, vrf->vrf_id); | |
1952 | ||
30c5d21e PG |
1953 | if (vrf->vrf_id == VRF_DEFAULT || |
1954 | vrf_get_backend() == VRF_BACKEND_NETNS) { | |
1955 | if (!bvrf->bg_shop) | |
4a9feb66 | 1956 | bvrf->bg_shop = bp_udp_shop(vrf); |
30c5d21e | 1957 | if (!bvrf->bg_mhop) |
4a9feb66 | 1958 | bvrf->bg_mhop = bp_udp_mhop(vrf); |
30c5d21e | 1959 | if (!bvrf->bg_shop6) |
4a9feb66 | 1960 | bvrf->bg_shop6 = bp_udp6_shop(vrf); |
30c5d21e | 1961 | if (!bvrf->bg_mhop6) |
4a9feb66 | 1962 | bvrf->bg_mhop6 = bp_udp6_mhop(vrf); |
30c5d21e | 1963 | if (!bvrf->bg_echo) |
4a9feb66 | 1964 | bvrf->bg_echo = bp_echo_socket(vrf); |
30c5d21e | 1965 | if (!bvrf->bg_echov6) |
4a9feb66 | 1966 | bvrf->bg_echov6 = bp_echov6_socket(vrf); |
30c5d21e | 1967 | |
29397ec1 RZ |
1968 | if (!bvrf->bg_ev[0] && bvrf->bg_shop != -1) |
1969 | thread_add_read(master, bfd_recv_cb, bvrf, | |
1970 | bvrf->bg_shop, &bvrf->bg_ev[0]); | |
1971 | if (!bvrf->bg_ev[1] && bvrf->bg_mhop != -1) | |
1972 | thread_add_read(master, bfd_recv_cb, bvrf, | |
1973 | bvrf->bg_mhop, &bvrf->bg_ev[1]); | |
f1446f98 | 1974 | if (!bvrf->bg_ev[2] && bvrf->bg_shop6 != -1) |
29397ec1 RZ |
1975 | thread_add_read(master, bfd_recv_cb, bvrf, |
1976 | bvrf->bg_shop6, &bvrf->bg_ev[2]); | |
f1446f98 | 1977 | if (!bvrf->bg_ev[3] && bvrf->bg_mhop6 != -1) |
29397ec1 RZ |
1978 | thread_add_read(master, bfd_recv_cb, bvrf, |
1979 | bvrf->bg_mhop6, &bvrf->bg_ev[3]); | |
1980 | if (!bvrf->bg_ev[4] && bvrf->bg_echo != -1) | |
1981 | thread_add_read(master, bfd_recv_cb, bvrf, | |
1982 | bvrf->bg_echo, &bvrf->bg_ev[4]); | |
f1446f98 | 1983 | if (!bvrf->bg_ev[5] && bvrf->bg_echov6 != -1) |
29397ec1 RZ |
1984 | thread_add_read(master, bfd_recv_cb, bvrf, |
1985 | bvrf->bg_echov6, &bvrf->bg_ev[5]); | |
30c5d21e | 1986 | } |
d24af713 | 1987 | if (vrf->vrf_id != VRF_DEFAULT) { |
54aadda1 | 1988 | bfdd_zclient_register(vrf->vrf_id); |
d24af713 PG |
1989 | bfdd_sessions_enable_vrf(vrf); |
1990 | } | |
9fc0bc5c PG |
1991 | return 0; |
1992 | } | |
1993 | ||
1994 | static int bfd_vrf_disable(struct vrf *vrf) | |
1995 | { | |
7bcadbae PG |
1996 | struct bfd_vrf_global *bvrf; |
1997 | ||
1998 | if (!vrf->info) | |
9fc0bc5c | 1999 | return 0; |
7bcadbae | 2000 | bvrf = vrf->info; |
54aadda1 | 2001 | |
d24af713 PG |
2002 | if (vrf->vrf_id != VRF_DEFAULT) { |
2003 | bfdd_sessions_disable_vrf(vrf); | |
54aadda1 | 2004 | bfdd_zclient_unregister(vrf->vrf_id); |
d24af713 | 2005 | } |
54aadda1 | 2006 | |
48da2c31 RZ |
2007 | if (bglobal.debug_zebra) |
2008 | zlog_debug("VRF disable %s id %d", vrf->name, vrf->vrf_id); | |
507d75d4 RZ |
2009 | |
2010 | /* Disable read/write poll triggering. */ | |
2011 | THREAD_OFF(bvrf->bg_ev[0]); | |
2012 | THREAD_OFF(bvrf->bg_ev[1]); | |
2013 | THREAD_OFF(bvrf->bg_ev[2]); | |
2014 | THREAD_OFF(bvrf->bg_ev[3]); | |
2015 | THREAD_OFF(bvrf->bg_ev[4]); | |
2016 | THREAD_OFF(bvrf->bg_ev[5]); | |
2017 | ||
7bcadbae PG |
2018 | /* Close all descriptors. */ |
2019 | socket_close(&bvrf->bg_echo); | |
2020 | socket_close(&bvrf->bg_shop); | |
2021 | socket_close(&bvrf->bg_mhop); | |
f1446f98 DS |
2022 | if (bvrf->bg_shop6 != -1) |
2023 | socket_close(&bvrf->bg_shop6); | |
2024 | if (bvrf->bg_mhop6 != -1) | |
2025 | socket_close(&bvrf->bg_mhop6); | |
8cf3c217 | 2026 | socket_close(&bvrf->bg_echo); |
f1446f98 DS |
2027 | if (bvrf->bg_echov6 != -1) |
2028 | socket_close(&bvrf->bg_echov6); | |
7bcadbae PG |
2029 | |
2030 | /* free context */ | |
2031 | XFREE(MTYPE_BFDD_VRF, bvrf); | |
2032 | vrf->info = NULL; | |
2033 | ||
9fc0bc5c PG |
2034 | return 0; |
2035 | } | |
2036 | ||
2037 | void bfd_vrf_init(void) | |
2038 | { | |
ac2cb9bf | 2039 | vrf_init(bfd_vrf_new, bfd_vrf_enable, bfd_vrf_disable, bfd_vrf_delete); |
9fc0bc5c PG |
2040 | } |
2041 | ||
7bcadbae PG |
2042 | void bfd_vrf_terminate(void) |
2043 | { | |
2044 | vrf_terminate(); | |
2045 | } | |
2046 | ||
2047 | struct bfd_vrf_global *bfd_vrf_look_by_session(struct bfd_session *bfd) | |
2048 | { | |
2049 | struct vrf *vrf; | |
2050 | ||
2051 | if (!vrf_is_backend_netns()) { | |
2052 | vrf = vrf_lookup_by_id(VRF_DEFAULT); | |
2053 | if (vrf) | |
2054 | return (struct bfd_vrf_global *)vrf->info; | |
2055 | return NULL; | |
2056 | } | |
2057 | if (!bfd) | |
2058 | return NULL; | |
2059 | if (!bfd->vrf) | |
2060 | return NULL; | |
2061 | return bfd->vrf->info; | |
2062 | } | |
f06e248c | 2063 | |
fa6e709f S |
2064 | unsigned long bfd_get_session_count(void) |
2065 | { | |
2066 | return bfd_key_hash->count; | |
2067 | } |