4 proj1=$(word 1,$(subst @, ,$(1)))
7 proj2=$(word 2,$(subst @, ,$(1)))
11 get=$(word $(call proj2,$(1)),$(subst -, ,$(call proj1,$(1))))
15 flip=$(if $(findstring 1,$(1)),2,1)
19 opp=$(call proj1,$(1))@$(call flip,$(call proj2,$(1)))
23 rget=$(call get,$(call opp,$(1)))
27 # can be redefined in conf.mk if that's not the expected behaviour
28 display?=host $(1) aka $(call solve,$(1))
30 #################### set variables after conf.mk
34 SSH_KEY_OPTION ?= -i $(SSH_KEY)
37 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE) -t
38 SSH = ssh $(SSH_OPTIONS)
40 ALL_NODE_IDS=$(shell (cut -d- -f1 < links; cut -d- -f2 < links) | sort -u)
45 @[ -d L ] || ( echo Creating tmp dir L; mkdir L)
46 @[ -d U ] || ( echo Creating tmp dir U; mkdir U)
47 @[ -d cache ] || ( echo Creating tmp dir cache; mkdir cache)
54 all: $(addprefix L/,$(shell cat links))
57 # could also do make ++SLICE
58 showslice: ++SLICE FORCE
61 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
64 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
67 dbs: $(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
70 switchds: $(foreach id,$(ALL_NODE_IDS),cache/switchd.$(id))
73 ### node-oriented targets
75 @echo "IP lookup for $(call display,$*)"
76 @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
79 @echo "Starting db server on $(call display,$*)"
80 @$(SSH) $(HOST_$*) sudo sliver-ovs start-db > $@ \
83 cache/switchd.%: cache/db.%
84 @echo "Starting vswitchd on $(call display,$*)"
85 @$(SSH) $(HOST_$*) sudo sliver-ovs start-switch > $@ \
88 cache/bridge.%: cache/switchd.%
89 @echo "Creating bridge on $(call display,$*)"
90 @$(SSH) $(HOST_$*) sudo sliver-ovs create-bridge $(IP_$*) > $@ \
93 cache/sshcheck.%: FORCE
94 @if $(SSH) $(HOST_$*) hostname 2> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
95 else echo "ssh on" $(call display,$*) "KO !!!"; fi
97 ### link-oriented targets
98 L/%: cache/link.%@1 cache/link.%@2
100 @echo "Created link $*"
102 U/%: del-iface.%@1 del-iface.%@2
104 @echo "Deleted link $*"
106 del-bridge.%: cache/db.%
107 @echo "Deleting bridge on $(call display,$*)"
108 @if [ -f cache/bridge.$* ]; then \
109 $(SSH) $(HOST_$*) sudo sliver-ovs del-bridge $$(cat cache/bridge.$*);\
111 @rm -f cache/bridge.$* \
112 cache/iface.$*-*@1 cache/iface.*-$*@2 \
113 cache/link.$*-*@? cache/link.*-$*@? \
116 del-switchd.%: del-bridge.%
117 @echo "Shutting down switchd on $(call display,$*)"
118 @if [ -f cache/switchd.$* ]; then \
119 $(SSH) $(HOST_$*) sudo ovs-appctl exit;\
121 @rm -f cache/switchd.$*
124 @echo "Shutting down db on $(call display,$*)"
125 @if [ -f cache/db.$* ]; then \
126 $(SSH) $(HOST_$*) sudo ovs-appctl --target=$(VARRUN)/ovsdb-server.$$(cat cache/db.$*).ctl exit;\
130 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
132 del-switchds: $(addprefix del-,$(notdir $(wildcard cache/switchd.*)))
134 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
136 shutdown: del-switches del-dbs
138 .PHONY: del-links del-switchds del-dbs shutdown
142 ( echo "digraph $(SLICE) {"; ls L | sed 's/-/->/;s/$$/;/'; echo "}" ) > $@
147 clean: $(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
151 .PHONY: clean distclean
155 del-iface.%: cache/db.$$(call get,%)
156 @echo "Removing interface for link $(call proj1,$*) from $(call get,$*)"
157 @$(SSH) $(HOST_$(call get,$*)) \
158 sudo sliver-ovs del-port L$(call proj1,$*)
159 @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
163 cache/iface.%: cache/bridge.$$(call get,%)
164 @echo "Creating interface for link $(call proj1,$*) on $(call get,$*)"
165 @$(SSH) $(HOST_$(call get,$*)) \
166 sudo sliver-ovs create-port $$(cat $^) L$(call proj1,$*) > $@ \
167 || { rm $@; exit 1; }
169 cache/link.%: cache/host.$$(call rget,$$*) cache/iface.% cache/iface.$$(call opp,$$*)
170 @echo "Setting port number of link $(call proj1,$*) on $(call get,$*)"
171 @$(SSH) $(HOST_$(call get,$*)) \
172 sudo ovs-vsctl set interface L$(call proj1,$*) \
173 options:remote_ip=$$(cat cache/host.$(call rget,$*)) \
174 options:remote_port=$$(cat cache/iface.$(call opp,$*)) \
177 #################### convenience, for debugging only
178 # make +foo : prints the value of $(foo)
179 # make ++foo : idem but verbose, i.e. foo=$(foo)
180 ++%: varname=$(subst +,,$@)
182 @echo "$(varname)=$($(varname))"
183 +%: varname=$(subst +,,$@)
185 @echo "$($(varname))"