Merge branch 'master' of ssh://git.onelab.eu/git/sliver-openvswitch
[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
102 DBS=$(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
103 dbs: $(DBS)
104 .PHONY: dbs
105
106 SWITCHS=$(foreach id,$(ALL_NODE_IDS),cache/switch.$(id))
107 switchs: $(SWITCHS)
108 .PHONY: switchs
109
110 start: dbs switchs
111 .PHONY: start
112
113 stop:$(foreach id,$(ALL_NODE_IDS),cache/stop.$(id))
114 .PHONY: stop
115
116 status:$(foreach id,$(ALL_NODE_IDS),cache/status.$(id))
117 .PHONY: status
118
119 BRIDGES=$(foreach id,$(ALL_NODE_IDS),cache/bridge.$(id))
120 bridges: $(BRIDGES)
121 .PHONY: bridges
122
123 CONTROLLERS=$(foreach id,$(ALL_NODE_IDS),cache/controller.$(id))
124 controllers: $(CONTROLLERS)
125 .PHONY: controllers
126
127 del-controllers: $(foreach id,$(ALL_NODE_IDS),del-controller-$(id))
128 .PHONY: del-controllers
129
130 ### node-oriented targets
131 # check ssh connectivity
132 sshcheck-%: FORCE
133         @if $(SSH) $(HOST_$*) hostname &> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
134          else echo "ssh on" $(call display,$*) "KO !!!"; fi
135
136 ovsversion-%: FORCE
137         @OVSVERSION=$$($(SSH) $(HOST_$*) rpm -q sliver-openvswitch); echo $(call display,$*) has $$OVSVERSION
138
139 # should probably replace sshcheck
140 cache/status.%: FORCE
141         @echo "=== DB and SWITCH processes on $(call display,$*)"
142         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs status
143
144 cache/host.%:
145         @echo "IP lookup for $(call display,$*)"
146         @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
147
148 cache/db.%:
149         @echo "Starting db server on $(call display,$*) - logs in $(call log,$@)"
150         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-db &> $(call log,$@) && touch $@
151
152 cache/switch.%: cache/db.%
153         @echo "Starting vswitchd on $(call display,$*) - logs in $(call log,$@)"
154         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-switch &> $(call log,$@) && touch $@
155
156 cache/bridge.%: cache/switch.%
157         @echo "Creating bridge on $(call display,$*) - logs in $(call log,$@)"
158         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs create-bridge $(BRIDGE) $(IP_$*) > $@ 2> $(call log,$@) || { rm $@; exit 1; }
159         @echo Created bridge with tap $$(cat $@) on $(call display,$*)
160
161 cache/controller.%: cache/bridge.%
162         @echo "Setting controller $(call default,CONTROLLER,$*) on $(call display,$*) - logs in $(call log,$@)"
163         @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl set-controller $(BRIDGE) $(call default,CONTROLLER,$*) &> $(call log,$@) || { rm $@; exit 1; }
164
165 # xxx this probably needs a more thorough cleanup in cache/
166 cache/stop.%: del-bridge.%
167         @echo "Stopping switch & db on $(call display,$*)"
168         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop && rm cache/switch.% cache/db.%
169
170 ### link-oriented targets
171 # L/<nodeid>-<node_id>
172 L/%: cache/link.%@1 cache/link.%@2
173         @touch $@
174         @echo "Created link $*"
175
176 U/%: del-iface.%@1 del-iface.%@2
177         @rm -f L/$*
178         @echo "Deleted link $*"
179
180 del-bridge.%: cache/db.%
181         @echo "Deleting bridge on $(call display,$*)"
182         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-bridge $(BRIDGE);
183         @rm -f cache/bridge.$* \
184               cache/iface.$*$(SEP)*@1 cache/iface.*$(SEP)$*@2 \
185               cache/link.$*$(SEP)*@?  cache/link.*$(SEP)$*@?  \
186               L/$*$(SEP)*             L/*$(SEP)$*
187
188 del-switch.%: del-bridge.%
189         @echo "Shutting down switch on $(call display,$*)"
190         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-switch
191         @rm -f cache/switch.$* 
192
193 del-db.%:
194         @echo "Shutting down db on $(call display,$*)"
195         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-db
196         @rm -f cache/db.$*
197
198 del-controller.%:
199         @echo "Removing controller for $(call display,$*)"
200         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-controller $(BRIDGE)
201         @rm -f cache/controller.$*
202
203 del-links: $(addprefix U/,$(notdir $(filter-out %.log,$(wildcard L/*))))
204
205 del-switchs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/switch.*))))
206
207 del-dbs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/db.*))))
208
209 shutdown: del-switchs del-dbs
210
211 .PHONY: del-links del-switchs del-dbs shutdown
212
213
214 ### snapshots
215 remote-snapshot: remote-snapshot-ips remote-snapshot-links
216         @true
217
218 remote-snapshot-ips: $(addprefix cache/rsnap.ip.,$(CONF_NODE_IDS))
219         @cat /dev/null $^
220
221 remote-snapshot-links: $(addprefix cache/rsnap.links.,$(CONF_NODE_IDS))
222         @sort -u /dev/null $^
223
224 ### update sliver-ovs
225 update: $(addprefix update-,$(CONF_NODE_IDS))
226         @true
227
228 update-%: FORCE
229         @[ -n "$(SLIVER_OVS)" ] || { echo "SLIVER_OVS not set" >&2; exit 1; }
230         @[ -f "$(SLIVER_OVS)" ] || { echo "$(SLIVER_OVS) not found" >&2; exit 1; }
231         @echo "Sending $(SLIVER_OVS) to $(call display,$*)"
232         @scp -q $(SLIVER_OVS) $(SLICE)@$(call solve,$*):
233         @$(SSH) $(call solve,$*) $(SUDO) mv $(notdir $(SLIVER_OVS)) /usr/sbin/sliver-ovs
234
235 .PHONY: remote-snapshot remote-snapshot-ips remote-snapshot-links
236
237 cache/rsnap.ip.%: 
238         @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl -- \
239                 get interface $(BRIDGE) options:local_ip options:local_netmask |\
240                 sed -n 's|"||g;1h;2{G;s|\(.*\)\n\(.*\)|IP_$* = \2/\1|p}' > $@ || \
241          { rm $@; exit 1; }
242
243 cache/rsnap.links.%: 
244         @$(SSH) $(HOST_$*) $(SUDO) ovs-vsctl list-ifaces $(BRIDGE) | \
245          sed -n 's/^L/LINKS += /p' > $@ || \
246          { rm $@; exit 1; }
247
248 .SECONDEXPANSION:
249
250 del-iface.%: cache/db.$$(call get,%)
251         @echo "Removing interface for link $(call linkpart,$*) from $(call get,$*)"
252         @$(SSH) $(HOST_$(call get,$*)) \
253                 $(SUDO) sliver-ovs del-port L$(call linkpart,$*)
254         @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
255
256
257 ### '%' here is leftid-rightid@{1,2}
258 # we retrieve % as $(*F)
259 #linkid=$(call linkpart,%)
260 #nodeid=$(call get,%)
261 #bridgefile=cache/bridge.$(nodeid)
262 cache/iface.%: cache/bridge.$$(call get,%)
263         @echo "Creating interface for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
264         @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs create-port $(BRIDGE) \
265                 L$(call linkpart,$(*F)) > $@ 2> $(call log,$@) || { rm $@; exit 1; }
266
267
268 # linkid=$(call linkpart,%)
269 # nodeid=$(call get,%)
270 # iface1=cache/iface.%
271 # iface2=cache/iface.$(call opp,%)
272 cache/link.%: cache/host.$$(call rget,%) cache/iface.% cache/iface.$$(call opp,%)
273         @echo "Setting port number of link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
274         @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs set-remote-endpoint L$(call linkpart,$(*F)) \
275                         $$(cat cache/host.$(call rget,$(*F))) \
276                         $$(cat cache/iface.$(call opp,$(*F))) 2> $(call log,$@) \
277          && touch $@
278
279 ####################
280 CLEANTARGETS=$(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/bridge.*))))
281 clean: $(CLEANTARGETS)
282 distclean:
283         rm -rf L U cache
284 .PHONY: clean distclean
285
286 ####################
287 graph.dot:
288         ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
289 graph.ps: graph.dot
290         dot -Tps < $^ > $@      
291
292
293 ####################
294 # 'virtual' targets in that there's no real file attached
295 define node_shortcuts
296 sshcheck.$(1): sshcheck-$(1) FORCE
297 db.$(1): cache/db.$(1) FORCE
298 switch.$(1): cache/switch.$(1) FORCE
299 start.$(1): cache/start.$(1) FORCE
300 stop.$(1): cache/stop.$(1) FORCE
301 status.$(1): cache/status.$(1) FORCE
302 bridge.$(1): cache/bridge.$(1) FORCE
303 host.$(1): cache/host.$(1) FORCE
304 # switch already depends on db, but well
305 cache/start.$(1): cache/db.$(1) cache/switch.$(1) FORCE
306 endef
307
308 $(foreach id,$(ALL_NODE_IDS), $(eval $(call node_shortcuts,$(id))))
309
310 define link_shortcuts
311 iface.%: cache/iface.%
312 link.%: cache/link.%
313 endef
314
315 $(foreach id,$(ALL_LINK_IDS), $(eval $(call link_shortcuts,$(id))))
316
317 help:
318         @cat Makefile.help
319
320 #################### convenience, for debugging only
321 # make +foo : prints the value of $(foo)
322 # make ++foo : idem but verbose, i.e. foo=$(foo)
323 ++%: varname=$(subst +,,$@)
324 ++%:
325         @echo "$(varname)=$($(varname))"
326 +%: varname=$(subst +,,$@)
327 +%:
328         @echo "$($(varname))"