blob: 89e67d0fac77f452ff1d55a194de6e31936c5821 [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"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +01008 "strings"
Filip Tehlarec5c40b2023-02-28 18:59:15 +01009 "syscall"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010010 "time"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010011
Filip Tehlar9abba112023-03-07 10:13:19 +010012 "github.com/edwarnicke/exechelper"
13
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010014 "go.fd.io/govpp"
15 "go.fd.io/govpp/api"
16 "go.fd.io/govpp/binapi/af_packet"
17 interfaces "go.fd.io/govpp/binapi/interface"
18 "go.fd.io/govpp/binapi/interface_types"
19 "go.fd.io/govpp/binapi/session"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010020 "go.fd.io/govpp/binapi/tapv2"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010021 "go.fd.io/govpp/binapi/vpe"
22 "go.fd.io/govpp/core"
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010023)
24
25const vppConfigTemplate = `unix {
26 nodaemon
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010027 log %[1]s%[4]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010028 full-coredump
29 cli-listen %[1]s%[2]s
30 runtime-dir %[1]s/var/run
31 gid vpp
32}
33
34api-trace {
35 on
36}
37
38api-segment {
39 gid vpp
40}
41
42socksvr {
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +010043 socket-name %[1]s%[3]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010044}
45
46statseg {
47 socket-name %[1]s/var/run/vpp/stats.sock
48}
49
50plugins {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010051 plugin default { disable }
52
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010053 plugin unittest_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010054 plugin quic_plugin.so { enable }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010055 plugin af_packet_plugin.so { enable }
56 plugin hs_apps_plugin.so { enable }
57 plugin http_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010058}
59
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010060logging {
61 default-log-level debug
62 default-syslog-log-level debug
63}
64
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010065`
66
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010067const (
68 defaultCliSocketFilePath = "/var/run/vpp/cli.sock"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010069 defaultApiSocketFilePath = "/var/run/vpp/api.sock"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010070 defaultLogFilePath = "/var/log/vpp/vpp.log"
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010071)
72
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010073type VppInstance struct {
Maros Ondrejicka300f70d2023-02-21 10:53:20 +010074 container *Container
75 additionalConfig Stanza
76 connection *core.Connection
77 apiChannel api.Channel
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010078}
79
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010080func (vpp *VppInstance) Suite() *HstSuite {
81 return vpp.container.suite
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010082}
83
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010084func (vpp *VppInstance) getCliSocket() string {
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +010085 return fmt.Sprintf("%s%s", vpp.container.GetContainerWorkDir(), defaultCliSocketFilePath)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010086}
87
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010088func (vpp *VppInstance) getRunDir() string {
89 return vpp.container.GetContainerWorkDir() + "/var/run/vpp"
90}
91
92func (vpp *VppInstance) getLogDir() string {
93 return vpp.container.GetContainerWorkDir() + "/var/log/vpp"
94}
95
96func (vpp *VppInstance) getEtcDir() string {
97 return vpp.container.GetContainerWorkDir() + "/etc/vpp"
98}
99
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100100func (vpp *VppInstance) start() error {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100101 // Create folders
102 containerWorkDir := vpp.container.GetContainerWorkDir()
103
104 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir())
105 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir())
106 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
107
108 // Create startup.conf inside the container
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100109 configContent := fmt.Sprintf(
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100110 vppConfigTemplate,
111 containerWorkDir,
112 defaultCliSocketFilePath,
113 defaultApiSocketFilePath,
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100114 defaultLogFilePath,
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100115 )
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100116 configContent += vpp.additionalConfig.ToString()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100117 startupFileName := vpp.getEtcDir() + "/startup.conf"
118 vpp.container.createFile(startupFileName, configContent)
119
Filip Tehlar1a661502023-03-08 11:55:50 +0100120 // create wrapper script for vppctl with proper CLI socket path
121 cliContent := "#!/usr/bin/bash\nvppctl -s " + vpp.getRunDir() + "/cli.sock"
122 vppcliFileName := "/usr/bin/vppcli"
123 vpp.container.createFile(vppcliFileName, cliContent)
124 vpp.container.exec("chmod 0755 " + vppcliFileName)
125
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100126 if *IsVppDebug {
127 sig := make(chan os.Signal, 1)
128 signal.Notify(sig, syscall.SIGINT)
129 cont := make(chan bool, 1)
130 go func() {
Filip Tehlar9abba112023-03-07 10:13:19 +0100131 <-sig
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100132 cont <- true
133 }()
134
135 // Start VPP in GDB and wait for user to attach it
136 vpp.container.execServer("su -c \"gdb -ex run --args vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
137 fmt.Println("run following command in different terminal:")
138 fmt.Println("docker exec -it " + vpp.container.name + " gdb -ex \"attach $(docker exec " + vpp.container.name + " pidof gdb)\"")
139 fmt.Println("Afterwards press CTRL+C to continue")
140 <-cont
141 fmt.Println("continuing...")
142 } else {
143 // Start VPP
144 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
145 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100146
147 // Connect to VPP and store the connection
148 sockAddress := vpp.container.GetHostWorkDir() + defaultApiSocketFilePath
149 conn, connEv, err := govpp.AsyncConnect(
150 sockAddress,
151 core.DefaultMaxReconnectAttempts,
152 core.DefaultReconnectInterval)
153 if err != nil {
154 fmt.Println("async connect error: ", err)
155 }
156 vpp.connection = conn
157
158 // ... wait for Connected event
159 e := <-connEv
160 if e.State != core.Connected {
161 fmt.Println("connecting to VPP failed: ", e.Error)
162 }
163
164 // ... check compatibility of used messages
165 ch, err := conn.NewAPIChannel()
166 if err != nil {
167 fmt.Println("creating channel failed: ", err)
168 }
169 if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
170 fmt.Println("compatibility error: ", err)
171 }
172 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
173 fmt.Println("compatibility error: ", err)
174 }
175 vpp.apiChannel = ch
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100176
177 return nil
178}
179
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100180func (vpp *VppInstance) vppctl(command string, arguments ...any) string {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100181 vppCliCommand := fmt.Sprintf(command, arguments...)
182 containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
183 vpp.container.name, vpp.getCliSocket(), vppCliCommand)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100184 vpp.Suite().log(containerExecCommand)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100185 output, err := exechelper.CombinedOutput(containerExecCommand)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100186 vpp.Suite().assertNil(err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100187
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100188 return string(output)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100189}
190
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100191func (vpp *VppInstance) waitForApp(appName string, timeout int) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100192 for i := 0; i < timeout; i++ {
193 o := vpp.vppctl("show app")
194 if strings.Contains(o, appName) {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100195 return
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100196 }
197 time.Sleep(1 * time.Second)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100198 }
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100199 vpp.Suite().assertNil(1, "Timeout while waiting for app '%s'", appName)
200 return
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100201}
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100202
203func (vpp *VppInstance) createAfPacket(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100204 veth *NetInterface,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100205) (interface_types.InterfaceIndex, error) {
206 createReq := &af_packet.AfPacketCreateV2{
207 UseRandomHwAddr: true,
208 HostIfName: veth.Name(),
209 }
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100210 if veth.HwAddress() != (MacAddress{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100211 createReq.UseRandomHwAddr = false
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100212 createReq.HwAddr = veth.HwAddress()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100213 }
214 createReply := &af_packet.AfPacketCreateV2Reply{}
215
216 if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil {
217 return 0, err
218 }
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100219 veth.SetIndex(createReply.SwIfIndex)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100220
221 // Set to up
222 upReq := &interfaces.SwInterfaceSetFlags{
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100223 SwIfIndex: veth.Index(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100224 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
225 }
226 upReply := &interfaces.SwInterfaceSetFlagsReply{}
227
228 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
229 return 0, err
230 }
231
232 // Add address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100233 if veth.AddressWithPrefix() == (AddressWithPrefix{}) {
234 var err error
235 var ip4Address string
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100236 if ip4Address, err = veth.addresser.NewIp4Address(veth.Peer().networkNumber); err == nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100237 veth.SetAddress(ip4Address)
238 } else {
239 return 0, err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100240 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100241 }
242 addressReq := &interfaces.SwInterfaceAddDelAddress{
243 IsAdd: true,
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100244 SwIfIndex: veth.Index(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100245 Prefix: veth.AddressWithPrefix(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100246 }
247 addressReply := &interfaces.SwInterfaceAddDelAddressReply{}
248
249 if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil {
250 return 0, err
251 }
252
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100253 return veth.Index(), nil
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100254}
255
256func (vpp *VppInstance) addAppNamespace(
257 secret uint64,
258 ifx interface_types.InterfaceIndex,
259 namespaceId string,
260) error {
261 req := &session.AppNamespaceAddDelV2{
262 Secret: secret,
263 SwIfIndex: ifx,
264 NamespaceID: namespaceId,
265 }
266 reply := &session.AppNamespaceAddDelV2Reply{}
267
268 if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil {
269 return err
270 }
271
272 sessionReq := &session.SessionEnableDisable{
273 IsEnable: true,
274 }
275 sessionReply := &session.SessionEnableDisableReply{}
276
277 if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil {
278 return err
279 }
280
281 return nil
282}
283
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100284func (vpp *VppInstance) createTap(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100285 tap *NetInterface,
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100286 tapId ...uint32,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100287) error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100288 var id uint32 = 1
289 if len(tapId) > 0 {
290 id = tapId[0]
291 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100292 createTapReq := &tapv2.TapCreateV2{
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100293 ID: id,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100294 HostIfNameSet: true,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100295 HostIfName: tap.Name(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100296 HostIP4PrefixSet: true,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100297 HostIP4Prefix: tap.IP4AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100298 }
299 createTapReply := &tapv2.TapCreateV2Reply{}
300
301 // Create tap interface
302 if err := vpp.apiChannel.SendRequest(createTapReq).ReceiveReply(createTapReply); err != nil {
303 return err
304 }
305
306 // Add address
307 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
308 IsAdd: true,
309 SwIfIndex: createTapReply.SwIfIndex,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100310 Prefix: tap.Peer().AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100311 }
312 addAddressReply := &interfaces.SwInterfaceAddDelAddressReply{}
313
314 if err := vpp.apiChannel.SendRequest(addAddressReq).ReceiveReply(addAddressReply); err != nil {
315 return err
316 }
317
318 // Set interface to up
319 upReq := &interfaces.SwInterfaceSetFlags{
320 SwIfIndex: createTapReply.SwIfIndex,
321 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
322 }
323 upReply := &interfaces.SwInterfaceSetFlagsReply{}
324
325 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
326 return err
327 }
328
329 return nil
330}
331
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100332func (vpp *VppInstance) saveLogs() {
333 logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
334 logSource := vpp.container.GetHostWorkDir() + defaultLogFilePath
335 cmd := exec.Command("cp", logSource, logTarget)
336 vpp.Suite().T().Helper()
337 vpp.Suite().log(cmd.String())
338 cmd.Run()
339}
340
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100341func (vpp *VppInstance) disconnect() {
342 vpp.connection.Disconnect()
343 vpp.apiChannel.Close()
344}