blob: 1a058c8e0e3f8f89245fca6c1cf2b2d0404d4d92 [file] [log] [blame]
Maros Ondrejicka11a03e92022-12-01 09:56:37 +01001package main
2
3import (
Filip Tehlarf3ee2b62023-01-09 12:07:09 +01004 "fmt"
Filip Tehlarec5c40b2023-02-28 18:59:15 +01005 "os"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +01006 "os/exec"
Filip Tehlarec5c40b2023-02-28 18:59:15 +01007 "os/signal"
Filip Tehlar543cd572023-06-27 10:01:37 +02008 "strconv"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +01009 "strings"
Filip Tehlarec5c40b2023-02-28 18:59:15 +010010 "syscall"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010011 "time"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010012
Filip Tehlar9abba112023-03-07 10:13:19 +010013 "github.com/edwarnicke/exechelper"
14
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010015 "go.fd.io/govpp"
16 "go.fd.io/govpp/api"
17 "go.fd.io/govpp/binapi/af_packet"
18 interfaces "go.fd.io/govpp/binapi/interface"
19 "go.fd.io/govpp/binapi/interface_types"
20 "go.fd.io/govpp/binapi/session"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010021 "go.fd.io/govpp/binapi/tapv2"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010022 "go.fd.io/govpp/binapi/vpe"
23 "go.fd.io/govpp/core"
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010024)
25
26const vppConfigTemplate = `unix {
27 nodaemon
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010028 log %[1]s%[4]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010029 full-coredump
30 cli-listen %[1]s%[2]s
31 runtime-dir %[1]s/var/run
32 gid vpp
33}
34
35api-trace {
36 on
37}
38
39api-segment {
40 gid vpp
41}
42
43socksvr {
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +010044 socket-name %[1]s%[3]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010045}
46
47statseg {
48 socket-name %[1]s/var/run/vpp/stats.sock
49}
50
51plugins {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010052 plugin default { disable }
53
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010054 plugin unittest_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010055 plugin quic_plugin.so { enable }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010056 plugin af_packet_plugin.so { enable }
57 plugin hs_apps_plugin.so { enable }
58 plugin http_plugin.so { enable }
Filip Tehlarcc1475c2023-11-29 12:59:05 +010059 plugin http_static_plugin.so { enable }
60 plugin prom_plugin.so { enable }
Filip Tehlar3336eef2023-11-29 07:40:18 +010061 plugin tlsopenssl_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010062}
63
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010064logging {
65 default-log-level debug
66 default-syslog-log-level debug
67}
68
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010069`
70
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010071const (
72 defaultCliSocketFilePath = "/var/run/vpp/cli.sock"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010073 defaultApiSocketFilePath = "/var/run/vpp/api.sock"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010074 defaultLogFilePath = "/var/log/vpp/vpp.log"
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010075)
76
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010077type VppInstance struct {
Maros Ondrejicka300f70d2023-02-21 10:53:20 +010078 container *Container
Filip Tehlar608d0062023-04-28 10:29:47 +020079 additionalConfig []Stanza
Maros Ondrejicka300f70d2023-02-21 10:53:20 +010080 connection *core.Connection
81 apiChannel api.Channel
Filip Tehlar608d0062023-04-28 10:29:47 +020082 cpus []int
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010083}
84
Maros Ondrejickae7625d02023-02-28 16:55:01 +010085func (vpp *VppInstance) getSuite() *HstSuite {
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010086 return vpp.container.suite
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010087}
88
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010089func (vpp *VppInstance) getCliSocket() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010090 return fmt.Sprintf("%s%s", vpp.container.getContainerWorkDir(), defaultCliSocketFilePath)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010091}
92
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010093func (vpp *VppInstance) getRunDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010094 return vpp.container.getContainerWorkDir() + "/var/run/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010095}
96
97func (vpp *VppInstance) getLogDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010098 return vpp.container.getContainerWorkDir() + "/var/log/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010099}
100
101func (vpp *VppInstance) getEtcDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100102 return vpp.container.getContainerWorkDir() + "/etc/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100103}
104
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100105func (vpp *VppInstance) start() error {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100106 // Create folders
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100107 containerWorkDir := vpp.container.getContainerWorkDir()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100108
109 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir())
110 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir())
111 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
112
113 // Create startup.conf inside the container
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100114 configContent := fmt.Sprintf(
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100115 vppConfigTemplate,
116 containerWorkDir,
117 defaultCliSocketFilePath,
118 defaultApiSocketFilePath,
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100119 defaultLogFilePath,
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100120 )
Filip Tehlar608d0062023-04-28 10:29:47 +0200121 configContent += vpp.generateCpuConfig()
122 for _, c := range vpp.additionalConfig {
123 configContent += c.toString()
124 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100125 startupFileName := vpp.getEtcDir() + "/startup.conf"
126 vpp.container.createFile(startupFileName, configContent)
127
Filip Tehlar1a661502023-03-08 11:55:50 +0100128 // create wrapper script for vppctl with proper CLI socket path
129 cliContent := "#!/usr/bin/bash\nvppctl -s " + vpp.getRunDir() + "/cli.sock"
130 vppcliFileName := "/usr/bin/vppcli"
131 vpp.container.createFile(vppcliFileName, cliContent)
132 vpp.container.exec("chmod 0755 " + vppcliFileName)
133
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100134 if *isVppDebug {
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100135 sig := make(chan os.Signal, 1)
136 signal.Notify(sig, syscall.SIGINT)
137 cont := make(chan bool, 1)
138 go func() {
Filip Tehlar9abba112023-03-07 10:13:19 +0100139 <-sig
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100140 cont <- true
141 }()
142
Filip Tehlara6b1a7d2023-09-02 08:39:25 +0200143 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100144 fmt.Println("run following command in different terminal:")
Filip Tehlara6b1a7d2023-09-02 08:39:25 +0200145 fmt.Println("docker exec -it " + vpp.container.name + " gdb -ex \"attach $(docker exec " + vpp.container.name + " pidof vpp)\"")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100146 fmt.Println("Afterwards press CTRL+C to continue")
147 <-cont
148 fmt.Println("continuing...")
149 } else {
150 // Start VPP
151 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
152 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100153
154 // Connect to VPP and store the connection
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100155 sockAddress := vpp.container.getHostWorkDir() + defaultApiSocketFilePath
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100156 conn, connEv, err := govpp.AsyncConnect(
157 sockAddress,
158 core.DefaultMaxReconnectAttempts,
159 core.DefaultReconnectInterval)
160 if err != nil {
161 fmt.Println("async connect error: ", err)
Filip Tehlar56e17cf2024-01-11 17:17:33 +0100162 return err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100163 }
164 vpp.connection = conn
165
166 // ... wait for Connected event
167 e := <-connEv
168 if e.State != core.Connected {
169 fmt.Println("connecting to VPP failed: ", e.Error)
170 }
171
172 // ... check compatibility of used messages
173 ch, err := conn.NewAPIChannel()
174 if err != nil {
175 fmt.Println("creating channel failed: ", err)
Filip Tehlar56e17cf2024-01-11 17:17:33 +0100176 return err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100177 }
178 if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
179 fmt.Println("compatibility error: ", err)
Filip Tehlar56e17cf2024-01-11 17:17:33 +0100180 return err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100181 }
182 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
183 fmt.Println("compatibility error: ", err)
Filip Tehlar56e17cf2024-01-11 17:17:33 +0100184 return err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100185 }
186 vpp.apiChannel = ch
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100187
188 return nil
189}
190
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100191func (vpp *VppInstance) vppctl(command string, arguments ...any) string {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100192 vppCliCommand := fmt.Sprintf(command, arguments...)
193 containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
194 vpp.container.name, vpp.getCliSocket(), vppCliCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100195 vpp.getSuite().log(containerExecCommand)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100196 output, err := exechelper.CombinedOutput(containerExecCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100197 vpp.getSuite().assertNil(err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100198
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100199 return string(output)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100200}
201
Filip Tehlar543cd572023-06-27 10:01:37 +0200202func (vpp *VppInstance) GetSessionStat(stat string) int {
203 o := vpp.vppctl("show session stats")
204 vpp.getSuite().log(o)
205 for _, line := range strings.Split(o, "\n") {
206 if strings.Contains(line, stat) {
207 tokens := strings.Split(strings.TrimSpace(line), " ")
208 val, err := strconv.Atoi(tokens[0])
209 if err != nil {
210 vpp.getSuite().FailNow("failed to parse stat value %s", err)
211 return 0
212 }
213 return val
214 }
215 }
216 return 0
217}
218
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100219func (vpp *VppInstance) waitForApp(appName string, timeout int) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100220 for i := 0; i < timeout; i++ {
221 o := vpp.vppctl("show app")
222 if strings.Contains(o, appName) {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100223 return
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100224 }
225 time.Sleep(1 * time.Second)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100226 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100227 vpp.getSuite().assertNil(1, "Timeout while waiting for app '%s'", appName)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100228}
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100229
230func (vpp *VppInstance) createAfPacket(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100231 veth *NetInterface,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100232) (interface_types.InterfaceIndex, error) {
233 createReq := &af_packet.AfPacketCreateV2{
234 UseRandomHwAddr: true,
235 HostIfName: veth.Name(),
236 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100237 if veth.hwAddress != (MacAddress{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100238 createReq.UseRandomHwAddr = false
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100239 createReq.HwAddr = veth.hwAddress
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100240 }
241 createReply := &af_packet.AfPacketCreateV2Reply{}
242
243 if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil {
244 return 0, err
245 }
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100246 veth.index = createReply.SwIfIndex
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100247
248 // Set to up
249 upReq := &interfaces.SwInterfaceSetFlags{
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100250 SwIfIndex: veth.index,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100251 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
252 }
253 upReply := &interfaces.SwInterfaceSetFlagsReply{}
254
255 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
256 return 0, err
257 }
258
259 // Add address
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100260 if veth.addressWithPrefix() == (AddressWithPrefix{}) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100261 var err error
262 var ip4Address string
Filip Tehlar3a910ab2023-06-08 17:39:39 +0200263 if ip4Address, err = veth.ip4AddrAllocator.NewIp4InterfaceAddress(veth.peer.networkNumber); err == nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100264 veth.ip4Address = ip4Address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100265 } else {
266 return 0, err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100267 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100268 }
269 addressReq := &interfaces.SwInterfaceAddDelAddress{
270 IsAdd: true,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100271 SwIfIndex: veth.index,
272 Prefix: veth.addressWithPrefix(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100273 }
274 addressReply := &interfaces.SwInterfaceAddDelAddressReply{}
275
276 if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil {
277 return 0, err
278 }
279
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100280 return veth.index, nil
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100281}
282
283func (vpp *VppInstance) addAppNamespace(
284 secret uint64,
285 ifx interface_types.InterfaceIndex,
286 namespaceId string,
287) error {
288 req := &session.AppNamespaceAddDelV2{
289 Secret: secret,
290 SwIfIndex: ifx,
291 NamespaceID: namespaceId,
292 }
293 reply := &session.AppNamespaceAddDelV2Reply{}
294
295 if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil {
296 return err
297 }
298
299 sessionReq := &session.SessionEnableDisable{
300 IsEnable: true,
301 }
302 sessionReply := &session.SessionEnableDisableReply{}
303
304 if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil {
305 return err
306 }
307
308 return nil
309}
310
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100311func (vpp *VppInstance) createTap(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100312 tap *NetInterface,
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100313 tapId ...uint32,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100314) error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100315 var id uint32 = 1
316 if len(tapId) > 0 {
317 id = tapId[0]
318 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100319 createTapReq := &tapv2.TapCreateV2{
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100320 ID: id,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100321 HostIfNameSet: true,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100322 HostIfName: tap.Name(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100323 HostIP4PrefixSet: true,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100324 HostIP4Prefix: tap.ip4AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100325 }
326 createTapReply := &tapv2.TapCreateV2Reply{}
327
328 // Create tap interface
329 if err := vpp.apiChannel.SendRequest(createTapReq).ReceiveReply(createTapReply); err != nil {
330 return err
331 }
332
333 // Add address
334 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
335 IsAdd: true,
336 SwIfIndex: createTapReply.SwIfIndex,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100337 Prefix: tap.peer.addressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100338 }
339 addAddressReply := &interfaces.SwInterfaceAddDelAddressReply{}
340
341 if err := vpp.apiChannel.SendRequest(addAddressReq).ReceiveReply(addAddressReply); err != nil {
342 return err
343 }
344
345 // Set interface to up
346 upReq := &interfaces.SwInterfaceSetFlags{
347 SwIfIndex: createTapReply.SwIfIndex,
348 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
349 }
350 upReply := &interfaces.SwInterfaceSetFlagsReply{}
351
352 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
353 return err
354 }
355
356 return nil
357}
358
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100359func (vpp *VppInstance) saveLogs() {
360 logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100361 logSource := vpp.container.getHostWorkDir() + defaultLogFilePath
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100362 cmd := exec.Command("cp", logSource, logTarget)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100363 vpp.getSuite().T().Helper()
364 vpp.getSuite().log(cmd.String())
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100365 cmd.Run()
366}
367
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100368func (vpp *VppInstance) disconnect() {
369 vpp.connection.Disconnect()
370 vpp.apiChannel.Close()
371}
Filip Tehlar608d0062023-04-28 10:29:47 +0200372
373func (vpp *VppInstance) generateCpuConfig() string {
374 var c Stanza
375 var s string
376 if len(vpp.cpus) < 1 {
377 return ""
378 }
379 c.newStanza("cpu").
380 append(fmt.Sprintf("main-core %d", vpp.cpus[0]))
381 workers := vpp.cpus[1:]
382
383 if len(workers) > 0 {
384 for i := 0; i < len(workers); i++ {
385 if i != 0 {
386 s = s + ", "
387 }
388 s = s + fmt.Sprintf("%d", workers[i])
389 }
390 c.append(fmt.Sprintf("corelist-workers %s", s))
391 }
392 return c.close().toString()
393}