ospf6d: add 'log-adjacency-changes [detail]'

Similar to OSPFv2, add support for 'log-adjacency-changes [detail]' to log
changes in adjacency state of ospfv3 neighbors.

Signed-off-by: Pradosh Mohapatra <pmohapat at cumulusnetworks.com>
Reviewed-by: Dinesh G Dutt <ddutt at cumulusnetworks.com>
Reviewed-by: Scott Feldman <sfeldma at cumulusnetworks.com>
Reviewed-by: Shrijeet Mukherjee <shm at cumulusnetworks.com>
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
diff --git a/ospf6d/ospf6_neighbor.c b/ospf6d/ospf6_neighbor.c
index b7d2e40..fb209fd 100644
--- a/ospf6d/ospf6_neighbor.c
+++ b/ospf6d/ospf6_neighbor.c
@@ -142,7 +142,7 @@
 }
 
 static void
-ospf6_neighbor_state_change (u_char next_state, struct ospf6_neighbor *on)
+ospf6_neighbor_state_change (u_char next_state, struct ospf6_neighbor *on, int event)
 {
   u_char prev_state;
 
@@ -158,11 +158,23 @@
   /* log */
   if (IS_OSPF6_DEBUG_NEIGHBOR (STATE))
     {
-      zlog_debug ("Neighbor state change %s: [%s]->[%s]", on->name,
+      zlog_debug ("Neighbor state change %s: [%s]->[%s] (%s)", on->name,
 		  ospf6_neighbor_state_str[prev_state],
-		  ospf6_neighbor_state_str[next_state]);
+		  ospf6_neighbor_state_str[next_state],
+		  ospf6_neighbor_event_string(event));
     }
 
+  /* Optionally notify about adjacency changes */
+  if (CHECK_FLAG(on->ospf6_if->area->ospf6->config_flags,
+		 OSPF6_LOG_ADJACENCY_CHANGES) &&
+      (CHECK_FLAG(on->ospf6_if->area->ospf6->config_flags,
+		  OSPF6_LOG_ADJACENCY_DETAIL) ||
+       (next_state == OSPF6_NEIGHBOR_FULL) || (next_state < prev_state)))
+    zlog_notice("AdjChg: Nbr %s: %s -> %s (%s)", on->name,
+		ospf6_neighbor_state_str[prev_state],
+		ospf6_neighbor_state_str[next_state],
+		ospf6_neighbor_event_string(event));
+
   if (prev_state == OSPF6_NEIGHBOR_FULL || next_state == OSPF6_NEIGHBOR_FULL)
     {
       OSPF6_ROUTER_LSA_SCHEDULE (on->ospf6_if->area);
@@ -223,7 +235,8 @@
                                            on->ospf6_if->dead_interval);
 
   if (on->state <= OSPF6_NEIGHBOR_DOWN)
-    ospf6_neighbor_state_change (OSPF6_NEIGHBOR_INIT, on);
+    ospf6_neighbor_state_change (OSPF6_NEIGHBOR_INIT, on,
+				 OSPF6_NEIGHBOR_EVENT_HELLO_RCVD);
 
   return 0;
 }
@@ -246,11 +259,13 @@
 
   if (! need_adjacency (on))
     {
-      ospf6_neighbor_state_change (OSPF6_NEIGHBOR_TWOWAY, on);
+      ospf6_neighbor_state_change (OSPF6_NEIGHBOR_TWOWAY, on,
+				   OSPF6_NEIGHBOR_EVENT_TWOWAY_RCVD);
       return 0;
     }
 
-  ospf6_neighbor_state_change (OSPF6_NEIGHBOR_EXSTART, on);
+  ospf6_neighbor_state_change (OSPF6_NEIGHBOR_EXSTART, on,
+			       OSPF6_NEIGHBOR_EVENT_TWOWAY_RCVD);
   SET_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MSBIT);
   SET_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MBIT);
   SET_FLAG (on->dbdesc_bits, OSPF6_DBDESC_IBIT);
@@ -327,7 +342,8 @@
     }
 
   UNSET_FLAG (on->dbdesc_bits, OSPF6_DBDESC_IBIT);
-  ospf6_neighbor_state_change (OSPF6_NEIGHBOR_EXCHANGE, on);
+  ospf6_neighbor_state_change (OSPF6_NEIGHBOR_EXCHANGE, on,
+			       OSPF6_NEIGHBOR_EVENT_NEGOTIATION_DONE);
 
   return 0;
 }
