Ejemplo n.º 1
0
    def _create_bgp_speaker_for_vlan(self, bgp_speaker_key, bgp_router):
        """Set up BGP speaker for an individual VLAN if required.

        Args:
            bgp_speaker_key (BgpSpeakerKey): BGP speaker key.
            bgp_router: Router.
        Returns:
            ryu.services.protocols.bgp.bgpspeaker.BGPSpeaker: BGP speaker.
        """
        route_handler = lambda x: self._bgp_route_handler(x, bgp_speaker_key)
        server_address = sorted(bgp_router.bgp_server_addresses_by_ipv(bgp_speaker_key.ipv))[0]
        beka = Beka(
            local_address=str(server_address),
            bgp_port=bgp_router.bgp_port(),
            local_as=bgp_router.bgp_as(),
            router_id=bgp_router.bgp_routerid(),
            peer_up_handler=self._bgp_up_handler,
            peer_down_handler=self._bgp_down_handler,
            route_handler=route_handler,
            error_handler=self.logger.warning)
        for ip_dst, ip_gw in self._vlan_prefixes_by_ipv(bgp_router.bgp_vlan(), bgp_speaker_key.ipv):
            beka.add_route(prefix=str(ip_dst), next_hop=str(ip_gw))
        for bgp_neighbor_address in bgp_router.bgp_neighbor_addresses_by_ipv(bgp_speaker_key.ipv):
            beka.add_neighbor(
                connect_mode=bgp_router.bgp_connect_mode(),
                peer_ip=str(bgp_neighbor_address),
                peer_as=bgp_router.bgp_neighbor_as())
        self.thread = hub.spawn(beka.run)
        self.thread.name = 'beka'
        return beka
Ejemplo n.º 2
0
    def _create_bgp_speaker_for_vlan(self, vlan, bgp_speaker_key):
        """Set up BGP speaker for an individual VLAN if required.

        Args:
            vlan (valve VLAN): VLAN for BGP speaker.
        Returns:
            ryu.services.protocols.bgp.bgpspeaker.BGPSpeaker: BGP speaker.
        """
        route_handler = lambda x: self._bgp_route_handler(x, bgp_speaker_key)
        server_address = sorted(vlan.bgp_server_addresses_by_ipv(bgp_speaker_key.ipv))[0]
        beka = Beka(
            local_address=str(server_address),
            bgp_port=vlan.bgp_port,
            local_as=vlan.bgp_as,
            router_id=vlan.bgp_routerid,
            peer_up_handler=self._bgp_up_handler,
            peer_down_handler=self._bgp_down_handler,
            route_handler=route_handler,
            error_handler=self.logger.warning)
        for ip_dst, ip_gw in self._vlan_prefixes_by_ipv(vlan, bgp_speaker_key.ipv):
            beka.add_route(prefix=str(ip_dst), next_hop=str(ip_gw))
        for bgp_neighbor_address in vlan.bgp_neighbor_addresses_by_ipv(bgp_speaker_key.ipv):
            beka.add_neighbor(
                connect_mode=vlan.bgp_connect_mode,
                peer_ip=str(bgp_neighbor_address),
                peer_as=vlan.bgp_neighbor_as)
        hub.spawn(beka.run)
        return beka
Ejemplo n.º 3
0
 def setUp(self):
     self.beka = Beka(
         local_address=None,
         bgp_port=None,
         local_as=None,
         router_id=None,
         peer_up_handler=None,
         peer_down_handler=None,
         route_handler=None,
         error_handler=None
     )
