Fix docker run command
[integration/test.git] / tools / fastbgp / play.py
index e6e8fd98b45f107ceac8868c28a3f52cb9477e75..d82cbab0073211264b61fc82eb93cddfe0fc25da 100755 (executable)
@@ -65,6 +65,8 @@ def parse_arguments():
     # we should mirror AS number from peer's open message.
     str_help = "Amount of IP prefixes to generate. (negative means ""infinite"")."
     parser.add_argument("--amount", default="1", type=int, help=str_help)
+    str_help = "Rpc server port."
+    parser.add_argument("--port", default="8000", type=int, help=str_help)
     str_help = "Maximum number of IP prefixes to be announced in one iteration"
     parser.add_argument("--insert", default="1", type=int, help=str_help)
     str_help = "Maximum number of IP prefixes to be withdrawn in one iteration"
@@ -126,6 +128,8 @@ def parse_arguments():
     parser.add_argument("--threshold", default="1000", type=int, help=str_help)
     str_help = "RFC 4760 Multiprotocol Extensions for BGP-4 supported"
     parser.add_argument("--rfc4760", default=True, type=bool, help=str_help)
+    str_help = "Using peerip instead of myip for xmlrpc server"
+    parser.add_argument("--usepeerip", default=False, action="store_true", help=str_help)
     str_help = "Link-State NLRI supported"
     parser.add_argument("--bgpls", default=False, type=bool, help=str_help)
     str_help = "Link-State NLRI: Identifier"
@@ -153,9 +157,30 @@ def parse_arguments():
     str_help = "How many play utilities are to be started."
     parser.add_argument("--multiplicity", default="1", type=int, help=str_help)
     str_help = "Open message includes multiprotocol extension capability l2vpn-evpn.\
-Enabling this flag makes the script not decoding the update mesage, because of not\
-supported decoding for these elements."
+    Enabling this flag makes the script not decoding the update mesage, because of not\
+    supported decoding for these elements."
     parser.add_argument("--evpn", default=False, action="store_true", help=str_help)
+    str_help = "Open message includes Multicast in MPLS/BGP IP VPNs arguments.\
+    Enabling this flag makes the script not decoding the update mesage, because of not\
+    supported decoding for these elements."
+    parser.add_argument("--grace", default="8", type=int, help=str_help)
+    str_help = "Open message includes Graceful-restart capability, containing AFI/SAFIS:\
+    IPV4-Unicast, IPV6-Unicast, BGP-LS\
+    Enabling this flag makes the script not decoding the update mesage, because of not\
+    supported decoding for these elements."
+    parser.add_argument("--mvpn", default=False, action="store_true", help=str_help)
+    str_help = "Open message includes L3VPN-MULTICAST arguments.\
+    Enabling this flag makes the script not decoding the update mesage, because of not\
+    supported decoding for these elements."
+    parser.add_argument("--l3vpn_mcast", default=False, action="store_true", help=str_help)
+    str_help = "Open message includes L3VPN-UNICAST arguments, without message decoding."
+    parser.add_argument("--l3vpn", default=False, action="store_true", help=str_help)
+    str_help = "Open message includes ROUTE-TARGET-CONSTRAIN arguments, without message decoding."
+    parser.add_argument("--rt_constrain", default=False, action="store_true", help=str_help)
+    str_help = "Open message includes ipv6-unicast family, without message decoding."
+    parser.add_argument("--ipv6", default=False, action="store_true", help=str_help)
+    str_help = "Add all supported families without message decoding."
+    parser.add_argument("--allf", default=False, action="store_true", help=str_help)
     parser.add_argument("--wfr", default=10, type=int, help="Wait for read timeout")
     str_help = "Skipping well known attributes for update message"
     parser.add_argument("--skipattr", default=False, action="store_true", help=str_help)
