diff options
| author | Vishvananda Ishaya <vishvananda@yahoo.com> | 2010-09-30 19:21:50 -0700 |
|---|---|---|
| committer | Vishvananda Ishaya <vishvananda@yahoo.com> | 2010-09-30 19:21:50 -0700 |
| commit | 8cb9e732115d531d80c2ae13bc21a48458cd5f2a (patch) | |
| tree | f8d80c3d08ff06cadd85471bd653daddfd94b2a5 | |
| parent | 1dda065c53cbe11a34e7ae60e11e30dfaf6bf7ac (diff) | |
| download | nova-8cb9e732115d531d80c2ae13bc21a48458cd5f2a.tar.gz nova-8cb9e732115d531d80c2ae13bc21a48458cd5f2a.tar.xz nova-8cb9e732115d531d80c2ae13bc21a48458cd5f2a.zip | |
create a new manager for flat networking including dhcp
| -rw-r--r-- | nova/network/linux_net.py | 22 | ||||
| -rw-r--r-- | nova/network/manager.py | 164 |
2 files changed, 101 insertions, 85 deletions
diff --git a/nova/network/linux_net.py b/nova/network/linux_net.py index 709195ba4..1628fbded 100644 --- a/nova/network/linux_net.py +++ b/nova/network/linux_net.py @@ -172,10 +172,10 @@ def update_dhcp(context, network_id): signal causing it to reload, otherwise spawn a new instance """ network_ref = db.network_get(context, network_id) - with open(_dhcp_file(network_ref['vlan'], 'conf'), 'w') as f: + with open(_dhcp_file(network_ref['bridge'], 'conf'), 'w') as f: f.write(get_dhcp_hosts(context, network_id)) - 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: @@ -238,11 +238,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' % _bin_file('nova-dhcpbridge'), ' --leasefile-ro'] return ''.join(cmd) @@ -259,10 +259,12 @@ 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""" - return os.path.abspath("%s/nova-%s.%s" % (FLAGS.networks_path, vlan, kind)) + return os.path.abspath("%s/nova-%s.%s" % (FLAGS.networks_path, + bridge, + kind)) def _bin_file(script): @@ -270,15 +272,15 @@ def _bin_file(script): return os.path.abspath(os.path.join(__file__, "../../../bin", script)) -def _dnsmasq_pid_for(vlan): - """Returns he pid for prior dnsmasq instance for a vlan +def _dnsmasq_pid_for(bridge): + """Returns he 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 1325c300b..4267dc9a9 100644 --- a/nova/network/manager.py +++ b/nova/network/manager.py @@ -50,6 +50,8 @@ flags.DEFINE_string('flat_network_broadcast', '192.168.0.255', 'Broadcast for simple network') 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(), @@ -98,14 +100,6 @@ class NetworkManager(manager.Manager): self._on_set_network_host(context, network_id) return host - def allocate_fixed_ip(self, context, instance_id, *args, **kwargs): - """Gets a fixed ip from the pool""" - raise NotImplementedError() - - def deallocate_fixed_ip(self, context, instance_id, *args, **kwargs): - """Returns a fixed ip to the pool""" - raise NotImplementedError() - def setup_fixed_ip(self, context, address): """Sets up rules for fixed ip""" raise NotImplementedError() @@ -144,6 +138,61 @@ 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 allocate_fixed_ip(self, context, instance_id, *args, **kwargs): + """Gets a fixed ip from the pool""" + network_ref = self.db.project_get_network(context, context.project.id) + address = self.db.fixed_ip_associate_pool(context, + network_ref['id'], + instance_id) + self.db.fixed_ip_update(context, address, {'allocated': True}) + return address + + 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}) + @property def _bottom_reserved_ips(self): # pylint: disable-msg=R0201 """Number of reserved ips at the bottom of the range""" @@ -177,20 +226,6 @@ class NetworkManager(manager.Manager): class FlatManager(NetworkManager): """Basic network where no vlans are used""" - def allocate_fixed_ip(self, context, instance_id, *args, **kwargs): - """Gets a fixed ip from the pool""" - network_ref = self.db.project_get_network(context, context.project.id) - address = self.db.fixed_ip_associate_pool(context, - network_ref['id'], - instance_id) - self.db.fixed_ip_update(context, address, {'allocated': True}) - return address - - 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}) - self.db.fixed_ip_disassociate(context, address) - def setup_compute_network(self, context, project_id): """Network is created manually""" pass @@ -199,6 +234,11 @@ class FlatManager(NetworkManager): """Currently no setup""" pass + 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}) + self.db.fixed_ip_disassociate(context, address) + def _on_set_network_host(self, context, network_id): """Called when this host becomes the host for a project""" # NOTE(vish): should there be two types of network objects @@ -220,6 +260,26 @@ class FlatManager(NetworkManager): self.db.fixed_ip_create(context, {'address': address}) +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 _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""" @@ -244,23 +304,19 @@ class VlanManager(NetworkManager): def allocate_fixed_ip(self, context, instance_id, *args, **kwargs): """Gets a fixed ip from the pool""" - network_ref = self.db.project_get_network(context, context.project.id) if kwargs.get('vpn', None): + network_ref = self.db.project_get_network(context, + context.project.id) address = network_ref['vpn_private_address'] self.db.fixed_ip_associate(context, address, instance_id) + self.db.fixed_ip_update(context, address, {'allocated': True}) else: - address = self.db.fixed_ip_associate_pool(context, - network_ref['id'], - instance_id) - self.db.fixed_ip_update(context, address, {'allocated': True}) + address = super(VlanManager, self).allocate_fixed_ip(context, + instance_id, + *args, + **kwargs) return address - 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""" fixed_ip_ref = self.db.fixed_ip_get_by_address(context, address) @@ -271,48 +327,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 allocate_network(self, context, project_id): """Set up the network""" self._ensure_indexes(context) |
