diff --git a/astara_router/commands/management.py b/astara_router/commands/management.py index be79e15..d5b0286 100644 --- a/astara_router/commands/management.py +++ b/astara_router/commands/management.py @@ -72,6 +72,7 @@ def configure_management(): ) parser.add_argument('mac_address', metavar='lladdr', type=str) parser.add_argument('ip_address', metavar='ipaddr', type=str) + parser.add_argument('--mtu', metavar='mtu', type=int, default=1280) args = parser.parse_args() ip_addr = netaddr.IPNetwork(args.ip_address) @@ -83,6 +84,8 @@ def configure_management(): if not intf.is_up: mgr.up(intf) + intf.mtu = args.mtu + if ip_addr not in intf.addresses: if ip_addr.version == 6: real_ifname = mgr.generic_to_host(intf.ifname) diff --git a/astara_router/drivers/ip.py b/astara_router/drivers/ip.py index 82968a9..26f36e4 100644 --- a/astara_router/drivers/ip.py +++ b/astara_router/drivers/ip.py @@ -156,6 +156,15 @@ class IPManager(base.Manager): real_ifname = self.generic_to_host(interface.ifname) self.sudo('link', 'set', real_ifname, 'down') + def set_mtu(self, interface): + """ + Sets the mtu on interface to mtu. + :param interface: the interface to set mtu + :type interface: astara_router.models.Interface + """ + real_ifname = self.generic_to_host(interface.ifname) + self.sudo('link', 'set', real_ifname, 'mtu', str(interface.mtu)) + def update_interface(self, interface, ignore_link_local=True): """ Updates a network interface, particularly its addresses @@ -177,6 +186,9 @@ class IPManager(base.Manager): # in case where primary and alias are swapped. self._update_addresses(real_ifname, interface, old_interface) + if interface.mtu is not None and old_interface.mtu != interface.mtu: + self.set_mtu(interface) + def _update_addresses(self, real_ifname, interface, old_interface): """ Compare the state of an interface, and add/remove address that have diff --git a/astara_router/models.py b/astara_router/models.py index 8140129..76251ee 100644 --- a/astara_router/models.py +++ b/astara_router/models.py @@ -37,7 +37,7 @@ class Interface(ModelBase): """ """ def __init__(self, ifname=None, addresses=[], groups=None, flags=None, - lladdr=None, mtu=1500, media=None, + lladdr=None, mtu=None, media=None, description=None, **extra_params): self.ifname = ifname self.description = description diff --git a/test/unit/drivers/test_ip.py b/test/unit/drivers/test_ip.py index 37994d1..465ea5b 100644 --- a/test/unit/drivers/test_ip.py +++ b/test/unit/drivers/test_ip.py @@ -63,6 +63,7 @@ class IPTestCase(TestCase): def test_get_interfaces(self): iface_a = mock.Mock() iface_a.ifname = 'em0' + iface_a.mtu = 1500 iface_b = mock.Mock() iface_b.ifname = 'em1' @@ -79,6 +80,7 @@ class IPTestCase(TestCase): def test_get_interface(self): iface_a = mock.Mock() iface_a.ifname = 'em0' + iface_a.mtu = 1500 iface = 'astara_router.drivers.ip._parse_interface' ifaces = 'astara_router.drivers.ip._parse_interfaces' with mock.patch(iface) as parse: @@ -126,7 +128,9 @@ class IPTestCase(TestCase): def test_update_interfaces(self): iface_a = mock.Mock() + iface_a.mtu = 1500 iface_b = mock.Mock() + iface_b.mtu = 1500 attr = 'update_interface' with mock.patch.object(ip.IPManager, attr) as update: @@ -162,6 +166,23 @@ class IPTestCase(TestCase): self.mock_execute.assert_has_calls( [mock.call(['/sbin/ip', 'link', 'set', 'em0', 'down'], 'sudo')]) + def test_set_mtu(self): + iface = mock.Mock() + iface.ifname = 'ge0' + iface.mtu = 1280 + + attr = 'ensure_mapping' + with mock.patch.object(ip.IPManager, attr) as ensure: + mgr = ip.IPManager() + mgr.host_mapping = {'em0': 'ge0'} + mgr.generic_mapping = {'ge0': 'em0'} + + mgr.set_mtu(iface) + + self.mock_execute.assert_has_calls( + [mock.call(['/sbin/ip', 'link', 'set', 'em0', 'mtu', '1280'], + 'sudo')]) + def _update_interface_test_hlpr(self, new_iface, old_iface, ignore_link_local=True): mock_methods = { @@ -185,10 +206,12 @@ class IPTestCase(TestCase): iface = mock.Mock() iface.ifname = 'ge0' iface.addresses = [] + iface.mtu = 1500 old_iface = mock.Mock(name='old') old_iface.ifname = 'ge0' old_iface.addresses = [] + old_iface.mtu = 1500 self._update_interface_test_hlpr(iface, old_iface) @@ -196,10 +219,12 @@ class IPTestCase(TestCase): iface = mock.Mock() iface.ifname = 'ge0' iface.addresses = [] + iface.mtu = 1500 old_iface = mock.Mock(name='old') old_iface.ifname = 'ge0' old_iface.addresses = [netaddr.IPAddress('fe80::1')] + old_iface.mtu = 1500 self._update_interface_test_hlpr(iface, old_iface) self.assertEqual(old_iface.addresses, []) @@ -208,12 +233,14 @@ class IPTestCase(TestCase): iface = mock.Mock() iface.ifname = 'ge0' iface.addresses = [] + iface.mtu = 1500 link_local = netaddr.IPAddress('fe80::1') old_iface = mock.Mock(name='old') old_iface.ifname = 'ge0' old_iface.addresses = [link_local] + old_iface.mtu = 1500 self._update_interface_test_hlpr(iface, old_iface, False) self.assertEqual(old_iface.addresses, [link_local])