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 java.util.concurrent.Executors;
16 import java.util.concurrent.LinkedBlockingQueue;
17 import org.opendaylight.ovsdb.openstack.netvirt.api.ClassifierProvider;
18 import org.opendaylight.ovsdb.openstack.netvirt.api.RoutingProvider;
19 import org.opendaylight.ovsdb.openstack.netvirt.providers.ConfigInterface;
20 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.AbstractServiceInstance;
21 import org.opendaylight.ovsdb.openstack.netvirt.providers.openflow13.Service;
22 import org.opendaylight.ovsdb.utils.mdsal.openflow.ActionUtils;
23 import org.opendaylight.ovsdb.utils.mdsal.openflow.InstructionUtils;
24 import org.opendaylight.ovsdb.utils.mdsal.openflow.MatchUtils;
25 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.yang.types.rev100924.MacAddress;
26 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.Action;
27 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionBuilder;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.action.types.rev131112.action.list.ActionKey;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.FlowId;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowBuilder;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.inventory.rev130819.tables.table.FlowKey;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.InstructionsBuilder;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.flow.MatchBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCase;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.ApplyActionsCaseBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.instruction.apply.actions._case.ApplyActionsBuilder;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.Instruction;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.rev131026.instruction.list.InstructionKey;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.nodes.NodeBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.EtherType;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.l2.types.rev130827.VlanId;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowjava.nx.match.rev140421.NxmNxReg0;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.openflowplugin.extension.nicira.action.rev140714.dst.choice.grouping.dst.choice.DstNxRegCaseBuilder;
47 import com.google.common.collect.Lists;
48 import org.osgi.framework.BundleContext;
49 import org.osgi.framework.ServiceReference;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 public class ClassifierService extends AbstractServiceInstance implements ClassifierProvider, ConfigInterface {
54 public final static long REG_VALUE_FROM_LOCAL = 0x1L;
55 public final static long REG_VALUE_FROM_REMOTE = 0x2L;
56 public static final Class<? extends NxmNxReg> REG_FIELD = NxmNxReg0.class;
58 public ClassifierService() {
59 super(Service.CLASSIFIER);
62 public ClassifierService(Service service) {
66 private static final Logger logger = LoggerFactory.getLogger(ClassifierService.class);
69 * (Table:Classifier) Egress VM Traffic Towards TEP
70 * Match: Destination Ethernet Addr and OpenFlow InPort
71 * Instruction: Set TunnelID and GOTO Table Tunnel Table (n)
72 * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
73 * actions=set_field:5->tun_id,goto_table=<next-table>"
77 public void programLocalInPort(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
78 String nodeName = OPENFLOW + dpidLong;
80 MatchBuilder matchBuilder = new MatchBuilder();
81 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
82 FlowBuilder flowBuilder = new FlowBuilder();
84 // Create the OF Match using MatchBuilder
85 flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
86 // TODO Broken In_Port Match
87 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
89 String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
90 // Add Flow Attributes
91 flowBuilder.setId(new FlowId(flowId));
92 FlowKey key = new FlowKey(new FlowId(flowId));
93 flowBuilder.setStrict(true);
94 flowBuilder.setBarrier(false);
95 flowBuilder.setTableId(getTable());
96 flowBuilder.setKey(key);
97 flowBuilder.setFlowName(flowId);
98 flowBuilder.setHardTimeout(0);
99 flowBuilder.setIdleTimeout(0);
102 // Instantiate the Builders for the OF Actions and Instructions
103 InstructionBuilder ib = new InstructionBuilder();
104 InstructionsBuilder isb = new InstructionsBuilder();
106 // Instructions List Stores Individual Instructions
107 List<Instruction> instructions = Lists.newArrayList();
109 // TODO Broken SetTunID
110 InstructionUtils.createSetTunnelIdInstructions(ib, new BigInteger(segmentationId));
111 ApplyActionsCase aac = (ApplyActionsCase) ib.getInstruction();
112 List<Action> actionList = aac.getApplyActions().getAction();
114 ActionBuilder ab = new ActionBuilder();
115 ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
116 BigInteger.valueOf(REG_VALUE_FROM_LOCAL)));
118 ab.setKey(new ActionKey(1));
119 actionList.add(ab.build());
122 ib.setKey(new InstructionKey(0));
123 instructions.add(ib.build());
125 // Next service GOTO Instructions Need to be appended to the List
126 ib = this.getMutablePipelineInstructionBuilder();
128 ib.setKey(new InstructionKey(1));
129 instructions.add(ib.build());
131 // Add InstructionBuilder to the Instruction(s)Builder List
132 isb.setInstruction(instructions);
134 // Add InstructionsBuilder to FlowBuilder
135 flowBuilder.setInstructions(isb.build());
137 writeFlow(flowBuilder, nodeBuilder);
139 removeFlow(flowBuilder, nodeBuilder);
144 * (Table:Classifier) Egress VM Traffic Towards TEP
145 * Match: Source Ethernet Addr and OpenFlow InPort
146 * Instruction: Set VLANID and GOTO Table Egress (n)
147 * table=0,in_port=2,dl_src=00:00:00:00:00:01 \
148 * actions=push_vlan, set_field:5->vlan_id,goto_table=<Next-Table>"
149 * 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
153 public void programLocalInPortSetVlan(Long dpidLong, String segmentationId, Long inPort, String attachedMac, boolean write) {
155 String nodeName = OPENFLOW + dpidLong;
157 MatchBuilder matchBuilder = new MatchBuilder();
158 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
159 FlowBuilder flowBuilder = new FlowBuilder();
161 // Create the OF Match using MatchBuilder
162 flowBuilder.setMatch(MatchUtils.createEthSrcMatch(matchBuilder, new MacAddress(attachedMac)).build());
163 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
164 /* openflowplugin requires a vlan match to add a vlan */
165 flowBuilder.setMatch(MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(0), false).build());
167 String flowId = "LocalMac_"+segmentationId+"_"+inPort+"_"+attachedMac;
168 // Add Flow Attributes
169 flowBuilder.setId(new FlowId(flowId));
170 FlowKey key = new FlowKey(new FlowId(flowId));
171 flowBuilder.setStrict(true);
172 flowBuilder.setBarrier(false);
173 flowBuilder.setTableId(getTable());
174 flowBuilder.setKey(key);
175 flowBuilder.setFlowName(flowId);
176 flowBuilder.setHardTimeout(0);
177 flowBuilder.setIdleTimeout(0);
180 // Instantiate the Builders for the OF Actions and Instructions
181 InstructionBuilder ib = new InstructionBuilder();
182 InstructionsBuilder isb = new InstructionsBuilder();
184 // Instructions List Stores Individual Instructions
185 List<Instruction> instructions = Lists.newArrayList();
187 // Set VLAN ID Instruction
188 InstructionUtils.createSetVlanInstructions(ib, new VlanId(Integer.valueOf(segmentationId)));
190 ib.setKey(new InstructionKey(0));
191 instructions.add(ib.build());
193 // Next service GOTO Instructions Need to be appended to the List
194 ib = this.getMutablePipelineInstructionBuilder();
196 ib.setKey(new InstructionKey(1));
197 instructions.add(ib.build());
199 // Add InstructionBuilder to the Instruction(s)Builder List
200 isb.setInstruction(instructions);
202 // Add InstructionsBuilder to FlowBuilder
203 flowBuilder.setInstructions(isb.build());
205 writeFlow(flowBuilder, nodeBuilder);
207 removeFlow(flowBuilder, nodeBuilder);
212 * (Table:Classifier) Drop frames source from a VM that do not
213 * match the associated MAC address of the local VM.
214 * Match: Low priority anything not matching the VM SMAC
216 * table=0,priority=16384,in_port=1 actions=drop"
220 public void programDropSrcIface(Long dpidLong, Long inPort, boolean write) {
222 String nodeName = OPENFLOW + dpidLong;
224 MatchBuilder matchBuilder = new MatchBuilder();
225 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
226 FlowBuilder flowBuilder = new FlowBuilder();
228 // Create the OF Match using MatchBuilder
229 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, inPort).build());
232 // Instantiate the Builders for the OF Actions and Instructions
233 InstructionBuilder ib = new InstructionBuilder();
234 InstructionsBuilder isb = new InstructionsBuilder();
236 // Instructions List Stores Individual Instructions
237 List<Instruction> instructions = Lists.newArrayList();
239 // Call the InstructionBuilder Methods Containing Actions
240 InstructionUtils.createDropInstructions(ib);
242 ib.setKey(new InstructionKey(0));
243 instructions.add(ib.build());
245 // Add InstructionBuilder to the Instruction(s)Builder List
246 isb.setInstruction(instructions);
248 // Add InstructionsBuilder to FlowBuilder
249 flowBuilder.setInstructions(isb.build());
252 String flowId = "DropFilter_"+inPort;
253 // Add Flow Attributes
254 flowBuilder.setId(new FlowId(flowId));
255 FlowKey key = new FlowKey(new FlowId(flowId));
256 flowBuilder.setStrict(true);
257 flowBuilder.setBarrier(false);
258 flowBuilder.setTableId(getTable());
259 flowBuilder.setKey(key);
260 flowBuilder.setFlowName(flowId);
261 flowBuilder.setPriority(8192);
262 flowBuilder.setHardTimeout(0);
263 flowBuilder.setIdleTimeout(0);
265 writeFlow(flowBuilder, nodeBuilder);
267 removeFlow(flowBuilder, nodeBuilder);
271 * (Table:0) Ingress Tunnel Traffic
272 * Match: OpenFlow InPort and Tunnel ID
273 * Action: GOTO Local Table (10)
274 * table=0,tun_id=0x5,in_port=10, actions=goto_table:2
278 public void programTunnelIn(Long dpidLong, String segmentationId,
279 Long ofPort, boolean write) {
281 String nodeName = OPENFLOW + dpidLong;
283 BigInteger tunnelId = new BigInteger(segmentationId);
284 MatchBuilder matchBuilder = new MatchBuilder();
285 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
286 FlowBuilder flowBuilder = new FlowBuilder();
288 // Create Match(es) and Set them in the FlowBuilder Object
289 flowBuilder.setMatch(MatchUtils.createTunnelIDMatch(matchBuilder, tunnelId).build());
290 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ofPort).build());
293 // Create the OF Actions and Instructions
294 InstructionBuilder ib = new InstructionBuilder();
295 InstructionsBuilder isb = new InstructionsBuilder();
297 // Instructions List Stores Individual Instructions
298 List<Instruction> instructions = Lists.newArrayList();
300 List<Action> actionList = Lists.newArrayList();
301 ActionBuilder ab = new ActionBuilder();
302 ab.setAction(ActionUtils.nxLoadRegAction(new DstNxRegCaseBuilder().setNxReg(REG_FIELD).build(),
303 BigInteger.valueOf(REG_VALUE_FROM_REMOTE)));
305 ab.setKey(new ActionKey(0));
306 actionList.add(ab.build());
308 ApplyActionsBuilder aab = new ApplyActionsBuilder();
309 aab.setAction(actionList);
310 ib.setInstruction(new ApplyActionsCaseBuilder().setApplyActions(aab.build()).build());
312 // Call the InstructionBuilder Methods Containing Actions
314 ib.setKey(new InstructionKey(0));
315 instructions.add(ib.build());
317 // Append the default pipeline after the first classification
318 ib = this.getMutablePipelineInstructionBuilder();
320 ib.setKey(new InstructionKey(1));
321 instructions.add(ib.build());
323 // Add InstructionBuilder to the Instruction(s)Builder List
324 isb.setInstruction(instructions);
326 // Add InstructionsBuilder to FlowBuilder
327 flowBuilder.setInstructions(isb.build());
330 String flowId = "TunnelIn_"+segmentationId+"_"+ofPort;
331 // Add Flow Attributes
332 flowBuilder.setId(new FlowId(flowId));
333 FlowKey key = new FlowKey(new FlowId(flowId));
334 flowBuilder.setStrict(true);
335 flowBuilder.setBarrier(false);
336 flowBuilder.setTableId(getTable());
337 flowBuilder.setKey(key);
338 flowBuilder.setFlowName(flowId);
339 flowBuilder.setHardTimeout(0);
340 flowBuilder.setIdleTimeout(0);
343 writeFlow(flowBuilder, nodeBuilder);
345 removeFlow(flowBuilder, nodeBuilder);
350 * (Table:0) Ingress VLAN Traffic
351 * Match: OpenFlow InPort and vlan ID
352 * Action: GOTO Local Table (20)
353 * table=0,vlan_id=0x5,in_port=10, actions=goto_table:2
354 * table=0,in_port=2,dl_vlan=2001 actions=goto_table:20
358 public void programVlanIn(Long dpidLong, String segmentationId, Long ethPort, boolean write) {
360 String nodeName = OPENFLOW + dpidLong;
362 MatchBuilder matchBuilder = new MatchBuilder();
363 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
364 FlowBuilder flowBuilder = new FlowBuilder();
366 // Create Match(es) and Set them in the FlowBuilder Object
367 flowBuilder.setMatch(MatchUtils.createVlanIdMatch(matchBuilder, new VlanId(Integer.valueOf(segmentationId)), true).build());
368 flowBuilder.setMatch(MatchUtils.createInPortMatch(matchBuilder, dpidLong, ethPort).build());
371 // Create the OF Actions and Instructions
372 InstructionBuilder ib = new InstructionBuilder();
373 InstructionsBuilder isb = new InstructionsBuilder();
375 // Instructions List Stores Individual Instructions
376 List<Instruction> instructions = Lists.newArrayList();
378 // Append the default pipeline after the first classification
379 ib = this.getMutablePipelineInstructionBuilder();
381 ib.setKey(new InstructionKey(0));
382 instructions.add(ib.build());
384 // Add InstructionBuilder to the Instruction(s)Builder List
385 isb.setInstruction(instructions);
387 // Add InstructionsBuilder to FlowBuilder
388 flowBuilder.setInstructions(isb.build());
391 String flowId = "VlanIn_"+segmentationId+"_"+ethPort;
392 // Add Flow Attributes
393 flowBuilder.setId(new FlowId(flowId));
394 FlowKey key = new FlowKey(new FlowId(flowId));
395 flowBuilder.setStrict(true);
396 flowBuilder.setBarrier(false);
397 flowBuilder.setTableId(getTable());
398 flowBuilder.setKey(key);
399 flowBuilder.setFlowName(flowId);
400 flowBuilder.setHardTimeout(0);
401 flowBuilder.setIdleTimeout(0);
403 writeFlow(flowBuilder, nodeBuilder);
405 removeFlow(flowBuilder, nodeBuilder);
410 * Create an LLDP Flow Rule to encapsulate into
411 * a packet_in that is sent to the controller
412 * for topology handling.
413 * Match: Ethertype 0x88CCL
414 * Action: Punt to Controller in a Packet_In msg
418 public void programLLDPPuntRule(Long dpidLong) {
420 String nodeName = OPENFLOW + dpidLong;
421 EtherType etherType = new EtherType(0x88CCL);
423 MatchBuilder matchBuilder = new MatchBuilder();
424 NodeBuilder nodeBuilder = createNodeBuilder(nodeName);
425 FlowBuilder flowBuilder = new FlowBuilder();
427 // Create Match(es) and Set them in the FlowBuilder Object
428 flowBuilder.setMatch(MatchUtils.createEtherTypeMatch(matchBuilder, etherType).build());
430 // Create the OF Actions and Instructions
431 InstructionBuilder ib = new InstructionBuilder();
432 InstructionsBuilder isb = new InstructionsBuilder();
434 // Instructions List Stores Individual Instructions
435 List<Instruction> instructions = Lists.newArrayList();
437 // Call the InstructionBuilder Methods Containing Actions
438 InstructionUtils.createSendToControllerInstructions(nodeName, ib);
440 ib.setKey(new InstructionKey(0));
441 instructions.add(ib.build());
443 // Add InstructionBuilder to the Instruction(s)Builder List
444 isb.setInstruction(instructions);
446 // Add InstructionsBuilder to FlowBuilder
447 flowBuilder.setInstructions(isb.build());
449 String flowId = "LLDP";
450 flowBuilder.setId(new FlowId(flowId));
451 FlowKey key = new FlowKey(new FlowId(flowId));
452 flowBuilder.setBarrier(true);
453 flowBuilder.setTableId(getTable());
454 flowBuilder.setKey(key);
455 flowBuilder.setFlowName(flowId);
456 flowBuilder.setHardTimeout(0);
457 flowBuilder.setIdleTimeout(0);
458 writeFlow(flowBuilder, nodeBuilder);
462 public void setDependencies(BundleContext bundleContext, ServiceReference serviceReference) {
463 super.setOrchestrator(bundleContext.getServiceReference(ClassifierProvider.class.getName()), this);
467 public void setDependencies(Object impl) {}