[quagga-dev 10831] [PATCH 4/4] ospfd: ospfd-ip-ospf-interface-command.patch

Dinesh G Dutt ddutt at cumulusnetworks.com
Thu Oct 24 05:06:57 BST 2013


OSPFv2: Support for specifying area with "ip ospf" interface command

per-interface ospf enable and area set command.

Signed-off-by: James Li <jli at cumulusnetworks.com>
---
 doc/ospfd.texi         |    5 +
 ospfd/ospf_interface.h |    2 +
 ospfd/ospf_vty.c       |  109 +++++++++++++++++++++++++++++++
 ospfd/ospfd.c          |  170 ++++++++++++++++++++++++++++++++++++++++++------
 ospfd/ospfd.h          |    5 +
 5 files changed, 271 insertions(+), 20 deletions(-)

diff --git a/doc/ospfd.texi b/doc/ospfd.texi
index 856a2ba..5b1b2ac 100644
--- a/doc/ospfd.texi
+++ b/doc/ospfd.texi
@@ -507,6 +507,11 @@ incremented by this value when transmitting.
 The default value is 1 seconds.
 @end deffn
 
+ at deffn {Interface Command} {ip ospf area (A.B.C.D|<0-4294967295>)} {}
+ at deffnx {Interface Command} {no ip ospf area} {}
+Enable ospf on an interface and set associated area.
+ at end deffn
+
 @node Redistribute routes to OSPF
 @section Redistribute routes to OSPF
 
diff --git a/ospfd/ospf_interface.h b/ospfd/ospf_interface.h
index ed698c8..64fa786 100644
--- a/ospfd/ospf_interface.h
+++ b/ospfd/ospf_interface.h
@@ -47,6 +47,8 @@ struct ospf_if_params
   DECLARE_IF_PARAM (u_int32_t, retransmit_interval); /* Retransmission Interval */
   DECLARE_IF_PARAM (u_char, passive_interface);      /* OSPF Interface is passive: no sending or receiving (no need to join multicast groups) */
   DECLARE_IF_PARAM (u_char, priority);               /* OSPF Interface priority */
+  /* Enable OSPF on this interface with area if_area */
+  DECLARE_IF_PARAM (struct in_addr, if_area);
   DECLARE_IF_PARAM (u_char, type);                   /* type of interface */
 #define OSPF_IF_ACTIVE                  0
 #define OSPF_IF_PASSIVE		        1
