summaryrefslogtreecommitdiffstats
path: root/nova
diff options
context:
space:
mode:
authorKeisuke Tagami <tagami.keisuke@lab.ntt.co.jp>2011-09-05 14:45:41 +0900
committerKeisuke Tagami <tagami.keisuke@lab.ntt.co.jp>2011-09-05 14:45:41 +0900
commit6b61a6be4a14444723e3728fb0fcdd77bac8fe74 (patch)
tree8b9d9e2605a71ae239d055771903d2986aa382e6 /nova
parent78a63bcad5f29c8927151556229271668b0f9e2b (diff)
Fix bug #835919 that output a option file for dnsmasq not to offer a default gateway on second vif.
Diffstat (limited to 'nova')
-rwxr-xr-x[-rw-r--r--]nova/db/sqlalchemy/api.py15
-rwxr-xr-x[-rw-r--r--]nova/network/linux_net.py55
2 files changed, 68 insertions, 2 deletions
diff --git a/nova/db/sqlalchemy/api.py b/nova/db/sqlalchemy/api.py
index b99667afc..aee1c2a55 100644..100755
--- 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
index 57c1d0c28..8d5a0bbbd 100644..100755
--- 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):