mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-08-31 01:35:56 +08:00
Bugfix/yousong hostman routes (#7563)
* netutils2: SNetInterface: add GetGatewayRoutes * hostman: use GetGatewayRoutes * iproute2: route: add and use AddNetlinkRoute * iproute2: route: List4 use RouteSpec * netutils2: SNetInterface: rename GetRoutes to getRoutes * netutils2: SNetInterface: add GetRouteSpecs * netutils2: SNetInterface: add GetGatewayRouteSpecs * hostman: hostbridge: SetupRoutes with iproute2.RouteSpec * hostman: hostinfo: move also other routes * netutils2: SNetInterface: remove now unused methods * iproute2: route: RouteGetByDst: use RouteSpec as return value
This commit is contained in:
@@ -40,7 +40,7 @@ type IBridgeDriver interface {
|
||||
Setup(IBridgeDriver) error
|
||||
SetupAddresses(net.IPMask) error
|
||||
SetupSlaveAddresses([][]string) error
|
||||
SetupRoutes(routes [][]string) error
|
||||
SetupRoutes([]iproute2.RouteSpec) error
|
||||
BringupInterface() error
|
||||
|
||||
Exists() (bool, error)
|
||||
@@ -229,12 +229,11 @@ func (d *SBaseBridgeDriver) SetupSlaveAddresses(slaveAddrs [][]string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (d *SBaseBridgeDriver) SetupRoutes(routes [][]string) error {
|
||||
func (d *SBaseBridgeDriver) SetupRoutes(routespecs []iproute2.RouteSpec) error {
|
||||
br := d.bridge.String()
|
||||
r := iproute2.NewRoute(br)
|
||||
for _, route := range routes {
|
||||
netStr, maskStr, gwStr := route[0], route[2], route[1]
|
||||
r.Add(netStr, maskStr, gwStr)
|
||||
for _, routespec := range routespecs {
|
||||
r.AddByRouteSpec(routespec)
|
||||
}
|
||||
if err := r.Err(); err != nil {
|
||||
return errors.Wrapf(err, "set routes on %s", br)
|
||||
@@ -243,10 +242,10 @@ func (d *SBaseBridgeDriver) SetupRoutes(routes [][]string) error {
|
||||
}
|
||||
|
||||
func (d *SBaseBridgeDriver) Setup(o IBridgeDriver) error {
|
||||
var routes [][]string
|
||||
var routes []iproute2.RouteSpec
|
||||
var slaveAddrs [][]string
|
||||
if d.inter != nil && len(d.inter.Addr) > 0 {
|
||||
routes = d.inter.GetRoutes(true)
|
||||
routes = d.inter.GetRouteSpecs()
|
||||
slaveAddrs = d.inter.GetSlaveAddresses()
|
||||
}
|
||||
exist, err := o.Exists()
|
||||
|
||||
+20
-14
@@ -26,6 +26,8 @@ const (
|
||||
errBadIP = errors.Error("bad ip")
|
||||
)
|
||||
|
||||
type RouteSpec = netlink.Route
|
||||
|
||||
type Route struct {
|
||||
*Link
|
||||
}
|
||||
@@ -46,7 +48,7 @@ func (route *Route) link() (link netlink.Link, ok bool) {
|
||||
return
|
||||
}
|
||||
|
||||
func (route *Route) List4() ([]netlink.Route, error) {
|
||||
func (route *Route) List4() ([]RouteSpec, error) {
|
||||
link, ok := route.link()
|
||||
if !ok {
|
||||
return nil, route.Err()
|
||||
@@ -70,22 +72,13 @@ func (route *Route) List4() ([]netlink.Route, error) {
|
||||
}
|
||||
|
||||
func (route *Route) AddByIPNet(ipnet *net.IPNet, gw net.IP) *Route {
|
||||
link, ok := route.link()
|
||||
if !ok {
|
||||
return route
|
||||
}
|
||||
|
||||
r := &netlink.Route{
|
||||
LinkIndex: link.Attrs().Index,
|
||||
Dst: ipnet,
|
||||
r := RouteSpec{
|
||||
Dst: ipnet,
|
||||
}
|
||||
if len(gw) > 0 {
|
||||
r.Gw = gw
|
||||
}
|
||||
if err := netlink.RouteReplace(r); err != nil {
|
||||
route.addErr(err, "RouteReplace %s", r.String())
|
||||
}
|
||||
return route
|
||||
return route.AddByRouteSpec(r)
|
||||
}
|
||||
|
||||
func (route *Route) AddByCidr(cidr string, gwStr string) *Route {
|
||||
@@ -98,6 +91,19 @@ func (route *Route) AddByCidr(cidr string, gwStr string) *Route {
|
||||
return route.AddByIPNet(dst, gw)
|
||||
}
|
||||
|
||||
func (route *Route) AddByRouteSpec(r RouteSpec) *Route {
|
||||
link, ok := route.link()
|
||||
if !ok {
|
||||
return route
|
||||
}
|
||||
|
||||
r.LinkIndex = link.Attrs().Index
|
||||
if err := netlink.RouteReplace(&r); err != nil {
|
||||
route.addErr(err, "RouteReplace %s", r.String())
|
||||
}
|
||||
return route
|
||||
}
|
||||
|
||||
func (route *Route) parseCidr(cidr, gwStr string) (dst *net.IPNet, gw net.IP, err error) {
|
||||
if _, dst, err = net.ParseCIDR(cidr); err != nil {
|
||||
err = errors.Wrap(err, "parse cidr")
|
||||
@@ -201,7 +207,7 @@ func (route *Route) DelByIPNet(ipnet *net.IPNet) *Route {
|
||||
return route
|
||||
}
|
||||
|
||||
func RouteGetByDst(dstStr string) ([]netlink.Route, error) {
|
||||
func RouteGetByDst(dstStr string) ([]RouteSpec, error) {
|
||||
dstIp := net.ParseIP(dstStr)
|
||||
routes, err := netlink.RouteGet(dstIp)
|
||||
return routes, err
|
||||
|
||||
@@ -44,32 +44,12 @@ func (n *SNetInterface) GetAddresses() [][]string {
|
||||
return r
|
||||
}
|
||||
|
||||
func (n *SNetInterface) GetRoutes(gwOnly bool) [][]string {
|
||||
rs, err := iproute2.NewRoute(n.name).List4()
|
||||
func (n *SNetInterface) GetRouteSpecs() []iproute2.RouteSpec {
|
||||
routespecs, err := iproute2.NewRoute(n.name).List4()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
res := [][]string{}
|
||||
for i := range rs {
|
||||
r := &rs[i]
|
||||
ok := true
|
||||
if masklen, _ := r.Dst.Mask.Size(); gwOnly && masklen != 0 {
|
||||
ok = false
|
||||
}
|
||||
if ok {
|
||||
gwStr := ""
|
||||
if len(r.Gw) > 0 {
|
||||
gwStr = r.Gw.String()
|
||||
}
|
||||
res = append(res, []string{
|
||||
r.Dst.IP.String(),
|
||||
gwStr,
|
||||
net.IP(r.Dst.Mask).String(),
|
||||
})
|
||||
}
|
||||
}
|
||||
return res
|
||||
return routespecs
|
||||
}
|
||||
|
||||
func DefaultSrcIpDev() (srcIp net.IP, ifname string, err error) {
|
||||
|
||||
Reference in New Issue
Block a user