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

Commit 1c3ec18

Browse files
authored
Merge pull request #477 from WeiZhang555/ovs-vlan-support
Add ovs vlan support
2 parents d9023bd + 5bd6123 commit 1c3ec18

File tree

4 files changed

+41
-42
lines changed

4 files changed

+41
-42
lines changed

api/descriptions.pb.go

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/descriptions.proto

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ message InterfaceDescription {
5757
string mac = 5;
5858
string gw = 6;
5959
string tapName = 7;
60+
string options = 8;
6061
}
6162

6263
message PortDescription {
@@ -110,4 +111,4 @@ message Process {
110111
repeated string Args = 7;
111112
repeated string Envs = 8;
112113
string Workdir = 9;
113-
}
114+
}

hypervisor/network/network_linux.go

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -793,45 +793,33 @@ func DeleteBridge(name string) error {
793793

794794
// AddToBridge attch interface to the bridge,
795795
// we only support ovs bridge and linux bridge at present.
796-
func AddToBridge(iface, master *net.Interface) error {
796+
func AddToBridge(iface, master *net.Interface, options string) error {
797797
link, err := netlink.LinkByName(master.Name)
798798
if err != nil {
799799
return err
800800
}
801801

802802
switch link.Type() {
803803
case "openvswitch":
804-
return AddToOpenvswitchBridge(iface, master)
804+
return AddToOpenvswitchBridge(iface, master, options)
805805
case "bridge":
806806
return AddToLinuxBridge(iface, master)
807807
default:
808808
return fmt.Errorf("unknown link type:%+v", link.Type())
809809
}
810810
}
811811

812-
func AddToOpenvswitchBridge(iface, master *net.Interface) error {
813-
glog.V(1).Infof("Found ovs bridge %s, attaching tap %s to it\n", master.Name, iface.Name)
814-
815-
// Check whether there is already a device with the same name has already been attached
816-
// to the ovs bridge or not. If so, skip the follow attaching operation.
817-
out, err := exec.Command("ovs-vsctl", "list-ports", master.Name).CombinedOutput()
818-
if err != nil {
819-
return err
820-
}
821-
ports := strings.Split(strings.TrimSpace(string(out)), "\n")
822-
for _, port := range ports {
823-
if port == iface.Name {
824-
glog.V(1).Infof("A port named %s already exists on bridge %s, using it.\n", iface.Name, master.Name)
825-
return nil
826-
}
827-
}
812+
func AddToOpenvswitchBridge(iface, master *net.Interface, options string) error {
813+
glog.V(3).Infof("Found ovs bridge %s, attaching tap %s to it\n", master.Name, iface.Name)
828814

829815
// ovs command "ovs-vsctl add-port BRIDGE PORT" add netwok device PORT to BRIDGE,
830816
// PORT and BRIDGE here indicate the device name respectively.
831-
out, err = exec.Command("ovs-vsctl", "add-port", master.Name, iface.Name).CombinedOutput()
817+
out, err := exec.Command("ovs-vsctl", "--may-exist", "add-port", master.Name, iface.Name).CombinedOutput()
832818
if err != nil {
833819
return fmt.Errorf("Ovs failed to add port: %s, error :%v", strings.TrimSpace(string(out)), err)
834820
}
821+
822+
out, err = exec.Command("ovs-vsctl", "set", "port", iface.Name, options).CombinedOutput()
835823
return nil
836824
}
837825

@@ -982,7 +970,7 @@ func UpAndAddToBridge(name string) error {
982970
glog.Error("cannot find bridge interface ", BridgeIface)
983971
return err
984972
}
985-
err = AddToBridge(inf, brg)
973+
err = AddToBridge(inf, brg, "")
986974
if err != nil {
987975
glog.Errorf("cannot add %s to %s ", name, BridgeIface)
988976
return err
@@ -996,7 +984,7 @@ func UpAndAddToBridge(name string) error {
996984
return nil
997985
}
998986

999-
func GetTapFd(tapname, bridge string) (device string, tapFile *os.File, err error) {
987+
func GetTapFd(tapname, bridge, options string) (device string, tapFile *os.File, err error) {
1000988
var (
1001989
req ifReq
1002990
errno syscall.Errno
@@ -1036,7 +1024,7 @@ func GetTapFd(tapname, bridge string) (device string, tapFile *os.File, err erro
10361024
return "", nil, err
10371025
}
10381026

1039-
err = AddToBridge(tapIface, bIface)
1027+
err = AddToBridge(tapIface, bIface, options)
10401028
if err != nil {
10411029
glog.Errorf("Add to bridge failed %s %s", bridge, device)
10421030
tapFile.Close()
@@ -1095,7 +1083,7 @@ func Allocate(vmId, requestedIP string, addrOnly bool) (*Settings, error) {
10951083
// return nil, err
10961084
//}
10971085

1098-
device, tapFile, err := GetTapFd("", BridgeIface)
1086+
device, tapFile, err := GetTapFd("", BridgeIface, "")
10991087
if err != nil {
11001088
IpAllocator.ReleaseIP(BridgeIPv4Net, net.ParseIP(setting.IPAddress))
11011089
return nil, err
@@ -1146,7 +1134,7 @@ func Configure(vmId, requestedIP string, addrOnly bool, inf *api.InterfaceDescri
11461134
}, nil
11471135
}
11481136

1149-
device, tapFile, err := GetTapFd(inf.TapName, inf.Bridge)
1137+
device, tapFile, err := GetTapFd(inf.TapName, inf.Bridge, inf.Options)
11501138
if err != nil {
11511139
return nil, err
11521140
}

supervisor/hyperpod.go

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,14 @@ type nsListener struct {
6767
cmd *exec.Cmd
6868
}
6969

70-
func GetBridgeFromIndex(idx int) (string, error) {
70+
func GetBridgeFromIndex(idx int) (string, string, error) {
7171
var attr, bridge *netlink.LinkAttrs
72+
var options string
7273

7374
links, err := netlink.LinkList()
7475
if err != nil {
7576
glog.Error(err)
76-
return "", err
77+
return "", "", err
7778
}
7879

7980
for _, link := range links {
@@ -88,7 +89,7 @@ func GetBridgeFromIndex(idx int) (string, error) {
8889
}
8990

9091
if attr == nil {
91-
return "", fmt.Errorf("cann't find nic whose ifindex is %d", idx)
92+
return "", "", fmt.Errorf("cann't find nic whose ifindex is %d", idx)
9293
}
9394

9495
for _, link := range links {
@@ -103,25 +104,31 @@ func GetBridgeFromIndex(idx int) (string, error) {
103104
}
104105

105106
if bridge == nil {
106-
return "", fmt.Errorf("cann't find bridge contains nic whose ifindex is %d", idx)
107+
return "", "", fmt.Errorf("cann't find bridge contains nic whose ifindex is %d", idx)
107108
}
108109

109110
if bridge.Name == "ovs-system" {
110111
veth, err := netlink.LinkByIndex(idx)
111112
if err != nil {
112-
return "", err
113+
return "", "", err
113114
}
114115

115116
out, err := exec.Command("ovs-vsctl", "port-to-br", veth.Attrs().Name).CombinedOutput()
116117
if err != nil {
117-
return "", err
118+
return "", "", err
118119
}
119120
bridge.Name = strings.TrimSpace(string(out))
121+
122+
out, err = exec.Command("ovs-vsctl", "get", "port", veth.Attrs().Name, "tag").CombinedOutput()
123+
if err != nil {
124+
return "", "", err
125+
}
126+
options = "tag=" + strings.TrimSpace(string(out))
120127
}
121128

122129
glog.V(3).Infof("find bridge %s", bridge.Name)
123130

124-
return bridge.Name, nil
131+
return bridge.Name, options, nil
125132
}
126133

127134
func (hp *HyperPod) initPodNetwork(c *Container) error {
@@ -168,7 +175,7 @@ func (hp *HyperPod) initPodNetwork(c *Container) error {
168175

169176
glog.V(3).Infof("interface configuration of pod ns is %#v", infos)
170177
for _, info := range infos {
171-
bridge, err := GetBridgeFromIndex(info.PeerIndex)
178+
bridge, options, err := GetBridgeFromIndex(info.PeerIndex)
172179
if err != nil {
173180
glog.Error(err)
174181
continue
@@ -177,10 +184,11 @@ func (hp *HyperPod) initPodNetwork(c *Container) error {
177184
nicId := strconv.Itoa(info.Index)
178185

179186
conf := &api.InterfaceDescription{
180-
Id: nicId, //ip as an id
181-
Lo: false,
182-
Bridge: bridge,
183-
Ip: info.Ip,
187+
Id: nicId, //ip as an id
188+
Lo: false,
189+
Bridge: bridge,
190+
Ip: info.Ip,
191+
Options: options,
184192
}
185193

186194
if gw_route != nil && gw_route.LinkIndex == info.Index {
@@ -263,17 +271,18 @@ func (hp *HyperPod) nsListenerStrap() {
263271
continue
264272
}
265273

266-
bridge, err := GetBridgeFromIndex(link.Attrs().ParentIndex)
274+
bridge, options, err := GetBridgeFromIndex(link.Attrs().ParentIndex)
267275
if err != nil {
268276
glog.Error(err)
269277
continue
270278
}
271279

272280
inf := &api.InterfaceDescription{
273-
Id: strconv.Itoa(link.Attrs().Index),
274-
Lo: false,
275-
Bridge: bridge,
276-
Ip: update.Addr.LinkAddress.String(),
281+
Id: strconv.Itoa(link.Attrs().Index),
282+
Lo: false,
283+
Bridge: bridge,
284+
Ip: update.Addr.LinkAddress.String(),
285+
Options: options,
277286
}
278287

279288
err = hp.vm.AddNic(inf)

0 commit comments

Comments
 (0)