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