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