Beispiel #1
0
func (m *CtMap) DumpToSlice() ([]CtEntryDump, error) {
	var entry CtEntry
	entries := []CtEntryDump{}

	switch m.Type {
	case CtTypeIPv6:
		var key, nextKey CtKey6
		for {
			err := bpf.GetNextKey(m.Fd, unsafe.Pointer(&key), unsafe.Pointer(&nextKey))
			if err != nil {
				break
			}

			err = bpf.LookupElement(
				m.Fd,
				unsafe.Pointer(&nextKey),
				unsafe.Pointer(&entry),
			)
			if err != nil {
				return nil, err
			}

			eDump := CtEntryDump{Key: nextKey, Value: entry}
			entries = append(entries, eDump)

			key = nextKey
		}

	case CtTypeIPv4:
		var key, nextKey CtKey4
		for {
			err := bpf.GetNextKey(m.Fd, unsafe.Pointer(&key), unsafe.Pointer(&nextKey))
			if err != nil {
				break
			}

			err = bpf.LookupElement(
				m.Fd,
				unsafe.Pointer(&nextKey),
				unsafe.Pointer(&entry),
			)
			if err != nil {
				return nil, err
			}

			eDump := CtEntryDump{Key: nextKey, Value: entry}
			entries = append(entries, eDump)

			key = nextKey
		}
	}

	return entries, nil
}
Beispiel #2
0
func (m *PolicyMap) DumpToSlice() ([]PolicyEntryDump, error) {
	var key, nextKey uint32
	key = MAX_KEYS
	entries := []PolicyEntryDump{}
	for {
		var entry PolicyEntry
		err := bpf.GetNextKey(
			m.Fd,
			unsafe.Pointer(&key),
			unsafe.Pointer(&nextKey),
		)

		if err != nil {
			break
		}

		err = bpf.LookupElement(
			m.Fd,
			unsafe.Pointer(&nextKey),
			unsafe.Pointer(&entry),
		)

		if err != nil {
			return nil, err
		} else {
			eDump := PolicyEntryDump{ID: nextKey, PolicyEntry: entry}
			entries = append(entries, eDump)
		}

		key = nextKey
	}

	return entries, nil
}
Beispiel #3
0
func (m *CtMap) doGc(interval uint16, key unsafe.Pointer, nextKey unsafe.Pointer, deleted *int) bool {
	var entry CtEntry

	err := bpf.GetNextKey(m.Fd, key, nextKey)
	if err != nil {
		return false
	}

	err = bpf.LookupElement(m.Fd, nextKey, unsafe.Pointer(&entry))
	if err != nil {
		return false
	}

	if entry.lifetime <= interval {
		bpf.DeleteElement(m.Fd, nextKey)
		(*deleted)++
	} else {
		entry.lifetime -= interval
		bpf.UpdateElement(m.Fd, nextKey, unsafe.Pointer(&entry), 0)
	}

	return true
}