blob: 2ecb4de3ecdcaad575310803dc0aba02a0bac740 [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"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +01006 "os/exec"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +01007 "strings"
8 "time"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +01009
10 "go.fd.io/govpp"
11 "go.fd.io/govpp/api"
12 "go.fd.io/govpp/binapi/af_packet"
13 interfaces "go.fd.io/govpp/binapi/interface"
14 "go.fd.io/govpp/binapi/interface_types"
15 "go.fd.io/govpp/binapi/session"
Maros Ondrejicka7550dd22023-02-07 20:40:27 +010016 "go.fd.io/govpp/binapi/tapv2"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010017 "go.fd.io/govpp/binapi/vpe"
18 "go.fd.io/govpp/core"
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010019)
20
21const vppConfigTemplate = `unix {
22 nodaemon
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010023 log %[1]s%[4]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010024 full-coredump
25 cli-listen %[1]s%[2]s
26 runtime-dir %[1]s/var/run
27 gid vpp
28}
29
30api-trace {
31 on
32}
33
34api-segment {
35 gid vpp
36}
37
38socksvr {
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +010039 socket-name %[1]s%[3]s
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010040}
41
42statseg {
43 socket-name %[1]s/var/run/vpp/stats.sock
44}
45
46plugins {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010047 plugin default { disable }
48
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010049 plugin unittest_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010050 plugin quic_plugin.so { enable }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010051 plugin af_packet_plugin.so { enable }
52 plugin hs_apps_plugin.so { enable }
53 plugin http_plugin.so { enable }
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010054}
55
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010056logging {
57 default-log-level debug
58 default-syslog-log-level debug
59}
60
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010061`
62
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010063const (
64 defaultCliSocketFilePath = "/var/run/vpp/cli.sock"
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010065 defaultApiSocketFilePath = "/var/run/vpp/api.sock"
Maros Ondrejickaa2d52622023-02-24 11:26:39 +010066 defaultLogFilePath = "/var/log/vpp/vpp.log"
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010067)
68
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010069type VppInstance struct {
Maros Ondrejicka300f70d2023-02-21 10:53:20 +010070 container *Container
71 additionalConfig Stanza
72 connection *core.Connection
73 apiChannel api.Channel
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010074}
75
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +010076func (vpp *VppInstance) Suite() *HstSuite {
77 return vpp.container.suite
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010078}
79
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010080func (vpp *VppInstance) getCliSocket() string {
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +010081 return fmt.Sprintf("%s%s", vpp.container.GetContainerWorkDir(), defaultCliSocketFilePath)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010082}
83
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010084func (vpp *VppInstance) getRunDir() string {
85 return vpp.container.GetContainerWorkDir() + "/var/run/vpp"
86}
87
88func (vpp *VppInstance) getLogDir() string {
89 return vpp.container.GetContainerWorkDir() + "/var/log/vpp"
90}
91
92func (vpp *VppInstance) getEtcDir() string {
93 return vpp.container.GetContainerWorkDir() + "/etc/vpp"
94}
95
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010096func (vpp *VppInstance) start() error {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +010097 // Create folders
98 containerWorkDir := vpp.container.GetContainerWorkDir()
99
100 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir())
101 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir())
102 vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
103
104 // Create startup.conf inside the container
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100105 configContent := fmt.Sprintf(
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100106 vppConfigTemplate,
107 containerWorkDir,
108 defaultCliSocketFilePath,
109 defaultApiSocketFilePath,
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100110 defaultLogFilePath,
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100111 )
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100112 configContent += vpp.additionalConfig.ToString()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100113 startupFileName := vpp.getEtcDir() + "/startup.conf"
114 vpp.container.createFile(startupFileName, configContent)
115
116 // Start VPP
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100117 vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100118
119 // Connect to VPP and store the connection
120 sockAddress := vpp.container.GetHostWorkDir() + defaultApiSocketFilePath
121 conn, connEv, err := govpp.AsyncConnect(
122 sockAddress,
123 core.DefaultMaxReconnectAttempts,
124 core.DefaultReconnectInterval)
125 if err != nil {
126 fmt.Println("async connect error: ", err)
127 }
128 vpp.connection = conn
129
130 // ... wait for Connected event
131 e := <-connEv
132 if e.State != core.Connected {
133 fmt.Println("connecting to VPP failed: ", e.Error)
134 }
135
136 // ... check compatibility of used messages
137 ch, err := conn.NewAPIChannel()
138 if err != nil {
139 fmt.Println("creating channel failed: ", err)
140 }
141 if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
142 fmt.Println("compatibility error: ", err)
143 }
144 if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
145 fmt.Println("compatibility error: ", err)
146 }
147 vpp.apiChannel = ch
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100148
149 return nil
150}
151
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100152func (vpp *VppInstance) vppctl(command string, arguments ...any) string {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100153 vppCliCommand := fmt.Sprintf(command, arguments...)
154 containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
155 vpp.container.name, vpp.getCliSocket(), vppCliCommand)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100156 vpp.Suite().log(containerExecCommand)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100157 output, err := exechelper.CombinedOutput(containerExecCommand)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100158 vpp.Suite().assertNil(err)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100159
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100160 return string(output)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100161}
162
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100163func (vpp *VppInstance) waitForApp(appName string, timeout int) error {
164 for i := 0; i < timeout; i++ {
165 o := vpp.vppctl("show app")
166 if strings.Contains(o, appName) {
167 return nil
168 }
169 time.Sleep(1 * time.Second)
Maros Ondrejickadb823ed2022-12-14 16:30:04 +0100170 }
Maros Ondrejicka7d7ab102023-02-14 12:56:49 +0100171 return fmt.Errorf("timeout while waiting for app '%s'", appName)
Maros Ondrejicka11a03e92022-12-01 09:56:37 +0100172}
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100173
174func (vpp *VppInstance) createAfPacket(
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100175 netInterface NetInterface,
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100176) (interface_types.InterfaceIndex, error) {
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100177 veth := netInterface.(*NetworkInterfaceVeth)
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100178
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100179 createReq := &af_packet.AfPacketCreateV2{
180 UseRandomHwAddr: true,
181 HostIfName: veth.Name(),
182 }
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100183 if veth.HwAddress() != (MacAddress{}) {
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100184 createReq.UseRandomHwAddr = false
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100185 createReq.HwAddr = veth.HwAddress()
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100186 }
187 createReply := &af_packet.AfPacketCreateV2Reply{}
188
189 if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil {
190 return 0, err
191 }
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100192 veth.SetIndex(createReply.SwIfIndex)
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100193
194 // Set to up
195 upReq := &interfaces.SwInterfaceSetFlags{
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100196 SwIfIndex: veth.Index(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100197 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
198 }
199 upReply := &interfaces.SwInterfaceSetFlagsReply{}
200
201 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
202 return 0, err
203 }
204
205 // Add address
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100206 if veth.AddressWithPrefix() == (AddressWithPrefix{}) {
207 var err error
208 var ip4Address string
Maros Ondrejicka300f70d2023-02-21 10:53:20 +0100209 if ip4Address, err = veth.addresser.NewIp4Address(veth.peerNetworkNumber); err == nil {
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100210 veth.SetAddress(ip4Address)
211 } else {
212 return 0, err
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100213 }
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100214 }
215 addressReq := &interfaces.SwInterfaceAddDelAddress{
216 IsAdd: true,
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100217 SwIfIndex: veth.Index(),
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100218 Prefix: veth.AddressWithPrefix(),
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100219 }
220 addressReply := &interfaces.SwInterfaceAddDelAddressReply{}
221
222 if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil {
223 return 0, err
224 }
225
Maros Ondrejicka2908f8c2023-02-02 08:58:04 +0100226 return veth.Index(), nil
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100227}
228
229func (vpp *VppInstance) addAppNamespace(
230 secret uint64,
231 ifx interface_types.InterfaceIndex,
232 namespaceId string,
233) error {
234 req := &session.AppNamespaceAddDelV2{
235 Secret: secret,
236 SwIfIndex: ifx,
237 NamespaceID: namespaceId,
238 }
239 reply := &session.AppNamespaceAddDelV2Reply{}
240
241 if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil {
242 return err
243 }
244
245 sessionReq := &session.SessionEnableDisable{
246 IsEnable: true,
247 }
248 sessionReply := &session.SessionEnableDisableReply{}
249
250 if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil {
251 return err
252 }
253
254 return nil
255}
256
Maros Ondrejicka7550dd22023-02-07 20:40:27 +0100257func (vpp *VppInstance) createTap(
258 hostInterfaceName string,
259 hostIp4Address IP4AddressWithPrefix,
260 vppIp4Address AddressWithPrefix,
261) error {
262 createTapReq := &tapv2.TapCreateV2{
263 HostIfNameSet: true,
264 HostIfName: hostInterfaceName,
265 HostIP4PrefixSet: true,
266 HostIP4Prefix: hostIp4Address,
267 }
268 createTapReply := &tapv2.TapCreateV2Reply{}
269
270 // Create tap interface
271 if err := vpp.apiChannel.SendRequest(createTapReq).ReceiveReply(createTapReply); err != nil {
272 return err
273 }
274
275 // Add address
276 addAddressReq := &interfaces.SwInterfaceAddDelAddress{
277 IsAdd: true,
278 SwIfIndex: createTapReply.SwIfIndex,
279 Prefix: vppIp4Address,
280 }
281 addAddressReply := &interfaces.SwInterfaceAddDelAddressReply{}
282
283 if err := vpp.apiChannel.SendRequest(addAddressReq).ReceiveReply(addAddressReply); err != nil {
284 return err
285 }
286
287 // Set interface to up
288 upReq := &interfaces.SwInterfaceSetFlags{
289 SwIfIndex: createTapReply.SwIfIndex,
290 Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
291 }
292 upReply := &interfaces.SwInterfaceSetFlagsReply{}
293
294 if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
295 return err
296 }
297
298 return nil
299}
300
Maros Ondrejickaa2d52622023-02-24 11:26:39 +0100301func (vpp *VppInstance) saveLogs() {
302 logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
303 logSource := vpp.container.GetHostWorkDir() + defaultLogFilePath
304 cmd := exec.Command("cp", logSource, logTarget)
305 vpp.Suite().T().Helper()
306 vpp.Suite().log(cmd.String())
307 cmd.Run()
308}
309
Maros Ondrejickaffa3f602023-01-26 10:07:29 +0100310func (vpp *VppInstance) disconnect() {
311 vpp.connection.Disconnect()
312 vpp.apiChannel.Close()
313}