+remote-snapshot-ips: $(addprefix cache/rsnap.ip.,$(CONF_NODE_IDS))
+ @cat /dev/null $^
+
+remote-snapshot-links: $(addprefix cache/rsnap.links.,$(CONF_NODE_IDS))
+ @sort -u /dev/null $^
+
+.PHONY: remote-snapshot remote-snapshot-ips remote-snapshot-links
+
+
+cache/rsnap.ip.%: FORCE
+ @$(SSH) $(HOST_$*) $(SUDO) \
+ sliver-ovs get-local-ip $(BRIDGE) | sed 's/^/IP_$*=/' > $@
+
+cache/rsnap.links.%: FORCE
+ @$(SSH) $(HOST_$*) $(SUDO) \
+ sliver-ovs get-local-links $(BRIDGE) | sed -n 's/^L/LINKS += /p' > $@
+
+### update sliver-ovs
+update: $(addprefix update-,$(CONF_NODE_IDS))
+ @true
+
+update-%: FORCE
+ @[ -n "$(SLIVER_OVS)" ] || { echo "SLIVER_OVS not set" >&2; exit 1; }
+ @[ -f "$(SLIVER_OVS)" ] || { echo "$(SLIVER_OVS) not found" >&2; exit 1; }
+ @echo "Sending $(SLIVER_OVS) to $(call display,$*)"
+ @scp $(SSH_KEY_OPTION) -q $(SLIVER_OVS) $(SLICE)@$(call solve,$*):
+ @$(SSH) $(call solve,$*) $(SUDO) mv $(notdir $(SLIVER_OVS)) /usr/sbin/sliver-ovs
+
+.SECONDEXPANSION:
+
+# 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 < $^ > $@
+
+####################
+# googlemap stuff
+# Uses:
+# GMAP_SERVER, the hostname that runs an ndnmap instance
+# GMAP_PROBES, a list of tuples of the form
+# <linkid>=<nodeid>=<internal_linkid>=<frequency>
+# e.g.
+# SENDER-MUX=SENDER=3=0.5
+# which would mean, the link 'SENDER-MUX' should be monitored at the 'SENDER' node and is coded
+# in links.json with id=3, every half second
+# sliver-ovs gmap-probe is designed to run in background,
+# and so that a new run will kill any previously running instance
+gprobes: $(foreach probe,$(GMAP_PROBES),gprobe-$(probe))
+
+gprobe-%: FORCE
+ $(SSH) -n $(call solve,$(word 2,$(subst @, ,$(*F)))) $(SUDO) \
+ sliver-ovs gmap-probe L$(word 1,$(subst @, ,$(*F))) $(GMAP_SERVER) $(word 3,$(subst @, ,$(*F))) $(word 4,$(subst @, ,$(*F)))
+
+jsons: routers.json links.json geocode.json
+.PHONE: jsons
+
+routers.json: $(CONF)
+ @{ echo "["; \
+ sep=" "; \
+ for n in $(CONF_NODE_IDS); do \
+ echo " $$sep\"$$n\""; \
+ sep=", "; \
+ done; \
+ echo "]"; \
+ } > $@
+
+links.json: $(foreach l,$(LINKS),cache/json.$(l))
+ @{ echo "["; \
+ cat $^ | sed '2,$$s/^/, /'; \
+ echo "]"; \
+ } > $@
+
+cache/json.%: $(CONF)
+ @echo "{\"id\": $($*_linkid), \"start\": \"$(call get,$*@1)\", \"end\": \"$(call get,$*@2)\"}" > $@
+
+geocode.json: $(foreach n,$(CONF_NODE_IDS),cache/geocode.$(n))
+ @{ echo "{"; \
+ sep=" "; \
+ for f in $^; do \
+ echo -n "$$sep"; \
+ cat $$f; \
+ sep=", "; \
+ done; \
+ echo "}"; \
+ } > $@
+
+cache/geocode.%: cache/loc.% $(CONF)
+ @{ echo "\"$*\": {"; \
+ echo " \"name\": \"$(HOST_$*)\","; \
+ echo " \"shortname\": \"$*\","; \
+ echo " \"site\": \"\","; \
+ echo " \"backbone\": false,"; \
+ echo " \"position\": [ $(shell cat cache/loc.$*) ]"; \
+ echo " }"; \
+ } > $@
+
+cache/loc.%: $(CONF)
+ @wget -O - 'http://freegeoip.net/csv/$(HOST_$*)' | \
+ awk -F, '{ printf "%s, %s\n", $$8, $$9 }' > $@
+
+####################
+# 'virtual' targets in that there's no real file attached
+define node_shortcuts
+sshcheck.$(1): sshcheck-$(1) FORCE
+db.$(1): cache/db.$(1) FORCE
+switch.$(1): cache/switch.$(1) FORCE
+start.$(1): cache/start.$(1) FORCE
+stop.$(1): cache/stop.$(1) FORCE
+status.$(1): cache/status.$(1) FORCE
+bridge.$(1): cache/bridge.$(1) FORCE
+host.$(1): cache/host.$(1) FORCE
+controller.$(1): cache/controller.$(1) FORCE
+# switch already depends on db, but well
+cache/start.$(1): cache/db.$(1) cache/switch.$(1) FORCE
+endef
+
+$(foreach id,$(ALL_NODE_IDS), $(eval $(call node_shortcuts,$(id))))
+
+define link_shortcuts
+iface.%: cache/iface.%
+endpoint.%: cache/endpoint.%
+endef
+
+$(foreach id,$(ALL_LINK_IDS), $(eval $(call link_shortcuts,$(id))))
+
+help:
+ @cat Makefile.help
+
+#################### convenience, for debugging only
+# make +foo : prints the value of $(foo)
+# make ++foo : idem but verbose, i.e. foo=$(foo)
+++%: varname=$(subst +,,$@)
+++%:
+ @echo "$(varname)=$($(varname))"
++%: varname=$(subst +,,$@)
++%:
+ @echo "$($(varname))"
+
+# external nodes and links
+#
+ALL_LINKS := $(LINKS) $(patsubst L/%,%,$(filter L/%,$(MAKECMDGOALS)))
+EXTERNAL_LINKS := $(filter $(foreach host,$(EXTERNAL_HOSTS),%-$(host)),$(ALL_LINKS))
+
+$(foreach host,$(EXTERNAL_HOSTS),sshcheck-$(host)): ;
+$(foreach host,$(EXTERNAL_HOSTS),ovsversion-$(host)): ;
+$(foreach host,$(EXTERNAL_HOSTS),showdpid-$(host)): ;
+$(foreach host,$(EXTERNAL_HOSTS),showmac-$(host)): ;
+$(foreach host,$(EXTERNAL_HOSTS),showports-$(host)): ;
+$(foreach host,$(EXTERNAL_HOSTS),update-$(host)): ;
+$(foreach host,$(EXTERNAL_HOSTS),del-controller-$(host)): ;
+$(foreach host,$(EXTERNAL_HOSTS),cache/status.$(host)): ;
+$(foreach host,$(EXTERNAL_HOSTS),cache/controller.$(host)): ;
+
+$(foreach host,$(EXTERNAL_HOSTS),cache/rsnap.ip.$(host)): ;
+ @touch $@
+
+$(foreach host,$(EXTERNAL_HOSTS),cache/rsnap.links.$(host)): ;
+ @touch $@
+
+$(foreach host,$(EXTERNAL_HOSTS),cache/db.$(host)):
+ @touch $@
+
+$(foreach host,$(EXTERNAL_HOSTS),cache/switch.$(host)): ;
+ @touch $@