diff options
author | Donald Sharp <sharpd@nvidia.com> | 2025-01-09 18:51:16 +0100 |
---|---|---|
committer | Donald Sharp <sharpd@nvidia.com> | 2025-01-10 16:07:11 +0100 |
commit | 348c2dc3f8dc683552c880dfbcb1e0b7dd203692 (patch) | |
tree | 4beaf9de73d3f396f12917043b128f33b97191c8 | |
parent | bgpd: su_remote and su_local are properties of the connection (diff) | |
download | frr-348c2dc3f8dc683552c880dfbcb1e0b7dd203692.tar.xz frr-348c2dc3f8dc683552c880dfbcb1e0b7dd203692.zip |
bgpd: Only update peer connection information when needed
Currently bgp is repeatedly grabbing peer connection information.
This is a bit overkill. There are two situations:
a) Opening a connection to the peer
In this case, we know the remote port/address a priori and can get
the local information by just asking the OS.
b) Peer opening a connection to us.
In this case, we know the local port/address a priori and can get
the remote information by just asking the OS.
Modify the code to just grab this data at the appropriate time.
Signed-off-by: Donald Sharp <sharpd@nvidia.com>
-rw-r--r-- | bgpd/bgp_fsm.c | 1 | ||||
-rw-r--r-- | bgpd/bgp_network.c | 36 | ||||
-rw-r--r-- | bgpd/bgp_network.h | 1 |
3 files changed, 22 insertions, 16 deletions
diff --git a/bgpd/bgp_fsm.c b/bgpd/bgp_fsm.c index 8097f5317..0e3ed9f0d 100644 --- a/bgpd/bgp_fsm.c +++ b/bgpd/bgp_fsm.c @@ -1819,7 +1819,6 @@ static void bgp_connect_in_progress_update_connection(struct peer_connection *co { struct peer *peer = connection->peer; - bgp_updatesockname(connection); if (!connection->su_remote && !BGP_CONNECTION_SU_UNSPEC(connection)) { /* if connect initiated, then dest port and dest addresses are well known */ connection->su_remote = sockunion_dup(&connection->su); diff --git a/bgpd/bgp_network.c b/bgpd/bgp_network.c index e4984ea46..af5d815d3 100644 --- a/bgpd/bgp_network.c +++ b/bgpd/bgp_network.c @@ -484,6 +484,9 @@ static void bgp_accept(struct event *thread) /* Dynamic neighbor has been created, let it proceed */ connection1->fd = bgp_sock; + connection1->su_local = sockunion_getsockname(connection1->fd); + connection1->su_remote = sockunion_dup(&su); + if (bgp_set_socket_ttl(connection1) < 0) { peer1->last_reset = PEER_DOWN_SOCKET_ERROR; zlog_err("%s: Unable to set min/max TTL on peer %s (dynamic), error received: %s(%d)", @@ -623,7 +626,10 @@ static void bgp_accept(struct event *thread) peer->doppelganger = peer1; peer1->doppelganger = peer; + connection->fd = bgp_sock; + connection->su_local = sockunion_getsockname(connection->fd); + connection->su_remote = sockunion_dup(&su); if (bgp_set_socket_ttl(connection) < 0) if (bgp_debug_neighbor_events(peer)) @@ -857,24 +863,28 @@ enum connect_result bgp_connect(struct peer_connection *connection) peer->host, connection->fd); /* Connect to the remote peer. */ - return sockunion_connect(connection->fd, &connection->su, - htons(peer->port), ifindex); -} + enum connect_result res; -void bgp_updatesockname(struct peer_connection *connection) -{ - if (connection->su_local) { - sockunion_free(connection->su_local); - connection->su_local = NULL; - } + res = sockunion_connect(connection->fd, &connection->su, htons(peer->port), ifindex); - if (connection->su_remote) { + if (connection->su_remote) sockunion_free(connection->su_remote); - connection->su_remote = NULL; + + connection->su_remote = sockunion_dup(&connection->su); + switch (connection->su.sa.sa_family) { + case AF_INET: + connection->su_remote->sin.sin_port = htons(peer->port); + break; + case AF_INET6: + connection->su_remote->sin6.sin6_port = htons(peer->port); + break; } + if (connection->su_local) + sockunion_free(connection->su_local); connection->su_local = sockunion_getsockname(connection->fd); - connection->su_remote = sockunion_getpeername(connection->fd); + + return res; } /* After TCP connection is established. Get local address and port. */ @@ -882,8 +892,6 @@ int bgp_getsockname(struct peer_connection *connection) { struct peer *peer = connection->peer; - bgp_updatesockname(connection); - if (!bgp_zebra_nexthop_set(connection->su_local, connection->su_remote, &peer->nexthop, peer)) { flog_err(EC_BGP_NH_UPD, diff --git a/bgpd/bgp_network.h b/bgpd/bgp_network.h index bd8c285f3..a2f4851f1 100644 --- a/bgpd/bgp_network.h +++ b/bgpd/bgp_network.h @@ -23,7 +23,6 @@ extern void bgp_close_vrf_socket(struct bgp *bgp); extern void bgp_close(void); extern enum connect_result bgp_connect(struct peer_connection *connection); extern int bgp_getsockname(struct peer_connection *connection); -extern void bgp_updatesockname(struct peer_connection *connection); extern int bgp_md5_set_prefix(struct bgp *bgp, struct prefix *p, const char *password); |