blob: f7cdecfa6dee28f2a6b2956d6052c3e066e3d462 [file] [log] [blame]
Renato Botelho do Coutoead1e532019-10-31 13:31:07 -05001#!/usr/bin/env python3
Stevene8fa6202017-08-30 14:36:45 -07002
3import unittest
4
Dave Wallace8800f732023-08-31 00:47:44 -04005from asfframework import VppAsfTestCase, VppTestRunner
Stevene8fa6202017-08-30 14:36:45 -07006
7from vpp_vhost_interface import VppVhostInterface
Dmitry Valter34fa0ce2024-03-11 10:38:46 +00008from config import config
Stevene8fa6202017-08-30 14:36:45 -07009
10
Dmitry Valter34fa0ce2024-03-11 10:38:46 +000011@unittest.skipIf("vhost" in config.excluded_plugins, "Exclude Vhost plugin tests")
Dave Wallace8800f732023-08-31 00:47:44 -040012class TesVhostInterface(VppAsfTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020013 """Vhost User Test Case"""
Stevene8fa6202017-08-30 14:36:45 -070014
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070015 @classmethod
16 def setUpClass(cls):
17 super(TesVhostInterface, cls).setUpClass()
18
19 @classmethod
20 def tearDownClass(cls):
21 super(TesVhostInterface, cls).tearDownClass()
Stevene8fa6202017-08-30 14:36:45 -070022
Juraj Slobodab3f90502018-10-04 15:15:16 +020023 def tearDown(self):
24 super(TesVhostInterface, self).tearDown()
25 if not self.vpp_dead:
26 if_dump = self.vapi.sw_interface_vhost_user_dump()
27 for ifc in if_dump:
28 self.vapi.delete_vhost_user_if(ifc.sw_if_index)
29
Stevene8fa6202017-08-30 14:36:45 -070030 def test_vhost(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020031 """Vhost User add/delete interface test"""
Stevene8fa6202017-08-30 14:36:45 -070032 self.logger.info("Vhost User add interfaces")
33
34 # create interface 1 (VirtualEthernet0/0/0)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020035 vhost_if1 = VppVhostInterface(self, sock_filename="/tmp/sock1")
Stevene8fa6202017-08-30 14:36:45 -070036 vhost_if1.add_vpp_config()
37 vhost_if1.admin_up()
38
39 # create interface 2 (VirtualEthernet0/0/1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020040 vhost_if2 = VppVhostInterface(self, sock_filename="/tmp/sock2")
Stevene8fa6202017-08-30 14:36:45 -070041 vhost_if2.add_vpp_config()
42 vhost_if2.admin_up()
43
44 # verify both interfaces in the show
45 ifs = self.vapi.cli("show interface")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020046 self.assertIn("VirtualEthernet0/0/0", ifs)
47 self.assertIn("VirtualEthernet0/0/1", ifs)
Stevene8fa6202017-08-30 14:36:45 -070048
49 # verify they are in the dump also
50 if_dump = self.vapi.sw_interface_vhost_user_dump()
51 self.assertTrue(vhost_if1.is_interface_config_in_dump(if_dump))
52 self.assertTrue(vhost_if2.is_interface_config_in_dump(if_dump))
53
54 # delete VirtualEthernet0/0/1
55 self.logger.info("Deleting VirtualEthernet0/0/1")
56 vhost_if2.remove_vpp_config()
57
58 self.logger.info("Verifying VirtualEthernet0/0/1 is deleted")
59
60 ifs = self.vapi.cli("show interface")
61 # verify VirtualEthernet0/0/0 still in the show
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020062 self.assertIn("VirtualEthernet0/0/0", ifs)
Stevene8fa6202017-08-30 14:36:45 -070063
64 # verify VirtualEthernet0/0/1 not in the show
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020065 self.assertNotIn("VirtualEthernet0/0/1", ifs)
Stevene8fa6202017-08-30 14:36:45 -070066
67 # verify VirtualEthernet0/0/1 is not in the dump
68 if_dump = self.vapi.sw_interface_vhost_user_dump()
69 self.assertFalse(vhost_if2.is_interface_config_in_dump(if_dump))
70
71 # verify VirtualEthernet0/0/0 is still in the dump
72 self.assertTrue(vhost_if1.is_interface_config_in_dump(if_dump))
73
74 # delete VirtualEthernet0/0/0
75 self.logger.info("Deleting VirtualEthernet0/0/0")
76 vhost_if1.remove_vpp_config()
77
78 self.logger.info("Verifying VirtualEthernet0/0/0 is deleted")
79
80 # verify VirtualEthernet0/0/0 not in the show
81 ifs = self.vapi.cli("show interface")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020082 self.assertNotIn("VirtualEthernet0/0/0", ifs)
Stevene8fa6202017-08-30 14:36:45 -070083
84 # verify VirtualEthernet0/0/0 is not in the dump
85 if_dump = self.vapi.sw_interface_vhost_user_dump()
86 self.assertFalse(vhost_if1.is_interface_config_in_dump(if_dump))
87
Juraj Slobodab3f90502018-10-04 15:15:16 +020088 def test_vhost_interface_state(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020089 """Vhost User interface states and events test"""
Juraj Slobodab3f90502018-10-04 15:15:16 +020090
91 self.vapi.want_interface_events()
92
93 # clear outstanding events
94 # (like delete interface events from other tests)
95 self.vapi.collect_events()
96
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020097 vhost_if = VppVhostInterface(self, sock_filename="/tmp/sock1")
Juraj Slobodab3f90502018-10-04 15:15:16 +020098
99 # create vhost interface
100 vhost_if.add_vpp_config()
101 self.sleep(0.1)
102 events = self.vapi.collect_events()
Ole Troan2e1c8962019-04-10 09:44:23 +0200103 # creating interface does now create events
104 self.assert_equal(len(events), 1, "number of events")
Juraj Slobodab3f90502018-10-04 15:15:16 +0200105
106 vhost_if.admin_up()
107 vhost_if.assert_interface_state(1, 0, expect_event=True)
108
109 vhost_if.admin_down()
110 vhost_if.assert_interface_state(0, 0, expect_event=True)
111
112 # delete vhost interface
113 vhost_if.remove_vpp_config()
114 event = self.vapi.wait_for_event(timeout=1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200115 self.assert_equal(event.sw_if_index, vhost_if.sw_if_index, "sw_if_index")
Juraj Slobodab3f90502018-10-04 15:15:16 +0200116 self.assert_equal(event.deleted, 1, "deleted flag")
117
118 # verify there are no more events
119 events = self.vapi.collect_events()
120 self.assert_equal(len(events), 0, "number of events")
121
Fahad Naeem08183d72022-04-04 10:31:04 -0400122 def test_vhost_interface_custom_mac_addr(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200123 """Vhost User interface custom mac address test"""
Fahad Naeem08183d72022-04-04 10:31:04 -0400124
125 mac_addr = "aa:bb:cc:dd:ee:ff"
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200126 vhost_if = VppVhostInterface(
127 self, sock_filename="/tmp/sock1", use_custom_mac=1, mac_address=mac_addr
128 )
Fahad Naeem08183d72022-04-04 10:31:04 -0400129
130 # create vhost interface
131 vhost_if.add_vpp_config()
132 self.sleep(0.1)
133
134 # verify mac in the dump
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200135 if_dump_list = self.vapi.sw_interface_dump(sw_if_index=vhost_if.sw_if_index)
Fahad Naeem08183d72022-04-04 10:31:04 -0400136 self.assert_equal(len(if_dump_list), 1, "if dump length")
137
138 [if_dump] = if_dump_list
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200139 self.assert_equal(if_dump.l2_address.mac_string, mac_addr, "MAC Address")
Fahad Naeem08183d72022-04-04 10:31:04 -0400140
141 # delete VirtualEthernet
142 self.logger.info("Deleting VirtualEthernet")
143 vhost_if.remove_vpp_config()
144
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200145
146if __name__ == "__main__":
Stevene8fa6202017-08-30 14:36:45 -0700147 unittest.main(testRunner=VppTestRunner)