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