Skip to content
This repository was archived by the owner on May 12, 2021. It is now read-only.

Commit c5d79eb

Browse files
committed
ipv6: Add support for ipv6 for netmon as well.
Netmon should now handle ipv6 addresses and routes as well. Signed-off-by: Archana Shinde <[email protected]>
1 parent b169476 commit c5d79eb

File tree

2 files changed

+87
-20
lines changed

2 files changed

+87
-20
lines changed

netmon/netmon.go

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ var (
5252
version = "unknown"
5353

5454
// For simplicity the code will only focus on IPv4 addresses for now.
55-
netlinkFamily = netlink.FAMILY_V4
55+
netlinkFamily = netlink.FAMILY_ALL
5656

5757
storageParentPath = "/var/run/kata-containers/netmon/sbs"
5858
)
@@ -275,11 +275,16 @@ func convertInterface(linkAttrs *netlink.LinkAttrs, linkType string, addrs []net
275275
netMask, _ := addr.Mask.Size()
276276

277277
ipAddr := &vcTypes.IPAddress{
278-
Family: netlinkFamily,
279278
Address: addr.IP.String(),
280279
Mask: fmt.Sprintf("%d", netMask),
281280
}
282281

282+
if addr.IP.To4() != nil {
283+
ipAddr.Family = netlink.FAMILY_V4
284+
} else {
285+
ipAddr.Family = netlink.FAMILY_V6
286+
}
287+
283288
ipAddrs = append(ipAddrs, ipAddr)
284289
}
285290

