small improvements
[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 # can be redefined in conf.mk if that's not the expected behaviour
27 display?=host $(1) aka $(call solve,$(1))
28
29 #################### set variables after conf.mk
30 ifeq "$(SSH_KEY)" ""
31 SSH_KEY_OPTION ?=
32 else
33 SSH_KEY_OPTION ?= -i $(SSH_KEY)
34 endif
35
36 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE)
37 SSH = ssh $(SSH_OPTIONS)
38
39 ALL_NODE_IDS=$(shell (cut -d- -f1 < links; cut -d- -f2 < links) | sort -u)
40
41 ####################
42 all+init: init all
43 init:
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)
47 .PHONY: all+init init
48
49 FORCE:
50
51 .SECONDARY:
52
53 all: $(addprefix L/,$(shell cat links))
54 .PHONY: all
55
56 nodes:
57         @echo $(foreach id,$(ALL_NODE_IDS),$(call display,$(id)))
58 .PHONY: nodes
59
60 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
61 .PHONY: sshchecks
62
63 dbs: $(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
64 .PHONY: dbs
65
66 switchds: $(foreach id,$(ALL_NODE_IDS),cache/switchd.$(id))
67 .PHONY: switchds
68
69 ### node-oriented targets
70 cache/host.%:
71         @echo "IP lookup for $(call display,$*)"
72         @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
73
74 cache/db.%:
75         @echo "Starting db server on $(call display,$*)"
76         @$(SSH) $(HOST_$*) sudo start_ovsdb-server > $@ \
77          || { rm $@; exit 1; }
78
79 cache/switchd.%: cache/db.%
80         @echo "Starting vswitchd on $(call display,$*)"
81         @$(SSH) $(HOST_$*) sudo start_vswitchd > $@ \
82          || { rm $@; exit 1; }
83
84 cache/bridge.%: cache/switchd.%
85         @echo "Creating bridge on $(call display,$*)"
86         @$(SSH) $(HOST_$*) sudo create_bridge $(IP_$*) > $@ \
87          || { rm $@; exit 1; }
88
89 cache/sshcheck.%: FORCE
90         @if $(SSH) $(HOST_$*) hostname 2> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
91          else echo "ssh on" $(call display,$*) "KO !!!"; fi
92
93 ### link-oriented targets
94 L/%: cache/link.%@1 cache/link.%@2
95         @touch $@
96         @echo "Created link $*"
97
98 U/%: del-iface.%@1 del-iface.%@2
99         @rm -f L/$*
100         @echo "Deleted link $*"
101
102 del-bridge.%: cache/db.%
103         @echo "Deleting bridge on $(call display,$*)"
104         @if [ -f cache/bridge.$* ]; then \
105                 $(SSH) $(HOST_$*) sudo del_bridge $$(cat cache/bridge.$*);\
106          fi
107         @rm -f cache/bridge.$* \
108               cache/iface.$*-*@1 cache/iface.*-$*@2 \
109               cache/link.$*-*@?  cache/link.*-$*@?  \
110               L/$*-*             L/*-$*
111
112 del-switchd.%: del-bridge.%
113         @echo "Shutting down switchd on $(call display,$*)"
114         @if [ -f cache/switchd.$* ]; then \
115                 $(SSH) $(HOST_$*) sudo ovs-appctl exit;\
116          fi
117         @rm -f cache/switchd.$*
118
119 del-db.%:
120         @echo "Shutting down db on $(call display,$*)"
121         @if [ -f cache/db.$* ]; then \
122                 $(SSH) $(HOST_$*) sudo ovs-appctl --target=$(VARRUN)/ovsdb-server.$$(cat cache/db.$*).ctl exit;\
123          fi
124         @rm -f cache/db.$*
125
126 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
127
128 del-switchds: $(addprefix del-,$(notdir $(wildcard cache/switchd.*)))
129
130 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
131
132 shutdown: del-switches del-dbs
133
134 .PHONY: del-links del-switchds del-dbs shutdown
135
136 ####################
137 graph.dot:
138         ( echo "digraph $(SLICE) {"; ls L | sed 's/-/->/;s/$$/;/'; echo "}" ) > $@
139 graph.ps: graph.dot
140         dot -Tps < $^ > $@      
141
142 ####################
143 clean: $(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
144 distclean:
145         rm -rf L U cache
146
147 .PHONY: clean distclean
148
149 .SECONDEXPANSION:
150
151 del-iface.%: cache/db.$$(call get,%)
152         @echo "Removing interface for link $(call proj1,$*) from $(call get,$*)"
153         @$(SSH) $(HOST_$(call get,$*)) \
154                 sudo del_port L$(call proj1,$*)
155         @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
156
157
158
159 cache/iface.%: cache/bridge.$$(call get,%)
160         @echo "Creating interface for link $(call proj1,$*) on $(call get,$*)"
161         @$(SSH) $(HOST_$(call get,$*)) \
162                 sudo create_port $$(cat $^) L$(call proj1,$*) > $@ \
163          || { rm $@; exit 1; }
164
165 cache/link.%: cache/host.$$(call rget,$$*) cache/iface.% cache/iface.$$(call opp,$$*)
166         @echo "Setting port number of link $(call proj1,$*) on $(call get,$*)"
167         @$(SSH) $(HOST_$(call get,$*)) \
168                 sudo ovs-vsctl set interface L$(call proj1,$*) \
169                         options:remote_ip=$$(cat cache/host.$(call rget,$*)) \
170                         options:remote_port=$$(cat cache/iface.$(call opp,$*)) \
171          && touch $@
172
173 #################### convenience, for debugging only
174 # make +foo : prints the value of $(foo)
175 # make ++foo : idem but verbose, i.e. foo=$(foo)
176 ++%: varname=$(subst +,,$@)
177 ++%:
178         @echo "$(varname)=$($(varname))"
179 +%: varname=$(subst +,,$@)
180 +%:
181         @echo "$($(varname))"