diff --git a/packages/omamobile-modem-sit/lib/data_network.py b/packages/omamobile-modem-sit/lib/data_network.py index 55c49c56d..39c8d0a16 100644 --- a/packages/omamobile-modem-sit/lib/data_network.py +++ b/packages/omamobile-modem-sit/lib/data_network.py @@ -76,6 +76,15 @@ def counters(stats): return link['rx']['bytes'], link['tx']['packets'] +def other_exits(routes, dev): + """The default routes that lead out other than through the bearer. One + whose link is down leads nowhere: the USB gadget's fallback route stays + configured without a cable, and counting it kept the cellular route from + coming back after a silent spell.""" + return [r for r in routes + if r.get('dev') != dev and 'linkdown' not in r.get('flags', ())] + + def looks_silent(routed, before, after): """The routed bearer sent packets but received nothing since the last check. Not proof: a few router solicitations get no answer on a live one.""" @@ -423,7 +432,7 @@ def watch(): nonlocal last, seen, check_at routes = json.loads(run('ip', '-j', 'route', 'show', 'default')) routed = any(r.get('dev') == dev for r in routes) - others = [r for r in routes if r.get('dev') != dev] + others = other_exits(routes, dev) if roaming_blocked(): if routed: run('ip', 'route', 'del', 'default', 'dev', dev, 'metric', '700') diff --git a/packages/omamobile-modem-sit/test/test_data_network.py b/packages/omamobile-modem-sit/test/test_data_network.py index 0c9895afc..696f035e8 100644 --- a/packages/omamobile-modem-sit/test/test_data_network.py +++ b/packages/omamobile-modem-sit/test/test_data_network.py @@ -6,7 +6,7 @@ import unittest from unittest.mock import patch import data_network -from data_network import Resolver, SleepHold, configuration, counters, guarded, looks_silent, sleep_signal +from data_network import Resolver, SleepHold, configuration, counters, guarded, looks_silent, other_exits, sleep_signal class ResolverTests(unittest.TestCase): def test_fallback_restoration_preserves_new_wifi_dns(self): @@ -76,6 +76,18 @@ def test_roaming_ends_the_wait(self): hold.cancel() self.assertFalse(hold.returns(others=True, now=301)) + def test_after_a_sleep_a_route_on_a_down_link_does_not_end_the_wait(self): + usb={'dst':'default','gateway':'172.16.42.2','dev':'usb0','metric':2048,'flags':['linkdown']} + wifi={'dst':'default','gateway':'192.168.178.1','dev':'wlp1s0','metric':600,'flags':[]} + hold = SleepHold(grace=10) + hold.announced(routed=True) + hold.over(now=100) + self.assertFalse(hold.returns(bool(other_exits([usb],'rmnet0')), now=101)) # the cable is out: wait the grace + self.assertTrue(hold.returns(bool(other_exits([usb,wifi],'rmnet0')), now=102)) # Wi-Fi is back + hold.announced(routed=True) + hold.over(now=200) + self.assertTrue(hold.returns(bool(other_exits([usb],'rmnet0')), now=210)) # no Wi-Fi: after the grace + def test_counters_read_ip_json(self): stats=[{'stats64':{'rx':{'bytes':0,'packets':0},'tx':{'bytes':900,'packets':12}}}] self.assertEqual(counters(stats),(0,12)) @@ -85,6 +97,13 @@ def test_receiving_idle_or_unrouted_is_not_silent(self): self.assertFalse(looks_silent(True,(100,10),(900,40))) self.assertFalse(looks_silent(True,(0,10),(0,10))) self.assertFalse(looks_silent(False,(0,10),(0,40))) + def test_a_route_on_a_down_link_is_no_other_exit(self): + usb={'dst':'default','gateway':'172.16.42.2','dev':'usb0','metric':2048,'flags':['linkdown']} + self.assertEqual(other_exits([usb],'rmnet0'),[]) + wifi={'dst':'default','gateway':'192.168.178.1','dev':'wlp1s0','metric':600,'flags':[]} + cell={'dst':'default','dev':'rmnet0','metric':700,'flags':[]} + self.assertEqual(other_exits([usb,wifi,cell],'rmnet0'),[wifi]) + self.assertEqual(other_exits([{'dst':'default','dev':'tun-home'}],'rmnet0'),[{'dst':'default','dev':'tun-home'}]) class WatchTests(unittest.TestCase): def test_a_stalled_or_failed_ip_is_retried(self): diff --git a/packages/omamobile-modem-sit/version b/packages/omamobile-modem-sit/version index b463c010a..0bdfd66f8 100644 --- a/packages/omamobile-modem-sit/version +++ b/packages/omamobile-modem-sit/version @@ -1 +1 @@ -0.3.41 +0.3.42