]>
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 RZ |
30 | #include "lib/jhash.h" |
31 | ||
32 | #include "bfd.h" | |
33 | ||
34 | DEFINE_QOBJ_TYPE(bfd_session); | |
35 | ||
36 | /* | |
37 | * Prototypes | |
38 | */ | |
39 | static uint32_t ptm_bfd_gen_ID(void); | |
40 | static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd); | |
41 | static void bfd_session_free(struct bfd_session *bs); | |
42 | static struct bfd_session *bfd_session_new(int sd); | |
43 | static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa, | |
44 | uint32_t ldisc); | |
45 | static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc); | |
46 | static const char *get_diag_str(int diag); | |
47 | ||
48 | ||
49 | /* | |
50 | * Functions | |
51 | */ | |
52 | struct bfd_session *bs_peer_find(struct bfd_peer_cfg *bpc) | |
53 | { | |
54 | struct bfd_session *bs; | |
55 | struct peer_label *pl; | |
56 | struct bfd_mhop_key mhop; | |
57 | struct bfd_shop_key shop; | |
58 | ||
59 | /* Try to find label first. */ | |
60 | if (bpc->bpc_has_label) { | |
61 | pl = pl_find(bpc->bpc_label); | |
62 | if (pl != NULL) { | |
63 | bs = pl->pl_bs; | |
64 | return bs; | |
65 | } | |
66 | } | |
67 | ||
68 | /* Otherwise fallback to peer/local hash lookup. */ | |
69 | if (bpc->bpc_mhop) { | |
70 | memset(&mhop, 0, sizeof(mhop)); | |
71 | mhop.peer = bpc->bpc_peer; | |
72 | mhop.local = bpc->bpc_local; | |
73 | if (bpc->bpc_has_vrfname) | |
74 | strlcpy(mhop.vrf_name, bpc->bpc_vrfname, | |
75 | sizeof(mhop.vrf_name)); | |
76 | ||
77 | bs = bfd_mhop_lookup(mhop); | |
78 | } else { | |
79 | memset(&shop, 0, sizeof(shop)); | |
80 | shop.peer = bpc->bpc_peer; | |
8a9f760e | 81 | if (bpc->bpc_has_localif) |
e9e2c950 RZ |
82 | strlcpy(shop.port_name, bpc->bpc_localif, |
83 | sizeof(shop.port_name)); | |
84 | ||
85 | bs = bfd_shop_lookup(shop); | |
86 | } | |
87 | ||
88 | return bs; | |
89 | } | |
90 | ||
91 | static uint32_t ptm_bfd_gen_ID(void) | |
92 | { | |
93 | static uint32_t sessionID = 1; | |
94 | ||
95 | return (sessionID++); | |
96 | } | |
97 | ||
98 | void ptm_bfd_start_xmt_timer(struct bfd_session *bfd, bool is_echo) | |
99 | { | |
100 | uint64_t jitter, xmt_TO; | |
101 | int maxpercent; | |
102 | ||
103 | xmt_TO = is_echo ? bfd->echo_xmt_TO : bfd->xmt_TO; | |
104 | ||
105 | /* | |
106 | * From section 6.5.2: trasmit interval should be randomly jittered | |
107 | * between | |
108 | * 75% and 100% of nominal value, unless detect_mult is 1, then should | |
109 | * be | |
110 | * between 75% and 90%. | |
111 | */ | |
112 | maxpercent = (bfd->detect_mult == 1) ? 16 : 26; | |
113 | jitter = (xmt_TO * (75 + (random() % maxpercent))) / 100; | |
114 | /* XXX remove that division above */ | |
115 | ||
116 | if (is_echo) | |
117 | bfd_echo_xmttimer_update(bfd, jitter); | |
118 | else | |
119 | bfd_xmttimer_update(bfd, jitter); | |
120 | } | |
121 | ||
122 | static void ptm_bfd_echo_xmt_TO(struct bfd_session *bfd) | |
123 | { | |
124 | /* Send the scheduled echo packet */ | |
125 | ptm_bfd_echo_snd(bfd); | |
126 | ||
127 | /* Restart the timer for next time */ | |
128 | ptm_bfd_start_xmt_timer(bfd, true); | |
129 | } | |
130 | ||
131 | void ptm_bfd_xmt_TO(struct bfd_session *bfd, int fbit) | |
132 | { | |
133 | /* Send the scheduled control packet */ | |
134 | ptm_bfd_snd(bfd, fbit); | |
135 | ||
136 | /* Restart the timer for next time */ | |
137 | ptm_bfd_start_xmt_timer(bfd, false); | |
138 | } | |
139 | ||
8bd859f6 | 140 | void ptm_bfd_echo_stop(struct bfd_session *bfd) |
e9e2c950 RZ |
141 | { |
142 | bfd->echo_xmt_TO = 0; | |
143 | bfd->echo_detect_TO = 0; | |
144 | BFD_UNSET_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE); | |
145 | ||
146 | bfd_echo_xmttimer_delete(bfd); | |
147 | bfd_echo_recvtimer_delete(bfd); | |
e9e2c950 RZ |
148 | } |
149 | ||
150 | void ptm_bfd_echo_start(struct bfd_session *bfd) | |
151 | { | |
152 | bfd->echo_detect_TO = (bfd->remote_detect_mult * bfd->echo_xmt_TO); | |
451eb5a2 RZ |
153 | if (bfd->echo_detect_TO > 0) |
154 | ptm_bfd_echo_xmt_TO(bfd); | |
e9e2c950 RZ |
155 | } |
156 | ||
157 | void ptm_bfd_ses_up(struct bfd_session *bfd) | |
158 | { | |
03e7f088 RZ |
159 | int old_state = bfd->ses_state; |
160 | ||
e9e2c950 RZ |
161 | bfd->local_diag = 0; |
162 | bfd->ses_state = PTM_BFD_UP; | |
e9e2c950 RZ |
163 | monotime(&bfd->uptime); |
164 | ||
c0ef9a8a RZ |
165 | /* Connection is up, lets negotiate timers. */ |
166 | bfd_set_polling(bfd); | |
167 | ||
168 | /* Start sending control packets with poll bit immediately. */ | |
169 | ptm_bfd_snd(bfd, 0); | |
e9e2c950 RZ |
170 | |
171 | control_notify(bfd); | |
172 | ||
0684c9b1 RZ |
173 | if (old_state != bfd->ses_state) { |
174 | bfd->stats.session_up++; | |
03e7f088 RZ |
175 | log_info("state-change: [%s] %s -> %s", bs_to_string(bfd), |
176 | state_list[old_state].str, | |
177 | state_list[bfd->ses_state].str); | |
0684c9b1 | 178 | } |
e9e2c950 RZ |
179 | } |
180 | ||
181 | void ptm_bfd_ses_dn(struct bfd_session *bfd, uint8_t diag) | |
182 | { | |
183 | int old_state = bfd->ses_state; | |
184 | ||
185 | bfd->local_diag = diag; | |
186 | bfd->discrs.remote_discr = 0; | |
187 | bfd->ses_state = PTM_BFD_DOWN; | |
188 | bfd->polling = 0; | |
189 | bfd->demand_mode = 0; | |
190 | monotime(&bfd->downtime); | |
191 | ||
192 | ptm_bfd_snd(bfd, 0); | |
193 | ||
194 | /* only signal clients when going from up->down state */ | |
195 | if (old_state == PTM_BFD_UP) | |
196 | control_notify(bfd); | |
197 | ||
e9e2c950 RZ |
198 | /* Stop echo packet transmission if they are active */ |
199 | if (BFD_CHECK_FLAG(bfd->flags, BFD_SESS_FLAG_ECHO_ACTIVE)) | |
8bd859f6 | 200 | ptm_bfd_echo_stop(bfd); |
03e7f088 | 201 | |
0684c9b1 RZ |
202 | if (old_state != bfd->ses_state) { |
203 | bfd->stats.session_down++; | |
03e7f088 RZ |
204 | log_info("state-change: [%s] %s -> %s reason:%s", |
205 | bs_to_string(bfd), state_list[old_state].str, | |
206 | state_list[bfd->ses_state].str, | |
207 | get_diag_str(bfd->local_diag)); | |
0684c9b1 | 208 | } |
e9e2c950 RZ |
209 | } |
210 | ||
211 | static int ptm_bfd_get_vrf_name(char *port_name, char *vrf_name) | |
212 | { | |
213 | struct bfd_iface *iface; | |
214 | struct bfd_vrf *vrf; | |
215 | ||
216 | if ((port_name == NULL) || (vrf_name == NULL)) | |
217 | return -1; | |
218 | ||
219 | iface = bfd_iface_lookup(port_name); | |
220 | if (iface) { | |
221 | vrf = bfd_vrf_lookup(iface->vrf_id); | |
222 | if (vrf) { | |
223 | strlcpy(vrf_name, vrf->name, sizeof(vrf->name)); | |
224 | return 0; | |
225 | } | |
226 | } | |
227 | return -1; | |
228 | } | |
229 | ||
230 | static struct bfd_session *bfd_find_disc(struct sockaddr_any *sa, | |
231 | uint32_t ldisc) | |
232 | { | |
233 | struct bfd_session *bs; | |
234 | ||
235 | bs = bfd_id_lookup(ldisc); | |
236 | if (bs == NULL) | |
237 | return NULL; | |
238 | ||
239 | /* Remove unused fields. */ | |
240 | switch (sa->sa_sin.sin_family) { | |
241 | case AF_INET: | |
242 | sa->sa_sin.sin_port = 0; | |
243 | if (memcmp(sa, &bs->shop.peer, sizeof(sa->sa_sin)) == 0) | |
244 | return bs; | |
245 | break; | |
246 | case AF_INET6: | |
247 | sa->sa_sin6.sin6_port = 0; | |
248 | if (memcmp(sa, &bs->shop.peer, sizeof(sa->sa_sin6)) == 0) | |
249 | return bs; | |
250 | break; | |
251 | } | |
252 | ||
253 | return NULL; | |
254 | } | |
255 | ||
256 | struct bfd_session *ptm_bfd_sess_find(struct bfd_pkt *cp, char *port_name, | |
257 | struct sockaddr_any *peer, | |
258 | struct sockaddr_any *local, | |
259 | char *vrf_name, bool is_mhop) | |
260 | { | |
261 | struct bfd_session *l_bfd = NULL; | |
262 | struct bfd_mhop_key mhop; | |
263 | struct bfd_shop_key shop; | |
9d63adda | 264 | char vrf_buf[MAXNAMELEN]; |
e9e2c950 | 265 | |
9d63adda RZ |
266 | /* Find our session using the ID signaled by the remote end. */ |
267 | if (cp->discrs.remote_discr) | |
268 | return bfd_find_disc(peer, ntohl(cp->discrs.remote_discr)); | |
269 | ||
270 | /* Search for session without using discriminator. */ | |
271 | if (is_mhop) { | |
272 | memset(&mhop, 0, sizeof(mhop)); | |
273 | mhop.peer = *peer; | |
274 | mhop.local = *local; | |
275 | if (vrf_name && vrf_name[0]) { | |
276 | strlcpy(mhop.vrf_name, vrf_name, sizeof(mhop.vrf_name)); | |
277 | } else if (port_name && port_name[0]) { | |
278 | memset(vrf_buf, 0, sizeof(vrf_buf)); | |
279 | if (ptm_bfd_get_vrf_name(port_name, vrf_buf) != -1) | |
03e7f088 RZ |
280 | strlcpy(mhop.vrf_name, vrf_buf, |
281 | sizeof(mhop.vrf_name)); | |
e9e2c950 RZ |
282 | } |
283 | ||
9d63adda RZ |
284 | l_bfd = bfd_mhop_lookup(mhop); |
285 | } else { | |
286 | memset(&shop, 0, sizeof(shop)); | |
287 | shop.peer = *peer; | |
288 | if (port_name && port_name[0]) | |
03e7f088 RZ |
289 | strlcpy(shop.port_name, port_name, |
290 | sizeof(shop.port_name)); | |
9d63adda RZ |
291 | |
292 | l_bfd = bfd_shop_lookup(shop); | |
e9e2c950 RZ |
293 | } |
294 | ||
9d63adda RZ |
295 | /* XXX maybe remoteDiscr should be checked for remoteHeard cases. */ |
296 | return l_bfd; | |
e9e2c950 RZ |
297 | } |
298 | ||
e9e2c950 RZ |
299 | int bfd_xmt_cb(struct thread *t) |
300 | { | |
301 | struct bfd_session *bs = THREAD_ARG(t); | |
302 | ||
303 | ptm_bfd_xmt_TO(bs, 0); | |
304 | ||
305 | return 0; | |
306 | } | |
307 | ||
308 | int bfd_echo_xmt_cb(struct thread *t) | |
309 | { | |
310 | struct bfd_session *bs = THREAD_ARG(t); | |
311 | ||
451eb5a2 RZ |
312 | if (bs->echo_xmt_TO > 0) |
313 | ptm_bfd_echo_xmt_TO(bs); | |
e9e2c950 RZ |
314 | |
315 | return 0; | |
316 | } | |
317 | ||
318 | /* Was ptm_bfd_detect_TO() */ | |
319 | int bfd_recvtimer_cb(struct thread *t) | |
320 | { | |
321 | struct bfd_session *bs = THREAD_ARG(t); | |
e9e2c950 RZ |
322 | |
323 | switch (bs->ses_state) { | |
324 | case PTM_BFD_INIT: | |
325 | case PTM_BFD_UP: | |
40675ea9 | 326 | ptm_bfd_ses_dn(bs, BD_CONTROL_EXPIRED); |
e9e2c950 RZ |
327 | bfd_recvtimer_update(bs); |
328 | break; | |
329 | ||
330 | default: | |
331 | /* Second detect time expiration, zero remote discr (section | |
332 | * 6.5.1) | |
333 | */ | |
334 | bs->discrs.remote_discr = 0; | |
335 | break; | |
336 | } | |
337 | ||
e9e2c950 RZ |
338 | return 0; |
339 | } | |
340 | ||
341 | /* Was ptm_bfd_echo_detect_TO() */ | |
342 | int bfd_echo_recvtimer_cb(struct thread *t) | |
343 | { | |
344 | struct bfd_session *bs = THREAD_ARG(t); | |
e9e2c950 RZ |
345 | |
346 | switch (bs->ses_state) { | |
347 | case PTM_BFD_INIT: | |
348 | case PTM_BFD_UP: | |
40675ea9 | 349 | ptm_bfd_ses_dn(bs, BD_ECHO_FAILED); |
e9e2c950 RZ |
350 | break; |
351 | } | |
352 | ||
e9e2c950 RZ |
353 | return 0; |
354 | } | |
355 | ||
356 | static struct bfd_session *bfd_session_new(int sd) | |
357 | { | |
358 | struct bfd_session *bs; | |
359 | ||
360 | bs = XCALLOC(MTYPE_BFDD_CONFIG, sizeof(*bs)); | |
361 | if (bs == NULL) | |
362 | return NULL; | |
363 | ||
364 | QOBJ_REG(bs, bfd_session); | |
365 | ||
f43b9368 | 366 | bs->timers.desired_min_tx = BFD_DEFDESIREDMINTX; |
e9e2c950 RZ |
367 | bs->timers.required_min_rx = BFD_DEFREQUIREDMINRX; |
368 | bs->timers.required_min_echo = BFD_DEF_REQ_MIN_ECHO; | |
369 | bs->detect_mult = BFD_DEFDETECTMULT; | |
370 | bs->mh_ttl = BFD_DEF_MHOP_TTL; | |
371 | ||
f43b9368 RZ |
372 | /* |
373 | * BFD connection startup must use slow timer. | |
374 | * | |
375 | * RFC 5880, Section 6.8.3. | |
376 | */ | |
377 | bs->cur_timers.desired_min_tx = BFD_DEF_SLOWTX; | |
378 | bs->cur_timers.required_min_rx = BFD_DEF_SLOWTX; | |
379 | bs->cur_timers.required_min_echo = 0; | |
380 | ||
381 | /* Set the appropriated timeouts for slow connection. */ | |
382 | bs->detect_TO = (BFD_DEFDETECTMULT * BFD_DEF_SLOWTX); | |
383 | bs->xmt_TO = BFD_DEF_SLOWTX; | |
384 | ||
385 | /* Initiate remote settings as well. */ | |
386 | bs->remote_timers = bs->cur_timers; | |
387 | bs->remote_detect_mult = BFD_DEFDETECTMULT; | |
388 | ||
e9e2c950 RZ |
389 | bs->sock = sd; |
390 | monotime(&bs->uptime); | |
391 | bs->downtime = bs->uptime; | |
392 | ||
393 | return bs; | |
394 | } | |
395 | ||
396 | int bfd_session_update_label(struct bfd_session *bs, const char *nlabel) | |
397 | { | |
398 | /* New label treatment: | |
399 | * - Check if the label is taken; | |
400 | * - Try to allocate the memory for it and register; | |
401 | */ | |
402 | if (bs->pl == NULL) { | |
403 | if (pl_find(nlabel) != NULL) { | |
404 | /* Someone is already using it. */ | |
405 | return -1; | |
406 | } | |
407 | ||
408 | if (pl_new(nlabel, bs) == NULL) | |
409 | return -1; | |
410 | ||
411 | return 0; | |
412 | } | |
413 | ||
414 | /* | |
415 | * Test label change consistency: | |
416 | * - Do nothing if it's the same label; | |
417 | * - Check if the future label is already taken; | |
418 | * - Change label; | |
419 | */ | |
420 | if (strcmp(nlabel, bs->pl->pl_label) == 0) | |
421 | return -1; | |
422 | if (pl_find(nlabel) != NULL) | |
423 | return -1; | |
424 | ||
425 | strlcpy(bs->pl->pl_label, nlabel, sizeof(bs->pl->pl_label)); | |
426 | return 0; | |
427 | } | |
428 | ||
429 | static void _bfd_session_update(struct bfd_session *bs, | |
430 | struct bfd_peer_cfg *bpc) | |
431 | { | |
432 | if (bpc->bpc_echo) { | |
433 | /* Check if echo mode is already active. */ | |
434 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO)) | |
435 | goto skip_echo; | |
436 | ||
437 | BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO); | |
e9e2c950 RZ |
438 | |
439 | /* Activate/update echo receive timeout timer. */ | |
73c62f8e | 440 | bs_echo_timer_handler(bs); |
e9e2c950 RZ |
441 | } else { |
442 | /* Check if echo mode is already disabled. */ | |
443 | if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO)) | |
444 | goto skip_echo; | |
445 | ||
446 | BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_ECHO); | |
8bd859f6 | 447 | ptm_bfd_echo_stop(bs); |
e9e2c950 RZ |
448 | } |
449 | ||
450 | skip_echo: | |
451 | if (bpc->bpc_has_txinterval) | |
f43b9368 | 452 | bs->timers.desired_min_tx = bpc->bpc_txinterval * 1000; |
e9e2c950 RZ |
453 | |
454 | if (bpc->bpc_has_recvinterval) | |
455 | bs->timers.required_min_rx = bpc->bpc_recvinterval * 1000; | |
456 | ||
457 | if (bpc->bpc_has_detectmultiplier) | |
458 | bs->detect_mult = bpc->bpc_detectmultiplier; | |
459 | ||
460 | if (bpc->bpc_has_echointerval) | |
461 | bs->timers.required_min_echo = bpc->bpc_echointerval * 1000; | |
462 | ||
463 | if (bpc->bpc_has_label) | |
464 | bfd_session_update_label(bs, bpc->bpc_label); | |
465 | ||
466 | if (bpc->bpc_shutdown) { | |
467 | /* Check if already shutdown. */ | |
468 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN)) | |
469 | return; | |
470 | ||
471 | BFD_SET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN); | |
472 | ||
473 | /* Disable all events. */ | |
474 | bfd_recvtimer_delete(bs); | |
475 | bfd_echo_recvtimer_delete(bs); | |
476 | bfd_xmttimer_delete(bs); | |
477 | bfd_echo_xmttimer_delete(bs); | |
478 | ||
479 | /* Change and notify state change. */ | |
480 | bs->ses_state = PTM_BFD_ADM_DOWN; | |
481 | control_notify(bs); | |
482 | ||
483 | ptm_bfd_snd(bs, 0); | |
484 | } else { | |
485 | /* Check if already working. */ | |
486 | if (!BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN)) | |
487 | return; | |
488 | ||
489 | BFD_UNSET_FLAG(bs->flags, BFD_SESS_FLAG_SHUTDOWN); | |
490 | ||
491 | /* Change and notify state change. */ | |
492 | bs->ses_state = PTM_BFD_DOWN; | |
493 | control_notify(bs); | |
494 | ||
495 | /* Enable all timers. */ | |
496 | bfd_recvtimer_update(bs); | |
497 | bfd_xmttimer_update(bs, bs->xmt_TO); | |
498 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO)) { | |
499 | bfd_echo_recvtimer_update(bs); | |
500 | bfd_echo_xmttimer_update(bs, bs->echo_xmt_TO); | |
501 | } | |
502 | } | |
503 | } | |
504 | ||
505 | static int bfd_session_update(struct bfd_session *bs, struct bfd_peer_cfg *bpc) | |
506 | { | |
507 | /* User didn't want to update, return failure. */ | |
508 | if (bpc->bpc_createonly) | |
509 | return -1; | |
510 | ||
511 | _bfd_session_update(bs, bpc); | |
512 | ||
e9e2c950 RZ |
513 | control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs); |
514 | ||
515 | return 0; | |
516 | } | |
517 | ||
518 | static void bfd_session_free(struct bfd_session *bs) | |
519 | { | |
520 | if (bs->sock != -1) | |
521 | close(bs->sock); | |
522 | ||
523 | bfd_recvtimer_delete(bs); | |
524 | bfd_echo_recvtimer_delete(bs); | |
525 | bfd_xmttimer_delete(bs); | |
526 | bfd_echo_xmttimer_delete(bs); | |
527 | ||
528 | bfd_id_delete(bs->discrs.my_discr); | |
529 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)) | |
530 | bfd_mhop_delete(bs->mhop); | |
531 | else | |
532 | bfd_shop_delete(bs->shop); | |
533 | ||
534 | pl_free(bs->pl); | |
535 | ||
536 | QOBJ_UNREG(bs); | |
537 | XFREE(MTYPE_BFDD_CONFIG, bs); | |
538 | } | |
539 | ||
540 | struct bfd_session *ptm_bfd_sess_new(struct bfd_peer_cfg *bpc) | |
541 | { | |
542 | struct bfd_session *bfd, *l_bfd; | |
80edb675 | 543 | struct interface *ifp = NULL; |
e9e2c950 RZ |
544 | int psock; |
545 | ||
546 | /* check to see if this needs a new session */ | |
547 | l_bfd = bs_peer_find(bpc); | |
548 | if (l_bfd) { | |
549 | /* Requesting a duplicated peer means update configuration. */ | |
550 | if (bfd_session_update(l_bfd, bpc) == 0) | |
551 | return l_bfd; | |
552 | else | |
553 | return NULL; | |
554 | } | |
555 | ||
80edb675 RZ |
556 | /* |
557 | * No session found, we have to allocate a new one. | |
558 | * | |
559 | * First a few critical checks: | |
560 | * | |
561 | * * Check that the specified interface exists. | |
562 | * * Attempt to create the UDP socket (might fail if we exceed | |
563 | * our limits). | |
564 | */ | |
565 | if (bpc->bpc_has_localif) { | |
566 | ifp = if_lookup_by_name(bpc->bpc_localif, VRF_DEFAULT); | |
567 | if (ifp == NULL) { | |
568 | log_error( | |
569 | "session-new: specified interface doesn't exists."); | |
570 | return NULL; | |
571 | } | |
572 | } | |
573 | ||
e9e2c950 RZ |
574 | /* |
575 | * Get socket for transmitting control packets. Note that if we | |
576 | * could use the destination port (3784) for the source | |
577 | * port we wouldn't need a socket per session. | |
578 | */ | |
579 | if (bpc->bpc_ipv4) { | |
580 | psock = bp_peer_socket(bpc); | |
03e7f088 | 581 | if (psock == -1) |
e9e2c950 | 582 | return NULL; |
e9e2c950 RZ |
583 | } else { |
584 | psock = bp_peer_socketv6(bpc); | |
03e7f088 | 585 | if (psock == -1) |
e9e2c950 | 586 | return NULL; |
e9e2c950 RZ |
587 | } |
588 | ||
589 | /* Get memory */ | |
590 | bfd = bfd_session_new(psock); | |
591 | if (bfd == NULL) { | |
03e7f088 | 592 | log_error("session-new: allocation failed"); |
e9e2c950 RZ |
593 | return NULL; |
594 | } | |
595 | ||
80edb675 RZ |
596 | if (bpc->bpc_has_localif && !bpc->bpc_mhop) |
597 | bfd->ifp = ifp; | |
e9e2c950 | 598 | |
43adc702 | 599 | if (bpc->bpc_ipv4 == false) { |
e9e2c950 RZ |
600 | BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_IPV6); |
601 | ||
43adc702 RZ |
602 | /* Set the IPv6 scope id for link-local addresses. */ |
603 | if (IN6_IS_ADDR_LINKLOCAL(&bpc->bpc_local.sa_sin6.sin6_addr)) | |
80edb675 RZ |
604 | bpc->bpc_local.sa_sin6.sin6_scope_id = |
605 | bfd->ifp != NULL ? bfd->ifp->ifindex | |
606 | : IFINDEX_INTERNAL; | |
43adc702 | 607 | if (IN6_IS_ADDR_LINKLOCAL(&bpc->bpc_peer.sa_sin6.sin6_addr)) |
80edb675 RZ |
608 | bpc->bpc_peer.sa_sin6.sin6_scope_id = |
609 | bfd->ifp != NULL ? bfd->ifp->ifindex | |
610 | : IFINDEX_INTERNAL; | |
43adc702 RZ |
611 | } |
612 | ||
e9e2c950 RZ |
613 | /* Initialize the session */ |
614 | bfd->ses_state = PTM_BFD_DOWN; | |
615 | bfd->discrs.my_discr = ptm_bfd_gen_ID(); | |
616 | bfd->discrs.remote_discr = 0; | |
617 | bfd->local_ip = bpc->bpc_local; | |
618 | bfd->local_address = bpc->bpc_local; | |
e9e2c950 | 619 | bfd_recvtimer_update(bfd); |
f43b9368 | 620 | ptm_bfd_start_xmt_timer(bfd, false); |
e9e2c950 | 621 | |
f43b9368 | 622 | /* Registrate session into data structures. */ |
e9e2c950 RZ |
623 | bfd_id_insert(bfd); |
624 | ||
625 | if (bpc->bpc_mhop) { | |
626 | BFD_SET_FLAG(bfd->flags, BFD_SESS_FLAG_MH); | |
627 | bfd->mhop.peer = bpc->bpc_peer; | |
628 | bfd->mhop.local = bpc->bpc_local; | |
629 | if (bpc->bpc_has_vrfname) | |
630 | strlcpy(bfd->mhop.vrf_name, bpc->bpc_vrfname, | |
631 | sizeof(bfd->mhop.vrf_name)); | |
632 | ||
633 | bfd_mhop_insert(bfd); | |
634 | } else { | |
635 | bfd->shop.peer = bpc->bpc_peer; | |
8a9f760e | 636 | if (bpc->bpc_has_localif) |
e9e2c950 RZ |
637 | strlcpy(bfd->shop.port_name, bpc->bpc_localif, |
638 | sizeof(bfd->shop.port_name)); | |
639 | ||
640 | bfd_shop_insert(bfd); | |
641 | } | |
642 | ||
e9e2c950 RZ |
643 | _bfd_session_update(bfd, bpc); |
644 | ||
03e7f088 | 645 | log_info("session-new: %s", bs_to_string(bfd)); |
e9e2c950 RZ |
646 | |
647 | control_notify_config(BCM_NOTIFY_CONFIG_ADD, bfd); | |
648 | ||
649 | return bfd; | |
650 | } | |
651 | ||
652 | int ptm_bfd_ses_del(struct bfd_peer_cfg *bpc) | |
653 | { | |
654 | struct bfd_session *bs; | |
655 | ||
656 | /* Find session and call free(). */ | |
657 | bs = bs_peer_find(bpc); | |
658 | if (bs == NULL) | |
659 | return -1; | |
660 | ||
661 | /* This pointer is being referenced, don't let it be deleted. */ | |
662 | if (bs->refcount > 0) { | |
03e7f088 RZ |
663 | log_error("session-delete: refcount failure: %" PRIu64 |
664 | " references", | |
665 | bs->refcount); | |
e9e2c950 RZ |
666 | return -1; |
667 | } | |
668 | ||
03e7f088 | 669 | log_info("session-delete: %s", bs_to_string(bs)); |
e9e2c950 RZ |
670 | |
671 | control_notify_config(BCM_NOTIFY_CONFIG_DELETE, bs); | |
672 | ||
673 | bfd_session_free(bs); | |
674 | ||
675 | return 0; | |
676 | } | |
677 | ||
d3f3a2c4 RZ |
678 | void bfd_set_polling(struct bfd_session *bs) |
679 | { | |
c0ef9a8a RZ |
680 | /* |
681 | * Start polling procedure: the only timers that require polling | |
682 | * to change value without losing connection are: | |
683 | * | |
684 | * - Desired minimum transmission interval; | |
685 | * - Required minimum receive interval; | |
686 | * | |
687 | * RFC 5880, Section 6.8.3. | |
688 | */ | |
d3f3a2c4 RZ |
689 | bs->polling = 1; |
690 | } | |
691 | ||
aef131af RZ |
692 | /* |
693 | * bs_<state>_handler() functions implement the BFD state machine | |
694 | * transition mechanism. `<state>` is the current session state and | |
695 | * the parameter `nstate` is the peer new state. | |
696 | */ | |
697 | void bs_admin_down_handler(struct bfd_session *bs, int nstate); | |
698 | void bs_down_handler(struct bfd_session *bs, int nstate); | |
699 | void bs_init_handler(struct bfd_session *bs, int nstate); | |
700 | void bs_up_handler(struct bfd_session *bs, int nstate); | |
701 | ||
702 | void bs_admin_down_handler(struct bfd_session *bs __attribute__((__unused__)), | |
703 | int nstate __attribute__((__unused__))) | |
704 | { | |
705 | /* | |
706 | * We are administratively down, there is no state machine | |
707 | * handling. | |
708 | */ | |
709 | } | |
710 | ||
711 | void bs_down_handler(struct bfd_session *bs, int nstate) | |
712 | { | |
713 | switch (nstate) { | |
714 | case PTM_BFD_ADM_DOWN: | |
715 | /* | |
716 | * Remote peer doesn't want to talk, so lets keep the | |
717 | * connection down. | |
718 | */ | |
719 | case PTM_BFD_UP: | |
720 | /* Peer can't be up yet, wait it go to 'init' or 'down'. */ | |
721 | break; | |
722 | ||
723 | case PTM_BFD_DOWN: | |
724 | /* | |
725 | * Remote peer agreed that the path is down, lets try to | |
726 | * bring it up. | |
727 | */ | |
728 | bs->ses_state = PTM_BFD_INIT; | |
729 | break; | |
730 | ||
731 | case PTM_BFD_INIT: | |
732 | /* | |
733 | * Remote peer told us his path is up, lets turn | |
734 | * activate the session. | |
735 | */ | |
736 | ptm_bfd_ses_up(bs); | |
737 | break; | |
738 | ||
739 | default: | |
740 | log_debug("state-change: unhandled neighbor state: %d", nstate); | |
741 | break; | |
742 | } | |
743 | } | |
744 | ||
745 | void bs_init_handler(struct bfd_session *bs, int nstate) | |
746 | { | |
747 | switch (nstate) { | |
748 | case PTM_BFD_ADM_DOWN: | |
749 | /* | |
750 | * Remote peer doesn't want to talk, so lets make the | |
751 | * connection down. | |
752 | */ | |
753 | bs->ses_state = PTM_BFD_DOWN; | |
754 | break; | |
755 | ||
756 | case PTM_BFD_DOWN: | |
757 | /* Remote peer hasn't moved to first stage yet. */ | |
758 | break; | |
759 | ||
760 | case PTM_BFD_INIT: | |
761 | case PTM_BFD_UP: | |
762 | /* We agreed on the settings and the path is up. */ | |
763 | ptm_bfd_ses_up(bs); | |
764 | break; | |
765 | ||
766 | default: | |
767 | log_debug("state-change: unhandled neighbor state: %d", nstate); | |
768 | break; | |
769 | } | |
770 | } | |
771 | ||
772 | void bs_up_handler(struct bfd_session *bs, int nstate) | |
773 | { | |
774 | switch (nstate) { | |
775 | case PTM_BFD_ADM_DOWN: | |
776 | case PTM_BFD_DOWN: | |
777 | /* Peer lost or asked to shutdown connection. */ | |
778 | ptm_bfd_ses_dn(bs, BD_NEIGHBOR_DOWN); | |
779 | break; | |
780 | ||
781 | case PTM_BFD_INIT: | |
782 | case PTM_BFD_UP: | |
783 | /* Path is up and working. */ | |
784 | break; | |
785 | ||
786 | default: | |
787 | log_debug("state-change: unhandled neighbor state: %d", nstate); | |
788 | break; | |
789 | } | |
790 | } | |
791 | ||
792 | void bs_state_handler(struct bfd_session *bs, int nstate) | |
793 | { | |
794 | switch (bs->ses_state) { | |
795 | case PTM_BFD_ADM_DOWN: | |
796 | bs_admin_down_handler(bs, nstate); | |
797 | break; | |
798 | case PTM_BFD_DOWN: | |
799 | bs_down_handler(bs, nstate); | |
800 | break; | |
801 | case PTM_BFD_INIT: | |
802 | bs_init_handler(bs, nstate); | |
803 | break; | |
804 | case PTM_BFD_UP: | |
805 | bs_up_handler(bs, nstate); | |
806 | break; | |
807 | ||
808 | default: | |
809 | log_debug("state-change: [%s] is in invalid state: %d", | |
810 | bs_to_string(bs), nstate); | |
811 | break; | |
812 | } | |
813 | } | |
814 | ||
c0ef9a8a RZ |
815 | /* |
816 | * Handles echo timer manipulation after updating timer. | |
817 | */ | |
818 | void bs_echo_timer_handler(struct bfd_session *bs) | |
819 | { | |
820 | uint32_t old_timer; | |
821 | ||
822 | /* | |
823 | * Before doing any echo handling, check if it is possible to | |
824 | * use it. | |
825 | * | |
826 | * - Check for `echo-mode` configuration. | |
827 | * - Check that we are not using multi hop (RFC 5883, | |
828 | * Section 3). | |
829 | * - Check that we are already at the up state. | |
830 | */ | |
831 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO) == 0 | |
832 | || BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH) | |
833 | || bs->ses_state != PTM_BFD_UP) | |
834 | return; | |
835 | ||
836 | /* Remote peer asked to stop echo. */ | |
837 | if (bs->remote_timers.required_min_echo == 0) { | |
838 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO_ACTIVE)) | |
8bd859f6 | 839 | ptm_bfd_echo_stop(bs); |
c0ef9a8a RZ |
840 | |
841 | return; | |
842 | } | |
843 | ||
844 | /* | |
845 | * Calculate the echo transmission timer: we must not send | |
846 | * echo packets faster than the minimum required time | |
847 | * announced by the remote system. | |
848 | * | |
849 | * RFC 5880, Section 6.8.9. | |
850 | */ | |
851 | old_timer = bs->echo_xmt_TO; | |
852 | if (bs->remote_timers.required_min_echo > bs->timers.required_min_echo) | |
853 | bs->echo_xmt_TO = bs->remote_timers.required_min_echo; | |
854 | else | |
855 | bs->echo_xmt_TO = bs->timers.required_min_echo; | |
856 | ||
857 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_ECHO_ACTIVE) == 0 | |
858 | || old_timer != bs->echo_xmt_TO) | |
859 | ptm_bfd_echo_start(bs); | |
860 | } | |
861 | ||
862 | /* | |
863 | * RFC 5880 Section 6.5. | |
864 | * | |
865 | * When a BFD control packet with the final bit is received, we must | |
866 | * update the session parameters. | |
867 | */ | |
868 | void bs_final_handler(struct bfd_session *bs) | |
869 | { | |
870 | /* Start using our new timers. */ | |
f43b9368 RZ |
871 | bs->cur_timers.desired_min_tx = bs->timers.desired_min_tx; |
872 | bs->cur_timers.required_min_rx = bs->timers.required_min_rx; | |
c0ef9a8a RZ |
873 | |
874 | /* | |
875 | * TODO: demand mode. See RFC 5880 Section 6.1. | |
876 | * | |
877 | * When using demand mode we must disable the detection timer | |
878 | * for lost control packets. | |
879 | */ | |
880 | if (bs->demand_mode) { | |
881 | /* Notify watchers about changed timers. */ | |
882 | control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs); | |
883 | return; | |
884 | } | |
885 | ||
886 | /* | |
887 | * Calculate detection time based on new timers. | |
888 | * | |
889 | * Transmission calculation: | |
890 | * We must respect the RequiredMinRxInterval from the remote | |
891 | * system: if our desired transmission timer is more than the | |
892 | * minimum receive rate, then we must lower it to at least the | |
893 | * minimum receive interval. | |
894 | * | |
895 | * RFC 5880, Section 6.8.3. | |
896 | */ | |
897 | if (bs->timers.desired_min_tx > bs->remote_timers.required_min_rx) | |
898 | bs->xmt_TO = bs->remote_timers.required_min_rx; | |
899 | else | |
900 | bs->xmt_TO = bs->timers.desired_min_tx; | |
901 | ||
902 | /* Apply new transmission timer immediately. */ | |
903 | ptm_bfd_start_xmt_timer(bs, false); | |
904 | ||
905 | /* | |
906 | * Detection timeout calculation: | |
907 | * The minimum detection timeout is the remote detection | |
908 | * multipler (number of packets to be missed) times the agreed | |
909 | * transmission interval. | |
910 | * | |
911 | * RFC 5880, Section 6.8.4. | |
912 | * | |
913 | * TODO: support sending/counting more packets inside detection | |
914 | * timeout. | |
915 | */ | |
916 | if (bs->remote_timers.required_min_rx > bs->timers.desired_min_tx) | |
917 | bs->detect_TO = bs->remote_detect_mult | |
918 | * bs->remote_timers.required_min_rx; | |
919 | else | |
920 | bs->detect_TO = bs->remote_detect_mult | |
921 | * bs->timers.desired_min_tx; | |
922 | ||
923 | /* Apply new receive timer immediately. */ | |
924 | bfd_recvtimer_update(bs); | |
925 | ||
926 | /* Notify watchers about changed timers. */ | |
927 | control_notify_config(BCM_NOTIFY_CONFIG_UPDATE, bs); | |
928 | } | |
929 | ||
e9e2c950 RZ |
930 | |
931 | /* | |
932 | * Helper functions. | |
933 | */ | |
934 | static const char *get_diag_str(int diag) | |
935 | { | |
936 | for (int i = 0; diag_list[i].str; i++) { | |
937 | if (diag_list[i].type == diag) | |
938 | return diag_list[i].str; | |
939 | } | |
940 | return "N/A"; | |
941 | } | |
942 | ||
943 | const char *satostr(struct sockaddr_any *sa) | |
944 | { | |
945 | #define INETSTR_BUFCOUNT 8 | |
946 | static char buf[INETSTR_BUFCOUNT][INET6_ADDRSTRLEN]; | |
947 | static int bufidx; | |
948 | struct sockaddr_in *sin = &sa->sa_sin; | |
949 | struct sockaddr_in6 *sin6 = &sa->sa_sin6; | |
950 | ||
951 | bufidx += (bufidx + 1) % INETSTR_BUFCOUNT; | |
952 | buf[bufidx][0] = 0; | |
953 | ||
954 | switch (sin->sin_family) { | |
955 | case AF_INET: | |
956 | inet_ntop(AF_INET, &sin->sin_addr, buf[bufidx], | |
957 | sizeof(buf[bufidx])); | |
958 | break; | |
959 | case AF_INET6: | |
960 | inet_ntop(AF_INET6, &sin6->sin6_addr, buf[bufidx], | |
961 | sizeof(buf[bufidx])); | |
962 | break; | |
963 | ||
964 | default: | |
965 | strlcpy(buf[bufidx], "unknown", sizeof(buf[bufidx])); | |
966 | break; | |
967 | } | |
968 | ||
969 | return buf[bufidx]; | |
970 | } | |
971 | ||
972 | const char *diag2str(uint8_t diag) | |
973 | { | |
974 | switch (diag) { | |
975 | case 0: | |
976 | return "ok"; | |
977 | case 1: | |
978 | return "control detection time expired"; | |
979 | case 2: | |
980 | return "echo function failed"; | |
981 | case 3: | |
982 | return "neighbor signaled session down"; | |
983 | case 4: | |
984 | return "forwarding plane reset"; | |
985 | case 5: | |
986 | return "path down"; | |
987 | case 6: | |
988 | return "concatenated path down"; | |
989 | case 7: | |
990 | return "administratively down"; | |
991 | case 8: | |
992 | return "reverse concatenated path down"; | |
993 | default: | |
994 | return "unknown"; | |
995 | } | |
996 | } | |
997 | ||
998 | int strtosa(const char *addr, struct sockaddr_any *sa) | |
999 | { | |
1000 | memset(sa, 0, sizeof(*sa)); | |
1001 | ||
1002 | if (inet_pton(AF_INET, addr, &sa->sa_sin.sin_addr) == 1) { | |
1003 | sa->sa_sin.sin_family = AF_INET; | |
1004 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN | |
1005 | sa->sa_sin.sin_len = sizeof(sa->sa_sin); | |
1006 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
1007 | return 0; | |
1008 | } | |
1009 | ||
1010 | if (inet_pton(AF_INET6, addr, &sa->sa_sin6.sin6_addr) == 1) { | |
1011 | sa->sa_sin6.sin6_family = AF_INET6; | |
1012 | #ifdef HAVE_STRUCT_SOCKADDR_SA_LEN | |
1013 | sa->sa_sin6.sin6_len = sizeof(sa->sa_sin6); | |
1014 | #endif /* HAVE_STRUCT_SOCKADDR_SA_LEN */ | |
1015 | return 0; | |
1016 | } | |
1017 | ||
1018 | return -1; | |
1019 | } | |
1020 | ||
1021 | void integer2timestr(uint64_t time, char *buf, size_t buflen) | |
1022 | { | |
1023 | unsigned int year, month, day, hour, minute, second; | |
1024 | int rv; | |
1025 | ||
1026 | #define MINUTES (60) | |
23586b05 DS |
1027 | #define HOURS (60 * MINUTES) |
1028 | #define DAYS (24 * HOURS) | |
1029 | #define MONTHS (30 * DAYS) | |
1030 | #define YEARS (12 * MONTHS) | |
e9e2c950 RZ |
1031 | if (time >= YEARS) { |
1032 | year = time / YEARS; | |
1033 | time -= year * YEARS; | |
1034 | ||
1035 | rv = snprintf(buf, buflen, "%u year(s), ", year); | |
1036 | buf += rv; | |
1037 | buflen -= rv; | |
1038 | } | |
1039 | if (time >= MONTHS) { | |
1040 | month = time / MONTHS; | |
1041 | time -= month * MONTHS; | |
1042 | ||
1043 | rv = snprintf(buf, buflen, "%u month(s), ", month); | |
1044 | buf += rv; | |
1045 | buflen -= rv; | |
1046 | } | |
1047 | if (time >= DAYS) { | |
1048 | day = time / DAYS; | |
1049 | time -= day * DAYS; | |
1050 | ||
1051 | rv = snprintf(buf, buflen, "%u day(s), ", day); | |
1052 | buf += rv; | |
1053 | buflen -= rv; | |
1054 | } | |
1055 | if (time >= HOURS) { | |
1056 | hour = time / HOURS; | |
1057 | time -= hour * HOURS; | |
1058 | ||
1059 | rv = snprintf(buf, buflen, "%u hour(s), ", hour); | |
1060 | buf += rv; | |
1061 | buflen -= rv; | |
1062 | } | |
1063 | if (time >= MINUTES) { | |
1064 | minute = time / MINUTES; | |
1065 | time -= minute * MINUTES; | |
1066 | ||
1067 | rv = snprintf(buf, buflen, "%u minute(s), ", minute); | |
1068 | buf += rv; | |
1069 | buflen -= rv; | |
1070 | } | |
1071 | second = time % MINUTES; | |
1072 | snprintf(buf, buflen, "%u second(s)", second); | |
1073 | } | |
1074 | ||
03e7f088 RZ |
1075 | const char *bs_to_string(struct bfd_session *bs) |
1076 | { | |
1077 | static char buf[256]; | |
1078 | int pos; | |
1079 | bool is_mhop = BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH); | |
1080 | ||
1081 | pos = snprintf(buf, sizeof(buf), "mhop:%s", is_mhop ? "yes" : "no"); | |
1082 | if (BFD_CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)) { | |
1083 | pos += snprintf(buf + pos, sizeof(buf) - pos, | |
1084 | " peer:%s local:%s", satostr(&bs->mhop.peer), | |
1085 | satostr(&bs->mhop.local)); | |
1086 | ||
1087 | if (bs->mhop.vrf_name[0]) | |
1088 | snprintf(buf + pos, sizeof(buf) - pos, " vrf:%s", | |
1089 | bs->mhop.vrf_name); | |
1090 | } else { | |
1091 | pos += snprintf(buf + pos, sizeof(buf) - pos, " peer:%s", | |
1092 | satostr(&bs->shop.peer)); | |
1093 | ||
1094 | if (bs->local_address.sa_sin.sin_family) | |
1095 | pos += snprintf(buf + pos, sizeof(buf) - pos, | |
1096 | " local:%s", | |
1097 | satostr(&bs->local_address)); | |
1098 | ||
1099 | if (bs->shop.port_name[0]) | |
1100 | snprintf(buf + pos, sizeof(buf) - pos, " interface:%s", | |
1101 | bs->shop.port_name); | |
1102 | } | |
1103 | ||
1104 | return buf; | |
1105 | } | |
1106 | ||
e9e2c950 RZ |
1107 | |
1108 | /* | |
1109 | * BFD hash data structures to find sessions. | |
1110 | */ | |
1111 | static struct hash *bfd_id_hash; | |
1112 | static struct hash *bfd_shop_hash; | |
1113 | static struct hash *bfd_mhop_hash; | |
1114 | static struct hash *bfd_vrf_hash; | |
1115 | static struct hash *bfd_iface_hash; | |
1116 | ||
1117 | static unsigned int bfd_id_hash_do(void *p); | |
e9e2c950 | 1118 | static unsigned int bfd_shop_hash_do(void *p); |
e9e2c950 | 1119 | static unsigned int bfd_mhop_hash_do(void *p); |
e9e2c950 | 1120 | static unsigned int bfd_vrf_hash_do(void *p); |
e9e2c950 | 1121 | static unsigned int bfd_iface_hash_do(void *p); |
e9e2c950 RZ |
1122 | |
1123 | static void _shop_key(struct bfd_session *bs, const struct bfd_shop_key *shop); | |
1124 | static void _shop_key2(struct bfd_session *bs, const struct bfd_shop_key *shop); | |
1125 | static void _mhop_key(struct bfd_session *bs, const struct bfd_mhop_key *mhop); | |
1126 | static int _iface_key(struct bfd_iface *iface, const char *ifname); | |
1127 | ||
1128 | static void _bfd_free(struct hash_backet *hb, | |
1129 | void *arg __attribute__((__unused__))); | |
1130 | static void _vrf_free(void *arg); | |
1131 | static void _iface_free(void *arg); | |
1132 | ||
1133 | /* BFD hash for our discriminator. */ | |
1134 | static unsigned int bfd_id_hash_do(void *p) | |
1135 | { | |
1136 | struct bfd_session *bs = p; | |
1137 | ||
1138 | return jhash_1word(bs->discrs.my_discr, 0); | |
1139 | } | |
1140 | ||
74df8d6d | 1141 | static bool bfd_id_hash_cmp(const void *n1, const void *n2) |
e9e2c950 RZ |
1142 | { |
1143 | const struct bfd_session *bs1 = n1, *bs2 = n2; | |
1144 | ||
1145 | return bs1->discrs.my_discr == bs2->discrs.my_discr; | |
1146 | } | |
1147 | ||
1148 | /* BFD hash for single hop. */ | |
1149 | static unsigned int bfd_shop_hash_do(void *p) | |
1150 | { | |
1151 | struct bfd_session *bs = p; | |
1152 | ||
1153 | return jhash(&bs->shop, sizeof(bs->shop), 0); | |
1154 | } | |
1155 | ||
74df8d6d | 1156 | static bool bfd_shop_hash_cmp(const void *n1, const void *n2) |
e9e2c950 RZ |
1157 | { |
1158 | const struct bfd_session *bs1 = n1, *bs2 = n2; | |
1159 | ||
1160 | return memcmp(&bs1->shop, &bs2->shop, sizeof(bs1->shop)) == 0; | |
1161 | } | |
1162 | ||
1163 | /* BFD hash for multi hop. */ | |
1164 | static unsigned int bfd_mhop_hash_do(void *p) | |
1165 | { | |
1166 | struct bfd_session *bs = p; | |
1167 | ||
1168 | return jhash(&bs->mhop, sizeof(bs->mhop), 0); | |
1169 | } | |
1170 | ||
74df8d6d | 1171 | static bool bfd_mhop_hash_cmp(const void *n1, const void *n2) |
e9e2c950 RZ |
1172 | { |
1173 | const struct bfd_session *bs1 = n1, *bs2 = n2; | |
1174 | ||
1175 | return memcmp(&bs1->mhop, &bs2->mhop, sizeof(bs1->mhop)) == 0; | |
1176 | } | |
1177 | ||
1178 | /* BFD hash for VRFs. */ | |
1179 | static unsigned int bfd_vrf_hash_do(void *p) | |
1180 | { | |
1181 | struct bfd_vrf *vrf = p; | |
1182 | ||
1183 | return jhash_1word(vrf->vrf_id, 0); | |
1184 | } | |
1185 | ||
74df8d6d | 1186 | static bool bfd_vrf_hash_cmp(const void *n1, const void *n2) |
e9e2c950 RZ |
1187 | { |
1188 | const struct bfd_vrf *v1 = n1, *v2 = n2; | |
1189 | ||
1190 | return v1->vrf_id == v2->vrf_id; | |
1191 | } | |
1192 | ||
1193 | /* BFD hash for interfaces. */ | |
1194 | static unsigned int bfd_iface_hash_do(void *p) | |
1195 | { | |
1196 | struct bfd_iface *iface = p; | |
1197 | ||
1198 | return string_hash_make(iface->ifname); | |
1199 | } | |
1200 | ||
74df8d6d | 1201 | static bool bfd_iface_hash_cmp(const void *n1, const void *n2) |
e9e2c950 RZ |
1202 | { |
1203 | const struct bfd_iface *i1 = n1, *i2 = n2; | |
1204 | ||
1205 | return strcmp(i1->ifname, i2->ifname) == 0; | |
1206 | } | |
1207 | ||
1208 | /* Helper functions */ | |
1209 | static void _shop_key(struct bfd_session *bs, const struct bfd_shop_key *shop) | |
1210 | { | |
1211 | bs->shop = *shop; | |
1212 | ||
1213 | /* Remove unused fields. */ | |
1214 | switch (bs->shop.peer.sa_sin.sin_family) { | |
1215 | case AF_INET: | |
1216 | bs->shop.peer.sa_sin.sin_port = 0; | |
1217 | break; | |
1218 | case AF_INET6: | |
1219 | bs->shop.peer.sa_sin6.sin6_port = 0; | |
1220 | break; | |
1221 | } | |
1222 | } | |
1223 | ||
1224 | static void _shop_key2(struct bfd_session *bs, const struct bfd_shop_key *shop) | |
1225 | { | |
1226 | _shop_key(bs, shop); | |
1227 | memset(bs->shop.port_name, 0, sizeof(bs->shop.port_name)); | |
1228 | } | |
1229 | ||
1230 | static void _mhop_key(struct bfd_session *bs, const struct bfd_mhop_key *mhop) | |
1231 | { | |
1232 | bs->mhop = *mhop; | |
1233 | ||
1234 | /* Remove unused fields. */ | |
1235 | switch (bs->mhop.peer.sa_sin.sin_family) { | |
1236 | case AF_INET: | |
1237 | bs->mhop.peer.sa_sin.sin_port = 0; | |
1238 | bs->mhop.local.sa_sin.sin_port = 0; | |
1239 | break; | |
1240 | case AF_INET6: | |
1241 | bs->mhop.peer.sa_sin6.sin6_port = 0; | |
1242 | bs->mhop.local.sa_sin6.sin6_port = 0; | |
1243 | break; | |
1244 | } | |
1245 | } | |
1246 | ||
1247 | static int _iface_key(struct bfd_iface *iface, const char *ifname) | |
1248 | { | |
1249 | size_t slen = sizeof(iface->ifname); | |
1250 | ||
1251 | memset(iface->ifname, 0, slen); | |
1252 | if (strlcpy(iface->ifname, ifname, slen) >= slen) | |
1253 | return -1; | |
1254 | ||
1255 | return 0; | |
1256 | } | |
1257 | ||
e9e2c950 RZ |
1258 | /* |
1259 | * Hash public interface / exported functions. | |
1260 | */ | |
1261 | ||
1262 | /* Lookup functions. */ | |
1263 | struct bfd_session *bfd_id_lookup(uint32_t id) | |
1264 | { | |
1265 | struct bfd_session bs; | |
1266 | ||
1267 | bs.discrs.my_discr = id; | |
1268 | ||
1269 | return hash_lookup(bfd_id_hash, &bs); | |
1270 | } | |
1271 | ||
1272 | struct bfd_session *bfd_shop_lookup(struct bfd_shop_key shop) | |
1273 | { | |
1274 | struct bfd_session bs, *bsp; | |
1275 | ||
1276 | _shop_key(&bs, &shop); | |
1277 | ||
1278 | bsp = hash_lookup(bfd_shop_hash, &bs); | |
1279 | if (bsp == NULL && bs.shop.port_name[0] != 0) { | |
1280 | /* | |
1281 | * Since the local interface spec is optional, try | |
1282 | * searching the key without it as well. | |
1283 | */ | |
1284 | _shop_key2(&bs, &shop); | |
1285 | bsp = hash_lookup(bfd_shop_hash, &bs); | |
1286 | } | |
1287 | ||
1288 | return bsp; | |
1289 | } | |
1290 | ||
1291 | struct bfd_session *bfd_mhop_lookup(struct bfd_mhop_key mhop) | |
1292 | { | |
1293 | struct bfd_session bs; | |
1294 | ||
1295 | _mhop_key(&bs, &mhop); | |
1296 | ||
2055ea09 | 1297 | return hash_lookup(bfd_mhop_hash, &bs); |
e9e2c950 RZ |
1298 | } |
1299 | ||
1300 | struct bfd_vrf *bfd_vrf_lookup(int vrf_id) | |
1301 | { | |
1302 | struct bfd_vrf vrf; | |
1303 | ||
1304 | vrf.vrf_id = vrf_id; | |
1305 | ||
1306 | return hash_lookup(bfd_vrf_hash, &vrf); | |
1307 | } | |
1308 | ||
1309 | struct bfd_iface *bfd_iface_lookup(const char *ifname) | |
1310 | { | |
1311 | struct bfd_iface iface; | |
1312 | ||
1313 | if (_iface_key(&iface, ifname) != 0) | |
1314 | return NULL; | |
1315 | ||
1316 | return hash_lookup(bfd_iface_hash, &iface); | |
1317 | } | |
1318 | ||
1319 | /* | |
1320 | * Delete functions. | |
1321 | * | |
1322 | * Delete functions searches and remove the item from the hash and | |
1323 | * returns a pointer to the removed item data. If the item was not found | |
1324 | * then it returns NULL. | |
1325 | * | |
1326 | * The data stored inside the hash is not free()ed, so you must do it | |
1327 | * manually after getting the pointer back. | |
1328 | */ | |
1329 | struct bfd_session *bfd_id_delete(uint32_t id) | |
1330 | { | |
1331 | struct bfd_session bs; | |
1332 | ||
1333 | bs.discrs.my_discr = id; | |
1334 | ||
1335 | return hash_release(bfd_id_hash, &bs); | |
1336 | } | |
1337 | ||
1338 | struct bfd_session *bfd_shop_delete(struct bfd_shop_key shop) | |
1339 | { | |
1340 | struct bfd_session bs, *bsp; | |
1341 | ||
1342 | _shop_key(&bs, &shop); | |
1343 | bsp = hash_release(bfd_shop_hash, &bs); | |
1344 | if (bsp == NULL && shop.port_name[0] != 0) { | |
1345 | /* | |
1346 | * Since the local interface spec is optional, try | |
1347 | * searching the key without it as well. | |
1348 | */ | |
1349 | _shop_key2(&bs, &shop); | |
1350 | bsp = hash_release(bfd_shop_hash, &bs); | |
1351 | } | |
1352 | ||
1353 | return bsp; | |
1354 | } | |
1355 | ||
1356 | struct bfd_session *bfd_mhop_delete(struct bfd_mhop_key mhop) | |
1357 | { | |
1358 | struct bfd_session bs; | |
1359 | ||
1360 | _mhop_key(&bs, &mhop); | |
1361 | ||
1362 | return hash_release(bfd_mhop_hash, &bs); | |
1363 | } | |
1364 | ||
1365 | struct bfd_vrf *bfd_vrf_delete(int vrf_id) | |
1366 | { | |
1367 | struct bfd_vrf vrf; | |
1368 | ||
1369 | vrf.vrf_id = vrf_id; | |
1370 | ||
1371 | return hash_release(bfd_vrf_hash, &vrf); | |
1372 | } | |
1373 | ||
1374 | struct bfd_iface *bfd_iface_delete(const char *ifname) | |
1375 | { | |
1376 | struct bfd_iface iface; | |
1377 | ||
1378 | if (_iface_key(&iface, ifname) != 0) | |
1379 | return NULL; | |
1380 | ||
1381 | return hash_release(bfd_iface_hash, &iface); | |
1382 | } | |
1383 | ||
1384 | /* Iteration functions. */ | |
1385 | void bfd_id_iterate(hash_iter_func hif, void *arg) | |
1386 | { | |
1387 | hash_iterate(bfd_id_hash, hif, arg); | |
1388 | } | |
1389 | ||
1390 | void bfd_shop_iterate(hash_iter_func hif, void *arg) | |
1391 | { | |
1392 | hash_iterate(bfd_shop_hash, hif, arg); | |
1393 | } | |
1394 | ||
1395 | void bfd_mhop_iterate(hash_iter_func hif, void *arg) | |
1396 | { | |
1397 | hash_iterate(bfd_mhop_hash, hif, arg); | |
1398 | } | |
1399 | ||
1400 | void bfd_vrf_iterate(hash_iter_func hif, void *arg) | |
1401 | { | |
1402 | hash_iterate(bfd_vrf_hash, hif, arg); | |
1403 | } | |
1404 | ||
1405 | void bfd_iface_iterate(hash_iter_func hif, void *arg) | |
1406 | { | |
1407 | hash_iterate(bfd_iface_hash, hif, arg); | |
1408 | } | |
1409 | ||
1410 | /* | |
1411 | * Insert functions. | |
1412 | * | |
1413 | * Inserts session into hash and returns `true` on success, otherwise | |
1414 | * `false`. | |
1415 | */ | |
1416 | bool bfd_id_insert(struct bfd_session *bs) | |
1417 | { | |
1418 | return (hash_get(bfd_id_hash, bs, hash_alloc_intern) == bs); | |
1419 | } | |
1420 | ||
1421 | bool bfd_shop_insert(struct bfd_session *bs) | |
1422 | { | |
1423 | return (hash_get(bfd_shop_hash, bs, hash_alloc_intern) == bs); | |
1424 | } | |
1425 | ||
1426 | bool bfd_mhop_insert(struct bfd_session *bs) | |
1427 | { | |
1428 | return (hash_get(bfd_mhop_hash, bs, hash_alloc_intern) == bs); | |
1429 | } | |
1430 | ||
1431 | bool bfd_vrf_insert(struct bfd_vrf *vrf) | |
1432 | { | |
1433 | return (hash_get(bfd_vrf_hash, vrf, hash_alloc_intern) == vrf); | |
1434 | } | |
1435 | ||
1436 | bool bfd_iface_insert(struct bfd_iface *iface) | |
1437 | { | |
1438 | return (hash_get(bfd_iface_hash, iface, hash_alloc_intern) == iface); | |
1439 | } | |
1440 | ||
1441 | void bfd_initialize(void) | |
1442 | { | |
1443 | bfd_id_hash = hash_create(bfd_id_hash_do, bfd_id_hash_cmp, | |
1444 | "BFD discriminator hash"); | |
1445 | bfd_shop_hash = hash_create(bfd_shop_hash_do, bfd_shop_hash_cmp, | |
1446 | "BFD single hop hash"); | |
1447 | bfd_mhop_hash = hash_create(bfd_mhop_hash_do, bfd_mhop_hash_cmp, | |
1448 | "BFD multihop hop hash"); | |
1449 | bfd_vrf_hash = | |
1450 | hash_create(bfd_vrf_hash_do, bfd_vrf_hash_cmp, "BFD VRF hash"); | |
1451 | bfd_iface_hash = hash_create(bfd_iface_hash_do, bfd_iface_hash_cmp, | |
1452 | "BFD interface hash"); | |
1453 | } | |
1454 | ||
1455 | static void _bfd_free(struct hash_backet *hb, | |
1456 | void *arg __attribute__((__unused__))) | |
1457 | { | |
1458 | struct bfd_session *bs = hb->data; | |
1459 | ||
1460 | bfd_session_free(bs); | |
1461 | } | |
1462 | ||
1463 | static void _vrf_free(void *arg) | |
1464 | { | |
1465 | struct bfd_vrf *vrf = arg; | |
1466 | ||
1467 | XFREE(MTYPE_BFDD_CONFIG, vrf); | |
1468 | } | |
1469 | ||
1470 | static void _iface_free(void *arg) | |
1471 | { | |
1472 | struct bfd_iface *iface = arg; | |
1473 | ||
1474 | XFREE(MTYPE_BFDD_CONFIG, iface); | |
1475 | } | |
1476 | ||
1477 | void bfd_shutdown(void) | |
1478 | { | |
1479 | /* | |
1480 | * Close and free all BFD sessions. | |
1481 | * | |
1482 | * _bfd_free() will call bfd_session_free() which will take care | |
1483 | * of removing the session from all hashes, so we just run an | |
1484 | * assert() here to make sure it really happened. | |
1485 | */ | |
1486 | bfd_id_iterate(_bfd_free, NULL); | |
1487 | assert(bfd_shop_hash->count == 0); | |
1488 | assert(bfd_mhop_hash->count == 0); | |
1489 | ||
1490 | /* Clean the VRF and interface hashes. */ | |
1491 | hash_clean(bfd_vrf_hash, _vrf_free); | |
1492 | hash_clean(bfd_iface_hash, _iface_free); | |
1493 | ||
1494 | /* Now free the hashes themselves. */ | |
1495 | hash_free(bfd_id_hash); | |
1496 | hash_free(bfd_shop_hash); | |
1497 | hash_free(bfd_mhop_hash); | |
1498 | hash_free(bfd_vrf_hash); | |
1499 | hash_free(bfd_iface_hash); | |
1500 | } |