+
diff --git a/apps/i2psnark/java/src/org/klomp/snark/PeerCheckerTask.java b/apps/i2psnark/java/src/org/klomp/snark/PeerCheckerTask.java index 57edb5577..278971a7d 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/PeerCheckerTask.java +++ b/apps/i2psnark/java/src/org/klomp/snark/PeerCheckerTask.java @@ -37,6 +37,8 @@ class PeerCheckerTask extends TimerTask this.coordinator = coordinator; } + private Random random = new Random(); + public void run() { synchronized(coordinator.peers) @@ -105,13 +107,19 @@ class PeerCheckerTask extends TimerTask + " C: " + peer.isChoked(), Snark.DEBUG); + // Choke half of them rather than all so it isn't so drastic... + // unless this torrent is over the limit all by itself. + boolean overBWLimitChoke = upload > 0 && + ((overBWLimit && random.nextBoolean()) || + (coordinator.overUpBWLimit(uploaded))); + // If we are at our max uploaders and we have lots of other // interested peers try to make some room. // (Note use of coordinator.uploaders) if (((coordinator.uploaders == uploadLimit && coordinator.interestedAndChoking > 0) || coordinator.uploaders > uploadLimit - || overBWLimit) + || overBWLimitChoke) && !peer.isChoking()) { // Check if it still wants pieces from us. @@ -127,14 +135,18 @@ class PeerCheckerTask extends TimerTask it.remove(); removed.add(peer); } - else if (overBWLimit) + else if (overBWLimitChoke) { - Snark.debug("BW limit, choke peer: " + peer, + Snark.debug("BW limit (" + upload + "/" + uploaded + "), choke peer: " + peer, Snark.INFO); peer.setChoking(true); uploaders--; coordinator.uploaders--; removedCount++; + + // Put it at the back of the list for fairness, even though we won't be unchoking this time + it.remove(); + removed.add(peer); } else if (peer.isInteresting() && peer.isChoked()) { @@ -220,7 +232,7 @@ class PeerCheckerTask extends TimerTask } // Optimistically unchoke a peer - if (!overBWLimit) + if ((!overBWLimit) && !coordinator.overUpBWLimit(uploaded)) coordinator.unchokePeer(); // Put peers back at the end of the list that we removed earlier. diff --git a/apps/i2psnark/java/src/org/klomp/snark/PeerConnectionOut.java b/apps/i2psnark/java/src/org/klomp/snark/PeerConnectionOut.java index 14527bd32..275a05c35 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/PeerConnectionOut.java +++ b/apps/i2psnark/java/src/org/klomp/snark/PeerConnectionOut.java @@ -392,6 +392,23 @@ class PeerConnectionOut implements Runnable req.sendTime = System.currentTimeMillis(); } + // Used by PeerState to limit pipelined requests + int queuedBytes() + { + int total = 0; + synchronized(sendQueue) + { + Iterator it = sendQueue.iterator(); + while (it.hasNext()) + { + Message m = (Message)it.next(); + if (m.type == Message.PIECE) + total += m.length; + } + } + return total; + } + void sendPiece(int piece, int begin, int length, byte[] bytes) { Message m = new Message(); diff --git a/apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java b/apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java index 8efd2332b..2304f050b 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java +++ b/apps/i2psnark/java/src/org/klomp/snark/PeerCoordinator.java @@ -38,8 +38,8 @@ public class PeerCoordinator implements PeerListener // package local for access by CheckDownLoadersTask final static long CHECK_PERIOD = 40*1000; // 40 seconds - final static int MAX_CONNECTIONS = 24; - final static int MAX_UPLOADERS = 4; + final static int MAX_CONNECTIONS = 16; + final static int MAX_UPLOADERS = 6; // Approximation of the number of current uploaders. // Resynced by PeerChecker once in a while. @@ -286,6 +286,14 @@ public class PeerCoordinator implements PeerListener // toDisconnect = peer to get out of synchronized(peers) peer.disconnect(false); // Don't deregister this connection/peer. } + // This is already checked in addPeer() but we could have gone over the limit since then + else if (peers.size() >= MAX_CONNECTIONS) + { + if (_log.shouldLog(Log.WARN)) + _log.warn("Already at MAX_CONNECTIONS in connected() with peer: " + peer); + // toDisconnect = peer to get out of synchronized(peers) + peer.disconnect(false); + } else { if (_log.shouldLog(Log.INFO)) @@ -307,6 +315,7 @@ public class PeerCoordinator implements PeerListener } } + // caller must synchronize on peers private static Peer peerIDInList(PeerID pid, List peers) { Iterator it = peers.iterator(); @@ -328,9 +337,13 @@ public class PeerCoordinator implements PeerListener } boolean need_more; + int peersize = 0; synchronized(peers) { - need_more = !peer.isConnected() && peers.size() < MAX_CONNECTIONS; + peersize = peers.size(); + // This isn't a strict limit, as we may have several pending connections; + // thus there is an additional check in connected() + need_more = (!peer.isConnected()) && peersize < MAX_CONNECTIONS; // Check if we already have this peer before we build the connection Peer old = peerIDInList(peer.getPeerID(), peers); need_more = need_more && ((old == null) || (old.getInactiveTime() > 8*60*1000)); @@ -354,15 +367,14 @@ public class PeerCoordinator implements PeerListener new I2PThread(r, threadName).start(); return true; } - else - if (_log.shouldLog(Log.DEBUG)) { - if (peer.isConnected()) - _log.info("Add peer already connected: " + peer); - else - _log.info("MAX_CONNECTIONS = " + MAX_CONNECTIONS - + " not accepting extra peer: " + peer); - } - return false; + if (_log.shouldLog(Log.DEBUG)) { + if (peer.isConnected()) + _log.info("Add peer already connected: " + peer); + else + _log.info("Connections: " + peersize + "/" + MAX_CONNECTIONS + + " not accepting extra peer: " + peer); + } + return false; } @@ -842,5 +854,10 @@ public class PeerCoordinator implements PeerListener { return Snark.overUpBWLimit(); } + + public boolean overUpBWLimit(long total) + { + return Snark.overUpBWLimit(total * 1000 / CHECK_PERIOD); + } } diff --git a/apps/i2psnark/java/src/org/klomp/snark/PeerState.java b/apps/i2psnark/java/src/org/klomp/snark/PeerState.java index dd38ac357..8f8b7bbc6 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/PeerState.java +++ b/apps/i2psnark/java/src/org/klomp/snark/PeerState.java @@ -62,7 +62,8 @@ class PeerState // If we have te resend outstanding requests (true after we got choked). private boolean resend = false; - private final static int MAX_PIPELINE = 2; + private final static int MAX_PIPELINE = 2; // this is for outbound requests + private final static int MAX_PIPELINE_BYTES = 128*1024; // this is for inbound requests private final static int PARTSIZE = 32*1024; // Snark was 16K, i2p-bt uses 64KB private final static int MAX_PARTSIZE = 64*1024; // Don't let anybody request more than this @@ -185,6 +186,15 @@ class PeerState return; } + // Limit total pipelined requests to MAX_PIPELINE bytes + // to conserve memory and prevent DOS + if (out.queuedBytes() + length > MAX_PIPELINE_BYTES) + { + if (_log.shouldLog(Log.WARN)) + _log.warn("Discarding request over pipeline limit from " + peer); + return; + } + byte[] pieceBytes = listener.gotRequest(peer, piece, begin, length); if (pieceBytes == null) { diff --git a/apps/i2psnark/java/src/org/klomp/snark/Snark.java b/apps/i2psnark/java/src/org/klomp/snark/Snark.java index f0fd66eaf..f7ea7f0fa 100644 --- a/apps/i2psnark/java/src/org/klomp/snark/Snark.java +++ b/apps/i2psnark/java/src/org/klomp/snark/Snark.java @@ -791,4 +791,9 @@ public class Snark Snark.debug("Total up bw: " + total + " Limit: " + limit, Snark.WARNING); return total > limit; } + + public static boolean overUpBWLimit(long total) { + long limit = 1024l * I2PSnarkUtil.instance().getMaxUpBW(); + return total > limit; + } } diff --git a/apps/routerconsole/jsp/help.jsp b/apps/routerconsole/jsp/help.jsp index 41103adaf..b1319ffd7 100644 --- a/apps/routerconsole/jsp/help.jsp +++ b/apps/routerconsole/jsp/help.jsp @@ -11,7 +11,10 @@ <%@include file="summary.jsp" %>