Skip to content

Commit d70d2f8

Browse files
author
Alena Prokharchyk
committed
StartRouter: set only control nic during the initial router start; plug exising public and guest nics after the router is started with control nic
1 parent c75fe80 commit d70d2f8

5 files changed

Lines changed: 103 additions & 48 deletions

File tree

server/src/com/cloud/network/router/VirtualNetworkApplianceManagerImpl.java

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@
201201
import com.cloud.utils.net.MacAddress;
202202
import com.cloud.utils.net.NetUtils;
203203
import com.cloud.vm.DomainRouterVO;
204+
import com.cloud.vm.Nic;
204205
import com.cloud.vm.NicProfile;
205206
import com.cloud.vm.NicVO;
206207
import com.cloud.vm.ReservationContext;
@@ -1565,7 +1566,10 @@ public boolean finalizeVirtualMachineProfile(VirtualMachineProfile<DomainRouterV
15651566
String defaultDns1 = null;
15661567
String defaultDns2 = null;
15671568

1568-
for (NicProfile nic : profile.getNics()) {
1569+
1570+
Iterator<NicProfile> it = profile.getNics().iterator();
1571+
while (it.hasNext()) {
1572+
NicProfile nic = it.next();
15691573
int deviceId = nic.getDeviceId();
15701574
buf.append(" eth").append(deviceId).append("ip=").append(nic.getIp4Address());
15711575
buf.append(" eth").append(deviceId).append("mask=").append(nic.getNetmask());
@@ -1600,6 +1604,10 @@ public boolean finalizeVirtualMachineProfile(VirtualMachineProfile<DomainRouterV
16001604
buf.append(" localgw=").append(dest.getPod().getGateway());
16011605
}
16021606
}
1607+
} else {
1608+
//Remove public and guest nics from the profile
1609+
s_logger.debug("Removing nic of type " + nic.getTrafficType() + " from virtual machine profile " + profile.getVirtualMachine());
1610+
it.remove();
16031611
}
16041612
}
16051613

