1 // Copyright (c) 2017 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 // simple-client is an example VPP management application that exercises the
16 // govpp API on real-world use-cases.
28 "git.fd.io/govpp.git/adapter/socketclient"
29 "git.fd.io/govpp.git/api"
30 interfaces "git.fd.io/govpp.git/binapi/interface"
31 "git.fd.io/govpp.git/binapi/interface_types"
32 "git.fd.io/govpp.git/binapi/ip"
33 "git.fd.io/govpp.git/binapi/ip_types"
34 "git.fd.io/govpp.git/binapi/mactime"
35 "git.fd.io/govpp.git/binapi/vpe"
36 "git.fd.io/govpp.git/core"
40 sockAddr = flag.String("sock", socketclient.DefaultSocketName, "Path to VPP binary API socket file")
46 fmt.Println("Starting simple client example")
48 // connect to VPP asynchronously
49 conn, conev, err := govpp.AsyncConnect(*sockAddr, core.DefaultMaxReconnectAttempts, core.DefaultReconnectInterval)
51 log.Fatalln("ERROR:", err)
53 defer conn.Disconnect()
55 // wait for Connected event
58 if e.State != core.Connected {
59 log.Fatalln("ERROR: connecting to VPP failed:", e.Error)
63 // create an API channel that will be used in the examples
64 ch, err := conn.NewAPIChannel()
66 log.Fatalln("ERROR: creating channel failed:", err)
70 if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
76 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
80 idx := createLoopback(ch)
84 ipAddressDump(ch, idx)
85 interfaceNotifications(ch, idx)
90 fmt.Printf("finished with %d errors\n", len(Errors))
93 fmt.Println("finished successfully")
99 func logError(err error, msg string) {
100 fmt.Printf("ERROR: %s: %v\n", msg, err)
101 Errors = append(Errors, err)
104 // vppVersion is the simplest API example - it retrieves VPP version.
105 func vppVersion(ch api.Channel) {
106 fmt.Println("Retrieving version")
108 req := &vpe.ShowVersion{}
109 reply := &vpe.ShowVersionReply{}
111 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
112 logError(err, "retrieving version")
115 fmt.Printf("reply: %+v\n", reply)
117 fmt.Printf("VPP version: %q\n", reply.Version)
122 // createLoopback sends request to create loopback interface.
123 func createLoopback(ch api.Channel) interface_types.InterfaceIndex {
124 fmt.Println("Creating loopback interface")
126 req := &interfaces.CreateLoopback{}
127 reply := &interfaces.CreateLoopbackReply{}
129 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
130 logError(err, "creating loopback interface")
133 fmt.Printf("reply: %+v\n", reply)
135 fmt.Printf("interface index: %v\n", reply.SwIfIndex)
139 return reply.SwIfIndex
142 // interfaceDump shows an example of multipart request (multiple replies are expected).
143 func interfaceDump(ch api.Channel) {
144 fmt.Println("Dumping interfaces")
147 reqCtx := ch.SendMultiRequest(&interfaces.SwInterfaceDump{
148 SwIfIndex: ^interface_types.InterfaceIndex(0),
151 msg := &interfaces.SwInterfaceDetails{}
152 stop, err := reqCtx.ReceiveReply(msg)
157 logError(err, "dumping interfaces")
161 fmt.Printf(" - interface #%d: %+v\n", n, msg)
169 // addIPAddress sends request to add IP address to interface.
170 func addIPAddress(ch api.Channel, index interface_types.InterfaceIndex) {
171 fmt.Printf("Adding IP address to interface to interface index %d\n", index)
173 req := &interfaces.SwInterfaceAddDelAddress{
176 Prefix: ip_types.AddressWithPrefix{
177 Address: ip_types.Address{
178 Af: ip_types.ADDRESS_IP4,
179 Un: ip_types.AddressUnionIP4(ip_types.IP4Address{10, 10, 0, uint8(index)}),
185 reply := &interfaces.SwInterfaceAddDelAddressReply{}
187 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
188 logError(err, "adding IP address to interface")
191 fmt.Printf("reply: %+v\n", reply)
197 func ipAddressDump(ch api.Channel, index interface_types.InterfaceIndex) {
198 fmt.Printf("Dumping IP addresses for interface index %d\n", index)
200 req := &ip.IPAddressDump{
203 reqCtx := ch.SendMultiRequest(req)
206 msg := &ip.IPAddressDetails{}
207 stop, err := reqCtx.ReceiveReply(msg)
209 logError(err, "dumping IP addresses")
215 fmt.Printf(" - ip address: %+v\n", msg)
223 // interfaceNotifications shows the usage of notification API. Note that for notifications,
224 // you are supposed to create your own Go channel with your preferred buffer size. If the channel's
225 // buffer is full, the notifications will not be delivered into it.
226 func interfaceNotifications(ch api.Channel, index interface_types.InterfaceIndex) {
227 fmt.Printf("Subscribing to notificaiton events for interface index %d\n", index)
229 notifChan := make(chan api.Message, 100)
231 // subscribe for specific notification message
232 sub, err := ch.SubscribeNotification(notifChan, &interfaces.SwInterfaceEvent{})
234 logError(err, "subscribing to interface events")
238 // enable interface events in VPP
239 err = ch.SendRequest(&interfaces.WantInterfaceEvents{
240 PID: uint32(os.Getpid()),
242 }).ReceiveReply(&interfaces.WantInterfaceEventsReply{})
244 logError(err, "enabling interface events")
248 // receive notifications
250 for notif := range notifChan {
251 e := notif.(*interfaces.SwInterfaceEvent)
252 fmt.Printf("incoming event: %+v\n", e)
257 // generate some events in VPP
258 err = ch.SendRequest(&interfaces.SwInterfaceSetFlags{
260 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
261 }).ReceiveReply(&interfaces.SwInterfaceSetFlagsReply{})
263 logError(err, "setting interface flags")
266 err = ch.SendRequest(&interfaces.SwInterfaceSetFlags{
269 }).ReceiveReply(&interfaces.SwInterfaceSetFlagsReply{})
271 logError(err, "setting interface flags")
275 // disable interface events in VPP
276 err = ch.SendRequest(&interfaces.WantInterfaceEvents{
277 PID: uint32(os.Getpid()),
279 }).ReceiveReply(&interfaces.WantInterfaceEventsReply{})
281 logError(err, "setting interface flags")
285 // unsubscribe from delivery of the notifications
286 err = sub.Unsubscribe()
288 logError(err, "unsubscribing from interface events")
296 func mactimeDump(conn api.Connection) {
297 fmt.Println("Sending mactime dump")
299 ctx := context.Background()
301 stream, err := conn.NewStream(ctx)
307 if err := stream.SendMsg(&mactime.MactimeDump{}); err != nil {
308 logError(err, "sending mactime dump")
314 msg, err := stream.RecvMsg()
316 logError(err, "dumping mactime")
321 case *mactime.MactimeDetails:
322 fmt.Printf(" - MactimeDetails: %+v\n", msg)
324 case *mactime.MactimeDumpReply:
325 fmt.Printf(" - MactimeDumpReply: %+v\n", msg)
329 logError(err, "unexpected message")
338 func marshal(v interface{}) {
339 fmt.Printf("GO: %#v\n", v)
340 b, err := json.MarshalIndent(v, "", " ")
344 fmt.Printf("JSON: %s\n", b)