Skip to content

Commit 157156d

Browse files
committed
1)Introduced new Network Guru - used for direct tagged network.
2)Made vm start with multiple networks
1 parent 8fb9486 commit 157156d

9 files changed

Lines changed: 238 additions & 35 deletions

File tree

‎client/tomcatconf/components.xml.in‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<adapter name="PublicNetworkGuru" class="com.cloud.network.guru.PublicNetworkGuru"/>
4545
<adapter name="PodBasedNetworkGuru" class="com.cloud.network.guru.PodBasedNetworkGuru"/>
4646
<adapter name="ControlNetworkGuru" class="com.cloud.network.guru.ControlNetworkGuru"/>
47+
<adapter name="DirectNetworkGuru" class="com.cloud.network.guru.DirectNetworkGuru"/>
4748
</adapters>
4849
<adapters key="com.cloud.storage.secondary.SecondaryStorageVmAllocator">
4950
<adapter name="Balance" class="com.cloud.storage.secondary.SecondaryStorageVmDefaultAllocator"/>

‎server/src/com/cloud/configuration/ConfigurationManagerImpl.java‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,6 +1507,11 @@ public Vlan createVlanAndPublicIpRange(CreateVlanIpRangeCmd cmd) throws Insuffic
15071507
throw new InvalidParameterValueException("Gateway, netmask and zoneId have to be passed in for virtual network");
15081508
}
15091509

1510+
//if end ip is not specified, default it to startIp
1511+
if (endIP == null && startIP != null) {
1512+
endIP = startIP;
1513+
}
1514+
15101515
//Verify that network is valid, and ip range matches network's cidr
15111516
if (networkId != null) {
15121517
NetworkVO network = _networkDao.findById(networkId);
@@ -2455,9 +2460,9 @@ public boolean deleteVlanIpRange(DeleteVlanIpRangeCmd cmd) throws InvalidParamet
24552460
throw new InvalidParameterValueException("Please specify a valid IP range id.");
24562461
}
24572462

2458-
if (vlan.getNetworkId() != null) {
2459-
throw new InvalidParameterValueException("Fail to delete a vlan range as there are networks associated with it");
2460-
}
2463+
// if (vlan.getNetworkId() != null) {
2464+
// throw new InvalidParameterValueException("Fail to delete a vlan range as there are networks associated with it");
2465+
// }
24612466

24622467
return deleteVlanAndPublicIpRange(userId, vlanDbId);
24632468

‎server/src/com/cloud/network/NetworkManager.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import java.util.List;
2121

22+
import com.cloud.dc.Vlan.VlanType;
2223
import com.cloud.deploy.DeployDestination;
2324
import com.cloud.deploy.DeploymentPlan;
2425
import com.cloud.exception.ConcurrentOperationException;
@@ -112,4 +113,6 @@ public interface NetworkManager extends NetworkService {
112113
String getNextAvailableMacAddressInNetwork(long networkConfigurationId) throws InsufficientAddressCapacityException;
113114

114115
boolean applyRules(Ip ip, List<? extends FirewallRule> rules, boolean continueOnError) throws ResourceUnavailableException;
116+
117+
PublicIp fetchNewPublicIp(long dcId, VlanType vlanUse, Account owner, Long networkId, boolean sourceNat) throws InsufficientAddressCapacityException;
115118
}

‎server/src/com/cloud/network/NetworkManagerImpl.java‎

Lines changed: 43 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -192,14 +192,19 @@ public class NetworkManagerImpl implements NetworkManager, NetworkService, Manag
192192

193193
private Map<String, String> _configs;
194194

