17 # @author: Girish Moodalbail, Oracle, Inc. |
17 # @author: Girish Moodalbail, Oracle, Inc. |
18 # |
18 # |
19 |
19 |
20 import netaddr |
20 import netaddr |
21 |
21 |
22 from quantum.agent.linux import utils |
22 from neutron.agent.linux import utils |
23 |
23 |
24 |
24 |
25 class CommandBase(object): |
25 class CommandBase(object): |
26 @classmethod |
26 @classmethod |
27 def execute_with_pfexec(cls, cmd, **kwargs): |
27 def execute_with_pfexec(cls, cmd, **kwargs): |
58 stdout = cls.execute(cmd) |
58 stdout = cls.execute(cmd) |
59 |
59 |
60 return ipaddr in stdout |
60 return ipaddr in stdout |
61 |
61 |
62 def ipaddr_list(self, filters=None): |
62 def ipaddr_list(self, filters=None): |
63 cmd = ['/usr/sbin/ipadm', 'show-addr', '-po', 'type,addr,', |
63 cmd = ['/usr/sbin/ipadm', 'show-addr', '-po', 'type,addr', |
64 self._ifname] |
64 self._ifname] |
65 stdout = self.execute(cmd) |
65 stdout = self.execute(cmd) |
66 atype_addrs = stdout.strip().split('\n') |
66 atype_addrs = stdout.strip().split('\n') |
67 result = {} |
67 result = {} |
68 for atype_addr in atype_addrs: |
68 for atype_addr in atype_addrs: |
69 atype, addr = atype_addr.split(':') |
69 atype, addr = atype_addr.split(':', 1) |
70 val = result.get(atype) |
70 val = result.get(atype) |
71 if val is None: |
71 if val is None: |
72 result[atype] = [] |
72 result[atype] = [] |
73 val = result.get(atype) |
73 val = result.get(atype) |
74 val.append(addr) |
74 # in the case of IPv6 addresses remove any escape '\' character |
|
75 val.append(addr.replace("\\", "")) |
75 return result |
76 return result |
76 |
77 |
77 def create_address(self, ipaddr, addrobjname=None, temp=True): |
78 def create_address(self, ipaddr, addrobjname=None, temp=True): |
78 if not self.ifname_exists(self._ifname): |
79 if not self.ifname_exists(self._ifname): |
79 # create ip interface |
80 # create ip interface |
165 cmd.append('-T') |
166 cmd.append('-T') |
166 cmd.append(tenantname) |
167 cmd.append(tenantname) |
167 |
168 |
168 return self.execute_with_pfexec(cmd) |
169 return self.execute_with_pfexec(cmd) |
169 |
170 |
170 def create_vnic(self, lower_link, mac_address=None, temp=True): |
171 def create_vnic(self, lower_link, mac_address=None, vid=None, temp=True): |
171 if self.datalink_exists(self._dlname): |
172 if self.datalink_exists(self._dlname): |
172 return |
173 return |
173 |
174 |
|
175 if vid: |
|
176 # If the default_tag of lower_link is same as vid, then there |
|
177 # is no need to set vid |
|
178 cmd = ['/usr/sbin/dladm', 'show-linkprop', '-co', 'value', |
|
179 '-p', 'default_tag', lower_link] |
|
180 stdout = utils.execute(cmd) |
|
181 if stdout.splitlines()[0].strip() == vid: |
|
182 vid = '0' |
|
183 else: |
|
184 vid = '0' |
174 cmd = ['/usr/sbin/dladm', 'create-vnic', '-l', lower_link, |
185 cmd = ['/usr/sbin/dladm', 'create-vnic', '-l', lower_link, |
175 '-m', mac_address, self._dlname] |
186 '-m', mac_address, '-v', vid, self._dlname] |
176 if temp: |
187 if temp: |
177 cmd.append('-t') |
188 cmd.append('-t') |
178 |
189 |
179 return self.execute_with_pfexec(cmd) |
190 return self.execute_with_pfexec(cmd) |
180 |
191 |
184 |
195 |
185 cmd = ['/usr/sbin/dladm', 'delete-vnic', self._dlname] |
196 cmd = ['/usr/sbin/dladm', 'delete-vnic', self._dlname] |
186 return self.execute_with_pfexec(cmd) |
197 return self.execute_with_pfexec(cmd) |
187 |
198 |
188 |
199 |
189 class IppoolCommand(CommandBase): |
200 class IPpoolCommand(CommandBase): |
190 '''Wrapper around Solaris ippool(1m) command''' |
201 '''Wrapper around Solaris ippool(1m) command''' |
191 |
202 |
192 def __init__(self, pool_name, role='ipf', pool_type='tree'): |
203 def __init__(self, pool_name, role='ipf', pool_type='tree'): |
193 self._pool_name = pool_name |
204 self._pool_name = pool_name |
194 self._role = role |
205 self._role = role |
246 cmd = ['/usr/sbin/ippool', '-R', '-m', self._pool_name, |
257 cmd = ['/usr/sbin/ippool', '-R', '-m', self._pool_name, |
247 '-o', self._role, '-t', self._pool_type] |
258 '-o', self._role, '-t', self._pool_type] |
248 self.execute_with_pfexec(cmd) |
259 self.execute_with_pfexec(cmd) |
249 |
260 |
250 |
261 |
251 class IpfilterCommand(CommandBase): |
262 class IPfilterCommand(CommandBase): |
252 '''Wrapper around Solaris ipf(1m) command''' |
263 '''Wrapper around Solaris ipf(1m) command''' |
253 |
264 |
254 def split_rules(self, rules): |
265 def split_rules(self, rules): |
255 # assumes that rules are inbound! |
266 # assumes that rules are inbound! |
256 cmd = ['/usr/sbin/ipfstat', '-i'] |
267 cmd = ['/usr/sbin/ipfstat', '-i'] |
280 if version == '6': |
291 if version == '6': |
281 cmd.append('-6') |
292 cmd.append('-6') |
282 return self.execute_with_pfexec(cmd, process_input=process_input) |
293 return self.execute_with_pfexec(cmd, process_input=process_input) |
283 |
294 |
284 |
295 |
285 class IpnatCommand(CommandBase): |
296 class IPnatCommand(CommandBase): |
286 '''Wrapper around Solaris ipnat(1m) command''' |
297 '''Wrapper around Solaris ipnat(1m) command''' |
287 |
298 |
288 def add_rules(self, rules): |
299 def add_rules(self, rules): |
289 process_input = '\n'.join(rules) |
300 process_input = '\n'.join(rules) |
290 cmd = ['/usr/sbin/ipnat', '-f', '-'] |
301 cmd = ['/usr/sbin/ipnat', '-f', '-'] |