def run(gobgpd_addr, vrf_name, prefix, nexthop):
    channel = implementations.insecure_channel(gobgpd_addr, 50051)
    with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:

        subnet = IPNetwork(prefix)
        ipaddr = subnet.ip
        masklen = subnet.prefixlen

        nlri = IPAddrPrefix(addr=ipaddr, length=masklen)
        bin_nlri = nlri.serialize()

        nexthop = BGPPathAttributeNextHop(value=nexthop)
        bin_nexthop = nexthop.serialize()

        origin = BGPPathAttributeOrigin(value=2)
        bin_origin = origin.serialize()

        pattrs = []
        pattrs.append(str(bin_nexthop))
        pattrs.append(str(bin_origin))

        path = {}
        path['nlri'] = str(bin_nlri)
        path['pattrs'] = pattrs

        uuid = stub.ModPath(gobgp_pb2.ModPathArguments(resource=Resource_VRF, name=vrf_name, path=path), _TIMEOUT_SECONDS)

        if uuid:
            print "Success!"
        else:
            print "Error!"
def run(gobgpd_addr, vrf_name, route_dist, import_rt, export_rt):
    channel = implementations.insecure_channel(gobgpd_addr, 50051)
    with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:

        bin_rd = str_to_bin_for_rd(route_dist)
        import_rts = []
        bin_import_rt = str_to_bin_for_rt(import_rt)
        import_rts.append(str(bin_import_rt))

        export_rts = []
        bin_export_rt = str_to_bin_for_rt(export_rt)
        export_rts.append(str(bin_export_rt))

        vrf = {}
        vrf['name'] = vrf_name
        vrf['rd'] = str(bin_rd)
        vrf['import_rt'] = import_rts
        vrf['export_rt'] = export_rts


        ret = stub.ModVrf(gobgp_pb2.ModVrfArguments(operation=Operation_ADD, vrf=vrf), _TIMEOUT_SECONDS)
        if ret.code == 0:
            print "Success!"
        else:
            print "Error!"
def run(gobgpd_addr, vrf_name):
    channel = implementations.insecure_channel(gobgpd_addr, 50051)
    with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:
        rib = stub.GetRib(gobgp_pb2.Table(type=Resource_VRF, family=RF_IPv4_UC, name=vrf_name), _TIMEOUT_SECONDS)

        destinations_target = rib.destinations
        for destination_target in destinations_target:
            paths_target = destination_target.paths
            for path_target in paths_target:

                nlri = LabelledVPNIPAddrPrefix.parser(path_target.nlri)
                print (" Rib.prefix     : %s" % nlri[0].prefix)
                print (" Rib.route_dist : %s" % nlri[0].route_dist)
                print (" Rib.label_list : %s" % nlri[0].label_list)
                for pattr in path_target.pattrs:
                    path_attr = _PathAttribute.parser(pattr)
                    if isinstance(path_attr[0], BGPPathAttributeOrigin):
                        print (" Rib.origin     : %s" % path_attr[0].value)
                    elif isinstance(path_attr[0], BGPPathAttributeAsPath):
                        if path_attr[0].type == 2:
                            print(" Rib.aspath     : %s" % path_attr[0].value)
                        else:
                            print(" Rib.aspath     : ???")
                    elif isinstance(path_attr[0], BGPPathAttributeMultiExitDisc):
                        print (" Rib.med        : %s" % path_attr[0].value)
                    elif isinstance(path_attr[0], BGPPathAttributeExtendedCommunities):
                        for community in path_attr[0].communities:
                            if isinstance(community, BGPTwoOctetAsSpecificExtendedCommunity):
                                print(" Rib.community  : %s:%s" % ( community.as_number,
                                                                   community.local_administrator))
                            else:
                                print(" Rib.community  : ???")
                    elif isinstance(path_attr[0], BGPPathAttributeMpReachNLRI):
                        print (" Rib.nexthop    : %s" % path_attr[0].next_hop)
                print "----------------------------"
