Bob zhou

[ONOS-4426] Upgrade Vtn Module when access same network segment

Change-Id: Id0d00e9d0e93d1baf4ff20560469316fee5a3186
......@@ -20,8 +20,10 @@ import org.onlab.packet.IpPrefix;
import org.onlab.packet.MacAddress;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
import org.onosproject.net.flowobjective.Objective;
import org.onosproject.net.flowobjective.Objective.Operation;
import org.onosproject.vtnrsc.SegmentationId;
/**
......@@ -134,4 +136,16 @@ public interface ClassifierService {
IpAddress dstIp, MacAddress dstmac,
SegmentationId actionVni,
Objective.Operation type);
/**
* Assemble the export port Arp Classifier table rules.
* Match: export port.
* Action: upload packet to controller.
*
* @param exportPort export port of ovs
* @param deviceId Device Id
* @param type the operation type of the flow rules
*/
void programExportPortArpClassifierRules(Port exportPort, DeviceId deviceId,
Operation type);
}
......
......@@ -79,6 +79,21 @@ public interface L2ForwardService {
Objective.Operation type);
/**
* The external out rule that message matches Table(50).
* Match: external port mac and vnid.
* Action: output external port.
*
* @param deviceId Device Id
* @param segmentationId the vnid of the host belong to
* @param outPort the ingress port of the external port
* @param sourceMac the mac of the external port
* @param type the operation of the flow
*/
void programExternalOut(DeviceId deviceId, SegmentationId segmentationId,
PortNumber outPort, MacAddress sourceMac,
Objective.Operation type);
/**
* The tunnel out rule that message matches Table(50).
* Match: host mac and vnid.
* Action: output tunnel port.
......
......@@ -16,8 +16,11 @@
package org.onosproject.vtn.table;
import org.onlab.packet.IpAddress;
import org.onlab.packet.IpPrefix;
import org.onlab.packet.MacAddress;
import org.onosproject.net.DeviceId;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.flowobjective.Objective;
import org.onosproject.vtnrsc.SegmentationId;
......@@ -30,6 +33,25 @@ public interface SnatService {
/**
* Assemble the SNAT table rules.
* Match: ipv4 type, vnid, destination ip and source ip.
* Action: set eth_src, set eth_dst, set ip_src, set vnid and goto L2Forward Table(50).
*
* @param deviceId Device Id
* @param matchVni the vni of L3 network
* @param srcIP source ip
* @param dstIP destination ip
* @param ethDst external gateway mac
* @param ethSrc external port mac
* @param ipSrc floating ip
* @param actionVni external network VNI
* @param type the operation type of the flow rules
*/
void programSnatSameSegmentRules(DeviceId deviceId, SegmentationId matchVni,
IpAddress srcIP, IpAddress dstIP, MacAddress ethDst,
MacAddress ethSrc, IpAddress ipSrc,
SegmentationId actionVni, Objective.Operation type);
/**
* Assemble the SNAT table rules.
* Match: ipv4 type, vnid and source ip.
* Action: set eth_src, set eth_dst, set ip_src, set vnid and goto L2Forward Table(50).
*
......@@ -42,8 +64,39 @@ public interface SnatService {
* @param actionVni external network VNI
* @param type the operation type of the flow rules
*/
void programRules(DeviceId deviceId, SegmentationId matchVni,
void programSnatDiffSegmentRules(DeviceId deviceId, SegmentationId matchVni,
IpAddress srcIP, MacAddress ethDst,
MacAddress ethSrc, IpAddress ipSrc,
SegmentationId actionVni, Objective.Operation type);
/**
* Assemble the SNAT table rules.
* Match: ipv4 type, vnid, destination ip and source ip.
* Action: upload to controller.
*
* @param deviceId Device Id
* @param matchVni the vni of L3 network
* @param srcIP source ip
* @param dstIP destination ip
* @param type the operation type of the flow rules
*/
void programSnatSameSegmentUploadControllerRules(DeviceId deviceId,
SegmentationId matchVni,
IpAddress srcIP,
IpAddress dstIP,
IpPrefix prefix,
Objective.Operation type);
/**
* Remove the SNAT table rules.
*
* @param deviceId Device Id
* @param selector selector of rules
* @param treatment treatment of rules
* @param priority priority of rules
* @param type the operation type of the flow rules
*/
void removeSnatRules(DeviceId deviceId, TrafficSelector selector,
TrafficTreatment treatment, int priority,
Objective.Operation type);
}
......
......@@ -28,6 +28,7 @@ import org.onlab.packet.IpPrefix;
import org.onlab.packet.MacAddress;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.DeviceId;
import org.onosproject.net.Port;
import org.onosproject.net.PortNumber;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
......@@ -40,6 +41,7 @@ import org.onosproject.net.flowobjective.FlowObjectiveService;
import org.onosproject.net.flowobjective.ForwardingObjective;
import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
import org.onosproject.net.flowobjective.Objective;
import org.onosproject.net.flowobjective.Objective.Operation;
import org.onosproject.vtn.table.ClassifierService;
import org.onosproject.vtnrsc.SegmentationId;
import org.slf4j.Logger;
......@@ -242,4 +244,24 @@ public class ClassifierServiceImpl implements ClassifierService {
flowObjectiveService.forward(deviceId, objective.remove());
}
}
@Override
public void programExportPortArpClassifierRules(Port exportPort,
DeviceId deviceId,
Operation type) {
TrafficSelector selector = DefaultTrafficSelector.builder()
.matchEthType(EtherType.ARP.ethType().toShort())
.matchInPort(exportPort.number()).build();
TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
treatment.add(Instructions.createOutput(PortNumber.CONTROLLER));
ForwardingObjective.Builder objective = DefaultForwardingObjective
.builder().withTreatment(treatment.build())
.withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC)
.withPriority(L3_CLASSIFIER_PRIORITY);
if (type.equals(Objective.Operation.ADD)) {
flowObjectiveService.forward(deviceId, objective.add());
} else {
flowObjectiveService.forward(deviceId, objective.remove());
}
}
}
......
......@@ -176,6 +176,28 @@ public final class L2ForwardServiceImpl implements L2ForwardService {
}
@Override
public void programExternalOut(DeviceId deviceId,
SegmentationId segmentationId,
PortNumber outPort, MacAddress sourceMac,
Objective.Operation type) {
TrafficSelector selector = DefaultTrafficSelector.builder()
.matchTunnelId(Long.parseLong(segmentationId.toString()))
.matchEthSrc(sourceMac).build();
TrafficTreatment treatment = DefaultTrafficTreatment.builder()
.setOutput(outPort).build();
ForwardingObjective.Builder objective = DefaultForwardingObjective
.builder().withTreatment(treatment).withSelector(selector)
.fromApp(appId).withFlag(Flag.SPECIFIC)
.withPriority(MAC_PRIORITY);
if (type.equals(Objective.Operation.ADD)) {
flowObjectiveService.forward(deviceId, objective.add());
} else {
flowObjectiveService.forward(deviceId, objective.remove());
}
}
@Override
public void programTunnelOut(DeviceId deviceId,
SegmentationId segmentationId,
PortNumber tunnelOutPort, MacAddress dstMac,
......
......@@ -16,7 +16,6 @@
package org.onosproject.vtn.table.impl;
import static com.google.common.base.Preconditions.checkNotNull;
import static org.slf4j.LoggerFactory.getLogger;
import org.onlab.osgi.DefaultServiceDirectory;
import org.onlab.osgi.ServiceDirectory;
......@@ -26,26 +25,29 @@ import org.onlab.packet.IpPrefix;
import org.onlab.packet.MacAddress;
import org.onosproject.core.ApplicationId;
import org.onosproject.net.DeviceId;
import org.onosproject.net.PortNumber;
import org.onosproject.net.flow.DefaultTrafficSelector;
import org.onosproject.net.flow.DefaultTrafficTreatment;
import org.onosproject.net.flow.TrafficSelector;
import org.onosproject.net.flow.TrafficTreatment;
import org.onosproject.net.flow.instructions.Instructions;
import org.onosproject.net.flowobjective.DefaultForwardingObjective;
import org.onosproject.net.flowobjective.FlowObjectiveService;
import org.onosproject.net.flowobjective.ForwardingObjective;
import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
import org.onosproject.net.flowobjective.Objective;
import org.onosproject.net.flowobjective.Objective.Operation;
import org.onosproject.vtn.table.SnatService;
import org.onosproject.vtnrsc.SegmentationId;
import org.slf4j.Logger;
/**
* Provides implementation of SnatService.
*/
public class SnatServiceImpl implements SnatService {
private final Logger log = getLogger(getClass());
private static final int SNAT_PRIORITY = 0xffff;
private static final int SNAT_SAME_SEG_PRIORITY = 0xffff;
private static final int SNAT_SAME_SEG_CON_PRIORITY = 0xfff0;
private static final int SNAT_DIFF_SEG_PRIORITY = 0xffe0;
private static final int PREFIC_LENGTH = 32;
private final FlowObjectiveService flowObjectiveService;
......@@ -63,7 +65,32 @@ public class SnatServiceImpl implements SnatService {
}
@Override
public void programRules(DeviceId deviceId, SegmentationId matchVni,
public void programSnatSameSegmentRules(DeviceId deviceId, SegmentationId matchVni,
IpAddress srcIP, IpAddress dstIP, MacAddress ethDst,
MacAddress ethSrc, IpAddress ipSrc,
SegmentationId actionVni, Objective.Operation type) {
TrafficSelector selector = DefaultTrafficSelector.builder()
.matchEthType(Ethernet.TYPE_IPV4)
.matchTunnelId(Long.parseLong(matchVni.segmentationId()))
.matchIPSrc(IpPrefix.valueOf(srcIP, PREFIC_LENGTH))
.matchIPDst(IpPrefix.valueOf(dstIP, PREFIC_LENGTH)).build();
TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
treatment.setEthDst(ethDst).setEthSrc(ethSrc).setIpSrc(ipSrc)
.setTunnelId(Long.parseLong(actionVni.segmentationId()));
ForwardingObjective.Builder objective = DefaultForwardingObjective
.builder().withTreatment(treatment.build())
.withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC)
.withPriority(SNAT_SAME_SEG_PRIORITY);
if (type.equals(Objective.Operation.ADD)) {
flowObjectiveService.forward(deviceId, objective.add());
} else {
flowObjectiveService.forward(deviceId, objective.remove());
}
}
@Override
public void programSnatDiffSegmentRules(DeviceId deviceId, SegmentationId matchVni,
IpAddress srcIP, MacAddress ethDst,
MacAddress ethSrc, IpAddress ipSrc,
SegmentationId actionVni, Objective.Operation type) {
......@@ -78,12 +105,51 @@ public class SnatServiceImpl implements SnatService {
ForwardingObjective.Builder objective = DefaultForwardingObjective
.builder().withTreatment(treatment.build())
.withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC)
.withPriority(SNAT_PRIORITY);
.withPriority(SNAT_DIFF_SEG_PRIORITY);
if (type.equals(Objective.Operation.ADD)) {
flowObjectiveService.forward(deviceId, objective.add());
} else {
flowObjectiveService.forward(deviceId, objective.remove());
}
}
@Override
public void programSnatSameSegmentUploadControllerRules(DeviceId deviceId,
SegmentationId matchVni,
IpAddress srcIP,
IpAddress dstIP,
IpPrefix prefix,
Operation type) {
TrafficSelector selector = DefaultTrafficSelector.builder()
.matchEthType(Ethernet.TYPE_IPV4)
.matchTunnelId(Long.parseLong(matchVni.segmentationId()))
.matchIPSrc(IpPrefix.valueOf(srcIP, PREFIC_LENGTH))
.matchIPDst(IpPrefix.valueOf(dstIP, prefix.prefixLength()))
.build();
TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
treatment.add(Instructions.createOutput(PortNumber.CONTROLLER));
ForwardingObjective.Builder objective = DefaultForwardingObjective
.builder().withTreatment(treatment.build())
.withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC)
.withPriority(SNAT_SAME_SEG_CON_PRIORITY);
if (type.equals(Objective.Operation.ADD)) {
flowObjectiveService.forward(deviceId, objective.add());
} else {
flowObjectiveService.forward(deviceId, objective.remove());
}
}
@Override
public void removeSnatRules(DeviceId deviceId, TrafficSelector selector,
TrafficTreatment treatment, int priority,
Objective.Operation type) {
ForwardingObjective.Builder objective = DefaultForwardingObjective
.builder().withTreatment(treatment).withSelector(selector)
.fromApp(appId).withFlag(Flag.SPECIFIC).withPriority(priority);
if (type.equals(Objective.Operation.ADD)) {
log.debug("RouteRules-->ADD");
flowObjectiveService.forward(deviceId, objective.add());
} else {
log.debug("RouteRules-->REMOVE");
flowObjectiveService.forward(deviceId, objective.remove());
}
}
......
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.onosproject.vtn.util;
import org.onlab.packet.IpAddress;
/**
* IpUtil utility class.
*/
public final class IpUtil {
private IpUtil() {
}
/**
* check source Ip and destination Ip in same Subnet.
*
* @param srcIp source Ip
* @param dstIp destination
* @param masks netmask length
* @return boolean
*/
public static boolean checkSameSegment(IpAddress srcIp, IpAddress dstIp,
int mask) {
String[] ips = srcIp.toString().split("\\.");
int ipAddr = (Integer.parseInt(ips[0]) << 24)
| (Integer.parseInt(ips[1]) << 16)
| (Integer.parseInt(ips[2]) << 8)
| Integer.parseInt(ips[3]);
int netmask = 0xFFFFFFFF << (32 - mask);
String[] cidrIps = dstIp.toString().split("\\.");
int cidrIpAddr = (Integer.parseInt(cidrIps[0]) << 24)
| (Integer.parseInt(cidrIps[1]) << 16)
| (Integer.parseInt(cidrIps[2]) << 8)
| Integer.parseInt(cidrIps[3]);
return (ipAddr & netmask) == (cidrIpAddr & netmask);
}
}