-
Notifications
You must be signed in to change notification settings - Fork 39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Map arguments optimization #421
Merged
Merged
Changes from 9 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
9df5c73
map_arg_optimization
Atul-source 5cc0d9e
clean bpf map before updating them
Atul-source 1fdb84e
Merge branch 'main' into map-arg-opt
Atul-source 1c7eefe
refactoring code
Atul-source ec393fa
removed unwanted changes
Atul-source afda647
capital removal
Atul-source 049fa09
addressing comments
Atul-source e00d540
removing test code
Atul-source 098f177
updated the comments
Atul-source cd1533a
addressed comments
Atul-source b5589ce
addressed comments
Atul-source 36327a9
addressed comments
Atul-source 7cc2db3
Merge branch 'l3af-project:main' into map-arg-opt
Atul-source File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -5,13 +5,13 @@ package bpfprogs | |||||
|
||||||
import ( | ||||||
"container/ring" | ||||||
"errors" | ||||||
"fmt" | ||||||
"math" | ||||||
"strconv" | ||||||
"strings" | ||||||
"unsafe" | ||||||
|
||||||
"github.com/cilium/ebpf" | ||||||
"github.com/l3af-project/l3afd/v2/models" | ||||||
"github.com/rs/zerolog/log" | ||||||
) | ||||||
|
||||||
|
@@ -33,66 +33,51 @@ type MetricsBPFMap struct { | |||||
lastValue float64 | ||||||
} | ||||||
|
||||||
// The update function is used to update eBPF maps, which are used by network functions. | ||||||
// Supported types are Array and Hash | ||||||
// Multiple values are comma separated | ||||||
// Hashmap can be multiple values or single values. | ||||||
// If hash map entries then key will be values and value will be set to 1 | ||||||
// In case of Array then key will be index starting from 0 and values are stored. | ||||||
// for e.g. | ||||||
// | ||||||
// HashMap scenario 1. --ports="80,443" values are stored in rl_ports_map BPF map | ||||||
// key => 80 value => 1 | ||||||
// key => 443 value => 1 | ||||||
// HashMap scenario 2. --ports="443" value is stored in rl_ports_map BPF map | ||||||
// key => 443 value => 1 | ||||||
// Array scenario 1. --ports="80,443" values are stored in rl_ports_map BPF map | ||||||
// key => 0 value => 80 | ||||||
// key => 1 value => 443 | ||||||
// Array scenario 2. --rate="10000" value is stored in rl_config_map BPF map | ||||||
// key => 0 value => 10000 | ||||||
func (b *BPFMap) Update(value string) error { | ||||||
|
||||||
log.Debug().Msgf("update map name %s ID %d", b.Name, b.MapID) | ||||||
// The RemoveMissingKeys function is used to delete all entries of eBPF maps, which are used by network functions. | ||||||
func (b *BPFMap) RemoveMissingKeys(args []models.KeyValue) error { | ||||||
ebpfMap, err := ebpf.NewMapFromID(b.MapID) | ||||||
if err != nil { | ||||||
return fmt.Errorf("access new map from ID failed %w", err) | ||||||
} | ||||||
defer ebpfMap.Close() | ||||||
|
||||||
// check values are single or multiple | ||||||
s := strings.Split(value, ",") | ||||||
|
||||||
if b.Type == ebpf.Hash { | ||||||
// clear map elements | ||||||
key := 0 | ||||||
val := 0 | ||||||
entries := ebpfMap.Iterate() | ||||||
for entries.Next(unsafe.Pointer(&key), unsafe.Pointer(&val)) { | ||||||
// Order of keys is non-deterministic due to randomized map seed | ||||||
if err := ebpfMap.Delete(unsafe.Pointer(&key)); err != nil { | ||||||
log.Warn().Err(err).Msgf("delete hash map for key %d failed", key) | ||||||
} | ||||||
} | ||||||
|
||||||
for key, val := range s { | ||||||
v, _ := strconv.ParseInt(val, 10, 64) | ||||||
x := 1 | ||||||
log.Info().Msgf("updating map %s key %d mapid %d", b.Name, v, b.MapID) | ||||||
if err := ebpfMap.Update(unsafe.Pointer(&v), unsafe.Pointer(&x), 0); err != nil { | ||||||
return fmt.Errorf("update hash map element failed for key %d error %w", key, err) | ||||||
KeyValueMap := make(map[int]bool, 0) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Define the size of KeyValueMap to the size of args instead of 0. |
||||||
for _, k := range args { | ||||||
KeyValueMap[k.Key] = true | ||||||
} | ||||||
var key, nextKey int | ||||||
for { | ||||||
err := ebpfMap.NextKey(unsafe.Pointer(&key), unsafe.Pointer(&nextKey)) | ||||||
if err != nil { | ||||||
if errors.Is(err, ebpf.ErrKeyNotExist) { | ||||||
break | ||||||
} else { | ||||||
return fmt.Errorf("get next key failed with error %w, mapid %d", err, b.MapID) | ||||||
} | ||||||
} | ||||||
} else if b.Type == ebpf.Array { | ||||||
for key, val := range s { | ||||||
v, _ := strconv.ParseInt(val, 10, 64) | ||||||
log.Info().Msgf("updating map %s key %d mapid %d", b.Name, v, b.MapID) | ||||||
if err := ebpfMap.Update(unsafe.Pointer(&key), unsafe.Pointer(&v), 0); err != nil { | ||||||
return fmt.Errorf("update array map index %d %w", key, err) | ||||||
key = nextKey | ||||||
_, IsKeyExists := KeyValueMap[key] | ||||||
if !IsKeyExists { | ||||||
log.Info().Msgf("removing key %v because it is missing\n", key) | ||||||
if err := ebpfMap.Delete(unsafe.Pointer(&key)); err != nil { | ||||||
return fmt.Errorf("delete key failed with error %w, mapid %d", err, b.MapID) | ||||||
} | ||||||
} | ||||||
} else { | ||||||
return fmt.Errorf("unsupported map type") | ||||||
} | ||||||
return nil | ||||||
} | ||||||
|
||||||
// The update function is used to update eBPF maps, which are used by network functions. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
func (b *BPFMap) Update(key, value int) error { | ||||||
|
||||||
log.Debug().Msgf("update map name %s ID %d", b.Name, b.MapID) | ||||||
ebpfMap, err := ebpf.NewMapFromID(b.MapID) | ||||||
if err != nil { | ||||||
return fmt.Errorf("access new map from ID failed %w", err) | ||||||
} | ||||||
defer ebpfMap.Close() | ||||||
log.Info().Msgf("updating map %s key %d mapid %d", b.Name, key, b.MapID) | ||||||
if err := ebpfMap.Update(unsafe.Pointer(&key), unsafe.Pointer(&value), 0); err != nil { | ||||||
return fmt.Errorf("update hash map element failed for key %d error %w", key, err) | ||||||
} | ||||||
return nil | ||||||
} | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.