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)))
26 # can be redefined in conf.mk if that's not the expected behaviour
27 display?=host $(1) aka $(call solve,$(1))
29 #################### set variables after conf.mk
33 SSH_KEY_OPTION ?= -i $(SSH_KEY)
36 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE) -t
37 SSH = ssh $(SSH_OPTIONS)
39 ALL_NODE_IDS=$(shell (cut -d- -f1 < links; cut -d- -f2 < links) | sort -u)
44 @[ -d L ] || ( echo Creating tmp dir L; mkdir L)
45 @[ -d U ] || ( echo Creating tmp dir U; mkdir U)
46 @[ -d cache ] || ( echo Creating tmp dir cache; mkdir cache)
53 all: $(addprefix L/,$(shell cat links))
56 # could also do make ++SLICE
57 showslice: ++SLICE FORCE
60 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
63 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
66 dbs: $(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
69 switchds: $(foreach id,$(ALL_NODE_IDS),cache/switchd.$(id))
72 ### node-oriented targets
74 @echo "IP lookup for $(call display,$*)"
75 @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
78 @echo "Starting db server on $(call display,$*)"
79 @$(SSH) $(HOST_$*) sudo sliver-ovs start-db > $@ \
82 cache/switchd.%: cache/db.%
83 @echo "Starting vswitchd on $(call display,$*)"
84 @$(SSH) $(HOST_$*) sudo sliver-ovs start-switch > $@ \
87 cache/bridge.%: cache/switchd.%
88 @echo "Creating bridge on $(call display,$*)"
89 @$(SSH) $(HOST_$*) sudo sliver-ovs create-bridge $(IP_$*) > $@ \
92 cache/sshcheck.%: FORCE
93 @if $(SSH) $(HOST_$*) hostname 2> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
94 else echo "ssh on" $(call display,$*) "KO !!!"; fi
96 ### link-oriented targets
97 L/%: cache/link.%@1 cache/link.%@2
99 @echo "Created link $*"
101 U/%: del-iface.%@1 del-iface.%@2
103 @echo "Deleted link $*"
105 del-bridge.%: cache/db.%
106 @echo "Deleting bridge on $(call display,$*)"
107 @if [ -f cache/bridge.$* ]; then \
108 $(SSH) $(HOST_$*) sudo sliver-ovs del-bridge $$(cat cache/bridge.$*);\
110 @rm -f cache/bridge.$* \
111 cache/iface.$*-*@1 cache/iface.*-$*@2 \
112 cache/link.$*-*@? cache/link.*-$*@? \
115 del-switchd.%: del-bridge.%
116 @echo "Shutting down switchd on $(call display,$*)"
117 @if [ -f cache/switchd.$* ]; then \
118 $(SSH) $(HOST_$*) sudo ovs-appctl exit;\
120 @rm -f cache/switchd.$*
123 @echo "Shutting down db on $(call display,$*)"
124 @if [ -f cache/db.$* ]; then \
125 $(SSH) $(HOST_$*) sudo ovs-appctl --target=$(VARRUN)/ovsdb-server.$$(cat cache/db.$*).ctl exit;\
129 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
131 del-switchds: $(addprefix del-,$(notdir $(wildcard cache/switchd.*)))
133 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
135 shutdown: del-switches del-dbs
137 .PHONY: del-links del-switchds del-dbs shutdown
141 ( echo "digraph $(SLICE) {"; ls L | sed 's/-/->/;s/$$/;/'; echo "}" ) > $@
146 clean: $(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
150 .PHONY: clean distclean
154 del-iface.%: cache/db.$$(call get,%)
155 @echo "Removing interface for link $(call proj1,$*) from $(call get,$*)"
156 @$(SSH) $(HOST_$(call get,$*)) \
157 sudo sliver-ovs del-port L$(call proj1,$*)
158 @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
162 cache/iface.%: cache/bridge.$$(call get,%)
163 @echo "Creating interface for link $(call proj1,$*) on $(call get,$*)"
164 @$(SSH) $(HOST_$(call get,$*)) \
165 sudo sliver-ovs create-port $$(cat $^) L$(call proj1,$*) > $@ \
166 || { rm $@; exit 1; }
168 cache/link.%: cache/host.$$(call rget,$$*) cache/iface.% cache/iface.$$(call opp,$$*)
169 @echo "Setting port number of link $(call proj1,$*) on $(call get,$*)"
170 @$(SSH) $(HOST_$(call get,$*)) \
171 sudo ovs-vsctl set interface L$(call proj1,$*) \
172 options:remote_ip=$$(cat cache/host.$(call rget,$*)) \
173 options:remote_port=$$(cat cache/iface.$(call opp,$*)) \
176 #################### convenience, for debugging only
177 # make +foo : prints the value of $(foo)
178 # make ++foo : idem but verbose, i.e. foo=$(foo)
179 ++%: varname=$(subst +,,$@)
181 @echo "$(varname)=$($(varname))"
182 +%: varname=$(subst +,,$@)
184 @echo "$($(varname))"