Skip to content

Commit a2e4b9a

Browse files
gwenyaaboch
authored andcommitted
veth: allow configuring peer attributes beyond namespace and address
Signed-off-by: Gwendolyn <[email protected]>
1 parent 9d88d83 commit a2e4b9a

File tree

3 files changed

+163
-24
lines changed

3 files changed

+163
-24
lines changed

link.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,17 @@ type Veth struct {
426426
PeerName string // veth on create only
427427
PeerHardwareAddr net.HardwareAddr
428428
PeerNamespace interface{}
429+
PeerTxQLen int
430+
PeerNumTxQueues uint32
431+
PeerNumRxQueues uint32
432+
PeerMTU uint32
433+
}
434+
435+
func NewVeth(attr LinkAttrs) *Veth {
436+
return &Veth{
437+
LinkAttrs: attr,
438+
PeerTxQLen: -1,
439+
}
429440
}
430441

431442
func (veth *Veth) Attrs() *LinkAttrs {

link_linux.go

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1696,16 +1696,25 @@ func (h *Handle) linkModify(link Link, flags int) error {
16961696
peer := data.AddRtAttr(nl.VETH_INFO_PEER, nil)
16971697
nl.NewIfInfomsgChild(peer, unix.AF_UNSPEC)
16981698
peer.AddRtAttr(unix.IFLA_IFNAME, nl.ZeroTerminated(link.PeerName))
1699-
if base.TxQLen >= 0 {
1699+
1700+
if link.PeerTxQLen >= 0 {
1701+
peer.AddRtAttr(unix.IFLA_TXQLEN, nl.Uint32Attr(uint32(link.PeerTxQLen)))
1702+
} else if base.TxQLen >= 0 {
17001703
peer.AddRtAttr(unix.IFLA_TXQLEN, nl.Uint32Attr(uint32(base.TxQLen)))
17011704
}
1702-
if base.NumTxQueues > 0 {
1705+
if link.PeerNumTxQueues > 0 {
1706+
peer.AddRtAttr(unix.IFLA_NUM_TX_QUEUES, nl.Uint32Attr(link.PeerNumTxQueues))
1707+
} else if base.NumTxQueues > 0 {
17031708
peer.AddRtAttr(unix.IFLA_NUM_TX_QUEUES, nl.Uint32Attr(uint32(base.NumTxQueues)))
17041709
}
1705-
if base.NumRxQueues > 0 {
1710+
if link.PeerNumRxQueues > 0 {
1711+
peer.AddRtAttr(unix.IFLA_NUM_RX_QUEUES, nl.Uint32Attr(link.PeerNumRxQueues))
1712+
} else if base.NumRxQueues > 0 {
17061713
peer.AddRtAttr(unix.IFLA_NUM_RX_QUEUES, nl.Uint32Attr(uint32(base.NumRxQueues)))
17071714
}
1708-
if base.MTU > 0 {
1715+
if link.PeerMTU > 0 {
1716+
peer.AddRtAttr(unix.IFLA_MTU, nl.Uint32Attr(link.PeerMTU))
1717+
} else if base.MTU > 0 {
17091718
peer.AddRtAttr(unix.IFLA_MTU, nl.Uint32Attr(uint32(base.MTU)))
17101719
}
17111720
if link.PeerHardwareAddr != nil {

link_test.go

Lines changed: 139 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,17 +1123,16 @@ func TestLinkAddDelVeth(t *testing.T) {
11231123

11241124
peerMAC, _ := net.ParseMAC("00:12:34:56:78:02")
11251125

1126-
veth := &Veth{
1127-
LinkAttrs: LinkAttrs{
1128-
Name: "foo",
1129-
TxQLen: testTxQLen,
1130-
MTU: 1400,
1131-
NumTxQueues: testTxQueues,
1132-
NumRxQueues: testRxQueues,
1133-
},
1134-
PeerName: "bar",
1135-
PeerHardwareAddr: peerMAC,
1136-
}
1126+
veth := NewVeth(LinkAttrs{
1127+
Name: "foo",
1128+
TxQLen: testTxQLen,
1129+
MTU: 1400,
1130+
NumTxQueues: testTxQueues,
1131+
NumRxQueues: testRxQueues,
1132+
})
1133+
1134+
veth.PeerName = "bar"
1135+
veth.PeerHardwareAddr = peerMAC
11371136
testLinkAddDel(t, veth)
11381137
}
11391138

@@ -1168,7 +1167,8 @@ func TestLinkAddVethWithDefaultTxQLen(t *testing.T) {
11681167
la := NewLinkAttrs()
11691168
la.Name = "foo"
11701169

1171-
veth := &Veth{LinkAttrs: la, PeerName: "bar"}
1170+
veth := NewVeth(la)
1171+
veth.PeerName = "bar"
11721172
if err := LinkAdd(veth); err != nil {
11731173
t.Fatal(err)
11741174
}
@@ -1203,7 +1203,8 @@ func TestLinkAddVethWithZeroTxQLen(t *testing.T) {
12031203
la.Name = "foo"
12041204
la.TxQLen = 0
12051205

1206-
veth := &Veth{LinkAttrs: la, PeerName: "bar"}
1206+
veth := NewVeth(la)
1207+
veth.PeerName = "bar"
12071208
if err := LinkAdd(veth); err != nil {
12081209
t.Fatal(err)
12091210
}
@@ -1231,6 +1232,124 @@ func TestLinkAddVethWithZeroTxQLen(t *testing.T) {
12311232
}
12321233
}
12331234

1235+
func TestLinkAddVethWithPeerAttrs(t *testing.T) {
1236+
tearDown := setUpNetlinkTest(t)
1237+
defer tearDown()
1238+
la := NewLinkAttrs()
1239+
la.Name = "foo"
1240+
la.MTU = 1500
1241+
la.TxQLen = 500
1242+
la.NumRxQueues = 2
1243+
la.NumTxQueues = 3
1244+
1245+
veth := NewVeth(la)
1246+
veth.PeerName = "bar"
1247+
veth.PeerMTU = 1400
1248+
veth.PeerTxQLen = 1000
1249+
veth.PeerNumRxQueues = 4
1250+
veth.PeerNumTxQueues = 5
1251+
if err := LinkAdd(veth); err != nil {
1252+
t.Fatal(err)
1253+
}
1254+
link, err := LinkByName("foo")
1255+
if err != nil {
1256+
t.Fatal(err)
1257+
}
1258+
if veth, ok := link.(*Veth); !ok {
1259+
t.Fatalf("unexpected link type: %T", link)
1260+
} else {
1261+
if veth.MTU != 1500 {
1262+
t.Fatalf("MTU is %d, should be %d", veth.MTU, 1500)
1263+
}
1264+
if veth.TxQLen != 500 {
1265+
t.Fatalf("TxQLen is %d, should be %d", veth.TxQLen, 500)
1266+
}
1267+
if veth.NumRxQueues != 2 {
1268+
t.Fatalf("NumRxQueues is %d, should be %d", veth.NumRxQueues, 2)
1269+
}
1270+
if veth.NumTxQueues != 3 {
1271+
t.Fatalf("NumTxQueues is %d, should be %d", veth.NumTxQueues, 3)
1272+
}
1273+
}
1274+
peer, err := LinkByName("bar")
1275+
if err != nil {
1276+
t.Fatal(err)
1277+
}
1278+
if veth, ok := peer.(*Veth); !ok {
1279+
t.Fatalf("unexpected link type: %T", link)
1280+
} else {
1281+
if veth.MTU != 1400 {
1282+
t.Fatalf("Peer MTU is %d, should be %d", veth.MTU, 1400)
1283+
}
1284+
if veth.TxQLen != 1000 {
1285+
t.Fatalf("Peer TxQLen is %d, should be %d", veth.TxQLen, 1000)
1286+
}
1287+
if veth.NumRxQueues != 4 {
1288+
t.Fatalf("Peer NumRxQueues is %d, should be %d", veth.NumRxQueues, 4)
1289+
}
1290+
if veth.NumTxQueues != 5 {
1291+
t.Fatalf("Peer NumTxQueues is %d, should be %d", veth.NumTxQueues, 5)
1292+
}
1293+
}
1294+
}
1295+
1296+
func TestLinkAddVethWithoutPeerAttrs(t *testing.T) {
1297+
tearDown := setUpNetlinkTest(t)
1298+
defer tearDown()
1299+
la := NewLinkAttrs()
1300+
la.Name = "foo"
1301+
la.MTU = 1500
1302+
la.TxQLen = 500
1303+
la.NumRxQueues = 2
1304+
la.NumTxQueues = 3
1305+
1306+
veth := NewVeth(la)
1307+
veth.PeerName = "bar"
1308+
if err := LinkAdd(veth); err != nil {
1309+
t.Fatal(err)
1310+
}
1311+
link, err := LinkByName("foo")
1312+
if err != nil {
1313+
t.Fatal(err)
1314+
}
1315+
if veth, ok := link.(*Veth); !ok {
1316+
t.Fatalf("unexpected link type: %T", link)
1317+
} else {
1318+
if veth.MTU != 1500 {
1319+
t.Fatalf("MTU is %d, should be %d", veth.MTU, 1500)
1320+
}
1321+
if veth.TxQLen != 500 {
1322+
t.Fatalf("TxQLen is %d, should be %d", veth.TxQLen, 500)
1323+
}
1324+
if veth.NumRxQueues != 2 {
1325+
t.Fatalf("NumRxQueues is %d, should be %d", veth.NumRxQueues, 2)
1326+
}
1327+
if veth.NumTxQueues != 3 {
1328+
t.Fatalf("NumTxQueues is %d, should be %d", veth.NumTxQueues, 3)
1329+
}
1330+
}
1331+
peer, err := LinkByName("bar")
1332+
if err != nil {
1333+
t.Fatal(err)
1334+
}
1335+
if veth, ok := peer.(*Veth); !ok {
1336+
t.Fatalf("unexpected link type: %T", link)
1337+
} else {
1338+
if veth.MTU != 1500 {
1339+
t.Fatalf("Peer MTU is %d, should be %d", veth.MTU, 1500)
1340+
}
1341+
if veth.TxQLen != 500 {
1342+
t.Fatalf("Peer TxQLen is %d, should be %d", veth.TxQLen, 500)
1343+
}
1344+
if veth.NumRxQueues != 2 {
1345+
t.Fatalf("Peer NumRxQueues is %d, should be %d", veth.NumRxQueues, 2)
1346+
}
1347+
if veth.NumTxQueues != 3 {
1348+
t.Fatalf("Peer NumTxQueues is %d, should be %d", veth.NumTxQueues, 3)
1349+
}
1350+
}
1351+
}
1352+
12341353
func TestLinkAddDelDummyWithGSO(t *testing.T) {
12351354
const (
12361355
gsoMaxSegs = 16
@@ -1449,7 +1568,7 @@ func TestLinkSetNs(t *testing.T) {
14491568
}
14501569
defer newns.Close()
14511570

1452-
link := &Veth{LinkAttrs{Name: "foo"}, "bar", nil, nil}
1571+
link := &Veth{LinkAttrs: LinkAttrs{Name: "foo"}, PeerName: "bar"}
14531572
if err := LinkAdd(link); err != nil {
14541573
t.Fatal(err)
14551574
}
@@ -1520,7 +1639,7 @@ func TestVethPeerNs(t *testing.T) {
15201639
}
15211640
defer newns.Close()
15221641

1523-
link := &Veth{LinkAttrs{Name: "foo"}, "bar", nil, NsFd(basens)}
1642+
link := &Veth{LinkAttrs: LinkAttrs{Name: "foo"}, PeerName: "bar", PeerNamespace: NsFd(basens)}
15241643
if err := LinkAdd(link); err != nil {
15251644
t.Fatal(err)
15261645
}
@@ -1573,7 +1692,7 @@ func TestVethPeerNs2(t *testing.T) {
15731692
}
15741693
defer twons.Close()
15751694

1576-
link := &Veth{LinkAttrs{Name: "foo", Namespace: NsFd(onens)}, "bar", nil, NsFd(basens)}
1695+
link := &Veth{LinkAttrs: LinkAttrs{Name: "foo", Namespace: NsFd(onens)}, PeerName: "bar", PeerNamespace: NsFd(basens)}
15771696
if err := LinkAdd(link); err != nil {
15781697
t.Fatal(err)
15791698
}
@@ -2132,7 +2251,7 @@ func TestLinkSubscribe(t *testing.T) {
21322251
t.Fatal(err)
21332252
}
21342253

2135-
link := &Veth{LinkAttrs{Name: "foo", TxQLen: testTxQLen, MTU: 1400}, "bar", nil, nil}
2254+
link := &Veth{LinkAttrs: LinkAttrs{Name: "foo", TxQLen: testTxQLen, MTU: 1400}, PeerName: "bar"}
21362255
if err := LinkAdd(link); err != nil {
21372256
t.Fatal(err)
21382257
}
@@ -2179,7 +2298,7 @@ func TestLinkSubscribeWithOptions(t *testing.T) {
21792298
t.Fatal(err)
21802299
}
21812300

2182-
link := &Veth{LinkAttrs{Name: "foo", TxQLen: testTxQLen, MTU: 1400}, "bar", nil, nil}
2301+
link := &Veth{LinkAttrs: LinkAttrs{Name: "foo", TxQLen: testTxQLen, MTU: 1400}, PeerName: "bar"}
21832302
if err := LinkAdd(link); err != nil {
21842303
t.Fatal(err)
21852304
}
@@ -2213,7 +2332,7 @@ func TestLinkSubscribeAt(t *testing.T) {
22132332
t.Fatal(err)
22142333
}
22152334

2216-
link := &Veth{LinkAttrs{Name: "test", TxQLen: testTxQLen, MTU: 1400}, "bar", nil, nil}
2335+
link := &Veth{LinkAttrs: LinkAttrs{Name: "test", TxQLen: testTxQLen, MTU: 1400}, PeerName: "bar"}
22172336
if err := nh.LinkAdd(link); err != nil {
22182337
t.Fatal(err)
22192338
}
@@ -2255,7 +2374,7 @@ func TestLinkSubscribeListExisting(t *testing.T) {
22552374
}
22562375
defer nh.Close()
22572376

2258-
link := &Veth{LinkAttrs{Name: "test", TxQLen: testTxQLen, MTU: 1400}, "bar", nil, nil}
2377+
link := &Veth{LinkAttrs: LinkAttrs{Name: "test", TxQLen: testTxQLen, MTU: 1400}, PeerName: "bar"}
22592378
if err := nh.LinkAdd(link); err != nil {
22602379
t.Fatal(err)
22612380
}

0 commit comments

Comments
 (0)