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/interfaces"
31 "git.fd.io/govpp.git/examples/binapi/ip"
32 "git.fd.io/govpp.git/examples/binapi/vpe"
36 sockAddr = flag.String("sock", socketclient.DefaultSocketName, "Path to VPP binary API socket file")
42 fmt.Println("Starting simple client example")
44 // connect to VPP asynchronously
45 conn, conev, err := govpp.AsyncConnect(*sockAddr, core.DefaultMaxReconnectAttempts, core.DefaultReconnectInterval)
47 log.Fatalln("ERROR:", err)
49 defer conn.Disconnect()
51 // wait for Connected event
54 if e.State != core.Connected {
55 log.Fatalln("ERROR: connecting to VPP failed:", e.Error)
59 // create an API channel that will be used in the examples
60 ch, err := conn.NewAPIChannel()
62 log.Fatalln("ERROR: creating channel failed:", err)
68 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
79 interfaceNotifications(ch)
82 fmt.Printf("finished with %d errors\n", len(Errors))
85 fmt.Println("finished successfully")
91 func logError(err error, msg string) {
92 fmt.Printf("ERROR: %s: %v\n", msg, err)
93 Errors = append(Errors, err)
96 // vppVersion is the simplest API example - it retrieves VPP version.
97 func vppVersion(ch api.Channel) {
98 fmt.Println("Retrieving version")
100 req := &vpe.ShowVersion{}
101 reply := &vpe.ShowVersionReply{}
103 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
104 logError(err, "retrieving version")
107 fmt.Printf("reply: %+v\n", reply)
109 fmt.Printf("VPP version: %q\n", cleanString(reply.Version))
113 // createLoopback sends request to create loopback interface.
114 func createLoopback(ch api.Channel) {
115 fmt.Println("Creating loopback interface")
117 req := &interfaces.CreateLoopback{}
118 reply := &interfaces.CreateLoopbackReply{}
120 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
121 logError(err, "creating loopback interface")
124 fmt.Printf("reply: %+v\n", reply)
126 fmt.Printf("loopback interface index: %v\n", reply.SwIfIndex)
130 // interfaceDump shows an example of multipart request (multiple replies are expected).
131 func interfaceDump(ch api.Channel) {
132 fmt.Println("Dumping interfaces")
134 reqCtx := ch.SendMultiRequest(&interfaces.SwInterfaceDump{})
136 msg := &interfaces.SwInterfaceDetails{}
137 stop, err := reqCtx.ReceiveReply(msg)
139 logError(err, "dumping interfaces")
145 fmt.Printf(" - interface: %+v\n", msg)
151 // addIPAddress sends request to add IP address to interface.
152 func addIPAddress(ch api.Channel) {
153 fmt.Println("Adding IP address to interface")
155 req := &interfaces.SwInterfaceAddDelAddress{
158 Address: []byte{10, 10, 0, 1},
160 /* below for 20.01-rc0
162 Prefix: interfaces.Prefix{
163 Address: interfaces.Address{
164 Af: interfaces.ADDRESS_IP4,
165 Un: interfaces.AddressUnionIP4(interfaces.IP4Address{10, 10, 0, 1}),
170 reply := &interfaces.SwInterfaceAddDelAddressReply{}
172 if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
173 logError(err, "adding IP address to interface")
176 fmt.Printf("reply: %+v\n", reply)
181 func ipAddressDump(ch api.Channel) {
182 fmt.Println("Dumping IP addresses")
184 req := &ip.IPAddressDump{
187 reqCtx := ch.SendMultiRequest(req)
190 msg := &ip.IPAddressDetails{}
191 stop, err := reqCtx.ReceiveReply(msg)
193 logError(err, "dumping IP addresses")
199 fmt.Printf(" - ip address: %+v\n", msg)
205 // interfaceNotifications shows the usage of notification API. Note that for notifications,
206 // you are supposed to create your own Go channel with your preferred buffer size. If the channel's
207 // buffer is full, the notifications will not be delivered into it.
208 func interfaceNotifications(ch api.Channel) {
209 fmt.Println("Subscribing to notificaiton events")
211 notifChan := make(chan api.Message, 100)
213 // subscribe for specific notification message
214 sub, err := ch.SubscribeNotification(notifChan, &interfaces.SwInterfaceEvent{})
216 logError(err, "subscribing to interface events")
220 // enable interface events in VPP
221 err = ch.SendRequest(&interfaces.WantInterfaceEvents{
222 PID: uint32(os.Getpid()),
224 }).ReceiveReply(&interfaces.WantInterfaceEventsReply{})
226 logError(err, "enabling interface events")
230 // generate some events in VPP
231 err = ch.SendRequest(&interfaces.SwInterfaceSetFlags{
233 }).ReceiveReply(&interfaces.SwInterfaceSetFlagsReply{})
235 logError(err, "setting interface flags")
238 err = ch.SendRequest(&interfaces.SwInterfaceSetFlags{
241 /* below for 20.01-rc0
243 Flags: interfaces.IF_STATUS_API_FLAG_ADMIN_UP,*/
244 }).ReceiveReply(&interfaces.SwInterfaceSetFlagsReply{})
246 logError(err, "setting interface flags")
250 // receive one notification
251 notif := (<-notifChan).(*interfaces.SwInterfaceEvent)
252 fmt.Printf("incoming event: %+v\n", notif)
254 // disable interface events in VPP
255 err = ch.SendRequest(&interfaces.WantInterfaceEvents{
256 PID: uint32(os.Getpid()),
258 }).ReceiveReply(&interfaces.WantInterfaceEventsReply{})
260 logError(err, "setting interface flags")
264 // unsubscribe from delivery of the notifications
265 err = sub.Unsubscribe()
267 logError(err, "unsubscribing from interface events")
274 func cleanString(str string) string {
275 return strings.Split(str, "\x00")[0]