diff options
author | ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp> | 2015-12-10 22:16:12 +0900 |
---|---|---|
committer | FUJITA Tomonori <fujita.tomonori@lab.ntt.co.jp> | 2015-12-15 12:09:51 +0900 |
commit | 7a0bbc22d8ec47daad3b0a2c77d400bcdf98d214 (patch) | |
tree | 170064c0aeb4cc8363bf5bbb3fb7335d84e3b784 | |
parent | af8c2a1b16c7b8092dbf31c21f7919f0de1ee379 (diff) |
docs: add a document about how to use gobgp as a bgp library
Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
-rw-r--r-- | README.md | 1 | ||||
-rw-r--r-- | docs/sources/lib.md | 85 |
2 files changed, 86 insertions, 0 deletions
@@ -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) + +## <a name="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) + } +} +``` |