blob: 1b09103f29754b423ecf09e03f74b3ad15549334 [file] [log] [blame]
Kris Michielsen91074432017-06-22 13:00:20 +02001"""
2 SRv6 LocalSIDs
3
4 object abstractions for representing SRv6 localSIDs in VPP
5"""
6
Paul Vinciguerraa279d9c2019-02-28 09:00:09 -08007from vpp_object import VppObject
Kris Michielsen91074432017-06-22 13:00:20 +02008from socket import inet_pton, inet_ntop, AF_INET, AF_INET6
Takeru Hayasakac4c205b2022-12-30 16:41:44 +09009import copy
Kris Michielsen91074432017-06-22 13:00:20 +020010
11
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020012class SRv6LocalSIDBehaviors:
Kris Michielsen91074432017-06-22 13:00:20 +020013 # from src/vnet/srv6/sr.h
14 SR_BEHAVIOR_END = 1
15 SR_BEHAVIOR_X = 2
16 SR_BEHAVIOR_T = 3
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020017 SR_BEHAVIOR_D_FIRST = 4 # Unused. Separator in between regular and D
Kris Michielsen91074432017-06-22 13:00:20 +020018 SR_BEHAVIOR_DX2 = 5
19 SR_BEHAVIOR_DX6 = 6
20 SR_BEHAVIOR_DX4 = 7
21 SR_BEHAVIOR_DT6 = 8
22 SR_BEHAVIOR_DT4 = 9
Tetsuya Murakamiec9cb962020-03-23 16:10:28 -070023 SR_BEHAVIOR_END_UN_PERF = 10
24 SR_BEHAVIOR_END_UN = 11
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020025 SR_BEHAVIOR_LAST = 12 # Must always be the last one
Kris Michielsen91074432017-06-22 13:00:20 +020026
27
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020028class SRv6PolicyType:
Kris Michielsen91074432017-06-22 13:00:20 +020029 # from src/vnet/srv6/sr.h
30 SR_POLICY_TYPE_DEFAULT = 0
31 SR_POLICY_TYPE_SPRAY = 1
Takeru Hayasakac4c205b2022-12-30 16:41:44 +090032 SR_POLICY_TYPE_TEF = 2
Kris Michielsen91074432017-06-22 13:00:20 +020033
34
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020035class SRv6PolicySteeringTypes:
Kris Michielsen91074432017-06-22 13:00:20 +020036 # from src/vnet/srv6/sr.h
37 SR_STEER_L2 = 2
38 SR_STEER_IPV4 = 4
39 SR_STEER_IPV6 = 6
40
41
42class VppSRv6LocalSID(VppObject):
43 """
44 SRv6 LocalSID
45 """
46
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020047 def __init__(
48 self,
49 test,
50 localsid,
51 behavior,
52 nh_addr,
53 end_psp,
54 sw_if_index,
55 vlan_index,
56 fib_table,
57 ):
Kris Michielsen91074432017-06-22 13:00:20 +020058 self._test = test
Pablo Camarillo3337bd22018-06-19 15:49:02 +020059 self.localsid = localsid
Kris Michielsen91074432017-06-22 13:00:20 +020060 self.behavior = behavior
Jakub Grajciar0938eba2020-03-04 13:08:27 +010061 self.nh_addr = nh_addr
Kris Michielsen91074432017-06-22 13:00:20 +020062 self.end_psp = end_psp
63 self.sw_if_index = sw_if_index
64 self.vlan_index = vlan_index
65 self.fib_table = fib_table
66 self._configured = False
67
68 def add_vpp_config(self):
69 self._test.vapi.sr_localsid_add_del(
Jakub Grajciar0938eba2020-03-04 13:08:27 +010070 localsid=self.localsid,
71 behavior=self.behavior,
72 nh_addr=self.nh_addr,
Kris Michielsen91074432017-06-22 13:00:20 +020073 is_del=0,
74 end_psp=self.end_psp,
75 sw_if_index=self.sw_if_index,
76 vlan_index=self.vlan_index,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020077 fib_table=self.fib_table,
78 )
Kris Michielsen91074432017-06-22 13:00:20 +020079 self._configured = True
80
81 def remove_vpp_config(self):
82 self._test.vapi.sr_localsid_add_del(
Jakub Grajciar0938eba2020-03-04 13:08:27 +010083 localsid=self.localsid,
84 behavior=self.behavior,
85 nh_addr=self.nh_addr,
Kris Michielsen91074432017-06-22 13:00:20 +020086 is_del=1,
87 end_psp=self.end_psp,
88 sw_if_index=self.sw_if_index,
89 vlan_index=self.vlan_index,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020090 fib_table=self.fib_table,
91 )
Kris Michielsen91074432017-06-22 13:00:20 +020092 self._configured = False
93
94 def query_vpp_config(self):
95 # sr_localsids_dump API is disabled
96 # use _configured flag for now
97 return self._configured
98
Kris Michielsen91074432017-06-22 13:00:20 +020099 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200100 return "%d;%s,%d" % (self.fib_table, self.localsid, self.behavior)
Kris Michielsen91074432017-06-22 13:00:20 +0200101
102
103class VppSRv6Policy(VppObject):
104 """
105 SRv6 Policy
106 """
107
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200108 def __init__(
109 self, test, bsid, is_encap, sr_type, weight, fib_table, segments, source
110 ):
Kris Michielsen91074432017-06-22 13:00:20 +0200111 self._test = test
112 self.bsid = bsid
Kris Michielsen91074432017-06-22 13:00:20 +0200113 self.is_encap = is_encap
114 self.sr_type = sr_type
115 self.weight = weight
116 self.fib_table = fib_table
117 self.segments = segments
Kris Michielsen91074432017-06-22 13:00:20 +0200118 self.n_segments = len(segments)
119 # source not passed to API
120 # self.source = inet_pton(AF_INET6, source)
121 self.source = source
122 self._configured = False
123
124 def add_vpp_config(self):
125 self._test.vapi.sr_policy_add(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200126 bsid=self.bsid,
127 weight=self.weight,
128 is_encap=self.is_encap,
129 is_spray=self.sr_type,
130 fib_table=self.fib_table,
131 sids={"num_sids": self.n_segments, "sids": self.segments},
132 )
Kris Michielsen91074432017-06-22 13:00:20 +0200133 self._configured = True
134
135 def remove_vpp_config(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200136 self._test.vapi.sr_policy_del(self.bsid)
Kris Michielsen91074432017-06-22 13:00:20 +0200137 self._configured = False
138
139 def query_vpp_config(self):
140 # no API to query SR Policies
141 # use _configured flag for now
142 return self._configured
143
Kris Michielsen91074432017-06-22 13:00:20 +0200144 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200145 return "%d;%s-><%s>;%d" % (
146 self.sr_type,
147 self.bsid,
148 ",".join(self.segments),
149 self.is_encap,
150 )
Kris Michielsen91074432017-06-22 13:00:20 +0200151
152
Takeru Hayasakac4c205b2022-12-30 16:41:44 +0900153class VppSRv6PolicyV2(VppObject):
154 """
155 SRv6 Policy
156 """
157
158 def __init__(
159 self,
160 test,
161 bsid,
162 is_encap,
163 sr_type,
164 weight,
165 fib_table,
166 segments,
167 encap_src,
168 source,
169 ):
170 self._test = test
171 self.bsid = bsid
172 self.is_encap = is_encap
173 self.sr_type = sr_type
174 self.weight = weight
175 self.fib_table = fib_table
176 self.segments = segments
177 self.encap_src = encap_src
178 self.n_segments = len(segments)
179
180 # source not passed to API
181 # self.source = inet_pton(AF_INET6, source)
182 self.source = source
183 self._configured = False
184
185 def add_vpp_config(self):
186 self._test.vapi.sr_policy_add_v2(
187 bsid_addr=self.bsid,
188 weight=self.weight,
189 is_encap=self.is_encap,
190 type=self.sr_type,
191 fib_table=self.fib_table,
192 encap_src=self.encap_src,
193 sids={
194 "num_sids": self.n_segments,
195 "sids": self._get_fixed_segments(),
196 "weight": 1,
197 },
198 )
199 self._configured = True
200
201 def remove_vpp_config(self):
202 self._test.vapi.sr_policy_del(self.bsid)
203 self._configured = False
204
205 def query_vpp_config(self):
206 # no API to query SR Policies
207 # use _configured flag for now
208 return self._configured
209
210 def object_id(self):
211 return "%d;%s-><%s>;%d" % (
212 self.sr_type,
213 self.bsid,
214 ",".join(self.segments),
215 self.is_encap,
216 )
217
218 def _get_fixed_segments(self):
219 segs = copy.copy(self.segments)
220 # note: array expect size is 16
221 for _ in range(16 - self.n_segments):
222 segs.append("")
223 return segs
224
225
Kris Michielsen91074432017-06-22 13:00:20 +0200226class VppSRv6Steering(VppObject):
227 """
228 SRv6 Steering
229 """
230
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200231 def __init__(
232 self,
233 test,
234 bsid,
235 prefix,
236 mask_width,
237 traffic_type,
238 sr_policy_index,
239 table_id,
240 sw_if_index,
241 ):
Kris Michielsen91074432017-06-22 13:00:20 +0200242 self._test = test
243 self.bsid = bsid
Kris Michielsen91074432017-06-22 13:00:20 +0200244 self.prefix = prefix
Kris Michielsen91074432017-06-22 13:00:20 +0200245 self.mask_width = mask_width
246 self.traffic_type = traffic_type
247 self.sr_policy_index = sr_policy_index
248 self.sw_if_index = sw_if_index
249 self.table_id = table_id
250 self._configured = False
251
252 def add_vpp_config(self):
253 self._test.vapi.sr_steering_add_del(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200254 is_del=0,
Takeru Hayasakac4c205b2022-12-30 16:41:44 +0900255 bsid_addr=self.bsid,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200256 sr_policy_index=self.sr_policy_index,
257 table_id=self.table_id,
258 prefix={"address": self.prefix, "len": self.mask_width},
259 sw_if_index=self.sw_if_index,
260 traffic_type=self.traffic_type,
261 )
Kris Michielsen91074432017-06-22 13:00:20 +0200262 self._configured = True
263
264 def remove_vpp_config(self):
265 self._test.vapi.sr_steering_add_del(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200266 is_del=1,
Takeru Hayasakac4c205b2022-12-30 16:41:44 +0900267 bsid_addr=self.bsid,
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200268 sr_policy_index=self.sr_policy_index,
269 table_id=self.table_id,
270 prefix={"address": self.prefix, "len": self.mask_width},
271 sw_if_index=self.sw_if_index,
272 traffic_type=self.traffic_type,
273 )
Kris Michielsen91074432017-06-22 13:00:20 +0200274 self._configured = False
275
276 def query_vpp_config(self):
277 # no API to query steering entries
278 # use _configured flag for now
279 return self._configured
280
Kris Michielsen91074432017-06-22 13:00:20 +0200281 def object_id(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200282 return "%d;%d;%s/%d->%s" % (
283 self.table_id,
284 self.traffic_type,
285 self.prefix,
286 self.mask_width,
287 self.bsid,
288 )