blob: a93921ed5be88d66406792cdad880e190ce49669 [file] [log] [blame]
Adrian Villin4677d922024-06-14 09:32:39 +02001package hst
Maros Ondrejicka11a03e92022-12-01 09:56:37 +01002
3import (
Adrian Villin46d66002024-05-15 04:33:41 -04004 "context"
Matus Fabiane99d2662024-07-19 16:04:09 +02005 "encoding/json"
Filip Tehlarf3ee2b62023-01-09 12:07:09 +01006 "fmt"
Matus Fabian82ad9662024-06-04 19:00:00 +02007 "go.fd.io/govpp/binapi/ethernet_types"
Adrian Villin637edda2024-05-06 06:55:34 -04008 "io"
Matus Fabian82ad9662024-06-04 19:00:00 +02009 "net"
Filip Tehlarec5c40b2023-02-28 18:59:15 +010010 "os"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010011 "os/exec"
Filip Tehlarec5c40b2023-02-28 18:59:15 +010012 "os/signal"
Filip Tehlar543cd572023-06-27 10:01:37 +020013 "strconv"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010014 "strings"
Filip Tehlarec5c40b2023-02-28 18:59:15 +010015 "syscall"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010016 "time"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010017
Filip Tehlar9abba112023-03-07 10:13:19 +010018 "github.com/edwarnicke/exechelper"
Adrian Villincee15aa2024-03-14 11:42:55 -040019 . "github.com/onsi/ginkgo/v2"
Adrian Villin46d66002024-05-15 04:33:41 -040020 "github.com/sirupsen/logrus"
Filip Tehlar9abba112023-03-07 10:13:19 +010021
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010022 "go.fd.io/govpp"
23 "go.fd.io/govpp/api"
24 "go.fd.io/govpp/binapi/af_packet"
25 interfaces "go.fd.io/govpp/binapi/interface"
26 "go.fd.io/govpp/binapi/interface_types"
27 "go.fd.io/govpp/binapi/session"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010028 "go.fd.io/govpp/binapi/tapv2"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010029 "go.fd.io/govpp/core"
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010030)
31
32const vppConfigTemplate = `unix {
33 nodaemon
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010034 log %[1]s%[4]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010035 full-coredump
Adrian Villin4995d0d2024-07-29 17:54:58 +020036 coredump-size unlimited
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010037 cli-listen %[1]s%[2]s
38 runtime-dir %[1]s/var/run
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010039}
40
41api-trace {
42 on
43}
44
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010045socksvr {
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +010046 socket-name %[1]s%[3]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010047}
48
49statseg {
50 socket-name %[1]s/var/run/vpp/stats.sock
51}
52
53plugins {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010054 plugin default { disable }
55
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010056 plugin unittest_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010057 plugin quic_plugin.so { enable }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010058 plugin af_packet_plugin.so { enable }
59 plugin hs_apps_plugin.so { enable }
60 plugin http_plugin.so { enable }
Filip Tehlarcc1475c2023-11-29 12:59:05 +010061 plugin http_static_plugin.so { enable }
62 plugin prom_plugin.so { enable }
Filip Tehlar3336eef2023-11-29 07:40:18 +010063 plugin tlsopenssl_plugin.so { enable }
Adrian Villincee15aa2024-03-14 11:42:55 -040064 plugin ping_plugin.so { enable }
Matus Fabianc899ab42024-04-22 13:42:00 +020065 plugin nsim_plugin.so { enable }
Matus Fabian82ad9662024-06-04 19:00:00 +020066 plugin mactime_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010067}
68
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010069logging {
70 default-log-level debug
71 default-syslog-log-level debug
72}
73
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010074`
75
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010076const (
77 defaultCliSocketFilePath = "/var/run/vpp/cli.sock"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010078 defaultApiSocketFilePath = "/var/run/vpp/api.sock"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010079 defaultLogFilePath = "/var/log/vpp/vpp.log"
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010080)
81
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010082type VppInstance struct {
Adrian Villin4677d922024-06-14 09:32:39 +020083 Container *Container
84 AdditionalConfig []Stanza
85 Connection *core.Connection
86 ApiStream api.Stream
87 Cpus []int
Hadi Rayan Al-Sandide0e85132024-06-24 10:28:58 +020088 CpuConfig VppCpuConfig
89}
90
91type VppCpuConfig struct {
92 PinMainCpu bool
93 PinWorkersCorelist bool
94 SkipCores int
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010095}
96
Matus Fabiane99d2662024-07-19 16:04:09 +020097type VppMemTrace struct {
98 Count int `json:"count"`
99 Size int `json:"bytes"`
100 Sample string `json:"sample"`
101 Traceback []string `json:"traceback"`
102}
103
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100104func (vpp *VppInstance) getSuite() *HstSuite {
Adrian Villin4677d922024-06-14 09:32:39 +0200105 return vpp.Container.Suite
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100106}
107
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100108func (vpp *VppInstance) getCliSocket() string {
Adrian Villin4677d922024-06-14 09:32:39 +0200109 return fmt.Sprintf("%s%s", vpp.Container.GetContainerWorkDir(), defaultCliSocketFilePath)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100110}
111
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100112func (vpp *VppInstance) getRunDir() string {
Adrian Villin4677d922024-06-14 09:32:39 +0200113 return vpp.Container.GetContainerWorkDir() + "/var/run/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100114}
115
116func (vpp *VppInstance) getLogDir() string {
Adrian Villin4677d922024-06-14 09:32:39 +0200117 return vpp.Container.GetContainerWorkDir() + "/var/log/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100118}
119
120func (vpp *VppInstance) getEtcDir() string {
Adrian Villin4677d922024-06-14 09:32:39 +0200121 return vpp.Container.GetContainerWorkDir() + "/etc/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100122}
123
Adrian Villin4677d922024-06-14 09:32:39 +0200124func (vpp *VppInstance) Start() error {
Adrian Villin93974e22024-05-30 06:10:59 -0400125 maxReconnectAttempts := 3
Adrian Villin637edda2024-05-06 06:55:34 -0400126 // Replace default logger in govpp with our own
127 govppLogger := logrus.New()
Adrian Villin4677d922024-06-14 09:32:39 +0200128 govppLogger.SetOutput(io.MultiWriter(vpp.getSuite().Logger.Writer(), GinkgoWriter))
Adrian Villin637edda2024-05-06 06:55:34 -0400129 core.SetLogger(govppLogger)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100130 // Create folders
Adrian Villin4677d922024-06-14 09:32:39 +0200131 containerWorkDir := vpp.Container.GetContainerWorkDir()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100132
Adrian Villin4677d922024-06-14 09:32:39 +0200133 vpp.Container.Exec("mkdir --mode=0700 -p " + vpp.getRunDir())
134 vpp.Container.Exec("mkdir --mode=0700 -p " + vpp.getLogDir())
135 vpp.Container.Exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100136
137 // Create startup.conf inside the container
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100138 configContent := fmt.Sprintf(
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100139 vppConfigTemplate,
140 containerWorkDir,
141 defaultCliSocketFilePath,
142 defaultApiSocketFilePath,
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100143 defaultLogFilePath,
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100144 )
Hadi Rayan Al-Sandide0e85132024-06-24 10:28:58 +0200145 configContent += vpp.generateVPPCpuConfig()
Adrian Villin4677d922024-06-14 09:32:39 +0200146 for _, c := range vpp.AdditionalConfig {
147 configContent += c.ToString()
Filip Tehlar608d0062023-04-28 10:29:47 +0200148 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100149 startupFileName := vpp.getEtcDir() + "/startup.conf"
Adrian Villin4677d922024-06-14 09:32:39 +0200150 vpp.Container.CreateFile(startupFileName, configContent)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100151
Filip Tehlar1a661502023-03-08 11:55:50 +0100152 // create wrapper script for vppctl with proper CLI socket path
153 cliContent := "#!/usr/bin/bash\nvppctl -s " + vpp.getRunDir() + "/cli.sock"
154 vppcliFileName := "/usr/bin/vppcli"
Adrian Villin4677d922024-06-14 09:32:39 +0200155 vpp.Container.CreateFile(vppcliFileName, cliContent)
156 vpp.Container.Exec("chmod 0755 " + vppcliFileName)
Filip Tehlar1a661502023-03-08 11:55:50 +0100157
Adrian Villin4677d922024-06-14 09:32:39 +0200158 vpp.getSuite().Log("starting vpp")
159 if *IsVppDebug {
Adrian Villin93974e22024-05-30 06:10:59 -0400160 // default = 3; VPP will timeout while debugging if there are not enough attempts
161 maxReconnectAttempts = 5000
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100162 sig := make(chan os.Signal, 1)
Adrian Villincee15aa2024-03-14 11:42:55 -0400163 signal.Notify(sig, syscall.SIGQUIT)
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100164 cont := make(chan bool, 1)
165 go func() {
Filip Tehlar9abba112023-03-07 10:13:19 +0100166 <-sig
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100167 cont <- true
168 }()
169
Adrian Villin4677d922024-06-14 09:32:39 +0200170 vpp.Container.ExecServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100171 fmt.Println("run following command in different terminal:")
Adrian Villin4677d922024-06-14 09:32:39 +0200172 fmt.Println("docker exec -it " + vpp.Container.Name + " gdb -ex \"attach $(docker exec " + vpp.Container.Name + " pidof vpp)\"")
Adrian Villincee15aa2024-03-14 11:42:55 -0400173 fmt.Println("Afterwards press CTRL+\\ to continue")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100174 <-cont
175 fmt.Println("continuing...")
176 } else {
177 // Start VPP
Adrian Villin4677d922024-06-14 09:32:39 +0200178 vpp.Container.ExecServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100179 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100180
Adrian Villin4677d922024-06-14 09:32:39 +0200181 vpp.getSuite().Log("connecting to vpp")
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100182 // Connect to VPP and store the connection
Adrian Villin4677d922024-06-14 09:32:39 +0200183 sockAddress := vpp.Container.GetHostWorkDir() + defaultApiSocketFilePath
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100184 conn, connEv, err := govpp.AsyncConnect(
185 sockAddress,
Adrian Villin93974e22024-05-30 06:10:59 -0400186 maxReconnectAttempts,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100187 core.DefaultReconnectInterval)
188 if err != nil {
Adrian Villin4677d922024-06-14 09:32:39 +0200189 vpp.getSuite().Log("async connect error: " + fmt.Sprint(err))
Filip Tehlar56e17cf2024-01-11 17:17:33 +0100190 return err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100191 }
Adrian Villin4677d922024-06-14 09:32:39 +0200192 vpp.Connection = conn
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100193
194 // ... wait for Connected event
195 e := <-connEv
196 if e.State != core.Connected {
Adrian Villin4677d922024-06-14 09:32:39 +0200197 vpp.getSuite().Log("connecting to VPP failed: " + fmt.Sprint(e.Error))
Hadi Rayan Al-Sandid0eccf452024-06-24 10:48:54 +0200198 return e.Error
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100199 }
200
Adrian Villin46d66002024-05-15 04:33:41 -0400201 ch, err := conn.NewStream(
202 context.Background(),
203 core.WithRequestSize(50),
204 core.WithReplySize(50),
Adrian Villin93974e22024-05-30 06:10:59 -0400205 core.WithReplyTimeout(time.Second*5))
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100206 if err != nil {
Adrian Villin4677d922024-06-14 09:32:39 +0200207 vpp.getSuite().Log("creating stream failed: " + fmt.Sprint(err))
Filip Tehlar56e17cf2024-01-11 17:17:33 +0100208 return err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100209 }
Adrian Villin4677d922024-06-14 09:32:39 +0200210 vpp.ApiStream = ch
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100211
212 return nil
213}
214
Adrian Villin4677d922024-06-14 09:32:39 +0200215func (vpp *VppInstance) Vppctl(command string, arguments ...any) string {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100216 vppCliCommand := fmt.Sprintf(command, arguments...)
217 containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
Adrian Villin4677d922024-06-14 09:32:39 +0200218 vpp.Container.Name, vpp.getCliSocket(), vppCliCommand)
219 vpp.getSuite().Log(containerExecCommand)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100220 output, err := exechelper.CombinedOutput(containerExecCommand)
Adrian Villin4677d922024-06-14 09:32:39 +0200221 vpp.getSuite().AssertNil(err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100222
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100223 return string(output)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100224}
225
Filip Tehlar543cd572023-06-27 10:01:37 +0200226func (vpp *VppInstance) GetSessionStat(stat string) int {
Adrian Villin4677d922024-06-14 09:32:39 +0200227 o := vpp.Vppctl("show session stats")
228 vpp.getSuite().Log(o)
Filip Tehlar543cd572023-06-27 10:01:37 +0200229 for _, line := range strings.Split(o, "\n") {
230 if strings.Contains(line, stat) {
231 tokens := strings.Split(strings.TrimSpace(line), " ")
232 val, err := strconv.Atoi(tokens[0])
233 if err != nil {
Adrian Villincee15aa2024-03-14 11:42:55 -0400234 Fail("failed to parse stat value %s" + fmt.Sprint(err))
Filip Tehlar543cd572023-06-27 10:01:37 +0200235 return 0
236 }
237 return val
238 }
239 }
240 return 0
241}
242
Adrian Villin4677d922024-06-14 09:32:39 +0200243func (vpp *VppInstance) WaitForApp(appName string, timeout int) {
244 vpp.getSuite().Log("waiting for app " + appName)
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100245 for i := 0; i < timeout; i++ {
Adrian Villin4677d922024-06-14 09:32:39 +0200246 o := vpp.Vppctl("show app")
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100247 if strings.Contains(o, appName) {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100248 return
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100249 }
250 time.Sleep(1 * time.Second)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100251 }
Adrian Villin4677d922024-06-14 09:32:39 +0200252 vpp.getSuite().AssertNil(1, "Timeout while waiting for app '%s'", appName)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100253}
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100254
255func (vpp *VppInstance) createAfPacket(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100256 veth *NetInterface,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100257) (interface_types.InterfaceIndex, error) {
Adrian Villin46d66002024-05-15 04:33:41 -0400258 createReq := &af_packet.AfPacketCreateV3{
259 Mode: 1,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100260 UseRandomHwAddr: true,
261 HostIfName: veth.Name(),
Adrian Villin46d66002024-05-15 04:33:41 -0400262 Flags: af_packet.AfPacketFlags(11),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100263 }
Adrian Villin4677d922024-06-14 09:32:39 +0200264 if veth.HwAddress != (MacAddress{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100265 createReq.UseRandomHwAddr = false
Adrian Villin4677d922024-06-14 09:32:39 +0200266 createReq.HwAddr = veth.HwAddress
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100267 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100268
Adrian Villin4677d922024-06-14 09:32:39 +0200269 vpp.getSuite().Log("create af-packet interface " + veth.Name())
270 if err := vpp.ApiStream.SendMsg(createReq); err != nil {
271 vpp.getSuite().HstFail()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100272 return 0, err
273 }
Adrian Villin4677d922024-06-14 09:32:39 +0200274 replymsg, err := vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400275 if err != nil {
276 return 0, err
277 }
278 reply := replymsg.(*af_packet.AfPacketCreateV3Reply)
279 err = api.RetvalToVPPApiError(reply.Retval)
280 if err != nil {
281 return 0, err
282 }
283
Adrian Villin4677d922024-06-14 09:32:39 +0200284 veth.Index = reply.SwIfIndex
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100285
286 // Set to up
287 upReq := &interfaces.SwInterfaceSetFlags{
Adrian Villin4677d922024-06-14 09:32:39 +0200288 SwIfIndex: veth.Index,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100289 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
290 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100291
Adrian Villin4677d922024-06-14 09:32:39 +0200292 vpp.getSuite().Log("set af-packet interface " + veth.Name() + " up")
293 if err := vpp.ApiStream.SendMsg(upReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400294 return 0, err
295 }
Adrian Villin4677d922024-06-14 09:32:39 +0200296 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400297 if err != nil {
298 return 0, err
299 }
300 reply2 := replymsg.(*interfaces.SwInterfaceSetFlagsReply)
301 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100302 return 0, err
303 }
304
305 // Add address
Adrian Villin4677d922024-06-14 09:32:39 +0200306 if veth.AddressWithPrefix() == (AddressWithPrefix{}) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100307 var err error
308 var ip4Address string
Adrian Villin4677d922024-06-14 09:32:39 +0200309 if ip4Address, err = veth.Ip4AddrAllocator.NewIp4InterfaceAddress(veth.Peer.NetworkNumber); err == nil {
310 veth.Ip4Address = ip4Address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100311 } else {
312 return 0, err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100313 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100314 }
315 addressReq := &interfaces.SwInterfaceAddDelAddress{
316 IsAdd: true,
Adrian Villin4677d922024-06-14 09:32:39 +0200317 SwIfIndex: veth.Index,
318 Prefix: veth.AddressWithPrefix(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100319 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100320
Adrian Villin4677d922024-06-14 09:32:39 +0200321 vpp.getSuite().Log("af-packet interface " + veth.Name() + " add address " + veth.Ip4Address)
322 if err := vpp.ApiStream.SendMsg(addressReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400323 return 0, err
324 }
Adrian Villin4677d922024-06-14 09:32:39 +0200325 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400326 if err != nil {
327 return 0, err
328 }
329 reply3 := replymsg.(*interfaces.SwInterfaceAddDelAddressReply)
330 err = api.RetvalToVPPApiError(reply3.Retval)
331 if err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100332 return 0, err
333 }
334
Adrian Villin4677d922024-06-14 09:32:39 +0200335 return veth.Index, nil
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100336}
337
338func (vpp *VppInstance) addAppNamespace(
339 secret uint64,
340 ifx interface_types.InterfaceIndex,
341 namespaceId string,
342) error {
Adrian Villin46d66002024-05-15 04:33:41 -0400343 req := &session.AppNamespaceAddDelV4{
344 IsAdd: true,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100345 Secret: secret,
346 SwIfIndex: ifx,
347 NamespaceID: namespaceId,
Adrian Villin46d66002024-05-15 04:33:41 -0400348 SockName: defaultApiSocketFilePath,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100349 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100350
Adrian Villin4677d922024-06-14 09:32:39 +0200351 vpp.getSuite().Log("add app namespace " + namespaceId)
352 if err := vpp.ApiStream.SendMsg(req); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400353 return err
354 }
Adrian Villin4677d922024-06-14 09:32:39 +0200355 replymsg, err := vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400356 if err != nil {
357 return err
358 }
359 reply := replymsg.(*session.AppNamespaceAddDelV4Reply)
360 if err = api.RetvalToVPPApiError(reply.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100361 return err
362 }
363
364 sessionReq := &session.SessionEnableDisable{
365 IsEnable: true,
366 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100367
Adrian Villin4677d922024-06-14 09:32:39 +0200368 vpp.getSuite().Log("enable app namespace " + namespaceId)
369 if err := vpp.ApiStream.SendMsg(sessionReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400370 return err
371 }
Adrian Villin4677d922024-06-14 09:32:39 +0200372 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400373 if err != nil {
374 return err
375 }
376 reply2 := replymsg.(*session.SessionEnableDisableReply)
377 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100378 return err
379 }
380
381 return nil
382}
383
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100384func (vpp *VppInstance) createTap(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100385 tap *NetInterface,
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100386 tapId ...uint32,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100387) error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100388 var id uint32 = 1
389 if len(tapId) > 0 {
390 id = tapId[0]
391 }
Adrian Villin46d66002024-05-15 04:33:41 -0400392 createTapReq := &tapv2.TapCreateV3{
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100393 ID: id,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100394 HostIfNameSet: true,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100395 HostIfName: tap.Name(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100396 HostIP4PrefixSet: true,
Adrian Villin4677d922024-06-14 09:32:39 +0200397 HostIP4Prefix: tap.Ip4AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100398 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100399
Adrian Villin4677d922024-06-14 09:32:39 +0200400 vpp.getSuite().Log("create tap interface " + tap.Name())
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100401 // Create tap interface
Adrian Villin4677d922024-06-14 09:32:39 +0200402 if err := vpp.ApiStream.SendMsg(createTapReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400403 return err
404 }
Adrian Villin4677d922024-06-14 09:32:39 +0200405 replymsg, err := vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400406 if err != nil {
407 return err
408 }
409 reply := replymsg.(*tapv2.TapCreateV3Reply)
410 if err = api.RetvalToVPPApiError(reply.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100411 return err
412 }
Adrian Villin4677d922024-06-14 09:32:39 +0200413 tap.Peer.Index = reply.SwIfIndex
Matus Fabian82ad9662024-06-04 19:00:00 +0200414
415 // Get name and mac
Adrian Villin4677d922024-06-14 09:32:39 +0200416 if err := vpp.ApiStream.SendMsg(&interfaces.SwInterfaceDump{
Matus Fabian82ad9662024-06-04 19:00:00 +0200417 SwIfIndex: reply.SwIfIndex,
418 }); err != nil {
419 return err
420 }
Adrian Villin4677d922024-06-14 09:32:39 +0200421 replymsg, err = vpp.ApiStream.RecvMsg()
Matus Fabian82ad9662024-06-04 19:00:00 +0200422 if err != nil {
423 return err
424 }
425 ifDetails := replymsg.(*interfaces.SwInterfaceDetails)
Adrian Villin4677d922024-06-14 09:32:39 +0200426 tap.Peer.name = ifDetails.InterfaceName
427 tap.Peer.HwAddress = ifDetails.L2Address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100428
429 // Add address
430 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
431 IsAdd: true,
Adrian Villin46d66002024-05-15 04:33:41 -0400432 SwIfIndex: reply.SwIfIndex,
Adrian Villin4677d922024-06-14 09:32:39 +0200433 Prefix: tap.Peer.AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100434 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100435
Adrian Villin4677d922024-06-14 09:32:39 +0200436 vpp.getSuite().Log("tap interface " + tap.Name() + " add address " + tap.Peer.Ip4Address)
437 if err := vpp.ApiStream.SendMsg(addAddressReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400438 return err
439 }
Adrian Villin4677d922024-06-14 09:32:39 +0200440 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400441 if err != nil {
442 return err
443 }
444 reply2 := replymsg.(*interfaces.SwInterfaceAddDelAddressReply)
445 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100446 return err
447 }
448
449 // Set interface to up
450 upReq := &interfaces.SwInterfaceSetFlags{
Adrian Villin46d66002024-05-15 04:33:41 -0400451 SwIfIndex: reply.SwIfIndex,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100452 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
453 }
Adrian Villin4677d922024-06-14 09:32:39 +0200454
455 vpp.getSuite().Log("set tap interface " + tap.Name() + " up")
456 if err := vpp.ApiStream.SendMsg(upReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400457 return err
458 }
Adrian Villin4677d922024-06-14 09:32:39 +0200459 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400460 if err != nil {
461 return err
462 }
463 reply3 := replymsg.(*interfaces.SwInterfaceSetFlagsReply)
464 if err = api.RetvalToVPPApiError(reply3.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100465 return err
466 }
467
Matus Fabian82ad9662024-06-04 19:00:00 +0200468 // Get host mac
469 netIntf, err := net.InterfaceByName(tap.Name())
470 if err == nil {
Adrian Villin4677d922024-06-14 09:32:39 +0200471 tap.HwAddress, _ = ethernet_types.ParseMacAddress(netIntf.HardwareAddr.String())
Matus Fabian82ad9662024-06-04 19:00:00 +0200472 }
473
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100474 return nil
475}
476
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100477func (vpp *VppInstance) saveLogs() {
Adrian Villin4995d0d2024-07-29 17:54:58 +0200478 logTarget := vpp.getSuite().getLogDirPath() + "vppinstance-" + vpp.Container.Name + ".log"
Adrian Villin4677d922024-06-14 09:32:39 +0200479 logSource := vpp.Container.GetHostWorkDir() + defaultLogFilePath
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100480 cmd := exec.Command("cp", logSource, logTarget)
Adrian Villin4677d922024-06-14 09:32:39 +0200481 vpp.getSuite().Log(cmd.String())
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100482 cmd.Run()
483}
484
Adrian Villin4677d922024-06-14 09:32:39 +0200485func (vpp *VppInstance) Disconnect() {
486 vpp.Connection.Disconnect()
487 vpp.ApiStream.Close()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100488}
Filip Tehlar608d0062023-04-28 10:29:47 +0200489
Hadi Rayan Al-Sandide0e85132024-06-24 10:28:58 +0200490func (vpp *VppInstance) setDefaultCpuConfig() {
491 vpp.CpuConfig.PinMainCpu = true
492 vpp.CpuConfig.PinWorkersCorelist = true
493 vpp.CpuConfig.SkipCores = 0
494}
495
496func (vpp *VppInstance) generateVPPCpuConfig() string {
Filip Tehlar608d0062023-04-28 10:29:47 +0200497 var c Stanza
498 var s string
Hadi Rayan Al-Sandide0e85132024-06-24 10:28:58 +0200499 startCpu := 0
Adrian Villin4677d922024-06-14 09:32:39 +0200500 if len(vpp.Cpus) < 1 {
Filip Tehlar608d0062023-04-28 10:29:47 +0200501 return ""
502 }
Hadi Rayan Al-Sandide0e85132024-06-24 10:28:58 +0200503
504 c.NewStanza("cpu")
505
506 // If skip-cores is valid, use as start value to assign main/workers CPUs
507 if vpp.CpuConfig.SkipCores != 0 {
508 c.Append(fmt.Sprintf("skip-cores %d", vpp.CpuConfig.SkipCores))
509 vpp.getSuite().Log(fmt.Sprintf("skip-cores %d", vpp.CpuConfig.SkipCores))
510 }
511
512 if len(vpp.Cpus) > vpp.CpuConfig.SkipCores {
513 startCpu = vpp.CpuConfig.SkipCores
514 }
515
516 if vpp.CpuConfig.PinMainCpu {
517 c.Append(fmt.Sprintf("main-core %d", vpp.Cpus[startCpu]))
518 vpp.getSuite().Log(fmt.Sprintf("main-core %d", vpp.Cpus[startCpu]))
519 }
520
521 workers := vpp.Cpus[startCpu+1:]
Filip Tehlar608d0062023-04-28 10:29:47 +0200522
523 if len(workers) > 0 {
Hadi Rayan Al-Sandide0e85132024-06-24 10:28:58 +0200524 if vpp.CpuConfig.PinWorkersCorelist {
525 for i := 0; i < len(workers); i++ {
526 if i != 0 {
527 s = s + ", "
528 }
529 s = s + fmt.Sprintf("%d", workers[i])
Filip Tehlar608d0062023-04-28 10:29:47 +0200530 }
Hadi Rayan Al-Sandide0e85132024-06-24 10:28:58 +0200531 c.Append(fmt.Sprintf("corelist-workers %s", s))
532 vpp.getSuite().Log("corelist-workers " + s)
533 } else {
534 s = fmt.Sprintf("%d", len(workers))
535 c.Append(fmt.Sprintf("workers %s", s))
536 vpp.getSuite().Log("workers " + s)
Filip Tehlar608d0062023-04-28 10:29:47 +0200537 }
Filip Tehlar608d0062023-04-28 10:29:47 +0200538 }
Hadi Rayan Al-Sandide0e85132024-06-24 10:28:58 +0200539
Adrian Villin4677d922024-06-14 09:32:39 +0200540 return c.Close().ToString()
Filip Tehlar608d0062023-04-28 10:29:47 +0200541}
Matus Fabiane99d2662024-07-19 16:04:09 +0200542
543// EnableMemoryTrace enables memory traces of VPP main-heap
544func (vpp *VppInstance) EnableMemoryTrace() {
545 vpp.getSuite().Log(vpp.Vppctl("memory-trace on main-heap"))
546}
547
548// GetMemoryTrace dumps memory traces for analysis
549func (vpp *VppInstance) GetMemoryTrace() ([]VppMemTrace, error) {
550 var trace []VppMemTrace
551 vpp.getSuite().Log(vpp.Vppctl("save memory-trace trace.json"))
552 err := vpp.Container.GetFile("/tmp/trace.json", "/tmp/trace.json")
553 if err != nil {
554 return nil, err
555 }
556 fileBytes, err := os.ReadFile("/tmp/trace.json")
557 if err != nil {
558 return nil, err
559 }
560 err = json.Unmarshal(fileBytes, &trace)
561 if err != nil {
562 return nil, err
563 }
564 return trace, nil
565}
566
567// memTracesSuppressCli filter out CLI related samples
568func memTracesSuppressCli(traces []VppMemTrace) []VppMemTrace {
569 var filtered []VppMemTrace
570 for i := 0; i < len(traces); i++ {
571 isCli := false
572 for j := 0; j < len(traces[i].Traceback); j++ {
573 if strings.Contains(traces[i].Traceback[j], "unix_cli") {
574 isCli = true
575 break
576 }
577 }
578 if !isCli {
579 filtered = append(filtered, traces[i])
580 }
581 }
582 return filtered
583}
584
585// MemLeakCheck compares memory traces at different point in time, analyzes if memory leaks happen and produces report
586func (vpp *VppInstance) MemLeakCheck(first, second []VppMemTrace) {
587 totalBytes := 0
588 totalCounts := 0
589 trace1 := memTracesSuppressCli(first)
590 trace2 := memTracesSuppressCli(second)
591 report := ""
592 for i := 0; i < len(trace2); i++ {
593 match := false
594 for j := 0; j < len(trace1); j++ {
595 if trace1[j].Sample == trace2[i].Sample {
596 if trace2[i].Size > trace1[j].Size {
597 deltaBytes := trace2[i].Size - trace1[j].Size
598 deltaCounts := trace2[i].Count - trace1[j].Count
599 report += fmt.Sprintf("grow %d byte(s) in %d allocation(s) from:\n", deltaBytes, deltaCounts)
600 for j := 0; j < len(trace2[i].Traceback); j++ {
601 report += fmt.Sprintf("\t#%d %s\n", j, trace2[i].Traceback[j])
602 }
603 totalBytes += deltaBytes
604 totalCounts += deltaCounts
605 }
606 match = true
607 break
608 }
609 }
610 if !match {
611 report += fmt.Sprintf("\nleak of %d byte(s) in %d allocation(s) from:\n", trace2[i].Size, trace2[i].Count)
612 for j := 0; j < len(trace2[i].Traceback); j++ {
613 report += fmt.Sprintf("\t#%d %s\n", j, trace2[i].Traceback[j])
614 }
615 totalBytes += trace2[i].Size
616 totalCounts += trace2[i].Count
617 }
618 }
619 summary := fmt.Sprintf("\nSUMMARY: %d byte(s) leaked in %d allocation(s)\n", totalBytes, totalCounts)
620 AddReportEntry(summary, report)
621}