blob: 6ae705a2ef92abe686c544d3f4ed32271d356958 [file] [log] [blame]
Andrea Campanellacbbb7952019-11-25 06:38:41 +00001/*
2 * Copyright 2016-present Open Networking Foundation
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.opencord.olt.impl;
17
Saurav Dasf62cea82020-08-26 17:43:04 -070018import static com.google.common.base.Strings.isNullOrEmpty;
19import static org.onlab.util.Tools.get;
20import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_TP_ID;
21import static org.opencord.olt.impl.OsgiPropertyConstants.DEFAULT_TP_ID_DEFAULT;
22import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_ON_NNI;
23import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_ON_NNI_DEFAULT;
24import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_V4;
25import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_V4_DEFAULT;
26import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_V6;
27import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_DHCP_V6_DEFAULT;
28import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_EAPOL;
29import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_EAPOL_DEFAULT;
30import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_IGMP_ON_NNI;
31import static org.opencord.olt.impl.OsgiPropertyConstants.ENABLE_IGMP_ON_NNI_DEFAULT;
32import static org.slf4j.LoggerFactory.getLogger;
33
34import java.util.Dictionary;
35import java.util.Iterator;
36import java.util.Properties;
37import java.util.Set;
38import java.util.concurrent.CompletableFuture;
39
Andrea Campanellacbbb7952019-11-25 06:38:41 +000040import org.onlab.packet.EthType;
41import org.onlab.packet.IPv4;
42import org.onlab.packet.IPv6;
43import org.onlab.packet.MacAddress;
44import org.onlab.packet.TpPort;
45import org.onlab.packet.VlanId;
46import org.onlab.util.Tools;
47import org.onosproject.cfg.ComponentConfigService;
48import org.onosproject.core.ApplicationId;
49import org.onosproject.core.CoreService;
50import org.onosproject.mastership.MastershipService;
51import org.onosproject.net.AnnotationKeys;
Matteo Scandolo3a037a32020-04-01 12:17:50 -070052import org.onosproject.net.ConnectPoint;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000053import org.onosproject.net.DeviceId;
54import org.onosproject.net.Port;
55import org.onosproject.net.PortNumber;
56import org.onosproject.net.device.DeviceService;
57import org.onosproject.net.flow.DefaultTrafficSelector;
58import org.onosproject.net.flow.DefaultTrafficTreatment;
59import org.onosproject.net.flow.TrafficSelector;
60import org.onosproject.net.flow.TrafficTreatment;
61import org.onosproject.net.flow.criteria.Criteria;
62import org.onosproject.net.flowobjective.DefaultFilteringObjective;
63import org.onosproject.net.flowobjective.DefaultForwardingObjective;
64import org.onosproject.net.flowobjective.FilteringObjective;
65import org.onosproject.net.flowobjective.FlowObjectiveService;
66import org.onosproject.net.flowobjective.ForwardingObjective;
67import org.onosproject.net.flowobjective.Objective;
68import org.onosproject.net.flowobjective.ObjectiveContext;
69import org.onosproject.net.flowobjective.ObjectiveError;
70import org.onosproject.net.meter.MeterId;
71import org.opencord.olt.internalapi.AccessDeviceFlowService;
72import org.opencord.olt.internalapi.AccessDeviceMeterService;
73import org.opencord.sadis.BandwidthProfileInformation;
74import org.opencord.sadis.BaseInformationService;
75import org.opencord.sadis.SadisService;
76import org.opencord.sadis.SubscriberAndDeviceInformation;
77import org.opencord.sadis.UniTagInformation;
78import org.osgi.service.component.ComponentContext;
79import org.osgi.service.component.annotations.Activate;
80import org.osgi.service.component.annotations.Component;
81import org.osgi.service.component.annotations.Deactivate;
82import org.osgi.service.component.annotations.Modified;
83import org.osgi.service.component.annotations.Reference;
84import org.osgi.service.component.annotations.ReferenceCardinality;
85import org.slf4j.Logger;
86
Saurav Dasf62cea82020-08-26 17:43:04 -070087import com.google.common.collect.Sets;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000088
89/**
90 * Provisions flow rules on access devices.
91 */
92@Component(immediate = true, property = {
Saurav Dasf62cea82020-08-26 17:43:04 -070093 ENABLE_DHCP_ON_NNI + ":Boolean=" + ENABLE_DHCP_ON_NNI_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +000094 ENABLE_DHCP_V4 + ":Boolean=" + ENABLE_DHCP_V4_DEFAULT,
95 ENABLE_DHCP_V6 + ":Boolean=" + ENABLE_DHCP_V6_DEFAULT,
Saurav Dasf62cea82020-08-26 17:43:04 -070096 ENABLE_IGMP_ON_NNI + ":Boolean=" + ENABLE_IGMP_ON_NNI_DEFAULT,
Andrea Campanellacbbb7952019-11-25 06:38:41 +000097 ENABLE_EAPOL + ":Boolean=" + ENABLE_EAPOL_DEFAULT,
98 DEFAULT_TP_ID + ":Integer=" + DEFAULT_TP_ID_DEFAULT
99})
100public class OltFlowService implements AccessDeviceFlowService {
101
102 private static final String APP_NAME = "org.opencord.olt";
103 private static final int NONE_TP_ID = -1;
104 private static final int NO_PCP = -1;
105 private static final Integer MAX_PRIORITY = 10000;
106 private static final Integer MIN_PRIORITY = 1000;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000107 private static final String INSTALLED = "installed";
108 private static final String REMOVED = "removed";
109 private static final String INSTALLATION = "installation";
110 private static final String REMOVAL = "removal";
111 private static final String V4 = "V4";
112 private static final String V6 = "V6";
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000113
114 private final Logger log = getLogger(getClass());
115
116 @Reference(cardinality = ReferenceCardinality.MANDATORY)
117 protected FlowObjectiveService flowObjectiveService;
118
119 @Reference(cardinality = ReferenceCardinality.MANDATORY)
120 protected CoreService coreService;
121
122 @Reference(cardinality = ReferenceCardinality.MANDATORY)
123 protected MastershipService mastershipService;
124
125 @Reference(cardinality = ReferenceCardinality.MANDATORY)
126 protected SadisService sadisService;
127
128 @Reference(cardinality = ReferenceCardinality.MANDATORY)
129 protected DeviceService deviceService;
130
131 @Reference(cardinality = ReferenceCardinality.MANDATORY)
132 protected AccessDeviceMeterService oltMeterService;
133
134 @Reference(cardinality = ReferenceCardinality.MANDATORY)
135 protected ComponentConfigService componentConfigService;
136
137 /**
Saurav Dasf62cea82020-08-26 17:43:04 -0700138 * Create DHCP trap flow on NNI port(s).
139 */
140 protected boolean enableDhcpOnNni = ENABLE_DHCP_ON_NNI_DEFAULT;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000141
142 /**
Saurav Dasf62cea82020-08-26 17:43:04 -0700143 * Enable flows for DHCP v4 if dhcp is required in sadis config.
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000144 **/
145 protected boolean enableDhcpV4 = ENABLE_DHCP_V4_DEFAULT;
146
147 /**
Saurav Dasf62cea82020-08-26 17:43:04 -0700148 * Enable flows for DHCP v6 if dhcp is required in sadis config.
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000149 **/
150 protected boolean enableDhcpV6 = ENABLE_DHCP_V6_DEFAULT;
151
152 /**
Saurav Dasf62cea82020-08-26 17:43:04 -0700153 * Create IGMP trap flow on NNI port(s).
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000154 **/
Saurav Dasf62cea82020-08-26 17:43:04 -0700155 protected boolean enableIgmpOnNni = ENABLE_IGMP_ON_NNI_DEFAULT;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000156
157 /**
158 * Send EAPOL authentication trap flows before subscriber provisioning.
159 **/
160 protected boolean enableEapol = ENABLE_EAPOL_DEFAULT;
161
162 /**
163 * Default technology profile id that is used for authentication trap flows.
164 **/
165 protected int defaultTechProfileId = DEFAULT_TP_ID_DEFAULT;
166
167 protected ApplicationId appId;
168 protected BaseInformationService<BandwidthProfileInformation> bpService;
169 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700170 private Set<ConnectPoint> pendingAddEapol = Sets.newConcurrentHashSet();
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200171 private Set<SubscriberFlowInfo> pendingEapolForMeters = Sets.newConcurrentHashSet();;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000172
173 @Activate
174 public void activate(ComponentContext context) {
175 bpService = sadisService.getBandwidthProfileService();
176 subsService = sadisService.getSubscriberInfoService();
177 componentConfigService.registerProperties(getClass());
178 appId = coreService.getAppId(APP_NAME);
Andrea Campanellafee86422020-06-04 16:01:27 +0200179 log.info("started");
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000180 }
181
182
183 @Deactivate
184 public void deactivate(ComponentContext context) {
185 componentConfigService.unregisterProperties(getClass(), false);
Andrea Campanellafee86422020-06-04 16:01:27 +0200186 log.info("stopped");
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000187 }
188
189 @Modified
190 public void modified(ComponentContext context) {
191
192 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
193
Saurav Dasf62cea82020-08-26 17:43:04 -0700194 Boolean o = Tools.isPropertyEnabled(properties, ENABLE_DHCP_ON_NNI);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000195 if (o != null) {
Saurav Dasf62cea82020-08-26 17:43:04 -0700196 enableDhcpOnNni = o;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000197 }
198
Andrea Campanella7c49b792020-05-11 11:36:53 +0200199 Boolean v4 = Tools.isPropertyEnabled(properties, ENABLE_DHCP_V4);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000200 if (v4 != null) {
201 enableDhcpV4 = v4;
202 }
203
Andrea Campanella7c49b792020-05-11 11:36:53 +0200204 Boolean v6 = Tools.isPropertyEnabled(properties, ENABLE_DHCP_V6);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000205 if (v6 != null) {
206 enableDhcpV6 = v6;
207 }
208
Saurav Dasf62cea82020-08-26 17:43:04 -0700209 Boolean p = Tools.isPropertyEnabled(properties, ENABLE_IGMP_ON_NNI);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000210 if (p != null) {
Saurav Dasf62cea82020-08-26 17:43:04 -0700211 enableIgmpOnNni = p;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000212 }
213
Andrea Campanella7c49b792020-05-11 11:36:53 +0200214 Boolean eap = Tools.isPropertyEnabled(properties, ENABLE_EAPOL);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000215 if (eap != null) {
216 enableEapol = eap;
217 }
218
Andrea Campanella7c49b792020-05-11 11:36:53 +0200219 String tpId = get(properties, DEFAULT_TP_ID);
220 defaultTechProfileId = isNullOrEmpty(tpId) ? DEFAULT_TP_ID_DEFAULT : Integer.parseInt(tpId.trim());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000221
Saurav Dasf62cea82020-08-26 17:43:04 -0700222 log.info("modified. Values = enableDhcpOnNni: {}, enableDhcpV4: {}, " +
223 "enableDhcpV6:{}, enableIgmpOnNni:{}, " +
Andrea Campanellafee86422020-06-04 16:01:27 +0200224 "enableEapol{}, defaultTechProfileId: {}",
Saurav Dasf62cea82020-08-26 17:43:04 -0700225 enableDhcpOnNni, enableDhcpV4, enableDhcpV6,
226 enableIgmpOnNni, enableEapol, defaultTechProfileId);
Andrea Campanellafee86422020-06-04 16:01:27 +0200227
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000228 }
229
230 @Override
231 public void processDhcpFilteringObjectives(DeviceId devId, PortNumber port,
232 MeterId upstreamMeterId,
233 UniTagInformation tagInformation,
234 boolean install,
235 boolean upstream) {
Saurav Dasf62cea82020-08-26 17:43:04 -0700236 if (upstream) {
237 // for UNI ports
238 if (tagInformation != null && !tagInformation.getIsDhcpRequired()) {
239 log.debug("Dhcp provisioning is disabled for UNI port {} on "
240 + "device {} for service {}", port, devId,
241 tagInformation.getServiceName());
242 return;
243 }
244 } else {
245 // for NNI ports
246 if (!enableDhcpOnNni) {
247 log.debug("Dhcp provisioning is disabled for NNI port {} on "
248 + "device {}", port, devId);
249 return;
250 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000251 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000252 int techProfileId = tagInformation != null ? tagInformation.getTechnologyProfileId() : NONE_TP_ID;
253 VlanId cTag = tagInformation != null ? tagInformation.getPonCTag() : VlanId.NONE;
254 VlanId unitagMatch = tagInformation != null ? tagInformation.getUniTagMatch() : VlanId.ANY;
Andrea Campanella0e34f562020-06-11 10:47:10 +0200255 Byte vlanPcp = tagInformation != null ? (byte) tagInformation.getUsPonCTagPriority() : null;
256
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000257
258 if (enableDhcpV4) {
259 int udpSrc = (upstream) ? 68 : 67;
260 int udpDst = (upstream) ? 67 : 68;
261
262 EthType ethType = EthType.EtherType.IPV4.ethType();
263 byte protocol = IPv4.PROTOCOL_UDP;
264
Andrea Campanella7c49b792020-05-11 11:36:53 +0200265 addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
Andrea Campanella0e34f562020-06-11 10:47:10 +0200266 upstreamMeterId, techProfileId, protocol, cTag, unitagMatch,
267 vlanPcp, upstream, install);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000268 }
269
270 if (enableDhcpV6) {
271 int udpSrc = (upstream) ? 547 : 546;
272 int udpDst = (upstream) ? 546 : 547;
273
274 EthType ethType = EthType.EtherType.IPV6.ethType();
275 byte protocol = IPv6.PROTOCOL_UDP;
276
Andrea Campanella7c49b792020-05-11 11:36:53 +0200277 addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
Andrea Campanella0e34f562020-06-11 10:47:10 +0200278 upstreamMeterId, techProfileId, protocol, cTag, unitagMatch,
279 vlanPcp, upstream, install);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000280 }
281 }
282
283 private void addDhcpFilteringObjectives(DeviceId devId, PortNumber port, int udpSrc, int udpDst,
284 EthType ethType, MeterId upstreamMeterId, int techProfileId, byte protocol,
Andrea Campanella0e34f562020-06-11 10:47:10 +0200285 VlanId cTag, VlanId unitagMatch,
286 Byte vlanPcp, boolean upstream,
287 boolean install) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000288
289 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
290 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
291
292 if (upstreamMeterId != null) {
293 treatmentBuilder.meter(upstreamMeterId);
294 }
295
296 if (techProfileId != NONE_TP_ID) {
297 treatmentBuilder.writeMetadata(createTechProfValueForWm(unitagMatch, techProfileId), 0);
298 }
299
300 FilteringObjective.Builder dhcpUpstreamBuilder = (install ? builder.permit() : builder.deny())
301 .withKey(Criteria.matchInPort(port))
302 .addCondition(Criteria.matchEthType(ethType))
303 .addCondition(Criteria.matchIPProtocol(protocol))
304 .addCondition(Criteria.matchUdpSrc(TpPort.tpPort(udpSrc)))
305 .addCondition(Criteria.matchUdpDst(TpPort.tpPort(udpDst)))
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000306 .fromApp(appId)
307 .withPriority(MAX_PRIORITY);
308
Andrea Campanella0e34f562020-06-11 10:47:10 +0200309 //VLAN changes and PCP matching need to happen only in the upstream directions
310 if (upstream) {
311 treatmentBuilder.setVlanId(cTag);
312 if (!VlanId.vlanId(VlanId.NO_VID).equals(unitagMatch)) {
313 dhcpUpstreamBuilder.addCondition(Criteria.matchVlanId(unitagMatch));
314 }
315 if (vlanPcp != null) {
316 treatmentBuilder.setVlanPcp(vlanPcp);
317 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000318 }
319
Andrea Campanella0e34f562020-06-11 10:47:10 +0200320 dhcpUpstreamBuilder.withMeta(treatmentBuilder
321 .setOutput(PortNumber.CONTROLLER).build());
322
323
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000324 FilteringObjective dhcpUpstream = dhcpUpstreamBuilder.add(new ObjectiveContext() {
325 @Override
326 public void onSuccess(Objective objective) {
327 log.info("DHCP {} filter for device {} on port {} {}.",
328 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
329 devId, port, (install) ? INSTALLED : REMOVED);
330 }
331
332 @Override
333 public void onError(Objective objective, ObjectiveError error) {
334 log.info("DHCP {} filter for device {} on port {} failed {} because {}",
335 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
336 devId, port, (install) ? INSTALLATION : REMOVAL,
337 error);
338 }
339 });
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000340 flowObjectiveService.filter(devId, dhcpUpstream);
341
342 }
343
344 @Override
345 public void processIgmpFilteringObjectives(DeviceId devId, PortNumber port,
346 MeterId upstreamMeterId,
347 UniTagInformation tagInformation,
348 boolean install,
349 boolean upstream) {
Saurav Dasf62cea82020-08-26 17:43:04 -0700350 if (upstream) {
351 // for UNI ports
352 if (tagInformation != null && !tagInformation.getIsIgmpRequired()) {
353 log.debug("Igmp provisioning is disabled for UNI port {} on "
354 + "device {} for service {}", port, devId,
355 tagInformation.getServiceName());
356 return;
357 }
358 } else {
359 // for NNI ports
360 if (!enableIgmpOnNni) {
361 log.debug("Igmp provisioning is disabled for NNI port {} on device {}",
362 port, devId);
363 return;
364 }
Matteo Scandolo34556e52020-05-08 12:34:13 -0700365 }
366
Andrea Campanellafee86422020-06-04 16:01:27 +0200367 log.debug("{} IGMP flows on {}:{}", (install) ?
368 "Installing" : "Removing", devId, port);
Andrea Campanella0e34f562020-06-11 10:47:10 +0200369 DefaultFilteringObjective.Builder filterBuilder = DefaultFilteringObjective.builder();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000370 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
371 if (upstream) {
372
373 if (tagInformation.getTechnologyProfileId() != NONE_TP_ID) {
374 treatmentBuilder.writeMetadata(createTechProfValueForWm(null,
375 tagInformation.getTechnologyProfileId()), 0);
376 }
377
378
379 if (upstreamMeterId != null) {
380 treatmentBuilder.meter(upstreamMeterId);
381 }
382
Andrea Campanella0e34f562020-06-11 10:47:10 +0200383 if (!VlanId.vlanId(VlanId.NO_VID).equals(tagInformation.getUniTagMatch())) {
384 filterBuilder.addCondition(Criteria.matchVlanId(tagInformation.getUniTagMatch()));
385 }
386
387 if (!VlanId.vlanId(VlanId.NO_VID).equals(tagInformation.getPonCTag())) {
388 treatmentBuilder.setVlanId(tagInformation.getPonCTag());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000389 }
390
391 if (tagInformation.getUsPonCTagPriority() != NO_PCP) {
Andrea Campanella0e34f562020-06-11 10:47:10 +0200392 treatmentBuilder.setVlanPcp((byte) tagInformation.getUsPonCTagPriority());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000393 }
394 }
395
Andrea Campanella0e34f562020-06-11 10:47:10 +0200396 filterBuilder = install ? filterBuilder.permit() : filterBuilder.deny();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000397
Andrea Campanella0e34f562020-06-11 10:47:10 +0200398 FilteringObjective igmp = filterBuilder
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000399 .withKey(Criteria.matchInPort(port))
400 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
401 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
402 .withMeta(treatmentBuilder
403 .setOutput(PortNumber.CONTROLLER).build())
404 .fromApp(appId)
405 .withPriority(MAX_PRIORITY)
406 .add(new ObjectiveContext() {
407 @Override
408 public void onSuccess(Objective objective) {
409 log.info("Igmp filter for {} on {} {}.",
410 devId, port, (install) ? INSTALLED : REMOVED);
411 }
412
413 @Override
414 public void onError(Objective objective, ObjectiveError error) {
415 log.info("Igmp filter for {} on {} failed {} because {}.",
416 devId, port, (install) ? INSTALLATION : REMOVAL,
417 error);
418 }
419 });
420
421 flowObjectiveService.filter(devId, igmp);
422 }
423
424 @Override
425 public void processEapolFilteringObjectives(DeviceId devId, PortNumber portNumber, String bpId,
426 CompletableFuture<ObjectiveError> filterFuture,
427 VlanId vlanId, boolean install) {
428
429 if (!enableEapol) {
430 log.debug("Eapol filtering is disabled. Completing filteringFuture immediately for the device {}", devId);
431 if (filterFuture != null) {
432 filterFuture.complete(null);
433 }
434 return;
435 }
436
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000437 BandwidthProfileInformation bpInfo = getBandwidthProfileInformation(bpId);
438 if (bpInfo == null) {
439 log.warn("Bandwidth profile {} is not found. Authentication flow"
440 + " will not be installed", bpId);
441 if (filterFuture != null) {
442 filterFuture.complete(ObjectiveError.BADPARAMS);
443 }
444 return;
445 }
446
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700447 ConnectPoint cp = new ConnectPoint(devId, portNumber);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000448 if (install) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700449 boolean added = pendingAddEapol.add(cp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000450 if (!added) {
451 if (filterFuture != null) {
452 log.warn("The eapol flow is processing for the port {}. Ignoring this request", portNumber);
453 filterFuture.complete(null);
454 }
455 return;
456 }
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700457 log.info("connectPoint added to pendingAddEapol map {}", cp.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000458 }
459
Andrea Campanella0e34f562020-06-11 10:47:10 +0200460 DefaultFilteringObjective.Builder filterBuilder = DefaultFilteringObjective.builder();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000461 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
462 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
463
464 // check if meter exists and create it only for an install
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200465 final MeterId meterId = oltMeterService.getMeterIdFromBpMapping(devId, bpInfo.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000466 if (meterId == null) {
467 if (install) {
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200468 log.debug("Need to install meter for EAPOL with bwp {}", bpInfo.id());
469 SubscriberFlowInfo fi = new SubscriberFlowInfo(devId, null, cp.port(),
470 new UniTagInformation.Builder()
471 .setPonCTag(vlanId).build(),
472 null, null,
473 null, bpInfo.id());
474 pendingEapolForMeters.add(fi);
475
Andrea Campanellad1e26642020-10-23 12:08:32 +0200476 //If false the meter is already being installed, skipping installation
477 if (!oltMeterService.checkAndAddPendingMeter(devId, bpInfo)) {
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200478 return;
479 }
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200480 MeterId innerMeterId = oltMeterService.createMeter(devId, bpInfo,
481 meterFuture);
482 fi.setUpMeterId(innerMeterId);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000483 } else {
484 // this case should not happen as the request to remove an eapol
485 // flow should mean that the flow points to a meter that exists.
486 // Nevertheless we can still delete the flow as we only need the
487 // correct 'match' to do so.
488 log.warn("Unknown meter id for bp {}, still proceeding with "
489 + "delete of eapol flow for {}/{}", bpInfo.id(), devId, portNumber);
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200490 SubscriberFlowInfo fi = new SubscriberFlowInfo(devId, null, cp.port(),
491 new UniTagInformation.Builder()
492 .setPonCTag(vlanId).build(),
493 null, meterId,
494 null, bpInfo.id());
Andrea Campanella0e34f562020-06-11 10:47:10 +0200495 handleEapol(filterFuture, install, cp, filterBuilder, treatmentBuilder, fi, meterId);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000496 }
497 } else {
498 log.debug("Meter {} was previously created for bp {}", meterId, bpInfo.id());
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200499 SubscriberFlowInfo fi = new SubscriberFlowInfo(devId, null, cp.port(),
500 new UniTagInformation.Builder()
501 .setPonCTag(vlanId).build(),
502 null, meterId,
503 null, bpInfo.id());
Andrea Campanella0e34f562020-06-11 10:47:10 +0200504 handleEapol(filterFuture, install, cp, filterBuilder, treatmentBuilder, fi, meterId);
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200505 //No need for the future, meter is present.
506 return;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000507 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000508 meterFuture.thenAcceptAsync(result -> {
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200509 //for each pending eapol flow we check if the meter is there.
510 Iterator<SubscriberFlowInfo> eapIterator = pendingEapolForMeters.iterator();
511 while (eapIterator.hasNext()) {
512 SubscriberFlowInfo fi = eapIterator.next();
513 if (result == null) {
514 MeterId mId = oltMeterService
Andrea Campanella600d2e22020-06-22 11:00:31 +0200515 .getMeterIdFromBpMapping(devId, fi.getUpBpInfo());
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200516 if (mId != null) {
Andrea Campanella0e34f562020-06-11 10:47:10 +0200517 handleEapol(filterFuture, install, cp, filterBuilder, treatmentBuilder, fi, mId);
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200518 eapIterator.remove();
519 }
520 } else {
521 log.warn("Meter installation error while sending eapol trap flow. " +
522 "Result {} and MeterId {}", result, meterId);
523 eapIterator.remove();
524 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200525 oltMeterService.removeFromPendingMeters(devId, bpInfo);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000526 }
527 });
528 }
529
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200530 private void handleEapol(CompletableFuture<ObjectiveError> filterFuture,
531 boolean install, ConnectPoint cp,
Andrea Campanella0e34f562020-06-11 10:47:10 +0200532 DefaultFilteringObjective.Builder filterBuilder,
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200533 TrafficTreatment.Builder treatmentBuilder,
534 SubscriberFlowInfo fi, MeterId mId) {
535 log.info("Meter {} for {} on {}/{} exists. {} EAPOL trap flow",
536 mId, fi.getUpBpInfo(), fi.getDevId(), fi.getUniPort(),
537 (install) ? "Installing" : "Removing");
538 int techProfileId = getDefaultTechProfileId(fi.getDevId(), fi.getUniPort());
539 // can happen in case of removal
540 if (mId != null) {
541 treatmentBuilder.meter(mId);
542 }
543 //Authentication trap flow uses only tech profile id as write metadata value
Andrea Campanella0e34f562020-06-11 10:47:10 +0200544 FilteringObjective eapol = (install ? filterBuilder.permit() : filterBuilder.deny())
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200545 .withKey(Criteria.matchInPort(fi.getUniPort()))
546 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200547 .withMeta(treatmentBuilder
548 .writeMetadata(createTechProfValueForWm(
549 fi.getTagInfo().getPonCTag(),
550 techProfileId), 0)
Andrea Campanella0e34f562020-06-11 10:47:10 +0200551 .setOutput(PortNumber.CONTROLLER)
552 .pushVlan()
553 .setVlanId(fi.getTagInfo().getPonCTag())
554 .build())
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200555 .fromApp(appId)
556 .withPriority(MAX_PRIORITY)
557 .add(new ObjectiveContext() {
558 @Override
559 public void onSuccess(Objective objective) {
Andrea Campanella600d2e22020-06-22 11:00:31 +0200560 log.info("Eapol filter {} for {} on {} {} with meter {}.",
561 objective.id(), fi.getDevId(), fi.getUniPort(),
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200562 (install) ? INSTALLED : REMOVED, mId);
563 if (filterFuture != null) {
564 filterFuture.complete(null);
565 }
566 pendingAddEapol.remove(cp);
567 }
568
569 @Override
570 public void onError(Objective objective, ObjectiveError error) {
Andrea Campanella600d2e22020-06-22 11:00:31 +0200571 log.error("Eapol filter {} for {} on {} with meter {} " +
572 "failed {} because {}", objective.id(),
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200573 fi.getDevId(), fi.getUniPort(), mId,
574 (install) ? INSTALLATION : REMOVAL,
575 error);
576 if (filterFuture != null) {
577 filterFuture.complete(error);
578 }
579 pendingAddEapol.remove(cp);
580 }
581 });
582 flowObjectiveService.filter(fi.getDevId(), eapol);
583 }
584
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000585 /**
586 * Installs trap filtering objectives for particular traffic types on an
587 * NNI port.
588 *
589 * @param devId device ID
590 * @param port port number
591 * @param install true to install, false to remove
592 */
593 @Override
594 public void processNniFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
Saurav Dasf62cea82020-08-26 17:43:04 -0700595 log.info("{} flows for NNI port {} on device {}",
596 install ? "Adding" : "Removing", port, devId);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000597 processLldpFilteringObjective(devId, port, install);
598 processDhcpFilteringObjectives(devId, port, null, null, install, false);
599 processIgmpFilteringObjectives(devId, port, null, null, install, false);
600 }
601
602
603 @Override
604 public void processLldpFilteringObjective(DeviceId devId, PortNumber port, boolean install) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000605 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
606
607 FilteringObjective lldp = (install ? builder.permit() : builder.deny())
608 .withKey(Criteria.matchInPort(port))
609 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
610 .withMeta(DefaultTrafficTreatment.builder()
611 .setOutput(PortNumber.CONTROLLER).build())
612 .fromApp(appId)
613 .withPriority(MAX_PRIORITY)
614 .add(new ObjectiveContext() {
615 @Override
616 public void onSuccess(Objective objective) {
617 log.info("LLDP filter for device {} on port {} {}.",
618 devId, port, (install) ? INSTALLED : REMOVED);
619 }
620
621 @Override
622 public void onError(Objective objective, ObjectiveError error) {
623 log.info("LLDP filter for device {} on port {} failed {} because {}",
624 devId, port, (install) ? INSTALLATION : REMOVAL,
625 error);
626 }
627 });
628
629 flowObjectiveService.filter(devId, lldp);
630 }
631
632 @Override
633 public ForwardingObjective.Builder createTransparentBuilder(PortNumber uplinkPort,
634 PortNumber subscriberPort,
635 MeterId meterId,
636 UniTagInformation tagInfo,
637 boolean upstream) {
638
639 TrafficSelector selector = DefaultTrafficSelector.builder()
640 .matchVlanId(tagInfo.getPonSTag())
641 .matchInPort(upstream ? subscriberPort : uplinkPort)
642 .matchInnerVlanId(tagInfo.getPonCTag())
643 .build();
644
645 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
646 if (meterId != null) {
647 tBuilder.meter(meterId);
648 }
649
650 TrafficTreatment treatment = tBuilder
651 .setOutput(upstream ? uplinkPort : subscriberPort)
652 .writeMetadata(createMetadata(upstream ? tagInfo.getPonSTag() : tagInfo.getPonCTag(),
653 tagInfo.getTechnologyProfileId(), upstream ? uplinkPort : subscriberPort), 0)
654 .build();
655
656 return createForwardingObjectiveBuilder(selector, treatment, MIN_PRIORITY);
657 }
658
659 @Override
660 public ForwardingObjective.Builder createUpBuilder(PortNumber uplinkPort,
661 PortNumber subscriberPort,
662 MeterId upstreamMeterId,
663 UniTagInformation uniTagInformation) {
664
665 TrafficSelector selector = DefaultTrafficSelector.builder()
666 .matchInPort(subscriberPort)
667 .matchVlanId(uniTagInformation.getUniTagMatch())
668 .build();
669
Andrea Campanella327c5722020-01-30 11:34:13 +0100670 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
671 //if the subscriberVlan (cTag) is different than ANY it needs to set.
672 if (uniTagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
673 treatmentBuilder.pushVlan()
674 .setVlanId(uniTagInformation.getPonCTag());
675 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000676
677 if (uniTagInformation.getUsPonCTagPriority() != NO_PCP) {
678 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonCTagPriority());
679 }
680
681 treatmentBuilder.pushVlan()
682 .setVlanId(uniTagInformation.getPonSTag());
683
684 if (uniTagInformation.getUsPonSTagPriority() != NO_PCP) {
685 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonSTagPriority());
686 }
687
688 treatmentBuilder.setOutput(uplinkPort)
689 .writeMetadata(createMetadata(uniTagInformation.getPonCTag(),
690 uniTagInformation.getTechnologyProfileId(), uplinkPort), 0L);
691
692 if (upstreamMeterId != null) {
693 treatmentBuilder.meter(upstreamMeterId);
694 }
695
696 return createForwardingObjectiveBuilder(selector, treatmentBuilder.build(), MIN_PRIORITY);
697 }
698
699 @Override
700 public ForwardingObjective.Builder createDownBuilder(PortNumber uplinkPort,
701 PortNumber subscriberPort,
702 MeterId downstreamMeterId,
703 UniTagInformation tagInformation) {
Andrea Campanella327c5722020-01-30 11:34:13 +0100704
705 //subscriberVlan can be any valid Vlan here including ANY to make sure the packet is tagged
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000706 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
707 .matchVlanId(tagInformation.getPonSTag())
708 .matchInPort(uplinkPort)
Andrea Campanella090e4a02020-02-05 13:53:55 +0100709 .matchInnerVlanId(tagInformation.getPonCTag());
710
711
712 if (tagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
713 selectorBuilder.matchMetadata(tagInformation.getPonCTag().toShort());
714 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000715
716 if (tagInformation.getDsPonSTagPriority() != NO_PCP) {
717 selectorBuilder.matchVlanPcp((byte) tagInformation.getDsPonSTagPriority());
718 }
719
720 if (tagInformation.getConfiguredMacAddress() != null &&
Daniele Moro7cbf4312020-03-06 17:24:12 -0800721 !tagInformation.getConfiguredMacAddress().equals("") &&
722 !MacAddress.NONE.equals(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()))) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000723 selectorBuilder.matchEthDst(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()));
724 }
725
726 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder()
727 .popVlan()
Andrea Campanella327c5722020-01-30 11:34:13 +0100728 .setOutput(subscriberPort);
729
Andrea Campanella327c5722020-01-30 11:34:13 +0100730 treatmentBuilder.writeMetadata(createMetadata(tagInformation.getPonCTag(),
731 tagInformation.getTechnologyProfileId(),
732 subscriberPort), 0);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000733
734 // to remark inner vlan header
735 if (tagInformation.getUsPonCTagPriority() != NO_PCP) {
736 treatmentBuilder.setVlanPcp((byte) tagInformation.getUsPonCTagPriority());
737 }
738
Andrea Campanella9a779292020-02-03 19:19:09 +0100739 if (!VlanId.NONE.equals(tagInformation.getUniTagMatch()) &&
740 tagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000741 treatmentBuilder.setVlanId(tagInformation.getUniTagMatch());
742 }
743
744 if (downstreamMeterId != null) {
745 treatmentBuilder.meter(downstreamMeterId);
746 }
747
748 return createForwardingObjectiveBuilder(selectorBuilder.build(), treatmentBuilder.build(), MIN_PRIORITY);
749 }
750
Andrea Campanella600d2e22020-06-22 11:00:31 +0200751 @Override
752 public void clearDeviceState(DeviceId deviceId) {
753 pendingEapolForMeters.removeIf(fi -> fi.getDevId().equals(deviceId));
754 pendingAddEapol.removeIf(connectPoint -> connectPoint.deviceId().equals(deviceId));
755
756 }
757
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000758 private DefaultForwardingObjective.Builder createForwardingObjectiveBuilder(TrafficSelector selector,
759 TrafficTreatment treatment,
760 Integer priority) {
761 return DefaultForwardingObjective.builder()
762 .withFlag(ForwardingObjective.Flag.VERSATILE)
763 .withPriority(priority)
764 .makePermanent()
765 .withSelector(selector)
766 .fromApp(appId)
767 .withTreatment(treatment);
768 }
769
770 /**
771 * Returns the write metadata value including tech profile reference and innerVlan.
772 * For param cVlan, null can be sent
773 *
774 * @param cVlan c (customer) tag of one subscriber
775 * @param techProfileId tech profile id of one subscriber
776 * @return the write metadata value including tech profile reference and innerVlan
777 */
778 private Long createTechProfValueForWm(VlanId cVlan, int techProfileId) {
779 if (cVlan == null || VlanId.NONE.equals(cVlan)) {
780 return (long) techProfileId << 32;
781 }
782 return ((long) (cVlan.id()) << 48 | (long) techProfileId << 32);
783 }
784
785 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
786 if (bandwidthProfile == null) {
787 return null;
788 }
789 return bpService.get(bandwidthProfile);
790 }
791
792 /**
793 * It will be used to support AT&T use case (for EAPOL flows).
794 * If multiple services are found in uniServiceList, returns default tech profile id
795 * If one service is found, returns the found one
796 *
797 * @param devId
798 * @param portNumber
799 * @return the default technology profile id
800 */
801 private int getDefaultTechProfileId(DeviceId devId, PortNumber portNumber) {
802 Port port = deviceService.getPort(devId, portNumber);
803 if (port != null) {
804 SubscriberAndDeviceInformation info = subsService.get(port.annotations().value(AnnotationKeys.PORT_NAME));
805 if (info != null && info.uniTagList().size() == 1) {
806 return info.uniTagList().get(0).getTechnologyProfileId();
807 }
808 }
809 return defaultTechProfileId;
810 }
811
812 /**
813 * Write metadata instruction value (metadata) is 8 bytes.
814 * <p>
815 * MS 2 bytes: C Tag
816 * Next 2 bytes: Technology Profile Id
817 * Next 4 bytes: Port number (uni or nni)
818 */
819 private Long createMetadata(VlanId innerVlan, int techProfileId, PortNumber egressPort) {
820 if (techProfileId == NONE_TP_ID) {
Andrea Campanella7c49b792020-05-11 11:36:53 +0200821 techProfileId = DEFAULT_TP_ID_DEFAULT;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000822 }
823
824 return ((long) (innerVlan.id()) << 48 | (long) techProfileId << 32) | egressPort.toLong();
825 }
826
827
828}