2015-08-19 23:02:04 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
2015-09-12 06:24:22 +00:00
|
|
|
"errors"
|
2015-08-19 23:02:04 +00:00
|
|
|
"fmt"
|
2015-09-12 06:24:22 +00:00
|
|
|
"github.com/vishvananda/netlink/nl"
|
2015-08-19 23:02:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Filter interface {
|
|
|
|
Attrs() *FilterAttrs
|
|
|
|
Type() string
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:12:26 +00:00
|
|
|
// FilterAttrs represents a netlink filter. A filter is associated with a link,
|
2015-08-19 23:02:04 +00:00
|
|
|
// has a handle and a parent. The root filter of a device should have a
|
|
|
|
// parent == HANDLE_ROOT.
|
|
|
|
type FilterAttrs struct {
|
|
|
|
LinkIndex int
|
|
|
|
Handle uint32
|
|
|
|
Parent uint32
|
|
|
|
Priority uint16 // lower is higher priority
|
|
|
|
Protocol uint16 // syscall.ETH_P_*
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q FilterAttrs) String() string {
|
|
|
|
return fmt.Sprintf("{LinkIndex: %d, Handle: %s, Parent: %s, Priority: %d, Protocol: %d}", q.LinkIndex, HandleStr(q.Handle), HandleStr(q.Parent), q.Priority, q.Protocol)
|
|
|
|
}
|
|
|
|
|
2016-02-29 07:12:34 +00:00
|
|
|
// Action represents an action in any supported filter.
|
|
|
|
type Action interface {
|
|
|
|
Type() string
|
|
|
|
}
|
|
|
|
|
|
|
|
type BpfAction struct {
|
|
|
|
nl.TcActBpf
|
|
|
|
Fd int
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (action *BpfAction) Type() string {
|
|
|
|
return "bpf"
|
|
|
|
}
|
|
|
|
|
|
|
|
type MirredAction struct {
|
|
|
|
nl.TcMirred
|
|
|
|
}
|
|
|
|
|
|
|
|
func (action *MirredAction) Type() string {
|
|
|
|
return "mirred"
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMirredAction(redirIndex int) *MirredAction {
|
|
|
|
return &MirredAction{
|
|
|
|
TcMirred: nl.TcMirred{
|
|
|
|
TcGen: nl.TcGen{Action: nl.TC_ACT_STOLEN},
|
|
|
|
Eaction: nl.TCA_EGRESS_REDIR,
|
|
|
|
Ifindex: uint32(redirIndex),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-19 23:02:04 +00:00
|
|
|
// U32 filters on many packet related properties
|
|
|
|
type U32 struct {
|
|
|
|
FilterAttrs
|
2016-02-29 07:12:34 +00:00
|
|
|
ClassId uint32
|
2015-08-19 23:02:04 +00:00
|
|
|
RedirIndex int
|
2016-02-29 07:12:34 +00:00
|
|
|
Actions []Action
|
2015-08-19 23:02:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (filter *U32) Attrs() *FilterAttrs {
|
|
|
|
return &filter.FilterAttrs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (filter *U32) Type() string {
|
|
|
|
return "u32"
|
|
|
|
}
|
|
|
|
|
2015-09-12 06:24:22 +00:00
|
|
|
type FilterFwAttrs struct {
|
|
|
|
ClassId uint32
|
|
|
|
InDev string
|
|
|
|
Mask uint32
|
|
|
|
Index uint32
|
|
|
|
Buffer uint32
|
|
|
|
Mtu uint32
|
|
|
|
Mpu uint16
|
|
|
|
Rate uint32
|
|
|
|
AvRate uint32
|
|
|
|
PeakRate uint32
|
2015-10-05 05:34:02 +00:00
|
|
|
Action int
|
2015-09-12 06:24:22 +00:00
|
|
|
Overhead uint16
|
|
|
|
LinkLayer int
|
|
|
|
}
|
|
|
|
|
2016-03-20 00:12:26 +00:00
|
|
|
// Fw filter filters on firewall marks
|
2015-09-12 06:24:22 +00:00
|
|
|
type Fw struct {
|
|
|
|
FilterAttrs
|
|
|
|
ClassId uint32
|
|
|
|
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
|
2016-03-20 00:12:26 +00:00
|
|
|
rcellLog := -1
|
|
|
|
pcellLog := -1
|
2015-09-12 06:24:22 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2015-10-05 05:34:02 +00:00
|
|
|
police.Action = int32(fattrs.Action)
|
2015-09-12 06:24:22 +00:00
|
|
|
if police.Rate.Rate != 0 {
|
|
|
|
police.Rate.Mpu = fattrs.Mpu
|
|
|
|
police.Rate.Overhead = fattrs.Overhead
|
2016-03-20 00:12:26 +00:00
|
|
|
if CalcRtable(&police.Rate, rtab, rcellLog, fattrs.Mtu, linklayer) < 0 {
|
|
|
|
return nil, errors.New("TBF: failed to calculate rate table")
|
2015-09-12 06:24:22 +00:00
|
|
|
}
|
|
|
|
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
|
2016-03-20 00:12:26 +00:00
|
|
|
if CalcRtable(&police.PeakRate, ptab, pcellLog, fattrs.Mtu, linklayer) < 0 {
|
|
|
|
return nil, errors.New("POLICE: failed to calculate peak rate table")
|
2015-09-12 06:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
}
|
|
|
|
|
2016-02-29 07:20:29 +00:00
|
|
|
type BpfFilter struct {
|
|
|
|
FilterAttrs
|
|
|
|
ClassId uint32
|
|
|
|
Fd int
|
|
|
|
Name string
|
|
|
|
DirectAction bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (filter *BpfFilter) Type() string {
|
|
|
|
return "bpf"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (filter *BpfFilter) Attrs() *FilterAttrs {
|
|
|
|
return &filter.FilterAttrs
|
|
|
|
}
|
|
|
|
|
2015-08-19 23:02:04 +00:00
|
|
|
// GenericFilter filters represent types that are not currently understood
|
|
|
|
// by this netlink library.
|
|
|
|
type GenericFilter struct {
|
|
|
|
FilterAttrs
|
|
|
|
FilterType string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (filter *GenericFilter) Attrs() *FilterAttrs {
|
|
|
|
return &filter.FilterAttrs
|
|
|
|
}
|
|
|
|
|
|
|
|
func (filter *GenericFilter) Type() string {
|
|
|
|
return filter.FilterType
|
|
|
|
}
|