blob: 6d9981d7048ef3add737e809e0b12432a017cec2 [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
Zack Williams8169f672018-10-16 12:01:55 -070049func KillProcess(name string) error {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090050 err := exec.Command("pkill", name).Run()
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090051 if err != nil {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090052 log.Printf("[ERROR] Fail to pkill %s: %v\n", name, err)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090053 return err
54 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +090055 log.Printf("Successfully killed %s\n", name)
Keita NISHIMOTO3b8b9c02018-10-09 09:40:01 +090056 return nil
57}
58
59func TearVethDown(veths []string) {
60 for _, veth := range veths {
61 RemoveVeth(veth)
62 }
63}
64
65func 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
86func 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
94func RemoveVeths(names []string) {
95 log.Printf("RemoveVeths() :%s\n", names)
96 for _, name := range names {
97 RemoveVeth(name)
98 }
99}
100
101func 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 NISHIMOTOc864da22018-10-15 22:41:42 +0900111}
112
113func 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 Williams8169f672018-10-16 12:01:55 -0700120 log.Printf("activateDHCPClient() for: %s\n", vethname)
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900121 return
122}
123
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900124func ActivateDHCPServer(veth string, serverip string) error {
Zack Williams8169f672018-10-16 12:01:55 -0700125 err := exec.Command("ip", "addr", "add", serverip, "dev", veth).Run()
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900126 if err != nil {
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900127 log.Printf("[ERROR] Fail to add ip to %s address: %s\n", veth, err)
128 return err
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900129 }
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900130 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 NISHIMOTOc864da22018-10-15 22:41:42 +0900136 conf := "/etc/dhcp/dhcpd.conf"
137 err = exec.Command(cmd, "-cf", conf, veth).Run()
138 if err != nil {
Zack Williams8169f672018-10-16 12:01:55 -0700139 log.Printf("[ERROR] Fail to activateDHCP Server (): %s\n", err)
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900140 return err
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900141 }
142 log.Printf("Activate DHCP Server()\n")
Keita NISHIMOTOca4da5f2018-10-15 22:48:52 +0900143 return err
Keita NISHIMOTOc864da22018-10-15 22:41:42 +0900144}