add solve_ip function
[sliver-openvswitch.git] / planetlab / exp-tool / Makefile
1 include conf.mk
2
3 # proj1(x@y) = x
4 proj1=$(word 1,$(subst @, ,$(1)))
5
6 # proj2(x@y) = y
7 proj2=$(word 2,$(subst @, ,$(1)))
8
9 # get(x-y@1) = x
10 # get(x-y@2) = y
11 get=$(word $(call proj2,$(1)),$(subst -, ,$(call proj1,$(1))))
12
13 # flip(1) = 2
14 # flip(2) = 1
15 flip=$(if $(findstring 1,$(1)),2,1)
16
17 # opp(x-y@1) = x-y@2
18 # opp(x-y@2) = x-y@1
19 opp=$(call proj1,$(1))@$(call flip,$(call proj2,$(1)))
20
21 # rget(x-y@1) = y
22 # rget(x-y@2) = x
23 rget=$(call get,$(call opp,$(1)))
24
25 solve=$(HOST_$(1))
26 solve_ip=$(IP_$(1))
27 # can be redefined in conf.mk if that's not the expected behaviour
28 display?=host $(1) aka $(call solve,$(1))
29
30 #################### set variables after conf.mk
31 ifeq "$(SSH_KEY)" ""
32 SSH_KEY_OPTION ?=
33 else
34 SSH_KEY_OPTION ?= -i $(SSH_KEY)
35 endif
36
37 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE) -t
38 SSH = ssh $(SSH_OPTIONS)
39
40 ALL_NODE_IDS=$(shell (cut -d- -f1 < links; cut -d- -f2 < links) | sort -u)
41
42 ####################
43 all+init: init all
44 init:
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)
48 .PHONY: all+init init
49
50 FORCE:
51
52 .SECONDARY:
53
54 all: $(addprefix L/,$(shell cat links))
55 .PHONY: all
56
57 # could also do make ++SLICE
58 showslice: ++SLICE FORCE
59
60 shownodes:
61         @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
62 .PHONY: shownodes
63
64 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
65 .PHONY: sshchecks
66
67 dbs: $(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
68 .PHONY: dbs
69
70 switchds: $(foreach id,$(ALL_NODE_IDS),cache/switchd.$(id))
71 .PHONY: switchds
72
73 ### node-oriented targets
74 cache/host.%:
75         @echo "IP lookup for $(call display,$*)"
76         @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
77
78 cache/db.%:
79         @echo "Starting db server on $(call display,$*)"
80         @$(SSH) $(HOST_$*) sudo sliver-ovs start-db > $@ \
81          || { rm $@; exit 1; }
82
83 cache/switchd.%: cache/db.%
84         @echo "Starting vswitchd on $(call display,$*)"
85         @$(SSH) $(HOST_$*) sudo sliver-ovs start-switch > $@ \
86          || { rm $@; exit 1; }
87
88 cache/bridge.%: cache/switchd.%
89         @echo "Creating bridge on $(call display,$*)"
90         @$(SSH) $(HOST_$*) sudo sliver-ovs create-bridge $(IP_$*) > $@ \
91          || { rm $@; exit 1; }
92
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
96
97 ### link-oriented targets
98 L/%: cache/link.%@1 cache/link.%@2
99         @touch $@
100         @echo "Created link $*"
101
102 U/%: del-iface.%@1 del-iface.%@2
103         @rm -f L/$*
104         @echo "Deleted link $*"
105
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.$*);\
110          fi
111         @rm -f cache/bridge.$* \
112               cache/iface.$*-*@1 cache/iface.*-$*@2 \
113               cache/link.$*-*@?  cache/link.*-$*@?  \
114               L/$*-*             L/*-$*
115
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;\
120          fi
121         @rm -f cache/switchd.$*
122
123 del-db.%:
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;\
127          fi
128         @rm -f cache/db.$*
129
130 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
131
132 del-switchds: $(addprefix del-,$(notdir $(wildcard cache/switchd.*)))
133
134 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
135
136 shutdown: del-switches del-dbs
137
138 .PHONY: del-links del-switchds del-dbs shutdown
139
140 ####################
141 graph.dot:
142         ( echo "digraph $(SLICE) {"; ls L | sed 's/-/->/;s/$$/;/'; echo "}" ) > $@
143 graph.ps: graph.dot
144         dot -Tps < $^ > $@      
145
146 ####################
147 clean: $(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
148 distclean:
149         rm -rf L U cache
150
151 .PHONY: clean distclean
152
153 .SECONDEXPANSION:
154
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,$*)
160
161
162
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; }
168
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,$*)) \
175          && touch $@
176
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 +,,$@)
181 ++%:
182         @echo "$(varname)=$($(varname))"
183 +%: varname=$(subst +,,$@)
184 +%:
185         @echo "$($(varname))"