This patch enables to configure the default local preference.
If not specified, the value will be 100 as a default.

Signed-off-by: Satoshi Fujimoto <satoshi.fujimo...@gmail.com>
---
 ryu/services/protocols/bgp/application.py     |  4 ++++
 ryu/services/protocols/bgp/bgp_sample_conf.py |  3 +++
 ryu/services/protocols/bgp/bgpspeaker.py      |  9 ++++++++-
 ryu/services/protocols/bgp/peer.py            |  6 ++++--
 ryu/services/protocols/bgp/rtconf/common.py   | 20 +++++++++++++++++++-
 5 files changed, 38 insertions(+), 4 deletions(-)

diff --git a/ryu/services/protocols/bgp/application.py 
b/ryu/services/protocols/bgp/application.py
index a3e386b..b446bf6 100644
--- a/ryu/services/protocols/bgp/application.py
+++ b/ryu/services/protocols/bgp/application.py
@@ -43,6 +43,8 @@ from ryu.services.protocols.bgp.rtconf.common import LOCAL_AS
 from ryu.services.protocols.bgp.rtconf.common import REFRESH_MAX_EOR_TIME
 from ryu.services.protocols.bgp.rtconf.common import REFRESH_STALEPATH_TIME
 from ryu.services.protocols.bgp.rtconf.common import ROUTER_ID
+from ryu.services.protocols.bgp.rtconf.common import LOCAL_PREF
+from ryu.services.protocols.bgp.rtconf.common import DEFAULT_LOCAL_PREF
 from ryu.services.protocols.bgp.utils.validation import is_valid_ipv4
 from ryu.services.protocols.bgp.utils.validation import is_valid_ipv6
 
@@ -262,6 +264,8 @@ class RyuBGPSpeaker(RyuApp):
             LABEL_RANGE, DEFAULT_LABEL_RANGE)
         bgp_settings['allow_local_as_in_count'] = settings.get(
             'allow_local_as_in_count', 0)
+        bgp_settings[LOCAL_PREF] = settings.get(
+            LOCAL_PREF, DEFAULT_LOCAL_PREF)
 
         # Create BGPSpeaker instance.
         LOG.debug('Starting BGPSpeaker...')
diff --git a/ryu/services/protocols/bgp/bgp_sample_conf.py 
b/ryu/services/protocols/bgp/bgp_sample_conf.py
index 9b9564c..60db0b9 100644
--- a/ryu/services/protocols/bgp/bgp_sample_conf.py
+++ b/ryu/services/protocols/bgp/bgp_sample_conf.py
@@ -25,6 +25,9 @@ BGP = {
     # BGP Router ID.
     'router_id': '172.17.0.1',
 
+    # Default local preference
+    'local_pref': 100,
+
     # List of BGP neighbors.
     # The parameters for each neighbor are the same as the arguments of
     # BGPSpeaker.neighbor_add() method.
diff --git a/ryu/services/protocols/bgp/bgpspeaker.py 
b/ryu/services/protocols/bgp/bgpspeaker.py
index f178424..7b9618e 100644
--- a/ryu/services/protocols/bgp/bgpspeaker.py
+++ b/ryu/services/protocols/bgp/bgpspeaker.py
@@ -65,6 +65,8 @@ from ryu.services.protocols.bgp.rtconf.common import 
REFRESH_MAX_EOR_TIME
 from ryu.services.protocols.bgp.rtconf.common import REFRESH_STALEPATH_TIME
 from ryu.services.protocols.bgp.rtconf.common import LABEL_RANGE
 from ryu.services.protocols.bgp.rtconf.common import ALLOW_LOCAL_AS_IN_COUNT
+from ryu.services.protocols.bgp.rtconf.common import LOCAL_PREF
+from ryu.services.protocols.bgp.rtconf.common import DEFAULT_LOCAL_PREF
 from ryu.services.protocols.bgp.rtconf import neighbors
 from ryu.services.protocols.bgp.rtconf import vrfs
 from ryu.services.protocols.bgp.rtconf.base import CAP_MBGP_IPV4
@@ -182,7 +184,8 @@ class BGPSpeaker(object):
                  ssh_port=None, ssh_host=None, ssh_host_key=None,
                  label_range=DEFAULT_LABEL_RANGE,
                  allow_local_as_in_count=0,
-                 cluster_id=None):
+                 cluster_id=None,
+                 local_pref=DEFAULT_LOCAL_PREF):
         """Create a new BGPSpeaker object with as_number and router_id to
         listen on bgp_server_port.
 
@@ -240,6 +243,9 @@ class BGPSpeaker(object):
         ``cluster_id`` specifies the cluster identifier for Route Reflector.
         It must be the string representation of an IPv4 address.
         If omitted, "router_id" is used for this field.
+
+        ``local_pref`` specifies the default local preference. It must be an
+        integer.
         """
 
         super(BGPSpeaker, self).__init__()
