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.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;
45 public class ClassifierService extends AbstractServiceInstance implements ClassifierProvider {
46 public final static long REG_VALUE_FROM_LOCAL = 0x1L;
47 public final static long REG_VALUE_FROM_REMOTE = 0x2L;
48 public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg0.class;
50 public ClassifierService() {
51 super(Service.CLASSIFIER);
54 public ClassifierService(Service service) {
59 public boolean isBridgeInPipeline (String nodeId) {
64 * (Table:Classifier) Egress VM Traffic Towards TEP
65 * Match: Destination Ethernet Addr and OpenFlow InPort
66 * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
67 * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
68 * actions=set_field:5->tun_id,goto_table=<next-table>"
72 public void programLocalInPort(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
73 String nodeName = OPENFLOW + dpidLong;
75 MatchBuilder matchBuilder = new MatchBuilder();
76 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
77 FlowBuilder flowBuilder = new FlowBuilder();
79 // Create the OF Match using MatchBuilder
80 flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
81 // TODO Broken In_Port Match
82 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
84 String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
85 // Add Flow Attributes
86 flowBuilder.setId(new FlowId(flowId));
87 FlowKey key = new FlowKey(new FlowId(flowId));
88 flowBuilder.setStrict(true);
89 flowBuilder.setBarrier(false);
90 flowBuilder.setTableId(getTable());
91 flowBuilder.setKey(key);
92 flowBuilder.setFlowName(flowId);
93 flowBuilder.setHardTimeout(0);
94 flowBuilder.setIdleTimeout(0);
97 // Instantiate the Builders for the OF Actions and Instructions
98 InstructionBuilder ib = new InstructionBuilder();
99 InstructionsBuilder isb = new InstructionsBuilder();
101 // Instructions List Stores Individual Instructions
102 List<Instruction> instructions = Lists.newArrayList();
104 // TODO Broken SetTunID
105 InstructionUtils.createSetTunnelIdInstructions(ib, new BigInteger(segmentationId));
106 ApplyActionsCase aac = (ApplyActionsCase) ib.getInstruction();
107 List<Action> actionList = aac.getApplyActions().getAction();
109 ActionBuilder ab = new ActionBuilder();
110 ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
111 BigInteger.valueOf(REG_VALUE_FROM_LOCAL)));
113 ab.setKey(new ActionKey(1));
114 actionList.add(ab.build());
117 ib.setKey(new InstructionKey(0));
118 instructions.add(ib.build());
120 // Next service GOTO Instructions Need to be appended to the List
121 ib = this.getMutablePipelineInstructionBuilder();
123 ib.setKey(new InstructionKey(1));
124 instructions.add(ib.build());
126 // Add InstructionBuilder to the Instruction(s)Builder List
127 isb.setInstruction(instructions);
129 // Add InstructionsBuilder to FlowBuilder
130 flowBuilder.setInstructions(isb.build());
132 writeFlow(flowBuilder, nodeBuilder);
134 removeFlow(flowBuilder, nodeBuilder);
139 * (Table:Classifier) Egress VM Traffic Towards TEP
140 * Match: Source Ethernet Addr and OpenFlow InPort
141 * Instruction: Set VLANID and GOTO Table Egress (n)
142 * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
143 * actions=push_vlan, set_field:5->vlan_id,goto_table=<Next-Table>"
147 public void programLocalInPortSetVlan(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
149 String nodeName = OPENFLOW + dpidLong;
151 MatchBuilder matchBuilder = new MatchBuilder();
152 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
153 FlowBuilder flowBuilder = new FlowBuilder();
155 // Create the OF Match using MatchBuilder
156 flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
157 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).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
349 public void programVlanIn(Long dpidLong, String segmentationId, Long ethPort, boolean write) {
351 String nodeName = OPENFLOW + dpidLong;
353 MatchBuilder matchBuilder = new MatchBuilder();
354 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
355 FlowBuilder flowBuilder = new FlowBuilder();
357 // Create Match(es) and Set them in the FlowBuilder Object
358 flowBuilder.setMatch(
359 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true)
361 .setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ethPort)
365 // Create the OF Actions and Instructions
366 InstructionBuilder ib = new InstructionBuilder();
367 InstructionsBuilder isb = new InstructionsBuilder();
369 // Instructions List Stores Individual Instructions
370 List<Instruction> instructions = Lists.newArrayList();
372 // Call the InstructionBuilder Methods Containing Actions
373 ib = this.getMutablePipelineInstructionBuilder();
375 ib.setKey(new InstructionKey(0));
376 instructions.add(ib.build());
378 // Add InstructionBuilder to the Instruction(s)Builder List
379 isb.setInstruction(instructions);
381 // Add InstructionsBuilder to FlowBuilder
382 flowBuilder.setInstructions(isb.build());
385 String flowId = "VlanIn_"+segmentationId+"_"+ethPort;
386 // Add Flow Attributes
387 flowBuilder.setId(new FlowId(flowId));
388 FlowKey key = new FlowKey(new FlowId(flowId));
389 flowBuilder.setStrict(true);
390 flowBuilder.setBarrier(false);
391 flowBuilder.setTableId(getTable());
392 flowBuilder.setKey(key);
393 flowBuilder.setFlowName(flowId);
394 flowBuilder.setHardTimeout(0);
395 flowBuilder.setIdleTimeout(0);
397 writeFlow(flowBuilder, nodeBuilder);
399 removeFlow(flowBuilder, nodeBuilder);
404 * Create an LLDP Flow Rule to encapsulate into
405 * a packet_in that is sent to the controller
406 * for topology handling.
407 * Match: Ethertype 0x88CCL
408 * Action: Punt to Controller in a Packet_In msg
412 public void programLLDPPuntRule(Long dpidLong) {
414 String nodeName = OPENFLOW + dpidLong;
415 EtherType etherType = new EtherType(0x88CCL);
417 MatchBuilder matchBuilder = new MatchBuilder();
418 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
419 FlowBuilder flowBuilder = new FlowBuilder();
421 // Create Match(es) and Set them in the FlowBuilder Object
422 flowBuilder.setMatch(MatchUtils.createEtherTypeMatch(matchBuilder, etherType).build());
424 // Create the OF Actions and Instructions
425 InstructionBuilder ib = new InstructionBuilder();
426 InstructionsBuilder isb = new InstructionsBuilder();
428 // Instructions List Stores Individual Instructions
429 List<Instruction> instructions = Lists.newArrayList();
431 // Call the InstructionBuilder Methods Containing Actions
432 InstructionUtils.createSendToControllerInstructions(ib);
434 ib.setKey(new InstructionKey(0));
435 instructions.add(ib.build());
437 // Add InstructionBuilder to the Instruction(s)Builder List
438 isb.setInstruction(instructions);
440 // Add InstructionsBuilder to FlowBuilder
441 flowBuilder.setInstructions(isb.build());
443 String flowId = "LLDP";
444 flowBuilder.setId(new FlowId(flowId));
445 FlowKey key = new FlowKey(new FlowId(flowId));
446 flowBuilder.setBarrier(true);
447 flowBuilder.setTableId(getTable());
448 flowBuilder.setKey(key);
449 flowBuilder.setFlowName(flowId);
450 flowBuilder.setHardTimeout(0);
451 flowBuilder.setIdleTimeout(0);
452 writeFlow(flowBuilder, nodeBuilder);