Return error as part of NewHtbClass

This commit is contained in:
chantra 2015-12-25 23:40:22 -08:00
parent 683203e227
commit c68d0f3e3c
3 changed files with 12 additions and 11 deletions

View File

@ -1,6 +1,7 @@
package netlink
import (
"errors"
"fmt"
"github.com/vishvananda/netlink/nl"
@ -54,7 +55,7 @@ type HtbClass struct {
Ctab [256]uint32
}
func NewHtbClass(attrs ClassAttrs, cattrs HtbClassAttrs) *HtbClass {
func NewHtbClass(attrs ClassAttrs, cattrs HtbClassAttrs) (*HtbClass, error) {
var rtab [256]uint32
var ctab [256]uint32
cell_log := -1
@ -82,12 +83,12 @@ func NewHtbClass(attrs ClassAttrs, cattrs HtbClassAttrs) *HtbClass {
tcrate := nl.TcRateSpec{Rate: uint32(rate)}
if CalcRtable(&tcrate, rtab, cell_log, uint32(mtu), linklayer) < 0 {
return nil
return nil, errors.New("HTB: failed to calculate rate table.")
}
tcceil := nl.TcRateSpec{Rate: uint32(ceil)}
if CalcRtable(&tcceil, ctab, ccell_log, uint32(mtu), linklayer) < 0 {
return nil
return nil, errors.New("HTB: failed to calculate ceil rate table.")
}
return &HtbClass{
@ -101,7 +102,7 @@ func NewHtbClass(attrs ClassAttrs, cattrs HtbClassAttrs) *HtbClass {
Prio: 0,
Rtab: rtab,
Ctab: ctab,
}
}, nil
}
func (q HtbClass) String() string {

View File

@ -51,7 +51,7 @@ func TestClassAddDel(t *testing.T) {
Rate: 1234000,
Cbuffer: 1690,
}
class := NewHtbClass(classattrs, htbclassattrs)
class, _ := NewHtbClass(classattrs, htbclassattrs)
if err := ClassAdd(class); err != nil {
t.Fatal(err)
}
@ -214,7 +214,7 @@ func TestHtbClassAddHtbClassChangeDel(t *testing.T) {
Rate: 1234000,
Cbuffer: 1690,
}
class := NewHtbClass(classattrs, htbclassattrs)
class, _ := NewHtbClass(classattrs, htbclassattrs)
if err := ClassAdd(class); err != nil {
t.Fatal(err)
}
@ -271,14 +271,14 @@ func TestHtbClassAddHtbClassChangeDel(t *testing.T) {
// First, test it fails if we change the Handle.
old_handle := classattrs.Handle
classattrs.Handle = MakeHandle(0xffff, 3)
class = NewHtbClass(classattrs, htbclassattrs)
class, _ = NewHtbClass(classattrs, htbclassattrs)
if err := ClassChange(class); err == nil {
t.Fatal("ClassChange should not work when using a different handle.")
}
// It should work with the same handle
classattrs.Handle = old_handle
htbclassattrs.Rate = 4321000
class = NewHtbClass(classattrs, htbclassattrs)
class, _ = NewHtbClass(classattrs, htbclassattrs)
if err := ClassChange(class); err != nil {
t.Fatal(err)
}
@ -327,7 +327,7 @@ func TestHtbClassAddHtbClassChangeDel(t *testing.T) {
// Then, replace by providing a new handle, n new class will be created.
// Replace acting as Change
class = NewHtbClass(classattrs, htbclassattrs)
class, _ = NewHtbClass(classattrs, htbclassattrs)
if err := ClassReplace(class); err != nil {
t.Fatal("Failed to replace class that is existing.")
}
@ -354,7 +354,7 @@ func TestHtbClassAddHtbClassChangeDel(t *testing.T) {
// It should work with the same handle
classattrs.Handle = MakeHandle(0xffff, 3)
class = NewHtbClass(classattrs, htbclassattrs)
class, _ = NewHtbClass(classattrs, htbclassattrs)
if err := ClassReplace(class); err != nil {
t.Fatal(err)
}

View File

@ -146,7 +146,7 @@ func TestFilterFwAddDel(t *testing.T) {
Rate: 1234000,
Cbuffer: 1690,
}
class := NewHtbClass(classattrs, htbclassattrs)
class, _ := NewHtbClass(classattrs, htbclassattrs)
if err := ClassAdd(class); err != nil {
t.Fatal(err)
}