]> git.puffer.fish Git - matthieu/frr.git/commitdiff
pimd: Close AutoRP socket when not needed
authorNathan Bahr <nbahr@atcorp.com>
Mon, 27 Jan 2025 15:57:04 +0000 (15:57 +0000)
committerNathan Bahr <nbahr@atcorp.com>
Fri, 31 Jan 2025 15:49:37 +0000 (15:49 +0000)
Don't leave the socket open if we are not enabled for discovery
or announcements.

Signed-off-by: Nathan Bahr <nbahr@atcorp.com>
(cherry picked from commit 5d102a0a7014e9c526381bacc398a9b06a64ca24)

pimd/pim_autorp.c
pimd/pim_cmd_common.c

index a07bd4ab3af7215b7dcfcb58ca6263569f28d7bb..e06f48fdc64487e57f993348a8a9e251af6bec4e 100644 (file)
@@ -83,6 +83,12 @@ static void pim_autorp_free(struct pim_autorp *autorp)
        pim_autorp_rp_fini(&(autorp->candidate_rp_list));
 }
 
+static bool pim_autorp_should_close(struct pim_autorp *autorp)
+{
+       /* If discovery is active, then we need the socket open */
+       return !autorp->do_discovery;
+}
+
 static bool pim_autorp_join_groups(struct interface *ifp)
 {
        struct pim_interface *pim_ifp;
@@ -490,6 +496,10 @@ static bool pim_autorp_socket_enable(struct pim_autorp *autorp)
 {
        int fd;
 
+       /* Return early if socket is already enabled */
+       if (autorp->sock != -1)
+               return true;
+
        frr_with_privs (&pimd_privs) {
                fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
                if (fd < 0) {
@@ -516,6 +526,10 @@ static bool pim_autorp_socket_enable(struct pim_autorp *autorp)
 
 static bool pim_autorp_socket_disable(struct pim_autorp *autorp)
 {
+       /* Return early if socket is already disabled */
+       if (autorp->sock == -1)
+               return true;
+
        if (close(autorp->sock)) {
                zlog_warn("Failure closing autorp socket: fd=%d errno=%d: %s",
                          autorp->sock, errno, safe_strerror(errno));
@@ -967,6 +981,12 @@ void pim_autorp_start_discovery(struct pim_instance *pim)
        struct interface *ifp;
        struct pim_autorp *autorp = pim->autorp;
 
+       /* Make sure the socket is open and ready */
+       if (!pim_autorp_socket_enable(autorp)) {
+               zlog_err("%s: AutoRP failed to open socket", __func__);
+               return;
+       }
+
        if (!autorp->do_discovery) {
                autorp->do_discovery = true;
                autorp_read_on(autorp);
@@ -996,6 +1016,10 @@ void pim_autorp_stop_discovery(struct pim_instance *pim)
                if (PIM_DEBUG_AUTORP)
                        zlog_debug("%s: AutoRP Discovery stopped", __func__);
        }
+
+       /* Close the socket if we need to */
+       if (pim_autorp_should_close(autorp) && !pim_autorp_socket_disable(autorp))
+               zlog_warn("%s: AutoRP failed to close socket", __func__);
 }
 
 void pim_autorp_init(struct pim_instance *pim)
@@ -1016,11 +1040,6 @@ void pim_autorp_init(struct pim_instance *pim)
 
        pim->autorp = autorp;
 
-       if (!pim_autorp_socket_enable(autorp)) {
-               zlog_err("%s: AutoRP failed to initialize, feature will not work correctly", __func__);
-               return;
-       }
-
        if (PIM_DEBUG_AUTORP)
                zlog_debug("%s: AutoRP Initialized", __func__);
 
index 02ddea8252d6bc4fd3fa688ac3e6f240b405d376..78da083445c5583b42b83877fe00d8753305265c 100644 (file)
@@ -608,26 +608,14 @@ int pim_process_no_rp_plist_cmd(struct vty *vty, const char *rp_str,
 
 int pim_process_autorp_cmd(struct vty *vty)
 {
-       char xpath[XPATH_MAXLEN];
-
-       snprintf(xpath, sizeof(xpath), "%s/%s", FRR_PIM_AUTORP_XPATH,
-                "discovery-enabled");
-
-       nb_cli_enqueue_change(vty, xpath, NB_OP_MODIFY, "true");
-
-       return nb_cli_apply_changes(vty, NULL);
+       nb_cli_enqueue_change(vty, "./discovery-enabled", NB_OP_MODIFY, "true");
+       return nb_cli_apply_changes(vty, "%s", FRR_PIM_AUTORP_XPATH);
 }
 
 int pim_process_no_autorp_cmd(struct vty *vty)
 {
-       char xpath[XPATH_MAXLEN];
-
-       snprintf(xpath, sizeof(xpath), "%s/%s", FRR_PIM_AUTORP_XPATH,
-                "discovery-enabled");
-
-       nb_cli_enqueue_change(vty, xpath, NB_OP_DESTROY, NULL);
-
-       return nb_cli_apply_changes(vty, NULL);
+       nb_cli_enqueue_change(vty, "./discovery-enabled", NB_OP_MODIFY, "false");
+       return nb_cli_apply_changes(vty, "%s", FRR_PIM_AUTORP_XPATH);
 }
 
 int pim_process_autorp_candidate_rp_cmd(struct vty *vty, bool no,