forked from KNEMC/KLALB
KLALB3.2
This commit is contained in:
@@ -14,10 +14,16 @@ public interface IPv6NetworkLink {
|
||||
public boolean isLoopBack();
|
||||
public Inet6AddressGroup getAddressGroup();
|
||||
public List<Neighbor> getNeighborsInfo();
|
||||
public List<RouteItem> getRouteItems();
|
||||
public void sendPacket(IPv6Packet pack, Inet6Address inet6Address) throws IOException;
|
||||
public boolean isCongress(IPv6Packet iPv6Packet);
|
||||
public boolean isCongress(IPv6Packet iPv6Packet,double scale);
|
||||
public default boolean isCongress(IPv6Packet iPv6Packet) {
|
||||
return isCongress(iPv6Packet, 1.0);
|
||||
}
|
||||
public String getName();
|
||||
public boolean isUp();
|
||||
public boolean canSend(IPv6Packet iPv6Packet);
|
||||
public void setReceiveConsumer(Consumer<IPv6Packet>con);
|
||||
void setRerouteConsumer(Consumer<IPv6Packet> rerouteConsumer);
|
||||
public boolean isReachSpeedLimit(IPv6Packet iPv6Packet);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.kne.cloud.network.ipv6;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.io.StreamCorruptedException;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.UnknownHostException;
|
||||
@@ -11,9 +12,12 @@ import java.nio.channels.WritableByteChannel;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
import org.kne.cloud.network.NetworkPacket;
|
||||
import org.kne.cloud.network.ipv6.IPv6Packet.IPv6ExtHeader;
|
||||
import org.kne.cloud.network.ipv6.IPv6Packet.IPv6SegmentRoutingHeader;
|
||||
import org.kne.cloud.network.klalb.KLALBPacket;
|
||||
import org.kne.cloud.network.srv6.IPv6SegmentRoutingTLV;
|
||||
import org.kne.cloud.network.srv6.IpV6RoutingSRHData;
|
||||
@@ -24,6 +28,29 @@ import org.kne.cloud.network.srv6.SRv6TLV;
|
||||
import org.pcap4j.packet.namednumber.IpNumber;
|
||||
|
||||
public class IPv6Packet extends NetworkPacket {
|
||||
|
||||
private long flowSeqMark=-1;
|
||||
|
||||
|
||||
|
||||
public long getFlowSeqMark() {
|
||||
return flowSeqMark;
|
||||
}
|
||||
|
||||
public void setFlowSeqMark(long flowSeqMark) {
|
||||
this.flowSeqMark = flowSeqMark;
|
||||
}
|
||||
|
||||
|
||||
private boolean TTLdecreased=false;
|
||||
|
||||
public boolean isTTLdecreased() {
|
||||
return TTLdecreased;
|
||||
}
|
||||
|
||||
public void setTTLdecreased(boolean tTLdecreased) {
|
||||
TTLdecreased = tTLdecreased;
|
||||
}
|
||||
private volatile ByteBuffer IPv6header ;
|
||||
|
||||
private volatile List<IPv6ExtHeader> headers = new ArrayList<>();
|
||||
@@ -41,7 +68,7 @@ public class IPv6Packet extends NetworkPacket {
|
||||
}
|
||||
|
||||
public IPv6Packet() {
|
||||
IPv6header = NetworkPacket.databufferpool_40.borrow();
|
||||
IPv6header = NetworkPacket.bufferAllocator.allocate(40);
|
||||
IPv6header.limit(IPv6_HEADER_LENGTH);
|
||||
}
|
||||
|
||||
@@ -76,6 +103,7 @@ public class IPv6Packet extends NetworkPacket {
|
||||
|
||||
public void markCE() {
|
||||
IPv6header.put(1,(byte) (IPv6header.get(1)|0b00110000));
|
||||
//new Exception().printStackTrace();
|
||||
}
|
||||
|
||||
public boolean isCE() {
|
||||
@@ -114,7 +142,24 @@ public class IPv6Packet extends NetworkPacket {
|
||||
public void setHopLimit(int hopLimit) {
|
||||
IPv6header.put(7, (byte) hopLimit);
|
||||
}
|
||||
|
||||
public void getRawSourceAddress(byte[] sourceAddress) {
|
||||
IPv6header.get(8, sourceAddress, 0, sourceAddress.length);
|
||||
}
|
||||
|
||||
public void setRawSourceAddress(byte[] sourceAddress) {
|
||||
IPv6header.put(8, sourceAddress, 0, sourceAddress.length);
|
||||
}
|
||||
|
||||
public void getRawDestinationAddress(byte[] destinationAddress) {
|
||||
IPv6header.get(24, destinationAddress, 0,destinationAddress.length);
|
||||
|
||||
}
|
||||
|
||||
public void setRawDestinationAddress(byte[] destinationAddress) {
|
||||
IPv6header.put(24, destinationAddress, 0, destinationAddress.length);
|
||||
}
|
||||
|
||||
public Inet6Address getSourceAddress() {
|
||||
byte[] b = new byte[16];
|
||||
IPv6header.get(8, b, 0, b.length);
|
||||
@@ -163,29 +208,6 @@ public class IPv6Packet extends NetworkPacket {
|
||||
return calcPayloadLength()+IPv6_HEADER_LENGTH;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void doDisposeAfterSend() {
|
||||
super.doDisposeAfterSend();
|
||||
payload.doDisposeAfterSend();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
super.dispose();
|
||||
/*ByteBuffer headerx = IPv6header;
|
||||
IPv6header = null;
|
||||
if(headerx!=null)
|
||||
NetworkPacket.databufferpool_40.back(headerx);*/
|
||||
}
|
||||
|
||||
@Override
|
||||
public void disposeAll() {
|
||||
super.disposeAll();
|
||||
for (int i = 0; i < headers.size(); i++) {
|
||||
headers.get(i).disposeAll();
|
||||
}
|
||||
payload.disposeAll();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
@@ -241,7 +263,7 @@ public class IPv6Packet extends NetworkPacket {
|
||||
headers.add(ext);
|
||||
break;
|
||||
case 43:
|
||||
ByteBuffer bbf=NetworkPacket.databufferpool_2048.borrow();
|
||||
ByteBuffer bbf=NetworkPacket.bufferAllocator.allocate(2048);
|
||||
bbf.limit(4);
|
||||
while (bbf.hasRemaining()) {
|
||||
if (din.read(bbf) == -1) {
|
||||
@@ -296,7 +318,11 @@ public class IPv6Packet extends NetworkPacket {
|
||||
KLALBPacket kp=KLALBPacket.readKLALBPacketFromChannel(din);
|
||||
this.payload=kp;
|
||||
break loop;
|
||||
|
||||
case 253:
|
||||
IPv6Payload epld=new IPv6EmptyPayload();
|
||||
epld.readFromChannel(din, payloadlength);
|
||||
this.payload=epld;
|
||||
break loop;
|
||||
default:
|
||||
IPv6Payload pld=new IPv6Payload(nextheader);
|
||||
pld.readFromChannel(din, payloadlength);
|
||||
@@ -376,35 +402,7 @@ public class IPv6Packet extends NetworkPacket {
|
||||
//System.out.println(payload);
|
||||
}*/
|
||||
|
||||
@Override
|
||||
public void lockAll() {
|
||||
super.lockAll();
|
||||
for (int i = 0; i < headers.size(); i++) {
|
||||
headers.get(i).lockAll();
|
||||
}
|
||||
payload.lockAll();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSomeDisposed() {
|
||||
if(super.isSomeDisposed()||payload.isSomeDisposed())
|
||||
return true;
|
||||
for (Iterator<IPv6ExtHeader> iterator = headers.iterator(); iterator.hasNext();) {
|
||||
IPv6ExtHeader iPv6ExtHeader = (IPv6ExtHeader) iterator.next();
|
||||
if(iPv6ExtHeader.isSomeDisposed())
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void unlockAll() {
|
||||
payload.unlockAll();
|
||||
for (int i = 0; i < headers.size(); i++) {
|
||||
headers.get(i).unlockAll();
|
||||
}
|
||||
super.unlockAll();
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
@@ -434,7 +432,7 @@ public class IPv6Packet extends NetworkPacket {
|
||||
this.protocolNumber = protocolNumber;
|
||||
this.onDefault=isonDefault;
|
||||
if(onDefault)
|
||||
this.data=NetworkPacket.databufferpool_65535.borrow();
|
||||
this.data=NetworkPacket.bufferAllocator.allocate(65535);
|
||||
}
|
||||
|
||||
public int getProtocolNumber() {
|
||||
@@ -446,13 +444,13 @@ public class IPv6Packet extends NetworkPacket {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToChannel(WritableByteChannel dto) throws IOException {
|
||||
public void writeToChannel(WritableByteChannel dto) throws IOException {
|
||||
if(onDefault)
|
||||
dto.write(data.slice(0,data.limit()));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void readFromChannel(ReadableByteChannel din, long length) throws IOException {
|
||||
public void readFromChannel(ReadableByteChannel din, long length) throws IOException {
|
||||
if(onDefault) {
|
||||
data.limit((int) length);
|
||||
while (data.hasRemaining()) {
|
||||
@@ -463,15 +461,7 @@ public class IPv6Packet extends NetworkPacket {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
super.dispose();
|
||||
/*if(onDefault) {
|
||||
ByteBuffer datax = data;
|
||||
data = null;
|
||||
NetworkPacket.databufferpool_65535.back(datax);
|
||||
}*/
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
protected boolean needEndPosition() {
|
||||
@@ -480,6 +470,28 @@ public class IPv6Packet extends NetworkPacket {
|
||||
|
||||
}
|
||||
|
||||
public static class IPv6EmptyPayload extends IPv6Payload{
|
||||
|
||||
public IPv6EmptyPayload() {
|
||||
super(253, false);
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLength() {
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeToChannel(WritableByteChannel dto) throws IOException {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromChannel(ReadableByteChannel din, long length) throws IOException {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static class IPv6ExtHeader extends NetworkPacket{
|
||||
private ByteBuffer data ;
|
||||
|
||||
@@ -505,9 +517,9 @@ public class IPv6Packet extends NetworkPacket {
|
||||
this.protocolNumber = protocolNumber;
|
||||
this.onDefault=isonDefault;
|
||||
if(onDefault) {
|
||||
this.data=NetworkPacket.databufferpool_2048.borrow();
|
||||
this.data=NetworkPacket.bufferAllocator.allocate(2048);
|
||||
}else {
|
||||
this.data=NetworkPacket.databufferpool_40.borrow();
|
||||
this.data=NetworkPacket.bufferAllocator.allocate(8);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -542,7 +554,7 @@ public class IPv6Packet extends NetworkPacket {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void writeToChannel(WritableByteChannel dto) throws IOException {
|
||||
public void writeToChannel(WritableByteChannel dto) throws IOException {
|
||||
if(onDefault) {
|
||||
int ext=(data.limit()-8)/8;
|
||||
setExtLength(ext);
|
||||
@@ -553,7 +565,7 @@ public class IPv6Packet extends NetworkPacket {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void readFromChannel(ReadableByteChannel din, long length) throws IOException {
|
||||
public void readFromChannel(ReadableByteChannel din, long length) throws IOException {
|
||||
data.limit(8);
|
||||
while (data.hasRemaining()) {
|
||||
if (din.read(data) == -1) {
|
||||
@@ -576,19 +588,7 @@ public class IPv6Packet extends NetworkPacket {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void dispose() {
|
||||
super.dispose();
|
||||
/*ByteBuffer bbft=data;
|
||||
data=null;
|
||||
if(bbft!=null) {
|
||||
if(bbft.capacity()==40) {
|
||||
NetworkPacket.databufferpool_40.back(bbft);
|
||||
}else {
|
||||
NetworkPacket.databufferpool_2048.back(bbft);
|
||||
}
|
||||
}*/
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -678,27 +678,37 @@ public class IPv6Packet extends NetworkPacket {
|
||||
public List<Inet6Address> getAddresses() {
|
||||
return addresses;
|
||||
}
|
||||
|
||||
public List<IPv6SegmentRoutingTLV> getTlvs() {
|
||||
return tlvs;
|
||||
}
|
||||
@Override
|
||||
protected void writeToChannel(WritableByteChannel dto) throws IOException {
|
||||
setExtLength(calcExtLength()/8);
|
||||
public void writeToChannel(WritableByteChannel dto) throws IOException {
|
||||
int exl=calcExtLength();
|
||||
if(exl%8!=0) {
|
||||
throw new StreamCorruptedException("extLength % 8 !=0");
|
||||
}
|
||||
|
||||
setExtLength(exl/8);
|
||||
setLastEntry(addresses.size()-1);
|
||||
super.writeToChannel(dto);
|
||||
for (Iterator<Inet6Address> iterator = addresses.iterator(); iterator.hasNext();) {
|
||||
Inet6Address inet6Address = (Inet6Address) iterator.next();
|
||||
|
||||
for(Inet6Address inet6Address:addresses) {
|
||||
dto.write(ByteBuffer.wrap(inet6Address.getAddress()));
|
||||
}
|
||||
|
||||
for(IPv6SegmentRoutingTLV tlv:tlvs) {
|
||||
IPv6SegmentRoutingTLV.writeIPv6SegmentRoutingTLVToChannel(dto, tlv);
|
||||
}
|
||||
}
|
||||
private int calcExtLength() {
|
||||
int tlvsl=0;
|
||||
for (Iterator<IPv6SegmentRoutingTLV> iterator = tlvs.iterator(); iterator.hasNext();) {
|
||||
IPv6SegmentRoutingTLV tlve = (IPv6SegmentRoutingTLV) iterator.next();
|
||||
for(IPv6SegmentRoutingTLV tlve:tlvs) {
|
||||
tlvsl+=tlve.getLength();
|
||||
}
|
||||
return addresses.size()*16+tlvsl;
|
||||
}
|
||||
@Override
|
||||
protected void readFromChannel(ReadableByteChannel din, long length) throws IOException {
|
||||
public void readFromChannel(ReadableByteChannel din, long length) throws IOException {
|
||||
super.readFromChannel(din, length);
|
||||
int extl=getExtLength();
|
||||
int laste=getLastEntry();
|
||||
@@ -722,6 +732,13 @@ public class IPv6Packet extends NetworkPacket {
|
||||
//System.out.println("SR:"+addr);
|
||||
addresses.add(addr);
|
||||
}
|
||||
tlvs.clear();
|
||||
while(usdl<rl) {
|
||||
IPv6SegmentRoutingTLV tlv=IPv6SegmentRoutingTLV.readIPv6SegmentRoutingTLVFromChannel(din);
|
||||
usdl+=tlv.getLength();
|
||||
tlvs.add(tlv);
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
@Override
|
||||
@@ -761,7 +778,20 @@ public class IPv6Packet extends NetworkPacket {
|
||||
public List<IPv6ExtHeader> getHeaders() {
|
||||
return headers;
|
||||
}
|
||||
|
||||
public IPv6SegmentRoutingHeader getSRHHeader() {
|
||||
IPv6SegmentRoutingHeader srhh = null;
|
||||
List<IPv6ExtHeader> exhs = headers;
|
||||
for (int j = 0; j < exhs.size(); j++) {
|
||||
IPv6ExtHeader exh = exhs.get(j);
|
||||
if (exh instanceof IPv6SegmentRoutingHeader) {
|
||||
if (((IPv6SegmentRoutingHeader) exh).getRoutingType() == 4) {
|
||||
srhh = (IPv6SegmentRoutingHeader) exh;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return srhh;
|
||||
}
|
||||
/*public IPv6DestinationHeader insertDestinationHeader(int index,int extLength) {
|
||||
long ptr;
|
||||
long lth=extLength*8+8;
|
||||
@@ -831,6 +861,18 @@ public class IPv6Packet extends NetworkPacket {
|
||||
protected boolean needEndPosition() {
|
||||
return false;
|
||||
}
|
||||
private AtomicInteger rerouteCounter=new AtomicInteger(0);
|
||||
public AtomicInteger getRerouteCounter() {
|
||||
return rerouteCounter;
|
||||
}
|
||||
private volatile boolean promise=false;
|
||||
public void setPromise(boolean b) {
|
||||
promise=b;
|
||||
}
|
||||
|
||||
public boolean isPromise() {
|
||||
return promise;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -3,12 +3,14 @@ package org.kne.cloud.network.ipv6;
|
||||
import java.io.Closeable;
|
||||
import java.io.IOException;
|
||||
import java.net.Inet6Address;
|
||||
import java.net.InetAddress;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutorService;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.LinkedBlockingQueue;
|
||||
@@ -19,14 +21,22 @@ import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.kne.cloud.network.NetworkPacket;
|
||||
import org.kne.cloud.network.ipv6.IPv6Packet.IPv6SegmentRoutingHeader;
|
||||
import org.kne.cloud.network.monitor.MonitorData;
|
||||
import org.kne.cloud.network.monitor.SpeedAndTrafficMonitorDataImpl;
|
||||
import org.kne.cloud.network.srv6.IPv6SegmentRoutingTLV;
|
||||
import org.kne.cloud.network.srv6.IpV6RoutingSRHData;
|
||||
import org.kne.cloud.network.srv6.PacketConsumer;
|
||||
import org.kne.cloud.network.srv6.PacketReorder;
|
||||
import org.kne.cloud.network.srv6.SEQSSegmentRoutingTLV;
|
||||
import org.kne.cloud.network.srv6.SRv6PacketReorder;
|
||||
import org.kne.cloud.network.srv6.SRv6PacketSeqMarker;
|
||||
import org.kne.cloud.network.srv6.SRv6StreamSequenceTLV;
|
||||
import org.kne.cloud.network.srv6.SRv6TLV;
|
||||
import org.kne.cloud.network.srv6.TCPTransimitAgent;
|
||||
import org.kne.cloud.network.tun.TUNNetworkDevice;
|
||||
import org.kne.concurrent.DisruptorExecutor;
|
||||
import org.kne.concurrent.HighPerformanceExecutor;
|
||||
import org.kne.concurrent.SpinLock;
|
||||
import org.kne.io.KNEChannels;
|
||||
|
||||
@@ -42,11 +52,11 @@ public class IPv6TUNLoopbackNetworkLink implements IPv6NetworkLink, Closeable, A
|
||||
|
||||
private Thread tr;
|
||||
|
||||
private ThreadPoolExecutor exc = (ThreadPoolExecutor) Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
|
||||
|
||||
private Lock slok=new SpinLock();
|
||||
|
||||
public IPv6TUNLoopbackNetworkLink(Inet6AddressGroup hostAddress, int mtu) throws IOException {
|
||||
private SRv6PacketSeqMarker seqm=new SRv6PacketSeqMarker();
|
||||
|
||||
public IPv6TUNLoopbackNetworkLink(Inet6AddressGroup hostAddress, int mtu,List<InetAddress>dns) throws IOException {
|
||||
if (tun != null)
|
||||
throw new IllegalStateException("already open!");
|
||||
try {
|
||||
@@ -56,31 +66,42 @@ public class IPv6TUNLoopbackNetworkLink implements IPv6NetworkLink, Closeable, A
|
||||
this.hostAddress = hostAddress;
|
||||
if (hostAddress != null)
|
||||
tun.setIPAddress(hostAddress.getAddress(), hostAddress.getPrefixLength());
|
||||
if(dns!=null) {
|
||||
tun.setDNSAddress(dns);
|
||||
}
|
||||
try {
|
||||
tun.setMTU(mtu);
|
||||
}catch(IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Thread tb = new Thread(() -> {
|
||||
while (true) {
|
||||
ByteBuffer tmp = NetworkPacket.databufferpool_65535.borrow();
|
||||
ByteBuffer tmp = NetworkPacket.bufferAllocator.allocate(65535);
|
||||
try {
|
||||
tun.read(tmp);
|
||||
tmp.flip();
|
||||
//System.out.println(tmp);
|
||||
if (IPv6Packet.getIPVersion(tmp.get(0)) == 6) {
|
||||
while(exc.getQueue().size()>10) {
|
||||
LockSupport.parkNanos(1000000);
|
||||
}
|
||||
exc.execute(() -> {
|
||||
|
||||
HighPerformanceExecutor.defaultExecutor.execute(() -> {
|
||||
try {
|
||||
IPv6Packet ipp = new IPv6Packet();
|
||||
ipp.readFromChannel(KNEChannels.newReadableChannel(tmp), mtu);
|
||||
NetworkPacket.databufferpool_65535.back(tmp);
|
||||
|
||||
//NetworkPacket.databufferpool_65535.back(tmp);
|
||||
if (con != null) {
|
||||
|
||||
monitor.getOutPacketCounterAL().incrementAndGet();
|
||||
monitor.getOutTrafficAL().addAndGet(ipp.getLength());
|
||||
ipp.setDisposeAfterSend(true);
|
||||
ipp.getPayload().setDisposeAfterSend(true);
|
||||
// ipp.setDisposeAfterSend(true);
|
||||
// ipp.getPayload().setDisposeAfterSend(true);
|
||||
ipp.setPromise(true);
|
||||
if(ipp.getPayload().getProtocolNumber()==6) {
|
||||
seqm.mark(ipp);
|
||||
}
|
||||
con.accept(ipp);
|
||||
} else {
|
||||
ipp.dispose();
|
||||
//ipp.dispose();
|
||||
}
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
@@ -88,10 +109,10 @@ public class IPv6TUNLoopbackNetworkLink implements IPv6NetworkLink, Closeable, A
|
||||
|
||||
});
|
||||
}else {
|
||||
NetworkPacket.databufferpool_65535.back(tmp);
|
||||
//NetworkPacket.databufferpool_65535.back(tmp);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
NetworkPacket.databufferpool_65535.back(tmp);
|
||||
//NetworkPacket.databufferpool_65535.back(tmp);
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
@@ -110,9 +131,16 @@ public class IPv6TUNLoopbackNetworkLink implements IPv6NetworkLink, Closeable, A
|
||||
e.printStackTrace();
|
||||
}finally {
|
||||
slok.unlock();
|
||||
NetworkPacket.databufferpool_65535.back(tmp);
|
||||
//NetworkPacket.databufferpool_65535.back(tmp);
|
||||
}
|
||||
} else {
|
||||
for(SRv6PacketReorder spr:reorder.values()) {
|
||||
try {
|
||||
spr.runOrdering();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
LockSupport.parkNanos(1000000L);
|
||||
}
|
||||
|
||||
@@ -132,48 +160,84 @@ public class IPv6TUNLoopbackNetworkLink implements IPv6NetworkLink, Closeable, A
|
||||
|
||||
private SpeedAndTrafficMonitorDataImpl monitor;
|
||||
|
||||
private ConcurrentHashMap<FlowSession, SRv6PacketReorder> reorder=new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void sendPacket(IPv6Packet pack, Inet6Address next) throws IOException {
|
||||
if (tun != null) {/*
|
||||
* AtomicLong seqs=null; try { IPv6RoutingHeader
|
||||
* srhh=getRoutingHeaderFromPacket(pack); if(srhh!=null) { byte[]srd=new
|
||||
* byte[(int) (srhh.getLength()-4)]; srhh.getData().get(4, srd);
|
||||
* IpV6RoutingSRHData srh=IpV6RoutingSRHData.newInstance( srd,0,srd.length);
|
||||
* SRv6StreamSequenceTLV ssqv= getStreamSequenceTLV(srh); if(ssqv!=null)
|
||||
* seqs=new AtomicLong( ssqv.getSequence()); } }catch(IllegalRawDataException
|
||||
* re) {
|
||||
*
|
||||
* }
|
||||
*/
|
||||
|
||||
// System.out.println("add");
|
||||
while(exc.getQueue().size()>10) {
|
||||
LockSupport.parkNanos(1000000);
|
||||
}
|
||||
exc.execute(()->{
|
||||
ByteBuffer tmp = NetworkPacket.databufferpool_65535.borrow();
|
||||
if (tun != null) {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
try {
|
||||
// System.out.println("rev");
|
||||
pack.writeToChannel(KNEChannels.newWritableChannel(tmp));
|
||||
monitor.getInPacketCounterAL().incrementAndGet();
|
||||
monitor.getInTrafficAL().addAndGet(pack.getLength());
|
||||
tmp.flip();
|
||||
sendQueue.add(tmp);
|
||||
LockSupport.unpark(tr);
|
||||
IPv6SegmentRoutingHeader srh= pack.getSRHHeader();
|
||||
SEQSSegmentRoutingTLV seqs=null;
|
||||
if(srh!=null) {
|
||||
List<IPv6SegmentRoutingTLV> l=srh.getTlvs();
|
||||
|
||||
for (int i = 0; i < l.size(); i++) {
|
||||
IPv6SegmentRoutingTLV tlv=l.get(i);
|
||||
if(tlv instanceof SEQSSegmentRoutingTLV) {
|
||||
seqs=(SEQSSegmentRoutingTLV) tlv;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
sendPacket0(pack,seqs);
|
||||
|
||||
} catch (IOException e) {
|
||||
NetworkPacket.databufferpool_65535.back(tmp);
|
||||
//NetworkPacket.databufferpool_65535.back(tmp);
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
pack.disposeAll();
|
||||
});
|
||||
//pack.disposeAll();
|
||||
|
||||
} else {
|
||||
pack.disposeAll();
|
||||
//pack.disposeAll();
|
||||
}
|
||||
}
|
||||
|
||||
private void sendPacket0(IPv6Packet pack,SEQSSegmentRoutingTLV seqs) throws IOException {
|
||||
|
||||
if(seqs!=null&&seqs.isKeepOrder()) {
|
||||
FlowSession fss=pack.getFlowSession();
|
||||
|
||||
|
||||
SRv6PacketReorder newr= new SRv6PacketReorder(new PacketConsumer() {
|
||||
|
||||
@Override
|
||||
public void accept(IPv6Packet packx) throws IOException {
|
||||
ByteBuffer tmp = NetworkPacket.bufferAllocator.allocate(65535);
|
||||
packx.writeToChannel(KNEChannels.newWritableChannel(tmp));
|
||||
monitor.getInPacketCounterAL().incrementAndGet();
|
||||
monitor.getInTrafficAL().addAndGet(packx.getLength());
|
||||
tmp.flip();
|
||||
sendQueue.add(tmp);
|
||||
LockSupport.unpark(tr);
|
||||
}
|
||||
});
|
||||
SRv6PacketReorder olr=reorder.putIfAbsent(fss,newr);
|
||||
if(olr==null) {
|
||||
olr=newr;
|
||||
}
|
||||
olr.put(pack,seqs.getSequence());
|
||||
|
||||
|
||||
}else {
|
||||
ByteBuffer tmp = NetworkPacket.bufferAllocator.allocate(65535);
|
||||
pack.writeToChannel(KNEChannels.newWritableChannel(tmp));
|
||||
monitor.getInPacketCounterAL().incrementAndGet();
|
||||
monitor.getInTrafficAL().addAndGet(pack.getLength());
|
||||
tmp.flip();
|
||||
sendQueue.add(tmp);
|
||||
LockSupport.unpark(tr);
|
||||
}
|
||||
}
|
||||
|
||||
private SRv6StreamSequenceTLV getStreamSequenceTLV(IpV6RoutingSRHData srh) {
|
||||
List<SRv6TLV> stv = srh.getTlvs();
|
||||
for (Iterator iterator = stv.iterator(); iterator.hasNext();) {
|
||||
@@ -199,8 +263,8 @@ public class IPv6TUNLoopbackNetworkLink implements IPv6NetworkLink, Closeable, A
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isCongress(IPv6Packet iPv6Packet) {
|
||||
return sendQueue.size() > 1000;
|
||||
public boolean isCongress(IPv6Packet iPv6Packet,double scale) {
|
||||
return sendQueue.size() > 1000*scale;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -243,6 +307,36 @@ public class IPv6TUNLoopbackNetworkLink implements IPv6NetworkLink, Closeable, A
|
||||
return hostAddress;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void setRerouteConsumer(Consumer<IPv6Packet> rerouteConsumer) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<RouteItem> getRouteItems() {
|
||||
List<RouteItem> rlist=new ArrayList<>();
|
||||
|
||||
rlist.add(new RouteItem(new Inet6AddressGroup(this.getAddressGroup().getAddress(), 128),
|
||||
this.getAddressGroup().getAddress(), this, "Direct", 0, 1, null, "D",true));
|
||||
|
||||
for (Iterator<Neighbor> iteratorx = getNeighborsInfo()
|
||||
.iterator(); iteratorx.hasNext();) {
|
||||
Neighbor addresses = (Neighbor) iteratorx.next();
|
||||
RouteItem ri = new RouteItem(new Inet6AddressGroup(addresses.getAddress().getAddress(), 128), addresses.getAddress().getAddress(),
|
||||
this, "Direct", 0, 128, addresses.getMonitor(), "D",false);
|
||||
rlist.add(ri);
|
||||
|
||||
RouteItem ris = new RouteItem(addresses.getLocator(), (Inet6Address) addresses.getLocator().getAddress(),
|
||||
this, "KLALB SRv6", 13, 128, addresses.getMonitor(), "D",false);
|
||||
rlist.add(ris);
|
||||
|
||||
}
|
||||
return rlist;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReachSpeedLimit(IPv6Packet iPv6Packet) {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -63,13 +63,43 @@ public class Inet6AddressGroup implements Comparable<Inet6AddressGroup>{
|
||||
public int getPrefixLength() {
|
||||
return prefixLength;
|
||||
}
|
||||
private byte[] cachedRawAddress;
|
||||
public byte[] getRawAddress() {
|
||||
if(cachedRawAddress==null) {
|
||||
return (cachedRawAddress=address.getAddress());
|
||||
}else {
|
||||
return cachedRawAddress;
|
||||
}
|
||||
}
|
||||
private byte[] cachedAndm;
|
||||
private byte[] getAndm(byte[]mask) {
|
||||
if(cachedAndm==null) {
|
||||
return (cachedAndm=and0(mask ,getRawAddress()));
|
||||
}else {
|
||||
return cachedAndm;
|
||||
}
|
||||
}
|
||||
public boolean checkMatch(Inet6Address ia) {
|
||||
byte[]mask=maskTransf[prefixLength];
|
||||
byte[]andj=and0(mask,ia.getAddress());
|
||||
byte[]andm=and0(mask ,address.getAddress());
|
||||
return Arrays.equals(andj,andm);
|
||||
byte[]andm=getAndm(mask);
|
||||
|
||||
return andeq0(mask,ia.getAddress(),andm);
|
||||
}
|
||||
private byte[] and0(byte[] bs, byte[] address2) {
|
||||
public boolean checkMatch(byte[] ia) {
|
||||
byte[]mask=maskTransf[prefixLength];
|
||||
byte[]andm=getAndm(mask);
|
||||
|
||||
return andeq0(mask,ia,andm);
|
||||
}
|
||||
private static boolean andeq0(byte[] bs, byte[] address2, byte[] andm) {
|
||||
for (int i = 0; i < bs.length; i++) {
|
||||
if(andm[i]!=(byte) (bs[i]&address2[i])) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
private static byte[] and0(byte[] bs, byte[] address2) {
|
||||
byte[]rez=new byte[bs.length];
|
||||
for (int i = 0; i < rez.length; i++) {
|
||||
rez[i]=(byte) (bs[i]&address2[i]);
|
||||
@@ -90,4 +120,5 @@ public class Inet6AddressGroup implements Comparable<Inet6AddressGroup>{
|
||||
address=(Inet6Address) Inet6Address.getByAddress(b);
|
||||
prefixLength=in.read();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -5,11 +5,14 @@ import java.net.InetAddress;
|
||||
import java.util.Objects;
|
||||
|
||||
import org.kne.cloud.network.monitor.MonitorData;
|
||||
import org.kne.cloud.network.monitor.QueueingMonitorDataImpl;
|
||||
import org.kne.cloud.network.te.BandwidthDistributer;
|
||||
|
||||
public class Neighbor {
|
||||
private Inet6AddressGroup address;
|
||||
private Inet6AddressGroup locator;
|
||||
private MonitorData monitor;
|
||||
private BandwidthDistributer<Inet6Address> bandwidthDistributer;
|
||||
public MonitorData getMonitor() {
|
||||
return monitor;
|
||||
}
|
||||
@@ -19,12 +22,21 @@ public class Neighbor {
|
||||
public Inet6AddressGroup getLocator() {
|
||||
return locator;
|
||||
}
|
||||
|
||||
public BandwidthDistributer<Inet6Address> getBandwidthDistributer() {
|
||||
return bandwidthDistributer;
|
||||
}
|
||||
public Neighbor(Inet6AddressGroup address, Inet6AddressGroup locator, MonitorData monitor) {
|
||||
super();
|
||||
this.address = address;
|
||||
this.locator = locator;
|
||||
this.monitor = monitor;
|
||||
}
|
||||
public Neighbor(Inet6AddressGroup peerAddress, Inet6AddressGroup remoteVaddr, QueueingMonitorDataImpl monitor2,
|
||||
BandwidthDistributer<Inet6Address> bandwidthDistributer) {
|
||||
this(peerAddress,remoteVaddr,monitor2);
|
||||
this.bandwidthDistributer=bandwidthDistributer;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Neighbor [address=" + address + ", locator=" + locator + ", monitor=" + monitor + "]";
|
||||
|
||||
@@ -11,7 +11,7 @@ import org.kne.cloud.network.monitor.DelayMonitorData;
|
||||
import org.kne.cloud.network.monitor.MonitorData;
|
||||
import org.kne.cloud.network.monitor.QueueingMonitorDataImpl;
|
||||
|
||||
public class RouteItem implements Comparable<RouteItem>{
|
||||
public class RouteItem implements Comparable<RouteItem>,Cloneable{
|
||||
private Inet6AddressGroup destination;
|
||||
private Inet6Address nexthop;
|
||||
private IPv6NetworkLink destlink;
|
||||
@@ -20,6 +20,7 @@ public class RouteItem implements Comparable<RouteItem>{
|
||||
private long cost;
|
||||
private String flag;
|
||||
private MonitorData monitor;
|
||||
private boolean isLoopback;
|
||||
public String getFlag() {
|
||||
return flag;
|
||||
}
|
||||
@@ -44,7 +45,7 @@ public class RouteItem implements Comparable<RouteItem>{
|
||||
&& pre == other.pre && Objects.equals(proto, other.proto);
|
||||
}
|
||||
public RouteItem(Inet6AddressGroup destination, Inet6Address nexthop, IPv6NetworkLink destlink, String proto, int pre,
|
||||
long cost,String flag) {
|
||||
long cost,String flag,boolean isLoopback) {
|
||||
super();
|
||||
this.destination = destination;
|
||||
this.nexthop = nexthop;
|
||||
@@ -53,9 +54,10 @@ public class RouteItem implements Comparable<RouteItem>{
|
||||
this.pre = pre;
|
||||
this.cost = cost;
|
||||
this.flag=flag;
|
||||
this.isLoopback=isLoopback;
|
||||
}
|
||||
public RouteItem(Inet6AddressGroup destination, Inet6Address nexthop, IPv6NetworkLink destlink, String proto, int pre,
|
||||
long cost,MonitorData monitor,String flag) {
|
||||
long cost,MonitorData monitor,String flag,boolean isLoopback) {
|
||||
super();
|
||||
this.destination = destination;
|
||||
this.nexthop = nexthop;
|
||||
@@ -65,6 +67,7 @@ public class RouteItem implements Comparable<RouteItem>{
|
||||
this.cost = cost;
|
||||
this.monitor=monitor;
|
||||
this.flag=flag;
|
||||
this.isLoopback=isLoopback;
|
||||
}
|
||||
public Inet6AddressGroup getDestination() {
|
||||
return destination;
|
||||
@@ -81,6 +84,9 @@ public class RouteItem implements Comparable<RouteItem>{
|
||||
public int getPre() {
|
||||
return pre;
|
||||
}
|
||||
public boolean isLoopback() {
|
||||
return isLoopback;
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return getDestination()+"\t"+getProto()+"\t"+getPre()+"\t"+getCost()+"\t"+getFlag()+"\t"+getNexthop().getHostAddress()+"\t"+getDestlink().getName();
|
||||
@@ -91,6 +97,9 @@ public class RouteItem implements Comparable<RouteItem>{
|
||||
public boolean checkMatch(Inet6Address ia) {
|
||||
return destination.checkMatch(ia);
|
||||
}
|
||||
public boolean checkMatch(byte[] ia) {
|
||||
return destination.checkMatch(ia);
|
||||
}
|
||||
@Override
|
||||
public int compareTo(RouteItem o) {
|
||||
int v1=destination.compareTo(o.destination);
|
||||
@@ -105,7 +114,35 @@ public class RouteItem implements Comparable<RouteItem>{
|
||||
if(monitor==null||o.monitor==null||(!(monitor instanceof DelayMonitorData))||(!(o.monitor instanceof DelayMonitorData)))
|
||||
return v3;
|
||||
if(!(monitor instanceof QueueingMonitorDataImpl)||!(o.monitor instanceof QueueingMonitorDataImpl))
|
||||
return Long.compare(((DelayMonitorData)monitor).getOutDelay(), ((DelayMonitorData)o.monitor).getOutDelay());
|
||||
return Long.compare(((DelayMonitorData)monitor).getOutDelay()+((QueueingMonitorDataImpl)monitor).getQueueingDelay(), ((DelayMonitorData)o.monitor).getOutDelay()+((QueueingMonitorDataImpl)o.monitor).getQueueingDelay());
|
||||
return Long.compare(outDelay, o.outDelay);
|
||||
return Long.compare(outDelay+queueingDelay, o.outDelay+o.queueingDelay);
|
||||
}
|
||||
@Override
|
||||
public Object clone() {
|
||||
try {
|
||||
RouteItem ri=(RouteItem) super.clone();
|
||||
if(monitor!=null)
|
||||
ri.monitor=(MonitorData) monitor.clone();
|
||||
return ri;
|
||||
} catch (CloneNotSupportedException e) {
|
||||
e.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
long outDelay;
|
||||
long queueingDelay;
|
||||
public void preSort() {
|
||||
if(monitor!=null) {
|
||||
outDelay=((DelayMonitorData)monitor).getOutDelay();
|
||||
if(monitor instanceof QueueingMonitorDataImpl) {
|
||||
queueingDelay=((QueueingMonitorDataImpl)monitor).getQueueingDelay();
|
||||
}
|
||||
}
|
||||
}
|
||||
public boolean ECMPequals(RouteItem prevr) {
|
||||
return prevr.getDestination().getPrefixLength()==getDestination().getPrefixLength()&&prevr.getPre()==getPre()&&prevr.getCost()==getCost();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package org.kne.cloud.network.ipv6;
|
||||
|
||||
import java.io.EOFException;
|
||||
import java.io.IOException;
|
||||
import java.nio.ByteBuffer;
|
||||
import java.nio.channels.ReadableByteChannel;
|
||||
import java.nio.channels.WritableByteChannel;
|
||||
|
||||
import org.kne.cloud.network.NetworkPacket;
|
||||
|
||||
public class TLV extends NetworkPacket{
|
||||
|
||||
private int headerLength=2;
|
||||
|
||||
public int getHeaderLength() {
|
||||
return headerLength;
|
||||
}
|
||||
|
||||
private boolean isDefault;
|
||||
|
||||
protected volatile ByteBuffer header;
|
||||
|
||||
private ByteBuffer data;
|
||||
|
||||
|
||||
|
||||
public ByteBuffer getData() {
|
||||
return data;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLength() {
|
||||
return headerLength+(isDefault?data.limit():0);
|
||||
}
|
||||
@Override
|
||||
public void writeToChannel(WritableByteChannel dto) throws IOException {
|
||||
if(isDefault&&(headerLength>1)) {
|
||||
setDataLength(data.limit());
|
||||
}
|
||||
header.limit(headerLength);
|
||||
dto.write(header.slice(0,header.limit()));
|
||||
if(isDefault&&(headerLength>1)) {
|
||||
dto.write(data.slice(0, data.limit()));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void readFromChannel(ReadableByteChannel din, long length) throws IOException {
|
||||
header.limit(1);
|
||||
while (header.hasRemaining()) {
|
||||
if (din.read(header) == -1) {
|
||||
throw new EOFException();
|
||||
}
|
||||
}
|
||||
if(headerLength>1) {
|
||||
header.limit(2);
|
||||
while (header.hasRemaining()) {
|
||||
if (din.read(header) == -1) {
|
||||
throw new EOFException();
|
||||
}
|
||||
}
|
||||
}
|
||||
header.flip();
|
||||
|
||||
if(isDefault&&(headerLength>1)) {
|
||||
data.clear();
|
||||
data.limit(getDataLength());
|
||||
while(data.hasRemaining()){
|
||||
if(din.read(data)==-1) {
|
||||
throw new EOFException();
|
||||
}
|
||||
}
|
||||
data.flip();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean needEndPosition() {
|
||||
return false;
|
||||
}
|
||||
|
||||
public int getType() {
|
||||
return header.get(0)&0xff;
|
||||
}
|
||||
public void setType(int type) {
|
||||
header.put(0,(byte) type);
|
||||
}
|
||||
|
||||
public int getDataLength() {
|
||||
return header.get(1)&0xff;
|
||||
}
|
||||
|
||||
public void setDataLength(int dataLength) {
|
||||
header.put(1,(byte) dataLength);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user