This commit is contained in:
Administrator
2025-04-24 10:16:38 +08:00
parent 6de65562be
commit 7267aec1fd
108 changed files with 7621 additions and 1890 deletions
@@ -32,26 +32,41 @@ import java.util.concurrent.locks.ReentrantLock;
import org.kne.cloud.network.ipv6.IPv6NetworkLink;
import org.kne.cloud.network.ipv6.Inet6AddressGroup;
import org.kne.cloud.network.ipv6.Neighbor;
import org.kne.cloud.network.klalb.KLALBVirtualRawSocket;
import org.kne.cloud.network.monitor.DelayMonitorData;
import org.kne.cloud.network.monitor.MonitorData;
import org.kne.cloud.network.monitor.SpeedAndTrafficAndDelayMonitorDataImpl;
import org.kne.cloud.network.monitor.SpeedAndTrafficMonitorData;
import org.kne.cloud.network.monitor.SpeedAndTrafficMonitorDataImpl;
import org.kne.cloud.network.scanner.InetAddressRange;
import org.kne.cloud.network.te.BandwidthDistributer;
import com.google.gson.internal.Pair;
public class KLALBRoutingProtocol extends Thread{
private static final boolean debug = false;
private RouterInfo selfRouterInfo;
private Map<Inet6Address, RouterInfo> netmap=new ConcurrentHashMap<>();
private volatile Map<Inet6Address,Long>addresses;
private volatile Map<Inet6Address, Set<LinkDirection>> paths;
private volatile Map<Inet6Address, List<LinkDirection>> paths;
public static final int DEFAULT_PORT=1001;
public static final int DEFAULT_PROTOCOL_NUMBER=252;
public static final long HOTSOPT_TIMEOUT = 1000000000;
public static final int HOTSOPT_REPORT_INTERVAL = 100000000;
@@ -63,60 +78,59 @@ public class KLALBRoutingProtocol extends Thread{
this.router=router;
}
private ReentrantLock sendlock=new ReentrantLock();
//private ReentrantLock sendlock=new ReentrantLock();
private long floodTimer=System.nanoTime();
private long floodTimer2=System.nanoTime();
private long requestTimer=System.nanoTime();
@Override
public void run() {
Thread.currentThread().setName("KLALB路由协议接收线程");
getSelfRouterInfo();
DatagramSocket ds = null;
KLALBVirtualRawSocket ds = null;
try{
ds=new DatagramSocket(new InetSocketAddress(router.getLocator().getAddress(), DEFAULT_PORT) );
DatagramSocket ds2=ds;
ds=new KLALBVirtualRawSocket(router,router.getLocator().getAddress(), DEFAULT_PROTOCOL_NUMBER);
KLALBVirtualRawSocket ds2=ds;
new Thread(()->{
Thread.currentThread().setName("KLALB路由协议接收线程");
while(true) {
try {
/*List<NetworkLink>links=router.getLinkTabel();
Object[] nls=links.toArray();
byte[]to=createLinkStatePacket(nls);
DatagramPacket dgp=new DatagramPacket(to,to.length);
for(int i=0;i<nls.length;i++) {
NetworkLink nl=(NetworkLink) nls[i];
if(!nl.isLoopBack()) {
Set<Inet6Address>neis=nl.discoverNeighbors();
for (Iterator<Inet6Address> iteratorx = neis.iterator(); iteratorx.hasNext();) {
Inet6Address addresses = (Inet6Address) iteratorx.next();
dgp.setAddress(addresses);
dgp.setPort(DEFAULT_PORT);
ds2.send(dgp);
}
}
}*/
RouterInfo slf= getSelfRouterInfo();
RouterInfo oslf=netmap.put(slf.getLocator().getAddress(), slf);
selfRouterInfo = getSelfRouterInfo();
RouterInfo oslf=netmap.put(selfRouterInfo.getLocator().getAddress(), selfRouterInfo);
noticeUpdate();
if(oslf==null||(!slf.equals(oslf))) {
//System.out.println("change:"+oslf+"\n"+slf);
/*if(oslf==null||(!selfRouterInfo.equals(oslf))) {
long cur=System.nanoTime();
if(cur-floodTimer2>5000000000L) {
//System.out.println("update10");
floodTimer2=cur;
floodPacket(ds2, null, new RouterInfoPacket(slf,true));
floodPacket(ds2, null, new RouterInfoPacket(selfRouterInfo,true));
}
}else {
long cur=System.nanoTime();
if(cur-floodTimer>60000000000L) {
//System.out.println("update60");
floodTimer=cur;
floodPacket(ds2, null, new RouterInfoPacket(slf,true));
floodPacket(ds2, null, new RouterInfoPacket(selfRouterInfo,true));
}
}*/
long cur=System.nanoTime();
if(cur-floodTimer>60000000000L) {
if(debug)
System.out.println("update60");
floodTimer=cur;
floodPacket(ds2, null, new RouterInfoPacket(selfRouterInfo,true,-1));
}
Set<Inet6Address> requestSet=new HashSet<>();
if(cur-floodTimer2>1000000000L) {
if(debug)
System.out.println("update1");
floodTimer2=cur;
floodPacket(ds2, null, new RouterInfoPacket(selfRouterInfo,true,router.getASN()));
}
//Set<Inet6Address> requestSet=new HashSet<>();
for (Iterator<Entry<Inet6Address, RouterInfo>> iterator = netmap.entrySet().iterator(); iterator.hasNext();) {
Entry<Inet6Address, RouterInfo> type = (Entry<Inet6Address, RouterInfo>) iterator.next();
RouterInfo val=type.getValue();
@@ -124,7 +138,7 @@ public class KLALBRoutingProtocol extends Thread{
iterator.remove();
noticeUpdate();
}
Set<NeighborInfo> ads=val.getNeighborAddresses();
/*List<NeighborInfo> ads=val.getNeighborAddresses();
for (Iterator<NeighborInfo> iterator2 = ads.iterator(); iterator2.hasNext();) {
Inet6Address address=iterator2.next().getLocator().getAddress();
//System.out.println(address);
@@ -132,35 +146,37 @@ public class KLALBRoutingProtocol extends Thread{
requestSet.add(address);
}
}
}*/
}
computeShortestPathIfUpdated();
/*long cur2=System.nanoTime();
if(cur2-requestTimer>1000000000L) {
requestTimer=cur2;
for (Iterator<Inet6Address> iterator = requestSet.iterator(); iterator.hasNext();) {
Inet6Address inet6Address = (Inet6Address) iterator.next();
byte[]to=new byte[] {0};
DatagramPacket dgp=new DatagramPacket(to,to.length);
dgp.setAddress(inet6Address);
dgp.setPort(DEFAULT_PORT);
writePacket(ds2, new RouterInfoRequestPacket( ), new InetSocketAddress(inet6Address, DEFAULT_PORT));
if(debug)
System.out.println("Request:"+inet6Address);
sendlock.lock();
try {
ds2.send(dgp);
}catch(SocketException e) {
System.out.println("Send failed:"+dgp.getAddress());
}finally {
sendlock.unlock();
}
}
}*/
for (Iterator<Entry<Inet6Address, HotspotAddressTimer>> iterator = hotspots.entrySet().iterator(); iterator.hasNext();) {
Entry<Inet6Address, HotspotAddressTimer> type = (Entry<Inet6Address, HotspotAddressTimer>) iterator.next();
if(type.getValue().checkReportTime()) {
//System.out.println("hotspot address:"+type.getKey());
writePacket(ds2, new RouterInfoPacket( netmap.get(router.getLocator().getAddress()),false), new InetSocketAddress(type.getKey(), DEFAULT_PORT));
writePacket(ds2, new RouterInfoPacket( netmap.get(router.getLocator().getAddress()),false,-1), new InetSocketAddress(type.getKey(), DEFAULT_PORT));
}
if(type.getValue().checkTimeOut()) {
iterator.remove();
@@ -193,8 +209,16 @@ public class KLALBRoutingProtocol extends Thread{
switch(kp.getType()) {
case KLALBRoutingProtocolPacket.RINFO_REQ:
RouterInfo rifr=netmap.get(router.getLocator().getAddress());
if(rifr!=null)
writePacket(ds, new RouterInfoPacket( rifr,false), dgp.getSocketAddress());
if(debug)
System.out.println("get Request from:"+dgp.getSocketAddress());
if(rifr!=null) {
writePacket(ds, new RouterInfoPacket( rifr,false,-1), dgp.getSocketAddress());
if(debug)
System.out.println("response router info:"+rifr);
}else {
if(debug)
System.out.println("router info is null!");
}
break;
case KLALBRoutingProtocolPacket.RINFO:
@@ -237,7 +261,8 @@ public class KLALBRoutingProtocol extends Thread{
}
}
private void floodPacket(DatagramSocket ds,SocketAddress except,KLALBRoutingProtocolPacket rifp) throws IOException {
private void floodPacket(KLALBVirtualRawSocket ds2,SocketAddress except,RouterInfoPacket rifp) throws IOException {
long asn=rifp.getASN();
List<IPv6NetworkLink>links=router.getLinkTabel();
Object[] nls=links.toArray();
for(int i=0;i<nls.length;i++) {
@@ -248,27 +273,31 @@ public class KLALBRoutingProtocol extends Thread{
Neighbor addresses = (Neighbor) iteratorx.next();
InetSocketAddress isa=new InetSocketAddress(addresses.getLocator().getAddress(),DEFAULT_PORT);
if(!isa.equals(except)) {
writePacket(ds, rifp, isa);
if(asn==-1) {
writePacket(ds2, rifp, isa);
}else{
RouterInfo nif= netmap.get(isa.getAddress());
if(nif!=null&&nif.getAsn()==asn) {
writePacket(ds2, rifp, isa);
}
}
}
}
}
}
}
private void writePacket(DatagramSocket ds,KLALBRoutingProtocolPacket pkt,SocketAddress dest) throws IOException {
private void writePacket(KLALBVirtualRawSocket ds2,KLALBRoutingProtocolPacket pkt,SocketAddress dest) throws IOException {
ByteArrayOutputStream bos=new ByteArrayOutputStream(65535);
KLALBRoutingProtocolPacket.writeKLALBPacketToChannel(Channels.newChannel(bos),pkt);
byte[]to=bos.toByteArray();
DatagramPacket dgpx=new DatagramPacket(to,to.length);
dgpx.setSocketAddress(dest);
sendlock.lock();
try {
ds.send(dgpx);
ds2.send(dgpx);
}catch(BindException e) {
e.printStackTrace();
System.err.println("Cannot assign:"+dest);
}finally {
sendlock.unlock();
}
}
@@ -329,47 +358,51 @@ public class KLALBRoutingProtocol extends Thread{
return;
}
Set<Entry<Inet6Address, RouterInfo>> s=netmap.entrySet();
Map<Inet6Address,Long>airs=new HashMap<>();
Map<Inet6Address,Long>airs=new HashMap<>(netmap.size()+1);
List<Inet6Address>rias=new ArrayList<>();
List<Inet6Address>rias=new ArrayList<>(netmap.size()+1);
Map<Inet6Address,Set< LinkDirection>>paths=new HashMap<>();
Map<Inet6Address,List< LinkDirection>>paths=new HashMap<>();
long number=0;
for (Iterator<Entry<Inet6Address, RouterInfo>> iterator = s.iterator(); iterator.hasNext();) {
Entry<Inet6Address, RouterInfo> entry = (Entry<Inet6Address, RouterInfo>) iterator.next();
for(Entry<Inet6Address, RouterInfo> entry:s) {
if(!airs.containsKey(entry.getKey())) {
airs.put(entry.getKey(),number);
rias.add( entry.getKey());
number++;
}
Set<NeighborInfo> ni= entry.getValue().getNeighborAddresses();
for (Iterator<NeighborInfo> iterator2 = ni.iterator(); iterator2.hasNext();) {
NeighborInfo neighborInfo = (NeighborInfo) iterator2.next();
List<NeighborInfo> ni= entry.getValue().getNeighborAddresses();
for(NeighborInfo neighborInfo :ni) {
Inet6Address ias2=neighborInfo.getLocator().getAddress();
if(!airs.containsKey(ias2)) {
airs.put(ias2,number);
rias.add( ias2);
number++;
}
Set<LinkDirection>lp1=paths.get(entry.getKey());
List<LinkDirection>lp1=paths.get(entry.getKey());
if(lp1==null) {
paths.put(entry.getKey(), lp1=new HashSet<>());
paths.put(entry.getKey(), lp1=new ArrayList<>());
}
lp1.add(new LinkDirection(neighborInfo.getLocal(),neighborInfo.getNeighbor(),entry.getKey(), ias2, neighborInfo.getUploadDelay(),neighborInfo.getUploadSpeed(),neighborInfo.getUploadSpeedMax()));
lp1.add(new LinkDirection(neighborInfo.getLocal(),neighborInfo.getNeighbor(),entry.getKey(), ias2, neighborInfo.getUploadDelay(),neighborInfo.getUploadDelayMin(),neighborInfo.getUploadSpeed(),neighborInfo.getUploadSpeedMax()));
Set<LinkDirection>lp2=paths.get(ias2);
/*Set<LinkDirection>lp2=paths.get(ias2);
if(lp2==null) {
paths.put(ias2, lp2=new HashSet<>());
}
lp2.add( new LinkDirection(neighborInfo.getNeighbor(),neighborInfo.getLocal(),ias2,entry.getKey() , neighborInfo.getDownloadDelay(),neighborInfo.getDownloadSpeed(),neighborInfo.getDownloadSpeedMax()));
lp2.add( new LinkDirection(neighborInfo.getNeighbor(),neighborInfo.getLocal(),ias2,entry.getKey() , neighborInfo.getDownloadDelay(),neighborInfo.getDownloadDelayMin(),neighborInfo.getDownloadSpeed(),neighborInfo.getDownloadSpeedMax()));
*/
}
}
long[][]pointers=new long[(int) airs.size()][2];
long heappos=0;
long linknumber=0;
for (Iterator<Entry<Inet6Address, Set<LinkDirection>>> iterator = paths.entrySet().iterator(); iterator.hasNext();) {
Entry<Inet6Address, Set<LinkDirection>> entry = (Entry<Inet6Address, Set<LinkDirection>>) iterator.next();
for(Entry<Inet6Address, List<LinkDirection>> entry:paths.entrySet()) {
/*for (Iterator<Entry<Inet6Address, Set<LinkDirection>>> iterator = paths.entrySet().iterator(); iterator.hasNext();) {
Entry<Inet6Address, Set<LinkDirection>> entry = (Entry<Inet6Address, Set<LinkDirection>>) iterator.next();*/
linknumber+=entry.getValue().size();
}
long[][]heap=new long[(int) (linknumber)][2];
@@ -377,12 +410,16 @@ public class KLALBRoutingProtocol extends Thread{
long heapindex=0;
pointers[(int) i][0]=heappos;
InetAddress ia=rias.get((int) i);
Set<LinkDirection>lks=paths.get(ia);
List<LinkDirection>lks=paths.get(ia);
if(lks!=null)
for(LinkDirection linkPath:lks) {
/*
for (Iterator<LinkDirection> iterator = lks.iterator(); iterator.hasNext();) {
LinkDirection linkPath=iterator.next();
LinkDirection linkPath=iterator.next();*/
heap[(int)(heappos+ heapindex)][0]=airs.get(linkPath.getToLocator());
heap[(int) (heappos+heapindex)][1]=linkPath.getWeight();
//heap[(int) (heappos+heapindex)][2]=linkPath.get
// heap[(int) (heappos+heapindex)][3]=0;
heapindex++;
}
@@ -391,6 +428,8 @@ public class KLALBRoutingProtocol extends Thread{
pointers[(int) i][1]=heapindex;
}
algorithm.setGraph(pointers,heap,airs.get(router.getLocator().getAddress()));
algorithm.run();
directions=new NetmapDirections(algorithm.getDirection(), rias, airs);
@@ -407,12 +446,12 @@ public class KLALBRoutingProtocol extends Thread{
private Inet6Address fromLocator;
private Inet6Address toLocator;
private long delay;
private long delayMin;
private long speed;
public long getSpeed() {
return speed;
}
private long bandwidth;
private long usedBandwidth=0;
public Inet6AddressGroup getFromAddress() {
return fromAddress;
}
@@ -425,22 +464,28 @@ public class KLALBRoutingProtocol extends Thread{
public long getBandwidth() {
return bandwidth;
}
public long getDelayMin() {
return delayMin;
}
public LinkDirection(Inet6AddressGroup fromAddress, Inet6AddressGroup toAddress, Inet6Address fromLocator,
Inet6Address toLocator,long delay,long speed, long bandwidth) {
Inet6Address toLocator, long delay, long delayMin, long speed, long bandwidth) {
super();
this.fromAddress = fromAddress;
this.toAddress = toAddress;
this.fromLocator = fromLocator;
this.toLocator = toLocator;
this.delay = delay;
this.speed=speed;
this.delayMin = delayMin;
this.speed = speed;
this.bandwidth = bandwidth;
}
@Override
public String toString() {
return "LinkDirection [fromAddress=" + fromAddress + ", toAddress=" + toAddress + ", fromLocator="
+ fromLocator + ", toLocator=" + toLocator + ", delay=" + delay + ", speed=" + speed
+ ", bandwidth=" + bandwidth + ", usedBandwidth=" + usedBandwidth + "]";
+ fromLocator + ", toLocator=" + toLocator + ", delay=" + delay + ", delayMin=" + delayMin
+ ", speed=" + speed + ", bandwidth=" + bandwidth + "]";
}
public Inet6Address getFromLocator() {
return fromLocator;
@@ -470,8 +515,7 @@ public class KLALBRoutingProtocol extends Thread{
private RouterInfo getSelfRouterInfo() {
List<IPv6NetworkLink>links=router.getLinkTabel();
Object[] nls=links.toArray();
RouterInfo ri=new RouterInfo(System.currentTimeMillis());
ri.setLocator(router.getLocator());
RouterInfo ri=new RouterInfo(System.currentTimeMillis(),router.getLocator(),router.getASN());
for(int i=0;i<nls.length;i++) {
IPv6NetworkLink nl=(IPv6NetworkLink) nls[i];
if((!nl.isLoopBack())&&nl.isUp()) {
@@ -480,6 +524,8 @@ public class KLALBRoutingProtocol extends Thread{
Neighbor addresses = (Neighbor) iteratorx.next();
long updelay=10000000000L;
long downdelay=10000000000L;
long updelayMin=10000000000L;
long downdelayMin=10000000000L;
long uploadspeed=1024L*1024;
long downloadspeed=1024L*1024;
long uploadspeedmax=1024L*1024;
@@ -489,6 +535,11 @@ public class KLALBRoutingProtocol extends Thread{
if(md instanceof DelayMonitorData) {
updelay=((DelayMonitorData) md).getOutDelay();
downdelay=((DelayMonitorData) md).getInDelay();
if(md instanceof SpeedAndTrafficAndDelayMonitorDataImpl) {
updelayMin=((SpeedAndTrafficAndDelayMonitorDataImpl) md).getOutDelayMin();
downdelayMin=((SpeedAndTrafficAndDelayMonitorDataImpl) md).getInDelayMin();
}
}
if(md instanceof SpeedAndTrafficMonitorData) {
uploadspeed=((SpeedAndTrafficMonitorData) md).getOutSpeed();
@@ -500,7 +551,9 @@ public class KLALBRoutingProtocol extends Thread{
}
}
}
ri.getNeighborAddresses().add(new NeighborInfo(nl.getAddressGroup(),addresses.getAddress(),addresses.getLocator(),updelay ,downdelay ,uploadspeed ,downloadspeed,uploadspeedmax ,downloadspeedmax));
NeighborInfo ni=new NeighborInfo(nl.getAddressGroup(),addresses.getAddress(),addresses.getLocator(),updelay ,downdelay,updelayMin ,downdelayMin ,uploadspeed ,downloadspeed,uploadspeedmax ,downloadspeedmax);
ni.setContext(addresses);
ri.getNeighborAddresses().add(ni);
}
}
@@ -513,7 +566,7 @@ public class KLALBRoutingProtocol extends Thread{
public Map<Inet6Address, Long> getAddresses() {
return addresses;
}
public Map<Inet6Address, Set<LinkDirection>> getPaths() {
public Map<Inet6Address, List<LinkDirection>> getPaths() {
return paths;
}
public List<Inet6Address> createSegmentList(Inet6Address dest) {
@@ -564,12 +617,41 @@ public class KLALBRoutingProtocol extends Thread{
private Map<Inet6Address, HotspotAddressTimer> hotspots=new ConcurrentHashMap<>();
public void putHotspotAddress(Inet6Address sourceAddress) {
if(true) {
HotspotAddressTimer hat=hotspots.get(sourceAddress);
if(hat==null) {
hotspots.put(sourceAddress, new HotspotAddressTimer());
}else {
hat.refreshTimeout();
}
}
}
private Map<Inet6Address,Long> toIpBandwidth=new ConcurrentHashMap<Inet6Address,Long>();
public void updateTotalRequestBandwidth(Inet6Address targetaAddress, Long treq) {
Objects.requireNonNull(targetaAddress);
//System.out.println("到"+targetaAddress.getHostAddress()+"请求带宽更新:"+treq);
if(treq<=0) {
if(toIpBandwidth.remove(targetaAddress)!=null) {
noticeUpdate();
}
}else {
Long old=toIpBandwidth.put(targetaAddress, treq);
if(old==null||old.longValue()!=treq.longValue()) {
noticeUpdate();
}
}
}
public Inet6Address getDijkstraPrevNode(Inet6Address text) {
NetmapDirections directionsx=directions;
if(directionsx==null)
return null;
long number= directionsx.getDirection_airs().get(text);
long prev= directionsx.getDirection()[(int)number];
if(prev==-1) {
return null;
}
return directionsx.getDirection_rias().get((int) prev);
}
}