ospf6d: add "auto-cost reference-bandwidth" command

This command allows the user to change to default reference bandwidth
for cost calculations. The default value is 100 Mbps. With a default
bandwidth of 10 MBps, the default cost becomes 10. Those values are
consistent with OSPFv2.

[DL: resolved conflicts in vty command additions & docs]
Signed-off-by: David Lamparter <equinox@opensourcerouting.org>
diff --git a/doc/ospf6d.texi b/doc/ospf6d.texi
index d9bf06d..31f4db0 100644
--- a/doc/ospf6d.texi
+++ b/doc/ospf6d.texi
@@ -66,6 +66,18 @@
 
 @end deffn
 
+@deffn {OSPF6 Command} {auto-cost reference-bandwidth @var{cost}} {}
+@deffnx {OSPF6 Command} {no auto-cost reference-bandwidth} {}
+This sets the reference bandwidth for cost calculations, where this
+bandwidth is considered equivalent to an OSPF cost of 1, specified in
+Mbits/s. The default is 100Mbit/s (i.e. a link of bandwidth 100Mbit/s
+or higher will have a cost of 1. Cost of lower bandwidth links will be
+scaled with reference to this cost).
+
+This configuration setting MUST be consistent across all routers
+within the OSPF domain.
+@end deffn
+
 @node OSPF6 area
 @section OSPF6 area
 
@@ -75,7 +87,8 @@
 @section OSPF6 interface
 
 @deffn {Interface Command} {ipv6 ospf6 cost COST} {}
-Sets interface's output cost.  Default value depends on the interface bandwidth.
+Sets interface's output cost.  Default value depends on the interface
+bandwidth and on the auto-cost reference bandwidth.
 @end deffn
 
 @deffn {Interface Command} {ipv6 ospf6 hello-interval HELLOINTERVAL} {}
diff --git a/ospf6d/README b/ospf6d/README
index 6db347f..f5a0046 100644
--- a/ospf6d/README
+++ b/ospf6d/README
@@ -85,6 +85,15 @@
     Binds interface to specified Area, and start
     sending OSPFv3 packets.
 
+  auto-cost reference-bandwidth COST
+    Sets the reference bandwidth for cost calculations, where this
+    bandwidth is considered equivalent to an OSPF cost of 1, specified
+    in Mbits/s. The default is 100Mbit/s (i.e. a link of bandwidth
+    100Mbit/s or higher will have a cost of 1. Cost of lower bandwidth
+    links will be scaled with reference to this cost).  This
+    configuration setting MUST be consistent across all routers within
+    the OSPF domain.
+
 Sample configuration is in ospf6d.conf.sample.
 
 --
diff --git a/ospf6d/ospf6_interface.c b/ospf6d/ospf6_interface.c
index e698893..4bc6155 100644
--- a/ospf6d/ospf6_interface.c
+++ b/ospf6d/ospf6_interface.c
@@ -125,7 +125,7 @@
   u_int32_t bw, refbw;
 
   bw = oi->interface->bandwidth ? oi->interface->bandwidth : OSPF6_INTERFACE_BANDWIDTH;
-  refbw = OSPF6_REFERENCE_BANDWIDTH;
+  refbw = ospf6 ? ospf6->ref_bandwidth : OSPF6_REFERENCE_BANDWIDTH;
 
   /* A specifed ip ospf cost overrides a calculated one. */
   if (CHECK_FLAG (oi->flag, OSPF6_INTERFACE_NOAUTOCOST))
@@ -1300,6 +1300,61 @@
   return CMD_SUCCESS;
 }
 
