Skip to content

Commit

Permalink
[OpenWrt] Improved static routes #43
Browse files Browse the repository at this point in the history
Closes #43
  • Loading branch information
nemesifier committed Mar 20, 2016
1 parent 848e71e commit 8aa7e21
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 13 deletions.
28 changes: 25 additions & 3 deletions docs/source/backends/openwrt.rst
Expand Up @@ -704,6 +704,26 @@ The static routes settings reside in the ``routes`` key of the *configuration di
which must contain a list of `NetJSON Static Route objects <http://netjson.org/rfc.html#routes1>`_
(see the link for the detailed specification).

Static route object extensions
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In addition to the default *NetJSON Route object options*, the ``OpenWrt`` backend
also allows to define the following optional settings:

+--------------+---------+-------------+---------------------------------------------------+
| key name | type | default | description |
+==============+=========+=============+===================================================+
| ``type`` | string | ``unicast`` | unicast, local, broadcast, multicast, unreachable |
| | | | prohibit, blackhole, anycast |
+--------------+---------+-------------+---------------------------------------------------+
| ``mtu`` | string | ```` | MTU for route, only numbers are allowed |
+--------------+---------+-------------+---------------------------------------------------+
| ``table`` | string | ``False`` | Routing table id, only numbers are allowed |
+--------------+---------+-------------+---------------------------------------------------+
| ``onlink`` | boolean | ```` | When enabled, gateway is on link even if the |
| | | | gateway does not match any interface prefix |
+--------------+---------+-------------+---------------------------------------------------+

Static route example
~~~~~~~~~~~~~~~~~~~~

Expand All @@ -719,14 +739,15 @@ The following *configuration dictionary*:
"next": "192.168.2.2",
"cost": 2,
"source": "192.168.1.10",
"table": 2,
"table": "2",
"onlink": True,
"mtu": 1450
"mtu": "1450"
},
{
"device": "eth1",
"destination": "fd89::1/128",
"next": "fd88::1"
"next": "fd88::1",
"cost": 0,
}
]
}
Expand All @@ -749,6 +770,7 @@ Will be rendered as follows::
config route6
option gateway 'fd88::1'
option interface 'eth1'
option metric '0'
option target 'fd89::1/128'

Policy routing
Expand Down
5 changes: 2 additions & 3 deletions netjsonconfig/backends/openwrt/renderers.py
Expand Up @@ -125,8 +125,7 @@ def _get_routes(self):
del uci_route['device']
del uci_route['next']
del uci_route['destination']
if uci_route.get('cost'):
del uci_route['cost']
del uci_route['cost']
network = ip_interface(route['destination'])
version = 'route' if network.version == 4 else 'route6'
target = network.ip if network.version == 4 else network.network
Expand All @@ -136,7 +135,7 @@ def _get_routes(self):
'interface': route['device'],
'target': str(target),
'gateway': route['next'],
'metric': route.get('cost'),
'metric': route['cost'],
'source': route.get('source')
})
if network.version == 4:
Expand Down
38 changes: 38 additions & 0 deletions netjsonconfig/backends/openwrt/schema.py
Expand Up @@ -125,6 +125,44 @@
}
}
},
"routes": {
"items": {
"properties": {
"type": {
"type": "string",
"enum": [
"unicast",
"local",
"broadcast",
"multicast",
"unreachable",
"prohibit",
"blackhole",
"anycast"
],
"default": "unicast",
"propertyOrder": 0,
},
"mtu": {
"type": "string",
"title": "MTU",
"propertyOrder": 6,
"pattern": "^[0-9]*$",
},
"table": {
"type": "string",
"propertyOrder": 7,
"pattern": "^[0-9]*$",
},
"onlink": {
"type": "boolean",
"default": False,
"format": "checkbox",
"propertyOrder": 8,
}
}
}
},
"ip_rules": {
"type": "array",
"title": "Policy routing",
Expand Down
17 changes: 14 additions & 3 deletions netjsonconfig/schema.py
Expand Up @@ -499,20 +499,31 @@
"required": [
"device",
"destination",
"next"
"next",
"cost"
],
"properties": {
"device": {
"type": "string",
"propertyOrder": 1,
},
"destination": {
"type": "string",
"propertyOrder": 2,
},
"next": {
"title": "next hop",
"type": "string",
"propertyOrder": 2,
},
"destination": {
"cost": {
"type": "integer",
"propertyOrder": 4,
"default": 0,
},
"source": {
"type": "string",
"propertyOrder": 3,
"propertyOrder": 5,
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions tests/openwrt/test_network.py
Expand Up @@ -163,17 +163,19 @@ def test_ipv4_routes(self):
{
"device": "eth1",
"destination": "192.168.3.1/24",
"next": "192.168.2.1"
"next": "192.168.2.1",
"cost": 0
},
{
"device": "eth1",
"destination": "192.168.4.1/24",
"next": "192.168.2.2",
"cost": 2,
"source": "192.168.1.10",
"table": 2,
"table": "2",
"onlink": True,
"mtu": 1450
"mtu": "1450",
"type": "unicast"
}
]
})
Expand All @@ -187,6 +189,7 @@ def test_ipv4_routes(self):
config route 'route1'
option gateway '192.168.2.1'
option interface 'eth1'
option metric '0'
option netmask '255.255.255.0'
option target '192.168.3.1'
Expand All @@ -200,6 +203,7 @@ def test_ipv4_routes(self):
option source '192.168.1.10'
option table '2'
option target '192.168.4.1'
option type 'unicast'
""")
self.assertEqual(o.render(), expected)

Expand All @@ -223,7 +227,8 @@ def test_ipv6_routes(self):
{
"device": "eth1",
"destination": "fd89::1/128",
"next": "fd88::1"
"next": "fd88::1",
"cost": 0
},
{
"device": "eth1",
Expand All @@ -244,6 +249,7 @@ def test_ipv6_routes(self):
config route6
option gateway 'fd88::1'
option interface 'eth1'
option metric '0'
option target 'fd89::1/128'
config route6
Expand Down

0 comments on commit 8aa7e21

Please sign in to comment.