From 3568a9cac73a2da19e86d82f561be10ae9dbe9a0 Mon Sep 17 00:00:00 2001 From: Rossella Sblendido Date: Wed, 25 Sep 2013 14:55:04 +0000 Subject: [PATCH] Add a route to reach the MD server when a subnet is created When the first subnet is created, the dhcp port is created and midonet plugin correctly adds the static route to reach the MD server in create_port. When a second or following subnets are created, a new ip is added to the dhcp port. This patch takes care of adding the static route to correcly reach the MD server in update_port. This fixes the problem of VMs not being able to reach the MD if assigned to the second subnet Closes-bug: #1231914 Change-Id: Ifc95f901d4222b76a4254e21295829ac8d82493b --- neutron/plugins/midonet/plugin.py | 30 ++++++++++++------- .../tests/unit/midonet/test_midonet_lib.py | 19 ++++++++++++ 2 files changed, 39 insertions(+), 10 deletions(-) diff --git a/neutron/plugins/midonet/plugin.py b/neutron/plugins/midonet/plugin.py index cd12ec5ce..196fa3936 100644 --- a/neutron/plugins/midonet/plugin.py +++ b/neutron/plugins/midonet/plugin.py @@ -625,20 +625,30 @@ class MidonetPluginV2(db_base_plugin_v2.NeutronDbPluginV2, old_port = self._get_port(context, id) net_id = old_port["network_id"] mac = old_port["mac_address"] - old_fixed_ips = old_port.get('fixed_ips') - + old_ips = old_port["fixed_ips"] # update the port DB p = super(MidonetPluginV2, self).update_port(context, id, port) - if "fixed_ips" in p: - # IPs have changed. Re-map the DHCP entries + new_ips = p["fixed_ips"] + if new_ips: bridge = self.client.get_bridge(net_id) - for cidr, ip, mac in self._dhcp_mappings( - context, old_fixed_ips, mac): - self.client.remove_dhcp_host(bridge, cidr, ip, mac) - for cidr, ip, mac in self._dhcp_mappings(context, - p["fixed_ips"], mac): - self.client.add_dhcp_host(bridge, cidr, ip, mac) + # If it's a DHCP port, add a route to reach the MD server + if _is_dhcp_port(p): + for cidr, ip in self._metadata_subnets( + context, new_ips): + self.client.add_dhcp_route_option( + bridge, cidr, ip, METADATA_DEFAULT_IP) + else: + # IPs have changed. Re-map the DHCP entries + for cidr, ip, mac in self._dhcp_mappings( + context, old_ips, mac): + self.client.remove_dhcp_host( + bridge, cidr, ip, mac) + + for cidr, ip, mac in self._dhcp_mappings( + context, new_ips, mac): + self.client.add_dhcp_host( + bridge, cidr, ip, mac) if (self._check_update_deletes_security_groups(port) or self._check_update_has_security_groups(port)): diff --git a/neutron/tests/unit/midonet/test_midonet_lib.py b/neutron/tests/unit/midonet/test_midonet_lib.py index 0fe95f99f..2b6889a1f 100644 --- a/neutron/tests/unit/midonet/test_midonet_lib.py +++ b/neutron/tests/unit/midonet/test_midonet_lib.py @@ -108,6 +108,25 @@ class MidoClientTestCase(testtools.TestCase): "2A:DB:6B:8C:19:99") bridge.assert_has_calls(calls, any_order=True) + def test_add_dhcp_route_option(self): + + bridge = mock.Mock() + subnet = bridge.get_dhcp_subnet.return_value + subnet.get_opt121_routes.return_value = None + dhcp_subnet_call = mock.call.get_dhcp_subnet("10.0.0.0_24") + dst_ip = "10.0.0.3/24" + gw_ip = "10.0.0.1" + prefix, length = dst_ip.split("/") + routes = [{'destinationPrefix': prefix, 'destinationLength': length, + 'gatewayAddr': gw_ip}] + opt121_routes_call = dhcp_subnet_call.opt121_routes(routes) + calls = [dhcp_subnet_call, opt121_routes_call, + opt121_routes_call.update()] + + self.client.add_dhcp_route_option(bridge, "10.0.0.0/24", + gw_ip, dst_ip) + bridge.assert_has_calls(calls, any_order=True) + def test_get_router_error(self): self.mock_api.get_router.side_effect = w_exc.HTTPInternalServerError() self.assertRaises(midonet_lib.MidonetApiException, -- 2.45.2