diff options
author | Jakub Grajciar <jgrajcia@cisco.com> | 2019-08-26 11:25:52 +0200 |
---|---|---|
committer | Damjan Marion <dmarion@me.com> | 2019-09-12 20:25:10 +0000 |
commit | 3b2db9002c14f9e0742622f2d503c5801d443827 (patch) | |
tree | 298fabfa77400f93ae0d214c47b90da9ee912d60 | |
parent | 3c7c613cf18f7825e22190be45ea7f054efafb77 (diff) |
devices: af_packet API cleanup
Use consistent API types.
- fix af_packet_dump dumping deleted interface
Type: fix
Signed-off-by: Jakub Grajciar <jgrajcia@cisco.com>
Change-Id: Ie8d138e30c8c51a2306bb2ad9ac0b7a49d5412bf
Signed-off-by: Jakub Grajciar <jgrajcia@cisco.com>
-rw-r--r-- | src/vnet/devices/af_packet/af_packet.api | 27 | ||||
-rw-r--r-- | src/vnet/devices/af_packet/af_packet.c | 24 | ||||
-rw-r--r-- | src/vnet/devices/af_packet/af_packet_api.c | 2 | ||||
-rw-r--r-- | test/test_af_packet.py | 39 | ||||
-rw-r--r-- | test/vpp_devices.py | 41 |
5 files changed, 109 insertions, 24 deletions
diff --git a/src/vnet/devices/af_packet/af_packet.api b/src/vnet/devices/af_packet/af_packet.api index 1399011e870..a7a25e4249e 100644 --- a/src/vnet/devices/af_packet/af_packet.api +++ b/src/vnet/devices/af_packet/af_packet.api @@ -13,23 +13,26 @@ * limitations under the License. */ -option version = "1.0.0"; +option version = "2.0.0"; + +import "vnet/interface_types.api"; +import "vnet/ethernet/ethernet_types.api"; /** \brief Create host-interface @param client_index - opaque cookie to identify the sender @param context - sender context, to match reply w/ request - @param host_if_name - interface name @param hw_addr - interface MAC @param use_random_hw_addr - use random generated MAC + @param host_if_name - interface name */ define af_packet_create { u32 client_index; u32 context; - u8 host_if_name[64]; - u8 hw_addr[6]; - u8 use_random_hw_addr; + vl_api_mac_address_t hw_addr; + bool use_random_hw_addr; + string host_if_name[64]; }; /** \brief Create host-interface response @@ -40,7 +43,7 @@ define af_packet_create_reply { u32 context; i32 retval; - u32 sw_if_index; + vl_api_interface_index_t sw_if_index; }; /** \brief Delete host-interface @@ -53,7 +56,7 @@ autoreply define af_packet_delete u32 client_index; u32 context; - u8 host_if_name[64]; + string host_if_name[64]; }; /** \brief Set l4 offload ckecksum calculation @@ -64,9 +67,9 @@ autoreply define af_packet_set_l4_cksum_offload { u32 client_index; u32 context; - - u8 sw_if_index; - u8 set; + + vl_api_interface_index_t sw_if_index; + bool set; }; /** \brief Dump af_packet interfaces request */ @@ -83,8 +86,8 @@ define af_packet_dump define af_packet_details { u32 context; - u32 sw_if_index; - u8 host_if_name[64]; + vl_api_interface_index_t sw_if_index; + string host_if_name[64]; }; /* diff --git a/src/vnet/devices/af_packet/af_packet.c b/src/vnet/devices/af_packet/af_packet.c index 871b7bfa427..22ddf4ecd11 100644 --- a/src/vnet/devices/af_packet/af_packet.c +++ b/src/vnet/devices/af_packet/af_packet.c @@ -505,17 +505,19 @@ af_packet_dump_ifs (af_packet_if_detail_t ** out_af_packet_ifs) af_packet_if_detail_t *r_af_packet_ifs = NULL; af_packet_if_detail_t *af_packet_if = NULL; - vec_foreach (apif, apm->interfaces) - { - vec_add2 (r_af_packet_ifs, af_packet_if, 1); - af_packet_if->sw_if_index = apif->sw_if_index; - if (apif->host_if_name) - { - clib_memcpy (af_packet_if->host_if_name, apif->host_if_name, - MIN (ARRAY_LEN (af_packet_if->host_if_name) - 1, - strlen ((const char *) apif->host_if_name))); - } - } + /* *INDENT-OFF* */ + pool_foreach (apif, apm->interfaces, + ({ + vec_add2 (r_af_packet_ifs, af_packet_if, 1); + af_packet_if->sw_if_index = apif->sw_if_index; + if (apif->host_if_name) + { + clib_memcpy (af_packet_if->host_if_name, apif->host_if_name, + MIN (ARRAY_LEN (af_packet_if->host_if_name) - 1, + strlen ((const char *) apif->host_if_name))); + } + })); + /* *INDENT-ON* */ *out_af_packet_ifs = r_af_packet_ifs; diff --git a/src/vnet/devices/af_packet/af_packet_api.c b/src/vnet/devices/af_packet/af_packet_api.c index 48f0588b3a1..dd9697a8a6b 100644 --- a/src/vnet/devices/af_packet/af_packet_api.c +++ b/src/vnet/devices/af_packet/af_packet_api.c @@ -100,7 +100,7 @@ static void vl_api_af_packet_delete_reply_t *rmp; int rv = 0; - rv = af_packet_set_l4_cksum_offload (vm, mp->sw_if_index, mp->set); + rv = af_packet_set_l4_cksum_offload (vm, ntohl (mp->sw_if_index), mp->set); REPLY_MACRO (VL_API_AF_PACKET_SET_L4_CKSUM_OFFLOAD_REPLY); } diff --git a/test/test_af_packet.py b/test/test_af_packet.py new file mode 100644 index 00000000000..9017fb71cbf --- /dev/null +++ b/test/test_af_packet.py @@ -0,0 +1,39 @@ +import unittest +import os +import psutil + +from framework import VppTestCase, VppTestRunner +from vpp_devices import VppAFPacketInterface + + +class TestAFPacket(VppTestCase): + """ Host interface Test Case """ + + host_if_name = 'afp0' + + @classmethod + def setUpClass(cls): + super(TestAFPacket, cls).setUpClass() + os.system('ip tuntap add dev ' + cls.host_if_name + ' mode tap') + + @classmethod + def tearDownClass(cls): + super(TestAFPacket, cls).tearDownClass() + os.system('ip link delete ' + cls.host_if_name) + + def test_tap_add_del(self): + """Create host interface""" + # check if host interface exists + self.assertTrue( + psutil.net_if_addrs().get( + self.host_if_name), + 'Host interface ' + + self.host_if_name + + ' does not exist') + afp0 = VppAFPacketInterface(self, self.host_if_name) + afp0.add_vpp_config() + self.assertTrue(afp0.query_vpp_config()) + + +if __name__ == '__main__': + unittest.main(testRunner=VppTestRunner) diff --git a/test/vpp_devices.py b/test/vpp_devices.py new file mode 100644 index 00000000000..2fd0057ed91 --- /dev/null +++ b/test/vpp_devices.py @@ -0,0 +1,41 @@ +from vpp_interface import VppInterface + + +class VppAFPacketInterface(VppInterface): + + @property + def host_if_name(self): + """Host interface name""" + return self._host_if_name + + def __init__(self, test, host_if_name, mac_addr=None): + self._test = test + self._host_if_name = host_if_name + self._mac_addr = mac_addr + + def get_vpp_dump(self): + dump = self._test.vapi.af_packet_dump() + for entry in dump: + if entry.sw_if_index == self.sw_if_index: + return entry + + def add_vpp_config(self): + use_random_mac = True if self._mac_addr else False + reply = self._test.vapi.af_packet_create( + host_if_name=self._host_if_name, + use_random_hw_addr=use_random_mac, + hw_addr=self._mac_addr) + self.set_sw_if_index(reply.sw_if_index) + self._test.registry.register(self, self.test.logger) + + def remove_vpp_config(self): + self._test.vapi.af_packet_delete(host_if_name=self._host_if_name) + + def query_vpp_config(self): + dump = self.get_vpp_dump() + if dump: + return True + return False + + def object_id(self): + return "af_packet-%s" % self._host_if_name |