We do not actually need a U directory
[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 solve=$(HOST_$(1))
42 solve_ip=$(IP_$(1))
43 # can be redefined in conf.mk if that's not the expected behaviour
44 display?=host $(1) aka $(call solve,$(1))
45
46 # log file name
47 log=$(addprefix log/,$(notdir $(1)))
48
49 #################### set variables after conf.mk
50 ifeq "$(SSH_KEY)" ""
51 SSH_KEY_OPTION ?=
52 else
53 SSH_KEY_OPTION ?= -i $(SSH_KEY)
54 endif
55
56 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE)
57 SSH = ssh $(SSH_OPTIONS)
58
59 SUDO ?= sudo -S
60
61 ALL_NODE_IDS=$(sort $(foreach link,$(LINKS),$(call leftnode,$(link))) $(foreach link,$(LINKS),$(call rightnode,$(link))))
62 ALL_LINK_IDS=$(addsuffix @1,$(LINKS)) $(addsuffix @2,$(LINKS))
63
64 ####################
65 all+init: init all
66 init:
67         @[ -d L ] || ( echo Creating tmp dir L; mkdir L)
68         @[ -d log ] || (echo Creating tmp dir log; mkdir log)
69         @[ -d cache ] || ( echo Creating tmp dir cache; mkdir cache)
70 .PHONY: all+init init
71
72 FORCE:
73
74 .SECONDARY:
75
76 LINKTARGETS=$(addprefix L/,$(LINKS))
77 all: $(LINKTARGETS)
78 .PHONY: all
79
80 # could also do make ++SLICE
81 showslice: ++SLICE FORCE
82
83 shownodes:
84         @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
85 showips:
86         @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id)) has ip/network set to $(IP_$(id));)
87 showlinks:
88         @$(foreach link,$(LINKS), echo $(call display,$(call leftnode,$(link))) '====>' $(call display,$(call rightnode,$(link)));)
89 .PHONY: shownodes showips showlinks
90
91 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
92 .PHONY: sshchecks
93
94 DBS=$(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
95 dbs: $(DBS)
96 .PHONY: dbs
97
98 SWITCHS=$(foreach id,$(ALL_NODE_IDS),cache/switch.$(id))
99 switchs: $(SWITCHS)
100 .PHONY: switchs
101
102 start: dbs switchs
103 .PHONY: start
104
105 stop:$(foreach id,$(ALL_NODE_IDS),cache/stop.$(id))
106 .PHONY: stop
107
108 status:$(foreach id,$(ALL_NODE_IDS),cache/status.$(id))
109 .PHONY: status
110
111 BRIDGES=$(foreach id,$(ALL_NODE_IDS),cache/bridge.$(id))
112 bridges: $(BRIDGES)
113 .PHONY: bridges
114
115 ### node-oriented targets
116 # check ssh connectivity
117 cache/sshcheck.%: FORCE
118         @if $(SSH) $(HOST_$*) hostname 2> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
119          else echo "ssh on" $(call display,$*) "KO !!!"; fi
120
121 # should probably replace sshcheck
122 cache/status.%: FORCE
123         @echo "=== DB and SWITCH processes on $(call display,$*)"
124         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs status
125
126 cache/host.%:
127         @echo "IP lookup for $(call display,$*)"
128         @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
129
130 cache/db.%:
131         @echo "Starting db server on $(call display,$*) - logs in $(call log,$@)"
132         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-db &> $(call log,$@) && touch $@
133
134 cache/switch.%: cache/db.%
135         @echo "Starting vswitchd on $(call display,$*) - logs in $(call log,$@)"
136         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-switch &> $(call log,$@) && touch $@
137
138 cache/bridge.%: cache/switch.%
139         @echo "Creating bridge on $(call display,$*) - logs in $(call log,$@)"
140         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs create-bridge $(BRIDGE) $(IP_$*) > $@ 2> $(call log,$@) || { rm $@; exit 1; }
141         @echo Created bridge with tap $$(cat $@) on $(call display,$*)
142
143 # xxx this probably needs a more thorough cleanup in cache/
144 cache/stop.%: del-bridge.%
145         @echo "Stopping switch & db on $(call display,$*)"
146         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop && rm cache/switch.% cache/db.%
147
148 ### link-oriented targets
149 # L/<nodeid>-<node_id>
150 L/%: cache/link.%@1 cache/link.%@2
151         @touch $@
152         @echo "Created link $*"
153
154 U/%: del-iface.%@1 del-iface.%@2
155         @rm -f L/$*
156         @echo "Deleted link $*"
157
158 del-bridge.%: cache/db.%
159         @echo "Deleting bridge on $(call display,$*)"
160         @if [ -f cache/bridge.$* ]; then \
161                 $(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-bridge $(BRIDGE);\
162          fi
163         @rm -f cache/bridge.$* \
164               cache/iface.$*$(SEP)*@1 cache/iface.*$(SEP)$*@2 \
165               cache/link.$*$(SEP)*@?  cache/link.*$(SEP)$*@?  \
166               L/$*$(SEP)*             L/*$(SEP)$*
167
168 del-switch.%: del-bridge.%
169         @echo "Shutting down switch on $(call display,$*)"
170         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-switch
171         @rm -f cache/switch.$* 
172
173 del-db.%:
174         @echo "Shutting down db on $(call display,$*)"
175         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-db
176         @rm -f cache/db.$*
177
178 del-links: $(addprefix U/,$(notdir $(filter-out %.log,$(wildcard L/*))))
179
180 del-switchs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/switch.*))))
181
182 del-dbs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/db.*))))
183
184 shutdown: del-switchs del-dbs
185
186 .PHONY: del-links del-switchs del-dbs shutdown
187
188 .SECONDEXPANSION:
189
190 del-iface.%: cache/db.$$(call get,%)
191         @echo "Removing interface for link $(call linkpart,$*) from $(call get,$*)"
192         @$(SSH) $(HOST_$(call get,$*)) \
193                 $(SUDO) sliver-ovs del-port L$(call linkpart,$*)
194         @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
195
196
197 ### '%' here is leftid-rightid@{1,2}
198 # we retrieve % as $(*F)
199 #linkid=$(call linkpart,%)
200 #nodeid=$(call get,%)
201 #bridgefile=cache/bridge.$(nodeid)
202 cache/iface.%: cache/bridge.$$(call get,%)
203         @echo "Creating interface for link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
204         @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs create-port $(BRIDGE) \
205                 L$(call linkpart,$(*F)) > $@ 2> $(call log,$@) || { rm $@; exit 1; }
206
207
208 # linkid=$(call linkpart,%)
209 # nodeid=$(call get,%)
210 # iface1=cache/iface.%
211 # iface2=cache/iface.$(call opp,%)
212 cache/link.%: cache/host.$$(call rget,%) cache/iface.% cache/iface.$$(call opp,%)
213         @echo "Setting port number of link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
214         @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs set-remote-endpoint L$(call linkpart,$(*F)) \
215                         $$(cat cache/host.$(call rget,$(*F))) \
216                         $$(cat cache/iface.$(call opp,$(*F))) 2> $(call log,$@) \
217          && touch $@
218
219 ####################
220 CLEANTARGETS=$(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/bridge.*))))
221 clean: $(CLEANTARGETS)
222 distclean:
223         rm -rf L U cache
224 .PHONY: clean distclean
225
226 ####################
227 graph.dot:
228         ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
229 graph.ps: graph.dot
230         dot -Tps < $^ > $@      
231
232 ####################
233 # 'virtual' targets in that there's no real file attached
234 define node_shortcuts
235 sshcheck.$(1): cache/sshcheck.$(1) FORCE
236 db.$(1): cache/db.$(1) FORCE
237 switch.$(1): cache/switch.$(1) FORCE
238 start.$(1): cache/start.$(1) FORCE
239 stop.$(1): cache/stop.$(1) FORCE
240 status.$(1): cache/status.$(1) FORCE
241 bridge.$(1): cache/bridge.$(1) FORCE
242 host.$(1): cache/host.$(1) FORCE
243 # switch already depends on db, but well
244 cache/start.$(1): cache/db.$(1) cache/switch.$(1) FORCE
245 endef
246
247 $(foreach id,$(ALL_NODE_IDS), $(eval $(call node_shortcuts,$(id))))
248
249 define link_shortcuts
250 iface.%: cache/iface.%
251 link.%: cache/link.%
252 endef
253
254 $(foreach id,$(ALL_LINK_IDS), $(eval $(call link_shortcuts,$(id))))
255
256 #################### convenience, for debugging only
257 # make +foo : prints the value of $(foo)
258 # make ++foo : idem but verbose, i.e. foo=$(foo)
259 ++%: varname=$(subst +,,$@)
260 ++%:
261         @echo "$(varname)=$($(varname))"
262 +%: varname=$(subst +,,$@)
263 +%:
264         @echo "$($(varname))"