summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLorenzo Colitti <lorenzo@google.com>2014-04-21 14:12:28 +0900
committerLorenzo Colitti <lorenzo@google.com>2015-02-02 17:47:26 +0900
commit7a799dc4db3d08e3308af6fc1e7a1fa1e404442c (patch)
tree65b5f17e29ca83c5dfd28ef86040c27318dd1b96
parent355dbf5fbbeff6c6b2a42fb5a5df673951cb2b17 (diff)
downloadextras-7a799dc4db3d08e3308af6fc1e7a1fa1e404442c.tar.gz
Better support for rule attributes.
1. Make attributes into a dict so we can find them. 2. Add a function to decode some attributes to Python types. Change-Id: I8d52a203818f5b24cf691cd2e86d52744d2ee191
-rw-r--r--tests/net_test/iproute.py22
-rwxr-xr-xtests/net_test/mark_test.py13
2 files changed, 23 insertions, 12 deletions
diff --git a/tests/net_test/iproute.py b/tests/net_test/iproute.py
index a899cb74..b7f8ec58 100644
--- a/tests/net_test/iproute.py
+++ b/tests/net_test/iproute.py
@@ -109,6 +109,17 @@ EXPERIMENTAL_FRA_UID_START = 18
EXPERIMENTAL_FRA_UID_END = 19
+def Decode(nla_type, nla_data):
+ if nla_type in [FRA_PRIORITY, FRA_FWMARK, FRA_TABLE,
+ EXPERIMENTAL_FRA_UID_START, EXPERIMENTAL_FRA_UID_END]:
+ return struct.unpack("=I", nla_data)[0]
+ elif nla_type in [FRA_OIFNAME]:
+ return nla_data.strip("\x00")
+ else:
+ # Don't know what this is.
+ return nla_data
+
+
def PaddedLength(length):
# TODO: This padding is probably overly simplistic.
return NLA_ALIGNTO * ((length / NLA_ALIGNTO) + (length % NLA_ALIGNTO != 0))
@@ -253,7 +264,7 @@ class IPRoute(object):
self._Debug(" %s" % rtmsg)
# Parse the attributes in the rtmsg.
- attributes = []
+ attributes = {}
bytesleft = nlmsghdr.length - len(nlmsghdr) - len(rtmsg)
while bytesleft:
# Read the nlattr header.
@@ -265,8 +276,13 @@ class IPRoute(object):
padded_len = PaddedLength(nla.nla_len) - len(nla)
nla_data, data = data[:datalen], data[padded_len:]
- attributes.append((nla, nla_data))
- self._Debug(" %s" % str(attributes[-1]))
+ # If it's an attribute we know about, try to decode it.
+ nla_data = Decode(nla.nla_type, nla_data)
+
+ if nla.nla_type in attributes:
+ raise ValueError("Duplicate attribute %d in rules")
+ attributes[nla.nla_type] = nla_data
+ self._Debug(" %s" % str((nla, nla_data)))
bytesleft -= (padded_len + len(nla))
rules.append((rtmsg, attributes))
diff --git a/tests/net_test/mark_test.py b/tests/net_test/mark_test.py
index 3411dd67..efae54d7 100755
--- a/tests/net_test/mark_test.py
+++ b/tests/net_test/mark_test.py
@@ -32,20 +32,15 @@ UDP_PAYLOAD = "hello"
# Check to see if the kernel supports UID routing.
def HaveUidRouting():
- result = False
-
# Create a rule with the UID range selector. If the kernel doesn't understand
# the selector, it will create a rule with no selectors.
iproute.IPRoute().UidRangeRule(6, True, 1000, 2000, 100)
- # Dump dump all the rules. If we find a rule using the UID range selector,
- # then the kernel supports UID range routing.
+ # Dump all the rules. If we find a rule using the UID range selector, then the
+ # kernel supports UID range routing.
rules = iproute.IPRoute().DumpRules(6)
- for unused_rtmsg, attributes in rules:
- for (nla, unused_nla_data) in attributes:
- if nla.nla_type == iproute.EXPERIMENTAL_FRA_UID_START:
- result = True
- break
+ result = any(iproute.EXPERIMENTAL_FRA_UID_START in attrs
+ for rule, attrs in rules)
# Delete the rule.
iproute.IPRoute().UidRangeRule(6, False, 1000, 2000, 100)