summaryrefslogtreecommitdiffhomepage
path: root/docs/sources/lib.md
blob: b1f80ca1ce763aabb3f337e3c36e4b1557a0a20c (plain)
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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/bgp"
	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(s, ":50051", s.GrpcReqCh)
	go g.Serve()

	// global configuration
	req := gobgp.NewGrpcRequest(gobgp.REQ_START_SERVER, "", bgp.RouteFamily(0), &api.StartServerRequest{
		Global: &api.Global{
			As:         65003,
			RouterId:   "192.168.0.4",
			ListenPort: -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_ADD_NEIGHBOR, "", bgp.RouteFamily(0), &api.AddNeighborRequest{
		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_ADD_PATH, "", bgp.RouteFamily(0), &api.AddPathRequest{
		Resource: api.Resource_GLOBAL,
		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_RIB, "", bgp.RF_IPv4_UC, &api.Table{
		Type: api.Resource_GLOBAL,
	})
	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)
	}
}
```