blob: 82dd6cf9575f85535aca8b35f95fb4a701daff5c [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 (
20 "log"
21 "os/exec"
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070022 "time"
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090023)
24
25const (
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 Scandolo4549d3f2018-10-19 12:48:20 -070032func ActivateWPASups(vethnames []string, delay int) error {
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090033 for _, vethname := range vethnames {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070034 time.Sleep(time.Duration(delay) * time.Second)
35 log.Printf("ActivateWPASups for interface %v\n", vethname)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090036 if err := activateWPASupplicant(vethname); err != nil {
37 return err
38 }
39 }
40 return nil
41}
42
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070043func ActivateDHCPClients(vethnames []string, delay int) error {
Keita NISHIMOTOc864da22018-10-15 22:41:42 +090044 for _, vethname := range vethnames {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -070045 time.Sleep(time.Duration(delay) * time.Second)
46 log.Printf("activateDHCPClient for interface %v\n", vethname)
Keita NISHIMOTOc864da22018-10-15 22:41:42 +090047 if err := activateDHCPClient(vethname); err != nil {
48 return err
49 }
50 }
51 return nil
52}
53
Zack Williams8169f672018-10-16 12:01:55 -070054func KillProcess(name string) error {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090055 err := exec.Command("pkill", name).Run()
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090056 if err != nil {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090057 log.Printf("[ERROR] Fail to pkill %s: %v\n", name, err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090058 return err
59 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090060 log.Printf("Successfully killed %s\n", name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090061 return nil
62}
63
64func TearVethDown(veths []string) {
65 for _, veth := range veths {
66 RemoveVeth(veth)
67 }
68}
69
70func 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
91func 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
99func RemoveVeths(names []string) {
100 log.Printf("RemoveVeths() :%s\n", names)
101 for _, name := range names {
102 RemoveVeth(name)
103 }
104}
105
106func 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 Scandolo4549d3f2018-10-19 12:48:20 -0700111 log.Printf("[ERROR] Fail to activateWPASupplicant() for: %s %v\n", vethname, err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900112 return
113 }
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700114 log.Printf("activateWPASupplicant() for: %s\n", vethname)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +0900115 return
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900116}
117
118func activateDHCPClient(vethname string) (err error) {
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700119 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 NISHIMOTOc864da22018-10-15 22:41:42 +0900125 }
Matteo Scandolo4549d3f2018-10-19 12:48:20 -0700126
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 NISHIMOTOc864da22018-10-15 22:41:42 +0900134 return
135}
136
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900137func ActivateDHCPServer(veth string, serverip string) error {
Zack Williams8169f672018-10-16 12:01:55 -0700138 err := exec.Command("ip", "addr", "add", serverip, "dev", veth).Run()
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900139 if err != nil {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900140 log.Printf("[ERROR] Fail to add ip to %s address: %s\n", veth, err)
141 return err
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900142 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900143 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 NISHIMOTOc864da22018-10-15 22:41:42 +0900149 conf := "/etc/dhcp/dhcpd.conf"
150 err = exec.Command(cmd, "-cf", conf, veth).Run()
151 if err != nil {
Zack Williams8169f672018-10-16 12:01:55 -0700152 log.Printf("[ERROR] Fail to activateDHCP Server (): %s\n", err)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900153 return err
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900154 }
155 log.Printf("Activate DHCP Server()\n")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900156 return err
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900157}