Skip to content

Commit 339a215

Browse files
chenchunvishvananda
authored andcommitted
Fix NewHtbClass
- Prio and Quantum should be set equal to the input params - Fix buffer and cbuffer due to incorrect hz - Fix Xmittime which is also not equal the behavior of c library. C library converts time to uint32 to drop precision before multiply tick_in_usec. ref https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/tree/tc/tc_core.c#n62
1 parent 3374423 commit 339a215

File tree

4 files changed

+25
-11
lines changed

4 files changed

+25
-11
lines changed

class_linux.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,22 @@ func NewHtbClass(attrs ClassAttrs, cattrs HtbClassAttrs) *HtbClass {
4343
if buffer == 0 {
4444
buffer = uint32(float64(rate)/Hz() + float64(mtu))
4545
}
46-
buffer = uint32(Xmittime(rate, buffer))
46+
buffer = Xmittime(rate, buffer)
4747

4848
if cbuffer == 0 {
4949
cbuffer = uint32(float64(ceil)/Hz() + float64(mtu))
5050
}
51-
cbuffer = uint32(Xmittime(ceil, cbuffer))
51+
cbuffer = Xmittime(ceil, cbuffer)
5252

5353
return &HtbClass{
5454
ClassAttrs: attrs,
5555
Rate: rate,
5656
Ceil: ceil,
5757
Buffer: buffer,
5858
Cbuffer: cbuffer,
59-
Quantum: 10,
6059
Level: 0,
61-
Prio: 0,
60+
Prio: cattrs.Prio,
61+
Quantum: cattrs.Quantum,
6262
}
6363
}
6464

class_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,8 @@ func TestClassAddDel(t *testing.T) {
9797
htbclassattrs := HtbClassAttrs{
9898
Rate: 1234000,
9999
Cbuffer: 1690,
100+
Prio: 2,
101+
Quantum: 1000,
100102
}
101103
class := NewHtbClass(classattrs, htbclassattrs)
102104
if err := ClassAdd(class); err != nil {
@@ -126,6 +128,12 @@ func TestClassAddDel(t *testing.T) {
126128
if htb.Cbuffer != class.Cbuffer {
127129
t.Fatal("Cbuffer doesn't match")
128130
}
131+
if htb.Prio != class.Prio {
132+
t.Fatal("Prio doesn't match")
133+
}
134+
if htb.Quantum != class.Quantum {
135+
t.Fatal("Quantum doesn't match")
136+
}
129137

130138
testClassStats(htb.ClassAttrs.Statistics, NewClassStatistics(), t)
131139

filter_linux.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func NewFw(attrs FilterAttrs, fattrs FilterFwAttrs) (*Fw, error) {
8989
if CalcRtable(&police.Rate, rtab[:], rcellLog, fattrs.Mtu, linklayer) < 0 {
9090
return nil, errors.New("TBF: failed to calculate rate table")
9191
}
92-
police.Burst = uint32(Xmittime(uint64(police.Rate.Rate), uint32(buffer)))
92+
police.Burst = Xmittime(uint64(police.Rate.Rate), uint32(buffer))
9393
}
9494
police.Mtu = fattrs.Mtu
9595
if police.PeakRate.Rate != 0 {
@@ -783,7 +783,7 @@ func CalcRtable(rate *nl.TcRateSpec, rtab []uint32, cellLog int, mtu uint32, lin
783783
}
784784
for i := 0; i < 256; i++ {
785785
sz = AdjustSize(uint((i+1)<<uint32(cellLog)), uint(mpu), linklayer)
786-
rtab[i] = uint32(Xmittime(uint64(bps), uint32(sz)))
786+
rtab[i] = Xmittime(uint64(bps), uint32(sz))
787787
}
788788
rate.CellAlign = -1
789789
rate.CellLog = uint8(cellLog)

qdisc_linux.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -598,10 +598,10 @@ func initClock() {
598598
return
599599
}
600600
parts := strings.Split(strings.TrimSpace(string(data)), " ")
601-
if len(parts) < 3 {
601+
if len(parts) < 4 {
602602
return
603603
}
604-
var vals [3]uint64
604+
var vals [4]uint64
605605
for i := range vals {
606606
val, err := strconv.ParseUint(parts[i], 16, 32)
607607
if err != nil {
@@ -615,7 +615,12 @@ func initClock() {
615615
}
616616
clockFactor = float64(vals[2]) / TIME_UNITS_PER_SEC
617617
tickInUsec = float64(vals[0]) / float64(vals[1]) * clockFactor
618-
hz = float64(vals[0])
618+
if vals[2] == 1000000 {
619+
// ref https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/tree/lib/utils.c#n963
620+
hz = float64(vals[3])
621+
} else {
622+
hz = 100
623+
}
619624
}
620625

621626
func TickInUsec() float64 {
@@ -663,6 +668,7 @@ func latency(rate uint64, limit, buffer uint32) float64 {
663668
return TIME_UNITS_PER_SEC*(float64(limit)/float64(rate)) - float64(tick2Time(buffer))
664669
}
665670

666-
func Xmittime(rate uint64, size uint32) float64 {
667-
return TickInUsec() * TIME_UNITS_PER_SEC * (float64(size) / float64(rate))
671+
func Xmittime(rate uint64, size uint32) uint32 {
672+
// https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/tree/tc/tc_core.c#n62
673+
return time2Tick(uint32(TIME_UNITS_PER_SEC * (float64(size) / float64(rate))))
668674
}

0 commit comments

Comments
 (0)