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