blob: 5222aa8637e6a5d4e6b32edb1bc768ae3125fdf4 [file] [log] [blame]
lishuai6c56f5e2015-11-17 16:38:19 +08001/*
2 * Copyright 2015 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 */
16package org.onosproject.vtn.table.impl;
17
18import static com.google.common.base.Preconditions.checkNotNull;
19import static org.slf4j.LoggerFactory.getLogger;
20
21import org.onlab.osgi.DefaultServiceDirectory;
22import org.onlab.osgi.ServiceDirectory;
lishuaiaca60262015-11-26 19:48:01 +080023import org.onlab.packet.EthType.EtherType;
lishuai9e4e43a2015-11-26 19:30:55 +080024import org.onlab.packet.Ethernet;
lishuaiaca60262015-11-26 19:48:01 +080025import org.onlab.packet.Ip4Address;
lishuai7547db42015-11-20 14:56:11 +080026import org.onlab.packet.IpAddress;
lishuai9e4e43a2015-11-26 19:30:55 +080027import org.onlab.packet.IpPrefix;
lishuai6c56f5e2015-11-17 16:38:19 +080028import org.onlab.packet.MacAddress;
29import org.onosproject.core.ApplicationId;
30import org.onosproject.net.DeviceId;
31import org.onosproject.net.PortNumber;
32import org.onosproject.net.flow.DefaultTrafficSelector;
33import org.onosproject.net.flow.DefaultTrafficTreatment;
34import org.onosproject.net.flow.TrafficSelector;
35import org.onosproject.net.flow.TrafficTreatment;
36import org.onosproject.net.flow.criteria.Criteria;
37import org.onosproject.net.flow.instructions.Instructions;
38import org.onosproject.net.flowobjective.DefaultForwardingObjective;
39import org.onosproject.net.flowobjective.FlowObjectiveService;
40import org.onosproject.net.flowobjective.ForwardingObjective;
41import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
42import org.onosproject.net.flowobjective.Objective;
43import org.onosproject.vtn.table.ClassifierService;
44import org.onosproject.vtnrsc.SegmentationId;
45import org.slf4j.Logger;
46
47import com.google.common.collect.Sets;
48
49/**
50 * Provides implementation of ClassifierService.
51 */
52public class ClassifierServiceImpl implements ClassifierService {
53 private final Logger log = getLogger(getClass());
54
lishuaiaca60262015-11-26 19:48:01 +080055 private static final EtherType ETH_TYPE = EtherType.ARP;
56 private static final int ARP_CLAFFIFIER_PRIORITY = 60000;
lishuai9e4e43a2015-11-26 19:30:55 +080057 private static final int L3_CLAFFIFIER_PRIORITY = 0xffff;
lishuai6c56f5e2015-11-17 16:38:19 +080058 private static final int L2_CLAFFIFIER_PRIORITY = 50000;
59
60 private final FlowObjectiveService flowObjectiveService;
61 private final ApplicationId appId;
62
63 /**
64 * Constructor.
65 *
66 * @param appId the application id of vtn
67 */
68 public ClassifierServiceImpl(ApplicationId appId) {
69 this.appId = checkNotNull(appId, "ApplicationId can not be null");
70 ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
71 this.flowObjectiveService = serviceDirectory.get(FlowObjectiveService.class);
72 }
73
74 @Override
75 public void programLocalIn(DeviceId deviceId,
76 SegmentationId segmentationId, PortNumber inPort,
77 MacAddress srcMac, ApplicationId appid,
78 Objective.Operation type) {
79 TrafficSelector selector = DefaultTrafficSelector.builder()
80 .matchInPort(inPort).matchEthSrc(srcMac).build();
81 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
82 treatment.add(Instructions
83 .modTunnelId(Long.parseLong(segmentationId.toString())));
84 ForwardingObjective.Builder objective = DefaultForwardingObjective
85 .builder().withTreatment(treatment.build())
86 .withSelector(selector).fromApp(appId).makePermanent()
87 .withFlag(Flag.SPECIFIC).withPriority(L2_CLAFFIFIER_PRIORITY);
88 if (type.equals(Objective.Operation.ADD)) {
89 log.debug("programLocalIn-->ADD");
90 flowObjectiveService.forward(deviceId, objective.add());
91 } else {
92 log.debug("programLocalIn-->REMOVE");
93 flowObjectiveService.forward(deviceId, objective.remove());
94 }
95 }
96
97 @Override
98 public void programTunnelIn(DeviceId deviceId,
99 SegmentationId segmentationId,
100 Iterable<PortNumber> localTunnelPorts,
101 Objective.Operation type) {
102 if (localTunnelPorts == null) {
103 log.info("No tunnel port in device");
104 return;
105 }
106 Sets.newHashSet(localTunnelPorts).stream().forEach(tp -> {
107 TrafficSelector selector = DefaultTrafficSelector.builder()
108 .matchInPort(tp).add(Criteria.matchTunnelId(Long
109 .parseLong(segmentationId.toString())))
110 .build();
111
112 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
113 .build();
114 ForwardingObjective.Builder objective = DefaultForwardingObjective
115 .builder().withTreatment(treatment).withSelector(selector)
116 .fromApp(appId).makePermanent().withFlag(Flag.SPECIFIC)
117 .withPriority(L2_CLAFFIFIER_PRIORITY);
118 if (type.equals(Objective.Operation.ADD)) {
119 log.debug("programTunnelIn-->ADD");
120 flowObjectiveService.forward(deviceId, objective.add());
121 } else {
122 log.debug("programTunnelIn-->REMOVE");
123 flowObjectiveService.forward(deviceId, objective.remove());
124 }
125 });
126 }
127
lishuai7547db42015-11-20 14:56:11 +0800128 @Override
lishuai9e4e43a2015-11-26 19:30:55 +0800129 public void programL3ExPortClassifierRules(DeviceId deviceId, PortNumber inPort,
130 IpAddress dstIp,
131 Objective.Operation type) {
132 TrafficSelector selector = DefaultTrafficSelector.builder()
133 .matchEthType(Ethernet.TYPE_IPV4).matchInPort(inPort)
134 .matchIPDst(IpPrefix.valueOf(dstIp, 32)).build();
135 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
136 ForwardingObjective.Builder objective = DefaultForwardingObjective
137 .builder().withTreatment(treatment).withSelector(selector)
138 .fromApp(appId).withFlag(Flag.SPECIFIC)
139 .withPriority(L3_CLAFFIFIER_PRIORITY);
140 if (type.equals(Objective.Operation.ADD)) {
141 log.debug("L3ExToInClassifierRules-->ADD");
142 flowObjectiveService.forward(deviceId, objective.add());
143 } else {
144 log.debug("L3ExToInClassifierRules-->REMOVE");
145 flowObjectiveService.forward(deviceId, objective.remove());
146 }
lishuai7547db42015-11-20 14:56:11 +0800147 }
148
149 @Override
lishuai478d02e2015-11-26 19:33:41 +0800150 public void programL3InPortClassifierRules(DeviceId deviceId, PortNumber inPort,
151 MacAddress srcMac, MacAddress dstMac,
152 SegmentationId actionVni,
153 Objective.Operation type) {
154 TrafficSelector selector = DefaultTrafficSelector.builder()
155 .matchInPort(inPort).matchEthSrc(srcMac).matchEthDst(dstMac)
156 .build();
157 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
158 .setTunnelId(Long.parseLong(actionVni.segmentationId())).build();
159 ForwardingObjective.Builder objective = DefaultForwardingObjective
160 .builder().withTreatment(treatment).withSelector(selector)
161 .fromApp(appId).withFlag(Flag.SPECIFIC)
162 .withPriority(L3_CLAFFIFIER_PRIORITY);
163 if (type.equals(Objective.Operation.ADD)) {
164 log.debug("L3InternalClassifierRules-->ADD");
165 flowObjectiveService.forward(deviceId, objective.add());
166 } else {
167 log.debug("L3InternalClassifierRules-->REMOVE");
168 flowObjectiveService.forward(deviceId, objective.remove());
169 }
lishuai7547db42015-11-20 14:56:11 +0800170 }
171
172 @Override
lishuaiaca60262015-11-26 19:48:01 +0800173 public void programArpClassifierRules(DeviceId deviceId, IpAddress dstIp,
174 SegmentationId actionVni,
175 Objective.Operation type) {
176 TrafficSelector selector = DefaultTrafficSelector.builder()
177 .matchEthType(ETH_TYPE.ethType().toShort())
178 .matchArpTpa(Ip4Address.valueOf(dstIp.toString()))
179 .build();
180 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
181 .setTunnelId(Long.parseLong(actionVni.segmentationId()))
182 .build();
183 ForwardingObjective.Builder objective = DefaultForwardingObjective
184 .builder().withTreatment(treatment).withSelector(selector)
185 .fromApp(appId).withFlag(Flag.SPECIFIC)
186 .withPriority(ARP_CLAFFIFIER_PRIORITY);
187 if (type.equals(Objective.Operation.ADD)) {
188 log.debug("ArpClassifierRules-->ADD");
189 flowObjectiveService.forward(deviceId, objective.add());
190 } else {
191 log.debug("ArpClassifierRules-->REMOVE");
192 flowObjectiveService.forward(deviceId, objective.remove());
193 }
lishuai7547db42015-11-20 14:56:11 +0800194 }
195
lishuai6c56f5e2015-11-17 16:38:19 +0800196}