Ejemplo n.º 4
0
class BekaTestCase(unittest.TestCase):
    def setUp(self):
        self.beka = Beka(
            local_address=None,
            bgp_port=None,
            local_as=None,
            router_id=None,
            peer_up_handler=None,
            peer_down_handler=None,
            route_handler=None,
            error_handler=None
        )

    def test_add_neighbor_must_be_passive(self):
        with self.assertRaises(ValueError) as context:
            self.beka.add_neighbor("active", "10.1.1.1", 65004)

        self.assertTrue("Only passive BGP supported" in str(context.exception))

    def test_add_neighbor_cannot_add_twice(self):
        self.assertFalse(self.beka.peers)
        self.beka.add_neighbor("passive", "10.1.1.1", 65004)
        self.assertTrue(self.beka.peers)
        self.assertEqual(self.beka.peers["10.1.1.1"], {
            "peer_ip": "10.1.1.1",
            "peer_as": 65004
        })

        with self.assertRaises(ValueError) as context:
            self.beka.add_neighbor("passive", "10.1.1.1", 65004)

        self.assertTrue("Peer already added" in str(context.exception))

    def test_add_route_adds_route(self):
        self.beka.add_route("10.1.0.0/16", "192.168.1.3")
        self.assertEqual(self.beka.routes_to_advertise[0],
            RouteAddition(
                prefix=IPPrefix.from_string("10.1.0.0/16"),
                next_hop=IPAddress.from_string("192.168.1.3"),
                as_path="",
                origin="IGP"
            )
        )

    def test_listening_on(self):
        beka = Beka(
            local_address='1.2.3.4',
            bgp_port=12345,
            local_as=23456,
            router_id='4.3.2.1',
            peer_up_handler=None,
            peer_down_handler=None,
            route_handler=None,
            error_handler=None
        )
        self.assertTrue(beka.listening_on('1.2.3.4', 12345))
        self.assertFalse(beka.listening_on('1.2.3.4', 23456))
        self.assertFalse(beka.listening_on('4.3.2.1', 12345))
        self.assertFalse(beka.listening_on('4.3.2.1', 23456))
        self.assertFalse(beka.listening_on('8.8.8.8', 53))
Ejemplo n.º 5
0
    def _create_bgp_speaker_for_vlan(self, vlan, bgp_speaker_key):
        """Set up BGP speaker for an individual VLAN if required.

        Args:
            vlan (valve VLAN): VLAN for BGP speaker.
        Returns:
            ryu.services.protocols.bgp.bgpspeaker.BGPSpeaker: BGP speaker.
        """
        route_handler = lambda x: self._bgp_route_handler(x, bgp_speaker_key)
        server_address = sorted(
            vlan.bgp_server_addresses_by_ipv(bgp_speaker_key.ipv))[0]
        beka = Beka(local_address=str(server_address),
                    bgp_port=vlan.bgp_port,
                    local_as=vlan.bgp_as,
                    router_id=vlan.bgp_routerid,
                    peer_up_handler=self._bgp_up_handler,
                    peer_down_handler=self._bgp_down_handler,
                    route_handler=route_handler,
                    error_handler=self.logger.warning)
        for ip_dst, ip_gw in self._vlan_prefixes_by_ipv(
                vlan, bgp_speaker_key.ipv):
            beka.add_route(prefix=str(ip_dst), next_hop=str(ip_gw))
        for bgp_neighbor_address in vlan.bgp_neighbor_addresses_by_ipv(
                bgp_speaker_key.ipv):
            beka.add_neighbor(connect_mode=vlan.bgp_connect_mode,
                              peer_ip=str(bgp_neighbor_address),
                              peer_as=vlan.bgp_neighbor_as)
        hub.spawn(beka.run)
        return beka
Ejemplo n.º 6
0
    def run(self):
        signal.signal(signal.SIGINT, self.signal_handler)
        pool = GreenPool()

        with open("beka.yaml") as file:
            config = yaml.load(file.read())
        for router in config["routers"]:
            printmsg("Starting Beka on %s" % router["local_address"])
            beka = Beka(
                router["local_address"],
                router["bgp_port"],
                router["local_as"],
                router["router_id"],
                self.peer_up_handler,
                self.peer_down_handler,
                self.route_handler,
                self.error_handler
            )
            for peer in router["peers"]:
                beka.add_neighbor(
                    "passive",
                    peer["peer_ip"],
                    peer["peer_as"],
                )
            if "routes" in router:
                for route in router["routes"]:
                    beka.add_route(
                        route["prefix"],
                        route["next_hop"]
                    )
            self.bekas.append(beka)
            pool.spawn_n(beka.run)
        pool.waitall()
        printmsg("All greenlets gone, exiting")
