Skip to content

Commit 5ca5851

Browse files
author
Naredula Janardhana Reddy
committed
Bug 12808:
Summary of changes: - Mutiple routing table for each public interface is added (previously there is only one routing table ). when the packet is send out of public interface corresponding per-interface routing table will be used. per-interface routing table will modified when ever ip/interface added/deleted. - New parameter is added to ipassoc command to include the default gateway for every interface/ip. prevously it is using only one public interface to send out, default gateway is obtained at the boot up time. - In the DNAT case. In the revese path(from guest vm to outside, or when DNAT packet receives from the eth0) the public ip/source ip will not be available till POSTROUTING. to overcome this, DNAT connection are marked with routing table number at the time of connection creation, in the reverse path the routing table# from DNAT connection is used to detect per-interface routing table.
1 parent f558aa0 commit 5ca5851

6 files changed

Lines changed: 92 additions & 4 deletions

File tree

‎core/src/com/cloud/agent/resource/virtualnetwork/VirtualRoutingResource.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,9 @@ public String assignPublicIpAddress(final String vmName,
635635

636636
String publicNic = "eth" + nicNum;
637637
command.add("-c", publicNic);
638+
639+
command.add("-g", vlanGateway);
640+
638641

639642
return command.execute();
640643
}

‎core/src/com/cloud/hypervisor/vmware/resource/VmwareResource.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,10 @@ protected void assignPublicIpAddress(VirtualMachineMO vmMo, final String vmName,
752752
}
753753

754754
args += " -c ";
755-
args += "eth" + publicNicInfo.first();
755+
args += "eth" + publicNicInfo.first();
756+
757+
args += " -g ";
758+
args += vlanGateway;
756759

