This patch enables to override the AS number of BGPSpeaker instance
by the "local_as" argument of BGPSpeaker.neighbor_add().

e.g.)
  speaker = BGPSpeaker(as_number=65001,  # the default AS number
                       router_id='10.0.0.1',
                       ...)

  speaker.neighbor_add(address='10.0.0.2',
                       remote_as=65101,
                       local_as=65002,  # override AS number
                       ...)

Additionally, this patch fixes a typo and sorts the comments of
BGPSpeaker.neighbor_add() method.

Signed-off-by: IWASE Yusuke <[email protected]>
---
 ryu/services/protocols/bgp/bgpspeaker.py       | 25 +++++++++++++++++--------
 ryu/services/protocols/bgp/peer.py             | 14 +++++++++-----
 ryu/services/protocols/bgp/rtconf/neighbors.py | 25 +++++++++++++++++--------
 3 files changed, 43 insertions(+), 21 deletions(-)

diff --git a/ryu/services/protocols/bgp/bgpspeaker.py 
b/ryu/services/protocols/bgp/bgpspeaker.py
index 25fb65f..ce3eaed 100644
--- a/ryu/services/protocols/bgp/bgpspeaker.py
+++ b/ryu/services/protocols/bgp/bgpspeaker.py
@@ -240,7 +240,8 @@ class BGPSpeaker(object):
                      next_hop=None, password=None, multi_exit_disc=None,
                      site_of_origins=None, is_route_server_client=False,
                      is_next_hop_self=False, local_address=None,
-                     local_port=None, connect_mode=DEFAULT_CONNECT_MODE):
+                     local_port=None, local_as=None,
+                     connect_mode=DEFAULT_CONNECT_MODE):
         """ This method registers a new neighbor. The BGP speaker tries to
         establish a bgp session with the peer (accepts a connection
         from the peer and also tries to connect to it).
@@ -261,11 +262,14 @@ class BGPSpeaker(object):
         ``enable_vpnv6`` enables VPNv6 address family for this
         neighbor. The default is False.
 
+        ``enable_enhanced_refresh`` enable Enhanced Route Refresh for this
+        neighbor. The default is False.
+
         ``next_hop`` specifies the next hop IP address. If not
         specified, host's ip address to access to a peer is used.
 
         ``password`` is used for the MD5 authentication if it's
-        specified. By default, the MD5 authenticaiton is disabled.
+        specified. By default, the MD5 authentication is disabled.
 
         ``multi_exit_disc`` specifies multi exit discriminator (MED) value.
         The default is None and if not specified, MED value is
@@ -280,17 +284,19 @@ class BGPSpeaker(object):
         ``is_next_hop_self`` specifies whether the BGP speaker announces
         its own ip address to iBGP neighbor or not as path's next_hop address.
 
-        ``connect_mode`` specifies how to connect to this neighbor.
-        CONNECT_MODE_ACTIVE tries to connect from us.
-        CONNECT_MODE_PASSIVE just listens and wait for the connection.
-        CONNECT_MODE_BOTH use both methods.
-        The default is CONNECT_MODE_BOTH
-
         ``local_address`` specifies Loopback interface address for
         iBGP peering.
 
         ``local_port`` specifies source TCP port for iBGP peering.
 
+        ``local_as`` specifies local AS number per-peer.
+        The default is the AS number of BGPSpeaker instance.
+
+        ``connect_mode`` specifies how to connect to this neighbor.
+        CONNECT_MODE_ACTIVE tries to connect from us.
+        CONNECT_MODE_PASSIVE just listens and wait for the connection.
+        CONNECT_MODE_BOTH use both methods.
+        The default is CONNECT_MODE_BOTH.
         """
         bgp_neighbor = {}
         bgp_neighbor[neighbors.IP_ADDRESS] = address
@@ -328,6 +334,9 @@ class BGPSpeaker(object):
         if local_port:
             bgp_neighbor[LOCAL_PORT] = local_port
 
+        if local_as:
+            bgp_neighbor[LOCAL_AS] = local_as
+
         call('neighbor.create', **bgp_neighbor)
 
     def neighbor_del(self, address):
diff --git a/ryu/services/protocols/bgp/peer.py 
b/ryu/services/protocols/bgp/peer.py
index 5c14806..53582b3 100644
--- a/ryu/services/protocols/bgp/peer.py
+++ b/ryu/services/protocols/bgp/peer.py
@@ -390,6 +390,10 @@ class Peer(Source, Sink, NeighborConfListener, Activity):
         return self._neigh_conf.multi_exit_disc
 
     @property
+    def local_as(self):
+        return self._neigh_conf.local_as
+
+    @property
     def in_filters(self):
         return self._in_filters
 
@@ -918,9 +922,9 @@ class Peer(Source, Sink, NeighborConfListener, Activity):
                 if (len(path_seg_list) > 0 and
                         isinstance(path_seg_list[0], list) and
                         len(path_seg_list[0]) < 255):
-                    path_seg_list[0].insert(0, self._core_service.asn)
+                    path_seg_list[0].insert(0, self.local_as)
                 else:
-                    path_seg_list.insert(0, [self._core_service.asn])
+                    path_seg_list.insert(0, [self.local_as])
                 aspath_attr = BGPPathAttributeAsPath(path_seg_list)
 
             # MULTI_EXIT_DISC Attribute.
