-
Notifications
You must be signed in to change notification settings - Fork 108
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add F-TEID allocation support (#840)
* feat: add f-teid allocation support Signed-off-by: Guillaume Belanger <[email protected]> * chore: uses else if instead of else and then if Signed-off-by: Guillaume Belanger <[email protected]> --------- Signed-off-by: Guillaume Belanger <[email protected]>
- Loading branch information
Showing
12 changed files
with
189 additions
and
19 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -123,6 +123,7 @@ sudo | |
tc | ||
TCP | ||
tcpdump | ||
TEID | ||
TRex | ||
UDP | ||
UE | ||
|
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 |
---|---|---|
@@ -1 +1 @@ | ||
1.5.1-dev | ||
2.0.0 |
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/omec-project/upf-epc | ||
|
||
go 1.21 | ||
go 1.23 | ||
|
||
require ( | ||
github.com/Showmax/go-fqdn v1.0.0 | ||
|
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 |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright 2024 Canonical Ltd. | ||
|
||
package pfcpiface | ||
|
||
import ( | ||
"errors" | ||
"math" | ||
"sync" | ||
) | ||
|
||
const ( | ||
minValue = 1 | ||
maxValue = math.MaxUint32 | ||
) | ||
|
||
type FTEIDGenerator struct { | ||
lock sync.Mutex | ||
offset uint32 | ||
usedMap map[uint32]bool | ||
} | ||
|
||
func NewFTEIDGenerator() *FTEIDGenerator { | ||
return &FTEIDGenerator{ | ||
offset: 0, | ||
usedMap: make(map[uint32]bool), | ||
} | ||
} | ||
|
||
// Allocate and return an id in range [minValue, maxValue] | ||
func (idGenerator *FTEIDGenerator) Allocate() (uint32, error) { | ||
idGenerator.lock.Lock() | ||
defer idGenerator.lock.Unlock() | ||
|
||
offsetBegin := idGenerator.offset | ||
for { | ||
if _, ok := idGenerator.usedMap[idGenerator.offset]; ok { | ||
idGenerator.updateOffset() | ||
|
||
if idGenerator.offset == offsetBegin { | ||
return 0, errors.New("no available value range to allocate id") | ||
} | ||
} else { | ||
break | ||
} | ||
} | ||
idGenerator.usedMap[idGenerator.offset] = true | ||
id := idGenerator.offset + minValue | ||
idGenerator.updateOffset() | ||
return id, nil | ||
} | ||
|
||
func (idGenerator *FTEIDGenerator) FreeID(id uint32) { | ||
if id < minValue { | ||
return | ||
} | ||
idGenerator.lock.Lock() | ||
defer idGenerator.lock.Unlock() | ||
delete(idGenerator.usedMap, id-minValue) | ||
} | ||
|
||
func (idGenerator *FTEIDGenerator) IsAllocated(id uint32) bool { | ||
if id < minValue { | ||
return false | ||
} | ||
idGenerator.lock.Lock() | ||
defer idGenerator.lock.Unlock() | ||
_, ok := idGenerator.usedMap[id-minValue] | ||
return ok | ||
} | ||
|
||
func (idGenerator *FTEIDGenerator) updateOffset() { | ||
idGenerator.offset++ | ||
idGenerator.offset = idGenerator.offset % maxValue | ||
} |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright 2024 Canonical Ltd. | ||
|
||
package pfcpiface_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/omec-project/upf-epc/pfcpiface" | ||
) | ||
|
||
func TestFTEIDAllocate(t *testing.T) { | ||
fteidGenerator := pfcpiface.NewFTEIDGenerator() | ||
|
||
fteid, err := fteidGenerator.Allocate() | ||
if err != nil { | ||
t.Errorf("FTEID allocation failed: %v", err) | ||
} | ||
if fteid < 1 { | ||
t.Errorf("FTEID allocation failed, value is too small: %v", fteid) | ||
} | ||
if !fteidGenerator.IsAllocated(fteid) { | ||
t.Errorf("FTEID was not allocated") | ||
} | ||
} | ||
|
||
func TestFTEIDFree(t *testing.T) { | ||
fteidGenerator := pfcpiface.NewFTEIDGenerator() | ||
fteid, err := fteidGenerator.Allocate() | ||
if err != nil { | ||
t.Errorf("FTEID allocation failed: %v", err) | ||
} | ||
|
||
fteidGenerator.FreeID(fteid) | ||
|
||
if fteidGenerator.IsAllocated(fteid) { | ||
t.Errorf("FTEID was not freed") | ||
} | ||
} |
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
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
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
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