Example #1
File: l2vpn.go Project: araobp/nlan
func addVpls(sVid string, sVni string, ip string, brInt string, con *context.Context) {
	cmd, cmdp := con.GetCmd()
	intBr := "int_br" + sVni
	cmdp("ovs-vsctl", "add-port", brInt, intBr, "tag="+sVid, "--", "set", "interface", intBr, "type=internal")
	cmd("ip", "link", "set", "dev", intBr, "up")
	cmd("ip", "addr", "add", "dev", intBr, ip)
Example #2
File: nodes.go Project: araobp/nlan
func AddNodes(nodes *nlan.Nodes, con *context.Context) (string, string) {
	cmd, cmdp := con.GetCmd()
	brTun := nodes.Ptn
	brInt := nodes.L2Sw
	patchTun := "patch-tun_" + brTun
	patchInt := "patch-int_" + brInt
	log.Printf("Adding bridges: %s and %s\n", brTun, brInt)
	// Adds br-int and br-tun and connects them to each other
	cmdp("ovs-vsctl", "add-br", brInt)
	cmdp("ovs-vsctl", "add-br", brTun)
	cmd("ovs-ofctl", "del-flows", brTun)
	cmdp("ovs-vsctl", "add-port", brInt, patchInt, "--", "set", "interface", patchInt, "type=patch", "options:peer="+patchTun)
	cmdp("ovs-vsctl", "add-port", brTun, patchTun, "--", "set", "interface", patchTun, "type=patch", "options:peer="+patchInt)
	cmdp("ovs-vsctl", "set-fail-mode", brTun, "secure")
	// Obtains ofport for 'patch-tun' port
	patchTunNum := strconv.Itoa(util.GetOfport(patchTun))
	log.Printf("patchTunNum(ofport): %s\n", patchTunNum)
	// Adds flow entries onto br-tun
	cmd("ovs-ofctl", "add-flow", brTun, "table=0,priority=1,in_port="+patchTunNum+",actions=resubmit(,1)")
	cmd("ovs-ofctl", "add-flow", brTun, "table=0,priority=0,actions=drop")
	cmd("ovs-ofctl", "add-flow", brTun, "table=1,priority=0,dl_dst=01:00:00:00:00:00/01:00:00:00:00:00,actions=resubmit(,19)")
	cmd("ovs-ofctl", "add-flow", brTun, "table=1,priority=0,dl_dst=00:00:00:00:00:00/01:00:00:00:00:00,actions=resubmit(,20)")
	cmd("ovs-ofctl", "add-flow", brTun, "table=2,priority=0,actions=drop")
	cmd("ovs-ofctl", "add-flow", brTun, "table=3,priority=0,actions=drop")
	cmd("ovs-ofctl", "add-flow", brTun, "table=10,priority=1,actions=learn(table=20,hard_timeout=300,priority=1,NXM_OF_VLAN_TCI[0..11],NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],load:0->NXM_OF_VLAN_TCI[],load:NXM_NX_TUN_ID[]->NXM_NX_TUN_ID[],output:NXM_OF_IN_PORT[]),output:"+patchTunNum)
	cmd("ovs-ofctl", "add-flow", brTun, "table=19,priority=0,actions=resubmit(,21)")
	cmd("ovs-ofctl", "add-flow", brTun, "table=20,priority=0,actions=resubmit(,21)")
	cmd("ovs-ofctl", "add-flow", brTun, "table=21,priority=0,actions=drop")
	return brTun, brInt
Example #3
func addVhosts(addr ipAddress, vhosts uint32, con *context.Context) {

	cmd, _ := con.GetCmd()
	br := "br_" + addr.abcd
	br_ip := addr.abcd
	cmd("brctl", "addbr", br)
	cmd("ip", "addr", "add", "dev", br, addr.network)
	cmd("ip", "link", "set", "dev", br, "up")
	for i := 1; i <= int(vhosts); i++ {
		d, _ := strconv.Atoi(addr.d)
		id := strconv.Itoa(d + i)
		ip := addr.abc + "." + id
		ns := ip
		cmd("ip", "netns", "add", ns)
		cmd("ip", "link", "add", ns, "type", "veth", "peer", "name", "temp")
		cmd("ip", "link", "set", "dev", "temp", "netns", ns)
		cmd("ip", "netns", "exec", ns, "ip", "link", "set", "dev", "temp", "name", "eth0")
		cmd("brctl", "addif", br, ns)
		cmd("ip", "link", "set", "dev", ns, "promisc", "on")
		cmd("ip", "link", "set", "dev", ns, "up")
		cmd("ip", "netns", "exec", ns, "ip", "link", "set", "dev", "eth0", "up")
		cmd("ip", "netns", "exec", ns, "ip", "addr", "add", "dev", "eth0", ip+"/"+addr.mask)
		cmd("ip", "netns", "exec", ns, "ip", "route", "add", "default", "via", br_ip, "dev", "eth0")
Example #4
func Crud(crud int, in map[string]*nlan.Interface, con *context.Context) {
	// TODO: CRUD operatoins
	cmd, _ := con.GetCmd()
	for dev, params := range in { // map key is not used.
		cmd("ip", "tunnel", "add", dev, "mode", params.Mode, "local", params.Local, "remote", params.Remote)
		cmd("ip", "addr", "add", params.Address, "dev", dev)
		cmd("ip", "link", "set", "dev", dev, "up")
Example #5
func addRouter(loopback string, embedded bool, s *gobgp.BgpServer, ospf []*nlan.Ospf, bgp map[string]*nlan.Attrs, con *context.Context) {

	cmd, cmdp := con.GetCmd()

	// Loopback address
	cmd("ip", "addr", "add", "dev", "lo", loopback)

	// Allow receiving packets from non-best-path interfaces
	cmd("sysctl", "-w", "net.ipv4.conf.all.rp_filter=2")
	// Allow routing packets with local addresses
	cmd("sysctl", "-w", "net.ipv4.conf.all.accept_local=1")

	var script [][]string
	if ospf != nil {
		script = append(script, []string{"router", "ospf"})
		script = append(script, []string{"redistribute", "connected"})
		for _, o := range ospf {
			area := o.Area
			networks := o.Networks
			log.Print("OSPF Networks: %v", networks)
			routerOspfNetworks(&script, area, networks)
		script = append(script, []string{"exit"})
	if bgp != nil {
		for as, neighs := range bgp {
			if embedded {
				routerId := strings.Split(loopback, "/")[0]
				asInt, _ := strconv.ParseInt(as, 10, 64)
				gobgpReqModGlobalConfig(s, routerId, asInt, con)
			} else {
				script = append(script, []string{"router", "bgp", as})
				script = append(script, []string{"redistribute", "connected"})
			if neighs != nil {
				if embedded {
					gobgpReqModNeighbor(s, neighs, con)
				} else {
					routerBgpNeighbors(&script, neighs)
		script = append(script, []string{"exit"})
	if len(script) > 0 && !embedded {
		batch := util.VtyshBatch(script)
		cmdp("vtysh", batch...)
Example #6
File: l2vpn.go Project: araobp/nlan
func addFlowEntries(sVid string, sVni string, peers *[]string, brTun string, con *context.Context) {
	cmd, _ := con.GetCmd()
	_ = "int_br" + sVni
	cmd("ovs-ofctl", "add-flow", brTun, "table=2,priority=1,tun_id="+sVni+",actions=mod_vlan_vid:"+sVid+",resubmit(,10)")
	// Broadcast tree for each vni
	l := util.GetVxlanPorts(peers)
	var buff bytes.Buffer
	for e := l.Front(); e != nil; e = e.Next() {
		v := e.Value
		outPort := strconv.Itoa(v.(int))
	outputPorts := buff.String()
	cmd("ovs-ofctl", "add-flow", brTun, "table=21,priority=1,dl_vlan="+sVid+",actions=strip_vlan,set_tunnel:"+sVni+outputPorts)
Example #7
func Crud(crud int, in *nlan.Router, con *context.Context) {

	loopback := in.Loopback
	embedded := in.EmbeddedBgp // quagga-bgpd(false) or gobgp(true)
	ospf := in.GetOspf()
	bgp := in.GetBgp()
	cmd, _ := con.GetCmd()
	log.Print("Router called...")

	var s *gobgp.BgpServer
	var g *gobgp.Server
	if embedded {
		cmd("/etc/init.d/quagga", "stop")
		s = gobgp.NewBgpServer()
		go s.Serve()
		g = gobgp.NewGrpcServer(50051, s.GrpcReqCh)
		go g.Serve()

	var crudRouter func(string, bool, *gobgp.BgpServer, []*nlan.Ospf, map[string]*nlan.Attrs, *context.Context)

	switch crud {
	case env.ADD:
		crudRouter = addRouter
	case env.UPDATE:
		crudRouter = updateRouter
	case env.DELETE:
		crudRouter = deleteRouter
		log.Fatal("CRUD unidentified")

	log.Printf("Loopback: %s", loopback)
	crudRouter(loopback, embedded, s, ospf, bgp, con)
	log.Print("crudRouter() completed")