blob: 8b44938942d724b4594f91416402d858ffe7cadc [file] [log] [blame]
lishuaieb749502015-11-26 20:07:35 +08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
lishuaieb749502015-11-26 20:07:35 +08003 *
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 */
16package org.onosproject.vtn.table.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
lishuaieb749502015-11-26 20:07:35 +080019
20import org.onlab.osgi.DefaultServiceDirectory;
21import org.onlab.osgi.ServiceDirectory;
22import org.onlab.packet.Ethernet;
23import org.onlab.packet.IpAddress;
24import org.onlab.packet.IpPrefix;
25import org.onlab.packet.MacAddress;
26import org.onosproject.core.ApplicationId;
27import org.onosproject.net.DeviceId;
Bob zhou59a21062016-05-12 19:40:05 +080028import org.onosproject.net.PortNumber;
lishuaieb749502015-11-26 20:07:35 +080029import org.onosproject.net.flow.DefaultTrafficSelector;
30import org.onosproject.net.flow.DefaultTrafficTreatment;
31import org.onosproject.net.flow.TrafficSelector;
32import org.onosproject.net.flow.TrafficTreatment;
Bob zhou59a21062016-05-12 19:40:05 +080033import org.onosproject.net.flow.instructions.Instructions;
lishuaieb749502015-11-26 20:07:35 +080034import org.onosproject.net.flowobjective.DefaultForwardingObjective;
35import org.onosproject.net.flowobjective.FlowObjectiveService;
36import org.onosproject.net.flowobjective.ForwardingObjective;
37import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
38import org.onosproject.net.flowobjective.Objective;
Bob zhou59a21062016-05-12 19:40:05 +080039import org.onosproject.net.flowobjective.Objective.Operation;
lishuaieb749502015-11-26 20:07:35 +080040import org.onosproject.vtn.table.SnatService;
41import org.onosproject.vtnrsc.SegmentationId;
lishuaieb749502015-11-26 20:07:35 +080042
43/**
44 * Provides implementation of SnatService.
45 */
46public class SnatServiceImpl implements SnatService {
lishuaieb749502015-11-26 20:07:35 +080047
Bob zhou59a21062016-05-12 19:40:05 +080048 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;
lishuaieb749502015-11-26 20:07:35 +080051 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 zhou59a21062016-05-12 19:40:05 +080068 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,
lishuaieb749502015-11-26 20:07:35 +080094 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 zhou59a21062016-05-12 19:40:05 +0800108 .withPriority(SNAT_DIFF_SEG_PRIORITY);
lishuaieb749502015-11-26 20:07:35 +0800109 if (type.equals(Objective.Operation.ADD)) {
lishuaieb749502015-11-26 20:07:35 +0800110 flowObjectiveService.forward(deviceId, objective.add());
111 } else {
Bob zhou59a21062016-05-12 19:40:05 +0800112 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 {
lishuaieb749502015-11-26 20:07:35 +0800153 flowObjectiveService.forward(deviceId, objective.remove());
154 }
155 }
156}