invalidate controllers on del-{bridge,switch}
[sliver-openvswitch.git] / planetlab / exp-tool / Makefile
1 # see README
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
5
6 # run make CONF=anotherconfig.mk if you need several configs
7
8 CONF ?= conf.mk
9 include $(CONF)
10
11 # if undefined in the conf file, use single dash
12 SEP?=-
13
14 # bridge name (XXX the same on all nodes)
15 BRIDGE?=$(SLICE)
16
17 ### helper functions
18 # flip(1) = 2
19 # flip(2) = 1
20 flip=$(if $(findstring 1,$(1)),2,1)
21 # cutsep (x-y)-> x y
22 cutsep=$(subst $(SEP), ,$(1))
23 # leftnode (x-y) -> x
24 leftnode=$(word 1,$(call cutsep,$(1)))
25 # rightnode (x-y) -> y
26 rightnode=$(word 2,$(call cutsep,$(1)))
27 # linkpart(x@y) = x
28 linkpart=$(word 1,$(subst @, ,$(1)))
29 # endpart(x@y) = y
30 endpart=$(word 2,$(subst @, ,$(1)))
31 # get(x-y@1) = x
32 # get(x-y@2) = y
33 get=$(word $(call endpart,$(1)),$(call cutsep,$(call linkpart,$(1))))
34 # opp(x-y@1) = x-y@2
35 # opp(x-y@2) = x-y@1
36 opp=$(call linkpart,$(1))@$(call flip,$(call endpart,$(1)))
37 # rget(x-y@1) = y
38 # rget(x-y@2) = x
39 rget=$(call get,$(call opp,$(1)))
40 ###
41 default=$(if $($(1)_$(2)),$($(1)_$(2)),$($(1)))
42 solve=$(HOST_$(1))
43 solve_ip=$(IP_$(1))
44 # can be redefined in conf.mk if that's not the expected behaviour
45 display?=host $(1) aka $(call solve,$(1))
46
47 # log file name
48 log=$(addprefix log/,$(notdir $(1)))
49
50 #################### set variables after conf.mk
51 ifeq "$(SSH_KEY)" ""
52 SSH_KEY_OPTION ?=
53 else
54 SSH_KEY_OPTION ?= -i $(SSH_KEY)
55 endif
56
57 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE)
58 SSH = ssh $(SSH_OPTIONS)
59
60 SUDO ?= sudo -S
61
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)))
66
67 ####################
68 init_all: init all
69 init:
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)
73 .PHONY: init_all init
74
75 FORCE:
76
77 .SECONDARY:
78
79 LINKTARGETS=$(addprefix L/,$(LINKS))
80 all: $(LINKTARGETS)
81 .PHONY: all
82
83 # could also do make ++SLICE
84 showslice: ++SLICE FORCE
85
86 shownodes:
87         @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
88 showhostnames: ++ALL_NODE_HOSTS
89 showips:
90         @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id)) has ip/network set to $(IP_$(id));)
91 showlinks:
92         @$(foreach link,$(LINKS), echo $(call display,$(call leftnode,$(link))) '====>' $(call display,$(call rightnode,$(link)));)
93 showcontrollers:
94         @$(foreach id,$(ALL_NODE_IDS), echo $(call display,$(id)) has controller $(call default,CONTROLLER,$(id));)
95 .PHONY: shownodes showhostnames showips showlinks showcontrollers
96
97 sshchecks: $(foreach id,$(ALL_NODE_IDS),sshcheck-$(id))
98 .PHONY: sshchecks
99 ovsversions: $(foreach id,$(ALL_NODE_IDS),ovsversion-$(id))
100 .PHONY: ovsversions
101 # more general form; run with  make sshs SSH-COMMAND="bla bla bla"
102 sshs: $(foreach id,$(ALL_NODE_IDS),ssh-$(id))
103 .PHONY: sshs
104
105 DBS=$(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
106 dbs: $(DBS)
107 .PHONY: dbs
108
109 SWITCHS=$(foreach id,$(ALL_NODE_IDS),cache/switch.$(id))
110 switchs: $(SWITCHS)
111 .PHONY: switchs
112
113 start: dbs switchs
114 .PHONY: start
115
116 stop:$(foreach id,$(ALL_NODE_IDS),cache/stop.$(id))
117 .PHONY: stop
118
119 status:$(foreach id,$(ALL_NODE_IDS),cache/status.$(id))
120 .PHONY: status
121
122 BRIDGES=$(foreach id,$(ALL_NODE_IDS),cache/bridge.$(id))
123 bridges: $(BRIDGES)
124 .PHONY: bridges
125
126 CONTROLLERS=$(foreach id,$(ALL_NODE_IDS),cache/controller.$(id))
127 controllers: $(CONTROLLERS)
128 .PHONY: controllers
129
130 del-controllers: $(foreach id,$(ALL_NODE_IDS),del-controller-$(id))
131 .PHONY: del-controllers
132
133 ### node-oriented targets
134 # check ssh connectivity
135 sshcheck-%: FORCE
136         @if $(SSH) $(HOST_$*) hostname &> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
137          else echo "ssh on" $(call display,$*) "KO !!!"; fi
138
139 ovsversion-%: FORCE
140         @OVSVERSION=$$($(SSH) $(HOST_$*) rpm -q sliver-openvswitch); echo $(call display,$*) has $$OVSVERSION
141
142 ssh-%: FORCE
143         @$(SSH) $(HOST_$*) $(SUDO) $(SSH-COMMAND)
144
145 # should probably replace sshcheck
146 cache/status.%: FORCE
147         @echo "=== DB and SWITCH processes on $(call display,$*)"
148         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs status ||:
149
150 cache/host.%:
151         @echo "IP lookup for $(call display,$*)"
152         @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
153
154 cache/db.%:
155         @echo "Starting db server on $(call display,$*) - logs in $(call log,$@)"
156         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-db &> $(call log,$@) && touch $@
157
158 cache/switch.%: | cache/db.%
159         @echo "Starting vswitchd on $(call display,$*) - logs in $(call log,$@)"
160         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-switch &> $(call log,$@) && touch $@
161
162 cache/bridge.%: | cache/db.%
163         @echo "Creating bridge on $(call display,$*) - logs in $(call log,$@)"
164         @$(SSH) $(HOST_$*) $(SUDO) \
165                 sliver-ovs create-bridge $(BRIDGE) $(IP_$*) $(call default,BROPTIONS,$*) &> $(call log,$@) \
166          && touch $@
167
168 cache/controller.%: cache/bridge.%
169         @echo "Setting controller $(call default,CONTROLLER,$*) on $(call display,$*) - logs in $(call log,$@)"
170         @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl set-controller $(BRIDGE) $(call default,CONTROLLER,$*) &> $(call log,$@) \
171          && touch $@
172
173 # xxx this probably needs a more thorough cleanup in cache/
174 cache/stop.%: del-bridge.%
175         @echo "Stopping switch & db on $(call display,$*)"
176         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop && rm cache/switch.$* cache/db.$*
177
178 ### link-oriented targets
179 # L/<nodeid1>-<node_id2>:
180 #       Establish a link between nodes <node_id1> and <node_id2>
181 L/%: cache/endpoint.%@1 cache/endpoint.%@2
182         @touch $@
183         @echo "Link $* is up"
184
185 # U/<node_id1>-<node_id2>
186 #       Tear down the link between nodes <node_id1> and <node_id2>
187 U/%: del-iface.%@1 del-iface.%@2
188         @rm -f L/$*
189         @echo "Deleted link $*"
190
191 # del-bridge.<node_id>: Delete the bridge on node <node_id>.
192 #
193 # We can do this only if the db on node <node_id> is running, but
194 # we don't need to re-delete if the db is restarted (hence the
195 # order-only dependency).
196 #
197 # Deleting a bridge also deletes all interfaces of the bridge
198 # as a side effect. This in turn invalidates local tunnel
199 # port numbers and endpoint info on both sides of each affected tunnel.
200 # The corresponding links obviously go down. 
201 # Controller information is also lost.
202 # We invalidate the cache accordingly.
203 del-bridge.%: | cache/db.%
204         @echo "Deleting bridge on $(call display,$*)"
205         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-bridge $(BRIDGE);
206         @rm -f cache/bridge.$* \
207               cache/iface.$*$(SEP)*@1 cache/iface.*$(SEP)$*@2 \
208               cache/port.$*$(SEP)*@1  cache/port.*$(SEP)$*@2  \
209               cache/endpoint.$*$(SEP)*@?  cache/endpoint.*$(SEP)$*@?  \
210               L/$*$(SEP)*             L/*$(SEP)$* \
211               cache/controller.$*
212
213 # del-switch.<node_id>: Stops the switch daemon on <node_id>.
214 #
215 # As a side effect, the local port numbers of the tunnels
216 # are no longer valid (they will change when the daemon is restarted)
217 # and, therefore, the endpoint info on the remote side of
218 # the tunnels must be invalidated. The links also go down.
219 # Controller information is also lost.
220 # We invalidate the cache accordingly.
221 del-switch.%:
222         @echo "Shutting down switch on $(call display,$*)"
223         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-switch
224         @rm -f cache/switch.$* \
225                cache/port.$*$(SEP)*@1  cache/port.*$(SEP)$*@2  \
226                cache/endpoint.$*$(SEP)*@2  cache/endpoint.*$(SEP)$*@1  \
227                L/$*$(SEP)*            L/*$(SEP)$* \
228                cache/controller.$*
229
230 # del-db.<node_id>: Stops the db daemon on <node_id>.
231 #
232 # This has no additional side effects.
233 del-db.%:
234         @echo "Shutting down db on $(call display,$*)"
235         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-db
236         @rm -f cache/db.$*
237
238 # del-controller.<node_id>:
239 #       Detaches <node_id> from the controller.
240 #
241 # This has no additional side effects.
242 del-controller-%: | cache/db.%
243         @echo "Removing controller for $(call display,$*)"
244         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-controller $(BRIDGE)
245         @rm -f cache/controller.$*
246
247 # del-links: Shortcut to delete all currently up links.
248 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
249
250 # del-switchs: Shortcut to stop all currently running switch daemons.
251 del-switchs: $(addprefix del-,$(notdir $(wildcard cache/switch.*)))
252
253 # del-dbs: Shortcut to stop all currently running db daemona.s
254 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
255
256 # shutdown: Shortcut to stop all currently running daemons.
257 shutdown: del-switchs del-dbs
258
259 .PHONY: del-links del-switchs del-dbs shutdown
260
261
262 ### snapshots
263 remote-snapshot: remote-snapshot-ips remote-snapshot-links
264         @true
265
266 remote-snapshot-ips: $(addprefix cache/rsnap.ip.,$(CONF_NODE_IDS))
267         @cat /dev/null $^
268
269 remote-snapshot-links: $(addprefix cache/rsnap.links.,$(CONF_NODE_IDS))
270         @sort -u /dev/null $^
271
272 ### update sliver-ovs
273 update: $(addprefix update-,$(CONF_NODE_IDS))
274         @true
275
276 update-%: FORCE
277         @[ -n "$(SLIVER_OVS)" ] || { echo "SLIVER_OVS not set" >&2; exit 1; }
278         @[ -f "$(SLIVER_OVS)" ] || { echo "$(SLIVER_OVS) not found" >&2; exit 1; }
279         @echo "Sending $(SLIVER_OVS) to $(call display,$*)"
280         @scp -q $(SLIVER_OVS) $(SLICE)@$(call solve,$*):
281         @$(SSH) $(call solve,$*) $(SUDO) mv $(notdir $(SLIVER_OVS)) /usr/sbin/sliver-ovs
282
283 .PHONY: remote-snapshot remote-snapshot-ips remote-snapshot-links
284
285 cache/rsnap.ip.%: 
286         @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl -- \
287                 get interface $(BRIDGE) options:local_ip options:local_netmask |\
288                 sed -n 's|"||g;1h;2{G;s|\(.*\)\n\(.*\)|IP_$* = \2/\1|p}' > $@ || \
289          { rm $@; exit 1; }
290
291 cache/rsnap.links.%: 
292         @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl list-ifaces $(BRIDGE) | \
293          sed -n 's/^L/LINKS += /p' > $@ || \
294          { rm $@; exit 1; }
295
296 .SECONDEXPANSION:
297
298 # del-iface.<node_id>-<node_id>@<endpoint>:
299 #       Deletes the interface of link <node_id1>-<node_id2> on either
300 #       <node_id1> or <node_id2>, according to <endpoint>.
301
302 # We need a running db daemon to do this, but we do not have to
303 # redo the delete if the db daemon is restarted.
304 #
305 # This also invalidates the local port of the tunnel and the endpoint
306 # info on both sides of the tunnel. The link goes down.
307 del-iface.%: | cache/db.$$(call get,%)
308         @echo "Removing interface for link $(call linkpart,$*) from $(call get,$*)"
309         @$(SSH) $(HOST_$(call get,$*)) \
310                 $(SUDO) sliver-ovs del-port L$(call linkpart,$*)
311         @rm -f cache/iface.$* \
312                cache/port.$* cache/endpoint.$* cache/endpoint.$(call opp,$*) \
313                L/$(call linkpart,$*)
314
315
316 ### '%' here is leftid-rightid@{1,2}
317 # we retrieve % as $(*F)
318 #linkid=$(call linkpart,%)
319 #nodeid=$(call get,%)
320 #bridgefile=cache/bridge.$(nodeid)
321
322 # cache/iface.<node_id1>-<node_id2>@<endpoint>:
323 #       Creates the interface for link <node_id1>-<node_id2> on
324 #       <node_id<endpoint>>. 
325 #
326 # The bridge of the local node must already exist, and we need to create
327 # the interface again if the bridge is re-created.
328 # We also need a running db daemon, but we do not need to do anything
329 # if the db daemon is restarted.
330 cache/iface.%: cache/bridge.$$(call get,%) | cache/db.$$(call get,%)
331         @echo "Creating interface for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
332         @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs create-port $(BRIDGE) \
333                 L$(call linkpart,$(*F)) &> $(call log,$@) \
334          && touch $@
335
336 # cache/port.<node_id1>-<node_id2>@<endpoint>:
337 #       Retrieves the local port of link <node_id1>-<node_id2> on
338 #       node <node_id<endpoint>>.
339 #
340 # The local interface must have been created already and the
341 # switch daemon must be running. We need to retrieve the port
342 # number again if the interface is re-created, or the switch
343 # daemon is restarted.
344 cache/port.%: cache/iface.% cache/switch.$$(call get,%)
345         @echo "Getting port number for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
346         @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) \
347                 ovs-appctl netdev-tunnel/get-port L$(call linkpart,$(*F)) > $@ 2> $(call log,$@) \
348          || { rm $@; exit 1; }
349
350
351 # linkid=$(call linkpart,%)
352 # nodeid=$(call get,%)
353 # iface1=cache/iface.%
354 # iface2=cache/iface.$(call opp,%)
355
356 # cache/endpoint.<node_id1>-<node_id2>@<endpoint>:
357 #       Sets the other side (IP address, UDP port) info for link <node_id1>-<node_id2>
358 #       on <node_id<endpoint>>.
359 #
360 # We need the IP address and the UDP port of the other side and the interace of this side.
361 # We need to set the info again if any of these change.
362 cache/endpoint.%: cache/host.$$(call rget,%) cache/port.$$(call opp,%) cache/iface.%
363         @echo "Setting port number of link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
364         @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs set-remote-endpoint L$(call linkpart,$(*F)) \
365                         $$(cat cache/host.$(call rget,$(*F))) \
366                         $$(cat cache/port.$(call opp,$(*F))) 2> $(call log,$@) \
367          && touch $@
368
369 ####################
370 CLEANTARGETS=$(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
371 clean: $(CLEANTARGETS)
372 distclean:
373         rm -rf L log cache
374 .PHONY: clean distclean
375
376 ####################
377 graph.dot:
378         ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
379 graph.ps: graph.dot
380         dot -Tps < $^ > $@      
381
382 ####################
383 # googlemap stuff
384 # Uses:
385 # GMAP_SERVER, the hostname that runs an ndnmap instance
386 # GMAP_PROBES, a list of tuples of the form
387 # <linkid>=<nodeid>=<internal_linkid>=<frequency>
388 # e.g.
389 # SENDER-MUX=SENDER=3=0.5
390 # which would mean, the link 'SENDER-MUX' should be monitored at the 'SENDER' node and is coded 
391 # in links.json with id=3, every half second
392 # sliver-ovs gmap-probe is designed to run in background,
393 # and so that a new run will kill any previously running instance
394 gprobes: $(foreach probe,$(GMAP_PROBES),gprobe-$(probe))
395
396 gprobe-%: FORCE
397         $(SSH) -n $(call solve,$(word 2,$(subst @, ,$(*F)))) $(SUDO) \
398         sliver-ovs gmap-probe L$(word 1,$(subst @, ,$(*F))) $(GMAP_SERVER) $(word 3,$(subst @, ,$(*F))) $(word 4,$(subst @, ,$(*F)))
399
400 ####################
401 # 'virtual' targets in that there's no real file attached
402 define node_shortcuts
403 sshcheck.$(1): sshcheck-$(1) FORCE
404 db.$(1): cache/db.$(1) FORCE
405 switch.$(1): cache/switch.$(1) FORCE
406 start.$(1): cache/start.$(1) FORCE
407 stop.$(1): cache/stop.$(1) FORCE
408 status.$(1): cache/status.$(1) FORCE
409 bridge.$(1): cache/bridge.$(1) FORCE
410 host.$(1): cache/host.$(1) FORCE
411 controller.$(1): cache/controller.$(1) FORCE
412 # switch already depends on db, but well
413 cache/start.$(1): cache/db.$(1) cache/switch.$(1) FORCE
414 endef
415
416 $(foreach id,$(ALL_NODE_IDS), $(eval $(call node_shortcuts,$(id))))
417
418 define link_shortcuts
419 iface.%: cache/iface.%
420 endpoint.%: cache/endpoint.%
421 endef
422
423 $(foreach id,$(ALL_LINK_IDS), $(eval $(call link_shortcuts,$(id))))
424
425 help:
426         @cat Makefile.help
427
428 #################### convenience, for debugging only
429 # make +foo : prints the value of $(foo)
430 # make ++foo : idem but verbose, i.e. foo=$(foo)
431 ++%: varname=$(subst +,,$@)
432 ++%:
433         @echo "$(varname)=$($(varname))"
434 +%: varname=$(subst +,,$@)
435 +%:
436         @echo "$($(varname))"