Renato Botelho do Couto | ead1e53 | 2019-10-31 13:31:07 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
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: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 13 | if ( |
| 14 | ue.id == e.udp_encap.id |
| 15 | and ue.src_ip == str(e.udp_encap.src_ip) |
| 16 | and ue.dst_ip == str(e.udp_encap.dst_ip) |
| 17 | and e.udp_encap.dst_port == ue.dst_port |
| 18 | and e.udp_encap.src_port == ue.src_port |
| 19 | ): |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 20 | return True |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 21 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 22 | return False |
| 23 | |
| 24 | |
| 25 | class VppUdpEncap(VppObject): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 26 | def __init__(self, test, src_ip, dst_ip, src_port, dst_port, table_id=0): |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 27 | self._test = test |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 28 | self.table_id = table_id |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 29 | self.src_ip_s = src_ip |
| 30 | self.dst_ip_s = dst_ip |
Ole Troan | 0bcad32 | 2018-12-11 13:04:01 +0100 | [diff] [blame] | 31 | self.src_ip = src_ip |
| 32 | self.dst_ip = dst_ip |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 33 | self.src_port = src_port |
| 34 | self.dst_port = dst_port |
| 35 | |
| 36 | def add_vpp_config(self): |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 37 | r = self._test.vapi.udp_encap_add( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 38 | self.src_ip, self.dst_ip, self.src_port, self.dst_port, self.table_id |
| 39 | ) |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 40 | self.id = r.id |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 41 | self._test.registry.register(self, self._test.logger) |
| 42 | |
| 43 | def remove_vpp_config(self): |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 44 | self._test.vapi.udp_encap_del(self.id) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 45 | |
| 46 | def query_vpp_config(self): |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 47 | return find_udp_encap(self._test, self) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 48 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 49 | def object_id(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 50 | return "udp-encap-%d" % self.id |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 51 | |
| 52 | def get_stats(self): |
| 53 | c = self._test.statistics.get_counter("/net/udp-encap") |
| 54 | return c[0][self.id] |