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 | |
| 6 | from vpp_object import * |
| 7 | from socket import inet_pton, inet_ntop, AF_INET, AF_INET6 |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 8 | from vpp_ip import * |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 9 | |
| 10 | |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 11 | def find_udp_encap(test, ue): |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 12 | encaps = test.vapi.udp_encap_dump() |
| 13 | for e in encaps: |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 14 | if ue.id == e.udp_encap.id \ |
Neale Ranns | c0a9314 | 2018-09-05 15:42:26 -0700 | [diff] [blame] | 15 | and ue.src_ip == e.udp_encap.src_ip \ |
| 16 | and ue.dst_ip == e.udp_encap.dst_ip \ |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 17 | and e.udp_encap.dst_port == ue.dst_port \ |
| 18 | and e.udp_encap.src_port == ue.src_port: |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 19 | return True |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 20 | |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 21 | return False |
| 22 | |
| 23 | |
| 24 | class VppUdpEncap(VppObject): |
| 25 | |
| 26 | def __init__(self, |
| 27 | test, |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 28 | src_ip, |
| 29 | dst_ip, |
| 30 | src_port, |
| 31 | dst_port, |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 32 | table_id=0): |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 33 | self._test = test |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 34 | self.table_id = table_id |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 35 | self.src_ip_s = src_ip |
| 36 | self.dst_ip_s = dst_ip |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 37 | self.src_ip = VppIpAddress(src_ip) |
| 38 | self.dst_ip = VppIpAddress(dst_ip) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 39 | self.src_port = src_port |
| 40 | self.dst_port = dst_port |
| 41 | |
| 42 | def add_vpp_config(self): |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 43 | r = self._test.vapi.udp_encap_add( |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 44 | self.src_ip.encode(), |
| 45 | self.dst_ip.encode(), |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 46 | self.src_port, |
| 47 | self.dst_port, |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 48 | self.table_id) |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 49 | self.id = r.id |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 50 | self._test.registry.register(self, self._test.logger) |
| 51 | |
| 52 | def remove_vpp_config(self): |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 53 | self._test.vapi.udp_encap_del(self.id) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 54 | |
| 55 | def query_vpp_config(self): |
Neale Ranns | d0df49f | 2018-08-08 01:06:40 -0700 | [diff] [blame] | 56 | return find_udp_encap(self._test, self) |
Neale Ranns | 810086d | 2017-11-05 16:26:46 -0800 | [diff] [blame] | 57 | |
| 58 | def __str__(self): |
| 59 | return self.object_id() |
| 60 | |
| 61 | def object_id(self): |
| 62 | return ("udp-encap-%d" % self.id) |
Neale Ranns | 9c0a3c4 | 2018-09-07 08:57:41 -0700 | [diff] [blame] | 63 | |
| 64 | def get_stats(self): |
| 65 | c = self._test.statistics.get_counter("/net/udp-encap") |
| 66 | return c[0][self.id] |