@@ -253,6 +259,7 @@ class BGPSpeaker(object):
             LABEL_RANGE: label_range,
             ALLOW_LOCAL_AS_IN_COUNT: allow_local_as_in_count,
             CLUSTER_ID: cluster_id,
+            LOCAL_PREF: local_pref,
         }
         self._core_start(settings)
         self._init_signal_listeners()
diff --git a/ryu/services/protocols/bgp/peer.py 
b/ryu/services/protocols/bgp/peer.py
index 3702739..8c252fb 100644
--- a/ryu/services/protocols/bgp/peer.py
+++ b/ryu/services/protocols/bgp/peer.py
@@ -1153,8 +1153,10 @@ class Peer(Source, Sink, NeighborConfListener, Activity):
                 # For iBGP peers we are required to send local-pref attribute
                 # for connected or local prefixes. We check if the path matches
                 # attribute_maps and set local-pref value.
-                # If the path doesn't match, we set default local-pref 100.
-                localpref_attr = BGPPathAttributeLocalPref(100)
+                # If the path doesn't match, we set default local-pref given
+                # from the user. The default value is 100.
+                localpref_attr = BGPPathAttributeLocalPref(
+                    self._common_conf.local_pref)
                 key = const.ATTR_MAPS_LABEL_DEFAULT
 
                 if isinstance(path, (Vpnv4Path, Vpnv6Path)):
diff --git a/ryu/services/protocols/bgp/rtconf/common.py 
b/ryu/services/protocols/bgp/rtconf/common.py
index 06cee45..f8427ca 100644
--- a/ryu/services/protocols/bgp/rtconf/common.py
+++ b/ryu/services/protocols/bgp/rtconf/common.py
@@ -41,6 +41,7 @@ CLUSTER_ID = 'cluster_id'
 LABEL_RANGE = 'label_range'
 LABEL_RANGE_MAX = 'max'
 LABEL_RANGE_MIN = 'min'
+LOCAL_PREF = 'local_pref'
 
 # Similar to Cisco command 'allowas-in'. Allows the local ASN in the path.
 # Facilitates auto rd, auto rt import/export
@@ -85,6 +86,7 @@ DEFAULT_TCP_CONN_TIMEOUT = 30
 DEFAULT_BGP_CONN_RETRY_TIME = 30
 DEFAULT_MED = 0
 DEFAULT_MAX_PATH_EXT_RTFILTER_ALL = True
+DEFAULT_LOCAL_PREF = 100
 
 
 @validate(name=ALLOW_LOCAL_AS_IN_COUNT)
@@ -219,6 +221,15 @@ def 
validate_max_path_ext_rtfilter_all(max_path_ext_rtfilter_all):
     return max_path_ext_rtfilter_all
 
 
+@validate(name=LOCAL_PREF)
+def validate_local_pref(local_pref):
+    if not isinstance(local_pref, numbers.Integral):
+        raise ConfigTypeError(desc=('Invalid local_pref'
+                                    ' configuration value %s' %
+                                    local_pref))
+    return local_pref
+
+
 class CommonConf(BaseConf):
     """Encapsulates configurations applicable to all peer sessions.
 
@@ -238,7 +249,8 @@ class CommonConf(BaseConf):
                                    BGP_CONN_RETRY_TIME,
                                    MAX_PATH_EXT_RTFILTER_ALL,
                                    ALLOW_LOCAL_AS_IN_COUNT,
-                                   CLUSTER_ID])
+                                   CLUSTER_ID,
+                                   LOCAL_PREF])
 
     def __init__(self, **kwargs):
         super(CommonConf, self).__init__(**kwargs)
@@ -264,6 +276,8 @@ class CommonConf(BaseConf):
             **kwargs)
         self._settings[CLUSTER_ID] = compute_optional_conf(
             CLUSTER_ID, kwargs[ROUTER_ID], **kwargs)
+        self._settings[LOCAL_PREF] = compute_optional_conf(
+            LOCAL_PREF, DEFAULT_LOCAL_PREF, **kwargs)
 
     # =========================================================================
     # Required attributes
@@ -316,6 +330,10 @@ class CommonConf(BaseConf):
     def max_path_ext_rtfilter_all(self):
         return self._settings[MAX_PATH_EXT_RTFILTER_ALL]
 
+    @property
+    def local_pref(self):
+        return self._settings[LOCAL_PREF]
+
     @classmethod
     def get_opt_settings(self):
         self_confs = super(CommonConf, self).get_opt_settings()
-- 
2.7.4


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, Slashdot.org! http://sdm.link/slashdot
_______________________________________________
Ryu-devel mailing list
Ryu-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ryu-devel

Reply via email to