Skip to content

Commit

Permalink
netns,tc: add netns support for tc
Browse files Browse the repository at this point in the history
Signed-off-by: ali-aqrabawi <[email protected]>
  • Loading branch information
Ali-aqrabawi committed Jun 28, 2024
1 parent 6a03b9e commit 2682237
Show file tree
Hide file tree
Showing 6 changed files with 182 additions and 41 deletions.
12 changes: 8 additions & 4 deletions src/lib/cmdgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -363,10 +363,12 @@ struct lyd_node *get_node_from_sr(const struct lyd_node *startcmd_node, char *no
else
ret = sr_get_data(sr_session, xpath, 0, 0, 0, &sr_data);
if (ret != SR_ERR_OK) {
fprintf(stderr,
"%s: failed to get node data from sysrepo ds."
" xpath = \"%s\": %s\n",
__func__, xpath, sr_strerror(ret));
if (ret != SR_ERR_NOT_FOUND) {
fprintf(stderr,
"%s: failed to get node data from sysrepo ds."
" xpath = \"%s\": %s\n",
__func__, xpath, sr_strerror(ret));
}
return NULL;
}
return sr_data->tree;
Expand Down Expand Up @@ -407,6 +409,8 @@ int find_netns(struct lyd_node *startcmd, char **netns)
return EXIT_FAILURE;
}
}
if (!strcmp(link_startcmd->schema->module->name, "iproute2-tc-filter"))
link_startcmd = lyd_parent(link_startcmd);
struct lyd_node *netns_dnode = get_node_from_sr(link_startcmd, "netns");
// if the netns_dnode does not exist in sysrepo, then this is create, we get the netns from
// the startcmd
Expand Down
57 changes: 40 additions & 17 deletions tests/cases/test_tc_filter.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ clean_up(){
ip link del if_tc_f2 2>/dev/null
ip link del if_tc_f3 2>/dev/null
ip link del if_tc_f4 2>/dev/null
ip -n tc_filter_red link delete name if_tc_f5_red
ip netns del tc_filter_red
}

ret=0
Expand All @@ -33,27 +35,27 @@ echo "---------------------"
sysrepocfg -d running --edit tests/cases/test_tc_filter_data.xml || ret=$?
# Check if sysrepocfg command failed
if [ -n "$ret" ] && [ "$ret" -ne 0 ]; then
echo "TEST-ERROR: failed to create filter in sysrepo datastore"
echo "TEST-ERROR:TC-FILTER: failed to create filter in sysrepo datastore"
clean_up
exit "$ret"
fi

output=$(tc filter show dev if_tc_f4 ingress)
# Step 2: Check if dev-filter created
if echo "$output" | grep -q "filter protocol ip pref 10 flower chain 0"; then
echo "TEST-INFO: dev-filter for if_tc_f4 created successfully (OK)"
echo "TEST-INFO:TC-FILTER: dev-filter for if_tc_f4 created successfully (OK)"
else
echo "TEST-ERROR: Failed to create dev-filter for if_tc_f4 (FAIL)"
echo "TEST-ERROR:TC-FILTER: Failed to create dev-filter for if_tc_f4 (FAIL)"
clean_up
exit 1
fi

output2=$(tc filter show block 10)
# Step 3: Check if share-block-filter is created
if echo "$output2" | grep -q "filter protocol ip pref 10 flower chain 0"; then
echo "TEST-INFO: share-block-filter for if_tc_f3 created successfully (OK)"
echo "TEST-INFO:TC-FILTER: share-block-filter for if_tc_f3 created successfully (OK)"
else
echo "TEST-ERROR: Failed to create share-block-filter for if_tc_f3 (FAIL)"
echo "TEST-ERROR:TC-FILTER: Failed to create share-block-filter for if_tc_f3 (FAIL)"
clean_up
exit 1
fi
Expand All @@ -62,9 +64,9 @@ output3=$(tc filter show dev if_tc_f2)

