blob: c39187589012bbc6ec8006f1bc81937d9f3bbe7b [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"
22)
23
24const (
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
31func 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 NISHIMOTOc864da22018-10-15 22:41:42 +090040func 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
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090049func KillAllWPASups() error {
50 err := exec.Command("pkill", "wpa_supplicant").Run()
51 if err != nil {
52 log.Printf("[ERROR] Fail to pkill wpa_supplicant: %v\n", err)
53 return err
54 }
55 return nil
56}
57
58func KillAllDHCPClients() error {
59 err := exec.Command("pkill", "dhclient").Run()
60 if err != nil {
61 log.Printf("[ERROR] Fail to pkill dhclient: %v\n", err)
62 return err
63 }
64 return nil
65}
66
67func TearVethDown(veths []string) {
68 for _, veth := range veths {
69 RemoveVeth(veth)
70 }
71}
72
73func CreateVethPairs(name1 string, name2 string) (err error) {
74 err = exec.Command("ip", "link", "add", name1, "type", "veth", "peer", "name", name2).Run()
75 if err != nil {
76 log.Printf("[ERROR] Fail to createVeth() for %s and %s veth creation error: %s\n", name1, name2, err.Error())
77 return
78 }
79 log.Printf("%s & %s was created.", name1, name2)
80 err = exec.Command("ip", "link", "set", name1, "up").Run()
81 if err != nil {
82 log.Println("[ERROR] Fail to createVeth() veth1 up", err)
83 return
84 }
85 err = exec.Command("ip", "link", "set", name2, "up").Run()
86 if err != nil {
87 log.Println("[ERROR] Fail to createVeth() veth2 up", err)
88 return
89 }
90 log.Printf("%s & %s was up.", name1, name2)
91 return
92}
93
94func RemoveVeth(name string) {
95 err := exec.Command("ip", "link", "del", name).Run()
96 if err != nil {
97 log.Println("[ERROR] Fail to removeVeth()", err)
98 }
99 log.Printf("%s was removed.", name)
100}
101
102func RemoveVeths(names []string) {
103 log.Printf("RemoveVeths() :%s\n", names)
104 for _, name := range names {
105 RemoveVeth(name)
106 }
107}
108
109func activateWPASupplicant(vethname string) (err error) {
110 cmd := "/sbin/wpa_supplicant"
111 conf := "/etc/wpa_supplicant/wpa_supplicant.conf"
112 err = exec.Command(cmd, "-D", "wired", "-i", vethname, "-c", conf).Start()
113 if err != nil {
114 log.Printf("[ERROR] Fail to activateWPASupplicant() for :%s %v\n", vethname, err)
115 return
116 }
117 log.Printf("activateWPASupplicant() for :%s\n", vethname)
118 return
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900119}
120
121func activateDHCPClient(vethname string) (err error) {
122 cmd := "/usr/local/bin/dhclient"
123 err = exec.Command(cmd, vethname).Start()
124 if err != nil {
125 log.Printf("[ERROR] Faile to activateWPASupplicant() for :%s %v\n", vethname, err)
126 return
127 }
128 log.Printf("activateDHCPClient()\n", vethname)
129 return
130}
131
132func ActivateDHCPServer(veth string, serverip string) {
133 cmd := "/sbin/ifconfig"
134 err := exec.Command(cmd, veth, serverip, "up").Run()
135 if err != nil {
136 log.Printf("[ERROR] Fail to up %s()\n", veth, err)
137 return
138 }
139 cmd = "/usr/local/bin/dhcpd"
140 conf := "/etc/dhcp/dhcpd.conf"
141 err = exec.Command(cmd, "-cf", conf, veth).Run()
142 if err != nil {
143 log.Printf("[ERROR] Fail to activateDHCP Server ()\n", err)
144 return
145 }
146 log.Printf("Activate DHCP Server()\n")
147 return
148}