blob: 11f68a61eac5671029cf9196809a596f2efd26a4 [file] [log] [blame]
Maros Ondrejicka11a03e92022-12-01 09:56:37 +01001package main
2
3import (
Adrian Villin46d66002024-05-15 04:33:41 -04004 "context"
Filip Tehlarf3ee2b62023-01-09 12:07:09 +01005 "fmt"
Adrian Villin637edda2024-05-06 06:55:34 -04006 "io"
Filip Tehlarec5c40b2023-02-28 18:59:15 +01007 "os"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +01008 "os/exec"
Filip Tehlarec5c40b2023-02-28 18:59:15 +01009 "os/signal"
Filip Tehlar543cd572023-06-27 10:01:37 +020010 "strconv"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010011 "strings"
Filip Tehlarec5c40b2023-02-28 18:59:15 +010012 "syscall"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010013 "time"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010014
Filip Tehlar9abba112023-03-07 10:13:19 +010015 "github.com/edwarnicke/exechelper"
Adrian Villincee15aa2024-03-14 11:42:55 -040016 . "github.com/onsi/ginkgo/v2"
Adrian Villin46d66002024-05-15 04:33:41 -040017 "github.com/sirupsen/logrus"
Filip Tehlar9abba112023-03-07 10:13:19 +010018
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010019 "go.fd.io/govpp"
20 "go.fd.io/govpp/api"
21 "go.fd.io/govpp/binapi/af_packet"
22 interfaces "go.fd.io/govpp/binapi/interface"
23 "go.fd.io/govpp/binapi/interface_types"
24 "go.fd.io/govpp/binapi/session"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010025 "go.fd.io/govpp/binapi/tapv2"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010026 "go.fd.io/govpp/core"
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010027)
28
29const vppConfigTemplate = `unix {
30 nodaemon
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010031 log %[1]s%[4]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010032 full-coredump
33 cli-listen %[1]s%[2]s
34 runtime-dir %[1]s/var/run
35 gid vpp
36}
37
38api-trace {
39 on
40}
41
42api-segment {
43 gid vpp
44}
45
46socksvr {
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +010047 socket-name %[1]s%[3]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010048}
49
50statseg {
51 socket-name %[1]s/var/run/vpp/stats.sock
52}
53
54plugins {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010055 plugin default { disable }
56
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010057 plugin unittest_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010058 plugin quic_plugin.so { enable }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010059 plugin af_packet_plugin.so { enable }
60 plugin hs_apps_plugin.so { enable }
61 plugin http_plugin.so { enable }
Filip Tehlarcc1475c2023-11-29 12:59:05 +010062 plugin http_static_plugin.so { enable }
63 plugin prom_plugin.so { enable }
Filip Tehlar3336eef2023-11-29 07:40:18 +010064 plugin tlsopenssl_plugin.so { enable }
Adrian Villincee15aa2024-03-14 11:42:55 -040065 plugin ping_plugin.so { enable }
Matus Fabianc899ab42024-04-22 13:42:00 +020066 plugin nsim_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 {
Maros Ondrejicka300f70d2023-02-21 10:53:20 +010083 container *Container
Filip Tehlar608d0062023-04-28 10:29:47 +020084 additionalConfig []Stanza
Maros Ondrejicka300f70d2023-02-21 10:53:20 +010085 connection *core.Connection
Adrian Villin46d66002024-05-15 04:33:41 -040086 apiStream api.Stream
Filip Tehlar608d0062023-04-28 10:29:47 +020087 cpus []int
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010088}
89
Maros Ondrejickae7625d02023-02-28 16:55:01 +010090func (vpp *VppInstance) getSuite() *HstSuite {
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010091 return vpp.container.suite
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010092}
93
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010094func (vpp *VppInstance) getCliSocket() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010095 return fmt.Sprintf("%s%s", vpp.container.getContainerWorkDir(), defaultCliSocketFilePath)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010096}
97
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010098func (vpp *VppInstance) getRunDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010099 return vpp.container.getContainerWorkDir() + "/var/run/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100100}
101
102func (vpp *VppInstance) getLogDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100103 return vpp.container.getContainerWorkDir() + "/var/log/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100104}
105
106func (vpp *VppInstance) getEtcDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100107 return vpp.container.getContainerWorkDir() + "/etc/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100108}
109
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100110func (vpp *VppInstance) start() error {
Adrian Villin637edda2024-05-06 06:55:34 -0400111 // Replace default logger in govpp with our own
112 govppLogger := logrus.New()
113 govppLogger.SetOutput(io.MultiWriter(vpp.getSuite().logger.Writer(), GinkgoWriter))
114 core.SetLogger(govppLogger)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100115 // Create folders
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100116 containerWorkDir := vpp.container.getContainerWorkDir()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100117
118 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir())
119 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir())
120 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
121
122 // Create startup.conf inside the container
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100123 configContent := fmt.Sprintf(
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100124 vppConfigTemplate,
125 containerWorkDir,
126 defaultCliSocketFilePath,
127 defaultApiSocketFilePath,
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100128 defaultLogFilePath,
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100129 )
Filip Tehlar608d0062023-04-28 10:29:47 +0200130 configContent += vpp.generateCpuConfig()
131 for _, c := range vpp.additionalConfig {
132 configContent += c.toString()
133 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100134 startupFileName := vpp.getEtcDir() + "/startup.conf"
135 vpp.container.createFile(startupFileName, configContent)
136
Filip Tehlar1a661502023-03-08 11:55:50 +0100137 // create wrapper script for vppctl with proper CLI socket path
138 cliContent := "#!/usr/bin/bash\nvppctl -s " + vpp.getRunDir() + "/cli.sock"
139 vppcliFileName := "/usr/bin/vppcli"
140 vpp.container.createFile(vppcliFileName, cliContent)
141 vpp.container.exec("chmod 0755 " + vppcliFileName)
142
Adrian Villincee15aa2024-03-14 11:42:55 -0400143 vpp.getSuite().log("starting vpp")
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100144 if *isVppDebug {
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100145 sig := make(chan os.Signal, 1)
Adrian Villincee15aa2024-03-14 11:42:55 -0400146 signal.Notify(sig, syscall.SIGQUIT)
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100147 cont := make(chan bool, 1)
148 go func() {
Filip Tehlar9abba112023-03-07 10:13:19 +0100149 <-sig
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100150 cont <- true
151 }()
152
Filip Tehlara6b1a7d2023-09-02 08:39:25 +0200153 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100154 fmt.Println("run following command in different terminal:")
Filip Tehlara6b1a7d2023-09-02 08:39:25 +0200155 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 -0400156 fmt.Println("Afterwards press CTRL+\\ to continue")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100157 <-cont
158 fmt.Println("continuing...")
159 } else {
160 // Start VPP
161 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
162 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100163
Adrian Villincee15aa2024-03-14 11:42:55 -0400164 vpp.getSuite().log("connecting to vpp")
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100165 // Connect to VPP and store the connection
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100166 sockAddress := vpp.container.getHostWorkDir() + defaultApiSocketFilePath
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100167 conn, connEv, err := govpp.AsyncConnect(
168 sockAddress,
169 core.DefaultMaxReconnectAttempts,
170 core.DefaultReconnectInterval)
171 if err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400172 vpp.getSuite().log("async connect error: " + fmt.Sprint(err))
Filip Tehlar56e17cf2024-01-11 17:17:33 +0100173 return err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100174 }
175 vpp.connection = conn
176
177 // ... wait for Connected event
178 e := <-connEv
179 if e.State != core.Connected {
Adrian Villin46d66002024-05-15 04:33:41 -0400180 vpp.getSuite().log("connecting to VPP failed: " + fmt.Sprint(e.Error))
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100181 }
182
Adrian Villin46d66002024-05-15 04:33:41 -0400183 ch, err := conn.NewStream(
184 context.Background(),
185 core.WithRequestSize(50),
186 core.WithReplySize(50),
187 core.WithReplyTimeout(time.Second*10))
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100188 if err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400189 vpp.getSuite().log("creating stream failed: " + fmt.Sprint(err))
Filip Tehlar56e17cf2024-01-11 17:17:33 +0100190 return err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100191 }
Adrian Villin46d66002024-05-15 04:33:41 -0400192 vpp.apiStream = ch
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100193
194 return nil
195}
196
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100197func (vpp *VppInstance) vppctl(command string, arguments ...any) string {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100198 vppCliCommand := fmt.Sprintf(command, arguments...)
199 containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
200 vpp.container.name, vpp.getCliSocket(), vppCliCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100201 vpp.getSuite().log(containerExecCommand)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100202 output, err := exechelper.CombinedOutput(containerExecCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100203 vpp.getSuite().assertNil(err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100204
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100205 return string(output)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100206}
207
Filip Tehlar543cd572023-06-27 10:01:37 +0200208func (vpp *VppInstance) GetSessionStat(stat string) int {
209 o := vpp.vppctl("show session stats")
210 vpp.getSuite().log(o)
211 for _, line := range strings.Split(o, "\n") {
212 if strings.Contains(line, stat) {
213 tokens := strings.Split(strings.TrimSpace(line), " ")
214 val, err := strconv.Atoi(tokens[0])
215 if err != nil {
Adrian Villincee15aa2024-03-14 11:42:55 -0400216 Fail("failed to parse stat value %s" + fmt.Sprint(err))
Filip Tehlar543cd572023-06-27 10:01:37 +0200217 return 0
218 }
219 return val
220 }
221 }
222 return 0
223}
224
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100225func (vpp *VppInstance) waitForApp(appName string, timeout int) {
Adrian Villincee15aa2024-03-14 11:42:55 -0400226 vpp.getSuite().log("waiting for app " + appName)
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100227 for i := 0; i < timeout; i++ {
228 o := vpp.vppctl("show app")
229 if strings.Contains(o, appName) {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100230 return
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100231 }
232 time.Sleep(1 * time.Second)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100233 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100234 vpp.getSuite().assertNil(1, "Timeout while waiting for app '%s'", appName)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100235}
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100236
237func (vpp *VppInstance) createAfPacket(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100238 veth *NetInterface,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100239) (interface_types.InterfaceIndex, error) {
Adrian Villin46d66002024-05-15 04:33:41 -0400240 createReq := &af_packet.AfPacketCreateV3{
241 Mode: 1,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100242 UseRandomHwAddr: true,
243 HostIfName: veth.Name(),
Adrian Villin46d66002024-05-15 04:33:41 -0400244 Flags: af_packet.AfPacketFlags(11),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100245 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100246 if veth.hwAddress != (MacAddress{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100247 createReq.UseRandomHwAddr = false
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100248 createReq.HwAddr = veth.hwAddress
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100249 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100250
Adrian Villincee15aa2024-03-14 11:42:55 -0400251 vpp.getSuite().log("create af-packet interface " + veth.Name())
Adrian Villin46d66002024-05-15 04:33:41 -0400252 if err := vpp.apiStream.SendMsg(createReq); err != nil {
253 vpp.getSuite().hstFail()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100254 return 0, err
255 }
Adrian Villin46d66002024-05-15 04:33:41 -0400256 replymsg, err := vpp.apiStream.RecvMsg()
257 if err != nil {
258 return 0, err
259 }
260 reply := replymsg.(*af_packet.AfPacketCreateV3Reply)
261 err = api.RetvalToVPPApiError(reply.Retval)
262 if err != nil {
263 return 0, err
264 }
265
266 veth.index = reply.SwIfIndex
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100267
268 // Set to up
269 upReq := &interfaces.SwInterfaceSetFlags{
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100270 SwIfIndex: veth.index,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100271 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
272 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100273
Adrian Villincee15aa2024-03-14 11:42:55 -0400274 vpp.getSuite().log("set af-packet interface " + veth.Name() + " up")
Adrian Villin46d66002024-05-15 04:33:41 -0400275 if err := vpp.apiStream.SendMsg(upReq); err != nil {
276 return 0, err
277 }
278 replymsg, err = vpp.apiStream.RecvMsg()
279 if err != nil {
280 return 0, err
281 }
282 reply2 := replymsg.(*interfaces.SwInterfaceSetFlagsReply)
283 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100284 return 0, err
285 }
286
287 // Add address
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100288 if veth.addressWithPrefix() == (AddressWithPrefix{}) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100289 var err error
290 var ip4Address string
Filip Tehlar3a910ab2023-06-08 17:39:39 +0200291 if ip4Address, err = veth.ip4AddrAllocator.NewIp4InterfaceAddress(veth.peer.networkNumber); err == nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100292 veth.ip4Address = ip4Address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100293 } else {
294 return 0, err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100295 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100296 }
297 addressReq := &interfaces.SwInterfaceAddDelAddress{
298 IsAdd: true,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100299 SwIfIndex: veth.index,
300 Prefix: veth.addressWithPrefix(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100301 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100302
Adrian Villincee15aa2024-03-14 11:42:55 -0400303 vpp.getSuite().log("af-packet interface " + veth.Name() + " add address " + veth.ip4Address)
Adrian Villin46d66002024-05-15 04:33:41 -0400304 if err := vpp.apiStream.SendMsg(addressReq); err != nil {
305 return 0, err
306 }
307 replymsg, err = vpp.apiStream.RecvMsg()
308 if err != nil {
309 return 0, err
310 }
311 reply3 := replymsg.(*interfaces.SwInterfaceAddDelAddressReply)
312 err = api.RetvalToVPPApiError(reply3.Retval)
313 if err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100314 return 0, err
315 }
316
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100317 return veth.index, nil
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100318}
319
320func (vpp *VppInstance) addAppNamespace(
321 secret uint64,
322 ifx interface_types.InterfaceIndex,
323 namespaceId string,
324) error {
Adrian Villin46d66002024-05-15 04:33:41 -0400325 req := &session.AppNamespaceAddDelV4{
326 IsAdd: true,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100327 Secret: secret,
328 SwIfIndex: ifx,
329 NamespaceID: namespaceId,
Adrian Villin46d66002024-05-15 04:33:41 -0400330 SockName: defaultApiSocketFilePath,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100331 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100332
Adrian Villincee15aa2024-03-14 11:42:55 -0400333 vpp.getSuite().log("add app namespace " + namespaceId)
Adrian Villin46d66002024-05-15 04:33:41 -0400334 if err := vpp.apiStream.SendMsg(req); err != nil {
335 return err
336 }
337 replymsg, err := vpp.apiStream.RecvMsg()
338 if err != nil {
339 return err
340 }
341 reply := replymsg.(*session.AppNamespaceAddDelV4Reply)
342 if err = api.RetvalToVPPApiError(reply.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100343 return err
344 }
345
346 sessionReq := &session.SessionEnableDisable{
347 IsEnable: true,
348 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100349
Adrian Villincee15aa2024-03-14 11:42:55 -0400350 vpp.getSuite().log("enable app namespace " + namespaceId)
Adrian Villin46d66002024-05-15 04:33:41 -0400351 if err := vpp.apiStream.SendMsg(sessionReq); err != nil {
352 return err
353 }
354 replymsg, err = vpp.apiStream.RecvMsg()
355 if err != nil {
356 return err
357 }
358 reply2 := replymsg.(*session.SessionEnableDisableReply)
359 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100360 return err
361 }
362
363 return nil
364}
365
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100366func (vpp *VppInstance) createTap(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100367 tap *NetInterface,
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100368 tapId ...uint32,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100369) error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100370 var id uint32 = 1
371 if len(tapId) > 0 {
372 id = tapId[0]
373 }
Adrian Villin46d66002024-05-15 04:33:41 -0400374 createTapReq := &tapv2.TapCreateV3{
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100375 ID: id,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100376 HostIfNameSet: true,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100377 HostIfName: tap.Name(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100378 HostIP4PrefixSet: true,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100379 HostIP4Prefix: tap.ip4AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100380 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100381
Adrian Villincee15aa2024-03-14 11:42:55 -0400382 vpp.getSuite().log("create tap interface " + tap.Name())
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100383 // Create tap interface
Adrian Villin46d66002024-05-15 04:33:41 -0400384 if err := vpp.apiStream.SendMsg(createTapReq); err != nil {
385 return err
386 }
387 replymsg, err := vpp.apiStream.RecvMsg()
388 if err != nil {
389 return err
390 }
391 reply := replymsg.(*tapv2.TapCreateV3Reply)
392 if err = api.RetvalToVPPApiError(reply.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100393 return err
394 }
395
396 // Add address
397 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
398 IsAdd: true,
Adrian Villin46d66002024-05-15 04:33:41 -0400399 SwIfIndex: reply.SwIfIndex,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100400 Prefix: tap.peer.addressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100401 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100402
Adrian Villincee15aa2024-03-14 11:42:55 -0400403 vpp.getSuite().log("tap interface " + tap.Name() + " add address " + tap.peer.ip4Address)
Adrian Villin46d66002024-05-15 04:33:41 -0400404 if err := vpp.apiStream.SendMsg(addAddressReq); err != nil {
405 return err
406 }
407 replymsg, err = vpp.apiStream.RecvMsg()
408 if err != nil {
409 return err
410 }
411 reply2 := replymsg.(*interfaces.SwInterfaceAddDelAddressReply)
412 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100413 return err
414 }
415
416 // Set interface to up
417 upReq := &interfaces.SwInterfaceSetFlags{
Adrian Villin46d66002024-05-15 04:33:41 -0400418 SwIfIndex: reply.SwIfIndex,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100419 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
420 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100421
Adrian Villincee15aa2024-03-14 11:42:55 -0400422 vpp.getSuite().log("set tap interface " + tap.Name() + " up")
Adrian Villin46d66002024-05-15 04:33:41 -0400423 if err := vpp.apiStream.SendMsg(upReq); err != nil {
424 return err
425 }
426 replymsg, err = vpp.apiStream.RecvMsg()
427 if err != nil {
428 return err
429 }
430 reply3 := replymsg.(*interfaces.SwInterfaceSetFlagsReply)
431 if err = api.RetvalToVPPApiError(reply3.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100432 return err
433 }
434
435 return nil
436}
437
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100438func (vpp *VppInstance) saveLogs() {
439 logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100440 logSource := vpp.container.getHostWorkDir() + defaultLogFilePath
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100441 cmd := exec.Command("cp", logSource, logTarget)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100442 vpp.getSuite().log(cmd.String())
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100443 cmd.Run()
444}
445
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100446func (vpp *VppInstance) disconnect() {
447 vpp.connection.Disconnect()
Adrian Villin46d66002024-05-15 04:33:41 -0400448 vpp.apiStream.Close()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100449}
Filip Tehlar608d0062023-04-28 10:29:47 +0200450
451func (vpp *VppInstance) generateCpuConfig() string {
452 var c Stanza
453 var s string
454 if len(vpp.cpus) < 1 {
455 return ""
456 }
457 c.newStanza("cpu").
458 append(fmt.Sprintf("main-core %d", vpp.cpus[0]))
459 workers := vpp.cpus[1:]
460
461 if len(workers) > 0 {
462 for i := 0; i < len(workers); i++ {
463 if i != 0 {
464 s = s + ", "
465 }
466 s = s + fmt.Sprintf("%d", workers[i])
467 }
468 c.append(fmt.Sprintf("corelist-workers %s", s))
469 }
470 return c.close().toString()
471}