blob: eb584633d5a0782ea894aaa0f5f8bfa1f73c5160 [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
Pratikshya Prasai657bdf72022-08-18 11:09:38 -04005from asfframework import VppTestCase, VppTestRunner
Stevene8fa6202017-08-30 14:36:45 -07006
7from vpp_vhost_interface import VppVhostInterface
8
9
10class TesVhostInterface(VppTestCase):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020011 """Vhost User Test Case"""
Stevene8fa6202017-08-30 14:36:45 -070012
Paul Vinciguerra7f9b7f92019-03-12 19:23:27 -070013 @classmethod
14 def setUpClass(cls):
15 super(TesVhostInterface, cls).setUpClass()
16
17 @classmethod
18 def tearDownClass(cls):
19 super(TesVhostInterface, cls).tearDownClass()
Stevene8fa6202017-08-30 14:36:45 -070020
Juraj Slobodab3f90502018-10-04 15:15:16 +020021 def tearDown(self):
22 super(TesVhostInterface, self).tearDown()
23 if not self.vpp_dead:
24 if_dump = self.vapi.sw_interface_vhost_user_dump()
25 for ifc in if_dump:
26 self.vapi.delete_vhost_user_if(ifc.sw_if_index)
27
Stevene8fa6202017-08-30 14:36:45 -070028 def test_vhost(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020029 """Vhost User add/delete interface test"""
Stevene8fa6202017-08-30 14:36:45 -070030 self.logger.info("Vhost User add interfaces")
31
32 # create interface 1 (VirtualEthernet0/0/0)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020033 vhost_if1 = VppVhostInterface(self, sock_filename="/tmp/sock1")
Stevene8fa6202017-08-30 14:36:45 -070034 vhost_if1.add_vpp_config()
35 vhost_if1.admin_up()
36
37 # create interface 2 (VirtualEthernet0/0/1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020038 vhost_if2 = VppVhostInterface(self, sock_filename="/tmp/sock2")
Stevene8fa6202017-08-30 14:36:45 -070039 vhost_if2.add_vpp_config()
40 vhost_if2.admin_up()
41
42 # verify both interfaces in the show
43 ifs = self.vapi.cli("show interface")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020044 self.assertIn("VirtualEthernet0/0/0", ifs)
45 self.assertIn("VirtualEthernet0/0/1", ifs)
Stevene8fa6202017-08-30 14:36:45 -070046
47 # verify they are in the dump also
48 if_dump = self.vapi.sw_interface_vhost_user_dump()
49 self.assertTrue(vhost_if1.is_interface_config_in_dump(if_dump))
50 self.assertTrue(vhost_if2.is_interface_config_in_dump(if_dump))
51
52 # delete VirtualEthernet0/0/1
53 self.logger.info("Deleting VirtualEthernet0/0/1")
54 vhost_if2.remove_vpp_config()
55
56 self.logger.info("Verifying VirtualEthernet0/0/1 is deleted")
57
58 ifs = self.vapi.cli("show interface")
59 # verify VirtualEthernet0/0/0 still in the show
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020060 self.assertIn("VirtualEthernet0/0/0", ifs)
Stevene8fa6202017-08-30 14:36:45 -070061
62 # verify VirtualEthernet0/0/1 not in the show
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020063 self.assertNotIn("VirtualEthernet0/0/1", ifs)
Stevene8fa6202017-08-30 14:36:45 -070064
65 # verify VirtualEthernet0/0/1 is not in the dump
66 if_dump = self.vapi.sw_interface_vhost_user_dump()
67 self.assertFalse(vhost_if2.is_interface_config_in_dump(if_dump))
68
69 # verify VirtualEthernet0/0/0 is still in the dump
70 self.assertTrue(vhost_if1.is_interface_config_in_dump(if_dump))
71
72 # delete VirtualEthernet0/0/0
73 self.logger.info("Deleting VirtualEthernet0/0/0")
74 vhost_if1.remove_vpp_config()
75
76 self.logger.info("Verifying VirtualEthernet0/0/0 is deleted")
77
78 # verify VirtualEthernet0/0/0 not in the show
79 ifs = self.vapi.cli("show interface")
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020080 self.assertNotIn("VirtualEthernet0/0/0", ifs)
Stevene8fa6202017-08-30 14:36:45 -070081
82 # verify VirtualEthernet0/0/0 is not in the dump
83 if_dump = self.vapi.sw_interface_vhost_user_dump()
84 self.assertFalse(vhost_if1.is_interface_config_in_dump(if_dump))
85
Juraj Slobodab3f90502018-10-04 15:15:16 +020086 def test_vhost_interface_state(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020087 """Vhost User interface states and events test"""
Juraj Slobodab3f90502018-10-04 15:15:16 +020088
89 self.vapi.want_interface_events()
90
91 # clear outstanding events
92 # (like delete interface events from other tests)
93 self.vapi.collect_events()
94
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020095 vhost_if = VppVhostInterface(self, sock_filename="/tmp/sock1")
Juraj Slobodab3f90502018-10-04 15:15:16 +020096
97 # create vhost interface
98 vhost_if.add_vpp_config()
99 self.sleep(0.1)
100 events = self.vapi.collect_events()
Ole Troan2e1c8962019-04-10 09:44:23 +0200101 # creating interface does now create events
102 self.assert_equal(len(events), 1, "number of events")
Juraj Slobodab3f90502018-10-04 15:15:16 +0200103
104 vhost_if.admin_up()
105 vhost_if.assert_interface_state(1, 0, expect_event=True)
106
107 vhost_if.admin_down()
108 vhost_if.assert_interface_state(0, 0, expect_event=True)
109
110 # delete vhost interface
111 vhost_if.remove_vpp_config()
112 event = self.vapi.wait_for_event(timeout=1)
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200113 self.assert_equal(event.sw_if_index, vhost_if.sw_if_index, "sw_if_index")
Juraj Slobodab3f90502018-10-04 15:15:16 +0200114 self.assert_equal(event.deleted, 1, "deleted flag")
115
116 # verify there are no more events
117 events = self.vapi.collect_events()
118 self.assert_equal(len(events), 0, "number of events")
119
Fahad Naeem08183d72022-04-04 10:31:04 -0400120 def test_vhost_interface_custom_mac_addr(self):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200121 """Vhost User interface custom mac address test"""
Fahad Naeem08183d72022-04-04 10:31:04 -0400122
123 mac_addr = "aa:bb:cc:dd:ee:ff"
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200124 vhost_if = VppVhostInterface(
125 self, sock_filename="/tmp/sock1", use_custom_mac=1, mac_address=mac_addr
126 )
Fahad Naeem08183d72022-04-04 10:31:04 -0400127
128 # create vhost interface
129 vhost_if.add_vpp_config()
130 self.sleep(0.1)
131
132 # verify mac in the dump
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200133 if_dump_list = self.vapi.sw_interface_dump(sw_if_index=vhost_if.sw_if_index)
Fahad Naeem08183d72022-04-04 10:31:04 -0400134 self.assert_equal(len(if_dump_list), 1, "if dump length")
135
136 [if_dump] = if_dump_list
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200137 self.assert_equal(if_dump.l2_address.mac_string, mac_addr, "MAC Address")
Fahad Naeem08183d72022-04-04 10:31:04 -0400138
139 # delete VirtualEthernet
140 self.logger.info("Deleting VirtualEthernet")
141 vhost_if.remove_vpp_config()
142
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200143
144if __name__ == "__main__":
Stevene8fa6202017-08-30 14:36:45 -0700145 unittest.main(testRunner=VppTestRunner)