4 Star 1 Fork 0

Gitee 极速下载/netlink

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/vishvananda/netlink
克隆/下载
link_test.go 78.63 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546
//go:build linux
// +build linux
package netlink
import (
"bytes"
"errors"
"fmt"
"net"
"os"
"os/exec"
"sort"
"strings"
"syscall"
"testing"
"time"
"github.com/vishvananda/netlink/nl"
"github.com/vishvananda/netns"
"golang.org/x/sys/unix"
)
const (
testTxQLen int = 100
defaultTxQLen int = 1000
testTxQueues int = 4
testRxQueues int = 8
)
func testLinkAddDel(t *testing.T, link Link) {
_, err := LinkList()
if err != nil {
t.Fatal(err)
}
if err := LinkAdd(link); err != nil {
t.Fatal(err)
}
base := link.Attrs()
result, err := LinkByName(base.Name)
if err != nil {
t.Fatal(err)
}
rBase := result.Attrs()
if base.Index != 0 {
if base.Index != rBase.Index {
t.Fatalf("index is %d, should be %d", rBase.Index, base.Index)
}
}
if base.Group > 0 {
if base.Group != rBase.Group {
t.Fatalf("group is %d, should be %d", rBase.Group, base.Group)
}
}
if vlan, ok := link.(*Vlan); ok {
other, ok := result.(*Vlan)
if !ok {
t.Fatal("Result of create is not a vlan")
}
if vlan.VlanId != other.VlanId {
t.Fatal("Link.VlanId id doesn't match")
}
}
if resultPrimary, ok := result.(*Netkit); ok {
if inputPrimary, ok := link.(*Netkit); ok {
if resultPrimary.Policy != inputPrimary.Policy {
t.Fatalf("Policy is %d, should be %d", int(resultPrimary.Policy), int(inputPrimary.Policy))
}
if resultPrimary.PeerPolicy != inputPrimary.PeerPolicy {
t.Fatalf("Peer Policy is %d, should be %d", int(resultPrimary.PeerPolicy), int(inputPrimary.PeerPolicy))
}
if resultPrimary.Mode != inputPrimary.Mode {
t.Fatalf("Mode is %d, should be %d", int(resultPrimary.Mode), int(inputPrimary.Mode))
}
if resultPrimary.SupportsScrub() && resultPrimary.Scrub != inputPrimary.Scrub {
t.Fatalf("Scrub is %d, should be %d", int(resultPrimary.Scrub), int(inputPrimary.Scrub))
}
if resultPrimary.SupportsScrub() && resultPrimary.PeerScrub != inputPrimary.PeerScrub {
t.Fatalf("Peer Scrub is %d, should be %d", int(resultPrimary.PeerScrub), int(inputPrimary.PeerScrub))
}
if inputPrimary.peerLinkAttrs.Name != "" {
var resultPeer *Netkit
pLink, err := LinkByName(inputPrimary.peerLinkAttrs.Name)
if err != nil {
t.Fatalf("Failed to get Peer netkit %s", inputPrimary.peerLinkAttrs.Name)
}
if resultPeer, ok = pLink.(*Netkit); !ok {
t.Fatalf("Peer %s is incorrect type", inputPrimary.peerLinkAttrs.Name)
}
if resultPrimary.PeerPolicy != resultPeer.Policy {
t.Fatalf("Peer Policy from primary is %d, should be %d", int(resultPrimary.PeerPolicy), int(resultPeer.Policy))
}
if resultPeer.PeerPolicy != resultPrimary.Policy {
t.Fatalf("PeerPolicy from peer is %d, should be %d", int(resultPeer.PeerPolicy), int(resultPrimary.Policy))
}
if resultPrimary.Mode != resultPeer.Mode {
t.Fatalf("Peer Mode from primary is %d, should be %d", int(resultPrimary.Mode), int(resultPeer.Mode))
}
if resultPrimary.IsPrimary() == resultPeer.IsPrimary() {
t.Fatalf("Both primary and peer device has the same value in IsPrimary() %t", resultPrimary.IsPrimary())
}
if resultPrimary.SupportsScrub() != resultPeer.SupportsScrub() {
t.Fatalf("Peer SupportsScrub() should return %v", resultPrimary.SupportsScrub())
}
if resultPrimary.PeerScrub != resultPeer.Scrub {
t.Fatalf("Scrub from peer is %d, should be %d", int(resultPeer.Scrub), int(resultPrimary.PeerScrub))
}
if resultPrimary.Scrub != resultPeer.PeerScrub {
t.Fatalf("PeerScrub from peer is %d, should be %d", int(resultPeer.PeerScrub), int(resultPrimary.Scrub))
}
}
}
}
if veth, ok := result.(*Veth); ok {
if rBase.TxQLen != base.TxQLen {
t.Fatalf("qlen is %d, should be %d", rBase.TxQLen, base.TxQLen)
}
if rBase.NumTxQueues != base.NumTxQueues {
t.Fatalf("txQueues is %d, should be %d", rBase.NumTxQueues, base.NumTxQueues)
}
if rBase.NumRxQueues != base.NumRxQueues {
t.Fatalf("rxQueues is %d, should be %d", rBase.NumRxQueues, base.NumRxQueues)
}
if rBase.MTU != base.MTU {
t.Fatalf("MTU is %d, should be %d", rBase.MTU, base.MTU)
}
if original, ok := link.(*Veth); ok {
if original.PeerName != "" {
var peer *Veth
other, err := LinkByName(original.PeerName)
if err != nil {
t.Fatalf("Peer %s not created", veth.PeerName)
}
if peer, ok = other.(*Veth); !ok {
t.Fatalf("Peer %s is incorrect type", veth.PeerName)
}
if peer.TxQLen != testTxQLen {
t.Fatalf("TxQLen of peer is %d, should be %d", peer.TxQLen, testTxQLen)
}
if peer.NumTxQueues != testTxQueues {
t.Fatalf("NumTxQueues of peer is %d, should be %d", peer.NumTxQueues, testTxQueues)
}
if peer.NumRxQueues != testRxQueues {
t.Fatalf("NumRxQueues of peer is %d, should be %d", peer.NumRxQueues, testRxQueues)
}
if !bytes.Equal(peer.Attrs().HardwareAddr, original.PeerHardwareAddr) {
t.Fatalf("Peer MAC addr is %s, should be %s", peer.Attrs().HardwareAddr, original.PeerHardwareAddr)
}
}
}
}
if _, ok := result.(*Veth); !ok {
if _, ok := result.(*Netkit); !ok {
// recent kernels set the parent index for veths/netkit in the response
if rBase.ParentIndex == 0 && base.ParentIndex != 0 {
t.Fatalf("Created link doesn't have parent %d but it should", base.ParentIndex)
} else if rBase.ParentIndex != 0 && base.ParentIndex == 0 {
t.Fatalf("Created link has parent %d but it shouldn't", rBase.ParentIndex)
} else if rBase.ParentIndex != 0 && base.ParentIndex != 0 {
if rBase.ParentIndex != base.ParentIndex {
t.Fatalf("Link.ParentIndex doesn't match %d != %d", rBase.ParentIndex, base.ParentIndex)
}
}
}
}
if _, ok := link.(*Wireguard); ok {
_, ok := result.(*Wireguard)
if !ok {
t.Fatal("Result of create is not a wireguard")
}
}
if vxlan, ok := link.(*Vxlan); ok {
other, ok := result.(*Vxlan)
if !ok {
t.Fatal("Result of create is not a vxlan")
}
compareVxlan(t, vxlan, other)
}
if ipv, ok := link.(*IPVlan); ok {
other, ok := result.(*IPVlan)
if !ok {
t.Fatal("Result of create is not a ipvlan")
}
if ipv.Mode != other.Mode {
t.Fatalf("Got unexpected mode: %d, expected: %d", other.Mode, ipv.Mode)
}
if ipv.Flag != other.Flag {
t.Fatalf("Got unexpected flag: %d, expected: %d", other.Flag, ipv.Flag)
}
}
if macv, ok := link.(*Macvlan); ok {
other, ok := result.(*Macvlan)
if !ok {
t.Fatal("Result of create is not a macvlan")
}
if macv.Mode != other.Mode {
t.Fatalf("Got unexpected mode: %d, expected: %d", other.Mode, macv.Mode)
}
if other.BCQueueLen > 0 || other.UsedBCQueueLen > 0 {
if other.UsedBCQueueLen < other.BCQueueLen {
t.Fatalf("UsedBCQueueLen (%d) is smaller than BCQueueLen (%d)", other.UsedBCQueueLen, other.BCQueueLen)
}
}
if macv.BCQueueLen > 0 {
if macv.BCQueueLen != other.BCQueueLen {
t.Fatalf("BCQueueLen not set correctly: %d, expected: %d", other.BCQueueLen, macv.BCQueueLen)
}
}
}
if macv, ok := link.(*Macvtap); ok {
other, ok := result.(*Macvtap)
if !ok {
t.Fatal("Result of create is not a macvtap")
}
if macv.Mode != other.Mode {
t.Fatalf("Got unexpected mode: %d, expected: %d", other.Mode, macv.Mode)
}
if other.BCQueueLen > 0 || other.UsedBCQueueLen > 0 {
if other.UsedBCQueueLen < other.BCQueueLen {
t.Fatalf("UsedBCQueueLen (%d) is smaller than BCQueueLen (%d)", other.UsedBCQueueLen, other.BCQueueLen)
}
}
if macv.BCQueueLen > 0 {
if macv.BCQueueLen != other.BCQueueLen {
t.Fatalf("BCQueueLen not set correctly: %d, expected: %d", other.BCQueueLen, macv.BCQueueLen)
}
}
}
if _, ok := link.(*Vti); ok {
_, ok := result.(*Vti)
if !ok {
t.Fatal("Result of create is not a vti")
}
}
if bond, ok := link.(*Bond); ok {
other, ok := result.(*Bond)
if !ok {
t.Fatal("Result of create is not a bond")
}
if bond.Mode != other.Mode {
t.Fatalf("Got unexpected mode: %d, expected: %d", other.Mode, bond.Mode)
}
if bond.ArpIpTargets != nil {
if other.ArpIpTargets == nil {
t.Fatalf("Got unexpected ArpIpTargets: nil")
}
if len(bond.ArpIpTargets) != len(other.ArpIpTargets) {
t.Fatalf("Got unexpected ArpIpTargets len: %d, expected: %d",
len(other.ArpIpTargets), len(bond.ArpIpTargets))
}
for i := range bond.ArpIpTargets {
if !bond.ArpIpTargets[i].Equal(other.ArpIpTargets[i]) {
t.Fatalf("Got unexpected ArpIpTargets: %s, expected: %s",
other.ArpIpTargets[i], bond.ArpIpTargets[i])
}
}
}
switch mode := bondModeToString[bond.Mode]; mode {
case "802.3ad":
if bond.AdSelect != other.AdSelect {
t.Fatalf("Got unexpected AdSelect: %d, expected: %d", other.AdSelect, bond.AdSelect)
}
if bond.AdActorSysPrio != other.AdActorSysPrio {
t.Fatalf("Got unexpected AdActorSysPrio: %d, expected: %d", other.AdActorSysPrio, bond.AdActorSysPrio)
}
if bond.AdUserPortKey != other.AdUserPortKey {
t.Fatalf("Got unexpected AdUserPortKey: %d, expected: %d", other.AdUserPortKey, bond.AdUserPortKey)
}
if !bytes.Equal(bond.AdActorSystem, other.AdActorSystem) {
t.Fatalf("Got unexpected AdActorSystem: %d, expected: %d", other.AdActorSystem, bond.AdActorSystem)
}
case "balance-tlb":
if bond.TlbDynamicLb != other.TlbDynamicLb {
t.Fatalf("Got unexpected TlbDynamicLb: %d, expected: %d", other.TlbDynamicLb, bond.TlbDynamicLb)
}
}
}
if iptun, ok := link.(*Iptun); ok {
other, ok := result.(*Iptun)
if !ok {
t.Fatal("Result of create is not a iptun")
}
if iptun.FlowBased != other.FlowBased {
t.Fatal("Iptun.FlowBased doesn't match")
}
}
if ip6tnl, ok := link.(*Ip6tnl); ok {
other, ok := result.(*Ip6tnl)
if !ok {
t.Fatal("Result of create is not a ip6tnl")
}
if ip6tnl.FlowBased != other.FlowBased {
t.Fatal("Ip6tnl.FlowBased doesn't match")
}
}
if _, ok := link.(*Sittun); ok {
_, ok := result.(*Sittun)
if !ok {
t.Fatal("Result of create is not a sittun")
}
}
if geneve, ok := link.(*Geneve); ok {
other, ok := result.(*Geneve)
if !ok {
t.Fatal("Result of create is not a Geneve")
}
compareGeneve(t, geneve, other)
}
if gretap, ok := link.(*Gretap); ok {
other, ok := result.(*Gretap)
if !ok {
t.Fatal("Result of create is not a Gretap")
}
compareGretap(t, gretap, other)
}
if gretun, ok := link.(*Gretun); ok {
other, ok := result.(*Gretun)
if !ok {
t.Fatal("Result of create is not a Gretun")
}
compareGretun(t, gretun, other)
}
if xfrmi, ok := link.(*Xfrmi); ok {
other, ok := result.(*Xfrmi)
if !ok {
t.Fatal("Result of create is not a xfrmi")
}
compareXfrmi(t, xfrmi, other)
}
if tuntap, ok := link.(*Tuntap); ok {
other, ok := result.(*Tuntap)
if !ok {
t.Fatal("Result of create is not a tuntap")
}
compareTuntap(t, tuntap, other)
}
if bareudp, ok := link.(*BareUDP); ok {
other, ok := result.(*BareUDP)
if !ok {
t.Fatal("Result of create is not a BareUDP")
}
compareBareUDP(t, bareudp, other)
}
if err = LinkDel(link); err != nil {
t.Fatal(err)
}
links, err := LinkList()
if err != nil {
t.Fatal(err)
}
for _, l := range links {
if l.Attrs().Name == link.Attrs().Name {
t.Fatal("Link not removed properly")
}
}
}
func compareGeneve(t *testing.T, expected, actual *Geneve) {
if actual.ID != expected.ID {
t.Fatalf("Geneve.ID doesn't match: %d %d", actual.ID, expected.ID)
}
// set the Dport to 6081 (the linux default) if it wasn't specified at creation
if expected.Dport == 0 {
expected.Dport = 6081
}
if actual.Dport != expected.Dport {
t.Fatal("Geneve.Dport doesn't match")
}
if actual.Ttl != expected.Ttl {
t.Fatal("Geneve.Ttl doesn't match")
}
if actual.Tos != expected.Tos {
t.Fatal("Geneve.Tos doesn't match")
}
if !actual.Remote.Equal(expected.Remote) {
t.Fatalf("Geneve.Remote is not equal: %s!=%s", actual.Remote, expected.Remote)
}
if actual.FlowBased != expected.FlowBased {
t.Fatal("Geneve.FlowBased doesn't match")
}
if actual.InnerProtoInherit != expected.InnerProtoInherit {
t.Fatal("Geneve.InnerProtoInherit doesn't match")
}
// TODO: we should implement the rest of the geneve methods
}
func compareGretap(t *testing.T, expected, actual *Gretap) {
if actual.IKey != expected.IKey {
t.Fatal("Gretap.IKey doesn't match")
}
if actual.OKey != expected.OKey {
t.Fatal("Gretap.OKey doesn't match")
}
if actual.EncapSport != expected.EncapSport {
t.Fatal("Gretap.EncapSport doesn't match")
}
if actual.EncapDport != expected.EncapDport {
t.Fatal("Gretap.EncapDport doesn't match")
}
if expected.Local != nil && !actual.Local.Equal(expected.Local) {
t.Fatal("Gretap.Local doesn't match")
}
if expected.Remote != nil && !actual.Remote.Equal(expected.Remote) {
t.Fatal("Gretap.Remote doesn't match")
}
if actual.IFlags != expected.IFlags {
t.Fatal("Gretap.IFlags doesn't match")
}
if actual.OFlags != expected.OFlags {
t.Fatal("Gretap.OFlags doesn't match")
}
if actual.PMtuDisc != expected.PMtuDisc {
t.Fatal("Gretap.PMtuDisc doesn't match")
}
if actual.Ttl != expected.Ttl {
t.Fatal("Gretap.Ttl doesn't match")
}
if actual.Tos != expected.Tos {
t.Fatal("Gretap.Tos doesn't match")
}
if actual.EncapType != expected.EncapType {
t.Fatal("Gretap.EncapType doesn't match")
}
if actual.EncapFlags != expected.EncapFlags {
t.Fatal("Gretap.EncapFlags doesn't match")
}
if actual.Link != expected.Link {
t.Fatal("Gretap.Link doesn't match")
}
if actual.FlowBased != expected.FlowBased {
t.Fatal("Gretap.FlowBased doesn't match")
}
}
func compareGretun(t *testing.T, expected, actual *Gretun) {
if actual.Link != expected.Link {
t.Fatal("Gretun.Link doesn't match")
}
if actual.IFlags != expected.IFlags {
t.Fatal("Gretun.IFlags doesn't match")
}
if actual.OFlags != expected.OFlags {
t.Fatal("Gretun.OFlags doesn't match")
}
if actual.IKey != expected.IKey {
t.Fatal("Gretun.IKey doesn't match")
}
if actual.OKey != expected.OKey {
t.Fatal("Gretun.OKey doesn't match")
}
if expected.Local != nil && !actual.Local.Equal(expected.Local) {
t.Fatal("Gretun.Local doesn't match")
}
if expected.Remote != nil && !actual.Remote.Equal(expected.Remote) {
t.Fatal("Gretun.Remote doesn't match")
}
if actual.Ttl != expected.Ttl {
t.Fatal("Gretun.Ttl doesn't match")
}
if actual.Tos != expected.Tos {
t.Fatal("Gretun.Tos doesn't match")
}
if actual.PMtuDisc != expected.PMtuDisc {
t.Fatal("Gretun.PMtuDisc doesn't match")
}
if actual.EncapType != expected.EncapType {
t.Fatal("Gretun.EncapType doesn't match")
}
if actual.EncapFlags != expected.EncapFlags {
t.Fatal("Gretun.EncapFlags doesn't match")
}
if actual.EncapSport != expected.EncapSport {
t.Fatal("Gretun.EncapSport doesn't match")
}
if actual.EncapDport != expected.EncapDport {
t.Fatal("Gretun.EncapDport doesn't match")
}
if actual.FlowBased != expected.FlowBased {
t.Fatal("Gretun.FlowBased doesn't match")
}
}
func compareVxlan(t *testing.T, expected, actual *Vxlan) {
if actual.VxlanId != expected.VxlanId {
t.Fatal("Vxlan.VxlanId doesn't match")
}
if expected.SrcAddr != nil && !actual.SrcAddr.Equal(expected.SrcAddr) {
t.Fatal("Vxlan.SrcAddr doesn't match")
}
if expected.Group != nil && !actual.Group.Equal(expected.Group) {
t.Fatal("Vxlan.Group doesn't match")
}
if expected.TTL != -1 && actual.TTL != expected.TTL {
t.Fatal("Vxlan.TTL doesn't match")
}
if expected.TOS != -1 && actual.TOS != expected.TOS {
t.Fatal("Vxlan.TOS doesn't match")
}
if actual.Learning != expected.Learning {
t.Fatal("Vxlan.Learning doesn't match")
}
if actual.Proxy != expected.Proxy {
t.Fatal("Vxlan.Proxy doesn't match")
}
if actual.RSC != expected.RSC {
t.Fatal("Vxlan.RSC doesn't match")
}
if actual.L2miss != expected.L2miss {
t.Fatal("Vxlan.L2miss doesn't match")
}
if actual.L3miss != expected.L3miss {
t.Fatal("Vxlan.L3miss doesn't match")
}
if actual.GBP != expected.GBP {
t.Fatal("Vxlan.GBP doesn't match")
}
if actual.FlowBased != expected.FlowBased {
t.Fatal("Vxlan.FlowBased doesn't match")
}
if actual.UDP6ZeroCSumTx != expected.UDP6ZeroCSumTx {
t.Fatal("Vxlan.UDP6ZeroCSumTx doesn't match")
}
if actual.UDP6ZeroCSumRx != expected.UDP6ZeroCSumRx {
t.Fatal("Vxlan.UDP6ZeroCSumRx doesn't match")
}
if expected.NoAge {
if !actual.NoAge {
t.Fatal("Vxlan.NoAge doesn't match")
}
} else if expected.Age > 0 && actual.Age != expected.Age {
t.Fatal("Vxlan.Age doesn't match")
}
if expected.Limit > 0 && actual.Limit != expected.Limit {
t.Fatal("Vxlan.Limit doesn't match")
}
if expected.Port > 0 && actual.Port != expected.Port {
t.Fatal("Vxlan.Port doesn't match")
}
if expected.PortLow > 0 || expected.PortHigh > 0 {
if actual.PortLow != expected.PortLow {
t.Fatal("Vxlan.PortLow doesn't match")
}
if actual.PortHigh != expected.PortHigh {
t.Fatal("Vxlan.PortHigh doesn't match")
}
}
}
func compareXfrmi(t *testing.T, expected, actual *Xfrmi) {
if expected.Ifid != actual.Ifid {
t.Fatal("Xfrmi.Ifid doesn't match")
}
}
func compareTuntap(t *testing.T, expected, actual *Tuntap) {
if expected.Mode != actual.Mode {
t.Fatalf("Tuntap.Mode doesn't match: expected : %+v, got %+v", expected.Mode, actual.Mode)
}
if expected.Owner != actual.Owner {
t.Fatal("Tuntap.Owner doesn't match")
}
if expected.Group != actual.Group {
t.Fatal("Tuntap.Group doesn't match")
}
if expected.NonPersist != actual.NonPersist {
t.Fatal("Tuntap.Group doesn't match")
}
}
func compareBareUDP(t *testing.T, expected, actual *BareUDP) {
// set the Port to 6635 (the linux default) if it wasn't specified at creation
if expected.Port == 0 {
expected.Port = 6635
}
if actual.Port != expected.Port {
t.Fatalf("BareUDP.Port doesn't match: %d %d", actual.Port, expected.Port)
}
if actual.EtherType != expected.EtherType {
t.Fatalf("BareUDP.EtherType doesn't match: %x %x", actual.EtherType, expected.EtherType)
}
if actual.SrcPortMin != expected.SrcPortMin {
t.Fatalf("BareUDP.SrcPortMin doesn't match: %d %d", actual.SrcPortMin, expected.SrcPortMin)
}
if actual.MultiProto != expected.MultiProto {
t.Fatal("BareUDP.MultiProto doesn't match")
}
}
func TestLinkAddDelWithIndex(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Dummy{LinkAttrs{Index: 1000, Name: "foo"}})
}
func TestLinkAddDelDummy(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Dummy{LinkAttrs{Name: "foo"}})
}
func TestLinkAddDelDummyWithGroup(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Dummy{LinkAttrs{Name: "foo", Group: 42}})
}
func TestLinkModify(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
linkName := "foo"
originalMTU := 1500
updatedMTU := 1442
link := &Dummy{LinkAttrs{Name: linkName, MTU: originalMTU}}
base := link.Attrs()
if err := LinkAdd(link); err != nil {
t.Fatal(err)
}
link.MTU = updatedMTU
if err := LinkModify(link); err != nil {
t.Fatal(err)
}
result, err := LinkByName(linkName)
if err != nil {
t.Fatal(err)
}
rBase := result.Attrs()
if rBase.MTU != updatedMTU {
t.Fatalf("MTU is %d, should be %d", rBase.MTU, base.MTU)
}
}
func TestLinkAddDelIfb(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Ifb{LinkAttrs{Name: "foo"}})
}
func TestLinkAddDelBridge(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Bridge{LinkAttrs: LinkAttrs{Name: "foo", MTU: 1400}})
}
func TestLinkAddDelGeneve(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Geneve{
LinkAttrs: LinkAttrs{Name: "foo4", EncapType: "geneve"},
ID: 0x1000,
Remote: net.IPv4(127, 0, 0, 1)})
testLinkAddDel(t, &Geneve{
LinkAttrs: LinkAttrs{Name: "foo6", EncapType: "geneve"},
ID: 0x1000,
Remote: net.ParseIP("2001:db8:ef33::2")})
}
func TestLinkAddDelGeneveFlowBased(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Geneve{
LinkAttrs: LinkAttrs{Name: "foo"},
Dport: 1234,
FlowBased: true})
}
func TestGeneveCompareToIP(t *testing.T) {
ns, tearDown := setUpNamedNetlinkTest(t)
defer tearDown()
expected := &Geneve{
ID: 0x764332, // 23 bits
Remote: net.ParseIP("1.2.3.4"),
Dport: 6081,
}
// Create interface
cmd := exec.Command("ip", "netns", "exec", ns,
"ip", "link", "add", "gen0",
"type", "geneve",
"vni", fmt.Sprint(expected.ID),
"remote", expected.Remote.String(),
// TODO: unit tests are currently done on ubuntu 16, and the version of iproute2 there doesn't support dstport
// We can still do most of the testing by verifying that we do read the default port
// "dstport", fmt.Sprint(expected.Dport),
)
out := &bytes.Buffer{}
cmd.Stdout = out
cmd.Stderr = out
if rc := cmd.Run(); rc != nil {
t.Fatal("failed creating link:", rc, out.String())
}
link, err := LinkByName("gen0")
if err != nil {
t.Fatal("Failed getting link: ", err)
}
actual, ok := link.(*Geneve)
if !ok {
t.Fatalf("resulted interface is not geneve: %T", link)
}
compareGeneve(t, expected, actual)
}
func TestLinkAddDelGretap(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Gretap{
LinkAttrs: LinkAttrs{Name: "foo4"},
IKey: 0x101,
OKey: 0x101,
PMtuDisc: 1,
Local: net.IPv4(127, 0, 0, 1),
Remote: net.IPv4(127, 0, 0, 1)})
testLinkAddDel(t, &Gretap{
LinkAttrs: LinkAttrs{Name: "foo6"},
IKey: 0x101,
OKey: 0x101,
Local: net.ParseIP("2001:db8:abcd::1"),
Remote: net.ParseIP("2001:db8:ef33::2")})
}
func TestLinkAddDelGretun(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Gretun{
LinkAttrs: LinkAttrs{Name: "foo4"},
Local: net.IPv4(127, 0, 0, 1),
Remote: net.IPv4(127, 0, 0, 1)})
testLinkAddDel(t, &Gretun{
LinkAttrs: LinkAttrs{Name: "foo6"},
Local: net.ParseIP("2001:db8:abcd::1"),
Remote: net.ParseIP("2001:db8:ef33::2")})
}
func TestLinkAddDelGretunPointToMultiPoint(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Gretun{
LinkAttrs: LinkAttrs{Name: "foo"},
Local: net.IPv4(127, 0, 0, 1),
IKey: 1234,
OKey: 1234})
testLinkAddDel(t, &Gretun{
LinkAttrs: LinkAttrs{Name: "foo6"},
Local: net.ParseIP("2001:db8:1234::4"),
IKey: 5678,
OKey: 7890})
}
func TestLinkAddDelGretunFlowBased(t *testing.T) {
minKernelRequired(t, 4, 3)
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Gretun{
LinkAttrs: LinkAttrs{Name: "foo"},
FlowBased: true})
}
func TestLinkAddDelGretapFlowBased(t *testing.T) {
minKernelRequired(t, 4, 3)
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Gretap{
LinkAttrs: LinkAttrs{Name: "foo"},
FlowBased: true})
}
func TestLinkAddDelVlan(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
parent := &Dummy{LinkAttrs{Name: "foo"}}
if err := LinkAdd(parent); err != nil {
t.Fatal(err)
}
testLinkAddDel(t, &Vlan{LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index}, 900, VLAN_PROTOCOL_8021Q})
if err := LinkDel(parent); err != nil {
t.Fatal(err)
}
}
func TestLinkAddDelMacvlan(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
parent := &Dummy{LinkAttrs{Name: "foo"}}
if err := LinkAdd(parent); err != nil {
t.Fatal(err)
}
testLinkAddDel(t, &Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_PRIVATE,
})
testLinkAddDel(t, &Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_BRIDGE,
})
testLinkAddDel(t, &Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_VEPA,
})
if err := LinkDel(parent); err != nil {
t.Fatal(err)
}
}
func TestLinkAddDelMacvtap(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
parent := &Dummy{LinkAttrs{Name: "foo"}}
if err := LinkAdd(parent); err != nil {
t.Fatal(err)
}
testLinkAddDel(t, &Macvtap{
Macvlan: Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_PRIVATE,
},
})
testLinkAddDel(t, &Macvtap{
Macvlan: Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_BRIDGE,
},
})
testLinkAddDel(t, &Macvtap{
Macvlan: Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_VEPA,
},
})
if err := LinkDel(parent); err != nil {
t.Fatal(err)
}
}
func TestLinkMacvBCQueueLen(t *testing.T) {
minKernelRequired(t, 5, 11)
tearDown := setUpNetlinkTest(t)
defer tearDown()
parent := &Dummy{LinkAttrs{Name: "foo"}}
if err := LinkAdd(parent); err != nil {
t.Fatal(err)
}
testLinkAddDel(t, &Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_PRIVATE,
BCQueueLen: 10000,
})
testLinkAddDel(t, &Macvtap{
Macvlan: Macvlan{
LinkAttrs: LinkAttrs{Name: "bar", ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_PRIVATE,
BCQueueLen: 10000,
},
})
if err := LinkDel(parent); err != nil {
t.Fatal(err)
}
}
func TestNetkitPeerNs(t *testing.T) {
minKernelRequired(t, 6, 7)
tearDown := setUpNetlinkTest(t)
defer tearDown()
basens, err := netns.Get()
if err != nil {
t.Fatal("Failed to get basens")
}
defer basens.Close()
nsOne, err := netns.New()
if err != nil {
t.Fatal("Failed to create nsOne")
}
defer nsOne.Close()
nsTwo, err := netns.New()
if err != nil {
t.Fatal("Failed to create nsTwo")
}
defer nsTwo.Close()
netkit := &Netkit{
LinkAttrs: LinkAttrs{
Name: "foo",
Namespace: NsFd(basens),
},
Mode: NETKIT_MODE_L2,
Policy: NETKIT_POLICY_FORWARD,
PeerPolicy: NETKIT_POLICY_BLACKHOLE,
}
peerAttr := &LinkAttrs{
Name: "bar",
Namespace: NsFd(nsOne),
}
netkit.SetPeerAttrs(peerAttr)
if err := LinkAdd(netkit); err != nil {
t.Fatal(err)
}
_, err = LinkByName("bar")
if err == nil {
t.Fatal("netkit link bar is in nsTwo")
}
_, err = LinkByName("foo")
if err == nil {
t.Fatal("netkit link foo is in nsTwo")
}
err = netns.Set(basens)
if err != nil {
t.Fatal("Failed to set basens")
}
_, err = LinkByName("foo")
if err != nil {
t.Fatal("netkit link foo is not in basens")
}
err = netns.Set(nsOne)
if err != nil {
t.Fatal("Failed to set nsOne")
}
_, err = LinkByName("bar")
if err != nil {
t.Fatal("netkit link bar is not in nsOne")
}
}
func TestLinkAddDelNetkit(t *testing.T) {
minKernelRequired(t, 6, 7)
tearDown := setUpNetlinkTest(t)
defer tearDown()
netkit := &Netkit{
LinkAttrs: LinkAttrs{
Name: "foo",
},
Mode: NETKIT_MODE_L2,
Policy: NETKIT_POLICY_FORWARD,
PeerPolicy: NETKIT_POLICY_BLACKHOLE,
Scrub: NETKIT_SCRUB_DEFAULT,
PeerScrub: NETKIT_SCRUB_NONE,
}
peerAttr := &LinkAttrs{
Name: "bar",
}
netkit.SetPeerAttrs(peerAttr)
testLinkAddDel(t, netkit)
}
func TestLinkAddDelVeth(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
peerMAC, _ := net.ParseMAC("00:12:34:56:78:02")
veth := &Veth{
LinkAttrs: LinkAttrs{
Name: "foo",
TxQLen: testTxQLen,
MTU: 1400,
NumTxQueues: testTxQueues,
NumRxQueues: testRxQueues,
},
PeerName: "bar",
PeerHardwareAddr: peerMAC,
}
testLinkAddDel(t, veth)
}
func TestLinkAddDelBond(t *testing.T) {
minKernelRequired(t, 3, 13)
tearDown := setUpNetlinkTest(t)
defer tearDown()
modes := []string{"802.3ad", "balance-tlb"}
for _, mode := range modes {
bond := NewLinkBond(LinkAttrs{Name: "foo"})
bond.Mode = StringToBondModeMap[mode]
switch mode {
case "802.3ad":
bond.AdSelect = BondAdSelect(BOND_AD_SELECT_BANDWIDTH)
bond.AdActorSysPrio = 1
bond.AdUserPortKey = 1
bond.AdActorSystem, _ = net.ParseMAC("06:aa:bb:cc:dd:ee")
bond.ArpIpTargets = []net.IP{net.ParseIP("1.1.1.1"), net.ParseIP("1.1.1.2")}
case "balance-tlb":
bond.TlbDynamicLb = 1
bond.ArpIpTargets = []net.IP{net.ParseIP("1.1.1.2"), net.ParseIP("1.1.1.1")}
}
testLinkAddDel(t, bond)
}
}
func TestLinkAddVethWithDefaultTxQLen(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
la := NewLinkAttrs()
la.Name = "foo"
veth := &Veth{LinkAttrs: la, PeerName: "bar"}
if err := LinkAdd(veth); err != nil {
t.Fatal(err)
}
link, err := LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if veth, ok := link.(*Veth); !ok {
t.Fatalf("unexpected link type: %T", link)
} else {
if veth.TxQLen != defaultTxQLen {
t.Fatalf("TxQLen is %d, should be %d", veth.TxQLen, defaultTxQLen)
}
}
peer, err := LinkByName("bar")
if err != nil {
t.Fatal(err)
}
if veth, ok := peer.(*Veth); !ok {
t.Fatalf("unexpected link type: %T", link)
} else {
if veth.TxQLen != defaultTxQLen {
t.Fatalf("TxQLen is %d, should be %d", veth.TxQLen, defaultTxQLen)
}
}
}
func TestLinkAddVethWithZeroTxQLen(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
la := NewLinkAttrs()
la.Name = "foo"
la.TxQLen = 0
veth := &Veth{LinkAttrs: la, PeerName: "bar"}
if err := LinkAdd(veth); err != nil {
t.Fatal(err)
}
link, err := LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if veth, ok := link.(*Veth); !ok {
t.Fatalf("unexpected link type: %T", link)
} else {
if veth.TxQLen != 0 {
t.Fatalf("TxQLen is %d, should be %d", veth.TxQLen, 0)
}
}
peer, err := LinkByName("bar")
if err != nil {
t.Fatal(err)
}
if veth, ok := peer.(*Veth); !ok {
t.Fatalf("unexpected link type: %T", link)
} else {
if veth.TxQLen != 0 {
t.Fatalf("TxQLen is %d, should be %d", veth.TxQLen, 0)
}
}
}
func TestLinkAddDelDummyWithGSO(t *testing.T) {
const (
gsoMaxSegs = 16
gsoMaxSize = 1 << 14
)
minKernelRequired(t, 4, 16)
tearDown := setUpNetlinkTest(t)
defer tearDown()
dummy := &Dummy{LinkAttrs: LinkAttrs{Name: "foo", GSOMaxSize: gsoMaxSize, GSOMaxSegs: gsoMaxSegs}}
if err := LinkAdd(dummy); err != nil {
t.Fatal(err)
}
link, err := LinkByName("foo")
if err != nil {
t.Fatal(err)
}
dummy, ok := link.(*Dummy)
if !ok {
t.Fatalf("unexpected link type: %T", link)
}
if dummy.GSOMaxSize != gsoMaxSize {
t.Fatalf("GSOMaxSize is %d, should be %d", dummy.GSOMaxSize, gsoMaxSize)
}
if dummy.GSOMaxSegs != gsoMaxSegs {
t.Fatalf("GSOMaxSeg is %d, should be %d", dummy.GSOMaxSegs, gsoMaxSegs)
}
}
func TestLinkAddDelDummyWithGRO(t *testing.T) {
const (
groMaxSize = 1 << 14
)
minKernelRequired(t, 5, 19)
tearDown := setUpNetlinkTest(t)
defer tearDown()
dummy := &Dummy{LinkAttrs: LinkAttrs{Name: "foo", GROMaxSize: groMaxSize}}
if err := LinkAdd(dummy); err != nil {
t.Fatal(err)
}
link, err := LinkByName("foo")
if err != nil {
t.Fatal(err)
}
dummy, ok := link.(*Dummy)
if !ok {
t.Fatalf("unexpected link type: %T", link)
}
if dummy.GROMaxSize != groMaxSize {
t.Fatalf("GROMaxSize is %d, should be %d", dummy.GROMaxSize, groMaxSize)
}
}
func TestLinkAddDummyWithTxQLen(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
la := NewLinkAttrs()
la.Name = "foo"
la.TxQLen = 1500
dummy := &Dummy{LinkAttrs: la}
if err := LinkAdd(dummy); err != nil {
t.Fatal(err)
}
link, err := LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if dummy, ok := link.(*Dummy); !ok {
t.Fatalf("unexpected link type: %T", link)
} else {
if dummy.TxQLen != 1500 {
t.Fatalf("TxQLen is %d, should be %d", dummy.TxQLen, 1500)
}
}
}
func TestLinkAddDelBridgeMaster(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
master := &Bridge{LinkAttrs: LinkAttrs{Name: "foo"}}
if err := LinkAdd(master); err != nil {
t.Fatal(err)
}
testLinkAddDel(t, &Dummy{LinkAttrs{Name: "bar", MasterIndex: master.Attrs().Index}})
if err := LinkDel(master); err != nil {
t.Fatal(err)
}
}
func testLinkSetUnsetResetMaster(t *testing.T, master, newmaster Link) {
slave := &Dummy{LinkAttrs{Name: "baz"}}
if err := LinkAdd(slave); err != nil {
t.Fatal(err)
}
nonexistsmaster := &Bridge{LinkAttrs: LinkAttrs{Name: "foobar"}}
if err := LinkSetMaster(slave, nonexistsmaster); err == nil {
t.Fatal("error expected")
}
if err := LinkSetMaster(slave, master); err != nil {
t.Fatal(err)
}
link, err := LinkByName("baz")
if err != nil {
t.Fatal(err)
}
if link.Attrs().MasterIndex != master.Attrs().Index {
t.Fatal("Master not set properly")
}
if err := LinkSetMaster(slave, newmaster); err != nil {
t.Fatal(err)
}
link, err = LinkByName("baz")
if err != nil {
t.Fatal(err)
}
if link.Attrs().MasterIndex != newmaster.Attrs().Index {
t.Fatal("Master not reset properly")
}
if err := LinkSetNoMaster(slave); err != nil {
t.Fatal(err)
}
link, err = LinkByName("baz")
if err != nil {
t.Fatal(err)
}
if link.Attrs().MasterIndex != 0 {
t.Fatal("Master not unset properly")
}
if err := LinkDel(slave); err != nil {
t.Fatal(err)
}
}
func TestLinkSetUnsetResetMaster(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
master := &Bridge{LinkAttrs: LinkAttrs{Name: "foo"}}
if err := LinkAdd(master); err != nil {
t.Fatal(err)
}
newmaster := &Bridge{LinkAttrs: LinkAttrs{Name: "bar"}}
if err := LinkAdd(newmaster); err != nil {
t.Fatal(err)
}
testLinkSetUnsetResetMaster(t, master, newmaster)
if err := LinkDel(newmaster); err != nil {
t.Fatal(err)
}
if err := LinkDel(master); err != nil {
t.Fatal(err)
}
}
func TestLinkSetUnsetResetMasterBond(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
master := NewLinkBond(LinkAttrs{Name: "foo"})
master.Mode = BOND_MODE_BALANCE_RR
if err := LinkAdd(master); err != nil {
t.Fatal(err)
}
newmaster := NewLinkBond(LinkAttrs{Name: "bar"})
newmaster.Mode = BOND_MODE_BALANCE_RR
if err := LinkAdd(newmaster); err != nil {
t.Fatal(err)
}
testLinkSetUnsetResetMaster(t, master, newmaster)
if err := LinkDel(newmaster); err != nil {
t.Fatal(err)
}
if err := LinkDel(master); err != nil {
t.Fatal(err)
}
}
func TestLinkSetNs(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
basens, err := netns.Get()
if err != nil {
t.Fatal("Failed to get basens")
}
defer basens.Close()
newns, err := netns.New()
if err != nil {
t.Fatal("Failed to create newns")
}
defer newns.Close()
link := &Veth{LinkAttrs{Name: "foo"}, "bar", nil, nil}
if err := LinkAdd(link); err != nil {
t.Fatal(err)
}
peer, err := LinkByName("bar")
if err != nil {
t.Fatal(err)
}
LinkSetNsFd(peer, int(basens))
if err != nil {
t.Fatal("Failed to set newns for link")
}
_, err = LinkByName("bar")
if err == nil {
t.Fatal("Link bar is still in newns")
}
err = netns.Set(basens)
if err != nil {
t.Fatal("Failed to set basens")
}
peer, err = LinkByName("bar")
if err != nil {
t.Fatal("Link is not in basens")
}
if err := LinkDel(peer); err != nil {
t.Fatal(err)
}
err = netns.Set(newns)
if err != nil {
t.Fatal("Failed to set newns")
}
_, err = LinkByName("foo")
if err == nil {
t.Fatal("Other half of veth pair not deleted")
}
}
func TestLinkAddDelWireguard(t *testing.T) {
minKernelRequired(t, 5, 6)
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Wireguard{LinkAttrs: LinkAttrs{Name: "wg0"}})
}
func TestVethPeerNs(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
basens, err := netns.Get()
if err != nil {
t.Fatal("Failed to get basens")
}
defer basens.Close()
newns, err := netns.New()
if err != nil {
t.Fatal("Failed to create newns")
}
defer newns.Close()
link := &Veth{LinkAttrs{Name: "foo"}, "bar", nil, NsFd(basens)}
if err := LinkAdd(link); err != nil {
t.Fatal(err)
}
_, err = LinkByName("bar")
if err == nil {
t.Fatal("Link bar is in newns")
}
err = netns.Set(basens)
if err != nil {
t.Fatal("Failed to set basens")
}
_, err = LinkByName("bar")
if err != nil {
t.Fatal("Link bar is not in basens")
}
err = netns.Set(newns)
if err != nil {
t.Fatal("Failed to set newns")
}
_, err = LinkByName("foo")
if err != nil {
t.Fatal("Link foo is not in newns")
}
}
func TestVethPeerNs2(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
basens, err := netns.Get()
if err != nil {
t.Fatal("Failed to get basens")
}
defer basens.Close()
onens, err := netns.New()
if err != nil {
t.Fatal("Failed to create newns")
}
defer onens.Close()
twons, err := netns.New()
if err != nil {
t.Fatal("Failed to create twons")
}
defer twons.Close()
link := &Veth{LinkAttrs{Name: "foo", Namespace: NsFd(onens)}, "bar", nil, NsFd(basens)}
if err := LinkAdd(link); err != nil {
t.Fatal(err)
}
_, err = LinkByName("foo")
if err == nil {
t.Fatal("Link foo is in twons")
}
_, err = LinkByName("bar")
if err == nil {
t.Fatal("Link bar is in twons")
}
err = netns.Set(basens)
if err != nil {
t.Fatal("Failed to set basens")
}
_, err = LinkByName("bar")
if err != nil {
t.Fatal("Link bar is not in basens")
}
err = netns.Set(onens)
if err != nil {
t.Fatal("Failed to set onens")
}
_, err = LinkByName("foo")
if err != nil {
t.Fatal("Link foo is not in onens")
}
}
func TestLinkAddDelVxlan(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
parent := &Dummy{
LinkAttrs{Name: "foo"},
}
if err := LinkAdd(parent); err != nil {
t.Fatal(err)
}
vxlan := Vxlan{
LinkAttrs: LinkAttrs{
Name: "bar",
},
VxlanId: 10,
VtepDevIndex: parent.Index,
Learning: true,
L2miss: true,
L3miss: true,
}
testLinkAddDel(t, &vxlan)
if err := LinkDel(parent); err != nil {
t.Fatal(err)
}
}
func TestLinkAddDelVxlanUdpCSum6(t *testing.T) {
minKernelRequired(t, 3, 16)
tearDown := setUpNetlinkTest(t)
defer tearDown()
parent := &Dummy{
LinkAttrs{Name: "foo"},
}
if err := LinkAdd(parent); err != nil {
t.Fatal(err)
}
vxlan := Vxlan{
LinkAttrs: LinkAttrs{
Name: "bar",
},
VxlanId: 10,
VtepDevIndex: parent.Index,
Learning: true,
L2miss: true,
L3miss: true,
UDP6ZeroCSumTx: true,
UDP6ZeroCSumRx: true,
}
testLinkAddDel(t, &vxlan)
if err := LinkDel(parent); err != nil {
t.Fatal(err)
}
}
func TestLinkAddDelVxlanGbp(t *testing.T) {
minKernelRequired(t, 4, 0)
tearDown := setUpNetlinkTest(t)
defer tearDown()
parent := &Dummy{
LinkAttrs{Name: "foo"},
}
if err := LinkAdd(parent); err != nil {
t.Fatal(err)
}
vxlan := Vxlan{
LinkAttrs: LinkAttrs{
Name: "bar",
},
VxlanId: 10,
VtepDevIndex: parent.Index,
Learning: true,
L2miss: true,
L3miss: true,
UDP6ZeroCSumTx: true,
UDP6ZeroCSumRx: true,
GBP: true,
}
testLinkAddDel(t, &vxlan)
if err := LinkDel(parent); err != nil {
t.Fatal(err)
}
}
func TestLinkAddDelVxlanFlowBased(t *testing.T) {
minKernelRequired(t, 4, 3)
tearDown := setUpNetlinkTest(t)
defer tearDown()
vxlan := Vxlan{
LinkAttrs: LinkAttrs{
Name: "foo",
},
Learning: false,
FlowBased: true,
}
testLinkAddDel(t, &vxlan)
}
func TestLinkAddDelBareUDP(t *testing.T) {
minKernelRequired(t, 5, 1)
setUpNetlinkTestWithKModule(t, "bareudp")
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &BareUDP{
LinkAttrs: LinkAttrs{Name: "foo99"},
Port: 6635,
EtherType: syscall.ETH_P_MPLS_UC,
SrcPortMin: 12345,
MultiProto: true,
})
testLinkAddDel(t, &BareUDP{
LinkAttrs: LinkAttrs{Name: "foo100"},
Port: 6635,
EtherType: syscall.ETH_P_IP,
SrcPortMin: 12345,
MultiProto: true,
})
}
func TestBareUDPCompareToIP(t *testing.T) {
if os.Getenv("CI") == "true" {
t.Skipf("Fails in CI due to old iproute2")
}
// requires iproute2 >= 5.10
minKernelRequired(t, 5, 9)
setUpNetlinkTestWithKModule(t, "bareudp")
ns, tearDown := setUpNamedNetlinkTest(t)
defer tearDown()
expected := &BareUDP{
Port: uint16(6635),
EtherType: syscall.ETH_P_MPLS_UC,
SrcPortMin: 12345,
MultiProto: true,
}
// Create interface
cmd := exec.Command("ip", "netns", "exec", ns,
"ip", "link", "add", "b0",
"type", "bareudp",
"dstport", fmt.Sprint(expected.Port),
"ethertype", "mpls_uc",
"srcportmin", fmt.Sprint(expected.SrcPortMin),
"multiproto",
)
out := &bytes.Buffer{}
cmd.Stdout = out
cmd.Stderr = out
if rc := cmd.Run(); rc != nil {
t.Fatal("failed creating link:", rc, out.String())
}
link, err := LinkByName("b0")
if err != nil {
t.Fatal("Failed getting link: ", err)
}
actual, ok := link.(*BareUDP)
if !ok {
t.Fatalf("resulted interface is not BareUDP: %T", link)
}
compareBareUDP(t, expected, actual)
}
func TestLinkAddDelIPVlanL2(t *testing.T) {
minKernelRequired(t, 4, 2)
tearDown := setUpNetlinkTest(t)
defer tearDown()
parent := &Dummy{LinkAttrs{Name: "foo"}}
if err := LinkAdd(parent); err != nil {
t.Fatal(err)
}
ipv := IPVlan{
LinkAttrs: LinkAttrs{
Name: "bar",
ParentIndex: parent.Index,
},
Mode: IPVLAN_MODE_L2,
}
testLinkAddDel(t, &ipv)
}
func TestLinkAddDelIPVlanL3(t *testing.T) {
minKernelRequired(t, 4, 2)
tearDown := setUpNetlinkTest(t)
defer tearDown()
parent := &Dummy{LinkAttrs{Name: "foo"}}
if err := LinkAdd(parent); err != nil {
t.Fatal(err)
}
ipv := IPVlan{
LinkAttrs: LinkAttrs{
Name: "bar",
ParentIndex: parent.Index,
},
Mode: IPVLAN_MODE_L3,
}
testLinkAddDel(t, &ipv)
}
func TestLinkAddDelIPVlanVepa(t *testing.T) {
minKernelRequired(t, 4, 15)
tearDown := setUpNetlinkTest(t)
defer tearDown()
parent := &Dummy{LinkAttrs{Name: "foo"}}
if err := LinkAdd(parent); err != nil {
t.Fatal(err)
}
ipv := IPVlan{
LinkAttrs: LinkAttrs{
Name: "bar",
ParentIndex: parent.Index,
},
Mode: IPVLAN_MODE_L3,
Flag: IPVLAN_FLAG_VEPA,
}
testLinkAddDel(t, &ipv)
}
func TestLinkAddDelIPVlanNoParent(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
ipv := IPVlan{
LinkAttrs: LinkAttrs{
Name: "bar",
},
Mode: IPVLAN_MODE_L3,
}
err := LinkAdd(&ipv)
if err == nil {
t.Fatal("Add should fail if ipvlan creating without ParentIndex")
}
if err.Error() != "Can't create ipvlan link without ParentIndex" {
t.Fatalf("Error should be about missing ParentIndex, got %q", err)
}
}
func TestLinkByIndex(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
dummy := &Dummy{LinkAttrs{Name: "dummy"}}
if err := LinkAdd(dummy); err != nil {
t.Fatal(err)
}
found, err := LinkByIndex(dummy.Index)
if err != nil {
t.Fatal(err)
}
if found.Attrs().Index != dummy.Attrs().Index {
t.Fatalf("Indices don't match: %v != %v", found.Attrs().Index, dummy.Attrs().Index)
}
LinkDel(dummy)
// test not found
_, err = LinkByIndex(dummy.Attrs().Index)
if err == nil {
t.Fatalf("LinkByIndex(%v) found deleted link", err)
}
}
func TestLinkSet(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
iface := &Dummy{LinkAttrs{Name: "foo"}}
if err := LinkAdd(iface); err != nil {
t.Fatal(err)
}
link, err := LinkByName("foo")
if err != nil {
t.Fatal(err)
}
err = LinkSetName(link, "bar")
if err != nil {
t.Fatalf("Could not change interface name: %v", err)
}
link, err = LinkByName("bar")
if err != nil {
t.Fatalf("Interface name not changed: %v", err)
}
err = LinkSetMTU(link, 1400)
if err != nil {
t.Fatalf("Could not set MTU: %v", err)
}
link, err = LinkByName("bar")
if err != nil {
t.Fatal(err)
}
if link.Attrs().MTU != 1400 {
t.Fatal("MTU not changed")
}
err = LinkSetTxQLen(link, 500)
if err != nil {
t.Fatalf("Could not set txqlen: %v", err)
}
link, err = LinkByName("bar")
if err != nil {
t.Fatal(err)
}
if link.Attrs().TxQLen != 500 {
t.Fatal("txqlen not changed")
}
addr, err := net.ParseMAC("00:12:34:56:78:AB")
if err != nil {
t.Fatal(err)
}
err = LinkSetHardwareAddr(link, addr)
if err != nil {
t.Fatal(err)
}
link, err = LinkByName("bar")
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(link.Attrs().HardwareAddr, addr) {
t.Fatalf("hardware address not changed")
}
err = LinkSetAlias(link, "barAlias")
if err != nil {
t.Fatalf("Could not set alias: %v", err)
}
link, err = LinkByName("bar")
if err != nil {
t.Fatal(err)
}
if link.Attrs().Alias != "barAlias" {
t.Fatalf("alias not changed")
}
link, err = LinkByAlias("barAlias")
if err != nil {
t.Fatal(err)
}
err = LinkSetGroup(link, 42)
if err != nil {
t.Fatalf("Could not set group: %v", err)
}
link, err = LinkByName("bar")
if err != nil {
t.Fatal(err)
}
if link.Attrs().Group != 42 {
t.Fatal("Link group not changed")
}
}
func TestLinkAltName(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
iface := &Dummy{LinkAttrs{Name: "bar"}}
if err := LinkAdd(iface); err != nil {
t.Fatal(err)
}
link, err := LinkByName("bar")
if err != nil {
t.Fatal(err)
}
altNames := []string{"altname", "altname2", "some_longer_altname"}
sort.Strings(altNames)
altNamesStr := strings.Join(altNames, ",")
for _, altname := range altNames {
err = LinkAddAltName(link, altname)
if err != nil {
t.Fatalf("Could not add %s: %v", altname, err)
}
}
link, err = LinkByName("bar")
if err != nil {
t.Fatal(err)
}
sort.Strings(link.Attrs().AltNames)
linkAltNamesStr := strings.Join(link.Attrs().AltNames, ",")
if altNamesStr != linkAltNamesStr {
t.Fatalf("Expected %s AltNames, got %s", altNamesStr, linkAltNamesStr)
}
for _, altname := range altNames {
link, err = LinkByName(altname)
if err != nil {
t.Fatal(err)
}
}
for idx, altName := range altNames {
err = LinkDelAltName(link, altName)
if err != nil {
t.Fatalf("Could not delete %s: %v", altName, err)
}
link, err = LinkByName("bar")
if err != nil {
t.Fatal(err)
}
sort.Strings(link.Attrs().AltNames)
linkAltNamesStr := strings.Join(link.Attrs().AltNames, ",")
altNamesStr := strings.Join(altNames[idx+1:], ",")
if linkAltNamesStr != altNamesStr {
t.Fatalf("Expected %s AltNames, got %s", altNamesStr, linkAltNamesStr)
}
}
}
func TestLinkSetARP(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
iface := &Veth{LinkAttrs: LinkAttrs{Name: "foo", TxQLen: testTxQLen, MTU: 1500}, PeerName: "banana"}
if err := LinkAdd(iface); err != nil {
t.Fatal(err)
}
link, err := LinkByName("foo")
if err != nil {
t.Fatal(err)
}
err = LinkSetARPOff(link)
if err != nil {
t.Fatal(err)
}
link, err = LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if link.Attrs().RawFlags&unix.IFF_NOARP != uint32(unix.IFF_NOARP) {
t.Fatalf("NOARP was not set")
}
err = LinkSetARPOn(link)
if err != nil {
t.Fatal(err)
}
link, err = LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if link.Attrs().RawFlags&unix.IFF_NOARP != 0 {
t.Fatalf("NOARP is still set")
}
}
func expectLinkUpdate(ch <-chan LinkUpdate, ifaceName string, up bool) bool {
for {
timeout := time.After(time.Minute)
select {
case update := <-ch:
if ifaceName == update.Link.Attrs().Name && (update.IfInfomsg.Flags&unix.IFF_UP != 0) == up {
return true
}
case <-timeout:
return false
}
}
}
func TestLinkSubscribe(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
ch := make(chan LinkUpdate)
done := make(chan struct{})
defer close(done)
if err := LinkSubscribe(ch, done); err != nil {
t.Fatal(err)
}
link := &Veth{LinkAttrs{Name: "foo", TxQLen: testTxQLen, MTU: 1400}, "bar", nil, nil}
if err := LinkAdd(link); err != nil {
t.Fatal(err)
}
if !expectLinkUpdate(ch, "foo", false) {
t.Fatal("Add update not received as expected")
}
if err := LinkSetUp(link); err != nil {
t.Fatal(err)
}
if !expectLinkUpdate(ch, "foo", true) {
t.Fatal("Link Up update not received as expected")
}
if err := LinkDel(link); err != nil {
t.Fatal(err)
}
if !expectLinkUpdate(ch, "foo", false) {
t.Fatal("Del update not received as expected")
}
}
func TestLinkSubscribeWithOptions(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
ch := make(chan LinkUpdate)
done := make(chan struct{})
defer close(done)
var lastError error
defer func() {
if lastError != nil {
t.Fatalf("Fatal error received during subscription: %v", lastError)
}
}()
if err := LinkSubscribeWithOptions(ch, done, LinkSubscribeOptions{
ErrorCallback: func(err error) {
lastError = err
},
}); err != nil {
t.Fatal(err)
}
link := &Veth{LinkAttrs{Name: "foo", TxQLen: testTxQLen, MTU: 1400}, "bar", nil, nil}
if err := LinkAdd(link); err != nil {
t.Fatal(err)
}
if !expectLinkUpdate(ch, "foo", false) {
t.Fatal("Add update not received as expected")
}
}
func TestLinkSubscribeAt(t *testing.T) {
skipUnlessRoot(t)
// 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.Close()
// Subscribe for Link events on the custom netns
ch := make(chan LinkUpdate)
done := make(chan struct{})
defer close(done)
if err := LinkSubscribeAt(newNs, ch, done); err != nil {
t.Fatal(err)
}
link := &Veth{LinkAttrs{Name: "test", TxQLen: testTxQLen, MTU: 1400}, "bar", nil, nil}
if err := nh.LinkAdd(link); err != nil {
t.Fatal(err)
}
if !expectLinkUpdate(ch, "test", false) {
t.Fatal("Add update not received as expected")
}
if err := nh.LinkSetUp(link); err != nil {
t.Fatal(err)
}
if !expectLinkUpdate(ch, "test", true) {
t.Fatal("Link Up update not received as expected")
}
if err := nh.LinkDel(link); err != nil {
t.Fatal(err)
}
if !expectLinkUpdate(ch, "test", false) {
t.Fatal("Del update not received as expected")
}
}
func TestLinkSubscribeListExisting(t *testing.T) {
skipUnlessRoot(t)
// 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.Close()
link := &Veth{LinkAttrs{Name: "test", TxQLen: testTxQLen, MTU: 1400}, "bar", nil, nil}
if err := nh.LinkAdd(link); err != nil {
t.Fatal(err)
}
// Subscribe for Link events on the custom netns
ch := make(chan LinkUpdate)
done := make(chan struct{})
defer close(done)
if err := LinkSubscribeWithOptions(ch, done, LinkSubscribeOptions{
Namespace: &newNs,
ListExisting: true},
); err != nil {
t.Fatal(err)
}
if !expectLinkUpdate(ch, "test", false) {
t.Fatal("Add update not received as expected")
}
if err := nh.LinkSetUp(link); err != nil {
t.Fatal(err)
}
if !expectLinkUpdate(ch, "test", true) {
t.Fatal("Link Up update not received as expected")
}
if err := nh.LinkDel(link); err != nil {
t.Fatal(err)
}
if !expectLinkUpdate(ch, "test", false) {
t.Fatal("Del update not received as expected")
}
}
func TestLinkStats(t *testing.T) {
defer setUpNetlinkTest(t)()
// Create a veth pair and verify the cross-stats once both
// ends are brought up and some ICMPv6 packets are exchanged
v0 := "v0"
v1 := "v1"
vethLink := &Veth{LinkAttrs: LinkAttrs{Name: v0}, PeerName: v1}
if err := LinkAdd(vethLink); err != nil {
t.Fatal(err)
}
veth0, err := LinkByName(v0)
if err != nil {
t.Fatal(err)
}
if err := LinkSetUp(veth0); err != nil {
t.Fatal(err)
}
veth1, err := LinkByName(v1)
if err != nil {
t.Fatal(err)
}
if err := LinkSetUp(veth1); err != nil {
t.Fatal(err)
}
time.Sleep(2 * time.Second)
// verify statistics
veth0, err = LinkByName(v0)
if err != nil {
t.Fatal(err)
}
veth1, err = LinkByName(v1)
if err != nil {
t.Fatal(err)
}
v0Stats := veth0.Attrs().Statistics
v1Stats := veth1.Attrs().Statistics
if v0Stats.RxPackets != v1Stats.TxPackets || v0Stats.TxPackets != v1Stats.RxPackets ||
v0Stats.RxBytes != v1Stats.TxBytes || v0Stats.TxBytes != v1Stats.RxBytes {
t.Fatalf("veth ends counters differ:\n%v\n%v", v0Stats, v1Stats)
}
}
func TestLinkXdp(t *testing.T) {
links, err := LinkList()
if err != nil {
t.Fatal(err)
}
var testXdpLink Link
for _, link := range links {
if link.Attrs().Xdp != nil && !link.Attrs().Xdp.Attached {
testXdpLink = link
break
}
}
if testXdpLink == nil {
t.Skipf("No link supporting XDP found")
}
fd, err := loadSimpleBpf(BPF_PROG_TYPE_XDP, 2 /*XDP_PASS*/)
if err != nil {
t.Skipf("Loading bpf program failed: %s", err)
}
if err := LinkSetXdpFd(testXdpLink, fd); err != nil {
t.Fatal(err)
}
if err := LinkSetXdpFdWithFlags(testXdpLink, fd, nl.XDP_FLAGS_UPDATE_IF_NOEXIST); !errors.Is(err, unix.EBUSY) {
t.Fatal(err)
}
if err := LinkSetXdpFd(testXdpLink, -1); err != nil {
t.Fatal(err)
}
}
func TestLinkAddDelIptun(t *testing.T) {
minKernelRequired(t, 4, 9)
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Iptun{
LinkAttrs: LinkAttrs{Name: "iptunfoo"},
PMtuDisc: 1,
Local: net.IPv4(127, 0, 0, 1),
Remote: net.IPv4(127, 0, 0, 1)})
}
func TestLinkAddDelIptunFlowBased(t *testing.T) {
minKernelRequired(t, 4, 9)
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Iptun{
LinkAttrs: LinkAttrs{Name: "iptunflowfoo"},
FlowBased: true,
})
}
func TestLinkAddDelIp6tnl(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Ip6tnl{
LinkAttrs: LinkAttrs{Name: "ip6tnltest"},
Local: net.ParseIP("2001:db8::100"),
Remote: net.ParseIP("2001:db8::200"),
})
}
func TestLinkAddDelIp6tnlFlowbased(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Ip6tnl{
LinkAttrs: LinkAttrs{Name: "ip6tnltest"},
FlowBased: true,
})
}
func TestLinkAddDelSittun(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Sittun{
LinkAttrs: LinkAttrs{Name: "sittunfoo"},
PMtuDisc: 1,
Local: net.IPv4(127, 0, 0, 1),
Remote: net.IPv4(127, 0, 0, 1)})
}
func TestLinkAddDelVti(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
testLinkAddDel(t, &Vti{
LinkAttrs: LinkAttrs{Name: "vtifoo"},
IKey: 0x101,
OKey: 0x101,
Local: net.IPv4(127, 0, 0, 1),
Remote: net.IPv4(127, 0, 0, 1)})
testLinkAddDel(t, &Vti{
LinkAttrs: LinkAttrs{Name: "vtibar"},
IKey: 0x101,
OKey: 0x101,
Local: net.IPv6loopback,
Remote: net.IPv6loopback})
}
func TestLinkSetGSOMaxSize(t *testing.T) {
minKernelRequired(t, 5, 19)
tearDown := setUpNetlinkTest(t)
defer tearDown()
iface := &Veth{LinkAttrs: LinkAttrs{Name: "foo", TxQLen: testTxQLen, MTU: 1500}, PeerName: "bar"}
if err := LinkAdd(iface); err != nil {
t.Fatal(err)
}
link, err := LinkByName("foo")
if err != nil {
t.Fatal(err)
}
err = LinkSetGSOMaxSize(link, 32768)
if err != nil {
t.Fatal(err)
}
link, err = LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if link.Attrs().GSOMaxSize != 32768 {
t.Fatalf("GSO max size was not modified")
}
}
func TestLinkSetGSOMaxSegs(t *testing.T) {
minKernelRequired(t, 5, 19)
tearDown := setUpNetlinkTest(t)
defer tearDown()
iface := &Veth{LinkAttrs: LinkAttrs{Name: "foo", TxQLen: testTxQLen, MTU: 1500}, PeerName: "bar"}
if err := LinkAdd(iface); err != nil {
t.Fatal(err)
}
link, err := LinkByName("foo")
if err != nil {
t.Fatal(err)
}
err = LinkSetGSOMaxSegs(link, 16)
if err != nil {
t.Fatal(err)
}
link, err = LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if link.Attrs().GSOMaxSegs != 16 {
t.Fatalf("GSO max segments was not modified")
}
}
func TestLinkSetGROMaxSize(t *testing.T) {
minKernelRequired(t, 5, 19)
tearDown := setUpNetlinkTest(t)
defer tearDown()
iface := &Veth{LinkAttrs: LinkAttrs{Name: "foo", TxQLen: testTxQLen, MTU: 1500}, PeerName: "bar"}
if err := LinkAdd(iface); err != nil {
t.Fatal(err)
}
link, err := LinkByName("foo")
if err != nil {
t.Fatal(err)
}
err = LinkSetGROMaxSize(link, 32768)
if err != nil {
t.Fatal(err)
}
link, err = LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if link.Attrs().GROMaxSize != 32768 {
t.Fatalf("GRO max size was not modified")
}
}
func TestLinkGetTSOMax(t *testing.T) {
minKernelRequired(t, 5, 19)
tearDown := setUpNetlinkTest(t)
defer tearDown()
iface := &Veth{LinkAttrs: LinkAttrs{Name: "foo", TxQLen: testTxQLen, MTU: 1500}, PeerName: "bar"}
if err := LinkAdd(iface); err != nil {
t.Fatal(err)
}
link, err := LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if link.Attrs().TSOMaxSize != 524280 || link.Attrs().TSOMaxSegs != 65535 {
t.Fatalf("TSO max size and segments could not be retrieved")
}
}
func TestLinkSetGSOIPv4MaxSize(t *testing.T) {
minKernelRequired(t, 6, 3)
tearDown := setUpNetlinkTest(t)
defer tearDown()
iface := &Veth{LinkAttrs: LinkAttrs{Name: "foo", TxQLen: testTxQLen, MTU: 1500}, PeerName: "bar"}
if err := LinkAdd(iface); err != nil {
t.Fatal(err)
}
link, err := LinkByName("foo")
if err != nil {
t.Fatal(err)
}
err = LinkSetGSOIPv4MaxSize(link, 32768)
if err != nil {
t.Fatal(err)
}
link, err = LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if link.Attrs().GSOIPv4MaxSize != 32768 {
t.Fatalf("GSO max size was not modified")
}
}
func TestLinkSetGROIPv4MaxSize(t *testing.T) {
minKernelRequired(t, 6, 3)
tearDown := setUpNetlinkTest(t)
defer tearDown()
iface := &Veth{LinkAttrs: LinkAttrs{Name: "foo", TxQLen: testTxQLen, MTU: 1500}, PeerName: "bar"}
if err := LinkAdd(iface); err != nil {
t.Fatal(err)
}
link, err := LinkByName("foo")
if err != nil {
t.Fatal(err)
}
err = LinkSetGROIPv4MaxSize(link, 32768)
if err != nil {
t.Fatal(err)
}
link, err = LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if link.Attrs().GROIPv4MaxSize != 32768 {
t.Fatalf("GRO max size was not modified")
}
}
func TestBridgeCreationWithMulticastSnooping(t *testing.T) {
minKernelRequired(t, 4, 4)
tearDown := setUpNetlinkTest(t)
defer tearDown()
bridgeWithDefaultMcastSnoopName := "foo"
bridgeWithDefaultMcastSnoop := &Bridge{LinkAttrs: LinkAttrs{Name: bridgeWithDefaultMcastSnoopName}}
if err := LinkAdd(bridgeWithDefaultMcastSnoop); err != nil {
t.Fatal(err)
}
expectMcastSnooping(t, bridgeWithDefaultMcastSnoopName, true)
if err := LinkDel(bridgeWithDefaultMcastSnoop); err != nil {
t.Fatal(err)
}
mcastSnoop := true
bridgeWithMcastSnoopOnName := "bar"
bridgeWithMcastSnoopOn := &Bridge{LinkAttrs: LinkAttrs{Name: bridgeWithMcastSnoopOnName}, MulticastSnooping: &mcastSnoop}
if err := LinkAdd(bridgeWithMcastSnoopOn); err != nil {
t.Fatal(err)
}
expectMcastSnooping(t, bridgeWithMcastSnoopOnName, true)
if err := LinkDel(bridgeWithMcastSnoopOn); err != nil {
t.Fatal(err)
}
mcastSnoop = false
bridgeWithMcastSnoopOffName := "foobar"
bridgeWithMcastSnoopOff := &Bridge{LinkAttrs: LinkAttrs{Name: bridgeWithMcastSnoopOffName}, MulticastSnooping: &mcastSnoop}
if err := LinkAdd(bridgeWithMcastSnoopOff); err != nil {
t.Fatal(err)
}
expectMcastSnooping(t, bridgeWithMcastSnoopOffName, false)
if err := LinkDel(bridgeWithMcastSnoopOff); err != nil {
t.Fatal(err)
}
}
func TestBridgeSetMcastSnoop(t *testing.T) {
minKernelRequired(t, 4, 4)
tearDown := setUpNetlinkTest(t)
defer tearDown()
bridgeName := "foo"
bridge := &Bridge{LinkAttrs: LinkAttrs{Name: bridgeName}}
if err := LinkAdd(bridge); err != nil {
t.Fatal(err)
}
expectMcastSnooping(t, bridgeName, true)
if err := BridgeSetMcastSnoop(bridge, false); err != nil {
t.Fatal(err)
}
expectMcastSnooping(t, bridgeName, false)
if err := BridgeSetMcastSnoop(bridge, true); err != nil {
t.Fatal(err)
}
expectMcastSnooping(t, bridgeName, true)
if err := LinkDel(bridge); err != nil {
t.Fatal(err)
}
}
func expectMcastSnooping(t *testing.T, linkName string, expected bool) {
bridge, err := LinkByName(linkName)
if err != nil {
t.Fatal(err)
}
if actual := *bridge.(*Bridge).MulticastSnooping; actual != expected {
t.Fatalf("expected %t got %t", expected, actual)
}
}
func TestBridgeSetVlanFiltering(t *testing.T) {
minKernelRequired(t, 4, 4)
tearDown := setUpNetlinkTest(t)
defer tearDown()
bridgeName := "foo"
bridge := &Bridge{LinkAttrs: LinkAttrs{Name: bridgeName}}
if err := LinkAdd(bridge); err != nil {
t.Fatal(err)
}
expectVlanFiltering(t, bridgeName, false)
if err := BridgeSetVlanFiltering(bridge, true); err != nil {
t.Fatal(err)
}
expectVlanFiltering(t, bridgeName, true)
if err := BridgeSetVlanFiltering(bridge, false); err != nil {
t.Fatal(err)
}
expectVlanFiltering(t, bridgeName, false)
if err := LinkDel(bridge); err != nil {
t.Fatal(err)
}
}
func TestBridgeDefaultPVID(t *testing.T) {
minKernelRequired(t, 4, 4)
tearDown := setUpNetlinkTest(t)
defer tearDown()
bridgeName := "foo"
bridge := &Bridge{LinkAttrs: LinkAttrs{Name: bridgeName}}
if err := LinkAdd(bridge); err != nil {
t.Fatal(err)
}
expectVlanDefaultPVID(t, bridgeName, 1)
if err := BridgeSetVlanDefaultPVID(bridge, 100); err != nil {
t.Fatal(err)
}
expectVlanDefaultPVID(t, bridgeName, 100)
if err := BridgeSetVlanDefaultPVID(bridge, 0); err != nil {
t.Fatal(err)
}
expectVlanDefaultPVID(t, bridgeName, 0)
if err := LinkDel(bridge); err != nil {
t.Fatal(err)
}
}
func expectVlanFiltering(t *testing.T, linkName string, expected bool) {
bridge, err := LinkByName(linkName)
if err != nil {
t.Fatal(err)
}
if actual := *bridge.(*Bridge).VlanFiltering; actual != expected {
t.Fatalf("expected %t got %t", expected, actual)
}
}
func expectVlanDefaultPVID(t *testing.T, linkName string, expected uint16) {
bridge, err := LinkByName(linkName)
if err != nil {
t.Fatal(err)
}
if actual := *bridge.(*Bridge).VlanDefaultPVID; actual != expected {
t.Fatalf("expected %d got %d", expected, actual)
}
}
func TestBridgeCreationWithAgeingTime(t *testing.T) {
minKernelRequired(t, 3, 18)
tearDown := setUpNetlinkTest(t)
defer tearDown()
bridgeWithSpecifiedAgeingTimeName := "foo"
ageingTime := uint32(20000)
bridgeWithSpecifiedAgeingTime := &Bridge{LinkAttrs: LinkAttrs{Name: bridgeWithSpecifiedAgeingTimeName}, AgeingTime: &ageingTime}
if err := LinkAdd(bridgeWithSpecifiedAgeingTime); err != nil {
t.Fatal(err)
}
retrievedBridge, err := LinkByName(bridgeWithSpecifiedAgeingTimeName)
if err != nil {
t.Fatal(err)
}
actualAgeingTime := *retrievedBridge.(*Bridge).AgeingTime
if actualAgeingTime != ageingTime {
t.Fatalf("expected %d got %d", ageingTime, actualAgeingTime)
}
if err := LinkDel(bridgeWithSpecifiedAgeingTime); err != nil {
t.Fatal(err)
}
bridgeWithDefaultAgeingTimeName := "bar"
bridgeWithDefaultAgeingTime := &Bridge{LinkAttrs: LinkAttrs{Name: bridgeWithDefaultAgeingTimeName}}
if err := LinkAdd(bridgeWithDefaultAgeingTime); err != nil {
t.Fatal(err)
}
retrievedBridge, err = LinkByName(bridgeWithDefaultAgeingTimeName)
if err != nil {
t.Fatal(err)
}
actualAgeingTime = *retrievedBridge.(*Bridge).AgeingTime
if actualAgeingTime != 30000 {
t.Fatalf("expected %d got %d", 30000, actualAgeingTime)
}
if err := LinkDel(bridgeWithDefaultAgeingTime); err != nil {
t.Fatal(err)
}
}
func TestBridgeCreationWithHelloTime(t *testing.T) {
minKernelRequired(t, 3, 18)
tearDown := setUpNetlinkTest(t)
defer tearDown()
bridgeWithSpecifiedHelloTimeName := "foo"
helloTime := uint32(300)
bridgeWithSpecifiedHelloTime := &Bridge{LinkAttrs: LinkAttrs{Name: bridgeWithSpecifiedHelloTimeName}, HelloTime: &helloTime}
if err := LinkAdd(bridgeWithSpecifiedHelloTime); err != nil {
t.Fatal(err)
}
retrievedBridge, err := LinkByName(bridgeWithSpecifiedHelloTimeName)
if err != nil {
t.Fatal(err)
}
actualHelloTime := *retrievedBridge.(*Bridge).HelloTime
if actualHelloTime != helloTime {
t.Fatalf("expected %d got %d", helloTime, actualHelloTime)
}
if err := LinkDel(bridgeWithSpecifiedHelloTime); err != nil {
t.Fatal(err)
}
bridgeWithDefaultHelloTimeName := "bar"
bridgeWithDefaultHelloTime := &Bridge{LinkAttrs: LinkAttrs{Name: bridgeWithDefaultHelloTimeName}}
if err := LinkAdd(bridgeWithDefaultHelloTime); err != nil {
t.Fatal(err)
}
retrievedBridge, err = LinkByName(bridgeWithDefaultHelloTimeName)
if err != nil {
t.Fatal(err)
}
actualHelloTime = *retrievedBridge.(*Bridge).HelloTime
if actualHelloTime != 200 {
t.Fatalf("expected %d got %d", 200, actualHelloTime)
}
if err := LinkDel(bridgeWithDefaultHelloTime); err != nil {
t.Fatal(err)
}
}
func TestBridgeCreationWithVlanFiltering(t *testing.T) {
minKernelRequired(t, 3, 18)
tearDown := setUpNetlinkTest(t)
defer tearDown()
bridgeWithVlanFilteringEnabledName := "foo"
vlanFiltering := true
bridgeWithVlanFilteringEnabled := &Bridge{LinkAttrs: LinkAttrs{Name: bridgeWithVlanFilteringEnabledName}, VlanFiltering: &vlanFiltering}
if err := LinkAdd(bridgeWithVlanFilteringEnabled); err != nil {
t.Fatal(err)
}
retrievedBridge, err := LinkByName(bridgeWithVlanFilteringEnabledName)
if err != nil {
t.Fatal(err)
}
retrievedVlanFilteringState := *retrievedBridge.(*Bridge).VlanFiltering
if retrievedVlanFilteringState != vlanFiltering {
t.Fatalf("expected %t got %t", vlanFiltering, retrievedVlanFilteringState)
}
if err := LinkDel(bridgeWithVlanFilteringEnabled); err != nil {
t.Fatal(err)
}
bridgeWithDefaultVlanFilteringName := "bar"
bridgeWIthDefaultVlanFiltering := &Bridge{LinkAttrs: LinkAttrs{Name: bridgeWithDefaultVlanFilteringName}}
if err := LinkAdd(bridgeWIthDefaultVlanFiltering); err != nil {
t.Fatal(err)
}
retrievedBridge, err = LinkByName(bridgeWithDefaultVlanFilteringName)
if err != nil {
t.Fatal(err)
}
retrievedVlanFilteringState = *retrievedBridge.(*Bridge).VlanFiltering
if retrievedVlanFilteringState != false {
t.Fatalf("expected %t got %t", false, retrievedVlanFilteringState)
}
if err := LinkDel(bridgeWIthDefaultVlanFiltering); err != nil {
t.Fatal(err)
}
}
func TestLinkSubscribeWithProtinfo(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
master := &Bridge{LinkAttrs: LinkAttrs{Name: "foo"}}
if err := LinkAdd(master); err != nil {
t.Fatal(err)
}
slave := &Veth{
LinkAttrs: LinkAttrs{
Name: "bar",
TxQLen: testTxQLen,
MTU: 1400,
MasterIndex: master.Attrs().Index,
},
PeerName: "bar-peer",
}
if err := LinkAdd(slave); err != nil {
t.Fatal(err)
}
ch := make(chan LinkUpdate)
done := make(chan struct{})
defer close(done)
if err := LinkSubscribe(ch, done); err != nil {
t.Fatal(err)
}
if err := LinkSetHairpin(slave, true); err != nil {
t.Fatal(err)
}
select {
case update := <-ch:
if !(update.Attrs().Name == "bar" && update.Attrs().Protinfo != nil &&
update.Attrs().Protinfo.Hairpin) {
t.Fatal("Hairpin update not received as expected")
}
case <-time.After(time.Minute):
t.Fatal("Hairpin update timed out")
}
if err := LinkDel(slave); err != nil {
t.Fatal(err)
}
if err := LinkDel(master); err != nil {
t.Fatal(err)
}
}
func testGTPLink(t *testing.T) *GTP {
conn1, err := net.ListenUDP("udp", &net.UDPAddr{
IP: net.ParseIP("0.0.0.0"),
Port: 3386,
})
if err != nil {
t.Fatal(err)
}
conn2, err := net.ListenUDP("udp", &net.UDPAddr{
IP: net.ParseIP("0.0.0.0"),
Port: 2152,
})
if err != nil {
t.Fatal(err)
}
fd1, _ := conn1.File()
fd2, _ := conn2.File()
return &GTP{
LinkAttrs: LinkAttrs{
Name: "gtp0",
},
FD0: int(fd1.Fd()),
FD1: int(fd2.Fd()),
}
}
func TestLinkAddDelGTP(t *testing.T) {
tearDown := setUpNetlinkTestWithKModule(t, "gtp")
defer tearDown()
gtp := testGTPLink(t)
testLinkAddDel(t, gtp)
}
func TestLinkAddDelXfrmi(t *testing.T) {
minKernelRequired(t, 4, 19)
defer setUpNetlinkTest(t)()
lo, _ := LinkByName("lo")
testLinkAddDel(t, &Xfrmi{
LinkAttrs: LinkAttrs{Name: "xfrm123", ParentIndex: lo.Attrs().Index},
Ifid: 123})
}
func TestLinkAddDelXfrmiNoId(t *testing.T) {
minKernelRequired(t, 4, 19)
defer setUpNetlinkTest(t)()
lo, _ := LinkByName("lo")
err := LinkAdd(&Xfrmi{
LinkAttrs: LinkAttrs{Name: "xfrm0", ParentIndex: lo.Attrs().Index}})
if !errors.Is(err, unix.EINVAL) {
t.Errorf("Error returned expected to be EINVAL")
}
}
func TestLinkByNameWhenLinkIsNotFound(t *testing.T) {
_, err := LinkByName("iammissing")
if err == nil {
t.Fatal("Link not expected to found")
}
_, ok := err.(LinkNotFoundError)
if !ok {
t.Errorf("Error returned expected to of LinkNotFoundError type: %v", err)
}
}
func TestLinkByAliasWhenLinkIsNotFound(t *testing.T) {
_, err := LinkByAlias("iammissing")
if err == nil {
t.Fatal("Link not expected to found")
}
_, ok := err.(LinkNotFoundError)
if !ok {
t.Errorf("Error returned expected to of LinkNotFoundError type: %v", err)
}
}
func TestLinkAddDelTuntap(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
// Mount sysfs so that sysfs gets the namespace tag of the current network namespace
// This is necessary so that /sys shows the network interfaces of the current namespace.
if err := syscall.Mount("sysfs", "/sys", "sysfs", syscall.MS_RDONLY, ""); err != nil {
t.Fatal("Cannot mount sysfs")
}
defer func() {
if err := syscall.Unmount("/sys", 0); err != nil {
t.Fatal("Cannot umount /sys")
}
}()
testLinkAddDel(t, &Tuntap{
LinkAttrs: LinkAttrs{Name: "foo"},
Mode: TUNTAP_MODE_TAP})
}
func TestLinkAddDelTuntapMq(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
if err := syscall.Mount("sysfs", "/sys", "sysfs", syscall.MS_RDONLY, ""); err != nil {
t.Fatal("Cannot mount sysfs")
}
defer func() {
if err := syscall.Unmount("/sys", 0); err != nil {
t.Fatal("Cannot umount /sys")
}
}()
testLinkAddDel(t, &Tuntap{
LinkAttrs: LinkAttrs{Name: "foo"},
Mode: TUNTAP_MODE_TAP,
Queues: 4})
testLinkAddDel(t, &Tuntap{
LinkAttrs: LinkAttrs{Name: "foo"},
Mode: TUNTAP_MODE_TAP,
Queues: 4,
Flags: TUNTAP_MULTI_QUEUE_DEFAULTS | TUNTAP_VNET_HDR})
}
func TestLinkAddDelTuntapOwnerGroup(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
if err := syscall.Mount("sysfs", "/sys", "sysfs", syscall.MS_RDONLY, ""); err != nil {
t.Fatal("Cannot mount sysfs")
}
defer func() {
if err := syscall.Unmount("/sys", 0); err != nil {
t.Fatal("Cannot umount /sys")
}
}()
testLinkAddDel(t, &Tuntap{
LinkAttrs: LinkAttrs{Name: "foo"},
Mode: TUNTAP_MODE_TAP,
Owner: 0,
Group: 0,
})
}
func TestVethPeerIndex(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
const (
vethPeer1 = "vethOne"
vethPeer2 = "vethTwo"
)
link := &Veth{
LinkAttrs: LinkAttrs{
Name: vethPeer1,
MTU: 1500,
Flags: net.FlagUp,
},
PeerName: vethPeer2,
}
if err := LinkAdd(link); err != nil {
t.Fatal(err)
}
linkOne, err := LinkByName("vethOne")
if err != nil {
t.Fatal(err)
}
linkTwo, err := LinkByName("vethTwo")
if err != nil {
t.Fatal(err)
}
peerIndexOne, err := VethPeerIndex(&Veth{LinkAttrs: *linkOne.Attrs()})
if err != nil {
t.Fatal(err)
}
peerIndexTwo, err := VethPeerIndex(&Veth{LinkAttrs: *linkTwo.Attrs()})
if err != nil {
t.Fatal(err)
}
if peerIndexOne != linkTwo.Attrs().Index {
t.Errorf("VethPeerIndex(%s) mismatch %d != %d", linkOne.Attrs().Name, peerIndexOne, linkTwo.Attrs().Index)
}
if peerIndexTwo != linkOne.Attrs().Index {
t.Errorf("VethPeerIndex(%s) mismatch %d != %d", linkTwo.Attrs().Name, peerIndexTwo, linkOne.Attrs().Index)
}
}
func TestLinkSlaveBond(t *testing.T) {
minKernelRequired(t, 3, 13)
tearDown := setUpNetlinkTest(t)
defer tearDown()
const (
bondName = "foo"
slaveName = "fooFoo"
)
bond := NewLinkBond(LinkAttrs{Name: bondName})
bond.Mode = BOND_MODE_BALANCE_RR
if err := LinkAdd(bond); err != nil {
t.Fatal(err)
}
defer LinkDel(bond)
slaveDummy := &Dummy{LinkAttrs{Name: slaveName}}
if err := LinkAdd(slaveDummy); err != nil {
t.Fatal(err)
}
defer LinkDel(slaveDummy)
if err := LinkSetBondSlave(slaveDummy, bond); err != nil {
t.Fatal(err)
}
slaveLink, err := LinkByName(slaveName)
if err != nil {
t.Fatal(err)
}
slave := slaveLink.Attrs().Slave
if slave == nil {
t.Errorf("for %s expected slave is not nil.", slaveName)
}
if slaveType := slave.SlaveType(); slaveType != "bond" {
t.Errorf("for %s expected slave type is 'bond', but '%s'", slaveName, slaveType)
}
}
func TestLinkSetBondSlaveQueueId(t *testing.T) {
minKernelRequired(t, 3, 13)
tearDown := setUpNetlinkTest(t)
defer tearDown()
const (
bondName = "foo"
slave1Name = "fooFoo"
)
bond := NewLinkBond(LinkAttrs{Name: bondName})
if err := LinkAdd(bond); err != nil {
t.Fatal(err)
}
defer LinkDel(bond)
slave := &Dummy{LinkAttrs{Name: slave1Name}}
if err := LinkAdd(slave); err != nil {
t.Fatal(err)
}
defer LinkDel(slave)
if err := LinkSetBondSlave(slave, bond); err != nil {
t.Fatal(err)
}
if err := pkgHandle.LinkSetBondSlaveQueueId(slave, 1); err != nil {
t.Fatal(err)
}
}
func TestLinkSetBondSlave(t *testing.T) {
minKernelRequired(t, 3, 13)
tearDown := setUpNetlinkTest(t)
defer tearDown()
const (
bondName = "foo"
slaveOneName = "fooFoo"
slaveTwoName = "fooBar"
)
bond := NewLinkBond(LinkAttrs{Name: bondName})
bond.Mode = StringToBondModeMap["802.3ad"]
bond.AdSelect = BondAdSelect(BOND_AD_SELECT_BANDWIDTH)
bond.AdActorSysPrio = 1
bond.AdUserPortKey = 1
bond.AdActorSystem, _ = net.ParseMAC("06:aa:bb:cc:dd:ee")
if err := LinkAdd(bond); err != nil {
t.Fatal(err)
}
bondLink, err := LinkByName(bondName)
if err != nil {
t.Fatal(err)
}
defer LinkDel(bondLink)
if err := LinkAdd(&Dummy{LinkAttrs{Name: slaveOneName}}); err != nil {
t.Fatal(err)
}
slaveOneLink, err := LinkByName(slaveOneName)
if err != nil {
t.Fatal(err)
}
defer LinkDel(slaveOneLink)
if err := LinkAdd(&Dummy{LinkAttrs{Name: slaveTwoName}}); err != nil {
t.Fatal(err)
}
slaveTwoLink, err := LinkByName(slaveTwoName)
if err != nil {
t.Fatal(err)
}
defer LinkDel(slaveTwoLink)
if err := LinkSetBondSlave(slaveOneLink, &Bond{LinkAttrs: *bondLink.Attrs()}); err != nil {
t.Fatal(err)
}
if err := LinkSetBondSlave(slaveTwoLink, &Bond{LinkAttrs: *bondLink.Attrs()}); err != nil {
t.Fatal(err)
}
// Update info about interfaces
slaveOneLink, err = LinkByName(slaveOneName)
if err != nil {
t.Fatal(err)
}
slaveTwoLink, err = LinkByName(slaveTwoName)
if err != nil {
t.Fatal(err)
}
if slaveOneLink.Attrs().MasterIndex != bondLink.Attrs().Index {
t.Errorf("For %s expected %s to be master", slaveOneLink.Attrs().Name, bondLink.Attrs().Name)
}
if slaveTwoLink.Attrs().MasterIndex != bondLink.Attrs().Index {
t.Errorf("For %s expected %s to be master", slaveTwoLink.Attrs().Name, bondLink.Attrs().Name)
}
}
func testFailover(t *testing.T, slaveName, bondName string) {
slaveLink, err := LinkByName(slaveName)
if err != nil {
t.Fatal(err)
}
bondLink, err := LinkByName(bondName)
if err != nil {
t.Fatal(err)
}
err = LinkSetBondSlaveActive(slaveLink, &Bond{LinkAttrs: *bondLink.Attrs()})
if err != nil {
t.Errorf("set slave link active failed: %v", err)
return
}
bondLink, err = LinkByName(bondName)
if err != nil {
t.Fatal(err)
}
bond := bondLink.(*Bond)
if bond.ActiveSlave != slaveLink.Attrs().Index {
t.Errorf("the current active slave %d is not expected as %d", bond.ActiveSlave, slaveLink.Attrs().Index)
}
}
func TestLinkFailover(t *testing.T) {
minKernelRequired(t, 3, 13)
tearDown := setUpNetlinkTest(t)
defer tearDown()
const (
bondName = "foo"
slaveOneName = "fooFoo"
slaveTwoName = "fooBar"
)
bond := NewLinkBond(LinkAttrs{Name: bondName})
bond.Mode = StringToBondModeMap["active-backup"]
bond.Miimon = 100
if err := LinkAdd(bond); err != nil {
t.Fatal(err)
}
bondLink, err := LinkByName(bondName)
if err != nil {
t.Fatal(err)
}
defer LinkDel(bondLink)
if err := LinkAdd(&Dummy{LinkAttrs{Name: slaveOneName}}); err != nil {
t.Fatal(err)
}
slaveOneLink, err := LinkByName(slaveOneName)
if err != nil {
t.Fatal(err)
}
defer LinkDel(slaveOneLink)
if err := LinkAdd(&Dummy{LinkAttrs{Name: slaveTwoName}}); err != nil {
t.Fatal(err)
}
slaveTwoLink, err := LinkByName(slaveTwoName)
if err != nil {
t.Fatal(err)
}
defer LinkDel(slaveTwoLink)
if err := LinkSetBondSlave(slaveOneLink, &Bond{LinkAttrs: *bondLink.Attrs()}); err != nil {
t.Fatal(err)
}
if err := LinkSetBondSlave(slaveTwoLink, &Bond{LinkAttrs: *bondLink.Attrs()}); err != nil {
t.Fatal(err)
}
testFailover(t, slaveOneName, bondName)
testFailover(t, slaveTwoName, bondName)
testFailover(t, slaveTwoName, bondName)
// del slave from bond
slaveOneLink, err = LinkByName(slaveOneName)
if err != nil {
t.Fatal(err)
}
err = LinkDelBondSlave(slaveOneLink, &Bond{LinkAttrs: *bondLink.Attrs()})
if err != nil {
t.Errorf("Remove slave %s from bond failed: %v", slaveOneName, err)
}
slaveOneLink, err = LinkByName(slaveOneName)
if err != nil {
t.Fatal(err)
}
if slaveOneLink.Attrs().MasterIndex > 0 {
t.Errorf("The nic %s is still a slave of %d", slaveOneName, slaveOneLink.Attrs().MasterIndex)
}
}
func TestLinkSetAllmulticast(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
iface := &Veth{LinkAttrs: LinkAttrs{Name: "foo"}, PeerName: "bar"}
if err := LinkAdd(iface); err != nil {
t.Fatal(err)
}
link, err := LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if err := LinkSetUp(link); err != nil {
t.Fatal(err)
}
link, err = LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if err := LinkSetAllmulticastOn(link); err != nil {
t.Fatal(err)
}
link, err = LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if link.Attrs().Allmulti != 1 {
t.Fatal("IFF_ALLMULTI was not set")
}
if err := LinkSetAllmulticastOff(link); err != nil {
t.Fatal(err)
}
link, err = LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if link.Attrs().Allmulti != 0 {
t.Fatal("IFF_ALLMULTI is still set")
}
}
func TestLinkSetMulticast(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
iface := &Veth{LinkAttrs: LinkAttrs{Name: "foo"}, PeerName: "bar"}
if err := LinkAdd(iface); err != nil {
t.Fatal(err)
}
link, err := LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if err := LinkSetUp(link); err != nil {
t.Fatal(err)
}
link, err = LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if err := LinkSetMulticastOn(link); err != nil {
t.Fatal(err)
}
link, err = LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if link.Attrs().Multi != 1 {
t.Fatal("IFF_MULTICAST was not set")
}
if err := LinkSetMulticastOff(link); err != nil {
t.Fatal(err)
}
link, err = LinkByName("foo")
if err != nil {
t.Fatal(err)
}
if link.Attrs().Multi != 0 {
t.Fatal("IFF_MULTICAST is still set")
}
}
func TestLinkSetMacvlanMode(t *testing.T) {
tearDown := setUpNetlinkTest(t)
defer tearDown()
const (
parentName = "foo"
macvlanName = "fooFoo"
macvtapName = "fooBar"
)
parent := &Dummy{LinkAttrs{Name: parentName}}
if err := LinkAdd(parent); err != nil {
t.Fatal(err)
}
defer LinkDel(parent)
testMacvlanMode := func(link Link, mode MacvlanMode) {
if err := LinkSetMacvlanMode(link, mode); err != nil {
t.Fatal(err)
}
name := link.Attrs().Name
result, err := LinkByName(name)
if err != nil {
t.Fatal(err)
}
var actual MacvlanMode
switch l := result.(type) {
case *Macvlan:
actual = l.Mode
case *Macvtap:
actual = l.Macvlan.Mode
}
if actual != mode {
t.Fatalf("expected %v got %v for %+v", mode, actual, link)
}
}
macvlan := &Macvlan{
LinkAttrs: LinkAttrs{Name: macvlanName, ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_BRIDGE,
}
if err := LinkAdd(macvlan); err != nil {
t.Fatal(err)
}
defer LinkDel(macvlan)
testMacvlanMode(macvlan, MACVLAN_MODE_VEPA)
testMacvlanMode(macvlan, MACVLAN_MODE_PRIVATE)
testMacvlanMode(macvlan, MACVLAN_MODE_SOURCE)
testMacvlanMode(macvlan, MACVLAN_MODE_BRIDGE)
macvtap := &Macvtap{
Macvlan: Macvlan{
LinkAttrs: LinkAttrs{Name: macvtapName, ParentIndex: parent.Attrs().Index},
Mode: MACVLAN_MODE_BRIDGE,
},
}
if err := LinkAdd(macvtap); err != nil {
t.Fatal(err)
}
defer LinkDel(macvtap)
testMacvlanMode(macvtap, MACVLAN_MODE_VEPA)
testMacvlanMode(macvtap, MACVLAN_MODE_PRIVATE)
testMacvlanMode(macvtap, MACVLAN_MODE_SOURCE)
testMacvlanMode(macvtap, MACVLAN_MODE_BRIDGE)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/mirrors/netlink.git
[email protected]:mirrors/netlink.git
mirrors
netlink
netlink
main

搜索帮助