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