2 * Copyright (c) 2014, 2015 Red Hat, Inc. and others. All rights reserved.
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
9 package org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.services;
11 import java.math.BigInteger;
12 import java.util.List;
14 import org.opendaylight.ovsdb.openstack.netvirt.api.ClassifierProvider;
15 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
16 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
17 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
18 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
19 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
20 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
22 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
23 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
24 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
25 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
43 import com.google.common.collect.Lists;
44 import org.osgi.framework.BundleContext;
45 import org.osgi.framework.ServiceReference;
47 public class ClassifierService extends AbstractServiceInstance implements ClassifierProvider, ConfigInterface {
48 public final static long REG_VALUE_FROM_LOCAL = 0x1L;
49 public final static long REG_VALUE_FROM_REMOTE = 0x2L;
50 public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg0.class;
52 public ClassifierService() {
53 super(Service.CLASSIFIER);
56 public ClassifierService(Service service) {
61 * (Table:Classifier) Egress VM Traffic Towards TEP
62 * Match: Destination Ethernet Addr and OpenFlow InPort
63 * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
64 * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
65 * actions=set_field:5->tun_id,goto_table=<next-table>"
69 public void programLocalInPort(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
70 String nodeName = OPENFLOW + dpidLong;
72 MatchBuilder matchBuilder = new MatchBuilder();
73 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
74 FlowBuilder flowBuilder = new FlowBuilder();
76 // Create the OF Match using MatchBuilder
77 flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
78 // TODO Broken In_Port Match
79 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
81 String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
82 // Add Flow Attributes
83 flowBuilder.setId(new FlowId(flowId));
84 FlowKey key = new FlowKey(new FlowId(flowId));
85 flowBuilder.setStrict(true);
86 flowBuilder.setBarrier(false);
87 flowBuilder.setTableId(getTable());
88 flowBuilder.setKey(key);
89 flowBuilder.setFlowName(flowId);
90 flowBuilder.setHardTimeout(0);
91 flowBuilder.setIdleTimeout(0);
94 // Instantiate the Builders for the OF Actions and Instructions
95 InstructionBuilder ib = new InstructionBuilder();
96 InstructionsBuilder isb = new InstructionsBuilder();
98 // Instructions List Stores Individual Instructions
99 List<Instruction> instructions = Lists.newArrayList();
101 // TODO Broken SetTunID
102 InstructionUtils.createSetTunnelIdInstructions(ib, new BigInteger(segmentationId));
103 ApplyActionsCase aac = (ApplyActionsCase) ib.getInstruction();
104 List<Action> actionList = aac.getApplyActions().getAction();
106 ActionBuilder ab = new ActionBuilder();
107 ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
108 BigInteger.valueOf(REG_VALUE_FROM_LOCAL)));
110 ab.setKey(new ActionKey(1));
111 actionList.add(ab.build());
114 ib.setKey(new InstructionKey(0));
115 instructions.add(ib.build());
117 // Next service GOTO Instructions Need to be appended to the List
118 ib = this.getMutablePipelineInstructionBuilder();
120 ib.setKey(new InstructionKey(1));
121 instructions.add(ib.build());
123 // Add InstructionBuilder to the Instruction(s)Builder List
124 isb.setInstruction(instructions);
126 // Add InstructionsBuilder to FlowBuilder
127 flowBuilder.setInstructions(isb.build());
129 writeFlow(flowBuilder, nodeBuilder);
131 removeFlow(flowBuilder, nodeBuilder);
136 * (Table:Classifier) Egress VM Traffic Towards TEP
137 * Match: Source Ethernet Addr and OpenFlow InPort
138 * Instruction: Set VLANID and GOTO Table Egress (n)
139 * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
140 * actions=push_vlan, set_field:5->vlan_id,goto_table=<Next-Table>"
141 * 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
145 public void programLocalInPortSetVlan(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
147 String nodeName = OPENFLOW + dpidLong;
149 MatchBuilder matchBuilder = new MatchBuilder();
150 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
151 FlowBuilder flowBuilder = new FlowBuilder();
153 // Create the OF Match using MatchBuilder
154 flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
155 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
156 /* openflowplugin requires a vlan match to add a vlan */
157 flowBuilder.setMatch(MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(0), false).build());
159 String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
160 // Add Flow Attributes
161 flowBuilder.setId(new FlowId(flowId));
162 FlowKey key = new FlowKey(new FlowId(flowId));
163 flowBuilder.setStrict(true);
164 flowBuilder.setBarrier(false);
165 flowBuilder.setTableId(getTable());
166 flowBuilder.setKey(key);
167 flowBuilder.setFlowName(flowId);
168 flowBuilder.setHardTimeout(0);
169 flowBuilder.setIdleTimeout(0);
172 // Instantiate the Builders for the OF Actions and Instructions
173 InstructionBuilder ib = new InstructionBuilder();
174 InstructionsBuilder isb = new InstructionsBuilder();
176 // Instructions List Stores Individual Instructions
177 List<Instruction> instructions = Lists.newArrayList();
179 // Set VLAN ID Instruction
180 InstructionUtils.createSetVlanInstructions(ib, new VlanId(Integer.valueOf(segmentationId)));
182 ib.setKey(new InstructionKey(0));
183 instructions.add(ib.build());
185 // Next service GOTO Instructions Need to be appended to the List
186 ib = this.getMutablePipelineInstructionBuilder();
188 ib.setKey(new InstructionKey(1));
189 instructions.add(ib.build());
191 // Add InstructionBuilder to the Instruction(s)Builder List
192 isb.setInstruction(instructions);
194 // Add InstructionsBuilder to FlowBuilder
195 flowBuilder.setInstructions(isb.build());
197 writeFlow(flowBuilder, nodeBuilder);
199 removeFlow(flowBuilder, nodeBuilder);
204 * (Table:Classifier) Drop frames source from a VM that do not
205 * match the associated MAC address of the local VM.
206 * Match: Low priority anything not matching the VM SMAC
208 * table=0,priority=16384,in_port=1 actions=drop"
212 public void programDropSrcIface(Long dpidLong, Long inPort, boolean write) {
214 String nodeName = OPENFLOW + dpidLong;
216 MatchBuilder matchBuilder = new MatchBuilder();
217 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
218 FlowBuilder flowBuilder = new FlowBuilder();
220 // Create the OF Match using MatchBuilder
221 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
224 // Instantiate the Builders for the OF Actions and Instructions
225 InstructionBuilder ib = new InstructionBuilder();
226 InstructionsBuilder isb = new InstructionsBuilder();
228 // Instructions List Stores Individual Instructions
229 List<Instruction> instructions = Lists.newArrayList();
231 // Call the InstructionBuilder Methods Containing Actions
232 InstructionUtils.createDropInstructions(ib);
234 ib.setKey(new InstructionKey(0));
235 instructions.add(ib.build());
237 // Add InstructionBuilder to the Instruction(s)Builder List
238 isb.setInstruction(instructions);
240 // Add InstructionsBuilder to FlowBuilder
241 flowBuilder.setInstructions(isb.build());
244 String flowId = "DropFilter_"+inPort;
245 // Add Flow Attributes
246 flowBuilder.setId(new FlowId(flowId));
247 FlowKey key = new FlowKey(new FlowId(flowId));
248 flowBuilder.setStrict(true);
249 flowBuilder.setBarrier(false);
250 flowBuilder.setTableId(getTable());
251 flowBuilder.setKey(key);
252 flowBuilder.setFlowName(flowId);
253 flowBuilder.setPriority(8192);
254 flowBuilder.setHardTimeout(0);
255 flowBuilder.setIdleTimeout(0);
257 writeFlow(flowBuilder, nodeBuilder);
259 removeFlow(flowBuilder, nodeBuilder);
263 * (Table:0) Ingress Tunnel Traffic
264 * Match: OpenFlow InPort and Tunnel ID
265 * Action: GOTO Local Table (10)
266 * table=0,tun_id=0x5,in_port=10, actions=goto_table:2
270 public void programTunnelIn(Long dpidLong, String segmentationId,
271 Long ofPort, boolean write) {
273 String nodeName = OPENFLOW + dpidLong;
275 BigInteger tunnelId = new BigInteger(segmentationId);
276 MatchBuilder matchBuilder = new MatchBuilder();
277 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
278 FlowBuilder flowBuilder = new FlowBuilder();
280 // Create Match(es) and Set them in the FlowBuilder Object
281 flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, tunnelId).build());
282 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ofPort).build());
285 // Create the OF Actions and Instructions
286 InstructionBuilder ib = new InstructionBuilder();
287 InstructionsBuilder isb = new InstructionsBuilder();
289 // Instructions List Stores Individual Instructions
290 List<Instruction> instructions = Lists.newArrayList();
292 List<Action> actionList = Lists.newArrayList();
293 ActionBuilder ab = new ActionBuilder();
294 ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
295 BigInteger.valueOf(REG_VALUE_FROM_REMOTE)));
297 ab.setKey(new ActionKey(0));
298 actionList.add(ab.build());
300 ApplyActionsBuilder aab = new ApplyActionsBuilder();
301 aab.setAction(actionList);
302 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
304 // Call the InstructionBuilder Methods Containing Actions
306 ib.setKey(new InstructionKey(0));
307 instructions.add(ib.build());
309 // Append the default pipeline after the first classification
310 ib = this.getMutablePipelineInstructionBuilder();
312 ib.setKey(new InstructionKey(1));
313 instructions.add(ib.build());
315 // Add InstructionBuilder to the Instruction(s)Builder List
316 isb.setInstruction(instructions);
318 // Add InstructionsBuilder to FlowBuilder
319 flowBuilder.setInstructions(isb.build());
322 String flowId = "TunnelIn_"+segmentationId+"_"+ofPort;
323 // Add Flow Attributes
324 flowBuilder.setId(new FlowId(flowId));
325 FlowKey key = new FlowKey(new FlowId(flowId));
326 flowBuilder.setStrict(true);
327 flowBuilder.setBarrier(false);
328 flowBuilder.setTableId(getTable());
329 flowBuilder.setKey(key);
330 flowBuilder.setFlowName(flowId);
331 flowBuilder.setHardTimeout(0);
332 flowBuilder.setIdleTimeout(0);
335 writeFlow(flowBuilder, nodeBuilder);
337 removeFlow(flowBuilder, nodeBuilder);
342 * (Table:0) Ingress VLAN Traffic
343 * Match: OpenFlow InPort and vlan ID
344 * Action: GOTO Local Table (20)
345 * table=0,vlan_id=0x5,in_port=10, actions=goto_table:2
346 * table=0,in_port=2,dl_vlan=2001 actions=goto_table:20
350 public void programVlanIn(Long dpidLong, String segmentationId, Long ethPort, boolean write) {
352 String nodeName = OPENFLOW + dpidLong;
354 MatchBuilder matchBuilder = new MatchBuilder();
355 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
356 FlowBuilder flowBuilder = new FlowBuilder();
358 // Create Match(es) and Set them in the FlowBuilder Object
359 flowBuilder.setMatch(MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
360 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ethPort).build());
363 // Create the OF Actions and Instructions
364 InstructionsBuilder isb = new InstructionsBuilder();
366 // Instructions List Stores Individual Instructions
367 List<Instruction> instructions = Lists.newArrayList();
369 // Append the default pipeline after the first classification
370 InstructionBuilder ib = this.getMutablePipelineInstructionBuilder();
372 ib.setKey(new InstructionKey(0));
373 instructions.add(ib.build());
375 // Add InstructionBuilder to the Instruction(s)Builder List
376 isb.setInstruction(instructions);
378 // Add InstructionsBuilder to FlowBuilder
379 flowBuilder.setInstructions(isb.build());
382 String flowId = "VlanIn_"+segmentationId+"_"+ethPort;
383 // Add Flow Attributes
384 flowBuilder.setId(new FlowId(flowId));
385 FlowKey key = new FlowKey(new FlowId(flowId));
386 flowBuilder.setStrict(true);
387 flowBuilder.setBarrier(false);
388 flowBuilder.setTableId(getTable());
389 flowBuilder.setKey(key);
390 flowBuilder.setFlowName(flowId);
391 flowBuilder.setHardTimeout(0);
392 flowBuilder.setIdleTimeout(0);
394 writeFlow(flowBuilder, nodeBuilder);
396 removeFlow(flowBuilder, nodeBuilder);
401 * Create an LLDP Flow Rule to encapsulate into
402 * a packet_in that is sent to the controller
403 * for topology handling.
404 * Match: Ethertype 0x88CCL
405 * Action: Punt to Controller in a Packet_In msg
409 public void programLLDPPuntRule(Long dpidLong) {
411 String nodeName = OPENFLOW + dpidLong;
412 EtherType etherType = new EtherType(0x88CCL);
414 MatchBuilder matchBuilder = new MatchBuilder();
415 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
416 FlowBuilder flowBuilder = new FlowBuilder();
418 // Create Match(es) and Set them in the FlowBuilder Object
419 flowBuilder.setMatch(MatchUtils.createEtherTypeMatch(matchBuilder, etherType).build());
421 // Create the OF Actions and Instructions
422 InstructionBuilder ib = new InstructionBuilder();
423 InstructionsBuilder isb = new InstructionsBuilder();
425 // Instructions List Stores Individual Instructions
426 List<Instruction> instructions = Lists.newArrayList();
428 // Call the InstructionBuilder Methods Containing Actions
429 InstructionUtils.createSendToControllerInstructions(nodeName, ib);
431 ib.setKey(new InstructionKey(0));
432 instructions.add(ib.build());
434 // Add InstructionBuilder to the Instruction(s)Builder List
435 isb.setInstruction(instructions);
437 // Add InstructionsBuilder to FlowBuilder
438 flowBuilder.setInstructions(isb.build());
440 String flowId = "LLDP";
441 flowBuilder.setId(new FlowId(flowId));
442 FlowKey key = new FlowKey(new FlowId(flowId));
443 flowBuilder.setBarrier(true);
444 flowBuilder.setTableId(getTable());
445 flowBuilder.setKey(key);
446 flowBuilder.setFlowName(flowId);
447 flowBuilder.setHardTimeout(0);
448 flowBuilder.setIdleTimeout(0);
449 writeFlow(flowBuilder, nodeBuilder);
453 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
454 super.setDependencies(bundleContext.getServiceReference(ClassifierProvider.class.getName()), this);
458 public void setDependencies(Object impl) {}