2 * Copyright (C) 2014 Red Hat, Inc.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 * Authors : Madhu Venugopal
10 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
12 import java.math.BigInteger;
13 import java.util.List;
15 import org.opendaylight.ovsdb.openstack.netvirt.api.ClassifierProvider;
16 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
17 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
18 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
19 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
20 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
21 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
44 import com.google.common.collect.Lists;
45 import org.osgi.framework.BundleContext;
46 import org.osgi.framework.ServiceReference;
48 public class ClassifierService extends AbstractServiceInstance implements ClassifierProvider, ConfigInterface {
49 public final static long REG_VALUE_FROM_LOCAL = 0x1L;
50 public final static long REG_VALUE_FROM_REMOTE = 0x2L;
51 public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg0.class;
53 public ClassifierService() {
54 super(Service.CLASSIFIER);
57 public ClassifierService(Service service) {
62 * (Table:Classifier) Egress VM Traffic Towards TEP
63 * Match: Destination Ethernet Addr and OpenFlow InPort
64 * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
65 * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
66 * actions=set_field:5->tun_id,goto_table=<next-table>"
70 public void programLocalInPort(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
71 String nodeName = OPENFLOW + dpidLong;
73 MatchBuilder matchBuilder = new MatchBuilder();
74 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
75 FlowBuilder flowBuilder = new FlowBuilder();
77 // Create the OF Match using MatchBuilder
78 flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
79 // TODO Broken In_Port Match
80 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
82 String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
83 // Add Flow Attributes
84 flowBuilder.setId(new FlowId(flowId));
85 FlowKey key = new FlowKey(new FlowId(flowId));
86 flowBuilder.setStrict(true);
87 flowBuilder.setBarrier(false);
88 flowBuilder.setTableId(getTable());
89 flowBuilder.setKey(key);
90 flowBuilder.setFlowName(flowId);
91 flowBuilder.setHardTimeout(0);
92 flowBuilder.setIdleTimeout(0);
95 // Instantiate the Builders for the OF Actions and Instructions
96 InstructionBuilder ib = new InstructionBuilder();
97 InstructionsBuilder isb = new InstructionsBuilder();
99 // Instructions List Stores Individual Instructions
100 List<Instruction> instructions = Lists.newArrayList();
102 // TODO Broken SetTunID
103 InstructionUtils.createSetTunnelIdInstructions(ib, new BigInteger(segmentationId));
104 ApplyActionsCase aac = (ApplyActionsCase) ib.getInstruction();
105 List<Action> actionList = aac.getApplyActions().getAction();
107 ActionBuilder ab = new ActionBuilder();
108 ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
109 BigInteger.valueOf(REG_VALUE_FROM_LOCAL)));
111 ab.setKey(new ActionKey(1));
112 actionList.add(ab.build());
115 ib.setKey(new InstructionKey(0));
116 instructions.add(ib.build());
118 // Next service GOTO Instructions Need to be appended to the List
119 ib = this.getMutablePipelineInstructionBuilder();
121 ib.setKey(new InstructionKey(1));
122 instructions.add(ib.build());
124 // Add InstructionBuilder to the Instruction(s)Builder List
125 isb.setInstruction(instructions);
127 // Add InstructionsBuilder to FlowBuilder
128 flowBuilder.setInstructions(isb.build());
130 writeFlow(flowBuilder, nodeBuilder);
132 removeFlow(flowBuilder, nodeBuilder);
137 * (Table:Classifier) Egress VM Traffic Towards TEP
138 * Match: Source Ethernet Addr and OpenFlow InPort
139 * Instruction: Set VLANID and GOTO Table Egress (n)
140 * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
141 * actions=push_vlan, set_field:5->vlan_id,goto_table=<Next-Table>"
142 * table=0,in_port=1,vlan_tci=0x0000/0x1fff,dl_src=fa:16:3e:70:2f:c2 actions=push_vlan:0x8100,set_field:6097->vlan_vid,goto_table:20
146 public void programLocalInPortSetVlan(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
148 String nodeName = OPENFLOW + dpidLong;
150 MatchBuilder matchBuilder = new MatchBuilder();
151 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
152 FlowBuilder flowBuilder = new FlowBuilder();
154 // Create the OF Match using MatchBuilder
155 flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
156 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
157 /* openflowplugin requires a vlan match to add a vlan */
158 flowBuilder.setMatch(MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(0), false).build());
160 String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
161 // Add Flow Attributes
162 flowBuilder.setId(new FlowId(flowId));
163 FlowKey key = new FlowKey(new FlowId(flowId));
164 flowBuilder.setStrict(true);
165 flowBuilder.setBarrier(false);
166 flowBuilder.setTableId(getTable());
167 flowBuilder.setKey(key);
168 flowBuilder.setFlowName(flowId);
169 flowBuilder.setHardTimeout(0);
170 flowBuilder.setIdleTimeout(0);
173 // Instantiate the Builders for the OF Actions and Instructions
174 InstructionBuilder ib = new InstructionBuilder();
175 InstructionsBuilder isb = new InstructionsBuilder();
177 // Instructions List Stores Individual Instructions
178 List<Instruction> instructions = Lists.newArrayList();
180 // Set VLAN ID Instruction
181 InstructionUtils.createSetVlanInstructions(ib, new VlanId(Integer.valueOf(segmentationId)));
183 ib.setKey(new InstructionKey(0));
184 instructions.add(ib.build());
186 // Next service GOTO Instructions Need to be appended to the List
187 ib = this.getMutablePipelineInstructionBuilder();
189 ib.setKey(new InstructionKey(1));
190 instructions.add(ib.build());
192 // Add InstructionBuilder to the Instruction(s)Builder List
193 isb.setInstruction(instructions);
195 // Add InstructionsBuilder to FlowBuilder
196 flowBuilder.setInstructions(isb.build());
198 writeFlow(flowBuilder, nodeBuilder);
200 removeFlow(flowBuilder, nodeBuilder);
205 * (Table:Classifier) Drop frames source from a VM that do not
206 * match the associated MAC address of the local VM.
207 * Match: Low priority anything not matching the VM SMAC
209 * table=0,priority=16384,in_port=1 actions=drop"
213 public void programDropSrcIface(Long dpidLong, Long inPort, boolean write) {
215 String nodeName = OPENFLOW + dpidLong;
217 MatchBuilder matchBuilder = new MatchBuilder();
218 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
219 FlowBuilder flowBuilder = new FlowBuilder();
221 // Create the OF Match using MatchBuilder
222 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
225 // Instantiate the Builders for the OF Actions and Instructions
226 InstructionBuilder ib = new InstructionBuilder();
227 InstructionsBuilder isb = new InstructionsBuilder();
229 // Instructions List Stores Individual Instructions
230 List<Instruction> instructions = Lists.newArrayList();
232 // Call the InstructionBuilder Methods Containing Actions
233 InstructionUtils.createDropInstructions(ib);
235 ib.setKey(new InstructionKey(0));
236 instructions.add(ib.build());
238 // Add InstructionBuilder to the Instruction(s)Builder List
239 isb.setInstruction(instructions);
241 // Add InstructionsBuilder to FlowBuilder
242 flowBuilder.setInstructions(isb.build());
245 String flowId = "DropFilter_"+inPort;
246 // Add Flow Attributes
247 flowBuilder.setId(new FlowId(flowId));
248 FlowKey key = new FlowKey(new FlowId(flowId));
249 flowBuilder.setStrict(true);
250 flowBuilder.setBarrier(false);
251 flowBuilder.setTableId(getTable());
252 flowBuilder.setKey(key);
253 flowBuilder.setFlowName(flowId);
254 flowBuilder.setPriority(8192);
255 flowBuilder.setHardTimeout(0);
256 flowBuilder.setIdleTimeout(0);
258 writeFlow(flowBuilder, nodeBuilder);
260 removeFlow(flowBuilder, nodeBuilder);
264 * (Table:0) Ingress Tunnel Traffic
265 * Match: OpenFlow InPort and Tunnel ID
266 * Action: GOTO Local Table (10)
267 * table=0,tun_id=0x5,in_port=10, actions=goto_table:2
271 public void programTunnelIn(Long dpidLong, String segmentationId,
272 Long ofPort, boolean write) {
274 String nodeName = OPENFLOW + dpidLong;
276 BigInteger tunnelId = new BigInteger(segmentationId);
277 MatchBuilder matchBuilder = new MatchBuilder();
278 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
279 FlowBuilder flowBuilder = new FlowBuilder();
281 // Create Match(es) and Set them in the FlowBuilder Object
282 flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, tunnelId).build());
283 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ofPort).build());
286 // Create the OF Actions and Instructions
287 InstructionBuilder ib = new InstructionBuilder();
288 InstructionsBuilder isb = new InstructionsBuilder();
290 // Instructions List Stores Individual Instructions
291 List<Instruction> instructions = Lists.newArrayList();
293 List<Action> actionList = Lists.newArrayList();
294 ActionBuilder ab = new ActionBuilder();
295 ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
296 BigInteger.valueOf(REG_VALUE_FROM_REMOTE)));
298 ab.setKey(new ActionKey(0));
299 actionList.add(ab.build());
301 ApplyActionsBuilder aab = new ApplyActionsBuilder();
302 aab.setAction(actionList);
303 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
305 // Call the InstructionBuilder Methods Containing Actions
307 ib.setKey(new InstructionKey(0));
308 instructions.add(ib.build());
310 // Append the default pipeline after the first classification
311 ib = this.getMutablePipelineInstructionBuilder();
313 ib.setKey(new InstructionKey(1));
314 instructions.add(ib.build());
316 // Add InstructionBuilder to the Instruction(s)Builder List
317 isb.setInstruction(instructions);
319 // Add InstructionsBuilder to FlowBuilder
320 flowBuilder.setInstructions(isb.build());
323 String flowId = "TunnelIn_"+segmentationId+"_"+ofPort;
324 // Add Flow Attributes
325 flowBuilder.setId(new FlowId(flowId));
326 FlowKey key = new FlowKey(new FlowId(flowId));
327 flowBuilder.setStrict(true);
328 flowBuilder.setBarrier(false);
329 flowBuilder.setTableId(getTable());
330 flowBuilder.setKey(key);
331 flowBuilder.setFlowName(flowId);
332 flowBuilder.setHardTimeout(0);
333 flowBuilder.setIdleTimeout(0);
336 writeFlow(flowBuilder, nodeBuilder);
338 removeFlow(flowBuilder, nodeBuilder);
343 * (Table:0) Ingress VLAN Traffic
344 * Match: OpenFlow InPort and vlan ID
345 * Action: GOTO Local Table (20)
346 * table=0,vlan_id=0x5,in_port=10, actions=goto_table:2
347 * table=0,in_port=2,dl_vlan=2001 actions=goto_table:20
351 public void programVlanIn(Long dpidLong, String segmentationId, Long ethPort, boolean write) {
353 String nodeName = OPENFLOW + dpidLong;
355 MatchBuilder matchBuilder = new MatchBuilder();
356 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
357 FlowBuilder flowBuilder = new FlowBuilder();
359 // Create Match(es) and Set them in the FlowBuilder Object
360 flowBuilder.setMatch(MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
361 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ethPort).build());
364 // Create the OF Actions and Instructions
365 InstructionsBuilder isb = new InstructionsBuilder();
367 // Instructions List Stores Individual Instructions
368 List<Instruction> instructions = Lists.newArrayList();
370 // Append the default pipeline after the first classification
371 InstructionBuilder ib = this.getMutablePipelineInstructionBuilder();
373 ib.setKey(new InstructionKey(0));
374 instructions.add(ib.build());
376 // Add InstructionBuilder to the Instruction(s)Builder List
377 isb.setInstruction(instructions);
379 // Add InstructionsBuilder to FlowBuilder
380 flowBuilder.setInstructions(isb.build());
383 String flowId = "VlanIn_"+segmentationId+"_"+ethPort;
384 // Add Flow Attributes
385 flowBuilder.setId(new FlowId(flowId));
386 FlowKey key = new FlowKey(new FlowId(flowId));
387 flowBuilder.setStrict(true);
388 flowBuilder.setBarrier(false);
389 flowBuilder.setTableId(getTable());
390 flowBuilder.setKey(key);
391 flowBuilder.setFlowName(flowId);
392 flowBuilder.setHardTimeout(0);
393 flowBuilder.setIdleTimeout(0);
395 writeFlow(flowBuilder, nodeBuilder);
397 removeFlow(flowBuilder, nodeBuilder);
402 * Create an LLDP Flow Rule to encapsulate into
403 * a packet_in that is sent to the controller
404 * for topology handling.
405 * Match: Ethertype 0x88CCL
406 * Action: Punt to Controller in a Packet_In msg
410 public void programLLDPPuntRule(Long dpidLong) {
412 String nodeName = OPENFLOW + dpidLong;
413 EtherType etherType = new EtherType(0x88CCL);
415 MatchBuilder matchBuilder = new MatchBuilder();
416 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
417 FlowBuilder flowBuilder = new FlowBuilder();
419 // Create Match(es) and Set them in the FlowBuilder Object
420 flowBuilder.setMatch(MatchUtils.createEtherTypeMatch(matchBuilder, etherType).build());
422 // Create the OF Actions and Instructions
423 InstructionBuilder ib = new InstructionBuilder();
424 InstructionsBuilder isb = new InstructionsBuilder();
426 // Instructions List Stores Individual Instructions
427 List<Instruction> instructions = Lists.newArrayList();
429 // Call the InstructionBuilder Methods Containing Actions
430 InstructionUtils.createSendToControllerInstructions(nodeName, ib);
432 ib.setKey(new InstructionKey(0));
433 instructions.add(ib.build());
435 // Add InstructionBuilder to the Instruction(s)Builder List
436 isb.setInstruction(instructions);
438 // Add InstructionsBuilder to FlowBuilder
439 flowBuilder.setInstructions(isb.build());
441 String flowId = "LLDP";
442 flowBuilder.setId(new FlowId(flowId));
443 FlowKey key = new FlowKey(new FlowId(flowId));
444 flowBuilder.setBarrier(true);
445 flowBuilder.setTableId(getTable());
446 flowBuilder.setKey(key);
447 flowBuilder.setFlowName(flowId);
448 flowBuilder.setHardTimeout(0);
449 flowBuilder.setIdleTimeout(0);
450 writeFlow(flowBuilder, nodeBuilder);
454 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
455 super.setDependencies(bundleContext.getServiceReference(ClassifierProvider.class.getName()), this);
459 public void setDependencies(Object impl) {}