Skip to content

Commit bb195b1

Browse files
committed
add third parameter domainID/srcID to key in template cache for IPFix/v9
1 parent 5036395 commit bb195b1

4 files changed

Lines changed: 36 additions & 61 deletions

File tree

ipfix/decoder.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ func (d *Decoder) decodeSet(mem MemCache, msg *Message) error {
169169
case rpcChan <- RPCRequest{
170170
ID: setHeader.SetID,
171171
IP: d.raddr,
172+
SrcID: msg.Header.DomainID,
172173
}:
173174
default:
174175
}
@@ -199,14 +200,13 @@ func (d *Decoder) decodeSet(mem MemCache, msg *Message) error {
199200
if err == nil {
200201
mem.insert(tr.TemplateID, d.raddr, tr, msg.Header.DomainID)
201202
}
202-
} else if setID <= 255 {
203-
if setID == 0 {
204-
// Invalid set
205-
err = nonfatalError{fmt.Errorf("failed to decodeSet / invalid setID")}
206-
}
207-
// Reserved set, do not read any records
208-
break
209-
} else {
203+
} else if setID >= 4 && setID <= 255 {
204+
// Reserved set, do not read any records
205+
break
206+
} else if setID == 0 {
207+
// Invalid set
208+
return fmt.Errorf("failed to decodeSet / invalid setID")
209+
} else {
210210
// Data set
211211
var data []DecodedField
212212
if data, err = d.decodeData(tr); err == nil {

ipfix/decoder_test.go

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,3 @@ func TestDecodeDataTpl(t *testing.T) {
230230
t.Error(err)
231231
}
232232
}
233-
234-
func TestDecodeTemplateWithInvalidSetID(t *testing.T) {
235-
var tpl = []byte{0, 10, 1, 32, 92, 88, 61, 152, 0, 1, 117, 22, 0, 0, 0, 0, /*invalid setID*/ 0, 0, 0, 68, 4, 0, 0, 15, 0, 153, 0, 8, 0, 152, 0, 8, 0, 1, 0, 8, 0, 2, 0, 8, 0, 60, 0, 1, 0, 10, 0, 4, 0, 14, 0, 4, 0, 61, 0, 1, 0, 8, 0, 4, 0, 12, 0, 4, 0, 7, 0, 2, 0, 11, 0, 2, 0, 5, 0, 1, 0, 6, 0, 1, 0, 4, 0, 1, 0, 2, 0, 68, 4, 1, 0, 15, 0, 153, 0, 8, 0, 152, 0, 8, 0, 1, 0, 8, 0, 2, 0, 8, 0, 60, 0, 1, 0, 10, 0, 4, 0, 14, 0, 4, 0, 61, 0, 1, 0, 8, 0, 4, 0, 12, 0, 4, 0, 7, 0, 2, 0, 11, 0, 2, 0, 5, 0, 1, 0, 6, 0, 1, 0, 4, 0, 1, 0, 2, 0, 68, 8, 0, 0, 15, 0, 153, 0, 8, 0, 152, 0, 8, 0, 1, 0, 8, 0, 2, 0, 8, 0, 60, 0, 1, 0, 10, 0, 4, 0, 14, 0, 4, 0, 61, 0, 1, 0, 27, 0, 16, 0, 28, 0, 16, 0, 5, 0, 1, 0, 7, 0, 2, 0, 11, 0, 2, 0, 6, 0, 1, 0, 4, 0, 1, 0, 2, 0, 68, 8, 1, 0, 15, 0, 153, 0, 8, 0, 152, 0, 8, 0, 1, 0, 8, 0, 2, 0, 8, 0, 60, 0, 1, 0, 10, 0, 4, 0, 14, 0, 4, 0, 61, 0, 1, 0, 27, 0, 16, 0, 28, 0, 16, 0, 5, 0, 1, 0, 7, 0, 2, 0, 11, 0, 2, 0, 6, 0, 1, 0, 4, 0, 1}
236-
237-
ip := net.ParseIP("127.0.0.1")
238-
mCache := GetCache("cache.file")
239-
d := NewDecoder(ip, tpl)
240-
_, err := d.Decode(mCache)
241-
242-
expectedErrorStr := `failed to decodeSet / invalid setID`
243-
if err != nil && err.Error() != expectedErrorStr {
244-
t.Error("unexpected error happened:", err)
245-
}
246-
}

ipfix/memcache.go

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,12 @@ func GetCache(cacheFile string) MemCache {
8282
func (m MemCache) getShard(templateId uint16, addr net.IP, domainId uint32) (*TemplatesShard, uint32) {
8383
var key []byte
8484
hash := fnv.New32()
85-
sId := make([]byte, 4)
85+
dId := make([]byte, 4)
8686
tID := make([]byte, 2)
87-
binary.LittleEndian.PutUint32(sId, domainId)
87+
binary.LittleEndian.PutUint32(dId, domainId)
8888
binary.BigEndian.PutUint16(tID, templateId)
8989
key = append(key, addr...)
90-
key = append(key, sId...)
90+
key = append(key, dId...)
9191
key = append(key, tID...)
9292

9393
hash.Write(key)
@@ -96,14 +96,14 @@ func (m MemCache) getShard(templateId uint16, addr net.IP, domainId uint32) (*Te
9696
return m[uint(hSum32)%uint(shardNo)], hSum32
9797
}
9898

99-
func (m *MemCache) insert(id uint16, addr net.IP, tr TemplateRecord, domainID uint32) {
99+
func (m MemCache) insert(id uint16, addr net.IP, tr TemplateRecord, domainID uint32) {
100100
shard, key := m.getShard(id, addr, domainID)
101101
shard.Lock()
102102
defer shard.Unlock()
103103
shard.Templates[key] = Data{tr, time.Now().Unix()}
104104
}
105105

106-
func (m *MemCache) retrieve(id uint16, addr net.IP, domainID uint32) (TemplateRecord, bool) {
106+
func (m MemCache) retrieve(id uint16, addr net.IP, domainID uint32) (TemplateRecord, bool) {
107107
shard, key := m.getShard(id, addr, domainID)
108108
shard.RLock()
109109
defer shard.RUnlock()
@@ -130,25 +130,6 @@ func (m MemCache) allSetIds() []int {
130130
return result
131131
}
132132

133-
// Fill a slice with all known set ids and their field counts. This is inefficient and is only used for error reporting or debugging.
134-
func (m MemCache) allSetIdsAndFieldCounts() []int {
135-
num := 0
136-
for _, shard := range m {
137-
num += len(shard.Templates)
138-
}
139-
result := make([]int, 0, num)
140-
for _, shard := range m {
141-
shard.RLock()
142-
for _, set := range shard.Templates {
143-
result = append(result, int(set.Template.TemplateID))
144-
result = append(result, int(set.Template.FieldCount))
145-
}
146-
shard.RUnlock()
147-
}
148-
sort.Ints(result)
149-
return result
150-
}
151-
152133
// Dump saves the current templates to hard disk
153134
func (m MemCache) Dump(cacheFile string) error {
154135
b, err := json.Marshal(

ipfix/memcache_test.go

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,24 +79,32 @@ func TestMemCacheAllSetIds(t *testing.T) {
7979
}
8080

8181
func TestMemCache_keyWithDifferentDomainIDs(t *testing.T) {
82-
var tpl1 TemplateRecord
83-
var tpl2 TemplateRecord
84-
82+
var tpl TemplateRecord
8583
ip := net.ParseIP("127.0.0.1")
8684
mCache := GetCache("cache.file")
8785

88-
tpl1.TemplateID = 310
89-
tpl1.FieldCount = 19
86+
tpl.TemplateID = 310
87+
tpl.FieldCount = 19
88+
mCache.insert(tpl.TemplateID, ip, tpl, 513)
9089

91-
tpl2.TemplateID = 310
92-
tpl2.FieldCount = 21
90+
tpl.FieldCount = 21
91+
mCache.insert(tpl.TemplateID, ip, tpl, 514)
9392

94-
mCache.insert(tpl1.TemplateID, ip, tpl1, 513)
95-
mCache.insert(tpl2.TemplateID, ip, tpl2, 514)
93+
v, ok := mCache.retrieve(tpl.TemplateID, ip, 513)
9694

97-
expected := []int{19, 21, 310, 310}
98-
actual := mCache.allSetIdsAndFieldCounts()
99-
if !reflect.DeepEqual(expected, actual) {
100-
t.Errorf("Expected set IDs %v, got %v", expected, actual)
101-
}
102-
}
95+
if !ok {
96+
t.Error("expected mCache retrieve status true, got", ok)
97+
}
98+
if v.FieldCount != 19 {
99+
t.Error("expected template id#:310 with Field count#:19, got", v.TemplateID, v.FieldCount)
100+
}
101+
102+
v, ok = mCache.retrieve(tpl.TemplateID, ip, 514)
103+
104+
if !ok {
105+
t.Error("expected mCache retrieve status true, got", ok)
106+
}
107+
if v.FieldCount != 21 {
108+
t.Error("expected template id#:310 with Field count#:21, got", v.TemplateID, v.FieldCount)
109+
}
110+
}

0 commit comments

Comments
 (0)