packet/mpls: Add method to convert the format of label
Signed-off-by: Shinpei Muraoka <shinpei.muraoka@gmail.com> Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
This commit is contained in:
committed by
FUJITA Tomonori
parent
ec1749beca
commit
a582b0b0dc
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user