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 * (Table:Classifier) Egress VM Traffic Towards TEP
60 * Match: Destination Ethernet Addr and OpenFlow InPort
61 * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
62 * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
63 * actions=set_field:5->tun_id,goto_table=<next-table>"
67 public void programLocalInPort(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
68 String nodeName = OPENFLOW + dpidLong;
70 MatchBuilder matchBuilder = new MatchBuilder();
71 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
72 FlowBuilder flowBuilder = new FlowBuilder();
74 // Create the OF Match using MatchBuilder
75 flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
76 // TODO Broken In_Port Match
77 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
79 String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
80 // Add Flow Attributes
81 flowBuilder.setId(new FlowId(flowId));
82 FlowKey key = new FlowKey(new FlowId(flowId));
83 flowBuilder.setStrict(true);
84 flowBuilder.setBarrier(false);
85 flowBuilder.setTableId(getTable());
86 flowBuilder.setKey(key);
87 flowBuilder.setFlowName(flowId);
88 flowBuilder.setHardTimeout(0);
89 flowBuilder.setIdleTimeout(0);
92 // Instantiate the Builders for the OF Actions and Instructions
93 InstructionBuilder ib = new InstructionBuilder();
94 InstructionsBuilder isb = new InstructionsBuilder();
96 // Instructions List Stores Individual Instructions
97 List<Instruction> instructions = Lists.newArrayList();
99 // TODO Broken SetTunID
100 InstructionUtils.createSetTunnelIdInstructions(ib, new BigInteger(segmentationId));
101 ApplyActionsCase aac = (ApplyActionsCase) ib.getInstruction();
102 List<Action> actionList = aac.getApplyActions().getAction();
104 ActionBuilder ab = new ActionBuilder();
105 ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
106 BigInteger.valueOf(REG_VALUE_FROM_LOCAL)));
108 ab.setKey(new ActionKey(1));
109 actionList.add(ab.build());
112 ib.setKey(new InstructionKey(0));
113 instructions.add(ib.build());
115 // Next service GOTO Instructions Need to be appended to the List
116 ib = this.getMutablePipelineInstructionBuilder();
118 ib.setKey(new InstructionKey(1));
119 instructions.add(ib.build());
121 // Add InstructionBuilder to the Instruction(s)Builder List
122 isb.setInstruction(instructions);
124 // Add InstructionsBuilder to FlowBuilder
125 flowBuilder.setInstructions(isb.build());
127 writeFlow(flowBuilder, nodeBuilder);
129 removeFlow(flowBuilder, nodeBuilder);
134 * (Table:Classifier) Egress VM Traffic Towards TEP
135 * Match: Source Ethernet Addr and OpenFlow InPort
136 * Instruction: Set VLANID and GOTO Table Egress (n)
137 * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
138 * actions=push_vlan, set_field:5->vlan_id,goto_table=<Next-Table>"
142 public void programLocalInPortSetVlan(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
144 String nodeName = OPENFLOW + dpidLong;
146 MatchBuilder matchBuilder = new MatchBuilder();
147 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
148 FlowBuilder flowBuilder = new FlowBuilder();
150 // Create the OF Match using MatchBuilder
151 flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
152 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
154 String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
155 // Add Flow Attributes
156 flowBuilder.setId(new FlowId(flowId));
157 FlowKey key = new FlowKey(new FlowId(flowId));
158 flowBuilder.setStrict(true);
159 flowBuilder.setBarrier(false);
160 flowBuilder.setTableId(getTable());
161 flowBuilder.setKey(key);
162 flowBuilder.setFlowName(flowId);
163 flowBuilder.setHardTimeout(0);
164 flowBuilder.setIdleTimeout(0);
167 // Instantiate the Builders for the OF Actions and Instructions
168 InstructionBuilder ib = new InstructionBuilder();
169 InstructionsBuilder isb = new InstructionsBuilder();
171 // Instructions List Stores Individual Instructions
172 List<Instruction> instructions = Lists.newArrayList();
174 // Set VLAN ID Instruction
175 InstructionUtils.createSetVlanInstructions(ib, new VlanId(Integer.valueOf(segmentationId)));
177 ib.setKey(new InstructionKey(0));
178 instructions.add(ib.build());
180 // Next service GOTO Instructions Need to be appended to the List
181 ib = this.getMutablePipelineInstructionBuilder();
183 ib.setKey(new InstructionKey(1));
184 instructions.add(ib.build());
186 // Add InstructionBuilder to the Instruction(s)Builder List
187 isb.setInstruction(instructions);
189 // Add InstructionsBuilder to FlowBuilder
190 flowBuilder.setInstructions(isb.build());
192 writeFlow(flowBuilder, nodeBuilder);
194 removeFlow(flowBuilder, nodeBuilder);
199 * (Table:Classifier) Drop frames source from a VM that do not
200 * match the associated MAC address of the local VM.
201 * Match: Low priority anything not matching the VM SMAC
203 * table=0,priority=16384,in_port=1 actions=drop"
207 public void programDropSrcIface(Long dpidLong, Long inPort, boolean write) {
209 String nodeName = OPENFLOW + dpidLong;
211 MatchBuilder matchBuilder = new MatchBuilder();
212 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
213 FlowBuilder flowBuilder = new FlowBuilder();
215 // Create the OF Match using MatchBuilder
216 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
219 // Instantiate the Builders for the OF Actions and Instructions
220 InstructionBuilder ib = new InstructionBuilder();
221 InstructionsBuilder isb = new InstructionsBuilder();
223 // Instructions List Stores Individual Instructions
224 List<Instruction> instructions = Lists.newArrayList();
226 // Call the InstructionBuilder Methods Containing Actions
227 InstructionUtils.createDropInstructions(ib);
229 ib.setKey(new InstructionKey(0));
230 instructions.add(ib.build());
232 // Add InstructionBuilder to the Instruction(s)Builder List
233 isb.setInstruction(instructions);
235 // Add InstructionsBuilder to FlowBuilder
236 flowBuilder.setInstructions(isb.build());
239 String flowId = "DropFilter_"+inPort;
240 // Add Flow Attributes
241 flowBuilder.setId(new FlowId(flowId));
242 FlowKey key = new FlowKey(new FlowId(flowId));
243 flowBuilder.setStrict(true);
244 flowBuilder.setBarrier(false);
245 flowBuilder.setTableId(getTable());
246 flowBuilder.setKey(key);
247 flowBuilder.setFlowName(flowId);
248 flowBuilder.setPriority(8192);
249 flowBuilder.setHardTimeout(0);
250 flowBuilder.setIdleTimeout(0);
252 writeFlow(flowBuilder, nodeBuilder);
254 removeFlow(flowBuilder, nodeBuilder);
258 * (Table:0) Ingress Tunnel Traffic
259 * Match: OpenFlow InPort and Tunnel ID
260 * Action: GOTO Local Table (10)
261 * table=0,tun_id=0x5,in_port=10, actions=goto_table:2
265 public void programTunnelIn(Long dpidLong, String segmentationId,
266 Long ofPort, boolean write) {
268 String nodeName = OPENFLOW + dpidLong;
270 BigInteger tunnelId = new BigInteger(segmentationId);
271 MatchBuilder matchBuilder = new MatchBuilder();
272 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
273 FlowBuilder flowBuilder = new FlowBuilder();
275 // Create Match(es) and Set them in the FlowBuilder Object
276 flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, tunnelId).build());
277 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ofPort).build());
280 // Create the OF Actions and Instructions
281 InstructionBuilder ib = new InstructionBuilder();
282 InstructionsBuilder isb = new InstructionsBuilder();
284 // Instructions List Stores Individual Instructions
285 List<Instruction> instructions = Lists.newArrayList();
287 List<Action> actionList = Lists.newArrayList();
288 ActionBuilder ab = new ActionBuilder();
289 ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
290 BigInteger.valueOf(REG_VALUE_FROM_REMOTE)));
292 ab.setKey(new ActionKey(0));
293 actionList.add(ab.build());
295 ApplyActionsBuilder aab = new ApplyActionsBuilder();
296 aab.setAction(actionList);
297 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
299 // Call the InstructionBuilder Methods Containing Actions
301 ib.setKey(new InstructionKey(0));
302 instructions.add(ib.build());
304 // Append the default pipeline after the first classification
305 ib = this.getMutablePipelineInstructionBuilder();
307 ib.setKey(new InstructionKey(1));
308 instructions.add(ib.build());
310 // Add InstructionBuilder to the Instruction(s)Builder List
311 isb.setInstruction(instructions);
313 // Add InstructionsBuilder to FlowBuilder
314 flowBuilder.setInstructions(isb.build());
317 String flowId = "TunnelIn_"+segmentationId+"_"+ofPort;
318 // Add Flow Attributes
319 flowBuilder.setId(new FlowId(flowId));
320 FlowKey key = new FlowKey(new FlowId(flowId));
321 flowBuilder.setStrict(true);
322 flowBuilder.setBarrier(false);
323 flowBuilder.setTableId(getTable());
324 flowBuilder.setKey(key);
325 flowBuilder.setFlowName(flowId);
326 flowBuilder.setHardTimeout(0);
327 flowBuilder.setIdleTimeout(0);
330 writeFlow(flowBuilder, nodeBuilder);
332 removeFlow(flowBuilder, nodeBuilder);
337 * (Table:0) Ingress VLAN Traffic
338 * Match: OpenFlow InPort and vlan ID
339 * Action: GOTO Local Table (20)
340 * table=0,vlan_id=0x5,in_port=10, actions=goto_table:2
344 public void programVlanIn(Long dpidLong, String segmentationId, Long ethPort, boolean write) {
346 String nodeName = OPENFLOW + dpidLong;
348 MatchBuilder matchBuilder = new MatchBuilder();
349 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
350 FlowBuilder flowBuilder = new FlowBuilder();
352 // Create Match(es) and Set them in the FlowBuilder Object
353 flowBuilder.setMatch(
354 MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true)
356 .setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ethPort)
360 // Create the OF Actions and Instructions
361 InstructionBuilder ib = new InstructionBuilder();
362 InstructionsBuilder isb = new InstructionsBuilder();
364 // Instructions List Stores Individual Instructions
365 List<Instruction> instructions = Lists.newArrayList();
367 // Call the InstructionBuilder Methods Containing Actions
368 ib = this.getMutablePipelineInstructionBuilder();
370 ib.setKey(new InstructionKey(0));
371 instructions.add(ib.build());
373 // Add InstructionBuilder to the Instruction(s)Builder List
374 isb.setInstruction(instructions);
376 // Add InstructionsBuilder to FlowBuilder
377 flowBuilder.setInstructions(isb.build());
380 String flowId = "VlanIn_"+segmentationId+"_"+ethPort;
381 // Add Flow Attributes
382 flowBuilder.setId(new FlowId(flowId));
383 FlowKey key = new FlowKey(new FlowId(flowId));
384 flowBuilder.setStrict(true);
385 flowBuilder.setBarrier(false);
386 flowBuilder.setTableId(getTable());
387 flowBuilder.setKey(key);
388 flowBuilder.setFlowName(flowId);
389 flowBuilder.setHardTimeout(0);
390 flowBuilder.setIdleTimeout(0);
392 writeFlow(flowBuilder, nodeBuilder);
394 removeFlow(flowBuilder, nodeBuilder);
399 * Create an LLDP Flow Rule to encapsulate into
400 * a packet_in that is sent to the controller
401 * for topology handling.
402 * Match: Ethertype 0x88CCL
403 * Action: Punt to Controller in a Packet_In msg
407 public void programLLDPPuntRule(Long dpidLong) {
409 String nodeName = OPENFLOW + dpidLong;
410 EtherType etherType = new EtherType(0x88CCL);
412 MatchBuilder matchBuilder = new MatchBuilder();
413 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
414 FlowBuilder flowBuilder = new FlowBuilder();
416 // Create Match(es) and Set them in the FlowBuilder Object
417 flowBuilder.setMatch(MatchUtils.createEtherTypeMatch(matchBuilder, etherType).build());
419 // Create the OF Actions and Instructions
420 InstructionBuilder ib = new InstructionBuilder();
421 InstructionsBuilder isb = new InstructionsBuilder();
423 // Instructions List Stores Individual Instructions
424 List<Instruction> instructions = Lists.newArrayList();
426 // Call the InstructionBuilder Methods Containing Actions
427 InstructionUtils.createSendToControllerInstructions(ib);
429 ib.setKey(new InstructionKey(0));
430 instructions.add(ib.build());
432 // Add InstructionBuilder to the Instruction(s)Builder List
433 isb.setInstruction(instructions);
435 // Add InstructionsBuilder to FlowBuilder
436 flowBuilder.setInstructions(isb.build());
438 String flowId = "LLDP";
439 flowBuilder.setId(new FlowId(flowId));
440 FlowKey key = new FlowKey(new FlowId(flowId));
441 flowBuilder.setBarrier(true);
442 flowBuilder.setTableId(getTable());
443 flowBuilder.setKey(key);
444 flowBuilder.setFlowName(flowId);
445 flowBuilder.setHardTimeout(0);
446 flowBuilder.setIdleTimeout(0);
447 writeFlow(flowBuilder, nodeBuilder);