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