diff --git a/ospfd/ospf_vty.c b/ospfd/ospf_vty.c
index dcd7129..e3ebd9b 100644
--- a/ospfd/ospf_vty.c
+++ b/ospfd/ospf_vty.c
@@ -446,6 +446,13 @@ DEFUN (ospf_network_area,
   struct in_addr area_id;
   int ret, format;
 
+  if (ospf->if_ospf_cli_count > 0)
+    {
+      vty_out (vty, "Please remove all ip ospf area x.x.x.x command.%s",
+               VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
   /* Get network prefix and Area ID. */
   VTY_GET_IPV4_PREFIX ("network prefix", p, argv[0]);
   VTY_GET_OSPF_AREA_ID (area_id, format, argv[1]);
@@ -5830,6 +5837,96 @@ ALIAS (no_ip_ospf_transmit_delay,
        "OSPF interface commands\n"
        "Link state transmit delay\n")
 
+DEFUN (ip_ospf_area,
+       ip_ospf_area_cmd,
+       "ip ospf area (A.B.C.D|<0-4294967295>)",
+       "IP Information\n"
+       "OSPF interface commands\n"
+       "Enable OSPF on this interface\n"
+       "OSPF area ID in IP address format\n"
+       "OSPF area ID as a decimal value\n")
+{
+  struct interface *ifp = vty->index;
+  int format, ret;
+  struct in_addr area_id;
+  struct ospf *ospf;
+  struct ospf_if_params *params;
+  struct route_node *rn;
+
+  ospf = ospf_lookup ();
+  if (ospf == NULL)
+    {
+      vty_out (vty, " OSPF Routing Process not enabled%s", VTY_NEWLINE);
+      return CMD_SUCCESS;
+    }
+
+  ret = ospf_str2area_id (argv[0], &area_id, &format);
+  if (ret < 0)
+    {
+      vty_out (vty, "Please specify area by A.B.C.D|<0-4294967295>%s",
+               VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+  if (memcmp (ifp->name, "VLINK", 5) == 0)
+    {
+      vty_out (vty, "Cannot enable OSPF on a virtual link.%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  params = IF_DEF_PARAMS (ifp);
+  if (OSPF_IF_PARAM_CONFIGURED(params, if_area))
+    {
+      vty_out (vty, "OSPF already enabled on this interface.%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  for (rn = route_top (ospf->networks); rn; rn = route_next (rn))
+    {
+      if (rn->info != NULL)
+        {
+          vty_out (vty, "Please remove network command first.%s", VTY_NEWLINE);
+          return CMD_WARNING;
+        }
+    }
+
+  /* enable ospf on this interface with area_id */
+  ospf_interface_set (ifp, area_id);
+  ospf->if_ospf_cli_count++;
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_ip_ospf_area,
+       no_ip_ospf_area_cmd,
+       "no ip ospf area",
+       NO_STR
+       "IP Information\n"
+       "OSPF interface commands\n"
+       "Disable OSPF on this interface\n")
+{
+  struct interface *ifp = vty->index;
+  struct ospf *ospf;
+  struct ospf_if_params *params;
+
+  ospf = ospf_lookup ();
+  if (ospf == NULL)
+    {
+      vty_out (vty, " OSPF Routing Process not enabled%s", VTY_NEWLINE);
+      return CMD_SUCCESS;
+    }
+
+  params = IF_DEF_PARAMS (ifp);
+  if (!OSPF_IF_PARAM_CONFIGURED(params, if_area))
+    {
+      vty_out (vty, "Can't find specified inteface area configuration.%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  ospf_interface_unset (ifp);
+  ospf->if_ospf_cli_count--;
+  return CMD_SUCCESS;
+}
+
 
 DEFUN (ospf_redistribute_source_metric_type,
        ospf_redistribute_source_metric_type_routemap_cmd,
@@ -7642,6 +7739,14 @@ config_write_interface (struct vty *vty)
 	    vty_out (vty, "%s", VTY_NEWLINE);
 	  }
 
+       /* Area  print. */
+       if (OSPF_IF_PARAM_CONFIGURED (params, if_area))
+         {
+           vty_out (vty, " ip ospf area %s%s",
+                    inet_ntoa (params->if_area),
+                    VTY_NEWLINE);
+         }
+
     /* MTU ignore print. */
     if (OSPF_IF_PARAM_CONFIGURED (params, mtu_ignore) &&
        params->mtu_ignore != OSPF_MTU_IGNORE_DEFAULT)
@@ -8288,6 +8393,10 @@ ospf_vty_if_init (void)
   install_element (INTERFACE_NODE, &no_ip_ospf_transmit_delay_addr_cmd);
   install_element (INTERFACE_NODE, &no_ip_ospf_transmit_delay_cmd);
 
+  /* "ip ospf area" commands. */
+  install_element (INTERFACE_NODE, &ip_ospf_area_cmd);
+  install_element (INTERFACE_NODE, &no_ip_ospf_area_cmd);
+
   /* These commands are compatibitliy for previous version. */
   install_element (INTERFACE_NODE, &ospf_authentication_key_cmd);
   install_element (INTERFACE_NODE, &no_ospf_authentication_key_cmd);
diff --git a/ospfd/ospfd.c b/ospfd/ospfd.c
index b1df5db..18d0db2 100644
--- a/ospfd/ospfd.c
+++ b/ospfd/ospfd.c
@@ -720,6 +720,76 @@ ospf_area_del_if (struct ospf_area *area, struct ospf_interface *oi)
   listnode_delete (area->oiflist, oi);
 }
 
+static struct ospf_interface *
+add_ospf_interface (struct interface *ifp, struct ospf_area *area,
+                    struct connected *co)
+{
+  struct ospf_interface *oi;
+
+  oi = ospf_if_new (area->ospf, ifp, co->address);
+  oi->connected = co;
+
+  oi->area = area;
+
+  oi->params = ospf_lookup_if_params (ifp, oi->address->u.prefix4);
+  oi->output_cost = ospf_if_get_output_cost (oi);
+
+  /* Add pseudo neighbor. */
+  ospf_nbr_add_self (oi);
+
+  /* Relate ospf interface to ospf instance. */
+  oi->ospf = area->ospf;
+
+  /* update network type as interface flag */
+  /* If network type is specified previously,
+     skip network type setting. */
+  oi->type = IF_DEF_PARAMS (ifp)->type;
+
+  ospf_area_add_if (oi->area, oi);
+
+  /* if router_id is not configured, dont bring up
+   * interfaces.
+   * ospf_router_id_update() will call ospf_if_update
+   * whenever r-id is configured instead.
+   */
+  if ((area->ospf->router_id.s_addr != 0)
+      && if_is_operative (ifp))
+    ospf_if_up (oi);
+
+  return (oi);
+}
+
+static void update_redistributed(struct ospf *ospf, int add_to_ospf)
+{
+  struct route_node *rn;
+  struct external_info *ei;
+
+  if (ospf_is_type_redistributed (ZEBRA_ROUTE_CONNECT))
+    if (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT))
+      {
+        for (rn = route_top (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT));
+             rn; rn = route_next (rn))
+          {
+            if ((ei = rn->info) != NULL)
+              {
+                if (add_to_ospf)
+                  {
+                    if (ospf_external_info_find_lsa (ospf, &ei->p))
+                      if (!ospf_distribute_check_connected (ospf, ei))
+                        ospf_external_lsa_flush (ospf, ei->type, &ei->p,
+                                                 ei->ifindex /*, ei->nexthop */);
+                  }
+                else
+                  {
+                    if (!ospf_external_info_find_lsa (ospf, &ei->p))
+                      if (ospf_distribute_check_connected (ospf, ei))
+                        ospf_external_lsa_originate (ospf, ei);
+                  }
+              }
+          }
+      }
+}
+
 
 /* Config network statement related functions. */
 static struct ospf_network *
@@ -749,7 +819,6 @@ ospf_network_set (struct ospf *ospf, struct prefix_ipv4 *p,
   struct ospf_network *network;
   struct ospf_area *area;
   struct route_node *rn;
-  struct external_info *ei;
   int ret = OSPF_AREA_ID_FORMAT_ADDRESS;
 
   rn = route_node_get (ospf->networks, (struct prefix *)p);
@@ -767,15 +836,7 @@ ospf_network_set (struct ospf *ospf, struct prefix_ipv4 *p,
   ospf_network_run ((struct prefix *)p, area);
 
   /* Update connected redistribute. */
-  if (ospf_is_type_redistributed (ZEBRA_ROUTE_CONNECT))
-    if (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT))
-      for (rn = route_top (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT));
-	   rn; rn = route_next (rn))
-	if ((ei = rn->info) != NULL)
-	  if (ospf_external_info_find_lsa (ospf, &ei->p))
-	    if (!ospf_distribute_check_connected (ospf, ei))
-	      ospf_external_lsa_flush (ospf, ei->type, &ei->p,
-				       ei->ifindex /*, ei->nexthop */);
+  update_redistributed(ospf, 1); /* interfaces possibly added */
 
   ospf_area_check_free (ospf, area_id);
 
@@ -788,7 +849,6 @@ ospf_network_unset (struct ospf *ospf, struct prefix_ipv4 *p,
 {
   struct route_node *rn;
   struct ospf_network *network;
-  struct external_info *ei;
   struct listnode *node, *nnode;
   struct ospf_interface *oi;
 
@@ -832,14 +892,76 @@ ospf_network_unset (struct ospf *ospf, struct prefix_ipv4 *p,
     }
   
   /* Update connected redistribute. */
-  if (ospf_is_type_redistributed (ZEBRA_ROUTE_CONNECT))
-    if (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT))
-      for (rn = route_top (EXTERNAL_INFO (ZEBRA_ROUTE_CONNECT));
-	   rn; rn = route_next (rn))
-	if ((ei = rn->info) != NULL)
-	  if (!ospf_external_info_find_lsa (ospf, &ei->p))
-	    if (ospf_distribute_check_connected (ospf, ei))
-	      ospf_external_lsa_originate (ospf, ei);
+  update_redistributed(ospf, 0); /* interfaces possibly removed */
+  ospf_area_check_free (ospf, area_id);
+
+  return 1;
+}
+
+int
+ospf_interface_set (struct interface *ifp, struct in_addr area_id)
+{
+  struct ospf_area *area;
+  struct listnode *cnode;
+  struct connected *co;
+  struct ospf *ospf;
+  struct ospf_if_params *params;
+  int ret = OSPF_AREA_ID_FORMAT_ADDRESS;
+
+  if ((ospf = ospf_lookup ()) == NULL)
+    return 1; /* Ospf not ready yet */
+
+  params = IF_DEF_PARAMS (ifp);
+
+  SET_IF_PARAM (params, if_area);
+  params->if_area = area_id;
+
+  area = ospf_area_get (ospf, area_id, ret);
+
+  for (ALL_LIST_ELEMENTS_RO (ifp->connected, cnode, co))
+    {
+      if (CHECK_FLAG(co->flags,ZEBRA_IFA_SECONDARY))
+        continue;
+
+      if (co->address->family == AF_INET
+          && !ospf_if_table_lookup(ifp, co->address))
+        {
+          add_ospf_interface(ifp, area, co);
+        }
+    }
+
+  /* Update connected redistribute. */
+  update_redistributed(ospf, 1); /* interface possibly added */
+  return 1;
+}
+
+int
+ospf_interface_unset (struct interface *ifp)
+{
+  struct ospf *ospf;
+  struct ospf_if_params *params;
+  struct listnode *node, *nnode;
+  struct ospf_interface *oi;
+  struct in_addr area_id;
+
+  ospf = ospf_lookup ();
+  if (!ospf)
+    return 1; /* Ospf not ready yet */
+
+  params = IF_DEF_PARAMS (ifp);
+  UNSET_IF_PARAM (params, if_area);
+  area_id = params->if_area;
+
+  for (ALL_LIST_ELEMENTS (ospf->oiflist, node, nnode, oi))
+    {
+      if (oi->type == OSPF_IFTYPE_VIRTUALLINK)
+        continue;
+      ospf_if_free (oi);
+    }
+
+  /* Update connected redistribute. */
+  update_redistributed(ospf, 0); /* interfaces possibly removed */
+  ospf_area_check_free (ospf, area_id);
 
   return 1;
 }
@@ -959,7 +1081,8 @@ ospf_if_update (struct ospf *ospf, struct interface *ifp)
   struct route_node *rn;
   struct ospf_network *network;
   struct ospf_area *area;
-  
+  struct ospf_if_params *params;
+
   if (!ospf)
     ospf = ospf_lookup ();
 
@@ -975,6 +1098,13 @@ ospf_if_update (struct ospf *ospf, struct interface *ifp)
         area = ospf_area_get (ospf, network->area_id, network->format);
         ospf_network_run_interface (&rn->p, area, ifp);
       }
+
+  /* create oif for any new co */
+  params = IF_DEF_PARAMS (ifp);
+  if (OSPF_IF_PARAM_CONFIGURED(params, if_area))
+    {
+      ospf_interface_set (ifp, params->if_area);
+    }
 }
 
 void
diff --git a/ospfd/ospfd.h b/ospfd/ospfd.h
index 1081334..7ee2784 100644
--- a/ospfd/ospfd.h
+++ b/ospfd/ospfd.h
@@ -274,6 +274,9 @@ struct ospf
   /* Statistics for LSA used for new instantiation. */
   u_int32_t rx_lsa_count;
  
+  /* Counter of "ip ospf area x.x.x.x" */
+  u_int32_t if_ospf_cli_count;
+
   struct route_table *distance_table;
 };
 
@@ -558,4 +561,6 @@ extern void ospf_snmp_init (void);
 
 extern void ospf_master_init (void);
 
+extern int ospf_interface_set (struct interface *ifp, struct in_addr area_id);
+extern int ospf_interface_unset (struct interface *ifp);
 #endif /* _ZEBRA_OSPFD_H */





More information about the Quagga-dev mailing list