]>
git.proxmox.com Git - mirror_ifupdown2.git/blob - ifupdown2/addons/tunnel.py
3 # Maximilian Wilhelm <max@rfc2324.org>
4 # -- Mon 10 Oct 2016 10:53:13 PM CEST
7 from ifupdown2
.ifupdown
.iface
import *
8 from ifupdown2
.ifupdown
.netlink
import netlink
10 from ifupdown2
.ifupdownaddons
.LinkUtils
import LinkUtils
11 from ifupdown2
.ifupdownaddons
.modulebase
import moduleBase
13 import ifupdown2
.ifupdown
.ifupdownflags
as ifupdownflags
15 from ifupdown
.iface
import *
16 from ifupdown
.netlink
import netlink
18 from ifupdownaddons
.LinkUtils
import LinkUtils
19 from ifupdownaddons
.modulebase
import moduleBase
21 import ifupdown
.ifupdownflags
as ifupdownflags
25 # TODO: Add checks for ipip tunnels.
27 class tunnel(moduleBase
):
29 ifupdown2 addon module to configure tunnels
32 'mhelp': 'create/configure GRE/IPIP/SIT and GRETAP tunnel interfaces',
35 'help': 'type of tunnel as in \'ip link\' command.',
36 'validvals': ['gre', 'gretap', 'ipip', 'sit', 'vti', 'ip6gre', 'ipip6', 'ip6ip6', 'vti6'],
38 'example': ['mode gre']
41 'help': 'IP of local tunnel endpoint',
42 'validvals': ['<ipv4>', '<ipv6>'],
44 'example': ['local 192.2.0.42']
47 'help': 'IP of remote tunnel endpoint',
48 'validvals': ['<ipv4>', '<ipv6>'],
50 'example': ['endpoint 192.2.0.23']
53 'help': 'TTL for tunnel packets',
54 'validvals': ['<number>'],
59 'help': 'Physical underlay device to use for tunnel packets',
60 'validvals': ['<interface>'],
62 'example': ['tunnel-physdev eth1']
67 def __init__(self
, *args
, **kargs
):
68 moduleBase
.__init
__(self
, *args
, **kargs
)
72 def _is_my_interface(ifaceobj
):
73 return ifaceobj
.addr_method
== "tunnel" and ifaceobj
.get_attr_value_first('mode')
75 def _has_config_changed(self
, attrs_present
, attrs_configured
):
76 for key
, value
in attrs_configured
.iteritems():
77 if attrs_present
.get(key
) != value
:
81 def _up(self
, ifaceobj
):
83 # attr_name -> ip route param name
87 'tunnel-physdev': 'dev',
90 mode
= ifaceobj
.get_attr_value_first('mode')
94 # Only include attributes which have been set and map ifupdown2 names
95 # to attribute names expected by iproute
96 for attr
, iproute_attr
in attr_map
.items():
97 attr_val
= ifaceobj
.get_attr_value_first(attr
)
99 attrs_mapped
[iproute_attr
] = attr_val
100 attrs
[attr
] = attr_val
102 # Create the tunnel if it doesn't exist yet...
103 if not self
.ipcmd
.link_exists(ifaceobj
.name
):
104 self
.ipcmd
.tunnel_create(ifaceobj
.name
, mode
, attrs_mapped
)
107 # If it's present, check if there were changes
108 current_attrs
= self
.ipcmd
.link_get_linkinfo_attrs(ifaceobj
.name
)
109 current_mode
= self
.ipcmd
.link_cache_get([ifaceobj
.name
, 'kind'])
112 if current_attrs
and current_mode
!= mode
or self
._has
_config
_changed
(current_attrs
, attrs
):
113 # Mode and some other changes are not possible without recreating the interface,
114 # so just recreate it IFF there have been changes.
115 self
.ipcmd
.link_delete(ifaceobj
.name
)
116 self
.ipcmd
.tunnel_create(ifaceobj
.name
, mode
, attrs_mapped
)
118 self
.log_warn(str(e
))
120 def _down(self
, ifaceobj
):
121 if not ifupdownflags
.flags
.PERFMODE
and not self
.ipcmd
.link_exists(ifaceobj
.name
):
124 self
.ipcmd
.link_delete(ifaceobj
.name
)
126 self
.log_warn(str(e
))
128 def get_dependent_ifacenames(self
, ifaceobj
, ifacenames_all
=None):
129 if not self
._is
_my
_interface
(ifaceobj
):
132 device
= ifaceobj
.get_attr_value_first('tunnel-physdev')
139 def _query_check_n_update(ifaceobj
, ifaceobjcurr
, attrname
, attrval
, running_attrval
):
140 if not ifaceobj
.get_attr_value_first(attrname
):
142 if running_attrval
and attrval
== running_attrval
:
143 ifaceobjcurr
.update_config_with_status(attrname
, attrval
, 0)
145 ifaceobjcurr
.update_config_with_status(attrname
, running_attrval
, 1)
147 def _query_check(self
, ifaceobj
, ifaceobjcurr
):
148 ifname
= ifaceobj
.name
150 if not self
.ipcmd
.link_exists(ifname
):
153 tunattrs
= self
.ipcmd
.link_get_linkinfo_attrs(ifaceobj
.name
)
155 ifaceobjcurr
.check_n_update_config_with_status_many(ifaceobj
, self
.get_mod_attrs(), -1)
158 tunattrs
["mode"] = self
.ipcmd
.link_get_kind(ifname
)
160 user_config_mode
= ifaceobj
.get_attr_value_first("mode")
161 if user_config_mode
in ('ipip6', 'ip6ip6'):
162 ifaceobj
.replace_config("mode", "ip6tnl")
164 for attr
in self
.get_mod_attrs():
165 if not ifaceobj
.get_attr_value_first(attr
):
168 # Validate all interface attributes set in the config.
169 # Remote any leading 'tunnel-' prefix in front of the attr name
170 # when accessing tunattrs parsed from 'ip -d link'.
171 self
._query
_check
_n
_update
(ifaceobj
, ifaceobjcurr
, attr
,
172 ifaceobj
.get_attr_value_first(attr
),
175 # Operations supported by this addon (yet).
179 'query-checkcurr': _query_check
183 return self
._run
_ops
.keys()
185 def _init_command_handlers(self
):
187 self
.ipcmd
= LinkUtils()
189 def run(self
, ifaceobj
, operation
, query_ifaceobj
=None, **extra_args
):
190 op_handler
= self
._run
_ops
.get(operation
)
194 if operation
!= 'query-running' and not self
._is
_my
_interface
(ifaceobj
):
197 self
._init
_command
_handlers
()
198 if operation
== 'query-checkcurr':
199 op_handler(self
, ifaceobj
, query_ifaceobj
)
201 op_handler(self
, ifaceobj
)