fea: openudp: update link state

This commit is contained in:
Daniel Ding
2022-10-07 18:56:06 +08:00
parent f354e6d85f
commit c03c099dfb
12 changed files with 362 additions and 103 deletions

View File

@@ -1,5 +1,6 @@
package database
import "C"
import (
"context"
"github.com/go-logr/logr"
@@ -60,8 +61,6 @@ func (o *OvSDB) WhereList(predict interface{}, result interface{}) error {
return o.Client.WhereCache(predict).List(o.Context(), result)
}
var Client *OvSDB
type DBClient struct {
Server string
Database string
@@ -103,8 +102,7 @@ func (c *DBClient) Open(handler *cache.EventHandlerFuncs) error {
if err := ovs.Connect(c.Context()); err != nil {
return err
}
Client = &OvSDB{Client: ovs}
c.Client = Client
c.Client = &OvSDB{Client: ovs}
if handler != nil {
processor := ovs.Cache()
if processor == nil {
@@ -119,16 +117,31 @@ func (c *DBClient) Open(handler *cache.EventHandlerFuncs) error {
}
var Conf *DBClient
var Client *OvSDB
func NewDBClient(handler *cache.EventHandlerFuncs) (*DBClient, error) {
func NewConfClient(handler *cache.EventHandlerFuncs) (*DBClient, error) {
var err error
if Conf == nil {
Conf = &DBClient{
obj := &DBClient{
Server: api.Server,
Database: api.Database,
Verbose: api.Verbose,
}
err = Conf.Open(handler)
err = obj.Open(handler)
if err == nil {
Conf = obj
Client = obj.Client
}
}
return Conf, err
}
func NewClient(handler *cache.EventHandlerFuncs) (*DBClient, error) {
obj := &DBClient{
Server: api.Server,
Database: api.Database,
Verbose: api.Verbose,
}
err := obj.Open(handler)
return obj, err
}

View File

@@ -1,5 +1,9 @@
package database
import (
"strconv"
)
type Switch struct {
UUID string `ovsdb:"_uuid" json:"uuid"`
Protocol string `ovsdb:"protocol" json:"protocol"`
@@ -32,6 +36,19 @@ type VirtualLink struct {
Status map[string]string `ovsdb:"status" json:"status"`
}
func (l *VirtualLink) IsUdpIn() bool {
if HasPrefix(l.Device, 4, "spi:") &&
HasPrefix(l.Connection, 4, "udp:") {
return true
}
return false
}
func (l *VirtualLink) Spi() uint32 {
spi, _ := strconv.Atoi(l.Device[4:])
return uint32(spi)
}
type OpenVPN struct {
UUID string `ovsdb:"_uuid" json:"uuid"`
Protocol string `ovsdb:"protocol" json:"protocol"`

View File

@@ -3,6 +3,8 @@ package database
import (
"github.com/luscis/openlan/pkg/libol"
"github.com/ovn-org/libovsdb/ovsdb"
"strconv"
"strings"
)
func PrintError(result []ovsdb.OperationResult) {
@@ -17,3 +19,19 @@ func PrintError(result []ovsdb.OperationResult) {
func GenUUID() string {
return libol.GenString(32)
}
func HasPrefix(value string, index int, dest string) bool {
if len(value) >= index {
return value[:index] == dest
}
return false
}
func GetAddrPort(conn string) (string, int) {
values := strings.SplitN(conn, ":", 2)
if len(values) == 2 {
port, _ := strconv.Atoi(values[1])
return values[0], port
}
return values[0], 0
}