2 # Copyright (C) 2013 Nicira, Inc. All Rights Reserved.
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
17 # - Doesn't support multicast other than "unknown-dst"
30 import ovs.unixctl.server
38 __pychecker__ = 'no-reuseattr' # Remove in pychecker >= 0.8.19.
39 vlog = ovs.vlog.Vlog("ovs-vtep")
49 def call_prog(prog, args_list):
50 cmd = [prog, "-vconsole:off"] + args_list
51 output = subprocess.Popen(cmd, stdout=subprocess.PIPE).communicate()
52 if len(output) == 0 or output[0] == None:
55 output = output[0].strip()
59 return call_prog("ovs-vsctl", shlex.split(args))
62 return call_prog("ovs-ofctl", shlex.split(args))
65 return call_prog("vtep-ctl", shlex.split(args))
68 def unixctl_exit(conn, unused_argv, unused_aux):
74 class Logical_Switch(object):
75 def __init__(self, ls_name):
79 self.short_name = "vtep_ls" + str(ls_count)
80 vlog.info("creating lswitch %s (%s)" % (self.name, self.short_name))
83 self.local_macs = set()
85 self.unknown_dsts = set()
90 vlog.info("destroying lswitch %s" % self.name)
93 column = vtep_ctl("--columns=tunnel_key find logical_switch "
94 "name=%s" % self.name)
95 tunnel_key = column.partition(":")[2].strip()
96 if (tunnel_key and type(eval(tunnel_key)) == types.IntType):
97 self.tunnel_key = tunnel_key
98 vlog.info("using tunnel key %s in %s"
99 % (self.tunnel_key, self.name))
102 vlog.warn("invalid tunnel key for %s, using 0" % self.name)
104 ovs_vsctl("--may-exist add-br %s" % self.short_name)
105 ovs_vsctl("br-set-external-id %s vtep_logical_switch true"
107 ovs_vsctl("br-set-external-id %s logical_switch_name %s"
108 % (self.short_name, self.name))
110 vtep_ctl("clear-local-macs %s" % self.name)
111 vtep_ctl("add-mcast-local %s unknown-dst %s" % (self.name, Tunnel_Ip))
113 ovs_ofctl("del-flows %s" % self.short_name)
114 ovs_ofctl("add-flow %s priority=0,action=drop" % self.short_name)
116 def update_flood(self):
117 flood_ports = self.ports.values()
119 # Traffic flowing from one 'unknown-dst' should not be flooded to
120 # port belonging to another 'unknown-dst'.
121 for tunnel in self.unknown_dsts:
122 port_no = self.tunnels[tunnel][0]
123 ovs_ofctl("add-flow %s table=1,priority=1,in_port=%s,action=%s"
124 % (self.short_name, port_no, ",".join(flood_ports)))
126 # Traffic coming from a VTEP physical port should only be flooded to
127 # one 'unknown-dst' and to all other physical ports that belong to that
128 # VTEP device and this logical switch.
129 for tunnel in self.unknown_dsts:
130 port_no = self.tunnels[tunnel][0]
131 flood_ports.append(port_no)
134 ovs_ofctl("add-flow %s table=1,priority=0,action=%s"
135 % (self.short_name, ",".join(flood_ports)))
137 def add_lbinding(self, lbinding):
138 vlog.info("adding %s binding to %s" % (lbinding, self.name))
139 port_no = ovs_vsctl("get Interface %s ofport" % lbinding)
140 self.ports[lbinding] = port_no
141 ovs_ofctl("add-flow %s in_port=%s,action=learn(table=1,"
142 "priority=1000,idle_timeout=15,cookie=0x5000,"
143 "NXM_OF_ETH_DST[]=NXM_OF_ETH_SRC[],"
144 "output:NXM_OF_IN_PORT[]),resubmit(,1)"
145 % (self.short_name, port_no))
149 def del_lbinding(self, lbinding):
150 vlog.info("removing %s binding from %s" % (lbinding, self.name))
151 port_no = self.ports[lbinding]
152 ovs_ofctl("del-flows %s in_port=%s" % (self.short_name, port_no));
153 del self.ports[lbinding]
156 def add_tunnel(self, tunnel):
158 vlog.info("adding tunnel %s" % tunnel)
159 encap, ip = tunnel.split("/")
161 if encap != "vxlan_over_ipv4":
162 vlog.warn("unsupported tunnel format %s" % encap)
166 tun_name = "vx" + str(tun_id)
168 ovs_vsctl("add-port %s %s -- set Interface %s type=vxlan "
169 "options:key=%s options:remote_ip=%s"
170 % (self.short_name, tun_name, tun_name, self.tunnel_key, ip))
173 port_no = ovs_vsctl("get Interface %s ofport" % tun_name)
177 vlog.warn("couldn't create tunnel %s" % tunnel)
178 ovs_vsctl("del-port %s %s" % (self.short_name, tun_name))
181 # Give the system a moment to allocate the port number
184 self.tunnels[tunnel] = (port_no, tun_name)
186 ovs_ofctl("add-flow %s table=0,priority=1000,in_port=%s,"
187 "actions=resubmit(,1)"
188 % (self.short_name, port_no))
190 def del_tunnel(self, tunnel):
191 vlog.info("removing tunnel %s" % tunnel)
193 port_no, tun_name = self.tunnels[tunnel]
194 ovs_ofctl("del-flows %s table=0,in_port=%s"
195 % (self.short_name, port_no))
196 ovs_vsctl("del-port %s %s" % (self.short_name, tun_name))
198 del self.tunnels[tunnel]
200 def update_local_macs(self):
201 flows = ovs_ofctl("dump-flows %s cookie=0x5000/-1,table=1"
202 % self.short_name).splitlines()
205 mac = re.split(r'.*dl_dst=(.*) .*', f)
209 for mac in macs.difference(self.local_macs):
210 vlog.info("adding local ucast %s to %s" % (mac, self.name))
211 vtep_ctl("add-ucast-local %s %s %s" % (self.name, mac, Tunnel_Ip))
213 for mac in self.local_macs.difference(macs):
214 vlog.info("removing local ucast %s from %s" % (mac, self.name))
215 vtep_ctl("del-ucast-local %s %s" % (self.name, mac))
217 self.local_macs = macs
219 def add_remote_mac(self, mac, tunnel):
220 port_no = self.tunnels.get(tunnel, (0,""))[0]
224 ovs_ofctl("add-flow %s table=1,priority=1000,dl_dst=%s,action=%s"
225 % (self.short_name, mac, port_no))
227 def del_remote_mac(self, mac):
228 ovs_ofctl("del-flows %s table=1,dl_dst=%s" % (self.short_name, mac))
230 def update_remote_macs(self):
236 mac_list = vtep_ctl("list-remote-macs %s" % self.name).splitlines()
237 for line in mac_list:
238 if (line.find("mcast-mac-remote") != -1):
242 entry = re.split(r' (.*) -> (.*)', line)
247 remote_macs[entry[1]] = entry[2]
249 if entry[1] != "unknown-dst":
252 unknown_dsts.add(entry[2])
254 tunnels.add(entry[2])
256 old_tunnels = set(self.tunnels.keys())
258 for tunnel in tunnels.difference(old_tunnels):
259 self.add_tunnel(tunnel)
261 for tunnel in old_tunnels.difference(tunnels):
262 self.del_tunnel(tunnel)
264 for mac in remote_macs.keys():
265 if (self.remote_macs.get(mac) != remote_macs[mac]):
266 self.add_remote_mac(mac, remote_macs[mac])
268 for mac in self.remote_macs.keys():
269 if not remote_macs.has_key(mac):
270 self.del_remote_mac(mac)
272 self.remote_macs = remote_macs
274 if (self.unknown_dsts != unknown_dsts):
275 self.unknown_dsts = unknown_dsts
278 def update_stats(self):
279 # Map Open_vSwitch's "interface:statistics" to columns of
280 # vtep's logical_binding_stats. Since we are using the 'interface' from
281 # the logical switch to collect stats, packets transmitted from it
282 # is received in the physical switch and vice versa.
283 stats_map = {'tx_packets':'packets_to_local',
284 'tx_bytes':'bytes_to_local',
285 'rx_packets':'packets_from_local',
286 'rx_bytes':'bytes_from_local'}
288 # Go through all the logical switch's interfaces that end with "-l"
289 # and copy the statistics to logical_binding_stats.
290 for interface in self.ports.iterkeys():
291 if not interface.endswith("-l"):
293 # Physical ports can have a '-' as part of its name.
294 vlan, remainder = interface.split("-", 1)
295 pp_name, logical = remainder.rsplit("-", 1)
296 uuid = vtep_ctl("get physical_port %s vlan_stats:%s"
301 for (mapfrom, mapto) in stats_map.iteritems():
302 value = ovs_vsctl("get interface %s statistics:%s"
303 % (interface, mapfrom)).strip('"')
304 vtep_ctl("set logical_binding_stats %s %s=%s"
305 % (uuid, mapto, value))
308 self.update_local_macs()
309 self.update_remote_macs()
312 def add_binding(binding, ls):
313 vlog.info("adding binding %s" % binding)
315 vlan, pp_name = binding.split("-", 1)
316 pbinding = binding+"-p"
317 lbinding = binding+"-l"
319 # Create a patch port that connects the VLAN+port to the lswitch.
320 # Do them as two separate calls so if one side already exists, the
321 # other side is created.
322 ovs_vsctl("add-port %s %s "
323 " -- set Interface %s type=patch options:peer=%s"
324 % (ps_name, pbinding, pbinding, lbinding))
325 ovs_vsctl("add-port %s %s "
326 " -- set Interface %s type=patch options:peer=%s"
327 % (ls.short_name, lbinding, lbinding, pbinding))
329 port_no = ovs_vsctl("get Interface %s ofport" % pp_name)
330 patch_no = ovs_vsctl("get Interface %s ofport" % pbinding)
331 vlan_ = vlan.lstrip('0')
333 ovs_ofctl("add-flow %s in_port=%s,dl_vlan=%s,action=strip_vlan,%s"
334 % (ps_name, port_no, vlan_, patch_no))
335 ovs_ofctl("add-flow %s in_port=%s,action=mod_vlan_vid:%s,%s"
336 % (ps_name, patch_no, vlan_, port_no))
338 ovs_ofctl("add-flow %s in_port=%s,action=%s"
339 % (ps_name, port_no, patch_no))
340 ovs_ofctl("add-flow %s in_port=%s,action=%s"
341 % (ps_name, patch_no, port_no))
343 # Create a logical_bindings_stats record.
346 vtep_ctl("set physical_port %s vlan_stats:%s=@stats --\
347 --id=@stats create logical_binding_stats packets_from_local=0"\
350 ls.add_lbinding(lbinding)
351 Bindings[binding] = ls.name
353 def del_binding(binding, ls):
354 vlog.info("removing binding %s" % binding)
356 vlan, pp_name = binding.split("-")
357 pbinding = binding+"-p"
358 lbinding = binding+"-l"
360 port_no = ovs_vsctl("get Interface %s ofport" % pp_name)
361 patch_no = ovs_vsctl("get Interface %s ofport" % pbinding)
362 vlan_ = vlan.lstrip('0')
364 ovs_ofctl("del-flows %s in_port=%s,dl_vlan=%s"
365 % (ps_name, port_no, vlan_))
366 ovs_ofctl("del-flows %s in_port=%s" % (ps_name, patch_no))
368 ovs_ofctl("del-flows %s in_port=%s" % (ps_name, port_no))
369 ovs_ofctl("del-flows %s in_port=%s" % (ps_name, patch_no))
371 ls.del_lbinding(lbinding)
373 # Destroy the patch port that connects the VLAN+port to the lswitch
374 ovs_vsctl("del-port %s %s -- del-port %s %s"
375 % (ps_name, pbinding, ls.short_name, lbinding))
377 # Remove the record that links vlan with stats in logical_binding_stats.
378 vtep_ctl("remove physical_port %s vlan_stats %s" % (pp_name, vlan))
380 del Bindings[binding]
382 def handle_physical():
383 # Gather physical ports except the patch ports we created
384 ovs_ports = ovs_vsctl("list-ports %s" % ps_name).split()
385 ovs_port_set = set([port for port in ovs_ports if port[-2:] != "-p"])
387 vtep_pp_set = set(vtep_ctl("list-ports %s" % ps_name).split())
389 for pp_name in ovs_port_set.difference(vtep_pp_set):
390 vlog.info("adding %s to %s" % (pp_name, ps_name))
391 vtep_ctl("add-port %s %s" % (ps_name, pp_name))
393 for pp_name in vtep_pp_set.difference(ovs_port_set):
394 vlog.info("deleting %s from %s" % (pp_name, ps_name))
395 vtep_ctl("del-port %s %s" % (ps_name, pp_name))
398 for pp_name in vtep_pp_set:
399 binding_set = set(vtep_ctl("list-bindings %s %s"
400 % (ps_name, pp_name)).splitlines())
402 for b in binding_set:
403 vlan, ls_name = b.split()
404 if ls_name not in Lswitches:
405 Lswitches[ls_name] = Logical_Switch(ls_name)
407 binding = "%s-%s" % (vlan, pp_name)
408 ls = Lswitches[ls_name]
409 new_bindings.add(binding)
411 if Bindings.has_key(binding):
412 if Bindings[binding] == ls_name:
415 del_binding(binding, Lswitches[Bindings[binding]])
417 add_binding(binding, ls)
420 dead_bindings = set(Bindings.keys()).difference(new_bindings)
421 for binding in dead_bindings:
422 ls_name = Bindings[binding]
423 ls = Lswitches[ls_name]
425 del_binding(binding, ls)
427 if not len(ls.ports):
428 ovs_vsctl("del-br %s" % Lswitches[ls_name].short_name)
429 vtep_ctl("clear-local-macs %s" % Lswitches[ls_name].name)
430 del Lswitches[ls_name]
433 br_list = ovs_vsctl("list-br").split()
434 if (ps_name not in br_list):
435 ovs.util.ovs_fatal(0, "couldn't find OVS bridge %s" % ps_name, vlog)
437 call_prog("vtep-ctl", ["set", "physical_switch", ps_name,
438 'description="OVS VTEP Emulator"'])
440 tunnel_ips = vtep_ctl("get physical_switch %s tunnel_ips"
441 % ps_name).strip('[]"').split(", ")
442 if len(tunnel_ips) != 1 or not tunnel_ips[0]:
443 ovs.util.ovs_fatal(0, "exactly one 'tunnel_ips' should be set", vlog)
446 Tunnel_Ip = tunnel_ips[0]
448 ovs_ofctl("del-flows %s" % ps_name)
450 # Remove any logical bridges from the previous run
452 if ovs_vsctl("br-get-external-id %s vtep_logical_switch"
454 # Remove the remote side of any logical switch
455 ovs_ports = ovs_vsctl("list-ports %s" % br).split()
456 for port in ovs_ports:
457 port_type = ovs_vsctl("get Interface %s type"
459 if port_type != "patch":
462 peer = ovs_vsctl("get Interface %s options:peer"
465 ovs_vsctl("del-port %s" % peer)
467 ovs_vsctl("del-br %s" % br)
471 parser = argparse.ArgumentParser()
472 parser.add_argument("ps_name", metavar="PS-NAME",
473 help="Name of physical switch.")
474 parser.add_argument("--root-prefix", metavar="DIR",
475 help="Use DIR as alternate root directory"
477 parser.add_argument("--version", action="version",
478 version="%s %s" % (ovs.util.PROGRAM_NAME, VERSION))
480 ovs.vlog.add_args(parser)
481 ovs.daemon.add_args(parser)
482 args = parser.parse_args()
483 ovs.vlog.handle_args(args)
484 ovs.daemon.handle_args(args)
488 root_prefix = args.root_prefix
491 ps_name = args.ps_name
493 ovs.daemon.daemonize()
495 ovs.unixctl.command_register("exit", "", 0, 0, unixctl_exit, None)
496 error, unixctl = ovs.unixctl.server.UnixctlServer.create(None,
499 ovs.util.ovs_fatal(error, "could not create unixctl server", vlog)
510 for ls_name, ls in Lswitches.items():
513 poller = ovs.poller.Poller()
515 poller.timer_wait(1000)
520 if __name__ == '__main__':
524 # Let system.exit() calls complete normally
527 vlog.exception("traceback")
528 sys.exit(ovs.daemon.RESTART_EXIT_CODE)