2022-12-01 09:56:37 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-01-09 12:07:09 +01:00
|
|
|
"fmt"
|
2022-12-01 09:56:37 +01:00
|
|
|
"github.com/edwarnicke/exechelper"
|
2023-02-24 11:26:39 +01:00
|
|
|
"os/exec"
|
2023-02-07 20:40:27 +01:00
|
|
|
"strings"
|
|
|
|
"time"
|
2023-01-26 10:07:29 +01:00
|
|
|
|
|
|
|
"go.fd.io/govpp"
|
|
|
|
"go.fd.io/govpp/api"
|
|
|
|
"go.fd.io/govpp/binapi/af_packet"
|
|
|
|
interfaces "go.fd.io/govpp/binapi/interface"
|
|
|
|
"go.fd.io/govpp/binapi/interface_types"
|
|
|
|
"go.fd.io/govpp/binapi/session"
|
2023-02-07 20:40:27 +01:00
|
|
|
"go.fd.io/govpp/binapi/tapv2"
|
2023-01-26 10:07:29 +01:00
|
|
|
"go.fd.io/govpp/binapi/vpe"
|
|
|
|
"go.fd.io/govpp/core"
|
2022-12-01 09:56:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
const vppConfigTemplate = `unix {
|
|
|
|
nodaemon
|
2023-02-24 11:26:39 +01:00
|
|
|
log %[1]s%[4]s
|
2022-12-01 09:56:37 +01:00
|
|
|
full-coredump
|
|
|
|
cli-listen %[1]s%[2]s
|
|
|
|
runtime-dir %[1]s/var/run
|
|
|
|
gid vpp
|
|
|
|
}
|
|
|
|
|
|
|
|
api-trace {
|
|
|
|
on
|
|
|
|
}
|
|
|
|
|
|
|
|
api-segment {
|
|
|
|
gid vpp
|
|
|
|
}
|
|
|
|
|
|
|
|
socksvr {
|
2023-02-14 12:56:49 +01:00
|
|
|
socket-name %[1]s%[3]s
|
2022-12-01 09:56:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
statseg {
|
|
|
|
socket-name %[1]s/var/run/vpp/stats.sock
|
|
|
|
}
|
|
|
|
|
|
|
|
plugins {
|
2023-01-26 10:07:29 +01:00
|
|
|
plugin default { disable }
|
|
|
|
|
2022-12-01 09:56:37 +01:00
|
|
|
plugin unittest_plugin.so { enable }
|
|
|
|
plugin quic_plugin.so { enable }
|
2023-01-26 10:07:29 +01:00
|
|
|
plugin af_packet_plugin.so { enable }
|
|
|
|
plugin hs_apps_plugin.so { enable }
|
|
|
|
plugin http_plugin.so { enable }
|
2022-12-01 09:56:37 +01:00
|
|
|
}
|
|
|
|
|
2023-02-24 11:26:39 +01:00
|
|
|
logging {
|
|
|
|
default-log-level debug
|
|
|
|
default-syslog-log-level debug
|
|
|
|
}
|
|
|
|
|
2022-12-01 09:56:37 +01:00
|
|
|
`
|
|
|
|
|
2022-12-14 16:30:04 +01:00
|
|
|
const (
|
|
|
|
defaultCliSocketFilePath = "/var/run/vpp/cli.sock"
|
2023-01-26 10:07:29 +01:00
|
|
|
defaultApiSocketFilePath = "/var/run/vpp/api.sock"
|
2023-02-24 11:26:39 +01:00
|
|
|
defaultLogFilePath = "/var/log/vpp/vpp.log"
|
2022-12-14 16:30:04 +01:00
|
|
|
)
|
|
|
|
|
2022-12-01 09:56:37 +01:00
|
|
|
type VppInstance struct {
|
2023-02-21 10:53:20 +01:00
|
|
|
container *Container
|
|
|
|
additionalConfig Stanza
|
|
|
|
connection *core.Connection
|
|
|
|
apiChannel api.Channel
|
2022-12-01 09:56:37 +01:00
|
|
|
}
|
|
|
|
|
2023-02-02 08:58:04 +01:00
|
|
|
func (vpp *VppInstance) Suite() *HstSuite {
|
|
|
|
return vpp.container.suite
|
2022-12-01 09:56:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vpp *VppInstance) getCliSocket() string {
|
2023-02-14 12:56:49 +01:00
|
|
|
return fmt.Sprintf("%s%s", vpp.container.GetContainerWorkDir(), defaultCliSocketFilePath)
|
2022-12-01 09:56:37 +01:00
|
|
|
}
|
|
|
|
|
2023-01-26 10:07:29 +01:00
|
|
|
func (vpp *VppInstance) getRunDir() string {
|
|
|
|
return vpp.container.GetContainerWorkDir() + "/var/run/vpp"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vpp *VppInstance) getLogDir() string {
|
|
|
|
return vpp.container.GetContainerWorkDir() + "/var/log/vpp"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vpp *VppInstance) getEtcDir() string {
|
|
|
|
return vpp.container.GetContainerWorkDir() + "/etc/vpp"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (vpp *VppInstance) start() error {
|
|
|
|
// Create folders
|
|
|
|
containerWorkDir := vpp.container.GetContainerWorkDir()
|
|
|
|
|
|
|
|
vpp.container.exec("mkdir --mode=0700 -p " + vpp.getRunDir())
|
|
|
|
vpp.container.exec("mkdir --mode=0700 -p " + vpp.getLogDir())
|
|
|
|
vpp.container.exec("mkdir --mode=0700 -p " + vpp.getEtcDir())
|
|
|
|
|
|
|
|
// Create startup.conf inside the container
|
2023-02-14 12:56:49 +01:00
|
|
|
configContent := fmt.Sprintf(
|
2023-02-21 10:53:20 +01:00
|
|
|
vppConfigTemplate,
|
|
|
|
containerWorkDir,
|
|
|
|
defaultCliSocketFilePath,
|
|
|
|
defaultApiSocketFilePath,
|
2023-02-24 11:26:39 +01:00
|
|
|
defaultLogFilePath,
|
2023-02-21 10:53:20 +01:00
|
|
|
)
|
2023-02-14 12:56:49 +01:00
|
|
|
configContent += vpp.additionalConfig.ToString()
|
2023-01-26 10:07:29 +01:00
|
|
|
startupFileName := vpp.getEtcDir() + "/startup.conf"
|
|
|
|
vpp.container.createFile(startupFileName, configContent)
|
|
|
|
|
|
|
|
// Start VPP
|
2023-02-24 11:26:39 +01:00
|
|
|
vpp.container.execServer("su -c \"vpp -c " + startupFileName + " &> /proc/1/fd/1\"")
|
2023-01-26 10:07:29 +01:00
|
|
|
|
|
|
|
// Connect to VPP and store the connection
|
|
|
|
sockAddress := vpp.container.GetHostWorkDir() + defaultApiSocketFilePath
|
|
|
|
conn, connEv, err := govpp.AsyncConnect(
|
|
|
|
sockAddress,
|
|
|
|
core.DefaultMaxReconnectAttempts,
|
|
|
|
core.DefaultReconnectInterval)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("async connect error: ", err)
|
|
|
|
}
|
|
|
|
vpp.connection = conn
|
|
|
|
|
|
|
|
// ... wait for Connected event
|
|
|
|
e := <-connEv
|
|
|
|
if e.State != core.Connected {
|
|
|
|
fmt.Println("connecting to VPP failed: ", e.Error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ... check compatibility of used messages
|
|
|
|
ch, err := conn.NewAPIChannel()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("creating channel failed: ", err)
|
|
|
|
}
|
|
|
|
if err := ch.CheckCompatiblity(vpe.AllMessages()...); err != nil {
|
|
|
|
fmt.Println("compatibility error: ", err)
|
|
|
|
}
|
|
|
|
if err := ch.CheckCompatiblity(interfaces.AllMessages()...); err != nil {
|
|
|
|
fmt.Println("compatibility error: ", err)
|
|
|
|
}
|
|
|
|
vpp.apiChannel = ch
|
2022-12-01 09:56:37 +01:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-02 08:58:04 +01:00
|
|
|
func (vpp *VppInstance) vppctl(command string, arguments ...any) string {
|
2023-01-26 10:07:29 +01:00
|
|
|
vppCliCommand := fmt.Sprintf(command, arguments...)
|
|
|
|
containerExecCommand := fmt.Sprintf("docker exec --detach=false %[1]s vppctl -s %[2]s %[3]s",
|
|
|
|
vpp.container.name, vpp.getCliSocket(), vppCliCommand)
|
2023-02-02 08:58:04 +01:00
|
|
|
vpp.Suite().log(containerExecCommand)
|
2023-01-26 10:07:29 +01:00
|
|
|
output, err := exechelper.CombinedOutput(containerExecCommand)
|
2023-02-02 08:58:04 +01:00
|
|
|
vpp.Suite().assertNil(err)
|
2022-12-01 09:56:37 +01:00
|
|
|
|
2023-02-02 08:58:04 +01:00
|
|
|
return string(output)
|
2022-12-01 09:56:37 +01:00
|
|
|
}
|
|
|
|
|
2023-02-07 20:40:27 +01:00
|
|
|
func (vpp *VppInstance) waitForApp(appName string, timeout int) error {
|
|
|
|
for i := 0; i < timeout; i++ {
|
|
|
|
o := vpp.vppctl("show app")
|
|
|
|
if strings.Contains(o, appName) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
time.Sleep(1 * time.Second)
|
2022-12-01 09:56:37 +01:00
|
|
|
}
|
2023-02-14 12:56:49 +01:00
|
|
|
return fmt.Errorf("timeout while waiting for app '%s'", appName)
|
2022-12-01 09:56:37 +01:00
|
|
|
}
|
2023-01-26 10:07:29 +01:00
|
|
|
|
|
|
|
func (vpp *VppInstance) createAfPacket(
|
2023-02-23 13:19:15 +01:00
|
|
|
veth *NetInterface,
|
2023-01-26 10:07:29 +01:00
|
|
|
) (interface_types.InterfaceIndex, error) {
|
|
|
|
createReq := &af_packet.AfPacketCreateV2{
|
|
|
|
UseRandomHwAddr: true,
|
|
|
|
HostIfName: veth.Name(),
|
|
|
|
}
|
2023-02-02 08:58:04 +01:00
|
|
|
if veth.HwAddress() != (MacAddress{}) {
|
2023-01-26 10:07:29 +01:00
|
|
|
createReq.UseRandomHwAddr = false
|
2023-02-02 08:58:04 +01:00
|
|
|
createReq.HwAddr = veth.HwAddress()
|
2023-01-26 10:07:29 +01:00
|
|
|
}
|
|
|
|
createReply := &af_packet.AfPacketCreateV2Reply{}
|
|
|
|
|
|
|
|
if err := vpp.apiChannel.SendRequest(createReq).ReceiveReply(createReply); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2023-02-02 08:58:04 +01:00
|
|
|
veth.SetIndex(createReply.SwIfIndex)
|
2023-01-26 10:07:29 +01:00
|
|
|
|
|
|
|
// Set to up
|
|
|
|
upReq := &interfaces.SwInterfaceSetFlags{
|
2023-02-02 08:58:04 +01:00
|
|
|
SwIfIndex: veth.Index(),
|
2023-01-26 10:07:29 +01:00
|
|
|
Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
|
|
|
|
}
|
|
|
|
upReply := &interfaces.SwInterfaceSetFlagsReply{}
|
|
|
|
|
|
|
|
if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add address
|
2023-02-07 20:40:27 +01:00
|
|
|
if veth.AddressWithPrefix() == (AddressWithPrefix{}) {
|
|
|
|
var err error
|
|
|
|
var ip4Address string
|
2023-02-23 13:19:15 +01:00
|
|
|
if ip4Address, err = veth.addresser.NewIp4Address(veth.Peer().networkNumber); err == nil {
|
2023-02-07 20:40:27 +01:00
|
|
|
veth.SetAddress(ip4Address)
|
|
|
|
} else {
|
|
|
|
return 0, err
|
2023-01-26 10:07:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
addressReq := &interfaces.SwInterfaceAddDelAddress{
|
|
|
|
IsAdd: true,
|
2023-02-02 08:58:04 +01:00
|
|
|
SwIfIndex: veth.Index(),
|
2023-02-07 20:40:27 +01:00
|
|
|
Prefix: veth.AddressWithPrefix(),
|
2023-01-26 10:07:29 +01:00
|
|
|
}
|
|
|
|
addressReply := &interfaces.SwInterfaceAddDelAddressReply{}
|
|
|
|
|
|
|
|
if err := vpp.apiChannel.SendRequest(addressReq).ReceiveReply(addressReply); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2023-02-02 08:58:04 +01:00
|
|
|
return veth.Index(), nil
|
2023-01-26 10:07:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (vpp *VppInstance) addAppNamespace(
|
|
|
|
secret uint64,
|
|
|
|
ifx interface_types.InterfaceIndex,
|
|
|
|
namespaceId string,
|
|
|
|
) error {
|
|
|
|
req := &session.AppNamespaceAddDelV2{
|
|
|
|
Secret: secret,
|
|
|
|
SwIfIndex: ifx,
|
|
|
|
NamespaceID: namespaceId,
|
|
|
|
}
|
|
|
|
reply := &session.AppNamespaceAddDelV2Reply{}
|
|
|
|
|
|
|
|
if err := vpp.apiChannel.SendRequest(req).ReceiveReply(reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sessionReq := &session.SessionEnableDisable{
|
|
|
|
IsEnable: true,
|
|
|
|
}
|
|
|
|
sessionReply := &session.SessionEnableDisableReply{}
|
|
|
|
|
|
|
|
if err := vpp.apiChannel.SendRequest(sessionReq).ReceiveReply(sessionReply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-07 20:40:27 +01:00
|
|
|
func (vpp *VppInstance) createTap(
|
2023-02-23 13:19:15 +01:00
|
|
|
id uint32,
|
|
|
|
tap *NetInterface,
|
2023-02-07 20:40:27 +01:00
|
|
|
) error {
|
|
|
|
createTapReq := &tapv2.TapCreateV2{
|
2023-02-23 13:19:15 +01:00
|
|
|
ID: id,
|
2023-02-07 20:40:27 +01:00
|
|
|
HostIfNameSet: true,
|
2023-02-23 13:19:15 +01:00
|
|
|
HostIfName: tap.Name(),
|
2023-02-07 20:40:27 +01:00
|
|
|
HostIP4PrefixSet: true,
|
2023-02-23 13:19:15 +01:00
|
|
|
HostIP4Prefix: tap.IP4AddressWithPrefix(),
|
2023-02-07 20:40:27 +01:00
|
|
|
}
|
|
|
|
createTapReply := &tapv2.TapCreateV2Reply{}
|
|
|
|
|
|
|
|
// Create tap interface
|
|
|
|
if err := vpp.apiChannel.SendRequest(createTapReq).ReceiveReply(createTapReply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add address
|
|
|
|
addAddressReq := &interfaces.SwInterfaceAddDelAddress{
|
|
|
|
IsAdd: true,
|
|
|
|
SwIfIndex: createTapReply.SwIfIndex,
|
2023-02-23 13:19:15 +01:00
|
|
|
Prefix: tap.Peer().AddressWithPrefix(),
|
2023-02-07 20:40:27 +01:00
|
|
|
}
|
|
|
|
addAddressReply := &interfaces.SwInterfaceAddDelAddressReply{}
|
|
|
|
|
|
|
|
if err := vpp.apiChannel.SendRequest(addAddressReq).ReceiveReply(addAddressReply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set interface to up
|
|
|
|
upReq := &interfaces.SwInterfaceSetFlags{
|
|
|
|
SwIfIndex: createTapReply.SwIfIndex,
|
|
|
|
Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
|
|
|
|
}
|
|
|
|
upReply := &interfaces.SwInterfaceSetFlagsReply{}
|
|
|
|
|
|
|
|
if err := vpp.apiChannel.SendRequest(upReq).ReceiveReply(upReply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-02-24 11:26:39 +01:00
|
|
|
func (vpp *VppInstance) saveLogs() {
|
|
|
|
logTarget := vpp.container.getLogDirPath() + "vppinstance-" + vpp.container.name + ".log"
|
|
|
|
logSource := vpp.container.GetHostWorkDir() + defaultLogFilePath
|
|
|
|
cmd := exec.Command("cp", logSource, logTarget)
|
|
|
|
vpp.Suite().T().Helper()
|
|
|
|
vpp.Suite().log(cmd.String())
|
|
|
|
cmd.Run()
|
|
|
|
}
|
|
|
|
|
2023-01-26 10:07:29 +01:00
|
|
|
func (vpp *VppInstance) disconnect() {
|
|
|
|
vpp.connection.Disconnect()
|
|
|
|
vpp.apiChannel.Close()
|
|
|
|
}
|