github.com/vishvananda/netlink@v1.3.0/rdma_link_test.go (about) 1 // +build linux 2 3 package netlink 4 5 import ( 6 "io/ioutil" 7 "strings" 8 "testing" 9 10 "github.com/vishvananda/netns" 11 ) 12 13 func setupRdmaKModule(t *testing.T, name string) { 14 skipUnlessRoot(t) 15 file, err := ioutil.ReadFile("/proc/modules") 16 if err != nil { 17 t.Fatal("Failed to open /proc/modules", err) 18 } 19 for _, line := range strings.Split(string(file), "\n") { 20 n := strings.Split(line, " ")[0] 21 if n == name { 22 return 23 } 24 25 } 26 t.Skipf("Test requires kmodule %q.", name) 27 } 28 29 func TestRdmaGetRdmaLink(t *testing.T) { 30 minKernelRequired(t, 4, 16) 31 setupRdmaKModule(t, "ib_core") 32 _, err := RdmaLinkByName("foo") 33 if err != nil { 34 t.Fatal(err) 35 } 36 } 37 38 func TestRdmaSetRdmaLinkName(t *testing.T) { 39 minKernelRequired(t, 4, 19) 40 setupRdmaKModule(t, "ib_core") 41 link, err := RdmaLinkByName("foo") 42 if err != nil { 43 t.Fatal(err) 44 } 45 // Set new name 46 err = RdmaLinkSetName(link, "bar") 47 if err != nil { 48 t.Fatal(err) 49 } 50 // Revert back to old name 51 err = RdmaLinkSetName(link, "foo") 52 if err != nil { 53 t.Fatal(err) 54 } 55 } 56 57 func TestRdmaSystemGetNetnsMode(t *testing.T) { 58 minKernelRequired(t, 5, 2) 59 setupRdmaKModule(t, "ib_core") 60 61 mode, err := RdmaSystemGetNetnsMode() 62 if err != nil { 63 t.Fatal(err) 64 } 65 t.Log("rdma system netns mode =", mode) 66 } 67 68 func TestRdmaSystemSetNetnsMode(t *testing.T) { 69 var newMode string 70 var mode string 71 var err error 72 73 minKernelRequired(t, 5, 2) 74 setupRdmaKModule(t, "ib_core") 75 76 mode, err = RdmaSystemGetNetnsMode() 77 if err != nil { 78 t.Fatal(err) 79 } 80 t.Log("current rdma system mode =", mode) 81 82 err = RdmaSystemSetNetnsMode(mode) 83 if err != nil { 84 t.Fatal(err) 85 } 86 // Flip the mode from current mode 87 if mode == "exclusive" { 88 RdmaSystemSetNetnsMode("shared") 89 } else { 90 RdmaSystemSetNetnsMode("exclusive") 91 } 92 newMode, err = RdmaSystemGetNetnsMode() 93 if err != nil { 94 t.Fatal(err) 95 } 96 t.Log("new rdma system mode =", newMode) 97 98 // Change back to original mode 99 err = RdmaSystemSetNetnsMode(mode) 100 if err != nil { 101 t.Fatal(err) 102 } 103 } 104 105 func TestRdmaLinkSetNsFd(t *testing.T) { 106 minKernelRequired(t, 5, 2) 107 setupRdmaKModule(t, "ib_core") 108 109 mode, err := RdmaSystemGetNetnsMode() 110 if err != nil { 111 t.Fatal(err) 112 } 113 t.Log("current rdma netns mode", mode) 114 err = RdmaSystemSetNetnsMode("exclusive") 115 if err != nil { 116 t.Fatal(err) 117 } 118 basens, err := netns.Get() 119 if err != nil { 120 RdmaSystemSetNetnsMode(mode) 121 t.Fatal("Failed to get basens") 122 } 123 defer basens.Close() 124 125 newns, err := netns.New() 126 if err != nil { 127 RdmaSystemSetNetnsMode(mode) 128 t.Fatal("Failed to create newns") 129 } 130 131 netns.Set(basens) 132 link, err := RdmaLinkByName("foo") 133 if err != nil { 134 // Remove the namespace as RDMA subsystem requires 135 // no namespace to exist when changing net namespace mode 136 newns.Close() 137 RdmaSystemSetNetnsMode(mode) 138 t.Fatal(err) 139 } 140 t.Log("rdma link: ", link) 141 142 err = RdmaLinkSetNsFd(link, uint32(newns)) 143 if err != nil { 144 newns.Close() 145 RdmaSystemSetNetnsMode(mode) 146 t.Fatal(err) 147 } 148 149 newns.Close() 150 //Set the old mode back at start of the test 151 err = RdmaSystemSetNetnsMode(mode) 152 if err != nil { 153 t.Fatal(err) 154 } 155 } 156 157 func TestRdmaLinkList(t *testing.T) { 158 minKernelRequired(t, 4, 16) 159 setupRdmaKModule(t, "ib_core") 160 links, err := RdmaLinkList() 161 if err != nil { 162 t.Fatal(err) 163 } 164 t.Log("RDMA devices:") 165 for _, link := range links { 166 t.Logf("%d: %s", link.Attrs.Index, link.Attrs.Name) 167 } 168 } 169 170 func TestRdmaLinkAddAndDel(t *testing.T) { 171 // related commit is https://github.com/torvalds/linux/commit/3856ec4b93c9463d36ee39098dde1fbbd29ec6dd. 172 minKernelRequired(t, 5, 1) 173 setupRdmaKModule(t, "rdma_rxe") 174 175 checkPresence := func(name string, exist bool) { 176 links, err := RdmaLinkList() 177 if err != nil { 178 t.Fatal(err) 179 } 180 181 found := false 182 for _, link := range links { 183 if link.Attrs.Name == name { 184 found = true 185 break 186 } 187 } 188 189 if found != exist { 190 t.Fatalf("expected rdma link %s presence=%v, but got presence=%v", name, exist, found) 191 } 192 } 193 194 linkName := t.Name() 195 196 if err := RdmaLinkAdd(linkName, "rxe", "lo"); err != nil { 197 t.Fatal(err) 198 } 199 200 checkPresence(linkName, true) 201 202 if err := RdmaLinkDel(linkName); err != nil { 203 t.Fatal(err) 204 } 205 206 checkPresence(linkName, false) 207 }