Skip to content

Commit ffe50e8

Browse files
sbezverkfujita
authored andcommitted
Add SR policy documentatiom
Signed-off-by: Serguei Bezverkhi <[email protected]>
1 parent 6e4eb28 commit ffe50e8

File tree

2 files changed

+187
-0
lines changed

2 files changed

+187
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Try [a binary release](https://github.com/osrg/gobgp/releases/latest).
3535
- Go Native BGP Library
3636
- [Basics](docs/sources/lib.md)
3737
- [BGP-LS](docs/sources/lib-ls.md)
38+
- [SR Policy](docs/sources/lib-srpolicy.md)
3839
- [Graceful Restart](docs/sources/graceful-restart.md)
3940
- [Additional Paths](docs/sources/add-paths.md)
4041
- [Peer Group](docs/sources/peer-group.md)

docs/sources/lib-srpolicy.md

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
# Using SR Policy in GoBGP library mode
2+
3+
This page explains how to use GoBGP for Injecting SR Policy. This example shows how to build new SR Policy NLRI and associated with NLRI attributes. This attributes are sent as Tunnel Encapsulation of type 15 (SR Policy) SUB TLV's.
4+
5+
## Contents
6+
7+
- [Basic SR Policy Example](#basic-srpolicy-example)
8+
9+
## Basic SR Policy Example
10+
11+
```go
12+
package main
13+
14+
import (
15+
"context"
16+
"encoding/binary"
17+
"fmt"
18+
"net"
19+
"os"
20+
21+
"github.com/golang/protobuf/ptypes"
22+
"github.com/golang/protobuf/ptypes/any"
23+
api "github.com/osrg/gobgp/api"
24+
toolbox "github.com/sbezverk/gobgptoolbox"
25+
"google.golang.org/grpc"
26+
"google.golang.org/protobuf/types/known/anypb"
27+
)
28+
29+
func AddSRPolicy(client api.GobgpApiClient) error {
30+
31+
nlrisr, _ := ptypes.MarshalAny(&api.SRPolicyNLRI{
32+
Length: 96,
33+
Distinguisher: 2,
34+
Color: 99,
35+
Endpoint: net.ParseIP("10.0.0.15").To4(),
36+
})
37+
// Origin attribute
38+
origin, _ := ptypes.MarshalAny(&api.OriginAttribute{
39+
Origin: 0,
40+
})
41+
// Next hop attribute
42+
nh, _ := ptypes.MarshalAny(&api.NextHopAttribute{
43+
NextHop: net.ParseIP("192.168.20.1").To4().String(),
44+
})
45+
// Extended communities attribute
46+
toolbox.MarshalRTFromString("")
47+
rtm, err := toolbox.MarshalRTFromString("10.0.0.8:0")
48+
if err != nil {
49+
return err
50+
}
51+
rt, _ := ptypes.MarshalAny(&api.ExtendedCommunitiesAttribute{
52+
Communities: []*any.Any{rtm},
53+
})
54+
// Tunnel Encapsulation Type 15 (SR Policy) sub tlvs
55+
s := make([]byte, 4)
56+
binary.BigEndian.PutUint32(s, 24321)
57+
sid, err := ptypes.MarshalAny(&api.SRBindingSID{
58+
SFlag: true,
59+
IFlag: false,
60+
Sid: s,
61+
})
62+
if err != nil {
63+
return err
64+
}
65+
bsid, err := ptypes.MarshalAny(&api.TunnelEncapSubTLVSRBindingSID{
66+
Bsid: sid,
67+
})
68+
if err != nil {
69+
return err
70+
}
71+
segment, err := ptypes.MarshalAny(&api.SegmentTypeA{
72+
Flags: &api.SegmentFlags{
73+
SFlag: true,
74+
},
75+
Label: 10203,
76+
})
77+
if err != nil {
78+
return err
79+
}
80+
seglist, err := ptypes.MarshalAny(&api.TunnelEncapSubTLVSRSegmentList{
81+
Weight: &api.SRWeight{
82+
Flags: 0,
83+
Weight: 12,
84+
},
85+
Segments: []*any.Any{segment},
86+
})
87+
if err != nil {
88+
return err
89+
}
90+
pref, err := ptypes.MarshalAny(&api.TunnelEncapSubTLVSRPreference{
91+
Flags: 0,
92+
Preference: 11,
93+
})
94+
if err != nil {
95+
return err
96+
}
97+
cpn, err := ptypes.MarshalAny(&api.TunnelEncapSubTLVSRCandidatePathName{
98+
CandidatePathName: "CandidatePathName",
99+
})
100+
if err != nil {
101+
return err
102+
}
103+
pri, err := ptypes.MarshalAny(&api.TunnelEncapSubTLVSRPriority{
104+
Priority: 10,
105+
})
106+
if err != nil {
107+
return err
108+
}
109+
// Tunnel Encapsulation attribute for SR Policy
110+
tun, err := ptypes.MarshalAny(&api.TunnelEncapAttribute{
111+
Tlvs: []*api.TunnelEncapTLV{
112+
{
113+
Type: 15,
114+
Tlvs: []*anypb.Any{bsid, seglist, pref, cpn, pri},
115+
},
116+
},
117+
})
118+
if err != nil {
119+
return err
120+
}
121+
attrs := []*any.Any{origin, nh, rt, tun}
122+
if _, err := client.AddPath(context.TODO(), &api.AddPathRequest{
123+
TableType: api.TableType_GLOBAL,
124+
Path: &api.Path{
125+
Nlri: nlrisr,
126+
Pattrs: attrs,
127+
Family: &api.Family{Afi: api.Family_AFI_IP, Safi: api.Family_SAFI_SR_POLICY},
128+
Best: true,
129+
SourceAsn: 65000,
130+
},
131+
}); err != nil {
132+
return fmt.Errorf("failed to run AddPath call with error: %v", err)
133+
}
134+
135+
return nil
136+
}
137+
138+
func main() {
139+
conn, err := grpc.DialContext(context.TODO(), "192.168.20.201:50051", grpc.WithInsecure())
140+
if err != nil {
141+
fmt.Printf("fail to connect to gobgp with error: %+v\n", err)
142+
os.Exit(1)
143+
}
144+
client := api.NewGobgpApiClient(conn)
145+
// Testing connection to gobgp by requesting its global config
146+
if _, err := client.GetBgp(context.TODO(), &api.GetBgpRequest{}); err != nil {
147+
fmt.Printf("fail to get gobgp info with error: %+v\n", err)
148+
os.Exit(1)
149+
}
150+
151+
if err := AddSRPolicy(client); err != nil {
152+
fmt.Printf("fail to add SR policy to gobgp with error: %+v\n", err)
153+
os.Exit(1)
154+
}
155+
}
156+
157+
```
158+
159+
## Result of injecting the SR policy
160+
161+
Once the sr policy is injected, gobgp will advertise it to the peers with SR Policy enabled address family. Below is the output collected from Cisco's XRV9K router with enabled SR policy address family. Please note since the information used such as: bsid, endpoint adress etc is not realistic, the router does not install the sr policy, but still, it correctly displays what was programmed.
162+
163+
```
164+
RP/0/RP0/CPU0:xrv9k-r1#sh bgp ipv4 sr-policy [2][99][10.0.0.15]/96
165+
Sun Nov 29 13:05:05.293 EST
166+
BGP routing table entry for [2][99][10.0.0.15]/96
167+
Versions:
168+
Process bRIB/RIB SendTblVer
169+
Speaker 37 37
170+
Last Modified: Nov 29 13:01:21.251 for 00:03:44
171+
Paths: (1 available, best #1)
172+
Not advertised to any peer
173+
Path #1: Received by speaker 0
174+
Not advertised to any peer
175+
Local, (Received from a RR-client)
176+
192.168.20.1 from 192.168.20.201 (192.168.20.201)
177+
Origin IGP, localpref 100, valid, internal, best, group-best
178+
Received Path ID 0, Local Path ID 1, version 37
179+
Extended community: RT:10.0.0.8:0
180+
Tunnel encap attribute type: 15 (SR policy)
181+
bsid 24321, preference 11, num of segment-lists 1
182+
segment-list 1, weight 12
183+
segments: {10203}
184+
Candidate path is not usable
185+
SR policy state is Down, Allocated bsid none
186+
```

0 commit comments

Comments
 (0)