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 | |
| 99 | func StartClientApp(env []string, clnCh chan error) { |
| 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 { |
| 121 | fmt.Printf("Client output: %s", o) |
| 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 { |
| 189 | fmt.Printf("wget error: '%s'.\n%s", err, o) |
| 190 | return |
| 191 | } |
| 192 | fmt.Printf("Client output: %s", o) |
| 193 | finished <- nil |
| 194 | } |
| 195 | |
| 196 | func (c *Stanza) NewStanza(name string) *Stanza { |
| 197 | c.Append("\n" + name + " {") |
| 198 | c.pad += 2 |
| 199 | return c |
| 200 | } |
| 201 | |
| 202 | func (c *Stanza) Append(name string) *Stanza { |
| 203 | c.content += strings.Repeat(" ", c.pad) |
| 204 | c.content += name + "\n" |
| 205 | return c |
| 206 | } |
| 207 | |
| 208 | func (c *Stanza) Close() *Stanza { |
| 209 | c.content += "}\n" |
| 210 | c.pad -= 2 |
| 211 | return c |
| 212 | } |
| 213 | |
| 214 | func (s *Stanza) ToString() string { |
| 215 | return s.content |
| 216 | } |
| 217 | |
| 218 | func (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 | } |