Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support multiple excluded networks #156

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 52 additions & 34 deletions tcconfig/shaper/htb.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@


import errno
import itertools
import re
from typing import List

Expand Down Expand Up @@ -140,21 +141,25 @@ def _add_rate(self):
def _add_exclude_filter(self):
import subprocrunner

if all(
[
typepy.is_null_string(param)
for param in (
self._tc_obj.exclude_dst_network,
self._tc_obj.exclude_src_network,
self._tc_obj.exclude_dst_port,
self._tc_obj.exclude_src_port,
)
]
):
validations = {
typepy.is_not_empty_sequence: (
self._tc_obj.exclude_dst_network,
self._tc_obj.exclude_src_network,
),
typepy.is_not_null_string: (
self._tc_obj.exclude_dst_port,
self._tc_obj.exclude_src_port,
),
}
has_filter = False
for check, params in validations.items():
has_filter |= any(check(param) for param in params)

if not has_filter:
logger.debug("no exclude filter found")
return

command_item_list = [
base_command_item_list = [
self._tc_obj.get_tc_command(TcSubCommand.FILTER),
self._dev,
"protocol {:s}".format(self._tc_obj.protocol),
Expand All @@ -163,37 +168,50 @@ def _add_exclude_filter(self):
"u32",
]

if typepy.is_not_null_string(self._tc_obj.exclude_dst_network):
command_item_list.append(
"match {:s} {:s} {:s}".format(
self._tc_obj.protocol_match, "dst", self._tc_obj.exclude_dst_network
return_code = 0

null_string = [None]
exclude_dst_networks = self._tc_obj.exclude_dst_network or null_string
exclude_src_networks = self._tc_obj.exclude_src_network or null_string

for exclude_dst_network, exclude_src_network in itertools.product(
exclude_dst_networks,
exclude_src_networks,
):
command_item_list = base_command_item_list[:]

if typepy.is_not_null_string(exclude_dst_network):
command_item_list.append(
"match {:s} {:s} {:s}".format(
self._tc_obj.protocol_match, "dst", exclude_dst_network
)
)
)

if typepy.is_not_null_string(self._tc_obj.exclude_src_network):
command_item_list.append(
"match {:s} {:s} {:s}".format(
self._tc_obj.protocol_match, "src", self._tc_obj.exclude_src_network
if typepy.is_not_null_string(exclude_src_network):
command_item_list.append(
"match {:s} {:s} {:s}".format(
self._tc_obj.protocol_match, "src", exclude_src_network
)
)
)

if typepy.is_not_null_string(self._tc_obj.exclude_dst_port):
command_item_list.append(
"match {:s} {:s} {:s} 0xffff".format(
self._tc_obj.protocol_match, "dport", self._tc_obj.exclude_dst_port
if typepy.is_not_null_string(self._tc_obj.exclude_dst_port):
command_item_list.append(
"match {:s} {:s} {:s} 0xffff".format(
self._tc_obj.protocol_match, "dport", self._tc_obj.exclude_dst_port
)
)
)

if typepy.is_not_null_string(self._tc_obj.exclude_src_port):
command_item_list.append(
"match {:s} {:s} {:s} 0xffff".format(
self._tc_obj.protocol_match, "sport", self._tc_obj.exclude_src_port
if typepy.is_not_null_string(self._tc_obj.exclude_src_port):
command_item_list.append(
"match {:s} {:s} {:s} 0xffff".format(
self._tc_obj.protocol_match, "sport", self._tc_obj.exclude_src_port
)
)
)

command_item_list.append("flowid {:s}".format(self.__classid_wo_shaping))
command_item_list.append("flowid {:s}".format(self.__classid_wo_shaping))
return_code |= subprocrunner.SubprocessRunner(" ".join(command_item_list)).run()

return subprocrunner.SubprocessRunner(" ".join(command_item_list)).run()
return return_code

def set_shaping(self):
is_add_shaping_rule = self._tc_obj.is_add_shaping_rule
Expand Down
2 changes: 2 additions & 0 deletions tcconfig/tcset.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,10 +201,12 @@ def get_arg_parser():
group.add_argument(
"--exclude-dst-network",
help="exclude a specific destination IP-address/network from a shaping rule.",
nargs="*",
)
group.add_argument(
"--exclude-src-network",
help="exclude a specific source IP-address/network from a shaping rule.",
nargs="*",
)
group.add_argument(
"--exclude-dst-port", help="exclude a specific destination port from a shaping rule."
Expand Down