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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
|
// Copyright (C) 2014,2015 Nippon Telegraph and Telephone Corporation.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package api
import (
"fmt"
log "github.com/Sirupsen/logrus"
"github.com/osrg/gobgp/packet"
"golang.org/x/net/context"
"google.golang.org/grpc"
"io"
"net"
)
const (
_ = iota
REQ_NEIGHBOR
REQ_NEIGHBORS
REQ_ADJ_RIB_IN
REQ_ADJ_RIB_OUT
REQ_LOCAL_RIB
REQ_NEIGHBOR_SHUTDOWN
REQ_NEIGHBOR_RESET
REQ_NEIGHBOR_SOFT_RESET
REQ_NEIGHBOR_SOFT_RESET_IN
REQ_NEIGHBOR_SOFT_RESET_OUT
REQ_NEIGHBOR_ENABLE
REQ_NEIGHBOR_DISABLE
REQ_GLOBAL_RIB
REQ_GLOBAL_ADD
REQ_GLOBAL_DELETE
)
const GRPC_PORT = 8080
type Server struct {
grpcServer *grpc.Server
bgpServerCh chan *GrpcRequest
}
func (s *Server) Serve() error {
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", GRPC_PORT))
if err != nil {
return fmt.Errorf("failed to listen: %v", err)
}
s.grpcServer.Serve(lis)
return nil
}
func (s *Server) GetNeighbor(ctx context.Context, arg *Arguments) (*Peer, error) {
var rf bgp.RouteFamily
req := NewGrpcRequest(REQ_NEIGHBOR, arg.RouterId, rf, nil)
s.bgpServerCh <- req
res := <-req.ResponseCh
if err := res.Err(); err != nil {
log.Debug(err.Error())
return nil, err
}
return res.Data.(*Peer), nil
}
func (s *Server) GetNeighbors(_ *Arguments, stream Grpc_GetNeighborsServer) error {
var rf bgp.RouteFamily
req := NewGrpcRequest(REQ_NEIGHBORS, "", rf, nil)
s.bgpServerCh <- req
for res := range req.ResponseCh {
if err := res.Err(); err != nil {
log.Debug(err.Error())
return err
}
if err := stream.Send(res.Data.(*Peer)); err != nil {
return err
}
}
return nil
}
func (s *Server) GetAdjRib(arg *Arguments, stream Grpc_GetAdjRibServer) error {
var reqType int
switch arg.Resource {
case Resource_ADJ_IN:
reqType = REQ_ADJ_RIB_IN
case Resource_ADJ_OUT:
reqType = REQ_ADJ_RIB_OUT
default:
return fmt.Errorf("unsupported resource type: %v", arg.Resource)
}
var rf bgp.RouteFamily
switch arg.Af {
case AddressFamily_IPV4:
rf = bgp.RF_IPv4_UC
case AddressFamily_IPV6:
rf = bgp.RF_IPv6_UC
case AddressFamily_EVPN:
rf = bgp.RF_EVPN
default:
return fmt.Errorf("unsupported resource type: %v", arg.Af)
}
req := NewGrpcRequest(reqType, arg.RouterId, rf, nil)
s.bgpServerCh <- req
for res := range req.ResponseCh {
if err := res.Err(); err != nil {
log.Debug(err.Error())
return err
}
if err := stream.Send(res.Data.(*Path)); err != nil {
return err
}
}
return nil
}
func (s *Server) GetRib(arg *Arguments, stream Grpc_GetRibServer) error {
var reqType int
switch arg.Resource {
case Resource_LOCAL:
reqType = REQ_LOCAL_RIB
case Resource_GLOBAL:
reqType = REQ_GLOBAL_RIB
default:
return fmt.Errorf("unsupported resource type: %v", arg.Resource)
}
var rf bgp.RouteFamily
switch arg.Af {
case AddressFamily_IPV4:
rf = bgp.RF_IPv4_UC
case AddressFamily_IPV6:
rf = bgp.RF_IPv6_UC
case AddressFamily_EVPN:
rf = bgp.RF_EVPN
default:
return fmt.Errorf("unsupported resource type: %v", arg.Af)
}
req := NewGrpcRequest(reqType, arg.RouterId, rf, nil)
s.bgpServerCh <- req
for res := range req.ResponseCh {
if err := res.Err(); err != nil {
log.Debug(err.Error())
return err
}
if err := stream.Send(res.Data.(*Destination)); err != nil {
return err
}
}
return nil
}
func (s *Server) neighbor(reqType int, arg *Arguments) (*Error, error) {
var rf bgp.RouteFamily
switch arg.Af {
case AddressFamily_IPV4:
rf = bgp.RF_IPv4_UC
case AddressFamily_IPV6:
rf = bgp.RF_IPv6_UC
case AddressFamily_EVPN:
rf = bgp.RF_EVPN
default:
return nil, fmt.Errorf("unsupported resource type: %v", arg.Af)
}
none := &Error{}
req := NewGrpcRequest(reqType, arg.RouterId, rf, nil)
s.bgpServerCh <- req
res := <-req.ResponseCh
if err := res.Err(); err != nil {
log.Debug(err.Error())
return nil, err
}
return none, nil
}
func (s *Server) Reset(ctx context.Context, arg *Arguments) (*Error, error) {
return s.neighbor(REQ_NEIGHBOR_RESET, arg)
}
func (s *Server) SoftReset(ctx context.Context, arg *Arguments) (*Error, error) {
return s.neighbor(REQ_NEIGHBOR_SOFT_RESET, arg)
}
func (s *Server) SoftResetIn(ctx context.Context, arg *Arguments) (*Error, error) {
return s.neighbor(REQ_NEIGHBOR_SOFT_RESET_IN, arg)
}
func (s *Server) SoftResetOut(ctx context.Context, arg *Arguments) (*Error, error) {
return s.neighbor(REQ_NEIGHBOR_SOFT_RESET_OUT, arg)
}
func (s *Server) Shutdown(ctx context.Context, arg *Arguments) (*Error, error) {
return s.neighbor(REQ_NEIGHBOR_SHUTDOWN, arg)
}
func (s *Server) Enable(ctx context.Context, arg *Arguments) (*Error, error) {
return s.neighbor(REQ_NEIGHBOR_ENABLE, arg)
}
func (s *Server) Disable(ctx context.Context, arg *Arguments) (*Error, error) {
return s.neighbor(REQ_NEIGHBOR_DISABLE, arg)
}
func (s *Server) modPath(reqType int, stream grpc.ServerStream) error {
for {
var err error
var arg *Arguments
if reqType == REQ_GLOBAL_ADD {
arg, err = stream.(Grpc_AddPathServer).Recv()
} else if reqType == REQ_GLOBAL_DELETE {
arg, err = stream.(Grpc_DeletePathServer).Recv()
} else {
return fmt.Errorf("unsupportd req: %d", reqType)
}
if err == io.EOF {
return nil
} else if err != nil {
return err
}
if arg.Resource != Resource_GLOBAL {
return fmt.Errorf("unsupported resource: %s", arg.Resource)
}
prefix := make(map[string]interface{}, 1)
prefix["prefix"] = arg.Prefix
var rf bgp.RouteFamily
switch arg.Af {
case AddressFamily_IPV4:
rf = bgp.RF_IPv4_UC
case AddressFamily_IPV6:
rf = bgp.RF_IPv6_UC
case AddressFamily_EVPN:
rf = bgp.RF_EVPN
default:
return fmt.Errorf("unsupported resource type: %v", arg.Af)
}
req := NewGrpcRequest(reqType, arg.RouterId, rf, prefix)
s.bgpServerCh <- req
res := <-req.ResponseCh
if err := res.Err(); err != nil {
log.Debug(err.Error())
return err
}
}
}
func (s *Server) AddPath(stream Grpc_AddPathServer) error {
return s.modPath(REQ_GLOBAL_ADD, stream)
}
func (s *Server) DeletePath(stream Grpc_DeletePathServer) error {
return s.modPath(REQ_GLOBAL_DELETE, stream)
}
type GrpcRequest struct {
RequestType int
RemoteAddr string
RouteFamily bgp.RouteFamily
ResponseCh chan *GrpcResponse
Err error
Data map[string]interface{}
}
func NewGrpcRequest(reqType int, remoteAddr string, rf bgp.RouteFamily, d map[string]interface{}) *GrpcRequest {
r := &GrpcRequest{
RequestType: reqType,
RouteFamily: rf,
RemoteAddr: remoteAddr,
ResponseCh: make(chan *GrpcResponse),
Data: d,
}
return r
}
type GrpcResponse struct {
ResponseErr error
Data interface{}
}
func (r *GrpcResponse) Err() error {
return r.ResponseErr
}
func NewGrpcServer(port int, bgpServerCh chan *GrpcRequest) *Server {
grpcServer := grpc.NewServer()
server := &Server{
grpcServer: grpcServer,
bgpServerCh: bgpServerCh,
}
RegisterGrpcServer(grpcServer, server)
return server
}
|