Removed global binapi VPP adapter
[govpp.git] / examples / multi-vpp / multi_vpp.go
1 // Copyright (c) 2020 Cisco and/or its affiliates.
2 //
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:
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 // multi-vpp is an example of managing multiple VPPs in single application.
16 package main
17
18 import (
19         "flag"
20         "fmt"
21         "git.fd.io/govpp.git"
22         "git.fd.io/govpp.git/adapter/socketclient"
23         "git.fd.io/govpp.git/api"
24         "git.fd.io/govpp.git/core"
25         "git.fd.io/govpp.git/examples/binapi/interface_types"
26         "git.fd.io/govpp.git/examples/binapi/interfaces"
27         "git.fd.io/govpp.git/examples/binapi/ip"
28         "git.fd.io/govpp.git/examples/binapi/ip_types"
29         "git.fd.io/govpp.git/examples/binapi/vpe"
30         "log"
31         "os"
32 )
33
34 var (
35         sockAddrVpp1 = flag.String("sock1", socketclient.DefaultSocketName, "Path to binary API socket file of the first VPP instance")
36         sockAddrVpp2 = flag.String("sock2", socketclient.DefaultSocketName, "Path to binary API socket file of the second VPP instance")
37 )
38
39 func main() {
40         flag.Parse()
41         fmt.Println("Starting multi-vpp example")
42
43         // since both of them default to the same value
44         if *sockAddrVpp1 == *sockAddrVpp2 {
45                 log.Fatalln("ERROR: identical VPP sockets defined, set at least one of them to non-default path")
46         }
47
48         // connect VPP1
49         conn1, err := connectToVPP(*sockAddrVpp1, 1)
50         if err != nil {
51                 log.Fatalf("ERROR: connecting VPP failed (socket %s): %v\n", *sockAddrVpp1, err)
52         }
53         defer conn1.Disconnect()
54         ch1, err := getAPIChannel(conn1)
55         if err != nil {
56                 log.Fatalf("ERROR: creating channel failed (socket: %s): %v\n", *sockAddrVpp1, err)
57         }
58         defer ch1.Close()
59
60         // connect VPP2
61         conn2, err := connectToVPP(*sockAddrVpp2, 2)
62         if err != nil {
63                 log.Fatalf("ERROR: connecting VPP failed (socket %s): %v\n", *sockAddrVpp2, err)
64         }
65         defer conn2.Disconnect()
66         ch2, err := getAPIChannel(conn2)
67         if err != nil {
68                 log.Fatalf("ERROR: creating channel failed (socket: %s): %v\n", *sockAddrVpp2, err)
69         }
70         defer ch2.Close()
71
72         // configure VPPs
73         ifIdx1 := createLoopback(ch1)
74         addIPToInterface(ch1, ifIdx1, "10.10.0.1/24")
75         ifIdx2 := createLoopback(ch2)
76         addIPToInterface(ch2, ifIdx2, "20.10.0.1/24")
77
78         // retrieve configuration from the VPPs
79         retrieveIPAddresses(ch1, ifIdx1)
80         retrieveIPAddresses(ch2, ifIdx2)
81
82         if len(Errors) > 0 {
83                 fmt.Printf("finished with %d errors\n", len(Errors))
84                 os.Exit(1)
85         } else {
86                 fmt.Println("finished successfully")
87         }
88 }
89
90 func connectToVPP(socket string, attempts int) (*core.Connection, error) {
91         connection, event, err := govpp.AsyncConnect(socket, attempts, core.DefaultReconnectInterval)
92         if err != nil {
93                 return nil, err
94         }
95
96         // handle connection event
97         select {
98         case e := <-event:
99                 if e.State != core.Connected {
100                         return nil, err
101                 }
102         }
103         return connection, nil
104 }
105
106 func getAPIChannel(conn *core.Connection) (api.Channel, error) {
107         ch, err := conn.NewAPIChannel()
108         if err != nil {
109                 return nil, err
110         }
111
112         if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
113                 return nil, err
114         }
115
116         getVppVersion(ch)
117
118         if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
119                 return nil, err
120         }
121         return ch, nil
122 }
123
124 // getVppVersion returns VPP version (simple API usage)
125 func getVppVersion(ch api.Channel) {
126         fmt.Println("Retrieving version")
127
128         req := &vpe.ShowVersion{}
129         reply := &vpe.ShowVersionReply{}
130
131         if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
132                 logError(err, "retrieving version")
133                 return
134         }
135         fmt.Printf("reply: %+v\n", reply)
136
137         fmt.Printf("VPP version: %q\n", reply.Version)
138         fmt.Println("OK")
139         fmt.Println()
140 }
141
142 var Errors []error
143
144 func logError(err error, msg string) {
145         fmt.Printf("ERROR: %s: %v\n", msg, err)
146         Errors = append(Errors, err)
147 }
148
149 // createLoopback sends request to create a loopback interface
150 func createLoopback(ch api.Channel) interface_types.InterfaceIndex {
151         fmt.Println("Adding loopback interface")
152
153         req := &interfaces.CreateLoopback{}
154         reply := &interfaces.CreateLoopbackReply{}
155
156         if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
157                 logError(err, "adding loopback interface")
158                 return 0
159         }
160         fmt.Printf("reply: %+v\n", reply)
161
162         fmt.Printf("interface index: %v\n", reply.SwIfIndex)
163         fmt.Println("OK")
164         fmt.Println()
165
166         return reply.SwIfIndex
167 }
168
169 // addIPToInterface sends request to add an IP address to an interface.
170 func addIPToInterface(ch api.Channel, index interface_types.InterfaceIndex, ip string) {
171         fmt.Printf("Setting up IP address to the interface with index %d\n", index)
172         prefix, err := ip_types.ParsePrefix(ip)
173         if err != nil {
174                 logError(err, "attempt to add invalid IP address")
175                 return
176         }
177
178
179         req := &interfaces.SwInterfaceAddDelAddress{
180                 SwIfIndex: index,
181                 IsAdd:     true,
182                 Prefix:    ip_types.AddressWithPrefix(prefix),
183         }
184         reply := &interfaces.SwInterfaceAddDelAddressReply{}
185
186         if err := ch.SendRequest(req).ReceiveReply(reply); err != nil {
187                 logError(err, "adding IP address to interface")
188                 return
189         }
190         fmt.Printf("reply: %+v\n", reply)
191
192         fmt.Println("OK")
193         fmt.Println()
194 }
195
196 func retrieveIPAddresses(ch api.Channel, index interface_types.InterfaceIndex) {
197         fmt.Printf("Retrieving IP addresses for interface index %d\n", index)
198
199         req := &ip.IPAddressDump{
200                 SwIfIndex: index,
201         }
202         reqCtx := ch.SendMultiRequest(req)
203
204         for {
205                 msg := &ip.IPAddressDetails{}
206                 stop, err := reqCtx.ReceiveReply(msg)
207                 if err != nil {
208                         logError(err, "dumping IP addresses")
209                         return
210                 }
211                 if stop {
212                         break
213                 }
214                 prefix := ip_types.Prefix(msg.Prefix)
215                 fmt.Printf(" - ip address: %+v\n", prefix.ToString())
216         }
217
218         fmt.Println("OK")
219         fmt.Println()
220 }