make the separator configurable
[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 # can be redefined in conf.mk if that's not the expected behaviour
29 display?=host $(1) aka $(call solve,$(1))
30
31 #################### set variables after conf.mk
32 ifeq "$(SSH_KEY)" ""
33 SSH_KEY_OPTION ?=
34 else
35 SSH_KEY_OPTION ?= -i $(SSH_KEY)
36 endif
37
38 SSH_OPTIONS ?= $(SSH_KEY_OPTION) -l $(SLICE) -t
39 SSH = ssh $(SSH_OPTIONS)
40
41 ALL_NODE_IDS=$(shell (cut -d- -f1 < links; cut -d- -f2 < links) | sort -u)
42
43 ####################
44 all+init: init all
45 init:
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)
49 .PHONY: all+init init
50
51 FORCE:
52
53 .SECONDARY:
54
55 all: $(addprefix L/,$(shell cat links))
56 .PHONY: all
57
58 # could also do make ++SLICE
59 showslice: ++SLICE FORCE
60
61 shownodes:
62         @$(foreach id,$(ALL_NODE_IDS),echo $(id)=$(call display,$(id));)
63 .PHONY: shownodes
64
65 sshchecks: $(foreach id,$(ALL_NODE_IDS),cache/sshcheck.$(id))
66 .PHONY: sshchecks
67
68 dbs: $(foreach id,$(ALL_NODE_IDS),cache/db.$(id))
69 .PHONY: dbs
70
71 switchds: $(foreach id,$(ALL_NODE_IDS),cache/switchd.$(id))
72 .PHONY: switchds
73
74 ### node-oriented targets
75 cache/host.%:
76         @echo "IP lookup for $(call display,$*)"
77         @host $(HOST_$*) | sed -n 's/^.*has address *//p' > $@
78
79 cache/db.%:
80         @echo "Starting db server on $(call display,$*)"
81         @$(SSH) $(HOST_$*) sudo sliver-ovs start-db > $@ \
82          || { rm $@; exit 1; }
83
84 cache/switchd.%: cache/db.%
85         @echo "Starting vswitchd on $(call display,$*)"
86         @$(SSH) $(HOST_$*) sudo sliver-ovs start-switch > $@ \
87          || { rm $@; exit 1; }
88
89 cache/bridge.%: cache/switchd.%
90         @echo "Creating bridge on $(call display,$*)"
91         @$(SSH) $(HOST_$*) sudo sliver-ovs create-bridge $(IP_$*) > $@ \
92          || { rm $@; exit 1; }
93
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
97
98 ### link-oriented targets
99 L/%: cache/link.%@1 cache/link.%@2
100         @touch $@
101         @echo "Created link $*"
102
103 U/%: del-iface.%@1 del-iface.%@2
104         @rm -f L/$*
105         @echo "Deleted link $*"
106
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.$*);\
111          fi
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)$*
116
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;\
121          fi
122         @rm -f cache/switchd.$*
123
124 del-db.%:
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;\
128          fi
129         @rm -f cache/db.$*
130
131 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
132
133 del-switchds: $(addprefix del-,$(notdir $(wildcard cache/switchd.*)))
134
135 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
136
137 shutdown: del-switches del-dbs
138
139 .PHONY: del-links del-switchds del-dbs shutdown
140
141 ####################
142 graph.dot:
143         ( echo "digraph $(SLICE) {"; ls L | sed 's/$(SEP)/->/;s/$$/;/'; echo "}" ) > $@
144 graph.ps: graph.dot
145         dot -Tps < $^ > $@      
146
147 ####################
148 clean: $(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
149 distclean:
150         rm -rf L U cache
151
152 .PHONY: clean distclean
153
154 .SECONDEXPANSION:
155
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,$*)
161
162
163
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; }
169
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,$*)) \
176          && touch $@
177
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 +,,$@)
182 ++%:
183         @echo "$(varname)=$($(varname))"
184 +%: varname=$(subst +,,$@)
185 +%:
186         @echo "$($(varname))"