pimd: Add ability to set DR Priority for an interface

From RFC 4601, Section 4.3.1:

The DR_Priority Option allows a network administrator to give
preference to a particular router in the DR election process by
giving it a numerically larger DR Priority.  The DR_Priority Option
SHOULD be included in every Hello message, even if no DR Priority is
explicitly configured on that interface.  This is necessary because
priority-based DR election is only enabled when all neighbors on an
interface advertise that they are capable of using the DR_Priority
Option.  The default priority is 1.

This modification allows the setting of the DR Priority on a per
interface basis.

Signed-off-by: Donald Sharp <sharpd@cumulusnetworks.com>
diff --git a/pimd/pim_cmd.c b/pimd/pim_cmd.c
index 10d0c86..9833411 100644
--- a/pimd/pim_cmd.c
+++ b/pimd/pim_cmd.c
@@ -563,10 +563,11 @@
   now = pim_time_monotonic_sec();
 
   vty_out(vty,
-	  "NonPri: Number of neighbors missing DR Priority hello option%s%s",
-	  VTY_NEWLINE, VTY_NEWLINE);
+	  "NonPri: Number of neighbors missing DR Priority hello option%s"
+	  "DrPri: Designated Router Priority sent%s%s",
+	  VTY_NEWLINE, VTY_NEWLINE, VTY_NEWLINE);
   
-  vty_out(vty, "Interface Address         DR              Uptime   Elections Changes NonPri%s", VTY_NEWLINE);
+  vty_out(vty, "Interface Address         DR              Uptime   Elections Changes NonPri      DrPri%s", VTY_NEWLINE);
 
   for (ALL_LIST_ELEMENTS_RO(iflist, node, ifp)) {
     struct pim_interface *pim_ifp;
@@ -590,7 +591,7 @@
     pim_inet4_dump("<dr?>", pim_ifp->pim_dr_addr,
 		   dr_str, sizeof(dr_str));
 
-    vty_out(vty, "%-9s %-15s %-15s %8s %9d %7d %6d%s",
+    vty_out(vty, "%-9s %-15s %-15s %8s %9d %7d %6d %10d%s",
 	    ifp->name,
 	    inet_ntoa(ifaddr),
 	    dr_str,
@@ -598,6 +599,7 @@
 	    pim_ifp->pim_dr_election_count,
 	    pim_ifp->pim_dr_election_changes,
 	    pim_ifp->pim_dr_num_nondrpri_neighbors,
+	    pim_ifp->pim_dr_priority,
 	    VTY_NEWLINE);
   }
 }
@@ -3123,6 +3125,66 @@
   return CMD_SUCCESS;
 }
 
