Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2017-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 | */ |
| 16 | |
| 17 | package org.onosproject.pipelines.fabric.pipeliner; |
| 18 | |
| 19 | import com.google.common.collect.ImmutableMap; |
| 20 | import com.google.common.collect.ImmutableSet; |
| 21 | import com.google.common.collect.Sets; |
Esin Karaman | 971fb7f | 2017-12-28 13:44:52 +0000 | [diff] [blame] | 22 | import org.onlab.packet.MacAddress; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 23 | import org.onosproject.net.flow.criteria.Criterion; |
Esin Karaman | 971fb7f | 2017-12-28 13:44:52 +0000 | [diff] [blame] | 24 | import org.onosproject.net.flow.criteria.EthCriterion; |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 25 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 26 | |
| 27 | import java.util.Map; |
| 28 | import java.util.Set; |
| 29 | |
| 30 | import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_DST; |
| 31 | import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE; |
| 32 | import static org.onosproject.net.flow.criteria.Criterion.Type.IPV4_DST; |
| 33 | import static org.onosproject.net.flow.criteria.Criterion.Type.IPV6_DST; |
| 34 | import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS; |
| 35 | import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_LABEL; |
| 36 | import static org.onosproject.net.flow.criteria.Criterion.Type.VLAN_VID; |
| 37 | |
| 38 | public enum ForwardingFunctionType { |
| 39 | /** |
| 40 | * L2 unicast, with vlan id + mac address criterion. |
| 41 | */ |
| 42 | L2_UNICAST, |
| 43 | |
| 44 | /** |
| 45 | * L2 broadcast, with vlan id criterion only. |
| 46 | */ |
| 47 | L2_BROADCAST, |
| 48 | |
| 49 | /** |
| 50 | * IPv4 unicast, with EtherType and IPv4 unicast destination address. |
| 51 | */ |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame^] | 52 | IPV4_ROUTING, |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 53 | |
| 54 | /** |
| 55 | * IPv6 unicast, with EtherType and IPv6 unicast destination address. |
| 56 | */ |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame^] | 57 | IPV6_ROUTING, |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 58 | |
| 59 | /** |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 60 | * MPLS, with EtherType, MPLS label and MPLS BOS(true) criterion. |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 61 | */ |
| 62 | MPLS, |
| 63 | |
| 64 | /** |
Yi Tseng | 1b154bd | 2017-11-20 17:48:19 -0800 | [diff] [blame] | 65 | * Pseudo-wire, with EtherType, MPLS label and MPLS BOS(false) criterion. |
| 66 | */ |
| 67 | PW, |
| 68 | |
| 69 | /** |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 70 | * Unsupported type. |
| 71 | */ |
| 72 | UNSUPPORTED; |
| 73 | |
| 74 | // Different criteria combinations for different FFT |
| 75 | private static final Set<Criterion.Type> L2_UNI_CRITERIA_TYPE = |
| 76 | ImmutableSet.of(VLAN_VID, ETH_DST); |
| 77 | private static final Set<Criterion.Type> L2_BRC_CRITERIA_TYPE = |
| 78 | ImmutableSet.of(VLAN_VID); |
| 79 | private static final Set<Criterion.Type> IPV4_UNI_CRITERIA_TYPE = |
| 80 | ImmutableSet.of(ETH_TYPE, IPV4_DST); |
| 81 | private static final Set<Criterion.Type> IPV4_MCAST_CRITERIA_TYPE = |
| 82 | ImmutableSet.of(ETH_TYPE, VLAN_VID, IPV4_DST); |
| 83 | private static final Set<Criterion.Type> IPV6_UNI_CRITERIA_TYPE = |
| 84 | ImmutableSet.of(ETH_TYPE, IPV6_DST); |
| 85 | private static final Set<Criterion.Type> IPV6_MCAST_CRITERIA_TYPE = |
| 86 | ImmutableSet.of(ETH_TYPE, VLAN_VID, IPV6_DST); |
| 87 | private static final Set<Criterion.Type> MPLS_UNI_CRITERIA_TYPE = |
| 88 | ImmutableSet.of(ETH_TYPE, MPLS_LABEL, MPLS_BOS); |
| 89 | |
| 90 | private static final Map<Set<Criterion.Type>, ForwardingFunctionType> FFT_MAP = |
| 91 | ImmutableMap.<Set<Criterion.Type>, ForwardingFunctionType>builder() |
| 92 | .put(L2_UNI_CRITERIA_TYPE, L2_UNICAST) |
| 93 | .put(L2_BRC_CRITERIA_TYPE, L2_BROADCAST) |
Charles Chan | 384aea2 | 2018-08-23 22:08:02 -0700 | [diff] [blame^] | 94 | .put(IPV4_UNI_CRITERIA_TYPE, IPV4_ROUTING) |
| 95 | .put(IPV4_MCAST_CRITERIA_TYPE, IPV4_ROUTING) |
| 96 | .put(IPV6_UNI_CRITERIA_TYPE, IPV6_ROUTING) |
| 97 | .put(IPV6_MCAST_CRITERIA_TYPE, IPV6_ROUTING) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 98 | .put(MPLS_UNI_CRITERIA_TYPE, MPLS) |
| 99 | .build(); |
| 100 | |
| 101 | /** |
| 102 | * Gets forwarding function type of the forwarding objective. |
| 103 | * |
| 104 | * @param fwd the forwarding objective |
| 105 | * @return forwarding function type of the forwarding objective |
| 106 | */ |
| 107 | public static ForwardingFunctionType getForwardingFunctionType(ForwardingObjective fwd) { |
| 108 | Set<Criterion.Type> criteriaType = Sets.newHashSet(); |
Esin Karaman | 971fb7f | 2017-12-28 13:44:52 +0000 | [diff] [blame] | 109 | //omit the criterion of ethDst type with the value of MacAddress.NONE since it indicates L2 broadcast |
| 110 | //if not, the objective is treated as L2 unicast |
| 111 | fwd.selector().criteria().stream().filter(criterion -> !ethDstIndicatesBroadcast(criterion)) |
| 112 | .map(Criterion::type) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 113 | .forEach(criteriaType::add); |
| 114 | |
| 115 | if (fwd.meta() != null) { |
Esin Karaman | 971fb7f | 2017-12-28 13:44:52 +0000 | [diff] [blame] | 116 | fwd.meta().criteria().stream().filter(criterion -> !ethDstIndicatesBroadcast(criterion)) |
| 117 | .map(Criterion::type) |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 118 | .forEach(criteriaType::add); |
| 119 | } |
| 120 | |
| 121 | return FFT_MAP.getOrDefault(criteriaType, UNSUPPORTED); |
| 122 | } |
Esin Karaman | 971fb7f | 2017-12-28 13:44:52 +0000 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * Segment Routing (SR) app. sets ethDst criterion to MacAddress.NONE for broadcast rules |
| 126 | * and demands drivers to treat the flow rules containing this criterion as broadcast rule. |
| 127 | * |
| 128 | * This method checks the type and value of the criterion and detects whether it constitutes |
| 129 | * broadcast or not. |
| 130 | * |
| 131 | * For more details check RoutingRulePopulator.updateSubnetBroadcastRule method of SR app. |
| 132 | * |
| 133 | * @param criterion Criterion object |
| 134 | * @return true if the type is ETH_DST and the mac value equals to MacAddress.NONE; false otherwise. |
| 135 | */ |
| 136 | private static boolean ethDstIndicatesBroadcast(Criterion criterion) { |
| 137 | return criterion.type().equals(Criterion.Type.ETH_DST) && |
| 138 | ((EthCriterion) criterion).mac().equals(MacAddress.NONE); |
| 139 | } |
Yi Tseng | 0b80972 | 2017-11-03 10:23:26 -0700 | [diff] [blame] | 140 | } |