Ejemplo n.º 7
0
    def _create_bgp_speaker_for_vlan(self, bgp_speaker_key, bgp_router):
        """Set up BGP speaker for an individual VLAN if required.

        Args:
            bgp_speaker_key (BgpSpeakerKey): BGP speaker key.
            bgp_router: Router.
        Returns:
            ryu.services.protocols.bgp.bgpspeaker.BGPSpeaker: BGP speaker.
        """
        server_address = sorted(
            bgp_router.bgp_server_addresses_by_ipv(bgp_speaker_key.ipv))[0]
        beka = Beka(local_address=str(server_address),
                    bgp_port=bgp_router.bgp_port(),
                    local_as=bgp_router.bgp_as(),
                    router_id=bgp_router.bgp_routerid(),
                    peer_up_handler=self._bgp_up_handler,
                    peer_down_handler=self._bgp_down_handler,
                    route_handler=lambda x: self._bgp_route_handler(
                        x, bgp_speaker_key),
                    error_handler=self.logger.warning)
        for ip_dst, ip_gw in self._vlan_prefixes_by_ipv(
                bgp_router.bgp_vlan(), bgp_speaker_key.ipv):
            beka.add_route(prefix=str(ip_dst), next_hop=str(ip_gw))
        for bgp_neighbor_address in bgp_router.bgp_neighbor_addresses_by_ipv(
                bgp_speaker_key.ipv):
            beka.add_neighbor(connect_mode=bgp_router.bgp_connect_mode(),
                              peer_ip=str(bgp_neighbor_address),
                              peer_as=bgp_router.bgp_neighbor_as())
        self.thread = hub.spawn(beka.run)
        self.thread.name = 'beka'
        return beka
Ejemplo n.º 8
0
 def test_listening_on(self):
     beka = Beka(
         local_address='1.2.3.4',
         bgp_port=12345,
         local_as=23456,
         router_id='4.3.2.1',
         peer_up_handler=None,
         peer_down_handler=None,
         route_handler=None,
         error_handler=None
     )
     self.assertTrue(beka.listening_on('1.2.3.4', 12345))
     self.assertFalse(beka.listening_on('1.2.3.4', 23456))
     self.assertFalse(beka.listening_on('4.3.2.1', 12345))
     self.assertFalse(beka.listening_on('4.3.2.1', 23456))
     self.assertFalse(beka.listening_on('8.8.8.8', 53))
Ejemplo n.º 9
0
    def _create_bgp_speaker_for_vlan(self, vlan, dp_id, vlan_vid, ipv):
        """Set up BGP speaker for an individual VLAN if required.

        Args:
            vlan (valve VLAN): VLAN for BGP speaker.
            dp_id (int): Datapath ID for BGP speaker.
            vlan_vid (vlan_vid): VLAN VID for BGP speaker.
        Returns:
            ryu.services.protocols.bgp.bgpspeaker.BGPSpeaker: BGP speaker.
        """
        handler = lambda x: self._bgp_route_handler(x, dp_id, vlan_vid)

        if len(vlan.bgp_server_addresses_by_ipv(ipv)) > 1:
            self.logger.warning(
                'Only one server per IPv per vlan supported')
        server_address = sorted(vlan.bgp_server_addresses_by_ipv(ipv))[0]
        beka = Beka(
            local_address=str(server_address),
            bgp_port=vlan.bgp_port,
            local_as=vlan.bgp_as,
            router_id=vlan.bgp_routerid,
            peer_up_handler=self._bgp_up_handler,
            peer_down_handler=self._bgp_down_handler,
            route_handler=handler,
            error_handler=self.logger.warning
        )
        for ip_dst, ip_gw in self._vlan_prefixes_by_ipv(vlan, ipv):
            beka.add_route(prefix=str(ip_dst), next_hop=str(ip_gw))
        for bgp_neighbor_address in vlan.bgp_neighbor_addresses_by_ipv(ipv):
            beka.add_neighbor(
                connect_mode="passive",
                peer_ip=str(bgp_neighbor_address),
                peer_as=vlan.bgp_neighbor_as
                )

        hub.spawn(beka.run)
        return beka