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