+# del-iface.<node_id>-<node_id>@<endpoint>:
+# Deletes the interface of link <node_id1>-<node_id2> on either
+# <node_id1> or <node_id2>, according to <endpoint>.
+#
+# We need a running db daemon to do this, but we do not have to
+# redo the delete if the db daemon is restarted.
+#
+# This also invalidates the local port of the tunnel and the endpoint
+# info on both sides of the tunnel. The link goes down.
+del-iface.%: | cache/db.$$(call get,%)
+ @echo "Removing interface for link $(call linkpart,$*) from $(call get,$*)"
+ @$(SSH) $(HOST_$(call get,$*)) \
+ $(SUDO) sliver-ovs del-port L$(call linkpart,$*)
+ @rm -f cache/iface.$* \
+ cache/port.$* cache/endpoint.$* cache/endpoint.$(call opp,$*) \
+ L/$(call linkpart,$*)
+
+
+### '%' here is leftid-rightid@{1,2}
+# we retrieve % as $(*F)
+#linkid=$(call linkpart,%)
+#nodeid=$(call get,%)
+#bridgefile=cache/bridge.$(nodeid)
+
+# cache/iface.<node_id1>-<node_id2>@<endpoint>:
+# Creates the interface for link <node_id1>-<node_id2> on
+# <node_id<endpoint>>.
+#
+# The bridge of the local node must already exist, and we need to create
+# the interface again if the bridge is re-created.
+# We also need a running db daemon, but we do not need to do anything
+# if the db daemon is restarted.
+cache/iface.%: cache/bridge.$$(call get,%) | cache/db.$$(call get,%)
+ @echo "Creating interface for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
+ @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs create-port $(BRIDGE) \
+ L$(call linkpart,$(*F)) > $(call log,$@) 2>&1
+ @touch $@
+
+# cache/port.<node_id1>-<node_id2>@<endpoint>:
+# Retrieves the local port of link <node_id1>-<node_id2> on
+# node <node_id<endpoint>>.
+#
+# The local interface must have been created already and the
+# switch daemon must be running. We need to retrieve the port
+# number again if the interface is re-created, or the switch
+# daemon is restarted.
+cache/port.%: cache/iface.% cache/switch.$$(call get,%)
+ @echo "Getting port number for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
+ @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) \
+ sliver-ovs get-local-endpoint L$(call linkpart,$(*F)) > $@ 2> $(call log,$@)
+
+
+# linkid=$(call linkpart,%)
+# nodeid=$(call get,%)
+# iface1=cache/iface.%
+# iface2=cache/iface.$(call opp,%)
+
+# cache/endpoint.<node_id1>-<node_id2>@<endpoint>:
+# Sets the other side (IP address, UDP port) info for link <node_id1>-<node_id2>
+# on <node_id<endpoint>>.
+#
+# We need the IP address and the UDP port of the other side and the interace of this side.
+# We need to set the info again if any of these change.
+cache/endpoint.%: cache/host.$$(call rget,%) cache/port.$$(call opp,%) cache/iface.%
+ @echo "Setting port number of link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
+ @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs set-remote-endpoint L$(call linkpart,$(*F)) \
+ $$(cat cache/host.$(call rget,$(*F))) \
+ $$(cat cache/port.$(call opp,$(*F))) 2> $(call log,$@)
+ @touch $@
+
+####################
+CLEANTARGETS=$(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
+clean: $(CLEANTARGETS)
+distclean:
+ rm -rf L log cache
+.PHONY: clean distclean
+
+####################
+graph.dot:
+ ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
+graph.ps: graph.dot
+ dot -Tps < $^ > $@