blob: 6a97c2b988410545f4bc22dba8d854c1b1dab236 [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:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020010 if d.sw_if_index == sw_if_index:
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020011 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:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020025 _test.vapi.memif_socket_filename_add_del(0, d.socket_id, d.socket_filename)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020026
27
28class VppSocketFilename(VppObject):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020029 def __init__(self, test, socket_id, socket_filename, add_default_folder=False):
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020030 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 Sekerad9b0c6f2022-04-26 19:02:15 +020040 1, self.socket_id, self.socket_filename
41 )
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020042 if self.add_default_folder:
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020043 self.socket_filename = "%s/%s" % (self._test.tempdir, self.socket_filename)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020044 return rv
45
46 def remove_vpp_config(self):
47 return self._test.vapi.memif_socket_filename_add_del(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020048 0, self.socket_id, self.socket_filename
49 )
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020050
51 def query_vpp_config(self):
52 return self._test.vapi.memif_socket_filename_dump()
53
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020054 def object_id(self):
Neale Ranns097fa662018-05-01 05:17:55 -070055 return "socket-filename-%d-%s" % (self.socket_id, self.socket_filename)
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020056
57
58class VppMemif(VppObject):
Klement Sekerad9b0c6f2022-04-26 19:02:15 +020059 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 Grajciarb1be2a02018-09-19 13:36:16 +020073 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 Sekerad9b0c6f2022-04-26 19:02:15 +020085 self.ip_prefix = IPv4Network(
86 "192.168.%d.%d/24" % (self.if_id + 1, self.role + 1), strict=False
87 )
Jakub Grajciarb1be2a02018-09-19 13:36:16 +020088
89 def add_vpp_config(self):
Neale Ranns097fa662018-05-01 05:17:55 -070090 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 Sekerad9b0c6f2022-04-26 19:02:15 +0200100 hw_addr=self.hw_addr,
101 )
Neale Ranns097fa662018-05-01 05:17:55 -0700102 try:
Neale Ranns097fa662018-05-01 05:17:55 -0700103 self.sw_if_index = rv.sw_if_index
104 except AttributeError:
Paul Vinciguerraa25a8572019-12-06 21:40:38 -0500105 # rv doesn't have .sw_if_index attribute
106 raise AttributeError("%s %s" % (self, rv))
Neale Ranns097fa662018-05-01 05:17:55 -0700107
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200108 return self.sw_if_index
109
110 def admin_up(self):
111 if self.sw_if_index:
Jakub Grajciar053204a2019-03-18 13:17:53 +0100112 return self._test.vapi.sw_interface_set_flags(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200113 sw_if_index=self.sw_if_index, flags=1
114 )
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200115
116 def admin_down(self):
117 if self.sw_if_index:
Jakub Grajciar053204a2019-03-18 13:17:53 +0100118 return self._test.vapi.sw_interface_set_flags(
Klement Sekerad9b0c6f2022-04-26 19:02:15 +0200119 sw_if_index=self.sw_if_index, flags=0
120 )
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200121
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 Grajciar546f9552019-08-21 10:51:21 +0200127 f = VppEnum.vl_api_if_status_flags_t.IF_STATUS_API_FLAG_LINK_UP
128 if dump.flags & f:
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200129 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 Sekerad9b0c6f2022-04-26 19:02:15 +0200137 sw_if_index=self.sw_if_index, prefix=self.ip_prefix
138 )
Jakub Grajciarb1be2a02018-09-19 13:36:16 +0200139
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 Grajciarb1be2a02018-09-19 13:36:16 +0200150 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)