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

feat(frr): call frr cmds via telnet #231

Merged
merged 1 commit into from
Oct 12, 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
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,7 @@ require (
github.com/yagipy/maintidx v1.0.0 // indirect
github.com/yeya24/promlinter v0.2.0 // indirect
github.com/ykadowak/zerologlint v0.1.3 // indirect
github.com/ziutek/telnet v0.0.0-20180329124119-c3b780dc415b // indirect
gitlab.com/bosi/decorder v0.4.0 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,8 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/ziutek/telnet v0.0.0-20180329124119-c3b780dc415b h1:VfPXB/wCGGt590QhD1bOpv2J/AmC/RJNTg/Q59HKSB0=
github.com/ziutek/telnet v0.0.0-20180329124119-c3b780dc415b/go.mod h1:IZpXDfkJ6tWD3PhBK5YzgQT+xJWh7OsdwiG8hA2MkO4=
gitlab.com/bosi/decorder v0.4.0 h1:HWuxAhSxIvsITcXeP+iIRg9d1cVfvVkmlF7M68GaoDY=
gitlab.com/bosi/decorder v0.4.0/go.mod h1:xarnteyUoJiOTEldDysquWKTVDCKo2TOIOIibSuWqOg=
go-simpler.org/assert v0.6.0 h1:QxSrXa4oRuo/1eHMXSBFHKvJIpWABayzKldqZyugG7E=
Expand Down
1 change: 1 addition & 0 deletions pkg/evpn/bridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ func (s *Server) CreateLogicalBridge(ctx context.Context, in *pb.CreateLogicalBr
}
// TODO: bridge link set dev vxlan-<LB-vlan-id> neigh_suppress on
}
// save object to the database
response := protoClone(in.LogicalBridge)
response.Status = &pb.LogicalBridgeStatus{OperStatus: pb.LBOperStatus_LB_OPER_STATUS_UP}
s.Bridges[in.LogicalBridge.Name] = response
Expand Down
1 change: 1 addition & 0 deletions pkg/evpn/port.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ func (s *Server) CreateBridgePort(ctx context.Context, in *pb.CreateBridgePortRe
fmt.Printf("Failed to up iface link: %v", err)
return nil, err
}
// save object to the database
response := protoClone(in.BridgePort)
response.Status = &pb.BridgePortStatus{OperStatus: pb.BPOperStatus_BP_OPER_STATUS_UP}
s.Ports[in.BridgePort.Name] = response
Expand Down
5 changes: 5 additions & 0 deletions pkg/evpn/svi.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"github.com/vishvananda/netlink"

pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"
"github.com/opiproject/opi-evpn-bridge/pkg/utils"

"go.einride.tech/aip/fieldbehavior"
"go.einride.tech/aip/resourceid"
Expand Down Expand Up @@ -117,6 +118,10 @@ func (s *Server) CreateSvi(ctx context.Context, in *pb.CreateSviRequest) (*pb.Sv
fmt.Printf("Failed to up link: %v", err)
return nil, err
}
// configure FRR
data, err := utils.TelnetDialAndCommunicate(ctx, "show bgp vrf "+path.Base(vrf.Name)+" vni")
fmt.Printf("TelnetDialAndCommunicate: %v:%v", data, err)
// save object to the database
response := protoClone(in.Svi)
response.Status = &pb.SviStatus{OperStatus: pb.SVIOperStatus_SVI_OPER_STATUS_UP}
s.Svis[in.Svi.Name] = response
Expand Down
5 changes: 5 additions & 0 deletions pkg/evpn/vrf.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/vishvananda/netlink"

pb "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go"
"github.com/opiproject/opi-evpn-bridge/pkg/utils"

"go.einride.tech/aip/fieldbehavior"
"go.einride.tech/aip/resourceid"
Expand Down Expand Up @@ -138,6 +139,10 @@ func (s *Server) CreateVrf(ctx context.Context, in *pb.CreateVrfRequest) (*pb.Vr
return nil, err
}
}
// configure FRR
data, err := utils.TelnetDialAndCommunicate(ctx, "show bgp vrf "+vrfName+" vni")
fmt.Printf("TelnetDialAndCommunicate: %v:%v", data, err)
// save object to the database
response := protoClone(in.Vrf)
response.Status = &pb.VrfStatus{LocalAs: 4, RoutingTable: tableID, Rmac: mac}
s.Vrfs[in.Vrf.Name] = response
Expand Down
63 changes: 63 additions & 0 deletions pkg/utils/telnet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries.

// Package utils contails useful helper functions
package utils

import (
"context"
"time"

"github.com/ziutek/telnet"
)

const (
network = "tcp"
password = "opi"
address = "localhost:2605"
timeout = 10 * time.Second
)

// TelnetDialAndCommunicate connects to telnet with password and runs command
func TelnetDialAndCommunicate(_ context.Context, command string) (string, error) {
// new connection every time
conn, err := telnet.DialTimeout(network, address, timeout)
if err != nil {
return "", err
}
defer func(t *telnet.Conn) { _ = t.Close() }(conn)

conn.SetUnixWriteMode(true)

err = conn.SetWriteDeadline(time.Now().Add(timeout))
if err != nil {
return "", err
}

// login
err = conn.SkipUntil("Password: ")
if err != nil {
return "", err
}
_, err = conn.Write([]byte(password + "\n"))
if err != nil {
return "", err
}

// command
err = conn.SkipUntil(">")
if err != nil {
return "", err
}
_, err = conn.Write([]byte(command + "\n"))
if err != nil {
return "", err
}

// response
data, err := conn.ReadBytes('>')
if err != nil {
return "", err
}
return string(data), nil
}