c6771f697f81433bb5e3f74b2f71b3e605ae9b2d
[vpp.git] / extras / hs-test / vppinstance.go
1 package main
2
3 import (
4         "fmt"
5         "os"
6         "os/exec"
7         "os/signal"
8         "strings"
9         "syscall"
10         "time"
11
12         "github.com/edwarnicke/exechelper"
13
14         "go.fd.io/govpp"
15         "go.fd.io/govpp/api"
16         "go.fd.io/govpp/binapi/af_packet"
17         interfaces "go.fd.io/govpp/binapi/interface"
18         "go.fd.io/govpp/binapi/interface_types"
19         "go.fd.io/govpp/binapi/session"
20         "go.fd.io/govpp/binapi/tapv2"
21         "go.fd.io/govpp/binapi/vpe"
22         "go.fd.io/govpp/core"
23 )
24
25 const vppConfigTemplate = `unix {
26   nodaemon
27   log %[1]s%[4]s
28   full-coredump
29   cli-listen %[1]s%[2]s
30   runtime-dir %[1]s/var/run
31   gid vpp
32 }
33
34 api-trace {
35   on
36 }
37
38 api-segment {
39   gid vpp
40 }
41
42 socksvr {
43   socket-name %[1]s%[3]s
44 }
45
46 statseg {
47   socket-name %[1]s/var/run/vpp/stats.sock
48 }
49
50 plugins {
51   plugin default { disable }
52
53   plugin unittest_plugin.so { enable }
54   plugin quic_plugin.so { enable }
55   plugin af_packet_plugin.so { enable }
56   plugin hs_apps_plugin.so { enable }
57   plugin http_plugin.so { enable }
58 }
59
60 logging {
61   default-log-level debug
62   default-syslog-log-level debug
63 }
64
65 `
66
67 const (
68         defaultCliSocketFilePath = "/var/run/vpp/cli.sock"
69         defaultApiSocketFilePath = "/var/run/vpp/api.sock"
70         defaultLogFilePath       = "/var/log/vpp/vpp.log"
71 )
72
73 type VppInstance struct {
74         container        *Container
75         additionalConfig Stanza
76         connection       *core.Connection
77         apiChannel       api.Channel
78 }
79
80 func (vpp *VppInstance) Suite() *HstSuite {
81         return vpp.container.suite
82 }
83
84 func (vpp *VppInstance) getCliSocket() string {
85         return fmt.Sprintf("%s%s", vpp.container.GetContainerWorkDir(), defaultCliSocketFilePath)
86 }
87
88 func (vpp *VppInstance) getRunDir() string {
89         return vpp.container.GetContainerWorkDir() + "/var/run/vpp"
90 }
91
92 func (vpp *VppInstance) getLogDir() string {
93         return vpp.container.GetContainerWorkDir() + "/var/log/vpp"
94 }
95
96 func (vpp *VppInstance) getEtcDir() string {
97         return vpp.container.GetContainerWorkDir() + "/etc/vpp"
98 }
99
100 func (vpp *VppInstance) start() error {
101         // Create folders
102         containerWorkDir := vpp.container.GetContainerWorkDir()
103
104         vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir())
105         vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir())
106         vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
107
108         // Create startup.conf inside the container
109         configContent := fmt.Sprintf(
110                 vppConfigTemplate,
111                 containerWorkDir,
112                 defaultCliSocketFilePath,
113                 defaultApiSocketFilePath,
114                 defaultLogFilePath,
115         )
116         configContent += vpp.additionalConfig.ToString()
117         startupFileName := vpp.getEtcDir() + "/startup.conf"
118         vpp.container.createFile(startupFileName, configContent)
119
120         if *IsVppDebug {
121                 sig := make(chan os.Signal, 1)
122                 signal.Notify(sig, syscall.SIGINT)
123                 cont := make(chan bool, 1)
124                 go func() {
125                         <-sig
126                         cont <- true
127                 }()
128
129                 // Start VPP in GDB and wait for user to attach it
130                 vpp.container.execServer("su -c \"gdb -ex run --args vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
131                 fmt.Println("run following command in different terminal:")
132                 fmt.Println("docker exec -it " + vpp.container.name + " gdb -ex \"attach $(docker exec " + vpp.container.name + " pidof gdb)\"")
133                 fmt.Println("Afterwards press CTRL+C to continue")
134                 <-cont
135                 fmt.Println("continuing...")
136         } else {
137                 // Start VPP
138                 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
139         }
140
141         // Connect to VPP and store the connection
142         sockAddress := vpp.container.GetHostWorkDir() + defaultApiSocketFilePath
143         conn, connEv, err := govpp.AsyncConnect(
144                 sockAddress,
145                 core.DefaultMaxReconnectAttempts,
146                 core.DefaultReconnectInterval)
147         if err != nil {
148                 fmt.Println("async connect error: ", err)
149         }
150         vpp.connection = conn
151
152         // ... wait for Connected event
153         e := <-connEv
154         if e.State != core.Connected {
155                 fmt.Println("connecting to VPP failed: ", e.Error)
156         }
157
158         // ... check compatibility of used messages
159         ch, err := conn.NewAPIChannel()
160         if err != nil {
161                 fmt.Println("creating channel failed: ", err)
162         }
163         if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
164                 fmt.Println("compatibility error: ", err)
165         }
166         if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
167                 fmt.Println("compatibility error: ", err)
168         }
169         vpp.apiChannel = ch
170
171         return nil
172 }
173
174 func (vpp *VppInstance) vppctl(command string, arguments ...any) string {
175         vppCliCommand := fmt.Sprintf(command, arguments...)
176         containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
177                 vpp.container.name, vpp.getCliSocket(), vppCliCommand)
178         vpp.Suite().log(containerExecCommand)
179         output, err := exechelper.CombinedOutput(containerExecCommand)
180         vpp.Suite().assertNil(err)
181
182         return string(output)
183 }
184
185 func (vpp *VppInstance) waitForApp(appName string, timeout int) {
186         for i := 0; i < timeout; i++ {
187                 o := vpp.vppctl("show app")
188                 if strings.Contains(o, appName) {
189                         return
190                 }
191                 time.Sleep(1 * time.Second)
192         }
193         vpp.Suite().assertNil(1, "Timeout while waiting for app '%s'", appName)
194         return
195 }
196
197 func (vpp *VppInstance) createAfPacket(
198         veth *NetInterface,
199 ) (interface_types.InterfaceIndex, error) {
200         createReq := &af_packet.AfPacketCreateV2{
201                 UseRandomHwAddr: true,
202                 HostIfName:      veth.Name(),
203         }
204         if veth.HwAddress() != (MacAddress{}) {
205                 createReq.UseRandomHwAddr = false
206                 createReq.HwAddr = veth.HwAddress()
207         }
208         createReply := &af_packet.AfPacketCreateV2Reply{}
209
210         if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil {
211                 return 0, err
212         }
213         veth.SetIndex(createReply.SwIfIndex)
214
215         // Set to up
216         upReq := &interfaces.SwInterfaceSetFlags{
217                 SwIfIndex: veth.Index(),
218                 Flags:     interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
219         }
220         upReply := &interfaces.SwInterfaceSetFlagsReply{}
221
222         if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
223                 return 0, err
224         }
225
226         // Add address
227         if veth.AddressWithPrefix() == (AddressWithPrefix{}) {
228                 var err error
229                 var ip4Address string
230                 if ip4Address, err = veth.addresser.NewIp4Address(veth.Peer().networkNumber); err == nil {
231                         veth.SetAddress(ip4Address)
232                 } else {
233                         return 0, err
234                 }
235         }
236         addressReq := &interfaces.SwInterfaceAddDelAddress{
237                 IsAdd:     true,
238                 SwIfIndex: veth.Index(),
239                 Prefix:    veth.AddressWithPrefix(),
240         }
241         addressReply := &interfaces.SwInterfaceAddDelAddressReply{}
242
243         if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil {
244                 return 0, err
245         }
246
247         return veth.Index(), nil
248 }
249
250 func (vpp *VppInstance) addAppNamespace(
251         secret uint64,
252         ifx interface_types.InterfaceIndex,
253         namespaceId string,
254 ) error {
255         req := &session.AppNamespaceAddDelV2{
256                 Secret:      secret,
257                 SwIfIndex:   ifx,
258                 NamespaceID: namespaceId,
259         }
260         reply := &session.AppNamespaceAddDelV2Reply{}
261
262         if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil {
263                 return err
264         }
265
266         sessionReq := &session.SessionEnableDisable{
267                 IsEnable: true,
268         }
269         sessionReply := &session.SessionEnableDisableReply{}
270
271         if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil {
272                 return err
273         }
274
275         return nil
276 }
277
278 func (vpp *VppInstance) createTap(
279         tap *NetInterface,
280         tapId ...uint32,
281 ) error {
282         var id uint32 = 1
283         if len(tapId) > 0 {
284                 id = tapId[0]
285         }
286         createTapReq := &tapv2.TapCreateV2{
287                 ID:               id,
288                 HostIfNameSet:    true,
289                 HostIfName:       tap.Name(),
290                 HostIP4PrefixSet: true,
291                 HostIP4Prefix:    tap.IP4AddressWithPrefix(),
292         }
293         createTapReply := &tapv2.TapCreateV2Reply{}
294
295         // Create tap interface
296         if err := vpp.apiChannel.SendRequest(createTapReq).ReceiveReply(createTapReply); err != nil {
297                 return err
298         }
299
300         // Add address
301         addAddressReq := &interfaces.SwInterfaceAddDelAddress{
302                 IsAdd:     true,
303                 SwIfIndex: createTapReply.SwIfIndex,
304                 Prefix:    tap.Peer().AddressWithPrefix(),
305         }
306         addAddressReply := &interfaces.SwInterfaceAddDelAddressReply{}
307
308         if err := vpp.apiChannel.SendRequest(addAddressReq).ReceiveReply(addAddressReply); err != nil {
309                 return err
310         }
311
312         // Set interface to up
313         upReq := &interfaces.SwInterfaceSetFlags{
314                 SwIfIndex: createTapReply.SwIfIndex,
315                 Flags:     interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
316         }
317         upReply := &interfaces.SwInterfaceSetFlagsReply{}
318
319         if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
320                 return err
321         }
322
323         return nil
324 }
325
326 func (vpp *VppInstance) saveLogs() {
327         logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
328         logSource := vpp.container.GetHostWorkDir() + defaultLogFilePath
329         cmd := exec.Command("cp", logSource, logTarget)
330         vpp.Suite().T().Helper()
331         vpp.Suite().log(cmd.String())
332         cmd.Run()
333 }
334
335 func (vpp *VppInstance) disconnect() {
336         vpp.connection.Disconnect()
337         vpp.apiChannel.Close()
338 }