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