Skip to content
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

update token fill frequency #95

Merged
merged 2 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion include/trTCM.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ typedef struct {
u64 tc;
u64 te;

u64 tokenRate;
u64 byteRate;
u64 lastUpdate;
u64 refillTokenTime;

spinlock_t lock;
} TrafficPolicer;
Expand Down
27 changes: 20 additions & 7 deletions src/gtpu/trTCM.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "trTCM.h"
#include "log.h"

TrafficPolicer* newTrafficPolicer(u64 tokenRate) {
TrafficPolicer* newTrafficPolicer(u64 bitRate) {
TrafficPolicer* p = (TrafficPolicer*)kmalloc(sizeof(TrafficPolicer), GFP_KERNEL);
if (p == NULL) {
GTP5G_ERR(NULL, "traffic policer memory allocation error\n");
Expand All @@ -14,10 +14,10 @@ TrafficPolicer* newTrafficPolicer(u64 tokenRate) {

spin_lock_init(&p->lock);

p->tokenRate = tokenRate * 125 ; // Kbit/s to byte/s (*1000/8)
p->byteRate = bitRate * 125 ; // Kbit/s to byte/s (*1000/8)

// 1ms as burst size
p->cbs = p->tokenRate / 125; // bytes
p->cbs = p->byteRate / 125; // bytes
p->ebs = p->cbs * 4; // bytes

// fill buckets at the begining
Expand All @@ -26,21 +26,34 @@ TrafficPolicer* newTrafficPolicer(u64 tokenRate) {

p->lastUpdate = ktime_get_ns();

p->refillTokenTime = 0;

return p;
}

Color policePacket(TrafficPolicer* p, int pktLen) {
u64 tokensToAdd;
u64 tc, te;
u64 elapsed;
u64 tokensToAdd = 0;
u64 tc, te = 0;
u64 elapsed = 0;
u64 now = ktime_get_ns();

spin_lock(&p->lock);

elapsed = now - p->lastUpdate;
p->lastUpdate = now;

tokensToAdd = elapsed * p->tokenRate / 1000000000;
// the total time elapsed since the last token refill
p->refillTokenTime = p->refillTokenTime + elapsed;

#define REFILL_TOKEN_INTERVAL 1000000 // ns (=1ms), 1 token = 1 byte
#define SECOND_TO_NANOSECOND 1000000000
if (p->refillTokenTime >= REFILL_TOKEN_INTERVAL) {
// add at least one token
p->refillTokenTime = p->refillTokenTime - REFILL_TOKEN_INTERVAL;
tokensToAdd = p->byteRate * (REFILL_TOKEN_INTERVAL / SECOND_TO_NANOSECOND);
} else {
tokensToAdd = 0;
}

tc = p->tc + tokensToAdd;
te = p->te;
Expand Down