blob: cf74b4c978dd4eddcb9b2348662c7ef91ab63cb7 [file] [log] [blame]
Paul Vinciguerraa279d9c2019-02-28 09:00:09 -08001from vpp_object import VppObject
Neale Ranns311124e2019-01-24 04:52:25 -08002from ipaddress import ip_address
Neale Ranns17dcec02019-01-09 21:22:20 -08003from vpp_papi import VppEnum
Neale Ranns311124e2019-01-24 04:52:25 -08004
5try:
6 text_type = unicode
7except NameError:
8 text_type = str
9
10
11class VppIpsecSpd(VppObject):
12 """
13 VPP SPD DB
14 """
15
16 def __init__(self, test, id):
17 self.test = test
18 self.id = id
19
20 def add_vpp_config(self):
21 self.test.vapi.ipsec_spd_add_del(self.id)
22 self.test.registry.register(self, self.test.logger)
23
24 def remove_vpp_config(self):
25 self.test.vapi.ipsec_spd_add_del(self.id, is_add=0)
26
27 def __str__(self):
28 return self.object_id()
29
30 def object_id(self):
31 return "ipsec-spd-%d" % self.id
32
33 def query_vpp_config(self):
34 spds = self.test.vapi.ipsec_spds_dump()
35 for spd in spds:
36 if spd.spd_id == self.id:
37 return True
38 return False
39
40
41class VppIpsecSpdItfBinding(VppObject):
42 """
43 VPP SPD DB to interface binding
44 (i.e. this SPD is used on this interfce)
45 """
46
47 def __init__(self, test, spd, itf):
48 self.test = test
49 self.spd = spd
50 self.itf = itf
51
52 def add_vpp_config(self):
53 self.test.vapi.ipsec_interface_add_del_spd(self.spd.id,
54 self.itf.sw_if_index)
55 self.test.registry.register(self, self.test.logger)
56
57 def remove_vpp_config(self):
58 self.test.vapi.ipsec_interface_add_del_spd(self.spd.id,
59 self.itf.sw_if_index,
60 is_add=0)
61
62 def __str__(self):
63 return self.object_id()
64
65 def object_id(self):
66 return "bind-%s-to-%s" % (self.spd.id, self.itf)
67
68 def query_vpp_config(self):
69 bs = self.test.vapi.ipsec_spd_interface_dump()
70 for b in bs:
71 if b.sw_if_index == self.itf.sw_if_index:
72 return True
73 return False
74
75
76class VppIpsecSpdEntry(VppObject):
77 """
78 VPP SPD DB Entry
79 """
80
81 def __init__(self, test, spd, sa_id,
82 local_start, local_stop,
83 remote_start, remote_stop,
84 proto,
85 priority=100,
Neale Ranns17dcec02019-01-09 21:22:20 -080086 policy=None,
Neale Ranns311124e2019-01-24 04:52:25 -080087 is_outbound=1,
88 remote_port_start=0,
89 remote_port_stop=65535,
90 local_port_start=0,
91 local_port_stop=65535):
92 self.test = test
93 self.spd = spd
94 self.sa_id = sa_id
95 self.local_start = ip_address(text_type(local_start))
96 self.local_stop = ip_address(text_type(local_stop))
97 self.remote_start = ip_address(text_type(remote_start))
98 self.remote_stop = ip_address(text_type(remote_stop))
99 self.proto = proto
100 self.is_outbound = is_outbound
101 self.priority = priority
Neale Ranns17dcec02019-01-09 21:22:20 -0800102 if not policy:
103 self.policy = (VppEnum.vl_api_ipsec_spd_action_t.
104 IPSEC_API_SPD_ACTION_BYPASS)
105 else:
106 self.policy = policy
Neale Ranns311124e2019-01-24 04:52:25 -0800107 self.is_ipv6 = (0 if self.local_start.version == 4 else 1)
108 self.local_port_start = local_port_start
109 self.local_port_stop = local_port_stop
110 self.remote_port_start = remote_port_start
111 self.remote_port_stop = remote_port_stop
112
113 def add_vpp_config(self):
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800114 rv = self.test.vapi.ipsec_spd_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800115 self.spd.id,
116 self.sa_id,
Neale Ranns17dcec02019-01-09 21:22:20 -0800117 self.local_start,
118 self.local_stop,
119 self.remote_start,
120 self.remote_stop,
Neale Ranns311124e2019-01-24 04:52:25 -0800121 protocol=self.proto,
122 is_ipv6=self.is_ipv6,
123 is_outbound=self.is_outbound,
124 priority=self.priority,
125 policy=self.policy,
126 local_port_start=self.local_port_start,
127 local_port_stop=self.local_port_stop,
128 remote_port_start=self.remote_port_start,
129 remote_port_stop=self.remote_port_stop)
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800130 self.stat_index = rv.stat_index
Neale Ranns311124e2019-01-24 04:52:25 -0800131 self.test.registry.register(self, self.test.logger)
132
133 def remove_vpp_config(self):
Neale Ranns17dcec02019-01-09 21:22:20 -0800134 self.test.vapi.ipsec_spd_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800135 self.spd.id,
136 self.sa_id,
Neale Ranns17dcec02019-01-09 21:22:20 -0800137 self.local_start,
138 self.local_stop,
139 self.remote_start,
140 self.remote_stop,
Neale Ranns311124e2019-01-24 04:52:25 -0800141 protocol=self.proto,
142 is_ipv6=self.is_ipv6,
143 is_outbound=self.is_outbound,
144 priority=self.priority,
145 policy=self.policy,
146 local_port_start=self.local_port_start,
147 local_port_stop=self.local_port_stop,
148 remote_port_start=self.remote_port_start,
149 remote_port_stop=self.remote_port_stop,
150 is_add=0)
151
152 def __str__(self):
153 return self.object_id()
154
155 def object_id(self):
156 return "spd-entry-%d-%d-%d-%d-%d-%d" % (self.spd.id,
157 self.priority,
158 self.policy,
159 self.is_outbound,
160 self.is_ipv6,
161 self.remote_port_start)
162
163 def query_vpp_config(self):
164 ss = self.test.vapi.ipsec_spd_dump(self.spd.id)
165 for s in ss:
Neale Ranns17dcec02019-01-09 21:22:20 -0800166 if s.entry.sa_id == self.sa_id and \
167 s.entry.is_outbound == self.is_outbound and \
168 s.entry.priority == self.priority and \
169 s.entry.policy == self.policy and \
170 s.entry.remote_address_start == self.remote_start and \
171 s.entry.remote_port_start == self.remote_port_start:
Neale Ranns311124e2019-01-24 04:52:25 -0800172 return True
173 return False
174
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800175 def get_stats(self):
176 c = self.test.statistics.get_counter("/net/ipsec/policy")
177 return c[0][self.stat_index]
178
Neale Ranns311124e2019-01-24 04:52:25 -0800179
180class VppIpsecSA(VppObject):
181 """
182 VPP SAD Entry
183 """
184
185 def __init__(self, test, id, spi,
186 integ_alg, integ_key,
187 crypto_alg, crypto_key,
188 proto,
189 tun_src=None, tun_dst=None,
Neale Ranns17dcec02019-01-09 21:22:20 -0800190 flags=None):
191 e = VppEnum.vl_api_ipsec_sad_flags_t
Neale Ranns311124e2019-01-24 04:52:25 -0800192 self.test = test
193 self.id = id
194 self.spi = spi
195 self.integ_alg = integ_alg
196 self.integ_key = integ_key
197 self.crypto_alg = crypto_alg
198 self.crypto_key = crypto_key
199 self.proto = proto
Neale Ranns17dcec02019-01-09 21:22:20 -0800200
Neale Ranns311124e2019-01-24 04:52:25 -0800201 self.tun_src = tun_src
202 self.tun_dst = tun_dst
Neale Ranns17dcec02019-01-09 21:22:20 -0800203 if not flags:
204 self.flags = e.IPSEC_API_SAD_FLAG_NONE
205 else:
206 self.flags = flags
Neale Ranns311124e2019-01-24 04:52:25 -0800207 if (tun_src):
208 self.tun_src = ip_address(text_type(tun_src))
Neale Ranns17dcec02019-01-09 21:22:20 -0800209 self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL
Neale Ranns311124e2019-01-24 04:52:25 -0800210 if (self.tun_src.version == 6):
Neale Ranns17dcec02019-01-09 21:22:20 -0800211 self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL_V6
Neale Ranns311124e2019-01-24 04:52:25 -0800212 if (tun_dst):
213 self.tun_dst = ip_address(text_type(tun_dst))
Neale Ranns311124e2019-01-24 04:52:25 -0800214
215 def add_vpp_config(self):
Neale Rannseba31ec2019-02-17 18:04:27 +0000216 r = self.test.vapi.ipsec_sad_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800217 self.id,
218 self.spi,
219 self.integ_alg,
220 self.integ_key,
221 self.crypto_alg,
222 self.crypto_key,
223 self.proto,
Neale Ranns17dcec02019-01-09 21:22:20 -0800224 (self.tun_src if self.tun_src else []),
225 (self.tun_dst if self.tun_dst else []),
226 flags=self.flags)
Neale Rannseba31ec2019-02-17 18:04:27 +0000227 self.stat_index = r.stat_index
Neale Ranns311124e2019-01-24 04:52:25 -0800228 self.test.registry.register(self, self.test.logger)
229
230 def remove_vpp_config(self):
Neale Ranns17dcec02019-01-09 21:22:20 -0800231 self.test.vapi.ipsec_sad_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800232 self.id,
233 self.spi,
234 self.integ_alg,
235 self.integ_key,
236 self.crypto_alg,
237 self.crypto_key,
238 self.proto,
Neale Ranns17dcec02019-01-09 21:22:20 -0800239 (self.tun_src if self.tun_src else []),
240 (self.tun_dst if self.tun_dst else []),
241 flags=self.flags,
Neale Ranns311124e2019-01-24 04:52:25 -0800242 is_add=0)
243
244 def __str__(self):
245 return self.object_id()
246
247 def object_id(self):
248 return "ipsec-sa-%d" % self.id
249
250 def query_vpp_config(self):
251 bs = self.test.vapi.ipsec_sa_dump()
252 for b in bs:
Neale Ranns8d7c5022019-02-06 01:41:05 -0800253 if b.entry.sad_id == self.id:
Neale Ranns311124e2019-01-24 04:52:25 -0800254 return True
255 return False
Neale Rannseba31ec2019-02-17 18:04:27 +0000256
257 def get_stats(self):
258 c = self.test.statistics.get_counter("/net/ipsec/sa")
259 return c[0][self.stat_index]