blob: a4210f543bfbcfd97e2a7ce8196761636403fee3 [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
40func KillAllWPASups() error {
41 err := exec.Command("pkill", "wpa_supplicant").Run()
42 if err != nil {
43 log.Printf("[ERROR] Fail to pkill wpa_supplicant: %v\n", err)
44 return err
45 }
46 return nil
47}
48
49func KillAllDHCPClients() error {
50 err := exec.Command("pkill", "dhclient").Run()
51 if err != nil {
52 log.Printf("[ERROR] Fail to pkill dhclient: %v\n", err)
53 return err
54 }
55 return nil
56}
57
58func TearVethDown(veths []string) {
59 for _, veth := range veths {
60 RemoveVeth(veth)
61 }
62}
63
64func CreateVethPairs(name1 string, name2 string) (err error) {
65 err = exec.Command("ip", "link", "add", name1, "type", "veth", "peer", "name", name2).Run()
66 if err != nil {
67 log.Printf("[ERROR] Fail to createVeth() for %s and %s veth creation error: %s\n", name1, name2, err.Error())
68 return
69 }
70 log.Printf("%s & %s was created.", name1, name2)
71 err = exec.Command("ip", "link", "set", name1, "up").Run()
72 if err != nil {
73 log.Println("[ERROR] Fail to createVeth() veth1 up", err)
74 return
75 }
76 err = exec.Command("ip", "link", "set", name2, "up").Run()
77 if err != nil {
78 log.Println("[ERROR] Fail to createVeth() veth2 up", err)
79 return
80 }
81 log.Printf("%s & %s was up.", name1, name2)
82 return
83}
84
85func RemoveVeth(name string) {
86 err := exec.Command("ip", "link", "del", name).Run()
87 if err != nil {
88 log.Println("[ERROR] Fail to removeVeth()", err)
89 }
90 log.Printf("%s was removed.", name)
91}
92
93func RemoveVeths(names []string) {
94 log.Printf("RemoveVeths() :%s\n", names)
95 for _, name := range names {
96 RemoveVeth(name)
97 }
98}
99
100func activateWPASupplicant(vethname string) (err error) {
101 cmd := "/sbin/wpa_supplicant"
102 conf := "/etc/wpa_supplicant/wpa_supplicant.conf"
103 err = exec.Command(cmd, "-D", "wired", "-i", vethname, "-c", conf).Start()
104 if err != nil {
105 log.Printf("[ERROR] Fail to activateWPASupplicant() for :%s %v\n", vethname, err)
106 return
107 }
108 log.Printf("activateWPASupplicant() for :%s\n", vethname)
109 return
110}