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 { |
Filip Tehlar | 058237e | 2023-01-11 10:58:58 +0100 | [diff] [blame] | 42 | 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 Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | ` |
| 52 | |
Maros Ondrejicka | 0db1575 | 2022-10-12 22:58:01 +0200 | [diff] [blame] | 53 | const 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 Ondrejicka | db823ed | 2022-12-14 16:30:04 +0100 | [diff] [blame] | 63 | const NetworkTopologyDir string = "topo-network/" |
| 64 | const ContainerTopologyDir string = "topo-containers/" |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 65 | |
| 66 | type Stanza struct { |
| 67 | content string |
| 68 | pad int |
| 69 | } |
| 70 | |
| 71 | type ActionResult struct { |
| 72 | Err error |
| 73 | Desc string |
| 74 | ErrOutput string |
| 75 | StdOutput string |
| 76 | } |
| 77 | |
| 78 | type JsonResult struct { |
| 79 | Code int |
| 80 | Desc string |
| 81 | ErrOutput string |
| 82 | StdOutput string |
| 83 | } |
| 84 | |
| 85 | func 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 Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 101 | func StartClientApp(env []string, clnCh chan error, clnRes chan string) { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 102 | 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 Ondrejicka | 8753180 | 2022-12-19 20:35:27 +0100 | [diff] [blame] | 123 | clnRes <- fmt.Sprintf("Client output: %s", o) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 124 | } |
| 125 | break |
| 126 | } |
| 127 | } |
| 128 | |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 129 | func waitForSyncFile(fname string) (*JsonResult, error) { |
| 130 | var res JsonResult |
| 131 | |
Maros Ondrejicka | 0db1575 | 2022-10-12 22:58:01 +0200 | [diff] [blame] | 132 | for i := 0; i < 360; i++ { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 133 | 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 Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 152 | func 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 Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 169 | func 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 Tehlar | c204c87 | 2022-12-21 08:59:16 +0100 | [diff] [blame] | 181 | func startWget(finished chan error, server_ip, port, query, netNs string) { |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 182 | defer func() { |
| 183 | finished <- errors.New("wget error") |
| 184 | }() |
| 185 | |
Filip Tehlar | dcca9e7 | 2023-01-12 16:21:55 +0100 | [diff] [blame] | 186 | cmd := NewCommand([]string{"wget", "--no-proxy", "--tries=5", "-q", "-O", "/dev/null", server_ip + ":" + port + "/" + query}, |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 187 | netNs) |
| 188 | o, err := cmd.CombinedOutput() |
| 189 | if err != nil { |
Filip Tehlar | c204c87 | 2022-12-21 08:59:16 +0100 | [diff] [blame] | 190 | finished <- fmt.Errorf("wget error: '%v\n\n%s'", err, o) |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 191 | return |
| 192 | } |
Filip Tehlar | 229f5fc | 2022-08-09 14:44:47 +0000 | [diff] [blame] | 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 | } |