Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 1 | import socket |
Neale Ranns | efd7bc2 | 2019-11-11 08:32:34 +0000 | [diff] [blame] | 2 | from ipaddress import IPv4Network |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 3 | |
| 4 | from vpp_object import VppObject |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 5 | from vpp_papi import VppEnum |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 6 | |
| 7 | |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 8 | def get_if_dump(dump, sw_if_index): |
| 9 | for d in dump: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 10 | if d.sw_if_index == sw_if_index: |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 11 | return d |
| 12 | |
| 13 | |
| 14 | def query_all_memif_vpp_config(_test): |
| 15 | return _test.vapi.memif_dump() |
| 16 | |
| 17 | |
| 18 | def remove_all_memif_vpp_config(_test): |
| 19 | dump = _test.vapi.memif_dump() |
| 20 | for d in dump: |
| 21 | _test.vapi.memif_delete(d.sw_if_index) |
| 22 | dump = _test.vapi.memif_socket_filename_dump() |
| 23 | for d in dump: |
| 24 | if d.socket_id != 0: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 25 | _test.vapi.memif_socket_filename_add_del(0, d.socket_id, d.socket_filename) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 26 | |
| 27 | |
| 28 | class VppSocketFilename(VppObject): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 29 | def __init__(self, test, socket_id, socket_filename, add_default_folder=False): |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 30 | self._test = test |
| 31 | self.socket_id = socket_id |
| 32 | self.socket_filename = socket_filename |
| 33 | |
| 34 | # if True insert default socket folder before socket filename, |
| 35 | # after adding vpp config |
| 36 | self.add_default_folder = add_default_folder |
| 37 | |
| 38 | def add_vpp_config(self): |
| 39 | rv = self._test.vapi.memif_socket_filename_add_del( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 40 | 1, self.socket_id, self.socket_filename |
| 41 | ) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 42 | if self.add_default_folder: |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 43 | self.socket_filename = "%s/%s" % (self._test.tempdir, self.socket_filename) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 44 | return rv |
| 45 | |
| 46 | def remove_vpp_config(self): |
| 47 | return self._test.vapi.memif_socket_filename_add_del( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 48 | 0, self.socket_id, self.socket_filename |
| 49 | ) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 50 | |
| 51 | def query_vpp_config(self): |
| 52 | return self._test.vapi.memif_socket_filename_dump() |
| 53 | |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 54 | def object_id(self): |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 55 | return "socket-filename-%d-%s" % (self.socket_id, self.socket_filename) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 56 | |
| 57 | |
| 58 | class VppMemif(VppObject): |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 59 | def __init__( |
| 60 | self, |
| 61 | test, |
| 62 | role, |
| 63 | mode, |
| 64 | rx_queues=0, |
| 65 | tx_queues=0, |
| 66 | if_id=0, |
| 67 | socket_id=0, |
| 68 | secret="", |
| 69 | ring_size=0, |
| 70 | buffer_size=0, |
| 71 | hw_addr="", |
| 72 | ): |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 73 | self._test = test |
| 74 | self.role = role |
| 75 | self.mode = mode |
| 76 | self.rx_queues = rx_queues |
| 77 | self.tx_queues = tx_queues |
| 78 | self.if_id = if_id |
| 79 | self.socket_id = socket_id |
| 80 | self.secret = secret |
| 81 | self.ring_size = ring_size |
| 82 | self.buffer_size = buffer_size |
| 83 | self.hw_addr = hw_addr |
| 84 | self.sw_if_index = None |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 85 | self.ip_prefix = IPv4Network( |
| 86 | "192.168.%d.%d/24" % (self.if_id + 1, self.role + 1), strict=False |
| 87 | ) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 88 | |
| 89 | def add_vpp_config(self): |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 90 | rv = self._test.vapi.memif_create( |
| 91 | role=self.role, |
| 92 | mode=self.mode, |
| 93 | rx_queues=self.rx_queues, |
| 94 | tx_queues=self.tx_queues, |
| 95 | id=self.if_id, |
| 96 | socket_id=self.socket_id, |
| 97 | secret=self.secret, |
| 98 | ring_size=self.ring_size, |
| 99 | buffer_size=self.buffer_size, |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 100 | hw_addr=self.hw_addr, |
| 101 | ) |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 102 | try: |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 103 | self.sw_if_index = rv.sw_if_index |
| 104 | except AttributeError: |
Paul Vinciguerra | a25a857 | 2019-12-06 21:40:38 -0500 | [diff] [blame] | 105 | # rv doesn't have .sw_if_index attribute |
| 106 | raise AttributeError("%s %s" % (self, rv)) |
Neale Ranns | 097fa66 | 2018-05-01 05:17:55 -0700 | [diff] [blame] | 107 | |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 108 | return self.sw_if_index |
| 109 | |
| 110 | def admin_up(self): |
| 111 | if self.sw_if_index: |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 112 | return self._test.vapi.sw_interface_set_flags( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 113 | sw_if_index=self.sw_if_index, flags=1 |
| 114 | ) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 115 | |
| 116 | def admin_down(self): |
| 117 | if self.sw_if_index: |
Jakub Grajciar | 053204a | 2019-03-18 13:17:53 +0100 | [diff] [blame] | 118 | return self._test.vapi.sw_interface_set_flags( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 119 | sw_if_index=self.sw_if_index, flags=0 |
| 120 | ) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 121 | |
| 122 | def wait_for_link_up(self, timeout, step=1): |
| 123 | if not self.sw_if_index: |
| 124 | return False |
| 125 | while True: |
| 126 | dump = self.query_vpp_config() |
Jakub Grajciar | 546f955 | 2019-08-21 10:51:21 +0200 | [diff] [blame] | 127 | f = VppEnum.vl_api_if_status_flags_t.IF_STATUS_API_FLAG_LINK_UP |
| 128 | if dump.flags & f: |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 129 | return True |
| 130 | self._test.sleep(step) |
| 131 | timeout -= step |
| 132 | if timeout <= 0: |
| 133 | return False |
| 134 | |
| 135 | def config_ip4(self): |
| 136 | return self._test.vapi.sw_interface_add_del_address( |
Klement Sekera | d9b0c6f | 2022-04-26 19:02:15 +0200 | [diff] [blame] | 137 | sw_if_index=self.sw_if_index, prefix=self.ip_prefix |
| 138 | ) |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 139 | |
| 140 | def remove_vpp_config(self): |
| 141 | self._test.vapi.memif_delete(self.sw_if_index) |
| 142 | self.sw_if_index = None |
| 143 | |
| 144 | def query_vpp_config(self): |
| 145 | if not self.sw_if_index: |
| 146 | return None |
| 147 | dump = self._test.vapi.memif_dump() |
| 148 | return get_if_dump(dump, self.sw_if_index) |
| 149 | |
Jakub Grajciar | b1be2a0 | 2018-09-19 13:36:16 +0200 | [diff] [blame] | 150 | def object_id(self): |
| 151 | if self.sw_if_index: |
| 152 | return "%d:%d:%d" % (self.role, self.if_id, self.sw_if_index) |
| 153 | else: |
| 154 | return "%d:%d:None" % (self.role, self.if_id) |