195-
@DB
196-
protected PublicIp fetchNewPublicIp(long dcId, VlanType vlanUse, Account owner, Long networkId, boolean sourceNat) throws InsufficientAddressCapacityException {
195+
@Override @DB
196+
public PublicIp fetchNewPublicIp(long dcId, VlanType vlanUse, Account owner, Long networkId, boolean sourceNat) throws InsufficientAddressCapacityException {
197197
Transaction txn = Transaction.currentTxn();
198198
txn.start();
199199
SearchCriteria<IPAddressVO> sc = AssignIpAddressSearch.create();
200200
sc.setParameters("dc", dcId);
201-
sc.setJoinParameters("vlan", "type", vlanUse);
202201

202+
//for direct network take ip addresses only from the vlans belonging to the network
203+
if (vlanUse == VlanType.DirectAttached) {
204+
sc.setJoinParameters("vlan", "networkId", networkId);
205+
}
206+
sc.setJoinParameters("vlan", "type", vlanUse);
207+
203208
Filter filter = new Filter(IPAddressVO.class, "vlanId", true, 0l, 1l);
204209

205210
List<IPAddressVO> addrs = _ipAddressDao.lockRows(sc, filter, true);
@@ -214,7 +219,11 @@ protected PublicIp fetchNewPublicIp(long dcId, VlanType vlanUse, Account owner,
214219
addr.setAllocatedTime(new Date());
215220
addr.setAllocatedInDomainId(owner.getDomainId());
216221
addr.setAllocatedToAccountId(owner.getId());
217-
addr.setAssociatedNetworkId(networkId);
222+
addr.setState(IpAddress.State.Allocating);
223+
224+
if (vlanUse == VlanType.DirectAttached) {
225+
addr.setState(IpAddress.State.Allocated);
226+
}
218227

219228
if (!_ipAddressDao.update(addr.getAddress(), addr)) {
220229
throw new CloudRuntimeException("Found address to allocate but unable to update: " + addr);
@@ -445,6 +454,7 @@ public boolean applyIpAssociations(Network network, boolean continueOnError) thr
445454
for (IPAddressVO addr : userIps) {
446455
if (addr.getState() == IpAddress.State.Allocating) {
447456
addr.setState(IpAddress.State.Allocated);
457+
addr.setAssociatedNetworkId(network.getId());
448458
_ipAddressDao.update(addr.getAddress(), addr);
449459
} else if (addr.getState() == IpAddress.State.Releasing) {
450460
_ipAddressDao.unassignIpAddress(addr.getAddress());
@@ -531,7 +541,6 @@ public IpAddress associateIP(AssociateIPAddrCmd cmd) throws ResourceAllocationEx
531541
_accountMgr.incrementResourceCount(ownerId, ResourceType.public_ip);
532542

533543
String ipAddress = ip.getAddress();
534-
535544
event.setParameters("address=" + ipAddress + "\nsourceNat=" + false + "\ndcId=" + zoneId);
536545
event.setDescription("Assigned a public IP address: " + ipAddress);
537546
_eventDao.persist(event);
@@ -714,6 +723,7 @@ public boolean configure(final String name, final Map<String, Object> params) th
714723
AssignIpAddressSearch.and("allocated", AssignIpAddressSearch.entity().getAllocatedTime(), Op.NULL);
715724
AssignIpAddressSearch.join("vlan", vlanSearch, vlanSearch.entity().getId(), AssignIpAddressSearch.entity().getVlanId(), JoinType.INNER);
716725
vlanSearch.and("type", vlanSearch.entity().getVlanType(), Op.EQ);
726+
vlanSearch.and("networkId", vlanSearch.entity().getNetworkId(), Op.EQ);
717727
AssignIpAddressSearch.done();
718728

719729
IpAddressSearch = _ipAddressDao.createSearchBuilder();
@@ -773,17 +783,21 @@ public List<NetworkVO> setupNetwork(Account owner, NetworkOfferingVO offering, N
773783
throw new ConcurrentOperationException("Unable to acquire lock on " + owner);
774784
}
775785
try {
776-
List<NetworkVO> configs = _networksDao.listBy(owner.getId(), offering.getId(), plan.getDataCenterId());
786+
777787
if (predefined == null || (predefined.getBroadcastUri() == null && predefined.getBroadcastDomainType() != BroadcastDomainType.Vlan)) {
788+
List<NetworkVO> configs = _networksDao.listBy(owner.getId(), offering.getId(), plan.getDataCenterId());
778789
if (configs.size() > 0) {
779790
if (s_logger.isDebugEnabled()) {
780791
s_logger.debug("Found existing network configuration for offering " + offering + ": " + configs.get(0));
781792
}
782793
return configs;
783794
}
795+
} else if (predefined != null && predefined.getBroadcastUri() != null) {
796+
//don't allow duplicated vlans in the same zone
797+
List<NetworkVO> configs = _networksDao.listBy(owner.getId(), offering.getId(), plan.getDataCenterId());
784798
}
785799

786-
configs = new ArrayList<NetworkVO>();
800+
List<NetworkVO> configs = new ArrayList<NetworkVO>();
787801

788802
long related = -1;
789803

@@ -1047,16 +1061,17 @@ public void prepare(VirtualMachineProfile<? extends VMInstanceVO> vmProfile, Dep
10471061
nic.setNetmask(profile.getNetmask());
10481062
nic.setGateway(profile.getGateway());
10491063
nic.setAddressFormat(profile.getFormat());
1050-
_nicDao.update(nic.getId(), nic);
1051-
for (NetworkElement element : _networkElements) {
1052-
if (s_logger.isDebugEnabled()) {
1053-
s_logger.debug("Asking " + element.getName() + " to prepare for " + nic);
1054-
}
1055-
element.prepare(config, profile, vmProfile, dest, context);
1056-
}
1064+
_nicDao.update(nic.getId(), nic);
10571065
} else {
10581066
profile = new NicProfile(nic, config, nic.getBroadcastUri(), nic.getIsolationUri());
10591067
}
1068+
1069+
for (NetworkElement element : _networkElements) {
1070+
if (s_logger.isDebugEnabled()) {
1071+
s_logger.debug("Asking " + element.getName() + " to prepare for " + nic);
1072+
}
1073+
element.prepare(config, profile, vmProfile, dest, context);
1074+
}
10601075

10611076
vmProfile.addNic(profile);
10621077
}
@@ -1587,6 +1602,11 @@ public Network createNetwork(CreateNetworkCmd cmd) throws InvalidParameterValueE
15871602
Boolean isShared = cmd.getIsShared();
15881603
Account owner = null;
15891604

1605+
//if end ip is not specified, default it to startIp
1606+
if (endIP == null && startIP != null) {
1607+
endIP = startIP;
1608+
}
1609+
15901610
//Check if network offering exists
15911611
NetworkOfferingVO networkOffering = _networkOfferingDao.findById(networkOfferingId);
15921612
if (networkOffering == null || networkOffering.isSystemOnly()) {
@@ -1617,6 +1637,15 @@ public Network createNetwork(CreateNetworkCmd cmd) throws InvalidParameterValueE
16171637
owner = ctxAccount;
16181638
}
16191639

1640+
//Don't allow to create network with vlan that already exists in the system
1641+
if (networkOffering.getGuestIpType() == GuestIpType.Direct && vlanId != null) {
1642+
String uri ="vlan://" + vlanId;
1643+
List<NetworkVO> networks = _networksDao.listBy(zoneId, uri);
1644+
if ((networks != null && !networks.isEmpty())) {
1645+
throw new InvalidParameterValueException("Network with vlan " + vlanId + " already exists in zone " + zoneId);
1646+
}
1647+
}
1648+
16201649
//if VlanId is Direct untagged, verify if there is already network of this type in the zone
16211650
if (networkOffering.getGuestIpType() == GuestIpType.DirectPodBased && vlanId != null && vlanId.equalsIgnoreCase(Vlan.UNTAGGED)) {
16221651
SearchBuilder<NetworkVO> sb = _networksDao.createSearchBuilder();

‎server/src/com/cloud/network/dao/NetworkDao.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,6 @@ public interface NetworkDao extends GenericDao<NetworkVO, Long> {
4444
String getNextAvailableMacAddress(long networkConfigId);
4545

4646
List<NetworkVO> listBy(long accountId, long networkId);
47+
48+
List<NetworkVO> listBy(long zoneId, String broadcastUri);
4749
}

‎server/src/com/cloud/network/dao/NetworkDaoImpl.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public class NetworkDaoImpl extends GenericDaoBase<NetworkVO, Long> implements N
5050
final SearchBuilder<NetworkVO> AccountSearch;
5151
final SearchBuilder<NetworkVO> RelatedConfigSearch;
5252
final SearchBuilder<NetworkVO> AccountNetworkSearch;
53+
final SearchBuilder<NetworkVO> ZoneBroadcastUriSearch;
5354

5455
NetworkAccountDaoImpl _accountsDao = new NetworkAccountDaoImpl();
5556
final TableGenerator _tgMacAddress;
@@ -93,6 +94,12 @@ protected NetworkDaoImpl() {
9394
AccountNetworkSearch.join("networkSearch", mapJoin, AccountNetworkSearch.entity().getId(), mapJoin.entity().getNetworkId(), JoinBuilder.JoinType.INNER);
9495
AccountNetworkSearch.done();
9596

97+
98+
ZoneBroadcastUriSearch = createSearchBuilder();
99+
ZoneBroadcastUriSearch.and("dataCenterId", ZoneBroadcastUriSearch.entity().getDataCenterId(), Op.EQ);
100+
ZoneBroadcastUriSearch.and("broadcastUri", ZoneBroadcastUriSearch.entity().getBroadcastUri(), Op.EQ);
101+
ZoneBroadcastUriSearch.done();
102+
96103
_tgMacAddress = _tgs.get("macAddress");
97104
}
98105

@@ -200,4 +207,11 @@ public List<NetworkVO> listBy(long accountId, long networkId) {
200207
sc.setJoinParameters("networkSearch", "accountId", accountId);
201208
return listBy(sc);
202209
}
210+
211+
public List<NetworkVO> listBy(long zoneId, String broadcastUri) {
212+
SearchCriteria<NetworkVO> sc = ZoneBroadcastUriSearch.create();
213+
sc.setParameters("dataCenterId", zoneId);
214+
sc.setParameters("broadcastUri", broadcastUri);
215+
return search(sc, null);
216+
}
203217
}
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/**
2+
*
3+
*/
4+
package com.cloud.network.guru;
5+
6+
import java.util.Random;
7+
8+
import javax.ejb.Local;
9+
10+
import org.apache.log4j.Logger;
11+
12+
import com.cloud.dc.DataCenter;
13+
import com.cloud.dc.DataCenterVO;
14+
import com.cloud.dc.Vlan.VlanType;
15+
import com.cloud.dc.dao.DataCenterDao;
16+
import com.cloud.dc.dao.VlanDao;
17+
import com.cloud.deploy.DeployDestination;
18+
import com.cloud.deploy.DeploymentPlan;
19+
import com.cloud.exception.ConcurrentOperationException;
20+
import com.cloud.exception.InsufficientAddressCapacityException;
21+
import com.cloud.exception.InsufficientVirtualNetworkCapcityException;
22+
import com.cloud.exception.InvalidParameterValueException;
23+
import com.cloud.network.Network;
24+
import com.cloud.network.NetworkManager;
25+
import com.cloud.network.NetworkVO;
26+
import com.cloud.network.Network.State;
27+
import com.cloud.network.Networks.AddressFormat;
28+
import com.cloud.network.Networks.BroadcastDomainType;
29+
import com.cloud.network.Networks.IsolationType;
30+
import com.cloud.network.Networks.Mode;
31+
import com.cloud.network.Networks.TrafficType;
32+
import com.cloud.network.addr.PublicIp;
33+
import com.cloud.network.dao.IPAddressDao;
34+
import com.cloud.offering.NetworkOffering;
35+
import com.cloud.offering.NetworkOffering.GuestIpType;
36+
import com.cloud.resource.Resource.ReservationStrategy;
37+
import com.cloud.user.Account;
38+
import com.cloud.utils.component.AdapterBase;
39+
import com.cloud.utils.component.Inject;
40+
import com.cloud.utils.net.NetUtils;
41+
import com.cloud.vm.NicProfile;
42+
import com.cloud.vm.ReservationContext;
43+
import com.cloud.vm.VirtualMachine;
44+
import com.cloud.vm.VirtualMachineProfile;
45+
46+
@Local(value={NetworkGuru.class})
47+
public class DirectNetworkGuru extends AdapterBase implements NetworkGuru {
48+
private static final Logger s_logger = Logger.getLogger(DirectNetworkGuru.class);
49+
50+
@Inject DataCenterDao _dcDao;
51+
@Inject VlanDao _vlanDao;
52+
@Inject NetworkManager _networkMgr;
53+
@Inject IPAddressDao _ipAddressDao;
54+
Random _rand = new Random(System.currentTimeMillis());
55+
56+
@Override
57+
public Network design(NetworkOffering offering, DeploymentPlan plan, Network userSpecified, Account owner) {
58+
//Change traffic type to Public after Alex's fix
59+
if (!(offering.getTrafficType() == TrafficType.Guest && offering.getGuestIpType() == GuestIpType.Direct)) {
60+
s_logger.trace("We only take care of public direct network, so this is no ours");
61+
return null;
62+
}
63+
64+
NetworkVO config = new NetworkVO(offering.getTrafficType(), offering.getGuestIpType(), Mode.Dhcp, BroadcastDomainType.Vlan, offering.getId(), plan.getDataCenterId());
65+
DataCenterVO dc = _dcDao.findById(plan.getDataCenterId());
66+
67+
if (userSpecified != null) {
68+
if ((userSpecified.getCidr() == null && userSpecified.getGateway() != null) ||
69+
(userSpecified.getCidr() != null && userSpecified.getGateway() == null)) {
70+
throw new InvalidParameterValueException("cidr and gateway must be specified together.");
71+
}
72+
73+
if (userSpecified.getCidr() != null) {
74+
config.setCidr(userSpecified.getCidr());
75+
config.setGateway(userSpecified.getGateway());
76+
}
77+
78+
if (userSpecified.getBroadcastUri() != null) {
79+
config.setBroadcastUri(userSpecified.getBroadcastUri());
80+
config.setState(State.Setup);
81+
}
82+
}
83+
84+
config.setDns1(dc.getDns1());
85+
config.setDns2(dc.getDns2());
86+
87+
return config;
88+
}
89+
90+
protected DirectNetworkGuru() {
91+
super();
92+
}
93+
94+
protected void getIp(NicProfile nic, DataCenter dc, VirtualMachineProfile<? extends VirtualMachine> vm, Network network) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException {
95+
if (nic.getIp4Address() == null) {
96+
PublicIp ip = _networkMgr.fetchNewPublicIp(dc.getId(), VlanType.DirectAttached, vm.getOwner(), network.getId(), false);
97+
nic.setIp4Address(ip.getAddress());
98+
nic.setGateway(ip.getGateway());
99+
nic.setNetmask(ip.getNetmask());
100+
nic.setIsolationUri(IsolationType.Vlan.toUri(ip.getVlanTag()));
101+
nic.setBroadcastType(BroadcastDomainType.Vlan);
102+
nic.setBroadcastUri(BroadcastDomainType.Vlan.toUri(ip.getVlanTag()));
103+
nic.setFormat(AddressFormat.Ip4);
104+
nic.setReservationId(ip.getVlanTag());
105+
nic.setMacAddress(NetUtils.long2Mac(ip.getMacAddress() | 0x060000000000l | (((long)_rand.nextInt(32768) << 25) & 0x00fffe000000l)));
106+
}
107+
nic.setDns1(dc.getDns1());
108+
nic.setDns2(dc.getDns2());
109+
}
110+
111+
@Override
112+
public NicProfile allocate(Network network, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) throws InsufficientVirtualNetworkCapcityException,
113+
InsufficientAddressCapacityException, ConcurrentOperationException {
114+
115+
if (nic == null) {
116+
nic = new NicProfile(ReservationStrategy.Create, null, null, null, null);
117+
} else if (nic.getIp4Address() == null) {
118+
nic.setStrategy(ReservationStrategy.Start);
119+
} else {
120+
nic.setStrategy(ReservationStrategy.Create);
121+
}
122+
123+
DataCenter dc = _dcDao.findById(network.getDataCenterId());
124+
getIp(nic, dc, vm, network);
125+
126+
return nic;
127+
}
128+
129+
@Override
130+
public void reserve(NicProfile nic, Network network, VirtualMachineProfile<? extends VirtualMachine> vm, DeployDestination dest, ReservationContext context) throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException, ConcurrentOperationException {
131+
if (nic.getIp4Address() == null) {
132+
getIp(nic, dest.getDataCenter(), vm, network);
133+
}
134+
}
135+
136+
@Override
137+
public boolean release(NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm, String reservationId) {
138+
return true;
139+
}
140+
141+
@Override
142+
public Network implement(Network network, NetworkOffering offering, DeployDestination destination, ReservationContext context) {
143+
return network;
144+
}
145+
146+
@Override
147+
public void deallocate(Network network, NicProfile nic, VirtualMachineProfile<? extends VirtualMachine> vm) {
148+
}
149+
150+
@Override
151+
public void destroy(Network network, NetworkOffering offering) {
152+
}
153+
154+
@Override
155+
public boolean trash(Network network, NetworkOffering offering, Account owner) {
156+
return true;
157+
}
158+
}

‎server/src/com/cloud/network/guru/GuestNetworkGuru.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ protected GuestNetworkGuru() {
7171

7272
@Override
7373
public Network design(NetworkOffering offering, DeploymentPlan plan, Network userSpecified, Account owner) {
74-
if (offering.getTrafficType() != TrafficType.Guest) {
74+
if (offering.getTrafficType() != TrafficType.Guest || offering.getGuestIpType() != GuestIpType.Virtual) {
7575
return null;
7676
}
7777

0 commit comments

Comments
 (0)