Skip to content

Commit bc12fd2

Browse files
author
Edison Su
committed
Summary: Introduce Vif Driver in KVM
Add BridgeVifDriver and move current vif implementation to it. - remove dependency on VirtualRoutingResource. - factor out some of the networking code in LibvirtComputingResource to BridgeVifDriver. Add base class for KVM VifDriver. Add VifDriver Interface for KVM. RB: https://reviews.apache.org/r/6285 Send-by: Tomoe Sugihara <tomoe@midokura.com>
1 parent 9be74d1 commit bc12fd2

5 files changed

Lines changed: 350 additions & 163 deletions

File tree

‎core/src/com/cloud/agent/resource/virtualnetwork/VirtualRoutingResource.java‎

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -639,49 +639,6 @@ public String assignPublicIpAddress(final String vmName,
639639

640640
return command.execute();
641641
}
642-
643-
private void deletExitingLinkLocalRoutTable(String linkLocalBr) {
644-
Script command = new Script("/bin/bash", _timeout);
645-
command.add("-c");
646-
command.add("ip route | grep " + NetUtils.getLinkLocalCIDR());
647-
OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser();
648-
String result = command.execute(parser);
649-
boolean foundLinkLocalBr = false;
650-
if (result == null && parser.getLines() != null) {
651-
String[] lines = parser.getLines().split("\\n");
652-
for (String line : lines) {
653-
String[] tokens = line.split(" ");
654-
if (!tokens[2].equalsIgnoreCase(linkLocalBr)) {
655-
Script.runSimpleBashScript("ip route del " + NetUtils.getLinkLocalCIDR());
656-
} else {
657-
foundLinkLocalBr = true;
658-
}
659-
}
660-
}
661-
if (!foundLinkLocalBr) {
662-
Script.runSimpleBashScript("ifconfig " + linkLocalBr + " 169.254.0.1;" + "ip route add " + NetUtils.getLinkLocalCIDR() + " dev " + linkLocalBr + " src " + NetUtils.getLinkLocalGateway());
663-
}
664-
}
665-
666-
public void createControlNetwork(String privBrName) {
667-
deletExitingLinkLocalRoutTable(privBrName);
668-
if (!isBridgeExists(privBrName)) {
669-
Script.runSimpleBashScript("brctl addbr " + privBrName + "; ifconfig " + privBrName + " up; ifconfig " + privBrName + " 169.254.0.1", _timeout);
670-
}
671-
}
672-
673-
private boolean isBridgeExists(String bridgeName) {
674-
Script command = new Script("/bin/sh", _timeout);
675-
command.add("-c");
676-
command.add("brctl show|grep " + bridgeName);
677-
final OutputInterpreter.OneLineParser parser = new OutputInterpreter.OneLineParser();
678-
String result = command.execute(parser);
679-
if (result != null || parser.getLine() == null) {
680-
return false;
681-
} else {
682-
return true;
683-
}
684-
}
685642

