Browse Source

[4308] Subnet selection in v4 tweaked, added two debug messages.

Tomek Mrugalski 9 years ago
parent
commit
c33b682e3d

+ 10 - 0
src/lib/dhcpsrv/cfg_subnets4.cc

@@ -147,6 +147,10 @@ CfgSubnets4::selectSubnet(const SubnetSelector& selector) const {
         // selection criteria below.
         // selection criteria below.
         if (subnet) {
         if (subnet) {
             return (subnet);
             return (subnet);
+        } else {
+            // Let's try to get an address from the local interface and
+            // try to match it to defined subnet.
+            iface->getAddress4(address);
         }
         }
     }
     }
 
 
@@ -180,6 +184,9 @@ CfgSubnets4::selectSubnet(const std::string& iface,
 
 
         // Eliminate those subnets that do not meet client class criteria.
         // Eliminate those subnets that do not meet client class criteria.
         if ((*subnet)->clientSupported(client_classes)) {
         if ((*subnet)->clientSupported(client_classes)) {
+            LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE,
+                      DHCPSRV_CFGMGR_SUBNET4_IFACE)
+                .arg((*subnet)->toText()).arg(iface);
             return (*subnet);
             return (*subnet);
         }
         }
     }
     }
@@ -201,6 +208,9 @@ CfgSubnets4::selectSubnet(const IOAddress& address,
 
 
         // Eliminate those subnets that do not meet client class criteria.
         // Eliminate those subnets that do not meet client class criteria.
         if ((*subnet)->clientSupported(client_classes)) {
         if ((*subnet)->clientSupported(client_classes)) {
+            LOG_DEBUG(dhcpsrv_logger, DHCPSRV_DBG_TRACE, DHCPSRV_CFGMGR_SUBNET4_ADDR)
+                .arg((*subnet)->toText()).arg(address.toText());
+
             return (*subnet);
             return (*subnet);
         }
         }
     }
     }

+ 13 - 0
src/lib/dhcpsrv/dhcpsrv_messages.mes

@@ -107,6 +107,19 @@ This is a debug message reporting that the DHCP configuration manager has
 returned the specified IPv4 subnet, because detected relay agent address
 returned the specified IPv4 subnet, because detected relay agent address
 matches value specified for this subnet.
 matches value specified for this subnet.
 
 
+% DHCPSRV_CFGMGR_SUBNET4_ADDR selected subnet %1 for packet received by matching address %2
+This is a debug message reporting that the DHCP configuration manager
+has returned the specified IPv4 subnet for a packet received. This particular
+subnet was selected, because an IPv4 address was matched to belong to that
+subnet.
+
+% DHCPSRV_CFGMGR_SUBNET4_IFACE selected subnet %1 for packet received over interface %2
+This is a debug message reporting that the DHCP configuration manager
+has returned the specified IPv4 subnet for a packet received over
+given interface.  This particular subnet was selected, because it
+was specified as being directly reachable over given interface. (see
+'interface' parameter in the subnet4 definition).
+
 % DHCPSRV_CFGMGR_SUBNET6 retrieved subnet %1 for address hint %2
 % DHCPSRV_CFGMGR_SUBNET6 retrieved subnet %1 for address hint %2
 This is a debug message reporting that the DHCP configuration manager has
 This is a debug message reporting that the DHCP configuration manager has
 returned the specified IPv6 subnet when given the address hint specified
 returned the specified IPv6 subnet when given the address hint specified

+ 37 - 0
src/lib/dhcpsrv/tests/cfg_subnets4_unittest.cc

@@ -61,6 +61,43 @@ TEST(CfgSubnets4Test, selectSubnetByCiaddr) {
     EXPECT_FALSE(cfg.selectSubnet(selector));
     EXPECT_FALSE(cfg.selectSubnet(selector));
 }
 }
 
 
+// This test verifies that it is possible to select a subnet by
+// matching an interface name.
+TEST(CfgSubnets4Test, selectSubnetByIface) {
+    // The IfaceMgrTestConfig object initializes fake interfaces:
+    // eth0, eth1 and lo on the configuration manager. The CfgSubnets4
+    // object uses addresses assigned to these fake interfaces to
+    // select the appropriate subnet.
+    IfaceMgrTestConfig config(true);
+
+    CfgSubnets4 cfg;
+
+    // Create 3 subnets.
+    Subnet4Ptr subnet1(new Subnet4(IOAddress("192.0.2.0"), 26, 1, 2, 3));
+    Subnet4Ptr subnet2(new Subnet4(IOAddress("192.0.2.64"), 26, 1, 2, 3));
+    Subnet4Ptr subnet3(new Subnet4(IOAddress("192.0.2.128"), 26, 1, 2, 3));
+    // No interface defined for subnet1
+    subnet2->setIface("lo");
+    subnet3->setIface("eth1");
+
+    cfg.add(subnet1);
+    cfg.add(subnet2);
+    cfg.add(subnet3);
+
+    // Make sure that initially the subnets don't exist.
+    SubnetSelector selector;
+    // Set an interface to a name that is not defined in the config.
+    // Subnet selection should fail.
+    selector.iface_name_ = "eth0";
+    ASSERT_FALSE(cfg.selectSubnet(selector));
+
+    // Now select an interface name that matches. Selection should succeed
+    // and return subnet3.
+    selector.iface_name_ = "eth1";
+    Subnet4Ptr selected = cfg.selectSubnet(selector);
+    ASSERT_TRUE(selected);
+    EXPECT_EQ(subnet3, selected);
+}
 
 
 // This test verifies that when the classification information is specified for
 // This test verifies that when the classification information is specified for
 // subnets, the proper subnets are returned by the subnet configuration.
 // subnets, the proper subnets are returned by the subnet configuration.