1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"path/filepath"
"runtime"
"runtime/pprof"
"time"
"github.com/ovn-org/libovsdb/client"
"github.com/ovn-org/libovsdb/database/inmemory"
"github.com/ovn-org/libovsdb/example/vswitchd"
"github.com/ovn-org/libovsdb/model"
"github.com/ovn-org/libovsdb/ovsdb"
"github.com/ovn-org/libovsdb/server"
)
var (
cpuprofile = flag.String("cpuprofile", "", "write cpu profile to this file")
memprofile = flag.String("memoryprofile", "", "write memory profile to this file")
port = flag.Int("port", 56640, "tcp port to listen on")
)
func main() {
flag.Parse()
var err error
if *cpuprofile != "" {
f, err := os.Create(*cpuprofile)
if err != nil {
log.Fatal(err)
}
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatal(err)
}
defer pprof.StopCPUProfile()
}
clientDBModel, err := vswitchd.FullDatabaseModel()
if err != nil {
log.Fatal(err)
}
wd, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
path := filepath.Join(wd, "vswitchd", "ovs.ovsschema")
f, err := os.Open(path)
if err != nil {
log.Fatal(err)
}
schema, err := ovsdb.SchemaFromFile(f)
if err != nil {
log.Fatal(err)
}
ovsDB := inmemory.NewDatabase(map[string]model.ClientDBModel{
schema.Name: clientDBModel,
})
dbModel, errs := model.NewDatabaseModel(schema, clientDBModel)
if len(errs) > 0 {
log.Fatal(errs)
}
s, err := server.NewOvsdbServer(ovsDB, dbModel)
if err != nil {
log.Fatal(err)
}
defer s.Close()
sig := make(chan os.Signal, 1)
signal.Notify(sig, os.Interrupt)
go func(o *server.OvsdbServer) {
if err := o.Serve("tcp", fmt.Sprintf(":%d", *port)); err != nil {
log.Fatal(err)
}
}(s)
time.Sleep(1 * time.Second)
c, err := client.NewOVSDBClient(clientDBModel, client.WithEndpoint(fmt.Sprintf("tcp::%d", *port)))
if err != nil {
log.Fatal(err)
}
err = c.Connect(context.Background())
if err != nil {
log.Fatal(err)
}
ovsRow := &vswitchd.OpenvSwitch{
UUID: "ovs",
}
ovsOps, err := c.Create(ovsRow)
if err != nil {
log.Fatal(err)
}
reply, err := c.Transact(context.Background(), ovsOps...)
if err != nil {
log.Fatal(err)
}
_, err = ovsdb.CheckOperationResults(reply, ovsOps)
if err != nil {
log.Fatal(err)
}
c.Close()
log.Printf("listening on tcp::%d", *port)
<-sig
if *memprofile != "" {
f, err := os.Create(*memprofile)
if err != nil {
log.Fatal(err)
}
defer f.Close()
runtime.GC()
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatal("could not write memory profile: ", err)
}
}
}
|