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

API interface change and UTs for TC functions #25

Merged
merged 5 commits into from
Aug 18, 2023
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/pr-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ jobs:
- name: Build
run: make build-linux
- name: Unit test
run: make unit-test
run: sudo make unit-test
- name: Upload code coverage
uses: codecov/codecov-action@v3
15 changes: 15 additions & 0 deletions pkg/tc/generate_mocks.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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.
package tc

//go:generate go run github.com/golang/mock/mockgen -destination mocks/tc_mocks.go . BpfTc
104 changes: 104 additions & 0 deletions pkg/tc/mocks/tc_mocks.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

68 changes: 59 additions & 9 deletions pkg/tc/tc.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ const (

var log = logger.Get()

type BpfTc interface {
TCIngressAttach(interfaceName string, progFD int, funcName string) error
TCIngressDetach(interfaceName string) error
TCEgressAttach(interfaceName string, progFD int, funcName string) error
TCEgressDetach(interfaceName string) error
CleanupQdiscs(ingressCleanup bool, egressCleanup bool) error
mismatchedInterfacePrefix(interfaceName string) error
}

var _ BpfTc = &bpfTc{}

type bpfTc struct {
InterfacePrefix string
}

func New(interfacePrefix string) BpfTc {
return &bpfTc{
InterfacePrefix: interfacePrefix,
}

}

func enableQdisc(link netlink.Link) bool {
qdiscs, err := netlink.QdiscList(link)
if err != nil {
Expand All @@ -54,7 +76,20 @@ func enableQdisc(link netlink.Link) bool {

}

func TCIngressAttach(interfaceName string, progFD int, funcName string) error {
func (m *bpfTc) mismatchedInterfacePrefix(interfaceName string) error {
if !strings.HasPrefix(interfaceName, m.InterfacePrefix) {
log.Errorf("expected prefix - %s but got %s", m.InterfacePrefix, interfaceName)
return errors.New("Mismatched initialized prefix name and passed interface name")
}
return nil
}

func (m *bpfTc) TCIngressAttach(interfaceName string, progFD int, funcName string) error {

if err := m.mismatchedInterfacePrefix(interfaceName); err != nil {
return err
}

intf, err := netlink.LinkByName(interfaceName)
if err != nil {
log.Errorf("failed to find device by name %s: %w", interfaceName, err)
Expand Down Expand Up @@ -101,7 +136,12 @@ func TCIngressAttach(interfaceName string, progFD int, funcName string) error {
return nil
}

func TCIngressDetach(interfaceName string) error {
func (m *bpfTc) TCIngressDetach(interfaceName string) error {

if err := m.mismatchedInterfacePrefix(interfaceName); err != nil {
return err
}

intf, err := netlink.LinkByName(interfaceName)
if err != nil {
log.Errorf("failed to find device by name %s: %w", interfaceName, err)
Expand Down Expand Up @@ -132,7 +172,12 @@ func TCIngressDetach(interfaceName string) error {
return fmt.Errorf("no active filter to detach-%s", interfaceName)
}

func TCEgressAttach(interfaceName string, progFD int, funcName string) error {
func (m *bpfTc) TCEgressAttach(interfaceName string, progFD int, funcName string) error {

if err := m.mismatchedInterfacePrefix(interfaceName); err != nil {
return err
}

intf, err := netlink.LinkByName(interfaceName)
if err != nil {
log.Errorf("failed to find device by name %s: %w", interfaceName, err)
Expand Down Expand Up @@ -179,7 +224,12 @@ func TCEgressAttach(interfaceName string, progFD int, funcName string) error {
return nil
}

func TCEgressDetach(interfaceName string) error {
func (m *bpfTc) TCEgressDetach(interfaceName string) error {

if err := m.mismatchedInterfacePrefix(interfaceName); err != nil {
return err
}

intf, err := netlink.LinkByName(interfaceName)
if err != nil {
log.Errorf("failed to find device by name %s: %w", interfaceName, err)
Expand Down Expand Up @@ -210,9 +260,9 @@ func TCEgressDetach(interfaceName string) error {
return fmt.Errorf("no active filter to detach-%s", interfaceName)
}

func CleanupQdiscs(prefix string, ingressCleanup bool, egressCleanup bool) error {
func (m *bpfTc) CleanupQdiscs(ingressCleanup bool, egressCleanup bool) error {

if prefix == "" {
if m.InterfacePrefix == "" {
log.Errorf("invalid empty prefix")
return nil
}
Expand All @@ -225,10 +275,10 @@ func CleanupQdiscs(prefix string, ingressCleanup bool, egressCleanup bool) error

for _, link := range linkList {
linkName := link.Attrs().Name
if strings.HasPrefix(linkName, prefix) {
if strings.HasPrefix(linkName, m.InterfacePrefix) {
if ingressCleanup {
log.Infof("Trying to cleanup ingress on %s", linkName)
err = TCIngressDetach(linkName)
err = m.TCIngressDetach(linkName)
if err != nil {
if err.Error() == FILTER_CLEANUP_FAILED {
log.Errorf("failed to detach ingress, might not be present so moving on")
Expand All @@ -238,7 +288,7 @@ func CleanupQdiscs(prefix string, ingressCleanup bool, egressCleanup bool) error

if egressCleanup {
log.Infof("Trying to cleanup egress on %s", linkName)
err = TCEgressDetach(linkName)
err = m.TCEgressDetach(linkName)
if err != nil {
if err.Error() == FILTER_CLEANUP_FAILED {
log.Errorf("failed to detach egress, might not be present so moving on")
Expand Down
Loading