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;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class ClassifierService extends AbstractServiceInstance implements ClassifierProvider, ConfigInterface {
51 public final static long REG_VALUE_FROM_LOCAL = 0x1L;
52 public final static long REG_VALUE_FROM_REMOTE = 0x2L;
53 public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg0.class;
55 public ClassifierService() {
56 super(Service.CLASSIFIER);
59 public ClassifierService(Service service) {
63 private static final Logger logger = LoggerFactory.getLogger(ClassifierService.class);
66 * (Table:Classifier) Egress VM Traffic Towards TEP
67 * Match: Destination Ethernet Addr and OpenFlow InPort
68 * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
69 * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
70 * actions=set_field:5->tun_id,goto_table=<next-table>"
74 public void programLocalInPort(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
75 String nodeName = OPENFLOW + dpidLong;
77 MatchBuilder matchBuilder = new MatchBuilder();
78 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
79 FlowBuilder flowBuilder = new FlowBuilder();
81 // Create the OF Match using MatchBuilder
82 flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
83 // TODO Broken In_Port Match
84 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
86 String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
87 // Add Flow Attributes
88 flowBuilder.setId(new FlowId(flowId));
89 FlowKey key = new FlowKey(new FlowId(flowId));
90 flowBuilder.setStrict(true);
91 flowBuilder.setBarrier(false);
92 flowBuilder.setTableId(getTable());
93 flowBuilder.setKey(key);
94 flowBuilder.setFlowName(flowId);
95 flowBuilder.setHardTimeout(0);
96 flowBuilder.setIdleTimeout(0);
99 // Instantiate the Builders for the OF Actions and Instructions
100 InstructionBuilder ib = new InstructionBuilder();
101 InstructionsBuilder isb = new InstructionsBuilder();
103 // Instructions List Stores Individual Instructions
104 List<Instruction> instructions = Lists.newArrayList();
106 // TODO Broken SetTunID
107 InstructionUtils.createSetTunnelIdInstructions(ib, new BigInteger(segmentationId));
108 ApplyActionsCase aac = (ApplyActionsCase) ib.getInstruction();
109 List<Action> actionList = aac.getApplyActions().getAction();
111 ActionBuilder ab = new ActionBuilder();
112 ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
113 BigInteger.valueOf(REG_VALUE_FROM_LOCAL)));
115 ab.setKey(new ActionKey(1));
116 actionList.add(ab.build());
119 ib.setKey(new InstructionKey(0));
120 instructions.add(ib.build());
122 // Next service GOTO Instructions Need to be appended to the List
123 ib = this.getMutablePipelineInstructionBuilder();
125 ib.setKey(new InstructionKey(1));
126 instructions.add(ib.build());
128 // Add InstructionBuilder to the Instruction(s)Builder List
129 isb.setInstruction(instructions);
131 // Add InstructionsBuilder to FlowBuilder
132 flowBuilder.setInstructions(isb.build());
134 writeFlow(flowBuilder, nodeBuilder);
136 removeFlow(flowBuilder, nodeBuilder);
141 * (Table:Classifier) Egress VM Traffic Towards TEP
142 * Match: Source Ethernet Addr and OpenFlow InPort
143 * Instruction: Set VLANID and GOTO Table Egress (n)
144 * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
145 * actions=push_vlan, set_field:5->vlan_id,goto_table=<Next-Table>"
146 * 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
150 public void programLocalInPortSetVlan(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
152 String nodeName = OPENFLOW + dpidLong;
154 MatchBuilder matchBuilder = new MatchBuilder();
155 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
156 FlowBuilder flowBuilder = new FlowBuilder();
158 // Create the OF Match using MatchBuilder
159 flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
160 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
161 /* openflowplugin requires a vlan match to add a vlan */
162 flowBuilder.setMatch(MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(0), false).build());
164 String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
165 // Add Flow Attributes
166 flowBuilder.setId(new FlowId(flowId));
167 FlowKey key = new FlowKey(new FlowId(flowId));
168 flowBuilder.setStrict(true);
169 flowBuilder.setBarrier(false);
170 flowBuilder.setTableId(getTable());
171 flowBuilder.setKey(key);
172 flowBuilder.setFlowName(flowId);
173 flowBuilder.setHardTimeout(0);
174 flowBuilder.setIdleTimeout(0);
177 // Instantiate the Builders for the OF Actions and Instructions
178 InstructionBuilder ib = new InstructionBuilder();
179 InstructionsBuilder isb = new InstructionsBuilder();
181 // Instructions List Stores Individual Instructions
182 List<Instruction> instructions = Lists.newArrayList();
184 // Set VLAN ID Instruction
185 InstructionUtils.createSetVlanInstructions(ib, new VlanId(Integer.valueOf(segmentationId)));
187 ib.setKey(new InstructionKey(0));
188 instructions.add(ib.build());
190 // Next service GOTO Instructions Need to be appended to the List
191 ib = this.getMutablePipelineInstructionBuilder();
193 ib.setKey(new InstructionKey(1));
194 instructions.add(ib.build());
196 // Add InstructionBuilder to the Instruction(s)Builder List
197 isb.setInstruction(instructions);
199 // Add InstructionsBuilder to FlowBuilder
200 flowBuilder.setInstructions(isb.build());
202 writeFlow(flowBuilder, nodeBuilder);
204 removeFlow(flowBuilder, nodeBuilder);
209 * (Table:Classifier) Drop frames source from a VM that do not
210 * match the associated MAC address of the local VM.
211 * Match: Low priority anything not matching the VM SMAC
213 * table=0,priority=16384,in_port=1 actions=drop"
217 public void programDropSrcIface(Long dpidLong, Long inPort, boolean write) {
219 String nodeName = OPENFLOW + dpidLong;
221 MatchBuilder matchBuilder = new MatchBuilder();
222 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
223 FlowBuilder flowBuilder = new FlowBuilder();
225 // Create the OF Match using MatchBuilder
226 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
229 // Instantiate the Builders for the OF Actions and Instructions
230 InstructionBuilder ib = new InstructionBuilder();
231 InstructionsBuilder isb = new InstructionsBuilder();
233 // Instructions List Stores Individual Instructions
234 List<Instruction> instructions = Lists.newArrayList();
236 // Call the InstructionBuilder Methods Containing Actions
237 InstructionUtils.createDropInstructions(ib);
239 ib.setKey(new InstructionKey(0));
240 instructions.add(ib.build());
242 // Add InstructionBuilder to the Instruction(s)Builder List
243 isb.setInstruction(instructions);
245 // Add InstructionsBuilder to FlowBuilder
246 flowBuilder.setInstructions(isb.build());
249 String flowId = "DropFilter_"+inPort;
250 // Add Flow Attributes
251 flowBuilder.setId(new FlowId(flowId));
252 FlowKey key = new FlowKey(new FlowId(flowId));
253 flowBuilder.setStrict(true);
254 flowBuilder.setBarrier(false);
255 flowBuilder.setTableId(getTable());
256 flowBuilder.setKey(key);
257 flowBuilder.setFlowName(flowId);
258 flowBuilder.setPriority(8192);
259 flowBuilder.setHardTimeout(0);
260 flowBuilder.setIdleTimeout(0);
262 writeFlow(flowBuilder, nodeBuilder);
264 removeFlow(flowBuilder, nodeBuilder);
268 * (Table:0) Ingress Tunnel Traffic
269 * Match: OpenFlow InPort and Tunnel ID
270 * Action: GOTO Local Table (10)
271 * table=0,tun_id=0x5,in_port=10, actions=goto_table:2
275 public void programTunnelIn(Long dpidLong, String segmentationId,
276 Long ofPort, boolean write) {
278 String nodeName = OPENFLOW + dpidLong;
280 BigInteger tunnelId = new BigInteger(segmentationId);
281 MatchBuilder matchBuilder = new MatchBuilder();
282 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
283 FlowBuilder flowBuilder = new FlowBuilder();
285 // Create Match(es) and Set them in the FlowBuilder Object
286 flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, tunnelId).build());
287 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ofPort).build());
290 // Create the OF Actions and Instructions
291 InstructionBuilder ib = new InstructionBuilder();
292 InstructionsBuilder isb = new InstructionsBuilder();
294 // Instructions List Stores Individual Instructions
295 List<Instruction> instructions = Lists.newArrayList();
297 List<Action> actionList = Lists.newArrayList();
298 ActionBuilder ab = new ActionBuilder();
299 ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
300 BigInteger.valueOf(REG_VALUE_FROM_REMOTE)));
302 ab.setKey(new ActionKey(0));
303 actionList.add(ab.build());
305 ApplyActionsBuilder aab = new ApplyActionsBuilder();
306 aab.setAction(actionList);
307 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
309 // Call the InstructionBuilder Methods Containing Actions
311 ib.setKey(new InstructionKey(0));
312 instructions.add(ib.build());
314 // Append the default pipeline after the first classification
315 ib = this.getMutablePipelineInstructionBuilder();
317 ib.setKey(new InstructionKey(1));
318 instructions.add(ib.build());
320 // Add InstructionBuilder to the Instruction(s)Builder List
321 isb.setInstruction(instructions);
323 // Add InstructionsBuilder to FlowBuilder
324 flowBuilder.setInstructions(isb.build());
327 String flowId = "TunnelIn_"+segmentationId+"_"+ofPort;
328 // Add Flow Attributes
329 flowBuilder.setId(new FlowId(flowId));
330 FlowKey key = new FlowKey(new FlowId(flowId));
331 flowBuilder.setStrict(true);
332 flowBuilder.setBarrier(false);
333 flowBuilder.setTableId(getTable());
334 flowBuilder.setKey(key);
335 flowBuilder.setFlowName(flowId);
336 flowBuilder.setHardTimeout(0);
337 flowBuilder.setIdleTimeout(0);
340 writeFlow(flowBuilder, nodeBuilder);
342 removeFlow(flowBuilder, nodeBuilder);
347 * (Table:0) Ingress VLAN Traffic
348 * Match: OpenFlow InPort and vlan ID
349 * Action: GOTO Local Table (20)
350 * table=0,vlan_id=0x5,in_port=10, actions=goto_table:2
351 * table=0,in_port=2,dl_vlan=2001 actions=goto_table:20
355 public void programVlanIn(Long dpidLong, String segmentationId, Long ethPort, boolean write) {
357 String nodeName = OPENFLOW + dpidLong;
359 MatchBuilder matchBuilder = new MatchBuilder();
360 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
361 FlowBuilder flowBuilder = new FlowBuilder();
363 // Create Match(es) and Set them in the FlowBuilder Object
364 flowBuilder.setMatch(MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
365 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ethPort).build());
368 // Create the OF Actions and Instructions
369 InstructionBuilder ib = new InstructionBuilder();
370 InstructionsBuilder isb = new InstructionsBuilder();
372 // Instructions List Stores Individual Instructions
373 List<Instruction> instructions = Lists.newArrayList();
375 // Append the default pipeline after the first classification
376 ib = this.getMutablePipelineInstructionBuilder();
378 ib.setKey(new InstructionKey(0));
379 instructions.add(ib.build());
381 // Add InstructionBuilder to the Instruction(s)Builder List
382 isb.setInstruction(instructions);
384 // Add InstructionsBuilder to FlowBuilder
385 flowBuilder.setInstructions(isb.build());
388 String flowId = "VlanIn_"+segmentationId+"_"+ethPort;
389 // Add Flow Attributes
390 flowBuilder.setId(new FlowId(flowId));
391 FlowKey key = new FlowKey(new FlowId(flowId));
392 flowBuilder.setStrict(true);
393 flowBuilder.setBarrier(false);
394 flowBuilder.setTableId(getTable());
395 flowBuilder.setKey(key);
396 flowBuilder.setFlowName(flowId);
397 flowBuilder.setHardTimeout(0);
398 flowBuilder.setIdleTimeout(0);
400 writeFlow(flowBuilder, nodeBuilder);
402 removeFlow(flowBuilder, nodeBuilder);
407 * Create an LLDP Flow Rule to encapsulate into
408 * a packet_in that is sent to the controller
409 * for topology handling.
410 * Match: Ethertype 0x88CCL
411 * Action: Punt to Controller in a Packet_In msg
415 public void programLLDPPuntRule(Long dpidLong) {
417 String nodeName = OPENFLOW + dpidLong;
418 EtherType etherType = new EtherType(0x88CCL);
420 MatchBuilder matchBuilder = new MatchBuilder();
421 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
422 FlowBuilder flowBuilder = new FlowBuilder();
424 // Create Match(es) and Set them in the FlowBuilder Object
425 flowBuilder.setMatch(MatchUtils.createEtherTypeMatch(matchBuilder, etherType).build());
427 // Create the OF Actions and Instructions
428 InstructionBuilder ib = new InstructionBuilder();
429 InstructionsBuilder isb = new InstructionsBuilder();
431 // Instructions List Stores Individual Instructions
432 List<Instruction> instructions = Lists.newArrayList();
434 // Call the InstructionBuilder Methods Containing Actions
435 InstructionUtils.createSendToControllerInstructions(nodeName, ib);
437 ib.setKey(new InstructionKey(0));
438 instructions.add(ib.build());
440 // Add InstructionBuilder to the Instruction(s)Builder List
441 isb.setInstruction(instructions);
443 // Add InstructionsBuilder to FlowBuilder
444 flowBuilder.setInstructions(isb.build());
446 String flowId = "LLDP";
447 flowBuilder.setId(new FlowId(flowId));
448 FlowKey key = new FlowKey(new FlowId(flowId));
449 flowBuilder.setBarrier(true);
450 flowBuilder.setTableId(getTable());
451 flowBuilder.setKey(key);
452 flowBuilder.setFlowName(flowId);
453 flowBuilder.setHardTimeout(0);
454 flowBuilder.setIdleTimeout(0);
455 writeFlow(flowBuilder, nodeBuilder);
459 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
460 super.setDependencies(bundleContext.getServiceReference(ClassifierProvider.class.getName()), this);
464 public void setDependencies(Object impl) {}