EtherGuard-VPN/tap/tap_vpp.go

377 lines
10 KiB
Go
Raw Normal View History

2021-08-26 11:06:40 +02:00
//+build !novpp
2021-08-23 21:11:01 +02:00
package tap
import (
"context"
2021-08-24 10:43:55 +02:00
"path"
2021-08-23 21:11:01 +02:00
"errors"
"fmt"
"log"
"os"
"sync"
"git.fd.io/govpp.git"
"git.fd.io/govpp.git/adapter/socketclient"
"git.fd.io/govpp.git/binapi/ethernet_types"
interfaces "git.fd.io/govpp.git/binapi/interface"
"git.fd.io/govpp.git/binapi/interface_types"
"git.fd.io/govpp.git/binapi/l2"
"git.fd.io/govpp.git/binapi/memif"
"git.fd.io/govpp.git/extras/libmemif"
2021-08-25 13:54:13 +02:00
"golang.org/x/sys/unix"
2021-08-23 21:11:01 +02:00
"github.com/KusakabeSi/EtherGuardVPN/config"
logger "github.com/sirupsen/logrus"
)
const (
ENV_VPP_MEMIF_SOCKET_DIR = "VPP_MEMIF_SOCKET_DIR"
ENV_VPP_SOCKET_PATH = "VPP_API_SOCKET_PATH"
2021-08-26 14:09:07 +02:00
VPP_SUPPORT = "VPP support enabled"
2021-08-23 21:11:01 +02:00
)
var (
//read from env
vppMemifSocketDir = "/var/run/eggo-vpp"
vppApiSocketPath = socketclient.DefaultSocketName // Path to VPP binary API socket file, default is /run/vpp/api.
//internal
NumQueues = uint8(1)
onConnectWg sync.WaitGroup
tunErrorChannel chan error
)
type VppTap struct {
name string
mtu int
ifuid uint32
memifSockPath string
SwIfIndex interface_types.InterfaceIndex
secret string
memif *libmemif.Memif
RxQueues int
RxintCh <-chan uint8
RxintChNext chan uint8
RxintErrCh <-chan error
TxQueues int
TxCount uint
logger *logger.Logger
errors chan error // async error handling
events chan Event
}
// New creates and returns a new TUN interface for the application.
func CreateVppTAP(iconfig config.InterfaceConf,NodeID config.Vertex, loglevel string) (tapdev Device, err error) {
2021-08-23 21:11:01 +02:00
// Setup TUN Config
2021-08-25 13:54:13 +02:00
if len(iconfig.Name) >= unix.IFNAMSIZ {
return nil, fmt.Errorf("interface name too long: %w", unix.ENAMETOOLONG)
}
2021-08-23 21:11:01 +02:00
// Set logger
log := logger.New()
log.Out = os.Stdout
log.Level = func() logger.Level {
switch loglevel {
case "verbose", "debug":
return logger.DebugLevel
case "error":
return logger.ErrorLevel
case "silent":
return logger.PanicLevel
}
return logger.ErrorLevel
}()
libmemif.SetLogger(log)
2021-08-24 10:43:55 +02:00
if os.Getenv(ENV_VPP_MEMIF_SOCKET_DIR) != "" {
vppMemifSocketDir = os.Getenv(ENV_VPP_MEMIF_SOCKET_DIR)
}
if os.Getenv(ENV_VPP_SOCKET_PATH) != "" {
vppApiSocketPath = os.Getenv(ENV_VPP_SOCKET_PATH)
}
if err := os.MkdirAll(vppMemifSocketDir, 0755); err != nil {
log.Fatalln("ERROR: Failed to create VPP memif socket folder " + vppMemifSocketDir)
return nil, err
}
2021-08-23 21:11:01 +02:00
// connect to VPP
conn, err := govpp.Connect(vppApiSocketPath)
if err != nil {
2021-08-24 10:43:55 +02:00
log.Fatalln("ERROR: Connecting to VPP failed:", err)
2021-08-23 21:11:01 +02:00
return nil, err
}
defer conn.Disconnect()
// create a channel
ch, err := conn.NewAPIChannel()
if err != nil {
log.Fatalln("ERROR: creating channel failed:", err)
return nil, err
}
defer ch.Close()
if err := ch.CheckCompatiblity(&memif.MemifSocketFilenameAddDel{}, &memif.MemifCreate{}, &memif.MemifDelete{}); err != nil {
return nil, err
}
if err := ch.CheckCompatiblity(&interfaces.SwInterfaceSetFlags{}, &interfaces.SwInterfaceSetMtu{}); err != nil {
return nil, err
}
if err := ch.CheckCompatiblity(&l2.L2fibAddDel{}, &l2.SwInterfaceSetL2Bridge{}); err != nil {
return nil, err
}
memifservice := memif.NewServiceClient(conn)
l2service := l2.NewServiceClient(conn)
interfacservice := interfaces.NewServiceClient(conn)
IfMacAddr, err := GetMacAddr(iconfig.MacAddrPrefix,uint32(NodeID))
2021-08-23 21:11:01 +02:00
if err != nil {
2021-08-24 10:43:55 +02:00
log.Fatalln("ERROR: Failed parse mac address:", iconfig.MacAddrPrefix)
2021-08-23 21:11:01 +02:00
return nil, err
}
2021-08-24 10:43:55 +02:00
vppIfMacAddr := ethernet_types.MacAddress(IfMacAddr)
2021-08-23 21:11:01 +02:00
tap := &VppTap{
name: iconfig.Name,
mtu: iconfig.MTU,
ifuid: iconfig.VPPIfaceID,
SwIfIndex: 0,
2021-08-24 10:43:55 +02:00
memifSockPath: path.Join(vppMemifSocketDir, iconfig.Name+".sock"),
2021-08-23 21:11:01 +02:00
secret: config.RandomStr(16, iconfig.Name),
logger: log,
2021-08-26 11:06:40 +02:00
RxintChNext: make(chan uint8, 1<<6),
2021-08-23 21:11:01 +02:00
errors: make(chan error, 1<<5),
events: make(chan Event, 1<<4),
}
// create memif socket id 1 filename /tmp/icmp-responder-example
_, err = memifservice.MemifSocketFilenameAddDel(context.Background(), &memif.MemifSocketFilenameAddDel{
IsAdd: true,
SocketID: iconfig.VPPIfaceID,
SocketFilename: tap.memifSockPath,
})
if err != nil {
return nil, err
}
// create interface memif id 1 socket-id 1 slave secret secret no-zero-copy
memifCreateReply, err := memifservice.MemifCreate(context.Background(), &memif.MemifCreate{
Role: memif.MEMIF_ROLE_API_SLAVE,
Mode: memif.MEMIF_MODE_API_ETHERNET,
RxQueues: NumQueues, // MEMIF_DEFAULT_RX_QUEUES
TxQueues: NumQueues, // MEMIF_DEFAULT_TX_QUEUES
ID: tap.ifuid,
SocketID: tap.ifuid,
RingSize: 1024, // MEMIF_DEFAULT_RING_SIZE
BufferSize: 2048, // MEMIF_DEFAULT_BUFFER_SIZE 2048
NoZeroCopy: true,
HwAddr: vppIfMacAddr,
Secret: tap.secret,
})
if err != nil {
return nil, err
}
tap.SwIfIndex = memifCreateReply.SwIfIndex
// set int state memif1/1 up
_, err = interfacservice.SwInterfaceSetFlags(context.Background(), &interfaces.SwInterfaceSetFlags{
SwIfIndex: tap.SwIfIndex,
Flags: interface_types.IF_STATUS_API_FLAG_ADMIN_UP,
})
if err != nil {
return nil, err
}
2021-08-26 14:09:07 +02:00
if iconfig.VPPBridgeID != 0 {
//set interface l2 bridge memif1/1 4242
_, err = l2service.SwInterfaceSetL2Bridge(context.Background(), &l2.SwInterfaceSetL2Bridge{
RxSwIfIndex: tap.SwIfIndex,
BdID: iconfig.VPPBridgeID,
PortType: l2.L2_API_PORT_TYPE_NORMAL,
Shg: 0,
Enable: true,
})
if err != nil {
return nil, err
}
2021-08-23 21:11:01 +02:00
}
//init libmemif
libmemif.Init(tap.name)
onConnectWg.Add(1)
memifCallbacks := &libmemif.MemifCallbacks{
OnConnect: OnConnect,
OnDisconnect: OnDisconnect,
}
// Prepare memif1 configuration.
memifConfig := &libmemif.MemifConfig{
MemifMeta: libmemif.MemifMeta{
IfName: tap.name,
ConnID: tap.ifuid,
SocketFilename: tap.memifSockPath,
Secret: tap.secret,
IsMaster: true,
Mode: libmemif.IfModeEthernet,
},
MemifShmSpecs: libmemif.MemifShmSpecs{
NumRxQueues: NumQueues,
NumTxQueues: NumQueues,
BufferSize: 2048,
Log2RingSize: 10,
},
}
// Create memif1 interface.
memif, err := libmemif.CreateInterface(memifConfig, memifCallbacks)
if err != nil {
tap.logger.Errorf("libmemif.CreateInterface() error: %v\n", err)
return nil, err
}
onConnectWg.Wait()
_, err = interfacservice.SwInterfaceSetMtu(context.Background(), &interfaces.SwInterfaceSetMtu{
SwIfIndex: tap.SwIfIndex,
Mtu: []uint32{uint32(tap.mtu)},
})
if err != nil {
return nil, err
}
tap.memif = memif
details, err := memif.GetDetails()
tap.RxQueues = len(details.RxQueues)
tap.RxintCh = memif.GetInterruptChan()
tap.RxintErrCh = memif.GetInterruptErrorChan()
tap.TxQueues = len(details.TxQueues)
2021-08-26 13:33:35 +02:00
tunErrorChannel = tap.errors
2021-08-26 11:06:40 +02:00
tap.events <- EventUp
return tap, nil
2021-08-23 21:11:01 +02:00
}
// SetMTU sets the Maximum Tansmission Unit Size for a
// Packet on the interface.
func (tap *VppTap) Read(buf []byte, offset int) (n int, err error) {
select {
case err = <-tap.RxintErrCh:
tap.logger.Errorf("libmemif.Memif.RxintErr() error: %v\n", err)
return 0, err
case err = <-tap.errors:
if err == nil {
err = errors.New("Device closed")
}
tap.logger.Errorf("tun error: %v\n", err)
return 0, err
case queueID := <-tap.RxintCh:
select {
case tap.RxintChNext <- queueID:
{
// Use non-blocking write to prevent program stuck
}
default:
tap.logger.Debugln("Buffer full")
}
case queueID := <-tap.RxintChNext:
packets, err := tap.memif.RxBurst(queueID, 1)
if err != nil {
tap.logger.Errorf("libmemif.Memif.RxBurst() error: %v\n", err)
return 0, err
}
if len(packets) == 0 {
// No more packets to read until the next interrupt.
return 0, nil
}
for _, packetData := range packets {
select {
case tap.RxintChNext <- queueID:
{
// Use non-blocking write to prevent program stuck
// repeatedly call RxBurst() until returns an empty slice of packets
}
default:
tap.logger.Debugln("Buffer full")
}
n = copy(buf[offset:], packetData)
}
}
return
} // read a packet from the device (without any additional headers)
func (tap *VppTap) Write(buf []byte, offset int) (size int, err error) {
queueID := tap.getTxQueueID()
buf = buf[offset:]
n, err := tap.memif.TxBurst(queueID, []libmemif.RawPacketData{buf})
return len(buf) * int(n), err
} // writes a packet to the device (without any additional headers)
func (tap *VppTap) Flush() error {
return nil
} // flush all previous writes to the device
func (tap *VppTap) MTU() (int, error) {
return tap.mtu, nil
} // returns the MTU of the device
func (tap *VppTap) Name() (string, error) {
return tap.name, nil
} // fetches and returns the current name
func (tap *VppTap) Events() chan Event {
return tap.events
} // returns a constant channel of events related to the device
func (tap *VppTap) Close() error {
// connect to VPP
conn, err := govpp.Connect(vppApiSocketPath)
if err != nil {
log.Fatalln("ERROR: connecting to VPP failed:", err)
}
defer conn.Disconnect()
// create a channel
ch, err := conn.NewAPIChannel()
if err != nil {
log.Fatalln("ERROR: creating channel failed:", err)
}
defer ch.Close()
memifservice := memif.NewServiceClient(conn)
// delete interface memif memif1/1
_, err = memifservice.MemifDelete(context.Background(), &memif.MemifDelete{
SwIfIndex: tap.SwIfIndex,
})
// delete memif socket id 1
_, err = memifservice.MemifSocketFilenameAddDel(context.Background(), &memif.MemifSocketFilenameAddDel{
IsAdd: false,
SocketID: tap.ifuid,
SocketFilename: tap.memifSockPath,
})
2021-08-26 13:33:35 +02:00
tap.memif.Close()
libmemif.Cleanup()
2021-08-23 21:11:01 +02:00
tap.events <- EventDown
2021-08-26 11:06:40 +02:00
close(tap.errors)
2021-08-23 21:11:01 +02:00
close(tap.events)
return nil
} // stops the device and closes the event channel
// OnConnect is called when a memif connection gets established.
func OnConnect(memif *libmemif.Memif) (err error) {
details, err := memif.GetDetails()
if err != nil {
fmt.Printf("libmemif.GetDetails() error: %v\n", err)
}
fmt.Printf("memif %s has been connected: %+v\n", memif.IfName, details)
onConnectWg.Done()
return nil
}
// OnDisconnect is called when a memif connection is lost.
func OnDisconnect(memif *libmemif.Memif) (err error) {
tunErrorChannel <- errors.New(fmt.Sprintf("memif %s has been disconnected", memif.IfName))
return nil
}
func (tun *VppTap) getTxQueueID() uint8 {
if tun.TxQueues == 1 {
return 0
}
tun.TxCount++
return uint8(tun.TxCount % uint(tun.TxQueues))
}