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