5 proj1=$(word 1,$(subst @, ,$(1)))
8 proj2=$(word 2,$(subst @, ,$(1)))
13 get=$(word $(call proj2,$(1)),$(subst $(SEP), ,$(call proj1,$(1))))
17 flip=$(if $(findstring 1,$(1)),2,1)
21 opp=$(call proj1,$(1))@$(call flip,$(call proj2,$(1)))
25 rget=$(call get,$(call opp,$(1)))
28 # can be redefined in conf.mk if that's not the expected behaviour
29 display?=host $(1) aka $(call solve,$(1))
31 #################### set variables after conf.mk
35 SSH_KEY_OPTION ?= -i $(SSH_KEY)
38 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE) -t
39 SSH = ssh $(SSH_OPTIONS)
41 ALL_NODE_IDS=$(shell (cut -d- -f1 < links; cut -d- -f2 < links) | sort -u)
46 @[ -d L ] || ( echo Creating tmp dir L; mkdir L)
47 @[ -d U ] || ( echo Creating tmp dir U; mkdir U)
48 @[ -d cache ] || ( echo Creating tmp dir cache; mkdir cache)
55 all: $(addprefix L/,$(shell cat links))
58 # could also do make ++SLICE
59 showslice: ++SLICE FORCE
62 @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
65 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
68 dbs: $(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
71 switchds: $(foreach id,$(ALL_NODE_IDS),cache/switchd.$(id))
74 ### node-oriented targets
76 @echo "IP lookup for $(call display,$*)"
77 @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
80 @echo "Starting db server on $(call display,$*)"
81 @$(SSH) $(HOST_$*) sudo sliver-ovs start-db > $@ \
84 cache/switchd.%: cache/db.%
85 @echo "Starting vswitchd on $(call display,$*)"
86 @$(SSH) $(HOST_$*) sudo sliver-ovs start-switch > $@ \
89 cache/bridge.%: cache/switchd.%
90 @echo "Creating bridge on $(call display,$*)"
91 @$(SSH) $(HOST_$*) sudo sliver-ovs create-bridge $(IP_$*) > $@ \
94 cache/sshcheck.%: FORCE
95 @if $(SSH) $(HOST_$*) hostname 2> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
96 else echo "ssh on" $(call display,$*) "KO !!!"; fi
98 ### link-oriented targets
99 L/%: cache/link.%@1 cache/link.%@2
101 @echo "Created link $*"
103 U/%: del-iface.%@1 del-iface.%@2
105 @echo "Deleted link $*"
107 del-bridge.%: cache/db.%
108 @echo "Deleting bridge on $(call display,$*)"
109 @if [ -f cache/bridge.$* ]; then \
110 $(SSH) $(HOST_$*) sudo sliver-ovs del-bridge $$(cat cache/bridge.$*);\
112 @rm -f cache/bridge.$* \
113 cache/iface.$*$(SEP)*@1 cache/iface.*$(SEP)$*@2 \
114 cache/link.$*$(SEP)*@? cache/link.*$(SEP)$*@? \
115 L/$*$(SEP)* L/*$(SEP)$*
117 del-switchd.%: del-bridge.%
118 @echo "Shutting down switchd on $(call display,$*)"
119 @if [ -f cache/switchd.$* ]; then \
120 $(SSH) $(HOST_$*) sudo ovs-appctl exit;\
122 @rm -f cache/switchd.$*
125 @echo "Shutting down db on $(call display,$*)"
126 @if [ -f cache/db.$* ]; then \
127 $(SSH) $(HOST_$*) sudo ovs-appctl --target=$(VARRUN)/ovsdb-server.$$(cat cache/db.$*).ctl exit;\
131 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
133 del-switchds: $(addprefix del-,$(notdir $(wildcard cache/switchd.*)))
135 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
137 shutdown: del-switches del-dbs
139 .PHONY: del-links del-switchds del-dbs shutdown
143 ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
148 clean: $(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
152 .PHONY: clean distclean
156 del-iface.%: cache/db.$$(call get,%)
157 @echo "Removing interface for link $(call proj1,$*) from $(call get,$*)"
158 @$(SSH) $(HOST_$(call get,$*)) \
159 sudo sliver-ovs del-port L$(call proj1,$*)
160 @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
164 cache/iface.%: cache/bridge.$$(call get,%)
165 @echo "Creating interface for link $(call proj1,$*) on $(call get,$*)"
166 @$(SSH) $(HOST_$(call get,$*)) \
167 sudo sliver-ovs create-port $$(cat $^) L$(call proj1,$*) > $@ \
168 || { rm $@; exit 1; }
170 cache/link.%: cache/host.$$(call rget,$$*) cache/iface.% cache/iface.$$(call opp,$$*)
171 @echo "Setting port number of link $(call proj1,$*) on $(call get,$*)"
172 @$(SSH) $(HOST_$(call get,$*)) \
173 sudo ovs-vsctl set interface L$(call proj1,$*) \
174 options:remote_ip=$$(cat cache/host.$(call rget,$*)) \
175 options:remote_port=$$(cat cache/iface.$(call opp,$*)) \
178 #################### convenience, for debugging only
179 # make +foo : prints the value of $(foo)
180 # make ++foo : idem but verbose, i.e. foo=$(foo)
181 ++%: varname=$(subst +,,$@)
183 @echo "$(varname)=$($(varname))"
184 +%: varname=$(subst +,,$@)
186 @echo "$($(varname))"