forked from usnistgov/ndn-dpdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface.go
219 lines (186 loc) · 5.49 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package pdump
/*
#include "../../csrc/pdump/format.h"
#include "../../csrc/pdump/source.h"
#include "../../csrc/iface/face.h"
*/
import "C"
import (
"errors"
"fmt"
"math"
"sync"
"unsafe"
"slices"
"github.com/gopacket/gopacket/layers"
"github.com/gopacket/gopacket/pcapgo"
"github.com/usnistgov/ndn-dpdk/core/pcg32"
"github.com/usnistgov/ndn-dpdk/core/urcu"
"github.com/usnistgov/ndn-dpdk/dpdk/eal"
"github.com/usnistgov/ndn-dpdk/dpdk/pktmbuf"
"github.com/usnistgov/ndn-dpdk/iface"
"github.com/usnistgov/ndn-dpdk/ndn"
"github.com/usnistgov/ndn-dpdk/ndni"
"go.uber.org/zap"
)
// Direction indicates traffic direction.
type Direction string
// Direction values.
const (
DirIncoming Direction = "RX"
DirOutgoing Direction = "TX"
)
var dirImpls = map[Direction]struct {
sllType C.rte_be16_t
getRef func(faceC *C.Face) *C.PdumpSourceRef
}{
DirIncoming: {
C.SLLIncoming,
func(faceC *C.Face) *C.PdumpSourceRef { return &faceC.impl.rxPdump },
},
DirOutgoing: {
C.SLLOutgoing,
func(faceC *C.Face) *C.PdumpSourceRef { return &faceC.impl.txPdump },
},
}
type faceDir struct {
face iface.ID
dir Direction
}
func (fd faceDir) String() string {
return fmt.Sprintf("%d-%s", fd.face, fd.dir)
}
func parseFaceDir(input string) (fd faceDir, e error) {
_, e = fmt.Sscanf(input, "%d-%s", &fd.face, &fd.dir)
return
}
var (
faceSources = map[faceDir]*FaceSource{}
faceClosingOnce sync.Once
)
func handleFaceClosing(id iface.ID) {
sourcesMutex.Lock()
defer sourcesMutex.Unlock()
for dir := range dirImpls {
s, ok := faceSources[faceDir{id, dir}]
if !ok {
continue
}
s.closeImpl()
}
}
// FaceConfig contains FaceSource configuration.
type FaceConfig struct {
Writer *Writer
Face iface.Face
Dir Direction
Names []NameFilterEntry
}
func (cfg *FaceConfig) validate() error {
errs := []error{}
if cfg.Writer == nil {
errs = append(errs, errors.New("writer not found"))
}
if cfg.Face == nil {
errs = append(errs, errors.New("face not found"))
}
if _, ok := dirImpls[cfg.Dir]; !ok {
errs = append(errs, errors.New("invalid traffic direction"))
}
if n := len(cfg.Names); n == 0 || n > MaxNames {
errs = append(errs, fmt.Errorf("must have between 1 and %d name filters", MaxNames))
}
for _, nf := range cfg.Names {
if !(nf.SampleProbability >= 0.0 && nf.SampleProbability <= 1.0) {
errs = append(errs, fmt.Errorf("sample probability of %s must be between 0.0 and 1.0", nf.Name))
}
}
return errors.Join(errs...)
}
// NameFilterEntry matches a name prefix and specifies its sample rate.
// An empty name matches all packets.
type NameFilterEntry struct {
Name ndn.Name `json:"name" gqldesc:"NDN name prefix."`
SampleProbability float64 `json:"sampleProbability" gqldesc:"Sample probability between 0.0 and 1.0." gqldflt:"1.0"`
}
// FaceSource is a packet dump source attached to a face on a single direction.
type FaceSource struct {
FaceConfig
key faceDir
logger *zap.Logger
c *C.PdumpFaceSource
}
func (s *FaceSource) setRef(expected, newPtr *C.PdumpSource) {
ref := dirImpls[s.Dir].getRef((*C.Face)(s.Face.Ptr()))
setSourceRef(ref, expected, newPtr)
}
// Close detaches the dump source.
func (s *FaceSource) Close() error {
sourcesMutex.Lock()
defer sourcesMutex.Unlock()
return s.closeImpl()
}
func (s *FaceSource) closeImpl() error {
s.logger.Info("FaceSource close")
s.setRef(&s.c.base, nil)
delete(faceSources, s.key)
go func() {
urcu.Synchronize()
s.Writer.stopSource()
s.logger.Info("FaceSource freed")
eal.Free(s.c)
}()
return nil
}
// NewFaceSource creates a FaceSource.
func NewFaceSource(cfg FaceConfig) (s *FaceSource, e error) {
if e := cfg.validate(); e != nil {
return nil, e
}
sourcesMutex.Lock()
defer sourcesMutex.Unlock()
s = &FaceSource{
FaceConfig: cfg,
key: faceDir{cfg.Face.ID(), cfg.Dir},
}
if _, ok := faceSources[s.key]; ok {
return nil, errors.New("another FaceSource is attached to this face and direction")
}
socket := s.Face.NumaSocket()
s.logger = logger.With(s.Face.ID().ZapField("face"), zap.String("dir", string(s.Dir)))
s.c = eal.Zmalloc[C.PdumpFaceSource]("PdumpFaceSource", C.sizeof_PdumpFaceSource, socket)
s.c.base = C.PdumpSource{
directMp: (*C.struct_rte_mempool)(pktmbuf.Direct.Get(socket).Ptr()),
queue: s.Writer.c.queue,
filter: C.PdumpSource_Filter(C.PdumpFaceSource_Filter),
mbufType: MbufTypeSLL | C.uint32_t(dirImpls[s.Dir].sllType),
mbufPort: C.uint16_t(s.Face.ID()),
mbufCopy: true,
}
pcg32.Init(unsafe.Pointer(&s.c.rng))
// sort by descending name length for longest prefix match
slices.SortFunc(s.Names, func(a, b NameFilterEntry) int { return len(b.Name) - len(a.Name) })
prefixes := ndni.NewLNamePrefixFilterBuilder(unsafe.Pointer(&s.c.nameL), unsafe.Sizeof(s.c.nameL),
unsafe.Pointer(&s.c.nameV), unsafe.Sizeof(s.c.nameV))
for i, nf := range s.Names {
if e := prefixes.Append(nf.Name); e != nil {
eal.Free(s.c)
return nil, errors.New("names too long")
}
s.c.sample[i] = C.uint32_t(math.Ceil(nf.SampleProbability * math.MaxUint32))
}
s.Writer.defineIntf(int(s.Face.ID()), pcapgo.NgInterface{
Name: fmt.Sprintf("face%d", s.Face.ID()),
Description: iface.LocatorString(s.Face.Locator()),
LinkType: layers.LinkTypeLinuxSLL,
})
s.Writer.startSource()
s.setRef(nil, &s.c.base)
faceClosingOnce.Do(func() { iface.OnFaceClosing(handleFaceClosing) })
faceSources[s.key] = s
s.logger.Info("FaceSource open",
zap.Uintptr("dumper", uintptr(unsafe.Pointer(s.c))),
zap.Uintptr("queue", uintptr(unsafe.Pointer(s.Writer.c.queue))),
)
return s, nil
}