@@ -355,10 +371,12 @@
 */
 
   if (on->request_list->count == 0)
-    ospf6_neighbor_state_change (OSPF6_NEIGHBOR_FULL, on);
+    ospf6_neighbor_state_change (OSPF6_NEIGHBOR_FULL, on,
+				 OSPF6_NEIGHBOR_EVENT_EXCHANGE_DONE);
   else
     {
-      ospf6_neighbor_state_change (OSPF6_NEIGHBOR_LOADING, on);
+      ospf6_neighbor_state_change (OSPF6_NEIGHBOR_LOADING, on,
+				   OSPF6_NEIGHBOR_EVENT_EXCHANGE_DONE);
 
       if (on->thread_send_lsreq == NULL)
 	on->thread_send_lsreq =
@@ -408,7 +426,8 @@
 
   assert (on->request_list->count == 0);
 
-  ospf6_neighbor_state_change (OSPF6_NEIGHBOR_FULL, on);
+  ospf6_neighbor_state_change (OSPF6_NEIGHBOR_FULL, on,
+			       OSPF6_NEIGHBOR_EVENT_LOADING_DONE);
 
   return 0;
 }
@@ -427,7 +446,8 @@
 
   if (on->state == OSPF6_NEIGHBOR_TWOWAY && need_adjacency (on))
     {
-      ospf6_neighbor_state_change (OSPF6_NEIGHBOR_EXSTART, on);
+      ospf6_neighbor_state_change (OSPF6_NEIGHBOR_EXSTART, on,
+				   OSPF6_NEIGHBOR_EVENT_ADJ_OK);
       SET_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MSBIT);
       SET_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MBIT);
       SET_FLAG (on->dbdesc_bits, OSPF6_DBDESC_IBIT);
@@ -440,7 +460,8 @@
   else if (on->state >= OSPF6_NEIGHBOR_EXSTART &&
            ! need_adjacency (on))
     {
-      ospf6_neighbor_state_change (OSPF6_NEIGHBOR_TWOWAY, on);
+      ospf6_neighbor_state_change (OSPF6_NEIGHBOR_TWOWAY, on,
+				   OSPF6_NEIGHBOR_EVENT_ADJ_OK);
       ospf6_lsdb_remove_all (on->summary_list);
       ospf6_lsdb_remove_all (on->request_list);
       for (lsa = ospf6_lsdb_head (on->retrans_list); lsa;
@@ -469,7 +490,8 @@
   if (IS_OSPF6_DEBUG_NEIGHBOR (EVENT))
     zlog_debug ("Neighbor Event %s: *SeqNumberMismatch*", on->name);
 
-  ospf6_neighbor_state_change (OSPF6_NEIGHBOR_EXSTART, on);
+  ospf6_neighbor_state_change (OSPF6_NEIGHBOR_EXSTART, on,
+			       OSPF6_NEIGHBOR_EVENT_SEQNUMBER_MISMATCH);
   SET_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MSBIT);
   SET_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MBIT);
   SET_FLAG (on->dbdesc_bits, OSPF6_DBDESC_IBIT);
@@ -507,7 +529,8 @@
   if (IS_OSPF6_DEBUG_NEIGHBOR (EVENT))
     zlog_debug ("Neighbor Event %s: *BadLSReq*", on->name);
 
-  ospf6_neighbor_state_change (OSPF6_NEIGHBOR_EXSTART, on);
+  ospf6_neighbor_state_change (OSPF6_NEIGHBOR_EXSTART, on,
+			       OSPF6_NEIGHBOR_EVENT_BAD_LSREQ);
   SET_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MSBIT);
   SET_FLAG (on->dbdesc_bits, OSPF6_DBDESC_MBIT);
   SET_FLAG (on->dbdesc_bits, OSPF6_DBDESC_IBIT);
@@ -545,7 +568,8 @@
   if (IS_OSPF6_DEBUG_NEIGHBOR (EVENT))
     zlog_debug ("Neighbor Event %s: *1Way-Received*", on->name);
 
-  ospf6_neighbor_state_change (OSPF6_NEIGHBOR_INIT, on);
+  ospf6_neighbor_state_change (OSPF6_NEIGHBOR_INIT, on,
+			       OSPF6_NEIGHBOR_EVENT_ONEWAY_RCVD);
   thread_add_event (master, neighbor_change, on->ospf6_if, 0);
 
   ospf6_lsdb_remove_all (on->summary_list);
