add set-remote-endpoint action to sliver-ovs
[sliver-openvswitch.git] / planetlab / exp-tool / Makefile
1 include conf.mk
2
3
4 # proj1(x@y) = x
5 proj1=$(word 1,$(subst @, ,$(1)))
6
7 # proj2(x@y) = y
8 proj2=$(word 2,$(subst @, ,$(1)))
9
10 SEP?=-
11 # get(x-y@1) = x
12 # get(x-y@2) = y
13 get=$(word $(call proj2,$(1)),$(subst $(SEP), ,$(call proj1,$(1))))
14
15 # flip(1) = 2
16 # flip(2) = 1
17 flip=$(if $(findstring 1,$(1)),2,1)
18
19 # opp(x-y@1) = x-y@2
20 # opp(x-y@2) = x-y@1
21 opp=$(call proj1,$(1))@$(call flip,$(call proj2,$(1)))
22
23 # rget(x-y@1) = y
24 # rget(x-y@2) = x
25 rget=$(call get,$(call opp,$(1)))
26
27 solve=$(HOST_$(1))
28 solve_ip=$(IP_$(1))
29 # can be redefined in conf.mk if that's not the expected behaviour
30 display?=host $(1) aka $(call solve,$(1))
31
32 #################### set variables after conf.mk
33 ifeq "$(SSH_KEY)" ""
34 SSH_KEY_OPTION ?=
35 else
36 SSH_KEY_OPTION ?= -i $(SSH_KEY)
37 endif
38
39 #SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE) -t
40 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE)
41 SSH = ssh $(SSH_OPTIONS)
42
43 SUDO ?= sudo -S
44
45 ALL_NODE_IDS=$(shell (cut -d- -f1 < links; cut -d- -f2 < links) | sort -u)
46
47 ####################
48 all+init: init all
49 init:
50         @[ -d L ] || ( echo Creating tmp dir L; mkdir L)
51         @[ -d U ] || ( echo Creating tmp dir U; mkdir U)
52         @[ -d cache ] || ( echo Creating tmp dir cache; mkdir cache)
53 .PHONY: all+init init
54
55 FORCE:
56
57 .SECONDARY:
58
59 all: $(addprefix L/,$(shell cat links))
60 .PHONY: all
61
62 # could also do make ++SLICE
63 showslice: ++SLICE FORCE
64
65 shownodes:
66         @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
67 showips:
68         @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id)) has ip/network set to $(IP_$(id));)
69 .PHONY: shownodes showips
70
71 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
72 .PHONY: sshchecks
73
74 dbs: $(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
75 .PHONY: dbs
76
77 switchs: $(foreach id,$(ALL_NODE_IDS),cache/switch.$(id))
78 .PHONY: switchs
79
80 start: dbs switchs
81 .PHONY: start
82
83 stop:$(foreach id,$(ALL_NODE_IDS),cache/stop.$(id))
84 .PHONY: stop
85
86 status:$(foreach id,$(ALL_NODE_IDS),cache/status.$(id))
87 .PHONY: status
88
89 ### node-oriented targets
90 # check ssh connectivity
91 cache/sshcheck.%: FORCE
92         @if $(SSH) $(HOST_$*) hostname 2> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
93          else echo "ssh on" $(call display,$*) "KO !!!"; fi
94
95 # should probably replace sshcheck
96 cache/status.%: FORCE
97         @echo "=== DB and SWITCH processes on $(call display,$*)"
98         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs status
99
100 cache/host.%:
101         @echo "IP lookup for $(call display,$*)"
102         @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
103
104 cache/db.%:
105         @echo "Starting db server on $(call display,$*) - logs in $@.log"
106         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-db &> $@.log && touch $@
107
108 cache/switch.%: cache/db.%
109         @echo "Starting vswitchd on $(call display,$*) - logs in $@.log"
110         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs start-switch &> $@.log && touch $@
111
112 cache/bridge.%: cache/switch.%
113         @echo "Creating bridge on $(call display,$*) - logs in $@.log"
114         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs create-bridge $(IP_$*) > $@ 2> $@.log || { rm $@; exit 1; }
115
116 # xxx this probably needs a more thorough cleanup in cache/
117 cache/stop.%: del-bridge.%
118         @echo "Stopping switch & db on $(call display,$*)"
119         @$(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop && rm cache/switch.% cache/db.%
120
121 ### link-oriented targets
122 L/%: cache/link.%@1 cache/link.%@2
123         @touch $@
124         @echo "Created link $*"
125
126 U/%: del-iface.%@1 del-iface.%@2
127         @rm -f L/$*
128         @echo "Deleted link $*"
129
130 del-bridge.%: cache/db.%
131         @echo "Deleting bridge on $(call display,$*)"
132         @if [ -f cache/bridge.$* ]; then \
133                 $(SSH) $(HOST_$*) $(SUDO) sliver-ovs del-bridge $$(cat cache/bridge.$*);\
134          fi
135         @rm -f cache/bridge.$* \
136               cache/iface.$*$(SEP)*@1 cache/iface.*$(SEP)$*@2 \
137               cache/link.$*$(SEP)*@?  cache/link.*$(SEP)$*@?  \
138               L/$*$(SEP)*             L/*$(SEP)$*
139
140 del-switch.%: del-bridge.%
141         @echo "Shutting down switch on $(call display,$*)"
142         @if [ -f cache/switch.$* ]; then \
143                 $(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-switch
144          fi
145         @rm -f cache/switch.$*
146
147 del-db.%:
148         @echo "Shutting down db on $(call display,$*)"
149         @if [ -f cache/db.$* ]; then \
150                 $(SSH) $(HOST_$*) $(SUDO) sliver-ovs stop-db
151          fi
152         @rm -f cache/db.$*
153
154 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
155
156 del-switchs: $(addprefix del-,$(notdir $(wildcard cache/switch.*)))
157
158 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
159
160 shutdown: del-switches del-dbs
161
162 .PHONY: del-links del-switchs del-dbs shutdown
163
164 ####################
165 graph.dot:
166         ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
167 graph.ps: graph.dot
168         dot -Tps < $^ > $@      
169
170 ####################
171 clean: $(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
172 distclean:
173         rm -rf L U cache
174
175 .PHONY: clean distclean
176
177 .SECONDEXPANSION:
178
179 del-iface.%: cache/db.$$(call get,%)
180         @echo "Removing interface for link $(call proj1,$*) from $(call get,$*)"
181         @$(SSH) $(HOST_$(call get,$*)) \
182                 $(SUDO) sliver-ovs del-port L$(call proj1,$*)
183         @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
184
185
186
187 cache/iface.%: cache/bridge.$$(call get,%)
188         @echo "Creating interface for link $(call proj1,$*) on $(call get,$*)"
189         @$(SSH) $(HOST_$(call get,$*)) \
190                 $(SUDO) sliver-ovs create-port $$(cat $^) L$(call proj1,$*) > $@ \
191          || { rm $@; exit 1; }
192
193 cache/link.%: cache/host.$$(call rget,$$*) cache/iface.% cache/iface.$$(call opp,$$*)
194         @echo "Setting port number of link $(call proj1,$*) on $(call get,$*)"
195         @$(SSH) $(HOST_$(call get,$*)) \
196                 $(SUDO) sliver-ovs set-remote-endpoint L$(call proj1,$*) \
197                         $$(cat cache/host.$(call rget,$*)) \
198                         $$(cat cache/iface.$(call opp,$*)) \
199          && touch $@
200
201 ####################
202 # 'virtual' targets in that there's no real file attached
203 define node_shortcuts
204 sshcheck.$(1): cache/sshcheck.$(1) FORCE
205 db.$(1): cache/db.$(1) FORCE
206 switch.$(1): cache/switch.$(1) FORCE
207 start.$(1): cache/start.$(1) FORCE
208 stop.$(1): cache/stop.$(1) FORCE
209 status.$(1): cache/status.$(1) FORCE
210 bridge.$(1): cache/bridge.$(1) FORCE
211 # switch already depends on db, but well
212 cache/start.$(1): cache/db.$(1) cache/switch.$(1) FORCE
213 endef
214
215 $(foreach id,$(ALL_NODE_IDS), $(eval $(call node_shortcuts,$(id))))
216
217 #define link_shortcuts
218 #
219 #endef
220
221 #################### convenience, for debugging only
222 # make +foo : prints the value of $(foo)
223 # make ++foo : idem but verbose, i.e. foo=$(foo)
224 ++%: varname=$(subst +,,$@)
225 ++%:
226         @echo "$(varname)=$($(varname))"
227 +%: varname=$(subst +,,$@)
228 +%:
229         @echo "$($(varname))"