[ONOS-4426] Upgrade Vtn Module when access same network segment
Change-Id: Id0d00e9d0e93d1baf4ff20560469316fee5a3186
Showing
8 changed files
with
253 additions
and
9 deletions
This diff is collapsed. Click to expand it.
... | @@ -20,8 +20,10 @@ import org.onlab.packet.IpPrefix; | ... | @@ -20,8 +20,10 @@ import org.onlab.packet.IpPrefix; |
20 | import org.onlab.packet.MacAddress; | 20 | import org.onlab.packet.MacAddress; |
21 | import org.onosproject.core.ApplicationId; | 21 | import org.onosproject.core.ApplicationId; |
22 | import org.onosproject.net.DeviceId; | 22 | import org.onosproject.net.DeviceId; |
23 | +import org.onosproject.net.Port; | ||
23 | import org.onosproject.net.PortNumber; | 24 | import org.onosproject.net.PortNumber; |
24 | import org.onosproject.net.flowobjective.Objective; | 25 | import org.onosproject.net.flowobjective.Objective; |
26 | +import org.onosproject.net.flowobjective.Objective.Operation; | ||
25 | import org.onosproject.vtnrsc.SegmentationId; | 27 | import org.onosproject.vtnrsc.SegmentationId; |
26 | 28 | ||
27 | /** | 29 | /** |
... | @@ -134,4 +136,16 @@ public interface ClassifierService { | ... | @@ -134,4 +136,16 @@ public interface ClassifierService { |
134 | IpAddress dstIp, MacAddress dstmac, | 136 | IpAddress dstIp, MacAddress dstmac, |
135 | SegmentationId actionVni, | 137 | SegmentationId actionVni, |
136 | Objective.Operation type); | 138 | Objective.Operation type); |
139 | + | ||
140 | + /** | ||
141 | + * Assemble the export port Arp Classifier table rules. | ||
142 | + * Match: export port. | ||
143 | + * Action: upload packet to controller. | ||
144 | + * | ||
145 | + * @param exportPort export port of ovs | ||
146 | + * @param deviceId Device Id | ||
147 | + * @param type the operation type of the flow rules | ||
148 | + */ | ||
149 | + void programExportPortArpClassifierRules(Port exportPort, DeviceId deviceId, | ||
150 | + Operation type); | ||
137 | } | 151 | } | ... | ... |
... | @@ -79,6 +79,21 @@ public interface L2ForwardService { | ... | @@ -79,6 +79,21 @@ public interface L2ForwardService { |
79 | Objective.Operation type); | 79 | Objective.Operation type); |
80 | 80 | ||
81 | /** | 81 | /** |
82 | + * The external out rule that message matches Table(50). | ||
83 | + * Match: external port mac and vnid. | ||
84 | + * Action: output external port. | ||
85 | + * | ||
86 | + * @param deviceId Device Id | ||
87 | + * @param segmentationId the vnid of the host belong to | ||
88 | + * @param outPort the ingress port of the external port | ||
89 | + * @param sourceMac the mac of the external port | ||
90 | + * @param type the operation of the flow | ||
91 | + */ | ||
92 | + void programExternalOut(DeviceId deviceId, SegmentationId segmentationId, | ||
93 | + PortNumber outPort, MacAddress sourceMac, | ||
94 | + Objective.Operation type); | ||
95 | + | ||
96 | + /** | ||
82 | * The tunnel out rule that message matches Table(50). | 97 | * The tunnel out rule that message matches Table(50). |
83 | * Match: host mac and vnid. | 98 | * Match: host mac and vnid. |
84 | * Action: output tunnel port. | 99 | * Action: output tunnel port. | ... | ... |
... | @@ -16,8 +16,11 @@ | ... | @@ -16,8 +16,11 @@ |
16 | package org.onosproject.vtn.table; | 16 | package org.onosproject.vtn.table; |
17 | 17 | ||
18 | import org.onlab.packet.IpAddress; | 18 | import org.onlab.packet.IpAddress; |
19 | +import org.onlab.packet.IpPrefix; | ||
19 | import org.onlab.packet.MacAddress; | 20 | import org.onlab.packet.MacAddress; |
20 | import org.onosproject.net.DeviceId; | 21 | import org.onosproject.net.DeviceId; |
22 | +import org.onosproject.net.flow.TrafficSelector; | ||
23 | +import org.onosproject.net.flow.TrafficTreatment; | ||
21 | import org.onosproject.net.flowobjective.Objective; | 24 | import org.onosproject.net.flowobjective.Objective; |
22 | import org.onosproject.vtnrsc.SegmentationId; | 25 | import org.onosproject.vtnrsc.SegmentationId; |
23 | 26 | ||
... | @@ -30,6 +33,25 @@ public interface SnatService { | ... | @@ -30,6 +33,25 @@ public interface SnatService { |
30 | 33 | ||
31 | /** | 34 | /** |
32 | * Assemble the SNAT table rules. | 35 | * Assemble the SNAT table rules. |
36 | + * Match: ipv4 type, vnid, destination ip and source ip. | ||
37 | + * Action: set eth_src, set eth_dst, set ip_src, set vnid and goto L2Forward Table(50). | ||
38 | + * | ||
39 | + * @param deviceId Device Id | ||
40 | + * @param matchVni the vni of L3 network | ||
41 | + * @param srcIP source ip | ||
42 | + * @param dstIP destination ip | ||
43 | + * @param ethDst external gateway mac | ||
44 | + * @param ethSrc external port mac | ||
45 | + * @param ipSrc floating ip | ||
46 | + * @param actionVni external network VNI | ||
47 | + * @param type the operation type of the flow rules | ||
48 | + */ | ||
49 | + void programSnatSameSegmentRules(DeviceId deviceId, SegmentationId matchVni, | ||
50 | + IpAddress srcIP, IpAddress dstIP, MacAddress ethDst, | ||
51 | + MacAddress ethSrc, IpAddress ipSrc, | ||
52 | + SegmentationId actionVni, Objective.Operation type); | ||
53 | + /** | ||
54 | + * Assemble the SNAT table rules. | ||
33 | * Match: ipv4 type, vnid and source ip. | 55 | * Match: ipv4 type, vnid and source ip. |
34 | * Action: set eth_src, set eth_dst, set ip_src, set vnid and goto L2Forward Table(50). | 56 | * Action: set eth_src, set eth_dst, set ip_src, set vnid and goto L2Forward Table(50). |
35 | * | 57 | * |
... | @@ -42,8 +64,39 @@ public interface SnatService { | ... | @@ -42,8 +64,39 @@ public interface SnatService { |
42 | * @param actionVni external network VNI | 64 | * @param actionVni external network VNI |
43 | * @param type the operation type of the flow rules | 65 | * @param type the operation type of the flow rules |
44 | */ | 66 | */ |
45 | - void programRules(DeviceId deviceId, SegmentationId matchVni, | 67 | + void programSnatDiffSegmentRules(DeviceId deviceId, SegmentationId matchVni, |
46 | IpAddress srcIP, MacAddress ethDst, | 68 | IpAddress srcIP, MacAddress ethDst, |
47 | MacAddress ethSrc, IpAddress ipSrc, | 69 | MacAddress ethSrc, IpAddress ipSrc, |
48 | SegmentationId actionVni, Objective.Operation type); | 70 | SegmentationId actionVni, Objective.Operation type); |
71 | + | ||
72 | + /** | ||
73 | + * Assemble the SNAT table rules. | ||
74 | + * Match: ipv4 type, vnid, destination ip and source ip. | ||
75 | + * Action: upload to controller. | ||
76 | + * | ||
77 | + * @param deviceId Device Id | ||
78 | + * @param matchVni the vni of L3 network | ||
79 | + * @param srcIP source ip | ||
80 | + * @param dstIP destination ip | ||
81 | + * @param type the operation type of the flow rules | ||
82 | + */ | ||
83 | + void programSnatSameSegmentUploadControllerRules(DeviceId deviceId, | ||
84 | + SegmentationId matchVni, | ||
85 | + IpAddress srcIP, | ||
86 | + IpAddress dstIP, | ||
87 | + IpPrefix prefix, | ||
88 | + Objective.Operation type); | ||
89 | + | ||
90 | + /** | ||
91 | + * Remove the SNAT table rules. | ||
92 | + * | ||
93 | + * @param deviceId Device Id | ||
94 | + * @param selector selector of rules | ||
95 | + * @param treatment treatment of rules | ||
96 | + * @param priority priority of rules | ||
97 | + * @param type the operation type of the flow rules | ||
98 | + */ | ||
99 | + void removeSnatRules(DeviceId deviceId, TrafficSelector selector, | ||
100 | + TrafficTreatment treatment, int priority, | ||
101 | + Objective.Operation type); | ||
49 | } | 102 | } | ... | ... |
... | @@ -28,6 +28,7 @@ import org.onlab.packet.IpPrefix; | ... | @@ -28,6 +28,7 @@ import org.onlab.packet.IpPrefix; |
28 | import org.onlab.packet.MacAddress; | 28 | import org.onlab.packet.MacAddress; |
29 | import org.onosproject.core.ApplicationId; | 29 | import org.onosproject.core.ApplicationId; |
30 | import org.onosproject.net.DeviceId; | 30 | import org.onosproject.net.DeviceId; |
31 | +import org.onosproject.net.Port; | ||
31 | import org.onosproject.net.PortNumber; | 32 | import org.onosproject.net.PortNumber; |
32 | import org.onosproject.net.flow.DefaultTrafficSelector; | 33 | import org.onosproject.net.flow.DefaultTrafficSelector; |
33 | import org.onosproject.net.flow.DefaultTrafficTreatment; | 34 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
... | @@ -40,6 +41,7 @@ import org.onosproject.net.flowobjective.FlowObjectiveService; | ... | @@ -40,6 +41,7 @@ import org.onosproject.net.flowobjective.FlowObjectiveService; |
40 | import org.onosproject.net.flowobjective.ForwardingObjective; | 41 | import org.onosproject.net.flowobjective.ForwardingObjective; |
41 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; | 42 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; |
42 | import org.onosproject.net.flowobjective.Objective; | 43 | import org.onosproject.net.flowobjective.Objective; |
44 | +import org.onosproject.net.flowobjective.Objective.Operation; | ||
43 | import org.onosproject.vtn.table.ClassifierService; | 45 | import org.onosproject.vtn.table.ClassifierService; |
44 | import org.onosproject.vtnrsc.SegmentationId; | 46 | import org.onosproject.vtnrsc.SegmentationId; |
45 | import org.slf4j.Logger; | 47 | import org.slf4j.Logger; |
... | @@ -242,4 +244,24 @@ public class ClassifierServiceImpl implements ClassifierService { | ... | @@ -242,4 +244,24 @@ public class ClassifierServiceImpl implements ClassifierService { |
242 | flowObjectiveService.forward(deviceId, objective.remove()); | 244 | flowObjectiveService.forward(deviceId, objective.remove()); |
243 | } | 245 | } |
244 | } | 246 | } |
247 | + | ||
248 | + @Override | ||
249 | + public void programExportPortArpClassifierRules(Port exportPort, | ||
250 | + DeviceId deviceId, | ||
251 | + Operation type) { | ||
252 | + TrafficSelector selector = DefaultTrafficSelector.builder() | ||
253 | + .matchEthType(EtherType.ARP.ethType().toShort()) | ||
254 | + .matchInPort(exportPort.number()).build(); | ||
255 | + TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); | ||
256 | + treatment.add(Instructions.createOutput(PortNumber.CONTROLLER)); | ||
257 | + ForwardingObjective.Builder objective = DefaultForwardingObjective | ||
258 | + .builder().withTreatment(treatment.build()) | ||
259 | + .withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC) | ||
260 | + .withPriority(L3_CLASSIFIER_PRIORITY); | ||
261 | + if (type.equals(Objective.Operation.ADD)) { | ||
262 | + flowObjectiveService.forward(deviceId, objective.add()); | ||
263 | + } else { | ||
264 | + flowObjectiveService.forward(deviceId, objective.remove()); | ||
265 | + } | ||
266 | + } | ||
245 | } | 267 | } | ... | ... |
... | @@ -176,6 +176,28 @@ public final class L2ForwardServiceImpl implements L2ForwardService { | ... | @@ -176,6 +176,28 @@ public final class L2ForwardServiceImpl implements L2ForwardService { |
176 | } | 176 | } |
177 | 177 | ||
178 | @Override | 178 | @Override |
179 | + public void programExternalOut(DeviceId deviceId, | ||
180 | + SegmentationId segmentationId, | ||
181 | + PortNumber outPort, MacAddress sourceMac, | ||
182 | + Objective.Operation type) { | ||
183 | + TrafficSelector selector = DefaultTrafficSelector.builder() | ||
184 | + .matchTunnelId(Long.parseLong(segmentationId.toString())) | ||
185 | + .matchEthSrc(sourceMac).build(); | ||
186 | + TrafficTreatment treatment = DefaultTrafficTreatment.builder() | ||
187 | + .setOutput(outPort).build(); | ||
188 | + ForwardingObjective.Builder objective = DefaultForwardingObjective | ||
189 | + .builder().withTreatment(treatment).withSelector(selector) | ||
190 | + .fromApp(appId).withFlag(Flag.SPECIFIC) | ||
191 | + .withPriority(MAC_PRIORITY); | ||
192 | + if (type.equals(Objective.Operation.ADD)) { | ||
193 | + flowObjectiveService.forward(deviceId, objective.add()); | ||
194 | + } else { | ||
195 | + flowObjectiveService.forward(deviceId, objective.remove()); | ||
196 | + } | ||
197 | + | ||
198 | + } | ||
199 | + | ||
200 | + @Override | ||
179 | public void programTunnelOut(DeviceId deviceId, | 201 | public void programTunnelOut(DeviceId deviceId, |
180 | SegmentationId segmentationId, | 202 | SegmentationId segmentationId, |
181 | PortNumber tunnelOutPort, MacAddress dstMac, | 203 | PortNumber tunnelOutPort, MacAddress dstMac, | ... | ... |
... | @@ -16,7 +16,6 @@ | ... | @@ -16,7 +16,6 @@ |
16 | package org.onosproject.vtn.table.impl; | 16 | package org.onosproject.vtn.table.impl; |
17 | 17 | ||
18 | import static com.google.common.base.Preconditions.checkNotNull; | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
19 | -import static org.slf4j.LoggerFactory.getLogger; | ||
20 | 19 | ||
21 | import org.onlab.osgi.DefaultServiceDirectory; | 20 | import org.onlab.osgi.DefaultServiceDirectory; |
22 | import org.onlab.osgi.ServiceDirectory; | 21 | import org.onlab.osgi.ServiceDirectory; |
... | @@ -26,26 +25,29 @@ import org.onlab.packet.IpPrefix; | ... | @@ -26,26 +25,29 @@ import org.onlab.packet.IpPrefix; |
26 | import org.onlab.packet.MacAddress; | 25 | import org.onlab.packet.MacAddress; |
27 | import org.onosproject.core.ApplicationId; | 26 | import org.onosproject.core.ApplicationId; |
28 | import org.onosproject.net.DeviceId; | 27 | import org.onosproject.net.DeviceId; |
28 | +import org.onosproject.net.PortNumber; | ||
29 | import org.onosproject.net.flow.DefaultTrafficSelector; | 29 | import org.onosproject.net.flow.DefaultTrafficSelector; |
30 | import org.onosproject.net.flow.DefaultTrafficTreatment; | 30 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
31 | import org.onosproject.net.flow.TrafficSelector; | 31 | import org.onosproject.net.flow.TrafficSelector; |
32 | import org.onosproject.net.flow.TrafficTreatment; | 32 | import org.onosproject.net.flow.TrafficTreatment; |
33 | +import org.onosproject.net.flow.instructions.Instructions; | ||
33 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; | 34 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
34 | import org.onosproject.net.flowobjective.FlowObjectiveService; | 35 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
35 | import org.onosproject.net.flowobjective.ForwardingObjective; | 36 | import org.onosproject.net.flowobjective.ForwardingObjective; |
36 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; | 37 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; |
37 | import org.onosproject.net.flowobjective.Objective; | 38 | import org.onosproject.net.flowobjective.Objective; |
39 | +import org.onosproject.net.flowobjective.Objective.Operation; | ||
38 | import org.onosproject.vtn.table.SnatService; | 40 | import org.onosproject.vtn.table.SnatService; |
39 | import org.onosproject.vtnrsc.SegmentationId; | 41 | import org.onosproject.vtnrsc.SegmentationId; |
40 | -import org.slf4j.Logger; | ||
41 | 42 | ||
42 | /** | 43 | /** |
43 | * Provides implementation of SnatService. | 44 | * Provides implementation of SnatService. |
44 | */ | 45 | */ |
45 | public class SnatServiceImpl implements SnatService { | 46 | public class SnatServiceImpl implements SnatService { |
46 | - private final Logger log = getLogger(getClass()); | ||
47 | 47 | ||
48 | - private static final int SNAT_PRIORITY = 0xffff; | 48 | + private static final int SNAT_SAME_SEG_PRIORITY = 0xffff; |
49 | + private static final int SNAT_SAME_SEG_CON_PRIORITY = 0xfff0; | ||
50 | + private static final int SNAT_DIFF_SEG_PRIORITY = 0xffe0; | ||
49 | private static final int PREFIC_LENGTH = 32; | 51 | private static final int PREFIC_LENGTH = 32; |
50 | 52 | ||
51 | private final FlowObjectiveService flowObjectiveService; | 53 | private final FlowObjectiveService flowObjectiveService; |
... | @@ -63,7 +65,32 @@ public class SnatServiceImpl implements SnatService { | ... | @@ -63,7 +65,32 @@ public class SnatServiceImpl implements SnatService { |
63 | } | 65 | } |
64 | 66 | ||
65 | @Override | 67 | @Override |
66 | - public void programRules(DeviceId deviceId, SegmentationId matchVni, | 68 | + public void programSnatSameSegmentRules(DeviceId deviceId, SegmentationId matchVni, |
69 | + IpAddress srcIP, IpAddress dstIP, MacAddress ethDst, | ||
70 | + MacAddress ethSrc, IpAddress ipSrc, | ||
71 | + SegmentationId actionVni, Objective.Operation type) { | ||
72 | + TrafficSelector selector = DefaultTrafficSelector.builder() | ||
73 | + .matchEthType(Ethernet.TYPE_IPV4) | ||
74 | + .matchTunnelId(Long.parseLong(matchVni.segmentationId())) | ||
75 | + .matchIPSrc(IpPrefix.valueOf(srcIP, PREFIC_LENGTH)) | ||
76 | + .matchIPDst(IpPrefix.valueOf(dstIP, PREFIC_LENGTH)).build(); | ||
77 | + | ||
78 | + TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); | ||
79 | + treatment.setEthDst(ethDst).setEthSrc(ethSrc).setIpSrc(ipSrc) | ||
80 | + .setTunnelId(Long.parseLong(actionVni.segmentationId())); | ||
81 | + ForwardingObjective.Builder objective = DefaultForwardingObjective | ||
82 | + .builder().withTreatment(treatment.build()) | ||
83 | + .withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC) | ||
84 | + .withPriority(SNAT_SAME_SEG_PRIORITY); | ||
85 | + if (type.equals(Objective.Operation.ADD)) { | ||
86 | + flowObjectiveService.forward(deviceId, objective.add()); | ||
87 | + } else { | ||
88 | + flowObjectiveService.forward(deviceId, objective.remove()); | ||
89 | + } | ||
90 | + } | ||
91 | + | ||
92 | + @Override | ||
93 | + public void programSnatDiffSegmentRules(DeviceId deviceId, SegmentationId matchVni, | ||
67 | IpAddress srcIP, MacAddress ethDst, | 94 | IpAddress srcIP, MacAddress ethDst, |
68 | MacAddress ethSrc, IpAddress ipSrc, | 95 | MacAddress ethSrc, IpAddress ipSrc, |
69 | SegmentationId actionVni, Objective.Operation type) { | 96 | SegmentationId actionVni, Objective.Operation type) { |
... | @@ -78,12 +105,51 @@ public class SnatServiceImpl implements SnatService { | ... | @@ -78,12 +105,51 @@ public class SnatServiceImpl implements SnatService { |
78 | ForwardingObjective.Builder objective = DefaultForwardingObjective | 105 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
79 | .builder().withTreatment(treatment.build()) | 106 | .builder().withTreatment(treatment.build()) |
80 | .withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC) | 107 | .withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC) |
81 | - .withPriority(SNAT_PRIORITY); | 108 | + .withPriority(SNAT_DIFF_SEG_PRIORITY); |
109 | + if (type.equals(Objective.Operation.ADD)) { | ||
110 | + flowObjectiveService.forward(deviceId, objective.add()); | ||
111 | + } else { | ||
112 | + flowObjectiveService.forward(deviceId, objective.remove()); | ||
113 | + } | ||
114 | + } | ||
115 | + | ||
116 | + @Override | ||
117 | + public void programSnatSameSegmentUploadControllerRules(DeviceId deviceId, | ||
118 | + SegmentationId matchVni, | ||
119 | + IpAddress srcIP, | ||
120 | + IpAddress dstIP, | ||
121 | + IpPrefix prefix, | ||
122 | + Operation type) { | ||
123 | + | ||
124 | + TrafficSelector selector = DefaultTrafficSelector.builder() | ||
125 | + .matchEthType(Ethernet.TYPE_IPV4) | ||
126 | + .matchTunnelId(Long.parseLong(matchVni.segmentationId())) | ||
127 | + .matchIPSrc(IpPrefix.valueOf(srcIP, PREFIC_LENGTH)) | ||
128 | + .matchIPDst(IpPrefix.valueOf(dstIP, prefix.prefixLength())) | ||
129 | + .build(); | ||
130 | + TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); | ||
131 | + treatment.add(Instructions.createOutput(PortNumber.CONTROLLER)); | ||
132 | + ForwardingObjective.Builder objective = DefaultForwardingObjective | ||
133 | + .builder().withTreatment(treatment.build()) | ||
134 | + .withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC) | ||
135 | + .withPriority(SNAT_SAME_SEG_CON_PRIORITY); | ||
136 | + if (type.equals(Objective.Operation.ADD)) { | ||
137 | + flowObjectiveService.forward(deviceId, objective.add()); | ||
138 | + } else { | ||
139 | + flowObjectiveService.forward(deviceId, objective.remove()); | ||
140 | + } | ||
141 | + } | ||
142 | + | ||
143 | + @Override | ||
144 | + public void removeSnatRules(DeviceId deviceId, TrafficSelector selector, | ||
145 | + TrafficTreatment treatment, int priority, | ||
146 | + Objective.Operation type) { | ||
147 | + ForwardingObjective.Builder objective = DefaultForwardingObjective | ||
148 | + .builder().withTreatment(treatment).withSelector(selector) | ||
149 | + .fromApp(appId).withFlag(Flag.SPECIFIC).withPriority(priority); | ||
82 | if (type.equals(Objective.Operation.ADD)) { | 150 | if (type.equals(Objective.Operation.ADD)) { |
83 | - log.debug("RouteRules-->ADD"); | ||
84 | flowObjectiveService.forward(deviceId, objective.add()); | 151 | flowObjectiveService.forward(deviceId, objective.add()); |
85 | } else { | 152 | } else { |
86 | - log.debug("RouteRules-->REMOVE"); | ||
87 | flowObjectiveService.forward(deviceId, objective.remove()); | 153 | flowObjectiveService.forward(deviceId, objective.remove()); |
88 | } | 154 | } |
89 | } | 155 | } | ... | ... |
1 | +/* | ||
2 | + * Copyright 2016-present Open Networking Laboratory | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.onosproject.vtn.util; | ||
17 | + | ||
18 | +import org.onlab.packet.IpAddress; | ||
19 | + | ||
20 | +/** | ||
21 | + * IpUtil utility class. | ||
22 | + */ | ||
23 | +public final class IpUtil { | ||
24 | + | ||
25 | + private IpUtil() { | ||
26 | + } | ||
27 | + | ||
28 | + /** | ||
29 | + * check source Ip and destination Ip in same Subnet. | ||
30 | + * | ||
31 | + * @param srcIp source Ip | ||
32 | + * @param dstIp destination | ||
33 | + * @param masks netmask length | ||
34 | + * @return boolean | ||
35 | + */ | ||
36 | + public static boolean checkSameSegment(IpAddress srcIp, IpAddress dstIp, | ||
37 | + int mask) { | ||
38 | + String[] ips = srcIp.toString().split("\\."); | ||
39 | + int ipAddr = (Integer.parseInt(ips[0]) << 24) | ||
40 | + | (Integer.parseInt(ips[1]) << 16) | ||
41 | + | (Integer.parseInt(ips[2]) << 8) | ||
42 | + | Integer.parseInt(ips[3]); | ||
43 | + int netmask = 0xFFFFFFFF << (32 - mask); | ||
44 | + String[] cidrIps = dstIp.toString().split("\\."); | ||
45 | + int cidrIpAddr = (Integer.parseInt(cidrIps[0]) << 24) | ||
46 | + | (Integer.parseInt(cidrIps[1]) << 16) | ||
47 | + | (Integer.parseInt(cidrIps[2]) << 8) | ||
48 | + | Integer.parseInt(cidrIps[3]); | ||
49 | + | ||
50 | + return (ipAddr & netmask) == (cidrIpAddr & netmask); | ||
51 | + } | ||
52 | +} |
-
Please register or login to post a comment