Skip to content
Draft
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
11 changes: 11 additions & 0 deletions cmd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
import (
"log"

"github.com/fsnotify/fsnotify"
"github.com/go-playground/validator/v10"
"github.com/spf13/pflag"
"github.com/spf13/viper"
Expand Down Expand Up @@ -88,6 +89,7 @@ func init() {

if err := v.ReadInConfig(); err != nil {
log.Printf("Unable to read config file, %v", err)
isConfigFileAvailable = false
}

log.Printf("Get raw config: %+v", v.AllSettings())
Expand All @@ -101,3 +103,12 @@ func (c *UpfConfig) Validate() error {
func (c *UpfConfig) Unmarshal() error {
return v.UnmarshalExact(c)
}

var isConfigFileAvailable bool = true

func WatchConfig(onConfigChange func(e fsnotify.Event)) {
if isConfigFileAvailable {
viper.OnConfigChange(onConfigChange)
viper.WatchConfig()
}
}
16 changes: 16 additions & 0 deletions cmd/config/startup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,19 @@ func Init() {

log.Printf("Apply eUPF config: %+v", Conf)
}

func ReadConfig() (UpfConfig, error) {
var conf UpfConfig
if err := conf.Unmarshal(); err != nil {
log.Fatalf("Unable to decode into struct, %v", err)
return UpfConfig{}, err
}

if err := conf.Validate(); err != nil {
log.Fatalf("eUPF config is invalid: %v", err)
return UpfConfig{}, err
}

log.Printf("Read config: %+v", conf)
return conf, nil
}
16 changes: 8 additions & 8 deletions cmd/core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ func CreateApiServer(bpfObjects *ebpf.BpfObjects, pfcpSrv *PfcpConnection, forwa
v1.GET("upf_pipeline", ListUpfPipeline(bpfObjects))
v1.GET("xdp_stats", DisplayXdpStatistics(forwardPlaneStats))
v1.GET("packet_stats", DisplayPacketStats(forwardPlaneStats))
config := v1.Group("config")

config := v1.Group("config")
{
config.GET("", DisplayConfig())
config.POST("", EditConfig)
config.POST("", UpdateConfig)
}
pdrMap := v1.Group("uplink_pdr_map")

pdrMap := v1.Group("uplink_pdr_map")
{
pdrMap.GET(":id", GetUplinkPdrValue(bpfObjects))
pdrMap.PUT(":id", SetUplinkPdrValue(bpfObjects))
}
}

qerMap := v1.Group("qer_map")
{
Expand Down Expand Up @@ -79,7 +79,7 @@ func CreateApiServer(bpfObjects *ebpf.BpfObjects, pfcpSrv *PfcpConnection, forwa
return &ApiServer{router: router}
}

func EditConfig(c *gin.Context) {
func UpdateConfig(c *gin.Context) {
var conf config.UpfConfig
if err := c.BindJSON(&conf); err != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{
Expand All @@ -90,7 +90,7 @@ func EditConfig(c *gin.Context) {
}
if err := SetConfig(conf); err != nil {
c.IndentedJSON(http.StatusBadRequest, gin.H{
"message": fmt.Sprintf("Error during editing config: %s", err.Error()),
"message": fmt.Sprintf("Error during updating config: %s", err.Error()),
})
} else {
c.Status(http.StatusOK)
Expand Down
14 changes: 14 additions & 0 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/edgecomllc/eupf/cmd/core"
"github.com/edgecomllc/eupf/cmd/ebpf"
"github.com/fsnotify/fsnotify"

"github.com/cilium/ebpf/link"
"github.com/edgecomllc/eupf/cmd/config"
Expand Down Expand Up @@ -107,6 +108,9 @@ func main() {
}
}()

// Handle config file change.
config.WatchConfig(OnConfigFileChange)

// Print the contents of the BPF hash map (source IP address -> packet count).
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
Expand Down Expand Up @@ -139,3 +143,13 @@ func StringToXDPAttachMode(Mode string) link.XDPAttachFlags {
return link.XDPGenericMode
}
}

func OnConfigFileChange(e fsnotify.Event) {
if e.Has(fsnotify.Create) || e.Has(fsnotify.Write) {
if conf, err := config.ReadConfig(); err == nil {
if err := core.SetConfig(conf); err != nil {
log.Error().Msgf("Error during config file change handling: %s", err.Error())
}
}
}
}