blob: 278ff36f1e45cf05ba761ec59a51882ee83bd3a4 [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
Neale Ranns311124e2019-01-24 04:52:25 -080027 def object_id(self):
28 return "ipsec-spd-%d" % self.id
29
30 def query_vpp_config(self):
31 spds = self.test.vapi.ipsec_spds_dump()
32 for spd in spds:
33 if spd.spd_id == self.id:
34 return True
35 return False
36
37
38class VppIpsecSpdItfBinding(VppObject):
39 """
40 VPP SPD DB to interface binding
Paul Vinciguerra8feeaff2019-03-27 11:25:48 -070041 (i.e. this SPD is used on this interface)
Neale Ranns311124e2019-01-24 04:52:25 -080042 """
43
44 def __init__(self, test, spd, itf):
45 self.test = test
46 self.spd = spd
47 self.itf = itf
48
49 def add_vpp_config(self):
50 self.test.vapi.ipsec_interface_add_del_spd(self.spd.id,
51 self.itf.sw_if_index)
52 self.test.registry.register(self, self.test.logger)
53
54 def remove_vpp_config(self):
55 self.test.vapi.ipsec_interface_add_del_spd(self.spd.id,
56 self.itf.sw_if_index,
57 is_add=0)
58
Neale Ranns311124e2019-01-24 04:52:25 -080059 def object_id(self):
60 return "bind-%s-to-%s" % (self.spd.id, self.itf)
61
62 def query_vpp_config(self):
63 bs = self.test.vapi.ipsec_spd_interface_dump()
64 for b in bs:
65 if b.sw_if_index == self.itf.sw_if_index:
66 return True
67 return False
68
69
70class VppIpsecSpdEntry(VppObject):
71 """
72 VPP SPD DB Entry
73 """
74
75 def __init__(self, test, spd, sa_id,
76 local_start, local_stop,
77 remote_start, remote_stop,
78 proto,
79 priority=100,
Neale Ranns17dcec02019-01-09 21:22:20 -080080 policy=None,
Neale Ranns311124e2019-01-24 04:52:25 -080081 is_outbound=1,
82 remote_port_start=0,
83 remote_port_stop=65535,
84 local_port_start=0,
85 local_port_stop=65535):
86 self.test = test
87 self.spd = spd
88 self.sa_id = sa_id
89 self.local_start = ip_address(text_type(local_start))
90 self.local_stop = ip_address(text_type(local_stop))
91 self.remote_start = ip_address(text_type(remote_start))
92 self.remote_stop = ip_address(text_type(remote_stop))
93 self.proto = proto
94 self.is_outbound = is_outbound
95 self.priority = priority
Neale Ranns17dcec02019-01-09 21:22:20 -080096 if not policy:
97 self.policy = (VppEnum.vl_api_ipsec_spd_action_t.
98 IPSEC_API_SPD_ACTION_BYPASS)
99 else:
100 self.policy = policy
Neale Ranns311124e2019-01-24 04:52:25 -0800101 self.is_ipv6 = (0 if self.local_start.version == 4 else 1)
102 self.local_port_start = local_port_start
103 self.local_port_stop = local_port_stop
104 self.remote_port_start = remote_port_start
105 self.remote_port_stop = remote_port_stop
106
107 def add_vpp_config(self):
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800108 rv = self.test.vapi.ipsec_spd_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800109 self.spd.id,
110 self.sa_id,
Neale Ranns17dcec02019-01-09 21:22:20 -0800111 self.local_start,
112 self.local_stop,
113 self.remote_start,
114 self.remote_stop,
Neale Ranns311124e2019-01-24 04:52:25 -0800115 protocol=self.proto,
116 is_ipv6=self.is_ipv6,
117 is_outbound=self.is_outbound,
118 priority=self.priority,
119 policy=self.policy,
120 local_port_start=self.local_port_start,
121 local_port_stop=self.local_port_stop,
122 remote_port_start=self.remote_port_start,
123 remote_port_stop=self.remote_port_stop)
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800124 self.stat_index = rv.stat_index
Neale Ranns311124e2019-01-24 04:52:25 -0800125 self.test.registry.register(self, self.test.logger)
126
127 def remove_vpp_config(self):
Neale Ranns17dcec02019-01-09 21:22:20 -0800128 self.test.vapi.ipsec_spd_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800129 self.spd.id,
130 self.sa_id,
Neale Ranns17dcec02019-01-09 21:22:20 -0800131 self.local_start,
132 self.local_stop,
133 self.remote_start,
134 self.remote_stop,
Neale Ranns311124e2019-01-24 04:52:25 -0800135 protocol=self.proto,
136 is_ipv6=self.is_ipv6,
137 is_outbound=self.is_outbound,
138 priority=self.priority,
139 policy=self.policy,
140 local_port_start=self.local_port_start,
141 local_port_stop=self.local_port_stop,
142 remote_port_start=self.remote_port_start,
143 remote_port_stop=self.remote_port_stop,
144 is_add=0)
145
Neale Ranns311124e2019-01-24 04:52:25 -0800146 def object_id(self):
147 return "spd-entry-%d-%d-%d-%d-%d-%d" % (self.spd.id,
148 self.priority,
149 self.policy,
150 self.is_outbound,
151 self.is_ipv6,
152 self.remote_port_start)
153
154 def query_vpp_config(self):
155 ss = self.test.vapi.ipsec_spd_dump(self.spd.id)
156 for s in ss:
Neale Ranns17dcec02019-01-09 21:22:20 -0800157 if s.entry.sa_id == self.sa_id and \
158 s.entry.is_outbound == self.is_outbound and \
159 s.entry.priority == self.priority and \
160 s.entry.policy == self.policy and \
161 s.entry.remote_address_start == self.remote_start and \
162 s.entry.remote_port_start == self.remote_port_start:
Neale Ranns311124e2019-01-24 04:52:25 -0800163 return True
164 return False
165
Neale Rannsa09c1ff2019-02-04 01:10:30 -0800166 def get_stats(self):
167 c = self.test.statistics.get_counter("/net/ipsec/policy")
168 return c[0][self.stat_index]
169
Neale Ranns311124e2019-01-24 04:52:25 -0800170
171class VppIpsecSA(VppObject):
172 """
173 VPP SAD Entry
174 """
175
176 def __init__(self, test, id, spi,
177 integ_alg, integ_key,
178 crypto_alg, crypto_key,
179 proto,
180 tun_src=None, tun_dst=None,
Neale Ranns17dcec02019-01-09 21:22:20 -0800181 flags=None):
182 e = VppEnum.vl_api_ipsec_sad_flags_t
Neale Ranns311124e2019-01-24 04:52:25 -0800183 self.test = test
184 self.id = id
185 self.spi = spi
186 self.integ_alg = integ_alg
187 self.integ_key = integ_key
188 self.crypto_alg = crypto_alg
189 self.crypto_key = crypto_key
190 self.proto = proto
Neale Ranns17dcec02019-01-09 21:22:20 -0800191
Neale Ranns311124e2019-01-24 04:52:25 -0800192 self.tun_src = tun_src
193 self.tun_dst = tun_dst
Neale Ranns17dcec02019-01-09 21:22:20 -0800194 if not flags:
195 self.flags = e.IPSEC_API_SAD_FLAG_NONE
196 else:
197 self.flags = flags
Neale Ranns311124e2019-01-24 04:52:25 -0800198 if (tun_src):
199 self.tun_src = ip_address(text_type(tun_src))
Neale Ranns17dcec02019-01-09 21:22:20 -0800200 self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL
Neale Ranns311124e2019-01-24 04:52:25 -0800201 if (self.tun_src.version == 6):
Neale Ranns17dcec02019-01-09 21:22:20 -0800202 self.flags = self.flags | e.IPSEC_API_SAD_FLAG_IS_TUNNEL_V6
Neale Ranns311124e2019-01-24 04:52:25 -0800203 if (tun_dst):
204 self.tun_dst = ip_address(text_type(tun_dst))
Neale Ranns311124e2019-01-24 04:52:25 -0800205
206 def add_vpp_config(self):
Neale Rannseba31ec2019-02-17 18:04:27 +0000207 r = self.test.vapi.ipsec_sad_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800208 self.id,
209 self.spi,
210 self.integ_alg,
211 self.integ_key,
212 self.crypto_alg,
213 self.crypto_key,
214 self.proto,
Neale Ranns17dcec02019-01-09 21:22:20 -0800215 (self.tun_src if self.tun_src else []),
216 (self.tun_dst if self.tun_dst else []),
217 flags=self.flags)
Neale Rannseba31ec2019-02-17 18:04:27 +0000218 self.stat_index = r.stat_index
Neale Ranns311124e2019-01-24 04:52:25 -0800219 self.test.registry.register(self, self.test.logger)
220
221 def remove_vpp_config(self):
Neale Ranns17dcec02019-01-09 21:22:20 -0800222 self.test.vapi.ipsec_sad_entry_add_del(
Neale Ranns311124e2019-01-24 04:52:25 -0800223 self.id,
224 self.spi,
225 self.integ_alg,
226 self.integ_key,
227 self.crypto_alg,
228 self.crypto_key,
229 self.proto,
Neale Ranns17dcec02019-01-09 21:22:20 -0800230 (self.tun_src if self.tun_src else []),
231 (self.tun_dst if self.tun_dst else []),
232 flags=self.flags,
Neale Ranns311124e2019-01-24 04:52:25 -0800233 is_add=0)
234
Neale Ranns311124e2019-01-24 04:52:25 -0800235 def object_id(self):
236 return "ipsec-sa-%d" % self.id
237
238 def query_vpp_config(self):
239 bs = self.test.vapi.ipsec_sa_dump()
240 for b in bs:
Neale Ranns8d7c5022019-02-06 01:41:05 -0800241 if b.entry.sad_id == self.id:
Neale Ranns311124e2019-01-24 04:52:25 -0800242 return True
243 return False
Neale Rannseba31ec2019-02-17 18:04:27 +0000244
245 def get_stats(self):
246 c = self.test.statistics.get_counter("/net/ipsec/sa")
247 return c[0][self.stat_index]