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>"
139 * 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
143 public void programLocalInPortSetVlan(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
145 String nodeName = OPENFLOW + dpidLong;
147 MatchBuilder matchBuilder = new MatchBuilder();
148 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
149 FlowBuilder flowBuilder = new FlowBuilder();
151 // Create the OF Match using MatchBuilder
152 flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
153 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
154 /* openflowplugin requires a vlan match to add a vlan */
155 flowBuilder.setMatch(MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(0), false).build());
157 String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
158 // Add Flow Attributes
159 flowBuilder.setId(new FlowId(flowId));
160 FlowKey key = new FlowKey(new FlowId(flowId));
161 flowBuilder.setStrict(true);
162 flowBuilder.setBarrier(false);
163 flowBuilder.setTableId(getTable());
164 flowBuilder.setKey(key);
165 flowBuilder.setFlowName(flowId);
166 flowBuilder.setHardTimeout(0);
167 flowBuilder.setIdleTimeout(0);
170 // Instantiate the Builders for the OF Actions and Instructions
171 InstructionBuilder ib = new InstructionBuilder();
172 InstructionsBuilder isb = new InstructionsBuilder();
174 // Instructions List Stores Individual Instructions
175 List<Instruction> instructions = Lists.newArrayList();
177 // Set VLAN ID Instruction
178 InstructionUtils.createSetVlanInstructions(ib, new VlanId(Integer.valueOf(segmentationId)));
180 ib.setKey(new InstructionKey(0));
181 instructions.add(ib.build());
183 // Next service GOTO Instructions Need to be appended to the List
184 ib = this.getMutablePipelineInstructionBuilder();
186 ib.setKey(new InstructionKey(1));
187 instructions.add(ib.build());
189 // Add InstructionBuilder to the Instruction(s)Builder List
190 isb.setInstruction(instructions);
192 // Add InstructionsBuilder to FlowBuilder
193 flowBuilder.setInstructions(isb.build());
195 writeFlow(flowBuilder, nodeBuilder);
197 removeFlow(flowBuilder, nodeBuilder);
202 * (Table:Classifier) Drop frames source from a VM that do not
203 * match the associated MAC address of the local VM.
204 * Match: Low priority anything not matching the VM SMAC
206 * table=0,priority=16384,in_port=1 actions=drop"
210 public void programDropSrcIface(Long dpidLong, Long inPort, boolean write) {
212 String nodeName = OPENFLOW + dpidLong;
214 MatchBuilder matchBuilder = new MatchBuilder();
215 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
216 FlowBuilder flowBuilder = new FlowBuilder();
218 // Create the OF Match using MatchBuilder
219 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
222 // Instantiate the Builders for the OF Actions and Instructions
223 InstructionBuilder ib = new InstructionBuilder();
224 InstructionsBuilder isb = new InstructionsBuilder();
226 // Instructions List Stores Individual Instructions
227 List<Instruction> instructions = Lists.newArrayList();
229 // Call the InstructionBuilder Methods Containing Actions
230 InstructionUtils.createDropInstructions(ib);
232 ib.setKey(new InstructionKey(0));
233 instructions.add(ib.build());
235 // Add InstructionBuilder to the Instruction(s)Builder List
236 isb.setInstruction(instructions);
238 // Add InstructionsBuilder to FlowBuilder
239 flowBuilder.setInstructions(isb.build());
242 String flowId = "DropFilter_"+inPort;
243 // Add Flow Attributes
244 flowBuilder.setId(new FlowId(flowId));
245 FlowKey key = new FlowKey(new FlowId(flowId));
246 flowBuilder.setStrict(true);
247 flowBuilder.setBarrier(false);
248 flowBuilder.setTableId(getTable());
249 flowBuilder.setKey(key);
250 flowBuilder.setFlowName(flowId);
251 flowBuilder.setPriority(8192);
252 flowBuilder.setHardTimeout(0);
253 flowBuilder.setIdleTimeout(0);
255 writeFlow(flowBuilder, nodeBuilder);
257 removeFlow(flowBuilder, nodeBuilder);
261 * (Table:0) Ingress Tunnel Traffic
262 * Match: OpenFlow InPort and Tunnel ID
263 * Action: GOTO Local Table (10)
264 * table=0,tun_id=0x5,in_port=10, actions=goto_table:2
268 public void programTunnelIn(Long dpidLong, String segmentationId,
269 Long ofPort, boolean write) {
271 String nodeName = OPENFLOW + dpidLong;
273 BigInteger tunnelId = new BigInteger(segmentationId);
274 MatchBuilder matchBuilder = new MatchBuilder();
275 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
276 FlowBuilder flowBuilder = new FlowBuilder();
278 // Create Match(es) and Set them in the FlowBuilder Object
279 flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, tunnelId).build());
280 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ofPort).build());
283 // Create the OF Actions and Instructions
284 InstructionBuilder ib = new InstructionBuilder();
285 InstructionsBuilder isb = new InstructionsBuilder();
287 // Instructions List Stores Individual Instructions
288 List<Instruction> instructions = Lists.newArrayList();
290 List<Action> actionList = Lists.newArrayList();
291 ActionBuilder ab = new ActionBuilder();
292 ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
293 BigInteger.valueOf(REG_VALUE_FROM_REMOTE)));
295 ab.setKey(new ActionKey(0));
296 actionList.add(ab.build());
298 ApplyActionsBuilder aab = new ApplyActionsBuilder();
299 aab.setAction(actionList);
300 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
302 // Call the InstructionBuilder Methods Containing Actions
304 ib.setKey(new InstructionKey(0));
305 instructions.add(ib.build());
307 // Append the default pipeline after the first classification
308 ib = this.getMutablePipelineInstructionBuilder();
310 ib.setKey(new InstructionKey(1));
311 instructions.add(ib.build());
313 // Add InstructionBuilder to the Instruction(s)Builder List
314 isb.setInstruction(instructions);
316 // Add InstructionsBuilder to FlowBuilder
317 flowBuilder.setInstructions(isb.build());
320 String flowId = "TunnelIn_"+segmentationId+"_"+ofPort;
321 // Add Flow Attributes
322 flowBuilder.setId(new FlowId(flowId));
323 FlowKey key = new FlowKey(new FlowId(flowId));
324 flowBuilder.setStrict(true);
325 flowBuilder.setBarrier(false);
326 flowBuilder.setTableId(getTable());
327 flowBuilder.setKey(key);
328 flowBuilder.setFlowName(flowId);
329 flowBuilder.setHardTimeout(0);
330 flowBuilder.setIdleTimeout(0);
333 writeFlow(flowBuilder, nodeBuilder);
335 removeFlow(flowBuilder, nodeBuilder);
340 * (Table:0) Ingress VLAN Traffic
341 * Match: OpenFlow InPort and vlan ID
342 * Action: GOTO Local Table (20)
343 * table=0,vlan_id=0x5,in_port=10, actions=goto_table:2
344 * table=0,in_port=2,dl_vlan=2001 actions=goto_table:20
348 public void programVlanIn(Long dpidLong, String segmentationId, Long ethPort, boolean write) {
350 String nodeName = OPENFLOW + dpidLong;
352 MatchBuilder matchBuilder = new MatchBuilder();
353 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
354 FlowBuilder flowBuilder = new FlowBuilder();
356 // Create Match(es) and Set them in the FlowBuilder Object
357 flowBuilder.setMatch(MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
358 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ethPort).build());
361 // Create the OF Actions and Instructions
362 InstructionBuilder ib = new InstructionBuilder();
363 InstructionsBuilder isb = new InstructionsBuilder();
365 // Instructions List Stores Individual Instructions
366 List<Instruction> instructions = Lists.newArrayList();
368 // Append the default pipeline after the first classification
369 ib = this.getMutablePipelineInstructionBuilder();
371 ib.setKey(new InstructionKey(0));
372 instructions.add(ib.build());
374 // Add InstructionBuilder to the Instruction(s)Builder List
375 isb.setInstruction(instructions);
377 // Add InstructionsBuilder to FlowBuilder
378 flowBuilder.setInstructions(isb.build());
381 String flowId = "VlanIn_"+segmentationId+"_"+ethPort;
382 // Add Flow Attributes
383 flowBuilder.setId(new FlowId(flowId));
384 FlowKey key = new FlowKey(new FlowId(flowId));
385 flowBuilder.setStrict(true);
386 flowBuilder.setBarrier(false);
387 flowBuilder.setTableId(getTable());
388 flowBuilder.setKey(key);
389 flowBuilder.setFlowName(flowId);
390 flowBuilder.setHardTimeout(0);
391 flowBuilder.setIdleTimeout(0);
393 writeFlow(flowBuilder, nodeBuilder);
395 removeFlow(flowBuilder, nodeBuilder);
400 * Create an LLDP Flow Rule to encapsulate into
401 * a packet_in that is sent to the controller
402 * for topology handling.
403 * Match: Ethertype 0x88CCL
404 * Action: Punt to Controller in a Packet_In msg
408 public void programLLDPPuntRule(Long dpidLong) {
410 String nodeName = OPENFLOW + dpidLong;
411 EtherType etherType = new EtherType(0x88CCL);
413 MatchBuilder matchBuilder = new MatchBuilder();
414 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
415 FlowBuilder flowBuilder = new FlowBuilder();
417 // Create Match(es) and Set them in the FlowBuilder Object
418 flowBuilder.setMatch(MatchUtils.createEtherTypeMatch(matchBuilder, etherType).build());
420 // Create the OF Actions and Instructions
421 InstructionBuilder ib = new InstructionBuilder();
422 InstructionsBuilder isb = new InstructionsBuilder();
424 // Instructions List Stores Individual Instructions
425 List<Instruction> instructions = Lists.newArrayList();
427 // Call the InstructionBuilder Methods Containing Actions
428 InstructionUtils.createSendToControllerInstructions(ib);
430 ib.setKey(new InstructionKey(0));
431 instructions.add(ib.build());
433 // Add InstructionBuilder to the Instruction(s)Builder List
434 isb.setInstruction(instructions);
436 // Add InstructionsBuilder to FlowBuilder
437 flowBuilder.setInstructions(isb.build());
439 String flowId = "LLDP";
440 flowBuilder.setId(new FlowId(flowId));
441 FlowKey key = new FlowKey(new FlowId(flowId));
442 flowBuilder.setBarrier(true);
443 flowBuilder.setTableId(getTable());
444 flowBuilder.setKey(key);
445 flowBuilder.setFlowName(flowId);
446 flowBuilder.setHardTimeout(0);
447 flowBuilder.setIdleTimeout(0);
448 writeFlow(flowBuilder, nodeBuilder);