Skip to content

Commit

Permalink
fix darwin build by moving code (vishvananda#138)
Browse files Browse the repository at this point in the history
Fixes issue vishvananda#135
  • Loading branch information
vishvananda authored Jun 15, 2016
1 parent 01c64f1 commit 0bc457d
Show file tree
Hide file tree
Showing 19 changed files with 226 additions and 228 deletions.
33 changes: 0 additions & 33 deletions class.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,39 +50,6 @@ type HtbClass struct {
Prio uint32
}

func NewHtbClass(attrs ClassAttrs, cattrs HtbClassAttrs) *HtbClass {
mtu := 1600
rate := cattrs.Rate / 8
ceil := cattrs.Ceil / 8
buffer := cattrs.Buffer
cbuffer := cattrs.Cbuffer

if ceil == 0 {
ceil = rate
}

if buffer == 0 {
buffer = uint32(float64(rate)/Hz() + float64(mtu))
}
buffer = uint32(Xmittime(rate, buffer))

if cbuffer == 0 {
cbuffer = uint32(float64(ceil)/Hz() + float64(mtu))
}
cbuffer = uint32(Xmittime(ceil, cbuffer))

return &HtbClass{
ClassAttrs: attrs,
Rate: rate,
Ceil: ceil,
Buffer: buffer,
Cbuffer: cbuffer,
Quantum: 10,
Level: 0,
Prio: 0,
}
}

func (q HtbClass) String() string {
return fmt.Sprintf("{Rate: %d, Ceil: %d, Buffer: %d, Cbuffer: %d}", q.Rate, q.Ceil, q.Buffer, q.Cbuffer)
}
Expand Down
34 changes: 34 additions & 0 deletions class_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,40 @@ import (
"github.com/vishvananda/netlink/nl"
)

// NOTE: function is in here because it uses other linux functions
func NewHtbClass(attrs ClassAttrs, cattrs HtbClassAttrs) *HtbClass {
mtu := 1600
rate := cattrs.Rate / 8
ceil := cattrs.Ceil / 8
buffer := cattrs.Buffer
cbuffer := cattrs.Cbuffer

if ceil == 0 {
ceil = rate
}

if buffer == 0 {
buffer = uint32(float64(rate)/Hz() + float64(mtu))
}
buffer = uint32(Xmittime(rate, buffer))

if cbuffer == 0 {
cbuffer = uint32(float64(ceil)/Hz() + float64(mtu))
}
cbuffer = uint32(Xmittime(ceil, cbuffer))

return &HtbClass{
ClassAttrs: attrs,
Rate: rate,
Ceil: ceil,
Buffer: buffer,
Cbuffer: cbuffer,
Quantum: 10,
Level: 0,
Prio: 0,
}
}

// ClassDel will delete a class from the system.
// Equivalent to: `tc class del $class`
func ClassDel(class Class) error {
Expand Down
75 changes: 1 addition & 74 deletions filter.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package netlink

import (
"errors"
"fmt"

"github.com/vishvananda/netlink/nl"
)
import "fmt"

type Filter interface {
Attrs() *FilterAttrs
Expand Down Expand Up @@ -217,74 +212,6 @@ type FilterFwAttrs struct {
LinkLayer int
}

// Fw filter filters on firewall marks
type Fw struct {
FilterAttrs
ClassId uint32
// TODO remove nl type from interface
Police nl.TcPolice
InDev string
// TODO Action
Mask uint32
AvRate uint32
Rtab [256]uint32
Ptab [256]uint32
}

func NewFw(attrs FilterAttrs, fattrs FilterFwAttrs) (*Fw, error) {
var rtab [256]uint32
var ptab [256]uint32
rcellLog := -1
pcellLog := -1
avrate := fattrs.AvRate / 8
police := nl.TcPolice{}
police.Rate.Rate = fattrs.Rate / 8
police.PeakRate.Rate = fattrs.PeakRate / 8
buffer := fattrs.Buffer
linklayer := nl.LINKLAYER_ETHERNET

if fattrs.LinkLayer != nl.LINKLAYER_UNSPEC {
linklayer = fattrs.LinkLayer
}

police.Action = int32(fattrs.Action)
if police.Rate.Rate != 0 {
police.Rate.Mpu = fattrs.Mpu
police.Rate.Overhead = fattrs.Overhead
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)))
}
police.Mtu = fattrs.Mtu
if police.PeakRate.Rate != 0 {
police.PeakRate.Mpu = fattrs.Mpu
police.PeakRate.Overhead = fattrs.Overhead
if CalcRtable(&police.PeakRate, ptab, pcellLog, fattrs.Mtu, linklayer) < 0 {
return nil, errors.New("POLICE: failed to calculate peak rate table")
}
}

return &Fw{
FilterAttrs: attrs,
ClassId: fattrs.ClassId,
InDev: fattrs.InDev,
Mask: fattrs.Mask,
Police: police,
AvRate: avrate,
Rtab: rtab,
Ptab: ptab,
}, nil
}

