blob: 1c28ec920b7af3edc2b7c382ea868cb8d7c98178 [file] [log] [blame]
Maros Ondrejicka11a03e92022-12-01 09:56:37 +01001package main
2
3import (
Filip Tehlarf3ee2b62023-01-09 12:07:09 +01004 "fmt"
Maros Ondrejicka11a03e92022-12-01 09:56:37 +01005 "github.com/edwarnicke/exechelper"
Filip Tehlarec5c40b2023-02-28 18:59:15 +01006 "os"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +01007 "os/exec"
Filip Tehlarec5c40b2023-02-28 18:59:15 +01008 "os/signal"
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
13 "go.fd.io/govpp"
14 "go.fd.io/govpp/api"
15 "go.fd.io/govpp/binapi/af_packet"
16 interfaces "go.fd.io/govpp/binapi/interface"
17 "go.fd.io/govpp/binapi/interface_types"
18 "go.fd.io/govpp/binapi/session"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010019 "go.fd.io/govpp/binapi/tapv2"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010020 "go.fd.io/govpp/binapi/vpe"
21 "go.fd.io/govpp/core"
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010022)
23
24const vppConfigTemplate = `unix {
25 nodaemon
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010026 log %[1]s%[4]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010027 full-coredump
28 cli-listen %[1]s%[2]s
29 runtime-dir %[1]s/var/run
30 gid vpp
31}
32
33api-trace {
34 on
35}
36
37api-segment {
38 gid vpp
39}
40
41socksvr {
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +010042 socket-name %[1]s%[3]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010043}
44
45statseg {
46 socket-name %[1]s/var/run/vpp/stats.sock
47}
48
49plugins {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010050 plugin default { disable }
51
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010052 plugin unittest_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010053 plugin quic_plugin.so { enable }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010054 plugin af_packet_plugin.so { enable }
55 plugin hs_apps_plugin.so { enable }
56 plugin http_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010057}
58
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010059logging {
60 default-log-level debug
61 default-syslog-log-level debug
62}
63
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010064`
65
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010066const (
67 defaultCliSocketFilePath = "/var/run/vpp/cli.sock"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010068 defaultApiSocketFilePath = "/var/run/vpp/api.sock"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010069 defaultLogFilePath = "/var/log/vpp/vpp.log"
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010070)
71
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010072type VppInstance struct {
Maros Ondrejicka300f70d2023-02-21 10:53:20 +010073 container *Container
74 additionalConfig Stanza
75 connection *core.Connection
76 apiChannel api.Channel
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010077}
78
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010079func (vpp *VppInstance) Suite() *HstSuite {
80 return vpp.container.suite
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010081}
82
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010083func (vpp *VppInstance) getCliSocket() string {
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +010084 return fmt.Sprintf("%s%s", vpp.container.GetContainerWorkDir(), defaultCliSocketFilePath)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010085}
86
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010087func (vpp *VppInstance) getRunDir() string {
88 return vpp.container.GetContainerWorkDir() + "/var/run/vpp"
89}
90
91func (vpp *VppInstance) getLogDir() string {
92 return vpp.container.GetContainerWorkDir() + "/var/log/vpp"
93}
94
95func (vpp *VppInstance) getEtcDir() string {
96 return vpp.container.GetContainerWorkDir() + "/etc/vpp"
97}
98
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010099func (vpp *VppInstance) start() error {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100100 // Create folders
101 containerWorkDir := vpp.container.GetContainerWorkDir()
102
103 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir())
104 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir())
105 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
106
107 // Create startup.conf inside the container
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100108 configContent := fmt.Sprintf(
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100109 vppConfigTemplate,
110 containerWorkDir,
111 defaultCliSocketFilePath,
112 defaultApiSocketFilePath,
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100113 defaultLogFilePath,
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100114 )
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100115 configContent += vpp.additionalConfig.ToString()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100116 startupFileName := vpp.getEtcDir() + "/startup.conf"
117 vpp.container.createFile(startupFileName, configContent)
118
Filip Tehlarec5c40b2023-02-28 18:59:15 +0100119 if *IsVppDebug {
120 sig := make(chan os.Signal, 1)
121 signal.Notify(sig, syscall.SIGINT)
122 cont := make(chan bool, 1)
123 go func() {
124 sig := <-sig
125 fmt.Println(sig)
126 cont <- true
127 }()
128
129 // Start VPP in GDB and wait for user to attach it
130 vpp.container.execServer("su -c \"gdb -ex run --args vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
131 fmt.Println("run following command in different terminal:")
132 fmt.Println("docker exec -it " + vpp.container.name + " gdb -ex \"attach $(docker exec " + vpp.container.name + " pidof gdb)\"")
133 fmt.Println("Afterwards press CTRL+C to continue")
134 <-cont
135 fmt.Println("continuing...")
136 } else {
137 // Start VPP
138 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
139 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100140
141 // Connect to VPP and store the connection
142 sockAddress := vpp.container.GetHostWorkDir() + defaultApiSocketFilePath
143 conn, connEv, err := govpp.AsyncConnect(
144 sockAddress,
145 core.DefaultMaxReconnectAttempts,
146 core.DefaultReconnectInterval)
147 if err != nil {
148 fmt.Println("async connect error: ", err)
149 }
150 vpp.connection = conn
151
152 // ... wait for Connected event
153 e := <-connEv
154 if e.State != core.Connected {
155 fmt.Println("connecting to VPP failed: ", e.Error)
156 }
157
158 // ... check compatibility of used messages
159 ch, err := conn.NewAPIChannel()
160 if err != nil {
161 fmt.Println("creating channel failed: ", err)
162 }
163 if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
164 fmt.Println("compatibility error: ", err)
165 }
166 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
167 fmt.Println("compatibility error: ", err)
168 }
169 vpp.apiChannel = ch
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100170
171 return nil
172}
173
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100174func (vpp *VppInstance) vppctl(command string, arguments ...any) string {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100175 vppCliCommand := fmt.Sprintf(command, arguments...)
176 containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
177 vpp.container.name, vpp.getCliSocket(), vppCliCommand)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100178 vpp.Suite().log(containerExecCommand)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100179 output, err := exechelper.CombinedOutput(containerExecCommand)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100180 vpp.Suite().assertNil(err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100181
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100182 return string(output)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100183}
184
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100185func (vpp *VppInstance) waitForApp(appName string, timeout int) {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100186 for i := 0; i < timeout; i++ {
187 o := vpp.vppctl("show app")
188 if strings.Contains(o, appName) {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100189 return
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100190 }
191 time.Sleep(1 * time.Second)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100192 }
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100193 vpp.Suite().assertNil(1, "Timeout while waiting for app '%s'", appName)
194 return
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100195}
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100196
197func (vpp *VppInstance) createAfPacket(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100198 veth *NetInterface,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100199) (interface_types.InterfaceIndex, error) {
200 createReq := &af_packet.AfPacketCreateV2{
201 UseRandomHwAddr: true,
202 HostIfName: veth.Name(),
203 }
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100204 if veth.HwAddress() != (MacAddress{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100205 createReq.UseRandomHwAddr = false
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100206 createReq.HwAddr = veth.HwAddress()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100207 }
208 createReply := &af_packet.AfPacketCreateV2Reply{}
209
210 if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil {
211 return 0, err
212 }
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100213 veth.SetIndex(createReply.SwIfIndex)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100214
215 // Set to up
216 upReq := &interfaces.SwInterfaceSetFlags{
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100217 SwIfIndex: veth.Index(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100218 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
219 }
220 upReply := &interfaces.SwInterfaceSetFlagsReply{}
221
222 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
223 return 0, err
224 }
225
226 // Add address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100227 if veth.AddressWithPrefix() == (AddressWithPrefix{}) {
228 var err error
229 var ip4Address string
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100230 if ip4Address, err = veth.addresser.NewIp4Address(veth.Peer().networkNumber); err == nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100231 veth.SetAddress(ip4Address)
232 } else {
233 return 0, err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100234 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100235 }
236 addressReq := &interfaces.SwInterfaceAddDelAddress{
237 IsAdd: true,
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100238 SwIfIndex: veth.Index(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100239 Prefix: veth.AddressWithPrefix(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100240 }
241 addressReply := &interfaces.SwInterfaceAddDelAddressReply{}
242
243 if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil {
244 return 0, err
245 }
246
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100247 return veth.Index(), nil
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100248}
249
250func (vpp *VppInstance) addAppNamespace(
251 secret uint64,
252 ifx interface_types.InterfaceIndex,
253 namespaceId string,
254) error {
255 req := &session.AppNamespaceAddDelV2{
256 Secret: secret,
257 SwIfIndex: ifx,
258 NamespaceID: namespaceId,
259 }
260 reply := &session.AppNamespaceAddDelV2Reply{}
261
262 if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil {
263 return err
264 }
265
266 sessionReq := &session.SessionEnableDisable{
267 IsEnable: true,
268 }
269 sessionReply := &session.SessionEnableDisableReply{}
270
271 if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil {
272 return err
273 }
274
275 return nil
276}
277
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100278func (vpp *VppInstance) createTap(
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100279 tap *NetInterface,
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100280 tapId ...uint32,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100281) error {
Maros Ondrejickac2f76f42023-02-27 13:22:45 +0100282 var id uint32 = 1
283 if len(tapId) > 0 {
284 id = tapId[0]
285 }
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100286 createTapReq := &tapv2.TapCreateV2{
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100287 ID: id,
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100288 HostIfNameSet: true,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100289 HostIfName: tap.Name(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100290 HostIP4PrefixSet: true,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100291 HostIP4Prefix: tap.IP4AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100292 }
293 createTapReply := &tapv2.TapCreateV2Reply{}
294
295 // Create tap interface
296 if err := vpp.apiChannel.SendRequest(createTapReq).ReceiveReply(createTapReply); err != nil {
297 return err
298 }
299
300 // Add address
301 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
302 IsAdd: true,
303 SwIfIndex: createTapReply.SwIfIndex,
Maros Ondrejicka40cba402023-02-23 13:19:15 +0100304 Prefix: tap.Peer().AddressWithPrefix(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100305 }
306 addAddressReply := &interfaces.SwInterfaceAddDelAddressReply{}
307
308 if err := vpp.apiChannel.SendRequest(addAddressReq).ReceiveReply(addAddressReply); err != nil {
309 return err
310 }
311
312 // Set interface to up
313 upReq := &interfaces.SwInterfaceSetFlags{
314 SwIfIndex: createTapReply.SwIfIndex,
315 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
316 }
317 upReply := &interfaces.SwInterfaceSetFlagsReply{}
318
319 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
320 return err
321 }
322
323 return nil
324}
325
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100326func (vpp *VppInstance) saveLogs() {
327 logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
328 logSource := vpp.container.GetHostWorkDir() + defaultLogFilePath
329 cmd := exec.Command("cp", logSource, logTarget)
330 vpp.Suite().T().Helper()
331 vpp.Suite().log(cmd.String())
332 cmd.Run()
333}
334
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100335func (vpp *VppInstance) disconnect() {
336 vpp.connection.Disconnect()
337 vpp.apiChannel.Close()
338}