757760
if (s_logger.isDebugEnabled()) {
758761
s_logger.debug("Run command on domain router " + privateIpAddress + ", /root/ipassoc.sh " + args);

‎core/src/com/cloud/hypervisor/xen/resource/CitrixResourceBase.java‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1639,7 +1639,10 @@ protected void assignPublicIpAddress(Connection conn, String vmName, String priv
16391639

16401640
args += " -c ";
16411641
args += "eth" + correctVif.getDevice(conn);
1642-
1642+
1643+
args += " -g ";
1644+
args += vlanGateway;
1645+
16431646

16441647
String result = callHostPlugin(conn, "vmops", "ipassoc", "args", args);
16451648
if (result == null || result.isEmpty()) {

‎patches/systemvm/debian/config/etc/iptables/iptables-router‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,5 @@ COMMIT
3030
:OUTPUT ACCEPT [0:0]
3131
:POSTROUTING ACCEPT [0:0]
3232
-A POSTROUTING -p udp --dport bootpc -j CHECKSUM --checksum-fill
33+
-A PREROUTING -m state --state ESTABLISHED,RELATED -j CONNMARK --restore-mark
3334
COMMIT

‎patches/systemvm/debian/config/root/firewall.sh‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ tcp_or_udp_entry() {
5959
local op=$5
6060
local proto=$6
6161
local cidrs=$7
62+
6263
logger -t cloud "$(basename $0): creating port fwd entry for PAT: public ip=$publicIp \
6364
instance ip=$instIp proto=$proto port=$port dport=$dport op=$op"
6465

@@ -67,11 +68,16 @@ tcp_or_udp_entry() {
6768
# the delete operation may have errored out but the only possible reason is
6869
# that the rules didn't exist in the first place
6970
local dev=$(ip_to_dev $publicIp)
71+
local tableNo=$(echo $dev | awk -F'eth' '{print $2}')
7072
# shortcircuit the process if error and it is an append operation
7173
# continue if it is delete
7274
(sudo iptables -t nat $op PREROUTING --proto $proto -i $dev -d $publicIp \
7375
--destination-port $port -j DNAT \
7476
--to-destination $instIp:$dport &>> $OUTFILE || [ "$op" == "-D" ]) &&
77+
(sudo iptables -t mangle $op PREROUTING --proto $proto -i $dev -d $publicIp \
78+
--destination-port $port -j MARK --set-mark $tableNo) &&
79+
(sudo iptables -t mangle $op PREROUTING --proto $proto -i $dev -d $publicIp \
80+
--destination-port $port -m state --state NEW -j CONNMARK --save-mark) &&
7581
(sudo iptables -t nat $op OUTPUT --proto $proto -d $publicIp \
7682
--destination-port $port -j DNAT \
7783
--to-destination $instIp:$dport &>> $OUTFILE || [ "$op" == "-D" ]) &&

‎patches/systemvm/debian/config/root/ipassoc.sh‎

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,72 @@ convert_primary_to_32() {
134134
done
135135
}
136136

137+
remove_routing() {
138+
local pubIp=$1
139+
local ipNoMask=$(echo $pubIp | awk -F'/' '{print $1}')
140+
local mask=$(echo $pubIp | awk -F'/' '{print $2}')
141+
local tableNo=$(echo $ethDev | awk -F'eth' '{print $2}')
142+
143+
local tableName="Table_$ethDev"
144+
local ethMask=$(ip route show | grep $ethDev | grep src | awk '{print $1}')
145+
if [ "$ethMask" != "" ]
146+
then
147+
# rules and routes will be deleted for the last ip of the interface.
148+
sudo ip route delete throw $ethMask table $tableName proto static
149+
sudo ip rule delete from $ethMask table $tableName
150+
sudo ip rule delete fwmark $tableNo table $tableName
151+
sudo ip route flush cache
152+
fi
153+
}
154+
155+
# copy eth0,eth1 and the current public interface
156+
copy_routes_from_main() {
157+
local tableName=$1
158+
159+
#get the network masks from the main table
160+
local eth0Mask=$(ip route show | grep eth0 | grep src | awk '{print $1}')
161+
local eth1Mask=$(ip route show | grep eth1 | grep src | awk '{print $1}')
162+
local ethMask=$(ip route show | grep $ethDev | grep src | awk '{print $1}')
163+
164+
# eth0,eth1 and other know routes will be skipped, so as main routing table will decide the route. This will be useful if the interface is down and up.
165+
sudo ip route add throw $eth0Mask table $tableName proto static
166+
sudo ip route add throw $eth1Mask table $tableName proto static
167+
sudo ip route add throw $ethMask table $tableName proto static
168+
return 0;
169+
}
170+
171+
add_routing() {
172+
local pubIp=$1
173+
local ipNoMask=$(echo $1 | awk -F'/' '{print $1}')
174+
local mask=$(echo $1 | awk -F'/' '{print $2}')
175+
176+
local tableName="Table_$ethDev"
177+
local tablePresent=$(grep $tableName /etc/iproute2/rt_tables)
178+
local tableNo=$(echo $ethDev | awk -F'eth' '{print $2}')
179+
if [ "$tablePresent" == "" ]
180+
then
181+
if [ "$tableNo" == ""]
182+
then
183+
return 0;
184+
fi
185+
echo "$tableNo $tableName" >> /etc/iproute2/rt_tables
186+
fi
187+
188+
copy_routes_from_main $tableName
189+
# NOTE: this entry will be deleted if the interface is down without knowing to Management server, in that case all the outside traffic will be send through main routing table or it will be the first public NIC.
190+
sudo ip route add default via $defaultGwIP table $tableName proto static
191+
sudo ip route flush cache
192+
193+
local ethMask=$(ip route show | grep $ethDev | grep src | awk '{print $1}')
194+
local rulePresent=$(ip rule show | grep $ethMask)
195+
if [ "$rulePresent" == "" ]
196+
then
197+
# rules will be added while adding the first ip of the interface
198+
sudo ip rule add from $ethMask table $tableName
199+
sudo ip rule add fwmark $tableNo table $tableName
200+
fi
201+
return 0;
202+
}
137203

138204
add_nat_entry() {
139205
local pubIp=$1
@@ -167,6 +233,7 @@ add_nat_entry() {
167233
sudo ip link set $ethDev up
168234
sudo arping -c 3 -I $ethDev -A -U -s $ipNoMask $ipNoMask;
169235
fi
236+
add_routing $1
170237

171238
return 0
172239
}
@@ -182,6 +249,7 @@ del_nat_entry() {
182249
sudo iptables -t nat -D POSTROUTING -j SNAT -o $ethDev --to-source $ipNoMask;
183250
sudo ip addr del dev $ethDev "$ipNoMask/$mask"
184251

252+
remove_routing $1
185253
if [ $? -gt 0 -a $? -ne 2 ]
186254
then
187255
return 1
@@ -205,7 +273,7 @@ add_an_ip () {
205273
sudo ip link set $ethDev up
206274
sudo arping -c 3 -I $ethDev -A -U -s $ipNoMask $ipNoMask;
207275
fi
208-
276+
add_routing $1
209277
return $?
210278

211279
}
@@ -236,6 +304,7 @@ remove_an_ip () {
236304
fi
237305
result=$?
238306
fi
307+
remove_routing $1
239308
if [ $result -gt 0 -a $result -ne 2 ]
240309
then
241310
return 1
@@ -268,7 +337,7 @@ then
268337
if_keep_state=1
269338
fi
270339

271-
while getopts 'fADa:l:c:' OPTION
340+
while getopts 'fADa:l:c:g:' OPTION
272341
do
273342
case $OPTION in
274343
A) Aflag=1
@@ -285,6 +354,9 @@ do
285354
c) cflag=1
286355
ethDev="$OPTARG"
287356
;;
357+
g) gflag=1
358+
defaultGwIP="$OPTARG"
359+
;;
288360
?) usage
289361
unlock_exit 2 $lock $locked
290362
;;

0 commit comments

Comments
 (0)