blob: d9326382bf8bc1bb1a15754bf6ce41565020253b [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;
255
256 if (enableDhcpV4) {
257 int udpSrc = (upstream) ? 68 : 67;
258 int udpDst = (upstream) ? 67 : 68;
259
260 EthType ethType = EthType.EtherType.IPV4.ethType();
261 byte protocol = IPv4.PROTOCOL_UDP;
262
Andrea Campanella7c49b792020-05-11 11:36:53 +0200263 addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000264 upstreamMeterId, techProfileId, protocol, cTag, unitagMatch, install);
265 }
266
267 if (enableDhcpV6) {
268 int udpSrc = (upstream) ? 547 : 546;
269 int udpDst = (upstream) ? 546 : 547;
270
271 EthType ethType = EthType.EtherType.IPV6.ethType();
272 byte protocol = IPv6.PROTOCOL_UDP;
273
Andrea Campanella7c49b792020-05-11 11:36:53 +0200274 addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000275 upstreamMeterId, techProfileId, protocol, cTag, unitagMatch, install);
276 }
277 }
278
279 private void addDhcpFilteringObjectives(DeviceId devId, PortNumber port, int udpSrc, int udpDst,
280 EthType ethType, MeterId upstreamMeterId, int techProfileId, byte protocol,
281 VlanId cTag, VlanId unitagMatch, boolean install) {
282
283 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
284 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
285
286 if (upstreamMeterId != null) {
287 treatmentBuilder.meter(upstreamMeterId);
288 }
289
290 if (techProfileId != NONE_TP_ID) {
291 treatmentBuilder.writeMetadata(createTechProfValueForWm(unitagMatch, techProfileId), 0);
292 }
293
294 FilteringObjective.Builder dhcpUpstreamBuilder = (install ? builder.permit() : builder.deny())
295 .withKey(Criteria.matchInPort(port))
296 .addCondition(Criteria.matchEthType(ethType))
297 .addCondition(Criteria.matchIPProtocol(protocol))
298 .addCondition(Criteria.matchUdpSrc(TpPort.tpPort(udpSrc)))
299 .addCondition(Criteria.matchUdpDst(TpPort.tpPort(udpDst)))
300 .withMeta(treatmentBuilder
301 .setOutput(PortNumber.CONTROLLER).build())
302 .fromApp(appId)
303 .withPriority(MAX_PRIORITY);
304
305 if (!VlanId.NONE.equals(cTag)) {
306 dhcpUpstreamBuilder.addCondition(Criteria.matchVlanId(cTag));
307 }
308
309 FilteringObjective dhcpUpstream = dhcpUpstreamBuilder.add(new ObjectiveContext() {
310 @Override
311 public void onSuccess(Objective objective) {
312 log.info("DHCP {} filter for device {} on port {} {}.",
313 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
314 devId, port, (install) ? INSTALLED : REMOVED);
315 }
316
317 @Override
318 public void onError(Objective objective, ObjectiveError error) {
319 log.info("DHCP {} filter for device {} on port {} failed {} because {}",
320 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
321 devId, port, (install) ? INSTALLATION : REMOVAL,
322 error);
323 }
324 });
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000325 flowObjectiveService.filter(devId, dhcpUpstream);
326
327 }
328
329 @Override
330 public void processIgmpFilteringObjectives(DeviceId devId, PortNumber port,
331 MeterId upstreamMeterId,
332 UniTagInformation tagInformation,
333 boolean install,
334 boolean upstream) {
Saurav Dasf62cea82020-08-26 17:43:04 -0700335 if (upstream) {
336 // for UNI ports
337 if (tagInformation != null && !tagInformation.getIsIgmpRequired()) {
338 log.debug("Igmp provisioning is disabled for UNI port {} on "
339 + "device {} for service {}", port, devId,
340 tagInformation.getServiceName());
341 return;
342 }
343 } else {
344 // for NNI ports
345 if (!enableIgmpOnNni) {
346 log.debug("Igmp provisioning is disabled for NNI port {} on device {}",
347 port, devId);
348 return;
349 }
Matteo Scandolo34556e52020-05-08 12:34:13 -0700350 }
351
Andrea Campanellafee86422020-06-04 16:01:27 +0200352 log.debug("{} IGMP flows on {}:{}", (install) ?
353 "Installing" : "Removing", devId, port);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000354 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
355 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
356 if (upstream) {
357
358 if (tagInformation.getTechnologyProfileId() != NONE_TP_ID) {
359 treatmentBuilder.writeMetadata(createTechProfValueForWm(null,
360 tagInformation.getTechnologyProfileId()), 0);
361 }
362
363
364 if (upstreamMeterId != null) {
365 treatmentBuilder.meter(upstreamMeterId);
366 }
367
368 if (!VlanId.NONE.equals(tagInformation.getPonCTag())) {
369 builder.addCondition(Criteria.matchVlanId(tagInformation.getPonCTag()));
370 }
371
372 if (tagInformation.getUsPonCTagPriority() != NO_PCP) {
373 builder.addCondition(Criteria.matchVlanPcp((byte) tagInformation.getUsPonCTagPriority()));
374 }
375 }
376
377 builder = install ? builder.permit() : builder.deny();
378
379 FilteringObjective igmp = builder
380 .withKey(Criteria.matchInPort(port))
381 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
382 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
383 .withMeta(treatmentBuilder
384 .setOutput(PortNumber.CONTROLLER).build())
385 .fromApp(appId)
386 .withPriority(MAX_PRIORITY)
387 .add(new ObjectiveContext() {
388 @Override
389 public void onSuccess(Objective objective) {
390 log.info("Igmp filter for {} on {} {}.",
391 devId, port, (install) ? INSTALLED : REMOVED);
392 }
393
394 @Override
395 public void onError(Objective objective, ObjectiveError error) {
396 log.info("Igmp filter for {} on {} failed {} because {}.",
397 devId, port, (install) ? INSTALLATION : REMOVAL,
398 error);
399 }
400 });
401
402 flowObjectiveService.filter(devId, igmp);
403 }
404
405 @Override
406 public void processEapolFilteringObjectives(DeviceId devId, PortNumber portNumber, String bpId,
407 CompletableFuture<ObjectiveError> filterFuture,
408 VlanId vlanId, boolean install) {
409
410 if (!enableEapol) {
411 log.debug("Eapol filtering is disabled. Completing filteringFuture immediately for the device {}", devId);
412 if (filterFuture != null) {
413 filterFuture.complete(null);
414 }
415 return;
416 }
417
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000418 BandwidthProfileInformation bpInfo = getBandwidthProfileInformation(bpId);
419 if (bpInfo == null) {
420 log.warn("Bandwidth profile {} is not found. Authentication flow"
421 + " will not be installed", bpId);
422 if (filterFuture != null) {
423 filterFuture.complete(ObjectiveError.BADPARAMS);
424 }
425 return;
426 }
427
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700428 ConnectPoint cp = new ConnectPoint(devId, portNumber);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000429 if (install) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700430 boolean added = pendingAddEapol.add(cp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000431 if (!added) {
432 if (filterFuture != null) {
433 log.warn("The eapol flow is processing for the port {}. Ignoring this request", portNumber);
434 filterFuture.complete(null);
435 }
436 return;
437 }
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700438 log.info("connectPoint added to pendingAddEapol map {}", cp.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000439 }
440
441 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
442 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
443 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
444
445 // check if meter exists and create it only for an install
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200446 final MeterId meterId = oltMeterService.getMeterIdFromBpMapping(devId, bpInfo.id());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000447 if (meterId == null) {
448 if (install) {
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200449 log.debug("Need to install meter for EAPOL with bwp {}", bpInfo.id());
450 SubscriberFlowInfo fi = new SubscriberFlowInfo(devId, null, cp.port(),
451 new UniTagInformation.Builder()
452 .setPonCTag(vlanId).build(),
453 null, null,
454 null, bpInfo.id());
455 pendingEapolForMeters.add(fi);
456
Andrea Campanella600d2e22020-06-22 11:00:31 +0200457 if (oltMeterService.isMeterPending(devId, bpInfo)) {
458 log.debug("Meter is already pending for EAPOL on {} with bp {}",
459 devId, bpInfo);
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200460 return;
461 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200462 oltMeterService.addToPendingMeters(devId, bpInfo);
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200463 MeterId innerMeterId = oltMeterService.createMeter(devId, bpInfo,
464 meterFuture);
465 fi.setUpMeterId(innerMeterId);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000466 } else {
467 // this case should not happen as the request to remove an eapol
468 // flow should mean that the flow points to a meter that exists.
469 // Nevertheless we can still delete the flow as we only need the
470 // correct 'match' to do so.
471 log.warn("Unknown meter id for bp {}, still proceeding with "
472 + "delete of eapol flow for {}/{}", bpInfo.id(), devId, portNumber);
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200473 SubscriberFlowInfo fi = new SubscriberFlowInfo(devId, null, cp.port(),
474 new UniTagInformation.Builder()
475 .setPonCTag(vlanId).build(),
476 null, meterId,
477 null, bpInfo.id());
478 handleEapol(filterFuture, install, cp, builder, treatmentBuilder, fi, meterId);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000479 }
480 } else {
481 log.debug("Meter {} was previously created for bp {}", meterId, bpInfo.id());
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200482 SubscriberFlowInfo fi = new SubscriberFlowInfo(devId, null, cp.port(),
483 new UniTagInformation.Builder()
484 .setPonCTag(vlanId).build(),
485 null, meterId,
486 null, bpInfo.id());
487 handleEapol(filterFuture, install, cp, builder, treatmentBuilder, fi, meterId);
488 //No need for the future, meter is present.
489 return;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000490 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000491 meterFuture.thenAcceptAsync(result -> {
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200492 //for each pending eapol flow we check if the meter is there.
493 Iterator<SubscriberFlowInfo> eapIterator = pendingEapolForMeters.iterator();
494 while (eapIterator.hasNext()) {
495 SubscriberFlowInfo fi = eapIterator.next();
496 if (result == null) {
497 MeterId mId = oltMeterService
Andrea Campanella600d2e22020-06-22 11:00:31 +0200498 .getMeterIdFromBpMapping(devId, fi.getUpBpInfo());
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200499 if (mId != null) {
500 handleEapol(filterFuture, install, cp, builder, treatmentBuilder, fi, mId);
501 eapIterator.remove();
502 }
503 } else {
504 log.warn("Meter installation error while sending eapol trap flow. " +
505 "Result {} and MeterId {}", result, meterId);
506 eapIterator.remove();
507 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200508 oltMeterService.removeFromPendingMeters(devId, bpInfo);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000509 }
510 });
511 }
512
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200513 private void handleEapol(CompletableFuture<ObjectiveError> filterFuture,
514 boolean install, ConnectPoint cp,
515 DefaultFilteringObjective.Builder builder,
516 TrafficTreatment.Builder treatmentBuilder,
517 SubscriberFlowInfo fi, MeterId mId) {
518 log.info("Meter {} for {} on {}/{} exists. {} EAPOL trap flow",
519 mId, fi.getUpBpInfo(), fi.getDevId(), fi.getUniPort(),
520 (install) ? "Installing" : "Removing");
521 int techProfileId = getDefaultTechProfileId(fi.getDevId(), fi.getUniPort());
522 // can happen in case of removal
523 if (mId != null) {
524 treatmentBuilder.meter(mId);
525 }
526 //Authentication trap flow uses only tech profile id as write metadata value
527 FilteringObjective eapol = (install ? builder.permit() : builder.deny())
528 .withKey(Criteria.matchInPort(fi.getUniPort()))
529 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
530 .addCondition(Criteria.matchVlanId(fi.getTagInfo().getPonCTag()))
531 .withMeta(treatmentBuilder
532 .writeMetadata(createTechProfValueForWm(
533 fi.getTagInfo().getPonCTag(),
534 techProfileId), 0)
535 .setOutput(PortNumber.CONTROLLER).build())
536 .fromApp(appId)
537 .withPriority(MAX_PRIORITY)
538 .add(new ObjectiveContext() {
539 @Override
540 public void onSuccess(Objective objective) {
Andrea Campanella600d2e22020-06-22 11:00:31 +0200541 log.info("Eapol filter {} for {} on {} {} with meter {}.",
542 objective.id(), fi.getDevId(), fi.getUniPort(),
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200543 (install) ? INSTALLED : REMOVED, mId);
544 if (filterFuture != null) {
545 filterFuture.complete(null);
546 }
547 pendingAddEapol.remove(cp);
548 }
549
550 @Override
551 public void onError(Objective objective, ObjectiveError error) {
Andrea Campanella600d2e22020-06-22 11:00:31 +0200552 log.error("Eapol filter {} for {} on {} with meter {} " +
553 "failed {} because {}", objective.id(),
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200554 fi.getDevId(), fi.getUniPort(), mId,
555 (install) ? INSTALLATION : REMOVAL,
556 error);
557 if (filterFuture != null) {
558 filterFuture.complete(error);
559 }
560 pendingAddEapol.remove(cp);
561 }
562 });
563 flowObjectiveService.filter(fi.getDevId(), eapol);
564 }
565
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000566 /**
567 * Installs trap filtering objectives for particular traffic types on an
568 * NNI port.
569 *
570 * @param devId device ID
571 * @param port port number
572 * @param install true to install, false to remove
573 */
574 @Override
575 public void processNniFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
Saurav Dasf62cea82020-08-26 17:43:04 -0700576 log.info("{} flows for NNI port {} on device {}",
577 install ? "Adding" : "Removing", port, devId);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000578 processLldpFilteringObjective(devId, port, install);
579 processDhcpFilteringObjectives(devId, port, null, null, install, false);
580 processIgmpFilteringObjectives(devId, port, null, null, install, false);
581 }
582
583
584 @Override
585 public void processLldpFilteringObjective(DeviceId devId, PortNumber port, boolean install) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000586 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
587
588 FilteringObjective lldp = (install ? builder.permit() : builder.deny())
589 .withKey(Criteria.matchInPort(port))
590 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
591 .withMeta(DefaultTrafficTreatment.builder()
592 .setOutput(PortNumber.CONTROLLER).build())
593 .fromApp(appId)
594 .withPriority(MAX_PRIORITY)
595 .add(new ObjectiveContext() {
596 @Override
597 public void onSuccess(Objective objective) {
598 log.info("LLDP filter for device {} on port {} {}.",
599 devId, port, (install) ? INSTALLED : REMOVED);
600 }
601
602 @Override
603 public void onError(Objective objective, ObjectiveError error) {
604 log.info("LLDP filter for device {} on port {} failed {} because {}",
605 devId, port, (install) ? INSTALLATION : REMOVAL,
606 error);
607 }
608 });
609
610 flowObjectiveService.filter(devId, lldp);
611 }
612
613 @Override
614 public ForwardingObjective.Builder createTransparentBuilder(PortNumber uplinkPort,
615 PortNumber subscriberPort,
616 MeterId meterId,
617 UniTagInformation tagInfo,
618 boolean upstream) {
619
620 TrafficSelector selector = DefaultTrafficSelector.builder()
621 .matchVlanId(tagInfo.getPonSTag())
622 .matchInPort(upstream ? subscriberPort : uplinkPort)
623 .matchInnerVlanId(tagInfo.getPonCTag())
624 .build();
625
626 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
627 if (meterId != null) {
628 tBuilder.meter(meterId);
629 }
630
631 TrafficTreatment treatment = tBuilder
632 .setOutput(upstream ? uplinkPort : subscriberPort)
633 .writeMetadata(createMetadata(upstream ? tagInfo.getPonSTag() : tagInfo.getPonCTag(),
634 tagInfo.getTechnologyProfileId(), upstream ? uplinkPort : subscriberPort), 0)
635 .build();
636
637 return createForwardingObjectiveBuilder(selector, treatment, MIN_PRIORITY);
638 }
639
640 @Override
641 public ForwardingObjective.Builder createUpBuilder(PortNumber uplinkPort,
642 PortNumber subscriberPort,
643 MeterId upstreamMeterId,
644 UniTagInformation uniTagInformation) {
645
646 TrafficSelector selector = DefaultTrafficSelector.builder()
647 .matchInPort(subscriberPort)
648 .matchVlanId(uniTagInformation.getUniTagMatch())
649 .build();
650
Andrea Campanella327c5722020-01-30 11:34:13 +0100651 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
652 //if the subscriberVlan (cTag) is different than ANY it needs to set.
653 if (uniTagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
654 treatmentBuilder.pushVlan()
655 .setVlanId(uniTagInformation.getPonCTag());
656 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000657
658 if (uniTagInformation.getUsPonCTagPriority() != NO_PCP) {
659 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonCTagPriority());
660 }
661
662 treatmentBuilder.pushVlan()
663 .setVlanId(uniTagInformation.getPonSTag());
664
665 if (uniTagInformation.getUsPonSTagPriority() != NO_PCP) {
666 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonSTagPriority());
667 }
668
669 treatmentBuilder.setOutput(uplinkPort)
670 .writeMetadata(createMetadata(uniTagInformation.getPonCTag(),
671 uniTagInformation.getTechnologyProfileId(), uplinkPort), 0L);
672
673 if (upstreamMeterId != null) {
674 treatmentBuilder.meter(upstreamMeterId);
675 }
676
677 return createForwardingObjectiveBuilder(selector, treatmentBuilder.build(), MIN_PRIORITY);
678 }
679
680 @Override
681 public ForwardingObjective.Builder createDownBuilder(PortNumber uplinkPort,
682 PortNumber subscriberPort,
683 MeterId downstreamMeterId,
684 UniTagInformation tagInformation) {
Andrea Campanella327c5722020-01-30 11:34:13 +0100685
686 //subscriberVlan can be any valid Vlan here including ANY to make sure the packet is tagged
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000687 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
688 .matchVlanId(tagInformation.getPonSTag())
689 .matchInPort(uplinkPort)
Andrea Campanella090e4a02020-02-05 13:53:55 +0100690 .matchInnerVlanId(tagInformation.getPonCTag());
691
692
693 if (tagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
694 selectorBuilder.matchMetadata(tagInformation.getPonCTag().toShort());
695 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000696
697 if (tagInformation.getDsPonSTagPriority() != NO_PCP) {
698 selectorBuilder.matchVlanPcp((byte) tagInformation.getDsPonSTagPriority());
699 }
700
701 if (tagInformation.getConfiguredMacAddress() != null &&
Daniele Moro7cbf4312020-03-06 17:24:12 -0800702 !tagInformation.getConfiguredMacAddress().equals("") &&
703 !MacAddress.NONE.equals(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()))) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000704 selectorBuilder.matchEthDst(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()));
705 }
706
707 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder()
708 .popVlan()
Andrea Campanella327c5722020-01-30 11:34:13 +0100709 .setOutput(subscriberPort);
710
Andrea Campanella327c5722020-01-30 11:34:13 +0100711 treatmentBuilder.writeMetadata(createMetadata(tagInformation.getPonCTag(),
712 tagInformation.getTechnologyProfileId(),
713 subscriberPort), 0);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000714
715 // to remark inner vlan header
716 if (tagInformation.getUsPonCTagPriority() != NO_PCP) {
717 treatmentBuilder.setVlanPcp((byte) tagInformation.getUsPonCTagPriority());
718 }
719
Andrea Campanella9a779292020-02-03 19:19:09 +0100720 if (!VlanId.NONE.equals(tagInformation.getUniTagMatch()) &&
721 tagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000722 treatmentBuilder.setVlanId(tagInformation.getUniTagMatch());
723 }
724
725 if (downstreamMeterId != null) {
726 treatmentBuilder.meter(downstreamMeterId);
727 }
728
729 return createForwardingObjectiveBuilder(selectorBuilder.build(), treatmentBuilder.build(), MIN_PRIORITY);
730 }
731
Andrea Campanella600d2e22020-06-22 11:00:31 +0200732 @Override
733 public void clearDeviceState(DeviceId deviceId) {
734 pendingEapolForMeters.removeIf(fi -> fi.getDevId().equals(deviceId));
735 pendingAddEapol.removeIf(connectPoint -> connectPoint.deviceId().equals(deviceId));
736
737 }
738
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000739 private DefaultForwardingObjective.Builder createForwardingObjectiveBuilder(TrafficSelector selector,
740 TrafficTreatment treatment,
741 Integer priority) {
742 return DefaultForwardingObjective.builder()
743 .withFlag(ForwardingObjective.Flag.VERSATILE)
744 .withPriority(priority)
745 .makePermanent()
746 .withSelector(selector)
747 .fromApp(appId)
748 .withTreatment(treatment);
749 }
750
751 /**
752 * Returns the write metadata value including tech profile reference and innerVlan.
753 * For param cVlan, null can be sent
754 *
755 * @param cVlan c (customer) tag of one subscriber
756 * @param techProfileId tech profile id of one subscriber
757 * @return the write metadata value including tech profile reference and innerVlan
758 */
759 private Long createTechProfValueForWm(VlanId cVlan, int techProfileId) {
760 if (cVlan == null || VlanId.NONE.equals(cVlan)) {
761 return (long) techProfileId << 32;
762 }
763 return ((long) (cVlan.id()) << 48 | (long) techProfileId << 32);
764 }
765
766 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
767 if (bandwidthProfile == null) {
768 return null;
769 }
770 return bpService.get(bandwidthProfile);
771 }
772
773 /**
774 * It will be used to support AT&T use case (for EAPOL flows).
775 * If multiple services are found in uniServiceList, returns default tech profile id
776 * If one service is found, returns the found one
777 *
778 * @param devId
779 * @param portNumber
780 * @return the default technology profile id
781 */
782 private int getDefaultTechProfileId(DeviceId devId, PortNumber portNumber) {
783 Port port = deviceService.getPort(devId, portNumber);
784 if (port != null) {
785 SubscriberAndDeviceInformation info = subsService.get(port.annotations().value(AnnotationKeys.PORT_NAME));
786 if (info != null && info.uniTagList().size() == 1) {
787 return info.uniTagList().get(0).getTechnologyProfileId();
788 }
789 }
790 return defaultTechProfileId;
791 }
792
793 /**
794 * Write metadata instruction value (metadata) is 8 bytes.
795 * <p>
796 * MS 2 bytes: C Tag
797 * Next 2 bytes: Technology Profile Id
798 * Next 4 bytes: Port number (uni or nni)
799 */
800 private Long createMetadata(VlanId innerVlan, int techProfileId, PortNumber egressPort) {
801 if (techProfileId == NONE_TP_ID) {
Andrea Campanella7c49b792020-05-11 11:36:53 +0200802 techProfileId = DEFAULT_TP_ID_DEFAULT;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000803 }
804
805 return ((long) (innerVlan.id()) << 48 | (long) techProfileId << 32) | egressPort.toLong();
806 }
807
808
809}