@@ -580,7 +604,8 @@
   on->drouter = on->prev_drouter = 0;
   on->bdrouter = on->prev_bdrouter = 0;
 
-  ospf6_neighbor_state_change (OSPF6_NEIGHBOR_DOWN, on);
+  ospf6_neighbor_state_change (OSPF6_NEIGHBOR_DOWN, on,
+			       OSPF6_NEIGHBOR_EVENT_INACTIVITY_TIMER);
   thread_add_event (master, neighbor_change, on->ospf6_if, 0);
 
   listnode_delete (on->ospf6_if->neighbor_list, on);
diff --git a/ospf6d/ospf6_neighbor.h b/ospf6d/ospf6_neighbor.h
index 750e1b2..8882189 100644
--- a/ospf6d/ospf6_neighbor.h
+++ b/ospf6d/ospf6_neighbor.h
@@ -108,6 +108,44 @@
 #define OSPF6_NEIGHBOR_LOADING  7
 #define OSPF6_NEIGHBOR_FULL     8
 
+/* Neighbor Events */
+#define OSPF6_NEIGHBOR_EVENT_NO_EVENT             0
+#define OSPF6_NEIGHBOR_EVENT_HELLO_RCVD           1
+#define OSPF6_NEIGHBOR_EVENT_TWOWAY_RCVD          2
+#define OSPF6_NEIGHBOR_EVENT_NEGOTIATION_DONE     3
+#define OSPF6_NEIGHBOR_EVENT_EXCHANGE_DONE        4
+#define OSPF6_NEIGHBOR_EVENT_LOADING_DONE         5
+#define OSPF6_NEIGHBOR_EVENT_ADJ_OK               6
+#define OSPF6_NEIGHBOR_EVENT_SEQNUMBER_MISMATCH   7
+#define OSPF6_NEIGHBOR_EVENT_BAD_LSREQ            8
+#define OSPF6_NEIGHBOR_EVENT_ONEWAY_RCVD          9
+#define OSPF6_NEIGHBOR_EVENT_INACTIVITY_TIMER    10
+#define OSPF6_NEIGHBOR_EVENT_MAX_EVENT           11
+
+static const char *ospf6_neighbor_event_str[] =
+  {
+    "NoEvent",
+    "HelloReceived",
+    "2-WayReceived",
+    "NegotiationDone",
+    "ExchangeDone",
+    "LoadingDone",
+    "AdjOK?",
+    "SeqNumberMismatch",
+    "BadLSReq",
+    "1-WayReceived",
+    "InactivityTimer",
+  };
+
+static const char *ospf6_neighbor_event_string (int event)
+{
+  #define OSPF6_NEIGHBOR_UNKNOWN_EVENT_STRING "UnknownEvent"
+
+  if (event < OSPF6_NEIGHBOR_EVENT_MAX_EVENT)
+      return ospf6_neighbor_event_str[event];
+  return OSPF6_NEIGHBOR_UNKNOWN_EVENT_STRING;
+}
+
 extern const char *ospf6_neighbor_state_str[];
 
 
diff --git a/ospf6d/ospf6_top.c b/ospf6d/ospf6_top.c
index dec7096..1f7cdc8 100644
--- a/ospf6d/ospf6_top.c
+++ b/ospf6d/ospf6_top.c
@@ -338,6 +338,56 @@
   return CMD_SUCCESS;
 }
 
