iopsys-feed/qosmngr/files/broadcom/lib/qos/qos.sh
2023-01-18 06:16:29 +00:00

1398 lines
35 KiB
Bash
Executable file

#!/bin/sh
. /lib/functions.sh
include /lib/ethernet
# include common code
. /lib/qos/ip_rule.sh
IP_RULE=""
BR_RULE=""
BR6_RULE=""
POLICER_COUNT=0
Q_COUNT=0
SP_Q_PRIO=7
#counter variable to assign classify order value if not added in config
temp_order=1
cfg_name=""
cfg_type=""
# Function to handle a queue order and
# update total number of queues
handle_q_order() {
local qid="$1" #queue section ID
config_get is_enable "$qid" "enable" 1
# No need to configure disabled queues
if [ $is_enable == '0' ]; then
return
fi
config_get ifname "$qid" "ifname"
# If ifname is empty that is good enough to break
if [ -z "$ifname" ];then
return
fi
# Create precedence file containing queue order per
# interface.
local precedence_file="/tmp/qos/$ifname/q_order"
local q_no=$(cat /tmp/qos/$ifname/q_idx)
config_get precedence "$qid" "precedence"
value=${precedence}_q${q_no}
echo $value >> $precedence_file
# Update the number of queues per interface.
q_no=$((q_no + 1))
echo $q_no > /tmp/qos/$ifname/q_idx
}
# Sort queue, lower value in uci means higher precedence, so this
# function sorts the precedence in decending order
sort_q_by_precedence() {
ifname="$1"
local order_file="/tmp/qos/$ifname/q_order"
local tmp_order_file="/tmp/qos/$ifname/q_order.tmp"
sort -n -k1 $order_file > $tmp_order_file
cp $tmp_order_file $order_file
rm -f $tmp_order_file
}
sort_by_precedence() {
for interf in $(db -q get hw.board.ethernetPortOrder); do
sort_q_by_precedence $interf
done
}
# function to handle a queue section shapingrate
handle_queue_shapingrate() {
local qid="$1" #queue section ID
local intf_name="$2"
config_get is_enable "$qid" "enable"
# no need to configure disabled queues
if [ $is_enable == '0' ]; then
return
fi
config_get ifname "$qid" "ifname"
# if ifname is empty that is good enough to break
if [ -z "$ifname" ];then
return
fi
# This is to get the qid per interface.
if [ "$intf_name" != "$ifname" ]; then
return
fi
config_get rate "$qid" "rate"
config_get bs "$qid" "burst_size"
# Call tmctl which is a broadcomm command to configure queues shapingrate.
tmctl setqcfg --devtype 0 --if $ifname --qid $Q_COUNT --shapingrate $rate --burstsize $bs &>/dev/null
Q_COUNT=$((Q_COUNT + 1))
}
# function to handle a queue section
handle_queue() {
local qid="$1" #queue section ID
local intf_name="$2"
config_get is_enable "$qid" "enable"
# no need to configure disabled queues
if [ $is_enable == '0' ]; then
return
fi
config_get ifname "$qid" "ifname"
# if ifname is empty that is good enough to break
if [ -z "$ifname" ];then
return
fi
# This is to get the qid per interface.
if [ "$intf_name" != "$ifname" ]; then
return
fi
local precedence_file="/tmp/qos/$ifname/q_order"
local temp_order=0
while read -r line; do
line_qid=${line: -1}
if [ "$line_qid" == "$Q_COUNT" ]; then
break
fi
temp_order=$((temp_order + 1))
done < "$precedence_file"
# for sp queue, no matter what the precedence value is configured in the uci file
# broadcom recommends that the highest precedence queue should have priority value
#7, the next priority q 6 and so on.
# Here, we have the index of the queue when sorted by precedence value in the file
# precedence_file so the order is calculated accordingly.
local order=`expr $SP_Q_PRIO - $temp_order`
config_get sc_alg "$qid" "scheduling"
config_get wgt "$qid" "weight" 1
config_get rate "$qid" "rate"
config_get bs "$qid" "burst_size"
config_get qsize "$qid" "queue_size" 1024
local salg=1
case "$sc_alg" in
"SP") salg=1
;;
"WRR") salg=2
;;
"WDRR") salg=3
;;
"WFQ") salg=4
;;
esac
# ignore precedence value in case of WRR, broadcom recommends that WRR queue should
# always have precedence value set to 0
if [ $salg -eq 2 ]; then
order=0
fi
# Call tmctl which is a broadcomm command to configure queues on a port.
tmctl setqcfg --devtype 0 --if $ifname --qid $Q_COUNT --priority $order --qsize $qsize --weight $wgt --schedmode $salg --shapingrate $rate --burstsize $bs
Q_COUNT=$((Q_COUNT + 1))
}
#function to handle a policer section
handle_policer() {
local p_sec="$1" # policer section ID
local dir=1 # default direction, upstream
config_get is_enable "$p_sec" "enable"
#no need to configure disabled policer
if [ $is_enable == '0' ]; then
return
fi
config_get cir "$p_sec" "committed_rate"
config_get cbs "$p_sec" "committed_burst_size" -1
config_get ebs "$p_sec" "excess_burst_size" 0
config_get pir "$p_sec" "peak_rate" 0
config_get pbs "$p_sec" "peak_burst_size" 0
config_get meter "$p_sec" "meter_type" 0
# Call tmctl which is a broadcomm command to configure policer.
tmctl createpolicer --dir $dir --pid $POLICER_COUNT --ptype $meter --cir $cir --cbs $cbs --ebs $ebs --pir $pir --pbs $pbs
POLICER_COUNT=$((POLICER_COUNT + 1))
}
#function to handle a shaper section
handle_shaper() {
sid="$1" #queue section ID
config_get is_enable "$sid" "enable"
# no need to configure disabled queues
if [ $is_enable == '0' ]; then
return
fi
config_get ifname "$sid" "ifname"
# if ifname is empty that is good enough to break
if [ -z "$ifname" ];then
return
fi
config_get rate "$sid" "rate"
# Convert the rate from bps to kbps.
if [ $rate -lt 1000 ];then
return
fi
rate=$(($rate / 1000))
config_get bs "$sid" "burst_size"
tmctl setportshaper --devtype 0 --if $ifname --shapingrate $rate --burstsize $bs
}
setup_qos() {
if [ ! -d "/tmp/qos" ]; then
mkdir -p /tmp/qos
fi
if [ ! -f "/tmp/qos/qos" ]; then
touch /tmp/qos/qos
cp /etc/config/qos /tmp/qos/qos
fi
ebtables -t broute -N qos
ret=$?
if [ $ret -eq 0 ]; then
ebtables -t broute -I BROUTING -j qos
else
ebtables -t broute -D BROUTING -j qos
ebtables -t broute -I BROUTING -j qos
fi
iptables -w -t mangle -N qos_forward
ret=$?
[ $ret -eq 0 ] && iptables -w -t mangle -I FORWARD -j qos_forward
iptables -w -t mangle -N qos_prerouting
ret=$?
[ $ret -eq 0 ] && iptables -w -t mangle -I PREROUTING -j qos_prerouting
iptables -w -t mangle -N qos_output
ret=$?
[ $ret -eq 0 ] && iptables -w -t mangle -I OUTPUT -j qos_output
ip6tables -t mangle -N qos_forward
ret=$?
[ $ret -eq 0 ] && ip6tables -t mangle -I FORWARD -j qos_forward
ip6tables -t mangle -N qos_prerouting
ret=$?
[ $ret -eq 0 ] && ip6tables -t mangle -I PREROUTING -j qos_prerouting
ip6tables -t mangle -N qos_output
ret=$?
[ $ret -eq 0 ] && ip6tables -t mangle -I OUTPUT -j qos_output
}
flush_chains() {
echo "ebtables -t broute -F qos" > /tmp/qos/classify.ebtables
echo "iptables -w -t mangle -F qos_forward" > /tmp/qos/classify.iptables
echo "iptables -w -t mangle -F qos_prerouting" >> /tmp/qos/classify.iptables
echo "iptables -w -t mangle -F qos_output" >> /tmp/qos/classify.iptables
echo "ip6tables -w -t mangle -F qos_forward" > /tmp/qos/classify.ip6tables
echo "ip6tables -w -t mangle -F qos_prerouting" >> /tmp/qos/classify.ip6tables
echo "ip6tables -w -t mangle -F qos_output" >> /tmp/qos/classify.ip6tables
}
init_broute_rule() {
BR_RULE=""
BR6_RULE=""
}
broute_filter_on_src_if() {
BR_RULE="$BR_RULE --in-if $1"
}
broute_filter_on_src_mac() {
BR_RULE="$BR_RULE --src $1"
}
broute_filter_on_dst_mac() {
BR_RULE="$BR_RULE --dst $1"
}
broute_filter_on_pcp() {
# 5.04 onwards the vlan extension in ebtables is used for classification
# on the basis of vlan params which needs proto to be defined as 802_1Q in
# order to add a rule, now, proto can also be defined by specifying proto uci
# option as well as the rule may have a vlan id as well in which case the
# proto will already be present, hence, this check to not add --proto more
# than once
case "$BR_RULE" in
*proto*)
BR_RULE="$BR_RULE --vlan-prio $1"
;;
*)
BR_RULE="$BR_RULE --proto 802_1Q --vlan-prio $1"
;;
esac
}
broute_filter_on_ether_type() {
BR_RULE="$BR_RULE --proto $1"
}
broute_filter_on_ether_type6() {
BR6_RULE="$BR6_RULE --proto IPv6"
}
ebt_match_src_ip() {
BR_RULE="$BR_RULE --ip-src $1"
}
ebt_match_dst_ip() {
BR_RULE="$BR_RULE --ip-dst $1"
}
ebt_match_ipv6_src_ip() {
BR_RULE="$BR_RULE --ip6-src $1"
}
ebt_match_ipv6_dst_ip() {
BR_RULE="$BR_RULE --ip6-dst $1"
}
ebt_match_ip_src_port() {
BR_RULE="$BR_RULE --ip-source-port $1"
}
ebt_match_ip_dst_port() {
BR_RULE="$BR_RULE --ip-destination-port $1"
}
ebt_match_ipv6_src_port() {
#when ethertype is not configured by user then both proto rules of ipv4
#and ipv6 to be installed so update BR6_RULE string as well otherwise
#update BR_RULE only for installation of ipv6 proto rule only.
if [ ! -z "$BR6_RULE" ]; then
BR6_RULE="$BR6_RULE --ip6-source-port $1"
else
BR_RULE="$BR_RULE --ip6-source-port $1"
fi
}
ebt_match_ipv6_dst_port() {
#when ethertype is not configured by user then both proto rules of ipv4
#and ipv6 to be installed so update BR6_RULE string as well otherwise
#update BR_RULE only for installation of ipv6 proto rule only.
if [ ! -z "$BR6_RULE" ]; then
BR6_RULE="$BR6_RULE --ip6-destination-port $1"
else
BR_RULE="$BR_RULE --ip6-destination-port $1"
fi
}
ebt_match_ip_protocol() {
BR_RULE="$BR_RULE --ip-proto $1"
}
ebt_match_ipv6_protocol() {
#when ethertype is not configured by user then both proto rules of ipv4
#and ipv6 to be installed so update BR6_RULE string as well otherwise
#update BR_RULE only for installation of ipv6 proto rule only.
if [ ! -z "$BR6_RULE" ]; then
BR6_RULE="$BR6_RULE --ip6-proto $1"
else
BR_RULE="$BR_RULE --ip6-proto $1"
fi
}
ebt_match_ipv6_dscp() {
#when ethertype is not configured by user then both proto rules of ipv4
#and ipv6 to be installed so update BR6_RULE string as well otherwise
#update BR_RULE only for installation of ipv6 proto rule only.
if [ ! -z "$BR6_RULE" ]; then
BR6_RULE="$BR6_RULE --ip6-tclass $1"
else
BR_RULE="$BR_RULE --ip6-tclass $1"
fi
}
broute_filter_on_dscp() {
# The broadcom option --ip-dscp-extend actually accepts tos
# and not dscp and that too in hex, hence, perform the conversion
# from dscp in uci to tos first and then convert to hex
tos_val=$(($1<<2))
tos_hex=$(printf "%x" $tos_val)
BR_RULE="$BR_RULE --ip-dscp-extend $tos_hex"
}
broute_filter_on_vid() {
if [ $1 -lt 0 ]; then
return
fi
case "$BR_RULE" in
*proto*)
BR_RULE="$BR_RULE --vlan-id $1"
;;
*)
BR_RULE="$BR_RULE --proto 802_1Q --vlan-id $1"
;;
esac
}
broute_rule_set_traffic_class() {
#when ethertype is not configured by user then both proto rules of ipv4
#and ipv6 to be installed so update BR6_RULE string as well otherwise
#update BR_RULE only for installation of ipv6 proto rule only.
BR_RULE="$BR_RULE -j mark --mark-or 0x$1 --mark-target ACCEPT"
if [ ! -z "$BR6_RULE" ]; then
BR6_RULE="$BR6_RULE -j mark --mark-or 0x$1 --mark-target ACCEPT"
fi
}
broute_append_rule() {
#when ethertype is not configured by user then both proto rules of ipv4
#and ipv6 to be installed otherwise install ipv6 proto rule only.
echo "ebtables -t broute -A qos $BR_RULE" >> /tmp/qos/classify.ebtables
if [ -n "$BR6_RULE" ]; then
echo "ebtables -t broute -A qos $BR6_RULE" >> /tmp/qos/classify.ebtables
fi
}
set_ip_addr()
{
local cid=$1
local match_src_ip_func=$2
local match_dst_ip_func=$3
config_get src_ip "$cid" "src_ip"
config_get dst_ip "$cid" "dest_ip"
if [ ! -z "$src_ip" ]; then
$match_src_ip_func "$src_ip"
fi
if [ ! -z "$dst_ip" ]; then
$match_dst_ip_func "$dst_ip"
fi
}
set_ports()
{
local cid=$1
local match_src_port_func=$2
local match_dst_port_func=$3
local src_port=""
local dst_port=""
local src_port_range=""
local dst_port_range=""
config_get src_port "$cid" "src_port"
config_get dst_port "$cid" "dest_port"
config_get src_port_range "$cid" "src_port_range"
config_get dst_port_range "$cid" "dest_port_range"
if ! [ -z $src_port ] && ! [ -z $src_port_range ]; then
$match_src_port_func "$src_port:$src_port_range"
elif [ ! -z $src_port ]; then
$match_src_port_func $src_port
fi
if ! [ -z $dst_port ] && ! [ -z $dst_port_range ]; then
$match_dst_port_func "$dst_port:$dst_port_range"
elif [ ! -z $dst_port ]; then
$match_dst_port_func $dst_port
fi
}
protocol_string_to_num()
{
local value=-1
case "$1" in
*[0-9]*) value="$1"
;;
TCP|tcp) value=6
;;
UDP|udp) value=17
;;
ICMP|icmp) value=1
;;
ICMPv6|icmpv6) value=58
;;
IGMP|igmp) value=2
;;
SCTP|sctp) value=132
;;
*) value=-1
;;
esac
echo $value
}
broute_ipv4_rule_options()
{
local cid=$1
config_get protocol "$cid" "proto"
config_get dscp_filter "$cid" "dscp_filter"
set_ip_addr $cid ebt_match_src_ip ebt_match_dst_ip
if [ ! -z $dscp_filter ]; then
broute_filter_on_dscp "$dscp_filter"
fi
if [ ! -z $protocol ]; then
local proto_num=$(protocol_string_to_num "$protocol")
ebt_match_ip_protocol "$proto_num"
#port installation for protol tcp/udp/sctp
if [ $proto_num = "6" ] || [ $proto_num = "17" ] || [ $proto_num = "132" ] ; then
set_ports "$cid" ebt_match_ip_src_port ebt_match_ip_dst_port
fi
fi
}
broute_ipv6_rule_options()
{
local cid=$1
config_get protocol "$cid" "proto"
config_get dscp_filter "$cid" "dscp_filter"
set_ip_addr $cid ebt_match_ipv6_src_ip ebt_match_ipv6_dst_ip
if [ ! -z $dscp_filter ]; then
local tos_val
local tos_hex
tos_val=$(($dscp_filter<<2))
tos_hex=$(printf "%x" $tos_val)
ebt_match_ipv6_dscp "$tos_hex"
fi
if [ ! -z $protocol ]; then
local proto_num=$(protocol_string_to_num "$protocol")
ebt_match_ipv6_protocol "$proto_num"
#port installation for protol tcp/udp/sctp
if [ $proto_num = "6" ] || [ $proto_num = "17" ] || [ $proto_num = "132" ]; then
set_ports "$cid" ebt_match_ipv6_src_port ebt_match_ipv6_dst_port
fi
fi
}
handle_ebtables_rules() {
sid=$1
local is_l2_rule=0
local src_dhcp_options=""
local dst_dhcp_options=""
local protocol=""
local ip_version=""
init_broute_rule
config_get src_if "$sid" "ifname"
config_get src_mac "$sid" "src_mac"
config_get dst_mac "$sid" "dst_mac"
config_get dscp_filter "$sid" "dscp_filter"
config_get pcp_check "$sid" "pcp_check"
config_get eth_type "$sid" "ethertype"
config_get vid "$sid" "vid_check"
config_get dhcp_type "$sid" "dhcp_type" # dhcpv4 or v6
config_get src_vcid "$sid" "src_vendor_class_id" # dhcp option 60
config_get dst_vcid "$sid" "dst_vendor_class_id" # dhcp option 60
config_get src_clid "$sid" "src_client_id" # dhcp option 61
config_get dst_clid "$sid" "dst_client_id" # dhcp option 61
config_get src_ucid "$sid" "src_user_class_id" # dhcp option 77
config_get dst_ucid "$sid" "dst_user_class_id" # dhcp option 77
config_get traffic_class "$sid" "traffic_class"
config_get protocol "$sid" "proto"
if [ -n "$src_if" ]; then
for interf in $(db -q get hw.board.ethernetPortOrder); do
if [ "$src_if" == "$interf" ]; then
src_if="$src_if+"
broute_filter_on_src_if $src_if
is_l2_rule=1
fi
done
fi
if [ -n "$src_mac" ]; then
broute_filter_on_src_mac $src_mac
is_l2_rule=1
fi
if [ -n "$dst_mac" ]; then
broute_filter_on_dst_mac $dst_mac
is_l2_rule=1
fi
if [ -n "$pcp_check" ]; then
broute_filter_on_pcp $pcp_check
is_l2_rule=1
fi
if [ -n "$eth_type" ]; then
broute_filter_on_ether_type $eth_type
is_l2_rule=1
fi
if [ -n "$vid" ]; then
broute_filter_on_vid $vid
is_l2_rule=1
fi
case $eth_type in
IPv4|IPV4|0800)
ip_version=4
;;
IPv6|IPV6|86DD)
ip_version=6
;;
*)
if [ -z "$eth_type" ]; then
case $src_ip$dest_ip in
*.*)
ip_version=4
broute_filter_on_ether_type "IPv4"
;;
*:*)
ip_version=6
broute_filter_on_ether_type "IPv6"
;;
*)
if [ -n "$protocol" ] || [ -n "$dscp_filter" ]; then
ip_version=1 #neither ether_type nor ip address used
#ethertype is not configured by user so install
#both proto ipv4 and ipv6 rule
BR6_RULE="$BR_RULE"
broute_filter_on_ether_type "IPv4"
broute_filter_on_ether_type6 "IPv6"
fi
esac
fi
esac
if [ "$ip_version" == "4" ] || [ "$ip_version" == "1" ]; then
broute_ipv4_rule_options "$sid"
is_l2_rule=1
fi
if [ "$ip_version" == "6" ] || [ "$ip_version" == "1" ]; then
broute_ipv6_rule_options "$sid"
is_l2_rule=1
fi
# first process options that will help figure our source mac address
# dhcp option for "vendor class id"
if [ -n "$src_vcid" ]; then
src_dhcp_options="$src_dhcp_options vcid=$src_vcid"
is_l2_rule=1
fi
# dhcp option for "client id"
if [ -n "$src_clid" ]; then
src_dhcp_options="$src_dhcp_options clid=$src_clid"
is_l2_rule=1
fi
# dhcp option for "user class id"
if [ -n "$src_ucid" ]; then
src_dhcp_options="$src_dhcp_options ucid=$src_ucid"
is_l2_rule=1
fi
# if src mac is already a classification criteria, then it
# does not really make sense to add it as a criteria to
# filter packets again based on source mac
if [ -n "$src_dhcp_options" -a -z "$src_mac" ]; then
comp="$(grep -i "$src_dhcp_options" /tmp/dhcp.client.options)"
if [ -n "$comp" ]; then
s_mac_add="$(echo $comp | head -n1 | awk '{print $1;}')"
if [ -n "$s_mac_add" ]; then
broute_filter_on_src_mac $s_mac_add
fi
fi
fi
# Now process options that will help figure our destination mac address
# dhcp option for "vendor class id"
if [ -n "$dst_vcid" ]; then
dst_dhcp_options="$dst_dhcp_options vcid=$dst_vcid"
is_l2_rule=1
fi
# dhcp option for "client id"
if [ -n "$dst_clid" ]; then
dst_dhcp_options="$dst_dhcp_options clid=$dst_clid"
is_l2_rule=1
fi
# dhcp option for "user class id"
if [ -n "$dst_ucid" ]; then
dst_dhcp_options="$dst_dhcp_options ucid=$dst_ucid"
is_l2_rule=1
fi
# if dst mac is already a classification criteria, then it
# does not really make sense to add it as a criteria to
# filter packets again based on dstination mac
if [ -n "$dst_dhcp_options" -a -z "$dst_mac" ]; then
comp="$(grep -i "$dst_dhcp_options" /tmp/dhcp.client.options)"
if [ -n "$comp" ]; then
d_mac_add="$(echo $comp | head -n1 | awk '{print $1;}')"
if [ -n "$d_mac_add" ]; then
broute_filter_on_dst_mac $d_mac_add
fi
fi
fi
if [ $is_l2_rule -eq 0 ]; then
return
fi
[ -n "$traffic_class" ] && broute_rule_set_traffic_class $traffic_class
[ -n "$BR_RULE" ] && broute_append_rule
}
init_iptables_rule() {
IP_RULE=""
}
iptables_filter_intf() {
IP_RULE="$IP_RULE -i $1"
}
iptables_filter_proto() {
IP_RULE="$IP_RULE -p $1"
}
iptables_filter_ip_src() {
IP_RULE="$IP_RULE -s $1"
}
iptables_filter_ip_dest() {
IP_RULE="$IP_RULE -d $1"
}
iptables_filter_port_dest() {
IP_RULE="$IP_RULE --dport $1"
}
iptables_filter_port_src() {
IP_RULE="$IP_RULE --sport $1"
}
iptables_filter_port_dest_range() {
IP_RULE="$IP_RULE --dport $1:$2"
}
iptables_filter_port_src_range() {
IP_RULE="$IP_RULE --sport $1:$2"
}
iptables_filter_dscp_filter() {
IP_RULE="$IP_RULE -m dscp --dscp $1"
}
iptables_filter_ip_len_min() {
IP_RULE="$IP_RULE -m length --length $1"
}
iptables_filter_ip_len_max() {
IP_RULE="$IP_RULE:$1"
}
iptables_set_dscp_mark() {
IP_RULE="$IP_RULE -j DSCP --set-dscp $1"
}
iptables_set_traffic_class() {
IP_RULE="$IP_RULE -j MARK --set-xmark 0x$1/0x$1"
}
append_rule_to_mangle_table() {
if [ $2 == 4 ]; then
echo "iptables -w -t mangle -A $1 $IP_RULE" >> /tmp/qos/classify.iptables
elif [ $2 == 6 ]; then
echo "ip6tables -w -t mangle -A $1 $IP_RULE" >> /tmp/qos/classify.ip6tables
elif [ $2 == 1 ]; then
echo "iptables -w -t mangle -A $1 $IP_RULE" >> /tmp/qos/classify.iptables
echo "ip6tables -w -t mangle -A $1 $IP_RULE" >> /tmp/qos/classify.ip6tables
fi
}
handle_iptables_rules() {
cid=$1
local ip_version=0
local is_l3_rule=0
init_iptables_rule
config_get proto "$cid" "proto"
config_get traffic_class "$cid" "traffic_class"
config_get dscp_mark "$cid" "dscp_mark"
config_get dscp_filter "$cid" "dscp_filter"
config_get dest_port "$cid" "dest_port"
config_get dest_port_range "$cid" "dest_port_range"
config_get src_port "$cid" "src_port"
config_get src_port_range "$cid" "src_port_range"
config_get dest_ip "$cid" "dest_ip"
config_get src_ip "$cid" "src_ip"
config_get ip_len_min "$cid" "ip_len_min"
config_get ip_len_max "$cid" "ip_len_max"
config_get ifname "$cid" "ifname"
#check version of ip
case $src_ip$dest_ip in
*.*)
ip_version=4
;;
*:*)
ip_version=6
;;
*)
ip_version=1 #ip address not used
esac
#filter interface
if [ -n "$ifname" ]; then
if [ "$ifname" != "lo" ]; then
iptables_filter_intf $ifname
fi
fi
# filter proto
if [ -n "$proto" ]; then
iptables_filter_proto $proto
is_l3_rule=1
fi
#filter src. ip
if [ -n "$src_ip" ]; then
iptables_filter_ip_src $src_ip
is_l3_rule=1
fi
#filter dest. ip
if [ -n "$dest_ip" ]; then
iptables_filter_ip_dest $dest_ip
is_l3_rule=1
fi
#filter dest. port
if [ -n "$dest_port" -a -z "$dest_port_range" ]; then
iptables_filter_port_dest $dest_port
is_l3_rule=1
fi
#filter src. port
if [ -n "$src_port" -a -z "$src_port_range" ]; then
iptables_filter_port_src $src_port
is_l3_rule=1
fi
#filter dest. port range
if [ -n "$dest_port" -a -n "$dest_port_range" ]; then
iptables_filter_port_dest_range $dest_port $dest_port_range
is_l3_rule=1
fi
#filter src. port range
if [ -n "$src_port" -a -n "$src_port_range" ]; then
iptables_filter_port_src_range $src_port $src_port_range
is_l3_rule=1
fi
#filter dscp
if [ -n "$dscp_filter" ]; then
iptables_filter_dscp_filter $dscp_filter
is_l3_rule=1
fi
#filter min. IP packet len.
if [ -n "$ip_len_min" ]; then
iptables_filter_ip_len_min $ip_len_min
is_l3_rule=1
fi
#filter max. IP packet len.
if [ -n "$ip_len_max" ]; then
iptables_filter_ip_len_max $ip_len_max
is_l3_rule=1
fi
if [ $is_l3_rule -eq 0 ]; then
return
fi
#set dscp mark
[ -n "$dscp_mark" ] && iptables_set_dscp_mark $dscp_mark
#set packet queue mark
[ -n "$traffic_class" ] && iptables_set_traffic_class $traffic_class
#write iptables rule for dscp marking
[ -n "$IP_RULE" -a -n "$dscp_mark" ] && append_rule_to_mangle_table "qos_forward" $ip_version
if [ -n "$IP_RULE" -a -n "$traffic_class" ]; then
if [ "$ifname" == "lo" ]; then
#write iptables rule for putting WAN directed internal packets in different queue
append_rule_to_mangle_table "qos_output" $ip_version
else
#write iptables rule for putting WAN directed LAN packets in different queue
append_rule_to_mangle_table "qos_prerouting" $ip_version
fi
fi
}
assign_policer_to_port() {
local ifname="$1"
local pindex="$2"
local portorder="$(db -q get hw.board.ethernetPortOrder)"
local wanport="$(db -q get hw.board.ethernetWanPort)"
local runner_lan_ports="$(bdmf_shell -c `cat /var/bdmf_sh_id` -cmd /b/examine port | grep ': port/index=lan' | sed -e 's#\.##g' | cut -f2 -d' ' | sort -u | xargs)"
local i=1
for port in $portorder; do
if [ "$ifname" == "$port" ]; then
if [ "$wanport" == "$port" ]; then
bdmf_shell -c `cat /var/bdmf_sh_id` -cmd /b/configure port/index=wan0 ingress_rate_limit={traffic_types=8,policer={policer/dir=us,index=$pindex}}
else
local lanport="$(echo -n "$runner_lan_ports" | cut -f${i} -d' ')"
bdmf_shell -c `cat /var/bdmf_sh_id` -cmd /b/configure $lanport ingress_rate_limit={traffic_types=8,policer={policer/dir=us,index=$pindex}}
fi
break
fi
i=$((i+1))
done
}
handle_policer_rules() {
local c_sec=$1
local policer_name
local ifname
local pname
local pindex=-1
local ingress_rate=0
local in_burst_size=0
config_get policer_name "$c_sec" "policer"
if [ -z "$policer_name" ];then
# no need to apply policer if policer not present in this
# classification rule
return
fi
config_get ifname "$c_sec" "ifname"
if [ -z "$ifname" ]; then
# cannot associate policer as interface is not mentioned
return
fi
local i=0
local max_policer_inst=$(cat /tmp/qos/max_policer_inst)
while :
do
if [ $i -eq $max_policer_inst ]; then
break
fi
pname="$(uci -q get qos.@policer[$i].name)"
if [ "$policer_name" == "$pname" ]; then
pindex=$i
ingress_rate=$(uci -q get qos.@policer[$i].committed_rate)
in_burst_rate=$(uci -q get qos.@policer[$i].committed_burst_size)
break
fi
i=$((i + 1))
done
if [ $pindex -lt 0 ]; then
# policer not found, no need to proceed further
return
fi
config_ingress_rate_limit $ifname $ingress_rate $in_burst_size
}
config_ingress_rate_limit() {
local ifname="$1"
local ingress_rate=$2
local in_burst_size=$3
local wanport="$(db -q get hw.board.ethernetWanPort)"
# Unit in uci file is in bps while that accepted by ethswctl is kbits
if [ $ingress_rate -lt 1000 ]; then
return
fi
ingress_rate=$((ingress_rate / 1000))
if [ $in_burst_size -eq 0 ]; then
in_burst_size=$ingress_rate
else
in_burst_size=$((in_burst_size / 1000))
fi
local unitport="$(get_port_number $ifname)"
local unit=$(echo $unitport | cut -d ' ' -f 1)
local port=$(echo $unitport | cut -d ' ' -f 2)
ethswctl -c rxratectrl -n $unit -p $port -x $ingress_rate -y $in_burst_size
}
# Function to handle a classify order
handle_classify_order() {
local cid="$1" #classify section ID
config_get is_enable "$cid" "enable" 1
# No need to configure disabled classify
if [ $is_enable == '0' ]; then
return
fi
# Create classify file containing classify order
local corder_file="/tmp/qos/classify.order"
config_get c_order "$cid" "order"
if [ -z "$c_order" ]; then
c_order=$temp_order;
temp_order=$((temp_order + 1))
fi
value=${c_order}_${cid}
echo $value >> $corder_file
}
# Sort classify, lower value in uci means higher precedence, so this
# function sorts the classify order in assending order
sort_classify_by_order() {
local corder_file="/tmp/qos/classify.order"
local tmp_corder_file="/tmp/qos/tmp_classify.order"
sort -n -k1 $corder_file > $tmp_corder_file
cp $tmp_corder_file $corder_file
rm -f $tmp_corder_file
}
#function to handle a classify section
handle_classify() {
local corder_file="/tmp/qos/classify.order"
while read -r line; do
line_cid=$(echo $line | cut -d '_' -f 2)
# add ip rule only for classify rules which has non empty
# value forwarding policy option
# if forwarding policy option value empty then add iptables/
# ip6tables/ebtables/rate_limit rules
config_get fwding_policy "$line_cid" "forwarding_policy"
if [ -n "$fwding_policy" ]; then
handle_ip_rule $line_cid $fwding_policy
else
handle_ebtables_rules $line_cid
handle_iptables_rules $line_cid
handle_policer_rules $line_cid
fi
done < "$corder_file"
}
configure_shaper() {
# Delete existing shaper
for intf in $(db get hw.board.ethernetPortOrder); do
tmctl setportshaper --devtype 0 --if $intf --shapingrate 0 --burstsize -1
done
# Load UCI file
config_load qos
# Processing shaper section(s)
config_foreach handle_shaper shaper
}
configure_classify() {
#processing classify section
# First remove old files
rm -f /tmp/qos/classify.ebtables
rm -f /tmp/qos/classify.iptables
rm -f /tmp/qos/classify.ip6tables
rm -f /tmp/qos/classify.order
rm -f /tmp/qos/tmp_classify.order
rm -f /tmp/qos/classify.iprule
#create files that will contain the rules if not present already
mkdir -p /tmp/qos/
touch /tmp/qos/classify.iptables
touch /tmp/qos/classify.ip6tables
touch /tmp/qos/classify.ebtables
touch /tmp/qos/classify.order
touch /tmp/qos/tmp_classify.order
touch /tmp/qos/classify.iprule
#add flush chain rules
flush_chains
#flush added ip rule
flush_ip_rule
# Load UCI file
config_load qos
config_foreach handle_classify_order classify
sort_classify_by_order
handle_classify
sh /tmp/qos/classify.ebtables
sh /tmp/qos/classify.iptables
sh /tmp/qos/classify.ip6tables
sh /tmp/qos/classify.iprule
# broadcom recommends that each time traffic class is set,
# the flows should be flushed for the new mapping to take
# effect, it then makes sense to make it a part of the
# qosmngr package itself.
fcctl flush
}
pre_configure_queue() {
# Delete queues
for intf in $(db get hw.board.ethernetPortOrder); do
rm -rf /tmp/qos/$intf
mkdir -p /tmp/qos/$intf
touch /tmp/qos/$intf/q_order
touch /tmp/qos/$intf/q_idx
echo 0 > /tmp/qos/$intf/q_idx
tmctl porttminit --devtype 0 --if $intf --numqueues 8
i=0
for i in 0 1 2 3 4 5 6 7; do
tmctl delqcfg --devtype 0 --if $intf --qid $i &>/dev/null
done
done
}
configure_queue_shaping_rate() {
# Load UCI file
config_load qos
for interf in $(db -q get hw.board.ethernetPortOrder); do
Q_COUNT=0
config_foreach handle_queue_shapingrate queue $interf
done
}
configure_queue() {
# Load UCI file
config_load qos
config_foreach handle_q_order queue
sort_by_precedence
for interf in $(db -q get hw.board.ethernetPortOrder); do
Q_COUNT=0
# sp queue have max priority value = no. of queue configured on the port
# hence read and update SP_Q_PRIO here
local q_no=$(cat /tmp/qos/$interf/q_idx)
SP_Q_PRIO=`expr $q_no - 1`
config_foreach handle_queue queue $interf
done
}
configure_policer() {
# The policer object is not available on non BCM968* chips, just clean up
# the old config if any and return
for intf in $(db get hw.board.ethernetPortOrder); do
local unitport="$(get_port_number $intf)"
local unit=$(echo $unitport | cut -d ' ' -f 1)
local port=$(echo $unitport | cut -d ' ' -f 2)
# setting rate and burst size to 0 disables rate limiting
if [ $port != "" -a $unit != "" ]; then
ethswctl -c rxratectrl -n $unit -p $port -x 0 -y 0 &>/dev/null
fi
done
# Delete policer
local i=0
local max_p_inst=0
if [ -f "/tmp/qos/max_policer_inst" ]; then
max_p_inst=$(cat /tmp/qos/max_policer_inst)
fi
while :
do
if [ $i -eq $max_p_inst ]; then
break
fi
tmctl deletepolicer --dir 1 --pid $i &>/dev/null
i=$((i + 1))
done
# reset the policer counter
echo 0 > /tmp/qos/max_policer_inst
# Load UCI file
config_load qos
config_foreach handle_policer policer
echo $POLICER_COUNT > /tmp/qos/max_policer_inst
}
configure_qos() {
#queue configuration is being done after shaper configuration,
#If port shapingrate configuration on DISC device is called after queue configuration then
#driver overwrites the queue shaping rate with default value of port shaping rate.
pre_configure_queue
configure_shaper
configure_queue
configure_policer
configure_classify
}
reload_qos() {
local service_name="$1"
local cpu_model="$(cat /proc/socinfo | grep 'SoC Name' | cut -d':' -f2)"
if [ -z "$service_name" ]; then
configure_qos
elif [ "$service_name" == "shaper" ]; then
configure_shaper
# call to configure queue shaping rate as in some platform port shaping rate
# configuration overwrites the queue shaping rate in driver to default value
# this will restore the queue shping rate
configure_queue_shaping_rate
elif [ "$service_name" == "queue" ]; then
pre_configure_queue
configure_queue
elif [ "$service_name" == "classify" ]; then
configure_classify
elif [ "$service_name" == "policer" ]; then
configure_policer
fi
}
get_cfg_added_deleted()
{
# return true if there is difference in number of queue configuration
# in /etc/config/qos and /tmp/qos/qos, false is returned if both file
# has same count of queue configuration.
local queue=0
local classify=0
local shaper=0
local policer=0
local old_cfg="false"
config_cb() {
# invoked on the just previous config_load, get the count of
# queue configuration in /etc/config/qos and /tmp/qos/qos.
cfg_type="$1"
cfg_name="$2"
if [ -z $cfg_name ] || [ -z $cfg_type ]; then
return
fi
if [ "$old_cfg" == "false" ]; then
eval $cfg_type=$(($cfg_type + 1))
else
eval $cfg_type=$(($cfg_type - 1))
fi
option_cb() {
local option="$1"
local value="$2"
if [ -z "$option" ] || [ -z "$value" ]; then
return
fi
if [ "$old_cfg" == "false" ]; then
eval $cfg_type=$(($cfg_type + 1))
else
eval $cfg_type=$(($cfg_type - 1))
fi
}
}
# config_load will trigger call for config_cb that is getting count
# of qos configuration, respective config counts will come.
config_load qos
# config_load will trigger call for config_cb that is decreasing count
# of qos configuration.
old_cfg="true"
UCI_CONFIG_DIR="/tmp/qos"
config_load qos
UCI_CONFIG_DIR="/etc/config"
reset_cb
if [ $classify -ne 0 ]; then
modified_config="classify"
fi
if [ $shaper -ne 0 ]; then
modified_config="$modified_config shaper"
fi
if [ $policer -ne 0 ]; then
modified_config="$modified_config policer"
fi
if [ $queue -eq 0 ]; then
echo "$modified_config"
return
else
echo "queue"
return
fi
}
# reload_qos_service is invoked on qos service reload.
reload_qos_service() {
q_cfg_restart="false"
policer="false"
classify="false"
shaper="false"
setup_qos
if ! [[ -f "/etc/config/qos" && -f "/tmp/qos/qos" ]]; then
configure_qos
cp /etc/config/qos /tmp/qos/qos
fi
config_cb() {
# this is invoked when config_load is called.
cfg_type="$1"
cfg_name="$2"
if [ -z $cfg_name ] || [ -z $cfg_type ]; then
return
fi
option_cb() {
# checking for if any parameters value is modified in queue cfg
# comparsion is done between /etc/config/qos and /tmp/qos/qos
local option="$1"
local value="$2"
local old_value=""
if [ -z "$option" ] || [ -z "$value" ]; then
return
fi
old_value=$(uci -q -c "/tmp/qos" get qos.$cfg_name.$option)
if ! [ "$old_value" == "$value" ]; then
if [ "$cfg_type" == "queue" ]; then
q_cfg_restart="true"
else
eval $cfg_type="true"
fi
fi
}
}
# if there is new addition/deletion of queue configuration
# then return is queue to trigger restart of qos.
# Otehrwise shaper policer classify
# respective operation config is invoked that does not change queue statistics
cfg_added_deleted=$(get_cfg_added_deleted)
if [ "$cfg_added_deleted" == "queue" ]; then
configure_qos
else
q_cfg_restart="false"
# config_load will trigger call for config_cb that is checking
# for modification in config value of queue config.
# if change of value of queue config is there then q_cfg_restart
# is set as true, else other qos config flag is set as true.
config_load qos
reset_cb
if [ "$q_cfg_restart" == "true" ]; then
configure_qos
else
for config in $cfg_added_deleted
do
eval $config="true"
done
if [ "$shaper" == "true" ]; then
reload_qos "shaper"
fi
if [ "$policer" == "true" ]; then
reload_qos "policer"
# change in policer config may need reconfiguration
# of classifier
reload_qos "classify"
fi
if [ "$classify" == "true" ]; then
reload_qos "classify"
fi
fi
fi
cp /etc/config/qos /tmp/qos/qos
}