blob: 48d2b7839177b9c8c8f16d38fc26ed1e66367116 [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))
Hadi Rayan Al-Sandid0eccf452024-06-24 10:48:54 +0200187 return e.Error
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100188 }
189
Adrian Villin46d66002024-05-15 04:33:41 -0400190 ch, err := conn.NewStream(
191 context.Background(),
192 core.WithRequestSize(50),
193 core.WithReplySize(50),
Adrian Villin93974e22024-05-30 06:10:59 -0400194 core.WithReplyTimeout(time.Second*5))
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100195 if err != nil {
Adrian Villin4677d922024-06-14 09:32:39 +0200196 vpp.getSuite().Log("creating stream failed: " + fmt.Sprint(err))
Filip Tehlar56e17cf2024-01-11 17:17:33 +0100197 return err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100198 }
Adrian Villin4677d922024-06-14 09:32:39 +0200199 vpp.ApiStream = ch
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100200
201 return nil
202}
203
Adrian Villin4677d922024-06-14 09:32:39 +0200204func (vpp *VppInstance) Vppctl(command string, arguments ...any) string {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100205 vppCliCommand := fmt.Sprintf(command, arguments...)
206 containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
Adrian Villin4677d922024-06-14 09:32:39 +0200207 vpp.Container.Name, vpp.getCliSocket(), vppCliCommand)
208 vpp.getSuite().Log(containerExecCommand)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100209 output, err := exechelper.CombinedOutput(containerExecCommand)
Adrian Villin4677d922024-06-14 09:32:39 +0200210 vpp.getSuite().AssertNil(err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100211
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100212 return string(output)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100213}
214
Filip Tehlar543cd572023-06-27 10:01:37 +0200215func (vpp *VppInstance) GetSessionStat(stat string) int {
Adrian Villin4677d922024-06-14 09:32:39 +0200216 o := vpp.Vppctl("show session stats")
217 vpp.getSuite().Log(o)
Filip Tehlar543cd572023-06-27 10:01:37 +0200218 for _, line := range strings.Split(o, "\n") {
219 if strings.Contains(line, stat) {
220 tokens := strings.Split(strings.TrimSpace(line), " ")
221 val, err := strconv.Atoi(tokens[0])
222 if err != nil {
Adrian Villincee15aa2024-03-14 11:42:55 -0400223 Fail("failed to parse stat value %s" + fmt.Sprint(err))
Filip Tehlar543cd572023-06-27 10:01:37 +0200224 return 0
225 }
226 return val
227 }
228 }
229 return 0
230}
231
Adrian Villin4677d922024-06-14 09:32:39 +0200232func (vpp *VppInstance) WaitForApp(appName string, timeout int) {
233 vpp.getSuite().Log("waiting for app " + appName)
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100234 for i := 0; i < timeout; i++ {
Adrian Villin4677d922024-06-14 09:32:39 +0200235 o := vpp.Vppctl("show app")
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100236 if strings.Contains(o, appName) {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100237 return
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100238 }
239 time.Sleep(1 * time.Second)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100240 }
Adrian Villin4677d922024-06-14 09:32:39 +0200241 vpp.getSuite().AssertNil(1, "Timeout while waiting for app '%s'", appName)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100242}
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100243
244func (vpp *VppInstance) createAfPacket(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100245 veth *NetInterface,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100246) (interface_types.InterfaceIndex, error) {
Adrian Villin46d66002024-05-15 04:33:41 -0400247 createReq := &af_packet.AfPacketCreateV3{
248 Mode: 1,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100249 UseRandomHwAddr: true,
250 HostIfName: veth.Name(),
Adrian Villin46d66002024-05-15 04:33:41 -0400251 Flags: af_packet.AfPacketFlags(11),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100252 }
Adrian Villin4677d922024-06-14 09:32:39 +0200253 if veth.HwAddress != (MacAddress{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100254 createReq.UseRandomHwAddr = false
Adrian Villin4677d922024-06-14 09:32:39 +0200255 createReq.HwAddr = veth.HwAddress
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100256 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100257
Adrian Villin4677d922024-06-14 09:32:39 +0200258 vpp.getSuite().Log("create af-packet interface " + veth.Name())
259 if err := vpp.ApiStream.SendMsg(createReq); err != nil {
260 vpp.getSuite().HstFail()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100261 return 0, err
262 }
Adrian Villin4677d922024-06-14 09:32:39 +0200263 replymsg, err := vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400264 if err != nil {
265 return 0, err
266 }
267 reply := replymsg.(*af_packet.AfPacketCreateV3Reply)
268 err = api.RetvalToVPPApiError(reply.Retval)
269 if err != nil {
270 return 0, err
271 }
272
Adrian Villin4677d922024-06-14 09:32:39 +0200273 veth.Index = reply.SwIfIndex
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100274
275 // Set to up
276 upReq := &interfaces.SwInterfaceSetFlags{
Adrian Villin4677d922024-06-14 09:32:39 +0200277 SwIfIndex: veth.Index,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100278 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
279 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100280
Adrian Villin4677d922024-06-14 09:32:39 +0200281 vpp.getSuite().Log("set af-packet interface " + veth.Name() + " up")
282 if err := vpp.ApiStream.SendMsg(upReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400283 return 0, err
284 }
Adrian Villin4677d922024-06-14 09:32:39 +0200285 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400286 if err != nil {
287 return 0, err
288 }
289 reply2 := replymsg.(*interfaces.SwInterfaceSetFlagsReply)
290 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100291 return 0, err
292 }
293
294 // Add address
Adrian Villin4677d922024-06-14 09:32:39 +0200295 if veth.AddressWithPrefix() == (AddressWithPrefix{}) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100296 var err error
297 var ip4Address string
Adrian Villin4677d922024-06-14 09:32:39 +0200298 if ip4Address, err = veth.Ip4AddrAllocator.NewIp4InterfaceAddress(veth.Peer.NetworkNumber); err == nil {
299 veth.Ip4Address = ip4Address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100300 } else {
301 return 0, err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100302 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100303 }
304 addressReq := &interfaces.SwInterfaceAddDelAddress{
305 IsAdd: true,
Adrian Villin4677d922024-06-14 09:32:39 +0200306 SwIfIndex: veth.Index,
307 Prefix: veth.AddressWithPrefix(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100308 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100309
Adrian Villin4677d922024-06-14 09:32:39 +0200310 vpp.getSuite().Log("af-packet interface " + veth.Name() + " add address " + veth.Ip4Address)
311 if err := vpp.ApiStream.SendMsg(addressReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400312 return 0, err
313 }
Adrian Villin4677d922024-06-14 09:32:39 +0200314 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400315 if err != nil {
316 return 0, err
317 }
318 reply3 := replymsg.(*interfaces.SwInterfaceAddDelAddressReply)
319 err = api.RetvalToVPPApiError(reply3.Retval)
320 if err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100321 return 0, err
322 }
323
Adrian Villin4677d922024-06-14 09:32:39 +0200324 return veth.Index, nil
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100325}
326
327func (vpp *VppInstance) addAppNamespace(
328 secret uint64,
329 ifx interface_types.InterfaceIndex,
330 namespaceId string,
331) error {
Adrian Villin46d66002024-05-15 04:33:41 -0400332 req := &session.AppNamespaceAddDelV4{
333 IsAdd: true,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100334 Secret: secret,
335 SwIfIndex: ifx,
336 NamespaceID: namespaceId,
Adrian Villin46d66002024-05-15 04:33:41 -0400337 SockName: defaultApiSocketFilePath,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100338 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100339
Adrian Villin4677d922024-06-14 09:32:39 +0200340 vpp.getSuite().Log("add app namespace " + namespaceId)
341 if err := vpp.ApiStream.SendMsg(req); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400342 return err
343 }
Adrian Villin4677d922024-06-14 09:32:39 +0200344 replymsg, err := vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400345 if err != nil {
346 return err
347 }
348 reply := replymsg.(*session.AppNamespaceAddDelV4Reply)
349 if err = api.RetvalToVPPApiError(reply.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100350 return err
351 }
352
353 sessionReq := &session.SessionEnableDisable{
354 IsEnable: true,
355 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100356
Adrian Villin4677d922024-06-14 09:32:39 +0200357 vpp.getSuite().Log("enable app namespace " + namespaceId)
358 if err := vpp.ApiStream.SendMsg(sessionReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400359 return err
360 }
Adrian Villin4677d922024-06-14 09:32:39 +0200361 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400362 if err != nil {
363 return err
364 }
365 reply2 := replymsg.(*session.SessionEnableDisableReply)
366 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100367 return err
368 }
369
370 return nil
371}
372
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100373func (vpp *VppInstance) createTap(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100374 tap *NetInterface,
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100375 tapId ...uint32,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100376) error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100377 var id uint32 = 1
378 if len(tapId) > 0 {
379 id = tapId[0]
380 }
Adrian Villin46d66002024-05-15 04:33:41 -0400381 createTapReq := &tapv2.TapCreateV3{
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100382 ID: id,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100383 HostIfNameSet: true,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100384 HostIfName: tap.Name(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100385 HostIP4PrefixSet: true,
Adrian Villin4677d922024-06-14 09:32:39 +0200386 HostIP4Prefix: tap.Ip4AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100387 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100388
Adrian Villin4677d922024-06-14 09:32:39 +0200389 vpp.getSuite().Log("create tap interface " + tap.Name())
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100390 // Create tap interface
Adrian Villin4677d922024-06-14 09:32:39 +0200391 if err := vpp.ApiStream.SendMsg(createTapReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400392 return err
393 }
Adrian Villin4677d922024-06-14 09:32:39 +0200394 replymsg, err := vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400395 if err != nil {
396 return err
397 }
398 reply := replymsg.(*tapv2.TapCreateV3Reply)
399 if err = api.RetvalToVPPApiError(reply.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100400 return err
401 }
Adrian Villin4677d922024-06-14 09:32:39 +0200402 tap.Peer.Index = reply.SwIfIndex
Matus Fabian82ad9662024-06-04 19:00:00 +0200403
404 // Get name and mac
Adrian Villin4677d922024-06-14 09:32:39 +0200405 if err := vpp.ApiStream.SendMsg(&interfaces.SwInterfaceDump{
Matus Fabian82ad9662024-06-04 19:00:00 +0200406 SwIfIndex: reply.SwIfIndex,
407 }); err != nil {
408 return err
409 }
Adrian Villin4677d922024-06-14 09:32:39 +0200410 replymsg, err = vpp.ApiStream.RecvMsg()
Matus Fabian82ad9662024-06-04 19:00:00 +0200411 if err != nil {
412 return err
413 }
414 ifDetails := replymsg.(*interfaces.SwInterfaceDetails)
Adrian Villin4677d922024-06-14 09:32:39 +0200415 tap.Peer.name = ifDetails.InterfaceName
416 tap.Peer.HwAddress = ifDetails.L2Address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100417
418 // Add address
419 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
420 IsAdd: true,
Adrian Villin46d66002024-05-15 04:33:41 -0400421 SwIfIndex: reply.SwIfIndex,
Adrian Villin4677d922024-06-14 09:32:39 +0200422 Prefix: tap.Peer.AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100423 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100424
Adrian Villin4677d922024-06-14 09:32:39 +0200425 vpp.getSuite().Log("tap interface " + tap.Name() + " add address " + tap.Peer.Ip4Address)
426 if err := vpp.ApiStream.SendMsg(addAddressReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400427 return err
428 }
Adrian Villin4677d922024-06-14 09:32:39 +0200429 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400430 if err != nil {
431 return err
432 }
433 reply2 := replymsg.(*interfaces.SwInterfaceAddDelAddressReply)
434 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100435 return err
436 }
437
438 // Set interface to up
439 upReq := &interfaces.SwInterfaceSetFlags{
Adrian Villin46d66002024-05-15 04:33:41 -0400440 SwIfIndex: reply.SwIfIndex,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100441 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
442 }
Adrian Villin4677d922024-06-14 09:32:39 +0200443
444 vpp.getSuite().Log("set tap interface " + tap.Name() + " up")
445 if err := vpp.ApiStream.SendMsg(upReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400446 return err
447 }
Adrian Villin4677d922024-06-14 09:32:39 +0200448 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400449 if err != nil {
450 return err
451 }
452 reply3 := replymsg.(*interfaces.SwInterfaceSetFlagsReply)
453 if err = api.RetvalToVPPApiError(reply3.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100454 return err
455 }
456
Matus Fabian82ad9662024-06-04 19:00:00 +0200457 // Get host mac
458 netIntf, err := net.InterfaceByName(tap.Name())
459 if err == nil {
Adrian Villin4677d922024-06-14 09:32:39 +0200460 tap.HwAddress, _ = ethernet_types.ParseMacAddress(netIntf.HardwareAddr.String())
Matus Fabian82ad9662024-06-04 19:00:00 +0200461 }
462
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100463 return nil
464}
465
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100466func (vpp *VppInstance) saveLogs() {
Adrian Villin4677d922024-06-14 09:32:39 +0200467 logTarget := vpp.Container.getLogDirPath() + "vppinstance-" + vpp.Container.Name + ".log"
468 logSource := vpp.Container.GetHostWorkDir() + defaultLogFilePath
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100469 cmd := exec.Command("cp", logSource, logTarget)
Adrian Villin4677d922024-06-14 09:32:39 +0200470 vpp.getSuite().Log(cmd.String())
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100471 cmd.Run()
472}
473
Adrian Villin4677d922024-06-14 09:32:39 +0200474func (vpp *VppInstance) Disconnect() {
475 vpp.Connection.Disconnect()
476 vpp.ApiStream.Close()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100477}
Filip Tehlar608d0062023-04-28 10:29:47 +0200478
479func (vpp *VppInstance) generateCpuConfig() string {
480 var c Stanza
481 var s string
Adrian Villin4677d922024-06-14 09:32:39 +0200482 if len(vpp.Cpus) < 1 {
Filip Tehlar608d0062023-04-28 10:29:47 +0200483 return ""
484 }
Adrian Villin4677d922024-06-14 09:32:39 +0200485 c.NewStanza("cpu").
486 Append(fmt.Sprintf("main-core %d", vpp.Cpus[0]))
487 vpp.getSuite().Log(fmt.Sprintf("main-core %d", vpp.Cpus[0]))
488 workers := vpp.Cpus[1:]
Filip Tehlar608d0062023-04-28 10:29:47 +0200489
490 if len(workers) > 0 {
491 for i := 0; i < len(workers); i++ {
492 if i != 0 {
493 s = s + ", "
494 }
495 s = s + fmt.Sprintf("%d", workers[i])
496 }
Adrian Villin4677d922024-06-14 09:32:39 +0200497 c.Append(fmt.Sprintf("corelist-workers %s", s))
498 vpp.getSuite().Log("corelist-workers " + s)
Filip Tehlar608d0062023-04-28 10:29:47 +0200499 }
Adrian Villin4677d922024-06-14 09:32:39 +0200500 return c.Close().ToString()
Filip Tehlar608d0062023-04-28 10:29:47 +0200501}