blob: 226f8af72b5d9ecc9f73f17fb9f2613d9392cb4c [file] [log] [blame]
Jakub Grajciarb1be2a02018-09-19 13:36:16 +02001import socket
Neale Rannsefd7bc22019-11-11 08:32:34 +00002from ipaddress import IPv4Network
Jakub Grajciarb1be2a02018-09-19 13:36:16 +02003
4from vpp_object import VppObject
Jakub Grajciar053204a2019-03-18 13:17:53 +01005from vpp_papi import VppEnum
Jakub Grajciarb1be2a02018-09-19 13:36:16 +02006
7
Jakub Grajciarb1be2a02018-09-19 13:36:16 +02008def get_if_dump(dump, sw_if_index):
9 for d in dump:
10 if (d.sw_if_index == sw_if_index):
11 return d
12
13
14def query_all_memif_vpp_config(_test):
15 return _test.vapi.memif_dump()
16
17
18def 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:
25 _test.vapi.memif_socket_filename_add_del(
26 0, d.socket_id, d.socket_filename)
27
28
29class VppSocketFilename(VppObject):
30 def __init__(self, test, socket_id, socket_filename,
31 add_default_folder=False):
32 self._test = test
33 self.socket_id = socket_id
34 self.socket_filename = socket_filename
35
36 # if True insert default socket folder before socket filename,
37 # after adding vpp config
38 self.add_default_folder = add_default_folder
39
40 def add_vpp_config(self):
41 rv = self._test.vapi.memif_socket_filename_add_del(
42 1, self.socket_id, self.socket_filename)
43 if self.add_default_folder:
Ole Troane5b83aa2019-10-18 03:39:26 +020044 self.socket_filename = "%s/%s" % (self._test.tempdir,
45 self.socket_filename)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020046 return rv
47
48 def remove_vpp_config(self):
49 return self._test.vapi.memif_socket_filename_add_del(
50 0, self.socket_id, self.socket_filename)
51
52 def query_vpp_config(self):
53 return self._test.vapi.memif_socket_filename_dump()
54
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020055 def object_id(self):
Neale Ranns097fa662018-05-01 05:17:55 -070056 return "socket-filename-%d-%s" % (self.socket_id, self.socket_filename)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020057
58
59class VppMemif(VppObject):
60 def __init__(self, test, role, mode, rx_queues=0, tx_queues=0, if_id=0,
61 socket_id=0, secret="", ring_size=0, buffer_size=0,
62 hw_addr=""):
63 self._test = test
64 self.role = role
65 self.mode = mode
66 self.rx_queues = rx_queues
67 self.tx_queues = tx_queues
68 self.if_id = if_id
69 self.socket_id = socket_id
70 self.secret = secret
71 self.ring_size = ring_size
72 self.buffer_size = buffer_size
73 self.hw_addr = hw_addr
74 self.sw_if_index = None
Neale Rannsefd7bc22019-11-11 08:32:34 +000075 self.ip_prefix = IPv4Network("192.168.%d.%d/24" %
76 (self.if_id + 1, self.role + 1),
77 strict=False)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020078
79 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -070080 rv = self._test.vapi.memif_create(
81 role=self.role,
82 mode=self.mode,
83 rx_queues=self.rx_queues,
84 tx_queues=self.tx_queues,
85 id=self.if_id,
86 socket_id=self.socket_id,
87 secret=self.secret,
88 ring_size=self.ring_size,
89 buffer_size=self.buffer_size,
90 hw_addr=self.hw_addr)
91 try:
Neale Ranns097fa662018-05-01 05:17:55 -070092 self.sw_if_index = rv.sw_if_index
93 except AttributeError:
Paul Vinciguerraa25a8572019-12-06 21:40:38 -050094 # rv doesn't have .sw_if_index attribute
95 raise AttributeError("%s %s" % (self, rv))
Neale Ranns097fa662018-05-01 05:17:55 -070096
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020097 return self.sw_if_index
98
99 def admin_up(self):
100 if self.sw_if_index:
Jakub Grajciar053204a2019-03-18 13:17:53 +0100101 return self._test.vapi.sw_interface_set_flags(
102 sw_if_index=self.sw_if_index, flags=1)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200103
104 def admin_down(self):
105 if self.sw_if_index:
Jakub Grajciar053204a2019-03-18 13:17:53 +0100106 return self._test.vapi.sw_interface_set_flags(
107 sw_if_index=self.sw_if_index, flags=0)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200108
109 def wait_for_link_up(self, timeout, step=1):
110 if not self.sw_if_index:
111 return False
112 while True:
113 dump = self.query_vpp_config()
Jakub Grajciar546f9552019-08-21 10:51:21 +0200114 f = VppEnum.vl_api_if_status_flags_t.IF_STATUS_API_FLAG_LINK_UP
115 if dump.flags & f:
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200116 return True
117 self._test.sleep(step)
118 timeout -= step
119 if timeout <= 0:
120 return False
121
122 def config_ip4(self):
123 return self._test.vapi.sw_interface_add_del_address(
Neale Rannsefd7bc22019-11-11 08:32:34 +0000124 sw_if_index=self.sw_if_index, prefix=self.ip_prefix)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200125
126 def remove_vpp_config(self):
127 self._test.vapi.memif_delete(self.sw_if_index)
128 self.sw_if_index = None
129
130 def query_vpp_config(self):
131 if not self.sw_if_index:
132 return None
133 dump = self._test.vapi.memif_dump()
134 return get_if_dump(dump, self.sw_if_index)
135
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200136 def object_id(self):
137 if self.sw_if_index:
138 return "%d:%d:%d" % (self.role, self.if_id, self.sw_if_index)
139 else:
140 return "%d:%d:None" % (self.role, self.if_id)