@@ -171,7 +196,7 @@ def establish_connection(arguments):
     """Establish connection to BGP peer.
 
     Arguments:
-        :arguments: following command-line argumets are used
+        :arguments: following command-line arguments are used
             - arguments.myip: local IP address
             - arguments.myport: local port
             - arguments.peerip: remote IP address
@@ -320,13 +345,14 @@ class MessageGenerator(object):
                 options for MesageGenerator initialisation
         Notes:
             Calculates and stores default values used later on for
-            message geeration.
+            message generation.
         """
         self.total_prefix_amount = args.amount
         # Number of update messages left to be sent.
         self.remaining_prefixes = self.total_prefix_amount
 
         # New parameters initialisation
+        self.port = args.port
         self.iteration = 0
         self.prefix_base_default = args.firstprefix
         self.prefix_length_default = args.prefixlen
@@ -355,7 +381,14 @@ class MessageGenerator(object):
         self.rfc4760 = args.rfc4760
         self.bgpls = args.bgpls
         self.evpn = args.evpn
+        self.mvpn = args.mvpn
+        self.l3vpn_mcast = args.l3vpn_mcast
+        self.l3vpn = args.l3vpn
+        self.rt_constrain = args.rt_constrain
+        self.ipv6 = args.ipv6
+        self.allf = args.allf
         self.skipattr = args.skipattr
+        self.grace = args.grace
         # Default values when BGP-LS Attributes are used
         if self.bgpls:
             self.prefix_count_to_add_default = 1
@@ -374,9 +407,11 @@ class MessageGenerator(object):
         s1_slots = ((self.total_prefix_amount -
                      self.remaining_prefixes_threshold - 1) /
                     self.prefix_count_to_add_default + 1)
-        s2_slots = ((self.remaining_prefixes_threshold - 1) /
-                    (self.prefix_count_to_add_default -
-                    self.prefix_count_to_del_default) + 1)
+        s2_slots = (
+            (self.remaining_prefixes_threshold - 1)
+            / (self.prefix_count_to_add_default - self.prefix_count_to_del_default)
+            + 1
+        )
         # S1_First_Index = 0
         # S1_Last_Index = s1_slots * self.prefix_count_to_add_default - 1
         s2_first_index = s1_slots * self.prefix_count_to_add_default
@@ -682,9 +717,10 @@ class MessageGenerator(object):
             if not self.phase2_start_time:
                 self.phase2_start_time = time.time()
         # tailor the number of prefixes if needed
-        prefix_count_to_add = (prefix_count_to_del +
-                               min(prefix_count_to_add - prefix_count_to_del,
-                                   self.remaining_prefixes))
+        prefix_count_to_add = (
+            prefix_count_to_del
+            + min(prefix_count_to_add - prefix_count_to_del, self.remaining_prefixes)
+        )
         # prefix slots selection for insertion and withdrawal
         slot_index_to_add = self.iteration
         slot_index_to_del = slot_index_to_add - self.slot_gap_default
@@ -785,7 +821,7 @@ class MessageGenerator(object):
 
         # Optional Parameters
         optional_parameters_hex = ""
-        if self.rfc4760:
+        if self.rfc4760 or self.allf:
             optional_parameter_hex = (
                 "\x02"  # Param type ("Capability Ad")
                 "\x06"  # Length (6 bytes)
@@ -798,7 +834,19 @@ class MessageGenerator(object):
             )
             optional_parameters_hex += optional_parameter_hex
 
-        if self.bgpls:
+        if self.ipv6 or self.allf:
+            optional_parameter_hex = (
+                "\x02"  # Param type ("Capability Ad")
+                "\x06"  # Length (6 bytes)
+                "\x01"  # Multiprotocol extetension capability,
+                "\x04"  # Capability value length
+                "\x00\x02"  # AFI (IPV6)
+                "\x00"  # (reserved)
+                "\x01"  # SAFI (UNICAST)
+            )
+            optional_parameters_hex += optional_parameter_hex
+
+        if self.bgpls or self.allf:
             optional_parameter_hex = (
                 "\x02"  # Param type ("Capability Ad")
                 "\x06"  # Length (6 bytes)
@@ -811,7 +859,7 @@ class MessageGenerator(object):
             )
             optional_parameters_hex += optional_parameter_hex
 