686643
private void deleteBridge(String brName) {
687644
Script cmd = new Script("/bin/sh", _timeout);
@@ -709,12 +666,6 @@ private void stopDnsmasq(String dnsmasqName) {
709666
cmd.execute();
710667
}
711668

712-
public void cleanupPrivateNetwork(String privNwName, String privBrName){
713-
if (isBridgeExists(privBrName)) {
714-
deleteBridge(privBrName);
715-
}
716-
}
717-
718669
// protected Answer execute(final SetFirewallRuleCommand cmd) {
719670
// String args;
720671
// if(cmd.getProtocol().toLowerCase().equals(NetUtils.NAT_PROTO)){
Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package com.cloud.hypervisor.kvm.resource;
21+
22+
import com.cloud.agent.api.to.NicTO;
23+
import com.cloud.agent.resource.virtualnetwork.VirtualRoutingResource;
24+
import com.cloud.exception.InternalErrorException;
25+
import com.cloud.network.Networks;
26+
import com.cloud.utils.NumbersUtil;
27+
import com.cloud.utils.net.NetUtils;
28+
import com.cloud.utils.script.OutputInterpreter;
29+
import com.cloud.utils.script.Script;
30+
import org.apache.log4j.Logger;
31+
import org.libvirt.LibvirtException;
32+
33+
import javax.naming.ConfigurationException;
34+
import java.net.URI;
35+
import java.util.Map;
36+
37+
public class BridgeVifDriver extends VifDriverBase {
38+
39+
private static final Logger s_logger = Logger
40+
.getLogger(BridgeVifDriver.class);
41+
private int _timeout;
42+
private String _modifyVlanPath;
43+
44+
@Override
45+
public void configure(Map<String, Object> params) throws ConfigurationException {
46+
47+
super.configure(params);
48+
49+
// Set the domr scripts directory
50+
params.put("domr.scripts.dir", "scripts/network/domr/kvm");
51+
52+
53+
String networkScriptsDir = (String) params.get("network.scripts.dir");
54+
if (networkScriptsDir == null) {
55+
networkScriptsDir = "scripts/vm/network/vnet";
56+
}
57+
58+
String value = (String) params.get("scripts.timeout");
59+
_timeout = NumbersUtil.parseInt(value, 30 * 60) * 1000;
60+
61+
_modifyVlanPath = Script.findScript(networkScriptsDir, "modifyvlan.sh");
62+
if (_modifyVlanPath == null) {
63+
throw new ConfigurationException("Unable to find modifyvlan.sh");
64+
}
65+
66+
try {
67+
createControlNetwork();
68+
} catch (LibvirtException e) {
69+
throw new ConfigurationException(e.getMessage());
70+
}
71+
}
72+
73+
@Override
74+
public LibvirtVMDef.InterfaceDef plug(NicTO nic, String guestOsType)
75+
throws InternalErrorException, LibvirtException {
76+
77+
if (s_logger.isDebugEnabled()) {
78+
s_logger.debug("nic=" + nic);
79+
}
80+
81+
LibvirtVMDef.InterfaceDef intf = new LibvirtVMDef.InterfaceDef();
82+
83+
String vlanId = null;
84+
if (nic.getBroadcastType() == Networks.BroadcastDomainType.Vlan) {
85+
URI broadcastUri = nic.getBroadcastUri();
86+
vlanId = broadcastUri.getHost();
87+
}
88+
if (nic.getType() == Networks.TrafficType.Guest) {
89+
if (nic.getBroadcastType() == Networks.BroadcastDomainType.Vlan
90+
&& !vlanId.equalsIgnoreCase("untagged")) {
91+
String brName = createVlanBr(vlanId, _pifs.get("private"));
92+
intf.defBridgeNet(brName, null, nic.getMac(), getGuestNicModel(guestOsType));
93+
} else {
94+
intf.defBridgeNet(_bridges.get("guest"), null, nic.getMac(), getGuestNicModel(guestOsType));
95+
}
96+
} else if (nic.getType() == Networks.TrafficType.Control) {
97+
/* Make sure the network is still there */
98+
createControlNetwork();
99+
intf.defBridgeNet(_bridges.get("linklocal"), null, nic.getMac(), getGuestNicModel(guestOsType));
100+
} else if (nic.getType() == Networks.TrafficType.Public) {
101+
if (nic.getBroadcastType() == Networks.BroadcastDomainType.Vlan
102+
&& !vlanId.equalsIgnoreCase("untagged")) {
103+
String brName = createVlanBr(vlanId, _pifs.get("public"));
104+
intf.defBridgeNet(brName, null, nic.getMac(), getGuestNicModel(guestOsType));
105+
} else {
106+
intf.defBridgeNet(_bridges.get("public"), null, nic.getMac(), getGuestNicModel(guestOsType));
107+
}
108+
} else if (nic.getType() == Networks.TrafficType.Management) {
109+
intf.defBridgeNet(_bridges.get("private"), null, nic.getMac(), getGuestNicModel(guestOsType));
110+
} else if (nic.getType() == Networks.TrafficType.Storage) {
111+
String storageBrName = nic.getName() == null ? _bridges.get("private")
112+
: nic.getName();
113+
intf.defBridgeNet(storageBrName, null, nic.getMac(), getGuestNicModel(guestOsType));
114+
}
115+
return intf;
116+
}
117+
118+
@Override
119+
public void unplug(LibvirtVMDef.InterfaceDef iface) {
120+
// Nothing needed as libvirt cleans up tap interface from bridge.
121+
}
122+
123+
private String setVnetBrName(String vnetId) {
124+
return "cloudVirBr" + vnetId;
125+
}
126+
127+
private String createVlanBr(String vlanId, String nic)
128+
throws InternalErrorException {
129+
String brName = setVnetBrName(vlanId);
130+
createVnet(vlanId, nic);
131+
return brName;
132+
}
133+
134+
private void createVnet(String vnetId, String pif)
135+
throws InternalErrorException {
136+
final Script command = new Script(_modifyVlanPath, _timeout, s_logger);
137+
command.add("-v", vnetId);
138+
command.add("-p", pif);
139+
command.add("-o", "add");
140+
141+
final String result = command.execute();
142+
if (result != null) {
143+
throw new InternalErrorException("Failed to create vnet " + vnetId
144+
+ ": " + result);
145+
}
146+
}
147+
148+
private void createControlNetwork() throws LibvirtException {
149+
createControlNetwork(_bridges.get("linklocal"));
150+
}
151+
152+
private void deletExitingLinkLocalRoutTable(String linkLocalBr) {
153+
Script command = new Script("/bin/bash", _timeout);
154+
command.add("-c");
155+
command.add("ip route | grep " + NetUtils.getLinkLocalCIDR());
156+
OutputInterpreter.AllLinesParser parser = new OutputInterpreter.AllLinesParser();
157+
String result = command.execute(parser);
158+
boolean foundLinkLocalBr = false;
159+
if (result == null && parser.getLines() != null) {
160+
String[] lines = parser.getLines().split("\\n");
161+
for (String line : lines) {
162+
String[] tokens = line.split(" ");
163+
if (!tokens[2].equalsIgnoreCase(linkLocalBr)) {
164+
Script.runSimpleBashScript("ip route del " + NetUtils.getLinkLocalCIDR());
165+
} else {
166+
foundLinkLocalBr = true;
167+
}
168+
}
169+
}
170+
if (!foundLinkLocalBr) {
171+
Script.runSimpleBashScript("ifconfig " + linkLocalBr + " 169.254.0.1;" + "ip route add " +
172+
NetUtils.getLinkLocalCIDR() + " dev " + linkLocalBr + " src " + NetUtils.getLinkLocalGateway());
173+
}
174+
}
175+
176+
private void createControlNetwork(String privBrName) {
177+
deletExitingLinkLocalRoutTable(privBrName);
178+
if (!isBridgeExists(privBrName)) {
179+
Script.runSimpleBashScript("brctl addbr " + privBrName + "; ifconfig " + privBrName + " up; ifconfig " +
180+
privBrName + " 169.254.0.1", _timeout);
181+
}
182+
183+
}
184+
185+
private boolean isBridgeExists(String bridgeName) {
186+
Script command = new Script("/bin/sh", _timeout);
187+
command.add("-c");
188+
command.add("brctl show|grep " + bridgeName);
189+
final OutputInterpreter.OneLineParser parser = new OutputInterpreter.OneLineParser();
190+
String result = command.execute(parser);
191+
if (result != null || parser.getLine() == null) {
192+
return false;
193+
} else {
194+
return true;
195+
}
196+
}
197+
}

0 commit comments

Comments
 (0)