blob: 5164a54aa9a4b00f233dbc4f81be2992ce0078a1 [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"
Filip Tehlarf3ee2b62023-01-09 12:07:09 +01005 "fmt"
Matus Fabian82ad9662024-06-04 19:00:00 +02006 "go.fd.io/govpp/binapi/ethernet_types"
Adrian Villin637edda2024-05-06 06:55:34 -04007 "io"
Matus Fabian82ad9662024-06-04 19:00:00 +02008 "net"
Filip Tehlarec5c40b2023-02-28 18:59:15 +01009 "os"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010010 "os/exec"
Filip Tehlarec5c40b2023-02-28 18:59:15 +010011 "os/signal"
Filip Tehlar543cd572023-06-27 10:01:37 +020012 "strconv"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010013 "strings"
Filip Tehlarec5c40b2023-02-28 18:59:15 +010014 "syscall"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010015 "time"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010016
Filip Tehlar9abba112023-03-07 10:13:19 +010017 "github.com/edwarnicke/exechelper"
Adrian Villincee15aa2024-03-14 11:42:55 -040018 . "github.com/onsi/ginkgo/v2"
Adrian Villin46d66002024-05-15 04:33:41 -040019 "github.com/sirupsen/logrus"
Filip Tehlar9abba112023-03-07 10:13:19 +010020
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010021 "go.fd.io/govpp"
22 "go.fd.io/govpp/api"
23 "go.fd.io/govpp/binapi/af_packet"
24 interfaces "go.fd.io/govpp/binapi/interface"
25 "go.fd.io/govpp/binapi/interface_types"
26 "go.fd.io/govpp/binapi/session"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010027 "go.fd.io/govpp/binapi/tapv2"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010028 "go.fd.io/govpp/core"
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010029)
30
31const vppConfigTemplate = `unix {
32 nodaemon
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010033 log %[1]s%[4]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010034 full-coredump
35 cli-listen %[1]s%[2]s
36 runtime-dir %[1]s/var/run
37 gid vpp
38}
39
40api-trace {
41 on
42}
43
44api-segment {
45 gid vpp
46}
47
48socksvr {
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +010049 socket-name %[1]s%[3]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010050}
51
52statseg {
53 socket-name %[1]s/var/run/vpp/stats.sock
54}
55
56plugins {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010057 plugin default { disable }
58
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010059 plugin unittest_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010060 plugin quic_plugin.so { enable }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010061 plugin af_packet_plugin.so { enable }
62 plugin hs_apps_plugin.so { enable }
63 plugin http_plugin.so { enable }
Filip Tehlarcc1475c2023-11-29 12:59:05 +010064 plugin http_static_plugin.so { enable }
65 plugin prom_plugin.so { enable }
Filip Tehlar3336eef2023-11-29 07:40:18 +010066 plugin tlsopenssl_plugin.so { enable }
Adrian Villincee15aa2024-03-14 11:42:55 -040067 plugin ping_plugin.so { enable }
Matus Fabianc899ab42024-04-22 13:42:00 +020068 plugin nsim_plugin.so { enable }
Matus Fabian82ad9662024-06-04 19:00:00 +020069 plugin mactime_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010070}
71
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010072logging {
73 default-log-level debug
74 default-syslog-log-level debug
75}
76
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010077`
78
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010079const (
80 defaultCliSocketFilePath = "/var/run/vpp/cli.sock"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010081 defaultApiSocketFilePath = "/var/run/vpp/api.sock"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010082 defaultLogFilePath = "/var/log/vpp/vpp.log"
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010083)
84
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010085type VppInstance struct {
Adrian Villin4677d922024-06-14 09:32:39 +020086 Container *Container
87 AdditionalConfig []Stanza
88 Connection *core.Connection
89 ApiStream api.Stream
90 Cpus []int
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010091}
92
Maros Ondrejickae7625d02023-02-28 16:55:01 +010093func (vpp *VppInstance) getSuite() *HstSuite {
Adrian Villin4677d922024-06-14 09:32:39 +020094 return vpp.Container.Suite
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010095}
96
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010097func (vpp *VppInstance) getCliSocket() string {
Adrian Villin4677d922024-06-14 09:32:39 +020098 return fmt.Sprintf("%s%s", vpp.Container.GetContainerWorkDir(), defaultCliSocketFilePath)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010099}
100
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100101func (vpp *VppInstance) getRunDir() string {
Adrian Villin4677d922024-06-14 09:32:39 +0200102 return vpp.Container.GetContainerWorkDir() + "/var/run/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100103}
104
105func (vpp *VppInstance) getLogDir() string {
Adrian Villin4677d922024-06-14 09:32:39 +0200106 return vpp.Container.GetContainerWorkDir() + "/var/log/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100107}
108
109func (vpp *VppInstance) getEtcDir() string {
Adrian Villin4677d922024-06-14 09:32:39 +0200110 return vpp.Container.GetContainerWorkDir() + "/etc/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100111}
112
Adrian Villin4677d922024-06-14 09:32:39 +0200113func (vpp *VppInstance) Start() error {
Adrian Villin93974e22024-05-30 06:10:59 -0400114 maxReconnectAttempts := 3
Adrian Villin637edda2024-05-06 06:55:34 -0400115 // Replace default logger in govpp with our own
116 govppLogger := logrus.New()
Adrian Villin4677d922024-06-14 09:32:39 +0200117 govppLogger.SetOutput(io.MultiWriter(vpp.getSuite().Logger.Writer(), GinkgoWriter))
Adrian Villin637edda2024-05-06 06:55:34 -0400118 core.SetLogger(govppLogger)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100119 // Create folders
Adrian Villin4677d922024-06-14 09:32:39 +0200120 containerWorkDir := vpp.Container.GetContainerWorkDir()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100121
Adrian Villin4677d922024-06-14 09:32:39 +0200122 vpp.Container.Exec("mkdir --mode=0700 -p " + vpp.getRunDir())
123 vpp.Container.Exec("mkdir --mode=0700 -p " + vpp.getLogDir())
124 vpp.Container.Exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100125
126 // Create startup.conf inside the container
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100127 configContent := fmt.Sprintf(
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100128 vppConfigTemplate,
129 containerWorkDir,
130 defaultCliSocketFilePath,
131 defaultApiSocketFilePath,
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100132 defaultLogFilePath,
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100133 )
Filip Tehlar608d0062023-04-28 10:29:47 +0200134 configContent += vpp.generateCpuConfig()
Adrian Villin4677d922024-06-14 09:32:39 +0200135 for _, c := range vpp.AdditionalConfig {
136 configContent += c.ToString()
Filip Tehlar608d0062023-04-28 10:29:47 +0200137 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100138 startupFileName := vpp.getEtcDir() + "/startup.conf"
Adrian Villin4677d922024-06-14 09:32:39 +0200139 vpp.Container.CreateFile(startupFileName, configContent)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100140
Filip Tehlar1a661502023-03-08 11:55:50 +0100141 // create wrapper script for vppctl with proper CLI socket path
142 cliContent := "#!/usr/bin/bash\nvppctl -s " + vpp.getRunDir() + "/cli.sock"
143 vppcliFileName := "/usr/bin/vppcli"
Adrian Villin4677d922024-06-14 09:32:39 +0200144 vpp.Container.CreateFile(vppcliFileName, cliContent)
145 vpp.Container.Exec("chmod 0755 " + vppcliFileName)
Filip Tehlar1a661502023-03-08 11:55:50 +0100146
Adrian Villin4677d922024-06-14 09:32:39 +0200147 vpp.getSuite().Log("starting vpp")
148 if *IsVppDebug {
Adrian Villin93974e22024-05-30 06:10:59 -0400149 // default = 3; VPP will timeout while debugging if there are not enough attempts
150 maxReconnectAttempts = 5000
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100151 sig := make(chan os.Signal, 1)
Adrian Villincee15aa2024-03-14 11:42:55 -0400152 signal.Notify(sig, syscall.SIGQUIT)
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100153 cont := make(chan bool, 1)
154 go func() {
Filip Tehlar9abba112023-03-07 10:13:19 +0100155 <-sig
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100156 cont <- true
157 }()
158
Adrian Villin4677d922024-06-14 09:32:39 +0200159 vpp.Container.ExecServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100160 fmt.Println("run following command in different terminal:")
Adrian Villin4677d922024-06-14 09:32:39 +0200161 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 -0400162 fmt.Println("Afterwards press CTRL+\\ to continue")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100163 <-cont
164 fmt.Println("continuing...")
165 } else {
166 // Start VPP
Adrian Villin4677d922024-06-14 09:32:39 +0200167 vpp.Container.ExecServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100168 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100169
Adrian Villin4677d922024-06-14 09:32:39 +0200170 vpp.getSuite().Log("connecting to vpp")
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100171 // Connect to VPP and store the connection
Adrian Villin4677d922024-06-14 09:32:39 +0200172 sockAddress := vpp.Container.GetHostWorkDir() + defaultApiSocketFilePath
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100173 conn, connEv, err := govpp.AsyncConnect(
174 sockAddress,
Adrian Villin93974e22024-05-30 06:10:59 -0400175 maxReconnectAttempts,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100176 core.DefaultReconnectInterval)
177 if err != nil {
Adrian Villin4677d922024-06-14 09:32:39 +0200178 vpp.getSuite().Log("async connect error: " + fmt.Sprint(err))
Filip Tehlar56e17cf2024-01-11 17:17:33 +0100179 return err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100180 }
Adrian Villin4677d922024-06-14 09:32:39 +0200181 vpp.Connection = conn
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100182
183 // ... wait for Connected event
184 e := <-connEv
185 if e.State != core.Connected {
Adrian Villin4677d922024-06-14 09:32:39 +0200186 vpp.getSuite().Log("connecting to VPP failed: " + fmt.Sprint(e.Error))
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100187 }
188
Adrian Villin46d66002024-05-15 04:33:41 -0400189 ch, err := conn.NewStream(
190 context.Background(),
191 core.WithRequestSize(50),
192 core.WithReplySize(50),
Adrian Villin93974e22024-05-30 06:10:59 -0400193 core.WithReplyTimeout(time.Second*5))
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100194 if err != nil {
Adrian Villin4677d922024-06-14 09:32:39 +0200195 vpp.getSuite().Log("creating stream failed: " + fmt.Sprint(err))
Filip Tehlar56e17cf2024-01-11 17:17:33 +0100196 return err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100197 }
Adrian Villin4677d922024-06-14 09:32:39 +0200198 vpp.ApiStream = ch
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100199
200 return nil
201}
202
Adrian Villin4677d922024-06-14 09:32:39 +0200203func (vpp *VppInstance) Vppctl(command string, arguments ...any) string {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100204 vppCliCommand := fmt.Sprintf(command, arguments...)
205 containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
Adrian Villin4677d922024-06-14 09:32:39 +0200206 vpp.Container.Name, vpp.getCliSocket(), vppCliCommand)
207 vpp.getSuite().Log(containerExecCommand)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100208 output, err := exechelper.CombinedOutput(containerExecCommand)
Adrian Villin4677d922024-06-14 09:32:39 +0200209 vpp.getSuite().AssertNil(err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100210
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100211 return string(output)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100212}
213
Filip Tehlar543cd572023-06-27 10:01:37 +0200214func (vpp *VppInstance) GetSessionStat(stat string) int {
Adrian Villin4677d922024-06-14 09:32:39 +0200215 o := vpp.Vppctl("show session stats")
216 vpp.getSuite().Log(o)
Filip Tehlar543cd572023-06-27 10:01:37 +0200217 for _, line := range strings.Split(o, "\n") {
218 if strings.Contains(line, stat) {
219 tokens := strings.Split(strings.TrimSpace(line), " ")
220 val, err := strconv.Atoi(tokens[0])
221 if err != nil {
Adrian Villincee15aa2024-03-14 11:42:55 -0400222 Fail("failed to parse stat value %s" + fmt.Sprint(err))
Filip Tehlar543cd572023-06-27 10:01:37 +0200223 return 0
224 }
225 return val
226 }
227 }
228 return 0
229}
230
Adrian Villin4677d922024-06-14 09:32:39 +0200231func (vpp *VppInstance) WaitForApp(appName string, timeout int) {
232 vpp.getSuite().Log("waiting for app " + appName)
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100233 for i := 0; i < timeout; i++ {
Adrian Villin4677d922024-06-14 09:32:39 +0200234 o := vpp.Vppctl("show app")
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100235 if strings.Contains(o, appName) {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100236 return
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100237 }
238 time.Sleep(1 * time.Second)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100239 }
Adrian Villin4677d922024-06-14 09:32:39 +0200240 vpp.getSuite().AssertNil(1, "Timeout while waiting for app '%s'", appName)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100241}
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100242
243func (vpp *VppInstance) createAfPacket(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100244 veth *NetInterface,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100245) (interface_types.InterfaceIndex, error) {
Adrian Villin46d66002024-05-15 04:33:41 -0400246 createReq := &af_packet.AfPacketCreateV3{
247 Mode: 1,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100248 UseRandomHwAddr: true,
249 HostIfName: veth.Name(),
Adrian Villin46d66002024-05-15 04:33:41 -0400250 Flags: af_packet.AfPacketFlags(11),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100251 }
Adrian Villin4677d922024-06-14 09:32:39 +0200252 if veth.HwAddress != (MacAddress{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100253 createReq.UseRandomHwAddr = false
Adrian Villin4677d922024-06-14 09:32:39 +0200254 createReq.HwAddr = veth.HwAddress
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100255 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100256
Adrian Villin4677d922024-06-14 09:32:39 +0200257 vpp.getSuite().Log("create af-packet interface " + veth.Name())
258 if err := vpp.ApiStream.SendMsg(createReq); err != nil {
259 vpp.getSuite().HstFail()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100260 return 0, err
261 }
Adrian Villin4677d922024-06-14 09:32:39 +0200262 replymsg, err := vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400263 if err != nil {
264 return 0, err
265 }
266 reply := replymsg.(*af_packet.AfPacketCreateV3Reply)
267 err = api.RetvalToVPPApiError(reply.Retval)
268 if err != nil {
269 return 0, err
270 }
271
Adrian Villin4677d922024-06-14 09:32:39 +0200272 veth.Index = reply.SwIfIndex
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100273
274 // Set to up
275 upReq := &interfaces.SwInterfaceSetFlags{
Adrian Villin4677d922024-06-14 09:32:39 +0200276 SwIfIndex: veth.Index,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100277 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
278 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100279
Adrian Villin4677d922024-06-14 09:32:39 +0200280 vpp.getSuite().Log("set af-packet interface " + veth.Name() + " up")
281 if err := vpp.ApiStream.SendMsg(upReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400282 return 0, err
283 }
Adrian Villin4677d922024-06-14 09:32:39 +0200284 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400285 if err != nil {
286 return 0, err
287 }
288 reply2 := replymsg.(*interfaces.SwInterfaceSetFlagsReply)
289 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100290 return 0, err
291 }
292
293 // Add address
Adrian Villin4677d922024-06-14 09:32:39 +0200294 if veth.AddressWithPrefix() == (AddressWithPrefix{}) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100295 var err error
296 var ip4Address string
Adrian Villin4677d922024-06-14 09:32:39 +0200297 if ip4Address, err = veth.Ip4AddrAllocator.NewIp4InterfaceAddress(veth.Peer.NetworkNumber); err == nil {
298 veth.Ip4Address = ip4Address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100299 } else {
300 return 0, err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100301 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100302 }
303 addressReq := &interfaces.SwInterfaceAddDelAddress{
304 IsAdd: true,
Adrian Villin4677d922024-06-14 09:32:39 +0200305 SwIfIndex: veth.Index,
306 Prefix: veth.AddressWithPrefix(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100307 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100308
Adrian Villin4677d922024-06-14 09:32:39 +0200309 vpp.getSuite().Log("af-packet interface " + veth.Name() + " add address " + veth.Ip4Address)
310 if err := vpp.ApiStream.SendMsg(addressReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400311 return 0, err
312 }
Adrian Villin4677d922024-06-14 09:32:39 +0200313 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400314 if err != nil {
315 return 0, err
316 }
317 reply3 := replymsg.(*interfaces.SwInterfaceAddDelAddressReply)
318 err = api.RetvalToVPPApiError(reply3.Retval)
319 if err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100320 return 0, err
321 }
322
Adrian Villin4677d922024-06-14 09:32:39 +0200323 return veth.Index, nil
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100324}
325
326func (vpp *VppInstance) addAppNamespace(
327 secret uint64,
328 ifx interface_types.InterfaceIndex,
329 namespaceId string,
330) error {
Adrian Villin46d66002024-05-15 04:33:41 -0400331 req := &session.AppNamespaceAddDelV4{
332 IsAdd: true,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100333 Secret: secret,
334 SwIfIndex: ifx,
335 NamespaceID: namespaceId,
Adrian Villin46d66002024-05-15 04:33:41 -0400336 SockName: defaultApiSocketFilePath,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100337 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100338
Adrian Villin4677d922024-06-14 09:32:39 +0200339 vpp.getSuite().Log("add app namespace " + namespaceId)
340 if err := vpp.ApiStream.SendMsg(req); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400341 return err
342 }
Adrian Villin4677d922024-06-14 09:32:39 +0200343 replymsg, err := vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400344 if err != nil {
345 return err
346 }
347 reply := replymsg.(*session.AppNamespaceAddDelV4Reply)
348 if err = api.RetvalToVPPApiError(reply.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100349 return err
350 }
351
352 sessionReq := &session.SessionEnableDisable{
353 IsEnable: true,
354 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100355
Adrian Villin4677d922024-06-14 09:32:39 +0200356 vpp.getSuite().Log("enable app namespace " + namespaceId)
357 if err := vpp.ApiStream.SendMsg(sessionReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400358 return err
359 }
Adrian Villin4677d922024-06-14 09:32:39 +0200360 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400361 if err != nil {
362 return err
363 }
364 reply2 := replymsg.(*session.SessionEnableDisableReply)
365 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100366 return err
367 }
368
369 return nil
370}
371
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100372func (vpp *VppInstance) createTap(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100373 tap *NetInterface,
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100374 tapId ...uint32,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100375) error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100376 var id uint32 = 1
377 if len(tapId) > 0 {
378 id = tapId[0]
379 }
Adrian Villin46d66002024-05-15 04:33:41 -0400380 createTapReq := &tapv2.TapCreateV3{
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100381 ID: id,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100382 HostIfNameSet: true,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100383 HostIfName: tap.Name(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100384 HostIP4PrefixSet: true,
Adrian Villin4677d922024-06-14 09:32:39 +0200385 HostIP4Prefix: tap.Ip4AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100386 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100387
Adrian Villin4677d922024-06-14 09:32:39 +0200388 vpp.getSuite().Log("create tap interface " + tap.Name())
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100389 // Create tap interface
Adrian Villin4677d922024-06-14 09:32:39 +0200390 if err := vpp.ApiStream.SendMsg(createTapReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400391 return err
392 }
Adrian Villin4677d922024-06-14 09:32:39 +0200393 replymsg, err := vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400394 if err != nil {
395 return err
396 }
397 reply := replymsg.(*tapv2.TapCreateV3Reply)
398 if err = api.RetvalToVPPApiError(reply.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100399 return err
400 }
Adrian Villin4677d922024-06-14 09:32:39 +0200401 tap.Peer.Index = reply.SwIfIndex
Matus Fabian82ad9662024-06-04 19:00:00 +0200402
403 // Get name and mac
Adrian Villin4677d922024-06-14 09:32:39 +0200404 if err := vpp.ApiStream.SendMsg(&interfaces.SwInterfaceDump{
Matus Fabian82ad9662024-06-04 19:00:00 +0200405 SwIfIndex: reply.SwIfIndex,
406 }); err != nil {
407 return err
408 }
Adrian Villin4677d922024-06-14 09:32:39 +0200409 replymsg, err = vpp.ApiStream.RecvMsg()
Matus Fabian82ad9662024-06-04 19:00:00 +0200410 if err != nil {
411 return err
412 }
413 ifDetails := replymsg.(*interfaces.SwInterfaceDetails)
Adrian Villin4677d922024-06-14 09:32:39 +0200414 tap.Peer.name = ifDetails.InterfaceName
415 tap.Peer.HwAddress = ifDetails.L2Address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100416
417 // Add address
418 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
419 IsAdd: true,
Adrian Villin46d66002024-05-15 04:33:41 -0400420 SwIfIndex: reply.SwIfIndex,
Adrian Villin4677d922024-06-14 09:32:39 +0200421 Prefix: tap.Peer.AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100422 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100423
Adrian Villin4677d922024-06-14 09:32:39 +0200424 vpp.getSuite().Log("tap interface " + tap.Name() + " add address " + tap.Peer.Ip4Address)
425 if err := vpp.ApiStream.SendMsg(addAddressReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400426 return err
427 }
Adrian Villin4677d922024-06-14 09:32:39 +0200428 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400429 if err != nil {
430 return err
431 }
432 reply2 := replymsg.(*interfaces.SwInterfaceAddDelAddressReply)
433 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100434 return err
435 }
436
437 // Set interface to up
438 upReq := &interfaces.SwInterfaceSetFlags{
Adrian Villin46d66002024-05-15 04:33:41 -0400439 SwIfIndex: reply.SwIfIndex,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100440 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
441 }
Adrian Villin4677d922024-06-14 09:32:39 +0200442
443 vpp.getSuite().Log("set tap interface " + tap.Name() + " up")
444 if err := vpp.ApiStream.SendMsg(upReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400445 return err
446 }
Adrian Villin4677d922024-06-14 09:32:39 +0200447 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400448 if err != nil {
449 return err
450 }
451 reply3 := replymsg.(*interfaces.SwInterfaceSetFlagsReply)
452 if err = api.RetvalToVPPApiError(reply3.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100453 return err
454 }
455
Matus Fabian82ad9662024-06-04 19:00:00 +0200456 // Get host mac
457 netIntf, err := net.InterfaceByName(tap.Name())
458 if err == nil {
Adrian Villin4677d922024-06-14 09:32:39 +0200459 tap.HwAddress, _ = ethernet_types.ParseMacAddress(netIntf.HardwareAddr.String())
Matus Fabian82ad9662024-06-04 19:00:00 +0200460 }
461
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100462 return nil
463}
464
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100465func (vpp *VppInstance) saveLogs() {
Adrian Villin4677d922024-06-14 09:32:39 +0200466 logTarget := vpp.Container.getLogDirPath() + "vppinstance-" + vpp.Container.Name + ".log"
467 logSource := vpp.Container.GetHostWorkDir() + defaultLogFilePath
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100468 cmd := exec.Command("cp", logSource, logTarget)
Adrian Villin4677d922024-06-14 09:32:39 +0200469 vpp.getSuite().Log(cmd.String())
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100470 cmd.Run()
471}
472
Adrian Villin4677d922024-06-14 09:32:39 +0200473func (vpp *VppInstance) Disconnect() {
474 vpp.Connection.Disconnect()
475 vpp.ApiStream.Close()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100476}
Filip Tehlar608d0062023-04-28 10:29:47 +0200477
478func (vpp *VppInstance) generateCpuConfig() string {
479 var c Stanza
480 var s string
Adrian Villin4677d922024-06-14 09:32:39 +0200481 if len(vpp.Cpus) < 1 {
Filip Tehlar608d0062023-04-28 10:29:47 +0200482 return ""
483 }
Adrian Villin4677d922024-06-14 09:32:39 +0200484 c.NewStanza("cpu").
485 Append(fmt.Sprintf("main-core %d", vpp.Cpus[0]))
486 vpp.getSuite().Log(fmt.Sprintf("main-core %d", vpp.Cpus[0]))
487 workers := vpp.Cpus[1:]
Filip Tehlar608d0062023-04-28 10:29:47 +0200488
489 if len(workers) > 0 {
490 for i := 0; i < len(workers); i++ {
491 if i != 0 {
492 s = s + ", "
493 }
494 s = s + fmt.Sprintf("%d", workers[i])
495 }
Adrian Villin4677d922024-06-14 09:32:39 +0200496 c.Append(fmt.Sprintf("corelist-workers %s", s))
497 vpp.getSuite().Log("corelist-workers " + s)
Filip Tehlar608d0062023-04-28 10:29:47 +0200498 }
Adrian Villin4677d922024-06-14 09:32:39 +0200499 return c.Close().ToString()
Filip Tehlar608d0062023-04-28 10:29:47 +0200500}