Merge branch 'forward-port'
[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) -t
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 <<<<<<< HEAD
76         @echo "Starting db server on $(call display,$*)"
77         @$(SSH) $(HOST_$*) sudo start_ovsdb-server > $@ \
78          || { rm $@; exit 1; }
79
80 cache/switchd.%: cache/db.%
81         @echo "Starting vswitchd on $(call display,$*)"
82         @$(SSH) $(HOST_$*) sudo start_vswitchd > $@ \
83          || { rm $@; exit 1; }
84
85 cache/bridge.%: cache/switchd.%
86         @echo "Creating bridge on $(call display,$*)"
87         @$(SSH) $(HOST_$*) sudo create_bridge $(IP_$*) > $@ \
88          || { rm $@; exit 1; }
89
90 cache/sshcheck.%: FORCE
91         @if $(SSH) $(HOST_$*) hostname 2> /dev/null; then echo "ssh on" $(call display,$*) "OK" ; \
92          else echo "ssh on" $(call display,$*) "KO !!!"; fi
93
94 ### link-oriented targets
95 L/%: cache/link.%@1 cache/link.%@2
96         @touch $@
97         @echo "Created link $*"
98
99 U/%: del-iface.%@1 del-iface.%@2
100         @rm -f L/$*
101         @echo "Deleted link $*"
102
103 del-bridge.%: cache/db.%
104         @echo "Deleting bridge on $(call display,$*)"
105         @if [ -f cache/bridge.$* ]; then \
106                 $(SSH) $(HOST_$*) sudo del_bridge $$(cat cache/bridge.$*);\
107          fi
108         @rm -f cache/bridge.$* \
109               cache/iface.$*-*@1 cache/iface.*-$*@2 \
110               cache/link.$*-*@?  cache/link.*-$*@?  \
111               L/$*-*             L/*-$*
112
113 del-switchd.%: del-bridge.%
114         @echo "Shutting down switchd on $(call display,$*)"
115         @if [ -f cache/switchd.$* ]; then \
116                 $(SSH) $(HOST_$*) sudo ovs-appctl exit;\
117          fi
118         @rm -f cache/switchd.$*
119
120 del-db.%:
121         @echo "Shutting down db on $(call display,$*)"
122         @if [ -f cache/db.$* ]; then \
123                 $(SSH) $(HOST_$*) sudo ovs-appctl --target=$(VARRUN)/ovsdb-server.$$(cat cache/db.$*).ctl exit;\
124          fi
125         @rm -f cache/db.$*
126
127 del-links: $(addprefix U/,$(notdir $(wildcard L/*)))
128
129 del-switchds: $(addprefix del-,$(notdir $(wildcard cache/switchd.*)))
130
131 del-dbs: $(addprefix del-,$(notdir $(wildcard cache/db.*)))
132
133 shutdown: del-switches del-dbs
134
135 .PHONY: del-links del-switchds del-dbs shutdown
136
137 ####################
138 graph.dot:
139         ( echo "digraph $(SLICE) {"; ls L | sed 's/-/->/;s/$$/;/'; echo "}" ) > $@
140 graph.ps: graph.dot
141         dot -Tps < $^ > $@      
142
143 ####################
144 clean: $(addprefix del-,$(notdir $(wildcard cache/bridge.*)))
145 distclean:
146         rm -rf L U cache
147
148 .PHONY: clean distclean
149
150 .SECONDEXPANSION:
151
152 del-iface.%: cache/db.$$(call get,%)
153         @echo "Removing interface for link $(call proj1,$*) from $(call get,$*)"
154         @$(SSH) $(HOST_$(call get,$*)) \
155                 sudo del_port L$(call proj1,$*)
156         @rm -f cache/iface.$* cache/link.$* cache/link.$(call opp,$*)
157
158
159
160 cache/iface.%: cache/bridge.$$(call get,%)
161         @echo "Creating interface for link $(call proj1,$*) on $(call get,$*)"
162         @$(SSH) $(HOST_$(call get,$*)) \
163                 sudo create_port $$(cat $^) L$(call proj1,$*) > $@ \
164          || { rm $@; exit 1; }
165
166 cache/link.%: cache/host.$$(call rget,$$*) cache/iface.% cache/iface.$$(call opp,$$*)
167         @echo "Setting port number of link $(call proj1,$*) on $(call get,$*)"
168         @$(SSH) $(HOST_$(call get,$*)) \
169                 sudo ovs-vsctl set interface L$(call proj1,$*) \
170                         options:remote_ip=$$(cat cache/host.$(call rget,$*)) \
171                         options:remote_port=$$(cat cache/iface.$(call opp,$*)) \
172          && touch $@
173
174 #################### convenience, for debugging only
175 # make +foo : prints the value of $(foo)
176 # make ++foo : idem but verbose, i.e. foo=$(foo)
177 ++%: varname=$(subst +,,$@)
178 ++%:
179         @echo "$(varname)=$($(varname))"
180 +%: varname=$(subst +,,$@)
181 +%:
182         @echo "$($(varname))"