2 # conf.mk is expected to define
3 # HOST_<id> and IP_<id> for all nodes involved, as well as
4 # LINKS as a list of <node_id>-<node_id> elements
6 # run make CONF=anotherconfig.mk if you need several configs
11 # if undefined in the conf file, use single dash
14 # bridge name (XXX the same on all nodes)
20 flip=$(if $(findstring 1,$(1)),2,1)
22 cutsep=$(subst $(SEP), ,$(1))
24 leftnode=$(word 1,$(call cutsep,$(1)))
25 # rightnode (x-y) -> y
26 rightnode=$(word 2,$(call cutsep,$(1)))
28 linkpart=$(word 1,$(subst @, ,$(1)))
30 endpart=$(word 2,$(subst @, ,$(1)))
33 get=$(word $(call endpart,$(1)),$(call cutsep,$(call linkpart,$(1))))
36 opp=$(call linkpart,$(1))@$(call flip,$(call endpart,$(1)))
39 rget=$(call get,$(call opp,$(1)))
41 default=$(if $($(1)_$(2)),$($(1)_$(2)),$($(1)))
44 # can be redefined in conf.mk if that's not the expected behaviour
45 display?=host $(1) aka $(call solve,$(1))
48 log=$(addprefix log/,$(notdir $(1)))
50 #################### set variables after conf.mk
54 SSH_KEY_OPTION ?= -i $(SSH_KEY)
57 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE)
58 SSH = ssh $(SSH_OPTIONS)
62 ALL_NODE_IDS=$(sort $(foreach link,$(LINKS),$(call leftnode,$(link))) $(foreach link,$(LINKS),$(call rightnode,$(link))))
63 ALL_NODE_HOSTS=$(foreach id,$(ALL_NODE_IDS),$(call solve,$(id)))
64 ALL_LINK_IDS=$(addsuffix @1,$(LINKS)) $(addsuffix @2,$(LINKS))
65 CONF_NODE_IDS=$(subst HOST_,,$(filter HOST_%,$(.VARIABLES)))
70 @[ -d L ] || ( echo Creating tmp dir L; mkdir L)
71 @[ -d log ] || (echo Creating tmp dir log; mkdir log)
72 @[ -d cache ] || ( echo Creating tmp dir cache; mkdir cache)
79 LINKTARGETS=$(addprefix L/,$(LINKS))
83 # could also do make ++SLICE
84 showslice: ++SLICE FORCE
87 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
88 showhostnames: ++ALL_NODE_HOSTS
90 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id)) has ip/network set to $(IP_$(id));)
92 @$(foreach link,$(LINKS), echo $(call display,$(call leftnode,$(link))) '====>' $(call display,$(call rightnode,$(link)));)
94 @$(foreach id,$(ALL_NODE_IDS), echo $(call display,$(id)) has controller $(call default,CONTROLLER,$(id));)
95 .PHONY: shownodes showhostnames showips showlinks showcontrollers
97 sshchecks: $(foreach id,$(ALL_NODE_IDS),sshcheck-$(id))
99 ovsversions: $(foreach id,$(ALL_NODE_IDS),ovsversion-$(id))
101 # more general form; run with make sshs SSH-COMMAND="bla bla bla"
102 sshs: $(foreach id,$(ALL_NODE_IDS),ssh-$(id))
104 showdpids: $(foreach id,$(ALL_NODE_IDS),showdpid-$(id))
107 showports: $(foreach id,$(ALL_NODE_IDS),showports-$(id))
110 DBS=$(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
114 SWITCHS=$(foreach id,$(ALL_NODE_IDS),cache/switch.$(id))
121 stop:$(foreach id,$(ALL_NODE_IDS),cache/stop.$(id))
124 status:$(foreach id,$(ALL_NODE_IDS),cache/status.$(id))
127 BRIDGES=$(foreach id,$(ALL_NODE_IDS),cache/bridge.$(id))
131 CONTROLLERS=$(foreach id,$(ALL_NODE_IDS),cache/controller.$(id))
132 controllers: $(CONTROLLERS)
135 del-controllers: $(foreach id,$(ALL_NODE_IDS),del-controller-$(id))
136 .PHONY: del-controllers
138 ### node-oriented targets
139 # check ssh connectivity
141 @if $(SSH) $(HOST_$*) hostname &> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
142 else echo "ssh on" $(call display,$*) "KO !!!"; fi
145 @OVSVERSION=$$($(SSH) $(HOST_$*) rpm -q sliver-openvswitch); echo $(call display,$*) has $$OVSVERSION
148 @$(SSH) $(HOST_$*) $(SUDO) $(SSH-COMMAND)
151 @echo $*_DPID=0x$$( $(SSH) $(HOST_$*) $(SUDO) ovs-vsctl get bridge $(BRIDGE) datapath_id | sed -e 's,",,g')
154 @$(SSH) $(HOST_$*) $(SUDO) ovs-ofctl show $(BRIDGE) \
155 | perl -nle '/(\d+)\(L(.*?)\Q$(SEP)\E(.*?)\):/ && \
156 print "PORT_".("$$2" eq "$*" ? "$$2_$$3" : "$$3_$$2")."=$$1"'
158 # should probably replace sshcheck
159 cache/status.%: FORCE
160 @echo "=== DB and SWITCH processes on $(call display,$*)"
161 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs status ||:
164 @echo "IP lookup for $(call display,$*)"
165 @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
168 @echo "Starting db server on $(call display,$*) - logs in $(call log,$@)"
169 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-db &> $(call log,$@) && touch $@
171 cache/switch.%: | cache/db.%
172 @echo "Starting vswitchd on $(call display,$*) - logs in $(call log,$@)"
173 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-switch &> $(call log,$@) && touch $@
175 cache/bridge.%: | cache/db.%
176 @echo "Creating bridge on $(call display,$*) - logs in $(call log,$@)"
177 @$(SSH) $(HOST_$*) $(SUDO) \
178 sliver-ovs create-bridge $(BRIDGE) $(IP_$*) $(call default,BROPTIONS,$*) &> $(call log,$@) \
179 && { echo "IP_$*=$(IP_$*)"; echo "BROPTIONS_$*=$(call default,BROPTIONS,$*)"; } > $@
181 cache/controller.%: cache/bridge.%
182 @echo "Setting controller $(call default,CONTROLLER,$*) on $(call display,$*) - logs in $(call log,$@)"
183 @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl set-controller $(BRIDGE) $(call default,CONTROLLER,$*) &> $(call log,$@) \
184 && echo "CONTROLLER_$*=$(call default,CONTROLLER,$*)" > $@
186 # xxx this probably needs a more thorough cleanup in cache/
187 cache/stop.%: del-bridge.%
188 @echo "Stopping switch & db on $(call display,$*)"
189 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop && rm cache/switch.$* cache/db.$*
191 ### link-oriented targets
192 # L/<nodeid1>-<node_id2>:
193 # Establish a link between nodes <node_id1> and <node_id2>
194 L/%: cache/endpoint.%@1 cache/endpoint.%@2
196 @echo "Link $* is up"
198 # U/<node_id1>-<node_id2>
199 # Tear down the link between nodes <node_id1> and <node_id2>
200 U/%: del-iface.%@1 del-iface.%@2
202 @echo "Deleted link $*"
204 # del-bridge.<node_id>: Delete the bridge on node <node_id>.
206 # We can do this only if the db on node <node_id> is running, but
207 # we don't need to re-delete if the db is restarted (hence the
208 # order-only dependency).
210 # Deleting a bridge also deletes all interfaces of the bridge
211 # as a side effect. This in turn invalidates local tunnel
212 # port numbers and endpoint info on both sides of each affected tunnel.
213 # The corresponding links obviously go down.
214 # Controller information is also lost.
215 # We invalidate the cache accordingly.
216 del-bridge.%: | cache/db.%
217 @echo "Deleting bridge on $(call display,$*)"
218 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-bridge $(BRIDGE);
219 @rm -f cache/bridge.$* \
220 cache/iface.$*$(SEP)*@1 cache/iface.*$(SEP)$*@2 \
221 cache/port.$*$(SEP)*@1 cache/port.*$(SEP)$*@2 \
222 cache/endpoint.$*$(SEP)*@? cache/endpoint.*$(SEP)$*@? \
223 L/$*$(SEP)* L/*$(SEP)$* \
226 # del-switch.<node_id>: Stops the switch daemon on <node_id>.
228 # As a side effect, the local port numbers of the tunnels
229 # are no longer valid (they will change when the daemon is restarted)
230 # and, therefore, the endpoint info on the remote side of
231 # the tunnels must be invalidated. The links also go down.
232 # Controller information is also lost.
233 # We invalidate the cache accordingly.
235 @echo "Shutting down switch on $(call display,$*)"
236 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-switch
237 @rm -f cache/switch.$* \
238 cache/port.$*$(SEP)*@1 cache/port.*$(SEP)$*@2 \
239 cache/endpoint.$*$(SEP)*@2 cache/endpoint.*$(SEP)$*@1 \
240 L/$*$(SEP)* L/*$(SEP)$* \
243 # del-db.<node_id>: Stops the db daemon on <node_id>.
245 # This has no additional side effects.
247 @echo "Shutting down db on $(call display,$*)"
248 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-db
251 # del-controller.<node_id>:
252 # Detaches <node_id> from the controller.
254 # This has no additional side effects.
255 del-controller-%: | cache/db.%
256 @echo "Removing controller for $(call display,$*)"
257 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-controller $(BRIDGE)
258 @rm -f cache/controller.$*
260 # del-links: Shortcut to delete all currently up links.
261 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
263 # del-switchs: Shortcut to stop all currently running switch daemons.
264 del-switchs: $(addprefix del-,$(notdir $(wildcard cache/switch.*)))
266 # del-dbs: Shortcut to stop all currently running db daemona.s
267 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
269 # shutdown: Shortcut to stop all currently running daemons.
270 shutdown: del-switchs del-dbs
272 .PHONY: del-links del-switchs del-dbs shutdown
277 @echo BRIDGE=$(BRIDGE)
278 @cat cache/bridge.* 2>/dev/null || :
279 @cat cache/controller.* 2>/dev/null || :
280 @ls L | sed 's/^/LINKS += /'
282 remote-snapshot: remote-snapshot-ips remote-snapshot-links
285 remote-snapshot-ips: $(addprefix cache/rsnap.ip.,$(CONF_NODE_IDS))
288 remote-snapshot-links: $(addprefix cache/rsnap.links.,$(CONF_NODE_IDS))
289 @sort -u /dev/null $^
291 .PHONY: remote-snapshot remote-snapshot-ips remote-snapshot-links
294 cache/rsnap.ip.%: FORCE
295 @$(SSH) $(HOST_$*) $(SUDO) \
296 sliver-ovs get-local-ip $(BRIDGE) | sed 's/^/IP_$*=/' > $@ \
297 || { rm $@; exit 1; }
299 cache/rsnap.links.%: FORCE
300 @$(SSH) $(HOST_$*) $(SUDO) \
301 sliver-ovs get-local-links $(BRIDGE) | sed -n 's/^L/LINKS += /p' > $@ \
302 || { rm $@; exit 1; }
304 ### update sliver-ovs
305 update: $(addprefix update-,$(CONF_NODE_IDS))
309 @[ -n "$(SLIVER_OVS)" ] || { echo "SLIVER_OVS not set" >&2; exit 1; }
310 @[ -f "$(SLIVER_OVS)" ] || { echo "$(SLIVER_OVS) not found" >&2; exit 1; }
311 @echo "Sending $(SLIVER_OVS) to $(call display,$*)"
312 @scp -q $(SLIVER_OVS) $(SLICE)@$(call solve,$*):
313 @$(SSH) $(call solve,$*) $(SUDO) mv $(notdir $(SLIVER_OVS)) /usr/sbin/sliver-ovs
317 # del-iface.<node_id>-<node_id>@<endpoint>:
318 # Deletes the interface of link <node_id1>-<node_id2> on either
319 # <node_id1> or <node_id2>, according to <endpoint>.
321 # We need a running db daemon to do this, but we do not have to
322 # redo the delete if the db daemon is restarted.
324 # This also invalidates the local port of the tunnel and the endpoint
325 # info on both sides of the tunnel. The link goes down.
326 del-iface.%: | cache/db.$$(call get,%)
327 @echo "Removing interface for link $(call linkpart,$*) from $(call get,$*)"
328 @$(SSH) $(HOST_$(call get,$*)) \
329 $(SUDO) sliver-ovs del-port L$(call linkpart,$*)
330 @rm -f cache/iface.$* \
331 cache/port.$* cache/endpoint.$* cache/endpoint.$(call opp,$*) \
332 L/$(call linkpart,$*)
335 ### '%' here is leftid-rightid@{1,2}
336 # we retrieve % as $(*F)
337 #linkid=$(call linkpart,%)
338 #nodeid=$(call get,%)
339 #bridgefile=cache/bridge.$(nodeid)
341 # cache/iface.<node_id1>-<node_id2>@<endpoint>:
342 # Creates the interface for link <node_id1>-<node_id2> on
343 # <node_id<endpoint>>.
345 # The bridge of the local node must already exist, and we need to create
346 # the interface again if the bridge is re-created.
347 # We also need a running db daemon, but we do not need to do anything
348 # if the db daemon is restarted.
349 cache/iface.%: cache/bridge.$$(call get,%) | cache/db.$$(call get,%)
350 @echo "Creating interface for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
351 @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs create-port $(BRIDGE) \
352 L$(call linkpart,$(*F)) &> $(call log,$@) \
355 # cache/port.<node_id1>-<node_id2>@<endpoint>:
356 # Retrieves the local port of link <node_id1>-<node_id2> on
357 # node <node_id<endpoint>>.
359 # The local interface must have been created already and the
360 # switch daemon must be running. We need to retrieve the port
361 # number again if the interface is re-created, or the switch
362 # daemon is restarted.
363 cache/port.%: cache/iface.% cache/switch.$$(call get,%)
364 @echo "Getting port number for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
365 @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) \
366 sliver-ovs get-local-endpoint L$(call linkpart,$(*F)) > $@ 2> $(call log,$@) \
367 || { rm $@; exit 1; }
370 # linkid=$(call linkpart,%)
371 # nodeid=$(call get,%)
372 # iface1=cache/iface.%
373 # iface2=cache/iface.$(call opp,%)
375 # cache/endpoint.<node_id1>-<node_id2>@<endpoint>:
376 # Sets the other side (IP address, UDP port) info for link <node_id1>-<node_id2>
377 # on <node_id<endpoint>>.
379 # We need the IP address and the UDP port of the other side and the interace of this side.
380 # We need to set the info again if any of these change.
381 cache/endpoint.%: cache/host.$$(call rget,%) cache/port.$$(call opp,%) cache/iface.%
382 @echo "Setting port number of link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
383 @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs set-remote-endpoint L$(call linkpart,$(*F)) \
384 $$(cat cache/host.$(call rget,$(*F))) \
385 $$(cat cache/port.$(call opp,$(*F))) 2> $(call log,$@) \
389 CLEANTARGETS=$(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
390 clean: $(CLEANTARGETS)
393 .PHONY: clean distclean
397 ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
404 # GMAP_SERVER, the hostname that runs an ndnmap instance
405 # GMAP_PROBES, a list of tuples of the form
406 # <linkid>=<nodeid>=<internal_linkid>=<frequency>
408 # SENDER-MUX=SENDER=3=0.5
409 # which would mean, the link 'SENDER-MUX' should be monitored at the 'SENDER' node and is coded
410 # in links.json with id=3, every half second
411 # sliver-ovs gmap-probe is designed to run in background,
412 # and so that a new run will kill any previously running instance
413 gprobes: $(foreach probe,$(GMAP_PROBES),gprobe-$(probe))
416 $(SSH) -n $(call solve,$(word 2,$(subst @, ,$(*F)))) $(SUDO) \
417 sliver-ovs gmap-probe L$(word 1,$(subst @, ,$(*F))) $(GMAP_SERVER) $(word 3,$(subst @, ,$(*F))) $(word 4,$(subst @, ,$(*F)))
419 jsons: routers.json links.json geocode.json
422 routers.json: $(CONF)
425 for n in $(CONF_NODE_IDS); do \
426 echo " $$sep\"$$n\""; \
432 links.json: $(foreach l,$(LINKS),cache/json.$(l))
434 cat $^ | sed '2,$$s/^/, /'; \
438 cache/json.%: $(CONF)
439 @echo "{\"id\": $($*_linkid), \"start\": \"$(call get,$*@1)\", \"end\": \"$(call get,$*@2)\"}" > $@
441 geocode.json: $(foreach n,$(CONF_NODE_IDS),cache/geocode.$(n))
452 cache/geocode.%: cache/loc.% $(CONF)
453 @{ echo "\"$*\": {"; \
454 echo " \"name\": \"$(HOST_$*)\","; \
455 echo " \"shortname\": \"$*\","; \
456 echo " \"site\": \"\","; \
457 echo " \"backbone\": false,"; \
458 echo " \"position\": [ $(shell cat cache/loc.$*) ]"; \
463 @wget -O - 'http://freegeoip.net/csv/$(HOST_$*)' | \
464 awk -F, '{ printf "%s, %s\n", $$8, $$9 }' > $@
467 # 'virtual' targets in that there's no real file attached
468 define node_shortcuts
469 sshcheck.$(1): sshcheck-$(1) FORCE
470 db.$(1): cache/db.$(1) FORCE
471 switch.$(1): cache/switch.$(1) FORCE
472 start.$(1): cache/start.$(1) FORCE
473 stop.$(1): cache/stop.$(1) FORCE
474 status.$(1): cache/status.$(1) FORCE
475 bridge.$(1): cache/bridge.$(1) FORCE
476 host.$(1): cache/host.$(1) FORCE
477 controller.$(1): cache/controller.$(1) FORCE
478 # switch already depends on db, but well
479 cache/start.$(1): cache/db.$(1) cache/switch.$(1) FORCE
482 $(foreach id,$(ALL_NODE_IDS), $(eval $(call node_shortcuts,$(id))))
484 define link_shortcuts
485 iface.%: cache/iface.%
486 endpoint.%: cache/endpoint.%
489 $(foreach id,$(ALL_LINK_IDS), $(eval $(call link_shortcuts,$(id))))
494 #################### convenience, for debugging only
495 # make +foo : prints the value of $(foo)
496 # make ++foo : idem but verbose, i.e. foo=$(foo)
497 ++%: varname=$(subst +,,$@)
499 @echo "$(varname)=$($(varname))"
500 +%: varname=$(subst +,,$@)
502 @echo "$($(varname))"