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",
"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",
"outputs": [
{
"vlan": 100,
"interface": "vxlan:3.3.3.5:43"
"segment": 43,
"remote": "3.3.3.5",
"protocol": "vxlan"
},
{
"vlan": 0,
"interface": "gre:3.3.3.3:55"
"segment": 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)
OpenAPI{}.Router(router)
ZTrust{}.Router(router)
Output{}.Router(router)
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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