# Step 3: Check if qdisc-filter u32 is created
if echo "$output3" | grep -q "filter parent 8002: protocol ip pref 20 u32 chain 0"; then
echo "TEST-INFO: qdisc-filter (u32) for if_tc_f1 created successfully (OK)"
echo "TEST-INFO:TC-FILTER: qdisc-filter (u32) for if_tc_f1 created successfully (OK)"
else
echo "TEST-ERROR: Failed to create qdisc-filter (u32) for if_tc_f1 (FAIL)"
echo "TEST-ERROR:TC-FILTER: Failed to create qdisc-filter (u32) for if_tc_f1 (FAIL)"
clean_up
exit 1
fi
Expand All @@ -73,9 +75,20 @@ fi
output4=$(tc filter show dev if_tc_f1)
# Step 3: Check if qdisc-filter flower is created
if echo "$output4" | grep -q "filter parent 8001: protocol ip pref 20 flower chain 0"; then
echo "TEST-INFO: qdisc-filter (flower) for if_tc_f1 created successfully (OK)"
echo "TEST-INFO:TC-FILTER: qdisc-filter (flower) for if_tc_f1 created successfully (OK)"
else
echo "TEST-ERROR: Failed to create qdisc-filter (flower) for if_tc_f1 (FAIL)"
echo "TEST-ERROR:TC-FILTER: Failed to create qdisc-filter (flower) for if_tc_f1 (FAIL)"
clean_up
exit 1
fi


output=$(tc -n tc_filter_red filter show dev if_tc_f5_red ingress)
# Step 4: Check if netns dev-filter created
if echo "$output" | grep -q "filter protocol ip pref 101 flower chain 0"; then
echo "TEST-INFO:TC-FILTER: netns dev-filter for if_tc_f5_red created successfully (OK)"
else
echo "TEST-ERROR:TC-FILTER: Failed to create netns dev-filter for if_tc_f5_red (FAIL)"
clean_up
exit 1
fi
Expand All @@ -99,47 +112,57 @@ sysrepocfg -C startup -d running || ret=$?
output=$(tc filter show dev if_tc_f4 ingress 2>/dev/null)
# Step 2: Check if dev-filter is still there
if echo "$output" | grep -q "filter protocol ip pref 10 flower chain 0"; then
echo "TEST-ERROR: Failed to delete dev-filter for if_tc_f4 (FAIL)"
echo "TEST-ERROR:TC-FILTER: Failed to delete dev-filter for if_tc_f4 (FAIL)"
clean_up
exit 1
else
echo "TEST-INFO: dev-filter for if_tc_f4 deleted successfully (OK)"
echo "TEST-INFO:TC-FILTER: dev-filter for if_tc_f4 deleted successfully (OK)"
fi

output2=$(tc filter show block 10 2>/dev/null)
# Step 3: Check if share-block-filter is still there
if echo "$output2" | grep -q "filter protocol ip pref 10 flower chain 0"; then
echo "TEST-ERROR: Failed to delete share-block-filter for if_tc_f3 (FAIL)"
echo "TEST-ERROR:TC-FILTER: Failed to delete share-block-filter for if_tc_f3 (FAIL)"
clean_up
exit 1
else
echo "TEST-INFO: share-block-filter for if_tc_f1 deleted successfully (OK)"
echo "TEST-INFO:TC-FILTER: share-block-filter for if_tc_f1 deleted successfully (OK)"
fi

output3=$(tc filter show dev if_tc_f2 2>/dev/null)

# Step 3: Check if qdisc-filter u32 is deleted
if echo "$output3" | grep -q "filter parent 8002: protocol ip pref 20 u32 chain 0"; then
echo "TEST-ERROR: Failed to create qdisc-filter (u32) for if_tc_f1 (FAIL)"
echo "TEST-ERROR:TC-FILTER: Failed to create qdisc-filter (u32) for if_tc_f1 (FAIL)"
clean_up
exit 1
else
echo "TEST-INFO: qdisc-filter (u32) for if_tc_f1 created successfully (OK)"
echo "TEST-INFO:TC-FILTER: qdisc-filter (u32) for if_tc_f1 created successfully (OK)"

