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.
27 "git.fd.io/govpp.git/adapter/socketclient"
28 "git.fd.io/govpp.git/api"
29 "git.fd.io/govpp.git/core"
30 "git.fd.io/govpp.git/examples/binapi/interface_types"
31 "git.fd.io/govpp.git/examples/binapi/interfaces"
32 "git.fd.io/govpp.git/examples/binapi/ip"
33 "git.fd.io/govpp.git/examples/binapi/ip_types"
34 "git.fd.io/govpp.git/examples/binapi/vpe"
38 sockAddr = flag.String("sock", socketclient.DefaultSocketName, "Path to VPP binary API socket file")
44 fmt.Println("Starting simple client example")
46 // connect to VPP asynchronously
47 conn, conev, err := govpp.AsyncConnect(*sockAddr, core.DefaultMaxReconnectAttempts, core.DefaultReconnectInterval)
49 log.Fatalln("ERROR:", err)
51 defer conn.Disconnect()
53 // wait for Connected event
56 if e.State != core.Connected {
57 log.Fatalln("ERROR: connecting to VPP failed:", e.Error)
61 // create an API channel that will be used in the examples
62 ch, err := conn.NewAPIChannel()
64 log.Fatalln("ERROR: creating channel failed:", err)
68 if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
74 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
78 idx := createLoopback(ch)
82 ipAddressDump(ch, idx)
83 interfaceNotifications(ch, idx)
86 fmt.Printf("finished with %d errors\n", len(Errors))
89 fmt.Println("finished successfully")
95 func logError(err error, msg string) {
96 fmt.Printf("ERROR: %s: %v\n", msg, err)
97 Errors = append(Errors, err)
100 // vppVersion is the simplest API example - it retrieves VPP version.
101 func vppVersion(ch api.Channel) {
102 fmt.Println("Retrieving version")
104 req := &vpe.ShowVersion{}
105 reply := &vpe.ShowVersionReply{}
107 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
108 logError(err, "retrieving version")
111 fmt.Printf("reply: %+v\n", reply)
113 fmt.Printf("VPP version: %q\n", cleanString(reply.Version))
118 // createLoopback sends request to create loopback interface.
119 func createLoopback(ch api.Channel) interfaces.InterfaceIndex {
120 fmt.Println("Creating loopback interface")
122 req := &interfaces.CreateLoopback{}
123 reply := &interfaces.CreateLoopbackReply{}
125 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
126 logError(err, "creating loopback interface")
129 fmt.Printf("reply: %+v\n", reply)
131 fmt.Printf("interface index: %v\n", reply.SwIfIndex)
135 return reply.SwIfIndex
138 // interfaceDump shows an example of multipart request (multiple replies are expected).
139 func interfaceDump(ch api.Channel) {
140 fmt.Println("Dumping interfaces")
143 reqCtx := ch.SendMultiRequest(&interfaces.SwInterfaceDump{})
145 msg := &interfaces.SwInterfaceDetails{}
146 stop, err := reqCtx.ReceiveReply(msg)
151 logError(err, "dumping interfaces")
155 fmt.Printf(" - interface #%d: %+v\n", n, msg)
162 // addIPAddress sends request to add IP address to interface.
163 func addIPAddress(ch api.Channel, index interfaces.InterfaceIndex) {
164 fmt.Printf("Adding IP address to interface to interface index %d\n", index)
166 req := &interfaces.SwInterfaceAddDelAddress{
169 Prefix: ip_types.AddressWithPrefix{
170 Address: interfaces.Address{
171 Af: ip_types.ADDRESS_IP4,
172 Un: ip_types.AddressUnionIP4(interfaces.IP4Address{10, 10, 0, uint8(index)}),
177 reply := &interfaces.SwInterfaceAddDelAddressReply{}
179 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
180 logError(err, "adding IP address to interface")
183 fmt.Printf("reply: %+v\n", reply)
189 func ipAddressDump(ch api.Channel, index interfaces.InterfaceIndex) {
190 fmt.Printf("Dumping IP addresses for interface index %d\n", index)
192 req := &ip.IPAddressDump{
195 reqCtx := ch.SendMultiRequest(req)
198 msg := &ip.IPAddressDetails{}
199 stop, err := reqCtx.ReceiveReply(msg)
201 logError(err, "dumping IP addresses")
207 fmt.Printf(" - ip address: %+v\n", msg)
214 // interfaceNotifications shows the usage of notification API. Note that for notifications,
215 // you are supposed to create your own Go channel with your preferred buffer size. If the channel's
216 // buffer is full, the notifications will not be delivered into it.
217 func interfaceNotifications(ch api.Channel, index interfaces.InterfaceIndex) {
218 fmt.Printf("Subscribing to notificaiton events for interface index %d\n", index)
220 notifChan := make(chan api.Message, 100)
222 // subscribe for specific notification message
223 sub, err := ch.SubscribeNotification(notifChan, &interfaces.SwInterfaceEvent{})
225 logError(err, "subscribing to interface events")
229 // enable interface events in VPP
230 err = ch.SendRequest(&interfaces.WantInterfaceEvents{
231 PID: uint32(os.Getpid()),
233 }).ReceiveReply(&interfaces.WantInterfaceEventsReply{})
235 logError(err, "enabling interface events")
239 // receive notifications
241 for notif := range notifChan {
242 fmt.Printf("incoming event: %+v\n", notif.(*interfaces.SwInterfaceEvent))
246 // generate some events in VPP
247 err = ch.SendRequest(&interfaces.SwInterfaceSetFlags{
249 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
250 }).ReceiveReply(&interfaces.SwInterfaceSetFlagsReply{})
252 logError(err, "setting interface flags")
255 err = ch.SendRequest(&interfaces.SwInterfaceSetFlags{
258 }).ReceiveReply(&interfaces.SwInterfaceSetFlagsReply{})
260 logError(err, "setting interface flags")
264 // disable interface events in VPP
265 err = ch.SendRequest(&interfaces.WantInterfaceEvents{
266 PID: uint32(os.Getpid()),
268 }).ReceiveReply(&interfaces.WantInterfaceEventsReply{})
270 logError(err, "setting interface flags")
274 // unsubscribe from delivery of the notifications
275 err = sub.Unsubscribe()
277 logError(err, "unsubscribing from interface events")
285 func cleanString(str string) string {
286 return strings.Split(str, "\x00")[0]