def run(gobgpd_addr, neighbor_address, local_as, peer_as):
    channel = implementations.insecure_channel(gobgpd_addr, 50051)
    with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:

        conf = {}
        if local_as == peer_as:
            conf['peer_type'] = PEER_TYPE_INTERNAL
        else:
            conf['peer_type'] = PEER_TYPE_EXTERNAL

        conf['neighbor_address'] = neighbor_address
        conf['local_as'] = local_as
        conf['peer_as'] = peer_as

        families = []
        families.append(RF_IPv4_UC)

	

        peer = {}
        peer['conf'] = conf
	peer['families'] = families

        uuid = stub.ModNeighbor(gobgp_pb2.ModNeighborArguments(operation=Operation_ADD, peer=peer), _TIMEOUT_SECONDS)

        if uuid:
            print "Success!"
        else:
            print "Error!"
def run(gobgpd_addr, routefamily):
    channel = implementations.insecure_channel(gobgpd_addr, 50051)
    with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:

        ribs = stub.MonitorBestChanged(gobgp_pb2.Arguments(family=routefamily),
                                                           _TIMEOUT_SECONDS)

        for rib in ribs:
            paths_target = rib.paths
            for path_target in paths_target:
                nlri = IPAddrPrefix.parser(path_target.nlri)
                print "----------------------------"
                print (" Rib.prefix     : %s" % nlri[0].prefix)
                for pattr in path_target.pattrs:
                    path_attr = _PathAttribute.parser(pattr)
                    if isinstance(path_attr[0], BGPPathAttributeOrigin):
                        print (" Rib.origin     : %s" % path_attr[0].value)
                    elif isinstance(path_attr[0], BGPPathAttributeAsPath):
                        if path_attr[0].type == 2:
                            print(" Rib.aspath     : %s" % path_attr[0].value)
                        else:
                            print(" Rib.aspath     : ???")
                    elif isinstance(path_attr[0], BGPPathAttributeMultiExitDisc):
                        print (" Rib.med        : %s" % path_attr[0].value)
                    elif isinstance(path_attr[0], BGPPathAttributeNextHop):
                        print (" Rib.nexthop    : %s" % path_attr[0].value)
                    elif isinstance(path_attr[0], BGPPathAttributeCommunities):
                        for community in path_attr[0].communities:
                            print(" Rib.community  : %s" % community)

                print (" Rib.is_withdraw : %s" % path_target.is_withdraw)
Ejemplo n.º 6
0
def run(af, gobgpd_addr, *prefixes, **kw):
    table_args = dict()
    table_args["family"] = libgobgp.get_route_family(_AF_NAME[af])
    table_args["destinations"] = [
        gobgp_pb2.Destination(prefix=p) for p in prefixes
    ]
    if kw["rib_in_neighbor"] is not None:
        table_args["type"] = gobgp_pb2.ADJ_IN
        table_args["name"] = kw["rib_in_neighbor"]
    elif kw["rib_out_neighbor"] is not None:
        table_args["type"] = gobgp_pb2.ADJ_OUT
        table_args["name"] = kw["rib_out_neighbor"]
    else:
        table_args["type"] = gobgp_pb2.GLOBAL
    # grpc request
    channel = implementations.insecure_channel(gobgpd_addr, 50051)
    try:
        with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:
            response_table = stub.GetRib(
                gobgp_pb2.GetRibRequest(table=gobgp_pb2.Table(**table_args)),
                _TIMEOUT_SECONDS).table
    except ExpirationError:
        print >> sys.stderr, "grpc request timed out!"
    except RemoteError, e:
        print >> sys.stderr, "grpc stub method failed:", e.details
def run(gobgpd_addr):
    channel = implementations.insecure_channel(gobgpd_addr, 50051)
    with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:
        vrfs = stub.GetVrfs(gobgp_pb2.Arguments(), _TIMEOUT_SECONDS)
        for vrf in vrfs:
            print(" Vrf.name : %s" % ( vrf.name))
            routeDist = _RouteDistinguisher.parser(vrf.rd)
            if routeDist.type == 0:
                print(" Vrf.rd   : %s:%s" % ( routeDist.admin, routeDist.assigned))
            else:
                print(" Vrf.rd   : ???")

            import_rt = vrf.import_rt
            for import_route_target in import_rt:
                import_rt_tmp = _ExtendedCommunity.parse(import_route_target)
                import_rt = import_rt_tmp[0]

                if isinstance(import_rt, BGPTwoOctetAsSpecificExtendedCommunity):
                    print(" Vrf.import_rt   : %s:%s" % ( import_rt.as_number, import_rt.local_administrator))
                else:
                    print(" Vrf.import_rt   : ???")

            export_rt = vrf.export_rt
            for export_route_target in export_rt:
                export_rt_tmp = _ExtendedCommunity.parse(export_route_target)
                export_rt = export_rt_tmp[0]

                if isinstance(export_rt, BGPTwoOctetAsSpecificExtendedCommunity):
                    print(" Vrf.export_rt   : %s:%s" % ( export_rt.as_number, export_rt.local_administrator))
                else:
                    print(" Vrf.export_rt   : ???")
            print "----------------------------"
