6b600f8c8d
Storage + CRUD (migration 0008, model, id-keyed store, REST handlers) + compiler rendering for mangle, accounting, and tc_devices/tc_classes/tc_filters/ tc_interfaces/tc_priorities, each owned by a device. (Nested tc option structs deferred.)
148 lines
6.3 KiB
Go
148 lines
6.3 KiB
Go
package compiler
|
|
|
|
// Traffic-control tier rendered into a device's config.
|
|
|
|
type RenderedMangle struct {
|
|
Action string `yaml:"action" json:"action"`
|
|
Chain string `yaml:"chain,omitempty" json:"chain,omitempty"`
|
|
MarkValue string `yaml:"mark_value,omitempty" json:"mark_value,omitempty"`
|
|
Source string `yaml:"source,omitempty" json:"source,omitempty"`
|
|
Dest string `yaml:"dest,omitempty" json:"dest,omitempty"`
|
|
Proto string `yaml:"proto,omitempty" json:"proto,omitempty"`
|
|
DPort []string `yaml:"dport,omitempty" json:"dport,omitempty"`
|
|
SPort []string `yaml:"sport,omitempty" json:"sport,omitempty"`
|
|
User string `yaml:"user,omitempty" json:"user,omitempty"`
|
|
Mark string `yaml:"mark,omitempty" json:"mark,omitempty"`
|
|
Length string `yaml:"length,omitempty" json:"length,omitempty"`
|
|
TOS string `yaml:"tos,omitempty" json:"tos,omitempty"`
|
|
Helper string `yaml:"helper,omitempty" json:"helper,omitempty"`
|
|
Probability *float64 `yaml:"probability,omitempty" json:"probability,omitempty"`
|
|
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
|
|
}
|
|
|
|
type RenderedAccounting struct {
|
|
Action string `yaml:"action" json:"action"`
|
|
Section string `yaml:"section,omitempty" json:"section,omitempty"`
|
|
Chain string `yaml:"chain,omitempty" json:"chain,omitempty"`
|
|
Source string `yaml:"source,omitempty" json:"source,omitempty"`
|
|
Dest string `yaml:"dest,omitempty" json:"dest,omitempty"`
|
|
Proto string `yaml:"proto,omitempty" json:"proto,omitempty"`
|
|
DPort []string `yaml:"dport,omitempty" json:"dport,omitempty"`
|
|
SPort []string `yaml:"sport,omitempty" json:"sport,omitempty"`
|
|
Mark string `yaml:"mark,omitempty" json:"mark,omitempty"`
|
|
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
|
|
}
|
|
|
|
type RenderedTCDevice struct {
|
|
Interface string `yaml:"interface" json:"interface"`
|
|
InBandwidth string `yaml:"in_bandwidth,omitempty" json:"in_bandwidth,omitempty"`
|
|
OutBandwidth string `yaml:"out_bandwidth,omitempty" json:"out_bandwidth,omitempty"`
|
|
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
|
|
}
|
|
|
|
type RenderedTCClass struct {
|
|
Interface string `yaml:"interface" json:"interface"`
|
|
Mark int `yaml:"mark,omitempty" json:"mark,omitempty"`
|
|
Rate string `yaml:"rate,omitempty" json:"rate,omitempty"`
|
|
Ceil string `yaml:"ceil,omitempty" json:"ceil,omitempty"`
|
|
Priority int `yaml:"priority,omitempty" json:"priority,omitempty"`
|
|
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
|
|
}
|
|
|
|
type RenderedTCFilter struct {
|
|
Class string `yaml:"class" json:"class"`
|
|
Source string `yaml:"source,omitempty" json:"source,omitempty"`
|
|
Dest string `yaml:"dest,omitempty" json:"dest,omitempty"`
|
|
Proto string `yaml:"proto,omitempty" json:"proto,omitempty"`
|
|
DPort []string `yaml:"dport,omitempty" json:"dport,omitempty"`
|
|
SPort []string `yaml:"sport,omitempty" json:"sport,omitempty"`
|
|
TOS string `yaml:"tos,omitempty" json:"tos,omitempty"`
|
|
Length int `yaml:"length,omitempty" json:"length,omitempty"`
|
|
Priority int `yaml:"priority,omitempty" json:"priority,omitempty"`
|
|
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
|
|
}
|
|
|
|
type RenderedTCInterface struct {
|
|
Interface string `yaml:"interface" json:"interface"`
|
|
Type string `yaml:"type,omitempty" json:"type,omitempty"`
|
|
InBandwidth string `yaml:"in_bandwidth,omitempty" json:"in_bandwidth,omitempty"`
|
|
OutBandwidth string `yaml:"out_bandwidth,omitempty" json:"out_bandwidth,omitempty"`
|
|
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
|
|
}
|
|
|
|
type RenderedTCPriority struct {
|
|
Band int `yaml:"band" json:"band"`
|
|
Proto string `yaml:"proto,omitempty" json:"proto,omitempty"`
|
|
DPort []string `yaml:"dport,omitempty" json:"dport,omitempty"`
|
|
SPort []string `yaml:"sport,omitempty" json:"sport,omitempty"`
|
|
Address string `yaml:"address,omitempty" json:"address,omitempty"`
|
|
Interface string `yaml:"interface,omitempty" json:"interface,omitempty"`
|
|
Helper string `yaml:"helper,omitempty" json:"helper,omitempty"`
|
|
Comment string `yaml:"comment,omitempty" json:"comment,omitempty"`
|
|
}
|
|
|
|
func renderTraffic(in Input, out *RenderedConfig) {
|
|
dev := in.Device.Name
|
|
for _, m := range in.Mangle {
|
|
if m.Device != dev {
|
|
continue
|
|
}
|
|
out.Mangle = append(out.Mangle, RenderedMangle{
|
|
Action: m.Action, Chain: m.Chain, MarkValue: m.MarkValue, Source: m.Source, Dest: m.Dest,
|
|
Proto: m.Proto, DPort: m.DPort, SPort: m.SPort, User: m.User, Mark: m.Mark,
|
|
Length: m.Length, TOS: m.TOS, Helper: m.Helper, Probability: m.Probability, Comment: m.Comment,
|
|
})
|
|
}
|
|
for _, a := range in.Accounting {
|
|
if a.Device != dev {
|
|
continue
|
|
}
|
|
out.Accounting = append(out.Accounting, RenderedAccounting{
|
|
Action: a.Action, Section: a.Section, Chain: a.Chain, Source: a.Source, Dest: a.Dest,
|
|
Proto: a.Proto, DPort: a.DPort, SPort: a.SPort, Mark: a.Mark, Comment: a.Comment,
|
|
})
|
|
}
|
|
for _, t := range in.TCDevices {
|
|
if t.Device != dev {
|
|
continue
|
|
}
|
|
out.TCDevices = append(out.TCDevices, RenderedTCDevice{
|
|
Interface: t.Interface, InBandwidth: t.InBandwidth, OutBandwidth: t.OutBandwidth, Comment: t.Comment,
|
|
})
|
|
}
|
|
for _, t := range in.TCClasses {
|
|
if t.Device != dev {
|
|
continue
|
|
}
|
|
out.TCClasses = append(out.TCClasses, RenderedTCClass{
|
|
Interface: t.Interface, Mark: t.Mark, Rate: t.Rate, Ceil: t.Ceil, Priority: t.Priority, Comment: t.Comment,
|
|
})
|
|
}
|
|
for _, t := range in.TCFilters {
|
|
if t.Device != dev {
|
|
continue
|
|
}
|
|
out.TCFilters = append(out.TCFilters, RenderedTCFilter{
|
|
Class: t.Class, Source: t.Source, Dest: t.Dest, Proto: t.Proto, DPort: t.DPort, SPort: t.SPort,
|
|
TOS: t.TOS, Length: t.Length, Priority: t.Priority, Comment: t.Comment,
|
|
})
|
|
}
|
|
for _, t := range in.TCInterfaces {
|
|
if t.Device != dev {
|
|
continue
|
|
}
|
|
out.TCInterfaces = append(out.TCInterfaces, RenderedTCInterface{
|
|
Interface: t.Interface, Type: t.Type, InBandwidth: t.InBandwidth, OutBandwidth: t.OutBandwidth, Comment: t.Comment,
|
|
})
|
|
}
|
|
for _, t := range in.TCPriorities {
|
|
if t.Device != dev {
|
|
continue
|
|
}
|
|
out.TCPriorities = append(out.TCPriorities, RenderedTCPriority{
|
|
Band: t.Band, Proto: t.Proto, DPort: t.DPort, SPort: t.SPort,
|
|
Address: t.Address, Interface: t.Interface, Helper: t.Helper, Comment: t.Comment,
|
|
})
|
|
}
|
|
}
|