fix: format output parameters

This commit is contained in:
Daniel Ding
2024-01-23 10:42:20 +08:00
parent 4d76ae91af
commit 430cf1c5d8
9 changed files with 131 additions and 128 deletions

View File

@@ -45,5 +45,21 @@
}, },
"acl": "acl-100", "acl": "acl-100",
"dhcp": "enable", "dhcp": "enable",
"namespace": "example" "namespace": "example",
"outputs": [
{
"segment": 43,
"remote": "3.3.3.5",
"protocol": "vxlan"
},
{
"segment": 55,
"remote": "3.3.3.3",
"protocol": "gre"
},
{
"segment": 23,
"remote": "enp2s2"
}
]
} }

View File

@@ -15,12 +15,18 @@
"dhcp": "enable", "dhcp": "enable",
"outputs": [ "outputs": [
{ {
"vlan": 100, "segment": 43,
"interface": "vxlan:3.3.3.5:43" "remote": "3.3.3.5",
"protocol": "vxlan"
}, },
{ {
"vlan": 0, "segment": 55,
"interface": "gre:3.3.3.3:55" "remote": "3.3.3.3",
"protocol": "gre"
},
{
"segment": 23,
"remote": "enp2s2"
} }
] ]
} }

View File

@@ -23,4 +23,5 @@ func Add(router *mux.Router, switcher Switcher) {
Log{}.Router(router) Log{}.Router(router)
OpenAPI{}.Router(router) OpenAPI{}.Router(router)
ZTrust{}.Router(router) ZTrust{}.Router(router)
Output{}.Router(router)
} }

View File

