Пример #1
0
//DeleteLocalProtoRoute withdraws local endpoints from protocol RIB
func (self *OfnetBgp) DeleteLocalProtoRoute(pathInfo *OfnetProtoRouteInfo) error {

	log.Infof("Received DeleteLocalProtoRoute to withdraw local endpoint to protocol RIB: %v", pathInfo)

	path := &api.Path{
		Pattrs: make([][]byte, 0),
	}

	//form appropraite path attributes for path to be withdrawn
	nlri := bgp.NewIPAddrPrefix(32, pathInfo.localEpIP)
	path.Nlri, _ = nlri.Serialize()
	origin, _ := bgp.NewPathAttributeOrigin(bgp.BGP_ORIGIN_ATTR_TYPE_EGP).Serialize()
	path.Pattrs = append(path.Pattrs, origin)
	aspathParam := []bgp.AsPathParamInterface{bgp.NewAs4PathParam(2, []uint32{self.myBgpAs})}
	aspath, _ := bgp.NewPathAttributeAsPath(aspathParam).Serialize()
	path.Pattrs = append(path.Pattrs, aspath)
	n, _ := bgp.NewPathAttributeNextHop(pathInfo.nextHopIP).Serialize()
	path.Pattrs = append(path.Pattrs, n)
	path.IsWithdraw = true
	name := ""

	arg := &api.ModPathArguments{
		Resource: api.Resource_GLOBAL,
		Name:     name,
		Paths:    []*api.Path{path},
	}

	//send arguement stream
	client := api.NewGobgpApiClient(self.cc)
	if client == nil {
		log.Errorf("Gobgpapi stream invalid")
		return nil
	}

	stream, err := client.ModPath(context.Background())
	if err != nil {
		log.Errorf("Fail to enforce Modpathi: %v", err)
		return err
	}

	err = stream.Send(arg)
	if err != nil {
		log.Errorf("Failed to send strean: %v", err)
		return err
	}
	stream.CloseSend()

	res, e := stream.CloseAndRecv()
	if e != nil {
		log.Errorf("Falied toclose stream ")
		return e
	}

	if res.Code != api.Error_SUCCESS {
		return fmt.Errorf("error: code: %d, msg: %s", res.Code, res.Msg)
	}
	return nil
}
Пример #2
0
/*
Bgp serve routine does the following:
1) Creates inb01 router port
2) Add MyBgp endpoint
3) Kicks off routines to monitor route updates and peer state
*/
func (self *OfnetBgp) StartProtoServer(routerInfo *OfnetProtoRouterInfo) error {
	log.Infof("Starting the Bgp Server with %v", routerInfo)
	//go routine to start gobgp server
	var len uint
	var err error
	self.routerIP, len, err = ParseCIDR(routerInfo.RouterIP)
	as, _ := strconv.Atoi(routerInfo.As)
	self.myBgpAs = uint32(as)

	self.modRibCh = make(chan *api.Path, 16)
	self.advPathCh = make(chan *api.Path, 16)

	timeout := grpc.WithTimeout(time.Second)
	conn, err := grpc.Dial("127.0.0.1:8080", timeout, grpc.WithBlock(), grpc.WithInsecure())
	if err != nil {
		log.Fatal(err)
	}
	self.cc = conn
	defer self.cc.Close()

	client := api.NewGobgpApiClient(self.cc)
	if client == nil {
		log.Errorf("Invalid Gobgpapi client")
		return errors.New("Error creating Gobgpapiclient")
	}
	path := &api.Path{
		Pattrs: make([][]byte, 0),
	}

	path.Nlri, _ = bgp.NewIPAddrPrefix(uint8(32), self.routerIP).Serialize()
	n, _ := bgp.NewPathAttributeNextHop("0.0.0.0").Serialize()
	path.Pattrs = append(path.Pattrs, n)
	origin, _ := bgp.NewPathAttributeOrigin(bgp.BGP_ORIGIN_ATTR_TYPE_INCOMPLETE).Serialize()
	path.Pattrs = append(path.Pattrs, origin)

	log.Debugf("Creating the loopback port ")
	err = self.agent.ovsDriver.CreatePort(self.intfName, "internal", 1)
	if err != nil {
		log.Errorf("Error creating the port: %v", err)
	}

	intfIP := fmt.Sprintf("%s/%d", self.routerIP, len)
	log.Debugf("Creating inb01 with ", intfIP)
	ofPortno, _ := self.agent.ovsDriver.GetOfpPortNo(self.intfName)

	link, err := netlink.LinkByName(self.intfName)
	if err != nil {
		log.Errorf("error finding link by name %v", self.intfName)
		return err
	}
	linkIP, err := netlink.ParseAddr(intfIP)
	if err != nil {
		log.Errorf("invalid ip: %s", intfIP)
		return err
	}
	netlink.AddrAdd(link, linkIP)
	netlink.LinkSetUp(link)
	if link == nil || ofPortno == 0 {
		log.Errorf("Error fetching %v/%v/%v information", self.intfName, link, ofPortno)
		return errors.New("Unable to fetch inb01 info")
	}

	intf, _ := net.InterfaceByName(self.intfName)
	vrf := "default"
	epid := self.agent.getEndpointIdByIpVrf(net.ParseIP(self.routerIP), vrf)
	default_vlan := uint16(1)
	_, ok := self.agent.createVrf(vrf)
	if !ok {
		log.Errorf("Error Creating default vrf for Bgp")
		return errors.New("Error creating default vrf")
	}
	self.agent.vlanVrf[default_vlan] = &vrf

	ep := &OfnetEndpoint{
		EndpointID:   epid,
		EndpointType: "internal-bgp",
		IpAddr:       net.ParseIP(self.routerIP),
		IpMask:       net.ParseIP("255.255.255.255"),
		Vrf:          "default",                  // FIXME set VRF correctly
		MacAddrStr:   intf.HardwareAddr.String(), //link.Attrs().HardwareAddr.String(),
		Vlan:         default_vlan,
		PortNo:       ofPortno,
		Timestamp:    time.Now(),
	}

	// Add the endpoint to local routing table

	err = self.agent.datapath.AddLocalEndpoint(*ep)
	if err != nil {
		log.Errorf("Error Adding Local Bgp Endpoint for endpoint %+v,err: %v", ep, err)
		return err
	}
	self.agent.endpointDb.Set(epid, ep)
	self.agent.localEndpointDb.Set(string(ep.PortNo), ep)

	//Add bgp router id as well
	bgpGlobalCfg := &bgpconf.Global{}
	setDefaultGlobalConfigValues(bgpGlobalCfg)
	bgpGlobalCfg.GlobalConfig.RouterId = net.ParseIP(self.routerIP)
	bgpGlobalCfg.GlobalConfig.As = self.myBgpAs
	self.bgpServer.SetGlobalType(*bgpGlobalCfg)

	//monitor route updates from peer
	go self.monitorBest()
	//monitor peer state
	go self.monitorPeer()
	self.start <- true
	for {
		select {
		case p := <-self.modRibCh:
			err = self.modRib(p)
			if err != nil {
				log.Error("failed to mod rib: ", err)
			}
		case <-self.stop:
			return nil
		}
	}
}