Skip to content

Commit

Permalink
Merge pull request #29185 from jirikotlin/develop
Browse files Browse the repository at this point in the history
Support of tunnels in openvswitch modules.
  • Loading branch information
Mike Place committed Nov 25, 2015
2 parents d18ec83 + 55b81ad commit ccf46a4
Show file tree
Hide file tree
Showing 3 changed files with 362 additions and 21 deletions.
190 changes: 189 additions & 1 deletion salt/modules/openvswitch.py
Expand Up @@ -215,7 +215,7 @@ def port_remove(br, port, if_exists=True):
Args:
br: A string - bridge name (If bridge is None, port is removed from whatever bridge contains it)
port: A string - port name (Required argument)
port: A string - port name.
if_exists: Bool, if False - attempting to delete a por that does not exist returns False. (Default True)
Returns:
Expand Down Expand Up @@ -243,6 +243,9 @@ def port_list(br):
'''
Lists all of the ports within bridge.
Args:
br: A string - bridge name.
Returns:
List of bridges (or empty list), False on failure.
Expand All @@ -258,3 +261,188 @@ def port_list(br):
retcode = result['retcode']
stdout = result['stdout']
return _stdout_list_split(retcode, stdout)


def port_get_tag(port):
'''
Lists tags of the port.
Args:
port: A string - port name.
Returns:
List of tags (or empty list), False on failure.
.. versionadded:: 2015.8.2
CLI Example:
.. code-block:: bash
salt '*' openvswitch.port_get_tag tap0
'''
cmd = 'ovs-vsctl get port {0} tag'.format(port)
result = __salt__['cmd.run_all'](cmd)
retcode = result['retcode']
stdout = result['stdout']
return _stdout_list_split(retcode, stdout)


def interface_get_options(port):
'''
Port's interface's optional parameters.
Args:
port: A string - port name.
Returns:
String containing optional parameters of port's interface, False on failure.
.. versionadded:: 2015.8.2
CLI Example:
.. code-block:: bash
salt '*' openvswitch.interface_get_options tap0
'''
cmd = 'ovs-vsctl get interface {0} options'.format(port)
result = __salt__['cmd.run_all'](cmd)
retcode = result['retcode']
stdout = result['stdout']
return _stdout_list_split(retcode, stdout)


def interface_get_type(port):
'''
Type of port's interface.
Args:
port: A string - port name.
Returns:
String - type of interface or empty string, False on failure.
.. versionadded:: 2015.8.2
CLI Example:
.. code-block:: bash
salt '*' openvswitch.interface_get_type tap0
'''
cmd = 'ovs-vsctl get interface {0} type'.format(port)
result = __salt__['cmd.run_all'](cmd)
retcode = result['retcode']
stdout = result['stdout']
return _stdout_list_split(retcode, stdout)


def port_create_vlan(br, port, id):
'''
Isolate VM traffic using VLANs.
Args:
br: A string - bridge name.
port: A string - port name.
id: An integer in the valid range 0 to 4095 (inclusive), name of VLAN.
Returns:
True on success, else False.
.. versionadded:: 2015.8.2
CLI Example:
.. code-block:: bash
salt '*' openvswitch.port_create_vlan br0 tap0 100
'''
interfaces = __salt__['network.interfaces']()
if not 0 <= id <= 4095:
return False
elif not bridge_exists(br):
return False
elif port not in interfaces:
return False
elif port in port_list(br):
cmd = 'ovs-vsctl set port {0} tag={1}'.format(port, id)
result = __salt__['cmd.run_all'](cmd)
return _retcode_to_bool(result['retcode'])
else:
cmd = 'ovs-vsctl add-port {0} {1} tag={2}'.format(br, port, id)
result = __salt__['cmd.run_all'](cmd)
return _retcode_to_bool(result['retcode'])


def port_create_gre(br, port, id, remote):
'''
Generic Routing Encapsulation - creates GRE tunnel between endpoints.
Args:
br: A string - bridge name.
port: A string - port name.
id: An integer - unsigned 32-bit number, tunnel's key.
remote: A string - remote endpoint's IP address.
Returns:
True on success, else False.
.. versionadded:: 2015.8.2
CLI Example:
.. code-block:: bash
salt '*' openvswitch.port_create_gre br0 gre1 5001 192.168.1.10
'''
if not 0 <= id < 2**32:
return False
elif not __salt__['dig.check_ip'](remote):
return False
elif not bridge_exists(br):
return False
elif port in port_list(br):
cmd = 'ovs-vsctl set interface {0} type=gre options:remote_ip={1} options:key={2}'.format(port, remote, id)
result = __salt__['cmd.run_all'](cmd)
return _retcode_to_bool(result['retcode'])
else:
cmd = 'ovs-vsctl add-port {0} {1} -- set interface {1} type=gre options:remote_ip={2} ' \
'options:key={3}'.format(br, port, remote, id)
result = __salt__['cmd.run_all'](cmd)
return _retcode_to_bool(result['retcode'])


def port_create_vxlan(br, port, id, remote, dst_port=None):
'''
Virtual eXtensible Local Area Network - creates VXLAN tunnel between endpoints.
Args:
br: A string - bridge name.
port: A string - port name.
id: An integer - unsigned 64-bit number, tunnel's key.
remote: A string - remote endpoint's IP address.
dst_port: An integer - port to use when creating tunnelport in the switch.
Returns:
True on success, else False.
.. versionadded:: 2015.8.2
CLI Example:
.. code-block:: bash
salt '*' openvswitch.port_create_vxlan br0 vx1 5001 192.168.1.10 8472
'''
dst_port = ' options:dst_port=' + str(dst_port) if 0 < dst_port <= 65535 else ''
if not 0 <= id < 2**64:
return False
elif not __salt__['dig.check_ip'](remote):
return False
elif not bridge_exists(br):
return False
elif port in port_list(br):
cmd = 'ovs-vsctl set interface {0} type=vxlan options:remote_ip={1} ' \
'options:key={2}{3}'.format(port, remote, id, dst_port)
result = __salt__['cmd.run_all'](cmd)
return _retcode_to_bool(result['retcode'])
else:
cmd = 'ovs-vsctl add-port {0} {1} -- set interface {1} type=vxlan options:remote_ip={2} ' \
'options:key={3}{4}'.format(br, port, remote, id, dst_port)
result = __salt__['cmd.run_all'](cmd)
return _retcode_to_bool(result['retcode'])
2 changes: 0 additions & 2 deletions salt/states/openvswitch_bridge.py
Expand Up @@ -40,7 +40,6 @@ def present(name):
else:
ret['result'] = None
ret['comment'] = comment_bridge_created
ret['changes'] = changes_bridge_created

return ret

Expand Down Expand Up @@ -90,7 +89,6 @@ def absent(name):
else:
ret['result'] = None
ret['comment'] = comment_bridge_deleted
ret['changes'] = changes_bridge_deleted

return ret

Expand Down

0 comments on commit ccf46a4

Please sign in to comment.