Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +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 setup |
| 18 | |
| 19 | import ( |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 20 | "os/exec" |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 21 | "time" |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 22 | "log" |
| 23 | "gerrit.opencord.org/voltha-bbsim/common" |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 24 | ) |
| 25 | |
| 26 | const ( |
| 27 | UNI_VETH_UP_PFX = "sim_uu" |
| 28 | UNI_VETH_DW_PFX = "sim_ud" |
| 29 | NNI_VETH_UP_PFX = "sim_nu" |
| 30 | NNI_VETH_DW_PFX = "sim_nd" |
| 31 | ) |
| 32 | |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 33 | func ActivateWPASups(vethnames []string, delay int) error { |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 34 | for _, vethname := range vethnames { |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 35 | time.Sleep(time.Duration(delay) * time.Second) |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 36 | logger.Debug("ActivateWPASups for interface %v\n", vethname) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 37 | if err := activateWPASupplicant(vethname); err != nil { |
| 38 | return err |
| 39 | } |
| 40 | } |
| 41 | return nil |
| 42 | } |
| 43 | |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 44 | func ActivateDHCPClients(vethnames []string, delay int) error { |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 45 | for _, vethname := range vethnames { |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 46 | time.Sleep(time.Duration(delay) * time.Second) |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 47 | logger.Debug("activateDHCPClient for interface %v\n", vethname) |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 48 | if err := activateDHCPClient(vethname); err != nil { |
| 49 | return err |
| 50 | } |
| 51 | } |
| 52 | return nil |
| 53 | } |
| 54 | |
Zack Williams | 8169f67 | 2018-10-16 12:01:55 -0700 | [diff] [blame] | 55 | func KillProcess(name string) error { |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 56 | err := exec.Command("pkill", name).Run() |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 57 | if err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 58 | logger.Error("Fail to pkill %s: %v\n", name, err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 59 | return err |
| 60 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 61 | logger.Info("Successfully killed %s\n", name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 62 | return nil |
| 63 | } |
| 64 | |
| 65 | func TearVethDown(veths []string) { |
| 66 | for _, veth := range veths { |
| 67 | RemoveVeth(veth) |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | func CreateVethPairs(name1 string, name2 string) (err error) { |
| 72 | err = exec.Command("ip", "link", "add", name1, "type", "veth", "peer", "name", name2).Run() |
| 73 | if err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 74 | logger.Error("Fail to createVeth() for %s and %s veth creation error: %s\n", name1, name2, err.Error()) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 75 | return |
| 76 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 77 | logger.Info("%s & %s was created.", name1, name2) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 78 | err = exec.Command("ip", "link", "set", name1, "up").Run() |
| 79 | if err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 80 | logger.Error("Fail to createVeth() veth1 up", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 81 | return |
| 82 | } |
| 83 | err = exec.Command("ip", "link", "set", name2, "up").Run() |
| 84 | if err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 85 | logger.Error("Fail to createVeth() veth2 up", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 86 | return |
| 87 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 88 | logger.Info("%s & %s was up.", name1, name2) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 89 | return |
| 90 | } |
| 91 | |
| 92 | func RemoveVeth(name string) { |
| 93 | err := exec.Command("ip", "link", "del", name).Run() |
| 94 | if err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 95 | logger.Error("Fail to removeVeth()", err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 96 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 97 | logger.Info("%s was removed.", name) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | func RemoveVeths(names []string) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 101 | logger.Info("RemoveVeths() :%s\n", names) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 102 | for _, name := range names { |
| 103 | RemoveVeth(name) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func activateWPASupplicant(vethname string) (err error) { |
| 108 | cmd := "/sbin/wpa_supplicant" |
| 109 | conf := "/etc/wpa_supplicant/wpa_supplicant.conf" |
| 110 | err = exec.Command(cmd, "-D", "wired", "-i", vethname, "-c", conf).Start() |
| 111 | if err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 112 | logger.Error("Fail to activateWPASupplicant() for :%s %v\n", vethname, err) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 113 | return |
| 114 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 115 | logger.Info("activateWPASupplicant() for :%s\n", vethname) |
Keita NISHIMOTO | 3b8b9c0 | 2018-10-09 09:40:01 +0900 | [diff] [blame] | 116 | return |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | func activateDHCPClient(vethname string) (err error) { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 120 | logger.Debug("activateDHCPClient() start for: %s\n", vethname) |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 121 | cmd := exec.Command("/usr/local/bin/dhclient", vethname) |
| 122 | // if err := cmd.Run(); err != nil { |
| 123 | if err := cmd.Start(); err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 124 | logger.Error("Fail to activateDHCPClient() for: %s", vethname) |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 125 | log.Panic(err) |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 126 | } |
Matteo Scandolo | 4549d3f | 2018-10-19 12:48:20 -0700 | [diff] [blame] | 127 | |
| 128 | // if the dhclient commands return we got an IP, |
| 129 | // so kill it |
| 130 | // if err := KillProcess("/usr/local/bin/dhclient"); err != nil { |
| 131 | // log.Printf("[ERROR] Failed to kill activateDHCPClient() for: %s", vethname, err) |
| 132 | // } |
| 133 | |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 134 | logger.Debug("activateDHCPClient() done for: %s\n", vethname) |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 135 | return |
| 136 | } |
| 137 | |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 138 | func ActivateDHCPServer(veth string, serverip string) error { |
Zack Williams | 8169f67 | 2018-10-16 12:01:55 -0700 | [diff] [blame] | 139 | err := exec.Command("ip", "addr", "add", serverip, "dev", veth).Run() |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 140 | if err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 141 | logger.Error("Fail to add ip to %s address: %s\n", veth, err) |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 142 | return err |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 143 | } |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 144 | err = exec.Command("ip", "link", "set", veth, "up").Run() |
| 145 | if err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 146 | logger.Error("Fail to set %s up: %s\n", veth, err) |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 147 | return err |
| 148 | } |
| 149 | cmd := "/usr/local/bin/dhcpd" |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 150 | conf := "/etc/dhcp/dhcpd.conf" |
| 151 | err = exec.Command(cmd, "-cf", conf, veth).Run() |
| 152 | if err != nil { |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 153 | logger.Error("Fail to activateDHCP Server (): %s\n", err) |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 154 | return err |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 155 | } |
Keita NISHIMOTO | c66b8eb | 2018-10-20 07:19:39 +0900 | [diff] [blame^] | 156 | logger.Info("DHCP Server is successfully activated !\n") |
Keita NISHIMOTO | ca4da5f | 2018-10-15 22:48:52 +0900 | [diff] [blame] | 157 | return err |
Keita NISHIMOTO | c864da2 | 2018-10-15 22:41:42 +0900 | [diff] [blame] | 158 | } |