|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright (c) 2022-2023 Intel Corporation, or its subsidiaries. |
| 3 | +// Copyright (c) 2022-2023 Dell Inc, or its subsidiaries. |
| 4 | +// Copyright (C) 2023 Nordix Foundation. |
| 5 | + |
| 6 | +// Package main is the main package of the application |
| 7 | +package main |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "fmt" |
| 12 | + "io" |
| 13 | + "log" |
| 14 | + "net" |
| 15 | + "net/http" |
| 16 | + "os" |
| 17 | + "os/signal" |
| 18 | + "path/filepath" |
| 19 | + "syscall" |
| 20 | + "time" |
| 21 | + |
| 22 | + pc "github.com/opiproject/opi-api/inventory/v1/gen/go" |
| 23 | + pe "github.com/opiproject/opi-api/network/evpn-gw/v1alpha1/gen/go" |
| 24 | + "github.com/opiproject/opi-evpn-bridge/pkg/bridge" |
| 25 | + "github.com/opiproject/opi-evpn-bridge/pkg/config" |
| 26 | + "github.com/opiproject/opi-evpn-bridge/pkg/infradb" |
| 27 | + "github.com/opiproject/opi-evpn-bridge/pkg/infradb/taskmanager" |
| 28 | + "github.com/opiproject/opi-evpn-bridge/pkg/port" |
| 29 | + "github.com/opiproject/opi-evpn-bridge/pkg/svi" |
| 30 | + "github.com/opiproject/opi-evpn-bridge/pkg/utils" |
| 31 | + "github.com/opiproject/opi-evpn-bridge/pkg/vrf" |
| 32 | + "github.com/opiproject/opi-smbios-bridge/pkg/inventory" |
| 33 | + "github.com/spf13/cobra" |
| 34 | + "github.com/spf13/viper" |
| 35 | + |
| 36 | + "google.golang.org/grpc" |
| 37 | + "google.golang.org/grpc/credentials/insecure" |
| 38 | + "google.golang.org/grpc/reflection" |
| 39 | + |
| 40 | + "github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging" |
| 41 | + "github.com/grpc-ecosystem/grpc-gateway/v2/runtime" |
| 42 | + ci_linux "github.com/opiproject/opi-evpn-bridge/pkg/LinuxCIModule" |
| 43 | + gen_linux "github.com/opiproject/opi-evpn-bridge/pkg/LinuxGeneralModule" |
| 44 | + frr "github.com/opiproject/opi-evpn-bridge/pkg/frr" |
| 45 | + netlink "github.com/opiproject/opi-evpn-bridge/pkg/netlink" |
| 46 | + intel_e2000_linux "github.com/opiproject/opi-intel-bridge/pkg/evpn/LinuxVendorModule/intele2000" |
| 47 | + "github.com/opiproject/opi-intel-bridge/pkg/evpn/vendor_plugins/intel-e2000/p4runtime/p4driverapi" |
| 48 | + ipu_vendor "github.com/opiproject/opi-intel-bridge/pkg/evpn/vendor_plugins/intel-e2000/p4runtime/p4translation" |
| 49 | + "go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc" |
| 50 | +) |
| 51 | + |
| 52 | +const ( |
| 53 | + intelStr = "intel_e2000" |
| 54 | +) |
| 55 | + |
| 56 | +var rootCmd = &cobra.Command{ |
| 57 | + Use: "opi-evpn-bridge", |
| 58 | + Short: "evpn bridge", |
| 59 | + Long: "evpn bridge application", |
| 60 | + |
| 61 | + Run: func(_ *cobra.Command, _ []string) { |
| 62 | + |
| 63 | + taskmanager.TaskMan.StartTaskManager() |
| 64 | + |
| 65 | + err := infradb.NewInfraDB(config.GlobalConfig.DBAddress, config.GlobalConfig.Database) |
| 66 | + if err != nil { |
| 67 | + log.Panicf("Error: %v", err) |
| 68 | + } |
| 69 | + go runGatewayServer(config.GlobalConfig.GRPCPort, config.GlobalConfig.HTTPPort) |
| 70 | + |
| 71 | + switch config.GlobalConfig.Buildenv { |
| 72 | + case intelStr: |
| 73 | + gen_linux.Initialize() |
| 74 | + intel_e2000_linux.Initialize() |
| 75 | + frr.Initialize() |
| 76 | + ipu_vendor.Initialize() |
| 77 | + |
| 78 | + case "ci": |
| 79 | + gen_linux.Initialize() |
| 80 | + ci_linux.Initialize() |
| 81 | + frr.Initialize() |
| 82 | + default: |
| 83 | + log.Panic(" ERROR: Could not find Build env ") |
| 84 | + } |
| 85 | + |
| 86 | + // Create GRD VRF configuration during startup |
| 87 | + if err := createGrdVrf(); err != nil { |
| 88 | + log.Panicf("Error: %v", err) |
| 89 | + } |
| 90 | + switch config.GlobalConfig.Buildenv { |
| 91 | + case intelStr: |
| 92 | + netlink.Initialize() |
| 93 | + default: |
| 94 | + } |
| 95 | + runGrpcServer(config.GlobalConfig.GRPCPort, config.GlobalConfig.TLSFiles) |
| 96 | + |
| 97 | + }, |
| 98 | +} |
| 99 | + |
| 100 | +// initialize the cobra configuration and bind the flags |
| 101 | +func initialize() error { |
| 102 | + cobra.OnInitialize(config.Initcfg) |
| 103 | + |
| 104 | + rootCmd.PersistentFlags().StringVarP(&config.GlobalConfig.CfgFile, "config", "c", "config.yaml", "config file path") |
| 105 | + rootCmd.PersistentFlags().Uint16Var(&config.GlobalConfig.GRPCPort, "grpcport", 50151, "The gRPC server port") |
| 106 | + rootCmd.PersistentFlags().Uint16Var(&config.GlobalConfig.HTTPPort, "httpport", 8082, "The HTTP server port") |
| 107 | + rootCmd.PersistentFlags().StringVar(&config.GlobalConfig.TLSFiles, "tlsfiles", "", "TLS files in server_cert:server_key:ca_cert format.") |
| 108 | + rootCmd.PersistentFlags().StringVar(&config.GlobalConfig.DBAddress, "dbaddress", "127.0.0.1:6379", "db address in ip_address:port format") |
| 109 | + rootCmd.PersistentFlags().StringVar(&config.GlobalConfig.Database, "database", "redis", "Database connection string") |
| 110 | + |
| 111 | + // Bind command-line flags to config fields |
| 112 | + if err := viper.GetViper().BindPFlags(rootCmd.PersistentFlags()); err != nil { |
| 113 | + log.Printf("Error binding flags to Viper: %v\n", err) |
| 114 | + return err |
| 115 | + } |
| 116 | + |
| 117 | + return nil |
| 118 | +} |
| 119 | + |
| 120 | +const logfile string = "opi-evpn-bridge.log" |
| 121 | + |
| 122 | +var logger *log.Logger |
| 123 | + |
| 124 | +// setupLogger sets the config for logger |
| 125 | +func setupLogger(filename string) { |
| 126 | + var err error |
| 127 | + filename = filepath.Clean(filename) |
| 128 | + out, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) |
| 129 | + if err != nil { |
| 130 | + log.Panic(err) |
| 131 | + } |
| 132 | + logger = log.New(io.MultiWriter(out), "", log.Lshortfile|log.LstdFlags) |
| 133 | + log.SetOutput(logger.Writer()) |
| 134 | +} |
| 135 | + |
| 136 | +func cleanUp() { |
| 137 | + log.Println("Defer function called") |
| 138 | + if err := infradb.DeleteAllResources(); err != nil { |
| 139 | + log.Println("Failed to delete all the resources: ", err) |
| 140 | + } |
| 141 | + switch config.GlobalConfig.Buildenv { |
| 142 | + case intelStr: |
| 143 | + gen_linux.DeInitialize() |
| 144 | + intel_e2000_linux.DeInitialize() |
| 145 | + frr.DeInitialize() |
| 146 | + netlink.DeInitialize() |
| 147 | + ipu_vendor.DeInitialize() |
| 148 | + close(p4driverapi.StopCh) |
| 149 | + |
| 150 | + case "ci": |
| 151 | + gen_linux.DeInitialize() |
| 152 | + ci_linux.DeInitialize() |
| 153 | + frr.DeInitialize() |
| 154 | + default: |
| 155 | + log.Panic(" ERROR: Could not find Build env ") |
| 156 | + } |
| 157 | + |
| 158 | + if err := infradb.Close(); err != nil { |
| 159 | + log.Println("Failed to close infradb") |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +// main function |
| 164 | +func main() { |
| 165 | + // setup file and console logger |
| 166 | + setupLogger(logfile) |
| 167 | + |
| 168 | + // initialize cobra config |
| 169 | + if err := initialize(); err != nil { |
| 170 | + // log.Println(err) |
| 171 | + log.Panicf("Error in initialize(): %v", err) |
| 172 | + } |
| 173 | + |
| 174 | + sigChan := make(chan os.Signal, 1) |
| 175 | + // Notify sigChan on SIGINT or SIGTERM. |
| 176 | + signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM) |
| 177 | + |
| 178 | + // This goroutine executes a blocking receive for signals. |
| 179 | + // When it gets one it will then exit the program. |
| 180 | + go func() { |
| 181 | + sig := <-sigChan |
| 182 | + switch sig { |
| 183 | + case syscall.SIGINT: |
| 184 | + cleanUp() |
| 185 | + fmt.Println("Received SIGINT, shutting down.") |
| 186 | + case syscall.SIGTERM: |
| 187 | + cleanUp() |
| 188 | + fmt.Println("Received SIGTERM, shutting down.") |
| 189 | + default: |
| 190 | + fmt.Println("Received unknown signal.") |
| 191 | + } |
| 192 | + // Perform any cleanup tasks here. |
| 193 | + // ... |
| 194 | + |
| 195 | + // Exit the program. |
| 196 | + os.Exit(0) |
| 197 | + }() |
| 198 | + |
| 199 | + // start the main cmd |
| 200 | + if err := rootCmd.Execute(); err != nil { |
| 201 | + log.Panicf("Error in Execute(): %v", err) |
| 202 | + } |
| 203 | + defer cleanUp() |
| 204 | +} |
| 205 | + |
| 206 | +// runGrpcServer start the grpc server for all the components |
| 207 | +func runGrpcServer(grpcPort uint16, tlsFiles string) { |
| 208 | + if config.GlobalConfig.Tracer { |
| 209 | + tp := utils.InitTracerProvider("opi-evpn-bridge") |
| 210 | + defer func() { |
| 211 | + if err := tp.Shutdown(context.Background()); err != nil { |
| 212 | + log.Panicf("Tracer Provider Shutdown: %v", err) |
| 213 | + } |
| 214 | + }() |
| 215 | + } |
| 216 | + |
| 217 | + lis, err := net.Listen("tcp", fmt.Sprintf(":%d", grpcPort)) |
| 218 | + if err != nil { |
| 219 | + log.Panicf("failed to listen: %v", err) |
| 220 | + } |
| 221 | + |
| 222 | + var serverOptions []grpc.ServerOption |
| 223 | + if tlsFiles == "" { |
| 224 | + log.Println("TLS files are not specified. Use insecure connection.") |
| 225 | + } else { |
| 226 | + log.Println("Use TLS certificate files:", tlsFiles) |
| 227 | + config, err := utils.ParseTLSFiles(tlsFiles) |
| 228 | + if err != nil { |
| 229 | + log.Panic("Failed to parse string with tls paths:", err) |
| 230 | + } |
| 231 | + log.Println("TLS config:", config) |
| 232 | + var option grpc.ServerOption |
| 233 | + if option, err = utils.SetupTLSCredentials(config); err != nil { |
| 234 | + log.Panic("Failed to setup TLS:", err) |
| 235 | + } |
| 236 | + serverOptions = append(serverOptions, option) |
| 237 | + } |
| 238 | + |
| 239 | + serverOptions = append(serverOptions, |
| 240 | + grpc.StatsHandler(otelgrpc.NewServerHandler()), |
| 241 | + grpc.UnaryInterceptor( |
| 242 | + logging.UnaryServerInterceptor(utils.InterceptorLogger(log.Default()), |
| 243 | + logging.WithLogOnEvents( |
| 244 | + logging.StartCall, |
| 245 | + logging.FinishCall, |
| 246 | + logging.PayloadReceived, |
| 247 | + logging.PayloadSent, |
| 248 | + ), |
| 249 | + )), |
| 250 | + ) |
| 251 | + s := grpc.NewServer(serverOptions...) |
| 252 | + |
| 253 | + bridgeServer := bridge.NewServer() |
| 254 | + portServer := port.NewServer() |
| 255 | + vrfServer := vrf.NewServer() |
| 256 | + sviServer := svi.NewServer() |
| 257 | + pe.RegisterLogicalBridgeServiceServer(s, bridgeServer) |
| 258 | + pe.RegisterBridgePortServiceServer(s, portServer) |
| 259 | + pe.RegisterVrfServiceServer(s, vrfServer) |
| 260 | + pe.RegisterSviServiceServer(s, sviServer) |
| 261 | + pc.RegisterInventoryServiceServer(s, &inventory.Server{}) |
| 262 | + |
| 263 | + reflection.Register(s) |
| 264 | + |
| 265 | + log.Printf("gRPC server listening at %v", lis.Addr()) |
| 266 | + if err := s.Serve(lis); err != nil { |
| 267 | + log.Panicf("failed to serve: %v", err) |
| 268 | + } |
| 269 | +} |
| 270 | + |
| 271 | +// runGatewayServer |
| 272 | +func runGatewayServer(grpcPort uint16, httpPort uint16) { |
| 273 | + ctx := context.Background() |
| 274 | + ctx, cancel := context.WithCancel(ctx) |
| 275 | + defer cancel() |
| 276 | + |
| 277 | + // Register gRPC server endpoint |
| 278 | + // Note: Make sure the gRPC server is running properly and accessible |
| 279 | + mux := runtime.NewServeMux() |
| 280 | + opts := []grpc.DialOption{grpc.WithTransportCredentials(insecure.NewCredentials())} |
| 281 | + |
| 282 | + // TODO: add/replace with more/less registrations, once opi-api compiler fixed |
| 283 | + err := pc.RegisterInventoryServiceHandlerFromEndpoint(ctx, mux, fmt.Sprintf(":%d", grpcPort), opts) |
| 284 | + if err != nil { |
| 285 | + log.Panic("cannot register handler server") |
| 286 | + } |
| 287 | + |
| 288 | + // Start HTTP server (and proxy calls to gRPC server endpoint) |
| 289 | + log.Printf("HTTP Server listening at %v", httpPort) |
| 290 | + server := &http.Server{ |
| 291 | + Addr: fmt.Sprintf(":%d", httpPort), |
| 292 | + Handler: mux, |
| 293 | + ReadTimeout: 5 * time.Second, |
| 294 | + WriteTimeout: 10 * time.Second, |
| 295 | + } |
| 296 | + err = server.ListenAndServe() |
| 297 | + if err != nil { |
| 298 | + log.Panic("cannot start HTTP gateway server") |
| 299 | + } |
| 300 | +} |
| 301 | + |
| 302 | +// createGrdVrf creates the grd vrf with vni 0 |
| 303 | +func createGrdVrf() error { |
| 304 | + grdVrf, err := infradb.NewVrfWithArgs("//network.opiproject.org/vrfs/GRD", nil, nil, nil) |
| 305 | + if err != nil { |
| 306 | + log.Printf("CreateGrdVrf(): Error in initializing GRD VRF object %+v\n", err) |
| 307 | + return err |
| 308 | + } |
| 309 | + |
| 310 | + err = infradb.CreateVrf(grdVrf) |
| 311 | + if err != nil { |
| 312 | + log.Printf("CreateGrdVrf(): Error in creating GRD VRF object %+v\n", err) |
| 313 | + return err |
| 314 | + } |
| 315 | + |
| 316 | + return nil |
| 317 | +} |
0 commit comments