2017-02-20 17:34:32 +00:00
|
|
|
// +build linux
|
|
|
|
|
2014-09-01 03:27:34 +00:00
|
|
|
package netlink
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net"
|
2015-08-19 15:31:39 +00:00
|
|
|
"syscall"
|
2014-09-01 03:27:34 +00:00
|
|
|
"testing"
|
2015-08-19 15:31:39 +00:00
|
|
|
"time"
|
2016-06-29 16:11:35 +00:00
|
|
|
|
|
|
|
"github.com/vishvananda/netns"
|
2014-09-01 03:27:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRouteAddDel(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
// get loopback interface
|
|
|
|
link, err := LinkByName("lo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bring the interface up
|
2015-11-26 15:09:33 +00:00
|
|
|
if err := LinkSetUp(link); err != nil {
|
2014-09-01 03:27:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a gateway route
|
2015-11-26 15:09:33 +00:00
|
|
|
dst := &net.IPNet{
|
|
|
|
IP: net.IPv4(192, 168, 0, 0),
|
|
|
|
Mask: net.CIDRMask(24, 32),
|
|
|
|
}
|
2014-09-01 03:27:34 +00:00
|
|
|
|
2015-11-26 15:09:33 +00:00
|
|
|
ip := net.IPv4(127, 1, 1, 1)
|
2014-10-13 23:09:09 +00:00
|
|
|
route := Route{LinkIndex: link.Attrs().Index, Dst: dst, Src: ip}
|
2015-11-26 15:09:33 +00:00
|
|
|
if err := RouteAdd(&route); err != nil {
|
2014-09-01 03:27:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
routes, err := RouteList(link, FAMILY_V4)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(routes) != 1 {
|
2015-11-26 15:09:33 +00:00
|
|
|
t.Fatal("Route not added properly")
|
2015-01-11 18:04:13 +00:00
|
|
|
}
|
|
|
|
|
2015-11-26 15:09:33 +00:00
|
|
|
dstIP := net.IPv4(192, 168, 0, 42)
|
2015-01-11 18:04:13 +00:00
|
|
|
routeToDstIP, err := RouteGet(dstIP)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(routeToDstIP) == 0 {
|
|
|
|
t.Fatal("Default route not present")
|
2014-09-01 03:27:34 +00:00
|
|
|
}
|
2015-11-26 15:09:33 +00:00
|
|
|
if err := RouteDel(&route); err != nil {
|
2014-09-01 03:27:34 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
routes, err = RouteList(link, FAMILY_V4)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(routes) != 0 {
|
|
|
|
t.Fatal("Route not removed properly")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2015-02-24 18:19:01 +00:00
|
|
|
|
2017-02-03 19:09:42 +00:00
|
|
|
func TestRouteReplace(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
// get loopback interface
|
|
|
|
link, err := LinkByName("lo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bring the interface up
|
|
|
|
if err := LinkSetUp(link); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a gateway route
|
|
|
|
dst := &net.IPNet{
|
|
|
|
IP: net.IPv4(192, 168, 0, 0),
|
|
|
|
Mask: net.CIDRMask(24, 32),
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := net.IPv4(127, 1, 1, 1)
|
|
|
|
route := Route{LinkIndex: link.Attrs().Index, Dst: dst, Src: ip}
|
|
|
|
if err := RouteAdd(&route); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
routes, err := RouteList(link, FAMILY_V4)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(routes) != 1 {
|
|
|
|
t.Fatal("Route not added properly")
|
|
|
|
}
|
|
|
|
|
|
|
|
ip = net.IPv4(127, 1, 1, 2)
|
|
|
|
route = Route{LinkIndex: link.Attrs().Index, Dst: dst, Src: ip}
|
|
|
|
if err := RouteReplace(&route); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
routes, err = RouteList(link, FAMILY_V4)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(routes) != 1 || !routes[0].Src.Equal(ip) {
|
|
|
|
t.Fatal("Route not replaced properly")
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := RouteDel(&route); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
routes, err = RouteList(link, FAMILY_V4)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(routes) != 0 {
|
|
|
|
t.Fatal("Route not removed properly")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-02-24 18:19:01 +00:00
|
|
|
func TestRouteAddIncomplete(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
// get loopback interface
|
|
|
|
link, err := LinkByName("lo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bring the interface up
|
|
|
|
if err = LinkSetUp(link); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
route := Route{LinkIndex: link.Attrs().Index}
|
|
|
|
if err := RouteAdd(&route); err == nil {
|
|
|
|
t.Fatal("Adding incomplete route should fail")
|
|
|
|
}
|
|
|
|
}
|
2015-08-19 15:31:39 +00:00
|
|
|
|
|
|
|
func expectRouteUpdate(ch <-chan RouteUpdate, t uint16, dst net.IP) bool {
|
|
|
|
for {
|
|
|
|
timeout := time.After(time.Minute)
|
|
|
|
select {
|
|
|
|
case update := <-ch:
|
|
|
|
if update.Type == t && update.Route.Dst.IP.Equal(dst) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
case <-timeout:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRouteSubscribe(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
ch := make(chan RouteUpdate)
|
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
if err := RouteSubscribe(ch, done); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get loopback interface
|
|
|
|
link, err := LinkByName("lo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bring the interface up
|
|
|
|
if err = LinkSetUp(link); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a gateway route
|
2015-11-26 15:09:33 +00:00
|
|
|
dst := &net.IPNet{
|
|
|
|
IP: net.IPv4(192, 168, 0, 0),
|
|
|
|
Mask: net.CIDRMask(24, 32),
|
|
|
|
}
|
2015-08-19 15:31:39 +00:00
|
|
|
|
2015-11-26 15:09:33 +00:00
|
|
|
ip := net.IPv4(127, 1, 1, 1)
|
2015-08-19 15:31:39 +00:00
|
|
|
route := Route{LinkIndex: link.Attrs().Index, Dst: dst, Src: ip}
|
2015-11-26 15:09:33 +00:00
|
|
|
if err := RouteAdd(&route); err != nil {
|
2015-08-19 15:31:39 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !expectRouteUpdate(ch, syscall.RTM_NEWROUTE, dst.IP) {
|
|
|
|
t.Fatal("Add update not received as expected")
|
|
|
|
}
|
2015-11-26 15:09:33 +00:00
|
|
|
if err := RouteDel(&route); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !expectRouteUpdate(ch, syscall.RTM_DELROUTE, dst.IP) {
|
|
|
|
t.Fatal("Del update not received as expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-29 16:11:35 +00:00
|
|
|
func TestRouteSubscribeAt(t *testing.T) {
|
2017-02-06 16:14:07 +00:00
|
|
|
skipUnlessRoot(t)
|
|
|
|
|
2016-06-29 16:11:35 +00:00
|
|
|
// Create an handle on a custom netns
|
|
|
|
newNs, err := netns.New()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer newNs.Close()
|
|
|
|
|
|
|
|
nh, err := NewHandleAt(newNs)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer nh.Delete()
|
|
|
|
|
|
|
|
// Subscribe for Route events on the custom netns
|
|
|
|
ch := make(chan RouteUpdate)
|
|
|
|
done := make(chan struct{})
|
|
|
|
defer close(done)
|
|
|
|
if err := RouteSubscribeAt(newNs, ch, done); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// get loopback interface
|
|
|
|
link, err := nh.LinkByName("lo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// bring the interface up
|
|
|
|
if err = nh.LinkSetUp(link); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a gateway route
|
|
|
|
dst := &net.IPNet{
|
|
|
|
IP: net.IPv4(192, 169, 0, 0),
|
|
|
|
Mask: net.CIDRMask(24, 32),
|
|
|
|
}
|
|
|
|
|
|
|
|
ip := net.IPv4(127, 100, 1, 1)
|
|
|
|
route := Route{LinkIndex: link.Attrs().Index, Dst: dst, Src: ip}
|
|
|
|
if err := nh.RouteAdd(&route); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !expectRouteUpdate(ch, syscall.RTM_NEWROUTE, dst.IP) {
|
|
|
|
t.Fatal("Add update not received as expected")
|
|
|
|
}
|
|
|
|
if err := nh.RouteDel(&route); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !expectRouteUpdate(ch, syscall.RTM_DELROUTE, dst.IP) {
|
|
|
|
t.Fatal("Del update not received as expected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-26 15:09:33 +00:00
|
|
|
func TestRouteExtraFields(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
// get loopback interface
|
|
|
|
link, err := LinkByName("lo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// bring the interface up
|
|
|
|
if err = LinkSetUp(link); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-08-19 15:31:39 +00:00
|
|
|
|
2015-11-26 15:09:33 +00:00
|
|
|
// add a gateway route
|
|
|
|
dst := &net.IPNet{
|
|
|
|
IP: net.IPv4(1, 1, 1, 1),
|
|
|
|
Mask: net.CIDRMask(32, 32),
|
|
|
|
}
|
|
|
|
|
|
|
|
src := net.IPv4(127, 3, 3, 3)
|
|
|
|
route := Route{
|
|
|
|
LinkIndex: link.Attrs().Index,
|
|
|
|
Dst: dst,
|
|
|
|
Src: src,
|
|
|
|
Scope: syscall.RT_SCOPE_LINK,
|
|
|
|
Priority: 13,
|
|
|
|
Table: syscall.RT_TABLE_MAIN,
|
|
|
|
Type: syscall.RTN_UNICAST,
|
2015-11-26 15:31:53 +00:00
|
|
|
Tos: 14,
|
2015-11-26 15:09:33 +00:00
|
|
|
}
|
|
|
|
if err := RouteAdd(&route); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-12-08 22:35:55 +00:00
|
|
|
routes, err := RouteListFiltered(FAMILY_V4, &Route{
|
|
|
|
Dst: dst,
|
|
|
|
Src: src,
|
|
|
|
Scope: syscall.RT_SCOPE_LINK,
|
|
|
|
Table: syscall.RT_TABLE_MAIN,
|
|
|
|
Type: syscall.RTN_UNICAST,
|
|
|
|
Tos: 14,
|
|
|
|
}, RT_FILTER_DST|RT_FILTER_SRC|RT_FILTER_SCOPE|RT_FILTER_TABLE|RT_FILTER_TYPE|RT_FILTER_TOS)
|
2015-08-19 15:31:39 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2015-11-26 15:09:33 +00:00
|
|
|
if len(routes) != 1 {
|
|
|
|
t.Fatal("Route not added properly")
|
|
|
|
}
|
2015-08-19 15:31:39 +00:00
|
|
|
|
2015-11-26 15:09:33 +00:00
|
|
|
if routes[0].Scope != syscall.RT_SCOPE_LINK {
|
|
|
|
t.Fatal("Invalid Scope. Route not added properly")
|
|
|
|
}
|
|
|
|
if routes[0].Priority != 13 {
|
|
|
|
t.Fatal("Invalid Priority. Route not added properly")
|
|
|
|
}
|
|
|
|
if routes[0].Table != syscall.RT_TABLE_MAIN {
|
|
|
|
t.Fatal("Invalid Scope. Route not added properly")
|
|
|
|
}
|
|
|
|
if routes[0].Type != syscall.RTN_UNICAST {
|
|
|
|
t.Fatal("Invalid Type. Route not added properly")
|
2015-08-19 15:31:39 +00:00
|
|
|
}
|
2015-11-26 15:31:53 +00:00
|
|
|
if routes[0].Tos != 14 {
|
|
|
|
t.Fatal("Invalid Tos. Route not added properly")
|
|
|
|
}
|
2015-08-19 15:31:39 +00:00
|
|
|
}
|
2016-06-15 05:02:24 +00:00
|
|
|
|
|
|
|
func TestRouteMultiPath(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
// get loopback interface
|
|
|
|
link, err := LinkByName("lo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// bring the interface up
|
|
|
|
if err = LinkSetUp(link); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add a gateway route
|
|
|
|
dst := &net.IPNet{
|
|
|
|
IP: net.IPv4(192, 168, 0, 0),
|
|
|
|
Mask: net.CIDRMask(24, 32),
|
|
|
|
}
|
|
|
|
|
|
|
|
idx := link.Attrs().Index
|
|
|
|
route := Route{Dst: dst, MultiPath: []*NexthopInfo{&NexthopInfo{LinkIndex: idx}, &NexthopInfo{LinkIndex: idx}}}
|
|
|
|
if err := RouteAdd(&route); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
routes, err := RouteList(nil, FAMILY_V4)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(routes) != 1 {
|
|
|
|
t.Fatal("MultiPath Route not added properly")
|
|
|
|
}
|
|
|
|
if len(routes[0].MultiPath) != 2 {
|
|
|
|
t.Fatal("MultiPath Route not added properly")
|
|
|
|
}
|
|
|
|
}
|
2016-11-20 00:14:08 +00:00
|
|
|
|
|
|
|
func TestFilterDefaultRoute(t *testing.T) {
|
|
|
|
tearDown := setUpNetlinkTest(t)
|
|
|
|
defer tearDown()
|
|
|
|
|
|
|
|
// get loopback interface
|
|
|
|
link, err := LinkByName("lo")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
// bring the interface up
|
|
|
|
if err = LinkSetUp(link); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
address := &Addr{
|
|
|
|
IPNet: &net.IPNet{
|
|
|
|
IP: net.IPv4(127, 0, 0, 2),
|
|
|
|
Mask: net.CIDRMask(24, 32),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
if err = AddrAdd(link, address); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add default route
|
|
|
|
gw := net.IPv4(127, 0, 0, 2)
|
|
|
|
|
|
|
|
defaultRoute := Route{
|
|
|
|
Dst: nil,
|
|
|
|
Gw: gw,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := RouteAdd(&defaultRoute); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// add an extra route
|
|
|
|
dst := &net.IPNet{
|
|
|
|
IP: net.IPv4(192, 168, 0, 0),
|
|
|
|
Mask: net.CIDRMask(24, 32),
|
|
|
|
}
|
|
|
|
|
|
|
|
extraRoute := Route{
|
|
|
|
Dst: dst,
|
|
|
|
Gw: gw,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := RouteAdd(&extraRoute); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
var filterTests = []struct {
|
|
|
|
filter *Route
|
|
|
|
mask uint64
|
|
|
|
expected net.IP
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
&Route{Dst: nil},
|
|
|
|
RT_FILTER_DST,
|
|
|
|
gw,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
&Route{Dst: dst},
|
|
|
|
RT_FILTER_DST,
|
|
|
|
gw,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, f := range filterTests {
|
|
|
|
routes, err := RouteListFiltered(FAMILY_V4, f.filter, f.mask)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if len(routes) != 1 {
|
|
|
|
t.Fatal("Route not filtered properly")
|
|
|
|
}
|
|
|
|
if !routes[0].Gw.Equal(gw) {
|
|
|
|
t.Fatal("Unexpected Gateway")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|