blob: d4f570046c763679179074e76fd18cd47a1fbf26 [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
Hadi Rayan Al-Sandide0e85132024-06-24 10:28:58 +020091 CpuConfig VppCpuConfig
92}
93
94type VppCpuConfig struct {
95 PinMainCpu bool
96 PinWorkersCorelist bool
97 SkipCores int
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010098}
99
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100100func (vpp *VppInstance) getSuite() *HstSuite {
Adrian Villin4677d922024-06-14 09:32:39 +0200101 return vpp.Container.Suite
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100102}
103
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100104func (vpp *VppInstance) getCliSocket() string {
Adrian Villin4677d922024-06-14 09:32:39 +0200105 return fmt.Sprintf("%s%s", vpp.Container.GetContainerWorkDir(), defaultCliSocketFilePath)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100106}
107
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100108func (vpp *VppInstance) getRunDir() string {
Adrian Villin4677d922024-06-14 09:32:39 +0200109 return vpp.Container.GetContainerWorkDir() + "/var/run/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100110}
111
112func (vpp *VppInstance) getLogDir() string {
Adrian Villin4677d922024-06-14 09:32:39 +0200113 return vpp.Container.GetContainerWorkDir() + "/var/log/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100114}
115
116func (vpp *VppInstance) getEtcDir() string {
Adrian Villin4677d922024-06-14 09:32:39 +0200117 return vpp.Container.GetContainerWorkDir() + "/etc/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100118}
119
Adrian Villin4677d922024-06-14 09:32:39 +0200120func (vpp *VppInstance) Start() error {
Adrian Villin93974e22024-05-30 06:10:59 -0400121 maxReconnectAttempts := 3
Adrian Villin637edda2024-05-06 06:55:34 -0400122 // Replace default logger in govpp with our own
123 govppLogger := logrus.New()
Adrian Villin4677d922024-06-14 09:32:39 +0200124 govppLogger.SetOutput(io.MultiWriter(vpp.getSuite().Logger.Writer(), GinkgoWriter))
Adrian Villin637edda2024-05-06 06:55:34 -0400125 core.SetLogger(govppLogger)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100126 // Create folders
Adrian Villin4677d922024-06-14 09:32:39 +0200127 containerWorkDir := vpp.Container.GetContainerWorkDir()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100128
Adrian Villin4677d922024-06-14 09:32:39 +0200129 vpp.Container.Exec("mkdir --mode=0700 -p " + vpp.getRunDir())
130 vpp.Container.Exec("mkdir --mode=0700 -p " + vpp.getLogDir())
131 vpp.Container.Exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100132
133 // Create startup.conf inside the container
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100134 configContent := fmt.Sprintf(
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100135 vppConfigTemplate,
136 containerWorkDir,
137 defaultCliSocketFilePath,
138 defaultApiSocketFilePath,
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100139 defaultLogFilePath,
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100140 )
Hadi Rayan Al-Sandide0e85132024-06-24 10:28:58 +0200141 configContent += vpp.generateVPPCpuConfig()
Adrian Villin4677d922024-06-14 09:32:39 +0200142 for _, c := range vpp.AdditionalConfig {
143 configContent += c.ToString()
Filip Tehlar608d0062023-04-28 10:29:47 +0200144 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100145 startupFileName := vpp.getEtcDir() + "/startup.conf"
Adrian Villin4677d922024-06-14 09:32:39 +0200146 vpp.Container.CreateFile(startupFileName, configContent)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100147
Filip Tehlar1a661502023-03-08 11:55:50 +0100148 // create wrapper script for vppctl with proper CLI socket path
149 cliContent := "#!/usr/bin/bash\nvppctl -s " + vpp.getRunDir() + "/cli.sock"
150 vppcliFileName := "/usr/bin/vppcli"
Adrian Villin4677d922024-06-14 09:32:39 +0200151 vpp.Container.CreateFile(vppcliFileName, cliContent)
152 vpp.Container.Exec("chmod 0755 " + vppcliFileName)
Filip Tehlar1a661502023-03-08 11:55:50 +0100153
Adrian Villin4677d922024-06-14 09:32:39 +0200154 vpp.getSuite().Log("starting vpp")
155 if *IsVppDebug {
Adrian Villin93974e22024-05-30 06:10:59 -0400156 // default = 3; VPP will timeout while debugging if there are not enough attempts
157 maxReconnectAttempts = 5000
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100158 sig := make(chan os.Signal, 1)
Adrian Villincee15aa2024-03-14 11:42:55 -0400159 signal.Notify(sig, syscall.SIGQUIT)
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100160 cont := make(chan bool, 1)
161 go func() {
Filip Tehlar9abba112023-03-07 10:13:19 +0100162 <-sig
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100163 cont <- true
164 }()
165
Adrian Villin4677d922024-06-14 09:32:39 +0200166 vpp.Container.ExecServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100167 fmt.Println("run following command in different terminal:")
Adrian Villin4677d922024-06-14 09:32:39 +0200168 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 -0400169 fmt.Println("Afterwards press CTRL+\\ to continue")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100170 <-cont
171 fmt.Println("continuing...")
172 } else {
173 // Start VPP
Adrian Villin4677d922024-06-14 09:32:39 +0200174 vpp.Container.ExecServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100175 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100176
Adrian Villin4677d922024-06-14 09:32:39 +0200177 vpp.getSuite().Log("connecting to vpp")
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100178 // Connect to VPP and store the connection
Adrian Villin4677d922024-06-14 09:32:39 +0200179 sockAddress := vpp.Container.GetHostWorkDir() + defaultApiSocketFilePath
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100180 conn, connEv, err := govpp.AsyncConnect(
181 sockAddress,
Adrian Villin93974e22024-05-30 06:10:59 -0400182 maxReconnectAttempts,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100183 core.DefaultReconnectInterval)
184 if err != nil {
Adrian Villin4677d922024-06-14 09:32:39 +0200185 vpp.getSuite().Log("async connect error: " + fmt.Sprint(err))
Filip Tehlar56e17cf2024-01-11 17:17:33 +0100186 return err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100187 }
Adrian Villin4677d922024-06-14 09:32:39 +0200188 vpp.Connection = conn
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100189
190 // ... wait for Connected event
191 e := <-connEv
192 if e.State != core.Connected {
Adrian Villin4677d922024-06-14 09:32:39 +0200193 vpp.getSuite().Log("connecting to VPP failed: " + fmt.Sprint(e.Error))
Hadi Rayan Al-Sandid0eccf452024-06-24 10:48:54 +0200194 return e.Error
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100195 }
196
Adrian Villin46d66002024-05-15 04:33:41 -0400197 ch, err := conn.NewStream(
198 context.Background(),
199 core.WithRequestSize(50),
200 core.WithReplySize(50),
Adrian Villin93974e22024-05-30 06:10:59 -0400201 core.WithReplyTimeout(time.Second*5))
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100202 if err != nil {
Adrian Villin4677d922024-06-14 09:32:39 +0200203 vpp.getSuite().Log("creating stream failed: " + fmt.Sprint(err))
Filip Tehlar56e17cf2024-01-11 17:17:33 +0100204 return err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100205 }
Adrian Villin4677d922024-06-14 09:32:39 +0200206 vpp.ApiStream = ch
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100207
208 return nil
209}
210
Adrian Villin4677d922024-06-14 09:32:39 +0200211func (vpp *VppInstance) Vppctl(command string, arguments ...any) string {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100212 vppCliCommand := fmt.Sprintf(command, arguments...)
213 containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
Adrian Villin4677d922024-06-14 09:32:39 +0200214 vpp.Container.Name, vpp.getCliSocket(), vppCliCommand)
215 vpp.getSuite().Log(containerExecCommand)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100216 output, err := exechelper.CombinedOutput(containerExecCommand)
Adrian Villin4677d922024-06-14 09:32:39 +0200217 vpp.getSuite().AssertNil(err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100218
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100219 return string(output)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100220}
221
Filip Tehlar543cd572023-06-27 10:01:37 +0200222func (vpp *VppInstance) GetSessionStat(stat string) int {
Adrian Villin4677d922024-06-14 09:32:39 +0200223 o := vpp.Vppctl("show session stats")
224 vpp.getSuite().Log(o)
Filip Tehlar543cd572023-06-27 10:01:37 +0200225 for _, line := range strings.Split(o, "\n") {
226 if strings.Contains(line, stat) {
227 tokens := strings.Split(strings.TrimSpace(line), " ")
228 val, err := strconv.Atoi(tokens[0])
229 if err != nil {
Adrian Villincee15aa2024-03-14 11:42:55 -0400230 Fail("failed to parse stat value %s" + fmt.Sprint(err))
Filip Tehlar543cd572023-06-27 10:01:37 +0200231 return 0
232 }
233 return val
234 }
235 }
236 return 0
237}
238
Adrian Villin4677d922024-06-14 09:32:39 +0200239func (vpp *VppInstance) WaitForApp(appName string, timeout int) {
240 vpp.getSuite().Log("waiting for app " + appName)
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100241 for i := 0; i < timeout; i++ {
Adrian Villin4677d922024-06-14 09:32:39 +0200242 o := vpp.Vppctl("show app")
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100243 if strings.Contains(o, appName) {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100244 return
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100245 }
246 time.Sleep(1 * time.Second)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100247 }
Adrian Villin4677d922024-06-14 09:32:39 +0200248 vpp.getSuite().AssertNil(1, "Timeout while waiting for app '%s'", appName)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100249}
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100250
251func (vpp *VppInstance) createAfPacket(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100252 veth *NetInterface,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100253) (interface_types.InterfaceIndex, error) {
Adrian Villin46d66002024-05-15 04:33:41 -0400254 createReq := &af_packet.AfPacketCreateV3{
255 Mode: 1,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100256 UseRandomHwAddr: true,
257 HostIfName: veth.Name(),
Adrian Villin46d66002024-05-15 04:33:41 -0400258 Flags: af_packet.AfPacketFlags(11),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100259 }
Adrian Villin4677d922024-06-14 09:32:39 +0200260 if veth.HwAddress != (MacAddress{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100261 createReq.UseRandomHwAddr = false
Adrian Villin4677d922024-06-14 09:32:39 +0200262 createReq.HwAddr = veth.HwAddress
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100263 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100264
Adrian Villin4677d922024-06-14 09:32:39 +0200265 vpp.getSuite().Log("create af-packet interface " + veth.Name())
266 if err := vpp.ApiStream.SendMsg(createReq); err != nil {
267 vpp.getSuite().HstFail()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100268 return 0, err
269 }
Adrian Villin4677d922024-06-14 09:32:39 +0200270 replymsg, err := vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400271 if err != nil {
272 return 0, err
273 }
274 reply := replymsg.(*af_packet.AfPacketCreateV3Reply)
275 err = api.RetvalToVPPApiError(reply.Retval)
276 if err != nil {
277 return 0, err
278 }
279
Adrian Villin4677d922024-06-14 09:32:39 +0200280 veth.Index = reply.SwIfIndex
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100281
282 // Set to up
283 upReq := &interfaces.SwInterfaceSetFlags{
Adrian Villin4677d922024-06-14 09:32:39 +0200284 SwIfIndex: veth.Index,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100285 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
286 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100287
Adrian Villin4677d922024-06-14 09:32:39 +0200288 vpp.getSuite().Log("set af-packet interface " + veth.Name() + " up")
289 if err := vpp.ApiStream.SendMsg(upReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400290 return 0, err
291 }
Adrian Villin4677d922024-06-14 09:32:39 +0200292 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400293 if err != nil {
294 return 0, err
295 }
296 reply2 := replymsg.(*interfaces.SwInterfaceSetFlagsReply)
297 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100298 return 0, err
299 }
300
301 // Add address
Adrian Villin4677d922024-06-14 09:32:39 +0200302 if veth.AddressWithPrefix() == (AddressWithPrefix{}) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100303 var err error
304 var ip4Address string
Adrian Villin4677d922024-06-14 09:32:39 +0200305 if ip4Address, err = veth.Ip4AddrAllocator.NewIp4InterfaceAddress(veth.Peer.NetworkNumber); err == nil {
306 veth.Ip4Address = ip4Address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100307 } else {
308 return 0, err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100309 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100310 }
311 addressReq := &interfaces.SwInterfaceAddDelAddress{
312 IsAdd: true,
Adrian Villin4677d922024-06-14 09:32:39 +0200313 SwIfIndex: veth.Index,
314 Prefix: veth.AddressWithPrefix(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100315 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100316
Adrian Villin4677d922024-06-14 09:32:39 +0200317 vpp.getSuite().Log("af-packet interface " + veth.Name() + " add address " + veth.Ip4Address)
318 if err := vpp.ApiStream.SendMsg(addressReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400319 return 0, err
320 }
Adrian Villin4677d922024-06-14 09:32:39 +0200321 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400322 if err != nil {
323 return 0, err
324 }
325 reply3 := replymsg.(*interfaces.SwInterfaceAddDelAddressReply)
326 err = api.RetvalToVPPApiError(reply3.Retval)
327 if err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100328 return 0, err
329 }
330
Adrian Villin4677d922024-06-14 09:32:39 +0200331 return veth.Index, nil
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100332}
333
334func (vpp *VppInstance) addAppNamespace(
335 secret uint64,
336 ifx interface_types.InterfaceIndex,
337 namespaceId string,
338) error {
Adrian Villin46d66002024-05-15 04:33:41 -0400339 req := &session.AppNamespaceAddDelV4{
340 IsAdd: true,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100341 Secret: secret,
342 SwIfIndex: ifx,
343 NamespaceID: namespaceId,
Adrian Villin46d66002024-05-15 04:33:41 -0400344 SockName: defaultApiSocketFilePath,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100345 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100346
Adrian Villin4677d922024-06-14 09:32:39 +0200347 vpp.getSuite().Log("add app namespace " + namespaceId)
348 if err := vpp.ApiStream.SendMsg(req); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400349 return err
350 }
Adrian Villin4677d922024-06-14 09:32:39 +0200351 replymsg, err := vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400352 if err != nil {
353 return err
354 }
355 reply := replymsg.(*session.AppNamespaceAddDelV4Reply)
356 if err = api.RetvalToVPPApiError(reply.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100357 return err
358 }
359
360 sessionReq := &session.SessionEnableDisable{
361 IsEnable: true,
362 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100363
Adrian Villin4677d922024-06-14 09:32:39 +0200364 vpp.getSuite().Log("enable app namespace " + namespaceId)
365 if err := vpp.ApiStream.SendMsg(sessionReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400366 return err
367 }
Adrian Villin4677d922024-06-14 09:32:39 +0200368 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400369 if err != nil {
370 return err
371 }
372 reply2 := replymsg.(*session.SessionEnableDisableReply)
373 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100374 return err
375 }
376
377 return nil
378}
379
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100380func (vpp *VppInstance) createTap(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100381 tap *NetInterface,
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100382 tapId ...uint32,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100383) error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100384 var id uint32 = 1
385 if len(tapId) > 0 {
386 id = tapId[0]
387 }
Adrian Villin46d66002024-05-15 04:33:41 -0400388 createTapReq := &tapv2.TapCreateV3{
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100389 ID: id,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100390 HostIfNameSet: true,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100391 HostIfName: tap.Name(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100392 HostIP4PrefixSet: true,
Adrian Villin4677d922024-06-14 09:32:39 +0200393 HostIP4Prefix: tap.Ip4AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100394 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100395
Adrian Villin4677d922024-06-14 09:32:39 +0200396 vpp.getSuite().Log("create tap interface " + tap.Name())
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100397 // Create tap interface
Adrian Villin4677d922024-06-14 09:32:39 +0200398 if err := vpp.ApiStream.SendMsg(createTapReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400399 return err
400 }
Adrian Villin4677d922024-06-14 09:32:39 +0200401 replymsg, err := vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400402 if err != nil {
403 return err
404 }
405 reply := replymsg.(*tapv2.TapCreateV3Reply)
406 if err = api.RetvalToVPPApiError(reply.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100407 return err
408 }
Adrian Villin4677d922024-06-14 09:32:39 +0200409 tap.Peer.Index = reply.SwIfIndex
Matus Fabian82ad9662024-06-04 19:00:00 +0200410
411 // Get name and mac
Adrian Villin4677d922024-06-14 09:32:39 +0200412 if err := vpp.ApiStream.SendMsg(&interfaces.SwInterfaceDump{
Matus Fabian82ad9662024-06-04 19:00:00 +0200413 SwIfIndex: reply.SwIfIndex,
414 }); err != nil {
415 return err
416 }
Adrian Villin4677d922024-06-14 09:32:39 +0200417 replymsg, err = vpp.ApiStream.RecvMsg()
Matus Fabian82ad9662024-06-04 19:00:00 +0200418 if err != nil {
419 return err
420 }
421 ifDetails := replymsg.(*interfaces.SwInterfaceDetails)
Adrian Villin4677d922024-06-14 09:32:39 +0200422 tap.Peer.name = ifDetails.InterfaceName
423 tap.Peer.HwAddress = ifDetails.L2Address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100424
425 // Add address
426 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
427 IsAdd: true,
Adrian Villin46d66002024-05-15 04:33:41 -0400428 SwIfIndex: reply.SwIfIndex,
Adrian Villin4677d922024-06-14 09:32:39 +0200429 Prefix: tap.Peer.AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100430 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100431
Adrian Villin4677d922024-06-14 09:32:39 +0200432 vpp.getSuite().Log("tap interface " + tap.Name() + " add address " + tap.Peer.Ip4Address)
433 if err := vpp.ApiStream.SendMsg(addAddressReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400434 return err
435 }
Adrian Villin4677d922024-06-14 09:32:39 +0200436 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400437 if err != nil {
438 return err
439 }
440 reply2 := replymsg.(*interfaces.SwInterfaceAddDelAddressReply)
441 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100442 return err
443 }
444
445 // Set interface to up
446 upReq := &interfaces.SwInterfaceSetFlags{
Adrian Villin46d66002024-05-15 04:33:41 -0400447 SwIfIndex: reply.SwIfIndex,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100448 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
449 }
Adrian Villin4677d922024-06-14 09:32:39 +0200450
451 vpp.getSuite().Log("set tap interface " + tap.Name() + " up")
452 if err := vpp.ApiStream.SendMsg(upReq); err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400453 return err
454 }
Adrian Villin4677d922024-06-14 09:32:39 +0200455 replymsg, err = vpp.ApiStream.RecvMsg()
Adrian Villin46d66002024-05-15 04:33:41 -0400456 if err != nil {
457 return err
458 }
459 reply3 := replymsg.(*interfaces.SwInterfaceSetFlagsReply)
460 if err = api.RetvalToVPPApiError(reply3.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100461 return err
462 }
463
Matus Fabian82ad9662024-06-04 19:00:00 +0200464 // Get host mac
465 netIntf, err := net.InterfaceByName(tap.Name())
466 if err == nil {
Adrian Villin4677d922024-06-14 09:32:39 +0200467 tap.HwAddress, _ = ethernet_types.ParseMacAddress(netIntf.HardwareAddr.String())
Matus Fabian82ad9662024-06-04 19:00:00 +0200468 }
469
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100470 return nil
471}
472
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100473func (vpp *VppInstance) saveLogs() {
Adrian Villin4677d922024-06-14 09:32:39 +0200474 logTarget := vpp.Container.getLogDirPath() + "vppinstance-" + vpp.Container.Name + ".log"
475 logSource := vpp.Container.GetHostWorkDir() + defaultLogFilePath
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100476 cmd := exec.Command("cp", logSource, logTarget)
Adrian Villin4677d922024-06-14 09:32:39 +0200477 vpp.getSuite().Log(cmd.String())
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100478 cmd.Run()
479}
480
Adrian Villin4677d922024-06-14 09:32:39 +0200481func (vpp *VppInstance) Disconnect() {
482 vpp.Connection.Disconnect()
483 vpp.ApiStream.Close()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100484}
Filip Tehlar608d0062023-04-28 10:29:47 +0200485
Hadi Rayan Al-Sandide0e85132024-06-24 10:28:58 +0200486func (vpp *VppInstance) setDefaultCpuConfig() {
487 vpp.CpuConfig.PinMainCpu = true
488 vpp.CpuConfig.PinWorkersCorelist = true
489 vpp.CpuConfig.SkipCores = 0
490}
491
492func (vpp *VppInstance) generateVPPCpuConfig() string {
Filip Tehlar608d0062023-04-28 10:29:47 +0200493 var c Stanza
494 var s string
Hadi Rayan Al-Sandide0e85132024-06-24 10:28:58 +0200495 startCpu := 0
Adrian Villin4677d922024-06-14 09:32:39 +0200496 if len(vpp.Cpus) < 1 {
Filip Tehlar608d0062023-04-28 10:29:47 +0200497 return ""
498 }
Hadi Rayan Al-Sandide0e85132024-06-24 10:28:58 +0200499
500 c.NewStanza("cpu")
501
502 // If skip-cores is valid, use as start value to assign main/workers CPUs
503 if vpp.CpuConfig.SkipCores != 0 {
504 c.Append(fmt.Sprintf("skip-cores %d", vpp.CpuConfig.SkipCores))
505 vpp.getSuite().Log(fmt.Sprintf("skip-cores %d", vpp.CpuConfig.SkipCores))
506 }
507
508 if len(vpp.Cpus) > vpp.CpuConfig.SkipCores {
509 startCpu = vpp.CpuConfig.SkipCores
510 }
511
512 if vpp.CpuConfig.PinMainCpu {
513 c.Append(fmt.Sprintf("main-core %d", vpp.Cpus[startCpu]))
514 vpp.getSuite().Log(fmt.Sprintf("main-core %d", vpp.Cpus[startCpu]))
515 }
516
517 workers := vpp.Cpus[startCpu+1:]
Filip Tehlar608d0062023-04-28 10:29:47 +0200518
519 if len(workers) > 0 {
Hadi Rayan Al-Sandide0e85132024-06-24 10:28:58 +0200520 if vpp.CpuConfig.PinWorkersCorelist {
521 for i := 0; i < len(workers); i++ {
522 if i != 0 {
523 s = s + ", "
524 }
525 s = s + fmt.Sprintf("%d", workers[i])
Filip Tehlar608d0062023-04-28 10:29:47 +0200526 }
Hadi Rayan Al-Sandide0e85132024-06-24 10:28:58 +0200527 c.Append(fmt.Sprintf("corelist-workers %s", s))
528 vpp.getSuite().Log("corelist-workers " + s)
529 } else {
530 s = fmt.Sprintf("%d", len(workers))
531 c.Append(fmt.Sprintf("workers %s", s))
532 vpp.getSuite().Log("workers " + s)
Filip Tehlar608d0062023-04-28 10:29:47 +0200533 }
Filip Tehlar608d0062023-04-28 10:29:47 +0200534 }
Hadi Rayan Al-Sandide0e85132024-06-24 10:28:58 +0200535
Adrian Villin4677d922024-06-14 09:32:39 +0200536 return c.Close().ToString()
Filip Tehlar608d0062023-04-28 10:29:47 +0200537}