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
6 # run make CONF=anotherconfig.mk if you need several configs
11 # if undefined in the conf file, use single dash
14 # bridge name (XXX the same on all nodes)
20 flip=$(if $(findstring 1,$(1)),2,1)
22 cutsep=$(subst $(SEP), ,$(1))
24 leftnode=$(word 1,$(call cutsep,$(1)))
25 # rightnode (x-y) -> y
26 rightnode=$(word 2,$(call cutsep,$(1)))
28 linkpart=$(word 1,$(subst @, ,$(1)))
30 endpart=$(word 2,$(subst @, ,$(1)))
33 get=$(word $(call endpart,$(1)),$(call cutsep,$(call linkpart,$(1))))
36 opp=$(call linkpart,$(1))@$(call flip,$(call endpart,$(1)))
39 rget=$(call get,$(call opp,$(1)))
43 # can be redefined in conf.mk if that's not the expected behaviour
44 display?=host $(1) aka $(call solve,$(1))
47 log=$(addprefix log/,$(notdir $(1)))
49 #################### set variables after conf.mk
53 SSH_KEY_OPTION ?= -i $(SSH_KEY)
56 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE)
57 SSH = ssh $(SSH_OPTIONS)
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))
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)
76 LINKTARGETS=$(addprefix L/,$(LINKS))
80 # could also do make ++SLICE
81 showslice: ++SLICE FORCE
84 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
86 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id)) has ip/network set to $(IP_$(id));)
88 @$(foreach link,$(LINKS), echo $(call display,$(call leftnode,$(link))) '====>' $(call display,$(call rightnode,$(link)));)
89 .PHONY: shownodes showips showlinks
91 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
94 DBS=$(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
98 SWITCHS=$(foreach id,$(ALL_NODE_IDS),cache/switch.$(id))
105 stop:$(foreach id,$(ALL_NODE_IDS),cache/stop.$(id))
108 status:$(foreach id,$(ALL_NODE_IDS),cache/status.$(id))
111 BRIDGES=$(foreach id,$(ALL_NODE_IDS),cache/bridge.$(id))
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
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
127 @echo "IP lookup for $(call display,$*)"
128 @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
131 @echo "Starting db server on $(call display,$*) - logs in $(call log,$@)"
132 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-db &> $(call log,$@) && touch $@
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 $@
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,$*)
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.%
148 ### link-oriented targets
149 # L/<nodeid>-<node_id>
150 L/%: cache/link.%@1 cache/link.%@2
152 @echo "Created link $*"
154 U/%: del-iface.%@1 del-iface.%@2
156 @echo "Deleted link $*"
158 del-bridge.%: cache/db.%
159 @echo "Deleting bridge on $(call display,$*)"
160 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-bridge $(BRIDGE);
161 @rm -f cache/bridge.$* \
162 cache/iface.$*$(SEP)*@1 cache/iface.*$(SEP)$*@2 \
163 cache/link.$*$(SEP)*@? cache/link.*$(SEP)$*@? \
164 L/$*$(SEP)* L/*$(SEP)$*
166 del-switch.%: del-bridge.%
167 @echo "Shutting down switch on $(call display,$*)"
168 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-switch
169 @rm -f cache/switch.$*
172 @echo "Shutting down db on $(call display,$*)"
173 @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-db
176 del-links: $(addprefix U/,$(notdir $(filter-out %.log,$(wildcard L/*))))
178 del-switchs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/switch.*))))
180 del-dbs: $(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/db.*))))
182 shutdown: del-switchs del-dbs
184 .PHONY: del-links del-switchs del-dbs shutdown
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,$*)
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 $(call log,$@)"
202 @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs create-port $(BRIDGE) \
203 L$(call linkpart,$(*F)) > $@ 2> $(call log,$@) || { rm $@; exit 1; }
206 # linkid=$(call linkpart,%)
207 # nodeid=$(call get,%)
208 # iface1=cache/iface.%
209 # iface2=cache/iface.$(call opp,%)
210 cache/link.%: cache/host.$$(call rget,%) cache/iface.% cache/iface.$$(call opp,%)
211 @echo "Setting port number of link $(call linkpart,$(*F)) on $(call display,$(call get,$(*F))) - logs in $(call log,$@)"
212 @$(SSH) $(call solve,$(call get,$(*F))) $(SUDO) sliver-ovs set-remote-endpoint L$(call linkpart,$(*F)) \
213 $$(cat cache/host.$(call rget,$(*F))) \
214 $$(cat cache/iface.$(call opp,$(*F))) 2> $(call log,$@) \
218 CLEANTARGETS=$(addprefix del-,$(notdir $(filter-out %.log,$(wildcard cache/bridge.*))))
219 clean: $(CLEANTARGETS)
222 .PHONY: clean distclean
226 ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
231 # 'virtual' targets in that there's no real file attached
232 define node_shortcuts
233 sshcheck.$(1): cache/sshcheck.$(1) FORCE
234 db.$(1): cache/db.$(1) FORCE
235 switch.$(1): cache/switch.$(1) FORCE
236 start.$(1): cache/start.$(1) FORCE
237 stop.$(1): cache/stop.$(1) FORCE
238 status.$(1): cache/status.$(1) FORCE
239 bridge.$(1): cache/bridge.$(1) FORCE
240 host.$(1): cache/host.$(1) FORCE
241 # switch already depends on db, but well
242 cache/start.$(1): cache/db.$(1) cache/switch.$(1) FORCE
245 $(foreach id,$(ALL_NODE_IDS), $(eval $(call node_shortcuts,$(id))))
247 define link_shortcuts
248 iface.%: cache/iface.%
252 $(foreach id,$(ALL_LINK_IDS), $(eval $(call link_shortcuts,$(id))))
254 #################### convenience, for debugging only
255 # make +foo : prints the value of $(foo)
256 # make ++foo : idem but verbose, i.e. foo=$(foo)
257 ++%: varname=$(subst +,,$@)
259 @echo "$(varname)=$($(varname))"
260 +%: varname=$(subst +,,$@)
262 @echo "$($(varname))"