|
| 1 | +// Copyright 2020 ETH Zurich |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// cbrtester application |
| 16 | +// For more documentation on the application see: |
| 17 | +// https://github.com/netsec-ethz/scion-apps/blob/master/README.md |
| 18 | +// https://github.com/netsec-ethz/scion-apps/blob/master/bwtester/README.md |
| 19 | + |
| 20 | +package main |
| 21 | + |
| 22 | +import ( |
| 23 | + "flag" |
| 24 | + "fmt" |
| 25 | + "os" |
| 26 | + "time" |
| 27 | + |
| 28 | + "github.com/scionproto/scion/go/lib/serrors" |
| 29 | + "github.com/scionproto/scion/go/lib/snet" |
| 30 | + |
| 31 | + "github.com/netsec-ethz/scion-apps/pkg/appnet" |
| 32 | +) |
| 33 | + |
| 34 | +func main() { |
| 35 | + port := flag.Uint("port", 0, "[Server] Local port to listen on") |
| 36 | + timeout := flag.Uint("timeout", 100, "[Server] Size of gap between subsequent packets that is considered a freeze (in ms)") |
| 37 | + |
| 38 | + bw := flag.Uint("bw", 1024*1024, "[Client] Rate to send at (in bps)") |
| 39 | + payload := flag.Uint("payload", 100, "[Client] Size of each packet in bytes") |
| 40 | + interactive := flag.Bool("interactive", false, "[Client] Select the path interactively") |
| 41 | + remoteAddr := flag.String("remote", "", "[Client] Remote (i.e. the server's) SCION Address (e.g. 17-ffaa:1:1,[127.0.0.1]:12345)") |
| 42 | + |
| 43 | + flag.Parse() |
| 44 | + |
| 45 | + if (*port > 0) == (len(*remoteAddr) > 0) { |
| 46 | + fmt.Println("Either specify -port for server or -remote for client") |
| 47 | + os.Exit(1) |
| 48 | + } |
| 49 | + |
| 50 | + var err error |
| 51 | + if *port > 0 { |
| 52 | + err = runServer(int(*port), int64(*timeout)) |
| 53 | + } else { |
| 54 | + err = runClient(*remoteAddr, int(*bw), int(*payload), *interactive) |
| 55 | + } |
| 56 | + if err != nil { |
| 57 | + fmt.Println("err", err) |
| 58 | + os.Exit(1) |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func runClient(address string, desiredThroughput int, payloadSize int, interactive bool) error { |
| 63 | + addr, err := appnet.ResolveUDPAddr(address) |
| 64 | + if err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + var path snet.Path |
| 68 | + if interactive { |
| 69 | + path, err = appnet.ChoosePathInteractive(addr.IA) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + appnet.SetPath(addr, path) |
| 74 | + } else { |
| 75 | + paths, err := appnet.QueryPaths(addr.IA) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + path = paths[0] |
| 80 | + } |
| 81 | + |
| 82 | + conn, err := appnet.DialAddr(addr) |
| 83 | + if err != nil { |
| 84 | + return err |
| 85 | + } |
| 86 | + defer conn.Close() |
| 87 | + fmt.Printf("Running client using payload size %v and rate %v via %v\n", payloadSize, desiredThroughput, path) |
| 88 | + |
| 89 | + numberOfPacketsPerSecond := float64(desiredThroughput) / 8. / float64(payloadSize) |
| 90 | + interval := time.Duration(float64(time.Second) / numberOfPacketsPerSecond) |
| 91 | + |
| 92 | + fmt.Printf("Sending %v packets per second with a gap of %v\n", numberOfPacketsPerSecond, interval) |
| 93 | + |
| 94 | + buffer := make([]byte, payloadSize) |
| 95 | + copy(buffer, []byte("cbrtester")) // allow easy identification of packets |
| 96 | + |
| 97 | + for { |
| 98 | + before := time.Now() |
| 99 | + _, err = conn.Write(buffer) |
| 100 | + if err != nil { |
| 101 | + fmt.Println("error writing", err) |
| 102 | + } |
| 103 | + took := time.Since(before) |
| 104 | + |
| 105 | + sleepAmount := interval - took |
| 106 | + |
| 107 | + if sleepAmount > 0 { |
| 108 | + time.Sleep(sleepAmount) |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func runServer(port int, timeout int64) error { |
| 114 | + listener, err := appnet.ListenPort(uint16(port)) |
| 115 | + if err != nil { |
| 116 | + return serrors.WrapStr("can't listen:", err) |
| 117 | + } |
| 118 | + defer listener.Close() |
| 119 | + fmt.Printf("%v,%v\n", appnet.DefNetwork().IA, listener.LocalAddr()) |
| 120 | + |
| 121 | + lastReceived := time.Now().Add(999 * time.Hour) // avoids elapsed > timeout in the 1st iter |
| 122 | + |
| 123 | + buffer := make([]byte, 16*1024) |
| 124 | + |
| 125 | + start := time.Now() |
| 126 | + started := false |
| 127 | + totalByteCount := 0 |
| 128 | + instaByteCount := 0 |
| 129 | + lastBWReport := time.Now() |
| 130 | + for { |
| 131 | + count, _, err := listener.ReadFrom(buffer) |
| 132 | + if err != nil { |
| 133 | + fmt.Println(err) |
| 134 | + continue |
| 135 | + } |
| 136 | + |
| 137 | + totalByteCount += count |
| 138 | + instaByteCount += count |
| 139 | + |
| 140 | + if !started { |
| 141 | + start = time.Now() |
| 142 | + started = true |
| 143 | + } |
| 144 | + |
| 145 | + elapsed := time.Since(lastReceived).Milliseconds() |
| 146 | + if elapsed > timeout { |
| 147 | + fmt.Printf("Freeze: %.3f s\n", float64(elapsed)/1000.0) |
| 148 | + } |
| 149 | + |
| 150 | + if time.Since(lastBWReport) > 5*time.Second { |
| 151 | + fmt.Printf("ave. bandwidth: %.3f kbps, insta. bandwidth: %.3f\n", |
| 152 | + float64(totalByteCount)*8./1024./time.Since(start).Seconds(), |
| 153 | + float64(instaByteCount)*8./1024./float64(time.Since(lastBWReport).Seconds())) |
| 154 | + |
| 155 | + lastBWReport = time.Now() |
| 156 | + instaByteCount = 0 |
| 157 | + } |
| 158 | + lastReceived = time.Now() |
| 159 | + } |
| 160 | +} |
0 commit comments