File: library_bgp_speaker.rst

package info (click to toggle)
python-os-ken 3.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 21,280 kB
  • sloc: python: 100,620; erlang: 14,517; ansic: 594; sh: 338; makefile: 136
file content (61 lines) | stat: -rw-r--r-- 2,007 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
*******************
BGP speaker library
*******************

Introduction
============

OS-Ken BGP speaker library helps you to enable your code to speak BGP
protocol. The library supports IPv4, IPv4 MPLS-labeled VPN, IPv6
MPLS-labeled VPN and L2VPN EVPN address families.

Example
=======

The following simple code creates a BGP instance with AS number 64512
and Router ID 10.0.0.1. It tries to establish a bgp session with a
peer (its IP is 192.168.177.32 and the AS number is 64513). The
instance advertizes some prefixes.

.. code-block:: python

    import eventlet

    # BGPSpeaker needs sockets patched
    eventlet.monkey_patch()

    # initialize a log handler
    # this is not strictly necessary but useful if you get messages like:
    #    No handlers could be found for logger "os_ken.lib.hub"
    import logging
    import sys
    log = logging.getLogger()
    log.addHandler(logging.StreamHandler(sys.stderr))

    from os_ken.services.protocols.bgp.bgpspeaker import BGPSpeaker

    def dump_remote_best_path_change(event):
        print 'the best path changed:', event.remote_as, event.prefix,\
            event.nexthop, event.is_withdraw

    def detect_peer_down(remote_ip, remote_as):
        print 'Peer down:', remote_ip, remote_as

    if __name__ == "__main__":
        speaker = BGPSpeaker(as_number=64512, router_id='10.0.0.1',
                             best_path_change_handler=dump_remote_best_path_change,
                             peer_down_handler=detect_peer_down)

        speaker.neighbor_add('192.168.177.32', 64513)
    # uncomment the below line if the speaker needs to talk with a bmp server.
    # speaker.bmp_server_add('192.168.177.2', 11019)
        count = 1
        while True:
            eventlet.sleep(30)
            prefix = '10.20.' + str(count) + '.0/24'
            print "add a new prefix", prefix
            speaker.prefix_add(prefix)
            count += 1
            if count == 4:
                speaker.shutdown()
                break