blob: 8a92776894c2f15d0ce4d1c7979e93dac12e1f1e [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 Villin93974e22024-05-30 06:10:59 -0400111 maxReconnectAttempts := 3
Adrian Villin637edda2024-05-06 06:55:34 -0400112 // Replace default logger in govpp with our own
113 govppLogger := logrus.New()
114 govppLogger.SetOutput(io.MultiWriter(vpp.getSuite().logger.Writer(), GinkgoWriter))
115 core.SetLogger(govppLogger)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100116 // Create folders
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100117 containerWorkDir := vpp.container.getContainerWorkDir()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100118
119 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir())
120 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir())
121 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
122
123 // Create startup.conf inside the container
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100124 configContent := fmt.Sprintf(
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100125 vppConfigTemplate,
126 containerWorkDir,
127 defaultCliSocketFilePath,
128 defaultApiSocketFilePath,
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100129 defaultLogFilePath,
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100130 )
Filip Tehlar608d0062023-04-28 10:29:47 +0200131 configContent += vpp.generateCpuConfig()
132 for _, c := range vpp.additionalConfig {
133 configContent += c.toString()
134 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100135 startupFileName := vpp.getEtcDir() + "/startup.conf"
136 vpp.container.createFile(startupFileName, configContent)
137
Filip Tehlar1a661502023-03-08 11:55:50 +0100138 // create wrapper script for vppctl with proper CLI socket path
139 cliContent := "#!/usr/bin/bash\nvppctl -s " + vpp.getRunDir() + "/cli.sock"
140 vppcliFileName := "/usr/bin/vppcli"
141 vpp.container.createFile(vppcliFileName, cliContent)
142 vpp.container.exec("chmod 0755 " + vppcliFileName)
143
Adrian Villincee15aa2024-03-14 11:42:55 -0400144 vpp.getSuite().log("starting vpp")
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100145 if *isVppDebug {
Adrian Villin93974e22024-05-30 06:10:59 -0400146 // default = 3; VPP will timeout while debugging if there are not enough attempts
147 maxReconnectAttempts = 5000
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100148 sig := make(chan os.Signal, 1)
Adrian Villincee15aa2024-03-14 11:42:55 -0400149 signal.Notify(sig, syscall.SIGQUIT)
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100150 cont := make(chan bool, 1)
151 go func() {
Filip Tehlar9abba112023-03-07 10:13:19 +0100152 <-sig
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100153 cont <- true
154 }()
155
Filip Tehlara6b1a7d2023-09-02 08:39:25 +0200156 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100157 fmt.Println("run following command in different terminal:")
Filip Tehlara6b1a7d2023-09-02 08:39:25 +0200158 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 -0400159 fmt.Println("Afterwards press CTRL+\\ to continue")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100160 <-cont
161 fmt.Println("continuing...")
162 } else {
163 // Start VPP
164 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
165 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100166
Adrian Villincee15aa2024-03-14 11:42:55 -0400167 vpp.getSuite().log("connecting to vpp")
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100168 // Connect to VPP and store the connection
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100169 sockAddress := vpp.container.getHostWorkDir() + defaultApiSocketFilePath
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100170 conn, connEv, err := govpp.AsyncConnect(
171 sockAddress,
Adrian Villin93974e22024-05-30 06:10:59 -0400172 maxReconnectAttempts,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100173 core.DefaultReconnectInterval)
174 if err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400175 vpp.getSuite().log("async connect error: " + fmt.Sprint(err))
Filip Tehlar56e17cf2024-01-11 17:17:33 +0100176 return err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100177 }
178 vpp.connection = conn
179
180 // ... wait for Connected event
181 e := <-connEv
182 if e.State != core.Connected {
Adrian Villin46d66002024-05-15 04:33:41 -0400183 vpp.getSuite().log("connecting to VPP failed: " + fmt.Sprint(e.Error))
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100184 }
185
Adrian Villin46d66002024-05-15 04:33:41 -0400186 ch, err := conn.NewStream(
187 context.Background(),
188 core.WithRequestSize(50),
189 core.WithReplySize(50),
Adrian Villin93974e22024-05-30 06:10:59 -0400190 core.WithReplyTimeout(time.Second*5))
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100191 if err != nil {
Adrian Villin46d66002024-05-15 04:33:41 -0400192 vpp.getSuite().log("creating stream failed: " + fmt.Sprint(err))
Filip Tehlar56e17cf2024-01-11 17:17:33 +0100193 return err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100194 }
Adrian Villin46d66002024-05-15 04:33:41 -0400195 vpp.apiStream = ch
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100196
197 return nil
198}
199
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100200func (vpp *VppInstance) vppctl(command string, arguments ...any) string {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100201 vppCliCommand := fmt.Sprintf(command, arguments...)
202 containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
203 vpp.container.name, vpp.getCliSocket(), vppCliCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100204 vpp.getSuite().log(containerExecCommand)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100205 output, err := exechelper.CombinedOutput(containerExecCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100206 vpp.getSuite().assertNil(err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100207
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100208 return string(output)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100209}
210
Filip Tehlar543cd572023-06-27 10:01:37 +0200211func (vpp *VppInstance) GetSessionStat(stat string) int {
212 o := vpp.vppctl("show session stats")
213 vpp.getSuite().log(o)
214 for _, line := range strings.Split(o, "\n") {
215 if strings.Contains(line, stat) {
216 tokens := strings.Split(strings.TrimSpace(line), " ")
217 val, err := strconv.Atoi(tokens[0])
218 if err != nil {
Adrian Villincee15aa2024-03-14 11:42:55 -0400219 Fail("failed to parse stat value %s" + fmt.Sprint(err))
Filip Tehlar543cd572023-06-27 10:01:37 +0200220 return 0
221 }
222 return val
223 }
224 }
225 return 0
226}
227
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100228func (vpp *VppInstance) waitForApp(appName string, timeout int) {
Adrian Villincee15aa2024-03-14 11:42:55 -0400229 vpp.getSuite().log("waiting for app " + appName)
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100230 for i := 0; i < timeout; i++ {
231 o := vpp.vppctl("show app")
232 if strings.Contains(o, appName) {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100233 return
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100234 }
235 time.Sleep(1 * time.Second)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100236 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100237 vpp.getSuite().assertNil(1, "Timeout while waiting for app '%s'", appName)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100238}
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100239
240func (vpp *VppInstance) createAfPacket(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100241 veth *NetInterface,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100242) (interface_types.InterfaceIndex, error) {
Adrian Villin46d66002024-05-15 04:33:41 -0400243 createReq := &af_packet.AfPacketCreateV3{
244 Mode: 1,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100245 UseRandomHwAddr: true,
246 HostIfName: veth.Name(),
Adrian Villin46d66002024-05-15 04:33:41 -0400247 Flags: af_packet.AfPacketFlags(11),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100248 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100249 if veth.hwAddress != (MacAddress{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100250 createReq.UseRandomHwAddr = false
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100251 createReq.HwAddr = veth.hwAddress
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100252 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100253
Adrian Villincee15aa2024-03-14 11:42:55 -0400254 vpp.getSuite().log("create af-packet interface " + veth.Name())
Adrian Villin46d66002024-05-15 04:33:41 -0400255 if err := vpp.apiStream.SendMsg(createReq); err != nil {
256 vpp.getSuite().hstFail()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100257 return 0, err
258 }
Adrian Villin46d66002024-05-15 04:33:41 -0400259 replymsg, err := vpp.apiStream.RecvMsg()
260 if err != nil {
261 return 0, err
262 }
263 reply := replymsg.(*af_packet.AfPacketCreateV3Reply)
264 err = api.RetvalToVPPApiError(reply.Retval)
265 if err != nil {
266 return 0, err
267 }
268
269 veth.index = reply.SwIfIndex
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100270
271 // Set to up
272 upReq := &interfaces.SwInterfaceSetFlags{
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100273 SwIfIndex: veth.index,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100274 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
275 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100276
Adrian Villincee15aa2024-03-14 11:42:55 -0400277 vpp.getSuite().log("set af-packet interface " + veth.Name() + " up")
Adrian Villin46d66002024-05-15 04:33:41 -0400278 if err := vpp.apiStream.SendMsg(upReq); err != nil {
279 return 0, err
280 }
281 replymsg, err = vpp.apiStream.RecvMsg()
282 if err != nil {
283 return 0, err
284 }
285 reply2 := replymsg.(*interfaces.SwInterfaceSetFlagsReply)
286 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100287 return 0, err
288 }
289
290 // Add address
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100291 if veth.addressWithPrefix() == (AddressWithPrefix{}) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100292 var err error
293 var ip4Address string
Filip Tehlar3a910ab2023-06-08 17:39:39 +0200294 if ip4Address, err = veth.ip4AddrAllocator.NewIp4InterfaceAddress(veth.peer.networkNumber); err == nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100295 veth.ip4Address = ip4Address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100296 } else {
297 return 0, err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100298 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100299 }
300 addressReq := &interfaces.SwInterfaceAddDelAddress{
301 IsAdd: true,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100302 SwIfIndex: veth.index,
303 Prefix: veth.addressWithPrefix(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100304 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100305
Adrian Villincee15aa2024-03-14 11:42:55 -0400306 vpp.getSuite().log("af-packet interface " + veth.Name() + " add address " + veth.ip4Address)
Adrian Villin46d66002024-05-15 04:33:41 -0400307 if err := vpp.apiStream.SendMsg(addressReq); err != nil {
308 return 0, err
309 }
310 replymsg, err = vpp.apiStream.RecvMsg()
311 if err != nil {
312 return 0, err
313 }
314 reply3 := replymsg.(*interfaces.SwInterfaceAddDelAddressReply)
315 err = api.RetvalToVPPApiError(reply3.Retval)
316 if err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100317 return 0, err
318 }
319
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100320 return veth.index, nil
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100321}
322
323func (vpp *VppInstance) addAppNamespace(
324 secret uint64,
325 ifx interface_types.InterfaceIndex,
326 namespaceId string,
327) error {
Adrian Villin46d66002024-05-15 04:33:41 -0400328 req := &session.AppNamespaceAddDelV4{
329 IsAdd: true,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100330 Secret: secret,
331 SwIfIndex: ifx,
332 NamespaceID: namespaceId,
Adrian Villin46d66002024-05-15 04:33:41 -0400333 SockName: defaultApiSocketFilePath,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100334 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100335
Adrian Villincee15aa2024-03-14 11:42:55 -0400336 vpp.getSuite().log("add app namespace " + namespaceId)
Adrian Villin46d66002024-05-15 04:33:41 -0400337 if err := vpp.apiStream.SendMsg(req); err != nil {
338 return err
339 }
340 replymsg, err := vpp.apiStream.RecvMsg()
341 if err != nil {
342 return err
343 }
344 reply := replymsg.(*session.AppNamespaceAddDelV4Reply)
345 if err = api.RetvalToVPPApiError(reply.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100346 return err
347 }
348
349 sessionReq := &session.SessionEnableDisable{
350 IsEnable: true,
351 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100352
Adrian Villincee15aa2024-03-14 11:42:55 -0400353 vpp.getSuite().log("enable app namespace " + namespaceId)
Adrian Villin46d66002024-05-15 04:33:41 -0400354 if err := vpp.apiStream.SendMsg(sessionReq); err != nil {
355 return err
356 }
357 replymsg, err = vpp.apiStream.RecvMsg()
358 if err != nil {
359 return err
360 }
361 reply2 := replymsg.(*session.SessionEnableDisableReply)
362 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100363 return err
364 }
365
366 return nil
367}
368
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100369func (vpp *VppInstance) createTap(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100370 tap *NetInterface,
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100371 tapId ...uint32,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100372) error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100373 var id uint32 = 1
374 if len(tapId) > 0 {
375 id = tapId[0]
376 }
Adrian Villin46d66002024-05-15 04:33:41 -0400377 createTapReq := &tapv2.TapCreateV3{
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100378 ID: id,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100379 HostIfNameSet: true,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100380 HostIfName: tap.Name(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100381 HostIP4PrefixSet: true,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100382 HostIP4Prefix: tap.ip4AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100383 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100384
Adrian Villincee15aa2024-03-14 11:42:55 -0400385 vpp.getSuite().log("create tap interface " + tap.Name())
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100386 // Create tap interface
Adrian Villin46d66002024-05-15 04:33:41 -0400387 if err := vpp.apiStream.SendMsg(createTapReq); err != nil {
388 return err
389 }
390 replymsg, err := vpp.apiStream.RecvMsg()
391 if err != nil {
392 return err
393 }
394 reply := replymsg.(*tapv2.TapCreateV3Reply)
395 if err = api.RetvalToVPPApiError(reply.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100396 return err
397 }
398
399 // Add address
400 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
401 IsAdd: true,
Adrian Villin46d66002024-05-15 04:33:41 -0400402 SwIfIndex: reply.SwIfIndex,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100403 Prefix: tap.peer.addressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100404 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100405
Adrian Villincee15aa2024-03-14 11:42:55 -0400406 vpp.getSuite().log("tap interface " + tap.Name() + " add address " + tap.peer.ip4Address)
Adrian Villin46d66002024-05-15 04:33:41 -0400407 if err := vpp.apiStream.SendMsg(addAddressReq); err != nil {
408 return err
409 }
410 replymsg, err = vpp.apiStream.RecvMsg()
411 if err != nil {
412 return err
413 }
414 reply2 := replymsg.(*interfaces.SwInterfaceAddDelAddressReply)
415 if err = api.RetvalToVPPApiError(reply2.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100416 return err
417 }
418
419 // Set interface to up
420 upReq := &interfaces.SwInterfaceSetFlags{
Adrian Villin46d66002024-05-15 04:33:41 -0400421 SwIfIndex: reply.SwIfIndex,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100422 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
423 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100424
Adrian Villincee15aa2024-03-14 11:42:55 -0400425 vpp.getSuite().log("set tap interface " + tap.Name() + " up")
Adrian Villin46d66002024-05-15 04:33:41 -0400426 if err := vpp.apiStream.SendMsg(upReq); err != nil {
427 return err
428 }
429 replymsg, err = vpp.apiStream.RecvMsg()
430 if err != nil {
431 return err
432 }
433 reply3 := replymsg.(*interfaces.SwInterfaceSetFlagsReply)
434 if err = api.RetvalToVPPApiError(reply3.Retval); err != nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100435 return err
436 }
437
438 return nil
439}
440
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100441func (vpp *VppInstance) saveLogs() {
442 logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100443 logSource := vpp.container.getHostWorkDir() + defaultLogFilePath
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100444 cmd := exec.Command("cp", logSource, logTarget)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100445 vpp.getSuite().log(cmd.String())
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100446 cmd.Run()
447}
448
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100449func (vpp *VppInstance) disconnect() {
450 vpp.connection.Disconnect()
Adrian Villin46d66002024-05-15 04:33:41 -0400451 vpp.apiStream.Close()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100452}
Filip Tehlar608d0062023-04-28 10:29:47 +0200453
454func (vpp *VppInstance) generateCpuConfig() string {
455 var c Stanza
456 var s string
457 if len(vpp.cpus) < 1 {
458 return ""
459 }
460 c.newStanza("cpu").
461 append(fmt.Sprintf("main-core %d", vpp.cpus[0]))
Adrian Villin0df582e2024-05-22 09:26:47 -0400462 vpp.getSuite().log(fmt.Sprintf("main-core %d", vpp.cpus[0]))
Filip Tehlar608d0062023-04-28 10:29:47 +0200463 workers := vpp.cpus[1:]
464
465 if len(workers) > 0 {
466 for i := 0; i < len(workers); i++ {
467 if i != 0 {
468 s = s + ", "
469 }
470 s = s + fmt.Sprintf("%d", workers[i])
471 }
472 c.append(fmt.Sprintf("corelist-workers %s", s))
Adrian Villin0df582e2024-05-22 09:26:47 -0400473 vpp.getSuite().log("corelist-workers " + s)
Filip Tehlar608d0062023-04-28 10:29:47 +0200474 }
475 return c.close().toString()
476}