def run(gobgpd_addr):
    channel = implementations.insecure_channel(gobgpd_addr, 50051)
    with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:
        rib = stub.GetRib(gobgp_pb2.Table(type=Resource_GLOBAL, family=RF_IPv4_UC), _TIMEOUT_SECONDS)

        destinations_target = rib.destinations
        for destination_target in destinations_target:
            print (" Rib.prefix     : %s" % destination_target.prefix)
Ejemplo n.º 9
0
def _call_grpc(channel, api_method, api_request, **kwargs):
    with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:
        try:
            api = getattr(stub, api_method)
            request = getattr(gobgp_pb2, api_request)
            return api(request(**kwargs), _TIMEOUT_SECONDS)
        except ExpirationError, e:
            print str(e)
            sys.exit(-1)
Ejemplo n.º 10
0
def run(gobgpd_addr, neighbor_addr):
    channel = implementations.insecure_channel(gobgpd_addr, 50051)
    with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:
        peer = stub.GetNeighbor(gobgp_pb2.Arguments(name=neighbor_addr), _TIMEOUT_SECONDS)
        print("BGP neighbor is %s, remote AS %d" % (peer.conf.neighbor_address, peer.conf.peer_as))
        print("  BGP version 4, remote router ID %s" % (peer.conf.id))
        print("  BGP state = %s, up for %s" % (peer.info.bgp_state, peer.timers.state.uptime))
        print("  BGP OutQ = %d, Flops = %d" % (peer.info.out_q, peer.info.flops))
        print("  Hold time is %d, keepalive interval is %d seconds" % (peer.timers.state.negotiated_hold_time, peer.timers.state.keepalive_interval))
        print("  Configured hold time is %d, keepalive interval is %d seconds" % (peer.timers.config.hold_time, peer.timers.config.keepalive_interval))
