-
Notifications
You must be signed in to change notification settings - Fork 26
/
intface.go
87 lines (73 loc) · 1.87 KB
/
intface.go
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
// Package intface implements an internal face for internal applications.
// It bridges a iface.Face (socket face) on DPDK side and an ndn.L3Face on application side.
package intface
import (
"github.com/usnistgov/ndn-dpdk/iface"
"github.com/usnistgov/ndn-dpdk/iface/socketface"
"github.com/usnistgov/ndn-dpdk/ndn"
"github.com/usnistgov/ndn-dpdk/ndn/l3"
"github.com/usnistgov/ndn-dpdk/ndn/sockettransport"
)
var faceByID = map[iface.ID]*IntFace{}
func init() {
iface.OnFaceClosing(func(id iface.ID) {
f := faceByID[id]
if f != nil {
close(f.Tx)
delete(faceByID, id)
}
})
}
// IntFace is an iface.Face and a ndn.L3Face connected together.
type IntFace struct {
// D is the face on DPDK side.
// Packets sent on D are received on A.
D iface.Face
// ID is the ID on DPDK side.
ID iface.ID
// A is the face on application side.
// Packets sent on A are received by D.
A l3.Face
// Rx is application side RX channel.
// It's equivalent to A.Rx().
Rx <-chan *ndn.Packet
// Tx is application side TX channel.
// It's equivalent to A.Tx().
Tx chan<- ndn.L3Packet
}
// New creates an IntFace.
func New(cfg socketface.Config) (f *IntFace, e error) {
trA, trD, e := sockettransport.Pipe(sockettransport.Config{
MTU: cfg.MTU,
})
if e != nil {
return nil, e
}
f = &IntFace{}
if f.A, e = l3.NewFace(trA, l3.FaceConfig{}); e != nil {
return nil, e
}
if f.D, e = socketface.Wrap(trD, cfg); e != nil {
return nil, e
}
f.ID = f.D.ID()
f.Rx = f.A.Rx()
f.Tx = f.A.Tx()
faceByID[f.ID] = f
return f, nil
}
// Must panics on error.
func Must(f *IntFace, e error) *IntFace {
if e != nil {
panic(e)
}
return f
}
// MustNew creates an IntFace with default settings, and panics on error.
func MustNew() *IntFace {
return Must(New(socketface.Config{}))
}
// SetDown changes up/down state on the DPDK side.
func (f *IntFace) SetDown(isDown bool) {
f.D.SetDown(isDown)
}