blob: c20c5e2ee8cbf7fe8385077e0264643395e1dc71 [file] [log] [blame]
Maros Ondrejicka11a03e92022-12-01 09:56:37 +01001package main
2
3import (
Filip Tehlarf3ee2b62023-01-09 12:07:09 +01004 "fmt"
Filip Tehlarec5c40b2023-02-28 18:59:15 +01005 "os"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +01006 "os/exec"
Filip Tehlarec5c40b2023-02-28 18:59:15 +01007 "os/signal"
Filip Tehlar543cd572023-06-27 10:01:37 +02008 "strconv"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +01009 "strings"
Filip Tehlarec5c40b2023-02-28 18:59:15 +010010 "syscall"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010011 "time"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010012
Filip Tehlar9abba112023-03-07 10:13:19 +010013 "github.com/edwarnicke/exechelper"
14
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010015 "go.fd.io/govpp"
16 "go.fd.io/govpp/api"
17 "go.fd.io/govpp/binapi/af_packet"
18 interfaces "go.fd.io/govpp/binapi/interface"
19 "go.fd.io/govpp/binapi/interface_types"
20 "go.fd.io/govpp/binapi/session"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010021 "go.fd.io/govpp/binapi/tapv2"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010022 "go.fd.io/govpp/binapi/vpe"
23 "go.fd.io/govpp/core"
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010024)
25
26const vppConfigTemplate = `unix {
27 nodaemon
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010028 log %[1]s%[4]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010029 full-coredump
30 cli-listen %[1]s%[2]s
31 runtime-dir %[1]s/var/run
32 gid vpp
33}
34
35api-trace {
36 on
37}
38
39api-segment {
40 gid vpp
41}
42
43socksvr {
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +010044 socket-name %[1]s%[3]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010045}
46
47statseg {
48 socket-name %[1]s/var/run/vpp/stats.sock
49}
50
51plugins {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010052 plugin default { disable }
53
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010054 plugin unittest_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010055 plugin quic_plugin.so { enable }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010056 plugin af_packet_plugin.so { enable }
57 plugin hs_apps_plugin.so { enable }
58 plugin http_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010059}
60
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010061logging {
62 default-log-level debug
63 default-syslog-log-level debug
64}
65
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010066`
67
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010068const (
69 defaultCliSocketFilePath = "/var/run/vpp/cli.sock"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010070 defaultApiSocketFilePath = "/var/run/vpp/api.sock"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010071 defaultLogFilePath = "/var/log/vpp/vpp.log"
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010072)
73
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010074type VppInstance struct {
Maros Ondrejicka300f70d2023-02-21 10:53:20 +010075 container *Container
Filip Tehlar608d0062023-04-28 10:29:47 +020076 additionalConfig []Stanza
Maros Ondrejicka300f70d2023-02-21 10:53:20 +010077 connection *core.Connection
78 apiChannel api.Channel
Filip Tehlar608d0062023-04-28 10:29:47 +020079 cpus []int
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010080}
81
Maros Ondrejickae7625d02023-02-28 16:55:01 +010082func (vpp *VppInstance) getSuite() *HstSuite {
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010083 return vpp.container.suite
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010084}
85
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010086func (vpp *VppInstance) getCliSocket() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010087 return fmt.Sprintf("%s%s", vpp.container.getContainerWorkDir(), defaultCliSocketFilePath)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010088}
89
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010090func (vpp *VppInstance) getRunDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010091 return vpp.container.getContainerWorkDir() + "/var/run/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010092}
93
94func (vpp *VppInstance) getLogDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010095 return vpp.container.getContainerWorkDir() + "/var/log/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010096}
97
98func (vpp *VppInstance) getEtcDir() string {
Maros Ondrejickae7625d02023-02-28 16:55:01 +010099 return vpp.container.getContainerWorkDir() + "/etc/vpp"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100100}
101
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100102func (vpp *VppInstance) start() error {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100103 // Create folders
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100104 containerWorkDir := vpp.container.getContainerWorkDir()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100105
106 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir())
107 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir())
108 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
109
110 // Create startup.conf inside the container
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100111 configContent := fmt.Sprintf(
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100112 vppConfigTemplate,
113 containerWorkDir,
114 defaultCliSocketFilePath,
115 defaultApiSocketFilePath,
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100116 defaultLogFilePath,
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100117 )
Filip Tehlar608d0062023-04-28 10:29:47 +0200118 configContent += vpp.generateCpuConfig()
119 for _, c := range vpp.additionalConfig {
120 configContent += c.toString()
121 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100122 startupFileName := vpp.getEtcDir() + "/startup.conf"
123 vpp.container.createFile(startupFileName, configContent)
124
Filip Tehlar1a661502023-03-08 11:55:50 +0100125 // create wrapper script for vppctl with proper CLI socket path
126 cliContent := "#!/usr/bin/bash\nvppctl -s " + vpp.getRunDir() + "/cli.sock"
127 vppcliFileName := "/usr/bin/vppcli"
128 vpp.container.createFile(vppcliFileName, cliContent)
129 vpp.container.exec("chmod 0755 " + vppcliFileName)
130
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100131 if *isVppDebug {
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100132 sig := make(chan os.Signal, 1)
133 signal.Notify(sig, syscall.SIGINT)
134 cont := make(chan bool, 1)
135 go func() {
Filip Tehlar9abba112023-03-07 10:13:19 +0100136 <-sig
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100137 cont <- true
138 }()
139
Filip Tehlara6b1a7d2023-09-02 08:39:25 +0200140 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100141 fmt.Println("run following command in different terminal:")
Filip Tehlara6b1a7d2023-09-02 08:39:25 +0200142 fmt.Println("docker exec -it " + vpp.container.name + " gdb -ex \"attach $(docker exec " + vpp.container.name + " pidof vpp)\"")
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100143 fmt.Println("Afterwards press CTRL+C to continue")
144 <-cont
145 fmt.Println("continuing...")
146 } else {
147 // Start VPP
148 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
149 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100150
151 // Connect to VPP and store the connection
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100152 sockAddress := vpp.container.getHostWorkDir() + defaultApiSocketFilePath
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100153 conn, connEv, err := govpp.AsyncConnect(
154 sockAddress,
155 core.DefaultMaxReconnectAttempts,
156 core.DefaultReconnectInterval)
157 if err != nil {
158 fmt.Println("async connect error: ", err)
159 }
160 vpp.connection = conn
161
162 // ... wait for Connected event
163 e := <-connEv
164 if e.State != core.Connected {
165 fmt.Println("connecting to VPP failed: ", e.Error)
166 }
167
168 // ... check compatibility of used messages
169 ch, err := conn.NewAPIChannel()
170 if err != nil {
171 fmt.Println("creating channel failed: ", err)
172 }
173 if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
174 fmt.Println("compatibility error: ", err)
175 }
176 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
177 fmt.Println("compatibility error: ", err)
178 }
179 vpp.apiChannel = ch
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100180
181 return nil
182}
183
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100184func (vpp *VppInstance) vppctl(command string, arguments ...any) string {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100185 vppCliCommand := fmt.Sprintf(command, arguments...)
186 containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
187 vpp.container.name, vpp.getCliSocket(), vppCliCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100188 vpp.getSuite().log(containerExecCommand)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100189 output, err := exechelper.CombinedOutput(containerExecCommand)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100190 vpp.getSuite().assertNil(err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100191
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100192 return string(output)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100193}
194
Filip Tehlar543cd572023-06-27 10:01:37 +0200195func (vpp *VppInstance) GetSessionStat(stat string) int {
196 o := vpp.vppctl("show session stats")
197 vpp.getSuite().log(o)
198 for _, line := range strings.Split(o, "\n") {
199 if strings.Contains(line, stat) {
200 tokens := strings.Split(strings.TrimSpace(line), " ")
201 val, err := strconv.Atoi(tokens[0])
202 if err != nil {
203 vpp.getSuite().FailNow("failed to parse stat value %s", err)
204 return 0
205 }
206 return val
207 }
208 }
209 return 0
210}
211
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100212func (vpp *VppInstance) waitForApp(appName string, timeout int) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100213 for i := 0; i < timeout; i++ {
214 o := vpp.vppctl("show app")
215 if strings.Contains(o, appName) {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100216 return
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100217 }
218 time.Sleep(1 * time.Second)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100219 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100220 vpp.getSuite().assertNil(1, "Timeout while waiting for app '%s'", appName)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100221}
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100222
223func (vpp *VppInstance) createAfPacket(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100224 veth *NetInterface,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100225) (interface_types.InterfaceIndex, error) {
226 createReq := &af_packet.AfPacketCreateV2{
227 UseRandomHwAddr: true,
228 HostIfName: veth.Name(),
229 }
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100230 if veth.hwAddress != (MacAddress{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100231 createReq.UseRandomHwAddr = false
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100232 createReq.HwAddr = veth.hwAddress
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100233 }
234 createReply := &af_packet.AfPacketCreateV2Reply{}
235
236 if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil {
237 return 0, err
238 }
Filip Tehlarb41b0af2023-03-20 12:39:20 +0100239 veth.index = createReply.SwIfIndex
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100240
241 // Set to up
242 upReq := &interfaces.SwInterfaceSetFlags{
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100243 SwIfIndex: veth.index,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100244 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
245 }
246 upReply := &interfaces.SwInterfaceSetFlagsReply{}
247
248 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
249 return 0, err
250 }
251
252 // Add address
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100253 if veth.addressWithPrefix() == (AddressWithPrefix{}) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100254 var err error
255 var ip4Address string
Filip Tehlar3a910ab2023-06-08 17:39:39 +0200256 if ip4Address, err = veth.ip4AddrAllocator.NewIp4InterfaceAddress(veth.peer.networkNumber); err == nil {
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100257 veth.ip4Address = ip4Address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100258 } else {
259 return 0, err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100260 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100261 }
262 addressReq := &interfaces.SwInterfaceAddDelAddress{
263 IsAdd: true,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100264 SwIfIndex: veth.index,
265 Prefix: veth.addressWithPrefix(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100266 }
267 addressReply := &interfaces.SwInterfaceAddDelAddressReply{}
268
269 if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil {
270 return 0, err
271 }
272
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100273 return veth.index, nil
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100274}
275
276func (vpp *VppInstance) addAppNamespace(
277 secret uint64,
278 ifx interface_types.InterfaceIndex,
279 namespaceId string,
280) error {
281 req := &session.AppNamespaceAddDelV2{
282 Secret: secret,
283 SwIfIndex: ifx,
284 NamespaceID: namespaceId,
285 }
286 reply := &session.AppNamespaceAddDelV2Reply{}
287
288 if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil {
289 return err
290 }
291
292 sessionReq := &session.SessionEnableDisable{
293 IsEnable: true,
294 }
295 sessionReply := &session.SessionEnableDisableReply{}
296
297 if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil {
298 return err
299 }
300
301 return nil
302}
303
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100304func (vpp *VppInstance) createTap(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100305 tap *NetInterface,
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100306 tapId ...uint32,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100307) error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100308 var id uint32 = 1
309 if len(tapId) > 0 {
310 id = tapId[0]
311 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100312 createTapReq := &tapv2.TapCreateV2{
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100313 ID: id,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100314 HostIfNameSet: true,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100315 HostIfName: tap.Name(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100316 HostIP4PrefixSet: true,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100317 HostIP4Prefix: tap.ip4AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100318 }
319 createTapReply := &tapv2.TapCreateV2Reply{}
320
321 // Create tap interface
322 if err := vpp.apiChannel.SendRequest(createTapReq).ReceiveReply(createTapReply); err != nil {
323 return err
324 }
325
326 // Add address
327 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
328 IsAdd: true,
329 SwIfIndex: createTapReply.SwIfIndex,
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100330 Prefix: tap.peer.addressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100331 }
332 addAddressReply := &interfaces.SwInterfaceAddDelAddressReply{}
333
334 if err := vpp.apiChannel.SendRequest(addAddressReq).ReceiveReply(addAddressReply); err != nil {
335 return err
336 }
337
338 // Set interface to up
339 upReq := &interfaces.SwInterfaceSetFlags{
340 SwIfIndex: createTapReply.SwIfIndex,
341 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
342 }
343 upReply := &interfaces.SwInterfaceSetFlagsReply{}
344
345 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
346 return err
347 }
348
349 return nil
350}
351
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100352func (vpp *VppInstance) saveLogs() {
353 logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100354 logSource := vpp.container.getHostWorkDir() + defaultLogFilePath
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100355 cmd := exec.Command("cp", logSource, logTarget)
Maros Ondrejickae7625d02023-02-28 16:55:01 +0100356 vpp.getSuite().T().Helper()
357 vpp.getSuite().log(cmd.String())
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100358 cmd.Run()
359}
360
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100361func (vpp *VppInstance) disconnect() {
362 vpp.connection.Disconnect()
363 vpp.apiChannel.Close()
364}
Filip Tehlar608d0062023-04-28 10:29:47 +0200365
366func (vpp *VppInstance) generateCpuConfig() string {
367 var c Stanza
368 var s string
369 if len(vpp.cpus) < 1 {
370 return ""
371 }
372 c.newStanza("cpu").
373 append(fmt.Sprintf("main-core %d", vpp.cpus[0]))
374 workers := vpp.cpus[1:]
375
376 if len(workers) > 0 {
377 for i := 0; i < len(workers); i++ {
378 if i != 0 {
379 s = s + ", "
380 }
381 s = s + fmt.Sprintf("%d", workers[i])
382 }
383 c.append(fmt.Sprintf("corelist-workers %s", s))
384 }
385 return c.close().toString()
386}