Signed-off-by: Shinpei Muraoka <[email protected]>
---
ryu/lib/packet/mpls.py | 30 ++++++++++++++++++++++++++----
ryu/tests/unit/packet/test_mpls.py | 36 +++++++++++++++++++++++++++++++++++-
2 files changed, 61 insertions(+), 5 deletions(-)
diff --git a/ryu/lib/packet/mpls.py b/ryu/lib/packet/mpls.py
index 6e8c364..9090e93 100644
--- a/ryu/lib/packet/mpls.py
+++ b/ryu/lib/packet/mpls.py
@@ -14,11 +14,11 @@
# limitations under the License.
import struct
-import socket
+
+import six
+
from . import packet_base
-from . import packet_utils
-from . import ipv4
-from . import ether_types as ether
+from ryu.lib import type_desc
class mpls(packet_base.PacketBase):
@@ -60,6 +60,7 @@ class mpls(packet_base.PacketBase):
label = label >> 12
msg = cls(label, exp, bsb, ttl)
if bsb:
+ from . import ipv4
return msg, ipv4.ipv4, buf[msg._MIN_LEN:]
else:
return msg, mpls, buf[msg._MIN_LEN:]
@@ -67,3 +68,24 @@ class mpls(packet_base.PacketBase):
def serialize(self, payload, prev):
val = self.label << 12 | self.exp << 9 | self.bsb << 8 | self.ttl
return struct.pack(mpls._PACK_STR, val)
+
+
+def label_from_bin(buf):
+ """
+ Converts binary representation label to integer.
+ :param buf: Binary representation of label.
+ :return: MPLS Label and BoS bit.
+ """
+
+ mpls_label = type_desc.Int3.to_user(six.binary_type(buf))
+ return mpls_label >> 4, mpls_label & 1
+
+
+def label_to_bin(mpls_label, is_bos=False):
+ """
+ Converts integer label to binary representation.
+ :param mpls_label: MPLS Label.
+ :param is_bos: BoS bit.
+ :return: Binary representation of label.
+ """
+ return type_desc.Int3.from_user(mpls_label << 4 | is_bos)
diff --git a/ryu/tests/unit/packet/test_mpls.py
b/ryu/tests/unit/packet/test_mpls.py
index bce393c..e3f9bcc 100644
--- a/ryu/tests/unit/packet/test_mpls.py
+++ b/ryu/tests/unit/packet/test_mpls.py
@@ -18,7 +18,7 @@ import unittest
import logging
import inspect
-from nose.tools import *
+from nose.tools import eq_
from ryu.lib.packet import mpls
@@ -56,3 +56,37 @@ class Test_mpls(unittest.TestCase):
jsondict = self.mp.to_jsondict()
mp = mpls.mpls.from_jsondict(jsondict['mpls'])
eq_(str(self.mp), str(mp))
+
+ def test_label_from_bin_true(self):
+ mpls_label = 0xfffff
+ is_bos = True
+ buf = b'\xff\xff\xf1'
+ mpls_label_out, is_bos_out = mpls.label_from_bin(buf)
+
+ eq_(mpls_label, mpls_label_out)
+ eq_(is_bos, is_bos_out)
+
+ def test_label_from_bin_false(self):
+ mpls_label = 0xfffff
+ is_bos = False
+ buf = b'\xff\xff\xf0'
+ mpls_label_out, is_bos_out = mpls.label_from_bin(buf)
+
+ eq_(mpls_label, mpls_label_out)
+ eq_(is_bos, is_bos_out)
+
+ def test_label_to_bin_true(self):
+ mpls_label = 0xfffff
+ is_bos = True
+ label = b'\xff\xff\xf1'
+ label_out = mpls.label_to_bin(mpls_label, is_bos)
+
+ eq_(label, label_out)
+
+ def test_label_to_bin_false(self):
+ mpls_label = 0xfffff
+ is_bos = False
+ label = b'\xff\xff\xf0'
+ label_out = mpls.label_to_bin(mpls_label, is_bos)
+
+ eq_(label, label_out)
--
2.7.4
------------------------------------------------------------------------------
The Command Line: Reinvented for Modern Developers
Did the resurgence of CLI tooling catch you by surprise?
Reconnect with the command line and become more productive.
Learn the new .NET and ASP.NET CLI. Get your free copy!
http://sdm.link/telerik
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel