From 4e38f82a0ad11891d08147b32d4a6b65daa02a94 Mon Sep 17 00:00:00 2001 From: Rafael Zalamena Date: Mon, 10 Aug 2020 17:17:16 -0300 Subject: [PATCH] bfdd: simplify and remove duplicated code Move the session configuration application logic to a single function. Signed-off-by: Rafael Zalamena --- bfdd/bfd.c | 67 ++++++++++++++----------------------------- bfdd/bfd.h | 9 ++++++ bfdd/bfdd_nb_config.c | 16 +++++------ 3 files changed, 37 insertions(+), 55 deletions(-) diff --git a/bfdd/bfd.c b/bfdd/bfd.c index 1d50d98c78..1fc9224752 100644 --- a/bfdd/bfd.c +++ b/bfdd/bfd.c @@ -125,55 +125,12 @@ void bfd_profile_free(struct bfd_profile *bp) XFREE(MTYPE_BFDD_PROFILE, bp); } -/** - * Removes a profile and tests whether it needs to apply the changes or not. - * - * \param bs the BFD session. - * \param apply whether or not to apply configurations immediately. - */ -static void _bfd_profile_remove(struct bfd_session *bs, bool apply) -{ - struct bfd_profile *bp; - - /* No profile applied, nothing to do. */ - bp = bs->profile; - if (bp == NULL) - return; - - /* Remove the profile association. */ - bs->profile = NULL; - - /* Set multiplier to the default. */ - bs->detect_mult = bs->peer_profile.detection_multiplier; - - /* Set timers back to user configuration. */ - bs->timers.desired_min_tx = bs->peer_profile.min_tx; - bs->timers.required_min_rx = bs->peer_profile.min_rx; - - /* We can only apply echo options on single hop sessions. */ - if (!CHECK_FLAG(bs->flags, BFD_SESS_FLAG_MH)) { - /* Set default echo timer. */ - bs->timers.required_min_echo = bs->peer_profile.min_echo_rx; - - /* Default is no echo mode. */ - if (apply) - bfd_set_echo(bs, bs->peer_profile.echo_mode); - } - - if (apply) { - bfd_set_passive_mode(bs, bs->peer_profile.passive); - bfd_set_shutdown(bs, bs->peer_profile.admin_shutdown); - } -} - void bfd_profile_apply(const char *profname, struct bfd_session *bs) { struct bfd_profile *bp; /* Remove previous profile if any. */ if (bs->profile_name) { - _bfd_profile_remove(bs, false); - /* We are changing profiles. */ if (strcmp(bs->profile_name, profname)) { XFREE(MTYPE_BFDD_PROFILE, bs->profile_name); @@ -185,12 +142,23 @@ void bfd_profile_apply(const char *profname, struct bfd_session *bs) /* Look up new profile to apply. */ bp = bfd_profile_lookup(profname); - if (bp == NULL) - return; /* Point to profile if it exists. */ bs->profile = bp; + /* Apply configuration. */ + bfd_session_apply(bs); +} + +void bfd_session_apply(struct bfd_session *bs) +{ + struct bfd_profile *bp; + uint32_t min_tx = bs->timers.desired_min_tx; + uint32_t min_rx = bs->timers.required_min_rx; + + /* Pick the source of configuration. */ + bp = bs->profile ? bs->profile : &bs->peer_profile; + /* Set multiplier if not the default. */ if (bs->peer_profile.detection_multiplier == BFD_DEFDETECTMULT) bs->detect_mult = bp->detection_multiplier; @@ -235,14 +203,21 @@ void bfd_profile_apply(const char *profname, struct bfd_session *bs) bfd_set_shutdown(bs, bp->admin_shutdown); else bfd_set_shutdown(bs, bs->peer_profile.admin_shutdown); + + /* If session interval changed negotiate new timers. */ + if (bs->ses_state == PTM_BFD_UP + && (bs->timers.desired_min_tx != min_tx + || bs->timers.required_min_rx != min_rx)) + bfd_set_polling(bs); } void bfd_profile_remove(struct bfd_session *bs) { /* Remove any previous set profile name. */ XFREE(MTYPE_BFDD_PROFILE, bs->profile_name); + bs->profile = NULL; - _bfd_profile_remove(bs, true); + bfd_session_apply(bs); } void gen_bfd_key(struct bfd_key *key, struct sockaddr_any *peer, diff --git a/bfdd/bfd.h b/bfdd/bfd.h index 88ca17a9a9..4f88e6b2ec 100644 --- a/bfdd/bfd.h +++ b/bfdd/bfd.h @@ -618,6 +618,15 @@ void bfd_set_shutdown(struct bfd_session *bs, bool shutdown); */ void bfd_set_passive_mode(struct bfd_session *bs, bool passive); +/** + * Picks the BFD session configuration from the appropriated source: + * if using the default peer configuration prefer profile (if it exists), + * otherwise use session. + * + * \param bs the BFD session. + */ +void bfd_session_apply(struct bfd_session *bs); + /* BFD hash data structures interface */ void bfd_initialize(void); void bfd_shutdown(void); diff --git a/bfdd/bfdd_nb_config.c b/bfdd/bfdd_nb_config.c index d50f746d3d..527c6d83ad 100644 --- a/bfdd/bfdd_nb_config.c +++ b/bfdd/bfdd_nb_config.c @@ -519,8 +519,8 @@ int bfdd_bfd_sessions_single_hop_detection_multiplier_modify( case NB_EV_APPLY: bs = nb_running_get_entry(args->dnode, NULL, true); - bs->detect_mult = detection_multiplier; bs->peer_profile.detection_multiplier = detection_multiplier; + bfd_session_apply(bs); break; case NB_EV_ABORT: @@ -555,9 +555,8 @@ int bfdd_bfd_sessions_single_hop_desired_transmission_interval_modify( if (tx_interval == bs->timers.desired_min_tx) return NB_OK; - bs->timers.desired_min_tx = tx_interval; bs->peer_profile.min_tx = tx_interval; - bfd_set_polling(bs); + bfd_session_apply(bs); break; case NB_EV_ABORT: @@ -592,9 +591,8 @@ int bfdd_bfd_sessions_single_hop_required_receive_interval_modify( if (rx_interval == bs->timers.required_min_rx) return NB_OK; - bs->timers.required_min_rx = rx_interval; bs->peer_profile.min_rx = rx_interval; - bfd_set_polling(bs); + bfd_session_apply(bs); break; case NB_EV_ABORT: @@ -628,7 +626,7 @@ int bfdd_bfd_sessions_single_hop_administrative_down_modify( bs = nb_running_get_entry(args->dnode, NULL, true); bs->peer_profile.admin_shutdown = shutdown; - bfd_set_shutdown(bs, shutdown); + bfd_session_apply(bs); return NB_OK; } @@ -658,7 +656,7 @@ int bfdd_bfd_sessions_single_hop_passive_mode_modify( bs = nb_running_get_entry(args->dnode, NULL, true); bs->peer_profile.passive = passive; - bfd_set_passive_mode(bs, passive); + bfd_session_apply(bs); return NB_OK; } @@ -686,7 +684,7 @@ int bfdd_bfd_sessions_single_hop_echo_mode_modify( bs = nb_running_get_entry(args->dnode, NULL, true); bs->peer_profile.echo_mode = echo; - bfd_set_echo(bs, echo); + bfd_session_apply(bs); return NB_OK; } @@ -716,8 +714,8 @@ int bfdd_bfd_sessions_single_hop_desired_echo_transmission_interval_modify( if (echo_interval == bs->timers.required_min_echo) return NB_OK; - bs->timers.required_min_echo = echo_interval; bs->peer_profile.min_echo_rx = echo_interval; + bfd_session_apply(bs); break; case NB_EV_ABORT: -- 2.39.5