forked from usnistgov/ndn-dpdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface.go
146 lines (128 loc) · 3.53 KB
/
face.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
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
// Package socketface implements UDP/TCP socket faces using Go net.Conn type.
package socketface
/*
#include "../../csrc/socketface/face.h"
*/
import "C"
import (
"context"
"errors"
"fmt"
"net"
"syscall"
"github.com/usnistgov/ndn-dpdk/core/logging"
"github.com/usnistgov/ndn-dpdk/dpdk/pktmbuf"
"github.com/usnistgov/ndn-dpdk/iface"
"github.com/usnistgov/ndn-dpdk/ndn/l3"
"github.com/usnistgov/ndn-dpdk/ndn/sockettransport"
"github.com/usnistgov/ndn-dpdk/ndni"
"go.uber.org/zap"
)
var logger = logging.New("socketface")
// New creates a socket face.
func New(loc Locator) (iface.Face, error) {
if e := loc.Validate(); e != nil {
return nil, e
}
var cfg Config
if loc.Config != nil {
cfg = *loc.Config
}
if cfg.MTU > 0 && ndni.PacketMempool.Config().Dataroom < pktmbuf.DefaultHeadroom+cfg.MTU {
return nil, errors.New("PacketMempool dataroom is too small for requested MTU")
}
transport, e := sockettransport.Dial(loc.Network, loc.Local, loc.Remote, cfg.transportConfig())
if e != nil {
return nil, e
}
return Wrap(transport, cfg)
}
// Wrap wraps a sockettransport.Transport to a socket face.
func Wrap(transport sockettransport.Transport, cfg Config) (iface.Face, error) {
_, isUDP := transport.Conn().(*net.UDPConn)
rxi := &rxConnsImpl
if isUDP && !gCfg.RxEpoll.Disabled {
rxi = &rxEpollImpl
}
txi := &txConnImpl
if isUDP && !gCfg.TxSyscall.Disabled {
txi = &txSyscallImpl
}
face := &socketFace{
transport: transport,
}
return iface.New(iface.NewParams{
Config: cfg.Config.WithMaxMTU(ndni.PacketMempool.Config().Dataroom - pktmbuf.DefaultHeadroom),
Socket: gCfg.numaSocket(),
SizeofPriv: C.sizeof_SocketFacePriv,
Init: func(f iface.Face) (res iface.InitResult, e error) {
face.Face = f
id, faceC := face.ID(), (*C.Face)(face.Ptr())
face.logger = logger.With(id.ZapField("id"))
face.priv = (*C.SocketFacePriv)(C.Face_GetPriv(faceC))
*face.priv = C.SocketFacePriv{
fd: -1,
}
res.Face = face
res.TxBurst = txi.txBurst
return
},
Start: func() error {
if e := rxi.start(face); e != nil {
face.transport.Close()
return e
}
if txi.start != nil {
txi.start(face)
}
iface.ActivateTxFace(face)
face.cancelStateChangeHandler = face.transport.OnStateChange(func(st l3.TransportState) {
face.SetDown(st != l3.TransportUp)
})
face.logger.Info("face started", zap.Stringer("rx-impl", rxi), zap.Stringer("tx-impl", txi))
return nil
},
Locator: func() iface.Locator {
conn := face.transport.Conn()
laddr, raddr := conn.LocalAddr(), conn.RemoteAddr()
var loc Locator
loc.Network = raddr.Network()
loc.Remote = raddr.String()
if laddr != nil {
loc.Local = laddr.String()
}
return loc
},
Stop: func() error {
face.cancelStateChangeHandler()
face.transport.Close()
iface.DeactivateTxFace(face)
return nil
},
Close: func() error {
return nil
},
ExCounters: func() any {
return face.transport.Counters()
},
})
}
// socketFace is a face using socket as transport.
type socketFace struct {
iface.Face
transport sockettransport.Transport
logger *zap.Logger
priv *C.SocketFacePriv
cancelStateChangeHandler func()
}
func (face *socketFace) rawControl(cb func(ctx context.Context, fd int) error) error {
raw, e := face.transport.Conn().(syscall.Conn).SyscallConn()
if e != nil {
return fmt.Errorf("SyscallConn: %w", e)
}
var e1 error
e0 := raw.Control(func(fd uintptr) {
e1 = cb(face.transport.Context(), int(fd))
})
return errors.Join(e0, e1)
}