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