Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3150,7 +3150,7 @@ public NicProfile createNicForVm(Network network, NicProfile requested, Reservat

//1) allocate nic (if needed) Always allocate if it is a user vm
if (nic == null || (vmProfile.getType() == VirtualMachine.Type.User)) {
int deviceId = _nicDao.countNics(vm.getId());
int deviceId = _nicDao.getFreeDeviceId(vm.getId());

nic = allocateNic(requested, network, false, deviceId, vmProfile).first();

Expand Down
2 changes: 1 addition & 1 deletion engine/schema/src/com/cloud/vm/dao/NicDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public interface NicDao extends GenericDao<NicVO, Long> {

String getIpAddress(long networkId, long instanceId);

int countNics(long instanceId);
int getFreeDeviceId(long instanceId);

NicVO findByNetworkIdInstanceIdAndBroadcastUri(long networkId, long instanceId, String broadcastUri);

Expand Down
31 changes: 20 additions & 11 deletions engine/schema/src/com/cloud/vm/dao/NicDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import org.springframework.stereotype.Component;

import com.cloud.utils.db.Filter;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.GenericSearchBuilder;
import com.cloud.utils.db.JoinBuilder;
Expand All @@ -44,7 +45,7 @@ public class NicDaoImpl extends GenericDaoBase<NicVO, Long> implements NicDao {
private SearchBuilder<NicVO> AllFieldsSearch;
private GenericSearchBuilder<NicVO, String> IpSearch;
private SearchBuilder<NicVO> NonReleasedSearch;
private GenericSearchBuilder<NicVO, Integer> CountBy;
private GenericSearchBuilder<NicVO, Integer> deviceIdSearch;
private GenericSearchBuilder<NicVO, Integer> CountByForStartingVms;

@Inject
Expand Down Expand Up @@ -81,11 +82,10 @@ protected void init() {
NonReleasedSearch.and("state", NonReleasedSearch.entity().getState(), Op.NOTIN);
NonReleasedSearch.done();

CountBy = createSearchBuilder(Integer.class);
CountBy.select(null, Func.COUNT, CountBy.entity().getId());
CountBy.and("vmId", CountBy.entity().getInstanceId(), Op.EQ);
CountBy.and("removed", CountBy.entity().getRemoved(), Op.NULL);
CountBy.done();
deviceIdSearch = createSearchBuilder(Integer.class);
deviceIdSearch.select(null, Func.DISTINCT, deviceIdSearch.entity().getDeviceId());
deviceIdSearch.and("instance", deviceIdSearch.entity().getInstanceId(), Op.EQ);
deviceIdSearch.done();

CountByForStartingVms = createSearchBuilder(Integer.class);
CountByForStartingVms.select(null, Func.COUNT, CountByForStartingVms.entity().getId());
Expand Down Expand Up @@ -222,11 +222,20 @@ public String getIpAddress(long networkId, long instanceId) {
}

@Override
public int countNics(long instanceId) {
SearchCriteria<Integer> sc = CountBy.create();
sc.setParameters("vmId", instanceId);
List<Integer> results = customSearch(sc, null);
return results.get(0);
public int getFreeDeviceId(long instanceId) {
Filter searchFilter = new Filter(NicVO.class, "deviceId", true, null, null);
SearchCriteria<Integer> sc = deviceIdSearch.create();
sc.setParameters("instance", instanceId);
List<Integer> deviceIds = customSearch(sc, searchFilter);

int freeDeviceId = 0;
for (int deviceId : deviceIds) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ustcweizhou Could you elaborate on what the logic does?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@borisroman
we get the list of deviceId order by asc at first, eg, 0,1,2,4
then we check the list, for index i, if (deviceIds[i] != i), then i is the first free ip.
in my example, the first free device_id is 3 (because deviceIds[3] = 4, not 3)
then we assign 3 as the device_id of new nic.

if (deviceId > freeDeviceId)
break;
freeDeviceId ++;
}

return freeDeviceId;
}

@Override
Expand Down