@@ -1893,15 +1901,6 @@ public boolean finalizeStart(VirtualMachineProfile<DomainRouterVO> profile, long
18931901
ReservationContext context) {
18941902
DomainRouterVO router = profile.getVirtualMachine();
18951903

1896-
//Get guest nic info
1897-
List<NicProfile> routerNics = profile.getNics();
1898-
List<Network> guestNetworks = new ArrayList<Network>();
1899-
for (NicProfile routerNic : routerNics) {
1900-
if (routerNic.getTrafficType() == TrafficType.Guest) {
1901-
guestNetworks.add(_networkMgr.getNetwork(routerNic.getNetworkId()));
1902-
}
1903-
}
1904-
19051904
boolean result = true;
19061905

19071906
Answer answer = cmds.getAnswer("checkSsh");
@@ -1917,6 +1916,23 @@ public boolean finalizeStart(VirtualMachineProfile<DomainRouterVO> profile, long
19171916
if (result == false) {
19181917
return result;
19191918
}
1919+
1920+
//Get guest nic info
1921+
Map<Nic, Network> guestNics = new HashMap<Nic, Network>();
1922+
Map<Nic, Network> publicNics = new HashMap<Nic, Network>();
1923+
List<Network> guestNetworks = new ArrayList<Network>();
1924+
1925+
List<? extends Nic> routerNics = _nicDao.listByVmId(profile.getId());
1926+
for (Nic routerNic : routerNics) {
1927+
Network network = _networkMgr.getNetwork(routerNic.getNetworkId());
1928+
if (network.getTrafficType() == TrafficType.Guest) {
1929+
guestNics.put(routerNic, network);
1930+
guestNetworks.add(network);
1931+
} else if (network.getTrafficType() == TrafficType.Public) {
1932+
publicNics.put(routerNic, network);
1933+
}
1934+
}
1935+
19201936
answer = cmds.getAnswer("getDomRVersion");
19211937
if (answer != null && answer instanceof GetDomRVersionAnswer) {
19221938
GetDomRVersionAnswer versionAnswer = (GetDomRVersionAnswer)answer;
@@ -1931,6 +1947,30 @@ public boolean finalizeStart(VirtualMachineProfile<DomainRouterVO> profile, long
19311947
} else {
19321948
result = false;
19331949
}
1950+
1951+
try {
1952+
//add router to public and guest networks
1953+
for (Nic publicNic : publicNics.keySet()) {
1954+
Network publicNtwk = publicNics.get(publicNic);
1955+
if (!addRouterToPublicNetwork(router, publicNtwk, _ipAddressDao.findByIpAndSourceNetworkId(publicNtwk.getId(),
1956+
publicNic.getIp4Address()))) {
1957+
s_logger.warn("Failed to plug nic " + publicNic + " to router " + router);
1958+
return false;
1959+
}
1960+
}
1961+
1962+
for (Nic guestNic : guestNics.keySet()) {
1963+
Network guestNtwk = guestNics.get(guestNic);
1964+
if (!addRouterToGuestNetwork(router, guestNtwk, false)) {
1965+
s_logger.warn("Failed to plug nic " + guestNic + " to router " + router);
1966+
return false;
1967+
}
1968+
}
1969+
} catch (Exception ex) {
1970+
s_logger.warn("Failed to plug nic for router " + router + " due to exception ", ex);
1971+
return false;
1972+
}
1973+
19341974

19351975
return result;
19361976
}
@@ -3051,7 +3091,7 @@ protected boolean setupGuestNetwork(Network network, VirtualRouter router, boole
30513091
}
30523092

30533093
NicVO nic = _nicDao.findByInstanceIdAndNetworkId(network.getId(), router.getId());
3054-
NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), null,
3094+
NicProfile nicProfile = new NicProfile(nic, network, nic.getBroadcastUri(), nic.getIsolationUri(), _networkMgr.getNetworkRate(network.getId(), router.getId()),
30553095
_networkMgr.isSecurityGroupSupportedInNetwork(network), _networkMgr.getNetworkTag(router.getHypervisorType(), network));
30563096

30573097
SetupGuestNetworkCommand setupCmd = new SetupGuestNetworkCommand(dhcpRange, networkDomain, isRedundant, priority,
@@ -3086,12 +3126,6 @@ public boolean addRouterToGuestNetwork(VirtualRouter router, Network network, bo
30863126
return false;
30873127
}
30883128

3089-
//Check if router is already a part of the Guest network
3090-
if (_networkMgr.isVmPartOfNetwork(router.getId(), network.getId())) {
3091-
s_logger.debug("Router " + router + " is already part of the Guest network " + network);
3092-
return true;
3093-
}
3094-
30953129
//Add router to the Guest network
30963130
boolean result = true;
30973131
try {
@@ -3155,29 +3189,23 @@ public boolean removeRouterFromGuestNetwork(VirtualRouter router, Network networ
31553189
return result;
31563190
}
31573191

3158-
protected boolean addRouterToPublicNetwork(VirtualRouter router, Network publicNetwork, IpAddress sourceNatIp)
3192+
protected boolean addRouterToPublicNetwork(VirtualRouter router, Network publicNetwork, IpAddress publicIpAddr)
31593193
throws ConcurrentOperationException,ResourceUnavailableException, InsufficientCapacityException {
31603194

31613195
if (publicNetwork.getTrafficType() != TrafficType.Public) {
31623196
s_logger.warn("Network " + publicNetwork + " is not of type " + TrafficType.Public);
31633197
return false;
31643198
}
31653199

3166-
//Check if router is already a part of the Public network
3167-
if (_networkMgr.isVmPartOfNetwork(router.getId(), publicNetwork.getId())) {
3168-
s_logger.debug("Router " + router + " is already part of the Public network " + publicNetwork);
3169-
return true;
3170-
}
3171-
31723200
//Add router to the Public network
31733201
boolean result = true;
31743202
try {
31753203

31763204
NicProfile publicNic = _itMgr.addVmToNetwork(router, publicNetwork);
31773205
//setup public network
31783206
if (publicNic != null) {
3179-
if (sourceNatIp != null) {
3180-
IPAddressVO ipVO = _ipAddressDao.findById(sourceNatIp.getId());
3207+
if (publicIpAddr != null) {
3208+
IPAddressVO ipVO = _ipAddressDao.findById(publicIpAddr.getId());
31813209
PublicIp publicIp = new PublicIp(ipVO, _vlanDao.findById(ipVO.getVlanId()),
31823210
NetUtils.createSequenceBasedMacAddress(ipVO.getMacAddress()));
31833211
result = setupPublicNetwork(publicNetwork, router, false, publicIp);
@@ -3242,10 +3270,11 @@ protected boolean removeRouterFromPublicNetwork(VirtualRouter router, Network pu
32423270
return result;
32433271
}
32443272

3245-
protected boolean setupPublicNetwork(Network network, VirtualRouter router, boolean add, PublicIp sourceNatIp)
3273+
protected boolean setupPublicNetwork(Network network, VirtualRouter router, boolean add, PublicIp ipAddress)
32463274
throws ConcurrentOperationException, ResourceUnavailableException{
32473275

32483276
List<PublicIp> publicIps = new ArrayList<PublicIp>(1);
3277+
publicIps.add(ipAddress);
32493278
Commands cmds = new Commands(OnError.Stop);
32503279
createAssociateIPCommands(router, publicIps, cmds, 0);
32513280
sendCommandsToRouter(router, cmds);
@@ -3254,7 +3283,7 @@ protected boolean setupPublicNetwork(Network network, VirtualRouter router, bool
32543283
IpAssocAnswer ipAssocAnswer = cmds.getAnswer(IpAssocAnswer.class);
32553284
String setup = add ? "set" : "destroy";
32563285
if (!(ipAssocAnswer != null && ipAssocAnswer.getResult())) {
3257-
s_logger.warn("Unable to " + setup + " guest network on router " + router);
3286+
s_logger.warn("Unable to " + setup + " public network on router " + router);
32583287
result = false;
32593288
}
32603289

server/src/com/cloud/vm/VirtualMachineManagerImpl.java

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2448,31 +2448,40 @@ public NicProfile addVmToNetwork(VirtualMachine vm, Network network) throws Conc
24482448
Host host = _hostDao.findById(vm.getHostId());
24492449
DeployDestination dest = new DeployDestination(dc, null, null, host);
24502450

2451-
s_logger.debug("Adding vm " + vm + " to network " + network);
2452-
2453-
Transaction txn = Transaction.currentTxn();
2454-
txn.start();
2455-
//1) allocate nic
2456-
NicProfile nic = _networkMgr.allocateNic(null, network, false,
2457-
100, vmProfile).first();
2458-
2459-
s_logger.debug("Nic is allocated successfully for vm " + vm + " in network " + network);
2460-
2461-
//2) Prepare nic
2462-
nic = _networkMgr.prepareNic(vmProfile, dest, context, nic.getId(), networkVO);
2463-
2464-
s_logger.debug("Nic is prepared successfully for vm " + vm + " in network " + network);
2451+
NicProfile nic = null;
2452+
NicVO nicVO = _nicsDao.findByInstanceIdAndNetworkId(network.getId(), vm.getId());
2453+
if (nicVO != null) {
2454+
nic = new NicProfile(nicVO, network, nicVO.getBroadcastUri(), nicVO.getIsolationUri(), _networkMgr.getNetworkRate(network.getId(), vm.getId()),
2455+
_networkMgr.isSecurityGroupSupportedInNetwork(network), _networkMgr.getNetworkTag(vm.getHypervisorType(), network));
2456+
}
24652457

2466-
txn.commit();
2458+
if (nic == null) {
2459+
s_logger.debug("Allocating nic for the " + vm + " in network " + network);
2460+
Transaction txn = Transaction.currentTxn();
2461+
txn.start();
2462+
//1) allocate nic and prepare nic if needed
2463+
int deviceId = _nicsDao.countNics(vm.getId());
2464+
2465+
nic = _networkMgr.allocateNic(null, network, false,
2466+
deviceId, vmProfile).first();
2467+
2468+
s_logger.debug("Nic is allocated successfully for vm " + vm + " in network " + network);
2469+
2470+
nic = _networkMgr.prepareNic(vmProfile, dest, context, nic.getId(), networkVO);
2471+
2472+
s_logger.debug("Nic is prepared successfully for vm " + vm + " in network " + network);
2473+
2474+
txn.commit();
2475+
}
24672476

2468-
//3) Convert vmProfile to vmTO
2477+
//2) Convert vmProfile to vmTO
24692478
HypervisorGuru hvGuru = _hvGuruMgr.getGuru(vmProfile.getVirtualMachine().getHypervisorType());
24702479
VirtualMachineTO vmTO = hvGuru.implement(vmProfile);
24712480

2472-
//4) Convert nicProfile to NicTO
2481+
//3) Convert nicProfile to NicTO
24732482
NicTO nicTO = toNicTO(nic, vmProfile.getVirtualMachine().getHypervisorType());
24742483

2475-
//5) plug the nic to the vm
2484+
//4) plug the nic to the vm
24762485
VirtualMachineGuru<VMInstanceVO> vmGuru = getVmGuru(vmVO);
24772486

24782487
if (vmGuru.plugNic(network, nicTO, vmTO, context, dest)) {
@@ -2482,7 +2491,6 @@ public NicProfile addVmToNetwork(VirtualMachine vm, Network network) throws Conc
24822491
s_logger.warn("Failed to plug nic to the vm " + vm + " in network " + network);
24832492
return null;
24842493
}
2485-
24862494
}
24872495

24882496
@Override

server/src/com/cloud/vm/dao/NicDao.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,6 @@ public interface NicDao extends GenericDao<NicVO, Long> {
4646
NicVO findNonReleasedByInstanceIdAndNetworkId(long networkId, long instanceId);
4747

4848
String getIpAddress(long networkId, long instanceId);
49+
50+
int countNics(long instanceId);
4951
}

server/src/com/cloud/vm/dao/NicDaoImpl.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ public class NicDaoImpl extends GenericDaoBase<NicVO, Long> implements NicDao {
3232
private final SearchBuilder<NicVO> AllFieldsSearch;
3333
private final GenericSearchBuilder<NicVO, String> IpSearch;
3434
private final SearchBuilder<NicVO> NonReleasedSearch;
35+
final GenericSearchBuilder<NicVO, Integer> CountBy;
36+
3537

3638
protected NicDaoImpl() {
3739
super();
@@ -55,6 +57,12 @@ protected NicDaoImpl() {
5557
NonReleasedSearch.and("network", NonReleasedSearch.entity().getNetworkId(), Op.EQ);
5658
NonReleasedSearch.and("state", NonReleasedSearch.entity().getState(), Op.NOTIN);
5759
NonReleasedSearch.done();
60+
61+
CountBy = createSearchBuilder(Integer.class);
62+
CountBy.select(null, Func.COUNT, CountBy.entity().getId());
63+
CountBy.and("vmId", CountBy.entity().getInstanceId(), Op.EQ);
64+
CountBy.and("removed", CountBy.entity().getRemoved(), Op.NULL);
65+
CountBy.done();
5866
}
5967

6068
@Override
@@ -150,4 +158,12 @@ public String getIpAddress(long networkId, long instanceId) {
150158
return findOneBy(sc).getIp4Address();
151159
}
152160

161+
@Override
162+
public int countNics(long instanceId) {
163+
SearchCriteria<Integer> sc = CountBy.create();
164+
sc.setParameters("vmId", instanceId);
165+
List<Integer> results = customSearch(sc, null);
166+
return results.get(0);
167+
}
168+
153169
}

wscript

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
# the following two variables are used by the target "waf dist"
55
# if you change 'em here, you need to change it also in cloud.spec, add a %changelog entry there, and add an entry in debian/changelog
66

7-
VERSION = '3.0.3.2012-06-06T23:35:14Z'
7+
VERSION = '3.0.3.2012-06-06T23:43:59Z'
88
APPNAME = 'cloud'
99

1010
import shutil,os

0 commit comments

Comments
 (0)