-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for handlers and named counters.
Signed-off-by: Tobias Buner <buner@anapaya.net>
- Loading branch information
Showing
15 changed files
with
418 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* This file is part of the go-nft project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* Copyright 2022 Red Hat, Inc. | ||
* | ||
*/ | ||
|
||
package config | ||
|
||
import ( | ||
"github.com/networkplumbing/go-nft/nft/schema" | ||
) | ||
|
||
// AddCounter appends the given named counter to the nftable config. | ||
// The rule is added without an explicit action (`add`). | ||
// Adding multiple times the same named counter has no affect when the config is applied. | ||
func (c *Config) AddCounter(counter *schema.NamedCounter) { | ||
nftable := schema.Nftable{Counter: counter} | ||
c.Nftables = append(c.Nftables, nftable) | ||
} | ||
|
||
// DeleteCounter appends a given rule to the nftable config with the `delete` action. | ||
// Attempting to delete a non-existing named counter, results with a failure when the config is applied. | ||
func (c *Config) DeleteCounter(counter *schema.NamedCounter) { | ||
nftable := schema.Nftable{Delete: &schema.Objects{Counter: counter}} | ||
c.Nftables = append(c.Nftables, nftable) | ||
} | ||
|
||
// LookupCounter searches the configuration for a matching counter and returns it. | ||
// The counter is matched first by the table. | ||
// Mutating the returned counter will result in mutating the configuration. | ||
func (c *Config) LookupCounter(toFind *schema.NamedCounter) *schema.NamedCounter { | ||
for _, nftable := range c.Nftables { | ||
if counter := nftable.Counter; counter != nil { | ||
match := counter.Table == toFind.Table && counter.Family == toFind.Family && counter.Name == toFind.Name | ||
if match { | ||
return counter | ||
} | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/* | ||
* This file is part of the go-nft project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* Copyright 2022 Red Hat, Inc. | ||
* | ||
*/ | ||
|
||
package config_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
assert "github.com/stretchr/testify/require" | ||
|
||
"github.com/networkplumbing/go-nft/nft" | ||
"github.com/networkplumbing/go-nft/nft/schema" | ||
) | ||
|
||
type CounterAction string | ||
|
||
// Counter Actions | ||
const ( | ||
CounterADD CounterAction = "add" | ||
CounterDELETE CounterAction = "delete" | ||
) | ||
|
||
type counterActionFunc func(*nft.Config, *schema.NamedCounter) | ||
|
||
const counterName = "test-counter" | ||
|
||
func TestCounter(t *testing.T) { | ||
testCounterActions(t) | ||
testCounterLookup(t) | ||
} | ||
|
||
func testCounterActions(t *testing.T) { | ||
actions := map[CounterAction]counterActionFunc{ | ||
CounterADD: func(c *nft.Config, t *schema.NamedCounter) { c.AddCounter(t) }, | ||
CounterDELETE: func(c *nft.Config, t *schema.NamedCounter) { c.DeleteCounter(t) }, | ||
} | ||
table := nft.NewTable(tableName, nft.FamilyIP) | ||
counter := nft.NewCounter(table, counterName) | ||
|
||
for action, actionFunc := range actions { | ||
testName := fmt.Sprintf("%s counter", string(action)) | ||
|
||
t.Run(testName, func(t *testing.T) { | ||
config := nft.NewConfig() | ||
actionFunc(config, counter) | ||
|
||
serializedConfig, err := config.ToJSON() | ||
assert.NoError(t, err) | ||
|
||
counterArgs := fmt.Sprintf(`"family":%q,"table":%q,"name":%q`, table.Family, table.Name, counterName) | ||
var expected []byte | ||
if action == CounterADD { | ||
expected = []byte(fmt.Sprintf(`{"nftables":[{"counter":{%s}}]}`, counterArgs)) | ||
} else { | ||
expected = []byte(fmt.Sprintf(`{"nftables":[{%q:{"counter":{%s}}}]}`, action, counterArgs)) | ||
} | ||
|
||
assert.Equal(t, string(expected), string(serializedConfig)) | ||
}) | ||
} | ||
} | ||
|
||
func testCounterLookup(t *testing.T) { | ||
config := nft.NewConfig() | ||
table_inet := nft.NewTable("table-inet", nft.FamilyINET) | ||
config.AddTable(table_inet) | ||
|
||
counter := nft.NewCounter(table_inet, "test-counter") | ||
config.AddCounter(counter) | ||
|
||
t.Run("Lookup an existing named counter", func(t *testing.T) { | ||
c := config.LookupCounter(counter) | ||
assert.Equal(t, counter, c) | ||
}) | ||
|
||
t.Run("Lookup a missing named counter", func(t *testing.T) { | ||
c := nft.NewCounter(table_inet, "counter-na") | ||
assert.Nil(t, config.LookupCounter(c)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* This file is part of the go-nft project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* Copyright 2022 Red Hat, Inc. | ||
* | ||
*/ | ||
|
||
package nft | ||
|
||
import ( | ||
"github.com/networkplumbing/go-nft/nft/schema" | ||
) | ||
|
||
// NewCounter returns a new schema counter structure for a named counter. | ||
func NewCounter(table *schema.Table, name string) *schema.NamedCounter { | ||
c := &schema.NamedCounter{ | ||
Family: table.Family, | ||
Table: table.Name, | ||
Name: name, | ||
} | ||
|
||
return c | ||
} |
Oops, something went wrong.