alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
alshabib | faa1e36 | 2015-04-02 15:01:54 -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 | */ |
| 16 | package org.onosproject.driver.pipeline; |
| 17 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 18 | import com.google.common.cache.Cache; |
| 19 | import com.google.common.cache.CacheBuilder; |
| 20 | import com.google.common.cache.RemovalCause; |
| 21 | import com.google.common.cache.RemovalNotification; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 22 | import org.onlab.osgi.ServiceDirectory; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 23 | import org.onlab.util.KryoNamespace; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 24 | import org.onosproject.net.DeviceId; |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 25 | import org.onosproject.net.behaviour.NextGroup; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 26 | import org.onosproject.net.behaviour.Pipeliner; |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 27 | import org.onosproject.net.behaviour.PipelinerContext; |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 28 | import org.onosproject.net.driver.AbstractHandlerBehaviour; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 29 | import org.onosproject.net.flow.DefaultFlowRule; |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 30 | import org.onosproject.net.flow.DefaultTrafficSelector; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 31 | import org.onosproject.net.flow.DefaultTrafficTreatment; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 32 | import org.onosproject.net.flow.FlowRule; |
| 33 | import org.onosproject.net.flow.FlowRuleOperations; |
| 34 | import org.onosproject.net.flow.FlowRuleOperationsContext; |
| 35 | import org.onosproject.net.flow.FlowRuleService; |
| 36 | import org.onosproject.net.flow.TrafficSelector; |
Srikanth Vavilapalli | f5b234a | 2015-04-21 13:04:13 -0700 | [diff] [blame] | 37 | import org.onosproject.net.flow.TrafficTreatment; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 38 | import org.onosproject.net.flowobjective.FilteringObjective; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 39 | import org.onosproject.net.flowobjective.FlowObjectiveStore; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 40 | import org.onosproject.net.flowobjective.ForwardingObjective; |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 41 | import org.onosproject.net.flowobjective.NextObjective; |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 42 | import org.onosproject.net.flowobjective.Objective; |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 43 | import org.onosproject.net.flowobjective.ObjectiveError; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 44 | import org.onosproject.net.group.DefaultGroupKey; |
| 45 | import org.onosproject.net.group.GroupKey; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 46 | import org.slf4j.Logger; |
| 47 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 48 | import java.util.List; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 49 | import java.util.concurrent.TimeUnit; |
| 50 | |
| 51 | import static org.slf4j.LoggerFactory.getLogger; |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 52 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 53 | /** |
| 54 | * Simple single table pipeline abstraction. |
| 55 | */ |
Thomas Vachuska | facc3f5 | 2015-04-10 08:58:36 -0700 | [diff] [blame] | 56 | public class DefaultSingleTablePipeline extends AbstractHandlerBehaviour implements Pipeliner { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 57 | |
| 58 | private final Logger log = getLogger(getClass()); |
| 59 | |
| 60 | private ServiceDirectory serviceDirectory; |
| 61 | private FlowRuleService flowRuleService; |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 62 | private FlowObjectiveStore flowObjectiveStore; |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 63 | private DeviceId deviceId; |
| 64 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 65 | private Cache<Integer, NextObjective> pendingNext; |
| 66 | |
| 67 | private KryoNamespace appKryo = new KryoNamespace.Builder() |
| 68 | .register(GroupKey.class) |
| 69 | .register(DefaultGroupKey.class) |
| 70 | .register(SingleGroup.class) |
| 71 | .register(byte[].class) |
Charles Chan | eefdedf | 2016-05-23 16:45:45 -0700 | [diff] [blame] | 72 | .build("DefaultSingleTablePipeline"); |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 73 | |
| 74 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 75 | @Override |
Thomas Vachuska | ca88bb7 | 2015-04-08 19:38:02 -0700 | [diff] [blame] | 76 | public void init(DeviceId deviceId, PipelinerContext context) { |
| 77 | this.serviceDirectory = context.directory(); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 78 | this.deviceId = deviceId; |
| 79 | |
| 80 | flowRuleService = serviceDirectory.get(FlowRuleService.class); |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 81 | flowObjectiveStore = serviceDirectory.get(FlowObjectiveStore.class); |
| 82 | |
| 83 | pendingNext = CacheBuilder.newBuilder() |
| 84 | .expireAfterWrite(20, TimeUnit.SECONDS) |
| 85 | .removalListener((RemovalNotification<Integer, NextObjective> notification) -> { |
| 86 | if (notification.getCause() == RemovalCause.EXPIRED) { |
| 87 | notification.getValue().context() |
| 88 | .ifPresent(c -> c.onError(notification.getValue(), |
| 89 | ObjectiveError.FLOWINSTALLATIONFAILED)); |
| 90 | } |
| 91 | }).build(); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 95 | public void filter(FilteringObjective filter) { |
| 96 | |
| 97 | TrafficTreatment.Builder actions; |
| 98 | switch (filter.type()) { |
| 99 | case PERMIT: |
| 100 | actions = (filter.meta() == null) ? |
| 101 | DefaultTrafficTreatment.builder().punt() : |
| 102 | DefaultTrafficTreatment.builder(filter.meta()); |
| 103 | break; |
| 104 | case DENY: |
| 105 | actions = (filter.meta() == null) ? |
| 106 | DefaultTrafficTreatment.builder() : |
| 107 | DefaultTrafficTreatment.builder(filter.meta()); |
| 108 | actions.drop(); |
| 109 | break; |
| 110 | default: |
| 111 | log.warn("Unknown filter type: {}", filter.type()); |
| 112 | actions = DefaultTrafficTreatment.builder().drop(); |
| 113 | } |
| 114 | |
| 115 | TrafficSelector.Builder selector = DefaultTrafficSelector.builder(); |
| 116 | |
| 117 | filter.conditions().stream().forEach(selector::add); |
| 118 | |
| 119 | if (filter.key() != null) { |
| 120 | selector.add(filter.key()); |
| 121 | } |
| 122 | |
| 123 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 124 | .forDevice(deviceId) |
| 125 | .withSelector(selector.build()) |
| 126 | .withTreatment(actions.build()) |
| 127 | .fromApp(filter.appId()) |
| 128 | .withPriority(filter.priority()); |
| 129 | |
| 130 | if (filter.permanent()) { |
| 131 | ruleBuilder.makePermanent(); |
| 132 | } else { |
| 133 | ruleBuilder.makeTemporary(filter.timeout()); |
| 134 | } |
| 135 | |
| 136 | installObjective(ruleBuilder, filter); |
| 137 | |
| 138 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 139 | |
| 140 | @Override |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 141 | public void forward(ForwardingObjective fwd) { |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 142 | TrafficSelector selector = fwd.selector(); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 143 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 144 | if (fwd.treatment() != null) { |
| 145 | // Deal with SPECIFIC and VERSATILE in the same manner. |
| 146 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 147 | .forDevice(deviceId) |
| 148 | .withSelector(selector) |
| 149 | .fromApp(fwd.appId()) |
| 150 | .withPriority(fwd.priority()) |
| 151 | .withTreatment(fwd.treatment()); |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 152 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 153 | if (fwd.permanent()) { |
| 154 | ruleBuilder.makePermanent(); |
| 155 | } else { |
| 156 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 157 | } |
| 158 | installObjective(ruleBuilder, fwd); |
| 159 | |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 160 | } else { |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 161 | NextObjective nextObjective = pendingNext.getIfPresent(fwd.nextId()); |
| 162 | if (nextObjective != null) { |
| 163 | pendingNext.invalidate(fwd.nextId()); |
| 164 | nextObjective.next().forEach(treat -> { |
| 165 | FlowRule.Builder ruleBuilder = DefaultFlowRule.builder() |
| 166 | .forDevice(deviceId) |
| 167 | .withSelector(selector) |
| 168 | .fromApp(fwd.appId()) |
| 169 | .withPriority(fwd.priority()) |
| 170 | .withTreatment(treat); |
| 171 | |
| 172 | if (fwd.permanent()) { |
| 173 | ruleBuilder.makePermanent(); |
| 174 | } else { |
| 175 | ruleBuilder.makeTemporary(fwd.timeout()); |
| 176 | } |
| 177 | installObjective(ruleBuilder, fwd); |
| 178 | }); |
| 179 | } else { |
| 180 | fwd.context().ifPresent(c -> c.onError(fwd, |
| 181 | ObjectiveError.GROUPMISSING)); |
| 182 | } |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 183 | } |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | private void installObjective(FlowRule.Builder ruleBuilder, Objective objective) { |
| 187 | FlowRuleOperations.Builder flowBuilder = FlowRuleOperations.builder(); |
| 188 | switch (objective.op()) { |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 189 | |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 190 | case ADD: |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 191 | flowBuilder.add(ruleBuilder.build()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 192 | break; |
| 193 | case REMOVE: |
alshabib | b452fd7 | 2015-04-22 20:46:20 -0700 | [diff] [blame] | 194 | flowBuilder.remove(ruleBuilder.build()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 195 | break; |
| 196 | default: |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 197 | log.warn("Unknown operation {}", objective.op()); |
alshabib | 2a441c6 | 2015-04-13 18:39:38 -0700 | [diff] [blame] | 198 | } |
| 199 | |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 200 | flowRuleService.apply(flowBuilder.build(new FlowRuleOperationsContext() { |
| 201 | @Override |
| 202 | public void onSuccess(FlowRuleOperations ops) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 203 | objective.context().ifPresent(context -> context.onSuccess(objective)); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Override |
| 207 | public void onError(FlowRuleOperations ops) { |
Sho SHIMIZU | ef7e290 | 2016-02-12 18:38:29 -0800 | [diff] [blame] | 208 | objective.context() |
| 209 | .ifPresent(context -> context.onError(objective, ObjectiveError.FLOWINSTALLATIONFAILED)); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 210 | } |
| 211 | })); |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 212 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 213 | |
| 214 | @Override |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 215 | public void next(NextObjective nextObjective) { |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 216 | |
| 217 | pendingNext.put(nextObjective.id(), nextObjective); |
| 218 | flowObjectiveStore.putNextGroup(nextObjective.id(), |
| 219 | new SingleGroup(new DefaultGroupKey(appKryo.serialize(nextObjective.id())))); |
Ray Milkey | ce7db1b | 2016-05-24 13:33:48 -0700 | [diff] [blame] | 220 | nextObjective.context().ifPresent(context -> context.onSuccess(nextObjective)); |
alshabib | c61e18c | 2016-02-02 23:05:25 -0800 | [diff] [blame] | 221 | } |
alshabib | 77b8848 | 2015-04-07 15:47:50 -0700 | [diff] [blame] | 222 | |
Saurav Das | 2443119 | 2016-03-07 19:13:00 -0800 | [diff] [blame] | 223 | @Override |
| 224 | public List<String> getNextMappings(NextGroup nextGroup) { |
| 225 | // Default single table pipeline does not use nextObjectives or groups |
| 226 | return null; |
| 227 | } |
| 228 | |
Michele Santuari | d2c8b15 | 2016-03-30 17:57:56 -0700 | [diff] [blame] | 229 | private class SingleGroup implements NextGroup { |
| 230 | |
| 231 | private final GroupKey key; |
| 232 | |
| 233 | public SingleGroup(GroupKey key) { |
| 234 | this.key = key; |
| 235 | } |
| 236 | |
| 237 | public GroupKey key() { |
| 238 | return key; |
| 239 | } |
| 240 | |
| 241 | @Override |
| 242 | public byte[] data() { |
| 243 | return appKryo.serialize(key); |
| 244 | } |
| 245 | |
| 246 | } |
alshabib | faa1e36 | 2015-04-02 15:01:54 -0700 | [diff] [blame] | 247 | } |