Last active 1731073054

tc priority setter

example.json Raw
1{
2 "eth0": {
3 "high": [22,80,443],
4 "medium": [8096,9091],
5 "low": [51413]
6 }
7}
netprio.sh Raw
1#!/bin/bash
2
3# Check if jq is installed
4if ! command -v jq &> /dev/null
5then
6 echo "jq is not installed. Please install it before running the script."
7 exit 1
8fi
9
10# Check if a JSON file is provided as an argument
11if [ -z "$1" ]; then
12 echo "Please provide the JSON configuration file as an argument."
13 exit 1
14fi
15
16# Read the JSON file
17JSON_FILE=$1
18
19# Apply rules for each network interface (eno1, wg0, etc.)
20for interface in $(jq -r 'keys[]' "$JSON_FILE"); do
21 echo "Applying rules for interface $interface..."
22
23 # Delete existing qdisc if it exists
24 tc qdisc del dev "$interface" root 2>/dev/null
25
26 # Set the prio queue for the interface
27 tc qdisc add dev "$interface" root handle 1: prio
28
29 # Set high priority ports
30 high_ports=$(jq -r ".\"$interface\".high[]" "$JSON_FILE")
31 for port in $high_ports; do
32 echo " Setting port $port as high priority on $interface..."
33 tc filter add dev "$interface" protocol ip parent 1: prio 0 u32 match ip dport "$port" 0xffff flowid 1:0
34 done
35
36 # Set medium priority ports
37 medium_ports=$(jq -r ".\"$interface\".medium[]" "$JSON_FILE")
38 for port in $medium_ports; do
39 echo " Setting port $port as medium priority on $interface..."
40 tc filter add dev "$interface" protocol ip parent 1: prio 1 u32 match ip dport "$port" 0xffff flowid 1:1
41 done
42
43 # Set low priority ports
44 low_ports=$(jq -r ".\"$interface\".low[]" "$JSON_FILE")
45 for port in $low_ports; do
46 echo " Setting port $port as low priority on $interface..."
47 tc filter add dev "$interface" protocol ip parent 1: prio 2 u32 match ip dport "$port" 0xffff flowid 1:2
48 done
49done
50
51echo "Rules have been successfully applied."