blob: f3b7ffa7578ca1336dbd65addc3b4043b5c05e4f [file] [log] [blame]
Filip Tehlar229f5fc2022-08-09 14:44:47 +00001package main
2
3import (
Filip Tehlar229f5fc2022-08-09 14:44:47 +00004 "encoding/json"
5 "errors"
6 "fmt"
7 "io"
8 "io/ioutil"
9 "os"
10 "os/exec"
11 "strings"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000012 "time"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000013)
14
Maros Ondrejicka11a03e92022-12-01 09:56:37 +010015// TODO remove `configTemplate` once its usage has been replaced everywhere with VppConfig
Filip Tehlar229f5fc2022-08-09 14:44:47 +000016const configTemplate = `unix {
17 nodaemon
18 log %[1]s/var/log/vpp/vpp.log
19 full-coredump
20 cli-listen %[1]s/var/run/vpp/cli.sock
21 runtime-dir %[1]s/var/run
22 gid vpp
23}
24
25api-trace {
26 on
27}
28
29api-segment {
30 gid vpp
31}
32
33socksvr {
34 socket-name %[1]s/var/run/vpp/api.sock
35}
36
37statseg {
38 socket-name %[1]s/var/run/vpp/stats.sock
39}
40
41plugins {
Filip Tehlar058237e2023-01-11 10:58:58 +010042 plugin default { disable }
43
44 plugin unittest_plugin.so { enable }
45 plugin quic_plugin.so { enable }
46 plugin af_packet_plugin.so { enable }
47 plugin hs_apps_plugin.so { enable }
48 plugin http_plugin.so { enable }
Filip Tehlar229f5fc2022-08-09 14:44:47 +000049}
50
51`
52
Maros Ondrejicka0db15752022-10-12 22:58:01 +020053const vclTemplate = `vcl {
54 app-socket-api %[1]s
55 app-scope-global
56 app-scope-local
57 namespace-id %[2]s
58 namespace-secret %[2]s
59 use-mq-eventfd
60}
61`
62
Maros Ondrejickadb823ed2022-12-14 16:30:04 +010063const NetworkTopologyDir string = "topo-network/"
64const ContainerTopologyDir string = "topo-containers/"
Filip Tehlar229f5fc2022-08-09 14:44:47 +000065
66type Stanza struct {
67 content string
68 pad int
69}
70
71type ActionResult struct {
72 Err error
73 Desc string
74 ErrOutput string
75 StdOutput string
76}
77
78type JsonResult struct {
79 Code int
80 Desc string
81 ErrOutput string
82 StdOutput string
83}
84
85func StartServerApp(running chan error, done chan struct{}, env []string) {
86 cmd := exec.Command("iperf3", "-4", "-s")
87 if env != nil {
88 cmd.Env = env
89 }
90 err := cmd.Start()
91 if err != nil {
92 msg := fmt.Errorf("failed to start iperf server: %v", err)
93 running <- msg
94 return
95 }
96 running <- nil
97 <-done
98 cmd.Process.Kill()
99}
100
Maros Ondrejicka87531802022-12-19 20:35:27 +0100101func StartClientApp(env []string, clnCh chan error, clnRes chan string) {
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000102 defer func() {
103 clnCh <- nil
104 }()
105
106 nTries := 0
107
108 for {
109 cmd := exec.Command("iperf3", "-c", "10.10.10.1", "-u", "-l", "1460", "-b", "10g")
110 if env != nil {
111 cmd.Env = env
112 }
113 o, err := cmd.CombinedOutput()
114 if err != nil {
115 if nTries > 5 {
116 clnCh <- fmt.Errorf("failed to start client app '%s'.\n%s", err, o)
117 return
118 }
119 time.Sleep(1 * time.Second)
120 nTries++
121 continue
122 } else {
Maros Ondrejicka87531802022-12-19 20:35:27 +0100123 clnRes <- fmt.Sprintf("Client output: %s", o)
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000124 }
125 break
126 }
127}
128
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000129func waitForSyncFile(fname string) (*JsonResult, error) {
130 var res JsonResult
131
Maros Ondrejicka0db15752022-10-12 22:58:01 +0200132 for i := 0; i < 360; i++ {
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000133 f, err := os.Open(fname)
134 if err == nil {
135 defer f.Close()
136
137 data, err := ioutil.ReadFile(fname)
138 if err != nil {
139 return nil, fmt.Errorf("read error: %v", err)
140 }
141 err = json.Unmarshal(data, &res)
142 if err != nil {
143 return nil, fmt.Errorf("json unmarshal error: %v", err)
144 }
145 return &res, nil
146 }
147 time.Sleep(1 * time.Second)
148 }
149 return nil, fmt.Errorf("no sync file found")
150}
151
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000152func assertFileSize(f1, f2 string) error {
153 fi1, err := os.Stat(f1)
154 if err != nil {
155 return err
156 }
157
158 fi2, err1 := os.Stat(f2)
159 if err1 != nil {
160 return err1
161 }
162
163 if fi1.Size() != fi2.Size() {
164 return fmt.Errorf("file sizes differ (%d vs %d)", fi1.Size(), fi2.Size())
165 }
166 return nil
167}
168
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000169func startHttpServer(running chan struct{}, done chan struct{}, addressPort, netNs string) {
170 cmd := NewCommand([]string{"./http_server", addressPort}, netNs)
171 err := cmd.Start()
172 if err != nil {
173 fmt.Println("Failed to start http server")
174 return
175 }
176 running <- struct{}{}
177 <-done
178 cmd.Process.Kill()
179}
180
Filip Tehlarc204c872022-12-21 08:59:16 +0100181func startWget(finished chan error, server_ip, port, query, netNs string) {
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000182 defer func() {
183 finished <- errors.New("wget error")
184 }()
185
Filip Tehlarc204c872022-12-21 08:59:16 +0100186 cmd := NewCommand([]string{"wget", "--tries=5", "-q", "-O", "/dev/null", server_ip + ":" + port + "/" + query},
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000187 netNs)
188 o, err := cmd.CombinedOutput()
189 if err != nil {
Filip Tehlarc204c872022-12-21 08:59:16 +0100190 finished <- fmt.Errorf("wget error: '%v\n\n%s'", err, o)
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000191 return
192 }
Filip Tehlar229f5fc2022-08-09 14:44:47 +0000193 finished <- nil
194}
195
196func (c *Stanza) NewStanza(name string) *Stanza {
197 c.Append("\n" + name + " {")
198 c.pad += 2
199 return c
200}
201
202func (c *Stanza) Append(name string) *Stanza {
203 c.content += strings.Repeat(" ", c.pad)
204 c.content += name + "\n"
205 return c
206}
207
208func (c *Stanza) Close() *Stanza {
209 c.content += "}\n"
210 c.pad -= 2
211 return c
212}
213
214func (s *Stanza) ToString() string {
215 return s.content
216}
217
218func (s *Stanza) SaveToFile(fileName string) error {
219 fo, err := os.Create(fileName)
220 if err != nil {
221 return err
222 }
223 defer fo.Close()
224
225 _, err = io.Copy(fo, strings.NewReader(s.content))
226 return err
227}