forked from apache/cloudstack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathControlNetworkGuru.java
More file actions
executable file
·247 lines (209 loc) · 9.24 KB
/
ControlNetworkGuru.java
File metadata and controls
executable file
·247 lines (209 loc) · 9.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.cloud.network.guru;
import java.util.Map;
import javax.ejb.Local;
import javax.inject.Inject;
import javax.naming.ConfigurationException;
import org.apache.log4j.Logger;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import com.cloud.configuration.Config;
import com.cloud.dc.DataCenter;
import com.cloud.dc.DataCenter.NetworkType;
import com.cloud.dc.DataCenterVO;
import com.cloud.dc.dao.DataCenterDao;
import com.cloud.deploy.DeployDestination;
import com.cloud.deploy.DeploymentPlan;
import com.cloud.exception.InsufficientAddressCapacityException;
import com.cloud.exception.InsufficientVirtualNetworkCapcityException;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.network.Network;
import com.cloud.network.NetworkModel;
import com.cloud.network.NetworkProfile;
import com.cloud.network.Networks.AddressFormat;
import com.cloud.network.Networks.BroadcastDomainType;
import com.cloud.network.Networks.Mode;
import com.cloud.network.Networks.TrafficType;
import com.cloud.network.dao.NetworkVO;
import com.cloud.offering.NetworkOffering;
import com.cloud.user.Account;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.utils.net.NetUtils;
import com.cloud.vm.Nic;
import com.cloud.vm.NicProfile;
import com.cloud.vm.ReservationContext;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachineProfile;
@Local(value = {NetworkGuru.class})
public class ControlNetworkGuru extends PodBasedNetworkGuru implements NetworkGuru {
private static final Logger s_logger = Logger.getLogger(ControlNetworkGuru.class);
@Inject
DataCenterDao _dcDao;
@Inject
ConfigurationDao _configDao;
@Inject
NetworkModel _networkMgr;
String _cidr;
String _gateway;
private static final TrafficType[] TrafficTypes = {TrafficType.Control};
@Override
public boolean isMyTrafficType(TrafficType type) {
for (TrafficType t : TrafficTypes) {
if (t == type) {
return true;
}
}
return false;
}
@Override
public TrafficType[] getSupportedTrafficType() {
return TrafficTypes;
}
protected boolean canHandle(NetworkOffering offering) {
if (offering.isSystemOnly() && isMyTrafficType(offering.getTrafficType())) {
return true;
} else {
s_logger.trace("We only care about System only Control network");
return false;
}
}
@Override
public Network design(NetworkOffering offering, DeploymentPlan plan, Network specifiedConfig, Account owner) {
if (!canHandle(offering)) {
return null;
}
NetworkVO config =
new NetworkVO(offering.getTrafficType(), Mode.Static, BroadcastDomainType.LinkLocal, offering.getId(), Network.State.Setup, plan.getDataCenterId(),
plan.getPhysicalNetworkId());
config.setCidr(_cidr);
config.setGateway(_gateway);
return config;
}
protected ControlNetworkGuru() {
super();
}
@Override
public NicProfile allocate(Network config, NicProfile nic, VirtualMachineProfile vm) throws InsufficientVirtualNetworkCapcityException,
InsufficientAddressCapacityException {
if (vm.getHypervisorType() == HypervisorType.VMware && !isRouterVm(vm)) {
NicProfile nicProf = new NicProfile(Nic.ReservationStrategy.Create, null, null, null, null);
String mac = _networkMgr.getNextAvailableMacAddressInNetwork(config.getId());
nicProf.setMacAddress(mac);
return nicProf;
}
if (nic != null) {
throw new CloudRuntimeException("Does not support nic specification at this time: " + nic);
}
return new NicProfile(Nic.ReservationStrategy.Start, null, null, null, null);
}
@Override
public void deallocate(Network config, NicProfile nic, VirtualMachineProfile vm) {
}
@Override
public void reserve(NicProfile nic, Network config, VirtualMachineProfile vm, DeployDestination dest, ReservationContext context)
throws InsufficientVirtualNetworkCapcityException, InsufficientAddressCapacityException {
assert nic.getTrafficType() == TrafficType.Control;
// we have to get management/private ip for the control nic for vmware/hyperv due ssh issues.
HypervisorType hType = dest.getHost().getHypervisorType();
if (((hType == HypervisorType.VMware) || (hType == HypervisorType.Hyperv)) && isRouterVm(vm)) {
if (dest.getDataCenter().getNetworkType() != NetworkType.Basic) {
super.reserve(nic, config, vm, dest, context);
String mac = _networkMgr.getNextAvailableMacAddressInNetwork(config.getId());
nic.setMacAddress(mac);
return;
} else {
// in basic mode and in VMware case, control network will be shared with guest network
String mac = _networkMgr.getNextAvailableMacAddressInNetwork(config.getId());
nic.setMacAddress(mac);
nic.setIp4Address("0.0.0.0");
nic.setNetmask("0.0.0.0");
nic.setFormat(AddressFormat.Ip4);
nic.setGateway("0.0.0.0");
return;
}
}
String ip = _dcDao.allocateLinkLocalIpAddress(dest.getDataCenter().getId(), dest.getPod().getId(), nic.getId(), context.getReservationId());
if (ip == null) {
throw new InsufficientAddressCapacityException("Insufficient link local address capacity", DataCenter.class, dest.getDataCenter().getId());
}
nic.setIp4Address(ip);
nic.setMacAddress(NetUtils.long2Mac(NetUtils.ip2Long(ip) | (14l << 40)));
nic.setNetmask("255.255.0.0");
nic.setFormat(AddressFormat.Ip4);
nic.setGateway(NetUtils.getLinkLocalGateway());
}
@Override
public boolean release(NicProfile nic, VirtualMachineProfile vm, String reservationId) {
assert nic.getTrafficType() == TrafficType.Control;
HypervisorType hType = vm.getHypervisorType();
if ( ( (hType == HypervisorType.VMware) || (hType == HypervisorType.Hyperv) )&& isRouterVm(vm)) {
long dcId = vm.getVirtualMachine().getDataCenterId();
DataCenterVO dcVo = _dcDao.findById(dcId);
if (dcVo.getNetworkType() != NetworkType.Basic) {
super.release(nic, vm, reservationId);
if (s_logger.isDebugEnabled()) {
s_logger.debug("Released nic: " + nic);
}
return true;
} else {
nic.deallocate();
if (s_logger.isDebugEnabled()) {
s_logger.debug("Released nic: " + nic);
}
return true;
}
}
_dcDao.releaseLinkLocalIpAddress(nic.getId(), reservationId);
nic.deallocate();
if (s_logger.isDebugEnabled()) {
s_logger.debug("Released nic: " + nic);
}
return true;
}
protected boolean isRouterVm(VirtualMachineProfile vm) {
return vm.getType() == VirtualMachine.Type.DomainRouter || vm.getType() == VirtualMachine.Type.InternalLoadBalancerVm;
}
@Override
public Network implement(Network config, NetworkOffering offering, DeployDestination destination, ReservationContext context)
throws InsufficientVirtualNetworkCapcityException {
assert config.getTrafficType() == TrafficType.Control : "Why are you sending this configuration to me " + config;
return config;
}
@Override
public void shutdown(NetworkProfile config, NetworkOffering offering) {
assert false : "Destroying a link local...Either you're out of your mind or something has changed.";
}
@Override
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
super.configure(name, params);
Map<String, String> dbParams = _configDao.getConfiguration(params);
_cidr = dbParams.get(Config.ControlCidr.toString());
if (_cidr == null) {
_cidr = "169.254.0.0/16";
}
_gateway = dbParams.get(Config.ControlGateway.toString());
if (_gateway == null) {
_gateway = NetUtils.getLinkLocalGateway();
}
s_logger.info("Control network setup: cidr=" + _cidr + "; gateway = " + _gateway);
return true;
}
@Override
public boolean trash(Network config, NetworkOffering offering) {
return true;
}
}