mirror of
https://github.com/luscis/openlan.git
synced 2025-10-07 09:30:54 +08:00
fix: remove route in cache.
This commit is contained in:
@@ -22,6 +22,7 @@ func (r Route) Add(c *cli.Context) error {
|
|||||||
pr := &schema.PrefixRoute{
|
pr := &schema.PrefixRoute{
|
||||||
Prefix: c.String("prefix"),
|
Prefix: c.String("prefix"),
|
||||||
NextHop: c.String("nexthop"),
|
NextHop: c.String("nexthop"),
|
||||||
|
FindHop: c.String("findhop"),
|
||||||
Metric: c.Int("metric"),
|
Metric: c.Int("metric"),
|
||||||
Mode: c.String("mode"),
|
Mode: c.String("mode"),
|
||||||
}
|
}
|
||||||
@@ -39,10 +40,7 @@ func (r Route) Remove(c *cli.Context) error {
|
|||||||
return libol.NewErr("invalid network")
|
return libol.NewErr("invalid network")
|
||||||
}
|
}
|
||||||
pr := &schema.PrefixRoute{
|
pr := &schema.PrefixRoute{
|
||||||
Prefix: c.String("prefix"),
|
Prefix: c.String("prefix"),
|
||||||
NextHop: c.String("nexthop"),
|
|
||||||
Metric: c.Int("metric"),
|
|
||||||
Mode: c.String("mode"),
|
|
||||||
}
|
}
|
||||||
url := r.Url(c.String("url"), network)
|
url := r.Url(c.String("url"), network)
|
||||||
clt := r.NewHttp(c.String("token"))
|
clt := r.NewHttp(c.String("token"))
|
||||||
@@ -66,9 +64,9 @@ func (r Route) Save(c *cli.Context) error {
|
|||||||
|
|
||||||
func (r Route) Tmpl() string {
|
func (r Route) Tmpl() string {
|
||||||
return `# total {{ len . }}
|
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 . }}
|
{{- 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 }}
|
{{- end }}
|
||||||
`
|
`
|
||||||
}
|
}
|
||||||
@@ -94,6 +92,7 @@ func (r Route) Commands() *cli.Command {
|
|||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{Name: "prefix", Required: true},
|
&cli.StringFlag{Name: "prefix", Required: true},
|
||||||
&cli.StringFlag{Name: "nexthop"},
|
&cli.StringFlag{Name: "nexthop"},
|
||||||
|
&cli.StringFlag{Name: "findhop"},
|
||||||
&cli.IntFlag{Name: "metric"},
|
&cli.IntFlag{Name: "metric"},
|
||||||
&cli.StringFlag{Name: "mode"},
|
&cli.StringFlag{Name: "mode"},
|
||||||
},
|
},
|
||||||
@@ -105,7 +104,6 @@ func (r Route) Commands() *cli.Command {
|
|||||||
Aliases: []string{"rm"},
|
Aliases: []string{"rm"},
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
&cli.StringFlag{Name: "prefix", Required: true},
|
&cli.StringFlag{Name: "prefix", Required: true},
|
||||||
&cli.StringFlag{Name: "nexthop"},
|
|
||||||
},
|
},
|
||||||
Action: r.Remove,
|
Action: r.Remove,
|
||||||
},
|
},
|
||||||
|
@@ -53,6 +53,7 @@ type ZTruster interface {
|
|||||||
type Router interface {
|
type Router interface {
|
||||||
AddRoute(route *schema.PrefixRoute, switcher Switcher) error
|
AddRoute(route *schema.PrefixRoute, switcher Switcher) error
|
||||||
DelRoute(route *schema.PrefixRoute, switcher Switcher) error
|
DelRoute(route *schema.PrefixRoute, switcher Switcher) error
|
||||||
|
ListRoute(call func(obj schema.PrefixRoute))
|
||||||
SaveRoute()
|
SaveRoute()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -4,8 +4,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/luscis/openlan/pkg/cache"
|
|
||||||
"github.com/luscis/openlan/pkg/models"
|
|
||||||
"github.com/luscis/openlan/pkg/schema"
|
"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)
|
routes := make([]schema.PrefixRoute, 0, 1024)
|
||||||
|
|
||||||
for u := range cache.Network.ListRoute(id) {
|
worker := Call.GetWorker(id)
|
||||||
if u == nil {
|
if worker == nil {
|
||||||
break
|
http.Error(w, "Network not found", http.StatusInternalServerError)
|
||||||
}
|
return
|
||||||
routes = append(routes, models.NewRouteSchema(u))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
worker.ListRoute(func(obj schema.PrefixRoute) {
|
||||||
|
routes = append(routes, obj)
|
||||||
|
})
|
||||||
ResponseJson(w, routes)
|
ResponseJson(w, routes)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -57,7 +58,6 @@ func (rt Route) Add(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ResponseJson(w, true)
|
ResponseJson(w, true)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rt Route) Del(w http.ResponseWriter, r *http.Request) {
|
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)
|
ResponseJson(w, true)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rt Route) Save(w http.ResponseWriter, r *http.Request) {
|
func (rt Route) Save(w http.ResponseWriter, r *http.Request) {
|
||||||
|
54
pkg/cache/network.go
vendored
54
pkg/cache/network.go
vendored
@@ -4,7 +4,6 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"net"
|
"net"
|
||||||
|
|
||||||
co "github.com/luscis/openlan/pkg/config"
|
|
||||||
"github.com/luscis/openlan/pkg/libol"
|
"github.com/luscis/openlan/pkg/libol"
|
||||||
"github.com/luscis/openlan/pkg/models"
|
"github.com/luscis/openlan/pkg/models"
|
||||||
"github.com/luscis/openlan/pkg/schema"
|
"github.com/luscis/openlan/pkg/schema"
|
||||||
@@ -33,59 +32,6 @@ func (w *network) Get(name string) *models.Network {
|
|||||||
return nil
|
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 {
|
func (w *network) List() <-chan *models.Network {
|
||||||
c := make(chan *models.Network, 128)
|
c := make(chan *models.Network, 128)
|
||||||
|
|
||||||
|
@@ -188,6 +188,12 @@ func (n *Network) FindRoute(value PrefixRoute) (PrefixRoute, int) {
|
|||||||
return PrefixRoute{}, -1
|
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 {
|
func (n *Network) AddRoute(value PrefixRoute) bool {
|
||||||
_, index := n.FindRoute(value)
|
_, index := n.FindRoute(value)
|
||||||
if index == -1 {
|
if index == -1 {
|
||||||
|
@@ -101,26 +101,6 @@ func NewNetworkSchema(n *Network) schema.Network {
|
|||||||
return sn
|
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 {
|
func NewOutputSchema(o *Output) schema.Output {
|
||||||
return schema.Output{
|
return schema.Output{
|
||||||
Network: o.Network,
|
Network: o.Network,
|
||||||
|
@@ -11,9 +11,9 @@ type Lease struct {
|
|||||||
type PrefixRoute struct {
|
type PrefixRoute struct {
|
||||||
Prefix string `json:"prefix"`
|
Prefix string `json:"prefix"`
|
||||||
NextHop string `json:"nexthop"`
|
NextHop string `json:"nexthop"`
|
||||||
|
FindHop string `json:"findhop"`
|
||||||
Metric int `json:"metric"`
|
Metric int `json:"metric"`
|
||||||
Mode string `json:"mode"`
|
Mode string `json:"mode"`
|
||||||
Origin string `json:"origin"`
|
|
||||||
MultiPath []MultiPath `json:"multipath,omitempty"`
|
MultiPath []MultiPath `json:"multipath,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -9,7 +9,6 @@ import (
|
|||||||
"strconv"
|
"strconv"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/luscis/openlan/pkg/cache"
|
|
||||||
co "github.com/luscis/openlan/pkg/config"
|
co "github.com/luscis/openlan/pkg/config"
|
||||||
"github.com/luscis/openlan/pkg/libol"
|
"github.com/luscis/openlan/pkg/libol"
|
||||||
"github.com/luscis/openlan/pkg/models"
|
"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) {
|
func (ng *FindHop) LoadRoute(findhop string, nlr *nl.Route) {
|
||||||
if driver, ok := ng.drivers[findhop]; ok {
|
if driver, ok := ng.drivers[findhop]; ok {
|
||||||
ng.out.Debug("FindHop.loadRoute: %v", nlr)
|
ng.out.Info("FindHop.loadRoute: %v", nlr)
|
||||||
driver.LoadRoute(nlr)
|
driver.LoadRoute(nlr)
|
||||||
} else {
|
} else {
|
||||||
ng.out.Error("FindHop.loadRoute: checker not found %s", findhop)
|
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) {
|
func (c *FindHopDriverImpl) updateRoute(nlr *nl.Route) {
|
||||||
c.out.Debug("FindHopDriverImpl.updateRoute: %v ", nlr)
|
c.out.Debug("FindHopDriverImpl.updateRoute: %v ", nlr)
|
||||||
multiPath := c.buildNexthopInfos()
|
multiPath := c.buildNexthopInfos()
|
||||||
modelMultiPath := c.modelMultiPath()
|
|
||||||
|
|
||||||
nlr.MultiPath = multiPath
|
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 := libol.NewPromise()
|
||||||
promise.Go(func() error {
|
promise.Go(func() error {
|
||||||
if err := nl.RouteReplace(nlr); err != nil {
|
if err := nl.RouteReplace(nlr); err != nil {
|
||||||
|
@@ -106,15 +106,8 @@ func (w *WorkerImpl) Initialize() {
|
|||||||
IpEnd: cfg.Subnet.End,
|
IpEnd: cfg.Subnet.End,
|
||||||
Netmask: cfg.Subnet.Netmask,
|
Netmask: cfg.Subnet.Netmask,
|
||||||
IfAddr: cfg.Bridge.Address,
|
IfAddr: cfg.Bridge.Address,
|
||||||
Routes: make([]*models.Route, 0, 2),
|
|
||||||
Config: cfg,
|
Config: cfg,
|
||||||
}
|
}
|
||||||
for _, rt := range cfg.Routes {
|
|
||||||
nRoute := w.newRoute(&rt)
|
|
||||||
if nRoute != nil {
|
|
||||||
n.Routes = append(n.Routes, nRoute)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
cache.Network.Add(&n)
|
cache.Network.Add(&n)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,7 +307,6 @@ func (w *WorkerImpl) loadRoute(rt co.PrefixRoute) {
|
|||||||
nlr.Priority = rt.Metric
|
nlr.Priority = rt.Metric
|
||||||
}
|
}
|
||||||
if rt.FindHop != "" {
|
if rt.FindHop != "" {
|
||||||
w.out.Info("WorkerImpl.loadRoute: %s, findhop: %s", nlr.String(), rt.FindHop)
|
|
||||||
w.findhop.LoadRoute(rt.FindHop, &nlr)
|
w.findhop.LoadRoute(rt.FindHop, &nlr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -896,41 +888,6 @@ func (w *WorkerImpl) createVPN() {
|
|||||||
w.vpn = obj
|
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) {
|
func (w *WorkerImpl) addVPNRoute(rt co.PrefixRoute) {
|
||||||
vpn := w.cfg.OpenVPN
|
vpn := w.cfg.OpenVPN
|
||||||
if vpn == nil {
|
if vpn == nil {
|
||||||
@@ -988,6 +945,19 @@ func (w *WorkerImpl) correctRoute(route *schema.PrefixRoute) co.PrefixRoute {
|
|||||||
return rt
|
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 {
|
func (w *WorkerImpl) AddRoute(route *schema.PrefixRoute, switcher api.Switcher) error {
|
||||||
rt := w.correctRoute(route)
|
rt := w.correctRoute(route)
|
||||||
if !w.cfg.AddRoute(rt) {
|
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 {
|
if inet, err := libol.ParseNet(rt.Prefix); err == nil {
|
||||||
w.addVPNSet(inet.String())
|
w.addVPNSet(inet.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
w.addCacheRoute(rt)
|
|
||||||
w.addVPNRoute(rt)
|
w.addVPNRoute(rt)
|
||||||
w.loadRoute(rt)
|
w.loadRoute(rt)
|
||||||
return nil
|
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 {
|
if inet, err := libol.ParseNet(delRt.Prefix); err == nil {
|
||||||
w.delVPNSet(inet.String())
|
w.delVPNSet(inet.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
w.delCacheRoute(delRt)
|
|
||||||
w.delVPNRoute(delRt)
|
w.delVPNRoute(delRt)
|
||||||
w.unloadRoute(delRt)
|
w.unloadRoute(delRt)
|
||||||
return nil
|
return nil
|
||||||
|
Reference in New Issue
Block a user