-
Notifications
You must be signed in to change notification settings - Fork 26
/
crypto.go
144 lines (122 loc) · 3.55 KB
/
crypto.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
package fwdp
/*
#include "../../csrc/fwdp/crypto.h"
*/
import "C"
import (
"errors"
"fmt"
"unsafe"
"github.com/usnistgov/ndn-dpdk/core/cptr"
"github.com/usnistgov/ndn-dpdk/dpdk/cryptodev"
"github.com/usnistgov/ndn-dpdk/dpdk/eal"
"github.com/usnistgov/ndn-dpdk/dpdk/ealthread"
"github.com/usnistgov/ndn-dpdk/dpdk/ringbuffer"
"github.com/usnistgov/ndn-dpdk/iface"
"github.com/usnistgov/ndn-dpdk/ndni"
)
// CryptoConfig contains crypto helper thread configuration.
type CryptoConfig struct {
InputCapacity int `json:"inputCapacity,omitempty"`
OpPoolCapacity int `json:"opPoolCapacity,omitempty"`
}
func (cfg *CryptoConfig) applyDefaults() {
cfg.InputCapacity = ringbuffer.AlignCapacity(cfg.InputCapacity, iface.MaxBurstSize)
if cfg.OpPoolCapacity <= 0 {
cfg.OpPoolCapacity = 1023
}
}
// Crypto represents a crypto helper thread.
type Crypto struct {
ealthread.ThreadWithCtrl
id int
c *C.FwCrypto
}
var (
_ ealthread.ThreadWithRole = (*Crypto)(nil)
_ ealthread.ThreadWithLoadStat = (*Crypto)(nil)
_ DispatchThread = (*Crypto)(nil)
)
// DispatchThreadID implements DispatchThread interface.
func (fwc *Crypto) DispatchThreadID() int {
return fwc.id
}
func (fwc *Crypto) String() string {
return fmt.Sprintf("crypto%d", fwc.id)
}
// DemuxOf implements DispatchThread interface.
func (fwc *Crypto) DemuxOf(t ndni.PktType) *iface.InputDemux {
if t == ndni.PktData {
return iface.InputDemuxFromPtr(unsafe.Pointer(&fwc.c.output))
}
return nil
}
// Close stops and releases the thread.
func (fwc *Crypto) Close() error {
fwc.Stop()
eal.Free(fwc.c)
return nil
}
// ThreadRole implements ealthread.ThreadWithRole interface.
func (Crypto) ThreadRole() string {
return RoleCrypto
}
// newCrypto creates a crypto helper thread.
func newCrypto(id int, lc eal.LCore, demuxPrep *demuxPreparer) (fwc *Crypto, e error) {
socket := lc.NumaSocket()
fwc = &Crypto{
id: id,
c: eal.ZmallocAligned[C.FwCrypto]("FwCrypto", C.sizeof_FwCrypto, 1, socket),
}
fwc.ThreadWithCtrl = ealthread.NewThreadWithCtrl(
cptr.Func0.C(C.FwCrypto_Run, unsafe.Pointer(fwc.c)),
unsafe.Pointer(&fwc.c.ctrl),
)
fwc.SetLCore(lc)
demuxPrep.Prepare(fwc, socket)
return fwc, nil
}
// CryptoShared contains per NUMA socket shared resources for crypto helper threads.
type CryptoShared struct {
input *ringbuffer.Ring
dev *cryptodev.CryptoDev
}
// AssignTo assigns shared resources to crypto helper threads.
func (fwcsh *CryptoShared) AssignTo(fwcs []*Crypto) {
qp := fwcsh.dev.QueuePairs()
for i, fwc := range fwcs {
fwc.c.input = (*C.struct_rte_ring)(fwcsh.input.Ptr())
qp[i].CopyToC(unsafe.Pointer(&fwc.c.cqp))
}
}
// ConnectTo connects forwarding thread to crypto input queue.
func (fwcsh *CryptoShared) ConnectTo(fwd *Fwd) {
fwd.c.cryptoHelper = (*C.struct_rte_ring)(fwcsh.input.Ptr())
}
// Close deletes resources.
func (fwcsh *CryptoShared) Close() error {
return errors.Join(
fwcsh.dev.Close(),
fwcsh.input.Close(),
)
}
func newCryptoShared(cfg CryptoConfig, socket eal.NumaSocket, count int) (fwcsh *CryptoShared, e error) {
cfg.applyDefaults()
fwcsh = &CryptoShared{}
ringConsumerMode := ringbuffer.ConsumerSingle
if count > 1 {
ringConsumerMode = ringbuffer.ConsumerMulti
}
fwcsh.input, e = ringbuffer.New(cfg.InputCapacity, socket, ringbuffer.ProducerMulti, ringConsumerMode)
if e != nil {
return nil, fmt.Errorf("ringbuffer.New: %w", e)
}
var vcfg cryptodev.VDevConfig
vcfg.Socket = socket
vcfg.NQueuePairs = count
fwcsh.dev, e = cryptodev.CreateVDev(vcfg)
if e != nil {
return nil, fmt.Errorf("cryptodev.CreateVDev: %w", e)
}
return fwcsh, nil
}