-
Notifications
You must be signed in to change notification settings - Fork 114
/
grpc_plugin.go
313 lines (277 loc) · 8.45 KB
/
grpc_plugin.go
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
// Copyright (c) 2019 Cisco and/or its affiliates.
//
// 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 rpc
import (
"golang.org/x/net/context"
controller "github.com/contiv/vpp/plugins/controller/api"
"github.com/contiv/vpp/plugins/grpc/rpc"
"github.com/ligato/cn-infra/db/keyval"
"github.com/ligato/cn-infra/infra"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/cn-infra/rpc/grpc"
"github.com/ligato/vpp-agent/api/models/linux/interfaces"
"github.com/ligato/vpp-agent/api/models/linux/l3"
"github.com/ligato/vpp-agent/api/models/vpp/acl"
"github.com/ligato/vpp-agent/api/models/vpp/interfaces"
"github.com/ligato/vpp-agent/api/models/vpp/ipsec"
"github.com/ligato/vpp-agent/api/models/vpp/l2"
"github.com/ligato/vpp-agent/api/models/vpp/l3"
"github.com/ligato/vpp-agent/api/models/vpp/nat"
"github.com/ligato/vpp-agent/api/models/vpp/punt"
"github.com/ligato/vpp-agent/pkg/models"
)
//go:generate protoc --proto_path=rpc --proto_path=$GOPATH/src --gogo_out=plugins=grpc:rpc rpc/rpc.proto
// Plugin implements GRPC access to Contiv's VPP-agent.
type Plugin struct {
Deps
localBroker keyval.ProtoBroker
// Services
changeSvc ChangeSvc
resyncSvc ResyncSvc
}
// Deps - dependencies of Plugin
type Deps struct {
infra.PluginDeps
GRPCServer grpc.Server
EventLoop controller.EventLoop
LocalDB keyval.KvProtoPlugin
}
// ChangeSvc implements DataChangeService.
type ChangeSvc struct {
log logging.Logger
plugin *Plugin
}
// ResyncSvc implements DataResyncService.
type ResyncSvc struct {
log logging.Logger
plugin *Plugin
}
// Init registers GRPC services.
func (p *Plugin) Init() error {
// create broker to local DB for config persisting
p.localBroker = p.LocalDB.NewBroker("")
// init service handlers
p.changeSvc.log = p.Log.NewLogger("grpcChangeSvc")
p.changeSvc.plugin = p
p.resyncSvc.log = p.Log.NewLogger("grpcResyncSvc")
p.resyncSvc.plugin = p
// Register all GRPC services if server is available.
// Register needs to be done before 'ListenAndServe' is called in GRPC plugin
grpcServer := p.GRPCServer.GetServer()
if grpcServer != nil {
rpc.RegisterDataChangeServiceServer(grpcServer, &p.changeSvc)
rpc.RegisterDataResyncServiceServer(grpcServer, &p.resyncSvc)
}
return nil
}
// GetConfigSnapshot returns full configuration snapshot that is currently
// required by the GRPC client to be applied.
func (p *Plugin) GetConfigSnapshot() (controller.KeyValuePairs, error) {
extConfig := make(controller.KeyValuePairs)
iterator, err := p.localBroker.ListValues("")
if err != nil {
return extConfig, err
}
for {
kv, stop := iterator.GetNext()
if stop {
break
}
key := kv.GetKey()
value, err := models.UnmarshalLazyValue(key, kv)
if err != nil {
p.Log.Warnf("Failed to de-serialize value received from GRPC for key: %s", key)
continue
}
extConfig[key] = value
}
iterator.Close()
return extConfig, nil
}
func (p *Plugin) dumpLocalDB() {
config, err := p.GetConfigSnapshot()
if err != nil {
p.Log.Errorf("Failed to dump local DB: %v", err)
return
}
p.Log.Debugf("GRPC local DB dump: %v", config)
}
// Close does nothing.
func (p *Plugin) Close() error {
return nil
}
// Put propagates request from GRPC client to add/modify some external configuration items.
func (svc *ChangeSvc) Put(ctx context.Context, data *rpc.DataRequest) (*rpc.PutResponse, error) {
// prepare configuration changes
config := buildConfig(data, false)
// persist changes
for key, value := range config {
err := svc.plugin.localBroker.Put(key, value)
if err != nil {
svc.log.Warnf("Failed to persist changes: %v", err)
}
}
// execute changes
event := controller.NewExternalConfigChange(svc.plugin.String(), true)
event.UpdatedKVs = config
err := svc.plugin.EventLoop.PushEvent(event)
if err == nil {
err = event.Wait()
}
return &rpc.PutResponse{}, err
}
// Del propagates request from GRPC client to remove some external configuration items.
func (svc *ChangeSvc) Del(ctx context.Context, data *rpc.DataRequest) (*rpc.DelResponse, error) {
// prepare configuration changes
config := buildConfig(data, true)
// persist changes
for key := range config {
_, err := svc.plugin.localBroker.Delete(key)
if err != nil {
svc.log.Warnf("Failed to persist changes: %v", err)
}
}
// execute changes
event := controller.NewExternalConfigChange(svc.plugin.String(), true)
event.UpdatedKVs = config
err := svc.plugin.EventLoop.PushEvent(event)
if err == nil {
err = event.Wait()
}
return &rpc.DelResponse{}, err
}
// Resync re-synchronizes configuration between the GRPC client and vpp-agent.
func (svc *ResyncSvc) Resync(ctx context.Context, data *rpc.DataRequest) (*rpc.ResyncResponse, error) {
// prepare configuration changes
config := buildConfig(data, false)
// resync local DB
err := svc.resyncDB(config)
if err != nil {
svc.log.Warnf("Failed to resync local DB: %v", err)
}
// execute changes
event := controller.NewExternalConfigResync(svc.plugin.String(), true)
event.ExternalConfig = config
err = svc.plugin.EventLoop.PushEvent(event)
if err == nil {
err = event.Wait()
}
return &rpc.ResyncResponse{}, err
}
func (svc *ResyncSvc) resyncDB(resyncCfg controller.KeyValuePairs) error {
keys := make(map[string]struct{})
// update database with values present in resyncCfg
for key, value := range resyncCfg {
keys[key] = struct{}{}
err := svc.plugin.localBroker.Put(key, value)
if err != nil {
return err
}
}
// read keys currently stored in DB, remove the obsolete ones
snapshot, err := svc.plugin.GetConfigSnapshot()
if err != nil {
return err
}
for key := range snapshot {
if _, inResyncCfg := keys[key]; !inResyncCfg {
_, err = svc.plugin.localBroker.Delete(key)
if err != nil {
return err
}
}
}
return nil
}
func buildConfig(data *rpc.DataRequest, delete bool) controller.KeyValuePairs {
extConfig := make(controller.KeyValuePairs)
for _, item := range data.AccessLists {
key := vpp_acl.Key(item.Name)
extConfig[key] = item
}
for _, item := range data.Interfaces {
key := vpp_interfaces.InterfaceKey(item.Name)
extConfig[key] = item
}
for _, item := range data.BridgeDomains {
key := vpp_l2.BridgeDomainKey(item.Name)
extConfig[key] = item
}
for _, item := range data.FIBs {
key := vpp_l2.FIBKey(item.BridgeDomain, item.PhysAddress)
extConfig[key] = item
}
for _, item := range data.XCons {
key := vpp_l2.XConnectKey(item.ReceiveInterface)
extConfig[key] = item
}
for _, item := range data.StaticRoutes {
key := vpp_l3.RouteKey(item.VrfId, item.DstNetwork, item.NextHopAddr)
extConfig[key] = item
}
for _, item := range data.ArpEntries {
key := vpp_l3.ArpEntryKey(item.Interface, item.IpAddress)
extConfig[key] = item
}
if data.ProxyArp != nil {
key := vpp_l3.ProxyARPKey()
extConfig[key] = data.ProxyArp
}
if data.IPScanNeighbor != nil {
key := vpp_l3.IPScanNeighborKey()
extConfig[key] = data.IPScanNeighbor
}
for _, item := range data.SAs {
key := vpp_ipsec.SAKey(item.Index)
extConfig[key] = item
}
for _, item := range data.SPDs {
key := vpp_ipsec.SPDKey(item.Index)
extConfig[key] = item
}
for _, item := range data.IPRedirectPunts {
key := vpp_punt.IPRedirectKey(item.L3Protocol, item.TxInterface)
extConfig[key] = item
}
for _, item := range data.ToHostPunts {
key := vpp_punt.ToHostKey(item.L3Protocol, item.L4Protocol, item.Port)
extConfig[key] = item
}
if data.NatGlobal != nil {
key := vpp_nat.GlobalNAT44Key()
extConfig[key] = data.NatGlobal
}
for _, item := range data.DNATs {
key := vpp_nat.DNAT44Key(item.Label)
extConfig[key] = item
}
for _, item := range data.LinuxInterfaces {
key := linux_interfaces.InterfaceKey(item.Name)
extConfig[key] = item
}
for _, item := range data.LinuxArpEntries {
key := linux_l3.ArpKey(item.Interface, item.IpAddress)
extConfig[key] = item
}
for _, item := range data.LinuxRoutes {
key := linux_l3.RouteKey(item.DstNetwork, item.OutgoingInterface)
extConfig[key] = item
}
if delete {
for key := range extConfig {
extConfig[key] = nil
}
}
return extConfig
}