Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1#!/usr/bin/env bash2#3# Copyright (c) 2017-present The Bitcoin Core developers4# Distributed under the MIT software license, see the accompanying5# file COPYING or http://www.opensource.org/licenses/mit-license.php.7export LC_ALL=C.UTF-88#network interface on which to limit traffic9IF="eth0"10#limit of the network interface in question11LINKCEIL="1gbit"12#limit outbound Bitcoin protocol traffic to this rate13LIMIT="160kbit"14#defines the IPv4 address space for which you wish to disable rate limiting15LOCALNET_V4="192.168.0.0/16"16#defines the IPv6 address space for which you wish to disable rate limiting17LOCALNET_V6="fe80::/10"19#delete existing rules ('Error: Cannot delete qdisc with handle of zero.' means there weren't any.)20tc qdisc del dev ${IF} root22#add root class23tc qdisc add dev ${IF} root handle 1: htb default 1025#add parent class26tc class add dev ${IF} parent 1: classid 1:1 htb rate ${LINKCEIL} ceil ${LINKCEIL}28#add our two classes. one unlimited, another limited29tc class add dev ${IF} parent 1:1 classid 1:10 htb rate ${LINKCEIL} ceil ${LINKCEIL} prio 030tc class add dev ${IF} parent 1:1 classid 1:11 htb rate ${LIMIT} ceil ${LIMIT} prio 132#add handles to our classes so packets marked with <x> go into the class with "... handle <x> fw ..."33tc filter add dev ${IF} parent 1: protocol ip prio 1 handle 1 fw classid 1:1034tc filter add dev ${IF} parent 1: protocol ip prio 2 handle 2 fw classid 1:1136if [ -n "${LOCALNET_V6}" ] ; then37# v6 cannot have the same priority value as v438tc filter add dev ${IF} parent 1: protocol ipv6 prio 3 handle 1 fw classid 1:1039tc filter add dev ${IF} parent 1: protocol ipv6 prio 4 handle 2 fw classid 1:1140fi42#delete any existing rules43#disable for now44#ret=045#while [ $ret -eq 0 ]; do46# iptables -t mangle -D OUTPUT 147# ret=$?48#done50#limit outgoing traffic to and from port 8333. but not when dealing with a host on the local network51# (defined by $LOCALNET_V4 and $LOCALNET_V6)52# --set-mark marks packages matching these criteria with the number "2" (v4)53# --set-mark marks packages matching these criteria with the number "4" (v6)54# these packets are filtered by the tc filter with "handle 2"55# this filter sends the packages into the 1:11 class, and this class is limited to ${LIMIT}56iptables -t mangle -A OUTPUT -p tcp -m tcp --dport 8333 ! -d ${LOCALNET_V4} -j MARK --set-mark 0x257iptables -t mangle -A OUTPUT -p tcp -m tcp --sport 8333 ! -d ${LOCALNET_V4} -j MARK --set-mark 0x259if [ -n "${LOCALNET_V6}" ] ; then60ip6tables -t mangle -A OUTPUT -p tcp -m tcp --dport 8333 ! -d ${LOCALNET_V6} -j MARK --set-mark 0x461ip6tables -t mangle -A OUTPUT -p tcp -m tcp --sport 8333 ! -d ${LOCALNET_V6} -j MARK --set-mark 0x462fi
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.