Increasing Network Speed with the example of Raspberry Pi4 and Raspbian

I could increase the maximum internet download speed of my Raspberry Pi4 running with the most current Raspbian 64 bit kernel from 60-70 MB/s to a bit over 90 MB/s with the following adjustments. Please note many servers limit downloads e.g. to 400 Mbit/s, even speed test servers.

1. A bash script - you could post this into /etc/rc.local:
# network speed
iface=eth0

# speed up internet - https://datatag.web.cern.ch/datatag/howto/tcp.html
/sbin/ifconfig $iface txqueuelen 8000
echo 8000 > /proc/sys/net/core/netdev_max_backlog
ip link set $iface qlen 20000

# google bbr - https://www.tecmint.com/increase-linux-server-internet-speed-with-tcp-bbr/
sysctl -w net.core.default_qdisc=fq
sysctl -w net.ipv4.tcp_congestion_control=bbr

# https://www.cyberciti.biz/faq/rhel-centos-fedora-debian-configure-rx-polling-mode/
ethtool -C $iface adaptive-rx on

# https://cromwell-intl.com/open-source/performance-tuning/ethernet.html
ethtool -K $iface tx-checksum-ipv4 on
ethtool -K $iface tx-checksum-ipv6 on

# not supported https://cromwell-intl.com/open-source/performance-tuning/ethernet.html
# ethtool -A $iface rx on
# ethtool -A $iface tx on
# ethtool -G $iface rx 4096 tx 4096

Kernel tweaks, you could put this in /etc/sysctl.d/99-netspeed.conf

# https://serverfault.com/a/758350
kernel.sem = 350 358400 64 1024
net.core.rmem_default = 262144
net.core.rmem_max = 4194304
net.core.wmem_default = 262144
net.core.wmem_max = 4194304
net.ipv4.tcp_window_scaling = 1
net.ipv4.tcp_adv_win_scale = 2
net.ipv4.tcp_moderate_rcvbuf = 1
net.ipv4.tcp_rmem = 4096 262144 4194304
net.ipv4.tcp_wmem = 4096 262144 4194304
net.ipv4.tcp_keepalive_time = 900
net.ipv4.tcp_keepalive_intvl = 900
net.ipv4.tcp_keepalive_probes = 9

3. Installing irqbalance:
sudo apt install irqbalance

4. Reboot

Enjoy!

Enable fstrim / TRIM / discard support especially for USB SATA disks in Linux

If fstrim tells you "discard operation is not supported" on a usb disk then you may want to try this tip. I'm assuming you're using /dev/sdb below, please adjust.

Check if this commands
sg_readcap -l /dev/sdb | grep unmap
shows "Unmap command supported (LBPU): 1". It must be  a 1 at some point or you're out of luck.

If that works. Try unmounting the drive. Then try this command as root:
umount /media/disk
echo unmap > /sys/block/sdb/device/scsi_disk/*/provisioning_mode

Now try again to see if it works:
mount /dev/sdb2 /media/disk
fstrim -v /media/diskidea

If that works for you, find out the device ID for the usb device with:
lsusb

and then post the IDs into a udev rule (exampe ID is the StarTech USB 3.0 to 2,5" SATA cable, which I've bought for my Raspberry Pi 4):
echo 'ACTION=="add|change", ATTRS{idVendor}=="174c", ATTRS{idProduct}=="55aa", SUBSYSTEM=="scsi_disk", ATTR{provisioning_mode}="unmap"' > /etc/udev/rules.d/99-trim-ssd-discard.rules

Hope this works for you! Thanks to the original idea!