blob: 83032c09c4c2892587e892f1c572bd93f48def00 [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
18import com.google.common.collect.Sets;
19import org.onlab.packet.EthType;
20import org.onlab.packet.IPv4;
21import org.onlab.packet.IPv6;
22import org.onlab.packet.MacAddress;
23import org.onlab.packet.TpPort;
24import org.onlab.packet.VlanId;
25import org.onlab.util.Tools;
26import org.onosproject.cfg.ComponentConfigService;
27import org.onosproject.core.ApplicationId;
28import org.onosproject.core.CoreService;
29import org.onosproject.mastership.MastershipService;
30import org.onosproject.net.AnnotationKeys;
Matteo Scandolo3a037a32020-04-01 12:17:50 -070031import org.onosproject.net.ConnectPoint;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000032import org.onosproject.net.DeviceId;
33import org.onosproject.net.Port;
34import org.onosproject.net.PortNumber;
35import org.onosproject.net.device.DeviceService;
36import org.onosproject.net.flow.DefaultTrafficSelector;
37import org.onosproject.net.flow.DefaultTrafficTreatment;
38import org.onosproject.net.flow.TrafficSelector;
39import org.onosproject.net.flow.TrafficTreatment;
40import org.onosproject.net.flow.criteria.Criteria;
41import org.onosproject.net.flowobjective.DefaultFilteringObjective;
42import org.onosproject.net.flowobjective.DefaultForwardingObjective;
43import org.onosproject.net.flowobjective.FilteringObjective;
44import org.onosproject.net.flowobjective.FlowObjectiveService;
45import org.onosproject.net.flowobjective.ForwardingObjective;
46import org.onosproject.net.flowobjective.Objective;
47import org.onosproject.net.flowobjective.ObjectiveContext;
48import org.onosproject.net.flowobjective.ObjectiveError;
49import org.onosproject.net.meter.MeterId;
50import org.opencord.olt.internalapi.AccessDeviceFlowService;
51import org.opencord.olt.internalapi.AccessDeviceMeterService;
52import org.opencord.sadis.BandwidthProfileInformation;
53import org.opencord.sadis.BaseInformationService;
54import org.opencord.sadis.SadisService;
55import org.opencord.sadis.SubscriberAndDeviceInformation;
56import org.opencord.sadis.UniTagInformation;
57import org.osgi.service.component.ComponentContext;
58import org.osgi.service.component.annotations.Activate;
59import org.osgi.service.component.annotations.Component;
60import org.osgi.service.component.annotations.Deactivate;
61import org.osgi.service.component.annotations.Modified;
62import org.osgi.service.component.annotations.Reference;
63import org.osgi.service.component.annotations.ReferenceCardinality;
64import org.slf4j.Logger;
65
66import java.util.Dictionary;
67import java.util.Properties;
68import java.util.Set;
69import java.util.concurrent.CompletableFuture;
70
71import static com.google.common.base.Strings.isNullOrEmpty;
72import static org.onlab.util.Tools.get;
73import static org.opencord.olt.impl.OsgiPropertyConstants.*;
74import static org.slf4j.LoggerFactory.getLogger;
75
76/**
77 * Provisions flow rules on access devices.
78 */
79@Component(immediate = true, property = {
80 ENABLE_DHCP_ON_PROVISIONING + ":Boolean=" + ENABLE_DHCP_ON_PROVISIONING_DEFAULT,
81 ENABLE_DHCP_V4 + ":Boolean=" + ENABLE_DHCP_V4_DEFAULT,
82 ENABLE_DHCP_V6 + ":Boolean=" + ENABLE_DHCP_V6_DEFAULT,
83 ENABLE_IGMP_ON_PROVISIONING + ":Boolean=" + ENABLE_IGMP_ON_PROVISIONING_DEFAULT,
84 ENABLE_EAPOL + ":Boolean=" + ENABLE_EAPOL_DEFAULT,
85 DEFAULT_TP_ID + ":Integer=" + DEFAULT_TP_ID_DEFAULT
86})
87public class OltFlowService implements AccessDeviceFlowService {
88
89 private static final String APP_NAME = "org.opencord.olt";
90 private static final int NONE_TP_ID = -1;
91 private static final int NO_PCP = -1;
92 private static final Integer MAX_PRIORITY = 10000;
93 private static final Integer MIN_PRIORITY = 1000;
Andrea Campanellacbbb7952019-11-25 06:38:41 +000094 private static final String INSTALLED = "installed";
95 private static final String REMOVED = "removed";
96 private static final String INSTALLATION = "installation";
97 private static final String REMOVAL = "removal";
98 private static final String V4 = "V4";
99 private static final String V6 = "V6";
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000100
101 private final Logger log = getLogger(getClass());
102
103 @Reference(cardinality = ReferenceCardinality.MANDATORY)
104 protected FlowObjectiveService flowObjectiveService;
105
106 @Reference(cardinality = ReferenceCardinality.MANDATORY)
107 protected CoreService coreService;
108
109 @Reference(cardinality = ReferenceCardinality.MANDATORY)
110 protected MastershipService mastershipService;
111
112 @Reference(cardinality = ReferenceCardinality.MANDATORY)
113 protected SadisService sadisService;
114
115 @Reference(cardinality = ReferenceCardinality.MANDATORY)
116 protected DeviceService deviceService;
117
118 @Reference(cardinality = ReferenceCardinality.MANDATORY)
119 protected AccessDeviceMeterService oltMeterService;
120
121 @Reference(cardinality = ReferenceCardinality.MANDATORY)
122 protected ComponentConfigService componentConfigService;
123
124 /**
125 * Create the DHCP Flow rules when a subscriber is provisioned.
126 **/
127 protected boolean enableDhcpOnProvisioning = ENABLE_DHCP_ON_PROVISIONING_DEFAULT;
128
129 /**
130 * Enable flows for DHCP v4.
131 **/
132 protected boolean enableDhcpV4 = ENABLE_DHCP_V4_DEFAULT;
133
134 /**
135 * Enable flows for DHCP v6.
136 **/
137 protected boolean enableDhcpV6 = ENABLE_DHCP_V6_DEFAULT;
138
139 /**
140 * Create IGMP Flow rules when a subscriber is provisioned.
141 **/
142 protected boolean enableIgmpOnProvisioning = ENABLE_IGMP_ON_PROVISIONING_DEFAULT;
143
144 /**
145 * Send EAPOL authentication trap flows before subscriber provisioning.
146 **/
147 protected boolean enableEapol = ENABLE_EAPOL_DEFAULT;
148
149 /**
150 * Default technology profile id that is used for authentication trap flows.
151 **/
152 protected int defaultTechProfileId = DEFAULT_TP_ID_DEFAULT;
153
154 protected ApplicationId appId;
155 protected BaseInformationService<BandwidthProfileInformation> bpService;
156 protected BaseInformationService<SubscriberAndDeviceInformation> subsService;
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700157 private Set<ConnectPoint> pendingAddEapol = Sets.newConcurrentHashSet();
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000158
159 @Activate
160 public void activate(ComponentContext context) {
161 bpService = sadisService.getBandwidthProfileService();
162 subsService = sadisService.getSubscriberInfoService();
163 componentConfigService.registerProperties(getClass());
164 appId = coreService.getAppId(APP_NAME);
Andrea Campanellafee86422020-06-04 16:01:27 +0200165 log.info("started");
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000166 }
167
168
169 @Deactivate
170 public void deactivate(ComponentContext context) {
171 componentConfigService.unregisterProperties(getClass(), false);
Andrea Campanellafee86422020-06-04 16:01:27 +0200172 log.info("stopped");
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000173 }
174
175 @Modified
176 public void modified(ComponentContext context) {
177
178 Dictionary<?, ?> properties = context != null ? context.getProperties() : new Properties();
179
Andrea Campanella7c49b792020-05-11 11:36:53 +0200180 Boolean o = Tools.isPropertyEnabled(properties, ENABLE_DHCP_ON_PROVISIONING);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000181 if (o != null) {
182 enableDhcpOnProvisioning = o;
183 }
184
Andrea Campanella7c49b792020-05-11 11:36:53 +0200185 Boolean v4 = Tools.isPropertyEnabled(properties, ENABLE_DHCP_V4);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000186 if (v4 != null) {
187 enableDhcpV4 = v4;
188 }
189
Andrea Campanella7c49b792020-05-11 11:36:53 +0200190 Boolean v6 = Tools.isPropertyEnabled(properties, ENABLE_DHCP_V6);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000191 if (v6 != null) {
192 enableDhcpV6 = v6;
193 }
194
Andrea Campanella7c49b792020-05-11 11:36:53 +0200195 Boolean p = Tools.isPropertyEnabled(properties, ENABLE_IGMP_ON_PROVISIONING);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000196 if (p != null) {
197 enableIgmpOnProvisioning = p;
198 }
199
Andrea Campanella7c49b792020-05-11 11:36:53 +0200200 Boolean eap = Tools.isPropertyEnabled(properties, ENABLE_EAPOL);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000201 if (eap != null) {
202 enableEapol = eap;
203 }
204
Andrea Campanella7c49b792020-05-11 11:36:53 +0200205 String tpId = get(properties, DEFAULT_TP_ID);
206 defaultTechProfileId = isNullOrEmpty(tpId) ? DEFAULT_TP_ID_DEFAULT : Integer.parseInt(tpId.trim());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000207
Andrea Campanellafee86422020-06-04 16:01:27 +0200208 log.info("modified. Values = enableDhcpOnProvisioning: {}, enableDhcpV4: {}, " +
209 "enableDhcpV6:{}, enableIgmpOnProvisioning:{}, " +
210 "enableEapol{}, defaultTechProfileId: {}",
211 enableDhcpOnProvisioning, enableDhcpV4, enableDhcpV6,
212 enableIgmpOnProvisioning, enableEapol, defaultTechProfileId);
213
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000214 }
215
216 @Override
217 public void processDhcpFilteringObjectives(DeviceId devId, PortNumber port,
218 MeterId upstreamMeterId,
219 UniTagInformation tagInformation,
220 boolean install,
221 boolean upstream) {
Andrea Campanella7c49b792020-05-11 11:36:53 +0200222 //tagInformation can be none in case of NNI port.
223 //in that case relying on global flag
224 if (!enableDhcpOnProvisioning || (tagInformation != null
225 && !tagInformation.getIsDhcpRequired())) {
226 log.debug("Dhcp provisioning is disabled for port {} on device {}", devId, port);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000227 return;
228 }
229
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000230 int techProfileId = tagInformation != null ? tagInformation.getTechnologyProfileId() : NONE_TP_ID;
231 VlanId cTag = tagInformation != null ? tagInformation.getPonCTag() : VlanId.NONE;
232 VlanId unitagMatch = tagInformation != null ? tagInformation.getUniTagMatch() : VlanId.ANY;
233
234 if (enableDhcpV4) {
235 int udpSrc = (upstream) ? 68 : 67;
236 int udpDst = (upstream) ? 67 : 68;
237
238 EthType ethType = EthType.EtherType.IPV4.ethType();
239 byte protocol = IPv4.PROTOCOL_UDP;
240
Andrea Campanella7c49b792020-05-11 11:36:53 +0200241 addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000242 upstreamMeterId, techProfileId, protocol, cTag, unitagMatch, install);
243 }
244
245 if (enableDhcpV6) {
246 int udpSrc = (upstream) ? 547 : 546;
247 int udpDst = (upstream) ? 546 : 547;
248
249 EthType ethType = EthType.EtherType.IPV6.ethType();
250 byte protocol = IPv6.PROTOCOL_UDP;
251
Andrea Campanella7c49b792020-05-11 11:36:53 +0200252 addDhcpFilteringObjectives(devId, port, udpSrc, udpDst, ethType,
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000253 upstreamMeterId, techProfileId, protocol, cTag, unitagMatch, install);
254 }
255 }
256
257 private void addDhcpFilteringObjectives(DeviceId devId, PortNumber port, int udpSrc, int udpDst,
258 EthType ethType, MeterId upstreamMeterId, int techProfileId, byte protocol,
259 VlanId cTag, VlanId unitagMatch, boolean install) {
260
261 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
262 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
263
264 if (upstreamMeterId != null) {
265 treatmentBuilder.meter(upstreamMeterId);
266 }
267
268 if (techProfileId != NONE_TP_ID) {
269 treatmentBuilder.writeMetadata(createTechProfValueForWm(unitagMatch, techProfileId), 0);
270 }
271
272 FilteringObjective.Builder dhcpUpstreamBuilder = (install ? builder.permit() : builder.deny())
273 .withKey(Criteria.matchInPort(port))
274 .addCondition(Criteria.matchEthType(ethType))
275 .addCondition(Criteria.matchIPProtocol(protocol))
276 .addCondition(Criteria.matchUdpSrc(TpPort.tpPort(udpSrc)))
277 .addCondition(Criteria.matchUdpDst(TpPort.tpPort(udpDst)))
278 .withMeta(treatmentBuilder
279 .setOutput(PortNumber.CONTROLLER).build())
280 .fromApp(appId)
281 .withPriority(MAX_PRIORITY);
282
283 if (!VlanId.NONE.equals(cTag)) {
284 dhcpUpstreamBuilder.addCondition(Criteria.matchVlanId(cTag));
285 }
286
287 FilteringObjective dhcpUpstream = dhcpUpstreamBuilder.add(new ObjectiveContext() {
288 @Override
289 public void onSuccess(Objective objective) {
290 log.info("DHCP {} filter for device {} on port {} {}.",
291 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
292 devId, port, (install) ? INSTALLED : REMOVED);
293 }
294
295 @Override
296 public void onError(Objective objective, ObjectiveError error) {
297 log.info("DHCP {} filter for device {} on port {} failed {} because {}",
298 (ethType.equals(EthType.EtherType.IPV4.ethType())) ? V4 : V6,
299 devId, port, (install) ? INSTALLATION : REMOVAL,
300 error);
301 }
302 });
303
304 flowObjectiveService.filter(devId, dhcpUpstream);
305
306 }
307
308 @Override
309 public void processIgmpFilteringObjectives(DeviceId devId, PortNumber port,
310 MeterId upstreamMeterId,
311 UniTagInformation tagInformation,
312 boolean install,
313 boolean upstream) {
Andrea Campanella7c49b792020-05-11 11:36:53 +0200314 //tagInformation can be none in case of NNI port.
315 //in that case relying on global flag
316 if (!enableIgmpOnProvisioning || (tagInformation != null
317 && !tagInformation.getIsIgmpRequired())) {
318 log.debug("Igmp provisioning is disabled for port {} on device {}", devId, port);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000319 return;
320 }
321
Matteo Scandolo34556e52020-05-08 12:34:13 -0700322 if (!upstream) {
Andrea Campanella7c49b792020-05-11 11:36:53 +0200323 log.debug("Direction on port {} for device {} " +
324 "is not Upstream, ignoring Igmp request", port, devId);
Matteo Scandolo34556e52020-05-08 12:34:13 -0700325 return;
326 }
327
Andrea Campanellafee86422020-06-04 16:01:27 +0200328 log.debug("{} IGMP flows on {}:{}", (install) ?
329 "Installing" : "Removing", devId, port);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000330 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
331 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
332 if (upstream) {
333
334 if (tagInformation.getTechnologyProfileId() != NONE_TP_ID) {
335 treatmentBuilder.writeMetadata(createTechProfValueForWm(null,
336 tagInformation.getTechnologyProfileId()), 0);
337 }
338
339
340 if (upstreamMeterId != null) {
341 treatmentBuilder.meter(upstreamMeterId);
342 }
343
344 if (!VlanId.NONE.equals(tagInformation.getPonCTag())) {
345 builder.addCondition(Criteria.matchVlanId(tagInformation.getPonCTag()));
346 }
347
348 if (tagInformation.getUsPonCTagPriority() != NO_PCP) {
349 builder.addCondition(Criteria.matchVlanPcp((byte) tagInformation.getUsPonCTagPriority()));
350 }
351 }
352
353 builder = install ? builder.permit() : builder.deny();
354
355 FilteringObjective igmp = builder
356 .withKey(Criteria.matchInPort(port))
357 .addCondition(Criteria.matchEthType(EthType.EtherType.IPV4.ethType()))
358 .addCondition(Criteria.matchIPProtocol(IPv4.PROTOCOL_IGMP))
359 .withMeta(treatmentBuilder
360 .setOutput(PortNumber.CONTROLLER).build())
361 .fromApp(appId)
362 .withPriority(MAX_PRIORITY)
363 .add(new ObjectiveContext() {
364 @Override
365 public void onSuccess(Objective objective) {
366 log.info("Igmp filter for {} on {} {}.",
367 devId, port, (install) ? INSTALLED : REMOVED);
368 }
369
370 @Override
371 public void onError(Objective objective, ObjectiveError error) {
372 log.info("Igmp filter for {} on {} failed {} because {}.",
373 devId, port, (install) ? INSTALLATION : REMOVAL,
374 error);
375 }
376 });
377
378 flowObjectiveService.filter(devId, igmp);
379 }
380
381 @Override
382 public void processEapolFilteringObjectives(DeviceId devId, PortNumber portNumber, String bpId,
383 CompletableFuture<ObjectiveError> filterFuture,
384 VlanId vlanId, boolean install) {
385
386 if (!enableEapol) {
387 log.debug("Eapol filtering is disabled. Completing filteringFuture immediately for the device {}", devId);
388 if (filterFuture != null) {
389 filterFuture.complete(null);
390 }
391 return;
392 }
393
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000394 BandwidthProfileInformation bpInfo = getBandwidthProfileInformation(bpId);
395 if (bpInfo == null) {
396 log.warn("Bandwidth profile {} is not found. Authentication flow"
397 + " will not be installed", bpId);
398 if (filterFuture != null) {
399 filterFuture.complete(ObjectiveError.BADPARAMS);
400 }
401 return;
402 }
403
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700404 ConnectPoint cp = new ConnectPoint(devId, portNumber);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000405 if (install) {
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700406 boolean added = pendingAddEapol.add(cp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000407 if (!added) {
408 if (filterFuture != null) {
409 log.warn("The eapol flow is processing for the port {}. Ignoring this request", portNumber);
410 filterFuture.complete(null);
411 }
412 return;
413 }
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700414 log.info("connectPoint added to pendingAddEapol map {}", cp.toString());
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000415 }
416
417 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
418 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
419 CompletableFuture<Object> meterFuture = new CompletableFuture<>();
420
421 // check if meter exists and create it only for an install
422 MeterId meterId = oltMeterService.getMeterIdFromBpMapping(devId, bpInfo.id());
423 if (meterId == null) {
424 if (install) {
425 meterId = oltMeterService.createMeter(devId, bpInfo, meterFuture);
426 treatmentBuilder.meter(meterId);
427 } else {
428 // this case should not happen as the request to remove an eapol
429 // flow should mean that the flow points to a meter that exists.
430 // Nevertheless we can still delete the flow as we only need the
431 // correct 'match' to do so.
432 log.warn("Unknown meter id for bp {}, still proceeding with "
433 + "delete of eapol flow for {}/{}", bpInfo.id(), devId, portNumber);
434 meterFuture.complete(null);
435 }
436 } else {
437 log.debug("Meter {} was previously created for bp {}", meterId, bpInfo.id());
438 treatmentBuilder.meter(meterId);
439 meterFuture.complete(null);
440 }
441
442 final MeterId mId = meterId;
443 meterFuture.thenAcceptAsync(result -> {
444 if (result == null) {
445 log.info("Meter {} for {} on {}/{} exists. {} EAPOL trap flow",
446 mId, bpId, devId, portNumber,
447 (install) ? "Installing" : "Removing");
448 int techProfileId = getDefaultTechProfileId(devId, portNumber);
449
450 //Authentication trap flow uses only tech profile id as write metadata value
451 FilteringObjective eapol = (install ? builder.permit() : builder.deny())
452 .withKey(Criteria.matchInPort(portNumber))
453 .addCondition(Criteria.matchEthType(EthType.EtherType.EAPOL.ethType()))
454 .addCondition(Criteria.matchVlanId(vlanId))
455 .withMeta(treatmentBuilder
456 .writeMetadata(createTechProfValueForWm(vlanId, techProfileId), 0)
457 .setOutput(PortNumber.CONTROLLER).build())
458 .fromApp(appId)
459 .withPriority(MAX_PRIORITY)
460 .add(new ObjectiveContext() {
461 @Override
462 public void onSuccess(Objective objective) {
463 log.info("Eapol filter for {} on {} {} with meter {}.",
464 devId, portNumber, (install) ? INSTALLED : REMOVED, mId);
465 if (filterFuture != null) {
466 filterFuture.complete(null);
467 }
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700468 pendingAddEapol.remove(cp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000469 }
470
471 @Override
472 public void onError(Objective objective, ObjectiveError error) {
473 log.info("Eapol filter for {} on {} with meter {} failed {} because {}",
474 devId, portNumber, mId, (install) ? INSTALLATION : REMOVAL,
475 error);
476 if (filterFuture != null) {
477 filterFuture.complete(error);
478 }
Matteo Scandolo3a037a32020-04-01 12:17:50 -0700479 pendingAddEapol.remove(cp);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000480 }
481 });
482
483 flowObjectiveService.filter(devId, eapol);
484 } else {
485 log.warn("Meter installation error while sending eapol trap flow. " +
486 "Result {} and MeterId {}", result, mId);
487 }
488 });
489 }
490
491 /**
492 * Installs trap filtering objectives for particular traffic types on an
493 * NNI port.
494 *
495 * @param devId device ID
496 * @param port port number
497 * @param install true to install, false to remove
498 */
499 @Override
500 public void processNniFilteringObjectives(DeviceId devId, PortNumber port, boolean install) {
501 log.info("Sending flows for NNI port {} of the device {}", port, devId);
502 processLldpFilteringObjective(devId, port, install);
503 processDhcpFilteringObjectives(devId, port, null, null, install, false);
504 processIgmpFilteringObjectives(devId, port, null, null, install, false);
505 }
506
507
508 @Override
509 public void processLldpFilteringObjective(DeviceId devId, PortNumber port, boolean install) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000510 DefaultFilteringObjective.Builder builder = DefaultFilteringObjective.builder();
511
512 FilteringObjective lldp = (install ? builder.permit() : builder.deny())
513 .withKey(Criteria.matchInPort(port))
514 .addCondition(Criteria.matchEthType(EthType.EtherType.LLDP.ethType()))
515 .withMeta(DefaultTrafficTreatment.builder()
516 .setOutput(PortNumber.CONTROLLER).build())
517 .fromApp(appId)
518 .withPriority(MAX_PRIORITY)
519 .add(new ObjectiveContext() {
520 @Override
521 public void onSuccess(Objective objective) {
522 log.info("LLDP filter for device {} on port {} {}.",
523 devId, port, (install) ? INSTALLED : REMOVED);
524 }
525
526 @Override
527 public void onError(Objective objective, ObjectiveError error) {
528 log.info("LLDP filter for device {} on port {} failed {} because {}",
529 devId, port, (install) ? INSTALLATION : REMOVAL,
530 error);
531 }
532 });
533
534 flowObjectiveService.filter(devId, lldp);
535 }
536
537 @Override
538 public ForwardingObjective.Builder createTransparentBuilder(PortNumber uplinkPort,
539 PortNumber subscriberPort,
540 MeterId meterId,
541 UniTagInformation tagInfo,
542 boolean upstream) {
543
544 TrafficSelector selector = DefaultTrafficSelector.builder()
545 .matchVlanId(tagInfo.getPonSTag())
546 .matchInPort(upstream ? subscriberPort : uplinkPort)
547 .matchInnerVlanId(tagInfo.getPonCTag())
548 .build();
549
550 TrafficTreatment.Builder tBuilder = DefaultTrafficTreatment.builder();
551 if (meterId != null) {
552 tBuilder.meter(meterId);
553 }
554
555 TrafficTreatment treatment = tBuilder
556 .setOutput(upstream ? uplinkPort : subscriberPort)
557 .writeMetadata(createMetadata(upstream ? tagInfo.getPonSTag() : tagInfo.getPonCTag(),
558 tagInfo.getTechnologyProfileId(), upstream ? uplinkPort : subscriberPort), 0)
559 .build();
560
561 return createForwardingObjectiveBuilder(selector, treatment, MIN_PRIORITY);
562 }
563
564 @Override
565 public ForwardingObjective.Builder createUpBuilder(PortNumber uplinkPort,
566 PortNumber subscriberPort,
567 MeterId upstreamMeterId,
568 UniTagInformation uniTagInformation) {
569
570 TrafficSelector selector = DefaultTrafficSelector.builder()
571 .matchInPort(subscriberPort)
572 .matchVlanId(uniTagInformation.getUniTagMatch())
573 .build();
574
Andrea Campanella327c5722020-01-30 11:34:13 +0100575 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder();
576 //if the subscriberVlan (cTag) is different than ANY it needs to set.
577 if (uniTagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
578 treatmentBuilder.pushVlan()
579 .setVlanId(uniTagInformation.getPonCTag());
580 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000581
582 if (uniTagInformation.getUsPonCTagPriority() != NO_PCP) {
583 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonCTagPriority());
584 }
585
586 treatmentBuilder.pushVlan()
587 .setVlanId(uniTagInformation.getPonSTag());
588
589 if (uniTagInformation.getUsPonSTagPriority() != NO_PCP) {
590 treatmentBuilder.setVlanPcp((byte) uniTagInformation.getUsPonSTagPriority());
591 }
592
593 treatmentBuilder.setOutput(uplinkPort)
594 .writeMetadata(createMetadata(uniTagInformation.getPonCTag(),
595 uniTagInformation.getTechnologyProfileId(), uplinkPort), 0L);
596
597 if (upstreamMeterId != null) {
598 treatmentBuilder.meter(upstreamMeterId);
599 }
600
601 return createForwardingObjectiveBuilder(selector, treatmentBuilder.build(), MIN_PRIORITY);
602 }
603
604 @Override
605 public ForwardingObjective.Builder createDownBuilder(PortNumber uplinkPort,
606 PortNumber subscriberPort,
607 MeterId downstreamMeterId,
608 UniTagInformation tagInformation) {
Andrea Campanella327c5722020-01-30 11:34:13 +0100609
610 //subscriberVlan can be any valid Vlan here including ANY to make sure the packet is tagged
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000611 TrafficSelector.Builder selectorBuilder = DefaultTrafficSelector.builder()
612 .matchVlanId(tagInformation.getPonSTag())
613 .matchInPort(uplinkPort)
Andrea Campanella090e4a02020-02-05 13:53:55 +0100614 .matchInnerVlanId(tagInformation.getPonCTag());
615
616
617 if (tagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
618 selectorBuilder.matchMetadata(tagInformation.getPonCTag().toShort());
619 }
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000620
621 if (tagInformation.getDsPonSTagPriority() != NO_PCP) {
622 selectorBuilder.matchVlanPcp((byte) tagInformation.getDsPonSTagPriority());
623 }
624
625 if (tagInformation.getConfiguredMacAddress() != null &&
Daniele Moro7cbf4312020-03-06 17:24:12 -0800626 !tagInformation.getConfiguredMacAddress().equals("") &&
627 !MacAddress.NONE.equals(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()))) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000628 selectorBuilder.matchEthDst(MacAddress.valueOf(tagInformation.getConfiguredMacAddress()));
629 }
630
631 TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder()
632 .popVlan()
Andrea Campanella327c5722020-01-30 11:34:13 +0100633 .setOutput(subscriberPort);
634
Andrea Campanella327c5722020-01-30 11:34:13 +0100635 treatmentBuilder.writeMetadata(createMetadata(tagInformation.getPonCTag(),
636 tagInformation.getTechnologyProfileId(),
637 subscriberPort), 0);
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000638
639 // to remark inner vlan header
640 if (tagInformation.getUsPonCTagPriority() != NO_PCP) {
641 treatmentBuilder.setVlanPcp((byte) tagInformation.getUsPonCTagPriority());
642 }
643
Andrea Campanella9a779292020-02-03 19:19:09 +0100644 if (!VlanId.NONE.equals(tagInformation.getUniTagMatch()) &&
645 tagInformation.getPonCTag().toShort() != VlanId.ANY_VALUE) {
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000646 treatmentBuilder.setVlanId(tagInformation.getUniTagMatch());
647 }
648
649 if (downstreamMeterId != null) {
650 treatmentBuilder.meter(downstreamMeterId);
651 }
652
653 return createForwardingObjectiveBuilder(selectorBuilder.build(), treatmentBuilder.build(), MIN_PRIORITY);
654 }
655
656 private DefaultForwardingObjective.Builder createForwardingObjectiveBuilder(TrafficSelector selector,
657 TrafficTreatment treatment,
658 Integer priority) {
659 return DefaultForwardingObjective.builder()
660 .withFlag(ForwardingObjective.Flag.VERSATILE)
661 .withPriority(priority)
662 .makePermanent()
663 .withSelector(selector)
664 .fromApp(appId)
665 .withTreatment(treatment);
666 }
667
668 /**
669 * Returns the write metadata value including tech profile reference and innerVlan.
670 * For param cVlan, null can be sent
671 *
672 * @param cVlan c (customer) tag of one subscriber
673 * @param techProfileId tech profile id of one subscriber
674 * @return the write metadata value including tech profile reference and innerVlan
675 */
676 private Long createTechProfValueForWm(VlanId cVlan, int techProfileId) {
677 if (cVlan == null || VlanId.NONE.equals(cVlan)) {
678 return (long) techProfileId << 32;
679 }
680 return ((long) (cVlan.id()) << 48 | (long) techProfileId << 32);
681 }
682
683 private BandwidthProfileInformation getBandwidthProfileInformation(String bandwidthProfile) {
684 if (bandwidthProfile == null) {
685 return null;
686 }
687 return bpService.get(bandwidthProfile);
688 }
689
690 /**
691 * It will be used to support AT&T use case (for EAPOL flows).
692 * If multiple services are found in uniServiceList, returns default tech profile id
693 * If one service is found, returns the found one
694 *
695 * @param devId
696 * @param portNumber
697 * @return the default technology profile id
698 */
699 private int getDefaultTechProfileId(DeviceId devId, PortNumber portNumber) {
700 Port port = deviceService.getPort(devId, portNumber);
701 if (port != null) {
702 SubscriberAndDeviceInformation info = subsService.get(port.annotations().value(AnnotationKeys.PORT_NAME));
703 if (info != null && info.uniTagList().size() == 1) {
704 return info.uniTagList().get(0).getTechnologyProfileId();
705 }
706 }
707 return defaultTechProfileId;
708 }
709
710 /**
711 * Write metadata instruction value (metadata) is 8 bytes.
712 * <p>
713 * MS 2 bytes: C Tag
714 * Next 2 bytes: Technology Profile Id
715 * Next 4 bytes: Port number (uni or nni)
716 */
717 private Long createMetadata(VlanId innerVlan, int techProfileId, PortNumber egressPort) {
718 if (techProfileId == NONE_TP_ID) {
Andrea Campanella7c49b792020-05-11 11:36:53 +0200719 techProfileId = DEFAULT_TP_ID_DEFAULT;
Andrea Campanellacbbb7952019-11-25 06:38:41 +0000720 }
721
722 return ((long) (innerVlan.id()) << 48 | (long) techProfileId << 32) | egressPort.toLong();
723 }
724
725
726}