-        if self.evpn:
+        if self.evpn or self.allf:
             optional_parameter_hex = (
                 "\x02"  # Param type ("Capability Ad")
                 "\x06"  # Length (6 bytes)
@@ -823,6 +871,84 @@ class MessageGenerator(object):
             )
             optional_parameters_hex += optional_parameter_hex
 
+        if self.mvpn or self.allf:
+            optional_parameter_hex = (
+                "\x02"  # Param type ("Capability Ad")
+                "\x06"  # Length (6 bytes)
+                "\x01"  # Multiprotocol extetension capability,
+                "\x04"  # Capability value length
+                "\x00\x01"  # AFI (IPV4)
+                "\x00"  # (reserved)
+                "\x05"  # SAFI (MCAST-VPN)
+            )
+            optional_parameters_hex += optional_parameter_hex
+            optional_parameter_hex = (
+                "\x02"  # Param type ("Capability Ad")
+                "\x06"  # Length (6 bytes)
+                "\x01"  # Multiprotocol extetension capability,
+                "\x04"  # Capability value length
+                "\x00\x02"  # AFI (IPV6)
+                "\x00"  # (reserved)
+                "\x05"  # SAFI (MCAST-VPN)
+            )
+            optional_parameters_hex += optional_parameter_hex
+
+        if self.l3vpn_mcast or self.allf:
+            optional_parameter_hex = (
+                "\x02"  # Param type ("Capability Ad")
+                "\x06"  # Length (6 bytes)
+                "\x01"  # Multiprotocol extetension capability,
+                "\x04"  # Capability value length
+                "\x00\x01"  # AFI (IPV4)
+                "\x00"  # (reserved)
+                "\x81"  # SAFI (L3VPN-MCAST)
+            )
+            optional_parameters_hex += optional_parameter_hex
+            optional_parameter_hex = (
+                "\x02"  # Param type ("Capability Ad")
+                "\x06"  # Length (6 bytes)
+                "\x01"  # Multiprotocol extetension capability,
+                "\x04"  # Capability value length
+                "\x00\x02"  # AFI (IPV6)
+                "\x00"  # (reserved)
+                "\x81"  # SAFI (L3VPN-MCAST)
+            )
+            optional_parameters_hex += optional_parameter_hex
+
+        if self.l3vpn or self.allf:
+            optional_parameter_hex = (
+                "\x02"  # Param type ("Capability Ad")
+                "\x06"  # Length (6 bytes)
+                "\x01"  # Multiprotocol extetension capability,
+                "\x04"  # Capability value length
+                "\x00\x01"  # AFI (IPV4)
+                "\x00"  # (reserved)
+                "\x80"  # SAFI (L3VPN-UNICAST)
+            )
+            optional_parameters_hex += optional_parameter_hex
+            optional_parameter_hex = (
+                "\x02"  # Param type ("Capability Ad")
+                "\x06"  # Length (6 bytes)
+                "\x01"  # Multiprotocol extetension capability,
+                "\x04"  # Capability value length
+                "\x00\x02"  # AFI (IPV6)
+                "\x00"  # (reserved)
+                "\x80"  # SAFI (L3VPN-UNICAST)
+            )
+            optional_parameters_hex += optional_parameter_hex
+
+        if self.rt_constrain or self.allf:
+            optional_parameter_hex = (
+                "\x02"  # Param type ("Capability Ad")
+                "\x06"  # Length (6 bytes)
+                "\x01"  # Multiprotocol extetension capability,
+                "\x04"  # Capability value length
+                "\x00\x01"  # AFI (IPV4)
+                "\x00"  # (reserved)
+                "\x84"  # SAFI (ROUTE-TARGET-CONSTRAIN)
+            )
+            optional_parameters_hex += optional_parameter_hex
+
         optional_parameter_hex = (
             "\x02"  # Param type ("Capability Ad")
             "\x06"  # Length (6 bytes)
@@ -835,6 +961,39 @@ class MessageGenerator(object):
         )
         optional_parameters_hex += optional_parameter_hex
 