+DEFUN (interface_ip_pim_drprio,
+       interface_ip_pim_drprio_cmd,
+       "ip pim drpriority <1-4294967295>",
+       IP_STR
+       PIM_STR
+       "Set the Designated Router Election Priority\n"
+       "Value of the new DR Priority\n")
+{
+  struct interface *ifp;
+  struct pim_interface *pim_ifp;
+  uint32_t old_dr_prio;
+
+  ifp = vty->index;
+  pim_ifp = ifp->info;
+
+  if (!pim_ifp) {
+    vty_out(vty, "Please enable PIM on interface, first%s", VTY_NEWLINE);
+    return CMD_WARNING;
+  }
+
+  old_dr_prio = pim_ifp->pim_dr_priority;
+
+  pim_ifp->pim_dr_priority = strtol(argv[0], NULL, 10);
+
+  if (old_dr_prio != pim_ifp->pim_dr_priority) {
+    if (pim_if_dr_election(ifp))
+      pim_hello_restart_now(ifp);
+  }
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (interface_no_ip_pim_drprio,
+       interface_no_ip_pim_drprio_cmd,
+       "no ip pim drpriority {<1-4294967295>}",
+       IP_STR
+       PIM_STR
+       "Revert the Designated Router Priority to default\n"
+       "Old Value of the Priority\n")
+{
+  struct interface *ifp;
+  struct pim_interface *pim_ifp;
+
+  ifp = vty->index;
+  pim_ifp = ifp->info;
+
+  if (!pim_ifp) {
+    vty_out(vty, "Pim no enabled on this interface%s", VTY_NEWLINE);
+    return CMD_WARNING;
+  }
+
+  if (pim_ifp->pim_dr_priority != PIM_DEFAULT_DR_PRIORITY) {
+    pim_ifp->pim_dr_priority = PIM_DEFAULT_DR_PRIORITY;
+    if (pim_if_dr_election(ifp))
+      pim_hello_restart_now(ifp);
+  }
+
+  return CMD_SUCCESS;
+}
+
 DEFUN (interface_ip_pim_ssm,
        interface_ip_pim_ssm_cmd,
        "ip pim ssm",
@@ -4684,7 +4746,9 @@
   install_element (INTERFACE_NODE, &interface_ip_igmp_query_max_response_time_dsec_cmd);
   install_element (INTERFACE_NODE, &interface_no_ip_igmp_query_max_response_time_dsec_cmd); 
   install_element (INTERFACE_NODE, &interface_ip_pim_ssm_cmd);
-  install_element (INTERFACE_NODE, &interface_no_ip_pim_ssm_cmd); 
+  install_element (INTERFACE_NODE, &interface_no_ip_pim_ssm_cmd);
+  install_element (INTERFACE_NODE, &interface_ip_pim_drprio_cmd);
+  install_element (INTERFACE_NODE, &interface_no_ip_pim_drprio_cmd);
 
   // Static mroutes NEB
   install_element (INTERFACE_NODE, &interface_ip_mroute_cmd);
diff --git a/pimd/pim_neighbor.c b/pimd/pim_neighbor.c
index 9404cec..eaf36df 100644
--- a/pimd/pim_neighbor.c
+++ b/pimd/pim_neighbor.c
@@ -106,7 +106,7 @@
   PIM Hello message is received, when a neighbor times out, or when a
   router's own DR Priority changes.
  */
-void pim_if_dr_election(struct interface *ifp)
+int pim_if_dr_election(struct interface *ifp)
 {
   struct pim_interface *pim_ifp = ifp->info;
   struct in_addr old_dr_addr;
@@ -140,7 +140,10 @@
     pim_if_update_join_desired(pim_ifp);
     pim_if_update_could_assert(ifp);
     pim_if_update_assert_tracking_desired(ifp);
+    return 1;
   }
+
+  return 0;
 }
 
 static void update_dr_priority(struct pim_neighbor *neigh,
diff --git a/pimd/pim_neighbor.h b/pimd/pim_neighbor.h
index 8f19c75..5b2172d 100644
--- a/pimd/pim_neighbor.h
+++ b/pimd/pim_neighbor.h
@@ -69,6 +69,6 @@
 			 struct list *addr_list);
 struct prefix *pim_neighbor_find_secondary(struct pim_neighbor *neigh,
 					   struct in_addr addr);
-void pim_if_dr_election(struct interface *ifp);
+int pim_if_dr_election(struct interface *ifp);
 
 #endif /* PIM_NEIGHBOR_H */
diff --git a/pimd/pim_vty.c b/pimd/pim_vty.c
index bcace95..e31e2d6 100644
--- a/pimd/pim_vty.c
+++ b/pimd/pim_vty.c
@@ -31,6 +31,7 @@
 #include "pim_cmd.h"
 #include "pim_str.h"
 #include "pim_ssmpingd.h"
+#include "pim_pim.h"
 
 int pim_debug_config_write(struct vty *vty)
 {
@@ -134,6 +135,13 @@
 	++writes;
       }
 
+      /* IF ip pim drpriority */
+      if (pim_ifp->pim_dr_priority != PIM_DEFAULT_DR_PRIORITY) {
+	vty_out(vty, " ip pim drpriority %d%s", pim_ifp->pim_dr_priority,
+		VTY_NEWLINE);
+	++writes;
+      }
+
       /* IF ip igmp */
       if (PIM_IF_TEST_IGMP(pim_ifp->options)) {
 	vty_out(vty, " ip igmp%s", VTY_NEWLINE);