Ejemplo n.º 11
0
def run(gobgpd_addr):
    channel = implementations.insecure_channel(gobgpd_addr, 50051)
    with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:
        rib = gobgp_pb2.GetRibRequest()
        rib.table.type = 2
        rib.table.family = 2
        peer = stub.GetRib(rib, _TIMEOUT_SECONDS).peers
        print(peer)

        '''
def run(gobgpd_addr, neighbor_addr):
    channel = implementations.insecure_channel(gobgpd_addr, 50051)
    with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:
        peer = stub.GetNeighbor(gobgp_pb2.Arguments(name=neighbor_addr), _TIMEOUT_SECONDS)
        print("BGP neighbor is %s, remote AS %d" % (peer.conf.neighbor_address, peer.conf.peer_as))
        print("  BGP version 4, remote router ID %s" % (peer.conf.id))
        print("  BGP state = %s, up for %s" % (peer.info.bgp_state, peer.timers.state.uptime))
        print("  BGP OutQ = %d, Flops = %d" % (peer.info.out_q, peer.info.flops))
        print("  Hold time is %d, keepalive interval is %d seconds" % (peer.timers.state.negotiated_hold_time, peer.timers.state.keepalive_interval))
        print("  Configured hold time is %d, keepalive interval is %d seconds" % (peer.timers.config.hold_time, peer.timers.config.keepalive_interval))
Ejemplo n.º 13
0
def run(gobgpd_addr, gobgpd_port, peer_address, peer_as):
    channel = implementations.insecure_channel(gobgpd_addr, gobgpd_port)

    peer = gobgp_pb2.Peer()
    peer.conf.neighbor_address = peer_address
    peer.conf.peer_as = peer_as

    with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:
        try:
            stub.AddNeighbor(gobgp_pb2.AddNeighborRequest(peer=peer), _TIMEOUT_SECONDS)
        except ExpirationError, e:
            print str(e)
            sys.exit(-1)
Ejemplo n.º 14
0
def run(gobgpd_addr):
    channel = implementations.insecure_channel(gobgpd_addr, 50051)
    with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:
        try:
            peers = stub.GetNeighbor(gobgp_pb2.GetNeighborRequest(), _TIMEOUT_SECONDS).peers
            for peer in peers:
                print("BGP neighbor is %s, remote AS %d" % (peer.conf.neighbor_address, peer.conf.peer_as))
                print("  BGP version 4, remote router ID %s" % (peer.conf.id))
                print("  BGP state = %s, up for %s" % (peer.info.bgp_state, peer.timers.state.uptime))
                print("  BGP OutQ = %d, Flops = %d" % (peer.info.out_q, peer.info.flops))
                print("  Hold time is %d, keepalive interval is %d seconds" % (peer.timers.state.negotiated_hold_time, peer.timers.state.keepalive_interval))
                print("  Configured hold time is %d, keepalive interval is %d seconds" % (peer.timers.config.hold_time, peer.timers.config.keepalive_interval))
        except ExpirationError, e:
            print str(e)
            sys.exit(-1)
def run(gobgpd_addr, neighbor_addr):
    channel = implementations.insecure_channel(gobgpd_addr, 50051)
    with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:
        peers = stub.MonitorPeerState(gobgp_pb2.Arguments(name=neighbor_addr), _TIMEOUT_SECONDS)

        if peers:
            def receive_signal(signum, stack):
                print('signal received:%d' % signum)
                peers.cancel()
                print('stream canceled')

            print('signal handler registered')

            for peer in peers:
                print("  BGP.info.bgp_state :%s" % ( peer.info.bgp_state))
Ejemplo n.º 16
0
    def modpath(self,gobgpd_addr,originate_path_list):
        channel = implementations.insecure_channel(gobgpd_addr, 50051)
        with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:
            paths = []
            for originate_path in originate_path_list:
                pattrs = []
                path = {}
                subnet = IPNetwork(originate_path['route'])
                ver = subnet.version
                addr = subnet.ip
                mask = subnet.prefixlen
                if ver == 4:
                    nlri = IPAddrPrefix(addr=addr, length=mask)
                    nexthop = BGPPathAttributeNextHop(value=originate_path['next_hop'])
                else :
                    nlri = IP6AddrPrefix(addr=addr, length=mask)
                    nexthop = BGPPathAttributeMpReachNLRI(next_hop = originate_path['next_hop'],nlri = [nlri],afi = AFI_IPV6 , safi = SAFI_UNICAST)

                bin_nlri = nlri.serialize()
                bin_nexthop = nexthop.serialize()

                pattrs.append(str(bin_nexthop))

                origin = BGPPathAttributeOrigin(value=ORIGIN)
                bin_origin = origin.serialize()
                pattrs.append(str(bin_origin))

                if originate_path['community'] :
                    community_set = self.community_convert(originate_path['community'])
                    communities = BGPPathAttributeCommunities(communities=community_set)
                    bin_communities = communities.serialize()
                    pattrs.append(str(bin_communities))

                #as_path = BGPPathAttributeAs4Path(value=[[1234,1111]])
                #bin_as_path = as_path.serialize()
                #pattrs.append(str(bin_as_path))

                path['nlri'] = str(bin_nlri)
                path['pattrs'] = pattrs

                paths.append(path)

            args = []
            args.append(gobgp_pb2.ModPathsArguments(resource=Resource_GLOBAL, paths=paths))
            ret = stub.ModPaths(args, _TIMEOUT_SECONDS)
def run(gobgpd_addr, routefamily):
    channel = implementations.insecure_channel(gobgpd_addr, 50051)
    with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:

        if routefamily:
            ribs = stub.MonitorBestChanged(gobgp_pb2.Arguments(family=routefamily), _TIMEOUT_SECONDS)
        else:
            ribs = stub.MonitorBestChanged(gobgp_pb2.Arguments(), _TIMEOUT_SECONDS)

        for rib in ribs:
            paths_target = rib.paths
            for path_target in paths_target:
                for pattr in path_target.pattrs:
                    path_attr = _PathAttribute.parser(pattr)
                    if isinstance(path_attr[0], BGPPathAttributeOrigin):
                        print (" Rib.origin     : %s" % path_attr[0].value)
                    elif isinstance(path_attr[0], BGPPathAttributeAsPath):
                        if path_attr[0].type == 2:
                            print(" Rib.aspath     : %s" % path_attr[0].value)
                        else:
                            print(" Rib.aspath     : ???")
                    elif isinstance(path_attr[0], BGPPathAttributeMultiExitDisc):
                        print (" Rib.med        : %s" % path_attr[0].value)
                    elif isinstance(path_attr[0], BGPPathAttributeExtendedCommunities):
                        for community in path_attr[0].communities:
                            if isinstance(community, BGPTwoOctetAsSpecificExtendedCommunity):
                                print(" Rib.community  : %s:%s" % ( community.as_number,
                                                                    community.local_administrator))
                            else:
                                print(" Rib.community  : ???")
                    elif isinstance(path_attr[0], BGPPathAttributeMpReachNLRI):
                        print (" Rib.prefix     : %s" % path_attr[0].nlri[0].prefix)
                        print (" Rib.route_dist : %s" % path_attr[0].nlri[0].route_dist)
                        print (" Rib.label_list : %s" % path_attr[0].nlri[0].label_list)
                        print (" Rib.nexthop    : %s" % path_attr[0].next_hop)
  

                print (" Rib.is_withdraw : %s" % path_target.is_withdraw)
                print (" Rib.best       : %s" % path_target.best)
                print "----------------------------"
Ejemplo n.º 18
0
def run(prefix, af, gobgpd_addr, withdraw=False, **kw):
    route_family = libgobgp.get_route_family(_AF_NAME[af])
    joined_args = prefix + " " + " ".join(
        map(lambda x: "{} {}".format(*x), kw.items()))
    serialized_path = libgobgp.serialize_path(
        route_family,
        joined_args,
    ).contents
    # nlri
    nlri = unpack_buf(serialized_path.nlri)
    # pattrs
    pattrs = []
    for pattr_p in serialized_path.path_attributes.contents[:serialized_path.
                                                            path_attributes_len]:
        pattrs.append(unpack_buf(pattr_p.contents))
    # path dict
    path = dict([
        ("family", route_family),
        ("nlri", nlri),
        ("pattrs", pattrs),
    ])
    # grpc request
    channel = implementations.insecure_channel(gobgpd_addr, 50051)
    try:
        with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:
            if not withdraw:
                res = stub.AddPath(gobgp_pb2.AddPathRequest(path=path),
                                   _TIMEOUT_SECONDS)
                # AddPathResponse uuid seems to have become empty since uuid became a member of Path structure.
                if res.uuid:
                    print str(UUID(bytes=res.uuid))
            else:
                path["is_withdraw"] = True
                res = stub.DeletePath(gobgp_pb2.DeletePathRequest(path=path),
                                      _TIMEOUT_SECONDS)
    except ExpirationError:
        print >> sys.stderr, "grpc request timed out!"
    except:
        traceback.print_exc()
Ejemplo n.º 19
0
def run(af, gobgpd_addr, *prefixes, **kw):
    table_args = dict()
    #table_args["family"] = libgobgp.get_route_family(_AF_NAME[af])
    table_args["family"] = 1
    #table_args["destinations"] =[ gobgp_pb2.Destination(prefix=p) for p in prefixes ]
    kw["rib_in_neighbor"] = "172.16.2.10"
    if kw["rib_in_neighbor"] is not None:
        table_args["type"] = gobgp_pb2.ADJ_IN
        table_args["name"] = kw["rib_in_neighbor"]
    else:
        table_args["type"] = gobgp_pb2.GLOBAL
    # grpc request
    channel = implementations.insecure_channel(gobgpd_addr, 50051)

    try:
        with gobgp_pb2.beta_create_GobgpApi_stub(channel) as stub:
            response_table = stub.GetRib(gobgp_pb2.GetRibRequest(table=gobgp_pb2.Table(**table_args)),_TIMEOUT_SECONDS).table
            print (response_table)
    except ExpirationError:
        print >> sys.stderr, "grpc request timed out!"
    except RemoteError as e:
        print >> sys.stderr, "grpc stub method failed:", e.details
    except:
        traceback.print_exc()
    else:
        if prefixes:
            for prefix in prefixes:
                try:
                    i = map(lambda d: d.prefix, response_table.destinations).index(prefix)
                    print_rib(response_table.destinations[i])
                except ValueError:
                    print >> sys.stderr, prefix
                    print >> sys.stderr, "  not in table!"
        else:
            for pb2_dest in response_table.destinations:
                print_rib(pb2_dest)