Andrew Yourtchenko | 669d07d | 2017-11-17 14:38:18 +0100 | [diff] [blame] | 1 | #!/usr/bin/env python |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 2 | """ |
| 3 | UDP encap objects |
| 4 | """ |
| 5 | |
Paul Vinciguerra | a279d9c | 2019-02-28 09:00:09 -0800 | [diff] [blame] | 6 | from vpp_object import VppObject |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 7 | from socket import inet_pton, inet_ntop, AF_INET, AF_INET6 |
| 8 | |
| 9 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 10 | def find_udp_encap(test, ue): |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 11 | encaps = test.vapi.udp_encap_dump() |
| 12 | for e in encaps: |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 13 | if ue.id == e.udp_encap.id \ |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 14 | and ue.src_ip == str(e.udp_encap.src_ip) \ |
| 15 | and ue.dst_ip == str(e.udp_encap.dst_ip) \ |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 16 | and e.udp_encap.dst_port == ue.dst_port \ |
| 17 | and e.udp_encap.src_port == ue.src_port: |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 18 | return True |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 19 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 20 | return False |
| 21 | |
| 22 | |
| 23 | class VppUdpEncap(VppObject): |
| 24 | |
| 25 | def __init__(self, |
| 26 | test, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 27 | src_ip, |
| 28 | dst_ip, |
| 29 | src_port, |
| 30 | dst_port, |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 31 | table_id=0): |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 32 | self._test = test |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 33 | self.table_id = table_id |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 34 | self.src_ip_s = src_ip |
| 35 | self.dst_ip_s = dst_ip |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 36 | self.src_ip = src_ip |
| 37 | self.dst_ip = dst_ip |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 38 | self.src_port = src_port |
| 39 | self.dst_port = dst_port |
| 40 | |
| 41 | def add_vpp_config(self): |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 42 | r = self._test.vapi.udp_encap_add( |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 43 | self.src_ip, |
| 44 | self.dst_ip, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 45 | self.src_port, |
| 46 | self.dst_port, |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 47 | self.table_id) |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 48 | self.id = r.id |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 49 | self._test.registry.register(self, self._test.logger) |
| 50 | |
| 51 | def remove_vpp_config(self): |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 52 | self._test.vapi.udp_encap_del(self.id) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 53 | |
| 54 | def query_vpp_config(self): |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 55 | return find_udp_encap(self._test, self) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 56 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 57 | def object_id(self): |
| 58 | return ("udp-encap-%d" % self.id) |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 59 | |
| 60 | def get_stats(self): |
| 61 | c = self._test.statistics.get_counter("/net/udp-encap") |
| 62 | return c[0][self.id] |