fi


output4=$(tc filter show dev if_tc_f1 2>/dev/null)
# Step 3: Check if qdisc-filter flower is created
if echo "$output4" | grep -q "filter parent 8001: protocol ip pref 20 flower chain 0"; then
echo "TEST-ERROR: Failed to create qdisc-filter (flower) for if_tc_f1 (FAIL)"
echo "TEST-ERROR:TC-FILTER: Failed to create qdisc-filter (flower) for if_tc_f1 (FAIL)"
clean_up
exit 1
else
echo "TEST-INFO: qdisc-filter (flower) for if_tc_f1 created successfully (OK)"
echo "TEST-INFO:TC-FILTER: qdisc-filter (flower) for if_tc_f1 created successfully (OK)"

fi

output=$(tc -n tc_filter_red filter show dev if_tc_f5_red ingress 2>/dev/null)
# Step 2: Check if dev-filter is still there
if echo "$output" | grep -q "filter protocol ip pref 101 flower chain 0"; then
echo "TEST-ERROR:TC-FILTER: Failed to delete netns dev-filter for if_tc_f5_red (FAIL)"
clean_up
exit 1
else
echo "TEST-INFO:TC-FILTER: netns dev-filter for if_tc_f5_red deleted successfully (OK)"
fi

# delete the Qdiscs and links
sysrepocfg -C startup -d running -m iproute2-tc-qdisc || ret=$?
sysrepocfg -C startup -d running -m iproute2-ip-link || ret=$?
Expand Down
53 changes: 53 additions & 0 deletions tests/cases/test_tc_filter_data.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
<netnses xmlns="urn:okda:iproute2:ip:netns">
<netns>
<name>tc_filter_red</name>
</netns>
</netnses>
<!--this test filter with classes -->
<links xmlns="urn:okda:iproute2:ip:link">
<link>
Expand All @@ -20,12 +25,19 @@
<type>dummy</type>
<admin-status>up</admin-status>
</link>
<link>
<name>if_tc_f5_red</name>
<type>dummy</type>
<admin-status>up</admin-status>
<netns>tc_filter_red</netns>
</link>
</links>
<tc-filters xmlns="urn:okda:iproute2:tc:filter">
<qdisc-filter>
<dev>if_tc_f1</dev>
<parent>8001:</parent>
<protocol>ip</protocol>
<netns>1</netns>
<rule>
<pref>20</pref>
<flower>
Expand Down Expand Up @@ -61,6 +73,7 @@
<dev>if_tc_f2</dev>
<parent>8002:</parent>
<protocol>ip</protocol>
<netns>1</netns>
<rule>
<pref>20</pref>
<u32>
Expand All @@ -77,6 +90,7 @@
<dev>if_tc_f4</dev>
<direction>ingress</direction>
<protocol>ip</protocol>
<netns>1</netns>
<rule>
<pref>10</pref>
<flower>
Expand All @@ -94,9 +108,32 @@
</flower>
</rule>
</dev-filter>
<dev-filter>
<dev>if_tc_f5_red</dev>
<direction>ingress</direction>
<protocol>ip</protocol>
<netns>tc_filter_red</netns>
<rule>
<pref>101</pref>
<flower>
<match>
<ip_proto>icmp</ip_proto>
<src_ip>11.21.11.21/32</src_ip>
</match>
<action>pass</action>
</flower>
</rule>
<rule>
<pref>201</pref>
<flower>
<action>drop</action>
</flower>
</rule>
</dev-filter>
<shared-block-filter xmlns="urn:okda:iproute2:tc:filter">
<block>10</block>
<protocol>ip</protocol>
<netns>1</netns>
<rule>
<pref>10</pref>
<flower>
Expand All @@ -111,6 +148,7 @@
<dev>if_tc_f1</dev>
<parent>8001:</parent>
<classid>8001:20</classid>
<netns>1</netns>
<htb>
<rate>20mbit</rate>
</htb>
Expand All @@ -119,6 +157,7 @@
<dev>if_tc_f1</dev>
<parent>8001:</parent>
<classid>8001:30</classid>
<netns>1</netns>
<htb>
<rate>30mbit</rate>
</htb>
Expand All @@ -127,6 +166,7 @@
<dev>if_tc_f1</dev>
<parent>8001:</parent>
<classid>8001:40</classid>
<netns>1</netns>
<htb>
<rate>40mbit</rate>
</htb>
Expand All @@ -135,6 +175,7 @@
<dev>if_tc_f2</dev>
<parent>8002:</parent>
<classid>8002:20</classid>
<netns>1</netns>
<htb>
<rate>20mbit</rate>
</htb>
Expand All @@ -145,12 +186,14 @@
<qdisc>
<dev>if_tc_f1</dev>
<parent>root</parent>
<netns>1</netns>
<handle>8001:</handle>
<qdisc-kind>htb</qdisc-kind>
</qdisc>
<qdisc>
<dev>if_tc_f1</dev>
<parent>8001:20</parent>
<netns>1</netns>
<qdisc-kind>pfifo</qdisc-kind>
<fifo-options>
<limit>1002</limit>
Expand All @@ -159,6 +202,7 @@
<qdisc>
<dev>if_tc_f1</dev>
<parent>8001:30</parent>
<netns>1</netns>
<qdisc-kind>pfifo</qdisc-kind>
<fifo-options>
<limit>1003</limit>
Expand All @@ -167,6 +211,7 @@
<qdisc>
<dev>if_tc_f1</dev>
<parent>8001:40</parent>
<netns>1</netns>
<qdisc-kind>pfifo</qdisc-kind>
<fifo-options>
<limit>1004</limit>
Expand All @@ -175,16 +220,24 @@
<qdisc>
<dev>if_tc_f2</dev>
<parent>root</parent>
<netns>1</netns>
<handle>8002:</handle>
<qdisc-kind>htb</qdisc-kind>
</qdisc>
<special-qdisc>
<dev>if_tc_f3</dev>
<qdisc-kind>clsact</qdisc-kind>
<netns>1</netns>
<ingress_block>10</ingress_block>
</special-qdisc>
<special-qdisc>
<dev>if_tc_f4</dev>
<qdisc-kind>ingress</qdisc-kind>
<netns>1</netns>
</special-qdisc>
<special-qdisc>
<dev>if_tc_f5_red</dev>
<qdisc-kind>ingress</qdisc-kind>
<netns>tc_filter_red</netns>
</special-qdisc>
</qdiscs>
5 changes: 5 additions & 0 deletions tests/cases/test_tc_qdisc_data.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
<qdisc>
<dev>if_tc3</dev>
<parent>root</parent>
<netns>1</netns>
<qdisc-kind>pfifo</qdisc-kind>
<fifo-options>
<limit>999</limit>
Expand All @@ -37,6 +38,7 @@
<qdisc>
<dev>if_tc4</dev>
<parent>root</parent>
<netns>1</netns>
<qdisc-kind>fq_codel</qdisc-kind>
<codel-options>
<limit>1001</limit>
Expand All @@ -47,6 +49,7 @@
<qdisc>
<dev>if_tc5</dev>
<parent>root</parent>
<netns>1</netns>
<qdisc-kind>tbf</qdisc-kind>
<tbf-options>
<rate>3200bps</rate>
Expand All @@ -57,12 +60,14 @@
<special-qdisc>
<dev>if_tc1</dev>
<qdisc-kind>clsact</qdisc-kind>
<netns>1</netns>
<ingress_block>10</ingress_block>
<egress_block>30</egress_block>
</special-qdisc>
<special-qdisc>
<dev>if_tc2</dev>
<qdisc-kind>ingress</qdisc-kind>
<netns>1</netns>
<ingress_block>10</ingress_block>
</special-qdisc>
</qdiscs>
Loading

0 comments on commit 2682237

Please sign in to comment.