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))
57 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
60 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
63 dbs: $(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
66 switchds: $(foreach id,$(ALL_NODE_IDS),cache/switchd.$(id))
69 ### node-oriented targets
71 @echo "IP lookup for $(call display,$*)"
72 @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
76 @echo "Starting db server on $(call display,$*)"
77 @$(SSH) $(HOST_$*) sudo start_ovsdb-server > $@ \
80 cache/switchd.%: cache/db.%
81 @echo "Starting vswitchd on $(call display,$*)"
82 @$(SSH) $(HOST_$*) sudo start_vswitchd > $@ \
85 cache/bridge.%: cache/switchd.%
86 @echo "Creating bridge on $(call display,$*)"
87 @$(SSH) $(HOST_$*) sudo create_bridge $(IP_$*) > $@ \
90 cache/sshcheck.%: FORCE
91 @if $(SSH) $(HOST_$*) hostname 2> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
92 else echo "ssh on" $(call display,$*) "KO !!!"; fi
94 ### link-oriented targets
95 L/%: cache/link.%@1 cache/link.%@2
97 @echo "Created link $*"
99 U/%: del-iface.%@1 del-iface.%@2
101 @echo "Deleted link $*"
103 del-bridge.%: cache/db.%
104 @echo "Deleting bridge on $(call display,$*)"
105 @if [ -f cache/bridge.$* ]; then \
106 $(SSH) $(HOST_$*) sudo del_bridge $$(cat cache/bridge.$*);\
108 @rm -f cache/bridge.$* \
109 cache/iface.$*-*@1 cache/iface.*-$*@2 \
110 cache/link.$*-*@? cache/link.*-$*@? \
113 del-switchd.%: del-bridge.%
114 @echo "Shutting down switchd on $(call display,$*)"
115 @if [ -f cache/switchd.$* ]; then \
116 $(SSH) $(HOST_$*) sudo ovs-appctl exit;\
118 @rm -f cache/switchd.$*
121 @echo "Shutting down db on $(call display,$*)"
122 @if [ -f cache/db.$* ]; then \
123 $(SSH) $(HOST_$*) sudo ovs-appctl --target=$(VARRUN)/ovsdb-server.$$(cat cache/db.$*).ctl exit;\
127 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
129 del-switchds: $(addprefix del-,$(notdir $(wildcard cache/switchd.*)))
131 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
133 shutdown: del-switches del-dbs
135 .PHONY: del-links del-switchds del-dbs shutdown
139 ( echo "digraph $(SLICE) {"; ls L | sed 's/-/->/;s/$$/;/'; echo "}" ) > $@
144 clean: $(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
148 .PHONY: clean distclean
152 del-iface.%: cache/db.$$(call get,%)
153 @echo "Removing interface for link $(call proj1,$*) from $(call get,$*)"
154 @$(SSH) $(HOST_$(call get,$*)) \
155 sudo del_port L$(call proj1,$*)
156 @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
160 cache/iface.%: cache/bridge.$$(call get,%)
161 @echo "Creating interface for link $(call proj1,$*) on $(call get,$*)"
162 @$(SSH) $(HOST_$(call get,$*)) \
163 sudo create_port $$(cat $^) L$(call proj1,$*) > $@ \
164 || { rm $@; exit 1; }
166 cache/link.%: cache/host.$$(call rget,$$*) cache/iface.% cache/iface.$$(call opp,$$*)
167 @echo "Setting port number of link $(call proj1,$*) on $(call get,$*)"
168 @$(SSH) $(HOST_$(call get,$*)) \
169 sudo ovs-vsctl set interface L$(call proj1,$*) \
170 options:remote_ip=$$(cat cache/host.$(call rget,$*)) \
171 options:remote_port=$$(cat cache/iface.$(call opp,$*)) \
174 #################### convenience, for debugging only
175 # make +foo : prints the value of $(foo)
176 # make ++foo : idem but verbose, i.e. foo=$(foo)
177 ++%: varname=$(subst +,,$@)
179 @echo "$(varname)=$($(varname))"
180 +%: varname=$(subst +,,$@)
182 @echo "$($(varname))"