-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new class MPLSVPN for ipv4/ipv6 MPLS processing
Signed-off-by: Peng Xiao <xiaoquwl@gmail.com>
- Loading branch information
1 parent
8fd9371
commit a183887
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
# Copyright 2016 Cisco Systems, Inc. | ||
# All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
import struct | ||
|
||
|
||
class MPLSVPN(object): | ||
"""The base class for ipv4/ipv6 mpls vpn | ||
""" | ||
WITHDARW_LABEL_HEX = b'\x80\x00\x00' | ||
WITHDARW_LABEL = 524288 | ||
|
||
@classmethod | ||
def parse_mpls_label_stack(cls, data): | ||
labels = [] | ||
while len(data) >= 3: | ||
label = struct.unpack('!L', b'\00'+data[:3])[0] | ||
data = data[3:] | ||
labels.append(label >> 4) | ||
if label & 0x001: | ||
break | ||
return labels | ||
|
||
@classmethod | ||
def construct_mpls_label_stack(cls, labels, bos=True): | ||
data = b'' | ||
for label in labels: | ||
if bos: | ||
data += struct.pack('!L', (label << 4 | 1))[1:] | ||
continue | ||
data += struct.pack('!L', label << 4)[1:] | ||
return data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Copyright 2016 Cisco Systems, Inc. | ||
# All rights reserved. | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); you may | ||
# not use this file except in compliance with the License. You may obtain | ||
# a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
# License for the specific language governing permissions and limitations | ||
# under the License. | ||
|
||
""" Test MPLS VPN """ | ||
|
||
import unittest | ||
|
||
from yabgp.message.attribute.nlri.mpls_vpn import MPLSVPN | ||
|
||
|
||
class TestMPLSVPN(unittest.TestCase): | ||
|
||
def test_parse_mpls_label_stack(self): | ||
label_bin = b'\x00\x01\x41' | ||
self.assertEqual([20], MPLSVPN.parse_mpls_label_stack(label_bin)) | ||
label_bin = b'\x00\x01\x10\x00\x01\x31' | ||
self.assertEqual([17, 19], MPLSVPN.parse_mpls_label_stack(label_bin)) | ||
label_bin = b'\x80\x00\x00' | ||
self.assertEqual([524288], MPLSVPN.parse_mpls_label_stack(label_bin)) | ||
|
||
def test_construct_mpls_label_stack(self): | ||
label_bin = b'\x00\x01\x41' | ||
self.assertEqual(label_bin, MPLSVPN.construct_mpls_label_stack(labels=[20])) | ||
|
||
if __name__ == '__main__': | ||
unittest.main() |