Skip to content

Commit

Permalink
Add new runtime mode -> pure TC mode
Browse files Browse the repository at this point in the history
  • Loading branch information
kerwenwwer committed Apr 14, 2024
1 parent 48cb926 commit 28cb937
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
8 changes: 7 additions & 1 deletion cmd/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// New initializes the local node list
func ProgramHandler(LinkName string, obj *bpf.BpfObjects, debug bool) (*xdp.Program, *xdp.Socket) {
func ProgramHandler(LinkName string, obj *bpf.BpfObjects, debug bool, mode int) (*xdp.Program, *xdp.Socket) {
// Get netlink by name
link, err := netlink.LinkByName(LinkName)
if err != nil {
Expand All @@ -25,6 +25,12 @@ func ProgramHandler(LinkName string, obj *bpf.BpfObjects, debug bool) (*xdp.Prog
if debug {
log.Printf("[BPF Handler]: TC program attached. ")
}

// If mode is 0, return program only (no need to create AF_XDP socket)
if mode == 0 {
return nil, nil
}

//Attach XDP program
program, err := bpf.AttachXDP(obj, link.Attrs().Index)
if err != nil {
Expand Down
5 changes: 2 additions & 3 deletions cmd/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ func write(nodeList *NodeList, addr string, port int, data []byte) {

// listen
func listen(nodeList *NodeList, mq chan []byte) {
if nodeList.Protocol == "UDP" {
if nodeList.Protocol == "UDP" || nodeList.Protocol == "TC" {
udpListen(nodeList, mq)
} else if nodeList.Protocol == "XDP" {
//udpListen(nodeList, mq)
xdpListen(nodeList, mq)
} else {
fmt.Println("Protocol not supported, only UDP and XDP.")
fmt.Println("Protocol not supported, only UDP, TC and XDP.")
}
}
17 changes: 11 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func main() {
// Flags for the server command.
serverCmd.Flags().StringVar(&config.NodeName, "name", "", "Node name for identifying in the network.")
serverCmd.Flags().StringVar(&config.LinkName, "link", DefaultLinkName, "Network link interface name.")
serverCmd.Flags().StringVar(&config.Protocol, "proto", DefaultProtocol, "Networking protocol (UDP/XDP).")
serverCmd.Flags().StringVar(&config.Protocol, "proto", DefaultProtocol, "Networking protocol (UDP/TC/XDP).")
serverCmd.Flags().BoolVar(&config.Debug, "debug", false, "Enables debug mode for verbose logging.")

// Client command configuration.
Expand Down Expand Up @@ -166,7 +166,11 @@ func initializeNodeList(cfg Config, address string) (cmd.NodeList, error) {
}

if cfg.Protocol == "XDP" {
if err := loadAndAssignBPFProgram(&nodeList, cfg.LinkName, cfg.Debug); err != nil {
if err := loadAndAssignBPFProgram(&nodeList, cfg.LinkName, cfg.Debug, 1); err != nil {
return cmd.NodeList{}, err
}
} else if cfg.Protocol == "TC" {
if err := loadAndAssignBPFProgram(&nodeList, cfg.LinkName, cfg.Debug, 0); err != nil {
return cmd.NodeList{}, err
}
}
Expand All @@ -178,17 +182,18 @@ func initializeNodeList(cfg Config, address string) (cmd.NodeList, error) {
return nodeList, nil
}

func loadAndAssignBPFProgram(nodeList *cmd.NodeList, linkName string, debug bool) error {
func loadAndAssignBPFProgram(nodeList *cmd.NodeList, linkName string, debug bool, mode int) error {
obj, err := bpf.LoadObjects()
if err != nil {
return fmt.Errorf("[Init.]: Failed to load BPF objects: %w", err)
}

nodeList.Program = obj
l, xsk := cmd.ProgramHandler(linkName, obj, debug)
defer l.Close()
l, xsk := cmd.ProgramHandler(linkName, obj, debug, mode)
if l != nil {
defer l.Close()
}
nodeList.Xsk = xsk

return nil
}

Expand Down

0 comments on commit 28cb937

Please sign in to comment.