Debian Codegraph @

networking_bagpipe/bagpipe_bgp/vpn/evpn/ovs.py

raw
1 # Copyright 2018 Orange
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 from oslo_config import cfg
16 from oslo_log import log as logging
17
18 from networking_bagpipe._i18n import _
19 from networking_bagpipe.bagpipe_bgp.common import config
20 from networking_bagpipe.bagpipe_bgp.common import dataplane_utils
21 from networking_bagpipe.bagpipe_bgp.common import log_decorator
22 from networking_bagpipe.bagpipe_bgp import constants as consts
23 from networking_bagpipe.bagpipe_bgp.engine import exa
24 from networking_bagpipe.bagpipe_bgp.vpn import dataplane_drivers as dp_drivers
25 from networking_bagpipe.bagpipe_bgp.vpn import evpn
26
27 from neutron.plugins.ml2.drivers.openvswitch.agent.openflow.native import \
28 br_tun
29 from neutron.plugins.ml2.drivers.openvswitch.agent import ovs_neutron_agent
30
31 from neutron_lib import constants as n_consts
32 from neutron_lib.plugins.ml2 import ovs_constants as ovs_const
33
34 LOG = logging.getLogger(__name__)
35
36 FLOOD = "flood"
37 FLOW_PRIORITY = 5
38
39
40 class OVSEVIDataplane(evpn.VPNInstanceDataplane):
41
42 def __init__(self, *args, **kwargs):
43 super().__init__(*args, **kwargs)
44
45 self.bridge = self.driver.bridge
46 # OpenFlow 1.3 is needed for mod_vlan_vid
47 self.bridge.use_at_least_protocol(ovs_const.OPENFLOW13)
48
49 self.tunnel_mgr = dataplane_utils.SharedObjectLifecycleManagerProxy(
50 self.driver.tunnel_mgr,
51 self.instance_id
52 )
53 self.flooding_ports = set()
54 self.vlan = None
55 self.local_ip = self.driver.get_local_address()
56
57 def cleanup(self):
58 self.bridge.delete_flows(strict=True,
59 table=ovs_const.FLOOD_TO_TUN,
60 priority=FLOW_PRIORITY,
61 dl_vlan=self.vlan)
62 self.bridge.delete_group(group_id=self.vlan)
63
64 @log_decorator.log_info
65 def vif_plugged(self, mac_address, ip_address_prefix, localport, label,
66 direction):
67 if 'vlan' not in localport:
68 raise Exception(_("missing localport['vlan'] parameter"))
69 if self.vlan and localport['vlan'] != self.vlan:
70 raise Exception(_("inconsistent vlan"))
71 else:
72 self.vlan = localport['vlan']
73
74 # map traffic to this EVI VNI to the right table, similarly as in
75 # OVSTunnelBridge.provision_local_vlan
76 self.bridge.add_flow(table=ovs_const.VXLAN_TUN_TO_LV,
77 priority=FLOW_PRIORITY,
78 tun_id=self.instance_label,
79 actions=("push_vlan:0x8100,mod_vlan_vid:%d,"
80 "resubmit(,%s)" %
81 (self.vlan, ovs_const.LEARN_FROM_TUN)))
82
83 @log_decorator.log_info
84 def vif_unplugged(self, mac_address, ip_address_prefix, localport, label,
85 direction, last_endpoint=True):
86 self.log.debug("nothing to do on unplug")
87
88 def _local_vni_actions(self, vni):
89 # "load:0->NXM_OF_IN_PORT[]" allows the packets coming from br-int
90 # via patch-port to go back to br-int via the same port
91 return "load:0->NXM_OF_IN_PORT[],set_tunnel:%d,resubmit(,%s)" % (
92 vni, ovs_const.VXLAN_TUN_TO_LV)
93
94 @log_decorator.log_info
95 def setup_dataplane_for_remote_endpoint(self, prefix, remote_pe, vni, nlri,
96 encaps):
97 mac = prefix
98 ip = nlri.ip
99
100 # what is done here is similar as
101 # OVSTunnelBridge.install_unicast_to_tun, but with local delivery to
102 # table VXLAN_TUN_TO_LV for routes advertized locally
103 if remote_pe == self.local_ip:
104 actions = self._local_vni_actions(vni)
105 else:
106 port, _ = self.tunnel_mgr.get_object(remote_pe, (vni, mac))
107 actions = "set_tunnel:%d,output:%s" % (vni, port)
108 self.bridge.add_flow(table=ovs_const.UCAST_TO_TUN,
109 priority=FLOW_PRIORITY,
110 dl_vlan=self.vlan,
111 dl_dst=mac,
112 actions="strip_vlan,%s" % actions)
113
114 # add ARP responder
115 if ip:
116 self.bridge.install_arp_responder(self.vlan, str(ip), str(mac))
117
118 @log_decorator.log
119 def remove_dataplane_for_remote_endpoint(self, prefix, remote_pe, vni,
120 nlri):
121 mac = prefix
122 ip = nlri.ip
123
124 self.bridge.delete_unicast_to_tun(self.vlan, mac)
125
126 if remote_pe != self.local_ip:
127 self.tunnel_mgr.free_object(remote_pe, (vni, mac))
128
129 # cleanup ARP responder
130 if ip:
131 self.bridge.delete_arp_responder(self.vlan, str(ip))
132
133 @log_decorator.log_info
134 def add_dataplane_for_bum_endpoint(self, remote_pe, vni, nlri, encaps):
135
136 if remote_pe == self.local_ip:
137 port = "local"
138 else:
139 port, _ = self.tunnel_mgr.get_object(remote_pe, (vni, FLOOD))
140 self.flooding_ports.add((port, vni))
141
142 self._update_flooding_buckets()
143
144 @log_decorator.log_info
145 def remove_dataplane_for_bum_endpoint(self, remote_pe, vni, nlri):
146
147 if remote_pe == self.local_ip:
148 port = "local"
149 else:
150 port = self.tunnel_mgr.find_object(remote_pe)
151
152 if port:
153 self.flooding_ports.remove((port, vni))
154
155 self._update_flooding_buckets()
156
157 if remote_pe != self.local_ip:
158 self.tunnel_mgr.free_object(remote_pe, (vni, FLOOD))
159
160 def _update_flooding_buckets(self):
161 buckets = []
162 for port, vni in self.flooding_ports:
163 if port == "local":
164 buckets.append("bucket=strip_vlan,%s" %
165 self._local_vni_actions(vni))
166 else:
167 buckets.append("bucket=strip_vlan,set_tunnel:%d,output:%s" %
168 (vni, port))
169 self.bridge.mod_group(group_id=self.vlan,
170 type='all',
171 buckets=','.join(buckets))
172 self.bridge.add_flow(table=ovs_const.FLOOD_TO_TUN,
173 priority=FLOW_PRIORITY,
174 dl_vlan=self.vlan,
175 actions="group:%d" % self.vlan)
176 self.log.debug("buckets: %s", buckets)
177
178 def set_gateway_port(self, linuxif, gateway_ip):
179 # nothing to do, because we make the assumption that the
180 # IPVPN driver is 'ovs' as well, and setup in conjunction
181 # with Neutron OVS BGPVPN extension which does the plugging
182 # between L2 and L3
183 pass
184
185 def gateway_port_down(self, linuxif):
186 pass
187
188 # Looking glass ####
189
190 def get_lg_local_info(self, path_prefix):
191 return {
192 "vlan": self.vlan,
193 "flooding-ports": [{"port": str(port), "vni": vni}
194 for port, vni in self.flooding_ports]
195 }
196
197
198 class TunnelManager(dataplane_utils.ObjectLifecycleManager):
199
200 def __init__(self, bridge, local_ip):
201 super().__init__()
202
203 self.bridge = bridge
204 self.local_ip = local_ip
205
206 @log_decorator.log_info
207 def create_object(self, remote_ip, *args, **kwargs):
208 port_name = ovs_neutron_agent.OVSNeutronAgent.get_tunnel_name(
209 n_consts.TYPE_VXLAN, self.local_ip, remote_ip)
210 tunnel = self.bridge.add_tunnel_port(port_name,
211 remote_ip,
212 self.local_ip,
213 n_consts.TYPE_VXLAN)
214
215 self.bridge.setup_tunnel_port(n_consts.TYPE_VXLAN, tunnel)
216 LOG.debug("tunnel for %s: %s (%s)", remote_ip, port_name, tunnel)
217
218 return tunnel
219
220 @log_decorator.log_info
221 def delete_object(self, tunnel):
222 self.bridge.delete_port(tunnel)
223
224
225 class OVSDataplaneDriver(dp_drivers.DataplaneDriver):
226
227 dataplane_instance_class = OVSEVIDataplane
228 type = consts.EVPN
229 ecmp_support = False
230 encaps = [exa.Encapsulation(exa.Encapsulation.Type.VXLAN)]
231
232 driver_opts = [
233 cfg.StrOpt("ovs_bridge", default="br-tun",
234 help=("Name of the OVS bridge to use, this has to be the "
235 "same as the tunneling bridge of the Neutron OVS "
236 "agent, usually br-tun")),
237 ]
238
239 def __init__(self, *args, **kwargs):
240 super().__init__(*args, **kwargs)
241
242 config.set_default_root_helper()
243
244 self.bridge = dataplane_utils.OVSBridgeWithGroups(
245 br_tun.OVSTunnelBridge(self.config.ovs_bridge, os_ken_app=self)
246 )
247 self.tunnel_mgr = TunnelManager(self.bridge,
248 self.get_local_address())
249
250 def needs_cleanup_assist(self):
251 return True
252
253 def reset_state(self):
254 # cleanup is taken care of by OVS Neutron Agent
255 pass
256
257 # Looking glass ####
258
259 def get_lg_local_info(self, path_prefix):
260 return {
261 "tunnels": self.tunnel_mgr.infos(),
262 }