mirror of
https://github.com/vishvananda/netlink.git
synced 2025-09-26 20:01:13 +08:00
Cleanup from golint and go vet
This commit is contained in:
2
Makefile
2
Makefile
@@ -11,7 +11,7 @@ goroot = $(addprefix ../../../,$(1))
|
||||
unroot = $(subst ../../../,,$(1))
|
||||
fmt = $(addprefix fmt-,$(1))
|
||||
|
||||
all: fmt test
|
||||
all: test
|
||||
|
||||
$(call goroot,$(DEPS)):
|
||||
go get $(call unroot,$@)
|
||||
|
@@ -12,7 +12,7 @@ func TestAddr(t *testing.T) {
|
||||
t.Skipf("Fails in travis with: addr_test.go:68: Address flags not set properly, got=0, expected=128")
|
||||
}
|
||||
// TODO: IFA_F_PERMANENT does not seem to be set by default on older kernels?
|
||||
var address = &net.IPNet{net.IPv4(127, 0, 0, 2), net.CIDRMask(24, 32)}
|
||||
var address = &net.IPNet{IP: net.IPv4(127, 0, 0, 2), Mask: net.CIDRMask(24, 32)}
|
||||
var addrTests = []struct {
|
||||
addr *Addr
|
||||
expected *Addr
|
||||
|
12
class.go
12
class.go
@@ -9,7 +9,7 @@ type Class interface {
|
||||
Type() string
|
||||
}
|
||||
|
||||
// Class represents a netlink class. A filter is associated with a link,
|
||||
// ClassAttrs represents a netlink class. A filter is associated with a link,
|
||||
// has a handle and a parent. The root filter of a device should have a
|
||||
// parent == HANDLE_ROOT.
|
||||
type ClassAttrs struct {
|
||||
@@ -20,7 +20,7 @@ type ClassAttrs struct {
|
||||
}
|
||||
|
||||
func (q ClassAttrs) String() string {
|
||||
return fmt.Sprintf("{LinkIndex: %d, Handle: %s, Parent: %s, Leaf: %s}", q.LinkIndex, HandleStr(q.Handle), HandleStr(q.Parent), q.Leaf)
|
||||
return fmt.Sprintf("{LinkIndex: %d, Handle: %s, Parent: %s, Leaf: %d}", q.LinkIndex, HandleStr(q.Handle), HandleStr(q.Parent), q.Leaf)
|
||||
}
|
||||
|
||||
type HtbClassAttrs struct {
|
||||
@@ -38,7 +38,7 @@ func (q HtbClassAttrs) String() string {
|
||||
return fmt.Sprintf("{Rate: %d, Ceil: %d, Buffer: %d, Cbuffer: %d}", q.Rate, q.Ceil, q.Buffer, q.Cbuffer)
|
||||
}
|
||||
|
||||
// Htb class
|
||||
// HtbClass represents an Htb class
|
||||
type HtbClass struct {
|
||||
ClassAttrs
|
||||
Rate uint64
|
||||
@@ -87,11 +87,11 @@ func (q HtbClass) String() string {
|
||||
return fmt.Sprintf("{Rate: %d, Ceil: %d, Buffer: %d, Cbuffer: %d}", q.Rate, q.Ceil, q.Buffer, q.Cbuffer)
|
||||
}
|
||||
|
||||
func (class *HtbClass) Attrs() *ClassAttrs {
|
||||
return &class.ClassAttrs
|
||||
func (q *HtbClass) Attrs() *ClassAttrs {
|
||||
return &q.ClassAttrs
|
||||
}
|
||||
|
||||
func (class *HtbClass) Type() string {
|
||||
func (q *HtbClass) Type() string {
|
||||
return "htb"
|
||||
}
|
||||
|
||||
|
@@ -73,20 +73,20 @@ func classPayload(req *nl.NetlinkRequest, class Class) error {
|
||||
opt.Prio = htb.Prio
|
||||
// TODO: Handle Debug properly. For now default to 0
|
||||
/* Calculate {R,C}Tab and set Rate and Ceil */
|
||||
cell_log := -1
|
||||
ccell_log := -1
|
||||
cellLog := -1
|
||||
ccellLog := -1
|
||||
linklayer := nl.LINKLAYER_ETHERNET
|
||||
mtu := 1600
|
||||
var rtab [256]uint32
|
||||
var ctab [256]uint32
|
||||
tcrate := nl.TcRateSpec{Rate: uint32(htb.Rate)}
|
||||
if CalcRtable(&tcrate, rtab, cell_log, uint32(mtu), linklayer) < 0 {
|
||||
return errors.New("HTB: failed to calculate rate table.")
|
||||
if CalcRtable(&tcrate, rtab, cellLog, uint32(mtu), linklayer) < 0 {
|
||||
return errors.New("HTB: failed to calculate rate table")
|
||||
}
|
||||
opt.Rate = tcrate
|
||||
tcceil := nl.TcRateSpec{Rate: uint32(htb.Ceil)}
|
||||
if CalcRtable(&tcceil, ctab, ccell_log, uint32(mtu), linklayer) < 0 {
|
||||
return errors.New("HTB: failed to calculate ceil rate table.")
|
||||
if CalcRtable(&tcceil, ctab, ccellLog, uint32(mtu), linklayer) < 0 {
|
||||
return errors.New("HTB: failed to calculate ceil rate table")
|
||||
}
|
||||
opt.Ceil = tcceil
|
||||
nl.NewRtAttrChild(options, nl.TCA_HTB_PARMS, opt.Serialize())
|
||||
|
@@ -269,14 +269,14 @@ func TestHtbClassAddHtbClassChangeDel(t *testing.T) {
|
||||
// For change to work, the handle and parent cannot be changed.
|
||||
|
||||
// First, test it fails if we change the Handle.
|
||||
old_handle := classattrs.Handle
|
||||
oldHandle := classattrs.Handle
|
||||
classattrs.Handle = MakeHandle(0xffff, 3)
|
||||
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
|
||||
classattrs.Handle = oldHandle
|
||||
htbclassattrs.Rate = 4321000
|
||||
class = NewHtbClass(classattrs, htbclassattrs)
|
||||
if err := ClassChange(class); err != nil {
|
||||
|
16
filter.go
16
filter.go
@@ -11,7 +11,7 @@ type Filter interface {
|
||||
Type() string
|
||||
}
|
||||
|
||||
// Filter represents a netlink filter. A filter is associated with a link,
|
||||
// FilterAttrs represents a netlink filter. A filter is associated with a link,
|
||||
// has a handle and a parent. The root filter of a device should have a
|
||||
// parent == HANDLE_ROOT.
|
||||
type FilterAttrs struct {
|
||||
@@ -91,7 +91,7 @@ type FilterFwAttrs struct {
|
||||
LinkLayer int
|
||||
}
|
||||
|
||||
// FwFilter filters on firewall marks
|
||||
// Fw filter filters on firewall marks
|
||||
type Fw struct {
|
||||
FilterAttrs
|
||||
ClassId uint32
|
||||
@@ -107,8 +107,8 @@ type Fw struct {
|
||||
func NewFw(attrs FilterAttrs, fattrs FilterFwAttrs) (*Fw, error) {
|
||||
var rtab [256]uint32
|
||||
var ptab [256]uint32
|
||||
rcell_log := -1
|
||||
pcell_log := -1
|
||||
rcellLog := -1
|
||||
pcellLog := -1
|
||||
avrate := fattrs.AvRate / 8
|
||||
police := nl.TcPolice{}
|
||||
police.Rate.Rate = fattrs.Rate / 8
|
||||
@@ -124,8 +124,8 @@ func NewFw(attrs FilterAttrs, fattrs FilterFwAttrs) (*Fw, error) {
|
||||
if police.Rate.Rate != 0 {
|
||||
police.Rate.Mpu = fattrs.Mpu
|
||||
police.Rate.Overhead = fattrs.Overhead
|
||||
if CalcRtable(&police.Rate, rtab, rcell_log, fattrs.Mtu, linklayer) < 0 {
|
||||
return nil, errors.New("TBF: failed to calculate rate table.")
|
||||
if CalcRtable(&police.Rate, rtab, rcellLog, fattrs.Mtu, linklayer) < 0 {
|
||||
return nil, errors.New("TBF: failed to calculate rate table")
|
||||
}
|
||||
police.Burst = uint32(Xmittime(uint64(police.Rate.Rate), uint32(buffer)))
|
||||
}
|
||||
@@ -133,8 +133,8 @@ func NewFw(attrs FilterAttrs, fattrs FilterFwAttrs) (*Fw, error) {
|
||||
if police.PeakRate.Rate != 0 {
|
||||
police.PeakRate.Mpu = fattrs.Mpu
|
||||
police.PeakRate.Overhead = fattrs.Overhead
|
||||
if CalcRtable(&police.PeakRate, ptab, pcell_log, fattrs.Mtu, linklayer) < 0 {
|
||||
return nil, errors.New("POLICE: failed to calculate peak rate table.")
|
||||
if CalcRtable(&police.PeakRate, ptab, pcellLog, fattrs.Mtu, linklayer) < 0 {
|
||||
return nil, errors.New("POLICE: failed to calculate peak rate table")
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -91,7 +91,7 @@ func FilterAdd(filter Filter) error {
|
||||
nl.NewRtAttrChild(options, nl.TCA_FW_CLASSID, b)
|
||||
}
|
||||
} else if bpf, ok := filter.(*BpfFilter); ok {
|
||||
var bpf_flags uint32
|
||||
var bpfFlags uint32
|
||||
if bpf.ClassId != 0 {
|
||||
nl.NewRtAttrChild(options, nl.TCA_BPF_CLASSID, nl.Uint32Attr(bpf.ClassId))
|
||||
}
|
||||
@@ -102,9 +102,9 @@ func FilterAdd(filter Filter) error {
|
||||
nl.NewRtAttrChild(options, nl.TCA_BPF_NAME, nl.ZeroTerminated(bpf.Name))
|
||||
}
|
||||
if bpf.DirectAction {
|
||||
bpf_flags |= nl.TCA_BPF_FLAG_ACT_DIRECT
|
||||
bpfFlags |= nl.TCA_BPF_FLAG_ACT_DIRECT
|
||||
}
|
||||
nl.NewRtAttrChild(options, nl.TCA_BPF_FLAGS, nl.Uint32Attr(bpf_flags))
|
||||
nl.NewRtAttrChild(options, nl.TCA_BPF_FLAGS, nl.Uint32Attr(bpfFlags))
|
||||
}
|
||||
|
||||
req.AddData(options)
|
||||
@@ -385,27 +385,27 @@ func AdjustSize(sz uint, mpu uint, linklayer int) uint {
|
||||
}
|
||||
}
|
||||
|
||||
func CalcRtable(rate *nl.TcRateSpec, rtab [256]uint32, cell_log int, mtu uint32, linklayer int) int {
|
||||
func CalcRtable(rate *nl.TcRateSpec, rtab [256]uint32, cellLog int, mtu uint32, linklayer int) int {
|
||||
bps := rate.Rate
|
||||
mpu := rate.Mpu
|
||||
var sz uint
|
||||
if mtu == 0 {
|
||||
mtu = 2047
|
||||
}
|
||||
if cell_log < 0 {
|
||||
cell_log = 0
|
||||
for (mtu >> uint(cell_log)) > 255 {
|
||||
cell_log++
|
||||
if cellLog < 0 {
|
||||
cellLog = 0
|
||||
for (mtu >> uint(cellLog)) > 255 {
|
||||
cellLog++
|
||||
}
|
||||
}
|
||||
for i := 0; i < 256; i++ {
|
||||
sz = AdjustSize(uint((i+1)<<uint32(cell_log)), uint(mpu), linklayer)
|
||||
sz = AdjustSize(uint((i+1)<<uint32(cellLog)), uint(mpu), linklayer)
|
||||
rtab[i] = uint32(Xmittime(uint64(bps), uint32(sz)))
|
||||
}
|
||||
rate.CellAlign = -1
|
||||
rate.CellLog = uint8(cell_log)
|
||||
rate.CellLog = uint8(cellLog)
|
||||
rate.Linklayer = uint8(linklayer & nl.TC_LINKLAYER_MASK)
|
||||
return cell_log
|
||||
return cellLog
|
||||
}
|
||||
|
||||
func DeserializeRtab(b []byte) [256]uint32 {
|
||||
|
4
link.go
4
link.go
@@ -425,7 +425,7 @@ const (
|
||||
BOND_AD_SELECT_COUNT
|
||||
)
|
||||
|
||||
// BondAdInfo
|
||||
// BondAdInfo represents ad info for bond
|
||||
type BondAdInfo struct {
|
||||
AggregatorId int
|
||||
NumPorts int
|
||||
@@ -526,7 +526,7 @@ func (bond *Bond) Type() string {
|
||||
return "bond"
|
||||
}
|
||||
|
||||
// GreTap devices must specify LocalIP and RemoteIP on create
|
||||
// Gretap devices must specify LocalIP and RemoteIP on create
|
||||
type Gretap struct {
|
||||
LinkAttrs
|
||||
IKey uint32
|
||||
|
@@ -72,7 +72,7 @@ func testLinkAddDel(t *testing.T, link Link) {
|
||||
} else {
|
||||
// recent kernels set the parent index for veths in the response
|
||||
if rBase.ParentIndex == 0 && base.ParentIndex != 0 {
|
||||
t.Fatal("Created link doesn't have parent %d but it should", base.ParentIndex)
|
||||
t.Fatalf("Created link doesn't have parent %d but it should", base.ParentIndex)
|
||||
} else if rBase.ParentIndex != 0 && base.ParentIndex == 0 {
|
||||
t.Fatalf("Created link has parent %d but it shouldn't", rBase.ParentIndex)
|
||||
} else if rBase.ParentIndex != 0 && base.ParentIndex != 0 {
|
||||
|
@@ -14,8 +14,8 @@ import (
|
||||
"github.com/vishvananda/netlink/nl"
|
||||
)
|
||||
|
||||
// Family type definitions
|
||||
const (
|
||||
// Family type definitions
|
||||
FAMILY_ALL = nl.FAMILY_ALL
|
||||
FAMILY_V4 = nl.FAMILY_V4
|
||||
FAMILY_V6 = nl.FAMILY_V6
|
||||
|
42
qdisc.go
42
qdisc.go
@@ -22,7 +22,7 @@ type Qdisc interface {
|
||||
Type() string
|
||||
}
|
||||
|
||||
// Qdisc represents a netlink qdisc. A qdisc is associated with a link,
|
||||
// QdiscAttrs represents a netlink qdisc. A qdisc is associated with a link,
|
||||
// has a handle, a parent and a refcnt. The root qdisc of a device should
|
||||
// have parent == HANDLE_ROOT.
|
||||
type QdiscAttrs struct {
|
||||
@@ -33,7 +33,7 @@ type QdiscAttrs struct {
|
||||
}
|
||||
|
||||
func (q QdiscAttrs) String() string {
|
||||
return fmt.Sprintf("{LinkIndex: %d, Handle: %s, Parent: %s, Refcnt: %s}", q.LinkIndex, HandleStr(q.Handle), HandleStr(q.Parent), q.Refcnt)
|
||||
return fmt.Sprintf("{LinkIndex: %d, Handle: %s, Parent: %s, Refcnt: %d}", q.LinkIndex, HandleStr(q.Handle), HandleStr(q.Parent), q.Refcnt)
|
||||
}
|
||||
|
||||
func MakeHandle(major, minor uint16) uint32 {
|
||||
@@ -154,7 +154,7 @@ type NetemQdiscAttrs struct {
|
||||
|
||||
func (q NetemQdiscAttrs) String() string {
|
||||
return fmt.Sprintf(
|
||||
"{Latency: %d, Limit: %d, Loss: %d, Gap: %d, Duplicate: %d, Jitter: %d}",
|
||||
"{Latency: %d, Limit: %d, Loss: %f, Gap: %d, Duplicate: %f, Jitter: %d}",
|
||||
q.Latency, q.Limit, q.Loss, q.Gap, q.Duplicate, q.Jitter,
|
||||
)
|
||||
}
|
||||
@@ -178,9 +178,9 @@ type Netem struct {
|
||||
|
||||
func NewNetem(attrs QdiscAttrs, nattrs NetemQdiscAttrs) *Netem {
|
||||
var limit uint32 = 1000
|
||||
var loss_corr, delay_corr, duplicate_corr uint32
|
||||
var reorder_prob, reorder_corr uint32
|
||||
var corrupt_prob, corrupt_corr uint32
|
||||
var lossCorr, delayCorr, duplicateCorr uint32
|
||||
var reorderProb, reorderCorr uint32
|
||||
var corruptProb, corruptCorr uint32
|
||||
|
||||
latency := nattrs.Latency
|
||||
loss := Percentage2u32(nattrs.Loss)
|
||||
@@ -190,13 +190,13 @@ func NewNetem(attrs QdiscAttrs, nattrs NetemQdiscAttrs) *Netem {
|
||||
|
||||
// Correlation
|
||||
if latency > 0 && jitter > 0 {
|
||||
delay_corr = Percentage2u32(nattrs.DelayCorr)
|
||||
delayCorr = Percentage2u32(nattrs.DelayCorr)
|
||||
}
|
||||
if loss > 0 {
|
||||
loss_corr = Percentage2u32(nattrs.LossCorr)
|
||||
lossCorr = Percentage2u32(nattrs.LossCorr)
|
||||
}
|
||||
if duplicate > 0 {
|
||||
duplicate_corr = Percentage2u32(nattrs.DuplicateCorr)
|
||||
duplicateCorr = Percentage2u32(nattrs.DuplicateCorr)
|
||||
}
|
||||
// FIXME should validate values(like loss/duplicate are percentages...)
|
||||
latency = time2Tick(latency)
|
||||
@@ -209,34 +209,34 @@ func NewNetem(attrs QdiscAttrs, nattrs NetemQdiscAttrs) *Netem {
|
||||
jitter = time2Tick(jitter)
|
||||
}
|
||||
|
||||
reorder_prob = Percentage2u32(nattrs.ReorderProb)
|
||||
reorder_corr = Percentage2u32(nattrs.ReorderCorr)
|
||||
reorderProb = Percentage2u32(nattrs.ReorderProb)
|
||||
reorderCorr = Percentage2u32(nattrs.ReorderCorr)
|
||||
|
||||
if reorder_prob > 0 {
|
||||
if reorderProb > 0 {
|
||||
// ERROR if lantency == 0
|
||||
if gap == 0 {
|
||||
gap = 1
|
||||
}
|
||||
}
|
||||
|
||||
corrupt_prob = Percentage2u32(nattrs.CorruptProb)
|
||||
corrupt_corr = Percentage2u32(nattrs.CorruptCorr)
|
||||
corruptProb = Percentage2u32(nattrs.CorruptProb)
|
||||
corruptCorr = Percentage2u32(nattrs.CorruptCorr)
|
||||
|
||||
return &Netem{
|
||||
QdiscAttrs: attrs,
|
||||
Latency: latency,
|
||||
DelayCorr: delay_corr,
|
||||
DelayCorr: delayCorr,
|
||||
Limit: limit,
|
||||
Loss: loss,
|
||||
LossCorr: loss_corr,
|
||||
LossCorr: lossCorr,
|
||||
Gap: gap,
|
||||
Duplicate: duplicate,
|
||||
DuplicateCorr: duplicate_corr,
|
||||
DuplicateCorr: duplicateCorr,
|
||||
Jitter: jitter,
|
||||
ReorderProb: reorder_prob,
|
||||
ReorderCorr: reorder_corr,
|
||||
CorruptProb: corrupt_prob,
|
||||
CorruptCorr: corrupt_corr,
|
||||
ReorderProb: reorderProb,
|
||||
ReorderCorr: reorderCorr,
|
||||
CorruptProb: corruptProb,
|
||||
CorruptCorr: corruptCorr,
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -334,9 +334,9 @@ const (
|
||||
)
|
||||
|
||||
var (
|
||||
tickInUsec float64 = 0.0
|
||||
clockFactor float64 = 0.0
|
||||
hz float64 = 0.0
|
||||
tickInUsec float64
|
||||
clockFactor float64
|
||||
hz float64
|
||||
)
|
||||
|
||||
func initClock() {
|
||||
|
@@ -10,7 +10,7 @@ func TestRuleAddDel(t *testing.T) {
|
||||
srcNet := &net.IPNet{IP: net.IPv4(172, 16, 0, 1), Mask: net.CIDRMask(16, 32)}
|
||||
dstNet := &net.IPNet{IP: net.IPv4(172, 16, 1, 1), Mask: net.CIDRMask(24, 32)}
|
||||
|
||||
rules_begin, err := RuleList(syscall.AF_INET)
|
||||
rulesBegin, err := RuleList(syscall.AF_INET)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -31,7 +31,7 @@ func TestRuleAddDel(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(rules) != len(rules_begin)+1 {
|
||||
if len(rules) != len(rulesBegin)+1 {
|
||||
t.Fatal("Rule not added properly")
|
||||
}
|
||||
|
||||
@@ -55,12 +55,12 @@ func TestRuleAddDel(t *testing.T) {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
rules_end, err := RuleList(syscall.AF_INET)
|
||||
rulesEnd, err := RuleList(syscall.AF_INET)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(rules_end) != len(rules_begin) {
|
||||
if len(rulesEnd) != len(rulesBegin) {
|
||||
t.Fatal("Rule not removed properly")
|
||||
}
|
||||
}
|
||||
|
@@ -29,7 +29,7 @@ func (e EncapType) String() string {
|
||||
return "unknown"
|
||||
}
|
||||
|
||||
// XfrmEncap represents the encapsulation to use for the ipsec encryption.
|
||||
// XfrmStateEncap represents the encapsulation to use for the ipsec encryption.
|
||||
type XfrmStateEncap struct {
|
||||
Type EncapType
|
||||
SrcPort int
|
||||
|
Reference in New Issue
Block a user