From 6b61a6be4a14444723e3728fb0fcdd77bac8fe74 Mon Sep 17 00:00:00 2001 From: Keisuke Tagami Date: Mon, 5 Sep 2011 14:45:41 +0900 Subject: Fix bug #835919 that output a option file for dnsmasq not to offer a default gateway on second vif. --- nova/db/sqlalchemy/api.py | 15 +++++++++++++ nova/network/linux_net.py | 55 +++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 2 deletions(-) mode change 100644 => 100755 nova/db/sqlalchemy/api.py mode change 100644 => 100755 nova/network/linux_net.py diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py old mode 100644 new mode 100755 index b99667afc..aee1c2a55 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -1415,6 +1415,21 @@ def instance_get_all_by_reservation(context, reservation_id): filter_by(project_id=context.project_id).\ filter_by(deleted=False).\ all() + +@require_admin_context +def instance_get_all_by_network(context, network_id): + session = get_session() + return session.query(models.Instance).\ + options(joinedload_all('fixed_ips.floating_ips')).\ + options(joinedload('virtual_interfaces')).\ + options(joinedload('security_groups')).\ + options(joinedload_all('fixed_ips.network')).\ + options(joinedload('metadata')).\ + options(joinedload('instance_type')).\ + filter_by(deleted=can_read_deleted(context)).\ + filter_by(network_id=network_id).\ + all() + @require_context diff --git a/nova/network/linux_net.py b/nova/network/linux_net.py old mode 100644 new mode 100755 index 57c1d0c28..8d5a0bbbd --- a/nova/network/linux_net.py +++ b/nova/network/linux_net.py @@ -511,6 +511,35 @@ def get_dhcp_hosts(context, network_ref): return '\n'.join(hosts) +def get_dhcp_opts(context, network_ref): + """Get network's hosts config in dhcp-opts format.""" + # create a decision dictionary for default gateway for eache instance + default_gateway_network_node = dict() + network_id = network_ref['id'] + instance_refs = db.instance_get_all_by_network(context, network_id) + ips_ref = db.network_get_associated_fixed_ips(context, network_id) + + for instance_ref in instance_refs: + instance_id = instance_ref['id'] + # nic number is decided by from this function in xxx function + vifs = db.virtual_interface_get_by_instance(context, instance_id) + if not vifs: + continue + # offer a default gateway to the first virtual interface of instance + first_vif = vifs[0] + default_gateway_network_node[instance_id] = first_vif['network_id'] + + for fixed_ip_ref in ips_ref: + instance_id = fixed_ip_ref['instance_id'] + target_network_id = default_gateway_network_node[instance_id] + if target_network_id == fixed_ip_ref['network_id']: + hosts.append(_host_dhcp_opts(fixed_ip_ref, gw=True)) + else: + hosts.append(_host_dhcp_opts(fixed_ip_ref, gw=None)) + + return '\n'.join(hosts) + + # NOTE(ja): Sending a HUP only reloads the hostfile, so any # configuration options (like dchp-range, vlan, ...) # aren't reloaded. @@ -526,8 +555,13 @@ def update_dhcp(context, dev, network_ref): with open(conffile, 'w') as f: f.write(get_dhcp_hosts(context, network_ref)) + optsfile = _dhcp_file(dev, 'opts') + with open(optsfile, 'w') as f: + f.write(get_dhcp_opts(context, network_ref)) + # Make sure dnsmasq can actually read it (it setuid()s to "nobody") os.chmod(conffile, 0644) + os.chmod(optsfile, 0644) pid = _dnsmasq_pid_for(dev) @@ -559,6 +593,7 @@ def update_dhcp(context, dev, network_ref): '--dhcp-lease-max=%s' % len(netaddr.IPNetwork(network_ref['cidr'])), '--dhcp-hostsfile=%s' % _dhcp_file(dev, 'conf'), '--dhcp-script=%s' % FLAGS.dhcpbridge, + '--dhcp-optsfile=%s' % _dhcp_file(dev, 'opts'), '--leasefile-ro'] if FLAGS.dns_server: cmd += ['-h', '-R', '--server=%s' % FLAGS.dns_server] @@ -625,13 +660,29 @@ def _host_lease(fixed_ip_ref): instance_ref['hostname'] or '*') +def _host_dhcp_network(fixed_ip_ref): + instance_ref = fixed_ip_ref['instance'] + return 'NW-i%08d-%s' % (instance_ref['id'], + fixed_ip_ref['network_id']) + + def _host_dhcp(fixed_ip_ref): """Return a host string for an address in dhcp-host format.""" instance_ref = fixed_ip_ref['instance'] - return '%s,%s.%s,%s' % (fixed_ip_ref['virtual_interface']['address'], + return '%s,%s.%s,%s,%s' % (fixed_ip_ref['virtual_interface']['address'], instance_ref['hostname'], FLAGS.dhcp_domain, - fixed_ip_ref['address']) + fixed_ip_ref['address'], + "net:" + _host_dhcp_network(fixed_ip_ref)) + + +def _host_dhcp_opts(fixed_ip_ref, gw): + """Return a host string for an address in dhcp-host format.""" + if not gw: + return '%s,%s' % (_host_dhcp_network(fixed_ip_ref), + 3) + else: + return '' def _execute(*cmd, **kwargs): -- cgit From d6f1a31d56de84398246498d0f2676d9741cdccf Mon Sep 17 00:00:00 2001 From: Keisuke Tagami Date: Mon, 5 Sep 2011 20:23:28 +0900 Subject: implement unit test for linux_net --- nova/db/api.py | 5 + nova/network/linux_net.py | 17 ++-- nova/tests/test_linux_net.py | 232 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 247 insertions(+), 7 deletions(-) mode change 100644 => 100755 nova/db/api.py create mode 100755 nova/tests/test_linux_net.py diff --git a/nova/db/api.py b/nova/db/api.py old mode 100644 new mode 100755 index 148887635..85e9c7669 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -532,6 +532,11 @@ def instance_get_all_by_reservation(context, reservation_id): return IMPL.instance_get_all_by_reservation(context, reservation_id) +def instance_get_all_by_network(context, network_id): + """Get all instances belonging to a network.""" + return IMPL.instance_get_all_by_network(context, network_id) + + def instance_get_by_fixed_ip(context, address): """Get an instance for a fixed ip by address.""" return IMPL.instance_get_by_fixed_ip(context, address) diff --git a/nova/network/linux_net.py b/nova/network/linux_net.py index 8d5a0bbbd..50d9ccf19 100755 --- a/nova/network/linux_net.py +++ b/nova/network/linux_net.py @@ -514,6 +514,7 @@ def get_dhcp_hosts(context, network_ref): def get_dhcp_opts(context, network_ref): """Get network's hosts config in dhcp-opts format.""" # create a decision dictionary for default gateway for eache instance + hosts = [] default_gateway_network_node = dict() network_id = network_ref['id'] instance_refs = db.instance_get_all_by_network(context, network_id) @@ -521,9 +522,10 @@ def get_dhcp_opts(context, network_ref): for instance_ref in instance_refs: instance_id = instance_ref['id'] - # nic number is decided by from this function in xxx function + # nic number is decided by xxx from this function in xxx function vifs = db.virtual_interface_get_by_instance(context, instance_id) - if not vifs: + if vifs == None: + default_gateway_network_node[instance_id] = None continue # offer a default gateway to the first virtual interface of instance first_vif = vifs[0] @@ -531,11 +533,12 @@ def get_dhcp_opts(context, network_ref): for fixed_ip_ref in ips_ref: instance_id = fixed_ip_ref['instance_id'] - target_network_id = default_gateway_network_node[instance_id] - if target_network_id == fixed_ip_ref['network_id']: - hosts.append(_host_dhcp_opts(fixed_ip_ref, gw=True)) - else: - hosts.append(_host_dhcp_opts(fixed_ip_ref, gw=None)) + if default_gateway_network_node.has_key(instance_id): + target_network_id = default_gateway_network_node[instance_id] + if target_network_id == fixed_ip_ref['network_id']: + hosts.append(_host_dhcp_opts(fixed_ip_ref, gw=True)) + else: + hosts.append(_host_dhcp_opts(fixed_ip_ref, gw=None)) return '\n'.join(hosts) diff --git a/nova/tests/test_linux_net.py b/nova/tests/test_linux_net.py new file mode 100755 index 000000000..3c3cdd0d9 --- /dev/null +++ b/nova/tests/test_linux_net.py @@ -0,0 +1,232 @@ +# vim: tabstop=4 shiftwidth=4 softtabstop=4 + +# Copyright 2011 NTT +# All Rights Reserved. +# +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. + +from nova import context +from nova import db +from nova import exception +from nova import log as logging +from nova import test +from nova import utils +from nova import flags +from nova.network import manager as network_manager + +import mox + +FLAGS = flags.FLAGS + +LOG = logging.getLogger('nova.tests.network') + + +HOST = "testhost" + +instances = [{'id': 0, + 'host': 'fake_instance00', + 'hostname': 'fake_instance00'}, + {'id': 1, + 'host': 'fake_instance01', + 'hostname': 'fake_instance01'}] + + +addresses = [{"address" : "10.0.0.1" }, + {"address" : "10.0.0.2" }, + {"address" : "10.0.0.3" }, + {"address" : "10.0.0.4" }] + + +networks = [{'id': 0, + 'uuid': "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", + 'label': 'test0', + 'injected': False, + 'multi_host': False, + 'cidr': '192.168.0.0/24', + 'cidr_v6': '2001:db8::/64', + 'gateway_v6': '2001:db8::1', + 'netmask_v6': '64', + 'netmask': '255.255.255.0', + 'bridge': 'fa0', + 'bridge_interface': 'fake_fa0', + 'gateway': '192.168.0.1', + 'broadcast': '192.168.0.255', + 'dns1': '192.168.0.1', + 'dns2': '192.168.0.2', + 'dhcp_server' : '0.0.0.0', + 'dhcp_start' : '192.168.100.1', + 'vlan': None, + 'host': None, + 'project_id': 'fake_project', + 'vpn_public_address': '192.168.0.2'}, + {'id': 1, + 'uuid': "bbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb", + 'label': 'test1', + 'injected': False, + 'multi_host': False, + 'cidr': '192.168.1.0/24', + 'cidr_v6': '2001:db9::/64', + 'gateway_v6': '2001:db9::1', + 'netmask_v6': '64', + 'netmask': '255.255.255.0', + 'bridge': 'fa1', + 'bridge_interface': 'fake_fa1', + 'gateway': '192.168.1.1', + 'broadcast': '192.168.1.255', + 'dns1': '192.168.0.1', + 'dns2': '192.168.0.2', + 'dhcp_server' : '0.0.0.0', + 'dhcp_start' : '192.168.100.1', + 'vlan': None, + 'host': None, + 'project_id': 'fake_project', + 'vpn_public_address': '192.168.1.2'}] + + +fixed_ips = [{'id': 0, + 'network_id': 0, + 'address': '192.168.0.100', + 'instance_id': 0, + 'allocated': True, + 'virtual_interface_id': 0, + 'virtual_interface' : addresses[0], + 'instance': instances[0], + 'floating_ips': []}, + {'id': 1, + 'network_id': 1, + 'address': '192.168.1.100', + 'instance_id': 0, + 'allocated': True, + 'virtual_interface_id': 1, + 'virtual_interface' : addresses[1], + 'instance': instances[0], + 'floating_ips': []}, + {'id': 2, + 'network_id': 0, + 'address': '192.168.0.101', + 'instance_id': 1, + 'allocated': True, + 'virtual_interface_id': 2, + 'virtual_interface' : addresses[2], + 'instance': instances[1], + 'floating_ips': []}, + {'id': 3, + 'network_id': 1, + 'address': '192.168.1.101', + 'instance_id': 1, + 'allocated': True, + 'virtual_interface_id': 3, + 'virtual_interface' : addresses[3], + 'instance': instances[1], + 'floating_ips': []}] + + + + +vifs = [{'id': 0, + 'address': 'DE:AD:BE:EF:00:00', + 'uuid': '00000000-0000-0000-0000-0000000000000000', + 'network_id': 0, + 'network': networks[0], + 'instance_id': 0}, + {'id': 1, + 'address': 'DE:AD:BE:EF:00:01', + 'uuid': '00000000-0000-0000-0000-0000000000000001', + 'network_id': 1, + 'network': networks[1], + 'instance_id': 0}, + {'id': 2, + 'address': 'DE:AD:BE:EF:00:02', + 'uuid': '00000000-0000-0000-0000-0000000000000002', + 'network_id': 1, + 'network': networks[1], + 'instance_id': 1}, + {'id': 3, + 'address': 'DE:AD:BE:EF:00:03', + 'uuid': '00000000-0000-0000-0000-0000000000000003', + 'network_id': 0, + 'network': networks[0], + 'instance_id': 1}] + + +class LinuxNetworkTestCase(test.TestCase): + + def setUp(self): + super(LinuxNetworkTestCase, self).setUp() + network_driver = FLAGS.network_driver + self.driver = utils.import_object(network_driver) + self.driver.db = db + + def test_update_dhcp(self): + self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') + self.mox.StubOutWithMock(db, 'instance_get_all_by_network') + self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') + + fixed_ips[1]['instance'] = instances[0] + db.network_get_associated_fixed_ips(mox.IgnoreArg(), + mox.IgnoreArg()).AndReturn(fixed_ips) + + db.instance_get_all_by_network(mox.IgnoreArg(), + mox.IgnoreArg()).AndReturn(instances) + db.network_get_associated_fixed_ips(mox.IgnoreArg(), + mox.IgnoreArg()).AndReturn(fixed_ips) + db.virtual_interface_get_by_instance(mox.IgnoreArg(), + mox.IgnoreArg()).AndReturn([vifs[0],vifs[1]]) + db.virtual_interface_get_by_instance(mox.IgnoreArg(), + mox.IgnoreArg()).AndReturn([vifs[2],vifs[3]]) + + self.mox.ReplayAll() + self.driver.update_dhcp(None, "eth0", networks[0]) + + + def test_get_dhcp_hosts(self): + self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') + + fixed_ips[1]['instance'] = instances[0] + db.network_get_associated_fixed_ips(mox.IgnoreArg(), + mox.IgnoreArg()).AndReturn(fixed_ips) + + self.mox.ReplayAll() + + hosts = self.driver.get_dhcp_hosts(None, networks[0]) + + self.assertEquals(hosts, + "10.0.0.1,fake_instance00.novalocal,192.168.0.100,net:NW-i00000000-0\n" \ + "10.0.0.2,fake_instance00.novalocal,192.168.1.100,net:NW-i00000000-1\n" \ + "10.0.0.3,fake_instance01.novalocal,192.168.0.101,net:NW-i00000001-0\n" \ + "10.0.0.4,fake_instance01.novalocal,192.168.1.101,net:NW-i00000001-1") + + + def test_get_dhcp_opts(self): + self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') + self.mox.StubOutWithMock(db, 'instance_get_all_by_network') + self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') + + fixed_ips[1]['instance'] = instances[0] + + db.instance_get_all_by_network(mox.IgnoreArg(), + mox.IgnoreArg()).AndReturn(instances) + db.network_get_associated_fixed_ips(mox.IgnoreArg(), + mox.IgnoreArg()).AndReturn(fixed_ips) + db.virtual_interface_get_by_instance(mox.IgnoreArg(), + mox.IgnoreArg()).AndReturn([vifs[0],vifs[1]]) + db.virtual_interface_get_by_instance(mox.IgnoreArg(), + mox.IgnoreArg()).AndReturn([vifs[2],vifs[3]]) + + self.mox.ReplayAll() + + opts = self.driver.get_dhcp_opts(None, networks[0]) + self.assertEquals(opts, '\nNW-i00000000-1,3\nNW-i00000001-0,3\n') + + + -- cgit From 418923c385a34265dd33a0c2ada3aa97a5749a06 Mon Sep 17 00:00:00 2001 From: Keisuke Tagami Date: Mon, 5 Sep 2011 21:13:00 +0900 Subject: format for pep8 --- nova/db/api.py | 4 +- nova/db/sqlalchemy/api.py | 5 +- nova/network/linux_net.py | 6 +- nova/tests/test_linux_net.py | 177 ++++++++++++++++++++++++++++++++----------- 4 files changed, 141 insertions(+), 51 deletions(-) diff --git a/nova/db/api.py b/nova/db/api.py index 85e9c7669..faac13966 100755 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -535,8 +535,8 @@ def instance_get_all_by_reservation(context, reservation_id): def instance_get_all_by_network(context, network_id): """Get all instances belonging to a network.""" return IMPL.instance_get_all_by_network(context, network_id) - - + + def instance_get_by_fixed_ip(context, address): """Get an instance for a fixed ip by address.""" return IMPL.instance_get_by_fixed_ip(context, address) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index aee1c2a55..3194a5231 100755 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -1415,8 +1415,8 @@ def instance_get_all_by_reservation(context, reservation_id): filter_by(project_id=context.project_id).\ filter_by(deleted=False).\ all() - -@require_admin_context + + def instance_get_all_by_network(context, network_id): session = get_session() return session.query(models.Instance).\ @@ -1431,7 +1431,6 @@ def instance_get_all_by_network(context, network_id): all() - @require_context def instance_get_by_fixed_ip(context, address): """Return instance ref by exact match of FixedIP""" diff --git a/nova/network/linux_net.py b/nova/network/linux_net.py index 50d9ccf19..4ebc3df23 100755 --- a/nova/network/linux_net.py +++ b/nova/network/linux_net.py @@ -438,7 +438,7 @@ def ensure_vpn_forward(public_ip, port, private_ip): def ensure_floating_forward(floating_ip, fixed_ip): """Ensure floating ip forwarding rule.""" - for chain, rule in floating_forward_rules(floating_ip, fixed_ip): + for chain, rule in floating_forward_rules(floaing_ip, fixed_ip): iptables_manager.ipv4['nat'].add_rule(chain, rule) iptables_manager.apply() @@ -518,7 +518,7 @@ def get_dhcp_opts(context, network_ref): default_gateway_network_node = dict() network_id = network_ref['id'] instance_refs = db.instance_get_all_by_network(context, network_id) - ips_ref = db.network_get_associated_fixed_ips(context, network_id) + ips_ref = db.network_get_associated_fixed_ips(context, network_id) for instance_ref in instance_refs: instance_id = instance_ref['id'] @@ -533,7 +533,7 @@ def get_dhcp_opts(context, network_ref): for fixed_ip_ref in ips_ref: instance_id = fixed_ip_ref['instance_id'] - if default_gateway_network_node.has_key(instance_id): + if instance_id in default_gateway_network_node: target_network_id = default_gateway_network_node[instance_id] if target_network_id == fixed_ip_ref['network_id']: hosts.append(_host_dhcp_opts(fixed_ip_ref, gw=True)) diff --git a/nova/tests/test_linux_net.py b/nova/tests/test_linux_net.py index 3c3cdd0d9..94c53817b 100755 --- a/nova/tests/test_linux_net.py +++ b/nova/tests/test_linux_net.py @@ -41,10 +41,10 @@ instances = [{'id': 0, 'hostname': 'fake_instance01'}] -addresses = [{"address" : "10.0.0.1" }, - {"address" : "10.0.0.2" }, - {"address" : "10.0.0.3" }, - {"address" : "10.0.0.4" }] +addresses = [{"address": "10.0.0.1"}, + {"address": "10.0.0.2"}, + {"address": "10.0.0.3"}, + {"address": "10.0.0.4"}] networks = [{'id': 0, @@ -63,8 +63,8 @@ networks = [{'id': 0, 'broadcast': '192.168.0.255', 'dns1': '192.168.0.1', 'dns2': '192.168.0.2', - 'dhcp_server' : '0.0.0.0', - 'dhcp_start' : '192.168.100.1', + 'dhcp_server': '0.0.0.0', + 'dhcp_start': '192.168.100.1', 'vlan': None, 'host': None, 'project_id': 'fake_project', @@ -85,8 +85,8 @@ networks = [{'id': 0, 'broadcast': '192.168.1.255', 'dns1': '192.168.0.1', 'dns2': '192.168.0.2', - 'dhcp_server' : '0.0.0.0', - 'dhcp_start' : '192.168.100.1', + 'dhcp_server': '0.0.0.0', + 'dhcp_start': '192.168.100.1', 'vlan': None, 'host': None, 'project_id': 'fake_project', @@ -99,7 +99,7 @@ fixed_ips = [{'id': 0, 'instance_id': 0, 'allocated': True, 'virtual_interface_id': 0, - 'virtual_interface' : addresses[0], + 'virtual_interface': addresses[0], 'instance': instances[0], 'floating_ips': []}, {'id': 1, @@ -108,7 +108,7 @@ fixed_ips = [{'id': 0, 'instance_id': 0, 'allocated': True, 'virtual_interface_id': 1, - 'virtual_interface' : addresses[1], + 'virtual_interface': addresses[1], 'instance': instances[0], 'floating_ips': []}, {'id': 2, @@ -117,7 +117,7 @@ fixed_ips = [{'id': 0, 'instance_id': 1, 'allocated': True, 'virtual_interface_id': 2, - 'virtual_interface' : addresses[2], + 'virtual_interface': addresses[2], 'instance': instances[1], 'floating_ips': []}, {'id': 3, @@ -126,13 +126,11 @@ fixed_ips = [{'id': 0, 'instance_id': 1, 'allocated': True, 'virtual_interface_id': 3, - 'virtual_interface' : addresses[3], + 'virtual_interface': addresses[3], 'instance': instances[1], 'floating_ips': []}] - - vifs = [{'id': 0, 'address': 'DE:AD:BE:EF:00:00', 'uuid': '00000000-0000-0000-0000-0000000000000000', @@ -167,66 +165,159 @@ class LinuxNetworkTestCase(test.TestCase): self.driver = utils.import_object(network_driver) self.driver.db = db - def test_update_dhcp(self): + def test_update_dhcp_for_nw00(self): self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') self.mox.StubOutWithMock(db, 'instance_get_all_by_network') self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') - fixed_ips[1]['instance'] = instances[0] db.network_get_associated_fixed_ips(mox.IgnoreArg(), - mox.IgnoreArg()).AndReturn(fixed_ips) + mox.IgnoreArg())\ + .AndReturn([fixed_ips[0], + fixed_ips[3]]) db.instance_get_all_by_network(mox.IgnoreArg(), - mox.IgnoreArg()).AndReturn(instances) + mox.IgnoreArg())\ + .AndReturn(instances) db.network_get_associated_fixed_ips(mox.IgnoreArg(), - mox.IgnoreArg()).AndReturn(fixed_ips) + mox.IgnoreArg())\ + .AndReturn([fixed_ips[0], + fixed_ips[3]]) db.virtual_interface_get_by_instance(mox.IgnoreArg(), - mox.IgnoreArg()).AndReturn([vifs[0],vifs[1]]) + mox.IgnoreArg())\ + .AndReturn([vifs[0], vifs[1]]) db.virtual_interface_get_by_instance(mox.IgnoreArg(), - mox.IgnoreArg()).AndReturn([vifs[2],vifs[3]]) - + mox.IgnoreArg())\ + .AndReturn([vifs[2], vifs[3]]) self.mox.ReplayAll() + self.driver.update_dhcp(None, "eth0", networks[0]) + def test_update_dhcp_for_nw01(self): + self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') + self.mox.StubOutWithMock(db, 'instance_get_all_by_network') + self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') + + db.network_get_associated_fixed_ips(mox.IgnoreArg(), + mox.IgnoreArg())\ + .AndReturn([fixed_ips[1], + fixed_ips[2]]) - def test_get_dhcp_hosts(self): + db.instance_get_all_by_network(mox.IgnoreArg(), + mox.IgnoreArg())\ + .AndReturn(instances) + db.network_get_associated_fixed_ips(mox.IgnoreArg(), + mox.IgnoreArg())\ + .AndReturn([fixed_ips[1], + fixed_ips[2]]) + db.virtual_interface_get_by_instance(mox.IgnoreArg(), + mox.IgnoreArg())\ + .AndReturn([vifs[0], vifs[1]]) + db.virtual_interface_get_by_instance(mox.IgnoreArg(), + mox.IgnoreArg())\ + .AndReturn([vifs[2], vifs[3]]) + self.mox.ReplayAll() + + self.driver.update_dhcp(None, "eth0", networks[0]) + + def test_get_dhcp_hosts_for_nw00(self): self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') - fixed_ips[1]['instance'] = instances[0] db.network_get_associated_fixed_ips(mox.IgnoreArg(), - mox.IgnoreArg()).AndReturn(fixed_ips) + mox.IgnoreArg())\ + .AndReturn([fixed_ips[0], + fixed_ips[3]]) + self.mox.ReplayAll() + + expected = \ + "10.0.0.1,fake_instance00.novalocal,"\ + "192.168.0.100,net:NW-i00000000-0\n"\ + "10.0.0.4,fake_instance01.novalocal,"\ + "192.168.1.101,net:NW-i00000001-1" + actual_hosts = self.driver.get_dhcp_hosts(None, networks[1]) + self.assertEquals(actual_hosts, expected) + + def test_get_dhcp_hosts_for_nw01(self): + self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') + + db.network_get_associated_fixed_ips(mox.IgnoreArg(), + mox.IgnoreArg())\ + .AndReturn([fixed_ips[1], + fixed_ips[2]]) self.mox.ReplayAll() - hosts = self.driver.get_dhcp_hosts(None, networks[0]) - - self.assertEquals(hosts, - "10.0.0.1,fake_instance00.novalocal,192.168.0.100,net:NW-i00000000-0\n" \ - "10.0.0.2,fake_instance00.novalocal,192.168.1.100,net:NW-i00000000-1\n" \ - "10.0.0.3,fake_instance01.novalocal,192.168.0.101,net:NW-i00000001-0\n" \ - "10.0.0.4,fake_instance01.novalocal,192.168.1.101,net:NW-i00000001-1") + expected = \ + "10.0.0.2,fake_instance00.novalocal,"\ + "192.168.1.100,net:NW-i00000000-1\n"\ + "10.0.0.3,fake_instance01.novalocal,"\ + "192.168.0.101,net:NW-i00000001-0" + actual_hosts = self.driver.get_dhcp_hosts(None, networks[0]) + + self.assertEquals(actual_hosts, expected) - - def test_get_dhcp_opts(self): + def test_get_dhcp_opts_for_nw00(self): self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') self.mox.StubOutWithMock(db, 'instance_get_all_by_network') self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') - fixed_ips[1]['instance'] = instances[0] - db.instance_get_all_by_network(mox.IgnoreArg(), - mox.IgnoreArg()).AndReturn(instances) + mox.IgnoreArg())\ + .AndReturn(instances) db.network_get_associated_fixed_ips(mox.IgnoreArg(), - mox.IgnoreArg()).AndReturn(fixed_ips) + mox.IgnoreArg())\ + .AndReturn([fixed_ips[0], + fixed_ips[3]]) db.virtual_interface_get_by_instance(mox.IgnoreArg(), - mox.IgnoreArg()).AndReturn([vifs[0],vifs[1]]) + mox.IgnoreArg())\ + .AndReturn([vifs[0], + vifs[1]]) db.virtual_interface_get_by_instance(mox.IgnoreArg(), - mox.IgnoreArg()).AndReturn([vifs[2],vifs[3]]) + mox.IgnoreArg())\ + .AndReturn([vifs[2], + vifs[3]]) + self.mox.ReplayAll() + + expected_opts = '\n'\ + '' + actual_opts = self.driver.get_dhcp_opts(None, networks[0]) + self.assertEquals(actual_opts, expected_opts) + + def test_get_dhcp_opts_for_nw01(self): + self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') + self.mox.StubOutWithMock(db, 'instance_get_all_by_network') + self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') + + db.instance_get_all_by_network(mox.IgnoreArg(), + mox.IgnoreArg())\ + .AndReturn(instances) + db.network_get_associated_fixed_ips(mox.IgnoreArg(), + mox.IgnoreArg())\ + .AndReturn([fixed_ips[1], + fixed_ips[2]]) + db.virtual_interface_get_by_instance(mox.IgnoreArg(), + mox.IgnoreArg())\ + .AndReturn([vifs[0], + vifs[1]]) + db.virtual_interface_get_by_instance(mox.IgnoreArg(), + mox.IgnoreArg())\ + .AndReturn([vifs[2], + vifs[3]]) self.mox.ReplayAll() - opts = self.driver.get_dhcp_opts(None, networks[0]) - self.assertEquals(opts, '\nNW-i00000000-1,3\nNW-i00000001-0,3\n') + expected_opts = 'NW-i00000000-1,3\n'\ + 'NW-i00000001-0,3' + actual_opts = self.driver.get_dhcp_opts(None, networks[1]) + + self.assertEquals(actual_opts, expected_opts) + + def test_dhcp_opts_default_gateway_network(self): + expected = "" + actual = self.driver._host_dhcp_opts(fixed_ips[0], True) + self.assertEquals(actual, expected) - + def test_dhcp_opts_not_default_gateway_network(self): + expected = "NW-i00000000-0,3" + actual = self.driver._host_dhcp_opts(fixed_ips[0], False) + self.assertEquals(actual, expected) -- cgit From 14314701bac27ce0e913f8e0587c354819a4bd9e Mon Sep 17 00:00:00 2001 From: Keisuke Tagami Date: Mon, 5 Sep 2011 21:32:52 +0900 Subject: format for pep8 --- nova/tests/test_linux_net.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nova/tests/test_linux_net.py b/nova/tests/test_linux_net.py index 94c53817b..93d9b02c9 100755 --- a/nova/tests/test_linux_net.py +++ b/nova/tests/test_linux_net.py @@ -316,7 +316,6 @@ class LinuxNetworkTestCase(test.TestCase): actual = self.driver._host_dhcp_opts(fixed_ips[0], True) self.assertEquals(actual, expected) - def test_dhcp_opts_not_default_gateway_network(self): expected = "NW-i00000000-0,3" actual = self.driver._host_dhcp_opts(fixed_ips[0], False) -- cgit From b9ef3dcaa241088e205e170ca0a236afb0ea34dd Mon Sep 17 00:00:00 2001 From: Keisuke Tagami Date: Tue, 6 Sep 2011 10:10:25 +0900 Subject: added me to Authors --- Authors | 1 + 1 file changed, 1 insertion(+) diff --git a/Authors b/Authors index b9e7a7d23..f775c6f0c 100644 --- a/Authors +++ b/Authors @@ -60,6 +60,7 @@ Joshua McKenty Justin Santa Barbara Justin Shepherd Kei Masumoto +Keisuke Tagami masumoto Ken Pepple Kevin Bringard -- cgit From b922e08a6c15eeaab1f7ec342c00673b610d0e76 Mon Sep 17 00:00:00 2001 From: Keisuke Tagami Date: Tue, 6 Sep 2011 10:14:27 +0900 Subject: correct a method to collect instances from db add interface data to test --- nova/db/api.py | 5 --- nova/db/sqlalchemy/api.py | 14 --------- nova/network/linux_net.py | 8 +++-- nova/tests/test_linux_net.py | 74 +++++++++++++++++++++++++++++--------------- 4 files changed, 54 insertions(+), 47 deletions(-) diff --git a/nova/db/api.py b/nova/db/api.py index faac13966..148887635 100755 --- a/nova/db/api.py +++ b/nova/db/api.py @@ -532,11 +532,6 @@ def instance_get_all_by_reservation(context, reservation_id): return IMPL.instance_get_all_by_reservation(context, reservation_id) -def instance_get_all_by_network(context, network_id): - """Get all instances belonging to a network.""" - return IMPL.instance_get_all_by_network(context, network_id) - - def instance_get_by_fixed_ip(context, address): """Get an instance for a fixed ip by address.""" return IMPL.instance_get_by_fixed_ip(context, address) diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py index 3194a5231..b99667afc 100755 --- a/nova/db/sqlalchemy/api.py +++ b/nova/db/sqlalchemy/api.py @@ -1417,20 +1417,6 @@ def instance_get_all_by_reservation(context, reservation_id): all() -def instance_get_all_by_network(context, network_id): - session = get_session() - return session.query(models.Instance).\ - options(joinedload_all('fixed_ips.floating_ips')).\ - options(joinedload('virtual_interfaces')).\ - options(joinedload('security_groups')).\ - options(joinedload_all('fixed_ips.network')).\ - options(joinedload('metadata')).\ - options(joinedload('instance_type')).\ - filter_by(deleted=can_read_deleted(context)).\ - filter_by(network_id=network_id).\ - all() - - @require_context def instance_get_by_fixed_ip(context, address): """Return instance ref by exact match of FixedIP""" diff --git a/nova/network/linux_net.py b/nova/network/linux_net.py index 4ebc3df23..c26e3574e 100755 --- a/nova/network/linux_net.py +++ b/nova/network/linux_net.py @@ -517,11 +517,13 @@ def get_dhcp_opts(context, network_ref): hosts = [] default_gateway_network_node = dict() network_id = network_ref['id'] - instance_refs = db.instance_get_all_by_network(context, network_id) + instance_set = set() ips_ref = db.network_get_associated_fixed_ips(context, network_id) - for instance_ref in instance_refs: - instance_id = instance_ref['id'] + for fixed_ip_ref in ips_ref: + instance_set.add(fixed_ip_ref['instance_id']) + + for instance_id in instance_set: # nic number is decided by xxx from this function in xxx function vifs = db.virtual_interface_get_by_instance(context, instance_id) if vifs == None: diff --git a/nova/tests/test_linux_net.py b/nova/tests/test_linux_net.py index 93d9b02c9..3f44fbd05 100755 --- a/nova/tests/test_linux_net.py +++ b/nova/tests/test_linux_net.py @@ -18,10 +18,10 @@ from nova import context from nova import db from nova import exception +from nova import flags from nova import log as logging from nova import test from nova import utils -from nova import flags from nova.network import manager as network_manager import mox @@ -44,7 +44,9 @@ instances = [{'id': 0, addresses = [{"address": "10.0.0.1"}, {"address": "10.0.0.2"}, {"address": "10.0.0.3"}, - {"address": "10.0.0.4"}] + {"address": "10.0.0.4"}, + {"address": "10.0.0.5"}, + {"address": "10.0.0.6"}] networks = [{'id': 0, @@ -128,6 +130,24 @@ fixed_ips = [{'id': 0, 'virtual_interface_id': 3, 'virtual_interface': addresses[3], 'instance': instances[1], + 'floating_ips': []}, + {'id': 4, + 'network_id': 0, + 'address': '192.168.0.102', + 'instance_id': 0, + 'allocated': True, + 'virtual_interface_id': 4, + 'virtual_interface': addresses[4], + 'instance': instances[0], + 'floating_ips': []}, + {'id': 5, + 'network_id': 1, + 'address': '192.168.1.102', + 'instance_id': 1, + 'allocated': True, + 'virtual_interface_id': 5, + 'virtual_interface': addresses[5], + 'instance': instances[1], 'floating_ips': []}] @@ -154,6 +174,18 @@ vifs = [{'id': 0, 'uuid': '00000000-0000-0000-0000-0000000000000003', 'network_id': 0, 'network': networks[0], + 'instance_id': 1}, + {'id': 4, + 'address': 'DE:AD:BE:EF:00:04', + 'uuid': '00000000-0000-0000-0000-0000000000000004', + 'network_id': 0, + 'network': networks[0], + 'instance_id': 0}, + {'id': 5, + 'address': 'DE:AD:BE:EF:00:05', + 'uuid': '00000000-0000-0000-0000-0000000000000005', + 'network_id': 1, + 'network': networks[1], 'instance_id': 1}] @@ -167,7 +199,6 @@ class LinuxNetworkTestCase(test.TestCase): def test_update_dhcp_for_nw00(self): self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') - self.mox.StubOutWithMock(db, 'instance_get_all_by_network') self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') db.network_get_associated_fixed_ips(mox.IgnoreArg(), @@ -175,9 +206,6 @@ class LinuxNetworkTestCase(test.TestCase): .AndReturn([fixed_ips[0], fixed_ips[3]]) - db.instance_get_all_by_network(mox.IgnoreArg(), - mox.IgnoreArg())\ - .AndReturn(instances) db.network_get_associated_fixed_ips(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn([fixed_ips[0], @@ -194,7 +222,6 @@ class LinuxNetworkTestCase(test.TestCase): def test_update_dhcp_for_nw01(self): self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') - self.mox.StubOutWithMock(db, 'instance_get_all_by_network') self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') db.network_get_associated_fixed_ips(mox.IgnoreArg(), @@ -202,9 +229,6 @@ class LinuxNetworkTestCase(test.TestCase): .AndReturn([fixed_ips[1], fixed_ips[2]]) - db.instance_get_all_by_network(mox.IgnoreArg(), - mox.IgnoreArg())\ - .AndReturn(instances) db.network_get_associated_fixed_ips(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn([fixed_ips[1], @@ -257,27 +281,27 @@ class LinuxNetworkTestCase(test.TestCase): def test_get_dhcp_opts_for_nw00(self): self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') - self.mox.StubOutWithMock(db, 'instance_get_all_by_network') self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') - db.instance_get_all_by_network(mox.IgnoreArg(), - mox.IgnoreArg())\ - .AndReturn(instances) db.network_get_associated_fixed_ips(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn([fixed_ips[0], - fixed_ips[3]]) + fixed_ips[3], + fixed_ips[4]]) db.virtual_interface_get_by_instance(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn([vifs[0], - vifs[1]]) + vifs[1], + vifs[4]]) db.virtual_interface_get_by_instance(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn([vifs[2], - vifs[3]]) + vifs[3], + vifs[5]]) self.mox.ReplayAll() expected_opts = '\n'\ + '\n'\ '' actual_opts = self.driver.get_dhcp_opts(None, networks[0]) @@ -285,28 +309,28 @@ class LinuxNetworkTestCase(test.TestCase): def test_get_dhcp_opts_for_nw01(self): self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') - self.mox.StubOutWithMock(db, 'instance_get_all_by_network') self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') - db.instance_get_all_by_network(mox.IgnoreArg(), - mox.IgnoreArg())\ - .AndReturn(instances) db.network_get_associated_fixed_ips(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn([fixed_ips[1], - fixed_ips[2]]) + fixed_ips[2], + fixed_ips[5]]) db.virtual_interface_get_by_instance(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn([vifs[0], - vifs[1]]) + vifs[1], + vifs[4]]) db.virtual_interface_get_by_instance(mox.IgnoreArg(), mox.IgnoreArg())\ .AndReturn([vifs[2], - vifs[3]]) + vifs[3], + vifs[5]]) self.mox.ReplayAll() expected_opts = 'NW-i00000000-1,3\n'\ - 'NW-i00000001-0,3' + 'NW-i00000001-0,3\n'\ + 'NW-i00000002-1,3' actual_opts = self.driver.get_dhcp_opts(None, networks[1]) self.assertEquals(actual_opts, expected_opts) -- cgit From 00c3cbe8621918fdd60f9068df2a41db52a7f76f Mon Sep 17 00:00:00 2001 From: Keisuke Tagami Date: Tue, 6 Sep 2011 10:20:28 +0900 Subject: revert codes for db --- nova/db/api.py | 0 nova/db/sqlalchemy/api.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) mode change 100755 => 100644 nova/db/api.py mode change 100755 => 100644 nova/db/sqlalchemy/api.py diff --git a/nova/db/api.py b/nova/db/api.py old mode 100755 new mode 100644 diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py old mode 100755 new mode 100644 -- cgit From d4668c7351fab83930a6fe5ee8f67dabe8027b48 Mon Sep 17 00:00:00 2001 From: Keisuke Tagami Date: Tue, 6 Sep 2011 13:57:14 +0900 Subject: fix a mistaking of deletion in ensure_floating_forward --- nova/network/linux_net.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nova/network/linux_net.py b/nova/network/linux_net.py index c26e3574e..48438760a 100755 --- a/nova/network/linux_net.py +++ b/nova/network/linux_net.py @@ -438,7 +438,7 @@ def ensure_vpn_forward(public_ip, port, private_ip): def ensure_floating_forward(floating_ip, fixed_ip): """Ensure floating ip forwarding rule.""" - for chain, rule in floating_forward_rules(floaing_ip, fixed_ip): + for chain, rule in floating_forward_rules(floating_ip, fixed_ip): iptables_manager.ipv4['nat'].add_rule(chain, rule) iptables_manager.apply() -- cgit From 6c55199d3ec900cea771e4f8077c87efa968c2f0 Mon Sep 17 00:00:00 2001 From: Keisuke Tagami Date: Tue, 6 Sep 2011 17:20:10 +0900 Subject: fix a mistaking of dataset and expected values on small test. --- nova/tests/test_linux_net.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/nova/tests/test_linux_net.py b/nova/tests/test_linux_net.py index 3f44fbd05..be6faa07b 100755 --- a/nova/tests/test_linux_net.py +++ b/nova/tests/test_linux_net.py @@ -114,7 +114,7 @@ fixed_ips = [{'id': 0, 'instance': instances[0], 'floating_ips': []}, {'id': 2, - 'network_id': 0, + 'network_id': 1, 'address': '192.168.0.101', 'instance_id': 1, 'allocated': True, @@ -123,7 +123,7 @@ fixed_ips = [{'id': 0, 'instance': instances[1], 'floating_ips': []}, {'id': 3, - 'network_id': 1, + 'network_id': 0, 'address': '192.168.1.101', 'instance_id': 1, 'allocated': True, @@ -256,7 +256,7 @@ class LinuxNetworkTestCase(test.TestCase): "10.0.0.1,fake_instance00.novalocal,"\ "192.168.0.100,net:NW-i00000000-0\n"\ "10.0.0.4,fake_instance01.novalocal,"\ - "192.168.1.101,net:NW-i00000001-1" + "192.168.1.101,net:NW-i00000001-0" actual_hosts = self.driver.get_dhcp_hosts(None, networks[1]) self.assertEquals(actual_hosts, expected) @@ -274,7 +274,7 @@ class LinuxNetworkTestCase(test.TestCase): "10.0.0.2,fake_instance00.novalocal,"\ "192.168.1.100,net:NW-i00000000-1\n"\ "10.0.0.3,fake_instance01.novalocal,"\ - "192.168.0.101,net:NW-i00000001-0" + "192.168.0.101,net:NW-i00000001-1" actual_hosts = self.driver.get_dhcp_hosts(None, networks[0]) self.assertEquals(actual_hosts, expected) @@ -301,7 +301,7 @@ class LinuxNetworkTestCase(test.TestCase): self.mox.ReplayAll() expected_opts = '\n'\ - '\n'\ + 'NW-i00000001-0,3\n'\ '' actual_opts = self.driver.get_dhcp_opts(None, networks[0]) @@ -329,8 +329,8 @@ class LinuxNetworkTestCase(test.TestCase): self.mox.ReplayAll() expected_opts = 'NW-i00000000-1,3\n'\ - 'NW-i00000001-0,3\n'\ - 'NW-i00000002-1,3' + '\n'\ + '' actual_opts = self.driver.get_dhcp_opts(None, networks[1]) self.assertEquals(actual_opts, expected_opts) -- cgit From 53357516226a1c00217c742eb512b7efc0f574b2 Mon Sep 17 00:00:00 2001 From: Tushar Patil Date: Tue, 6 Sep 2011 18:01:57 -0700 Subject: Added use_single_default_gateway to switch from multiple default gateways to single default gateway --- nova/network/linux_net.py | 72 ++++++++++++++++++++------------------------ nova/tests/test_linux_net.py | 17 +++-------- 2 files changed, 38 insertions(+), 51 deletions(-) diff --git a/nova/network/linux_net.py b/nova/network/linux_net.py index 48438760a..affdde701 100755 --- a/nova/network/linux_net.py +++ b/nova/network/linux_net.py @@ -68,6 +68,9 @@ flags.DEFINE_string('linuxnet_interface_driver', 'Driver used to create ethernet devices.') flags.DEFINE_string('linuxnet_ovs_integration_bridge', 'br-int', 'Name of Open vSwitch bridge used with linuxnet') +flags.DEFINE_bool('use_single_default_gateway', + False, 'Use single default gateway. Only first nic of vm' + ' will get default gateway from dhcp server') binary_name = os.path.basename(inspect.stack()[-1][1]) @@ -513,35 +516,27 @@ def get_dhcp_hosts(context, network_ref): def get_dhcp_opts(context, network_ref): """Get network's hosts config in dhcp-opts format.""" - # create a decision dictionary for default gateway for eache instance hosts = [] - default_gateway_network_node = dict() - network_id = network_ref['id'] - instance_set = set() - ips_ref = db.network_get_associated_fixed_ips(context, network_id) - - for fixed_ip_ref in ips_ref: - instance_set.add(fixed_ip_ref['instance_id']) - - for instance_id in instance_set: - # nic number is decided by xxx from this function in xxx function - vifs = db.virtual_interface_get_by_instance(context, instance_id) - if vifs == None: - default_gateway_network_node[instance_id] = None - continue - # offer a default gateway to the first virtual interface of instance - first_vif = vifs[0] - default_gateway_network_node[instance_id] = first_vif['network_id'] - - for fixed_ip_ref in ips_ref: - instance_id = fixed_ip_ref['instance_id'] - if instance_id in default_gateway_network_node: - target_network_id = default_gateway_network_node[instance_id] - if target_network_id == fixed_ip_ref['network_id']: - hosts.append(_host_dhcp_opts(fixed_ip_ref, gw=True)) - else: - hosts.append(_host_dhcp_opts(fixed_ip_ref, gw=None)) - + ips_ref = db.network_get_associated_fixed_ips(context, network_ref['id']) + + if ips_ref: + #set of instance ids + instance_set = set([fixed_ip_ref['instance_id'] + for fixed_ip_ref in ips_ref]) + default_gw_network_node = {} + for instance_id in instance_set: + vifs = db.virtual_interface_get_by_instance(context, instance_id) + if vifs: + #offer a default gateway to the first virtual interface + default_gw_network_node[instance_id] = vifs[0]['network_id'] + + for fixed_ip_ref in ips_ref: + instance_id = fixed_ip_ref['instance_id'] + if instance_id in default_gw_network_node: + target_network_id = default_gw_network_node[instance_id] + # we don't want default gateway for this fixed ip + if target_network_id != fixed_ip_ref['network_id']: + hosts.append(_host_dhcp_opts(fixed_ip_ref)) return '\n'.join(hosts) @@ -560,13 +555,14 @@ def update_dhcp(context, dev, network_ref): with open(conffile, 'w') as f: f.write(get_dhcp_hosts(context, network_ref)) - optsfile = _dhcp_file(dev, 'opts') - with open(optsfile, 'w') as f: - f.write(get_dhcp_opts(context, network_ref)) + if FLAGS.use_single_default_gateway: + optsfile = _dhcp_file(dev, 'opts') + with open(optsfile, 'w') as f: + f.write(get_dhcp_opts(context, network_ref)) + os.chmod(optsfile, 0644) # Make sure dnsmasq can actually read it (it setuid()s to "nobody") os.chmod(conffile, 0644) - os.chmod(optsfile, 0644) pid = _dnsmasq_pid_for(dev) @@ -598,11 +594,13 @@ def update_dhcp(context, dev, network_ref): '--dhcp-lease-max=%s' % len(netaddr.IPNetwork(network_ref['cidr'])), '--dhcp-hostsfile=%s' % _dhcp_file(dev, 'conf'), '--dhcp-script=%s' % FLAGS.dhcpbridge, - '--dhcp-optsfile=%s' % _dhcp_file(dev, 'opts'), '--leasefile-ro'] if FLAGS.dns_server: cmd += ['-h', '-R', '--server=%s' % FLAGS.dns_server] + if FLAGS.use_single_default_gateway: + cmd += ['--dhcp-optsfile=%s' % _dhcp_file(dev, 'opts')] + _execute(*cmd, run_as_root=True) @@ -681,13 +679,9 @@ def _host_dhcp(fixed_ip_ref): "net:" + _host_dhcp_network(fixed_ip_ref)) -def _host_dhcp_opts(fixed_ip_ref, gw): +def _host_dhcp_opts(fixed_ip_ref): """Return a host string for an address in dhcp-host format.""" - if not gw: - return '%s,%s' % (_host_dhcp_network(fixed_ip_ref), - 3) - else: - return '' + return '%s,%s' % (_host_dhcp_network(fixed_ip_ref), 3) def _execute(*cmd, **kwargs): diff --git a/nova/tests/test_linux_net.py b/nova/tests/test_linux_net.py index be6faa07b..4c72f1e0f 100755 --- a/nova/tests/test_linux_net.py +++ b/nova/tests/test_linux_net.py @@ -23,6 +23,7 @@ from nova import log as logging from nova import test from nova import utils from nova.network import manager as network_manager +from nova.network import linux_net import mox @@ -194,6 +195,7 @@ class LinuxNetworkTestCase(test.TestCase): def setUp(self): super(LinuxNetworkTestCase, self).setUp() network_driver = FLAGS.network_driver + self.flags(use_single_default_gateway=True) self.driver = utils.import_object(network_driver) self.driver.db = db @@ -300,9 +302,7 @@ class LinuxNetworkTestCase(test.TestCase): vifs[5]]) self.mox.ReplayAll() - expected_opts = '\n'\ - 'NW-i00000001-0,3\n'\ - '' + expected_opts = 'NW-i00000001-0,3' actual_opts = self.driver.get_dhcp_opts(None, networks[0]) self.assertEquals(actual_opts, expected_opts) @@ -328,19 +328,12 @@ class LinuxNetworkTestCase(test.TestCase): vifs[5]]) self.mox.ReplayAll() - expected_opts = 'NW-i00000000-1,3\n'\ - '\n'\ - '' + expected_opts = "NW-i00000000-1,3" actual_opts = self.driver.get_dhcp_opts(None, networks[1]) self.assertEquals(actual_opts, expected_opts) - def test_dhcp_opts_default_gateway_network(self): - expected = "" - actual = self.driver._host_dhcp_opts(fixed_ips[0], True) - self.assertEquals(actual, expected) - def test_dhcp_opts_not_default_gateway_network(self): expected = "NW-i00000000-0,3" - actual = self.driver._host_dhcp_opts(fixed_ips[0], False) + actual = self.driver._host_dhcp_opts(fixed_ips[0]) self.assertEquals(actual, expected) -- cgit From a158d5721c18902b290a42bb3874b34e54dbcd7b Mon Sep 17 00:00:00 2001 From: Tushar Patil Date: Wed, 7 Sep 2011 13:10:05 -0700 Subject: exclude net tag from host_dhcp if use_single_default_gateway flag is set to false --- nova/network/linux_net.py | 17 ++++++++++++----- nova/tests/test_linux_net.py | 6 ++++++ 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/nova/network/linux_net.py b/nova/network/linux_net.py index affdde701..7d89b2bcc 100755 --- a/nova/network/linux_net.py +++ b/nova/network/linux_net.py @@ -672,11 +672,18 @@ def _host_dhcp_network(fixed_ip_ref): def _host_dhcp(fixed_ip_ref): """Return a host string for an address in dhcp-host format.""" instance_ref = fixed_ip_ref['instance'] - return '%s,%s.%s,%s,%s' % (fixed_ip_ref['virtual_interface']['address'], - instance_ref['hostname'], - FLAGS.dhcp_domain, - fixed_ip_ref['address'], - "net:" + _host_dhcp_network(fixed_ip_ref)) + vif = fixed_ip_ref['virtual_interface'] + if FLAGS.use_single_default_gateway: + return '%s,%s.%s,%s,%s' % (vif['address'], + instance_ref['hostname'], + FLAGS.dhcp_domain, + fixed_ip_ref['address'], + "net:" + _host_dhcp_network(fixed_ip_ref)) + else: + return '%s,%s.%s,%s' % (vif['address'], + instance_ref['hostname'], + FLAGS.dhcp_domain, + fixed_ip_ref['address']) def _host_dhcp_opts(fixed_ip_ref): diff --git a/nova/tests/test_linux_net.py b/nova/tests/test_linux_net.py index 4c72f1e0f..6d0a2b6bd 100755 --- a/nova/tests/test_linux_net.py +++ b/nova/tests/test_linux_net.py @@ -337,3 +337,9 @@ class LinuxNetworkTestCase(test.TestCase): expected = "NW-i00000000-0,3" actual = self.driver._host_dhcp_opts(fixed_ips[0]) self.assertEquals(actual, expected) + + def test_host_dhcp_without_default_gateway_network(self): + self.flags(use_single_default_gateway=False) + expected = ("10.0.0.1,fake_instance00.novalocal,192.168.0.100") + actual = self.driver._host_dhcp(fixed_ips[0]) + self.assertEquals(actual, expected) -- cgit From 4d98408d4ec605ad9a591d5166f2b8ea6e723ecb Mon Sep 17 00:00:00 2001 From: Tushar Patil Date: Wed, 7 Sep 2011 13:37:58 -0700 Subject: modified unit tests, set use_single_default_gateway flag to True whereever needed instead of setting it in the init method --- nova/tests/test_linux_net.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/nova/tests/test_linux_net.py b/nova/tests/test_linux_net.py index 6d0a2b6bd..99577b88e 100755 --- a/nova/tests/test_linux_net.py +++ b/nova/tests/test_linux_net.py @@ -195,11 +195,11 @@ class LinuxNetworkTestCase(test.TestCase): def setUp(self): super(LinuxNetworkTestCase, self).setUp() network_driver = FLAGS.network_driver - self.flags(use_single_default_gateway=True) self.driver = utils.import_object(network_driver) self.driver.db = db def test_update_dhcp_for_nw00(self): + self.flags(use_single_default_gateway=True) self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') @@ -223,6 +223,7 @@ class LinuxNetworkTestCase(test.TestCase): self.driver.update_dhcp(None, "eth0", networks[0]) def test_update_dhcp_for_nw01(self): + self.flags(use_single_default_gateway=True) self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') self.mox.StubOutWithMock(db, 'virtual_interface_get_by_instance') @@ -246,6 +247,7 @@ class LinuxNetworkTestCase(test.TestCase): self.driver.update_dhcp(None, "eth0", networks[0]) def test_get_dhcp_hosts_for_nw00(self): + self.flags(use_single_default_gateway=True) self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') db.network_get_associated_fixed_ips(mox.IgnoreArg(), @@ -264,6 +266,7 @@ class LinuxNetworkTestCase(test.TestCase): self.assertEquals(actual_hosts, expected) def test_get_dhcp_hosts_for_nw01(self): + self.flags(use_single_default_gateway=True) self.mox.StubOutWithMock(db, 'network_get_associated_fixed_ips') db.network_get_associated_fixed_ips(mox.IgnoreArg(), @@ -339,7 +342,6 @@ class LinuxNetworkTestCase(test.TestCase): self.assertEquals(actual, expected) def test_host_dhcp_without_default_gateway_network(self): - self.flags(use_single_default_gateway=False) expected = ("10.0.0.1,fake_instance00.novalocal,192.168.0.100") actual = self.driver._host_dhcp(fixed_ips[0]) self.assertEquals(actual, expected) -- cgit