fix: remove route in cache.

This commit is contained in:
Daniel Ding
2024-08-29 19:57:02 +08:00
parent 1cce1d661a
commit 65a0ea5624
9 changed files with 35 additions and 147 deletions

View File

@@ -22,6 +22,7 @@ func (r Route) Add(c *cli.Context) error {
pr := &schema.PrefixRoute{
Prefix: c.String("prefix"),
NextHop: c.String("nexthop"),
FindHop: c.String("findhop"),
Metric: c.Int("metric"),
Mode: c.String("mode"),
}
@@ -40,9 +41,6 @@ func (r Route) Remove(c *cli.Context) error {
}
pr := &schema.PrefixRoute{
Prefix: c.String("prefix"),
NextHop: c.String("nexthop"),
Metric: c.Int("metric"),
Mode: c.String("mode"),
}
url := r.Url(c.String("url"), network)
clt := r.NewHttp(c.String("token"))
@@ -66,9 +64,9 @@ func (r Route) Save(c *cli.Context) error {
func (r Route) Tmpl() string {
return `# total {{ len . }}
{{ps -25 "prefix"}} {{ps -25 "nexthop"}} {{ps -8 "metric"}} {{ps -8 "mode"}} {{ps -15 "origin"}}
{{ps -25 "prefix"}} {{ps -25 "nexthop"}} {{ps -8 "metric"}} {{ps -8 "mode"}}
{{- range . }}
{{ps -25 .Prefix}} {{ps -25 .NextHop}} {{pi -8 .Metric }} {{ps -8 .Mode}} {{ps -15 .Origin}}
{{ps -25 .Prefix}} {{ if .FindHop }}{{ps -25 .FindHop}}{{ else }}{{ps -25 .NextHop}}{{ end }} {{pi -8 .Metric }} {{ps -8 .Mode}}
{{- end }}
`
}
@@ -94,6 +92,7 @@ func (r Route) Commands() *cli.Command {
Flags: []cli.Flag{
&cli.StringFlag{Name: "prefix", Required: true},
&cli.StringFlag{Name: "nexthop"},
&cli.StringFlag{Name: "findhop"},
&cli.IntFlag{Name: "metric"},
&cli.StringFlag{Name: "mode"},
},
@@ -105,7 +104,6 @@ func (r Route) Commands() *cli.Command {
Aliases: []string{"rm"},
Flags: []cli.Flag{
&cli.StringFlag{Name: "prefix", Required: true},
&cli.StringFlag{Name: "nexthop"},
},
Action: r.Remove,
},

View File

@@ -53,6 +53,7 @@ type ZTruster interface {
type Router interface {
AddRoute(route *schema.PrefixRoute, switcher Switcher) error
DelRoute(route *schema.PrefixRoute, switcher Switcher) error
ListRoute(call func(obj schema.PrefixRoute))
SaveRoute()
}

View File

@@ -4,8 +4,6 @@ import (
"net/http"
"github.com/gorilla/mux"
"github.com/luscis/openlan/pkg/cache"
"github.com/luscis/openlan/pkg/models"
"github.com/luscis/openlan/pkg/schema"
)
@@ -26,12 +24,15 @@ func (rt Route) List(w http.ResponseWriter, r *http.Request) {
routes := make([]schema.PrefixRoute, 0, 1024)
for u := range cache.Network.ListRoute(id) {
if u == nil {
break
}
routes = append(routes, models.NewRouteSchema(u))
worker := Call.GetWorker(id)
if worker == nil {
http.Error(w, "Network not found", http.StatusInternalServerError)
return
}
worker.ListRoute(func(obj schema.PrefixRoute) {
routes = append(routes, obj)
})
ResponseJson(w, routes)
}
@@ -57,7 +58,6 @@ func (rt Route) Add(w http.ResponseWriter, r *http.Request) {
}
ResponseJson(w, true)
}
func (rt Route) Del(w http.ResponseWriter, r *http.Request) {
@@ -82,7 +82,6 @@ func (rt Route) Del(w http.ResponseWriter, r *http.Request) {
}
ResponseJson(w, true)
}
func (rt Route) Save(w http.ResponseWriter, r *http.Request) {

54
pkg/cache/network.go vendored
View File

@@ -4,7 +4,6 @@ import (
"encoding/binary"
"net"
co "github.com/luscis/openlan/pkg/config"
"github.com/luscis/openlan/pkg/libol"
"github.com/luscis/openlan/pkg/models"
"github.com/luscis/openlan/pkg/schema"
@@ -33,59 +32,6 @@ func (w *network) Get(name string) *models.Network {
return nil
}
// add/del route
func (w *network) ListRoute(name string) <-chan *models.Route {
c := make(chan *models.Route, 128)
n := w.Get(name)
if n != nil {
go func() {
for _, route := range n.Routes {
if route != nil {
c <- route
}
}
c <- nil //Finish channel by nil.
}()
} else {
c <- nil
}
return c
}
func (w *network) UpdateRoute(name string, rt co.PrefixRoute, call func(obj *models.Route)) {
n := w.Get(name)
if n != nil {
for _, route := range n.Routes {
if route.Prefix == rt.Prefix {
call(route)
break
}
}
}
return
}
func (w *network) DelRoute(name string, rt co.PrefixRoute) {
n := w.Get(name)
if n != nil {
for i, route := range n.Routes {
if route.Prefix == rt.Prefix {
n.Routes = append(n.Routes[:i], n.Routes[i+1:]...)
break
}
}
}
}
func (w *network) AddRoute(name string, route *models.Route) {
n := w.Get(name)
if n != nil && route != nil {
n.Routes = append(n.Routes, route)
}
}
func (w *network) List() <-chan *models.Network {
c := make(chan *models.Network, 128)

View File

@@ -188,6 +188,12 @@ func (n *Network) FindRoute(value PrefixRoute) (PrefixRoute, int) {
return PrefixRoute{}, -1
}
func (n *Network) ListRoute(call func(value PrefixRoute)) {
for _, obj := range n.Routes {
call(obj)
}
}
func (n *Network) AddRoute(value PrefixRoute) bool {
_, index := n.FindRoute(value)
if index == -1 {

View File

@@ -101,26 +101,6 @@ func NewNetworkSchema(n *Network) schema.Network {
return sn
}
func NewRouteSchema(route *Route) schema.PrefixRoute {
multiPath := make([]schema.MultiPath, 0, len(route.MultiPath))
for _, mp := range route.MultiPath {
multiPath = append(multiPath, schema.MultiPath{
NextHop: mp.NextHop,
Weight: mp.Weight,
})
}
pr := schema.PrefixRoute{
Prefix: route.Prefix,
NextHop: route.NextHop,
Metric: route.Metric,
Mode: route.Mode,
Origin: route.Origin,
MultiPath: multiPath,
}
return pr
}
func NewOutputSchema(o *Output) schema.Output {
return schema.Output{
Network: o.Network,

View File

@@ -11,9 +11,9 @@ type Lease struct {
type PrefixRoute struct {
Prefix string `json:"prefix"`
NextHop string `json:"nexthop"`
FindHop string `json:"findhop"`
Metric int `json:"metric"`
Mode string `json:"mode"`
Origin string `json:"origin"`
MultiPath []MultiPath `json:"multipath,omitempty"`
}

View File

@@ -9,7 +9,6 @@ import (
"strconv"
"time"
"github.com/luscis/openlan/pkg/cache"
co "github.com/luscis/openlan/pkg/config"
"github.com/luscis/openlan/pkg/libol"
"github.com/luscis/openlan/pkg/models"
@@ -96,7 +95,7 @@ func (ng *FindHop) DelFindHop(name string, cfg co.FindHop) {
func (ng *FindHop) LoadRoute(findhop string, nlr *nl.Route) {
if driver, ok := ng.drivers[findhop]; ok {
ng.out.Debug("FindHop.loadRoute: %v", nlr)
ng.out.Info("FindHop.loadRoute: %v", nlr)
driver.LoadRoute(nlr)
} else {
ng.out.Error("FindHop.loadRoute: checker not found %s", findhop)
@@ -210,16 +209,9 @@ func (c *FindHopDriverImpl) buildNexthopInfos() []*nl.NexthopInfo {
func (c *FindHopDriverImpl) updateRoute(nlr *nl.Route) {
c.out.Debug("FindHopDriverImpl.updateRoute: %v ", nlr)
multiPath := c.buildNexthopInfos()
modelMultiPath := c.modelMultiPath()
nlr.MultiPath = multiPath
cache.Network.UpdateRoute(c.Network, co.PrefixRoute{
Prefix: nlr.Dst.String(),
}, func(obj *models.Route) {
obj.MultiPath = modelMultiPath
})
promise := libol.NewPromise()
promise.Go(func() error {
if err := nl.RouteReplace(nlr); err != nil {

View File

@@ -106,15 +106,8 @@ func (w *WorkerImpl) Initialize() {
IpEnd: cfg.Subnet.End,
Netmask: cfg.Subnet.Netmask,
IfAddr: cfg.Bridge.Address,
Routes: make([]*models.Route, 0, 2),
Config: cfg,
}
for _, rt := range cfg.Routes {
nRoute := w.newRoute(&rt)
if nRoute != nil {
n.Routes = append(n.Routes, nRoute)
}
}
cache.Network.Add(&n)
}
@@ -314,7 +307,6 @@ func (w *WorkerImpl) loadRoute(rt co.PrefixRoute) {
nlr.Priority = rt.Metric
}
if rt.FindHop != "" {
w.out.Info("WorkerImpl.loadRoute: %s, findhop: %s", nlr.String(), rt.FindHop)
w.findhop.LoadRoute(rt.FindHop, &nlr)
return
}
@@ -896,41 +888,6 @@ func (w *WorkerImpl) createVPN() {
w.vpn = obj
}
func (w *WorkerImpl) delCacheRoute(rt co.PrefixRoute) {
if rt.NextHop == "" {
w.out.Warn("WorkerImpl.DelCacheRoute: %s noNextHop", rt.Prefix)
return
}
rte := models.NewRoute(rt.Prefix, w.IfAddr(), rt.Mode)
if rt.Metric > 0 {
rte.Metric = rt.Metric
}
if rt.FindHop == "" && rt.NextHop != "" {
rte.Origin = rt.NextHop
}
cache.Network.DelRoute(w.cfg.Name, rt)
}
func (w *WorkerImpl) addCacheRoute(rt co.PrefixRoute) {
w.out.Info("WorkerImpl.addCacheRoute: %v ", rt)
if rt.NextHop == "" {
w.out.Warn("WorkerImpl.AddCacheRoute: %s ", rt.Prefix)
return
}
rte := models.NewRoute(rt.Prefix, w.IfAddr(), rt.Mode)
if rt.Metric > 0 {
rte.Metric = rt.Metric
}
if rt.FindHop == "" && rt.NextHop != "" {
rte.Origin = rt.NextHop
}
cache.Network.AddRoute(w.cfg.Name, rte)
}
func (w *WorkerImpl) addVPNRoute(rt co.PrefixRoute) {
vpn := w.cfg.OpenVPN
if vpn == nil {
@@ -988,6 +945,19 @@ func (w *WorkerImpl) correctRoute(route *schema.PrefixRoute) co.PrefixRoute {
return rt
}
func (w *WorkerImpl) ListRoute(call func(obj schema.PrefixRoute)) {
w.cfg.ListRoute(func(obj co.PrefixRoute) {
data := schema.PrefixRoute{
Prefix: obj.Prefix,
NextHop: obj.NextHop,
FindHop: obj.FindHop,
Mode: obj.Mode,
Metric: obj.Metric,
}
call(data)
})
}
func (w *WorkerImpl) AddRoute(route *schema.PrefixRoute, switcher api.Switcher) error {
rt := w.correctRoute(route)
if !w.cfg.AddRoute(rt) {
@@ -1000,8 +970,6 @@ func (w *WorkerImpl) AddRoute(route *schema.PrefixRoute, switcher api.Switcher)
if inet, err := libol.ParseNet(rt.Prefix); err == nil {
w.addVPNSet(inet.String())
}
w.addCacheRoute(rt)
w.addVPNRoute(rt)
w.loadRoute(rt)
return nil
@@ -1019,8 +987,6 @@ func (w *WorkerImpl) DelRoute(route *schema.PrefixRoute, switcher api.Switcher)
if inet, err := libol.ParseNet(delRt.Prefix); err == nil {
w.delVPNSet(inet.String())
}
w.delCacheRoute(delRt)
w.delVPNRoute(delRt)
w.unloadRoute(delRt)
return nil