github.com/osrg/gobgp/v3@v3.30.0/tools/grpc/sr_policy.py (about) 1 #!/usr/bin/env python 2 3 from __future__ import absolute_import 4 from __future__ import print_function 5 6 import grpc 7 from google.protobuf.any_pb2 import Any 8 9 import gobgp_pb2 10 import gobgp_pb2_grpc 11 import attribute_pb2 12 13 _TIMEOUT_SECONDS = 1000 14 15 16 def go_bgp_subnet(color, endpoint_device, target_device, sid_list, bsid_value, nh): 17 """ 18 inject or delete an route with <ACME>-CIDR and <ACME>-SCRUBBING community 19 NLRI 20 ORIGIN 21 AS_PATH 22 LP 23 EXTENDED COMMUNITIES 24 RT 25 TUNNEL ENCAP 26 TLVs 27 SR Policy 28 SUB-TLVs 29 Preference 30 Binding-SID 31 SEG-LIST 32 WEIGHT 33 SEGMENT(1..n) 34 """ 35 channel = grpc.insecure_channel("localhost:50051") 36 stub = gobgp_pb2_grpc.GobgpApiStub(channel) 37 attributes = [] 38 segments = [] 39 # bgp-sr-te safi 40 family = gobgp_pb2.Family( 41 afi=gobgp_pb2.Family.AFI_IP, safi=gobgp_pb2.Family.SAFI_SR_POLICY 42 ) 43 44 # sr-te policy nlri 45 nlri = Any() 46 nlri.Pack( 47 attribute_pb2.SRPolicyNLRI( 48 color=color, 49 distinguisher=2, 50 endpoint=bytes(map(int, endpoint_device.split("."))), 51 length=96, 52 ) 53 ) 54 55 # next-hop 56 next_hop = Any() 57 next_hop.Pack( 58 attribute_pb2.NextHopAttribute( 59 next_hop=nh, 60 ) 61 ) 62 attributes.append(next_hop) 63 64 # Origin 65 origin = Any() 66 origin.Pack(attribute_pb2.OriginAttribute(origin=0)) 67 attributes.append(origin) 68 # Ext RT Communities 69 rt = Any() 70 rt.Pack( 71 attribute_pb2.IPv4AddressSpecificExtended( 72 address=target_device, local_admin=0, sub_type=0x02, is_transitive=False 73 ) 74 ) 75 communities = Any() 76 communities.Pack( 77 attribute_pb2.ExtendedCommunitiesAttribute( 78 communities=[rt], 79 ) 80 ) 81 attributes.append(communities) 82 # generic sid used for bsid 83 sid = Any() 84 sid.Pack( 85 attribute_pb2.SRBindingSID( 86 s_flag=False, i_flag=False, sid=(bsid_value).to_bytes(4, byteorder="big") 87 ) 88 ) 89 # bsid 90 bsid = Any() 91 bsid.Pack(attribute_pb2.TunnelEncapSubTLVSRBindingSID(bsid=sid)) 92 93 # generic segment lbl 94 for n in sid_list: 95 segment = Any() 96 segment.Pack( 97 attribute_pb2.SegmentTypeA( 98 flags=attribute_pb2.SegmentFlags(s_flag=False), label=n << 12 99 ) 100 ) 101 segments.append(segment) 102 # segment list 103 seglist = Any() 104 seglist.Pack( 105 attribute_pb2.TunnelEncapSubTLVSRSegmentList( 106 weight=attribute_pb2.SRWeight(flags=0, weight=12), 107 segments=segments, 108 ) 109 ) 110 # pref 111 pref = Any() 112 pref.Pack(attribute_pb2.TunnelEncapSubTLVSRPreference(flags=0, preference=11)) 113 # path name not used for now 114 cpn = Any() 115 cpn.Pack( 116 attribute_pb2.TunnelEncapSubTLVSRCandidatePathName( 117 candidate_path_name="test-path" 118 ) 119 ) 120 # priority not used for now 121 pri = Any() 122 pri.Pack(attribute_pb2.TunnelEncapSubTLVSRPriority(priority=10)) 123 tun = Any() 124 125 # generate tunnel 126 tun.Pack( 127 attribute_pb2.TunnelEncapAttribute( 128 tlvs=[ 129 attribute_pb2.TunnelEncapTLV( 130 type=15, 131 tlvs=[ 132 pref, 133 bsid, 134 seglist, 135 # cpn, 136 # pri, 137 ], 138 ) 139 ] 140 ) 141 ) 142 143 attributes.append(tun) 144 145 stub.AddPath( 146 gobgp_pb2.AddPathRequest( 147 table_type=gobgp_pb2.GLOBAL, 148 path=gobgp_pb2.Path( 149 nlri=nlri, 150 pattrs=attributes, 151 family=family, 152 best=True, 153 ), 154 ), 155 _TIMEOUT_SECONDS, 156 ) 157 158 159 if __name__ == "__main__": 160 nh = "10.100.1.201" # gobgp ip 161 endpoint_device = "10.6.6.6" # https://datatracker.ietf.org/doc/html/draft-ietf-idr-segment-routing-te-policy-16#section-2.3 162 color = 100 163 target_device = "10.1.1.1" # intended head-ends for the advertised SR Policy update 164 bsid_value = 300004 # bsid 165 sid_list = [200002, 200006] # label stack 166 go_bgp_subnet( 167 color, 168 endpoint_device=endpoint_device, 169 target_device=target_device, 170 bsid_value=bsid_value, 171 sid_list=sid_list, 172 nh=nh, 173 ) 174