+        if self.grace != 8:
+            b = list(bin(self.grace)[2:])
+            b = b + [0] * (3 - len(b))
+            length = "\x08"
+            if b[1] == '1':
+                restart_flag = "\x80\x05"
+            else:
+                restart_flag = "\x00\x05"
+            if b[2] == '1':
+                ipv4_flag = "\x80"
+            else:
+                ipv4_flag = "\x00"
+            if b[0] == '1':
+                ll_gr = "\x47\x07\x00\x01\x01\x00\x00\x00\x1e"
+                length = "\x11"
+            else:
+                ll_gr = ""
+            logger.debug("Grace parameters list: {}".format(b))
+            # "\x02" Param type ("Capability Ad")
+            # :param length: Length of whole message
+            # "\x40" Graceful-restart capability
+            # "\x06" Length (6 bytes)
+            # "\x00" Restart Flag (customizable - turned on when grace == 2,3,6,7)
+            # "\x05" Restart timer (5sec)
+            # "\x00\x01" AFI (IPV4)
+            # "\x01"  SAFI (Unicast)
+            # "\x00"  Ipv4 Flag (customizable - turned on when grace == 1,3,5,7)
+            # "\x47\x07\x00\x01\x01\x00\x00\x00\x1e" ipv4 ll-graceful-restart capability, timer 30sec
+            # ll-gr turned on when grace is between 4-7
+            optional_parameter_hex = "\x02{}\x40\x06{}\x00\x01\x01{}{}".format(
+                length, restart_flag, ipv4_flag, ll_gr)
+            optional_parameters_hex += optional_parameter_hex
+
         # Optional Parameters Length
         optional_parameters_length = len(optional_parameters_hex)
         optional_parameters_length_hex = struct.pack("B",
@@ -995,7 +1154,7 @@ class MessageGenerator(object):
             if cluster_list_item is not None:
                 path_attributes_hex += (
                     "\x80"  # Flags ("Optional, non-transitive")
-                    "\x09"  # Type (CLUSTER_LIST)
+                    "\x0a"  # Type (CLUSTER_LIST)
                     "\x04"  # Length (4)
                 )           # one CLUSTER_LIST item (4 bytes)
                 path_attributes_hex += struct.pack(">I", int(cluster_list_item))
@@ -1055,6 +1214,9 @@ class MessageGenerator(object):
             nlri_hex
         )
 
+        if self.grace != 8 and self.grace != 0 and end_of_rib:
+            message_hex = (marker_hex + binascii.unhexlify("00170200000000"))
+
         if self.log_debug:
             logger.debug("UPDATE message encoding")
             logger.debug("  Marker=0x" + binascii.hexlify(marker_hex))
@@ -1278,7 +1440,9 @@ class ReadTracker(object):
     for idle waiting.
     """
 
-    def __init__(self, bgp_socket, timer, storage, evpn=False, wait_for_read=10):
+    def __init__(self, bgp_socket, timer, storage, evpn=False, mvpn=False,
+                 l3vpn_mcast=False, allf=False, l3vpn=False, rt_constrain=False,
+                 ipv6=False, grace=8, wait_for_read=10):
         """The reader initialisation.
 
         Arguments:
@@ -1286,6 +1450,12 @@ class ReadTracker(object):
             timer: timer to be used for scheduling
             storage: thread safe dict
             evpn: flag that evpn functionality is tested
+            mvpn: flag that mvpn functionality is tested
+            grace: flag that grace-restart functionality is tested
+            l3vpn_mcast: flag that l3vpn_mcast functionality is tested
+            l3vpn: flag that l3vpn unicast functionality is tested
+            rt_constrain: flag that rt-constrain functionality is tested
+            allf: flag for all family testing.
         """
         # References to outside objects.
         self.socket = bgp_socket
@@ -1308,7 +1478,14 @@ class ReadTracker(object):
         self.rx_activity_detected = True
         self.storage = storage
         self.evpn = evpn
+        self.mvpn = mvpn
+        self.l3vpn_mcast = l3vpn_mcast
+        self.l3vpn = l3vpn
+        self.rt_constrain = rt_constrain
+        self.ipv6 = ipv6
+        self.allf = allf
         self.wfr = wait_for_read
+        self.grace = grace
 
     def read_message_chunk(self):
         """Read up to one message
