Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 1 | from vpp_interface import VppInterface |
| 2 | from vpp_papi import VppEnum |
| 3 | |
| 4 | |
| 5 | INDEX_INVALID = 0xffffffff |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 6 | DEFAULT_PORT = 4789 |
| 7 | UNDEFINED_PORT = 0 |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 8 | |
| 9 | |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 10 | def find_vxlan_tunnel(test, src, dst, s_port, d_port, vni): |
| 11 | ts = test.vapi.vxlan_tunnel_v2_dump(INDEX_INVALID) |
| 12 | |
| 13 | src_port = DEFAULT_PORT |
| 14 | if s_port != UNDEFINED_PORT: |
| 15 | src_port = s_port |
| 16 | |
| 17 | dst_port = DEFAULT_PORT |
| 18 | if d_port != UNDEFINED_PORT: |
| 19 | dst_port = d_port |
| 20 | |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 21 | for t in ts: |
| 22 | if src == str(t.src_address) and \ |
| 23 | dst == str(t.dst_address) and \ |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 24 | src_port == t.src_port and \ |
| 25 | dst_port == t.dst_port and \ |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 26 | t.vni == vni: |
| 27 | return t.sw_if_index |
| 28 | return INDEX_INVALID |
| 29 | |
| 30 | |
| 31 | class VppVxlanTunnel(VppInterface): |
| 32 | """ |
| 33 | VPP VXLAN interface |
| 34 | """ |
| 35 | |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 36 | def __init__(self, test, src, dst, vni, |
| 37 | src_port=UNDEFINED_PORT, dst_port=UNDEFINED_PORT, |
| 38 | mcast_itf=None, |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 39 | mcast_sw_if_index=INDEX_INVALID, |
| 40 | decap_next_index=INDEX_INVALID, |
| 41 | encap_vrf_id=None, instance=0xffffffff): |
| 42 | """ Create VXLAN Tunnel interface """ |
| 43 | super(VppVxlanTunnel, self).__init__(test) |
| 44 | self.src = src |
| 45 | self.dst = dst |
| 46 | self.vni = vni |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 47 | self.src_port = src_port |
| 48 | self.dst_port = dst_port |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 49 | self.mcast_itf = mcast_itf |
| 50 | self.mcast_sw_if_index = mcast_sw_if_index |
| 51 | self.encap_vrf_id = encap_vrf_id |
| 52 | self.decap_next_index = decap_next_index |
| 53 | self.instance = instance |
| 54 | |
| 55 | if (self.mcast_itf): |
| 56 | self.mcast_sw_if_index = self.mcast_itf.sw_if_index |
| 57 | |
| 58 | def add_vpp_config(self): |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 59 | reply = self.test.vapi.vxlan_add_del_tunnel_v2( |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 60 | is_add=1, src_address=self.src, dst_address=self.dst, vni=self.vni, |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 61 | src_port=self.src_port, dst_port=self.dst_port, |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 62 | mcast_sw_if_index=self.mcast_sw_if_index, |
| 63 | encap_vrf_id=self.encap_vrf_id, |
| 64 | instance=self.instance, decap_next_index=self.decap_next_index) |
| 65 | self.set_sw_if_index(reply.sw_if_index) |
| 66 | self._test.registry.register(self, self._test.logger) |
| 67 | |
| 68 | def remove_vpp_config(self): |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 69 | self.test.vapi.vxlan_add_del_tunnel_v2( |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 70 | is_add=0, src_address=self.src, dst_address=self.dst, vni=self.vni, |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 71 | src_port=self.src_port, dst_port=self.dst_port, |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 72 | mcast_sw_if_index=self.mcast_sw_if_index, |
| 73 | encap_vrf_id=self.encap_vrf_id, instance=self.instance, |
| 74 | decap_next_index=self.decap_next_index) |
| 75 | |
| 76 | def query_vpp_config(self): |
| 77 | return (INDEX_INVALID != find_vxlan_tunnel(self._test, |
| 78 | self.src, |
| 79 | self.dst, |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 80 | self.src_port, |
| 81 | self.dst_port, |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 82 | self.vni)) |
| 83 | |
| 84 | def object_id(self): |
| 85 | return "vxlan-%d-%d-%s-%s" % (self.sw_if_index, self.vni, |
| 86 | self.src, self.dst) |