func (filter *Fw) Attrs() *FilterAttrs {
return &filter.FilterAttrs
}

func (filter *Fw) Type() string {
return "fw"
}

type BpfFilter struct {
FilterAttrs
ClassId uint32
Expand Down
71 changes: 71 additions & 0 deletions filter_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,83 @@ package netlink
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"syscall"

"github.com/vishvananda/netlink/nl"
)

// Fw filter filters on firewall marks
// NOTE: this is in filter_linux because it refers to nl.TcPolice which
// is defined in nl/tc_linux.go
type Fw struct {
FilterAttrs
ClassId uint32
// TODO remove nl type from interface
Police nl.TcPolice
InDev string
// TODO Action
Mask uint32
AvRate uint32
Rtab [256]uint32
Ptab [256]uint32
}

func NewFw(attrs FilterAttrs, fattrs FilterFwAttrs) (*Fw, error) {
var rtab [256]uint32
var ptab [256]uint32
rcellLog := -1
pcellLog := -1
avrate := fattrs.AvRate / 8
police := nl.TcPolice{}
police.Rate.Rate = fattrs.Rate / 8
police.PeakRate.Rate = fattrs.PeakRate / 8
buffer := fattrs.Buffer
linklayer := nl.LINKLAYER_ETHERNET

if fattrs.LinkLayer != nl.LINKLAYER_UNSPEC {
linklayer = fattrs.LinkLayer
}

police.Action = int32(fattrs.Action)
if police.Rate.Rate != 0 {
police.Rate.Mpu = fattrs.Mpu
police.Rate.Overhead = fattrs.Overhead
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)))
}
police.Mtu = fattrs.Mtu
if police.PeakRate.Rate != 0 {
police.PeakRate.Mpu = fattrs.Mpu
police.PeakRate.Overhead = fattrs.Overhead
if CalcRtable(&police.PeakRate, ptab, pcellLog, fattrs.Mtu, linklayer) < 0 {
return nil, errors.New("POLICE: failed to calculate peak rate table")
}
}

return &Fw{
FilterAttrs: attrs,
ClassId: fattrs.ClassId,
InDev: fattrs.InDev,
Mask: fattrs.Mask,
Police: police,
AvRate: avrate,
Rtab: rtab,
Ptab: ptab,
}, nil
}

func (filter *Fw) Attrs() *FilterAttrs {
return &filter.FilterAttrs
}

func (filter *Fw) Type() string {
return "fw"
}

