Skip to content
This repository was archived by the owner on Apr 8, 2025. It is now read-only.

Commit b6e6618

Browse files
authored
Merge pull request #18 from nokia/dev-remove-ovs-python-script
Use go functions to control OVS via cmdline
2 parents 15a4a6e + be33aee commit b6e6618

File tree

4 files changed

+53
-127
lines changed

4 files changed

+53
-127
lines changed

Diff for: Makefile

+2-16
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ endif
2424

2525
# Building the cnivpp subfolder requires VPP to be installed, or at least a
2626
# handful of files in the proper installed location. VPPINSTALLED indicates
27-
# if required VPP files are installed.
27+
# if required VPP files are installed.
2828
# For 'make clean', VPPLCLINSTALLED indicates if 'make install' installed
2929
# the minimum set of files or if VPP is actually installed.
3030
ifeq ($(shell test -e $(VPPLIBDIR)/libvppapiclient.so && echo -n yes),yes)
@@ -39,15 +39,6 @@ else
3939
VPPLCLINSTALLED=0
4040
endif
4141

42-
#
43-
# OVS Variables
44-
#
45-
ifeq ($(shell test -e /usr/share/openvswitch/scripts/ovs-config.py && echo -n yes),yes)
46-
OVS_PY_INSTALLED=1
47-
else
48-
OVS_PY_INSTALLED=0
49-
endif
50-
5142

5243
# Default to build
5344
default: build
@@ -147,11 +138,6 @@ endif
147138
@echo Installed /usr/share/vpp/api/*.json
148139
@rm -rf tmpvpp
149140
endif
150-
ifeq ($(OVS_PY_INSTALLED),0)
151-
@echo OVS Python Script not installed. Installing now.
152-
@$(SUDO) -E mkdir -p /usr/share/openvswitch/scripts/
153-
@$(SUDO) -E cp ./cniovs/scripts/ovs-config.py /usr/share/openvswitch/scripts/.
154-
endif
155141

156142

157143
extras:
@@ -166,7 +152,7 @@ clean:
166152
@rm -f cnivpp/test/memifAddDel/memifAddDel
167153
@rm -f cnivpp/test/vhostUserAddDel/vhostUserAddDel
168154
@rm -f cnivpp/test/ipAddDel/ipAddDel
169-
@rm -f vendor/git.fd.io/govpp.git/cmd/binapi-generator/binapi-generator
155+
@rm -f vendor/git.fd.io/govpp.git/cmd/binapi-generator/binapi-generator
170156
@rm -f userspace/userspace
171157
ifeq ($(VPPLCLINSTALLED),1)
172158
@echo VPP was installed by *make install*, so cleaning up files.

Diff for: cniovs/cniovs/cniovs.go

+5-19
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"errors"
3030
"fmt"
3131
"os"
32-
"os/exec"
3332
"path/filepath"
3433
"regexp"
3534
"strings"
@@ -38,15 +37,14 @@ import (
3837
"github.com/containernetworking/cni/pkg/types/current"
3938

4039
"github.com/intel/userspace-cni-network-plugin/cniovs/ovsdb"
41-
"github.com/intel/userspace-cni-network-plugin/usrsptypes"
4240
"github.com/intel/userspace-cni-network-plugin/logging"
41+
"github.com/intel/userspace-cni-network-plugin/usrsptypes"
4342
)
4443

4544
//
4645
// Constants
4746
//
4847
const defaultCNIDir = "/var/lib/cni/vhostuser"
49-
const defaultOvsScript = "/usr/share/openvswitch/scripts/ovs-config.py"
5048

5149
//
5250
// Types
@@ -144,11 +142,6 @@ func (cniOvs CniOvs) DelFromContainer(conf *usrsptypes.NetConf, args *skel.CmdAr
144142
// Utility Functions
145143
//
146144

147-
// execCommand Execute shell commands and return the output.
148-
func execCommand(cmd string, args []string) ([]byte, error) {
149-
return exec.Command(cmd, args...).Output()
150-
}
151-
152145
func generateRandomMacAddress() string {
153146
buf := make([]byte, 6)
154147
if _, err := rand.Read(buf); err != nil {
@@ -178,16 +171,10 @@ func addLocalDeviceVhost(conf *usrsptypes.NetConf, args *skel.CmdArgs, data *ovs
178171
}
179172
}
180173

181-
sockPath := filepath.Join(sockDir, sockRef)
182-
183174
// ovs-vsctl add-port
184-
cmd_args := []string{"create", sockPath}
185-
if output, err := execCommand(defaultOvsScript, cmd_args); err == nil {
186-
vhostName := strings.Replace(string(output), "\n", "", -1)
187-
188-
cmd_args = []string{"getmac", vhostName}
189-
if output, err := execCommand(defaultCNIDir, cmd_args); err == nil {
190-
data.VhostMac = strings.Replace(string(output), "\n", "", -1)
175+
if vhostName, err := createVhostPort(sockDir, sockRef); err == nil {
176+
if vhostPortMac, err := getVhostPortMac(vhostName); err == nil {
177+
data.VhostMac = vhostPortMac
191178
}
192179

193180
data.Vhostname = vhostName
@@ -200,8 +187,7 @@ func addLocalDeviceVhost(conf *usrsptypes.NetConf, args *skel.CmdArgs, data *ovs
200187
func delLocalDeviceVhost(conf *usrsptypes.NetConf, args *skel.CmdArgs, data *ovsdb.OvsSavedData) error {
201188

202189
// ovs-vsctl --if-exists del-port
203-
cmd_args := []string{"delete", data.Vhostname}
204-
if _, err := execCommand(defaultOvsScript, cmd_args); err == nil {
190+
if err := deleteVhostPort(data.Vhostname); err == nil {
205191
path := filepath.Join(defaultCNIDir, args.ContainerID)
206192

207193
folder, err := os.Open(path)

Diff for: cniovs/cniovs/ovsctrl.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package cniovs
2+
3+
import (
4+
"os"
5+
"os/exec"
6+
"strings"
7+
)
8+
9+
func execCommand(cmd string, args []string) ([]byte, error) {
10+
return exec.Command(cmd, args...).Output()
11+
}
12+
13+
/*
14+
Functions to control OVS by using the ovs-vsctl cmdline client.
15+
*/
16+
17+
func createVhostPort(sock_dir string, sock_name string) (string, error) {
18+
// Create socket
19+
cmd := "ovs-vsctl"
20+
args := []string{"add-port", "br0", sock_name, "--", "set", "Interface", sock_name, "type=dpdkvhostuser"}
21+
if _, err := execCommand(cmd, args); err != nil {
22+
return "", err
23+
}
24+
25+
// Move socket to defined dir for easier mounting
26+
return sock_name, os.Rename(
27+
"/usr/local/var/run/openvswitch/"+sock_name,
28+
sock_dir+"/"+sock_name)
29+
}
30+
31+
func deleteVhostPort(sock_name string) error {
32+
cmd := "ovs-vsctl"
33+
args := []string{"--if-exists", "del-port", "br0", sock_name}
34+
_, err := execCommand(cmd, args)
35+
return err
36+
}
37+
38+
func getVhostPortMac(sock_name string) (string, error) {
39+
cmd := "ovs-vsctl"
40+
args := []string{"--bare", "--columns=mac", "find", "port", "name=" + sock_name}
41+
if mac_b, err := execCommand(cmd, args); err != nil {
42+
return "", err
43+
} else {
44+
return strings.Replace(string(mac_b), "\n", "", -1), nil
45+
}
46+
}

Diff for: cniovs/scripts/ovs-config.py

-92
This file was deleted.

0 commit comments

Comments
 (0)