On Thu, 14 Aug 2014 15:41:45 +0800
Peng Xiao <[email protected]> wrote:

Applied, thanks a lot!

Can you create a patch by 'git format-patch' command next time?

> +@_PathAttribute.register_type(BGP_ATTR_TYPE_CLUSTER_LIST)
> +class BGPPathAttributeClusterList(_PathAttribute):
> +    # CLUSTER_LIST is a new, optional, non-transitive BGP attribute of Type
> +    # code 10. It is a sequence of CLUSTER_ID values representing the
> +    # reflection path that the route has passed.
> +    _VALUE_PACK_STR = '!4s'
> +    _ATTR_FLAGS = BGP_ATTR_FLAG_OPTIONAL
> +    _TYPE = {
> +        'ascii': [
> +            'value'
> +        ]
> +    }
> +
> +    @classmethod
> +    def parse_value(cls, buf):
> +        rest = buf
> +        cluster_list = []
> +        elem_size = struct.calcsize(cls._VALUE_PACK_STR)
> +        while len(rest) >= elem_size:
> +            (cluster_id, ) = struct.unpack_from(
> +                cls._VALUE_PACK_STR, buffer(rest))
> +            cluster_list.append(addrconv.ipv4.bin_to_text(cluster_id))
> +            rest = rest[elem_size:]
> +        return {
> +            'value': cluster_list,
> +        }
> +
> +    def serialize_value(self):
> +        buf = bytearray()
> +        for cluster_id in self.value:
> +            cluster_id_bin = bytearray()
> +            msg_pack_into(
> +                self._VALUE_PACK_STR,
> +                cluster_id_bin,
> +                0,
> +                addrconv.ipv4.text_to_bin(cluster_id))
> +            buf += cluster_id_bin
> +        return buf

You don't need to call bytearray() multiple times. You can call
msg_pack_into with the same buffer with an offset like the following:

diff --git a/ryu/lib/packet/bgp.py b/ryu/lib/packet/bgp.py
index 9cc9839..f523eeb 100644
--- a/ryu/lib/packet/bgp.py
+++ b/ryu/lib/packet/bgp.py
@@ -1688,14 +1688,14 @@ class BGPPathAttributeClusterList(_PathAttribute):
 
     def serialize_value(self):
         buf = bytearray()
+        offset = 0
         for cluster_id in self.value:
-            cluster_id_bin = bytearray()
             msg_pack_into(
                 self._VALUE_PACK_STR,
-                cluster_id_bin,
-                0,
+                buf,
+                offset,
                 addrconv.ipv4.text_to_bin(cluster_id))
-            buf += cluster_id_bin
+            offset += struct.calcsize(self._VALUE_PACK_STR)
         return buf
 
 

------------------------------------------------------------------------------
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to