@@ -9,7 +9,7 @@ import (
"github.com/luscis/openlan/pkg/libol" "github.com/luscis/openlan/pkg/libol"
) )
var index = 1024 var index = 99
func GenName(prefix string) string { func GenName(prefix string) string {
index += 1 index += 1

View File

@@ -1,7 +1,8 @@
package config package config
type Output struct { type Output struct {
Vlan int `json:"vlan"` Segment int `json:"segment"`
Interface string `json:"interface"` // format, like: gre:<addr>, vxlan:<addr>:<vni> Protocol string `json:"protocol"` // gre, vxlan, etc.
Link string `json:"link"` // link name Remote string `json:"remote"`
Link string `json:"link"` // link name
} }

View File

@@ -3,15 +3,15 @@ package models
import "time" import "time"
type Output struct { type Output struct {
Network string Network string
Protocol string Protocol string
Connection string Remote string
Vlan int Segment int
Device string Device string
RxBytes uint64 RxBytes uint64
TxBytes uint64 TxBytes uint64
ErrPkt uint64 ErrPkt uint64
NewTime int64 NewTime int64
} }
func (o *Output) UpTime() int64 { func (o *Output) UpTime() int64 {

View File

@@ -119,12 +119,13 @@ func NewNetworkSchema(n *Network) schema.Network {
func NewOutputSchema(o *Output) schema.Output { func NewOutputSchema(o *Output) schema.Output {
return schema.Output{ return schema.Output{
Network: o.Network, Network: o.Network,
Protocol: o.Protocol, Protocol: o.Protocol,
Connection: o.Connection, Remote: o.Remote,
Device: o.Device, Segment: o.Segment,
RxBytes: o.RxBytes, Device: o.Device,
TxBytes: o.TxBytes, RxBytes: o.RxBytes,
AliveTime: o.UpTime(), TxBytes: o.TxBytes,
AliveTime: o.UpTime(),
} }
} }

View File

@@ -1,13 +1,13 @@
package schema package schema
type Output struct { type Output struct {
Network string `json:"network"` Network string `json:"network"`
Protocol string `json:"protocol"` Protocol string `json:"protocol"`
Connection string `json:"connection"` Remote string `json:"Remote"`
Vlan int `json:"vlan"` Segment int `json:"segment"`
Device string `json:"device"` Device string `json:"device"`
RxBytes uint64 `json:"rxBytes"` RxBytes uint64 `json:"rxBytes"`
TxBytes uint64 `json:"txBytes"` TxBytes uint64 `json:"txBytes"`
ErrPkt uint64 `json:"errors"` ErrPkt uint64 `json:"errors"`
AliveTime int64 `json:"aliveTime"` AliveTime int64 `json:"aliveTime"`
} }

View File

@@ -3,7 +3,6 @@ package cswitch
import ( import (
"fmt" "fmt"
"net" "net"
"strconv"
"strings" "strings"
"time" "time"
@@ -35,11 +34,14 @@ func NewNetworker(c *co.Network) api.Networker {
} }
type LinuxPort struct { type LinuxPort struct {
name string // gre:xx, vxlan:xx cfg co.Output
vlan int
link string link string
} }
func (l *LinuxPort) String() string {
return fmt.Sprintf("%s:%s:%d", l.cfg.Protocol, l.cfg.Remote, l.cfg.Segment)
}
type WorkerImpl struct { type WorkerImpl struct {
uuid string uuid string
cfg *co.Network cfg *co.Network
@@ -132,121 +134,100 @@ func (w *WorkerImpl) Initialize() {
w.forwardVPN() w.forwardVPN()
} }
func (w *WorkerImpl) AddPhysical(bridge string, vlan int, output string) { func (w *WorkerImpl) AddPhysical(bridge string, output string) {
link, err := nl.LinkByName(output)
if err != nil {
w.out.Error("WorkerImpl.LinkByName %s %s", output, err)
return
}
slaver := output
if vlan > 0 {
if err := nl.LinkSetUp(link); err != nil {
w.out.Warn("WorkerImpl.LinkSetUp %s %s", output, err)
}
subLink := &nl.Vlan{
LinkAttrs: nl.LinkAttrs{
Name: fmt.Sprintf("%s.%d", output, vlan),
ParentIndex: link.Attrs().Index,
},
VlanId: vlan,
}
if err := nl.LinkAdd(subLink); err != nil {
w.out.Error("WorkerImpl.LinkAdd %s %s", subLink.Name, err)
return
}
slaver = subLink.Name
}
br := cn.NewBrCtl(bridge, 0) br := cn.NewBrCtl(bridge, 0)
if err := br.AddPort(slaver); err != nil { if err := br.AddPort(output); err != nil {
w.out.Warn("WorkerImpl.AddPhysical %s", err) w.out.Warn("WorkerImpl.AddPhysical %s", err)
} }
} }
func (w *WorkerImpl) AddOutput(bridge string, port *LinuxPort) { func (w *WorkerImpl) AddOutput(bridge string, port *LinuxPort) {
name := port.name cfg := port.cfg
values := strings.SplitN(name, ":", 6)
out := &models.Output{ out := &models.Output{
Network: w.cfg.Name, Network: w.cfg.Name,
NewTime: time.Now().Unix(), NewTime: time.Now().Unix(),
Protocol: cfg.Protocol,
Remote: cfg.Remote,
Segment: cfg.Segment,
} }
mtu := 0 mtu := 0
if values[0] == "gre" { if cfg.Protocol == "gre" {
if len(values) < 3 {
w.out.Error("WorkerImpl.LinkAdd %s wrong", name)
return
}
if port.link == "" { if port.link == "" {
port.link = co.GenName("gre") port.link = co.GenName("gre")
} }
key, _ := strconv.Atoi(values[2])
mtu = 1460 mtu = 1460
link := &nl.Gretap{ link := &nl.Gretap{
IKey: uint32(key), IKey: uint32(cfg.Segment),
OKey: uint32(key), OKey: uint32(cfg.Segment),
LinkAttrs: nl.LinkAttrs{ LinkAttrs: nl.LinkAttrs{
Name: port.link, Name: port.link,
MTU: mtu, MTU: mtu,
}, },
Local: libol.ParseAddr("0.0.0.0"), Local: libol.ParseAddr("0.0.0.0"),
Remote: libol.ParseAddr(values[1]), Remote: libol.ParseAddr(cfg.Remote),
PMtuDisc: 1, PMtuDisc: 1,
} }
if err := nl.LinkAdd(link); err != nil { if err := nl.LinkAdd(link); err != nil {
w.out.Error("WorkerImpl.LinkAdd %s %s", name, err) w.out.Error("WorkerImpl.LinkAdd %s %s", port.String(), err)
return return
} }
out.Protocol = "gre" } else if cfg.Protocol == "vxlan" {
out.Connection = fmt.Sprintf("%s:%s", values[1], values[2])
} else if values[0] == "vxlan" {
if len(values) < 3 {
w.out.Error("WorkerImpl.LinkAdd %s wrong", name)
return
}
if port.link == "" { if port.link == "" {
port.link = co.GenName("vxn") port.link = co.GenName("vxn")
} }
dport := 8472 dport := 8472
if len(values) == 4 {
dport, _ = strconv.Atoi(values[3])
}
vni, _ := strconv.Atoi(values[2])
mtu = 1450 mtu = 1450
link := &nl.Vxlan{ link := &nl.Vxlan{
VxlanId: vni, VxlanId: cfg.Segment,
LinkAttrs: nl.LinkAttrs{ LinkAttrs: nl.LinkAttrs{
TxQLen: -1, TxQLen: -1,
Name: port.link, Name: port.link,
MTU: mtu, MTU: mtu,
}, },
Group: libol.ParseAddr(values[1]), Group: libol.ParseAddr(cfg.Remote),
Port: dport, Port: dport,
} }
if err := nl.LinkAdd(link); err != nil { if err := nl.LinkAdd(link); err != nil {
w.out.Error("WorkerImpl.LinkAdd %s %s", name, err) w.out.Error("WorkerImpl.LinkAdd %s %s", port.String(), err)
return return
} }
out.Protocol = "vxlan"
out.Connection = fmt.Sprintf("%s:%s", values[1], values[2])
} else { } else {
port.link = name link, err := nl.LinkByName(cfg.Remote)
if link == nil {
w.out.Error("WorkerImpl.AddOutput %s %s", cfg.Remote, err)
return
}
if err := nl.LinkSetUp(link); err != nil {
w.out.Warn("WorkerImpl.AddOutput %s %s", cfg.Remote, err)
}
if port.link == "" {
port.link = fmt.Sprintf("%s.%d", cfg.Remote, cfg.Segment)
}
subLink := &nl.Vlan{
LinkAttrs: nl.LinkAttrs{
Name: port.link,
ParentIndex: link.Attrs().Index,
},
VlanId: cfg.Segment,
}
if err := nl.LinkAdd(subLink); err != nil {
w.out.Error("WorkerImpl.linkAdd %s %s", subLink.Name, err)
return
}
} }
if w.br != nil && mtu > 0 { if mtu > 0 {
w.br.SetMtu(mtu) if w.br != nil {
w.br.SetMtu(mtu)
}
} }
out.Device = port.link out.Device = port.link
cache.Output.Add(port.link, out) cache.Output.Add(port.link, out)
w.out.Info("WorkerImpl.AddOutput %s %s", port.link, port.name) w.out.Info("WorkerImpl.AddOutput %s %s", port.link, port.String())
w.AddPhysical(bridge, port.vlan, port.link) w.AddPhysical(bridge, port.link)
} }
func (w *WorkerImpl) loadRoutes() { func (w *WorkerImpl) loadRoutes() {
@@ -350,8 +331,7 @@ func (w *WorkerImpl) Start(v api.Switcher) {
for _, output := range cfg.Outputs { for _, output := range cfg.Outputs {
port := &LinuxPort{ port := &LinuxPort{
name: output.Interface, cfg: output,
vlan: output.Vlan,
} }
w.AddOutput(cfg.Bridge.Name, port) w.AddOutput(cfg.Bridge.Name, port)
w.outputs = append(w.outputs, port) w.outputs = append(w.outputs, port)
@@ -418,50 +398,48 @@ func (w *WorkerImpl) Start(v api.Switcher) {
fire.Start() fire.Start()
} }
func (w *WorkerImpl) DelPhysical(bridge string, vlan int, output string) { func (w *WorkerImpl) DelPhysical(bridge string, output string) {
if vlan > 0 { br := cn.NewBrCtl(bridge, 0)
subLink := &nl.Vlan{ if err := br.DelPort(output); err != nil {
LinkAttrs: nl.LinkAttrs{ w.out.Warn("WorkerImpl.DelPhysical %s", err)
Name: fmt.Sprintf("%s.%d", output, vlan),
},
}
if err := nl.LinkDel(subLink); err != nil {
w.out.Error("WorkerImpl.DelPhysical.LinkDel %s %s", subLink.Name, err)
return
}
} else {
br := cn.NewBrCtl(bridge, 0)
if err := br.DelPort(output); err != nil {
w.out.Warn("WorkerImpl.DelPhysical %s", err)
}
} }
} }
func (w *WorkerImpl) DelOutput(bridge string, port *LinuxPort) { func (w *WorkerImpl) DelOutput(bridge string, port *LinuxPort) {
w.out.Info("WorkerImpl.DelOutput %s %s", port.link, port.name) cfg := port.cfg
w.out.Info("WorkerImpl.DelOutput %s %s", port.link, port.String())
cache.Output.Del(port.link) cache.Output.Del(port.link)
w.DelPhysical(bridge, port.vlan, port.link) w.DelPhysical(bridge, port.link)
values := strings.SplitN(port.name, ":", 6) if cfg.Protocol == "gre" {
if values[0] == "gre" {
link := &nl.Gretap{ link := &nl.Gretap{
LinkAttrs: nl.LinkAttrs{ LinkAttrs: nl.LinkAttrs{
Name: port.link, Name: port.link,
}, },
} }
if err := nl.LinkDel(link); err != nil { if err := nl.LinkDel(link); err != nil {
w.out.Error("WorkerImpl.DelOutput.LinkDel %s %s", link.Name, err) w.out.Error("WorkerImpl.LinkDel %s %s", link.Name, err)
return return
} }
} else if values[0] == "vxlan" { } else if cfg.Protocol == "vxlan" {
link := &nl.Vxlan{ link := &nl.Vxlan{
LinkAttrs: nl.LinkAttrs{ LinkAttrs: nl.LinkAttrs{
Name: port.link, Name: port.link,
}, },
} }
if err := nl.LinkDel(link); err != nil { if err := nl.LinkDel(link); err != nil {
w.out.Error("WorkerImpl.DelOutput.LinkDel %s %s", link.Name, err) w.out.Error("WorkerImpl.LinkDel %s %s", link.Name, err)
return
}
} else {
link := &nl.Vlan{
LinkAttrs: nl.LinkAttrs{
Name: port.link,
},
}
if err := nl.LinkDel(link); err != nil {
w.out.Error("WorkerImpl.LinkDel %s %s", link.Name, err)
return return
} }
} }