synced ch
This commit is contained in:
parent
8319879b7b
commit
4387974a3a
@ -374,24 +374,33 @@ def is_bridge_member(nic):
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def configure_phy_nic_mtu(mng_ip=None):
|
def configure_phy_nic_mtu(mgmt_ip=None):
|
||||||
"""Configure mtu for physical nic."""
|
"""Configure mtu for physical nic."""
|
||||||
phy_nic_mtu = config('phy-nic-mtu')
|
phy_nic_mtu = config('phy-nic-mtu')
|
||||||
if phy_nic_mtu >= 1500:
|
if phy_nic_mtu >= 1500:
|
||||||
phy_nic = None
|
phy_nic = None
|
||||||
if mng_ip is None:
|
if mgmt_ip is None:
|
||||||
mng_ip = unit_get('private-address')
|
mgmt_ip = unit_get('private-address')
|
||||||
|
|
||||||
for nic in list_nics(['eth', 'bond', 'br']):
|
for nic in list_nics(['eth', 'bond', 'br']):
|
||||||
if mng_ip in get_ipv4_addr(nic, fatal=False):
|
if config('prefer-ipv6'):
|
||||||
|
addrs = get_ipv6_addr(iface=nic, fatal=False)
|
||||||
|
else:
|
||||||
|
addrs = get_ipv4_addr(iface=nic, fatal=False)
|
||||||
|
|
||||||
|
if mgmt_ip in addrs:
|
||||||
phy_nic = nic
|
phy_nic = nic
|
||||||
# need to find the associated phy nic for bridge
|
# If bridge iface, find the associated phy nic
|
||||||
if nic.startswith('br'):
|
if nic.startswith('br'):
|
||||||
for brnic in get_bridge_nics(nic):
|
for brnic in get_bridge_nics(nic):
|
||||||
if brnic.startswith('eth') or brnic.startswith('bond'):
|
if brnic.startswith('eth') or brnic.startswith('bond'):
|
||||||
phy_nic = brnic
|
phy_nic = brnic
|
||||||
break
|
break
|
||||||
|
|
||||||
|
if phy_nic and phy_nic_mtu != get_nic_mtu(phy_nic):
|
||||||
|
set_nic_mtu(phy_nic, str(phy_nic_mtu), persistence=True)
|
||||||
|
log('Setting mtu=%s for phy_nic=%s' % (phy_nic_mtu,
|
||||||
|
phy_nic),
|
||||||
|
level=INFO)
|
||||||
|
|
||||||
break
|
break
|
||||||
if phy_nic is not None and phy_nic_mtu != get_nic_mtu(phy_nic):
|
|
||||||
set_nic_mtu(phy_nic, str(phy_nic_mtu), persistence=True)
|
|
||||||
log('set mtu={} for phy_nic={}'
|
|
||||||
.format(phy_nic_mtu, phy_nic), level=INFO)
|
|
||||||
|
@ -372,39 +372,72 @@ def list_nics(nic_type):
|
|||||||
|
|
||||||
|
|
||||||
def set_nic_mtu(nic, mtu, persistence=False):
|
def set_nic_mtu(nic, mtu, persistence=False):
|
||||||
'''Set MTU on a network interface'''
|
"""Set MTU on a network interface."""
|
||||||
cmd = ['ip', 'link', 'set', nic, 'mtu', mtu]
|
cmd = ['ip', 'link', 'set', nic, 'mtu', mtu]
|
||||||
subprocess.check_call(cmd)
|
subprocess.check_call(cmd)
|
||||||
|
|
||||||
# persistence mtu configuration
|
# persistence mtu configuration
|
||||||
if not persistence:
|
if not persistence:
|
||||||
return
|
return
|
||||||
if os.path.exists("/etc/network/interfaces.d/%s.cfg" % nic):
|
|
||||||
nic_cfg_file = "/etc/network/interfaces.d/%s.cfg" % nic
|
|
||||||
else:
|
|
||||||
nic_cfg_file = "/etc/network/interfaces"
|
|
||||||
|
|
||||||
f = open(nic_cfg_file, "r")
|
nic_cfg_path = "/etc/network/interfaces"
|
||||||
lines = f.readlines()
|
nicd_cfg_path = "%s.d/%s.cfg" % (nic_cfg_path, nic)
|
||||||
|
if os.path.exists(nicd_cfg_path):
|
||||||
|
nic_cfg_path = nicd_cfg_path
|
||||||
|
|
||||||
|
with open(nic_cfg_path, "r") as fd:
|
||||||
|
lines = fd.readlines()
|
||||||
|
|
||||||
found = False
|
found = False
|
||||||
length = len(lines)
|
length = len(lines)
|
||||||
for i in range(len(lines)):
|
for i, line in enumerate(lines):
|
||||||
lines[i] = lines[i].replace('\n', '')
|
lines[i] = line.strip()
|
||||||
if lines[i].startswith("iface %s" % nic):
|
if line.strip().startswith("iface %s" % nic):
|
||||||
found = True
|
found = True
|
||||||
lines.insert(i + 1, " up ip link set $IFACE mtu %s" % mtu)
|
index = i
|
||||||
lines.insert(i + 2, " down ip link set $IFACE mtu 1500")
|
|
||||||
if length > i + 2 and lines[i + 3].startswith(" up ip link set $IFACE mtu"):
|
|
||||||
del lines[i + 3]
|
|
||||||
if length > i + 2 and lines[i + 3].startswith(" down ip link set $IFACE mtu"):
|
|
||||||
del lines[i + 3]
|
|
||||||
break
|
break
|
||||||
if not found:
|
|
||||||
lines.insert(length + 1, "")
|
inserts_cfg_found = """
|
||||||
lines.insert(length + 2, "auto %s" % nic)
|
up ip link set $IFACE mtu %s
|
||||||
lines.insert(length + 3, "iface %s inet dhcp" % nic)
|
down ip link set $IFACE mtu 1500
|
||||||
lines.insert(length + 4, " up ip link set $IFACE mtu %s" % mtu)
|
""" % (mtu)
|
||||||
lines.insert(length + 5, " down ip link set $IFACE mtu 1500")
|
|
||||||
write_file(path=nic_cfg_file, content="\n".join(lines), perms=0o644)
|
inserts_cfg_not_found = """
|
||||||
|
auto %s
|
||||||
|
iface %s inet dhcp
|
||||||
|
up ip link set $IFACE mtu %s
|
||||||
|
down ip link set $IFACE mtu 1500
|
||||||
|
""" % (nic, nic, mtu)
|
||||||
|
|
||||||
|
deletes = """
|
||||||
|
up ip link set $IFACE mtu
|
||||||
|
down ip link set $IFACE mtu
|
||||||
|
"""
|
||||||
|
|
||||||
|
if found:
|
||||||
|
for line in inserts_cfg_found.split('\n'):
|
||||||
|
if not line.strip():
|
||||||
|
continue
|
||||||
|
|
||||||
|
index += 1
|
||||||
|
lines.insert(index, " %s" % line.strip())
|
||||||
|
|
||||||
|
for line in deletes.split('\n'):
|
||||||
|
if not line.strip():
|
||||||
|
continue
|
||||||
|
|
||||||
|
if length > index and lines[index + 1].startswith(line):
|
||||||
|
del lines[index + 1]
|
||||||
|
else:
|
||||||
|
index = length
|
||||||
|
for line in inserts_cfg_not_found.split('\n'):
|
||||||
|
if not line.strip():
|
||||||
|
continue
|
||||||
|
|
||||||
|
index += 1
|
||||||
|
lines.insert(index, " %s" % line.strip())
|
||||||
|
|
||||||
|
write_file(path=nic_cfg_path, content="\n".join(lines), perms=0o644)
|
||||||
|
|
||||||
|
|
||||||
def get_nic_mtu(nic):
|
def get_nic_mtu(nic):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user