Esempio n. 1
0
// NewOvsSwitch Creates a new OVS switch instance
func NewOvsSwitch(bridgeName, netType, localIP string) (*OvsSwitch, error) {
	var err error

	sw := new(OvsSwitch)
	sw.bridgeName = bridgeName
	sw.netType = netType

	// Determine the failure mode
	failMode := ""
	if netType == "vxlan" {
		failMode = "secure"
	}

	// Create OVS db driver
	sw.ovsdbDriver, err = NewOvsdbDriver(bridgeName, failMode)
	if err != nil {
		log.Fatalf("Error creating ovsdb driver. Err: %v", err)
	}

	// For Vxlan, initialize ofnet. For VLAN mode, we use OVS normal forwarding
	if netType == "vxlan" {
		// Create an ofnet agent
		sw.ofnetAgent, err = ofnet.NewOfnetAgent("vxlan", net.ParseIP(localIP),
			ofnet.OFNET_AGENT_PORT, 6633)
		if err != nil {
			log.Fatalf("Error initializing ofnet")
			return nil, err
		}

		// Add controller to the OVS
		ctrlerIP := "127.0.0.1"
		ctrlerPort := uint16(6633)
		target := fmt.Sprintf("tcp:%s:%d", ctrlerIP, ctrlerPort)
		if !sw.ovsdbDriver.IsControllerPresent(target) {
			err = sw.ovsdbDriver.AddController(ctrlerIP, ctrlerPort)
			if err != nil {
				log.Fatalf("Error adding controller to OVS. Err: %v", err)
				return nil, err
			}
		}

		log.Infof("Waiting for OVS switch to connect..")

		// Wait for a while for OVS switch to connect to ofnet agent
		sw.ofnetAgent.WaitForSwitchConnection()

		log.Infof("Switch (vxlan) connected.")
	}

	return sw, nil
}
Esempio n. 2
0
// NewOvsSwitch Creates a new OVS switch instance
func NewOvsSwitch(bridgeName, netType, localIP string) (*OvsSwitch, error) {
	var err error

	sw := new(OvsSwitch)
	sw.bridgeName = bridgeName
	sw.netType = netType

	// Create OVS db driver
	sw.ovsdbDriver = ovsdbDriver.NewOvsDriver(bridgeName)

	// Create an ofnet agent
	sw.ofnetAgent, err = ofnet.NewOfnetAgent(netType, net.ParseIP(localIP), ofnet.OFNET_AGENT_PORT, OVS_CTRLER_PORT)
	if err != nil {
		log.Fatalf("Error initializing ofnet")
		return nil, err
	}

	// Add controller to the OVS
	if !sw.ovsdbDriver.IsControllerPresent(OVS_CTRLER_IP, OVS_CTRLER_PORT) {
		err = sw.ovsdbDriver.AddController(OVS_CTRLER_IP, OVS_CTRLER_PORT)
		if err != nil {
			log.Fatalf("Error adding controller to OVS. Err: %v", err)
			return nil, err
		}
	}

	// Wait a little for master to be ready before we connect
	time.Sleep(300 * time.Millisecond)

	log.Infof("Waiting for OVS switch to connect..")

	// Wait for a while for OVS switch to connect to ofnet agent
	for i := 0; i < 10; i++ {
		time.Sleep(1 * time.Second)
		if sw.ofnetAgent.IsSwitchConnected() {
			break
		}
	}

	log.Infof("Switch connected.")

	return sw, nil
}
Esempio n. 3
0
// NewOvsSwitch Creates a new OVS switch instance
func NewOvsSwitch(bridgeName, netType, localIP string) (*OvsSwitch, error) {
	var err error

	sw := new(OvsSwitch)
	sw.bridgeName = bridgeName
	sw.netType = netType

	// Determine the failure mode
	failMode := ""
	if netType == "vxlan" {
		failMode = "secure"
	}

	// Create OVS db driver
	sw.ovsdbDriver, err = NewOvsdbDriver(bridgeName, failMode)
	if err != nil {
		log.Fatalf("Error creating ovsdb driver. Err: %v", err)
	}

	// For Vxlan, initialize ofnet. For VLAN mode, we use OVS normal forwarding
	if netType == "vxlan" {
		// Create ofnet master
		// FIXME: Move ofnet master to netmaster.
		sw.ofnetMaster = ofnet.NewOfnetMaster(ofnet.OFNET_MASTER_PORT)
		if sw.ofnetMaster == nil {
			log.Fatalf("Error creating ofnet master")
		}

		// Create an ofnet agent
		sw.ofnetAgent, err = ofnet.NewOfnetAgent("vxlan", net.ParseIP(localIP),
			ofnet.OFNET_AGENT_PORT, 6633)
		if err != nil {
			log.Fatalf("Error initializing ofnet")
			return nil, err
		}

		// Add controller to the OVS
		ctrlerIP := "127.0.0.1"
		ctrlerPort := uint16(6633)
		target := fmt.Sprintf("tcp:%s:%d", ctrlerIP, ctrlerPort)
		if !sw.ovsdbDriver.IsControllerPresent(target) {
			err = sw.ovsdbDriver.AddController(ctrlerIP, ctrlerPort)
			if err != nil {
				log.Fatalf("Error adding controller to OVS. Err: %v", err)
				return nil, err
			}
		}

		// Wait a little for master to be ready before we connect
		time.Sleep(300 * time.Millisecond)

		// Let local ofnet agent connect to local master too.
		var resp bool
		masterInfo := ofnet.OfnetNode{
			HostAddr: localIP,
			HostPort: ofnet.OFNET_MASTER_PORT,
		}
		err = sw.ofnetAgent.AddMaster(&masterInfo, &resp)
		if err != nil {
			log.Errorf("Error adding %s as ofnet master. Err: %v", localIP, err)
		}

		log.Infof("Waiting for OVS switch to connect..")

		// Wait for a while for OVS switch to connect to ofnet agent
		for i := 0; i < 10; i++ {
			time.Sleep(1 * time.Second)
			if sw.ofnetAgent.IsSwitchConnected() {
				break
			}
		}

		log.Infof("Switch connected.")
	}

	return sw, nil
}
Esempio n. 4
0
// NewOvsSwitch Creates a new OVS switch instance
func NewOvsSwitch(bridgeName, netType, localIP string, fwdMode string, routerInfo ...string) (*OvsSwitch, error) {
	var err error

	sw := new(OvsSwitch)
	sw.bridgeName = bridgeName
	sw.netType = netType

	// Create OVS db driver
	sw.ovsdbDriver, err = NewOvsdbDriver(bridgeName, "secure")
	if err != nil {
		log.Fatalf("Error creating ovsdb driver. Err: %v", err)
	}

	// For Vxlan, initialize ofnet. For VLAN mode, we use OVS normal forwarding
	if netType == "vxlan" {
		// Create an ofnet agent
		if fwdMode == "bridge" {
			sw.ofnetAgent, err = ofnet.NewOfnetAgent("vxlan", net.ParseIP(localIP),
				vxlanOfnetPort, vxlanCtrlerPort)
		} else if fwdMode == "routing" {
			sw.ofnetAgent, err = ofnet.NewOfnetAgent("vrouter", net.ParseIP(localIP),
				vxlanOfnetPort, vxlanCtrlerPort)
		} else {
			log.Errorf("Invalid Forwarding mode")
			return nil, errors.New("Invalid forwarding mode. Expects 'bridge' or 'routing'")
		}
		if err != nil {
			log.Fatalf("Error initializing ofnet")
			return nil, err
		}

		// Add controller to the OVS
		ctrlerIP := "127.0.0.1"
		target := fmt.Sprintf("tcp:%s:%d", ctrlerIP, vxlanCtrlerPort)
		if !sw.ovsdbDriver.IsControllerPresent(target) {
			err = sw.ovsdbDriver.AddController(ctrlerIP, vxlanCtrlerPort)

			log.Infof("Waiting for OVS switch(%s) to connect..", netType)

			// Wait for a while for OVS switch to connect to ofnet agent
			sw.ofnetAgent.WaitForSwitchConnection()

			log.Infof("Switch (vxlan) connected.")
		}
	}
	if netType == "vlan" {
		// Create an ofnet agent
		if fwdMode == "bridge" {
			// Create an ofnet agent
			sw.ofnetAgent, err = ofnet.NewOfnetAgent(netType, net.ParseIP(localIP), vlanOfnetPort, vlanCtrlerPort)

		} else if fwdMode == "routing" {
			sw.ofnetAgent, err = ofnet.NewOfnetAgent("vlrouter", net.ParseIP(localIP),
				vlanOfnetPort, vlanCtrlerPort, routerInfo...)
		} else {
			log.Errorf("Invalid Forwarding mode")
			return nil, errors.New("Invalid forwarding mode. Expects 'bridge' or 'routing'")
		}

		if err != nil {
			log.Fatalf("Error initializing ofnet")
			return nil, err
		}

		// Add controller to the OVS
		ctrlerIP := "127.0.0.1"
		target := fmt.Sprintf("tcp:%s:%d", ctrlerIP, vlanCtrlerPort)
		if !sw.ovsdbDriver.IsControllerPresent(target) {
			err = sw.ovsdbDriver.AddController(ctrlerIP, vlanCtrlerPort)
			if err != nil {
				log.Fatalf("Error adding controller to OVS. Err: %v", err)
				return nil, err
			}
		}

		log.Infof("Waiting for OVS switch to connect..")

		// Wait for a while for OVS switch to connect to ofnet agent
		sw.ofnetAgent.WaitForSwitchConnection()

		log.Infof("Switch (vlan) connected.")
	}

	return sw, nil
}
Esempio n. 5
0
// NewOvsSwitch Creates a new OVS switch instance
func NewOvsSwitch(bridgeName, netType, localIP string, fwdMode string,
	routerInfo ...string) (*OvsSwitch, error) {
	var err error
	var datapath string
	var ofnetPort, ctrlrPort uint16

	sw := new(OvsSwitch)
	sw.bridgeName = bridgeName
	sw.netType = netType

	// Create OVS db driver
	sw.ovsdbDriver, err = NewOvsdbDriver(bridgeName, "secure")
	if err != nil {
		log.Fatalf("Error creating ovsdb driver. Err: %v", err)
	}

	if netType == "vxlan" {
		ofnetPort = vxlanOfnetPort
		ctrlrPort = vxlanCtrlerPort
		switch fwdMode {
		case "bridge":
			datapath = "vxlan"
		case "routing":
			datapath = "vrouter"
		default:
			log.Errorf("Invalid datapath mode")
			return nil, errors.New("Invalid forwarding mode. Expects 'bridge' or 'routing'")
		}
	} else if netType == "vlan" {
		ofnetPort = vlanOfnetPort
		ctrlrPort = vlanCtrlerPort
		switch fwdMode {
		case "bridge":
			datapath = "vlan"
		case "routing":
			datapath = "vlrouter"
		default:
			log.Errorf("Invalid datapath mode")
			return nil, errors.New("Invalid forwarding mode. Expects 'bridge' or 'routing'")
		}
	}

	// Create an ofnet agent
	sw.ofnetAgent, err = ofnet.NewOfnetAgent(bridgeName, datapath, net.ParseIP(localIP),
		ofnetPort, ctrlrPort, routerInfo...)

	if err != nil {
		log.Fatalf("Error initializing ofnet")
		return nil, err
	}

	// Add controller to the OVS
	ctrlerIP := "127.0.0.1"
	target := fmt.Sprintf("tcp:%s:%d", ctrlerIP, ctrlrPort)
	if !sw.ovsdbDriver.IsControllerPresent(target) {
		err = sw.ovsdbDriver.AddController(ctrlerIP, ctrlrPort)

		log.Infof("Waiting for OVS switch(%s) to connect..", netType)

		// Wait for a while for OVS switch to connect to ofnet agent
		sw.ofnetAgent.WaitForSwitchConnection()

		log.Infof("Switch (%s) connected.", netType)
	}
	return sw, nil
}