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