1
1
package pdump
2
2
3
3
/*
4
- #include "../../csrc/pdump/face.h"
5
4
#include "../../csrc/pdump/format.h"
5
+ #include "../../csrc/pdump/source.h"
6
6
#include "../../csrc/iface/face.h"
7
7
*/
8
8
import "C"
@@ -15,6 +15,8 @@ import (
15
15
"sync"
16
16
"unsafe"
17
17
18
+ "github.com/google/gopacket/layers"
19
+ "github.com/google/gopacket/pcapgo"
18
20
"github.com/usnistgov/ndn-dpdk/core/urcu"
19
21
"github.com/usnistgov/ndn-dpdk/dpdk/eal"
20
22
"github.com/usnistgov/ndn-dpdk/dpdk/pktmbuf"
@@ -36,15 +38,15 @@ const (
36
38
37
39
var dirImpls = map [Direction ]struct {
38
40
sllType C.rte_be16_t
39
- getRef func (faceC * C.Face ) * C.PdumpFaceRef
41
+ getRef func (faceC * C.Face ) * C.PdumpSourceRef
40
42
}{
41
43
DirIncoming : {
42
44
C .SLLIncoming ,
43
- func (faceC * C.Face ) * C.PdumpFaceRef { return & faceC .impl .rx .pdump },
45
+ func (faceC * C.Face ) * C.PdumpSourceRef { return & faceC .impl .rx .pdump },
44
46
},
45
47
DirOutgoing : {
46
48
C .SLLOutgoing ,
47
- func (faceC * C.Face ) * C.PdumpFaceRef { return & faceC .impl .tx .pdump },
49
+ func (faceC * C.Face ) * C.PdumpSourceRef { return & faceC .impl .tx .pdump },
48
50
},
49
51
}
50
52
@@ -64,24 +66,23 @@ func parseFaceDir(input string) (fd faceDir, e error) {
64
66
65
67
var (
66
68
faceSources = map [faceDir ]* FaceSource {}
67
- faceSourcesLock sync.Mutex
68
69
faceClosingOnce sync.Once
69
70
)
70
71
71
72
func handleFaceClosing (id iface.ID ) {
72
- faceSourcesLock .Lock ()
73
- defer faceSourcesLock .Unlock ()
73
+ sourcesLock .Lock ()
74
+ defer sourcesLock .Unlock ()
74
75
75
76
for dir := range dirImpls {
76
- fs , ok := faceSources [faceDir {id , dir }]
77
+ s , ok := faceSources [faceDir {id , dir }]
77
78
if ! ok {
78
79
continue
79
80
}
80
- fs .closeImpl ()
81
+ s .closeImpl ()
81
82
}
82
83
}
83
84
84
- // FaceConfig contains face dumper configuration.
85
+ // FaceConfig contains FaceSource configuration.
85
86
type FaceConfig struct {
86
87
Writer * Writer
87
88
Face iface.Face
@@ -129,90 +130,90 @@ type FaceSource struct {
129
130
FaceConfig
130
131
key faceDir
131
132
logger * zap.Logger
132
- c * C.PdumpFace
133
+ c * C.PdumpFaceSource
133
134
}
134
135
135
- func (fs * FaceSource ) setPdumpFaceRef (expected , newPtr * C.PdumpFace ) {
136
- ref := dirImpls [fs .Dir ].getRef ((* C .Face )(fs .Face .Ptr ()))
137
- if old := C .PdumpFaceRef_Set (ref , newPtr ); old != expected {
138
- fs .logger .Panic ("PdumpFaceRef pointer mismatch" ,
139
- zap .Uintptr ("new" , uintptr (unsafe .Pointer (newPtr ))),
140
- zap .Uintptr ("old" , uintptr (unsafe .Pointer (old ))),
141
- zap .Uintptr ("expected" , uintptr (unsafe .Pointer (expected ))),
142
- )
143
- }
136
+ func (s * FaceSource ) setRef (expected , newPtr * C.PdumpSource ) {
137
+ ref := dirImpls [s .Dir ].getRef ((* C .Face )(s .Face .Ptr ()))
138
+ setSourceRef (ref , expected , newPtr )
144
139
}
145
140
146
141
// Close detaches the dump source.
147
- func (fs * FaceSource ) Close () error {
148
- faceSourcesLock .Lock ()
149
- defer faceSourcesLock .Unlock ()
150
- return fs .closeImpl ()
142
+ func (s * FaceSource ) Close () error {
143
+ sourcesLock .Lock ()
144
+ defer sourcesLock .Unlock ()
145
+ return s .closeImpl ()
151
146
}
152
147
153
- func (fs * FaceSource ) closeImpl () error {
154
- fs .logger .Info ("PdumpFace close" )
155
- fs . setPdumpFaceRef ( fs . c , nil )
156
- delete (faceSources , fs .key )
148
+ func (s * FaceSource ) closeImpl () error {
149
+ s .logger .Info ("FaceSource close" )
150
+ s . setRef ( & s . c . base , nil )
151
+ delete (faceSources , s .key )
157
152
158
153
go func () {
159
154
urcu .Synchronize ()
160
- fs .Writer .stopSource ()
161
- fs .logger .Info ("PdumpFace freed" )
162
- eal .Free (fs .c )
155
+ s .Writer .stopSource ()
156
+ s .logger .Info ("FaceSource freed" )
157
+ eal .Free (s .c )
163
158
}()
164
159
return nil
165
160
}
166
161
167
162
// NewFaceSource creates a FaceSource.
168
- func NewFaceSource (cfg FaceConfig ) (fs * FaceSource , e error ) {
163
+ func NewFaceSource (cfg FaceConfig ) (s * FaceSource , e error ) {
169
164
if e := cfg .validate (); e != nil {
170
165
return nil , e
171
166
}
172
167
173
- faceSourcesLock .Lock ()
174
- defer faceSourcesLock .Unlock ()
168
+ sourcesLock .Lock ()
169
+ defer sourcesLock .Unlock ()
175
170
176
- fs = & FaceSource {
171
+ s = & FaceSource {
177
172
FaceConfig : cfg ,
178
173
key : faceDir {cfg .Face .ID (), cfg .Dir },
179
174
}
180
- if _ , ok := faceSources [fs .key ]; ok {
181
- return nil , errors .New ("another PdumpFace is attached to this face and direction" )
175
+ if _ , ok := faceSources [s .key ]; ok {
176
+ return nil , errors .New ("another FaceSource is attached to this face and direction" )
182
177
}
183
- socket := cfg .Face .NumaSocket ()
178
+ socket := s .Face .NumaSocket ()
184
179
185
- fs .logger = logger .With (cfg .Face .ID ().ZapField ("face" ), zap .String ("dir" , string (cfg .Dir )))
186
- fs .c = (* C .PdumpFace )(eal .Zmalloc ("PdumpFace " , C .sizeof_PdumpFace , socket ))
187
- * fs . c = C.PdumpFace {
180
+ s .logger = logger .With (s .Face .ID ().ZapField ("face" ), zap .String ("dir" , string (s .Dir )))
181
+ s .c = (* C .PdumpFaceSource )(eal .Zmalloc ("PdumpFaceSource " , C .sizeof_PdumpFaceSource , socket ))
182
+ s . c . base = C.PdumpSource {
188
183
directMp : (* C .struct_rte_mempool )(pktmbuf .Direct .Get (socket ).Ptr ()),
189
- queue : fs .Writer .c .queue ,
190
- sllType : dirImpls [cfg .Dir ].sllType ,
184
+ queue : s .Writer .c .queue ,
185
+ filter : C .PdumpSource_Filter (C .PdumpFaceSource_Filter ),
186
+ mbufType : MbufTypeSLL | C .uint32_t (dirImpls [s .Dir ].sllType ),
187
+ mbufPort : C .uint16_t (s .Face .ID ()),
188
+ mbufCopy : true ,
191
189
}
192
- C .pcg32_srandom_r (& fs .c .rng , C .uint64_t (rand .Uint64 ()), C .uint64_t (rand .Uint64 ()))
190
+ C .pcg32_srandom_r (& s .c .rng , C .uint64_t (rand .Uint64 ()), C .uint64_t (rand .Uint64 ()))
193
191
194
192
// sort by decending name length for longest prefix match
195
- sort .Slice (cfg .Names , func (i , j int ) bool { return len (cfg .Names [i ].Name ) > len (cfg .Names [j ].Name ) })
196
- prefixes := ndni .NewLNamePrefixFilterBuilder (unsafe .Pointer (& fs .c .nameL ), unsafe .Sizeof (fs .c .nameL ),
197
- unsafe .Pointer (& fs .c .nameV ), unsafe .Sizeof (fs .c .nameV ))
198
- for i , nf := range cfg .Names {
193
+ sort .Slice (s .Names , func (i , j int ) bool { return len (s .Names [i ].Name ) > len (s .Names [j ].Name ) })
194
+ prefixes := ndni .NewLNamePrefixFilterBuilder (unsafe .Pointer (& s .c .nameL ), unsafe .Sizeof (s .c .nameL ),
195
+ unsafe .Pointer (& s .c .nameV ), unsafe .Sizeof (s .c .nameV ))
196
+ for i , nf := range s .Names {
199
197
if e := prefixes .Append (nf .Name ); e != nil {
200
- eal .Free (fs .c )
198
+ eal .Free (s .c )
201
199
return nil , errors .New ("names too long" )
202
200
}
203
- fs .c .sample [i ] = C .uint32_t (math .Ceil (nf .SampleProbability * math .MaxUint32 ))
201
+ s .c .sample [i ] = C .uint32_t (math .Ceil (nf .SampleProbability * math .MaxUint32 ))
204
202
}
205
203
206
- fs .Writer .defineFace (fs .Face )
207
- fs .Writer .startSource ()
208
- fs .setPdumpFaceRef (nil , fs .c )
204
+ s .Writer .defineIntf (int (s .Face .ID ()), pcapgo.NgInterface {
205
+ Name : fmt .Sprintf ("face%d" , s .Face .ID ()),
206
+ Description : iface .LocatorString (s .Face .Locator ()),
207
+ LinkType : layers .LinkTypeLinuxSLL ,
208
+ })
209
+ s .Writer .startSource ()
210
+ s .setRef (nil , & s .c .base )
209
211
210
212
faceClosingOnce .Do (func () { iface .OnFaceClosing (handleFaceClosing ) })
211
- faceSources [fs .key ] = fs
212
-
213
- fs .logger .Info ("PdumpFace open" ,
214
- zap .Uintptr ("dumper" , uintptr (unsafe .Pointer (fs .c ))),
215
- zap .Uintptr ("queue" , uintptr (unsafe .Pointer (fs .c .queue ))),
213
+ faceSources [s .key ] = s
214
+ s .logger .Info ("FaceSource open" ,
215
+ zap .Uintptr ("dumper" , uintptr (unsafe .Pointer (s .c ))),
216
+ zap .Uintptr ("queue" , uintptr (unsafe .Pointer (s .Writer .c .queue ))),
216
217
)
217
- return fs , nil
218
+ return s , nil
218
219
}
0 commit comments