make showmacs - for now this is very patchy
[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 showdpids: $(foreach id,$(ALL_NODE_IDS),showdpid-$(id))
105 .PHONY: showdpids
106 showmacs: $(foreach id,$(ALL_NODE_IDS),showmac-$(id))
107 .PHONY: showmacs
108 showports: $(foreach id,$(ALL_NODE_IDS),showports-$(id))
109 .PHONY: showports
110
111 DBS=$(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
112 dbs: $(DBS)
113 .PHONY: dbs
114
115 SWITCHS=$(foreach id,$(ALL_NODE_IDS),cache/switch.$(id))
116 switchs: $(SWITCHS)
117 .PHONY: switchs
118
119 start: dbs switchs
120 .PHONY: start
121
122 stop:$(foreach id,$(ALL_NODE_IDS),cache/stop.$(id))
123 .PHONY: stop
124
125 status:$(foreach id,$(ALL_NODE_IDS),cache/status.$(id))
126 .PHONY: status
127
128 BRIDGES=$(foreach id,$(ALL_NODE_IDS),cache/bridge.$(id))
129 bridges: $(BRIDGES)
130 .PHONY: bridges
131
132 CONTROLLERS=$(foreach id,$(ALL_NODE_IDS),cache/controller.$(id))
133 controllers: $(CONTROLLERS)
134 .PHONY: controllers
135
136 del-controllers: $(foreach id,$(ALL_NODE_IDS),del-controller-$(id))
137 .PHONY: del-controllers
138
139 ### node-oriented targets
140 # check ssh connectivity
141 sshcheck-%: FORCE
142         @if $(SSH) $(HOST_$*) hostname &> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
143          else echo "ssh on" $(call display,$*) "KO !!!"; fi
144
145 ovsversion-%: FORCE
146         @OVSVERSION=$$($(SSH) $(HOST_$*) rpm -q sliver-openvswitch); echo $(call display,$*) has $$OVSVERSION
147
148 ssh-%: FORCE
149         @$(SSH) $(HOST_$*) $(SUDO) $(SSH-COMMAND)
150
151 showdpid-%: FORCE
152         @echo $*_DPID=0x$$( $(SSH) $(HOST_$*) $(SUDO) ovs-vsctl get bridge $(BRIDGE) datapath_id | sed -e 's,",,g')
153
154 showmac-%: FORCE
155         @echo $*_MAC=$$( $(SSH) $(HOST_$*) $(SUDO) sliver-ovs show-mac)
156
157 showports-%: FORCE
158         @$(SSH) $(HOST_$*) $(SUDO) ovs-ofctl show $(BRIDGE) \
159                 | perl -nle '/(\d+)\(L(.*?)\Q$(SEP)\E(.*?)\):/ && \
160                         print "PORT_".("$$2" eq "$*" ? "$$2_$$3" : "$$3_$$2")."=$$1"'
161 # should probably replace sshcheck
162 cache/status.%: FORCE
163         @echo "=== DB and SWITCH processes on $(call display,$*)"
164         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs status ||:
165
166 cache/host.%:
167         @echo "IP lookup for $(call display,$*)"
168         @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
169
170 cache/db.%:
171         @echo "Starting db server on $(call display,$*) - logs in $(call log,$@)"
172         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-db &> $(call log,$@) && touch $@
173
174 cache/switch.%: | cache/db.%
175         @echo "Starting vswitchd on $(call display,$*) - logs in $(call log,$@)"
176         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-switch &> $(call log,$@) && touch $@
177
178 cache/bridge.%: | cache/db.%
179         @echo "Creating bridge on $(call display,$*) - logs in $(call log,$@)"
180         @$(SSH) $(HOST_$*) $(SUDO) \
181                 sliver-ovs create-bridge $(BRIDGE) $(IP_$*) $(call default,BROPTIONS,$*) &> $(call log,$@) \
182          && { echo "IP_$*=$(IP_$*)"; echo "BROPTIONS_$*=$(call default,BROPTIONS,$*)"; } > $@
183
184 cache/controller.%: cache/bridge.%
185         @echo "Setting controller $(call default,CONTROLLER,$*) on $(call display,$*) - logs in $(call log,$@)"
186         @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl set-controller $(BRIDGE) $(call default,CONTROLLER,$*) &> $(call log,$@) \
187          && echo "CONTROLLER_$*=$(call default,CONTROLLER,$*)" > $@
188
189 # xxx this probably needs a more thorough cleanup in cache/
190 cache/stop.%: del-bridge.%
191         @echo "Stopping switch & db on $(call display,$*)"
192         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop && rm cache/switch.$* cache/db.$*
193
194 ### link-oriented targets
195 # L/<nodeid1>-<node_id2>:
196 #       Establish a link between nodes <node_id1> and <node_id2>
197 L/%: cache/endpoint.%@1 cache/endpoint.%@2
198         @touch $@
199         @echo "Link $* is up"
200
201 # U/<node_id1>-<node_id2>
202 #       Tear down the link between nodes <node_id1> and <node_id2>
203 U/%: del-iface.%@1 del-iface.%@2
204         @rm -f L/$*
205         @echo "Deleted link $*"
206
207 # del-bridge.<node_id>: Delete the bridge on node <node_id>.
208 #
209 # We can do this only if the db on node <node_id> is running, but
210 # we don't need to re-delete if the db is restarted (hence the
211 # order-only dependency).
212 #
213 # Deleting a bridge also deletes all interfaces of the bridge
214 # as a side effect. This in turn invalidates local tunnel
215 # port numbers and endpoint info on both sides of each affected tunnel.
216 # The corresponding links obviously go down. 
217 # Controller information is also lost.
218 # We invalidate the cache accordingly.
219 del-bridge.%: | cache/db.%
220         @echo "Deleting bridge on $(call display,$*)"
221         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-bridge $(BRIDGE);
222         @rm -f cache/bridge.$* \
223               cache/iface.$*$(SEP)*@1 cache/iface.*$(SEP)$*@2 \
224               cache/port.$*$(SEP)*@1  cache/port.*$(SEP)$*@2  \
225               cache/endpoint.$*$(SEP)*@?  cache/endpoint.*$(SEP)$*@?  \
226               L/$*$(SEP)*             L/*$(SEP)$* \
227               cache/controller.$*
228
229 # del-switch.<node_id>: Stops the switch daemon on <node_id>.
230 #
231 # As a side effect, the local port numbers of the tunnels
232 # are no longer valid (they will change when the daemon is restarted)
233 # and, therefore, the endpoint info on the remote side of
234 # the tunnels must be invalidated. The links also go down.
235 # Controller information is also lost.
236 # We invalidate the cache accordingly.
237 del-switch.%:
238         @echo "Shutting down switch on $(call display,$*)"
239         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-switch
240         @rm -f cache/switch.$* \
241                cache/port.$*$(SEP)*@1  cache/port.*$(SEP)$*@2  \
242                cache/endpoint.$*$(SEP)*@2  cache/endpoint.*$(SEP)$*@1  \
243                L/$*$(SEP)*            L/*$(SEP)$* \
244                cache/controller.$*
245
246 # del-db.<node_id>: Stops the db daemon on <node_id>.
247 #
248 # This has no additional side effects.
249 del-db.%:
250         @echo "Shutting down db on $(call display,$*)"
251         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-db
252         @rm -f cache/db.$*
253
254 # del-controller.<node_id>:
255 #       Detaches <node_id> from the controller.
256 #
257 # This has no additional side effects.
258 del-controller-%: | cache/db.%
259         @echo "Removing controller for $(call display,$*)"
260         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-controller $(BRIDGE)
261         @rm -f cache/controller.$*
262
263 # del-links: Shortcut to delete all currently up links.
264 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
265
266 # del-switchs: Shortcut to stop all currently running switch daemons.
267 del-switchs: $(addprefix del-,$(notdir $(wildcard cache/switch.*)))
268
269 # del-dbs: Shortcut to stop all currently running db daemona.s
270 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
271
272 # shutdown: Shortcut to stop all currently running daemons.
273 shutdown: del-switchs del-dbs
274
275 .PHONY: del-links del-switchs del-dbs shutdown
276
277
278 ### snapshots
279 snapshot:
280         @echo BRIDGE=$(BRIDGE)
281         @cat cache/bridge.* 2>/dev/null || :
282         @cat cache/controller.* 2>/dev/null || :
283         @ls L | sed 's/^/LINKS += /'
284
285 remote-snapshot: remote-snapshot-ips remote-snapshot-links
286         @true
287
288 remote-snapshot-ips: $(addprefix cache/rsnap.ip.,$(CONF_NODE_IDS))
289         @cat /dev/null $^
290
291 remote-snapshot-links: $(addprefix cache/rsnap.links.,$(CONF_NODE_IDS))
292         @sort -u /dev/null $^
293
294 .PHONY: remote-snapshot remote-snapshot-ips remote-snapshot-links
295
296
297 cache/rsnap.ip.%: FORCE
298         @$(SSH) $(HOST_$*) $(SUDO) \
299                 sliver-ovs get-local-ip $(BRIDGE) | sed 's/^/IP_$*=/' > $@ \
300          || { rm $@; exit 1; }
301
302 cache/rsnap.links.%: FORCE
303         @$(SSH) $(HOST_$*) $(SUDO) \
304                 sliver-ovs get-local-links $(BRIDGE) | sed -n 's/^L/LINKS += /p' > $@ \
305          || { rm $@; exit 1; }
306
307 ### update sliver-ovs
308 update: $(addprefix update-,$(CONF_NODE_IDS))
309         @true
310
311 update-%: FORCE
312         @[ -n "$(SLIVER_OVS)" ] || { echo "SLIVER_OVS not set" >&2; exit 1; }
313         @[ -f "$(SLIVER_OVS)" ] || { echo "$(SLIVER_OVS) not found" >&2; exit 1; }
314         @echo "Sending $(SLIVER_OVS) to $(call display,$*)"
315         @scp -q $(SLIVER_OVS) $(SLICE)@$(call solve,$*):
316         @$(SSH) $(call solve,$*) $(SUDO) mv $(notdir $(SLIVER_OVS)) /usr/sbin/sliver-ovs
317
318 .SECONDEXPANSION:
319
320 # del-iface.<node_id>-<node_id>@<endpoint>:
321 #       Deletes the interface of link <node_id1>-<node_id2> on either
322 #       <node_id1> or <node_id2>, according to <endpoint>.
323
324 # We need a running db daemon to do this, but we do not have to
325 # redo the delete if the db daemon is restarted.
326 #
327 # This also invalidates the local port of the tunnel and the endpoint
328 # info on both sides of the tunnel. The link goes down.
329 del-iface.%: | cache/db.$$(call get,%)
330         @echo "Removing interface for link $(call linkpart,$*) from $(call get,$*)"
331         @$(SSH) $(HOST_$(call get,$*)) \
332                 $(SUDO) sliver-ovs del-port L$(call linkpart,$*)
333         @rm -f cache/iface.$* \
334                cache/port.$* cache/endpoint.$* cache/endpoint.$(call opp,$*) \
335                L/$(call linkpart,$*)
336
337
338 ### '%' here is leftid-rightid@{1,2}
339 # we retrieve % as $(*F)
340 #linkid=$(call linkpart,%)
341 #nodeid=$(call get,%)
342 #bridgefile=cache/bridge.$(nodeid)
343
344 # cache/iface.<node_id1>-<node_id2>@<endpoint>:
345 #       Creates the interface for link <node_id1>-<node_id2> on
346 #       <node_id<endpoint>>. 
347 #
348 # The bridge of the local node must already exist, and we need to create
349 # the interface again if the bridge is re-created.
350 # We also need a running db daemon, but we do not need to do anything
351 # if the db daemon is restarted.
352 cache/iface.%: cache/bridge.$$(call get,%) | cache/db.$$(call get,%)
353         @echo "Creating interface for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
354         @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs create-port $(BRIDGE) \
355                 L$(call linkpart,$(*F)) &> $(call log,$@) \
356          && touch $@
357
358 # cache/port.<node_id1>-<node_id2>@<endpoint>:
359 #       Retrieves the local port of link <node_id1>-<node_id2> on
360 #       node <node_id<endpoint>>.
361 #
362 # The local interface must have been created already and the
363 # switch daemon must be running. We need to retrieve the port
364 # number again if the interface is re-created, or the switch
365 # daemon is restarted.
366 cache/port.%: cache/iface.% cache/switch.$$(call get,%)
367         @echo "Getting port number for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
368         @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) \
369                 sliver-ovs get-local-endpoint L$(call linkpart,$(*F)) > $@ 2> $(call log,$@) \
370          || { rm $@; exit 1; }
371
372
373 # linkid=$(call linkpart,%)
374 # nodeid=$(call get,%)
375 # iface1=cache/iface.%
376 # iface2=cache/iface.$(call opp,%)
377
378 # cache/endpoint.<node_id1>-<node_id2>@<endpoint>:
379 #       Sets the other side (IP address, UDP port) info for link <node_id1>-<node_id2>
380 #       on <node_id<endpoint>>.
381 #
382 # We need the IP address and the UDP port of the other side and the interace of this side.
383 # We need to set the info again if any of these change.
384 cache/endpoint.%: cache/host.$$(call rget,%) cache/port.$$(call opp,%) cache/iface.%
385         @echo "Setting port number of link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
386         @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs set-remote-endpoint L$(call linkpart,$(*F)) \
387                         $$(cat cache/host.$(call rget,$(*F))) \
388                         $$(cat cache/port.$(call opp,$(*F))) 2> $(call log,$@) \
389          && touch $@
390
391 ####################
392 CLEANTARGETS=$(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
393 clean: $(CLEANTARGETS)
394 distclean:
395         rm -rf L log cache
396 .PHONY: clean distclean
397
398 ####################
399 graph.dot:
400         ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
401 graph.ps: graph.dot
402         dot -Tps < $^ > $@      
403
404 ####################
405 # googlemap stuff
406 # Uses:
407 # GMAP_SERVER, the hostname that runs an ndnmap instance
408 # GMAP_PROBES, a list of tuples of the form
409 # <linkid>=<nodeid>=<internal_linkid>=<frequency>
410 # e.g.
411 # SENDER-MUX=SENDER=3=0.5
412 # which would mean, the link 'SENDER-MUX' should be monitored at the 'SENDER' node and is coded 
413 # in links.json with id=3, every half second
414 # sliver-ovs gmap-probe is designed to run in background,
415 # and so that a new run will kill any previously running instance
416 gprobes: $(foreach probe,$(GMAP_PROBES),gprobe-$(probe))
417
418 gprobe-%: FORCE
419         $(SSH) -n $(call solve,$(word 2,$(subst @, ,$(*F)))) $(SUDO) \
420         sliver-ovs gmap-probe L$(word 1,$(subst @, ,$(*F))) $(GMAP_SERVER) $(word 3,$(subst @, ,$(*F))) $(word 4,$(subst @, ,$(*F)))
421
422 jsons: routers.json links.json geocode.json
423 .PHONE: jsons
424
425 routers.json: $(CONF)
426         @{  echo "[";   \
427             sep="  ";   \
428             for n in $(CONF_NODE_IDS); do \
429                 echo "    $$sep\"$$n\"";  \
430                 sep=", "; \
431             done; \
432             echo "]"; \
433           } > $@
434
435 links.json: $(foreach l,$(LINKS),cache/json.$(l))
436         @{  echo "["; \
437             cat $^ | sed '2,$$s/^/, /'; \
438             echo "]"; \
439          } > $@
440
441 cache/json.%: $(CONF)
442         @echo "{\"id\": $($*_linkid), \"start\": \"$(call get,$*@1)\", \"end\": \"$(call get,$*@2)\"}" > $@
443
444 geocode.json: $(foreach n,$(CONF_NODE_IDS),cache/geocode.$(n))
445         @{  echo "{"; \
446             sep="  "; \
447             for f in $^; do \
448                 echo -n "$$sep"; \
449                 cat $$f; \
450                 sep=", "; \
451             done; \
452             echo "}"; \
453          } > $@
454              
455 cache/geocode.%: cache/loc.% $(CONF)
456         @{  echo "\"$*\": {"; \
457             echo "    \"name\": \"$(HOST_$*)\","; \
458             echo "    \"shortname\": \"$*\","; \
459             echo "    \"site\": \"\","; \
460             echo "    \"backbone\": false,"; \
461             echo "    \"position\": [ $(shell cat cache/loc.$*) ]"; \
462             echo "   }"; \
463          } > $@
464
465 cache/loc.%: $(CONF)
466         @wget -O - 'http://freegeoip.net/csv/$(HOST_$*)' | \
467             awk -F, '{ printf "%s, %s\n", $$8, $$9 }' > $@
468
469 ####################
470 # 'virtual' targets in that there's no real file attached
471 define node_shortcuts
472 sshcheck.$(1): sshcheck-$(1) FORCE
473 db.$(1): cache/db.$(1) FORCE
474 switch.$(1): cache/switch.$(1) FORCE
475 start.$(1): cache/start.$(1) FORCE
476 stop.$(1): cache/stop.$(1) FORCE
477 status.$(1): cache/status.$(1) FORCE
478 bridge.$(1): cache/bridge.$(1) FORCE
479 host.$(1): cache/host.$(1) FORCE
480 controller.$(1): cache/controller.$(1) FORCE
481 # switch already depends on db, but well
482 cache/start.$(1): cache/db.$(1) cache/switch.$(1) FORCE
483 endef
484
485 $(foreach id,$(ALL_NODE_IDS), $(eval $(call node_shortcuts,$(id))))
486
487 define link_shortcuts
488 iface.%: cache/iface.%
489 endpoint.%: cache/endpoint.%
490 endef
491
492 $(foreach id,$(ALL_LINK_IDS), $(eval $(call link_shortcuts,$(id))))
493
494 help:
495         @cat Makefile.help
496
497 #################### convenience, for debugging only
498 # make +foo : prints the value of $(foo)
499 # make ++foo : idem but verbose, i.e. foo=$(foo)
500 ++%: varname=$(subst +,,$@)
501 ++%:
502         @echo "$(varname)=$($(varname))"
503 +%: varname=$(subst +,,$@)
504 +%:
505         @echo "$($(varname))"