blob: b27a917a2fba8b5a516afcfbebc3fc87246caff6 [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;
lishuai9e4e43a2015-11-26 19:30:55 +080023import org.onlab.packet.Ethernet;
lishuai7547db42015-11-20 14:56:11 +080024import org.onlab.packet.IpAddress;
lishuai9e4e43a2015-11-26 19:30:55 +080025import org.onlab.packet.IpPrefix;
lishuai6c56f5e2015-11-17 16:38:19 +080026import org.onlab.packet.MacAddress;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.net.DeviceId;
29import org.onosproject.net.PortNumber;
30import org.onosproject.net.flow.DefaultTrafficSelector;
31import org.onosproject.net.flow.DefaultTrafficTreatment;
32import org.onosproject.net.flow.TrafficSelector;
33import org.onosproject.net.flow.TrafficTreatment;
34import org.onosproject.net.flow.criteria.Criteria;
35import org.onosproject.net.flow.instructions.Instructions;
36import org.onosproject.net.flowobjective.DefaultForwardingObjective;
37import org.onosproject.net.flowobjective.FlowObjectiveService;
38import org.onosproject.net.flowobjective.ForwardingObjective;
39import org.onosproject.net.flowobjective.ForwardingObjective.Flag;
40import org.onosproject.net.flowobjective.Objective;
lishuai7547db42015-11-20 14:56:11 +080041import org.onosproject.net.flowobjective.Objective.Operation;
lishuai6c56f5e2015-11-17 16:38:19 +080042import org.onosproject.vtn.table.ClassifierService;
43import org.onosproject.vtnrsc.SegmentationId;
44import org.slf4j.Logger;
45
46import com.google.common.collect.Sets;
47
48/**
49 * Provides implementation of ClassifierService.
50 */
51public class ClassifierServiceImpl implements ClassifierService {
52 private final Logger log = getLogger(getClass());
53
lishuai9e4e43a2015-11-26 19:30:55 +080054 private static final int L3_CLAFFIFIER_PRIORITY = 0xffff;
lishuai6c56f5e2015-11-17 16:38:19 +080055 private static final int L2_CLAFFIFIER_PRIORITY = 50000;
56
57 private final FlowObjectiveService flowObjectiveService;
58 private final ApplicationId appId;
59
60 /**
61 * Constructor.
62 *
63 * @param appId the application id of vtn
64 */
65 public ClassifierServiceImpl(ApplicationId appId) {
66 this.appId = checkNotNull(appId, "ApplicationId can not be null");
67 ServiceDirectory serviceDirectory = new DefaultServiceDirectory();
68 this.flowObjectiveService = serviceDirectory.get(FlowObjectiveService.class);
69 }
70
71 @Override
72 public void programLocalIn(DeviceId deviceId,
73 SegmentationId segmentationId, PortNumber inPort,
74 MacAddress srcMac, ApplicationId appid,
75 Objective.Operation type) {
76 TrafficSelector selector = DefaultTrafficSelector.builder()
77 .matchInPort(inPort).matchEthSrc(srcMac).build();
78 TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder();
79 treatment.add(Instructions
80 .modTunnelId(Long.parseLong(segmentationId.toString())));
81 ForwardingObjective.Builder objective = DefaultForwardingObjective
82 .builder().withTreatment(treatment.build())
83 .withSelector(selector).fromApp(appId).makePermanent()
84 .withFlag(Flag.SPECIFIC).withPriority(L2_CLAFFIFIER_PRIORITY);
85 if (type.equals(Objective.Operation.ADD)) {
86 log.debug("programLocalIn-->ADD");
87 flowObjectiveService.forward(deviceId, objective.add());
88 } else {
89 log.debug("programLocalIn-->REMOVE");
90 flowObjectiveService.forward(deviceId, objective.remove());
91 }
92 }
93
94 @Override
95 public void programTunnelIn(DeviceId deviceId,
96 SegmentationId segmentationId,
97 Iterable<PortNumber> localTunnelPorts,
98 Objective.Operation type) {
99 if (localTunnelPorts == null) {
100 log.info("No tunnel port in device");
101 return;
102 }
103 Sets.newHashSet(localTunnelPorts).stream().forEach(tp -> {
104 TrafficSelector selector = DefaultTrafficSelector.builder()
105 .matchInPort(tp).add(Criteria.matchTunnelId(Long
106 .parseLong(segmentationId.toString())))
107 .build();
108
109 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
110 .build();
111 ForwardingObjective.Builder objective = DefaultForwardingObjective
112 .builder().withTreatment(treatment).withSelector(selector)
113 .fromApp(appId).makePermanent().withFlag(Flag.SPECIFIC)
114 .withPriority(L2_CLAFFIFIER_PRIORITY);
115 if (type.equals(Objective.Operation.ADD)) {
116 log.debug("programTunnelIn-->ADD");
117 flowObjectiveService.forward(deviceId, objective.add());
118 } else {
119 log.debug("programTunnelIn-->REMOVE");
120 flowObjectiveService.forward(deviceId, objective.remove());
121 }
122 });
123 }
124
lishuai7547db42015-11-20 14:56:11 +0800125 @Override
lishuai9e4e43a2015-11-26 19:30:55 +0800126 public void programL3ExPortClassifierRules(DeviceId deviceId, PortNumber inPort,
127 IpAddress dstIp,
128 Objective.Operation type) {
129 TrafficSelector selector = DefaultTrafficSelector.builder()
130 .matchEthType(Ethernet.TYPE_IPV4).matchInPort(inPort)
131 .matchIPDst(IpPrefix.valueOf(dstIp, 32)).build();
132 TrafficTreatment treatment = DefaultTrafficTreatment.builder().build();
133 ForwardingObjective.Builder objective = DefaultForwardingObjective
134 .builder().withTreatment(treatment).withSelector(selector)
135 .fromApp(appId).withFlag(Flag.SPECIFIC)
136 .withPriority(L3_CLAFFIFIER_PRIORITY);
137 if (type.equals(Objective.Operation.ADD)) {
138 log.debug("L3ExToInClassifierRules-->ADD");
139 flowObjectiveService.forward(deviceId, objective.add());
140 } else {
141 log.debug("L3ExToInClassifierRules-->REMOVE");
142 flowObjectiveService.forward(deviceId, objective.remove());
143 }
lishuai7547db42015-11-20 14:56:11 +0800144 }
145
146 @Override
lishuai478d02e2015-11-26 19:33:41 +0800147 public void programL3InPortClassifierRules(DeviceId deviceId, PortNumber inPort,
148 MacAddress srcMac, MacAddress dstMac,
149 SegmentationId actionVni,
150 Objective.Operation type) {
151 TrafficSelector selector = DefaultTrafficSelector.builder()
152 .matchInPort(inPort).matchEthSrc(srcMac).matchEthDst(dstMac)
153 .build();
154 TrafficTreatment treatment = DefaultTrafficTreatment.builder()
155 .setTunnelId(Long.parseLong(actionVni.segmentationId())).build();
156 ForwardingObjective.Builder objective = DefaultForwardingObjective
157 .builder().withTreatment(treatment).withSelector(selector)
158 .fromApp(appId).withFlag(Flag.SPECIFIC)
159 .withPriority(L3_CLAFFIFIER_PRIORITY);
160 if (type.equals(Objective.Operation.ADD)) {
161 log.debug("L3InternalClassifierRules-->ADD");
162 flowObjectiveService.forward(deviceId, objective.add());
163 } else {
164 log.debug("L3InternalClassifierRules-->REMOVE");
165 flowObjectiveService.forward(deviceId, objective.remove());
166 }
lishuai7547db42015-11-20 14:56:11 +0800167 }
168
169 @Override
170 public void programArpClassifierRules(DeviceId deviceId, IpAddress srcGwIp,
171 SegmentationId srcVni, Operation type) {
172 // TODO Auto-generated method stub
173 }
174
lishuai6c56f5e2015-11-17 16:38:19 +0800175}