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 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 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: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 22 | if ( |
| 23 | src == str(t.src_address) |
| 24 | and dst == str(t.dst_address) |
| 25 | and src_port == t.src_port |
| 26 | and dst_port == t.dst_port |
| 27 | and t.vni == vni |
| 28 | ): |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 29 | return t.sw_if_index |
| 30 | return INDEX_INVALID |
| 31 | |
| 32 | |
| 33 | class VppVxlanTunnel(VppInterface): |
| 34 | """ |
| 35 | VPP VXLAN interface |
| 36 | """ |
| 37 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 38 | def __init__( |
| 39 | self, |
| 40 | test, |
| 41 | src, |
| 42 | dst, |
| 43 | vni, |
| 44 | src_port=UNDEFINED_PORT, |
| 45 | dst_port=UNDEFINED_PORT, |
| 46 | mcast_itf=None, |
| 47 | mcast_sw_if_index=INDEX_INVALID, |
| 48 | decap_next_index=INDEX_INVALID, |
| 49 | encap_vrf_id=None, |
| 50 | instance=0xFFFFFFFF, |
| 51 | is_l3=False, |
| 52 | ): |
| 53 | """Create VXLAN Tunnel interface""" |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 54 | super(VppVxlanTunnel, self).__init__(test) |
| 55 | self.src = src |
| 56 | self.dst = dst |
| 57 | self.vni = vni |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 58 | self.src_port = src_port |
| 59 | self.dst_port = dst_port |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 60 | self.mcast_itf = mcast_itf |
| 61 | self.mcast_sw_if_index = mcast_sw_if_index |
| 62 | self.encap_vrf_id = encap_vrf_id |
| 63 | self.decap_next_index = decap_next_index |
| 64 | self.instance = instance |
Nathan Skrzypczak | 3e38422 | 2021-04-21 19:56:34 +0200 | [diff] [blame] | 65 | self.is_l3 = is_l3 |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 66 | |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 67 | if self.mcast_itf: |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 68 | self.mcast_sw_if_index = self.mcast_itf.sw_if_index |
| 69 | |
| 70 | def add_vpp_config(self): |
Nathan Skrzypczak | 3e38422 | 2021-04-21 19:56:34 +0200 | [diff] [blame] | 71 | reply = self.test.vapi.vxlan_add_del_tunnel_v3( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 72 | is_add=1, |
| 73 | src_address=self.src, |
| 74 | dst_address=self.dst, |
| 75 | vni=self.vni, |
| 76 | src_port=self.src_port, |
| 77 | dst_port=self.dst_port, |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 78 | mcast_sw_if_index=self.mcast_sw_if_index, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 79 | encap_vrf_id=self.encap_vrf_id, |
| 80 | is_l3=self.is_l3, |
| 81 | instance=self.instance, |
| 82 | decap_next_index=self.decap_next_index, |
| 83 | ) |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 84 | self.set_sw_if_index(reply.sw_if_index) |
| 85 | self._test.registry.register(self, self._test.logger) |
| 86 | |
| 87 | def remove_vpp_config(self): |
Artem Glazychev | 839dcc0 | 2020-12-01 02:39:21 +0700 | [diff] [blame] | 88 | self.test.vapi.vxlan_add_del_tunnel_v2( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 89 | is_add=0, |
| 90 | src_address=self.src, |
| 91 | dst_address=self.dst, |
| 92 | vni=self.vni, |
| 93 | src_port=self.src_port, |
| 94 | dst_port=self.dst_port, |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 95 | mcast_sw_if_index=self.mcast_sw_if_index, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 96 | encap_vrf_id=self.encap_vrf_id, |
| 97 | instance=self.instance, |
| 98 | decap_next_index=self.decap_next_index, |
| 99 | ) |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 100 | |
| 101 | def query_vpp_config(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 102 | return INDEX_INVALID != find_vxlan_tunnel( |
| 103 | self._test, self.src, self.dst, self.src_port, self.dst_port, self.vni |
| 104 | ) |
Jakub Grajciar | 7c0eb56 | 2020-03-02 13:55:31 +0100 | [diff] [blame] | 105 | |
| 106 | def object_id(self): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 107 | return "vxlan-%d-%d-%s-%s" % (self.sw_if_index, self.vni, self.src, self.dst) |