Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 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 | */ |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 16 | package org.onosproject.driver.pipeline.ofdpa; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 17 | |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 18 | import com.google.common.collect.ImmutableList; |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 20 | import org.onlab.osgi.ServiceDirectory; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 21 | import org.onlab.packet.Ethernet; |
Julia Ferguson | d8f145e | 2017-08-10 18:15:24 +0000 | [diff] [blame] | 22 | import org.onlab.packet.IpAddress; |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 23 | import org.onlab.packet.IpPrefix; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 24 | import org.onlab.packet.VlanId; |
| 25 | import org.onlab.util.KryoNamespace; |
| 26 | import org.onosproject.core.ApplicationId; |
| 27 | import org.onosproject.core.CoreService; |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 28 | import org.onosproject.driver.extensions.Ofdpa3MplsType; |
| 29 | import org.onosproject.driver.extensions.Ofdpa3SetMplsType; |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 30 | import org.onosproject.driver.extensions.OfdpaMatchVlanVid; |
| 31 | import org.onosproject.driver.extensions.OfdpaSetVlanVid; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.Port; |
| 34 | import org.onosproject.net.PortNumber; |
| 35 | import org.onosproject.net.behaviour.NextGroup; |
| 36 | import org.onosproject.net.behaviour.Pipeliner; |
| 37 | import org.onosproject.net.behaviour.PipelinerContext; |
| 38 | import org.onosproject.net.device.DeviceService; |
| 39 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
| 40 | import org.onosproject.net.flow.DefaultFlowRule; |
| 41 | import org.onosproject.net.flow.DefaultTrafficSelector; |
| 42 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
| 43 | import org.onosproject.net.flow.FlowRule; |
| 44 | import org.onosproject.net.flow.FlowRuleOperations; |
| 45 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 46 | import org.onosproject.net.flow.FlowRuleService; |
| 47 | import org.onosproject.net.flow.TrafficSelector; |
| 48 | import org.onosproject.net.flow.TrafficTreatment; |
| 49 | import org.onosproject.net.flow.criteria.Criteria; |
| 50 | import org.onosproject.net.flow.criteria.Criterion; |
| 51 | import org.onosproject.net.flow.criteria.EthCriterion; |
| 52 | import org.onosproject.net.flow.criteria.EthTypeCriterion; |
| 53 | import org.onosproject.net.flow.criteria.IPCriterion; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 54 | import org.onosproject.net.flow.criteria.Icmpv6CodeCriterion; |
| 55 | import org.onosproject.net.flow.criteria.Icmpv6TypeCriterion; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 56 | import org.onosproject.net.flow.criteria.MplsBosCriterion; |
| 57 | import org.onosproject.net.flow.criteria.MplsCriterion; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 58 | import org.onosproject.net.flow.criteria.PortCriterion; |
Charles Chan | 30122a7 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 59 | import org.onosproject.net.flow.criteria.TcpPortCriterion; |
| 60 | import org.onosproject.net.flow.criteria.UdpPortCriterion; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 61 | import org.onosproject.net.flow.criteria.VlanIdCriterion; |
| 62 | import org.onosproject.net.flow.instructions.Instruction; |
| 63 | import org.onosproject.net.flow.instructions.Instructions.OutputInstruction; |
| 64 | import org.onosproject.net.flow.instructions.L2ModificationInstruction; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 65 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.L2SubType; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 66 | import org.onosproject.net.flow.instructions.L2ModificationInstruction.ModVlanIdInstruction; |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 67 | import org.onosproject.net.flow.instructions.L3ModificationInstruction; |
| 68 | import org.onosproject.net.flow.instructions.L3ModificationInstruction.L3SubType; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 69 | import org.onosproject.net.flowobjective.FilteringObjective; |
| 70 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
| 71 | import org.onosproject.net.flowobjective.ForwardingObjective; |
| 72 | import org.onosproject.net.flowobjective.NextObjective; |
| 73 | import org.onosproject.net.flowobjective.Objective; |
| 74 | import org.onosproject.net.flowobjective.ObjectiveError; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 75 | import org.onosproject.net.group.DefaultGroupKey; |
| 76 | import org.onosproject.net.group.Group; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 77 | import org.onosproject.net.group.GroupKey; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 78 | import org.onosproject.net.group.GroupService; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 79 | import org.onosproject.store.serializers.KryoNamespaces; |
| 80 | import org.slf4j.Logger; |
| 81 | |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 82 | import java.util.ArrayDeque; |
| 83 | import java.util.ArrayList; |
| 84 | import java.util.Collection; |
| 85 | import java.util.Collections; |
| 86 | import java.util.Deque; |
| 87 | import java.util.List; |
| 88 | import java.util.Objects; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 89 | import java.util.concurrent.ScheduledExecutorService; |
| 90 | import java.util.concurrent.TimeUnit; |
| 91 | |
| 92 | import static java.util.concurrent.Executors.newScheduledThreadPool; |
Yi Tseng | 3a77b4f | 2017-02-06 15:02:17 -0800 | [diff] [blame] | 93 | import static org.onlab.packet.MacAddress.BROADCAST; |
Charles Chan | 206cac0 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 94 | import static org.onlab.packet.MacAddress.IPV4_MULTICAST; |
| 95 | import static org.onlab.packet.MacAddress.IPV6_MULTICAST; |
Yi Tseng | 3a77b4f | 2017-02-06 15:02:17 -0800 | [diff] [blame] | 96 | import static org.onlab.packet.MacAddress.NONE; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 97 | import static org.onlab.util.Tools.groupedThreads; |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 98 | import static org.onosproject.driver.pipeline.ofdpa.OfdpaGroupHandlerUtility.*; |
Charles Chan | 30122a7 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 99 | import static org.onosproject.net.flow.criteria.Criterion.Type.ETH_TYPE; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 100 | import static org.slf4j.LoggerFactory.getLogger; |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 101 | import static org.onosproject.net.flow.criteria.Criterion.Type.MPLS_BOS; |
| 102 | import static org.onosproject.net.flowobjective.NextObjective.Type.HASHED; |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 103 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 104 | /** |
| 105 | * Driver for Broadcom's OF-DPA v2.0 TTP. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 106 | */ |
Charles Chan | 361154b | 2016-03-24 10:23:39 -0700 | [diff] [blame] | 107 | public class Ofdpa2Pipeline extends AbstractHandlerBehaviour implements Pipeliner { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 108 | protected static final int PORT_TABLE = 0; |
| 109 | protected static final int VLAN_TABLE = 10; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 110 | protected static final int VLAN_1_TABLE = 11; |
| 111 | protected static final int MPLS_L2_PORT_FLOW_TABLE = 13; |
| 112 | protected static final int MPLS_L2_PORT_PCP_TRUST_FLOW_TABLE = 16; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 113 | protected static final int TMAC_TABLE = 20; |
| 114 | protected static final int UNICAST_ROUTING_TABLE = 30; |
| 115 | protected static final int MULTICAST_ROUTING_TABLE = 40; |
| 116 | protected static final int MPLS_TABLE_0 = 23; |
| 117 | protected static final int MPLS_TABLE_1 = 24; |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 118 | protected static final int MPLS_L3_TYPE_TABLE = 27; |
| 119 | protected static final int MPLS_TYPE_TABLE = 29; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 120 | protected static final int BRIDGING_TABLE = 50; |
| 121 | protected static final int ACL_TABLE = 60; |
| 122 | protected static final int MAC_LEARNING_TABLE = 254; |
| 123 | protected static final long OFPP_MAX = 0xffffff00L; |
| 124 | |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 125 | protected static final int HIGHEST_PRIORITY = 0xffff; |
Saurav Das | 2857f38 | 2015-11-03 14:39:27 -0800 | [diff] [blame] | 126 | protected static final int DEFAULT_PRIORITY = 0x8000; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 127 | protected static final int LOWEST_PRIORITY = 0x0; |
| 128 | |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 129 | protected static final int MPLS_L2_PORT_PRIORITY = 2; |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 130 | protected static final int MPLS_TUNNEL_ID_BASE = 0x10000; |
| 131 | protected static final int MPLS_TUNNEL_ID_MAX = 0x1FFFF; |
| 132 | |
Pier Ventre | 70d53ba | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 133 | protected static final int MPLS_UNI_PORT_MAX = 0x0000FFFF; |
Pier Ventre | 70d53ba | 2016-11-17 22:26:29 -0800 | [diff] [blame] | 134 | protected static final int MPLS_NNI_PORT_BASE = 0x00020000; |
| 135 | protected static final int MPLS_NNI_PORT_MAX = 0x0002FFFF; |
| 136 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 137 | private final Logger log = getLogger(getClass()); |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 138 | protected ServiceDirectory serviceDirectory; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 139 | protected FlowRuleService flowRuleService; |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 140 | protected CoreService coreService; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 141 | protected GroupService groupService; |
| 142 | protected FlowObjectiveStore flowObjectiveStore; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 143 | protected DeviceId deviceId; |
| 144 | protected ApplicationId driverId; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 145 | protected DeviceService deviceService; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 146 | protected static KryoNamespace appKryo = new KryoNamespace.Builder() |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 147 | .register(KryoNamespaces.API) |
| 148 | .register(GroupKey.class) |
| 149 | .register(DefaultGroupKey.class) |
| 150 | .register(OfdpaNextGroup.class) |
| 151 | .register(ArrayDeque.class) |
| 152 | .build("Ofdpa2Pipeline"); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 153 | |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 154 | protected Ofdpa2GroupHandler groupHandler; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 155 | |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 156 | // flows installations to be retried |
Charles Chan | c6e64bb | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 157 | private ScheduledExecutorService executorService |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 158 | = newScheduledThreadPool(5, groupedThreads("OfdpaPipeliner", "retry-%d", log)); |
Charles Chan | c6e64bb | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 159 | private static final int MAX_RETRY_ATTEMPTS = 10; |
| 160 | private static final int RETRY_MS = 1000; |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 161 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 162 | @Override |
| 163 | public void init(DeviceId deviceId, PipelinerContext context) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 164 | this.deviceId = deviceId; |
| 165 | |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 166 | serviceDirectory = context.directory(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 167 | coreService = serviceDirectory.get(CoreService.class); |
| 168 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
| 169 | groupService = serviceDirectory.get(GroupService.class); |
| 170 | flowObjectiveStore = context.store(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 171 | deviceService = serviceDirectory.get(DeviceService.class); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 172 | |
Charles Chan | 40132b3 | 2017-01-22 00:19:37 -0800 | [diff] [blame] | 173 | initDriverId(); |
| 174 | initGroupHander(context); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 175 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 176 | initializePipeline(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Charles Chan | 40132b3 | 2017-01-22 00:19:37 -0800 | [diff] [blame] | 179 | protected void initDriverId() { |
| 180 | driverId = coreService.registerApplication( |
| 181 | "org.onosproject.driver.Ofdpa2Pipeline"); |
| 182 | } |
| 183 | |
| 184 | protected void initGroupHander(PipelinerContext context) { |
| 185 | groupHandler = new Ofdpa2GroupHandler(); |
| 186 | groupHandler.init(deviceId, context); |
| 187 | } |
| 188 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 189 | protected void initializePipeline() { |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 190 | // OF-DPA does not require initializing the pipeline as it puts default |
| 191 | // rules automatically in the hardware. However emulation of OFDPA in |
| 192 | // software switches does require table-miss-entries. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 193 | } |
| 194 | |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 195 | /** |
| 196 | * Determines whether this pipeline requires OFDPA match and set VLAN extensions. |
| 197 | * |
| 198 | * @return true to use the extensions |
| 199 | */ |
| 200 | protected boolean requireVlanExtensions() { |
| 201 | return true; |
| 202 | } |
| 203 | |
Saurav Das | 86d13e8 | 2017-04-28 17:03:48 -0700 | [diff] [blame] | 204 | /** |
| 205 | * Determines whether in-port should be matched on in TMAC table rules. |
| 206 | * |
| 207 | * @return true if match on in-port should be programmed |
| 208 | */ |
| 209 | protected boolean matchInPortTmacTable() { |
| 210 | return true; |
| 211 | } |
| 212 | |
Charles Chan | 30122a7 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 213 | /** |
| 214 | * Determines whether matching L4 destination port on IPv6 packets is supported in ACL table. |
| 215 | * |
| 216 | * @return true if matching L4 destination port on IPv6 packets is supported in ACL table. |
| 217 | */ |
| 218 | protected boolean supportIpv6L4Dst() { |
| 219 | return true; |
| 220 | } |
| 221 | |
Saurav Das | 9504700 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 222 | /** |
| 223 | * Determines whether this driver should continue to retry flows that point |
| 224 | * to empty groups. See CORD-554. |
| 225 | * |
| 226 | * @return true if the driver should retry flows |
| 227 | */ |
| 228 | protected boolean shouldRetry() { |
| 229 | return true; |
| 230 | } |
| 231 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 232 | ////////////////////////////////////// |
| 233 | // Flow Objectives |
| 234 | ////////////////////////////////////// |
| 235 | |
| 236 | @Override |
| 237 | public void filter(FilteringObjective filteringObjective) { |
| 238 | if (filteringObjective.type() == FilteringObjective.Type.PERMIT) { |
| 239 | processFilter(filteringObjective, |
| 240 | filteringObjective.op() == Objective.Operation.ADD, |
| 241 | filteringObjective.appId()); |
| 242 | } else { |
| 243 | // Note that packets that don't match the PERMIT filter are |
| 244 | // automatically denied. The DENY filter is used to deny packets |
| 245 | // that are otherwise permitted by the PERMIT filter. |
| 246 | // Use ACL table flow rules here for DENY filtering objectives |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 247 | log.warn("filter objective other than PERMIT currently not supported"); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 248 | fail(filteringObjective, ObjectiveError.UNSUPPORTED); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public void forward(ForwardingObjective fwd) { |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 254 | Collection<FlowRule> rules = processForward(fwd); |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 255 | if (rules == null || rules.isEmpty()) { |
| 256 | // Assumes fail message has already been generated to the objective |
| 257 | // context. Returning here prevents spurious pass message to be |
| 258 | // generated by FlowRule service for empty flowOps. |
| 259 | return; |
| 260 | } |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 261 | sendForward(fwd, rules); |
| 262 | } |
| 263 | |
Charles Chan | c6e64bb | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 264 | private void sendForward(ForwardingObjective fwd, Collection<FlowRule> rules) { |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 265 | FlowRuleOperations.Builder flowOpsBuilder = FlowRuleOperations.builder(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 266 | switch (fwd.op()) { |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 267 | case ADD: |
| 268 | rules.stream() |
| 269 | .filter(Objects::nonNull) |
| 270 | .forEach(flowOpsBuilder::add); |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 271 | log.debug("Applying a add fwd-obj {} to sw:{}", fwd.id(), deviceId); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 272 | break; |
| 273 | case REMOVE: |
| 274 | rules.stream() |
| 275 | .filter(Objects::nonNull) |
| 276 | .forEach(flowOpsBuilder::remove); |
Pier Ventre | 42287df | 2016-11-09 14:17:26 -0800 | [diff] [blame] | 277 | log.debug("Deleting a flow rule to sw:{}", deviceId); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 278 | break; |
| 279 | default: |
| 280 | fail(fwd, ObjectiveError.UNKNOWN); |
| 281 | log.warn("Unknown forwarding type {}", fwd.op()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 282 | } |
| 283 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 284 | flowRuleService.apply(flowOpsBuilder.build(new FlowRuleOperationsContext() { |
| 285 | @Override |
| 286 | public void onSuccess(FlowRuleOperations ops) { |
| 287 | pass(fwd); |
| 288 | } |
| 289 | |
| 290 | @Override |
| 291 | public void onError(FlowRuleOperations ops) { |
| 292 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 293 | } |
| 294 | })); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 295 | } |
| 296 | |
| 297 | @Override |
| 298 | public void next(NextObjective nextObjective) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 299 | NextGroup nextGroup = flowObjectiveStore.getNextGroup(nextObjective.id()); |
| 300 | switch (nextObjective.op()) { |
| 301 | case ADD: |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 302 | if (nextGroup != null) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 303 | log.warn("Cannot add next {} that already exists in device {}", |
| 304 | nextObjective.id(), deviceId); |
| 305 | return; |
| 306 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 307 | log.debug("Processing NextObjective id {} in dev {} - add group", |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 308 | nextObjective.id(), deviceId); |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 309 | groupHandler.addGroup(nextObjective); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 310 | break; |
| 311 | case ADD_TO_EXISTING: |
| 312 | if (nextGroup != null) { |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 313 | log.debug("Processing NextObjective id {} in dev {} - add bucket", |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 314 | nextObjective.id(), deviceId); |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 315 | groupHandler.addBucketToGroup(nextObjective, nextGroup); |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 316 | } else { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 317 | // it is possible that group-chain has not been fully created yet |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 318 | log.debug("Waiting to add bucket to group for next-id:{} in dev:{}", |
| 319 | nextObjective.id(), deviceId); |
Yi Tseng | 47f82dc | 2017-03-05 22:48:39 -0800 | [diff] [blame] | 320 | |
| 321 | // by design multiple pending bucket is allowed for the group |
| 322 | groupHandler.pendingBuckets.compute(nextObjective.id(), (nextId, pendBkts) -> { |
| 323 | if (pendBkts == null) { |
| 324 | pendBkts = Sets.newHashSet(); |
| 325 | } |
| 326 | pendBkts.add(nextObjective); |
| 327 | return pendBkts; |
| 328 | }); |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 329 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 330 | break; |
| 331 | case REMOVE: |
| 332 | if (nextGroup == null) { |
| 333 | log.warn("Cannot remove next {} that does not exist in device {}", |
| 334 | nextObjective.id(), deviceId); |
| 335 | return; |
| 336 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 337 | log.debug("Processing NextObjective id {} in dev {} - remove group", |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 338 | nextObjective.id(), deviceId); |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 339 | groupHandler.removeGroup(nextObjective, nextGroup); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 340 | break; |
| 341 | case REMOVE_FROM_EXISTING: |
| 342 | if (nextGroup == null) { |
| 343 | log.warn("Cannot remove from next {} that does not exist in device {}", |
| 344 | nextObjective.id(), deviceId); |
| 345 | return; |
| 346 | } |
Saurav Das | 1547b3f | 2017-05-05 17:01:08 -0700 | [diff] [blame] | 347 | log.debug("Processing NextObjective id {} in dev {} - remove bucket", |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 348 | nextObjective.id(), deviceId); |
Charles Chan | 425854b | 2016-04-11 15:32:12 -0700 | [diff] [blame] | 349 | groupHandler.removeBucketFromGroup(nextObjective, nextGroup); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 350 | break; |
Saurav Das | ceccf24 | 2017-08-03 18:30:35 -0700 | [diff] [blame] | 351 | case VERIFY: |
| 352 | if (nextGroup == null) { |
| 353 | log.warn("Cannot verify next {} that does not exist in device {}", |
| 354 | nextObjective.id(), deviceId); |
| 355 | return; |
| 356 | } |
| 357 | log.debug("Processing NextObjective id {} in dev {} - verify", |
| 358 | nextObjective.id(), deviceId); |
| 359 | groupHandler.verifyGroup(nextObjective, nextGroup); |
| 360 | break; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 361 | default: |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 362 | log.warn("Unsupported operation {}", nextObjective.op()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 363 | } |
| 364 | } |
| 365 | |
| 366 | ////////////////////////////////////// |
| 367 | // Flow handling |
| 368 | ////////////////////////////////////// |
| 369 | |
| 370 | /** |
Yi Tseng | 0fdc07e | 2017-04-24 19:17:08 -0700 | [diff] [blame] | 371 | * As per OFDPA 2.0 TTP, filtering of VLAN ids and MAC addresses (for routing) |
| 372 | * configured on switch ports happen in different tables. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 373 | * |
| 374 | * @param filt the filtering objective |
| 375 | * @param install indicates whether to add or remove the objective |
| 376 | * @param applicationId the application that sent this objective |
| 377 | */ |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 378 | protected void processFilter(FilteringObjective filt, |
| 379 | boolean install, ApplicationId applicationId) { |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 380 | // This driver only processes filtering criteria defined with switch |
| 381 | // ports as the key |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 382 | PortCriterion portCriterion = null; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 383 | EthCriterion ethCriterion = null; |
| 384 | VlanIdCriterion vidCriterion = null; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 385 | if (!filt.key().equals(Criteria.dummy()) && |
| 386 | filt.key().type() == Criterion.Type.IN_PORT) { |
| 387 | portCriterion = (PortCriterion) filt.key(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 388 | } |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 389 | if (portCriterion == null) { |
| 390 | log.debug("No IN_PORT defined in filtering objective from app: {}", |
| 391 | applicationId); |
| 392 | } else { |
| 393 | log.debug("Received filtering objective for dev/port: {}/{}", deviceId, |
| 394 | portCriterion.port()); |
| 395 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 396 | // convert filtering conditions for switch-intfs into flowrules |
| 397 | FlowRuleOperations.Builder ops = FlowRuleOperations.builder(); |
| 398 | for (Criterion criterion : filt.conditions()) { |
Yi Tseng | 0fdc07e | 2017-04-24 19:17:08 -0700 | [diff] [blame] | 399 | switch (criterion.type()) { |
| 400 | case ETH_DST: |
| 401 | case ETH_DST_MASKED: |
| 402 | ethCriterion = (EthCriterion) criterion; |
| 403 | break; |
| 404 | case VLAN_VID: |
| 405 | vidCriterion = (VlanIdCriterion) criterion; |
| 406 | break; |
| 407 | default: |
| 408 | log.warn("Unsupported filter {}", criterion); |
| 409 | fail(filt, ObjectiveError.UNSUPPORTED); |
| 410 | return; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 411 | } |
| 412 | } |
| 413 | |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 414 | VlanId assignedVlan = null; |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 415 | if (vidCriterion != null) { |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 416 | // Use the VLAN in criterion if metadata is not present and the traffic is tagged |
Charles Chan | 0d6db51 | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 417 | if (!vidCriterion.vlanId().equals(VlanId.NONE)) { |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 418 | assignedVlan = vidCriterion.vlanId(); |
Charles Chan | 0d6db51 | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 419 | } else if (filt.meta() != null) { |
| 420 | assignedVlan = readVlanFromTreatment(filt.meta()); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 421 | } |
Charles Chan | e849c19 | 2016-01-11 18:28:54 -0800 | [diff] [blame] | 422 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 423 | if (assignedVlan == null) { |
| 424 | log.error("Driver fails to extract VLAN information. " |
Frank Wang | 3a98e0a | 2017-08-11 11:09:30 +0800 | [diff] [blame] | 425 | + "Not processing VLAN filters on device {}.", deviceId); |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 426 | log.debug("VLAN ID in criterion={}, metadata={}", |
| 427 | readVlanFromTreatment(filt.meta()), vidCriterion.vlanId()); |
| 428 | fail(filt, ObjectiveError.BADPARAMS); |
| 429 | return; |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
Yi Tseng | 3a77b4f | 2017-02-06 15:02:17 -0800 | [diff] [blame] | 433 | if (ethCriterion == null || ethCriterion.mac().equals(NONE)) { |
Charles Chan | 985b12e | 2016-05-11 19:47:22 -0700 | [diff] [blame] | 434 | // NOTE: it is possible that a filtering objective only has vidCriterion |
Saurav Das | 961beb2 | 2017-03-29 19:09:17 -0700 | [diff] [blame] | 435 | log.warn("filtering objective missing dstMac, cannot program TMAC table"); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 436 | } else { |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 437 | List<List<FlowRule>> allStages = processEthDstFilter(portCriterion, ethCriterion, |
| 438 | vidCriterion, assignedVlan, applicationId); |
| 439 | for (List<FlowRule> flowRules : allStages) { |
| 440 | log.trace("Starting a new flow rule stage"); |
| 441 | ops.newStage(); |
Charles Chan | 0d6db51 | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 442 | |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 443 | for (FlowRule flowRule : flowRules) { |
| 444 | log.trace("{} flow rules in TMAC table: {} for dev: {}", |
| 445 | (install) ? "adding" : "removing", flowRules, deviceId); |
| 446 | |
| 447 | if (install) { |
| 448 | ops = ops.add(flowRule); |
Charles Chan | 0d6db51 | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 449 | } else { |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 450 | // NOTE: Only remove TMAC flow when there is no more enabled port within the |
| 451 | // same VLAN on this device if TMAC doesn't support matching on in_port. |
| 452 | if (matchInPortTmacTable() || (filt.meta() != null && filt.meta().clearedDeferred())) { |
| 453 | ops = ops.remove(flowRule); |
| 454 | } else { |
| 455 | log.debug("Abort TMAC flow removal on {}. Some other ports still share this TMAC flow"); |
| 456 | } |
Charles Chan | 0d6db51 | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 457 | } |
| 458 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 459 | } |
| 460 | } |
| 461 | |
Charles Chan | 985b12e | 2016-05-11 19:47:22 -0700 | [diff] [blame] | 462 | if (vidCriterion == null) { |
| 463 | // NOTE: it is possible that a filtering objective only has ethCriterion |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 464 | log.info("filtering objective missing VLAN, cannot program VLAN Table"); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 465 | } else { |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 466 | List<List<FlowRule>> allStages = processVlanIdFilter( |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 467 | portCriterion, vidCriterion, assignedVlan, applicationId); |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 468 | for (List<FlowRule> flowRules : allStages) { |
| 469 | log.trace("Starting a new flow rule stage"); |
| 470 | ops.newStage(); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 471 | |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 472 | for (FlowRule flowRule : flowRules) { |
| 473 | log.trace("{} flow rules in VLAN table: {} for dev: {}", |
| 474 | (install) ? "adding" : "removing", flowRule, deviceId); |
| 475 | ops = install ? ops.add(flowRule) : ops.remove(flowRule); |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 476 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 477 | } |
| 478 | } |
| 479 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 480 | // apply filtering flow rules |
| 481 | flowRuleService.apply(ops.build(new FlowRuleOperationsContext() { |
| 482 | @Override |
| 483 | public void onSuccess(FlowRuleOperations ops) { |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 484 | log.debug("Applied {} filtering rules in device {}", |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 485 | ops.stages().get(0).size(), deviceId); |
| 486 | pass(filt); |
| 487 | } |
| 488 | |
| 489 | @Override |
| 490 | public void onError(FlowRuleOperations ops) { |
| 491 | log.info("Failed to apply all filtering rules in dev {}", deviceId); |
| 492 | fail(filt, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 493 | } |
| 494 | })); |
| 495 | |
| 496 | } |
| 497 | |
| 498 | /** |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 499 | * Internal implementation of processVlanIdFilter. |
| 500 | * <p> |
| 501 | * The is_present bit in set_vlan_vid action is required to be 0 in OFDPA i12. |
| 502 | * Since it is non-OF spec, we need an extension treatment for that. |
| 503 | * The useVlanExtension must be set to false for OFDPA i12. |
| 504 | * </p> |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 505 | * <p> |
| 506 | * NOTE: Separate VLAN filtering rules and assignment rules |
| 507 | * into different stages in order to guarantee that filtering rules |
| 508 | * always go first, as required by OFDPA. |
| 509 | * </p> |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 510 | * |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 511 | * @param portCriterion port on device for which this filter is programmed |
| 512 | * @param vidCriterion vlan assigned to port, or NONE for untagged |
| 513 | * @param assignedVlan assigned vlan-id for untagged packets |
| 514 | * @param applicationId for application programming this filter |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 515 | * @return stages of flow rules for port-vlan filters |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 516 | */ |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 517 | protected List<List<FlowRule>> processVlanIdFilter(PortCriterion portCriterion, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 518 | VlanIdCriterion vidCriterion, |
| 519 | VlanId assignedVlan, |
| 520 | ApplicationId applicationId) { |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 521 | List<FlowRule> filteringRules = new ArrayList<>(); |
| 522 | List<FlowRule> assignmentRules = new ArrayList<>(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 523 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 524 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 525 | TrafficSelector.Builder preSelector = null; |
| 526 | TrafficTreatment.Builder preTreatment = null; |
| 527 | |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 528 | treatment.transition(TMAC_TABLE); |
| 529 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 530 | if (vidCriterion.vlanId() == VlanId.NONE) { |
| 531 | // untagged packets are assigned vlans |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 532 | preSelector = DefaultTrafficSelector.builder(); |
| 533 | if (requireVlanExtensions()) { |
| 534 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(VlanId.NONE); |
| 535 | selector.extension(ofdpaMatchVlanVid, deviceId); |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 536 | OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan); |
| 537 | treatment.extension(ofdpaSetVlanVid, deviceId); |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 538 | |
| 539 | OfdpaMatchVlanVid preOfdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan); |
| 540 | preSelector.extension(preOfdpaMatchVlanVid, deviceId); |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 541 | } else { |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 542 | selector.matchVlanId(VlanId.NONE); |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 543 | treatment.setVlanId(assignedVlan); |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 544 | |
| 545 | preSelector.matchVlanId(assignedVlan); |
| 546 | } |
| 547 | preTreatment = DefaultTrafficTreatment.builder().transition(TMAC_TABLE); |
| 548 | } else { |
| 549 | if (requireVlanExtensions()) { |
| 550 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId()); |
| 551 | selector.extension(ofdpaMatchVlanVid, deviceId); |
| 552 | } else { |
| 553 | selector.matchVlanId(vidCriterion.vlanId()); |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 554 | } |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 555 | |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 556 | if (!assignedVlan.equals(vidCriterion.vlanId())) { |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 557 | if (requireVlanExtensions()) { |
Charles Chan | c03782d | 2017-01-31 13:57:55 -0800 | [diff] [blame] | 558 | OfdpaSetVlanVid ofdpaSetVlanVid = new OfdpaSetVlanVid(assignedVlan); |
| 559 | treatment.extension(ofdpaSetVlanVid, deviceId); |
| 560 | } else { |
| 561 | treatment.setVlanId(assignedVlan); |
| 562 | } |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 563 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 564 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 565 | |
| 566 | // ofdpa cannot match on ALL portnumber, so we need to use separate |
| 567 | // rules for each port. |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 568 | List<PortNumber> portnums = new ArrayList<>(); |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 569 | if (portCriterion != null) { |
| 570 | if (PortNumber.ALL.equals(portCriterion.port())) { |
| 571 | for (Port port : deviceService.getPorts(deviceId)) { |
| 572 | if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) { |
| 573 | portnums.add(port.number()); |
| 574 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 575 | } |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 576 | } else { |
| 577 | portnums.add(portCriterion.port()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 578 | } |
| 579 | } else { |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 580 | log.warn("Filtering Objective missing Port Criterion . " + |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 581 | "VLAN Table cannot be programmed for {}", deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 582 | } |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 583 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 584 | for (PortNumber pnum : portnums) { |
Saurav Das | 4f98008 | 2015-11-05 13:39:15 -0800 | [diff] [blame] | 585 | // create rest of flowrule |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 586 | selector.matchInPort(pnum); |
| 587 | FlowRule rule = DefaultFlowRule.builder() |
| 588 | .forDevice(deviceId) |
| 589 | .withSelector(selector.build()) |
| 590 | .withTreatment(treatment.build()) |
| 591 | .withPriority(DEFAULT_PRIORITY) |
| 592 | .fromApp(applicationId) |
| 593 | .makePermanent() |
| 594 | .forTable(VLAN_TABLE).build(); |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 595 | assignmentRules.add(rule); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 596 | |
| 597 | if (preSelector != null) { |
| 598 | preSelector.matchInPort(pnum); |
| 599 | FlowRule preRule = DefaultFlowRule.builder() |
| 600 | .forDevice(deviceId) |
| 601 | .withSelector(preSelector.build()) |
| 602 | .withTreatment(preTreatment.build()) |
| 603 | .withPriority(DEFAULT_PRIORITY) |
| 604 | .fromApp(applicationId) |
| 605 | .makePermanent() |
| 606 | .forTable(VLAN_TABLE).build(); |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 607 | filteringRules.add(preRule); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 608 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 609 | } |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 610 | return ImmutableList.of(assignmentRules, filteringRules); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /** |
| 614 | * Allows routed packets with correct destination MAC to be directed |
| 615 | * to unicast-IP routing table or MPLS forwarding table. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 616 | * |
| 617 | * @param portCriterion port on device for which this filter is programmed |
| 618 | * @param ethCriterion dstMac of device for which is filter is programmed |
| 619 | * @param vidCriterion vlan assigned to port, or NONE for untagged |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 620 | * @param assignedVlan assigned vlan-id for untagged packets |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 621 | * @param applicationId for application programming this filter |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 622 | * @return stages of flow rules for port-vlan filters |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 623 | |
| 624 | */ |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 625 | protected List<List<FlowRule>> processEthDstFilter(PortCriterion portCriterion, |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 626 | EthCriterion ethCriterion, |
| 627 | VlanIdCriterion vidCriterion, |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 628 | VlanId assignedVlan, |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 629 | ApplicationId applicationId) { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 630 | // Consider PortNumber.ANY as wildcard. Match ETH_DST only |
Sivachidambaram Subramanian | c598d06 | 2017-05-16 22:42:06 +0530 | [diff] [blame] | 631 | if (portCriterion != null && PortNumber.ANY.equals(portCriterion.port())) { |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 632 | return processEthDstOnlyFilter(ethCriterion, applicationId); |
| 633 | } |
| 634 | |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 635 | // Multicast MAC |
| 636 | if (ethCriterion.mask() != null) { |
Charles Chan | 206cac0 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 637 | return processMcastEthDstFilter(ethCriterion, assignedVlan, applicationId); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 638 | } |
| 639 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 640 | //handling untagged packets via assigned VLAN |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 641 | if (vidCriterion != null && vidCriterion.vlanId() == VlanId.NONE) { |
Saurav Das | 0e99e2b | 2015-10-28 12:39:42 -0700 | [diff] [blame] | 642 | vidCriterion = (VlanIdCriterion) Criteria.matchVlanId(assignedVlan); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 643 | } |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 644 | List<FlowRule> rules = new ArrayList<>(); |
| 645 | OfdpaMatchVlanVid ofdpaMatchVlanVid = null; |
| 646 | if (vidCriterion != null && requireVlanExtensions()) { |
| 647 | ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vidCriterion.vlanId()); |
| 648 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 649 | // ofdpa cannot match on ALL portnumber, so we need to use separate |
| 650 | // rules for each port. |
Yuta HIGUCHI | 2dce08a | 2017-04-20 21:57:48 -0700 | [diff] [blame] | 651 | List<PortNumber> portnums = new ArrayList<>(); |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 652 | if (portCriterion != null) { |
| 653 | if (PortNumber.ALL.equals(portCriterion.port())) { |
| 654 | for (Port port : deviceService.getPorts(deviceId)) { |
| 655 | if (port.number().toLong() > 0 && port.number().toLong() < OFPP_MAX) { |
| 656 | portnums.add(port.number()); |
| 657 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 658 | } |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 659 | } else { |
| 660 | portnums.add(portCriterion.port()); |
| 661 | } |
| 662 | for (PortNumber pnum : portnums) { |
| 663 | rules.add(buildTmacRuleForIpv4(ethCriterion, |
| 664 | vidCriterion, |
| 665 | ofdpaMatchVlanVid, |
| 666 | applicationId, |
| 667 | pnum)); |
| 668 | rules.add(buildTmacRuleForMpls(ethCriterion, |
| 669 | vidCriterion, |
| 670 | ofdpaMatchVlanVid, |
| 671 | applicationId, |
| 672 | pnum)); |
| 673 | rules.add(buildTmacRuleForIpv6(ethCriterion, |
| 674 | vidCriterion, |
| 675 | ofdpaMatchVlanVid, |
| 676 | applicationId, |
| 677 | pnum)); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 678 | } |
| 679 | } else { |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 680 | rules.add(buildTmacRuleForIpv4(ethCriterion, |
| 681 | vidCriterion, |
| 682 | ofdpaMatchVlanVid, |
| 683 | applicationId, |
| 684 | null)); |
| 685 | rules.add(buildTmacRuleForMpls(ethCriterion, |
| 686 | vidCriterion, |
| 687 | ofdpaMatchVlanVid, |
| 688 | applicationId, |
| 689 | null)); |
| 690 | rules.add(buildTmacRuleForIpv6(ethCriterion, |
| 691 | vidCriterion, |
| 692 | ofdpaMatchVlanVid, |
| 693 | applicationId, |
| 694 | null)); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 695 | } |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 696 | return ImmutableList.of(rules); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 697 | } |
| 698 | |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 699 | /** |
| 700 | * Builds TMAC rules for IPv4 packets. |
| 701 | * |
Charles Chan | 0d6db51 | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 702 | * @param ethCriterion dst mac matching |
| 703 | * @param vidCriterion vlan id assigned to the port |
| 704 | * @param ofdpaMatchVlanVid OFDPA vlan id matching |
| 705 | * @param applicationId application id |
| 706 | * @param pnum port number |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 707 | * @return TMAC rule for IPV4 packets |
| 708 | */ |
| 709 | private FlowRule buildTmacRuleForIpv4(EthCriterion ethCriterion, |
| 710 | VlanIdCriterion vidCriterion, |
| 711 | OfdpaMatchVlanVid ofdpaMatchVlanVid, |
| 712 | ApplicationId applicationId, |
| 713 | PortNumber pnum) { |
| 714 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 715 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 716 | if (pnum != null) { |
| 717 | if (matchInPortTmacTable()) { |
| 718 | selector.matchInPort(pnum); |
| 719 | } else { |
Saurav Das | 9aa28bd | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 720 | log.debug("Pipeline does not support IN_PORT matching in TMAC table, " + |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 721 | "ignoring the IN_PORT criteria"); |
| 722 | } |
| 723 | } |
| 724 | if (vidCriterion != null) { |
| 725 | if (requireVlanExtensions()) { |
| 726 | selector.extension(ofdpaMatchVlanVid, deviceId); |
| 727 | } else { |
| 728 | selector.matchVlanId(vidCriterion.vlanId()); |
| 729 | } |
| 730 | } |
| 731 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 732 | selector.matchEthDst(ethCriterion.mac()); |
| 733 | treatment.transition(UNICAST_ROUTING_TABLE); |
| 734 | return DefaultFlowRule.builder() |
| 735 | .forDevice(deviceId) |
| 736 | .withSelector(selector.build()) |
| 737 | .withTreatment(treatment.build()) |
| 738 | .withPriority(DEFAULT_PRIORITY) |
| 739 | .fromApp(applicationId) |
| 740 | .makePermanent() |
| 741 | .forTable(TMAC_TABLE).build(); |
| 742 | } |
| 743 | |
| 744 | /** |
| 745 | * Builds TMAC rule for MPLS packets. |
| 746 | * |
Charles Chan | 0d6db51 | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 747 | * @param ethCriterion dst mac matching |
| 748 | * @param vidCriterion vlan id assigned to the port |
| 749 | * @param ofdpaMatchVlanVid OFDPA vlan id matching |
| 750 | * @param applicationId application id |
| 751 | * @param pnum port number |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 752 | * @return TMAC rule for MPLS packets |
| 753 | */ |
| 754 | private FlowRule buildTmacRuleForMpls(EthCriterion ethCriterion, |
| 755 | VlanIdCriterion vidCriterion, |
| 756 | OfdpaMatchVlanVid ofdpaMatchVlanVid, |
| 757 | ApplicationId applicationId, |
| 758 | PortNumber pnum) { |
| 759 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 760 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 761 | if (pnum != null) { |
| 762 | if (matchInPortTmacTable()) { |
| 763 | selector.matchInPort(pnum); |
| 764 | } else { |
Saurav Das | 9aa28bd | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 765 | log.debug("Pipeline does not support IN_PORT matching in TMAC table, " + |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 766 | "ignoring the IN_PORT criteria"); |
| 767 | } |
| 768 | } |
| 769 | if (vidCriterion != null) { |
| 770 | if (requireVlanExtensions()) { |
| 771 | selector.extension(ofdpaMatchVlanVid, deviceId); |
| 772 | } else { |
| 773 | selector.matchVlanId(vidCriterion.vlanId()); |
| 774 | } |
| 775 | } |
| 776 | selector.matchEthType(Ethernet.MPLS_UNICAST); |
| 777 | selector.matchEthDst(ethCriterion.mac()); |
| 778 | treatment.transition(MPLS_TABLE_0); |
| 779 | return DefaultFlowRule.builder() |
| 780 | .forDevice(deviceId) |
| 781 | .withSelector(selector.build()) |
| 782 | .withTreatment(treatment.build()) |
| 783 | .withPriority(DEFAULT_PRIORITY) |
| 784 | .fromApp(applicationId) |
| 785 | .makePermanent() |
| 786 | .forTable(TMAC_TABLE).build(); |
| 787 | } |
| 788 | |
| 789 | /** |
| 790 | * Builds TMAC rules for IPv6 packets. |
| 791 | * |
Charles Chan | 0d6db51 | 2017-12-19 19:55:57 -0800 | [diff] [blame] | 792 | * @param ethCriterion dst mac matching |
| 793 | * @param vidCriterion vlan id assigned to the port |
| 794 | * @param ofdpaMatchVlanVid OFDPA vlan id matching |
| 795 | * @param applicationId application id |
| 796 | * @param pnum port number |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 797 | * @return TMAC rule for IPV6 packets |
| 798 | */ |
| 799 | private FlowRule buildTmacRuleForIpv6(EthCriterion ethCriterion, |
| 800 | VlanIdCriterion vidCriterion, |
| 801 | OfdpaMatchVlanVid ofdpaMatchVlanVid, |
| 802 | ApplicationId applicationId, |
| 803 | PortNumber pnum) { |
| 804 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 805 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 806 | if (pnum != null) { |
| 807 | if (matchInPortTmacTable()) { |
| 808 | selector.matchInPort(pnum); |
| 809 | } else { |
Saurav Das | 9aa28bd | 2017-12-05 15:00:23 -0800 | [diff] [blame] | 810 | log.debug("Pipeline does not support IN_PORT matching in TMAC table, " + |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 811 | "ignoring the IN_PORT criteria"); |
| 812 | } |
| 813 | } |
| 814 | if (vidCriterion != null) { |
| 815 | if (requireVlanExtensions()) { |
| 816 | selector.extension(ofdpaMatchVlanVid, deviceId); |
| 817 | } else { |
| 818 | selector.matchVlanId(vidCriterion.vlanId()); |
| 819 | } |
| 820 | } |
| 821 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 822 | selector.matchEthDst(ethCriterion.mac()); |
| 823 | treatment.transition(UNICAST_ROUTING_TABLE); |
| 824 | return DefaultFlowRule.builder() |
| 825 | .forDevice(deviceId) |
| 826 | .withSelector(selector.build()) |
| 827 | .withTreatment(treatment.build()) |
| 828 | .withPriority(DEFAULT_PRIORITY) |
| 829 | .fromApp(applicationId) |
| 830 | .makePermanent() |
| 831 | .forTable(TMAC_TABLE).build(); |
| 832 | } |
| 833 | |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 834 | protected List<List<FlowRule>> processEthDstOnlyFilter(EthCriterion ethCriterion, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 835 | ApplicationId applicationId) { |
Pier Luigi | 0e35863 | 2017-01-31 09:35:05 -0800 | [diff] [blame] | 836 | ImmutableList.Builder<FlowRule> builder = ImmutableList.builder(); |
| 837 | |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 838 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 839 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
| 840 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 841 | selector.matchEthDst(ethCriterion.mac()); |
| 842 | treatment.transition(UNICAST_ROUTING_TABLE); |
| 843 | FlowRule rule = DefaultFlowRule.builder() |
| 844 | .forDevice(deviceId) |
| 845 | .withSelector(selector.build()) |
| 846 | .withTreatment(treatment.build()) |
| 847 | .withPriority(DEFAULT_PRIORITY) |
| 848 | .fromApp(applicationId) |
| 849 | .makePermanent() |
| 850 | .forTable(TMAC_TABLE).build(); |
Pier Luigi | 0e35863 | 2017-01-31 09:35:05 -0800 | [diff] [blame] | 851 | builder.add(rule); |
| 852 | |
| 853 | selector = DefaultTrafficSelector.builder(); |
| 854 | treatment = DefaultTrafficTreatment.builder(); |
| 855 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 856 | selector.matchEthDst(ethCriterion.mac()); |
| 857 | treatment.transition(UNICAST_ROUTING_TABLE); |
| 858 | rule = DefaultFlowRule.builder() |
| 859 | .forDevice(deviceId) |
| 860 | .withSelector(selector.build()) |
| 861 | .withTreatment(treatment.build()) |
| 862 | .withPriority(DEFAULT_PRIORITY) |
| 863 | .fromApp(applicationId) |
| 864 | .makePermanent() |
| 865 | .forTable(TMAC_TABLE).build(); |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 866 | return ImmutableList.of(builder.add(rule).build()); |
Charles Chan | 5270ed0 | 2016-01-30 23:22:37 -0800 | [diff] [blame] | 867 | } |
| 868 | |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 869 | List<List<FlowRule>> processMcastEthDstFilter(EthCriterion ethCriterion, |
Charles Chan | 206cac0 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 870 | VlanId assignedVlan, |
Yi Tseng | ef19de1 | 2017-04-24 11:33:05 -0700 | [diff] [blame] | 871 | ApplicationId applicationId) { |
Julia Ferguson | d8f145e | 2017-08-10 18:15:24 +0000 | [diff] [blame] | 872 | ImmutableList.Builder<FlowRule> builder = ImmutableList.builder(); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 873 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 874 | TrafficTreatment.Builder treatment = DefaultTrafficTreatment.builder(); |
Charles Chan | 206cac0 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 875 | FlowRule rule; |
Julia Ferguson | d8f145e | 2017-08-10 18:15:24 +0000 | [diff] [blame] | 876 | |
Charles Chan | 206cac0 | 2017-10-20 19:09:16 -0700 | [diff] [blame] | 877 | if (IPV4_MULTICAST.equals(ethCriterion.mac())) { |
| 878 | selector.matchEthType(Ethernet.TYPE_IPV4); |
| 879 | selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask()); |
| 880 | selector.matchVlanId(assignedVlan); |
| 881 | treatment.transition(MULTICAST_ROUTING_TABLE); |
| 882 | rule = DefaultFlowRule.builder() |
| 883 | .forDevice(deviceId) |
| 884 | .withSelector(selector.build()) |
| 885 | .withTreatment(treatment.build()) |
| 886 | .withPriority(DEFAULT_PRIORITY) |
| 887 | .fromApp(applicationId) |
| 888 | .makePermanent() |
| 889 | .forTable(TMAC_TABLE).build(); |
| 890 | builder.add(rule); |
| 891 | } |
| 892 | |
| 893 | if (IPV6_MULTICAST.equals(ethCriterion.mac())) { |
| 894 | selector = DefaultTrafficSelector.builder(); |
| 895 | treatment = DefaultTrafficTreatment.builder(); |
| 896 | selector.matchEthType(Ethernet.TYPE_IPV6); |
| 897 | selector.matchEthDstMasked(ethCriterion.mac(), ethCriterion.mask()); |
| 898 | selector.matchVlanId(assignedVlan); |
| 899 | treatment.transition(MULTICAST_ROUTING_TABLE); |
| 900 | rule = DefaultFlowRule.builder() |
| 901 | .forDevice(deviceId) |
| 902 | .withSelector(selector.build()) |
| 903 | .withTreatment(treatment.build()) |
| 904 | .withPriority(DEFAULT_PRIORITY) |
| 905 | .fromApp(applicationId) |
| 906 | .makePermanent() |
| 907 | .forTable(TMAC_TABLE).build(); |
| 908 | builder.add(rule); |
| 909 | } |
Charles Chan | 3aed59a | 2018-03-02 16:43:28 -0800 | [diff] [blame^] | 910 | return ImmutableList.of(builder.build()); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 911 | } |
| 912 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 913 | private Collection<FlowRule> processForward(ForwardingObjective fwd) { |
| 914 | switch (fwd.flag()) { |
| 915 | case SPECIFIC: |
| 916 | return processSpecific(fwd); |
| 917 | case VERSATILE: |
| 918 | return processVersatile(fwd); |
| 919 | default: |
| 920 | fail(fwd, ObjectiveError.UNKNOWN); |
| 921 | log.warn("Unknown forwarding flag {}", fwd.flag()); |
| 922 | } |
| 923 | return Collections.emptySet(); |
| 924 | } |
| 925 | |
| 926 | /** |
| 927 | * In the OF-DPA 2.0 pipeline, versatile forwarding objectives go to the |
| 928 | * ACL table. |
| 929 | * @param fwd the forwarding objective of type 'versatile' |
| 930 | * @return a collection of flow rules to be sent to the switch. An empty |
| 931 | * collection may be returned if there is a problem in processing |
| 932 | * the flow rule |
| 933 | */ |
Saurav Das | 5202596 | 2016-01-28 22:30:01 -0800 | [diff] [blame] | 934 | protected Collection<FlowRule> processVersatile(ForwardingObjective fwd) { |
Saurav Das | 018605f | 2017-02-18 14:05:44 -0800 | [diff] [blame] | 935 | log.debug("Processing versatile forwarding objective:{} in dev:{}", |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 936 | fwd.id(), deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 937 | |
| 938 | EthTypeCriterion ethType = |
Saurav Das | 77b5e90 | 2016-01-27 17:01:59 -0800 | [diff] [blame] | 939 | (EthTypeCriterion) fwd.selector().getCriterion(Criterion.Type.ETH_TYPE); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 940 | if (ethType == null) { |
Saurav Das | d2fded0 | 2016-12-02 15:43:47 -0800 | [diff] [blame] | 941 | log.error("Versatile forwarding objective:{} must include ethType", |
| 942 | fwd.id()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 943 | fail(fwd, ObjectiveError.BADPARAMS); |
| 944 | return Collections.emptySet(); |
| 945 | } |
| 946 | if (fwd.nextId() == null && fwd.treatment() == null) { |
| 947 | log.error("Forwarding objective {} from {} must contain " |
| 948 | + "nextId or Treatment", fwd.selector(), fwd.appId()); |
Zsolt Haraszti | 9faab75 | 2016-02-17 15:55:20 -0800 | [diff] [blame] | 949 | fail(fwd, ObjectiveError.BADPARAMS); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 950 | return Collections.emptySet(); |
| 951 | } |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 952 | |
Saurav Das | 77b5e90 | 2016-01-27 17:01:59 -0800 | [diff] [blame] | 953 | TrafficSelector.Builder sbuilder = DefaultTrafficSelector.builder(); |
| 954 | fwd.selector().criteria().forEach(criterion -> { |
| 955 | if (criterion instanceof VlanIdCriterion) { |
| 956 | VlanId vlanId = ((VlanIdCriterion) criterion).vlanId(); |
| 957 | // ensure that match does not include vlan = NONE as OF-DPA does not |
| 958 | // match untagged packets this way in the ACL table. |
| 959 | if (vlanId.equals(VlanId.NONE)) { |
| 960 | return; |
| 961 | } |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 962 | if (requireVlanExtensions()) { |
| 963 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanId); |
| 964 | sbuilder.extension(ofdpaMatchVlanVid, deviceId); |
| 965 | } else { |
| 966 | sbuilder.matchVlanId(vlanId); |
| 967 | } |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 968 | } else if (criterion instanceof Icmpv6TypeCriterion || |
| 969 | criterion instanceof Icmpv6CodeCriterion) { |
| 970 | /* |
| 971 | * We silenty discard these criterions, our current |
| 972 | * OFDPA platform does not support these matches on |
| 973 | * the ACL table. |
| 974 | */ |
| 975 | log.warn("ICMPv6 Type and ICMPv6 Code are not supported"); |
Charles Chan | 30122a7 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 976 | } else if (criterion instanceof TcpPortCriterion || criterion instanceof UdpPortCriterion) { |
| 977 | // FIXME: QMX switches do not support L4 dst port matching in ACL table. |
| 978 | // Currently L4 dst port matching is only used by DHCP relay feature |
| 979 | // and therefore is safe to be replaced with L4 src port matching. |
| 980 | // We need to revisit this if L4 dst port is used for other purpose in the future. |
| 981 | if (!supportIpv6L4Dst() && isIpv6(fwd.selector())) { |
Charles Chan | 30122a7 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 982 | switch (criterion.type()) { |
| 983 | case UDP_DST: |
Charles Chan | 30122a7 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 984 | case UDP_DST_MASKED: |
Charles Chan | 30122a7 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 985 | case TCP_DST: |
Charles Chan | 30122a7 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 986 | case TCP_DST_MASKED: |
Charles Chan | 30122a7 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 987 | break; |
| 988 | default: |
| 989 | sbuilder.add(criterion); |
| 990 | } |
| 991 | } else { |
| 992 | sbuilder.add(criterion); |
| 993 | } |
Saurav Das | 77b5e90 | 2016-01-27 17:01:59 -0800 | [diff] [blame] | 994 | } else { |
| 995 | sbuilder.add(criterion); |
| 996 | } |
| 997 | }); |
| 998 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 999 | // XXX driver does not currently do type checking as per Tables 65-67 in |
| 1000 | // OFDPA 2.0 spec. The only allowed treatment is a punt to the controller. |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 1001 | TrafficTreatment.Builder ttBuilder = DefaultTrafficTreatment.builder(); |
| 1002 | if (fwd.treatment() != null) { |
| 1003 | for (Instruction ins : fwd.treatment().allInstructions()) { |
| 1004 | if (ins instanceof OutputInstruction) { |
| 1005 | OutputInstruction o = (OutputInstruction) ins; |
Charles Chan | c6e64bb | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1006 | if (PortNumber.CONTROLLER.equals(o.port())) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 1007 | ttBuilder.add(o); |
| 1008 | } else { |
| 1009 | log.warn("Only allowed treatments in versatile forwarding " |
| 1010 | + "objectives are punts to the controller"); |
| 1011 | } |
| 1012 | } else { |
| 1013 | log.warn("Cannot process instruction in versatile fwd {}", ins); |
| 1014 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1015 | } |
Charles Chan | 2df0e8a | 2017-01-09 11:45:08 -0800 | [diff] [blame] | 1016 | if (fwd.treatment().clearedDeferred()) { |
| 1017 | ttBuilder.wipeDeferred(); |
| 1018 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1019 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1020 | if (fwd.nextId() != null) { |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 1021 | // overide case |
| 1022 | NextGroup next = getGroupForNextObjective(fwd.nextId()); |
Sivachidambaram Subramanian | a0efdcc | 2017-06-22 05:26:46 +0530 | [diff] [blame] | 1023 | if (next == null) { |
| 1024 | fail(fwd, ObjectiveError.BADPARAMS); |
| 1025 | return Collections.emptySet(); |
| 1026 | } |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 1027 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 1028 | // we only need the top level group's key to point the flow to it |
| 1029 | Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst()); |
| 1030 | if (group == null) { |
| 1031 | log.warn("Group with key:{} for next-id:{} not found in dev:{}", |
| 1032 | gkeys.get(0).peekFirst(), fwd.nextId(), deviceId); |
| 1033 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 1034 | return Collections.emptySet(); |
| 1035 | } |
| 1036 | ttBuilder.deferred().group(group.id()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1037 | } |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 1038 | |
| 1039 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 1040 | .fromApp(fwd.appId()) |
| 1041 | .withPriority(fwd.priority()) |
| 1042 | .forDevice(deviceId) |
Saurav Das | 77b5e90 | 2016-01-27 17:01:59 -0800 | [diff] [blame] | 1043 | .withSelector(sbuilder.build()) |
Saurav Das | 49cb5a1 | 2016-01-16 22:54:07 -0800 | [diff] [blame] | 1044 | .withTreatment(ttBuilder.build()) |
| 1045 | .makePermanent() |
| 1046 | .forTable(ACL_TABLE); |
| 1047 | return Collections.singletonList(ruleBuilder.build()); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1048 | } |
| 1049 | |
| 1050 | /** |
| 1051 | * In the OF-DPA 2.0 pipeline, specific forwarding refers to the IP table |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1052 | * (unicast or multicast) or the L2 table (mac + vlan) or the MPLS table. |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1053 | * |
| 1054 | * @param fwd the forwarding objective of type 'specific' |
| 1055 | * @return a collection of flow rules. Typically there will be only one |
| 1056 | * for this type of forwarding objective. An empty set may be |
| 1057 | * returned if there is an issue in processing the objective. |
| 1058 | */ |
Charles Chan | c6e64bb | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1059 | private Collection<FlowRule> processSpecific(ForwardingObjective fwd) { |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 1060 | log.debug("Processing specific fwd objective:{} in dev:{} with next:{}", |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1061 | fwd.id(), deviceId, fwd.nextId()); |
| 1062 | boolean isEthTypeObj = isSupportedEthTypeObjective(fwd); |
| 1063 | boolean isEthDstObj = isSupportedEthDstObjective(fwd); |
| 1064 | |
| 1065 | if (isEthTypeObj) { |
| 1066 | return processEthTypeSpecific(fwd); |
| 1067 | } else if (isEthDstObj) { |
| 1068 | return processEthDstSpecific(fwd); |
| 1069 | } else { |
| 1070 | log.warn("processSpecific: Unsupported forwarding objective " |
| 1071 | + "criteria fwd:{} in dev:{}", fwd.nextId(), deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1072 | fail(fwd, ObjectiveError.UNSUPPORTED); |
| 1073 | return Collections.emptySet(); |
| 1074 | } |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1075 | } |
| 1076 | |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1077 | /** |
| 1078 | * Handles forwarding rules to the IP and MPLS tables. |
| 1079 | * |
| 1080 | * @param fwd the forwarding objective |
| 1081 | * @return A collection of flow rules, or an empty set |
| 1082 | */ |
| 1083 | protected Collection<FlowRule> processEthTypeSpecific(ForwardingObjective fwd) { |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 1084 | return processEthTypeSpecificInternal(fwd, false, ACL_TABLE); |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | /** |
| 1088 | * Internal implementation of processEthTypeSpecific. |
| 1089 | * <p> |
| 1090 | * Wildcarded IPv4_DST is not supported in OFDPA i12. Therefore, we break |
| 1091 | * the rule into 0.0.0.0/1 and 128.0.0.0/1. |
| 1092 | * The allowDefaultRoute must be set to false for OFDPA i12. |
| 1093 | * </p> |
| 1094 | * |
| 1095 | * @param fwd the forwarding objective |
| 1096 | * @param allowDefaultRoute allow wildcarded IPv4_DST or not |
Ray Milkey | 0bb1e10 | 2016-11-10 14:51:27 -0800 | [diff] [blame] | 1097 | * @param mplsNextTable next MPLS table |
Charles Chan | f9e9865 | 2016-09-07 16:54:23 -0700 | [diff] [blame] | 1098 | * @return A collection of flow rules, or an empty set |
| 1099 | */ |
| 1100 | protected Collection<FlowRule> processEthTypeSpecificInternal(ForwardingObjective fwd, |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 1101 | boolean allowDefaultRoute, |
| 1102 | int mplsNextTable) { |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1103 | TrafficSelector selector = fwd.selector(); |
| 1104 | EthTypeCriterion ethType = |
| 1105 | (EthTypeCriterion) selector.getCriterion(Criterion.Type.ETH_TYPE); |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1106 | boolean popMpls = false; |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1107 | boolean emptyGroup = false; |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1108 | int forTableId; |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1109 | TrafficSelector.Builder filteredSelector = DefaultTrafficSelector.builder(); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 1110 | TrafficTreatment.Builder tb = DefaultTrafficTreatment.builder(); |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1111 | TrafficSelector.Builder complementarySelector = DefaultTrafficSelector.builder(); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 1112 | |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1113 | if (ethType.ethType().toShort() == Ethernet.TYPE_IPV4) { |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1114 | if (buildIpv4Selector(filteredSelector, complementarySelector, fwd, allowDefaultRoute) < 0) { |
| 1115 | return Collections.emptyList(); |
| 1116 | } |
| 1117 | // We need to set properly the next table |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1118 | IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip(); |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1119 | if (ipv4Dst.isMulticast()) { |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1120 | forTableId = MULTICAST_ROUTING_TABLE; |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1121 | } else { |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1122 | forTableId = UNICAST_ROUTING_TABLE; |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1123 | } |
Charles Chan | c6e64bb | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1124 | |
| 1125 | /* |
| 1126 | // XXX decrementing IP ttl is done automatically for routing, this |
| 1127 | // action is ignored or rejected in ofdpa as it is not fully implemented |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 1128 | if (fwd.treatment() != null) { |
| 1129 | for (Instruction instr : fwd.treatment().allInstructions()) { |
| 1130 | if (instr instanceof L3ModificationInstruction && |
| 1131 | ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) { |
Charles Chan | c6e64bb | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1132 | tb.deferred().add(instr); |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 1133 | } |
| 1134 | } |
| 1135 | } |
Charles Chan | c6e64bb | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1136 | */ |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1137 | } else if (ethType.ethType().toShort() == Ethernet.TYPE_IPV6) { |
| 1138 | if (buildIpv6Selector(filteredSelector, fwd) < 0) { |
| 1139 | return Collections.emptyList(); |
| 1140 | } |
Kalhee Kim | 7c6f4f8 | 2017-10-04 14:59:11 +0000 | [diff] [blame] | 1141 | //We need to set the proper next table |
| 1142 | IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip(); |
| 1143 | if (ipv6Dst.isMulticast()) { |
| 1144 | forTableId = MULTICAST_ROUTING_TABLE; |
| 1145 | } else { |
| 1146 | forTableId = UNICAST_ROUTING_TABLE; |
| 1147 | } |
| 1148 | |
Charles Chan | c6e64bb | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1149 | // XXX decrementing IP ttl is done automatically for routing, this |
| 1150 | // action is ignored or rejected in ofdpa as it is not fully implemented |
| 1151 | /* |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1152 | if (fwd.treatment() != null) { |
| 1153 | for (Instruction instr : fwd.treatment().allInstructions()) { |
| 1154 | if (instr instanceof L3ModificationInstruction && |
| 1155 | ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) { |
Charles Chan | c6e64bb | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1156 | tb.deferred().add(instr); |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1157 | } |
| 1158 | } |
| 1159 | } |
Charles Chan | c6e64bb | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1160 | */ |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1161 | } else { |
| 1162 | filteredSelector |
| 1163 | .matchEthType(Ethernet.MPLS_UNICAST) |
| 1164 | .matchMplsLabel(((MplsCriterion) |
| 1165 | selector.getCriterion(Criterion.Type.MPLS_LABEL)).label()); |
| 1166 | MplsBosCriterion bos = (MplsBosCriterion) selector |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 1167 | .getCriterion(MPLS_BOS); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1168 | if (bos != null) { |
| 1169 | filteredSelector.matchMplsBos(bos.mplsBos()); |
| 1170 | } |
| 1171 | forTableId = MPLS_TABLE_1; |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1172 | log.debug("processing MPLS specific forwarding objective {} -> next:{}" |
| 1173 | + " in dev {}", fwd.id(), fwd.nextId(), deviceId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1174 | |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 1175 | if (fwd.treatment() != null) { |
| 1176 | for (Instruction instr : fwd.treatment().allInstructions()) { |
| 1177 | if (instr instanceof L2ModificationInstruction && |
| 1178 | ((L2ModificationInstruction) instr).subtype() == L2SubType.MPLS_POP) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1179 | popMpls = true; |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1180 | // OF-DPA does not pop in MPLS table in some cases. For the L3 VPN, it requires |
Saurav Das | 9c70534 | 2017-01-06 11:36:01 -0800 | [diff] [blame] | 1181 | // setting the MPLS_TYPE so pop can happen down the pipeline |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1182 | if (mplsNextTable == MPLS_TYPE_TABLE && isNotMplsBos(selector)) { |
| 1183 | tb.immediate().popMpls(); |
| 1184 | } |
Charles Chan | 14967c2 | 2015-12-07 11:11:50 -0800 | [diff] [blame] | 1185 | } |
| 1186 | if (instr instanceof L3ModificationInstruction && |
| 1187 | ((L3ModificationInstruction) instr).subtype() == L3SubType.DEC_TTL) { |
| 1188 | // FIXME Should modify the app to send the correct DEC_MPLS_TTL instruction |
| 1189 | tb.immediate().decMplsTtl(); |
| 1190 | } |
| 1191 | if (instr instanceof L3ModificationInstruction && |
| 1192 | ((L3ModificationInstruction) instr).subtype() == L3SubType.TTL_IN) { |
| 1193 | tb.immediate().add(instr); |
| 1194 | } |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1195 | } |
| 1196 | } |
| 1197 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1198 | |
| 1199 | if (fwd.nextId() != null) { |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1200 | if (forTableId == MPLS_TABLE_1 && !popMpls) { |
| 1201 | log.warn("SR CONTINUE case cannot be handled as MPLS ECMP " |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 1202 | + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}" |
| 1203 | + "in this device {}", fwd.id(), fwd.nextId(), deviceId); |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1204 | // XXX We could convert to forwarding to a single-port, via a MPLS interface, |
| 1205 | // or a MPLS SWAP (with-same) but that would have to be handled in the next-objective. |
| 1206 | // Also the pop-mpls logic used here won't work in non-BoS case. |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1207 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1208 | return Collections.emptySet(); |
| 1209 | } |
| 1210 | |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1211 | NextGroup next = getGroupForNextObjective(fwd.nextId()); |
| 1212 | if (next != null) { |
| 1213 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 1214 | // we only need the top level group's key to point the flow to it |
| 1215 | Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst()); |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 1216 | if (isNotMplsBos(selector) && group.type().equals(HASHED)) { |
| 1217 | log.warn("SR CONTINUE case cannot be handled as MPLS ECMP " |
| 1218 | + "is not implemented in OF-DPA yet. Aborting this flow {} -> next:{}" |
| 1219 | + "in this device {}", fwd.id(), fwd.nextId(), deviceId); |
| 1220 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 1221 | return Collections.emptySet(); |
| 1222 | } |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1223 | if (group == null) { |
| 1224 | log.warn("Group with key:{} for next-id:{} not found in dev:{}", |
| 1225 | gkeys.get(0).peekFirst(), fwd.nextId(), deviceId); |
| 1226 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 1227 | return Collections.emptySet(); |
| 1228 | } |
| 1229 | tb.deferred().group(group.id()); |
Saurav Das | 9504700 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 1230 | // retrying flows may be necessary due to bug CORD-554 |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1231 | if (gkeys.size() == 1 && gkeys.get(0).size() == 1) { |
Saurav Das | 9504700 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 1232 | if (shouldRetry()) { |
| 1233 | log.warn("Found empty group 0x{} in dev:{} .. will retry fwd:{}", |
| 1234 | Integer.toHexString(group.id().id()), deviceId, |
| 1235 | fwd.id()); |
| 1236 | emptyGroup = true; |
| 1237 | } |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1238 | } |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 1239 | } else { |
| 1240 | log.warn("Cannot find group for nextId:{} in dev:{}. Aborting fwd:{}", |
| 1241 | fwd.nextId(), deviceId, fwd.id()); |
| 1242 | fail(fwd, ObjectiveError.FLOWINSTALLATIONFAILED); |
| 1243 | return Collections.emptySet(); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1244 | } |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1245 | } |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 1246 | |
| 1247 | if (forTableId == MPLS_TABLE_1) { |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 1248 | if (mplsNextTable == MPLS_L3_TYPE_TABLE) { |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 1249 | Ofdpa3SetMplsType setMplsType = new Ofdpa3SetMplsType(Ofdpa3MplsType.L3_PHP); |
Saurav Das | 9c70534 | 2017-01-06 11:36:01 -0800 | [diff] [blame] | 1250 | // set mpls type as apply_action |
| 1251 | tb.immediate().extension(setMplsType, deviceId); |
Charles Chan | cad338a | 2016-09-16 18:03:11 -0700 | [diff] [blame] | 1252 | } |
| 1253 | tb.transition(mplsNextTable); |
| 1254 | } else { |
| 1255 | tb.transition(ACL_TABLE); |
| 1256 | } |
| 1257 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1258 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 1259 | .fromApp(fwd.appId()) |
| 1260 | .withPriority(fwd.priority()) |
| 1261 | .forDevice(deviceId) |
Saurav Das | 8a0732e | 2015-11-20 15:27:53 -0800 | [diff] [blame] | 1262 | .withSelector(filteredSelector.build()) |
| 1263 | .withTreatment(tb.build()) |
| 1264 | .forTable(forTableId); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1265 | |
| 1266 | if (fwd.permanent()) { |
| 1267 | ruleBuilder.makePermanent(); |
| 1268 | } else { |
| 1269 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 1270 | } |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1271 | Collection<FlowRule> flowRuleCollection = new ArrayList<>(); |
| 1272 | flowRuleCollection.add(ruleBuilder.build()); |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1273 | if (!allowDefaultRoute) { |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1274 | flowRuleCollection.add( |
| 1275 | defaultRoute(fwd, complementarySelector, forTableId, tb) |
| 1276 | ); |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1277 | log.debug("Default rule 0.0.0.0/0 is being installed two rules"); |
| 1278 | } |
Saurav Das | 9504700 | 2018-01-25 09:49:01 -0800 | [diff] [blame] | 1279 | |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1280 | if (emptyGroup) { |
| 1281 | executorService.schedule(new RetryFlows(fwd, flowRuleCollection), |
| 1282 | RETRY_MS, TimeUnit.MILLISECONDS); |
| 1283 | } |
Flavio Castro | e10fa24 | 2016-01-15 12:43:51 -0800 | [diff] [blame] | 1284 | return flowRuleCollection; |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1285 | } |
| 1286 | |
Charles Chan | c6e64bb | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1287 | private int buildIpv4Selector(TrafficSelector.Builder builderToUpdate, |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1288 | TrafficSelector.Builder extBuilder, |
| 1289 | ForwardingObjective fwd, |
| 1290 | boolean allowDefaultRoute) { |
| 1291 | TrafficSelector selector = fwd.selector(); |
| 1292 | |
| 1293 | IpPrefix ipv4Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV4_DST)).ip(); |
| 1294 | if (ipv4Dst.isMulticast()) { |
| 1295 | if (ipv4Dst.prefixLength() != 32) { |
| 1296 | log.warn("Multicast specific forwarding objective can only be /32"); |
| 1297 | fail(fwd, ObjectiveError.BADPARAMS); |
| 1298 | return -1; |
| 1299 | } |
| 1300 | VlanId assignedVlan = readVlanFromSelector(fwd.meta()); |
| 1301 | if (assignedVlan == null) { |
| 1302 | log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort."); |
| 1303 | fail(fwd, ObjectiveError.BADPARAMS); |
| 1304 | return -1; |
| 1305 | } |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 1306 | if (requireVlanExtensions()) { |
| 1307 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan); |
| 1308 | builderToUpdate.extension(ofdpaMatchVlanVid, deviceId); |
| 1309 | } else { |
| 1310 | builderToUpdate.matchVlanId(assignedVlan); |
| 1311 | } |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1312 | builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst); |
| 1313 | log.debug("processing IPv4 multicast specific forwarding objective {} -> next:{}" |
| 1314 | + " in dev:{}", fwd.id(), fwd.nextId(), deviceId); |
| 1315 | } else { |
| 1316 | if (ipv4Dst.prefixLength() == 0) { |
| 1317 | if (allowDefaultRoute) { |
| 1318 | // The entire IPV4_DST field is wildcarded intentionally |
| 1319 | builderToUpdate.matchEthType(Ethernet.TYPE_IPV4); |
| 1320 | } else { |
| 1321 | // NOTE: The switch does not support matching 0.0.0.0/0 |
| 1322 | // Split it into 0.0.0.0/1 and 128.0.0.0/1 |
| 1323 | builderToUpdate.matchEthType(Ethernet.TYPE_IPV4) |
| 1324 | .matchIPDst(IpPrefix.valueOf("0.0.0.0/1")); |
| 1325 | extBuilder.matchEthType(Ethernet.TYPE_IPV4) |
| 1326 | .matchIPDst(IpPrefix.valueOf("128.0.0.0/1")); |
| 1327 | } |
| 1328 | } else { |
| 1329 | builderToUpdate.matchEthType(Ethernet.TYPE_IPV4).matchIPDst(ipv4Dst); |
| 1330 | } |
| 1331 | log.debug("processing IPv4 unicast specific forwarding objective {} -> next:{}" |
| 1332 | + " in dev:{}", fwd.id(), fwd.nextId(), deviceId); |
| 1333 | } |
| 1334 | return 0; |
| 1335 | } |
| 1336 | |
| 1337 | /** |
| 1338 | * Helper method to build Ipv6 selector using the selector provided by |
| 1339 | * a forwarding objective. |
| 1340 | * |
| 1341 | * @param builderToUpdate the builder to update |
| 1342 | * @param fwd the selector to read |
| 1343 | * @return 0 if the update ends correctly. -1 if the matches |
| 1344 | * are not yet supported |
| 1345 | */ |
Charles Chan | c6e64bb | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1346 | int buildIpv6Selector(TrafficSelector.Builder builderToUpdate, |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1347 | ForwardingObjective fwd) { |
| 1348 | |
| 1349 | TrafficSelector selector = fwd.selector(); |
| 1350 | |
| 1351 | IpPrefix ipv6Dst = ((IPCriterion) selector.getCriterion(Criterion.Type.IPV6_DST)).ip(); |
| 1352 | if (ipv6Dst.isMulticast()) { |
Julia Ferguson | d8f145e | 2017-08-10 18:15:24 +0000 | [diff] [blame] | 1353 | if (ipv6Dst.prefixLength() != IpAddress.INET6_BIT_LENGTH) { |
| 1354 | log.warn("Multicast specific forwarding objective can only be /128"); |
| 1355 | fail(fwd, ObjectiveError.BADPARAMS); |
| 1356 | return -1; |
| 1357 | } |
| 1358 | VlanId assignedVlan = readVlanFromSelector(fwd.meta()); |
| 1359 | if (assignedVlan == null) { |
| 1360 | log.warn("VLAN ID required by multicast specific fwd obj is missing. Abort."); |
| 1361 | fail(fwd, ObjectiveError.BADPARAMS); |
| 1362 | return -1; |
| 1363 | } |
| 1364 | if (requireVlanExtensions()) { |
| 1365 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(assignedVlan); |
| 1366 | builderToUpdate.extension(ofdpaMatchVlanVid, deviceId); |
| 1367 | } else { |
| 1368 | builderToUpdate.matchVlanId(assignedVlan); |
| 1369 | } |
| 1370 | builderToUpdate.matchEthType(Ethernet.TYPE_IPV6).matchIPv6Dst(ipv6Dst); |
| 1371 | log.debug("processing IPv6 multicast specific forwarding objective {} -> next:{}" |
| 1372 | + " in dev:{}", fwd.id(), fwd.nextId(), deviceId); |
| 1373 | } else { |
| 1374 | if (ipv6Dst.prefixLength() != 0) { |
| 1375 | builderToUpdate.matchIPv6Dst(ipv6Dst); |
| 1376 | } |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1377 | builderToUpdate.matchEthType(Ethernet.TYPE_IPV6); |
| 1378 | log.debug("processing IPv6 unicast specific forwarding objective {} -> next:{}" |
| 1379 | + " in dev:{}", fwd.id(), fwd.nextId(), deviceId); |
Julia Ferguson | d8f145e | 2017-08-10 18:15:24 +0000 | [diff] [blame] | 1380 | } |
Pier Luigi | 3bfe32c | 2017-01-30 09:47:36 -0800 | [diff] [blame] | 1381 | return 0; |
| 1382 | } |
| 1383 | |
Charles Chan | c6e64bb | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1384 | FlowRule defaultRoute(ForwardingObjective fwd, |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1385 | TrafficSelector.Builder complementarySelector, |
| 1386 | int forTableId, |
| 1387 | TrafficTreatment.Builder tb) { |
| 1388 | FlowRule.Builder rule = DefaultFlowRule.builder() |
| 1389 | .fromApp(fwd.appId()) |
| 1390 | .withPriority(fwd.priority()) |
| 1391 | .forDevice(deviceId) |
| 1392 | .withSelector(complementarySelector.build()) |
| 1393 | .withTreatment(tb.build()) |
| 1394 | .forTable(forTableId); |
| 1395 | if (fwd.permanent()) { |
| 1396 | rule.makePermanent(); |
| 1397 | } else { |
| 1398 | rule.makeTemporary(fwd.timeout()); |
| 1399 | } |
| 1400 | return rule.build(); |
| 1401 | } |
| 1402 | |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1403 | /** |
| 1404 | * Handles forwarding rules to the L2 bridging table. Flow actions are not |
| 1405 | * allowed in the bridging table - instead we use L2 Interface group or |
| 1406 | * L2 flood group |
| 1407 | * |
| 1408 | * @param fwd the forwarding objective |
| 1409 | * @return A collection of flow rules, or an empty set |
| 1410 | */ |
| 1411 | protected Collection<FlowRule> processEthDstSpecific(ForwardingObjective fwd) { |
| 1412 | List<FlowRule> rules = new ArrayList<>(); |
| 1413 | |
| 1414 | // Build filtered selector |
| 1415 | TrafficSelector selector = fwd.selector(); |
| 1416 | EthCriterion ethCriterion = (EthCriterion) selector |
| 1417 | .getCriterion(Criterion.Type.ETH_DST); |
| 1418 | VlanIdCriterion vlanIdCriterion = (VlanIdCriterion) selector |
| 1419 | .getCriterion(Criterion.Type.VLAN_VID); |
| 1420 | |
| 1421 | if (vlanIdCriterion == null) { |
| 1422 | log.warn("Forwarding objective for bridging requires vlan. Not " |
| 1423 | + "installing fwd:{} in dev:{}", fwd.id(), deviceId); |
| 1424 | fail(fwd, ObjectiveError.BADPARAMS); |
| 1425 | return Collections.emptySet(); |
| 1426 | } |
| 1427 | |
| 1428 | TrafficSelector.Builder filteredSelectorBuilder = |
| 1429 | DefaultTrafficSelector.builder(); |
Yi Tseng | 3a77b4f | 2017-02-06 15:02:17 -0800 | [diff] [blame] | 1430 | |
| 1431 | if (!ethCriterion.mac().equals(NONE) && |
| 1432 | !ethCriterion.mac().equals(BROADCAST)) { |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1433 | filteredSelectorBuilder.matchEthDst(ethCriterion.mac()); |
| 1434 | log.debug("processing L2 forwarding objective:{} -> next:{} in dev:{}", |
| 1435 | fwd.id(), fwd.nextId(), deviceId); |
| 1436 | } else { |
Yi Tseng | 3a77b4f | 2017-02-06 15:02:17 -0800 | [diff] [blame] | 1437 | // Use wildcard DST_MAC if the MacAddress is None or Broadcast |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1438 | log.debug("processing L2 Broadcast forwarding objective:{} -> next:{} " |
| 1439 | + "in dev:{} for vlan:{}", |
| 1440 | fwd.id(), fwd.nextId(), deviceId, vlanIdCriterion.vlanId()); |
| 1441 | } |
Charles Chan | d117263 | 2017-03-15 17:33:09 -0700 | [diff] [blame] | 1442 | if (requireVlanExtensions()) { |
| 1443 | OfdpaMatchVlanVid ofdpaMatchVlanVid = new OfdpaMatchVlanVid(vlanIdCriterion.vlanId()); |
| 1444 | filteredSelectorBuilder.extension(ofdpaMatchVlanVid, deviceId); |
| 1445 | } else { |
| 1446 | filteredSelectorBuilder.matchVlanId(vlanIdCriterion.vlanId()); |
| 1447 | } |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1448 | TrafficSelector filteredSelector = filteredSelectorBuilder.build(); |
| 1449 | |
| 1450 | if (fwd.treatment() != null) { |
| 1451 | log.warn("Ignoring traffic treatment in fwd rule {} meant for L2 table" |
| 1452 | + "for dev:{}. Expecting only nextId", fwd.id(), deviceId); |
| 1453 | } |
| 1454 | |
| 1455 | TrafficTreatment.Builder treatmentBuilder = DefaultTrafficTreatment.builder(); |
| 1456 | if (fwd.nextId() != null) { |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1457 | NextGroup next = getGroupForNextObjective(fwd.nextId()); |
Saurav Das | 4ce4596 | 2015-11-24 23:21:05 -0800 | [diff] [blame] | 1458 | if (next != null) { |
| 1459 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 1460 | // we only need the top level group's key to point the flow to it |
| 1461 | Group group = groupService.getGroup(deviceId, gkeys.get(0).peekFirst()); |
| 1462 | if (group != null) { |
| 1463 | treatmentBuilder.deferred().group(group.id()); |
| 1464 | } else { |
| 1465 | log.warn("Group with key:{} for next-id:{} not found in dev:{}", |
| 1466 | gkeys.get(0).peekFirst(), fwd.nextId(), deviceId); |
| 1467 | fail(fwd, ObjectiveError.GROUPMISSING); |
| 1468 | return Collections.emptySet(); |
| 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | treatmentBuilder.immediate().transition(ACL_TABLE); |
| 1473 | TrafficTreatment filteredTreatment = treatmentBuilder.build(); |
| 1474 | |
| 1475 | // Build bridging table entries |
| 1476 | FlowRule.Builder flowRuleBuilder = DefaultFlowRule.builder(); |
| 1477 | flowRuleBuilder.fromApp(fwd.appId()) |
| 1478 | .withPriority(fwd.priority()) |
| 1479 | .forDevice(deviceId) |
| 1480 | .withSelector(filteredSelector) |
| 1481 | .withTreatment(filteredTreatment) |
| 1482 | .forTable(BRIDGING_TABLE); |
| 1483 | if (fwd.permanent()) { |
| 1484 | flowRuleBuilder.makePermanent(); |
| 1485 | } else { |
| 1486 | flowRuleBuilder.makeTemporary(fwd.timeout()); |
| 1487 | } |
| 1488 | rules.add(flowRuleBuilder.build()); |
| 1489 | return rules; |
| 1490 | } |
| 1491 | |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1492 | ////////////////////////////////////// |
| 1493 | // Helper Methods and Classes |
| 1494 | ////////////////////////////////////// |
| 1495 | |
| 1496 | private boolean isSupportedEthTypeObjective(ForwardingObjective fwd) { |
| 1497 | TrafficSelector selector = fwd.selector(); |
| 1498 | EthTypeCriterion ethType = (EthTypeCriterion) selector |
| 1499 | .getCriterion(Criterion.Type.ETH_TYPE); |
| 1500 | return !((ethType == null) || |
| 1501 | ((ethType.ethType().toShort() != Ethernet.TYPE_IPV4) && |
Pier Ventre | e0ae7a3 | 2016-11-23 09:57:42 -0800 | [diff] [blame] | 1502 | (ethType.ethType().toShort() != Ethernet.MPLS_UNICAST)) && |
| 1503 | (ethType.ethType().toShort() != Ethernet.TYPE_IPV6)); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1504 | } |
| 1505 | |
| 1506 | private boolean isSupportedEthDstObjective(ForwardingObjective fwd) { |
| 1507 | TrafficSelector selector = fwd.selector(); |
| 1508 | EthCriterion ethDst = (EthCriterion) selector |
| 1509 | .getCriterion(Criterion.Type.ETH_DST); |
| 1510 | VlanIdCriterion vlanId = (VlanIdCriterion) selector |
| 1511 | .getCriterion(Criterion.Type.VLAN_VID); |
| 1512 | return !(ethDst == null && vlanId == null); |
| 1513 | } |
| 1514 | |
Charles Chan | c6e64bb | 2018-03-02 13:26:22 -0800 | [diff] [blame] | 1515 | NextGroup getGroupForNextObjective(Integer nextId) { |
Saurav Das | 423fe2b | 2015-12-04 10:52:59 -0800 | [diff] [blame] | 1516 | NextGroup next = flowObjectiveStore.getNextGroup(nextId); |
| 1517 | if (next != null) { |
| 1518 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(next.data()); |
| 1519 | if (gkeys != null && !gkeys.isEmpty()) { |
| 1520 | return next; |
| 1521 | } else { |
| 1522 | log.warn("Empty next group found in FlowObjective store for " |
| 1523 | + "next-id:{} in dev:{}", nextId, deviceId); |
| 1524 | } |
| 1525 | } else { |
| 1526 | log.warn("next-id {} not found in Flow objective store for dev:{}", |
| 1527 | nextId, deviceId); |
| 1528 | } |
| 1529 | return null; |
| 1530 | } |
| 1531 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1532 | protected static void pass(Objective obj) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 1533 | obj.context().ifPresent(context -> context.onSuccess(obj)); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1534 | } |
| 1535 | |
Charles Chan | 188ebf5 | 2015-12-23 00:15:11 -0800 | [diff] [blame] | 1536 | protected static void fail(Objective obj, ObjectiveError error) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 1537 | obj.context().ifPresent(context -> context.onError(obj, error)); |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1538 | } |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1539 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1540 | @Override |
| 1541 | public List<String> getNextMappings(NextGroup nextGroup) { |
| 1542 | List<String> mappings = new ArrayList<>(); |
| 1543 | List<Deque<GroupKey>> gkeys = appKryo.deserialize(nextGroup.data()); |
| 1544 | for (Deque<GroupKey> gkd : gkeys) { |
| 1545 | Group lastGroup = null; |
Yuta HIGUCHI | 2dce08a | 2017-04-20 21:57:48 -0700 | [diff] [blame] | 1546 | StringBuilder gchain = new StringBuilder(); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1547 | for (GroupKey gk : gkd) { |
| 1548 | Group g = groupService.getGroup(deviceId, gk); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1549 | if (g == null) { |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 1550 | gchain.append(" NoGrp").append(" -->"); |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1551 | continue; |
| 1552 | } |
| 1553 | gchain.append(" 0x").append(Integer.toHexString(g.id().id())) |
| 1554 | .append(" -->"); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1555 | lastGroup = g; |
| 1556 | } |
| 1557 | // add port information for last group in group-chain |
Yuta HIGUCHI | 2dce08a | 2017-04-20 21:57:48 -0700 | [diff] [blame] | 1558 | List<Instruction> lastGroupIns = new ArrayList<>(); |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1559 | if (lastGroup != null && !lastGroup.buckets().buckets().isEmpty()) { |
Saurav Das | 2519081 | 2016-05-27 13:54:07 -0700 | [diff] [blame] | 1560 | lastGroupIns = lastGroup.buckets().buckets().get(0) |
| 1561 | .treatment().allInstructions(); |
| 1562 | } |
| 1563 | for (Instruction i: lastGroupIns) { |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1564 | if (i instanceof OutputInstruction) { |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1565 | gchain.append(" port:").append(((OutputInstruction) i).port()); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1566 | } |
| 1567 | } |
Saurav Das | 8be4e3a | 2016-03-11 17:19:07 -0800 | [diff] [blame] | 1568 | mappings.add(gchain.toString()); |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 1569 | } |
| 1570 | return mappings; |
| 1571 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1572 | |
Pier Ventre | 140a894 | 2016-11-02 07:26:38 -0700 | [diff] [blame] | 1573 | static boolean isMplsBos(TrafficSelector selector) { |
| 1574 | MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS); |
| 1575 | return bosCriterion != null && bosCriterion.mplsBos(); |
| 1576 | } |
| 1577 | |
| 1578 | static boolean isNotMplsBos(TrafficSelector selector) { |
| 1579 | MplsBosCriterion bosCriterion = (MplsBosCriterion) selector.getCriterion(MPLS_BOS); |
| 1580 | return bosCriterion != null && !bosCriterion.mplsBos(); |
| 1581 | } |
| 1582 | |
Charles Chan | 30122a7 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 1583 | private static boolean isIpv6(TrafficSelector selector) { |
| 1584 | EthTypeCriterion ethTypeCriterion = (EthTypeCriterion) selector.getCriterion(ETH_TYPE); |
| 1585 | return ethTypeCriterion != null && ethTypeCriterion.ethType().toShort() == Ethernet.TYPE_IPV6; |
| 1586 | } |
| 1587 | |
| 1588 | static VlanId readVlanFromSelector(TrafficSelector selector) { |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1589 | if (selector == null) { |
| 1590 | return null; |
| 1591 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1592 | Criterion criterion = selector.getCriterion(Criterion.Type.VLAN_VID); |
| 1593 | return (criterion == null) |
| 1594 | ? null : ((VlanIdCriterion) criterion).vlanId(); |
| 1595 | } |
| 1596 | |
Charles Chan | 30122a7 | 2017-10-05 15:17:15 -0700 | [diff] [blame] | 1597 | static IpPrefix readIpDstFromSelector(TrafficSelector selector) { |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1598 | if (selector == null) { |
| 1599 | return null; |
| 1600 | } |
Charles Chan | 5b9df8d | 2016-03-28 22:21:40 -0700 | [diff] [blame] | 1601 | Criterion criterion = selector.getCriterion(Criterion.Type.IPV4_DST); |
| 1602 | return (criterion == null) ? null : ((IPCriterion) criterion).ip(); |
| 1603 | } |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1604 | |
| 1605 | private static VlanId readVlanFromTreatment(TrafficTreatment treatment) { |
Saurav Das | 59232cf | 2016-04-27 18:35:50 -0700 | [diff] [blame] | 1606 | if (treatment == null) { |
| 1607 | return null; |
| 1608 | } |
Charles Chan | d55e84d | 2016-03-30 17:54:24 -0700 | [diff] [blame] | 1609 | for (Instruction i : treatment.allInstructions()) { |
| 1610 | if (i instanceof ModVlanIdInstruction) { |
| 1611 | return ((ModVlanIdInstruction) i).vlanId(); |
| 1612 | } |
| 1613 | } |
| 1614 | return null; |
| 1615 | } |
Saurav Das | 1ce0a7b | 2016-10-21 14:06:29 -0700 | [diff] [blame] | 1616 | |
| 1617 | /** |
| 1618 | * Utility class that retries sending flows a fixed number of times, even if |
| 1619 | * some of the attempts are successful. Used only for forwarding objectives. |
| 1620 | */ |
| 1621 | protected final class RetryFlows implements Runnable { |
| 1622 | int attempts = MAX_RETRY_ATTEMPTS; |
| 1623 | private Collection<FlowRule> retryFlows; |
| 1624 | private ForwardingObjective fwd; |
| 1625 | |
| 1626 | RetryFlows(ForwardingObjective fwd, Collection<FlowRule> retryFlows) { |
| 1627 | this.fwd = fwd; |
| 1628 | this.retryFlows = retryFlows; |
| 1629 | } |
| 1630 | |
| 1631 | @Override |
| 1632 | public void run() { |
| 1633 | log.info("RETRY FLOWS ATTEMPT# {} for fwd:{} rules:{}", |
| 1634 | MAX_RETRY_ATTEMPTS - attempts, fwd.id(), retryFlows.size()); |
| 1635 | sendForward(fwd, retryFlows); |
| 1636 | if (--attempts > 0) { |
| 1637 | executorService.schedule(this, RETRY_MS, TimeUnit.MILLISECONDS); |
| 1638 | } |
| 1639 | } |
| 1640 | } |
| 1641 | |
Saurav Das | 822c4e2 | 2015-10-23 10:51:11 -0700 | [diff] [blame] | 1642 | } |