@@ -303,8 +308,6 @@ func convertInterface(linkAttrs *netlink.LinkAttrs, linkType string, addrs []net
303308
func convertRoutes(netRoutes []netlink.Route) []vcTypes.Route {
304309
var routes []vcTypes.Route
305310

306-
// Ignore routes with IPv6 addresses as this is not supported
307-
// by Kata yet.
308311
for _, netRoute := range netRoutes {
309312
dst := ""
310313

@@ -313,25 +316,30 @@ func convertRoutes(netRoutes []netlink.Route) []vcTypes.Route {
313316
}
314317

315318
if netRoute.Dst != nil {
316-
if netRoute.Dst.IP.To4() != nil {
319+
dst = netRoute.Dst.String()
320+
if netRoute.Dst.IP.To4() != nil || netRoute.Dst.IP.To16() != nil {
317321
dst = netRoute.Dst.String()
318322
} else {
319-
netmonLog.WithField("destination", netRoute.Dst.IP.String()).Warn("Not IPv4 format")
323+
netmonLog.WithField("destination", netRoute.Dst.IP.String()).Warn("Unexpected network address format")
320324
}
321325
}
322326

323327
src := ""
324-
if netRoute.Src.To4() != nil {
325-
src = netRoute.Src.String()
326-
} else {
327-
netmonLog.WithField("source", netRoute.Src.String()).Warn("Not IPv4 format")
328+
if netRoute.Src != nil {
329+
if netRoute.Src.To4() != nil || netRoute.Src.To16() != nil {
330+
src = netRoute.Src.String()
331+
} else {
332+
netmonLog.WithField("source", netRoute.Src.String()).Warn("Unexpected network address format")
333+
}
328334
}
329335

330336
gw := ""
331-
if netRoute.Gw.To4() != nil {
332-
gw = netRoute.Gw.String()
333-
} else {
334-
netmonLog.WithField("gateway", netRoute.Gw.String()).Warn("Not IPv4 format")
337+
if netRoute.Gw != nil {
338+
if netRoute.Gw.To4() != nil || netRoute.Gw.To16() != nil {
339+
gw = netRoute.Gw.String()
340+
} else {
341+
netmonLog.WithField("gateway", netRoute.Gw.String()).Warn("Unexpected network address format")
342+
}
335343
}
336344

337345
dev := ""

netmon/netmon_test.go

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package main
77

88
import (
99
"encoding/json"
10+
"fmt"
1011
"io/ioutil"
1112
"net"
1213
"os"
@@ -37,6 +38,8 @@ const (
3738
testHwAddr = "02:00:ca:fe:00:48"
3839
testIPAddress = "192.168.0.15"
3940
testIPAddressWithMask = "192.168.0.15/32"
41+
testIP6Address = "2001:db8:1::242:ac11:2"
42+
testIP6AddressWithMask = "2001:db8:1::/64"
4043
testScope = 1
4144
testTxQLen = -1
4245
testIfaceIndex = 5
@@ -169,6 +172,11 @@ func TestConvertInterface(t *testing.T) {
169172
IP: net.ParseIP(testIPAddress),
170173
},
171174
},
175+
{
176+
IPNet: &net.IPNet{
177+
IP: net.ParseIP(testIP6Address),
178+
},
179+
},
172180
}
173181

174182
linkAttrs := &netlink.LinkAttrs{
@@ -186,15 +194,21 @@ func TestConvertInterface(t *testing.T) {
186194
HwAddr: testHwAddr,
187195
IPAddresses: []*vcTypes.IPAddress{
188196
{
189-
Family: netlinkFamily,
197+
Family: netlink.FAMILY_V4,
190198
Address: testIPAddress,
191199
Mask: "0",
192200
},
201+
{
202+
Family: netlink.FAMILY_V6,
203+
Address: testIP6Address,
204+
Mask: "0",
205+
},
193206
},
194207
LinkType: linkType,
195208
}
196209

197210
got := convertInterface(linkAttrs, linkType, addrs)
211+
198212
assert.True(t, reflect.DeepEqual(expected, got),
199213
"Got %+v\nExpected %+v", got, expected)
200214
}
@@ -204,6 +218,10 @@ func TestConvertRoutes(t *testing.T) {
204218
assert.Nil(t, err)
205219
assert.NotNil(t, ipNet)
206220

221+
_, ip6Net, err := net.ParseCIDR(testIP6AddressWithMask)
222+
assert.Nil(t, err)
223+
assert.NotNil(t, ipNet)
224+
207225
routes := []netlink.Route{
208226
{
209227
Dst: ipNet,
@@ -212,6 +230,13 @@ func TestConvertRoutes(t *testing.T) {
212230
LinkIndex: -1,
213231
Scope: testScope,
214232
},
233+
{
234+
Dst: ip6Net,
235+
Src: nil,
236+
Gw: nil,
237+
LinkIndex: -1,
238+
Scope: testScope,
239+
},
215240
}
216241

217242
expected := []vcTypes.Route{
@@ -221,6 +246,12 @@ func TestConvertRoutes(t *testing.T) {
221246
Source: testIPAddress,
222247
Scope: uint32(testScope),
223248
},
249+
{
250+
Dest: testIP6AddressWithMask,
251+
Gateway: "",
252+
Source: "",
253+
Scope: uint32(testScope),
254+
},
224255
}
225256

226257
got := convertRoutes(routes)
@@ -269,12 +300,40 @@ func testCreateDummyNetwork(t *testing.T, handler *netlink.Handle) (int, vcTypes
269300
attrs := link.Attrs()
270301
assert.NotNil(t, attrs)
271302

303+
addrs, err := handler.AddrList(link, netlinkFamily)
304+
assert.Nil(t, err)
305+
306+
var ipAddrs []*vcTypes.IPAddress
307+
308+
// Scan addresses for ipv6 link local address which is automatically assigned
309+
for _, addr := range addrs {
310+
if addr.IPNet == nil {
311+
continue
312+
}
313+
314+
netMask, _ := addr.Mask.Size()
315+
316+
ipAddr := &vcTypes.IPAddress{
317+
Address: addr.IP.String(),
318+
Mask: fmt.Sprintf("%d", netMask),
319+
}
320+
321+
if addr.IP.To4() != nil {
322+
ipAddr.Family = netlink.FAMILY_V4
323+
} else {
324+
ipAddr.Family = netlink.FAMILY_V6
325+
}
326+
327+
ipAddrs = append(ipAddrs, ipAddr)
328+
}
329+
272330
iface := vcTypes.Interface{
273-
Device: testIfaceName,
274-
Name: testIfaceName,
275-
Mtu: uint64(testMTU),
276-
HwAddr: testHwAddr,
277-
LinkType: link.Type(),
331+
Device: testIfaceName,
332+
Name: testIfaceName,
333+
Mtu: uint64(testMTU),
334+
HwAddr: testHwAddr,
335+
LinkType: link.Type(),
336+
IPAddresses: ipAddrs,
278337
}
279338

280339
return attrs.Index, iface

0 commit comments

Comments
 (0)