@@ -1186,7 +1190,7 @@ class Peer(Source, Sink, NeighborConfListener, Activity):
 
         Current setting include capabilities, timers and ids.
         """
-        asnum = self._common_conf.local_as
+        asnum = self.local_as
         bgpid = self._common_conf.router_id
         holdtime = self._neigh_conf.hold_time
 
@@ -1369,7 +1373,7 @@ class Peer(Source, Sink, NeighborConfListener, Activity):
 
         aspath = umsg_pattrs.get(BGP_ATTR_TYPE_AS_PATH)
         # Check if AS_PATH has loops.
-        if aspath.has_local_as(self._common_conf.local_as):
+        if aspath.has_local_as(self.local_as):
             LOG.error('Update message AS_PATH has loops. Ignoring this'
                       ' UPDATE. %s', update_msg)
             return
@@ -1497,7 +1501,7 @@ class Peer(Source, Sink, NeighborConfListener, Activity):
 
         aspath = umsg_pattrs.get(BGP_ATTR_TYPE_AS_PATH)
         # Check if AS_PATH has loops.
-        if aspath.has_local_as(self._common_conf.local_as):
+        if aspath.has_local_as(self.local_as):
             LOG.error('Update message AS_PATH has loops. Ignoring this'
                       ' UPDATE. %s', update_msg)
             return
diff --git a/ryu/services/protocols/bgp/rtconf/neighbors.py 
b/ryu/services/protocols/bgp/rtconf/neighbors.py
index 4ab0d57..a12af81 100644
--- a/ryu/services/protocols/bgp/rtconf/neighbors.py
+++ b/ryu/services/protocols/bgp/rtconf/neighbors.py
@@ -74,6 +74,7 @@ ENABLED = 'enabled'
 CHANGES = 'changes'
 LOCAL_ADDRESS = 'local_address'
 LOCAL_PORT = 'local_port'
+LOCAL_AS = 'local_as'
 PEER_NEXT_HOP = 'peer_next_hop'
 PASSWORD = 'password'
 IN_FILTER = 'in_filter'
@@ -299,7 +300,7 @@ class NeighborConf(ConfWithId, ConfWithStats):
                                    CAP_RTC, RTC_AS, HOLD_TIME,
                                    ENABLED, MULTI_EXIT_DISC, MAX_PREFIXES,
                                    ADVERTISE_PEER_AS, SITE_OF_ORIGINS,
-                                   LOCAL_ADDRESS, LOCAL_PORT,
+                                   LOCAL_ADDRESS, LOCAL_PORT, LOCAL_AS,
                                    PEER_NEXT_HOP, PASSWORD,
                                    IN_FILTER, OUT_FILTER,
                                    IS_ROUTE_SERVER_CLIENT, CHECK_FIRST_AS,
@@ -366,6 +367,13 @@ class NeighborConf(ConfWithId, ConfWithStats):
         self._settings[LOCAL_PORT] = compute_optional_conf(
             LOCAL_PORT, None, **kwargs)
 
+        # We use the global defined local (router) AS as the default
+        # local AS.
+        from ryu.services.protocols.bgp.core_manager import CORE_MANAGER
+        g_local_as = CORE_MANAGER.common_conf.local_as
+        self._settings[LOCAL_AS] = compute_optional_conf(
+            LOCAL_AS, g_local_as, **kwargs)
+
         self._settings[PEER_NEXT_HOP] = compute_optional_conf(
             PEER_NEXT_HOP, None, **kwargs)
 
@@ -373,14 +381,11 @@ class NeighborConf(ConfWithId, ConfWithStats):
             PASSWORD, None, **kwargs)
 
         # RTC configurations.
-        self._settings[CAP_RTC] = \
-            compute_optional_conf(CAP_RTC, DEFAULT_CAP_RTC, **kwargs)
+        self._settings[CAP_RTC] = compute_optional_conf(
+            CAP_RTC, DEFAULT_CAP_RTC, **kwargs)
         # Default RTC_AS is local (router) AS.
-        from ryu.services.protocols.bgp.core_manager import \
-            CORE_MANAGER
-        default_rt_as = CORE_MANAGER.common_conf.local_as
-        self._settings[RTC_AS] = \
-            compute_optional_conf(RTC_AS, default_rt_as, **kwargs)
+        self._settings[RTC_AS] = compute_optional_conf(
+            RTC_AS, g_local_as, **kwargs)
 
         # Since ConfWithId' default values use str(self) and repr(self), we
         # call super method after we have initialized other settings.
@@ -437,6 +442,10 @@ class NeighborConf(ConfWithId, ConfWithStats):
     # =========================================================================
 
     @property
+    def local_as(self):
+        return self._settings[LOCAL_AS]
+
+    @property
     def hold_time(self):
         return self._settings[HOLD_TIME]
 
-- 
2.7.4


------------------------------------------------------------------------------
What NetFlow Analyzer can do for you? Monitors network bandwidth and traffic
patterns at an interface-level. Reveals which users, apps, and protocols are 
consuming the most bandwidth. Provides multi-vendor support for NetFlow, 
J-Flow, sFlow and other flows. Make informed decisions using capacity 
planning reports. https://ad.doubleclick.net/ddm/clk/305295220;132659582;e
_______________________________________________
Ryu-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to