Skip to content

Commit

Permalink
<fix>[network]: fix UsedIpVO order by ipInBinary to support ipv6
Browse files Browse the repository at this point in the history
Resolves: ZSTAC-68984

Change-Id: I6868776c409e42772ca04bc38624ba432950f2fe
  • Loading branch information
bustezero committed Aug 30, 2024
1 parent 75c4d5e commit 0f5fa4d
Show file tree
Hide file tree
Showing 15 changed files with 158 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ protected void scripts() {
vo.setIpVersion(IPv6Constants.IPv6);
vo.setVmNicUuid(nic.getUuid());
vo.setL3NetworkUuid(nic.getL3NetworkUuid());
vo.setIpInBinary(NetworkUtils.ipStringToBytes(vo.getIp()));
vo.setIpRangeUuid(new StaticIpOperator().getIpRangeUuid(nic.getL3NetworkUuid(), vo.getIp()));
nic.setUsedIpUuid(vo.getUuid());
nicVO.setUsedIpUuid(vo.getUuid());
Expand All @@ -178,6 +179,7 @@ protected void scripts() {
vo.setVmNicUuid(nic.getUuid());
vo.setL3NetworkUuid(nic.getL3NetworkUuid());
vo.setIpInLong(NetworkUtils.ipv4StringToLong(vo.getIp()));
vo.setIpInBinary(NetworkUtils.ipStringToBytes(vo.getIp()));
vo.setIpRangeUuid(new StaticIpOperator().getIpRangeUuid(nic.getL3NetworkUuid(), vo.getIp()));
nic.setUsedIpUuid(vo.getUuid());
nicVO.setUsedIpUuid(vo.getUuid());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3583,6 +3583,7 @@ public void run(FlowTrigger trigger, Map data) {
vo.setVmNicUuid(nicVO.getUuid());
vo.setL3NetworkUuid(nicVO.getL3NetworkUuid());
vo.setIpRangeUuid(new StaticIpOperator().getIpRangeUuid(vo.getL3NetworkUuid(), vo.getIp()));
vo.setIpInBinary(NetworkUtils.ipStringToBytes(vo.getIp()));
nicVO.setUsedIpUuid(vo.getUuid());
nicVO.setIp(vo.getIp());
nicVO.setNetmask(vo.getNetmask());
Expand All @@ -3595,14 +3596,14 @@ public void run(FlowTrigger trigger, Map data) {
UsedIpVO vo = new UsedIpVO();
vo.setUuid(Platform.getUuid());
if (NetworkUtils.isIpv4Address(msg.getIp())) {
vo.setIpInLong(NetworkUtils.ipv4StringToLong(msg.getIp()));
vo.setIp(msg.getIp());
vo.setNetmask(msg.getNetmask());
vo.setGateway(msg.getGateway().isEmpty() ? "" : msg.getGateway());
vo.setIpVersion(IPv6Constants.IPv4);
vo.setVmNicUuid(nicVO.getUuid());
vo.setL3NetworkUuid(nicVO.getL3NetworkUuid());
vo.setIpInLong(NetworkUtils.ipv4StringToLong(vo.getIp()));
vo.setIpInBinary(NetworkUtils.ipStringToBytes(vo.getIp()));
vo.setIpRangeUuid(new StaticIpOperator().getIpRangeUuid(vo.getL3NetworkUuid(), vo.getIp()));
nicVO.setUsedIpUuid(vo.getUuid());
nicVO.setIp(vo.getIp());
Expand All @@ -3617,6 +3618,7 @@ public void run(FlowTrigger trigger, Map data) {
vo.setIpVersion(IPv6Constants.IPv6);
vo.setVmNicUuid(nicVO.getUuid());
vo.setL3NetworkUuid(nicVO.getL3NetworkUuid());
vo.setIpInBinary(NetworkUtils.ipStringToBytes(vo.getIp()));
nicVO.setUsedIpUuid(vo.getUuid());
nicVO.setIp(vo.getIp());
nicVO.setNetmask(vo.getNetmask());
Expand Down Expand Up @@ -6278,6 +6280,7 @@ public void run(FlowTrigger trigger, Map data) {
vo.setVmNicUuid(msg.getVmNicUuid());
vo.setL3NetworkUuid(msg.getDestL3NetworkUuid());
vo.setIpRangeUuid(new StaticIpOperator().getIpRangeUuid(vo.getL3NetworkUuid(), vo.getIp()));
vo.setIpInBinary(NetworkUtils.ipStringToBytes(vo.getIp()));
nicVO.setUsedIpUuid(vo.getUuid());
nicVO.setIp(nicIpAddressInfo.ipv4Address);
nicVO.setGateway(nicIpAddressInfo.ipv4Gateway);
Expand All @@ -6297,6 +6300,7 @@ public void run(FlowTrigger trigger, Map data) {
vo.setVmNicUuid(msg.getVmNicUuid());
vo.setL3NetworkUuid(msg.getDestL3NetworkUuid());
vo.setIpInLong(NetworkUtils.ipv4StringToLong(vo.getIp()));
vo.setIpInBinary(NetworkUtils.ipStringToBytes(vo.getIp()));
vo.setIpRangeUuid(new StaticIpOperator().getIpRangeUuid(vo.getL3NetworkUuid(), vo.getIp()));
nicVO.setUsedIpUuid(vo.getUuid());
nicVO.setIp(nicIpAddressInfo.ipv4Address);
Expand Down
67 changes: 67 additions & 0 deletions conf/db/upgrade/V5.2.0__schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,70 @@ update EventSubscriptionVO set name = 'NIC IP Configured in VM has been Occupied
CALL ADD_COLUMN('MdevDeviceSpecVO', 'vendor', 'VARCHAR(128)', 1, NULL);

CALL ADD_COLUMN('BareMetal2ChassisGpuDeviceVO', 'isDriverLoaded', 'TINYINT(1)', 0, 0);

DELIMITER $$
DROP FUNCTION IF EXISTS `INET6_ATON` $$
CREATE FUNCTION `INET6_ATON`(
ip VARCHAR(128)
) RETURNS BINARY(16)
BEGIN
DECLARE binary_ip BINARY(16);
DECLARE hextet VARCHAR(5);
DECLARE i INT DEFAULT 1;
DECLARE segment_position INT DEFAULT 1;
DECLARE segment_count INT;
DECLARE expanded_ip VARCHAR(45);
IF INSTR(ip, '.') > 0 THEN
SET binary_ip = CONCAT(REPEAT(UNHEX('00'), 10), UNHEX('FFFF'), UNHEX(LPAD(HEX(INET_ATON(ip)), 8, '0')));
ELSE
IF INSTR(ip, '::') > 0 THEN
SET segment_count = LENGTH(ip) - LENGTH(REPLACE(ip, ':', '')) + 1;
SET expanded_ip = REPLACE(ip, '::', CONCAT(':', REPEAT(':0000', 8 - segment_count), ':'));
IF LEFT(expanded_ip, 1) = ':' THEN
SET expanded_ip = SUBSTRING(expanded_ip, 2);
END IF;
IF RIGHT(expanded_ip, 1) = ':' THEN
SET expanded_ip = SUBSTRING(expanded_ip, 1, LENGTH(expanded_ip) - 1);
END IF;
ELSE
SET expanded_ip = ip;
END IF;
SET binary_ip = 0x00000000000000000000000000000000;
WHILE i <= 8 DO
SET hextet = SUBSTRING_INDEX(SUBSTRING_INDEX(expanded_ip, ':', i), ':', -1);
IF LENGTH(hextet) > 0 THEN
SET binary_ip = INSERT(binary_ip, segment_position, 4, UNHEX(LPAD(hextet, 4, '0')));
END IF;
SET segment_position = segment_position + 2;
SET i = i + 1;
END WHILE;
END IF;
RETURN binary_ip;
END$$
DELIMITER ;
DELIMITER $$

DELIMITER $$
CREATE PROCEDURE upgradeIpInLongColumn()
BEGIN
DECLARE columnExists BOOLEAN DEFAULT FALSE;

SELECT COUNT(*) INTO columnExists
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'UsedIpVO'
AND COLUMN_NAME = 'ipInBinary'
AND TABLE_SCHEMA = 'zstack';

IF columnExists = FALSE THEN
ALTER TABLE `zstack`.`UsedIpVO` ADD COLUMN `ipInBinary` VARBINARY(16) NOT NULL AFTER `ipInLong`;
END IF;

UPDATE `zstack`.`UsedIpVO`
SET `ipInBinary` = CASE
WHEN `ipInLong` != 0 THEN UNHEX(LPAD(HEX(`ipInLong`), 8, '0'))
ELSE INET6_ATON(`ip`)
END;
SELECT CURTIME();
END $$
DELIMITER ;
CALL upgradeIpInLongColumn();
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class UsedIpInventory implements Serializable {
@APINoSee
private String metaData;
private long ipInLong;
private byte[] ipInBinary;
private String vmNicUuid;
private Timestamp createDate;
private Timestamp lastOpDate;
Expand All @@ -46,6 +47,7 @@ public static UsedIpInventory valueOf(UsedIpVO vo) {
inv.setIpVersion(vo.getIpVersion());
inv.setIp(vo.getIp());
inv.setIpInLong(vo.getIpInLong());
inv.setIpInBinary(vo.getIpInBinary());
inv.setIpRangeUuid(vo.getIpRangeUuid());
inv.setL3NetworkUuid(vo.getL3NetworkUuid());
inv.setGateway(vo.getGateway());
Expand Down Expand Up @@ -98,6 +100,13 @@ public void setIpInLong(long ipInLong) {
this.ipInLong = ipInLong;
}

public byte[] getIpInBinary() {
return ipInBinary;
}

public void setIpInBinary(byte[] ipInBinary) {
this.ipInBinary = ipInBinary;
}
public Timestamp getCreateDate() {
return createDate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ doc {
type "long"
since "5.0.0"
}
field {
name "ipInBinary"
desc ""
type "byte[]"
since "5.2.0"
}
field {
name "vmNicUuid"
desc "云主机网卡UUID"
Expand Down
12 changes: 12 additions & 0 deletions header/src/main/java/org/zstack/header/network/l3/UsedIpVO.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ public class UsedIpVO {
@Index
private long ipInLong;

@Column
@Index
private byte[] ipInBinary;

@Column
private String usedFor;

Expand Down Expand Up @@ -111,6 +115,14 @@ public void setIpInLong(long ipInLong) {
this.ipInLong = ipInLong;
}

public byte[] getIpInBinary() {
return ipInBinary;
}

public void setIpInBinary(byte[] ipInBinary) {
this.ipInBinary = ipInBinary;
}

public String getL3NetworkUuid() {
return l3NetworkUuid;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,7 @@ private void handle(APIAddReservedIpRangeMsg msg) {
vo.setNetmask(ipr.getNetmask());
vo.setGateway(ipr.getGateway());
vo.setIpInLong(i);
vo.setIpInBinary(NetworkUtils.ipStringToBytes(vo.getIp()));
vo.setUsedFor(IpAllocatedReason.Reserved.toString());
vo.setMetaData(reservedIpRangeVO.getUuid());

Expand Down Expand Up @@ -507,6 +508,7 @@ private void handle(APIAddReservedIpRangeMsg msg) {
//vo.setVmNicUuid(nic.getUuid());
vo.setIpVersion(ipr.getIpVersion());
vo.setIp(newIp);
vo.setIpInBinary(NetworkUtils.ipStringToBytes(vo.getIp()));
vo.setNetmask(ipr.getNetmask());
vo.setGateway(ipr.getGateway());
vo.setUsedFor(IpAllocatedReason.Reserved.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,7 @@ private UsedIpInventory reserveIpv6(IpRangeVO ipRange, String ip, boolean allowD
vo.setUuid(uuid);
vo.setIpRangeUuid(ipRange.getUuid());
vo.setIp(IPv6NetworkUtils.getIpv6AddressCanonicalString(ip));
vo.setIpInBinary(NetworkUtils.ipStringToBytes(vo.getIp()));
vo.setL3NetworkUuid(ipRange.getL3NetworkUuid());
vo.setNetmask(ipRange.getNetmask());
vo.setGateway(ipRange.getGateway());
Expand All @@ -636,6 +637,7 @@ private UsedIpInventory reserveIpv4(IpRangeVO ipRange, String ip, boolean allowD
try {
UsedIpVO vo = new UsedIpVO(ipRange.getUuid(), ip);
vo.setIpInLong(NetworkUtils.ipv4StringToLong(ip));
vo.setIpInBinary(NetworkUtils.ipStringToBytes(vo.getIp()));
String uuid;
if (allowDuplicatedAddress) {
uuid = Platform.getUuid();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,29 +434,33 @@ private Long countResource(APIGetL3NetworkIpStatisticMsg msg) {
private List<IpStatisticData> ipStatisticAll(APIGetL3NetworkIpStatisticMsg msg, String sortBy) {
/*
select uip.ip, vip.uuid as vipUuid, vip.name as vipName, it.uuid as vmInstanceUuid, it.name as vmInstanceName, it.type, uip.createDate
from (select uuid, ip, IpInLong, createDate, vmNicUuid
from (select uuid, ip, IpInLong, ipInBinary, createDate, vmNicUuid
from UsedIpVO
where l3NetworkUuid = '{uuid}' [and ip like '{ip}']
order by {sortBy} {direction}
order by LENGTH({sortBy}) {direction}, {sortBy} {direction}
limit {limit} offset {start}) uip
left join (select uuid, name, usedIpUuid from VipVO
where l3NetworkUuid = '{l3Uuid}') vip on uip.uuid = vip.usedIpUuid
left join (select uuid, vmInstanceUuid from VmNicVO) nic on uip.vmNicUuid = nic.uuid
left join (select uuid, name, type from VmInstanceVO) it on nic.vmInstanceUuid = it.uuid
order by {sortBy} {direction};
order by LENGTH({sortBy}) {direction}, {sortBy} {direction};
*/
if (SortBy.IP.equals(msg.getSortBy())) {
sortBy = "ipInBinary";
}
Map<String, String> dhcpMap = getExistingDhcpServerIp(msg.getL3NetworkUuid(), IPv6Constants.DUAL_STACK);
Set<String> dhcp = dhcpMap.keySet();
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append("select uip.ip, vip.uuid as vipUuid, vip.name as vipName, it.uuid as vmUuid, it.name as vmName, it.type, uip.createDate ")
.append("from (select uuid, ip, ipInLong, createDate, vmNicUuid from UsedIpVO where l3NetworkUuid = '")
.append("from (select uuid, ip, ipInLong, ipInBinary, createDate, vmNicUuid from UsedIpVO where l3NetworkUuid = '")
.append(msg.getL3NetworkUuid()).append('\'');

if (StringUtils.isNotEmpty(msg.getIp())) {
sqlBuilder.append(" and ip like '").append(msg.getIp()).append('\'');
}

sqlBuilder.append(" order by ").append(sortBy).append(' ').append(msg.getSortDirection()).append(" limit ")
sqlBuilder.append(" order by LENGTH(").append(sortBy).append(") ").append(msg.getSortDirection())
.append(", ").append(sortBy).append(" ").append(msg.getSortDirection()).append(" limit ")
.append(msg.getLimit()).append(" offset ").append(msg.getStart()).append(") uip ")
.append("left join ")
.append("(select uuid, name, usedIpUuid from VipVO ")
Expand All @@ -466,7 +470,8 @@ left join (select uuid, name, type from VmInstanceVO) it on nic.vmInstanceUuid =
.append("(select uuid, vmInstanceUuid from VmNicVO) nic on uip.vmNicUuid = nic.uuid ")
.append("left join ")
.append("(select uuid, name, type from VmInstanceVO) it on it.uuid = nic.vmInstanceUuid ")
.append("order by ").append(sortBy).append(' ').append(msg.getSortDirection());
.append("order by LENGTH(").append(sortBy).append(") ").append(msg.getSortDirection())
.append(", ").append(sortBy).append(" ").append(msg.getSortDirection());

Query q = dbf.getEntityManager().createNativeQuery(sqlBuilder.toString());
List<Object[]> results = q.getResultList();
Expand Down Expand Up @@ -2479,6 +2484,7 @@ public void afterAddIpRange(IpRangeInventory ipr, List<String> systemTags) {
vo.setNetmask(ipr.getNetmask());
vo.setGateway(ipr.getGateway());
vo.setIpInLong(i);
vo.setIpInBinary(NetworkUtils.ipStringToBytes(vo.getIp()));
vo.setUsedFor(IpAllocatedReason.Reserved.toString());
vo.setMetaData(reservedIpRange.getUuid());
usedIpVOS.add(vo);
Expand Down Expand Up @@ -2509,6 +2515,7 @@ public void afterAddIpRange(IpRangeInventory ipr, List<String> systemTags) {
//vo.setVmNicUuid(nic.getUuid());
vo.setIpVersion(ipr.getIpVersion());
vo.setIp(newIp);
vo.setIpInBinary(NetworkUtils.ipStringToBytes(vo.getIp()));
vo.setNetmask(ipr.getNetmask());
vo.setGateway(ipr.getGateway());
vo.setUsedFor(IpAllocatedReason.Reserved.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ private void procReservedIP(APIAddIpRangeByNetworkCidrMsg msg){
servIpInfo.setL3NetworkUuid(ipRangeVO.getL3NetworkUuid());
servIpInfo.setIp(servIp);
servIpInfo.setIpInLong(NetworkUtils.ipv4StringToLong(servIp));
servIpInfo.setIpInBinary(NetworkUtils.ipStringToBytes(servIpInfo.getIp()));
servIpInfo.setGateway(ipRangeVO.getGateway());
servIpInfo.setNetmask(ipRangeVO.getNetmask());
servIpInfo.setIpVersion(IPv6Constants.IPv4);
Expand All @@ -326,6 +327,7 @@ private void procReservedIP(APIAddIpRangeByNetworkCidrMsg msg){
edgeIpInfo.setUuid(Platform.getUuid());
edgeIpInfo.setIp(edgeIp);
edgeIpInfo.setIpInLong(NetworkUtils.ipv4StringToLong(edgeIp));
edgeIpInfo.setIpInBinary(NetworkUtils.ipStringToBytes(edgeIpInfo.getIp()));
dbf.persist(edgeIpInfo);
}

Expand Down
16 changes: 16 additions & 0 deletions rest/src/main/resources/scripts/SdkDataStructureGenerator.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,9 @@ ${output.join("\n")}
}

def isZStackClass(Class clz) {
if (clz.isArray() && clz.getComponentType() == byte.class) {
return false
}
if (clz.getName().startsWith("java.") || int.class == clz || long.class == clz
|| short.class == clz || char.class == clz || boolean.class == clz || float.class == clz
|| double.class == clz) {
Expand Down Expand Up @@ -341,6 +344,19 @@ ${output.join("\n")}
return null
}

// handle byte[] type
if (field.type == byte[].class) {
return """\
public byte[] ${fname};
public void set${StringUtils.capitalize(fname)}(byte[] ${fname}) {
this.${fname} = ${fname};
}
public byte[] get${StringUtils.capitalize(fname)}() {
return this.${fname};
}
"""
}

// java type
if (Collection.class.isAssignableFrom(field.type)) {
Class genericType = FieldUtils.getGenericType(field)
Expand Down
8 changes: 8 additions & 0 deletions sdk/src/main/java/org/zstack/sdk/UsedIpInventory.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ public long getIpInLong() {
return this.ipInLong;
}

public byte[] ipInBinary;
public void setIpInBinary(byte[] ipInBinary) {
this.ipInBinary = ipInBinary;
}
public byte[] getIpInBinary() {
return this.ipInBinary;
}

public java.lang.String vmNicUuid;
public void setVmNicUuid(java.lang.String vmNicUuid) {
this.vmNicUuid = vmNicUuid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ private void takeIp(L3NetworkInventory l3inv, IpRangeInventory ipinv, String...
UsedIpVO vo = new UsedIpVO(ipinv.getUuid(), ip);
vo.setUuid(Platform.getUuid());
vo.setIpInLong(NetworkUtils.ipv4StringToLong(ip));
vo.setIpInBinary(NetworkUtils.ipStringToBytes(vo.getIp()));
vo.setL3NetworkUuid(l3inv.getUuid());
dbf.persist(vo);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ private void takeIp(L3NetworkInventory l3inv, IpRangeInventory ipinv, String sta
UsedIpVO vo = new UsedIpVO(ipinv.getUuid(), ip);
vo.setUuid(Platform.getUuid());
vo.setIpInLong(NetworkUtils.ipv4StringToLong(ip));
vo.setIpInBinary(NetworkUtils.ipStringToBytes(vo.getIp()));
vo.setL3NetworkUuid(l3inv.getUuid());
dbf.persist(vo);
}
Expand Down
Loading

0 comments on commit 0f5fa4d

Please sign in to comment.