Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
N
neo
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
2
Merge Requests
2
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Jobs
Commits
Open sidebar
Kirill Smelkov
neo
Commits
8dfe0341
Commit
8dfe0341
authored
Feb 12, 2018
by
Kirill Smelkov
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
X neonet: unexport PktBuf
not used outside neonet.
parent
889b8d42
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
40 additions
and
42 deletions
+40
-42
go/neo/neonet/connection.go
go/neo/neonet/connection.go
+14
-14
go/neo/neonet/connection_test.go
go/neo/neonet/connection_test.go
+12
-12
go/neo/neonet/pkt.go
go/neo/neonet/pkt.go
+14
-16
No files found.
go/neo/neonet/connection.go
View file @
8dfe0341
...
...
@@ -120,7 +120,7 @@ type Conn struct {
link
*
NodeLink
connId
uint32
rxq
chan
*
P
ktBuf
// received packets for this Conn go here
rxq
chan
*
p
ktBuf
// received packets for this Conn go here
rxqWrite
atomic32
// 1 while serveRecv is doing `rxq <- ...`
rxqRead
atomic32
// +1 while Conn.Recv is doing `... <- rxq`
rxdownFlag
atomic32
// 1 when RX is marked no longer operational
...
...
@@ -234,7 +234,7 @@ func newNodeLink(conn net.Conn, role LinkRole) *NodeLink {
// XXX make it per-link?
var
connPool
=
sync
.
Pool
{
New
:
func
()
interface
{}
{
return
&
Conn
{
rxq
:
make
(
chan
*
P
ktBuf
,
1
),
// NOTE non-blocking - see serveRecv XXX +buf ?
rxq
:
make
(
chan
*
p
ktBuf
,
1
),
// NOTE non-blocking - see serveRecv XXX +buf ?
txerr
:
make
(
chan
error
,
1
),
// NOTE non-blocking - see Conn.Send
txdown
:
make
(
chan
struct
{}),
// rxdown: make(chan struct{}),
...
...
@@ -681,7 +681,7 @@ func (c *Conn) errRecvShutdown() error {
}
// recvPkt receives raw packet from connection
func
(
c
*
Conn
)
recvPkt
()
(
*
P
ktBuf
,
error
)
{
func
(
c
*
Conn
)
recvPkt
()
(
*
p
ktBuf
,
error
)
{
// semantically equivalent to the following:
// (this is hot path and select is not used for performance reason)
//
...
...
@@ -693,7 +693,7 @@ func (c *Conn) recvPkt() (*PktBuf, error) {
// return pkt, nil
// }
var
pkt
*
P
ktBuf
var
pkt
*
p
ktBuf
var
err
error
c
.
rxqRead
.
Add
(
1
)
...
...
@@ -943,7 +943,7 @@ func (link *NodeLink) replyNoConn(connId uint32, errMsg proto.Msg) {
// txReq is request to transmit a packet. Result error goes back to errch
type
txReq
struct
{
pkt
*
P
ktBuf
pkt
*
p
ktBuf
errch
chan
error
}
...
...
@@ -968,12 +968,12 @@ func (c *Conn) errSendShutdown() error {
// sendPkt sends raw packet via connection.
//
// on success pkt is freed.
func
(
c
*
Conn
)
sendPkt
(
pkt
*
P
ktBuf
)
error
{
func
(
c
*
Conn
)
sendPkt
(
pkt
*
p
ktBuf
)
error
{
err
:=
c
.
sendPkt2
(
pkt
)
return
c
.
err
(
"send"
,
err
)
}
func
(
c
*
Conn
)
sendPkt2
(
pkt
*
P
ktBuf
)
error
{
func
(
c
*
Conn
)
sendPkt2
(
pkt
*
p
ktBuf
)
error
{
// connId must be set to one associated with this connection
if
pkt
.
Header
()
.
ConnId
!=
packed
.
Hton32
(
c
.
connId
)
{
panic
(
"Conn.sendPkt: connId wrong"
)
...
...
@@ -1058,7 +1058,7 @@ func (nl *NodeLink) serveSend() {
// however this adds overhead and is not needed in light mode.
// sendPktDirect sends raw packet with appropriate connection ID directly via link.
func
(
c
*
Conn
)
sendPktDirect
(
pkt
*
P
ktBuf
)
error
{
func
(
c
*
Conn
)
sendPktDirect
(
pkt
*
p
ktBuf
)
error
{
// set pkt connId associated with this connection
pkt
.
Header
()
.
ConnId
=
packed
.
Hton32
(
c
.
connId
)
...
...
@@ -1088,7 +1088,7 @@ const dumpio = false
// tx error, if any, is returned as is and is analyzed in serveSend.
//
// XXX pkt should be freed always or only on error?
func
(
nl
*
NodeLink
)
sendPkt
(
pkt
*
P
ktBuf
)
error
{
func
(
nl
*
NodeLink
)
sendPkt
(
pkt
*
p
ktBuf
)
error
{
if
dumpio
{
// XXX -> log
fmt
.
Printf
(
"%v > %v: %v
\n
"
,
nl
.
peerLink
.
LocalAddr
(),
nl
.
peerLink
.
RemoteAddr
(),
pkt
)
...
...
@@ -1106,7 +1106,7 @@ var ErrPktTooBig = errors.New("packet too big")
// recvPkt receives raw packet from peer.
//
// rx error, if any, is returned as is and is analyzed in serveRecv
func
(
nl
*
NodeLink
)
recvPkt
()
(
*
P
ktBuf
,
error
)
{
func
(
nl
*
NodeLink
)
recvPkt
()
(
*
p
ktBuf
,
error
)
{
pkt
:=
pktAlloc
(
4096
)
// len=4K but cap can be more since pkt is from pool - use all space to buffer reads
// XXX vvv -> pktAlloc() ?
...
...
@@ -1462,8 +1462,8 @@ func (c *Conn) err(op string, e error) error {
//trace:event traceMsgSendPre(l *NodeLink, connId uint32, msg proto.Msg)
// XXX do we also need traceConnSend?
// msgPack allocates
P
ktBuf and encodes msg into it.
func
msgPack
(
connId
uint32
,
msg
proto
.
Msg
)
*
P
ktBuf
{
// msgPack allocates
p
ktBuf and encodes msg into it.
func
msgPack
(
connId
uint32
,
msg
proto
.
Msg
)
*
p
ktBuf
{
l
:=
msg
.
NEOMsgEncodedLen
()
buf
:=
pktAlloc
(
proto
.
PktHeaderLen
+
l
)
...
...
@@ -1491,7 +1491,7 @@ func (c *Conn) Recv() (proto.Msg, error) {
return
msg
,
err
}
func
(
c
*
Conn
)
_Recv
(
pkt
*
P
ktBuf
)
(
proto
.
Msg
,
error
)
{
func
(
c
*
Conn
)
_Recv
(
pkt
*
p
ktBuf
)
(
proto
.
Msg
,
error
)
{
// decode packet
pkth
:=
pkt
.
Header
()
msgCode
:=
packed
.
Ntoh16
(
pkth
.
MsgCode
)
...
...
@@ -1562,7 +1562,7 @@ func (c *Conn) Expect(msgv ...proto.Msg) (which int, err error) {
return
which
,
err
}
func
(
c
*
Conn
)
_Expect
(
pkt
*
P
ktBuf
,
msgv
...
proto
.
Msg
)
(
int
,
error
)
{
func
(
c
*
Conn
)
_Expect
(
pkt
*
p
ktBuf
,
msgv
...
proto
.
Msg
)
(
int
,
error
)
{
pkth
:=
pkt
.
Header
()
msgCode
:=
packed
.
Ntoh16
(
pkth
.
MsgCode
)
...
...
go/neo/neonet/connection_test.go
View file @
8dfe0341
...
...
@@ -60,12 +60,12 @@ func xaccept(nl *NodeLink) *Conn {
return
c
}
func
xsendPkt
(
c
interface
{
sendPkt
(
*
PktBuf
)
error
},
pkt
*
P
ktBuf
)
{
func
xsendPkt
(
c
interface
{
sendPkt
(
*
pktBuf
)
error
},
pkt
*
p
ktBuf
)
{
err
:=
c
.
sendPkt
(
pkt
)
exc
.
Raiseif
(
err
)
}
func
xrecvPkt
(
c
interface
{
recvPkt
()
(
*
PktBuf
,
error
)
})
*
P
ktBuf
{
func
xrecvPkt
(
c
interface
{
recvPkt
()
(
*
pktBuf
,
error
)
})
*
p
ktBuf
{
pkt
,
err
:=
c
.
recvPkt
()
exc
.
Raiseif
(
err
)
return
pkt
...
...
@@ -103,9 +103,9 @@ func xconnError(err error) error {
return
ce
.
Err
}
// Prepare
P
ktBuf with content
func
_mkpkt
(
connid
uint32
,
msgcode
uint16
,
payload
[]
byte
)
*
P
ktBuf
{
pkt
:=
&
P
ktBuf
{
make
([]
byte
,
proto
.
PktHeaderLen
+
len
(
payload
))}
// Prepare
p
ktBuf with content
func
_mkpkt
(
connid
uint32
,
msgcode
uint16
,
payload
[]
byte
)
*
p
ktBuf
{
pkt
:=
&
p
ktBuf
{
make
([]
byte
,
proto
.
PktHeaderLen
+
len
(
payload
))}
h
:=
pkt
.
Header
()
h
.
ConnId
=
packed
.
Hton32
(
connid
)
h
.
MsgCode
=
packed
.
Hton16
(
msgcode
)
...
...
@@ -114,13 +114,13 @@ func _mkpkt(connid uint32, msgcode uint16, payload []byte) *PktBuf {
return
pkt
}
func
(
c
*
Conn
)
mkpkt
(
msgcode
uint16
,
payload
[]
byte
)
*
P
ktBuf
{
func
(
c
*
Conn
)
mkpkt
(
msgcode
uint16
,
payload
[]
byte
)
*
p
ktBuf
{
// in Conn exchange connid is automatically set by Conn.sendPkt
return
_mkpkt
(
c
.
connId
,
msgcode
,
payload
)
}
// Verify
P
ktBuf is as expected
func
xverifyPkt
(
pkt
*
P
ktBuf
,
connid
uint32
,
msgcode
uint16
,
payload
[]
byte
)
{
// Verify
p
ktBuf is as expected
func
xverifyPkt
(
pkt
*
p
ktBuf
,
connid
uint32
,
msgcode
uint16
,
payload
[]
byte
)
{
errv
:=
xerr
.
Errorv
{}
h
:=
pkt
.
Header
()
// TODO include caller location
...
...
@@ -141,8 +141,8 @@ func xverifyPkt(pkt *PktBuf, connid uint32, msgcode uint16, payload []byte) {
exc
.
Raiseif
(
errv
.
Err
()
)
}
// Verify
P
ktBuf to match expected message
func
xverifyPktMsg
(
pkt
*
P
ktBuf
,
connid
uint32
,
msg
proto
.
Msg
)
{
// Verify
p
ktBuf to match expected message
func
xverifyPktMsg
(
pkt
*
p
ktBuf
,
connid
uint32
,
msg
proto
.
Msg
)
{
data
:=
make
([]
byte
,
msg
.
NEOMsgEncodedLen
())
msg
.
NEOMsgEncode
(
data
)
xverifyPkt
(
pkt
,
connid
,
msg
.
NEOMsgCode
(),
data
)
...
...
@@ -201,7 +201,7 @@ func TestNodeLink(t *testing.T) {
tdelay
()
xclose
(
nl1
)
})
pkt
=
&
P
ktBuf
{[]
byte
(
"data"
)}
pkt
=
&
p
ktBuf
{[]
byte
(
"data"
)}
err
=
nl1
.
sendPkt
(
pkt
)
if
err
!=
io
.
ErrClosedPipe
{
t
.
Fatalf
(
"NodeLink.sendPkt() after close: err = %v"
,
err
)
...
...
@@ -259,7 +259,7 @@ func TestNodeLink(t *testing.T) {
tdelay
()
xclose
(
nl2
)
})
pkt
=
&
P
ktBuf
{[]
byte
(
"data"
)}
pkt
=
&
p
ktBuf
{[]
byte
(
"data"
)}
err
=
nl1
.
sendPkt
(
pkt
)
if
err
!=
io
.
ErrClosedPipe
{
// NOTE io.ErrClosedPipe on Write per io.Pipe
t
.
Fatalf
(
"NodeLink.sendPkt() after peer shutdown: pkt = %v err = %v"
,
pkt
,
err
)
...
...
go/neo/neonet/pkt.go
View file @
8dfe0341
...
...
@@ -32,50 +32,48 @@ import (
"lab.nexedi.com/kirr/neo/go/xcommon/packed"
)
//
P
ktBuf is a buffer with full raw packet (header + data).
//
p
ktBuf is a buffer with full raw packet (header + data).
//
// variables of type PktBuf are usually named "pkb" (packet buffer), similar to "skb" in Linux.
//
// Allocate PktBuf via pktAlloc() and free via PktBuf.Free().
type
PktBuf
struct
{
// Allocate pktBuf via pktAlloc() and free via pktBuf.Free().
type
pktBuf
struct
{
Data
[]
byte
// whole packet data including all headers
}
// Header returns pointer to packet header.
func
(
pkt
*
P
ktBuf
)
Header
()
*
proto
.
PktHeader
{
func
(
pkt
*
p
ktBuf
)
Header
()
*
proto
.
PktHeader
{
// XXX check len(Data) < PktHeader ? -> no, Data has to be allocated with cap >= PktHeaderLen
return
(
*
proto
.
PktHeader
)(
unsafe
.
Pointer
(
&
pkt
.
Data
[
0
]))
}
// Payload returns []byte representing packet payload.
func
(
pkt
*
P
ktBuf
)
Payload
()
[]
byte
{
func
(
pkt
*
p
ktBuf
)
Payload
()
[]
byte
{
return
pkt
.
Data
[
proto
.
PktHeaderLen
:
]
}
// ----
P
ktBuf freelist ----
// ----
p
ktBuf freelist ----
// pktBufPool is sync.Pool<pktBuf>
var
pktBufPool
=
sync
.
Pool
{
New
:
func
()
interface
{}
{
return
&
P
ktBuf
{
Data
:
make
([]
byte
,
0
,
4096
)}
return
&
p
ktBuf
{
Data
:
make
([]
byte
,
0
,
4096
)}
}}
// pktAlloc allocates
P
ktBuf with len=n
func
pktAlloc
(
n
int
)
*
P
ktBuf
{
pkt
:=
pktBufPool
.
Get
()
.
(
*
P
ktBuf
)
// pktAlloc allocates
p
ktBuf with len=n
func
pktAlloc
(
n
int
)
*
p
ktBuf
{
pkt
:=
pktBufPool
.
Get
()
.
(
*
p
ktBuf
)
pkt
.
Data
=
xbytes
.
Realloc
(
pkt
.
Data
,
n
)
return
pkt
}
// Free marks pkt as no longer needed.
func
(
pkt
*
P
ktBuf
)
Free
()
{
func
(
pkt
*
p
ktBuf
)
Free
()
{
pktBufPool
.
Put
(
pkt
)
}
// ----
P
ktBuf dump ----
// ----
p
ktBuf dump ----
// Strings dumps a packet in human-readable form
func
(
pkt
*
P
ktBuf
)
String
()
string
{
func
(
pkt
*
p
ktBuf
)
String
()
string
{
if
len
(
pkt
.
Data
)
<
proto
.
PktHeaderLen
{
return
fmt
.
Sprintf
(
"(! < PktHeaderLen) % x"
,
pkt
.
Data
)
}
...
...
@@ -110,7 +108,7 @@ func (pkt *PktBuf) String() string {
}
// Dump dumps a packet in raw form
func
(
pkt
*
P
ktBuf
)
Dump
()
string
{
func
(
pkt
*
p
ktBuf
)
Dump
()
string
{
if
len
(
pkt
.
Data
)
<
proto
.
PktHeaderLen
{
return
fmt
.
Sprintf
(
"(! < pktHeaderLen) % x"
,
pkt
.
Data
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment