-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathstream_client.go
377 lines (321 loc) · 9.36 KB
/
stream_client.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
// 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.
// stream-client is an example VPP management application that exercises the
// govpp API on real-world use-cases.
package main
import (
"context"
"flag"
"fmt"
"log"
"net"
"os"
"sync"
"time"
"go.fd.io/govpp"
"go.fd.io/govpp/adapter/socketclient"
"go.fd.io/govpp/api"
interfaces "go.fd.io/govpp/binapi/interface"
"go.fd.io/govpp/binapi/interface_types"
"go.fd.io/govpp/binapi/ip"
"go.fd.io/govpp/binapi/ip_types"
"go.fd.io/govpp/binapi/mactime"
"go.fd.io/govpp/binapi/memclnt"
"go.fd.io/govpp/binapi/vpe"
"go.fd.io/govpp/core"
)
var (
sockAddr = flag.String("sock", socketclient.DefaultSocketName, "Path to VPP binary API socket file")
)
func main() {
flag.Parse()
fmt.Println("Starting stream client example")
// connect to VPP asynchronously
conn, connEv, err := govpp.AsyncConnect(*sockAddr, core.DefaultMaxReconnectAttempts, core.DefaultReconnectInterval)
if err != nil {
log.Fatalln("ERROR:", err)
}
defer conn.Disconnect()
// wait for Connected event
e := <-connEv
if e.State != core.Connected {
log.Fatalln("ERROR: connecting to VPP failed:", e.Error)
}
// check compatibility of used messages
ch, err := conn.NewAPIChannel()
if err != nil {
log.Fatalln("ERROR: creating channel failed:", err)
}
defer ch.Close()
if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
log.Fatalf("compatibility check failed: %v", err)
}
if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
log.Printf("compatibility check failed: %v", err)
}
// process errors encountered during the example
defer func() {
if len(errors) > 0 {
fmt.Printf("finished with %d errors\n", len(errors))
os.Exit(1)
} else {
fmt.Println("finished successfully")
}
}()
// send and receive messages using stream (low-low level API)
stream, err := conn.NewStream(context.Background(),
core.WithRequestSize(50),
core.WithReplySize(50),
core.WithReplyTimeout(2*time.Second))
if err != nil {
panic(err)
}
defer func() {
if err := stream.Close(); err != nil {
logError(err, "closing the stream")
}
}()
getVppVersion(stream)
idx := createLoopback(stream)
interfaceDump(stream)
addIPAddress(stream, idx)
ipAddressDump(stream, idx)
mactimeDump(stream)
interfaceNotifications(conn, idx)
}
func getVppVersion(stream api.Stream) {
fmt.Println("Retrieving version")
if err := stream.SendMsg(&vpe.ShowVersion{}); err != nil {
logError(err, "get version request")
return
}
recvMsg, err := stream.RecvMsg()
if err != nil {
logError(err, "get version reply")
return
}
reply := recvMsg.(*vpe.ShowVersionReply)
if api.RetvalToVPPApiError(reply.Retval) != nil {
logError(err, "get version reply retval")
return
}
fmt.Printf("VPP version: %v\n", reply.Version)
}
func createLoopback(stream api.Stream) (ifIdx interface_types.InterfaceIndex) {
fmt.Println("Creating loopback interface..")
if err := stream.SendMsg(&interfaces.CreateLoopback{}); err != nil {
logError(err, "create loopback request")
return
}
recv, err := stream.RecvMsg()
if err != nil {
logError(err, "create loopback reply")
return
}
reply := recv.(*interfaces.CreateLoopbackReply)
if api.RetvalToVPPApiError(reply.Retval) != nil {
logError(err, "create loopback reply retval")
return
}
fmt.Printf("Loopback interface created: %v\n", reply.SwIfIndex)
return reply.SwIfIndex
}
func interfaceDump(stream api.Stream) {
fmt.Println("Listing interfaces")
if err := stream.SendMsg(&interfaces.SwInterfaceDump{
SwIfIndex: ^interface_types.InterfaceIndex(0),
}); err != nil {
logError(err, "list interfaces request")
return
}
if err := stream.SendMsg(&memclnt.ControlPing{}); err != nil {
logError(err, "ControlPing request")
return
}
Loop:
for {
msg, err := stream.RecvMsg()
if err != nil {
logError(err, "receiving interface list")
return
}
switch m := msg.(type) {
case *interfaces.SwInterfaceDetails:
fmt.Printf("- interface: %s (index: %v)\n", m.InterfaceName, m.SwIfIndex)
case *memclnt.ControlPingReply:
fmt.Printf(" - ControlPingReply: %+v\n", m)
break Loop
default:
logError(err, "unexpected message")
return
}
}
}
func addIPAddress(stream api.Stream, index interface_types.InterfaceIndex) {
addr := ip_types.NewAddress(net.IPv4(10, 10, 0, byte(index)))
fmt.Printf("Adding IP address %v to interface (index %d)\n", addr, index)
if err := stream.SendMsg(&interfaces.SwInterfaceAddDelAddress{
SwIfIndex: index,
IsAdd: true,
Prefix: ip_types.AddressWithPrefix{Address: addr, Len: 32},
}); err != nil {
logError(err, "add IP address request")
return
}
recv, err := stream.RecvMsg()
if err != nil {
logError(err, "add IP address reply")
return
}
reply := recv.(*interfaces.SwInterfaceAddDelAddressReply)
if api.RetvalToVPPApiError(reply.Retval) != nil {
logError(err, "add IP address reply retval")
return
}
fmt.Printf("IP address %v added\n", addr)
}
func ipAddressDump(stream api.Stream, index interface_types.InterfaceIndex) {
fmt.Printf("Listing IP addresses for interface (index %d)\n", index)
if err := stream.SendMsg(&ip.IPAddressDump{
SwIfIndex: index,
}); err != nil {
logError(err, "dump IP address request")
return
}
if err := stream.SendMsg(&memclnt.ControlPing{}); err != nil {
logError(err, "sending ControlPing")
return
}
Loop:
for {
msg, err := stream.RecvMsg()
if err != nil {
logError(err, "receiving IP addresses")
return
}
switch msg.(type) {
case *ip.IPAddressDetails:
fmt.Printf(" - IPAddressDetails: %+v\n", msg)
case *memclnt.ControlPingReply:
fmt.Printf(" - ControlPingReply: %+v\n", msg)
break Loop
default:
logError(err, "unexpected message")
return
}
}
}
// Mactime dump uses MactimeDumpReply message as an end of the stream
// notification instead of the control ping.
func mactimeDump(stream api.Stream) {
fmt.Println("Sending mactime dump")
if err := stream.SendMsg(&mactime.MactimeDump{}); err != nil {
logError(err, "mactime dump request")
return
}
Loop:
for {
msg, err := stream.RecvMsg()
if err != nil {
logError(err, "receiving mactime dump")
return
}
switch m := msg.(type) {
case *mactime.MactimeDetails:
fmt.Printf(" - MactimeDetails: %+v\n", m)
case *mactime.MactimeDumpReply:
if err := api.RetvalToVPPApiError(m.Retval); err != nil && err != api.NO_CHANGE {
logError(err, "mactime dump reply retval")
return
}
fmt.Printf(" - MactimeDumpReply: %+v\n", m)
break Loop
default:
logError(err, "unexpected message")
return
}
}
}
// interfaceNotifications demonstrates how to watch for interface events.
func interfaceNotifications(conn api.Connection, index interface_types.InterfaceIndex) {
// start watcher for specific event message
watcher, err := conn.WatchEvent(context.Background(), (*interfaces.SwInterfaceEvent)(nil))
if err != nil {
logError(err, "watching interface events")
return
}
// enable interface events in VPP
var reply interfaces.WantInterfaceEventsReply
err = conn.Invoke(context.Background(), &interfaces.WantInterfaceEvents{
PID: uint32(os.Getpid()),
EnableDisable: 1,
}, &reply)
if err != nil || api.RetvalToVPPApiError(reply.Retval) != nil {
logError(err, "enabling interface events")
return
}
fmt.Printf("watching interface events for index %d\n", index)
var wg sync.WaitGroup
// receive notifications
wg.Add(1)
go func() {
defer wg.Done()
for notif := range watcher.Events() {
e := notif.(*interfaces.SwInterfaceEvent)
fmt.Printf("incoming interface event: %+v\n", e)
}
fmt.Println("watcher done")
}()
// generate some events in VPP
setInterfaceStatus(conn, index, true)
setInterfaceStatus(conn, index, false)
// disable interface events in VPP
reply.Reset()
if err := conn.Invoke(context.Background(), &interfaces.WantInterfaceEvents{
PID: uint32(os.Getpid()),
EnableDisable: 0,
}, &reply); err != nil || api.RetvalToVPPApiError(reply.Retval) != nil {
logError(err, "disabling interface events")
return
}
// unsubscribe from delivery of the notifications
watcher.Close()
// generate ignored events in VPP
setInterfaceStatus(conn, index, true)
wg.Wait()
}
func setInterfaceStatus(conn api.Connection, ifIdx interface_types.InterfaceIndex, up bool) {
var flags interface_types.IfStatusFlags
if up {
flags = interface_types.IF_STATUS_API_FLAG_ADMIN_UP
} else {
flags = 0
}
var reply interfaces.SwInterfaceSetFlagsReply
if err := conn.Invoke(context.Background(), &interfaces.SwInterfaceSetFlags{
SwIfIndex: ifIdx,
Flags: flags,
}, &reply); err != nil {
logError(err, "setting interface flags")
return
} else if err = api.RetvalToVPPApiError(reply.Retval); err != nil {
logError(err, "setting interface flags retval")
return
}
}
var errors []error
func logError(err error, msg string) {
fmt.Printf("ERROR: %s: %v\n", msg, err)
errors = append(errors, err)
}