@@ -1516,6 +1693,41 @@ class ReadTracker(object):
             logger.debug("Skipping update decoding due to evpn data expected")
             return
 
+        logger.debug("Graceful-restart {}".format(self.grace))
+        if self.grace != 8:
+            logger.debug("Skipping update decoding due to graceful-restart data expected")
+            return
+
+        logger.debug("Mvpn {}".format(self.mvpn))
+        if self.mvpn:
+            logger.debug("Skipping update decoding due to mvpn data expected")
+            return
+
+        logger.debug("L3vpn-mcast {}".format(self.l3vpn_mcast))
+        if self.l3vpn_mcast:
+            logger.debug("Skipping update decoding due to l3vpn_mcast data expected")
+            return
+
+        logger.debug("L3vpn-unicast {}".format(self.l3vpn))
+        if self.l3vpn_mcast:
+            logger.debug("Skipping update decoding due to l3vpn-unicast data expected")
+            return
+
+        logger.debug("Route-Target-Constrain {}".format(self.rt_constrain))
+        if self.rt_constrain:
+            logger.debug("Skipping update decoding due to Route-Target-Constrain data expected")
+            return
+
+        logger.debug("Ipv6-Unicast {}".format(self.ipv6))
+        if self.ipv6:
+            logger.debug("Skipping update decoding due to Ipv6 data expected")
+            return
+
+        logger.debug("Allf {}".format(self.allf))
+        if self.allf:
+            logger.debug("Skipping update decoding")
+            return
+
         if msg_type == 2:
             logger.debug("Message type: 0x%s (update)",
                          binascii.b2a_hex(msg_type_hex))
@@ -1680,7 +1892,10 @@ class StateTracker(object):
         self.generator = generator
         self.timer = timer
         # Sub-trackers.
-        self.reader = ReadTracker(bgp_socket, timer, storage, evpn=cliargs.evpn, wait_for_read=cliargs.wfr)
+        self.reader = ReadTracker(bgp_socket, timer, storage, evpn=cliargs.evpn, mvpn=cliargs.mvpn,
+                                  l3vpn_mcast=cliargs.l3vpn_mcast, l3vpn=cliargs.l3vpn, allf=cliargs.allf,
+                                  rt_constrain=cliargs.rt_constrain, ipv6=cliargs.ipv6, grace=cliargs.grace,
+                                  wait_for_read=cliargs.wfr)
         self.writer = WriteTracker(bgp_socket, generator, timer)
         # Prioritization state.
         self.prioritize_writing = False
@@ -1824,6 +2039,8 @@ def job(arguments, inqueue, storage):
     # FIXME: Add parameter to send default open message first,
     # to work with "you first" peers.
     msg_in = read_open_message(bgp_socket)
+    logger.info(binascii.hexlify(msg_in))
+    storage['open'] = binascii.hexlify(msg_in)
     timer = TimeTracker(msg_in)
     generator = MessageGenerator(arguments)
     msg_out = generator.open_message()
@@ -1854,6 +2071,7 @@ def job(arguments, inqueue, storage):
 
 class Rpcs:
     '''Handler for SimpleXMLRPCServer'''
+
     def __init__(self, sendqueue, storage):
         '''Init method
 
@@ -1909,6 +2127,7 @@ def threaded_job(arguments):
     utils_left = arguments.multiplicity
     prefix_current = arguments.firstprefix
     myip_current = arguments.myip
+    port = arguments.port
     thread_args = []
     rpcqueue = Queue.Queue()
     storage = SafeDict()
@@ -1937,7 +2156,11 @@ def threaded_job(arguments):
         print "Error: unable to start thread."
         raise SystemExit(2)
 
-    rpcserver = SimpleXMLRPCServer((arguments.myip.compressed, 8000), allow_none=True)
+    if arguments.usepeerip:
+        ip = arguments.peerip
+    else:
+        ip = arguments.myip
+    rpcserver = SimpleXMLRPCServer((ip.compressed, port), allow_none=True)
     rpcserver.register_instance(Rpcs(rpcqueue, storage))
     rpcserver.serve_forever()