summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2012-10-04 22:21:20 +0900
committerFUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>2012-10-05 19:00:32 +0900
commit9314fa4c64a2b8b21d3f749c12cce348dfc1966c (patch)
tree9bbcb157e8180c6ca7e280537b42726d1a3f2292
parent7ad45aa1c1434c587a672f44e36abd3f5cbaa25f (diff)
packet lib: add mpls
Signed-off-by: FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp>
-rw-r--r--ryu/lib/packet/ethernet.py2
-rw-r--r--ryu/lib/packet/mpls.py51
2 files changed, 53 insertions, 0 deletions
diff --git a/ryu/lib/packet/ethernet.py b/ryu/lib/packet/ethernet.py
index de034562..f266ade3 100644
--- a/ryu/lib/packet/ethernet.py
+++ b/ryu/lib/packet/ethernet.py
@@ -16,6 +16,7 @@
import struct
from . import packet_base
from . import vlan
+from . import mpls
from ryu.ofproto import ether
@@ -43,3 +44,4 @@ class ethernet(packet_base.PacketBase):
# copy vlan _TYPES
ethernet._TYPES = vlan.vlan._TYPES
ethernet.register_packet_type(vlan.vlan, ether.ETH_TYPE_8021Q)
+ethernet.register_packet_type(mpls.mpls, ether.ETH_TYPE_MPLS)
diff --git a/ryu/lib/packet/mpls.py b/ryu/lib/packet/mpls.py
new file mode 100644
index 00000000..3dc42f67
--- /dev/null
+++ b/ryu/lib/packet/mpls.py
@@ -0,0 +1,51 @@
+# Copyright (C) 2012 Nippon Telegraph and Telephone Corporation.
+#
+# 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
+import socket
+from . import packet_base
+from . import packet_utils
+from . import ipv4
+from ryu.ofproto import ether
+
+
+class mpls(packet_base.PacketBase):
+ _PACK_STR = '!I'
+ _MIN_LEN = struct.calcsize(_PACK_STR)
+
+ def __init__(self, label, exp, bsb, ttl):
+ super(mpls, self).__init__()
+ self.label = label
+ self.exp = exp
+ self.bsb = bsb
+ self.ttl = ttl
+ self.length = mpls._MIN_LEN
+
+ @classmethod
+ def parser(cls, buf):
+ (label,) = struct.unpack_from(cls._PACK_STR, buf)
+ ttl = label & 0xff
+ bsb = (label >> 8) & 1
+ exp = (label >> 9) & 7
+ label = label >> 12
+ msg = cls(label, exp, bsb, ttl)
+ if bsb:
+ return msg, ipv4.ipv4
+ else:
+ return msg, mpls
+
+ def serialize(self, payload, prev):
+ val = self.label << 12 | self.exp << 9 | self.bsb << 8 | self.ttl
+ return struct.pack(mpls._PACK_STR, val)