/usr/share/gocode/src/github.com/linuxkit/virtsock/cmd/sock_stress/stream_echo.go is in golang-github-linuxkit-virtsock-dev 0.0~git20170720.0.0416e3d-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | package main
// This implements a echo server over a SOCK_STREAM connection. The
// client sends random data and random amount of data to a server
// which echos it back. The client computes MD5 checksums on the data
// sent and received.
import (
"crypto/md5"
"fmt"
"io"
"log"
"math/rand"
"net"
"time"
)
type streamEcho struct{}
func newStreamEchoTest() streamEcho {
return streamEcho{}
}
func (t streamEcho) Server(s Sock) {
l := s.Listen()
defer l.Close()
connid := 0
for {
conn, err := l.Accept()
if err != nil {
log.Fatalf("Accept(): %s\n", err)
}
prDebug("[%05d] accept(): %s -> %s \n", connid, conn.RemoteAddr(), conn.LocalAddr())
go t.handleRequest(conn, connid)
connid++
}
}
func (t streamEcho) handleRequest(c net.Conn, connid int) {
defer func() {
prDebug("[%05d] Closing\n", connid)
err := c.Close()
if err != nil {
prError("[%05d] Close(): %s\n", connid, err)
}
}()
start := time.Now()
n, err := io.Copy(c, c)
if err != nil {
prError("[%05d] Copy(): %s", connid, err)
return
}
diffTime := time.Since(start)
prInfo("[%05d] ECHOED: %10d bytes in %10.4f ms\n", connid, n, diffTime.Seconds()*1000)
}
func (t streamEcho) Client(s Sock, conid int) {
c, err := s.Dial(conid)
if err != nil {
prError("[%05d] Failed to Dial: %s %s\n", conid, s, err)
return
}
defer c.Close()
// Create buffer with random data and random length.
// Make sure the buffer is not zero-length
buflen := minDataLen
if maxDataLen > minDataLen {
buflen += rand.Intn(maxDataLen - minDataLen + 1)
}
hash0 := md5.New()
var txTime, rxTime time.Duration
start := time.Now()
w := make(chan int)
go func() {
total := 0
remaining := buflen
for remaining > 0 {
batch := 0
bufsize := minBufLen
if maxBufLen > minBufLen {
bufsize += rand.Intn(maxBufLen - minBufLen + 1)
}
if remaining > bufsize {
batch = bufsize
} else {
batch = remaining
}
txbuf := randBuf(batch)
hash0.Write(txbuf)
e := make(chan error, 0)
go func() {
l, err := c.Write(txbuf)
if err != nil {
e <- err
} else if l != batch {
e <- fmt.Errorf("Sent incorrect length: expected %d, got %d", batch, l)
} else {
e <- nil
}
}()
select {
case err := <-e:
if err != nil {
prError("[%05d] Failed to send: %s\n", conid, err)
break
}
case <-time.After(ioTimeout):
prError("[%05d] Send timed out\n", conid)
break
}
total += batch
remaining -= batch
}
// Tell the other end that we are done
c.CloseWrite()
txTime = time.Since(start)
w <- total
}()
hash1 := md5.New()
totalReceived := 0
remaining := buflen
for remaining > 0 {
batch := 0
bufsize := minBufLen
if maxBufLen > minBufLen {
bufsize += rand.Intn(maxBufLen - minBufLen + 1)
}
if remaining > bufsize {
batch = bufsize
} else {
batch = remaining
}
rxbuf := make([]byte, batch)
e := make(chan error, 0)
go func() {
l, err := io.ReadFull(c, rxbuf)
if err != nil {
e <- err
} else if l != batch {
e <- fmt.Errorf("Received incorrect length, expected %d, got %d", batch, l)
} else {
e <- nil
}
}()
select {
case err := <-e:
if err != nil {
prError("[%05d] Failed to receive: %s\n", conid, err)
break
}
case <-time.After(ioTimeout):
prError("[%05d] Receive timed out\n", conid)
break
}
hash1.Write(rxbuf)
remaining -= batch
totalReceived += batch
}
rxTime = time.Since(start)
totalSent := <-w
csum0 := md5Hash(hash0)
prDebug("[%05d] TX: %d bytes, md5=%02x in %s\n", conid, totalSent, csum0, txTime)
csum1 := md5Hash(hash1)
prInfo("[%05d] TX/RX: %10d bytes in %10.4f ms\n", conid, totalReceived, rxTime.Seconds()*1000)
if csum0 != csum1 {
prError("[%05d] Checksums don't match", conid)
}
}
|