+DEFUN (ospf6_log_adjacency_changes,
+       ospf6_log_adjacency_changes_cmd,
+       "log-adjacency-changes",
+       "Log changes in adjacency state\n")
+{
+  struct ospf6 *ospf6 = vty->index;
+
+  SET_FLAG(ospf6->config_flags, OSPF6_LOG_ADJACENCY_CHANGES);
+  return CMD_SUCCESS;
+}
+
+DEFUN (ospf6_log_adjacency_changes_detail,
+       ospf6_log_adjacency_changes_detail_cmd,
+       "log-adjacency-changes detail",
+              "Log changes in adjacency state\n"
+       "Log all state changes\n")
+{
+  struct ospf6 *ospf6 = vty->index;
+
+  SET_FLAG(ospf6->config_flags, OSPF6_LOG_ADJACENCY_CHANGES);
+  SET_FLAG(ospf6->config_flags, OSPF6_LOG_ADJACENCY_DETAIL);
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_ospf6_log_adjacency_changes,
+       no_ospf6_log_adjacency_changes_cmd,
+       "no log-adjacency-changes",
+              NO_STR
+       "Log changes in adjacency state\n")
+{
+  struct ospf6 *ospf6 = vty->index;
+
+  UNSET_FLAG(ospf6->config_flags, OSPF6_LOG_ADJACENCY_DETAIL);
+  UNSET_FLAG(ospf6->config_flags, OSPF6_LOG_ADJACENCY_CHANGES);
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_ospf6_log_adjacency_changes_detail,
+       no_ospf6_log_adjacency_changes_detail_cmd,
+       "no log-adjacency-changes detail",
+              NO_STR
+              "Log changes in adjacency state\n"
+       "Log all state changes\n")
+{
+  struct ospf6 *ospf6 = vty->index;
+
+  UNSET_FLAG(ospf6->config_flags, OSPF6_LOG_ADJACENCY_DETAIL);
+  return CMD_SUCCESS;
+}
+
 DEFUN (ospf6_interface_area,
        ospf6_interface_area_cmd,
        "interface IFNAME area A.B.C.D",
@@ -592,6 +642,16 @@
   vty_out (vty, " Number of areas in this router is %u%s",
            listcount (o->area_list), VNL);
 
+  if (CHECK_FLAG(o->config_flags, OSPF6_LOG_ADJACENCY_CHANGES))
+    {
+      if (CHECK_FLAG(o->config_flags, OSPF6_LOG_ADJACENCY_DETAIL))
+	vty_out(vty, " All adjacency changes are logged%s",VTY_NEWLINE);
+      else
+	vty_out(vty, " Adjacency changes are logged%s",VTY_NEWLINE);
+    }
+
+  vty_out (vty, "%s",VTY_NEWLINE);
+
   for (ALL_LIST_ELEMENTS_RO (o->area_list, n, oa))
     ospf6_area_show (vty, oa);
 }
@@ -782,6 +842,15 @@
   if (ospf6->router_id_static != 0)
     vty_out (vty, " router-id %s%s", router_id, VNL);
 
+  /* log-adjacency-changes flag print. */
+  if (CHECK_FLAG(ospf6->config_flags, OSPF6_LOG_ADJACENCY_CHANGES))
+    {
+      vty_out(vty, " log-adjacency-changes");
+      if (CHECK_FLAG(ospf6->config_flags, OSPF6_LOG_ADJACENCY_DETAIL))
+	vty_out(vty, " detail");
+      vty_out(vty, "%s", VTY_NEWLINE);
+    }
+
   ospf6_stub_router_config_write (vty);
   ospf6_redistribute_config_write (vty);
   ospf6_area_config_write (vty);
@@ -836,6 +905,10 @@
 
   install_default (OSPF6_NODE);
   install_element (OSPF6_NODE, &ospf6_router_id_cmd);
+  install_element (OSPF6_NODE, &ospf6_log_adjacency_changes_cmd);
+  install_element (OSPF6_NODE, &ospf6_log_adjacency_changes_detail_cmd);
+  install_element (OSPF6_NODE, &no_ospf6_log_adjacency_changes_cmd);
+  install_element (OSPF6_NODE, &no_ospf6_log_adjacency_changes_detail_cmd);
   install_element (OSPF6_NODE, &ospf6_interface_area_cmd);
   install_element (OSPF6_NODE, &no_ospf6_interface_area_cmd);
   install_element (OSPF6_NODE, &ospf6_stub_router_admin_cmd);
diff --git a/ospf6d/ospf6_top.h b/ospf6d/ospf6_top.h
index 02eb915..9d7cfd9 100644
--- a/ospf6d/ospf6_top.h
+++ b/ospf6d/ospf6_top.h
@@ -60,6 +60,11 @@
 
   u_char flag;
 
+  /* Configured flags */
+  u_char config_flags;
+#define OSPF6_LOG_ADJACENCY_CHANGES      (1 << 0)
+#define OSPF6_LOG_ADJACENCY_DETAIL       (1 << 1)
+
   /* SPF parameters */
   unsigned int spf_delay;		/* SPF delay time. */
   unsigned int spf_holdtime;		/* SPF hold time. */