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