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