blob: f934c548aa485d1f9bb1dae150436112c27033c4 [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 Campanella600d2e22020-06-22 11:00:31 +0200476 if (oltMeterService.isMeterPending(devId, bpInfo)) {
477 log.debug("Meter is already pending for EAPOL on {} with bp {}",
478 devId, bpInfo);
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200479 return;
480 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200481 oltMeterService.addToPendingMeters(devId, bpInfo);
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200482 MeterId innerMeterId = oltMeterService.createMeter(devId, bpInfo,
483 meterFuture);
484 fi.setUpMeterId(innerMeterId);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000485 } else {
486 // this case should not happen as the request to remove an eapol
487 // flow should mean that the flow points to a meter that exists.
488 // Nevertheless we can still delete the flow as we only need the
489 // correct 'match' to do so.
490 log.warn("Unknown meter id for bp {}, still proceeding with "
491 + "delete of eapol flow for {}/{}", bpInfo.id(), devId, portNumber);
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200492 SubscriberFlowInfo fi = new SubscriberFlowInfo(devId, null, cp.port(),
493 new UniTagInformation.Builder()
494 .setPonCTag(vlanId).build(),
495 null, meterId,
496 null, bpInfo.id());
Andrea Campanella0e34f562020-06-11 10:47:10 +0200497 handleEapol(filterFuture, install, cp, filterBuilder, treatmentBuilder, fi, meterId);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000498 }
499 } else {
500 log.debug("Meter {} was previously created for bp {}", meterId, bpInfo.id());
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200501 SubscriberFlowInfo fi = new SubscriberFlowInfo(devId, null, cp.port(),
502 new UniTagInformation.Builder()
503 .setPonCTag(vlanId).build(),
504 null, meterId,
505 null, bpInfo.id());
Andrea Campanella0e34f562020-06-11 10:47:10 +0200506 handleEapol(filterFuture, install, cp, filterBuilder, treatmentBuilder, fi, meterId);
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200507 //No need for the future, meter is present.
508 return;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000509 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000510 meterFuture.thenAcceptAsync(result -> {
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200511 //for each pending eapol flow we check if the meter is there.
512 Iterator<SubscriberFlowInfo> eapIterator = pendingEapolForMeters.iterator();
513 while (eapIterator.hasNext()) {
514 SubscriberFlowInfo fi = eapIterator.next();
515 if (result == null) {
516 MeterId mId = oltMeterService
Andrea Campanella600d2e22020-06-22 11:00:31 +0200517 .getMeterIdFromBpMapping(devId, fi.getUpBpInfo());
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200518 if (mId != null) {
Andrea Campanella0e34f562020-06-11 10:47:10 +0200519 handleEapol(filterFuture, install, cp, filterBuilder, treatmentBuilder, fi, mId);
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200520 eapIterator.remove();
521 }
522 } else {
523 log.warn("Meter installation error while sending eapol trap flow. " +
524 "Result {} and MeterId {}", result, meterId);
525 eapIterator.remove();
526 }
Andrea Campanella600d2e22020-06-22 11:00:31 +0200527 oltMeterService.removeFromPendingMeters(devId, bpInfo);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000528 }
529 });
530 }
531
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200532 private void handleEapol(CompletableFuture<ObjectiveError> filterFuture,
533 boolean install, ConnectPoint cp,
Andrea Campanella0e34f562020-06-11 10:47:10 +0200534 DefaultFilteringObjective.Builder filterBuilder,
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200535 TrafficTreatment.Builder treatmentBuilder,
536 SubscriberFlowInfo fi, MeterId mId) {
537 log.info("Meter {} for {} on {}/{} exists. {} EAPOL trap flow",
538 mId, fi.getUpBpInfo(), fi.getDevId(), fi.getUniPort(),
539 (install) ? "Installing" : "Removing");
540 int techProfileId = getDefaultTechProfileId(fi.getDevId(), fi.getUniPort());
541 // can happen in case of removal
542 if (mId != null) {
543 treatmentBuilder.meter(mId);
544 }
545 //Authentication trap flow uses only tech profile id as write metadata value
Andrea Campanella0e34f562020-06-11 10:47:10 +0200546 FilteringObjective eapol = (install ? filterBuilder.permit() : filterBuilder.deny())
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200547 .withKey(Criteria.matchInPort(fi.getUniPort()))
548 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200549 .withMeta(treatmentBuilder
550 .writeMetadata(createTechProfValueForWm(
551 fi.getTagInfo().getPonCTag(),
552 techProfileId), 0)
Andrea Campanella0e34f562020-06-11 10:47:10 +0200553 .setOutput(PortNumber.CONTROLLER)
554 .pushVlan()
555 .setVlanId(fi.getTagInfo().getPonCTag())
556 .build())
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200557 .fromApp(appId)
558 .withPriority(MAX_PRIORITY)
559 .add(new ObjectiveContext() {
560 @Override
561 public void onSuccess(Objective objective) {
Andrea Campanella600d2e22020-06-22 11:00:31 +0200562 log.info("Eapol filter {} for {} on {} {} with meter {}.",
563 objective.id(), fi.getDevId(), fi.getUniPort(),
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200564 (install) ? INSTALLED : REMOVED, mId);
565 if (filterFuture != null) {
566 filterFuture.complete(null);
567 }
568 pendingAddEapol.remove(cp);
569 }
570
571 @Override
572 public void onError(Objective objective, ObjectiveError error) {
Andrea Campanella600d2e22020-06-22 11:00:31 +0200573 log.error("Eapol filter {} for {} on {} with meter {} " +
574 "failed {} because {}", objective.id(),
Andrea Campanella3ce4d282020-06-09 13:46:58 +0200575 fi.getDevId(), fi.getUniPort(), mId,
576 (install) ? INSTALLATION : REMOVAL,
577 error);
578 if (filterFuture != null) {
579 filterFuture.complete(error);
580 }
581 pendingAddEapol.remove(cp);
582 }
583 });
584 flowObjectiveService.filter(fi.getDevId(), eapol);
585 }
586
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000587 /**
588 * Installs trap filtering objectives for particular traffic types on an
589 * NNI port.
590 *
591 * @param devId device ID
592 * @param port port number
593 * @param install true to install, false to remove
594 */
595 @Override
596 public void processNniFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
Saurav Dasf62cea82020-08-26 17:43:04 -0700597 log.info("{} flows for NNI port {} on device {}",
598 install ? "Adding" : "Removing", port, devId);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000599 processLldpFilteringObjective(devId, port, install);
600 processDhcpFilteringObjectives(devId, port, null, null, install, false);
601 processIgmpFilteringObjectives(devId, port, null, null, install, false);
602 }
603
604
605 @Override
606 public void processLldpFilteringObjective(DeviceId devId, PortNumber port, boolean install) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000607 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
608
609 FilteringObjective lldp = (install ? builder.permit() : builder.deny())
610 .withKey(Criteria.matchInPort(port))
611 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
612 .withMeta(DefaultTrafficTreatment.builder()
613 .setOutput(PortNumber.CONTROLLER).build())
614 .fromApp(appId)
615 .withPriority(MAX_PRIORITY)
616 .add(new ObjectiveContext() {
617 @Override
618 public void onSuccess(Objective objective) {
619 log.info("LLDP filter for device {} on port {} {}.",
620 devId, port, (install) ? INSTALLED : REMOVED);
621 }
622
623 @Override
624 public void onError(Objective objective, ObjectiveError error) {
625 log.info("LLDP filter for device {} on port {} failed {} because {}",
626 devId, port, (install) ? INSTALLATION : REMOVAL,
627 error);
628 }
629 });
630
631 flowObjectiveService.filter(devId, lldp);
632 }
633
634 @Override
635 public ForwardingObjective.Builder createTransparentBuilder(PortNumber uplinkPort,
636 PortNumber subscriberPort,
637 MeterId meterId,
638 UniTagInformation tagInfo,
639 boolean upstream) {
640
641 TrafficSelector selector = DefaultTrafficSelector.builder()
642 .matchVlanId(tagInfo.getPonSTag())
643 .matchInPort(upstream ? subscriberPort : uplinkPort)
644 .matchInnerVlanId(tagInfo.getPonCTag())
645 .build();
646
647 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
648 if (meterId != null) {
649 tBuilder.meter(meterId);
650 }
651
652 TrafficTreatment treatment = tBuilder
653 .setOutput(upstream ? uplinkPort : subscriberPort)
654 .writeMetadata(createMetadata(upstream ? tagInfo.getPonSTag() : tagInfo.getPonCTag(),
655 tagInfo.getTechnologyProfileId(), upstream ? uplinkPort : subscriberPort), 0)
656 .build();
657
658 return createForwardingObjectiveBuilder(selector, treatment, MIN_PRIORITY);
659 }
660
661 @Override
662 public ForwardingObjective.Builder createUpBuilder(PortNumber uplinkPort,
663 PortNumber subscriberPort,
664 MeterId upstreamMeterId,
665 UniTagInformation uniTagInformation) {
666
667 TrafficSelector selector = DefaultTrafficSelector.builder()
668 .matchInPort(subscriberPort)
669 .matchVlanId(uniTagInformation.getUniTagMatch())
670 .build();
671
Andrea Campanella327c5722020-01-30 11:34:13 +0100672 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
673 //if the subscriberVlan (cTag) is different than ANY it needs to set.
674 if (uniTagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
675 treatmentBuilder.pushVlan()
676 .setVlanId(uniTagInformation.getPonCTag());
677 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000678
679 if (uniTagInformation.getUsPonCTagPriority() != NO_PCP) {
680 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonCTagPriority());
681 }
682
683 treatmentBuilder.pushVlan()
684 .setVlanId(uniTagInformation.getPonSTag());
685
686 if (uniTagInformation.getUsPonSTagPriority() != NO_PCP) {
687 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonSTagPriority());
688 }
689
690 treatmentBuilder.setOutput(uplinkPort)
691 .writeMetadata(createMetadata(uniTagInformation.getPonCTag(),
692 uniTagInformation.getTechnologyProfileId(), uplinkPort), 0L);
693
694 if (upstreamMeterId != null) {
695 treatmentBuilder.meter(upstreamMeterId);
696 }
697
698 return createForwardingObjectiveBuilder(selector, treatmentBuilder.build(), MIN_PRIORITY);
699 }
700
701 @Override
702 public ForwardingObjective.Builder createDownBuilder(PortNumber uplinkPort,
703 PortNumber subscriberPort,
704 MeterId downstreamMeterId,
705 UniTagInformation tagInformation) {
Andrea Campanella327c5722020-01-30 11:34:13 +0100706
707 //subscriberVlan can be any valid Vlan here including ANY to make sure the packet is tagged
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000708 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
709 .matchVlanId(tagInformation.getPonSTag())
710 .matchInPort(uplinkPort)
Andrea Campanella090e4a02020-02-05 13:53:55 +0100711 .matchInnerVlanId(tagInformation.getPonCTag());
712
713
714 if (tagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
715 selectorBuilder.matchMetadata(tagInformation.getPonCTag().toShort());
716 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000717
718 if (tagInformation.getDsPonSTagPriority() != NO_PCP) {
719 selectorBuilder.matchVlanPcp((byte) tagInformation.getDsPonSTagPriority());
720 }
721
722 if (tagInformation.getConfiguredMacAddress() != null &&
Daniele Moro7cbf4312020-03-06 17:24:12 -0800723 !tagInformation.getConfiguredMacAddress().equals("") &&
724 !MacAddress.NONE.equals(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()))) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000725 selectorBuilder.matchEthDst(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()));
726 }
727
728 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder()
729 .popVlan()
Andrea Campanella327c5722020-01-30 11:34:13 +0100730 .setOutput(subscriberPort);
731
Andrea Campanella327c5722020-01-30 11:34:13 +0100732 treatmentBuilder.writeMetadata(createMetadata(tagInformation.getPonCTag(),
733 tagInformation.getTechnologyProfileId(),
734 subscriberPort), 0);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000735
736 // to remark inner vlan header
737 if (tagInformation.getUsPonCTagPriority() != NO_PCP) {
738 treatmentBuilder.setVlanPcp((byte) tagInformation.getUsPonCTagPriority());
739 }
740
Andrea Campanella9a779292020-02-03 19:19:09 +0100741 if (!VlanId.NONE.equals(tagInformation.getUniTagMatch()) &&
742 tagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000743 treatmentBuilder.setVlanId(tagInformation.getUniTagMatch());
744 }
745
746 if (downstreamMeterId != null) {
747 treatmentBuilder.meter(downstreamMeterId);
748 }
749
750 return createForwardingObjectiveBuilder(selectorBuilder.build(), treatmentBuilder.build(), MIN_PRIORITY);
751 }
752
Andrea Campanella600d2e22020-06-22 11:00:31 +0200753 @Override
754 public void clearDeviceState(DeviceId deviceId) {
755 pendingEapolForMeters.removeIf(fi -> fi.getDevId().equals(deviceId));
756 pendingAddEapol.removeIf(connectPoint -> connectPoint.deviceId().equals(deviceId));
757
758 }
759
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000760 private DefaultForwardingObjective.Builder createForwardingObjectiveBuilder(TrafficSelector selector,
761 TrafficTreatment treatment,
762 Integer priority) {
763 return DefaultForwardingObjective.builder()
764 .withFlag(ForwardingObjective.Flag.VERSATILE)
765 .withPriority(priority)
766 .makePermanent()
767 .withSelector(selector)
768 .fromApp(appId)
769 .withTreatment(treatment);
770 }
771
772 /**
773 * Returns the write metadata value including tech profile reference and innerVlan.
774 * For param cVlan, null can be sent
775 *
776 * @param cVlan c (customer) tag of one subscriber
777 * @param techProfileId tech profile id of one subscriber
778 * @return the write metadata value including tech profile reference and innerVlan
779 */
780 private Long createTechProfValueForWm(VlanId cVlan, int techProfileId) {
781 if (cVlan == null || VlanId.NONE.equals(cVlan)) {
782 return (long) techProfileId << 32;
783 }
784 return ((long) (cVlan.id()) << 48 | (long) techProfileId << 32);
785 }
786
787 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
788 if (bandwidthProfile == null) {
789 return null;
790 }
791 return bpService.get(bandwidthProfile);
792 }
793
794 /**
795 * It will be used to support AT&T use case (for EAPOL flows).
796 * If multiple services are found in uniServiceList, returns default tech profile id
797 * If one service is found, returns the found one
798 *
799 * @param devId
800 * @param portNumber
801 * @return the default technology profile id
802 */
803 private int getDefaultTechProfileId(DeviceId devId, PortNumber portNumber) {
804 Port port = deviceService.getPort(devId, portNumber);
805 if (port != null) {
806 SubscriberAndDeviceInformation info = subsService.get(port.annotations().value(AnnotationKeys.PORT_NAME));
807 if (info != null && info.uniTagList().size() == 1) {
808 return info.uniTagList().get(0).getTechnologyProfileId();
809 }
810 }
811 return defaultTechProfileId;
812 }
813
814 /**
815 * Write metadata instruction value (metadata) is 8 bytes.
816 * <p>
817 * MS 2 bytes: C Tag
818 * Next 2 bytes: Technology Profile Id
819 * Next 4 bytes: Port number (uni or nni)
820 */
821 private Long createMetadata(VlanId innerVlan, int techProfileId, PortNumber egressPort) {
822 if (techProfileId == NONE_TP_ID) {
Andrea Campanella7c49b792020-05-11 11:36:53 +0200823 techProfileId = DEFAULT_TP_ID_DEFAULT;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000824 }
825
826 return ((long) (innerVlan.id()) << 48 | (long) techProfileId << 32) | egressPort.toLong();
827 }
828
829
830}