lishuai | eb74950 | 2015-11-26 20:07:35 +0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Foundation |
lishuai | eb74950 | 2015-11-26 20:07:35 +0800 | [diff] [blame] | 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.table.impl; |
| 17 | |
| 18 | import static com.google.common.base.Preconditions.checkNotNull; |
lishuai | eb74950 | 2015-11-26 20:07:35 +0800 | [diff] [blame] | 19 | |
| 20 | import org.onlab.osgi.DefaultServiceDirectory; |
| 21 | import org.onlab.osgi.ServiceDirectory; |
| 22 | import org.onlab.packet.Ethernet; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onlab.packet.IpPrefix; |
| 25 | import org.onlab.packet.MacAddress; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.net.DeviceId; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 28 | import org.onosproject.net.PortNumber; |
lishuai | eb74950 | 2015-11-26 20:07:35 +0800 | [diff] [blame] | 29 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 30 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 31 | import org.onosproject.net.flow.TrafficSelector; |
| 32 | import org.onosproject.net.flow.TrafficTreatment; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 33 | import org.onosproject.net.flow.instructions.Instructions; |
lishuai | eb74950 | 2015-11-26 20:07:35 +0800 | [diff] [blame] | 34 | import org.onosproject.net.flowobjective.DefaultForwardingObjective; |
| 35 | import org.onosproject.net.flowobjective.FlowObjectiveService; |
| 36 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 37 | import org.onosproject.net.flowobjective.ForwardingObjective.Flag; |
| 38 | import org.onosproject.net.flowobjective.Objective; |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 39 | import org.onosproject.net.flowobjective.Objective.Operation; |
lishuai | eb74950 | 2015-11-26 20:07:35 +0800 | [diff] [blame] | 40 | import org.onosproject.vtn.table.SnatService; |
| 41 | import org.onosproject.vtnrsc.SegmentationId; |
lishuai | eb74950 | 2015-11-26 20:07:35 +0800 | [diff] [blame] | 42 | |
| 43 | /** |
| 44 | * Provides implementation of SnatService. |
| 45 | */ |
| 46 | public class SnatServiceImpl implements SnatService { |
lishuai | eb74950 | 2015-11-26 20:07:35 +0800 | [diff] [blame] | 47 | |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 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; |
lishuai | eb74950 | 2015-11-26 20:07:35 +0800 | [diff] [blame] | 51 | private static final int PREFIC_LENGTH = 32; |
| 52 | |
| 53 | private final FlowObjectiveService flowObjectiveService; |
| 54 | private final ApplicationId appId; |
| 55 | |
| 56 | /** |
| 57 | * Construct a SnatServiceImpl object. |
| 58 | * |
| 59 | * @param appId the application id of vtn |
| 60 | */ |
| 61 | public SnatServiceImpl(ApplicationId appId) { |
| 62 | this.appId = checkNotNull(appId, "ApplicationId can not be null"); |
| 63 | ServiceDirectory serviceDirectory = new DefaultServiceDirectory(); |
| 64 | this.flowObjectiveService = serviceDirectory.get(FlowObjectiveService.class); |
| 65 | } |
| 66 | |
| 67 | @Override |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 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, |
lishuai | eb74950 | 2015-11-26 20:07:35 +0800 | [diff] [blame] | 94 | IpAddress srcIP, MacAddress ethDst, |
| 95 | MacAddress ethSrc, IpAddress ipSrc, |
| 96 | SegmentationId actionVni, Objective.Operation type) { |
| 97 | TrafficSelector selector = DefaultTrafficSelector.builder() |
| 98 | .matchEthType(Ethernet.TYPE_IPV4) |
| 99 | .matchTunnelId(Long.parseLong(matchVni.segmentationId())) |
| 100 | .matchIPSrc(IpPrefix.valueOf(srcIP, PREFIC_LENGTH)).build(); |
| 101 | |
| 102 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 103 | treatment.setEthDst(ethDst).setEthSrc(ethSrc).setIpSrc(ipSrc) |
| 104 | .setTunnelId(Long.parseLong(actionVni.segmentationId())); |
| 105 | ForwardingObjective.Builder objective = DefaultForwardingObjective |
| 106 | .builder().withTreatment(treatment.build()) |
| 107 | .withSelector(selector).fromApp(appId).withFlag(Flag.SPECIFIC) |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 108 | .withPriority(SNAT_DIFF_SEG_PRIORITY); |
lishuai | eb74950 | 2015-11-26 20:07:35 +0800 | [diff] [blame] | 109 | if (type.equals(Objective.Operation.ADD)) { |
lishuai | eb74950 | 2015-11-26 20:07:35 +0800 | [diff] [blame] | 110 | flowObjectiveService.forward(deviceId, objective.add()); |
| 111 | } else { |
Bob zhou | 59a2106 | 2016-05-12 19:40:05 +0800 | [diff] [blame] | 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); |
| 150 | if (type.equals(Objective.Operation.ADD)) { |
| 151 | flowObjectiveService.forward(deviceId, objective.add()); |
| 152 | } else { |
lishuai | eb74950 | 2015-11-26 20:07:35 +0800 | [diff] [blame] | 153 | flowObjectiveService.forward(deviceId, objective.remove()); |
| 154 | } |
| 155 | } |
| 156 | } |