Skip to content

Commit 3b7e16c

Browse files
aroradamanaboch
authored andcommitted
Add ConntrackDeleteFilters
ConntrackDeleteFilters enables users to delete flow entries that match any of the specified filters. This allows users to delete multiple flow entries with a single dump table call. Signed-off-by: Daman Arora <[email protected]>
1 parent 4317e32 commit 3b7e16c

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

conntrack_linux.go

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,13 @@ func ConntrackUpdate(table ConntrackTableType, family InetFamily, flow *Conntrac
7070
// ConntrackDeleteFilter deletes entries on the specified table on the base of the filter
7171
// conntrack -D [table] parameters Delete conntrack or expectation
7272
func ConntrackDeleteFilter(table ConntrackTableType, family InetFamily, filter CustomConntrackFilter) (uint, error) {
73-
return pkgHandle.ConntrackDeleteFilter(table, family, filter)
73+
return pkgHandle.ConntrackDeleteFilters(table, family, filter)
74+
}
75+
76+
// ConntrackDeleteFilters deletes entries on the specified table matching any of the specified filters
77+
// conntrack -D [table] parameters Delete conntrack or expectation
78+
func ConntrackDeleteFilters(table ConntrackTableType, family InetFamily, filters ...CustomConntrackFilter) (uint, error) {
79+
return pkgHandle.ConntrackDeleteFilters(table, family, filters...)
7480
}
7581

7682
// ConntrackTableList returns the flow list of a table of a specific family using the netlink handle passed
@@ -133,9 +139,9 @@ func (h *Handle) ConntrackUpdate(table ConntrackTableType, family InetFamily, fl
133139
return err
134140
}
135141

136-
// ConntrackDeleteFilter deletes entries on the specified table on the base of the filter using the netlink handle passed
142+
// ConntrackDeleteFilters deletes entries on the specified table matching any of the specified filters using the netlink handle passed
137143
// conntrack -D [table] parameters Delete conntrack or expectation
138-
func (h *Handle) ConntrackDeleteFilter(table ConntrackTableType, family InetFamily, filter CustomConntrackFilter) (uint, error) {
144+
func (h *Handle) ConntrackDeleteFilters(table ConntrackTableType, family InetFamily, filters ...CustomConntrackFilter) (uint, error) {
139145
res, err := h.dumpConntrackTable(table, family)
140146
if err != nil {
141147
return 0, err
@@ -144,12 +150,16 @@ func (h *Handle) ConntrackDeleteFilter(table ConntrackTableType, family InetFami
144150
var matched uint
145151
for _, dataRaw := range res {
146152
flow := parseRawData(dataRaw)
147-
if match := filter.MatchConntrackFlow(flow); match {
148-
req2 := h.newConntrackRequest(table, family, nl.IPCTNL_MSG_CT_DELETE, unix.NLM_F_ACK)
149-
// skip the first 4 byte that are the netfilter header, the newConntrackRequest is adding it already
150-
req2.AddRawData(dataRaw[4:])
151-
req2.Execute(unix.NETLINK_NETFILTER, 0)
152-
matched++
153+
for _, filter := range filters {
154+
if match := filter.MatchConntrackFlow(flow); match {
155+
req2 := h.newConntrackRequest(table, family, nl.IPCTNL_MSG_CT_DELETE, unix.NLM_F_ACK)
156+
// skip the first 4 byte that are the netfilter header, the newConntrackRequest is adding it already
157+
req2.AddRawData(dataRaw[4:])
158+
req2.Execute(unix.NETLINK_NETFILTER, 0)
159+
matched++
160+
// flow is already deleted, no need to match on other filters and continue to the next flow.
161+
break
162+
}
153163
}
154164
}
155165

conntrack_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ func TestConntrackTableDelete(t *testing.T) {
311311

312312
// Flush entries of groupB
313313
var deleted uint
314-
if deleted, err = h.ConntrackDeleteFilter(ConntrackTable, unix.AF_INET, filter); err != nil {
314+
if deleted, err = h.ConntrackDeleteFilters(ConntrackTable, unix.AF_INET, filter); err != nil {
315315
t.Fatalf("Error during the erase: %s", err)
316316
}
317317
if deleted != 5 {

0 commit comments

Comments
 (0)