forked from ligato/vpp-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
395 lines (352 loc) · 12.4 KB
/
main.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright (c) 2017 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 main
import (
"context"
"log"
"net"
"os"
"sync"
"time"
"github.com/ligato/cn-infra/logging"
"github.com/ligato/cn-infra/logging/logrus"
"github.com/ligato/cn-infra/utils/safeclose"
"github.com/ligato/vpp-agent/clientv1/vpp/remoteclient"
"github.com/ligato/vpp-agent/plugins/vpp/model/acl"
"github.com/ligato/vpp-agent/plugins/vpp/model/interfaces"
"github.com/ligato/vpp-agent/plugins/vpp/model/l2"
"github.com/ligato/vpp-agent/plugins/vpp/model/l3"
"github.com/ligato/vpp-agent/plugins/vpp/model/rpc"
"fmt"
"github.com/ligato/cn-infra/agent"
"github.com/namsral/flag"
"google.golang.org/grpc"
)
const (
defaultAddress = "localhost:9111"
defaultSocket = "tcp"
)
var address = defaultAddress
var socketType string
// init sets the default logging level
func init() {
logrus.DefaultLogger().SetOutput(os.Stdout)
logrus.DefaultLogger().SetLevel(logging.DebugLevel)
}
/********
* Main *
********/
// Start Agent plugins selected for this example.
func main() {
flag.StringVar(&address, "address", defaultAddress, "address of GRPC server")
flag.StringVar(&socketType, "socket-type", defaultSocket, "socket type [tcp, tcp4, tcp6, unix, unixpacket]")
//Init close channel to stop the example.
exampleFinished := make(chan struct{}, 1)
// Inject dependencies to example plugin
ep := &ExamplePlugin{}
// Start Agent
a := agent.NewAgent(
agent.AllPlugins(ep),
agent.QuitOnClose(exampleFinished),
)
if err := a.Run(); err != nil {
log.Fatal()
}
// End when the localhost example is finished.
go closeExample("localhost example finished", exampleFinished)
}
// Stop the agent with desired info message.
func closeExample(message string, exampleFinished chan struct{}) {
time.Sleep(25 * time.Second)
logrus.DefaultLogger().Info(message)
close(exampleFinished)
}
/******************
* Example plugin *
******************/
// PluginName represents name of plugin.
const PluginName = "grpc-config-example"
// ExamplePlugin demonstrates the use of the remoteclient to locally transport example configuration into the default VPP plugins.
type ExamplePlugin struct {
wg sync.WaitGroup
cancel context.CancelFunc
conn *grpc.ClientConn
}
// Init initializes example plugin.
func (plugin *ExamplePlugin) Init() (err error) {
// Set up connection to the server.
switch socketType {
case "tcp", "tcp4", "tcp6", "unix", "unixpacket":
plugin.conn, err = grpc.Dial("unix", grpc.WithInsecure(),
grpc.WithDialer(dialer(socketType, address, 2*time.Second)))
default:
return fmt.Errorf("unknown gRPC socket type: %s", socketType)
}
// Apply initial VPP configuration.
plugin.resyncVPP()
// Schedule reconfiguration.
var ctx context.Context
ctx, plugin.cancel = context.WithCancel(context.Background())
plugin.wg.Add(1)
go plugin.reconfigureVPP(ctx)
logrus.DefaultLogger().Info("Initialization of the example plugin has completed")
return nil
}
// Close cleans up the resources.
func (plugin *ExamplePlugin) Close() error {
plugin.cancel()
plugin.wg.Wait()
err := safeclose.Close(plugin.conn)
if err != nil {
return err
}
logrus.DefaultLogger().Info("Closed example plugin")
return nil
}
// String returns plugin name
func (plugin *ExamplePlugin) String() string {
return PluginName
}
// Dialer for unix domain socket
func dialer(socket, address string, timeoutVal time.Duration) func(string, time.Duration) (net.Conn, error) {
return func(addr string, timeout time.Duration) (net.Conn, error) {
// Pass values
addr, timeout = address, timeoutVal
// Dial with timeout
return net.DialTimeout(socket, addr, timeoutVal)
}
}
// resyncVPP propagates snapshot of the whole initial configuration to VPP plugins.
func (plugin *ExamplePlugin) resyncVPP() {
err := remoteclient.DataResyncRequestGRPC(rpc.NewDataResyncServiceClient(plugin.conn)).
Interface(&memif1AsMaster).
Interface(&tap1Disabled).
Interface(&loopback1).
StaticRoute(&routeThroughMemif1).
Send().ReceiveReply()
if err != nil {
logrus.DefaultLogger().Errorf("Failed to apply initial VPP configuration: %v", err)
} else {
logrus.DefaultLogger().Info("Successfully applied initial VPP configuration")
}
}
// reconfigureVPP simulates a set of changes in the configuration related to VPP plugins.
func (plugin *ExamplePlugin) reconfigureVPP(ctx context.Context) {
_, dstNetAddr, err := net.ParseCIDR("192.168.2.1/32")
if err != nil {
return
}
nextHopAddr := net.ParseIP("192.168.1.1")
select {
case <-time.After(3 * time.Second):
// Simulate configuration change exactly 15seconds after resync.
err := remoteclient.DataChangeRequestGRPC(rpc.NewDataChangeServiceClient(plugin.conn)).
Put().
Interface(&memif1AsSlave). /* turn memif1 into slave, remove the IP address */
Interface(&memif2). /* newly added memif interface */
Interface(&tap1Enabled). /* enable tap1 interface */
Interface(&loopback1WithAddr). /* assign IP address to loopback1 interface */
ACL(&acl1). /* declare ACL for the traffic leaving tap1 interface */
XConnect(&XConMemif1ToMemif2). /* xconnect memif interfaces */
BD(&BDLoopback1ToTap1). /* put loopback and tap1 into the same bridge domain */
Delete().
StaticRoute(0, dstNetAddr.String(), nextHopAddr.String()). /* remove the route going through memif1 */
Send().ReceiveReply()
if err != nil {
logrus.DefaultLogger().Errorf("Failed to reconfigure VPP: %v", err)
} else {
logrus.DefaultLogger().Info("Successfully reconfigured VPP")
}
case <-ctx.Done():
// Cancel the scheduled re-configuration.
logrus.DefaultLogger().Info("Planned VPP re-configuration was canceled")
}
plugin.wg.Done()
}
/*************************
* Example plugin config *
*************************/
/*****************************************************
* After Resync *
* *
* +---------------------------------------------+ *
* | | *
* +-----------+ +---------------------+ *
* | tap1 | | memif1 | *
* | DISABLED | +--> | MASTER | *
* +-----------+ | | IP: 192.168.1.1/24 | *
* | | +---------------------+ *
* | +-----------+ | | *
* | | loopback1 | + | *
* | +-----------+ route for 192.168.2.1 | *
* | | *
* +---------------------------------------------+ *
* *
*****************************************************/
/********************************************************
* After Data Change Request *
* *
* +------------------------------------------------+ *
* | | *
* +---------+ +------+ +----------+ *
* | tap1 |-| acl1 |-+ +------| memif1 | *
* | ENABLED | +------+ | | | SLAVE | *
* +---------+ | | +----------+ *
* | Bridge xconnect | *
* | domain | +----------+ *
* | | | | memif2 | *
* | +------------+ | +------| SLAVE | *
* | | loopback1 |----+ +----------| *
* | +------------+ | *
* | | *
* +------------------------------------------------+ *
* *
********************************************************/
var (
// memif1AsMaster is an example of a memory interface configuration. (Master=true, with IPv4 address).
memif1AsMaster = interfaces.Interfaces_Interface{
Name: "memif1",
Type: interfaces.InterfaceType_MEMORY_INTERFACE,
Enabled: true,
Memif: &interfaces.Interfaces_Interface_Memif{
Id: 1,
Master: true,
SocketFilename: "/tmp/memif1.sock",
},
Mtu: 1500,
IpAddresses: []string{"192.168.1.1/24"},
}
// memif1AsSlave is the original memif1 turned into slave and stripped of the IP address.
memif1AsSlave = interfaces.Interfaces_Interface{
Name: "memif1",
Type: interfaces.InterfaceType_MEMORY_INTERFACE,
Enabled: true,
Memif: &interfaces.Interfaces_Interface_Memif{
Id: 1,
Master: false,
SocketFilename: "/tmp/memif1.sock",
},
Mtu: 1500,
}
// Memif2 is a slave memif without IP address and to be xconnected with memif1.
memif2 = interfaces.Interfaces_Interface{
Name: "memif2",
Type: interfaces.InterfaceType_MEMORY_INTERFACE,
Enabled: true,
Memif: &interfaces.Interfaces_Interface_Memif{
Id: 2,
Master: false,
SocketFilename: "/tmp/memif2.sock",
},
Mtu: 1500,
}
// XConMemif1ToMemif2 defines xconnect between memifs.
XConMemif1ToMemif2 = l2.XConnectPairs_XConnectPair{
ReceiveInterface: memif1AsSlave.Name,
TransmitInterface: memif2.Name,
}
// tap1Disabled is a disabled tap interface.
tap1Disabled = interfaces.Interfaces_Interface{
Name: "tap1",
Type: interfaces.InterfaceType_TAP_INTERFACE,
Enabled: false,
Tap: &interfaces.Interfaces_Interface_Tap{
HostIfName: "linux-tap1",
},
Mtu: 1500,
}
// tap1Enabled is an enabled tap1 interface.
tap1Enabled = interfaces.Interfaces_Interface{
Name: "tap1",
Type: interfaces.InterfaceType_TAP_INTERFACE,
Enabled: true,
Tap: &interfaces.Interfaces_Interface_Tap{
HostIfName: "linux-tap1",
},
Mtu: 1500,
}
acl1 = acl.AccessLists_Acl{
AclName: "acl1",
Rules: []*acl.AccessLists_Acl_Rule{
{
RuleName: "rule1",
AclAction: acl.AclAction_DENY,
Match: &acl.AccessLists_Acl_Rule_Match{
IpRule: &acl.AccessLists_Acl_Rule_Match_IpRule{
Ip: &acl.AccessLists_Acl_Rule_Match_IpRule_Ip{
DestinationNetwork: "10.1.1.0/24",
SourceNetwork: "10.1.2.0/24",
},
Tcp: &acl.AccessLists_Acl_Rule_Match_IpRule_Tcp{
DestinationPortRange: &acl.AccessLists_Acl_Rule_Match_IpRule_PortRange{
LowerPort: 50,
UpperPort: 150,
},
SourcePortRange: &acl.AccessLists_Acl_Rule_Match_IpRule_PortRange{
LowerPort: 1000,
UpperPort: 2000,
},
},
},
},
},
},
Interfaces: &acl.AccessLists_Acl_Interfaces{
Egress: []string{"tap1"},
},
}
// loopback1 is an example of a loopback interface configuration (without IP address assigned).
loopback1 = interfaces.Interfaces_Interface{
Name: "loopback1",
Type: interfaces.InterfaceType_SOFTWARE_LOOPBACK,
Enabled: true,
Mtu: 1500,
}
// loopback1WithAddr extends loopback1 definition with an IP address.
loopback1WithAddr = interfaces.Interfaces_Interface{
Name: "loopback1",
Type: interfaces.InterfaceType_SOFTWARE_LOOPBACK,
Enabled: true,
Mtu: 1500,
IpAddresses: []string{"10.0.0.1/24"},
}
// BDLoopback1ToTap1 is a bridge domain with tap1 and loopback1 interfaces in it.
// Loopback is set to be BVI.
BDLoopback1ToTap1 = l2.BridgeDomains_BridgeDomain{
Name: "br1",
Flood: false,
UnknownUnicastFlood: false,
Forward: true,
Learn: true,
ArpTermination: false,
MacAge: 0, /* means disable aging */
Interfaces: []*l2.BridgeDomains_BridgeDomain_Interfaces{
{
Name: "loopback1",
BridgedVirtualInterface: true,
}, {
Name: "tap1",
BridgedVirtualInterface: false,
},
},
}
// routeThroughMemif1 is an example route configuration, with memif1 being the next hop.
routeThroughMemif1 = l3.StaticRoutes_Route{
Description: "Description",
VrfId: 0,
DstIpAddr: "192.168.2.1/32",
NextHopAddr: "192.168.1.1", // Memif1AsMaster
Weight: 5,
}
)