+DEFUN (auto_cost_reference_bandwidth,
+       auto_cost_reference_bandwidth_cmd,
+       "auto-cost reference-bandwidth <1-4294967>",
+       "Calculate OSPF interface cost according to bandwidth\n"
+       "Use reference bandwidth method to assign OSPF cost\n"
+       "The reference bandwidth in terms of Mbits per second\n")
+{
+  struct ospf6 *o = vty->index;
+  struct ospf6_area *oa;
+  struct ospf6_interface *oi;
+  struct listnode *i, *j;
+  u_int32_t refbw;
+
+  refbw = strtol (argv[0], NULL, 10);
+  if (refbw < 1 || refbw > 4294967)
+    {
+      vty_out (vty, "reference-bandwidth value is invalid%s", VTY_NEWLINE);
+      return CMD_WARNING;
+    }
+
+  /* If reference bandwidth is changed. */
+  if ((refbw * 1000) == o->ref_bandwidth)
+    return CMD_SUCCESS;
+
+  o->ref_bandwidth = refbw * 1000;
+  for (ALL_LIST_ELEMENTS_RO (o->area_list, i, oa))
+      for (ALL_LIST_ELEMENTS_RO (oa->if_list, j, oi))
+          ospf6_interface_recalculate_cost (oi);
+
+  return CMD_SUCCESS;
+}
+
+DEFUN (no_auto_cost_reference_bandwidth,
+       no_auto_cost_reference_bandwidth_cmd,
+       "no auto-cost reference-bandwidth",
+       NO_STR
+       "Calculate OSPF interface cost according to bandwidth\n"
+       "Use reference bandwidth method to assign OSPF cost\n")
+{
+  struct ospf6 *o = vty->index;
+  struct ospf6_area *oa;
+  struct ospf6_interface *oi;
+  struct listnode *i, *j;
+
+  if (o->ref_bandwidth == OSPF6_REFERENCE_BANDWIDTH)
+    return CMD_SUCCESS;
+
+  o->ref_bandwidth = OSPF6_REFERENCE_BANDWIDTH;
+  for (ALL_LIST_ELEMENTS_RO (o->area_list, i, oa))
+      for (ALL_LIST_ELEMENTS_RO (oa->if_list, j, oi))
+          ospf6_interface_recalculate_cost (oi);
+
+  return CMD_SUCCESS;
+}
+
 DEFUN (ipv6_ospf6_hellointerval,
        ipv6_ospf6_hellointerval_cmd,
        "ipv6 ospf6 hello-interval <1-65535>",
@@ -1854,6 +1909,10 @@
 
   install_element (INTERFACE_NODE, &ipv6_ospf6_network_cmd);
   install_element (INTERFACE_NODE, &no_ipv6_ospf6_network_cmd);
+
+  /* reference bandwidth commands */
+  install_element (OSPF6_NODE, &auto_cost_reference_bandwidth_cmd);
+  install_element (OSPF6_NODE, &no_auto_cost_reference_bandwidth_cmd);
 }
 
 DEFUN (debug_ospf6_interface,
diff --git a/ospf6d/ospf6_snmp.c b/ospf6d/ospf6_snmp.c
index 4660392..4be8be0 100644
--- a/ospf6d/ospf6_snmp.c
+++ b/ospf6d/ospf6_snmp.c
@@ -488,7 +488,9 @@
     case OSPFv3DEMANDEXTENSIONS:
       return SNMP_INTEGER (0);	/* Not supported */
     case OSPFv3REFERENCEBANDWIDTH:
-      return SNMP_INTEGER (100000);
+      if (ospf6)
+        return SNMP_INTEGER (ospf6->ref_bandwidth);
+      /* Otherwise, like for "not implemented". */
     case OSPFv3RESTARTSUPPORT:
     case OSPFv3RESTARTINTERVAL:
     case OSPFv3RESTARTSTRICTLSACHECKING:
diff --git a/ospf6d/ospf6_top.c b/ospf6d/ospf6_top.c
index 7c0922a..7191270 100644
--- a/ospf6d/ospf6_top.c
+++ b/ospf6d/ospf6_top.c
@@ -148,6 +148,8 @@
 
   o->external_id_table = route_table_init ();
 
+  o->ref_bandwidth = OSPF6_REFERENCE_BANDWIDTH;
+
   return o;
 }
 
@@ -890,6 +892,10 @@
       vty_out(vty, "%s", VTY_NEWLINE);
     }
 
+  if (ospf6->ref_bandwidth != OSPF6_REFERENCE_BANDWIDTH)
+    vty_out (vty, " auto-cost reference-bandwidth %d%s", ospf6->ref_bandwidth / 1000,
+             VNL);
+
   ospf6_stub_router_config_write (vty);
   ospf6_redistribute_config_write (vty);
   ospf6_area_config_write (vty);
diff --git a/ospf6d/ospf6_top.h b/ospf6d/ospf6_top.h
index 866f92f..d6f4bf0 100644
--- a/ospf6d/ospf6_top.h
+++ b/ospf6d/ospf6_top.h
@@ -80,6 +80,8 @@
   struct thread *t_spf_calc;	        /* SPF calculation timer. */
   struct thread *t_ase_calc;		/* ASE calculation timer. */
   struct thread *maxage_remover;
+
+  u_int32_t ref_bandwidth;
 };
 
 #define OSPF6_DISABLED    0x01