forked from KNEMC/KLALB
KLALB V3.4
This commit is contained in:
@@ -15,6 +15,7 @@ import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.locks.LockSupport;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import org.kne.cloud.network.ByteBufferAllocator;
|
||||
import org.kne.cloud.network.NetworkPacket;
|
||||
@@ -23,6 +24,8 @@ import org.kne.cloud.network.ThreadTool;
|
||||
import org.kne.cloud.network.VirtualRawSocketImpl;
|
||||
import org.kne.cloud.network.ipv6.IPv6Packet;
|
||||
import org.kne.cloud.network.ipv6.IPv6Packet.IPv6Payload;
|
||||
import org.kne.cloud.network.ipv6.Inet6AddressGroup;
|
||||
import org.kne.cloud.network.ipv6.LoopbackIPv6NetworkLink;
|
||||
import org.kne.cloud.network.srv6.PacketConsumer;
|
||||
import org.kne.cloud.network.srv6.SRv6Router;
|
||||
import org.kne.concurrent.HighPerformanceExecutor;
|
||||
@@ -32,7 +35,7 @@ public class KLALBVirtualRawSocketImpl extends VirtualRawSocketImpl implements P
|
||||
|
||||
private boolean ipHeaderInclude=false;
|
||||
|
||||
private SRv6Router router;
|
||||
private LoopbackIPv6NetworkLink link;
|
||||
|
||||
|
||||
protected volatile Inet6Address remoteaddr;
|
||||
@@ -42,13 +45,13 @@ public class KLALBVirtualRawSocketImpl extends VirtualRawSocketImpl implements P
|
||||
|
||||
private volatile int inputchachesize = 1024*1024;
|
||||
|
||||
protected SRv6Router getRouter() {
|
||||
return router;
|
||||
protected LoopbackIPv6NetworkLink getLoopbackLink() {
|
||||
return link;
|
||||
}
|
||||
|
||||
public KLALBVirtualRawSocketImpl(SRv6Router router) {
|
||||
public KLALBVirtualRawSocketImpl(LoopbackIPv6NetworkLink link) {
|
||||
super();
|
||||
this.router = router;
|
||||
this.link = link;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -116,7 +119,7 @@ public class KLALBVirtualRawSocketImpl extends VirtualRawSocketImpl implements P
|
||||
|
||||
@Override
|
||||
protected void close() {
|
||||
router.getProtocolNumberRegister().remove(bindProtocolNumber);
|
||||
link.getProtocolNumberRegister().remove(bindProtocolNumber);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -134,13 +137,22 @@ public class KLALBVirtualRawSocketImpl extends VirtualRawSocketImpl implements P
|
||||
if (!(address instanceof Inet6Address)) {
|
||||
throw new IllegalArgumentException("invalid address type, KLALB socket can only use IPV6 address");
|
||||
}
|
||||
if ((!address.isAnyLocalAddress()) && (!address.equals(router.getLocator().getAddress()))) {
|
||||
if (!address.isAnyLocalAddress() ) {
|
||||
boolean contains=false;
|
||||
for(Inet6AddressGroup adg:link.getAddressGroups()) {
|
||||
if(adg.checkMatch((Inet6Address) address)) {
|
||||
contains=true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if(!contains) {
|
||||
throw new BindException("must bind to self");
|
||||
}
|
||||
}
|
||||
|
||||
localaddr=(Inet6Address) address;
|
||||
this.bindProtocolNumber=protocolNumber;
|
||||
if(router.getProtocolNumberRegister().putIfAbsent(protocolNumber, this)!=null) {
|
||||
if(link.getProtocolNumberRegister().putIfAbsent(protocolNumber, this)!=null) {
|
||||
throw new BindException("protocol number already bind");
|
||||
}
|
||||
}
|
||||
@@ -181,7 +193,11 @@ public class KLALBVirtualRawSocketImpl extends VirtualRawSocketImpl implements P
|
||||
ipv.setTrafficClass(0);
|
||||
ipv.setFlowLabel(0);
|
||||
ipv.setHopLimit(255);
|
||||
ipv.setSourceAddress(router.getLocator().getAddress());
|
||||
if(!localaddr.isAnyLocalAddress()) {
|
||||
ipv.setSourceAddress(localaddr);
|
||||
}else {
|
||||
ipv.setSourceAddress(link.getRouter().getLocator().getAddress());
|
||||
}
|
||||
InetAddress address= p.getAddress();
|
||||
if (!(address instanceof Inet6Address)) {
|
||||
throw new IllegalArgumentException("invalid address type, KLALB socket can only use IPV6 address");
|
||||
@@ -193,7 +209,10 @@ public class KLALBVirtualRawSocketImpl extends VirtualRawSocketImpl implements P
|
||||
pl.getData().put(buf);
|
||||
pl.getData().flip();
|
||||
ipv.setPayload(pl);
|
||||
router.insertSRHandRoutePacket(ipv);
|
||||
Consumer<IPv6Packet>cons=link.getReceiveConsumer();
|
||||
if(cons!=null) {
|
||||
cons.accept(ipv);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -213,7 +232,7 @@ public class KLALBVirtualRawSocketImpl extends VirtualRawSocketImpl implements P
|
||||
p.setAddress(pack.getSourceAddress());
|
||||
ByteBuffer buffer= ByteBuffer.wrap(p.getData(),p.getOffset(),p.getLength());
|
||||
if(ipHeaderInclude) {
|
||||
|
||||
|
||||
}else {
|
||||
try {
|
||||
pack.getPayload().writeToChannel(KNEChannels.newWritableChannel(buffer));
|
||||
@@ -236,7 +255,7 @@ public class KLALBVirtualRawSocketImpl extends VirtualRawSocketImpl implements P
|
||||
while(true) {
|
||||
pol=recvQueue.peek();
|
||||
if(pol!=null) {
|
||||
recvQueueUsed.addAndGet((int) -pol.getPayload().getLength());
|
||||
recvQueueUsed.addAndGet((int) -pol.getPayload().getTotalLength());
|
||||
return pol;
|
||||
}
|
||||
parkThread=Thread.currentThread();
|
||||
@@ -248,7 +267,7 @@ public class KLALBVirtualRawSocketImpl extends VirtualRawSocketImpl implements P
|
||||
while(true) {
|
||||
pol=recvQueue.poll();
|
||||
if(pol!=null) {
|
||||
recvQueueUsed.addAndGet((int) -pol.getPayload().getLength());
|
||||
recvQueueUsed.addAndGet((int) -pol.getPayload().getTotalLength());
|
||||
return pol;
|
||||
}
|
||||
parkThread=Thread.currentThread();
|
||||
@@ -259,14 +278,14 @@ public class KLALBVirtualRawSocketImpl extends VirtualRawSocketImpl implements P
|
||||
private AtomicInteger recvQueueUsed=new AtomicInteger(0);
|
||||
|
||||
@Override
|
||||
public void accept(IPv6Packet packx) throws IOException {
|
||||
public boolean accept(IPv6Packet packx) throws IOException {
|
||||
if(recvQueueUsed.get()<=inputchachesize) {
|
||||
if(recvQueue.offer(packx)) {
|
||||
recvQueueUsed.addAndGet((int) packx.getPayload().getLength());
|
||||
recvQueueUsed.addAndGet((int) packx.getPayload().getTotalLength());
|
||||
LockSupport.unpark(parkThread);
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user