summaryrefslogtreecommitdiffhomepage
path: root/server
diff options
context:
space:
mode:
authorISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>2015-04-04 14:02:46 +0000
committerISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>2015-04-04 14:02:46 +0000
commit01a0fc94cc86a536544ab641bbaf9f54aad1593c (patch)
tree141716b2b98da6bad1a59a2500c2601ddf82c11f /server
parent6d2fec2f4f86edc29a058b4971553fe48b1a7c7c (diff)
api/server: add apis to add/delete routes to/from global rib
add: curl -X POST -d prefix=10.0.0.0/24 http://localhost:8080/v1/bgp/global/ipv4 delete: curl -X DELETE -d prefix=10.0.0.0/24 http://localhost:8080/v1/bgp/global/ipv4 Signed-off-by: ISHIDA Wataru <ishida.wataru@lab.ntt.co.jp>
Diffstat (limited to 'server')
-rw-r--r--server/peer.go65
-rw-r--r--server/peer_test.go18
-rw-r--r--server/server.go3
3 files changed, 76 insertions, 10 deletions
diff --git a/server/peer.go b/server/peer.go
index 362482a7..8c62d105 100644
--- a/server/peer.go
+++ b/server/peer.go
@@ -17,6 +17,7 @@ package server
import (
"encoding/json"
+ "fmt"
log "github.com/Sirupsen/logrus"
"github.com/osrg/gobgp/api"
"github.com/osrg/gobgp/config"
@@ -101,6 +102,9 @@ func NewPeer(g config.Global, peer config.Neighbor, serverMsgCh chan *serverMsg,
LocalID: g.RouterId,
Address: peer.NeighborAddress,
}
+ if isGlobalRib {
+ p.peerInfo.ID = g.RouterId
+ }
rfList := p.configuredRFlist()
p.adjRib = table.NewAdjRib(rfList)
p.rib = table.NewTableManager(p.peerConfig.NeighborAddress.String(), rfList)
@@ -287,6 +291,67 @@ func (peer *Peer) sendMessages(msgs []*bgp.BGPMessage) {
func (peer *Peer) handleREST(restReq *api.RestRequest) {
result := &api.RestResponse{}
switch restReq.RequestType {
+ case api.REQ_GLOBAL_ADD, api.REQ_GLOBAL_DELETE:
+ rf := restReq.RouteFamily
+ prefixes := restReq.Data["prefix"].([]string)
+ var isWithdraw bool
+ if restReq.RequestType == api.REQ_GLOBAL_DELETE {
+ isWithdraw = true
+ }
+
+ pList := make([]table.Path, 0, len(prefixes))
+ for _, prefix := range prefixes {
+ var nlri bgp.AddrPrefixInterface
+ pattr := make([]bgp.PathAttributeInterface, 0)
+ pattr = append(pattr, bgp.NewPathAttributeOrigin(bgp.BGP_ORIGIN_ATTR_TYPE_IGP))
+ asparam := bgp.NewAs4PathParam(bgp.BGP_ASPATH_ATTR_TYPE_SEQ, []uint32{peer.peerInfo.AS})
+ pattr = append(pattr, bgp.NewPathAttributeAsPath([]bgp.AsPathParamInterface{asparam}))
+
+ if rf == bgp.RF_IPv4_UC {
+ ip, net, _ := net.ParseCIDR(prefix)
+ if ip.To4() == nil {
+ result.ResponseErr = fmt.Errorf("Invalid ipv4 prefix: %s", prefix)
+ restReq.ResponseCh <- result
+ close(restReq.ResponseCh)
+ return
+ }
+ ones, _ := net.Mask.Size()
+ nlri = &bgp.NLRInfo{
+ IPAddrPrefix: *bgp.NewIPAddrPrefix(uint8(ones), ip.String()),
+ }
+
+ pattr = append(pattr, bgp.NewPathAttributeNextHop("0.0.0.0"))
+
+ } else if rf == bgp.RF_IPv6_UC {
+ ip, net, _ := net.ParseCIDR(prefix)
+ if ip.To16() == nil {
+ result.ResponseErr = fmt.Errorf("Invalid ipv6 prefix: %s", prefix)
+ restReq.ResponseCh <- result
+ close(restReq.ResponseCh)
+ return
+ }
+ ones, _ := net.Mask.Size()
+ nlri = bgp.NewIPv6AddrPrefix(uint8(ones), ip.String())
+
+ pattr = append(pattr, bgp.NewPathAttributeMpReachNLRI("::", []bgp.AddrPrefixInterface{nlri}))
+
+ } else {
+ result.ResponseErr = fmt.Errorf("Unsupported address family: %s", rf)
+ restReq.ResponseCh <- result
+ close(restReq.ResponseCh)
+ return
+ }
+
+ p := table.CreatePath(peer.peerInfo, nlri, pattr, isWithdraw, time.Now())
+ pList = append(pList, p)
+ }
+
+ pm := &peerMsg{
+ msgType: PEER_MSG_PATH,
+ msgData: pList,
+ }
+ peer.peerMsgCh <- pm
+
case api.REQ_LOCAL_RIB, api.REQ_GLOBAL_RIB:
// just empty so we use ipv4 for any route family
j, _ := json.Marshal(table.NewIPv4Table(0))
diff --git a/server/peer_test.go b/server/peer_test.go
index 23588fdc..90c64960 100644
--- a/server/peer_test.go
+++ b/server/peer_test.go
@@ -123,7 +123,7 @@ func TestPeerAdminShutdownWhileEstablished(t *testing.T) {
peer.connCh <- m
waitUntil(assert, bgp.BGP_FSM_ESTABLISHED, peer, 1000)
- restReq := api.NewRestRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC)
+ restReq := api.NewRestRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil)
msg := &serverMsg{
msgType: SRV_MSG_API,
msgData: restReq,
@@ -166,7 +166,7 @@ func TestPeerAdminShutdownWhileIdle(t *testing.T) {
waitUntil(assert, bgp.BGP_FSM_IDLE, peer, 1000)
- restReq := api.NewRestRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC)
+ restReq := api.NewRestRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil)
msg := &serverMsg{
msgType: SRV_MSG_API,
msgData: restReq,
@@ -201,7 +201,7 @@ func TestPeerAdminShutdownWhileActive(t *testing.T) {
waitUntil(assert, bgp.BGP_FSM_ACTIVE, peer, 1000)
- restReq := api.NewRestRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC)
+ restReq := api.NewRestRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil)
msg := &serverMsg{
msgType: SRV_MSG_API,
msgData: restReq,
@@ -238,7 +238,7 @@ func TestPeerAdminShutdownWhileOpensent(t *testing.T) {
peer.connCh <- m
waitUntil(assert, bgp.BGP_FSM_OPENSENT, peer, 1000)
- restReq := api.NewRestRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC)
+ restReq := api.NewRestRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil)
msg := &serverMsg{
msgType: SRV_MSG_API,
msgData: restReq,
@@ -283,7 +283,7 @@ func TestPeerAdminShutdownWhileOpenconfirm(t *testing.T) {
peer.connCh <- m
waitUntil(assert, bgp.BGP_FSM_OPENCONFIRM, peer, 1000)
- restReq := api.NewRestRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC)
+ restReq := api.NewRestRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil)
msg := &serverMsg{
msgType: SRV_MSG_API,
msgData: restReq,
@@ -334,7 +334,7 @@ func TestPeerAdminEnable(t *testing.T) {
waitUntil(assert, bgp.BGP_FSM_ESTABLISHED, peer, 1000)
// shutdown peer at first
- restReq := api.NewRestRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC)
+ restReq := api.NewRestRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil)
msg := &serverMsg{
msgType: SRV_MSG_API,
msgData: restReq,
@@ -350,7 +350,7 @@ func TestPeerAdminEnable(t *testing.T) {
assert.Equal(ADMIN_STATE_DOWN, peer.fsm.adminState)
// enable peer
- restReq = api.NewRestRequest(api.REQ_NEIGHBOR_ENABLE, "0.0.0.0", bgp.RF_IPv4_UC)
+ restReq = api.NewRestRequest(api.REQ_NEIGHBOR_ENABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil)
msg = &serverMsg{
msgType: SRV_MSG_API,
msgData: restReq,
@@ -397,7 +397,7 @@ func TestPeerAdminShutdownReject(t *testing.T) {
peer.connCh <- m
waitUntil(assert, bgp.BGP_FSM_OPENSENT, peer, 1000)
- restReq := api.NewRestRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC)
+ restReq := api.NewRestRequest(api.REQ_NEIGHBOR_DISABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil)
msg := &serverMsg{
msgType: SRV_MSG_API,
msgData: restReq,
@@ -411,7 +411,7 @@ func TestPeerAdminShutdownReject(t *testing.T) {
json.Unmarshal(result.Data, &res)
assert.Equal("previous request is still remaining", res["result"])
- restReq = api.NewRestRequest(api.REQ_NEIGHBOR_ENABLE, "0.0.0.0", bgp.RF_IPv4_UC)
+ restReq = api.NewRestRequest(api.REQ_NEIGHBOR_ENABLE, "0.0.0.0", bgp.RF_IPv4_UC, nil)
msg = &serverMsg{
msgType: SRV_MSG_API,
msgData: restReq,
diff --git a/server/server.go b/server/server.go
index ebb7a611..a47f9177 100644
--- a/server/server.go
+++ b/server/server.go
@@ -116,6 +116,7 @@ func (server *BgpServer) Serve() {
neighConf := config.Neighbor{
NeighborAddress: g.RouterId,
AfiSafiList: g.AfiSafiList,
+ PeerAs: g.As,
}
server.globalRib = NewPeer(g, neighConf, globalSch, globalPch, nil, true, make(map[string]*policy.Policy))
@@ -320,7 +321,7 @@ func (server *BgpServer) handleRest(restReq *api.RestRequest) {
}
restReq.ResponseCh <- result
close(restReq.ResponseCh)
- case api.REQ_GLOBAL_RIB:
+ case api.REQ_GLOBAL_RIB, api.REQ_GLOBAL_ADD, api.REQ_GLOBAL_DELETE:
msg := &serverMsg{
msgType: SRV_MSG_API,
msgData: restReq,