Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-present Open Networking Foundation |
| 3 | |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package core |
| 18 | |
| 19 | import ( |
| 20 | "context" |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 21 | "os/exec" |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 22 | "sync" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 23 | "time" |
| 24 | |
| 25 | "gerrit.opencord.org/voltha-bbsim/common/logger" |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 26 | "gerrit.opencord.org/voltha-bbsim/common/utils" |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 27 | log "github.com/sirupsen/logrus" |
| 28 | "golang.org/x/sync/errgroup" |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 29 | ) |
| 30 | |
| 31 | const ( |
| 32 | DEFAULT Mode = iota |
| 33 | AAA |
| 34 | BOTH |
| 35 | ) |
| 36 | |
| 37 | type Mode int |
| 38 | |
| 39 | type Tester struct { |
| 40 | Mode Mode |
| 41 | AAAWait int |
| 42 | DhcpWait int |
| 43 | DhcpServerIP string |
| 44 | Processes []string |
| 45 | Intvl int |
| 46 | cancel context.CancelFunc |
| 47 | } |
| 48 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 49 | func NewTester(opt *option) *Tester { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 50 | t := new(Tester) |
| 51 | t.AAAWait = opt.aaawait |
| 52 | t.DhcpWait = opt.dhcpwait |
| 53 | t.DhcpServerIP = opt.dhcpservip |
| 54 | t.Intvl = opt.intvl_test |
| 55 | t.Mode = opt.Mode |
| 56 | return t |
| 57 | } |
| 58 | |
| 59 | //Blocking |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 60 | func (t *Tester) Start(s *Server) error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 61 | ctx := context.Background() |
| 62 | ctx, cancel := context.WithCancel(ctx) |
| 63 | t.cancel = cancel |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 64 | defer func() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 65 | cancel() |
| 66 | t.Initialize() |
| 67 | logger.Debug("Tester Done") |
| 68 | }() |
| 69 | logger.Info("Tester Run() start") |
| 70 | if t.Mode == DEFAULT { |
| 71 | //Empty |
| 72 | } else if t.Mode == AAA || t.Mode == BOTH { |
| 73 | eg, child := errgroup.WithContext(ctx) |
| 74 | child, cancel := context.WithCancel(child) |
| 75 | eg.Go(func() error { |
| 76 | defer func() { |
| 77 | logger.Debug("exeAAATest Done") |
| 78 | }() |
| 79 | err := t.exeAAATest(child, s, t.AAAWait) |
| 80 | return err |
| 81 | }) |
| 82 | |
| 83 | if t.Mode == BOTH { |
| 84 | eg.Go(func() error { |
| 85 | defer func() { |
| 86 | logger.Debug("exeDHCPTest Done") |
| 87 | }() |
| 88 | |
| 89 | err := t.exeDHCPTest(ctx, s, t.DhcpWait) |
| 90 | return err |
| 91 | }) |
| 92 | } |
| 93 | if err := eg.Wait(); err != nil { |
| 94 | logger.Error("Error happened in tester: %s", err) |
| 95 | cancel() |
| 96 | return err |
| 97 | } else { |
| 98 | logger.Debug("Test successfully finished") |
| 99 | } |
| 100 | } |
| 101 | return nil |
| 102 | } |
| 103 | |
| 104 | func (t *Tester) Stop(s *Server) error { |
| 105 | if t.cancel != nil { |
| 106 | t.cancel() |
| 107 | } |
| 108 | return nil |
| 109 | } |
| 110 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 111 | func (t *Tester) Initialize() { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 112 | logger.Info("Tester Initialize () called") |
| 113 | processes := t.Processes |
| 114 | logger.Debug("Runnig Process: %s", processes) |
| 115 | KillProcesses(processes) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 116 | exec.Command("rm", "/var/run/dhcpd.pid").Run() //This is for DHCP server activation |
| 117 | exec.Command("touch", "/var/run/dhcpd.pid").Run() //This is for DHCP server activation |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 118 | } |
| 119 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 120 | type UniVeth struct { |
| 121 | OnuId uint32 |
| 122 | Veth string |
| 123 | } |
| 124 | |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 125 | func (t *Tester) exeAAATest(ctx context.Context, s *Server, wait int) error { |
| 126 | tick := time.NewTicker(time.Second) |
| 127 | defer tick.Stop() |
| 128 | logger.Info("exeAAATest stands by....") |
| 129 | infos, err := s.GetUniIoinfos("outside") |
| 130 | if err != nil { |
| 131 | return err |
| 132 | } |
| 133 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 134 | univeths := []UniVeth{} |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 135 | for _, info := range infos { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 136 | uv := UniVeth{ |
| 137 | OnuId: info.onuid, |
| 138 | Veth: info.Name, |
| 139 | } |
| 140 | univeths = append(univeths, uv) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 141 | } |
| 142 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 143 | for sec := 1; sec <= wait; sec++ { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 144 | select { |
| 145 | case <-ctx.Done(): |
| 146 | logger.Debug("exeAAATest thread receives close ") |
| 147 | return nil |
| 148 | case <-tick.C: |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 149 | logger.WithField("seconds", wait-sec).Info("exeAAATest stands by ... ", wait-sec) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 150 | if sec == wait { |
| 151 | wg := sync.WaitGroup{} |
| 152 | wg.Add(1) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 153 | go func() error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 154 | defer wg.Done() |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 155 | err = activateWPASups(ctx, univeths, t.Intvl, s) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 156 | if err != nil { |
| 157 | return err |
| 158 | } |
| 159 | logger.Info("WPA supplicants are successfully activated ") |
| 160 | t.Processes = append(t.Processes, "wpa_supplicant") |
| 161 | logger.Debug("Running Process:%s", t.Processes) |
| 162 | return nil |
| 163 | }() |
| 164 | wg.Wait() |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | return nil |
| 169 | } |
| 170 | |
| 171 | func (t *Tester) exeDHCPTest(ctx context.Context, s *Server, wait int) error { |
| 172 | tick := time.NewTicker(time.Second) |
| 173 | defer tick.Stop() |
| 174 | logger.Info("exeDHCPTest stands by....") |
| 175 | info, err := s.IdentifyNniIoinfo("outside") |
| 176 | if err != nil { |
| 177 | return err |
| 178 | } |
| 179 | |
| 180 | err = activateDHCPServer(info.Name, t.DhcpServerIP) |
| 181 | if err != nil { |
| 182 | return err |
| 183 | } |
| 184 | t.Processes = append(t.Processes, "dhcpd") |
| 185 | logger.Debug("Running Process:%s", t.Processes) |
| 186 | |
| 187 | infos, err := s.GetUniIoinfos("outside") |
| 188 | if err != nil { |
| 189 | return err |
| 190 | } |
| 191 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 192 | univeths := []UniVeth{} |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 193 | for _, info := range infos { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 194 | uv := UniVeth{ |
| 195 | OnuId: info.onuid, |
| 196 | Veth: info.Name, |
| 197 | } |
| 198 | univeths = append(univeths, uv) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 199 | } |
| 200 | |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 201 | for sec := 1; sec <= wait; sec++ { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 202 | select { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 203 | case <-ctx.Done(): |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 204 | logger.Debug("exeDHCPTest thread receives close ") |
| 205 | return nil |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 206 | case <-tick.C: |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 207 | logger.WithField("seconds", wait-sec).Info("exeDHCPTest stands by ... ", wait-sec) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 208 | if sec == wait { |
| 209 | wg := sync.WaitGroup{} |
| 210 | wg.Add(1) |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 211 | go func() error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 212 | defer wg.Done() |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 213 | err = activateDHCPClients(ctx, univeths, t.Intvl, s) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 214 | if err != nil { |
| 215 | return err |
| 216 | } |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 217 | logger.WithFields(log.Fields{ |
| 218 | "univeths": univeths, |
| 219 | }).Info("DHCP clients are successfully activated") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 220 | t.Processes = append(t.Processes, "dhclient") |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 221 | logger.Debug("Running Process: ", t.Processes) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 222 | return nil |
| 223 | }() |
| 224 | wg.Wait() |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | return nil |
| 229 | } |
| 230 | |
| 231 | func KillProcesses(pnames []string) error { |
| 232 | for _, pname := range pnames { |
| 233 | killProcess(pname) |
| 234 | } |
| 235 | return nil |
| 236 | } |
| 237 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 238 | func activateWPASups(ctx context.Context, vethnames []UniVeth, intvl int, s *Server) error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 239 | tick := time.NewTicker(time.Duration(intvl) * time.Second) |
| 240 | defer tick.Stop() |
| 241 | i := 0 |
| 242 | for { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 243 | select { |
| 244 | case <-tick.C: |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 245 | if i < len(vethnames) { |
| 246 | vethname := vethnames[i] |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 247 | if err := activateWPASupplicant(vethname, s); err != nil { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 248 | return err |
| 249 | } |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 250 | i++ |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 251 | } |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 252 | case <-ctx.Done(): |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 253 | logger.Debug("activateWPASups was canceled by context.") |
| 254 | return nil |
| 255 | } |
| 256 | } |
| 257 | return nil |
| 258 | } |
| 259 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 260 | func activateDHCPClients(ctx context.Context, vethnames []UniVeth, intvl int, s *Server) error { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 261 | tick := time.NewTicker(time.Duration(intvl) * time.Second) |
| 262 | defer tick.Stop() |
| 263 | i := 0 |
| 264 | for { |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 265 | select { |
| 266 | case <-tick.C: |
| 267 | if i < len(vethnames) { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 268 | vethname := vethnames[i] |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 269 | if err := activateDHCPClient(vethname, s); err != nil { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 270 | return err |
| 271 | } |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 272 | i++ |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 273 | } |
Matteo Scandolo | 88e9189 | 2018-11-06 16:29:19 -0800 | [diff] [blame] | 274 | case <-ctx.Done(): |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 275 | logger.Debug("activateDHCPClients was canceled by context.") |
| 276 | return nil |
| 277 | } |
| 278 | } |
| 279 | return nil |
| 280 | } |
| 281 | |
| 282 | func killProcess(name string) error { |
| 283 | err := exec.Command("pkill", name).Run() |
| 284 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 285 | logger.Error("Fail to pkill %s: %v", name, err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 286 | return err |
| 287 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 288 | logger.Info("Successfully killed %s", name) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 289 | return nil |
| 290 | } |
| 291 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 292 | func activateWPASupplicant(univeth UniVeth, s *Server) (err error) { |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 293 | cmd := "/sbin/wpa_supplicant" |
| 294 | conf := "/etc/wpa_supplicant/wpa_supplicant.conf" |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 295 | err = exec.Command(cmd, "-D", "wired", "-i", univeth.Veth, "-c", conf).Start() |
| 296 | onu, _ := s.GetOnuByID(univeth.OnuId) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 297 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 298 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 299 | "err": err, |
| 300 | "veth": univeth.Veth, |
| 301 | }).Error("Fail to activateWPASupplicant()", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 302 | return |
| 303 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 304 | logger.Info("activateWPASupplicant() for :%s", univeth.Veth) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 305 | return |
| 306 | } |
| 307 | |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 308 | func activateDHCPClient(univeth UniVeth, s *Server) (err error) { |
| 309 | onu, _ := s.GetOnuByID(univeth.OnuId) |
| 310 | utils.LoggerWithOnu(onu).WithFields(log.Fields{ |
| 311 | "veth": univeth.Veth, |
| 312 | }).Info("activateDHCPClient() start for: %s", univeth) |
| 313 | cmd := exec.Command("/usr/local/bin/dhclient", univeth.Veth) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 314 | if err := cmd.Start(); err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 315 | logger.Error("Fail to activateDHCPClient() for: %s", univeth) |
| 316 | logger.Panic("activateDHCPClient %s", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 317 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 318 | logger.Debug("activateDHCPClient() done for: %s", univeth) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 319 | return |
| 320 | } |
| 321 | |
| 322 | func activateDHCPServer(veth string, serverip string) error { |
| 323 | err := exec.Command("ip", "addr", "add", serverip, "dev", veth).Run() |
| 324 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 325 | logger.Error("Fail to add ip to %s address: %s", veth, err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 326 | return err |
| 327 | } |
| 328 | err = exec.Command("ip", "link", "set", veth, "up").Run() |
| 329 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 330 | logger.Error("Fail to set %s up: %s", veth, err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 331 | return err |
| 332 | } |
| 333 | cmd := "/usr/local/bin/dhcpd" |
| 334 | conf := "/etc/dhcp/dhcpd.conf" |
| 335 | err = exec.Command(cmd, "-cf", conf, veth).Run() |
| 336 | if err != nil { |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 337 | logger.Error("Fail to activateDHCP Server (): %s", err) |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 338 | return err |
| 339 | } |
Matteo Scandolo | 2aca22c | 2018-11-08 14:12:07 -0800 | [diff] [blame] | 340 | logger.Info("DHCP Server is successfully activated !") |
Keita NISHIMOTO | 9708e04 | 2018-10-27 09:24:44 +0900 | [diff] [blame] | 341 | return err |
| 342 | } |