From 7a0bbc22d8ec47daad3b0a2c77d400bcdf98d214 Mon Sep 17 00:00:00 2001 From: ISHIDA Wataru Date: Thu, 10 Dec 2015 22:16:12 +0900 Subject: docs: add a document about how to use gobgp as a bgp library Signed-off-by: ISHIDA Wataru --- README.md | 1 + docs/sources/lib.md | 85 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 docs/sources/lib.md diff --git a/README.md b/README.md index 486802c6..97886d49 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,7 @@ No dependency hell (library, package, etc) thanks to Go. * [Flowspec](https://github.com/osrg/gobgp/blob/master/docs/sources/flowspec.md) * [RPKI](https://github.com/osrg/gobgp/blob/master/docs/sources/rpki.md) * [Managing GoBGP with your favorite language](https://github.com/osrg/gobgp/blob/master/docs/sources/grpc-client.md) + * [Using GoBGP as a Go Native BGP library](https://github.com/osrg/gobgp/blob/master/docs/sources/lib.md) ## Community, discussion and support diff --git a/docs/sources/lib.md b/docs/sources/lib.md new file mode 100644 index 00000000..a8e55894 --- /dev/null +++ b/docs/sources/lib.md @@ -0,0 +1,85 @@ +# GoBGP as a Go Native BGP library + +This page explains how to use GoBGP as a Go Native BGP library. + +## Contents +- [Basic Example](#basic) + +## Basic Example + +```go +package main + +import ( + log "github.com/Sirupsen/logrus" + api "github.com/osrg/gobgp/api" + "github.com/osrg/gobgp/gobgp/cmd" + "github.com/osrg/gobgp/packet" + gobgp "github.com/osrg/gobgp/server" +) + +func main() { + log.SetLevel(log.DebugLevel) + s := gobgp.NewBgpServer() + go s.Serve() + + // start grpc api server. this is not mandatory + // but you will be able to use `gobgp` cmd with this. + g := gobgp.NewGrpcServer(gobgp.GRPC_PORT, s.GrpcReqCh) + go g.Serve() + + // global configuration + req := gobgp.NewGrpcRequest(gobgp.REQ_MOD_GLOBAL_CONFIG, "", bgp.RouteFamily(0), &api.ModGlobalConfigArguments{ + Operation: api.Operation_ADD, + Global: &api.Global{ + As: 65003, + RouterId: "192.168.0.4", + Port: -1, // gobgp won't listen on tcp:179 + }, + }) + s.GrpcReqCh <- req + res := <-req.ResponseCh + if err := res.Err(); err != nil { + log.Fatal(err) + } + + // neighbor configuration + req = gobgp.NewGrpcRequest(gobgp.REQ_MOD_NEIGHBOR, "", bgp.RouteFamily(0), &api.ModNeighborArguments{ + Operation: api.Operation_ADD, + Peer: &api.Peer{ + Conf: &api.PeerConf{ + NeighborAddress: "192.168.0.3", + PeerAs: 65000, + }, + Transport: &api.Transport{ + LocalAddress: "192.168.0.4", + }, + }, + }) + s.GrpcReqCh <- req + res = <-req.ResponseCh + if err := res.Err(); err != nil { + log.Fatal(err) + } + + // add routes + path, _ := cmd.ParsePath(bgp.RF_IPv4_UC, []string{"10.0.0.0/24", "nexthop", "10.10.10.10"}) + req = gobgp.NewGrpcRequest(gobgp.REQ_MOD_PATH, "", bgp.RouteFamily(0), &api.ModPathArguments{ + Resource: api.Resource_GLOBAL, + Paths: []*api.Path{path}, + }) + s.GrpcReqCh <- req + res = <-req.ResponseCh + if err := res.Err(); err != nil { + log.Fatal(err) + } + + // monitor new routes + req = gobgp.NewGrpcRequest(gobgp.REQ_MONITOR_GLOBAL_BEST_CHANGED, "", bgp.RF_IPv4_UC, nil) + s.GrpcReqCh <- req + for res := range req.ResponseCh { + p, _ := cmd.ApiStruct2Path(res.Data.(*api.Destination).Paths[0]) + cmd.ShowRoute(p, false, false, false, true, false) + } +} +``` -- cgit v1.2.3