// FilterDel will delete a filter from the system.
// Equivalent to: `tc filter del $filter`
func FilterDel(filter Filter) error {
Expand Down
File renamed without changes.
6 changes: 0 additions & 6 deletions link.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package netlink
import (
"fmt"
"net"
"syscall"
)

// Link represents a link device from netlink. Shared link attributes
Expand Down Expand Up @@ -173,11 +172,6 @@ func (macvtap Macvtap) Type() string {

type TuntapMode uint16

const (
TUNTAP_MODE_TUN TuntapMode = syscall.IFF_TUN
TUNTAP_MODE_TAP TuntapMode = syscall.IFF_TAP
)

// Tuntap links created via /dev/tun/tap, but can be destroyed via netlink
type Tuntap struct {
LinkAttrs
Expand Down
5 changes: 5 additions & 0 deletions link_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ import (

const SizeofLinkStats = 0x5c

const (
TUNTAP_MODE_TUN TuntapMode = syscall.IFF_TUN
TUNTAP_MODE_TAP TuntapMode = syscall.IFF_TAP
)

var native = nl.NativeEndian()
var lookupByDump = false

Expand Down
13 changes: 1 addition & 12 deletions netlink.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,7 @@
// interface that is loosly modeled on the iproute2 cli.
package netlink

import (
"net"

"github.com/vishvananda/netlink/nl"
)

// Family type definitions
const (
FAMILY_ALL = nl.FAMILY_ALL
FAMILY_V4 = nl.FAMILY_V4
FAMILY_V6 = nl.FAMILY_V6
)
import "net"

// ParseIPNet parses a string in ip/net format and returns a net.IPNet.
// This is valuable because addresses in netlink are often IPNets and
Expand Down
10 changes: 10 additions & 0 deletions netlink_linux.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package netlink

import "github.com/vishvananda/netlink/nl"

// Family type definitions
const (
FAMILY_ALL = nl.FAMILY_ALL
FAMILY_V4 = nl.FAMILY_V4
FAMILY_V6 = nl.FAMILY_V6
)
4 changes: 2 additions & 2 deletions netlink_unspecified.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,6 @@ func NeighList(linkIndex, family int) ([]Neigh, error) {
return nil, ErrNotImplemented
}

func NeighDeserialize(m []byte) (*Ndmsg, *Neigh, error) {
return nil, nil, ErrNotImplemented
func NeighDeserialize(m []byte) (*Neigh, error) {
return nil, ErrNotImplemented
}
64 changes: 0 additions & 64 deletions qdisc.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,70 +176,6 @@ type Netem struct {
CorruptCorr uint32
}

func NewNetem(attrs QdiscAttrs, nattrs NetemQdiscAttrs) *Netem {
var limit uint32 = 1000
var lossCorr, delayCorr, duplicateCorr uint32
var reorderProb, reorderCorr uint32
var corruptProb, corruptCorr uint32

latency := nattrs.Latency
loss := Percentage2u32(nattrs.Loss)
gap := nattrs.Gap
duplicate := Percentage2u32(nattrs.Duplicate)
jitter := nattrs.Jitter

// Correlation
if latency > 0 && jitter > 0 {
delayCorr = Percentage2u32(nattrs.DelayCorr)
}
if loss > 0 {
lossCorr = Percentage2u32(nattrs.LossCorr)
}
if duplicate > 0 {
duplicateCorr = Percentage2u32(nattrs.DuplicateCorr)
}
// FIXME should validate values(like loss/duplicate are percentages...)
latency = time2Tick(latency)

if nattrs.Limit != 0 {
limit = nattrs.Limit
}
// Jitter is only value if latency is > 0
if latency > 0 {
jitter = time2Tick(jitter)
}

reorderProb = Percentage2u32(nattrs.ReorderProb)
reorderCorr = Percentage2u32(nattrs.ReorderCorr)

if reorderProb > 0 {
// ERROR if lantency == 0
if gap == 0 {
gap = 1
}
}

corruptProb = Percentage2u32(nattrs.CorruptProb)
corruptCorr = Percentage2u32(nattrs.CorruptCorr)

return &Netem{
QdiscAttrs: attrs,
Latency: latency,
DelayCorr: delayCorr,
Limit: limit,
Loss: loss,
LossCorr: lossCorr,
Gap: gap,
Duplicate: duplicate,
DuplicateCorr: duplicateCorr,
Jitter: jitter,
ReorderProb: reorderProb,
ReorderCorr: reorderCorr,
CorruptProb: corruptProb,
CorruptCorr: corruptCorr,
}
}

func (qdisc *Netem) Attrs() *QdiscAttrs {
return &qdisc.QdiscAttrs
}
Expand Down
Loading

0 comments on commit 0bc457d

Please sign in to comment.