Skip to content

Commit

Permalink
command channel for reattaching interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
davidcoles committed Jun 24, 2024
1 parent 9b01a44 commit a7cca80
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmd/config.pl
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,8 @@ sub expect {
my($expect) = @_;
my @expect;

return [ 0 ] if $expect eq 'any';

foreach (split(/\s+/, $expect)) {
my @val;

Expand Down
2 changes: 1 addition & 1 deletion cmd/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module vc5
go 1.19

require (
github.com/davidcoles/cue v0.1.1
github.com/davidcoles/cue v0.1.2
github.com/davidcoles/xvs v0.1.15
github.com/elastic/go-elasticsearch/v7 v7.17.10
)
Expand Down
4 changes: 2 additions & 2 deletions cmd/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/davidcoles/cue v0.1.1 h1:R2hG4gSVKuCj7UmTQGhZS/ZRmfDULqmA03uXwx7rgOg=
github.com/davidcoles/cue v0.1.1/go.mod h1:26FTBytVHJ1XQWOGC+Cfnx4Q9xV8k1xr6K5uZ7s/EBw=
github.com/davidcoles/cue v0.1.2 h1:UZnjl6ezaG7WC9iFbarB/+u7rDVa4aExSCx1ugtwMIU=
github.com/davidcoles/cue v0.1.2/go.mod h1:26FTBytVHJ1XQWOGC+Cfnx4Q9xV8k1xr6K5uZ7s/EBw=
github.com/davidcoles/xvs v0.1.15 h1:FJ5CN0VEqyoilK/R0Pvc51lKpLsOTqcstPilkkxgbZw=
github.com/davidcoles/xvs v0.1.15/go.mod h1:NQg6Ob9zLr49qH3I6E6zqnGp4tVBu4cT/3vXUB2XA8w=
github.com/elastic/go-elasticsearch/v7 v7.17.10 h1:TCQ8i4PmIJuBunvBS6bwT2ybzVFxxUhhltAs3Gyu1yo=
Expand Down
17 changes: 17 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ func main() {
mp := flag.Bool("M", false, "Use multiprotocol extensions on loopback BGP") // experimental - may change
delay := flag.Uint("D", 0, "Delay between initialisaton of interfaces") // experimental - may change
flows := flag.Uint("F", 0, "Set maximum number of flows") // experimental - may change
cmd_path := flag.String("C", "", "Command channel path") // experimental - may change

// Changing number of flows will only work on some kernels
// Not supported: 5.4.0-171-generic
Expand Down Expand Up @@ -148,6 +149,18 @@ func main() {
go bgpListener(l, logs.sub("bgp"))
}

var cmd_sock net.Listener

if *cmd_path != "" {
os.Remove(*cmd_path)

cmd_sock, err = net.Listen("unix", *cmd_path)

if err != nil {
log.Fatal(err)
}
}

for _, i := range nics {
ethtool(i)
}
Expand All @@ -170,6 +183,10 @@ func main() {
log.Fatal("Couldn't start client: ", err)
}

if cmd_sock != nil {
go readCommands(cmd_sock, client, logs.sub("command"))
}

routerID := address.As4()

if *asn > 0 {
Expand Down
55 changes: 55 additions & 0 deletions cmd/xvs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"net/netip"
"os"
"os/exec"
"regexp"
"sync/atomic"
"time"

Expand Down Expand Up @@ -367,3 +368,57 @@ func multicast_recv(c Client, address string) {
}
}
}

func readCommands(sock net.Listener, client Client, log *sub) {
// eg.: echo enp130s0f0 | socat - UNIX-CLIENT:/var/run/vc5

re := regexp.MustCompile(`\s+`)

for {
conn, err := sock.Accept()
if err != nil {
log.ERR("accept", err.Error())
} else {
go func() {
s := bufio.NewScanner(conn)

for s.Scan() {

line := s.Text()

var cmd []string

for _, s := range re.Split(line, -1) {
if s != "" {
cmd = append(cmd, s)
}
}

l := len(cmd)

if l < 1 {
continue
}

switch cmd[0] {
case "reattach":
if l != 2 {
fmt.Println("Usage: reattach <interface>")
continue
}

nic := cmd[1]
if err := client.ReattachBPF(nic); err != nil {
log.ERR(cmd[0], fmt.Sprintf("%s: %s\n", nic, err.Error()))
} else {
log.NOTICE(cmd[0], fmt.Sprintf("%s: OK\n", nic))
}

default:
log.ERR("unknown", fmt.Sprintf("Unknown command: %s", cmd[0]))
}
}
}()
}
}
}

0 comments on commit a7cca80

Please sign in to comment.