Fix metadata address usage

There are places where we need to use a metadata address
in different forms:

  169.254.169.254
    - when binding to an address, used with a port

  169.254.169.254/32
    - when configuring an address on an interface
    - when adding a route

  169.254.0.0/16
    - when checking if a metadata subnet is present

We were not always using them correctly in either the
DHCP or OVN code, try and correct the usage. This will
make it easier to update the code when adding support
for metadata over IPv6.

Change-Id: I1780aa99204cc24e668d9798f4a5111eae83ecdb
This commit is contained in:
Brian Haley 2020-06-26 13:06:25 -04:00
parent d9d273e538
commit ebd5480e90
6 changed files with 14 additions and 17 deletions

View File

@ -50,10 +50,8 @@ TCP = 'tcp'
DNS_PORT = 53
DHCPV4_PORT = 67
DHCPV6_PORT = 547
METADATA_DEFAULT_PREFIX = 16
METADATA_DEFAULT_IP = '169.254.169.254'
METADATA_DEFAULT_CIDR = '%s/%d' % (METADATA_DEFAULT_IP,
METADATA_DEFAULT_PREFIX)
METADATA_SUBNET_CIDR = '169.254.0.0/16'
METADATA_PORT = 80
WIN2k3_STATIC_DNS = 249
NS_PREFIX = 'qdhcp-'
@ -1141,11 +1139,11 @@ class Dnsmasq(DhcpLocalProcess):
subnet_dhcp_ip = subnet_to_interface_ip.get(subnet.id)
if subnet_dhcp_ip:
host_routes.append(
'%s/32,%s' % (METADATA_DEFAULT_IP, subnet_dhcp_ip)
'%s,%s' % (constants.METADATA_CIDR, subnet_dhcp_ip)
)
elif not isolated_subnets[subnet.id] and gateway:
host_routes.append(
'%s/32,%s' % (METADATA_DEFAULT_IP, gateway)
'%s,%s' % (constants.METADATA_CIDR, gateway)
)
if subnet.ip_version == 4:
@ -1305,7 +1303,7 @@ class Dnsmasq(DhcpLocalProcess):
@staticmethod
def has_metadata_subnet(subnets):
"""Check if the subnets has a metadata subnet."""
meta_cidr = netaddr.IPNetwork(METADATA_DEFAULT_CIDR)
meta_cidr = netaddr.IPNetwork(METADATA_SUBNET_CIDR)
if any(netaddr.IPNetwork(s.cidr) in meta_cidr
for s in subnets):
return True
@ -1710,7 +1708,7 @@ class DeviceManager(object):
ip_cidrs.append('%s/%s' % (gateway, net.prefixlen))
if self.conf.force_metadata or self.conf.enable_isolated_metadata:
ip_cidrs.append(METADATA_DEFAULT_CIDR)
ip_cidrs.append(constants.METADATA_CIDR)
self.driver.init_l3(interface_name, ip_cidrs,
namespace=network.namespace)

View File

@ -373,7 +373,7 @@ class MetadataAgent(object):
mac = match.group()
ip_addresses = set(
port.external_ids[ovn_const.OVN_CIDRS_EXT_ID_KEY].split(' '))
ip_addresses.add(ovn_const.METADATA_DEFAULT_CIDR)
ip_addresses.add(n_const.METADATA_CIDR)
metadata_port = MetadataPortInfo(mac, ip_addresses)
# Create the VETH pair if it's not created. Also the add_veth function

View File

@ -275,10 +275,7 @@ HA_CHASSIS_GROUP_HIGHEST_PRIORITY = 32767
# TODO(lucasagomes): Move this to neutron-lib later.
# Metadata constants
METADATA_DEFAULT_PREFIX = 16
METADATA_DEFAULT_IP = '169.254.169.254'
METADATA_DEFAULT_CIDR = '%s/%d' % (METADATA_DEFAULT_IP,
METADATA_DEFAULT_PREFIX)
METADATA_PORT = 80
# OVN igmp options

View File

@ -1820,9 +1820,9 @@ class TestDeviceManager(base.BaseTestCase):
if port == fake_ipv6_port:
expected_ips = ['2001:db8::a8bb:ccff:fedd:ee99/64',
'169.254.169.254/16']
const.METADATA_CIDR]
else:
expected_ips = ['172.9.9.9/24', '169.254.169.254/16']
expected_ips = ['172.9.9.9/24', const.METADATA_CIDR]
expected = [mock.call.get_device_name(port)]

View File

@ -2975,7 +2975,7 @@ class TestDnsmasq(TestBase):
for alloc in FakeDhcpPort().fixed_ips]
options, idx_map = dm._generate_opts_per_subnet()
contains_metadata_ip = any(['%s/32' % dhcp.METADATA_DEFAULT_IP in line
contains_metadata_ip = any(['%s' % constants.METADATA_CIDR in line
for line in options])
self.assertEqual(expected_mdt_ip, contains_metadata_ip)
@ -3141,7 +3141,7 @@ class TestDeviceManager(TestConfBase):
expect_ips = ['192.168.0.6/24', 'fdca:3ba5:a17a:4ba3::2/64']
if enable_isolated_metadata or force_metadata:
expect_ips.append(dhcp.METADATA_DEFAULT_CIDR)
expect_ips.append(constants.METADATA_CIDR)
mgr.driver.init_l3.assert_called_with('ns-XXX',
expect_ips,
namespace='qdhcp-ns')

View File

@ -15,6 +15,7 @@
import collections
from unittest import mock
from neutron_lib import constants as n_const
from oslo_config import cfg
from oslo_config import fixture as config_fixture
@ -25,6 +26,7 @@ from neutron.agent.linux.ip_lib import IpNetnsCommand as ip_netns
from neutron.agent.linux.ip_lib import IPWrapper as ip_wrap
from neutron.agent.ovn.metadata import agent
from neutron.agent.ovn.metadata import driver
from neutron.common.ovn import constants as ovn_const
from neutron.conf.agent.metadata import config as meta_conf
from neutron.conf.agent.ovn.metadata import config as ovn_meta_conf
from neutron.tests import base
@ -258,13 +260,13 @@ class TestMetadataAgent(base.BaseTestCase):
# Check that the metadata port has the IP addresses properly
# configured and that IPv6 address has been skipped.
expected_calls = [mock.call('10.0.0.1/23'),
mock.call('169.254.169.254/16')]
mock.call(n_const.METADATA_CIDR)]
self.assertEqual(sorted(expected_calls),
sorted(ip_addr_add.call_args_list))
# Check that metadata proxy has been spawned
spawn_mdp.assert_called_once_with(
mock.ANY, 'namespace', 80, mock.ANY,
bind_address='169.254.169.254', network_id='1')
bind_address=ovn_const.METADATA_DEFAULT_IP, network_id='1')
# Check that the chassis has been updated with the datapath.
update_chassis.assert_called_once_with('1')