diff options
-rw-r--r-- | nova/network/linux_net.py | 23 | ||||
-rw-r--r-- | nova/network/manager.py | 115 |
2 files changed, 81 insertions, 57 deletions
diff --git a/nova/network/linux_net.py b/nova/network/linux_net.py index 0891c02b1..c0be0e8cc 100644 --- a/nova/network/linux_net.py +++ b/nova/network/linux_net.py @@ -180,14 +180,14 @@ def update_dhcp(context, network_id): """ network_ref = db.network_get(context, network_id) - conffile = _dhcp_file(network_ref['vlan'], 'conf') + conffile = _dhcp_file(network_ref['bridge'], 'conf') with open(conffile, 'w') as f: f.write(get_dhcp_hosts(context, network_id)) # Make sure dnsmasq can actually read it (it setuid()s to "nobody") os.chmod(conffile, 0644) - pid = _dnsmasq_pid_for(network_ref['vlan']) + pid = _dnsmasq_pid_for(network_ref['bridge']) # if dnsmasq is already running, then tell it to reload if pid: @@ -250,11 +250,11 @@ def _dnsmasq_cmd(net): ' --strict-order', ' --bind-interfaces', ' --conf-file=', - ' --pid-file=%s' % _dhcp_file(net['vlan'], 'pid'), + ' --pid-file=%s' % _dhcp_file(net['bridge'], 'pid'), ' --listen-address=%s' % net['gateway'], ' --except-interface=lo', ' --dhcp-range=%s,static,120s' % net['dhcp_start'], - ' --dhcp-hostsfile=%s' % _dhcp_file(net['vlan'], 'conf'), + ' --dhcp-hostsfile=%s' % _dhcp_file(net['bridge'], 'conf'), ' --dhcp-script=%s' % FLAGS.dhcpbridge, ' --leasefile-ro'] return ''.join(cmd) @@ -271,24 +271,25 @@ def _stop_dnsmasq(network): logging.debug("Killing dnsmasq threw %s", exc) -def _dhcp_file(vlan, kind): - """Return path to a pid, leases or conf file for a vlan""" +def _dhcp_file(bridge, kind): + """Return path to a pid, leases or conf file for a bridge""" if not os.path.exists(FLAGS.networks_path): os.makedirs(FLAGS.networks_path) + return os.path.abspath("%s/nova-%s.%s" % (FLAGS.networks_path, + bridge, + kind)) - return os.path.abspath("%s/nova-%s.%s" % (FLAGS.networks_path, vlan, kind)) - -def _dnsmasq_pid_for(vlan): - """Returns he pid for prior dnsmasq instance for a vlan +def _dnsmasq_pid_for(bridge): + """Returns the pid for prior dnsmasq instance for a bridge Returns None if no pid file exists If machine has rebooted pid might be incorrect (caller should check) """ - pid_file = _dhcp_file(vlan, 'pid') + pid_file = _dhcp_file(bridge, 'pid') if os.path.exists(pid_file): with open(pid_file, 'r') as f: diff --git a/nova/network/manager.py b/nova/network/manager.py index a1d10c119..2ea1c1aa0 100644 --- a/nova/network/manager.py +++ b/nova/network/manager.py @@ -39,6 +39,8 @@ flags.DEFINE_string('flat_network_bridge', 'br100', 'Bridge for simple network instances') flags.DEFINE_string('flat_network_dns', '8.8.4.4', 'Dns for simple network') +flags.DEFINE_string('flat_network_dhcp_start', '192.168.0.2', + 'Dhcp start for FlatDhcp') flags.DEFINE_integer('vlan_start', 100, 'First VLAN for private networks') flags.DEFINE_integer('num_networks', 1000, 'Number of networks to support') flags.DEFINE_string('vpn_ip', utils.get_my_ip(), @@ -93,7 +95,7 @@ class NetworkManager(manager.Manager): """Gets a fixed ip from the pool""" raise NotImplementedError() - def deallocate_fixed_ip(self, context, instance_id, *args, **kwargs): + def deallocate_fixed_ip(self, context, address, *args, **kwargs): """Returns a fixed ip to the pool""" raise NotImplementedError() @@ -135,6 +137,48 @@ class NetworkManager(manager.Manager): """Returns an floating ip to the pool""" self.db.floating_ip_deallocate(context, floating_address) + def lease_fixed_ip(self, context, mac, address): + """Called by dhcp-bridge when ip is leased""" + logging.debug("Leasing IP %s", address) + fixed_ip_ref = self.db.fixed_ip_get_by_address(context, address) + instance_ref = fixed_ip_ref['instance'] + if not instance_ref: + raise exception.Error("IP %s leased that isn't associated" % + address) + if instance_ref['mac_address'] != mac: + raise exception.Error("IP %s leased to bad mac %s vs %s" % + (address, instance_ref['mac_address'], mac)) + self.db.fixed_ip_update(context, + fixed_ip_ref['address'], + {'leased': True}) + if not fixed_ip_ref['allocated']: + logging.warn("IP %s leased that was already deallocated", address) + + def release_fixed_ip(self, context, mac, address): + """Called by dhcp-bridge when ip is released""" + logging.debug("Releasing IP %s", address) + fixed_ip_ref = self.db.fixed_ip_get_by_address(context, address) + instance_ref = fixed_ip_ref['instance'] + if not instance_ref: + raise exception.Error("IP %s released that isn't associated" % + address) + if instance_ref['mac_address'] != mac: + raise exception.Error("IP %s released from bad mac %s vs %s" % + (address, instance_ref['mac_address'], mac)) + if not fixed_ip_ref['leased']: + logging.warn("IP %s released that was not leased", address) + self.db.fixed_ip_update(context, + fixed_ip_ref['str_id'], + {'leased': False}) + if not fixed_ip_ref['allocated']: + self.db.fixed_ip_disassociate(context, address) + # NOTE(vish): dhcp server isn't updated until next setup, this + # means there will stale entries in the conf file + # the code below will update the file if necessary + if FLAGS.update_dhcp_on_disassociate: + network_ref = self.db.fixed_ip_get_network(context, address) + self.driver.update_dhcp(context, network_ref['id']) + def get_network(self, context): """Get the network for the current context""" raise NotImplementedError() @@ -144,7 +188,6 @@ class NetworkManager(manager.Manager): """Create networks based on parameters""" raise NotImplementedError() - @property def _bottom_reserved_ips(self): # pylint: disable-msg=R0201 """Number of reserved ips at the bottom of the range""" @@ -243,6 +286,30 @@ class FlatManager(NetworkManager): +class FlatDHCPManager(NetworkManager): + """Flat networking with dhcp""" + + def setup_fixed_ip(self, context, address): + """Setup dhcp for this network""" + network_ref = db.fixed_ip_get_by_address(context, address) + self.driver.update_dhcp(context, network_ref['id']) + + def deallocate_fixed_ip(self, context, address, *args, **kwargs): + """Returns a fixed ip to the pool""" + self.db.fixed_ip_update(context, address, {'allocated': False}) + + def _on_set_network_host(self, context, network_id): + """Called when this host becomes the host for a project""" + super(FlatDHCPManager, self)._on_set_network_host(context, network_id) + network_ref = self.db.network_get(context, network_id) + self.db.network_update(context, + network_id, + {'dhcp_start': FLAGS.flat_network_dhcp_start}) + self.driver.ensure_bridge(network_ref['bridge'], + FLAGS.bridge_dev, + network_ref) + + class VlanManager(NetworkManager): """Vlan network with dhcp""" @@ -285,8 +352,6 @@ class VlanManager(NetworkManager): def deallocate_fixed_ip(self, context, address, *args, **kwargs): """Returns a fixed ip to the pool""" self.db.fixed_ip_update(context, address, {'allocated': False}) - fixed_ip_ref = self.db.fixed_ip_get_by_address(context, address) - def setup_fixed_ip(self, context, address): """Sets forwarding rules and dhcp for fixed ip""" @@ -298,48 +363,6 @@ class VlanManager(NetworkManager): network_ref['vpn_private_address']) self.driver.update_dhcp(context, network_ref['id']) - def lease_fixed_ip(self, context, mac, address): - """Called by dhcp-bridge when ip is leased""" - logging.debug("Leasing IP %s", address) - fixed_ip_ref = self.db.fixed_ip_get_by_address(context, address) - instance_ref = fixed_ip_ref['instance'] - if not instance_ref: - raise exception.Error("IP %s leased that isn't associated" % - address) - if instance_ref['mac_address'] != mac: - raise exception.Error("IP %s leased to bad mac %s vs %s" % - (address, instance_ref['mac_address'], mac)) - self.db.fixed_ip_update(context, - fixed_ip_ref['address'], - {'leased': True}) - if not fixed_ip_ref['allocated']: - logging.warn("IP %s leased that was already deallocated", address) - - def release_fixed_ip(self, context, mac, address): - """Called by dhcp-bridge when ip is released""" - logging.debug("Releasing IP %s", address) - fixed_ip_ref = self.db.fixed_ip_get_by_address(context, address) - instance_ref = fixed_ip_ref['instance'] - if not instance_ref: - raise exception.Error("IP %s released that isn't associated" % - address) - if instance_ref['mac_address'] != mac: - raise exception.Error("IP %s released from bad mac %s vs %s" % - (address, instance_ref['mac_address'], mac)) - if not fixed_ip_ref['leased']: - logging.warn("IP %s released that was not leased", address) - self.db.fixed_ip_update(context, - fixed_ip_ref['str_id'], - {'leased': False}) - if not fixed_ip_ref['allocated']: - self.db.fixed_ip_disassociate(context, address) - # NOTE(vish): dhcp server isn't updated until next setup, this - # means there will stale entries in the conf file - # the code below will update the file if necessary - if FLAGS.update_dhcp_on_disassociate: - network_ref = self.db.fixed_ip_get_network(context, address) - self.driver.update_dhcp(context, network_ref['id']) - def setup_compute_network(self, context, instance_id): """Sets up matching network for compute hosts""" network_ref = db.network_get_by_instance(context, instance_id) |