blob: ffbc9a0f876ee0456c952d224010de856b00ae21 [file] [log] [blame]
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +09001/*
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
17package setup
18
19import (
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090020 "os/exec"
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070021 "time"
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090022 "log"
23 "gerrit.opencord.org/voltha-bbsim/common"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090024)
25
26const (
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 Scandolo4549d3f2018-10-19 12:48:20 -070033func ActivateWPASups(vethnames []string, delay int) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090034 for _, vethname := range vethnames {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070035 time.Sleep(time.Duration(delay) * time.Second)
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090036 logger.Debug("ActivateWPASups for interface %v\n", vethname)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090037 if err := activateWPASupplicant(vethname); err != nil {
38 return err
39 }
40 }
41 return nil
42}
43
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070044func ActivateDHCPClients(vethnames []string, delay int) error {
Keita NISHIMOTOc864da22018-10-15 22:41:42 +090045 for _, vethname := range vethnames {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070046 time.Sleep(time.Duration(delay) * time.Second)
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090047 logger.Debug("activateDHCPClient for interface %v\n", vethname)
Keita NISHIMOTOc864da22018-10-15 22:41:42 +090048 if err := activateDHCPClient(vethname); err != nil {
49 return err
50 }
51 }
52 return nil
53}
54
Zack Williams8169f672018-10-16 12:01:55 -070055func KillProcess(name string) error {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090056 err := exec.Command("pkill", name).Run()
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090057 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090058 logger.Error("Fail to pkill %s: %v\n", name, err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090059 return err
60 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090061 logger.Info("Successfully killed %s\n", name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090062 return nil
63}
64
65func TearVethDown(veths []string) {
66 for _, veth := range veths {
67 RemoveVeth(veth)
68 }
69}
70
71func 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 NISHIMOTOc66b8eb2018-10-20 07:19:39 +090074 logger.Error("Fail to createVeth() for %s and %s veth creation error: %s\n", name1, name2, err.Error())
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090075 return
76 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090077 logger.Info("%s & %s was created.", name1, name2)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090078 err = exec.Command("ip", "link", "set", name1, "up").Run()
79 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090080 logger.Error("Fail to createVeth() veth1 up", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090081 return
82 }
83 err = exec.Command("ip", "link", "set", name2, "up").Run()
84 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090085 logger.Error("Fail to createVeth() veth2 up", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090086 return
87 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090088 logger.Info("%s & %s was up.", name1, name2)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090089 return
90}
91
92func RemoveVeth(name string) {
93 err := exec.Command("ip", "link", "del", name).Run()
94 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090095 logger.Error("Fail to removeVeth()", err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090096 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +090097 logger.Info("%s was removed.", name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090098}
99
100func RemoveVeths(names []string) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900101 logger.Info("RemoveVeths() :%s\n", names)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900102 for _, name := range names {
103 RemoveVeth(name)
104 }
105}
106
107func 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 NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900112 logger.Error("Fail to activateWPASupplicant() for :%s %v\n", vethname, err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900113 return
114 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900115 logger.Info("activateWPASupplicant() for :%s\n", vethname)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900116 return
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900117}
118
119func activateDHCPClient(vethname string) (err error) {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900120 logger.Debug("activateDHCPClient() start for: %s\n", vethname)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700121 cmd := exec.Command("/usr/local/bin/dhclient", vethname)
122 // if err := cmd.Run(); err != nil {
123 if err := cmd.Start(); err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900124 logger.Error("Fail to activateDHCPClient() for: %s", vethname)
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700125 log.Panic(err)
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900126 }
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700127
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 NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900134 logger.Debug("activateDHCPClient() done for: %s\n", vethname)
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900135 return
136}
137
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900138func ActivateDHCPServer(veth string, serverip string) error {
Zack Williams8169f672018-10-16 12:01:55 -0700139 err := exec.Command("ip", "addr", "add", serverip, "dev", veth).Run()
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900140 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900141 logger.Error("Fail to add ip to %s address: %s\n", veth, err)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900142 return err
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900143 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900144 err = exec.Command("ip", "link", "set", veth, "up").Run()
145 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900146 logger.Error("Fail to set %s up: %s\n", veth, err)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900147 return err
148 }
149 cmd := "/usr/local/bin/dhcpd"
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900150 conf := "/etc/dhcp/dhcpd.conf"
151 err = exec.Command(cmd, "-cf", conf, veth).Run()
152 if err != nil {
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900153 logger.Error("Fail to activateDHCP Server (): %s\n", err)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900154 return err
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900155 }
Keita NISHIMOTOc66b8eb2018-10-20 07:19:39 +0900156 logger.Info("DHCP Server is successfully activated !\n")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900157 return err
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900158}